ide-cd.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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 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. dma_error = hwif->dma_ops->dma_end(drive);
  544. if (dma_error) {
  545. printk(KERN_ERR PFX "%s: DMA %s error\n", drive->name,
  546. write ? "write" : "read");
  547. ide_dma_off(drive);
  548. }
  549. }
  550. rc = cdrom_decode_status(drive, 0, &stat);
  551. if (rc) {
  552. if (rc == 2)
  553. goto out_end;
  554. return ide_stopped;
  555. }
  556. /* using dma, transfer is complete now */
  557. if (dma) {
  558. if (dma_error)
  559. return ide_error(drive, "dma error", stat);
  560. if (blk_fs_request(rq)) {
  561. ide_complete_rq(drive, 0, rq->nr_sectors
  562. ? (rq->nr_sectors << 9) : ide_rq_bytes(rq));
  563. return ide_stopped;
  564. } else if (rq->cmd_type == REQ_TYPE_ATA_PC && !rq->bio) {
  565. ide_complete_rq(drive, 0, 512);
  566. return ide_stopped;
  567. }
  568. if (blk_pc_request(rq) == 0 && uptodate == 0)
  569. rq->cmd_flags |= REQ_FAILED;
  570. goto out_end;
  571. }
  572. ide_read_bcount_and_ireason(drive, &len, &ireason);
  573. thislen = blk_fs_request(rq) ? len : cmd->nleft;
  574. if (thislen > len)
  575. thislen = len;
  576. ide_debug_log(IDE_DBG_PC, "DRQ: stat: 0x%x, thislen: %d",
  577. stat, thislen);
  578. /* If DRQ is clear, the command has completed. */
  579. if ((stat & ATA_DRQ) == 0) {
  580. if (blk_fs_request(rq)) {
  581. /*
  582. * If we're not done reading/writing, complain.
  583. * Otherwise, complete the command normally.
  584. */
  585. uptodate = 1;
  586. if (cmd->nleft > 0) {
  587. printk(KERN_ERR PFX "%s: %s: data underrun "
  588. "(%u bytes)\n", drive->name, __func__,
  589. cmd->nleft);
  590. if (!write)
  591. rq->cmd_flags |= REQ_FAILED;
  592. uptodate = 0;
  593. }
  594. } else if (!blk_pc_request(rq)) {
  595. ide_cd_request_sense_fixup(drive, cmd);
  596. /* complain if we still have data left to transfer */
  597. uptodate = cmd->nleft ? 0 : 1;
  598. if (uptodate == 0)
  599. rq->cmd_flags |= REQ_FAILED;
  600. }
  601. goto out_end;
  602. }
  603. /* check which way to transfer data */
  604. rc = ide_cd_check_ireason(drive, rq, len, ireason, write);
  605. if (rc)
  606. goto out_end;
  607. cmd->last_xfer_len = 0;
  608. ide_debug_log(IDE_DBG_PC, "data transfer, rq->cmd_type: 0x%x, "
  609. "ireason: 0x%x",
  610. rq->cmd_type, ireason);
  611. /* transfer data */
  612. while (thislen > 0) {
  613. int blen = min_t(int, thislen, cmd->nleft);
  614. if (cmd->nleft == 0) {
  615. if (blk_fs_request(rq) && !write)
  616. /*
  617. * If the buffers are full, pipe the rest into
  618. * oblivion.
  619. */
  620. ide_pad_transfer(drive, 0, thislen);
  621. else {
  622. printk(KERN_ERR PFX "%s: confused, missing data\n",
  623. drive->name);
  624. blk_dump_rq_flags(rq, rq_data_dir(rq)
  625. ? "cdrom_newpc_intr, write"
  626. : "cdrom_newpc_intr, read");
  627. }
  628. break;
  629. }
  630. ide_pio_bytes(drive, cmd, write, blen);
  631. cmd->last_xfer_len += blen;
  632. thislen -= blen;
  633. len -= blen;
  634. if (sense && write == 0)
  635. rq->sense_len += blen;
  636. }
  637. /* pad, if necessary */
  638. if (!blk_fs_request(rq) && len > 0)
  639. ide_pad_transfer(drive, write, len);
  640. if (blk_pc_request(rq)) {
  641. timeout = rq->timeout;
  642. } else {
  643. timeout = ATAPI_WAIT_PC;
  644. if (!blk_fs_request(rq))
  645. expiry = ide_cd_expiry;
  646. }
  647. hwif->expiry = expiry;
  648. ide_set_handler(drive, cdrom_newpc_intr, timeout);
  649. return ide_started;
  650. out_end:
  651. if (blk_pc_request(rq) && rc == 0) {
  652. unsigned int dlen = rq->data_len;
  653. rq->data_len = 0;
  654. if (blk_end_request(rq, 0, dlen))
  655. BUG();
  656. hwif->rq = NULL;
  657. } else {
  658. if (sense && uptodate)
  659. ide_cd_complete_failed_rq(drive, rq);
  660. if (blk_fs_request(rq)) {
  661. if (cmd->nleft == 0)
  662. uptodate = 1;
  663. } else {
  664. if (uptodate <= 0 && rq->errors == 0)
  665. rq->errors = -EIO;
  666. }
  667. if (uptodate == 0)
  668. ide_cd_error_cmd(drive, cmd);
  669. /* make sure it's fully ended */
  670. if (blk_pc_request(rq))
  671. nsectors = (rq->data_len + 511) >> 9;
  672. else
  673. nsectors = rq->hard_nr_sectors;
  674. if (nsectors == 0)
  675. nsectors = 1;
  676. if (blk_fs_request(rq) == 0) {
  677. rq->data_len -= (cmd->nbytes - cmd->nleft);
  678. if (uptodate == 0 && (cmd->tf_flags & IDE_TFLAG_WRITE))
  679. rq->data_len += cmd->last_xfer_len;
  680. }
  681. ide_complete_rq(drive, uptodate ? 0 : -EIO, nsectors << 9);
  682. if (sense && rc == 2)
  683. ide_error(drive, "request sense failure", stat);
  684. }
  685. return ide_stopped;
  686. }
  687. static ide_startstop_t cdrom_start_rw(ide_drive_t *drive, struct request *rq)
  688. {
  689. struct cdrom_info *cd = drive->driver_data;
  690. struct request_queue *q = drive->queue;
  691. int write = rq_data_dir(rq) == WRITE;
  692. unsigned short sectors_per_frame =
  693. queue_hardsect_size(q) >> SECTOR_BITS;
  694. ide_debug_log(IDE_DBG_RQ, "rq->cmd[0]: 0x%x, rq->cmd_flags: 0x%x, "
  695. "secs_per_frame: %u",
  696. rq->cmd[0], rq->cmd_flags, sectors_per_frame);
  697. if (write) {
  698. /* disk has become write protected */
  699. if (get_disk_ro(cd->disk))
  700. return ide_stopped;
  701. } else {
  702. /*
  703. * We may be retrying this request after an error. Fix up any
  704. * weirdness which might be present in the request packet.
  705. */
  706. q->prep_rq_fn(q, rq);
  707. }
  708. /* fs requests *must* be hardware frame aligned */
  709. if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
  710. (rq->sector & (sectors_per_frame - 1)))
  711. return ide_stopped;
  712. /* use DMA, if possible */
  713. drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  714. if (write)
  715. cd->devinfo.media_written = 1;
  716. rq->timeout = ATAPI_WAIT_PC;
  717. return ide_started;
  718. }
  719. static void cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
  720. {
  721. ide_debug_log(IDE_DBG_PC, "rq->cmd[0]: 0x%x, rq->cmd_type: 0x%x",
  722. rq->cmd[0], rq->cmd_type);
  723. if (blk_pc_request(rq))
  724. rq->cmd_flags |= REQ_QUIET;
  725. else
  726. rq->cmd_flags &= ~REQ_FAILED;
  727. drive->dma = 0;
  728. /* sg request */
  729. if (rq->bio || ((rq->cmd_type == REQ_TYPE_ATA_PC) && rq->data_len)) {
  730. struct request_queue *q = drive->queue;
  731. unsigned int alignment;
  732. char *buf;
  733. if (rq->bio)
  734. buf = bio_data(rq->bio);
  735. else
  736. buf = rq->data;
  737. drive->dma = !!(drive->dev_flags & IDE_DFLAG_USING_DMA);
  738. /*
  739. * check if dma is safe
  740. *
  741. * NOTE! The "len" and "addr" checks should possibly have
  742. * separate masks.
  743. */
  744. alignment = queue_dma_alignment(q) | q->dma_pad_mask;
  745. if ((unsigned long)buf & alignment
  746. || rq->data_len & q->dma_pad_mask
  747. || object_is_on_stack(buf))
  748. drive->dma = 0;
  749. }
  750. }
  751. static ide_startstop_t ide_cd_do_request(ide_drive_t *drive, struct request *rq,
  752. sector_t block)
  753. {
  754. struct ide_cmd cmd;
  755. int uptodate = 0, nsectors;
  756. ide_debug_log(IDE_DBG_RQ, "cmd: 0x%x, block: %llu",
  757. rq->cmd[0], (unsigned long long)block);
  758. if (drive->debug_mask & IDE_DBG_RQ)
  759. blk_dump_rq_flags(rq, "ide_cd_do_request");
  760. if (blk_fs_request(rq)) {
  761. if (cdrom_start_rw(drive, rq) == ide_stopped)
  762. goto out_end;
  763. } else if (blk_sense_request(rq) || blk_pc_request(rq) ||
  764. rq->cmd_type == REQ_TYPE_ATA_PC) {
  765. if (!rq->timeout)
  766. rq->timeout = ATAPI_WAIT_PC;
  767. cdrom_do_block_pc(drive, rq);
  768. } else if (blk_special_request(rq)) {
  769. /* right now this can only be a reset... */
  770. uptodate = 1;
  771. goto out_end;
  772. } else {
  773. blk_dump_rq_flags(rq, DRV_NAME " bad flags");
  774. if (rq->errors == 0)
  775. rq->errors = -EIO;
  776. goto out_end;
  777. }
  778. memset(&cmd, 0, sizeof(cmd));
  779. if (rq_data_dir(rq))
  780. cmd.tf_flags |= IDE_TFLAG_WRITE;
  781. cmd.rq = rq;
  782. if (blk_fs_request(rq) || rq->data_len) {
  783. ide_init_sg_cmd(&cmd, blk_fs_request(rq) ? (rq->nr_sectors << 9)
  784. : rq->data_len);
  785. ide_map_sg(drive, &cmd);
  786. }
  787. return ide_issue_pc(drive, &cmd);
  788. out_end:
  789. nsectors = rq->hard_nr_sectors;
  790. if (nsectors == 0)
  791. nsectors = 1;
  792. ide_complete_rq(drive, uptodate ? 0 : -EIO, nsectors << 9);
  793. return ide_stopped;
  794. }
  795. /*
  796. * Ioctl handling.
  797. *
  798. * Routines which queue packet commands take as a final argument a pointer to a
  799. * request_sense struct. If execution of the command results in an error with a
  800. * CHECK CONDITION status, this structure will be filled with the results of the
  801. * subsequent request sense command. The pointer can also be NULL, in which case
  802. * no sense information is returned.
  803. */
  804. static void msf_from_bcd(struct atapi_msf *msf)
  805. {
  806. msf->minute = bcd2bin(msf->minute);
  807. msf->second = bcd2bin(msf->second);
  808. msf->frame = bcd2bin(msf->frame);
  809. }
  810. int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
  811. {
  812. struct cdrom_info *info = drive->driver_data;
  813. struct cdrom_device_info *cdi = &info->devinfo;
  814. unsigned char cmd[BLK_MAX_CDB];
  815. ide_debug_log(IDE_DBG_FUNC, "enter");
  816. memset(cmd, 0, BLK_MAX_CDB);
  817. cmd[0] = GPCMD_TEST_UNIT_READY;
  818. /*
  819. * Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to switch CDs
  820. * instead of supporting the LOAD_UNLOAD opcode.
  821. */
  822. cmd[7] = cdi->sanyo_slot % 3;
  823. return ide_cd_queue_pc(drive, cmd, 0, NULL, NULL, sense, 0, REQ_QUIET);
  824. }
  825. static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
  826. unsigned long *sectors_per_frame,
  827. struct request_sense *sense)
  828. {
  829. struct {
  830. __be32 lba;
  831. __be32 blocklen;
  832. } capbuf;
  833. int stat;
  834. unsigned char cmd[BLK_MAX_CDB];
  835. unsigned len = sizeof(capbuf);
  836. u32 blocklen;
  837. ide_debug_log(IDE_DBG_FUNC, "enter");
  838. memset(cmd, 0, BLK_MAX_CDB);
  839. cmd[0] = GPCMD_READ_CDVD_CAPACITY;
  840. stat = ide_cd_queue_pc(drive, cmd, 0, &capbuf, &len, sense, 0,
  841. REQ_QUIET);
  842. if (stat)
  843. return stat;
  844. /*
  845. * Sanity check the given block size
  846. */
  847. blocklen = be32_to_cpu(capbuf.blocklen);
  848. switch (blocklen) {
  849. case 512:
  850. case 1024:
  851. case 2048:
  852. case 4096:
  853. break;
  854. default:
  855. printk(KERN_ERR PFX "%s: weird block size %u\n",
  856. drive->name, blocklen);
  857. printk(KERN_ERR PFX "%s: default to 2kb block size\n",
  858. drive->name);
  859. blocklen = 2048;
  860. break;
  861. }
  862. *capacity = 1 + be32_to_cpu(capbuf.lba);
  863. *sectors_per_frame = blocklen >> SECTOR_BITS;
  864. ide_debug_log(IDE_DBG_PROBE, "cap: %lu, sectors_per_frame: %lu",
  865. *capacity, *sectors_per_frame);
  866. return 0;
  867. }
  868. static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
  869. int format, char *buf, int buflen,
  870. struct request_sense *sense)
  871. {
  872. unsigned char cmd[BLK_MAX_CDB];
  873. ide_debug_log(IDE_DBG_FUNC, "enter");
  874. memset(cmd, 0, BLK_MAX_CDB);
  875. cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  876. cmd[6] = trackno;
  877. cmd[7] = (buflen >> 8);
  878. cmd[8] = (buflen & 0xff);
  879. cmd[9] = (format << 6);
  880. if (msf_flag)
  881. cmd[1] = 2;
  882. return ide_cd_queue_pc(drive, cmd, 0, buf, &buflen, sense, 0, REQ_QUIET);
  883. }
  884. /* Try to read the entire TOC for the disk into our internal buffer. */
  885. int ide_cd_read_toc(ide_drive_t *drive, struct request_sense *sense)
  886. {
  887. int stat, ntracks, i;
  888. struct cdrom_info *info = drive->driver_data;
  889. struct cdrom_device_info *cdi = &info->devinfo;
  890. struct atapi_toc *toc = info->toc;
  891. struct {
  892. struct atapi_toc_header hdr;
  893. struct atapi_toc_entry ent;
  894. } ms_tmp;
  895. long last_written;
  896. unsigned long sectors_per_frame = SECTORS_PER_FRAME;
  897. ide_debug_log(IDE_DBG_FUNC, "enter");
  898. if (toc == NULL) {
  899. /* try to allocate space */
  900. toc = kmalloc(sizeof(struct atapi_toc), GFP_KERNEL);
  901. if (toc == NULL) {
  902. printk(KERN_ERR PFX "%s: No cdrom TOC buffer!\n",
  903. drive->name);
  904. return -ENOMEM;
  905. }
  906. info->toc = toc;
  907. }
  908. /*
  909. * Check to see if the existing data is still valid. If it is,
  910. * just return.
  911. */
  912. (void) cdrom_check_status(drive, sense);
  913. if (drive->atapi_flags & IDE_AFLAG_TOC_VALID)
  914. return 0;
  915. /* try to get the total cdrom capacity and sector size */
  916. stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
  917. sense);
  918. if (stat)
  919. toc->capacity = 0x1fffff;
  920. set_capacity(info->disk, toc->capacity * sectors_per_frame);
  921. /* save a private copy of the TOC capacity for error handling */
  922. drive->probed_capacity = toc->capacity * sectors_per_frame;
  923. blk_queue_hardsect_size(drive->queue,
  924. sectors_per_frame << SECTOR_BITS);
  925. /* first read just the header, so we know how long the TOC is */
  926. stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
  927. sizeof(struct atapi_toc_header), sense);
  928. if (stat)
  929. return stat;
  930. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
  931. toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
  932. toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
  933. }
  934. ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
  935. if (ntracks <= 0)
  936. return -EIO;
  937. if (ntracks > MAX_TRACKS)
  938. ntracks = MAX_TRACKS;
  939. /* now read the whole schmeer */
  940. stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
  941. (char *)&toc->hdr,
  942. sizeof(struct atapi_toc_header) +
  943. (ntracks + 1) *
  944. sizeof(struct atapi_toc_entry), sense);
  945. if (stat && toc->hdr.first_track > 1) {
  946. /*
  947. * Cds with CDI tracks only don't have any TOC entries, despite
  948. * of this the returned values are
  949. * first_track == last_track = number of CDI tracks + 1,
  950. * so that this case is indistinguishable from the same layout
  951. * plus an additional audio track. If we get an error for the
  952. * regular case, we assume a CDI without additional audio
  953. * tracks. In this case the readable TOC is empty (CDI tracks
  954. * are not included) and only holds the Leadout entry.
  955. *
  956. * Heiko Eißfeldt.
  957. */
  958. ntracks = 0;
  959. stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
  960. (char *)&toc->hdr,
  961. sizeof(struct atapi_toc_header) +
  962. (ntracks + 1) *
  963. sizeof(struct atapi_toc_entry),
  964. sense);
  965. if (stat)
  966. return stat;
  967. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
  968. toc->hdr.first_track = (u8)bin2bcd(CDROM_LEADOUT);
  969. toc->hdr.last_track = (u8)bin2bcd(CDROM_LEADOUT);
  970. } else {
  971. toc->hdr.first_track = CDROM_LEADOUT;
  972. toc->hdr.last_track = CDROM_LEADOUT;
  973. }
  974. }
  975. if (stat)
  976. return stat;
  977. toc->hdr.toc_length = be16_to_cpu(toc->hdr.toc_length);
  978. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD) {
  979. toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
  980. toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
  981. }
  982. for (i = 0; i <= ntracks; i++) {
  983. if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
  984. if (drive->atapi_flags & IDE_AFLAG_TOCTRACKS_AS_BCD)
  985. toc->ent[i].track = bcd2bin(toc->ent[i].track);
  986. msf_from_bcd(&toc->ent[i].addr.msf);
  987. }
  988. toc->ent[i].addr.lba = msf_to_lba(toc->ent[i].addr.msf.minute,
  989. toc->ent[i].addr.msf.second,
  990. toc->ent[i].addr.msf.frame);
  991. }
  992. if (toc->hdr.first_track != CDROM_LEADOUT) {
  993. /* read the multisession information */
  994. stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
  995. sizeof(ms_tmp), sense);
  996. if (stat)
  997. return stat;
  998. toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
  999. } else {
  1000. ms_tmp.hdr.last_track = CDROM_LEADOUT;
  1001. ms_tmp.hdr.first_track = ms_tmp.hdr.last_track;
  1002. toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
  1003. }
  1004. if (drive->atapi_flags & IDE_AFLAG_TOCADDR_AS_BCD) {
  1005. /* re-read multisession information using MSF format */
  1006. stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
  1007. sizeof(ms_tmp), sense);
  1008. if (stat)
  1009. return stat;
  1010. msf_from_bcd(&ms_tmp.ent.addr.msf);
  1011. toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
  1012. ms_tmp.ent.addr.msf.second,
  1013. ms_tmp.ent.addr.msf.frame);
  1014. }
  1015. toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
  1016. /* now try to get the total cdrom capacity */
  1017. stat = cdrom_get_last_written(cdi, &last_written);
  1018. if (!stat && (last_written > toc->capacity)) {
  1019. toc->capacity = last_written;
  1020. set_capacity(info->disk, toc->capacity * sectors_per_frame);
  1021. drive->probed_capacity = toc->capacity * sectors_per_frame;
  1022. }
  1023. /* Remember that we've read this stuff. */
  1024. drive->atapi_flags |= IDE_AFLAG_TOC_VALID;
  1025. return 0;
  1026. }
  1027. int ide_cdrom_get_capabilities(ide_drive_t *drive, u8 *buf)
  1028. {
  1029. struct cdrom_info *info = drive->driver_data;
  1030. struct cdrom_device_info *cdi = &info->devinfo;
  1031. struct packet_command cgc;
  1032. int stat, attempts = 3, size = ATAPI_CAPABILITIES_PAGE_SIZE;
  1033. ide_debug_log(IDE_DBG_FUNC, "enter");
  1034. if ((drive->atapi_flags & IDE_AFLAG_FULL_CAPS_PAGE) == 0)
  1035. size -= ATAPI_CAPABILITIES_PAGE_PAD_SIZE;
  1036. init_cdrom_command(&cgc, buf, size, CGC_DATA_UNKNOWN);
  1037. do {
  1038. /* we seem to get stat=0x01,err=0x00 the first time (??) */
  1039. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  1040. if (!stat)
  1041. break;
  1042. } while (--attempts);
  1043. return stat;
  1044. }
  1045. void ide_cdrom_update_speed(ide_drive_t *drive, u8 *buf)
  1046. {
  1047. struct cdrom_info *cd = drive->driver_data;
  1048. u16 curspeed, maxspeed;
  1049. ide_debug_log(IDE_DBG_FUNC, "enter");
  1050. if (drive->atapi_flags & IDE_AFLAG_LE_SPEED_FIELDS) {
  1051. curspeed = le16_to_cpup((__le16 *)&buf[8 + 14]);
  1052. maxspeed = le16_to_cpup((__le16 *)&buf[8 + 8]);
  1053. } else {
  1054. curspeed = be16_to_cpup((__be16 *)&buf[8 + 14]);
  1055. maxspeed = be16_to_cpup((__be16 *)&buf[8 + 8]);
  1056. }
  1057. ide_debug_log(IDE_DBG_PROBE, "curspeed: %u, maxspeed: %u",
  1058. curspeed, maxspeed);
  1059. cd->current_speed = (curspeed + (176/2)) / 176;
  1060. cd->max_speed = (maxspeed + (176/2)) / 176;
  1061. }
  1062. #define IDE_CD_CAPABILITIES \
  1063. (CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK | CDC_SELECT_SPEED | \
  1064. CDC_SELECT_DISC | CDC_MULTI_SESSION | CDC_MCN | CDC_MEDIA_CHANGED | \
  1065. CDC_PLAY_AUDIO | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R | \
  1066. CDC_CD_RW | CDC_DVD | CDC_DVD_R | CDC_DVD_RAM | CDC_GENERIC_PACKET | \
  1067. CDC_MO_DRIVE | CDC_MRW | CDC_MRW_W | CDC_RAM)
  1068. static struct cdrom_device_ops ide_cdrom_dops = {
  1069. .open = ide_cdrom_open_real,
  1070. .release = ide_cdrom_release_real,
  1071. .drive_status = ide_cdrom_drive_status,
  1072. .media_changed = ide_cdrom_check_media_change_real,
  1073. .tray_move = ide_cdrom_tray_move,
  1074. .lock_door = ide_cdrom_lock_door,
  1075. .select_speed = ide_cdrom_select_speed,
  1076. .get_last_session = ide_cdrom_get_last_session,
  1077. .get_mcn = ide_cdrom_get_mcn,
  1078. .reset = ide_cdrom_reset,
  1079. .audio_ioctl = ide_cdrom_audio_ioctl,
  1080. .capability = IDE_CD_CAPABILITIES,
  1081. .generic_packet = ide_cdrom_packet,
  1082. };
  1083. static int ide_cdrom_register(ide_drive_t *drive, int nslots)
  1084. {
  1085. struct cdrom_info *info = drive->driver_data;
  1086. struct cdrom_device_info *devinfo = &info->devinfo;
  1087. ide_debug_log(IDE_DBG_PROBE, "nslots: %d", nslots);
  1088. devinfo->ops = &ide_cdrom_dops;
  1089. devinfo->speed = info->current_speed;
  1090. devinfo->capacity = nslots;
  1091. devinfo->handle = drive;
  1092. strcpy(devinfo->name, drive->name);
  1093. if (drive->atapi_flags & IDE_AFLAG_NO_SPEED_SELECT)
  1094. devinfo->mask |= CDC_SELECT_SPEED;
  1095. devinfo->disk = info->disk;
  1096. return register_cdrom(devinfo);
  1097. }
  1098. static int ide_cdrom_probe_capabilities(ide_drive_t *drive)
  1099. {
  1100. struct cdrom_info *cd = drive->driver_data;
  1101. struct cdrom_device_info *cdi = &cd->devinfo;
  1102. u8 buf[ATAPI_CAPABILITIES_PAGE_SIZE];
  1103. mechtype_t mechtype;
  1104. int nslots = 1;
  1105. ide_debug_log(IDE_DBG_PROBE, "media: 0x%x, atapi_flags: 0x%lx",
  1106. drive->media, drive->atapi_flags);
  1107. cdi->mask = (CDC_CD_R | CDC_CD_RW | CDC_DVD | CDC_DVD_R |
  1108. CDC_DVD_RAM | CDC_SELECT_DISC | CDC_PLAY_AUDIO |
  1109. CDC_MO_DRIVE | CDC_RAM);
  1110. if (drive->media == ide_optical) {
  1111. cdi->mask &= ~(CDC_MO_DRIVE | CDC_RAM);
  1112. printk(KERN_ERR PFX "%s: ATAPI magneto-optical drive\n",
  1113. drive->name);
  1114. return nslots;
  1115. }
  1116. if (drive->atapi_flags & IDE_AFLAG_PRE_ATAPI12) {
  1117. drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
  1118. cdi->mask &= ~CDC_PLAY_AUDIO;
  1119. return nslots;
  1120. }
  1121. /*
  1122. * We have to cheat a little here. the packet will eventually be queued
  1123. * with ide_cdrom_packet(), which extracts the drive from cdi->handle.
  1124. * Since this device hasn't been registered with the Uniform layer yet,
  1125. * it can't do this. Same goes for cdi->ops.
  1126. */
  1127. cdi->handle = drive;
  1128. cdi->ops = &ide_cdrom_dops;
  1129. if (ide_cdrom_get_capabilities(drive, buf))
  1130. return 0;
  1131. if ((buf[8 + 6] & 0x01) == 0)
  1132. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  1133. if (buf[8 + 6] & 0x08)
  1134. drive->atapi_flags &= ~IDE_AFLAG_NO_EJECT;
  1135. if (buf[8 + 3] & 0x01)
  1136. cdi->mask &= ~CDC_CD_R;
  1137. if (buf[8 + 3] & 0x02)
  1138. cdi->mask &= ~(CDC_CD_RW | CDC_RAM);
  1139. if (buf[8 + 2] & 0x38)
  1140. cdi->mask &= ~CDC_DVD;
  1141. if (buf[8 + 3] & 0x20)
  1142. cdi->mask &= ~(CDC_DVD_RAM | CDC_RAM);
  1143. if (buf[8 + 3] & 0x10)
  1144. cdi->mask &= ~CDC_DVD_R;
  1145. if ((buf[8 + 4] & 0x01) || (drive->atapi_flags & IDE_AFLAG_PLAY_AUDIO_OK))
  1146. cdi->mask &= ~CDC_PLAY_AUDIO;
  1147. mechtype = buf[8 + 6] >> 5;
  1148. if (mechtype == mechtype_caddy ||
  1149. mechtype == mechtype_popup ||
  1150. (drive->atapi_flags & IDE_AFLAG_NO_AUTOCLOSE))
  1151. cdi->mask |= CDC_CLOSE_TRAY;
  1152. if (cdi->sanyo_slot > 0) {
  1153. cdi->mask &= ~CDC_SELECT_DISC;
  1154. nslots = 3;
  1155. } else if (mechtype == mechtype_individual_changer ||
  1156. mechtype == mechtype_cartridge_changer) {
  1157. nslots = cdrom_number_of_slots(cdi);
  1158. if (nslots > 1)
  1159. cdi->mask &= ~CDC_SELECT_DISC;
  1160. }
  1161. ide_cdrom_update_speed(drive, buf);
  1162. printk(KERN_INFO PFX "%s: ATAPI", drive->name);
  1163. /* don't print speed if the drive reported 0 */
  1164. if (cd->max_speed)
  1165. printk(KERN_CONT " %dX", cd->max_speed);
  1166. printk(KERN_CONT " %s", (cdi->mask & CDC_DVD) ? "CD-ROM" : "DVD-ROM");
  1167. if ((cdi->mask & CDC_DVD_R) == 0 || (cdi->mask & CDC_DVD_RAM) == 0)
  1168. printk(KERN_CONT " DVD%s%s",
  1169. (cdi->mask & CDC_DVD_R) ? "" : "-R",
  1170. (cdi->mask & CDC_DVD_RAM) ? "" : "/RAM");
  1171. if ((cdi->mask & CDC_CD_R) == 0 || (cdi->mask & CDC_CD_RW) == 0)
  1172. printk(KERN_CONT " CD%s%s",
  1173. (cdi->mask & CDC_CD_R) ? "" : "-R",
  1174. (cdi->mask & CDC_CD_RW) ? "" : "/RW");
  1175. if ((cdi->mask & CDC_SELECT_DISC) == 0)
  1176. printk(KERN_CONT " changer w/%d slots", nslots);
  1177. else
  1178. printk(KERN_CONT " drive");
  1179. printk(KERN_CONT ", %dkB Cache\n",
  1180. be16_to_cpup((__be16 *)&buf[8 + 12]));
  1181. return nslots;
  1182. }
  1183. /* standard prep_rq_fn that builds 10 byte cmds */
  1184. static int ide_cdrom_prep_fs(struct request_queue *q, struct request *rq)
  1185. {
  1186. int hard_sect = queue_hardsect_size(q);
  1187. long block = (long)rq->hard_sector / (hard_sect >> 9);
  1188. unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
  1189. memset(rq->cmd, 0, BLK_MAX_CDB);
  1190. if (rq_data_dir(rq) == READ)
  1191. rq->cmd[0] = GPCMD_READ_10;
  1192. else
  1193. rq->cmd[0] = GPCMD_WRITE_10;
  1194. /*
  1195. * fill in lba
  1196. */
  1197. rq->cmd[2] = (block >> 24) & 0xff;
  1198. rq->cmd[3] = (block >> 16) & 0xff;
  1199. rq->cmd[4] = (block >> 8) & 0xff;
  1200. rq->cmd[5] = block & 0xff;
  1201. /*
  1202. * and transfer length
  1203. */
  1204. rq->cmd[7] = (blocks >> 8) & 0xff;
  1205. rq->cmd[8] = blocks & 0xff;
  1206. rq->cmd_len = 10;
  1207. return BLKPREP_OK;
  1208. }
  1209. /*
  1210. * Most of the SCSI commands are supported directly by ATAPI devices.
  1211. * This transform handles the few exceptions.
  1212. */
  1213. static int ide_cdrom_prep_pc(struct request *rq)
  1214. {
  1215. u8 *c = rq->cmd;
  1216. /* transform 6-byte read/write commands to the 10-byte version */
  1217. if (c[0] == READ_6 || c[0] == WRITE_6) {
  1218. c[8] = c[4];
  1219. c[5] = c[3];
  1220. c[4] = c[2];
  1221. c[3] = c[1] & 0x1f;
  1222. c[2] = 0;
  1223. c[1] &= 0xe0;
  1224. c[0] += (READ_10 - READ_6);
  1225. rq->cmd_len = 10;
  1226. return BLKPREP_OK;
  1227. }
  1228. /*
  1229. * it's silly to pretend we understand 6-byte sense commands, just
  1230. * reject with ILLEGAL_REQUEST and the caller should take the
  1231. * appropriate action
  1232. */
  1233. if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
  1234. rq->errors = ILLEGAL_REQUEST;
  1235. return BLKPREP_KILL;
  1236. }
  1237. return BLKPREP_OK;
  1238. }
  1239. static int ide_cdrom_prep_fn(struct request_queue *q, struct request *rq)
  1240. {
  1241. if (blk_fs_request(rq))
  1242. return ide_cdrom_prep_fs(q, rq);
  1243. else if (blk_pc_request(rq))
  1244. return ide_cdrom_prep_pc(rq);
  1245. return 0;
  1246. }
  1247. struct cd_list_entry {
  1248. const char *id_model;
  1249. const char *id_firmware;
  1250. unsigned int cd_flags;
  1251. };
  1252. #ifdef CONFIG_IDE_PROC_FS
  1253. static sector_t ide_cdrom_capacity(ide_drive_t *drive)
  1254. {
  1255. unsigned long capacity, sectors_per_frame;
  1256. if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
  1257. return 0;
  1258. return capacity * sectors_per_frame;
  1259. }
  1260. static int proc_idecd_read_capacity(char *page, char **start, off_t off,
  1261. int count, int *eof, void *data)
  1262. {
  1263. ide_drive_t *drive = data;
  1264. int len;
  1265. len = sprintf(page, "%llu\n", (long long)ide_cdrom_capacity(drive));
  1266. PROC_IDE_READ_RETURN(page, start, off, count, eof, len);
  1267. }
  1268. static ide_proc_entry_t idecd_proc[] = {
  1269. { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
  1270. { NULL, 0, NULL, NULL }
  1271. };
  1272. static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
  1273. {
  1274. return idecd_proc;
  1275. }
  1276. static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
  1277. {
  1278. return NULL;
  1279. }
  1280. #endif
  1281. static const struct cd_list_entry ide_cd_quirks_list[] = {
  1282. /* SCR-3231 doesn't support the SET_CD_SPEED command. */
  1283. { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT },
  1284. /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
  1285. { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
  1286. IDE_AFLAG_PRE_ATAPI12, },
  1287. /* Vertos 300, some versions of this drive like to talk BCD. */
  1288. { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, },
  1289. /* Vertos 600 ESD. */
  1290. { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, },
  1291. /*
  1292. * Sanyo 3 CD changer uses a non-standard command for CD changing
  1293. * (by default standard ATAPI support for CD changers is used).
  1294. */
  1295. { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD },
  1296. { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD },
  1297. { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD },
  1298. /* Stingray 8X CD-ROM. */
  1299. { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
  1300. /*
  1301. * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
  1302. * mode sense page capabilities size, but older drives break.
  1303. */
  1304. { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
  1305. { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
  1306. /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
  1307. { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS },
  1308. /*
  1309. * Some drives used by Apple don't advertise audio play
  1310. * but they do support reading TOC & audio datas.
  1311. */
  1312. { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1313. { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1314. { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1315. { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1316. { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1317. { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1318. { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE },
  1319. { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE },
  1320. { NULL, NULL, 0 }
  1321. };
  1322. static unsigned int ide_cd_flags(u16 *id)
  1323. {
  1324. const struct cd_list_entry *cle = ide_cd_quirks_list;
  1325. while (cle->id_model) {
  1326. if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
  1327. (cle->id_firmware == NULL ||
  1328. strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
  1329. return cle->cd_flags;
  1330. cle++;
  1331. }
  1332. return 0;
  1333. }
  1334. static int ide_cdrom_setup(ide_drive_t *drive)
  1335. {
  1336. struct cdrom_info *cd = drive->driver_data;
  1337. struct cdrom_device_info *cdi = &cd->devinfo;
  1338. u16 *id = drive->id;
  1339. char *fw_rev = (char *)&id[ATA_ID_FW_REV];
  1340. int nslots;
  1341. ide_debug_log(IDE_DBG_PROBE, "enter");
  1342. blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
  1343. blk_queue_dma_alignment(drive->queue, 31);
  1344. blk_queue_update_dma_pad(drive->queue, 15);
  1345. drive->queue->unplug_delay = (1 * HZ) / 1000;
  1346. if (!drive->queue->unplug_delay)
  1347. drive->queue->unplug_delay = 1;
  1348. drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
  1349. drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
  1350. if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
  1351. fw_rev[4] == '1' && fw_rev[6] <= '2')
  1352. drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
  1353. IDE_AFLAG_TOCADDR_AS_BCD);
  1354. else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
  1355. fw_rev[4] == '1' && fw_rev[6] <= '2')
  1356. drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
  1357. else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
  1358. /* 3 => use CD in slot 0 */
  1359. cdi->sanyo_slot = 3;
  1360. nslots = ide_cdrom_probe_capabilities(drive);
  1361. /* set correct block size */
  1362. blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
  1363. if (ide_cdrom_register(drive, nslots)) {
  1364. printk(KERN_ERR PFX "%s: %s failed to register device with the"
  1365. " cdrom driver.\n", drive->name, __func__);
  1366. cd->devinfo.handle = NULL;
  1367. return 1;
  1368. }
  1369. ide_proc_register_driver(drive, cd->driver);
  1370. return 0;
  1371. }
  1372. static void ide_cd_remove(ide_drive_t *drive)
  1373. {
  1374. struct cdrom_info *info = drive->driver_data;
  1375. ide_debug_log(IDE_DBG_FUNC, "enter");
  1376. ide_proc_unregister_driver(drive, info->driver);
  1377. device_del(&info->dev);
  1378. del_gendisk(info->disk);
  1379. mutex_lock(&idecd_ref_mutex);
  1380. put_device(&info->dev);
  1381. mutex_unlock(&idecd_ref_mutex);
  1382. }
  1383. static void ide_cd_release(struct device *dev)
  1384. {
  1385. struct cdrom_info *info = to_ide_drv(dev, cdrom_info);
  1386. struct cdrom_device_info *devinfo = &info->devinfo;
  1387. ide_drive_t *drive = info->drive;
  1388. struct gendisk *g = info->disk;
  1389. ide_debug_log(IDE_DBG_FUNC, "enter");
  1390. kfree(info->toc);
  1391. if (devinfo->handle == drive)
  1392. unregister_cdrom(devinfo);
  1393. drive->driver_data = NULL;
  1394. blk_queue_prep_rq(drive->queue, NULL);
  1395. g->private_data = NULL;
  1396. put_disk(g);
  1397. kfree(info);
  1398. }
  1399. static int ide_cd_probe(ide_drive_t *);
  1400. static struct ide_driver ide_cdrom_driver = {
  1401. .gen_driver = {
  1402. .owner = THIS_MODULE,
  1403. .name = "ide-cdrom",
  1404. .bus = &ide_bus_type,
  1405. },
  1406. .probe = ide_cd_probe,
  1407. .remove = ide_cd_remove,
  1408. .version = IDECD_VERSION,
  1409. .do_request = ide_cd_do_request,
  1410. #ifdef CONFIG_IDE_PROC_FS
  1411. .proc_entries = ide_cd_proc_entries,
  1412. .proc_devsets = ide_cd_proc_devsets,
  1413. #endif
  1414. };
  1415. static int idecd_open(struct block_device *bdev, fmode_t mode)
  1416. {
  1417. struct cdrom_info *info = ide_cd_get(bdev->bd_disk);
  1418. int rc = -ENOMEM;
  1419. if (!info)
  1420. return -ENXIO;
  1421. rc = cdrom_open(&info->devinfo, bdev, mode);
  1422. if (rc < 0)
  1423. ide_cd_put(info);
  1424. return rc;
  1425. }
  1426. static int idecd_release(struct gendisk *disk, fmode_t mode)
  1427. {
  1428. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1429. cdrom_release(&info->devinfo, mode);
  1430. ide_cd_put(info);
  1431. return 0;
  1432. }
  1433. static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
  1434. {
  1435. struct packet_command cgc;
  1436. char buffer[16];
  1437. int stat;
  1438. char spindown;
  1439. if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
  1440. return -EFAULT;
  1441. init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
  1442. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
  1443. if (stat)
  1444. return stat;
  1445. buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
  1446. return cdrom_mode_select(cdi, &cgc);
  1447. }
  1448. static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
  1449. {
  1450. struct packet_command cgc;
  1451. char buffer[16];
  1452. int stat;
  1453. char spindown;
  1454. init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
  1455. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
  1456. if (stat)
  1457. return stat;
  1458. spindown = buffer[11] & 0x0f;
  1459. if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
  1460. return -EFAULT;
  1461. return 0;
  1462. }
  1463. static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
  1464. unsigned int cmd, unsigned long arg)
  1465. {
  1466. struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
  1467. int err;
  1468. switch (cmd) {
  1469. case CDROMSETSPINDOWN:
  1470. return idecd_set_spindown(&info->devinfo, arg);
  1471. case CDROMGETSPINDOWN:
  1472. return idecd_get_spindown(&info->devinfo, arg);
  1473. default:
  1474. break;
  1475. }
  1476. err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
  1477. if (err == -EINVAL)
  1478. err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
  1479. return err;
  1480. }
  1481. static int idecd_media_changed(struct gendisk *disk)
  1482. {
  1483. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1484. return cdrom_media_changed(&info->devinfo);
  1485. }
  1486. static int idecd_revalidate_disk(struct gendisk *disk)
  1487. {
  1488. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1489. struct request_sense sense;
  1490. ide_cd_read_toc(info->drive, &sense);
  1491. return 0;
  1492. }
  1493. static struct block_device_operations idecd_ops = {
  1494. .owner = THIS_MODULE,
  1495. .open = idecd_open,
  1496. .release = idecd_release,
  1497. .locked_ioctl = idecd_ioctl,
  1498. .media_changed = idecd_media_changed,
  1499. .revalidate_disk = idecd_revalidate_disk
  1500. };
  1501. /* module options */
  1502. static unsigned long debug_mask;
  1503. module_param(debug_mask, ulong, 0644);
  1504. MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
  1505. static int ide_cd_probe(ide_drive_t *drive)
  1506. {
  1507. struct cdrom_info *info;
  1508. struct gendisk *g;
  1509. struct request_sense sense;
  1510. ide_debug_log(IDE_DBG_PROBE, "driver_req: %s, media: 0x%x",
  1511. drive->driver_req, drive->media);
  1512. if (!strstr("ide-cdrom", drive->driver_req))
  1513. goto failed;
  1514. if (drive->media != ide_cdrom && drive->media != ide_optical)
  1515. goto failed;
  1516. drive->debug_mask = debug_mask;
  1517. drive->irq_handler = cdrom_newpc_intr;
  1518. info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
  1519. if (info == NULL) {
  1520. printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
  1521. drive->name);
  1522. goto failed;
  1523. }
  1524. g = alloc_disk(1 << PARTN_BITS);
  1525. if (!g)
  1526. goto out_free_cd;
  1527. ide_init_disk(g, drive);
  1528. info->dev.parent = &drive->gendev;
  1529. info->dev.release = ide_cd_release;
  1530. dev_set_name(&info->dev, dev_name(&drive->gendev));
  1531. if (device_register(&info->dev))
  1532. goto out_free_disk;
  1533. info->drive = drive;
  1534. info->driver = &ide_cdrom_driver;
  1535. info->disk = g;
  1536. g->private_data = &info->driver;
  1537. drive->driver_data = info;
  1538. g->minors = 1;
  1539. g->driverfs_dev = &drive->gendev;
  1540. g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
  1541. if (ide_cdrom_setup(drive)) {
  1542. put_device(&info->dev);
  1543. goto failed;
  1544. }
  1545. ide_cd_read_toc(drive, &sense);
  1546. g->fops = &idecd_ops;
  1547. g->flags |= GENHD_FL_REMOVABLE;
  1548. add_disk(g);
  1549. return 0;
  1550. out_free_disk:
  1551. put_disk(g);
  1552. out_free_cd:
  1553. kfree(info);
  1554. failed:
  1555. return -ENODEV;
  1556. }
  1557. static void __exit ide_cdrom_exit(void)
  1558. {
  1559. driver_unregister(&ide_cdrom_driver.gen_driver);
  1560. }
  1561. static int __init ide_cdrom_init(void)
  1562. {
  1563. printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
  1564. return driver_register(&ide_cdrom_driver.gen_driver);
  1565. }
  1566. MODULE_ALIAS("ide:*m-cdrom*");
  1567. MODULE_ALIAS("ide-cd");
  1568. module_init(ide_cdrom_init);
  1569. module_exit(ide_cdrom_exit);
  1570. MODULE_LICENSE("GPL");