OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [cdrom/] [sjcd.c] - Blame information for rev 1275

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* -- sjcd.c
2
 *
3
 *   Sanyo CD-ROM device driver implementation, Version 1.6
4
 *   Copyright (C) 1995  Vadim V. Model
5
 *
6
 *   model@cecmow.enet.dec.com
7
 *   vadim@rbrf.ru
8
 *   vadim@ipsun.ras.ru
9
 *
10
 *
11
 *  This driver is based on pre-works by Eberhard Moenkeberg (emoenke@gwdg.de);
12
 *  it was developed under use of mcd.c from Martin Harriss, with help of
13
 *  Eric van der Maarel (H.T.M.v.d.Maarel@marin.nl).
14
 *
15
 *  It is planned to include these routines into sbpcd.c later - to make
16
 *  a "mixed use" on one cable possible for all kinds of drives which use
17
 *  the SoundBlaster/Panasonic style CDROM interface. But today, the
18
 *  ability to install directly from CDROM is more important than flexibility.
19
 *
20
 *  This program is free software; you can redistribute it and/or modify
21
 *  it under the terms of the GNU General Public License as published by
22
 *  the Free Software Foundation; either version 2 of the License, or
23
 *  (at your option) any later version.
24
 *
25
 *  This program is distributed in the hope that it will be useful,
26
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 *  GNU General Public License for more details.
29
 *
30
 *  You should have received a copy of the GNU General Public License
31
 *  along with this program; if not, write to the Free Software
32
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33
 *
34
 *  History:
35
 *  1.1 First public release with kernel version 1.3.7.
36
 *      Written by Vadim Model.
37
 *  1.2 Added detection and configuration of cdrom interface
38
 *      on ISP16 soundcard.
39
 *      Allow for command line options: sjcd=<io_base>,<irq>,<dma>
40
 *  1.3 Some minor changes to README.sjcd.
41
 *  1.4 MSS Sound support!! Listen to a CD through the speakers.
42
 *  1.5 Module support and bugfixes.
43
 *      Tray locking.
44
 *  1.6 Removed ISP16 code from this driver.
45
 *      Allow only to set io base address on command line: sjcd=<io_base>
46
 *      Changes to Documentation/cdrom/sjcd
47
 *      Added cleanup after any error in the initialisation.
48
 *  1.7 Added code to set the sector size tables to prevent the bug present in
49
 *      the previous version of this driver.  Coded added by Anthony Barbachan
50
 *      from bugfix tip originally suggested by Alan Cox.
51
 *
52
 *  November 1999 -- Make kernel-parameter implementation work with 2.3.x
53
 *                   Removed init_module & cleanup_module in favor of
54
 *                   module_init & module_exit.
55
 *                   Torben Mathiasen <tmm@image.dk>
56
 */
57
 
58
#define SJCD_VERSION_MAJOR 1
59
#define SJCD_VERSION_MINOR 7
60
 
61
#include <linux/module.h>
62
#include <linux/errno.h>
63
#include <linux/sched.h>
64
#include <linux/mm.h>
65
#include <linux/timer.h>
66
#include <linux/fs.h>
67
#include <linux/kernel.h>
68
#include <linux/cdrom.h>
69
#include <linux/ioport.h>
70
#include <linux/string.h>
71
#include <linux/major.h>
72
#include <linux/init.h>
73
#include <linux/devfs_fs_kernel.h>
74
 
75
#include <asm/system.h>
76
#include <asm/io.h>
77
#include <asm/uaccess.h>
78
 
79
#define MAJOR_NR SANYO_CDROM_MAJOR
80
#include <linux/blk.h>
81
#include "sjcd.h"
82
 
83
static int sjcd_present = 0;
84
 
85
#define SJCD_BUF_SIZ 32         /* cdr-h94a has internal 64K buffer */
86
 
87
/*
88
 * buffer for block size conversion
89
 */
90
static char sjcd_buf[2048 * SJCD_BUF_SIZ];
91
static volatile int sjcd_buf_bn[SJCD_BUF_SIZ], sjcd_next_bn;
92
static volatile int sjcd_buf_in, sjcd_buf_out = -1;
93
 
94
/*
95
 * Status.
96
 */
97
static unsigned short sjcd_status_valid = 0;
98
static unsigned short sjcd_door_closed;
99
static unsigned short sjcd_door_was_open;
100
static unsigned short sjcd_media_is_available;
101
static unsigned short sjcd_media_is_changed;
102
static unsigned short sjcd_toc_uptodate = 0;
103
static unsigned short sjcd_command_failed;
104
static volatile unsigned char sjcd_completion_status = 0;
105
static volatile unsigned char sjcd_completion_error = 0;
106
static unsigned short sjcd_command_is_in_progress = 0;
107
static unsigned short sjcd_error_reported = 0;
108
 
109
static int sjcd_open_count;
110
 
111
static int sjcd_audio_status;
112
static struct sjcd_play_msf sjcd_playing;
113
 
114
static int sjcd_base = SJCD_BASE_ADDR;
115
 
116
MODULE_PARM(sjcd_base, "i");
117
 
118
static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
119
 
120
/*
121
 * Data transfer.
122
 */
123
static volatile unsigned short sjcd_transfer_is_active = 0;
124
 
125
enum sjcd_transfer_state {
126
        SJCD_S_IDLE = 0,
127
        SJCD_S_START = 1,
128
        SJCD_S_MODE = 2,
129
        SJCD_S_READ = 3,
130
        SJCD_S_DATA = 4,
131
        SJCD_S_STOP = 5,
132
        SJCD_S_STOPPING = 6
133
};
134
static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
135
static long sjcd_transfer_timeout = 0;
136
static int sjcd_read_count = 0;
137
static unsigned char sjcd_mode = 0;
138
 
139
#define SJCD_READ_TIMEOUT 5000
140
 
141
#if defined( SJCD_GATHER_STAT )
142
/*
143
 * Statistic.
144
 */
145
static struct sjcd_stat statistic;
146
#endif
147
 
148
/*
149
 * Timer.
150
 */
151
static struct timer_list sjcd_delay_timer;
152
 
153
#define SJCD_SET_TIMER( func, tmout )           \
154
    ( sjcd_delay_timer.expires = jiffies+tmout,         \
155
      sjcd_delay_timer.function = ( void * )func, \
156
      add_timer( &sjcd_delay_timer ) )
157
 
158
#define CLEAR_TIMER del_timer( &sjcd_delay_timer )
159
 
160
static int sjcd_cleanup(void);
161
 
162
/*
163
 * Set up device, i.e., use command line data to set
164
 * base address.
165
 */
166
#ifndef MODULE
167
static int __init sjcd_setup(char *str)
168
{
169
        int ints[2];
170
        (void) get_options(str, ARRAY_SIZE(ints), ints);
171
        if (ints[0] > 0)
172
                sjcd_base = ints[1];
173
 
174
        return 1;
175
}
176
 
177
__setup("sjcd=", sjcd_setup);
178
 
179
#endif
180
 
181
/*
182
 * Special converters.
183
 */
184
static unsigned char bin2bcd(int bin)
185
{
186
        int u, v;
187
 
188
        u = bin % 10;
189
        v = bin / 10;
190
        return (u | (v << 4));
191
}
192
 
193
static int bcd2bin(unsigned char bcd)
194
{
195
        return ((bcd >> 4) * 10 + (bcd & 0x0F));
196
}
197
 
198
static long msf2hsg(struct msf *mp)
199
{
200
        return (bcd2bin(mp->frame) + bcd2bin(mp->sec) * 75
201
                + bcd2bin(mp->min) * 4500 - 150);
202
}
203
 
204
static void hsg2msf(long hsg, struct msf *msf)
205
{
206
        hsg += 150;
207
        msf->min = hsg / 4500;
208
        hsg %= 4500;
209
        msf->sec = hsg / 75;
210
        msf->frame = hsg % 75;
211
        msf->min = bin2bcd(msf->min);   /* convert to BCD */
212
        msf->sec = bin2bcd(msf->sec);
213
        msf->frame = bin2bcd(msf->frame);
214
}
215
 
216
/*
217
 * Send a command to cdrom. Invalidate status.
218
 */
219
static void sjcd_send_cmd(unsigned char cmd)
220
{
221
#if defined( SJCD_TRACE )
222
        printk("SJCD: send_cmd( 0x%x )\n", cmd);
223
#endif
224
        outb(cmd, SJCDPORT(0));
225
        sjcd_command_is_in_progress = 1;
226
        sjcd_status_valid = 0;
227
        sjcd_command_failed = 0;
228
}
229
 
