ide-cd.c 56 KB

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