ide-cd.c 56 KB

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