ide-cd.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. /*
  2. * ATAPI CD-ROM driver.
  3. *
  4. * Copyright (C) 1994-1996 Scott Snyder <snyder@fnald0.fnal.gov>
  5. * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
  6. * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
  7. * Copyright (C) 2005, 2007-2009 Bartlomiej Zolnierkiewicz
  8. *
  9. * May be copied or modified under the terms of the GNU General Public
  10. * License. See linux/COPYING for more information.
  11. *
  12. * See Documentation/cdrom/ide-cd for usage information.
  13. *
  14. * Suggestions are welcome. Patches that work are more welcome though. ;-)
  15. *
  16. * Documentation:
  17. * Mt. Fuji (SFF8090 version 4) and ATAPI (SFF-8020i rev 2.6) standards.
  18. *
  19. * For historical changelog please see:
  20. * Documentation/ide/ChangeLog.ide-cd.1994-2004
  21. */
  22. #define DRV_NAME "ide-cd"
  23. #define PFX DRV_NAME ": "
  24. #define IDECD_VERSION "5.00"
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/sched/task_stack.h>
  29. #include <linux/delay.h>
  30. #include <linux/timer.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/errno.h>
  35. #include <linux/cdrom.h>
  36. #include <linux/ide.h>
  37. #include <linux/completion.h>
  38. #include <linux/mutex.h>
  39. #include <linux/bcd.h>
  40. /* For SCSI -> ATAPI command conversion */
  41. #include <scsi/scsi.h>
  42. #include <linux/io.h>
  43. #include <asm/byteorder.h>
  44. #include <linux/uaccess.h>
  45. #include <asm/unaligned.h>
  46. #include "ide-cd.h"
  47. static DEFINE_MUTEX(ide_cd_mutex);
  48. static DEFINE_MUTEX(idecd_ref_mutex);
  49. static void ide_cd_release(struct device *);
  50. static struct cdrom_info *ide_cd_get(struct gendisk *disk)
  51. {
  52. struct cdrom_info *cd = NULL;
  53. mutex_lock(&idecd_ref_mutex);
  54. cd = ide_drv_g(disk, cdrom_info);
  55. if (cd) {
  56. if (ide_device_get(cd->drive))
  57. cd = NULL;
  58. else
  59. get_device(&cd->dev);
  60. }
  61. mutex_unlock(&idecd_ref_mutex);
  62. return cd;
  63. }
  64. static void ide_cd_put(struct cdrom_info *cd)
  65. {
  66. ide_drive_t *drive = cd->drive;
  67. mutex_lock(&idecd_ref_mutex);
  68. put_device(&cd->dev);
  69. ide_device_put(drive);
  70. mutex_unlock(&idecd_ref_mutex);
  71. }
  72. /*
  73. * Generic packet command support and error handling routines.
  74. */
  75. /* Mark that we've seen a media change and invalidate our internal buffers. */
  76. static void cdrom_saw_media_change(ide_drive_t *drive)
  77. {
  78. drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
  79. drive->atapi_flags &= ~IDE_AFLAG_TOC_VALID;
  80. }
  81. static int cdrom_log_sense(ide_drive_t *drive, struct request *rq)
  82. {
  83. struct request_sense *sense = &drive->sense_data;
  84. int log = 0;
  85. if (!sense || !rq || (rq->rq_flags & RQF_QUIET))
  86. return 0;
  87. ide_debug_log(IDE_DBG_SENSE, "sense_key: 0x%x", sense->sense_key);
  88. switch (sense->sense_key) {
  89. case NO_SENSE:
  90. case RECOVERED_ERROR:
  91. break;
  92. case NOT_READY:
  93. /*
  94. * don't care about tray state messages for e.g. capacity
  95. * commands or in-progress or becoming ready
  96. */
  97. if (sense->asc == 0x3a || sense->asc == 0x04)
  98. break;
  99. log = 1;
  100. break;
  101. case ILLEGAL_REQUEST:
  102. /*
  103. * don't log START_STOP unit with LoEj set, since we cannot
  104. * reliably check if drive can auto-close
  105. */
  106. if (scsi_req(rq)->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
  107. break;
  108. log = 1;
  109. break;
  110. case UNIT_ATTENTION:
  111. /*
  112. * Make good and sure we've seen this potential media change.
  113. * Some drives (i.e. Creative) fail to present the correct sense
  114. * key in the error register.
  115. */
  116. cdrom_saw_media_change(drive);
  117. break;
  118. default:
  119. log = 1;
  120. break;
  121. }
  122. return log;
  123. }
  124. static void cdrom_analyze_sense_data(ide_drive_t *drive,
  125. struct request *failed_command)
  126. {
  127. struct request_sense *sense = &drive->sense_data;
  128. struct cdrom_info *info = drive->driver_data;
  129. unsigned long sector;
  130. unsigned long bio_sectors;
  131. ide_debug_log(IDE_DBG_SENSE, "error_code: 0x%x, sense_key: 0x%x",
  132. sense->error_code, sense->sense_key);
  133. if (failed_command)
  134. ide_debug_log(IDE_DBG_SENSE, "failed cmd: 0x%x",
  135. failed_command->cmd[0]);
  136. if (!cdrom_log_sense(drive, failed_command))
  137. return;
  138. /*
  139. * If a read toc is executed for a CD-R or CD-RW medium where the first
  140. * toc has not been recorded yet, it will fail with 05/24/00 (which is a
  141. * confusing error)
  142. */
  143. if (failed_command && scsi_req(failed_command)->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
  144. if (sense->sense_key == 0x05 && sense->asc == 0x24)
  145. return;
  146. /* current error */
  147. if (sense->error_code == 0x70) {
  148. switch (sense->sense_key) {
  149. case MEDIUM_ERROR:
  150. case VOLUME_OVERFLOW:
  151. case ILLEGAL_REQUEST:
  152. if (!sense->valid)
  153. break;
  154. if (failed_command == NULL ||
  155. blk_rq_is_passthrough(failed_command))
  156. break;
  157. sector = (sense->information[0] << 24) |
  158. (sense->information[1] << 16) |
  159. (sense->information[2] << 8) |
  160. (sense->information[3]);
  161. if (queue_logical_block_size(drive->queue) == 2048)
  162. /* device sector size is 2K */
  163. sector <<= 2;
  164. bio_sectors = max(bio_sectors(failed_command->bio), 4U);
  165. sector &= ~(bio_sectors - 1);
  166. /*
  167. * The SCSI specification allows for the value
  168. * returned by READ CAPACITY to be up to 75 2K
  169. * sectors past the last readable block.
  170. * Therefore, if we hit a medium error within the
  171. * last 75 2K sectors, we decrease the saved size
  172. * value.
  173. */
  174. if (sector < get_capacity(info->disk) &&
  175. drive->probed_capacity - sector < 4 * 75)
  176. set_capacity(info->disk, sector);
  177. }
  178. }
  179. ide_cd_log_error(drive->name, failed_command, sense);
  180. }
  181. static void ide_cd_complete_failed_rq(ide_drive_t *drive, struct request *rq)
  182. {
  183. /*
  184. * For ATA_PRIV_SENSE, "rq->special" points to the original
  185. * failed request. Also, the sense data should be read
  186. * directly from rq which might be different from the original
  187. * sense buffer if it got copied during mapping.
  188. */
  189. struct request *failed = (struct request *)rq->special;
  190. void *sense = bio_data(rq->bio);
  191. if (failed) {
  192. /*
  193. * Sense is always read into drive->sense_data, copy back to the
  194. * original request.
  195. */
  196. memcpy(scsi_req(failed)->sense, sense, 18);
  197. scsi_req(failed)->sense_len = scsi_req(rq)->sense_len;
  198. cdrom_analyze_sense_data(drive, failed);
  199. if (ide_end_rq(drive, failed, BLK_STS_IOERR, blk_rq_bytes(failed)))
  200. BUG();
  201. } else
  202. cdrom_analyze_sense_data(drive, NULL);
  203. }
  204. /*
  205. * Allow the drive 5 seconds to recover; some devices will return NOT_READY
  206. * while flushing data from cache.
  207. *
  208. * returns: 0 failed (write timeout expired)
  209. * 1 success
  210. */
  211. static int ide_cd_breathe(ide_drive_t *drive, struct request *rq)
  212. {
  213. struct cdrom_info *info = drive->driver_data;
  214. if (!scsi_req(rq)->result)
  215. info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
  216. scsi_req(rq)->result = 1;
  217. if (time_after(jiffies, info->write_timeout))
  218. return 0;
  219. else {
  220. /*
  221. * take a breather
  222. */
  223. blk_delay_queue(drive->queue, 1);
  224. return 1;
  225. }
  226. }
  227. /**
  228. * Returns:
  229. * 0: if the request should be continued.
  230. * 1: if the request will be going through error recovery.
  231. * 2: if the request should be ended.
  232. */
  233. static int cdrom_decode_status(ide_drive_t *drive, u8 stat)
  234. {
  235. ide_hwif_t *hwif = drive->hwif;
  236. struct request *rq = hwif->rq;
  237. int err, sense_key, do_end_request = 0;
  238. /* get the IDE error register */
  239. err = ide_read_error(drive);
  240. sense_key = err >> 4;
  241. ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, rq->cmd_type: 0x%x, err: 0x%x, "
  242. "stat 0x%x",
  243. rq->cmd[0], rq->cmd_type, err, stat);
  244. if (ata_sense_request(rq)) {
  245. /*
  246. * We got an error trying to get sense info from the drive
  247. * (probably while trying to recover from a former error).
  248. * Just give up.
  249. */
  250. rq->rq_flags |= RQF_FAILED;
  251. return 2;
  252. }
  253. /* if we have an error, pass CHECK_CONDITION as the SCSI status byte */
  254. if (blk_rq_is_scsi(rq) && !scsi_req(rq)->result)
  255. scsi_req(rq)->result = SAM_STAT_CHECK_CONDITION;
  256. if (blk_noretry_request(rq))
  257. do_end_request = 1;
  258. switch (sense_key) {
  259. case NOT_READY:
  260. if (req_op(rq) == REQ_OP_WRITE) {
  261. if (ide_cd_breathe(drive, rq))
  262. return 1;
  263. } else {
  264. cdrom_saw_media_change(drive);
  265. if (!blk_rq_is_passthrough(rq) &&
  266. !(rq->rq_flags & RQF_QUIET))
  267. printk(KERN_ERR PFX "%s: tray open\n",
  268. drive->name);
  269. }
  270. do_end_request = 1;
  271. break;
  272. case UNIT_ATTENTION:
  273. cdrom_saw_media_change(drive);
  274. if (blk_rq_is_passthrough(rq))
  275. return 0;
  276. /*
  277. * Arrange to retry the request but be sure to give up if we've
  278. * retried too many times.
  279. */
  280. if (++scsi_req(rq)->result > ERROR_MAX)
  281. do_end_request = 1;
  282. break;
  283. case ILLEGAL_REQUEST:
  284. /*
  285. * Don't print error message for this condition -- SFF8090i
  286. * indicates that 5/24/00 is the correct response to a request
  287. * to close the tray if the drive doesn't have that capability.
  288. *
  289. * cdrom_log_sense() knows this!
  290. */
  291. if (scsi_req(rq)->cmd[0] == GPCMD_START_STOP_UNIT)
  292. break;
  293. /* fall-through */
  294. case DATA_PROTECT:
  295. /*
  296. * No point in retrying after an illegal request or data
  297. * protect error.
  298. */
  299. if (!(rq->rq_flags & RQF_QUIET))
  300. ide_dump_status(drive, "command error", stat);
  301. do_end_request = 1;
  302. break;
  303. case MEDIUM_ERROR:
  304. /*
  305. * No point in re-trying a zillion times on a bad sector.
  306. * If we got here the error is not correctable.
  307. */
  308. if (!(rq->rq_flags & RQF_QUIET))
  309. ide_dump_status(drive, "media error "
  310. "(bad sector)", stat);
  311. do_end_request = 1;
  312. break;
  313. case BLANK_CHECK:
  314. /* disk appears blank? */
  315. if (!(rq->rq_flags & RQF_QUIET))
  316. ide_dump_status(drive, "media error (blank)",
  317. stat);
  318. do_end_request = 1;
  319. break;
  320. default:
  321. if (blk_rq_is_passthrough(rq))
  322. break;
  323. if (err & ~ATA_ABORTED) {
  324. /* go to the default handler for other errors */
  325. ide_error(drive, "cdrom_decode_status", stat);
  326. return 1;
  327. } else if (++scsi_req(rq)->result > ERROR_MAX)
  328. /* we've racked up too many retries, abort */
  329. do_end_request = 1;
  330. }
  331. if (blk_rq_is_passthrough(rq)) {
  332. rq->rq_flags |= RQF_FAILED;
  333. do_end_request = 1;
  334. }
  335. /*
  336. * End a request through request sense analysis when we have sense data.
  337. * We need this in order to perform end of media processing.
  338. */
  339. if (do_end_request)
  340. goto end_request;
  341. /* if we got a CHECK_CONDITION status, queue a request sense command */
  342. if (stat & ATA_ERR)
  343. return ide_queue_sense_rq(drive, NULL) ? 2 : 1;
  344. return 1;
  345. end_request:
  346. if (stat & ATA_ERR) {
  347. hwif->rq = NULL;
  348. return ide_queue_sense_rq(drive, rq) ? 2 : 1;
  349. } else
  350. return 2;
  351. }
  352. static void ide_cd_request_sense_fixup(ide_drive_t *drive, struct ide_cmd *cmd)
  353. {
  354. struct request *rq = cmd->rq;
  355. ide_debug_log(IDE_DBG_FUNC, "rq->cmd[0]: 0x%x", rq->cmd[0]);
  356. /*
  357. * Some of the trailing request sense fields are optional,
  358. * and some drives don't send them. Sigh.
  359. */
  360. if (scsi_req(rq)->cmd[0] == GPCMD_REQUEST_SENSE &&
  361. cmd->nleft > 0 && cmd->nleft <= 5)
  362. cmd->nleft = 0;
  363. }
  364. int ide_cd_queue_pc(ide_drive_t *drive, const unsigned char *cmd,
  365. int write, void *buffer, unsigned *bufflen,
  366. struct scsi_sense_hdr *sshdr, int timeout,
  367. req_flags_t rq_flags)
  368. {
  369. struct cdrom_info *info = drive->driver_data;
  370. struct scsi_sense_hdr local_sshdr;
  371. int retries = 10;
  372. bool failed;
  373. ide_debug_log(IDE_DBG_PC, "cmd[0]: 0x%x, write: 0x%x, timeout: %d, "
  374. "rq_flags: 0x%x",
  375. cmd[0], write, timeout, rq_flags);
  376. if (!sshdr)
  377. sshdr = &local_sshdr;
  378. /* start of retry loop */
  379. do {
  380. struct request *rq;
  381. int error;
  382. bool delay = false;
  383. rq = blk_get_request(drive->queue,
  384. write ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
  385. memcpy(scsi_req(rq)->cmd, cmd, BLK_MAX_CDB);
  386. ide_req(rq)->type = ATA_PRIV_PC;
  387. rq->rq_flags |= rq_flags;
  388. rq->timeout = timeout;
  389. if (buffer) {
  390. error = blk_rq_map_kern(drive->queue, rq, buffer,
  391. *bufflen, GFP_NOIO);
  392. if (error) {
  393. blk_put_request(rq);
  394. return error;
  395. }
  396. }
  397. blk_execute_rq(drive->queue, info->disk, rq, 0);
  398. error = scsi_req(rq)->result ? -EIO : 0;
  399. if (buffer)
  400. *bufflen = scsi_req(rq)->resid_len;
  401. scsi_normalize_sense(scsi_req(rq)->sense,
  402. scsi_req(rq)->sense_len, sshdr);
  403. /*
  404. * FIXME: we should probably abort/retry or something in case of
  405. * failure.
  406. */
  407. failed = (rq->rq_flags & RQF_FAILED) != 0;
  408. if (failed) {
  409. /*
  410. * The request failed. Retry if it was due to a unit
  411. * attention status (usually means media was changed).
  412. */
  413. if (sshdr->sense_key == UNIT_ATTENTION)
  414. cdrom_saw_media_change(drive);
  415. else if (sshdr->sense_key == NOT_READY &&
  416. sshdr->asc == 4 && sshdr->ascq != 4) {
  417. /*
  418. * The drive is in the process of loading
  419. * a disk. Retry, but wait a little to give
  420. * the drive time to complete the load.
  421. */
  422. delay = true;
  423. } else {
  424. /* otherwise, don't retry */
  425. retries = 0;
  426. }
  427. --retries;
  428. }
  429. blk_put_request(rq);
  430. if (delay)
  431. ssleep(2);
  432. } while (failed && retries >= 0);
  433. /* return an error if the command failed */
  434. return failed ? -EIO : 0;
  435. }
  436. /*
  437. * returns true if rq has been completed
  438. */
  439. static bool ide_cd_error_cmd(ide_drive_t *drive, struct ide_cmd *cmd)
  440. {
  441. unsigned int nr_bytes = cmd->nbytes - cmd->nleft;
  442. if (cmd->tf_flags & IDE_TFLAG_WRITE)
  443. nr_bytes -= cmd->last_xfer_len;
  444. if (nr_bytes > 0) {
  445. ide_complete_rq(drive, BLK_STS_OK, nr_bytes);
  446. return true;
  447. }
  448. return false;
  449. }
  450. static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
  451. {
  452. ide_hwif_t *hwif = drive->hwif;
  453. struct ide_cmd *cmd = &hwif->cmd;
  454. struct request *rq = hwif->rq;
  455. ide_expiry_t *expiry = NULL;
  456. int dma_error = 0, dma, thislen, uptodate = 0;
  457. int write = (rq_data_dir(rq) == WRITE) ? 1 : 0, rc = 0;
  458. int sense = ata_sense_request(rq);
  459. unsigned int timeout;
  460. u16 len;
  461. u8 ireason, stat;
  462. ide_debug_log(IDE_DBG_PC, "cmd: 0x%x, write: 0x%x", rq->cmd[0], write);
  463. /* check for errors */
  464. dma = drive->dma;
  465. if (dma) {
  466. drive->dma = 0;
  467. drive->waiting_for_dma = 0;
  468. dma_error = hwif->dma_ops->dma_end(drive);
  469. ide_dma_unmap_sg(drive, cmd);
  470. if (dma_error) {
  471. printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
  472. write ? "write" : "read");
  473. ide_dma_off(drive);
  474. }
  475. }
  476. /* check status */
  477. stat = hwif->tp_ops->read_status(hwif);
  478. if (!OK_STAT(stat, 0, BAD_R_STAT)) {
  479. rc = cdrom_decode_status(drive, stat);
  480. if (rc) {
  481. if (rc == 2)
  482. goto out_end;
  483. return ide_stopped;
  484. }
  485. }
  486. /* using dma, transfer is complete now */
  487. if (dma) {
  488. if (dma_error)
  489. return ide_error(drive, "dma error", stat);
  490. uptodate = 1;
  491. goto out_end;
  492. }
  493. ide_read_bcount_and_ireason(drive, &len, &ireason);
  494. thislen = !blk_rq_is_passthrough(rq) ? len : cmd->nleft;
  495. if (thislen > len)
  496. thislen = len;
  497. ide_debug_log(IDE_DBG_PC, "DRQ: stat: 0x%x, thislen: %d",
  498. stat, thislen);
  499. /* If DRQ is clear, the command has completed. */
  500. if ((stat & ATA_DRQ) == 0) {
  501. switch (req_op(rq)) {
  502. default:
  503. /*
  504. * If we're not done reading/writing, complain.
  505. * Otherwise, complete the command normally.
  506. */
  507. uptodate = 1;
  508. if (cmd->nleft > 0) {
  509. printk(KERN_ERR PFX "%s: %s: data underrun "
  510. "(%u bytes)\n", drive->name, __func__,
  511. cmd->nleft);
  512. if (!write)
  513. rq->rq_flags |= RQF_FAILED;
  514. uptodate = 0;
  515. }
  516. goto out_end;
  517. case REQ_OP_DRV_IN:
  518. case REQ_OP_DRV_OUT:
  519. ide_cd_request_sense_fixup(drive, cmd);
  520. uptodate = cmd->nleft ? 0 : 1;
  521. /*
  522. * suck out the remaining bytes from the drive in an
  523. * attempt to complete the data xfer. (see BZ#13399)
  524. */
  525. if (!(stat & ATA_ERR) && !uptodate && thislen) {
  526. ide_pio_bytes(drive, cmd, write, thislen);
  527. uptodate = cmd->nleft ? 0 : 1;
  528. }
  529. if (!uptodate)
  530. rq->rq_flags |= RQF_FAILED;
  531. goto out_end;
  532. case REQ_OP_SCSI_IN:
  533. case REQ_OP_SCSI_OUT:
  534. goto out_end;
  535. }
  536. }
  537. rc = ide_check_ireason(drive, rq, len, ireason, write);
  538. if (rc)
  539. goto out_end;
  540. cmd->last_xfer_len = 0;
  541. ide_debug_log(IDE_DBG_PC, "data transfer, rq->cmd_type: 0x%x, "
  542. "ireason: 0x%x",
  543. rq->cmd_type, ireason);
  544. /* transfer data */
  545. while (thislen > 0) {
  546. int blen = min_t(int, thislen, cmd->nleft);
  547. if (cmd->nleft == 0)
  548. break;
  549. ide_pio_bytes(drive, cmd, write, blen);
  550. cmd->last_xfer_len += blen;
  551. thislen -= blen;
  552. len -= blen;
  553. if (sense && write == 0)
  554. scsi_req(rq)->sense_len += blen;
  555. }
  556. /* pad, if necessary */
  557. if (len > 0) {
  558. if (blk_rq_is_passthrough(rq) || write == 0)
  559. ide_pad_transfer(drive, write, len);
  560. else {
  561. printk(KERN_ERR PFX "%s: confused, missing data\n",
  562. drive->name);
  563. blk_dump_rq_flags(rq, "cdrom_newpc_intr");
  564. }
  565. }
  566. switch (req_op(rq)) {
  567. case REQ_OP_SCSI_IN:
  568. case REQ_OP_SCSI_OUT:
  569. timeout = rq->timeout;
  570. break;
  571. case REQ_OP_DRV_IN:
  572. case REQ_OP_DRV_OUT:
  573. expiry = ide_cd_expiry;
  574. /*FALLTHRU*/
  575. default:
  576. timeout = ATAPI_WAIT_PC;
  577. break;
  578. }
  579. hwif->expiry = expiry;
  580. ide_set_handler(drive, cdrom_newpc_intr, timeout);
  581. return ide_started;
  582. out_end:
  583. if (blk_rq_is_scsi(rq) && rc == 0) {
  584. scsi_req(rq)->resid_len = 0;
  585. blk_end_request_all(rq, BLK_STS_OK);
  586. hwif->rq = NULL;
  587. } else {
  588. if (sense && uptodate)
  589. ide_cd_complete_failed_rq(drive, rq);
  590. if (!blk_rq_is_passthrough(rq)) {
  591. if (cmd->nleft == 0)
  592. uptodate = 1;
  593. } else {
  594. if (uptodate <= 0 && scsi_req(rq)->result == 0)
  595. scsi_req(rq)->result = -EIO;
  596. }
  597. if (uptodate == 0 && rq->bio)
  598. if (ide_cd_error_cmd(drive, cmd))
  599. return ide_stopped;
  600. /* make sure it's fully ended */
  601. if (blk_rq_is_passthrough(rq)) {
  602. scsi_req(rq)->resid_len -= cmd->nbytes - cmd->nleft;
  603. if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE))
  604. scsi_req(rq)->resid_len += cmd->last_xfer_len;
  605. }
  606. ide_complete_rq(drive, uptodate ? BLK_STS_OK : BLK_STS_IOERR, blk_rq_bytes(rq));
  607. if (sense && rc == 2)
  608. ide_error(drive, "request sense failure", stat);
  609. }
  610. return ide_stopped;
  611. }
  612. static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
  613. {
  614. struct cdrom_info *cd = drive->driver_data;
  615. struct request_queue *q = drive->queue;
  616. int write = rq_data_dir(rq) == WRITE;
  617. unsigned short sectors_per_frame =
  618. queue_logical_block_size(q) >> SECTOR_SHIFT;
  619. ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, rq->cmd_flags: 0x%x, "
  620. "secs_per_frame: %u",
  621. rq->cmd[0], rq->cmd_flags, sectors_per_frame);
  622. if (write) {
  623. /* disk has become write protected */
  624. if (get_disk_ro(cd->disk))
  625. return ide_stopped;
  626. } else {
  627. /*
  628. * We may be retrying this request after an error. Fix up any
  629. * weirdness which might be present in the request packet.
  630. */
  631. q->prep_rq_fn(q, rq);
  632. }
  633. /* fs requests *must* be hardware frame aligned */
  634. if ((blk_rq_sectors(rq) & (sectors_per_frame - 1)) ||
  635. (blk_rq_pos(rq) & (sectors_per_frame - 1)))
  636. return ide_stopped;
  637. /* use DMA, if possible */
  638. drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  639. if (write)
  640. cd->devinfo.media_written = 1;
  641. rq->timeout = ATAPI_WAIT_PC;
  642. return ide_started;
  643. }
  644. static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
  645. {
  646. ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x",
  647. rq->cmd[0], rq->cmd_type);
  648. if (blk_rq_is_scsi(rq))
  649. rq->rq_flags |= RQF_QUIET;
  650. else
  651. rq->rq_flags &= ~RQF_FAILED;
  652. drive->dma = 0;
  653. /* sg request */
  654. if (rq->bio) {
  655. struct request_queue *q = drive->queue;
  656. char *buf = bio_data(rq->bio);
  657. unsigned int alignment;
  658. drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  659. /*
  660. * check if dma is safe
  661. *
  662. * NOTE! The "len" and "addr" checks should possibly have
  663. * separate masks.
  664. */
  665. alignment = queue_dma_alignment(q) | q->dma_pad_mask;
  666. if ((unsigned long)buf & alignment
  667. || blk_rq_bytes(rq) & q->dma_pad_mask
  668. || object_is_on_stack(buf))
  669. drive->dma = 0;
  670. }
  671. }
  672. static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
  673. sector_t block)
  674. {
  675. struct ide_cmd cmd;
  676. int uptodate = 0;
  677. unsigned int nsectors;
  678. ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, block: %llu",
  679. rq->cmd[0], (unsigned long long)block);
  680. if (drive->debug_mask & IDE_DBG_RQ)
  681. blk_dump_rq_flags(rq, "ide_cd_do_request");
  682. switch (req_op(rq)) {
  683. default:
  684. if (cdrom_start_rw(drive, rq) == ide_stopped)
  685. goto out_end;
  686. break;
  687. case REQ_OP_SCSI_IN:
  688. case REQ_OP_SCSI_OUT:
  689. handle_pc:
  690. if (!rq->timeout)
  691. rq->timeout = ATAPI_WAIT_PC;
  692. cdrom_do_block_pc(drive, rq);
  693. break;
  694. case REQ_OP_DRV_IN:
  695. case REQ_OP_DRV_OUT:
  696. switch (ide_req(rq)->type) {
  697. case ATA_PRIV_MISC:
  698. /* right now this can only be a reset... */
  699. uptodate = 1;
  700. goto out_end;
  701. case ATA_PRIV_SENSE:
  702. case ATA_PRIV_PC:
  703. goto handle_pc;
  704. default:
  705. BUG();
  706. }
  707. }
  708. /* prepare sense request for this command */
  709. ide_prep_sense(drive, rq);
  710. memset(&cmd, 0, sizeof(cmd));
  711. if (rq_data_dir(rq))
  712. cmd.tf_flags |= IDE_TFLAG_WRITE;
  713. cmd.rq = rq;
  714. if (!blk_rq_is_passthrough(rq) || blk_rq_bytes(rq)) {
  715. ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
  716. ide_map_sg(drive, &cmd);
  717. }
  718. return ide_issue_pc(drive, &cmd);
  719. out_end:
  720. nsectors = blk_rq_sectors(rq);
  721. if (nsectors == 0)
  722. nsectors = 1;
  723. ide_complete_rq(drive, uptodate ? BLK_STS_OK : BLK_STS_IOERR, nsectors << 9);
  724. return ide_stopped;
  725. }
  726. /*
  727. * Ioctl handling.
  728. *
  729. * Routines which queue packet commands take as a final argument a pointer to a
  730. * request_sense struct. If execution of the command results in an error with a
  731. * CHECK CONDITION status, this structure will be filled with the results of the
  732. * subsequent request sense command. The pointer can also be NULL, in which case
  733. * no sense information is returned.
  734. */
  735. static void msf_from_bcd(struct atapi_msf *msf)
  736. {
  737. msf->minute = bcd2bin(msf->minute);
  738. msf->second = bcd2bin(msf->second);
  739. msf->frame = bcd2bin(msf->frame);
  740. }
  741. int cdrom_check_status(ide_drive_t *drive, struct scsi_sense_hdr *sshdr)
  742. {
  743. struct cdrom_info *info = drive->driver_data;
  744. struct cdrom_device_info *cdi;
  745. unsigned char cmd[BLK_MAX_CDB];
  746. ide_debug_log(IDE_DBG_FUNC, "enter");
  747. if (!info)
  748. return -EIO;
  749. cdi = &info->devinfo;
  750. memset(cmd, 0, BLK_MAX_CDB);
  751. cmd[0] = GPCMD_TEST_UNIT_READY;
  752. /*
  753. * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs
  754. * instead of supporting the LOAD_UNLOAD opcode.
  755. */
  756. cmd[7] = cdi->sanyo_slot % 3;
  757. return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sshdr, 0, RQF_QUIET);
  758. }
  759. static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
  760. unsigned long *sectors_per_frame)
  761. {
  762. struct {
  763. __be32 lba;
  764. __be32 blocklen;
  765. } capbuf;
  766. int stat;
  767. unsigned char cmd[BLK_MAX_CDB];
  768. unsigned len = sizeof(capbuf);
  769. u32 blocklen;
  770. ide_debug_log(IDE_DBG_FUNC, "enter");
  771. memset(cmd, 0, BLK_MAX_CDB);
  772. cmd[0] = GPCMD_READ_CDVD_CAPACITY;
  773. stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, NULL, 0,
  774. RQF_QUIET);
  775. if (stat)
  776. return stat;
  777. /*
  778. * Sanity check the given block size, in so far as making
  779. * sure the sectors_per_frame we give to the caller won't
  780. * end up being bogus.
  781. */
  782. blocklen = be32_to_cpu(capbuf.blocklen);
  783. blocklen = (blocklen >> SECTOR_SHIFT) << SECTOR_SHIFT;
  784. switch (blocklen) {
  785. case 512:
  786. case 1024:
  787. case 2048:
  788. case 4096:
  789. break;
  790. default:
  791. printk_once(KERN_ERR PFX "%s: weird block size %u; "
  792. "setting default block size to 2048\n",
  793. drive->name, blocklen);
  794. blocklen = 2048;
  795. break;
  796. }
  797. *capacity = 1 + be32_to_cpu(capbuf.lba);
  798. *sectors_per_frame = blocklen >> SECTOR_SHIFT;
  799. ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu",
  800. *capacity, *sectors_per_frame);
  801. return 0;
  802. }
  803. static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
  804. int format, char *buf, int buflen)
  805. {
  806. unsigned char cmd[BLK_MAX_CDB];
  807. ide_debug_log(IDE_DBG_FUNC, "enter");
  808. memset(cmd, 0, BLK_MAX_CDB);
  809. cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  810. cmd[6] = trackno;
  811. cmd[7] = (buflen >> 8);
  812. cmd[8] = (buflen & 0xff);
  813. cmd[9] = (format << 6);
  814. if (msf_flag)
  815. cmd[1] = 2;
  816. return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, NULL, 0, RQF_QUIET);
  817. }
  818. /* Try to read the entire TOC for the disk into our internal buffer. */
  819. int ide_cd_read_toc(ide_drive_t *drive)
  820. {
  821. int stat, ntracks, i;
  822. struct cdrom_info *info = drive->driver_data;
  823. struct cdrom_device_info *cdi = &info->devinfo;
  824. struct atapi_toc *toc = info->toc;
  825. struct {
  826. struct atapi_toc_header hdr;
  827. struct atapi_toc_entry ent;
  828. } ms_tmp;
  829. long last_written;
  830. unsigned long sectors_per_frame = SECTORS_PER_FRAME;
  831. ide_debug_log(IDE_DBG_FUNC, "enter");
  832. if (toc == NULL) {
  833. /* try to allocate space */
  834. toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
  835. if (toc == NULL) {
  836. printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n",
  837. drive->name);
  838. return -ENOMEM;
  839. }
  840. info->toc = toc;
  841. }
  842. /*
  843. * Check to see if the existing data is still valid. If it is,
  844. * just return.
  845. */
  846. (void) cdrom_check_status(drive, NULL);
  847. if (drive->atapi_flags & IDE_AFLAG_TOC_VALID)
  848. return 0;
  849. /* try to get the total cdrom capacity and sector size */
  850. stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame);
  851. if (stat)
  852. toc->capacity = 0x1fffff;
  853. set_capacity(info->disk, toc->capacity * sectors_per_frame);
  854. /* save a private copy of the TOC capacity for error handling */
  855. drive->probed_capacity = toc->capacity * sectors_per_frame;
  856. blk_queue_logical_block_size(drive->queue,
  857. sectors_per_frame << SECTOR_SHIFT);
  858. /* first read just the header, so we know how long the TOC is */
  859. stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
  860. sizeof(struct atapi_toc_header));
  861. if (stat)
  862. return stat;
  863. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
  864. toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
  865. toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
  866. }
  867. ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
  868. if (ntracks <= 0)
  869. return -EIO;
  870. if (ntracks > MAX_TRACKS)
  871. ntracks = MAX_TRACKS;
  872. /* now read the whole schmeer */
  873. stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
  874. (char *)&toc->hdr,
  875. sizeof(struct atapi_toc_header) +
  876. (ntracks + 1) *
  877. sizeof(struct atapi_toc_entry));
  878. if (stat && toc->hdr.first_track > 1) {
  879. /*
  880. * Cds with CDI tracks only don't have any TOC entries, despite
  881. * of this the returned values are
  882. * first_track == last_track = number of CDI tracks + 1,
  883. * so that this case is indistinguishable from the same layout
  884. * plus an additional audio track. If we get an error for the
  885. * regular case, we assume a CDI without additional audio
  886. * tracks. In this case the readable TOC is empty (CDI tracks
  887. * are not included) and only holds the Leadout entry.
  888. *
  889. * Heiko Eißfeldt.
  890. */
  891. ntracks = 0;
  892. stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
  893. (char *)&toc->hdr,
  894. sizeof(struct atapi_toc_header) +
  895. (ntracks + 1) *
  896. sizeof(struct atapi_toc_entry));
  897. if (stat)
  898. return stat;
  899. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
  900. toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT);
  901. toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT);
  902. } else {
  903. toc->hdr.first_track = CDROM_LEADOUT;
  904. toc->hdr.last_track = CDROM_LEADOUT;
  905. }
  906. }
  907. if (stat)
  908. return stat;
  909. toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
  910. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
  911. toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
  912. toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
  913. }
  914. for (i = 0; i <= ntracks; i++) {
  915. if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
  916. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD)
  917. toc->ent[i].track = bcd2bin(toc->ent[i].track);
  918. msf_from_bcd(&toc->ent[i].addr.msf);
  919. }
  920. toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute,
  921. toc->ent[i].addr.msf.second,
  922. toc->ent[i].addr.msf.frame);
  923. }
  924. if (toc->hdr.first_track != CDROM_LEADOUT) {
  925. /* read the multisession information */
  926. stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
  927. sizeof(ms_tmp));
  928. if (stat)
  929. return stat;
  930. toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
  931. } else {
  932. ms_tmp.hdr.last_track = CDROM_LEADOUT;
  933. ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
  934. toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
  935. }
  936. if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
  937. /* re-read multisession information using MSF format */
  938. stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
  939. sizeof(ms_tmp));
  940. if (stat)
  941. return stat;
  942. msf_from_bcd(&ms_tmp.ent.addr.msf);
  943. toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
  944. ms_tmp.ent.addr.msf.second,
  945. ms_tmp.ent.addr.msf.frame);
  946. }
  947. toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
  948. /* now try to get the total cdrom capacity */
  949. stat = cdrom_get_last_written(cdi, &last_written);
  950. if (!stat && (last_written > toc->capacity)) {
  951. toc->capacity = last_written;
  952. set_capacity(info->disk, toc->capacity * sectors_per_frame);
  953. drive->probed_capacity = toc->capacity * sectors_per_frame;
  954. }
  955. /* Remember that we've read this stuff. */
  956. drive->atapi_flags |= IDE_AFLAG_TOC_VALID;
  957. return 0;
  958. }
  959. int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
  960. {
  961. struct cdrom_info *info = drive->driver_data;
  962. struct cdrom_device_info *cdi = &info->devinfo;
  963. struct packet_command cgc;
  964. int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
  965. ide_debug_log(IDE_DBG_FUNC, "enter");
  966. if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
  967. size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
  968. init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
  969. do {
  970. /* we seem to get stat=0x01,err=0x00 the first time (??) */
  971. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  972. if (!stat)
  973. break;
  974. } while (--attempts);
  975. return stat;
  976. }
  977. void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
  978. {
  979. struct cdrom_info *cd = drive->driver_data;
  980. u16 curspeed, maxspeed;
  981. ide_debug_log(IDE_DBG_FUNC, "enter");
  982. if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
  983. curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
  984. maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]);
  985. } else {
  986. curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]);
  987. maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
  988. }
  989. ide_debug_log(IDE_DBG_PROBE, "curspeed: %u, maxspeed: %u",
  990. curspeed, maxspeed);
  991. cd->current_speed = DIV_ROUND_CLOSEST(curspeed, 176);
  992. cd->max_speed = DIV_ROUND_CLOSEST(maxspeed, 176);
  993. }
  994. #define IDE_CD_CAPABILITIES \
  995. (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
  996. CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
  997. CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
  998. CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
  999. CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
  1000. static const struct cdrom_device_ops ide_cdrom_dops = {
  1001. .open = ide_cdrom_open_real,
  1002. .release = ide_cdrom_release_real,
  1003. .drive_status = ide_cdrom_drive_status,
  1004. .check_events = ide_cdrom_check_events_real,
  1005. .tray_move = ide_cdrom_tray_move,
  1006. .lock_door = ide_cdrom_lock_door,
  1007. .select_speed = ide_cdrom_select_speed,
  1008. .get_last_session = ide_cdrom_get_last_session,
  1009. .get_mcn = ide_cdrom_get_mcn,
  1010. .reset = ide_cdrom_reset,
  1011. .audio_ioctl = ide_cdrom_audio_ioctl,
  1012. .capability = IDE_CD_CAPABILITIES,
  1013. .generic_packet = ide_cdrom_packet,
  1014. };
  1015. static int ide_cdrom_register(ide_drive_t *drive, int nslots)
  1016. {
  1017. struct cdrom_info *info = drive->driver_data;
  1018. struct cdrom_device_info *devinfo = &info->devinfo;
  1019. ide_debug_log(IDE_DBG_PROBE, "nslots: %d", nslots);
  1020. devinfo->ops = &ide_cdrom_dops;
  1021. devinfo->speed = info->current_speed;
  1022. devinfo->capacity = nslots;
  1023. devinfo->handle = drive;
  1024. strcpy(devinfo->name, drive->name);
  1025. if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT)
  1026. devinfo->mask |= CDC_SELECT_SPEED;
  1027. devinfo->disk = info->disk;
  1028. return register_cdrom(devinfo);
  1029. }
  1030. static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
  1031. {
  1032. struct cdrom_info *cd = drive->driver_data;
  1033. struct cdrom_device_info *cdi = &cd->devinfo;
  1034. u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
  1035. mechtype_t mechtype;
  1036. int nslots = 1;
  1037. ide_debug_log(IDE_DBG_PROBE, "media: 0x%x, atapi_flags: 0x%lx",
  1038. drive->media, drive->atapi_flags);
  1039. cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
  1040. CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
  1041. CDC_MO_DRIVE | CDC_RAM);
  1042. if (drive->media == ide_optical) {
  1043. cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
  1044. printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n",
  1045. drive->name);
  1046. return nslots;
  1047. }
  1048. if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) {
  1049. drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
  1050. cdi->mask &= ~CDC_PLAY_AUDIO;
  1051. return nslots;
  1052. }
  1053. /*
  1054. * We have to cheat a little here. the packet will eventually be queued
  1055. * with ide_cdrom_packet(), which extracts the drive from cdi->handle.
  1056. * Since this device hasn't been registered with the Uniform layer yet,
  1057. * it can't do this. Same goes for cdi->ops.
  1058. */
  1059. cdi->handle = drive;
  1060. cdi->ops = &ide_cdrom_dops;
  1061. if (ide_cdrom_get_capabilities(drive, buf))
  1062. return 0;
  1063. if ((buf[8 + 6] & 0x01) == 0)
  1064. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  1065. if (buf[8 + 6] & 0x08)
  1066. drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
  1067. if (buf[8 + 3] & 0x01)
  1068. cdi->mask &= ~CDC_CD_R;
  1069. if (buf[8 + 3] & 0x02)
  1070. cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
  1071. if (buf[8 + 2] & 0x38)
  1072. cdi->mask &= ~CDC_DVD;
  1073. if (buf[8 + 3] & 0x20)
  1074. cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
  1075. if (buf[8 + 3] & 0x10)
  1076. cdi->mask &= ~CDC_DVD_R;
  1077. if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK))
  1078. cdi->mask &= ~CDC_PLAY_AUDIO;
  1079. mechtype = buf[8 + 6] >> 5;
  1080. if (mechtype == mechtype_caddy ||
  1081. mechtype == mechtype_popup ||
  1082. (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE))
  1083. cdi->mask |= CDC_CLOSE_TRAY;
  1084. if (cdi->sanyo_slot > 0) {
  1085. cdi->mask &= ~CDC_SELECT_DISC;
  1086. nslots = 3;
  1087. } else if (mechtype == mechtype_individual_changer ||
  1088. mechtype == mechtype_cartridge_changer) {
  1089. nslots = cdrom_number_of_slots(cdi);
  1090. if (nslots > 1)
  1091. cdi->mask &= ~CDC_SELECT_DISC;
  1092. }
  1093. ide_cdrom_update_speed(drive, buf);
  1094. printk(KERN_INFO PFX "%s: ATAPI", drive->name);
  1095. /* don't print speed if the drive reported 0 */
  1096. if (cd->max_speed)
  1097. printk(KERN_CONT " %dX", cd->max_speed);
  1098. printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
  1099. if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
  1100. printk(KERN_CONT " DVD%s%s",
  1101. (cdi->mask & CDC_DVD_R) ? "" : "-R",
  1102. (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM");
  1103. if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
  1104. printk(KERN_CONT " CD%s%s",
  1105. (cdi->mask & CDC_CD_R) ? "" : "-R",
  1106. (cdi->mask & CDC_CD_RW) ? "" : "/RW");
  1107. if ((cdi->mask & CDC_SELECT_DISC) == 0)
  1108. printk(KERN_CONT " changer w/%d slots", nslots);
  1109. else
  1110. printk(KERN_CONT " drive");
  1111. printk(KERN_CONT ", %dkB Cache\n",
  1112. be16_to_cpup((__be16 *)&buf[8 + 12]));
  1113. return nslots;
  1114. }
  1115. /* standard prep_rq_fn that builds 10 byte cmds */
  1116. static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
  1117. {
  1118. int hard_sect = queue_logical_block_size(q);
  1119. long block = (long)blk_rq_pos(rq) / (hard_sect >> 9);
  1120. unsigned long blocks = blk_rq_sectors(rq) / (hard_sect >> 9);
  1121. struct scsi_request *req = scsi_req(rq);
  1122. q->initialize_rq_fn(rq);
  1123. if (rq_data_dir(rq) == READ)
  1124. req->cmd[0] = GPCMD_READ_10;
  1125. else
  1126. req->cmd[0] = GPCMD_WRITE_10;
  1127. /*
  1128. * fill in lba
  1129. */
  1130. req->cmd[2] = (block >> 24) & 0xff;
  1131. req->cmd[3] = (block >> 16) & 0xff;
  1132. req->cmd[4] = (block >> 8) & 0xff;
  1133. req->cmd[5] = block & 0xff;
  1134. /*
  1135. * and transfer length
  1136. */
  1137. req->cmd[7] = (blocks >> 8) & 0xff;
  1138. req->cmd[8] = blocks & 0xff;
  1139. req->cmd_len = 10;
  1140. return BLKPREP_OK;
  1141. }
  1142. /*
  1143. * Most of the SCSI commands are supported directly by ATAPI devices.
  1144. * This transform handles the few exceptions.
  1145. */
  1146. static int ide_cdrom_prep_pc(struct request *rq)
  1147. {
  1148. u8 *c = scsi_req(rq)->cmd;
  1149. /* transform 6-byte read/write commands to the 10-byte version */
  1150. if (c[0] == READ_6 || c[0] == WRITE_6) {
  1151. c[8] = c[4];
  1152. c[5] = c[3];
  1153. c[4] = c[2];
  1154. c[3] = c[1] & 0x1f;
  1155. c[2] = 0;
  1156. c[1] &= 0xe0;
  1157. c[0] += (READ_10 - READ_6);
  1158. scsi_req(rq)->cmd_len = 10;
  1159. return BLKPREP_OK;
  1160. }
  1161. /*
  1162. * it's silly to pretend we understand 6-byte sense commands, just
  1163. * reject with ILLEGAL_REQUEST and the caller should take the
  1164. * appropriate action
  1165. */
  1166. if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
  1167. scsi_req(rq)->result = ILLEGAL_REQUEST;
  1168. return BLKPREP_KILL;
  1169. }
  1170. return BLKPREP_OK;
  1171. }
  1172. static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
  1173. {
  1174. if (!blk_rq_is_passthrough(rq))
  1175. return ide_cdrom_prep_fs(q, rq);
  1176. else if (blk_rq_is_scsi(rq))
  1177. return ide_cdrom_prep_pc(rq);
  1178. return 0;
  1179. }
  1180. struct cd_list_entry {
  1181. const char *id_model;
  1182. const char *id_firmware;
  1183. unsigned int cd_flags;
  1184. };
  1185. #ifdef CONFIG_IDE_PROC_FS
  1186. static sector_t ide_cdrom_capacity(ide_drive_t *drive)
  1187. {
  1188. unsigned long capacity, sectors_per_frame;
  1189. if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame))
  1190. return 0;
  1191. return capacity * sectors_per_frame;
  1192. }
  1193. static int idecd_capacity_proc_show(struct seq_file *m, void *v)
  1194. {
  1195. ide_drive_t *drive = m->private;
  1196. seq_printf(m, "%llu\n", (long long)ide_cdrom_capacity(drive));
  1197. return 0;
  1198. }
  1199. static ide_proc_entry_t idecd_proc[] = {
  1200. { "capacity", S_IFREG|S_IRUGO, idecd_capacity_proc_show },
  1201. {}
  1202. };
  1203. static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
  1204. {
  1205. return idecd_proc;
  1206. }
  1207. static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
  1208. {
  1209. return NULL;
  1210. }
  1211. #endif
  1212. static const struct cd_list_entry ide_cd_quirks_list[] = {
  1213. /* SCR-3231 doesn't support the SET_CD_SPEED command. */
  1214. { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT },
  1215. /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
  1216. { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
  1217. IDE_AFLAG_PRE_ATAPI12, },
  1218. /* Vertos 300, some versions of this drive like to talk BCD. */
  1219. { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, },
  1220. /* Vertos 600 ESD. */
  1221. { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, },
  1222. /*
  1223. * Sanyo 3 CD changer uses a non-standard command for CD changing
  1224. * (by default standard ATAPI support for CD changers is used).
  1225. */
  1226. { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD },
  1227. { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD },
  1228. { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD },
  1229. /* Stingray 8X CD-ROM. */
  1230. { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
  1231. /*
  1232. * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
  1233. * mode sense page capabilities size, but older drives break.
  1234. */
  1235. { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
  1236. { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
  1237. /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
  1238. { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS },
  1239. /*
  1240. * Some drives used by Apple don't advertise audio play
  1241. * but they do support reading TOC & audio datas.
  1242. */
  1243. { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1244. { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1245. { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1246. { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1247. { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1248. { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1249. { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE },
  1250. { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE },
  1251. { NULL, NULL, 0 }
  1252. };
  1253. static unsigned int ide_cd_flags(u16 *id)
  1254. {
  1255. const struct cd_list_entry *cle = ide_cd_quirks_list;
  1256. while (cle->id_model) {
  1257. if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
  1258. (cle->id_firmware == NULL ||
  1259. strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
  1260. return cle->cd_flags;
  1261. cle++;
  1262. }
  1263. return 0;
  1264. }
  1265. static int ide_cdrom_setup(ide_drive_t *drive)
  1266. {
  1267. struct cdrom_info *cd = drive->driver_data;
  1268. struct cdrom_device_info *cdi = &cd->devinfo;
  1269. struct request_queue *q = drive->queue;
  1270. u16 *id = drive->id;
  1271. char *fw_rev = (char *)&id[ATA_ID_FW_REV];
  1272. int nslots;
  1273. ide_debug_log(IDE_DBG_PROBE, "enter");
  1274. blk_queue_prep_rq(q, ide_cdrom_prep_fn);
  1275. blk_queue_dma_alignment(q, 31);
  1276. blk_queue_update_dma_pad(q, 15);
  1277. drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
  1278. drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
  1279. if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
  1280. fw_rev[4] == '1' && fw_rev[6] <= '2')
  1281. drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
  1282. IDE_AFLAG_TOCADDR_AS_BCD);
  1283. else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
  1284. fw_rev[4] == '1' && fw_rev[6] <= '2')
  1285. drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
  1286. else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
  1287. /* 3 => use CD in slot 0 */
  1288. cdi->sanyo_slot = 3;
  1289. nslots = ide_cdrom_probe_capabilities(drive);
  1290. blk_queue_logical_block_size(q, CD_FRAMESIZE);
  1291. if (ide_cdrom_register(drive, nslots)) {
  1292. printk(KERN_ERR PFX "%s: %s failed to register device with the"
  1293. " cdrom driver.\n", drive->name, __func__);
  1294. cd->devinfo.handle = NULL;
  1295. return 1;
  1296. }
  1297. ide_proc_register_driver(drive, cd->driver);
  1298. return 0;
  1299. }
  1300. static void ide_cd_remove(ide_drive_t *drive)
  1301. {
  1302. struct cdrom_info *info = drive->driver_data;
  1303. ide_debug_log(IDE_DBG_FUNC, "enter");
  1304. ide_proc_unregister_driver(drive, info->driver);
  1305. device_del(&info->dev);
  1306. del_gendisk(info->disk);
  1307. mutex_lock(&idecd_ref_mutex);
  1308. put_device(&info->dev);
  1309. mutex_unlock(&idecd_ref_mutex);
  1310. }
  1311. static void ide_cd_release(struct device *dev)
  1312. {
  1313. struct cdrom_info *info = to_ide_drv(dev, cdrom_info);
  1314. struct cdrom_device_info *devinfo = &info->devinfo;
  1315. ide_drive_t *drive = info->drive;
  1316. struct gendisk *g = info->disk;
  1317. ide_debug_log(IDE_DBG_FUNC, "enter");
  1318. kfree(info->toc);
  1319. if (devinfo->handle == drive)
  1320. unregister_cdrom(devinfo);
  1321. drive->driver_data = NULL;
  1322. blk_queue_prep_rq(drive->queue, NULL);
  1323. g->private_data = NULL;
  1324. put_disk(g);
  1325. kfree(info);
  1326. }
  1327. static int ide_cd_probe(ide_drive_t *);
  1328. static struct ide_driver ide_cdrom_driver = {
  1329. .gen_driver = {
  1330. .owner = THIS_MODULE,
  1331. .name = "ide-cdrom",
  1332. .bus = &ide_bus_type,
  1333. },
  1334. .probe = ide_cd_probe,
  1335. .remove = ide_cd_remove,
  1336. .version = IDECD_VERSION,
  1337. .do_request = ide_cd_do_request,
  1338. #ifdef CONFIG_IDE_PROC_FS
  1339. .proc_entries = ide_cd_proc_entries,
  1340. .proc_devsets = ide_cd_proc_devsets,
  1341. #endif
  1342. };
  1343. static int idecd_open(struct block_device *bdev, fmode_t mode)
  1344. {
  1345. struct cdrom_info *info;
  1346. int rc = -ENXIO;
  1347. check_disk_change(bdev);
  1348. mutex_lock(&ide_cd_mutex);
  1349. info = ide_cd_get(bdev->bd_disk);
  1350. if (!info)
  1351. goto out;
  1352. rc = cdrom_open(&info->devinfo, bdev, mode);
  1353. if (rc < 0)
  1354. ide_cd_put(info);
  1355. out:
  1356. mutex_unlock(&ide_cd_mutex);
  1357. return rc;
  1358. }
  1359. static void idecd_release(struct gendisk *disk, fmode_t mode)
  1360. {
  1361. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1362. mutex_lock(&ide_cd_mutex);
  1363. cdrom_release(&info->devinfo, mode);
  1364. ide_cd_put(info);
  1365. mutex_unlock(&ide_cd_mutex);
  1366. }
  1367. static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
  1368. {
  1369. struct packet_command cgc;
  1370. char buffer[16];
  1371. int stat;
  1372. char spindown;
  1373. if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
  1374. return -EFAULT;
  1375. init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
  1376. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
  1377. if (stat)
  1378. return stat;
  1379. buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
  1380. return cdrom_mode_select(cdi, &cgc);
  1381. }
  1382. static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
  1383. {
  1384. struct packet_command cgc;
  1385. char buffer[16];
  1386. int stat;
  1387. char spindown;
  1388. init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
  1389. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
  1390. if (stat)
  1391. return stat;
  1392. spindown = buffer[11] & 0x0f;
  1393. if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
  1394. return -EFAULT;
  1395. return 0;
  1396. }
  1397. static int idecd_locked_ioctl(struct block_device *bdev, fmode_t mode,
  1398. unsigned int cmd, unsigned long arg)
  1399. {
  1400. struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
  1401. int err;
  1402. switch (cmd) {
  1403. case CDROMSETSPINDOWN:
  1404. return idecd_set_spindown(&info->devinfo, arg);
  1405. case CDROMGETSPINDOWN:
  1406. return idecd_get_spindown(&info->devinfo, arg);
  1407. default:
  1408. break;
  1409. }
  1410. err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
  1411. if (err == -EINVAL)
  1412. err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
  1413. return err;
  1414. }
  1415. static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
  1416. unsigned int cmd, unsigned long arg)
  1417. {
  1418. int ret;
  1419. mutex_lock(&ide_cd_mutex);
  1420. ret = idecd_locked_ioctl(bdev, mode, cmd, arg);
  1421. mutex_unlock(&ide_cd_mutex);
  1422. return ret;
  1423. }
  1424. static unsigned int idecd_check_events(struct gendisk *disk,
  1425. unsigned int clearing)
  1426. {
  1427. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1428. return cdrom_check_events(&info->devinfo, clearing);
  1429. }
  1430. static int idecd_revalidate_disk(struct gendisk *disk)
  1431. {
  1432. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1433. ide_cd_read_toc(info->drive);
  1434. return 0;
  1435. }
  1436. static const struct block_device_operations idecd_ops = {
  1437. .owner = THIS_MODULE,
  1438. .open = idecd_open,
  1439. .release = idecd_release,
  1440. .ioctl = idecd_ioctl,
  1441. .check_events = idecd_check_events,
  1442. .revalidate_disk = idecd_revalidate_disk
  1443. };
  1444. /* module options */
  1445. static unsigned long debug_mask;
  1446. module_param(debug_mask, ulong, 0644);
  1447. MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
  1448. static int ide_cd_probe(ide_drive_t *drive)
  1449. {
  1450. struct cdrom_info *info;
  1451. struct gendisk *g;
  1452. ide_debug_log(IDE_DBG_PROBE, "driver_req: %s, media: 0x%x",
  1453. drive->driver_req, drive->media);
  1454. if (!strstr("ide-cdrom", drive->driver_req))
  1455. goto failed;
  1456. if (drive->media != ide_cdrom && drive->media != ide_optical)
  1457. goto failed;
  1458. drive->debug_mask = debug_mask;
  1459. drive->irq_handler = cdrom_newpc_intr;
  1460. info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
  1461. if (info == NULL) {
  1462. printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
  1463. drive->name);
  1464. goto failed;
  1465. }
  1466. g = alloc_disk(1 << PARTN_BITS);
  1467. if (!g)
  1468. goto out_free_cd;
  1469. ide_init_disk(g, drive);
  1470. info->dev.parent = &drive->gendev;
  1471. info->dev.release = ide_cd_release;
  1472. dev_set_name(&info->dev, "%s", dev_name(&drive->gendev));
  1473. if (device_register(&info->dev))
  1474. goto out_free_disk;
  1475. info->drive = drive;
  1476. info->driver = &ide_cdrom_driver;
  1477. info->disk = g;
  1478. g->private_data = &info->driver;
  1479. drive->driver_data = info;
  1480. g->minors = 1;
  1481. g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
  1482. if (ide_cdrom_setup(drive)) {
  1483. put_device(&info->dev);
  1484. goto failed;
  1485. }
  1486. ide_cd_read_toc(drive);
  1487. g->fops = &idecd_ops;
  1488. g->flags |= GENHD_FL_REMOVABLE | GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE;
  1489. device_add_disk(&drive->gendev, g, NULL);
  1490. return 0;
  1491. out_free_disk:
  1492. put_disk(g);
  1493. out_free_cd:
  1494. kfree(info);
  1495. failed:
  1496. return -ENODEV;
  1497. }
  1498. static void __exit ide_cdrom_exit(void)
  1499. {
  1500. driver_unregister(&ide_cdrom_driver.gen_driver);
  1501. }
  1502. static int __init ide_cdrom_init(void)
  1503. {
  1504. printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
  1505. return driver_register(&ide_cdrom_driver.gen_driver);
  1506. }
  1507. MODULE_ALIAS("ide:*m-cdrom*");
  1508. MODULE_ALIAS("ide-cd");
  1509. module_init(ide_cdrom_init);
  1510. module_exit(ide_cdrom_exit);
  1511. MODULE_LICENSE("GPL");