ide-cd.c 46 KB

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