ide-cd.c 48 KB

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