PageRenderTime 78ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/ide/ide-cd.c

https://bitbucket.org/evzijst/gittest
C | 3524 lines | 2182 code | 535 blank | 807 comment | 568 complexity | 28a50b51c4f2de81781a39e90cf8186b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * linux/drivers/ide/ide-cd.c
  3. *
  4. * Copyright (C) 1994, 1995, 1996 scott snyder <snyder@fnald0.fnal.gov>
  5. * Copyright (C) 1996-1998 Erik Andersen <andersee@debian.org>
  6. * Copyright (C) 1998-2000 Jens Axboe <axboe@suse.de>
  7. *
  8. * May be copied or modified under the terms of the GNU General Public
  9. * License. See linux/COPYING for more information.
  10. *
  11. * ATAPI CD-ROM driver. To be used with ide.c.
  12. * See Documentation/cdrom/ide-cd for usage information.
  13. *
  14. * Suggestions are welcome. Patches that work are more welcome though. ;-)
  15. * For those wishing to work on this driver, please be sure you download
  16. * and comply with the latest Mt. Fuji (SFF8090 version 4) and ATAPI
  17. * (SFF-8020i rev 2.6) standards. These documents can be obtained by
  18. * anonymous ftp from:
  19. * ftp://fission.dt.wdc.com/pub/standards/SFF_atapi/spec/SFF8020-r2.6/PS/8020r26.ps
  20. * ftp://ftp.avc-pioneer.com/Mtfuji4/Spec/Fuji4r10.pdf
  21. *
  22. * Drives that deviate from these standards will be accommodated as much
  23. * as possible via compile time or command-line options. Since I only have
  24. * a few drives, you generally need to send me patches...
  25. *
  26. * ----------------------------------
  27. * TO DO LIST:
  28. * -Make it so that Pioneer CD DR-A24X and friends don't get screwed up on
  29. * boot
  30. *
  31. * ----------------------------------
  32. * 1.00 Oct 31, 1994 -- Initial version.
  33. * 1.01 Nov 2, 1994 -- Fixed problem with starting request in
  34. * cdrom_check_status.
  35. * 1.03 Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
  36. * (from mlord) -- minor changes to cdrom_setup()
  37. * -- renamed ide_dev_s to ide_drive_t, enable irq on command
  38. * 2.00 Nov 27, 1994 -- Generalize packet command interface;
  39. * add audio ioctls.
  40. * 2.01 Dec 3, 1994 -- Rework packet command interface to handle devices
  41. * which send an interrupt when ready for a command.
  42. * 2.02 Dec 11, 1994 -- Cache the TOC in the driver.
  43. * Don't use SCMD_PLAYAUDIO_TI; it's not included
  44. * in the current version of ATAPI.
  45. * Try to use LBA instead of track or MSF addressing
  46. * when possible.
  47. * Don't wait for READY_STAT.
  48. * 2.03 Jan 10, 1995 -- Rewrite block read routines to handle block sizes
  49. * other than 2k and to move multiple sectors in a
  50. * single transaction.
  51. * 2.04 Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
  52. * Thanks to Nick Saw <cwsaw@pts7.pts.mot.com> for
  53. * help in figuring this out. Ditto for Acer and
  54. * Aztech drives, which seem to have the same problem.
  55. * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
  56. * 2.05 Jun 8, 1995 -- Don't attempt to retry after an illegal request
  57. * or data protect error.
  58. * Use HWIF and DEV_HWIF macros as in ide.c.
  59. * Always try to do a request_sense after
  60. * a failed command.
  61. * Include an option to give textual descriptions
  62. * of ATAPI errors.
  63. * Fix a bug in handling the sector cache which
  64. * showed up if the drive returned data in 512 byte
  65. * blocks (like Pioneer drives). Thanks to
  66. * Richard Hirst <srh@gpt.co.uk> for diagnosing this.
  67. * Properly supply the page number field in the
  68. * MODE_SELECT command.
  69. * PLAYAUDIO12 is broken on the Aztech; work around it.
  70. * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
  71. * (my apologies to Scott, but now ide-cd.c is independent)
  72. * 3.00 Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
  73. * Implement CDROMREADAUDIO ioctl (UNTESTED).
  74. * Use input_ide_data() and output_ide_data().
  75. * Add door locking.
  76. * Fix usage count leak in cdrom_open, which happened
  77. * when a read-write mount was attempted.
  78. * Try to load the disk on open.
  79. * Implement CDROMEJECT_SW ioctl (off by default).
  80. * Read total cdrom capacity during open.
  81. * Rearrange logic in cdrom_decode_status. Issue
  82. * request sense commands for failed packet commands
  83. * from here instead of from cdrom_queue_packet_command.
  84. * Fix a race condition in retrieving error information.
  85. * Suppress printing normal unit attention errors and
  86. * some drive not ready errors.
  87. * Implement CDROMVOLREAD ioctl.
  88. * Implement CDROMREADMODE1/2 ioctls.
  89. * Fix race condition in setting up interrupt handlers
  90. * when the `serialize' option is used.
  91. * 3.01 Sep 2, 1995 -- Fix ordering of reenabling interrupts in
  92. * cdrom_queue_request.
  93. * Another try at using ide_[input,output]_data.
  94. * 3.02 Sep 16, 1995 -- Stick total disk capacity in partition table as well.
  95. * Make VERBOSE_IDE_CD_ERRORS dump failed command again.
  96. * Dump out more information for ILLEGAL REQUEST errs.
  97. * Fix handling of errors occurring before the
  98. * packet command is transferred.
  99. * Fix transfers with odd bytelengths.
  100. * 3.03 Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
  101. * `DCI-2S10' drives are broken too.
  102. * 3.04 Nov 20, 1995 -- So are Vertos drives.
  103. * 3.05 Dec 1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
  104. * 3.06 Dec 16, 1995 -- Add support needed for partitions.
  105. * More workarounds for Vertos bugs (based on patches
  106. * from Holger Dietze <dietze@aix520.informatik.uni-leipzig.de>).
  107. * Try to eliminate byteorder assumptions.
  108. * Use atapi_cdrom_subchnl struct definition.
  109. * Add STANDARD_ATAPI compilation option.
  110. * 3.07 Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
  111. * Vertos 300.
  112. * Add NO_DOOR_LOCKING configuration option.
  113. * Handle drive_cmd requests w/NULL args (for hdparm -t).
  114. * Work around sporadic Sony55e audio play problem.
  115. * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
  116. * problem with "hde=cdrom" with no drive present. -ml
  117. * 3.08 Mar 6, 1996 -- More Vertos workarounds.
  118. * 3.09 Apr 5, 1996 -- Add CDROMCLOSETRAY ioctl.
  119. * Switch to using MSF addressing for audio commands.
  120. * Reformat to match kernel tabbing style.
  121. * Add CDROM_GET_UPC ioctl.
  122. * 3.10 Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
  123. * 3.11 Apr 29, 1996 -- Patch from Heiko Eissfeldt <heiko@colossus.escape.de>
  124. * to remove redundant verify_area calls.
  125. * 3.12 May 7, 1996 -- Rudimentary changer support. Based on patches
  126. * from Gerhard Zuber <zuber@berlin.snafu.de>.
  127. * Let open succeed even if there's no loaded disc.
  128. * 3.13 May 19, 1996 -- Fixes for changer code.
  129. * 3.14 May 29, 1996 -- Add work-around for Vertos 600.
  130. * (From Hennus Bergman <hennus@sky.ow.nl>.)
  131. * 3.15 July 2, 1996 -- Added support for Sanyo 3 CD changers
  132. * from Ben Galliart <bgallia@luc.edu> with
  133. * special help from Jeff Lightfoot
  134. * <jeffml@pobox.com>
  135. * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
  136. * 3.16 Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
  137. * 3.17 Sep 17, 1996 -- Tweak audio reads for some drives.
  138. * Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
  139. * 3.18 Oct 31, 1996 -- Added module and DMA support.
  140. *
  141. *
  142. * 4.00 Nov 5, 1996 -- New ide-cd maintainer,
  143. * Erik B. Andersen <andersee@debian.org>
  144. * -- Newer Creative drives don't always set the error
  145. * register correctly. Make sure we see media changes
  146. * regardless.
  147. * -- Integrate with generic cdrom driver.
  148. * -- CDROMGETSPINDOWN and CDROMSETSPINDOWN ioctls, based on
  149. * a patch from Ciro Cattuto <>.
  150. * -- Call set_device_ro.
  151. * -- Implement CDROMMECHANISMSTATUS and CDROMSLOTTABLE
  152. * ioctls, based on patch by Erik Andersen
  153. * -- Add some probes of drive capability during setup.
  154. *
  155. * 4.01 Nov 11, 1996 -- Split into ide-cd.c and ide-cd.h
  156. * -- Removed CDROMMECHANISMSTATUS and CDROMSLOTTABLE
  157. * ioctls in favor of a generalized approach
  158. * using the generic cdrom driver.
  159. * -- Fully integrated with the 2.1.X kernel.
  160. * -- Other stuff that I forgot (lots of changes)
  161. *
  162. * 4.02 Dec 01, 1996 -- Applied patch from Gadi Oxman <gadio@netvision.net.il>
  163. * to fix the drive door locking problems.
  164. *
  165. * 4.03 Dec 04, 1996 -- Added DSC overlap support.
  166. * 4.04 Dec 29, 1996 -- Added CDROMREADRAW ioclt based on patch
  167. * by Ales Makarov (xmakarov@sun.felk.cvut.cz)
  168. *
  169. * 4.05 Nov 20, 1997 -- Modified to print more drive info on init
  170. * Minor other changes
  171. * Fix errors on CDROMSTOP (If you have a "Dolphin",
  172. * you must define IHAVEADOLPHIN)
  173. * Added identifier so new Sanyo CD-changer works
  174. * Better detection if door locking isn't supported
  175. *
  176. * 4.06 Dec 17, 1997 -- fixed endless "tray open" messages -ml
  177. * 4.07 Dec 17, 1997 -- fallback to set pc->stat on "tray open"
  178. * 4.08 Dec 18, 1997 -- spew less noise when tray is empty
  179. * -- fix speed display for ACER 24X, 18X
  180. * 4.09 Jan 04, 1998 -- fix handling of the last block so we return
  181. * an end of file instead of an I/O error (Gadi)
  182. * 4.10 Jan 24, 1998 -- fixed a bug so now changers can change to a new
  183. * slot when there is no disc in the current slot.
  184. * -- Fixed a memory leak where info->changer_info was
  185. * malloc'ed but never free'd when closing the device.
  186. * -- Cleaned up the global namespace a bit by making more
  187. * functions static that should already have been.
  188. * 4.11 Mar 12, 1998 -- Added support for the CDROM_SELECT_SPEED ioctl
  189. * based on a patch for 2.0.33 by Jelle Foks
  190. * <jelle@scintilla.utwente.nl>, a patch for 2.0.33
  191. * by Toni Giorgino <toni@pcape2.pi.infn.it>, the SCSI
  192. * version, and my own efforts. -erik
  193. * -- Fixed a stupid bug which egcs was kind enough to
  194. * inform me of where "Illegal mode for this track"
  195. * was never returned due to a comparison on data
  196. * types of limited range.
  197. * 4.12 Mar 29, 1998 -- Fixed bug in CDROM_SELECT_SPEED so write speed is
  198. * now set ionly for CD-R and CD-RW drives. I had
  199. * removed this support because it produced errors.
  200. * It produced errors _only_ for non-writers. duh.
  201. * 4.13 May 05, 1998 -- Suppress useless "in progress of becoming ready"
  202. * messages, since this is not an error.
  203. * -- Change error messages to be const
  204. * -- Remove a "\t" which looks ugly in the syslogs
  205. * 4.14 July 17, 1998 -- Change to pointing to .ps version of ATAPI spec
  206. * since the .pdf version doesn't seem to work...
  207. * -- Updated the TODO list to something more current.
  208. *
  209. * 4.15 Aug 25, 1998 -- Updated ide-cd.h to respect mechine endianess,
  210. * patch thanks to "Eddie C. Dost" <ecd@skynet.be>
  211. *
  212. * 4.50 Oct 19, 1998 -- New maintainers!
  213. * Jens Axboe <axboe@image.dk>
  214. * Chris Zwilling <chris@cloudnet.com>
  215. *
  216. * 4.51 Dec 23, 1998 -- Jens Axboe <axboe@image.dk>
  217. * - ide_cdrom_reset enabled since the ide subsystem
  218. * handles resets fine now. <axboe@image.dk>
  219. * - Transfer size fix for Samsung CD-ROMs, thanks to
  220. * "Ville Hallik" <ville.hallik@mail.ee>.
  221. * - other minor stuff.
  222. *
  223. * 4.52 Jan 19, 1999 -- Jens Axboe <axboe@image.dk>
  224. * - Detect DVD-ROM/RAM drives
  225. *
  226. * 4.53 Feb 22, 1999 - Include other model Samsung and one Goldstar
  227. * drive in transfer size limit.
  228. * - Fix the I/O error when doing eject without a medium
  229. * loaded on some drives.
  230. * - CDROMREADMODE2 is now implemented through
  231. * CDROMREADRAW, since many drives don't support
  232. * MODE2 (even though ATAPI 2.6 says they must).
  233. * - Added ignore parameter to ide-cd (as a module), eg
  234. * insmod ide-cd ignore='hda hdb'
  235. * Useful when using ide-cd in conjunction with
  236. * ide-scsi. TODO: non-modular way of doing the
  237. * same.
  238. *
  239. * 4.54 Aug 5, 1999 - Support for MMC2 class commands through the generic
  240. * packet interface to cdrom.c.
  241. * - Unified audio ioctl support, most of it.
  242. * - cleaned up various deprecated verify_area().
  243. * - Added ide_cdrom_packet() as the interface for
  244. * the Uniform generic_packet().
  245. * - bunch of other stuff, will fill in logs later.
  246. * - report 1 slot for non-changers, like the other
  247. * cd-rom drivers. don't report select disc for
  248. * non-changers as well.
  249. * - mask out audio playing, if the device can't do it.
  250. *
  251. * 4.55 Sep 1, 1999 - Eliminated the rest of the audio ioctls, except
  252. * for CDROMREADTOC[ENTRY|HEADER]. Some of the drivers
  253. * use this independently of the actual audio handling.
  254. * They will disappear later when I get the time to
  255. * do it cleanly.
  256. * - Minimize the TOC reading - only do it when we
  257. * know a media change has occurred.
  258. * - Moved all the CDROMREADx ioctls to the Uniform layer.
  259. * - Heiko Eissfeldt <heiko@colossus.escape.de> supplied
  260. * some fixes for CDI.
  261. * - CD-ROM leaving door locked fix from Andries
  262. * Brouwer <Andries.Brouwer@cwi.nl>
  263. * - Erik Andersen <andersen@xmission.com> unified
  264. * commands across the various drivers and how
  265. * sense errors are handled.
  266. *
  267. * 4.56 Sep 12, 1999 - Removed changer support - it is now in the
  268. * Uniform layer.
  269. * - Added partition based multisession handling.
  270. * - Mode sense and mode select moved to the
  271. * Uniform layer.
  272. * - Fixed a problem with WPI CDS-32X drive - it
  273. * failed the capabilities
  274. *
  275. * 4.57 Apr 7, 2000 - Fixed sense reporting.
  276. * - Fixed possible oops in ide_cdrom_get_last_session()
  277. * - Fix locking mania and make ide_cdrom_reset relock
  278. * - Stop spewing errors to log when magicdev polls with
  279. * TEST_UNIT_READY on some drives.
  280. * - Various fixes from Tobias Ringstrom:
  281. * tray if it was locked prior to the reset.
  282. * - cdrom_read_capacity returns one frame too little.
  283. * - Fix real capacity reporting.
  284. *
  285. * 4.58 May 1, 2000 - Clean up ACER50 stuff.
  286. * - Fix small problem with ide_cdrom_capacity
  287. *
  288. * 4.59 Aug 11, 2000 - Fix changer problem in cdrom_read_toc, we weren't
  289. * correctly sensing a disc change.
  290. * - Rearranged some code
  291. * - Use extended sense on drives that support it for
  292. * correctly reporting tray status -- from
  293. * Michael D Johnson <johnsom@orst.edu>
  294. * 4.60 Dec 17, 2003 - Add mt rainier support
  295. * - Bump timeout for packet commands, matches sr
  296. * - Odd stuff
  297. * 4.61 Jan 22, 2004 - support hardware sector sizes other than 2kB,
  298. * Pascal Schmidt <der.eremit@email.de>
  299. *
  300. *************************************************************************/
  301. #define IDECD_VERSION "4.61"
  302. #include <linux/config.h>
  303. #include <linux/module.h>
  304. #include <linux/types.h>
  305. #include <linux/kernel.h>
  306. #include <linux/delay.h>
  307. #include <linux/timer.h>
  308. #include <linux/slab.h>
  309. #include <linux/interrupt.h>
  310. #include <linux/errno.h>
  311. #include <linux/cdrom.h>
  312. #include <linux/ide.h>
  313. #include <linux/completion.h>
  314. #include <scsi/scsi.h> /* For SCSI -> ATAPI command conversion */
  315. #include <asm/irq.h>
  316. #include <asm/io.h>
  317. #include <asm/byteorder.h>
  318. #include <asm/uaccess.h>
  319. #include <asm/unaligned.h>
  320. #include "ide-cd.h"
  321. static DECLARE_MUTEX(idecd_ref_sem);
  322. #define to_ide_cd(obj) container_of(obj, struct cdrom_info, kref)
  323. #define ide_cd_g(disk) \
  324. container_of((disk)->private_data, struct cdrom_info, driver)
  325. static struct cdrom_info *ide_cd_get(struct gendisk *disk)
  326. {
  327. struct cdrom_info *cd = NULL;
  328. down(&idecd_ref_sem);
  329. cd = ide_cd_g(disk);
  330. if (cd)
  331. kref_get(&cd->kref);
  332. up(&idecd_ref_sem);
  333. return cd;
  334. }
  335. static void ide_cd_release(struct kref *);
  336. static void ide_cd_put(struct cdrom_info *cd)
  337. {
  338. down(&idecd_ref_sem);
  339. kref_put(&cd->kref, ide_cd_release);
  340. up(&idecd_ref_sem);
  341. }
  342. /****************************************************************************
  343. * Generic packet command support and error handling routines.
  344. */
  345. /* Mark that we've seen a media change, and invalidate our internal
  346. buffers. */
  347. static void cdrom_saw_media_change (ide_drive_t *drive)
  348. {
  349. struct cdrom_info *info = drive->driver_data;
  350. CDROM_STATE_FLAGS (drive)->media_changed = 1;
  351. CDROM_STATE_FLAGS (drive)->toc_valid = 0;
  352. info->nsectors_buffered = 0;
  353. }
  354. static int cdrom_log_sense(ide_drive_t *drive, struct request *rq,
  355. struct request_sense *sense)
  356. {
  357. int log = 0;
  358. if (!sense || !rq || (rq->flags & REQ_QUIET))
  359. return 0;
  360. switch (sense->sense_key) {
  361. case NO_SENSE: case RECOVERED_ERROR:
  362. break;
  363. case NOT_READY:
  364. /*
  365. * don't care about tray state messages for
  366. * e.g. capacity commands or in-progress or
  367. * becoming ready
  368. */
  369. if (sense->asc == 0x3a || sense->asc == 0x04)
  370. break;
  371. log = 1;
  372. break;
  373. case ILLEGAL_REQUEST:
  374. /*
  375. * don't log START_STOP unit with LoEj set, since
  376. * we cannot reliably check if drive can auto-close
  377. */
  378. if (rq->cmd[0] == GPCMD_START_STOP_UNIT && sense->asc == 0x24)
  379. log = 0;
  380. break;
  381. case UNIT_ATTENTION:
  382. /*
  383. * Make good and sure we've seen this potential media
  384. * change. Some drives (i.e. Creative) fail to present
  385. * the correct sense key in the error register.
  386. */
  387. cdrom_saw_media_change(drive);
  388. break;
  389. default:
  390. log = 1;
  391. break;
  392. }
  393. return log;
  394. }
  395. static
  396. void cdrom_analyze_sense_data(ide_drive_t *drive,
  397. struct request *failed_command,
  398. struct request_sense *sense)
  399. {
  400. if (!cdrom_log_sense(drive, failed_command, sense))
  401. return;
  402. /*
  403. * If a read toc is executed for a CD-R or CD-RW medium where
  404. * the first toc has not been recorded yet, it will fail with
  405. * 05/24/00 (which is a confusing error)
  406. */
  407. if (failed_command && failed_command->cmd[0] == GPCMD_READ_TOC_PMA_ATIP)
  408. if (sense->sense_key == 0x05 && sense->asc == 0x24)
  409. return;
  410. #if VERBOSE_IDE_CD_ERRORS
  411. {
  412. int i;
  413. const char *s;
  414. char buf[80];
  415. printk ("ATAPI device %s:\n", drive->name);
  416. if (sense->error_code==0x70)
  417. printk(" Error: ");
  418. else if (sense->error_code==0x71)
  419. printk(" Deferred Error: ");
  420. else if (sense->error_code == 0x7f)
  421. printk(" Vendor-specific Error: ");
  422. else
  423. printk(" Unknown Error Type: ");
  424. if (sense->sense_key < ARY_LEN(sense_key_texts))
  425. s = sense_key_texts[sense->sense_key];
  426. else
  427. s = "bad sense key!";
  428. printk("%s -- (Sense key=0x%02x)\n", s, sense->sense_key);
  429. if (sense->asc == 0x40) {
  430. sprintf(buf, "Diagnostic failure on component 0x%02x",
  431. sense->ascq);
  432. s = buf;
  433. } else {
  434. int lo = 0, mid, hi = ARY_LEN(sense_data_texts);
  435. unsigned long key = (sense->sense_key << 16);
  436. key |= (sense->asc << 8);
  437. if (!(sense->ascq >= 0x80 && sense->ascq <= 0xdd))
  438. key |= sense->ascq;
  439. s = NULL;
  440. while (hi > lo) {
  441. mid = (lo + hi) / 2;
  442. if (sense_data_texts[mid].asc_ascq == key ||
  443. sense_data_texts[mid].asc_ascq == (0xff0000|key)) {
  444. s = sense_data_texts[mid].text;
  445. break;
  446. }
  447. else if (sense_data_texts[mid].asc_ascq > key)
  448. hi = mid;
  449. else
  450. lo = mid+1;
  451. }
  452. }
  453. if (s == NULL) {
  454. if (sense->asc > 0x80)
  455. s = "(vendor-specific error)";
  456. else
  457. s = "(reserved error code)";
  458. }
  459. printk(KERN_ERR " %s -- (asc=0x%02x, ascq=0x%02x)\n",
  460. s, sense->asc, sense->ascq);
  461. if (failed_command != NULL) {
  462. int lo=0, mid, hi= ARY_LEN (packet_command_texts);
  463. s = NULL;
  464. while (hi > lo) {
  465. mid = (lo + hi) / 2;
  466. if (packet_command_texts[mid].packet_command ==
  467. failed_command->cmd[0]) {
  468. s = packet_command_texts[mid].text;
  469. break;
  470. }
  471. if (packet_command_texts[mid].packet_command >
  472. failed_command->cmd[0])
  473. hi = mid;
  474. else
  475. lo = mid+1;
  476. }
  477. printk (KERN_ERR " The failed \"%s\" packet command was: \n \"", s);
  478. for (i=0; i<sizeof (failed_command->cmd); i++)
  479. printk ("%02x ", failed_command->cmd[i]);
  480. printk ("\"\n");
  481. }
  482. /* The SKSV bit specifies validity of the sense_key_specific
  483. * in the next two commands. It is bit 7 of the first byte.
  484. * In the case of NOT_READY, if SKSV is set the drive can
  485. * give us nice ETA readings.
  486. */
  487. if (sense->sense_key == NOT_READY && (sense->sks[0] & 0x80)) {
  488. int progress = (sense->sks[1] << 8 | sense->sks[2]) * 100;
  489. printk(KERN_ERR " Command is %02d%% complete\n", progress / 0xffff);
  490. }
  491. if (sense->sense_key == ILLEGAL_REQUEST &&
  492. (sense->sks[0] & 0x80) != 0) {
  493. printk(KERN_ERR " Error in %s byte %d",
  494. (sense->sks[0] & 0x40) != 0 ?
  495. "command packet" : "command data",
  496. (sense->sks[1] << 8) + sense->sks[2]);
  497. if ((sense->sks[0] & 0x40) != 0)
  498. printk (" bit %d", sense->sks[0] & 0x07);
  499. printk ("\n");
  500. }
  501. }
  502. #else /* not VERBOSE_IDE_CD_ERRORS */
  503. /* Suppress printing unit attention and `in progress of becoming ready'
  504. errors when we're not being verbose. */
  505. if (sense->sense_key == UNIT_ATTENTION ||
  506. (sense->sense_key == NOT_READY && (sense->asc == 4 ||
  507. sense->asc == 0x3a)))
  508. return;
  509. printk(KERN_ERR "%s: error code: 0x%02x sense_key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
  510. drive->name,
  511. sense->error_code, sense->sense_key,
  512. sense->asc, sense->ascq);
  513. #endif /* not VERBOSE_IDE_CD_ERRORS */
  514. }
  515. /*
  516. * Initialize a ide-cd packet command request
  517. */
  518. static void cdrom_prepare_request(ide_drive_t *drive, struct request *rq)
  519. {
  520. struct cdrom_info *cd = drive->driver_data;
  521. ide_init_drive_cmd(rq);
  522. rq->flags = REQ_PC;
  523. rq->rq_disk = cd->disk;
  524. }
  525. static void cdrom_queue_request_sense(ide_drive_t *drive, void *sense,
  526. struct request *failed_command)
  527. {
  528. struct cdrom_info *info = drive->driver_data;
  529. struct request *rq = &info->request_sense_request;
  530. if (sense == NULL)
  531. sense = &info->sense_data;
  532. /* stuff the sense request in front of our current request */
  533. cdrom_prepare_request(drive, rq);
  534. rq->data = sense;
  535. rq->cmd[0] = GPCMD_REQUEST_SENSE;
  536. rq->cmd[4] = rq->data_len = 18;
  537. rq->flags = REQ_SENSE;
  538. /* NOTE! Save the failed command in "rq->buffer" */
  539. rq->buffer = (void *) failed_command;
  540. (void) ide_do_drive_cmd(drive, rq, ide_preempt);
  541. }
  542. static void cdrom_end_request (ide_drive_t *drive, int uptodate)
  543. {
  544. struct request *rq = HWGROUP(drive)->rq;
  545. int nsectors = rq->hard_cur_sectors;
  546. if ((rq->flags & REQ_SENSE) && uptodate) {
  547. /*
  548. * For REQ_SENSE, "rq->buffer" points to the original failed
  549. * request
  550. */
  551. struct request *failed = (struct request *) rq->buffer;
  552. struct cdrom_info *info = drive->driver_data;
  553. void *sense = &info->sense_data;
  554. unsigned long flags;
  555. if (failed) {
  556. if (failed->sense) {
  557. sense = failed->sense;
  558. failed->sense_len = rq->sense_len;
  559. }
  560. /*
  561. * now end failed request
  562. */
  563. spin_lock_irqsave(&ide_lock, flags);
  564. end_that_request_chunk(failed, 0, failed->data_len);
  565. end_that_request_last(failed);
  566. spin_unlock_irqrestore(&ide_lock, flags);
  567. }
  568. cdrom_analyze_sense_data(drive, failed, sense);
  569. }
  570. if (!rq->current_nr_sectors && blk_fs_request(rq))
  571. uptodate = 1;
  572. /* make sure it's fully ended */
  573. if (blk_pc_request(rq))
  574. nsectors = (rq->data_len + 511) >> 9;
  575. if (!nsectors)
  576. nsectors = 1;
  577. ide_end_request(drive, uptodate, nsectors);
  578. }
  579. /* Returns 0 if the request should be continued.
  580. Returns 1 if the request was ended. */
  581. static int cdrom_decode_status(ide_drive_t *drive, int good_stat, int *stat_ret)
  582. {
  583. struct request *rq = HWGROUP(drive)->rq;
  584. int stat, err, sense_key;
  585. /* Check for errors. */
  586. stat = HWIF(drive)->INB(IDE_STATUS_REG);
  587. if (stat_ret)
  588. *stat_ret = stat;
  589. if (OK_STAT(stat, good_stat, BAD_R_STAT))
  590. return 0;
  591. /* Get the IDE error register. */
  592. err = HWIF(drive)->INB(IDE_ERROR_REG);
  593. sense_key = err >> 4;
  594. if (rq == NULL) {
  595. printk("%s: missing rq in cdrom_decode_status\n", drive->name);
  596. return 1;
  597. }
  598. if (rq->flags & REQ_SENSE) {
  599. /* We got an error trying to get sense info
  600. from the drive (probably while trying
  601. to recover from a former error). Just give up. */
  602. rq->flags |= REQ_FAILED;
  603. cdrom_end_request(drive, 0);
  604. ide_error(drive, "request sense failure", stat);
  605. return 1;
  606. } else if (rq->flags & (REQ_PC | REQ_BLOCK_PC)) {
  607. /* All other functions, except for READ. */
  608. unsigned long flags;
  609. /*
  610. * if we have an error, pass back CHECK_CONDITION as the
  611. * scsi status byte
  612. */
  613. if ((rq->flags & REQ_BLOCK_PC) && !rq->errors)
  614. rq->errors = SAM_STAT_CHECK_CONDITION;
  615. /* Check for tray open. */
  616. if (sense_key == NOT_READY) {
  617. cdrom_saw_media_change (drive);
  618. } else if (sense_key == UNIT_ATTENTION) {
  619. /* Check for media change. */
  620. cdrom_saw_media_change (drive);
  621. /*printk("%s: media changed\n",drive->name);*/
  622. return 0;
  623. } else if (!(rq->flags & REQ_QUIET)) {
  624. /* Otherwise, print an error. */
  625. ide_dump_status(drive, "packet command error", stat);
  626. }
  627. rq->flags |= REQ_FAILED;
  628. /*
  629. * instead of playing games with moving completions around,
  630. * remove failed request completely and end it when the
  631. * request sense has completed
  632. */
  633. if (stat & ERR_STAT) {
  634. spin_lock_irqsave(&ide_lock, flags);
  635. blkdev_dequeue_request(rq);
  636. HWGROUP(drive)->rq = NULL;
  637. spin_unlock_irqrestore(&ide_lock, flags);
  638. cdrom_queue_request_sense(drive, rq->sense, rq);
  639. } else
  640. cdrom_end_request(drive, 0);
  641. } else if (blk_fs_request(rq)) {
  642. int do_end_request = 0;
  643. /* Handle errors from READ and WRITE requests. */
  644. if (blk_noretry_request(rq))
  645. do_end_request = 1;
  646. if (sense_key == NOT_READY) {
  647. /* Tray open. */
  648. if (rq_data_dir(rq) == READ) {
  649. cdrom_saw_media_change (drive);
  650. /* Fail the request. */
  651. printk ("%s: tray open\n", drive->name);
  652. do_end_request = 1;
  653. } else {
  654. struct cdrom_info *info = drive->driver_data;
  655. /* allow the drive 5 seconds to recover, some
  656. * devices will return this error while flushing
  657. * data from cache */
  658. if (!rq->errors)
  659. info->write_timeout = jiffies + ATAPI_WAIT_WRITE_BUSY;
  660. rq->errors = 1;
  661. if (time_after(jiffies, info->write_timeout))
  662. do_end_request = 1;
  663. else {
  664. unsigned long flags;
  665. /*
  666. * take a breather relying on the
  667. * unplug timer to kick us again
  668. */
  669. spin_lock_irqsave(&ide_lock, flags);
  670. blk_plug_device(drive->queue);
  671. spin_unlock_irqrestore(&ide_lock,flags);
  672. return 1;
  673. }
  674. }
  675. } else if (sense_key == UNIT_ATTENTION) {
  676. /* Media change. */
  677. cdrom_saw_media_change (drive);
  678. /* Arrange to retry the request.
  679. But be sure to give up if we've retried
  680. too many times. */
  681. if (++rq->errors > ERROR_MAX)
  682. do_end_request = 1;
  683. } else if (sense_key == ILLEGAL_REQUEST ||
  684. sense_key == DATA_PROTECT) {
  685. /* No point in retrying after an illegal
  686. request or data protect error.*/
  687. ide_dump_status (drive, "command error", stat);
  688. do_end_request = 1;
  689. } else if (sense_key == MEDIUM_ERROR) {
  690. /* No point in re-trying a zillion times on a bad
  691. * sector... If we got here the error is not correctable */
  692. ide_dump_status (drive, "media error (bad sector)", stat);
  693. do_end_request = 1;
  694. } else if (sense_key == BLANK_CHECK) {
  695. /* Disk appears blank ?? */
  696. ide_dump_status (drive, "media error (blank)", stat);
  697. do_end_request = 1;
  698. } else if ((err & ~ABRT_ERR) != 0) {
  699. /* Go to the default handler
  700. for other errors. */
  701. ide_error(drive, "cdrom_decode_status", stat);
  702. return 1;
  703. } else if ((++rq->errors > ERROR_MAX)) {
  704. /* We've racked up too many retries. Abort. */
  705. do_end_request = 1;
  706. }
  707. if (do_end_request)
  708. cdrom_end_request(drive, 0);
  709. /* If we got a CHECK_CONDITION status,
  710. queue a request sense command. */
  711. if ((stat & ERR_STAT) != 0)
  712. cdrom_queue_request_sense(drive, NULL, NULL);
  713. } else {
  714. blk_dump_rq_flags(rq, "ide-cd: bad rq");
  715. cdrom_end_request(drive, 0);
  716. }
  717. /* Retry, or handle the next request. */
  718. return 1;
  719. }
  720. static int cdrom_timer_expiry(ide_drive_t *drive)
  721. {
  722. struct request *rq = HWGROUP(drive)->rq;
  723. unsigned long wait = 0;
  724. /*
  725. * Some commands are *slow* and normally take a long time to
  726. * complete. Usually we can use the ATAPI "disconnect" to bypass
  727. * this, but not all commands/drives support that. Let
  728. * ide_timer_expiry keep polling us for these.
  729. */
  730. switch (rq->cmd[0]) {
  731. case GPCMD_BLANK:
  732. case GPCMD_FORMAT_UNIT:
  733. case GPCMD_RESERVE_RZONE_TRACK:
  734. case GPCMD_CLOSE_TRACK:
  735. case GPCMD_FLUSH_CACHE:
  736. wait = ATAPI_WAIT_PC;
  737. break;
  738. default:
  739. if (!(rq->flags & REQ_QUIET))
  740. printk(KERN_INFO "ide-cd: cmd 0x%x timed out\n", rq->cmd[0]);
  741. wait = 0;
  742. break;
  743. }
  744. return wait;
  745. }
  746. /* Set up the device registers for transferring a packet command on DEV,
  747. expecting to later transfer XFERLEN bytes. HANDLER is the routine
  748. which actually transfers the command to the drive. If this is a
  749. drq_interrupt device, this routine will arrange for HANDLER to be
  750. called when the interrupt from the drive arrives. Otherwise, HANDLER
  751. will be called immediately after the drive is prepared for the transfer. */
  752. static ide_startstop_t cdrom_start_packet_command(ide_drive_t *drive,
  753. int xferlen,
  754. ide_handler_t *handler)
  755. {
  756. ide_startstop_t startstop;
  757. struct cdrom_info *info = drive->driver_data;
  758. ide_hwif_t *hwif = drive->hwif;
  759. /* Wait for the controller to be idle. */
  760. if (ide_wait_stat(&startstop, drive, 0, BUSY_STAT, WAIT_READY))
  761. return startstop;
  762. if (info->dma)
  763. info->dma = !hwif->dma_setup(drive);
  764. /* Set up the controller registers. */
  765. /* FIXME: for Virtual DMA we must check harder */
  766. HWIF(drive)->OUTB(info->dma, IDE_FEATURE_REG);
  767. HWIF(drive)->OUTB(0, IDE_IREASON_REG);
  768. HWIF(drive)->OUTB(0, IDE_SECTOR_REG);
  769. HWIF(drive)->OUTB(xferlen & 0xff, IDE_BCOUNTL_REG);
  770. HWIF(drive)->OUTB(xferlen >> 8 , IDE_BCOUNTH_REG);
  771. if (IDE_CONTROL_REG)
  772. HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
  773. if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
  774. /* packet command */
  775. ide_execute_command(drive, WIN_PACKETCMD, handler, ATAPI_WAIT_PC, cdrom_timer_expiry);
  776. return ide_started;
  777. } else {
  778. unsigned long flags;
  779. /* packet command */
  780. spin_lock_irqsave(&ide_lock, flags);
  781. hwif->OUTBSYNC(drive, WIN_PACKETCMD, IDE_COMMAND_REG);
  782. ndelay(400);
  783. spin_unlock_irqrestore(&ide_lock, flags);
  784. return (*handler) (drive);
  785. }
  786. }
  787. /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
  788. The device registers must have already been prepared
  789. by cdrom_start_packet_command.
  790. HANDLER is the interrupt handler to call when the command completes
  791. or there's data ready. */
  792. /*
  793. * changed 5 parameters to 3 for dvd-ram
  794. * struct packet_command *pc; now packet_command_t *pc;
  795. */
  796. #define ATAPI_MIN_CDB_BYTES 12
  797. static ide_startstop_t cdrom_transfer_packet_command (ide_drive_t *drive,
  798. struct request *rq,
  799. ide_handler_t *handler)
  800. {
  801. ide_hwif_t *hwif = drive->hwif;
  802. int cmd_len;
  803. struct cdrom_info *info = drive->driver_data;
  804. ide_startstop_t startstop;
  805. if (CDROM_CONFIG_FLAGS(drive)->drq_interrupt) {
  806. /* Here we should have been called after receiving an interrupt
  807. from the device. DRQ should how be set. */
  808. /* Check for errors. */
  809. if (cdrom_decode_status(drive, DRQ_STAT, NULL))
  810. return ide_stopped;
  811. } else {
  812. /* Otherwise, we must wait for DRQ to get set. */
  813. if (ide_wait_stat(&startstop, drive, DRQ_STAT,
  814. BUSY_STAT, WAIT_READY))
  815. return startstop;
  816. }
  817. /* Arm the interrupt handler. */
  818. ide_set_handler(drive, handler, rq->timeout, cdrom_timer_expiry);
  819. /* ATAPI commands get padded out to 12 bytes minimum */
  820. cmd_len = COMMAND_SIZE(rq->cmd[0]);
  821. if (cmd_len < ATAPI_MIN_CDB_BYTES)
  822. cmd_len = ATAPI_MIN_CDB_BYTES;
  823. /* Send the command to the device. */
  824. HWIF(drive)->atapi_output_bytes(drive, rq->cmd, cmd_len);
  825. /* Start the DMA if need be */
  826. if (info->dma)
  827. hwif->dma_start(drive);
  828. return ide_started;
  829. }
  830. /****************************************************************************
  831. * Block read functions.
  832. */
  833. /*
  834. * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
  835. * buffer. Once the first sector is added, any subsequent sectors are
  836. * assumed to be continuous (until the buffer is cleared). For the first
  837. * sector added, SECTOR is its sector number. (SECTOR is then ignored until
  838. * the buffer is cleared.)
  839. */
  840. static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
  841. int sectors_to_transfer)
  842. {
  843. struct cdrom_info *info = drive->driver_data;
  844. /* Number of sectors to read into the buffer. */
  845. int sectors_to_buffer = min_t(int, sectors_to_transfer,
  846. (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
  847. info->nsectors_buffered);
  848. char *dest;
  849. /* If we couldn't get a buffer, don't try to buffer anything... */
  850. if (info->buffer == NULL)
  851. sectors_to_buffer = 0;
  852. /* If this is the first sector in the buffer, remember its number. */
  853. if (info->nsectors_buffered == 0)
  854. info->sector_buffered = sector;
  855. /* Read the data into the buffer. */
  856. dest = info->buffer + info->nsectors_buffered * SECTOR_SIZE;
  857. while (sectors_to_buffer > 0) {
  858. HWIF(drive)->atapi_input_bytes(drive, dest, SECTOR_SIZE);
  859. --sectors_to_buffer;
  860. --sectors_to_transfer;
  861. ++info->nsectors_buffered;
  862. dest += SECTOR_SIZE;
  863. }
  864. /* Throw away any remaining data. */
  865. while (sectors_to_transfer > 0) {
  866. static char dum[SECTOR_SIZE];
  867. HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
  868. --sectors_to_transfer;
  869. }
  870. }
  871. /*
  872. * Check the contents of the interrupt reason register from the cdrom
  873. * and attempt to recover if there are problems. Returns 0 if everything's
  874. * ok; nonzero if the request has been terminated.
  875. */
  876. static inline
  877. int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
  878. {
  879. if (ireason == 2)
  880. return 0;
  881. else if (ireason == 0) {
  882. /* Whoops... The drive is expecting to receive data from us! */
  883. printk(KERN_ERR "%s: read_intr: Drive wants to transfer data the "
  884. "wrong way!\n", drive->name);
  885. /* Throw some data at the drive so it doesn't hang
  886. and quit this request. */
  887. while (len > 0) {
  888. int dum = 0;
  889. HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof (dum));
  890. len -= sizeof (dum);
  891. }
  892. } else if (ireason == 1) {
  893. /* Some drives (ASUS) seem to tell us that status
  894. * info is available. just get it and ignore.
  895. */
  896. (void) HWIF(drive)->INB(IDE_STATUS_REG);
  897. return 0;
  898. } else {
  899. /* Drive wants a command packet, or invalid ireason... */
  900. printk(KERN_ERR "%s: read_intr: bad interrupt reason %x\n", drive->name,
  901. ireason);
  902. }
  903. cdrom_end_request(drive, 0);
  904. return -1;
  905. }
  906. /*
  907. * Interrupt routine. Called when a read request has completed.
  908. */
  909. static ide_startstop_t cdrom_read_intr (ide_drive_t *drive)
  910. {
  911. int stat;
  912. int ireason, len, sectors_to_transfer, nskip;
  913. struct cdrom_info *info = drive->driver_data;
  914. u8 lowcyl = 0, highcyl = 0;
  915. int dma = info->dma, dma_error = 0;
  916. struct request *rq = HWGROUP(drive)->rq;
  917. /*
  918. * handle dma case
  919. */
  920. if (dma) {
  921. info->dma = 0;
  922. if ((dma_error = HWIF(drive)->ide_dma_end(drive)))
  923. __ide_dma_off(drive);
  924. }
  925. if (cdrom_decode_status(drive, 0, &stat))
  926. return ide_stopped;
  927. if (dma) {
  928. if (!dma_error) {
  929. ide_end_request(drive, 1, rq->nr_sectors);
  930. return ide_stopped;
  931. } else
  932. return ide_error(drive, "dma error", stat);
  933. }
  934. /* Read the interrupt reason and the transfer length. */
  935. ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
  936. lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  937. highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  938. len = lowcyl + (256 * highcyl);
  939. /* If DRQ is clear, the command has completed. */
  940. if ((stat & DRQ_STAT) == 0) {
  941. /* If we're not done filling the current buffer, complain.
  942. Otherwise, complete the command normally. */
  943. if (rq->current_nr_sectors > 0) {
  944. printk (KERN_ERR "%s: cdrom_read_intr: data underrun (%d blocks)\n",
  945. drive->name, rq->current_nr_sectors);
  946. rq->flags |= REQ_FAILED;
  947. cdrom_end_request(drive, 0);
  948. } else
  949. cdrom_end_request(drive, 1);
  950. return ide_stopped;
  951. }
  952. /* Check that the drive is expecting to do the same thing we are. */
  953. if (cdrom_read_check_ireason (drive, len, ireason))
  954. return ide_stopped;
  955. /* Assume that the drive will always provide data in multiples
  956. of at least SECTOR_SIZE, as it gets hairy to keep track
  957. of the transfers otherwise. */
  958. if ((len % SECTOR_SIZE) != 0) {
  959. printk (KERN_ERR "%s: cdrom_read_intr: Bad transfer size %d\n",
  960. drive->name, len);
  961. if (CDROM_CONFIG_FLAGS(drive)->limit_nframes)
  962. printk (KERN_ERR " This drive is not supported by this version of the driver\n");
  963. else {
  964. printk (KERN_ERR " Trying to limit transfer sizes\n");
  965. CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
  966. }
  967. cdrom_end_request(drive, 0);
  968. return ide_stopped;
  969. }
  970. /* The number of sectors we need to read from the drive. */
  971. sectors_to_transfer = len / SECTOR_SIZE;
  972. /* First, figure out if we need to bit-bucket
  973. any of the leading sectors. */
  974. nskip = min_t(int, rq->current_nr_sectors - bio_cur_sectors(rq->bio), sectors_to_transfer);
  975. while (nskip > 0) {
  976. /* We need to throw away a sector. */
  977. static char dum[SECTOR_SIZE];
  978. HWIF(drive)->atapi_input_bytes(drive, dum, sizeof (dum));
  979. --rq->current_nr_sectors;
  980. --nskip;
  981. --sectors_to_transfer;
  982. }
  983. /* Now loop while we still have data to read from the drive. */
  984. while (sectors_to_transfer > 0) {
  985. int this_transfer;
  986. /* If we've filled the present buffer but there's another
  987. chained buffer after it, move on. */
  988. if (rq->current_nr_sectors == 0 && rq->nr_sectors)
  989. cdrom_end_request(drive, 1);
  990. /* If the buffers are full, cache the rest of the data in our
  991. internal buffer. */
  992. if (rq->current_nr_sectors == 0) {
  993. cdrom_buffer_sectors(drive, rq->sector, sectors_to_transfer);
  994. sectors_to_transfer = 0;
  995. } else {
  996. /* Transfer data to the buffers.
  997. Figure out how many sectors we can transfer
  998. to the current buffer. */
  999. this_transfer = min_t(int, sectors_to_transfer,
  1000. rq->current_nr_sectors);
  1001. /* Read this_transfer sectors
  1002. into the current buffer. */
  1003. while (this_transfer > 0) {
  1004. HWIF(drive)->atapi_input_bytes(drive, rq->buffer, SECTOR_SIZE);
  1005. rq->buffer += SECTOR_SIZE;
  1006. --rq->nr_sectors;
  1007. --rq->current_nr_sectors;
  1008. ++rq->sector;
  1009. --this_transfer;
  1010. --sectors_to_transfer;
  1011. }
  1012. }
  1013. }
  1014. /* Done moving data! Wait for another interrupt. */
  1015. ide_set_handler(drive, &cdrom_read_intr, ATAPI_WAIT_PC, NULL);
  1016. return ide_started;
  1017. }
  1018. /*
  1019. * Try to satisfy some of the current read request from our cached data.
  1020. * Returns nonzero if the request has been completed, zero otherwise.
  1021. */
  1022. static int cdrom_read_from_buffer (ide_drive_t *drive)
  1023. {
  1024. struct cdrom_info *info = drive->driver_data;
  1025. struct request *rq = HWGROUP(drive)->rq;
  1026. unsigned short sectors_per_frame;
  1027. sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
  1028. /* Can't do anything if there's no buffer. */
  1029. if (info->buffer == NULL) return 0;
  1030. /* Loop while this request needs data and the next block is present
  1031. in our cache. */
  1032. while (rq->nr_sectors > 0 &&
  1033. rq->sector >= info->sector_buffered &&
  1034. rq->sector < info->sector_buffered + info->nsectors_buffered) {
  1035. if (rq->current_nr_sectors == 0)
  1036. cdrom_end_request(drive, 1);
  1037. memcpy (rq->buffer,
  1038. info->buffer +
  1039. (rq->sector - info->sector_buffered) * SECTOR_SIZE,
  1040. SECTOR_SIZE);
  1041. rq->buffer += SECTOR_SIZE;
  1042. --rq->current_nr_sectors;
  1043. --rq->nr_sectors;
  1044. ++rq->sector;
  1045. }
  1046. /* If we've satisfied the current request,
  1047. terminate it successfully. */
  1048. if (rq->nr_sectors == 0) {
  1049. cdrom_end_request(drive, 1);
  1050. return -1;
  1051. }
  1052. /* Move on to the next buffer if needed. */
  1053. if (rq->current_nr_sectors == 0)
  1054. cdrom_end_request(drive, 1);
  1055. /* If this condition does not hold, then the kluge i use to
  1056. represent the number of sectors to skip at the start of a transfer
  1057. will fail. I think that this will never happen, but let's be
  1058. paranoid and check. */
  1059. if (rq->current_nr_sectors < bio_cur_sectors(rq->bio) &&
  1060. (rq->sector & (sectors_per_frame - 1))) {
  1061. printk(KERN_ERR "%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
  1062. drive->name, (long)rq->sector);
  1063. cdrom_end_request(drive, 0);
  1064. return -1;
  1065. }
  1066. return 0;
  1067. }
  1068. /*
  1069. * Routine to send a read packet command to the drive.
  1070. * This is usually called directly from cdrom_start_read.
  1071. * However, for drq_interrupt devices, it is called from an interrupt
  1072. * when the drive is ready to accept the command.
  1073. */
  1074. static ide_startstop_t cdrom_start_read_continuation (ide_drive_t *drive)
  1075. {
  1076. struct request *rq = HWGROUP(drive)->rq;
  1077. unsigned short sectors_per_frame;
  1078. int nskip;
  1079. sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
  1080. /* If the requested sector doesn't start on a cdrom block boundary,
  1081. we must adjust the start of the transfer so that it does,
  1082. and remember to skip the first few sectors.
  1083. If the CURRENT_NR_SECTORS field is larger than the size
  1084. of the buffer, it will mean that we're to skip a number
  1085. of sectors equal to the amount by which CURRENT_NR_SECTORS
  1086. is larger than the buffer size. */
  1087. nskip = rq->sector & (sectors_per_frame - 1);
  1088. if (nskip > 0) {
  1089. /* Sanity check... */
  1090. if (rq->current_nr_sectors != bio_cur_sectors(rq->bio) &&
  1091. (rq->sector & (sectors_per_frame - 1))) {
  1092. printk(KERN_ERR "%s: cdrom_start_read_continuation: buffer botch (%u)\n",
  1093. drive->name, rq->current_nr_sectors);
  1094. cdrom_end_request(drive, 0);
  1095. return ide_stopped;
  1096. }
  1097. rq->current_nr_sectors += nskip;
  1098. }
  1099. /* Set up the command */
  1100. rq->timeout = ATAPI_WAIT_PC;
  1101. /* Send the command to the drive and return. */
  1102. return cdrom_transfer_packet_command(drive, rq, &cdrom_read_intr);
  1103. }
  1104. #define IDECD_SEEK_THRESHOLD (1000) /* 1000 blocks */
  1105. #define IDECD_SEEK_TIMER (5 * WAIT_MIN_SLEEP) /* 100 ms */
  1106. #define IDECD_SEEK_TIMEOUT (2 * WAIT_CMD) /* 20 sec */
  1107. static ide_startstop_t cdrom_seek_intr (ide_drive_t *drive)
  1108. {
  1109. struct cdrom_info *info = drive->driver_data;
  1110. int stat;
  1111. static int retry = 10;
  1112. if (cdrom_decode_status(drive, 0, &stat))
  1113. return ide_stopped;
  1114. CDROM_CONFIG_FLAGS(drive)->seeking = 1;
  1115. if (retry && time_after(jiffies, info->start_seek + IDECD_SEEK_TIMER)) {
  1116. if (--retry == 0) {
  1117. /*
  1118. * this condition is far too common, to bother
  1119. * users about it
  1120. */
  1121. /* printk("%s: disabled DSC seek overlap\n", drive->name);*/
  1122. drive->dsc_overlap = 0;
  1123. }
  1124. }
  1125. return ide_stopped;
  1126. }
  1127. static ide_startstop_t cdrom_start_seek_continuation (ide_drive_t *drive)
  1128. {
  1129. struct request *rq = HWGROUP(drive)->rq;
  1130. sector_t frame = rq->sector;
  1131. sector_div(frame, queue_hardsect_size(drive->queue) >> SECTOR_BITS);
  1132. memset(rq->cmd, 0, sizeof(rq->cmd));
  1133. rq->cmd[0] = GPCMD_SEEK;
  1134. put_unaligned(cpu_to_be32(frame), (unsigned int *) &rq->cmd[2]);
  1135. rq->timeout = ATAPI_WAIT_PC;
  1136. return cdrom_transfer_packet_command(drive, rq, &cdrom_seek_intr);
  1137. }
  1138. static ide_startstop_t cdrom_start_seek (ide_drive_t *drive, unsigned int block)
  1139. {
  1140. struct cdrom_info *info = drive->driver_data;
  1141. info->dma = 0;
  1142. info->cmd = 0;
  1143. info->start_seek = jiffies;
  1144. return cdrom_start_packet_command(drive, 0, cdrom_start_seek_continuation);
  1145. }
  1146. /* Fix up a possibly partially-processed request so that we can
  1147. start it over entirely, or even put it back on the request queue. */
  1148. static void restore_request (struct request *rq)
  1149. {
  1150. if (rq->buffer != bio_data(rq->bio)) {
  1151. sector_t n = (rq->buffer - (char *) bio_data(rq->bio)) / SECTOR_SIZE;
  1152. rq->buffer = bio_data(rq->bio);
  1153. rq->nr_sectors += n;
  1154. rq->sector -= n;
  1155. }
  1156. rq->hard_cur_sectors = rq->current_nr_sectors = bio_cur_sectors(rq->bio);
  1157. rq->hard_nr_sectors = rq->nr_sectors;
  1158. rq->hard_sector = rq->sector;
  1159. rq->q->prep_rq_fn(rq->q, rq);
  1160. }
  1161. /*
  1162. * Start a read request from the CD-ROM.
  1163. */
  1164. static ide_startstop_t cdrom_start_read (ide_drive_t *drive, unsigned int block)
  1165. {
  1166. struct cdrom_info *info = drive->driver_data;
  1167. struct request *rq = HWGROUP(drive)->rq;
  1168. unsigned short sectors_per_frame;
  1169. sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
  1170. /* We may be retrying this request after an error. Fix up
  1171. any weirdness which might be present in the request packet. */
  1172. restore_request(rq);
  1173. /* Satisfy whatever we can of this request from our cached sector. */
  1174. if (cdrom_read_from_buffer(drive))
  1175. return ide_stopped;
  1176. blk_attempt_remerge(drive->queue, rq);
  1177. /* Clear the local sector buffer. */
  1178. info->nsectors_buffered = 0;
  1179. /* use dma, if possible. */
  1180. info->dma = drive->using_dma;
  1181. if ((rq->sector & (sectors_per_frame - 1)) ||
  1182. (rq->nr_sectors & (sectors_per_frame - 1)))
  1183. info->dma = 0;
  1184. info->cmd = READ;
  1185. /* Start sending the read request to the drive. */
  1186. return cdrom_start_packet_command(drive, 32768, cdrom_start_read_continuation);
  1187. }
  1188. /****************************************************************************
  1189. * Execute all other packet commands.
  1190. */
  1191. /* Interrupt routine for packet command completion. */
  1192. static ide_startstop_t cdrom_pc_intr (ide_drive_t *drive)
  1193. {
  1194. int ireason, len, thislen;
  1195. struct request *rq = HWGROUP(drive)->rq;
  1196. u8 lowcyl = 0, highcyl = 0;
  1197. int stat;
  1198. /* Check for errors. */
  1199. if (cdrom_decode_status(drive, 0, &stat))
  1200. return ide_stopped;
  1201. /* Read the interrupt reason and the transfer length. */
  1202. ireason = HWIF(drive)->INB(IDE_IREASON_REG);
  1203. lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  1204. highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  1205. len = lowcyl + (256 * highcyl);
  1206. /* If DRQ is clear, the command has completed.
  1207. Complain if we still have data left to transfer. */
  1208. if ((stat & DRQ_STAT) == 0) {
  1209. /* Some of the trailing request sense fields are optional, and
  1210. some drives don't send them. Sigh. */
  1211. if (rq->cmd[0] == GPCMD_REQUEST_SENSE &&
  1212. rq->data_len > 0 &&
  1213. rq->data_len <= 5) {
  1214. while (rq->data_len > 0) {
  1215. *(unsigned char *)rq->data++ = 0;
  1216. --rq->data_len;
  1217. }
  1218. }
  1219. if (rq->data_len == 0)
  1220. cdrom_end_request(drive, 1);
  1221. else {
  1222. /* Comment this out, because this always happens
  1223. right after a reset occurs, and it is annoying to
  1224. always print expected stuff. */
  1225. /*
  1226. printk ("%s: cdrom_pc_intr: data underrun %d\n",
  1227. drive->name, pc->buflen);
  1228. */
  1229. rq->flags |= REQ_FAILED;
  1230. cdrom_end_request(drive, 0);
  1231. }
  1232. return ide_stopped;
  1233. }
  1234. /* Figure out how much data to transfer. */
  1235. thislen = rq->data_len;
  1236. if (thislen > len) thislen = len;
  1237. /* The drive wants to be written to. */
  1238. if ((ireason & 3) == 0) {
  1239. if (!rq->data) {
  1240. blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
  1241. goto confused;
  1242. }
  1243. /* Transfer the data. */
  1244. HWIF(drive)->atapi_output_bytes(drive, rq->data, thislen);
  1245. /* If we haven't moved enough data to satisfy the drive,
  1246. add some padding. */
  1247. while (len > thislen) {
  1248. int dum = 0;
  1249. HWIF(drive)->atapi_output_bytes(drive, &dum, sizeof(dum));
  1250. len -= sizeof(dum);
  1251. }
  1252. /* Keep count of how much data we've moved. */
  1253. rq->data += thislen;
  1254. rq->data_len -= thislen;
  1255. }
  1256. /* Same drill for reading. */
  1257. else if ((ireason & 3) == 2) {
  1258. if (!rq->data) {
  1259. blk_dump_rq_flags(rq, "cdrom_pc_intr, write");
  1260. goto confused;
  1261. }
  1262. /* Transfer the data. */
  1263. HWIF(drive)->atapi_input_bytes(drive, rq->data, thislen);
  1264. /* If we haven't moved enough data to satisfy the drive,
  1265. add some padding. */
  1266. while (len > thislen) {
  1267. int dum = 0;
  1268. HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
  1269. len -= sizeof(dum);
  1270. }
  1271. /* Keep count of how much data we've moved. */
  1272. rq->data += thislen;
  1273. rq->data_len -= thislen;
  1274. if (rq->flags & REQ_SENSE)
  1275. rq->sense_len += thislen;
  1276. } else {
  1277. confused:
  1278. printk (KERN_ERR "%s: cdrom_pc_intr: The drive "
  1279. "appears confused (ireason = 0x%02x)\n",
  1280. drive->name, ireason);
  1281. rq->flags |= REQ_FAILED;
  1282. }
  1283. /* Now we wait for another interrupt. */
  1284. ide_set_handler(drive, &cdrom_pc_intr, ATAPI_WAIT_PC, cdrom_timer_expiry);
  1285. return ide_started;
  1286. }
  1287. static ide_startstop_t cdrom_do_pc_continuation (ide_drive_t *drive)
  1288. {
  1289. struct request *rq = HWGROUP(drive)->rq;
  1290. if (!rq->timeout)
  1291. rq->timeout = ATAPI_WAIT_PC;
  1292. /* Send the command to the drive and return. */
  1293. return cdrom_transfer_packet_command(drive, rq, &cdrom_pc_intr);
  1294. }
  1295. static ide_startstop_t cdrom_do_packet_command (ide_drive_t *drive)
  1296. {
  1297. int len;
  1298. struct request *rq = HWGROUP(drive)->rq;
  1299. struct cdrom_info *info = drive->driver_data;
  1300. info->dma = 0;
  1301. info->cmd = 0;
  1302. rq->flags &= ~REQ_FAILED;
  1303. len = rq->data_len;
  1304. /* Start sending the command to the drive. */
  1305. return cdrom_start_packet_command(drive, len, cdrom_do_pc_continuation);
  1306. }
  1307. static
  1308. int cdrom_queue_packet_command(ide_drive_t *drive, struct request *rq)
  1309. {
  1310. struct request_sense sense;
  1311. int retries = 10;
  1312. unsigned int flags = rq->flags;
  1313. if (rq->sense == NULL)
  1314. rq->sense = &sense;
  1315. /* Start of retry loop. */
  1316. do {
  1317. int error;
  1318. unsigned long time = jiffies;
  1319. rq->flags = flags;
  1320. error = ide_do_drive_cmd(drive, rq, ide_wait);
  1321. time = jiffies - time;
  1322. /* FIXME: we should probably abort/retry or something
  1323. * in case of failure */
  1324. if (rq->flags & REQ_FAILED) {
  1325. /* The request failed. Retry if it was due to a unit
  1326. attention status
  1327. (usually means media was changed). */
  1328. struct request_sense *reqbuf = rq->sense;
  1329. if (reqbuf->sense_key == UNIT_ATTENTION)
  1330. cdrom_saw_media_change(drive);
  1331. else if (reqbuf->sense_key == NOT_READY &&
  1332. reqbuf->asc == 4 && reqbuf->ascq != 4) {
  1333. /* The drive is in the process of loading
  1334. a disk. Retry, but wait a little to give
  1335. the drive time to complete the load. */
  1336. ssleep(2);
  1337. } else {
  1338. /* Otherwise, don't retry. */
  1339. retries = 0;
  1340. }
  1341. --retries;
  1342. }
  1343. /* End of retry loop. */
  1344. } while ((rq->flags & REQ_FAILED) && retries >= 0);
  1345. /* Return an error if the command failed. */
  1346. return (rq->flags & REQ_FAILED) ? -EIO : 0;
  1347. }
  1348. /*
  1349. * Write handling
  1350. */
  1351. static inline int cdrom_write_check_ireason(ide_drive_t *drive, int len, int ireason)
  1352. {
  1353. /* Two notes about IDE interrupt reason here - 0 means that
  1354. * the drive wants to receive data from us, 2 means that
  1355. * the drive is expecting to transfer data to us.
  1356. */
  1357. if (ireason == 0)
  1358. return 0;
  1359. else if (ireason == 2) {
  1360. /* Whoops... The drive wants to send data. */
  1361. printk(KERN_ERR "%s: write_intr: wrong transfer direction!\n",
  1362. drive->name);
  1363. while (len > 0) {
  1364. int dum = 0;
  1365. HWIF(drive)->atapi_input_bytes(drive, &dum, sizeof(dum));
  1366. len -= sizeof(dum);
  1367. }
  1368. } else {
  1369. /* Drive wants a command packet, or invalid ireason... */
  1370. printk(KERN_ERR "%s: write_intr: bad interrupt reason %x\n",
  1371. drive->name, ireason);
  1372. }
  1373. cdrom_end_request(drive, 0);
  1374. return 1;
  1375. }
  1376. static void post_transform_command(struct request *req)
  1377. {
  1378. u8 *c = req->cmd;
  1379. char *ibuf;
  1380. if (!blk_pc_request(req))
  1381. return;
  1382. if (req->bio)
  1383. ibuf = bio_data(req->bio);
  1384. else
  1385. ibuf = req->data;
  1386. if (!ibuf)
  1387. return;
  1388. /*
  1389. * set ansi-revision and response data as atapi
  1390. */
  1391. if (c[0] == GPCMD_INQUIRY) {
  1392. ibuf[2] |= 2;
  1393. ibuf[3] = (ibuf[3] & 0xf0) | 2;
  1394. }
  1395. }
  1396. typedef void (xfer_func_t)(ide_drive_t *, void *, u32);
  1397. /*
  1398. * best way to deal with dma that is not sector aligned right now... note
  1399. * that in this path we are not using ->data or ->buffer at all. this irs
  1400. * can replace cdrom_pc_intr, cdrom_read_intr, and cdrom_write_intr in the
  1401. * future.
  1402. */
  1403. static ide_startstop_t cdrom_newpc_intr(ide_drive_t *drive)
  1404. {
  1405. struct cdrom_info *info = drive->driver_data;
  1406. struct request *rq = HWGROUP(drive)->rq;
  1407. int dma_error, dma, stat, ireason, len, thislen;
  1408. u8 lowcyl, highcyl;
  1409. xfer_func_t *xferfunc;
  1410. unsigned long flags;
  1411. /* Check for errors. */
  1412. dma_error = 0;
  1413. dma = info->dma;
  1414. if (dma) {
  1415. info->dma = 0;
  1416. dma_error = HWIF(drive)->ide_dma_end(drive);
  1417. }
  1418. if (cdrom_decode_status(drive, 0, &stat))
  1419. return ide_stopped;
  1420. /*
  1421. * using dma, transfer is complete now
  1422. */
  1423. if (dma) {
  1424. if (dma_error) {
  1425. printk(KERN_ERR "ide-cd: dma error\n");
  1426. __ide_dma_off(drive);
  1427. return ide_error(drive, "dma error", stat);
  1428. }
  1429. end_that_request_chunk(rq, 1, rq->data_len);
  1430. rq->data_len = 0;
  1431. goto end_request;
  1432. }
  1433. /*
  1434. * ok we fall to pio :/
  1435. */
  1436. ireason = HWIF(drive)->INB(IDE_IREASON_REG) & 0x3;
  1437. lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  1438. highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  1439. len = lowcyl + (256 * highcyl);
  1440. thislen = rq->data_len;
  1441. if (thislen > len)
  1442. thislen = len;
  1443. /*
  1444. * If DRQ is clear, the command has completed.
  1445. */
  1446. if ((stat & DRQ_STAT) == 0)
  1447. goto end_request;
  1448. /*
  1449. * check which way to transfer data
  1450. */
  1451. if (rq_data_dir(rq) == WRITE) {
  1452. /*
  1453. * write to drive
  1454. */
  1455. if (cdrom_write_check_ireason(drive, len, ireason))
  1456. return ide_stopped;
  1457. xferfunc = HWIF(drive)->atapi_output_bytes;
  1458. } else {
  1459. /*
  1460. * read from drive
  1461. */
  1462. if (cdrom_read_check_ireason(drive, len, ireason))
  1463. return ide_stopped;
  1464. xferfunc = HWIF(drive)->atapi_input_bytes;
  1465. }
  1466. /*
  1467. * transfer data
  1468. */
  1469. while (thislen > 0) {
  1470. int blen = blen = rq->data_len;
  1471. char *ptr = rq->data;
  1472. /*
  1473. * bio backed?
  1474. */
  1475. if (rq->bio) {
  1476. ptr = bio_data(rq->bio);
  1477. blen = bio_iovec(rq->bio)->bv_len;
  1478. }
  1479. if (!ptr) {
  1480. printk(KERN_ERR "%s: confused, missing data\n", drive->name);
  1481. break;
  1482. }
  1483. if (blen > thislen)
  1484. blen = thislen;
  1485. xferfunc(drive, ptr, blen);
  1486. thislen -= blen;
  1487. len -= blen;
  1488. rq->data_len -= blen;
  1489. if (rq->bio)
  1490. end_that_request_chunk(rq, 1, blen);
  1491. else
  1492. rq->data += blen;
  1493. }
  1494. /*
  1495. * pad, if necessary
  1496. */
  1497. if (len > 0) {
  1498. while (len > 0) {
  1499. int pad = 0;
  1500. xferfunc(drive, &pad, sizeof(pad));
  1501. len -= sizeof(pad);
  1502. }
  1503. }
  1504. if (HWGROUP(drive)->handler != NULL)
  1505. BUG();
  1506. ide_set_handler(drive, cdrom_newpc_intr, rq->timeout, NULL);
  1507. return ide_started;
  1508. end_request:
  1509. if (!rq->data_len)
  1510. post_transform_command(rq);
  1511. spin_lock_irqsave(&ide_lock, flags);
  1512. blkdev_dequeue_request(rq);
  1513. end_that_request_last(rq);
  1514. HWGROUP(drive)->rq = NULL;
  1515. spin_unlock_irqrestore(&ide_lock, flags);
  1516. return ide_stopped;
  1517. }
  1518. static ide_startstop_t cdrom_write_intr(ide_drive_t *drive)
  1519. {
  1520. int stat, ireason, len, sectors_to_transfer, uptodate;
  1521. struct cdrom_info *info = drive->driver_data;
  1522. int dma_error = 0, dma = info->dma;
  1523. u8 lowcyl = 0, highcyl = 0;
  1524. struct request *rq = HWGROUP(drive)->rq;
  1525. /* Check for errors. */
  1526. if (dma) {
  1527. info->dma = 0;
  1528. if ((dma_error = HWIF(drive)->ide_dma_end(drive))) {
  1529. printk(KERN_ERR "ide-cd: write dma error\n");
  1530. __ide_dma_off(drive);
  1531. }
  1532. }
  1533. if (cdrom_decode_status(drive, 0, &stat))
  1534. return ide_stopped;
  1535. /*
  1536. * using dma, transfer is complete now
  1537. */
  1538. if (dma) {
  1539. if (dma_error)
  1540. return ide_error(drive, "dma error", stat);
  1541. ide_end_request(drive, 1, rq->nr_sectors);
  1542. return ide_stopped;
  1543. }
  1544. /* Read the interrupt reason and the transfer length. */
  1545. ireason = HWIF(drive)->INB(IDE_IREASON_REG);
  1546. lowcyl = HWIF(drive)->INB(IDE_BCOUNTL_REG);
  1547. highcyl = HWIF(drive)->INB(IDE_BCOUNTH_REG);
  1548. len = lowcyl + (256 * highcyl);
  1549. /* If DRQ is clear, the command has completed. */
  1550. if ((stat & DRQ_STAT) == 0) {
  1551. /* If we're not done writing, complain.
  1552. * Otherwise, complete the command normally.
  1553. */
  1554. uptodate = 1;
  1555. if (rq->current_nr_sectors > 0) {
  1556. printk(KERN_ERR "%s: write_intr: data underrun (%d blocks)\n",
  1557. drive->name, rq->current_nr_sectors);
  1558. uptodate = 0;
  1559. }
  1560. cdrom_end_request(drive, uptodate);
  1561. return ide_stopped;
  1562. }
  1563. /* Check that the drive is expecting to do the same thing we are. */
  1564. if (cdrom_write_check_ireason(drive, len, ireason))
  1565. return ide_stopped;
  1566. sectors_to_transfer = len / SECTOR_SIZE;
  1567. /*
  1568. * now loop and write out the data
  1569. */
  1570. while (sectors_to_transfer > 0) {
  1571. int this_transfer;
  1572. if (!rq->current_nr_sectors) {
  1573. printk(KERN_ERR "ide-cd: write_intr: oops\n");
  1574. break;
  1575. }
  1576. /*
  1577. * Figure out how many sectors we can transfer
  1578. */
  1579. this_transfer = min_t(int, sectors_to_transfer, rq->current_nr_sectors);
  1580. while (this_transfer > 0) {
  1581. HWIF(drive)->atapi_output_bytes(drive, rq->buffer, SECTOR_SIZE);
  1582. rq->buffer += SECTOR_SIZE;
  1583. --rq->nr_sectors;
  1584. --rq->current_nr_sectors;
  1585. ++rq->sector;
  1586. --this_transfer;
  1587. --sectors_to_transfer;
  1588. }
  1589. /*
  1590. * current buffer complete, move on
  1591. */
  1592. if (rq->current_nr_sectors == 0 && rq->nr_sectors)
  1593. cdrom_end_request(drive, 1);
  1594. }
  1595. /* re-arm handler */
  1596. ide_set_handler(drive, &cdrom_write_intr, ATAPI_WAIT_PC, NULL);
  1597. return ide_started;
  1598. }
  1599. static ide_startstop_t cdrom_start_write_cont(ide_drive_t *drive)
  1600. {
  1601. struct request *rq = HWGROUP(drive)->rq;
  1602. #if 0 /* the immediate bit */
  1603. rq->cmd[1] = 1 << 3;
  1604. #endif
  1605. rq->timeout = ATAPI_WAIT_PC;
  1606. return cdrom_transfer_packet_command(drive, rq, cdrom_write_intr);
  1607. }
  1608. static ide_startstop_t cdrom_start_write(ide_drive_t *drive, struct request *rq)
  1609. {
  1610. struct cdrom_info *info = drive->driver_data;
  1611. struct gendisk *g = info->disk;
  1612. unsigned short sectors_per_frame = queue_hardsect_size(drive->queue) >> SECTOR_BITS;
  1613. /*
  1614. * writes *must* be hardware frame aligned
  1615. */
  1616. if ((rq->nr_sectors & (sectors_per_frame - 1)) ||
  1617. (rq->sector & (sectors_per_frame - 1))) {
  1618. cdrom_end_request(drive, 0);
  1619. return ide_stopped;
  1620. }
  1621. /*
  1622. * disk has become write protected
  1623. */
  1624. if (g->policy) {
  1625. cdrom_end_request(drive, 0);
  1626. return ide_stopped;
  1627. }
  1628. /*
  1629. * for dvd-ram and such media, it's a really big deal to get
  1630. * big writes all the time. so scour the queue and attempt to
  1631. * remerge requests, often the plugging will not have had time
  1632. * to do this properly
  1633. */
  1634. blk_attempt_remerge(drive->queue, rq);
  1635. info->nsectors_buffered = 0;
  1636. /* use dma, if possible. we don't need to check more, since we
  1637. * know that the transfer is always (at least!) frame aligned */
  1638. info->dma = drive->using_dma ? 1 : 0;
  1639. info->cmd = WRITE;
  1640. info->devinfo.media_written = 1;
  1641. /* Start sending the write request to the drive. */
  1642. return cdrom_start_packet_command(drive, 32768, cdrom_start_write_cont);
  1643. }
  1644. static ide_startstop_t cdrom_do_newpc_cont(ide_drive_t *drive)
  1645. {
  1646. struct request *rq = HWGROUP(drive)->rq;
  1647. if (!rq->timeout)
  1648. rq->timeout = ATAPI_WAIT_PC;
  1649. return cdrom_transfer_packet_command(drive, rq, cdrom_newpc_intr);
  1650. }
  1651. static ide_startstop_t cdrom_do_block_pc(ide_drive_t *drive, struct request *rq)
  1652. {
  1653. struct cdrom_info *info = drive->driver_data;
  1654. rq->flags |= REQ_QUIET;
  1655. info->dma = 0;
  1656. info->cmd = 0;
  1657. /*
  1658. * sg request
  1659. */
  1660. if (rq->bio) {
  1661. int mask = drive->queue->dma_alignment;
  1662. unsigned long addr = (unsigned long) page_address(bio_page(rq->bio));
  1663. info->cmd = rq_data_dir(rq);
  1664. info->dma = drive->using_dma;
  1665. /*
  1666. * check if dma is safe
  1667. */
  1668. if ((rq->data_len & mask) || (addr & mask))
  1669. info->dma = 0;
  1670. }
  1671. /* Start sending the command to the drive. */
  1672. return cdrom_start_packet_command(drive, rq->data_len, cdrom_do_newpc_cont);
  1673. }
  1674. /****************************************************************************
  1675. * cdrom driver request routine.
  1676. */
  1677. static ide_startstop_t
  1678. ide_do_rw_cdrom (ide_drive_t *drive, struct request *rq, sector_t block)
  1679. {
  1680. ide_startstop_t action;
  1681. struct cdrom_info *info = drive->driver_data;
  1682. if (blk_fs_request(rq)) {
  1683. if (CDROM_CONFIG_FLAGS(drive)->seeking) {
  1684. unsigned long elapsed = jiffies - info->start_seek;
  1685. int stat = HWIF(drive)->INB(IDE_STATUS_REG);
  1686. if ((stat & SEEK_STAT) != SEEK_STAT) {
  1687. if (elapsed < IDECD_SEEK_TIMEOUT) {
  1688. ide_stall_queue(drive, IDECD_SEEK_TIMER);
  1689. return ide_stopped;
  1690. }
  1691. printk (KERN_ERR "%s: DSC timeout\n", drive->name);
  1692. }
  1693. CDROM_CONFIG_FLAGS(drive)->seeking = 0;
  1694. }
  1695. if ((rq_data_dir(rq) == READ) && IDE_LARGE_SEEK(info->last_block, block, IDECD_SEEK_THRESHOLD) && drive->dsc_overlap) {
  1696. action = cdrom_start_seek(drive, block);
  1697. } else {
  1698. if (rq_data_dir(rq) == READ)
  1699. action = cdrom_start_read(drive, block);
  1700. else
  1701. action = cdrom_start_write(drive, rq);
  1702. }
  1703. info->last_block = block;
  1704. return action;
  1705. } else if (rq->flags & (REQ_PC | REQ_SENSE)) {
  1706. return cdrom_do_packet_command(drive);
  1707. } else if (rq->flags & REQ_BLOCK_PC) {
  1708. return cdrom_do_block_pc(drive, rq);
  1709. } else if (rq->flags & REQ_SPECIAL) {
  1710. /*
  1711. * right now this can only be a reset...
  1712. */
  1713. cdrom_end_request(drive, 1);
  1714. return ide_stopped;
  1715. }
  1716. blk_dump_rq_flags(rq, "ide-cd bad flags");
  1717. cdrom_end_request(drive, 0);
  1718. return ide_stopped;
  1719. }
  1720. /****************************************************************************
  1721. * Ioctl handling.
  1722. *
  1723. * Routines which queue packet commands take as a final argument a pointer
  1724. * to a request_sense struct. If execution of the command results
  1725. * in an error with a CHECK CONDITION status, this structure will be filled
  1726. * with the results of the subsequent request sense command. The pointer
  1727. * can also be NULL, in which case no sense information is returned.
  1728. */
  1729. #if ! STANDARD_ATAPI
  1730. static inline
  1731. int bin2bcd (int x)
  1732. {
  1733. return (x%10) | ((x/10) << 4);
  1734. }
  1735. static inline
  1736. int bcd2bin (int x)
  1737. {
  1738. return (x >> 4) * 10 + (x & 0x0f);
  1739. }
  1740. static
  1741. void msf_from_bcd (struct atapi_msf *msf)
  1742. {
  1743. msf->minute = bcd2bin (msf->minute);
  1744. msf->second = bcd2bin (msf->second);
  1745. msf->frame = bcd2bin (msf->frame);
  1746. }
  1747. #endif /* not STANDARD_ATAPI */
  1748. static inline
  1749. void lba_to_msf (int lba, byte *m, byte *s, byte *f)
  1750. {
  1751. lba += CD_MSF_OFFSET;
  1752. lba &= 0xffffff; /* negative lbas use only 24 bits */
  1753. *m = lba / (CD_SECS * CD_FRAMES);
  1754. lba %= (CD_SECS * CD_FRAMES);
  1755. *s = lba / CD_FRAMES;
  1756. *f = lba % CD_FRAMES;
  1757. }
  1758. static inline
  1759. int msf_to_lba (byte m, byte s, byte f)
  1760. {
  1761. return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_MSF_OFFSET;
  1762. }
  1763. static int cdrom_check_status(ide_drive_t *drive, struct request_sense *sense)
  1764. {
  1765. struct request req;
  1766. struct cdrom_info *info = drive->driver_data;
  1767. struct cdrom_device_info *cdi = &info->devinfo;
  1768. cdrom_prepare_request(drive, &req);
  1769. req.sense = sense;
  1770. req.cmd[0] = GPCMD_TEST_UNIT_READY;
  1771. req.flags |= REQ_QUIET;
  1772. #if ! STANDARD_ATAPI
  1773. /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
  1774. switch CDs instead of supporting the LOAD_UNLOAD opcode */
  1775. req.cmd[7] = cdi->sanyo_slot % 3;
  1776. #endif /* not STANDARD_ATAPI */
  1777. return cdrom_queue_packet_command(drive, &req);
  1778. }
  1779. /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
  1780. static int
  1781. cdrom_lockdoor(ide_drive_t *drive, int lockflag, struct request_sense *sense)
  1782. {
  1783. struct request_sense my_sense;
  1784. struct request req;
  1785. int stat;
  1786. if (sense == NULL)
  1787. sense = &my_sense;
  1788. /* If the drive cannot lock the door, just pretend. */
  1789. if (CDROM_CONFIG_FLAGS(drive)->no_doorlock) {
  1790. stat = 0;
  1791. } else {
  1792. cdrom_prepare_request(drive, &req);
  1793. req.sense = sense;
  1794. req.cmd[0] = GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL;
  1795. req.cmd[4] = lockflag ? 1 : 0;
  1796. stat = cdrom_queue_packet_command(drive, &req);
  1797. }
  1798. /* If we got an illegal field error, the drive
  1799. probably cannot lock the door. */
  1800. if (stat != 0 &&
  1801. sense->sense_key == ILLEGAL_REQUEST &&
  1802. (sense->asc == 0x24 || sense->asc == 0x20)) {
  1803. printk (KERN_ERR "%s: door locking not supported\n",
  1804. drive->name);
  1805. CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
  1806. stat = 0;
  1807. }
  1808. /* no medium, that's alright. */
  1809. if (stat != 0 && sense->sense_key == NOT_READY && sense->asc == 0x3a)
  1810. stat = 0;
  1811. if (stat == 0)
  1812. CDROM_STATE_FLAGS(drive)->door_locked = lockflag;
  1813. return stat;
  1814. }
  1815. /* Eject the disk if EJECTFLAG is 0.
  1816. If EJECTFLAG is 1, try to reload the disk. */
  1817. static int cdrom_eject(ide_drive_t *drive, int ejectflag,
  1818. struct request_sense *sense)
  1819. {
  1820. struct request req;
  1821. char loej = 0x02;
  1822. if (CDROM_CONFIG_FLAGS(drive)->no_eject && !ejectflag)
  1823. return -EDRIVE_CANT_DO_THIS;
  1824. /* reload fails on some drives, if the tray is locked */
  1825. if (CDROM_STATE_FLAGS(drive)->door_locked && ejectflag)
  1826. return 0;
  1827. cdrom_prepare_request(drive, &req);
  1828. /* only tell drive to close tray if open, if it can do that */
  1829. if (ejectflag && !CDROM_CONFIG_FLAGS(drive)->close_tray)
  1830. loej = 0;
  1831. req.sense = sense;
  1832. req.cmd[0] = GPCMD_START_STOP_UNIT;
  1833. req.cmd[4] = loej | (ejectflag != 0);
  1834. return cdrom_queue_packet_command(drive, &req);
  1835. }
  1836. static int cdrom_read_capacity(ide_drive_t *drive, unsigned long *capacity,
  1837. unsigned long *sectors_per_frame,
  1838. struct request_sense *sense)
  1839. {
  1840. struct {
  1841. __u32 lba;
  1842. __u32 blocklen;
  1843. } capbuf;
  1844. int stat;
  1845. struct request req;
  1846. cdrom_prepare_request(drive, &req);
  1847. req.sense = sense;
  1848. req.cmd[0] = GPCMD_READ_CDVD_CAPACITY;
  1849. req.data = (char *)&capbuf;
  1850. req.data_len = sizeof(capbuf);
  1851. stat = cdrom_queue_packet_command(drive, &req);
  1852. if (stat == 0) {
  1853. *capacity = 1 + be32_to_cpu(capbuf.lba);
  1854. *sectors_per_frame =
  1855. be32_to_cpu(capbuf.blocklen) >> SECTOR_BITS;
  1856. }
  1857. return stat;
  1858. }
  1859. static int cdrom_read_tocentry(ide_drive_t *drive, int trackno, int msf_flag,
  1860. int format, char *buf, int buflen,
  1861. struct request_sense *sense)
  1862. {
  1863. struct request req;
  1864. cdrom_prepare_request(drive, &req);
  1865. req.sense = sense;
  1866. req.data = buf;
  1867. req.data_len = buflen;
  1868. req.flags |= REQ_QUIET;
  1869. req.cmd[0] = GPCMD_READ_TOC_PMA_ATIP;
  1870. req.cmd[6] = trackno;
  1871. req.cmd[7] = (buflen >> 8);
  1872. req.cmd[8] = (buflen & 0xff);
  1873. req.cmd[9] = (format << 6);
  1874. if (msf_flag)
  1875. req.cmd[1] = 2;
  1876. return cdrom_queue_packet_command(drive, &req);
  1877. }
  1878. /* Try to read the entire TOC for the disk into our internal buffer. */
  1879. static int cdrom_read_toc(ide_drive_t *drive, struct request_sense *sense)
  1880. {
  1881. int stat, ntracks, i;
  1882. struct cdrom_info *info = drive->driver_data;
  1883. struct cdrom_device_info *cdi = &info->devinfo;
  1884. struct atapi_toc *toc = info->toc;
  1885. struct {
  1886. struct atapi_toc_header hdr;
  1887. struct atapi_toc_entry ent;
  1888. } ms_tmp;
  1889. long last_written;
  1890. unsigned long sectors_per_frame = SECTORS_PER_FRAME;
  1891. if (toc == NULL) {
  1892. /* Try to allocate space. */
  1893. toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
  1894. GFP_KERNEL);
  1895. info->toc = toc;
  1896. if (toc == NULL) {
  1897. printk (KERN_ERR "%s: No cdrom TOC buffer!\n", drive->name);
  1898. return -ENOMEM;
  1899. }
  1900. }
  1901. /* Check to see if the existing data is still valid.
  1902. If it is, just return. */
  1903. (void) cdrom_check_status(drive, sense);
  1904. if (CDROM_STATE_FLAGS(drive)->toc_valid)
  1905. return 0;
  1906. /* Try to get the total cdrom capacity and sector size. */
  1907. stat = cdrom_read_capacity(drive, &toc->capacity, &sectors_per_frame,
  1908. sense);
  1909. if (stat)
  1910. toc->capacity = 0x1fffff;
  1911. set_capacity(info->disk, toc->capacity * sectors_per_frame);
  1912. blk_queue_hardsect_size(drive->queue,
  1913. sectors_per_frame << SECTOR_BITS);
  1914. /* First read just the header, so we know how long the TOC is. */
  1915. stat = cdrom_read_tocentry(drive, 0, 1, 0, (char *) &toc->hdr,
  1916. sizeof(struct atapi_toc_header), sense);
  1917. if (stat) return stat;
  1918. #if ! STANDARD_ATAPI
  1919. if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
  1920. toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
  1921. toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
  1922. }
  1923. #endif /* not STANDARD_ATAPI */
  1924. ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
  1925. if (ntracks <= 0)
  1926. return -EIO;
  1927. if (ntracks > MAX_TRACKS)
  1928. ntracks = MAX_TRACKS;
  1929. /* Now read the whole schmeer. */
  1930. stat = cdrom_read_tocentry(drive, toc->hdr.first_track, 1, 0,
  1931. (char *)&toc->hdr,
  1932. sizeof(struct atapi_toc_header) +
  1933. (ntracks + 1) *
  1934. sizeof(struct atapi_toc_entry), sense);
  1935. if (stat && toc->hdr.first_track > 1) {
  1936. /* Cds with CDI tracks only don't have any TOC entries,
  1937. despite of this the returned values are
  1938. first_track == last_track = number of CDI tracks + 1,
  1939. so that this case is indistinguishable from the same
  1940. layout plus an additional audio track.
  1941. If we get an error for the regular case, we assume
  1942. a CDI without additional audio tracks. In this case
  1943. the readable TOC is empty (CDI tracks are not included)
  1944. and only holds the Leadout entry. Heiko Eißfeldt */
  1945. ntracks = 0;
  1946. stat = cdrom_read_tocentry(drive, CDROM_LEADOUT, 1, 0,
  1947. (char *)&toc->hdr,
  1948. sizeof(struct atapi_toc_header) +
  1949. (ntracks + 1) *
  1950. sizeof(struct atapi_toc_entry),
  1951. sense);
  1952. if (stat) {
  1953. return stat;
  1954. }
  1955. #if ! STANDARD_ATAPI
  1956. if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
  1957. toc->hdr.first_track = bin2bcd(CDROM_LEADOUT);
  1958. toc->hdr.last_track = bin2bcd(CDROM_LEADOUT);
  1959. } else
  1960. #endif /* not STANDARD_ATAPI */
  1961. {
  1962. toc->hdr.first_track = CDROM_LEADOUT;
  1963. toc->hdr.last_track = CDROM_LEADOUT;
  1964. }
  1965. }
  1966. if (stat)
  1967. return stat;
  1968. toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
  1969. #if ! STANDARD_ATAPI
  1970. if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd) {
  1971. toc->hdr.first_track = bcd2bin(toc->hdr.first_track);
  1972. toc->hdr.last_track = bcd2bin(toc->hdr.last_track);
  1973. }
  1974. #endif /* not STANDARD_ATAPI */
  1975. for (i=0; i<=ntracks; i++) {
  1976. #if ! STANDARD_ATAPI
  1977. if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
  1978. if (CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd)
  1979. toc->ent[i].track = bcd2bin(toc->ent[i].track);
  1980. msf_from_bcd(&toc->ent[i].addr.msf);
  1981. }
  1982. #endif /* not STANDARD_ATAPI */
  1983. toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
  1984. toc->ent[i].addr.msf.second,
  1985. toc->ent[i].addr.msf.frame);
  1986. }
  1987. /* Read the multisession information. */
  1988. if (toc->hdr.first_track != CDROM_LEADOUT) {
  1989. /* Read the multisession information. */
  1990. stat = cdrom_read_tocentry(drive, 0, 0, 1, (char *)&ms_tmp,
  1991. sizeof(ms_tmp), sense);
  1992. if (stat) return stat;
  1993. toc->last_session_lba = be32_to_cpu(ms_tmp.ent.addr.lba);
  1994. } else {
  1995. ms_tmp.hdr.first_track = ms_tmp.hdr.last_track = CDROM_LEADOUT;
  1996. toc->last_session_lba = msf_to_lba(0, 2, 0); /* 0m 2s 0f */
  1997. }
  1998. #if ! STANDARD_ATAPI
  1999. if (CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd) {
  2000. /* Re-read multisession information using MSF format */
  2001. stat = cdrom_read_tocentry(drive, 0, 1, 1, (char *)&ms_tmp,
  2002. sizeof(ms_tmp), sense);
  2003. if (stat)
  2004. return stat;
  2005. msf_from_bcd (&ms_tmp.ent.addr.msf);
  2006. toc->last_session_lba = msf_to_lba(ms_tmp.ent.addr.msf.minute,
  2007. ms_tmp.ent.addr.msf.second,
  2008. ms_tmp.ent.addr.msf.frame);
  2009. }
  2010. #endif /* not STANDARD_ATAPI */
  2011. toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
  2012. /* Now try to get the total cdrom capacity. */
  2013. stat = cdrom_get_last_written(cdi, &last_written);
  2014. if (!stat && (last_written > toc->capacity)) {
  2015. toc->capacity = last_written;
  2016. set_capacity(info->disk, toc->capacity * sectors_per_frame);
  2017. }
  2018. /* Remember that we've read this stuff. */
  2019. CDROM_STATE_FLAGS(drive)->toc_valid = 1;
  2020. return 0;
  2021. }
  2022. static int cdrom_read_subchannel(ide_drive_t *drive, int format, char *buf,
  2023. int buflen, struct request_sense *sense)
  2024. {
  2025. struct request req;
  2026. cdrom_prepare_request(drive, &req);
  2027. req.sense = sense;
  2028. req.data = buf;
  2029. req.data_len = buflen;
  2030. req.cmd[0] = GPCMD_READ_SUBCHANNEL;
  2031. req.cmd[1] = 2; /* MSF addressing */
  2032. req.cmd[2] = 0x40; /* request subQ data */
  2033. req.cmd[3] = format;
  2034. req.cmd[7] = (buflen >> 8);
  2035. req.cmd[8] = (buflen & 0xff);
  2036. return cdrom_queue_packet_command(drive, &req);
  2037. }
  2038. /* ATAPI cdrom drives are free to select the speed you request or any slower
  2039. rate :-( Requesting too fast a speed will _not_ produce an error. */
  2040. static int cdrom_select_speed(ide_drive_t *drive, int speed,
  2041. struct request_sense *sense)
  2042. {
  2043. struct request req;
  2044. cdrom_prepare_request(drive, &req);
  2045. req.sense = sense;
  2046. if (speed == 0)
  2047. speed = 0xffff; /* set to max */
  2048. else
  2049. speed *= 177; /* Nx to kbytes/s */
  2050. req.cmd[0] = GPCMD_SET_SPEED;
  2051. /* Read Drive speed in kbytes/second MSB */
  2052. req.cmd[2] = (speed >> 8) & 0xff;
  2053. /* Read Drive speed in kbytes/second LSB */
  2054. req.cmd[3] = speed & 0xff;
  2055. if (CDROM_CONFIG_FLAGS(drive)->cd_r ||
  2056. CDROM_CONFIG_FLAGS(drive)->cd_rw ||
  2057. CDROM_CONFIG_FLAGS(drive)->dvd_r) {
  2058. /* Write Drive speed in kbytes/second MSB */
  2059. req.cmd[4] = (speed >> 8) & 0xff;
  2060. /* Write Drive speed in kbytes/second LSB */
  2061. req.cmd[5] = speed & 0xff;
  2062. }
  2063. return cdrom_queue_packet_command(drive, &req);
  2064. }
  2065. static int cdrom_play_audio(ide_drive_t *drive, int lba_start, int lba_end)
  2066. {
  2067. struct request_sense sense;
  2068. struct request req;
  2069. cdrom_prepare_request(drive, &req);
  2070. req.sense = &sense;
  2071. req.cmd[0] = GPCMD_PLAY_AUDIO_MSF;
  2072. lba_to_msf(lba_start, &req.cmd[3], &req.cmd[4], &req.cmd[5]);
  2073. lba_to_msf(lba_end-1, &req.cmd[6], &req.cmd[7], &req.cmd[8]);
  2074. return cdrom_queue_packet_command(drive, &req);
  2075. }
  2076. static int cdrom_get_toc_entry(ide_drive_t *drive, int track,
  2077. struct atapi_toc_entry **ent)
  2078. {
  2079. struct cdrom_info *info = drive->driver_data;
  2080. struct atapi_toc *toc = info->toc;
  2081. int ntracks;
  2082. /*
  2083. * don't serve cached data, if the toc isn't valid
  2084. */
  2085. if (!CDROM_STATE_FLAGS(drive)->toc_valid)
  2086. return -EINVAL;
  2087. /* Check validity of requested track number. */
  2088. ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
  2089. if (toc->hdr.first_track == CDROM_LEADOUT) ntracks = 0;
  2090. if (track == CDROM_LEADOUT)
  2091. *ent = &toc->ent[ntracks];
  2092. else if (track < toc->hdr.first_track ||
  2093. track > toc->hdr.last_track)
  2094. return -EINVAL;
  2095. else
  2096. *ent = &toc->ent[track - toc->hdr.first_track];
  2097. return 0;
  2098. }
  2099. /* the generic packet interface to cdrom.c */
  2100. static int ide_cdrom_packet(struct cdrom_device_info *cdi,
  2101. struct packet_command *cgc)
  2102. {
  2103. struct request req;
  2104. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2105. if (cgc->timeout <= 0)
  2106. cgc->timeout = ATAPI_WAIT_PC;
  2107. /* here we queue the commands from the uniform CD-ROM
  2108. layer. the packet must be complete, as we do not
  2109. touch it at all. */
  2110. cdrom_prepare_request(drive, &req);
  2111. memcpy(req.cmd, cgc->cmd, CDROM_PACKET_SIZE);
  2112. if (cgc->sense)
  2113. memset(cgc->sense, 0, sizeof(struct request_sense));
  2114. req.data = cgc->buffer;
  2115. req.data_len = cgc->buflen;
  2116. req.timeout = cgc->timeout;
  2117. if (cgc->quiet)
  2118. req.flags |= REQ_QUIET;
  2119. req.sense = cgc->sense;
  2120. cgc->stat = cdrom_queue_packet_command(drive, &req);
  2121. if (!cgc->stat)
  2122. cgc->buflen -= req.data_len;
  2123. return cgc->stat;
  2124. }
  2125. static
  2126. int ide_cdrom_dev_ioctl (struct cdrom_device_info *cdi,
  2127. unsigned int cmd, unsigned long arg)
  2128. {
  2129. struct packet_command cgc;
  2130. char buffer[16];
  2131. int stat;
  2132. init_cdrom_command(&cgc, buffer, sizeof(buffer), CGC_DATA_UNKNOWN);
  2133. /* These will be moved into the Uniform layer shortly... */
  2134. switch (cmd) {
  2135. case CDROMSETSPINDOWN: {
  2136. char spindown;
  2137. if (copy_from_user(&spindown, (void __user *) arg, sizeof(char)))
  2138. return -EFAULT;
  2139. if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
  2140. return stat;
  2141. buffer[11] = (buffer[11] & 0xf0) | (spindown & 0x0f);
  2142. return cdrom_mode_select(cdi, &cgc);
  2143. }
  2144. case CDROMGETSPINDOWN: {
  2145. char spindown;
  2146. if ((stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CDROM_PAGE, 0)))
  2147. return stat;
  2148. spindown = buffer[11] & 0x0f;
  2149. if (copy_to_user((void __user *) arg, &spindown, sizeof (char)))
  2150. return -EFAULT;
  2151. return 0;
  2152. }
  2153. default:
  2154. return -EINVAL;
  2155. }
  2156. }
  2157. static
  2158. int ide_cdrom_audio_ioctl (struct cdrom_device_info *cdi,
  2159. unsigned int cmd, void *arg)
  2160. {
  2161. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2162. struct cdrom_info *info = drive->driver_data;
  2163. int stat;
  2164. switch (cmd) {
  2165. /*
  2166. * emulate PLAY_AUDIO_TI command with PLAY_AUDIO_10, since
  2167. * atapi doesn't support it
  2168. */
  2169. case CDROMPLAYTRKIND: {
  2170. unsigned long lba_start, lba_end;
  2171. struct cdrom_ti *ti = (struct cdrom_ti *)arg;
  2172. struct atapi_toc_entry *first_toc, *last_toc;
  2173. stat = cdrom_get_toc_entry(drive, ti->cdti_trk0, &first_toc);
  2174. if (stat)
  2175. return stat;
  2176. stat = cdrom_get_toc_entry(drive, ti->cdti_trk1, &last_toc);
  2177. if (stat)
  2178. return stat;
  2179. if (ti->cdti_trk1 != CDROM_LEADOUT)
  2180. ++last_toc;
  2181. lba_start = first_toc->addr.lba;
  2182. lba_end = last_toc->addr.lba;
  2183. if (lba_end <= lba_start)
  2184. return -EINVAL;
  2185. return cdrom_play_audio(drive, lba_start, lba_end);
  2186. }
  2187. case CDROMREADTOCHDR: {
  2188. struct cdrom_tochdr *tochdr = (struct cdrom_tochdr *) arg;
  2189. struct atapi_toc *toc;
  2190. /* Make sure our saved TOC is valid. */
  2191. stat = cdrom_read_toc(drive, NULL);
  2192. if (stat) return stat;
  2193. toc = info->toc;
  2194. tochdr->cdth_trk0 = toc->hdr.first_track;
  2195. tochdr->cdth_trk1 = toc->hdr.last_track;
  2196. return 0;
  2197. }
  2198. case CDROMREADTOCENTRY: {
  2199. struct cdrom_tocentry *tocentry = (struct cdrom_tocentry*) arg;
  2200. struct atapi_toc_entry *toce;
  2201. stat = cdrom_get_toc_entry(drive, tocentry->cdte_track, &toce);
  2202. if (stat) return stat;
  2203. tocentry->cdte_ctrl = toce->control;
  2204. tocentry->cdte_adr = toce->adr;
  2205. if (tocentry->cdte_format == CDROM_MSF) {
  2206. lba_to_msf (toce->addr.lba,
  2207. &tocentry->cdte_addr.msf.minute,
  2208. &tocentry->cdte_addr.msf.second,
  2209. &tocentry->cdte_addr.msf.frame);
  2210. } else
  2211. tocentry->cdte_addr.lba = toce->addr.lba;
  2212. return 0;
  2213. }
  2214. default:
  2215. return -EINVAL;
  2216. }
  2217. }
  2218. static
  2219. int ide_cdrom_reset (struct cdrom_device_info *cdi)
  2220. {
  2221. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2222. struct request_sense sense;
  2223. struct request req;
  2224. int ret;
  2225. cdrom_prepare_request(drive, &req);
  2226. req.flags = REQ_SPECIAL | REQ_QUIET;
  2227. ret = ide_do_drive_cmd(drive, &req, ide_wait);
  2228. /*
  2229. * A reset will unlock the door. If it was previously locked,
  2230. * lock it again.
  2231. */
  2232. if (CDROM_STATE_FLAGS(drive)->door_locked)
  2233. (void) cdrom_lockdoor(drive, 1, &sense);
  2234. return ret;
  2235. }
  2236. static
  2237. int ide_cdrom_tray_move (struct cdrom_device_info *cdi, int position)
  2238. {
  2239. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2240. struct request_sense sense;
  2241. if (position) {
  2242. int stat = cdrom_lockdoor(drive, 0, &sense);
  2243. if (stat) return stat;
  2244. }
  2245. return cdrom_eject(drive, !position, &sense);
  2246. }
  2247. static
  2248. int ide_cdrom_lock_door (struct cdrom_device_info *cdi, int lock)
  2249. {
  2250. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2251. return cdrom_lockdoor(drive, lock, NULL);
  2252. }
  2253. static
  2254. int ide_cdrom_select_speed (struct cdrom_device_info *cdi, int speed)
  2255. {
  2256. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2257. struct request_sense sense;
  2258. int stat;
  2259. if ((stat = cdrom_select_speed(drive, speed, &sense)) < 0)
  2260. return stat;
  2261. cdi->speed = CDROM_STATE_FLAGS(drive)->current_speed;
  2262. return 0;
  2263. }
  2264. /*
  2265. * add logic to try GET_EVENT command first to check for media and tray
  2266. * status. this should be supported by newer cd-r/w and all DVD etc
  2267. * drives
  2268. */
  2269. static
  2270. int ide_cdrom_drive_status (struct cdrom_device_info *cdi, int slot_nr)
  2271. {
  2272. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2273. struct media_event_desc med;
  2274. struct request_sense sense;
  2275. int stat;
  2276. if (slot_nr != CDSL_CURRENT)
  2277. return -EINVAL;
  2278. stat = cdrom_check_status(drive, &sense);
  2279. if (!stat || sense.sense_key == UNIT_ATTENTION)
  2280. return CDS_DISC_OK;
  2281. if (!cdrom_get_media_event(cdi, &med)) {
  2282. if (med.media_present)
  2283. return CDS_DISC_OK;
  2284. else if (med.door_open)
  2285. return CDS_TRAY_OPEN;
  2286. else
  2287. return CDS_NO_DISC;
  2288. }
  2289. if (sense.sense_key == NOT_READY && sense.asc == 0x04 && sense.ascq == 0x04)
  2290. return CDS_DISC_OK;
  2291. /*
  2292. * If not using Mt Fuji extended media tray reports,
  2293. * just return TRAY_OPEN since ATAPI doesn't provide
  2294. * any other way to detect this...
  2295. */
  2296. if (sense.sense_key == NOT_READY) {
  2297. if (sense.asc == 0x3a) {
  2298. if (sense.ascq == 1)
  2299. return CDS_NO_DISC;
  2300. else if (sense.ascq == 0 || sense.ascq == 2)
  2301. return CDS_TRAY_OPEN;
  2302. }
  2303. }
  2304. return CDS_DRIVE_NOT_READY;
  2305. }
  2306. static
  2307. int ide_cdrom_get_last_session (struct cdrom_device_info *cdi,
  2308. struct cdrom_multisession *ms_info)
  2309. {
  2310. struct atapi_toc *toc;
  2311. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2312. struct cdrom_info *info = drive->driver_data;
  2313. struct request_sense sense;
  2314. int ret;
  2315. if (!CDROM_STATE_FLAGS(drive)->toc_valid || info->toc == NULL)
  2316. if ((ret = cdrom_read_toc(drive, &sense)))
  2317. return ret;
  2318. toc = info->toc;
  2319. ms_info->addr.lba = toc->last_session_lba;
  2320. ms_info->xa_flag = toc->xa_flag;
  2321. return 0;
  2322. }
  2323. static
  2324. int ide_cdrom_get_mcn (struct cdrom_device_info *cdi,
  2325. struct cdrom_mcn *mcn_info)
  2326. {
  2327. int stat;
  2328. char mcnbuf[24];
  2329. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2330. /* get MCN */
  2331. if ((stat = cdrom_read_subchannel(drive, 2, mcnbuf, sizeof (mcnbuf), NULL)))
  2332. return stat;
  2333. memcpy (mcn_info->medium_catalog_number, mcnbuf+9,
  2334. sizeof (mcn_info->medium_catalog_number)-1);
  2335. mcn_info->medium_catalog_number[sizeof (mcn_info->medium_catalog_number)-1]
  2336. = '\0';
  2337. return 0;
  2338. }
  2339. /****************************************************************************
  2340. * Other driver requests (open, close, check media change).
  2341. */
  2342. static
  2343. int ide_cdrom_check_media_change_real (struct cdrom_device_info *cdi,
  2344. int slot_nr)
  2345. {
  2346. ide_drive_t *drive = (ide_drive_t*) cdi->handle;
  2347. int retval;
  2348. if (slot_nr == CDSL_CURRENT) {
  2349. (void) cdrom_check_status(drive, NULL);
  2350. retval = CDROM_STATE_FLAGS(drive)->media_changed;
  2351. CDROM_STATE_FLAGS(drive)->media_changed = 0;
  2352. return retval;
  2353. } else {
  2354. return -EINVAL;
  2355. }
  2356. }
  2357. static
  2358. int ide_cdrom_open_real (struct cdrom_device_info *cdi, int purpose)
  2359. {
  2360. return 0;
  2361. }
  2362. /*
  2363. * Close down the device. Invalidate all cached blocks.
  2364. */
  2365. static
  2366. void ide_cdrom_release_real (struct cdrom_device_info *cdi)
  2367. {
  2368. ide_drive_t *drive = cdi->handle;
  2369. if (!cdi->use_count)
  2370. CDROM_STATE_FLAGS(drive)->toc_valid = 0;
  2371. }
  2372. /****************************************************************************
  2373. * Device initialization.
  2374. */
  2375. static struct cdrom_device_ops ide_cdrom_dops = {
  2376. .open = ide_cdrom_open_real,
  2377. .release = ide_cdrom_release_real,
  2378. .drive_status = ide_cdrom_drive_status,
  2379. .media_changed = ide_cdrom_check_media_change_real,
  2380. .tray_move = ide_cdrom_tray_move,
  2381. .lock_door = ide_cdrom_lock_door,
  2382. .select_speed = ide_cdrom_select_speed,
  2383. .get_last_session = ide_cdrom_get_last_session,
  2384. .get_mcn = ide_cdrom_get_mcn,
  2385. .reset = ide_cdrom_reset,
  2386. .audio_ioctl = ide_cdrom_audio_ioctl,
  2387. .dev_ioctl = ide_cdrom_dev_ioctl,
  2388. .capability = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
  2389. CDC_SELECT_SPEED | CDC_SELECT_DISC |
  2390. CDC_MULTI_SESSION | CDC_MCN |
  2391. CDC_MEDIA_CHANGED | CDC_PLAY_AUDIO | CDC_RESET |
  2392. CDC_IOCTLS | CDC_DRIVE_STATUS | CDC_CD_R |
  2393. CDC_CD_RW | CDC_DVD | CDC_DVD_R| CDC_DVD_RAM |
  2394. CDC_GENERIC_PACKET | CDC_MO_DRIVE | CDC_MRW |
  2395. CDC_MRW_W | CDC_RAM,
  2396. .generic_packet = ide_cdrom_packet,
  2397. };
  2398. static int ide_cdrom_register (ide_drive_t *drive, int nslots)
  2399. {
  2400. struct cdrom_info *info = drive->driver_data;
  2401. struct cdrom_device_info *devinfo = &info->devinfo;
  2402. devinfo->ops = &ide_cdrom_dops;
  2403. devinfo->mask = 0;
  2404. devinfo->speed = CDROM_STATE_FLAGS(drive)->current_speed;
  2405. devinfo->capacity = nslots;
  2406. devinfo->handle = (void *) drive;
  2407. strcpy(devinfo->name, drive->name);
  2408. /* set capability mask to match the probe. */
  2409. if (!CDROM_CONFIG_FLAGS(drive)->cd_r)
  2410. devinfo->mask |= CDC_CD_R;
  2411. if (!CDROM_CONFIG_FLAGS(drive)->cd_rw)
  2412. devinfo->mask |= CDC_CD_RW;
  2413. if (!CDROM_CONFIG_FLAGS(drive)->dvd)
  2414. devinfo->mask |= CDC_DVD;
  2415. if (!CDROM_CONFIG_FLAGS(drive)->dvd_r)
  2416. devinfo->mask |= CDC_DVD_R;
  2417. if (!CDROM_CONFIG_FLAGS(drive)->dvd_ram)
  2418. devinfo->mask |= CDC_DVD_RAM;
  2419. if (!CDROM_CONFIG_FLAGS(drive)->is_changer)
  2420. devinfo->mask |= CDC_SELECT_DISC;
  2421. if (!CDROM_CONFIG_FLAGS(drive)->audio_play)
  2422. devinfo->mask |= CDC_PLAY_AUDIO;
  2423. if (!CDROM_CONFIG_FLAGS(drive)->close_tray)
  2424. devinfo->mask |= CDC_CLOSE_TRAY;
  2425. if (!CDROM_CONFIG_FLAGS(drive)->mo_drive)
  2426. devinfo->mask |= CDC_MO_DRIVE;
  2427. devinfo->disk = info->disk;
  2428. return register_cdrom(devinfo);
  2429. }
  2430. static
  2431. int ide_cdrom_get_capabilities(ide_drive_t *drive, struct atapi_capabilities_page *cap)
  2432. {
  2433. struct cdrom_info *info = drive->driver_data;
  2434. struct cdrom_device_info *cdi = &info->devinfo;
  2435. struct packet_command cgc;
  2436. int stat, attempts = 3, size = sizeof(*cap);
  2437. /*
  2438. * ACER50 (and others?) require the full spec length mode sense
  2439. * page capabilities size, but older drives break.
  2440. */
  2441. if (!(!strcmp(drive->id->model, "ATAPI CD ROM DRIVE 50X MAX") ||
  2442. !strcmp(drive->id->model, "WPI CDS-32X")))
  2443. size -= sizeof(cap->pad);
  2444. init_cdrom_command(&cgc, cap, size, CGC_DATA_UNKNOWN);
  2445. do { /* we seem to get stat=0x01,err=0x00 the first time (??) */
  2446. stat = cdrom_mode_sense(cdi, &cgc, GPMODE_CAPABILITIES_PAGE, 0);
  2447. if (!stat)
  2448. break;
  2449. } while (--attempts);
  2450. return stat;
  2451. }
  2452. static
  2453. int ide_cdrom_probe_capabilities (ide_drive_t *drive)
  2454. {
  2455. struct cdrom_info *info = drive->driver_data;
  2456. struct cdrom_device_info *cdi = &info->devinfo;
  2457. struct atapi_capabilities_page cap;
  2458. int nslots = 1;
  2459. if (drive->media == ide_optical) {
  2460. CDROM_CONFIG_FLAGS(drive)->mo_drive = 1;
  2461. CDROM_CONFIG_FLAGS(drive)->ram = 1;
  2462. printk(KERN_ERR "%s: ATAPI magneto-optical drive\n", drive->name);
  2463. return nslots;
  2464. }
  2465. if (CDROM_CONFIG_FLAGS(drive)->nec260 ||
  2466. !strcmp(drive->id->model,"STINGRAY 8422 IDE 8X CD-ROM 7-27-95")) {
  2467. CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
  2468. CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
  2469. return nslots;
  2470. }
  2471. /*
  2472. * we have to cheat a little here. the packet will eventually
  2473. * be queued with ide_cdrom_packet(), which extracts the
  2474. * drive from cdi->handle. Since this device hasn't been
  2475. * registered with the Uniform layer yet, it can't do this.
  2476. * Same goes for cdi->ops.
  2477. */
  2478. cdi->handle = (ide_drive_t *) drive;
  2479. cdi->ops = &ide_cdrom_dops;
  2480. if (ide_cdrom_get_capabilities(drive, &cap))
  2481. return 0;
  2482. if (cap.lock == 0)
  2483. CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
  2484. if (cap.eject)
  2485. CDROM_CONFIG_FLAGS(drive)->no_eject = 0;
  2486. if (cap.cd_r_write)
  2487. CDROM_CONFIG_FLAGS(drive)->cd_r = 1;
  2488. if (cap.cd_rw_write) {
  2489. CDROM_CONFIG_FLAGS(drive)->cd_rw = 1;
  2490. CDROM_CONFIG_FLAGS(drive)->ram = 1;
  2491. }
  2492. if (cap.test_write)
  2493. CDROM_CONFIG_FLAGS(drive)->test_write = 1;
  2494. if (cap.dvd_ram_read || cap.dvd_r_read || cap.dvd_rom)
  2495. CDROM_CONFIG_FLAGS(drive)->dvd = 1;
  2496. if (cap.dvd_ram_write) {
  2497. CDROM_CONFIG_FLAGS(drive)->dvd_ram = 1;
  2498. CDROM_CONFIG_FLAGS(drive)->ram = 1;
  2499. }
  2500. if (cap.dvd_r_write)
  2501. CDROM_CONFIG_FLAGS(drive)->dvd_r = 1;
  2502. if (cap.audio_play)
  2503. CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
  2504. if (cap.mechtype == mechtype_caddy || cap.mechtype == mechtype_popup)
  2505. CDROM_CONFIG_FLAGS(drive)->close_tray = 0;
  2506. /* Some drives used by Apple don't advertise audio play
  2507. * but they do support reading TOC & audio datas
  2508. */
  2509. if (strcmp(drive->id->model, "MATSHITADVD-ROM SR-8187") == 0 ||
  2510. strcmp(drive->id->model, "MATSHITADVD-ROM SR-8186") == 0 ||
  2511. strcmp(drive->id->model, "MATSHITADVD-ROM SR-8176") == 0 ||
  2512. strcmp(drive->id->model, "MATSHITADVD-ROM SR-8174") == 0)
  2513. CDROM_CONFIG_FLAGS(drive)->audio_play = 1;
  2514. #if ! STANDARD_ATAPI
  2515. if (cdi->sanyo_slot > 0) {
  2516. CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
  2517. nslots = 3;
  2518. }
  2519. else
  2520. #endif /* not STANDARD_ATAPI */
  2521. if (cap.mechtype == mechtype_individual_changer ||
  2522. cap.mechtype == mechtype_cartridge_changer) {
  2523. if ((nslots = cdrom_number_of_slots(cdi)) > 1) {
  2524. CDROM_CONFIG_FLAGS(drive)->is_changer = 1;
  2525. CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 1;
  2526. }
  2527. }
  2528. /* The ACER/AOpen 24X cdrom has the speed fields byte-swapped */
  2529. if (!drive->id->model[0] &&
  2530. !strncmp(drive->id->fw_rev, "241N", 4)) {
  2531. CDROM_STATE_FLAGS(drive)->current_speed =
  2532. (((unsigned int)cap.curspeed) + (176/2)) / 176;
  2533. CDROM_CONFIG_FLAGS(drive)->max_speed =
  2534. (((unsigned int)cap.maxspeed) + (176/2)) / 176;
  2535. } else {
  2536. CDROM_STATE_FLAGS(drive)->current_speed =
  2537. (ntohs(cap.curspeed) + (176/2)) / 176;
  2538. CDROM_CONFIG_FLAGS(drive)->max_speed =
  2539. (ntohs(cap.maxspeed) + (176/2)) / 176;
  2540. }
  2541. /* don't print speed if the drive reported 0.
  2542. */
  2543. printk(KERN_INFO "%s: ATAPI", drive->name);
  2544. if (CDROM_CONFIG_FLAGS(drive)->max_speed)
  2545. printk(" %dX", CDROM_CONFIG_FLAGS(drive)->max_speed);
  2546. printk(" %s", CDROM_CONFIG_FLAGS(drive)->dvd ? "DVD-ROM" : "CD-ROM");
  2547. if (CDROM_CONFIG_FLAGS(drive)->dvd_r|CDROM_CONFIG_FLAGS(drive)->dvd_ram)
  2548. printk(" DVD%s%s",
  2549. (CDROM_CONFIG_FLAGS(drive)->dvd_r)? "-R" : "",
  2550. (CDROM_CONFIG_FLAGS(drive)->dvd_ram)? "-RAM" : "");
  2551. if (CDROM_CONFIG_FLAGS(drive)->cd_r|CDROM_CONFIG_FLAGS(drive)->cd_rw)
  2552. printk(" CD%s%s",
  2553. (CDROM_CONFIG_FLAGS(drive)->cd_r)? "-R" : "",
  2554. (CDROM_CONFIG_FLAGS(drive)->cd_rw)? "/RW" : "");
  2555. if (CDROM_CONFIG_FLAGS(drive)->is_changer)
  2556. printk(" changer w/%d slots", nslots);
  2557. else
  2558. printk(" drive");
  2559. printk(", %dkB Cache", be16_to_cpu(cap.buffer_size));
  2560. if (drive->using_dma)
  2561. ide_dma_verbose(drive);
  2562. printk("\n");
  2563. return nslots;
  2564. }
  2565. static void ide_cdrom_add_settings(ide_drive_t *drive)
  2566. {
  2567. ide_add_setting(drive, "dsc_overlap", SETTING_RW, -1, -1, TYPE_BYTE, 0, 1, 1, 1, &drive->dsc_overlap, NULL);
  2568. }
  2569. /*
  2570. * standard prep_rq_fn that builds 10 byte cmds
  2571. */
  2572. static int ide_cdrom_prep_fs(request_queue_t *q, struct request *rq)
  2573. {
  2574. int hard_sect = queue_hardsect_size(q);
  2575. long block = (long)rq->hard_sector / (hard_sect >> 9);
  2576. unsigned long blocks = rq->hard_nr_sectors / (hard_sect >> 9);
  2577. memset(rq->cmd, 0, sizeof(rq->cmd));
  2578. if (rq_data_dir(rq) == READ)
  2579. rq->cmd[0] = GPCMD_READ_10;
  2580. else
  2581. rq->cmd[0] = GPCMD_WRITE_10;
  2582. /*
  2583. * fill in lba
  2584. */
  2585. rq->cmd[2] = (block >> 24) & 0xff;
  2586. rq->cmd[3] = (block >> 16) & 0xff;
  2587. rq->cmd[4] = (block >> 8) & 0xff;
  2588. rq->cmd[5] = block & 0xff;
  2589. /*
  2590. * and transfer length
  2591. */
  2592. rq->cmd[7] = (blocks >> 8) & 0xff;
  2593. rq->cmd[8] = blocks & 0xff;
  2594. rq->cmd_len = 10;
  2595. return BLKPREP_OK;
  2596. }
  2597. /*
  2598. * Most of the SCSI commands are supported directly by ATAPI devices.
  2599. * This transform handles the few exceptions.
  2600. */
  2601. static int ide_cdrom_prep_pc(struct request *rq)
  2602. {
  2603. u8 *c = rq->cmd;
  2604. /*
  2605. * Transform 6-byte read/write commands to the 10-byte version
  2606. */
  2607. if (c[0] == READ_6 || c[0] == WRITE_6) {
  2608. c[8] = c[4];
  2609. c[5] = c[3];
  2610. c[4] = c[2];
  2611. c[3] = c[1] & 0x1f;
  2612. c[2] = 0;
  2613. c[1] &= 0xe0;
  2614. c[0] += (READ_10 - READ_6);
  2615. rq->cmd_len = 10;
  2616. return BLKPREP_OK;
  2617. }
  2618. /*
  2619. * it's silly to pretend we understand 6-byte sense commands, just
  2620. * reject with ILLEGAL_REQUEST and the caller should take the
  2621. * appropriate action
  2622. */
  2623. if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
  2624. rq->errors = ILLEGAL_REQUEST;
  2625. return BLKPREP_KILL;
  2626. }
  2627. return BLKPREP_OK;
  2628. }
  2629. static int ide_cdrom_prep_fn(request_queue_t *q, struct request *rq)
  2630. {
  2631. if (rq->flags & REQ_CMD)
  2632. return ide_cdrom_prep_fs(q, rq);
  2633. else if (rq->flags & REQ_BLOCK_PC)
  2634. return ide_cdrom_prep_pc(rq);
  2635. return 0;
  2636. }
  2637. static
  2638. int ide_cdrom_setup (ide_drive_t *drive)
  2639. {
  2640. struct cdrom_info *info = drive->driver_data;
  2641. struct cdrom_device_info *cdi = &info->devinfo;
  2642. int nslots;
  2643. blk_queue_prep_rq(drive->queue, ide_cdrom_prep_fn);
  2644. blk_queue_dma_alignment(drive->queue, 31);
  2645. drive->queue->unplug_delay = (1 * HZ) / 1000;
  2646. if (!drive->queue->unplug_delay)
  2647. drive->queue->unplug_delay = 1;
  2648. drive->special.all = 0;
  2649. CDROM_STATE_FLAGS(drive)->media_changed = 1;
  2650. CDROM_STATE_FLAGS(drive)->toc_valid = 0;
  2651. CDROM_STATE_FLAGS(drive)->door_locked = 0;
  2652. #if NO_DOOR_LOCKING
  2653. CDROM_CONFIG_FLAGS(drive)->no_doorlock = 1;
  2654. #else
  2655. CDROM_CONFIG_FLAGS(drive)->no_doorlock = 0;
  2656. #endif
  2657. CDROM_CONFIG_FLAGS(drive)->drq_interrupt = ((drive->id->config & 0x0060) == 0x20);
  2658. CDROM_CONFIG_FLAGS(drive)->is_changer = 0;
  2659. CDROM_CONFIG_FLAGS(drive)->cd_r = 0;
  2660. CDROM_CONFIG_FLAGS(drive)->cd_rw = 0;
  2661. CDROM_CONFIG_FLAGS(drive)->test_write = 0;
  2662. CDROM_CONFIG_FLAGS(drive)->dvd = 0;
  2663. CDROM_CONFIG_FLAGS(drive)->dvd_r = 0;
  2664. CDROM_CONFIG_FLAGS(drive)->dvd_ram = 0;
  2665. CDROM_CONFIG_FLAGS(drive)->no_eject = 1;
  2666. CDROM_CONFIG_FLAGS(drive)->supp_disc_present = 0;
  2667. CDROM_CONFIG_FLAGS(drive)->audio_play = 0;
  2668. CDROM_CONFIG_FLAGS(drive)->close_tray = 1;
  2669. /* limit transfer size per interrupt. */
  2670. CDROM_CONFIG_FLAGS(drive)->limit_nframes = 0;
  2671. /* a testament to the nice quality of Samsung drives... */
  2672. if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2430"))
  2673. CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
  2674. else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-2432"))
  2675. CDROM_CONFIG_FLAGS(drive)->limit_nframes = 1;
  2676. /* the 3231 model does not support the SET_CD_SPEED command */
  2677. else if (!strcmp(drive->id->model, "SAMSUNG CD-ROM SCR-3231"))
  2678. cdi->mask |= CDC_SELECT_SPEED;
  2679. #if ! STANDARD_ATAPI
  2680. /* by default Sanyo 3 CD changer support is turned off and
  2681. ATAPI Rev 2.2+ standard support for CD changers is used */
  2682. cdi->sanyo_slot = 0;
  2683. CDROM_CONFIG_FLAGS(drive)->nec260 = 0;
  2684. CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 0;
  2685. CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 0;
  2686. CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 0;
  2687. CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 0;
  2688. if (strcmp (drive->id->model, "V003S0DS") == 0 &&
  2689. drive->id->fw_rev[4] == '1' &&
  2690. drive->id->fw_rev[6] <= '2') {
  2691. /* Vertos 300.
  2692. Some versions of this drive like to talk BCD. */
  2693. CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
  2694. CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
  2695. CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
  2696. CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
  2697. }
  2698. else if (strcmp (drive->id->model, "V006E0DS") == 0 &&
  2699. drive->id->fw_rev[4] == '1' &&
  2700. drive->id->fw_rev[6] <= '2') {
  2701. /* Vertos 600 ESD. */
  2702. CDROM_CONFIG_FLAGS(drive)->toctracks_as_bcd = 1;
  2703. }
  2704. else if (strcmp(drive->id->model, "NEC CD-ROM DRIVE:260") == 0 &&
  2705. strncmp(drive->id->fw_rev, "1.01", 4) == 0) { /* FIXME */
  2706. /* Old NEC260 (not R).
  2707. This drive was released before the 1.2 version
  2708. of the spec. */
  2709. CDROM_CONFIG_FLAGS(drive)->tocaddr_as_bcd = 1;
  2710. CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
  2711. CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
  2712. CDROM_CONFIG_FLAGS(drive)->nec260 = 1;
  2713. }
  2714. else if (strcmp(drive->id->model, "WEARNES CDD-120") == 0 &&
  2715. strncmp(drive->id->fw_rev, "A1.1", 4) == 0) { /* FIXME */
  2716. /* Wearnes */
  2717. CDROM_CONFIG_FLAGS(drive)->playmsf_as_bcd = 1;
  2718. CDROM_CONFIG_FLAGS(drive)->subchan_as_bcd = 1;
  2719. }
  2720. /* Sanyo 3 CD changer uses a non-standard command
  2721. for CD changing */
  2722. else if ((strcmp(drive->id->model, "CD-ROM CDR-C3 G") == 0) ||
  2723. (strcmp(drive->id->model, "CD-ROM CDR-C3G") == 0) ||
  2724. (strcmp(drive->id->model, "CD-ROM CDR_C36") == 0)) {
  2725. /* uses CD in slot 0 when value is set to 3 */
  2726. cdi->sanyo_slot = 3;
  2727. }
  2728. #endif /* not STANDARD_ATAPI */
  2729. info->toc = NULL;
  2730. info->buffer = NULL;
  2731. info->sector_buffered = 0;
  2732. info->nsectors_buffered = 0;
  2733. info->changer_info = NULL;
  2734. info->last_block = 0;
  2735. info->start_seek = 0;
  2736. nslots = ide_cdrom_probe_capabilities (drive);
  2737. /*
  2738. * set correct block size
  2739. */
  2740. blk_queue_hardsect_size(drive->queue, CD_FRAMESIZE);
  2741. if (drive->autotune == IDE_TUNE_DEFAULT ||
  2742. drive->autotune == IDE_TUNE_AUTO)
  2743. drive->dsc_overlap = (drive->next != drive);
  2744. #if 0
  2745. drive->dsc_overlap = (HWIF(drive)->no_dsc) ? 0 : 1;
  2746. if (HWIF(drive)->no_dsc) {
  2747. printk(KERN_INFO "ide-cd: %s: disabling DSC overlap\n",
  2748. drive->name);
  2749. drive->dsc_overlap = 0;
  2750. }
  2751. #endif
  2752. if (ide_cdrom_register(drive, nslots)) {
  2753. printk (KERN_ERR "%s: ide_cdrom_setup failed to register device with the cdrom driver.\n", drive->name);
  2754. info->devinfo.handle = NULL;
  2755. return 1;
  2756. }
  2757. ide_cdrom_add_settings(drive);
  2758. return 0;
  2759. }
  2760. static
  2761. sector_t ide_cdrom_capacity (ide_drive_t *drive)
  2762. {
  2763. unsigned long capacity, sectors_per_frame;
  2764. if (cdrom_read_capacity(drive, &capacity, &sectors_per_frame, NULL))
  2765. return 0;
  2766. return capacity * sectors_per_frame;
  2767. }
  2768. static
  2769. int ide_cdrom_cleanup(ide_drive_t *drive)
  2770. {
  2771. struct cdrom_info *info = drive->driver_data;
  2772. if (ide_unregister_subdriver(drive)) {
  2773. printk(KERN_ERR "%s: %s: failed to ide_unregister_subdriver\n",
  2774. __FUNCTION__, drive->name);
  2775. return 1;
  2776. }
  2777. del_gendisk(info->disk);
  2778. ide_cd_put(info);
  2779. return 0;
  2780. }
  2781. static void ide_cd_release(struct kref *kref)
  2782. {
  2783. struct cdrom_info *info = to_ide_cd(kref);
  2784. struct cdrom_device_info *devinfo = &info->devinfo;
  2785. ide_drive_t *drive = info->drive;
  2786. struct gendisk *g = info->disk;
  2787. if (info->buffer != NULL)
  2788. kfree(info->buffer);
  2789. if (info->toc != NULL)
  2790. kfree(info->toc);
  2791. if (info->changer_info != NULL)
  2792. kfree(info->changer_info);
  2793. if (devinfo->handle == drive && unregister_cdrom(devinfo))
  2794. printk(KERN_ERR "%s: %s failed to unregister device from the cdrom "
  2795. "driver.\n", __FUNCTION__, drive->name);
  2796. drive->dsc_overlap = 0;
  2797. drive->driver_data = NULL;
  2798. blk_queue_prep_rq(drive->queue, NULL);
  2799. g->private_data = NULL;
  2800. put_disk(g);
  2801. kfree(info);
  2802. }
  2803. static int ide_cdrom_attach (ide_drive_t *drive);
  2804. #ifdef CONFIG_PROC_FS
  2805. static int proc_idecd_read_capacity
  2806. (char *page, char **start, off_t off, int count, int *eof, void *data)
  2807. {
  2808. ide_drive_t*drive = (ide_drive_t *)data;
  2809. int len;
  2810. len = sprintf(page,"%llu\n", (long long)ide_cdrom_capacity(drive));
  2811. PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
  2812. }
  2813. static ide_proc_entry_t idecd_proc[] = {
  2814. { "capacity", S_IFREG|S_IRUGO, proc_idecd_read_capacity, NULL },
  2815. { NULL, 0, NULL, NULL }
  2816. };
  2817. #else
  2818. # define idecd_proc NULL
  2819. #endif
  2820. static ide_driver_t ide_cdrom_driver = {
  2821. .owner = THIS_MODULE,
  2822. .name = "ide-cdrom",
  2823. .version = IDECD_VERSION,
  2824. .media = ide_cdrom,
  2825. .busy = 0,
  2826. .supports_dsc_overlap = 1,
  2827. .cleanup = ide_cdrom_cleanup,
  2828. .do_request = ide_do_rw_cdrom,
  2829. .end_request = ide_end_request,
  2830. .error = __ide_error,
  2831. .abort = __ide_abort,
  2832. .proc = idecd_proc,
  2833. .attach = ide_cdrom_attach,
  2834. .drives = LIST_HEAD_INIT(ide_cdrom_driver.drives),
  2835. };
  2836. static int idecd_open(struct inode * inode, struct file * file)
  2837. {
  2838. struct gendisk *disk = inode->i_bdev->bd_disk;
  2839. struct cdrom_info *info;
  2840. ide_drive_t *drive;
  2841. int rc = -ENOMEM;
  2842. if (!(info = ide_cd_get(disk)))
  2843. return -ENXIO;
  2844. drive = info->drive;
  2845. drive->usage++;
  2846. if (!info->buffer)
  2847. info->buffer = kmalloc(SECTOR_BUFFER_SIZE,
  2848. GFP_KERNEL|__GFP_REPEAT);
  2849. if (!info->buffer || (rc = cdrom_open(&info->devinfo, inode, file)))
  2850. drive->usage--;
  2851. if (rc < 0)
  2852. ide_cd_put(info);
  2853. return rc;
  2854. }
  2855. static int idecd_release(struct inode * inode, struct file * file)
  2856. {
  2857. struct gendisk *disk = inode->i_bdev->bd_disk;
  2858. struct cdrom_info *info = ide_cd_g(disk);
  2859. ide_drive_t *drive = info->drive;
  2860. cdrom_release (&info->devinfo, file);
  2861. drive->usage--;
  2862. ide_cd_put(info);
  2863. return 0;
  2864. }
  2865. static int idecd_ioctl (struct inode *inode, struct file *file,
  2866. unsigned int cmd, unsigned long arg)
  2867. {
  2868. struct block_device *bdev = inode->i_bdev;
  2869. struct cdrom_info *info = ide_cd_g(bdev->bd_disk);
  2870. int err;
  2871. err = generic_ide_ioctl(info->drive, file, bdev, cmd, arg);
  2872. if (err == -EINVAL)
  2873. err = cdrom_ioctl(file, &info->devinfo, inode, cmd, arg);
  2874. return err;
  2875. }
  2876. static int idecd_media_changed(struct gendisk *disk)
  2877. {
  2878. struct cdrom_info *info = ide_cd_g(disk);
  2879. return cdrom_media_changed(&info->devinfo);
  2880. }
  2881. static int idecd_revalidate_disk(struct gendisk *disk)
  2882. {
  2883. struct cdrom_info *info = ide_cd_g(disk);
  2884. struct request_sense sense;
  2885. cdrom_read_toc(info->drive, &sense);
  2886. return 0;
  2887. }
  2888. static struct block_device_operations idecd_ops = {
  2889. .owner = THIS_MODULE,
  2890. .open = idecd_open,
  2891. .release = idecd_release,
  2892. .ioctl = idecd_ioctl,
  2893. .media_changed = idecd_media_changed,
  2894. .revalidate_disk= idecd_revalidate_disk
  2895. };
  2896. /* options */
  2897. static char *ignore = NULL;
  2898. module_param(ignore, charp, 0400);
  2899. MODULE_DESCRIPTION("ATAPI CD-ROM Driver");
  2900. static int ide_cdrom_attach (ide_drive_t *drive)
  2901. {
  2902. struct cdrom_info *info;
  2903. struct gendisk *g;
  2904. struct request_sense sense;
  2905. if (!strstr("ide-cdrom", drive->driver_req))
  2906. goto failed;
  2907. if (!drive->present)
  2908. goto failed;
  2909. if (drive->media != ide_cdrom && drive->media != ide_optical)
  2910. goto failed;
  2911. /* skip drives that we were told to ignore */
  2912. if (ignore != NULL) {
  2913. if (strstr(ignore, drive->name)) {
  2914. printk(KERN_INFO "ide-cd: ignoring drive %s\n", drive->name);
  2915. goto failed;
  2916. }
  2917. }
  2918. if (drive->scsi) {
  2919. printk(KERN_INFO "ide-cd: passing drive %s to ide-scsi emulation.\n", drive->name);
  2920. goto failed;
  2921. }
  2922. info = (struct cdrom_info *) kmalloc (sizeof (struct cdrom_info), GFP_KERNEL);
  2923. if (info == NULL) {
  2924. printk(KERN_ERR "%s: Can't allocate a cdrom structure\n", drive->name);
  2925. goto failed;
  2926. }
  2927. g = alloc_disk(1 << PARTN_BITS);
  2928. if (!g)
  2929. goto out_free_cd;
  2930. ide_init_disk(g, drive);
  2931. if (ide_register_subdriver(drive, &ide_cdrom_driver)) {
  2932. printk(KERN_ERR "%s: Failed to register the driver with ide.c\n",
  2933. drive->name);
  2934. goto out_put_disk;
  2935. }
  2936. memset(info, 0, sizeof (struct cdrom_info));
  2937. kref_init(&info->kref);
  2938. info->drive = drive;
  2939. info->driver = &ide_cdrom_driver;
  2940. info->disk = g;
  2941. g->private_data = &info->driver;
  2942. drive->driver_data = info;
  2943. DRIVER(drive)->busy++;
  2944. g->minors = 1;
  2945. snprintf(g->devfs_name, sizeof(g->devfs_name),
  2946. "%s/cd", drive->devfs_name);
  2947. g->driverfs_dev = &drive->gendev;
  2948. g->flags = GENHD_FL_CD | GENHD_FL_REMOVABLE;
  2949. if (ide_cdrom_setup(drive)) {
  2950. struct cdrom_device_info *devinfo = &info->devinfo;
  2951. DRIVER(drive)->busy--;
  2952. ide_unregister_subdriver(drive);
  2953. if (info->buffer != NULL)
  2954. kfree(info->buffer);
  2955. if (info->toc != NULL)
  2956. kfree(info->toc);
  2957. if (info->changer_info != NULL)
  2958. kfree(info->changer_info);
  2959. if (devinfo->handle == drive && unregister_cdrom(devinfo))
  2960. printk (KERN_ERR "%s: ide_cdrom_cleanup failed to unregister device from the cdrom driver.\n", drive->name);
  2961. kfree(info);
  2962. drive->driver_data = NULL;
  2963. goto failed;
  2964. }
  2965. DRIVER(drive)->busy--;
  2966. cdrom_read_toc(drive, &sense);
  2967. g->fops = &idecd_ops;
  2968. g->flags |= GENHD_FL_REMOVABLE;
  2969. add_disk(g);
  2970. return 0;
  2971. out_put_disk:
  2972. put_disk(g);
  2973. out_free_cd:
  2974. kfree(info);
  2975. failed:
  2976. return 1;
  2977. }
  2978. static void __exit ide_cdrom_exit(void)
  2979. {
  2980. ide_unregister_driver(&ide_cdrom_driver);
  2981. }
  2982. static int ide_cdrom_init(void)
  2983. {
  2984. ide_register_driver(&ide_cdrom_driver);
  2985. return 0;
  2986. }
  2987. module_init(ide_cdrom_init);
  2988. module_exit(ide_cdrom_exit);
  2989. MODULE_LICENSE("GPL");