ide-cd.c 52 KB

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