ide-cd.c 56 KB

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