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