1 #define GSCD_VERSION "0.4a Oliver Raupach <raupach@nwfs1.rz.fh-hannover.de>"
4 linux/drivers/block/gscd.c - GoldStar R420 CDROM driver
6 Copyright (C) 1995 Oliver Raupach <raupach@nwfs1.rz.fh-hannover.de>
7 based upon pre-works by Eberhard Moenkeberg <emoenke@gwdg.de>
10 For all kind of other information about the GoldStar CDROM
11 and this Linux device driver I installed a WWW-URL:
12 http://linux.rz.fh-hannover.de/~raupach
15 If you are the editor of a Linux CD, you should
16 enable gscd.c within your boot floppy kernel and
17 send me one of your CDs for free.
20 --------------------------------------------------------------------
21 This program is free software; you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation; either version 2, or (at your option)
26 This program is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with this program; if not, write to the Free Software
33 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 --------------------------------------------------------------------
37 9 November 1999 -- Make kernel-parameter implementation work with 2.3.x
38 Removed init_module & cleanup_module in favor of
39 module_init & module_exit.
40 Torben Mathiasen <tmm@image.dk>
44 /* These settings are for various debug-level. Leave they untouched ... */
46 #define NO_IOCTL_DEBUG
47 #define NO_MODULE_DEBUG
48 #define NO_FUTURE_WORK
49 /*------------------------*/
51 #include <linux/module.h>
53 #include <linux/slab.h>
54 #include <linux/errno.h>
55 #include <linux/signal.h>
56 #include <linux/timer.h>
59 #include <linux/kernel.h>
60 #include <linux/cdrom.h>
61 #include <linux/ioport.h>
62 #include <linux/major.h>
63 #include <linux/string.h>
64 #include <linux/init.h>
66 #include <asm/system.h>
68 #include <asm/uaccess.h>
70 #define MAJOR_NR GOLDSTAR_CDROM_MAJOR
71 #include <linux/blkdev.h>
74 static int gscdPresent
= 0;
76 static unsigned char gscd_buf
[2048]; /* buffer for block size conversion */
77 static int gscd_bn
= -1;
78 static short gscd_port
= GSCD_BASE_ADDR
;
79 module_param_named(gscd
, gscd_port
, short, 0);
81 /* Kommt spaeter vielleicht noch mal dran ...
82 * static DECLARE_WAIT_QUEUE_HEAD(gscd_waitq);
85 static void gscd_read_cmd(struct request
*req
);
86 static void gscd_hsg2msf(long hsg
, struct msf
*msf
);
87 static void gscd_bin2bcd(unsigned char *p
);
89 /* Schnittstellen zum Kern/FS */
91 static void __do_gscd_request(unsigned long dummy
);
92 static int gscd_ioctl(struct inode
*, struct file
*, unsigned int,
94 static int gscd_open(struct inode
*, struct file
*);
95 static int gscd_release(struct inode
*, struct file
*);
96 static int check_gscd_med_chg(struct gendisk
*disk
);
98 /* GoldStar Funktionen */
100 static void cmd_out(int, char *, char *, int);
101 static void cmd_status(void);
102 static void init_cd_drive(int);
104 static int get_status(void);
105 static void clear_Audio(void);
106 static void cc_invalidate(void);
108 /* some things for the next version */
110 static void update_state(void);
111 static long gscd_msf2hsg(struct msf
*mp
);
112 static int gscd_bcd2bin(unsigned char bcd
);
116 /* lo-level cmd-Funktionen */
118 static void cmd_info_in(char *, int);
119 static void cmd_end(void);
120 static void cmd_read_b(char *, int, int);
121 static void cmd_read_w(char *, int, int);
122 static int cmd_unit_alive(void);
123 static void cmd_write_cmd(char *);
126 /* GoldStar Variablen */
128 static int curr_drv_state
;
129 static int drv_states
[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
131 static int disk_state
;
135 static unsigned char drv_num_read
;
136 static unsigned char f_dsk_valid
;
137 static unsigned char current_drive
;
138 static unsigned char f_drv_ok
;
141 static char f_AudioPlay
;
142 static char f_AudioPause
;
143 static int AudioStart_m
;
144 static int AudioStart_f
;
145 static int AudioEnd_m
;
146 static int AudioEnd_f
;
148 static DEFINE_TIMER(gscd_timer
, NULL
, 0, 0);
149 static DEFINE_SPINLOCK(gscd_lock
);
150 static struct request_queue
*gscd_queue
;
152 static struct block_device_operations gscd_fops
= {
153 .owner
= THIS_MODULE
,
155 .release
= gscd_release
,
157 .media_changed
= check_gscd_med_chg
,
161 * Checking if the media has been changed
162 * (not yet implemented)
164 static int check_gscd_med_chg(struct gendisk
*disk
)
167 printk("gscd: check_med_change\n");
174 /* Using new interface for kernel-parameters */
176 static int __init
gscd_setup(char *str
)
179 (void) get_options(str
, ARRAY_SIZE(ints
), ints
);
187 __setup("gscd=", gscd_setup
);
191 static int gscd_ioctl(struct inode
*ip
, struct file
*fp
, unsigned int cmd
,
194 unsigned char to_do
[10];
199 case CDROMSTART
: /* Spin up the drive */
200 /* Don't think we can do this. Even if we could,
201 * I think the drive times out and stops after a while
202 * anyway. For now, ignore it.
206 case CDROMRESUME
: /* keine Ahnung was das ist */
212 to_do
[0] = CMD_TRAY_CTL
;
213 cmd_out(TYPE_INFO
, (char *) &to_do
, (char *) &dummy
, 0);
225 * Take care of the different block sizes between cdrom and Linux.
226 * When Linux gets variable block sizes this will probably go away.
229 static void gscd_transfer(struct request
*req
)
231 while (req
->nr_sectors
> 0 && gscd_bn
== req
->sector
/ 4) {
232 long offs
= (req
->sector
& 3) * 512;
233 memcpy(req
->buffer
, gscd_buf
+ offs
, 512);
242 * I/O request routine called from Linux kernel.
245 static void do_gscd_request(request_queue_t
* q
)
247 __do_gscd_request(0);
250 static void __do_gscd_request(unsigned long dummy
)
257 req
= elv_next_request(gscd_queue
);
262 nsect
= req
->nr_sectors
;
264 if (req
->sector
== -1)
267 if (req
->cmd
!= READ
) {
268 printk("GSCD: bad cmd %u\n", rq_data_dir(req
));
275 /* if we satisfied the request from the buffer, we're done. */
277 if (req
->nr_sectors
== 0) {
282 printk("GSCD: block %d, nsect %d\n", block
, nsect
);
292 * Check the result of the set-mode command. On success, send the
296 static void gscd_read_cmd(struct request
*req
)
299 struct gscd_Play_msf gscdcmd
;
300 char cmd
[] = { CMD_READ
, 0x80, 0, 0, 0, 0, 1 }; /* cmd mode M-S-F secth sectl */
303 if (disk_state
& (ST_NO_DISK
| ST_DOOR_OPEN
)) {
304 printk("GSCD: no disk or door open\n");
307 if (disk_state
& ST_INVALID
) {
308 printk("GSCD: disk invalid\n");
311 gscd_bn
= -1; /* purge our buffer */
312 block
= req
->sector
/ 4;
313 gscd_hsg2msf(block
, &gscdcmd
.start
); /* cvt to msf format */
315 cmd
[2] = gscdcmd
.start
.min
;
316 cmd
[3] = gscdcmd
.start
.sec
;
317 cmd
[4] = gscdcmd
.start
.frame
;
320 printk("GSCD: read msf %d:%d:%d\n", cmd
[2], cmd
[3],
323 cmd_out(TYPE_DATA
, (char *) &cmd
,
324 (char *) &gscd_buf
[0], 1);
326 gscd_bn
= req
->sector
/ 4;
331 SET_TIMER(__do_gscd_request
, 1);
336 * Open the device special file. Check that a disk is in.
339 static int gscd_open(struct inode
*ip
, struct file
*fp
)
344 printk("GSCD: open\n");
347 if (gscdPresent
== 0)
348 return -ENXIO
; /* no hardware */
351 st
= disk_state
& (ST_NO_DISK
| ST_DOOR_OPEN
);
353 printk("GSCD: no disk or door open\n");
357 /* if (updateToc() < 0)
366 * On close, we flush all gscd blocks from the buffer cache.
369 static int gscd_release(struct inode
*inode
, struct file
*file
)
373 printk("GSCD: release\n");
382 static int get_status(void)
387 status
= disk_state
& (ST_x08
| ST_x04
| ST_INVALID
| ST_x01
);
389 if (status
== (ST_x08
| ST_x04
| ST_INVALID
| ST_x01
)) {
398 static void cc_invalidate(void)
402 current_drive
= 0xFF;
409 static void clear_Audio(void)
425 static int wait_drv_ready(void)
430 found
= inb(GSCDPORT(0));
432 read
= inb(GSCDPORT(0));
434 } while (read
!= found
);
437 printk("Wait for: %d\n", read
);
443 static void cc_Ident(char *respons
)
445 char to_do
[] = { CMD_IDENT
, 0, 0 };
447 cmd_out(TYPE_INFO
, (char *) &to_do
, (char *) respons
, (int) 0x1E);
451 static void cc_SetSpeed(void)
453 char to_do
[] = { CMD_SETSPEED
, 0, 0 };
457 to_do
[1] = speed
& 0x0F;
458 cmd_out(TYPE_INFO
, (char *) &to_do
, (char *) &dummy
, 0);
462 static void cc_Reset(void)
464 char to_do
[] = { CMD_RESET
, 0 };
467 cmd_out(TYPE_INFO
, (char *) &to_do
, (char *) &dummy
, 0);
470 static void cmd_status(void)
472 char to_do
[] = { CMD_STATUS
, 0 };
475 cmd_out(TYPE_INFO
, (char *) &to_do
, (char *) &dummy
, 0);
478 printk("GSCD: Status: %d\n", disk_state
);
483 static void cmd_out(int cmd_type
, char *cmd
, char *respo_buf
, int respo_count
)
488 result
= wait_drv_ready();
489 if (result
!= drv_mode
) {
490 unsigned long test_loops
= 0xFFFF;
493 outb(curr_drv_state
, GSCDPORT(0));
497 result
= wait_drv_ready();
499 } while ((result
!= drv_mode
) && (test_loops
> 0));
501 if (result
!= drv_mode
) {
502 disk_state
= ST_x08
| ST_x04
| ST_INVALID
;
507 for (i
= 1, dummy
= 1; i
< 0xFFFF; i
++) {
515 if (cmd_unit_alive() != 0x08) {
517 /* game over for this unit */
518 disk_state
= ST_x08
| ST_x04
| ST_INVALID
;
526 if (drv_mode
== 0x09) {
528 printk("GSCD: magic ...\n");
529 outb(result
, GSCDPORT(2));
532 /* write the command to the drive */
537 result
= wait_drv_ready();
538 if (result
!= drv_mode
) {
540 if (result
== 0x04) { /* Mode 4 */
545 disk_state
= inb(GSCDPORT(2));
548 result
= wait_drv_ready();
549 } while (result
!= drv_mode
);
553 if (result
== 0x06) { /* Mode 6 */
559 if (cmd_type
== TYPE_DATA
) {
563 /* read the data to the buffer (word) */
565 /* (*(cmd+1))?(CD_FRAMESIZE/2):(CD_FRAMESIZE_RAW/2) */
573 /* read the data to the buffer (byte) */
575 /* (*(cmd+1))?(CD_FRAMESIZE):(CD_FRAMESIZE_RAW) */
583 /* read the info to the buffer */
584 cmd_info_in(respo_buf
,
594 disk_state
= ST_x08
| ST_x04
| ST_INVALID
;
606 static void cmd_write_cmd(char *pstr
)
615 /* calculate the number of parameter */
619 for (i
= 0; i
< j
; i
++) {
620 outb(*pstr
, GSCDPORT(2));
626 static int cmd_unit_alive(void)
629 unsigned long max_test_loops
;
637 outb(curr_drv_state
, GSCDPORT(0));
638 max_test_loops
= 0xFFFF;
641 result
= wait_drv_ready();
643 } while ((result
!= 0x08) && (max_test_loops
> 0));
649 static void cmd_info_in(char *pb
, int count
)
662 read
= inb(GSCDPORT(2));
671 result
= wait_drv_ready();
672 } while (result
== 0x0E);
673 } while (result
== 6);
680 static void cmd_read_b(char *pb
, int count
, int size
)
694 result
= wait_drv_ready();
695 } while (result
!= 6 || result
== 0x0E);
705 for (i
= 0; i
< size
; i
++) {
706 *pb
= inb(GSCDPORT(2));
717 static void cmd_end(void)
728 result
= wait_drv_ready();
729 if (result
== drv_mode
) {
732 } while (result
!= 4);
739 disk_state
= inb(GSCDPORT(2));
742 result
= wait_drv_ready();
743 } while (result
!= drv_mode
);
749 static void cmd_read_w(char *pb
, int count
, int size
)
762 result
= wait_drv_ready();
763 } while (result
!= 6 || result
== 0x0E);
770 for (i
= 0; i
< size
; i
++) {
771 /* na, hier muss ich noch mal drueber nachdenken */
772 *pb
= inw(GSCDPORT(2));
782 static int __init
find_drives(void)
790 pdrv
= (int *) &drv_states
;
791 curr_drv_state
= 0xFE;
795 for (i
= 0; i
< 8; i
++) {
798 disk_state
&= ST_x08
| ST_x04
| ST_INVALID
| ST_x01
;
799 if (disk_state
!= (ST_x08
| ST_x04
| ST_INVALID
)) {
801 *pdrv
= curr_drv_state
;
802 init_cd_drive(drvnum
);
813 /* curr_drv_state<<1; <-- das geht irgendwie nicht */
814 /* muss heissen: curr_drv_state <<= 1; (ist ja Wert-Zuweisung) */
818 printk("DriveState: %d\n", curr_drv_state
);
826 static void __init
init_cd_drive(int num
)
831 printk("GSCD: init unit %d\n", num
);
832 cc_Ident((char *) &resp
);
834 printk("GSCD: identification: ");
835 for (i
= 0; i
< 0x1E; i
++) {
836 printk("%c", resp
[i
]);
846 static void update_state(void)
851 if ((disk_state
& (ST_x08
| ST_x04
| ST_INVALID
| ST_x01
)) == 0) {
852 if (disk_state
== (ST_x08
| ST_x04
| ST_INVALID
)) {
856 if ((disk_state
& (ST_x08
| ST_x04
| ST_INVALID
| ST_x01
))
865 if (disk_state
& ST_PLAYING
) {
877 static struct gendisk
*gscd_disk
;
879 static void __exit
gscd_exit(void)
883 del_gendisk(gscd_disk
);
885 if ((unregister_blkdev(MAJOR_NR
, "gscd") == -EINVAL
)) {
886 printk("What's that: can't unregister GoldStar-module\n");
889 blk_cleanup_queue(gscd_queue
);
890 release_region(gscd_port
, GSCD_IO_EXTENT
);
891 printk(KERN_INFO
"GoldStar-module released.\n");
894 /* This is the common initialisation for the GoldStar drive. */
895 /* It is called at boot time AND for module init. */
896 static int __init
gscd_init(void)
902 printk(KERN_INFO
"GSCD: version %s\n", GSCD_VERSION
);
904 "GSCD: Trying to detect a Goldstar R420 CD-ROM drive at 0x%X.\n",
907 if (!request_region(gscd_port
, GSCD_IO_EXTENT
, "gscd")) {
908 printk(KERN_WARNING
"GSCD: Init failed, I/O port (%X) already"
909 " in use.\n", gscd_port
);
915 result
= wait_drv_ready();
916 if (result
== 0x09) {
917 printk(KERN_WARNING
"GSCD: DMA kann ich noch nicht!\n");
922 if (result
== 0x0b) {
926 printk(KERN_WARNING
"GSCD: GoldStar CD-ROM Drive is"
933 if ((result
!= 0x0b) && (result
!= 0x09)) {
934 printk(KERN_WARNING
"GSCD: GoldStar Interface Adapter does not "
935 "exist or H/W error\n");
940 /* reset all drives */
942 while (drv_states
[i
] != 0) {
943 curr_drv_state
= drv_states
[i
];
944 printk(KERN_INFO
"GSCD: Reset unit %d ... ", i
);
950 gscd_disk
= alloc_disk(1);
953 gscd_disk
->major
= MAJOR_NR
;
954 gscd_disk
->first_minor
= 0;
955 gscd_disk
->fops
= &gscd_fops
;
956 sprintf(gscd_disk
->disk_name
, "gscd");
958 if (register_blkdev(MAJOR_NR
, "gscd")) {
963 gscd_queue
= blk_init_queue(do_gscd_request
, &gscd_lock
);
972 gscd_disk
->queue
= gscd_queue
;
975 printk(KERN_INFO
"GSCD: GoldStar CD-ROM Drive found.\n");
979 unregister_blkdev(MAJOR_NR
, "gscd");
983 release_region(gscd_port
, GSCD_IO_EXTENT
);
987 static void gscd_hsg2msf(long hsg
, struct msf
*msf
)
989 hsg
+= CD_MSF_OFFSET
;
990 msf
->min
= hsg
/ (CD_FRAMES
* CD_SECS
);
991 hsg
%= CD_FRAMES
* CD_SECS
;
992 msf
->sec
= hsg
/ CD_FRAMES
;
993 msf
->frame
= hsg
% CD_FRAMES
;
995 gscd_bin2bcd(&msf
->min
); /* convert to BCD */
996 gscd_bin2bcd(&msf
->sec
);
997 gscd_bin2bcd(&msf
->frame
);
1001 static void gscd_bin2bcd(unsigned char *p
)
1012 static long gscd_msf2hsg(struct msf
*mp
)
1014 return gscd_bcd2bin(mp
->frame
)
1015 + gscd_bcd2bin(mp
->sec
) * CD_FRAMES
1016 + gscd_bcd2bin(mp
->min
) * CD_FRAMES
* CD_SECS
- CD_MSF_OFFSET
;
1019 static int gscd_bcd2bin(unsigned char bcd
)
1021 return (bcd
>> 4) * 10 + (bcd
& 0xF);
1025 MODULE_AUTHOR("Oliver Raupach <raupach@nwfs1.rz.fh-hannover.de>");
1026 MODULE_LICENSE("GPL");
1027 module_init(gscd_init
);
1028 module_exit(gscd_exit
);
1029 MODULE_ALIAS_BLOCKDEV_MAJOR(GOLDSTAR_CDROM_MAJOR
);