230
/*
231
 * Send a command with one arg to cdrom. Invalidate status.
232
 */
233
static void sjcd_send_1_cmd(unsigned char cmd, unsigned char a)
234
{
235
#if defined( SJCD_TRACE )
236
        printk("SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a);
237
#endif
238
        outb(cmd, SJCDPORT(0));
239
        outb(a, SJCDPORT(0));
240
        sjcd_command_is_in_progress = 1;
241
        sjcd_status_valid = 0;
242
        sjcd_command_failed = 0;
243
}
244
 
245
/*
246
 * Send a command with four args to cdrom. Invalidate status.
247
 */
248
static void sjcd_send_4_cmd(unsigned char cmd, unsigned char a,
249
                            unsigned char b, unsigned char c,
250
                            unsigned char d)
251
{
252
#if defined( SJCD_TRACE )
253
        printk("SJCD: send_4_cmd( 0x%x )\n", cmd);
254
#endif
255
        outb(cmd, SJCDPORT(0));
256
        outb(a, SJCDPORT(0));
257
        outb(b, SJCDPORT(0));
258
        outb(c, SJCDPORT(0));
259
        outb(d, SJCDPORT(0));
260
        sjcd_command_is_in_progress = 1;
261
        sjcd_status_valid = 0;
262
        sjcd_command_failed = 0;
263
}
264
 
265
/*
266
 * Send a play or read command to cdrom. Invalidate Status.
267
 */
268
static void sjcd_send_6_cmd(unsigned char cmd, struct sjcd_play_msf *pms)
269
{
270
#if defined( SJCD_TRACE )
271
        printk("SJCD: send_long_cmd( 0x%x )\n", cmd);
272
#endif
273
        outb(cmd, SJCDPORT(0));
274
        outb(pms->start.min, SJCDPORT(0));
275
        outb(pms->start.sec, SJCDPORT(0));
276
        outb(pms->start.frame, SJCDPORT(0));
277
        outb(pms->end.min, SJCDPORT(0));
278
        outb(pms->end.sec, SJCDPORT(0));
279
        outb(pms->end.frame, SJCDPORT(0));
280
        sjcd_command_is_in_progress = 1;
281
        sjcd_status_valid = 0;
282
        sjcd_command_failed = 0;
283
}
284
 
285
/*
286
 * Get a value from the data port. Should not block, so we use a little
287
 * wait for a while. Returns 0 if OK.
288
 */
289
static int sjcd_load_response(void *buf, int len)
290
{
291
        unsigned char *resp = (unsigned char *) buf;
292
 
293
        for (; len; --len) {
294
                int i;
295
                for (i = 200;
296
                     i-- && !SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))););
297
                if (i > 0)
298
                        *resp++ = (unsigned char) inb(SJCDPORT(0));
299
                else
300
                        break;
301
        }
302
        return (len);
303
}
304
 
305
/*
306
 * Load and parse command completion status (drive info byte and maybe error).
307
 * Sorry, no error classification yet.
308
 */
309
static void sjcd_load_status(void)
310
{
311
        sjcd_media_is_changed = 0;
312
        sjcd_completion_error = 0;
313
        sjcd_completion_status = inb(SJCDPORT(0));
314
        if (sjcd_completion_status & SST_DOOR_OPENED) {
315
                sjcd_door_closed = sjcd_media_is_available = 0;
316
        } else {
317
                sjcd_door_closed = 1;
318
                if (sjcd_completion_status & SST_MEDIA_CHANGED)
319
                        sjcd_media_is_available = sjcd_media_is_changed =
320
                            1;
321
                else if (sjcd_completion_status & 0x0F) {
322
                        /*
323
                         * OK, we seem to catch an error ...
324
                         */
325
                        while (!SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))));
326
                        sjcd_completion_error = inb(SJCDPORT(0));
327
                        if ((sjcd_completion_status & 0x08) &&
328
                            (sjcd_completion_error & 0x40))
329
                                sjcd_media_is_available = 0;
330
                        else
331
                                sjcd_command_failed = 1;
332
                } else
333
                        sjcd_media_is_available = 1;
334
        }
335
        /*
336
         * Ok, status loaded successfully.
337
         */
338
        sjcd_status_valid = 1, sjcd_error_reported = 0;
339
        sjcd_command_is_in_progress = 0;
340
 
341
        /*
342
         * If the disk is changed, the TOC is not valid.
343
         */
344
        if (sjcd_media_is_changed)
345
                sjcd_toc_uptodate = 0;
346
#if defined( SJCD_TRACE )
347
        printk("SJCD: status %02x.%02x loaded.\n",
348
               (int) sjcd_completion_status, (int) sjcd_completion_error);
349
#endif
350
}
351
 
352
/*
353
 * Read status from cdrom. Check to see if the status is available.
354
 */
355
static int sjcd_check_status(void)
356
{
357
        /*
358
         * Try to load the response from cdrom into buffer.
359
         */
360
        if (SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1)))) {
361
                sjcd_load_status();
362
                return (1);
363
        } else {
364
                /*
365
                 * No status is available.
366
                 */
367
                return (0);
368
        }
369
}
370
 
371
/*
372
 * This is just timeout counter, and nothing more. Surprised ? :-)
373
 */
374
static volatile long sjcd_status_timeout;
375
 
376
/*
377
 * We need about 10 seconds to wait. The longest command takes about 5 seconds
378
 * to probe the disk (usually after tray closed or drive reset). Other values
379
 * should be thought of for other commands.
380
 */
381
#define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
382
 
383
static void sjcd_status_timer(void)
384
{
385
        if (sjcd_check_status()) {
386
                /*
387
                 * The command completed and status is loaded, stop waiting.
388
                 */
389
                wake_up(&sjcd_waitq);
390
        } else if (--sjcd_status_timeout <= 0) {
391
                /*
392
                 * We are timed out.
393
                 */
394
                wake_up(&sjcd_waitq);
395
        } else {
396
                /*
397
                 * We have still some time to wait. Try again.
398
                 */
399
                SJCD_SET_TIMER(sjcd_status_timer, 1);
400
        }
401
}
402
 
403
/*
404
 * Wait for status for 10 sec approx. Returns non-positive when timed out.
405
 * Should not be used while reading data CDs.
406
 */
407
static int sjcd_wait_for_status(void)
408
{
409
        sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
410
        SJCD_SET_TIMER(sjcd_status_timer, 1);
411
        sleep_on(&sjcd_waitq);
412
#if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
413
        if (sjcd_status_timeout <= 0)
414
                printk("SJCD: Error Wait For Status.\n");
415
#endif
416
        return (sjcd_status_timeout);
417
}
418
 
419
static int sjcd_receive_status(void)
420
{
421
        int i;
422
#if defined( SJCD_TRACE )
423
        printk("SJCD: receive_status\n");
424
#endif
425
        /*
426
         * Wait a bit for status available.
427
         */
428
        for (i = 200; i-- && (sjcd_check_status() == 0););
429
        if (i < 0) {
430
#if defined( SJCD_TRACE )
431
                printk("SJCD: long wait for status\n");
432
#endif
433
                if (sjcd_wait_for_status() <= 0)
434
                        printk("SJCD: Timeout when read status.\n");
435
                else
436
                        i = 0;
437
        }
438
        return (i);
439
}
440
 
441
/*
442
 * Load the status. Issue get status command and wait for status available.
443
 */
444
static void sjcd_get_status(void)
445
{
446
#if defined( SJCD_TRACE )
447
        printk("SJCD: get_status\n");
448
#endif
449
        sjcd_send_cmd(SCMD_GET_STATUS);
450
        sjcd_receive_status();
451
}
452
 
453
/*
454
 * Check the drive if the disk is changed. Should be revised.
455
 */
456
static int sjcd_disk_change(kdev_t full_dev)
457
{
458
#if 0
459
        printk("SJCD: sjcd_disk_change( 0x%x )\n", full_dev);
460
#endif
461
        if (MINOR(full_dev) > 0) {
462
                printk("SJCD: request error: invalid device minor.\n");
463
                return 0;
464
        }
465
        if (!sjcd_command_is_in_progress)
466
                sjcd_get_status();
467
        return (sjcd_status_valid ? sjcd_media_is_changed : 0);
468
}
469
 
470
/*
471
 * Read the table of contents (TOC) and TOC header if necessary.
472
 * We assume that the drive contains no more than 99 toc entries.
473
 */
