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