4 * Low level driver for Ensoniq SoundScape
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
15 * Sergey Smitienko : ensoniq p'n'p support
16 * Christoph Hellwig : adapted to module_init/module_exit
17 * Bartlomiej Zolnierkiewicz : added __init to attach_sscape()
18 * Chris Rankin : Specify that this module owns the coprocessor
19 * Arnaldo C. de Melo : added missing restore_flags in sscape_pnp_upload_file
22 #include <linux/init.h>
23 #include <linux/module.h>
25 #include "sound_config.h"
26 #include "sound_firmware.h"
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/signal.h>
31 #include <linux/fcntl.h>
32 #include <linux/ctype.h>
33 #include <linux/stddef.h>
34 #include <linux/kmod.h>
37 #include <linux/wait.h>
38 #include <linux/slab.h>
39 #include <linux/ioport.h>
40 #include <linux/delay.h>
41 #include <linux/proc_fs.h>
42 #include <linux/wrapper.h>
43 #include <linux/spinlock.h>
66 #define GA_INTSTAT_REG 0
67 #define GA_INTENA_REG 1
70 #define GA_INTCFG_REG 4
71 #define GA_DMACFG_REG 5
72 #define GA_CDCFG_REG 6
73 #define GA_SMCFGA_REG 7
74 #define GA_SMCFGB_REG 8
75 #define GA_HMCTL_REG 9
78 * DMA channel identifiers (A and B)
81 #define SSCAPE_DMA_A 0
82 #define SSCAPE_DMA_B 1
84 #define PORT(name) (devc->base+name)
87 * Host commands recognized by the OBP microcode
90 #define CMD_GEN_HOST_ACK 0x80
91 #define CMD_GEN_MPU_ACK 0x81
92 #define CMD_GET_BOARD_TYPE 0x82
93 #define CMD_SET_CONTROL 0x88 /* Old firmware only */
94 #define CMD_GET_CONTROL 0x89 /* Old firmware only */
95 #define CTL_MASTER_VOL 0
96 #define CTL_MIC_MODE 2
97 #define CTL_SYNTH_VOL 4
98 #define CTL_WAVE_VOL 7
99 #define CMD_SET_EXTMIDI 0x8a
100 #define CMD_GET_EXTMIDI 0x8b
101 #define CMD_SET_MT32 0x8c
102 #define CMD_GET_MT32 0x8d
109 typedef struct sscape_info
113 int codec
, codec_irq
; /* required to setup pnp cards*/
117 unsigned long raw_buf_phys
;
118 int buffsize
; /* -------------------------- */
120 int ok
; /* Properly detected */
129 static struct sscape_info adev_info
= {
133 static struct sscape_info
*devc
= &adev_info
;
134 static int sscape_mididev
= -1;
136 /* Some older cards have assigned interrupt bits differently than new ones */
137 static char valid_interrupts_old
[] = {
141 static char valid_interrupts_new
[] = {
145 static char *valid_interrupts
= valid_interrupts_new
;
148 * See the bottom of the driver. This can be set by spea =0/1.
152 static char old_hardware
= 1;
154 static char old_hardware
= 0;
157 static void sleep(unsigned howlong
)
159 current
->state
= TASK_INTERRUPTIBLE
;
160 schedule_timeout(howlong
);
163 static unsigned char sscape_read(struct sscape_info
*devc
, int reg
)
168 spin_lock_irqsave(&devc
->lock
,flags
);
169 outb(reg
, PORT(ODIE_ADDR
));
170 val
= inb(PORT(ODIE_DATA
));
171 spin_unlock_irqrestore(&devc
->lock
,flags
);
175 static void sscape_write(struct sscape_info
*devc
, int reg
, int data
)
179 spin_lock_irqsave(&devc
->lock
,flags
);
180 outb(reg
, PORT(ODIE_ADDR
));
181 outb(data
, PORT(ODIE_DATA
));
182 spin_unlock_irqrestore(&devc
->lock
,flags
);
185 static unsigned char sscape_pnp_read_codec(sscape_info
* devc
, unsigned char reg
)
190 spin_lock_irqsave(&devc
->lock
,flags
);
191 outb( reg
, devc
-> codec
);
192 res
= inb (devc
-> codec
+ 1);
193 spin_unlock_irqrestore(&devc
->lock
,flags
);
198 static void sscape_pnp_write_codec(sscape_info
* devc
, unsigned char reg
, unsigned char data
)
202 spin_lock_irqsave(&devc
->lock
,flags
);
203 outb( reg
, devc
-> codec
);
204 outb( data
, devc
-> codec
+ 1);
205 spin_unlock_irqrestore(&devc
->lock
,flags
);
208 static void host_open(struct sscape_info
*devc
)
210 outb((0x00), PORT(HOST_CTRL
)); /* Put the board to the host mode */
213 static void host_close(struct sscape_info
*devc
)
215 outb((0x03), PORT(HOST_CTRL
)); /* Put the board to the MIDI mode */
218 static int host_write(struct sscape_info
*devc
, unsigned char *data
, int count
)
223 spin_lock_irqsave(&devc
->lock
,flags
);
225 * Send the command and data bytes
228 for (i
= 0; i
< count
; i
++)
230 for (timeout_val
= 10000; timeout_val
> 0; timeout_val
--)
231 if (inb(PORT(HOST_CTRL
)) & TX_READY
)
234 if (timeout_val
<= 0)
236 spin_unlock_irqrestore(&devc
->lock
,flags
);
239 outb(data
[i
], PORT(HOST_DATA
));
241 spin_unlock_irqrestore(&devc
->lock
,flags
);
245 static int host_read(struct sscape_info
*devc
)
251 spin_lock_irqsave(&devc
->lock
,flags
);
256 for (timeout_val
= 10000; timeout_val
> 0; timeout_val
--)
257 if (inb(PORT(HOST_CTRL
)) & RX_READY
)
260 if (timeout_val
<= 0)
262 spin_unlock_irqrestore(&devc
->lock
,flags
);
265 data
= inb(PORT(HOST_DATA
));
266 spin_unlock_irqrestore(&devc
->lock
,flags
);
271 static int host_command1(struct sscape_info
*devc
, int cmd
)
273 unsigned char buf
[10];
274 buf
[0] = (unsigned char) (cmd
& 0xff);
275 return host_write(devc
, buf
, 1);
280 static int host_command2(struct sscape_info
*devc
, int cmd
, int parm1
)
282 unsigned char buf
[10];
284 buf
[0] = (unsigned char) (cmd
& 0xff);
285 buf
[1] = (unsigned char) (parm1
& 0xff);
287 return host_write(devc
, buf
, 2);
290 static int host_command3(struct sscape_info
*devc
, int cmd
, int parm1
, int parm2
)
292 unsigned char buf
[10];
294 buf
[0] = (unsigned char) (cmd
& 0xff);
295 buf
[1] = (unsigned char) (parm1
& 0xff);
296 buf
[2] = (unsigned char) (parm2
& 0xff);
297 return host_write(devc
, buf
, 3);
300 static void set_mt32(struct sscape_info
*devc
, int value
)
303 host_command2(devc
, CMD_SET_MT32
, value
? 1 : 0);
304 if (host_read(devc
) != CMD_ACK
)
306 /* printk( "SNDSCAPE: Setting MT32 mode failed\n"); */
311 static void set_control(struct sscape_info
*devc
, int ctrl
, int value
)
314 host_command3(devc
, CMD_SET_CONTROL
, ctrl
, value
);
315 if (host_read(devc
) != CMD_ACK
)
317 /* printk( "SNDSCAPE: Setting control (%d) failed\n", ctrl); */
322 static void do_dma(struct sscape_info
*devc
, int dma_chan
, unsigned long buf
, int blk_size
, int mode
)
326 if (dma_chan
!= SSCAPE_DMA_A
)
328 printk(KERN_WARNING
"soundscape: Tried to use DMA channel != A. Why?\n");
331 audio_devs
[devc
->codec_audiodev
]->flags
&= ~DMA_AUTOMODE
;
332 DMAbuf_start_dma(devc
->codec_audiodev
, buf
, blk_size
, mode
);
333 audio_devs
[devc
->codec_audiodev
]->flags
|= DMA_AUTOMODE
;
335 temp
= devc
->dma
<< 4; /* Setup DMA channel select bits */
337 temp
|= 0x80; /* 8 bit DMA channel */
339 temp
|= 1; /* Trigger DMA */
340 sscape_write(devc
, GA_DMAA_REG
, temp
);
341 temp
&= 0xfe; /* Clear DMA trigger */
342 sscape_write(devc
, GA_DMAA_REG
, temp
);
345 static int verify_mpu(struct sscape_info
*devc
)
348 * The SoundScape board could be in three modes (MPU, 8250 and host).
349 * If the card is not in the MPU mode, enabling the MPU driver will
350 * cause infinite loop (the driver believes that there is always some
351 * received data in the buffer.
353 * Detect this by looking if there are more than 10 received MIDI bytes
354 * (0x00) in the buffer.
359 for (i
= 0; i
< 10; i
++)
361 if (inb(devc
->base
+ HOST_CTRL
) & 0x80)
364 if (inb(devc
->base
) != 0x00)
367 printk(KERN_WARNING
"SoundScape: The device is not in the MPU-401 mode\n");
371 static int sscape_coproc_open(void *dev_info
, int sub_device
)
373 if (sub_device
== COPR_MIDI
)
376 if (!verify_mpu(devc
))
382 static void sscape_coproc_close(void *dev_info
, int sub_device
)
384 struct sscape_info
*devc
= dev_info
;
387 spin_lock_irqsave(&devc
->lock
,flags
);
388 if (devc
->dma_allocated
)
390 sscape_write(devc
, GA_DMAA_REG
, 0x20); /* DMA channel disabled */
391 devc
->dma_allocated
= 0;
393 spin_unlock_irqrestore(&devc
->lock
,flags
);
397 static void sscape_coproc_reset(void *dev_info
)
401 static int sscape_download_boot(struct sscape_info
*devc
, unsigned char *block
, int size
, int flag
)
405 volatile int done
, timeout_val
;
406 static unsigned char codec_dma_bits
= 0;
408 if (flag
& CPF_FIRST
)
411 * First block. Have to allocate DMA and to reset the board
415 spin_lock_irqsave(&devc
->lock
,flags
);
416 codec_dma_bits
= sscape_read(devc
, GA_CDCFG_REG
);
418 if (devc
->dma_allocated
== 0)
419 devc
->dma_allocated
= 1;
421 spin_unlock_irqrestore(&devc
->lock
,flags
);
423 sscape_write(devc
, GA_HMCTL_REG
,
424 (temp
= sscape_read(devc
, GA_HMCTL_REG
)) & 0x3f); /*Reset */
426 for (timeout_val
= 10000; timeout_val
> 0; timeout_val
--)
427 sscape_read(devc
, GA_HMCTL_REG
); /* Delay */
429 /* Take board out of reset */
430 sscape_write(devc
, GA_HMCTL_REG
,
431 (temp
= sscape_read(devc
, GA_HMCTL_REG
)) | 0x80);
434 * Transfer one code block using DMA
436 if (audio_devs
[devc
->codec_audiodev
]->dmap_out
->raw_buf
== NULL
)
438 printk(KERN_WARNING
"soundscape: DMA buffer not available\n");
441 memcpy(audio_devs
[devc
->codec_audiodev
]->dmap_out
->raw_buf
, block
, size
);
443 spin_lock_irqsave(&devc
->lock
,flags
);
445 /******** INTERRUPTS DISABLED NOW ********/
447 do_dma(devc
, SSCAPE_DMA_A
,
448 audio_devs
[devc
->codec_audiodev
]->dmap_out
->raw_buf_phys
,
449 size
, DMA_MODE_WRITE
);
452 * Wait until transfer completes.
457 while (!done
&& timeout_val
-- > 0)
463 clear_dma_ff(devc
->dma
);
464 if ((resid
= get_dma_residue(devc
->dma
)) == 0)
468 spin_unlock_irqrestore(&devc
->lock
,flags
);
475 * Take the board out of reset
477 outb((0x00), PORT(HOST_CTRL
));
478 outb((0x00), PORT(MIDI_CTRL
));
480 temp
= sscape_read(devc
, GA_HMCTL_REG
);
482 sscape_write(devc
, GA_HMCTL_REG
, temp
); /* Kickstart the board */
485 * Wait until the ODB wakes up
487 spin_lock_irqsave(&devc
->lock
,flags
);
489 timeout_val
= 5 * HZ
;
490 while (!done
&& timeout_val
-- > 0)
495 x
= inb(PORT(HOST_DATA
));
496 if (x
== 0xff || x
== 0xfe) /* OBP startup acknowledge */
498 DDB(printk("Soundscape: Acknowledge = %x\n", x
));
502 sscape_write(devc
, GA_CDCFG_REG
, codec_dma_bits
);
504 spin_unlock_irqrestore(&devc
->lock
,flags
);
507 printk(KERN_ERR
"soundscape: The OBP didn't respond after code download\n");
510 spin_lock_irqsave(&devc
->lock
,flags
);
512 timeout_val
= 5 * HZ
;
513 while (!done
&& timeout_val
-- > 0)
516 if (inb(PORT(HOST_DATA
)) == 0xfe) /* Host startup acknowledge */
519 spin_unlock_irqrestore(&devc
->lock
,flags
);
522 printk(KERN_ERR
"soundscape: OBP Initialization failed.\n");
525 printk(KERN_INFO
"SoundScape board initialized OK\n");
526 set_control(devc
, CTL_MASTER_VOL
, 100);
527 set_control(devc
, CTL_SYNTH_VOL
, 100);
531 * Temporary debugging aid. Print contents of the registers after
532 * downloading the code.
537 for (i
= 0; i
< 13; i
++)
538 printk("I%d = %02x (new value)\n", i
, sscape_read(devc
, i
));
546 static int download_boot_block(void *dev_info
, copr_buffer
* buf
)
548 if (buf
->len
<= 0 || buf
->len
> sizeof(buf
->data
))
551 if (!sscape_download_boot(devc
, buf
->data
, buf
->len
, buf
->flags
))
553 printk(KERN_ERR
"soundscape: Unable to load microcode block to the OBP.\n");
559 static int sscape_coproc_ioctl(void *dev_info
, unsigned int cmd
, caddr_t arg
, int local
)
566 case SNDCTL_COPR_RESET
:
567 sscape_coproc_reset(dev_info
);
570 case SNDCTL_COPR_LOAD
:
571 buf
= (copr_buffer
*) vmalloc(sizeof(copr_buffer
));
574 if (copy_from_user(buf
, arg
, sizeof(copr_buffer
)))
579 err
= download_boot_block(dev_info
, buf
);
588 static coproc_operations sscape_coproc_operations
=
599 static int sscape_detected
= 0;
600 static int sscape_is_pnp
= 0;
602 void __init
attach_sscape(struct address_info
*hw_config
)
606 * Config register values for Spea/V7 Media FX and Ensoniq S-2000.
607 * These values are card
608 * dependent. If you have another SoundScape based card, you have to
609 * find the correct values. Do the following:
610 * - Compile this driver with SSCAPE_DEBUG1 defined.
611 * - Shut down and power off your machine.
612 * - Boot with DOS so that the SSINIT.EXE program is run.
613 * - Warm boot to {Linux|SYSV|BSD} and write down the lines displayed
614 * when detecting the SoundScape.
615 * - Modify the following list to use the values printed during boot.
616 * Undefine the SSCAPE_DEBUG1
618 #define SSCAPE_REGS { \
620 /* I1 */ 0xf0, /* Note! Ignored. Set always to 0xf0 */ \
621 /* I2 */ 0x20, /* Note! Ignored. Set always to 0x20 */ \
622 /* I3 */ 0x20, /* Note! Ignored. Set always to 0x20 */ \
623 /* I4 */ 0xf5, /* Ignored */ \
626 /* I7 */ 0x2e, /* I7 MEM config A. Likely to vary between models */ \
627 /* I8 */ 0x00, /* I8 MEM config B. Likely to vary between models */ \
628 /* I9 */ 0x40 /* Ignored */ \
633 static unsigned char regs
[10] = SSCAPE_REGS
;
635 int i
, irq_bits
= 0xff;
637 if (sscape_detected
!= hw_config
->io_base
)
640 request_region(devc
->base
+ 2, 6, "SoundScape");
643 valid_interrupts
= valid_interrupts_old
;
644 conf_printf("Ensoniq SoundScape (old)", hw_config
);
647 conf_printf("Ensoniq SoundScape", hw_config
);
649 for (i
= 0; i
< sizeof(valid_interrupts
); i
++)
651 if (hw_config
->irq
== valid_interrupts
[i
])
657 if (hw_config
->irq
> 15 || (regs
[4] = irq_bits
== 0xff))
659 printk(KERN_ERR
"Invalid IRQ%d\n", hw_config
->irq
);
663 if (!sscape_is_pnp
) {
665 spin_lock_irqsave(&devc
->lock
,flags
);
666 for (i
= 1; i
< 10; i
++)
670 case 1: /* Host interrupt enable */
671 sscape_write(devc
, i
, 0xf0); /* All interrupts enabled */
674 case 2: /* DMA A status/trigger register */
675 case 3: /* DMA B status/trigger register */
676 sscape_write(devc
, i
, 0x20); /* DMA channel disabled */
679 case 4: /* Host interrupt config reg */
680 sscape_write(devc
, i
, 0xf0 | (irq_bits
<< 2) | irq_bits
);
683 case 5: /* Don't destroy CD-ROM DMA config bits (0xc0) */
684 sscape_write(devc
, i
, (regs
[i
] & 0x3f) | (sscape_read(devc
, i
) & 0xc0));
687 case 6: /* CD-ROM config (WSS codec actually) */
688 sscape_write(devc
, i
, regs
[i
]);
691 case 9: /* Master control reg. Don't modify CR-ROM bits. Disable SB emul */
692 sscape_write(devc
, i
, (sscape_read(devc
, i
) & 0xf0) | 0x08);
696 sscape_write(devc
, i
, regs
[i
]);
699 spin_unlock_irqrestore(&devc
->lock
,flags
);
703 * Temporary debugging aid. Print contents of the registers after
709 for (i
= 0; i
< 13; i
++)
710 printk("I%d = %02x (new value)\n", i
, sscape_read(devc
, i
));
714 if (probe_mpu401(hw_config
))
715 hw_config
->always_detect
= 1;
716 hw_config
->name
= "SoundScape";
718 hw_config
->irq
*= -1; /* Negative value signals IRQ sharing */
719 attach_mpu401(hw_config
, THIS_MODULE
);
720 hw_config
->irq
*= -1; /* Restore it */
722 if (hw_config
->slots
[1] != -1) /* The MPU driver installed itself */
724 sscape_mididev
= hw_config
->slots
[1];
725 midi_devs
[hw_config
->slots
[1]]->coproc
= &sscape_coproc_operations
;
727 sscape_write(devc
, GA_INTENA_REG
, 0x80); /* Master IRQ enable */
732 static int detect_ga(sscape_info
* devc
)
736 DDB(printk("Entered Soundscape detect_ga(%x)\n", devc
->base
));
738 if (check_region(devc
->base
, 8))
742 * First check that the address register of "ODIE" is
743 * there and that it has exactly 4 writable bits.
747 if ((save
= inb(PORT(ODIE_ADDR
))) & 0xf0)
749 DDB(printk("soundscape: Detect error A\n"));
752 outb((0x00), PORT(ODIE_ADDR
));
753 if (inb(PORT(ODIE_ADDR
)) != 0x00)
755 DDB(printk("soundscape: Detect error B\n"));
758 outb((0xff), PORT(ODIE_ADDR
));
759 if (inb(PORT(ODIE_ADDR
)) != 0x0f)
761 DDB(printk("soundscape: Detect error C\n"));
764 outb((save
), PORT(ODIE_ADDR
));
767 * Now verify that some indirect registers return zero on some bits.
768 * This may break the driver with some future revisions of "ODIE" but...
771 if (sscape_read(devc
, 0) & 0x0c)
773 DDB(printk("soundscape: Detect error D (%x)\n", sscape_read(devc
, 0)));
776 if (sscape_read(devc
, 1) & 0x0f)
778 DDB(printk("soundscape: Detect error E\n"));
781 if (sscape_read(devc
, 5) & 0x0f)
783 DDB(printk("soundscape: Detect error F\n"));
789 static int sscape_read_host_ctrl(sscape_info
* devc
)
791 return host_read(devc
);
794 static void sscape_write_host_ctrl2(sscape_info
*devc
, int a
, int b
)
796 host_command2(devc
, a
, b
);
799 static int sscape_alloc_dma(sscape_info
*devc
)
801 char *start_addr
, *end_addr
;
806 if (devc
->raw_buf
!= NULL
) return 0; /* Already done */
807 dma_pagesize
= (devc
->dma
< 4) ? (64 * 1024) : (128 * 1024);
808 devc
->raw_buf
= NULL
;
809 devc
->buffsize
= 8192*4;
810 if (devc
->buffsize
> dma_pagesize
) devc
->buffsize
= dma_pagesize
;
813 * Now loop until we get a free buffer. Try to get smaller buffer if
814 * it fails. Don't accept smaller than 8k buffer for performance
817 while (start_addr
== NULL
&& devc
->buffsize
> PAGE_SIZE
) {
818 for (sz
= 0, size
= PAGE_SIZE
; size
< devc
->buffsize
; sz
++, size
<<= 1);
819 devc
->buffsize
= PAGE_SIZE
* (1 << sz
);
820 start_addr
= (char *) __get_free_pages(GFP_ATOMIC
|GFP_DMA
, sz
);
821 if (start_addr
== NULL
) devc
->buffsize
/= 2;
824 if (start_addr
== NULL
) {
825 printk(KERN_ERR
"sscape pnp init error: Couldn't allocate DMA buffer\n");
828 /* make some checks */
829 end_addr
= start_addr
+ devc
->buffsize
- 1;
830 /* now check if it fits into the same dma-pagesize */
832 if (((long) start_addr
& ~(dma_pagesize
- 1)) != ((long) end_addr
& ~(dma_pagesize
- 1))
833 || end_addr
>= (char *) (MAX_DMA_ADDRESS
)) {
834 printk(KERN_ERR
"sscape pnp: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr
, devc
->buffsize
);
838 devc
->raw_buf
= start_addr
;
839 devc
->raw_buf_phys
= virt_to_bus(start_addr
);
841 for (page
= virt_to_page(start_addr
); page
<= virt_to_page(end_addr
); page
++)
842 mem_map_reserve(page
);
846 static void sscape_free_dma(sscape_info
*devc
)
849 unsigned long start_addr
, end_addr
;
852 if (devc
->raw_buf
== NULL
) return;
853 for (sz
= 0, size
= PAGE_SIZE
; size
< devc
->buffsize
; sz
++, size
<<= 1);
854 start_addr
= (unsigned long) devc
->raw_buf
;
855 end_addr
= start_addr
+ devc
->buffsize
;
857 for (page
= virt_to_page(start_addr
); page
<= virt_to_page(end_addr
); page
++)
858 mem_map_unreserve(page
);
860 free_pages((unsigned long) devc
->raw_buf
, sz
);
861 devc
->raw_buf
= NULL
;
864 /* Intel version !!!!!!!!! */
866 static int sscape_start_dma(int chan
, unsigned long physaddr
, int count
, int dma_mode
)
870 flags
= claim_dma_lock();
873 set_dma_mode(chan
, dma_mode
);
874 set_dma_addr(chan
, physaddr
);
875 set_dma_count(chan
, count
);
877 release_dma_lock(flags
);
881 static void sscape_pnp_start_dma(sscape_info
* devc
, int arg
)
884 if (arg
== 0) reg
= 2;
887 sscape_write(devc
, reg
, sscape_read( devc
, reg
) | 0x01);
888 sscape_write(devc
, reg
, sscape_read( devc
, reg
) & 0xFE);
891 static int sscape_pnp_wait_dma (sscape_info
* devc
, int arg
)
897 if (arg
== 0) reg
= 2;
903 d
= sscape_read(devc
, reg
) & 1;
906 } while (i
< 500000);
907 d
= sscape_read(devc
, reg
) & 1;
911 static int sscape_pnp_alloc_dma(sscape_info
* devc
)
913 /* printk(KERN_INFO "sscape: requesting dma\n"); */
914 if (request_dma(devc
-> dma
, "sscape")) return 0;
915 /* printk(KERN_INFO "sscape: dma channel allocated\n"); */
916 if (!sscape_alloc_dma(devc
)) {
917 free_dma(devc
-> dma
);
923 static void sscape_pnp_free_dma(sscape_info
* devc
)
925 sscape_free_dma( devc
);
926 free_dma(devc
-> dma
);
927 /* printk(KERN_INFO "sscape: dma released\n"); */
930 static int sscape_pnp_upload_file(sscape_info
* devc
, char* fn
)
938 sscape_write( devc
, 9, sscape_read(devc
, 9 ) & 0x3F );
939 sscape_write( devc
, 2, (devc
-> dma
<< 4) | 0x80 );
940 sscape_write( devc
, 3, 0x20 );
941 sscape_write( devc
, 9, sscape_read( devc
, 9 ) | 0x80 );
943 len
= mod_firmware_load(fn
, &data
);
945 printk(KERN_ERR
"sscape: file not found: %s\n", fn
);
949 spin_lock_irqsave(&devc
->lock
,flags
);
951 if (len
> devc
-> buffsize
) l
= devc
->buffsize
;
954 memcpy(devc
->raw_buf
, dt
, l
); dt
+= l
;
955 sscape_start_dma(devc
->dma
, devc
->raw_buf_phys
, l
, 0x48);
956 sscape_pnp_start_dma ( devc
, 0 );
957 if (sscape_pnp_wait_dma ( devc
, 0 ) == 0) {
958 spin_unlock_irqrestore(&devc
->lock
,flags
);
963 spin_unlock_irqrestore(&devc
->lock
,flags
);
966 outb(0, devc
-> base
+ 2);
967 outb(0, devc
-> base
);
969 sscape_write ( devc
, 9, sscape_read( devc
, 9 ) | 0x40);
971 timeout_val
= 5 * HZ
;
972 while (!done
&& timeout_val
-- > 0)
976 x
= inb( devc
-> base
+ 3);
977 if (x
== 0xff || x
== 0xfe) /* OBP startup acknowledge */
979 //printk(KERN_ERR "Soundscape: Acknowledge = %x\n", x);
983 timeout_val
= 5 * HZ
;
985 while (!done
&& timeout_val
-- > 0)
989 x
= inb( devc
-> base
+ 3);
990 if (x
== 0xfe) /* OBP startup acknowledge */
992 //printk(KERN_ERR "Soundscape: Acknowledge = %x\n", x);
997 if ( !done
) printk(KERN_ERR
"soundscape: OBP Initialization failed.\n");
999 sscape_write( devc
, 2, devc
->ic_type
== IC_ODIE
? 0x70 : 0x40);
1000 sscape_write( devc
, 3, (devc
-> dma
<< 4) + 0x80);
1004 static void __init
sscape_pnp_init_hw(sscape_info
* devc
)
1006 unsigned char midi_irq
= 0, sb_irq
= 0;
1008 static char code_file_name
[23] = "/sndscape/sndscape.cox";
1010 int sscape_sb_enable
= 0;
1011 int sscape_joystic_enable
= 0x7f;
1012 int sscape_mic_enable
= 0;
1013 int sscape_ext_midi
= 0;
1015 if ( !sscape_pnp_alloc_dma(devc
) ) {
1016 printk(KERN_ERR
"sscape: faild to allocate dma\n");
1020 for (i
= 0; i
< 4; i
++) {
1021 if ( devc
-> irq
== valid_interrupts
[i
] )
1023 if ( devc
-> codec_irq
== valid_interrupts
[i
] )
1027 sscape_write( devc
, 5, 0x50);
1028 sscape_write( devc
, 7, 0x2e);
1029 sscape_write( devc
, 8, 0x00);
1031 sscape_write( devc
, 2, devc
->ic_type
== IC_ODIE
? 0x70 : 0x40);
1032 sscape_write( devc
, 3, ( devc
-> dma
<< 4) | 0x80);
1034 if ( sscape_sb_enable
)
1035 sscape_write (devc
, 4, 0xF0 | (sb_irq
<< 2) | midi_irq
);
1037 sscape_write (devc
, 4, 0xF0 | (midi_irq
<<2) | midi_irq
);
1039 i
= 0x10; //sscape_read(devc, 9) & (devc->ic_type == IC_ODIE ? 0xf0 : 0xc0);
1040 if ( sscape_sb_enable
)
1041 i
|= devc
->ic_type
== IC_ODIE
? 0x05 : 0x07;
1042 if (sscape_joystic_enable
) i
|= 8;
1044 sscape_write (devc
, 9, i
);
1045 sscape_write (devc
, 6, 0x80);
1046 sscape_write (devc
, 1, 0x80);
1048 if (devc
-> codec_type
== 2) {
1049 sscape_pnp_write_codec( devc
, 0x0C, 0x50);
1050 sscape_pnp_write_codec( devc
, 0x10, sscape_pnp_read_codec( devc
, 0x10) & 0x3F);
1051 sscape_pnp_write_codec( devc
, 0x11, sscape_pnp_read_codec( devc
, 0x11) | 0xC0);
1052 sscape_pnp_write_codec( devc
, 29, 0x20);
1055 if (sscape_pnp_upload_file(devc
, "/sndscape/scope.cod") == 0 ) {
1056 printk(KERN_ERR
"sscape: faild to upload file /sndscape/scope.cod\n");
1057 sscape_pnp_free_dma(devc
);
1061 i
= sscape_read_host_ctrl( devc
);
1063 if ( (i
& 0x0F) > 7 ) {
1064 printk(KERN_ERR
"sscape: scope.cod faild\n");
1065 sscape_pnp_free_dma(devc
);
1068 if ( i
& 0x10 ) sscape_write( devc
, 7, 0x2F);
1069 code_file_name
[21] = (char) ( i
& 0x0F) + 0x30;
1070 if (sscape_pnp_upload_file( devc
, code_file_name
) == 0) {
1071 printk(KERN_ERR
"sscape: faild to upload file %s\n", code_file_name
);
1072 sscape_pnp_free_dma(devc
);
1076 if (devc
->ic_type
!= IC_ODIE
) {
1077 sscape_pnp_write_codec( devc
, 10, (sscape_pnp_read_codec(devc
, 10) & 0x7f) |
1078 ( sscape_mic_enable
== 0 ? 0x00 : 0x80) );
1080 sscape_write_host_ctrl2( devc
, 0x84, 0x64 ); /* MIDI volume */
1081 sscape_write_host_ctrl2( devc
, 0x86, 0x64 ); /* MIDI volume?? */
1082 sscape_write_host_ctrl2( devc
, 0x8A, sscape_ext_midi
);
1084 sscape_pnp_write_codec ( devc
, 6, 0x3f ); //WAV_VOL
1085 sscape_pnp_write_codec ( devc
, 7, 0x3f ); //WAV_VOL
1086 sscape_pnp_write_codec ( devc
, 2, 0x1F ); //WD_CDXVOLL
1087 sscape_pnp_write_codec ( devc
, 3, 0x1F ); //WD_CDXVOLR
1089 if (devc
-> codec_type
== 1) {
1090 sscape_pnp_write_codec ( devc
, 4, 0x1F );
1091 sscape_pnp_write_codec ( devc
, 5, 0x1F );
1092 sscape_write_host_ctrl2( devc
, 0x88, sscape_mic_enable
);
1095 sscape_pnp_write_codec ( devc
, 0x10, 0x1F << 1);
1096 sscape_pnp_write_codec ( devc
, 0x11, 0xC0 | (0x1F << 1));
1098 t
= sscape_pnp_read_codec( devc
, 0x00) & 0xDF;
1099 if ( (sscape_mic_enable
== 0)) t
|= 0;
1101 sscape_pnp_write_codec ( devc
, 0x00, t
);
1102 t
= sscape_pnp_read_codec( devc
, 0x01) & 0xDF;
1103 if ( (sscape_mic_enable
== 0) ) t
|= 0;
1105 sscape_pnp_write_codec ( devc
, 0x01, t
);
1106 sscape_pnp_write_codec ( devc
, 0x40 | 29 , 0x20);
1107 outb(0, devc
-> codec
);
1109 if (devc
-> ic_type
== IC_OPUS
) {
1110 int i
= sscape_read( devc
, 9 );
1111 sscape_write( devc
, 9, i
| 3 );
1112 sscape_write( devc
, 3, 0x40);
1114 if (check_region(0x228, 1)) {
1116 release_region(0x228,1);
1118 sscape_write( devc
, 3, (devc
-> dma
<< 4) | 0x80);
1119 sscape_write( devc
, 9, i
);
1122 host_close ( devc
);
1123 sscape_pnp_free_dma(devc
);
1126 static int __init
detect_sscape_pnp(sscape_info
* devc
)
1128 long i
, irq_bits
= 0xff;
1131 DDB(printk("Entered detect_sscape_pnp(%x)\n", devc
->base
));
1133 if (check_region(devc
->base
, 8)) {
1134 printk(KERN_ERR
"detect_sscape_pnp: port %x is not free\n", devc
->base
);
1138 if (check_region(devc
->codec
, 2)) {
1139 printk(KERN_ERR
"detect_sscape_pnp: port %x is not free\n", devc
->codec
);
1143 if ( (inb( devc
-> base
+ 2) & 0x78) != 0) return 0;
1145 d
= inb ( devc
-> base
+ 4) & 0xF0;
1146 if ( (d
& 0x80) != 0) return 0;
1149 devc
->codec_type
= 1;
1150 devc
->ic_type
= IC_ODIE
;
1152 else if ( (d
& 0x60) != 0) {
1153 devc
->codec_type
= 2;
1154 devc
->ic_type
= IC_OPUS
;
1156 else if ( (d
& 0x40) != 0) {
1157 devc
->codec_type
= 2;
1158 devc
->ic_type
= IC_ODIE
;
1164 outb(0xFA, devc
-> base
+4);
1165 if ((inb( devc
-> base
+4) & 0x9F) != 0x0A)
1167 outb(0xFE, devc
-> base
+4);
1168 if ( (inb(devc
-> base
+4) & 0x9F) != 0x0E)
1170 if ( (inb(devc
-> base
+5) & 0x9F) != 0x0E)
1173 if (devc
->codec_type
== 2) {
1174 if (devc
-> codec
!= devc
-> base
+ 8)
1175 printk("soundscape warning: incorrect codec port specified\n");
1176 devc
-> codec
= devc
-> base
+ 8;
1177 d
= 0x10 | (sscape_read(devc
, 9) & 0xCF);
1178 sscape_write(devc
, 9, d
);
1179 sscape_write(devc
, 6, 0x80);
1181 //todo: check codec is not base + 8
1184 d
= (sscape_read(devc
, 9) & 0x3F) | 0xC0;
1185 sscape_write(devc
, 9, d
);
1187 for (i
= 0; i
< 550000; i
++)
1188 if ( !(inb(devc
-> codec
) & 0x80) ) break;
1190 d
= inb(devc
-> codec
);
1193 if ( inb(devc
-> codec
+ 2) == 0xFF)
1196 sscape_write(devc
, 9, sscape_read(devc
, 9) & 0x3F );
1198 d
= inb(devc
-> codec
) & 0x80;
1200 printk(KERN_INFO
"soundscape: hardware detected\n");
1201 valid_interrupts
= valid_interrupts_new
;
1203 printk(KERN_INFO
"soundscape: board looks like media fx\n");
1204 valid_interrupts
= valid_interrupts_old
;
1208 sscape_write( devc
, 9, 0xC0 | (sscape_read(devc
, 9) & 0x3F) );
1210 for (i
= 0; i
< 550000; i
++)
1211 if ( !(inb(devc
-> codec
) & 0x80))
1214 sscape_pnp_init_hw(devc
);
1216 for (i
= 0; i
< sizeof(valid_interrupts
); i
++)
1218 if (devc
->codec_irq
== valid_interrupts
[i
]) {
1223 sscape_write(devc
, GA_INTENA_REG
, 0x00);
1224 sscape_write(devc
, GA_DMACFG_REG
, 0x50);
1225 sscape_write(devc
, GA_DMAA_REG
, 0x70);
1226 sscape_write(devc
, GA_DMAB_REG
, 0x20);
1227 sscape_write(devc
, GA_INTCFG_REG
, 0xf0);
1228 sscape_write(devc
, GA_CDCFG_REG
, 0x89 | (devc
->dma
<< 4) | (irq_bits
<< 1));
1230 sscape_pnp_write_codec( devc
, 0, sscape_pnp_read_codec( devc
, 0) | 0x20);
1231 sscape_pnp_write_codec( devc
, 0, sscape_pnp_read_codec( devc
, 1) | 0x20);
1236 static int __init
probe_sscape(struct address_info
*hw_config
)
1239 if (sscape_detected
!= 0 && sscape_detected
!= hw_config
->io_base
)
1242 devc
->base
= hw_config
->io_base
;
1243 devc
->irq
= hw_config
->irq
;
1244 devc
->dma
= hw_config
->dma
;
1245 devc
->osp
= hw_config
->osp
;
1247 #ifdef SSCAPE_DEBUG1
1249 * Temporary debugging aid. Print contents of the registers before
1255 for (i
= 0; i
< 13; i
++)
1256 printk("I%d = %02x (old value)\n", i
, sscape_read(devc
, i
));
1261 if (!detect_ga(devc
)) {
1262 if (detect_sscape_pnp(devc
)) {
1263 sscape_detected
= hw_config
->io_base
;
1269 if (old_hardware
) /* Check that it's really an old Spea/Reveal card. */
1274 if (!((tmp
= sscape_read(devc
, GA_HMCTL_REG
)) & 0xc0))
1276 sscape_write(devc
, GA_HMCTL_REG
, tmp
| 0x80);
1277 for (cc
= 0; cc
< 200000; ++cc
)
1278 inb(devc
->base
+ ODIE_ADDR
);
1281 sscape_detected
= hw_config
->io_base
;
1285 static int __init
probe_ss_ms_sound(struct address_info
*hw_config
)
1287 int i
, irq_bits
= 0xff;
1292 printk(KERN_ERR
"soundscape: Card not detected\n");
1297 printk(KERN_ERR
"soundscape: Invalid initialization order.\n");
1300 for (i
= 0; i
< sizeof(valid_interrupts
); i
++)
1302 if (hw_config
->irq
== valid_interrupts
[i
])
1308 if (hw_config
->irq
> 15 || irq_bits
== 0xff)
1310 printk(KERN_ERR
"soundscape: Invalid MSS IRQ%d\n", hw_config
->irq
);
1314 if (!sscape_is_pnp
) {
1316 ad_flags
= 0x12345677; /* Tell that we may have a CS4248 chip (Spea-V7 Media FX) */
1317 return ad1848_detect(hw_config
->io_base
, &ad_flags
, hw_config
->osp
);
1321 ad_flags
= 0x12345677; /* Tell that we may have a CS4248 chip (Spea-V7 Media FX) */
1323 ad_flags
= 0x87654321; /* Tell that we have a soundscape pnp with 1845 chip */
1324 return ad1848_detect(hw_config
->io_base
, &ad_flags
, hw_config
->osp
);
1328 static void __init
attach_ss_ms_sound(struct address_info
*hw_config
)
1331 * This routine configures the SoundScape card for use with the
1332 * Win Sound System driver. The AD1848 codec interface uses the CD-ROM
1333 * config registers of the "ODIE".
1336 int i
, irq_bits
= 0xff;
1339 if (!sscape_is_pnp
) /*pnp is already setup*/
1342 * Setup the DMA polarity.
1344 sscape_write(devc
, GA_DMACFG_REG
, 0x50);
1347 * Take the gate-array off of the DMA channel.
1349 sscape_write(devc
, GA_DMAB_REG
, 0x20);
1352 * Init the AD1848 (CD-ROM) config reg.
1354 for (i
= 0; i
< sizeof(valid_interrupts
); i
++)
1356 if (hw_config
->irq
== valid_interrupts
[i
])
1362 sscape_write(devc
, GA_CDCFG_REG
, 0x89 | (hw_config
->dma
<< 4) | (irq_bits
<< 1));
1365 if (hw_config
->irq
== devc
->irq
)
1366 printk(KERN_WARNING
"soundscape: Warning! The WSS mode can't share IRQ with MIDI\n");
1368 hw_config
->slots
[0] = ad1848_init(
1369 sscape_is_pnp
? "SoundScape" : "SoundScape PNP",
1379 if (hw_config
->slots
[0] != -1) /* The AD1848 driver installed itself */
1381 audio_devs
[hw_config
->slots
[0]]->coproc
= &sscape_coproc_operations
;
1382 devc
->codec_audiodev
= hw_config
->slots
[0];
1383 devc
->my_audiodev
= hw_config
->slots
[0];
1385 /* Set proper routings here (what are they) */
1386 AD1848_REROUTE(SOUND_MIXER_LINE1
, SOUND_MIXER_LINE
);
1389 #ifdef SSCAPE_DEBUG5
1391 * Temporary debugging aid. Print contents of the registers
1392 * after the AD1848 device has been initialized.
1397 for (i
= 0; i
< 13; i
++)
1398 printk("I%d = %02x\n", i
, sscape_read(devc
, i
));
1404 static void __exit
unload_sscape(struct address_info
*hw_config
)
1406 release_region(devc
->base
+ 2, 6);
1407 unload_mpu401(hw_config
);
1410 static void __exit
unload_ss_ms_sound(struct address_info
*hw_config
)
1412 ad1848_unload(hw_config
->io_base
,
1417 sound_unload_audiodev(hw_config
->slots
[0]);
1420 static struct address_info cfg
;
1421 static struct address_info cfg_mpu
;
1423 static int __initdata spea
= -1;
1424 static int __initdata mss
= 0;
1425 static int __initdata dma
= -1;
1426 static int __initdata irq
= -1;
1427 static int __initdata io
= -1;
1428 static int __initdata mpu_irq
= -1;
1429 static int __initdata mpu_io
= -1;
1431 MODULE_PARM(dma
, "i");
1432 MODULE_PARM(irq
, "i");
1433 MODULE_PARM(io
, "i");
1434 MODULE_PARM(spea
, "i"); /* spea=0/1 set the old_hardware */
1435 MODULE_PARM(mpu_irq
, "i");
1436 MODULE_PARM(mpu_io
, "i");
1437 MODULE_PARM(mss
, "i");
1439 static int __init
init_sscape(void)
1441 printk(KERN_INFO
"Soundscape driver Copyright (C) by Hannu Savolainen 1993-1996\n");
1447 cfg_mpu
.irq
= mpu_irq
;
1448 cfg_mpu
.io_base
= mpu_io
;
1449 /* WEH - Try to get right dma channel */
1452 devc
->codec
= cfg
.io_base
;
1453 devc
->codec_irq
= cfg
.irq
;
1454 devc
->codec_type
= 0;
1456 devc
->raw_buf
= NULL
;
1457 spin_lock_init(&devc
->lock
);
1459 if (cfg
.dma
== -1 || cfg
.irq
== -1 || cfg
.io_base
== -1) {
1460 printk(KERN_ERR
"DMA, IRQ, and IO port must be specified.\n");
1464 if (cfg_mpu
.irq
== -1 && cfg_mpu
.io_base
!= -1) {
1465 printk(KERN_ERR
"MPU_IRQ must be specified if MPU_IO is set.\n");
1470 old_hardware
= spea
;
1471 printk(KERN_INFO
"Forcing %s hardware support.\n",
1474 if (probe_sscape(&cfg_mpu
) == 0)
1477 attach_sscape(&cfg_mpu
);
1479 mss
= probe_ss_ms_sound(&cfg
);
1482 attach_ss_ms_sound(&cfg
);
1487 static void __exit
cleanup_sscape(void)
1490 unload_ss_ms_sound(&cfg
);
1491 unload_sscape(&cfg_mpu
);
1494 module_init(init_sscape
);
1495 module_exit(cleanup_sscape
);
1498 static int __init
setup_sscape(char *str
)
1500 /* io, irq, dma, mpu_io, mpu_irq */
1503 str
= get_options(str
, ARRAY_SIZE(ints
), ints
);
1514 __setup("sscape=", setup_sscape
);
1516 MODULE_LICENSE("GPL");