474
static struct sjcd_hw_disk_info sjcd_table_of_contents[SJCD_MAX_TRACKS];
475
static unsigned char sjcd_first_track_no, sjcd_last_track_no;
476
#define sjcd_disk_length  sjcd_table_of_contents[0].un.track_msf
477
 
478
static int sjcd_update_toc(void)
479
{
480
        struct sjcd_hw_disk_info info;
481
        int i;
482
#if defined( SJCD_TRACE )
483
        printk("SJCD: update toc:\n");
484
#endif
485
        /*
486
         * check to see if we need to do anything
487
         */
488
        if (sjcd_toc_uptodate)
489
                return (0);
490
 
491
        /*
492
         * Get the TOC start information.
493
         */
494
        sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK);
495
        sjcd_receive_status();
496
 
497
        if (!sjcd_status_valid) {
498
                printk("SJCD: cannot load status.\n");
499
                return (-1);
500
        }
501
 
502
        if (!sjcd_media_is_available) {
503
                printk("SJCD: no disk in drive\n");
504
                return (-1);
505
        }
506
 
507
        if (!sjcd_command_failed) {
508
                if (sjcd_load_response(&info, sizeof(info)) != 0) {
509
                        printk
510
                            ("SJCD: cannot load response about TOC start.\n");
511
                        return (-1);
512
                }
513
                sjcd_first_track_no = bcd2bin(info.un.track_no);
514
        } else {
515
                printk("SJCD: get first failed\n");
516
                return (-1);
517
        }
518
#if defined( SJCD_TRACE )
519
        printk("SJCD: TOC start 0x%02x ", sjcd_first_track_no);
520
#endif
521
        /*
522
         * Get the TOC finish information.
523
         */
524
        sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK);
525
        sjcd_receive_status();
526
 
527
        if (!sjcd_status_valid) {
528
                printk("SJCD: cannot load status.\n");
529
                return (-1);
530
        }
531
 
532
        if (!sjcd_media_is_available) {
533
                printk("SJCD: no disk in drive\n");
534
                return (-1);
535
        }
536
 
537
        if (!sjcd_command_failed) {
538
                if (sjcd_load_response(&info, sizeof(info)) != 0) {
539
                        printk
540
                            ("SJCD: cannot load response about TOC finish.\n");
541
                        return (-1);
542
                }
543
                sjcd_last_track_no = bcd2bin(info.un.track_no);
544
        } else {
545
                printk("SJCD: get last failed\n");
546
                return (-1);
547
        }
548
#if defined( SJCD_TRACE )
549
        printk("SJCD: TOC finish 0x%02x ", sjcd_last_track_no);
550
#endif
551
        for (i = sjcd_first_track_no; i <= sjcd_last_track_no; i++) {
552
                /*
553
                 * Get the first track information.
554
                 */
555
                sjcd_send_1_cmd(SCMD_GET_DISK_INFO, bin2bcd(i));
556
                sjcd_receive_status();
557
 
558
                if (!sjcd_status_valid) {
559
                        printk("SJCD: cannot load status.\n");
560
                        return (-1);
561
                }
562
 
563
                if (!sjcd_media_is_available) {
564
                        printk("SJCD: no disk in drive\n");
565
                        return (-1);
566
                }
567
 
568
                if (!sjcd_command_failed) {
569
                        if (sjcd_load_response(&sjcd_table_of_contents[i],
570
                                               sizeof(struct
571
                                                      sjcd_hw_disk_info))
572
                            != 0) {
573
                                printk
574
                                    ("SJCD: cannot load info for %d track\n",
575
                                     i);
576
                                return (-1);
577
                        }
578
                } else {
579
                        printk("SJCD: get info %d failed\n", i);
580
                        return (-1);
581
                }
582
        }
583
 
584
        /*
585
         * Get the disk length info.
586
         */
587
        sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE);
588
        sjcd_receive_status();
589
 
590
        if (!sjcd_status_valid) {
591
                printk("SJCD: cannot load status.\n");
592
                return (-1);
593
        }
594
 
595
        if (!sjcd_media_is_available) {
596
                printk("SJCD: no disk in drive\n");
597
                return (-1);
598
        }
599
 
600
        if (!sjcd_command_failed) {
601
                if (sjcd_load_response(&info, sizeof(info)) != 0) {
602
                        printk
603
                            ("SJCD: cannot load response about disk size.\n");
604
                        return (-1);
605
                }
606
                sjcd_disk_length.min = info.un.track_msf.min;
607
                sjcd_disk_length.sec = info.un.track_msf.sec;
608
                sjcd_disk_length.frame = info.un.track_msf.frame;
609
        } else {
610
                printk("SJCD: get size failed\n");
611
                return (1);
612
        }
613
#if defined( SJCD_TRACE )
614
        printk("SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
615
               sjcd_disk_length.sec, sjcd_disk_length.frame);
616
#endif
617
        return (0);
618
}
619
 
620
/*
621
 * Load subchannel information.
622
 */
623
static int sjcd_get_q_info(struct sjcd_hw_qinfo *qp)
624
{
625
        int s;
626
#if defined( SJCD_TRACE )
627
        printk("SJCD: load sub q\n");
628
#endif
629
        sjcd_send_cmd(SCMD_GET_QINFO);
630
        s = sjcd_receive_status();
631
        if (s < 0 || sjcd_command_failed || !sjcd_status_valid) {
632
                sjcd_send_cmd(0xF2);
633
                s = sjcd_receive_status();
634
                if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
635
                        return (-1);
636
                sjcd_send_cmd(SCMD_GET_QINFO);
637
                s = sjcd_receive_status();
638
                if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
639
                        return (-1);
640
        }
641
        if (sjcd_media_is_available)
642
                if (sjcd_load_response(qp, sizeof(*qp)) == 0)
643
                        return (0);
644
        return (-1);
645
}
646
 
647
/*
648
 * Start playing from the specified position.
649
 */
650
static int sjcd_play(struct sjcd_play_msf *mp)
651
{
652
        struct sjcd_play_msf msf;
653
 
654
        /*
655
         * Turn the device to play mode.
656
         */
657
        sjcd_send_1_cmd(SCMD_SET_MODE, SCMD_MODE_PLAY);
658
        if (sjcd_receive_status() < 0)
659
                return (-1);
660
 
661
        /*
662
         * Seek to the starting point.
663
         */
664
        msf.start = mp->start;
665
        msf.end.min = msf.end.sec = msf.end.frame = 0x00;
666
        sjcd_send_6_cmd(SCMD_SEEK, &msf);
667
        if (sjcd_receive_status() < 0)
668
                return (-1);
669
 
670
        /*
671
         * Start playing.
672
         */
673
        sjcd_send_6_cmd(SCMD_PLAY, mp);
674
        return (sjcd_receive_status());
675
}
676
 
677
/*
678
 * Tray control functions.
679
 */
680
static int sjcd_tray_close(void)
681
{
682
#if defined( SJCD_TRACE )
683
        printk("SJCD: tray_close\n");
684
#endif
685
        sjcd_send_cmd(SCMD_CLOSE_TRAY);
686
        return (sjcd_receive_status());
687
}
688
 
689
static int sjcd_tray_lock(void)
690
{
691
#if defined( SJCD_TRACE )
692
        printk("SJCD: tray_lock\n");
693
#endif
694
        sjcd_send_cmd(SCMD_LOCK_TRAY);
695
        return (sjcd_receive_status());
696
}
697
 
698
static int sjcd_tray_unlock(void)
699
{
700
#if defined( SJCD_TRACE )
701
        printk("SJCD: tray_unlock\n");
702
#endif
703
        sjcd_send_cmd(SCMD_UNLOCK_TRAY);
704
        return (sjcd_receive_status());
705
}
706
 
707
static int sjcd_tray_open(void)
708
{
709
#if defined( SJCD_TRACE )
710
        printk("SJCD: tray_open\n");
711
#endif
712
        sjcd_send_cmd(SCMD_EJECT_TRAY);
713
        return (sjcd_receive_status());
714
}
715
 
716
/*
717
 * Do some user commands.
718
 */
719
static int sjcd_ioctl(struct inode *ip, struct file *fp,
720
                      unsigned int cmd, unsigned long arg)
