ide-cd.c 54 KB

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