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