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