ide-cd.c 47 KB

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