forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optcd.c
2106 lines (1739 loc) · 50.4 KB
/
optcd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* linux/drivers/cdrom/optcd.c - Optics Storage 8000 AT CDROM driver
$Id: optcd.c,v 1.11 1997/01/26 07:13:00 davem Exp $
Copyright (C) 1995 Leo Spiekman ([email protected])
Based on Aztech CD268 CDROM driver by Werner Zimmermann and preworks
by Eberhard Moenkeberg ([email protected]).
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* Revision history
14-5-95 v0.0 Plays sound tracks. No reading of data CDs yet.
Detection of disk change doesn't work.
21-5-95 v0.1 First ALPHA version. CD can be mounted. The
device major nr is borrowed from the Aztech
driver. Speed is around 240 kb/s, as measured
with "time dd if=/dev/cdrom of=/dev/null \
bs=2048 count=4096".
24-6-95 v0.2 Reworked the #defines for the command codes
and the like, as well as the structure of
the hardware communication protocol, to
reflect the "official" documentation, kindly
supplied by C.K. Tan, Optics Storage Pte. Ltd.
Also tidied up the state machine somewhat.
28-6-95 v0.3 Removed the ISP-16 interface code, as this
should go into its own driver. The driver now
has its own major nr.
Disk change detection now seems to work, too.
This version became part of the standard
kernel as of version 1.3.7
24-9-95 v0.4 Re-inserted ISP-16 interface code which I
copied from sjcd.c, with a few changes.
Updated README.optcd. Submitted for
inclusion in 1.3.21
29-9-95 v0.4a Fixed bug that prevented compilation as module
25-10-95 v0.5 Started multisession code. Implementation
copied from Werner Zimmermann, who copied it
from Heiko Schlittermann's mcdx.
17-1-96 v0.6 Multisession works; some cleanup too.
18-4-96 v0.7 Increased some timing constants;
thanks to Luke McFarlane. Also tidied up some
printk behaviour. ISP16 initialization
is now handled by a separate driver.
09-11-99 Make kernel-parameter implementation work with 2.3.x
Removed init_module & cleanup_module in favor of
module_init & module_exit.
Torben Mathiasen <[email protected]>
*/
/* Includes */
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <asm/io.h>
#include <linux/blkdev.h>
#include <linux/cdrom.h>
#include "optcd.h"
#include <asm/uaccess.h>
#define MAJOR_NR OPTICS_CDROM_MAJOR
#define QUEUE (opt_queue)
#define CURRENT elv_next_request(opt_queue)
/* Debug support */
/* Don't forget to add new debug flags here. */
#if DEBUG_DRIVE_IF | DEBUG_VFS | DEBUG_CONV | DEBUG_TOC | \
DEBUG_BUFFERS | DEBUG_REQUEST | DEBUG_STATE | DEBUG_MULTIS
#define DEBUG(x) debug x
static void debug(int debug_this, const char* fmt, ...)
{
char s[1024];
va_list args;
if (!debug_this)
return;
va_start(args, fmt);
vsprintf(s, fmt, args);
printk(KERN_DEBUG "optcd: %s\n", s);
va_end(args);
}
#else
#define DEBUG(x)
#endif
/* Drive hardware/firmware characteristics
Identifiers in accordance with Optics Storage documentation */
#define optcd_port optcd /* Needed for the modutils. */
static short optcd_port = OPTCD_PORTBASE; /* I/O base of drive. */
module_param(optcd_port, short, 0);
/* Drive registers, read */
#define DATA_PORT optcd_port /* Read data/status */
#define STATUS_PORT optcd_port+1 /* Indicate data/status availability */
/* Drive registers, write */
#define COMIN_PORT optcd_port /* For passing command/parameter */
#define RESET_PORT optcd_port+1 /* Write anything and wait 0.5 sec */
#define HCON_PORT optcd_port+2 /* Host Xfer Configuration */
/* Command completion/status read from DATA register */
#define ST_DRVERR 0x80
#define ST_DOOR_OPEN 0x40
#define ST_MIXEDMODE_DISK 0x20
#define ST_MODE_BITS 0x1c
#define ST_M_STOP 0x00
#define ST_M_READ 0x04
#define ST_M_AUDIO 0x04
#define ST_M_PAUSE 0x08
#define ST_M_INITIAL 0x0c
#define ST_M_ERROR 0x10
#define ST_M_OTHERS 0x14
#define ST_MODE2TRACK 0x02
#define ST_DSK_CHG 0x01
#define ST_L_LOCK 0x01
#define ST_CMD_OK 0x00
#define ST_OP_OK 0x01
#define ST_PA_OK 0x02
#define ST_OP_ERROR 0x05
#define ST_PA_ERROR 0x06
/* Error codes (appear as command completion code from DATA register) */
/* Player related errors */
#define ERR_ILLCMD 0x11 /* Illegal command to player module */
#define ERR_ILLPARM 0x12 /* Illegal parameter to player module */
#define ERR_SLEDGE 0x13
#define ERR_FOCUS 0x14
#define ERR_MOTOR 0x15
#define ERR_RADIAL 0x16
#define ERR_PLL 0x17 /* PLL lock error */
#define ERR_SUB_TIM 0x18 /* Subcode timeout error */
#define ERR_SUB_NF 0x19 /* Subcode not found error */
#define ERR_TRAY 0x1a
#define ERR_TOC 0x1b /* Table of Contents read error */
#define ERR_JUMP 0x1c
/* Data errors */
#define ERR_MODE 0x21
#define ERR_FORM 0x22
#define ERR_HEADADDR 0x23 /* Header Address not found */
#define ERR_CRC 0x24
#define ERR_ECC 0x25 /* Uncorrectable ECC error */
#define ERR_CRC_UNC 0x26 /* CRC error and uncorrectable error */
#define ERR_ILLBSYNC 0x27 /* Illegal block sync error */
#define ERR_VDST 0x28 /* VDST not found */
/* Timeout errors */
#define ERR_READ_TIM 0x31 /* Read timeout error */
#define ERR_DEC_STP 0x32 /* Decoder stopped */
#define ERR_DEC_TIM 0x33 /* Decoder interrupt timeout error */
/* Function abort codes */
#define ERR_KEY 0x41 /* Key -Detected abort */
#define ERR_READ_FINISH 0x42 /* Read Finish */
/* Second Byte diagnostic codes */
#define ERR_NOBSYNC 0x01 /* No block sync */
#define ERR_SHORTB 0x02 /* Short block */
#define ERR_LONGB 0x03 /* Long block */
#define ERR_SHORTDSP 0x04 /* Short DSP word */
#define ERR_LONGDSP 0x05 /* Long DSP word */
/* Status availability flags read from STATUS register */
#define FL_EJECT 0x20
#define FL_WAIT 0x10 /* active low */
#define FL_EOP 0x08 /* active low */
#define FL_STEN 0x04 /* Status available when low */
#define FL_DTEN 0x02 /* Data available when low */
#define FL_DRQ 0x01 /* active low */
#define FL_RESET 0xde /* These bits are high after a reset */
#define FL_STDT (FL_STEN|FL_DTEN)
/* Transfer mode, written to HCON register */
#define HCON_DTS 0x08
#define HCON_SDRQB 0x04
#define HCON_LOHI 0x02
#define HCON_DMA16 0x01
/* Drive command set, written to COMIN register */
/* Quick response commands */
#define COMDRVST 0x20 /* Drive Status Read */
#define COMERRST 0x21 /* Error Status Read */
#define COMIOCTLISTAT 0x22 /* Status Read; reset disk changed bit */
#define COMINITSINGLE 0x28 /* Initialize Single Speed */
#define COMINITDOUBLE 0x29 /* Initialize Double Speed */
#define COMUNLOCK 0x30 /* Unlock */
#define COMLOCK 0x31 /* Lock */
#define COMLOCKST 0x32 /* Lock/Unlock Status */
#define COMVERSION 0x40 /* Get Firmware Revision */
#define COMVOIDREADMODE 0x50 /* Void Data Read Mode */
/* Read commands */
#define COMFETCH 0x60 /* Prefetch Data */
#define COMREAD 0x61 /* Read */
#define COMREADRAW 0x62 /* Read Raw Data */
#define COMREADALL 0x63 /* Read All 2646 Bytes */
/* Player control commands */
#define COMLEADIN 0x70 /* Seek To Lead-in */
#define COMSEEK 0x71 /* Seek */
#define COMPAUSEON 0x80 /* Pause On */
#define COMPAUSEOFF 0x81 /* Pause Off */
#define COMSTOP 0x82 /* Stop */
#define COMOPEN 0x90 /* Open Tray Door */
#define COMCLOSE 0x91 /* Close Tray Door */
#define COMPLAY 0xa0 /* Audio Play */
#define COMPLAY_TNO 0xa2 /* Audio Play By Track Number */
#define COMSUBQ 0xb0 /* Read Sub-q Code */
#define COMLOCATION 0xb1 /* Read Head Position */
/* Audio control commands */
#define COMCHCTRL 0xc0 /* Audio Channel Control */
/* Miscellaneous (test) commands */
#define COMDRVTEST 0xd0 /* Write Test Bytes */
#define COMTEST 0xd1 /* Diagnostic Test */
/* Low level drive interface. Only here we do actual I/O
Waiting for status / data available */
/* Busy wait until FLAG goes low. Return 0 on timeout. */
static inline int flag_low(int flag, unsigned long timeout)
{
int flag_high;
unsigned long count = 0;
while ((flag_high = (inb(STATUS_PORT) & flag)))
if (++count >= timeout)
break;
DEBUG((DEBUG_DRIVE_IF, "flag_low 0x%x count %ld%s",
flag, count, flag_high ? " timeout" : ""));
return !flag_high;
}
/* Timed waiting for status or data */
static int sleep_timeout; /* max # of ticks to sleep */
static DECLARE_WAIT_QUEUE_HEAD(waitq);
static void sleep_timer(unsigned long data);
static DEFINE_TIMER(delay_timer, sleep_timer, 0, 0);
static DEFINE_SPINLOCK(optcd_lock);
static struct request_queue *opt_queue;
/* Timer routine: wake up when desired flag goes low,
or when timeout expires. */
static void sleep_timer(unsigned long data)
{
int flags = inb(STATUS_PORT) & FL_STDT;
if (flags == FL_STDT && --sleep_timeout > 0) {
mod_timer(&delay_timer, jiffies + HZ/100); /* multi-statement macro */
} else
wake_up(&waitq);
}
/* Sleep until FLAG goes low. Return 0 on timeout or wrong flag low. */
static int sleep_flag_low(int flag, unsigned long timeout)
{
int flag_high;
DEBUG((DEBUG_DRIVE_IF, "sleep_flag_low"));
sleep_timeout = timeout;
flag_high = inb(STATUS_PORT) & flag;
if (flag_high && sleep_timeout > 0) {
mod_timer(&delay_timer, jiffies + HZ/100);
sleep_on(&waitq);
flag_high = inb(STATUS_PORT) & flag;
}
DEBUG((DEBUG_DRIVE_IF, "flag 0x%x count %ld%s",
flag, timeout, flag_high ? " timeout" : ""));
return !flag_high;
}
/* Low level drive interface. Only here we do actual I/O
Sending commands and parameters */
/* Errors in the command protocol */
#define ERR_IF_CMD_TIMEOUT 0x100
#define ERR_IF_ERR_TIMEOUT 0x101
#define ERR_IF_RESP_TIMEOUT 0x102
#define ERR_IF_DATA_TIMEOUT 0x103
#define ERR_IF_NOSTAT 0x104
/* Send command code. Return <0 indicates error */
static int send_cmd(int cmd)
{
unsigned char ack;
DEBUG((DEBUG_DRIVE_IF, "sending command 0x%02x\n", cmd));
outb(HCON_DTS, HCON_PORT); /* Enable Suspend Data Transfer */
outb(cmd, COMIN_PORT); /* Send command code */
if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */
return -ERR_IF_CMD_TIMEOUT;
ack = inb(DATA_PORT); /* read command acknowledge */
outb(HCON_SDRQB, HCON_PORT); /* Disable Suspend Data Transfer */
return ack==ST_OP_OK ? 0 : -ack;
}
/* Send command parameters. Return <0 indicates error */
static int send_params(struct cdrom_msf *params)
{
unsigned char ack;
DEBUG((DEBUG_DRIVE_IF, "sending parameters"
" %02x:%02x:%02x"
" %02x:%02x:%02x",
params->cdmsf_min0,
params->cdmsf_sec0,
params->cdmsf_frame0,
params->cdmsf_min1,
params->cdmsf_sec1,
params->cdmsf_frame1));
outb(params->cdmsf_min0, COMIN_PORT);
outb(params->cdmsf_sec0, COMIN_PORT);
outb(params->cdmsf_frame0, COMIN_PORT);
outb(params->cdmsf_min1, COMIN_PORT);
outb(params->cdmsf_sec1, COMIN_PORT);
outb(params->cdmsf_frame1, COMIN_PORT);
if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */
return -ERR_IF_CMD_TIMEOUT;
ack = inb(DATA_PORT); /* read command acknowledge */
return ack==ST_PA_OK ? 0 : -ack;
}
/* Send parameters for SEEK command. Return <0 indicates error */
static int send_seek_params(struct cdrom_msf *params)
{
unsigned char ack;
DEBUG((DEBUG_DRIVE_IF, "sending seek parameters"
" %02x:%02x:%02x",
params->cdmsf_min0,
params->cdmsf_sec0,
params->cdmsf_frame0));
outb(params->cdmsf_min0, COMIN_PORT);
outb(params->cdmsf_sec0, COMIN_PORT);
outb(params->cdmsf_frame0, COMIN_PORT);
if (!flag_low(FL_STEN, BUSY_TIMEOUT)) /* Wait for status */
return -ERR_IF_CMD_TIMEOUT;
ack = inb(DATA_PORT); /* read command acknowledge */
return ack==ST_PA_OK ? 0 : -ack;
}
/* Wait for command execution status. Choice between busy waiting
and sleeping. Return value <0 indicates timeout. */
static inline int get_exec_status(int busy_waiting)
{
unsigned char exec_status;
if (busy_waiting
? !flag_low(FL_STEN, BUSY_TIMEOUT)
: !sleep_flag_low(FL_STEN, SLEEP_TIMEOUT))
return -ERR_IF_CMD_TIMEOUT;
exec_status = inb(DATA_PORT);
DEBUG((DEBUG_DRIVE_IF, "returned exec status 0x%02x", exec_status));
return exec_status;
}
/* Wait busy for extra byte of data that a command returns.
Return value <0 indicates timeout. */
static inline int get_data(int short_timeout)
{
unsigned char data;
if (!flag_low(FL_STEN, short_timeout ? FAST_TIMEOUT : BUSY_TIMEOUT))
return -ERR_IF_DATA_TIMEOUT;
data = inb(DATA_PORT);
DEBUG((DEBUG_DRIVE_IF, "returned data 0x%02x", data));
return data;
}
/* Returns 0 if failed */
static int reset_drive(void)
{
unsigned long count = 0;
int flags;
DEBUG((DEBUG_DRIVE_IF, "reset drive"));
outb(0, RESET_PORT);
while (++count < RESET_WAIT)
inb(DATA_PORT);
count = 0;
while ((flags = (inb(STATUS_PORT) & FL_RESET)) != FL_RESET)
if (++count >= BUSY_TIMEOUT)
break;
DEBUG((DEBUG_DRIVE_IF, "reset %s",
flags == FL_RESET ? "succeeded" : "failed"));
if (flags != FL_RESET)
return 0; /* Reset failed */
outb(HCON_SDRQB, HCON_PORT); /* Disable Suspend Data Transfer */
return 1; /* Reset succeeded */
}
/* Facilities for asynchronous operation */
/* Read status/data availability flags FL_STEN and FL_DTEN */
static inline int stdt_flags(void)
{
return inb(STATUS_PORT) & FL_STDT;
}
/* Fetch status that has previously been waited for. <0 means not available */
static inline int fetch_status(void)
{
unsigned char status;
if (inb(STATUS_PORT) & FL_STEN)
return -ERR_IF_NOSTAT;
status = inb(DATA_PORT);
DEBUG((DEBUG_DRIVE_IF, "fetched exec status 0x%02x", status));
return status;
}
/* Fetch data that has previously been waited for. */
static inline void fetch_data(char *buf, int n)
{
insb(DATA_PORT, buf, n);
DEBUG((DEBUG_DRIVE_IF, "fetched 0x%x bytes", n));
}
/* Flush status and data fifos */
static inline void flush_data(void)
{
while ((inb(STATUS_PORT) & FL_STDT) != FL_STDT)
inb(DATA_PORT);
DEBUG((DEBUG_DRIVE_IF, "flushed fifos"));
}
/* Command protocol */
/* Send a simple command and wait for response. Command codes < COMFETCH
are quick response commands */
static inline int exec_cmd(int cmd)
{
int ack = send_cmd(cmd);
if (ack < 0)
return ack;
return get_exec_status(cmd < COMFETCH);
}
/* Send a command with parameters. Don't wait for the response,
* which consists of data blocks read from the CD. */
static inline int exec_read_cmd(int cmd, struct cdrom_msf *params)
{
int ack = send_cmd(cmd);
if (ack < 0)
return ack;
return send_params(params);
}
/* Send a seek command with parameters and wait for response */
static inline int exec_seek_cmd(int cmd, struct cdrom_msf *params)
{
int ack = send_cmd(cmd);
if (ack < 0)
return ack;
ack = send_seek_params(params);
if (ack < 0)
return ack;
return 0;
}
/* Send a command with parameters and wait for response */
static inline int exec_long_cmd(int cmd, struct cdrom_msf *params)
{
int ack = exec_read_cmd(cmd, params);
if (ack < 0)
return ack;
return get_exec_status(0);
}
/* Address conversion routines */
/* Binary to BCD (2 digits) */
static inline void single_bin2bcd(u_char *p)
{
DEBUG((DEBUG_CONV, "bin2bcd %02d", *p));
*p = (*p % 10) | ((*p / 10) << 4);
}
/* Convert entire msf struct */
static void bin2bcd(struct cdrom_msf *msf)
{
single_bin2bcd(&msf->cdmsf_min0);
single_bin2bcd(&msf->cdmsf_sec0);
single_bin2bcd(&msf->cdmsf_frame0);
single_bin2bcd(&msf->cdmsf_min1);
single_bin2bcd(&msf->cdmsf_sec1);
single_bin2bcd(&msf->cdmsf_frame1);
}
/* Linear block address to minute, second, frame form */
#define CD_FPM (CD_SECS * CD_FRAMES) /* frames per minute */
static void lba2msf(int lba, struct cdrom_msf *msf)
{
DEBUG((DEBUG_CONV, "lba2msf %d", lba));
lba += CD_MSF_OFFSET;
msf->cdmsf_min0 = lba / CD_FPM; lba %= CD_FPM;
msf->cdmsf_sec0 = lba / CD_FRAMES;
msf->cdmsf_frame0 = lba % CD_FRAMES;
msf->cdmsf_min1 = 0;
msf->cdmsf_sec1 = 0;
msf->cdmsf_frame1 = 0;
bin2bcd(msf);
}
/* Two BCD digits to binary */
static inline u_char bcd2bin(u_char bcd)
{
DEBUG((DEBUG_CONV, "bcd2bin %x%02x", bcd));
return (bcd >> 4) * 10 + (bcd & 0x0f);
}
static void msf2lba(union cdrom_addr *addr)
{
addr->lba = addr->msf.minute * CD_FPM
+ addr->msf.second * CD_FRAMES
+ addr->msf.frame - CD_MSF_OFFSET;
}
/* Minute, second, frame address BCD to binary or to linear address,
depending on MODE */
static void msf_bcd2bin(union cdrom_addr *addr)
{
addr->msf.minute = bcd2bin(addr->msf.minute);
addr->msf.second = bcd2bin(addr->msf.second);
addr->msf.frame = bcd2bin(addr->msf.frame);
}
/* High level drive commands */
static int audio_status = CDROM_AUDIO_NO_STATUS;
static char toc_uptodate = 0;
static char disk_changed = 1;
/* Get drive status, flagging completion of audio play and disk changes. */
static int drive_status(void)
{
int status;
status = exec_cmd(COMIOCTLISTAT);
DEBUG((DEBUG_DRIVE_IF, "IOCTLISTAT: %03x", status));
if (status < 0)
return status;
if (status == 0xff) /* No status available */
return -ERR_IF_NOSTAT;
if (((status & ST_MODE_BITS) != ST_M_AUDIO) &&
(audio_status == CDROM_AUDIO_PLAY)) {
audio_status = CDROM_AUDIO_COMPLETED;
}
if (status & ST_DSK_CHG) {
toc_uptodate = 0;
disk_changed = 1;
audio_status = CDROM_AUDIO_NO_STATUS;
}
return status;
}
/* Read the current Q-channel info. Also used for reading the
table of contents. qp->cdsc_format must be set on entry to
indicate the desired address format */
static int get_q_channel(struct cdrom_subchnl *qp)
{
int status, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
status = drive_status();
if (status < 0)
return status;
qp->cdsc_audiostatus = audio_status;
status = exec_cmd(COMSUBQ);
if (status < 0)
return status;
d1 = get_data(0);
if (d1 < 0)
return d1;
qp->cdsc_adr = d1;
qp->cdsc_ctrl = d1 >> 4;
d2 = get_data(0);
if (d2 < 0)
return d2;
qp->cdsc_trk = bcd2bin(d2);
d3 = get_data(0);
if (d3 < 0)
return d3;
qp->cdsc_ind = bcd2bin(d3);
d4 = get_data(0);
if (d4 < 0)
return d4;
qp->cdsc_reladdr.msf.minute = d4;
d5 = get_data(0);
if (d5 < 0)
return d5;
qp->cdsc_reladdr.msf.second = d5;
d6 = get_data(0);
if (d6 < 0)
return d6;
qp->cdsc_reladdr.msf.frame = d6;
d7 = get_data(0);
if (d7 < 0)
return d7;
/* byte not used */
d8 = get_data(0);
if (d8 < 0)
return d8;
qp->cdsc_absaddr.msf.minute = d8;
d9 = get_data(0);
if (d9 < 0)
return d9;
qp->cdsc_absaddr.msf.second = d9;
d10 = get_data(0);
if (d10 < 0)
return d10;
qp->cdsc_absaddr.msf.frame = d10;
DEBUG((DEBUG_TOC, "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
d1, d2, d3, d4, d5, d6, d7, d8, d9, d10));
msf_bcd2bin(&qp->cdsc_absaddr);
msf_bcd2bin(&qp->cdsc_reladdr);
if (qp->cdsc_format == CDROM_LBA) {
msf2lba(&qp->cdsc_absaddr);
msf2lba(&qp->cdsc_reladdr);
}
return 0;
}
/* Table of contents handling */
/* Errors in table of contents */
#define ERR_TOC_MISSINGINFO 0x120
#define ERR_TOC_MISSINGENTRY 0x121
struct cdrom_disk_info {
unsigned char first;
unsigned char last;
struct cdrom_msf0 disk_length;
struct cdrom_msf0 first_track;
/* Multisession info: */
unsigned char next;
struct cdrom_msf0 next_session;
struct cdrom_msf0 last_session;
unsigned char multi;
unsigned char xa;
unsigned char audio;
};
static struct cdrom_disk_info disk_info;
#define MAX_TRACKS 111
static struct cdrom_subchnl toc[MAX_TRACKS];
#define QINFO_FIRSTTRACK 100 /* bcd2bin(0xa0) */
#define QINFO_LASTTRACK 101 /* bcd2bin(0xa1) */
#define QINFO_DISKLENGTH 102 /* bcd2bin(0xa2) */
#define QINFO_NEXTSESSION 110 /* bcd2bin(0xb0) */
#define I_FIRSTTRACK 0x01
#define I_LASTTRACK 0x02
#define I_DISKLENGTH 0x04
#define I_NEXTSESSION 0x08
#define I_ALL (I_FIRSTTRACK | I_LASTTRACK | I_DISKLENGTH)
#if DEBUG_TOC
static void toc_debug_info(int i)
{
printk(KERN_DEBUG "#%3d ctl %1x, adr %1x, track %2d index %3d"
" %2d:%02d.%02d %2d:%02d.%02d\n",
i, toc[i].cdsc_ctrl, toc[i].cdsc_adr,
toc[i].cdsc_trk, toc[i].cdsc_ind,
toc[i].cdsc_reladdr.msf.minute,
toc[i].cdsc_reladdr.msf.second,
toc[i].cdsc_reladdr.msf.frame,
toc[i].cdsc_absaddr.msf.minute,
toc[i].cdsc_absaddr.msf.second,
toc[i].cdsc_absaddr.msf.frame);
}
#endif
static int read_toc(void)
{
int status, limit, count;
unsigned char got_info = 0;
struct cdrom_subchnl q_info;
#if DEBUG_TOC
int i;
#endif
DEBUG((DEBUG_TOC, "starting read_toc"));
count = 0;
for (limit = 60; limit > 0; limit--) {
int index;
q_info.cdsc_format = CDROM_MSF;
status = get_q_channel(&q_info);
if (status < 0)
return status;
index = q_info.cdsc_ind;
if (index > 0 && index < MAX_TRACKS
&& q_info.cdsc_trk == 0 && toc[index].cdsc_ind == 0) {
toc[index] = q_info;
DEBUG((DEBUG_TOC, "got %d", index));
if (index < 100)
count++;
switch (q_info.cdsc_ind) {
case QINFO_FIRSTTRACK:
got_info |= I_FIRSTTRACK;
break;
case QINFO_LASTTRACK:
got_info |= I_LASTTRACK;
break;
case QINFO_DISKLENGTH:
got_info |= I_DISKLENGTH;
break;
case QINFO_NEXTSESSION:
got_info |= I_NEXTSESSION;
break;
}
}
if ((got_info & I_ALL) == I_ALL
&& toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute + count
>= toc[QINFO_LASTTRACK].cdsc_absaddr.msf.minute + 1)
break;
}
/* Construct disk_info from TOC */
if (disk_info.first == 0) {
disk_info.first = toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute;
disk_info.first_track.minute =
toc[disk_info.first].cdsc_absaddr.msf.minute;
disk_info.first_track.second =
toc[disk_info.first].cdsc_absaddr.msf.second;
disk_info.first_track.frame =
toc[disk_info.first].cdsc_absaddr.msf.frame;
}
disk_info.last = toc[QINFO_LASTTRACK].cdsc_absaddr.msf.minute;
disk_info.disk_length.minute =
toc[QINFO_DISKLENGTH].cdsc_absaddr.msf.minute;
disk_info.disk_length.second =
toc[QINFO_DISKLENGTH].cdsc_absaddr.msf.second-2;
disk_info.disk_length.frame =
toc[QINFO_DISKLENGTH].cdsc_absaddr.msf.frame;
disk_info.next_session.minute =
toc[QINFO_NEXTSESSION].cdsc_reladdr.msf.minute;
disk_info.next_session.second =
toc[QINFO_NEXTSESSION].cdsc_reladdr.msf.second;
disk_info.next_session.frame =
toc[QINFO_NEXTSESSION].cdsc_reladdr.msf.frame;
disk_info.next = toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute;
disk_info.last_session.minute =
toc[disk_info.next].cdsc_absaddr.msf.minute;
disk_info.last_session.second =
toc[disk_info.next].cdsc_absaddr.msf.second;
disk_info.last_session.frame =
toc[disk_info.next].cdsc_absaddr.msf.frame;
toc[disk_info.last + 1].cdsc_absaddr.msf.minute =
disk_info.disk_length.minute;
toc[disk_info.last + 1].cdsc_absaddr.msf.second =
disk_info.disk_length.second;
toc[disk_info.last + 1].cdsc_absaddr.msf.frame =
disk_info.disk_length.frame;
#if DEBUG_TOC
for (i = 1; i <= disk_info.last + 1; i++)
toc_debug_info(i);
toc_debug_info(QINFO_FIRSTTRACK);
toc_debug_info(QINFO_LASTTRACK);
toc_debug_info(QINFO_DISKLENGTH);
toc_debug_info(QINFO_NEXTSESSION);
#endif
DEBUG((DEBUG_TOC, "exiting read_toc, got_info %x, count %d",
got_info, count));
if ((got_info & I_ALL) != I_ALL
|| toc[QINFO_FIRSTTRACK].cdsc_absaddr.msf.minute + count
< toc[QINFO_LASTTRACK].cdsc_absaddr.msf.minute + 1)
return -ERR_TOC_MISSINGINFO;
return 0;
}
#ifdef MULTISESSION
static int get_multi_disk_info(void)
{
int sessions, status;
struct cdrom_msf multi_index;
for (sessions = 2; sessions < 10 /* %%for now */; sessions++) {
int count;
for (count = 100; count < MAX_TRACKS; count++)
toc[count].cdsc_ind = 0;
multi_index.cdmsf_min0 = disk_info.next_session.minute;
multi_index.cdmsf_sec0 = disk_info.next_session.second;
multi_index.cdmsf_frame0 = disk_info.next_session.frame;
if (multi_index.cdmsf_sec0 >= 20)
multi_index.cdmsf_sec0 -= 20;
else {
multi_index.cdmsf_sec0 += 40;
multi_index.cdmsf_min0--;
}
DEBUG((DEBUG_MULTIS, "Try %d: %2d:%02d.%02d", sessions,
multi_index.cdmsf_min0,
multi_index.cdmsf_sec0,
multi_index.cdmsf_frame0));
bin2bcd(&multi_index);
multi_index.cdmsf_min1 = 0;
multi_index.cdmsf_sec1 = 0;
multi_index.cdmsf_frame1 = 1;
status = exec_read_cmd(COMREAD, &multi_index);
if (status < 0) {
DEBUG((DEBUG_TOC, "exec_read_cmd COMREAD: %02x",
-status));
break;
}
status = sleep_flag_low(FL_DTEN, MULTI_SEEK_TIMEOUT) ?
0 : -ERR_TOC_MISSINGINFO;
flush_data();
if (status < 0) {
DEBUG((DEBUG_TOC, "sleep_flag_low: %02x", -status));
break;
}
status = read_toc();
if (status < 0) {
DEBUG((DEBUG_TOC, "read_toc: %02x", -status));
break;
}
disk_info.multi = 1;
}
exec_cmd(COMSTOP);
if (status < 0)
return -EIO;
return 0;
}
#endif /* MULTISESSION */
static int update_toc(void)
{
int status, count;
if (toc_uptodate)
return 0;
DEBUG((DEBUG_TOC, "starting update_toc"));
disk_info.first = 0;
for (count = 0; count < MAX_TRACKS; count++)
toc[count].cdsc_ind = 0;
status = exec_cmd(COMLEADIN);
if (status < 0)
return -EIO;
status = read_toc();
if (status < 0) {
DEBUG((DEBUG_TOC, "read_toc: %02x", -status));
return -EIO;
}
/* Audio disk detection. Look at first track. */
disk_info.audio =
(toc[disk_info.first].cdsc_ctrl & CDROM_DATA_TRACK) ? 0 : 1;
/* XA detection */
disk_info.xa = drive_status() & ST_MODE2TRACK;
/* Multisession detection: if we want this, define MULTISESSION */
disk_info.multi = 0;
#ifdef MULTISESSION
if (disk_info.xa)
get_multi_disk_info(); /* Here disk_info.multi is set */
#endif /* MULTISESSION */
if (disk_info.multi)
printk(KERN_WARNING "optcd: Multisession support experimental, "
"see Documentation/cdrom/optcd\n");
DEBUG((DEBUG_TOC, "exiting update_toc"));
toc_uptodate = 1;
return 0;
}
/* Request handling */
static int current_valid(void)
{
return CURRENT &&
CURRENT->cmd == READ &&
CURRENT->sector != -1;
}
/* Buffers for block size conversion. */
#define NOBUF -1
static char buf[CD_FRAMESIZE * N_BUFS];
static volatile int buf_bn[N_BUFS], next_bn;
static volatile int buf_in = 0, buf_out = NOBUF;
static inline void opt_invalidate_buffers(void)
{
int i;
DEBUG((DEBUG_BUFFERS, "executing opt_invalidate_buffers"));
for (i = 0; i < N_BUFS; i++)
buf_bn[i] = NOBUF;
buf_out = NOBUF;
}