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 spinlock_t sjcd_lock = SPIN_LOCK_UNLOCKED;
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_PARM(sjcd_base, "i");
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 struct timer_list sjcd_delay_timer = TIMER_INITIALIZER(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 #if defined( SJCD_TRACE )
720 printk("SJCD:ioctl\n");
727 if (!sjcd_status_valid)
729 if (sjcd_update_toc() < 0)
734 #if defined( SJCD_TRACE )
735 printk("SJCD: ioctl: start\n");
741 #if defined( SJCD_TRACE )
742 printk("SJCD: ioctl: stop\n");
744 sjcd_send_cmd(SCMD_PAUSE);
745 (void) sjcd_receive_status();
746 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
751 struct sjcd_hw_qinfo q_info;
752 #if defined( SJCD_TRACE )
753 printk("SJCD: ioctl: pause\n");
755 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
756 sjcd_send_cmd(SCMD_PAUSE);
757 (void) sjcd_receive_status();
758 if (sjcd_get_q_info(&q_info) < 0) {
760 CDROM_AUDIO_NO_STATUS;
764 sjcd_playing.start = q_info.abs;
772 #if defined( SJCD_TRACE )
773 printk("SJCD: ioctl: resume\n");
775 if (sjcd_audio_status == CDROM_AUDIO_PAUSED) {
777 * continue play starting at saved location
779 if (sjcd_play(&sjcd_playing) < 0) {
792 case CDROMPLAYTRKIND:{
795 #if defined( SJCD_TRACE )
796 printk("SJCD: ioctl: playtrkind\n");
798 if (!copy_from_user(&ti, (void *) arg, sizeof(ti))) {
800 if (ti.cdti_trk0 < sjcd_first_track_no)
802 if (ti.cdti_trk1 > sjcd_last_track_no)
803 ti.cdti_trk1 = sjcd_last_track_no;
804 if (ti.cdti_trk0 > ti.cdti_trk1)
808 sjcd_table_of_contents[ti.cdti_trk0].
812 sjcd_last_track_no) ?
813 sjcd_table_of_contents[ti.cdti_trk1 +
815 track_msf : sjcd_table_of_contents[0].
818 if (sjcd_play(&sjcd_playing) < 0) {
830 struct cdrom_msf sjcd_msf;
832 #if defined( SJCD_TRACE )
833 printk("SJCD: ioctl: playmsf\n");
836 verify_area(VERIFY_READ, (void *) arg,
837 sizeof(sjcd_msf))) == 0) {
838 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
839 sjcd_send_cmd(SCMD_PAUSE);
840 (void) sjcd_receive_status();
842 CDROM_AUDIO_NO_STATUS;
845 if (copy_from_user(&sjcd_msf, (void *) arg,
849 sjcd_playing.start.min =
850 bin2bcd(sjcd_msf.cdmsf_min0);
851 sjcd_playing.start.sec =
852 bin2bcd(sjcd_msf.cdmsf_sec0);
853 sjcd_playing.start.frame =
854 bin2bcd(sjcd_msf.cdmsf_frame0);
855 sjcd_playing.end.min =
856 bin2bcd(sjcd_msf.cdmsf_min1);
857 sjcd_playing.end.sec =
858 bin2bcd(sjcd_msf.cdmsf_sec1);
859 sjcd_playing.end.frame =
860 bin2bcd(sjcd_msf.cdmsf_frame1);
862 if (sjcd_play(&sjcd_playing) < 0) {
873 case CDROMREADTOCHDR:{
874 struct cdrom_tochdr toc_header;
875 #if defined (SJCD_TRACE )
876 printk("SJCD: ioctl: readtocheader\n");
878 toc_header.cdth_trk0 = sjcd_first_track_no;
879 toc_header.cdth_trk1 = sjcd_last_track_no;
880 if (copy_to_user((void *)arg, &toc_header,
886 case CDROMREADTOCENTRY:{
887 struct cdrom_tocentry toc_entry;
889 #if defined( SJCD_TRACE )
890 printk("SJCD: ioctl: readtocentry\n");
893 verify_area(VERIFY_WRITE, (void *) arg,
894 sizeof(toc_entry))) == 0) {
895 struct sjcd_hw_disk_info *tp;
897 if (copy_from_user(&toc_entry, (void *) arg,
900 if (toc_entry.cdte_track == CDROM_LEADOUT)
901 tp = &sjcd_table_of_contents[0];
902 else if (toc_entry.cdte_track <
905 else if (toc_entry.cdte_track >
909 tp = &sjcd_table_of_contents
910 [toc_entry.cdte_track];
913 tp->track_control & 0x0F;
914 toc_entry.cdte_ctrl =
915 tp->track_control >> 4;
917 switch (toc_entry.cdte_format) {
919 toc_entry.cdte_addr.lba =
920 msf2hsg(&(tp->un.track_msf));
923 toc_entry.cdte_addr.msf.minute =
924 bcd2bin(tp->un.track_msf.min);
925 toc_entry.cdte_addr.msf.second =
926 bcd2bin(tp->un.track_msf.sec);
927 toc_entry.cdte_addr.msf.frame =
928 bcd2bin(tp->un.track_msf.
934 if (copy_to_user((void *) arg, &toc_entry,
942 struct cdrom_subchnl subchnl;
944 #if defined( SJCD_TRACE )
945 printk("SJCD: ioctl: subchnl\n");
948 verify_area(VERIFY_WRITE, (void *) arg,
949 sizeof(subchnl))) == 0) {
950 struct sjcd_hw_qinfo q_info;
952 if (copy_from_user(&subchnl, (void *) arg,
956 if (sjcd_get_q_info(&q_info) < 0)
959 subchnl.cdsc_audiostatus =
962 q_info.track_control & 0x0F;
964 q_info.track_control >> 4;
966 bcd2bin(q_info.track_no);
967 subchnl.cdsc_ind = bcd2bin(q_info.x);
969 switch (subchnl.cdsc_format) {
971 subchnl.cdsc_absaddr.lba =
972 msf2hsg(&(q_info.abs));
973 subchnl.cdsc_reladdr.lba =
974 msf2hsg(&(q_info.rel));
977 subchnl.cdsc_absaddr.msf.minute =
978 bcd2bin(q_info.abs.min);
979 subchnl.cdsc_absaddr.msf.second =
980 bcd2bin(q_info.abs.sec);
981 subchnl.cdsc_absaddr.msf.frame =
982 bcd2bin(q_info.abs.frame);
983 subchnl.cdsc_reladdr.msf.minute =
984 bcd2bin(q_info.rel.min);
985 subchnl.cdsc_reladdr.msf.second =
986 bcd2bin(q_info.rel.sec);
987 subchnl.cdsc_reladdr.msf.frame =
988 bcd2bin(q_info.rel.frame);
993 if (copy_to_user((void *) arg, &subchnl,
1001 struct cdrom_volctrl vol_ctrl;
1003 #if defined( SJCD_TRACE )
1004 printk("SJCD: ioctl: volctrl\n");
1007 verify_area(VERIFY_READ, (void *) arg,
1008 sizeof(vol_ctrl))) == 0) {
1009 unsigned char dummy[4];
1011 if (copy_from_user(&vol_ctrl, (void *) arg,
1014 sjcd_send_4_cmd(SCMD_SET_VOLUME,
1015 vol_ctrl.channel0, 0xFF,
1016 vol_ctrl.channel1, 0xFF);
1017 if (sjcd_receive_status() < 0)
1019 (void) sjcd_load_response(dummy, 4);
1025 #if defined( SJCD_TRACE )
1026 printk("SJCD: ioctl: eject\n");
1028 if (!sjcd_command_is_in_progress) {
1030 sjcd_send_cmd(SCMD_EJECT_TRAY);
1031 (void) sjcd_receive_status();
1036 #if defined( SJCD_GATHER_STAT )
1038 #if defined( SJCD_TRACE )
1039 printk("SJCD: ioctl: statistic\n");
1041 if (copy_to_user((void *)arg, &statistic,
1054 * Invalidate internal buffers of the driver.
1056 static void sjcd_invalidate_buffers(void)
1059 for (i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[i++] = -1);
1064 * Take care of the different block sizes between cdrom and Linux.
1065 * When Linux gets variable block sizes this will probably go away.
1068 static int current_valid(void)
1071 CURRENT->cmd == READ &&
1072 CURRENT->sector != -1;
1075 static void sjcd_transfer(void)
1077 #if defined( SJCD_TRACE )
1078 printk("SJCD: transfer:\n");
1080 if (current_valid()) {
1081 while (CURRENT->nr_sectors) {
1082 int i, bn = CURRENT->sector / 4;
1084 i < SJCD_BUF_SIZ && sjcd_buf_bn[i] != bn;
1086 if (i < SJCD_BUF_SIZ) {
1088 (i * 4 + (CURRENT->sector & 3)) * 512;
1089 int nr_sectors = 4 - (CURRENT->sector & 3);
1090 if (sjcd_buf_out != i) {
1092 if (sjcd_buf_bn[i] != bn) {
1097 if (nr_sectors > CURRENT->nr_sectors)
1098 nr_sectors = CURRENT->nr_sectors;
1099 #if defined( SJCD_TRACE )
1100 printk("SJCD: copy out\n");
1102 memcpy(CURRENT->buffer, sjcd_buf + offs,
1104 CURRENT->nr_sectors -= nr_sectors;
1105 CURRENT->sector += nr_sectors;
1106 CURRENT->buffer += nr_sectors * 512;
1113 #if defined( SJCD_TRACE )
1114 printk("SJCD: transfer: done\n");
1118 static void sjcd_poll(void)
1120 #if defined( SJCD_GATHER_STAT )
1122 * Update total number of ticks.
1125 statistic.tticks[sjcd_transfer_state]++;
1128 ReSwitch:switch (sjcd_transfer_state) {
1131 #if defined( SJCD_GATHER_STAT )
1132 statistic.idle_ticks++;
1134 #if defined( SJCD_TRACE )
1135 printk("SJCD_S_IDLE\n");
1141 #if defined( SJCD_GATHER_STAT )
1142 statistic.start_ticks++;
1144 sjcd_send_cmd(SCMD_GET_STATUS);
1145 sjcd_transfer_state =
1147 SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1148 sjcd_transfer_timeout = 500;
1149 #if defined( SJCD_TRACE )
1150 printk("SJCD_S_START: goto SJCD_S_%s mode\n",
1151 sjcd_transfer_state ==
1152 SJCD_S_READ ? "READ" : "MODE");
1158 if (sjcd_check_status()) {
1160 * Previous command is completed.
1162 if (!sjcd_status_valid
1163 || sjcd_command_failed) {
1164 #if defined( SJCD_TRACE )
1166 ("SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1168 sjcd_transfer_state = SJCD_S_STOP;
1172 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1173 sjcd_send_1_cmd(SCMD_SET_MODE,
1175 sjcd_transfer_state = SJCD_S_READ;
1176 sjcd_transfer_timeout = 1000;
1177 #if defined( SJCD_TRACE )
1179 ("SJCD_S_MODE: goto SJCD_S_READ mode\n");
1182 #if defined( SJCD_GATHER_STAT )
1184 statistic.mode_ticks++;
1190 if (sjcd_status_valid ? 1 : sjcd_check_status()) {
1192 * Previous command is completed.
1194 if (!sjcd_status_valid
1195 || sjcd_command_failed) {
1196 #if defined( SJCD_TRACE )
1198 ("SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1200 sjcd_transfer_state = SJCD_S_STOP;
1203 if (!sjcd_media_is_available) {
1204 #if defined( SJCD_TRACE )
1206 ("SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n");
1208 sjcd_transfer_state = SJCD_S_STOP;
1211 if (sjcd_mode != SCMD_MODE_COOKED) {
1213 * We seem to come from set mode. So discard one byte of result.
1215 if (sjcd_load_response
1216 (&sjcd_mode, 1) != 0) {
1217 #if defined( SJCD_TRACE )
1219 ("SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n");
1221 sjcd_transfer_state =
1225 if (sjcd_mode != SCMD_MODE_COOKED) {
1226 #if defined( SJCD_TRACE )
1228 ("SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n");
1230 sjcd_transfer_state =
1236 if (current_valid()) {
1237 struct sjcd_play_msf msf;
1239 sjcd_next_bn = CURRENT->sector / 4;
1240 hsg2msf(sjcd_next_bn, &msf.start);
1243 msf.end.frame = sjcd_read_count =
1245 #if defined( SJCD_TRACE )
1247 ("SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1248 msf.start.min, msf.start.sec,
1249 msf.start.frame, msf.end.min,
1250 msf.end.sec, msf.end.frame);
1252 ("sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n",
1253 sjcd_next_bn, sjcd_buf_in,
1255 sjcd_buf_bn[sjcd_buf_in]);
1257 sjcd_send_6_cmd(SCMD_DATA_READ,
1259 sjcd_transfer_state = SJCD_S_DATA;
1260 sjcd_transfer_timeout = 500;
1261 #if defined( SJCD_TRACE )
1263 ("SJCD_S_READ: go to SJCD_S_DATA mode\n");
1266 #if defined( SJCD_TRACE )
1268 ("SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n");
1270 sjcd_transfer_state = SJCD_S_STOP;
1274 #if defined( SJCD_GATHER_STAT )
1276 statistic.read_ticks++;
1287 #if defined( SJCD_TRACE )
1288 printk("SJCD_S_DATA: status = 0x%02x\n", stat);
1290 if (SJCD_STATUS_AVAILABLE(stat)) {
1292 * No data is waiting for us in the drive buffer. Status of operation
1293 * completion is available. Read and parse it.
1297 if (!sjcd_status_valid
1298 || sjcd_command_failed) {
1299 #if defined( SJCD_TRACE )
1301 ("SJCD: read block %d failed, maybe audio disk? Giving up\n",
1304 if (current_valid())
1305 end_request(CURRENT, 0);
1306 #if defined( SJCD_TRACE )
1308 ("SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n");
1310 sjcd_transfer_state = SJCD_S_STOP;
1314 if (!sjcd_media_is_available) {
1316 ("SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n");
1317 sjcd_transfer_state = SJCD_S_STOP;
1321 sjcd_transfer_state = SJCD_S_READ;
1323 } else if (SJCD_DATA_AVAILABLE(stat)) {
1325 * One frame is read into device buffer. We must copy it to our memory.
1326 * Otherwise cdrom hangs up. Check to see if we have something to copy
1329 if (!current_valid()
1330 && sjcd_buf_in == sjcd_buf_out) {
1331 #if defined( SJCD_TRACE )
1333 ("SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n");
1335 (" ... all the date would be discarded\n");
1337 sjcd_transfer_state = SJCD_S_STOP;
1342 * Everything seems to be OK. Just read the frame and recalculate
1345 sjcd_buf_bn[sjcd_buf_in] = -1; /* ??? */
1347 sjcd_buf + 2048 * sjcd_buf_in, 2048);
1348 #if defined( SJCD_TRACE )
1350 ("SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1351 sjcd_next_bn, sjcd_buf_in,
1353 sjcd_buf_bn[sjcd_buf_in]);
1355 sjcd_buf_bn[sjcd_buf_in] = sjcd_next_bn++;
1356 if (sjcd_buf_out == -1)
1357 sjcd_buf_out = sjcd_buf_in;
1358 if (++sjcd_buf_in == SJCD_BUF_SIZ)
1362 * Only one frame is ready at time. So we should turn over to wait for
1363 * another frame. If we need that, of course.
1365 if (--sjcd_read_count == 0) {
1367 * OK, request seems to be precessed. Continue transferring...
1369 if (!sjcd_transfer_is_active) {
1370 while (current_valid()) {
1372 * Continue transferring.
1384 if (current_valid() &&
1385 (CURRENT->sector / 4 <
1387 || CURRENT->sector / 4 >
1390 #if defined( SJCD_TRACE )
1392 ("SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n");
1394 sjcd_transfer_state =
1400 * Now we should turn around rather than wait for while.
1404 #if defined( SJCD_GATHER_STAT )
1406 statistic.data_ticks++;
1412 sjcd_read_count = 0;
1413 sjcd_send_cmd(SCMD_STOP);
1414 sjcd_transfer_state = SJCD_S_STOPPING;
1415 sjcd_transfer_timeout = 500;
1416 #if defined( SJCD_GATHER_STAT )
1417 statistic.stop_ticks++;
1422 case SJCD_S_STOPPING:{
1425 stat = inb(SJCDPORT(1));
1426 #if defined( SJCD_TRACE )
1427 printk("SJCD_S_STOP: status = 0x%02x\n", stat);
1429 if (SJCD_DATA_AVAILABLE(stat)) {
1431 #if defined( SJCD_TRACE )
1432 printk("SJCD_S_STOP: discard data\n");
1435 * Discard all the data from the pipe. Foolish method.
1438 (void) inb(SJCDPORT(2)));
1439 sjcd_transfer_timeout = 500;
1440 } else if (SJCD_STATUS_AVAILABLE(stat)) {
1442 if (sjcd_status_valid
1443 && sjcd_media_is_changed) {
1444 sjcd_toc_uptodate = 0;
1445 sjcd_invalidate_buffers();
1447 if (current_valid()) {
1448 if (sjcd_status_valid)
1449 sjcd_transfer_state =
1452 sjcd_transfer_state =
1455 sjcd_transfer_state = SJCD_S_IDLE;
1458 #if defined( SJCD_GATHER_STAT )
1460 statistic.stopping_ticks++;
1466 printk("SJCD: poll: invalid state %d\n",
1467 sjcd_transfer_state);
1471 if (--sjcd_transfer_timeout == 0) {
1472 printk("SJCD: timeout in state %d\n", sjcd_transfer_state);
1473 while (current_valid())
1474 end_request(CURRENT, 0);
1475 sjcd_send_cmd(SCMD_STOP);
1476 sjcd_transfer_state = SJCD_S_IDLE;
1481 * Get back in some time. 1 should be replaced with count variable to
1482 * avoid unnecessary testings.
1484 SJCD_SET_TIMER(sjcd_poll, 1);
1487 static void do_sjcd_request(request_queue_t * q)
1489 #if defined( SJCD_TRACE )
1490 printk("SJCD: do_sjcd_request(%ld+%ld)\n",
1491 CURRENT->sector, CURRENT->nr_sectors);
1493 sjcd_transfer_is_active = 1;
1494 while (current_valid()) {
1496 if (CURRENT->nr_sectors == 0)
1497 end_request(CURRENT, 1);
1499 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1500 if (sjcd_transfer_state == SJCD_S_IDLE) {
1501 if (!sjcd_toc_uptodate) {
1502 if (sjcd_update_toc() < 0) {
1504 ("SJCD: transfer: discard\n");
1505 while (current_valid())
1506 end_request(CURRENT, 0);
1510 sjcd_transfer_state = SJCD_S_START;
1511 SJCD_SET_TIMER(sjcd_poll, HZ / 100);
1516 sjcd_transfer_is_active = 0;
1517 #if defined( SJCD_TRACE )
1519 ("sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1520 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1521 sjcd_buf_bn[sjcd_buf_in]);
1522 printk("do_sjcd_request ends\n");
1527 * Open the device special file. Check disk is in.
1529 static int sjcd_open(struct inode *ip, struct file *fp)
1532 * Check the presence of device.
1538 * Only read operations are allowed. Really? (:-)
1543 if (sjcd_open_count == 0) {
1544 int s, sjcd_open_tries;
1545 /* We don't know that, do we? */
1547 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1550 sjcd_door_was_open = 0;
1551 sjcd_transfer_state = SJCD_S_IDLE;
1552 sjcd_invalidate_buffers();
1553 sjcd_status_valid = 0;
1556 * Strict status checking.
1558 for (sjcd_open_tries = 4; --sjcd_open_tries;) {
1559 if (!sjcd_status_valid)
1561 if (!sjcd_status_valid) {
1562 #if defined( SJCD_DIAGNOSTIC )
1564 ("SJCD: open: timed out when check status.\n");
1567 } else if (!sjcd_media_is_available) {
1568 #if defined( SJCD_DIAGNOSTIC )
1569 printk("SJCD: open: no disk in drive\n");
1571 if (!sjcd_door_closed) {
1572 sjcd_door_was_open = 1;
1573 #if defined( SJCD_TRACE )
1575 ("SJCD: open: close the tray\n");
1577 s = sjcd_tray_close();
1578 if (s < 0 || !sjcd_status_valid
1579 || sjcd_command_failed) {
1580 #if defined( SJCD_DIAGNOSTIC )
1582 ("SJCD: open: tray close attempt failed\n");
1592 s = sjcd_tray_lock();
1593 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1594 #if defined( SJCD_DIAGNOSTIC )
1595 printk("SJCD: open: tray lock attempt failed\n");
1599 #if defined( SJCD_TRACE )
1600 printk("SJCD: open: done\n");
1612 * On close, we flush all sjcd blocks from the buffer cache.
1614 static int sjcd_release(struct inode *inode, struct file *file)
1618 #if defined( SJCD_TRACE )
1619 printk("SJCD: release\n");
1621 if (--sjcd_open_count == 0) {
1622 sjcd_invalidate_buffers();
1623 s = sjcd_tray_unlock();
1624 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1625 #if defined( SJCD_DIAGNOSTIC )
1627 ("SJCD: release: tray unlock attempt failed.\n");
1630 if (sjcd_door_was_open) {
1631 s = sjcd_tray_open();
1632 if (s < 0 || !sjcd_status_valid
1633 || sjcd_command_failed) {
1634 #if defined( SJCD_DIAGNOSTIC )
1636 ("SJCD: release: tray unload attempt failed.\n");
1645 * A list of file operations allowed for this cdrom.
1647 static struct block_device_operations sjcd_fops = {
1648 .owner = THIS_MODULE,
1650 .release = sjcd_release,
1651 .ioctl = sjcd_ioctl,
1652 .media_changed = sjcd_disk_change,
1656 * Following stuff is intended for initialization of the cdrom. It
1657 * first looks for presence of device. If the device is present, it
1658 * will be reset. Then read the version of the drive and load status.
1659 * The version is two BCD-coded bytes.
1662 unsigned char major, minor;
1665 static struct gendisk *sjcd_disk;
1668 * Test for presence of drive and initialize it. Called at boot time.
1669 * Probe cdrom, find out version and status.
1671 static int __init sjcd_init(void)
1676 "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n",
1677 SJCD_VERSION_MAJOR, SJCD_VERSION_MINOR);
1679 #if defined( SJCD_TRACE )
1680 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1683 if (register_blkdev(MAJOR_NR, "sjcd"))
1686 sjcd_queue = blk_init_queue(do_sjcd_request, &sjcd_lock);
1690 blk_queue_hardsect_size(sjcd_queue, 2048);
1692 sjcd_disk = alloc_disk(1);
1694 printk(KERN_ERR "SJCD: can't allocate disk");
1697 sjcd_disk->major = MAJOR_NR,
1698 sjcd_disk->first_minor = 0,
1699 sjcd_disk->fops = &sjcd_fops,
1700 sprintf(sjcd_disk->disk_name, "sjcd");
1701 sprintf(sjcd_disk->devfs_name, "sjcd");
1703 if (!request_region(sjcd_base, 4,"sjcd")) {
1705 ("SJCD: Init failed, I/O port (%X) is already in use\n",
1711 * Check for card. Since we are booting now, we can't use standard
1714 printk(KERN_INFO "SJCD: Resetting: ");
1715 sjcd_send_cmd(SCMD_RESET);
1716 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1717 unsigned long timer;
1722 for (timer = jiffies; time_before_eq(jiffies, timer););
1725 (void) sjcd_check_status();
1727 if (i == 0 || sjcd_command_failed) {
1728 printk(" reset failed, no drive found.\n");
1734 * Get and print out cdrom version.
1736 printk(KERN_INFO "SJCD: Getting version: ");
1737 sjcd_send_cmd(SCMD_GET_VERSION);
1738 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1739 unsigned long timer;
1744 for (timer = jiffies; time_before_eq(jiffies, timer););
1747 (void) sjcd_check_status();
1749 if (i == 0 || sjcd_command_failed) {
1750 printk(" get version failed, no drive found.\n");
1754 if (sjcd_load_response(&sjcd_version, sizeof(sjcd_version)) == 0) {
1755 printk(" %1x.%02x\n", (int) sjcd_version.major,
1756 (int) sjcd_version.minor);
1758 printk(" read version failed, no drive found.\n");
1763 * Check and print out the tray state. (if it is needed?).
1765 if (!sjcd_status_valid) {
1766 printk(KERN_INFO "SJCD: Getting status: ");
1767 sjcd_send_cmd(SCMD_GET_STATUS);
1768 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1769 unsigned long timer;
1774 for (timer = jiffies;
1775 time_before_eq(jiffies, timer););
1778 (void) sjcd_check_status();
1780 if (i == 0 || sjcd_command_failed) {
1781 printk(" get status failed, no drive found.\n");
1787 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1788 sjcd_disk->queue = sjcd_queue;
1789 add_disk(sjcd_disk);
1794 release_region(sjcd_base, 4);
1796 put_disk(sjcd_disk);
1798 blk_cleanup_queue(sjcd_queue);
1800 if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1801 printk("SJCD: cannot unregister device.\n");
1805 static void __exit sjcd_exit(void)
1807 del_gendisk(sjcd_disk);
1808 put_disk(sjcd_disk);
1809 release_region(sjcd_base, 4);
1810 blk_cleanup_queue(sjcd_queue);
1811 if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1812 printk("SJCD: cannot unregister device.\n");
1813 printk(KERN_INFO "SJCD: module: removed.\n");
1816 module_init(sjcd_init);
1817 module_exit(sjcd_exit);
1819 MODULE_LICENSE("GPL");
1820 MODULE_ALIAS_BLOCKDEV_MAJOR(SANYO_CDROM_MAJOR);