This source file includes following definitions.
- cdrom_analyze_sense_data
- restore_request
- cdrom_queue_request_sense
- cdrom_end_request
- cdrom_saw_media_change
- cdrom_decode_status
- cdrom_start_packet_command
- cdrom_transfer_packet_command
- cdrom_buffer_sectors
- cdrom_read_check_ireason
- cdrom_read_intr
- cdrom_read_from_buffer
- cdrom_start_read_continuation
- cdrom_start_read
- cdrom_pc_intr
- cdrom_do_pc_continuation
- cdrom_do_packet_command
- cdrom_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 #ifndef VERBOSE_IDE_CD_ERRORS
57 #define VERBOSE_IDE_CD_ERRORS 0
58 #endif
59
60
61
62 #include <linux/cdrom.h>
63
64 #define SECTOR_SIZE 512
65 #define SECTOR_BITS 9
66 #define SECTORS_PER_FRAME (CD_FRAMESIZE / SECTOR_SIZE)
67
68 #define MIN(a,b) ((a) < (b) ? (a) : (b))
69
70 #if 1
71 #define OUT_WORDS(b,n) outsw (IDE_PORT (HD_DATA, DEV_HWIF), (b), (n))
72 #define IN_WORDS(b,n) insw (IDE_PORT (HD_DATA, DEV_HWIF), (b), (n))
73 #else
74 #define OUT_WORDS(b,n) output_ide_data(dev,b,(n)/2)
75 #define IN_WORDS(b,n) input_ide_data(dev,b,(n)/2)
76 #endif
77
78
79 #define PACKET_COMMAND 4315
80 #define REQUEST_SENSE_COMMAND 4316
81
82 #define WIN_PACKETCMD 0xa0
83
84
85
86 #define TEST_UNIT_READY 0x00
87 #define REQUEST_SENSE 0x03
88 #define START_STOP 0x1b
89 #define ALLOW_MEDIUM_REMOVAL 0x1e
90 #define READ_10 0x28
91 #define MODE_SENSE_10 0x5a
92 #define MODE_SELECT_10 0x55
93
94
95
96
97 #define NO_SENSE 0x00
98 #define RECOVERED_ERROR 0x01
99 #define NOT_READY 0x02
100 #define MEDIUM_ERROR 0x03
101 #define HARDWARE_ERROR 0x04
102 #define ILLEGAL_REQUEST 0x05
103 #define UNIT_ATTENTION 0x06
104 #define DATA_PROTECT 0x07
105 #define ABORTED_COMMAND 0x0b
106 #define MISCOMPARE 0x0e
107
108
109 struct packet_command {
110 char *buffer;
111 int buflen;
112 int stat;
113 unsigned char c[12];
114 };
115
116
117 struct atapi_request_sense {
118 unsigned char error_code : 7;
119 unsigned char valid : 1;
120 byte reserved1;
121 unsigned char sense_key : 4;
122 unsigned char reserved2 : 1;
123 unsigned char ili : 1;
124 unsigned char reserved3 : 2;
125 byte info[4];
126 byte sense_len;
127 byte command_info[4];
128 byte asc;
129 byte ascq;
130 byte fru;
131 byte sense_key_specific[3];
132 };
133
134
135
136
137
138 struct ide_cd_flags {
139 unsigned drq_interrupt : 1;
140
141 unsigned no_playaudio12: 1;
142
143 unsigned media_changed : 1;
144 unsigned toc_valid : 1;
145 unsigned no_lba_toc : 1;
146 unsigned msf_as_bcd : 1;
147 unsigned reserved : 2;
148 };
149
150 #define CDROM_FLAGS(dev) ((struct ide_cd_flags *)&((dev)->bios_sect))
151
152
153
154
155 #define MAX_TRACKS 99
156 struct atapi_toc_header {
157 unsigned short toc_length;
158 byte first_track;
159 byte last_track;
160 };
161
162 struct atapi_toc_entry {
163 byte reserved1;
164 unsigned control : 4;
165 unsigned adr : 4;
166 byte track;
167 byte reserved2;
168 unsigned lba;
169 };
170
171 struct atapi_toc {
172 struct atapi_toc_header hdr;
173 struct atapi_toc_entry ent[MAX_TRACKS+1];
174 };
175
176
177 #define SECTOR_BUFFER_SIZE CD_FRAMESIZE
178
179
180 struct cdrom_info {
181
182
183
184
185 struct atapi_toc *toc;
186
187
188
189
190
191
192
193
194 unsigned long sector_buffered;
195 unsigned long nsectors_buffered;
196 char *sector_buffer;
197
198
199
200 struct atapi_request_sense sense_data;
201 };
202
203
204 static struct cdrom_info cdrom_info[2][MAX_DRIVES];
205
206
207
208
209 static struct request request_sense_request[2];
210 static struct packet_command request_sense_pc[2];
211
212
213
214
215
216
217
218 #define ARY_LEN(a) ((sizeof(a) / sizeof(a[0])))
219
220 #if VERBOSE_IDE_CD_ERRORS
221
222
223
224 char *sense_key_texts[16] = {
225 "No sense data",
226 "Recovered error",
227 "Not ready",
228 "Medium error",
229 "Hardware error",
230 "Illegal request",
231 "Unit attention",
232 "Data protect",
233 "(reserved)",
234 "(reserved)",
235 "(reserved)",
236 "Aborted command",
237 "(reserved)",
238 "(reserved)",
239 "Miscompare",
240 "(reserved)",
241 };
242
243
244
245
246 struct {
247 short asc_ascq;
248 char *text;
249 } sense_data_texts[] = {
250 { 0x0000, "No additional sense information" },
251 { 0x0011, "Audio play operation in progress" },
252 { 0x0012, "Audio play operation paused" },
253 { 0x0013, "Audio play operation successfully completed" },
254 { 0x0014, "Audio play operation stopped due to error" },
255 { 0x0015, "No current audio status to return" },
256
257 { 0x0200, "No seek complete" },
258
259 { 0x0400, "Logical unit not ready - cause not reportable" },
260 { 0x0401, "Logical unit not ready - in progress (sic) of becoming ready" },
261 { 0x0402, "Logical unit not ready - initializing command required" },
262 { 0x0403, "Logical unit not ready - manual intervention required" },
263
264 { 0x0600, "No reference position found" },
265
266 { 0x0900, "Track following error" },
267 { 0x0901, "Tracking servo failure" },
268 { 0x0902, "Focus servo failure" },
269 { 0x0903, "Spindle servo failure" },
270
271 { 0x1100, "Unrecovered read error" },
272 { 0x1106, "CIRC unrecovered error" },
273
274 { 0x1500, "Random positioning error" },
275 { 0x1501, "Mechanical positioning error" },
276 { 0x1502, "Positioning error detected by read of medium" },
277
278 { 0x1700, "Recovered data with no error correction applied" },
279 { 0x1701, "Recovered data with retries" },
280 { 0x1702, "Recovered data with positive head offset" },
281 { 0x1703, "Recovered data with negative head offset" },
282 { 0x1704, "Recovered data with retries and/or CIRC applied" },
283 { 0x1705, "Recovered data using previous sector ID" },
284
285 { 0x1800, "Recovered data with error correction applied" },
286 { 0x1801, "Recovered data with error correction and retries applied" },
287 { 0x1802, "Recovered data - the data was auto-reallocated" },
288 { 0x1803, "Recovered data with CIRC" },
289 { 0x1804, "Recovered data with L-EC" },
290 { 0x1805, "Recovered data - recommend reassignment" },
291 { 0x1806, "Recovered data - recommend rewrite" },
292
293 { 0x1a00, "Parameter list length error" },
294
295 { 0x2000, "Invalid command operation code" },
296
297 { 0x2100, "Logical block address out of range" },
298
299 { 0x2400, "Invalid field in command packet" },
300
301 { 0x2600, "Invalid field in parameter list" },
302 { 0x2601, "Parameter not supported" },
303 { 0x2602, "Parameter value invalid" },
304 { 0x2603, "Threshold parameters not supported" },
305
306 { 0x2800, "Not ready to ready transition, medium may have changed" },
307
308 { 0x2900, "Power on, reset or bus device reset occurred" },
309
310 { 0x2a00, "Parameters changed" },
311 { 0x2a01, "Mode parameters changed" },
312
313 { 0x3000, "Incompatible medium installed" },
314 { 0x3001, "Cannot read medium - unknown format" },
315 { 0x3002, "Cannot read medium - incompatible format" },
316
317 { 0x3700, "Rounded parameter" },
318
319 { 0x3900, "Saving parameters not supported" },
320
321 { 0x3a00, "Medium not present" },
322
323 { 0x3f00, "ATAPI CD-ROM drive operating conditions have changed" },
324 { 0x3f01, "Microcode has been changed" },
325 { 0x3f02, "Changed operating definition" },
326 { 0x3f03, "Inquiry data has changed" },
327
328 { 0x4000, "Diagnostic failure on component (ASCQ)" },
329
330 { 0x4400, "Internal ATAPI CD-ROM drive failure" },
331
332 { 0x4e00, "Overlapped commands attempted" },
333
334 { 0x5300, "Media load or eject failed" },
335 { 0x5302, "Medium removal prevented" },
336
337 { 0x5700, "Unable to recover table of contents" },
338
339 { 0x5a00, "Operator request or state change input (unspecified)" },
340 { 0x5a01, "Operator medium removal request" },
341
342 { 0x5b00, "Threshold condition met" },
343
344 { 0x5c00, "Status change" },
345
346 { 0x6300, "End of user area encountered on this track" },
347
348 { 0x6400, "Illegal mode for this track" },
349
350 { 0xbf00, "Loss of streaming" },
351 };
352 #endif
353
354
355
356
357
358
359
360
361 static
362 void cdrom_analyze_sense_data (ide_dev_t *dev,
363 struct atapi_request_sense *reqbuf,
364 struct packet_command *failed_command)
365 {
366
367
368
369 if (failed_command &&
370 failed_command->c[0] == SCMD_READ_SUBCHANNEL &&
371 (reqbuf->sense_key == 2 || reqbuf->sense_key == 6))
372 return;
373
374 #if VERBOSE_IDE_CD_ERRORS
375 {
376 int i;
377 char *s;
378 char buf[80];
379
380 printk ("ATAPI device %s:\n", dev->name);
381
382 printk (" Error code: %x\n", reqbuf->error_code);
383
384 if (reqbuf->sense_key >= 0 &&
385 reqbuf->sense_key < ARY_LEN (sense_key_texts))
386 s = sense_key_texts[reqbuf->sense_key];
387 else
388 s = "(bad sense key)";
389
390 printk (" Sense key: %x - %s\n", reqbuf->sense_key, s);
391
392 if (reqbuf->asc == 0x40) {
393 sprintf (buf, "Diagnostic failure on component %x", reqbuf->ascq);
394 s = buf;
395 }
396
397 else {
398 int lo, hi;
399 int key = (reqbuf->asc << 8);
400 if ( ! (reqbuf->ascq >= 0x80 && reqbuf->ascq <= 0xdd) )
401 key |= reqbuf->ascq;
402
403 lo = 0;
404 hi = ARY_LEN (sense_data_texts);
405 s = NULL;
406
407 while (hi > lo) {
408 int mid = (lo + hi) / 2;
409 if (sense_data_texts[mid].asc_ascq == key) {
410 s = sense_data_texts[mid].text;
411 break;
412 }
413 else if (sense_data_texts[mid].asc_ascq > key)
414 hi = mid;
415 else
416 lo = mid+1;
417 }
418 }
419
420 if (s == NULL) {
421 if (reqbuf->asc > 0x80)
422 s = "(vendor-specific error)";
423 else
424 s = "(reserved error code)";
425 }
426
427 printk (" Additional sense data: %x, %x - %s\n",
428 reqbuf->asc, reqbuf->ascq, s);
429
430 if (failed_command != NULL) {
431 printk (" Failed packet command: ");
432 for (i=0; i<sizeof (failed_command->c); i++)
433 printk ("%02x ", failed_command->c[i]);
434 printk ("\n");
435 }
436 }
437
438 #else
439 printk ("%s: code: %x key: %x asc: %x ascq: %x\n",
440 dev->name,
441 reqbuf->error_code, reqbuf->sense_key, reqbuf->asc, reqbuf->ascq);
442 #endif
443 }
444
445
446
447
448 static void restore_request (struct request *rq)
449 {
450 if (rq->buffer != rq->bh->b_data)
451 {
452 int n = (rq->buffer - rq->bh->b_data) / SECTOR_SIZE;
453 rq->buffer = rq->bh->b_data;
454 rq->nr_sectors += n;
455 rq->sector -= n;
456 }
457 rq->current_nr_sectors = rq->bh->b_size >> SECTOR_BITS;
458 }
459
460
461 static void cdrom_queue_request_sense (ide_dev_t *dev)
462 {
463 struct request *rq;
464 struct packet_command *pc;
465 struct atapi_request_sense *reqbuf;
466 unsigned long flags;
467
468 int major = ide_major[DEV_HWIF];
469
470 save_flags (flags);
471 cli ();
472
473 rq = ide_cur_rq[DEV_HWIF];
474
475
476 if (rq != NULL)
477 {
478 restore_request (rq);
479 rq->next = blk_dev[major].current_request;
480 blk_dev[major].current_request = rq;
481 ide_cur_rq[DEV_HWIF] = NULL;
482 }
483
484 restore_flags (flags);
485
486
487 reqbuf = &cdrom_info[DEV_HWIF][dev->select.b.drive].sense_data;
488
489 pc = &request_sense_pc[DEV_HWIF];
490 memset (pc, 0, sizeof (*pc));
491
492 pc->c[0] = REQUEST_SENSE;
493 pc->c[4] = sizeof (*reqbuf);
494 pc->buffer = (char *)reqbuf;
495 pc->buflen = sizeof (*reqbuf);
496
497 rq = &request_sense_request[DEV_HWIF];
498 rq->dev = MKDEV (major, (dev->select.b.drive) << PARTN_BITS);
499 rq->cmd = REQUEST_SENSE_COMMAND;
500 rq->errors = 0;
501 rq->sector = 0;
502 rq->nr_sectors = 0;
503 rq->current_nr_sectors = 0;
504 rq->buffer = (char *)pc;
505 rq->sem = NULL;
506 rq->bh = NULL;
507 rq->bhtail = NULL;
508 rq->next = NULL;
509
510 save_flags (flags);
511 cli ();
512
513
514 rq->next = blk_dev[major].current_request;
515 blk_dev[major].current_request = rq;
516
517 restore_flags (flags);
518 }
519
520
521 static void cdrom_end_request (int uptodate, ide_dev_t *dev)
522 {
523 struct request *rq = ide_cur_rq[DEV_HWIF];
524
525
526
527 if (!uptodate && rq->bh != 0)
528 {
529 int adj = rq->current_nr_sectors - 1;
530 rq->current_nr_sectors -= adj;
531 rq->sector += adj;
532 }
533
534 if (rq->cmd == REQUEST_SENSE_COMMAND && uptodate)
535 {
536 struct atapi_request_sense *reqbuf;
537 reqbuf = &cdrom_info[DEV_HWIF][dev->select.b.drive].sense_data;
538 cdrom_analyze_sense_data (dev, reqbuf, NULL);
539 }
540
541 end_request (uptodate, DEV_HWIF);
542 }
543
544
545
546
547 static void cdrom_saw_media_change (ide_dev_t *dev)
548 {
549 CDROM_FLAGS (dev)->media_changed = 1;
550 CDROM_FLAGS (dev)->toc_valid = 0;
551 cdrom_info[DEV_HWIF][dev->select.b.drive].nsectors_buffered = 0;
552 }
553
554
555
556
557 static int cdrom_decode_status (ide_dev_t *dev, int good_stat, int *stat_ret)
558 {
559 struct request *rq = ide_cur_rq[DEV_HWIF];
560 int stat, err, sense_key, cmd;
561
562
563 stat = GET_STAT (DEV_HWIF);
564 *stat_ret = stat;
565
566 if (OK_STAT (stat, good_stat, BAD_R_STAT))
567 return 0;
568
569
570 err = IN_BYTE (HD_ERROR, DEV_HWIF);
571 sense_key = err >> 4;
572
573 if (rq == NULL)
574 printk ("%s : missing request in cdrom_decode_status\n", dev->name);
575 else
576 {
577 cmd = rq->cmd;
578
579
580 if (sense_key == NOT_READY)
581 {
582 struct packet_command *pc;
583 cdrom_saw_media_change (dev);
584
585
586 if (cmd == READ)
587 {
588 printk ("%s : tray open\n", dev->name);
589 cdrom_end_request (0, dev);
590 }
591
592 else
593 {
594
595
596
597
598
599
600 pc = (struct packet_command *)rq->buffer;
601 if (pc->c[0] != SCMD_READ_SUBCHANNEL)
602 printk ("%s : tray open\n", dev->name);
603
604
605 pc->stat = 1;
606 cdrom_end_request (1, dev);
607 }
608 }
609
610
611 else if (sense_key == UNIT_ATTENTION)
612 {
613 cdrom_saw_media_change (dev);
614 printk ("%s: media changed\n", dev->name);
615
616
617
618
619
620 if (cmd == PACKET_COMMAND)
621 {
622 struct packet_command *pc = (struct packet_command *)rq->buffer;
623 pc->stat = 1;
624 cdrom_end_request (1, dev);
625 }
626
627
628
629 else if ((++rq->errors > ERROR_MAX))
630 {
631 cdrom_end_request (0, dev);
632 }
633 }
634
635
636 else if (cmd == PACKET_COMMAND)
637 {
638 struct packet_command *pc = (struct packet_command *)rq->buffer;
639 dump_status (DEV_HWIF, "packet command error", stat);
640 pc->stat = 1;
641 cdrom_end_request (1, dev);
642 }
643
644
645 else if (sense_key == ILLEGAL_REQUEST || sense_key == DATA_PROTECT)
646 {
647 dump_status (DEV_HWIF, "command error", stat);
648 cdrom_end_request (0, dev);
649 }
650
651
652 else if ((err & ~ABRT_ERR) != 0)
653 {
654 ide_error (dev, "cdrom_decode_status", stat);
655 }
656
657
658 else if ((++rq->errors > ERROR_MAX))
659 {
660 cdrom_end_request (0, dev);
661 }
662
663
664
665
666
667 if ((stat & ERR_STAT) != 0 && cmd == READ)
668 cdrom_queue_request_sense (dev);
669 }
670
671
672 DO_REQUEST;
673 return 1;
674 }
675
676
677
678
679
680
681 static int cdrom_start_packet_command (ide_dev_t *dev, int xferlen)
682 {
683
684 if (wait_stat (dev, 0, BUSY_STAT, WAIT_READY)) return 1;
685
686
687 OUT_BYTE (0, HD_FEATURE);
688 OUT_BYTE (0, HD_NSECTOR);
689 OUT_BYTE (0, HD_SECTOR);
690
691 OUT_BYTE (xferlen & 0xff, HD_LCYL);
692 OUT_BYTE (xferlen >> 8 , HD_HCYL);
693 OUT_BYTE (dev->ctl, HD_CMD);
694 OUT_BYTE (WIN_PACKETCMD, HD_COMMAND);
695
696 return 0;
697 }
698
699
700
701
702
703 static int cdrom_transfer_packet_command (ide_dev_t *dev,
704 char *cmd_buf, int cmd_len)
705 {
706 if (CDROM_FLAGS (dev)->drq_interrupt)
707 {
708
709
710 int stat_dum;
711
712
713 if (cdrom_decode_status (dev, DRQ_STAT, &stat_dum)) return 1;
714 }
715 else
716 {
717
718 if (wait_stat (dev, DRQ_STAT, BUSY_STAT, WAIT_READY)) return 1;
719 }
720
721
722 OUT_WORDS (cmd_buf, cmd_len/2);
723
724 return 0;
725 }
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740 static void cdrom_buffer_sectors (ide_dev_t *dev, unsigned long sector,
741 int sectors_to_transfer)
742 {
743 struct cdrom_info *info = &cdrom_info[DEV_HWIF][dev->select.b.drive];
744
745
746 int sectors_to_buffer = MIN (sectors_to_transfer,
747 (SECTOR_BUFFER_SIZE >> SECTOR_BITS) -
748 info->nsectors_buffered);
749
750 char *dest;
751
752
753
754
755 if (info->sector_buffer == NULL)
756 {
757 info->sector_buffer = (char *) kmalloc (SECTOR_BUFFER_SIZE, GFP_ATOMIC);
758
759
760 if (info->sector_buffer == NULL)
761 sectors_to_buffer = 0;
762 }
763
764
765 if (info->nsectors_buffered == 0)
766 info->sector_buffered = sector;
767
768
769 dest = info->sector_buffer + info->nsectors_buffered * SECTOR_SIZE;
770 while (sectors_to_buffer > 0)
771 {
772 IN_WORDS (dest, SECTOR_SIZE / 2);
773 --sectors_to_buffer;
774 --sectors_to_transfer;
775 ++info->nsectors_buffered;
776 dest += SECTOR_SIZE;
777 }
778
779
780 while (sectors_to_transfer > 0)
781 {
782 char dum[SECTOR_SIZE];
783 IN_WORDS (dum, sizeof (dum) / 2);
784 --sectors_to_transfer;
785 }
786 }
787
788
789
790
791
792
793
794 static inline
795 int cdrom_read_check_ireason (ide_dev_t *dev, int len, int ireason)
796 {
797 ireason &= 3;
798 if (ireason == 2) return 0;
799
800 if (ireason == 0)
801 {
802
803 printk ("%s: cdrom_read_intr: "
804 "Drive wants to transfer data the wrong way!\n",
805 dev->name);
806
807
808
809 while (len > 0)
810 {
811 short dum = 0;
812 OUT_WORDS (&dum, 1);
813 len -= 2;
814 }
815 }
816
817 else
818 {
819
820 printk ("%s: cdrom_read_intr: bad interrupt reason %d\n",
821 dev->name, ireason);
822 }
823
824 cdrom_end_request (0, dev);
825 DO_REQUEST;
826 return -1;
827 }
828
829
830
831
832
833 static void cdrom_read_intr (ide_dev_t *dev)
834 {
835 int stat;
836 int ireason, len, sectors_to_transfer, nskip;
837
838 struct request *rq = ide_cur_rq[DEV_HWIF];
839
840
841 if (cdrom_decode_status (dev, 0, &stat)) return;
842
843
844 ireason = IN_BYTE (HD_NSECTOR, DEV_HWIF);
845 len = IN_BYTE (HD_LCYL, DEV_HWIF) + 256 * IN_BYTE (HD_HCYL, DEV_HWIF);
846
847
848 if ((stat & DRQ_STAT) == 0)
849 {
850
851
852 if (rq->current_nr_sectors > 0)
853 {
854 printk ("%s: cdrom_read_intr: data underrun (%ld blocks)\n",
855 dev->name, rq->current_nr_sectors);
856 cdrom_end_request (0, dev);
857 }
858 else
859 cdrom_end_request (1, dev);
860
861 DO_REQUEST;
862 return;
863 }
864
865
866 if (cdrom_read_check_ireason (dev, len, ireason)) return;
867
868
869
870 if ((len % SECTOR_SIZE) != 0)
871 {
872 printk ("%s: cdrom_read_intr: Bad transfer size %d\n",
873 dev->name, len);
874 printk (" This drive is not supported by this version of the driver\n");
875 cdrom_end_request (0, dev);
876 DO_REQUEST;
877 return;
878 }
879
880
881 sectors_to_transfer = len / SECTOR_SIZE;
882
883
884 nskip = MIN ((int)(rq->current_nr_sectors - (rq->bh->b_size >> SECTOR_BITS)),
885 sectors_to_transfer);
886
887 while (nskip > 0)
888 {
889
890 char dum[SECTOR_SIZE];
891 IN_WORDS (dum, sizeof (dum) / 2);
892
893 --rq->current_nr_sectors;
894 --nskip;
895 --sectors_to_transfer;
896 }
897
898
899 while (sectors_to_transfer > 0)
900 {
901 int this_transfer;
902
903
904
905 if (rq->current_nr_sectors == 0 &&
906 rq->nr_sectors > 0)
907 cdrom_end_request (1, dev);
908
909
910
911 if (rq->current_nr_sectors == 0)
912 {
913 cdrom_buffer_sectors (dev, rq->sector, sectors_to_transfer);
914 sectors_to_transfer = 0;
915 }
916 else
917 {
918
919
920
921 this_transfer = MIN (sectors_to_transfer,
922 rq->current_nr_sectors);
923
924
925 while (this_transfer > 0)
926 {
927 IN_WORDS (rq->buffer, SECTOR_SIZE / 2);
928 rq->buffer += SECTOR_SIZE;
929 --rq->nr_sectors;
930 --rq->current_nr_sectors;
931 ++rq->sector;
932 --this_transfer;
933 --sectors_to_transfer;
934 }
935 }
936 }
937
938
939
940 ide_handler[DEV_HWIF] = cdrom_read_intr;
941 }
942
943
944
945
946
947
948 static int cdrom_read_from_buffer (ide_dev_t *dev)
949 {
950 struct cdrom_info *info = &cdrom_info[DEV_HWIF][dev->select.b.drive];
951 struct request *rq = ide_cur_rq[DEV_HWIF];
952
953
954 if (info->sector_buffer == NULL) return 0;
955
956
957
958 while (rq->nr_sectors > 0 &&
959 rq->sector >= info->sector_buffered &&
960 rq->sector < info->sector_buffered + info->nsectors_buffered)
961 {
962 if (rq->current_nr_sectors == 0)
963 cdrom_end_request (1, dev);
964
965 memcpy (rq->buffer,
966 info->sector_buffer +
967 (rq->sector - info->sector_buffered) * SECTOR_SIZE,
968 SECTOR_SIZE);
969 rq->buffer += SECTOR_SIZE;
970 --rq->current_nr_sectors;
971 --rq->nr_sectors;
972 ++rq->sector;
973 }
974
975
976 if (rq->nr_sectors == 0)
977 {
978 cdrom_end_request (1, dev);
979 return -1;
980 }
981
982
983 if (rq->current_nr_sectors == 0)
984 cdrom_end_request (1, dev);
985
986
987
988
989
990 if (rq->current_nr_sectors < (rq->bh->b_size >> SECTOR_BITS) &&
991 (rq->sector % SECTORS_PER_FRAME) != 0)
992 {
993 printk ("%s: cdrom_read_from_buffer: buffer botch (%ld)\n",
994 dev->name, rq->sector);
995 cdrom_end_request (0, dev);
996 return -1;
997 }
998
999 return 0;
1000 }
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 static int cdrom_start_read_continuation (ide_dev_t *dev)
1011 {
1012 struct packet_command pc;
1013 struct request *rq = ide_cur_rq[DEV_HWIF];
1014
1015 int nsect, sector, nframes, frame, nskip;
1016
1017
1018 nsect = rq->nr_sectors;
1019
1020
1021 sector = rq->sector;
1022
1023
1024
1025
1026
1027
1028
1029 nskip = (sector % SECTORS_PER_FRAME);
1030 if (nskip > 0)
1031 {
1032
1033 if (rq->current_nr_sectors != (rq->bh->b_size >> SECTOR_BITS))
1034 {
1035 printk ("%s: cdrom_start_read_continuation: buffer botch (%ld)\n",
1036 dev->name, rq->current_nr_sectors);
1037 cdrom_end_request (0, dev);
1038 DO_REQUEST;
1039 return 1;
1040 }
1041
1042 sector -= nskip;
1043 nsect += nskip;
1044 rq->current_nr_sectors += nskip;
1045 }
1046
1047
1048
1049 nframes = (nsect + SECTORS_PER_FRAME-1) / SECTORS_PER_FRAME;
1050 frame = sector / SECTORS_PER_FRAME;
1051
1052
1053 nframes = MIN (nframes, 65535);
1054
1055
1056 memset (&pc.c, 0, sizeof (pc.c));
1057 pc.c[0] = READ_10;
1058 pc.c[7] = (nframes >> 8);
1059 pc.c[8] = (nframes & 0xff);
1060
1061
1062 {
1063 union {
1064 struct {unsigned char b0, b1, b2, b3;} b;
1065 struct {unsigned long l0;} l;
1066 } conv;
1067 conv.l.l0 = frame;
1068 pc.c[2] = conv.b.b3;
1069 pc.c[3] = conv.b.b2;
1070 pc.c[4] = conv.b.b1;
1071 pc.c[5] = conv.b.b0;
1072 }
1073
1074 if (cdrom_transfer_packet_command (dev, pc.c, sizeof (pc.c)))
1075 return 1;
1076
1077
1078 ide_handler[DEV_HWIF] = cdrom_read_intr;
1079
1080 return 0;
1081 }
1082
1083
1084
1085
1086
1087
1088
1089
1090 static int cdrom_start_read (ide_dev_t *dev, unsigned int block)
1091 {
1092 struct request *rq = ide_cur_rq[DEV_HWIF];
1093
1094
1095
1096 restore_request (rq);
1097
1098
1099 if (cdrom_read_from_buffer (dev))
1100 return 1;
1101
1102
1103 cdrom_info[DEV_HWIF][dev->select.b.drive].nsectors_buffered = 0;
1104
1105 if (cdrom_start_packet_command (dev, 32768))
1106 return 1;
1107
1108 if (CDROM_FLAGS (dev)->drq_interrupt)
1109 ide_handler[DEV_HWIF] = (void (*)(ide_dev_t *))cdrom_start_read_continuation;
1110 else
1111 {
1112 if (cdrom_start_read_continuation (dev))
1113 return 1;
1114 }
1115
1116 return 0;
1117 }
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 static int
1128 cdrom_request_sense (ide_dev_t *dev, struct atapi_request_sense *reqbuf);
1129
1130
1131
1132 static void cdrom_pc_intr (ide_dev_t *dev)
1133 {
1134 int ireason, len, stat, thislen;
1135 struct request *rq = ide_cur_rq[DEV_HWIF];
1136 struct packet_command *pc = (struct packet_command *)rq->buffer;
1137
1138
1139 if (cdrom_decode_status (dev, 0, &stat)) return;
1140
1141
1142 ireason = IN_BYTE (HD_NSECTOR, DEV_HWIF);
1143 len = IN_BYTE (HD_LCYL, DEV_HWIF) + 256 * IN_BYTE (HD_HCYL, DEV_HWIF);
1144
1145
1146
1147 if ((stat & DRQ_STAT) == 0)
1148 {
1149
1150
1151 if (pc->c[0] == REQUEST_SENSE && pc->buflen > 0 && pc->buflen <= 5) {
1152 while (pc->buflen > 0) {
1153 *pc->buffer++ = 0;
1154 --pc->buflen;
1155 }
1156 }
1157
1158 if (pc->buflen == 0)
1159 cdrom_end_request (1, dev);
1160 else
1161 {
1162 printk ("%s: cdrom_pc_intr: data underrun %d\n",
1163 dev->name, pc->buflen);
1164 pc->stat = 1;
1165 cdrom_end_request (1, dev);
1166 }
1167 DO_REQUEST;
1168 return;
1169 }
1170
1171
1172 thislen = pc->buflen;
1173 if (thislen < 0) thislen = -thislen;
1174 if (thislen > len) thislen = len;
1175
1176
1177 if ((ireason & 3) == 0)
1178 {
1179
1180 if (pc->buflen > 0)
1181 {
1182 printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
1183 dev->name);
1184 pc->stat = 1;
1185 thislen = 0;
1186 }
1187
1188
1189 OUT_WORDS (pc->buffer, thislen / 2);
1190
1191
1192
1193 while (len > thislen)
1194 {
1195 short dum = 0;
1196 OUT_WORDS (&dum, 1);
1197 len -= 2;
1198 }
1199
1200
1201 pc->buffer += thislen;
1202 pc->buflen += thislen;
1203 }
1204
1205
1206 else if ((ireason & 3) == 2)
1207 {
1208
1209 if (pc->buflen < 0)
1210 {
1211 printk ("%s: cdrom_pc_intr: Drive wants to transfer data the wrong way!\n",
1212 dev->name);
1213 pc->stat = 1;
1214 thislen = 0;
1215 }
1216
1217
1218 IN_WORDS (pc->buffer, thislen / 2);
1219
1220
1221
1222 while (len > thislen)
1223 {
1224 short dum = 0;
1225 IN_WORDS (&dum, 1);
1226 len -= 2;
1227 }
1228
1229
1230 pc->buffer += thislen;
1231 pc->buflen -= thislen;
1232 }
1233
1234 else
1235 {
1236 printk ("%s: cdrom_pc_intr: The drive appears confused (ireason = 0x%2x)\n",
1237 dev->name, ireason);
1238 pc->stat = 1;
1239 }
1240
1241
1242 ide_handler[DEV_HWIF] = cdrom_pc_intr;
1243 }
1244
1245
1246 static int cdrom_do_pc_continuation (ide_dev_t *dev)
1247 {
1248 struct request *rq = ide_cur_rq[DEV_HWIF];
1249 struct packet_command *pc = (struct packet_command *)rq->buffer;
1250
1251 if (cdrom_transfer_packet_command (dev, pc->c, sizeof (pc->c)))
1252 return 1;
1253
1254
1255 ide_handler[DEV_HWIF] = cdrom_pc_intr;
1256
1257 return 0;
1258 }
1259
1260
1261 static int cdrom_do_packet_command (ide_dev_t *dev)
1262 {
1263 int len;
1264 struct request *rq = ide_cur_rq[DEV_HWIF];
1265 struct packet_command *pc = (struct packet_command *)rq->buffer;
1266
1267 len = pc->buflen;
1268 if (len < 0) len = -len;
1269
1270 pc->stat = 0;
1271
1272 if (cdrom_start_packet_command (dev, len))
1273 return 1;
1274
1275 if (CDROM_FLAGS (dev)->drq_interrupt)
1276 ide_handler[DEV_HWIF] = (void (*)(ide_dev_t *))cdrom_do_pc_continuation;
1277 else
1278 {
1279 if (cdrom_do_pc_continuation (dev))
1280 return 1;
1281 }
1282
1283 return 0;
1284 }
1285
1286
1287 static
1288 int cdrom_queue_packet_command (ide_dev_t *dev, struct packet_command *pc)
1289 {
1290 int retries = 3;
1291 unsigned long flags;
1292 struct request req, **p, **pfirst;
1293 struct semaphore sem = MUTEX_LOCKED;
1294 int major = ide_major[DEV_HWIF];
1295
1296 retry:
1297 req.dev = MKDEV (major, (dev->select.b.drive) << PARTN_BITS);
1298 req.cmd = PACKET_COMMAND;
1299 req.errors = 0;
1300 req.sector = 0;
1301 req.nr_sectors = 0;
1302 req.current_nr_sectors = 0;
1303 req.buffer = (char *)pc;
1304 req.sem = &sem;
1305 req.bh = NULL;
1306 req.bhtail = NULL;
1307 req.next = NULL;
1308
1309 save_flags (flags);
1310 cli ();
1311
1312 p = &blk_dev[major].current_request;
1313 pfirst = p;
1314 while ((*p) != NULL)
1315 {
1316 p = &((*p)->next);
1317 }
1318 *p = &req;
1319 if (p == pfirst)
1320 blk_dev[major].request_fn ();
1321
1322 restore_flags (flags);
1323
1324 down (&sem);
1325
1326 if (pc->stat != 0)
1327 {
1328
1329
1330
1331
1332
1333 struct atapi_request_sense *reqbuf =
1334 &cdrom_info[DEV_HWIF][dev->select.b.drive].sense_data;
1335
1336 if (pc->c[0] == REQUEST_SENSE || cdrom_request_sense (dev, reqbuf))
1337 {
1338 memset (reqbuf, 0, sizeof (*reqbuf));
1339 reqbuf->asc = 0xff;
1340 }
1341 cdrom_analyze_sense_data (dev, reqbuf, pc);
1342
1343
1344
1345 if (reqbuf->sense_key == UNIT_ATTENTION && retries > 0)
1346 {
1347 --retries;
1348 goto retry;
1349 }
1350
1351 return -EIO;
1352 }
1353 else
1354 return 0;
1355 }
1356
1357
1358
1359
1360
1361
1362
1363 static int do_rw_cdrom (ide_dev_t *dev, unsigned long block)
1364 {
1365 struct request *rq = ide_cur_rq[DEV_HWIF];
1366
1367 if (rq -> cmd == PACKET_COMMAND || rq -> cmd == REQUEST_SENSE_COMMAND)
1368 return cdrom_do_packet_command (dev);
1369
1370 if (rq -> cmd != READ)
1371 {
1372 printk ("ide-cd: bad cmd %d\n", rq -> cmd);
1373 cdrom_end_request (0, dev);
1374 return 1;
1375 }
1376
1377 return cdrom_start_read (dev, block);
1378 }
1379
1380
1381
1382
1383
1384
1385
1386 static inline
1387 void byte_swap_word (unsigned short *x)
1388 {
1389 char *c = (char *)x;
1390 char d = c[0];
1391 c[0] = c[1];
1392 c[1] = d;
1393 }
1394
1395
1396 static inline
1397 void byte_swap_long (unsigned *x)
1398 {
1399 char *c = (char *)x;
1400 char d = c[0];
1401 c[0] = c[3];
1402 c[3] = d;
1403 d = c[1];
1404 c[1] = c[2];
1405 c[2] = d;
1406 }
1407
1408
1409 static
1410 int bin2bcd (int x)
1411 {
1412 return (x%10) | ((x/10) << 4);
1413 }
1414
1415
1416 static inline
1417 void lba_to_msf (int lba, byte *m, byte *s, byte *f)
1418 {
1419 lba += CD_BLOCK_OFFSET;
1420 lba &= 0xffffff;
1421 *m = lba / (CD_SECS * CD_FRAMES);
1422 lba %= (CD_SECS * CD_FRAMES);
1423 *s = lba / CD_FRAMES;
1424 *f = lba % CD_FRAMES;
1425 }
1426
1427
1428 static inline
1429 int msf_to_lba (byte m, byte s, byte f)
1430 {
1431 return (((m * CD_SECS) + s) * CD_FRAMES + f) - CD_BLOCK_OFFSET;
1432 }
1433
1434
1435 static void
1436 cdrom_check_status (ide_dev_t *dev)
1437 {
1438 struct packet_command pc;
1439
1440 memset (&pc, 0, sizeof (pc));
1441
1442 pc.c[0] = TEST_UNIT_READY;
1443
1444 (void) cdrom_queue_packet_command (dev, &pc);
1445 }
1446
1447
1448 static int
1449 cdrom_request_sense (ide_dev_t *dev, struct atapi_request_sense *reqbuf)
1450 {
1451 struct packet_command pc;
1452
1453 memset (&pc, 0, sizeof (pc));
1454
1455 pc.c[0] = REQUEST_SENSE;
1456 pc.c[4] = sizeof (*reqbuf);
1457 pc.buffer = (char *)reqbuf;
1458 pc.buflen = sizeof (*reqbuf);
1459
1460 return cdrom_queue_packet_command (dev, &pc);
1461 }
1462
1463
1464 #if 0
1465
1466 static int
1467 cdrom_lockdoor (ide_dev_t *dev, int lockflag)
1468 {
1469 struct packet_command pc;
1470
1471 memset (&pc, 0, sizeof (pc));
1472
1473 pc.c[0] = ALLOW_MEDIUM_REMOVAL;
1474 pc.c[4] = (lockflag != 0);
1475 return cdrom_queue_packet_command (dev, &pc);
1476 }
1477 #endif
1478
1479
1480
1481
1482 static int
1483 cdrom_eject (ide_dev_t *dev, int ejectflag)
1484 {
1485 struct packet_command pc;
1486
1487 memset (&pc, 0, sizeof (pc));
1488
1489 pc.c[0] = START_STOP;
1490 pc.c[4] = 2 + (ejectflag != 0);
1491 return cdrom_queue_packet_command (dev, &pc);
1492 }
1493
1494
1495 static int
1496 cdrom_pause (ide_dev_t *dev, int pauseflag)
1497 {
1498 struct packet_command pc;
1499
1500 memset (&pc, 0, sizeof (pc));
1501
1502 pc.c[0] = SCMD_PAUSE_RESUME;
1503 pc.c[8] = !pauseflag;
1504 return cdrom_queue_packet_command (dev, &pc);
1505 }
1506
1507
1508 static int
1509 cdrom_startstop (ide_dev_t *dev, int startflag)
1510 {
1511 struct packet_command pc;
1512
1513 memset (&pc, 0, sizeof (pc));
1514
1515 pc.c[0] = START_STOP;
1516 pc.c[1] = 1;
1517 pc.c[4] = startflag;
1518 return cdrom_queue_packet_command (dev, &pc);
1519 }
1520
1521
1522 static int
1523 cdrom_read_tocentry (ide_dev_t *dev, int trackno, int msf_flag,
1524 char *buf, int buflen)
1525 {
1526 struct packet_command pc;
1527
1528 memset (&pc, 0, sizeof (pc));
1529
1530 pc.buffer = buf;
1531 pc.buflen = buflen;
1532 pc.c[0] = SCMD_READ_TOC;
1533 pc.c[6] = trackno;
1534 pc.c[7] = (buflen >> 8);
1535 pc.c[8] = (buflen & 0xff);
1536 if (msf_flag) pc.c[1] = 2;
1537 return cdrom_queue_packet_command (dev, &pc);
1538 }
1539
1540
1541
1542 static int
1543 cdrom_read_toc (ide_dev_t *dev)
1544 {
1545 int msf_flag;
1546 int stat, ntracks, i;
1547 struct atapi_toc *toc = cdrom_info[DEV_HWIF][dev->select.b.drive].toc;
1548
1549 if (toc == NULL)
1550 {
1551
1552 toc = (struct atapi_toc *) kmalloc (sizeof (struct atapi_toc),
1553 GFP_KERNEL);
1554 cdrom_info[DEV_HWIF][dev->select.b.drive].toc = toc;
1555 }
1556
1557 if (toc == NULL)
1558 {
1559 printk ("%s: No cdrom TOC buffer!\n", dev->name);
1560 return -EIO;
1561 }
1562
1563
1564
1565 if (CDROM_FLAGS (dev)->toc_valid)
1566 cdrom_check_status (dev);
1567
1568 if (CDROM_FLAGS (dev)->toc_valid) return 0;
1569
1570
1571 msf_flag = (CDROM_FLAGS (dev)->no_lba_toc);
1572
1573
1574 stat = cdrom_read_tocentry (dev, 0, msf_flag, (char *)toc,
1575 sizeof (struct atapi_toc_header) +
1576 sizeof (struct atapi_toc_entry));
1577 if (stat) return stat;
1578
1579 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1580 if (ntracks <= 0) return -EIO;
1581 if (ntracks > MAX_TRACKS) ntracks = MAX_TRACKS;
1582
1583
1584 stat = cdrom_read_tocentry (dev, 0, msf_flag, (char *)toc,
1585 sizeof (struct atapi_toc_header) +
1586 (ntracks+1) * sizeof (struct atapi_toc_entry));
1587 if (stat) return stat;
1588 byte_swap_word (&toc->hdr.toc_length);
1589 for (i=0; i<=ntracks; i++)
1590 {
1591 if (msf_flag)
1592 {
1593 byte *adr = (byte *)&(toc->ent[i].lba);
1594 toc->ent[i].lba = msf_to_lba (adr[1], adr[2], adr[3]);
1595 }
1596 else
1597 byte_swap_long (&toc->ent[i].lba);
1598 }
1599
1600
1601 CDROM_FLAGS (dev)->toc_valid = 1;
1602
1603 return 0;
1604 }
1605
1606
1607 static int
1608 cdrom_read_subchannel (ide_dev_t *dev,
1609 char *buf, int buflen)
1610 {
1611 struct packet_command pc;
1612
1613 memset (&pc, 0, sizeof (pc));
1614
1615 pc.buffer = buf;
1616 pc.buflen = buflen;
1617 pc.c[0] = SCMD_READ_SUBCHANNEL;
1618 pc.c[2] = 0x40;
1619 pc.c[3] = 0x01;
1620 pc.c[7] = (buflen >> 8);
1621 pc.c[8] = (buflen & 0xff);
1622 return cdrom_queue_packet_command (dev, &pc);
1623 }
1624
1625
1626
1627 static int
1628 cdrom_mode_sense (ide_dev_t *dev, int pageno, int modeflag,
1629 char *buf, int buflen)
1630 {
1631 struct packet_command pc;
1632
1633 memset (&pc, 0, sizeof (pc));
1634
1635 pc.buffer = buf;
1636 pc.buflen = buflen;
1637 pc.c[0] = MODE_SENSE_10;
1638 pc.c[2] = pageno | (modeflag << 6);
1639 pc.c[7] = (buflen >> 8);
1640 pc.c[8] = (buflen & 0xff);
1641 return cdrom_queue_packet_command (dev, &pc);
1642 }
1643
1644
1645 static int
1646 cdrom_mode_select (ide_dev_t *dev, int pageno, char *buf, int buflen)
1647 {
1648 struct packet_command pc;
1649
1650 memset (&pc, 0, sizeof (pc));
1651
1652 pc.buffer = buf;
1653 pc.buflen = - buflen;
1654 pc.c[0] = MODE_SELECT_10;
1655 pc.c[1] = 0x10;
1656 pc.c[2] = pageno;
1657 pc.c[7] = (buflen >> 8);
1658 pc.c[8] = (buflen & 0xff);
1659 return cdrom_queue_packet_command (dev, &pc);
1660 }
1661
1662
1663 static int
1664 cdrom_play_lba_range_play12 (ide_dev_t *dev, int lba_start, int lba_end)
1665 {
1666 struct packet_command pc;
1667
1668 memset (&pc, 0, sizeof (pc));
1669
1670 pc.c[0] = SCMD_PLAYAUDIO12;
1671 *(int *)(&pc.c[2]) = lba_start;
1672 *(int *)(&pc.c[6]) = lba_end - lba_start;
1673 byte_swap_long ((int *)(&pc.c[2]));
1674 byte_swap_long ((int *)(&pc.c[6]));
1675
1676 return cdrom_queue_packet_command (dev, &pc);
1677 }
1678
1679
1680 static int
1681 cdrom_play_lba_range_msf (ide_dev_t *dev, int lba_start, int lba_end)
1682 {
1683 struct packet_command pc;
1684
1685 memset (&pc, 0, sizeof (pc));
1686
1687 pc.c[0] = SCMD_PLAYAUDIO_MSF;
1688 lba_to_msf (lba_start, &pc.c[3], &pc.c[4], &pc.c[5]);
1689 lba_to_msf (lba_end-1, &pc.c[6], &pc.c[7], &pc.c[8]);
1690
1691 if (CDROM_FLAGS (dev)->msf_as_bcd)
1692 {
1693 pc.c[3] = bin2bcd (pc.c[3]);
1694 pc.c[4] = bin2bcd (pc.c[4]);
1695 pc.c[5] = bin2bcd (pc.c[5]);
1696 pc.c[6] = bin2bcd (pc.c[6]);
1697 pc.c[7] = bin2bcd (pc.c[7]);
1698 pc.c[8] = bin2bcd (pc.c[8]);
1699 }
1700
1701 return cdrom_queue_packet_command (dev, &pc);
1702 }
1703
1704
1705
1706
1707 static int
1708 cdrom_play_lba_range (ide_dev_t *dev, int lba_start, int lba_end)
1709 {
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721 if (CDROM_FLAGS (dev)->no_playaudio12)
1722 return cdrom_play_lba_range_msf (dev, lba_start, lba_end);
1723 else
1724 {
1725 int stat;
1726 struct atapi_request_sense *reqbuf;
1727
1728 stat = cdrom_play_lba_range_play12 (dev, lba_start, lba_end);
1729 if (stat == 0) return 0;
1730
1731
1732 reqbuf = &cdrom_info[DEV_HWIF][dev->select.b.drive].sense_data;
1733 if (reqbuf->sense_key == 0x05 && reqbuf->asc == 0x20)
1734 {
1735
1736
1737 printk ("%s: Drive does not support PLAYAUDIO12; "
1738 "trying PLAYAUDIO_MSF\n", dev->name);
1739 CDROM_FLAGS (dev)->no_playaudio12 = 1;
1740 CDROM_FLAGS (dev)->msf_as_bcd = 1;
1741 return cdrom_play_lba_range_msf (dev, lba_start, lba_end);
1742 }
1743
1744
1745 return stat;
1746 }
1747 }
1748
1749
1750 static
1751 int cdrom_get_toc_entry (ide_dev_t *dev, int track,
1752 struct atapi_toc_entry **ent)
1753 {
1754 int stat, ntracks;
1755 struct atapi_toc *toc;
1756
1757
1758 stat = cdrom_read_toc (dev);
1759 if (stat) return stat;
1760
1761 toc = cdrom_info[DEV_HWIF][dev->select.b.drive].toc;
1762
1763
1764 ntracks = toc->hdr.last_track - toc->hdr.first_track + 1;
1765 if (track == CDROM_LEADOUT)
1766 *ent = &toc->ent[ntracks];
1767 else if (track < toc->hdr.first_track ||
1768 track > toc->hdr.last_track)
1769 return -EINVAL;
1770 else
1771 *ent = &toc->ent[track - toc->hdr.first_track];
1772
1773 return 0;
1774 }
1775
1776
1777 static int ide_cdrom_ioctl (ide_dev_t *dev, struct inode *inode,
1778 struct file *file, unsigned int cmd, unsigned long arg)
1779 {
1780 switch (cmd)
1781 {
1782 case CDROMEJECT:
1783 return cdrom_eject (dev, 0);
1784
1785 case CDROMPAUSE:
1786 return cdrom_pause (dev, 1);
1787
1788 case CDROMRESUME:
1789 return cdrom_pause (dev, 0);
1790
1791 case CDROMSTART:
1792 return cdrom_startstop (dev, 1);
1793
1794 case CDROMSTOP:
1795 return cdrom_startstop (dev, 0);
1796
1797 case CDROMPLAYMSF:
1798 {
1799 struct cdrom_msf msf;
1800 int stat, lba_start, lba_end;
1801
1802 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (msf));
1803 if (stat) return stat;
1804
1805 memcpy_fromfs (&msf, (void *) arg, sizeof(msf));
1806
1807 lba_start = msf_to_lba (msf.cdmsf_min0, msf.cdmsf_sec0,
1808 msf.cdmsf_frame0);
1809 lba_end = msf_to_lba (msf.cdmsf_min1, msf.cdmsf_sec1,
1810 msf.cdmsf_frame1) + 1;
1811
1812 if (lba_end <= lba_start) return -EINVAL;
1813
1814 return cdrom_play_lba_range (dev, lba_start, lba_end);
1815 }
1816
1817
1818
1819 case CDROMPLAYTRKIND:
1820 {
1821 int stat, lba_start, lba_end;
1822 struct cdrom_ti ti;
1823 struct atapi_toc_entry *first_toc, *last_toc;
1824
1825 stat = verify_area (VERIFY_READ, (void *)arg, sizeof (ti));
1826 if (stat) return stat;
1827
1828 memcpy_fromfs (&ti, (void *) arg, sizeof(ti));
1829
1830 stat = cdrom_get_toc_entry (dev, ti.cdti_trk0, &first_toc);
1831 if (stat) return stat;
1832 stat = cdrom_get_toc_entry (dev, ti.cdti_trk1, &last_toc);
1833 if (stat) return stat;
1834
1835 if (ti.cdti_trk1 != CDROM_LEADOUT) ++last_toc;
1836 lba_start = first_toc->lba;
1837 lba_end = last_toc->lba;
1838
1839 if (lba_end <= lba_start) return -EINVAL;
1840
1841 return cdrom_play_lba_range (dev, lba_start, lba_end);
1842 }
1843
1844 case CDROMREADTOCHDR:
1845 {
1846 int stat;
1847 struct cdrom_tochdr tochdr;
1848 struct atapi_toc *toc;
1849
1850 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tochdr));
1851 if (stat) return stat;
1852
1853
1854 stat = cdrom_read_toc (dev);
1855 if (stat) return stat;
1856
1857 toc = cdrom_info[DEV_HWIF][dev->select.b.drive].toc;
1858 tochdr.cdth_trk0 = toc->hdr.first_track;
1859 tochdr.cdth_trk1 = toc->hdr.last_track;
1860
1861 memcpy_tofs ((void *) arg, &tochdr, sizeof (tochdr));
1862
1863 return stat;
1864 }
1865
1866 case CDROMREADTOCENTRY:
1867 {
1868 int stat;
1869 struct cdrom_tocentry tocentry;
1870 struct atapi_toc_entry *toce;
1871
1872 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (tocentry));
1873 if (stat) return stat;
1874 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (tocentry));
1875 if (stat) return stat;
1876
1877 memcpy_fromfs (&tocentry, (void *) arg, sizeof (tocentry));
1878
1879 stat = cdrom_get_toc_entry (dev, tocentry.cdte_track, &toce);
1880 if (stat) return stat;
1881
1882 tocentry.cdte_ctrl = toce->control;
1883 tocentry.cdte_adr = toce->adr;
1884
1885 if (tocentry.cdte_format == CDROM_MSF)
1886 {
1887
1888 lba_to_msf (toce->lba,
1889 &tocentry.cdte_addr.msf.minute,
1890 &tocentry.cdte_addr.msf.second,
1891 &tocentry.cdte_addr.msf.frame);
1892 }
1893 else
1894 tocentry.cdte_addr.lba = toce->lba;
1895
1896 memcpy_tofs ((void *) arg, &tocentry, sizeof (tocentry));
1897
1898 return stat;
1899 }
1900
1901 case CDROMSUBCHNL:
1902 {
1903 char buffer[16];
1904 int stat, abs_lba, rel_lba;
1905 struct cdrom_subchnl subchnl;
1906
1907 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (subchnl));
1908 if (stat) return stat;
1909 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (subchnl));
1910 if (stat) return stat;
1911
1912 memcpy_fromfs (&subchnl, (void *) arg, sizeof (subchnl));
1913
1914 stat = cdrom_read_subchannel (dev, buffer, sizeof (buffer));
1915 if (stat) return stat;
1916
1917 abs_lba = *(int *)&buffer[8];
1918 rel_lba = *(int *)&buffer[12];
1919 byte_swap_long (&abs_lba);
1920 byte_swap_long (&rel_lba);
1921
1922 if (subchnl.cdsc_format == CDROM_MSF)
1923 {
1924 lba_to_msf (abs_lba,
1925 &subchnl.cdsc_absaddr.msf.minute,
1926 &subchnl.cdsc_absaddr.msf.second,
1927 &subchnl.cdsc_absaddr.msf.frame);
1928 lba_to_msf (rel_lba,
1929 &subchnl.cdsc_reladdr.msf.minute,
1930 &subchnl.cdsc_reladdr.msf.second,
1931 &subchnl.cdsc_reladdr.msf.frame);
1932 }
1933 else
1934 {
1935 subchnl.cdsc_absaddr.lba = abs_lba;
1936 subchnl.cdsc_reladdr.lba = rel_lba;
1937 }
1938
1939 subchnl.cdsc_audiostatus = buffer[1];
1940 subchnl.cdsc_ctrl = buffer[5] & 0xf;
1941 subchnl.cdsc_trk = buffer[6];
1942 subchnl.cdsc_ind = buffer[7];
1943
1944 memcpy_tofs ((void *) arg, &subchnl, sizeof (subchnl));
1945
1946 return stat;
1947 }
1948
1949 case CDROMVOLCTRL:
1950 {
1951 struct cdrom_volctrl volctrl;
1952 char buffer[24], mask[24];
1953 int stat;
1954
1955 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (volctrl));
1956 if (stat) return stat;
1957 memcpy_fromfs (&volctrl, (void *) arg, sizeof (volctrl));
1958
1959 stat = cdrom_mode_sense (dev, 0x0e, 0, buffer, sizeof (buffer));
1960 if (stat) return stat;
1961 stat = cdrom_mode_sense (dev, 0x0e, 1, mask , sizeof (buffer));
1962 if (stat) return stat;
1963
1964 buffer[1] = buffer[2] = 0;
1965
1966 buffer[17] = volctrl.channel0 & mask[17];
1967 buffer[19] = volctrl.channel1 & mask[19];
1968 buffer[21] = volctrl.channel2 & mask[21];
1969 buffer[23] = volctrl.channel3 & mask[23];
1970
1971 return cdrom_mode_select (dev, 0x0e, buffer, sizeof (buffer));
1972 }
1973
1974 #ifdef TEST
1975 case 0x1234:
1976 {
1977 int stat;
1978 struct packet_command pc;
1979
1980 memset (&pc, 0, sizeof (pc));
1981
1982 stat = verify_area (VERIFY_READ, (void *) arg, sizeof (pc.c));
1983 if (stat) return stat;
1984 memcpy_fromfs (&pc.c, (void *) arg, sizeof (pc.c));
1985
1986 return cdrom_queue_packet_command (dev, &pc);
1987 }
1988
1989 case 0x1235:
1990 {
1991 int stat;
1992 struct atapi_request_sense reqbuf;
1993
1994 stat = verify_area (VERIFY_WRITE, (void *) arg, sizeof (reqbuf));
1995 if (stat) return stat;
1996
1997 stat = cdrom_request_sense (dev, &reqbuf);
1998
1999 memcpy_tofs ((void *) arg, &reqbuf, sizeof (reqbuf));
2000
2001 return stat;
2002 }
2003 #endif
2004
2005 default:
2006 return -EPERM;
2007 }
2008
2009 }
2010
2011
2012
2013
2014
2015
2016
2017 static int cdrom_check_media_change (ide_dev_t *dev)
2018 {
2019 int retval;
2020
2021 cdrom_check_status (dev);
2022
2023 retval = CDROM_FLAGS (dev)->media_changed;
2024 CDROM_FLAGS (dev)->media_changed = 0;
2025
2026 return retval;
2027 }
2028
2029
2030 static int
2031 cdrom_open (struct inode *ip, struct file *fp, ide_dev_t *dev)
2032 {
2033
2034 if (fp->f_mode & 2) return -EROFS;
2035
2036 #if 0
2037
2038 if (dev->usage == 1)
2039 (void) cdrom_lockdoor (dev, 1);
2040 #endif
2041
2042
2043 return 0;
2044 }
2045
2046
2047
2048
2049
2050
2051 static void
2052 cdrom_release (struct inode *inode, struct file *file, ide_dev_t *dev)
2053 {
2054 if (dev->usage == 0)
2055 {
2056 invalidate_buffers (inode->i_rdev);
2057
2058 #if 0
2059
2060 (void) cdrom_lockdoor (dev, 0);
2061 #endif
2062 }
2063 }
2064
2065
2066
2067
2068
2069
2070
2071 static void cdrom_setup (ide_dev_t *dev)
2072 {
2073
2074 ide_capacity[DEV_HWIF][dev->select.b.drive] = 0x1fffff;
2075
2076 ide_blksizes[DEV_HWIF][dev->select.b.drive << PARTN_BITS] = CD_FRAMESIZE;
2077
2078 dev->special.all = 0;
2079
2080 CDROM_FLAGS (dev)->media_changed = 0;
2081 CDROM_FLAGS (dev)->toc_valid = 0;
2082
2083 CDROM_FLAGS (dev)->no_playaudio12 = 0;
2084 CDROM_FLAGS (dev)->no_lba_toc = 0;
2085 CDROM_FLAGS (dev)->msf_as_bcd = 0;
2086 CDROM_FLAGS (dev)->drq_interrupt = ((dev->id->config & 0x0060) == 0x20);
2087
2088
2089 if (strcmp (dev->id->model, "CD220E") == 0)
2090 CDROM_FLAGS (dev)->no_lba_toc = 1;
2091
2092 else if (strcmp (dev->id->model, "TO-ICSLYAL") == 0 ||
2093 strcmp (dev->id->model, "OTI-SCYLLA") == 0)
2094 CDROM_FLAGS (dev)->no_lba_toc = 1;
2095
2096 else if (strcmp (dev->id->model, "CDA26803I SE") == 0)
2097 {
2098 CDROM_FLAGS (dev)->no_lba_toc = 1;
2099
2100
2101 CDROM_FLAGS (dev)->no_playaudio12 = 1;
2102 }
2103
2104 cdrom_info[DEV_HWIF][dev->select.b.drive].toc = NULL;
2105 cdrom_info[DEV_HWIF][dev->select.b.drive].sector_buffer = NULL;
2106 cdrom_info[DEV_HWIF][dev->select.b.drive].sector_buffered = 0;
2107 cdrom_info[DEV_HWIF][dev->select.b.drive].nsectors_buffered = 0;
2108 }
2109
2110
2111 #undef MIN
2112 #undef SECTOR_SIZE
2113 #undef SECTOR_BITS
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126