ide-cd.c 52 KB

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