Import 2.1.118
[davej-history.git] / drivers / cdrom / sjcd.c
blob6681f2a19b76261395460af13258132fb355692d
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.
51 #define SJCD_VERSION_MAJOR 1
52 #define SJCD_VERSION_MINOR 6
54 #ifdef MODULE
55 #include <linux/module.h>
56 #endif /* MODULE */
58 #include <linux/errno.h>
59 #include <linux/sched.h>
60 #include <linux/mm.h>
61 #include <linux/timer.h>
62 #include <linux/fs.h>
63 #include <linux/kernel.h>
64 #include <linux/cdrom.h>
65 #include <linux/ioport.h>
66 #include <linux/string.h>
67 #include <linux/major.h>
68 #include <linux/init.h>
70 #include <asm/system.h>
71 #include <asm/io.h>
72 #include <asm/uaccess.h>
74 #define MAJOR_NR SANYO_CDROM_MAJOR
75 #include <linux/blk.h>
76 #include "sjcd.h"
78 static int sjcd_present = 0;
80 #define SJCD_BUF_SIZ 32 /* cdr-h94a has internal 64K buffer */
83 * buffer for block size conversion
85 static char sjcd_buf[ 2048 * SJCD_BUF_SIZ ];
86 static volatile int sjcd_buf_bn[ SJCD_BUF_SIZ ], sjcd_next_bn;
87 static volatile int sjcd_buf_in, sjcd_buf_out = -1;
90 * Status.
92 static unsigned short sjcd_status_valid = 0;
93 static unsigned short sjcd_door_closed;
94 static unsigned short sjcd_door_was_open;
95 static unsigned short sjcd_media_is_available;
96 static unsigned short sjcd_media_is_changed;
97 static unsigned short sjcd_toc_uptodate = 0;
98 static unsigned short sjcd_command_failed;
99 static volatile unsigned char sjcd_completion_status = 0;
100 static volatile unsigned char sjcd_completion_error = 0;
101 static unsigned short sjcd_command_is_in_progress = 0;
102 static unsigned short sjcd_error_reported = 0;
104 static int sjcd_open_count;
106 static int sjcd_audio_status;
107 static struct sjcd_play_msf sjcd_playing;
109 static int sjcd_base = SJCD_BASE_ADDR;
111 #ifdef MODULE
112 MODULE_PARM(sjcd_base, "i");
113 #endif
115 static struct wait_queue *sjcd_waitq = NULL;
118 * Data transfer.
120 static volatile unsigned short sjcd_transfer_is_active = 0;
122 enum sjcd_transfer_state {
123 SJCD_S_IDLE = 0,
124 SJCD_S_START = 1,
125 SJCD_S_MODE = 2,
126 SJCD_S_READ = 3,
127 SJCD_S_DATA = 4,
128 SJCD_S_STOP = 5,
129 SJCD_S_STOPPING = 6
131 static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
132 static long sjcd_transfer_timeout = 0;
133 static int sjcd_read_count = 0;
134 static unsigned char sjcd_mode = 0;
136 #define SJCD_READ_TIMEOUT 5000
138 #if defined( SJCD_GATHER_STAT )
140 * Statistic.
142 static struct sjcd_stat statistic;
143 #endif
146 * Timer.
148 static struct timer_list sjcd_delay_timer = { NULL, NULL, 0, 0, NULL };
150 #define SJCD_SET_TIMER( func, tmout ) \
151 ( sjcd_delay_timer.expires = jiffies+tmout, \
152 sjcd_delay_timer.function = ( void * )func, \
153 add_timer( &sjcd_delay_timer ) )
155 #define CLEAR_TIMER del_timer( &sjcd_delay_timer )
157 static int sjcd_cleanup(void);
160 * Set up device, i.e., use command line data to set
161 * base address.
163 __initfunc(void sjcd_setup( char *str, int *ints ))
165 if (ints[0] > 0)
166 sjcd_base = ints[1];
170 * Special converters.
172 static unsigned char bin2bcd( int bin ){
173 int u, v;
175 u = bin % 10; v = bin / 10;
176 return( u | ( v << 4 ) );
179 static int bcd2bin( unsigned char bcd ){
180 return( ( bcd >> 4 ) * 10 + ( bcd & 0x0F ) );
183 static long msf2hsg( struct msf *mp ){
184 return( bcd2bin( mp->frame ) + bcd2bin( mp->sec ) * 75
185 + bcd2bin( mp->min ) * 4500 - 150 );
188 static void hsg2msf( long hsg, struct msf *msf ){
189 hsg += 150; msf->min = hsg / 4500;
190 hsg %= 4500; msf->sec = hsg / 75; msf->frame = hsg % 75;
191 msf->min = bin2bcd( msf->min ); /* convert to BCD */
192 msf->sec = bin2bcd( msf->sec );
193 msf->frame = bin2bcd( msf->frame );
197 * Send a command to cdrom. Invalidate status.
199 static void sjcd_send_cmd( unsigned char cmd ){
200 #if defined( SJCD_TRACE )
201 printk( "SJCD: send_cmd( 0x%x )\n", cmd );
202 #endif
203 outb( cmd, SJCDPORT( 0 ) );
204 sjcd_command_is_in_progress = 1;
205 sjcd_status_valid = 0;
206 sjcd_command_failed = 0;
210 * Send a command with one arg to cdrom. Invalidate status.
212 static void sjcd_send_1_cmd( unsigned char cmd, unsigned char a ){
213 #if defined( SJCD_TRACE )
214 printk( "SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a );
215 #endif
216 outb( cmd, SJCDPORT( 0 ) );
217 outb( a, SJCDPORT( 0 ) );
218 sjcd_command_is_in_progress = 1;
219 sjcd_status_valid = 0;
220 sjcd_command_failed = 0;
224 * Send a command with four args to cdrom. Invalidate status.
226 static void sjcd_send_4_cmd( unsigned char cmd, unsigned char a,
227 unsigned char b, unsigned char c, unsigned char d ){
228 #if defined( SJCD_TRACE )
229 printk( "SJCD: send_4_cmd( 0x%x )\n", cmd );
230 #endif
231 outb( cmd, SJCDPORT( 0 ) );
232 outb( a, SJCDPORT( 0 ) );
233 outb( b, SJCDPORT( 0 ) );
234 outb( c, SJCDPORT( 0 ) );
235 outb( d, SJCDPORT( 0 ) );
236 sjcd_command_is_in_progress = 1;
237 sjcd_status_valid = 0;
238 sjcd_command_failed = 0;
242 * Send a play or read command to cdrom. Invalidate Status.
244 static void sjcd_send_6_cmd( unsigned char cmd, struct sjcd_play_msf *pms ){
245 #if defined( SJCD_TRACE )
246 printk( "SJCD: send_long_cmd( 0x%x )\n", cmd );
247 #endif
248 outb( cmd, SJCDPORT( 0 ) );
249 outb( pms->start.min, SJCDPORT( 0 ) );
250 outb( pms->start.sec, SJCDPORT( 0 ) );
251 outb( pms->start.frame, SJCDPORT( 0 ) );
252 outb( pms->end.min, SJCDPORT( 0 ) );
253 outb( pms->end.sec, SJCDPORT( 0 ) );
254 outb( pms->end.frame, SJCDPORT( 0 ) );
255 sjcd_command_is_in_progress = 1;
256 sjcd_status_valid = 0;
257 sjcd_command_failed = 0;
261 * Get a value from the data port. Should not block, so we use a little
262 * wait for a while. Returns 0 if OK.
264 static int sjcd_load_response( void *buf, int len ){
265 unsigned char *resp = ( unsigned char * )buf;
267 for( ; len; --len ){
268 int i;
269 for( i = 200; i-- && !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ); );
270 if( i > 0 ) *resp++ = ( unsigned char )inb( SJCDPORT( 0 ) );
271 else break;
273 return( len );
277 * Load and parse command completion status (drive info byte and maybe error).
278 * Sorry, no error classification yet.
280 static void sjcd_load_status( void ){
281 sjcd_media_is_changed = 0;
282 sjcd_completion_error = 0;
283 sjcd_completion_status = inb( SJCDPORT( 0 ) );
284 if( sjcd_completion_status & SST_DOOR_OPENED ){
285 sjcd_door_closed = sjcd_media_is_available = 0;
286 } else {
287 sjcd_door_closed = 1;
288 if( sjcd_completion_status & SST_MEDIA_CHANGED )
289 sjcd_media_is_available = sjcd_media_is_changed = 1;
290 else if( sjcd_completion_status & 0x0F ){
292 * OK, we seem to catch an error ...
294 while( !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) );
295 sjcd_completion_error = inb( SJCDPORT( 0 ) );
296 if( ( sjcd_completion_status & 0x08 ) &&
297 ( sjcd_completion_error & 0x40 ) )
298 sjcd_media_is_available = 0;
299 else sjcd_command_failed = 1;
300 } else sjcd_media_is_available = 1;
303 * Ok, status loaded successfully.
305 sjcd_status_valid = 1, sjcd_error_reported = 0;
306 sjcd_command_is_in_progress = 0;
309 * If the disk is changed, the TOC is not valid.
311 if( sjcd_media_is_changed ) sjcd_toc_uptodate = 0;
312 #if defined( SJCD_TRACE )
313 printk( "SJCD: status %02x.%02x loaded.\n",
314 ( int )sjcd_completion_status, ( int )sjcd_completion_error );
315 #endif
319 * Read status from cdrom. Check to see if the status is available.
321 static int sjcd_check_status( void ){
323 * Try to load the response from cdrom into buffer.
325 if( SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) ){
326 sjcd_load_status();
327 return( 1 );
328 } else {
330 * No status is available.
332 return( 0 );
337 * This is just timeout counter, and nothing more. Surprised ? :-)
339 static volatile long sjcd_status_timeout;
342 * We need about 10 seconds to wait. The longest command takes about 5 seconds
343 * to probe the disk (usually after tray closed or drive reset). Other values
344 * should be thought of for other commands.
346 #define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
348 static void sjcd_status_timer( void ){
349 if( sjcd_check_status() ){
351 * The command completed and status is loaded, stop waiting.
353 wake_up( &sjcd_waitq );
354 } else if( --sjcd_status_timeout <= 0 ){
356 * We are timed out.
358 wake_up( &sjcd_waitq );
359 } else {
361 * We have still some time to wait. Try again.
363 SJCD_SET_TIMER( sjcd_status_timer, 1 );
368 * Wait for status for 10 sec approx. Returns non-positive when timed out.
369 * Should not be used while reading data CDs.
371 static int sjcd_wait_for_status( void ){
372 sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
373 SJCD_SET_TIMER( sjcd_status_timer, 1 );
374 sleep_on( &sjcd_waitq );
375 #if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
376 if( sjcd_status_timeout <= 0 )
377 printk( "SJCD: Error Wait For Status.\n" );
378 #endif
379 return( sjcd_status_timeout );
382 static int sjcd_receive_status( void ){
383 int i;
384 #if defined( SJCD_TRACE )
385 printk( "SJCD: receive_status\n" );
386 #endif
388 * Wait a bit for status available.
390 for( i = 200; i-- && ( sjcd_check_status() == 0 ); );
391 if( i < 0 ){
392 #if defined( SJCD_TRACE )
393 printk( "SJCD: long wait for status\n" );
394 #endif
395 if( sjcd_wait_for_status() <= 0 )
396 printk( "SJCD: Timeout when read status.\n" );
397 else i = 0;
399 return( i );
403 * Load the status. Issue get status command and wait for status available.
405 static void sjcd_get_status( void ){
406 #if defined( SJCD_TRACE )
407 printk( "SJCD: get_status\n" );
408 #endif
409 sjcd_send_cmd( SCMD_GET_STATUS );
410 sjcd_receive_status();
414 * Check the drive if the disk is changed. Should be revised.
416 static int sjcd_disk_change( kdev_t full_dev ){
417 #if 0
418 printk( "SJCD: sjcd_disk_change( 0x%x )\n", full_dev );
419 #endif
420 if( MINOR( full_dev ) > 0 ){
421 printk( "SJCD: request error: invalid device minor.\n" );
422 return 0;
424 if( !sjcd_command_is_in_progress )
425 sjcd_get_status();
426 return( sjcd_status_valid ? sjcd_media_is_changed : 0 );
430 * Read the table of contents (TOC) and TOC header if necessary.
431 * We assume that the drive contains no more than 99 toc entries.
433 static struct sjcd_hw_disk_info sjcd_table_of_contents[ SJCD_MAX_TRACKS ];
434 static unsigned char sjcd_first_track_no, sjcd_last_track_no;
435 #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
437 static int sjcd_update_toc( void ){
438 struct sjcd_hw_disk_info info;
439 int i;
440 #if defined( SJCD_TRACE )
441 printk( "SJCD: update toc:\n" );
442 #endif
444 * check to see if we need to do anything
446 if( sjcd_toc_uptodate ) return( 0 );
449 * Get the TOC start information.
451 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK );
452 sjcd_receive_status();
454 if( !sjcd_status_valid ){
455 printk( "SJCD: cannot load status.\n" );
456 return( -1 );
459 if( !sjcd_media_is_available ){
460 printk( "SJCD: no disk in drive\n" );
461 return( -1 );
464 if( !sjcd_command_failed ){
465 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
466 printk( "SJCD: cannot load response about TOC start.\n" );
467 return( -1 );
469 sjcd_first_track_no = bcd2bin( info.un.track_no );
470 } else {
471 printk( "SJCD: get first failed\n" );
472 return( -1 );
474 #if defined( SJCD_TRACE )
475 printk( "SJCD: TOC start 0x%02x ", sjcd_first_track_no );
476 #endif
478 * Get the TOC finish information.
480 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK );
481 sjcd_receive_status();
483 if( !sjcd_status_valid ){
484 printk( "SJCD: cannot load status.\n" );
485 return( -1 );
488 if( !sjcd_media_is_available ){
489 printk( "SJCD: no disk in drive\n" );
490 return( -1 );
493 if( !sjcd_command_failed ){
494 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
495 printk( "SJCD: cannot load response about TOC finish.\n" );
496 return( -1 );
498 sjcd_last_track_no = bcd2bin( info.un.track_no );
499 } else {
500 printk( "SJCD: get last failed\n" );
501 return( -1 );
503 #if defined( SJCD_TRACE )
504 printk( "SJCD: TOC finish 0x%02x ", sjcd_last_track_no );
505 #endif
506 for( i = sjcd_first_track_no; i <= sjcd_last_track_no; i++ ){
508 * Get the first track information.
510 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, bin2bcd( i ) );
511 sjcd_receive_status();
513 if( !sjcd_status_valid ){
514 printk( "SJCD: cannot load status.\n" );
515 return( -1 );
518 if( !sjcd_media_is_available ){
519 printk( "SJCD: no disk in drive\n" );
520 return( -1 );
523 if( !sjcd_command_failed ){
524 if( sjcd_load_response( &sjcd_table_of_contents[ i ],
525 sizeof( struct sjcd_hw_disk_info ) ) != 0 ){
526 printk( "SJCD: cannot load info for %d track\n", i );
527 return( -1 );
529 } else {
530 printk( "SJCD: get info %d failed\n", i );
531 return( -1 );
536 * Get the disk length info.
538 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE );
539 sjcd_receive_status();
541 if( !sjcd_status_valid ){
542 printk( "SJCD: cannot load status.\n" );
543 return( -1 );
546 if( !sjcd_media_is_available ){
547 printk( "SJCD: no disk in drive\n" );
548 return( -1 );
551 if( !sjcd_command_failed ){
552 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
553 printk( "SJCD: cannot load response about disk size.\n" );
554 return( -1 );
556 sjcd_disk_length.min = info.un.track_msf.min;
557 sjcd_disk_length.sec = info.un.track_msf.sec;
558 sjcd_disk_length.frame = info.un.track_msf.frame;
559 } else {
560 printk( "SJCD: get size failed\n" );
561 return( 1 );
563 #if defined( SJCD_TRACE )
564 printk( "SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
565 sjcd_disk_length.sec, sjcd_disk_length.frame );
566 #endif
567 return( 0 );
571 * Load subchannel information.
573 static int sjcd_get_q_info( struct sjcd_hw_qinfo *qp ){
574 int s;
575 #if defined( SJCD_TRACE )
576 printk( "SJCD: load sub q\n" );
577 #endif
578 sjcd_send_cmd( SCMD_GET_QINFO );
579 s = sjcd_receive_status();
580 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ){
581 sjcd_send_cmd( 0xF2 );
582 s = sjcd_receive_status();
583 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
584 sjcd_send_cmd( SCMD_GET_QINFO );
585 s = sjcd_receive_status();
586 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
588 if( sjcd_media_is_available )
589 if( sjcd_load_response( qp, sizeof( *qp ) ) == 0 ) return( 0 );
590 return( -1 );
594 * Start playing from the specified position.
596 static int sjcd_play( struct sjcd_play_msf *mp ){
597 struct sjcd_play_msf msf;
600 * Turn the device to play mode.
602 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_PLAY );
603 if( sjcd_receive_status() < 0 ) return( -1 );
606 * Seek to the starting point.
608 msf.start = mp->start;
609 msf.end.min = msf.end.sec = msf.end.frame = 0x00;
610 sjcd_send_6_cmd( SCMD_SEEK, &msf );
611 if( sjcd_receive_status() < 0 ) return( -1 );
614 * Start playing.
616 sjcd_send_6_cmd( SCMD_PLAY, mp );
617 return( sjcd_receive_status() );
621 * Tray control functions.
623 static int sjcd_tray_close( void ){
624 #if defined( SJCD_TRACE )
625 printk( "SJCD: tray_close\n" );
626 #endif
627 sjcd_send_cmd( SCMD_CLOSE_TRAY );
628 return( sjcd_receive_status() );
631 static int sjcd_tray_lock( void ){
632 #if defined( SJCD_TRACE )
633 printk( "SJCD: tray_lock\n" );
634 #endif
635 sjcd_send_cmd( SCMD_LOCK_TRAY );
636 return( sjcd_receive_status() );
639 static int sjcd_tray_unlock( void ){
640 #if defined( SJCD_TRACE )
641 printk( "SJCD: tray_unlock\n" );
642 #endif
643 sjcd_send_cmd( SCMD_UNLOCK_TRAY );
644 return( sjcd_receive_status() );
647 static int sjcd_tray_open( void ){
648 #if defined( SJCD_TRACE )
649 printk( "SJCD: tray_open\n" );
650 #endif
651 sjcd_send_cmd( SCMD_EJECT_TRAY );
652 return( sjcd_receive_status() );
656 * Do some user commands.
658 static int sjcd_ioctl( struct inode *ip, struct file *fp,
659 unsigned int cmd, unsigned long arg ){
660 #if defined( SJCD_TRACE )
661 printk( "SJCD:ioctl\n" );
662 #endif
664 if( ip == NULL ) return( -EINVAL );
666 sjcd_get_status();
667 if( !sjcd_status_valid ) return( -EIO );
668 if( sjcd_update_toc() < 0 ) return( -EIO );
670 switch( cmd ){
671 case CDROMSTART:{
672 #if defined( SJCD_TRACE )
673 printk( "SJCD: ioctl: start\n" );
674 #endif
675 return( 0 );
678 case CDROMSTOP:{
679 #if defined( SJCD_TRACE )
680 printk( "SJCD: ioctl: stop\n" );
681 #endif
682 sjcd_send_cmd( SCMD_PAUSE );
683 ( void )sjcd_receive_status();
684 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
685 return( 0 );
688 case CDROMPAUSE:{
689 struct sjcd_hw_qinfo q_info;
690 #if defined( SJCD_TRACE )
691 printk( "SJCD: ioctl: pause\n" );
692 #endif
693 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
694 sjcd_send_cmd( SCMD_PAUSE );
695 ( void )sjcd_receive_status();
696 if( sjcd_get_q_info( &q_info ) < 0 ){
697 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
698 } else {
699 sjcd_audio_status = CDROM_AUDIO_PAUSED;
700 sjcd_playing.start = q_info.abs;
702 return( 0 );
703 } else return( -EINVAL );
706 case CDROMRESUME:{
707 #if defined( SJCD_TRACE )
708 printk( "SJCD: ioctl: resume\n" );
709 #endif
710 if( sjcd_audio_status == CDROM_AUDIO_PAUSED ){
712 * continue play starting at saved location
714 if( sjcd_play( &sjcd_playing ) < 0 ){
715 sjcd_audio_status = CDROM_AUDIO_ERROR;
716 return( -EIO );
717 } else {
718 sjcd_audio_status = CDROM_AUDIO_PLAY;
719 return( 0 );
721 } else return( -EINVAL );
724 case CDROMPLAYTRKIND:{
725 struct cdrom_ti ti; int s;
726 #if defined( SJCD_TRACE )
727 printk( "SJCD: ioctl: playtrkind\n" );
728 #endif
729 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( ti ) ) ) == 0 ){
730 copy_from_user( &ti, (void *)arg, sizeof( ti ) );
732 if( ti.cdti_trk0 < sjcd_first_track_no ) return( -EINVAL );
733 if( ti.cdti_trk1 > sjcd_last_track_no )
734 ti.cdti_trk1 = sjcd_last_track_no;
735 if( ti.cdti_trk0 > ti.cdti_trk1 ) return( -EINVAL );
737 sjcd_playing.start = sjcd_table_of_contents[ ti.cdti_trk0 ].un.track_msf;
738 sjcd_playing.end = ( ti.cdti_trk1 < sjcd_last_track_no ) ?
739 sjcd_table_of_contents[ ti.cdti_trk1 + 1 ].un.track_msf :
740 sjcd_table_of_contents[ 0 ].un.track_msf;
742 if( sjcd_play( &sjcd_playing ) < 0 ){
743 sjcd_audio_status = CDROM_AUDIO_ERROR;
744 return( -EIO );
745 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
747 return( s );
750 case CDROMPLAYMSF:{
751 struct cdrom_msf sjcd_msf; int s;
752 #if defined( SJCD_TRACE )
753 printk( "SJCD: ioctl: playmsf\n" );
754 #endif
755 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( sjcd_msf ) ) ) == 0 ){
756 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
757 sjcd_send_cmd( SCMD_PAUSE );
758 ( void )sjcd_receive_status();
759 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
762 copy_from_user( &sjcd_msf, (void *)arg, sizeof( sjcd_msf ) );
764 sjcd_playing.start.min = bin2bcd( sjcd_msf.cdmsf_min0 );
765 sjcd_playing.start.sec = bin2bcd( sjcd_msf.cdmsf_sec0 );
766 sjcd_playing.start.frame = bin2bcd( sjcd_msf.cdmsf_frame0 );
767 sjcd_playing.end.min = bin2bcd( sjcd_msf.cdmsf_min1 );
768 sjcd_playing.end.sec = bin2bcd( sjcd_msf.cdmsf_sec1 );
769 sjcd_playing.end.frame = bin2bcd( sjcd_msf.cdmsf_frame1 );
771 if( sjcd_play( &sjcd_playing ) < 0 ){
772 sjcd_audio_status = CDROM_AUDIO_ERROR;
773 return( -EIO );
774 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
776 return( s );
779 case CDROMREADTOCHDR:{
780 struct cdrom_tochdr toc_header; int s;
781 #if defined (SJCD_TRACE )
782 printk( "SJCD: ioctl: readtocheader\n" );
783 #endif
784 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_header ) ) ) == 0 ){
785 toc_header.cdth_trk0 = sjcd_first_track_no;
786 toc_header.cdth_trk1 = sjcd_last_track_no;
787 copy_to_user( (void *)arg, &toc_header, sizeof( toc_header ) );
789 return( s );
792 case CDROMREADTOCENTRY:{
793 struct cdrom_tocentry toc_entry; int s;
794 #if defined( SJCD_TRACE )
795 printk( "SJCD: ioctl: readtocentry\n" );
796 #endif
797 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_entry ) ) ) == 0 ){
798 struct sjcd_hw_disk_info *tp;
800 copy_from_user( &toc_entry, (void *)arg, sizeof( toc_entry ) );
802 if( toc_entry.cdte_track == CDROM_LEADOUT )
803 tp = &sjcd_table_of_contents[ 0 ];
804 else if( toc_entry.cdte_track < sjcd_first_track_no ) return( -EINVAL );
805 else if( toc_entry.cdte_track > sjcd_last_track_no ) return( -EINVAL );
806 else tp = &sjcd_table_of_contents[ toc_entry.cdte_track ];
808 toc_entry.cdte_adr = tp->track_control & 0x0F;
809 toc_entry.cdte_ctrl = tp->track_control >> 4;
811 switch( toc_entry.cdte_format ){
812 case CDROM_LBA:
813 toc_entry.cdte_addr.lba = msf2hsg( &( tp->un.track_msf ) );
814 break;
815 case CDROM_MSF:
816 toc_entry.cdte_addr.msf.minute = bcd2bin( tp->un.track_msf.min );
817 toc_entry.cdte_addr.msf.second = bcd2bin( tp->un.track_msf.sec );
818 toc_entry.cdte_addr.msf.frame = bcd2bin( tp->un.track_msf.frame );
819 break;
820 default: return( -EINVAL );
822 copy_to_user( (void *)arg, &toc_entry, sizeof( toc_entry ) );
824 return( s );
827 case CDROMSUBCHNL:{
828 struct cdrom_subchnl subchnl; int s;
829 #if defined( SJCD_TRACE )
830 printk( "SJCD: ioctl: subchnl\n" );
831 #endif
832 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( subchnl ) ) ) == 0 ){
833 struct sjcd_hw_qinfo q_info;
835 copy_from_user( &subchnl, (void *)arg, sizeof( subchnl ) );
836 if( sjcd_get_q_info( &q_info ) < 0 ) return( -EIO );
838 subchnl.cdsc_audiostatus = sjcd_audio_status;
839 subchnl.cdsc_adr = q_info.track_control & 0x0F;
840 subchnl.cdsc_ctrl = q_info.track_control >> 4;
841 subchnl.cdsc_trk = bcd2bin( q_info.track_no );
842 subchnl.cdsc_ind = bcd2bin( q_info.x );
844 switch( subchnl.cdsc_format ){
845 case CDROM_LBA:
846 subchnl.cdsc_absaddr.lba = msf2hsg( &( q_info.abs ) );
847 subchnl.cdsc_reladdr.lba = msf2hsg( &( q_info.rel ) );
848 break;
849 case CDROM_MSF:
850 subchnl.cdsc_absaddr.msf.minute = bcd2bin( q_info.abs.min );
851 subchnl.cdsc_absaddr.msf.second = bcd2bin( q_info.abs.sec );
852 subchnl.cdsc_absaddr.msf.frame = bcd2bin( q_info.abs.frame );
853 subchnl.cdsc_reladdr.msf.minute = bcd2bin( q_info.rel.min );
854 subchnl.cdsc_reladdr.msf.second = bcd2bin( q_info.rel.sec );
855 subchnl.cdsc_reladdr.msf.frame = bcd2bin( q_info.rel.frame );
856 break;
857 default: return( -EINVAL );
859 copy_to_user( (void *)arg, &subchnl, sizeof( subchnl ) );
861 return( s );
864 case CDROMVOLCTRL:{
865 struct cdrom_volctrl vol_ctrl; int s;
866 #if defined( SJCD_TRACE )
867 printk( "SJCD: ioctl: volctrl\n" );
868 #endif
869 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( vol_ctrl ) ) ) == 0 ){
870 unsigned char dummy[ 4 ];
872 copy_from_user( &vol_ctrl, (void *)arg, sizeof( vol_ctrl ) );
873 sjcd_send_4_cmd( SCMD_SET_VOLUME, vol_ctrl.channel0, 0xFF,
874 vol_ctrl.channel1, 0xFF );
875 if( sjcd_receive_status() < 0 ) return( -EIO );
876 ( void )sjcd_load_response( dummy, 4 );
878 return( s );
881 case CDROMEJECT:{
882 #if defined( SJCD_TRACE )
883 printk( "SJCD: ioctl: eject\n" );
884 #endif
885 if( !sjcd_command_is_in_progress ){
886 sjcd_tray_unlock();
887 sjcd_send_cmd( SCMD_EJECT_TRAY );
888 ( void )sjcd_receive_status();
890 return( 0 );
893 #if defined( SJCD_GATHER_STAT )
894 case 0xABCD:{
895 int s;
896 #if defined( SJCD_TRACE )
897 printk( "SJCD: ioctl: statistic\n" );
898 #endif
899 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( statistic ) ) ) == 0 )
900 copy_to_user( (void *)arg, &statistic, sizeof( statistic ) );
901 return( s );
903 #endif
905 default:
906 return( -EINVAL );
911 * Invalidate internal buffers of the driver.
913 static void sjcd_invalidate_buffers( void ){
914 int i;
915 for( i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[ i++ ] = -1 );
916 sjcd_buf_out = -1;
920 * Take care of the different block sizes between cdrom and Linux.
921 * When Linux gets variable block sizes this will probably go away.
924 #define CURRENT_IS_VALID \
925 ( CURRENT != NULL && MAJOR( CURRENT->rq_dev ) == MAJOR_NR && \
926 CURRENT->cmd == READ && CURRENT->sector != -1 )
928 static void sjcd_transfer( void ){
929 #if defined( SJCD_TRACE )
930 printk( "SJCD: transfer:\n" );
931 #endif
932 if( CURRENT_IS_VALID ){
933 while( CURRENT->nr_sectors ){
934 int i, bn = CURRENT->sector / 4;
935 for( i = 0; i < SJCD_BUF_SIZ && sjcd_buf_bn[ i ] != bn; i++ );
936 if( i < SJCD_BUF_SIZ ){
937 int offs = ( i * 4 + ( CURRENT->sector & 3 ) ) * 512;
938 int nr_sectors = 4 - ( CURRENT->sector & 3 );
939 if( sjcd_buf_out != i ){
940 sjcd_buf_out = i;
941 if( sjcd_buf_bn[ i ] != bn ){
942 sjcd_buf_out = -1;
943 continue;
946 if( nr_sectors > CURRENT->nr_sectors )
947 nr_sectors = CURRENT->nr_sectors;
948 #if defined( SJCD_TRACE )
949 printk( "SJCD: copy out\n" );
950 #endif
951 memcpy( CURRENT->buffer, sjcd_buf + offs, nr_sectors * 512 );
952 CURRENT->nr_sectors -= nr_sectors;
953 CURRENT->sector += nr_sectors;
954 CURRENT->buffer += nr_sectors * 512;
955 } else {
956 sjcd_buf_out = -1;
957 break;
961 #if defined( SJCD_TRACE )
962 printk( "SJCD: transfer: done\n" );
963 #endif
966 static void sjcd_poll( void ){
967 #if defined( SJCD_GATHER_STAT )
969 * Update total number of ticks.
971 statistic.ticks++;
972 statistic.tticks[ sjcd_transfer_state ]++;
973 #endif
975 ReSwitch: switch( sjcd_transfer_state ){
977 case SJCD_S_IDLE:{
978 #if defined( SJCD_GATHER_STAT )
979 statistic.idle_ticks++;
980 #endif
981 #if defined( SJCD_TRACE )
982 printk( "SJCD_S_IDLE\n" );
983 #endif
984 return;
987 case SJCD_S_START:{
988 #if defined( SJCD_GATHER_STAT )
989 statistic.start_ticks++;
990 #endif
991 sjcd_send_cmd( SCMD_GET_STATUS );
992 sjcd_transfer_state =
993 sjcd_mode == SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
994 sjcd_transfer_timeout = 500;
995 #if defined( SJCD_TRACE )
996 printk( "SJCD_S_START: goto SJCD_S_%s mode\n",
997 sjcd_transfer_state == SJCD_S_READ ? "READ" : "MODE" );
998 #endif
999 break;
1002 case SJCD_S_MODE:{
1003 if( sjcd_check_status() ){
1005 * Previous command is completed.
1007 if( !sjcd_status_valid || sjcd_command_failed ){
1008 #if defined( SJCD_TRACE )
1009 printk( "SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1010 #endif
1011 sjcd_transfer_state = SJCD_S_STOP;
1012 goto ReSwitch;
1015 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1016 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_COOKED );
1017 sjcd_transfer_state = SJCD_S_READ; sjcd_transfer_timeout = 1000;
1018 #if defined( SJCD_TRACE )
1019 printk( "SJCD_S_MODE: goto SJCD_S_READ mode\n" );
1020 #endif
1022 #if defined( SJCD_GATHER_STAT )
1023 else statistic.mode_ticks++;
1024 #endif
1025 break;
1028 case SJCD_S_READ:{
1029 if( sjcd_status_valid ? 1 : sjcd_check_status() ){
1031 * Previous command is completed.
1033 if( !sjcd_status_valid || sjcd_command_failed ){
1034 #if defined( SJCD_TRACE )
1035 printk( "SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1036 #endif
1037 sjcd_transfer_state = SJCD_S_STOP;
1038 goto ReSwitch;
1040 if( !sjcd_media_is_available ){
1041 #if defined( SJCD_TRACE )
1042 printk( "SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n" );
1043 #endif
1044 sjcd_transfer_state = SJCD_S_STOP;
1045 goto ReSwitch;
1047 if( sjcd_mode != SCMD_MODE_COOKED ){
1049 * We seem to come from set mode. So discard one byte of result.
1051 if( sjcd_load_response( &sjcd_mode, 1 ) != 0 ){
1052 #if defined( SJCD_TRACE )
1053 printk( "SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n" );
1054 #endif
1055 sjcd_transfer_state = SJCD_S_STOP;
1056 goto ReSwitch;
1058 if( sjcd_mode != SCMD_MODE_COOKED ){
1059 #if defined( SJCD_TRACE )
1060 printk( "SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n" );
1061 #endif
1062 sjcd_transfer_state = SJCD_S_STOP;
1063 goto ReSwitch;
1067 if( CURRENT_IS_VALID ){
1068 struct sjcd_play_msf msf;
1070 sjcd_next_bn = CURRENT->sector / 4;
1071 hsg2msf( sjcd_next_bn, &msf.start );
1072 msf.end.min = 0; msf.end.sec = 0;
1073 msf.end.frame = sjcd_read_count = SJCD_BUF_SIZ;
1074 #if defined( SJCD_TRACE )
1075 printk( "SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1076 msf.start.min, msf.start.sec, msf.start.frame,
1077 msf.end.min, msf.end.sec, msf.end.frame );
1078 printk( "sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n", \
1079 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1080 sjcd_buf_bn[ sjcd_buf_in ] );
1081 #endif
1082 sjcd_send_6_cmd( SCMD_DATA_READ, &msf );
1083 sjcd_transfer_state = SJCD_S_DATA;
1084 sjcd_transfer_timeout = 500;
1085 #if defined( SJCD_TRACE )
1086 printk( "SJCD_S_READ: go to SJCD_S_DATA mode\n" );
1087 #endif
1088 } else {
1089 #if defined( SJCD_TRACE )
1090 printk( "SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n" );
1091 #endif
1092 sjcd_transfer_state = SJCD_S_STOP;
1093 goto ReSwitch;
1096 #if defined( SJCD_GATHER_STAT )
1097 else statistic.read_ticks++;
1098 #endif
1099 break;
1102 case SJCD_S_DATA:{
1103 unsigned char stat;
1105 sjcd_s_data: stat = inb( SJCDPORT( 1 ) );
1106 #if defined( SJCD_TRACE )
1107 printk( "SJCD_S_DATA: status = 0x%02x\n", stat );
1108 #endif
1109 if( SJCD_STATUS_AVAILABLE( stat ) ){
1111 * No data is waiting for us in the drive buffer. Status of operation
1112 * completion is available. Read and parse it.
1114 sjcd_load_status();
1116 if( !sjcd_status_valid || sjcd_command_failed ){
1117 #if defined( SJCD_TRACE )
1118 printk( "SJCD: read block %d failed, maybe audio disk? Giving up\n",
1119 sjcd_next_bn );
1120 #endif
1121 if( CURRENT_IS_VALID ) end_request( 0 );
1122 #if defined( SJCD_TRACE )
1123 printk( "SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n" );
1124 #endif
1125 sjcd_transfer_state = SJCD_S_STOP;
1126 goto ReSwitch;
1129 if( !sjcd_media_is_available ){
1130 printk( "SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n" );
1131 sjcd_transfer_state = SJCD_S_STOP;
1132 goto ReSwitch;
1135 sjcd_transfer_state = SJCD_S_READ;
1136 goto ReSwitch;
1137 } else if( SJCD_DATA_AVAILABLE( stat ) ){
1139 * One frame is read into device buffer. We must copy it to our memory.
1140 * Otherwise cdrom hangs up. Check to see if we have something to copy
1141 * to.
1143 if( !CURRENT_IS_VALID && sjcd_buf_in == sjcd_buf_out ){
1144 #if defined( SJCD_TRACE )
1145 printk( "SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n" );
1146 printk( " ... all the date would be discarded\n" );
1147 #endif
1148 sjcd_transfer_state = SJCD_S_STOP;
1149 goto ReSwitch;
1153 * Everything seems to be OK. Just read the frame and recalculate
1154 * indices.
1156 sjcd_buf_bn[ sjcd_buf_in ] = -1; /* ??? */
1157 insb( SJCDPORT( 2 ), sjcd_buf + 2048 * sjcd_buf_in, 2048 );
1158 #if defined( SJCD_TRACE )
1159 printk( "SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1160 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1161 sjcd_buf_bn[ sjcd_buf_in ] );
1162 #endif
1163 sjcd_buf_bn[ sjcd_buf_in ] = sjcd_next_bn++;
1164 if( sjcd_buf_out == -1 ) sjcd_buf_out = sjcd_buf_in;
1165 if( ++sjcd_buf_in == SJCD_BUF_SIZ ) sjcd_buf_in = 0;
1168 * Only one frame is ready at time. So we should turn over to wait for
1169 * another frame. If we need that, of course.
1171 if( --sjcd_read_count == 0 ){
1173 * OK, request seems to be precessed. Continue transferring...
1175 if( !sjcd_transfer_is_active ){
1176 while( CURRENT_IS_VALID ){
1178 * Continue transferring.
1180 sjcd_transfer();
1181 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1182 else break;
1185 if( CURRENT_IS_VALID &&
1186 ( CURRENT->sector / 4 < sjcd_next_bn ||
1187 CURRENT->sector / 4 > sjcd_next_bn + SJCD_BUF_SIZ ) ){
1188 #if defined( SJCD_TRACE )
1189 printk( "SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n" );
1190 #endif
1191 sjcd_transfer_state = SJCD_S_STOP;
1192 goto ReSwitch;
1196 * Now we should turn around rather than wait for while.
1198 goto sjcd_s_data;
1200 #if defined( SJCD_GATHER_STAT )
1201 else statistic.data_ticks++;
1202 #endif
1203 break;
1206 case SJCD_S_STOP:{
1207 sjcd_read_count = 0;
1208 sjcd_send_cmd( SCMD_STOP );
1209 sjcd_transfer_state = SJCD_S_STOPPING;
1210 sjcd_transfer_timeout = 500;
1211 #if defined( SJCD_GATHER_STAT )
1212 statistic.stop_ticks++;
1213 #endif
1214 break;
1217 case SJCD_S_STOPPING:{
1218 unsigned char stat;
1220 stat = inb( SJCDPORT( 1 ) );
1221 #if defined( SJCD_TRACE )
1222 printk( "SJCD_S_STOP: status = 0x%02x\n", stat );
1223 #endif
1224 if( SJCD_DATA_AVAILABLE( stat ) ){
1225 int i;
1226 #if defined( SJCD_TRACE )
1227 printk( "SJCD_S_STOP: discard data\n" );
1228 #endif
1230 * Discard all the data from the pipe. Foolish method.
1232 for( i = 2048; i--; ( void )inb( SJCDPORT( 2 ) ) );
1233 sjcd_transfer_timeout = 500;
1234 } else if( SJCD_STATUS_AVAILABLE( stat ) ){
1235 sjcd_load_status();
1236 if( sjcd_status_valid && sjcd_media_is_changed ) {
1237 sjcd_toc_uptodate = 0;
1238 sjcd_invalidate_buffers();
1240 if( CURRENT_IS_VALID ){
1241 if( sjcd_status_valid ) sjcd_transfer_state = SJCD_S_READ;
1242 else sjcd_transfer_state = SJCD_S_START;
1243 } else sjcd_transfer_state = SJCD_S_IDLE;
1244 goto ReSwitch;
1246 #if defined( SJCD_GATHER_STAT )
1247 else statistic.stopping_ticks++;
1248 #endif
1249 break;
1252 default:
1253 printk( "SJCD: poll: invalid state %d\n", sjcd_transfer_state );
1254 return;
1257 if( --sjcd_transfer_timeout == 0 ){
1258 printk( "SJCD: timeout in state %d\n", sjcd_transfer_state );
1259 while( CURRENT_IS_VALID ) end_request( 0 );
1260 sjcd_send_cmd( SCMD_STOP );
1261 sjcd_transfer_state = SJCD_S_IDLE;
1262 goto ReSwitch;
1266 * Get back in some time. 1 should be replaced with count variable to
1267 * avoid unnecessary testings.
1269 SJCD_SET_TIMER( sjcd_poll, 1 );
1272 static void do_sjcd_request( void ){
1273 #if defined( SJCD_TRACE )
1274 printk( "SJCD: do_sjcd_request(%ld+%ld)\n",
1275 CURRENT->sector, CURRENT->nr_sectors );
1276 #endif
1277 sjcd_transfer_is_active = 1;
1278 while( CURRENT_IS_VALID ){
1280 * Who of us are paranoiac?
1282 if( CURRENT->bh && !buffer_locked(CURRENT->bh) )
1283 panic( DEVICE_NAME ": block not locked" );
1285 sjcd_transfer();
1286 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1287 else {
1288 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1289 if( sjcd_transfer_state == SJCD_S_IDLE ){
1290 if( !sjcd_toc_uptodate ){
1291 if( sjcd_update_toc() < 0 ){
1292 printk( "SJCD: transfer: discard\n" );
1293 while( CURRENT_IS_VALID ) end_request( 0 );
1294 break;
1297 sjcd_transfer_state = SJCD_S_START;
1298 SJCD_SET_TIMER( sjcd_poll, HZ/100 );
1300 break;
1303 sjcd_transfer_is_active = 0;
1304 #if defined( SJCD_TRACE )
1305 printk( "sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1306 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out, sjcd_buf_bn[ sjcd_buf_in ] );
1307 printk( "do_sjcd_request ends\n" );
1308 #endif
1312 * Open the device special file. Check disk is in.
1314 int sjcd_open( struct inode *ip, struct file *fp ){
1316 * Check the presence of device.
1318 if( !sjcd_present ) return( -ENXIO );
1321 * Only read operations are allowed. Really? (:-)
1323 if( fp->f_mode & 2 ) return( -EROFS );
1325 if( sjcd_open_count == 0 ){
1326 int s, sjcd_open_tries;
1327 /* We don't know that, do we? */
1329 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1331 sjcd_mode = 0;
1332 sjcd_door_was_open = 0;
1333 sjcd_transfer_state = SJCD_S_IDLE;
1334 sjcd_invalidate_buffers();
1335 sjcd_status_valid = 0;
1338 * Strict status checking.
1340 for( sjcd_open_tries = 4; --sjcd_open_tries; ){
1341 if( !sjcd_status_valid ) sjcd_get_status();
1342 if( !sjcd_status_valid ){
1343 #if defined( SJCD_DIAGNOSTIC )
1344 printk( "SJCD: open: timed out when check status.\n" );
1345 #endif
1346 return( -EIO );
1347 } else if( !sjcd_media_is_available ){
1348 #if defined( SJCD_DIAGNOSTIC )
1349 printk("SJCD: open: no disk in drive\n");
1350 #endif
1351 if( !sjcd_door_closed ){
1352 sjcd_door_was_open = 1;
1353 #if defined( SJCD_TRACE )
1354 printk("SJCD: open: close the tray\n");
1355 #endif
1356 s = sjcd_tray_close();
1357 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1358 #if defined( SJCD_DIAGNOSTIC )
1359 printk("SJCD: open: tray close attempt failed\n");
1360 #endif
1361 return( -EIO );
1363 continue;
1364 } else return( -EIO );
1366 break;
1368 s = sjcd_tray_lock();
1369 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1370 #if defined( SJCD_DIAGNOSTIC )
1371 printk("SJCD: open: tray lock attempt failed\n");
1372 #endif
1373 return( -EIO );
1375 #if defined( SJCD_TRACE )
1376 printk( "SJCD: open: done\n" );
1377 #endif
1379 #ifdef MODULE
1380 MOD_INC_USE_COUNT;
1381 #endif
1382 ++sjcd_open_count;
1383 return( 0 );
1387 * On close, we flush all sjcd blocks from the buffer cache.
1389 static int sjcd_release( struct inode *inode, struct file *file ){
1390 int s;
1392 #if defined( SJCD_TRACE )
1393 printk( "SJCD: release\n" );
1394 #endif
1395 #ifdef MODULE
1396 MOD_DEC_USE_COUNT;
1397 #endif
1398 if( --sjcd_open_count == 0 ){
1399 sjcd_invalidate_buffers();
1400 sync_dev( inode->i_rdev );
1401 invalidate_buffers( inode->i_rdev );
1402 s = sjcd_tray_unlock();
1403 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1404 #if defined( SJCD_DIAGNOSTIC )
1405 printk("SJCD: release: tray unlock attempt failed.\n");
1406 #endif
1408 if( sjcd_door_was_open ){
1409 s = sjcd_tray_open();
1410 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1411 #if defined( SJCD_DIAGNOSTIC )
1412 printk("SJCD: release: tray unload attempt failed.\n");
1413 #endif
1417 return 0;
1421 * A list of file operations allowed for this cdrom.
1423 static struct file_operations sjcd_fops = {
1424 NULL, /* lseek - default */
1425 block_read, /* read - general block-dev read */
1426 block_write, /* write - general block-dev write */
1427 NULL, /* readdir - bad */
1428 NULL, /* poll */
1429 sjcd_ioctl, /* ioctl */
1430 NULL, /* mmap */
1431 sjcd_open, /* open */
1432 NULL, /* flush */
1433 sjcd_release, /* release */
1434 NULL, /* fsync */
1435 NULL, /* fasync */
1436 sjcd_disk_change, /* media change */
1437 NULL /* revalidate */
1441 * Following stuff is intended for initialization of the cdrom. It
1442 * first looks for presence of device. If the device is present, it
1443 * will be reset. Then read the version of the drive and load status.
1444 * The version is two BCD-coded bytes.
1446 static struct {
1447 unsigned char major, minor;
1448 } sjcd_version;
1451 * Test for presence of drive and initialize it. Called at boot time.
1452 * Probe cdrom, find out version and status.
1454 __initfunc(int sjcd_init( void )){
1455 int i;
1457 printk(KERN_INFO "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n", SJCD_VERSION_MAJOR,
1458 SJCD_VERSION_MINOR);
1460 #if defined( SJCD_TRACE )
1461 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1462 #endif
1464 if( register_blkdev( MAJOR_NR, "sjcd", &sjcd_fops ) != 0 ){
1465 printk( "SJCD: Unable to get major %d for Sanyo CD-ROM\n", MAJOR_NR );
1466 return( -EIO );
1469 blk_dev[ MAJOR_NR ].request_fn = DEVICE_REQUEST;
1470 read_ahead[ MAJOR_NR ] = 4;
1472 if( check_region( sjcd_base, 4 ) ){
1473 printk( "SJCD: Init failed, I/O port (%X) is already in use\n",
1474 sjcd_base );
1475 sjcd_cleanup();
1476 return( -EIO );
1480 * Check for card. Since we are booting now, we can't use standard
1481 * wait algorithm.
1483 printk(KERN_INFO "SJCD: Resetting: " );
1484 sjcd_send_cmd( SCMD_RESET );
1485 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1486 unsigned long timer;
1489 * Wait 10ms approx.
1491 for( timer = jiffies; jiffies <= timer; );
1492 if ( (i % 100) == 0 ) printk( "." );
1493 ( void )sjcd_check_status();
1495 if( i == 0 || sjcd_command_failed ){
1496 printk( " reset failed, no drive found.\n" );
1497 sjcd_cleanup();
1498 return( -EIO );
1499 } else printk( "\n" );
1502 * Get and print out cdrom version.
1504 printk(KERN_INFO "SJCD: Getting version: " );
1505 sjcd_send_cmd( SCMD_GET_VERSION );
1506 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1507 unsigned long timer;
1510 * Wait 10ms approx.
1512 for( timer = jiffies; jiffies <= timer; );
1513 if ( (i % 100) == 0 ) printk( "." );
1514 ( void )sjcd_check_status();
1516 if( i == 0 || sjcd_command_failed ){
1517 printk( " get version failed, no drive found.\n" );
1518 sjcd_cleanup();
1519 return( -EIO );
1522 if( sjcd_load_response( &sjcd_version, sizeof( sjcd_version ) ) == 0 ){
1523 printk( " %1x.%02x\n", ( int )sjcd_version.major,
1524 ( int )sjcd_version.minor );
1525 } else {
1526 printk( " read version failed, no drive found.\n" );
1527 sjcd_cleanup();
1528 return( -EIO );
1532 * Check and print out the tray state. (if it is needed?).
1534 if( !sjcd_status_valid ){
1535 printk(KERN_INFO "SJCD: Getting status: " );
1536 sjcd_send_cmd( SCMD_GET_STATUS );
1537 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1538 unsigned long timer;
1541 * Wait 10ms approx.
1543 for( timer = jiffies; jiffies <= timer; );
1544 if ( (i % 100) == 0 ) printk( "." );
1545 ( void )sjcd_check_status();
1547 if( i == 0 || sjcd_command_failed ){
1548 printk( " get status failed, no drive found.\n" );
1549 sjcd_cleanup();
1550 return( -EIO );
1551 } else printk( "\n" );
1554 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1556 sjcd_present++;
1557 return( 0 );
1560 static int
1561 sjcd_cleanup(void)
1563 if( (unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL) )
1564 printk( "SJCD: cannot unregister device.\n" );
1565 else
1566 release_region( sjcd_base, 4 );
1568 return(0);
1571 #ifdef MODULE
1573 int init_module(void)
1575 return sjcd_init();
1578 void cleanup_module(void)
1580 if ( sjcd_cleanup() )
1581 printk( "SJCD: module: cannot be removed.\n" );
1582 else
1583 printk(KERN_INFO "SJCD: module: removed.\n");
1585 #endif