721
{
722
#if defined( SJCD_TRACE )
723
        printk("SJCD:ioctl\n");
724
#endif
725
 
726
        if (ip == NULL)
727
                return (-EINVAL);
728
 
729
        sjcd_get_status();
730
        if (!sjcd_status_valid)
731
                return (-EIO);
732
        if (sjcd_update_toc() < 0)
733
                return (-EIO);
734
 
735
        switch (cmd) {
736
        case CDROMSTART:{
737
#if defined( SJCD_TRACE )
738
                        printk("SJCD: ioctl: start\n");
739
#endif
740
                        return (0);
741
                }
742
 
743
        case CDROMSTOP:{
744
#if defined( SJCD_TRACE )
745
                        printk("SJCD: ioctl: stop\n");
746
#endif
747
                        sjcd_send_cmd(SCMD_PAUSE);
748
                        (void) sjcd_receive_status();
749
                        sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
750
                        return (0);
751
                }
752
 
753
        case CDROMPAUSE:{
754
                        struct sjcd_hw_qinfo q_info;
755
#if defined( SJCD_TRACE )
756
                        printk("SJCD: ioctl: pause\n");
757
#endif
758
                        if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
759
                                sjcd_send_cmd(SCMD_PAUSE);
760
                                (void) sjcd_receive_status();
761
                                if (sjcd_get_q_info(&q_info) < 0) {
762
                                        sjcd_audio_status =
763
                                            CDROM_AUDIO_NO_STATUS;
764
                                } else {
765
                                        sjcd_audio_status =
766
                                            CDROM_AUDIO_PAUSED;
767
                                        sjcd_playing.start = q_info.abs;
768
                                }
769
                                return (0);
770
                        } else
771
                                return (-EINVAL);
772
                }
773
 
774
        case CDROMRESUME:{
775
#if defined( SJCD_TRACE )
776
                        printk("SJCD: ioctl: resume\n");
777
#endif
778
                        if (sjcd_audio_status == CDROM_AUDIO_PAUSED) {
779
                                /*
780
                                 * continue play starting at saved location
781
                                 */
782
                                if (sjcd_play(&sjcd_playing) < 0) {
783
                                        sjcd_audio_status =
784
                                            CDROM_AUDIO_ERROR;
785
                                        return (-EIO);
786
                                } else {
787
                                        sjcd_audio_status =
788
                                            CDROM_AUDIO_PLAY;
789
                                        return (0);
790
                                }
791
                        } else
792
                                return (-EINVAL);
793
                }
794
 
795
        case CDROMPLAYTRKIND:{
796
                        struct cdrom_ti ti;
797
                        int s;
798
#if defined( SJCD_TRACE )
799
                        printk("SJCD: ioctl: playtrkind\n");
800
#endif
801
                        if ((s =
802
                             verify_area(VERIFY_READ, (void *) arg,
803
                                         sizeof(ti))) == 0) {
804
                                copy_from_user(&ti, (void *) arg,
805
                                               sizeof(ti));
806
 
807
                                if (ti.cdti_trk0 < sjcd_first_track_no)
808
                                        return (-EINVAL);
809
                                if (ti.cdti_trk1 > sjcd_last_track_no)
810
                                        ti.cdti_trk1 = sjcd_last_track_no;
811
                                if (ti.cdti_trk0 > ti.cdti_trk1)
812
                                        return (-EINVAL);
813
 
814
                                sjcd_playing.start =
815
                                    sjcd_table_of_contents[ti.cdti_trk0].
816
                                    un.track_msf;
817
                                sjcd_playing.end =
818
                                    (ti.cdti_trk1 <
819
                                     sjcd_last_track_no) ?
820
                                    sjcd_table_of_contents[ti.cdti_trk1 +
821
                                                           1].un.
822
                                    track_msf : sjcd_table_of_contents[0].
823
                                    un.track_msf;
824
 
825
                                if (sjcd_play(&sjcd_playing) < 0) {
826
                                        sjcd_audio_status =
827
                                            CDROM_AUDIO_ERROR;
828
                                        return (-EIO);
829
                                } else
830
                                        sjcd_audio_status =
831
                                            CDROM_AUDIO_PLAY;
832
                        }
833
                        return (s);
834
                }
835
 
836
        case CDROMPLAYMSF:{
837
                        struct cdrom_msf sjcd_msf;
838
                        int s;
839
#if defined( SJCD_TRACE )
840
                        printk("SJCD: ioctl: playmsf\n");
841
#endif
842
                        if ((s =
843
                             verify_area(VERIFY_READ, (void *) arg,
844
                                         sizeof(sjcd_msf))) == 0) {
845
                                if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
846
                                        sjcd_send_cmd(SCMD_PAUSE);
847
                                        (void) sjcd_receive_status();
848
                                        sjcd_audio_status =
849
                                            CDROM_AUDIO_NO_STATUS;
850
                                }
851
 
852
                                copy_from_user(&sjcd_msf, (void *) arg,
853
                                               sizeof(sjcd_msf));
854
 
855
                                sjcd_playing.start.min =
856
                                    bin2bcd(sjcd_msf.cdmsf_min0);
857
                                sjcd_playing.start.sec =
858
                                    bin2bcd(sjcd_msf.cdmsf_sec0);
859
                                sjcd_playing.start.frame =
860
                                    bin2bcd(sjcd_msf.cdmsf_frame0);
861
                                sjcd_playing.end.min =
862
                                    bin2bcd(sjcd_msf.cdmsf_min1);
863
                                sjcd_playing.end.sec =
864
                                    bin2bcd(sjcd_msf.cdmsf_sec1);
865
                                sjcd_playing.end.frame =
866
                                    bin2bcd(sjcd_msf.cdmsf_frame1);
867
 
868
                                if (sjcd_play(&sjcd_playing) < 0) {
869
                                        sjcd_audio_status =
870
                                            CDROM_AUDIO_ERROR;
871
                                        return (-EIO);
872
                                } else
873
                                        sjcd_audio_status =
874
                                            CDROM_AUDIO_PLAY;
875
                        }
876
                        return (s);
877
                }
878
 
879
        case CDROMREADTOCHDR:{
880
                        struct cdrom_tochdr toc_header;
881
                        int s;
882
#if defined (SJCD_TRACE )
883
                        printk("SJCD: ioctl: readtocheader\n");
884
#endif
885
                        if ((s =
886
                             verify_area(VERIFY_WRITE, (void *) arg,
887
                                         sizeof(toc_header))) == 0) {
888
                                toc_header.cdth_trk0 = sjcd_first_track_no;
889
                                toc_header.cdth_trk1 = sjcd_last_track_no;
890
                                copy_to_user((void *) arg, &toc_header,
891
                                             sizeof(toc_header));
892
                        }
893
                        return (s);
894
                }
895
 
896
        case CDROMREADTOCENTRY:{
897
                        struct cdrom_tocentry toc_entry;
898
                        int s;
899
#if defined( SJCD_TRACE )
900
                        printk("SJCD: ioctl: readtocentry\n");
901
#endif
902
                        if ((s =
903
                             verify_area(VERIFY_WRITE, (void *) arg,
904
                                         sizeof(toc_entry))) == 0) {
905
                                struct sjcd_hw_disk_info *tp;
906
 
907
                                copy_from_user(&toc_entry, (void *) arg,
908
                                               sizeof(toc_entry));
909
 
910
                                if (toc_entry.cdte_track == CDROM_LEADOUT)
911
                                        tp = &sjcd_table_of_contents[0];
912
                                else if (toc_entry.cdte_track <
913
                                         sjcd_first_track_no)
914
                                        return (-EINVAL);
915
                                else if (toc_entry.cdte_track >
916
                                         sjcd_last_track_no)
917
                                        return (-EINVAL);
918
                                else
919
                                        tp = &sjcd_table_of_contents
920
                                            [toc_entry.cdte_track];
921
 
922
                                toc_entry.cdte_adr =
923
                                    tp->track_control & 0x0F;
924
                                toc_entry.cdte_ctrl =
925
                                    tp->track_control >> 4;
926
 
927
                                switch (toc_entry.cdte_format) {
928
                                case CDROM_LBA:
929
                                        toc_entry.cdte_addr.lba =
930
                                            msf2hsg(&(tp->un.track_msf));
931
                                        break;
932
                                case CDROM_MSF:
933
                                        toc_entry.cdte_addr.msf.minute =
934
                                            bcd2bin(tp->un.track_msf.min);
935
                                        toc_entry.cdte_addr.msf.second =
936
                                            bcd2bin(tp->un.track_msf.sec);
937
                                        toc_entry.cdte_addr.msf.frame =
938
                                            bcd2bin(tp->un.track_msf.
939
                                                    frame);
940
                                        break;
941
                                default:
942
                                        return (-EINVAL);
943
                                }
944
                                copy_to_user((void *) arg, &toc_entry,
945
                                             sizeof(toc_entry));
946
                        }
947
                        return (s);
948
                }
