ide-cd.c 56 KB

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