This source file includes following definitions.
- 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_queue_packet_command
- do_rw_cdrom
- byte_swap_word
- byte_swap_long
- bin2bcd
- lba_to_msf
- msf_to_lba
- cdrom_check_status
- cdrom_request_sense
- cdrom_lockdoor
- cdrom_eject
- cdrom_pause
- cdrom_startstop
- 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
- cdrom_get_toc_entry
- ide_cdrom_ioctl
- cdrom_check_media_change
- cdrom_open
- cdrom_release
- 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 #include <linux/cdrom.h>
37
38 #define SECTOR_SIZE 512
39 #define SECTOR_BITS 9
40 #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
41
42 #define MIN(a,b) ((a) < (b) ? (a) : (b))
43
44 #define OUT_WORDS(b,n) outsw (IDE_PORT (HD_DATA, dev->hwif), (b), (n))
45 #define IN_WORDS(b,n) insw (IDE_PORT (HD_DATA, dev->hwif), (b), (n))
46
47
48 #define PACKET_COMMAND 4315
49
50 #define WIN_PACKETCMD 0xa0
51
52
53
54 #define TEST_UNIT_READY 0x00
55 #define REQUEST_SENSE 0x03
56 #define START_STOP 0x1b
57 #define ALLOW_MEDIUM_REMOVAL 0x1e
58 #define READ_10 0x28
59 #define MODE_SENSE_10 0x5a
60 #define MODE_SELECT_10 0x55
61
62 struct packet_command {
63 char *buffer;
64 int buflen;
65 int stat;
66 unsigned char c[12];
67 };
68
69
70 struct atapi_request_sense {
71 unsigned char error_code : 7;
72 unsigned char valid : 1;
73 byte reserved1;
74 unsigned char sense_key : 4;
75 unsigned char reserved2 : 1;
76 unsigned char ili : 1;
77 unsigned char reserved3 : 2;
78 byte info[4];
79 byte sense_len;
80 byte command_info[4];
81 byte asc;
82 byte ascq;
83 byte fru;
84 byte sense_key_specific[3];
85 };
86
87
88
89
90
91 struct ide_cd_flags {
92 unsigned drq_interrupt : 1;
93
94 unsigned no_playaudio12: 1;
95
96 unsigned media_changed : 1;
97 unsigned toc_valid : 1;
98 unsigned no_lba_toc : 1;
99 unsigned reserved : 3;
100 };
101
102 #define CDROM_FLAGS(dev) ((struct ide_cd_flags *)&((dev)->bios_sect))
103
104
105
106
107 #define MAX_TRACKS 99
108 struct atapi_toc_header {
109 unsigned short toc_length;
110 byte first_track;
111 byte last_track;
112 };
113
114 struct atapi_toc_entry {
115 byte reserved1;
116 unsigned control : 4;
117 unsigned adr : 4;
118 byte track;
119 byte reserved2;
120 unsigned lba;
121 };
122
123 struct atapi_toc {
124 struct atapi_toc_header hdr;
125 struct atapi_toc_entry ent[MAX_TRACKS+1];
126 };
127
128
129 #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
130
131
132 struct cdrom_info {
133
134
135
136
137 struct atapi_toc *toc;
138
139
140
141
142
143
144
145
146 unsigned long sector_buffered;
147 unsigned long nsectors_buffered;
148 char *sector_buffer;
149 };
150
151
152 static struct cdrom_info cdrom_info[2][MAX_DRIVES];
153
154
155
156
157
158
159
160
161 static void cdrom_end_request (int uptodate, ide_dev_t *dev)
162 {
163 struct request *rq = ide_cur_rq[dev->hwif];
164
165
166
167 if (!uptodate && rq->bh != 0)
168 {
169 int adj = rq->current_nr_sectors - 1;
170 rq->current_nr_sectors -= adj;
171 rq->sector += adj;
172 }
173
174 end_request (uptodate, dev->hwif);
175 }
176
177
178
179
180 static void cdrom_saw_media_change (ide_dev_t *dev)
181 {
182 CDROM_FLAGS (dev)->media_changed = 1;
183 CDROM_FLAGS (dev)->toc_valid = 0;
184 cdrom_info[dev->hwif][dev->select.b.drive].nsectors_buffered = 0;
185 }
186
187
188
189
190 static int cdrom_decode_status (ide_dev_t *dev, int good_stat, int *stat_ret)
191 {
192 struct request *rq = ide_cur_rq[dev->hwif];
193 int stat, err;
194
195
196 stat = GET_STAT (dev->hwif);
197 *stat_ret = stat;
198
199 if (OK_STAT (stat, good_stat, BAD_RW_STAT))
200 return 0;
201
202
203 err = IN_BYTE (HD_ERROR, dev->hwif);
204
205
206 if ((err & 0xf0) == 0x20)
207 {
208 struct packet_command *pc;
209 cdrom_saw_media_change (dev);
210
211
212 if (rq->cmd == READ)
213 {
214 printk ("%s : tray open\n", dev->name);
215 cdrom_end_request (0, dev);
216 }
217
218 else
219 {
220
221
222
223
224
225
226 pc = (struct packet_command *)rq->buffer;
227 if (pc->c[0] != SCMD_READ_SUBCHANNEL)
228 printk ("%s : tray open\n", dev->name);
229
230
231 pc->stat = 1;
232 cdrom_end_request (1, dev);
233 }
234 }
235
236
237 else if ((err & 0xf0) == 0x60)
238 {
239 cdrom_saw_media_change (dev);
240 printk ("%s: media changed\n", dev->name);
241
242
243
244 if ((++rq->errors > ERROR_MAX))
245 {
246 cdrom_end_request (0, dev);
247 }
248 }
249
250
251 else if (rq->cmd == PACKET_COMMAND)
252 {
253 struct packet_command *pc = (struct packet_command *)rq->buffer;
254 dump_status (dev->hwif, "packet command error", stat);
255 pc->stat = 1;
256 cdrom_end_request (1, dev);
257 }
258
259
260 else if ((err & ~ABRT_ERR) != 0)
261 {
262 ide_error (dev, "cdrom_decode_status", stat);
263 }
264
265
266 else if ((++rq->errors > ERROR_MAX))
267 {
268 cdrom_end_request (0, dev);
269 }
270
271
272 DO_REQUEST;
273 return 1;
274 }
275
276
277
278
279
280
281 static int cdrom_start_packet_command (ide_dev_t *dev, int xferlen)
282 {
283
284 if (wait_stat (dev, 0, BUSY_STAT, WAIT_READY)) return 1;
285
286
287 OUT_BYTE (0, HD_FEATURE);
288 OUT_BYTE (0, HD_NSECTOR);
289 OUT_BYTE (0, HD_SECTOR);
290
291 OUT_BYTE (xferlen & 0xff, HD_LCYL);
292 OUT_BYTE (xferlen >> 8 , HD_HCYL);
293 OUT_BYTE (dev->ctl, HD_CMD);
294 OUT_BYTE (WIN_PACKETCMD, HD_COMMAND);
295
296 return 0;
297 }
298
299
300
301
302
303 static int cdrom_transfer_packet_command (ide_dev_t *dev,
304 char *cmd_buf, int cmd_len)
305 {
306 if (CDROM_FLAGS (dev)->drq_interrupt)
307 {
308
309
310 int stat_dum;
311
312
313 if (cdrom_decode_status (dev, DRQ_STAT, &stat_dum)) return 1;
314 }
315 else
316 {
317
318 if (wait_stat (dev, DRQ_STAT, BAD_STAT, WAIT_READY)) return 1;
319 }
320
321
322 OUT_WORDS (cmd_buf, cmd_len/2);
323
324 return 0;
325 }
326
327
328
329
330
331
332
333
334
335
336
337 static void cdrom_buffer_sectors (ide_dev_t *dev, unsigned long sector,
338 int sectors_to_transfer)
339 {
340 struct cdrom_info *info = &cdrom_info[dev->hwif][dev->select.b.drive];
341
342
343 int sectors_to_buffer = MIN (sectors_to_transfer,
344 (SECTOR_BUFFER_SIZE >> SECTOR_BITS));
345
346 char *dest;
347
348
349
350
351 if (info->sector_buffer == NULL)
352 {
353 info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE, GFP_ATOMIC);
354
355
356 if (info->sector_buffer == NULL)
357 sectors_to_buffer = 0;
358 }
359
360
361 info->sector_buffered = sector;
362 info->nsectors_buffered = sectors_to_buffer;
363
364
365 dest = info->sector_buffer;
366 while (sectors_to_buffer > 0)
367 {
368 IN_WORDS (dest, SECTOR_SIZE / 2);
369 --sectors_to_buffer;
370 --sectors_to_transfer;
371 dest += SECTOR_SIZE;
372 }
373
374
375 while (sectors_to_transfer > 0)
376 {
377 char dum[SECTOR_SIZE];
378 IN_WORDS (dest, sizeof (dum) / 2);
379 --sectors_to_transfer;
380 }
381 }
382
383
384
385
386
387
388
389 static inline
390 int cdrom_read_check_ireason (ide_dev_t *dev, int len, int ireason)
391 {
392 ireason &= 3;
393 if (ireason == 2) return 0;
394
395 if (ireason == 0)
396 {
397
398 printk ("%s: cdrom_read_intr: "
399 "Drive wants to transfer data the wrong way!\n",
400 dev->name);
401
402
403
404 while (len > 0)
405 {
406 short dum = 0;
407 OUT_WORDS (&dum, 1);
408 len -= 2;
409 }
410 }
411
412 else
413 {
414
415 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
416 dev->name, ireason);
417 }
418
419 cdrom_end_request (0, dev);
420 DO_REQUEST;
421 return -1;
422 }
423
424
425
426
427
428 static void cdrom_read_intr (ide_dev_t *dev)
429 {
430 int stat;
431 int ireason, len, sectors_to_transfer, nskip;
432
433 struct request *rq = ide_cur_rq[dev->hwif];
434
435
436 if (cdrom_decode_status (dev, 0, &stat)) return;
437
438
439 ireason = IN_BYTE (HD_NSECTOR, dev->hwif);
440 len = IN_BYTE (HD_LCYL, dev->hwif) + 256 * IN_BYTE (HD_HCYL, dev->hwif);
441
442
443 if ((stat & DRQ_STAT) == 0)
444 {
445
446
447 if (rq->current_nr_sectors > 0)
448 {
449 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
450 dev->name, rq->current_nr_sectors);
451 cdrom_end_request (0, dev);
452 }
453 else
454 cdrom_end_request (1, dev);
455
456 DO_REQUEST;
457 return;
458 }
459
460
461 if (cdrom_read_check_ireason (dev, len, ireason)) return;
462
463
464
465 if ((len % SECTOR_SIZE) != 0)
466 {
467 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
468 dev->name, len);
469 printk (" This drive is not supported by this version of the driver\n");
470 cdrom_end_request (0, dev);
471 DO_REQUEST;
472 return;
473 }
474
475
476 sectors_to_transfer = len / SECTOR_SIZE;
477
478
479 nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
480 sectors_to_transfer);
481
482 while (nskip > 0)
483 {
484
485 char dum[SECTOR_SIZE];
486 IN_WORDS (dum, sizeof (dum) / 2);
487
488 --rq->current_nr_sectors;
489 --nskip;
490 --sectors_to_transfer;
491 }
492
493
494 while (sectors_to_transfer > 0)
495 {
496 int this_transfer;
497
498
499
500 if (rq->current_nr_sectors == 0 &&
501 rq->nr_sectors > 0)
502 cdrom_end_request (1, dev);
503
504
505
506 if (rq->current_nr_sectors == 0)
507 {
508 cdrom_buffer_sectors (dev, rq->sector, sectors_to_transfer);
509 sectors_to_transfer = 0;
510 }
511 else
512 {
513
514
515
516 this_transfer = MIN (sectors_to_transfer,
517 rq->current_nr_sectors);
518
519
520 while (this_transfer > 0)
521 {
522 IN_WORDS (rq->buffer, SECTOR_SIZE / 2);
523 rq->buffer += SECTOR_SIZE;
524 --rq->nr_sectors;
525 --rq->current_nr_sectors;
526 ++rq->sector;
527 --this_transfer;
528 --sectors_to_transfer;
529 }
530 }
531 }
532
533
534
535 ide_handler[dev->hwif] = cdrom_read_intr;
536 }
537
538
539
540
541
542
543 static int cdrom_read_from_buffer (ide_dev_t *dev)
544 {
545 struct cdrom_info *info = &cdrom_info[dev->hwif][dev->select.b.drive];
546 struct request *rq = ide_cur_rq[dev->hwif];
547
548
549 if (info->sector_buffer == NULL) return 0;
550
551
552
553 while (rq->nr_sectors > 0 &&
554 rq->sector >= info->sector_buffered &&
555 rq->sector < info->sector_buffered + info->nsectors_buffered)
556 {
557 if (rq->current_nr_sectors == 0)
558 cdrom_end_request (1, dev);
559
560 memcpy (rq->buffer,
561 info->sector_buffer +
562 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
563 SECTOR_SIZE);
564 rq->buffer += SECTOR_SIZE;
565 --rq->current_nr_sectors;
566 --rq->nr_sectors;
567 ++rq->sector;
568 }
569
570
571 if (rq->nr_sectors == 0)
572 {
573 cdrom_end_request (1, dev);
574 return -1;
575 }
576
577
578 if (rq->current_nr_sectors == 0)
579 cdrom_end_request (1, dev);
580
581
582
583
584
585 if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
586 (rq->sector % SECTORS_PER_FRAME) != 0)
587 {
588 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
589 dev->name, rq->sector);
590 cdrom_end_request (0, dev);
591 return -1;
592 }
593
594 return 0;
595 }
596
597
598
599
600
601
602
603
604
605 static int cdrom_start_read_continuation (ide_dev_t *dev)
606 {
607 struct packet_command pc;
608 struct request *rq = ide_cur_rq[dev->hwif];
609
610 int nsect, sector, nframes, frame, nskip;
611
612
613 nsect = rq->nr_sectors;
614
615
616 sector = rq->sector;
617
618
619
620
621
622
623
624 nskip = (sector % SECTORS_PER_FRAME);
625 if (nskip > 0)
626 {
627
628 if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS))
629 {
630 printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
631 dev->name, rq->current_nr_sectors);
632 cdrom_end_request (0, dev);
633 DO_REQUEST;
634 return 1;
635 }
636
637 sector -= nskip;
638 nsect += nskip;
639 rq->current_nr_sectors += nskip;
640 }
641
642
643
644 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
645 frame = sector / SECTORS_PER_FRAME;
646
647
648 nframes = MIN (nframes, 65535);
649
650
651 memset (&pc.c, 0, sizeof (pc.c));
652 pc.c[0] = READ_10;
653 pc.c[7] = (nframes >> 8);
654 pc.c[8] = (nframes & 0xff);
655
656
657 {
658 union {
659 struct {unsigned char b0, b1, b2, b3;} b;
660 struct {unsigned long l0;} l;
661 } conv;
662 conv.l.l0 = frame;
663 pc.c[2] = conv.b.b3;
664 pc.c[3] = conv.b.b2;
665 pc.c[4] = conv.b.b1;
666 pc.c[5] = conv.b.b0;
667 }
668
669 if (cdrom_transfer_packet_command (dev, pc.c, sizeof (pc.c)))
670 return 1;
671
672
673 ide_handler[dev->hwif] = cdrom_read_intr;
674
675 return 0;
676 }
677
678
679
680
681
682
683
684
685 static int cdrom_start_read (ide_dev_t *dev, unsigned int block)
686 {
687 struct request *rq = ide_cur_rq[dev->hwif];
688
689
690
691 if (rq->buffer != rq->bh->b_data)
692 {
693 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
694 rq->buffer = rq->bh->b_data;
695 rq->nr_sectors += n;
696 rq->current_nr_sectors += n;
697 rq->sector -= n;
698 }
699
700 if (rq->current_nr_sectors > (rq->bh->b_size >> SECTOR_BITS))
701 rq->current_nr_sectors = rq->bh->b_size;
702
703
704 if (cdrom_read_from_buffer (dev))
705 return 1;
706
707 if (cdrom_start_packet_command (dev, 32768))
708 return 1;
709
710 if (CDROM_FLAGS (dev)->drq_interrupt)
711 ide_handler[dev->hwif] = (void (*)(ide_dev_t *))cdrom_start_read_continuation;
712 else
713 {
714 if (cdrom_start_read_continuation (dev))
715 return 1;
716 }
717
718 return 0;
719 }
720
721
722
723
724
725
726
727
728 static void cdrom_pc_intr (ide_dev_t *dev)
729 {
730 int ireason, len, stat, thislen;
731 struct request *rq = ide_cur_rq[dev->hwif];
732 struct packet_command *pc = (struct packet_command *)rq->buffer;
733
734
735 if (cdrom_decode_status (dev, 0, &stat)) return;
736
737
738 ireason = IN_BYTE (HD_NSECTOR, dev->hwif);
739 len = IN_BYTE (HD_LCYL, dev->hwif) + 256 * IN_BYTE (HD_HCYL, dev->hwif);
740
741
742
743 if ((stat & DRQ_STAT) == 0)
744 {
745 if (pc->buflen == 0)
746 cdrom_end_request (1, dev);
747 else
748 {
749 printk ("%s: cdrom_pc_intr: data underrun %d\n",
750 dev->name, pc->buflen);
751 pc->stat = 1;
752 cdrom_end_request (1, dev);
753 }
754 DO_REQUEST;
755 return;
756 }
757
758
759 thislen = pc->buflen;
760 if (thislen < 0) thislen = -thislen;
761 if (thislen > len) thislen = len;
762
763
764 if ((ireason & 3) == 0)
765 {
766
767 if (pc->buflen > 0)
768 {
769 printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
770 dev->name);
771 pc->stat = 1;
772 thislen = 0;
773 }
774
775
776 OUT_WORDS (pc->buffer, thislen / 2);
777
778
779
780 while (len > thislen)
781 {
782 short dum = 0;
783 OUT_WORDS (&dum, 1);
784 len -= 2;
785 }
786
787
788 pc->buffer += thislen;
789 pc->buflen += thislen;
790 }
791
792
793 else if ((ireason & 3) == 2)
794 {
795
796 if (pc->buflen < 0)
797 {
798 printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
799 dev->name);
800 pc->stat = 1;
801 thislen = 0;
802 }
803
804
805 IN_WORDS (pc->buffer, thislen / 2);
806
807
808
809 while (len > thislen)
810 {
811 short dum = 0;
812 IN_WORDS (&dum, 1);
813 len -= 2;
814 }
815
816
817 pc->buffer += thislen;
818 pc->buflen -= thislen;
819 }
820
821 else
822 {
823 printk ("%s: cdrom_pc_intr: The drive appears confused (ireason = 0x%2x)\n",
824 dev->name, ireason);
825 pc->stat = 1;
826 }
827
828
829 ide_handler[dev->hwif] = cdrom_pc_intr;
830 }
831
832
833 static int cdrom_do_pc_continuation (ide_dev_t *dev)
834 {
835 struct request *rq = ide_cur_rq[dev->hwif];
836 struct packet_command *pc = (struct packet_command *)rq->buffer;
837
838 if (cdrom_transfer_packet_command (dev, pc->c, sizeof (pc->c)))
839 return 1;
840
841
842 ide_handler[dev->hwif] = cdrom_pc_intr;
843
844 return 0;
845 }
846
847
848 static int cdrom_do_packet_command (ide_dev_t *dev)
849 {
850 int len;
851 struct request *rq = ide_cur_rq[dev->hwif];
852 struct packet_command *pc = (struct packet_command *)rq->buffer;
853
854 len = pc->buflen;
855 if (len < 0) len = -len;
856
857 pc->stat = 0;
858
859 if (cdrom_start_packet_command (dev, len))
860 return 1;
861
862 if (CDROM_FLAGS (dev)->drq_interrupt)
863 ide_handler[dev->hwif] = (void (*)(ide_dev_t *))cdrom_do_pc_continuation;
864 else
865 {
866 if (cdrom_do_pc_continuation (dev))
867 return 1;
868 }
869
870 return 0;
871 }
872
873
874 static
875 int cdrom_queue_packet_command (ide_dev_t *dev, struct packet_command *pc)
876 {
877 unsigned long flags;
878 struct request req, **p, **pfirst;
879 struct semaphore sem = MUTEX_LOCKED;
880 int major = ide_major[dev->hwif];
881
882 req.dev = MKDEV (major, (dev->select.b.drive) << PARTN_BITS);
883 req.cmd = PACKET_COMMAND;
884 req.errors = 0;
885 req.sector = 0;
886 req.nr_sectors = 0;
887 req.current_nr_sectors = 0;
888 req.buffer = (char *)pc;
889 req.sem = &sem;
890 req.bh = NULL;
891 req.bhtail = NULL;
892 req.next = NULL;
893
894 save_flags (flags);
895 cli ();
896
897 p = &blk_dev[major].current_request;
898 pfirst = p;
899 while ((*p) != NULL)
900 {
901 p = &((*p)->next);
902 }
903 *p = &req;
904 if (p == pfirst)
905 blk_dev[major].request_fn ();
906
907 restore_flags (flags);
908
909 down (&sem);
910
911 if (pc->stat != 0)
912 return -EIO;
913 else
914 return 0;
915 }
916
917
918
919
920
921
922
923 static int do_rw_cdrom (ide_dev_t *dev, unsigned long block)
924 {
925 struct request *rq = ide_cur_rq[dev->hwif];
926
927 if (rq -> cmd == PACKET_COMMAND)
928 return cdrom_do_packet_command (dev);
929
930 if (rq -> cmd != READ)
931 {
932 printk ("ide-cd: bad cmd %d\n", rq -> cmd);
933 cdrom_end_request (0, dev);
934 return 1;
935 }
936
937 return cdrom_start_read (dev, block);
938 }
939
940
941
942
943
944
945
946 static inline
947 void byte_swap_word (unsigned short *x)
948 {
949 char *c = (char *)x;
950 char d = c[0];
951 c[0] = c[1];
952 c[1] = d;
953 }
954
955
956 static inline
957 void byte_swap_long (unsigned *x)
958 {
959 char *c = (char *)x;
960 char d = c[0];
961 c[0] = c[3];
962 c[3] = d;
963 d = c[1];
964 c[1] = c[2];
965 c[2] = d;
966 }
967
968
969 static
970 int bin2bcd (int x)
971 {
972 return (x%10) | ((x/10) << 4);
973 }
974
975
976 static inline
977 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
978 {
979 lba += CD_BLOCK_OFFSET;
980 lba &= 0xffffff;
981 *m = lba / (CD_SECS * CD_FRAMES);
982 lba %= (CD_SECS * CD_FRAMES);
983 *s = lba / CD_FRAMES;
984 *f = lba % CD_FRAMES;
985 }
986
987
988 static inline
989 int msf_to_lba (byte m, byte s, byte f)
990 {
991 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
992 }
993
994
995 static void
996 cdrom_check_status (ide_dev_t *dev)
997 {
998 struct packet_command pc;
999
1000 memset (&pc, 0, sizeof (pc));
1001
1002 pc.c[0] = TEST_UNIT_READY;
1003
1004 (void) cdrom_queue_packet_command (dev, &pc);
1005 }
1006
1007
1008 static int
1009 cdrom_request_sense (ide_dev_t *dev, struct atapi_request_sense *reqbuf)
1010 {
1011 struct packet_command pc;
1012
1013 memset (&pc, 0, sizeof (pc));
1014
1015 pc.c[0] = REQUEST_SENSE;
1016 pc.c[4] = sizeof (*reqbuf);
1017 pc.buffer = (char *)reqbuf;
1018 pc.buflen = sizeof (*reqbuf);
1019
1020 return cdrom_queue_packet_command (dev, &pc);
1021 }
1022
1023
1024 #if 0
1025
1026 static int
1027 cdrom_lockdoor (ide_dev_t *dev, int lockflag)
1028 {
1029 struct packet_command pc;
1030
1031 memset (&pc, 0, sizeof (pc));
1032
1033 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1034 pc.c[4] = (lockflag != 0);
1035 return cdrom_queue_packet_command (dev, &pc);
1036 }
1037 #endif
1038
1039
1040
1041
1042 static int
1043 cdrom_eject (ide_dev_t *dev, int ejectflag)
1044 {
1045 struct packet_command pc;
1046
1047 memset (&pc, 0, sizeof (pc));
1048
1049 pc.c[0] = START_STOP;
1050 pc.c[4] = 2 + (ejectflag != 0);
1051 return cdrom_queue_packet_command (dev, &pc);
1052 }
1053
1054
1055 static int
1056 cdrom_pause (ide_dev_t *dev, int pauseflag)
1057 {
1058 struct packet_command pc;
1059
1060 memset (&pc, 0, sizeof (pc));
1061
1062 pc.c[0] = SCMD_PAUSE_RESUME;
1063 pc.c[8] = !pauseflag;
1064 return cdrom_queue_packet_command (dev, &pc);
1065 }
1066
1067
1068 static int
1069 cdrom_startstop (ide_dev_t *dev, int startflag)
1070 {
1071 struct packet_command pc;
1072
1073 memset (&pc, 0, sizeof (pc));
1074
1075 pc.c[0] = START_STOP;
1076 pc.c[1] = 1;
1077 pc.c[4] = startflag;
1078 return cdrom_queue_packet_command (dev, &pc);
1079 }
1080
1081
1082 static int
1083 cdrom_read_tocentry (ide_dev_t *dev, int trackno, int msf_flag,
1084 char *buf, int buflen)
1085 {
1086 struct packet_command pc;
1087
1088 memset (&pc, 0, sizeof (pc));
1089
1090 pc.buffer = buf;
1091 pc.buflen = buflen;
1092 pc.c[0] = SCMD_READ_TOC;
1093 pc.c[6] = trackno;
1094 pc.c[7] = (buflen >> 8);
1095 pc.c[8] = (buflen & 0xff);
1096 if (msf_flag) pc.c[1] = 2;
1097 return cdrom_queue_packet_command (dev, &pc);
1098 }
1099
1100
1101
1102 static int
1103 cdrom_read_toc (ide_dev_t *dev)
1104 {
1105 int msf_flag;
1106 int stat, ntracks, i;
1107 struct atapi_toc *toc = cdrom_info[dev->hwif][dev->select.b.drive].toc;
1108
1109 if (toc == NULL)
1110 {
1111
1112 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1113 GFP_KERNEL);
1114 cdrom_info[dev->hwif][dev->select.b.drive].toc = toc;
1115 }
1116
1117 if (toc == NULL)
1118 {
1119 printk ("%s: No cdrom TOC buffer!\n", dev->name);
1120 return -EIO;
1121 }
1122
1123
1124
1125 if (CDROM_FLAGS (dev)->toc_valid)
1126 cdrom_check_status (dev);
1127
1128 if (CDROM_FLAGS (dev)->toc_valid) return 0;
1129
1130
1131 msf_flag = (CDROM_FLAGS (dev)->no_lba_toc);
1132
1133
1134 stat = cdrom_read_tocentry (dev, 0, msf_flag, (char *)toc,
1135 sizeof (struct atapi_toc_header) +
1136 sizeof (struct atapi_toc_entry));
1137 if (stat) return stat;
1138
1139 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1140 if (ntracks <= 0) return -EIO;
1141 if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1142
1143
1144 stat = cdrom_read_tocentry (dev, 0, msf_flag, (char *)toc,
1145 sizeof (struct atapi_toc_header) +
1146 (ntracks+1) * sizeof (struct atapi_toc_entry));
1147 if (stat) return stat;
1148 byte_swap_word (&toc->hdr.toc_length);
1149 for (i=0; i<=ntracks; i++)
1150 {
1151 if (msf_flag)
1152 {
1153 byte *adr = (byte *)&(toc->ent[i].lba);
1154 toc->ent[i].lba = msf_to_lba (adr[1], adr[2], adr[3]);
1155 }
1156 else
1157 byte_swap_long (&toc->ent[i].lba);
1158 }
1159
1160
1161 CDROM_FLAGS (dev)->toc_valid = 1;
1162
1163 return 0;
1164 }
1165
1166
1167 static int
1168 cdrom_read_subchannel (ide_dev_t *dev,
1169 char *buf, int buflen)
1170 {
1171 struct packet_command pc;
1172
1173 memset (&pc, 0, sizeof (pc));
1174
1175 pc.buffer = buf;
1176 pc.buflen = buflen;
1177 pc.c[0] = SCMD_READ_SUBCHANNEL;
1178 pc.c[2] = 0x40;
1179 pc.c[3] = 0x01;
1180 pc.c[7] = (buflen >> 8);
1181 pc.c[8] = (buflen & 0xff);
1182 return cdrom_queue_packet_command (dev, &pc);
1183 }
1184
1185
1186
1187 static int
1188 cdrom_mode_sense (ide_dev_t *dev, int pageno, int modeflag,
1189 char *buf, int buflen)
1190 {
1191 struct packet_command pc;
1192
1193 memset (&pc, 0, sizeof (pc));
1194
1195 pc.buffer = buf;
1196 pc.buflen = buflen;
1197 pc.c[0] = MODE_SENSE_10;
1198 pc.c[2] = pageno | (modeflag << 6);
1199 pc.c[7] = (buflen >> 8);
1200 pc.c[8] = (buflen & 0xff);
1201 return cdrom_queue_packet_command (dev, &pc);
1202 }
1203
1204
1205 static int
1206 cdrom_mode_select (ide_dev_t *dev, char *buf, int buflen)
1207 {
1208 struct packet_command pc;
1209
1210 memset (&pc, 0, sizeof (pc));
1211
1212 pc.buffer = buf;
1213 pc.buflen = - buflen;
1214 pc.c[0] = MODE_SELECT_10;
1215 pc.c[1] = 0x10;
1216 pc.c[7] = (buflen >> 8);
1217 pc.c[8] = (buflen & 0xff);
1218 return cdrom_queue_packet_command (dev, &pc);
1219 }
1220
1221
1222 static int
1223 cdrom_play_lba_range_play12 (ide_dev_t *dev, int lba_start, int lba_end)
1224 {
1225 struct packet_command pc;
1226
1227 memset (&pc, 0, sizeof (pc));
1228
1229 pc.c[0] = SCMD_PLAYAUDIO12;
1230 *(int *)(&pc.c[2]) = lba_start;
1231 *(int *)(&pc.c[6]) = lba_end - lba_start;
1232 byte_swap_long ((int *)(&pc.c[2]));
1233 byte_swap_long ((int *)(&pc.c[6]));
1234
1235 return cdrom_queue_packet_command (dev, &pc);
1236 }
1237
1238
1239 static int
1240 cdrom_play_lba_range_msf (ide_dev_t *dev, int lba_start, int lba_end)
1241 {
1242 struct packet_command pc;
1243
1244 memset (&pc, 0, sizeof (pc));
1245
1246 pc.c[0] = SCMD_PLAYAUDIO_MSF;
1247 lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1248 lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1249
1250 pc.c[3] = bin2bcd (pc.c[3]);
1251 pc.c[4] = bin2bcd (pc.c[4]);
1252 pc.c[5] = bin2bcd (pc.c[5]);
1253 pc.c[6] = bin2bcd (pc.c[6]);
1254 pc.c[7] = bin2bcd (pc.c[7]);
1255 pc.c[8] = bin2bcd (pc.c[8]);
1256
1257 return cdrom_queue_packet_command (dev, &pc);
1258 }
1259
1260
1261
1262
1263 static int
1264 cdrom_play_lba_range (ide_dev_t *dev, int lba_start, int lba_end)
1265 {
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277 if (CDROM_FLAGS (dev)->no_playaudio12)
1278 return cdrom_play_lba_range_msf (dev, lba_start, lba_end);
1279 else
1280 {
1281 int stat, stat2;
1282 struct atapi_request_sense reqbuf;
1283
1284 stat = cdrom_play_lba_range_play12 (dev, lba_start, lba_end);
1285 if (stat == 0) return 0;
1286
1287
1288 stat2 = cdrom_request_sense (dev, &reqbuf);
1289 if (stat2) return stat;
1290
1291 if (reqbuf.sense_key == 0x05 && reqbuf.asc == 0x20)
1292 {
1293
1294
1295 printk ("%s: Drive does not support PLAYAUDIO12; "
1296 "trying PLAYAUDIO_MSF\n", dev->name);
1297 CDROM_FLAGS (dev)->no_playaudio12 = 1;
1298 return cdrom_play_lba_range_msf (dev, lba_start, lba_end);
1299 }
1300
1301
1302 return stat;
1303 }
1304 }
1305
1306
1307 static
1308 int cdrom_get_toc_entry (ide_dev_t *dev, int track,
1309 struct atapi_toc_entry **ent)
1310 {
1311 int stat, ntracks;
1312 struct atapi_toc *toc;
1313
1314
1315 stat = cdrom_read_toc (dev);
1316 if (stat) return stat;
1317
1318 toc = cdrom_info[dev->hwif][dev->select.b.drive].toc;
1319
1320
1321 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1322 if (track == CDROM_LEADOUT)
1323 *ent = &toc->ent[ntracks];
1324 else if (track < toc->hdr.first_track ||
1325 track > toc->hdr.last_track)
1326 return -EINVAL;
1327 else
1328 *ent = &toc->ent[track - toc->hdr.first_track];
1329
1330 return 0;
1331 }
1332
1333
1334 static int ide_cdrom_ioctl (ide_dev_t *dev, struct inode *inode,
1335 struct file *file, unsigned int cmd, unsigned long arg)
1336 {
1337 switch (cmd)
1338 {
1339 case CDROMEJECT:
1340 return cdrom_eject (dev, 0);
1341
1342 case CDROMPAUSE:
1343 return cdrom_pause (dev, 1);
1344
1345 case CDROMRESUME:
1346 return cdrom_pause (dev, 0);
1347
1348 case CDROMSTART:
1349 return cdrom_startstop (dev, 1);
1350
1351 case CDROMSTOP:
1352 return cdrom_startstop (dev, 0);
1353
1354 case CDROMPLAYMSF:
1355 {
1356 struct cdrom_msf msf;
1357 int stat, lba_start, lba_end;
1358
1359 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
1360 if (stat) return stat;
1361
1362 memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
1363
1364 lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
1365 msf.cdmsf_frame0);
1366 lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
1367 msf.cdmsf_frame1) + 1;
1368
1369 if (lba_end <= lba_start) return -EINVAL;
1370
1371 return cdrom_play_lba_range (dev, lba_start, lba_end);
1372 }
1373
1374
1375
1376 case CDROMPLAYTRKIND:
1377 {
1378 int stat, lba_start, lba_end;
1379 struct cdrom_ti ti;
1380 struct atapi_toc_entry *first_toc, *last_toc;
1381
1382 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
1383 if (stat) return stat;
1384
1385 memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
1386
1387 stat = cdrom_get_toc_entry (dev, ti.cdti_trk0, &first_toc);
1388 if (stat) return stat;
1389 stat = cdrom_get_toc_entry (dev, ti.cdti_trk1, &last_toc);
1390 if (stat) return stat;
1391
1392 if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
1393 lba_start = first_toc->lba;
1394 lba_end = last_toc->lba;
1395
1396 if (lba_end <= lba_start) return -EINVAL;
1397
1398 return cdrom_play_lba_range (dev, lba_start, lba_end);
1399 }
1400
1401 case CDROMREADTOCHDR:
1402 {
1403 int stat;
1404 struct cdrom_tochdr tochdr;
1405 struct atapi_toc *toc;
1406
1407 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tochdr));
1408 if (stat) return stat;
1409
1410
1411 stat = cdrom_read_toc (dev);
1412 if (stat) return stat;
1413
1414 toc = cdrom_info[dev->hwif][dev->select.b.drive].toc;
1415 tochdr.cdth_trk0 = toc->hdr.first_track;
1416 tochdr.cdth_trk1 = toc->hdr.last_track;
1417
1418 memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
1419
1420 return stat;
1421 }
1422
1423 case CDROMREADTOCENTRY:
1424 {
1425 int stat;
1426 struct cdrom_tocentry tocentry;
1427 struct atapi_toc_entry *toce;
1428
1429 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (tocentry));
1430 if (stat) return stat;
1431 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tocentry));
1432 if (stat) return stat;
1433
1434 memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
1435
1436 stat = cdrom_get_toc_entry (dev, tocentry.cdte_track, &toce);
1437 if (stat) return stat;
1438
1439 tocentry.cdte_ctrl = toce->control;
1440 tocentry.cdte_adr = toce->adr;
1441
1442 if (tocentry.cdte_format == CDROM_MSF)
1443 {
1444
1445 lba_to_msf (toce->lba,
1446 &tocentry.cdte_addr.msf.minute,
1447 &tocentry.cdte_addr.msf.second,
1448 &tocentry.cdte_addr.msf.frame);
1449 }
1450 else
1451 tocentry.cdte_addr.lba = toce->lba;
1452
1453 memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
1454
1455 return stat;
1456 }
1457
1458 case CDROMSUBCHNL:
1459 {
1460 char buffer[16];
1461 int stat, abs_lba, rel_lba;
1462 struct cdrom_subchnl subchnl;
1463
1464 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (subchnl));
1465 if (stat) return stat;
1466 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (subchnl));
1467 if (stat) return stat;
1468
1469 memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
1470
1471 stat = cdrom_read_subchannel (dev, buffer, sizeof (buffer));
1472 if (stat) return stat;
1473
1474 abs_lba = *(int *)&buffer[8];
1475 rel_lba = *(int *)&buffer[12];
1476 byte_swap_long (&abs_lba);
1477 byte_swap_long (&rel_lba);
1478
1479 if (subchnl.cdsc_format == CDROM_MSF)
1480 {
1481 lba_to_msf (abs_lba,
1482 &subchnl.cdsc_absaddr.msf.minute,
1483 &subchnl.cdsc_absaddr.msf.second,
1484 &subchnl.cdsc_absaddr.msf.frame);
1485 lba_to_msf (rel_lba,
1486 &subchnl.cdsc_reladdr.msf.minute,
1487 &subchnl.cdsc_reladdr.msf.second,
1488 &subchnl.cdsc_reladdr.msf.frame);
1489 }
1490 else
1491 {
1492 subchnl.cdsc_absaddr.lba = abs_lba;
1493 subchnl.cdsc_reladdr.lba = rel_lba;
1494 }
1495
1496 subchnl.cdsc_audiostatus = buffer[1];
1497 subchnl.cdsc_ctrl = buffer[5] & 0xf;
1498 subchnl.cdsc_trk = buffer[6];
1499 subchnl.cdsc_ind = buffer[7];
1500
1501 memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
1502
1503 return stat;
1504 }
1505
1506 case CDROMVOLCTRL:
1507 {
1508 struct cdrom_volctrl volctrl;
1509 char buffer[24], mask[24];
1510 int stat;
1511
1512 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (volctrl));
1513 if (stat) return stat;
1514 memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
1515
1516 stat = cdrom_mode_sense (dev, 0x0e, 0, buffer, sizeof (buffer));
1517 if (stat) return stat;
1518 stat = cdrom_mode_sense (dev, 0x0e, 1, mask , sizeof (buffer));
1519 if (stat) return stat;
1520
1521 buffer[1] = buffer[2] = 0;
1522
1523 buffer[17] = volctrl.channel0 & mask[17];
1524 buffer[19] = volctrl.channel1 & mask[19];
1525 buffer[21] = volctrl.channel2 & mask[21];
1526 buffer[23] = volctrl.channel3 & mask[23];
1527
1528 return cdrom_mode_select (dev, buffer, sizeof (buffer));
1529 }
1530
1531 #ifdef TEST
1532 case 0x1234:
1533 {
1534 int stat;
1535 struct packet_command pc;
1536
1537 memset (&pc, 0, sizeof (pc));
1538
1539 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
1540 if (stat) return stat;
1541 memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
1542
1543 return cdrom_queue_packet_command (dev, &pc);
1544 }
1545
1546 case 0x1235:
1547 {
1548 int stat;
1549 struct atapi_request_sense reqbuf;
1550
1551 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (reqbuf));
1552 if (stat) return stat;
1553
1554 stat = cdrom_request_sense (dev, &reqbuf);
1555
1556 memcpy_tofs ((void *) arg, &reqbuf, sizeof (reqbuf));
1557
1558 return stat;
1559 }
1560 #endif
1561
1562 default:
1563 return -EPERM;
1564 }
1565
1566 }
1567
1568
1569
1570
1571
1572
1573
1574 static int cdrom_check_media_change (ide_dev_t *dev)
1575 {
1576 int retval;
1577
1578 cdrom_check_status (dev);
1579
1580 retval = CDROM_FLAGS (dev)->media_changed;
1581 CDROM_FLAGS (dev)->media_changed = 0;
1582
1583 return retval;
1584 }
1585
1586
1587 static int
1588 cdrom_open (struct inode *ip, struct file *fp, ide_dev_t *dev)
1589 {
1590
1591 if (fp->f_mode & 2) return -EROFS;
1592
1593 #if 0
1594
1595 if (dev->usage == 1)
1596 (void) cdrom_lockdoor (dev, 1);
1597 #endif
1598
1599
1600 return 0;
1601 }
1602
1603
1604
1605
1606
1607
1608 static void
1609 cdrom_release (struct inode *inode, struct file *file, ide_dev_t *dev)
1610 {
1611 if (dev->usage == 0)
1612 {
1613 invalidate_buffers (inode->i_rdev);
1614
1615 #if 0
1616
1617 (void) cdrom_lockdoor (dev, 0);
1618 #endif
1619 }
1620 }
1621
1622
1623
1624
1625
1626
1627
1628 static void cdrom_setup (ide_dev_t *dev)
1629 {
1630
1631 ide_capacity[dev->hwif][dev->select.b.drive] = 0x1fffff;
1632
1633 ide_blksizes[dev->hwif][dev->select.b.drive << PARTN_BITS] = CD_FRAMESIZE;
1634
1635 dev->special.all = 0;
1636
1637 CDROM_FLAGS (dev)->media_changed = 0;
1638 CDROM_FLAGS (dev)->toc_valid = 0;
1639
1640 CDROM_FLAGS (dev)->no_playaudio12 = 0;
1641 CDROM_FLAGS (dev)->no_lba_toc = 0;
1642 CDROM_FLAGS (dev)->drq_interrupt = ((dev->id->config & 0x0060) == 0x20);
1643
1644
1645 if (strcmp (dev->id->model, "CD220E") == 0)
1646 CDROM_FLAGS (dev)->no_lba_toc = 1;
1647
1648 else if (strcmp (dev->id->model, "TO-ICSLYAL") == 0 ||
1649 strcmp (dev->id->model, "OTI-SCYLLA") == 0)
1650 CDROM_FLAGS (dev)->no_lba_toc = 1;
1651
1652 else if (strcmp (dev->id->model, "CDA26803I SE") == 0)
1653 CDROM_FLAGS (dev)->no_lba_toc = 1;
1654
1655 cdrom_info[dev->hwif][dev->select.b.drive].toc = NULL;
1656 cdrom_info[dev->hwif][dev->select.b.drive].sector_buffer = NULL;
1657 cdrom_info[dev->hwif][dev->select.b.drive].sector_buffered = 0;
1658 cdrom_info[dev->hwif][dev->select.b.drive].nsectors_buffered = 0;
1659 }
1660
1661
1662 #undef MIN
1663 #undef SECTOR_SIZE
1664 #undef SECTOR_BITS
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676