ide-cd.c 53 KB

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