ide-cd.c 53 KB

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