Import 2.3.10pre5
[davej-history.git] / drivers / cdrom / sjcd.c
blob35697369fec70095dcaa1ca42431dcf58205818a
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.
54 #define SJCD_VERSION_MAJOR 1
55 #define SJCD_VERSION_MINOR 7
57 #ifdef MODULE
58 #include <linux/module.h>
59 #endif /* MODULE */
61 #include <linux/errno.h>
62 #include <linux/sched.h>
63 #include <linux/mm.h>
64 #include <linux/timer.h>
65 #include <linux/fs.h>
66 #include <linux/kernel.h>
67 #include <linux/cdrom.h>
68 #include <linux/ioport.h>
69 #include <linux/string.h>
70 #include <linux/major.h>
71 #include <linux/init.h>
73 #include <asm/system.h>
74 #include <asm/io.h>
75 #include <asm/uaccess.h>
77 #define MAJOR_NR SANYO_CDROM_MAJOR
78 #include <linux/blk.h>
79 #include "sjcd.h"
81 static int sjcd_present = 0;
83 #define SJCD_BUF_SIZ 32 /* cdr-h94a has internal 64K buffer */
86 * buffer for block size conversion
88 static char sjcd_buf[ 2048 * SJCD_BUF_SIZ ];
89 static volatile int sjcd_buf_bn[ SJCD_BUF_SIZ ], sjcd_next_bn;
90 static volatile int sjcd_buf_in, sjcd_buf_out = -1;
93 * Status.
95 static unsigned short sjcd_status_valid = 0;
96 static unsigned short sjcd_door_closed;
97 static unsigned short sjcd_door_was_open;
98 static unsigned short sjcd_media_is_available;
99 static unsigned short sjcd_media_is_changed;
100 static unsigned short sjcd_toc_uptodate = 0;
101 static unsigned short sjcd_command_failed;
102 static volatile unsigned char sjcd_completion_status = 0;
103 static volatile unsigned char sjcd_completion_error = 0;
104 static unsigned short sjcd_command_is_in_progress = 0;
105 static unsigned short sjcd_error_reported = 0;
107 static int sjcd_open_count;
109 static int sjcd_audio_status;
110 static struct sjcd_play_msf sjcd_playing;
112 static int sjcd_base = SJCD_BASE_ADDR;
114 #ifdef MODULE
115 MODULE_PARM(sjcd_base, "i");
116 #endif
118 static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
121 * Data transfer.
123 static volatile unsigned short sjcd_transfer_is_active = 0;
125 enum sjcd_transfer_state {
126 SJCD_S_IDLE = 0,
127 SJCD_S_START = 1,
128 SJCD_S_MODE = 2,
129 SJCD_S_READ = 3,
130 SJCD_S_DATA = 4,
131 SJCD_S_STOP = 5,
132 SJCD_S_STOPPING = 6
134 static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
135 static long sjcd_transfer_timeout = 0;
136 static int sjcd_read_count = 0;
137 static unsigned char sjcd_mode = 0;
139 #define SJCD_READ_TIMEOUT 5000
141 #if defined( SJCD_GATHER_STAT )
143 * Statistic.
145 static struct sjcd_stat statistic;
146 #endif
149 * Timer.
151 static struct timer_list sjcd_delay_timer = { NULL, NULL, 0, 0, NULL };
153 #define SJCD_SET_TIMER( func, tmout ) \
154 ( sjcd_delay_timer.expires = jiffies+tmout, \
155 sjcd_delay_timer.function = ( void * )func, \
156 add_timer( &sjcd_delay_timer ) )
158 #define CLEAR_TIMER del_timer( &sjcd_delay_timer )
160 static int sjcd_cleanup(void);
163 * Set up device, i.e., use command line data to set
164 * base address.
166 void __init sjcd_setup( char *str, int *ints )
168 if (ints[0] > 0)
169 sjcd_base = ints[1];
173 * Special converters.
175 static unsigned char bin2bcd( int bin ){
176 int u, v;
178 u = bin % 10; v = bin / 10;
179 return( u | ( v << 4 ) );
182 static int bcd2bin( unsigned char bcd ){
183 return( ( bcd >> 4 ) * 10 + ( bcd & 0x0F ) );
186 static long msf2hsg( struct msf *mp ){
187 return( bcd2bin( mp->frame ) + bcd2bin( mp->sec ) * 75
188 + bcd2bin( mp->min ) * 4500 - 150 );
191 static void hsg2msf( long hsg, struct msf *msf ){
192 hsg += 150; msf->min = hsg / 4500;
193 hsg %= 4500; msf->sec = hsg / 75; msf->frame = hsg % 75;
194 msf->min = bin2bcd( msf->min ); /* convert to BCD */
195 msf->sec = bin2bcd( msf->sec );
196 msf->frame = bin2bcd( msf->frame );
200 * Send a command to cdrom. Invalidate status.
202 static void sjcd_send_cmd( unsigned char cmd ){
203 #if defined( SJCD_TRACE )
204 printk( "SJCD: send_cmd( 0x%x )\n", cmd );
205 #endif
206 outb( cmd, SJCDPORT( 0 ) );
207 sjcd_command_is_in_progress = 1;
208 sjcd_status_valid = 0;
209 sjcd_command_failed = 0;
213 * Send a command with one arg to cdrom. Invalidate status.
215 static void sjcd_send_1_cmd( unsigned char cmd, unsigned char a ){
216 #if defined( SJCD_TRACE )
217 printk( "SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a );
218 #endif
219 outb( cmd, SJCDPORT( 0 ) );
220 outb( a, SJCDPORT( 0 ) );
221 sjcd_command_is_in_progress = 1;
222 sjcd_status_valid = 0;
223 sjcd_command_failed = 0;
227 * Send a command with four args to cdrom. Invalidate status.
229 static void sjcd_send_4_cmd( unsigned char cmd, unsigned char a,
230 unsigned char b, unsigned char c, unsigned char d ){
231 #if defined( SJCD_TRACE )
232 printk( "SJCD: send_4_cmd( 0x%x )\n", cmd );
233 #endif
234 outb( cmd, SJCDPORT( 0 ) );
235 outb( a, SJCDPORT( 0 ) );
236 outb( b, SJCDPORT( 0 ) );
237 outb( c, SJCDPORT( 0 ) );
238 outb( d, SJCDPORT( 0 ) );
239 sjcd_command_is_in_progress = 1;
240 sjcd_status_valid = 0;
241 sjcd_command_failed = 0;
245 * Send a play or read command to cdrom. Invalidate Status.
247 static void sjcd_send_6_cmd( unsigned char cmd, struct sjcd_play_msf *pms ){
248 #if defined( SJCD_TRACE )
249 printk( "SJCD: send_long_cmd( 0x%x )\n", cmd );
250 #endif
251 outb( cmd, SJCDPORT( 0 ) );
252 outb( pms->start.min, SJCDPORT( 0 ) );
253 outb( pms->start.sec, SJCDPORT( 0 ) );
254 outb( pms->start.frame, SJCDPORT( 0 ) );
255 outb( pms->end.min, SJCDPORT( 0 ) );
256 outb( pms->end.sec, SJCDPORT( 0 ) );
257 outb( pms->end.frame, SJCDPORT( 0 ) );
258 sjcd_command_is_in_progress = 1;
259 sjcd_status_valid = 0;
260 sjcd_command_failed = 0;
264 * Get a value from the data port. Should not block, so we use a little
265 * wait for a while. Returns 0 if OK.
267 static int sjcd_load_response( void *buf, int len ){
268 unsigned char *resp = ( unsigned char * )buf;
270 for( ; len; --len ){
271 int i;
272 for( i = 200; i-- && !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ); );
273 if( i > 0 ) *resp++ = ( unsigned char )inb( SJCDPORT( 0 ) );
274 else break;
276 return( len );
280 * Load and parse command completion status (drive info byte and maybe error).
281 * Sorry, no error classification yet.
283 static void sjcd_load_status( void ){
284 sjcd_media_is_changed = 0;
285 sjcd_completion_error = 0;
286 sjcd_completion_status = inb( SJCDPORT( 0 ) );
287 if( sjcd_completion_status & SST_DOOR_OPENED ){
288 sjcd_door_closed = sjcd_media_is_available = 0;
289 } else {
290 sjcd_door_closed = 1;
291 if( sjcd_completion_status & SST_MEDIA_CHANGED )
292 sjcd_media_is_available = sjcd_media_is_changed = 1;
293 else if( sjcd_completion_status & 0x0F ){
295 * OK, we seem to catch an error ...
297 while( !SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) );
298 sjcd_completion_error = inb( SJCDPORT( 0 ) );
299 if( ( sjcd_completion_status & 0x08 ) &&
300 ( sjcd_completion_error & 0x40 ) )
301 sjcd_media_is_available = 0;
302 else sjcd_command_failed = 1;
303 } else sjcd_media_is_available = 1;
306 * Ok, status loaded successfully.
308 sjcd_status_valid = 1, sjcd_error_reported = 0;
309 sjcd_command_is_in_progress = 0;
312 * If the disk is changed, the TOC is not valid.
314 if( sjcd_media_is_changed ) sjcd_toc_uptodate = 0;
315 #if defined( SJCD_TRACE )
316 printk( "SJCD: status %02x.%02x loaded.\n",
317 ( int )sjcd_completion_status, ( int )sjcd_completion_error );
318 #endif
322 * Read status from cdrom. Check to see if the status is available.
324 static int sjcd_check_status( void ){
326 * Try to load the response from cdrom into buffer.
328 if( SJCD_STATUS_AVAILABLE( inb( SJCDPORT( 1 ) ) ) ){
329 sjcd_load_status();
330 return( 1 );
331 } else {
333 * No status is available.
335 return( 0 );
340 * This is just timeout counter, and nothing more. Surprised ? :-)
342 static volatile long sjcd_status_timeout;
345 * We need about 10 seconds to wait. The longest command takes about 5 seconds
346 * to probe the disk (usually after tray closed or drive reset). Other values
347 * should be thought of for other commands.
349 #define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
351 static void sjcd_status_timer( void ){
352 if( sjcd_check_status() ){
354 * The command completed and status is loaded, stop waiting.
356 wake_up( &sjcd_waitq );
357 } else if( --sjcd_status_timeout <= 0 ){
359 * We are timed out.
361 wake_up( &sjcd_waitq );
362 } else {
364 * We have still some time to wait. Try again.
366 SJCD_SET_TIMER( sjcd_status_timer, 1 );
371 * Wait for status for 10 sec approx. Returns non-positive when timed out.
372 * Should not be used while reading data CDs.
374 static int sjcd_wait_for_status( void ){
375 sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
376 SJCD_SET_TIMER( sjcd_status_timer, 1 );
377 sleep_on( &sjcd_waitq );
378 #if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
379 if( sjcd_status_timeout <= 0 )
380 printk( "SJCD: Error Wait For Status.\n" );
381 #endif
382 return( sjcd_status_timeout );
385 static int sjcd_receive_status( void ){
386 int i;
387 #if defined( SJCD_TRACE )
388 printk( "SJCD: receive_status\n" );
389 #endif
391 * Wait a bit for status available.
393 for( i = 200; i-- && ( sjcd_check_status() == 0 ); );
394 if( i < 0 ){
395 #if defined( SJCD_TRACE )
396 printk( "SJCD: long wait for status\n" );
397 #endif
398 if( sjcd_wait_for_status() <= 0 )
399 printk( "SJCD: Timeout when read status.\n" );
400 else i = 0;
402 return( i );
406 * Load the status. Issue get status command and wait for status available.
408 static void sjcd_get_status( void ){
409 #if defined( SJCD_TRACE )
410 printk( "SJCD: get_status\n" );
411 #endif
412 sjcd_send_cmd( SCMD_GET_STATUS );
413 sjcd_receive_status();
417 * Check the drive if the disk is changed. Should be revised.
419 static int sjcd_disk_change( kdev_t full_dev ){
420 #if 0
421 printk( "SJCD: sjcd_disk_change( 0x%x )\n", full_dev );
422 #endif
423 if( MINOR( full_dev ) > 0 ){
424 printk( "SJCD: request error: invalid device minor.\n" );
425 return 0;
427 if( !sjcd_command_is_in_progress )
428 sjcd_get_status();
429 return( sjcd_status_valid ? sjcd_media_is_changed : 0 );
433 * Read the table of contents (TOC) and TOC header if necessary.
434 * We assume that the drive contains no more than 99 toc entries.
436 static struct sjcd_hw_disk_info sjcd_table_of_contents[ SJCD_MAX_TRACKS ];
437 static unsigned char sjcd_first_track_no, sjcd_last_track_no;
438 #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
440 static int sjcd_update_toc( void ){
441 struct sjcd_hw_disk_info info;
442 int i;
443 #if defined( SJCD_TRACE )
444 printk( "SJCD: update toc:\n" );
445 #endif
447 * check to see if we need to do anything
449 if( sjcd_toc_uptodate ) return( 0 );
452 * Get the TOC start information.
454 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK );
455 sjcd_receive_status();
457 if( !sjcd_status_valid ){
458 printk( "SJCD: cannot load status.\n" );
459 return( -1 );
462 if( !sjcd_media_is_available ){
463 printk( "SJCD: no disk in drive\n" );
464 return( -1 );
467 if( !sjcd_command_failed ){
468 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
469 printk( "SJCD: cannot load response about TOC start.\n" );
470 return( -1 );
472 sjcd_first_track_no = bcd2bin( info.un.track_no );
473 } else {
474 printk( "SJCD: get first failed\n" );
475 return( -1 );
477 #if defined( SJCD_TRACE )
478 printk( "SJCD: TOC start 0x%02x ", sjcd_first_track_no );
479 #endif
481 * Get the TOC finish information.
483 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK );
484 sjcd_receive_status();
486 if( !sjcd_status_valid ){
487 printk( "SJCD: cannot load status.\n" );
488 return( -1 );
491 if( !sjcd_media_is_available ){
492 printk( "SJCD: no disk in drive\n" );
493 return( -1 );
496 if( !sjcd_command_failed ){
497 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
498 printk( "SJCD: cannot load response about TOC finish.\n" );
499 return( -1 );
501 sjcd_last_track_no = bcd2bin( info.un.track_no );
502 } else {
503 printk( "SJCD: get last failed\n" );
504 return( -1 );
506 #if defined( SJCD_TRACE )
507 printk( "SJCD: TOC finish 0x%02x ", sjcd_last_track_no );
508 #endif
509 for( i = sjcd_first_track_no; i <= sjcd_last_track_no; i++ ){
511 * Get the first track information.
513 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, bin2bcd( i ) );
514 sjcd_receive_status();
516 if( !sjcd_status_valid ){
517 printk( "SJCD: cannot load status.\n" );
518 return( -1 );
521 if( !sjcd_media_is_available ){
522 printk( "SJCD: no disk in drive\n" );
523 return( -1 );
526 if( !sjcd_command_failed ){
527 if( sjcd_load_response( &sjcd_table_of_contents[ i ],
528 sizeof( struct sjcd_hw_disk_info ) ) != 0 ){
529 printk( "SJCD: cannot load info for %d track\n", i );
530 return( -1 );
532 } else {
533 printk( "SJCD: get info %d failed\n", i );
534 return( -1 );
539 * Get the disk length info.
541 sjcd_send_1_cmd( SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE );
542 sjcd_receive_status();
544 if( !sjcd_status_valid ){
545 printk( "SJCD: cannot load status.\n" );
546 return( -1 );
549 if( !sjcd_media_is_available ){
550 printk( "SJCD: no disk in drive\n" );
551 return( -1 );
554 if( !sjcd_command_failed ){
555 if( sjcd_load_response( &info, sizeof( info ) ) != 0 ){
556 printk( "SJCD: cannot load response about disk size.\n" );
557 return( -1 );
559 sjcd_disk_length.min = info.un.track_msf.min;
560 sjcd_disk_length.sec = info.un.track_msf.sec;
561 sjcd_disk_length.frame = info.un.track_msf.frame;
562 } else {
563 printk( "SJCD: get size failed\n" );
564 return( 1 );
566 #if defined( SJCD_TRACE )
567 printk( "SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
568 sjcd_disk_length.sec, sjcd_disk_length.frame );
569 #endif
570 return( 0 );
574 * Load subchannel information.
576 static int sjcd_get_q_info( struct sjcd_hw_qinfo *qp ){
577 int s;
578 #if defined( SJCD_TRACE )
579 printk( "SJCD: load sub q\n" );
580 #endif
581 sjcd_send_cmd( SCMD_GET_QINFO );
582 s = sjcd_receive_status();
583 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ){
584 sjcd_send_cmd( 0xF2 );
585 s = sjcd_receive_status();
586 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
587 sjcd_send_cmd( SCMD_GET_QINFO );
588 s = sjcd_receive_status();
589 if( s < 0 || sjcd_command_failed || !sjcd_status_valid ) return( -1 );
591 if( sjcd_media_is_available )
592 if( sjcd_load_response( qp, sizeof( *qp ) ) == 0 ) return( 0 );
593 return( -1 );
597 * Start playing from the specified position.
599 static int sjcd_play( struct sjcd_play_msf *mp ){
600 struct sjcd_play_msf msf;
603 * Turn the device to play mode.
605 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_PLAY );
606 if( sjcd_receive_status() < 0 ) return( -1 );
609 * Seek to the starting point.
611 msf.start = mp->start;
612 msf.end.min = msf.end.sec = msf.end.frame = 0x00;
613 sjcd_send_6_cmd( SCMD_SEEK, &msf );
614 if( sjcd_receive_status() < 0 ) return( -1 );
617 * Start playing.
619 sjcd_send_6_cmd( SCMD_PLAY, mp );
620 return( sjcd_receive_status() );
624 * Tray control functions.
626 static int sjcd_tray_close( void ){
627 #if defined( SJCD_TRACE )
628 printk( "SJCD: tray_close\n" );
629 #endif
630 sjcd_send_cmd( SCMD_CLOSE_TRAY );
631 return( sjcd_receive_status() );
634 static int sjcd_tray_lock( void ){
635 #if defined( SJCD_TRACE )
636 printk( "SJCD: tray_lock\n" );
637 #endif
638 sjcd_send_cmd( SCMD_LOCK_TRAY );
639 return( sjcd_receive_status() );
642 static int sjcd_tray_unlock( void ){
643 #if defined( SJCD_TRACE )
644 printk( "SJCD: tray_unlock\n" );
645 #endif
646 sjcd_send_cmd( SCMD_UNLOCK_TRAY );
647 return( sjcd_receive_status() );
650 static int sjcd_tray_open( void ){
651 #if defined( SJCD_TRACE )
652 printk( "SJCD: tray_open\n" );
653 #endif
654 sjcd_send_cmd( SCMD_EJECT_TRAY );
655 return( sjcd_receive_status() );
659 * Do some user commands.
661 static int sjcd_ioctl( struct inode *ip, struct file *fp,
662 unsigned int cmd, unsigned long arg ){
663 #if defined( SJCD_TRACE )
664 printk( "SJCD:ioctl\n" );
665 #endif
667 if( ip == NULL ) return( -EINVAL );
669 sjcd_get_status();
670 if( !sjcd_status_valid ) return( -EIO );
671 if( sjcd_update_toc() < 0 ) return( -EIO );
673 switch( cmd ){
674 case CDROMSTART:{
675 #if defined( SJCD_TRACE )
676 printk( "SJCD: ioctl: start\n" );
677 #endif
678 return( 0 );
681 case CDROMSTOP:{
682 #if defined( SJCD_TRACE )
683 printk( "SJCD: ioctl: stop\n" );
684 #endif
685 sjcd_send_cmd( SCMD_PAUSE );
686 ( void )sjcd_receive_status();
687 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
688 return( 0 );
691 case CDROMPAUSE:{
692 struct sjcd_hw_qinfo q_info;
693 #if defined( SJCD_TRACE )
694 printk( "SJCD: ioctl: pause\n" );
695 #endif
696 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
697 sjcd_send_cmd( SCMD_PAUSE );
698 ( void )sjcd_receive_status();
699 if( sjcd_get_q_info( &q_info ) < 0 ){
700 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
701 } else {
702 sjcd_audio_status = CDROM_AUDIO_PAUSED;
703 sjcd_playing.start = q_info.abs;
705 return( 0 );
706 } else return( -EINVAL );
709 case CDROMRESUME:{
710 #if defined( SJCD_TRACE )
711 printk( "SJCD: ioctl: resume\n" );
712 #endif
713 if( sjcd_audio_status == CDROM_AUDIO_PAUSED ){
715 * continue play starting at saved location
717 if( sjcd_play( &sjcd_playing ) < 0 ){
718 sjcd_audio_status = CDROM_AUDIO_ERROR;
719 return( -EIO );
720 } else {
721 sjcd_audio_status = CDROM_AUDIO_PLAY;
722 return( 0 );
724 } else return( -EINVAL );
727 case CDROMPLAYTRKIND:{
728 struct cdrom_ti ti; int s;
729 #if defined( SJCD_TRACE )
730 printk( "SJCD: ioctl: playtrkind\n" );
731 #endif
732 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( ti ) ) ) == 0 ){
733 copy_from_user( &ti, (void *)arg, sizeof( ti ) );
735 if( ti.cdti_trk0 < sjcd_first_track_no ) return( -EINVAL );
736 if( ti.cdti_trk1 > sjcd_last_track_no )
737 ti.cdti_trk1 = sjcd_last_track_no;
738 if( ti.cdti_trk0 > ti.cdti_trk1 ) return( -EINVAL );
740 sjcd_playing.start = sjcd_table_of_contents[ ti.cdti_trk0 ].un.track_msf;
741 sjcd_playing.end = ( ti.cdti_trk1 < sjcd_last_track_no ) ?
742 sjcd_table_of_contents[ ti.cdti_trk1 + 1 ].un.track_msf :
743 sjcd_table_of_contents[ 0 ].un.track_msf;
745 if( sjcd_play( &sjcd_playing ) < 0 ){
746 sjcd_audio_status = CDROM_AUDIO_ERROR;
747 return( -EIO );
748 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
750 return( s );
753 case CDROMPLAYMSF:{
754 struct cdrom_msf sjcd_msf; int s;
755 #if defined( SJCD_TRACE )
756 printk( "SJCD: ioctl: playmsf\n" );
757 #endif
758 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( sjcd_msf ) ) ) == 0 ){
759 if( sjcd_audio_status == CDROM_AUDIO_PLAY ){
760 sjcd_send_cmd( SCMD_PAUSE );
761 ( void )sjcd_receive_status();
762 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
765 copy_from_user( &sjcd_msf, (void *)arg, sizeof( sjcd_msf ) );
767 sjcd_playing.start.min = bin2bcd( sjcd_msf.cdmsf_min0 );
768 sjcd_playing.start.sec = bin2bcd( sjcd_msf.cdmsf_sec0 );
769 sjcd_playing.start.frame = bin2bcd( sjcd_msf.cdmsf_frame0 );
770 sjcd_playing.end.min = bin2bcd( sjcd_msf.cdmsf_min1 );
771 sjcd_playing.end.sec = bin2bcd( sjcd_msf.cdmsf_sec1 );
772 sjcd_playing.end.frame = bin2bcd( sjcd_msf.cdmsf_frame1 );
774 if( sjcd_play( &sjcd_playing ) < 0 ){
775 sjcd_audio_status = CDROM_AUDIO_ERROR;
776 return( -EIO );
777 } else sjcd_audio_status = CDROM_AUDIO_PLAY;
779 return( s );
782 case CDROMREADTOCHDR:{
783 struct cdrom_tochdr toc_header; int s;
784 #if defined (SJCD_TRACE )
785 printk( "SJCD: ioctl: readtocheader\n" );
786 #endif
787 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_header ) ) ) == 0 ){
788 toc_header.cdth_trk0 = sjcd_first_track_no;
789 toc_header.cdth_trk1 = sjcd_last_track_no;
790 copy_to_user( (void *)arg, &toc_header, sizeof( toc_header ) );
792 return( s );
795 case CDROMREADTOCENTRY:{
796 struct cdrom_tocentry toc_entry; int s;
797 #if defined( SJCD_TRACE )
798 printk( "SJCD: ioctl: readtocentry\n" );
799 #endif
800 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( toc_entry ) ) ) == 0 ){
801 struct sjcd_hw_disk_info *tp;
803 copy_from_user( &toc_entry, (void *)arg, sizeof( toc_entry ) );
805 if( toc_entry.cdte_track == CDROM_LEADOUT )
806 tp = &sjcd_table_of_contents[ 0 ];
807 else if( toc_entry.cdte_track < sjcd_first_track_no ) return( -EINVAL );
808 else if( toc_entry.cdte_track > sjcd_last_track_no ) return( -EINVAL );
809 else tp = &sjcd_table_of_contents[ toc_entry.cdte_track ];
811 toc_entry.cdte_adr = tp->track_control & 0x0F;
812 toc_entry.cdte_ctrl = tp->track_control >> 4;
814 switch( toc_entry.cdte_format ){
815 case CDROM_LBA:
816 toc_entry.cdte_addr.lba = msf2hsg( &( tp->un.track_msf ) );
817 break;
818 case CDROM_MSF:
819 toc_entry.cdte_addr.msf.minute = bcd2bin( tp->un.track_msf.min );
820 toc_entry.cdte_addr.msf.second = bcd2bin( tp->un.track_msf.sec );
821 toc_entry.cdte_addr.msf.frame = bcd2bin( tp->un.track_msf.frame );
822 break;
823 default: return( -EINVAL );
825 copy_to_user( (void *)arg, &toc_entry, sizeof( toc_entry ) );
827 return( s );
830 case CDROMSUBCHNL:{
831 struct cdrom_subchnl subchnl; int s;
832 #if defined( SJCD_TRACE )
833 printk( "SJCD: ioctl: subchnl\n" );
834 #endif
835 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( subchnl ) ) ) == 0 ){
836 struct sjcd_hw_qinfo q_info;
838 copy_from_user( &subchnl, (void *)arg, sizeof( subchnl ) );
839 if( sjcd_get_q_info( &q_info ) < 0 ) return( -EIO );
841 subchnl.cdsc_audiostatus = sjcd_audio_status;
842 subchnl.cdsc_adr = q_info.track_control & 0x0F;
843 subchnl.cdsc_ctrl = q_info.track_control >> 4;
844 subchnl.cdsc_trk = bcd2bin( q_info.track_no );
845 subchnl.cdsc_ind = bcd2bin( q_info.x );
847 switch( subchnl.cdsc_format ){
848 case CDROM_LBA:
849 subchnl.cdsc_absaddr.lba = msf2hsg( &( q_info.abs ) );
850 subchnl.cdsc_reladdr.lba = msf2hsg( &( q_info.rel ) );
851 break;
852 case CDROM_MSF:
853 subchnl.cdsc_absaddr.msf.minute = bcd2bin( q_info.abs.min );
854 subchnl.cdsc_absaddr.msf.second = bcd2bin( q_info.abs.sec );
855 subchnl.cdsc_absaddr.msf.frame = bcd2bin( q_info.abs.frame );
856 subchnl.cdsc_reladdr.msf.minute = bcd2bin( q_info.rel.min );
857 subchnl.cdsc_reladdr.msf.second = bcd2bin( q_info.rel.sec );
858 subchnl.cdsc_reladdr.msf.frame = bcd2bin( q_info.rel.frame );
859 break;
860 default: return( -EINVAL );
862 copy_to_user( (void *)arg, &subchnl, sizeof( subchnl ) );
864 return( s );
867 case CDROMVOLCTRL:{
868 struct cdrom_volctrl vol_ctrl; int s;
869 #if defined( SJCD_TRACE )
870 printk( "SJCD: ioctl: volctrl\n" );
871 #endif
872 if( ( s = verify_area( VERIFY_READ, (void *)arg, sizeof( vol_ctrl ) ) ) == 0 ){
873 unsigned char dummy[ 4 ];
875 copy_from_user( &vol_ctrl, (void *)arg, sizeof( vol_ctrl ) );
876 sjcd_send_4_cmd( SCMD_SET_VOLUME, vol_ctrl.channel0, 0xFF,
877 vol_ctrl.channel1, 0xFF );
878 if( sjcd_receive_status() < 0 ) return( -EIO );
879 ( void )sjcd_load_response( dummy, 4 );
881 return( s );
884 case CDROMEJECT:{
885 #if defined( SJCD_TRACE )
886 printk( "SJCD: ioctl: eject\n" );
887 #endif
888 if( !sjcd_command_is_in_progress ){
889 sjcd_tray_unlock();
890 sjcd_send_cmd( SCMD_EJECT_TRAY );
891 ( void )sjcd_receive_status();
893 return( 0 );
896 #if defined( SJCD_GATHER_STAT )
897 case 0xABCD:{
898 int s;
899 #if defined( SJCD_TRACE )
900 printk( "SJCD: ioctl: statistic\n" );
901 #endif
902 if( ( s = verify_area( VERIFY_WRITE, (void *)arg, sizeof( statistic ) ) ) == 0 )
903 copy_to_user( (void *)arg, &statistic, sizeof( statistic ) );
904 return( s );
906 #endif
908 default:
909 return( -EINVAL );
914 * Invalidate internal buffers of the driver.
916 static void sjcd_invalidate_buffers( void ){
917 int i;
918 for( i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[ i++ ] = -1 );
919 sjcd_buf_out = -1;
923 * Take care of the different block sizes between cdrom and Linux.
924 * When Linux gets variable block sizes this will probably go away.
927 #define CURRENT_IS_VALID \
928 ( CURRENT != NULL && MAJOR( CURRENT->rq_dev ) == MAJOR_NR && \
929 CURRENT->cmd == READ && CURRENT->sector != -1 )
931 static void sjcd_transfer( void ){
932 #if defined( SJCD_TRACE )
933 printk( "SJCD: transfer:\n" );
934 #endif
935 if( CURRENT_IS_VALID ){
936 while( CURRENT->nr_sectors ){
937 int i, bn = CURRENT->sector / 4;
938 for( i = 0; i < SJCD_BUF_SIZ && sjcd_buf_bn[ i ] != bn; i++ );
939 if( i < SJCD_BUF_SIZ ){
940 int offs = ( i * 4 + ( CURRENT->sector & 3 ) ) * 512;
941 int nr_sectors = 4 - ( CURRENT->sector & 3 );
942 if( sjcd_buf_out != i ){
943 sjcd_buf_out = i;
944 if( sjcd_buf_bn[ i ] != bn ){
945 sjcd_buf_out = -1;
946 continue;
949 if( nr_sectors > CURRENT->nr_sectors )
950 nr_sectors = CURRENT->nr_sectors;
951 #if defined( SJCD_TRACE )
952 printk( "SJCD: copy out\n" );
953 #endif
954 memcpy( CURRENT->buffer, sjcd_buf + offs, nr_sectors * 512 );
955 CURRENT->nr_sectors -= nr_sectors;
956 CURRENT->sector += nr_sectors;
957 CURRENT->buffer += nr_sectors * 512;
958 } else {
959 sjcd_buf_out = -1;
960 break;
964 #if defined( SJCD_TRACE )
965 printk( "SJCD: transfer: done\n" );
966 #endif
969 static void sjcd_poll( void ){
970 #if defined( SJCD_GATHER_STAT )
972 * Update total number of ticks.
974 statistic.ticks++;
975 statistic.tticks[ sjcd_transfer_state ]++;
976 #endif
978 ReSwitch: switch( sjcd_transfer_state ){
980 case SJCD_S_IDLE:{
981 #if defined( SJCD_GATHER_STAT )
982 statistic.idle_ticks++;
983 #endif
984 #if defined( SJCD_TRACE )
985 printk( "SJCD_S_IDLE\n" );
986 #endif
987 return;
990 case SJCD_S_START:{
991 #if defined( SJCD_GATHER_STAT )
992 statistic.start_ticks++;
993 #endif
994 sjcd_send_cmd( SCMD_GET_STATUS );
995 sjcd_transfer_state =
996 sjcd_mode == SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
997 sjcd_transfer_timeout = 500;
998 #if defined( SJCD_TRACE )
999 printk( "SJCD_S_START: goto SJCD_S_%s mode\n",
1000 sjcd_transfer_state == SJCD_S_READ ? "READ" : "MODE" );
1001 #endif
1002 break;
1005 case SJCD_S_MODE:{
1006 if( sjcd_check_status() ){
1008 * Previous command is completed.
1010 if( !sjcd_status_valid || sjcd_command_failed ){
1011 #if defined( SJCD_TRACE )
1012 printk( "SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1013 #endif
1014 sjcd_transfer_state = SJCD_S_STOP;
1015 goto ReSwitch;
1018 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1019 sjcd_send_1_cmd( SCMD_SET_MODE, SCMD_MODE_COOKED );
1020 sjcd_transfer_state = SJCD_S_READ; sjcd_transfer_timeout = 1000;
1021 #if defined( SJCD_TRACE )
1022 printk( "SJCD_S_MODE: goto SJCD_S_READ mode\n" );
1023 #endif
1025 #if defined( SJCD_GATHER_STAT )
1026 else statistic.mode_ticks++;
1027 #endif
1028 break;
1031 case SJCD_S_READ:{
1032 if( sjcd_status_valid ? 1 : sjcd_check_status() ){
1034 * Previous command is completed.
1036 if( !sjcd_status_valid || sjcd_command_failed ){
1037 #if defined( SJCD_TRACE )
1038 printk( "SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n" );
1039 #endif
1040 sjcd_transfer_state = SJCD_S_STOP;
1041 goto ReSwitch;
1043 if( !sjcd_media_is_available ){
1044 #if defined( SJCD_TRACE )
1045 printk( "SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n" );
1046 #endif
1047 sjcd_transfer_state = SJCD_S_STOP;
1048 goto ReSwitch;
1050 if( sjcd_mode != SCMD_MODE_COOKED ){
1052 * We seem to come from set mode. So discard one byte of result.
1054 if( sjcd_load_response( &sjcd_mode, 1 ) != 0 ){
1055 #if defined( SJCD_TRACE )
1056 printk( "SJCD_S_READ: load failed: 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 ){
1062 #if defined( SJCD_TRACE )
1063 printk( "SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n" );
1064 #endif
1065 sjcd_transfer_state = SJCD_S_STOP;
1066 goto ReSwitch;
1070 if( CURRENT_IS_VALID ){
1071 struct sjcd_play_msf msf;
1073 sjcd_next_bn = CURRENT->sector / 4;
1074 hsg2msf( sjcd_next_bn, &msf.start );
1075 msf.end.min = 0; msf.end.sec = 0;
1076 msf.end.frame = sjcd_read_count = SJCD_BUF_SIZ;
1077 #if defined( SJCD_TRACE )
1078 printk( "SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1079 msf.start.min, msf.start.sec, msf.start.frame,
1080 msf.end.min, msf.end.sec, msf.end.frame );
1081 printk( "sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n", \
1082 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1083 sjcd_buf_bn[ sjcd_buf_in ] );
1084 #endif
1085 sjcd_send_6_cmd( SCMD_DATA_READ, &msf );
1086 sjcd_transfer_state = SJCD_S_DATA;
1087 sjcd_transfer_timeout = 500;
1088 #if defined( SJCD_TRACE )
1089 printk( "SJCD_S_READ: go to SJCD_S_DATA mode\n" );
1090 #endif
1091 } else {
1092 #if defined( SJCD_TRACE )
1093 printk( "SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n" );
1094 #endif
1095 sjcd_transfer_state = SJCD_S_STOP;
1096 goto ReSwitch;
1099 #if defined( SJCD_GATHER_STAT )
1100 else statistic.read_ticks++;
1101 #endif
1102 break;
1105 case SJCD_S_DATA:{
1106 unsigned char stat;
1108 sjcd_s_data: stat = inb( SJCDPORT( 1 ) );
1109 #if defined( SJCD_TRACE )
1110 printk( "SJCD_S_DATA: status = 0x%02x\n", stat );
1111 #endif
1112 if( SJCD_STATUS_AVAILABLE( stat ) ){
1114 * No data is waiting for us in the drive buffer. Status of operation
1115 * completion is available. Read and parse it.
1117 sjcd_load_status();
1119 if( !sjcd_status_valid || sjcd_command_failed ){
1120 #if defined( SJCD_TRACE )
1121 printk( "SJCD: read block %d failed, maybe audio disk? Giving up\n",
1122 sjcd_next_bn );
1123 #endif
1124 if( CURRENT_IS_VALID ) end_request( 0 );
1125 #if defined( SJCD_TRACE )
1126 printk( "SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n" );
1127 #endif
1128 sjcd_transfer_state = SJCD_S_STOP;
1129 goto ReSwitch;
1132 if( !sjcd_media_is_available ){
1133 printk( "SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n" );
1134 sjcd_transfer_state = SJCD_S_STOP;
1135 goto ReSwitch;
1138 sjcd_transfer_state = SJCD_S_READ;
1139 goto ReSwitch;
1140 } else if( SJCD_DATA_AVAILABLE( stat ) ){
1142 * One frame is read into device buffer. We must copy it to our memory.
1143 * Otherwise cdrom hangs up. Check to see if we have something to copy
1144 * to.
1146 if( !CURRENT_IS_VALID && sjcd_buf_in == sjcd_buf_out ){
1147 #if defined( SJCD_TRACE )
1148 printk( "SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n" );
1149 printk( " ... all the date would be discarded\n" );
1150 #endif
1151 sjcd_transfer_state = SJCD_S_STOP;
1152 goto ReSwitch;
1156 * Everything seems to be OK. Just read the frame and recalculate
1157 * indices.
1159 sjcd_buf_bn[ sjcd_buf_in ] = -1; /* ??? */
1160 insb( SJCDPORT( 2 ), sjcd_buf + 2048 * sjcd_buf_in, 2048 );
1161 #if defined( SJCD_TRACE )
1162 printk( "SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1163 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1164 sjcd_buf_bn[ sjcd_buf_in ] );
1165 #endif
1166 sjcd_buf_bn[ sjcd_buf_in ] = sjcd_next_bn++;
1167 if( sjcd_buf_out == -1 ) sjcd_buf_out = sjcd_buf_in;
1168 if( ++sjcd_buf_in == SJCD_BUF_SIZ ) sjcd_buf_in = 0;
1171 * Only one frame is ready at time. So we should turn over to wait for
1172 * another frame. If we need that, of course.
1174 if( --sjcd_read_count == 0 ){
1176 * OK, request seems to be precessed. Continue transferring...
1178 if( !sjcd_transfer_is_active ){
1179 while( CURRENT_IS_VALID ){
1181 * Continue transferring.
1183 sjcd_transfer();
1184 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1185 else break;
1188 if( CURRENT_IS_VALID &&
1189 ( CURRENT->sector / 4 < sjcd_next_bn ||
1190 CURRENT->sector / 4 > sjcd_next_bn + SJCD_BUF_SIZ ) ){
1191 #if defined( SJCD_TRACE )
1192 printk( "SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n" );
1193 #endif
1194 sjcd_transfer_state = SJCD_S_STOP;
1195 goto ReSwitch;
1199 * Now we should turn around rather than wait for while.
1201 goto sjcd_s_data;
1203 #if defined( SJCD_GATHER_STAT )
1204 else statistic.data_ticks++;
1205 #endif
1206 break;
1209 case SJCD_S_STOP:{
1210 sjcd_read_count = 0;
1211 sjcd_send_cmd( SCMD_STOP );
1212 sjcd_transfer_state = SJCD_S_STOPPING;
1213 sjcd_transfer_timeout = 500;
1214 #if defined( SJCD_GATHER_STAT )
1215 statistic.stop_ticks++;
1216 #endif
1217 break;
1220 case SJCD_S_STOPPING:{
1221 unsigned char stat;
1223 stat = inb( SJCDPORT( 1 ) );
1224 #if defined( SJCD_TRACE )
1225 printk( "SJCD_S_STOP: status = 0x%02x\n", stat );
1226 #endif
1227 if( SJCD_DATA_AVAILABLE( stat ) ){
1228 int i;
1229 #if defined( SJCD_TRACE )
1230 printk( "SJCD_S_STOP: discard data\n" );
1231 #endif
1233 * Discard all the data from the pipe. Foolish method.
1235 for( i = 2048; i--; ( void )inb( SJCDPORT( 2 ) ) );
1236 sjcd_transfer_timeout = 500;
1237 } else if( SJCD_STATUS_AVAILABLE( stat ) ){
1238 sjcd_load_status();
1239 if( sjcd_status_valid && sjcd_media_is_changed ) {
1240 sjcd_toc_uptodate = 0;
1241 sjcd_invalidate_buffers();
1243 if( CURRENT_IS_VALID ){
1244 if( sjcd_status_valid ) sjcd_transfer_state = SJCD_S_READ;
1245 else sjcd_transfer_state = SJCD_S_START;
1246 } else sjcd_transfer_state = SJCD_S_IDLE;
1247 goto ReSwitch;
1249 #if defined( SJCD_GATHER_STAT )
1250 else statistic.stopping_ticks++;
1251 #endif
1252 break;
1255 default:
1256 printk( "SJCD: poll: invalid state %d\n", sjcd_transfer_state );
1257 return;
1260 if( --sjcd_transfer_timeout == 0 ){
1261 printk( "SJCD: timeout in state %d\n", sjcd_transfer_state );
1262 while( CURRENT_IS_VALID ) end_request( 0 );
1263 sjcd_send_cmd( SCMD_STOP );
1264 sjcd_transfer_state = SJCD_S_IDLE;
1265 goto ReSwitch;
1269 * Get back in some time. 1 should be replaced with count variable to
1270 * avoid unnecessary testings.
1272 SJCD_SET_TIMER( sjcd_poll, 1 );
1275 static void do_sjcd_request( void ){
1276 #if defined( SJCD_TRACE )
1277 printk( "SJCD: do_sjcd_request(%ld+%ld)\n",
1278 CURRENT->sector, CURRENT->nr_sectors );
1279 #endif
1280 sjcd_transfer_is_active = 1;
1281 while( CURRENT_IS_VALID ){
1283 * Who of us are paranoiac?
1285 if( CURRENT->bh && !buffer_locked(CURRENT->bh) )
1286 panic( DEVICE_NAME ": block not locked" );
1288 sjcd_transfer();
1289 if( CURRENT->nr_sectors == 0 ) end_request( 1 );
1290 else {
1291 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1292 if( sjcd_transfer_state == SJCD_S_IDLE ){
1293 if( !sjcd_toc_uptodate ){
1294 if( sjcd_update_toc() < 0 ){
1295 printk( "SJCD: transfer: discard\n" );
1296 while( CURRENT_IS_VALID ) end_request( 0 );
1297 break;
1300 sjcd_transfer_state = SJCD_S_START;
1301 SJCD_SET_TIMER( sjcd_poll, HZ/100 );
1303 break;
1306 sjcd_transfer_is_active = 0;
1307 #if defined( SJCD_TRACE )
1308 printk( "sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1309 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out, sjcd_buf_bn[ sjcd_buf_in ] );
1310 printk( "do_sjcd_request ends\n" );
1311 #endif
1315 * Open the device special file. Check disk is in.
1317 int sjcd_open( struct inode *ip, struct file *fp ){
1319 * Check the presence of device.
1321 if( !sjcd_present ) return( -ENXIO );
1324 * Only read operations are allowed. Really? (:-)
1326 if( fp->f_mode & 2 ) return( -EROFS );
1328 if( sjcd_open_count == 0 ){
1329 int s, sjcd_open_tries;
1330 /* We don't know that, do we? */
1332 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1334 sjcd_mode = 0;
1335 sjcd_door_was_open = 0;
1336 sjcd_transfer_state = SJCD_S_IDLE;
1337 sjcd_invalidate_buffers();
1338 sjcd_status_valid = 0;
1341 * Strict status checking.
1343 for( sjcd_open_tries = 4; --sjcd_open_tries; ){
1344 if( !sjcd_status_valid ) sjcd_get_status();
1345 if( !sjcd_status_valid ){
1346 #if defined( SJCD_DIAGNOSTIC )
1347 printk( "SJCD: open: timed out when check status.\n" );
1348 #endif
1349 return( -EIO );
1350 } else if( !sjcd_media_is_available ){
1351 #if defined( SJCD_DIAGNOSTIC )
1352 printk("SJCD: open: no disk in drive\n");
1353 #endif
1354 if( !sjcd_door_closed ){
1355 sjcd_door_was_open = 1;
1356 #if defined( SJCD_TRACE )
1357 printk("SJCD: open: close the tray\n");
1358 #endif
1359 s = sjcd_tray_close();
1360 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1361 #if defined( SJCD_DIAGNOSTIC )
1362 printk("SJCD: open: tray close attempt failed\n");
1363 #endif
1364 return( -EIO );
1366 continue;
1367 } else return( -EIO );
1369 break;
1371 s = sjcd_tray_lock();
1372 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1373 #if defined( SJCD_DIAGNOSTIC )
1374 printk("SJCD: open: tray lock attempt failed\n");
1375 #endif
1376 return( -EIO );
1378 #if defined( SJCD_TRACE )
1379 printk( "SJCD: open: done\n" );
1380 #endif
1382 #ifdef MODULE
1383 MOD_INC_USE_COUNT;
1384 #endif
1385 ++sjcd_open_count;
1386 return( 0 );
1390 * On close, we flush all sjcd blocks from the buffer cache.
1392 static int sjcd_release( struct inode *inode, struct file *file ){
1393 int s;
1395 #if defined( SJCD_TRACE )
1396 printk( "SJCD: release\n" );
1397 #endif
1398 #ifdef MODULE
1399 MOD_DEC_USE_COUNT;
1400 #endif
1401 if( --sjcd_open_count == 0 ){
1402 sjcd_invalidate_buffers();
1403 sync_dev( inode->i_rdev );
1404 invalidate_buffers( inode->i_rdev );
1405 s = sjcd_tray_unlock();
1406 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1407 #if defined( SJCD_DIAGNOSTIC )
1408 printk("SJCD: release: tray unlock attempt failed.\n");
1409 #endif
1411 if( sjcd_door_was_open ){
1412 s = sjcd_tray_open();
1413 if( s < 0 || !sjcd_status_valid || sjcd_command_failed ){
1414 #if defined( SJCD_DIAGNOSTIC )
1415 printk("SJCD: release: tray unload attempt failed.\n");
1416 #endif
1420 return 0;
1424 * A list of file operations allowed for this cdrom.
1426 static struct file_operations sjcd_fops = {
1427 NULL, /* lseek - default */
1428 block_read, /* read - general block-dev read */
1429 block_write, /* write - general block-dev write */
1430 NULL, /* readdir - bad */
1431 NULL, /* poll */
1432 sjcd_ioctl, /* ioctl */
1433 NULL, /* mmap */
1434 sjcd_open, /* open */
1435 NULL, /* flush */
1436 sjcd_release, /* release */
1437 NULL, /* fsync */
1438 NULL, /* fasync */
1439 sjcd_disk_change, /* media change */
1440 NULL /* revalidate */
1443 static int blksize = 2048;
1444 static int secsize = 2048;
1447 * Following stuff is intended for initialization of the cdrom. It
1448 * first looks for presence of device. If the device is present, it
1449 * will be reset. Then read the version of the drive and load status.
1450 * The version is two BCD-coded bytes.
1452 static struct {
1453 unsigned char major, minor;
1454 } sjcd_version;
1457 * Test for presence of drive and initialize it. Called at boot time.
1458 * Probe cdrom, find out version and status.
1460 int __init sjcd_init( void ){
1461 int i;
1463 printk(KERN_INFO "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n", SJCD_VERSION_MAJOR,
1464 SJCD_VERSION_MINOR);
1466 #if defined( SJCD_TRACE )
1467 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1468 #endif
1470 hardsect_size[MAJOR_NR] = &secsize;
1471 blksize_size[MAJOR_NR] = &blksize;
1473 if( register_blkdev( MAJOR_NR, "sjcd", &sjcd_fops ) != 0 ){
1474 printk( "SJCD: Unable to get major %d for Sanyo CD-ROM\n", MAJOR_NR );
1475 return( -EIO );
1478 blk_dev[ MAJOR_NR ].request_fn = DEVICE_REQUEST;
1479 read_ahead[ MAJOR_NR ] = 4;
1481 if( check_region( sjcd_base, 4 ) ){
1482 printk( "SJCD: Init failed, I/O port (%X) is already in use\n",
1483 sjcd_base );
1484 sjcd_cleanup();
1485 return( -EIO );
1489 * Check for card. Since we are booting now, we can't use standard
1490 * wait algorithm.
1492 printk(KERN_INFO "SJCD: Resetting: " );
1493 sjcd_send_cmd( SCMD_RESET );
1494 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1495 unsigned long timer;
1498 * Wait 10ms approx.
1500 for( timer = jiffies; time_before_eq(jiffies, timer); );
1501 if ( (i % 100) == 0 ) printk( "." );
1502 ( void )sjcd_check_status();
1504 if( i == 0 || sjcd_command_failed ){
1505 printk( " reset failed, no drive found.\n" );
1506 sjcd_cleanup();
1507 return( -EIO );
1508 } else printk( "\n" );
1511 * Get and print out cdrom version.
1513 printk(KERN_INFO "SJCD: Getting version: " );
1514 sjcd_send_cmd( SCMD_GET_VERSION );
1515 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1516 unsigned long timer;
1519 * Wait 10ms approx.
1521 for( timer = jiffies; time_before_eq(jiffies, timer); );
1522 if ( (i % 100) == 0 ) printk( "." );
1523 ( void )sjcd_check_status();
1525 if( i == 0 || sjcd_command_failed ){
1526 printk( " get version failed, no drive found.\n" );
1527 sjcd_cleanup();
1528 return( -EIO );
1531 if( sjcd_load_response( &sjcd_version, sizeof( sjcd_version ) ) == 0 ){
1532 printk( " %1x.%02x\n", ( int )sjcd_version.major,
1533 ( int )sjcd_version.minor );
1534 } else {
1535 printk( " read version failed, no drive found.\n" );
1536 sjcd_cleanup();
1537 return( -EIO );
1541 * Check and print out the tray state. (if it is needed?).
1543 if( !sjcd_status_valid ){
1544 printk(KERN_INFO "SJCD: Getting status: " );
1545 sjcd_send_cmd( SCMD_GET_STATUS );
1546 for( i = 1000; i > 0 && !sjcd_status_valid; --i ){
1547 unsigned long timer;
1550 * Wait 10ms approx.
1552 for( timer = jiffies; time_before_eq(jiffies, timer); );
1553 if ( (i % 100) == 0 ) printk( "." );
1554 ( void )sjcd_check_status();
1556 if( i == 0 || sjcd_command_failed ){
1557 printk( " get status failed, no drive found.\n" );
1558 sjcd_cleanup();
1559 return( -EIO );
1560 } else printk( "\n" );
1563 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1565 sjcd_present++;
1566 return( 0 );
1569 static int
1570 sjcd_cleanup(void)
1572 if( (unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL) )
1573 printk( "SJCD: cannot unregister device.\n" );
1574 else
1575 release_region( sjcd_base, 4 );
1577 return(0);
1580 #ifdef MODULE
1582 int init_module(void)
1584 return sjcd_init();
1587 void cleanup_module(void)
1589 if ( sjcd_cleanup() )
1590 printk( "SJCD: module: cannot be removed.\n" );
1591 else
1592 printk(KERN_INFO "SJCD: module: removed.\n");
1594 #endif