commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.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                                 copy_from_user(&sjcd_msf, (void *) arg,
846                                                sizeof(sjcd_msf));
847
848                                 sjcd_playing.start.min =
849                                     bin2bcd(sjcd_msf.cdmsf_min0);
850                                 sjcd_playing.start.sec =
851                                     bin2bcd(sjcd_msf.cdmsf_sec0);
852                                 sjcd_playing.start.frame =
853                                     bin2bcd(sjcd_msf.cdmsf_frame0);
854                                 sjcd_playing.end.min =
855                                     bin2bcd(sjcd_msf.cdmsf_min1);
856                                 sjcd_playing.end.sec =
857                                     bin2bcd(sjcd_msf.cdmsf_sec1);
858                                 sjcd_playing.end.frame =
859                                     bin2bcd(sjcd_msf.cdmsf_frame1);
860
861                                 if (sjcd_play(&sjcd_playing) < 0) {
862                                         sjcd_audio_status =
863                                             CDROM_AUDIO_ERROR;
864                                         return (-EIO);
865                                 } else
866                                         sjcd_audio_status =
867                                             CDROM_AUDIO_PLAY;
868                         }
869                         return (s);
870                 }
871
872         case CDROMREADTOCHDR:{
873                         struct cdrom_tochdr toc_header;
874 #if defined (SJCD_TRACE )
875                         printk("SJCD: ioctl: readtocheader\n");
876 #endif
877                         toc_header.cdth_trk0 = sjcd_first_track_no;
878                         toc_header.cdth_trk1 = sjcd_last_track_no;
879                         if (copy_to_user((void *)arg, &toc_header,
880                                          sizeof(toc_header)))
881                                 return -EFAULT;
882                         return 0;
883                 }
884
885         case CDROMREADTOCENTRY:{
886                         struct cdrom_tocentry toc_entry;
887                         int s;
888 #if defined( SJCD_TRACE )
889                         printk("SJCD: ioctl: readtocentry\n");
890 #endif
891                         if ((s =
892                              verify_area(VERIFY_WRITE, (void *) arg,
893                                          sizeof(toc_entry))) == 0) {
894                                 struct sjcd_hw_disk_info *tp;
895
896                                 copy_from_user(&toc_entry, (void *) arg,
897                                                sizeof(toc_entry));
898
899                                 if (toc_entry.cdte_track == CDROM_LEADOUT)
900                                         tp = &sjcd_table_of_contents[0];
901                                 else if (toc_entry.cdte_track <
902                                          sjcd_first_track_no)
903                                         return (-EINVAL);
904                                 else if (toc_entry.cdte_track >
905                                          sjcd_last_track_no)
906                                         return (-EINVAL);
907                                 else
908                                         tp = &sjcd_table_of_contents
909                                             [toc_entry.cdte_track];
910
911                                 toc_entry.cdte_adr =
912                                     tp->track_control & 0x0F;
913                                 toc_entry.cdte_ctrl =
914                                     tp->track_control >> 4;
915
916                                 switch (toc_entry.cdte_format) {
917                                 case CDROM_LBA:
918                                         toc_entry.cdte_addr.lba =
919                                             msf2hsg(&(tp->un.track_msf));
920                                         break;
921                                 case CDROM_MSF:
922                                         toc_entry.cdte_addr.msf.minute =
923                                             bcd2bin(tp->un.track_msf.min);
924                                         toc_entry.cdte_addr.msf.second =
925                                             bcd2bin(tp->un.track_msf.sec);
926                                         toc_entry.cdte_addr.msf.frame =
927                                             bcd2bin(tp->un.track_msf.
928                                                     frame);
929                                         break;
930                                 default:
931                                         return (-EINVAL);
932                                 }
933                                 if (copy_to_user((void *) arg, &toc_entry,
934                                                  sizeof(toc_entry)))
935                                         s = -EFAULT;
936                         }
937                         return (s);
938                 }
939
940         case CDROMSUBCHNL:{
941                         struct cdrom_subchnl subchnl;
942                         int s;
943 #if defined( SJCD_TRACE )
944                         printk("SJCD: ioctl: subchnl\n");
945 #endif
946                         if ((s =
947                              verify_area(VERIFY_WRITE, (void *) arg,
948                                          sizeof(subchnl))) == 0) {
949                                 struct sjcd_hw_qinfo q_info;
950
951                                 copy_from_user(&subchnl, (void *) arg,
952                                                sizeof(subchnl));
953                                 if (sjcd_get_q_info(&q_info) < 0)
954                                         return (-EIO);
955
956                                 subchnl.cdsc_audiostatus =
957                                     sjcd_audio_status;
958                                 subchnl.cdsc_adr =
959                                     q_info.track_control & 0x0F;
960                                 subchnl.cdsc_ctrl =
961                                     q_info.track_control >> 4;
962                                 subchnl.cdsc_trk =
963                                     bcd2bin(q_info.track_no);
964                                 subchnl.cdsc_ind = bcd2bin(q_info.x);
965
966                                 switch (subchnl.cdsc_format) {
967                                 case CDROM_LBA:
968                                         subchnl.cdsc_absaddr.lba =
969                                             msf2hsg(&(q_info.abs));
970                                         subchnl.cdsc_reladdr.lba =
971                                             msf2hsg(&(q_info.rel));
972                                         break;
973                                 case CDROM_MSF:
974                                         subchnl.cdsc_absaddr.msf.minute =
975                                             bcd2bin(q_info.abs.min);
976                                         subchnl.cdsc_absaddr.msf.second =
977                                             bcd2bin(q_info.abs.sec);
978                                         subchnl.cdsc_absaddr.msf.frame =
979                                             bcd2bin(q_info.abs.frame);
980                                         subchnl.cdsc_reladdr.msf.minute =
981                                             bcd2bin(q_info.rel.min);
982                                         subchnl.cdsc_reladdr.msf.second =
983                                             bcd2bin(q_info.rel.sec);
984                                         subchnl.cdsc_reladdr.msf.frame =
985                                             bcd2bin(q_info.rel.frame);
986                                         break;
987                                 default:
988                                         return (-EINVAL);
989                                 }
990                                 if (copy_to_user((void *) arg, &subchnl,
991                                                  sizeof(subchnl)))
992                                         s = -EFAULT;
993                         }
994                         return (s);
995                 }
996
997         case CDROMVOLCTRL:{
998                         struct cdrom_volctrl vol_ctrl;
999                         int s;
1000 #if defined( SJCD_TRACE )
1001                         printk("SJCD: ioctl: volctrl\n");
1002 #endif
1003                         if ((s =
1004                              verify_area(VERIFY_READ, (void *) arg,
1005                                          sizeof(vol_ctrl))) == 0) {
1006                                 unsigned char dummy[4];
1007
1008                                 copy_from_user(&vol_ctrl, (void *) arg,
1009                                                sizeof(vol_ctrl));
1010                                 sjcd_send_4_cmd(SCMD_SET_VOLUME,
1011                                                 vol_ctrl.channel0, 0xFF,
1012                                                 vol_ctrl.channel1, 0xFF);
1013                                 if (sjcd_receive_status() < 0)
1014                                         return (-EIO);
1015                                 (void) sjcd_load_response(dummy, 4);
1016                         }
1017                         return (s);
1018                 }
1019
1020         case CDROMEJECT:{
1021 #if defined( SJCD_TRACE )
1022                         printk("SJCD: ioctl: eject\n");
1023 #endif
1024                         if (!sjcd_command_is_in_progress) {
1025                                 sjcd_tray_unlock();
1026                                 sjcd_send_cmd(SCMD_EJECT_TRAY);
1027                                 (void) sjcd_receive_status();
1028                         }
1029                         return (0);
1030                 }
1031
1032 #if defined( SJCD_GATHER_STAT )
1033         case 0xABCD:{
1034 #if defined( SJCD_TRACE )
1035                         printk("SJCD: ioctl: statistic\n");
1036 #endif
1037                         if (copy_to_user((void *)arg, &statistic,
1038                                          sizeof(statistic)))
1039                                 return -EFAULT;
1040                         return 0;
1041                 }
1042 #endif
1043
1044         default:
1045                 return (-EINVAL);
1046         }
1047 }
1048
1049 /*
1050  * Invalidate internal buffers of the driver.
1051  */
1052 static void sjcd_invalidate_buffers(void)
1053 {
1054         int i;
1055         for (i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[i++] = -1);
1056         sjcd_buf_out = -1;
1057 }
1058
1059 /*
1060  * Take care of the different block sizes between cdrom and Linux.
1061  * When Linux gets variable block sizes this will probably go away.
1062  */
1063
1064 static int current_valid(void)
1065 {
1066         return CURRENT &&
1067                 CURRENT->cmd == READ &&
1068                 CURRENT->sector != -1;
1069 }
1070
1071 static void sjcd_transfer(void)
1072 {
1073 #if defined( SJCD_TRACE )
1074         printk("SJCD: transfer:\n");
1075 #endif
1076         if (current_valid()) {
1077                 while (CURRENT->nr_sectors) {
1078                         int i, bn = CURRENT->sector / 4;
1079                         for (i = 0;
1080                              i < SJCD_BUF_SIZ && sjcd_buf_bn[i] != bn;
1081                              i++);
1082                         if (i < SJCD_BUF_SIZ) {
1083                                 int offs =
1084                                     (i * 4 + (CURRENT->sector & 3)) * 512;
1085                                 int nr_sectors = 4 - (CURRENT->sector & 3);
1086                                 if (sjcd_buf_out != i) {
1087                                         sjcd_buf_out = i;
1088                                         if (sjcd_buf_bn[i] != bn) {
1089                                                 sjcd_buf_out = -1;
1090                                                 continue;
1091                                         }
1092                                 }
1093                                 if (nr_sectors > CURRENT->nr_sectors)
1094                                         nr_sectors = CURRENT->nr_sectors;
1095 #if defined( SJCD_TRACE )
1096                                 printk("SJCD: copy out\n");
1097 #endif
1098                                 memcpy(CURRENT->buffer, sjcd_buf + offs,
1099                                        nr_sectors * 512);
1100                                 CURRENT->nr_sectors -= nr_sectors;
1101                                 CURRENT->sector += nr_sectors;
1102                                 CURRENT->buffer += nr_sectors * 512;
1103                         } else {
1104                                 sjcd_buf_out = -1;
1105                                 break;
1106                         }
1107                 }
1108         }
1109 #if defined( SJCD_TRACE )
1110         printk("SJCD: transfer: done\n");
1111 #endif
1112 }
1113
1114 static void sjcd_poll(void)
1115 {
1116 #if defined( SJCD_GATHER_STAT )
1117         /*
1118          * Update total number of ticks.
1119          */
1120         statistic.ticks++;
1121         statistic.tticks[sjcd_transfer_state]++;
1122 #endif
1123
1124       ReSwitch:switch (sjcd_transfer_state) {
1125
1126         case SJCD_S_IDLE:{
1127 #if defined( SJCD_GATHER_STAT )
1128                         statistic.idle_ticks++;
1129 #endif
1130 #if defined( SJCD_TRACE )
1131                         printk("SJCD_S_IDLE\n");
1132 #endif
1133                         return;
1134                 }
1135
1136         case SJCD_S_START:{
1137 #if defined( SJCD_GATHER_STAT )
1138                         statistic.start_ticks++;
1139 #endif
1140                         sjcd_send_cmd(SCMD_GET_STATUS);
1141                         sjcd_transfer_state =
1142                             sjcd_mode ==
1143                             SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1144                         sjcd_transfer_timeout = 500;
1145 #if defined( SJCD_TRACE )
1146                         printk("SJCD_S_START: goto SJCD_S_%s mode\n",
1147                                sjcd_transfer_state ==
1148                                SJCD_S_READ ? "READ" : "MODE");
1149 #endif
1150                         break;
1151                 }
1152
1153         case SJCD_S_MODE:{
1154                         if (sjcd_check_status()) {
1155                                 /*
1156                                  * Previous command is completed.
1157                                  */
1158                                 if (!sjcd_status_valid
1159                                     || sjcd_command_failed) {
1160 #if defined( SJCD_TRACE )
1161                                         printk
1162                                             ("SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1163 #endif
1164                                         sjcd_transfer_state = SJCD_S_STOP;
1165                                         goto ReSwitch;
1166                                 }
1167
1168                                 sjcd_mode = 0;  /* unknown mode; should not be valid when failed */
1169                                 sjcd_send_1_cmd(SCMD_SET_MODE,
1170                                                 SCMD_MODE_COOKED);
1171                                 sjcd_transfer_state = SJCD_S_READ;
1172                                 sjcd_transfer_timeout = 1000;
1173 #if defined( SJCD_TRACE )
1174                                 printk
1175                                     ("SJCD_S_MODE: goto SJCD_S_READ mode\n");
1176 #endif
1177                         }
1178 #if defined( SJCD_GATHER_STAT )
1179                         else
1180                                 statistic.mode_ticks++;
1181 #endif
1182                         break;
1183                 }
1184
1185         case SJCD_S_READ:{
1186                         if (sjcd_status_valid ? 1 : sjcd_check_status()) {
1187                                 /*
1188                                  * Previous command is completed.
1189                                  */
1190                                 if (!sjcd_status_valid
1191                                     || sjcd_command_failed) {
1192 #if defined( SJCD_TRACE )
1193                                         printk
1194                                             ("SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1195 #endif
1196                                         sjcd_transfer_state = SJCD_S_STOP;
1197                                         goto ReSwitch;
1198                                 }
1199                                 if (!sjcd_media_is_available) {
1200 #if defined( SJCD_TRACE )
1201                                         printk
1202                                             ("SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n");
1203 #endif
1204                                         sjcd_transfer_state = SJCD_S_STOP;
1205                                         goto ReSwitch;
1206                                 }
1207                                 if (sjcd_mode != SCMD_MODE_COOKED) {
1208                                         /*
1209                                          * We seem to come from set mode. So discard one byte of result.
1210                                          */
1211                                         if (sjcd_load_response
1212                                             (&sjcd_mode, 1) != 0) {
1213 #if defined( SJCD_TRACE )
1214                                                 printk
1215                                                     ("SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n");
1216 #endif
1217                                                 sjcd_transfer_state =
1218                                                     SJCD_S_STOP;
1219                                                 goto ReSwitch;
1220                                         }
1221                                         if (sjcd_mode != SCMD_MODE_COOKED) {
1222 #if defined( SJCD_TRACE )
1223                                                 printk
1224                                                     ("SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n");
1225 #endif
1226                                                 sjcd_transfer_state =
1227                                                     SJCD_S_STOP;
1228                                                 goto ReSwitch;
1229                                         }
1230                                 }
1231
1232                                 if (current_valid()) {
1233                                         struct sjcd_play_msf msf;
1234
1235                                         sjcd_next_bn = CURRENT->sector / 4;
1236                                         hsg2msf(sjcd_next_bn, &msf.start);
1237                                         msf.end.min = 0;
1238                                         msf.end.sec = 0;
1239                                         msf.end.frame = sjcd_read_count =
1240                                             SJCD_BUF_SIZ;
1241 #if defined( SJCD_TRACE )
1242                                         printk
1243                                             ("SJCD: ---reading msf-address %x:%x:%x  %x:%x:%x\n",
1244                                              msf.start.min, msf.start.sec,
1245                                              msf.start.frame, msf.end.min,
1246                                              msf.end.sec, msf.end.frame);
1247                                         printk
1248                                             ("sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n",
1249                                              sjcd_next_bn, sjcd_buf_in,
1250                                              sjcd_buf_out,
1251                                              sjcd_buf_bn[sjcd_buf_in]);
1252 #endif
1253                                         sjcd_send_6_cmd(SCMD_DATA_READ,
1254                                                         &msf);
1255                                         sjcd_transfer_state = SJCD_S_DATA;
1256                                         sjcd_transfer_timeout = 500;
1257 #if defined( SJCD_TRACE )
1258                                         printk
1259                                             ("SJCD_S_READ: go to SJCD_S_DATA mode\n");
1260 #endif
1261                                 } else {
1262 #if defined( SJCD_TRACE )
1263                                         printk
1264                                             ("SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n");
1265 #endif
1266                                         sjcd_transfer_state = SJCD_S_STOP;
1267                                         goto ReSwitch;
1268                                 }
1269                         }
1270 #if defined( SJCD_GATHER_STAT )
1271                         else
1272                                 statistic.read_ticks++;
1273 #endif
1274                         break;
1275                 }
1276
1277         case SJCD_S_DATA:{
1278                         unsigned char stat;
1279
1280                       sjcd_s_data:stat =
1281                             inb(SJCDPORT
1282                                 (1));
1283 #if defined( SJCD_TRACE )
1284                         printk("SJCD_S_DATA: status = 0x%02x\n", stat);
1285 #endif
1286                         if (SJCD_STATUS_AVAILABLE(stat)) {
1287                                 /*
1288                                  * No data is waiting for us in the drive buffer. Status of operation
1289                                  * completion is available. Read and parse it.
1290                                  */
1291                                 sjcd_load_status();
1292
1293                                 if (!sjcd_status_valid
1294                                     || sjcd_command_failed) {
1295 #if defined( SJCD_TRACE )
1296                                         printk
1297                                             ("SJCD: read block %d failed, maybe audio disk? Giving up\n",
1298                                              sjcd_next_bn);
1299 #endif
1300                                         if (current_valid())
1301                                                 end_request(CURRENT, 0);
1302 #if defined( SJCD_TRACE )
1303                                         printk
1304                                             ("SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n");
1305 #endif
1306                                         sjcd_transfer_state = SJCD_S_STOP;
1307                                         goto ReSwitch;
1308                                 }
1309
1310                                 if (!sjcd_media_is_available) {
1311                                         printk
1312                                             ("SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n");
1313                                         sjcd_transfer_state = SJCD_S_STOP;
1314                                         goto ReSwitch;
1315                                 }
1316
1317                                 sjcd_transfer_state = SJCD_S_READ;
1318                                 goto ReSwitch;
1319                         } else if (SJCD_DATA_AVAILABLE(stat)) {
1320                                 /*
1321                                  * One frame is read into device buffer. We must copy it to our memory.
1322                                  * Otherwise cdrom hangs up. Check to see if we have something to copy
1323                                  * to.
1324                                  */
1325                                 if (!current_valid()
1326                                     && sjcd_buf_in == sjcd_buf_out) {
1327 #if defined( SJCD_TRACE )
1328                                         printk
1329                                             ("SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n");
1330                                         printk
1331                                             (" ... all the date would be discarded\n");
1332 #endif
1333                                         sjcd_transfer_state = SJCD_S_STOP;
1334                                         goto ReSwitch;
1335                                 }
1336
1337                                 /*
1338                                  * Everything seems to be OK. Just read the frame and recalculate
1339                                  * indices.
1340                                  */
1341                                 sjcd_buf_bn[sjcd_buf_in] = -1;  /* ??? */
1342                                 insb(SJCDPORT(2),
1343                                      sjcd_buf + 2048 * sjcd_buf_in, 2048);
1344 #if defined( SJCD_TRACE )
1345                                 printk
1346                                     ("SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1347                                      sjcd_next_bn, sjcd_buf_in,
1348                                      sjcd_buf_out,
1349                                      sjcd_buf_bn[sjcd_buf_in]);
1350 #endif
1351                                 sjcd_buf_bn[sjcd_buf_in] = sjcd_next_bn++;
1352                                 if (sjcd_buf_out == -1)
1353                                         sjcd_buf_out = sjcd_buf_in;
1354                                 if (++sjcd_buf_in == SJCD_BUF_SIZ)
1355                                         sjcd_buf_in = 0;
1356
1357                                 /*
1358                                  * Only one frame is ready at time. So we should turn over to wait for
1359                                  * another frame. If we need that, of course.
1360                                  */
1361                                 if (--sjcd_read_count == 0) {
1362                                         /*
1363                                          * OK, request seems to be precessed. Continue transferring...
1364                                          */
1365                                         if (!sjcd_transfer_is_active) {
1366                                                 while (current_valid()) {
1367                                                         /*
1368                                                          * Continue transferring.
1369                                                          */
1370                                                         sjcd_transfer();
1371                                                         if (CURRENT->
1372                                                             nr_sectors ==
1373                                                             0)
1374                                                                 end_request
1375                                                                     (CURRENT, 1);
1376                                                         else
1377                                                                 break;
1378                                                 }
1379                                         }
1380                                         if (current_valid() &&
1381                                             (CURRENT->sector / 4 <
1382                                              sjcd_next_bn
1383                                              || CURRENT->sector / 4 >
1384                                              sjcd_next_bn +
1385                                              SJCD_BUF_SIZ)) {
1386 #if defined( SJCD_TRACE )
1387                                                 printk
1388                                                     ("SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n");
1389 #endif
1390                                                 sjcd_transfer_state =
1391                                                     SJCD_S_STOP;
1392                                                 goto ReSwitch;
1393                                         }
1394                                 }
1395                                 /*
1396                                  * Now we should turn around rather than wait for while.
1397                                  */
1398                                 goto sjcd_s_data;
1399                         }
1400 #if defined( SJCD_GATHER_STAT )
1401                         else
1402                                 statistic.data_ticks++;
1403 #endif
1404                         break;
1405                 }
1406
1407         case SJCD_S_STOP:{
1408                         sjcd_read_count = 0;
1409                         sjcd_send_cmd(SCMD_STOP);
1410                         sjcd_transfer_state = SJCD_S_STOPPING;
1411                         sjcd_transfer_timeout = 500;
1412 #if defined( SJCD_GATHER_STAT )
1413                         statistic.stop_ticks++;
1414 #endif
1415                         break;
1416                 }
1417
1418         case SJCD_S_STOPPING:{
1419                         unsigned char stat;
1420
1421                         stat = inb(SJCDPORT(1));
1422 #if defined( SJCD_TRACE )
1423                         printk("SJCD_S_STOP: status = 0x%02x\n", stat);
1424 #endif
1425                         if (SJCD_DATA_AVAILABLE(stat)) {
1426                                 int i;
1427 #if defined( SJCD_TRACE )
1428                                 printk("SJCD_S_STOP: discard data\n");
1429 #endif
1430                                 /*
1431                                  * Discard all the data from the pipe. Foolish method.
1432                                  */
1433                                 for (i = 2048; i--;
1434                                      (void) inb(SJCDPORT(2)));
1435                                 sjcd_transfer_timeout = 500;
1436                         } else if (SJCD_STATUS_AVAILABLE(stat)) {
1437                                 sjcd_load_status();
1438                                 if (sjcd_status_valid
1439                                     && sjcd_media_is_changed) {
1440                                         sjcd_toc_uptodate = 0;
1441                                         sjcd_invalidate_buffers();
1442                                 }
1443                                 if (current_valid()) {
1444                                         if (sjcd_status_valid)
1445                                                 sjcd_transfer_state =
1446                                                     SJCD_S_READ;
1447                                         else
1448                                                 sjcd_transfer_state =
1449                                                     SJCD_S_START;
1450                                 } else
1451                                         sjcd_transfer_state = SJCD_S_IDLE;
1452                                 goto ReSwitch;
1453                         }
1454 #if defined( SJCD_GATHER_STAT )
1455                         else
1456                                 statistic.stopping_ticks++;
1457 #endif
1458                         break;
1459                 }
1460
1461         default:
1462                 printk("SJCD: poll: invalid state %d\n",
1463                        sjcd_transfer_state);
1464                 return;
1465         }
1466
1467         if (--sjcd_transfer_timeout == 0) {
1468                 printk("SJCD: timeout in state %d\n", sjcd_transfer_state);
1469                 while (current_valid())
1470                         end_request(CURRENT, 0);
1471                 sjcd_send_cmd(SCMD_STOP);
1472                 sjcd_transfer_state = SJCD_S_IDLE;
1473                 goto ReSwitch;
1474         }
1475
1476         /*
1477          * Get back in some time. 1 should be replaced with count variable to
1478          * avoid unnecessary testings.
1479          */
1480         SJCD_SET_TIMER(sjcd_poll, 1);
1481 }
1482
1483 static void do_sjcd_request(request_queue_t * q)
1484 {
1485 #if defined( SJCD_TRACE )
1486         printk("SJCD: do_sjcd_request(%ld+%ld)\n",
1487                CURRENT->sector, CURRENT->nr_sectors);
1488 #endif
1489         sjcd_transfer_is_active = 1;
1490         while (current_valid()) {
1491                 sjcd_transfer();
1492                 if (CURRENT->nr_sectors == 0)
1493                         end_request(CURRENT, 1);
1494                 else {
1495                         sjcd_buf_out = -1;      /* Want to read a block not in buffer */
1496                         if (sjcd_transfer_state == SJCD_S_IDLE) {
1497                                 if (!sjcd_toc_uptodate) {
1498                                         if (sjcd_update_toc() < 0) {
1499                                                 printk
1500                                                     ("SJCD: transfer: discard\n");
1501                                                 while (current_valid())
1502                                                         end_request(CURRENT, 0);
1503                                                 break;
1504                                         }
1505                                 }
1506                                 sjcd_transfer_state = SJCD_S_START;
1507                                 SJCD_SET_TIMER(sjcd_poll, HZ / 100);
1508                         }
1509                         break;
1510                 }
1511         }
1512         sjcd_transfer_is_active = 0;
1513 #if defined( SJCD_TRACE )
1514         printk
1515             ("sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1516              sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1517              sjcd_buf_bn[sjcd_buf_in]);
1518         printk("do_sjcd_request ends\n");
1519 #endif
1520 }
1521
1522 /*
1523  * Open the device special file. Check disk is in.
1524  */
1525 static int sjcd_open(struct inode *ip, struct file *fp)
1526 {
1527         /*
1528          * Check the presence of device.
1529          */
1530         if (!sjcd_present)
1531                 return (-ENXIO);
1532
1533         /*
1534          * Only read operations are allowed. Really? (:-)
1535          */
1536         if (fp->f_mode & 2)
1537                 return (-EROFS);
1538
1539         if (sjcd_open_count == 0) {
1540                 int s, sjcd_open_tries;
1541 /* We don't know that, do we? */
1542 /*
1543     sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1544 */
1545                 sjcd_mode = 0;
1546                 sjcd_door_was_open = 0;
1547                 sjcd_transfer_state = SJCD_S_IDLE;
1548                 sjcd_invalidate_buffers();
1549                 sjcd_status_valid = 0;
1550
1551                 /*
1552                  * Strict status checking.
1553                  */
1554                 for (sjcd_open_tries = 4; --sjcd_open_tries;) {
1555                         if (!sjcd_status_valid)
1556                                 sjcd_get_status();
1557                         if (!sjcd_status_valid) {
1558 #if defined( SJCD_DIAGNOSTIC )
1559                                 printk
1560                                     ("SJCD: open: timed out when check status.\n");
1561 #endif
1562                                 goto err_out;
1563                         } else if (!sjcd_media_is_available) {
1564 #if defined( SJCD_DIAGNOSTIC )
1565                                 printk("SJCD: open: no disk in drive\n");
1566 #endif
1567                                 if (!sjcd_door_closed) {
1568                                         sjcd_door_was_open = 1;
1569 #if defined( SJCD_TRACE )
1570                                         printk
1571                                             ("SJCD: open: close the tray\n");
1572 #endif
1573                                         s = sjcd_tray_close();
1574                                         if (s < 0 || !sjcd_status_valid
1575                                             || sjcd_command_failed) {
1576 #if defined( SJCD_DIAGNOSTIC )
1577                                                 printk
1578                                                     ("SJCD: open: tray close attempt failed\n");
1579 #endif
1580                                                 goto err_out;
1581                                         }
1582                                         continue;
1583                                 } else
1584                                         goto err_out;
1585                         }
1586                         break;
1587                 }
1588                 s = sjcd_tray_lock();
1589                 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1590 #if defined( SJCD_DIAGNOSTIC )
1591                         printk("SJCD: open: tray lock attempt failed\n");
1592 #endif
1593                         goto err_out;
1594                 }
1595 #if defined( SJCD_TRACE )
1596                 printk("SJCD: open: done\n");
1597 #endif
1598         }
1599
1600         ++sjcd_open_count;
1601         return (0);
1602
1603       err_out:
1604         return (-EIO);
1605 }
1606
1607 /*
1608  * On close, we flush all sjcd blocks from the buffer cache.
1609  */
1610 static int sjcd_release(struct inode *inode, struct file *file)
1611 {
1612         int s;
1613
1614 #if defined( SJCD_TRACE )
1615         printk("SJCD: release\n");
1616 #endif
1617         if (--sjcd_open_count == 0) {
1618                 sjcd_invalidate_buffers();
1619                 s = sjcd_tray_unlock();
1620                 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1621 #if defined( SJCD_DIAGNOSTIC )
1622                         printk
1623                             ("SJCD: release: tray unlock attempt failed.\n");
1624 #endif
1625                 }
1626                 if (sjcd_door_was_open) {
1627                         s = sjcd_tray_open();
1628                         if (s < 0 || !sjcd_status_valid
1629                             || sjcd_command_failed) {
1630 #if defined( SJCD_DIAGNOSTIC )
1631                                 printk
1632                                     ("SJCD: release: tray unload attempt failed.\n");
1633 #endif
1634                         }
1635                 }
1636         }
1637         return 0;
1638 }
1639
1640 /*
1641  * A list of file operations allowed for this cdrom.
1642  */
1643 static struct block_device_operations sjcd_fops = {
1644         .owner          = THIS_MODULE,
1645         .open           = sjcd_open,
1646         .release        = sjcd_release,
1647         .ioctl          = sjcd_ioctl,
1648         .media_changed  = sjcd_disk_change,
1649 };
1650
1651 /*
1652  * Following stuff is intended for initialization of the cdrom. It
1653  * first looks for presence of device. If the device is present, it
1654  * will be reset. Then read the version of the drive and load status.
1655  * The version is two BCD-coded bytes.
1656  */
1657 static struct {
1658         unsigned char major, minor;
1659 } sjcd_version;
1660
1661 static struct gendisk *sjcd_disk;
1662
1663 /*
1664  * Test for presence of drive and initialize it. Called at boot time.
1665  * Probe cdrom, find out version and status.
1666  */
1667 static int __init sjcd_init(void)
1668 {
1669         int i;
1670
1671         printk(KERN_INFO
1672                "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n",
1673                SJCD_VERSION_MAJOR, SJCD_VERSION_MINOR);
1674
1675 #if defined( SJCD_TRACE )
1676         printk("SJCD: sjcd=0x%x: ", sjcd_base);
1677 #endif
1678
1679         if (register_blkdev(MAJOR_NR, "sjcd"))
1680                 return -EIO;
1681
1682         sjcd_queue = blk_init_queue(do_sjcd_request, &sjcd_lock);
1683         if (!sjcd_queue)
1684                 goto out0;
1685
1686         blk_queue_hardsect_size(sjcd_queue, 2048);
1687
1688         sjcd_disk = alloc_disk(1);
1689         if (!sjcd_disk) {
1690                 printk(KERN_ERR "SJCD: can't allocate disk");
1691                 goto out1;
1692         }
1693         sjcd_disk->major = MAJOR_NR,
1694         sjcd_disk->first_minor = 0,
1695         sjcd_disk->fops = &sjcd_fops,
1696         sprintf(sjcd_disk->disk_name, "sjcd");
1697         sprintf(sjcd_disk->devfs_name, "sjcd");
1698
1699         if (check_region(sjcd_base, 4)) {
1700                 printk
1701                     ("SJCD: Init failed, I/O port (%X) is already in use\n",
1702                      sjcd_base);
1703                 goto out2;
1704         }
1705
1706         /*
1707          * Check for card. Since we are booting now, we can't use standard
1708          * wait algorithm.
1709          */
1710         printk(KERN_INFO "SJCD: Resetting: ");
1711         sjcd_send_cmd(SCMD_RESET);
1712         for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1713                 unsigned long timer;
1714
1715                 /*
1716                  * Wait 10ms approx.
1717                  */
1718                 for (timer = jiffies; time_before_eq(jiffies, timer););
1719                 if ((i % 100) == 0)
1720                         printk(".");
1721                 (void) sjcd_check_status();
1722         }
1723         if (i == 0 || sjcd_command_failed) {
1724                 printk(" reset failed, no drive found.\n");
1725                 goto out3;
1726         } else
1727                 printk("\n");
1728
1729         /*
1730          * Get and print out cdrom version.
1731          */
1732         printk(KERN_INFO "SJCD: Getting version: ");
1733         sjcd_send_cmd(SCMD_GET_VERSION);
1734         for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1735                 unsigned long timer;
1736
1737                 /*
1738                  * Wait 10ms approx.
1739                  */
1740                 for (timer = jiffies; time_before_eq(jiffies, timer););
1741                 if ((i % 100) == 0)
1742                         printk(".");
1743                 (void) sjcd_check_status();
1744         }
1745         if (i == 0 || sjcd_command_failed) {
1746                 printk(" get version failed, no drive found.\n");
1747                 goto out3;
1748         }
1749
1750         if (sjcd_load_response(&sjcd_version, sizeof(sjcd_version)) == 0) {
1751                 printk(" %1x.%02x\n", (int) sjcd_version.major,
1752                        (int) sjcd_version.minor);
1753         } else {
1754                 printk(" read version failed, no drive found.\n");
1755                 goto out3;
1756         }
1757
1758         /*
1759          * Check and print out the tray state. (if it is needed?).
1760          */
1761         if (!sjcd_status_valid) {
1762                 printk(KERN_INFO "SJCD: Getting status: ");
1763                 sjcd_send_cmd(SCMD_GET_STATUS);
1764                 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1765                         unsigned long timer;
1766
1767                         /*
1768                          * Wait 10ms approx.
1769                          */
1770                         for (timer = jiffies;
1771                              time_before_eq(jiffies, timer););
1772                         if ((i % 100) == 0)
1773                                 printk(".");
1774                         (void) sjcd_check_status();
1775                 }
1776                 if (i == 0 || sjcd_command_failed) {
1777                         printk(" get status failed, no drive found.\n");
1778                         goto out3;
1779                 } else
1780                         printk("\n");
1781         }
1782
1783         printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1784         sjcd_disk->queue = sjcd_queue;
1785         add_disk(sjcd_disk);
1786
1787         sjcd_present++;
1788         return (0);
1789 out3:
1790         release_region(sjcd_base, 4);
1791 out2:
1792         put_disk(sjcd_disk);
1793 out1:
1794         blk_cleanup_queue(sjcd_queue);
1795 out0:
1796         if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1797                 printk("SJCD: cannot unregister device.\n");
1798         return (-EIO);
1799 }
1800
1801 static void __exit sjcd_exit(void)
1802 {
1803         del_gendisk(sjcd_disk);
1804         put_disk(sjcd_disk);
1805         release_region(sjcd_base, 4);
1806         blk_cleanup_queue(sjcd_queue);
1807         if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1808                 printk("SJCD: cannot unregister device.\n");
1809         printk(KERN_INFO "SJCD: module: removed.\n");
1810 }
1811
1812 module_init(sjcd_init);
1813 module_exit(sjcd_exit);
1814
1815 MODULE_LICENSE("GPL");