2 * Low-level ALSA driver for the ENSONIQ SoundScape PnP
3 * Copyright (c) by Chris Rankin
5 * This driver was written in part using information obtained from
6 * the OSS/Free SoundScape driver, written by Hannu Savolainen.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/init.h>
25 #include <linux/err.h>
26 #include <linux/isa.h>
27 #include <linux/delay.h>
28 #include <linux/pnp.h>
29 #include <linux/spinlock.h>
30 #include <linux/moduleparam.h>
32 #include <sound/core.h>
33 #include <sound/hwdep.h>
34 #include <sound/cs4231.h>
35 #include <sound/mpu401.h>
36 #include <sound/initval.h>
38 #include <sound/sscape_ioctl.h>
41 MODULE_AUTHOR("Chris Rankin");
42 MODULE_DESCRIPTION("ENSONIQ SoundScape PnP driver");
43 MODULE_LICENSE("GPL");
45 static int index[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IDX;
46 static char* id[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_STR;
47 static long port[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_PORT;
48 static long wss_port[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_PORT;
49 static int irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
50 static int mpu_irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
51 static int dma[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
52 static int dma2[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
54 module_param_array(index, int, NULL, 0444);
55 MODULE_PARM_DESC(index, "Index number for SoundScape soundcard");
57 module_param_array(id, charp, NULL, 0444);
58 MODULE_PARM_DESC(id, "Description for SoundScape card");
60 module_param_array(port, long, NULL, 0444);
61 MODULE_PARM_DESC(port, "Port # for SoundScape driver.");
63 module_param_array(wss_port, long, NULL, 0444);
64 MODULE_PARM_DESC(wss_port, "WSS Port # for SoundScape driver.");
66 module_param_array(irq, int, NULL, 0444);
67 MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver.");
69 module_param_array(mpu_irq, int, NULL, 0444);
70 MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver.");
72 module_param_array(dma, int, NULL, 0444);
73 MODULE_PARM_DESC(dma, "DMA # for SoundScape driver.");
75 module_param_array(dma2, int, NULL, 0444);
76 MODULE_PARM_DESC(dma2, "DMA2 # for SoundScape driver.");
79 static int isa_registered;
80 static int pnp_registered;
82 static struct pnp_card_device_id sscape_pnpids[] = {
83 { .id = "ENS3081", .devs = { { "ENS0000" } } }, /* Soundscape PnP */
84 { .id = "ENS4081", .devs = { { "ENS1011" } } }, /* VIVO90 */
85 { .id = "" } /* end */
88 MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
92 #define MPU401_IO(i) ((i) + 0)
93 #define MIDI_DATA_IO(i) ((i) + 0)
94 #define MIDI_CTRL_IO(i) ((i) + 1)
95 #define HOST_CTRL_IO(i) ((i) + 2)
96 #define HOST_DATA_IO(i) ((i) + 3)
97 #define ODIE_ADDR_IO(i) ((i) + 4)
98 #define ODIE_DATA_IO(i) ((i) + 5)
99 #define CODEC_IO(i) ((i) + 8)
104 #define RX_READY 0x01
105 #define TX_READY 0x02
108 #define CMD_SET_MIDI_VOL 0x84
109 #define CMD_GET_MIDI_VOL 0x85
110 #define CMD_XXX_MIDI_VOL 0x86
111 #define CMD_SET_EXTMIDI 0x8a
112 #define CMD_GET_EXTMIDI 0x8b
113 #define CMD_SET_MT32 0x8c
114 #define CMD_GET_MT32 0x8d
129 #define DMA_8BIT 0x80
132 #define AD1845_FREQ_SEL_MSB 0x16
133 #define AD1845_FREQ_SEL_LSB 0x17
148 struct resource *io_res;
149 struct resource *wss_res;
150 struct snd_cs4231 *chip;
151 struct snd_mpu401 *mpu;
152 struct snd_hwdep *hw;
155 * The MIDI device won't work until we've loaded
156 * its firmware via a hardware-dependent device IOCTL
160 unsigned long midi_usage;
161 unsigned char midi_vol;
164 #define INVALID_IRQ ((unsigned)-1)
167 static inline struct soundscape *get_card_soundscape(struct snd_card *c)
169 return (struct soundscape *) (c->private_data);
172 static inline struct soundscape *get_mpu401_soundscape(struct snd_mpu401 * mpu)
174 return (struct soundscape *) (mpu->private_data);
177 static inline struct soundscape *get_hwdep_soundscape(struct snd_hwdep * hw)
179 return (struct soundscape *) (hw->private_data);
184 * Allocates some kernel memory that we can use for DMA.
185 * I think this means that the memory has to map to
186 * contiguous pages of physical memory.
188 static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
191 if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
193 snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", size);
202 * Release the DMA-able kernel memory ...
204 static void free_dmabuf(struct snd_dma_buffer *buf)
206 if (buf && buf->area)
207 snd_dma_free_pages(buf);
212 * This function writes to the SoundScape's control registers,
213 * but doesn't do any locking. It's up to the caller to do that.
214 * This is why this function is "unsafe" ...
216 static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
218 outb(reg, ODIE_ADDR_IO(io_base));
219 outb(val, ODIE_DATA_IO(io_base));
223 * Write to the SoundScape's control registers, and do the
224 * necessary locking ...
226 static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
230 spin_lock_irqsave(&s->lock, flags);
231 sscape_write_unsafe(s->io_base, reg, val);
232 spin_unlock_irqrestore(&s->lock, flags);
236 * Read from the SoundScape's control registers, but leave any
237 * locking to the caller. This is why the function is "unsafe" ...
239 static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
241 outb(reg, ODIE_ADDR_IO(io_base));
242 return inb(ODIE_DATA_IO(io_base));
246 * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
248 static inline void set_host_mode_unsafe(unsigned io_base)
250 outb(0x0, HOST_CTRL_IO(io_base));
254 * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
256 static inline void set_midi_mode_unsafe(unsigned io_base)
258 outb(0x3, HOST_CTRL_IO(io_base));
262 * Read the SoundScape's host-mode control register, but leave
263 * any locking issues to the caller ...
265 static inline int host_read_unsafe(unsigned io_base)
268 if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
269 data = inb(HOST_DATA_IO(io_base));
276 * Read the SoundScape's host-mode control register, performing
277 * a limited amount of busy-waiting if the register isn't ready.
278 * Also leaves all locking-issues to the caller ...
280 static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
284 while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
293 * Write to the SoundScape's host-mode control registers, but
294 * leave any locking issues to the caller ...
296 static inline int host_write_unsafe(unsigned io_base, unsigned char data)
298 if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
299 outb(data, HOST_DATA_IO(io_base));
307 * Write to the SoundScape's host-mode control registers, performing
308 * a limited amount of busy-waiting if the register isn't ready.
309 * Also leaves all locking-issues to the caller ...
311 static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
316 while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
326 * Check that the MIDI subsystem is operational. If it isn't,
327 * then we will hang the computer if we try to use it ...
329 * NOTE: This check is based upon observation, not documentation.
331 static inline int verify_mpu401(const struct snd_mpu401 * mpu)
333 return ((inb(MIDI_CTRL_IO(mpu->port)) & 0xc0) == 0x80);
337 * This is apparently the standard way to initailise an MPU-401
339 static inline void initialise_mpu401(const struct snd_mpu401 * mpu)
341 outb(0, MIDI_DATA_IO(mpu->port));
345 * Tell the SoundScape to activate the AD1845 chip (I think).
346 * The AD1845 detection fails if we *don't* do this, so I
347 * think that this is a good idea ...
349 static inline void activate_ad1845_unsafe(unsigned io_base)
351 sscape_write_unsafe(io_base, GA_HMCTL_REG, (sscape_read_unsafe(io_base, GA_HMCTL_REG) & 0xcf) | 0x10);
352 sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
356 * Do the necessary ALSA-level cleanup to deallocate our driver ...
358 static void soundscape_free(struct snd_card *c)
360 struct soundscape *sscape = get_card_soundscape(c);
361 release_and_free_resource(sscape->io_res);
362 release_and_free_resource(sscape->wss_res);
363 free_dma(sscape->chip->dma1);
367 * Tell the SoundScape to begin a DMA tranfer using the given channel.
368 * All locking issues are left to the caller.
370 static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
372 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) | 0x01);
373 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) & 0xfe);
377 * Wait for a DMA transfer to complete. This is a "limited busy-wait",
378 * and all locking issues are left to the caller.
380 static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
382 while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
387 return (sscape_read_unsafe(io_base, reg) & 0x01);
391 * Wait for the On-Board Processor to return its start-up
392 * acknowledgement sequence. This wait is too long for
393 * us to perform "busy-waiting", and so we must sleep.
394 * This in turn means that we must not be holding any
395 * spinlocks when we call this function.
397 static int obp_startup_ack(struct soundscape *s, unsigned timeout)
399 while (timeout != 0) {
403 schedule_timeout_uninterruptible(1);
405 spin_lock_irqsave(&s->lock, flags);
406 x = inb(HOST_DATA_IO(s->io_base));
407 spin_unlock_irqrestore(&s->lock, flags);
408 if ((x & 0xfe) == 0xfe)
418 * Wait for the host to return its start-up acknowledgement
419 * sequence. This wait is too long for us to perform
420 * "busy-waiting", and so we must sleep. This in turn means
421 * that we must not be holding any spinlocks when we call
424 static int host_startup_ack(struct soundscape *s, unsigned timeout)
426 while (timeout != 0) {
430 schedule_timeout_uninterruptible(1);
432 spin_lock_irqsave(&s->lock, flags);
433 x = inb(HOST_DATA_IO(s->io_base));
434 spin_unlock_irqrestore(&s->lock, flags);
445 * Upload a byte-stream into the SoundScape using DMA channel A.
447 static int upload_dma_data(struct soundscape *s,
448 const unsigned char __user *data,
452 struct snd_dma_buffer dma;
455 if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
458 spin_lock_irqsave(&s->lock, flags);
461 * Reset the board ...
463 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
466 * Enable the DMA channels and configure them ...
468 sscape_write_unsafe(s->io_base, GA_DMACFG_REG, 0x50);
469 sscape_write_unsafe(s->io_base, GA_DMAA_REG, (s->chip->dma1 << 4) | DMA_8BIT);
470 sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
473 * Take the board out of reset ...
475 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
478 * Upload the user's data (firmware?) to the SoundScape
479 * board through the DMA channel ...
485 * Apparently, copying to/from userspace can sleep.
486 * We are therefore forbidden from holding any
487 * spinlocks while we copy ...
489 spin_unlock_irqrestore(&s->lock, flags);
492 * Remember that the data that we want to DMA
493 * comes from USERSPACE. We have already verified
494 * the userspace pointer ...
496 len = min(size, dma.bytes);
497 len -= __copy_from_user(dma.area, data, len);
502 * Grab that spinlock again, now that we've
505 spin_lock_irqsave(&s->lock, flags);
507 snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
508 sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
509 if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
511 * Don't forget to release this spinlock we're holding ...
513 spin_unlock_irqrestore(&s->lock, flags);
515 snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
521 set_host_mode_unsafe(s->io_base);
524 * Boot the board ... (I think)
526 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x40);
527 spin_unlock_irqrestore(&s->lock, flags);
530 * If all has gone well, then the board should acknowledge
531 * the new upload and tell us that it has rebooted OK. We
532 * give it 5 seconds (max) ...
535 if (!obp_startup_ack(s, 5)) {
536 snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
538 } else if (!host_startup_ack(s, 5)) {
539 snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
545 * NOTE!!! We are NOT holding any spinlocks at this point !!!
547 sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
554 * Upload the bootblock(?) into the SoundScape. The only
555 * purpose of this block of code seems to be to tell
556 * us which version of the microcode we should be using.
558 * NOTE: The boot-block data resides in USER-SPACE!!!
559 * However, we have already verified its memory
560 * addresses by the time we get here.
562 static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock __user *bb)
568 ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
570 spin_lock_irqsave(&sscape->lock, flags);
572 data = host_read_ctrl_unsafe(sscape->io_base, 100);
574 set_midi_mode_unsafe(sscape->io_base);
575 spin_unlock_irqrestore(&sscape->lock, flags);
579 snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
582 else if (__copy_to_user(&bb->version, &data, sizeof(bb->version))) {
591 * Upload the microcode into the SoundScape. The
592 * microcode is 64K of data, and if we try to copy
593 * it into a local variable then we will SMASH THE
594 * KERNEL'S STACK! We therefore leave it in USER
595 * SPACE, and save ourselves from copying it at all.
597 static int sscape_upload_microcode(struct soundscape *sscape,
598 const struct sscape_microcode __user *mc)
605 * We are going to have to copy this data into a special
606 * DMA-able buffer before we can upload it. We shall therefore
607 * just check that the data pointer is valid for now.
609 * NOTE: This buffer is 64K long! That's WAY too big to
610 * copy into a stack-temporary anyway.
612 if ( get_user(code, &mc->code) ||
613 !access_ok(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE) )
616 if ((err = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
617 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
620 spin_lock_irqsave(&sscape->lock, flags);
621 set_midi_mode_unsafe(sscape->io_base);
622 spin_unlock_irqrestore(&sscape->lock, flags);
624 initialise_mpu401(sscape->mpu);
630 * Hardware-specific device functions, to implement special
631 * IOCTLs for the SoundScape card. This is how we upload
632 * the microcode into the card, for example, and so we
633 * must ensure that no two processes can open this device
634 * simultaneously, and that we can't open it at all if
635 * someone is using the MIDI device.
637 static int sscape_hw_open(struct snd_hwdep * hw, struct file *file)
639 register struct soundscape *sscape = get_hwdep_soundscape(hw);
643 spin_lock_irqsave(&sscape->fwlock, flags);
645 if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
648 sscape->hw_in_use = 1;
652 spin_unlock_irqrestore(&sscape->fwlock, flags);
656 static int sscape_hw_release(struct snd_hwdep * hw, struct file *file)
658 register struct soundscape *sscape = get_hwdep_soundscape(hw);
661 spin_lock_irqsave(&sscape->fwlock, flags);
662 sscape->hw_in_use = 0;
663 spin_unlock_irqrestore(&sscape->fwlock, flags);
667 static int sscape_hw_ioctl(struct snd_hwdep * hw, struct file *file,
668 unsigned int cmd, unsigned long arg)
670 struct soundscape *sscape = get_hwdep_soundscape(hw);
674 case SND_SSCAPE_LOAD_BOOTB:
676 register struct sscape_bootblock __user *bb = (struct sscape_bootblock __user *) arg;
679 * We are going to have to copy this data into a special
680 * DMA-able buffer before we can upload it. We shall therefore
681 * just check that the data pointer is valid for now ...
683 if ( !access_ok(VERIFY_READ, bb->code, sizeof(bb->code)) )
687 * Now check that we can write the firmware version number too...
689 if ( !access_ok(VERIFY_WRITE, &bb->version, sizeof(bb->version)) )
692 err = sscape_upload_bootblock(sscape, bb);
696 case SND_SSCAPE_LOAD_MCODE:
698 register const struct sscape_microcode __user *mc = (const struct sscape_microcode __user *) arg;
700 err = sscape_upload_microcode(sscape, mc);
714 * Mixer control for the SoundScape's MIDI device.
716 static int sscape_midi_info(struct snd_kcontrol *ctl,
717 struct snd_ctl_elem_info *uinfo)
719 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
721 uinfo->value.integer.min = 0;
722 uinfo->value.integer.max = 127;
726 static int sscape_midi_get(struct snd_kcontrol *kctl,
727 struct snd_ctl_elem_value *uctl)
729 struct snd_cs4231 *chip = snd_kcontrol_chip(kctl);
730 struct snd_card *card = chip->card;
731 register struct soundscape *s = get_card_soundscape(card);
734 spin_lock_irqsave(&s->lock, flags);
735 set_host_mode_unsafe(s->io_base);
737 if (host_write_ctrl_unsafe(s->io_base, CMD_GET_MIDI_VOL, 100)) {
738 uctl->value.integer.value[0] = host_read_ctrl_unsafe(s->io_base, 100);
741 set_midi_mode_unsafe(s->io_base);
742 spin_unlock_irqrestore(&s->lock, flags);
746 static int sscape_midi_put(struct snd_kcontrol *kctl,
747 struct snd_ctl_elem_value *uctl)
749 struct snd_cs4231 *chip = snd_kcontrol_chip(kctl);
750 struct snd_card *card = chip->card;
751 register struct soundscape *s = get_card_soundscape(card);
755 spin_lock_irqsave(&s->lock, flags);
758 * We need to put the board into HOST mode before we
759 * can send any volume-changing HOST commands ...
761 set_host_mode_unsafe(s->io_base);
764 * To successfully change the MIDI volume setting, you seem to
765 * have to write a volume command, write the new volume value,
766 * and then perform another volume-related command. Perhaps the
767 * first command is an "open" and the second command is a "close"?
769 if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
773 change = (host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
774 && host_write_ctrl_unsafe(s->io_base, ((unsigned char) uctl->value.integer. value[0]) & 127, 100)
775 && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100));
779 * Take the board out of HOST mode and back into MIDI mode ...
781 set_midi_mode_unsafe(s->io_base);
783 spin_unlock_irqrestore(&s->lock, flags);
787 static struct snd_kcontrol_new midi_mixer_ctl = {
788 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
790 .info = sscape_midi_info,
791 .get = sscape_midi_get,
792 .put = sscape_midi_put
796 * The SoundScape can use two IRQs from a possible set of four.
797 * These IRQs are encoded as bit patterns so that they can be
798 * written to the control registers.
800 static unsigned __devinit get_irq_config(int irq)
802 static const int valid_irq[] = { 9, 5, 7, 10 };
805 for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) {
806 if (irq == valid_irq[cfg])
815 * Perform certain arcane port-checks to see whether there
816 * is a SoundScape board lurking behind the given ports.
818 static int __devinit detect_sscape(struct soundscape *s)
823 int codec = s->wss_base;
825 spin_lock_irqsave(&s->lock, flags);
828 * The following code is lifted from the original OSS driver,
829 * and as I don't have a datasheet I cannot really comment
830 * on what it is doing...
832 if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
835 d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
841 s->ic_type = IC_ODIE;
842 } else if ((d & 0x60) != 0) {
844 s->ic_type = IC_OPUS;
848 outb(0xfa, ODIE_ADDR_IO(s->io_base));
849 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
852 outb(0xfe, ODIE_ADDR_IO(s->io_base));
853 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
856 outb(0xfe, ODIE_ADDR_IO(s->io_base));
857 d = inb(ODIE_DATA_IO(s->io_base));
858 if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e)
861 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
862 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
864 if (s->type == SSCAPE_VIVO)
866 /* wait for WSS codec */
867 for (d = 0; d < 500; d++) {
868 if ((inb(codec) & 0x80) == 0)
870 spin_unlock_irqrestore(&s->lock, flags);
872 spin_lock_irqsave(&s->lock, flags);
874 snd_printd(KERN_INFO "init delay = %d ms\n", d);
877 * SoundScape successfully detected!
882 spin_unlock_irqrestore(&s->lock, flags);
887 * ALSA callback function, called when attempting to open the MIDI device.
888 * Check that the MIDI firmware has been loaded, because we don't want
889 * to crash the machine. Also check that someone isn't using the hardware
892 static int mpu401_open(struct snd_mpu401 * mpu)
896 if (!verify_mpu401(mpu)) {
897 snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
900 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
903 spin_lock_irqsave(&sscape->fwlock, flags);
905 if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
908 ++(sscape->midi_usage);
912 spin_unlock_irqrestore(&sscape->fwlock, flags);
918 static void mpu401_close(struct snd_mpu401 * mpu)
920 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
923 spin_lock_irqsave(&sscape->fwlock, flags);
924 --(sscape->midi_usage);
925 spin_unlock_irqrestore(&sscape->fwlock, flags);
929 * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
931 static int __devinit create_mpu401(struct snd_card *card, int devnum, unsigned long port, int irq)
933 struct soundscape *sscape = get_card_soundscape(card);
934 struct snd_rawmidi *rawmidi;
937 if ((err = snd_mpu401_uart_new(card, devnum,
939 port, MPU401_INFO_INTEGRATED,
942 struct snd_mpu401 *mpu = (struct snd_mpu401 *) rawmidi->private_data;
943 mpu->open_input = mpu401_open;
944 mpu->open_output = mpu401_open;
945 mpu->close_input = mpu401_close;
946 mpu->close_output = mpu401_close;
947 mpu->private_data = sscape;
950 initialise_mpu401(mpu);
958 * Override for the CS4231 playback format function.
959 * The AD1845 has much simpler format and rate selection.
961 static void ad1845_playback_format(struct snd_cs4231 * chip, struct snd_pcm_hw_params *params, unsigned char format)
964 unsigned rate = params_rate(params);
967 * The AD1845 can't handle sample frequencies
968 * outside of 4 kHZ to 50 kHZ
972 else if (rate < 4000)
975 spin_lock_irqsave(&chip->reg_lock, flags);
978 * Program the AD1845 correctly for the playback stream.
979 * Note that we do NOT need to toggle the MCE bit because
980 * the PLAYBACK_ENABLE bit of the Interface Configuration
983 * NOTE: We seem to need to write to the MSB before the LSB
984 * to get the correct sample frequency.
986 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, (format & 0xf0));
987 snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
988 snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
990 spin_unlock_irqrestore(&chip->reg_lock, flags);
994 * Override for the CS4231 capture format function.
995 * The AD1845 has much simpler format and rate selection.
997 static void ad1845_capture_format(struct snd_cs4231 * chip, struct snd_pcm_hw_params *params, unsigned char format)
1000 unsigned rate = params_rate(params);
1003 * The AD1845 can't handle sample frequencies
1004 * outside of 4 kHZ to 50 kHZ
1008 else if (rate < 4000)
1011 spin_lock_irqsave(&chip->reg_lock, flags);
1014 * Program the AD1845 correctly for the playback stream.
1015 * Note that we do NOT need to toggle the MCE bit because
1016 * the CAPTURE_ENABLE bit of the Interface Configuration
1019 * NOTE: We seem to need to write to the MSB before the LSB
1020 * to get the correct sample frequency.
1022 snd_cs4231_out(chip, CS4231_REC_FORMAT, (format & 0xf0));
1023 snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
1024 snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
1026 spin_unlock_irqrestore(&chip->reg_lock, flags);
1030 * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
1031 * is very much like a CS4231, with a few extra bits. We will
1032 * try to support at least some of the extra bits by overriding
1033 * some of the CS4231 callback.
1035 static int __devinit create_ad1845(struct snd_card *card, unsigned port,
1036 int irq, int dma1, int dma2)
1038 register struct soundscape *sscape = get_card_soundscape(card);
1039 struct snd_cs4231 *chip;
1042 if (sscape->type == SSCAPE_VIVO)
1048 err = snd_cs4231_create(card,
1049 port, -1, irq, dma1, dma2,
1050 CS4231_HW_DETECT, CS4231_HWSHARE_DMA1, &chip);
1052 unsigned long flags;
1053 struct snd_pcm *pcm;
1055 #define AD1845_FREQ_SEL_ENABLE 0x08
1057 #define AD1845_PWR_DOWN_CTRL 0x1b
1058 #define AD1845_CRYS_CLOCK_SEL 0x1d
1061 * It turns out that the PLAYBACK_ENABLE bit is set
1062 * by the lowlevel driver ...
1064 #define AD1845_IFACE_CONFIG \
1065 (CS4231_AUTOCALIB | CS4231_RECORD_ENABLE | CS4231_PLAYBACK_ENABLE)
1066 snd_cs4231_mce_up(chip);
1067 spin_lock_irqsave(&chip->reg_lock, flags);
1068 snd_cs4231_out(chip, CS4231_IFACE_CTRL, AD1845_IFACE_CONFIG);
1069 spin_unlock_irqrestore(&chip->reg_lock, flags);
1070 snd_cs4231_mce_down(chip);
1073 if (sscape->type != SSCAPE_VIVO) {
1076 * The input clock frequency on the SoundScape must
1077 * be 14.31818 MHz, because we must set this register
1078 * to get the playback to sound correct ...
1080 snd_cs4231_mce_up(chip);
1081 spin_lock_irqsave(&chip->reg_lock, flags);
1082 snd_cs4231_out(chip, AD1845_CRYS_CLOCK_SEL, 0x20);
1083 spin_unlock_irqrestore(&chip->reg_lock, flags);
1084 snd_cs4231_mce_down(chip);
1087 * More custom configuration:
1088 * a) select "mode 2" and provide a current drive of 8mA
1089 * b) enable frequency selection (for capture/playback)
1091 spin_lock_irqsave(&chip->reg_lock, flags);
1092 snd_cs4231_out(chip, CS4231_MISC_INFO,
1093 CS4231_MODE2 | 0x10);
1094 val = snd_cs4231_in(chip, AD1845_PWR_DOWN_CTRL);
1095 snd_cs4231_out(chip, AD1845_PWR_DOWN_CTRL,
1096 val | AD1845_FREQ_SEL_ENABLE);
1097 spin_unlock_irqrestore(&chip->reg_lock, flags);
1100 err = snd_cs4231_pcm(chip, 0, &pcm);
1102 snd_printk(KERN_ERR "sscape: No PCM device "
1103 "for AD1845 chip\n");
1107 err = snd_cs4231_mixer(chip);
1109 snd_printk(KERN_ERR "sscape: No mixer device "
1110 "for AD1845 chip\n");
1113 err = snd_cs4231_timer(chip, 0, NULL);
1115 snd_printk(KERN_ERR "sscape: No timer device "
1116 "for AD1845 chip\n");
1120 if (sscape->type != SSCAPE_VIVO) {
1121 err = snd_ctl_add(card,
1122 snd_ctl_new1(&midi_mixer_ctl, chip));
1124 snd_printk(KERN_ERR "sscape: Could not create "
1125 "MIDI mixer control\n");
1128 chip->set_playback_format = ad1845_playback_format;
1129 chip->set_capture_format = ad1845_capture_format;
1132 strcpy(card->driver, "SoundScape");
1133 strcpy(card->shortname, pcm->name);
1134 snprintf(card->longname, sizeof(card->longname),
1135 "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n",
1136 pcm->name, chip->port, chip->irq,
1137 chip->dma1, chip->dma2);
1139 sscape->chip = chip;
1148 * Create an ALSA soundcard entry for the SoundScape, using
1149 * the given list of port, IRQ and DMA resources.
1151 static int __devinit create_sscape(int dev, struct snd_card *card)
1153 struct soundscape *sscape = get_card_soundscape(card);
1156 unsigned mpu_irq_cfg;
1158 struct resource *io_res;
1159 struct resource *wss_res;
1160 unsigned long flags;
1164 * Check that the user didn't pass us garbage data ...
1166 irq_cfg = get_irq_config(irq[dev]);
1167 if (irq_cfg == INVALID_IRQ) {
1168 snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", irq[dev]);
1172 mpu_irq_cfg = get_irq_config(mpu_irq[dev]);
1173 if (mpu_irq_cfg == INVALID_IRQ) {
1174 printk(KERN_ERR "sscape: Invalid IRQ %d\n", mpu_irq[dev]);
1180 * Grab IO ports that we will need to probe so that we
1181 * can detect and control this hardware ...
1183 io_res = request_region(xport, 8, "SoundScape");
1185 snd_printk(KERN_ERR "sscape: can't grab port 0x%x\n", xport);
1189 if (sscape->type == SSCAPE_VIVO) {
1190 wss_res = request_region(wss_port[dev], 4, "SoundScape");
1192 snd_printk(KERN_ERR "sscape: can't grab port 0x%lx\n",
1195 goto _release_region;
1200 * Grab one DMA channel ...
1202 err = request_dma(dma[dev], "SoundScape");
1204 snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", dma[dev]);
1205 goto _release_region;
1208 spin_lock_init(&sscape->lock);
1209 spin_lock_init(&sscape->fwlock);
1210 sscape->io_res = io_res;
1211 sscape->wss_res = wss_res;
1212 sscape->io_base = xport;
1213 sscape->wss_base = wss_port[dev];
1215 if (!detect_sscape(sscape)) {
1216 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1221 printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
1222 sscape->io_base, irq[dev], dma[dev]);
1224 if (sscape->type != SSCAPE_VIVO) {
1226 * Now create the hardware-specific device so that we can
1227 * load the microcode into the on-board processor.
1228 * We cannot use the MPU-401 MIDI system until this firmware
1229 * has been loaded into the card.
1231 err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw));
1233 printk(KERN_ERR "sscape: Failed to create "
1234 "firmware device\n");
1237 strlcpy(sscape->hw->name, "SoundScape M68K",
1238 sizeof(sscape->hw->name));
1239 sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
1240 sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
1241 sscape->hw->ops.open = sscape_hw_open;
1242 sscape->hw->ops.release = sscape_hw_release;
1243 sscape->hw->ops.ioctl = sscape_hw_ioctl;
1244 sscape->hw->private_data = sscape;
1248 * Tell the on-board devices where their resources are (I think -
1249 * I can't be sure without a datasheet ... So many magic values!)
1251 spin_lock_irqsave(&sscape->lock, flags);
1253 activate_ad1845_unsafe(sscape->io_base);
1255 sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
1256 sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
1257 sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
1260 * Enable and configure the DMA channels ...
1262 sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
1263 dma_cfg = (sscape->ic_type == IC_ODIE ? 0x70 : 0x40);
1264 sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
1265 sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
1267 sscape_write_unsafe(sscape->io_base,
1268 GA_INTCFG_REG, 0xf0 | (mpu_irq_cfg << 2) | mpu_irq_cfg);
1269 sscape_write_unsafe(sscape->io_base,
1270 GA_CDCFG_REG, 0x09 | DMA_8BIT
1271 | (dma[dev] << 4) | (irq_cfg << 1));
1273 spin_unlock_irqrestore(&sscape->lock, flags);
1276 * We have now enabled the codec chip, and so we should
1277 * detect the AD1845 device ...
1279 err = create_ad1845(card, wss_port[dev], irq[dev],
1280 dma[dev], dma2[dev]);
1282 printk(KERN_ERR "sscape: No AD1845 device at 0x%lx, IRQ %d\n",
1283 wss_port[dev], irq[dev]);
1286 #define MIDI_DEVNUM 0
1287 if (sscape->type != SSCAPE_VIVO) {
1288 err = create_mpu401(card, MIDI_DEVNUM,
1289 MPU401_IO(xport), mpu_irq[dev]);
1291 printk(KERN_ERR "sscape: Failed to create "
1292 "MPU-401 device at 0x%x\n",
1298 * Enable the master IRQ ...
1300 sscape_write(sscape, GA_INTENA_REG, 0x80);
1305 sscape->midi_vol = 0;
1306 host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
1307 host_write_ctrl_unsafe(sscape->io_base, 0, 100);
1308 host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
1312 * Now that we have successfully created this sound card,
1313 * it is safe to store the pointer.
1314 * NOTE: we only register the sound card's "destructor"
1315 * function now that our "constructor" has completed.
1317 card->private_free = soundscape_free;
1325 release_and_free_resource(wss_res);
1326 release_and_free_resource(io_res);
1332 static int __devinit snd_sscape_match(struct device *pdev, unsigned int i)
1335 * Make sure we were given ALL of the other parameters.
1337 if (port[i] == SNDRV_AUTO_PORT)
1340 if (irq[i] == SNDRV_AUTO_IRQ ||
1341 mpu_irq[i] == SNDRV_AUTO_IRQ ||
1342 dma[i] == SNDRV_AUTO_DMA) {
1344 "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1351 static int __devinit snd_sscape_probe(struct device *pdev, unsigned int dev)
1353 struct snd_card *card;
1354 struct soundscape *sscape;
1357 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1358 sizeof(struct soundscape));
1362 sscape = get_card_soundscape(card);
1363 sscape->type = SSCAPE;
1366 ret = create_sscape(dev, card);
1370 snd_card_set_dev(card, pdev);
1371 if ((ret = snd_card_register(card)) < 0) {
1372 printk(KERN_ERR "sscape: Failed to register sound card\n");
1375 dev_set_drvdata(pdev, card);
1379 snd_card_free(card);
1383 static int __devexit snd_sscape_remove(struct device *devptr, unsigned int dev)
1385 snd_card_free(dev_get_drvdata(devptr));
1386 dev_set_drvdata(devptr, NULL);
1390 #define DEV_NAME "sscape"
1392 static struct isa_driver snd_sscape_driver = {
1393 .match = snd_sscape_match,
1394 .probe = snd_sscape_probe,
1395 .remove = __devexit_p(snd_sscape_remove),
1396 /* FIXME: suspend/resume */
1403 static inline int __devinit get_next_autoindex(int i)
1405 while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT)
1411 static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1412 const struct pnp_card_device_id *pid)
1415 struct pnp_dev *dev;
1416 struct snd_card *card;
1417 struct soundscape *sscape;
1421 * Allow this function to fail *quietly* if all the ISA PnP
1422 * devices were configured using module parameters instead.
1424 if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS)
1428 * We have found a candidate ISA PnP card. Now we
1429 * have to check that it has the devices that we
1430 * expect it to have.
1432 * We will NOT try and autoconfigure all of the resources
1433 * needed and then activate the card as we are assuming that
1434 * has already been done at boot-time using /proc/isapnp.
1435 * We shall simply try to give each active card the resources
1436 * that it wants. This is a sensible strategy for a modular
1437 * system where unused modules are unloaded regularly.
1439 * This strategy is utterly useless if we compile the driver
1440 * into the kernel, of course.
1442 // printk(KERN_INFO "sscape: %s\n", card->name);
1445 * Check that we still have room for another sound card ...
1447 dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
1451 if (!pnp_is_active(dev)) {
1452 if (pnp_activate_dev(dev) < 0) {
1453 printk(KERN_INFO "sscape: device is inactive\n");
1459 * Create a new ALSA sound card entry, in anticipation
1460 * of detecting our hardware ...
1462 card = snd_card_new(index[idx], id[idx], THIS_MODULE,
1463 sizeof(struct soundscape));
1467 sscape = get_card_soundscape(card);
1470 * Identify card model ...
1472 if (!strncmp("ENS4081", pid->id, 7))
1473 sscape->type = SSCAPE_VIVO;
1475 sscape->type = SSCAPE_PNP;
1478 * Read the correct parameters off the ISA PnP bus ...
1480 port[idx] = pnp_port_start(dev, 0);
1481 irq[idx] = pnp_irq(dev, 0);
1482 mpu_irq[idx] = pnp_irq(dev, 1);
1483 dma[idx] = pnp_dma(dev, 0) & 0x03;
1484 if (sscape->type == SSCAPE_PNP) {
1485 dma2[idx] = dma[idx];
1486 wss_port[idx] = CODEC_IO(port[idx]);
1488 wss_port[idx] = pnp_port_start(dev, 1);
1489 dma2[idx] = pnp_dma(dev, 1);
1492 ret = create_sscape(idx, card);
1496 snd_card_set_dev(card, &pcard->card->dev);
1497 if ((ret = snd_card_register(card)) < 0) {
1498 printk(KERN_ERR "sscape: Failed to register sound card\n");
1502 pnp_set_card_drvdata(pcard, card);
1507 snd_card_free(card);
1511 static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1513 snd_card_free(pnp_get_card_drvdata(pcard));
1514 pnp_set_card_drvdata(pcard, NULL);
1517 static struct pnp_card_driver sscape_pnpc_driver = {
1518 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1520 .id_table = sscape_pnpids,
1521 .probe = sscape_pnp_detect,
1522 .remove = __devexit_p(sscape_pnp_remove),
1525 #endif /* CONFIG_PNP */
1527 static int __init sscape_init(void)
1531 err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS);
1536 err = pnp_register_card_driver(&sscape_pnpc_driver);
1546 static void __exit sscape_exit(void)
1550 pnp_unregister_card_driver(&sscape_pnpc_driver);
1553 isa_unregister_driver(&snd_sscape_driver);
1556 module_init(sscape_init);
1557 module_exit(sscape_exit);