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 <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/pnp.h>
28 #include <linux/spinlock.h>
29 #include <linux/moduleparam.h>
31 #include <sound/core.h>
32 #include <sound/hwdep.h>
33 #include <sound/cs4231.h>
34 #include <sound/mpu401.h>
35 #include <sound/initval.h>
37 #include <sound/sscape_ioctl.h>
39 #define chip_t cs4231_t
42 MODULE_AUTHOR("Chris Rankin");
43 MODULE_DESCRIPTION("ENSONIQ SoundScape PnP driver");
44 MODULE_LICENSE("GPL");
46 static int index[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IDX;
47 static char* id[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_STR;
48 static long port[SNDRV_CARDS] __devinitdata = { [0 ... (SNDRV_CARDS-1)] = SNDRV_AUTO_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;
54 module_param_array(index, int, boot_devs, 0444);
55 MODULE_PARM_DESC(index, "Index number for SoundScape soundcard");
56 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
58 module_param_array(id, charp, boot_devs, 0444);
59 MODULE_PARM_DESC(id, "Description for SoundScape card");
60 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
62 module_param_array(port, long, boot_devs, 0444);
63 MODULE_PARM_DESC(port, "Port # for SoundScape driver.");
64 MODULE_PARM_SYNTAX(port, SNDRV_ENABLED);
66 module_param_array(irq, int, boot_devs, 0444);
67 MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver.");
68 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
70 module_param_array(mpu_irq, int, boot_devs, 0444);
71 MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver.");
72 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
74 module_param_array(dma, int, boot_devs, 0444);
75 MODULE_PARM_DESC(dma, "DMA # for SoundScape driver.");
76 MODULE_PARM_SYNTAX(dma, SNDRV_DMA8_DESC);
79 static struct pnp_card_device_id sscape_pnpids[] = {
80 { .id = "ENS3081", .devs = { { "ENS0000" } } },
81 { .id = "" } /* end */
84 MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
87 static snd_card_t *sscape_card[SNDRV_CARDS];
90 #define MPU401_IO(i) ((i) + 0)
91 #define MIDI_DATA_IO(i) ((i) + 0)
92 #define MIDI_CTRL_IO(i) ((i) + 1)
93 #define HOST_CTRL_IO(i) ((i) + 2)
94 #define HOST_DATA_IO(i) ((i) + 3)
95 #define ODIE_ADDR_IO(i) ((i) + 4)
96 #define ODIE_DATA_IO(i) ((i) + 5)
97 #define CODEC_IO(i) ((i) + 8)
102 #define RX_READY 0x01
103 #define TX_READY 0x02
106 #define CMD_SET_MIDI_VOL 0x84
107 #define CMD_GET_MIDI_VOL 0x85
108 #define CMD_XXX_MIDI_VOL 0x86
109 #define CMD_SET_EXTMIDI 0x8a
110 #define CMD_GET_EXTMIDI 0x8b
111 #define CMD_SET_MT32 0x8c
112 #define CMD_GET_MT32 0x8d
127 #define DMA_8BIT 0x80
130 #define AD1845_FREQ_SEL_MSB 0x16
131 #define AD1845_FREQ_SEL_LSB 0x17
138 struct resource *io_res;
144 * The MIDI device won't work until we've loaded
145 * its firmware via a hardware-dependent device IOCTL
149 unsigned long midi_usage;
150 unsigned char midi_vol;
153 #define INVALID_IRQ ((unsigned)-1)
156 static inline struct soundscape *get_card_soundscape(snd_card_t * c)
158 return (struct soundscape *) (c->private_data);
161 static inline struct soundscape *get_mpu401_soundscape(mpu401_t * mpu)
163 return (struct soundscape *) (mpu->private_data);
166 static inline struct soundscape *get_hwdep_soundscape(snd_hwdep_t * hw)
168 return (struct soundscape *) (hw->private_data);
173 * Allocates some kernel memory that we can use for DMA.
174 * I think this means that the memory has to map to
175 * contiguous pages of physical memory.
177 static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
180 struct snd_dma_device dev;
181 memset(&dev, 0, sizeof(dev));
182 dev.type = SNDRV_DMA_TYPE_DEV;
183 dev.dev = snd_dma_isa_data();
184 if (snd_dma_alloc_pages_fallback(&dev, size, buf) < 0) {
185 snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", size);
194 * Release the DMA-able kernel memory ...
196 static void free_dmabuf(struct snd_dma_buffer *buf)
198 if (buf && buf->area) {
199 struct snd_dma_device dev;
200 memset(&dev, 0, sizeof(dev));
201 dev.type = SNDRV_DMA_TYPE_DEV;
202 dev.dev = snd_dma_isa_data();
203 snd_dma_free_pages(&dev, buf);
209 * This function writes to the SoundScape's control registers,
210 * but doesn't do any locking. It's up to the caller to do that.
211 * This is why this function is "unsafe" ...
213 static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
215 outb(reg, ODIE_ADDR_IO(io_base));
216 outb(val, ODIE_DATA_IO(io_base));
220 * Write to the SoundScape's control registers, and do the
221 * necessary locking ...
223 static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
227 spin_lock_irqsave(&s->lock, flags);
228 sscape_write_unsafe(s->io_base, reg, val);
229 spin_unlock_irqrestore(&s->lock, flags);
233 * Read from the SoundScape's control registers, but leave any
234 * locking to the caller. This is why the function is "unsafe" ...
236 static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
238 outb(reg, ODIE_ADDR_IO(io_base));
239 return inb(ODIE_DATA_IO(io_base));
243 * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
245 static inline void set_host_mode_unsafe(unsigned io_base)
247 outb(0x0, HOST_CTRL_IO(io_base));
251 * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
253 static inline void set_midi_mode_unsafe(unsigned io_base)
255 outb(0x3, HOST_CTRL_IO(io_base));
259 * Read the SoundScape's host-mode control register, but leave
260 * any locking issues to the caller ...
262 static inline int host_read_unsafe(unsigned io_base)
265 if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
266 data = inb(HOST_DATA_IO(io_base));
273 * Read the SoundScape's host-mode control register, performing
274 * a limited amount of busy-waiting if the register isn't ready.
275 * Also leaves all locking-issues to the caller ...
277 static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
281 while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
290 * Write to the SoundScape's host-mode control registers, but
291 * leave any locking issues to the caller ...
293 static inline int host_write_unsafe(unsigned io_base, unsigned char data)
295 if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
296 outb(data, HOST_DATA_IO(io_base));
304 * Write to the SoundScape's host-mode control registers, performing
305 * a limited amount of busy-waiting if the register isn't ready.
306 * Also leaves all locking-issues to the caller ...
308 static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
313 while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
323 * Check that the MIDI subsystem is operational. If it isn't,
324 * then we will hang the computer if we try to use it ...
326 * NOTE: This check is based upon observation, not documentation.
328 static inline int verify_mpu401(const mpu401_t * mpu)
330 return ((inb(MIDI_CTRL_IO(mpu->port)) & 0xc0) == 0x80);
334 * This is apparently the standard way to initailise an MPU-401
336 static inline void initialise_mpu401(const mpu401_t * mpu)
338 outb(0, MIDI_DATA_IO(mpu->port));
342 * Tell the SoundScape to activate the AD1845 chip (I think).
343 * The AD1845 detection fails if we *don't* do this, so I
344 * think that this is a good idea ...
346 static inline void activate_ad1845_unsafe(unsigned io_base)
348 sscape_write_unsafe(io_base, GA_HMCTL_REG, (sscape_read_unsafe(io_base, GA_HMCTL_REG) & 0xcf) | 0x10);
349 sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
353 * Do the necessary ALSA-level cleanup to deallocate our driver ...
355 static void soundscape_free(snd_card_t * c)
357 register struct soundscape *sscape = get_card_soundscape(c);
358 release_resource(sscape->io_res);
359 kfree_nocheck(sscape->io_res);
360 free_dma(sscape->chip->dma1);
364 * Put this process into an idle wait-state for a certain number
365 * of "jiffies". The process can almost certainly be rescheduled
366 * while we're waiting, and so we must NOT be holding any spinlocks
367 * when we call this function. If we are then we risk DEADLOCK in
368 * SMP (Ha!) or pre-emptible kernels.
370 static inline void sleep(long jiffs, int state)
372 set_current_state(state);
373 schedule_timeout(jiffs);
377 * Tell the SoundScape to begin a DMA tranfer using the given channel.
378 * All locking issues are left to the caller.
380 static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
382 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) | 0x01);
383 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) & 0xfe);
387 * Wait for a DMA transfer to complete. This is a "limited busy-wait",
388 * and all locking issues are left to the caller.
390 static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
392 while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
397 return (sscape_read_unsafe(io_base, reg) & 0x01);
401 * Wait for the On-Board Processor to return its start-up
402 * acknowledgement sequence. This wait is too long for
403 * us to perform "busy-waiting", and so we must sleep.
404 * This in turn means that we must not be holding any
405 * spinlocks when we call this function.
407 static int obp_startup_ack(struct soundscape *s, unsigned timeout)
409 while (timeout != 0) {
413 sleep(1, TASK_INTERRUPTIBLE);
415 spin_lock_irqsave(&s->lock, flags);
416 x = inb(HOST_DATA_IO(s->io_base));
417 spin_unlock_irqrestore(&s->lock, flags);
418 if ((x & 0xfe) == 0xfe)
428 * Wait for the host to return its start-up acknowledgement
429 * sequence. This wait is too long for us to perform
430 * "busy-waiting", and so we must sleep. This in turn means
431 * that we must not be holding any spinlocks when we call
434 static int host_startup_ack(struct soundscape *s, unsigned timeout)
436 while (timeout != 0) {
440 sleep(1, TASK_INTERRUPTIBLE);
442 spin_lock_irqsave(&s->lock, flags);
443 x = inb(HOST_DATA_IO(s->io_base));
444 spin_unlock_irqrestore(&s->lock, flags);
455 * Upload a byte-stream into the SoundScape using DMA channel A.
457 static int upload_dma_data(struct soundscape *s,
458 const unsigned char *data,
462 struct snd_dma_buffer dma;
465 if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
468 spin_lock_irqsave(&s->lock, flags);
471 * Reset the board ...
473 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
476 * Enable the DMA channels and configure them ...
478 sscape_write_unsafe(s->io_base, GA_DMACFG_REG, 0x50);
479 sscape_write_unsafe(s->io_base, GA_DMAA_REG, (s->chip->dma1 << 4) | DMA_8BIT);
480 sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
483 * Take the board out of reset ...
485 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
488 * Upload the user's data (firmware?) to the SoundScape
489 * board through the DMA channel ...
495 * Apparently, copying to/from userspace can sleep.
496 * We are therefore forbidden from holding any
497 * spinlocks while we copy ...
499 spin_unlock_irqrestore(&s->lock, flags);
502 * Remember that the data that we want to DMA
503 * comes from USERSPACE. We have already verified
504 * the userspace pointer ...
506 len = min(size, dma.bytes);
507 __copy_from_user(dma.area, data, len);
512 * Grab that spinlock again, now that we've
515 spin_lock_irqsave(&s->lock, flags);
517 snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
518 sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
519 if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
521 * Don't forget to release this spinlock we're holding ...
523 spin_unlock_irqrestore(&s->lock, flags);
525 snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
531 set_host_mode_unsafe(s->io_base);
534 * Boot the board ... (I think)
536 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x40);
537 spin_unlock_irqrestore(&s->lock, flags);
540 * If all has gone well, then the board should acknowledge
541 * the new upload and tell us that it has rebooted OK. We
542 * give it 5 seconds (max) ...
545 if (!obp_startup_ack(s, 5)) {
546 snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
548 } else if (!host_startup_ack(s, 5)) {
549 snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
555 * NOTE!!! We are NOT holding any spinlocks at this point !!!
557 sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
564 * Upload the bootblock(?) into the SoundScape. The only
565 * purpose of this block of code seems to be to tell
566 * us which version of the microcode we should be using.
568 * NOTE: The boot-block data resides in USER-SPACE!!!
569 * However, we have already verified its memory
570 * addresses by the time we get here.
572 static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock *bb)
578 ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
580 spin_lock_irqsave(&sscape->lock, flags);
582 data = host_read_ctrl_unsafe(sscape->io_base, 100);
584 set_midi_mode_unsafe(sscape->io_base);
585 spin_unlock_irqrestore(&sscape->lock, flags);
589 snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
592 __copy_to_user(&bb->version, &data, sizeof(bb->version));
600 * Upload the microcode into the SoundScape. The
601 * microcode is 64K of data, and if we try to copy
602 * it into a local variable then we will SMASH THE
603 * KERNEL'S STACK! We therefore leave it in USER
604 * SPACE, and save ourselves from copying it at all.
606 static int sscape_upload_microcode(struct soundscape *sscape,
607 const struct sscape_microcode *mc)
614 * We are going to have to copy this data into a special
615 * DMA-able buffer before we can upload it. We shall therefore
616 * just check that the data pointer is valid for now.
618 * NOTE: This buffer is 64K long! That's WAY too big to
619 * copy into a stack-temporary anyway.
621 if (get_user(code, &mc->code))
623 if ((err = verify_area(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE)) != 0)
626 if ((ret = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
627 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
630 spin_lock_irqsave(&sscape->lock, flags);
631 set_midi_mode_unsafe(sscape->io_base);
632 spin_unlock_irqrestore(&sscape->lock, flags);
634 initialise_mpu401(sscape->mpu);
640 * Hardware-specific device functions, to implement special
641 * IOCTLs for the SoundScape card. This is how we upload
642 * the microcode into the card, for example, and so we
643 * must ensure that no two processes can open this device
644 * simultaneously, and that we can't open it at all if
645 * someone is using the MIDI device.
647 static int sscape_hw_open(snd_hwdep_t * hw, struct file *file)
649 register struct soundscape *sscape = get_hwdep_soundscape(hw);
653 spin_lock_irqsave(&sscape->fwlock, flags);
655 if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
658 sscape->hw_in_use = 1;
662 spin_unlock_irqrestore(&sscape->fwlock, flags);
666 static int sscape_hw_release(snd_hwdep_t * hw, struct file *file)
668 register struct soundscape *sscape = get_hwdep_soundscape(hw);
671 spin_lock_irqsave(&sscape->fwlock, flags);
672 sscape->hw_in_use = 0;
673 spin_unlock_irqrestore(&sscape->fwlock, flags);
677 static int sscape_hw_ioctl(snd_hwdep_t * hw, struct file *file,
678 unsigned int cmd, unsigned long arg)
680 struct soundscape *sscape = get_hwdep_soundscape(hw);
684 case SND_SSCAPE_LOAD_BOOTB:
686 register struct sscape_bootblock *bb = (struct sscape_bootblock *) arg;
689 * We are going to have to copy this data into a special
690 * DMA-able buffer before we can upload it. We shall therefore
691 * just check that the data pointer is valid for now ...
693 if ((err = verify_area(VERIFY_READ, bb->code, sizeof(bb->code))) != 0)
697 * Now check that we can write the firmware version number too...
699 if ((err = verify_area(VERIFY_WRITE, &bb->version, sizeof(bb->version))) != 0)
702 err = sscape_upload_bootblock(sscape, bb);
706 case SND_SSCAPE_LOAD_MCODE:
708 register const struct sscape_microcode *mc = (const struct sscape_microcode *) arg;
710 err = sscape_upload_microcode(sscape, mc);
724 * Mixer control for the SoundScape's MIDI device.
726 static int sscape_midi_info(snd_kcontrol_t * ctl,
727 snd_ctl_elem_info_t * uinfo)
729 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
731 uinfo->value.integer.min = 0;
732 uinfo->value.integer.max = 127;
736 static int sscape_midi_get(snd_kcontrol_t * kctl,
737 snd_ctl_elem_value_t * uctl)
739 cs4231_t *chip = snd_kcontrol_chip(kctl);
740 snd_card_t *card = chip->card;
741 register struct soundscape *s = get_card_soundscape(card);
744 spin_lock_irqsave(&s->lock, flags);
745 set_host_mode_unsafe(s->io_base);
747 if (host_write_ctrl_unsafe(s->io_base, CMD_GET_MIDI_VOL, 100)) {
748 uctl->value.integer.value[0] = host_read_ctrl_unsafe(s->io_base, 100);
751 set_midi_mode_unsafe(s->io_base);
752 spin_unlock_irqrestore(&s->lock, flags);
756 static int sscape_midi_put(snd_kcontrol_t * kctl,
757 snd_ctl_elem_value_t * uctl)
759 cs4231_t *chip = snd_kcontrol_chip(kctl);
760 snd_card_t *card = chip->card;
761 register struct soundscape *s = get_card_soundscape(card);
765 spin_lock_irqsave(&s->lock, flags);
768 * We need to put the board into HOST mode before we
769 * can send any volume-changing HOST commands ...
771 set_host_mode_unsafe(s->io_base);
774 * To successfully change the MIDI volume setting, you seem to
775 * have to write a volume command, write the new volume value,
776 * and then perform another volume-related command. Perhaps the
777 * first command is an "open" and the second command is a "close"?
779 if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
783 change = (host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
784 && host_write_ctrl_unsafe(s->io_base, ((unsigned char) uctl->value.integer. value[0]) & 127, 100)
785 && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100));
789 * Take the board out of HOST mode and back into MIDI mode ...
791 set_midi_mode_unsafe(s->io_base);
793 spin_unlock_irqrestore(&s->lock, flags);
797 static snd_kcontrol_new_t midi_mixer_ctl = {
798 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
800 .info = sscape_midi_info,
801 .get = sscape_midi_get,
802 .put = sscape_midi_put
806 * The SoundScape can use two IRQs from a possible set of four.
807 * These IRQs are encoded as bit patterns so that they can be
808 * written to the control registers.
810 static unsigned __devinit get_irq_config(int irq)
812 static const int valid_irq[] = { 9, 5, 7, 10 };
815 for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) {
816 if (irq == valid_irq[cfg])
825 * Perform certain arcane port-checks to see whether there
826 * is a SoundScape board lurking behind the given ports.
828 static int __devinit detect_sscape(struct soundscape *s)
834 spin_lock_irqsave(&s->lock, flags);
837 * The following code is lifted from the original OSS driver,
838 * and as I don't have a datasheet I cannot really comment
839 * on what it is doing...
841 if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
844 d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
850 s->ic_type = IC_ODIE;
851 } else if ((d & 0x60) != 0) {
853 s->ic_type = IC_OPUS;
857 outb(0xfa, ODIE_ADDR_IO(s->io_base));
858 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
861 outb(0xfe, ODIE_ADDR_IO(s->io_base));
862 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
864 if ((inb(ODIE_DATA_IO(s->io_base)) & 0x9f) != 0x0e)
868 * SoundScape successfully detected!
873 spin_unlock_irqrestore(&s->lock, flags);
878 * ALSA callback function, called when attempting to open the MIDI device.
879 * Check that the MIDI firmware has been loaded, because we don't want
880 * to crash the machine. Also check that someone isn't using the hardware
883 static int mpu401_open(mpu401_t * mpu)
887 if (!verify_mpu401(mpu)) {
888 snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
891 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
894 spin_lock_irqsave(&sscape->fwlock, flags);
896 if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
899 ++(sscape->midi_usage);
903 spin_unlock_irqrestore(&sscape->fwlock, flags);
909 static void mpu401_close(mpu401_t * mpu)
911 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
914 spin_lock_irqsave(&sscape->fwlock, flags);
915 --(sscape->midi_usage);
916 spin_unlock_irqrestore(&sscape->fwlock, flags);
920 * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
922 static int __devinit create_mpu401(snd_card_t * card, int devnum, unsigned long port, int irq)
924 struct soundscape *sscape = get_card_soundscape(card);
925 snd_rawmidi_t *rawmidi;
928 #define MPU401_SHARE_HARDWARE 1
929 if ((err = snd_mpu401_uart_new(card, devnum,
931 port, MPU401_SHARE_HARDWARE,
934 mpu401_t *mpu = (mpu401_t *) rawmidi->private_data;
935 mpu->open_input = mpu401_open;
936 mpu->open_output = mpu401_open;
937 mpu->close_input = mpu401_close;
938 mpu->close_output = mpu401_close;
939 mpu->private_data = sscape;
942 initialise_mpu401(mpu);
950 * Override for the CS4231 playback format function.
951 * The AD1845 has much simpler format and rate selection.
953 static void ad1845_playback_format(cs4231_t * chip, snd_pcm_hw_params_t * params, unsigned char format)
956 unsigned rate = params_rate(params);
959 * The AD1845 can't handle sample frequencies
960 * outside of 4 kHZ to 50 kHZ
964 else if (rate < 4000)
967 spin_lock_irqsave(&chip->reg_lock, flags);
970 * Program the AD1845 correctly for the playback stream.
971 * Note that we do NOT need to toggle the MCE bit because
972 * the PLAYBACK_ENABLE bit of the Interface Configuration
975 * NOTE: We seem to need to write to the MSB before the LSB
976 * to get the correct sample frequency.
978 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, (format & 0xf0));
979 snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
980 snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
982 spin_unlock_irqrestore(&chip->reg_lock, flags);
986 * Override for the CS4231 capture format function.
987 * The AD1845 has much simpler format and rate selection.
989 static void ad1845_capture_format(cs4231_t * chip, snd_pcm_hw_params_t * params, unsigned char format)
992 unsigned rate = params_rate(params);
995 * The AD1845 can't handle sample frequencies
996 * outside of 4 kHZ to 50 kHZ
1000 else if (rate < 4000)
1003 spin_lock_irqsave(&chip->reg_lock, flags);
1006 * Program the AD1845 correctly for the playback stream.
1007 * Note that we do NOT need to toggle the MCE bit because
1008 * the CAPTURE_ENABLE bit of the Interface Configuration
1011 * NOTE: We seem to need to write to the MSB before the LSB
1012 * to get the correct sample frequency.
1014 snd_cs4231_out(chip, CS4231_REC_FORMAT, (format & 0xf0));
1015 snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
1016 snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
1018 spin_unlock_irqrestore(&chip->reg_lock, flags);
1022 * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
1023 * is very much like a CS4231, with a few extra bits. We will
1024 * try to support at least some of the extra bits by overriding
1025 * some of the CS4231 callback.
1027 static int __devinit create_ad1845(snd_card_t * card, unsigned port, int irq, int dma1)
1029 register struct soundscape *sscape = get_card_soundscape(card);
1033 #define CS4231_SHARE_HARDWARE (CS4231_HWSHARE_DMA1 | CS4231_HWSHARE_DMA2)
1035 * The AD1845 PCM device is only half-duplex, and so
1036 * we only give it one DMA channel ...
1038 if ((err = snd_cs4231_create(card,
1039 port, -1, irq, dma1, dma1,
1041 CS4231_HWSHARE_DMA1, &chip)) == 0) {
1042 unsigned long flags;
1045 #define AD1845_FREQ_SEL_ENABLE 0x08
1047 #define AD1845_PWR_DOWN_CTRL 0x1b
1048 #define AD1845_CRYS_CLOCK_SEL 0x1d
1051 * It turns out that the PLAYBACK_ENABLE bit is set
1052 * by the lowlevel driver ...
1054 #define AD1845_IFACE_CONFIG \
1055 (CS4231_AUTOCALIB | CS4231_RECORD_ENABLE | CS4231_PLAYBACK_ENABLE)
1056 snd_cs4231_mce_up(chip);
1057 spin_lock_irqsave(&chip->reg_lock, flags);
1058 snd_cs4231_out(chip, CS4231_IFACE_CTRL, AD1845_IFACE_CONFIG);
1059 spin_unlock_irqrestore(&chip->reg_lock, flags);
1060 snd_cs4231_mce_down(chip);
1064 * The input clock frequency on the SoundScape must
1065 * be 14.31818 MHz, because we must set this register
1066 * to get the playback to sound correct ...
1068 snd_cs4231_mce_up(chip);
1069 spin_lock_irqsave(&chip->reg_lock, flags);
1070 snd_cs4231_out(chip, AD1845_CRYS_CLOCK_SEL, 0x20);
1071 spin_unlock_irqrestore(&chip->reg_lock, flags);
1072 snd_cs4231_mce_down(chip);
1075 * More custom configuration:
1076 * a) select "mode 2", and provide a current drive of 8 mA
1077 * b) enable frequency selection (for capture/playback)
1079 spin_lock_irqsave(&chip->reg_lock, flags);
1080 snd_cs4231_out(chip, CS4231_MISC_INFO, (CS4231_MODE2 | 0x10));
1081 snd_cs4231_out(chip, AD1845_PWR_DOWN_CTRL, snd_cs4231_in(chip, AD1845_PWR_DOWN_CTRL) | AD1845_FREQ_SEL_ENABLE);
1082 spin_unlock_irqrestore(&chip->reg_lock, flags);
1084 if ((err = snd_cs4231_pcm(chip, 0, &pcm)) < 0) {
1085 snd_printk(KERN_ERR "sscape: No PCM device for AD1845 chip\n");
1089 if ((err = snd_cs4231_mixer(chip)) < 0) {
1090 snd_printk(KERN_ERR "sscape: No mixer device for AD1845 chip\n");
1094 if ((err = snd_ctl_add(card, snd_ctl_new1(&midi_mixer_ctl, chip))) < 0) {
1095 snd_printk(KERN_ERR "sscape: Could not create MIDI mixer control\n");
1099 strcpy(card->driver, "SoundScape");
1100 strcpy(card->shortname, pcm->name);
1101 snprintf(card->longname, sizeof(card->longname),
1102 "%s at 0x%lx, IRQ %d, DMA %d\n",
1103 pcm->name, chip->port, chip->irq, chip->dma1);
1104 chip->set_playback_format = ad1845_playback_format;
1105 chip->set_capture_format = ad1845_capture_format;
1106 sscape->chip = chip;
1125 static inline struct params*
1126 init_params(struct params *params,
1134 params->index = index;
1136 params->port = port;
1138 params->mpu_irq = mpu_irq;
1139 params->dma1 = (dma1 & 0x03);
1146 * Create an ALSA soundcard entry for the SoundScape, using
1147 * the given list of port, IRQ and DMA resources.
1149 static int __devinit create_sscape(const struct params *params, snd_card_t **rcardp)
1152 register struct soundscape *sscape;
1153 register unsigned dma_cfg;
1155 unsigned mpu_irq_cfg;
1156 struct resource *io_res;
1157 unsigned long flags;
1161 * Check that the user didn't pass us garbage data ...
1163 irq_cfg = get_irq_config(params->irq);
1164 if (irq_cfg == INVALID_IRQ) {
1165 snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", params->irq);
1169 mpu_irq_cfg = get_irq_config(params->mpu_irq);
1170 if (mpu_irq_cfg == INVALID_IRQ) {
1171 printk(KERN_ERR "sscape: Invalid IRQ %d\n", params->mpu_irq);
1176 * Grab IO ports that we will need to probe so that we
1177 * can detect and control this hardware ...
1179 if ((io_res = request_region(params->port, 8, "SoundScape")) == NULL) {
1180 snd_printk(KERN_ERR "sscape: can't grab port 0x%x\n", params->port);
1185 * Grab both DMA channels (OK, only one for now) ...
1187 if ((err = request_dma(params->dma1, "SoundScape")) < 0) {
1188 snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", params->dma1);
1189 goto _release_region;
1193 * Create a new ALSA sound card entry, in anticipation
1194 * of detecting our hardware ...
1196 if ((card = snd_card_new(params->index, params->id, THIS_MODULE, sizeof(struct soundscape))) == NULL) {
1201 sscape = get_card_soundscape(card);
1202 spin_lock_init(&sscape->lock);
1203 spin_lock_init(&sscape->fwlock);
1204 sscape->io_res = io_res;
1205 sscape->io_base = params->port;
1207 if (!detect_sscape(sscape)) {
1208 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1213 printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
1214 sscape->io_base, params->irq, params->dma1);
1217 * Now create the hardware-specific device so that we can
1218 * load the microcode into the on-board processor.
1219 * We cannot use the MPU-401 MIDI system until this firmware
1220 * has been loaded into the card.
1222 if ((err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw))) < 0) {
1223 printk(KERN_ERR "sscape: Failed to create firmware device\n");
1226 strlcpy(sscape->hw->name, "SoundScape M68K", sizeof(sscape->hw->name));
1227 sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
1228 sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
1229 sscape->hw->ops.open = sscape_hw_open;
1230 sscape->hw->ops.release = sscape_hw_release;
1231 sscape->hw->ops.ioctl = sscape_hw_ioctl;
1232 sscape->hw->private_data = sscape;
1235 * Tell the on-board devices where their resources are (I think -
1236 * I can't be sure without a datasheet ... So many magic values!)
1238 spin_lock_irqsave(&sscape->lock, flags);
1240 activate_ad1845_unsafe(sscape->io_base);
1242 sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
1243 sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
1244 sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
1247 * Enable and configure the DMA channels ...
1249 sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
1250 dma_cfg = (sscape->ic_type == IC_ODIE ? 0x70 : 0x40);
1251 sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
1252 sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
1254 sscape_write_unsafe(sscape->io_base,
1255 GA_INTCFG_REG, 0xf0 | (mpu_irq_cfg << 2) | mpu_irq_cfg);
1256 sscape_write_unsafe(sscape->io_base,
1257 GA_CDCFG_REG, 0x09 | DMA_8BIT | (params->dma1 << 4) | (irq_cfg << 1));
1259 spin_unlock_irqrestore(&sscape->lock, flags);
1262 * We have now enabled the codec chip, and so we should
1263 * detect the AD1845 device ...
1265 if ((err = create_ad1845(card, CODEC_IO(params->port), params->irq, params->dma1)) < 0) {
1266 printk(KERN_ERR "sscape: No AD1845 device at 0x%x, IRQ %d\n",
1267 CODEC_IO(params->port), params->irq);
1270 #define MIDI_DEVNUM 0
1271 if ((err = create_mpu401(card, MIDI_DEVNUM, MPU401_IO(params->port), params->mpu_irq)) < 0) {
1272 printk(KERN_ERR "sscape: Failed to create MPU-401 device at 0x%x\n",
1273 MPU401_IO(params->port));
1278 * Enable the master IRQ ...
1280 sscape_write(sscape, GA_INTENA_REG, 0x80);
1282 if ((err = snd_card_register(card)) < 0) {
1283 printk(KERN_ERR "sscape: Failed to register sound card\n");
1290 sscape->midi_vol = 0;
1291 host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
1292 host_write_ctrl_unsafe(sscape->io_base, 0, 100);
1293 host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
1296 * Now that we have successfully created this sound card,
1297 * it is safe to store the pointer.
1298 * NOTE: we only register the sound card's "destructor"
1299 * function now that our "constructor" has completed.
1301 card->private_free = soundscape_free;
1307 snd_card_free(card);
1310 free_dma(params->dma1);
1313 release_resource(io_res);
1314 kfree_nocheck(io_res);
1320 static int sscape_cards __devinitdata;
1321 static struct params sscape_params[SNDRV_CARDS] __devinitdata;
1324 static inline int __devinit get_next_autoindex(int i)
1326 while ((i < SNDRV_CARDS) && (port[i] != SNDRV_AUTO_PORT)) {
1334 static inline int __devinit is_port_known(unsigned io, struct params *params, int cards)
1336 while (--cards >= 0) {
1337 if (params[cards].port == io)
1344 static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1345 const struct pnp_card_device_id *pid)
1347 struct pnp_dev *dev;
1352 * Allow this function to fail *quietly* if all the ISA PnP
1353 * devices were configured using module parameters instead.
1355 if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS) {
1360 * We have found a candidate ISA PnP card. Now we
1361 * have to check that it has the devices that we
1362 * expect it to have.
1364 * We will NOT try and autoconfigure all of the resources
1365 * needed and then activate the card as we are assuming that
1366 * has already been done at boot-time using /proc/isapnp.
1367 * We shall simply try to give each active card the resources
1368 * that it wants. This is a sensible strategy for a modular
1369 * system where unused modules are unloaded regularly.
1371 * This strategy is utterly useless if we compile the driver
1372 * into the kernel, of course.
1374 // printk(KERN_INFO "sscape: %s\n", card->name);
1377 * Check that we still have room for another sound card ...
1379 if (sscape_cards >= SNDRV_CARDS) {
1380 printk(KERN_ERR "sscape: No room for another ALSA device\n");
1386 dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
1388 struct params *this;
1389 if (!pnp_is_active(dev)) {
1390 if (pnp_activate_dev(dev) < 0) {
1391 printk(KERN_INFO "sscape: device is inactive\n");
1396 * Read the correct parameters off the ISA PnP bus ...
1398 this = init_params(&sscape_params[sscape_cards],
1401 pnp_port_start(dev, 0),
1407 * Do we know about this sound card already?
1409 if ( !is_port_known(this->port, sscape_params, sscape_cards) ) {
1412 ret = create_sscape(this, &card);
1415 snd_card_set_dev(card, &pcard->card->dev);
1416 pnp_set_card_drvdata(pcard, card);
1425 static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1427 snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
1429 pnp_set_card_drvdata(pcard, NULL);
1430 snd_card_disconnect(card);
1431 snd_card_free_in_thread(card);
1434 static struct pnp_card_driver sscape_pnpc_driver = {
1435 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1437 .id_table = sscape_pnpids,
1438 .probe = sscape_pnp_detect,
1439 .remove = __devexit_p(sscape_pnp_remove),
1442 #endif /* CONFIG_PNP */
1444 static int __init sscape_manual_probe(struct params *params)
1450 for (i = 0; i < SNDRV_CARDS; ++i) {
1452 * We do NOT probe for ports.
1453 * If we're not given a port number for this
1454 * card then we completely ignore this line
1457 if (port[i] == SNDRV_AUTO_PORT)
1461 * Make sure we were given ALL of the other parameters.
1463 if ( (irq[i] == SNDRV_AUTO_IRQ) ||
1464 (mpu_irq[i] == SNDRV_AUTO_IRQ) ||
1465 (dma[i] == SNDRV_AUTO_DMA) ) {
1467 "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1472 * This cards looks OK ...
1474 init_params(params, index[i], id[i], port[i], irq[i], mpu_irq[i], dma[i]);
1476 ret = create_sscape(params, &card);
1480 sscape_card[sscape_cards] = card;
1489 static void sscape_exit(void)
1494 pnp_unregister_card_driver(&sscape_pnpc_driver);
1496 for (i = 0; i < ARRAY_SIZE(sscape_card); ++i) {
1497 snd_card_free(sscape_card[i]);
1502 static int __init sscape_init(void)
1507 * First check whether we were passed any parameters.
1508 * These MUST take precedence over ANY automatic way
1509 * of allocating cards, because the operator is
1510 * S-P-E-L-L-I-N-G it out for us...
1512 ret = sscape_manual_probe(sscape_params);
1515 for (i = 0; i < sscape_cards; ++i)
1516 snd_card_free(sscape_card[i]);
1521 if (sscape_cards < SNDRV_CARDS) {
1522 ret = pnp_register_card_driver(&sscape_pnpc_driver);
1533 module_init(sscape_init);
1534 module_exit(sscape_exit);