ide-cd.c 56 KB

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