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