ide-cd.c 48 KB

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