949
 
950
        case CDROMSUBCHNL:{
951
                        struct cdrom_subchnl subchnl;
952
                        int s;
953
#if defined( SJCD_TRACE )
954
                        printk("SJCD: ioctl: subchnl\n");
955
#endif
956
                        if ((s =
957
                             verify_area(VERIFY_WRITE, (void *) arg,
958
                                         sizeof(subchnl))) == 0) {
959
                                struct sjcd_hw_qinfo q_info;
960
 
961
                                copy_from_user(&subchnl, (void *) arg,
962
                                               sizeof(subchnl));
963
                                if (sjcd_get_q_info(&q_info) < 0)
964
                                        return (-EIO);
965
 
966
                                subchnl.cdsc_audiostatus =
967
                                    sjcd_audio_status;
968
                                subchnl.cdsc_adr =
969
                                    q_info.track_control & 0x0F;
970
                                subchnl.cdsc_ctrl =
971
                                    q_info.track_control >> 4;
972
                                subchnl.cdsc_trk =
973
                                    bcd2bin(q_info.track_no);
974
                                subchnl.cdsc_ind = bcd2bin(q_info.x);
975
 
976
                                switch (subchnl.cdsc_format) {
977
                                case CDROM_LBA:
978
                                        subchnl.cdsc_absaddr.lba =
979
                                            msf2hsg(&(q_info.abs));
980
                                        subchnl.cdsc_reladdr.lba =
981
                                            msf2hsg(&(q_info.rel));
982
                                        break;
983
                                case CDROM_MSF:
984
                                        subchnl.cdsc_absaddr.msf.minute =
985
                                            bcd2bin(q_info.abs.min);
986
                                        subchnl.cdsc_absaddr.msf.second =
987
                                            bcd2bin(q_info.abs.sec);
988
                                        subchnl.cdsc_absaddr.msf.frame =
989
                                            bcd2bin(q_info.abs.frame);
990
                                        subchnl.cdsc_reladdr.msf.minute =
991
                                            bcd2bin(q_info.rel.min);
992
                                        subchnl.cdsc_reladdr.msf.second =
993
                                            bcd2bin(q_info.rel.sec);
994
                                        subchnl.cdsc_reladdr.msf.frame =
995
                                            bcd2bin(q_info.rel.frame);
996
                                        break;
997
                                default:
998
                                        return (-EINVAL);
999
                                }
1000
                                copy_to_user((void *) arg, &subchnl,
1001
                                             sizeof(subchnl));
1002
                        }
1003
                        return (s);
1004
                }
1005
 
1006
        case CDROMVOLCTRL:{
1007
                        struct cdrom_volctrl vol_ctrl;
1008
                        int s;
1009
#if defined( SJCD_TRACE )
1010
                        printk("SJCD: ioctl: volctrl\n");
1011
#endif
1012
                        if ((s =
1013
                             verify_area(VERIFY_READ, (void *) arg,
1014
                                         sizeof(vol_ctrl))) == 0) {
1015
                                unsigned char dummy[4];
1016
 
1017
                                copy_from_user(&vol_ctrl, (void *) arg,
1018
                                               sizeof(vol_ctrl));
1019
                                sjcd_send_4_cmd(SCMD_SET_VOLUME,
1020
                                                vol_ctrl.channel0, 0xFF,
1021
                                                vol_ctrl.channel1, 0xFF);
1022
                                if (sjcd_receive_status() < 0)
1023
                                        return (-EIO);
1024
                                (void) sjcd_load_response(dummy, 4);
1025
                        }
1026
                        return (s);
1027
                }
1028
 
1029
        case CDROMEJECT:{
1030
#if defined( SJCD_TRACE )
1031
                        printk("SJCD: ioctl: eject\n");
1032
#endif
1033
                        if (!sjcd_command_is_in_progress) {
1034
                                sjcd_tray_unlock();
1035
                                sjcd_send_cmd(SCMD_EJECT_TRAY);
1036
                                (void) sjcd_receive_status();
1037
                        }
1038
                        return (0);
1039
                }
1040
 
1041
#if defined( SJCD_GATHER_STAT )
1042
        case 0xABCD:{
1043
                        int s;
1044
#if defined( SJCD_TRACE )
1045
                        printk("SJCD: ioctl: statistic\n");
1046
#endif
1047
                        if ((s =
1048
                             verify_area(VERIFY_WRITE, (void *) arg,
1049
                                         sizeof(statistic))) == 0)
1050
                                copy_to_user((void *) arg, &statistic,
1051
                                             sizeof(statistic));
1052
                        return (s);
1053
                }
1054
#endif
1055
 
1056
        default:
1057
                return (-EINVAL);
1058
        }
1059
}
1060
 
1061
/*
1062
 * Invalidate internal buffers of the driver.
1063
 */
1064
static void sjcd_invalidate_buffers(void)
1065
{
1066
        int i;
1067
        for (i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[i++] = -1);
1068
        sjcd_buf_out = -1;
1069
}
1070
 
1071
/*
1072
 * Take care of the different block sizes between cdrom and Linux.
1073
 * When Linux gets variable block sizes this will probably go away.
1074
 */
1075
 
1076
#define CURRENT_IS_VALID                                      \
1077
    ( !QUEUE_EMPTY && MAJOR( CURRENT->rq_dev ) == MAJOR_NR && \
1078
      CURRENT->cmd == READ && CURRENT->sector != -1 )
1079
 
1080
static void sjcd_transfer(void)
1081
{
1082
#if defined( SJCD_TRACE )
1083
        printk("SJCD: transfer:\n");
1084
#endif
1085
        if (CURRENT_IS_VALID) {
1086
                while (CURRENT->nr_sectors) {
1087
                        int i, bn = CURRENT->sector / 4;
1088
                        for (i = 0;
1089
                             i < SJCD_BUF_SIZ && sjcd_buf_bn[i] != bn;
1090
                             i++);
1091
                        if (i < SJCD_BUF_SIZ) {
1092
                                int offs =
1093
                                    (i * 4 + (CURRENT->sector & 3)) * 512;
1094
                                int nr_sectors = 4 - (CURRENT->sector & 3);
1095
                                if (sjcd_buf_out != i) {
1096
                                        sjcd_buf_out = i;
1097
                                        if (sjcd_buf_bn[i] != bn) {
1098
                                                sjcd_buf_out = -1;
1099
                                                continue;
1100
                                        }
1101
                                }
1102
                                if (nr_sectors > CURRENT->nr_sectors)
1103
                                        nr_sectors = CURRENT->nr_sectors;
1104
#if defined( SJCD_TRACE )
1105
                                printk("SJCD: copy out\n");
1106
#endif
1107
                                memcpy(CURRENT->buffer, sjcd_buf + offs,
1108
                                       nr_sectors * 512);
1109
                                CURRENT->nr_sectors -= nr_sectors;
1110
                                CURRENT->sector += nr_sectors;
1111
                                CURRENT->buffer += nr_sectors * 512;
1112
                        } else {
1113
                                sjcd_buf_out = -1;
1114
                                break;
1115
                        }
1116
                }
1117
        }
1118
#if defined( SJCD_TRACE )
1119
        printk("SJCD: transfer: done\n");
1120
#endif
1121
}
1122
 
