ide-cd.c 60 KB

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