ide-cd.c 53 KB

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