|
|
1.1 root 1: /* #define VERBOSE_IDE_CD_ERRORS 1 */
2: /*
3: * linux/drivers/block/ide-cd.c
4: * ATAPI cd-rom driver. To be used with ide.c.
5: * See Documentation/cdrom/ide-cd for usage information.
6: *
7: * Copyright (C) 1994, 1995, 1996 scott snyder <[email protected]>
8: * Copyright (C) 1996, 1997 Erik Andersen <[email protected]>
9: * Copyright (C) 1998 Jens Axboe and Chris Zwilling
10: *
11: * May be copied or modified under the terms of the GNU General Public License
12: * see linux/COPYING for more information.
13: *
14: * 1.00 Oct 31, 1994 -- Initial version.
15: * 1.01 Nov 2, 1994 -- Fixed problem with starting request in
16: * cdrom_check_status.
17: * 1.03 Nov 25, 1994 -- leaving unmask_intr[] as a user-setting (as for disks)
18: * (from mlord) -- minor changes to cdrom_setup()
19: * -- renamed ide_dev_s to ide_drive_t, enable irq on command
20: * 2.00 Nov 27, 1994 -- Generalize packet command interface;
21: * add audio ioctls.
22: * 2.01 Dec 3, 1994 -- Rework packet command interface to handle devices
23: * which send an interrupt when ready for a command.
24: * 2.02 Dec 11, 1994 -- Cache the TOC in the driver.
25: * Don't use SCMD_PLAYAUDIO_TI; it's not included
26: * in the current version of ATAPI.
27: * Try to use LBA instead of track or MSF addressing
28: * when possible.
29: * Don't wait for READY_STAT.
30: * 2.03 Jan 10, 1995 -- Rewrite block read routines to handle block sizes
31: * other than 2k and to move multiple sectors in a
32: * single transaction.
33: * 2.04 Apr 21, 1995 -- Add work-around for Creative Labs CD220E drives.
34: * Thanks to Nick Saw <[email protected]> for
35: * help in figuring this out. Ditto for Acer and
36: * Aztech drives, which seem to have the same problem.
37: * 2.04b May 30, 1995 -- Fix to match changes in ide.c version 3.16 -ml
38: * 2.05 Jun 8, 1995 -- Don't attempt to retry after an illegal request
39: * or data protect error.
40: * Use HWIF and DEV_HWIF macros as in ide.c.
41: * Always try to do a request_sense after
42: * a failed command.
43: * Include an option to give textual descriptions
44: * of ATAPI errors.
45: * Fix a bug in handling the sector cache which
46: * showed up if the drive returned data in 512 byte
47: * blocks (like Pioneer drives). Thanks to
48: * Richard Hirst <[email protected]> for diagnosing this.
49: * Properly supply the page number field in the
50: * MODE_SELECT command.
51: * PLAYAUDIO12 is broken on the Aztech; work around it.
52: * 2.05x Aug 11, 1995 -- lots of data structure renaming/restructuring in ide.c
53: * (my apologies to Scott, but now ide-cd.c is independent)
54: * 3.00 Aug 22, 1995 -- Implement CDROMMULTISESSION ioctl.
55: * Implement CDROMREADAUDIO ioctl (UNTESTED).
56: * Use input_ide_data() and output_ide_data().
57: * Add door locking.
58: * Fix usage count leak in cdrom_open, which happened
59: * when a read-write mount was attempted.
60: * Try to load the disk on open.
61: * Implement CDROMEJECT_SW ioctl (off by default).
62: * Read total cdrom capacity during open.
63: * Rearrange logic in cdrom_decode_status. Issue
64: * request sense commands for failed packet commands
65: * from here instead of from cdrom_queue_packet_command.
66: * Fix a race condition in retrieving error information.
67: * Suppress printing normal unit attention errors and
68: * some drive not ready errors.
69: * Implement CDROMVOLREAD ioctl.
70: * Implement CDROMREADMODE1/2 ioctls.
71: * Fix race condition in setting up interrupt handlers
72: * when the `serialize' option is used.
73: * 3.01 Sep 2, 1995 -- Fix ordering of reenabling interrupts in
74: * cdrom_queue_request.
75: * Another try at using ide_[input,output]_data.
76: * 3.02 Sep 16, 1995 -- Stick total disk capacity in partition table as well.
77: * Make VERBOSE_IDE_CD_ERRORS dump failed command again.
78: * Dump out more information for ILLEGAL REQUEST errs.
79: * Fix handling of errors occurring before the
80: * packet command is transferred.
81: * Fix transfers with odd bytelengths.
82: * 3.03 Oct 27, 1995 -- Some Creative drives have an id of just `CD'.
83: * `DCI-2S10' drives are broken too.
84: * 3.04 Nov 20, 1995 -- So are Vertos drives.
85: * 3.05 Dec 1, 1995 -- Changes to go with overhaul of ide.c and ide-tape.c
86: * 3.06 Dec 16, 1995 -- Add support needed for partitions.
87: * More workarounds for Vertos bugs (based on patches
88: * from Holger Dietze <[email protected]>).
89: * Try to eliminate byteorder assumptions.
90: * Use atapi_cdrom_subchnl struct definition.
91: * Add STANDARD_ATAPI compilation option.
92: * 3.07 Jan 29, 1996 -- More twiddling for broken drives: Sony 55D,
93: * Vertos 300.
94: * Add NO_DOOR_LOCKING configuration option.
95: * Handle drive_cmd requests w/NULL args (for hdparm -t).
96: * Work around sporadic Sony55e audio play problem.
97: * 3.07a Feb 11, 1996 -- check drive->id for NULL before dereferencing, to fix
98: * problem with "hde=cdrom" with no drive present. -ml
99: * 3.08 Mar 6, 1996 -- More Vertos workarounds.
100: * 3.09 Apr 5, 1996 -- Add CDROMCLOSETRAY ioctl.
101: * Switch to using MSF addressing for audio commands.
102: * Reformat to match kernel tabbing style.
103: * Add CDROM_GET_UPC ioctl.
104: * 3.10 Apr 10, 1996 -- Fix compilation error with STANDARD_ATAPI.
105: * 3.11 Apr 29, 1996 -- Patch from Heiko Eissfeldt <[email protected]>
106: * to remove redundant verify_area calls.
107: * 3.12 May 7, 1996 -- Rudimentary changer support. Based on patches
108: * from Gerhard Zuber <[email protected]>.
109: * Let open succeed even if there's no loaded disc.
110: * 3.13 May 19, 1996 -- Fixes for changer code.
111: * 3.14 May 29, 1996 -- Add work-around for Vertos 600.
112: * (From Hennus Bergman <[email protected]>.)
113: * 3.15 July 2, 1996 -- Added support for Sanyo 3 CD changers
114: * from Ben Galliart <[email protected]> with
115: * special help from Jeff Lightfoot
116: * <[email protected]>
117: * 3.15a July 9, 1996 -- Improved Sanyo 3 CD changer identification
118: * 3.16 Jul 28, 1996 -- Fix from Gadi to reduce kernel stack usage for ioctl.
119: * 3.17 Sep 17, 1996 -- Tweak audio reads for some drives.
120: * Start changing CDROMLOADFROMSLOT to CDROM_SELECT_DISC.
121: *
122: * 3.19 Nov 5, 1996 -- New ide-cd maintainer:
123: * Erik B. Andersen <[email protected]>
124: * 3.20 Jan 13,1997 -- Bug Fixes:
125: * Fix errors on CDROMSTOP (If you have a "Dolphin",
126: * you must define IHAVEADOLPHIN)
127: * Added identifier so new Sanyo CD-changer works
128: * Better detection if door locking isn't supported
129: * 3.21 Jun 16,1997 -- Add work-around for GCD-R580B
130: *
131: * 3.22 Nov 13, 1998 -- New ide-cd maintainers:
132: * Jens Axboe <[email protected]>
133: * Chris Zwilling <[email protected]>
134: *
135: * NOTE: Direct audio reads will only work on some types of drive.
136: * So far, i've received reports of success for Sony and Toshiba drives.
137: *
138: * ALSO NOTE:
139: *
140: * The ide cdrom driver has undergone extensive changes for the
141: * latest development kernel. If you wish to add new features to
142: * this driver, make your changes to the latest version in the
143: * development kernel. Only Bug fixes will be accepted for this
144: * version.
145: *
146: * For those wishing to work on this driver, please be sure you download
147: * and comply with the latest ATAPI standard. This document can be
148: * obtained by anonymous ftp from fission.dt.wdc.com in directory:
149: * /pub/standards/atapi/spec/SFF8020-r2.6/PDF/8020r26.pdf
150: *
151: */
152:
153:
154: /***************************************************************************/
155:
1.1.1.2 root 156: #ifdef MACH
157: #include <kern/sched_prim.h>
158: #endif
159:
1.1 root 160: #include <linux/types.h>
161: #include <linux/kernel.h>
162: #include <linux/delay.h>
163: #include <linux/timer.h>
164: #include <linux/malloc.h>
165: #include <linux/ioport.h>
166: #include <linux/interrupt.h>
167: #include <linux/blkdev.h>
168: #include <linux/errno.h>
169: #include <linux/hdreg.h>
170: #include <linux/cdrom.h>
171: #include <linux/ucdrom.h>
172: #include <asm/irq.h>
173: #include <asm/io.h>
174: #include <asm/byteorder.h>
175: #include <asm/segment.h>
176: #include <asm/unaligned.h>
177:
178: #include "ide.h"
179:
180:
181:
182: /* Turn this on to have the driver print out the meanings of the
183: ATAPI error codes. This will use up additional kernel-space
184: memory, though. */
185:
186: #ifndef VERBOSE_IDE_CD_ERRORS
187: #define VERBOSE_IDE_CD_ERRORS 0
188: #endif
189:
190:
191: /* Turning this on will remove code to work around various nonstandard
192: ATAPI implementations. If you know your drive follows the standard,
193: this will give you a slightly smaller kernel. */
194:
195: #ifndef STANDARD_ATAPI
196: #define STANDARD_ATAPI 0
197: #endif
198:
199:
200: /* Turning this on will disable the door-locking functionality.
201: This is apparently needed for supermount. */
202:
203: #ifndef NO_DOOR_LOCKING
204: #define NO_DOOR_LOCKING 0
205: #endif
206:
207:
208: /* Size of buffer to allocate, in blocks, for audio reads. */
209:
210: #ifndef CDROM_NBLOCKS_BUFFER
211: #define CDROM_NBLOCKS_BUFFER 8
212: #endif
213:
214:
215: /************************************************************************/
216:
217: #define SECTOR_SIZE 512
218: #define SECTOR_BITS 9
219: #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
220:
221: #define MIN(a,b) ((a) < (b) ? (a) : (b))
222:
223: /* special command codes for strategy routine. */
224: #define PACKET_COMMAND 4315
225: #define REQUEST_SENSE_COMMAND 4316
226: #define RESET_DRIVE_COMMAND 4317
227:
228: /* Some ATAPI command opcodes (just like SCSI).
229: (Some other cdrom-specific codes are in cdrom.h.) */
230: #define TEST_UNIT_READY 0x00
231: #define REQUEST_SENSE 0x03
232: #define START_STOP 0x1b
233: #define ALLOW_MEDIUM_REMOVAL 0x1e
234: #define READ_CAPACITY 0x25
235: #define READ_10 0x28
236: #define MODE_SENSE_10 0x5a
237: #define MODE_SELECT_10 0x55
238: #define READ_CD 0xbe
239:
240: #define LOAD_UNLOAD 0xa6
241:
242:
243: /* ATAPI sense keys (mostly copied from scsi.h). */
244:
245: #define NO_SENSE 0x00
246: #define RECOVERED_ERROR 0x01
247: #define NOT_READY 0x02
248: #define MEDIUM_ERROR 0x03
249: #define HARDWARE_ERROR 0x04
250: #define ILLEGAL_REQUEST 0x05
251: #define UNIT_ATTENTION 0x06
252: #define DATA_PROTECT 0x07
253: #define ABORTED_COMMAND 0x0b
254: #define MISCOMPARE 0x0e
255:
256: /* We want some additional flags for cd-rom drives.
257: To save space in the ide_drive_t struct, use some fields which
258: doesn't make sense for cd-roms -- `bios_sect' and `bios_head'. */
259:
260: /* Configuration flags. These describe the capabilities of the drive.
261: They generally do not change after initialization, unless we learn
262: more about the drive from stuff failing. */
263: struct ide_cd_config_flags {
264: __u8 drq_interrupt : 1; /* Device sends an interrupt when ready
265: for a packet command. */
266: __u8 no_doorlock : 1; /* Drive cannot lock the door. */
267: #if ! STANDARD_ATAPI
268: __u8 old_readcd : 1; /* Drive uses old READ CD opcode. */
269: __u8 playmsf_as_bcd : 1; /* PLAYMSF command takes BCD args. */
270: __u8 tocaddr_as_bcd : 1; /* TOC addresses are in BCD. */
271: __u8 toctracks_as_bcd : 1; /* TOC track numbers are in BCD. */
272: __u8 subchan_as_bcd : 1; /* Subchannel info is in BCD. */
273: #endif /* not STANDARD_ATAPI */
274: __u8 reserved : 1;
275: };
276: #define CDROM_CONFIG_FLAGS(drive) ((struct ide_cd_config_flags *)&((drive)->bios_sect))
277:
278:
279: /* State flags. These give information about the current state of the
280: drive, and will change during normal operation. */
281: struct ide_cd_state_flags {
282: __u8 media_changed : 1; /* Driver has noticed a media change. */
283: __u8 toc_valid : 1; /* Saved TOC information is current. */
284: __u8 door_locked : 1; /* We think that the drive door is locked. */
285: __u8 eject_on_close: 1; /* Drive should eject when device is closed. */
286: __u8 sanyo_slot : 2; /* Sanyo 3 CD changer support */
287: __u8 reserved : 2;
288: };
289: #define CDROM_STATE_FLAGS(drive) ((struct ide_cd_state_flags *)&((drive)->bios_head))
290:
291:
292: #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
293:
294:
295:
296: /****************************************************************************
297: * Routines to read and write data from/to the drive, using
298: * the routines input_ide_data() and output_ide_data() from ide.c.
299: *
300: * These routines will round up any request for an odd number of bytes,
301: * so if an odd bytecount is specified, be sure that there's at least one
302: * extra byte allocated for the buffer.
303: */
304:
305:
306: static inline
307: void cdrom_in_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
308: {
309: ++bytecount;
310: ide_input_data (drive, buffer, bytecount / 4);
311: if ((bytecount & 0x03) >= 2) {
312: insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
313: }
314: }
315:
316:
317: static inline
318: void cdrom_out_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
319: {
320: ++bytecount;
321: ide_output_data (drive, buffer, bytecount / 4);
322: if ((bytecount & 0x03) >= 2) {
323: outsw (IDE_DATA_REG,
324: ((byte *)buffer) + (bytecount & ~0x03), 1);
325: }
326: }
327:
328:
329:
330: /****************************************************************************
331: * Descriptions of ATAPI error codes.
332: */
333:
334: #define ARY_LEN(a) ((sizeof(a) / sizeof(a[0])))
335:
336: #if VERBOSE_IDE_CD_ERRORS
337:
338: /* From Table 124 of the ATAPI 1.2 spec. */
339:
340: char *sense_key_texts[16] = {
341: "No sense data",
342: "Recovered error",
343: "Not ready",
344: "Medium error",
345: "Hardware error",
346: "Illegal request",
347: "Unit attention",
348: "Data protect",
349: "(reserved)",
350: "(reserved)",
351: "(reserved)",
352: "Aborted command",
353: "(reserved)",
354: "(reserved)",
355: "Miscompare",
356: "(reserved)",
357: };
358:
359:
360: /* From Table 125 of the ATAPI 1.2 spec. */
361:
362: struct {
363: short asc_ascq;
364: char *text;
365: } sense_data_texts[] = {
366: { 0x0000, "No additional sense information" },
367: { 0x0011, "Audio play operation in progress" },
368: { 0x0012, "Audio play operation paused" },
369: { 0x0013, "Audio play operation successfully completed" },
370: { 0x0014, "Audio play operation stopped due to error" },
371: { 0x0015, "No current audio status to return" },
372:
373: { 0x0200, "No seek complete" },
374:
375: { 0x0400, "Logical unit not ready - cause not reportable" },
376: { 0x0401,
377: "Logical unit not ready - in progress (sic) of becoming ready" },
378: { 0x0402, "Logical unit not ready - initializing command required" },
379: { 0x0403, "Logical unit not ready - manual intervention required" },
380:
381: { 0x0600, "No reference position found" },
382:
383: { 0x0900, "Track following error" },
384: { 0x0901, "Tracking servo failure" },
385: { 0x0902, "Focus servo failure" },
386: { 0x0903, "Spindle servo failure" },
387:
388: { 0x1100, "Unrecovered read error" },
389: { 0x1106, "CIRC unrecovered error" },
390:
391: { 0x1500, "Random positioning error" },
392: { 0x1501, "Mechanical positioning error" },
393: { 0x1502, "Positioning error detected by read of medium" },
394:
395: { 0x1700, "Recovered data with no error correction applied" },
396: { 0x1701, "Recovered data with retries" },
397: { 0x1702, "Recovered data with positive head offset" },
398: { 0x1703, "Recovered data with negative head offset" },
399: { 0x1704, "Recovered data with retries and/or CIRC applied" },
400: { 0x1705, "Recovered data using previous sector ID" },
401:
402: { 0x1800, "Recovered data with error correction applied" },
403: { 0x1801, "Recovered data with error correction and retries applied" },
404: { 0x1802, "Recovered data - the data was auto-reallocated" },
405: { 0x1803, "Recovered data with CIRC" },
406: { 0x1804, "Recovered data with L-EC" },
407: { 0x1805, "Recovered data - recommend reassignment" },
408: { 0x1806, "Recovered data - recommend rewrite" },
409:
410: { 0x1a00, "Parameter list length error" },
411:
412: { 0x2000, "Invalid command operation code" },
413:
414: { 0x2100, "Logical block address out of range" },
415:
416: { 0x2400, "Invalid field in command packet" },
417:
418: { 0x2600, "Invalid field in parameter list" },
419: { 0x2601, "Parameter not supported" },
420: { 0x2602, "Parameter value invalid" },
421: { 0x2603, "Threshold parameters not supported" },
422:
423: { 0x2800, "Not ready to ready transition, medium may have changed" },
424:
425: { 0x2900, "Power on, reset or bus device reset occurred" },
426:
427: { 0x2a00, "Parameters changed" },
428: { 0x2a01, "Mode parameters changed" },
429:
430: { 0x3000, "Incompatible medium installed" },
431: { 0x3001, "Cannot read medium - unknown format" },
432: { 0x3002, "Cannot read medium - incompatible format" },
433:
434: { 0x3700, "Rounded parameter" },
435:
436: { 0x3900, "Saving parameters not supported" },
437:
438: { 0x3a00, "Medium not present" },
439:
440: { 0x3f00, "ATAPI CD-ROM drive operating conditions have changed" },
441: { 0x3f01, "Microcode has been changed" },
442: { 0x3f02, "Changed operating definition" },
443: { 0x3f03, "Inquiry data has changed" },
444:
445: { 0x4000, "Diagnostic failure on component (ASCQ)" },
446:
447: { 0x4400, "Internal ATAPI CD-ROM drive failure" },
448:
449: { 0x4e00, "Overlapped commands attempted" },
450:
451: { 0x5300, "Media load or eject failed" },
452: { 0x5302, "Medium removal prevented" },
453:
454: { 0x5700, "Unable to recover table of contents" },
455:
456: { 0x5a00, "Operator request or state change input (unspecified)" },
457: { 0x5a01, "Operator medium removal request" },
458:
459: { 0x5b00, "Threshold condition met" },
460:
461: { 0x5c00, "Status change" },
462:
463: { 0x6300, "End of user area encountered on this track" },
464:
465: { 0x6400, "Illegal mode for this track" },
466:
467: { 0xbf00, "Loss of streaming" },
468: };
469: #endif
470:
471:
472:
473: /****************************************************************************
474: * Generic packet command support and error handling routines.
475: */
476:
477:
478: static
479: void cdrom_analyze_sense_data (ide_drive_t *drive,
480: struct atapi_request_sense *reqbuf,
481: struct packet_command *failed_command)
482: {
483: /* Don't print not ready or unit attention errors for READ_SUBCHANNEL.
484: Workman (and probably other programs) uses this command to poll
485: the drive, and we don't want to fill the syslog
486: with useless errors. */
487: if (failed_command &&
488: failed_command->c[0] == SCMD_READ_SUBCHANNEL &&
489: (reqbuf->sense_key == NOT_READY ||
490: reqbuf->sense_key == UNIT_ATTENTION))
491: return;
492:
493: #if VERBOSE_IDE_CD_ERRORS
494: {
495: int i;
496: char *s;
497: char buf[80];
498:
499: printk ("ATAPI device %s:\n", drive->name);
500:
501: printk (" Error code: 0x%02x\n", reqbuf->error_code);
502:
503: if (reqbuf->sense_key >= 0 &&
504: reqbuf->sense_key < ARY_LEN (sense_key_texts))
505: s = sense_key_texts[reqbuf->sense_key];
506: else
507: s = "(bad sense key)";
508:
509: printk (" Sense key: 0x%02x - %s\n", reqbuf->sense_key, s);
510:
511: if (reqbuf->asc == 0x40) {
512: sprintf (buf, "Diagnostic failure on component 0x%02x",
513: reqbuf->ascq);
514: s = buf;
515: } else {
516: int lo, hi;
517: int key = (reqbuf->asc << 8);
518: if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
519: key |= reqbuf->ascq;
520:
521: lo = 0;
522: hi = ARY_LEN (sense_data_texts);
523: s = NULL;
524:
525: while (hi > lo) {
526: int mid = (lo + hi) / 2;
527: if (sense_data_texts[mid].asc_ascq == key) {
528: s = sense_data_texts[mid].text;
529: break;
530: }
531: else if (sense_data_texts[mid].asc_ascq > key)
532: hi = mid;
533: else
534: lo = mid+1;
535: }
536: }
537:
538: if (s == NULL) {
539: if (reqbuf->asc > 0x80)
540: s = "(vendor-specific error)";
541: else
542: s = "(reserved error code)";
543: }
544:
545: printk (" Additional sense data: 0x%02x, 0x%02x - %s\n",
546: reqbuf->asc, reqbuf->ascq, s);
547:
548: if (failed_command != NULL) {
549: printk (" Failed packet command: ");
550: for (i=0; i<sizeof (failed_command->c); i++)
551: printk ("%02x ", failed_command->c[i]);
552: printk ("\n");
553: }
554:
555: if (reqbuf->sense_key == ILLEGAL_REQUEST &&
556: (reqbuf->sense_key_specific[0] & 0x80) != 0) {
557: printk (" Error in %s byte %d",
558: (reqbuf->sense_key_specific[0] & 0x40) != 0
559: ? "command packet"
560: : "command data",
561: (reqbuf->sense_key_specific[1] << 8) +
562: reqbuf->sense_key_specific[2]);
563:
564: if ((reqbuf->sense_key_specific[0] & 0x40) != 0) {
565: printk (" bit %d",
566: reqbuf->sense_key_specific[0] & 0x07);
567: }
568:
569: printk ("\n");
570: }
571: }
572:
573: #else /* not VERBOSE_IDE_CD_ERRORS */
574:
575: /* Suppress printing unit attention and `in progress of becoming ready'
576: errors when we're not being verbose. */
577:
578: if (reqbuf->sense_key == UNIT_ATTENTION ||
579: (reqbuf->sense_key == NOT_READY && (reqbuf->asc == 4 ||
580: reqbuf->asc == 0x3a)))
581: return;
582:
583: printk ("%s: code: 0x%02x key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
584: drive->name,
585: reqbuf->error_code, reqbuf->sense_key,
586: reqbuf->asc, reqbuf->ascq);
587: #endif /* not VERBOSE_IDE_CD_ERRORS */
588: }
589:
590:
591: /* Fix up a possibly partially-processed request so that we can
592: start it over entirely, or even put it back on the request queue. */
593: static void restore_request (struct request *rq)
594: {
595: if (rq->buffer != rq->bh->b_data) {
596: int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
597: rq->buffer = rq->bh->b_data;
598: rq->nr_sectors += n;
599: rq->sector -= n;
600: }
601: rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
602: }
603:
604:
605: static void cdrom_queue_request_sense (ide_drive_t *drive,
606: struct semaphore *sem,
607: struct atapi_request_sense *reqbuf,
608: struct packet_command *failed_command)
609: {
610: struct request *rq;
611: struct packet_command *pc;
612: int len;
613:
614: /* If the request didn't explicitly specify where
615: to put the sense data, use the statically allocated structure. */
616: if (reqbuf == NULL)
617: reqbuf = &drive->cdrom_info.sense_data;
618:
619: /* Make up a new request to retrieve sense information. */
620:
621: pc = &HWIF(drive)->request_sense_pc;
622: memset (pc, 0, sizeof (*pc));
623:
624: /* The request_sense structure has an odd number of (16-bit) words,
625: which won't work well with 32-bit transfers. However, we don't care
626: about the last two bytes, so just truncate the structure down
627: to an even length. */
628: len = sizeof (*reqbuf) / 4;
629: len *= 4;
630:
631: pc->c[0] = REQUEST_SENSE;
632: pc->c[4] = len;
1.1.1.2 root 633: pc->buffer = (unsigned char *)reqbuf;
1.1 root 634: pc->buflen = len;
635: pc->sense_data = (struct atapi_request_sense *)failed_command;
636:
637: /* stuff the sense request in front of our current request */
638:
639: rq = &HWIF(drive)->request_sense_request;
640: ide_init_drive_cmd (rq);
641: rq->cmd = REQUEST_SENSE_COMMAND;
642: rq->buffer = (char *)pc;
643: rq->sem = sem;
644: (void) ide_do_drive_cmd (drive, rq, ide_preempt);
645: }
646:
647:
648: static void cdrom_end_request (int uptodate, ide_drive_t *drive)
649: {
650: struct request *rq = HWGROUP(drive)->rq;
651:
1.1.1.3 ! root 652: if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate && !rq->quiet) {
1.1 root 653: struct packet_command *pc = (struct packet_command *)
654: rq->buffer;
655: cdrom_analyze_sense_data (drive,
656: (struct atapi_request_sense *)
657: (pc->buffer - pc->c[4]),
658: (struct packet_command *)
659: pc->sense_data);
660: }
661:
662: ide_end_request (uptodate, HWGROUP(drive));
663: }
664:
665:
666: /* Mark that we've seen a media change, and invalidate our internal
667: buffers. */
668: static void cdrom_saw_media_change (ide_drive_t *drive)
669: {
670: CDROM_STATE_FLAGS (drive)->media_changed = 1;
671: CDROM_STATE_FLAGS (drive)->toc_valid = 0;
672: drive->cdrom_info.nsectors_buffered = 0;
673: }
674:
675:
676: /* Returns 0 if the request should be continued.
677: Returns 1 if the request was ended. */
678: static int cdrom_decode_status (ide_drive_t *drive, int good_stat,
679: int *stat_ret)
680: {
681: struct request *rq = HWGROUP(drive)->rq;
682: int stat, err, sense_key, cmd;
683:
684: /* Check for errors. */
685: stat = GET_STAT();
686: *stat_ret = stat;
687:
688: if (OK_STAT (stat, good_stat, BAD_R_STAT))
689: return 0;
690:
691: /* Got an error. */
692: err = IN_BYTE (IDE_ERROR_REG);
693: sense_key = err >> 4;
694:
695: if (rq == NULL)
696: printk ("%s : missing request in cdrom_decode_status\n",
697: drive->name);
698: else {
699: cmd = rq->cmd;
700:
701: if (cmd == REQUEST_SENSE_COMMAND) {
702: /* We got an error trying to get sense info
703: from the drive (probably while trying
704: to recover from a former error). Just give up. */
705:
706: struct packet_command *pc = (struct packet_command *)
707: rq->buffer;
708: pc->stat = 1;
709: cdrom_end_request (1, drive);
710: ide_error (drive, "request sense failure", stat);
711: return 1;
712:
713: } else if (cmd == PACKET_COMMAND) {
714: /* All other functions, except for READ. */
715:
716: struct packet_command *pc = (struct packet_command *)
717: rq->buffer;
718: struct semaphore *sem = NULL;
719:
720: /* Check for tray open. */
721: if (sense_key == NOT_READY) {
722: cdrom_saw_media_change (drive);
723:
724: /* Print an error message to the syslog.
725: Exception: don't print anything if this
726: is a read subchannel command. This is
727: because workman constantly polls the drive
728: with this command, and we don't want
729: to uselessly fill up the syslog. */
1.1.1.3 ! root 730: if (pc->c[0] != SCMD_READ_SUBCHANNEL && !rq->quiet)
1.1 root 731: printk ("%s : tray open or drive not ready\n",
732: drive->name);
733: } else if (sense_key == UNIT_ATTENTION) {
734: /* Check for media change. */
735: cdrom_saw_media_change (drive);
1.1.1.3 ! root 736: if (!rq->quiet)
! 737: printk ("%s: media changed\n", drive->name);
1.1 root 738: } else {
739: /* Otherwise, print an error. */
1.1.1.3 ! root 740: if (!rq->quiet)
! 741: ide_dump_status (drive, "packet command error",
1.1 root 742: stat);
743: }
744:
745: /* Set the error flag and complete the request.
746: Then, if we have a CHECK CONDITION status,
747: queue a request sense command. We must be careful,
748: though: we don't want the thread in
749: cdrom_queue_packet_command to wake up until
750: the request sense has completed. We do this
751: by transferring the semaphore from the packet
752: command request to the request sense request. */
753:
754: if ((stat & ERR_STAT) != 0) {
755: sem = rq->sem;
756: rq->sem = NULL;
757: }
758:
759: pc->stat = 1;
760: cdrom_end_request (1, drive);
761:
762: if ((stat & ERR_STAT) != 0)
763: cdrom_queue_request_sense (drive, sem,
764: pc->sense_data, pc);
765: } else {
766: /* Handle errors from READ requests. */
767:
768: if (sense_key == NOT_READY) {
769: /* Tray open. */
770: cdrom_saw_media_change (drive);
771:
772: /* Fail the request. */
1.1.1.3 ! root 773: if (!rq->quiet)
! 774: printk ("%s : tray open\n", drive->name);
1.1 root 775: cdrom_end_request (0, drive);
776: } else if (sense_key == UNIT_ATTENTION) {
777: /* Media change. */
778: cdrom_saw_media_change (drive);
779:
780: /* Arrange to retry the request.
781: But be sure to give up if we've retried
782: too many times. */
783: if (++rq->errors > ERROR_MAX)
784: cdrom_end_request (0, drive);
785: } else if (sense_key == ILLEGAL_REQUEST ||
786: sense_key == DATA_PROTECT) {
787: /* No point in retrying after an illegal
788: request or data protect error.*/
1.1.1.3 ! root 789: if (!rq->quiet)
! 790: ide_dump_status (drive, "command error", stat);
1.1 root 791: cdrom_end_request (0, drive);
792: } else if ((err & ~ABRT_ERR) != 0) {
793: /* Go to the default handler
794: for other errors. */
795: ide_error (drive, "cdrom_decode_status", stat);
796: return 1;
797: } else if ((++rq->errors > ERROR_MAX)) {
798: /* We've racked up too many retries. Abort. */
799: cdrom_end_request (0, drive);
800: }
801:
802: /* If we got a CHECK_CONDITION status,
803: queue a request sense command. */
804: if ((stat & ERR_STAT) != 0)
805: cdrom_queue_request_sense (drive,
806: NULL, NULL, NULL);
807: }
808: }
809:
810: /* Retry, or handle the next request. */
811: return 1;
812: }
813:
814:
815: /* Set up the device registers for transferring a packet command on DEV,
816: expecting to later transfer XFERLEN bytes. HANDLER is the routine
817: which actually transfers the command to the drive. If this is a
818: drq_interrupt device, this routine will arrange for HANDLER to be
819: called when the interrupt from the drive arrives. Otherwise, HANDLER
820: will be called immediately after the drive is prepared for the transfer. */
821:
822: static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
823: ide_handler_t *handler)
824: {
825: /* Wait for the controller to be idle. */
826: if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
827:
828: /* Set up the controller registers. */
829: OUT_BYTE (0, IDE_FEATURE_REG);
830: OUT_BYTE (0, IDE_NSECTOR_REG);
831: OUT_BYTE (0, IDE_SECTOR_REG);
832:
833: OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
834: OUT_BYTE (xferlen >> 8 , IDE_HCYL_REG);
835: OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
836:
837: if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
838: ide_set_handler (drive, handler, WAIT_CMD);
839: OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
840: } else {
841: OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
842: (*handler) (drive);
843: }
844:
845: return 0;
846: }
847:
848:
849: /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
850: The device registers must have already been prepared
851: by cdrom_start_packet_command.
852: HANDLER is the interrupt handler to call when the command completes
853: or there's data ready. */
854: static int cdrom_transfer_packet_command (ide_drive_t *drive,
1.1.1.2 root 855: unsigned char *cmd_buf, int cmd_len,
1.1 root 856: ide_handler_t *handler)
857: {
858: if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
859: /* Here we should have been called after receiving an interrupt
860: from the device. DRQ should how be set. */
861: int stat_dum;
862:
863: /* Check for errors. */
864: if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum)) return 1;
865: } else {
866: /* Otherwise, we must wait for DRQ to get set. */
867: if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
868: return 1;
869: }
870:
871: /* Arm the interrupt handler. */
872: ide_set_handler (drive, handler, WAIT_CMD);
873:
874: /* Send the command to the device. */
875: cdrom_out_bytes (drive, cmd_buf, cmd_len);
876:
877: return 0;
878: }
879:
880:
881:
882: /****************************************************************************
883: * Block read functions.
884: */
885:
886: /*
887: * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
888: * buffer. Once the first sector is added, any subsequent sectors are
889: * assumed to be continuous (until the buffer is cleared). For the first
890: * sector added, SECTOR is its sector number. (SECTOR is then ignored until
891: * the buffer is cleared.)
892: */
893: static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
894: int sectors_to_transfer)
895: {
896: struct cdrom_info *info = &drive->cdrom_info;
897:
898: /* Number of sectors to read into the buffer. */
899: int sectors_to_buffer = MIN (sectors_to_transfer,
900: (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
901: info->nsectors_buffered);
902:
903: char *dest;
904:
905: /* If we don't yet have a sector buffer, try to allocate one.
906: If we can't get one atomically, it's not fatal -- we'll just throw
907: the data away rather than caching it. */
908: if (info->sector_buffer == NULL) {
909: info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE,
910: GFP_ATOMIC);
911:
912: /* If we couldn't get a buffer,
913: don't try to buffer anything... */
914: if (info->sector_buffer == NULL)
915: sectors_to_buffer = 0;
916: }
917:
918: /* If this is the first sector in the buffer, remember its number. */
919: if (info->nsectors_buffered == 0)
920: info->sector_buffered = sector;
921:
922: /* Read the data into the buffer. */
923: dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
924: while (sectors_to_buffer > 0) {
925: cdrom_in_bytes (drive, dest, SECTOR_SIZE);
926: --sectors_to_buffer;
927: --sectors_to_transfer;
928: ++info->nsectors_buffered;
929: dest += SECTOR_SIZE;
930: }
931:
932: /* Throw away any remaining data. */
933: while (sectors_to_transfer > 0) {
934: char dum[SECTOR_SIZE];
935: cdrom_in_bytes (drive, dum, sizeof (dum));
936: --sectors_to_transfer;
937: }
938: }
939:
940:
941: /*
942: * Check the contents of the interrupt reason register from the cdrom
943: * and attempt to recover if there are problems. Returns 0 if everything's
944: * ok; nonzero if the request has been terminated.
945: */
946: static inline
947: int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
948: {
949: ireason &= 3;
950: if (ireason == 2) return 0;
951:
952: if (ireason == 0) {
953: /* Whoops... The drive is expecting to receive data from us! */
954: printk ("%s: cdrom_read_intr: "
955: "Drive wants to transfer data the wrong way!\n",
956: drive->name);
957:
958: /* Throw some data at the drive so it doesn't hang
959: and quit this request. */
960: while (len > 0) {
961: int dum = 0;
962: cdrom_out_bytes (drive, &dum, sizeof (dum));
963: len -= sizeof (dum);
964: }
965: } else {
966: /* Drive wants a command packet, or invalid ireason... */
967: printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
968: drive->name, ireason);
969: }
970:
971: cdrom_end_request (0, drive);
972: return -1;
973: }
974:
975:
976: /*
977: * Interrupt routine. Called when a read request has completed.
978: */
979: static void cdrom_read_intr (ide_drive_t *drive)
980: {
981: int stat;
982: int ireason, len, sectors_to_transfer, nskip;
983:
984: struct request *rq = HWGROUP(drive)->rq;
985:
986: /* Check for errors. */
987: if (cdrom_decode_status (drive, 0, &stat)) return;
988:
989: /* Read the interrupt reason and the transfer length. */
990: ireason = IN_BYTE (IDE_NSECTOR_REG);
991: len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
992:
993: /* If DRQ is clear, the command has completed. */
994: if ((stat & DRQ_STAT) == 0) {
995: /* If we're not done filling the current buffer, complain.
996: Otherwise, complete the command normally. */
997: if (rq->current_nr_sectors > 0) {
998: printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
999: drive->name, rq->current_nr_sectors);
1000: cdrom_end_request (0, drive);
1001: } else
1002: cdrom_end_request (1, drive);
1003:
1004: return;
1005: }
1006:
1007: /* Check that the drive is expecting to do the same thing we are. */
1008: if (cdrom_read_check_ireason (drive, len, ireason)) return;
1009:
1010: /* Assume that the drive will always provide data in multiples
1011: of at least SECTOR_SIZE, as it gets hairy to keep track
1012: of the transfers otherwise. */
1013: if ((len % SECTOR_SIZE) != 0) {
1014: printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
1015: drive->name, len);
1016: printk (" This drive is not supported by this version of the driver\n");
1017: cdrom_end_request (0, drive);
1018: return;
1019: }
1020:
1021: /* The number of sectors we need to read from the drive. */
1022: sectors_to_transfer = len / SECTOR_SIZE;
1023:
1024: /* First, figure out if we need to bit-bucket
1025: any of the leading sectors. */
1026: nskip = MIN ((int)(rq->current_nr_sectors -
1027: (rq->bh->b_size >> SECTOR_BITS)),
1028: sectors_to_transfer);
1029:
1030: while (nskip > 0) {
1031: /* We need to throw away a sector. */
1032: char dum[SECTOR_SIZE];
1033: cdrom_in_bytes (drive, dum, sizeof (dum));
1034:
1035: --rq->current_nr_sectors;
1036: --nskip;
1037: --sectors_to_transfer;
1038: }
1039:
1040: /* Now loop while we still have data to read from the drive. */
1041: while (sectors_to_transfer > 0) {
1042: int this_transfer;
1043:
1044: /* If we've filled the present buffer but there's another
1045: chained buffer after it, move on. */
1046: if (rq->current_nr_sectors == 0 &&
1047: rq->nr_sectors > 0)
1048: cdrom_end_request (1, drive);
1049:
1050: /* If the buffers are full, cache the rest of the data in our
1051: internal buffer. */
1052: if (rq->current_nr_sectors == 0) {
1053: cdrom_buffer_sectors (drive,
1054: rq->sector, sectors_to_transfer);
1055: sectors_to_transfer = 0;
1056: } else {
1057: /* Transfer data to the buffers.
1058: Figure out how many sectors we can transfer
1059: to the current buffer. */
1060: this_transfer = MIN (sectors_to_transfer,
1061: rq->current_nr_sectors);
1062:
1063: /* Read this_transfer sectors
1064: into the current buffer. */
1065: while (this_transfer > 0) {
1066: cdrom_in_bytes (drive
1067: , rq->buffer, SECTOR_SIZE);
1068: rq->buffer += SECTOR_SIZE;
1069: --rq->nr_sectors;
1070: --rq->current_nr_sectors;
1071: ++rq->sector;
1072: --this_transfer;
1073: --sectors_to_transfer;
1074: }
1075: }
1076: }
1077:
1078: /* Done moving data!
1079: Wait for another interrupt. */
1080: ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);
1081: }
1082:
1083:
1084: /*
1085: * Try to satisfy some of the current read request from our cached data.
1086: * Returns nonzero if the request has been completed, zero otherwise.
1087: */
1088: static int cdrom_read_from_buffer (ide_drive_t *drive)
1089: {
1090: struct cdrom_info *info = &drive->cdrom_info;
1091: struct request *rq = HWGROUP(drive)->rq;
1092:
1093: /* Can't do anything if there's no buffer. */
1094: if (info->sector_buffer == NULL) return 0;
1095:
1096: /* Loop while this request needs data and the next block is present
1097: in our cache. */
1098: while (rq->nr_sectors > 0 &&
1099: rq->sector >= info->sector_buffered &&
1100: rq->sector < info->sector_buffered + info->nsectors_buffered) {
1101: if (rq->current_nr_sectors == 0)
1102: cdrom_end_request (1, drive);
1103:
1104: memcpy (rq->buffer,
1105: info->sector_buffer +
1106: (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1107: SECTOR_SIZE);
1108: rq->buffer += SECTOR_SIZE;
1109: --rq->current_nr_sectors;
1110: --rq->nr_sectors;
1111: ++rq->sector;
1112: }
1113:
1114: /* If we've satisfied the current request,
1115: terminate it successfully. */
1116: if (rq->nr_sectors == 0) {
1117: cdrom_end_request (1, drive);
1118: return -1;
1119: }
1120:
1121: /* Move on to the next buffer if needed. */
1122: if (rq->current_nr_sectors == 0)
1123: cdrom_end_request (1, drive);
1124:
1125: /* If this condition does not hold, then the kluge i use to
1126: represent the number of sectors to skip at the start of a transfer
1127: will fail. I think that this will never happen, but let's be
1128: paranoid and check. */
1129: if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1130: (rq->sector % SECTORS_PER_FRAME) != 0) {
1131: printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1132: drive->name, rq->sector);
1133: cdrom_end_request (0, drive);
1134: return -1;
1135: }
1136:
1137: return 0;
1138: }
1139:
1140:
1141:
1142: /*
1143: * Routine to send a read packet command to the drive.
1144: * This is usually called directly from cdrom_start_read.
1145: * However, for drq_interrupt devices, it is called from an interrupt
1146: * when the drive is ready to accept the command.
1147: */
1148: static void cdrom_start_read_continuation (ide_drive_t *drive)
1149: {
1150: struct packet_command pc;
1151: struct request *rq = HWGROUP(drive)->rq;
1152:
1153: int nsect, sector, nframes, frame, nskip;
1154:
1155: /* Number of sectors to transfer. */
1156: nsect = rq->nr_sectors;
1157:
1158: #if !STANDARD_ATAPI
1159: if (nsect > drive->cdrom_info.max_sectors)
1160: nsect = drive->cdrom_info.max_sectors;
1161: #endif /* not STANDARD_ATAPI */
1162:
1163: /* Starting sector. */
1164: sector = rq->sector;
1165:
1166: /* If the requested sector doesn't start on a cdrom block boundary,
1167: we must adjust the start of the transfer so that it does,
1168: and remember to skip the first few sectors.
1169: If the CURRENT_NR_SECTORS field is larger than the size
1170: of the buffer, it will mean that we're to skip a number
1171: of sectors equal to the amount by which CURRENT_NR_SECTORS
1172: is larger than the buffer size. */
1173: nskip = (sector % SECTORS_PER_FRAME);
1174: if (nskip > 0) {
1175: /* Sanity check... */
1176: if (rq->current_nr_sectors !=
1177: (rq->bh->b_size >> SECTOR_BITS)) {
1178: printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1179: drive->name, rq->current_nr_sectors);
1180: cdrom_end_request (0, drive);
1181: return;
1182: }
1183:
1184: sector -= nskip;
1185: nsect += nskip;
1186: rq->current_nr_sectors += nskip;
1187: }
1188:
1189: /* Convert from sectors to cdrom blocks, rounding up the transfer
1190: length if needed. */
1191: nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1192: frame = sector / SECTORS_PER_FRAME;
1193:
1194: /* Largest number of frames was can transfer at once is 64k-1. */
1195: nframes = MIN (nframes, 65535);
1196:
1197: /* Set up the command */
1198: memset (&pc.c, 0, sizeof (pc.c));
1199: pc.c[0] = READ_10;
1200: pc.c[7] = (nframes >> 8);
1201: pc.c[8] = (nframes & 0xff);
1202: put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]);
1203:
1204: /* Send the command to the drive and return. */
1205: (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c),
1206: &cdrom_read_intr);
1207: }
1208:
1209:
1210: /*
1211: * Start a read request from the CD-ROM.
1212: */
1213: static void cdrom_start_read (ide_drive_t *drive, unsigned int block)
1214: {
1215: struct request *rq = HWGROUP(drive)->rq;
1216: int minor = MINOR (rq->rq_dev);
1217:
1218: /* If the request is relative to a partition, fix it up to refer to the
1219: absolute address. */
1220: if ((minor & PARTN_MASK) != 0) {
1221: rq->sector = block;
1222: minor &= ~PARTN_MASK;
1223: rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1224: }
1225:
1226: /* We may be retrying this request after an error. Fix up
1227: any weirdness which might be present in the request packet. */
1228: restore_request (rq);
1229:
1230: /* Satisfy whatever we can of this request from our cached sector. */
1231: if (cdrom_read_from_buffer (drive))
1232: return;
1233:
1234: /* Clear the local sector buffer. */
1235: drive->cdrom_info.nsectors_buffered = 0;
1236:
1237: /* Start sending the read request to the drive. */
1238: cdrom_start_packet_command (drive, 32768,
1239: cdrom_start_read_continuation);
1240: }
1241:
1242:
1243:
1244:
1245: /****************************************************************************
1246: * Execute all other packet commands.
1247: */
1248:
1249: /* Forward declarations. */
1250: static int
1251: cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1252: struct atapi_request_sense *reqbuf);
1253:
1254:
1255:
1256: /* Interrupt routine for packet command completion. */
1257: static void cdrom_pc_intr (ide_drive_t *drive)
1258: {
1259: int ireason, len, stat, thislen;
1260: struct request *rq = HWGROUP(drive)->rq;
1261: struct packet_command *pc = (struct packet_command *)rq->buffer;
1262:
1263: /* Check for errors. */
1264: if (cdrom_decode_status (drive, 0, &stat)) return;
1265:
1266: /* Read the interrupt reason and the transfer length. */
1267: ireason = IN_BYTE (IDE_NSECTOR_REG);
1268: len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1269:
1270: /* If DRQ is clear, the command has completed.
1271: Complain if we still have data left to transfer. */
1272: if ((stat & DRQ_STAT) == 0) {
1273: /* Some of the trailing request sense fields are optional, and
1274: some drives don't send them. Sigh. */
1275: if (pc->c[0] == REQUEST_SENSE &&
1276: pc->buflen > 0 &&
1277: pc->buflen <= 5) {
1278: while (pc->buflen > 0) {
1279: *pc->buffer++ = 0;
1280: --pc->buflen;
1281: }
1282: }
1283:
1284: if (pc->buflen == 0)
1285: cdrom_end_request (1, drive);
1286: else {
1287: printk ("%s: cdrom_pc_intr: data underrun %d\n",
1288: drive->name, pc->buflen);
1289: pc->stat = 1;
1290: cdrom_end_request (1, drive);
1291: }
1292: return;
1293: }
1294:
1295: /* Figure out how much data to transfer. */
1296: thislen = pc->buflen;
1297: if (thislen < 0) thislen = -thislen;
1298: if (thislen > len) thislen = len;
1299:
1300: /* The drive wants to be written to. */
1301: if ((ireason & 3) == 0) {
1302: /* Check that we want to write. */
1303: if (pc->buflen > 0) {
1304: printk ("%s: cdrom_pc_intr: Drive wants "
1305: "to transfer data the wrong way!\n",
1306: drive->name);
1307: pc->stat = 1;
1308: thislen = 0;
1309: }
1310:
1311: /* Transfer the data. */
1312: cdrom_out_bytes (drive, pc->buffer, thislen);
1313:
1314: /* If we haven't moved enough data to satisfy the drive,
1315: add some padding. */
1316: while (len > thislen) {
1317: int dum = 0;
1318: cdrom_out_bytes (drive, &dum, sizeof (dum));
1319: len -= sizeof (dum);
1320: }
1321:
1322: /* Keep count of how much data we've moved. */
1323: pc->buffer += thislen;
1324: pc->buflen += thislen;
1325: }
1326:
1327: /* Same drill for reading. */
1328: else if ((ireason & 3) == 2) {
1329: /* Check that we want to read. */
1330: if (pc->buflen < 0) {
1331: printk ("%s: cdrom_pc_intr: Drive wants to "
1332: "transfer data the wrong way!\n",
1333: drive->name);
1334: pc->stat = 1;
1335: thislen = 0;
1336: }
1337:
1338: /* Transfer the data. */
1339: cdrom_in_bytes (drive, pc->buffer, thislen);
1340:
1341: /* If we haven't moved enough data to satisfy the drive,
1342: add some padding. */
1343: while (len > thislen) {
1344: int dum = 0;
1345: cdrom_in_bytes (drive, &dum, sizeof (dum));
1346: len -= sizeof (dum);
1347: }
1348:
1349: /* Keep count of how much data we've moved. */
1350: pc->buffer += thislen;
1351: pc->buflen -= thislen;
1352: } else {
1353: printk ("%s: cdrom_pc_intr: The drive "
1354: "appears confused (ireason = 0x%2x)\n",
1355: drive->name, ireason);
1356: pc->stat = 1;
1357: }
1358:
1359: /* Now we wait for another interrupt. */
1360: ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);
1361: }
1362:
1363:
1364: static void cdrom_do_pc_continuation (ide_drive_t *drive)
1365: {
1366: struct request *rq = HWGROUP(drive)->rq;
1367: struct packet_command *pc = (struct packet_command *)rq->buffer;
1368:
1369: /* Send the command to the drive and return. */
1370: cdrom_transfer_packet_command (drive, pc->c,
1371: sizeof (pc->c), &cdrom_pc_intr);
1372: }
1373:
1374:
1375: static void cdrom_do_packet_command (ide_drive_t *drive)
1376: {
1377: int len;
1378: struct request *rq = HWGROUP(drive)->rq;
1379: struct packet_command *pc = (struct packet_command *)rq->buffer;
1380:
1381: len = pc->buflen;
1382: if (len < 0) len = -len;
1383:
1384: pc->stat = 0;
1385:
1386: /* Start sending the command to the drive. */
1387: cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1388: }
1389:
1390: /* Sleep for TIME jiffies.
1391: Not to be called from an interrupt handler. */
1.1.1.2 root 1392: #ifdef MACH
1393: static
1394: void cdrom_sleep (int time)
1395: {
1396: int xxx;
1397:
1398: assert_wait ((event_t) &xxx, TRUE);
1399: thread_set_timeout (time);
1400: schedule ();
1401: }
1402: #else
1.1 root 1403: static
1404: void cdrom_sleep (int time)
1405: {
1406: current->state = TASK_INTERRUPTIBLE;
1407: current->timeout = jiffies + time;
1408: schedule ();
1409: }
1.1.1.2 root 1410: #endif
1.1 root 1411:
1412: static
1.1.1.3 ! root 1413: int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc, int quiet)
1.1 root 1414: {
1415: struct atapi_request_sense my_reqbuf;
1416: int retries = 10;
1417: struct request req;
1418:
1419: /* If our caller has not provided a place to stick any sense data,
1420: use our own area. */
1421: if (pc->sense_data == NULL)
1422: pc->sense_data = &my_reqbuf;
1423: pc->sense_data->sense_key = 0;
1424:
1425: /* Start of retry loop. */
1426: do {
1427: ide_init_drive_cmd (&req);
1428: req.cmd = PACKET_COMMAND;
1429: req.buffer = (char *)pc;
1.1.1.3 ! root 1430: req.quiet = quiet;
1.1 root 1431: (void) ide_do_drive_cmd (drive, &req, ide_wait);
1432:
1433: if (pc->stat != 0) {
1434: /* The request failed. Retry if it was due to a unit
1435: attention status
1436: (usually means media was changed). */
1437: struct atapi_request_sense *reqbuf = pc->sense_data;
1438:
1439: if (reqbuf->sense_key == UNIT_ATTENTION)
1440: ;
1441: else if (reqbuf->sense_key == NOT_READY &&
1442: reqbuf->asc == 4) {
1443: /* The drive is in the process of loading
1444: a disk. Retry, but wait a little to give
1445: the drive time to complete the load. */
1446: cdrom_sleep (HZ);
1447: } else
1448: /* Otherwise, don't retry. */
1449: retries = 0;
1450:
1451: --retries;
1452: }
1453:
1454: /* End of retry loop. */
1455: } while (pc->stat != 0 && retries >= 0);
1456:
1457:
1458: /* Return an error if the command failed. */
1459: if (pc->stat != 0)
1460: return -EIO;
1461: else {
1462: /* The command succeeded. If it was anything other than
1463: a request sense, eject, or door lock command,
1464: and we think that the door is presently, lock it again.
1465: (The door was probably unlocked via an explicit
1466: CDROMEJECT ioctl.) */
1467: if (CDROM_STATE_FLAGS (drive)->door_locked == 0 &&
1468: (pc->c[0] != REQUEST_SENSE &&
1469: pc->c[0] != ALLOW_MEDIUM_REMOVAL &&
1470: pc->c[0] != START_STOP)) {
1471: (void) cdrom_lockdoor (drive, 1, NULL);
1472: }
1473: return 0;
1474: }
1475: }
1476:
1477:
1478: /****************************************************************************
1479: * cdrom driver request routine.
1480: */
1481:
1482: void ide_do_rw_cdrom (ide_drive_t *drive, unsigned long block)
1483: {
1484: struct request *rq = HWGROUP(drive)->rq;
1485:
1486: if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1487: cdrom_do_packet_command (drive);
1488: else if (rq -> cmd == RESET_DRIVE_COMMAND) {
1489: cdrom_end_request (1, drive);
1490: ide_do_reset (drive);
1491: return;
1492: } else if (rq -> cmd != READ) {
1493: printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1494: cdrom_end_request (0, drive);
1495: } else
1496: cdrom_start_read (drive, block);
1497: }
1498:
1499:
1500:
1501: /****************************************************************************
1502: * Ioctl handling.
1503: *
1504: * Routines which queue packet commands take as a final argument a pointer
1505: * to an atapi_request_sense struct. If execution of the command results
1506: * in an error with a CHECK CONDITION status, this structure will be filled
1507: * with the results of the subsequent request sense command. The pointer
1508: * can also be NULL, in which case no sense information is returned.
1509: */
1510:
1511: #if ! STANDARD_ATAPI
1512: static inline
1513: int bin2bcd (int x)
1514: {
1515: return (x%10) | ((x/10) << 4);
1516: }
1517:
1518:
1519: static inline
1520: int bcd2bin (int x)
1521: {
1522: return (x >> 4) * 10 + (x & 0x0f);
1523: }
1524:
1525: static
1526: void msf_from_bcd (struct atapi_msf *msf)
1527: {
1528: msf->minute = bcd2bin (msf->minute);
1529: msf->second = bcd2bin (msf->second);
1530: msf->frame = bcd2bin (msf->frame);
1531: }
1532:
1533: #endif /* not STANDARD_ATAPI */
1534:
1535:
1536: static inline
1537: void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1538: {
1539: lba += CD_BLOCK_OFFSET;
1540: lba &= 0xffffff; /* negative lbas use only 24 bits */
1541: *m = lba / (CD_SECS * CD_FRAMES);
1542: lba %= (CD_SECS * CD_FRAMES);
1543: *s = lba / CD_FRAMES;
1544: *f = lba % CD_FRAMES;
1545: }
1546:
1547:
1548: static inline
1549: int msf_to_lba (byte m, byte s, byte f)
1550: {
1551: return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
1552: }
1553:
1554:
1555: static int
1556: cdrom_check_status (ide_drive_t *drive,
1557: struct atapi_request_sense *reqbuf)
1558: {
1559: struct packet_command pc;
1560:
1561: memset (&pc, 0, sizeof (pc));
1562:
1563: pc.sense_data = reqbuf;
1564: pc.c[0] = TEST_UNIT_READY;
1565:
1566: /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1567: switch CDs instead of supporting the LOAD_UNLOAD opcode */
1568:
1569: pc.c[7] = CDROM_STATE_FLAGS (drive)->sanyo_slot % 3;
1570:
1.1.1.3 ! root 1571: return cdrom_queue_packet_command (drive, &pc, 1);
1.1 root 1572: }
1573:
1574:
1575: /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1576: static int
1577: cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1578: struct atapi_request_sense *reqbuf)
1579: {
1580: struct atapi_request_sense my_reqbuf;
1581: int stat;
1582: struct packet_command pc;
1583:
1584: if (reqbuf == NULL)
1585: reqbuf = &my_reqbuf;
1586:
1587: /* If the drive cannot lock the door, just pretend. */
1588: if (CDROM_CONFIG_FLAGS (drive)->no_doorlock)
1589: stat = 0;
1590: else {
1591: memset (&pc, 0, sizeof (pc));
1592: pc.sense_data = reqbuf;
1593:
1594: pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1595: pc.c[4] = (lockflag != 0);
1.1.1.3 ! root 1596: stat = cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1597: }
1598:
1599: if (stat == 0)
1600: CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1601: else {
1602: /* If we got an illegal field error, the drive
1603: probably cannot lock the door. */
1604: if (reqbuf->sense_key == ILLEGAL_REQUEST &&
1605: (reqbuf->asc == 0x24 || reqbuf->asc == 0x20)) {
1606: printk ("%s: door locking not supported\n",
1607: drive->name);
1608: CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1609: stat = 0;
1610: CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1611: }
1612: }
1613: return stat;
1614: }
1615:
1616:
1617: /* Eject the disk if EJECTFLAG is 0.
1618: If EJECTFLAG is 1, try to reload the disk. */
1619: static int
1620: cdrom_eject (ide_drive_t *drive, int ejectflag,
1621: struct atapi_request_sense *reqbuf)
1622: {
1623: struct packet_command pc;
1624:
1625: memset (&pc, 0, sizeof (pc));
1626: pc.sense_data = reqbuf;
1627:
1628: pc.c[0] = START_STOP;
1629: pc.c[4] = 2 + (ejectflag != 0);
1.1.1.3 ! root 1630: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1631: }
1632:
1633:
1634: static int
1635: cdrom_pause (ide_drive_t *drive, int pauseflag,
1636: struct atapi_request_sense *reqbuf)
1637: {
1638: struct packet_command pc;
1639:
1640: memset (&pc, 0, sizeof (pc));
1641: pc.sense_data = reqbuf;
1642:
1643: pc.c[0] = SCMD_PAUSE_RESUME;
1644: pc.c[8] = !pauseflag;
1.1.1.3 ! root 1645: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1646: }
1647:
1648:
1649: static int
1650: cdrom_startstop (ide_drive_t *drive, int startflag,
1651: struct atapi_request_sense *reqbuf)
1652: {
1653: struct packet_command pc;
1654:
1655: memset (&pc, 0, sizeof (pc));
1656: pc.sense_data = reqbuf;
1657:
1658: pc.c[0] = START_STOP;
1659: pc.c[1] = 1;
1660: pc.c[4] = startflag;
1.1.1.3 ! root 1661: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1662: }
1663:
1664:
1665: static int
1666: cdrom_read_capacity (ide_drive_t *drive, unsigned *capacity,
1667: struct atapi_request_sense *reqbuf)
1668: {
1669: struct {
1670: unsigned lba;
1671: unsigned blocklen;
1672: } capbuf;
1673:
1674: int stat;
1675: struct packet_command pc;
1676:
1677: memset (&pc, 0, sizeof (pc));
1678: pc.sense_data = reqbuf;
1679:
1680: pc.c[0] = READ_CAPACITY;
1.1.1.2 root 1681: pc.buffer = (unsigned char *)&capbuf;
1.1 root 1682: pc.buflen = sizeof (capbuf);
1683:
1.1.1.3 ! root 1684: stat = cdrom_queue_packet_command (drive, &pc, 1);
1.1 root 1685: if (stat == 0)
1686: *capacity = ntohl (capbuf.lba);
1687:
1688: return stat;
1689: }
1690:
1691:
1692: static int
1693: cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
1694: int format, char *buf, int buflen,
1695: struct atapi_request_sense *reqbuf)
1696: {
1697: struct packet_command pc;
1698:
1699: memset (&pc, 0, sizeof (pc));
1700: pc.sense_data = reqbuf;
1701:
1.1.1.2 root 1702: pc.buffer = (unsigned char *)buf;
1.1 root 1703: pc.buflen = buflen;
1704: pc.c[0] = SCMD_READ_TOC;
1705: pc.c[6] = trackno;
1706: pc.c[7] = (buflen >> 8);
1707: pc.c[8] = (buflen & 0xff);
1708: pc.c[9] = (format << 6);
1709: if (msf_flag) pc.c[1] = 2;
1.1.1.3 ! root 1710: return cdrom_queue_packet_command (drive, &pc, 1);
1.1 root 1711: }
1712:
1713:
1714: /* Try to read the entire TOC for the disk into our internal buffer. */
1715: static int
1716: cdrom_read_toc (ide_drive_t *drive,
1717: struct atapi_request_sense *reqbuf)
1718: {
1719: int stat, ntracks, i;
1720: struct atapi_toc *toc = drive->cdrom_info.toc;
1721: struct {
1722: struct atapi_toc_header hdr;
1723: struct atapi_toc_entry ent;
1724: } ms_tmp;
1725:
1726: if (toc == NULL) {
1727: /* Try to allocate space. */
1728: toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1729: GFP_KERNEL);
1730: drive->cdrom_info.toc = toc;
1731: }
1732:
1733: if (toc == NULL) {
1734: printk ("%s: No cdrom TOC buffer!\n", drive->name);
1735: return -EIO;
1736: }
1737:
1738: /* Check to see if the existing data is still valid.
1739: If it is, just return. */
1740: if (CDROM_STATE_FLAGS (drive)->toc_valid)
1741: (void) cdrom_check_status (drive, NULL);
1742:
1743: if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1744:
1745: /* First read just the header, so we know how long the TOC is. */
1746: stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1747: sizeof (struct atapi_toc_header) +
1748: sizeof (struct atapi_toc_entry),
1749: reqbuf);
1750: if (stat) return stat;
1751:
1752: #if ! STANDARD_ATAPI
1753: if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1754: toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1755: toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1756: }
1757: #endif /* not STANDARD_ATAPI */
1758:
1759: ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1760: if (ntracks <= 0) return -EIO;
1761: if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1762:
1763: /* Now read the whole schmeer. */
1764: stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1765: sizeof (struct atapi_toc_header) +
1766: (ntracks+1) *
1767: sizeof (struct atapi_toc_entry),
1768: reqbuf);
1769: if (stat) return stat;
1770: toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1771:
1772: #if ! STANDARD_ATAPI
1773: if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1774: toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1775: toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1776: }
1777: #endif /* not STANDARD_ATAPI */
1778:
1779: for (i=0; i<=ntracks; i++) {
1780: #if ! STANDARD_ATAPI
1781: if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1782: if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1783: toc->ent[i].track = bcd2bin (toc->ent[i].track);
1784: msf_from_bcd (&toc->ent[i].addr.msf);
1785: }
1786: #endif /* not STANDARD_ATAPI */
1787: toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1788: toc->ent[i].addr.msf.second,
1789: toc->ent[i].addr.msf.frame);
1790: }
1791:
1792: /* Read the multisession information. */
1793: stat = cdrom_read_tocentry (drive, 0, 1, 1,
1794: (char *)&ms_tmp, sizeof (ms_tmp),
1795: reqbuf);
1796: if (stat) return stat;
1797:
1798: #if ! STANDARD_ATAPI
1799: if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1800: msf_from_bcd (&ms_tmp.ent.addr.msf);
1801: #endif /* not STANDARD_ATAPI */
1802:
1803: toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1804: ms_tmp.ent.addr.msf.second,
1805: ms_tmp.ent.addr.msf.frame);
1806:
1807: toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1808:
1809: /* Now try to get the total cdrom capacity. */
1810: stat = cdrom_read_capacity (drive, &toc->capacity, reqbuf);
1811: if (stat) toc->capacity = 0x1fffff;
1812:
1813: HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS]
1814: = toc->capacity * SECTORS_PER_FRAME;
1815: drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1816:
1817: /* Remember that we've read this stuff. */
1818: CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1819:
1820: return 0;
1821: }
1822:
1823:
1824: static int
1825: cdrom_read_subchannel (ide_drive_t *drive, int format,
1826: char *buf, int buflen,
1827: struct atapi_request_sense *reqbuf)
1828: {
1829: struct packet_command pc;
1830:
1831: memset (&pc, 0, sizeof (pc));
1832: pc.sense_data = reqbuf;
1833:
1.1.1.2 root 1834: pc.buffer = (unsigned char *) buf;
1.1 root 1835: pc.buflen = buflen;
1836: pc.c[0] = SCMD_READ_SUBCHANNEL;
1837: pc.c[1] = 2; /* MSF addressing */
1838: pc.c[2] = 0x40; /* request subQ data */
1839: pc.c[3] = format,
1840: pc.c[7] = (buflen >> 8);
1841: pc.c[8] = (buflen & 0xff);
1.1.1.3 ! root 1842: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1843: }
1844:
1845:
1846: /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1847: static int
1848: cdrom_mode_sense (ide_drive_t *drive, int pageno, int modeflag,
1849: char *buf, int buflen,
1850: struct atapi_request_sense *reqbuf)
1851: {
1852: struct packet_command pc;
1853:
1854: memset (&pc, 0, sizeof (pc));
1855: pc.sense_data = reqbuf;
1856:
1.1.1.2 root 1857: pc.buffer = (unsigned char *)buf;
1.1 root 1858: pc.buflen = buflen;
1859: pc.c[0] = MODE_SENSE_10;
1860: pc.c[2] = pageno | (modeflag << 6);
1861: pc.c[7] = (buflen >> 8);
1862: pc.c[8] = (buflen & 0xff);
1.1.1.3 ! root 1863: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1864: }
1865:
1866:
1867: static int
1868: cdrom_mode_select (ide_drive_t *drive, int pageno, char *buf, int buflen,
1869: struct atapi_request_sense *reqbuf)
1870: {
1871: struct packet_command pc;
1872:
1873: memset (&pc, 0, sizeof (pc));
1874: pc.sense_data = reqbuf;
1875:
1.1.1.2 root 1876: pc.buffer = (unsigned char *)buf;
1.1 root 1877: pc.buflen = - buflen;
1878: pc.c[0] = MODE_SELECT_10;
1879: pc.c[1] = 0x10;
1880: pc.c[2] = pageno;
1881: pc.c[7] = (buflen >> 8);
1882: pc.c[8] = (buflen & 0xff);
1.1.1.3 ! root 1883: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1884: }
1885:
1886:
1887: static int
1888: cdrom_play_lba_range_1 (ide_drive_t *drive, int lba_start, int lba_end,
1889: struct atapi_request_sense *reqbuf)
1890: {
1891: struct packet_command pc;
1892:
1893: memset (&pc, 0, sizeof (pc));
1894: pc.sense_data = reqbuf;
1895:
1896: pc.c[0] = SCMD_PLAYAUDIO_MSF;
1897: lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1898: lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1899:
1900: #if ! STANDARD_ATAPI
1901: if (CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd) {
1902: pc.c[3] = bin2bcd (pc.c[3]);
1903: pc.c[4] = bin2bcd (pc.c[4]);
1904: pc.c[5] = bin2bcd (pc.c[5]);
1905: pc.c[6] = bin2bcd (pc.c[6]);
1906: pc.c[7] = bin2bcd (pc.c[7]);
1907: pc.c[8] = bin2bcd (pc.c[8]);
1908: }
1909: #endif /* not STANDARD_ATAPI */
1910:
1.1.1.3 ! root 1911: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 1912: }
1913:
1914:
1915: /* Play audio starting at LBA LBA_START and finishing with the
1916: LBA before LBA_END. */
1917: static int
1918: cdrom_play_lba_range (ide_drive_t *drive, int lba_start, int lba_end,
1919: struct atapi_request_sense *reqbuf)
1920: {
1921: int i, stat;
1922: struct atapi_request_sense my_reqbuf;
1923:
1924: if (reqbuf == NULL)
1925: reqbuf = &my_reqbuf;
1926:
1927: /* Some drives, will, for certain audio cds,
1928: give an error if you ask them to play the entire cd using the
1929: values which are returned in the TOC. The play will succeed,
1930: however, if the ending address is adjusted downwards
1931: by a few frames. */
1932: for (i=0; i<75; i++) {
1933: stat = cdrom_play_lba_range_1 (drive, lba_start, lba_end,
1934: reqbuf);
1935:
1936: if (stat == 0 ||
1937: !(reqbuf->sense_key == ILLEGAL_REQUEST &&
1938: reqbuf->asc == 0x24))
1939: return stat;
1940:
1941: --lba_end;
1942: if (lba_end <= lba_start) break;
1943: }
1944:
1945: return stat;
1946: }
1947:
1948:
1949: static
1950: int cdrom_get_toc_entry (ide_drive_t *drive, int track,
1951: struct atapi_toc_entry **ent,
1952: struct atapi_request_sense *reqbuf)
1953: {
1954: int stat, ntracks;
1955: struct atapi_toc *toc;
1956:
1957: /* Make sure our saved TOC is valid. */
1958: stat = cdrom_read_toc (drive, reqbuf);
1959: if (stat) return stat;
1960:
1961: toc = drive->cdrom_info.toc;
1962:
1963: /* Check validity of requested track number. */
1964: ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1965: if (track == CDROM_LEADOUT)
1966: *ent = &toc->ent[ntracks];
1967: else if (track < toc->hdr.first_track ||
1968: track > toc->hdr.last_track)
1969: return -EINVAL;
1970: else
1971: *ent = &toc->ent[track - toc->hdr.first_track];
1972:
1973: return 0;
1974: }
1975:
1976:
1977: static int
1978: cdrom_read_block (ide_drive_t *drive, int format, int lba, int nblocks,
1979: char *buf, int buflen,
1980: struct atapi_request_sense *reqbuf)
1981: {
1982: struct packet_command pc;
1983: struct atapi_request_sense my_reqbuf;
1984: int stat;
1985:
1986: if (reqbuf == NULL)
1987: reqbuf = &my_reqbuf;
1988:
1989: memset (&pc, 0, sizeof (pc));
1990: pc.sense_data = reqbuf;
1991:
1.1.1.2 root 1992: pc.buffer = (unsigned char *)buf;
1.1 root 1993: pc.buflen = buflen;
1994:
1995: #if ! STANDARD_ATAPI
1996: if (CDROM_CONFIG_FLAGS (drive)->old_readcd)
1997: pc.c[0] = 0xd4;
1998: else
1999: #endif /* not STANDARD_ATAPI */
2000: pc.c[0] = READ_CD;
2001:
2002: pc.c[1] = (format << 2);
2003: put_unaligned(htonl(lba), (unsigned int *) &pc.c[2]);
2004: pc.c[8] = (nblocks & 0xff);
2005: pc.c[7] = ((nblocks>>8) & 0xff);
2006: pc.c[6] = ((nblocks>>16) & 0xff);
2007: if (format <= 1)
2008: pc.c[9] = 0xf8;
2009: else
2010: pc.c[9] = 0x10;
2011:
1.1.1.3 ! root 2012: stat = cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 2013:
2014: #if ! STANDARD_ATAPI
2015: /* If the drive doesn't recognize the READ CD opcode, retry the command
2016: with an older opcode for that command. */
2017: if (stat && reqbuf->sense_key == ILLEGAL_REQUEST &&
2018: reqbuf->asc == 0x20 &&
2019: CDROM_CONFIG_FLAGS (drive)->old_readcd == 0) {
2020: printk ("%s: Drive does not recognize READ_CD;"
2021: "trying opcode 0xd4\n",
2022: drive->name);
2023: CDROM_CONFIG_FLAGS (drive)->old_readcd = 1;
2024: return cdrom_read_block (drive, format, lba, nblocks,
2025: buf, buflen, reqbuf);
2026: }
2027: #endif /* not STANDARD_ATAPI */
2028:
2029: return stat;
2030: }
2031:
2032:
2033: /* If SLOT<0, unload the current slot. Otherwise, try to load SLOT. */
2034: static int
2035: cdrom_load_unload (ide_drive_t *drive, int slot,
2036: struct atapi_request_sense *reqbuf)
2037: {
2038: /* if the drive is a Sanyo 3 CD changer then TEST_UNIT_READY
2039: (used in the cdrom_check_status function) is used to
2040: switch CDs instead of LOAD_UNLOAD */
2041:
2042: if (CDROM_STATE_FLAGS (drive)->sanyo_slot > 0) {
2043:
2044: if ((slot == 1) || (slot == 2)) {
2045: CDROM_STATE_FLAGS (drive)->sanyo_slot = slot;
2046: } else if (slot >= 0) {
2047: CDROM_STATE_FLAGS (drive)->sanyo_slot = 3;
2048: } else {
2049: return 0;
2050: }
2051:
2052: return cdrom_check_status (drive, NULL);
2053:
2054: } else {
2055:
2056: /* ATAPI Rev. 2.2+ standard for requesting switching of
2057: CDs in a multiplatter device */
2058:
2059: struct packet_command pc;
2060:
2061: memset (&pc, 0, sizeof (pc));
2062: pc.sense_data = reqbuf;
2063:
2064: pc.c[0] = LOAD_UNLOAD;
2065: pc.c[4] = 2 + (slot >= 0);
2066: pc.c[8] = slot;
1.1.1.3 ! root 2067: return cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 2068:
2069: }
2070: }
2071:
2072:
2073: int ide_cdrom_ioctl (ide_drive_t *drive, struct inode *inode,
2074: struct file *file, unsigned int cmd, unsigned long arg)
2075: {
2076: switch (cmd) {
2077: case CDROMEJECT: {
2078: int stat;
2079:
2080: if (drive->usage > 1)
2081: return -EBUSY;
2082:
2083: stat = cdrom_lockdoor (drive, 0, NULL);
2084: if (stat) return stat;
2085:
2086: return cdrom_eject (drive, 0, NULL);
2087: }
2088:
2089: case CDROMCLOSETRAY: {
2090: int stat;
2091: if (drive->usage > 1)
2092: return -EBUSY;
2093:
2094: stat = cdrom_eject (drive, 1, NULL);
2095: if (stat) return stat;
2096:
2097: return cdrom_lockdoor (drive, 1, NULL);
2098: }
2099:
2100: case CDROMEJECT_SW: {
2101: CDROM_STATE_FLAGS (drive)->eject_on_close = arg;
2102: return 0;
2103: }
2104:
2105: case CDROMPAUSE:
2106: return cdrom_pause (drive, 1, NULL);
2107:
2108: case CDROMRESUME:
2109: return cdrom_pause (drive, 0, NULL);
2110:
2111: case CDROMSTART:
2112: return cdrom_startstop (drive, 1, NULL);
2113:
2114: case CDROMSTOP: {
2115: #ifdef IHAVEADOLPHIN
2116: /* Certain Drives require this. Most don't
2117: and will produce errors upon CDROMSTOP
2118: pit says the Dolphin needs this. If you
2119: own a dolphin, just define IHAVEADOLPHIN somewhere */
2120: int stat;
2121: stat = cdrom_startstop (drive, 0, NULL);
2122: if (stat) return stat;
2123: return cdrom_eject (drive, 1, NULL);
2124: #endif /* end of IHAVEADOLPHIN */
2125: return cdrom_startstop (drive, 0, NULL);
2126: }
2127:
2128: case CDROMPLAYMSF: {
2129: struct cdrom_msf msf;
2130: int stat, lba_start, lba_end;
2131:
2132: stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
2133: if (stat) return stat;
2134:
2135: memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
2136:
2137: lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
2138: msf.cdmsf_frame0);
2139: lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
2140: msf.cdmsf_frame1) + 1;
2141:
2142: if (lba_end <= lba_start) return -EINVAL;
2143:
2144: return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2145: }
2146:
2147: /* Like just about every other Linux cdrom driver, we ignore the
2148: index part of the request here. */
2149: case CDROMPLAYTRKIND: {
2150: int stat, lba_start, lba_end;
2151: struct cdrom_ti ti;
2152: struct atapi_toc_entry *first_toc, *last_toc;
2153:
2154: stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
2155: if (stat) return stat;
2156:
2157: memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
2158:
2159: stat = cdrom_get_toc_entry (drive, ti.cdti_trk0, &first_toc,
2160: NULL);
2161: if (stat) return stat;
2162: stat = cdrom_get_toc_entry (drive, ti.cdti_trk1, &last_toc,
2163: NULL);
2164: if (stat) return stat;
2165:
2166: if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
2167: lba_start = first_toc->addr.lba;
2168: lba_end = last_toc->addr.lba;
2169:
2170: if (lba_end <= lba_start) return -EINVAL;
2171:
2172: return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2173: }
2174:
2175: case CDROMREADTOCHDR: {
2176: int stat;
2177: struct cdrom_tochdr tochdr;
2178: struct atapi_toc *toc;
2179:
2180: stat = verify_area (VERIFY_WRITE, (void *) arg,
2181: sizeof (tochdr));
2182: if (stat) return stat;
2183:
2184: /* Make sure our saved TOC is valid. */
2185: stat = cdrom_read_toc (drive, NULL);
2186: if (stat) return stat;
2187:
2188: toc = drive->cdrom_info.toc;
2189: tochdr.cdth_trk0 = toc->hdr.first_track;
2190: tochdr.cdth_trk1 = toc->hdr.last_track;
2191:
2192: memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
2193:
2194: return stat;
2195: }
2196:
2197: case CDROMREADTOCENTRY: {
2198: int stat;
2199: struct cdrom_tocentry tocentry;
2200: struct atapi_toc_entry *toce;
2201:
2202: stat = verify_area (VERIFY_WRITE, (void *) arg,
2203: sizeof (tocentry));
2204: if (stat) return stat;
2205:
2206: memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
2207:
2208: stat = cdrom_get_toc_entry (drive, tocentry.cdte_track, &toce,
2209: NULL);
2210: if (stat) return stat;
2211:
2212: tocentry.cdte_ctrl = toce->control;
2213: tocentry.cdte_adr = toce->adr;
2214:
2215: if (tocentry.cdte_format == CDROM_MSF) {
2216: /* convert to MSF */
2217: lba_to_msf (toce->addr.lba,
2218: &tocentry.cdte_addr.msf.minute,
2219: &tocentry.cdte_addr.msf.second,
2220: &tocentry.cdte_addr.msf.frame);
2221: } else
2222: tocentry.cdte_addr.lba = toce->addr.lba;
2223:
2224: memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
2225:
2226: return stat;
2227: }
2228:
2229: case CDROMSUBCHNL: {
2230: struct atapi_cdrom_subchnl scbuf;
2231: int stat;
2232: struct cdrom_subchnl subchnl;
2233:
2234: stat = verify_area (VERIFY_WRITE, (void *) arg,
2235: sizeof (subchnl));
2236: if (stat) return stat;
2237:
2238: memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
2239:
2240: stat = cdrom_read_subchannel (drive, 1, /* current position */
2241: (char *)&scbuf, sizeof (scbuf),
2242: NULL);
2243: if (stat) return stat;
2244:
2245: #if ! STANDARD_ATAPI
2246: if (CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd) {
2247: msf_from_bcd (&scbuf.acdsc_absaddr.msf);
2248: msf_from_bcd (&scbuf.acdsc_reladdr.msf);
2249: }
2250: if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
2251: scbuf.acdsc_trk = bcd2bin (scbuf.acdsc_trk);
2252: #endif /* not STANDARD_ATAPI */
2253:
2254: if (subchnl.cdsc_format == CDROM_MSF) {
2255: subchnl.cdsc_absaddr.msf.minute =
2256: scbuf.acdsc_absaddr.msf.minute;
2257: subchnl.cdsc_absaddr.msf.second =
2258: scbuf.acdsc_absaddr.msf.second;
2259: subchnl.cdsc_absaddr.msf.frame =
2260: scbuf.acdsc_absaddr.msf.frame;
2261:
2262: subchnl.cdsc_reladdr.msf.minute =
2263: scbuf.acdsc_reladdr.msf.minute;
2264: subchnl.cdsc_reladdr.msf.second =
2265: scbuf.acdsc_reladdr.msf.second;
2266: subchnl.cdsc_reladdr.msf.frame =
2267: scbuf.acdsc_reladdr.msf.frame;
2268: } else {
2269: subchnl.cdsc_absaddr.lba =
2270: msf_to_lba (scbuf.acdsc_absaddr.msf.minute,
2271: scbuf.acdsc_absaddr.msf.second,
2272: scbuf.acdsc_absaddr.msf.frame);
2273: subchnl.cdsc_reladdr.lba =
2274: msf_to_lba (scbuf.acdsc_reladdr.msf.minute,
2275: scbuf.acdsc_reladdr.msf.second,
2276: scbuf.acdsc_reladdr.msf.frame);
2277: }
2278:
2279: subchnl.cdsc_audiostatus = scbuf.acdsc_audiostatus;
2280: subchnl.cdsc_ctrl = scbuf.acdsc_ctrl;
2281: subchnl.cdsc_trk = scbuf.acdsc_trk;
2282: subchnl.cdsc_ind = scbuf.acdsc_ind;
2283:
2284: memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
2285:
2286: return stat;
2287: }
2288:
2289: case CDROMVOLCTRL: {
2290: struct cdrom_volctrl volctrl;
2291: char buffer[24], mask[24];
2292: int stat;
2293:
2294: stat = verify_area (VERIFY_READ, (void *) arg,
2295: sizeof (volctrl));
2296: if (stat) return stat;
2297: memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
2298:
2299: stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2300: sizeof (buffer), NULL);
2301: if (stat) return stat;
2302: stat = cdrom_mode_sense (drive, 0x0e, 1, mask,
2303: sizeof (buffer), NULL);
2304: if (stat) return stat;
2305:
2306: buffer[1] = buffer[2] = 0;
2307:
2308: buffer[17] = volctrl.channel0 & mask[17];
2309: buffer[19] = volctrl.channel1 & mask[19];
2310: buffer[21] = volctrl.channel2 & mask[21];
2311: buffer[23] = volctrl.channel3 & mask[23];
2312:
2313: return cdrom_mode_select (drive, 0x0e, buffer,
2314: sizeof (buffer), NULL);
2315: }
2316:
2317: case CDROMVOLREAD: {
2318: struct cdrom_volctrl volctrl;
2319: char buffer[24];
2320: int stat;
2321:
2322: stat = verify_area (VERIFY_WRITE, (void *) arg,
2323: sizeof (volctrl));
2324: if (stat) return stat;
2325:
2326: stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2327: sizeof (buffer), NULL);
2328: if (stat) return stat;
2329:
2330: volctrl.channel0 = buffer[17];
2331: volctrl.channel1 = buffer[19];
2332: volctrl.channel2 = buffer[21];
2333: volctrl.channel3 = buffer[23];
2334:
2335: memcpy_tofs ((void *) arg, &volctrl, sizeof (volctrl));
2336:
2337: return 0;
2338: }
2339:
2340: case CDROMMULTISESSION: {
2341: struct cdrom_multisession ms_info;
2342: struct atapi_toc *toc;
2343: int stat;
2344:
2345: stat = verify_area (VERIFY_WRITE, (void *)arg,
2346: sizeof (ms_info));
2347: if (stat) return stat;
2348:
2349: memcpy_fromfs (&ms_info, (void *)arg, sizeof (ms_info));
2350:
2351: /* Make sure the TOC information is valid. */
2352: stat = cdrom_read_toc (drive, NULL);
2353: if (stat) return stat;
2354:
2355: toc = drive->cdrom_info.toc;
2356:
2357: if (ms_info.addr_format == CDROM_MSF)
2358: lba_to_msf (toc->last_session_lba,
2359: &ms_info.addr.msf.minute,
2360: &ms_info.addr.msf.second,
2361: &ms_info.addr.msf.frame);
2362: else if (ms_info.addr_format == CDROM_LBA)
2363: ms_info.addr.lba = toc->last_session_lba;
2364: else
2365: return -EINVAL;
2366:
2367: ms_info.xa_flag = toc->xa_flag;
2368:
2369: memcpy_tofs ((void *)arg, &ms_info, sizeof (ms_info));
2370:
2371: return 0;
2372: }
2373:
2374: /* Read 2352 byte blocks from audio tracks. */
2375: case CDROMREADAUDIO: {
2376: int stat, lba;
2377: struct atapi_toc *toc;
2378: struct cdrom_read_audio ra;
2379: char *buf;
2380:
2381: /* Make sure the TOC is up to date. */
2382: stat = cdrom_read_toc (drive, NULL);
2383: if (stat) return stat;
2384:
2385: toc = drive->cdrom_info.toc;
2386:
2387: stat = verify_area (VERIFY_READ, (char *)arg, sizeof (ra));
2388: if (stat) return stat;
2389:
2390: memcpy_fromfs (&ra, (void *)arg, sizeof (ra));
2391:
2392: if (ra.nframes < 0 || ra.nframes > toc->capacity)
2393: return -EINVAL;
2394: else if (ra.nframes == 0)
2395: return 0;
2396:
2397: stat = verify_area (VERIFY_WRITE, (char *)ra.buf,
2398: ra.nframes * CD_FRAMESIZE_RAW);
2399: if (stat) return stat;
2400:
2401: if (ra.addr_format == CDROM_MSF)
2402: lba = msf_to_lba (ra.addr.msf.minute,
2403: ra.addr.msf.second,
2404: ra.addr.msf.frame);
2405: else if (ra.addr_format == CDROM_LBA)
2406: lba = ra.addr.lba;
2407: else
2408: return -EINVAL;
2409:
2410: if (lba < 0 || lba >= toc->capacity)
2411: return -EINVAL;
2412:
2413: buf = (char *) kmalloc (CDROM_NBLOCKS_BUFFER*CD_FRAMESIZE_RAW,
2414: GFP_KERNEL);
2415: if (buf == NULL)
2416: return -ENOMEM;
2417:
2418: while (ra.nframes > 0) {
2419: int this_nblocks = ra.nframes;
2420: if (this_nblocks > CDROM_NBLOCKS_BUFFER)
2421: this_nblocks = CDROM_NBLOCKS_BUFFER;
2422: stat = cdrom_read_block
2423: (drive, 1, lba, this_nblocks,
2424: buf, this_nblocks * CD_FRAMESIZE_RAW, NULL);
2425: if (stat) break;
2426:
2427: memcpy_tofs (ra.buf, buf,
2428: this_nblocks * CD_FRAMESIZE_RAW);
2429: ra.buf += this_nblocks * CD_FRAMESIZE_RAW;
2430: ra.nframes -= this_nblocks;
2431: lba += this_nblocks;
2432: }
2433:
2434: kfree (buf);
2435: return stat;
2436: }
2437: case CDROMREADRAW:
2438: case CDROMREADMODE1:
2439: case CDROMREADMODE2: {
2440: struct cdrom_msf msf;
2441: int blocksize, format, stat, lba;
2442: char *buf;
2443:
2444: if (cmd == CDROMREADMODE1) {
2445: blocksize = CD_FRAMESIZE;
2446: format = 2;
2447: } else if (cmd == CDROMREADMODE2) {
2448: blocksize = CD_FRAMESIZE_RAW0;
2449: format = 3;
2450: } else {
2451: blocksize = CD_FRAMESIZE_RAW;
2452: format = 0;
2453: }
2454:
2455: stat = verify_area (VERIFY_WRITE, (char *)arg, blocksize);
2456: if (stat) return stat;
2457:
2458: memcpy_fromfs (&msf, (void *)arg, sizeof (msf));
2459:
2460: lba = msf_to_lba (msf.cdmsf_min0,
2461: msf.cdmsf_sec0,
2462: msf.cdmsf_frame0);
2463:
2464: /* DON'T make sure the TOC is up to date. */
2465: /* stat = cdrom_read_toc (drive, NULL);
2466: if (stat) return stat;
2467:
2468: toc = drive->cdrom_info.toc;
2469:
2470: if (lba < 0 || lba >= toc->capacity)
2471: return -EINVAL; */
2472:
2473: buf = (char *) kmalloc (CD_FRAMESIZE_RAW, GFP_KERNEL);
2474: if (buf == NULL)
2475: return -ENOMEM;
2476:
2477: stat = cdrom_read_block (drive, format, lba, 1, buf, blocksize,
2478: NULL);
2479: if (stat == 0)
2480: memcpy_tofs ((char *)arg, buf, blocksize);
2481:
2482: kfree (buf);
2483: return stat;
2484: }
2485:
2486: case CDROM_GET_UPC: {
2487: int stat;
2488: char mcnbuf[24];
2489: struct cdrom_mcn mcn;
2490:
2491: stat = verify_area (VERIFY_WRITE, (void *) arg,
2492: sizeof (mcn));
2493: if (stat) return stat;
2494:
2495: stat = cdrom_read_subchannel (drive, 2, /* get MCN */
2496: mcnbuf, sizeof (mcnbuf),
2497: NULL);
2498: if (stat) return stat;
2499:
2500: memcpy (mcn.medium_catalog_number, mcnbuf+9,
2501: sizeof (mcn.medium_catalog_number)-1);
2502: mcn.medium_catalog_number[sizeof (mcn.medium_catalog_number)-1]
2503: = '\0';
2504:
2505: memcpy_tofs ((void *) arg, &mcn, sizeof (mcn));
2506:
2507: return stat;
2508: }
2509:
2510: case CDROMLOADFROMSLOT:
2511: printk ("%s: Use CDROM_SELECT_DISC "
2512: " instead of CDROMLOADFROMSLOT.\n", drive->name);
2513: /* Fall through. */
2514:
2515: case CDROM_SELECT_DISC: {
2516: struct atapi_request_sense my_reqbuf;
2517: int stat;
2518:
2519: if (drive->usage > 1)
2520: return -EBUSY;
2521:
2522: (void) cdrom_load_unload (drive, -1, NULL);
2523:
2524: cdrom_saw_media_change (drive);
2525: if (arg == -1) {
2526: (void) cdrom_lockdoor (drive, 0, NULL);
2527: return 0;
2528: }
2529: (void) cdrom_load_unload (drive, (int)arg, NULL);
2530:
2531: stat = cdrom_check_status (drive, &my_reqbuf);
2532: if (stat && my_reqbuf.sense_key == NOT_READY) {
2533: return -ENOENT;
2534: }
2535:
2536: /* And try to read the TOC information now. */
2537: return cdrom_read_toc (drive, &my_reqbuf);
2538: }
2539:
2540: #if 0 /* Doesn't work reliably yet. */
2541: case CDROMRESET: {
2542: struct request req;
2543: ide_init_drive_cmd (&req);
2544: req.cmd = RESET_DRIVE_COMMAND;
2545: return ide_do_drive_cmd (drive, &req, ide_wait);
2546: }
2547: #endif
2548:
2549:
2550: #ifdef TEST
2551: case 0x1234: {
2552: int stat;
2553: struct packet_command pc;
2554: int len, lena;
2555:
2556: memset (&pc, 0, sizeof (pc));
2557:
2558: stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
2559: if (stat) return stat;
2560: memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
2561: arg += sizeof (pc.c);
2562:
2563: stat = verify_area (VERIFY_READ, (void *) arg, sizeof (len));
2564: if (stat) return stat;
2565: memcpy_fromfs (&len, (void *) arg , sizeof (len));
2566: arg += sizeof (len);
2567:
2568: if (len > 0) {
2569: stat = verify_area (VERIFY_WRITE, (void *) arg, len);
2570: if (stat) return stat;
2571: }
2572:
2573: lena = len;
2574: if (lena < 0) lena = 0;
2575:
2576: {
2577: char buf[lena];
2578: if (len > 0) {
2579: pc.buflen = len;
2580: pc.buffer = buf;
2581: }
2582:
1.1.1.3 ! root 2583: stat = cdrom_queue_packet_command (drive, &pc, 0);
1.1 root 2584:
2585: if (len > 0)
2586: memcpy_tofs ((void *)arg, buf, len);
2587: }
2588:
2589: return stat;
2590: }
2591: #endif
2592:
2593: default:
2594: return -EPERM;
2595: }
2596:
2597: }
2598:
2599:
2600:
2601: /****************************************************************************
2602: * Other driver requests (open, close, check media change).
2603: */
2604:
2605: int ide_cdrom_check_media_change (ide_drive_t *drive)
2606: {
2607: int retval;
2608:
2609: (void) cdrom_check_status (drive, NULL);
2610:
2611: retval = CDROM_STATE_FLAGS (drive)->media_changed;
2612: CDROM_STATE_FLAGS (drive)->media_changed = 0;
2613:
2614: return retval;
2615: }
2616:
2617:
2618: int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2619: {
2620: /* no write access */
2621: if (fp->f_mode & 2) {
2622: --drive->usage;
2623: return -EROFS;
2624: }
2625:
2626: /* If this is the first open, check the drive status. */
2627: if (drive->usage == 1) {
2628: int stat;
2629: struct atapi_request_sense my_reqbuf;
2630: my_reqbuf.sense_key = 0;
2631:
2632: /* Get the drive status. */
2633: stat = cdrom_check_status (drive, &my_reqbuf);
2634:
2635: /* If the tray is open, try to close it. */
2636: if (stat && my_reqbuf.sense_key == NOT_READY) {
2637: cdrom_eject (drive, 1, &my_reqbuf);
2638: stat = cdrom_check_status (drive, &my_reqbuf);
2639: }
2640:
2641: /* If things worked ok, lock the door and read the
2642: TOC information. */
2643: if (stat == 0 || my_reqbuf.sense_key == UNIT_ATTENTION) {
2644: (void) cdrom_lockdoor (drive, 1, &my_reqbuf);
2645: (void) cdrom_read_toc (drive, &my_reqbuf);
1.1.1.3 ! root 2646: } else {
! 2647: /* Otherwise return as missing */
! 2648: --drive->usage;
! 2649: return -ENXIO;
1.1 root 2650: }
2651: }
2652:
2653: return 0;
2654: }
2655:
2656:
2657: /*
2658: * Close down the device. Invalidate all cached blocks.
2659: */
2660:
2661: void ide_cdrom_release (struct inode *inode, struct file *file,
2662: ide_drive_t *drive)
2663: {
2664: if (drive->usage == 0) {
2665: invalidate_buffers (inode->i_rdev);
2666:
2667: /* Unlock the door. */
2668: (void) cdrom_lockdoor (drive, 0, NULL);
2669:
2670: /* Do an eject if we were requested to do so. */
2671: if (CDROM_STATE_FLAGS (drive)->eject_on_close)
2672: (void) cdrom_eject (drive, 0, NULL);
2673: }
2674: }
2675:
2676:
2677:
2678: /****************************************************************************
2679: * Device initialization.
2680: */
2681:
2682: void ide_cdrom_setup (ide_drive_t *drive)
2683: {
2684: blksize_size[HWIF(drive)->major][drive->select.b.unit << PARTN_BITS] =
2685: CD_FRAMESIZE;
2686:
2687: drive->special.all = 0;
2688: drive->ready_stat = 0;
2689:
2690: CDROM_STATE_FLAGS (drive)->media_changed = 0;
2691: CDROM_STATE_FLAGS (drive)->toc_valid = 0;
2692: CDROM_STATE_FLAGS (drive)->door_locked = 0;
2693:
2694: /* Turn this off by default, since many people don't like it. */
2695: CDROM_STATE_FLAGS (drive)->eject_on_close= 0;
2696:
2697: #if NO_DOOR_LOCKING
2698: CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2699: #else
2700: CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2701: #endif
2702:
2703: /* by default Sanyo 3 CD changer support is turned off and
2704: ATAPI Rev 2.2+ standard support for CD changers is used */
2705: CDROM_STATE_FLAGS (drive)->sanyo_slot = 0;
2706:
2707: if (drive->id != NULL)
2708: CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2709: ((drive->id->config & 0x0060) == 0x20);
2710: else
2711: CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2712:
2713: #if ! STANDARD_ATAPI
2714: drive->cdrom_info.max_sectors = 252;
2715:
2716: CDROM_CONFIG_FLAGS (drive)->old_readcd = 0;
2717: CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2718: CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2719: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2720: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2721:
2722: if (drive->id != NULL) {
1.1.1.2 root 2723: const char *model = (const char *)drive->id->model;
2724: const char *fw_rev = (const char *)drive->id->fw_rev;
2725:
2726: if (strcmp (model, "V003S0DS") == 0 &&
2727: fw_rev[4] == '1' &&
2728: fw_rev[6] <= '2') {
1.1 root 2729: /* Vertos 300.
2730: Some versions of this drive like to talk BCD. */
2731: CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2732: CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2733: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2734: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2735: }
2736:
1.1.1.2 root 2737: else if (strcmp (model, "V006E0DS") == 0 &&
2738: fw_rev[4] == '1' &&
2739: fw_rev[6] <= '2') {
1.1 root 2740: /* Vertos 600 ESD. */
2741: CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2742: }
2743:
1.1.1.2 root 2744: else if (strcmp (model, "GCD-R580B") == 0)
1.1 root 2745: drive->cdrom_info.max_sectors = 124;
2746:
1.1.1.2 root 2747: else if (strcmp (model,
1.1 root 2748: "NEC CD-ROM DRIVE:260") == 0 &&
1.1.1.2 root 2749: strcmp (fw_rev, "1.01") == 0) {
1.1 root 2750: /* Old NEC260 (not R). */
2751: CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2752: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2753: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2754: }
2755:
1.1.1.2 root 2756: else if (strcmp (model, "WEARNES CDD-120") == 0 &&
2757: strcmp (fw_rev, "A1.1") == 0) {
1.1 root 2758: /* Wearnes */
2759: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2760: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2761: }
2762:
2763: /* Sanyo 3 CD changer uses a non-standard command
2764: for CD changing */
1.1.1.2 root 2765: else if ((strcmp(model, "CD-ROM CDR-C3 G") == 0) ||
2766: (strcmp(model, "CD-ROM CDR-C3G") == 0) ||
2767: (strcmp(model, "CD-ROM CDR_C36") == 0)) {
1.1 root 2768: /* uses CD in slot 0 when value is set to 3 */
2769: CDROM_STATE_FLAGS (drive)->sanyo_slot = 3;
2770: }
2771:
2772: }
2773: #endif /* not STANDARD_ATAPI */
2774:
2775: drive->cdrom_info.toc = NULL;
2776: drive->cdrom_info.sector_buffer = NULL;
2777: drive->cdrom_info.sector_buffered = 0;
2778: drive->cdrom_info.nsectors_buffered = 0;
2779: }
2780:
2781:
2782:
2783: /*
2784: * TODO (for 2.1?):
2785: * Avoid printing error messages for expected errors from the drive.
2786: * Integrate with generic cdrom driver.
2787: * Query the drive to find what features are available
2788: * before trying to use them.
2789: * Integrate spindown time adjustment patch.
2790: * Modularize.
2791: * CDROMRESET ioctl.
2792: * Better support for changers.
2793: */
2794:
2795:
2796:
2797: /*==========================================================================*/
2798: /*
2799: * Local variables:
2800: * c-basic-offset: 8
2801: * End:
2802: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.