ide-cd.c 45 KB

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