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>
64 #include <linux/timer.h>
66 #include <linux/kernel.h>
67 #include <linux/cdrom.h>
68 #include <linux/ioport.h>
69 #include <linux/string.h>
70 #include <linux/major.h>
71 #include <linux/init.h>
73 #include <asm/system.h>
75 #include <asm/uaccess.h>
76 #include <linux/blkdev.h>
79 static int sjcd_present = 0;
80 static struct request_queue *sjcd_queue;
82 #define MAJOR_NR SANYO_CDROM_MAJOR
83 #define QUEUE (sjcd_queue)
84 #define CURRENT elv_next_request(sjcd_queue)
86 #define SJCD_BUF_SIZ 32 /* cdr-h94a has internal 64K buffer */
89 * buffer for block size conversion
91 static char sjcd_buf[2048 * SJCD_BUF_SIZ];
92 static volatile int sjcd_buf_bn[SJCD_BUF_SIZ], sjcd_next_bn;
93 static volatile int sjcd_buf_in, sjcd_buf_out = -1;
98 static unsigned short sjcd_status_valid = 0;
99 static unsigned short sjcd_door_closed;
100 static unsigned short sjcd_door_was_open;
101 static unsigned short sjcd_media_is_available;
102 static unsigned short sjcd_media_is_changed;
103 static unsigned short sjcd_toc_uptodate = 0;
104 static unsigned short sjcd_command_failed;
105 static volatile unsigned char sjcd_completion_status = 0;
106 static volatile unsigned char sjcd_completion_error = 0;
107 static unsigned short sjcd_command_is_in_progress = 0;
108 static unsigned short sjcd_error_reported = 0;
109 static DEFINE_SPINLOCK(sjcd_lock);
111 static int sjcd_open_count;
113 static int sjcd_audio_status;
114 static struct sjcd_play_msf sjcd_playing;
116 static int sjcd_base = SJCD_BASE_ADDR;
118 module_param(sjcd_base, int, 0);
120 static DECLARE_WAIT_QUEUE_HEAD(sjcd_waitq);
125 static volatile unsigned short sjcd_transfer_is_active = 0;
127 enum sjcd_transfer_state {
136 static enum sjcd_transfer_state sjcd_transfer_state = SJCD_S_IDLE;
137 static long sjcd_transfer_timeout = 0;
138 static int sjcd_read_count = 0;
139 static unsigned char sjcd_mode = 0;
141 #define SJCD_READ_TIMEOUT 5000
143 #if defined( SJCD_GATHER_STAT )
147 static struct sjcd_stat statistic;
153 static DEFINE_TIMER(sjcd_delay_timer, NULL, 0, 0);
155 #define SJCD_SET_TIMER( func, tmout ) \
156 ( sjcd_delay_timer.expires = jiffies+tmout, \
157 sjcd_delay_timer.function = ( void * )func, \
158 add_timer( &sjcd_delay_timer ) )
160 #define CLEAR_TIMER del_timer( &sjcd_delay_timer )
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(struct gendisk *disk)
459 printk("SJCD: sjcd_disk_change(%s)\n", disk->disk_name);
461 if (!sjcd_command_is_in_progress)
463 return (sjcd_status_valid ? sjcd_media_is_changed : 0);
467 * Read the table of contents (TOC) and TOC header if necessary.
468 * We assume that the drive contains no more than 99 toc entries.
470 static struct sjcd_hw_disk_info sjcd_table_of_contents[SJCD_MAX_TRACKS];
471 static unsigned char sjcd_first_track_no, sjcd_last_track_no;
472 #define sjcd_disk_length sjcd_table_of_contents[0].un.track_msf
474 static int sjcd_update_toc(void)
476 struct sjcd_hw_disk_info info;
478 #if defined( SJCD_TRACE )
479 printk("SJCD: update toc:\n");
482 * check to see if we need to do anything
484 if (sjcd_toc_uptodate)
488 * Get the TOC start information.
490 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_1_TRACK);
491 sjcd_receive_status();
493 if (!sjcd_status_valid) {
494 printk("SJCD: cannot load status.\n");
498 if (!sjcd_media_is_available) {
499 printk("SJCD: no disk in drive\n");
503 if (!sjcd_command_failed) {
504 if (sjcd_load_response(&info, sizeof(info)) != 0) {
506 ("SJCD: cannot load response about TOC start.\n");
509 sjcd_first_track_no = bcd2bin(info.un.track_no);
511 printk("SJCD: get first failed\n");
514 #if defined( SJCD_TRACE )
515 printk("SJCD: TOC start 0x%02x ", sjcd_first_track_no);
518 * Get the TOC finish information.
520 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_L_TRACK);
521 sjcd_receive_status();
523 if (!sjcd_status_valid) {
524 printk("SJCD: cannot load status.\n");
528 if (!sjcd_media_is_available) {
529 printk("SJCD: no disk in drive\n");
533 if (!sjcd_command_failed) {
534 if (sjcd_load_response(&info, sizeof(info)) != 0) {
536 ("SJCD: cannot load response about TOC finish.\n");
539 sjcd_last_track_no = bcd2bin(info.un.track_no);
541 printk("SJCD: get last failed\n");
544 #if defined( SJCD_TRACE )
545 printk("SJCD: TOC finish 0x%02x ", sjcd_last_track_no);
547 for (i = sjcd_first_track_no; i <= sjcd_last_track_no; i++) {
549 * Get the first track information.
551 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, bin2bcd(i));
552 sjcd_receive_status();
554 if (!sjcd_status_valid) {
555 printk("SJCD: cannot load status.\n");
559 if (!sjcd_media_is_available) {
560 printk("SJCD: no disk in drive\n");
564 if (!sjcd_command_failed) {
565 if (sjcd_load_response(&sjcd_table_of_contents[i],
570 ("SJCD: cannot load info for %d track\n",
575 printk("SJCD: get info %d failed\n", i);
581 * Get the disk length info.
583 sjcd_send_1_cmd(SCMD_GET_DISK_INFO, SCMD_GET_D_SIZE);
584 sjcd_receive_status();
586 if (!sjcd_status_valid) {
587 printk("SJCD: cannot load status.\n");
591 if (!sjcd_media_is_available) {
592 printk("SJCD: no disk in drive\n");
596 if (!sjcd_command_failed) {
597 if (sjcd_load_response(&info, sizeof(info)) != 0) {
599 ("SJCD: cannot load response about disk size.\n");
602 sjcd_disk_length.min = info.un.track_msf.min;
603 sjcd_disk_length.sec = info.un.track_msf.sec;
604 sjcd_disk_length.frame = info.un.track_msf.frame;
606 printk("SJCD: get size failed\n");
609 #if defined( SJCD_TRACE )
610 printk("SJCD: (%02x:%02x.%02x)\n", sjcd_disk_length.min,
611 sjcd_disk_length.sec, sjcd_disk_length.frame);
617 * Load subchannel information.
619 static int sjcd_get_q_info(struct sjcd_hw_qinfo *qp)
622 #if defined( SJCD_TRACE )
623 printk("SJCD: load sub q\n");
625 sjcd_send_cmd(SCMD_GET_QINFO);
626 s = sjcd_receive_status();
627 if (s < 0 || sjcd_command_failed || !sjcd_status_valid) {
629 s = sjcd_receive_status();
630 if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
632 sjcd_send_cmd(SCMD_GET_QINFO);
633 s = sjcd_receive_status();
634 if (s < 0 || sjcd_command_failed || !sjcd_status_valid)
637 if (sjcd_media_is_available)
638 if (sjcd_load_response(qp, sizeof(*qp)) == 0)
644 * Start playing from the specified position.
646 static int sjcd_play(struct sjcd_play_msf *mp)
648 struct sjcd_play_msf msf;
651 * Turn the device to play mode.
653 sjcd_send_1_cmd(SCMD_SET_MODE, SCMD_MODE_PLAY);
654 if (sjcd_receive_status() < 0)
658 * Seek to the starting point.
660 msf.start = mp->start;
661 msf.end.min = msf.end.sec = msf.end.frame = 0x00;
662 sjcd_send_6_cmd(SCMD_SEEK, &msf);
663 if (sjcd_receive_status() < 0)
669 sjcd_send_6_cmd(SCMD_PLAY, mp);
670 return (sjcd_receive_status());
674 * Tray control functions.
676 static int sjcd_tray_close(void)
678 #if defined( SJCD_TRACE )
679 printk("SJCD: tray_close\n");
681 sjcd_send_cmd(SCMD_CLOSE_TRAY);
682 return (sjcd_receive_status());
685 static int sjcd_tray_lock(void)
687 #if defined( SJCD_TRACE )
688 printk("SJCD: tray_lock\n");
690 sjcd_send_cmd(SCMD_LOCK_TRAY);
691 return (sjcd_receive_status());
694 static int sjcd_tray_unlock(void)
696 #if defined( SJCD_TRACE )
697 printk("SJCD: tray_unlock\n");
699 sjcd_send_cmd(SCMD_UNLOCK_TRAY);
700 return (sjcd_receive_status());
703 static int sjcd_tray_open(void)
705 #if defined( SJCD_TRACE )
706 printk("SJCD: tray_open\n");
708 sjcd_send_cmd(SCMD_EJECT_TRAY);
709 return (sjcd_receive_status());
713 * Do some user commands.
715 static int sjcd_ioctl(struct inode *ip, struct file *fp,
716 unsigned int cmd, unsigned long arg)
718 void __user *argp = (void __user *)arg;
719 #if defined( SJCD_TRACE )
720 printk("SJCD:ioctl\n");
724 if (!sjcd_status_valid)
726 if (sjcd_update_toc() < 0)
731 #if defined( SJCD_TRACE )
732 printk("SJCD: ioctl: start\n");
738 #if defined( SJCD_TRACE )
739 printk("SJCD: ioctl: stop\n");
741 sjcd_send_cmd(SCMD_PAUSE);
742 (void) sjcd_receive_status();
743 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
748 struct sjcd_hw_qinfo q_info;
749 #if defined( SJCD_TRACE )
750 printk("SJCD: ioctl: pause\n");
752 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
753 sjcd_send_cmd(SCMD_PAUSE);
754 (void) sjcd_receive_status();
755 if (sjcd_get_q_info(&q_info) < 0) {
757 CDROM_AUDIO_NO_STATUS;
761 sjcd_playing.start = q_info.abs;
769 #if defined( SJCD_TRACE )
770 printk("SJCD: ioctl: resume\n");
772 if (sjcd_audio_status == CDROM_AUDIO_PAUSED) {
774 * continue play starting at saved location
776 if (sjcd_play(&sjcd_playing) < 0) {
789 case CDROMPLAYTRKIND:{
792 #if defined( SJCD_TRACE )
793 printk("SJCD: ioctl: playtrkind\n");
795 if (!copy_from_user(&ti, argp, sizeof(ti))) {
797 if (ti.cdti_trk0 < sjcd_first_track_no)
799 if (ti.cdti_trk1 > sjcd_last_track_no)
800 ti.cdti_trk1 = sjcd_last_track_no;
801 if (ti.cdti_trk0 > ti.cdti_trk1)
805 sjcd_table_of_contents[ti.cdti_trk0].
809 sjcd_last_track_no) ?
810 sjcd_table_of_contents[ti.cdti_trk1 +
812 track_msf : sjcd_table_of_contents[0].
815 if (sjcd_play(&sjcd_playing) < 0) {
827 struct cdrom_msf sjcd_msf;
829 #if defined( SJCD_TRACE )
830 printk("SJCD: ioctl: playmsf\n");
833 access_ok(VERIFY_READ, argp, sizeof(sjcd_msf))
834 ? 0 : -EFAULT) == 0) {
835 if (sjcd_audio_status == CDROM_AUDIO_PLAY) {
836 sjcd_send_cmd(SCMD_PAUSE);
837 (void) sjcd_receive_status();
839 CDROM_AUDIO_NO_STATUS;
842 if (copy_from_user(&sjcd_msf, argp,
846 sjcd_playing.start.min =
847 bin2bcd(sjcd_msf.cdmsf_min0);
848 sjcd_playing.start.sec =
849 bin2bcd(sjcd_msf.cdmsf_sec0);
850 sjcd_playing.start.frame =
851 bin2bcd(sjcd_msf.cdmsf_frame0);
852 sjcd_playing.end.min =
853 bin2bcd(sjcd_msf.cdmsf_min1);
854 sjcd_playing.end.sec =
855 bin2bcd(sjcd_msf.cdmsf_sec1);
856 sjcd_playing.end.frame =
857 bin2bcd(sjcd_msf.cdmsf_frame1);
859 if (sjcd_play(&sjcd_playing) < 0) {
870 case CDROMREADTOCHDR:{
871 struct cdrom_tochdr toc_header;
872 #if defined (SJCD_TRACE )
873 printk("SJCD: ioctl: readtocheader\n");
875 toc_header.cdth_trk0 = sjcd_first_track_no;
876 toc_header.cdth_trk1 = sjcd_last_track_no;
877 if (copy_to_user(argp, &toc_header,
883 case CDROMREADTOCENTRY:{
884 struct cdrom_tocentry toc_entry;
886 #if defined( SJCD_TRACE )
887 printk("SJCD: ioctl: readtocentry\n");
890 access_ok(VERIFY_WRITE, argp, sizeof(toc_entry))
891 ? 0 : -EFAULT) == 0) {
892 struct sjcd_hw_disk_info *tp;
894 if (copy_from_user(&toc_entry, argp,
897 if (toc_entry.cdte_track == CDROM_LEADOUT)
898 tp = &sjcd_table_of_contents[0];
899 else if (toc_entry.cdte_track <
902 else if (toc_entry.cdte_track >
906 tp = &sjcd_table_of_contents
907 [toc_entry.cdte_track];
910 tp->track_control & 0x0F;
911 toc_entry.cdte_ctrl =
912 tp->track_control >> 4;
914 switch (toc_entry.cdte_format) {
916 toc_entry.cdte_addr.lba =
917 msf2hsg(&(tp->un.track_msf));
920 toc_entry.cdte_addr.msf.minute =
921 bcd2bin(tp->un.track_msf.min);
922 toc_entry.cdte_addr.msf.second =
923 bcd2bin(tp->un.track_msf.sec);
924 toc_entry.cdte_addr.msf.frame =
925 bcd2bin(tp->un.track_msf.
931 if (copy_to_user(argp, &toc_entry,
939 struct cdrom_subchnl subchnl;
941 #if defined( SJCD_TRACE )
942 printk("SJCD: ioctl: subchnl\n");
945 access_ok(VERIFY_WRITE, argp, sizeof(subchnl))
946 ? 0 : -EFAULT) == 0) {
947 struct sjcd_hw_qinfo q_info;
949 if (copy_from_user(&subchnl, argp,
953 if (sjcd_get_q_info(&q_info) < 0)
956 subchnl.cdsc_audiostatus =
959 q_info.track_control & 0x0F;
961 q_info.track_control >> 4;
963 bcd2bin(q_info.track_no);
964 subchnl.cdsc_ind = bcd2bin(q_info.x);
966 switch (subchnl.cdsc_format) {
968 subchnl.cdsc_absaddr.lba =
969 msf2hsg(&(q_info.abs));
970 subchnl.cdsc_reladdr.lba =
971 msf2hsg(&(q_info.rel));
974 subchnl.cdsc_absaddr.msf.minute =
975 bcd2bin(q_info.abs.min);
976 subchnl.cdsc_absaddr.msf.second =
977 bcd2bin(q_info.abs.sec);
978 subchnl.cdsc_absaddr.msf.frame =
979 bcd2bin(q_info.abs.frame);
980 subchnl.cdsc_reladdr.msf.minute =
981 bcd2bin(q_info.rel.min);
982 subchnl.cdsc_reladdr.msf.second =
983 bcd2bin(q_info.rel.sec);
984 subchnl.cdsc_reladdr.msf.frame =
985 bcd2bin(q_info.rel.frame);
990 if (copy_to_user(argp, &subchnl,
998 struct cdrom_volctrl vol_ctrl;
1000 #if defined( SJCD_TRACE )
1001 printk("SJCD: ioctl: volctrl\n");
1004 access_ok(VERIFY_READ, argp, sizeof(vol_ctrl))
1005 ? 0 : -EFAULT) == 0) {
1006 unsigned char dummy[4];
1008 if (copy_from_user(&vol_ctrl, argp,
1011 sjcd_send_4_cmd(SCMD_SET_VOLUME,
1012 vol_ctrl.channel0, 0xFF,
1013 vol_ctrl.channel1, 0xFF);
1014 if (sjcd_receive_status() < 0)
1016 (void) sjcd_load_response(dummy, 4);
1022 #if defined( SJCD_TRACE )
1023 printk("SJCD: ioctl: eject\n");
1025 if (!sjcd_command_is_in_progress) {
1027 sjcd_send_cmd(SCMD_EJECT_TRAY);
1028 (void) sjcd_receive_status();
1033 #if defined( SJCD_GATHER_STAT )
1035 #if defined( SJCD_TRACE )
1036 printk("SJCD: ioctl: statistic\n");
1038 if (copy_to_user(argp, &statistic, sizeof(statistic)))
1050 * Invalidate internal buffers of the driver.
1052 static void sjcd_invalidate_buffers(void)
1055 for (i = 0; i < SJCD_BUF_SIZ; sjcd_buf_bn[i++] = -1);
1060 * Take care of the different block sizes between cdrom and Linux.
1061 * When Linux gets variable block sizes this will probably go away.
1064 static int current_valid(void)
1067 CURRENT->cmd == READ &&
1068 CURRENT->sector != -1;
1071 static void sjcd_transfer(void)
1073 #if defined( SJCD_TRACE )
1074 printk("SJCD: transfer:\n");
1076 if (current_valid()) {
1077 while (CURRENT->nr_sectors) {
1078 int i, bn = CURRENT->sector / 4;
1080 i < SJCD_BUF_SIZ && sjcd_buf_bn[i] != bn;
1082 if (i < SJCD_BUF_SIZ) {
1084 (i * 4 + (CURRENT->sector & 3)) * 512;
1085 int nr_sectors = 4 - (CURRENT->sector & 3);
1086 if (sjcd_buf_out != i) {
1088 if (sjcd_buf_bn[i] != bn) {
1093 if (nr_sectors > CURRENT->nr_sectors)
1094 nr_sectors = CURRENT->nr_sectors;
1095 #if defined( SJCD_TRACE )
1096 printk("SJCD: copy out\n");
1098 memcpy(CURRENT->buffer, sjcd_buf + offs,
1100 CURRENT->nr_sectors -= nr_sectors;
1101 CURRENT->sector += nr_sectors;
1102 CURRENT->buffer += nr_sectors * 512;
1109 #if defined( SJCD_TRACE )
1110 printk("SJCD: transfer: done\n");
1114 static void sjcd_poll(void)
1116 #if defined( SJCD_GATHER_STAT )
1118 * Update total number of ticks.
1121 statistic.tticks[sjcd_transfer_state]++;
1124 ReSwitch:switch (sjcd_transfer_state) {
1127 #if defined( SJCD_GATHER_STAT )
1128 statistic.idle_ticks++;
1130 #if defined( SJCD_TRACE )
1131 printk("SJCD_S_IDLE\n");
1137 #if defined( SJCD_GATHER_STAT )
1138 statistic.start_ticks++;
1140 sjcd_send_cmd(SCMD_GET_STATUS);
1141 sjcd_transfer_state =
1143 SCMD_MODE_COOKED ? SJCD_S_READ : SJCD_S_MODE;
1144 sjcd_transfer_timeout = 500;
1145 #if defined( SJCD_TRACE )
1146 printk("SJCD_S_START: goto SJCD_S_%s mode\n",
1147 sjcd_transfer_state ==
1148 SJCD_S_READ ? "READ" : "MODE");
1154 if (sjcd_check_status()) {
1156 * Previous command is completed.
1158 if (!sjcd_status_valid
1159 || sjcd_command_failed) {
1160 #if defined( SJCD_TRACE )
1162 ("SJCD_S_MODE: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1164 sjcd_transfer_state = SJCD_S_STOP;
1168 sjcd_mode = 0; /* unknown mode; should not be valid when failed */
1169 sjcd_send_1_cmd(SCMD_SET_MODE,
1171 sjcd_transfer_state = SJCD_S_READ;
1172 sjcd_transfer_timeout = 1000;
1173 #if defined( SJCD_TRACE )
1175 ("SJCD_S_MODE: goto SJCD_S_READ mode\n");
1178 #if defined( SJCD_GATHER_STAT )
1180 statistic.mode_ticks++;
1186 if (sjcd_status_valid ? 1 : sjcd_check_status()) {
1188 * Previous command is completed.
1190 if (!sjcd_status_valid
1191 || sjcd_command_failed) {
1192 #if defined( SJCD_TRACE )
1194 ("SJCD_S_READ: pre-cmd failed: goto to SJCD_S_STOP mode\n");
1196 sjcd_transfer_state = SJCD_S_STOP;
1199 if (!sjcd_media_is_available) {
1200 #if defined( SJCD_TRACE )
1202 ("SJCD_S_READ: no disk: goto to SJCD_S_STOP mode\n");
1204 sjcd_transfer_state = SJCD_S_STOP;
1207 if (sjcd_mode != SCMD_MODE_COOKED) {
1209 * We seem to come from set mode. So discard one byte of result.
1211 if (sjcd_load_response
1212 (&sjcd_mode, 1) != 0) {
1213 #if defined( SJCD_TRACE )
1215 ("SJCD_S_READ: load failed: goto to SJCD_S_STOP mode\n");
1217 sjcd_transfer_state =
1221 if (sjcd_mode != SCMD_MODE_COOKED) {
1222 #if defined( SJCD_TRACE )
1224 ("SJCD_S_READ: mode failed: goto to SJCD_S_STOP mode\n");
1226 sjcd_transfer_state =
1232 if (current_valid()) {
1233 struct sjcd_play_msf msf;
1235 sjcd_next_bn = CURRENT->sector / 4;
1236 hsg2msf(sjcd_next_bn, &msf.start);
1239 msf.end.frame = sjcd_read_count =
1241 #if defined( SJCD_TRACE )
1243 ("SJCD: ---reading msf-address %x:%x:%x %x:%x:%x\n",
1244 msf.start.min, msf.start.sec,
1245 msf.start.frame, msf.end.min,
1246 msf.end.sec, msf.end.frame);
1248 ("sjcd_next_bn:%x buf_in:%x buf_out:%x buf_bn:%x\n",
1249 sjcd_next_bn, sjcd_buf_in,
1251 sjcd_buf_bn[sjcd_buf_in]);
1253 sjcd_send_6_cmd(SCMD_DATA_READ,
1255 sjcd_transfer_state = SJCD_S_DATA;
1256 sjcd_transfer_timeout = 500;
1257 #if defined( SJCD_TRACE )
1259 ("SJCD_S_READ: go to SJCD_S_DATA mode\n");
1262 #if defined( SJCD_TRACE )
1264 ("SJCD_S_READ: nothing to read: go to SJCD_S_STOP mode\n");
1266 sjcd_transfer_state = SJCD_S_STOP;
1270 #if defined( SJCD_GATHER_STAT )
1272 statistic.read_ticks++;
1283 #if defined( SJCD_TRACE )
1284 printk("SJCD_S_DATA: status = 0x%02x\n", stat);
1286 if (SJCD_STATUS_AVAILABLE(stat)) {
1288 * No data is waiting for us in the drive buffer. Status of operation
1289 * completion is available. Read and parse it.
1293 if (!sjcd_status_valid
1294 || sjcd_command_failed) {
1295 #if defined( SJCD_TRACE )
1297 ("SJCD: read block %d failed, maybe audio disk? Giving up\n",
1300 if (current_valid())
1301 end_request(CURRENT, 0);
1302 #if defined( SJCD_TRACE )
1304 ("SJCD_S_DATA: pre-cmd failed: go to SJCD_S_STOP mode\n");
1306 sjcd_transfer_state = SJCD_S_STOP;
1310 if (!sjcd_media_is_available) {
1312 ("SJCD_S_DATA: no disk: go to SJCD_S_STOP mode\n");
1313 sjcd_transfer_state = SJCD_S_STOP;
1317 sjcd_transfer_state = SJCD_S_READ;
1319 } else if (SJCD_DATA_AVAILABLE(stat)) {
1321 * One frame is read into device buffer. We must copy it to our memory.
1322 * Otherwise cdrom hangs up. Check to see if we have something to copy
1325 if (!current_valid()
1326 && sjcd_buf_in == sjcd_buf_out) {
1327 #if defined( SJCD_TRACE )
1329 ("SJCD_S_DATA: nothing to read: go to SJCD_S_STOP mode\n");
1331 (" ... all the date would be discarded\n");
1333 sjcd_transfer_state = SJCD_S_STOP;
1338 * Everything seems to be OK. Just read the frame and recalculate
1341 sjcd_buf_bn[sjcd_buf_in] = -1; /* ??? */
1343 sjcd_buf + 2048 * sjcd_buf_in, 2048);
1344 #if defined( SJCD_TRACE )
1346 ("SJCD_S_DATA: next_bn=%d, buf_in=%d, buf_out=%d, buf_bn=%d\n",
1347 sjcd_next_bn, sjcd_buf_in,
1349 sjcd_buf_bn[sjcd_buf_in]);
1351 sjcd_buf_bn[sjcd_buf_in] = sjcd_next_bn++;
1352 if (sjcd_buf_out == -1)
1353 sjcd_buf_out = sjcd_buf_in;
1354 if (++sjcd_buf_in == SJCD_BUF_SIZ)
1358 * Only one frame is ready at time. So we should turn over to wait for
1359 * another frame. If we need that, of course.
1361 if (--sjcd_read_count == 0) {
1363 * OK, request seems to be precessed. Continue transferring...
1365 if (!sjcd_transfer_is_active) {
1366 while (current_valid()) {
1368 * Continue transferring.
1380 if (current_valid() &&
1381 (CURRENT->sector / 4 <
1383 || CURRENT->sector / 4 >
1386 #if defined( SJCD_TRACE )
1388 ("SJCD_S_DATA: can't read: go to SJCD_S_STOP mode\n");
1390 sjcd_transfer_state =
1396 * Now we should turn around rather than wait for while.
1400 #if defined( SJCD_GATHER_STAT )
1402 statistic.data_ticks++;
1408 sjcd_read_count = 0;
1409 sjcd_send_cmd(SCMD_STOP);
1410 sjcd_transfer_state = SJCD_S_STOPPING;
1411 sjcd_transfer_timeout = 500;
1412 #if defined( SJCD_GATHER_STAT )
1413 statistic.stop_ticks++;
1418 case SJCD_S_STOPPING:{
1421 stat = inb(SJCDPORT(1));
1422 #if defined( SJCD_TRACE )
1423 printk("SJCD_S_STOP: status = 0x%02x\n", stat);
1425 if (SJCD_DATA_AVAILABLE(stat)) {
1427 #if defined( SJCD_TRACE )
1428 printk("SJCD_S_STOP: discard data\n");
1431 * Discard all the data from the pipe. Foolish method.
1434 (void) inb(SJCDPORT(2)));
1435 sjcd_transfer_timeout = 500;
1436 } else if (SJCD_STATUS_AVAILABLE(stat)) {
1438 if (sjcd_status_valid
1439 && sjcd_media_is_changed) {
1440 sjcd_toc_uptodate = 0;
1441 sjcd_invalidate_buffers();
1443 if (current_valid()) {
1444 if (sjcd_status_valid)
1445 sjcd_transfer_state =
1448 sjcd_transfer_state =
1451 sjcd_transfer_state = SJCD_S_IDLE;
1454 #if defined( SJCD_GATHER_STAT )
1456 statistic.stopping_ticks++;
1462 printk("SJCD: poll: invalid state %d\n",
1463 sjcd_transfer_state);
1467 if (--sjcd_transfer_timeout == 0) {
1468 printk("SJCD: timeout in state %d\n", sjcd_transfer_state);
1469 while (current_valid())
1470 end_request(CURRENT, 0);
1471 sjcd_send_cmd(SCMD_STOP);
1472 sjcd_transfer_state = SJCD_S_IDLE;
1477 * Get back in some time. 1 should be replaced with count variable to
1478 * avoid unnecessary testings.
1480 SJCD_SET_TIMER(sjcd_poll, 1);
1483 static void do_sjcd_request(request_queue_t * q)
1485 #if defined( SJCD_TRACE )
1486 printk("SJCD: do_sjcd_request(%ld+%ld)\n",
1487 CURRENT->sector, CURRENT->nr_sectors);
1489 sjcd_transfer_is_active = 1;
1490 while (current_valid()) {
1492 if (CURRENT->nr_sectors == 0)
1493 end_request(CURRENT, 1);
1495 sjcd_buf_out = -1; /* Want to read a block not in buffer */
1496 if (sjcd_transfer_state == SJCD_S_IDLE) {
1497 if (!sjcd_toc_uptodate) {
1498 if (sjcd_update_toc() < 0) {
1500 ("SJCD: transfer: discard\n");
1501 while (current_valid())
1502 end_request(CURRENT, 0);
1506 sjcd_transfer_state = SJCD_S_START;
1507 SJCD_SET_TIMER(sjcd_poll, HZ / 100);
1512 sjcd_transfer_is_active = 0;
1513 #if defined( SJCD_TRACE )
1515 ("sjcd_next_bn:%x sjcd_buf_in:%x sjcd_buf_out:%x sjcd_buf_bn:%x\n",
1516 sjcd_next_bn, sjcd_buf_in, sjcd_buf_out,
1517 sjcd_buf_bn[sjcd_buf_in]);
1518 printk("do_sjcd_request ends\n");
1523 * Open the device special file. Check disk is in.
1525 static int sjcd_open(struct inode *ip, struct file *fp)
1528 * Check the presence of device.
1534 * Only read operations are allowed. Really? (:-)
1539 if (sjcd_open_count == 0) {
1540 int s, sjcd_open_tries;
1541 /* We don't know that, do we? */
1543 sjcd_audio_status = CDROM_AUDIO_NO_STATUS;
1546 sjcd_door_was_open = 0;
1547 sjcd_transfer_state = SJCD_S_IDLE;
1548 sjcd_invalidate_buffers();
1549 sjcd_status_valid = 0;
1552 * Strict status checking.
1554 for (sjcd_open_tries = 4; --sjcd_open_tries;) {
1555 if (!sjcd_status_valid)
1557 if (!sjcd_status_valid) {
1558 #if defined( SJCD_DIAGNOSTIC )
1560 ("SJCD: open: timed out when check status.\n");
1563 } else if (!sjcd_media_is_available) {
1564 #if defined( SJCD_DIAGNOSTIC )
1565 printk("SJCD: open: no disk in drive\n");
1567 if (!sjcd_door_closed) {
1568 sjcd_door_was_open = 1;
1569 #if defined( SJCD_TRACE )
1571 ("SJCD: open: close the tray\n");
1573 s = sjcd_tray_close();
1574 if (s < 0 || !sjcd_status_valid
1575 || sjcd_command_failed) {
1576 #if defined( SJCD_DIAGNOSTIC )
1578 ("SJCD: open: tray close attempt failed\n");
1588 s = sjcd_tray_lock();
1589 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1590 #if defined( SJCD_DIAGNOSTIC )
1591 printk("SJCD: open: tray lock attempt failed\n");
1595 #if defined( SJCD_TRACE )
1596 printk("SJCD: open: done\n");
1608 * On close, we flush all sjcd blocks from the buffer cache.
1610 static int sjcd_release(struct inode *inode, struct file *file)
1614 #if defined( SJCD_TRACE )
1615 printk("SJCD: release\n");
1617 if (--sjcd_open_count == 0) {
1618 sjcd_invalidate_buffers();
1619 s = sjcd_tray_unlock();
1620 if (s < 0 || !sjcd_status_valid || sjcd_command_failed) {
1621 #if defined( SJCD_DIAGNOSTIC )
1623 ("SJCD: release: tray unlock attempt failed.\n");
1626 if (sjcd_door_was_open) {
1627 s = sjcd_tray_open();
1628 if (s < 0 || !sjcd_status_valid
1629 || sjcd_command_failed) {
1630 #if defined( SJCD_DIAGNOSTIC )
1632 ("SJCD: release: tray unload attempt failed.\n");
1641 * A list of file operations allowed for this cdrom.
1643 static struct block_device_operations sjcd_fops = {
1644 .owner = THIS_MODULE,
1646 .release = sjcd_release,
1647 .ioctl = sjcd_ioctl,
1648 .media_changed = sjcd_disk_change,
1652 * Following stuff is intended for initialization of the cdrom. It
1653 * first looks for presence of device. If the device is present, it
1654 * will be reset. Then read the version of the drive and load status.
1655 * The version is two BCD-coded bytes.
1658 unsigned char major, minor;
1661 static struct gendisk *sjcd_disk;
1664 * Test for presence of drive and initialize it. Called at boot time.
1665 * Probe cdrom, find out version and status.
1667 static int __init sjcd_init(void)
1672 "SJCD: Sanyo CDR-H94A cdrom driver version %d.%d.\n",
1673 SJCD_VERSION_MAJOR, SJCD_VERSION_MINOR);
1675 #if defined( SJCD_TRACE )
1676 printk("SJCD: sjcd=0x%x: ", sjcd_base);
1679 if (register_blkdev(MAJOR_NR, "sjcd"))
1682 sjcd_queue = blk_init_queue(do_sjcd_request, &sjcd_lock);
1686 blk_queue_hardsect_size(sjcd_queue, 2048);
1688 sjcd_disk = alloc_disk(1);
1690 printk(KERN_ERR "SJCD: can't allocate disk");
1693 sjcd_disk->major = MAJOR_NR,
1694 sjcd_disk->first_minor = 0,
1695 sjcd_disk->fops = &sjcd_fops,
1696 sprintf(sjcd_disk->disk_name, "sjcd");
1698 if (!request_region(sjcd_base, 4,"sjcd")) {
1700 ("SJCD: Init failed, I/O port (%X) is already in use\n",
1706 * Check for card. Since we are booting now, we can't use standard
1709 printk(KERN_INFO "SJCD: Resetting: ");
1710 sjcd_send_cmd(SCMD_RESET);
1711 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1712 unsigned long timer;
1717 for (timer = jiffies; time_before_eq(jiffies, timer););
1720 (void) sjcd_check_status();
1722 if (i == 0 || sjcd_command_failed) {
1723 printk(" reset failed, no drive found.\n");
1729 * Get and print out cdrom version.
1731 printk(KERN_INFO "SJCD: Getting version: ");
1732 sjcd_send_cmd(SCMD_GET_VERSION);
1733 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1734 unsigned long timer;
1739 for (timer = jiffies; time_before_eq(jiffies, timer););
1742 (void) sjcd_check_status();
1744 if (i == 0 || sjcd_command_failed) {
1745 printk(" get version failed, no drive found.\n");
1749 if (sjcd_load_response(&sjcd_version, sizeof(sjcd_version)) == 0) {
1750 printk(" %1x.%02x\n", (int) sjcd_version.major,
1751 (int) sjcd_version.minor);
1753 printk(" read version failed, no drive found.\n");
1758 * Check and print out the tray state. (if it is needed?).
1760 if (!sjcd_status_valid) {
1761 printk(KERN_INFO "SJCD: Getting status: ");
1762 sjcd_send_cmd(SCMD_GET_STATUS);
1763 for (i = 1000; i > 0 && !sjcd_status_valid; --i) {
1764 unsigned long timer;
1769 for (timer = jiffies;
1770 time_before_eq(jiffies, timer););
1773 (void) sjcd_check_status();
1775 if (i == 0 || sjcd_command_failed) {
1776 printk(" get status failed, no drive found.\n");
1782 printk(KERN_INFO "SJCD: Status: port=0x%x.\n", sjcd_base);
1783 sjcd_disk->queue = sjcd_queue;
1784 add_disk(sjcd_disk);
1789 release_region(sjcd_base, 4);
1791 put_disk(sjcd_disk);
1793 blk_cleanup_queue(sjcd_queue);
1795 if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1796 printk("SJCD: cannot unregister device.\n");
1800 static void __exit sjcd_exit(void)
1802 del_gendisk(sjcd_disk);
1803 put_disk(sjcd_disk);
1804 release_region(sjcd_base, 4);
1805 blk_cleanup_queue(sjcd_queue);
1806 if ((unregister_blkdev(MAJOR_NR, "sjcd") == -EINVAL))
1807 printk("SJCD: cannot unregister device.\n");
1808 printk(KERN_INFO "SJCD: module: removed.\n");
1811 module_init(sjcd_init);
1812 module_exit(sjcd_exit);
1814 MODULE_LICENSE("GPL");
1815 MODULE_ALIAS_BLOCKDEV_MAJOR(SANYO_CDROM_MAJOR);