ide-cd.c 58 KB

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