ide-cd.c 46 KB

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