ide-cd.c 56 KB

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