1123
static void sjcd_poll(void)
1124
{
1125
#if defined( SJCD_GATHER_STAT )
1126
        /*
1127
         * Update total number of ticks.
1128
         */
1129
        statistic.ticks++;
1130
        statistic.tticks[sjcd_transfer_state]++;
1131
#endif
1132
 
1133
      ReSwitch:switch (sjcd_transfer_state) {
1134
 
1135
        case SJCD_S_IDLE:{
1136
#if defined( SJCD_GATHER_STAT )
1137
                        statistic.idle_ticks++;
1138
#endif
1139
#if defined( SJCD_TRACE )
1140
                        printk("SJCD_S_IDLE\n");
1141
#endif
1142
                        return;
1143
                }
1144
 
1145
        case SJCD_S_START:{
1146
#if defined( SJCD_GATHER_STAT )
1147
                        statistic.start_ticks++;
1148
#endif
1149
                        sjcd_send_cmd(SCMD_GET_STATUS);
1150
                        sjcd_transfer_state =
1151
                            sjcd_mode ==
1152
                            SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1153
                        sjcd_transfer_timeout = 500;
1154
#if defined( SJCD_TRACE )
1155
                        printk("SJCD_S_START: goto SJCD_S_%s mode\n",
1156
                               sjcd_transfer_state ==
1157
                               SJCD_S_READ ? "READ" : "MODE");
1158
#endif
1159
                        break;
1160
                }
1161
 
1162
        case SJCD_S_MODE:{
1163
                        if (sjcd_check_status()) {
1164
                                /*
1165
                                 * Previous command is completed.
1166
                                 */
1167
                                if (!sjcd_status_valid
1168
                                    || sjcd_command_failed) {
1169
#if defined( SJCD_TRACE )
1170
                                        printk
1171
                                            ("SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1172
#endif
1173
                                        sjcd_transfer_state = SJCD_S_STOP;
1174
                                        goto ReSwitch;
1175
                                }
1176
 
1177
                                sjcd_mode = 0;   /* unknown mode; should not be valid when failed */
1178
                                sjcd_send_1_cmd(SCMD_SET_MODE,
1179
                                                SCMD_MODE_COOKED);
1180
                                sjcd_transfer_state = SJCD_S_READ;
1181
                                sjcd_transfer_timeout = 1000;
1182
#if defined( SJCD_TRACE )
1183
                                printk
1184
                                    ("SJCD_S_MODE: goto SJCD_S_READ mode\n");
1185
#endif
1186
                        }
1187
#if defined( SJCD_GATHER_STAT )
1188
                        else
1189
                                statistic.mode_ticks++;
1190
#endif
1191
                        break;
1192
                }
1193
 
1194
        case SJCD_S_READ:{
1195
                        if (sjcd_status_valid ? 1 : sjcd_check_status()) {
1196
                                /*
1197
                                 * Previous command is completed.
1198
                                 */
1199
                                if (!sjcd_status_valid
1200
                                    || sjcd_command_failed) {
1201
#if defined( SJCD_TRACE )
1202
                                        printk
1203
                                            ("SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1204
#endif
1205
                                        sjcd_transfer_state = SJCD_S_STOP;
1206
                                        goto ReSwitch;
1207
                                }
1208
                                if (!sjcd_media_is_available) {
1209
#if defined( SJCD_TRACE )
1210
                                        printk
1211
                                            ("SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n");
1212
#endif
1213
                                        sjcd_transfer_state = SJCD_S_STOP;
1214
                                        goto ReSwitch;
1215
                                }
1216
                                if (sjcd_mode != SCMD_MODE_COOKED) {
1217
                                        /*
1218
                                         * We seem to come from set mode. So discard one byte of result.
1219
                                         */
1220
                                        if (sjcd_load_response
1221
                                            (&sjcd_mode, 1) != 0) {
1222
#if defined( SJCD_TRACE )
1223
                                                printk
1224
                                                    ("SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n");
1225
#endif
1226
                                                sjcd_transfer_state =
1227
                                                    SJCD_S_STOP;
1228
                                                goto ReSwitch;
1229
                                        }
1230
                                        if (sjcd_mode != SCMD_MODE_COOKED) {
1231
#if defined( SJCD_TRACE )
1232
                                                printk
1233
                                                    ("SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n");
1234
#endif
1235
                                                sjcd_transfer_state =
1236
                                                    SJCD_S_STOP;
1237
                                                goto ReSwitch;
1238
                                        }
1239
                                }
1240
 
1241
                                if (CURRENT_IS_VALID) {
1242
                                        struct sjcd_play_msf msf;
1243
 
1244
                                        sjcd_next_bn = CURRENT->sector / 4;
1245
                                        hsg2msf(sjcd_next_bn, &msf.start);
1246
                                        msf.end.min = 0;
1247
                                        msf.end.sec = 0;
1248
                                        msf.end.frame = sjcd_read_count =
1249
                                            SJCD_BUF_SIZ;
1250
#if defined( SJCD_TRACE )
1251
                                        printk
1252
                                            ("SJCD: ---reading msf-address %x:%x:%x  %x:%x:%x\n",
1253
                                             msf.start.min, msf.start.sec,
1254
                                             msf.start.frame, msf.end.min,
1255
                                             msf.end.sec, msf.end.frame);
1256
                                        printk
1257
                                            ("sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n",
1258
                                             sjcd_next_bn, sjcd_buf_in,
1259
                                             sjcd_buf_out,
1260
                                             sjcd_buf_bn[sjcd_buf_in]);
1261
#endif
1262
                                        sjcd_send_6_cmd(SCMD_DATA_READ,
1263
                                                        &msf);
1264
                                        sjcd_transfer_state = SJCD_S_DATA;
1265
                                        sjcd_transfer_timeout = 500;
1266
#if defined( SJCD_TRACE )
1267
                                        printk
1268
                                            ("SJCD_S_READ: go to SJCD_S_DATA mode\n");
1269
#endif
1270
                                } else {
1271
#if defined( SJCD_TRACE )
1272
                                        printk
1273
                                            ("SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n");
1274
#endif
1275
                                        sjcd_transfer_state = SJCD_S_STOP;
1276
                                        goto ReSwitch;
1277
                                }
1278
                        }
1279
#if defined( SJCD_GATHER_STAT )
1280
                        else
1281
                                statistic.read_ticks++;
1282
#endif
1283
                        break;
1284
                }
1285
 
1286
        case SJCD_S_DATA:{
1287
                        unsigned char stat;
1288
 
1289
                      sjcd_s_data:stat =
1290
                            inb(SJCDPORT
1291
                                (1));
1292
#if defined( SJCD_TRACE )
1293
                        printk("SJCD_S_DATA: status = 0x%02x\n", stat);
1294
#endif
1295
                        if (SJCD_STATUS_AVAILABLE(stat)) {
1296
                                /*
1297
                                 * No data is waiting for us in the drive buffer. Status of operation
1298
                                 * completion is available. Read and parse it.
1299
                                 */
1300
                                sjcd_load_status();
1301
 
1302
                                if (!sjcd_status_valid
1303
                                    || sjcd_command_failed) {
1304
#if defined( SJCD_TRACE )
1305
                                        printk
1306
                                            ("SJCD: read block %d failed, maybe audio disk? Giving up\n",
1307
                                             sjcd_next_bn);
1308
#endif
1309
                                        if (CURRENT_IS_VALID)
1310
                                                end_request(0);
1311
#if defined( SJCD_TRACE )
1312
                                        printk
1313
                                            ("SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n");
1314
#endif
1315
                                        sjcd_transfer_state = SJCD_S_STOP;
1316
                                        goto ReSwitch;
1317
                                }
1318
 
1319
                                if (!sjcd_media_is_available) {
1320
                                        printk
1321
                                            ("SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n");
1322
                                        sjcd_transfer_state = SJCD_S_STOP;
1323
                                        goto ReSwitch;
1324
                                }
1325
 
1326
                                sjcd_transfer_state = SJCD_S_READ;
1327
                                goto ReSwitch;
1328
                        } else if (SJCD_DATA_AVAILABLE(stat)) {
1329
                                /*
1330
                                 * One frame is read into device buffer. We must copy it to our memory.
1331
                                 * Otherwise cdrom hangs up. Check to see if we have something to copy
1332
                                 * to.
1333
                                 */
1334
                                if (!CURRENT_IS_VALID
1335
                                    && sjcd_buf_in == sjcd_buf_out) {
1336
#if defined( SJCD_TRACE )
1337
                                        printk
1338
                                            ("SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n");
1339
                                        printk
1340
                                            (" ... all the date would be discarded\n");
1341
#endif
1342
                                        sjcd_transfer_state = SJCD_S_STOP;
1343
                                        goto ReSwitch;
1344
                                }
1345
 
1346
                                /*
1347
                                 * Everything seems to be OK. Just read the frame and recalculate
1348
                                 * indices.
1349
                                 */
1350
                                sjcd_buf_bn[sjcd_buf_in] = -1;  /* ??? */
1351
                                insb(SJCDPORT(2),
1352
                                     sjcd_buf + 2048 * sjcd_buf_in, 2048);
1353
#if defined( SJCD_TRACE )
1354
                                printk
1355
                                    ("SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1356
                                     sjcd_next_bn, sjcd_buf_in,
1357
                                     sjcd_buf_out,
1358
                                     sjcd_buf_bn[sjcd_buf_in]);
1359
#endif
1360
                                sjcd_buf_bn[sjcd_buf_in] = sjcd_next_bn++;
1361
                                if (sjcd_buf_out == -1)
1362
                                        sjcd_buf_out = sjcd_buf_in;
1363
                                if (++sjcd_buf_in == SJCD_BUF_SIZ)
1364
                                        sjcd_buf_in = 0;
1365
 
1366
                                /*
1367
                                 * Only one frame is ready at time. So we should turn over to wait for
1368
                                 * another frame. If we need that, of course.
1369
                                 */
1370
                                if (--sjcd_read_count == 0) {
1371
                                        /*
1372
                                         * OK, request seems to be precessed. Continue transferring...
1373
                                         */
1374
                                        if (!sjcd_transfer_is_active) {
1375
                                                while (CURRENT_IS_VALID) {
1376
                                                        /*
1377
                                                         * Continue transferring.
1378
                                                         */
1379
                                                        sjcd_transfer();
1380
                                                        if (CURRENT->
1381
                                                            nr_sectors ==
1382
                                                            0)
1383
                                                                end_request
1384
                                                                    (1);
1385
                                                        else
1386
                                                                break;
1387
                                                }
1388
                                        }
1389
                                        if (CURRENT_IS_VALID &&
1390
                                            (CURRENT->sector / 4 <
1391
                                             sjcd_next_bn
1392
                                             || CURRENT->sector / 4 >
1393
                                             sjcd_next_bn +
1394
                                             SJCD_BUF_SIZ)) {
1395
#if defined( SJCD_TRACE )
1396
                                                printk
1397
                                                    ("SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n");
1398
#endif
1399
                                                sjcd_transfer_state =
1400
                                                    SJCD_S_STOP;
1401
                                                goto ReSwitch;
1402
                                        }
1403
                                }
1404
                                /*
1405
                                 * Now we should turn around rather than wait for while.
1406
                                 */
1407
                                goto sjcd_s_data;
1408
                        }
1409
#if defined( SJCD_GATHER_STAT )
1410
                        else
1411
                                statistic.data_ticks++;
1412
#endif
1413
                        break;
1414
                }
1415
 
1416
        case SJCD_S_STOP:{
1417
                        sjcd_read_count = 0;
1418
                        sjcd_send_cmd(SCMD_STOP);
1419
                        sjcd_transfer_state = SJCD_S_STOPPING;
1420
                        sjcd_transfer_timeout = 500;
1421
#if defined( SJCD_GATHER_STAT )
1422
                        statistic.stop_ticks++;
1423
#endif
1424
                        break;
1425
                }
1426
 
1427
        case SJCD_S_STOPPING:{
1428
                        unsigned char stat;
1429
 
1430
                        stat = inb(SJCDPORT(1));
1431
#if defined( SJCD_TRACE )
1432
                        printk("SJCD_S_STOP: status = 0x%02x\n", stat);
1433
#endif
1434
                        if (SJCD_DATA_AVAILABLE(stat)) {
1435
                                int i;
1436
#if defined( SJCD_TRACE )
1437
                                printk("SJCD_S_STOP: discard data\n");
1438
#endif
1439
                                /*
1440
                                 * Discard all the data from the pipe. Foolish method.
1441
                                 */
1442
                                for (i = 2048; i--;
1443
                                     (void) inb(SJCDPORT(2)));
1444
                                sjcd_transfer_timeout = 500;
1445
                        } else if (SJCD_STATUS_AVAILABLE(stat)) {
1446
                                sjcd_load_status();
1447
                                if (sjcd_status_valid
1448
                                    && sjcd_media_is_changed) {
1449
                                        sjcd_toc_uptodate = 0;
1450
                                        sjcd_invalidate_buffers();
1451
                                }
1452
                                if (CURRENT_IS_VALID) {
1453
                                        if (sjcd_status_valid)
1454
                                                sjcd_transfer_state =
1455
                                                    SJCD_S_READ;
1456
                                        else
1457
                                                sjcd_transfer_state =
1458
                                                    SJCD_S_START;
1459
                                } else
1460
                                        sjcd_transfer_state = SJCD_S_IDLE;
1461
                                goto ReSwitch;
1462
                        }
1463
#if defined( SJCD_GATHER_STAT )
1464
                        else
1465
                                statistic.stopping_ticks++;
1466
#endif
1467
                        break;
1468
                }
1469
 
1470
        default:
1471
                printk("SJCD: poll: invalid state %d\n",
1472
                       sjcd_transfer_state);
1473
                return;
1474
        }
1475
 
1476
        if (--sjcd_transfer_timeout == 0) {
1477
                printk("SJCD: timeout in state %d\n", sjcd_transfer_state);
1478
                while (CURRENT_IS_VALID)
1479
                        end_request(0);
1480
                sjcd_send_cmd(SCMD_STOP);
1481
                sjcd_transfer_state = SJCD_S_IDLE;
1482
                goto ReSwitch;
1483
        }
1484
 
1485
        /*
1486
         * Get back in some time. 1 should be replaced with count variable to
1487
         * avoid unnecessary testings.
1488
         */
1489
        SJCD_SET_TIMER(sjcd_poll, 1);
1490
}
1491
 
1492
static void do_sjcd_request(request_queue_t * q)
1493
{
1494
#if defined( SJCD_TRACE )
1495
        printk("SJCD: do_sjcd_request(%ld+%ld)\n",
1496
               CURRENT->sector, CURRENT->nr_sectors);
1497
#endif
1498
        sjcd_transfer_is_active = 1;
1499
        while (CURRENT_IS_VALID) {
1500
                /*
1501
                 * Who of us are paranoiac?
1502
                 */
1503
                if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1504
                        panic(DEVICE_NAME ": block not locked");
1505
 
1506
                sjcd_transfer();
1507
                if (CURRENT->nr_sectors == 0)
1508
                        end_request(1);
1509
                else {
1510
                        sjcd_buf_out = -1;      /* Want to read a block not in buffer */
1511
                        if (sjcd_transfer_state == SJCD_S_IDLE) {
1512
                                if (!sjcd_toc_uptodate) {
1513
                                        if (sjcd_update_toc() < 0) {
1514
                                                printk
1515
                                                    ("SJCD: transfer: discard\n");
1516
                                                while (CURRENT_IS_VALID)
1517
                                                        end_request(0);
1518
                                                break;
1519
                                        }
1520
                                }
1521
                                sjcd_transfer_state = SJCD_S_START;
1522
                                SJCD_SET_TIMER(sjcd_poll, HZ / 100);
1523
                        }
1524
                        break;
1525
                }
1526
        }
1527
        sjcd_transfer_is_active = 0;
1528
#if defined( SJCD_TRACE )
1529
        printk
1530
            ("sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1531
             sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1532
             sjcd_buf_bn[sjcd_buf_in]);
1533
        printk("do_sjcd_request ends\n");
1534
#endif
1535
}
1536
 
1537
/*
1538
 * Open the device special file. Check disk is in.
1539
 */
1540
int sjcd_open(struct inode *ip, struct file *fp)
1541
{
1542
        /*
1543
         * Check the presence of device.
1544
         */
1545
        if (!sjcd_present)
1546
                return (-ENXIO);
1547
 
1548
        /*
1549
         * Only read operations are allowed. Really? (:-)
1550
         */
1551
        if (fp->f_mode & 2)
1552
                return (-EROFS);
1553
 
1554
        if (sjcd_open_count == 0) {
1555
                int s, sjcd_open_tries;
1556
/* We don't know that, do we? */
1557
/*
1558
    sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1559
*/
1560
                sjcd_mode = 0;
1561
                sjcd_door_was_open = 0;
1562
                sjcd_transfer_state = SJCD_S_IDLE;
1563
                sjcd_invalidate_buffers();
1564
                sjcd_status_valid = 0;
1565
 
1566
                /*
1567
                 * Strict status checking.
1568
                 */
1569
                for (sjcd_open_tries = 4; --sjcd_open_tries;) {
1570
                        if (!sjcd_status_valid)
1571
                                sjcd_get_status();
1572
                        if (!sjcd_status_valid) {
1573
#if defined( SJCD_DIAGNOSTIC )
1574
                                printk
1575
                                    ("SJCD: open: timed out when check status.\n");
1576
#endif
1577
                                goto err_out;
1578
                        } else if (!sjcd_media_is_available) {
1579
#if defined( SJCD_DIAGNOSTIC )
1580
                                printk("SJCD: open: no disk in drive\n");
1581
#endif
1582
                                if (!sjcd_door_closed) {
1583
                                        sjcd_door_was_open = 1;
1584
#if defined( SJCD_TRACE )
1585
                                        printk
1586
                                            ("SJCD: open: close the tray\n");
1587
#endif
1588
                                        s = sjcd_tray_close();
1589
                                        if (s < 0 || !sjcd_status_valid
1590
                                            || sjcd_command_failed) {
1591
#if defined( SJCD_DIAGNOSTIC )
1592
                                                printk
1593
                                                    ("SJCD: open: tray close attempt failed\n");
1594
#endif
1595
                                                goto err_out;
1596
                                        }
1597
                                        continue;
1598
                                } else
1599
                                        goto err_out;
1600
                        }
1601
                        break;
1602
                }
1603
                s = sjcd_tray_lock();
1604
                if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1605
#if defined( SJCD_DIAGNOSTIC )
1606
                        printk("SJCD: open: tray lock attempt failed\n");
1607
#endif
1608
                        goto err_out;
1609
                }
1610
#if defined( SJCD_TRACE )
1611
                printk("SJCD: open: done\n");
1612
#endif
1613
        }
1614
 
1615
        ++sjcd_open_count;
1616
        return (0);
1617
 
1618
      err_out:
1619
        return (-EIO);
1620
}
1621
 
1622
/*
1623
 * On close, we flush all sjcd blocks from the buffer cache.
1624
 */
1625
static int sjcd_release(struct inode *inode, struct file *file)
1626
{
1627
        int s;
1628
 
1629
#if defined( SJCD_TRACE )
1630
        printk("SJCD: release\n");
1631
#endif
1632
        if (--sjcd_open_count == 0) {
1633
                sjcd_invalidate_buffers();
1634
                s = sjcd_tray_unlock();
1635
                if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1636
#if defined( SJCD_DIAGNOSTIC )
1637
                        printk
1638
                            ("SJCD: release: tray unlock attempt failed.\n");
1639
#endif
1640
                }
1641
                if (sjcd_door_was_open) {
1642
                        s = sjcd_tray_open();
1643
                        if (s < 0 || !sjcd_status_valid
1644
                            || sjcd_command_failed) {
1645
#if defined( SJCD_DIAGNOSTIC )
1646
                                printk
1647
                                    ("SJCD: release: tray unload attempt failed.\n");
1648
#endif
1649
                        }
1650
                }
1651
        }
1652
        return 0;
1653
}
1654
 
