This source file includes following definitions.
- cdrom_in_bytes
- cdrom_out_bytes
- cdrom_analyze_sense_data
- restore_request
- cdrom_queue_request_sense
- cdrom_end_request
- cdrom_saw_media_change
- cdrom_decode_status
- cdrom_start_packet_command
- cdrom_transfer_packet_command
- cdrom_buffer_sectors
- cdrom_read_check_ireason
- cdrom_read_intr
- cdrom_read_from_buffer
- cdrom_start_read_continuation
- cdrom_start_read
- cdrom_pc_intr
- cdrom_do_pc_continuation
- cdrom_do_packet_command
- cdrom_sleep
- cdrom_queue_packet_command
- ide_do_rw_cdrom
- bin2bcd
- bcd2bin
- msf_from_bcd
- lba_to_msf
- msf_to_lba
- cdrom_check_status
- cdrom_lockdoor
- cdrom_eject
- cdrom_pause
- cdrom_startstop
- cdrom_read_capacity
- cdrom_read_tocentry
- cdrom_read_toc
- cdrom_read_subchannel
- cdrom_mode_sense
- cdrom_mode_select
- cdrom_play_lba_range_1
- cdrom_play_lba_range
- cdrom_get_toc_entry
- cdrom_read_block
- ide_cdrom_ioctl
- ide_cdrom_check_media_change
- ide_cdrom_open
- ide_cdrom_release
- ide_cdrom_setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 #include <linux/types.h>
112 #include <linux/kernel.h>
113 #include <linux/delay.h>
114 #include <linux/timer.h>
115 #include <linux/malloc.h>
116 #include <linux/ioport.h>
117 #include <linux/interrupt.h>
118 #include <linux/blkdev.h>
119 #include <linux/errno.h>
120 #include <linux/hdreg.h>
121 #include <linux/cdrom.h>
122 #include <asm/irq.h>
123 #include <asm/io.h>
124 #include <asm/byteorder.h>
125 #include <asm/segment.h>
126 #ifdef __alpha__
127 # include <asm/unaligned.h>
128 #endif
129
130 #include "ide.h"
131
132
133
134
135
136
137
138 #ifndef VERBOSE_IDE_CD_ERRORS
139 #define VERBOSE_IDE_CD_ERRORS 0
140 #endif
141
142
143
144
145
146
147 #ifndef STANDARD_ATAPI
148 #define STANDARD_ATAPI 0
149 #endif
150
151
152
153
154
155 #ifndef NO_DOOR_LOCKING
156 #define NO_DOOR_LOCKING 0
157 #endif
158
159
160
161
162 #define SECTOR_SIZE 512
163 #define SECTOR_BITS 9
164 #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
165
166 #define MIN(a,b) ((a) < (b) ? (a) : (b))
167
168
169 #define PACKET_COMMAND 4315
170 #define REQUEST_SENSE_COMMAND 4316
171 #define RESET_DRIVE_COMMAND 4317
172
173
174
175 #define TEST_UNIT_READY 0x00
176 #define REQUEST_SENSE 0x03
177 #define START_STOP 0x1b
178 #define ALLOW_MEDIUM_REMOVAL 0x1e
179 #define READ_CAPACITY 0x25
180 #define READ_10 0x28
181 #define MODE_SENSE_10 0x5a
182 #define MODE_SELECT_10 0x55
183 #define READ_CD 0xbe
184
185
186
187
188 #define NO_SENSE 0x00
189 #define RECOVERED_ERROR 0x01
190 #define NOT_READY 0x02
191 #define MEDIUM_ERROR 0x03
192 #define HARDWARE_ERROR 0x04
193 #define ILLEGAL_REQUEST 0x05
194 #define UNIT_ATTENTION 0x06
195 #define DATA_PROTECT 0x07
196 #define ABORTED_COMMAND 0x0b
197 #define MISCOMPARE 0x0e
198
199
200
201
202
203
204
205
206 struct ide_cd_config_flags {
207 __u8 drq_interrupt : 1;
208
209 __u8 no_doorlock : 1;
210 #if ! STANDARD_ATAPI
211 __u8 old_readcd : 1;
212 __u8 playmsf_as_bcd : 1;
213 __u8 tocaddr_as_bcd : 1;
214 __u8 toctracks_as_bcd : 1;
215 __u8 subchan_as_bcd : 1;
216 #endif
217 __u8 reserved : 1;
218 };
219 #define CDROM_CONFIG_FLAGS(drive) ((struct ide_cd_config_flags *)&((drive)->bios_sect))
220
221
222
223
224 struct ide_cd_state_flags {
225 __u8 media_changed : 1;
226 __u8 toc_valid : 1;
227 __u8 door_locked : 1;
228 __u8 eject_on_close: 1;
229 __u8 reserved : 4;
230 };
231 #define CDROM_STATE_FLAGS(drive) ((struct ide_cd_state_flags *)&((drive)->bios_head))
232
233
234 #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
235
236
237
238
239
240
241
242
243
244
245
246
247
248 static inline
249 void cdrom_in_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
250 {
251 ++bytecount;
252 ide_input_data (drive, buffer, bytecount / 4);
253 if ((bytecount & 0x03) >= 2) {
254 insw (IDE_DATA_REG, ((byte *)buffer) + (bytecount & ~0x03), 1);
255 }
256 }
257
258
259 static inline
260 void cdrom_out_bytes (ide_drive_t *drive, void *buffer, uint bytecount)
261 {
262 ++bytecount;
263 ide_output_data (drive, buffer, bytecount / 4);
264 if ((bytecount & 0x03) >= 2) {
265 outsw (IDE_DATA_REG,
266 ((byte *)buffer) + (bytecount & ~0x03), 1);
267 }
268 }
269
270
271
272
273
274
275
276 #define ARY_LEN(a) ((sizeof(a) / sizeof(a[0])))
277
278 #if VERBOSE_IDE_CD_ERRORS
279
280
281
282 char *sense_key_texts[16] = {
283 "No sense data",
284 "Recovered error",
285 "Not ready",
286 "Medium error",
287 "Hardware error",
288 "Illegal request",
289 "Unit attention",
290 "Data protect",
291 "(reserved)",
292 "(reserved)",
293 "(reserved)",
294 "Aborted command",
295 "(reserved)",
296 "(reserved)",
297 "Miscompare",
298 "(reserved)",
299 };
300
301
302
303
304 struct {
305 short asc_ascq;
306 char *text;
307 } sense_data_texts[] = {
308 { 0x0000, "No additional sense information" },
309 { 0x0011, "Audio play operation in progress" },
310 { 0x0012, "Audio play operation paused" },
311 { 0x0013, "Audio play operation successfully completed" },
312 { 0x0014, "Audio play operation stopped due to error" },
313 { 0x0015, "No current audio status to return" },
314
315 { 0x0200, "No seek complete" },
316
317 { 0x0400, "Logical unit not ready - cause not reportable" },
318 { 0x0401,
319 "Logical unit not ready - in progress (sic) of becoming ready" },
320 { 0x0402, "Logical unit not ready - initializing command required" },
321 { 0x0403, "Logical unit not ready - manual intervention required" },
322
323 { 0x0600, "No reference position found" },
324
325 { 0x0900, "Track following error" },
326 { 0x0901, "Tracking servo failure" },
327 { 0x0902, "Focus servo failure" },
328 { 0x0903, "Spindle servo failure" },
329
330 { 0x1100, "Unrecovered read error" },
331 { 0x1106, "CIRC unrecovered error" },
332
333 { 0x1500, "Random positioning error" },
334 { 0x1501, "Mechanical positioning error" },
335 { 0x1502, "Positioning error detected by read of medium" },
336
337 { 0x1700, "Recovered data with no error correction applied" },
338 { 0x1701, "Recovered data with retries" },
339 { 0x1702, "Recovered data with positive head offset" },
340 { 0x1703, "Recovered data with negative head offset" },
341 { 0x1704, "Recovered data with retries and/or CIRC applied" },
342 { 0x1705, "Recovered data using previous sector ID" },
343
344 { 0x1800, "Recovered data with error correction applied" },
345 { 0x1801, "Recovered data with error correction and retries applied" },
346 { 0x1802, "Recovered data - the data was auto-reallocated" },
347 { 0x1803, "Recovered data with CIRC" },
348 { 0x1804, "Recovered data with L-EC" },
349 { 0x1805, "Recovered data - recommend reassignment" },
350 { 0x1806, "Recovered data - recommend rewrite" },
351
352 { 0x1a00, "Parameter list length error" },
353
354 { 0x2000, "Invalid command operation code" },
355
356 { 0x2100, "Logical block address out of range" },
357
358 { 0x2400, "Invalid field in command packet" },
359
360 { 0x2600, "Invalid field in parameter list" },
361 { 0x2601, "Parameter not supported" },
362 { 0x2602, "Parameter value invalid" },
363 { 0x2603, "Threshold parameters not supported" },
364
365 { 0x2800, "Not ready to ready transition, medium may have changed" },
366
367 { 0x2900, "Power on, reset or bus device reset occurred" },
368
369 { 0x2a00, "Parameters changed" },
370 { 0x2a01, "Mode parameters changed" },
371
372 { 0x3000, "Incompatible medium installed" },
373 { 0x3001, "Cannot read medium - unknown format" },
374 { 0x3002, "Cannot read medium - incompatible format" },
375
376 { 0x3700, "Rounded parameter" },
377
378 { 0x3900, "Saving parameters not supported" },
379
380 { 0x3a00, "Medium not present" },
381
382 { 0x3f00, "ATAPI CD-ROM drive operating conditions have changed" },
383 { 0x3f01, "Microcode has been changed" },
384 { 0x3f02, "Changed operating definition" },
385 { 0x3f03, "Inquiry data has changed" },
386
387 { 0x4000, "Diagnostic failure on component (ASCQ)" },
388
389 { 0x4400, "Internal ATAPI CD-ROM drive failure" },
390
391 { 0x4e00, "Overlapped commands attempted" },
392
393 { 0x5300, "Media load or eject failed" },
394 { 0x5302, "Medium removal prevented" },
395
396 { 0x5700, "Unable to recover table of contents" },
397
398 { 0x5a00, "Operator request or state change input (unspecified)" },
399 { 0x5a01, "Operator medium removal request" },
400
401 { 0x5b00, "Threshold condition met" },
402
403 { 0x5c00, "Status change" },
404
405 { 0x6300, "End of user area encountered on this track" },
406
407 { 0x6400, "Illegal mode for this track" },
408
409 { 0xbf00, "Loss of streaming" },
410 };
411 #endif
412
413
414
415
416
417
418
419
420 static
421 void cdrom_analyze_sense_data (ide_drive_t *drive,
422 struct atapi_request_sense *reqbuf,
423 struct packet_command *failed_command)
424 {
425
426
427
428
429 if (failed_command &&
430 failed_command->c[0] == SCMD_READ_SUBCHANNEL &&
431 (reqbuf->sense_key == NOT_READY ||
432 reqbuf->sense_key == UNIT_ATTENTION))
433 return;
434
435 #if VERBOSE_IDE_CD_ERRORS
436 {
437 int i;
438 char *s;
439 char buf[80];
440
441 printk ("ATAPI device %s:\n", drive->name);
442
443 printk (" Error code: 0x%02x\n", reqbuf->error_code);
444
445 if (reqbuf->sense_key >= 0 &&
446 reqbuf->sense_key < ARY_LEN (sense_key_texts))
447 s = sense_key_texts[reqbuf->sense_key];
448 else
449 s = "(bad sense key)";
450
451 printk (" Sense key: 0x%02x - %s\n", reqbuf->sense_key, s);
452
453 if (reqbuf->asc == 0x40) {
454 sprintf (buf, "Diagnostic failure on component 0x%02x",
455 reqbuf->ascq);
456 s = buf;
457 } else {
458 int lo, hi;
459 int key = (reqbuf->asc << 8);
460 if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
461 key |= reqbuf->ascq;
462
463 lo = 0;
464 hi = ARY_LEN (sense_data_texts);
465 s = NULL;
466
467 while (hi > lo) {
468 int mid = (lo + hi) / 2;
469 if (sense_data_texts[mid].asc_ascq == key) {
470 s = sense_data_texts[mid].text;
471 break;
472 }
473 else if (sense_data_texts[mid].asc_ascq > key)
474 hi = mid;
475 else
476 lo = mid+1;
477 }
478 }
479
480 if (s == NULL) {
481 if (reqbuf->asc > 0x80)
482 s = "(vendor-specific error)";
483 else
484 s = "(reserved error code)";
485 }
486
487 printk (" Additional sense data: 0x%02x, 0x%02x - %s\n",
488 reqbuf->asc, reqbuf->ascq, s);
489
490 if (failed_command != NULL) {
491 printk (" Failed packet command: ");
492 for (i=0; i<sizeof (failed_command->c); i++)
493 printk ("%02x ", failed_command->c[i]);
494 printk ("\n");
495 }
496
497 if (reqbuf->sense_key == ILLEGAL_REQUEST &&
498 (reqbuf->sense_key_specific[0] & 0x80) != 0) {
499 printk (" Error in %s byte %d",
500 (reqbuf->sense_key_specific[0] & 0x40) != 0
501 ? "command packet"
502 : "command data",
503 (reqbuf->sense_key_specific[1] << 8) +
504 reqbuf->sense_key_specific[2]);
505
506 if ((reqbuf->sense_key_specific[0] & 0x40) != 0) {
507 printk (" bit %d",
508 reqbuf->sense_key_specific[0] & 0x07);
509 }
510
511 printk ("\n");
512 }
513 }
514
515 #else
516
517
518
519
520 if (reqbuf->sense_key == UNIT_ATTENTION ||
521 (reqbuf->sense_key == NOT_READY && (reqbuf->asc == 4 ||
522 reqbuf->asc == 0x3a)))
523 return;
524
525 printk ("%s: code: 0x%02x key: 0x%02x asc: 0x%02x ascq: 0x%02x\n",
526 drive->name,
527 reqbuf->error_code, reqbuf->sense_key,
528 reqbuf->asc, reqbuf->ascq);
529 #endif
530 }
531
532
533
534
535 static void restore_request (struct request *rq)
536 {
537 if (rq->buffer != rq->bh->b_data) {
538 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
539 rq->buffer = rq->bh->b_data;
540 rq->nr_sectors += n;
541 rq->sector -= n;
542 }
543 rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
544 }
545
546
547 static void cdrom_queue_request_sense (ide_drive_t *drive,
548 struct semaphore *sem,
549 struct atapi_request_sense *reqbuf,
550 struct packet_command *failed_command)
551 {
552 struct request *rq;
553 struct packet_command *pc;
554 int len;
555
556
557
558 if (reqbuf == NULL)
559 reqbuf = &drive->cdrom_info.sense_data;
560
561
562
563 pc = &HWIF(drive)->request_sense_pc;
564 memset (pc, 0, sizeof (*pc));
565
566
567
568
569
570 len = sizeof (*reqbuf) / 4;
571 len *= 4;
572
573 pc->c[0] = REQUEST_SENSE;
574 pc->c[4] = len;
575 pc->buffer = (char *)reqbuf;
576 pc->buflen = len;
577 pc->sense_data = (struct atapi_request_sense *)failed_command;
578
579
580
581 rq = &HWIF(drive)->request_sense_request;
582 ide_init_drive_cmd (rq);
583 rq->cmd = REQUEST_SENSE_COMMAND;
584 rq->buffer = (char *)pc;
585 rq->sem = sem;
586 (void) ide_do_drive_cmd (drive, rq, ide_preempt);
587 }
588
589
590 static void cdrom_end_request (int uptodate, ide_drive_t *drive)
591 {
592 struct request *rq = HWGROUP(drive)->rq;
593
594
595
596 if (!uptodate && rq->bh != 0) {
597 int adj = rq->current_nr_sectors - 1;
598 rq->current_nr_sectors -= adj;
599 rq->sector += adj;
600 }
601
602 if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate) {
603 struct packet_command *pc = (struct packet_command *)
604 rq->buffer;
605 cdrom_analyze_sense_data (drive,
606 (struct atapi_request_sense *)
607 (pc->buffer - pc->c[4]),
608 (struct packet_command *)
609 pc->sense_data);
610 }
611
612 ide_end_request (uptodate, HWGROUP(drive));
613 }
614
615
616
617
618 static void cdrom_saw_media_change (ide_drive_t *drive)
619 {
620 CDROM_STATE_FLAGS (drive)->media_changed = 1;
621 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
622 drive->cdrom_info.nsectors_buffered = 0;
623 }
624
625
626
627
628 static int cdrom_decode_status (ide_drive_t *drive, int good_stat,
629 int *stat_ret)
630 {
631 struct request *rq = HWGROUP(drive)->rq;
632 int stat, err, sense_key, cmd;
633
634
635 stat = GET_STAT();
636 *stat_ret = stat;
637
638 if (OK_STAT (stat, good_stat, BAD_R_STAT))
639 return 0;
640
641
642 err = IN_BYTE (IDE_ERROR_REG);
643 sense_key = err >> 4;
644
645 if (rq == NULL)
646 printk ("%s : missing request in cdrom_decode_status\n",
647 drive->name);
648 else {
649 cmd = rq->cmd;
650
651 if (cmd == REQUEST_SENSE_COMMAND) {
652
653
654
655
656 struct packet_command *pc = (struct packet_command *)
657 rq->buffer;
658 pc->stat = 1;
659 cdrom_end_request (1, drive);
660 ide_error (drive, "request sense failure", stat);
661 return 1;
662
663 } else if (cmd == PACKET_COMMAND) {
664
665
666 struct packet_command *pc = (struct packet_command *)
667 rq->buffer;
668 struct semaphore *sem = NULL;
669
670
671 if (sense_key == NOT_READY) {
672 cdrom_saw_media_change (drive);
673
674
675
676
677
678
679
680 if (pc->c[0] != SCMD_READ_SUBCHANNEL)
681 printk ("%s : tray open or drive not ready\n",
682 drive->name);
683 } else if (sense_key == UNIT_ATTENTION) {
684
685 cdrom_saw_media_change (drive);
686 printk ("%s: media changed\n", drive->name);
687 } else {
688
689 ide_dump_status (drive, "packet command error",
690 stat);
691 }
692
693
694
695
696
697
698
699
700
701
702 if ((stat & ERR_STAT) != 0) {
703 sem = rq->sem;
704 rq->sem = NULL;
705 }
706
707 pc->stat = 1;
708 cdrom_end_request (1, drive);
709
710 if ((stat & ERR_STAT) != 0)
711 cdrom_queue_request_sense (drive, sem,
712 pc->sense_data, pc);
713 } else {
714
715
716 if (sense_key == NOT_READY) {
717
718 cdrom_saw_media_change (drive);
719
720
721 printk ("%s : tray open\n", drive->name);
722 cdrom_end_request (0, drive);
723 } else if (sense_key == UNIT_ATTENTION) {
724
725 cdrom_saw_media_change (drive);
726
727
728
729
730 if (++rq->errors > ERROR_MAX)
731 cdrom_end_request (0, drive);
732 } else if (sense_key == ILLEGAL_REQUEST ||
733 sense_key == DATA_PROTECT) {
734
735
736 ide_dump_status (drive, "command error", stat);
737 cdrom_end_request (0, drive);
738 } else if ((err & ~ABRT_ERR) != 0) {
739
740
741 ide_error (drive, "cdrom_decode_status", stat);
742 return 1;
743 } else if ((++rq->errors > ERROR_MAX)) {
744
745 cdrom_end_request (0, drive);
746 }
747
748
749
750 if ((stat & ERR_STAT) != 0)
751 cdrom_queue_request_sense (drive,
752 NULL, NULL, NULL);
753 }
754 }
755
756
757 return 1;
758 }
759
760
761
762
763
764
765
766
767
768 static int cdrom_start_packet_command (ide_drive_t *drive, int xferlen,
769 ide_handler_t *handler)
770 {
771
772 if (ide_wait_stat (drive, 0, BUSY_STAT, WAIT_READY)) return 1;
773
774
775 OUT_BYTE (0, IDE_FEATURE_REG);
776 OUT_BYTE (0, IDE_NSECTOR_REG);
777 OUT_BYTE (0, IDE_SECTOR_REG);
778
779 OUT_BYTE (xferlen & 0xff, IDE_LCYL_REG);
780 OUT_BYTE (xferlen >> 8 , IDE_HCYL_REG);
781 OUT_BYTE (drive->ctl, IDE_CONTROL_REG);
782
783 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
784 ide_set_handler (drive, handler, WAIT_CMD);
785 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
786 } else {
787 OUT_BYTE (WIN_PACKETCMD, IDE_COMMAND_REG);
788 (*handler) (drive);
789 }
790
791 return 0;
792 }
793
794
795
796
797
798
799
800 static int cdrom_transfer_packet_command (ide_drive_t *drive,
801 char *cmd_buf, int cmd_len,
802 ide_handler_t *handler)
803 {
804 if (CDROM_CONFIG_FLAGS (drive)->drq_interrupt) {
805
806
807 int stat_dum;
808
809
810 if (cdrom_decode_status (drive, DRQ_STAT, &stat_dum)) return 1;
811 } else {
812
813 if (ide_wait_stat (drive, DRQ_STAT, BUSY_STAT, WAIT_READY))
814 return 1;
815 }
816
817
818 ide_set_handler (drive, handler, WAIT_CMD);
819
820
821 cdrom_out_bytes (drive, cmd_buf, cmd_len);
822
823 return 0;
824 }
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839 static void cdrom_buffer_sectors (ide_drive_t *drive, unsigned long sector,
840 int sectors_to_transfer)
841 {
842 struct cdrom_info *info = &drive->cdrom_info;
843
844
845 int sectors_to_buffer = MIN (sectors_to_transfer,
846 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
847 info->nsectors_buffered);
848
849 char *dest;
850
851
852
853
854 if (info->sector_buffer == NULL) {
855 info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE,
856 GFP_ATOMIC);
857
858
859
860 if (info->sector_buffer == NULL)
861 sectors_to_buffer = 0;
862 }
863
864
865 if (info->nsectors_buffered == 0)
866 info->sector_buffered = sector;
867
868
869 dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
870 while (sectors_to_buffer > 0) {
871 cdrom_in_bytes (drive, dest, SECTOR_SIZE);
872 --sectors_to_buffer;
873 --sectors_to_transfer;
874 ++info->nsectors_buffered;
875 dest += SECTOR_SIZE;
876 }
877
878
879 while (sectors_to_transfer > 0) {
880 char dum[SECTOR_SIZE];
881 cdrom_in_bytes (drive, dum, sizeof (dum));
882 --sectors_to_transfer;
883 }
884 }
885
886
887
888
889
890
891
892 static inline
893 int cdrom_read_check_ireason (ide_drive_t *drive, int len, int ireason)
894 {
895 ireason &= 3;
896 if (ireason == 2) return 0;
897
898 if (ireason == 0) {
899
900 printk ("%s: cdrom_read_intr: "
901 "Drive wants to transfer data the wrong way!\n",
902 drive->name);
903
904
905
906 while (len > 0) {
907 int dum = 0;
908 cdrom_out_bytes (drive, &dum, sizeof (dum));
909 len -= sizeof (dum);
910 }
911 } else {
912
913 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
914 drive->name, ireason);
915 }
916
917 cdrom_end_request (0, drive);
918 return -1;
919 }
920
921
922
923
924
925 static void cdrom_read_intr (ide_drive_t *drive)
926 {
927 int stat;
928 int ireason, len, sectors_to_transfer, nskip;
929
930 struct request *rq = HWGROUP(drive)->rq;
931
932
933 if (cdrom_decode_status (drive, 0, &stat)) return;
934
935
936 ireason = IN_BYTE (IDE_NSECTOR_REG);
937 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
938
939
940 if ((stat & DRQ_STAT) == 0) {
941
942
943 if (rq->current_nr_sectors > 0) {
944 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
945 drive->name, rq->current_nr_sectors);
946 cdrom_end_request (0, drive);
947 } else
948 cdrom_end_request (1, drive);
949
950 return;
951 }
952
953
954 if (cdrom_read_check_ireason (drive, len, ireason)) return;
955
956
957
958
959 if ((len % SECTOR_SIZE) != 0) {
960 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
961 drive->name, len);
962 printk (" This drive is not supported by this version of the driver\n");
963 cdrom_end_request (0, drive);
964 return;
965 }
966
967
968 sectors_to_transfer = len / SECTOR_SIZE;
969
970
971
972 nskip = MIN ((int)(rq->current_nr_sectors -
973 (rq->bh->b_size >> SECTOR_BITS)),
974 sectors_to_transfer);
975
976 while (nskip > 0) {
977
978 char dum[SECTOR_SIZE];
979 cdrom_in_bytes (drive, dum, sizeof (dum));
980
981 --rq->current_nr_sectors;
982 --nskip;
983 --sectors_to_transfer;
984 }
985
986
987 while (sectors_to_transfer > 0) {
988 int this_transfer;
989
990
991
992 if (rq->current_nr_sectors == 0 &&
993 rq->nr_sectors > 0)
994 cdrom_end_request (1, drive);
995
996
997
998 if (rq->current_nr_sectors == 0) {
999 cdrom_buffer_sectors (drive,
1000 rq->sector, sectors_to_transfer);
1001 sectors_to_transfer = 0;
1002 } else {
1003
1004
1005
1006 this_transfer = MIN (sectors_to_transfer,
1007 rq->current_nr_sectors);
1008
1009
1010
1011 while (this_transfer > 0) {
1012 cdrom_in_bytes (drive
1013 , rq->buffer, SECTOR_SIZE);
1014 rq->buffer += SECTOR_SIZE;
1015 --rq->nr_sectors;
1016 --rq->current_nr_sectors;
1017 ++rq->sector;
1018 --this_transfer;
1019 --sectors_to_transfer;
1020 }
1021 }
1022 }
1023
1024
1025
1026 ide_set_handler (drive, &cdrom_read_intr, WAIT_CMD);
1027 }
1028
1029
1030
1031
1032
1033
1034 static int cdrom_read_from_buffer (ide_drive_t *drive)
1035 {
1036 struct cdrom_info *info = &drive->cdrom_info;
1037 struct request *rq = HWGROUP(drive)->rq;
1038
1039
1040 if (info->sector_buffer == NULL) return 0;
1041
1042
1043
1044 while (rq->nr_sectors > 0 &&
1045 rq->sector >= info->sector_buffered &&
1046 rq->sector < info->sector_buffered + info->nsectors_buffered) {
1047 if (rq->current_nr_sectors == 0)
1048 cdrom_end_request (1, drive);
1049
1050 memcpy (rq->buffer,
1051 info->sector_buffer +
1052 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
1053 SECTOR_SIZE);
1054 rq->buffer += SECTOR_SIZE;
1055 --rq->current_nr_sectors;
1056 --rq->nr_sectors;
1057 ++rq->sector;
1058 }
1059
1060
1061
1062 if (rq->nr_sectors == 0) {
1063 cdrom_end_request (1, drive);
1064 return -1;
1065 }
1066
1067
1068 if (rq->current_nr_sectors == 0)
1069 cdrom_end_request (1, drive);
1070
1071
1072
1073
1074
1075 if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
1076 (rq->sector % SECTORS_PER_FRAME) != 0) {
1077 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
1078 drive->name, rq->sector);
1079 cdrom_end_request (0, drive);
1080 return -1;
1081 }
1082
1083 return 0;
1084 }
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094 static void cdrom_start_read_continuation (ide_drive_t *drive)
1095 {
1096 struct packet_command pc;
1097 struct request *rq = HWGROUP(drive)->rq;
1098
1099 int nsect, sector, nframes, frame, nskip;
1100
1101
1102 nsect = rq->nr_sectors;
1103
1104
1105 sector = rq->sector;
1106
1107
1108
1109
1110
1111
1112
1113
1114 nskip = (sector % SECTORS_PER_FRAME);
1115 if (nskip > 0) {
1116
1117 if (rq->current_nr_sectors !=
1118 (rq->bh->b_size >> SECTOR_BITS)) {
1119 printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1120 drive->name, rq->current_nr_sectors);
1121 cdrom_end_request (0, drive);
1122 return;
1123 }
1124
1125 sector -= nskip;
1126 nsect += nskip;
1127 rq->current_nr_sectors += nskip;
1128 }
1129
1130
1131
1132 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1133 frame = sector / SECTORS_PER_FRAME;
1134
1135
1136 nframes = MIN (nframes, 65535);
1137
1138
1139 memset (&pc.c, 0, sizeof (pc.c));
1140 pc.c[0] = READ_10;
1141 pc.c[7] = (nframes >> 8);
1142 pc.c[8] = (nframes & 0xff);
1143 #ifdef __alpha__
1144 stl_u (htonl (frame), (unsigned int *) &pc.c[2]);
1145 #else
1146 *(int *)(&pc.c[2]) = htonl (frame);
1147 #endif
1148
1149
1150 (void) cdrom_transfer_packet_command (drive, pc.c, sizeof (pc.c),
1151 &cdrom_read_intr);
1152 }
1153
1154
1155
1156
1157
1158 static void cdrom_start_read (ide_drive_t *drive, unsigned int block)
1159 {
1160 struct request *rq = HWGROUP(drive)->rq;
1161 int minor = MINOR (rq->rq_dev);
1162
1163
1164
1165 if ((minor & PARTN_MASK) != 0) {
1166 rq->sector = block;
1167 minor &= ~PARTN_MASK;
1168 rq->rq_dev = MKDEV (MAJOR(rq->rq_dev), minor);
1169 }
1170
1171
1172
1173 restore_request (rq);
1174
1175
1176 if (cdrom_read_from_buffer (drive))
1177 return;
1178
1179
1180 drive->cdrom_info.nsectors_buffered = 0;
1181
1182
1183 cdrom_start_packet_command (drive, 32768,
1184 cdrom_start_read_continuation);
1185 }
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195 static int
1196 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1197 struct atapi_request_sense *reqbuf);
1198
1199
1200
1201
1202 static void cdrom_pc_intr (ide_drive_t *drive)
1203 {
1204 int ireason, len, stat, thislen;
1205 struct request *rq = HWGROUP(drive)->rq;
1206 struct packet_command *pc = (struct packet_command *)rq->buffer;
1207
1208
1209 if (cdrom_decode_status (drive, 0, &stat)) return;
1210
1211
1212 ireason = IN_BYTE (IDE_NSECTOR_REG);
1213 len = IN_BYTE (IDE_LCYL_REG) + 256 * IN_BYTE (IDE_HCYL_REG);
1214
1215
1216
1217 if ((stat & DRQ_STAT) == 0) {
1218
1219
1220 if (pc->c[0] == REQUEST_SENSE &&
1221 pc->buflen > 0 &&
1222 pc->buflen <= 5) {
1223 while (pc->buflen > 0) {
1224 *pc->buffer++ = 0;
1225 --pc->buflen;
1226 }
1227 }
1228
1229 if (pc->buflen == 0)
1230 cdrom_end_request (1, drive);
1231 else {
1232 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1233 drive->name, pc->buflen);
1234 pc->stat = 1;
1235 cdrom_end_request (1, drive);
1236 }
1237 return;
1238 }
1239
1240
1241 thislen = pc->buflen;
1242 if (thislen < 0) thislen = -thislen;
1243 if (thislen > len) thislen = len;
1244
1245
1246 if ((ireason & 3) == 0) {
1247
1248 if (pc->buflen > 0) {
1249 printk ("%s: cdrom_pc_intr: Drive wants "
1250 "to transfer data the wrong way!\n",
1251 drive->name);
1252 pc->stat = 1;
1253 thislen = 0;
1254 }
1255
1256
1257 cdrom_out_bytes (drive, pc->buffer, thislen);
1258
1259
1260
1261 while (len > thislen) {
1262 int dum = 0;
1263 cdrom_out_bytes (drive, &dum, sizeof (dum));
1264 len -= sizeof (dum);
1265 }
1266
1267
1268 pc->buffer += thislen;
1269 pc->buflen += thislen;
1270 }
1271
1272
1273 else if ((ireason & 3) == 2) {
1274
1275 if (pc->buflen < 0) {
1276 printk ("%s: cdrom_pc_intr: Drive wants to "
1277 "transfer data the wrong way!\n",
1278 drive->name);
1279 pc->stat = 1;
1280 thislen = 0;
1281 }
1282
1283
1284 cdrom_in_bytes (drive, pc->buffer, thislen);
1285
1286
1287
1288 while (len > thislen) {
1289 int dum = 0;
1290 cdrom_in_bytes (drive, &dum, sizeof (dum));
1291 len -= sizeof (dum);
1292 }
1293
1294
1295 pc->buffer += thislen;
1296 pc->buflen -= thislen;
1297 } else {
1298 printk ("%s: cdrom_pc_intr: The drive "
1299 "appears confused (ireason = 0x%2x)\n",
1300 drive->name, ireason);
1301 pc->stat = 1;
1302 }
1303
1304
1305 ide_set_handler (drive, &cdrom_pc_intr, WAIT_CMD);
1306 }
1307
1308
1309 static void cdrom_do_pc_continuation (ide_drive_t *drive)
1310 {
1311 struct request *rq = HWGROUP(drive)->rq;
1312 struct packet_command *pc = (struct packet_command *)rq->buffer;
1313
1314
1315 cdrom_transfer_packet_command (drive, pc->c,
1316 sizeof (pc->c), &cdrom_pc_intr);
1317 }
1318
1319
1320 static void cdrom_do_packet_command (ide_drive_t *drive)
1321 {
1322 int len;
1323 struct request *rq = HWGROUP(drive)->rq;
1324 struct packet_command *pc = (struct packet_command *)rq->buffer;
1325
1326 len = pc->buflen;
1327 if (len < 0) len = -len;
1328
1329 pc->stat = 0;
1330
1331
1332 cdrom_start_packet_command (drive, len, cdrom_do_pc_continuation);
1333 }
1334
1335
1336
1337
1338 static
1339 void cdrom_sleep (int time)
1340 {
1341 current->state = TASK_INTERRUPTIBLE;
1342 current->timeout = jiffies + time;
1343 schedule ();
1344 }
1345
1346 static
1347 int cdrom_queue_packet_command (ide_drive_t *drive, struct packet_command *pc)
1348 {
1349 struct atapi_request_sense my_reqbuf;
1350 int retries = 10;
1351 struct request req;
1352
1353
1354
1355 if (pc->sense_data == NULL)
1356 pc->sense_data = &my_reqbuf;
1357 pc->sense_data->sense_key = 0;
1358
1359
1360 do {
1361 ide_init_drive_cmd (&req);
1362 req.cmd = PACKET_COMMAND;
1363 req.buffer = (char *)pc;
1364 (void) ide_do_drive_cmd (drive, &req, ide_wait);
1365
1366 if (pc->stat != 0) {
1367
1368
1369
1370 struct atapi_request_sense *reqbuf = pc->sense_data;
1371
1372 if (reqbuf->sense_key == UNIT_ATTENTION)
1373 ;
1374 else if (reqbuf->sense_key == NOT_READY &&
1375 reqbuf->asc == 4) {
1376
1377
1378
1379 cdrom_sleep (HZ);
1380 } else
1381
1382 retries = 0;
1383
1384 --retries;
1385 }
1386
1387
1388 } while (pc->stat != 0 && retries >= 0);
1389
1390
1391
1392 if (pc->stat != 0)
1393 return -EIO;
1394 else {
1395
1396
1397
1398
1399
1400 if (CDROM_STATE_FLAGS (drive)->door_locked == 0 &&
1401 (pc->c[0] != REQUEST_SENSE &&
1402 pc->c[0] != ALLOW_MEDIUM_REMOVAL &&
1403 pc->c[0] != START_STOP)) {
1404 (void) cdrom_lockdoor (drive, 1, NULL);
1405 }
1406 return 0;
1407 }
1408 }
1409
1410
1411
1412
1413
1414
1415 void ide_do_rw_cdrom (ide_drive_t *drive, unsigned long block)
1416 {
1417 struct request *rq = HWGROUP(drive)->rq;
1418
1419 if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1420 cdrom_do_packet_command (drive);
1421 else if (rq -> cmd == RESET_DRIVE_COMMAND) {
1422 cdrom_end_request (1, drive);
1423 ide_do_reset (drive);
1424 return;
1425 } else if (rq -> cmd != READ) {
1426 printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1427 cdrom_end_request (0, drive);
1428 } else
1429 cdrom_start_read (drive, block);
1430 }
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444 #if ! STANDARD_ATAPI
1445 static inline
1446 int bin2bcd (int x)
1447 {
1448 return (x%10) | ((x/10) << 4);
1449 }
1450
1451
1452 static inline
1453 int bcd2bin (int x)
1454 {
1455 return (x >> 4) * 10 + (x & 0x0f);
1456 }
1457
1458 static
1459 void msf_from_bcd (struct atapi_msf *msf)
1460 {
1461 msf->minute = bcd2bin (msf->minute);
1462 msf->second = bcd2bin (msf->second);
1463 msf->frame = bcd2bin (msf->frame);
1464 }
1465
1466 #endif
1467
1468
1469 static inline
1470 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1471 {
1472 lba += CD_BLOCK_OFFSET;
1473 lba &= 0xffffff;
1474 *m = lba / (CD_SECS * CD_FRAMES);
1475 lba %= (CD_SECS * CD_FRAMES);
1476 *s = lba / CD_FRAMES;
1477 *f = lba % CD_FRAMES;
1478 }
1479
1480
1481 static inline
1482 int msf_to_lba (byte m, byte s, byte f)
1483 {
1484 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
1485 }
1486
1487
1488 static int
1489 cdrom_check_status (ide_drive_t *drive,
1490 struct atapi_request_sense *reqbuf)
1491 {
1492 struct packet_command pc;
1493
1494 memset (&pc, 0, sizeof (pc));
1495
1496 pc.sense_data = reqbuf;
1497 pc.c[0] = TEST_UNIT_READY;
1498
1499 return cdrom_queue_packet_command (drive, &pc);
1500 }
1501
1502
1503
1504 static int
1505 cdrom_lockdoor (ide_drive_t *drive, int lockflag,
1506 struct atapi_request_sense *reqbuf)
1507 {
1508 struct atapi_request_sense my_reqbuf;
1509 int stat;
1510 struct packet_command pc;
1511
1512 if (reqbuf == NULL)
1513 reqbuf = &my_reqbuf;
1514
1515
1516 if (CDROM_CONFIG_FLAGS (drive)->no_doorlock)
1517 stat = 0;
1518 else {
1519 memset (&pc, 0, sizeof (pc));
1520 pc.sense_data = reqbuf;
1521
1522 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1523 pc.c[4] = (lockflag != 0);
1524 stat = cdrom_queue_packet_command (drive, &pc);
1525 }
1526
1527 if (stat == 0)
1528 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1529 else {
1530
1531
1532 if (reqbuf->sense_key == ILLEGAL_REQUEST &&
1533 reqbuf->asc == 0x24) {
1534 printk ("%s: door locking not supported\n",
1535 drive->name);
1536 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
1537 stat = 0;
1538 CDROM_STATE_FLAGS (drive)->door_locked = lockflag;
1539 }
1540 }
1541 return stat;
1542 }
1543
1544
1545
1546
1547 static int
1548 cdrom_eject (ide_drive_t *drive, int ejectflag,
1549 struct atapi_request_sense *reqbuf)
1550 {
1551 struct packet_command pc;
1552
1553 memset (&pc, 0, sizeof (pc));
1554 pc.sense_data = reqbuf;
1555
1556 pc.c[0] = START_STOP;
1557 pc.c[4] = 2 + (ejectflag != 0);
1558 return cdrom_queue_packet_command (drive, &pc);
1559 }
1560
1561
1562 static int
1563 cdrom_pause (ide_drive_t *drive, int pauseflag,
1564 struct atapi_request_sense *reqbuf)
1565 {
1566 struct packet_command pc;
1567
1568 memset (&pc, 0, sizeof (pc));
1569 pc.sense_data = reqbuf;
1570
1571 pc.c[0] = SCMD_PAUSE_RESUME;
1572 pc.c[8] = !pauseflag;
1573 return cdrom_queue_packet_command (drive, &pc);
1574 }
1575
1576
1577 static int
1578 cdrom_startstop (ide_drive_t *drive, int startflag,
1579 struct atapi_request_sense *reqbuf)
1580 {
1581 struct packet_command pc;
1582
1583 memset (&pc, 0, sizeof (pc));
1584 pc.sense_data = reqbuf;
1585
1586 pc.c[0] = START_STOP;
1587 pc.c[1] = 1;
1588 pc.c[4] = startflag;
1589 return cdrom_queue_packet_command (drive, &pc);
1590 }
1591
1592
1593 static int
1594 cdrom_read_capacity (ide_drive_t *drive, unsigned *capacity,
1595 struct atapi_request_sense *reqbuf)
1596 {
1597 struct {
1598 unsigned lba;
1599 unsigned blocklen;
1600 } capbuf;
1601
1602 int stat;
1603 struct packet_command pc;
1604
1605 memset (&pc, 0, sizeof (pc));
1606 pc.sense_data = reqbuf;
1607
1608 pc.c[0] = READ_CAPACITY;
1609 pc.buffer = (char *)&capbuf;
1610 pc.buflen = sizeof (capbuf);
1611
1612 stat = cdrom_queue_packet_command (drive, &pc);
1613 if (stat == 0)
1614 *capacity = ntohl (capbuf.lba);
1615
1616 return stat;
1617 }
1618
1619
1620 static int
1621 cdrom_read_tocentry (ide_drive_t *drive, int trackno, int msf_flag,
1622 int format, char *buf, int buflen,
1623 struct atapi_request_sense *reqbuf)
1624 {
1625 struct packet_command pc;
1626
1627 memset (&pc, 0, sizeof (pc));
1628 pc.sense_data = reqbuf;
1629
1630 pc.buffer = buf;
1631 pc.buflen = buflen;
1632 pc.c[0] = SCMD_READ_TOC;
1633 pc.c[6] = trackno;
1634 pc.c[7] = (buflen >> 8);
1635 pc.c[8] = (buflen & 0xff);
1636 pc.c[9] = (format << 6);
1637 if (msf_flag) pc.c[1] = 2;
1638 return cdrom_queue_packet_command (drive, &pc);
1639 }
1640
1641
1642
1643 static int
1644 cdrom_read_toc (ide_drive_t *drive,
1645 struct atapi_request_sense *reqbuf)
1646 {
1647 int stat, ntracks, i;
1648 struct atapi_toc *toc = drive->cdrom_info.toc;
1649 struct {
1650 struct atapi_toc_header hdr;
1651 struct atapi_toc_entry ent;
1652 } ms_tmp;
1653
1654 if (toc == NULL) {
1655
1656 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1657 GFP_KERNEL);
1658 drive->cdrom_info.toc = toc;
1659 }
1660
1661 if (toc == NULL) {
1662 printk ("%s: No cdrom TOC buffer!\n", drive->name);
1663 return -EIO;
1664 }
1665
1666
1667
1668 if (CDROM_STATE_FLAGS (drive)->toc_valid)
1669 (void) cdrom_check_status (drive, NULL);
1670
1671 if (CDROM_STATE_FLAGS (drive)->toc_valid) return 0;
1672
1673
1674 stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1675 sizeof (struct atapi_toc_header) +
1676 sizeof (struct atapi_toc_entry),
1677 reqbuf);
1678 if (stat) return stat;
1679
1680 #if ! STANDARD_ATAPI
1681 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1682 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1683 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1684 }
1685 #endif
1686
1687 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1688 if (ntracks <= 0) return -EIO;
1689 if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1690
1691
1692 stat = cdrom_read_tocentry (drive, 0, 1, 0, (char *)&toc->hdr,
1693 sizeof (struct atapi_toc_header) +
1694 (ntracks+1) *
1695 sizeof (struct atapi_toc_entry),
1696 reqbuf);
1697 if (stat) return stat;
1698 toc->hdr.toc_length = ntohs (toc->hdr.toc_length);
1699
1700 #if ! STANDARD_ATAPI
1701 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd) {
1702 toc->hdr.first_track = bcd2bin (toc->hdr.first_track);
1703 toc->hdr.last_track = bcd2bin (toc->hdr.last_track);
1704 }
1705 #endif
1706
1707 for (i=0; i<=ntracks; i++) {
1708 #if ! STANDARD_ATAPI
1709 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd) {
1710 if (CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd)
1711 toc->ent[i].track = bcd2bin (toc->ent[i].track);
1712 msf_from_bcd (&toc->ent[i].addr.msf);
1713 }
1714 #endif
1715 toc->ent[i].addr.lba = msf_to_lba (toc->ent[i].addr.msf.minute,
1716 toc->ent[i].addr.msf.second,
1717 toc->ent[i].addr.msf.frame);
1718 }
1719
1720
1721 stat = cdrom_read_tocentry (drive, 0, 1, 1,
1722 (char *)&ms_tmp, sizeof (ms_tmp),
1723 reqbuf);
1724 if (stat) return stat;
1725
1726 #if ! STANDARD_ATAPI
1727 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
1728 msf_from_bcd (&ms_tmp.ent.addr.msf);
1729 #endif
1730
1731 toc->last_session_lba = msf_to_lba (ms_tmp.ent.addr.msf.minute,
1732 ms_tmp.ent.addr.msf.second,
1733 ms_tmp.ent.addr.msf.frame);
1734
1735 toc->xa_flag = (ms_tmp.hdr.first_track != ms_tmp.hdr.last_track);
1736
1737
1738 stat = cdrom_read_capacity (drive, &toc->capacity, reqbuf);
1739 if (stat) toc->capacity = 0x1fffff;
1740
1741 HWIF(drive)->gd->sizes[drive->select.b.unit << PARTN_BITS]
1742 = toc->capacity * SECTORS_PER_FRAME;
1743 drive->part[0].nr_sects = toc->capacity * SECTORS_PER_FRAME;
1744
1745
1746 CDROM_STATE_FLAGS (drive)->toc_valid = 1;
1747
1748 return 0;
1749 }
1750
1751
1752 static int
1753 cdrom_read_subchannel (ide_drive_t *drive, int format,
1754 char *buf, int buflen,
1755 struct atapi_request_sense *reqbuf)
1756 {
1757 struct packet_command pc;
1758
1759 memset (&pc, 0, sizeof (pc));
1760 pc.sense_data = reqbuf;
1761
1762 pc.buffer = buf;
1763 pc.buflen = buflen;
1764 pc.c[0] = SCMD_READ_SUBCHANNEL;
1765 pc.c[1] = 2;
1766 pc.c[2] = 0x40;
1767 pc.c[3] = format,
1768 pc.c[7] = (buflen >> 8);
1769 pc.c[8] = (buflen & 0xff);
1770 return cdrom_queue_packet_command (drive, &pc);
1771 }
1772
1773
1774
1775 static int
1776 cdrom_mode_sense (ide_drive_t *drive, int pageno, int modeflag,
1777 char *buf, int buflen,
1778 struct atapi_request_sense *reqbuf)
1779 {
1780 struct packet_command pc;
1781
1782 memset (&pc, 0, sizeof (pc));
1783 pc.sense_data = reqbuf;
1784
1785 pc.buffer = buf;
1786 pc.buflen = buflen;
1787 pc.c[0] = MODE_SENSE_10;
1788 pc.c[2] = pageno | (modeflag << 6);
1789 pc.c[7] = (buflen >> 8);
1790 pc.c[8] = (buflen & 0xff);
1791 return cdrom_queue_packet_command (drive, &pc);
1792 }
1793
1794
1795 static int
1796 cdrom_mode_select (ide_drive_t *drive, int pageno, char *buf, int buflen,
1797 struct atapi_request_sense *reqbuf)
1798 {
1799 struct packet_command pc;
1800
1801 memset (&pc, 0, sizeof (pc));
1802 pc.sense_data = reqbuf;
1803
1804 pc.buffer = buf;
1805 pc.buflen = - buflen;
1806 pc.c[0] = MODE_SELECT_10;
1807 pc.c[1] = 0x10;
1808 pc.c[2] = pageno;
1809 pc.c[7] = (buflen >> 8);
1810 pc.c[8] = (buflen & 0xff);
1811 return cdrom_queue_packet_command (drive, &pc);
1812 }
1813
1814
1815 static int
1816 cdrom_play_lba_range_1 (ide_drive_t *drive, int lba_start, int lba_end,
1817 struct atapi_request_sense *reqbuf)
1818 {
1819 struct packet_command pc;
1820
1821 memset (&pc, 0, sizeof (pc));
1822 pc.sense_data = reqbuf;
1823
1824 pc.c[0] = SCMD_PLAYAUDIO_MSF;
1825 lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1826 lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1827
1828 #if ! STANDARD_ATAPI
1829 if (CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd) {
1830 pc.c[3] = bin2bcd (pc.c[3]);
1831 pc.c[4] = bin2bcd (pc.c[4]);
1832 pc.c[5] = bin2bcd (pc.c[5]);
1833 pc.c[6] = bin2bcd (pc.c[6]);
1834 pc.c[7] = bin2bcd (pc.c[7]);
1835 pc.c[8] = bin2bcd (pc.c[8]);
1836 }
1837 #endif
1838
1839 return cdrom_queue_packet_command (drive, &pc);
1840 }
1841
1842
1843
1844
1845 static int
1846 cdrom_play_lba_range (ide_drive_t *drive, int lba_start, int lba_end,
1847 struct atapi_request_sense *reqbuf)
1848 {
1849 int i, stat;
1850 struct atapi_request_sense my_reqbuf;
1851
1852 if (reqbuf == NULL)
1853 reqbuf = &my_reqbuf;
1854
1855
1856
1857
1858
1859
1860 for (i=0; i<75; i++) {
1861 stat = cdrom_play_lba_range_1 (drive, lba_start, lba_end,
1862 reqbuf);
1863
1864 if (stat == 0 ||
1865 !(reqbuf->sense_key == ILLEGAL_REQUEST &&
1866 reqbuf->asc == 0x24))
1867 return stat;
1868
1869 --lba_end;
1870 if (lba_end <= lba_start) break;
1871 }
1872
1873 return stat;
1874 }
1875
1876
1877 static
1878 int cdrom_get_toc_entry (ide_drive_t *drive, int track,
1879 struct atapi_toc_entry **ent,
1880 struct atapi_request_sense *reqbuf)
1881 {
1882 int stat, ntracks;
1883 struct atapi_toc *toc;
1884
1885
1886 stat = cdrom_read_toc (drive, reqbuf);
1887 if (stat) return stat;
1888
1889 toc = drive->cdrom_info.toc;
1890
1891
1892 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1893 if (track == CDROM_LEADOUT)
1894 *ent = &toc->ent[ntracks];
1895 else if (track < toc->hdr.first_track ||
1896 track > toc->hdr.last_track)
1897 return -EINVAL;
1898 else
1899 *ent = &toc->ent[track - toc->hdr.first_track];
1900
1901 return 0;
1902 }
1903
1904
1905 static int
1906 cdrom_read_block (ide_drive_t *drive, int format, int lba,
1907 char *buf, int buflen,
1908 struct atapi_request_sense *reqbuf)
1909 {
1910 struct packet_command pc;
1911 struct atapi_request_sense my_reqbuf;
1912 int stat;
1913
1914 if (reqbuf == NULL)
1915 reqbuf = &my_reqbuf;
1916
1917 memset (&pc, 0, sizeof (pc));
1918 pc.sense_data = reqbuf;
1919
1920 pc.buffer = buf;
1921 pc.buflen = buflen;
1922
1923 #if ! STANDARD_ATAPI
1924 if (CDROM_CONFIG_FLAGS (drive)->old_readcd)
1925 pc.c[0] = 0xd4;
1926 else
1927 #endif
1928 pc.c[0] = READ_CD;
1929
1930 pc.c[1] = (format << 2);
1931 #ifdef __alpha__
1932 stl_u(htonl (lba), (unsigned int *) &pc.c[2]);
1933 #else
1934 *(int *)(&pc.c[2]) = htonl (lba);
1935 #endif
1936 pc.c[8] = 1;
1937 pc.c[9] = 0x10;
1938
1939 stat = cdrom_queue_packet_command (drive, &pc);
1940
1941 #if ! STANDARD_ATAPI
1942
1943
1944 if (stat && reqbuf->sense_key == ILLEGAL_REQUEST &&
1945 reqbuf->asc == 0x20 &&
1946 CDROM_CONFIG_FLAGS (drive)->old_readcd == 0) {
1947 printk ("%s: Drive does not recognize READ_CD;"
1948 "trying opcode 0xd4\n",
1949 drive->name);
1950 CDROM_CONFIG_FLAGS (drive)->old_readcd = 1;
1951 return cdrom_read_block (drive, format, lba, buf, buflen,
1952 reqbuf);
1953 }
1954 #endif
1955
1956 return stat;
1957 }
1958
1959
1960 int ide_cdrom_ioctl (ide_drive_t *drive, struct inode *inode,
1961 struct file *file, unsigned int cmd, unsigned long arg)
1962 {
1963 switch (cmd) {
1964 case CDROMEJECT: {
1965 int stat;
1966
1967 if (drive->usage > 1)
1968 return -EBUSY;
1969
1970 stat = cdrom_lockdoor (drive, 0, NULL);
1971 if (stat) return stat;
1972
1973 return cdrom_eject (drive, 0, NULL);
1974 }
1975
1976 case CDROMCLOSETRAY: {
1977 int stat;
1978 if (drive->usage > 1)
1979 return -EBUSY;
1980
1981 stat = cdrom_eject (drive, 1, NULL);
1982 if (stat) return stat;
1983
1984 return cdrom_lockdoor (drive, 1, NULL);
1985 }
1986
1987 case CDROMEJECT_SW: {
1988 CDROM_STATE_FLAGS (drive)->eject_on_close = arg;
1989 return 0;
1990 }
1991
1992 case CDROMPAUSE:
1993 return cdrom_pause (drive, 1, NULL);
1994
1995 case CDROMRESUME:
1996 return cdrom_pause (drive, 0, NULL);
1997
1998 case CDROMSTART:
1999 return cdrom_startstop (drive, 1, NULL);
2000
2001 case CDROMSTOP: {
2002 int stat;
2003
2004 stat = cdrom_startstop (drive, 0, NULL);
2005 if (stat) return stat;
2006
2007 return cdrom_eject (drive, 1, NULL);
2008 }
2009
2010 case CDROMPLAYMSF: {
2011 struct cdrom_msf msf;
2012 int stat, lba_start, lba_end;
2013
2014 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
2015 if (stat) return stat;
2016
2017 memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
2018
2019 lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
2020 msf.cdmsf_frame0);
2021 lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
2022 msf.cdmsf_frame1) + 1;
2023
2024 if (lba_end <= lba_start) return -EINVAL;
2025
2026 return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2027 }
2028
2029
2030
2031 case CDROMPLAYTRKIND: {
2032 int stat, lba_start, lba_end;
2033 struct cdrom_ti ti;
2034 struct atapi_toc_entry *first_toc, *last_toc;
2035
2036 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
2037 if (stat) return stat;
2038
2039 memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
2040
2041 stat = cdrom_get_toc_entry (drive, ti.cdti_trk0, &first_toc,
2042 NULL);
2043 if (stat) return stat;
2044 stat = cdrom_get_toc_entry (drive, ti.cdti_trk1, &last_toc,
2045 NULL);
2046 if (stat) return stat;
2047
2048 if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
2049 lba_start = first_toc->addr.lba;
2050 lba_end = last_toc->addr.lba;
2051
2052 if (lba_end <= lba_start) return -EINVAL;
2053
2054 return cdrom_play_lba_range (drive, lba_start, lba_end, NULL);
2055 }
2056
2057 case CDROMREADTOCHDR: {
2058 int stat;
2059 struct cdrom_tochdr tochdr;
2060 struct atapi_toc *toc;
2061
2062 stat = verify_area (VERIFY_WRITE, (void *) arg,
2063 sizeof (tochdr));
2064 if (stat) return stat;
2065
2066
2067 stat = cdrom_read_toc (drive, NULL);
2068 if (stat) return stat;
2069
2070 toc = drive->cdrom_info.toc;
2071 tochdr.cdth_trk0 = toc->hdr.first_track;
2072 tochdr.cdth_trk1 = toc->hdr.last_track;
2073
2074 memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
2075
2076 return stat;
2077 }
2078
2079 case CDROMREADTOCENTRY: {
2080 int stat;
2081 struct cdrom_tocentry tocentry;
2082 struct atapi_toc_entry *toce;
2083
2084 stat = verify_area (VERIFY_WRITE, (void *) arg,
2085 sizeof (tocentry));
2086 if (stat) return stat;
2087
2088 memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
2089
2090 stat = cdrom_get_toc_entry (drive, tocentry.cdte_track, &toce,
2091 NULL);
2092 if (stat) return stat;
2093
2094 tocentry.cdte_ctrl = toce->control;
2095 tocentry.cdte_adr = toce->adr;
2096
2097 if (tocentry.cdte_format == CDROM_MSF) {
2098
2099 lba_to_msf (toce->addr.lba,
2100 &tocentry.cdte_addr.msf.minute,
2101 &tocentry.cdte_addr.msf.second,
2102 &tocentry.cdte_addr.msf.frame);
2103 } else
2104 tocentry.cdte_addr.lba = toce->addr.lba;
2105
2106 memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
2107
2108 return stat;
2109 }
2110
2111 case CDROMSUBCHNL: {
2112 struct atapi_cdrom_subchnl scbuf;
2113 int stat;
2114 struct cdrom_subchnl subchnl;
2115
2116 stat = verify_area (VERIFY_WRITE, (void *) arg,
2117 sizeof (subchnl));
2118 if (stat) return stat;
2119
2120 memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
2121
2122 stat = cdrom_read_subchannel (drive, 1,
2123 (char *)&scbuf, sizeof (scbuf),
2124 NULL);
2125 if (stat) return stat;
2126
2127 #if ! STANDARD_ATAPI
2128 if (CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd) {
2129 msf_from_bcd (&scbuf.acdsc_absaddr.msf);
2130 msf_from_bcd (&scbuf.acdsc_reladdr.msf);
2131 }
2132 if (CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd)
2133 scbuf.acdsc_trk = bcd2bin (scbuf.acdsc_trk);
2134 #endif
2135
2136 if (subchnl.cdsc_format == CDROM_MSF) {
2137 subchnl.cdsc_absaddr.msf.minute =
2138 scbuf.acdsc_absaddr.msf.minute;
2139 subchnl.cdsc_absaddr.msf.second =
2140 scbuf.acdsc_absaddr.msf.second;
2141 subchnl.cdsc_absaddr.msf.frame =
2142 scbuf.acdsc_absaddr.msf.frame;
2143
2144 subchnl.cdsc_reladdr.msf.minute =
2145 scbuf.acdsc_reladdr.msf.minute;
2146 subchnl.cdsc_reladdr.msf.second =
2147 scbuf.acdsc_reladdr.msf.second;
2148 subchnl.cdsc_reladdr.msf.frame =
2149 scbuf.acdsc_reladdr.msf.frame;
2150 } else {
2151 subchnl.cdsc_absaddr.lba =
2152 msf_to_lba (scbuf.acdsc_absaddr.msf.minute,
2153 scbuf.acdsc_absaddr.msf.second,
2154 scbuf.acdsc_absaddr.msf.frame);
2155 subchnl.cdsc_reladdr.lba =
2156 msf_to_lba (scbuf.acdsc_reladdr.msf.minute,
2157 scbuf.acdsc_reladdr.msf.second,
2158 scbuf.acdsc_reladdr.msf.frame);
2159 }
2160
2161 subchnl.cdsc_audiostatus = scbuf.acdsc_audiostatus;
2162 subchnl.cdsc_ctrl = scbuf.acdsc_ctrl;
2163 subchnl.cdsc_trk = scbuf.acdsc_trk;
2164 subchnl.cdsc_ind = scbuf.acdsc_ind;
2165
2166 memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
2167
2168 return stat;
2169 }
2170
2171 case CDROMVOLCTRL: {
2172 struct cdrom_volctrl volctrl;
2173 char buffer[24], mask[24];
2174 int stat;
2175
2176 stat = verify_area (VERIFY_READ, (void *) arg,
2177 sizeof (volctrl));
2178 if (stat) return stat;
2179 memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
2180
2181 stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2182 sizeof (buffer), NULL);
2183 if (stat) return stat;
2184 stat = cdrom_mode_sense (drive, 0x0e, 1, mask,
2185 sizeof (buffer), NULL);
2186 if (stat) return stat;
2187
2188 buffer[1] = buffer[2] = 0;
2189
2190 buffer[17] = volctrl.channel0 & mask[17];
2191 buffer[19] = volctrl.channel1 & mask[19];
2192 buffer[21] = volctrl.channel2 & mask[21];
2193 buffer[23] = volctrl.channel3 & mask[23];
2194
2195 return cdrom_mode_select (drive, 0x0e, buffer,
2196 sizeof (buffer), NULL);
2197 }
2198
2199 case CDROMVOLREAD: {
2200 struct cdrom_volctrl volctrl;
2201 char buffer[24];
2202 int stat;
2203
2204 stat = verify_area (VERIFY_WRITE, (void *) arg,
2205 sizeof (volctrl));
2206 if (stat) return stat;
2207
2208 stat = cdrom_mode_sense (drive, 0x0e, 0, buffer,
2209 sizeof (buffer), NULL);
2210 if (stat) return stat;
2211
2212 volctrl.channel0 = buffer[17];
2213 volctrl.channel1 = buffer[19];
2214 volctrl.channel2 = buffer[21];
2215 volctrl.channel3 = buffer[23];
2216
2217 memcpy_tofs ((void *) arg, &volctrl, sizeof (volctrl));
2218
2219 return 0;
2220 }
2221
2222 case CDROMMULTISESSION: {
2223 struct cdrom_multisession ms_info;
2224 struct atapi_toc *toc;
2225 int stat;
2226
2227 stat = verify_area (VERIFY_WRITE, (void *)arg,
2228 sizeof (ms_info));
2229 if (stat) return stat;
2230
2231 memcpy_fromfs (&ms_info, (void *)arg, sizeof (ms_info));
2232
2233
2234 stat = cdrom_read_toc (drive, NULL);
2235 if (stat) return stat;
2236
2237 toc = drive->cdrom_info.toc;
2238
2239 if (ms_info.addr_format == CDROM_MSF)
2240 lba_to_msf (toc->last_session_lba,
2241 &ms_info.addr.msf.minute,
2242 &ms_info.addr.msf.second,
2243 &ms_info.addr.msf.frame);
2244 else if (ms_info.addr_format == CDROM_LBA)
2245 ms_info.addr.lba = toc->last_session_lba;
2246 else
2247 return -EINVAL;
2248
2249 ms_info.xa_flag = toc->xa_flag;
2250
2251 memcpy_tofs ((void *)arg, &ms_info, sizeof (ms_info));
2252
2253 return 0;
2254 }
2255
2256
2257 case CDROMREADAUDIO: {
2258 int stat, lba;
2259 struct atapi_toc *toc;
2260 struct cdrom_read_audio ra;
2261 char buf[CD_FRAMESIZE_RAW];
2262
2263
2264 stat = cdrom_read_toc (drive, NULL);
2265 if (stat) return stat;
2266
2267 toc = drive->cdrom_info.toc;
2268
2269 stat = verify_area (VERIFY_READ, (char *)arg, sizeof (ra));
2270 if (stat) return stat;
2271
2272 memcpy_fromfs (&ra, (void *)arg, sizeof (ra));
2273
2274 if (ra.nframes < 0 || ra.nframes > toc->capacity)
2275 return -EINVAL;
2276 else if (ra.nframes == 0)
2277 return 0;
2278
2279 stat = verify_area (VERIFY_WRITE, (char *)ra.buf,
2280 ra.nframes * CD_FRAMESIZE_RAW);
2281 if (stat) return stat;
2282
2283 if (ra.addr_format == CDROM_MSF)
2284 lba = msf_to_lba (ra.addr.msf.minute,
2285 ra.addr.msf.second,
2286 ra.addr.msf.frame);
2287 else if (ra.addr_format == CDROM_LBA)
2288 lba = ra.addr.lba;
2289 else
2290 return -EINVAL;
2291
2292 if (lba < 0 || lba >= toc->capacity)
2293 return -EINVAL;
2294
2295 while (ra.nframes > 0) {
2296 stat = cdrom_read_block (drive, 1, lba, buf,
2297 CD_FRAMESIZE_RAW, NULL);
2298 if (stat) return stat;
2299 memcpy_tofs (ra.buf, buf, CD_FRAMESIZE_RAW);
2300 ra.buf += CD_FRAMESIZE_RAW;
2301 --ra.nframes;
2302 ++lba;
2303 }
2304
2305 return 0;
2306 }
2307
2308 case CDROMREADMODE1:
2309 case CDROMREADMODE2: {
2310 struct cdrom_msf msf;
2311 int blocksize, format, stat, lba;
2312 struct atapi_toc *toc;
2313 char buf[CD_FRAMESIZE_RAW0];
2314
2315 if (cmd == CDROMREADMODE1) {
2316 blocksize = CD_FRAMESIZE;
2317 format = 2;
2318 } else {
2319 blocksize = CD_FRAMESIZE_RAW0;
2320 format = 3;
2321 }
2322
2323 stat = verify_area (VERIFY_WRITE, (char *)arg, blocksize);
2324 if (stat) return stat;
2325
2326 memcpy_fromfs (&msf, (void *)arg, sizeof (msf));
2327
2328 lba = msf_to_lba (msf.cdmsf_min0,
2329 msf.cdmsf_sec0,
2330 msf.cdmsf_frame0);
2331
2332
2333 stat = cdrom_read_toc (drive, NULL);
2334 if (stat) return stat;
2335
2336 toc = drive->cdrom_info.toc;
2337
2338 if (lba < 0 || lba >= toc->capacity)
2339 return -EINVAL;
2340
2341 stat = cdrom_read_block (drive, format, lba, buf, blocksize,
2342 NULL);
2343 if (stat) return stat;
2344
2345 memcpy_tofs ((char *)arg, buf, blocksize);
2346 return 0;
2347 }
2348
2349 case CDROM_GET_UPC: {
2350 int stat;
2351 char mcnbuf[24];
2352 struct cdrom_mcn mcn;
2353
2354 stat = verify_area (VERIFY_WRITE, (void *) arg,
2355 sizeof (mcn));
2356 if (stat) return stat;
2357
2358 stat = cdrom_read_subchannel (drive, 2,
2359 mcnbuf, sizeof (mcnbuf),
2360 NULL);
2361 if (stat) return stat;
2362
2363 memcpy (mcn.medium_catalog_number, mcnbuf+9,
2364 sizeof (mcn.medium_catalog_number)-1);
2365 mcn.medium_catalog_number[sizeof (mcn.medium_catalog_number)-1]
2366 = '\0';
2367
2368 memcpy_tofs ((void *) arg, &mcn, sizeof (mcn));
2369
2370 return stat;
2371 }
2372
2373 #if 0
2374 case CDROMRESET: {
2375 struct request req;
2376 ide_init_drive_cmd (&req);
2377 req.cmd = RESET_DRIVE_COMMAND;
2378 return ide_do_drive_cmd (drive, &req, ide_wait);
2379 }
2380 #endif
2381
2382
2383 #ifdef TEST
2384 case 0x1234: {
2385 int stat;
2386 struct packet_command pc;
2387 int len, lena;
2388
2389 memset (&pc, 0, sizeof (pc));
2390
2391 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
2392 if (stat) return stat;
2393 memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
2394 arg += sizeof (pc.c);
2395
2396 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (len));
2397 if (stat) return stat;
2398 memcpy_fromfs (&len, (void *) arg , sizeof (len));
2399 arg += sizeof (len);
2400
2401 if (len > 0) {
2402 stat = verify_area (VERIFY_WRITE, (void *) arg, len);
2403 if (stat) return stat;
2404 }
2405
2406 lena = len;
2407 if (lena < 0) lena = 0;
2408
2409 {
2410 char buf[lena];
2411 if (len > 0) {
2412 pc.buflen = len;
2413 pc.buffer = buf;
2414 }
2415
2416 stat = cdrom_queue_packet_command (drive, &pc);
2417
2418 if (len > 0)
2419 memcpy_tofs ((void *)arg, buf, len);
2420 }
2421
2422 return stat;
2423 }
2424 #endif
2425
2426 default:
2427 return -EPERM;
2428 }
2429
2430 }
2431
2432
2433
2434
2435
2436
2437
2438 int ide_cdrom_check_media_change (ide_drive_t *drive)
2439 {
2440 int retval;
2441
2442 (void) cdrom_check_status (drive, NULL);
2443
2444 retval = CDROM_STATE_FLAGS (drive)->media_changed;
2445 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2446
2447 return retval;
2448 }
2449
2450
2451 int ide_cdrom_open (struct inode *ip, struct file *fp, ide_drive_t *drive)
2452 {
2453
2454 if (fp->f_mode & 2) {
2455 --drive->usage;
2456 return -EROFS;
2457 }
2458
2459
2460 if (drive->usage == 1) {
2461 int stat;
2462 struct atapi_request_sense my_reqbuf;
2463 my_reqbuf.sense_key = 0;
2464
2465
2466 stat = cdrom_check_status (drive, &my_reqbuf);
2467
2468
2469 if (stat && my_reqbuf.sense_key == NOT_READY) {
2470 cdrom_eject (drive, 1, &my_reqbuf);
2471 stat = cdrom_check_status (drive, &my_reqbuf);
2472 }
2473
2474
2475 if (stat && my_reqbuf.sense_key != UNIT_ATTENTION) {
2476 --drive->usage;
2477 return -ENXIO;
2478 }
2479
2480
2481 (void) cdrom_lockdoor (drive, 1, &my_reqbuf);
2482
2483
2484 (void) cdrom_read_toc (drive, &my_reqbuf);
2485 }
2486
2487 return 0;
2488 }
2489
2490
2491
2492
2493
2494
2495 void ide_cdrom_release (struct inode *inode, struct file *file,
2496 ide_drive_t *drive)
2497 {
2498 if (drive->usage == 0) {
2499 invalidate_buffers (inode->i_rdev);
2500
2501
2502 (void) cdrom_lockdoor (drive, 0, NULL);
2503
2504
2505 if (CDROM_STATE_FLAGS (drive)->eject_on_close)
2506 (void) cdrom_eject (drive, 0, NULL);
2507 }
2508 }
2509
2510
2511
2512
2513
2514
2515
2516 void ide_cdrom_setup (ide_drive_t *drive)
2517 {
2518 blksize_size[HWIF(drive)->major][drive->select.b.unit << PARTN_BITS] =
2519 CD_FRAMESIZE;
2520
2521 drive->special.all = 0;
2522 drive->ready_stat = 0;
2523
2524 CDROM_STATE_FLAGS (drive)->media_changed = 0;
2525 CDROM_STATE_FLAGS (drive)->toc_valid = 0;
2526 CDROM_STATE_FLAGS (drive)->door_locked = 0;
2527
2528
2529 CDROM_STATE_FLAGS (drive)->eject_on_close= 0;
2530
2531 #if NO_DOOR_LOCKING
2532 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 1;
2533 #else
2534 CDROM_CONFIG_FLAGS (drive)->no_doorlock = 0;
2535 #endif
2536
2537 if (drive->id != NULL)
2538 CDROM_CONFIG_FLAGS (drive)->drq_interrupt =
2539 ((drive->id->config & 0x0060) == 0x20);
2540 else
2541 CDROM_CONFIG_FLAGS (drive)->drq_interrupt = 0;
2542
2543 #if ! STANDARD_ATAPI
2544 CDROM_CONFIG_FLAGS (drive)->old_readcd = 0;
2545 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 0;
2546 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 0;
2547 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 0;
2548 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 0;
2549
2550 if (drive->id != NULL) {
2551 if (strcmp (drive->id->model, "V003S0DS") == 0 &&
2552 drive->id->fw_rev[4] == '1' &&
2553 drive->id->fw_rev[6] <= '2') {
2554
2555
2556 CDROM_CONFIG_FLAGS (drive)->toctracks_as_bcd = 1;
2557 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2558 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2559 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2560 }
2561
2562 else if (strcmp (drive->id->model,
2563 "NEC CD-ROM DRIVE:260") == 0 &&
2564 strcmp (drive->id->fw_rev, "1.01") == 0) {
2565
2566 CDROM_CONFIG_FLAGS (drive)->tocaddr_as_bcd = 1;
2567 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2568 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2569 }
2570
2571 else if (strcmp (drive->id->model, "WEARNES CDD-120") == 0 &&
2572 strcmp (drive->id->fw_rev, "A1.1") == 0) {
2573
2574 CDROM_CONFIG_FLAGS (drive)->playmsf_as_bcd = 1;
2575 CDROM_CONFIG_FLAGS (drive)->subchan_as_bcd = 1;
2576 }
2577 }
2578 #endif
2579
2580 drive->cdrom_info.toc = NULL;
2581 drive->cdrom_info.sector_buffer = NULL;
2582 drive->cdrom_info.sector_buffered = 0;
2583 drive->cdrom_info.nsectors_buffered = 0;
2584 }
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607