Revert last change. Bug noticed by Linus.
[linux-2.6/linux-mips.git] / drivers / cdrom / sjcd.c
blobb793bac4bf21fefb5c42a33ff70c2d50320ae652
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 #include <linux/module.h>
62 #include <linux/errno.h>
63 #include <linux/sched.h>
64 #include <linux/mm.h>
65 #include <linux/timer.h>
66 #include <linux/fs.h>
67 #include <linux/kernel.h>
68 #include <linux/cdrom.h>
69 #include <linux/ioport.h>
70 #include <linux/string.h>
71 #include <linux/major.h>
72 #include <linux/init.h>
73 #include <linux/devfs_fs_kernel.h>
75 #include <asm/system.h>
76 #include <asm/io.h>
77 #include <asm/uaccess.h>
79 #define MAJOR_NR SANYO_CDROM_MAJOR
80 #include <linux/blk.h>
81 #include "sjcd.h"
83 static int sjcd_present = 0;
85 #define SJCD_BUF_SIZ 32 /* cdr-h94a has internal 64K buffer */
88 * buffer for block size conversion
90 static char sjcd_buf[ 2048 * SJCD_BUF_SIZ ];
91 static volatile int sjcd_buf_bn[ SJCD_BUF_SIZ ], sjcd_next_bn;
92 static volatile int sjcd_buf_in, sjcd_buf_out = -1;
95 * Status.
97 static unsigned short sjcd_status_valid = 0;
98 static unsigned short sjcd_door_closed;
99 static unsigned short sjcd_door_was_open;
100 static unsigned short sjcd_media_is_available;
101 static unsigned short sjcd_media_is_changed;
102 static unsigned short sjcd_toc_uptodate = 0;
103 static unsigned short sjcd_command_failed;
104 static volatile unsigned char sjcd_completion_status = 0;
105 static volatile unsigned char sjcd_completion_error = 0;
106 static unsigned short sjcd_command_is_in_progress = 0;
107 static unsigned short sjcd_error_reported = 0;
109 static int sjcd_open_count;
111 static int sjcd_audio_status;
112 static struct sjcd_play_msf sjcd_playing;
114 static int sjcd_base = SJCD_BASE_ADDR;
116 #ifdef MODULE
117 MODULE_PARM(sjcd_base, "i");
118 #endif
120 static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
123 * Data transfer.
125 static volatile unsigned short sjcd_transfer_is_active = 0;
127 enum sjcd_transfer_state {
128 SJCD_S_IDLE = 0,
129 SJCD_S_START = 1,
130 SJCD_S_MODE = 2,
131 SJCD_S_READ = 3,
132 SJCD_S_DATA = 4,
133 SJCD_S_STOP = 5,
134 SJCD_S_STOPPING = 6
136 static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
137 static long sjcd_transfer_timeout = 0;
138 static int sjcd_read_count = 0;
139 static unsigned char sjcd_mode = 0;
141 #define SJCD_READ_TIMEOUT 5000
143 #if defined( SJCD_GATHER_STAT )
145 * Statistic.
147 static struct sjcd_stat statistic;
148 #endif
151 * Timer.
153 static struct timer_list sjcd_delay_timer = { function: NULL };
155 #define SJCD_SET_TIMER( func, tmout ) \
156 ( sjcd_delay_timer.expires = jiffies+tmout, \
157 sjcd_delay_timer.function = ( void * )func, \
158 add_timer( &sjcd_delay_timer ) )
160 #define CLEAR_TIMER del_timer( &sjcd_delay_timer )
162 static int sjcd_cleanup(void);
165 * Set up device, i.e., use command line data to set
166 * base address.
168 #ifndef MODULE
169 static int __init sjcd_setup( char *str)
171 int ints[2];
172 (void)get_options(str, ARRAY_SIZE(ints), ints);
173 if (ints[0] > 0)
174 sjcd_base = ints[1];
176 return 1;
179 __setup("sjcd=", sjcd_setup);
181 #endif
184 * Special converters.
186 static unsigned char bin2bcd( int bin ){
187 int u, v;
189 u = bin % 10; v = bin / 10;
190 return( u | ( v << 4 ) );
193 static int bcd2bin( unsigned char bcd ){
194 return( ( bcd >> 4 ) * 10 + ( bcd & 0x0F ) );
197 static long msf2hsg( struct msf *mp ){
198 return( bcd2bin( mp->frame ) + bcd2bin( mp->sec ) * 75
199 + bcd2bin( mp->min ) * 4500 - 150 );
202 static void hsg2msf( long hsg, struct msf *msf ){
203 hsg += 150; msf->min = hsg / 4500;
204 hsg %= 4500; msf->sec = hsg / 75; msf->frame = hsg % 75;
205 msf->min = bin2bcd( msf->min ); /* convert to BCD */
206 msf->sec = bin2bcd( msf->sec );
207 msf->frame = bin2bcd( msf->frame );
211 * Send a command to cdrom. Invalidate status.
213 static void sjcd_send_cmd( unsigned char cmd ){
214 #if defined( SJCD_TRACE )
215 printk( "SJCD: send_cmd( 0x%x )\n", cmd );
216 #endif
217 outb( cmd, SJCDPORT( 0 ) );
218 sjcd_command_is_in_progress = 1;
219 sjcd_status_valid = 0;
220 sjcd_command_failed = 0;
224 * Send a command with one arg to cdrom. Invalidate status.
226 static void sjcd_send_1_cmd( unsigned char cmd, unsigned char a ){
227 #if defined( SJCD_TRACE )
228 printk( "SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a );
229 #endif
230 outb( cmd, SJCDPORT( 0 ) );
231 outb( a, SJCDPORT( 0 ) );
232 sjcd_command_is_in_progress = 1;
233 sjcd_status_valid = 0;
234 sjcd_command_failed = 0;
238 * Send a command with four args to cdrom. Invalidate status.
240 static void sjcd_send_4_cmd( unsigned char cmd, unsigned char a,
241 unsigned char b, unsigned char c, unsigned char d ){
242 #if defined( SJCD_TRACE )
243 printk( "SJCD: send_4_cmd( 0x%x )\n", cmd );
244 #endif
245 outb( cmd, SJCDPORT( 0 ) );
246 outb( a, SJCDPORT( 0 ) );
247 outb( b, SJCDPORT( 0 ) );
248 outb( c, SJCDPORT( 0 ) );
249 outb( d, SJCDPORT( 0 ) );
250 sjcd_command_is_in_progress = 1;
251 sjcd_status_valid = 0;
252 sjcd_command_failed = 0;
256 * Send a play or read command to cdrom. Invalidate Status.
258 static void sjcd_send_6_cmd( unsigned char cmd, struct sjcd_play_msf *pms ){
259 #if defined( SJCD_TRACE )
260 printk( "SJCD: send_long_cmd( 0x%x )\n", cmd );
261 #endif
262 outb( cmd, SJCDPORT( 0 ) );
263 outb( pms->start.min, SJCDPORT( 0 ) );
264 outb( pms->start.sec, SJCDPORT( 0 ) );
265 outb( pms->start.frame, SJCDPORT( 0 ) );
266 outb( pms->end.min, SJCDPORT( 0 ) );
267 outb( pms->end.sec, SJCDPORT( 0 ) );
268 outb( pms->end.frame, SJCDPORT( 0 ) );
269 sjcd_command_is_in_progress = 1;
270 sjcd_status_valid = 0;
271 sjcd_command_failed = 0;
275 * Get a value from the data port. Should not block, so we use a little
276 * wait for a while. Returns 0 if OK.
278 static int sjcd_load_response( void *buf, int len ){
279 unsigned char *resp = ( unsigned char * )buf;
281 for( ; len; --len ){
282 int i;
283 for( i = 200; i-- && !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ); );
284 if( i > 0 ) *resp++ = ( unsigned char )inb( SJCDPORT( 0 ) );
285 else break;
287 return( len );
291 * Load and parse command completion status (drive info byte and maybe error).
292 * Sorry, no error classification yet.
294 static void sjcd_load_status( void ){
295 sjcd_media_is_changed = 0;
296 sjcd_completion_error = 0;
297 sjcd_completion_status = inb( SJCDPORT( 0 ) );
298 if( sjcd_completion_status & SST_DOOR_OPENED ){
299 sjcd_door_closed = sjcd_media_is_available = 0;
300 } else {
301 sjcd_door_closed = 1;
302 if( sjcd_completion_status & SST_MEDIA_CHANGED )
303 sjcd_media_is_available = sjcd_media_is_changed = 1;
304 else if( sjcd_completion_status & 0x0F ){
306 * OK, we seem to catch an error ...
308 while( !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) );
309 sjcd_completion_error = inb( SJCDPORT( 0 ) );
310 if( ( sjcd_completion_status & 0x08 ) &&
311 ( sjcd_completion_error & 0x40 ) )
312 sjcd_media_is_available = 0;
313 else sjcd_command_failed = 1;
314 } else sjcd_media_is_available = 1;
317 * Ok, status loaded successfully.
319 sjcd_status_valid = 1, sjcd_error_reported = 0;
320 sjcd_command_is_in_progress = 0;
323 * If the disk is changed, the TOC is not valid.
325 if( sjcd_media_is_changed ) sjcd_toc_uptodate = 0;
326 #if defined( SJCD_TRACE )
327 printk( "SJCD: status %02x.%02x loaded.\n",
328 ( int )sjcd_completion_status, ( int )sjcd_completion_error );
329 #endif
333 * Read status from cdrom. Check to see if the status is available.
335 static int sjcd_check_status( void ){
337 * Try to load the response from cdrom into buffer.
339 if( SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) ){
340 sjcd_load_status();
341 return( 1 );
342 } else {
344 * No status is available.
346 return( 0 );
351 * This is just timeout counter, and nothing more. Surprised ? :-)
353 static volatile long sjcd_status_timeout;
356 * We need about 10 seconds to wait. The longest command takes about 5 seconds
357 * to probe the disk (usually after tray closed or drive reset). Other values
358 * should be thought of for other commands.
360 #define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
362 static void sjcd_status_timer( void ){
363 if( sjcd_check_status() ){
365 * The command completed and status is loaded, stop waiting.
367 wake_up( &sjcd_waitq );
368 } else if( --sjcd_status_timeout <= 0 ){
370 * We are timed out.
372 wake_up( &sjcd_waitq );
373 } else {
375 * We have still some time to wait. Try again.
377 SJCD_SET_TIMER( sjcd_status_timer, 1 );
382 * Wait for status for 10 sec approx. Returns non-positive when timed out.
383 * Should not be used while reading data CDs.
385 static int sjcd_wait_for_status( void ){
386 sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
387 SJCD_SET_TIMER( sjcd_status_timer, 1 );
388 sleep_on( &sjcd_waitq );
389 #if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
390 if( sjcd_status_timeout <= 0 )
391 printk( "SJCD: Error Wait For Status.\n" );
392 #endif
393 return( sjcd_status_timeout );
396 static int sjcd_receive_status( void ){
397 int i;
398 #if defined( SJCD_TRACE )
399 printk( "SJCD: receive_status\n" );
400 #endif
402 * Wait a bit for status available.
404 for( i = 200; i-- && ( sjcd_check_status() == 0 ); );
405 if( i < 0 ){
406 #if defined( SJCD_TRACE )
407 printk( "SJCD: long wait for status\n" );
408 #endif
409 if( sjcd_wait_for_status() <= 0 )
410 printk( "SJCD: Timeout when read status.\n" );
411 else i = 0;
413 return( i );
417 * Load the status. Issue get status command and wait for status available.
419 static void sjcd_get_status( void ){
420 #if defined( SJCD_TRACE )
421 printk( "SJCD: get_status\n" );
422 #endif
423 sjcd_send_cmd( SCMD_GET_STATUS );
424 sjcd_receive_status();
428 * Check the drive if the disk is changed. Should be revised.
430 static int sjcd_disk_change( kdev_t full_dev ){
431 #if 0
432 printk( "SJCD: sjcd_disk_change( 0x%x )\n", full_dev );
433 #endif
434 if( MINOR( full_dev ) > 0 ){
435 printk( "SJCD: request error: invalid device minor.\n" );
436 return 0;
438 if( !sjcd_command_is_in_progress )
439 sjcd_get_status();
440 return( sjcd_status_valid ? sjcd_media_is_changed : 0 );
444 * Read the table of contents (TOC) and TOC header if necessary.
445 * We assume that the drive contains no more than 99 toc entries.
447 static struct sjcd_hw_disk_info sjcd_table_of_contents[ SJCD_MAX_TRACKS ];
448 static unsigned char sjcd_first_track_no, sjcd_last_track_no;
449 #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
451 static int sjcd_update_toc( void ){
452 struct sjcd_hw_disk_info info;
453 int i;
454 #if defined( SJCD_TRACE )
455 printk( "SJCD: update toc:\n" );
456 #endif
458 * check to see if we need to do anything
460 if( sjcd_toc_uptodate ) return( 0 );
463 * Get the TOC start information.
465 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK );
466 sjcd_receive_status();
468 if( !sjcd_status_valid ){
469 printk( "SJCD: cannot load status.\n" );
470 return( -1 );
473 if( !sjcd_media_is_available ){
474 printk( "SJCD: no disk in drive\n" );
475 return( -1 );
478 if( !sjcd_command_failed ){
479 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
480 printk( "SJCD: cannot load response about TOC start.\n" );
481 return( -1 );
483 sjcd_first_track_no = bcd2bin( info.un.track_no );
484 } else {
485 printk( "SJCD: get first failed\n" );
486 return( -1 );
488 #if defined( SJCD_TRACE )
489 printk( "SJCD: TOC start 0x%02x ", sjcd_first_track_no );
490 #endif
492 * Get the TOC finish information.
494 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK );
495 sjcd_receive_status();
497 if( !sjcd_status_valid ){
498 printk( "SJCD: cannot load status.\n" );
499 return( -1 );
502 if( !sjcd_media_is_available ){
503 printk( "SJCD: no disk in drive\n" );
504 return( -1 );
507 if( !sjcd_command_failed ){
508 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
509 printk( "SJCD: cannot load response about TOC finish.\n" );
510 return( -1 );
512 sjcd_last_track_no = bcd2bin( info.un.track_no );
513 } else {
514 printk( "SJCD: get last failed\n" );
515 return( -1 );
517 #if defined( SJCD_TRACE )
518 printk( "SJCD: TOC finish 0x%02x ", sjcd_last_track_no );
519 #endif
520 for( i = sjcd_first_track_no; i <= sjcd_last_track_no; i++ ){
522 * Get the first track information.
524 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, bin2bcd( i ) );
525 sjcd_receive_status();
527 if( !sjcd_status_valid ){
528 printk( "SJCD: cannot load status.\n" );
529 return( -1 );
532 if( !sjcd_media_is_available ){
533 printk( "SJCD: no disk in drive\n" );
534 return( -1 );
537 if( !sjcd_command_failed ){
538 if( sjcd_load_response( &sjcd_table_of_contents[ i ],
539 sizeof( struct sjcd_hw_disk_info ) ) != 0 ){
540 printk( "SJCD: cannot load info for %d track\n", i );
541 return( -1 );
543 } else {
544 printk( "SJCD: get info %d failed\n", i );
545 return( -1 );
550 * Get the disk length info.
552 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE );
553 sjcd_receive_status();
555 if( !sjcd_status_valid ){
556 printk( "SJCD: cannot load status.\n" );
557 return( -1 );
560 if( !sjcd_media_is_available ){
561 printk( "SJCD: no disk in drive\n" );
562 return( -1 );
565 if( !sjcd_command_failed ){
566 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
567 printk( "SJCD: cannot load response about disk size.\n" );
568 return( -1 );
570 sjcd_disk_length.min = info.un.track_msf.min;
571 sjcd_disk_length.sec = info.un.track_msf.sec;
572 sjcd_disk_length.frame = info.un.track_msf.frame;
573 } else {
574 printk( "SJCD: get size failed\n" );
575 return( 1 );
577 #if defined( SJCD_TRACE )
578 printk( "SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
579 sjcd_disk_length.sec, sjcd_disk_length.frame );
580 #endif
581 return( 0 );
585 * Load subchannel information.
587 static int sjcd_get_q_info( struct sjcd_hw_qinfo *qp ){
588 int s;
589 #if defined( SJCD_TRACE )
590 printk( "SJCD: load sub q\n" );
591 #endif
592 sjcd_send_cmd( SCMD_GET_QINFO );
593 s = sjcd_receive_status();
594 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ){
595 sjcd_send_cmd( 0xF2 );
596 s = sjcd_receive_status();
597 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
598 sjcd_send_cmd( SCMD_GET_QINFO );
599 s = sjcd_receive_status();
600 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
602 if( sjcd_media_is_available )
603 if( sjcd_load_response( qp, sizeof( *qp ) ) == 0 ) return( 0 );
604 return( -1 );
608 * Start playing from the specified position.
610 static int sjcd_play( struct sjcd_play_msf *mp ){
611 struct sjcd_play_msf msf;
614 * Turn the device to play mode.
616 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_PLAY );
617 if( sjcd_receive_status() < 0 ) return( -1 );
620 * Seek to the starting point.
622 msf.start = mp->start;
623 msf.end.min = msf.end.sec = msf.end.frame = 0x00;
624 sjcd_send_6_cmd( SCMD_SEEK, &msf );
625 if( sjcd_receive_status() < 0 ) return( -1 );
628 * Start playing.
630 sjcd_send_6_cmd( SCMD_PLAY, mp );
631 return( sjcd_receive_status() );
635 * Tray control functions.
637 static int sjcd_tray_close( void ){
638 #if defined( SJCD_TRACE )
639 printk( "SJCD: tray_close\n" );
640 #endif
641 sjcd_send_cmd( SCMD_CLOSE_TRAY );
642 return( sjcd_receive_status() );
645 static int sjcd_tray_lock( void ){
646 #if defined( SJCD_TRACE )
647 printk( "SJCD: tray_lock\n" );
648 #endif
649 sjcd_send_cmd( SCMD_LOCK_TRAY );
650 return( sjcd_receive_status() );
653 static int sjcd_tray_unlock( void ){
654 #if defined( SJCD_TRACE )
655 printk( "SJCD: tray_unlock\n" );
656 #endif
657 sjcd_send_cmd( SCMD_UNLOCK_TRAY );
658 return( sjcd_receive_status() );
661 static int sjcd_tray_open( void ){
662 #if defined( SJCD_TRACE )
663 printk( "SJCD: tray_open\n" );
664 #endif
665 sjcd_send_cmd( SCMD_EJECT_TRAY );
666 return( sjcd_receive_status() );
670 * Do some user commands.
672 static int sjcd_ioctl( struct inode *ip, struct file *fp,
673 unsigned int cmd, unsigned long arg ){
674 #if defined( SJCD_TRACE )
675 printk( "SJCD:ioctl\n" );
676 #endif
678 if( ip == NULL ) return( -EINVAL );
680 sjcd_get_status();
681 if( !sjcd_status_valid ) return( -EIO );
682 if( sjcd_update_toc() < 0 ) return( -EIO );
684 switch( cmd ){
685 case CDROMSTART:{
686 #if defined( SJCD_TRACE )
687 printk( "SJCD: ioctl: start\n" );
688 #endif
689 return( 0 );
692 case CDROMSTOP:{
693 #if defined( SJCD_TRACE )
694 printk( "SJCD: ioctl: stop\n" );
695 #endif
696 sjcd_send_cmd( SCMD_PAUSE );
697 ( void )sjcd_receive_status();
698 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
699 return( 0 );
702 case CDROMPAUSE:{
703 struct sjcd_hw_qinfo q_info;
704 #if defined( SJCD_TRACE )
705 printk( "SJCD: ioctl: pause\n" );
706 #endif
707 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
708 sjcd_send_cmd( SCMD_PAUSE );
709 ( void )sjcd_receive_status();
710 if( sjcd_get_q_info( &q_info ) < 0 ){
711 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
712 } else {
713 sjcd_audio_status = CDROM_AUDIO_PAUSED;
714 sjcd_playing.start = q_info.abs;
716 return( 0 );
717 } else return( -EINVAL );
720 case CDROMRESUME:{
721 #if defined( SJCD_TRACE )
722 printk( "SJCD: ioctl: resume\n" );
723 #endif
724 if( sjcd_audio_status == CDROM_AUDIO_PAUSED ){
726 * continue play starting at saved location
728 if( sjcd_play( &sjcd_playing ) < 0 ){
729 sjcd_audio_status = CDROM_AUDIO_ERROR;
730 return( -EIO );
731 } else {
732 sjcd_audio_status = CDROM_AUDIO_PLAY;
733 return( 0 );
735 } else return( -EINVAL );
738 case CDROMPLAYTRKIND:{
739 struct cdrom_ti ti; int s;
740 #if defined( SJCD_TRACE )
741 printk( "SJCD: ioctl: playtrkind\n" );
742 #endif
743 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( ti ) ) ) == 0 ){
744 copy_from_user( &ti, (void *)arg, sizeof( ti ) );
746 if( ti.cdti_trk0 < sjcd_first_track_no ) return( -EINVAL );
747 if( ti.cdti_trk1 > sjcd_last_track_no )
748 ti.cdti_trk1 = sjcd_last_track_no;
749 if( ti.cdti_trk0 > ti.cdti_trk1 ) return( -EINVAL );
751 sjcd_playing.start = sjcd_table_of_contents[ ti.cdti_trk0 ].un.track_msf;
752 sjcd_playing.end = ( ti.cdti_trk1 < sjcd_last_track_no ) ?
753 sjcd_table_of_contents[ ti.cdti_trk1 + 1 ].un.track_msf :
754 sjcd_table_of_contents[ 0 ].un.track_msf;
756 if( sjcd_play( &sjcd_playing ) < 0 ){
757 sjcd_audio_status = CDROM_AUDIO_ERROR;
758 return( -EIO );
759 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
761 return( s );
764 case CDROMPLAYMSF:{
765 struct cdrom_msf sjcd_msf; int s;
766 #if defined( SJCD_TRACE )
767 printk( "SJCD: ioctl: playmsf\n" );
768 #endif
769 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( sjcd_msf ) ) ) == 0 ){
770 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
771 sjcd_send_cmd( SCMD_PAUSE );
772 ( void )sjcd_receive_status();
773 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
776 copy_from_user( &sjcd_msf, (void *)arg, sizeof( sjcd_msf ) );
778 sjcd_playing.start.min = bin2bcd( sjcd_msf.cdmsf_min0 );
779 sjcd_playing.start.sec = bin2bcd( sjcd_msf.cdmsf_sec0 );
780 sjcd_playing.start.frame = bin2bcd( sjcd_msf.cdmsf_frame0 );
781 sjcd_playing.end.min = bin2bcd( sjcd_msf.cdmsf_min1 );
782 sjcd_playing.end.sec = bin2bcd( sjcd_msf.cdmsf_sec1 );
783 sjcd_playing.end.frame = bin2bcd( sjcd_msf.cdmsf_frame1 );
785 if( sjcd_play( &sjcd_playing ) < 0 ){
786 sjcd_audio_status = CDROM_AUDIO_ERROR;
787 return( -EIO );
788 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
790 return( s );
793 case CDROMREADTOCHDR:{
794 struct cdrom_tochdr toc_header; int s;
795 #if defined (SJCD_TRACE )
796 printk( "SJCD: ioctl: readtocheader\n" );
797 #endif
798 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_header ) ) ) == 0 ){
799 toc_header.cdth_trk0 = sjcd_first_track_no;
800 toc_header.cdth_trk1 = sjcd_last_track_no;
801 copy_to_user( (void *)arg, &toc_header, sizeof( toc_header ) );
803 return( s );
806 case CDROMREADTOCENTRY:{
807 struct cdrom_tocentry toc_entry; int s;
808 #if defined( SJCD_TRACE )
809 printk( "SJCD: ioctl: readtocentry\n" );
810 #endif
811 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_entry ) ) ) == 0 ){
812 struct sjcd_hw_disk_info *tp;
814 copy_from_user( &toc_entry, (void *)arg, sizeof( toc_entry ) );
816 if( toc_entry.cdte_track == CDROM_LEADOUT )
817 tp = &sjcd_table_of_contents[ 0 ];
818 else if( toc_entry.cdte_track < sjcd_first_track_no ) return( -EINVAL );
819 else if( toc_entry.cdte_track > sjcd_last_track_no ) return( -EINVAL );
820 else tp = &sjcd_table_of_contents[ toc_entry.cdte_track ];
822 toc_entry.cdte_adr = tp->track_control & 0x0F;
823 toc_entry.cdte_ctrl = tp->track_control >> 4;
825 switch( toc_entry.cdte_format ){
826 case CDROM_LBA:
827 toc_entry.cdte_addr.lba = msf2hsg( &( tp->un.track_msf ) );
828 break;
829 case CDROM_MSF:
830 toc_entry.cdte_addr.msf.minute = bcd2bin( tp->un.track_msf.min );
831 toc_entry.cdte_addr.msf.second = bcd2bin( tp->un.track_msf.sec );
832 toc_entry.cdte_addr.msf.frame = bcd2bin( tp->un.track_msf.frame );
833 break;
834 default: return( -EINVAL );
836 copy_to_user( (void *)arg, &toc_entry, sizeof( toc_entry ) );
838 return( s );
841 case CDROMSUBCHNL:{
842 struct cdrom_subchnl subchnl; int s;
843 #if defined( SJCD_TRACE )
844 printk( "SJCD: ioctl: subchnl\n" );
845 #endif
846 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( subchnl ) ) ) == 0 ){
847 struct sjcd_hw_qinfo q_info;
849 copy_from_user( &subchnl, (void *)arg, sizeof( subchnl ) );
850 if( sjcd_get_q_info( &q_info ) < 0 ) return( -EIO );
852 subchnl.cdsc_audiostatus = sjcd_audio_status;
853 subchnl.cdsc_adr = q_info.track_control & 0x0F;
854 subchnl.cdsc_ctrl = q_info.track_control >> 4;
855 subchnl.cdsc_trk = bcd2bin( q_info.track_no );
856 subchnl.cdsc_ind = bcd2bin( q_info.x );
858 switch( subchnl.cdsc_format ){
859 case CDROM_LBA:
860 subchnl.cdsc_absaddr.lba = msf2hsg( &( q_info.abs ) );
861 subchnl.cdsc_reladdr.lba = msf2hsg( &( q_info.rel ) );
862 break;
863 case CDROM_MSF:
864 subchnl.cdsc_absaddr.msf.minute = bcd2bin( q_info.abs.min );
865 subchnl.cdsc_absaddr.msf.second = bcd2bin( q_info.abs.sec );
866 subchnl.cdsc_absaddr.msf.frame = bcd2bin( q_info.abs.frame );
867 subchnl.cdsc_reladdr.msf.minute = bcd2bin( q_info.rel.min );
868 subchnl.cdsc_reladdr.msf.second = bcd2bin( q_info.rel.sec );
869 subchnl.cdsc_reladdr.msf.frame = bcd2bin( q_info.rel.frame );
870 break;
871 default: return( -EINVAL );
873 copy_to_user( (void *)arg, &subchnl, sizeof( subchnl ) );
875 return( s );
878 case CDROMVOLCTRL:{
879 struct cdrom_volctrl vol_ctrl; int s;
880 #if defined( SJCD_TRACE )
881 printk( "SJCD: ioctl: volctrl\n" );
882 #endif
883 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( vol_ctrl ) ) ) == 0 ){
884 unsigned char dummy[ 4 ];
886 copy_from_user( &vol_ctrl, (void *)arg, sizeof( vol_ctrl ) );
887 sjcd_send_4_cmd( SCMD_SET_VOLUME, vol_ctrl.channel0, 0xFF,
888 vol_ctrl.channel1, 0xFF );
889 if( sjcd_receive_status() < 0 ) return( -EIO );
890 ( void )sjcd_load_response( dummy, 4 );
892 return( s );
895 case CDROMEJECT:{
896 #if defined( SJCD_TRACE )
897 printk( "SJCD: ioctl: eject\n" );
898 #endif
899 if( !sjcd_command_is_in_progress ){
900 sjcd_tray_unlock();
901 sjcd_send_cmd( SCMD_EJECT_TRAY );
902 ( void )sjcd_receive_status();
904 return( 0 );
907 #if defined( SJCD_GATHER_STAT )
908 case 0xABCD:{
909 int s;
910 #if defined( SJCD_TRACE )
911 printk( "SJCD: ioctl: statistic\n" );
912 #endif
913 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( statistic ) ) ) == 0 )
914 copy_to_user( (void *)arg, &statistic, sizeof( statistic ) );
915 return( s );
917 #endif
919 default:
920 return( -EINVAL );
925 * Invalidate internal buffers of the driver.
927 static void sjcd_invalidate_buffers( void ){
928 int i;
929 for( i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[ i++ ] = -1 );
930 sjcd_buf_out = -1;
934 * Take care of the different block sizes between cdrom and Linux.
935 * When Linux gets variable block sizes this will probably go away.
938 #define CURRENT_IS_VALID \
939 ( !QUEUE_EMPTY && MAJOR( CURRENT->rq_dev ) == MAJOR_NR && \
940 CURRENT->cmd == READ && CURRENT->sector != -1 )
942 static void sjcd_transfer( void ){
943 #if defined( SJCD_TRACE )
944 printk( "SJCD: transfer:\n" );
945 #endif
946 if( CURRENT_IS_VALID ){
947 while( CURRENT->nr_sectors ){
948 int i, bn = CURRENT->sector / 4;
949 for( i = 0; i < SJCD_BUF_SIZ && sjcd_buf_bn[ i ] != bn; i++ );
950 if( i < SJCD_BUF_SIZ ){
951 int offs = ( i * 4 + ( CURRENT->sector & 3 ) ) * 512;
952 int nr_sectors = 4 - ( CURRENT->sector & 3 );
953 if( sjcd_buf_out != i ){
954 sjcd_buf_out = i;
955 if( sjcd_buf_bn[ i ] != bn ){
956 sjcd_buf_out = -1;
957 continue;
960 if( nr_sectors > CURRENT->nr_sectors )
961 nr_sectors = CURRENT->nr_sectors;
962 #if defined( SJCD_TRACE )
963 printk( "SJCD: copy out\n" );
964 #endif
965 memcpy( CURRENT->buffer, sjcd_buf + offs, nr_sectors * 512 );
966 CURRENT->nr_sectors -= nr_sectors;
967 CURRENT->sector += nr_sectors;
968 CURRENT->buffer += nr_sectors * 512;
969 } else {
970 sjcd_buf_out = -1;
971 break;
975 #if defined( SJCD_TRACE )
976 printk( "SJCD: transfer: done\n" );
977 #endif
980 static void sjcd_poll( void ){
981 #if defined( SJCD_GATHER_STAT )
983 * Update total number of ticks.
985 statistic.ticks++;
986 statistic.tticks[ sjcd_transfer_state ]++;
987 #endif
989 ReSwitch: switch( sjcd_transfer_state ){
991 case SJCD_S_IDLE:{
992 #if defined( SJCD_GATHER_STAT )
993 statistic.idle_ticks++;
994 #endif
995 #if defined( SJCD_TRACE )
996 printk( "SJCD_S_IDLE\n" );
997 #endif
998 return;
1001 case SJCD_S_START:{
1002 #if defined( SJCD_GATHER_STAT )
1003 statistic.start_ticks++;
1004 #endif
1005 sjcd_send_cmd( SCMD_GET_STATUS );
1006 sjcd_transfer_state =
1007 sjcd_mode == SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1008 sjcd_transfer_timeout = 500;
1009 #if defined( SJCD_TRACE )
1010 printk( "SJCD_S_START: goto SJCD_S_%s mode\n",
1011 sjcd_transfer_state == SJCD_S_READ ? "READ" : "MODE" );
1012 #endif
1013 break;
1016 case SJCD_S_MODE:{
1017 if( sjcd_check_status() ){
1019 * Previous command is completed.
1021 if( !sjcd_status_valid || sjcd_command_failed ){
1022 #if defined( SJCD_TRACE )
1023 printk( "SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1024 #endif
1025 sjcd_transfer_state = SJCD_S_STOP;
1026 goto ReSwitch;
1029 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1030 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_COOKED );
1031 sjcd_transfer_state = SJCD_S_READ; sjcd_transfer_timeout = 1000;
1032 #if defined( SJCD_TRACE )
1033 printk( "SJCD_S_MODE: goto SJCD_S_READ mode\n" );
1034 #endif
1036 #if defined( SJCD_GATHER_STAT )
1037 else statistic.mode_ticks++;
1038 #endif
1039 break;
1042 case SJCD_S_READ:{
1043 if( sjcd_status_valid ? 1 : sjcd_check_status() ){
1045 * Previous command is completed.
1047 if( !sjcd_status_valid || sjcd_command_failed ){
1048 #if defined( SJCD_TRACE )
1049 printk( "SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1050 #endif
1051 sjcd_transfer_state = SJCD_S_STOP;
1052 goto ReSwitch;
1054 if( !sjcd_media_is_available ){
1055 #if defined( SJCD_TRACE )
1056 printk( "SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n" );
1057 #endif
1058 sjcd_transfer_state = SJCD_S_STOP;
1059 goto ReSwitch;
1061 if( sjcd_mode != SCMD_MODE_COOKED ){
1063 * We seem to come from set mode. So discard one byte of result.
1065 if( sjcd_load_response( &sjcd_mode, 1 ) != 0 ){
1066 #if defined( SJCD_TRACE )
1067 printk( "SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n" );
1068 #endif
1069 sjcd_transfer_state = SJCD_S_STOP;
1070 goto ReSwitch;
1072 if( sjcd_mode != SCMD_MODE_COOKED ){
1073 #if defined( SJCD_TRACE )
1074 printk( "SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n" );
1075 #endif
1076 sjcd_transfer_state = SJCD_S_STOP;
1077 goto ReSwitch;
1081 if( CURRENT_IS_VALID ){
1082 struct sjcd_play_msf msf;
1084 sjcd_next_bn = CURRENT->sector / 4;
1085 hsg2msf( sjcd_next_bn, &msf.start );
1086 msf.end.min = 0; msf.end.sec = 0;
1087 msf.end.frame = sjcd_read_count = SJCD_BUF_SIZ;
1088 #if defined( SJCD_TRACE )
1089 printk( "SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1090 msf.start.min, msf.start.sec, msf.start.frame,
1091 msf.end.min, msf.end.sec, msf.end.frame );
1092 printk( "sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n", \
1093 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1094 sjcd_buf_bn[ sjcd_buf_in ] );
1095 #endif
1096 sjcd_send_6_cmd( SCMD_DATA_READ, &msf );
1097 sjcd_transfer_state = SJCD_S_DATA;
1098 sjcd_transfer_timeout = 500;
1099 #if defined( SJCD_TRACE )
1100 printk( "SJCD_S_READ: go to SJCD_S_DATA mode\n" );
1101 #endif
1102 } else {
1103 #if defined( SJCD_TRACE )
1104 printk( "SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n" );
1105 #endif
1106 sjcd_transfer_state = SJCD_S_STOP;
1107 goto ReSwitch;
1110 #if defined( SJCD_GATHER_STAT )
1111 else statistic.read_ticks++;
1112 #endif
1113 break;
1116 case SJCD_S_DATA:{
1117 unsigned char stat;
1119 sjcd_s_data: stat = inb( SJCDPORT( 1 ) );
1120 #if defined( SJCD_TRACE )
1121 printk( "SJCD_S_DATA: status = 0x%02x\n", stat );
1122 #endif
1123 if( SJCD_STATUS_AVAILABLE( stat ) ){
1125 * No data is waiting for us in the drive buffer. Status of operation
1126 * completion is available. Read and parse it.
1128 sjcd_load_status();
1130 if( !sjcd_status_valid || sjcd_command_failed ){
1131 #if defined( SJCD_TRACE )
1132 printk( "SJCD: read block %d failed, maybe audio disk? Giving up\n",
1133 sjcd_next_bn );
1134 #endif
1135 if( CURRENT_IS_VALID ) end_request( 0 );
1136 #if defined( SJCD_TRACE )
1137 printk( "SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n" );
1138 #endif
1139 sjcd_transfer_state = SJCD_S_STOP;
1140 goto ReSwitch;
1143 if( !sjcd_media_is_available ){
1144 printk( "SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n" );
1145 sjcd_transfer_state = SJCD_S_STOP;
1146 goto ReSwitch;
1149 sjcd_transfer_state = SJCD_S_READ;
1150 goto ReSwitch;
1151 } else if( SJCD_DATA_AVAILABLE( stat ) ){
1153 * One frame is read into device buffer. We must copy it to our memory.
1154 * Otherwise cdrom hangs up. Check to see if we have something to copy
1155 * to.
1157 if( !CURRENT_IS_VALID && sjcd_buf_in == sjcd_buf_out ){
1158 #if defined( SJCD_TRACE )
1159 printk( "SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n" );
1160 printk( " ... all the date would be discarded\n" );
1161 #endif
1162 sjcd_transfer_state = SJCD_S_STOP;
1163 goto ReSwitch;
1167 * Everything seems to be OK. Just read the frame and recalculate
1168 * indices.
1170 sjcd_buf_bn[ sjcd_buf_in ] = -1; /* ??? */
1171 insb( SJCDPORT( 2 ), sjcd_buf + 2048 * sjcd_buf_in, 2048 );
1172 #if defined( SJCD_TRACE )
1173 printk( "SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1174 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1175 sjcd_buf_bn[ sjcd_buf_in ] );
1176 #endif
1177 sjcd_buf_bn[ sjcd_buf_in ] = sjcd_next_bn++;
1178 if( sjcd_buf_out == -1 ) sjcd_buf_out = sjcd_buf_in;
1179 if( ++sjcd_buf_in == SJCD_BUF_SIZ ) sjcd_buf_in = 0;
1182 * Only one frame is ready at time. So we should turn over to wait for
1183 * another frame. If we need that, of course.
1185 if( --sjcd_read_count == 0 ){
1187 * OK, request seems to be precessed. Continue transferring...
1189 if( !sjcd_transfer_is_active ){
1190 while( CURRENT_IS_VALID ){
1192 * Continue transferring.
1194 sjcd_transfer();
1195 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1196 else break;
1199 if( CURRENT_IS_VALID &&
1200 ( CURRENT->sector / 4 < sjcd_next_bn ||
1201 CURRENT->sector / 4 > sjcd_next_bn + SJCD_BUF_SIZ ) ){
1202 #if defined( SJCD_TRACE )
1203 printk( "SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n" );
1204 #endif
1205 sjcd_transfer_state = SJCD_S_STOP;
1206 goto ReSwitch;
1210 * Now we should turn around rather than wait for while.
1212 goto sjcd_s_data;
1214 #if defined( SJCD_GATHER_STAT )
1215 else statistic.data_ticks++;
1216 #endif
1217 break;
1220 case SJCD_S_STOP:{
1221 sjcd_read_count = 0;
1222 sjcd_send_cmd( SCMD_STOP );
1223 sjcd_transfer_state = SJCD_S_STOPPING;
1224 sjcd_transfer_timeout = 500;
1225 #if defined( SJCD_GATHER_STAT )
1226 statistic.stop_ticks++;
1227 #endif
1228 break;
1231 case SJCD_S_STOPPING:{
1232 unsigned char stat;
1234 stat = inb( SJCDPORT( 1 ) );
1235 #if defined( SJCD_TRACE )
1236 printk( "SJCD_S_STOP: status = 0x%02x\n", stat );
1237 #endif
1238 if( SJCD_DATA_AVAILABLE( stat ) ){
1239 int i;
1240 #if defined( SJCD_TRACE )
1241 printk( "SJCD_S_STOP: discard data\n" );
1242 #endif
1244 * Discard all the data from the pipe. Foolish method.
1246 for( i = 2048; i--; ( void )inb( SJCDPORT( 2 ) ) );
1247 sjcd_transfer_timeout = 500;
1248 } else if( SJCD_STATUS_AVAILABLE( stat ) ){
1249 sjcd_load_status();
1250 if( sjcd_status_valid && sjcd_media_is_changed ) {
1251 sjcd_toc_uptodate = 0;
1252 sjcd_invalidate_buffers();
1254 if( CURRENT_IS_VALID ){
1255 if( sjcd_status_valid ) sjcd_transfer_state = SJCD_S_READ;
1256 else sjcd_transfer_state = SJCD_S_START;
1257 } else sjcd_transfer_state = SJCD_S_IDLE;
1258 goto ReSwitch;
1260 #if defined( SJCD_GATHER_STAT )
1261 else statistic.stopping_ticks++;
1262 #endif
1263 break;
1266 default:
1267 printk( "SJCD: poll: invalid state %d\n", sjcd_transfer_state );
1268 return;
1271 if( --sjcd_transfer_timeout == 0 ){
1272 printk( "SJCD: timeout in state %d\n", sjcd_transfer_state );
1273 while( CURRENT_IS_VALID ) end_request( 0 );
1274 sjcd_send_cmd( SCMD_STOP );
1275 sjcd_transfer_state = SJCD_S_IDLE;
1276 goto ReSwitch;
1280 * Get back in some time. 1 should be replaced with count variable to
1281 * avoid unnecessary testings.
1283 SJCD_SET_TIMER( sjcd_poll, 1 );
1286 static void do_sjcd_request( request_queue_t * q ){
1287 #if defined( SJCD_TRACE )
1288 printk( "SJCD: do_sjcd_request(%ld+%ld)\n",
1289 CURRENT->sector, CURRENT->nr_sectors );
1290 #endif
1291 sjcd_transfer_is_active = 1;
1292 while( CURRENT_IS_VALID ){
1294 * Who of us are paranoiac?
1296 if( CURRENT->bh && !buffer_locked(CURRENT->bh) )
1297 panic( DEVICE_NAME ": block not locked" );
1299 sjcd_transfer();
1300 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1301 else {
1302 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1303 if( sjcd_transfer_state == SJCD_S_IDLE ){
1304 if( !sjcd_toc_uptodate ){
1305 if( sjcd_update_toc() < 0 ){
1306 printk( "SJCD: transfer: discard\n" );
1307 while( CURRENT_IS_VALID ) end_request( 0 );
1308 break;
1311 sjcd_transfer_state = SJCD_S_START;
1312 SJCD_SET_TIMER( sjcd_poll, HZ/100 );
1314 break;
1317 sjcd_transfer_is_active = 0;
1318 #if defined( SJCD_TRACE )
1319 printk( "sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1320 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out, sjcd_buf_bn[ sjcd_buf_in ] );
1321 printk( "do_sjcd_request ends\n" );
1322 #endif
1326 * Open the device special file. Check disk is in.
1328 int sjcd_open( struct inode *ip, struct file *fp ){
1330 * Check the presence of device.
1332 if( !sjcd_present ) return( -ENXIO );
1335 * Only read operations are allowed. Really? (:-)
1337 if( fp->f_mode & 2 ) return( -EROFS );
1339 MOD_INC_USE_COUNT;
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 goto err_out;
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 goto err_out;
1379 continue;
1380 } else goto err_out;
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 goto err_out;
1391 #if defined( SJCD_TRACE )
1392 printk( "SJCD: open: done\n" );
1393 #endif
1396 ++sjcd_open_count;
1397 return( 0 );
1399 err_out:
1400 MOD_DEC_USE_COUNT;
1401 return( -EIO );
1405 * On close, we flush all sjcd blocks from the buffer cache.
1407 static int sjcd_release( struct inode *inode, struct file *file ){
1408 int s;
1410 #if defined( SJCD_TRACE )
1411 printk( "SJCD: release\n" );
1412 #endif
1413 #ifdef MODULE
1414 MOD_DEC_USE_COUNT;
1415 #endif
1416 if( --sjcd_open_count == 0 ){
1417 sjcd_invalidate_buffers();
1418 s = sjcd_tray_unlock();
1419 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1420 #if defined( SJCD_DIAGNOSTIC )
1421 printk("SJCD: release: tray unlock attempt failed.\n");
1422 #endif
1424 if( sjcd_door_was_open ){
1425 s = sjcd_tray_open();
1426 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1427 #if defined( SJCD_DIAGNOSTIC )
1428 printk("SJCD: release: tray unload attempt failed.\n");
1429 #endif
1433 return 0;
1437 * A list of file operations allowed for this cdrom.
1439 static struct block_device_operations sjcd_fops = {
1440 open: sjcd_open,
1441 release: sjcd_release,
1442 ioctl: sjcd_ioctl,
1443 check_media_change: sjcd_disk_change,
1446 static int blksize = 2048;
1447 static int secsize = 2048;
1450 * Following stuff is intended for initialization of the cdrom. It
1451 * first looks for presence of device. If the device is present, it
1452 * will be reset. Then read the version of the drive and load status.
1453 * The version is two BCD-coded bytes.
1455 static struct {
1456 unsigned char major, minor;
1457 } sjcd_version;
1460 * Test for presence of drive and initialize it. Called at boot time.
1461 * Probe cdrom, find out version and status.
1463 int __init sjcd_init( void ){
1464 int i;
1466 printk(KERN_INFO "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n", SJCD_VERSION_MAJOR,
1467 SJCD_VERSION_MINOR);
1469 #if defined( SJCD_TRACE )
1470 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1471 #endif
1473 hardsect_size[MAJOR_NR] = &secsize;
1474 blksize_size[MAJOR_NR] = &blksize;
1476 if( devfs_register_blkdev( MAJOR_NR, "sjcd", &sjcd_fops ) != 0 ){
1477 printk( "SJCD: Unable to get major %d for Sanyo CD-ROM\n", MAJOR_NR );
1478 return( -EIO );
1481 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1482 read_ahead[ MAJOR_NR ] = 4;
1483 register_disk(NULL, MKDEV(MAJOR_NR,0), 1, &sjcd_fops, 0);
1485 if( check_region( sjcd_base, 4 ) ){
1486 printk( "SJCD: Init failed, I/O port (%X) is already in use\n",
1487 sjcd_base );
1488 sjcd_cleanup();
1489 return( -EIO );
1493 * Check for card. Since we are booting now, we can't use standard
1494 * wait algorithm.
1496 printk(KERN_INFO "SJCD: Resetting: " );
1497 sjcd_send_cmd( SCMD_RESET );
1498 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1499 unsigned long timer;
1502 * Wait 10ms approx.
1504 for( timer = jiffies; time_before_eq(jiffies, timer); );
1505 if ( (i % 100) == 0 ) printk( "." );
1506 ( void )sjcd_check_status();
1508 if( i == 0 || sjcd_command_failed ){
1509 printk( " reset failed, no drive found.\n" );
1510 sjcd_cleanup();
1511 return( -EIO );
1512 } else printk( "\n" );
1515 * Get and print out cdrom version.
1517 printk(KERN_INFO "SJCD: Getting version: " );
1518 sjcd_send_cmd( SCMD_GET_VERSION );
1519 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1520 unsigned long timer;
1523 * Wait 10ms approx.
1525 for( timer = jiffies; time_before_eq(jiffies, timer); );
1526 if ( (i % 100) == 0 ) printk( "." );
1527 ( void )sjcd_check_status();
1529 if( i == 0 || sjcd_command_failed ){
1530 printk( " get version failed, no drive found.\n" );
1531 sjcd_cleanup();
1532 return( -EIO );
1535 if( sjcd_load_response( &sjcd_version, sizeof( sjcd_version ) ) == 0 ){
1536 printk( " %1x.%02x\n", ( int )sjcd_version.major,
1537 ( int )sjcd_version.minor );
1538 } else {
1539 printk( " read version failed, no drive found.\n" );
1540 sjcd_cleanup();
1541 return( -EIO );
1545 * Check and print out the tray state. (if it is needed?).
1547 if( !sjcd_status_valid ){
1548 printk(KERN_INFO "SJCD: Getting status: " );
1549 sjcd_send_cmd( SCMD_GET_STATUS );
1550 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1551 unsigned long timer;
1554 * Wait 10ms approx.
1556 for( timer = jiffies; time_before_eq(jiffies, timer); );
1557 if ( (i % 100) == 0 ) printk( "." );
1558 ( void )sjcd_check_status();
1560 if( i == 0 || sjcd_command_failed ){
1561 printk( " get status failed, no drive found.\n" );
1562 sjcd_cleanup();
1563 return( -EIO );
1564 } else printk( "\n" );
1567 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1568 devfs_register (NULL, "sjcd", DEVFS_FL_DEFAULT, MAJOR_NR, 0,
1569 S_IFBLK | S_IRUGO | S_IWUGO, &sjcd_fops, NULL);
1571 sjcd_present++;
1572 return( 0 );
1575 static int
1576 sjcd_cleanup(void)
1578 if( (devfs_unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL) )
1579 printk( "SJCD: cannot unregister device.\n" );
1580 else {
1581 release_region( sjcd_base, 4 );
1582 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1585 return(0);
1589 void __exit sjcd_exit(void)
1591 devfs_unregister(devfs_find_handle(NULL, "sjcd", 0, 0, DEVFS_SPECIAL_BLK,0));
1592 if ( sjcd_cleanup() )
1593 printk( "SJCD: module: cannot be removed.\n" );
1594 else
1595 printk(KERN_INFO "SJCD: module: removed.\n");
1598 #ifdef MODULE
1599 module_init(sjcd_init);
1600 #endif
1601 module_exit(sjcd_exit);