ide-cd.c 56 KB

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