ide-cd.c 46 KB

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