ide-cd.c 54 KB

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