ide-cd.c 56 KB

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