Import 2.3.39pre2
[davej-history.git] / drivers / cdrom / sjcd.c
blob97107b96935994e3a6c581bdda8701e8440b89e6
1 /* -- sjcd.c
3 * Sanyo CD-ROM device driver implementation, Version 1.6
4 * Copyright (C) 1995 Vadim V. Model
6 * model@cecmow.enet.dec.com
7 * vadim@rbrf.ru
8 * vadim@ipsun.ras.ru
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).
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.
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.
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.
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.
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.
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>
58 #define SJCD_VERSION_MAJOR 1
59 #define SJCD_VERSION_MINOR 7
61 #ifdef MODULE
62 #include <linux/module.h>
63 #endif /* MODULE */
65 #include <linux/errno.h>
66 #include <linux/sched.h>
67 #include <linux/mm.h>
68 #include <linux/timer.h>
69 #include <linux/fs.h>
70 #include <linux/kernel.h>
71 #include <linux/cdrom.h>
72 #include <linux/ioport.h>
73 #include <linux/string.h>
74 #include <linux/major.h>
75 #include <linux/init.h>
77 #include <asm/system.h>
78 #include <asm/io.h>
79 #include <asm/uaccess.h>
81 #define MAJOR_NR SANYO_CDROM_MAJOR
82 #include <linux/blk.h>
83 #include "sjcd.h"
85 static int sjcd_present = 0;
87 #define SJCD_BUF_SIZ 32 /* cdr-h94a has internal 64K buffer */
90 * buffer for block size conversion
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;
97 * Status.
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;
111 static int sjcd_open_count;
113 static int sjcd_audio_status;
114 static struct sjcd_play_msf sjcd_playing;
116 static int sjcd_base = SJCD_BASE_ADDR;
118 #ifdef MODULE
119 MODULE_PARM(sjcd_base, "i");
120 #endif
122 static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
125 * Data transfer.
127 static volatile unsigned short sjcd_transfer_is_active = 0;
129 enum sjcd_transfer_state {
130 SJCD_S_IDLE = 0,
131 SJCD_S_START = 1,
132 SJCD_S_MODE = 2,
133 SJCD_S_READ = 3,
134 SJCD_S_DATA = 4,
135 SJCD_S_STOP = 5,
136 SJCD_S_STOPPING = 6
138 static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
139 static long sjcd_transfer_timeout = 0;
140 static int sjcd_read_count = 0;
141 static unsigned char sjcd_mode = 0;
143 #define SJCD_READ_TIMEOUT 5000
145 #if defined( SJCD_GATHER_STAT )
147 * Statistic.
149 static struct sjcd_stat statistic;
150 #endif
153 * Timer.
155 static struct timer_list sjcd_delay_timer = { NULL, NULL, 0, 0, NULL };
157 #define SJCD_SET_TIMER( func, tmout ) \
158 ( sjcd_delay_timer.expires = jiffies+tmout, \
159 sjcd_delay_timer.function = ( void * )func, \
160 add_timer( &sjcd_delay_timer ) )
162 #define CLEAR_TIMER del_timer( &sjcd_delay_timer )
164 static int sjcd_cleanup(void);
167 * Set up device, i.e., use command line data to set
168 * base address.
170 #ifndef MODULE
171 static int __init sjcd_setup( char *str)
173 int ints[2];
174 (void)get_options(str, ARRAY_SIZE(ints), ints);
175 if (ints[0] > 0)
176 sjcd_base = ints[1];
178 return 1;
181 __setup("sjcd=", sjcd_setup);
183 #endif
186 * Special converters.
188 static unsigned char bin2bcd( int bin ){
189 int u, v;
191 u = bin % 10; v = bin / 10;
192 return( u | ( v << 4 ) );
195 static int bcd2bin( unsigned char bcd ){
196 return( ( bcd >> 4 ) * 10 + ( bcd & 0x0F ) );
199 static long msf2hsg( struct msf *mp ){
200 return( bcd2bin( mp->frame ) + bcd2bin( mp->sec ) * 75
201 + bcd2bin( mp->min ) * 4500 - 150 );
204 static void hsg2msf( long hsg, struct msf *msf ){
205 hsg += 150; msf->min = hsg / 4500;
206 hsg %= 4500; msf->sec = hsg / 75; msf->frame = hsg % 75;
207 msf->min = bin2bcd( msf->min ); /* convert to BCD */
208 msf->sec = bin2bcd( msf->sec );
209 msf->frame = bin2bcd( msf->frame );
213 * Send a command to cdrom. Invalidate status.
215 static void sjcd_send_cmd( unsigned char cmd ){
216 #if defined( SJCD_TRACE )
217 printk( "SJCD: send_cmd( 0x%x )\n", cmd );
218 #endif
219 outb( cmd, SJCDPORT( 0 ) );
220 sjcd_command_is_in_progress = 1;
221 sjcd_status_valid = 0;
222 sjcd_command_failed = 0;
226 * Send a command with one arg to cdrom. Invalidate status.
228 static void sjcd_send_1_cmd( unsigned char cmd, unsigned char a ){
229 #if defined( SJCD_TRACE )
230 printk( "SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a );
231 #endif
232 outb( cmd, SJCDPORT( 0 ) );
233 outb( a, SJCDPORT( 0 ) );
234 sjcd_command_is_in_progress = 1;
235 sjcd_status_valid = 0;
236 sjcd_command_failed = 0;
240 * Send a command with four args to cdrom. Invalidate status.
242 static void sjcd_send_4_cmd( unsigned char cmd, unsigned char a,
243 unsigned char b, unsigned char c, unsigned char d ){
244 #if defined( SJCD_TRACE )
245 printk( "SJCD: send_4_cmd( 0x%x )\n", cmd );
246 #endif
247 outb( cmd, SJCDPORT( 0 ) );
248 outb( a, SJCDPORT( 0 ) );
249 outb( b, SJCDPORT( 0 ) );
250 outb( c, SJCDPORT( 0 ) );
251 outb( d, SJCDPORT( 0 ) );
252 sjcd_command_is_in_progress = 1;
253 sjcd_status_valid = 0;
254 sjcd_command_failed = 0;
258 * Send a play or read command to cdrom. Invalidate Status.
260 static void sjcd_send_6_cmd( unsigned char cmd, struct sjcd_play_msf *pms ){
261 #if defined( SJCD_TRACE )
262 printk( "SJCD: send_long_cmd( 0x%x )\n", cmd );
263 #endif
264 outb( cmd, SJCDPORT( 0 ) );
265 outb( pms->start.min, SJCDPORT( 0 ) );
266 outb( pms->start.sec, SJCDPORT( 0 ) );
267 outb( pms->start.frame, SJCDPORT( 0 ) );
268 outb( pms->end.min, SJCDPORT( 0 ) );
269 outb( pms->end.sec, SJCDPORT( 0 ) );
270 outb( pms->end.frame, SJCDPORT( 0 ) );
271 sjcd_command_is_in_progress = 1;
272 sjcd_status_valid = 0;
273 sjcd_command_failed = 0;
277 * Get a value from the data port. Should not block, so we use a little
278 * wait for a while. Returns 0 if OK.
280 static int sjcd_load_response( void *buf, int len ){
281 unsigned char *resp = ( unsigned char * )buf;
283 for( ; len; --len ){
284 int i;
285 for( i = 200; i-- && !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ); );
286 if( i > 0 ) *resp++ = ( unsigned char )inb( SJCDPORT( 0 ) );
287 else break;
289 return( len );
293 * Load and parse command completion status (drive info byte and maybe error).
294 * Sorry, no error classification yet.
296 static void sjcd_load_status( void ){
297 sjcd_media_is_changed = 0;
298 sjcd_completion_error = 0;
299 sjcd_completion_status = inb( SJCDPORT( 0 ) );
300 if( sjcd_completion_status & SST_DOOR_OPENED ){
301 sjcd_door_closed = sjcd_media_is_available = 0;
302 } else {
303 sjcd_door_closed = 1;
304 if( sjcd_completion_status & SST_MEDIA_CHANGED )
305 sjcd_media_is_available = sjcd_media_is_changed = 1;
306 else if( sjcd_completion_status & 0x0F ){
308 * OK, we seem to catch an error ...
310 while( !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) );
311 sjcd_completion_error = inb( SJCDPORT( 0 ) );
312 if( ( sjcd_completion_status & 0x08 ) &&
313 ( sjcd_completion_error & 0x40 ) )
314 sjcd_media_is_available = 0;
315 else sjcd_command_failed = 1;
316 } else sjcd_media_is_available = 1;
319 * Ok, status loaded successfully.
321 sjcd_status_valid = 1, sjcd_error_reported = 0;
322 sjcd_command_is_in_progress = 0;
325 * If the disk is changed, the TOC is not valid.
327 if( sjcd_media_is_changed ) sjcd_toc_uptodate = 0;
328 #if defined( SJCD_TRACE )
329 printk( "SJCD: status %02x.%02x loaded.\n",
330 ( int )sjcd_completion_status, ( int )sjcd_completion_error );
331 #endif
335 * Read status from cdrom. Check to see if the status is available.
337 static int sjcd_check_status( void ){
339 * Try to load the response from cdrom into buffer.
341 if( SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) ){
342 sjcd_load_status();
343 return( 1 );
344 } else {
346 * No status is available.
348 return( 0 );
353 * This is just timeout counter, and nothing more. Surprised ? :-)
355 static volatile long sjcd_status_timeout;
358 * We need about 10 seconds to wait. The longest command takes about 5 seconds
359 * to probe the disk (usually after tray closed or drive reset). Other values
360 * should be thought of for other commands.
362 #define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
364 static void sjcd_status_timer( void ){
365 if( sjcd_check_status() ){
367 * The command completed and status is loaded, stop waiting.
369 wake_up( &sjcd_waitq );
370 } else if( --sjcd_status_timeout <= 0 ){
372 * We are timed out.
374 wake_up( &sjcd_waitq );
375 } else {
377 * We have still some time to wait. Try again.
379 SJCD_SET_TIMER( sjcd_status_timer, 1 );
384 * Wait for status for 10 sec approx. Returns non-positive when timed out.
385 * Should not be used while reading data CDs.
387 static int sjcd_wait_for_status( void ){
388 sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
389 SJCD_SET_TIMER( sjcd_status_timer, 1 );
390 sleep_on( &sjcd_waitq );
391 #if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
392 if( sjcd_status_timeout <= 0 )
393 printk( "SJCD: Error Wait For Status.\n" );
394 #endif
395 return( sjcd_status_timeout );
398 static int sjcd_receive_status( void ){
399 int i;
400 #if defined( SJCD_TRACE )
401 printk( "SJCD: receive_status\n" );
402 #endif
404 * Wait a bit for status available.
406 for( i = 200; i-- && ( sjcd_check_status() == 0 ); );
407 if( i < 0 ){
408 #if defined( SJCD_TRACE )
409 printk( "SJCD: long wait for status\n" );
410 #endif
411 if( sjcd_wait_for_status() <= 0 )
412 printk( "SJCD: Timeout when read status.\n" );
413 else i = 0;
415 return( i );
419 * Load the status. Issue get status command and wait for status available.
421 static void sjcd_get_status( void ){
422 #if defined( SJCD_TRACE )
423 printk( "SJCD: get_status\n" );
424 #endif
425 sjcd_send_cmd( SCMD_GET_STATUS );
426 sjcd_receive_status();
430 * Check the drive if the disk is changed. Should be revised.
432 static int sjcd_disk_change( kdev_t full_dev ){
433 #if 0
434 printk( "SJCD: sjcd_disk_change( 0x%x )\n", full_dev );
435 #endif
436 if( MINOR( full_dev ) > 0 ){
437 printk( "SJCD: request error: invalid device minor.\n" );
438 return 0;
440 if( !sjcd_command_is_in_progress )
441 sjcd_get_status();
442 return( sjcd_status_valid ? sjcd_media_is_changed : 0 );
446 * Read the table of contents (TOC) and TOC header if necessary.
447 * We assume that the drive contains no more than 99 toc entries.
449 static struct sjcd_hw_disk_info sjcd_table_of_contents[ SJCD_MAX_TRACKS ];
450 static unsigned char sjcd_first_track_no, sjcd_last_track_no;
451 #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
453 static int sjcd_update_toc( void ){
454 struct sjcd_hw_disk_info info;
455 int i;
456 #if defined( SJCD_TRACE )
457 printk( "SJCD: update toc:\n" );
458 #endif
460 * check to see if we need to do anything
462 if( sjcd_toc_uptodate ) return( 0 );
465 * Get the TOC start information.
467 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK );
468 sjcd_receive_status();
470 if( !sjcd_status_valid ){
471 printk( "SJCD: cannot load status.\n" );
472 return( -1 );
475 if( !sjcd_media_is_available ){
476 printk( "SJCD: no disk in drive\n" );
477 return( -1 );
480 if( !sjcd_command_failed ){
481 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
482 printk( "SJCD: cannot load response about TOC start.\n" );
483 return( -1 );
485 sjcd_first_track_no = bcd2bin( info.un.track_no );
486 } else {
487 printk( "SJCD: get first failed\n" );
488 return( -1 );
490 #if defined( SJCD_TRACE )
491 printk( "SJCD: TOC start 0x%02x ", sjcd_first_track_no );
492 #endif
494 * Get the TOC finish information.
496 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK );
497 sjcd_receive_status();
499 if( !sjcd_status_valid ){
500 printk( "SJCD: cannot load status.\n" );
501 return( -1 );
504 if( !sjcd_media_is_available ){
505 printk( "SJCD: no disk in drive\n" );
506 return( -1 );
509 if( !sjcd_command_failed ){
510 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
511 printk( "SJCD: cannot load response about TOC finish.\n" );
512 return( -1 );
514 sjcd_last_track_no = bcd2bin( info.un.track_no );
515 } else {
516 printk( "SJCD: get last failed\n" );
517 return( -1 );
519 #if defined( SJCD_TRACE )
520 printk( "SJCD: TOC finish 0x%02x ", sjcd_last_track_no );
521 #endif
522 for( i = sjcd_first_track_no; i <= sjcd_last_track_no; i++ ){
524 * Get the first track information.
526 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, bin2bcd( i ) );
527 sjcd_receive_status();
529 if( !sjcd_status_valid ){
530 printk( "SJCD: cannot load status.\n" );
531 return( -1 );
534 if( !sjcd_media_is_available ){
535 printk( "SJCD: no disk in drive\n" );
536 return( -1 );
539 if( !sjcd_command_failed ){
540 if( sjcd_load_response( &sjcd_table_of_contents[ i ],
541 sizeof( struct sjcd_hw_disk_info ) ) != 0 ){
542 printk( "SJCD: cannot load info for %d track\n", i );
543 return( -1 );
545 } else {
546 printk( "SJCD: get info %d failed\n", i );
547 return( -1 );
552 * Get the disk length info.
554 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE );
555 sjcd_receive_status();
557 if( !sjcd_status_valid ){
558 printk( "SJCD: cannot load status.\n" );
559 return( -1 );
562 if( !sjcd_media_is_available ){
563 printk( "SJCD: no disk in drive\n" );
564 return( -1 );
567 if( !sjcd_command_failed ){
568 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
569 printk( "SJCD: cannot load response about disk size.\n" );
570 return( -1 );
572 sjcd_disk_length.min = info.un.track_msf.min;
573 sjcd_disk_length.sec = info.un.track_msf.sec;
574 sjcd_disk_length.frame = info.un.track_msf.frame;
575 } else {
576 printk( "SJCD: get size failed\n" );
577 return( 1 );
579 #if defined( SJCD_TRACE )
580 printk( "SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
581 sjcd_disk_length.sec, sjcd_disk_length.frame );
582 #endif
583 return( 0 );
587 * Load subchannel information.
589 static int sjcd_get_q_info( struct sjcd_hw_qinfo *qp ){
590 int s;
591 #if defined( SJCD_TRACE )
592 printk( "SJCD: load sub q\n" );
593 #endif
594 sjcd_send_cmd( SCMD_GET_QINFO );
595 s = sjcd_receive_status();
596 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ){
597 sjcd_send_cmd( 0xF2 );
598 s = sjcd_receive_status();
599 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
600 sjcd_send_cmd( SCMD_GET_QINFO );
601 s = sjcd_receive_status();
602 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
604 if( sjcd_media_is_available )
605 if( sjcd_load_response( qp, sizeof( *qp ) ) == 0 ) return( 0 );
606 return( -1 );
610 * Start playing from the specified position.
612 static int sjcd_play( struct sjcd_play_msf *mp ){
613 struct sjcd_play_msf msf;
616 * Turn the device to play mode.
618 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_PLAY );
619 if( sjcd_receive_status() < 0 ) return( -1 );
622 * Seek to the starting point.
624 msf.start = mp->start;
625 msf.end.min = msf.end.sec = msf.end.frame = 0x00;
626 sjcd_send_6_cmd( SCMD_SEEK, &msf );
627 if( sjcd_receive_status() < 0 ) return( -1 );
630 * Start playing.
632 sjcd_send_6_cmd( SCMD_PLAY, mp );
633 return( sjcd_receive_status() );
637 * Tray control functions.
639 static int sjcd_tray_close( void ){
640 #if defined( SJCD_TRACE )
641 printk( "SJCD: tray_close\n" );
642 #endif
643 sjcd_send_cmd( SCMD_CLOSE_TRAY );
644 return( sjcd_receive_status() );
647 static int sjcd_tray_lock( void ){
648 #if defined( SJCD_TRACE )
649 printk( "SJCD: tray_lock\n" );
650 #endif
651 sjcd_send_cmd( SCMD_LOCK_TRAY );
652 return( sjcd_receive_status() );
655 static int sjcd_tray_unlock( void ){
656 #if defined( SJCD_TRACE )
657 printk( "SJCD: tray_unlock\n" );
658 #endif
659 sjcd_send_cmd( SCMD_UNLOCK_TRAY );
660 return( sjcd_receive_status() );
663 static int sjcd_tray_open( void ){
664 #if defined( SJCD_TRACE )
665 printk( "SJCD: tray_open\n" );
666 #endif
667 sjcd_send_cmd( SCMD_EJECT_TRAY );
668 return( sjcd_receive_status() );
672 * Do some user commands.
674 static int sjcd_ioctl( struct inode *ip, struct file *fp,
675 unsigned int cmd, unsigned long arg ){
676 #if defined( SJCD_TRACE )
677 printk( "SJCD:ioctl\n" );
678 #endif
680 if( ip == NULL ) return( -EINVAL );
682 sjcd_get_status();
683 if( !sjcd_status_valid ) return( -EIO );
684 if( sjcd_update_toc() < 0 ) return( -EIO );
686 switch( cmd ){
687 case CDROMSTART:{
688 #if defined( SJCD_TRACE )
689 printk( "SJCD: ioctl: start\n" );
690 #endif
691 return( 0 );
694 case CDROMSTOP:{
695 #if defined( SJCD_TRACE )
696 printk( "SJCD: ioctl: stop\n" );
697 #endif
698 sjcd_send_cmd( SCMD_PAUSE );
699 ( void )sjcd_receive_status();
700 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
701 return( 0 );
704 case CDROMPAUSE:{
705 struct sjcd_hw_qinfo q_info;
706 #if defined( SJCD_TRACE )
707 printk( "SJCD: ioctl: pause\n" );
708 #endif
709 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
710 sjcd_send_cmd( SCMD_PAUSE );
711 ( void )sjcd_receive_status();
712 if( sjcd_get_q_info( &q_info ) < 0 ){
713 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
714 } else {
715 sjcd_audio_status = CDROM_AUDIO_PAUSED;
716 sjcd_playing.start = q_info.abs;
718 return( 0 );
719 } else return( -EINVAL );
722 case CDROMRESUME:{
723 #if defined( SJCD_TRACE )
724 printk( "SJCD: ioctl: resume\n" );
725 #endif
726 if( sjcd_audio_status == CDROM_AUDIO_PAUSED ){
728 * continue play starting at saved location
730 if( sjcd_play( &sjcd_playing ) < 0 ){
731 sjcd_audio_status = CDROM_AUDIO_ERROR;
732 return( -EIO );
733 } else {
734 sjcd_audio_status = CDROM_AUDIO_PLAY;
735 return( 0 );
737 } else return( -EINVAL );
740 case CDROMPLAYTRKIND:{
741 struct cdrom_ti ti; int s;
742 #if defined( SJCD_TRACE )
743 printk( "SJCD: ioctl: playtrkind\n" );
744 #endif
745 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( ti ) ) ) == 0 ){
746 copy_from_user( &ti, (void *)arg, sizeof( ti ) );
748 if( ti.cdti_trk0 < sjcd_first_track_no ) return( -EINVAL );
749 if( ti.cdti_trk1 > sjcd_last_track_no )
750 ti.cdti_trk1 = sjcd_last_track_no;
751 if( ti.cdti_trk0 > ti.cdti_trk1 ) return( -EINVAL );
753 sjcd_playing.start = sjcd_table_of_contents[ ti.cdti_trk0 ].un.track_msf;
754 sjcd_playing.end = ( ti.cdti_trk1 < sjcd_last_track_no ) ?
755 sjcd_table_of_contents[ ti.cdti_trk1 + 1 ].un.track_msf :
756 sjcd_table_of_contents[ 0 ].un.track_msf;
758 if( sjcd_play( &sjcd_playing ) < 0 ){
759 sjcd_audio_status = CDROM_AUDIO_ERROR;
760 return( -EIO );
761 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
763 return( s );
766 case CDROMPLAYMSF:{
767 struct cdrom_msf sjcd_msf; int s;
768 #if defined( SJCD_TRACE )
769 printk( "SJCD: ioctl: playmsf\n" );
770 #endif
771 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( sjcd_msf ) ) ) == 0 ){
772 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
773 sjcd_send_cmd( SCMD_PAUSE );
774 ( void )sjcd_receive_status();
775 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
778 copy_from_user( &sjcd_msf, (void *)arg, sizeof( sjcd_msf ) );
780 sjcd_playing.start.min = bin2bcd( sjcd_msf.cdmsf_min0 );
781 sjcd_playing.start.sec = bin2bcd( sjcd_msf.cdmsf_sec0 );
782 sjcd_playing.start.frame = bin2bcd( sjcd_msf.cdmsf_frame0 );
783 sjcd_playing.end.min = bin2bcd( sjcd_msf.cdmsf_min1 );
784 sjcd_playing.end.sec = bin2bcd( sjcd_msf.cdmsf_sec1 );
785 sjcd_playing.end.frame = bin2bcd( sjcd_msf.cdmsf_frame1 );
787 if( sjcd_play( &sjcd_playing ) < 0 ){
788 sjcd_audio_status = CDROM_AUDIO_ERROR;
789 return( -EIO );
790 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
792 return( s );
795 case CDROMREADTOCHDR:{
796 struct cdrom_tochdr toc_header; int s;
797 #if defined (SJCD_TRACE )
798 printk( "SJCD: ioctl: readtocheader\n" );
799 #endif
800 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_header ) ) ) == 0 ){
801 toc_header.cdth_trk0 = sjcd_first_track_no;
802 toc_header.cdth_trk1 = sjcd_last_track_no;
803 copy_to_user( (void *)arg, &toc_header, sizeof( toc_header ) );
805 return( s );
808 case CDROMREADTOCENTRY:{
809 struct cdrom_tocentry toc_entry; int s;
810 #if defined( SJCD_TRACE )
811 printk( "SJCD: ioctl: readtocentry\n" );
812 #endif
813 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_entry ) ) ) == 0 ){
814 struct sjcd_hw_disk_info *tp;
816 copy_from_user( &toc_entry, (void *)arg, sizeof( toc_entry ) );
818 if( toc_entry.cdte_track == CDROM_LEADOUT )
819 tp = &sjcd_table_of_contents[ 0 ];
820 else if( toc_entry.cdte_track < sjcd_first_track_no ) return( -EINVAL );
821 else if( toc_entry.cdte_track > sjcd_last_track_no ) return( -EINVAL );
822 else tp = &sjcd_table_of_contents[ toc_entry.cdte_track ];
824 toc_entry.cdte_adr = tp->track_control & 0x0F;
825 toc_entry.cdte_ctrl = tp->track_control >> 4;
827 switch( toc_entry.cdte_format ){
828 case CDROM_LBA:
829 toc_entry.cdte_addr.lba = msf2hsg( &( tp->un.track_msf ) );
830 break;
831 case CDROM_MSF:
832 toc_entry.cdte_addr.msf.minute = bcd2bin( tp->un.track_msf.min );
833 toc_entry.cdte_addr.msf.second = bcd2bin( tp->un.track_msf.sec );
834 toc_entry.cdte_addr.msf.frame = bcd2bin( tp->un.track_msf.frame );
835 break;
836 default: return( -EINVAL );
838 copy_to_user( (void *)arg, &toc_entry, sizeof( toc_entry ) );
840 return( s );
843 case CDROMSUBCHNL:{
844 struct cdrom_subchnl subchnl; int s;
845 #if defined( SJCD_TRACE )
846 printk( "SJCD: ioctl: subchnl\n" );
847 #endif
848 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( subchnl ) ) ) == 0 ){
849 struct sjcd_hw_qinfo q_info;
851 copy_from_user( &subchnl, (void *)arg, sizeof( subchnl ) );
852 if( sjcd_get_q_info( &q_info ) < 0 ) return( -EIO );
854 subchnl.cdsc_audiostatus = sjcd_audio_status;
855 subchnl.cdsc_adr = q_info.track_control & 0x0F;
856 subchnl.cdsc_ctrl = q_info.track_control >> 4;
857 subchnl.cdsc_trk = bcd2bin( q_info.track_no );
858 subchnl.cdsc_ind = bcd2bin( q_info.x );
860 switch( subchnl.cdsc_format ){
861 case CDROM_LBA:
862 subchnl.cdsc_absaddr.lba = msf2hsg( &( q_info.abs ) );
863 subchnl.cdsc_reladdr.lba = msf2hsg( &( q_info.rel ) );
864 break;
865 case CDROM_MSF:
866 subchnl.cdsc_absaddr.msf.minute = bcd2bin( q_info.abs.min );
867 subchnl.cdsc_absaddr.msf.second = bcd2bin( q_info.abs.sec );
868 subchnl.cdsc_absaddr.msf.frame = bcd2bin( q_info.abs.frame );
869 subchnl.cdsc_reladdr.msf.minute = bcd2bin( q_info.rel.min );
870 subchnl.cdsc_reladdr.msf.second = bcd2bin( q_info.rel.sec );
871 subchnl.cdsc_reladdr.msf.frame = bcd2bin( q_info.rel.frame );
872 break;
873 default: return( -EINVAL );
875 copy_to_user( (void *)arg, &subchnl, sizeof( subchnl ) );
877 return( s );
880 case CDROMVOLCTRL:{
881 struct cdrom_volctrl vol_ctrl; int s;
882 #if defined( SJCD_TRACE )
883 printk( "SJCD: ioctl: volctrl\n" );
884 #endif
885 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( vol_ctrl ) ) ) == 0 ){
886 unsigned char dummy[ 4 ];
888 copy_from_user( &vol_ctrl, (void *)arg, sizeof( vol_ctrl ) );
889 sjcd_send_4_cmd( SCMD_SET_VOLUME, vol_ctrl.channel0, 0xFF,
890 vol_ctrl.channel1, 0xFF );
891 if( sjcd_receive_status() < 0 ) return( -EIO );
892 ( void )sjcd_load_response( dummy, 4 );
894 return( s );
897 case CDROMEJECT:{
898 #if defined( SJCD_TRACE )
899 printk( "SJCD: ioctl: eject\n" );
900 #endif
901 if( !sjcd_command_is_in_progress ){
902 sjcd_tray_unlock();
903 sjcd_send_cmd( SCMD_EJECT_TRAY );
904 ( void )sjcd_receive_status();
906 return( 0 );
909 #if defined( SJCD_GATHER_STAT )
910 case 0xABCD:{
911 int s;
912 #if defined( SJCD_TRACE )
913 printk( "SJCD: ioctl: statistic\n" );
914 #endif
915 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( statistic ) ) ) == 0 )
916 copy_to_user( (void *)arg, &statistic, sizeof( statistic ) );
917 return( s );
919 #endif
921 default:
922 return( -EINVAL );
927 * Invalidate internal buffers of the driver.
929 static void sjcd_invalidate_buffers( void ){
930 int i;
931 for( i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[ i++ ] = -1 );
932 sjcd_buf_out = -1;
936 * Take care of the different block sizes between cdrom and Linux.
937 * When Linux gets variable block sizes this will probably go away.
940 #define CURRENT_IS_VALID \
941 ( CURRENT != NULL && MAJOR( CURRENT->rq_dev ) == MAJOR_NR && \
942 CURRENT->cmd == READ && CURRENT->sector != -1 )
944 static void sjcd_transfer( void ){
945 #if defined( SJCD_TRACE )
946 printk( "SJCD: transfer:\n" );
947 #endif
948 if( CURRENT_IS_VALID ){
949 while( CURRENT->nr_sectors ){
950 int i, bn = CURRENT->sector / 4;
951 for( i = 0; i < SJCD_BUF_SIZ && sjcd_buf_bn[ i ] != bn; i++ );
952 if( i < SJCD_BUF_SIZ ){
953 int offs = ( i * 4 + ( CURRENT->sector & 3 ) ) * 512;
954 int nr_sectors = 4 - ( CURRENT->sector & 3 );
955 if( sjcd_buf_out != i ){
956 sjcd_buf_out = i;
957 if( sjcd_buf_bn[ i ] != bn ){
958 sjcd_buf_out = -1;
959 continue;
962 if( nr_sectors > CURRENT->nr_sectors )
963 nr_sectors = CURRENT->nr_sectors;
964 #if defined( SJCD_TRACE )
965 printk( "SJCD: copy out\n" );
966 #endif
967 memcpy( CURRENT->buffer, sjcd_buf + offs, nr_sectors * 512 );
968 CURRENT->nr_sectors -= nr_sectors;
969 CURRENT->sector += nr_sectors;
970 CURRENT->buffer += nr_sectors * 512;
971 } else {
972 sjcd_buf_out = -1;
973 break;
977 #if defined( SJCD_TRACE )
978 printk( "SJCD: transfer: done\n" );
979 #endif
982 static void sjcd_poll( void ){
983 #if defined( SJCD_GATHER_STAT )
985 * Update total number of ticks.
987 statistic.ticks++;
988 statistic.tticks[ sjcd_transfer_state ]++;
989 #endif
991 ReSwitch: switch( sjcd_transfer_state ){
993 case SJCD_S_IDLE:{
994 #if defined( SJCD_GATHER_STAT )
995 statistic.idle_ticks++;
996 #endif
997 #if defined( SJCD_TRACE )
998 printk( "SJCD_S_IDLE\n" );
999 #endif
1000 return;
1003 case SJCD_S_START:{
1004 #if defined( SJCD_GATHER_STAT )
1005 statistic.start_ticks++;
1006 #endif
1007 sjcd_send_cmd( SCMD_GET_STATUS );
1008 sjcd_transfer_state =
1009 sjcd_mode == SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1010 sjcd_transfer_timeout = 500;
1011 #if defined( SJCD_TRACE )
1012 printk( "SJCD_S_START: goto SJCD_S_%s mode\n",
1013 sjcd_transfer_state == SJCD_S_READ ? "READ" : "MODE" );
1014 #endif
1015 break;
1018 case SJCD_S_MODE:{
1019 if( sjcd_check_status() ){
1021 * Previous command is completed.
1023 if( !sjcd_status_valid || sjcd_command_failed ){
1024 #if defined( SJCD_TRACE )
1025 printk( "SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1026 #endif
1027 sjcd_transfer_state = SJCD_S_STOP;
1028 goto ReSwitch;
1031 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1032 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_COOKED );
1033 sjcd_transfer_state = SJCD_S_READ; sjcd_transfer_timeout = 1000;
1034 #if defined( SJCD_TRACE )
1035 printk( "SJCD_S_MODE: goto SJCD_S_READ mode\n" );
1036 #endif
1038 #if defined( SJCD_GATHER_STAT )
1039 else statistic.mode_ticks++;
1040 #endif
1041 break;
1044 case SJCD_S_READ:{
1045 if( sjcd_status_valid ? 1 : sjcd_check_status() ){
1047 * Previous command is completed.
1049 if( !sjcd_status_valid || sjcd_command_failed ){
1050 #if defined( SJCD_TRACE )
1051 printk( "SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1052 #endif
1053 sjcd_transfer_state = SJCD_S_STOP;
1054 goto ReSwitch;
1056 if( !sjcd_media_is_available ){
1057 #if defined( SJCD_TRACE )
1058 printk( "SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n" );
1059 #endif
1060 sjcd_transfer_state = SJCD_S_STOP;
1061 goto ReSwitch;
1063 if( sjcd_mode != SCMD_MODE_COOKED ){
1065 * We seem to come from set mode. So discard one byte of result.
1067 if( sjcd_load_response( &sjcd_mode, 1 ) != 0 ){
1068 #if defined( SJCD_TRACE )
1069 printk( "SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n" );
1070 #endif
1071 sjcd_transfer_state = SJCD_S_STOP;
1072 goto ReSwitch;
1074 if( sjcd_mode != SCMD_MODE_COOKED ){
1075 #if defined( SJCD_TRACE )
1076 printk( "SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n" );
1077 #endif
1078 sjcd_transfer_state = SJCD_S_STOP;
1079 goto ReSwitch;
1083 if( CURRENT_IS_VALID ){
1084 struct sjcd_play_msf msf;
1086 sjcd_next_bn = CURRENT->sector / 4;
1087 hsg2msf( sjcd_next_bn, &msf.start );
1088 msf.end.min = 0; msf.end.sec = 0;
1089 msf.end.frame = sjcd_read_count = SJCD_BUF_SIZ;
1090 #if defined( SJCD_TRACE )
1091 printk( "SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1092 msf.start.min, msf.start.sec, msf.start.frame,
1093 msf.end.min, msf.end.sec, msf.end.frame );
1094 printk( "sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n", \
1095 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1096 sjcd_buf_bn[ sjcd_buf_in ] );
1097 #endif
1098 sjcd_send_6_cmd( SCMD_DATA_READ, &msf );
1099 sjcd_transfer_state = SJCD_S_DATA;
1100 sjcd_transfer_timeout = 500;
1101 #if defined( SJCD_TRACE )
1102 printk( "SJCD_S_READ: go to SJCD_S_DATA mode\n" );
1103 #endif
1104 } else {
1105 #if defined( SJCD_TRACE )
1106 printk( "SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n" );
1107 #endif
1108 sjcd_transfer_state = SJCD_S_STOP;
1109 goto ReSwitch;
1112 #if defined( SJCD_GATHER_STAT )
1113 else statistic.read_ticks++;
1114 #endif
1115 break;
1118 case SJCD_S_DATA:{
1119 unsigned char stat;
1121 sjcd_s_data: stat = inb( SJCDPORT( 1 ) );
1122 #if defined( SJCD_TRACE )
1123 printk( "SJCD_S_DATA: status = 0x%02x\n", stat );
1124 #endif
1125 if( SJCD_STATUS_AVAILABLE( stat ) ){
1127 * No data is waiting for us in the drive buffer. Status of operation
1128 * completion is available. Read and parse it.
1130 sjcd_load_status();
1132 if( !sjcd_status_valid || sjcd_command_failed ){
1133 #if defined( SJCD_TRACE )
1134 printk( "SJCD: read block %d failed, maybe audio disk? Giving up\n",
1135 sjcd_next_bn );
1136 #endif
1137 if( CURRENT_IS_VALID ) end_request( 0 );
1138 #if defined( SJCD_TRACE )
1139 printk( "SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n" );
1140 #endif
1141 sjcd_transfer_state = SJCD_S_STOP;
1142 goto ReSwitch;
1145 if( !sjcd_media_is_available ){
1146 printk( "SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n" );
1147 sjcd_transfer_state = SJCD_S_STOP;
1148 goto ReSwitch;
1151 sjcd_transfer_state = SJCD_S_READ;
1152 goto ReSwitch;
1153 } else if( SJCD_DATA_AVAILABLE( stat ) ){
1155 * One frame is read into device buffer. We must copy it to our memory.
1156 * Otherwise cdrom hangs up. Check to see if we have something to copy
1157 * to.
1159 if( !CURRENT_IS_VALID && sjcd_buf_in == sjcd_buf_out ){
1160 #if defined( SJCD_TRACE )
1161 printk( "SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n" );
1162 printk( " ... all the date would be discarded\n" );
1163 #endif
1164 sjcd_transfer_state = SJCD_S_STOP;
1165 goto ReSwitch;
1169 * Everything seems to be OK. Just read the frame and recalculate
1170 * indices.
1172 sjcd_buf_bn[ sjcd_buf_in ] = -1; /* ??? */
1173 insb( SJCDPORT( 2 ), sjcd_buf + 2048 * sjcd_buf_in, 2048 );
1174 #if defined( SJCD_TRACE )
1175 printk( "SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1176 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1177 sjcd_buf_bn[ sjcd_buf_in ] );
1178 #endif
1179 sjcd_buf_bn[ sjcd_buf_in ] = sjcd_next_bn++;
1180 if( sjcd_buf_out == -1 ) sjcd_buf_out = sjcd_buf_in;
1181 if( ++sjcd_buf_in == SJCD_BUF_SIZ ) sjcd_buf_in = 0;
1184 * Only one frame is ready at time. So we should turn over to wait for
1185 * another frame. If we need that, of course.
1187 if( --sjcd_read_count == 0 ){
1189 * OK, request seems to be precessed. Continue transferring...
1191 if( !sjcd_transfer_is_active ){
1192 while( CURRENT_IS_VALID ){
1194 * Continue transferring.
1196 sjcd_transfer();
1197 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1198 else break;
1201 if( CURRENT_IS_VALID &&
1202 ( CURRENT->sector / 4 < sjcd_next_bn ||
1203 CURRENT->sector / 4 > sjcd_next_bn + SJCD_BUF_SIZ ) ){
1204 #if defined( SJCD_TRACE )
1205 printk( "SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n" );
1206 #endif
1207 sjcd_transfer_state = SJCD_S_STOP;
1208 goto ReSwitch;
1212 * Now we should turn around rather than wait for while.
1214 goto sjcd_s_data;
1216 #if defined( SJCD_GATHER_STAT )
1217 else statistic.data_ticks++;
1218 #endif
1219 break;
1222 case SJCD_S_STOP:{
1223 sjcd_read_count = 0;
1224 sjcd_send_cmd( SCMD_STOP );
1225 sjcd_transfer_state = SJCD_S_STOPPING;
1226 sjcd_transfer_timeout = 500;
1227 #if defined( SJCD_GATHER_STAT )
1228 statistic.stop_ticks++;
1229 #endif
1230 break;
1233 case SJCD_S_STOPPING:{
1234 unsigned char stat;
1236 stat = inb( SJCDPORT( 1 ) );
1237 #if defined( SJCD_TRACE )
1238 printk( "SJCD_S_STOP: status = 0x%02x\n", stat );
1239 #endif
1240 if( SJCD_DATA_AVAILABLE( stat ) ){
1241 int i;
1242 #if defined( SJCD_TRACE )
1243 printk( "SJCD_S_STOP: discard data\n" );
1244 #endif
1246 * Discard all the data from the pipe. Foolish method.
1248 for( i = 2048; i--; ( void )inb( SJCDPORT( 2 ) ) );
1249 sjcd_transfer_timeout = 500;
1250 } else if( SJCD_STATUS_AVAILABLE( stat ) ){
1251 sjcd_load_status();
1252 if( sjcd_status_valid && sjcd_media_is_changed ) {
1253 sjcd_toc_uptodate = 0;
1254 sjcd_invalidate_buffers();
1256 if( CURRENT_IS_VALID ){
1257 if( sjcd_status_valid ) sjcd_transfer_state = SJCD_S_READ;
1258 else sjcd_transfer_state = SJCD_S_START;
1259 } else sjcd_transfer_state = SJCD_S_IDLE;
1260 goto ReSwitch;
1262 #if defined( SJCD_GATHER_STAT )
1263 else statistic.stopping_ticks++;
1264 #endif
1265 break;
1268 default:
1269 printk( "SJCD: poll: invalid state %d\n", sjcd_transfer_state );
1270 return;
1273 if( --sjcd_transfer_timeout == 0 ){
1274 printk( "SJCD: timeout in state %d\n", sjcd_transfer_state );
1275 while( CURRENT_IS_VALID ) end_request( 0 );
1276 sjcd_send_cmd( SCMD_STOP );
1277 sjcd_transfer_state = SJCD_S_IDLE;
1278 goto ReSwitch;
1282 * Get back in some time. 1 should be replaced with count variable to
1283 * avoid unnecessary testings.
1285 SJCD_SET_TIMER( sjcd_poll, 1 );
1288 static void do_sjcd_request( request_queue_t * q ){
1289 #if defined( SJCD_TRACE )
1290 printk( "SJCD: do_sjcd_request(%ld+%ld)\n",
1291 CURRENT->sector, CURRENT->nr_sectors );
1292 #endif
1293 sjcd_transfer_is_active = 1;
1294 while( CURRENT_IS_VALID ){
1296 * Who of us are paranoiac?
1298 if( CURRENT->bh && !buffer_locked(CURRENT->bh) )
1299 panic( DEVICE_NAME ": block not locked" );
1301 sjcd_transfer();
1302 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1303 else {
1304 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1305 if( sjcd_transfer_state == SJCD_S_IDLE ){
1306 if( !sjcd_toc_uptodate ){
1307 if( sjcd_update_toc() < 0 ){
1308 printk( "SJCD: transfer: discard\n" );
1309 while( CURRENT_IS_VALID ) end_request( 0 );
1310 break;
1313 sjcd_transfer_state = SJCD_S_START;
1314 SJCD_SET_TIMER( sjcd_poll, HZ/100 );
1316 break;
1319 sjcd_transfer_is_active = 0;
1320 #if defined( SJCD_TRACE )
1321 printk( "sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1322 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out, sjcd_buf_bn[ sjcd_buf_in ] );
1323 printk( "do_sjcd_request ends\n" );
1324 #endif
1328 * Open the device special file. Check disk is in.
1330 int sjcd_open( struct inode *ip, struct file *fp ){
1332 * Check the presence of device.
1334 if( !sjcd_present ) return( -ENXIO );
1337 * Only read operations are allowed. Really? (:-)
1339 if( fp->f_mode & 2 ) return( -EROFS );
1341 if( sjcd_open_count == 0 ){
1342 int s, sjcd_open_tries;
1343 /* We don't know that, do we? */
1345 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1347 sjcd_mode = 0;
1348 sjcd_door_was_open = 0;
1349 sjcd_transfer_state = SJCD_S_IDLE;
1350 sjcd_invalidate_buffers();
1351 sjcd_status_valid = 0;
1354 * Strict status checking.
1356 for( sjcd_open_tries = 4; --sjcd_open_tries; ){
1357 if( !sjcd_status_valid ) sjcd_get_status();
1358 if( !sjcd_status_valid ){
1359 #if defined( SJCD_DIAGNOSTIC )
1360 printk( "SJCD: open: timed out when check status.\n" );
1361 #endif
1362 return( -EIO );
1363 } else if( !sjcd_media_is_available ){
1364 #if defined( SJCD_DIAGNOSTIC )
1365 printk("SJCD: open: no disk in drive\n");
1366 #endif
1367 if( !sjcd_door_closed ){
1368 sjcd_door_was_open = 1;
1369 #if defined( SJCD_TRACE )
1370 printk("SJCD: open: close the tray\n");
1371 #endif
1372 s = sjcd_tray_close();
1373 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1374 #if defined( SJCD_DIAGNOSTIC )
1375 printk("SJCD: open: tray close attempt failed\n");
1376 #endif
1377 return( -EIO );
1379 continue;
1380 } else return( -EIO );
1382 break;
1384 s = sjcd_tray_lock();
1385 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1386 #if defined( SJCD_DIAGNOSTIC )
1387 printk("SJCD: open: tray lock attempt failed\n");
1388 #endif
1389 return( -EIO );
1391 #if defined( SJCD_TRACE )
1392 printk( "SJCD: open: done\n" );
1393 #endif
1395 #ifdef MODULE
1396 MOD_INC_USE_COUNT;
1397 #endif
1398 ++sjcd_open_count;
1399 return( 0 );
1403 * On close, we flush all sjcd blocks from the buffer cache.
1405 static int sjcd_release( struct inode *inode, struct file *file ){
1406 int s;
1408 #if defined( SJCD_TRACE )
1409 printk( "SJCD: release\n" );
1410 #endif
1411 #ifdef MODULE
1412 MOD_DEC_USE_COUNT;
1413 #endif
1414 if( --sjcd_open_count == 0 ){
1415 sjcd_invalidate_buffers();
1416 s = sjcd_tray_unlock();
1417 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1418 #if defined( SJCD_DIAGNOSTIC )
1419 printk("SJCD: release: tray unlock attempt failed.\n");
1420 #endif
1422 if( sjcd_door_was_open ){
1423 s = sjcd_tray_open();
1424 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1425 #if defined( SJCD_DIAGNOSTIC )
1426 printk("SJCD: release: tray unload attempt failed.\n");
1427 #endif
1431 return 0;
1435 * A list of file operations allowed for this cdrom.
1437 static struct block_device_operations sjcd_fops = {
1438 open: sjcd_open,
1439 release: sjcd_release,
1440 ioctl: sjcd_ioctl,
1441 check_media_change: sjcd_disk_change,
1444 static int blksize = 2048;
1445 static int secsize = 2048;
1448 * Following stuff is intended for initialization of the cdrom. It
1449 * first looks for presence of device. If the device is present, it
1450 * will be reset. Then read the version of the drive and load status.
1451 * The version is two BCD-coded bytes.
1453 static struct {
1454 unsigned char major, minor;
1455 } sjcd_version;
1458 * Test for presence of drive and initialize it. Called at boot time.
1459 * Probe cdrom, find out version and status.
1461 int __init sjcd_init( void ){
1462 int i;
1464 printk(KERN_INFO "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n", SJCD_VERSION_MAJOR,
1465 SJCD_VERSION_MINOR);
1467 #if defined( SJCD_TRACE )
1468 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1469 #endif
1471 hardsect_size[MAJOR_NR] = &secsize;
1472 blksize_size[MAJOR_NR] = &blksize;
1474 if( register_blkdev( MAJOR_NR, "sjcd", &sjcd_fops ) != 0 ){
1475 printk( "SJCD: Unable to get major %d for Sanyo CD-ROM\n", MAJOR_NR );
1476 return( -EIO );
1479 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1480 read_ahead[ MAJOR_NR ] = 4;
1482 if( check_region( sjcd_base, 4 ) ){
1483 printk( "SJCD: Init failed, I/O port (%X) is already in use\n",
1484 sjcd_base );
1485 sjcd_cleanup();
1486 return( -EIO );
1490 * Check for card. Since we are booting now, we can't use standard
1491 * wait algorithm.
1493 printk(KERN_INFO "SJCD: Resetting: " );
1494 sjcd_send_cmd( SCMD_RESET );
1495 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1496 unsigned long timer;
1499 * Wait 10ms approx.
1501 for( timer = jiffies; time_before_eq(jiffies, timer); );
1502 if ( (i % 100) == 0 ) printk( "." );
1503 ( void )sjcd_check_status();
1505 if( i == 0 || sjcd_command_failed ){
1506 printk( " reset failed, no drive found.\n" );
1507 sjcd_cleanup();
1508 return( -EIO );
1509 } else printk( "\n" );
1512 * Get and print out cdrom version.
1514 printk(KERN_INFO "SJCD: Getting version: " );
1515 sjcd_send_cmd( SCMD_GET_VERSION );
1516 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1517 unsigned long timer;
1520 * Wait 10ms approx.
1522 for( timer = jiffies; time_before_eq(jiffies, timer); );
1523 if ( (i % 100) == 0 ) printk( "." );
1524 ( void )sjcd_check_status();
1526 if( i == 0 || sjcd_command_failed ){
1527 printk( " get version failed, no drive found.\n" );
1528 sjcd_cleanup();
1529 return( -EIO );
1532 if( sjcd_load_response( &sjcd_version, sizeof( sjcd_version ) ) == 0 ){
1533 printk( " %1x.%02x\n", ( int )sjcd_version.major,
1534 ( int )sjcd_version.minor );
1535 } else {
1536 printk( " read version failed, no drive found.\n" );
1537 sjcd_cleanup();
1538 return( -EIO );
1542 * Check and print out the tray state. (if it is needed?).
1544 if( !sjcd_status_valid ){
1545 printk(KERN_INFO "SJCD: Getting status: " );
1546 sjcd_send_cmd( SCMD_GET_STATUS );
1547 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1548 unsigned long timer;
1551 * Wait 10ms approx.
1553 for( timer = jiffies; time_before_eq(jiffies, timer); );
1554 if ( (i % 100) == 0 ) printk( "." );
1555 ( void )sjcd_check_status();
1557 if( i == 0 || sjcd_command_failed ){
1558 printk( " get status failed, no drive found.\n" );
1559 sjcd_cleanup();
1560 return( -EIO );
1561 } else printk( "\n" );
1564 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1566 sjcd_present++;
1567 return( 0 );
1570 static int
1571 sjcd_cleanup(void)
1573 if( (unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL) )
1574 printk( "SJCD: cannot unregister device.\n" );
1575 else
1576 release_region( sjcd_base, 4 );
1578 return(0);
1582 void __exit sjcd_exit(void)
1584 if ( sjcd_cleanup() )
1585 printk( "SJCD: module: cannot be removed.\n" );
1586 else
1587 printk(KERN_INFO "SJCD: module: removed.\n");
1590 #ifdef MODULE
1591 module_init(sjcd_init);
1592 #endif
1593 module_exit(sjcd_exit);