1655
/*
1656
 * A list of file operations allowed for this cdrom.
1657
 */
1658
static struct block_device_operations sjcd_fops = {
1659
        owner:THIS_MODULE,
1660
        open:sjcd_open,
1661
        release:sjcd_release,
1662
        ioctl:sjcd_ioctl,
1663
        check_media_change:sjcd_disk_change,
1664
};
1665
 
1666
static int blksize = 2048;
1667
static int secsize = 2048;
1668
 
1669
/*
1670
 * Following stuff is intended for initialization of the cdrom. It
1671
 * first looks for presence of device. If the device is present, it
1672
 * will be reset. Then read the version of the drive and load status.
1673
 * The version is two BCD-coded bytes.
1674
 */
1675
static struct {
1676
        unsigned char major, minor;
1677
} sjcd_version;
1678
 
1679
/*
1680
 * Test for presence of drive and initialize it. Called at boot time.
1681
 * Probe cdrom, find out version and status.
1682
 */
1683
int __init sjcd_init(void)
1684
{
1685
        int i;
1686
 
1687
        printk(KERN_INFO
1688
               "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n",
1689
               SJCD_VERSION_MAJOR, SJCD_VERSION_MINOR);
1690
 
1691
#if defined( SJCD_TRACE )
1692
        printk("SJCD: sjcd=0x%x: ", sjcd_base);
1693
#endif
1694
 
1695
        hardsect_size[MAJOR_NR] = &secsize;
1696
        blksize_size[MAJOR_NR] = &blksize;
1697
 
1698
        if (devfs_register_blkdev(MAJOR_NR, "sjcd", &sjcd_fops) != 0) {
1699
                printk("SJCD: Unable to get major %d for Sanyo CD-ROM\n",
1700
                       MAJOR_NR);
1701
                return (-EIO);
1702
        }
1703
 
1704
        blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1705
        read_ahead[MAJOR_NR] = 4;
1706
        register_disk(NULL, MKDEV(MAJOR_NR, 0), 1, &sjcd_fops, 0);
1707
 
1708
        if (check_region(sjcd_base, 4)) {
1709
                printk
1710
                    ("SJCD: Init failed, I/O port (%X) is already in use\n",
1711
                     sjcd_base);
1712
                sjcd_cleanup();
1713
                return (-EIO);
1714
        }
1715
 
1716
        /*
1717
         * Check for card. Since we are booting now, we can't use standard
1718
         * wait algorithm.
1719
         */
1720
        printk(KERN_INFO "SJCD: Resetting: ");
1721
        sjcd_send_cmd(SCMD_RESET);
1722
        for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1723
                unsigned long timer;
1724
 
1725
                /*
1726
                 * Wait 10ms approx.
1727
                 */
1728
                for (timer = jiffies; time_before_eq(jiffies, timer););
1729
                if ((i % 100) == 0)
1730
                        printk(".");
1731
                (void) sjcd_check_status();
1732
        }
