[ALSA] sscape: driver extension to 2nd DMA and WSS port
[safe/jmp/linux-2.6] / sound / isa / sscape.c
1 /*
2  *   Low-level ALSA driver for the ENSONIQ SoundScape PnP
3  *   Copyright (c) by Chris Rankin
4  *
5  *   This driver was written in part using information obtained from
6  *   the OSS/Free SoundScape driver, written by Hannu Savolainen.
7  *
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/isa.h>
28 #include <linux/delay.h>
29 #include <linux/pnp.h>
30 #include <linux/spinlock.h>
31 #include <linux/moduleparam.h>
32 #include <asm/dma.h>
33 #include <sound/core.h>
34 #include <sound/hwdep.h>
35 #include <sound/cs4231.h>
36 #include <sound/mpu401.h>
37 #include <sound/initval.h>
38
39 #include <sound/sscape_ioctl.h>
40
41
42 MODULE_AUTHOR("Chris Rankin");
43 MODULE_DESCRIPTION("ENSONIQ SoundScape PnP driver");
44 MODULE_LICENSE("GPL");
45
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 = SNDRV_DEFAULT_PORT;
49 static long wss_port[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_PORT;
50 static int irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
51 static int mpu_irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
52 static int dma[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
53 static int dma2[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
54
55 module_param_array(index, int, NULL, 0444);
56 MODULE_PARM_DESC(index, "Index number for SoundScape soundcard");
57
58 module_param_array(id, charp, NULL, 0444);
59 MODULE_PARM_DESC(id, "Description for SoundScape card");
60
61 module_param_array(port, long, NULL, 0444);
62 MODULE_PARM_DESC(port, "Port # for SoundScape driver.");
63
64 module_param_array(wss_port, long, NULL, 0444);
65 MODULE_PARM_DESC(wss_port, "WSS Port # for SoundScape driver.");
66
67 module_param_array(irq, int, NULL, 0444);
68 MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver.");
69
70 module_param_array(mpu_irq, int, NULL, 0444);
71 MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver.");
72
73 module_param_array(dma, int, NULL, 0444);
74 MODULE_PARM_DESC(dma, "DMA # for SoundScape driver.");
75
76 module_param_array(dma2, int, NULL, 0444);
77 MODULE_PARM_DESC(dma2, "DMA2 # for SoundScape driver.");
78
79 #ifdef CONFIG_PNP
80 static int isa_registered;
81 static int pnp_registered;
82
83 static struct pnp_card_device_id sscape_pnpids[] = {
84         { .id = "ENS3081", .devs = { { "ENS0000" } } }, /* Soundscape PnP */
85         { .id = "ENS4081", .devs = { { "ENS1011" } } }, /* VIVO90 */
86         { .id = "" }    /* end */
87 };
88
89 MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
90 #endif
91
92
93 #define MPU401_IO(i)     ((i) + 0)
94 #define MIDI_DATA_IO(i)  ((i) + 0)
95 #define MIDI_CTRL_IO(i)  ((i) + 1)
96 #define HOST_CTRL_IO(i)  ((i) + 2)
97 #define HOST_DATA_IO(i)  ((i) + 3)
98 #define ODIE_ADDR_IO(i)  ((i) + 4)
99 #define ODIE_DATA_IO(i)  ((i) + 5)
100 #define CODEC_IO(i)      ((i) + 8)
101
102 #define IC_ODIE  1
103 #define IC_OPUS  2
104
105 #define RX_READY 0x01
106 #define TX_READY 0x02
107
108 #define CMD_ACK           0x80
109 #define CMD_SET_MIDI_VOL  0x84
110 #define CMD_GET_MIDI_VOL  0x85
111 #define CMD_XXX_MIDI_VOL  0x86
112 #define CMD_SET_EXTMIDI   0x8a
113 #define CMD_GET_EXTMIDI   0x8b
114 #define CMD_SET_MT32      0x8c
115 #define CMD_GET_MT32      0x8d
116
117 enum GA_REG {
118         GA_INTSTAT_REG = 0,
119         GA_INTENA_REG,
120         GA_DMAA_REG,
121         GA_DMAB_REG,
122         GA_INTCFG_REG,
123         GA_DMACFG_REG,
124         GA_CDCFG_REG,
125         GA_SMCFGA_REG,
126         GA_SMCFGB_REG,
127         GA_HMCTL_REG
128 };
129
130 #define DMA_8BIT  0x80
131
132
133 #define AD1845_FREQ_SEL_MSB    0x16
134 #define AD1845_FREQ_SEL_LSB    0x17
135
136 enum card_type {
137         SSCAPE,
138         SSCAPE_PNP,
139         SSCAPE_VIVO,
140 };
141
142 struct soundscape {
143         spinlock_t lock;
144         unsigned io_base;
145         int codec_type;
146         int ic_type;
147         enum card_type type;
148         struct resource *io_res;
149         struct snd_cs4231 *chip;
150         struct snd_mpu401 *mpu;
151         struct snd_hwdep *hw;
152
153         /*
154          * The MIDI device won't work until we've loaded
155          * its firmware via a hardware-dependent device IOCTL
156          */
157         spinlock_t fwlock;
158         int hw_in_use;
159         unsigned long midi_usage;
160         unsigned char midi_vol;
161 };
162
163 #define INVALID_IRQ  ((unsigned)-1)
164
165
166 static inline struct soundscape *get_card_soundscape(struct snd_card *c)
167 {
168         return (struct soundscape *) (c->private_data);
169 }
170
171 static inline struct soundscape *get_mpu401_soundscape(struct snd_mpu401 * mpu)
172 {
173         return (struct soundscape *) (mpu->private_data);
174 }
175
176 static inline struct soundscape *get_hwdep_soundscape(struct snd_hwdep * hw)
177 {
178         return (struct soundscape *) (hw->private_data);
179 }
180
181
182 /*
183  * Allocates some kernel memory that we can use for DMA.
184  * I think this means that the memory has to map to
185  * contiguous pages of physical memory.
186  */
187 static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
188 {
189         if (buf) {
190                 if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
191                                                  size, buf) < 0) {
192                         snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", size);
193                         return NULL;
194                 }
195         }
196
197         return buf;
198 }
199
200 /*
201  * Release the DMA-able kernel memory ...
202  */
203 static void free_dmabuf(struct snd_dma_buffer *buf)
204 {
205         if (buf && buf->area)
206                 snd_dma_free_pages(buf);
207 }
208
209
210 /*
211  * This function writes to the SoundScape's control registers,
212  * but doesn't do any locking. It's up to the caller to do that.
213  * This is why this function is "unsafe" ...
214  */
215 static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
216 {
217         outb(reg, ODIE_ADDR_IO(io_base));
218         outb(val, ODIE_DATA_IO(io_base));
219 }
220
221 /*
222  * Write to the SoundScape's control registers, and do the
223  * necessary locking ...
224  */
225 static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
226 {
227         unsigned long flags;
228
229         spin_lock_irqsave(&s->lock, flags);
230         sscape_write_unsafe(s->io_base, reg, val);
231         spin_unlock_irqrestore(&s->lock, flags);
232 }
233
234 /*
235  * Read from the SoundScape's control registers, but leave any
236  * locking to the caller. This is why the function is "unsafe" ...
237  */
238 static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
239 {
240         outb(reg, ODIE_ADDR_IO(io_base));
241         return inb(ODIE_DATA_IO(io_base));
242 }
243
244 /*
245  * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
246  */
247 static inline void set_host_mode_unsafe(unsigned io_base)
248 {
249         outb(0x0, HOST_CTRL_IO(io_base));
250 }
251
252 /*
253  * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
254  */
255 static inline void set_midi_mode_unsafe(unsigned io_base)
256 {
257         outb(0x3, HOST_CTRL_IO(io_base));
258 }
259
260 /*
261  * Read the SoundScape's host-mode control register, but leave
262  * any locking issues to the caller ...
263  */
264 static inline int host_read_unsafe(unsigned io_base)
265 {
266         int data = -1;
267         if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
268                 data = inb(HOST_DATA_IO(io_base));
269         }
270
271         return data;
272 }
273
274 /*
275  * Read the SoundScape's host-mode control register, performing
276  * a limited amount of busy-waiting if the register isn't ready.
277  * Also leaves all locking-issues to the caller ...
278  */
279 static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
280 {
281         int data;
282
283         while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
284                 udelay(100);
285                 --timeout;
286         } /* while */
287
288         return data;
289 }
290
291 /*
292  * Write to the SoundScape's host-mode control registers, but
293  * leave any locking issues to the caller ...
294  */
295 static inline int host_write_unsafe(unsigned io_base, unsigned char data)
296 {
297         if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
298                 outb(data, HOST_DATA_IO(io_base));
299                 return 1;
300         }
301
302         return 0;
303 }
304
305 /*
306  * Write to the SoundScape's host-mode control registers, performing
307  * a limited amount of busy-waiting if the register isn't ready.
308  * Also leaves all locking-issues to the caller ...
309  */
310 static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
311                                   unsigned timeout)
312 {
313         int err;
314
315         while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
316                 udelay(100);
317                 --timeout;
318         } /* while */
319
320         return err;
321 }
322
323
324 /*
325  * Check that the MIDI subsystem is operational. If it isn't,
326  * then we will hang the computer if we try to use it ...
327  *
328  * NOTE: This check is based upon observation, not documentation.
329  */
330 static inline int verify_mpu401(const struct snd_mpu401 * mpu)
331 {
332         return ((inb(MIDI_CTRL_IO(mpu->port)) & 0xc0) == 0x80);
333 }
334
335 /*
336  * This is apparently the standard way to initailise an MPU-401
337  */
338 static inline void initialise_mpu401(const struct snd_mpu401 * mpu)
339 {
340         outb(0, MIDI_DATA_IO(mpu->port));
341 }
342
343 /*
344  * Tell the SoundScape to activate the AD1845 chip (I think).
345  * The AD1845 detection fails if we *don't* do this, so I
346  * think that this is a good idea ...
347  */
348 static inline void activate_ad1845_unsafe(unsigned io_base)
349 {
350         sscape_write_unsafe(io_base, GA_HMCTL_REG, (sscape_read_unsafe(io_base, GA_HMCTL_REG) & 0xcf) | 0x10);
351         sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
352 }
353
354 /*
355  * Do the necessary ALSA-level cleanup to deallocate our driver ...
356  */
357 static void soundscape_free(struct snd_card *c)
358 {
359         register struct soundscape *sscape = get_card_soundscape(c);
360         release_and_free_resource(sscape->io_res);
361         free_dma(sscape->chip->dma1);
362 }
363
364 /*
365  * Tell the SoundScape to begin a DMA tranfer using the given channel.
366  * All locking issues are left to the caller.
367  */
368 static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
369 {
370         sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) | 0x01);
371         sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) & 0xfe);
372 }
373
374 /*
375  * Wait for a DMA transfer to complete. This is a "limited busy-wait",
376  * and all locking issues are left to the caller.
377  */
378 static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
379 {
380         while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
381                 udelay(100);
382                 --timeout;
383         } /* while */
384
385         return (sscape_read_unsafe(io_base, reg) & 0x01);
386 }
387
388 /*
389  * Wait for the On-Board Processor to return its start-up
390  * acknowledgement sequence. This wait is too long for
391  * us to perform "busy-waiting", and so we must sleep.
392  * This in turn means that we must not be holding any
393  * spinlocks when we call this function.
394  */
395 static int obp_startup_ack(struct soundscape *s, unsigned timeout)
396 {
397         while (timeout != 0) {
398                 unsigned long flags;
399                 unsigned char x;
400
401                 schedule_timeout(1);
402
403                 spin_lock_irqsave(&s->lock, flags);
404                 x = inb(HOST_DATA_IO(s->io_base));
405                 spin_unlock_irqrestore(&s->lock, flags);
406                 if ((x & 0xfe) == 0xfe)
407                         return 1;
408
409                 --timeout;
410         } /* while */
411
412         return 0;
413 }
414
415 /*
416  * Wait for the host to return its start-up acknowledgement
417  * sequence. This wait is too long for us to perform
418  * "busy-waiting", and so we must sleep. This in turn means
419  * that we must not be holding any spinlocks when we call
420  * this function.
421  */
422 static int host_startup_ack(struct soundscape *s, unsigned timeout)
423 {
424         while (timeout != 0) {
425                 unsigned long flags;
426                 unsigned char x;
427
428                 schedule_timeout(1);
429
430                 spin_lock_irqsave(&s->lock, flags);
431                 x = inb(HOST_DATA_IO(s->io_base));
432                 spin_unlock_irqrestore(&s->lock, flags);
433                 if (x == 0xfe)
434                         return 1;
435
436                 --timeout;
437         } /* while */
438
439         return 0;
440 }
441
442 /*
443  * Upload a byte-stream into the SoundScape using DMA channel A.
444  */
445 static int upload_dma_data(struct soundscape *s,
446                            const unsigned char __user *data,
447                            size_t size)
448 {
449         unsigned long flags;
450         struct snd_dma_buffer dma;
451         int ret;
452
453         if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
454                 return -ENOMEM;
455
456         spin_lock_irqsave(&s->lock, flags);
457
458         /*
459          * Reset the board ...
460          */
461         sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
462
463         /*
464          * Enable the DMA channels and configure them ...
465          */
466         sscape_write_unsafe(s->io_base, GA_DMACFG_REG, 0x50);
467         sscape_write_unsafe(s->io_base, GA_DMAA_REG, (s->chip->dma1 << 4) | DMA_8BIT);
468         sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
469
470         /*
471          * Take the board out of reset ...
472          */
473         sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
474
475         /*
476          * Upload the user's data (firmware?) to the SoundScape
477          * board through the DMA channel ...
478          */
479         while (size != 0) {
480                 unsigned long len;
481
482                 /*
483                  * Apparently, copying to/from userspace can sleep.
484                  * We are therefore forbidden from holding any
485                  * spinlocks while we copy ...
486                  */
487                 spin_unlock_irqrestore(&s->lock, flags);
488
489                 /*
490                  * Remember that the data that we want to DMA
491                  * comes from USERSPACE. We have already verified
492                  * the userspace pointer ...
493                  */
494                 len = min(size, dma.bytes);
495                 len -= __copy_from_user(dma.area, data, len);
496                 data += len;
497                 size -= len;
498
499                 /*
500                  * Grab that spinlock again, now that we've
501                  * finished copying!
502                  */
503                 spin_lock_irqsave(&s->lock, flags);
504
505                 snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
506                 sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
507                 if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
508                         /*
509                          * Don't forget to release this spinlock we're holding ...
510                          */
511                         spin_unlock_irqrestore(&s->lock, flags);
512
513                         snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
514                         ret = -EAGAIN;
515                         goto _release_dma;
516                 }
517         } /* while */
518
519         set_host_mode_unsafe(s->io_base);
520
521         /*
522          * Boot the board ... (I think)
523          */
524         sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x40);
525         spin_unlock_irqrestore(&s->lock, flags);
526
527         /*
528          * If all has gone well, then the board should acknowledge
529          * the new upload and tell us that it has rebooted OK. We
530          * give it 5 seconds (max) ...
531          */
532         ret = 0;
533         if (!obp_startup_ack(s, 5)) {
534                 snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
535                 ret = -EAGAIN;
536         } else if (!host_startup_ack(s, 5)) {
537                 snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
538                 ret = -EAGAIN;
539         }
540
541 _release_dma:
542         /*
543          * NOTE!!! We are NOT holding any spinlocks at this point !!!
544          */
545         sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
546         free_dmabuf(&dma);
547
548         return ret;
549 }
550
551 /*
552  * Upload the bootblock(?) into the SoundScape. The only
553  * purpose of this block of code seems to be to tell
554  * us which version of the microcode we should be using.
555  *
556  * NOTE: The boot-block data resides in USER-SPACE!!!
557  *       However, we have already verified its memory
558  *       addresses by the time we get here.
559  */
560 static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock __user *bb)
561 {
562         unsigned long flags;
563         int data = 0;
564         int ret;
565
566         ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
567
568         spin_lock_irqsave(&sscape->lock, flags);
569         if (ret == 0) {
570                 data = host_read_ctrl_unsafe(sscape->io_base, 100);
571         }
572         set_midi_mode_unsafe(sscape->io_base);
573         spin_unlock_irqrestore(&sscape->lock, flags);
574
575         if (ret == 0) {
576                 if (data < 0) {
577                         snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
578                         ret = -EAGAIN;
579                 }
580                 else if (__copy_to_user(&bb->version, &data, sizeof(bb->version))) {
581                         ret = -EFAULT;
582                 }
583         }
584
585         return ret;
586 }
587
588 /*
589  * Upload the microcode into the SoundScape. The
590  * microcode is 64K of data, and if we try to copy
591  * it into a local variable then we will SMASH THE
592  * KERNEL'S STACK! We therefore leave it in USER
593  * SPACE, and save ourselves from copying it at all.
594  */
595 static int sscape_upload_microcode(struct soundscape *sscape,
596                                    const struct sscape_microcode __user *mc)
597 {
598         unsigned long flags;
599         char __user *code;
600         int err;
601
602         /*
603          * We are going to have to copy this data into a special
604          * DMA-able buffer before we can upload it. We shall therefore
605          * just check that the data pointer is valid for now.
606          *
607          * NOTE: This buffer is 64K long! That's WAY too big to
608          *       copy into a stack-temporary anyway.
609          */
610         if ( get_user(code, &mc->code) ||
611              !access_ok(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE) )
612                 return -EFAULT;
613
614         if ((err = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
615                 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
616         }
617
618         spin_lock_irqsave(&sscape->lock, flags);
619         set_midi_mode_unsafe(sscape->io_base);
620         spin_unlock_irqrestore(&sscape->lock, flags);
621
622         initialise_mpu401(sscape->mpu);
623
624         return err;
625 }
626
627 /*
628  * Hardware-specific device functions, to implement special
629  * IOCTLs for the SoundScape card. This is how we upload
630  * the microcode into the card, for example, and so we
631  * must ensure that no two processes can open this device
632  * simultaneously, and that we can't open it at all if
633  * someone is using the MIDI device.
634  */
635 static int sscape_hw_open(struct snd_hwdep * hw, struct file *file)
636 {
637         register struct soundscape *sscape = get_hwdep_soundscape(hw);
638         unsigned long flags;
639         int err;
640
641         spin_lock_irqsave(&sscape->fwlock, flags);
642
643         if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
644                 err = -EBUSY;
645         } else {
646                 sscape->hw_in_use = 1;
647                 err = 0;
648         }
649
650         spin_unlock_irqrestore(&sscape->fwlock, flags);
651         return err;
652 }
653
654 static int sscape_hw_release(struct snd_hwdep * hw, struct file *file)
655 {
656         register struct soundscape *sscape = get_hwdep_soundscape(hw);
657         unsigned long flags;
658
659         spin_lock_irqsave(&sscape->fwlock, flags);
660         sscape->hw_in_use = 0;
661         spin_unlock_irqrestore(&sscape->fwlock, flags);
662         return 0;
663 }
664
665 static int sscape_hw_ioctl(struct snd_hwdep * hw, struct file *file,
666                            unsigned int cmd, unsigned long arg)
667 {
668         struct soundscape *sscape = get_hwdep_soundscape(hw);
669         int err = -EBUSY;
670
671         switch (cmd) {
672         case SND_SSCAPE_LOAD_BOOTB:
673                 {
674                         register struct sscape_bootblock __user *bb = (struct sscape_bootblock __user *) arg;
675
676                         /*
677                          * We are going to have to copy this data into a special
678                          * DMA-able buffer before we can upload it. We shall therefore
679                          * just check that the data pointer is valid for now ...
680                          */
681                         if ( !access_ok(VERIFY_READ, bb->code, sizeof(bb->code)) )
682                                 return -EFAULT;
683
684                         /*
685                          * Now check that we can write the firmware version number too...
686                          */
687                         if ( !access_ok(VERIFY_WRITE, &bb->version, sizeof(bb->version)) )
688                                 return -EFAULT;
689
690                         err = sscape_upload_bootblock(sscape, bb);
691                 }
692                 break;
693
694         case SND_SSCAPE_LOAD_MCODE:
695                 {
696                         register const struct sscape_microcode __user *mc = (const struct sscape_microcode __user *) arg;
697
698                         err = sscape_upload_microcode(sscape, mc);
699                 }
700                 break;
701
702         default:
703                 err = -EINVAL;
704                 break;
705         } /* switch */
706
707         return err;
708 }
709
710
711 /*
712  * Mixer control for the SoundScape's MIDI device.
713  */
714 static int sscape_midi_info(struct snd_kcontrol *ctl,
715                             struct snd_ctl_elem_info *uinfo)
716 {
717         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
718         uinfo->count = 1;
719         uinfo->value.integer.min = 0;
720         uinfo->value.integer.max = 127;
721         return 0;
722 }
723
724 static int sscape_midi_get(struct snd_kcontrol *kctl,
725                            struct snd_ctl_elem_value *uctl)
726 {
727         struct snd_cs4231 *chip = snd_kcontrol_chip(kctl);
728         struct snd_card *card = chip->card;
729         register struct soundscape *s = get_card_soundscape(card);
730         unsigned long flags;
731
732         spin_lock_irqsave(&s->lock, flags);
733         set_host_mode_unsafe(s->io_base);
734
735         if (host_write_ctrl_unsafe(s->io_base, CMD_GET_MIDI_VOL, 100)) {
736                 uctl->value.integer.value[0] = host_read_ctrl_unsafe(s->io_base, 100);
737         }
738
739         set_midi_mode_unsafe(s->io_base);
740         spin_unlock_irqrestore(&s->lock, flags);
741         return 0;
742 }
743
744 static int sscape_midi_put(struct snd_kcontrol *kctl,
745                            struct snd_ctl_elem_value *uctl)
746 {
747         struct snd_cs4231 *chip = snd_kcontrol_chip(kctl);
748         struct snd_card *card = chip->card;
749         register struct soundscape *s = get_card_soundscape(card);
750         unsigned long flags;
751         int change;
752
753         spin_lock_irqsave(&s->lock, flags);
754
755         /*
756          * We need to put the board into HOST mode before we
757          * can send any volume-changing HOST commands ...
758          */
759         set_host_mode_unsafe(s->io_base);
760
761         /*
762          * To successfully change the MIDI volume setting, you seem to
763          * have to write a volume command, write the new volume value,
764          * and then perform another volume-related command. Perhaps the
765          * first command is an "open" and the second command is a "close"?
766          */
767         if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
768                 change = 0;
769                 goto __skip_change;
770         }
771         change = (host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
772                   && host_write_ctrl_unsafe(s->io_base, ((unsigned char) uctl->value.integer. value[0]) & 127, 100)
773                   && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100));
774       __skip_change:
775
776         /*
777          * Take the board out of HOST mode and back into MIDI mode ...
778          */
779         set_midi_mode_unsafe(s->io_base);
780
781         spin_unlock_irqrestore(&s->lock, flags);
782         return change;
783 }
784
785 static struct snd_kcontrol_new midi_mixer_ctl = {
786         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
787         .name = "MIDI",
788         .info = sscape_midi_info,
789         .get = sscape_midi_get,
790         .put = sscape_midi_put
791 };
792
793 /*
794  * The SoundScape can use two IRQs from a possible set of four.
795  * These IRQs are encoded as bit patterns so that they can be
796  * written to the control registers.
797  */
798 static unsigned __devinit get_irq_config(int irq)
799 {
800         static const int valid_irq[] = { 9, 5, 7, 10 };
801         unsigned cfg;
802
803         for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) {
804                 if (irq == valid_irq[cfg])
805                         return cfg;
806         } /* for */
807
808         return INVALID_IRQ;
809 }
810
811
812 /*
813  * Perform certain arcane port-checks to see whether there
814  * is a SoundScape board lurking behind the given ports.
815  */
816 static int __devinit detect_sscape(struct soundscape *s)
817 {
818         unsigned long flags;
819         unsigned d;
820         int retval = 0;
821
822         spin_lock_irqsave(&s->lock, flags);
823
824         /*
825          * The following code is lifted from the original OSS driver,
826          * and as I don't have a datasheet I cannot really comment
827          * on what it is doing...
828          */
829         if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
830                 goto _done;
831
832         d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
833         if ((d & 0x80) != 0)
834                 goto _done;
835
836         if (d == 0) {
837                 s->codec_type = 1;
838                 s->ic_type = IC_ODIE;
839         } else if ((d & 0x60) != 0) {
840                 s->codec_type = 2;
841                 s->ic_type = IC_OPUS;
842         } else
843                 goto _done;
844
845         outb(0xfa, ODIE_ADDR_IO(s->io_base));
846         if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
847                 goto _done;
848
849         outb(0xfe, ODIE_ADDR_IO(s->io_base));
850         if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
851                 goto _done;
852         if ((inb(ODIE_DATA_IO(s->io_base)) & 0x9f) != 0x0e)
853                 goto _done;
854
855         /*
856          * SoundScape successfully detected!
857          */
858         retval = 1;
859
860         _done:
861         spin_unlock_irqrestore(&s->lock, flags);
862         return retval;
863 }
864
865 /*
866  * ALSA callback function, called when attempting to open the MIDI device.
867  * Check that the MIDI firmware has been loaded, because we don't want
868  * to crash the machine. Also check that someone isn't using the hardware
869  * IOCTL device.
870  */
871 static int mpu401_open(struct snd_mpu401 * mpu)
872 {
873         int err;
874
875         if (!verify_mpu401(mpu)) {
876                 snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
877                 err = -ENODEV;
878         } else {
879                 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
880                 unsigned long flags;
881
882                 spin_lock_irqsave(&sscape->fwlock, flags);
883
884                 if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
885                         err = -EBUSY;
886                 } else {
887                         ++(sscape->midi_usage);
888                         err = 0;
889                 }
890
891                 spin_unlock_irqrestore(&sscape->fwlock, flags);
892         }
893
894         return err;
895 }
896
897 static void mpu401_close(struct snd_mpu401 * mpu)
898 {
899         register struct soundscape *sscape = get_mpu401_soundscape(mpu);
900         unsigned long flags;
901
902         spin_lock_irqsave(&sscape->fwlock, flags);
903         --(sscape->midi_usage);
904         spin_unlock_irqrestore(&sscape->fwlock, flags);
905 }
906
907 /*
908  * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
909  */
910 static int __devinit create_mpu401(struct snd_card *card, int devnum, unsigned long port, int irq)
911 {
912         struct soundscape *sscape = get_card_soundscape(card);
913         struct snd_rawmidi *rawmidi;
914         int err;
915
916         if ((err = snd_mpu401_uart_new(card, devnum,
917                                        MPU401_HW_MPU401,
918                                        port, MPU401_INFO_INTEGRATED,
919                                        irq, IRQF_DISABLED,
920                                        &rawmidi)) == 0) {
921                 struct snd_mpu401 *mpu = (struct snd_mpu401 *) rawmidi->private_data;
922                 mpu->open_input = mpu401_open;
923                 mpu->open_output = mpu401_open;
924                 mpu->close_input = mpu401_close;
925                 mpu->close_output = mpu401_close;
926                 mpu->private_data = sscape;
927                 sscape->mpu = mpu;
928
929                 initialise_mpu401(mpu);
930         }
931
932         return err;
933 }
934
935
936 /*
937  * Override for the CS4231 playback format function.
938  * The AD1845 has much simpler format and rate selection.
939  */
940 static void ad1845_playback_format(struct snd_cs4231 * chip, struct snd_pcm_hw_params *params, unsigned char format)
941 {
942         unsigned long flags;
943         unsigned rate = params_rate(params);
944
945         /*
946          * The AD1845 can't handle sample frequencies
947          * outside of 4 kHZ to 50 kHZ
948          */
949         if (rate > 50000)
950                 rate = 50000;
951         else if (rate < 4000)
952                 rate = 4000;
953
954         spin_lock_irqsave(&chip->reg_lock, flags);
955
956         /*
957          * Program the AD1845 correctly for the playback stream.
958          * Note that we do NOT need to toggle the MCE bit because
959          * the PLAYBACK_ENABLE bit of the Interface Configuration
960          * register is set.
961          * 
962          * NOTE: We seem to need to write to the MSB before the LSB
963          *       to get the correct sample frequency.
964          */
965         snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, (format & 0xf0));
966         snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
967         snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
968
969         spin_unlock_irqrestore(&chip->reg_lock, flags);
970 }
971
972 /*
973  * Override for the CS4231 capture format function. 
974  * The AD1845 has much simpler format and rate selection.
975  */
976 static void ad1845_capture_format(struct snd_cs4231 * chip, struct snd_pcm_hw_params *params, unsigned char format)
977 {
978         unsigned long flags;
979         unsigned rate = params_rate(params);
980
981         /*
982          * The AD1845 can't handle sample frequencies 
983          * outside of 4 kHZ to 50 kHZ
984          */
985         if (rate > 50000)
986                 rate = 50000;
987         else if (rate < 4000)
988                 rate = 4000;
989
990         spin_lock_irqsave(&chip->reg_lock, flags);
991
992         /*
993          * Program the AD1845 correctly for the playback stream.
994          * Note that we do NOT need to toggle the MCE bit because
995          * the CAPTURE_ENABLE bit of the Interface Configuration
996          * register is set.
997          *
998          * NOTE: We seem to need to write to the MSB before the LSB
999          *       to get the correct sample frequency.
1000          */
1001         snd_cs4231_out(chip, CS4231_REC_FORMAT, (format & 0xf0));
1002         snd_cs4231_out(chip, AD1845_FREQ_SEL_MSB, (unsigned char) (rate >> 8));
1003         snd_cs4231_out(chip, AD1845_FREQ_SEL_LSB, (unsigned char) rate);
1004
1005         spin_unlock_irqrestore(&chip->reg_lock, flags);
1006 }
1007
1008 /*
1009  * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
1010  * is very much like a CS4231, with a few extra bits. We will
1011  * try to support at least some of the extra bits by overriding
1012  * some of the CS4231 callback.
1013  */
1014 static int __devinit create_ad1845(struct snd_card *card, unsigned port,
1015                                    int irq, int dma1, int dma2)
1016 {
1017         register struct soundscape *sscape = get_card_soundscape(card);
1018         struct snd_cs4231 *chip;
1019         int err;
1020
1021         if (dma1 == dma2)
1022                 dma2 = -1;
1023
1024         err = snd_cs4231_create(card,
1025                                 port, -1, irq, dma1, dma2,
1026                                 CS4231_HW_DETECT, CS4231_HWSHARE_DMA1, &chip);
1027         if (!err) {
1028                 unsigned long flags;
1029                 struct snd_pcm *pcm;
1030
1031 #define AD1845_FREQ_SEL_ENABLE  0x08
1032
1033 #define AD1845_PWR_DOWN_CTRL   0x1b
1034 #define AD1845_CRYS_CLOCK_SEL  0x1d
1035
1036 /*
1037  * It turns out that the PLAYBACK_ENABLE bit is set
1038  * by the lowlevel driver ...
1039  *
1040 #define AD1845_IFACE_CONFIG  \
1041            (CS4231_AUTOCALIB | CS4231_RECORD_ENABLE | CS4231_PLAYBACK_ENABLE)
1042     snd_cs4231_mce_up(chip);
1043     spin_lock_irqsave(&chip->reg_lock, flags);
1044     snd_cs4231_out(chip, CS4231_IFACE_CTRL, AD1845_IFACE_CONFIG);
1045     spin_unlock_irqrestore(&chip->reg_lock, flags);
1046     snd_cs4231_mce_down(chip);
1047  */
1048
1049                 /*
1050                  * The input clock frequency on the SoundScape must
1051                  * be 14.31818 MHz, because we must set this register
1052                  * to get the playback to sound correct ...
1053                  */
1054                 snd_cs4231_mce_up(chip);
1055                 spin_lock_irqsave(&chip->reg_lock, flags);
1056                 snd_cs4231_out(chip, AD1845_CRYS_CLOCK_SEL, 0x20);
1057                 spin_unlock_irqrestore(&chip->reg_lock, flags);
1058                 snd_cs4231_mce_down(chip);
1059
1060                 /*
1061                  * More custom configuration:
1062                  * a) select "mode 2", and provide a current drive of 8 mA
1063                  * b) enable frequency selection (for capture/playback)
1064                  */
1065                 spin_lock_irqsave(&chip->reg_lock, flags);
1066                 snd_cs4231_out(chip, CS4231_MISC_INFO, (CS4231_MODE2 | 0x10));
1067                 snd_cs4231_out(chip, AD1845_PWR_DOWN_CTRL, snd_cs4231_in(chip, AD1845_PWR_DOWN_CTRL) | AD1845_FREQ_SEL_ENABLE);
1068                 spin_unlock_irqrestore(&chip->reg_lock, flags);
1069
1070                 if ((err = snd_cs4231_pcm(chip, 0, &pcm)) < 0) {
1071                         snd_printk(KERN_ERR "sscape: No PCM device for AD1845 chip\n");
1072                         goto _error;
1073                 }
1074
1075                 if ((err = snd_cs4231_mixer(chip)) < 0) {
1076                         snd_printk(KERN_ERR "sscape: No mixer device for AD1845 chip\n");
1077                         goto _error;
1078                 }
1079
1080                 if ((err = snd_ctl_add(card, snd_ctl_new1(&midi_mixer_ctl, chip))) < 0) {
1081                         snd_printk(KERN_ERR "sscape: Could not create MIDI mixer control\n");
1082                         goto _error;
1083                 }
1084
1085                 strcpy(card->driver, "SoundScape");
1086                 strcpy(card->shortname, pcm->name);
1087                 snprintf(card->longname, sizeof(card->longname),
1088                          "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n",
1089                          pcm->name, chip->port, chip->irq,
1090                          chip->dma1, chip->dma2);
1091                 chip->set_playback_format = ad1845_playback_format;
1092                 chip->set_capture_format = ad1845_capture_format;
1093                 sscape->chip = chip;
1094         }
1095
1096         _error:
1097         return err;
1098 }
1099
1100
1101 /*
1102  * Create an ALSA soundcard entry for the SoundScape, using
1103  * the given list of port, IRQ and DMA resources.
1104  */
1105 static int __devinit create_sscape(int dev, struct snd_card *card)
1106 {
1107         register struct soundscape *sscape;
1108         register unsigned dma_cfg;
1109         unsigned irq_cfg;
1110         unsigned mpu_irq_cfg;
1111         unsigned xport;
1112         struct resource *io_res;
1113         unsigned long flags;
1114         int err;
1115
1116         /*
1117          * Check that the user didn't pass us garbage data ...
1118          */
1119         irq_cfg = get_irq_config(irq[dev]);
1120         if (irq_cfg == INVALID_IRQ) {
1121                 snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", irq[dev]);
1122                 return -ENXIO;
1123         }
1124
1125         mpu_irq_cfg = get_irq_config(mpu_irq[dev]);
1126         if (mpu_irq_cfg == INVALID_IRQ) {
1127                 printk(KERN_ERR "sscape: Invalid IRQ %d\n", mpu_irq[dev]);
1128                 return -ENXIO;
1129         }
1130         xport = port[dev];
1131
1132         /*
1133          * Grab IO ports that we will need to probe so that we
1134          * can detect and control this hardware ...
1135          */
1136         if ((io_res = request_region(xport, 8, "SoundScape")) == NULL) {
1137                 snd_printk(KERN_ERR "sscape: can't grab port 0x%x\n", xport);
1138                 return -EBUSY;
1139         }
1140
1141         /*
1142          * Grab both DMA channels (OK, only one for now) ...
1143          */
1144         err = request_dma(dma[dev], "SoundScape");
1145         if (err < 0) {
1146                 snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", dma[dev]);
1147                 goto _release_region;
1148         }
1149
1150         sscape = get_card_soundscape(card);
1151         spin_lock_init(&sscape->lock);
1152         spin_lock_init(&sscape->fwlock);
1153         sscape->io_res = io_res;
1154         sscape->io_base = xport;
1155
1156         if (!detect_sscape(sscape)) {
1157                 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1158                 err = -ENODEV;
1159                 goto _release_dma;
1160         }
1161
1162         printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
1163                          sscape->io_base, irq[dev], dma[dev]);
1164
1165         /*
1166          * Now create the hardware-specific device so that we can
1167          * load the microcode into the on-board processor.
1168          * We cannot use the MPU-401 MIDI system until this firmware
1169          * has been loaded into the card.
1170          */
1171         if ((err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw))) < 0) {
1172                 printk(KERN_ERR "sscape: Failed to create firmware device\n");
1173                 goto _release_dma;
1174         }
1175         strlcpy(sscape->hw->name, "SoundScape M68K", sizeof(sscape->hw->name));
1176         sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
1177         sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
1178         sscape->hw->ops.open = sscape_hw_open;
1179         sscape->hw->ops.release = sscape_hw_release;
1180         sscape->hw->ops.ioctl = sscape_hw_ioctl;
1181         sscape->hw->private_data = sscape;
1182
1183         /*
1184          * Tell the on-board devices where their resources are (I think -
1185          * I can't be sure without a datasheet ... So many magic values!)
1186          */
1187         spin_lock_irqsave(&sscape->lock, flags);
1188
1189         activate_ad1845_unsafe(sscape->io_base);
1190
1191         sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
1192         sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
1193         sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
1194
1195         /*
1196          * Enable and configure the DMA channels ...
1197          */
1198         sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
1199         dma_cfg = (sscape->ic_type == IC_ODIE ? 0x70 : 0x40);
1200         sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
1201         sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
1202
1203         sscape_write_unsafe(sscape->io_base,
1204                             GA_INTCFG_REG, 0xf0 | (mpu_irq_cfg << 2) | mpu_irq_cfg);
1205         sscape_write_unsafe(sscape->io_base,
1206                             GA_CDCFG_REG, 0x09 | DMA_8BIT
1207                             | (dma[dev] << 4) | (irq_cfg << 1));
1208
1209         spin_unlock_irqrestore(&sscape->lock, flags);
1210
1211         /*
1212          * We have now enabled the codec chip, and so we should
1213          * detect the AD1845 device ...
1214          */
1215         err = create_ad1845(card, wss_port[dev], irq[dev],
1216                             dma[dev], dma2[dev]);
1217         if (err < 0) {
1218                 printk(KERN_ERR "sscape: No AD1845 device at 0x%lx, IRQ %d\n",
1219                        wss_port[dev], irq[dev]);
1220                 goto _release_dma;
1221         }
1222 #define MIDI_DEVNUM  0
1223         if ((err = create_mpu401(card, MIDI_DEVNUM, MPU401_IO(xport), mpu_irq[dev])) < 0) {
1224                 printk(KERN_ERR "sscape: Failed to create MPU-401 device at 0x%x\n",
1225                                 MPU401_IO(xport));
1226                 goto _release_dma;
1227         }
1228
1229         /*
1230          * Enable the master IRQ ...
1231          */
1232         sscape_write(sscape, GA_INTENA_REG, 0x80);
1233
1234         /*
1235          * Initialize mixer
1236          */
1237         sscape->midi_vol = 0;
1238         host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
1239         host_write_ctrl_unsafe(sscape->io_base, 0, 100);
1240         host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
1241
1242         /*
1243          * Now that we have successfully created this sound card,
1244          * it is safe to store the pointer.
1245          * NOTE: we only register the sound card's "destructor"
1246          *       function now that our "constructor" has completed.
1247          */
1248         card->private_free = soundscape_free;
1249
1250         return 0;
1251
1252 _release_dma:
1253         free_dma(dma[dev]);
1254
1255 _release_region:
1256         release_and_free_resource(io_res);
1257
1258         return err;
1259 }
1260
1261
1262 static int __devinit snd_sscape_match(struct device *pdev, unsigned int i)
1263 {
1264         /*
1265          * Make sure we were given ALL of the other parameters.
1266          */
1267         if (port[i] == SNDRV_AUTO_PORT)
1268                 return 0;
1269
1270         if (irq[i] == SNDRV_AUTO_IRQ ||
1271             mpu_irq[i] == SNDRV_AUTO_IRQ ||
1272             dma[i] == SNDRV_AUTO_DMA) {
1273                 printk(KERN_INFO
1274                        "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1275                 return 0;
1276         }
1277
1278         return 1;
1279 }
1280
1281 static int __devinit snd_sscape_probe(struct device *pdev, unsigned int dev)
1282 {
1283         struct snd_card *card;
1284         struct soundscape *sscape;
1285         int ret;
1286
1287         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1288                             sizeof(struct soundscape));
1289         if (!card)
1290                 return -ENOMEM;
1291
1292         sscape = get_card_soundscape(card);
1293         sscape->type = SSCAPE;
1294
1295         dma[dev] &= 0x03;
1296         ret = create_sscape(dev, card);
1297         if (ret < 0)
1298                 goto _release_card;
1299
1300         snd_card_set_dev(card, pdev);
1301         if ((ret = snd_card_register(card)) < 0) {
1302                 printk(KERN_ERR "sscape: Failed to register sound card\n");
1303                 goto _release_card;
1304         }
1305         dev_set_drvdata(pdev, card);
1306         return 0;
1307
1308 _release_card:
1309         snd_card_free(card);
1310         return ret;
1311 }
1312
1313 static int __devexit snd_sscape_remove(struct device *devptr, unsigned int dev)
1314 {
1315         snd_card_free(dev_get_drvdata(devptr));
1316         dev_set_drvdata(devptr, NULL);
1317         return 0;
1318 }
1319
1320 #define DEV_NAME "sscape"
1321
1322 static struct isa_driver snd_sscape_driver = {
1323         .match          = snd_sscape_match,
1324         .probe          = snd_sscape_probe,
1325         .remove         = __devexit_p(snd_sscape_remove),
1326         /* FIXME: suspend/resume */
1327         .driver         = {
1328                 .name   = DEV_NAME
1329         },
1330 };
1331
1332 #ifdef CONFIG_PNP
1333 static inline int __devinit get_next_autoindex(int i)
1334 {
1335         while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT)
1336                 ++i;
1337         return i;
1338 }
1339
1340
1341 static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1342                                        const struct pnp_card_device_id *pid)
1343 {
1344         static int idx = 0;
1345         struct pnp_dev *dev;
1346         struct snd_card *card;
1347         struct soundscape *sscape;
1348         int ret;
1349
1350         /*
1351          * Allow this function to fail *quietly* if all the ISA PnP
1352          * devices were configured using module parameters instead.
1353          */
1354         if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS)
1355                 return -ENOSPC;
1356
1357         /*
1358          * We have found a candidate ISA PnP card. Now we
1359          * have to check that it has the devices that we
1360          * expect it to have.
1361          *
1362          * We will NOT try and autoconfigure all of the resources
1363          * needed and then activate the card as we are assuming that
1364          * has already been done at boot-time using /proc/isapnp.
1365          * We shall simply try to give each active card the resources
1366          * that it wants. This is a sensible strategy for a modular
1367          * system where unused modules are unloaded regularly.
1368          *
1369          * This strategy is utterly useless if we compile the driver
1370          * into the kernel, of course.
1371          */
1372         // printk(KERN_INFO "sscape: %s\n", card->name);
1373
1374         /*
1375          * Check that we still have room for another sound card ...
1376          */
1377         dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
1378         if (! dev)
1379                 return -ENODEV;
1380
1381         if (!pnp_is_active(dev)) {
1382                 if (pnp_activate_dev(dev) < 0) {
1383                         printk(KERN_INFO "sscape: device is inactive\n");
1384                         return -EBUSY;
1385                 }
1386         }
1387
1388         /*
1389          * Create a new ALSA sound card entry, in anticipation
1390          * of detecting our hardware ...
1391          */
1392         card = snd_card_new(index[idx], id[idx], THIS_MODULE,
1393                             sizeof(struct soundscape));
1394         if (!card)
1395                 return -ENOMEM;
1396
1397         sscape = get_card_soundscape(card);
1398
1399         /*
1400          * Identify card model ...
1401          */
1402         if (!strncmp("ENS4081", pid->id, 7))
1403                 sscape->type = SSCAPE_VIVO;
1404         else
1405                 sscape->type = SSCAPE_PNP;
1406
1407         /* VIVO fails for now */
1408         if (sscape->type == SSCAPE_VIVO) {
1409                 ret = -ENODEV;
1410                 goto _release_card;
1411         }
1412
1413         /*
1414          * Read the correct parameters off the ISA PnP bus ...
1415          */
1416         port[idx] = pnp_port_start(dev, 0);
1417         irq[idx] = pnp_irq(dev, 0);
1418         mpu_irq[idx] = pnp_irq(dev, 1);
1419         dma[idx] = pnp_dma(dev, 0) & 0x03;
1420         dma2[idx] = dma[idx];
1421         wss_port[idx] = CODEC_IO(port[idx]);
1422
1423         ret = create_sscape(idx, card);
1424         if (ret < 0)
1425                 goto _release_card;
1426
1427         snd_card_set_dev(card, &pcard->card->dev);
1428         if ((ret = snd_card_register(card)) < 0) {
1429                 printk(KERN_ERR "sscape: Failed to register sound card\n");
1430                 goto _release_card;
1431         }
1432
1433         pnp_set_card_drvdata(pcard, card);
1434         ++idx;
1435         return 0;
1436
1437 _release_card:
1438         snd_card_free(card);
1439         return ret;
1440 }
1441
1442 static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1443 {
1444         snd_card_free(pnp_get_card_drvdata(pcard));
1445         pnp_set_card_drvdata(pcard, NULL);
1446 }
1447
1448 static struct pnp_card_driver sscape_pnpc_driver = {
1449         .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1450         .name = "sscape",
1451         .id_table = sscape_pnpids,
1452         .probe = sscape_pnp_detect,
1453         .remove = __devexit_p(sscape_pnp_remove),
1454 };
1455
1456 #endif /* CONFIG_PNP */
1457
1458 static int __init sscape_init(void)
1459 {
1460         int err;
1461
1462         err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS);
1463 #ifdef CONFIG_PNP
1464         if (!err)
1465                 isa_registered = 1;
1466
1467         err = pnp_register_card_driver(&sscape_pnpc_driver);
1468         if (!err)
1469                 pnp_registered = 1;
1470
1471         if (isa_registered)
1472                 err = 0;
1473 #endif
1474         return err;
1475 }
1476
1477 static void __exit sscape_exit(void)
1478 {
1479 #ifdef CONFIG_PNP
1480         if (pnp_registered)
1481                 pnp_unregister_card_driver(&sscape_pnpc_driver);
1482         if (isa_registered)
1483 #endif
1484                 isa_unregister_driver(&snd_sscape_driver);
1485 }
1486
1487 module_init(sscape_init);
1488 module_exit(sscape_exit);