ide-cd.c 70 KB

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