This source file includes following definitions.
- mcdx_ioctl
- do_mcdx_request
- mcdx_open
- mcdx_close
- check_mcdx_media_change
- mcdx_setup
- mcdx_delay
- mcdx_intr
- mcdx_talk
- init_module
- cleanup_module
- trace
- warn
- mcdx_init
- mcdx_transfer
- port
- irq
- bcd2uint
- uint2bcd
- log2msf
- msf2log
- mcdx_readtoc
- mcdx_playmsf
- mcdx_playtrk
- mcdx_closedoor
- mcdx_stop
- mcdx_hold
- mcdx_eject
- mcdx_requestsubqcode
- mcdx_requestmultidiskinfo
- mcdx_requesttocdata
- mcdx_setdrivemode
- mcdx_setdatamode
- mcdx_config
- mcdx_requestversion
- mcdx_reset
- mcdx_lockdoor
- mcdx_getstatus
- mcdx_getval
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 #if RCS
36 static const char *mcdx_c_version
37 = "mcdx.c,v 1.7 1995/08/27 01:46:41 heiko Exp";
38 #endif
39
40 #include <linux/module.h>
41
42 #include <linux/errno.h>
43 #include <linux/signal.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/fs.h>
47 #include <linux/kernel.h>
48 #include <linux/cdrom.h>
49 #include <linux/ioport.h>
50 #include <linux/mm.h>
51 #include <linux/malloc.h>
52 #include <asm/system.h>
53 #include <asm/io.h>
54 #include <asm/segment.h>
55
56
57 #include <linux/major.h>
58
59 #ifndef MITSUMI_X_CDROM_MAJOR
60 #define MITSUMI_X_CDROM_MAJOR 20
61 #define DEVICE_NAME "Mitsumi CD-ROM"
62
63 #define DEVICE_REQUEST do_mcdx_request
64 #define DEVICE_NR(device) (MINOR(device))
65 #define DEVICE_ON(device)
66 #define DEVICE_OFF(device)
67 #endif
68
69 #define MAJOR_NR MITSUMI_X_CDROM_MAJOR
70 #include <linux/blk.h>
71 #define mcdx_drive_map mcdx
72 #include <linux/mcdx.h>
73
74
75
76 const int REQUEST_SIZE = 200;
77 const int DIRECT_SIZE = 200;
78
79 enum drivemodes { TOC, DATA, RAW, COOKED };
80 enum datamodes { MODE0, MODE1, MODE2 };
81 enum resetmodes { SOFT, HARD };
82
83 const int SINGLE = 0x01;
84 const int DOUBLE = 0x02;
85 const int DOOR = 0x04;
86 const int MULTI = 0x08;
87 const int READY = 0x70;
88
89 const unsigned char READSSPEED = 0xc0;
90 const unsigned char READDSPEED = 0xc1;
91
92
93
94 struct s_msf {
95 unsigned char minute;
96 unsigned char second;
97 unsigned char frame;
98 };
99
100 struct s_subqcode {
101 unsigned char control;
102 unsigned char tno;
103 unsigned char index;
104 struct s_msf tt;
105 struct s_msf dt;
106 };
107
108 struct s_diskinfo {
109 unsigned int n_first;
110 unsigned int n_last;
111 struct s_msf msf_leadout;
112 struct s_msf msf_first;
113 };
114
115 struct s_multi {
116 unsigned char multi;
117 struct s_msf msf_last;
118 };
119
120 struct s_version {
121 unsigned char code;
122 unsigned char ver;
123 };
124
125
126
127 struct s_drive_stuff {
128
129 struct wait_queue *busyq;
130 struct wait_queue *lockq;
131 struct wait_queue *sleepq;
132
133
134 volatile int introk;
135 volatile int busy;
136 volatile int lock;
137
138
139 struct s_diskinfo di;
140 struct s_multi multi;
141 struct s_subqcode* toc;
142 struct s_subqcode start;
143 struct s_subqcode stop;
144 int xa;
145 int audio;
146 int audiostatus;
147
148
149 volatile int valid;
150 volatile int pending;
151 volatile int off_direct;
152 volatile int off_requested;
153
154
155 void* wreg_data;
156 void* wreg_reset;
157 void* wreg_hcon;
158 void* wreg_chn;
159 void* rreg_data;
160 void* rreg_status;
161
162 int irq;
163 int minor;
164 int present;
165 char readcmd;
166 char playcmd;
167 unsigned long changed;
168 unsigned long xxx;
169 int users;
170 int lastsector;
171 int errno;
172
173 };
174
175
176
177
178
179
180
181
182
183 int mcdx_init(void);
184 void do_mcdx_request(void);
185
186 int check_mcdx_media_change(kdev_t);
187
188
189 void mcdx_setup(char *, int *);
190
191
192
193
194
195
196 static void mcdx_intr(int, struct pt_regs*);
197
198
199 static int mcdx_open(struct inode*, struct file*);
200 static void mcdx_close(struct inode*, struct file*);
201 static int mcdx_ioctl(struct inode*, struct file*, unsigned int, unsigned long);
202
203
204 static void log2msf(unsigned int, struct s_msf*);
205 static unsigned int msf2log(const struct s_msf*);
206 static unsigned int uint2bcd(unsigned int);
207 static unsigned int bcd2uint(unsigned char);
208 #if MCDX_DEBUG
209 static void TRACE((int level, const char* fmt, ...));
210 #endif
211 static void warn(const char* fmt, ...);
212 static char *port(int*);
213 static int irq(int*);
214 static void mcdx_delay(struct s_drive_stuff*, long jifs);
215 static int mcdx_transfer(struct s_drive_stuff*, char* buf, int sector, int nr_sectors);
216
217 static int mcdx_config(struct s_drive_stuff*, int);
218 static int mcdx_closedoor(struct s_drive_stuff*, int);
219 static int mcdx_requestversion(struct s_drive_stuff*, struct s_version*, int);
220 static int mcdx_lockdoor(struct s_drive_stuff*, int, int);
221 static int mcdx_stop(struct s_drive_stuff*, int);
222 static int mcdx_hold(struct s_drive_stuff*, int);
223 static int mcdx_reset(struct s_drive_stuff*, enum resetmodes, int);
224 static int mcdx_eject(struct s_drive_stuff*, int);
225 static int mcdx_setdrivemode(struct s_drive_stuff*, enum drivemodes, int);
226 static int mcdx_setdatamode(struct s_drive_stuff*, enum datamodes, int);
227 static int mcdx_requestsubqcode(struct s_drive_stuff*, struct s_subqcode*, int);
228 static int mcdx_requestmultidiskinfo(struct s_drive_stuff*, struct s_multi*, int);
229 static int mcdx_requesttocdata(struct s_drive_stuff*, struct s_diskinfo*, int);
230 static int mcdx_getstatus(struct s_drive_stuff*, int);
231 static int mcdx_getval(struct s_drive_stuff*, int to, int delay, char*);
232
233 static int mcdx_talk(struct s_drive_stuff*,
234 const unsigned char* cmd, size_t, void *buffer,
235 size_t size, unsigned int timeout, int);
236 static int mcdx_readtoc(struct s_drive_stuff*);
237 static int mcdx_playtrk(struct s_drive_stuff*, const struct cdrom_ti*);
238 static int mcdx_playmsf(struct s_drive_stuff*, const struct cdrom_msf*);
239
240
241
242 static int dummy0;
243 static int mcdx_drive_map[][2] = MCDX_DRIVEMAP;
244 static struct s_drive_stuff* mcdx_stuffp[MCDX_NDRIVES];
245 static struct s_drive_stuff* mcdx_irq_map[16] =
246 {0, 0, 0, 0, 0, 0, 0, 0,
247 0, 0, 0, 0, 0, 0, 0, 0};
248
249 static struct file_operations mcdx_fops = {
250 NULL,
251 block_read,
252 block_write,
253 NULL,
254 NULL,
255 mcdx_ioctl,
256 NULL,
257 mcdx_open,
258 mcdx_close,
259 NULL,
260 NULL,
261 check_mcdx_media_change,
262 NULL
263 };
264
265
266
267 static int
268 mcdx_ioctl(
269 struct inode* ip, struct file* fp,
270 unsigned int cmd, unsigned long arg)
271 {
272 struct s_drive_stuff *stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
273
274 if (!stuffp->present) return -ENXIO;
275 if (!ip) return -EINVAL;
276
277 switch (cmd) {
278 case CDROMSTART: {
279 TRACE((IOCTL, "ioctl() START\n"));
280 return 0;
281 }
282
283 case CDROMSTOP: {
284 TRACE((IOCTL, "ioctl() STOP\n"));
285 stuffp->audiostatus = CDROM_AUDIO_INVALID;
286 if (-1 == mcdx_stop(stuffp, 1))
287 return -EIO;
288 return 0;
289 }
290
291 case CDROMPLAYTRKIND: {
292 int ans;
293 struct cdrom_ti ti;
294
295 TRACE((IOCTL, "ioctl() PLAYTRKIND\n"));
296 if ((ans = verify_area(VERIFY_READ, (void*) arg, sizeof(ti))))
297 return ans;
298 memcpy_fromfs(&ti, (void*) arg, sizeof(ti));
299 if ((ti.cdti_trk0 < stuffp->di.n_first)
300 || (ti.cdti_trk0 > stuffp->di.n_last)
301 || (ti.cdti_trk1 < stuffp->di.n_first))
302 return -EINVAL;
303 if (ti.cdti_trk1 > stuffp->di.n_last) ti.cdti_trk1 = stuffp->di.n_last;
304 TRACE((IOCTL, "ioctl() track %d to %d\n", ti.cdti_trk0, ti.cdti_trk1));
305
306 return mcdx_playtrk(stuffp, &ti);
307 }
308
309 case CDROMPLAYMSF: {
310 int ans;
311 struct cdrom_msf msf;
312
313 TRACE((IOCTL, "ioctl() PLAYMSF\n"));
314
315 if ((stuffp->audiostatus == CDROM_AUDIO_PLAY)
316 && (-1 == mcdx_hold(stuffp, 1))) return -EIO;
317
318 if ((ans = verify_area(
319 VERIFY_READ, (void*) arg, sizeof(struct cdrom_msf))))
320 return ans;
321
322 memcpy_fromfs(&msf, (void*) arg, sizeof msf);
323
324 msf.cdmsf_min0 = uint2bcd(msf.cdmsf_min0);
325 msf.cdmsf_sec0 = uint2bcd(msf.cdmsf_sec0);
326 msf.cdmsf_frame0 = uint2bcd(msf.cdmsf_frame0);
327
328 msf.cdmsf_min1 = uint2bcd(msf.cdmsf_min1);
329 msf.cdmsf_sec1 = uint2bcd(msf.cdmsf_sec1);
330 msf.cdmsf_frame1 = uint2bcd(msf.cdmsf_frame1);
331
332 return mcdx_playmsf(stuffp, &msf);
333 }
334
335 case CDROMRESUME: {
336 TRACE((IOCTL, "ioctl() RESUME\n"));
337 return mcdx_playtrk(stuffp, NULL);
338 }
339
340 case CDROMREADTOCENTRY: {
341 struct cdrom_tocentry entry;
342 struct s_subqcode *tp = NULL;
343 int ans;
344
345 TRACE((IOCTL, "ioctl() READTOCENTRY\n"));
346
347 if (-1 == mcdx_readtoc(stuffp)) return -1;
348
349 if ((ans = verify_area(VERIFY_READ, (void *) arg, sizeof(entry)))) return ans;
350 memcpy_fromfs(&entry, (void *) arg, sizeof(entry));
351
352 if (entry.cdte_track == CDROM_LEADOUT)
353 tp = &stuffp->toc[stuffp->di.n_last - stuffp->di.n_first + 1];
354 else if (entry.cdte_track > stuffp->di.n_last
355 || entry.cdte_track < stuffp->di.n_first) return -EINVAL;
356 else tp = &stuffp->toc[entry.cdte_track - stuffp->di.n_first];
357
358 if (NULL == tp) WARN(("FATAL.\n"));
359
360 entry.cdte_adr = tp->control;
361 entry.cdte_ctrl = tp->control >> 4;
362
363 if (entry.cdte_format == CDROM_MSF) {
364 entry.cdte_addr.msf.minute = bcd2uint(tp->dt.minute);
365 entry.cdte_addr.msf.second = bcd2uint(tp->dt.second);
366 entry.cdte_addr.msf.frame = bcd2uint(tp->dt.frame);
367 } else if (entry.cdte_format == CDROM_LBA)
368 entry.cdte_addr.lba = msf2log(&tp->dt);
369 else return -EINVAL;
370
371 if ((ans = verify_area(VERIFY_WRITE, (void*) arg, sizeof(entry)))) return ans;
372 memcpy_tofs((void*) arg, &entry, sizeof(entry));
373
374 return 0;
375 }
376
377 case CDROMSUBCHNL: {
378 int ans;
379 struct cdrom_subchnl sub;
380 struct s_subqcode q;
381
382 TRACE((IOCTL, "ioctl() SUBCHNL\n"));
383
384 if ((ans = verify_area(VERIFY_READ,
385 (void*) arg, sizeof(sub)))) return ans;
386
387 memcpy_fromfs(&sub, (void*) arg, sizeof(sub));
388
389 if (-1 == mcdx_requestsubqcode(stuffp, &q, 2)) return -EIO;
390
391 TRACE((IOCTL, "audiostatus: %x\n", stuffp->audiostatus));
392 sub.cdsc_audiostatus = stuffp->audiostatus;
393 sub.cdsc_adr = q.control;
394 sub.cdsc_ctrl = q.control >> 4;
395 sub.cdsc_trk = bcd2uint(q.tno);
396 sub.cdsc_ind = bcd2uint(q.index);
397
398 TRACE((IOCTL, "trk %d, ind %d\n",
399 sub.cdsc_trk, sub.cdsc_ind));
400
401 if (sub.cdsc_format == CDROM_LBA) {
402 sub.cdsc_absaddr.lba = msf2log(&q.dt);
403 sub.cdsc_reladdr.lba = msf2log(&q.tt);
404 TRACE((IOCTL, "lba: abs %d, rel %d\n",
405 sub.cdsc_absaddr.lba,
406 sub.cdsc_reladdr.lba));
407 } else if (sub.cdsc_format == CDROM_MSF) {
408 sub.cdsc_absaddr.msf.minute = bcd2uint(q.dt.minute);
409 sub.cdsc_absaddr.msf.second = bcd2uint(q.dt.second);
410 sub.cdsc_absaddr.msf.frame = bcd2uint(q.dt.frame);
411 sub.cdsc_reladdr.msf.minute = bcd2uint(q.tt.minute);
412 sub.cdsc_reladdr.msf.second = bcd2uint(q.tt.second);
413 sub.cdsc_reladdr.msf.frame = bcd2uint(q.tt.frame);
414 TRACE((IOCTL,
415 "msf: abs %02d:%02d:%02d, rel %02d:%02d:%02d\n",
416 sub.cdsc_absaddr.msf.minute,
417 sub.cdsc_absaddr.msf.second,
418 sub.cdsc_absaddr.msf.frame,
419 sub.cdsc_reladdr.msf.minute,
420 sub.cdsc_reladdr.msf.second,
421 sub.cdsc_reladdr.msf.frame));
422 } else return -EINVAL;
423
424 if ((ans = verify_area(VERIFY_WRITE, (void*) arg, sizeof(sub))))
425 return ans;
426 memcpy_tofs((void*) arg, &sub, sizeof(sub));
427
428 return 0;
429 }
430
431 case CDROMREADTOCHDR: {
432 struct cdrom_tochdr toc;
433 int ans;
434
435 TRACE((IOCTL, "ioctl() READTOCHDR\n"));
436 if ((ans = verify_area(VERIFY_WRITE, (void*) arg, sizeof toc)))
437 return ans;
438 toc.cdth_trk0 = stuffp->di.n_first;
439 toc.cdth_trk1 = stuffp->di.n_last;
440 memcpy_tofs((void*) arg, &toc, sizeof toc);
441 TRACE((IOCTL, "ioctl() track0 = %d, track1 = %d\n",
442 stuffp->di.n_first, stuffp->di.n_last));
443 return 0;
444 }
445
446 case CDROMPAUSE: {
447 TRACE((IOCTL, "ioctl() PAUSE\n"));
448 if (stuffp->audiostatus != CDROM_AUDIO_PLAY) return -EINVAL;
449 if (-1 == mcdx_stop(stuffp, 1)) return -EIO;
450 if (-1 == mcdx_requestsubqcode(stuffp, &stuffp->start, 1))
451 return -EIO;
452
453 stuffp->audiostatus = CDROM_AUDIO_PAUSED;
454 return 0;
455 }
456
457 case CDROMMULTISESSION: {
458 int ans;
459 struct cdrom_multisession ms;
460 TRACE((IOCTL, "ioctl() MULTISESSION\n"));
461 if (0 != (ans = verify_area(VERIFY_READ, (void*) arg,
462 sizeof(struct cdrom_multisession))))
463 return ans;
464
465 memcpy_fromfs(&ms, (void*) arg, sizeof(struct cdrom_multisession));
466 if (ms.addr_format == CDROM_MSF) {
467 ms.addr.msf.minute = bcd2uint(stuffp->multi.msf_last.minute);
468 ms.addr.msf.second = bcd2uint(stuffp->multi.msf_last.second);
469 ms.addr.msf.frame = bcd2uint(stuffp->multi.msf_last.frame);
470 } else if (ms.addr_format == CDROM_LBA)
471 ms.addr.lba = msf2log(&stuffp->multi.msf_last);
472 else
473 return -EINVAL;
474 ms.xa_flag = stuffp->xa;
475
476 if (0 != (ans = verify_area(VERIFY_WRITE, (void*) arg,
477 sizeof(struct cdrom_multisession))))
478 return ans;
479
480 memcpy_tofs((void*) arg, &ms, sizeof(struct cdrom_multisession));
481 if (ms.addr_format == CDROM_MSF)
482 TRACE((IOCTL,
483 "ioctl() (%d, %02x:%02x.%02x [%02x:%02x.%02x])\n",
484 ms.xa_flag,
485 ms.addr.msf.minute,
486 ms.addr.msf.second,
487 ms.addr.msf.frame,
488 stuffp->multi.msf_last.minute,
489 stuffp->multi.msf_last.second,
490 stuffp->multi.msf_last.frame));
491 else
492 {
493 dummy0=0;
494 TRACE((IOCTL,
495 "ioctl() (%d, 0x%08x [%02x:%02x.%02x])\n",
496 ms.xa_flag,
497 ms.addr.lba,
498 stuffp->multi.msf_last.minute,
499 stuffp->multi.msf_last.second,
500 stuffp->multi.msf_last.frame));
501 }
502 return 0;
503 }
504
505 case CDROMEJECT: {
506 TRACE((IOCTL, "ioctl() EJECT\n"));
507 if (stuffp->users > 1) return -EBUSY;
508 if (-1 == mcdx_eject(stuffp, 1)) return -EIO;
509 return 0;
510 }
511
512 case CDROMVOLCTRL: {
513 TRACE((IOCTL, "ioctl() volctrl\n"));
514 return 0;
515 }
516
517 default:
518 WARN(("ioctl(): unknown request 0x%04x\n", cmd));
519 return -EINVAL;
520 }
521 }
522
523 void do_mcdx_request()
524 {
525 int dev;
526 struct s_drive_stuff *stuffp;
527
528 again:
529
530 TRACE((REQUEST, "do_request()\n"));
531
532 if ((CURRENT == NULL) || (CURRENT->rq_status == RQ_INACTIVE)) {
533 TRACE((REQUEST, "do_request() done\n"));
534 return;
535 }
536
537 stuffp = mcdx_stuffp[MINOR(CURRENT->rq_dev)];
538 TRACE((REQUEST, "do_request() stuffp = %p\n", stuffp));
539
540 INIT_REQUEST;
541 dev = MINOR(CURRENT->rq_dev);
542
543 if ((dev < 0) || (dev >= MCDX_NDRIVES) || (!stuffp->present)) {
544 WARN(("do_request(): bad device: %s\n",
545 kdevname(CURRENT->rq_dev)));
546 end_request(0);
547 goto again;
548 }
549
550 if (stuffp->audio) {
551 WARN(("do_request() attempt to read from audio cd\n"));
552 end_request(0);
553 goto again;
554 }
555
556 switch (CURRENT->cmd) {
557 case WRITE:
558 WARN(("do_request(): attempt to write to cd!!\n"));
559 end_request(0);
560 break;
561
562 case READ:
563 stuffp->errno = 0;
564 while (CURRENT->nr_sectors) {
565 int i;
566
567 if (-1 == (i = mcdx_transfer(
568 stuffp,
569 CURRENT->buffer,
570 CURRENT->sector,
571 CURRENT->nr_sectors))) {
572 WARN(("do_request() read error\n"));
573 if (stuffp->errno == MCDX_EOM) {
574 CURRENT->sector += CURRENT->nr_sectors;
575 CURRENT->nr_sectors = 0;
576 }
577 end_request(0);
578 goto again;
579 }
580 CURRENT->sector += i;
581 CURRENT->nr_sectors -= i;
582 CURRENT->buffer += (i * 512);
583
584 }
585
586 end_request(1);
587 break;
588
589 default:
590 panic(MCDX "do_request: unknown command.\n");
591 break;
592 }
593
594 goto again;
595 }
596
597 static int
598 mcdx_open(struct inode *ip, struct file *fp)
599 {
600 struct s_drive_stuff *stuffp;
601
602 TRACE((OPENCLOSE, "open()\n"));
603 stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
604 if (!stuffp->present) return -ENXIO;
605
606 if (-1 == mcdx_getstatus(stuffp, 1)) return -EIO;
607
608
609
610 if (inb((unsigned int) stuffp->rreg_status) & MCDX_RBIT_DOOR)
611 mcdx_closedoor(stuffp, 1);
612
613
614 if (stuffp->xxx < stuffp->changed) {
615
616 TRACE((OPENCLOSE, "open() media changed\n"));
617
618
619
620
621
622
623
624
625
626
627 stuffp->audiostatus = CDROM_AUDIO_INVALID;
628
629
630 {
631 TRACE((OPENCLOSE, "open() Request multisession info\n"));
632 if (-1 == mcdx_requestmultidiskinfo(stuffp, &stuffp->multi, 6))
633 return -EIO;
634
635 if (stuffp->multi.multi > 2)
636 WARN(("open() unknown multisession value (%d)\n", stuffp->multi.multi));
637
638
639 if (!stuffp->multi.multi)
640 stuffp->multi.msf_last.second = 2;
641
642 TRACE((OPENCLOSE, "open() MS: %d, last @ %02x:%02x.%02x\n",
643 stuffp->multi.multi,
644 stuffp->multi.msf_last.minute,
645 stuffp->multi.msf_last.second,
646 stuffp->multi.msf_last.frame));
647 }
648
649
650 if (-1 == mcdx_requesttocdata(stuffp, &stuffp->di, 1)) return -EIO;
651
652 stuffp->lastsector = (CD_FRAMESIZE / 512)
653 * msf2log(&stuffp->di.msf_leadout) - 1;
654
655 TRACE((OPENCLOSE, "open() start %d (%02x:%02x.%02x) %d\n",
656 stuffp->di.n_first,
657 stuffp->di.msf_first.minute,
658 stuffp->di.msf_first.second,
659 stuffp->di.msf_first.frame,
660 msf2log(&stuffp->di.msf_first)));
661 TRACE((OPENCLOSE, "open() last %d (%02x:%02x.%02x) %d\n",
662 stuffp->di.n_last,
663 stuffp->di.msf_leadout.minute,
664 stuffp->di.msf_leadout.second,
665 stuffp->di.msf_leadout.frame,
666 msf2log(&stuffp->di.msf_leadout)));
667
668 if (stuffp->toc) {
669 TRACE((MALLOC, "open() free toc @ %p\n", stuffp->toc));
670 kfree(stuffp->toc);
671 }
672 stuffp->toc = NULL;
673
674 TRACE((OPENCLOSE, "open() init irq generation\n"));
675 if (-1 == mcdx_config(stuffp, 1)) return -EIO;
676
677
678 {
679 char buf[512];
680 int ans;
681 int tries;
682
683 stuffp->xa = 0;
684 stuffp->audio = 0;
685
686 for (tries = 6; tries; tries--) {
687 TRACE((OPENCLOSE, "open() try as %s\n",
688 stuffp->xa ? "XA" : "normal"));
689
690
691 if (-1 == (ans = mcdx_setdatamode(stuffp,
692 stuffp->xa ? MODE2 : MODE1, 1)))
693 return -EIO;
694
695 if ((stuffp->audio = e_audio(ans))) break;
696
697 while (0 == (ans = mcdx_transfer(stuffp, buf, 0, 1)))
698 ;
699
700 if (ans == 1) break;
701 stuffp->xa = !stuffp->xa;
702 }
703 if (!tries) return -EIO;
704 }
705
706
707 if (-1 == mcdx_setdrivemode(stuffp,
708 stuffp->xa ? RAW : COOKED, 1))
709 return -EIO;
710
711 if (stuffp->audio) {
712 INFO(("open() audio disk found\n"));
713 } else {
714 INFO(("open() %s%s disk found\n",
715 stuffp->xa ? "XA / " : "",
716 stuffp->multi.multi ? "Multi Session" : "Single Session"));
717 }
718
719 stuffp->xxx = jiffies;
720 }
721
722
723 if (0 == stuffp->users && (-1 == mcdx_lockdoor(stuffp, 1, 1)))
724 return -EIO;
725
726 stuffp->users++;
727 MOD_INC_USE_COUNT;
728 return 0;
729 }
730
731 static void
732 mcdx_close(struct inode *ip, struct file *fp)
733 {
734 struct s_drive_stuff *stuffp;
735
736 TRACE((OPENCLOSE, "close()\n"));
737
738 stuffp = mcdx_stuffp[MINOR(ip->i_rdev)];
739
740 if (0 == --stuffp->users) {
741 sync_dev(ip->i_rdev);
742
743
744 invalidate_buffers(ip->i_rdev);
745
746 if (-1 == mcdx_lockdoor(stuffp, 0, 1))
747 printk(MCDX ": Cannot unlock the door\n");
748 }
749 MOD_DEC_USE_COUNT;
750
751 return;
752 }
753
754 int check_mcdx_media_change(kdev_t full_dev)
755
756
757
758
759
760 {
761 INFO(("check_mcdx_media_change called for device %s\n",
762 kdevname(full_dev)));
763 return 0;
764 }
765
766 void mcdx_setup(char *str, int *pi)
767 {
768 #if MCDX_DEBUG
769 printk(MCDX ":: setup(%s, %d) called\n",
770 str, pi[0]);
771 #endif
772 }
773
774
775
776 static void mcdx_delay(struct s_drive_stuff *stuff, long jifs)
777
778
779
780
781 {
782 unsigned long tout = jiffies + jifs;
783
784 TRACE((INIT, "mcdx_delay %d\n", jifs));
785 if (jifs < 0) return;
786
787 #if 1
788 while (jiffies < tout) {
789 current->timeout = jiffies;
790 schedule();
791 }
792 #else
793 if (current->pid == 0) {
794 while (jiffies < tout) {
795 current->timeout = jiffies;
796 schedule();
797 }
798 } else {
799 current->timeout = tout;
800 current->state = TASK_INTERRUPTIBLE;
801 while (current->timeout) {
802 interruptible_sleep_on(&stuff->sleepq);
803 }
804 }
805 #endif
806 }
807
808 static void
809 mcdx_intr(int irq, struct pt_regs* regs)
810 {
811 struct s_drive_stuff *stuffp;
812 unsigned char x;
813
814 stuffp = mcdx_irq_map[irq];
815
816 if (!stuffp->busy) {
817 INFO(("intr() unexpected interrupt @ irq %d\n", irq));
818 return;
819 }
820
821
822 if (0 == (stuffp->introk =
823 (~(x = inb((unsigned int) stuffp->rreg_status)) & MCDX_RBIT_DTEN)))
824 TRACE((IRQ, "intr() irq %d failed, status %02x %02x\n",
825 irq, x, inb((unsigned int) stuffp->rreg_data)));
826 else
827 {
828 dummy0=0;
829 TRACE((IRQ, "irq() irq %d ok, status %02x\n", irq, x));
830 }
831 stuffp->busy = 0;
832 wake_up_interruptible(&stuffp->busyq);
833 }
834
835
836 static int
837 mcdx_talk (
838 struct s_drive_stuff *stuffp,
839 const unsigned char *cmd, size_t cmdlen,
840 void *buffer, size_t size,
841 unsigned int timeout, int tries)
842
843
844
845 {
846 char c;
847 int st;
848
849 if (!buffer || size == 0) buffer = &c, size = 1;
850
851 while (stuffp->lock)
852 interruptible_sleep_on(&stuffp->lockq);
853
854 if (current->signal && ~current->blocked) {
855 WARN(("talk() got signal %d\n", current->signal));
856 return -1;
857 }
858
859 stuffp->lock = 1;
860 stuffp->valid = 0;
861
862 #if MCDX_DEBUG & TALK
863 {
864 unsigned char i;
865 TRACE((TALK, "talk() %d / %d tries, res.size %d, command 0x%02x",
866 tries, timeout, size, (unsigned char) cmd[0]));
867 for (i = 1; i < cmdlen; i++) printk(" 0x%02x", cmd[i]);
868 printk("\n");
869 }
870 #endif
871
872
873
874 for (st = -1; st == -1 && tries; tries--) {
875
876 size_t sz = size;
877 char* bp = buffer;
878
879 outsb((unsigned int) stuffp->wreg_data, cmd, cmdlen);
880 TRACE((TALK, "talk() command sent\n"));
881
882
883 if (-1 == mcdx_getval(stuffp, timeout, 0, bp)) {
884 WARN(("talk() %02x timed out (status), %d tr%s left\n",
885 cmd[0], tries - 1, tries == 2 ? "y" : "ies"));
886 continue;
887 }
888 st = *bp++;
889 sz--;
890
891 TRACE((TALK, "talk() got status 0x%02x\n", st));
892
893
894 if (e_cmderr(st)) {
895 WARN(("command error %02x (%d)\n", cmd[0], cmdlen));
896 st = -1;
897 continue;
898 }
899
900
901 if (stuffp->audiostatus == CDROM_AUDIO_INVALID)
902 stuffp->audiostatus =
903 e_audiobusy(st) ? CDROM_AUDIO_PLAY : CDROM_AUDIO_NO_STATUS;
904
905
906 if (e_changed(st)) {
907 INFO(("talk() media changed\n"));
908 stuffp->changed = jiffies;
909 }
910
911
912 while (sz--) {
913 if (-1 == mcdx_getval(stuffp, timeout, -1, bp++)) {
914 WARN(("talk() %02x timed out (data), %d tr%s left\n",
915 cmd[0], tries - 1, tries == 2 ? "y" : "ies"));
916 st = -1; break;
917 }
918 TRACE((TALK, "talk() got 0x%02x\n", *(bp - 1)));
919 }
920 }
921
922 #if QUIET == 0
923 if (!tries && st == -1) INFO(("talk() giving up\n"));
924 #endif
925
926 stuffp->lock = 0;
927 wake_up_interruptible(&stuffp->lockq);
928
929 TRACE((TALK, "talk() done with 0x%02x\n", st));
930 return st;
931 }
932
933
934 #ifdef MODULE
935
936 int init_module(void)
937 {
938 int i;
939 int drives = 0;
940
941 mcdx_init();
942 for (i = 0; i < MCDX_NDRIVES; i++) {
943 if (mcdx_stuffp[i]) {
944 TRACE((INIT, "init_module() drive %d stuff @ %p\n",
945 i, mcdx_stuffp[i]));
946 drives++;
947 }
948 }
949
950 if (!drives)
951 return -EIO;
952
953 return 0;
954 }
955
956 void cleanup_module(void)
957 {
958 int i;
959
960 WARN(("cleanup_module called\n"));
961
962 for (i = 0; i < MCDX_NDRIVES; i++) {
963 struct s_drive_stuff *stuffp;
964 stuffp = mcdx_stuffp[i];
965 if (!stuffp) continue;
966 release_region((unsigned long) stuffp->wreg_data, MCDX_IO_SIZE);
967 free_irq(stuffp->irq);
968 if (stuffp->toc) {
969 TRACE((MALLOC, "cleanup_module() free toc @ %p\n", stuffp->toc));
970 kfree(stuffp->toc);
971 }
972 TRACE((MALLOC, "cleanup_module() free stuffp @ %p\n", stuffp));
973 mcdx_stuffp[i] = NULL;
974 kfree(stuffp);
975 }
976
977 if (unregister_blkdev(MAJOR_NR, DEVICE_NAME) != 0)
978 WARN(("cleanup() unregister_blkdev() failed\n"));
979 else INFO(("cleanup() succeeded\n"));
980 }
981
982 #endif MODULE
983
984
985
986 #if MCDX_DEBUG
987 void trace(int level, const char* fmt, ...)
988 {
989 char s[255];
990 va_list args;
991 if (level < 1) return;
992 va_start(args, fmt);
993 if (sizeof(s) < vsprintf(s, fmt, args))
994 printk(MCDX ":: dprintf exeeds limit!!\n");
995 else printk(MCDX ":: %s", s);
996 va_end(args);
997 }
998 #endif
999
1000 void warn(const char* fmt, ...)
1001 {
1002 char s[255];
1003 va_list args;
1004 va_start(args, fmt);
1005 if (sizeof(s) < vsprintf(s, fmt, args))
1006 printk(MCDX ":: dprintf exeeds limit!!\n");
1007 else printk(MCDX ": %s", s);
1008 va_end(args);
1009 }
1010
1011
1012 int mcdx_init(void)
1013 {
1014 int drive;
1015
1016 INFO((": Version 1.0a "
1017 "mcdx.c,v 1.7 1995/08/27 01:46:41 heiko Exp\n"));
1018
1019
1020 for (drive = 0; drive < MCDX_NDRIVES; drive++)
1021 mcdx_stuffp[drive] = NULL;
1022
1023
1024 for (drive = 0; drive < MCDX_NDRIVES; drive++) {
1025 struct s_version version;
1026 struct s_drive_stuff* stuffp;
1027 int size;
1028
1029 size = sizeof(*stuffp);
1030
1031 TRACE((INIT, "init() try drive %d\n", drive));
1032
1033 TRACE((MALLOC, "init() malloc %d bytes\n", size));
1034 if (!(stuffp = kmalloc(size, GFP_KERNEL))) {
1035 WARN(("init() malloc failed\n"));
1036 break;
1037 }
1038
1039 TRACE((INIT, "init() got %d bytes for drive stuff @ %p\n", sizeof(*stuffp), stuffp));
1040
1041
1042 memset(stuffp, 0, sizeof(*stuffp));
1043
1044 stuffp->present = 0;
1045 stuffp->toc = NULL;
1046 stuffp->changed = jiffies;
1047
1048
1049 stuffp->irq = irq(mcdx_drive_map[drive]);
1050 stuffp->wreg_data = stuffp->rreg_data = port(mcdx_drive_map[drive]);
1051 stuffp->wreg_reset = stuffp->rreg_status = stuffp->wreg_data + 1;
1052 stuffp->wreg_hcon = stuffp->wreg_reset + 1;
1053 stuffp->wreg_chn = stuffp->wreg_hcon + 1;
1054
1055
1056 if (0 != check_region((unsigned int) stuffp->wreg_data, MCDX_IO_SIZE)) {
1057 WARN(("%s=0x%3p,%d: "
1058 "Init failed. I/O ports (0x%3p..0x3p) already in use.\n"
1059 MCDX,
1060 stuffp->wreg_data, stuffp->irq,
1061 stuffp->wreg_data,
1062 stuffp->wreg_data + MCDX_IO_SIZE - 1));
1063 TRACE((MALLOC, "init() free stuffp @ %p\n", stuffp));
1064 kfree(stuffp);
1065 TRACE((INIT, "init() continue at next drive\n"));
1066 continue;
1067 }
1068
1069 TRACE((INIT, "init() i/o port is available at 0x%3p\n", stuffp->wreg_data));
1070
1071 TRACE((INIT, "init() hardware reset\n"));
1072 mcdx_reset(stuffp, HARD, 1);
1073
1074 TRACE((INIT, "init() get version\n"));
1075 if (-1 == mcdx_requestversion(stuffp, &version, 4)) {
1076
1077 WARN(("%s=0x%3p,%d: Init failed. Can't get version.\n",
1078 MCDX,
1079 stuffp->wreg_data, stuffp->irq));
1080 TRACE((MALLOC, "init() free stuffp @ %p\n", stuffp));
1081 kfree(stuffp);
1082 TRACE((INIT, "init() continue at next drive\n"));
1083 continue;
1084 }
1085
1086 switch (version.code) {
1087 case 'D':
1088 stuffp->readcmd = READDSPEED;
1089 stuffp->present = DOUBLE | DOOR | MULTI;
1090 break;
1091 case 'F':
1092 stuffp->readcmd = READSSPEED;
1093 stuffp->present = SINGLE | DOOR | MULTI;
1094 break;
1095 case 'M':
1096 stuffp->readcmd = READSSPEED;
1097 stuffp->present = SINGLE;
1098 break;
1099 default:
1100 stuffp->present = 0; break;
1101 }
1102
1103 stuffp->playcmd = READSSPEED;
1104
1105
1106 if (!stuffp->present) {
1107 WARN(("%s=0x%3p,%d: Init failed. No Mitsumi CD-ROM?.\n",
1108 MCDX, stuffp->wreg_data, stuffp->irq));
1109 kfree(stuffp);
1110 continue;
1111 }
1112
1113 TRACE((INIT, "init() register blkdev\n"));
1114 if (register_blkdev(MAJOR_NR, DEVICE_NAME, &mcdx_fops) != 0) {
1115 WARN(("%s=0x%3p,%d: Init failed. Can't get major %d.\n",
1116 MCDX,
1117 stuffp->wreg_data, stuffp->irq, MAJOR_NR));
1118 kfree(stuffp);
1119 continue;
1120 }
1121
1122 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1123 read_ahead[MAJOR_NR] = READ_AHEAD;
1124
1125 #if WE_KNOW_WHY
1126 blksize_size[MAJOR_NR] = BLKSIZES;
1127 #endif
1128
1129 TRACE((INIT, "init() subscribe irq and i/o\n"));
1130 mcdx_irq_map[stuffp->irq] = stuffp;
1131 if (request_irq(stuffp->irq, mcdx_intr, SA_INTERRUPT, DEVICE_NAME)) {
1132 WARN(("%s=0x%3p,%d: Init failed. Can't get irq (%d).\n",
1133 MCDX,
1134 stuffp->wreg_data, stuffp->irq, stuffp->irq));
1135 stuffp->irq = 0;
1136 kfree(stuffp);
1137 continue;
1138 }
1139 request_region((unsigned int) stuffp->wreg_data,
1140 MCDX_IO_SIZE,
1141 DEVICE_NAME);
1142
1143 TRACE((INIT, "init() get garbage\n"));
1144 {
1145 int i;
1146 mcdx_delay(stuffp, HZ/2);
1147 for (i = 100; i; i--) (void) inb((unsigned int) stuffp->rreg_status);
1148 }
1149
1150
1151 #if WE_KNOW_WHY
1152 outb(0x50, (unsigned int) stuffp->wreg_chn);
1153 #endif
1154
1155 TRACE((INIT, "init() set non dma but irq mode\n"));
1156 mcdx_config(stuffp, 1);
1157
1158 stuffp->minor = drive;
1159
1160 WARN((DEVICE_NAME " installed at 0x%3p, irq %d."
1161 " (Firmware version %c %x)\n",
1162 stuffp->wreg_data, stuffp->irq, version.code,
1163 version.ver));
1164 mcdx_stuffp[drive] = stuffp;
1165 TRACE((INIT, "init() mcdx_stuffp[%d] = %p\n", drive, stuffp));
1166 }
1167
1168 return 0;
1169 }
1170
1171
1172 static int mcdx_transfer(struct s_drive_stuff *stuffp,
1173 char *p, int sector, int nr_sectors)
1174
1175
1176
1177 {
1178 int off;
1179 int done = 0;
1180
1181 TRACE((TRANSFER, "transfer() %d sectors at sector %d\n",
1182 nr_sectors, sector));
1183
1184 if (stuffp->audio) {
1185 WARN(("attempt to read from audio cd\n"));
1186 return -1;
1187 }
1188
1189 while (stuffp->lock)
1190 interruptible_sleep_on(&stuffp->lockq);
1191 if (current->signal && ~current->blocked) {
1192 WARN(("talk() got signal %d\n", current->signal));
1193 }
1194
1195 if (stuffp->valid
1196 && (sector >= stuffp->pending)
1197 && (sector < stuffp->off_direct)) {
1198
1199
1200 off = stuffp->off_requested < (off = sector + nr_sectors)
1201 ? stuffp->off_requested : off;
1202
1203 stuffp->lock = current->pid;
1204
1205 do {
1206 int sig = 0;
1207 int to = 0;
1208
1209
1210 current->timeout = jiffies + 5*HZ;
1211 while (stuffp->busy) {
1212 interruptible_sleep_on(&stuffp->busyq);
1213 if ((sig = (current->signal && ~current->blocked))
1214 || (to = (current->timeout == 0))) {
1215 break;
1216 }
1217 }
1218
1219 current->timeout = 0;
1220
1221
1222 if (((stuffp->busy == 0) && !stuffp->introk)
1223 || sig
1224 || to) {
1225 if ((stuffp->busy == 0) && !stuffp->introk)
1226 WARN(("mcdx_transfer() failure in data request\n"));
1227 else if (to)
1228 WARN(("mcdx_transfer(): timeout\n"));
1229 else if (sig)
1230 WARN(("mcdx_transfer(): got signal 0x%lx\n", current->signal));
1231
1232 stuffp->lock = 0;
1233 stuffp->busy = 0;
1234 wake_up_interruptible(&stuffp->lockq);
1235 wake_up_interruptible(&stuffp->busyq);
1236 stuffp->errno = MCDX_E;
1237 TRACE((TRANSFER, "transfer() done (-1)\n"));
1238 return -1;
1239 }
1240
1241
1242
1243 if (stuffp->xa && (0 == (stuffp->pending & 3))) {
1244 const int HEAD = CD_FRAMESIZE_RAW - CD_XA_TAIL - CD_FRAMESIZE;
1245 TRACE((TRANSFER, "transfer() sector %d, skip %d header bytes\n",
1246 stuffp->pending, HEAD));
1247 insb((unsigned int) stuffp->rreg_data, p, HEAD);
1248 }
1249
1250
1251
1252 TRACE((TRANSFER, "transfer() read sector %d\n", stuffp->pending));
1253 insb((unsigned int) stuffp->rreg_data, p, 512);
1254
1255
1256
1257
1258 if ((stuffp->busy = (3 == (stuffp->pending & 3))) && stuffp->xa) {
1259 char dummy[CD_XA_TAIL];
1260 TRACE((TRANSFER, "transfer() sector %d, skip %d trailer bytes\n",
1261 stuffp->pending, CD_XA_TAIL));
1262 insb((unsigned int) stuffp->rreg_data, &dummy[0], CD_XA_TAIL);
1263 }
1264
1265 if (stuffp->pending == sector) {
1266 p += 512;
1267 done++;
1268 sector++;
1269 }
1270 }
1271 while (++(stuffp->pending) < off);
1272
1273 stuffp->lock = 0;
1274 wake_up_interruptible(&stuffp->lockq);
1275
1276 } else {
1277
1278 static unsigned char cmd[] = {
1279 0,
1280 0, 0, 0,
1281 0, 0, 0
1282 };
1283
1284 cmd[0] = stuffp->readcmd;
1285
1286 stuffp->valid = 1;
1287 stuffp->pending = sector & ~3;
1288
1289
1290 TRACE((TRANSFER, "transfer() request sector %d\n", stuffp->pending));
1291 if (stuffp->pending > stuffp->lastsector) {
1292 WARN(("transfer() sector %d from nirvana requested.\n",
1293 stuffp->pending));
1294 stuffp->errno = MCDX_EOM;
1295 TRACE((TRANSFER, "transfer() done (-1)\n"));
1296 return -1;
1297 }
1298
1299 if ((stuffp->off_direct = stuffp->pending + DIRECT_SIZE)
1300 > stuffp->lastsector + 1)
1301 stuffp->off_direct = stuffp->lastsector + 1;
1302 if ((stuffp->off_requested = stuffp->pending + REQUEST_SIZE)
1303 > stuffp->lastsector + 1)
1304 stuffp->off_requested = stuffp->lastsector + 1;
1305
1306 TRACE((TRANSFER, "transfer() pending %d\n", stuffp->pending));
1307 TRACE((TRANSFER, "transfer() off_dir %d\n", stuffp->off_direct));
1308 TRACE((TRANSFER, "transfer() off_req %d\n", stuffp->off_requested));
1309
1310 {
1311 struct s_msf pending;
1312 log2msf(stuffp->pending / 4, &pending);
1313 cmd[1] = pending.minute;
1314 cmd[2] = pending.second;
1315 cmd[3] = pending.frame;
1316 }
1317
1318 stuffp->busy = 1;
1319 cmd[6] = (unsigned char) (stuffp->off_requested - stuffp->pending) / 4;
1320
1321 outsb((unsigned int) stuffp->wreg_data, cmd, sizeof cmd);
1322
1323 }
1324
1325 stuffp->off_direct = (stuffp->off_direct += done) < stuffp->off_requested
1326 ? stuffp->off_direct : stuffp->off_requested;
1327
1328 TRACE((TRANSFER, "transfer() done (%d)\n", done));
1329 return done;
1330 }
1331
1332
1333
1334
1335 static char* port(int *ip) { return (char*) ip[0]; }
1336 static int irq(int *ip) { return ip[1]; }
1337
1338
1339
1340 static unsigned int bcd2uint(unsigned char c)
1341 { return (c >> 4) * 10 + (c & 0x0f); }
1342
1343 static unsigned int uint2bcd(unsigned int ival)
1344 { return ((ival / 10) << 4) | (ival % 10); }
1345
1346 static void log2msf(unsigned int l, struct s_msf* pmsf)
1347 {
1348 l += CD_BLOCK_OFFSET;
1349 pmsf->minute = uint2bcd(l / 4500), l %= 4500;
1350 pmsf->second = uint2bcd(l / 75);
1351 pmsf->frame = uint2bcd(l % 75);
1352 }
1353
1354 static unsigned int msf2log(const struct s_msf* pmsf)
1355 {
1356 return bcd2uint(pmsf->frame)
1357 + bcd2uint(pmsf->second) * 75
1358 + bcd2uint(pmsf->minute) * 4500
1359 - CD_BLOCK_OFFSET;
1360 }
1361
1362 int mcdx_readtoc(struct s_drive_stuff* stuffp)
1363
1364
1365 {
1366
1367 if (stuffp->toc) {
1368 TRACE((IOCTL, "ioctl() toc already read\n"));
1369 return 0;
1370 }
1371
1372 TRACE((IOCTL, "ioctl() readtoc for %d tracks\n",
1373 stuffp->di.n_last - stuffp->di.n_first + 1));
1374
1375 if (-1 == mcdx_hold(stuffp, 1)) return -1;
1376
1377 TRACE((IOCTL, "ioctl() tocmode\n"));
1378 if (-1 == mcdx_setdrivemode(stuffp, TOC, 1)) return -EIO;
1379
1380
1381 {
1382 int size;
1383 size = sizeof(struct s_subqcode) * (stuffp->di.n_last - stuffp->di.n_first + 2);
1384
1385 TRACE((MALLOC, "ioctl() malloc %d bytes\n", size));
1386 stuffp->toc = kmalloc(size, GFP_KERNEL);
1387 if (!stuffp->toc) {
1388 WARN(("Cannot malloc %s bytes for toc\n", size));
1389 mcdx_setdrivemode(stuffp, DATA, 1);
1390 return -EIO;
1391 }
1392 }
1393
1394
1395 {
1396 int trk;
1397 int retries;
1398
1399 for (trk = 0;
1400 trk < (stuffp->di.n_last - stuffp->di.n_first + 1);
1401 trk++)
1402 stuffp->toc[trk].index = 0;
1403
1404 for (retries = 300; retries; retries--) {
1405 struct s_subqcode q;
1406 unsigned int idx;
1407
1408 if (-1 == mcdx_requestsubqcode(stuffp, &q, 1)) {
1409 mcdx_setdrivemode(stuffp, DATA, 1);
1410 return -EIO;
1411 }
1412
1413 idx = bcd2uint(q.index);
1414
1415 if ((idx > 0)
1416 && (idx <= stuffp->di.n_last)
1417 && (q.tno == 0)
1418 && (stuffp->toc[idx - stuffp->di.n_first].index == 0)) {
1419 stuffp->toc[idx - stuffp->di.n_first] = q;
1420 TRACE((IOCTL, "ioctl() toc idx %d (trk %d)\n", idx, trk));
1421 trk--;
1422 }
1423 if (trk == 0) break;
1424 }
1425 memset(&stuffp->toc[stuffp->di.n_last - stuffp->di.n_first + 1],
1426 0, sizeof(stuffp->toc[0]));
1427 stuffp->toc[stuffp->di.n_last - stuffp->di.n_first + 1].dt
1428 = stuffp->di.msf_leadout;
1429 }
1430
1431
1432 TRACE((IOCTL, "ioctl() undo toc mode\n"));
1433 if (-1 == mcdx_setdrivemode(stuffp, DATA, 2))
1434 return -EIO;
1435
1436 #if MCDX_DEBUG && IOCTL
1437 { int trk;
1438 for (trk = 0;
1439 trk < (stuffp->di.n_last - stuffp->di.n_first + 2);
1440 trk++)
1441 TRACE((IOCTL, "ioctl() %d readtoc %02x %02x %02x"
1442 " %02x:%02x.%02x %02x:%02x.%02x\n",
1443 trk + stuffp->di.n_first,
1444 stuffp->toc[trk].control, stuffp->toc[trk].tno, stuffp->toc[trk].index,
1445 stuffp->toc[trk].tt.minute, stuffp->toc[trk].tt.second, stuffp->toc[trk].tt.frame,
1446 stuffp->toc[trk].dt.minute, stuffp->toc[trk].dt.second, stuffp->toc[trk].dt.frame));
1447 }
1448 #endif
1449
1450 return 0;
1451 }
1452
1453 static int
1454 mcdx_playmsf(struct s_drive_stuff* stuffp, const struct cdrom_msf* msf)
1455 {
1456 unsigned char cmd[7] = {
1457 0, 0, 0, 0, 0, 0, 0
1458 };
1459
1460 cmd[0] = stuffp->playcmd;
1461
1462 cmd[1] = msf->cdmsf_min0;
1463 cmd[2] = msf->cdmsf_sec0;
1464 cmd[3] = msf->cdmsf_frame0;
1465 cmd[4] = msf->cdmsf_min1;
1466 cmd[5] = msf->cdmsf_sec1;
1467 cmd[6] = msf->cdmsf_frame1;
1468
1469 TRACE((IOCTL, "ioctl(): play %x "
1470 "%02x:%02x:%02x -- %02x:%02x:%02x\n",
1471 cmd[0], cmd[1], cmd[2], cmd[3],
1472 cmd[4], cmd[5], cmd[6]));
1473
1474 outsb((unsigned int) stuffp->wreg_data, cmd, sizeof cmd);
1475
1476 if (-1 == mcdx_getval(stuffp, 1*HZ, 0, NULL)) {
1477 WARN(("playmsf() timeout\n"));
1478 return -1;
1479 }
1480
1481 stuffp->audiostatus = CDROM_AUDIO_PLAY;
1482 return 0;
1483 }
1484
1485 static int
1486 mcdx_playtrk(struct s_drive_stuff* stuffp, const struct cdrom_ti* ti)
1487 {
1488 struct s_subqcode* p;
1489 struct cdrom_msf msf;
1490
1491 if (-1 == mcdx_readtoc(stuffp)) return -1;
1492
1493 if (ti) p = &stuffp->toc[ti->cdti_trk0 - stuffp->di.n_first];
1494 else p = &stuffp->start;
1495
1496 msf.cdmsf_min0 = p->dt.minute;
1497 msf.cdmsf_sec0 = p->dt.second;
1498 msf.cdmsf_frame0 = p->dt.frame;
1499
1500 if (ti) {
1501 p = &stuffp->toc[ti->cdti_trk1 - stuffp->di.n_first + 1];
1502 stuffp->stop = *p;
1503 } else p = &stuffp->stop;
1504
1505 msf.cdmsf_min1 = p->dt.minute;
1506 msf.cdmsf_sec1 = p->dt.second;
1507 msf.cdmsf_frame1 = p->dt.frame;
1508
1509 return mcdx_playmsf(stuffp, &msf);
1510 }
1511
1512
1513
1514
1515 static int
1516 mcdx_closedoor(struct s_drive_stuff *stuffp, int tries)
1517 {
1518 if (stuffp->present & DOOR)
1519 return mcdx_talk(stuffp, "\xf8", 1, NULL, 0, 5*HZ, tries);
1520 else
1521 return 0;
1522 }
1523
1524 static int
1525 mcdx_stop(struct s_drive_stuff *stuffp, int tries)
1526 { return mcdx_talk(stuffp, "\xf0", 1, NULL, 0, 2*HZ, tries); }
1527
1528 static int
1529 mcdx_hold(struct s_drive_stuff *stuffp, int tries)
1530 { return mcdx_talk(stuffp, "\x70", 1, NULL, 0, 2*HZ, tries); }
1531
1532 static int
1533 mcdx_eject(struct s_drive_stuff *stuffp, int tries)
1534 {
1535 if (stuffp->present & DOOR)
1536 return mcdx_talk(stuffp, "\xf6", 1, NULL, 0, 5*HZ, tries);
1537 else
1538 return 0;
1539 }
1540
1541 static int
1542 mcdx_requestsubqcode(struct s_drive_stuff *stuffp,
1543 struct s_subqcode *sub,
1544 int tries)
1545 {
1546 char buf[11];
1547 int ans;
1548
1549 if (-1 == (ans = mcdx_talk(
1550 stuffp, "\x20", 1, buf, sizeof(buf),
1551 2*HZ, tries)))
1552 return -1;
1553 sub->control = buf[1];
1554 sub->tno = buf[2];
1555 sub->index = buf[3];
1556 sub->tt.minute = buf[4];
1557 sub->tt.second = buf[5];
1558 sub->tt.frame = buf[6];
1559 sub->dt.minute = buf[8];
1560 sub->dt.second = buf[9];
1561 sub->dt.frame = buf[10];
1562
1563 return ans;
1564 }
1565
1566 static int
1567 mcdx_requestmultidiskinfo(struct s_drive_stuff *stuffp, struct s_multi *multi, int tries)
1568 {
1569 char buf[5];
1570 int ans;
1571
1572 if (stuffp->present & MULTI) {
1573 ans = mcdx_talk(stuffp, "\x11", 1, buf, sizeof(buf), 2*HZ, tries);
1574 multi->multi = buf[1];
1575 multi->msf_last.minute = buf[2];
1576 multi->msf_last.second = buf[3];
1577 multi->msf_last.frame = buf[4];
1578 return ans;
1579 } else {
1580 multi->multi = 0;
1581 return 0;
1582 }
1583 }
1584
1585 static int
1586 mcdx_requesttocdata(struct s_drive_stuff *stuffp, struct s_diskinfo *info, int tries)
1587 {
1588 char buf[9];
1589 int ans;
1590 ans = mcdx_talk(stuffp, "\x10", 1, buf, sizeof(buf), 2*HZ, tries);
1591 info->n_first = bcd2uint(buf[1]);
1592 info->n_last = bcd2uint(buf[2]);
1593 info->msf_leadout.minute = buf[3];
1594 info->msf_leadout.second = buf[4];
1595 info->msf_leadout.frame = buf[5];
1596 info->msf_first.minute = buf[6];
1597 info->msf_first.second = buf[7];
1598 info->msf_first.frame = buf[8];
1599 return ans;
1600 }
1601
1602 static int
1603 mcdx_setdrivemode(struct s_drive_stuff *stuffp, enum drivemodes mode, int tries)
1604 {
1605 char cmd[2];
1606 int ans;
1607
1608 TRACE((HW, "setdrivemode() %d\n", mode));
1609
1610 if (-1 == (ans = mcdx_talk(stuffp, "\xc2", 1, cmd, sizeof(cmd), 5*HZ, tries)))
1611 return -1;
1612
1613 switch (mode) {
1614 case TOC: cmd[1] |= 0x04; break;
1615 case DATA: cmd[1] &= ~0x04; break;
1616 case RAW: cmd[1] |= 0x40; break;
1617 case COOKED: cmd[1] &= ~0x40; break;
1618 default: break;
1619 }
1620 cmd[0] = 0x50;
1621 return mcdx_talk(stuffp, cmd, 2, NULL, 0, 5*HZ, tries);
1622 }
1623
1624
1625 static int
1626 mcdx_setdatamode(struct s_drive_stuff *stuffp, enum datamodes mode, int tries)
1627 {
1628 unsigned char cmd[2] = { 0xa0 };
1629 TRACE((HW, "setdatamode() %d\n", mode));
1630 switch (mode) {
1631 case MODE0: cmd[1] = 0x00; break;
1632 case MODE1: cmd[1] = 0x01; break;
1633 case MODE2: cmd[1] = 0x02; break;
1634 default: return -EINVAL;
1635 }
1636 return mcdx_talk(stuffp, cmd, 2, NULL, 0, 5*HZ, tries);
1637 }
1638
1639 static int
1640 mcdx_config(struct s_drive_stuff *stuffp, int tries)
1641 {
1642 char cmd[4];
1643
1644 TRACE((HW, "config()\n"));
1645
1646 cmd[0] = 0x90;
1647
1648 cmd[1] = 0x10;
1649 cmd[2] = 0x05;
1650
1651 if (-1 == mcdx_talk(stuffp, cmd, 3, NULL, 0, 1*HZ, tries))
1652 return -1;
1653
1654 cmd[1] = 0x02;
1655 cmd[2] = 0x00;
1656
1657 return mcdx_talk(stuffp, cmd, 3, NULL, 0, 1*HZ, tries);
1658 }
1659
1660 static int
1661 mcdx_requestversion(struct s_drive_stuff *stuffp, struct s_version *ver, int tries)
1662 {
1663 char buf[3];
1664 int ans;
1665
1666 if (-1 == (ans = mcdx_talk(stuffp, "\xdc", 1, buf, sizeof(buf), 2*HZ, tries)))
1667 return ans;
1668
1669 ver->code = buf[1];
1670 ver->ver = buf[2];
1671
1672 return ans;
1673 }
1674
1675 static int
1676 mcdx_reset(struct s_drive_stuff *stuffp, enum resetmodes mode, int tries)
1677 {
1678 if (mode == HARD) {
1679 outb(0, (unsigned int) stuffp->wreg_chn);
1680 outb(0, (unsigned int) stuffp->wreg_reset);
1681 return 0;
1682 } else return mcdx_talk(stuffp, "\x60", 1, NULL, 0, 5*HZ, tries);
1683 }
1684
1685 static int
1686 mcdx_lockdoor(struct s_drive_stuff *stuffp, int lock, int tries)
1687 {
1688 char cmd[2] = { 0xfe };
1689 if (stuffp->present & DOOR) {
1690 cmd[1] = lock ? 0x01 : 0x00;
1691 return mcdx_talk(stuffp, cmd, sizeof(cmd), NULL, 0, 5*HZ, tries);
1692 } else
1693 return 0;
1694 }
1695
1696 static int
1697 mcdx_getstatus(struct s_drive_stuff *stuffp, int tries)
1698 { return mcdx_talk(stuffp, "\x40", 1, NULL, 0, 5*HZ, tries); }
1699
1700 static int
1701 mcdx_getval(struct s_drive_stuff *stuffp, int to, int delay, char* buf)
1702 {
1703 unsigned long timeout = to + jiffies;
1704 char c;
1705
1706 if (!buf) buf = &c;
1707
1708 while (inb((unsigned int) stuffp->rreg_status) & MCDX_RBIT_STEN) {
1709 if (jiffies > timeout) return -1;
1710 mcdx_delay(stuffp, delay);
1711 }
1712
1713 *buf = (unsigned char) inb((unsigned int) stuffp->rreg_data) & 0xff;
1714
1715 return 0;
1716 }