ide-cd.c 85 KB

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