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