ide-cd.c 56 KB

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