ide-cd.c 57 KB

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