3 * Sanyo CD-ROM device driver implementation, Version 1.6
4 * Copyright (C) 1995 Vadim V. Model
6 * model@cecmow.enet.dec.com
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.
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
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.
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
62 #include <linux/module.h>
65 #include <linux/errno.h>
66 #include <linux/sched.h>
68 #include <linux/timer.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>
79 #include <asm/uaccess.h>
81 #define MAJOR_NR SANYO_CDROM_MAJOR
82 #include <linux/blk.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;
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
;
119 MODULE_PARM(sjcd_base
, "i");
122 static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq
);
127 static volatile unsigned short sjcd_transfer_is_active
= 0;
129 enum sjcd_transfer_state
{
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 )
149 static struct sjcd_stat statistic
;
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
171 static int __init
sjcd_setup( char *str
)
174 (void)get_options(str
, ARRAY_SIZE(ints
), ints
);
181 __setup("sjcd=", sjcd_setup
);
186 * Special converters.
188 static unsigned char bin2bcd( int bin
){
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
);
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
);
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
);
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
);
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
;
285 for( i
= 200; i
-- && !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ); );
286 if( i
> 0 ) *resp
++ = ( unsigned char )inb( SJCDPORT( 0 ) );
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;
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
);
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 ) ) ) ){
346 * No status is available.
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 ){
374 wake_up( &sjcd_waitq
);
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" );
395 return( sjcd_status_timeout
);
398 static int sjcd_receive_status( void ){
400 #if defined( SJCD_TRACE )
401 printk( "SJCD: receive_status\n" );
404 * Wait a bit for status available.
406 for( i
= 200; i
-- && ( sjcd_check_status() == 0 ); );
408 #if defined( SJCD_TRACE )
409 printk( "SJCD: long wait for status\n" );
411 if( sjcd_wait_for_status() <= 0 )
412 printk( "SJCD: Timeout when read status.\n" );
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" );
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
){
434 printk( "SJCD: sjcd_disk_change( 0x%x )\n", full_dev
);
436 if( MINOR( full_dev
) > 0 ){
437 printk( "SJCD: request error: invalid device minor.\n" );
440 if( !sjcd_command_is_in_progress
)
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
;
456 #if defined( SJCD_TRACE )
457 printk( "SJCD: update toc:\n" );
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" );
475 if( !sjcd_media_is_available
){
476 printk( "SJCD: no disk in drive\n" );
480 if( !sjcd_command_failed
){
481 if( sjcd_load_response( &info
, sizeof( info
) ) != 0 ){
482 printk( "SJCD: cannot load response about TOC start.\n" );
485 sjcd_first_track_no
= bcd2bin( info
.un
.track_no
);
487 printk( "SJCD: get first failed\n" );
490 #if defined( SJCD_TRACE )
491 printk( "SJCD: TOC start 0x%02x ", sjcd_first_track_no
);
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" );
504 if( !sjcd_media_is_available
){
505 printk( "SJCD: no disk in drive\n" );
509 if( !sjcd_command_failed
){
510 if( sjcd_load_response( &info
, sizeof( info
) ) != 0 ){
511 printk( "SJCD: cannot load response about TOC finish.\n" );
514 sjcd_last_track_no
= bcd2bin( info
.un
.track_no
);
516 printk( "SJCD: get last failed\n" );
519 #if defined( SJCD_TRACE )
520 printk( "SJCD: TOC finish 0x%02x ", sjcd_last_track_no
);
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" );
534 if( !sjcd_media_is_available
){
535 printk( "SJCD: no disk in drive\n" );
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
);
546 printk( "SJCD: get info %d failed\n", i
);
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" );
562 if( !sjcd_media_is_available
){
563 printk( "SJCD: no disk in drive\n" );
567 if( !sjcd_command_failed
){
568 if( sjcd_load_response( &info
, sizeof( info
) ) != 0 ){
569 printk( "SJCD: cannot load response about disk size.\n" );
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
;
576 printk( "SJCD: get size failed\n" );
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
);
587 * Load subchannel information.
589 static int sjcd_get_q_info( struct sjcd_hw_qinfo
*qp
){
591 #if defined( SJCD_TRACE )
592 printk( "SJCD: load sub q\n" );
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 );
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 );
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" );
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" );
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" );
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" );
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" );
680 if( ip
== NULL
) return( -EINVAL
);
683 if( !sjcd_status_valid
) return( -EIO
);
684 if( sjcd_update_toc() < 0 ) return( -EIO
);
688 #if defined( SJCD_TRACE )
689 printk( "SJCD: ioctl: start\n" );
695 #if defined( SJCD_TRACE )
696 printk( "SJCD: ioctl: stop\n" );
698 sjcd_send_cmd( SCMD_PAUSE
);
699 ( void )sjcd_receive_status();
700 sjcd_audio_status
= CDROM_AUDIO_NO_STATUS
;
705 struct sjcd_hw_qinfo q_info
;
706 #if defined( SJCD_TRACE )
707 printk( "SJCD: ioctl: pause\n" );
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
;
715 sjcd_audio_status
= CDROM_AUDIO_PAUSED
;
716 sjcd_playing
.start
= q_info
.abs
;
719 } else return( -EINVAL
);
723 #if defined( SJCD_TRACE )
724 printk( "SJCD: ioctl: resume\n" );
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
;
734 sjcd_audio_status
= CDROM_AUDIO_PLAY
;
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" );
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
;
761 } else sjcd_audio_status
= CDROM_AUDIO_PLAY
;
767 struct cdrom_msf sjcd_msf
; int s
;
768 #if defined( SJCD_TRACE )
769 printk( "SJCD: ioctl: playmsf\n" );
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
;
790 } else sjcd_audio_status
= CDROM_AUDIO_PLAY
;
795 case CDROMREADTOCHDR
:{
796 struct cdrom_tochdr toc_header
; int s
;
797 #if defined (SJCD_TRACE )
798 printk( "SJCD: ioctl: readtocheader\n" );
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
) );
808 case CDROMREADTOCENTRY
:{
809 struct cdrom_tocentry toc_entry
; int s
;
810 #if defined( SJCD_TRACE )
811 printk( "SJCD: ioctl: readtocentry\n" );
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
){
829 toc_entry
.cdte_addr
.lba
= msf2hsg( &( tp
->un
.track_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
);
836 default: return( -EINVAL
);
838 copy_to_user( (void *)arg
, &toc_entry
, sizeof( toc_entry
) );
844 struct cdrom_subchnl subchnl
; int s
;
845 #if defined( SJCD_TRACE )
846 printk( "SJCD: ioctl: subchnl\n" );
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
){
862 subchnl
.cdsc_absaddr
.lba
= msf2hsg( &( q_info
.abs
) );
863 subchnl
.cdsc_reladdr
.lba
= msf2hsg( &( q_info
.rel
) );
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
);
873 default: return( -EINVAL
);
875 copy_to_user( (void *)arg
, &subchnl
, sizeof( subchnl
) );
881 struct cdrom_volctrl vol_ctrl
; int s
;
882 #if defined( SJCD_TRACE )
883 printk( "SJCD: ioctl: volctrl\n" );
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 );
898 #if defined( SJCD_TRACE )
899 printk( "SJCD: ioctl: eject\n" );
901 if( !sjcd_command_is_in_progress
){
903 sjcd_send_cmd( SCMD_EJECT_TRAY
);
904 ( void )sjcd_receive_status();
909 #if defined( SJCD_GATHER_STAT )
912 #if defined( SJCD_TRACE )
913 printk( "SJCD: ioctl: statistic\n" );
915 if( ( s
= verify_area( VERIFY_WRITE
, (void *)arg
, sizeof( statistic
) ) ) == 0 )
916 copy_to_user( (void *)arg
, &statistic
, sizeof( statistic
) );
927 * Invalidate internal buffers of the driver.
929 static void sjcd_invalidate_buffers( void ){
931 for( i
= 0; i
< SJCD_BUF_SIZ
; sjcd_buf_bn
[ i
++ ] = -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" );
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
){
957 if( sjcd_buf_bn
[ i
] != bn
){
962 if( nr_sectors
> CURRENT
->nr_sectors
)
963 nr_sectors
= CURRENT
->nr_sectors
;
964 #if defined( SJCD_TRACE )
965 printk( "SJCD: copy out\n" );
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;
977 #if defined( SJCD_TRACE )
978 printk( "SJCD: transfer: done\n" );
982 static void sjcd_poll( void ){
983 #if defined( SJCD_GATHER_STAT )
985 * Update total number of ticks.
988 statistic
.tticks
[ sjcd_transfer_state
]++;
991 ReSwitch
: switch( sjcd_transfer_state
){
994 #if defined( SJCD_GATHER_STAT )
995 statistic
.idle_ticks
++;
997 #if defined( SJCD_TRACE )
998 printk( "SJCD_S_IDLE\n" );
1004 #if defined( SJCD_GATHER_STAT )
1005 statistic
.start_ticks
++;
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" );
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" );
1027 sjcd_transfer_state
= SJCD_S_STOP
;
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" );
1038 #if defined( SJCD_GATHER_STAT )
1039 else statistic
.mode_ticks
++;
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" );
1053 sjcd_transfer_state
= SJCD_S_STOP
;
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" );
1060 sjcd_transfer_state
= SJCD_S_STOP
;
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" );
1071 sjcd_transfer_state
= SJCD_S_STOP
;
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" );
1078 sjcd_transfer_state
= SJCD_S_STOP
;
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
] );
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" );
1105 #if defined( SJCD_TRACE )
1106 printk( "SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n" );
1108 sjcd_transfer_state
= SJCD_S_STOP
;
1112 #if defined( SJCD_GATHER_STAT )
1113 else statistic
.read_ticks
++;
1121 sjcd_s_data
: stat
= inb( SJCDPORT( 1 ) );
1122 #if defined( SJCD_TRACE )
1123 printk( "SJCD_S_DATA: status = 0x%02x\n", stat
);
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.
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",
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" );
1141 sjcd_transfer_state
= SJCD_S_STOP
;
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
;
1151 sjcd_transfer_state
= SJCD_S_READ
;
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
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" );
1164 sjcd_transfer_state
= SJCD_S_STOP
;
1169 * Everything seems to be OK. Just read the frame and recalculate
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
] );
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.
1197 if( CURRENT
->nr_sectors
== 0 ) end_request( 1 );
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" );
1207 sjcd_transfer_state
= SJCD_S_STOP
;
1212 * Now we should turn around rather than wait for while.
1216 #if defined( SJCD_GATHER_STAT )
1217 else statistic
.data_ticks
++;
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
++;
1233 case SJCD_S_STOPPING
:{
1236 stat
= inb( SJCDPORT( 1 ) );
1237 #if defined( SJCD_TRACE )
1238 printk( "SJCD_S_STOP: status = 0x%02x\n", stat
);
1240 if( SJCD_DATA_AVAILABLE( stat
) ){
1242 #if defined( SJCD_TRACE )
1243 printk( "SJCD_S_STOP: discard data\n" );
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
) ){
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
;
1262 #if defined( SJCD_GATHER_STAT )
1263 else statistic
.stopping_ticks
++;
1269 printk( "SJCD: poll: invalid state %d\n", sjcd_transfer_state
);
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
;
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
);
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" );
1302 if( CURRENT
->nr_sectors
== 0 ) end_request( 1 );
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 );
1313 sjcd_transfer_state
= SJCD_S_START
;
1314 SJCD_SET_TIMER( sjcd_poll
, HZ
/100 );
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" );
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;
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" );
1363 } else if( !sjcd_media_is_available
){
1364 #if defined( SJCD_DIAGNOSTIC )
1365 printk("SJCD: open: no disk in drive\n");
1367 if( !sjcd_door_closed
){
1368 sjcd_door_was_open
= 1;
1369 #if defined( SJCD_TRACE )
1370 printk("SJCD: open: close the tray\n");
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");
1380 } else return( -EIO
);
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");
1391 #if defined( SJCD_TRACE )
1392 printk( "SJCD: open: done\n" );
1403 * On close, we flush all sjcd blocks from the buffer cache.
1405 static int sjcd_release( struct inode
*inode
, struct file
*file
){
1408 #if defined( SJCD_TRACE )
1409 printk( "SJCD: release\n" );
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");
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");
1435 * A list of file operations allowed for this cdrom.
1437 static struct block_device_operations sjcd_fops
= {
1439 release
: sjcd_release
,
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.
1454 unsigned char major
, minor
;
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 ){
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
);
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
);
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",
1490 * Check for card. Since we are booting now, we can't use standard
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
;
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" );
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
;
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" );
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
);
1536 printk( " read version failed, no drive found.\n" );
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
;
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" );
1561 } else printk( "\n" );
1564 printk(KERN_INFO
"SJCD: Status: port=0x%x.\n", sjcd_base
);
1573 if( (unregister_blkdev(MAJOR_NR
, "sjcd") == -EINVAL
) )
1574 printk( "SJCD: cannot unregister device.\n" );
1576 release_region( sjcd_base
, 4 );
1582 void __exit
sjcd_exit(void)
1584 if ( sjcd_cleanup() )
1585 printk( "SJCD: module: cannot be removed.\n" );
1587 printk(KERN_INFO
"SJCD: module: removed.\n");
1591 module_init(sjcd_init
);
1593 module_exit(sjcd_exit
);