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>
74 #include <asm/system.h>
76 #include <asm/uaccess.h>
77 #include <linux/blkdev.h>
80 static int sjcd_present = 0;
81 static struct request_queue *sjcd_queue;
83 #define MAJOR_NR SANYO_CDROM_MAJOR
84 #define QUEUE (sjcd_queue)
85 #define CURRENT elv_next_request(sjcd_queue)
87 #define SJCD_BUF_SIZ 32 /* cdr-h94a has internal 64K buffer */
90 * buffer for block size conversion
92 static char sjcd_buf[2048 * SJCD_BUF_SIZ];
93 static volatile int sjcd_buf_bn[SJCD_BUF_SIZ], sjcd_next_bn;
94 static volatile int sjcd_buf_in, sjcd_buf_out = -1;
99 static unsigned short sjcd_status_valid = 0;
100 static unsigned short sjcd_door_closed;
101 static unsigned short sjcd_door_was_open;
102 static unsigned short sjcd_media_is_available;
103 static unsigned short sjcd_media_is_changed;
104 static unsigned short sjcd_toc_uptodate = 0;
105 static unsigned short sjcd_command_failed;
106 static volatile unsigned char sjcd_completion_status = 0;
107 static volatile unsigned char sjcd_completion_error = 0;
108 static unsigned short sjcd_command_is_in_progress = 0;
109 static unsigned short sjcd_error_reported = 0;
110 static DEFINE_SPINLOCK(sjcd_lock);
112 static int sjcd_open_count;
114 static int sjcd_audio_status;
115 static struct sjcd_play_msf sjcd_playing;
117 static int sjcd_base = SJCD_BASE_ADDR;
119 module_param(sjcd_base, int, 0);
121 static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
126 static volatile unsigned short sjcd_transfer_is_active = 0;
128 enum sjcd_transfer_state {
137 static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
138 static long sjcd_transfer_timeout = 0;
139 static int sjcd_read_count = 0;
140 static unsigned char sjcd_mode = 0;
142 #define SJCD_READ_TIMEOUT 5000
144 #if defined( SJCD_GATHER_STAT )
148 static struct sjcd_stat statistic;
154 static DEFINE_TIMER(sjcd_delay_timer, NULL, 0, 0);
156 #define SJCD_SET_TIMER( func, tmout ) \
157 ( sjcd_delay_timer.expires = jiffies+tmout, \
158 sjcd_delay_timer.function = ( void * )func, \
159 add_timer( &sjcd_delay_timer ) )
161 #define CLEAR_TIMER del_timer( &sjcd_delay_timer )
164 * Set up device, i.e., use command line data to set
168 static int __init sjcd_setup(char *str)
171 (void) get_options(str, ARRAY_SIZE(ints), ints);
178 __setup("sjcd=", sjcd_setup);
183 * Special converters.
185 static unsigned char bin2bcd(int bin)
191 return (u | (v << 4));
194 static int bcd2bin(unsigned char bcd)
196 return ((bcd >> 4) * 10 + (bcd & 0x0F));
199 static long msf2hsg(struct msf *mp)
201 return (bcd2bin(mp->frame) + bcd2bin(mp->sec) * 75
202 + bcd2bin(mp->min) * 4500 - 150);
205 static void hsg2msf(long hsg, struct msf *msf)
208 msf->min = hsg / 4500;
211 msf->frame = hsg % 75;
212 msf->min = bin2bcd(msf->min); /* convert to BCD */
213 msf->sec = bin2bcd(msf->sec);
214 msf->frame = bin2bcd(msf->frame);
218 * Send a command to cdrom. Invalidate status.
220 static void sjcd_send_cmd(unsigned char cmd)
222 #if defined( SJCD_TRACE )
223 printk("SJCD: send_cmd( 0x%x )\n", cmd);
225 outb(cmd, SJCDPORT(0));
226 sjcd_command_is_in_progress = 1;
227 sjcd_status_valid = 0;
228 sjcd_command_failed = 0;
232 * Send a command with one arg to cdrom. Invalidate status.
234 static void sjcd_send_1_cmd(unsigned char cmd, unsigned char a)
236 #if defined( SJCD_TRACE )
237 printk("SJCD: send_1_cmd( 0x%x, 0x%x )\n", cmd, a);
239 outb(cmd, SJCDPORT(0));
240 outb(a, SJCDPORT(0));
241 sjcd_command_is_in_progress = 1;
242 sjcd_status_valid = 0;
243 sjcd_command_failed = 0;
247 * Send a command with four args to cdrom. Invalidate status.
249 static void sjcd_send_4_cmd(unsigned char cmd, unsigned char a,
250 unsigned char b, unsigned char c,
253 #if defined( SJCD_TRACE )
254 printk("SJCD: send_4_cmd( 0x%x )\n", cmd);
256 outb(cmd, SJCDPORT(0));
257 outb(a, SJCDPORT(0));
258 outb(b, SJCDPORT(0));
259 outb(c, SJCDPORT(0));
260 outb(d, SJCDPORT(0));
261 sjcd_command_is_in_progress = 1;
262 sjcd_status_valid = 0;
263 sjcd_command_failed = 0;
267 * Send a play or read command to cdrom. Invalidate Status.
269 static void sjcd_send_6_cmd(unsigned char cmd, struct sjcd_play_msf *pms)
271 #if defined( SJCD_TRACE )
272 printk("SJCD: send_long_cmd( 0x%x )\n", cmd);
274 outb(cmd, SJCDPORT(0));
275 outb(pms->start.min, SJCDPORT(0));
276 outb(pms->start.sec, SJCDPORT(0));
277 outb(pms->start.frame, SJCDPORT(0));
278 outb(pms->end.min, SJCDPORT(0));
279 outb(pms->end.sec, SJCDPORT(0));
280 outb(pms->end.frame, SJCDPORT(0));
281 sjcd_command_is_in_progress = 1;
282 sjcd_status_valid = 0;
283 sjcd_command_failed = 0;
287 * Get a value from the data port. Should not block, so we use a little
288 * wait for a while. Returns 0 if OK.
290 static int sjcd_load_response(void *buf, int len)
292 unsigned char *resp = (unsigned char *) buf;
297 i-- && !SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))););
299 *resp++ = (unsigned char) inb(SJCDPORT(0));
307 * Load and parse command completion status (drive info byte and maybe error).
308 * Sorry, no error classification yet.
310 static void sjcd_load_status(void)
312 sjcd_media_is_changed = 0;
313 sjcd_completion_error = 0;
314 sjcd_completion_status = inb(SJCDPORT(0));
315 if (sjcd_completion_status & SST_DOOR_OPENED) {
316 sjcd_door_closed = sjcd_media_is_available = 0;
318 sjcd_door_closed = 1;
319 if (sjcd_completion_status & SST_MEDIA_CHANGED)
320 sjcd_media_is_available = sjcd_media_is_changed =
322 else if (sjcd_completion_status & 0x0F) {
324 * OK, we seem to catch an error ...
326 while (!SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1))));
327 sjcd_completion_error = inb(SJCDPORT(0));
328 if ((sjcd_completion_status & 0x08) &&
329 (sjcd_completion_error & 0x40))
330 sjcd_media_is_available = 0;
332 sjcd_command_failed = 1;
334 sjcd_media_is_available = 1;
337 * Ok, status loaded successfully.
339 sjcd_status_valid = 1, sjcd_error_reported = 0;
340 sjcd_command_is_in_progress = 0;
343 * If the disk is changed, the TOC is not valid.
345 if (sjcd_media_is_changed)
346 sjcd_toc_uptodate = 0;
347 #if defined( SJCD_TRACE )
348 printk("SJCD: status %02x.%02x loaded.\n",
349 (int) sjcd_completion_status, (int) sjcd_completion_error);
354 * Read status from cdrom. Check to see if the status is available.
356 static int sjcd_check_status(void)
359 * Try to load the response from cdrom into buffer.
361 if (SJCD_STATUS_AVAILABLE(inb(SJCDPORT(1)))) {
366 * No status is available.
373 * This is just timeout counter, and nothing more. Surprised ? :-)
375 static volatile long sjcd_status_timeout;
378 * We need about 10 seconds to wait. The longest command takes about 5 seconds
379 * to probe the disk (usually after tray closed or drive reset). Other values
380 * should be thought of for other commands.
382 #define SJCD_WAIT_FOR_STATUS_TIMEOUT 1000
384 static void sjcd_status_timer(void)
386 if (sjcd_check_status()) {
388 * The command completed and status is loaded, stop waiting.
390 wake_up(&sjcd_waitq);
391 } else if (--sjcd_status_timeout <= 0) {
395 wake_up(&sjcd_waitq);
398 * We have still some time to wait. Try again.
400 SJCD_SET_TIMER(sjcd_status_timer, 1);
405 * Wait for status for 10 sec approx. Returns non-positive when timed out.
406 * Should not be used while reading data CDs.
408 static int sjcd_wait_for_status(void)
410 sjcd_status_timeout = SJCD_WAIT_FOR_STATUS_TIMEOUT;
411 SJCD_SET_TIMER(sjcd_status_timer, 1);
412 sleep_on(&sjcd_waitq);
413 #if defined( SJCD_DIAGNOSTIC ) || defined ( SJCD_TRACE )
414 if (sjcd_status_timeout <= 0)
415 printk("SJCD: Error Wait For Status.\n");
417 return (sjcd_status_timeout);
420 static int sjcd_receive_status(void)
423 #if defined( SJCD_TRACE )
424 printk("SJCD: receive_status\n");
427 * Wait a bit for status available.
429 for (i = 200; i-- && (sjcd_check_status() == 0););
431 #if defined( SJCD_TRACE )
432 printk("SJCD: long wait for status\n");
434 if (sjcd_wait_for_status() <= 0)
435 printk("SJCD: Timeout when read status.\n");
443 * Load the status. Issue get status command and wait for status available.
445 static void sjcd_get_status(void)
447 #if defined( SJCD_TRACE )
448 printk("SJCD: get_status\n");
450 sjcd_send_cmd(SCMD_GET_STATUS);
451 sjcd_receive_status();
455 * Check the drive if the disk is changed. Should be revised.
457 static int sjcd_disk_change(struct gendisk *disk)
460 printk("SJCD: sjcd_disk_change(%s)\n", disk->disk_name);
462 if (!sjcd_command_is_in_progress)
464 return (sjcd_status_valid ? sjcd_media_is_changed : 0);
468 * Read the table of contents (TOC) and TOC header if necessary.
469 * We assume that the drive contains no more than 99 toc entries.
471 static struct sjcd_hw_disk_info sjcd_table_of_contents[SJCD_MAX_TRACKS];
472 static unsigned char sjcd_first_track_no, sjcd_last_track_no;
473 #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
475 static int sjcd_update_toc(void)
477 struct sjcd_hw_disk_info info;
479 #if defined( SJCD_TRACE )
480 printk("SJCD: update toc:\n");
483 * check to see if we need to do anything
485 if (sjcd_toc_uptodate)
489 * Get the TOC start information.
491 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK);
492 sjcd_receive_status();
494 if (!sjcd_status_valid) {
495 printk("SJCD: cannot load status.\n");
499 if (!sjcd_media_is_available) {
500 printk("SJCD: no disk in drive\n");
504 if (!sjcd_command_failed) {
505 if (sjcd_load_response(&info, sizeof(info)) != 0) {
507 ("SJCD: cannot load response about TOC start.\n");
510 sjcd_first_track_no = bcd2bin(info.un.track_no);
512 printk("SJCD: get first failed\n");
515 #if defined( SJCD_TRACE )
516 printk("SJCD: TOC start 0x%02x ", sjcd_first_track_no);
519 * Get the TOC finish information.
521 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK);
522 sjcd_receive_status();
524 if (!sjcd_status_valid) {
525 printk("SJCD: cannot load status.\n");
529 if (!sjcd_media_is_available) {
530 printk("SJCD: no disk in drive\n");
534 if (!sjcd_command_failed) {
535 if (sjcd_load_response(&info, sizeof(info)) != 0) {
537 ("SJCD: cannot load response about TOC finish.\n");
540 sjcd_last_track_no = bcd2bin(info.un.track_no);
542 printk("SJCD: get last failed\n");
545 #if defined( SJCD_TRACE )
546 printk("SJCD: TOC finish 0x%02x ", sjcd_last_track_no);
548 for (i = sjcd_first_track_no; i <= sjcd_last_track_no; i++) {
550 * Get the first track information.
552 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, bin2bcd(i));
553 sjcd_receive_status();
555 if (!sjcd_status_valid) {
556 printk("SJCD: cannot load status.\n");
560 if (!sjcd_media_is_available) {
561 printk("SJCD: no disk in drive\n");
565 if (!sjcd_command_failed) {
566 if (sjcd_load_response(&sjcd_table_of_contents[i],
571 ("SJCD: cannot load info for %d track\n",
576 printk("SJCD: get info %d failed\n", i);
582 * Get the disk length info.
584 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE);
585 sjcd_receive_status();
587 if (!sjcd_status_valid) {
588 printk("SJCD: cannot load status.\n");
592 if (!sjcd_media_is_available) {
593 printk("SJCD: no disk in drive\n");
597 if (!sjcd_command_failed) {
598 if (sjcd_load_response(&info, sizeof(info)) != 0) {
600 ("SJCD: cannot load response about disk size.\n");
603 sjcd_disk_length.min = info.un.track_msf.min;
604 sjcd_disk_length.sec = info.un.track_msf.sec;
605 sjcd_disk_length.frame = info.un.track_msf.frame;
607 printk("SJCD: get size failed\n");
610 #if defined( SJCD_TRACE )
611 printk("SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
612 sjcd_disk_length.sec, sjcd_disk_length.frame);
618 * Load subchannel information.
620 static int sjcd_get_q_info(struct sjcd_hw_qinfo *qp)
623 #if defined( SJCD_TRACE )
624 printk("SJCD: load sub q\n");
626 sjcd_send_cmd(SCMD_GET_QINFO);
627 s = sjcd_receive_status();
628 if (s < 0 || sjcd_command_failed || !sjcd_status_valid) {
630 s = sjcd_receive_status();
631 if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
633 sjcd_send_cmd(SCMD_GET_QINFO);
634 s = sjcd_receive_status();
635 if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
638 if (sjcd_media_is_available)
639 if (sjcd_load_response(qp, sizeof(*qp)) == 0)
645 * Start playing from the specified position.
647 static int sjcd_play(struct sjcd_play_msf *mp)
649 struct sjcd_play_msf msf;
652 * Turn the device to play mode.
654 sjcd_send_1_cmd(SCMD_SET_MODE, SCMD_MODE_PLAY);
655 if (sjcd_receive_status() < 0)
659 * Seek to the starting point.
661 msf.start = mp->start;
662 msf.end.min = msf.end.sec = msf.end.frame = 0x00;
663 sjcd_send_6_cmd(SCMD_SEEK, &msf);
664 if (sjcd_receive_status() < 0)
670 sjcd_send_6_cmd(SCMD_PLAY, mp);
671 return (sjcd_receive_status());
675 * Tray control functions.
677 static int sjcd_tray_close(void)
679 #if defined( SJCD_TRACE )
680 printk("SJCD: tray_close\n");
682 sjcd_send_cmd(SCMD_CLOSE_TRAY);
683 return (sjcd_receive_status());
686 static int sjcd_tray_lock(void)
688 #if defined( SJCD_TRACE )
689 printk("SJCD: tray_lock\n");
691 sjcd_send_cmd(SCMD_LOCK_TRAY);
692 return (sjcd_receive_status());
695 static int sjcd_tray_unlock(void)
697 #if defined( SJCD_TRACE )
698 printk("SJCD: tray_unlock\n");
700 sjcd_send_cmd(SCMD_UNLOCK_TRAY);
701 return (sjcd_receive_status());
704 static int sjcd_tray_open(void)
706 #if defined( SJCD_TRACE )
707 printk("SJCD: tray_open\n");
709 sjcd_send_cmd(SCMD_EJECT_TRAY);
710 return (sjcd_receive_status());
714 * Do some user commands.
716 static int sjcd_ioctl(struct inode *ip, struct file *fp,
717 unsigned int cmd, unsigned long arg)
719 void __user *argp = (void __user *)arg;
720 #if defined( SJCD_TRACE )
721 printk("SJCD:ioctl\n");
725 if (!sjcd_status_valid)
727 if (sjcd_update_toc() < 0)
732 #if defined( SJCD_TRACE )
733 printk("SJCD: ioctl: start\n");
739 #if defined( SJCD_TRACE )
740 printk("SJCD: ioctl: stop\n");
742 sjcd_send_cmd(SCMD_PAUSE);
743 (void) sjcd_receive_status();
744 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
749 struct sjcd_hw_qinfo q_info;
750 #if defined( SJCD_TRACE )
751 printk("SJCD: ioctl: pause\n");
753 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
754 sjcd_send_cmd(SCMD_PAUSE);
755 (void) sjcd_receive_status();
756 if (sjcd_get_q_info(&q_info) < 0) {
758 CDROM_AUDIO_NO_STATUS;
762 sjcd_playing.start = q_info.abs;
770 #if defined( SJCD_TRACE )
771 printk("SJCD: ioctl: resume\n");
773 if (sjcd_audio_status == CDROM_AUDIO_PAUSED) {
775 * continue play starting at saved location
777 if (sjcd_play(&sjcd_playing) < 0) {
790 case CDROMPLAYTRKIND:{
793 #if defined( SJCD_TRACE )
794 printk("SJCD: ioctl: playtrkind\n");
796 if (!copy_from_user(&ti, argp, sizeof(ti))) {
798 if (ti.cdti_trk0 < sjcd_first_track_no)
800 if (ti.cdti_trk1 > sjcd_last_track_no)
801 ti.cdti_trk1 = sjcd_last_track_no;
802 if (ti.cdti_trk0 > ti.cdti_trk1)
806 sjcd_table_of_contents[ti.cdti_trk0].
810 sjcd_last_track_no) ?
811 sjcd_table_of_contents[ti.cdti_trk1 +
813 track_msf : sjcd_table_of_contents[0].
816 if (sjcd_play(&sjcd_playing) < 0) {
828 struct cdrom_msf sjcd_msf;
830 #if defined( SJCD_TRACE )
831 printk("SJCD: ioctl: playmsf\n");
834 access_ok(VERIFY_READ, argp, sizeof(sjcd_msf))
835 ? 0 : -EFAULT) == 0) {
836 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
837 sjcd_send_cmd(SCMD_PAUSE);
838 (void) sjcd_receive_status();
840 CDROM_AUDIO_NO_STATUS;
843 if (copy_from_user(&sjcd_msf, argp,
847 sjcd_playing.start.min =
848 bin2bcd(sjcd_msf.cdmsf_min0);
849 sjcd_playing.start.sec =
850 bin2bcd(sjcd_msf.cdmsf_sec0);
851 sjcd_playing.start.frame =
852 bin2bcd(sjcd_msf.cdmsf_frame0);
853 sjcd_playing.end.min =
854 bin2bcd(sjcd_msf.cdmsf_min1);
855 sjcd_playing.end.sec =
856 bin2bcd(sjcd_msf.cdmsf_sec1);
857 sjcd_playing.end.frame =
858 bin2bcd(sjcd_msf.cdmsf_frame1);
860 if (sjcd_play(&sjcd_playing) < 0) {
871 case CDROMREADTOCHDR:{
872 struct cdrom_tochdr toc_header;
873 #if defined (SJCD_TRACE )
874 printk("SJCD: ioctl: readtocheader\n");
876 toc_header.cdth_trk0 = sjcd_first_track_no;
877 toc_header.cdth_trk1 = sjcd_last_track_no;
878 if (copy_to_user(argp, &toc_header,
884 case CDROMREADTOCENTRY:{
885 struct cdrom_tocentry toc_entry;
887 #if defined( SJCD_TRACE )
888 printk("SJCD: ioctl: readtocentry\n");
891 access_ok(VERIFY_WRITE, argp, sizeof(toc_entry))
892 ? 0 : -EFAULT) == 0) {
893 struct sjcd_hw_disk_info *tp;
895 if (copy_from_user(&toc_entry, argp,
898 if (toc_entry.cdte_track == CDROM_LEADOUT)
899 tp = &sjcd_table_of_contents[0];
900 else if (toc_entry.cdte_track <
903 else if (toc_entry.cdte_track >
907 tp = &sjcd_table_of_contents
908 [toc_entry.cdte_track];
911 tp->track_control & 0x0F;
912 toc_entry.cdte_ctrl =
913 tp->track_control >> 4;
915 switch (toc_entry.cdte_format) {
917 toc_entry.cdte_addr.lba =
918 msf2hsg(&(tp->un.track_msf));
921 toc_entry.cdte_addr.msf.minute =
922 bcd2bin(tp->un.track_msf.min);
923 toc_entry.cdte_addr.msf.second =
924 bcd2bin(tp->un.track_msf.sec);
925 toc_entry.cdte_addr.msf.frame =
926 bcd2bin(tp->un.track_msf.
932 if (copy_to_user(argp, &toc_entry,
940 struct cdrom_subchnl subchnl;
942 #if defined( SJCD_TRACE )
943 printk("SJCD: ioctl: subchnl\n");
946 access_ok(VERIFY_WRITE, argp, sizeof(subchnl))
947 ? 0 : -EFAULT) == 0) {
948 struct sjcd_hw_qinfo q_info;
950 if (copy_from_user(&subchnl, argp,
954 if (sjcd_get_q_info(&q_info) < 0)
957 subchnl.cdsc_audiostatus =
960 q_info.track_control & 0x0F;
962 q_info.track_control >> 4;
964 bcd2bin(q_info.track_no);
965 subchnl.cdsc_ind = bcd2bin(q_info.x);
967 switch (subchnl.cdsc_format) {
969 subchnl.cdsc_absaddr.lba =
970 msf2hsg(&(q_info.abs));
971 subchnl.cdsc_reladdr.lba =
972 msf2hsg(&(q_info.rel));
975 subchnl.cdsc_absaddr.msf.minute =
976 bcd2bin(q_info.abs.min);
977 subchnl.cdsc_absaddr.msf.second =
978 bcd2bin(q_info.abs.sec);
979 subchnl.cdsc_absaddr.msf.frame =
980 bcd2bin(q_info.abs.frame);
981 subchnl.cdsc_reladdr.msf.minute =
982 bcd2bin(q_info.rel.min);
983 subchnl.cdsc_reladdr.msf.second =
984 bcd2bin(q_info.rel.sec);
985 subchnl.cdsc_reladdr.msf.frame =
986 bcd2bin(q_info.rel.frame);
991 if (copy_to_user(argp, &subchnl,
999 struct cdrom_volctrl vol_ctrl;
1001 #if defined( SJCD_TRACE )
1002 printk("SJCD: ioctl: volctrl\n");
1005 access_ok(VERIFY_READ, argp, sizeof(vol_ctrl))
1006 ? 0 : -EFAULT) == 0) {
1007 unsigned char dummy[4];
1009 if (copy_from_user(&vol_ctrl, argp,
1012 sjcd_send_4_cmd(SCMD_SET_VOLUME,
1013 vol_ctrl.channel0, 0xFF,
1014 vol_ctrl.channel1, 0xFF);
1015 if (sjcd_receive_status() < 0)
1017 (void) sjcd_load_response(dummy, 4);
1023 #if defined( SJCD_TRACE )
1024 printk("SJCD: ioctl: eject\n");
1026 if (!sjcd_command_is_in_progress) {
1028 sjcd_send_cmd(SCMD_EJECT_TRAY);
1029 (void) sjcd_receive_status();
1034 #if defined( SJCD_GATHER_STAT )
1036 #if defined( SJCD_TRACE )
1037 printk("SJCD: ioctl: statistic\n");
1039 if (copy_to_user(argp, &statistic, sizeof(statistic)))
1051 * Invalidate internal buffers of the driver.
1053 static void sjcd_invalidate_buffers(void)
1056 for (i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[i++] = -1);
1061 * Take care of the different block sizes between cdrom and Linux.
1062 * When Linux gets variable block sizes this will probably go away.
1065 static int current_valid(void)
1068 CURRENT->cmd == READ &&
1069 CURRENT->sector != -1;
1072 static void sjcd_transfer(void)
1074 #if defined( SJCD_TRACE )
1075 printk("SJCD: transfer:\n");
1077 if (current_valid()) {
1078 while (CURRENT->nr_sectors) {
1079 int i, bn = CURRENT->sector / 4;
1081 i < SJCD_BUF_SIZ && sjcd_buf_bn[i] != bn;
1083 if (i < SJCD_BUF_SIZ) {
1085 (i * 4 + (CURRENT->sector & 3)) * 512;
1086 int nr_sectors = 4 - (CURRENT->sector & 3);
1087 if (sjcd_buf_out != i) {
1089 if (sjcd_buf_bn[i] != bn) {
1094 if (nr_sectors > CURRENT->nr_sectors)
1095 nr_sectors = CURRENT->nr_sectors;
1096 #if defined( SJCD_TRACE )
1097 printk("SJCD: copy out\n");
1099 memcpy(CURRENT->buffer, sjcd_buf + offs,
1101 CURRENT->nr_sectors -= nr_sectors;
1102 CURRENT->sector += nr_sectors;
1103 CURRENT->buffer += nr_sectors * 512;
1110 #if defined( SJCD_TRACE )
1111 printk("SJCD: transfer: done\n");
1115 static void sjcd_poll(void)
1117 #if defined( SJCD_GATHER_STAT )
1119 * Update total number of ticks.
1122 statistic.tticks[sjcd_transfer_state]++;
1125 ReSwitch:switch (sjcd_transfer_state) {
1128 #if defined( SJCD_GATHER_STAT )
1129 statistic.idle_ticks++;
1131 #if defined( SJCD_TRACE )
1132 printk("SJCD_S_IDLE\n");
1138 #if defined( SJCD_GATHER_STAT )
1139 statistic.start_ticks++;
1141 sjcd_send_cmd(SCMD_GET_STATUS);
1142 sjcd_transfer_state =
1144 SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1145 sjcd_transfer_timeout = 500;
1146 #if defined( SJCD_TRACE )
1147 printk("SJCD_S_START: goto SJCD_S_%s mode\n",
1148 sjcd_transfer_state ==
1149 SJCD_S_READ ? "READ" : "MODE");
1155 if (sjcd_check_status()) {
1157 * Previous command is completed.
1159 if (!sjcd_status_valid
1160 || sjcd_command_failed) {
1161 #if defined( SJCD_TRACE )
1163 ("SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1165 sjcd_transfer_state = SJCD_S_STOP;
1169 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1170 sjcd_send_1_cmd(SCMD_SET_MODE,
1172 sjcd_transfer_state = SJCD_S_READ;
1173 sjcd_transfer_timeout = 1000;
1174 #if defined( SJCD_TRACE )
1176 ("SJCD_S_MODE: goto SJCD_S_READ mode\n");
1179 #if defined( SJCD_GATHER_STAT )
1181 statistic.mode_ticks++;
1187 if (sjcd_status_valid ? 1 : sjcd_check_status()) {
1189 * Previous command is completed.
1191 if (!sjcd_status_valid
1192 || sjcd_command_failed) {
1193 #if defined( SJCD_TRACE )
1195 ("SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1197 sjcd_transfer_state = SJCD_S_STOP;
1200 if (!sjcd_media_is_available) {
1201 #if defined( SJCD_TRACE )
1203 ("SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n");
1205 sjcd_transfer_state = SJCD_S_STOP;
1208 if (sjcd_mode != SCMD_MODE_COOKED) {
1210 * We seem to come from set mode. So discard one byte of result.
1212 if (sjcd_load_response
1213 (&sjcd_mode, 1) != 0) {
1214 #if defined( SJCD_TRACE )
1216 ("SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n");
1218 sjcd_transfer_state =
1222 if (sjcd_mode != SCMD_MODE_COOKED) {
1223 #if defined( SJCD_TRACE )
1225 ("SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n");
1227 sjcd_transfer_state =
1233 if (current_valid()) {
1234 struct sjcd_play_msf msf;
1236 sjcd_next_bn = CURRENT->sector / 4;
1237 hsg2msf(sjcd_next_bn, &msf.start);
1240 msf.end.frame = sjcd_read_count =
1242 #if defined( SJCD_TRACE )
1244 ("SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1245 msf.start.min, msf.start.sec,
1246 msf.start.frame, msf.end.min,
1247 msf.end.sec, msf.end.frame);
1249 ("sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n",
1250 sjcd_next_bn, sjcd_buf_in,
1252 sjcd_buf_bn[sjcd_buf_in]);
1254 sjcd_send_6_cmd(SCMD_DATA_READ,
1256 sjcd_transfer_state = SJCD_S_DATA;
1257 sjcd_transfer_timeout = 500;
1258 #if defined( SJCD_TRACE )
1260 ("SJCD_S_READ: go to SJCD_S_DATA mode\n");
1263 #if defined( SJCD_TRACE )
1265 ("SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n");
1267 sjcd_transfer_state = SJCD_S_STOP;
1271 #if defined( SJCD_GATHER_STAT )
1273 statistic.read_ticks++;
1284 #if defined( SJCD_TRACE )
1285 printk("SJCD_S_DATA: status = 0x%02x\n", stat);
1287 if (SJCD_STATUS_AVAILABLE(stat)) {
1289 * No data is waiting for us in the drive buffer. Status of operation
1290 * completion is available. Read and parse it.
1294 if (!sjcd_status_valid
1295 || sjcd_command_failed) {
1296 #if defined( SJCD_TRACE )
1298 ("SJCD: read block %d failed, maybe audio disk? Giving up\n",
1301 if (current_valid())
1302 end_request(CURRENT, 0);
1303 #if defined( SJCD_TRACE )
1305 ("SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n");
1307 sjcd_transfer_state = SJCD_S_STOP;
1311 if (!sjcd_media_is_available) {
1313 ("SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n");
1314 sjcd_transfer_state = SJCD_S_STOP;
1318 sjcd_transfer_state = SJCD_S_READ;
1320 } else if (SJCD_DATA_AVAILABLE(stat)) {
1322 * One frame is read into device buffer. We must copy it to our memory.
1323 * Otherwise cdrom hangs up. Check to see if we have something to copy
1326 if (!current_valid()
1327 && sjcd_buf_in == sjcd_buf_out) {
1328 #if defined( SJCD_TRACE )
1330 ("SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n");
1332 (" ... all the date would be discarded\n");
1334 sjcd_transfer_state = SJCD_S_STOP;
1339 * Everything seems to be OK. Just read the frame and recalculate
1342 sjcd_buf_bn[sjcd_buf_in] = -1; /* ??? */
1344 sjcd_buf + 2048 * sjcd_buf_in, 2048);
1345 #if defined( SJCD_TRACE )
1347 ("SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1348 sjcd_next_bn, sjcd_buf_in,
1350 sjcd_buf_bn[sjcd_buf_in]);
1352 sjcd_buf_bn[sjcd_buf_in] = sjcd_next_bn++;
1353 if (sjcd_buf_out == -1)
1354 sjcd_buf_out = sjcd_buf_in;
1355 if (++sjcd_buf_in == SJCD_BUF_SIZ)
1359 * Only one frame is ready at time. So we should turn over to wait for
1360 * another frame. If we need that, of course.
1362 if (--sjcd_read_count == 0) {
1364 * OK, request seems to be precessed. Continue transferring...
1366 if (!sjcd_transfer_is_active) {
1367 while (current_valid()) {
1369 * Continue transferring.
1381 if (current_valid() &&
1382 (CURRENT->sector / 4 <
1384 || CURRENT->sector / 4 >
1387 #if defined( SJCD_TRACE )
1389 ("SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n");
1391 sjcd_transfer_state =
1397 * Now we should turn around rather than wait for while.
1401 #if defined( SJCD_GATHER_STAT )
1403 statistic.data_ticks++;
1409 sjcd_read_count = 0;
1410 sjcd_send_cmd(SCMD_STOP);
1411 sjcd_transfer_state = SJCD_S_STOPPING;
1412 sjcd_transfer_timeout = 500;
1413 #if defined( SJCD_GATHER_STAT )
1414 statistic.stop_ticks++;
1419 case SJCD_S_STOPPING:{
1422 stat = inb(SJCDPORT(1));
1423 #if defined( SJCD_TRACE )
1424 printk("SJCD_S_STOP: status = 0x%02x\n", stat);
1426 if (SJCD_DATA_AVAILABLE(stat)) {
1428 #if defined( SJCD_TRACE )
1429 printk("SJCD_S_STOP: discard data\n");
1432 * Discard all the data from the pipe. Foolish method.
1435 (void) inb(SJCDPORT(2)));
1436 sjcd_transfer_timeout = 500;
1437 } else if (SJCD_STATUS_AVAILABLE(stat)) {
1439 if (sjcd_status_valid
1440 && sjcd_media_is_changed) {
1441 sjcd_toc_uptodate = 0;
1442 sjcd_invalidate_buffers();
1444 if (current_valid()) {
1445 if (sjcd_status_valid)
1446 sjcd_transfer_state =
1449 sjcd_transfer_state =
1452 sjcd_transfer_state = SJCD_S_IDLE;
1455 #if defined( SJCD_GATHER_STAT )
1457 statistic.stopping_ticks++;
1463 printk("SJCD: poll: invalid state %d\n",
1464 sjcd_transfer_state);
1468 if (--sjcd_transfer_timeout == 0) {
1469 printk("SJCD: timeout in state %d\n", sjcd_transfer_state);
1470 while (current_valid())
1471 end_request(CURRENT, 0);
1472 sjcd_send_cmd(SCMD_STOP);
1473 sjcd_transfer_state = SJCD_S_IDLE;
1478 * Get back in some time. 1 should be replaced with count variable to
1479 * avoid unnecessary testings.
1481 SJCD_SET_TIMER(sjcd_poll, 1);
1484 static void do_sjcd_request(request_queue_t * q)
1486 #if defined( SJCD_TRACE )
1487 printk("SJCD: do_sjcd_request(%ld+%ld)\n",
1488 CURRENT->sector, CURRENT->nr_sectors);
1490 sjcd_transfer_is_active = 1;
1491 while (current_valid()) {
1493 if (CURRENT->nr_sectors == 0)
1494 end_request(CURRENT, 1);
1496 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1497 if (sjcd_transfer_state == SJCD_S_IDLE) {
1498 if (!sjcd_toc_uptodate) {
1499 if (sjcd_update_toc() < 0) {
1501 ("SJCD: transfer: discard\n");
1502 while (current_valid())
1503 end_request(CURRENT, 0);
1507 sjcd_transfer_state = SJCD_S_START;
1508 SJCD_SET_TIMER(sjcd_poll, HZ / 100);
1513 sjcd_transfer_is_active = 0;
1514 #if defined( SJCD_TRACE )
1516 ("sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1517 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1518 sjcd_buf_bn[sjcd_buf_in]);
1519 printk("do_sjcd_request ends\n");
1524 * Open the device special file. Check disk is in.
1526 static int sjcd_open(struct inode *ip, struct file *fp)
1529 * Check the presence of device.
1535 * Only read operations are allowed. Really? (:-)
1540 if (sjcd_open_count == 0) {
1541 int s, sjcd_open_tries;
1542 /* We don't know that, do we? */
1544 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1547 sjcd_door_was_open = 0;
1548 sjcd_transfer_state = SJCD_S_IDLE;
1549 sjcd_invalidate_buffers();
1550 sjcd_status_valid = 0;
1553 * Strict status checking.
1555 for (sjcd_open_tries = 4; --sjcd_open_tries;) {
1556 if (!sjcd_status_valid)
1558 if (!sjcd_status_valid) {
1559 #if defined( SJCD_DIAGNOSTIC )
1561 ("SJCD: open: timed out when check status.\n");
1564 } else if (!sjcd_media_is_available) {
1565 #if defined( SJCD_DIAGNOSTIC )
1566 printk("SJCD: open: no disk in drive\n");
1568 if (!sjcd_door_closed) {
1569 sjcd_door_was_open = 1;
1570 #if defined( SJCD_TRACE )
1572 ("SJCD: open: close the tray\n");
1574 s = sjcd_tray_close();
1575 if (s < 0 || !sjcd_status_valid
1576 || sjcd_command_failed) {
1577 #if defined( SJCD_DIAGNOSTIC )
1579 ("SJCD: open: tray close attempt failed\n");
1589 s = sjcd_tray_lock();
1590 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1591 #if defined( SJCD_DIAGNOSTIC )
1592 printk("SJCD: open: tray lock attempt failed\n");
1596 #if defined( SJCD_TRACE )
1597 printk("SJCD: open: done\n");
1609 * On close, we flush all sjcd blocks from the buffer cache.
1611 static int sjcd_release(struct inode *inode, struct file *file)
1615 #if defined( SJCD_TRACE )
1616 printk("SJCD: release\n");
1618 if (--sjcd_open_count == 0) {
1619 sjcd_invalidate_buffers();
1620 s = sjcd_tray_unlock();
1621 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1622 #if defined( SJCD_DIAGNOSTIC )
1624 ("SJCD: release: tray unlock attempt failed.\n");
1627 if (sjcd_door_was_open) {
1628 s = sjcd_tray_open();
1629 if (s < 0 || !sjcd_status_valid
1630 || sjcd_command_failed) {
1631 #if defined( SJCD_DIAGNOSTIC )
1633 ("SJCD: release: tray unload attempt failed.\n");
1642 * A list of file operations allowed for this cdrom.
1644 static struct block_device_operations sjcd_fops = {
1645 .owner = THIS_MODULE,
1647 .release = sjcd_release,
1648 .ioctl = sjcd_ioctl,
1649 .media_changed = sjcd_disk_change,
1653 * Following stuff is intended for initialization of the cdrom. It
1654 * first looks for presence of device. If the device is present, it
1655 * will be reset. Then read the version of the drive and load status.
1656 * The version is two BCD-coded bytes.
1659 unsigned char major, minor;
1662 static struct gendisk *sjcd_disk;
1665 * Test for presence of drive and initialize it. Called at boot time.
1666 * Probe cdrom, find out version and status.
1668 static int __init sjcd_init(void)
1673 "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n",
1674 SJCD_VERSION_MAJOR, SJCD_VERSION_MINOR);
1676 #if defined( SJCD_TRACE )
1677 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1680 if (register_blkdev(MAJOR_NR, "sjcd"))
1683 sjcd_queue = blk_init_queue(do_sjcd_request, &sjcd_lock);
1687 blk_queue_hardsect_size(sjcd_queue, 2048);
1689 sjcd_disk = alloc_disk(1);
1691 printk(KERN_ERR "SJCD: can't allocate disk");
1694 sjcd_disk->major = MAJOR_NR,
1695 sjcd_disk->first_minor = 0,
1696 sjcd_disk->fops = &sjcd_fops,
1697 sprintf(sjcd_disk->disk_name, "sjcd");
1699 if (!request_region(sjcd_base, 4,"sjcd")) {
1701 ("SJCD: Init failed, I/O port (%X) is already in use\n",
1707 * Check for card. Since we are booting now, we can't use standard
1710 printk(KERN_INFO "SJCD: Resetting: ");
1711 sjcd_send_cmd(SCMD_RESET);
1712 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1713 unsigned long timer;
1718 for (timer = jiffies; time_before_eq(jiffies, timer););
1721 (void) sjcd_check_status();
1723 if (i == 0 || sjcd_command_failed) {
1724 printk(" reset failed, no drive found.\n");
1730 * Get and print out cdrom version.
1732 printk(KERN_INFO "SJCD: Getting version: ");
1733 sjcd_send_cmd(SCMD_GET_VERSION);
1734 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1735 unsigned long timer;
1740 for (timer = jiffies; time_before_eq(jiffies, timer););
1743 (void) sjcd_check_status();
1745 if (i == 0 || sjcd_command_failed) {
1746 printk(" get version failed, no drive found.\n");
1750 if (sjcd_load_response(&sjcd_version, sizeof(sjcd_version)) == 0) {
1751 printk(" %1x.%02x\n", (int) sjcd_version.major,
1752 (int) sjcd_version.minor);
1754 printk(" read version failed, no drive found.\n");
1759 * Check and print out the tray state. (if it is needed?).
1761 if (!sjcd_status_valid) {
1762 printk(KERN_INFO "SJCD: Getting status: ");
1763 sjcd_send_cmd(SCMD_GET_STATUS);
1764 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1765 unsigned long timer;
1770 for (timer = jiffies;
1771 time_before_eq(jiffies, timer););
1774 (void) sjcd_check_status();
1776 if (i == 0 || sjcd_command_failed) {
1777 printk(" get status failed, no drive found.\n");
1783 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1784 sjcd_disk->queue = sjcd_queue;
1785 add_disk(sjcd_disk);
1790 release_region(sjcd_base, 4);
1792 put_disk(sjcd_disk);
1794 blk_cleanup_queue(sjcd_queue);
1796 if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1797 printk("SJCD: cannot unregister device.\n");
1801 static void __exit sjcd_exit(void)
1803 del_gendisk(sjcd_disk);
1804 put_disk(sjcd_disk);
1805 release_region(sjcd_base, 4);
1806 blk_cleanup_queue(sjcd_queue);
1807 if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1808 printk("SJCD: cannot unregister device.\n");
1809 printk(KERN_INFO "SJCD: module: removed.\n");
1812 module_init(sjcd_init);
1813 module_exit(sjcd_exit);
1815 MODULE_LICENSE("GPL");
1816 MODULE_ALIAS_BLOCKDEV_MAJOR(SANYO_CDROM_MAJOR);