ide-cd.c 48 KB

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