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
61 #include <linux/module.h>
62 #include <linux/errno.h>
63 #include <linux/sched.h>
65 #include <linux/timer.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>
77 #include <asm/uaccess.h>
79 #define MAJOR_NR SANYO_CDROM_MAJOR
80 #include <linux/blk.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;
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 MODULE_PARM(sjcd_base, "i");
118 static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
123 static volatile unsigned short sjcd_transfer_is_active = 0;
125 enum sjcd_transfer_state {
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 )
145 static struct sjcd_stat statistic;
151 static struct timer_list sjcd_delay_timer;
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
167 static int __init sjcd_setup(char *str)
170 (void) get_options(str, ARRAY_SIZE(ints), ints);
177 __setup("sjcd=", sjcd_setup);
182 * Special converters.
184 static unsigned char bin2bcd(int bin)
190 return (u | (v << 4));
193 static int bcd2bin(unsigned char bcd)
195 return ((bcd >> 4) * 10 + (bcd & 0x0F));
198 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)
207 msf->min = hsg / 4500;
210 msf->frame = hsg % 75;
211 msf->min = bin2bcd(msf->min); /* convert to BCD */
212 msf->sec = bin2bcd(msf->sec);
213 msf->frame = bin2bcd(msf->frame);
217 * Send a command to cdrom. Invalidate status.
219 static void sjcd_send_cmd(unsigned char cmd)
221 #if defined( SJCD_TRACE )
222 printk("SJCD: send_cmd( 0x%x )\n", cmd);
224 outb(cmd, SJCDPORT(0));
225 sjcd_command_is_in_progress = 1;
226 sjcd_status_valid = 0;
227 sjcd_command_failed = 0;
231 * Send a command with one arg to cdrom. Invalidate status.
233 static void sjcd_send_1_cmd(unsigned char cmd, unsigned char a)
235 #if defined( SJCD_TRACE )
236 printk("SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a);
238 outb(cmd, SJCDPORT(0));
239 outb(a, SJCDPORT(0));
240 sjcd_command_is_in_progress = 1;
241 sjcd_status_valid = 0;
242 sjcd_command_failed = 0;
246 * Send a command with four args to cdrom. Invalidate status.
248 static void sjcd_send_4_cmd(unsigned char cmd, unsigned char a,
249 unsigned char b, unsigned char c,
252 #if defined( SJCD_TRACE )
253 printk("SJCD: send_4_cmd( 0x%x )\n", cmd);
255 outb(cmd, SJCDPORT(0));
256 outb(a, SJCDPORT(0));
257 outb(b, SJCDPORT(0));
258 outb(c, SJCDPORT(0));
259 outb(d, SJCDPORT(0));
260 sjcd_command_is_in_progress = 1;
261 sjcd_status_valid = 0;
262 sjcd_command_failed = 0;
266 * Send a play or read command to cdrom. Invalidate Status.
268 static void sjcd_send_6_cmd(unsigned char cmd, struct sjcd_play_msf *pms)
270 #if defined( SJCD_TRACE )
271 printk("SJCD: send_long_cmd( 0x%x )\n", cmd);
273 outb(cmd, SJCDPORT(0));
274 outb(pms->start.min, SJCDPORT(0));
275 outb(pms->start.sec, SJCDPORT(0));
276 outb(pms->start.frame, SJCDPORT(0));
277 outb(pms->end.min, SJCDPORT(0));
278 outb(pms->end.sec, SJCDPORT(0));
279 outb(pms->end.frame, SJCDPORT(0));
280 sjcd_command_is_in_progress = 1;
281 sjcd_status_valid = 0;
282 sjcd_command_failed = 0;
286 * Get a value from the data port. Should not block, so we use a little
287 * wait for a while. Returns 0 if OK.
289 static int sjcd_load_response(void *buf, int len)
291 unsigned char *resp = (unsigned char *) buf;
296 i-- && !SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))););
298 *resp++ = (unsigned char) inb(SJCDPORT(0));
306 * Load and parse command completion status (drive info byte and maybe error).
307 * Sorry, no error classification yet.
309 static void sjcd_load_status(void)
311 sjcd_media_is_changed = 0;
312 sjcd_completion_error = 0;
313 sjcd_completion_status = inb(SJCDPORT(0));
314 if (sjcd_completion_status & SST_DOOR_OPENED) {
315 sjcd_door_closed = sjcd_media_is_available = 0;
317 sjcd_door_closed = 1;
318 if (sjcd_completion_status & SST_MEDIA_CHANGED)
319 sjcd_media_is_available = sjcd_media_is_changed =
321 else if (sjcd_completion_status & 0x0F) {
323 * OK, we seem to catch an error ...
325 while (!SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))));
326 sjcd_completion_error = inb(SJCDPORT(0));
327 if ((sjcd_completion_status & 0x08) &&
328 (sjcd_completion_error & 0x40))
329 sjcd_media_is_available = 0;
331 sjcd_command_failed = 1;
333 sjcd_media_is_available = 1;
336 * Ok, status loaded successfully.
338 sjcd_status_valid = 1, sjcd_error_reported = 0;
339 sjcd_command_is_in_progress = 0;
342 * If the disk is changed, the TOC is not valid.
344 if (sjcd_media_is_changed)
345 sjcd_toc_uptodate = 0;
346 #if defined( SJCD_TRACE )
347 printk("SJCD: status %02x.%02x loaded.\n",
348 (int) sjcd_completion_status, (int) sjcd_completion_error);
353 * Read status from cdrom. Check to see if the status is available.
355 static int sjcd_check_status(void)
358 * Try to load the response from cdrom into buffer.
360 if (SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1)))) {
365 * No status is available.
372 * This is just timeout counter, and nothing more. Surprised ? :-)
374 static volatile long sjcd_status_timeout;
377 * We need about 10 seconds to wait. The longest command takes about 5 seconds
378 * to probe the disk (usually after tray closed or drive reset). Other values
379 * should be thought of for other commands.
381 #define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
383 static void sjcd_status_timer(void)
385 if (sjcd_check_status()) {
387 * The command completed and status is loaded, stop waiting.
389 wake_up(&sjcd_waitq);
390 } else if (--sjcd_status_timeout <= 0) {
394 wake_up(&sjcd_waitq);
397 * We have still some time to wait. Try again.
399 SJCD_SET_TIMER(sjcd_status_timer, 1);
404 * Wait for status for 10 sec approx. Returns non-positive when timed out.
405 * Should not be used while reading data CDs.
407 static int sjcd_wait_for_status(void)
409 sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
410 SJCD_SET_TIMER(sjcd_status_timer, 1);
411 sleep_on(&sjcd_waitq);
412 #if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
413 if (sjcd_status_timeout <= 0)
414 printk("SJCD: Error Wait For Status.\n");
416 return (sjcd_status_timeout);
419 static int sjcd_receive_status(void)
422 #if defined( SJCD_TRACE )
423 printk("SJCD: receive_status\n");
426 * Wait a bit for status available.
428 for (i = 200; i-- && (sjcd_check_status() == 0););
430 #if defined( SJCD_TRACE )
431 printk("SJCD: long wait for status\n");
433 if (sjcd_wait_for_status() <= 0)
434 printk("SJCD: Timeout when read status.\n");
442 * Load the status. Issue get status command and wait for status available.
444 static void sjcd_get_status(void)
446 #if defined( SJCD_TRACE )
447 printk("SJCD: get_status\n");
449 sjcd_send_cmd(SCMD_GET_STATUS);
450 sjcd_receive_status();
454 * Check the drive if the disk is changed. Should be revised.
456 static int sjcd_disk_change(kdev_t full_dev)
459 printk("SJCD: sjcd_disk_change( 0x%x )\n", full_dev);
461 if (MINOR(full_dev) > 0) {
462 printk("SJCD: request error: invalid device minor.\n");
465 if (!sjcd_command_is_in_progress)
467 return (sjcd_status_valid ? sjcd_media_is_changed : 0);
471 * Read the table of contents (TOC) and TOC header if necessary.
472 * We assume that the drive contains no more than 99 toc entries.
474 static struct sjcd_hw_disk_info sjcd_table_of_contents[SJCD_MAX_TRACKS];
475 static unsigned char sjcd_first_track_no, sjcd_last_track_no;
476 #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
478 static int sjcd_update_toc(void)
480 struct sjcd_hw_disk_info info;
482 #if defined( SJCD_TRACE )
483 printk("SJCD: update toc:\n");
486 * check to see if we need to do anything
488 if (sjcd_toc_uptodate)
492 * Get the TOC start information.
494 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK);
495 sjcd_receive_status();
497 if (!sjcd_status_valid) {
498 printk("SJCD: cannot load status.\n");
502 if (!sjcd_media_is_available) {
503 printk("SJCD: no disk in drive\n");
507 if (!sjcd_command_failed) {
508 if (sjcd_load_response(&info, sizeof(info)) != 0) {
510 ("SJCD: cannot load response about TOC start.\n");
513 sjcd_first_track_no = bcd2bin(info.un.track_no);
515 printk("SJCD: get first failed\n");
518 #if defined( SJCD_TRACE )
519 printk("SJCD: TOC start 0x%02x ", sjcd_first_track_no);
522 * Get the TOC finish information.
524 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK);
525 sjcd_receive_status();
527 if (!sjcd_status_valid) {
528 printk("SJCD: cannot load status.\n");
532 if (!sjcd_media_is_available) {
533 printk("SJCD: no disk in drive\n");
537 if (!sjcd_command_failed) {
538 if (sjcd_load_response(&info, sizeof(info)) != 0) {
540 ("SJCD: cannot load response about TOC finish.\n");
543 sjcd_last_track_no = bcd2bin(info.un.track_no);
545 printk("SJCD: get last failed\n");
548 #if defined( SJCD_TRACE )
549 printk("SJCD: TOC finish 0x%02x ", sjcd_last_track_no);
551 for (i = sjcd_first_track_no; i <= sjcd_last_track_no; i++) {
553 * Get the first track information.
555 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, bin2bcd(i));
556 sjcd_receive_status();
558 if (!sjcd_status_valid) {
559 printk("SJCD: cannot load status.\n");
563 if (!sjcd_media_is_available) {
564 printk("SJCD: no disk in drive\n");
568 if (!sjcd_command_failed) {
569 if (sjcd_load_response(&sjcd_table_of_contents[i],
574 ("SJCD: cannot load info for %d track\n",
579 printk("SJCD: get info %d failed\n", i);
585 * Get the disk length info.
587 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE);
588 sjcd_receive_status();
590 if (!sjcd_status_valid) {
591 printk("SJCD: cannot load status.\n");
595 if (!sjcd_media_is_available) {
596 printk("SJCD: no disk in drive\n");
600 if (!sjcd_command_failed) {
601 if (sjcd_load_response(&info, sizeof(info)) != 0) {
603 ("SJCD: cannot load response about disk size.\n");
606 sjcd_disk_length.min = info.un.track_msf.min;
607 sjcd_disk_length.sec = info.un.track_msf.sec;
608 sjcd_disk_length.frame = info.un.track_msf.frame;
610 printk("SJCD: get size failed\n");
613 #if defined( SJCD_TRACE )
614 printk("SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
615 sjcd_disk_length.sec, sjcd_disk_length.frame);
621 * Load subchannel information.
623 static int sjcd_get_q_info(struct sjcd_hw_qinfo *qp)
626 #if defined( SJCD_TRACE )
627 printk("SJCD: load sub q\n");
629 sjcd_send_cmd(SCMD_GET_QINFO);
630 s = sjcd_receive_status();
631 if (s < 0 || sjcd_command_failed || !sjcd_status_valid) {
633 s = sjcd_receive_status();
634 if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
636 sjcd_send_cmd(SCMD_GET_QINFO);
637 s = sjcd_receive_status();
638 if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
641 if (sjcd_media_is_available)
642 if (sjcd_load_response(qp, sizeof(*qp)) == 0)
648 * Start playing from the specified position.
650 static int sjcd_play(struct sjcd_play_msf *mp)
652 struct sjcd_play_msf msf;
655 * Turn the device to play mode.
657 sjcd_send_1_cmd(SCMD_SET_MODE, SCMD_MODE_PLAY);
658 if (sjcd_receive_status() < 0)
662 * Seek to the starting point.
664 msf.start = mp->start;
665 msf.end.min = msf.end.sec = msf.end.frame = 0x00;
666 sjcd_send_6_cmd(SCMD_SEEK, &msf);
667 if (sjcd_receive_status() < 0)
673 sjcd_send_6_cmd(SCMD_PLAY, mp);
674 return (sjcd_receive_status());
678 * Tray control functions.
680 static int sjcd_tray_close(void)
682 #if defined( SJCD_TRACE )
683 printk("SJCD: tray_close\n");
685 sjcd_send_cmd(SCMD_CLOSE_TRAY);
686 return (sjcd_receive_status());
689 static int sjcd_tray_lock(void)
691 #if defined( SJCD_TRACE )
692 printk("SJCD: tray_lock\n");
694 sjcd_send_cmd(SCMD_LOCK_TRAY);
695 return (sjcd_receive_status());
698 static int sjcd_tray_unlock(void)
700 #if defined( SJCD_TRACE )
701 printk("SJCD: tray_unlock\n");
703 sjcd_send_cmd(SCMD_UNLOCK_TRAY);
704 return (sjcd_receive_status());
707 static int sjcd_tray_open(void)
709 #if defined( SJCD_TRACE )
710 printk("SJCD: tray_open\n");
712 sjcd_send_cmd(SCMD_EJECT_TRAY);
713 return (sjcd_receive_status());
717 * Do some user commands.
719 static int sjcd_ioctl(struct inode *ip, struct file *fp,
720 unsigned int cmd, unsigned long arg)
722 #if defined( SJCD_TRACE )
723 printk("SJCD:ioctl\n");
730 if (!sjcd_status_valid)
732 if (sjcd_update_toc() < 0)
737 #if defined( SJCD_TRACE )
738 printk("SJCD: ioctl: start\n");
744 #if defined( SJCD_TRACE )
745 printk("SJCD: ioctl: stop\n");
747 sjcd_send_cmd(SCMD_PAUSE);
748 (void) sjcd_receive_status();
749 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
754 struct sjcd_hw_qinfo q_info;
755 #if defined( SJCD_TRACE )
756 printk("SJCD: ioctl: pause\n");
758 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
759 sjcd_send_cmd(SCMD_PAUSE);
760 (void) sjcd_receive_status();
761 if (sjcd_get_q_info(&q_info) < 0) {
763 CDROM_AUDIO_NO_STATUS;
767 sjcd_playing.start = q_info.abs;
775 #if defined( SJCD_TRACE )
776 printk("SJCD: ioctl: resume\n");
778 if (sjcd_audio_status == CDROM_AUDIO_PAUSED) {
780 * continue play starting at saved location
782 if (sjcd_play(&sjcd_playing) < 0) {
795 case CDROMPLAYTRKIND:{
798 #if defined( SJCD_TRACE )
799 printk("SJCD: ioctl: playtrkind\n");
802 verify_area(VERIFY_READ, (void *) arg,
804 copy_from_user(&ti, (void *) arg,
807 if (ti.cdti_trk0 < sjcd_first_track_no)
809 if (ti.cdti_trk1 > sjcd_last_track_no)
810 ti.cdti_trk1 = sjcd_last_track_no;
811 if (ti.cdti_trk0 > ti.cdti_trk1)
815 sjcd_table_of_contents[ti.cdti_trk0].
819 sjcd_last_track_no) ?
820 sjcd_table_of_contents[ti.cdti_trk1 +
822 track_msf : sjcd_table_of_contents[0].
825 if (sjcd_play(&sjcd_playing) < 0) {
837 struct cdrom_msf sjcd_msf;
839 #if defined( SJCD_TRACE )
840 printk("SJCD: ioctl: playmsf\n");
843 verify_area(VERIFY_READ, (void *) arg,
844 sizeof(sjcd_msf))) == 0) {
845 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
846 sjcd_send_cmd(SCMD_PAUSE);
847 (void) sjcd_receive_status();
849 CDROM_AUDIO_NO_STATUS;
852 copy_from_user(&sjcd_msf, (void *) arg,
855 sjcd_playing.start.min =
856 bin2bcd(sjcd_msf.cdmsf_min0);
857 sjcd_playing.start.sec =
858 bin2bcd(sjcd_msf.cdmsf_sec0);
859 sjcd_playing.start.frame =
860 bin2bcd(sjcd_msf.cdmsf_frame0);
861 sjcd_playing.end.min =
862 bin2bcd(sjcd_msf.cdmsf_min1);
863 sjcd_playing.end.sec =
864 bin2bcd(sjcd_msf.cdmsf_sec1);
865 sjcd_playing.end.frame =
866 bin2bcd(sjcd_msf.cdmsf_frame1);
868 if (sjcd_play(&sjcd_playing) < 0) {
879 case CDROMREADTOCHDR:{
880 struct cdrom_tochdr toc_header;
882 #if defined (SJCD_TRACE )
883 printk("SJCD: ioctl: readtocheader\n");
886 verify_area(VERIFY_WRITE, (void *) arg,
887 sizeof(toc_header))) == 0) {
888 toc_header.cdth_trk0 = sjcd_first_track_no;
889 toc_header.cdth_trk1 = sjcd_last_track_no;
890 copy_to_user((void *) arg, &toc_header,
896 case CDROMREADTOCENTRY:{
897 struct cdrom_tocentry toc_entry;
899 #if defined( SJCD_TRACE )
900 printk("SJCD: ioctl: readtocentry\n");
903 verify_area(VERIFY_WRITE, (void *) arg,
904 sizeof(toc_entry))) == 0) {
905 struct sjcd_hw_disk_info *tp;
907 copy_from_user(&toc_entry, (void *) arg,
910 if (toc_entry.cdte_track == CDROM_LEADOUT)
911 tp = &sjcd_table_of_contents[0];
912 else if (toc_entry.cdte_track <
915 else if (toc_entry.cdte_track >
919 tp = &sjcd_table_of_contents
920 [toc_entry.cdte_track];
923 tp->track_control & 0x0F;
924 toc_entry.cdte_ctrl =
925 tp->track_control >> 4;
927 switch (toc_entry.cdte_format) {
929 toc_entry.cdte_addr.lba =
930 msf2hsg(&(tp->un.track_msf));
933 toc_entry.cdte_addr.msf.minute =
934 bcd2bin(tp->un.track_msf.min);
935 toc_entry.cdte_addr.msf.second =
936 bcd2bin(tp->un.track_msf.sec);
937 toc_entry.cdte_addr.msf.frame =
938 bcd2bin(tp->un.track_msf.
944 copy_to_user((void *) arg, &toc_entry,
951 struct cdrom_subchnl subchnl;
953 #if defined( SJCD_TRACE )
954 printk("SJCD: ioctl: subchnl\n");
957 verify_area(VERIFY_WRITE, (void *) arg,
958 sizeof(subchnl))) == 0) {
959 struct sjcd_hw_qinfo q_info;
961 copy_from_user(&subchnl, (void *) arg,
963 if (sjcd_get_q_info(&q_info) < 0)
966 subchnl.cdsc_audiostatus =
969 q_info.track_control & 0x0F;
971 q_info.track_control >> 4;
973 bcd2bin(q_info.track_no);
974 subchnl.cdsc_ind = bcd2bin(q_info.x);
976 switch (subchnl.cdsc_format) {
978 subchnl.cdsc_absaddr.lba =
979 msf2hsg(&(q_info.abs));
980 subchnl.cdsc_reladdr.lba =
981 msf2hsg(&(q_info.rel));
984 subchnl.cdsc_absaddr.msf.minute =
985 bcd2bin(q_info.abs.min);
986 subchnl.cdsc_absaddr.msf.second =
987 bcd2bin(q_info.abs.sec);
988 subchnl.cdsc_absaddr.msf.frame =
989 bcd2bin(q_info.abs.frame);
990 subchnl.cdsc_reladdr.msf.minute =
991 bcd2bin(q_info.rel.min);
992 subchnl.cdsc_reladdr.msf.second =
993 bcd2bin(q_info.rel.sec);
994 subchnl.cdsc_reladdr.msf.frame =
995 bcd2bin(q_info.rel.frame);
1000 copy_to_user((void *) arg, &subchnl,
1007 struct cdrom_volctrl vol_ctrl;
1009 #if defined( SJCD_TRACE )
1010 printk("SJCD: ioctl: volctrl\n");
1013 verify_area(VERIFY_READ, (void *) arg,
1014 sizeof(vol_ctrl))) == 0) {
1015 unsigned char dummy[4];
1017 copy_from_user(&vol_ctrl, (void *) arg,
1019 sjcd_send_4_cmd(SCMD_SET_VOLUME,
1020 vol_ctrl.channel0, 0xFF,
1021 vol_ctrl.channel1, 0xFF);
1022 if (sjcd_receive_status() < 0)
1024 (void) sjcd_load_response(dummy, 4);
1030 #if defined( SJCD_TRACE )
1031 printk("SJCD: ioctl: eject\n");
1033 if (!sjcd_command_is_in_progress) {
1035 sjcd_send_cmd(SCMD_EJECT_TRAY);
1036 (void) sjcd_receive_status();
1041 #if defined( SJCD_GATHER_STAT )
1044 #if defined( SJCD_TRACE )
1045 printk("SJCD: ioctl: statistic\n");
1048 verify_area(VERIFY_WRITE, (void *) arg,
1049 sizeof(statistic))) == 0)
1050 copy_to_user((void *) arg, &statistic,
1062 * Invalidate internal buffers of the driver.
1064 static void sjcd_invalidate_buffers(void)
1067 for (i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[i++] = -1);
1072 * Take care of the different block sizes between cdrom and Linux.
1073 * When Linux gets variable block sizes this will probably go away.
1076 #define CURRENT_IS_VALID \
1077 ( !QUEUE_EMPTY && MAJOR( CURRENT->rq_dev ) == MAJOR_NR && \
1078 CURRENT->cmd == READ && CURRENT->sector != -1 )
1080 static void sjcd_transfer(void)
1082 #if defined( SJCD_TRACE )
1083 printk("SJCD: transfer:\n");
1085 if (CURRENT_IS_VALID) {
1086 while (CURRENT->nr_sectors) {
1087 int i, bn = CURRENT->sector / 4;
1089 i < SJCD_BUF_SIZ && sjcd_buf_bn[i] != bn;
1091 if (i < SJCD_BUF_SIZ) {
1093 (i * 4 + (CURRENT->sector & 3)) * 512;
1094 int nr_sectors = 4 - (CURRENT->sector & 3);
1095 if (sjcd_buf_out != i) {
1097 if (sjcd_buf_bn[i] != bn) {
1102 if (nr_sectors > CURRENT->nr_sectors)
1103 nr_sectors = CURRENT->nr_sectors;
1104 #if defined( SJCD_TRACE )
1105 printk("SJCD: copy out\n");
1107 memcpy(CURRENT->buffer, sjcd_buf + offs,
1109 CURRENT->nr_sectors -= nr_sectors;
1110 CURRENT->sector += nr_sectors;
1111 CURRENT->buffer += nr_sectors * 512;
1118 #if defined( SJCD_TRACE )
1119 printk("SJCD: transfer: done\n");
1123 static void sjcd_poll(void)
1125 #if defined( SJCD_GATHER_STAT )
1127 * Update total number of ticks.
1130 statistic.tticks[sjcd_transfer_state]++;
1133 ReSwitch:switch (sjcd_transfer_state) {
1136 #if defined( SJCD_GATHER_STAT )
1137 statistic.idle_ticks++;
1139 #if defined( SJCD_TRACE )
1140 printk("SJCD_S_IDLE\n");
1146 #if defined( SJCD_GATHER_STAT )
1147 statistic.start_ticks++;
1149 sjcd_send_cmd(SCMD_GET_STATUS);
1150 sjcd_transfer_state =
1152 SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1153 sjcd_transfer_timeout = 500;
1154 #if defined( SJCD_TRACE )
1155 printk("SJCD_S_START: goto SJCD_S_%s mode\n",
1156 sjcd_transfer_state ==
1157 SJCD_S_READ ? "READ" : "MODE");
1163 if (sjcd_check_status()) {
1165 * Previous command is completed.
1167 if (!sjcd_status_valid
1168 || sjcd_command_failed) {
1169 #if defined( SJCD_TRACE )
1171 ("SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1173 sjcd_transfer_state = SJCD_S_STOP;
1177 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1178 sjcd_send_1_cmd(SCMD_SET_MODE,
1180 sjcd_transfer_state = SJCD_S_READ;
1181 sjcd_transfer_timeout = 1000;
1182 #if defined( SJCD_TRACE )
1184 ("SJCD_S_MODE: goto SJCD_S_READ mode\n");
1187 #if defined( SJCD_GATHER_STAT )
1189 statistic.mode_ticks++;
1195 if (sjcd_status_valid ? 1 : sjcd_check_status()) {
1197 * Previous command is completed.
1199 if (!sjcd_status_valid
1200 || sjcd_command_failed) {
1201 #if defined( SJCD_TRACE )
1203 ("SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1205 sjcd_transfer_state = SJCD_S_STOP;
1208 if (!sjcd_media_is_available) {
1209 #if defined( SJCD_TRACE )
1211 ("SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n");
1213 sjcd_transfer_state = SJCD_S_STOP;
1216 if (sjcd_mode != SCMD_MODE_COOKED) {
1218 * We seem to come from set mode. So discard one byte of result.
1220 if (sjcd_load_response
1221 (&sjcd_mode, 1) != 0) {
1222 #if defined( SJCD_TRACE )
1224 ("SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n");
1226 sjcd_transfer_state =
1230 if (sjcd_mode != SCMD_MODE_COOKED) {
1231 #if defined( SJCD_TRACE )
1233 ("SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n");
1235 sjcd_transfer_state =
1241 if (CURRENT_IS_VALID) {
1242 struct sjcd_play_msf msf;
1244 sjcd_next_bn = CURRENT->sector / 4;
1245 hsg2msf(sjcd_next_bn, &msf.start);
1248 msf.end.frame = sjcd_read_count =
1250 #if defined( SJCD_TRACE )
1252 ("SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1253 msf.start.min, msf.start.sec,
1254 msf.start.frame, msf.end.min,
1255 msf.end.sec, msf.end.frame);
1257 ("sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n",
1258 sjcd_next_bn, sjcd_buf_in,
1260 sjcd_buf_bn[sjcd_buf_in]);
1262 sjcd_send_6_cmd(SCMD_DATA_READ,
1264 sjcd_transfer_state = SJCD_S_DATA;
1265 sjcd_transfer_timeout = 500;
1266 #if defined( SJCD_TRACE )
1268 ("SJCD_S_READ: go to SJCD_S_DATA mode\n");
1271 #if defined( SJCD_TRACE )
1273 ("SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n");
1275 sjcd_transfer_state = SJCD_S_STOP;
1279 #if defined( SJCD_GATHER_STAT )
1281 statistic.read_ticks++;
1292 #if defined( SJCD_TRACE )
1293 printk("SJCD_S_DATA: status = 0x%02x\n", stat);
1295 if (SJCD_STATUS_AVAILABLE(stat)) {
1297 * No data is waiting for us in the drive buffer. Status of operation
1298 * completion is available. Read and parse it.
1302 if (!sjcd_status_valid
1303 || sjcd_command_failed) {
1304 #if defined( SJCD_TRACE )
1306 ("SJCD: read block %d failed, maybe audio disk? Giving up\n",
1309 if (CURRENT_IS_VALID)
1311 #if defined( SJCD_TRACE )
1313 ("SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n");
1315 sjcd_transfer_state = SJCD_S_STOP;
1319 if (!sjcd_media_is_available) {
1321 ("SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n");
1322 sjcd_transfer_state = SJCD_S_STOP;
1326 sjcd_transfer_state = SJCD_S_READ;
1328 } else if (SJCD_DATA_AVAILABLE(stat)) {
1330 * One frame is read into device buffer. We must copy it to our memory.
1331 * Otherwise cdrom hangs up. Check to see if we have something to copy
1334 if (!CURRENT_IS_VALID
1335 && sjcd_buf_in == sjcd_buf_out) {
1336 #if defined( SJCD_TRACE )
1338 ("SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n");
1340 (" ... all the date would be discarded\n");
1342 sjcd_transfer_state = SJCD_S_STOP;
1347 * Everything seems to be OK. Just read the frame and recalculate
1350 sjcd_buf_bn[sjcd_buf_in] = -1; /* ??? */
1352 sjcd_buf + 2048 * sjcd_buf_in, 2048);
1353 #if defined( SJCD_TRACE )
1355 ("SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1356 sjcd_next_bn, sjcd_buf_in,
1358 sjcd_buf_bn[sjcd_buf_in]);
1360 sjcd_buf_bn[sjcd_buf_in] = sjcd_next_bn++;
1361 if (sjcd_buf_out == -1)
1362 sjcd_buf_out = sjcd_buf_in;
1363 if (++sjcd_buf_in == SJCD_BUF_SIZ)
1367 * Only one frame is ready at time. So we should turn over to wait for
1368 * another frame. If we need that, of course.
1370 if (--sjcd_read_count == 0) {
1372 * OK, request seems to be precessed. Continue transferring...
1374 if (!sjcd_transfer_is_active) {
1375 while (CURRENT_IS_VALID) {
1377 * Continue transferring.
1389 if (CURRENT_IS_VALID &&
1390 (CURRENT->sector / 4 <
1392 || CURRENT->sector / 4 >
1395 #if defined( SJCD_TRACE )
1397 ("SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n");
1399 sjcd_transfer_state =
1405 * Now we should turn around rather than wait for while.
1409 #if defined( SJCD_GATHER_STAT )
1411 statistic.data_ticks++;
1417 sjcd_read_count = 0;
1418 sjcd_send_cmd(SCMD_STOP);
1419 sjcd_transfer_state = SJCD_S_STOPPING;
1420 sjcd_transfer_timeout = 500;
1421 #if defined( SJCD_GATHER_STAT )
1422 statistic.stop_ticks++;
1427 case SJCD_S_STOPPING:{
1430 stat = inb(SJCDPORT(1));
1431 #if defined( SJCD_TRACE )
1432 printk("SJCD_S_STOP: status = 0x%02x\n", stat);
1434 if (SJCD_DATA_AVAILABLE(stat)) {
1436 #if defined( SJCD_TRACE )
1437 printk("SJCD_S_STOP: discard data\n");
1440 * Discard all the data from the pipe. Foolish method.
1443 (void) inb(SJCDPORT(2)));
1444 sjcd_transfer_timeout = 500;
1445 } else if (SJCD_STATUS_AVAILABLE(stat)) {
1447 if (sjcd_status_valid
1448 && sjcd_media_is_changed) {
1449 sjcd_toc_uptodate = 0;
1450 sjcd_invalidate_buffers();
1452 if (CURRENT_IS_VALID) {
1453 if (sjcd_status_valid)
1454 sjcd_transfer_state =
1457 sjcd_transfer_state =
1460 sjcd_transfer_state = SJCD_S_IDLE;
1463 #if defined( SJCD_GATHER_STAT )
1465 statistic.stopping_ticks++;
1471 printk("SJCD: poll: invalid state %d\n",
1472 sjcd_transfer_state);
1476 if (--sjcd_transfer_timeout == 0) {
1477 printk("SJCD: timeout in state %d\n", sjcd_transfer_state);
1478 while (CURRENT_IS_VALID)
1480 sjcd_send_cmd(SCMD_STOP);
1481 sjcd_transfer_state = SJCD_S_IDLE;
1486 * Get back in some time. 1 should be replaced with count variable to
1487 * avoid unnecessary testings.
1489 SJCD_SET_TIMER(sjcd_poll, 1);
1492 static void do_sjcd_request(request_queue_t * q)
1494 #if defined( SJCD_TRACE )
1495 printk("SJCD: do_sjcd_request(%ld+%ld)\n",
1496 CURRENT->sector, CURRENT->nr_sectors);
1498 sjcd_transfer_is_active = 1;
1499 while (CURRENT_IS_VALID) {
1501 * Who of us are paranoiac?
1503 if (CURRENT->bh && !buffer_locked(CURRENT->bh))
1504 panic(DEVICE_NAME ": block not locked");
1507 if (CURRENT->nr_sectors == 0)
1510 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1511 if (sjcd_transfer_state == SJCD_S_IDLE) {
1512 if (!sjcd_toc_uptodate) {
1513 if (sjcd_update_toc() < 0) {
1515 ("SJCD: transfer: discard\n");
1516 while (CURRENT_IS_VALID)
1521 sjcd_transfer_state = SJCD_S_START;
1522 SJCD_SET_TIMER(sjcd_poll, HZ / 100);
1527 sjcd_transfer_is_active = 0;
1528 #if defined( SJCD_TRACE )
1530 ("sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1531 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1532 sjcd_buf_bn[sjcd_buf_in]);
1533 printk("do_sjcd_request ends\n");
1538 * Open the device special file. Check disk is in.
1540 int sjcd_open(struct inode *ip, struct file *fp)
1543 * Check the presence of device.
1549 * Only read operations are allowed. Really? (:-)
1554 if (sjcd_open_count == 0) {
1555 int s, sjcd_open_tries;
1556 /* We don't know that, do we? */
1558 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1561 sjcd_door_was_open = 0;
1562 sjcd_transfer_state = SJCD_S_IDLE;
1563 sjcd_invalidate_buffers();
1564 sjcd_status_valid = 0;
1567 * Strict status checking.
1569 for (sjcd_open_tries = 4; --sjcd_open_tries;) {
1570 if (!sjcd_status_valid)
1572 if (!sjcd_status_valid) {
1573 #if defined( SJCD_DIAGNOSTIC )
1575 ("SJCD: open: timed out when check status.\n");
1578 } else if (!sjcd_media_is_available) {
1579 #if defined( SJCD_DIAGNOSTIC )
1580 printk("SJCD: open: no disk in drive\n");
1582 if (!sjcd_door_closed) {
1583 sjcd_door_was_open = 1;
1584 #if defined( SJCD_TRACE )
1586 ("SJCD: open: close the tray\n");
1588 s = sjcd_tray_close();
1589 if (s < 0 || !sjcd_status_valid
1590 || sjcd_command_failed) {
1591 #if defined( SJCD_DIAGNOSTIC )
1593 ("SJCD: open: tray close attempt failed\n");
1603 s = sjcd_tray_lock();
1604 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1605 #if defined( SJCD_DIAGNOSTIC )
1606 printk("SJCD: open: tray lock attempt failed\n");
1610 #if defined( SJCD_TRACE )
1611 printk("SJCD: open: done\n");
1623 * On close, we flush all sjcd blocks from the buffer cache.
1625 static int sjcd_release(struct inode *inode, struct file *file)
1629 #if defined( SJCD_TRACE )
1630 printk("SJCD: release\n");
1632 if (--sjcd_open_count == 0) {
1633 sjcd_invalidate_buffers();
1634 s = sjcd_tray_unlock();
1635 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1636 #if defined( SJCD_DIAGNOSTIC )
1638 ("SJCD: release: tray unlock attempt failed.\n");
1641 if (sjcd_door_was_open) {
1642 s = sjcd_tray_open();
1643 if (s < 0 || !sjcd_status_valid
1644 || sjcd_command_failed) {
1645 #if defined( SJCD_DIAGNOSTIC )
1647 ("SJCD: release: tray unload attempt failed.\n");
1656 * A list of file operations allowed for this cdrom.
1658 static struct block_device_operations sjcd_fops = {
1661 release:sjcd_release,
1663 check_media_change:sjcd_disk_change,
1666 static int blksize = 2048;
1667 static int secsize = 2048;
1670 * Following stuff is intended for initialization of the cdrom. It
1671 * first looks for presence of device. If the device is present, it
1672 * will be reset. Then read the version of the drive and load status.
1673 * The version is two BCD-coded bytes.
1676 unsigned char major, minor;
1680 * Test for presence of drive and initialize it. Called at boot time.
1681 * Probe cdrom, find out version and status.
1683 int __init sjcd_init(void)
1688 "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n",
1689 SJCD_VERSION_MAJOR, SJCD_VERSION_MINOR);
1691 #if defined( SJCD_TRACE )
1692 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1695 hardsect_size[MAJOR_NR] = &secsize;
1696 blksize_size[MAJOR_NR] = &blksize;
1698 if (devfs_register_blkdev(MAJOR_NR, "sjcd", &sjcd_fops) != 0) {
1699 printk("SJCD: Unable to get major %d for Sanyo CD-ROM\n",
1704 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1705 read_ahead[MAJOR_NR] = 4;
1706 register_disk(NULL, MKDEV(MAJOR_NR, 0), 1, &sjcd_fops, 0);
1708 if (check_region(sjcd_base, 4)) {
1710 ("SJCD: Init failed, I/O port (%X) is already in use\n",
1717 * Check for card. Since we are booting now, we can't use standard
1720 printk(KERN_INFO "SJCD: Resetting: ");
1721 sjcd_send_cmd(SCMD_RESET);
1722 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1723 unsigned long timer;
1728 for (timer = jiffies; time_before_eq(jiffies, timer););
1731 (void) sjcd_check_status();
1733 if (i == 0 || sjcd_command_failed) {
1734 printk(" reset failed, no drive found.\n");
1741 * Get and print out cdrom version.
1743 printk(KERN_INFO "SJCD: Getting version: ");
1744 sjcd_send_cmd(SCMD_GET_VERSION);
1745 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1746 unsigned long timer;
1751 for (timer = jiffies; time_before_eq(jiffies, timer););
1754 (void) sjcd_check_status();
1756 if (i == 0 || sjcd_command_failed) {
1757 printk(" get version failed, no drive found.\n");
1762 if (sjcd_load_response(&sjcd_version, sizeof(sjcd_version)) == 0) {
1763 printk(" %1x.%02x\n", (int) sjcd_version.major,
1764 (int) sjcd_version.minor);
1766 printk(" read version failed, no drive found.\n");
1772 * Check and print out the tray state. (if it is needed?).
1774 if (!sjcd_status_valid) {
1775 printk(KERN_INFO "SJCD: Getting status: ");
1776 sjcd_send_cmd(SCMD_GET_STATUS);
1777 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1778 unsigned long timer;
1783 for (timer = jiffies;
1784 time_before_eq(jiffies, timer););
1787 (void) sjcd_check_status();
1789 if (i == 0 || sjcd_command_failed) {
1790 printk(" get status failed, no drive found.\n");
1797 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1798 devfs_register(NULL, "sjcd", DEVFS_FL_DEFAULT, MAJOR_NR, 0,
1799 S_IFBLK | S_IRUGO | S_IWUGO, &sjcd_fops, NULL);
1805 static int sjcd_cleanup(void)
1807 if ((devfs_unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1808 printk("SJCD: cannot unregister device.\n");
1810 release_region(sjcd_base, 4);
1811 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1818 void __exit sjcd_exit(void)
1820 devfs_unregister(devfs_find_handle
1821 (NULL, "sjcd", 0, 0, DEVFS_SPECIAL_BLK, 0));
1823 printk("SJCD: module: cannot be removed.\n");
1825 printk(KERN_INFO "SJCD: module: removed.\n");
1829 module_init(sjcd_init);
1831 module_exit(sjcd_exit);
1834 MODULE_LICENSE("GPL");