ide-cd.c 60 KB

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