1733
        if (i == 0 || sjcd_command_failed) {
1734
                printk(" reset failed, no drive found.\n");
1735
                sjcd_cleanup();
1736
                return (-EIO);
1737
        } else
1738
                printk("\n");
1739
 
1740
        /*
1741
         * Get and print out cdrom version.
1742
         */
1743
        printk(KERN_INFO "SJCD: Getting version: ");
1744
        sjcd_send_cmd(SCMD_GET_VERSION);
1745
        for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1746
                unsigned long timer;
1747
 
1748
                /*
1749
                 * Wait 10ms approx.
1750
                 */
1751
                for (timer = jiffies; time_before_eq(jiffies, timer););
1752
                if ((i % 100) == 0)
1753
                        printk(".");
1754
                (void) sjcd_check_status();
1755
        }
1756
        if (i == 0 || sjcd_command_failed) {
1757
                printk(" get version failed, no drive found.\n");
1758
                sjcd_cleanup();
1759
                return (-EIO);
1760
        }
1761
 
1762
        if (sjcd_load_response(&sjcd_version, sizeof(sjcd_version)) == 0) {
1763
                printk(" %1x.%02x\n", (int) sjcd_version.major,
1764
                       (int) sjcd_version.minor);
1765
        } else {
1766
                printk(" read version failed, no drive found.\n");
1767
                sjcd_cleanup();
1768
                return (-EIO);
1769
        }
1770
 
1771
        /*
1772
         * Check and print out the tray state. (if it is needed?).
1773
         */
1774
        if (!sjcd_status_valid) {
1775
                printk(KERN_INFO "SJCD: Getting status: ");
1776
                sjcd_send_cmd(SCMD_GET_STATUS);
1777
                for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1778
                        unsigned long timer;
1779
 
1780
                        /*
1781
                         * Wait 10ms approx.
1782
                         */
1783
                        for (timer = jiffies;
1784
                             time_before_eq(jiffies, timer););
1785
                        if ((i % 100) == 0)
1786
                                printk(".");
1787
                        (void) sjcd_check_status();
1788
                }
1789
                if (i == 0 || sjcd_command_failed) {
1790
                        printk(" get status failed, no drive found.\n");
1791
                        sjcd_cleanup();
1792
                        return (-EIO);
1793
                } else
1794
                        printk("\n");
1795
        }
1796
 
1797
        printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1798
        devfs_register(NULL, "sjcd", DEVFS_FL_DEFAULT, MAJOR_NR, 0,
1799
                       S_IFBLK | S_IRUGO | S_IWUGO, &sjcd_fops, NULL);
1800
 
1801
        sjcd_present++;
1802
        return (0);
1803
}
1804
 
1805
static int sjcd_cleanup(void)
1806
{
1807
        if ((devfs_unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1808
                printk("SJCD: cannot unregister device.\n");
1809
        else {
1810
                release_region(sjcd_base, 4);
1811
                blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1812
        }
1813
 
1814
        return (0);
1815
}
1816
 
1817
 
1818
void __exit sjcd_exit(void)
1819
{
1820
        devfs_unregister(devfs_find_handle
1821
                         (NULL, "sjcd", 0, 0, DEVFS_SPECIAL_BLK, 0));
1822
        if (sjcd_cleanup())
1823
                printk("SJCD: module: cannot be removed.\n");
1824
        else
1825
                printk(KERN_INFO "SJCD: module: removed.\n");
1826
}
1827
 
1828
#ifdef MODULE
1829
module_init(sjcd_init);
1830
#endif
1831
module_exit(sjcd_exit);
1832
 
1833
 
1834
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.