ide-cd.c 53 KB

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