|
|
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:
652: if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
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. */
730: if (pc->c[0] != SCMD_READ_SUBCHANNEL)
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);
736: printk ("%s: media changed\n", drive->name);
737: } else {
738: /* Otherwise, print an error. */
739: ide_dump_status (drive, "packet command error",
740: stat);
741: }
742:
743: /* Set the error flag and complete the request.
744: Then, if we have a CHECK CONDITION status,
745: queue a request sense command. We must be careful,
746: though: we don't want the thread in
747: cdrom_queue_packet_command to wake up until
748: the request sense has completed. We do this
749: by transferring the semaphore from the packet
750: command request to the request sense request. */
751:
752: if ((stat & ERR_STAT) != 0) {
753: sem = rq->sem;
754: rq->sem = NULL;
755: }
756:
757: pc->stat = 1;
758: cdrom_end_request (1, drive);
759:
760: if ((stat & ERR_STAT) != 0)
761: cdrom_queue_request_sense (drive, sem,
762: pc->sense_data, pc);
763: } else {
764: /* Handle errors from READ requests. */
765:
766: if (sense_key == NOT_READY) {
767: /* Tray open. */
768: cdrom_saw_media_change (drive);
769:
770: /* Fail the request. */
771: printk ("%s : tray open\n", drive->name);
772: cdrom_end_request (0, drive);
773: } else if (sense_key == UNIT_ATTENTION) {
774: /* Media change. */
775: cdrom_saw_media_change (drive);
776:
777: /* Arrange to retry the request.
778: But be sure to give up if we've retried
779: too many times. */
780: if (++rq->errors > ERROR_MAX)
781: cdrom_end_request (0, drive);
782: } else if (sense_key == ILLEGAL_REQUEST ||
783: sense_key == DATA_PROTECT) {
784: /* No point in retrying after an illegal
785: request or data protect error.*/
786: ide_dump_status (drive, "command error", stat);
787: cdrom_end_request (0, drive);
788: } else if ((err & ~ABRT_ERR) != 0) {
789: /* Go to the default handler
790: for other errors. */
791: ide_error (drive, "cdrom_decode_status", stat);
792: return 1;
793: } else if ((++rq->errors > ERROR_MAX)) {
794: /* We've racked up too many retries. Abort. */
795: cdrom_end_request (0, drive);
796: }
797:
798: /* If we got a CHECK_CONDITION status,
799: queue a request sense command. */
800: if ((stat & ERR_STAT) != 0)
801: cdrom_queue_request_sense (drive,
802: NULL, NULL, NULL);
803: }
804: }
805:
806: /* Retry, or handle the next request. */
807: return 1;
808: }
809:
810:
811: /* Set up the device registers for transferring a packet command on DEV,
812: expecting to later transfer XFERLEN bytes. HANDLER is the routine
813: which actually transfers the command to the drive. If this is a
814: drq_interrupt device, this routine will arrange for HANDLER to be
815: called when the interrupt from the drive arrives. Otherwise, HANDLER
816: will be called immediately after the drive is prepared for the transfer. */
817:
818: static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
819: ide_handler_t *handler)
820: {
821: /* Wait for the controller to be idle. */
822: if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
823:
824: /* Set up the controller registers. */
825: OUT_BYTE (0, IDE_FEATURE_REG);
826: OUT_BYTE (0, IDE_NSECTOR_REG);
827: OUT_BYTE (0, IDE_SECTOR_REG);
828:
829: OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
830: OUT_BYTE (xferlen >> 8 , IDE_HCYL_REG);
831: OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
832:
833: if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
834: ide_set_handler (drive, handler, WAIT_CMD);
835: OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
836: } else {
837: OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG); /* packet command */
838: (*handler) (drive);
839: }
840:
841: return 0;
842: }
843:
844:
845: /* Send a packet command to DRIVE described by CMD_BUF and CMD_LEN.
846: The device registers must have already been prepared
847: by cdrom_start_packet_command.
848: HANDLER is the interrupt handler to call when the command completes
849: or there's data ready. */
850: static int cdrom_transfer_packet_command (ide_drive_t *drive,
1.1.1.2 ! root 851: unsigned char *cmd_buf, int cmd_len,
1.1 root 852: ide_handler_t *handler)
853: {
854: if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
855: /* Here we should have been called after receiving an interrupt
856: from the device. DRQ should how be set. */
857: int stat_dum;
858:
859: /* Check for errors. */
860: if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum)) return 1;
861: } else {
862: /* Otherwise, we must wait for DRQ to get set. */
863: if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
864: return 1;
865: }
866:
867: /* Arm the interrupt handler. */
868: ide_set_handler (drive, handler, WAIT_CMD);
869:
870: /* Send the command to the device. */
871: cdrom_out_bytes (drive, cmd_buf, cmd_len);
872:
873: return 0;
874: }
875:
876:
877:
878: /****************************************************************************
879: * Block read functions.
880: */
881:
882: /*
883: * Buffer up to SECTORS_TO_TRANSFER sectors from the drive in our sector
884: * buffer. Once the first sector is added, any subsequent sectors are
885: * assumed to be continuous (until the buffer is cleared). For the first
886: * sector added, SECTOR is its sector number. (SECTOR is then ignored until
887: * the buffer is cleared.)
888: */
889: static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
890: int sectors_to_transfer)
891: {
892: struct cdrom_info *info = &drive->cdrom_info;
893:
894: /* Number of sectors to read into the buffer. */
895: int sectors_to_buffer = MIN (sectors_to_transfer,
896: (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
897: info->nsectors_buffered);
898:
899: char *dest;
900:
901: /* If we don't yet have a sector buffer, try to allocate one.
902: If we can't get one atomically, it's not fatal -- we'll just throw
903: the data away rather than caching it. */
904: if (info->sector_buffer == NULL) {
905: info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE,
906: GFP_ATOMIC);
907:
908: /* If we couldn't get a buffer,
909: don't try to buffer anything... */
910: if (info->sector_buffer == NULL)
911: sectors_to_buffer = 0;
912: }
913:
914: /* If this is the first sector in the buffer, remember its number. */
915: if (info->nsectors_buffered == 0)
916: info->sector_buffered = sector;
917:
918: /* Read the data into the buffer. */
919: dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
920: while (sectors_to_buffer > 0) {
921: cdrom_in_bytes (drive, dest, SECTOR_SIZE);
922: --sectors_to_buffer;
923: --sectors_to_transfer;
924: ++info->nsectors_buffered;
925: dest += SECTOR_SIZE;
926: }
927:
928: /* Throw away any remaining data. */
929: while (sectors_to_transfer > 0) {
930: char dum[SECTOR_SIZE];
931: cdrom_in_bytes (drive, dum, sizeof (dum));
932: --sectors_to_transfer;
933: }
934: }
935:
936:
937: /*
938: * Check the contents of the interrupt reason register from the cdrom
939: * and attempt to recover if there are problems. Returns 0 if everything's
940: * ok; nonzero if the request has been terminated.
941: */
942: static inline
943: int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
944: {
945: ireason &= 3;
946: if (ireason == 2) return 0;
947:
948: if (ireason == 0) {
949: /* Whoops... The drive is expecting to receive data from us! */
950: printk ("%s: cdrom_read_intr: "
951: "Drive wants to transfer data the wrong way!\n",
952: drive->name);
953:
954: /* Throw some data at the drive so it doesn't hang
955: and quit this request. */
956: while (len > 0) {
957: int dum = 0;
958: cdrom_out_bytes (drive, &dum, sizeof (dum));
959: len -= sizeof (dum);
960: }
961: } else {
962: /* Drive wants a command packet, or invalid ireason... */
963: printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
964: drive->name, ireason);
965: }
966:
967: cdrom_end_request (0, drive);
968: return -1;
969: }
970:
971:
972: /*
973: * Interrupt routine. Called when a read request has completed.
974: */
975: static void cdrom_read_intr (ide_drive_t *drive)
976: {
977: int stat;
978: int ireason, len, sectors_to_transfer, nskip;
979:
980: struct request *rq = HWGROUP(drive)->rq;
981:
982: /* Check for errors. */
983: if (cdrom_decode_status (drive, 0, &stat)) return;
984:
985: /* Read the interrupt reason and the transfer length. */
986: ireason = IN_BYTE (IDE_NSECTOR_REG);
987: len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
988:
989: /* If DRQ is clear, the command has completed. */
990: if ((stat & DRQ_STAT) == 0) {
991: /* If we're not done filling the current buffer, complain.
992: Otherwise, complete the command normally. */
993: if (rq->current_nr_sectors > 0) {
994: printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
995: drive->name, rq->current_nr_sectors);
996: cdrom_end_request (0, drive);
997: } else
998: cdrom_end_request (1, drive);
999:
1000: return;
1001: }
1002:
1003: /* Check that the drive is expecting to do the same thing we are. */
1004: if (cdrom_read_check_ireason (drive, len, ireason)) return;
1005:
1006: /* Assume that the drive will always provide data in multiples
1007: of at least SECTOR_SIZE, as it gets hairy to keep track
1008: of the transfers otherwise. */
1009: if ((len % SECTOR_SIZE) != 0) {
1010: printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
1011: drive->name, len);
1012: printk (" This drive is not supported by this version of the driver\n");
1013: cdrom_end_request (0, drive);
1014: return;
1015: }
1016:
1017: /* The number of sectors we need to read from the drive. */
1018: sectors_to_transfer = len / SECTOR_SIZE;
1019:
1020: /* First, figure out if we need to bit-bucket
1021: any of the leading sectors. */
1022: nskip = MIN ((int)(rq->current_nr_sectors -
1023: (rq->bh->b_size >> SECTOR_BITS)),
1024: sectors_to_transfer);
1025:
1026: while (nskip > 0) {
1027: /* We need to throw away a sector. */
1028: char dum[SECTOR_SIZE];
1029: cdrom_in_bytes (drive, dum, sizeof (dum));
1030:
1031: --rq->current_nr_sectors;
1032: --nskip;
1033: --sectors_to_transfer;
1034: }
1035:
1036: /* Now loop while we still have data to read from the drive. */
1037: while (sectors_to_transfer > 0) {
1038: int this_transfer;
1039:
1040: /* If we've filled the present buffer but there's another
1041: chained buffer after it, move on. */
1042: if (rq->current_nr_sectors == 0 &&
1043: rq->nr_sectors > 0)
1044: cdrom_end_request (1, drive);
1045:
1046: /* If the buffers are full, cache the rest of the data in our
1047: internal buffer. */
1048: if (rq->current_nr_sectors == 0) {
1049: cdrom_buffer_sectors (drive,
1050: rq->sector, sectors_to_transfer);
1051: sectors_to_transfer = 0;
1052: } else {
1053: /* Transfer data to the buffers.
1054: Figure out how many sectors we can transfer
1055: to the current buffer. */
1056: this_transfer = MIN (sectors_to_transfer,
1057: rq->current_nr_sectors);
1058:
1059: /* Read this_transfer sectors
1060: into the current buffer. */
1061: while (this_transfer > 0) {
1062: cdrom_in_bytes (drive
1063: , rq->buffer, SECTOR_SIZE);
1064: rq->buffer += SECTOR_SIZE;
1065: --rq->nr_sectors;
1066: --rq->current_nr_sectors;
1067: ++rq->sector;
1068: --this_transfer;
1069: --sectors_to_transfer;
1070: }
1071: }
1072: }
1073:
1074: /* Done moving data!
1075: Wait for another interrupt. */
1076: ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);
1077: }
1078:
1079:
1080: /*
1081: * Try to satisfy some of the current read request from our cached data.
1082: * Returns nonzero if the request has been completed, zero otherwise.
1083: */
1084: static int cdrom_read_from_buffer (ide_drive_t *drive)
1085: {
1086: struct cdrom_info *info = &drive->cdrom_info;
1087: struct request *rq = HWGROUP(drive)->rq;
1088:
1089: /* Can't do anything if there's no buffer. */
1090: if (info->sector_buffer == NULL) return 0;
1091:
1092: /* Loop while this request needs data and the next block is present
1093: in our cache. */
1094: while (rq->nr_sectors > 0 &&
1095: rq->sector >= info->sector_buffered &&
1096: rq->sector < info->sector_buffered + info->nsectors_buffered) {
1097: if (rq->current_nr_sectors == 0)
1098: cdrom_end_request (1, drive);
1099:
1100: memcpy (rq->buffer,
1101: info->sector_buffer +
1102: (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1103: SECTOR_SIZE);
1104: rq->buffer += SECTOR_SIZE;
1105: --rq->current_nr_sectors;
1106: --rq->nr_sectors;
1107: ++rq->sector;
1108: }
1109:
1110: /* If we've satisfied the current request,
1111: terminate it successfully. */
1112: if (rq->nr_sectors == 0) {
1113: cdrom_end_request (1, drive);
1114: return -1;
1115: }
1116:
1117: /* Move on to the next buffer if needed. */
1118: if (rq->current_nr_sectors == 0)
1119: cdrom_end_request (1, drive);
1120:
1121: /* If this condition does not hold, then the kluge i use to
1122: represent the number of sectors to skip at the start of a transfer
1123: will fail. I think that this will never happen, but let's be
1124: paranoid and check. */
1125: if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1126: (rq->sector % SECTORS_PER_FRAME) != 0) {
1127: printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1128: drive->name, rq->sector);
1129: cdrom_end_request (0, drive);
1130: return -1;
1131: }
1132:
1133: return 0;
1134: }
1135:
1136:
1137:
1138: /*
1139: * Routine to send a read packet command to the drive.
1140: * This is usually called directly from cdrom_start_read.
1141: * However, for drq_interrupt devices, it is called from an interrupt
1142: * when the drive is ready to accept the command.
1143: */
1144: static void cdrom_start_read_continuation (ide_drive_t *drive)
1145: {
1146: struct packet_command pc;
1147: struct request *rq = HWGROUP(drive)->rq;
1148:
1149: int nsect, sector, nframes, frame, nskip;
1150:
1151: /* Number of sectors to transfer. */
1152: nsect = rq->nr_sectors;
1153:
1154: #if !STANDARD_ATAPI
1155: if (nsect > drive->cdrom_info.max_sectors)
1156: nsect = drive->cdrom_info.max_sectors;
1157: #endif /* not STANDARD_ATAPI */
1158:
1159: /* Starting sector. */
1160: sector = rq->sector;
1161:
1162: /* If the requested sector doesn't start on a cdrom block boundary,
1163: we must adjust the start of the transfer so that it does,
1164: and remember to skip the first few sectors.
1165: If the CURRENT_NR_SECTORS field is larger than the size
1166: of the buffer, it will mean that we're to skip a number
1167: of sectors equal to the amount by which CURRENT_NR_SECTORS
1168: is larger than the buffer size. */
1169: nskip = (sector % SECTORS_PER_FRAME);
1170: if (nskip > 0) {
1171: /* Sanity check... */
1172: if (rq->current_nr_sectors !=
1173: (rq->bh->b_size >> SECTOR_BITS)) {
1174: printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1175: drive->name, rq->current_nr_sectors);
1176: cdrom_end_request (0, drive);
1177: return;
1178: }
1179:
1180: sector -= nskip;
1181: nsect += nskip;
1182: rq->current_nr_sectors += nskip;
1183: }
1184:
1185: /* Convert from sectors to cdrom blocks, rounding up the transfer
1186: length if needed. */
1187: nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1188: frame = sector / SECTORS_PER_FRAME;
1189:
1190: /* Largest number of frames was can transfer at once is 64k-1. */
1191: nframes = MIN (nframes, 65535);
1192:
1193: /* Set up the command */
1194: memset (&pc.c, 0, sizeof (pc.c));
1195: pc.c[0] = READ_10;
1196: pc.c[7] = (nframes >> 8);
1197: pc.c[8] = (nframes & 0xff);
1198: put_unaligned(htonl (frame), (unsigned int *) &pc.c[2]);
1199:
1200: /* Send the command to the drive and return. */
1201: (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c),
1202: &cdrom_read_intr);
1203: }
1204:
1205:
1206: /*
1207: * Start a read request from the CD-ROM.
1208: */
1209: static void cdrom_start_read (ide_drive_t *drive, unsigned int block)
1210: {
1211: struct request *rq = HWGROUP(drive)->rq;
1212: int minor = MINOR (rq->rq_dev);
1213:
1214: /* If the request is relative to a partition, fix it up to refer to the
1215: absolute address. */
1216: if ((minor & PARTN_MASK) != 0) {
1217: rq->sector = block;
1218: minor &= ~PARTN_MASK;
1219: rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1220: }
1221:
1222: /* We may be retrying this request after an error. Fix up
1223: any weirdness which might be present in the request packet. */
1224: restore_request (rq);
1225:
1226: /* Satisfy whatever we can of this request from our cached sector. */
1227: if (cdrom_read_from_buffer (drive))
1228: return;
1229:
1230: /* Clear the local sector buffer. */
1231: drive->cdrom_info.nsectors_buffered = 0;
1232:
1233: /* Start sending the read request to the drive. */
1234: cdrom_start_packet_command (drive, 32768,
1235: cdrom_start_read_continuation);
1236: }
1237:
1238:
1239:
1240:
1241: /****************************************************************************
1242: * Execute all other packet commands.
1243: */
1244:
1245: /* Forward declarations. */
1246: static int
1247: cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1248: struct atapi_request_sense *reqbuf);
1249:
1250:
1251:
1252: /* Interrupt routine for packet command completion. */
1253: static void cdrom_pc_intr (ide_drive_t *drive)
1254: {
1255: int ireason, len, stat, thislen;
1256: struct request *rq = HWGROUP(drive)->rq;
1257: struct packet_command *pc = (struct packet_command *)rq->buffer;
1258:
1259: /* Check for errors. */
1260: if (cdrom_decode_status (drive, 0, &stat)) return;
1261:
1262: /* Read the interrupt reason and the transfer length. */
1263: ireason = IN_BYTE (IDE_NSECTOR_REG);
1264: len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1265:
1266: /* If DRQ is clear, the command has completed.
1267: Complain if we still have data left to transfer. */
1268: if ((stat & DRQ_STAT) == 0) {
1269: /* Some of the trailing request sense fields are optional, and
1270: some drives don't send them. Sigh. */
1271: if (pc->c[0] == REQUEST_SENSE &&
1272: pc->buflen > 0 &&
1273: pc->buflen <= 5) {
1274: while (pc->buflen > 0) {
1275: *pc->buffer++ = 0;
1276: --pc->buflen;
1277: }
1278: }
1279:
1280: if (pc->buflen == 0)
1281: cdrom_end_request (1, drive);
1282: else {
1283: printk ("%s: cdrom_pc_intr: data underrun %d\n",
1284: drive->name, pc->buflen);
1285: pc->stat = 1;
1286: cdrom_end_request (1, drive);
1287: }
1288: return;
1289: }
1290:
1291: /* Figure out how much data to transfer. */
1292: thislen = pc->buflen;
1293: if (thislen < 0) thislen = -thislen;
1294: if (thislen > len) thislen = len;
1295:
1296: /* The drive wants to be written to. */
1297: if ((ireason & 3) == 0) {
1298: /* Check that we want to write. */
1299: if (pc->buflen > 0) {
1300: printk ("%s: cdrom_pc_intr: Drive wants "
1301: "to transfer data the wrong way!\n",
1302: drive->name);
1303: pc->stat = 1;
1304: thislen = 0;
1305: }
1306:
1307: /* Transfer the data. */
1308: cdrom_out_bytes (drive, pc->buffer, thislen);
1309:
1310: /* If we haven't moved enough data to satisfy the drive,
1311: add some padding. */
1312: while (len > thislen) {
1313: int dum = 0;
1314: cdrom_out_bytes (drive, &dum, sizeof (dum));
1315: len -= sizeof (dum);
1316: }
1317:
1318: /* Keep count of how much data we've moved. */
1319: pc->buffer += thislen;
1320: pc->buflen += thislen;
1321: }
1322:
1323: /* Same drill for reading. */
1324: else if ((ireason & 3) == 2) {
1325: /* Check that we want to read. */
1326: if (pc->buflen < 0) {
1327: printk ("%s: cdrom_pc_intr: Drive wants to "
1328: "transfer data the wrong way!\n",
1329: drive->name);
1330: pc->stat = 1;
1331: thislen = 0;
1332: }
1333:
1334: /* Transfer the data. */
1335: cdrom_in_bytes (drive, pc->buffer, thislen);
1336:
1337: /* If we haven't moved enough data to satisfy the drive,
1338: add some padding. */
1339: while (len > thislen) {
1340: int dum = 0;
1341: cdrom_in_bytes (drive, &dum, sizeof (dum));
1342: len -= sizeof (dum);
1343: }
1344:
1345: /* Keep count of how much data we've moved. */
1346: pc->buffer += thislen;
1347: pc->buflen -= thislen;
1348: } else {
1349: printk ("%s: cdrom_pc_intr: The drive "
1350: "appears confused (ireason = 0x%2x)\n",
1351: drive->name, ireason);
1352: pc->stat = 1;
1353: }
1354:
1355: /* Now we wait for another interrupt. */
1356: ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);
1357: }
1358:
1359:
1360: static void cdrom_do_pc_continuation (ide_drive_t *drive)
1361: {
1362: struct request *rq = HWGROUP(drive)->rq;
1363: struct packet_command *pc = (struct packet_command *)rq->buffer;
1364:
1365: /* Send the command to the drive and return. */
1366: cdrom_transfer_packet_command (drive, pc->c,
1367: sizeof (pc->c), &cdrom_pc_intr);
1368: }
1369:
1370:
1371: static void cdrom_do_packet_command (ide_drive_t *drive)
1372: {
1373: int len;
1374: struct request *rq = HWGROUP(drive)->rq;
1375: struct packet_command *pc = (struct packet_command *)rq->buffer;
1376:
1377: len = pc->buflen;
1378: if (len < 0) len = -len;
1379:
1380: pc->stat = 0;
1381:
1382: /* Start sending the command to the drive. */
1383: cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1384: }
1385:
1386: /* Sleep for TIME jiffies.
1387: Not to be called from an interrupt handler. */
1.1.1.2 ! root 1388: #ifdef MACH
! 1389: static
! 1390: void cdrom_sleep (int time)
! 1391: {
! 1392: int xxx;
! 1393:
! 1394: assert_wait ((event_t) &xxx, TRUE);
! 1395: thread_set_timeout (time);
! 1396: schedule ();
! 1397: }
! 1398: #else
1.1 root 1399: static
1400: void cdrom_sleep (int time)
1401: {
1402: current->state = TASK_INTERRUPTIBLE;
1403: current->timeout = jiffies + time;
1404: schedule ();
1405: }
1.1.1.2 ! root 1406: #endif
1.1 root 1407:
1408: static
1409: int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc)
1410: {
1411: struct atapi_request_sense my_reqbuf;
1412: int retries = 10;
1413: struct request req;
1414:
1415: /* If our caller has not provided a place to stick any sense data,
1416: use our own area. */
1417: if (pc->sense_data == NULL)
1418: pc->sense_data = &my_reqbuf;
1419: pc->sense_data->sense_key = 0;
1420:
1421: /* Start of retry loop. */
1422: do {
1423: ide_init_drive_cmd (&req);
1424: req.cmd = PACKET_COMMAND;
1425: req.buffer = (char *)pc;
1426: (void) ide_do_drive_cmd (drive, &req, ide_wait);
1427:
1428: if (pc->stat != 0) {
1429: /* The request failed. Retry if it was due to a unit
1430: attention status
1431: (usually means media was changed). */
1432: struct atapi_request_sense *reqbuf = pc->sense_data;
1433:
1434: if (reqbuf->sense_key == UNIT_ATTENTION)
1435: ;
1436: else if (reqbuf->sense_key == NOT_READY &&
1437: reqbuf->asc == 4) {
1438: /* The drive is in the process of loading
1439: a disk. Retry, but wait a little to give
1440: the drive time to complete the load. */
1441: cdrom_sleep (HZ);
1442: } else
1443: /* Otherwise, don't retry. */
1444: retries = 0;
1445:
1446: --retries;
1447: }
1448:
1449: /* End of retry loop. */
1450: } while (pc->stat != 0 && retries >= 0);
1451:
1452:
1453: /* Return an error if the command failed. */
1454: if (pc->stat != 0)
1455: return -EIO;
1456: else {
1457: /* The command succeeded. If it was anything other than
1458: a request sense, eject, or door lock command,
1459: and we think that the door is presently, lock it again.
1460: (The door was probably unlocked via an explicit
1461: CDROMEJECT ioctl.) */
1462: if (CDROM_STATE_FLAGS (drive)->door_locked == 0 &&
1463: (pc->c[0] != REQUEST_SENSE &&
1464: pc->c[0] != ALLOW_MEDIUM_REMOVAL &&
1465: pc->c[0] != START_STOP)) {
1466: (void) cdrom_lockdoor (drive, 1, NULL);
1467: }
1468: return 0;
1469: }
1470: }
1471:
1472:
1473: /****************************************************************************
1474: * cdrom driver request routine.
1475: */
1476:
1477: void ide_do_rw_cdrom (ide_drive_t *drive, unsigned long block)
1478: {
1479: struct request *rq = HWGROUP(drive)->rq;
1480:
1481: if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1482: cdrom_do_packet_command (drive);
1483: else if (rq -> cmd == RESET_DRIVE_COMMAND) {
1484: cdrom_end_request (1, drive);
1485: ide_do_reset (drive);
1486: return;
1487: } else if (rq -> cmd != READ) {
1488: printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1489: cdrom_end_request (0, drive);
1490: } else
1491: cdrom_start_read (drive, block);
1492: }
1493:
1494:
1495:
1496: /****************************************************************************
1497: * Ioctl handling.
1498: *
1499: * Routines which queue packet commands take as a final argument a pointer
1500: * to an atapi_request_sense struct. If execution of the command results
1501: * in an error with a CHECK CONDITION status, this structure will be filled
1502: * with the results of the subsequent request sense command. The pointer
1503: * can also be NULL, in which case no sense information is returned.
1504: */
1505:
1506: #if ! STANDARD_ATAPI
1507: static inline
1508: int bin2bcd (int x)
1509: {
1510: return (x%10) | ((x/10) << 4);
1511: }
1512:
1513:
1514: static inline
1515: int bcd2bin (int x)
1516: {
1517: return (x >> 4) * 10 + (x & 0x0f);
1518: }
1519:
1520: static
1521: void msf_from_bcd (struct atapi_msf *msf)
1522: {
1523: msf->minute = bcd2bin (msf->minute);
1524: msf->second = bcd2bin (msf->second);
1525: msf->frame = bcd2bin (msf->frame);
1526: }
1527:
1528: #endif /* not STANDARD_ATAPI */
1529:
1530:
1531: static inline
1532: void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1533: {
1534: lba += CD_BLOCK_OFFSET;
1535: lba &= 0xffffff; /* negative lbas use only 24 bits */
1536: *m = lba / (CD_SECS * CD_FRAMES);
1537: lba %= (CD_SECS * CD_FRAMES);
1538: *s = lba / CD_FRAMES;
1539: *f = lba % CD_FRAMES;
1540: }
1541:
1542:
1543: static inline
1544: int msf_to_lba (byte m, byte s, byte f)
1545: {
1546: return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
1547: }
1548:
1549:
1550: static int
1551: cdrom_check_status (ide_drive_t *drive,
1552: struct atapi_request_sense *reqbuf)
1553: {
1554: struct packet_command pc;
1555:
1556: memset (&pc, 0, sizeof (pc));
1557:
1558: pc.sense_data = reqbuf;
1559: pc.c[0] = TEST_UNIT_READY;
1560:
1561: /* the Sanyo 3 CD changer uses byte 7 of TEST_UNIT_READY to
1562: switch CDs instead of supporting the LOAD_UNLOAD opcode */
1563:
1564: pc.c[7] = CDROM_STATE_FLAGS (drive)->sanyo_slot % 3;
1565:
1566: return cdrom_queue_packet_command (drive, &pc);
1567: }
1568:
1569:
1570: /* Lock the door if LOCKFLAG is nonzero; unlock it otherwise. */
1571: static int
1572: cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1573: struct atapi_request_sense *reqbuf)
1574: {
1575: struct atapi_request_sense my_reqbuf;
1576: int stat;
1577: struct packet_command pc;
1578:
1579: if (reqbuf == NULL)
1580: reqbuf = &my_reqbuf;
1581:
1582: /* If the drive cannot lock the door, just pretend. */
1583: if (CDROM_CONFIG_FLAGS (drive)->no_doorlock)
1584: stat = 0;
1585: else {
1586: memset (&pc, 0, sizeof (pc));
1587: pc.sense_data = reqbuf;
1588:
1589: pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1590: pc.c[4] = (lockflag != 0);
1591: stat = cdrom_queue_packet_command (drive, &pc);
1592: }
1593:
1594: if (stat == 0)
1595: CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1596: else {
1597: /* If we got an illegal field error, the drive
1598: probably cannot lock the door. */
1599: if (reqbuf->sense_key == ILLEGAL_REQUEST &&
1600: (reqbuf->asc == 0x24 || reqbuf->asc == 0x20)) {
1601: printk ("%s: door locking not supported\n",
1602: drive->name);
1603: CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1604: stat = 0;
1605: CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1606: }
1607: }
1608: return stat;
1609: }
1610:
1611:
1612: /* Eject the disk if EJECTFLAG is 0.
1613: If EJECTFLAG is 1, try to reload the disk. */
1614: static int
1615: cdrom_eject (ide_drive_t *drive, int ejectflag,
1616: struct atapi_request_sense *reqbuf)
1617: {
1618: struct packet_command pc;
1619:
1620: memset (&pc, 0, sizeof (pc));
1621: pc.sense_data = reqbuf;
1622:
1623: pc.c[0] = START_STOP;
1624: pc.c[4] = 2 + (ejectflag != 0);
1625: return cdrom_queue_packet_command (drive, &pc);
1626: }
1627:
1628:
1629: static int
1630: cdrom_pause (ide_drive_t *drive, int pauseflag,
1631: struct atapi_request_sense *reqbuf)
1632: {
1633: struct packet_command pc;
1634:
1635: memset (&pc, 0, sizeof (pc));
1636: pc.sense_data = reqbuf;
1637:
1638: pc.c[0] = SCMD_PAUSE_RESUME;
1639: pc.c[8] = !pauseflag;
1640: return cdrom_queue_packet_command (drive, &pc);
1641: }
1642:
1643:
1644: static int
1645: cdrom_startstop (ide_drive_t *drive, int startflag,
1646: struct atapi_request_sense *reqbuf)
1647: {
1648: struct packet_command pc;
1649:
1650: memset (&pc, 0, sizeof (pc));
1651: pc.sense_data = reqbuf;
1652:
1653: pc.c[0] = START_STOP;
1654: pc.c[1] = 1;
1655: pc.c[4] = startflag;
1656: return cdrom_queue_packet_command (drive, &pc);
1657: }
1658:
1659:
1660: static int
1661: cdrom_read_capacity (ide_drive_t *drive, unsigned *capacity,
1662: struct atapi_request_sense *reqbuf)
1663: {
1664: struct {
1665: unsigned lba;
1666: unsigned blocklen;
1667: } capbuf;
1668:
1669: int stat;
1670: struct packet_command pc;
1671:
1672: memset (&pc, 0, sizeof (pc));
1673: pc.sense_data = reqbuf;
1674:
1675: pc.c[0] = READ_CAPACITY;
1.1.1.2 ! root 1676: pc.buffer = (unsigned char *)&capbuf;
1.1 root 1677: pc.buflen = sizeof (capbuf);
1678:
1679: stat = cdrom_queue_packet_command (drive, &pc);
1680: if (stat == 0)
1681: *capacity = ntohl (capbuf.lba);
1682:
1683: return stat;
1684: }
1685:
1686:
1687: static int
1688: cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
1689: int format, char *buf, int buflen,
1690: struct atapi_request_sense *reqbuf)
1691: {
1692: struct packet_command pc;
1693:
1694: memset (&pc, 0, sizeof (pc));
1695: pc.sense_data = reqbuf;
1696:
1.1.1.2 ! root 1697: pc.buffer = (unsigned char *)buf;
1.1 root 1698: pc.buflen = buflen;
1699: pc.c[0] = SCMD_READ_TOC;
1700: pc.c[6] = trackno;
1701: pc.c[7] = (buflen >> 8);
1702: pc.c[8] = (buflen & 0xff);
1703: pc.c[9] = (format << 6);
1704: if (msf_flag) pc.c[1] = 2;
1705: return cdrom_queue_packet_command (drive, &pc);
1706: }
1707:
1708:
1709: /* Try to read the entire TOC for the disk into our internal buffer. */
1710: static int
1711: cdrom_read_toc (ide_drive_t *drive,
1712: struct atapi_request_sense *reqbuf)
1713: {
1714: int stat, ntracks, i;
1715: struct atapi_toc *toc = drive->cdrom_info.toc;
1716: struct {
1717: struct atapi_toc_header hdr;
1718: struct atapi_toc_entry ent;
1719: } ms_tmp;
1720:
1721: if (toc == NULL) {
1722: /* Try to allocate space. */
1723: toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1724: GFP_KERNEL);
1725: drive->cdrom_info.toc = toc;
1726: }
1727:
1728: if (toc == NULL) {
1729: printk ("%s: No cdrom TOC buffer!\n", drive->name);
1730: return -EIO;
1731: }
1732:
1733: /* Check to see if the existing data is still valid.
1734: If it is, just return. */
1735: if (CDROM_STATE_FLAGS (drive)->toc_valid)
1736: (void) cdrom_check_status (drive, NULL);
1737:
1738: if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1739:
1740: /* First read just the header, so we know how long the TOC is. */
1741: stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1742: sizeof (struct atapi_toc_header) +
1743: sizeof (struct atapi_toc_entry),
1744: reqbuf);
1745: if (stat) return stat;
1746:
1747: #if ! STANDARD_ATAPI
1748: if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1749: toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1750: toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1751: }
1752: #endif /* not STANDARD_ATAPI */
1753:
1754: ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1755: if (ntracks <= 0) return -EIO;
1756: if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1757:
1758: /* Now read the whole schmeer. */
1759: stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1760: sizeof (struct atapi_toc_header) +
1761: (ntracks+1) *
1762: sizeof (struct atapi_toc_entry),
1763: reqbuf);
1764: if (stat) return stat;
1765: toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1766:
1767: #if ! STANDARD_ATAPI
1768: if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1769: toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1770: toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1771: }
1772: #endif /* not STANDARD_ATAPI */
1773:
1774: for (i=0; i<=ntracks; i++) {
1775: #if ! STANDARD_ATAPI
1776: if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1777: if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1778: toc->ent[i].track = bcd2bin (toc->ent[i].track);
1779: msf_from_bcd (&toc->ent[i].addr.msf);
1780: }
1781: #endif /* not STANDARD_ATAPI */
1782: toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1783: toc->ent[i].addr.msf.second,
1784: toc->ent[i].addr.msf.frame);
1785: }
1786:
1787: /* Read the multisession information. */
1788: stat = cdrom_read_tocentry (drive, 0, 1, 1,
1789: (char *)&ms_tmp, sizeof (ms_tmp),
1790: reqbuf);
1791: if (stat) return stat;
1792:
1793: #if ! STANDARD_ATAPI
1794: if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1795: msf_from_bcd (&ms_tmp.ent.addr.msf);
1796: #endif /* not STANDARD_ATAPI */
1797:
1798: toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1799: ms_tmp.ent.addr.msf.second,
1800: ms_tmp.ent.addr.msf.frame);
1801:
1802: toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1803:
1804: /* Now try to get the total cdrom capacity. */
1805: stat = cdrom_read_capacity (drive, &toc->capacity, reqbuf);
1806: if (stat) toc->capacity = 0x1fffff;
1807:
1808: HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS]
1809: = toc->capacity * SECTORS_PER_FRAME;
1810: drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1811:
1812: /* Remember that we've read this stuff. */
1813: CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1814:
1815: return 0;
1816: }
1817:
1818:
1819: static int
1820: cdrom_read_subchannel (ide_drive_t *drive, int format,
1821: char *buf, int buflen,
1822: struct atapi_request_sense *reqbuf)
1823: {
1824: struct packet_command pc;
1825:
1826: memset (&pc, 0, sizeof (pc));
1827: pc.sense_data = reqbuf;
1828:
1.1.1.2 ! root 1829: pc.buffer = (unsigned char *) buf;
1.1 root 1830: pc.buflen = buflen;
1831: pc.c[0] = SCMD_READ_SUBCHANNEL;
1832: pc.c[1] = 2; /* MSF addressing */
1833: pc.c[2] = 0x40; /* request subQ data */
1834: pc.c[3] = format,
1835: pc.c[7] = (buflen >> 8);
1836: pc.c[8] = (buflen & 0xff);
1837: return cdrom_queue_packet_command (drive, &pc);
1838: }
1839:
1840:
1841: /* modeflag: 0 = current, 1 = changeable mask, 2 = default, 3 = saved */
1842: static int
1843: cdrom_mode_sense (ide_drive_t *drive, int pageno, int modeflag,
1844: char *buf, int buflen,
1845: struct atapi_request_sense *reqbuf)
1846: {
1847: struct packet_command pc;
1848:
1849: memset (&pc, 0, sizeof (pc));
1850: pc.sense_data = reqbuf;
1851:
1.1.1.2 ! root 1852: pc.buffer = (unsigned char *)buf;
1.1 root 1853: pc.buflen = buflen;
1854: pc.c[0] = MODE_SENSE_10;
1855: pc.c[2] = pageno | (modeflag << 6);
1856: pc.c[7] = (buflen >> 8);
1857: pc.c[8] = (buflen & 0xff);
1858: return cdrom_queue_packet_command (drive, &pc);
1859: }
1860:
1861:
1862: static int
1863: cdrom_mode_select (ide_drive_t *drive, int pageno, char *buf, int buflen,
1864: struct atapi_request_sense *reqbuf)
1865: {
1866: struct packet_command pc;
1867:
1868: memset (&pc, 0, sizeof (pc));
1869: pc.sense_data = reqbuf;
1870:
1.1.1.2 ! root 1871: pc.buffer = (unsigned char *)buf;
1.1 root 1872: pc.buflen = - buflen;
1873: pc.c[0] = MODE_SELECT_10;
1874: pc.c[1] = 0x10;
1875: pc.c[2] = pageno;
1876: pc.c[7] = (buflen >> 8);
1877: pc.c[8] = (buflen & 0xff);
1878: return cdrom_queue_packet_command (drive, &pc);
1879: }
1880:
1881:
1882: static int
1883: cdrom_play_lba_range_1 (ide_drive_t *drive, int lba_start, int lba_end,
1884: struct atapi_request_sense *reqbuf)
1885: {
1886: struct packet_command pc;
1887:
1888: memset (&pc, 0, sizeof (pc));
1889: pc.sense_data = reqbuf;
1890:
1891: pc.c[0] = SCMD_PLAYAUDIO_MSF;
1892: lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1893: lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1894:
1895: #if ! STANDARD_ATAPI
1896: if (CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd) {
1897: pc.c[3] = bin2bcd (pc.c[3]);
1898: pc.c[4] = bin2bcd (pc.c[4]);
1899: pc.c[5] = bin2bcd (pc.c[5]);
1900: pc.c[6] = bin2bcd (pc.c[6]);
1901: pc.c[7] = bin2bcd (pc.c[7]);
1902: pc.c[8] = bin2bcd (pc.c[8]);
1903: }
1904: #endif /* not STANDARD_ATAPI */
1905:
1906: return cdrom_queue_packet_command (drive, &pc);
1907: }
1908:
1909:
1910: /* Play audio starting at LBA LBA_START and finishing with the
1911: LBA before LBA_END. */
1912: static int
1913: cdrom_play_lba_range (ide_drive_t *drive, int lba_start, int lba_end,
1914: struct atapi_request_sense *reqbuf)
1915: {
1916: int i, stat;
1917: struct atapi_request_sense my_reqbuf;
1918:
1919: if (reqbuf == NULL)
1920: reqbuf = &my_reqbuf;
1921:
1922: /* Some drives, will, for certain audio cds,
1923: give an error if you ask them to play the entire cd using the
1924: values which are returned in the TOC. The play will succeed,
1925: however, if the ending address is adjusted downwards
1926: by a few frames. */
1927: for (i=0; i<75; i++) {
1928: stat = cdrom_play_lba_range_1 (drive, lba_start, lba_end,
1929: reqbuf);
1930:
1931: if (stat == 0 ||
1932: !(reqbuf->sense_key == ILLEGAL_REQUEST &&
1933: reqbuf->asc == 0x24))
1934: return stat;
1935:
1936: --lba_end;
1937: if (lba_end <= lba_start) break;
1938: }
1939:
1940: return stat;
1941: }
1942:
1943:
1944: static
1945: int cdrom_get_toc_entry (ide_drive_t *drive, int track,
1946: struct atapi_toc_entry **ent,
1947: struct atapi_request_sense *reqbuf)
1948: {
1949: int stat, ntracks;
1950: struct atapi_toc *toc;
1951:
1952: /* Make sure our saved TOC is valid. */
1953: stat = cdrom_read_toc (drive, reqbuf);
1954: if (stat) return stat;
1955:
1956: toc = drive->cdrom_info.toc;
1957:
1958: /* Check validity of requested track number. */
1959: ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1960: if (track == CDROM_LEADOUT)
1961: *ent = &toc->ent[ntracks];
1962: else if (track < toc->hdr.first_track ||
1963: track > toc->hdr.last_track)
1964: return -EINVAL;
1965: else
1966: *ent = &toc->ent[track - toc->hdr.first_track];
1967:
1968: return 0;
1969: }
1970:
1971:
1972: static int
1973: cdrom_read_block (ide_drive_t *drive, int format, int lba, int nblocks,
1974: char *buf, int buflen,
1975: struct atapi_request_sense *reqbuf)
1976: {
1977: struct packet_command pc;
1978: struct atapi_request_sense my_reqbuf;
1979: int stat;
1980:
1981: if (reqbuf == NULL)
1982: reqbuf = &my_reqbuf;
1983:
1984: memset (&pc, 0, sizeof (pc));
1985: pc.sense_data = reqbuf;
1986:
1.1.1.2 ! root 1987: pc.buffer = (unsigned char *)buf;
1.1 root 1988: pc.buflen = buflen;
1989:
1990: #if ! STANDARD_ATAPI
1991: if (CDROM_CONFIG_FLAGS (drive)->old_readcd)
1992: pc.c[0] = 0xd4;
1993: else
1994: #endif /* not STANDARD_ATAPI */
1995: pc.c[0] = READ_CD;
1996:
1997: pc.c[1] = (format << 2);
1998: put_unaligned(htonl(lba), (unsigned int *) &pc.c[2]);
1999: pc.c[8] = (nblocks & 0xff);
2000: pc.c[7] = ((nblocks>>8) & 0xff);
2001: pc.c[6] = ((nblocks>>16) & 0xff);
2002: if (format <= 1)
2003: pc.c[9] = 0xf8;
2004: else
2005: pc.c[9] = 0x10;
2006:
2007: stat = cdrom_queue_packet_command (drive, &pc);
2008:
2009: #if ! STANDARD_ATAPI
2010: /* If the drive doesn't recognize the READ CD opcode, retry the command
2011: with an older opcode for that command. */
2012: if (stat && reqbuf->sense_key == ILLEGAL_REQUEST &&
2013: reqbuf->asc == 0x20 &&
2014: CDROM_CONFIG_FLAGS (drive)->old_readcd == 0) {
2015: printk ("%s: Drive does not recognize READ_CD;"
2016: "trying opcode 0xd4\n",
2017: drive->name);
2018: CDROM_CONFIG_FLAGS (drive)->old_readcd = 1;
2019: return cdrom_read_block (drive, format, lba, nblocks,
2020: buf, buflen, reqbuf);
2021: }
2022: #endif /* not STANDARD_ATAPI */
2023:
2024: return stat;
2025: }
2026:
2027:
2028: /* If SLOT<0, unload the current slot. Otherwise, try to load SLOT. */
2029: static int
2030: cdrom_load_unload (ide_drive_t *drive, int slot,
2031: struct atapi_request_sense *reqbuf)
2032: {
2033: /* if the drive is a Sanyo 3 CD changer then TEST_UNIT_READY
2034: (used in the cdrom_check_status function) is used to
2035: switch CDs instead of LOAD_UNLOAD */
2036:
2037: if (CDROM_STATE_FLAGS (drive)->sanyo_slot > 0) {
2038:
2039: if ((slot == 1) || (slot == 2)) {
2040: CDROM_STATE_FLAGS (drive)->sanyo_slot = slot;
2041: } else if (slot >= 0) {
2042: CDROM_STATE_FLAGS (drive)->sanyo_slot = 3;
2043: } else {
2044: return 0;
2045: }
2046:
2047: return cdrom_check_status (drive, NULL);
2048:
2049: } else {
2050:
2051: /* ATAPI Rev. 2.2+ standard for requesting switching of
2052: CDs in a multiplatter device */
2053:
2054: struct packet_command pc;
2055:
2056: memset (&pc, 0, sizeof (pc));
2057: pc.sense_data = reqbuf;
2058:
2059: pc.c[0] = LOAD_UNLOAD;
2060: pc.c[4] = 2 + (slot >= 0);
2061: pc.c[8] = slot;
2062: return cdrom_queue_packet_command (drive, &pc);
2063:
2064: }
2065: }
2066:
2067:
2068: int ide_cdrom_ioctl (ide_drive_t *drive, struct inode *inode,
2069: struct file *file, unsigned int cmd, unsigned long arg)
2070: {
2071: switch (cmd) {
2072: case CDROMEJECT: {
2073: int stat;
2074:
2075: if (drive->usage > 1)
2076: return -EBUSY;
2077:
2078: stat = cdrom_lockdoor (drive, 0, NULL);
2079: if (stat) return stat;
2080:
2081: return cdrom_eject (drive, 0, NULL);
2082: }
2083:
2084: case CDROMCLOSETRAY: {
2085: int stat;
2086: if (drive->usage > 1)
2087: return -EBUSY;
2088:
2089: stat = cdrom_eject (drive, 1, NULL);
2090: if (stat) return stat;
2091:
2092: return cdrom_lockdoor (drive, 1, NULL);
2093: }
2094:
2095: case CDROMEJECT_SW: {
2096: CDROM_STATE_FLAGS (drive)->eject_on_close = arg;
2097: return 0;
2098: }
2099:
2100: case CDROMPAUSE:
2101: return cdrom_pause (drive, 1, NULL);
2102:
2103: case CDROMRESUME:
2104: return cdrom_pause (drive, 0, NULL);
2105:
2106: case CDROMSTART:
2107: return cdrom_startstop (drive, 1, NULL);
2108:
2109: case CDROMSTOP: {
2110: #ifdef IHAVEADOLPHIN
2111: /* Certain Drives require this. Most don't
2112: and will produce errors upon CDROMSTOP
2113: pit says the Dolphin needs this. If you
2114: own a dolphin, just define IHAVEADOLPHIN somewhere */
2115: int stat;
2116: stat = cdrom_startstop (drive, 0, NULL);
2117: if (stat) return stat;
2118: return cdrom_eject (drive, 1, NULL);
2119: #endif /* end of IHAVEADOLPHIN */
2120: return cdrom_startstop (drive, 0, NULL);
2121: }
2122:
2123: case CDROMPLAYMSF: {
2124: struct cdrom_msf msf;
2125: int stat, lba_start, lba_end;
2126:
2127: stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
2128: if (stat) return stat;
2129:
2130: memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
2131:
2132: lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
2133: msf.cdmsf_frame0);
2134: lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
2135: msf.cdmsf_frame1) + 1;
2136:
2137: if (lba_end <= lba_start) return -EINVAL;
2138:
2139: return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2140: }
2141:
2142: /* Like just about every other Linux cdrom driver, we ignore the
2143: index part of the request here. */
2144: case CDROMPLAYTRKIND: {
2145: int stat, lba_start, lba_end;
2146: struct cdrom_ti ti;
2147: struct atapi_toc_entry *first_toc, *last_toc;
2148:
2149: stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
2150: if (stat) return stat;
2151:
2152: memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
2153:
2154: stat = cdrom_get_toc_entry (drive, ti.cdti_trk0, &first_toc,
2155: NULL);
2156: if (stat) return stat;
2157: stat = cdrom_get_toc_entry (drive, ti.cdti_trk1, &last_toc,
2158: NULL);
2159: if (stat) return stat;
2160:
2161: if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
2162: lba_start = first_toc->addr.lba;
2163: lba_end = last_toc->addr.lba;
2164:
2165: if (lba_end <= lba_start) return -EINVAL;
2166:
2167: return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2168: }
2169:
2170: case CDROMREADTOCHDR: {
2171: int stat;
2172: struct cdrom_tochdr tochdr;
2173: struct atapi_toc *toc;
2174:
2175: stat = verify_area (VERIFY_WRITE, (void *) arg,
2176: sizeof (tochdr));
2177: if (stat) return stat;
2178:
2179: /* Make sure our saved TOC is valid. */
2180: stat = cdrom_read_toc (drive, NULL);
2181: if (stat) return stat;
2182:
2183: toc = drive->cdrom_info.toc;
2184: tochdr.cdth_trk0 = toc->hdr.first_track;
2185: tochdr.cdth_trk1 = toc->hdr.last_track;
2186:
2187: memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
2188:
2189: return stat;
2190: }
2191:
2192: case CDROMREADTOCENTRY: {
2193: int stat;
2194: struct cdrom_tocentry tocentry;
2195: struct atapi_toc_entry *toce;
2196:
2197: stat = verify_area (VERIFY_WRITE, (void *) arg,
2198: sizeof (tocentry));
2199: if (stat) return stat;
2200:
2201: memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
2202:
2203: stat = cdrom_get_toc_entry (drive, tocentry.cdte_track, &toce,
2204: NULL);
2205: if (stat) return stat;
2206:
2207: tocentry.cdte_ctrl = toce->control;
2208: tocentry.cdte_adr = toce->adr;
2209:
2210: if (tocentry.cdte_format == CDROM_MSF) {
2211: /* convert to MSF */
2212: lba_to_msf (toce->addr.lba,
2213: &tocentry.cdte_addr.msf.minute,
2214: &tocentry.cdte_addr.msf.second,
2215: &tocentry.cdte_addr.msf.frame);
2216: } else
2217: tocentry.cdte_addr.lba = toce->addr.lba;
2218:
2219: memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
2220:
2221: return stat;
2222: }
2223:
2224: case CDROMSUBCHNL: {
2225: struct atapi_cdrom_subchnl scbuf;
2226: int stat;
2227: struct cdrom_subchnl subchnl;
2228:
2229: stat = verify_area (VERIFY_WRITE, (void *) arg,
2230: sizeof (subchnl));
2231: if (stat) return stat;
2232:
2233: memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
2234:
2235: stat = cdrom_read_subchannel (drive, 1, /* current position */
2236: (char *)&scbuf, sizeof (scbuf),
2237: NULL);
2238: if (stat) return stat;
2239:
2240: #if ! STANDARD_ATAPI
2241: if (CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd) {
2242: msf_from_bcd (&scbuf.acdsc_absaddr.msf);
2243: msf_from_bcd (&scbuf.acdsc_reladdr.msf);
2244: }
2245: if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
2246: scbuf.acdsc_trk = bcd2bin (scbuf.acdsc_trk);
2247: #endif /* not STANDARD_ATAPI */
2248:
2249: if (subchnl.cdsc_format == CDROM_MSF) {
2250: subchnl.cdsc_absaddr.msf.minute =
2251: scbuf.acdsc_absaddr.msf.minute;
2252: subchnl.cdsc_absaddr.msf.second =
2253: scbuf.acdsc_absaddr.msf.second;
2254: subchnl.cdsc_absaddr.msf.frame =
2255: scbuf.acdsc_absaddr.msf.frame;
2256:
2257: subchnl.cdsc_reladdr.msf.minute =
2258: scbuf.acdsc_reladdr.msf.minute;
2259: subchnl.cdsc_reladdr.msf.second =
2260: scbuf.acdsc_reladdr.msf.second;
2261: subchnl.cdsc_reladdr.msf.frame =
2262: scbuf.acdsc_reladdr.msf.frame;
2263: } else {
2264: subchnl.cdsc_absaddr.lba =
2265: msf_to_lba (scbuf.acdsc_absaddr.msf.minute,
2266: scbuf.acdsc_absaddr.msf.second,
2267: scbuf.acdsc_absaddr.msf.frame);
2268: subchnl.cdsc_reladdr.lba =
2269: msf_to_lba (scbuf.acdsc_reladdr.msf.minute,
2270: scbuf.acdsc_reladdr.msf.second,
2271: scbuf.acdsc_reladdr.msf.frame);
2272: }
2273:
2274: subchnl.cdsc_audiostatus = scbuf.acdsc_audiostatus;
2275: subchnl.cdsc_ctrl = scbuf.acdsc_ctrl;
2276: subchnl.cdsc_trk = scbuf.acdsc_trk;
2277: subchnl.cdsc_ind = scbuf.acdsc_ind;
2278:
2279: memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
2280:
2281: return stat;
2282: }
2283:
2284: case CDROMVOLCTRL: {
2285: struct cdrom_volctrl volctrl;
2286: char buffer[24], mask[24];
2287: int stat;
2288:
2289: stat = verify_area (VERIFY_READ, (void *) arg,
2290: sizeof (volctrl));
2291: if (stat) return stat;
2292: memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
2293:
2294: stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2295: sizeof (buffer), NULL);
2296: if (stat) return stat;
2297: stat = cdrom_mode_sense (drive, 0x0e, 1, mask,
2298: sizeof (buffer), NULL);
2299: if (stat) return stat;
2300:
2301: buffer[1] = buffer[2] = 0;
2302:
2303: buffer[17] = volctrl.channel0 & mask[17];
2304: buffer[19] = volctrl.channel1 & mask[19];
2305: buffer[21] = volctrl.channel2 & mask[21];
2306: buffer[23] = volctrl.channel3 & mask[23];
2307:
2308: return cdrom_mode_select (drive, 0x0e, buffer,
2309: sizeof (buffer), NULL);
2310: }
2311:
2312: case CDROMVOLREAD: {
2313: struct cdrom_volctrl volctrl;
2314: char buffer[24];
2315: int stat;
2316:
2317: stat = verify_area (VERIFY_WRITE, (void *) arg,
2318: sizeof (volctrl));
2319: if (stat) return stat;
2320:
2321: stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2322: sizeof (buffer), NULL);
2323: if (stat) return stat;
2324:
2325: volctrl.channel0 = buffer[17];
2326: volctrl.channel1 = buffer[19];
2327: volctrl.channel2 = buffer[21];
2328: volctrl.channel3 = buffer[23];
2329:
2330: memcpy_tofs ((void *) arg, &volctrl, sizeof (volctrl));
2331:
2332: return 0;
2333: }
2334:
2335: case CDROMMULTISESSION: {
2336: struct cdrom_multisession ms_info;
2337: struct atapi_toc *toc;
2338: int stat;
2339:
2340: stat = verify_area (VERIFY_WRITE, (void *)arg,
2341: sizeof (ms_info));
2342: if (stat) return stat;
2343:
2344: memcpy_fromfs (&ms_info, (void *)arg, sizeof (ms_info));
2345:
2346: /* Make sure the TOC information is valid. */
2347: stat = cdrom_read_toc (drive, NULL);
2348: if (stat) return stat;
2349:
2350: toc = drive->cdrom_info.toc;
2351:
2352: if (ms_info.addr_format == CDROM_MSF)
2353: lba_to_msf (toc->last_session_lba,
2354: &ms_info.addr.msf.minute,
2355: &ms_info.addr.msf.second,
2356: &ms_info.addr.msf.frame);
2357: else if (ms_info.addr_format == CDROM_LBA)
2358: ms_info.addr.lba = toc->last_session_lba;
2359: else
2360: return -EINVAL;
2361:
2362: ms_info.xa_flag = toc->xa_flag;
2363:
2364: memcpy_tofs ((void *)arg, &ms_info, sizeof (ms_info));
2365:
2366: return 0;
2367: }
2368:
2369: /* Read 2352 byte blocks from audio tracks. */
2370: case CDROMREADAUDIO: {
2371: int stat, lba;
2372: struct atapi_toc *toc;
2373: struct cdrom_read_audio ra;
2374: char *buf;
2375:
2376: /* Make sure the TOC is up to date. */
2377: stat = cdrom_read_toc (drive, NULL);
2378: if (stat) return stat;
2379:
2380: toc = drive->cdrom_info.toc;
2381:
2382: stat = verify_area (VERIFY_READ, (char *)arg, sizeof (ra));
2383: if (stat) return stat;
2384:
2385: memcpy_fromfs (&ra, (void *)arg, sizeof (ra));
2386:
2387: if (ra.nframes < 0 || ra.nframes > toc->capacity)
2388: return -EINVAL;
2389: else if (ra.nframes == 0)
2390: return 0;
2391:
2392: stat = verify_area (VERIFY_WRITE, (char *)ra.buf,
2393: ra.nframes * CD_FRAMESIZE_RAW);
2394: if (stat) return stat;
2395:
2396: if (ra.addr_format == CDROM_MSF)
2397: lba = msf_to_lba (ra.addr.msf.minute,
2398: ra.addr.msf.second,
2399: ra.addr.msf.frame);
2400: else if (ra.addr_format == CDROM_LBA)
2401: lba = ra.addr.lba;
2402: else
2403: return -EINVAL;
2404:
2405: if (lba < 0 || lba >= toc->capacity)
2406: return -EINVAL;
2407:
2408: buf = (char *) kmalloc (CDROM_NBLOCKS_BUFFER*CD_FRAMESIZE_RAW,
2409: GFP_KERNEL);
2410: if (buf == NULL)
2411: return -ENOMEM;
2412:
2413: while (ra.nframes > 0) {
2414: int this_nblocks = ra.nframes;
2415: if (this_nblocks > CDROM_NBLOCKS_BUFFER)
2416: this_nblocks = CDROM_NBLOCKS_BUFFER;
2417: stat = cdrom_read_block
2418: (drive, 1, lba, this_nblocks,
2419: buf, this_nblocks * CD_FRAMESIZE_RAW, NULL);
2420: if (stat) break;
2421:
2422: memcpy_tofs (ra.buf, buf,
2423: this_nblocks * CD_FRAMESIZE_RAW);
2424: ra.buf += this_nblocks * CD_FRAMESIZE_RAW;
2425: ra.nframes -= this_nblocks;
2426: lba += this_nblocks;
2427: }
2428:
2429: kfree (buf);
2430: return stat;
2431: }
2432: case CDROMREADRAW:
2433: case CDROMREADMODE1:
2434: case CDROMREADMODE2: {
2435: struct cdrom_msf msf;
2436: int blocksize, format, stat, lba;
2437: char *buf;
2438:
2439: if (cmd == CDROMREADMODE1) {
2440: blocksize = CD_FRAMESIZE;
2441: format = 2;
2442: } else if (cmd == CDROMREADMODE2) {
2443: blocksize = CD_FRAMESIZE_RAW0;
2444: format = 3;
2445: } else {
2446: blocksize = CD_FRAMESIZE_RAW;
2447: format = 0;
2448: }
2449:
2450: stat = verify_area (VERIFY_WRITE, (char *)arg, blocksize);
2451: if (stat) return stat;
2452:
2453: memcpy_fromfs (&msf, (void *)arg, sizeof (msf));
2454:
2455: lba = msf_to_lba (msf.cdmsf_min0,
2456: msf.cdmsf_sec0,
2457: msf.cdmsf_frame0);
2458:
2459: /* DON'T make sure the TOC is up to date. */
2460: /* stat = cdrom_read_toc (drive, NULL);
2461: if (stat) return stat;
2462:
2463: toc = drive->cdrom_info.toc;
2464:
2465: if (lba < 0 || lba >= toc->capacity)
2466: return -EINVAL; */
2467:
2468: buf = (char *) kmalloc (CD_FRAMESIZE_RAW, GFP_KERNEL);
2469: if (buf == NULL)
2470: return -ENOMEM;
2471:
2472: stat = cdrom_read_block (drive, format, lba, 1, buf, blocksize,
2473: NULL);
2474: if (stat == 0)
2475: memcpy_tofs ((char *)arg, buf, blocksize);
2476:
2477: kfree (buf);
2478: return stat;
2479: }
2480:
2481: case CDROM_GET_UPC: {
2482: int stat;
2483: char mcnbuf[24];
2484: struct cdrom_mcn mcn;
2485:
2486: stat = verify_area (VERIFY_WRITE, (void *) arg,
2487: sizeof (mcn));
2488: if (stat) return stat;
2489:
2490: stat = cdrom_read_subchannel (drive, 2, /* get MCN */
2491: mcnbuf, sizeof (mcnbuf),
2492: NULL);
2493: if (stat) return stat;
2494:
2495: memcpy (mcn.medium_catalog_number, mcnbuf+9,
2496: sizeof (mcn.medium_catalog_number)-1);
2497: mcn.medium_catalog_number[sizeof (mcn.medium_catalog_number)-1]
2498: = '\0';
2499:
2500: memcpy_tofs ((void *) arg, &mcn, sizeof (mcn));
2501:
2502: return stat;
2503: }
2504:
2505: case CDROMLOADFROMSLOT:
2506: printk ("%s: Use CDROM_SELECT_DISC "
2507: " instead of CDROMLOADFROMSLOT.\n", drive->name);
2508: /* Fall through. */
2509:
2510: case CDROM_SELECT_DISC: {
2511: struct atapi_request_sense my_reqbuf;
2512: int stat;
2513:
2514: if (drive->usage > 1)
2515: return -EBUSY;
2516:
2517: (void) cdrom_load_unload (drive, -1, NULL);
2518:
2519: cdrom_saw_media_change (drive);
2520: if (arg == -1) {
2521: (void) cdrom_lockdoor (drive, 0, NULL);
2522: return 0;
2523: }
2524: (void) cdrom_load_unload (drive, (int)arg, NULL);
2525:
2526: stat = cdrom_check_status (drive, &my_reqbuf);
2527: if (stat && my_reqbuf.sense_key == NOT_READY) {
2528: return -ENOENT;
2529: }
2530:
2531: /* And try to read the TOC information now. */
2532: return cdrom_read_toc (drive, &my_reqbuf);
2533: }
2534:
2535: #if 0 /* Doesn't work reliably yet. */
2536: case CDROMRESET: {
2537: struct request req;
2538: ide_init_drive_cmd (&req);
2539: req.cmd = RESET_DRIVE_COMMAND;
2540: return ide_do_drive_cmd (drive, &req, ide_wait);
2541: }
2542: #endif
2543:
2544:
2545: #ifdef TEST
2546: case 0x1234: {
2547: int stat;
2548: struct packet_command pc;
2549: int len, lena;
2550:
2551: memset (&pc, 0, sizeof (pc));
2552:
2553: stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
2554: if (stat) return stat;
2555: memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
2556: arg += sizeof (pc.c);
2557:
2558: stat = verify_area (VERIFY_READ, (void *) arg, sizeof (len));
2559: if (stat) return stat;
2560: memcpy_fromfs (&len, (void *) arg , sizeof (len));
2561: arg += sizeof (len);
2562:
2563: if (len > 0) {
2564: stat = verify_area (VERIFY_WRITE, (void *) arg, len);
2565: if (stat) return stat;
2566: }
2567:
2568: lena = len;
2569: if (lena < 0) lena = 0;
2570:
2571: {
2572: char buf[lena];
2573: if (len > 0) {
2574: pc.buflen = len;
2575: pc.buffer = buf;
2576: }
2577:
2578: stat = cdrom_queue_packet_command (drive, &pc);
2579:
2580: if (len > 0)
2581: memcpy_tofs ((void *)arg, buf, len);
2582: }
2583:
2584: return stat;
2585: }
2586: #endif
2587:
2588: default:
2589: return -EPERM;
2590: }
2591:
2592: }
2593:
2594:
2595:
2596: /****************************************************************************
2597: * Other driver requests (open, close, check media change).
2598: */
2599:
2600: int ide_cdrom_check_media_change (ide_drive_t *drive)
2601: {
2602: int retval;
2603:
2604: (void) cdrom_check_status (drive, NULL);
2605:
2606: retval = CDROM_STATE_FLAGS (drive)->media_changed;
2607: CDROM_STATE_FLAGS (drive)->media_changed = 0;
2608:
2609: return retval;
2610: }
2611:
2612:
2613: int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2614: {
2615: /* no write access */
2616: if (fp->f_mode & 2) {
2617: --drive->usage;
2618: return -EROFS;
2619: }
2620:
2621: /* If this is the first open, check the drive status. */
2622: if (drive->usage == 1) {
2623: int stat;
2624: struct atapi_request_sense my_reqbuf;
2625: my_reqbuf.sense_key = 0;
2626:
2627: /* Get the drive status. */
2628: stat = cdrom_check_status (drive, &my_reqbuf);
2629:
2630: /* If the tray is open, try to close it. */
2631: if (stat && my_reqbuf.sense_key == NOT_READY) {
2632: cdrom_eject (drive, 1, &my_reqbuf);
2633: stat = cdrom_check_status (drive, &my_reqbuf);
2634: }
2635:
2636: /* If things worked ok, lock the door and read the
2637: TOC information. */
2638: if (stat == 0 || my_reqbuf.sense_key == UNIT_ATTENTION) {
2639: (void) cdrom_lockdoor (drive, 1, &my_reqbuf);
2640: (void) cdrom_read_toc (drive, &my_reqbuf);
2641: }
2642: }
2643:
2644: return 0;
2645: }
2646:
2647:
2648: /*
2649: * Close down the device. Invalidate all cached blocks.
2650: */
2651:
2652: void ide_cdrom_release (struct inode *inode, struct file *file,
2653: ide_drive_t *drive)
2654: {
2655: if (drive->usage == 0) {
2656: invalidate_buffers (inode->i_rdev);
2657:
2658: /* Unlock the door. */
2659: (void) cdrom_lockdoor (drive, 0, NULL);
2660:
2661: /* Do an eject if we were requested to do so. */
2662: if (CDROM_STATE_FLAGS (drive)->eject_on_close)
2663: (void) cdrom_eject (drive, 0, NULL);
2664: }
2665: }
2666:
2667:
2668:
2669: /****************************************************************************
2670: * Device initialization.
2671: */
2672:
2673: void ide_cdrom_setup (ide_drive_t *drive)
2674: {
2675: blksize_size[HWIF(drive)->major][drive->select.b.unit << PARTN_BITS] =
2676: CD_FRAMESIZE;
2677:
2678: drive->special.all = 0;
2679: drive->ready_stat = 0;
2680:
2681: CDROM_STATE_FLAGS (drive)->media_changed = 0;
2682: CDROM_STATE_FLAGS (drive)->toc_valid = 0;
2683: CDROM_STATE_FLAGS (drive)->door_locked = 0;
2684:
2685: /* Turn this off by default, since many people don't like it. */
2686: CDROM_STATE_FLAGS (drive)->eject_on_close= 0;
2687:
2688: #if NO_DOOR_LOCKING
2689: CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2690: #else
2691: CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2692: #endif
2693:
2694: /* by default Sanyo 3 CD changer support is turned off and
2695: ATAPI Rev 2.2+ standard support for CD changers is used */
2696: CDROM_STATE_FLAGS (drive)->sanyo_slot = 0;
2697:
2698: if (drive->id != NULL)
2699: CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2700: ((drive->id->config & 0x0060) == 0x20);
2701: else
2702: CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2703:
2704: #if ! STANDARD_ATAPI
2705: drive->cdrom_info.max_sectors = 252;
2706:
2707: CDROM_CONFIG_FLAGS (drive)->old_readcd = 0;
2708: CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2709: CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2710: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2711: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2712:
2713: if (drive->id != NULL) {
1.1.1.2 ! root 2714: const char *model = (const char *)drive->id->model;
! 2715: const char *fw_rev = (const char *)drive->id->fw_rev;
! 2716:
! 2717: if (strcmp (model, "V003S0DS") == 0 &&
! 2718: fw_rev[4] == '1' &&
! 2719: fw_rev[6] <= '2') {
1.1 root 2720: /* Vertos 300.
2721: Some versions of this drive like to talk BCD. */
2722: CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2723: CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2724: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2725: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2726: }
2727:
1.1.1.2 ! root 2728: else if (strcmp (model, "V006E0DS") == 0 &&
! 2729: fw_rev[4] == '1' &&
! 2730: fw_rev[6] <= '2') {
1.1 root 2731: /* Vertos 600 ESD. */
2732: CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2733: }
2734:
1.1.1.2 ! root 2735: else if (strcmp (model, "GCD-R580B") == 0)
1.1 root 2736: drive->cdrom_info.max_sectors = 124;
2737:
1.1.1.2 ! root 2738: else if (strcmp (model,
1.1 root 2739: "NEC CD-ROM DRIVE:260") == 0 &&
1.1.1.2 ! root 2740: strcmp (fw_rev, "1.01") == 0) {
1.1 root 2741: /* Old NEC260 (not R). */
2742: CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2743: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2744: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2745: }
2746:
1.1.1.2 ! root 2747: else if (strcmp (model, "WEARNES CDD-120") == 0 &&
! 2748: strcmp (fw_rev, "A1.1") == 0) {
1.1 root 2749: /* Wearnes */
2750: CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2751: CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2752: }
2753:
2754: /* Sanyo 3 CD changer uses a non-standard command
2755: for CD changing */
1.1.1.2 ! root 2756: else if ((strcmp(model, "CD-ROM CDR-C3 G") == 0) ||
! 2757: (strcmp(model, "CD-ROM CDR-C3G") == 0) ||
! 2758: (strcmp(model, "CD-ROM CDR_C36") == 0)) {
1.1 root 2759: /* uses CD in slot 0 when value is set to 3 */
2760: CDROM_STATE_FLAGS (drive)->sanyo_slot = 3;
2761: }
2762:
2763: }
2764: #endif /* not STANDARD_ATAPI */
2765:
2766: drive->cdrom_info.toc = NULL;
2767: drive->cdrom_info.sector_buffer = NULL;
2768: drive->cdrom_info.sector_buffered = 0;
2769: drive->cdrom_info.nsectors_buffered = 0;
2770: }
2771:
2772:
2773:
2774: /*
2775: * TODO (for 2.1?):
2776: * Avoid printing error messages for expected errors from the drive.
2777: * Integrate with generic cdrom driver.
2778: * Query the drive to find what features are available
2779: * before trying to use them.
2780: * Integrate spindown time adjustment patch.
2781: * Modularize.
2782: * CDROMRESET ioctl.
2783: * Better support for changers.
2784: */
2785:
2786:
2787:
2788: /*==========================================================================*/
2789: /*
2790: * Local variables:
2791: * c-basic-offset: 8
2792: * End:
2793: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.