ide-cd.c 60 KB

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