ide-cd.c 46 KB

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