ide-cd.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  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. static ide_proc_entry_t *ide_cd_proc_entries(ide_drive_t *drive)
  1541. {
  1542. return idecd_proc;
  1543. }
  1544. static const struct ide_proc_devset *ide_cd_proc_devsets(ide_drive_t *drive)
  1545. {
  1546. return NULL;
  1547. }
  1548. #endif
  1549. static const struct cd_list_entry ide_cd_quirks_list[] = {
  1550. /* Limit transfer size per interrupt. */
  1551. { "SAMSUNG CD-ROM SCR-2430", NULL, IDE_AFLAG_LIMIT_NFRAMES },
  1552. { "SAMSUNG CD-ROM SCR-2432", NULL, IDE_AFLAG_LIMIT_NFRAMES },
  1553. /* SCR-3231 doesn't support the SET_CD_SPEED command. */
  1554. { "SAMSUNG CD-ROM SCR-3231", NULL, IDE_AFLAG_NO_SPEED_SELECT },
  1555. /* Old NEC260 (not R) was released before ATAPI 1.2 spec. */
  1556. { "NEC CD-ROM DRIVE:260", "1.01", IDE_AFLAG_TOCADDR_AS_BCD |
  1557. IDE_AFLAG_PRE_ATAPI12, },
  1558. /* Vertos 300, some versions of this drive like to talk BCD. */
  1559. { "V003S0DS", NULL, IDE_AFLAG_VERTOS_300_SSD, },
  1560. /* Vertos 600 ESD. */
  1561. { "V006E0DS", NULL, IDE_AFLAG_VERTOS_600_ESD, },
  1562. /*
  1563. * Sanyo 3 CD changer uses a non-standard command for CD changing
  1564. * (by default standard ATAPI support for CD changers is used).
  1565. */
  1566. { "CD-ROM CDR-C3 G", NULL, IDE_AFLAG_SANYO_3CD },
  1567. { "CD-ROM CDR-C3G", NULL, IDE_AFLAG_SANYO_3CD },
  1568. { "CD-ROM CDR_C36", NULL, IDE_AFLAG_SANYO_3CD },
  1569. /* Stingray 8X CD-ROM. */
  1570. { "STINGRAY 8422 IDE 8X CD-ROM 7-27-95", NULL, IDE_AFLAG_PRE_ATAPI12 },
  1571. /*
  1572. * ACER 50X CD-ROM and WPI 32X CD-ROM require the full spec length
  1573. * mode sense page capabilities size, but older drives break.
  1574. */
  1575. { "ATAPI CD ROM DRIVE 50X MAX", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
  1576. { "WPI CDS-32X", NULL, IDE_AFLAG_FULL_CAPS_PAGE },
  1577. /* ACER/AOpen 24X CD-ROM has the speed fields byte-swapped. */
  1578. { "", "241N", IDE_AFLAG_LE_SPEED_FIELDS },
  1579. /*
  1580. * Some drives used by Apple don't advertise audio play
  1581. * but they do support reading TOC & audio datas.
  1582. */
  1583. { "MATSHITADVD-ROM SR-8187", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1584. { "MATSHITADVD-ROM SR-8186", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1585. { "MATSHITADVD-ROM SR-8176", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1586. { "MATSHITADVD-ROM SR-8174", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1587. { "Optiarc DVD RW AD-5200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1588. { "Optiarc DVD RW AD-7200A", NULL, IDE_AFLAG_PLAY_AUDIO_OK },
  1589. { "Optiarc DVD RW AD-7543A", NULL, IDE_AFLAG_NO_AUTOCLOSE },
  1590. { "TEAC CD-ROM CD-224E", NULL, IDE_AFLAG_NO_AUTOCLOSE },
  1591. { NULL, NULL, 0 }
  1592. };
  1593. static unsigned int ide_cd_flags(u16 *id)
  1594. {
  1595. const struct cd_list_entry *cle = ide_cd_quirks_list;
  1596. while (cle->id_model) {
  1597. if (strcmp(cle->id_model, (char *)&id[ATA_ID_PROD]) == 0 &&
  1598. (cle->id_firmware == NULL ||
  1599. strstr((char *)&id[ATA_ID_FW_REV], cle->id_firmware)))
  1600. return cle->cd_flags;
  1601. cle++;
  1602. }
  1603. return 0;
  1604. }
  1605. static int ide_cdrom_setup(ide_drive_t *drive)
  1606. {
  1607. struct cdrom_info *cd = drive->driver_data;
  1608. struct cdrom_device_info *cdi = &cd->devinfo;
  1609. u16 *id = drive->id;
  1610. char *fw_rev = (char *)&id[ATA_ID_FW_REV];
  1611. int nslots;
  1612. ide_debug_log(IDE_DBG_PROBE, "Call %s\n", __func__);
  1613. blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
  1614. blk_queue_dma_alignment(drive->queue, 31);
  1615. blk_queue_update_dma_pad(drive->queue, 15);
  1616. drive->queue->unplug_delay = (1 * HZ) / 1000;
  1617. if (!drive->queue->unplug_delay)
  1618. drive->queue->unplug_delay = 1;
  1619. drive->dev_flags |= IDE_DFLAG_MEDIA_CHANGED;
  1620. drive->atapi_flags = IDE_AFLAG_NO_EJECT | ide_cd_flags(id);
  1621. if ((drive->atapi_flags & IDE_AFLAG_VERTOS_300_SSD) &&
  1622. fw_rev[4] == '1' && fw_rev[6] <= '2')
  1623. drive->atapi_flags |= (IDE_AFLAG_TOCTRACKS_AS_BCD |
  1624. IDE_AFLAG_TOCADDR_AS_BCD);
  1625. else if ((drive->atapi_flags & IDE_AFLAG_VERTOS_600_ESD) &&
  1626. fw_rev[4] == '1' && fw_rev[6] <= '2')
  1627. drive->atapi_flags |= IDE_AFLAG_TOCTRACKS_AS_BCD;
  1628. else if (drive->atapi_flags & IDE_AFLAG_SANYO_3CD)
  1629. /* 3 => use CD in slot 0 */
  1630. cdi->sanyo_slot = 3;
  1631. nslots = ide_cdrom_probe_capabilities(drive);
  1632. /* set correct block size */
  1633. blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
  1634. if (ide_cdrom_register(drive, nslots)) {
  1635. printk(KERN_ERR PFX "%s: %s failed to register device with the"
  1636. " cdrom driver.\n", drive->name, __func__);
  1637. cd->devinfo.handle = NULL;
  1638. return 1;
  1639. }
  1640. ide_proc_register_driver(drive, cd->driver);
  1641. return 0;
  1642. }
  1643. static void ide_cd_remove(ide_drive_t *drive)
  1644. {
  1645. struct cdrom_info *info = drive->driver_data;
  1646. ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
  1647. ide_proc_unregister_driver(drive, info->driver);
  1648. del_gendisk(info->disk);
  1649. ide_cd_put(info);
  1650. }
  1651. static void ide_cd_release(struct kref *kref)
  1652. {
  1653. struct cdrom_info *info = to_ide_drv(kref, cdrom_info);
  1654. struct cdrom_device_info *devinfo = &info->devinfo;
  1655. ide_drive_t *drive = info->drive;
  1656. struct gendisk *g = info->disk;
  1657. ide_debug_log(IDE_DBG_FUNC, "Call %s\n", __func__);
  1658. kfree(info->toc);
  1659. if (devinfo->handle == drive)
  1660. unregister_cdrom(devinfo);
  1661. drive->driver_data = NULL;
  1662. blk_queue_prep_rq(drive->queue, NULL);
  1663. g->private_data = NULL;
  1664. put_disk(g);
  1665. kfree(info);
  1666. }
  1667. static int ide_cd_probe(ide_drive_t *);
  1668. static ide_driver_t ide_cdrom_driver = {
  1669. .gen_driver = {
  1670. .owner = THIS_MODULE,
  1671. .name = "ide-cdrom",
  1672. .bus = &ide_bus_type,
  1673. },
  1674. .probe = ide_cd_probe,
  1675. .remove = ide_cd_remove,
  1676. .version = IDECD_VERSION,
  1677. .do_request = ide_cd_do_request,
  1678. .end_request = ide_end_request,
  1679. .error = __ide_error,
  1680. #ifdef CONFIG_IDE_PROC_FS
  1681. .proc_entries = ide_cd_proc_entries,
  1682. .proc_devsets = ide_cd_proc_devsets,
  1683. #endif
  1684. };
  1685. static int idecd_open(struct block_device *bdev, fmode_t mode)
  1686. {
  1687. struct cdrom_info *info = ide_cd_get(bdev->bd_disk);
  1688. int rc = -ENOMEM;
  1689. if (!info)
  1690. return -ENXIO;
  1691. rc = cdrom_open(&info->devinfo, bdev, mode);
  1692. if (rc < 0)
  1693. ide_cd_put(info);
  1694. return rc;
  1695. }
  1696. static int idecd_release(struct gendisk *disk, fmode_t mode)
  1697. {
  1698. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1699. cdrom_release(&info->devinfo, mode);
  1700. ide_cd_put(info);
  1701. return 0;
  1702. }
  1703. static int idecd_set_spindown(struct cdrom_device_info *cdi, unsigned long arg)
  1704. {
  1705. struct packet_command cgc;
  1706. char buffer[16];
  1707. int stat;
  1708. char spindown;
  1709. if (copy_from_user(&spindown, (void __user *)arg, sizeof(char)))
  1710. return -EFAULT;
  1711. init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
  1712. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
  1713. if (stat)
  1714. return stat;
  1715. buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
  1716. return cdrom_mode_select(cdi, &cgc);
  1717. }
  1718. static int idecd_get_spindown(struct cdrom_device_info *cdi, unsigned long arg)
  1719. {
  1720. struct packet_command cgc;
  1721. char buffer[16];
  1722. int stat;
  1723. char spindown;
  1724. init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
  1725. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0);
  1726. if (stat)
  1727. return stat;
  1728. spindown = buffer[11] & 0x0f;
  1729. if (copy_to_user((void __user *)arg, &spindown, sizeof(char)))
  1730. return -EFAULT;
  1731. return 0;
  1732. }
  1733. static int idecd_ioctl(struct block_device *bdev, fmode_t mode,
  1734. unsigned int cmd, unsigned long arg)
  1735. {
  1736. struct cdrom_info *info = ide_drv_g(bdev->bd_disk, cdrom_info);
  1737. int err;
  1738. switch (cmd) {
  1739. case CDROMSETSPINDOWN:
  1740. return idecd_set_spindown(&info->devinfo, arg);
  1741. case CDROMGETSPINDOWN:
  1742. return idecd_get_spindown(&info->devinfo, arg);
  1743. default:
  1744. break;
  1745. }
  1746. err = generic_ide_ioctl(info->drive, bdev, cmd, arg);
  1747. if (err == -EINVAL)
  1748. err = cdrom_ioctl(&info->devinfo, bdev, mode, cmd, arg);
  1749. return err;
  1750. }
  1751. static int idecd_media_changed(struct gendisk *disk)
  1752. {
  1753. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1754. return cdrom_media_changed(&info->devinfo);
  1755. }
  1756. static int idecd_revalidate_disk(struct gendisk *disk)
  1757. {
  1758. struct cdrom_info *info = ide_drv_g(disk, cdrom_info);
  1759. struct request_sense sense;
  1760. ide_cd_read_toc(info->drive, &sense);
  1761. return 0;
  1762. }
  1763. static struct block_device_operations idecd_ops = {
  1764. .owner = THIS_MODULE,
  1765. .open = idecd_open,
  1766. .release = idecd_release,
  1767. .locked_ioctl = idecd_ioctl,
  1768. .media_changed = idecd_media_changed,
  1769. .revalidate_disk = idecd_revalidate_disk
  1770. };
  1771. /* module options */
  1772. static char *ignore;
  1773. module_param(ignore, charp, 0400);
  1774. static unsigned long debug_mask;
  1775. module_param(debug_mask, ulong, 0644);
  1776. MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
  1777. static int ide_cd_probe(ide_drive_t *drive)
  1778. {
  1779. struct cdrom_info *info;
  1780. struct gendisk *g;
  1781. struct request_sense sense;
  1782. ide_debug_log(IDE_DBG_PROBE, "Call %s, drive->driver_req: %s, "
  1783. "drive->media: 0x%x\n", __func__, drive->driver_req,
  1784. drive->media);
  1785. if (!strstr("ide-cdrom", drive->driver_req))
  1786. goto failed;
  1787. if (drive->media != ide_cdrom && drive->media != ide_optical)
  1788. goto failed;
  1789. /* skip drives that we were told to ignore */
  1790. if (ignore != NULL) {
  1791. if (strstr(ignore, drive->name)) {
  1792. printk(KERN_INFO PFX "ignoring drive %s\n",
  1793. drive->name);
  1794. goto failed;
  1795. }
  1796. }
  1797. drive->debug_mask = debug_mask;
  1798. info = kzalloc(sizeof(struct cdrom_info), GFP_KERNEL);
  1799. if (info == NULL) {
  1800. printk(KERN_ERR PFX "%s: Can't allocate a cdrom structure\n",
  1801. drive->name);
  1802. goto failed;
  1803. }
  1804. g = alloc_disk(1 << PARTN_BITS);
  1805. if (!g)
  1806. goto out_free_cd;
  1807. ide_init_disk(g, drive);
  1808. kref_init(&info->kref);
  1809. info->drive = drive;
  1810. info->driver = &ide_cdrom_driver;
  1811. info->disk = g;
  1812. g->private_data = &info->driver;
  1813. drive->driver_data = info;
  1814. g->minors = 1;
  1815. g->driverfs_dev = &drive->gendev;
  1816. g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
  1817. if (ide_cdrom_setup(drive)) {
  1818. ide_cd_release(&info->kref);
  1819. goto failed;
  1820. }
  1821. ide_cd_read_toc(drive, &sense);
  1822. g->fops = &idecd_ops;
  1823. g->flags |= GENHD_FL_REMOVABLE;
  1824. add_disk(g);
  1825. return 0;
  1826. out_free_cd:
  1827. kfree(info);
  1828. failed:
  1829. return -ENODEV;
  1830. }
  1831. static void __exit ide_cdrom_exit(void)
  1832. {
  1833. driver_unregister(&ide_cdrom_driver.gen_driver);
  1834. }
  1835. static int __init ide_cdrom_init(void)
  1836. {
  1837. printk(KERN_INFO DRV_NAME " driver " IDECD_VERSION "\n");
  1838. return driver_register(&ide_cdrom_driver.gen_driver);
  1839. }
  1840. MODULE_ALIAS("ide:*m-cdrom*");
  1841. MODULE_ALIAS("ide-cd");
  1842. module_init(ide_cdrom_init);
  1843. module_exit(ide_cdrom_exit);
  1844. MODULE_LICENSE("GPL");