ide-cd.c 53 KB

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