patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / 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/delay.h>
27 #include <linux/pnp.h>
28 #include <linux/spinlock.h>
29 #include <linux/moduleparam.h>
30 #include <asm/dma.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>
36
37 #include <sound/sscape_ioctl.h>
38
39 #define chip_t cs4231_t
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 = { [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;
52 static int boot_devs;
53
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);
57
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);
61
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);
65
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);
69
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);
73
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);
77   
78 #ifdef CONFIG_PNP
79 static struct pnp_card_device_id sscape_pnpids[] = {
80         { .id = "ENS3081", .devs = { { "ENS0000" } } },
81         { .id = "" }    /* end */
82 };
83
84 MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
85 #endif
86
87 static snd_card_t *sscape_card[SNDRV_CARDS];
88
89
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)
98
99 #define IC_ODIE  1
100 #define IC_OPUS  2
101
102 #define RX_READY 0x01
103 #define TX_READY 0x02
104
105 #define CMD_ACK           0x80
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
113
114 enum GA_REG {
115         GA_INTSTAT_REG = 0,
116         GA_INTENA_REG,
117         GA_DMAA_REG,
118         GA_DMAB_REG,
119         GA_INTCFG_REG,
120         GA_DMACFG_REG,
121         GA_CDCFG_REG,
122         GA_SMCFGA_REG,
123         GA_SMCFGB_REG,
124         GA_HMCTL_REG
125 };
126
127 #define DMA_8BIT  0x80
128
129
130 #define AD1845_FREQ_SEL_MSB    0x16
131 #define AD1845_FREQ_SEL_LSB    0x17
132
133 struct soundscape {
134         spinlock_t lock;
135         unsigned io_base;
136         int codec_type;
137         int ic_type;
138         struct resource *io_res;
139         cs4231_t *chip;
140         mpu401_t *mpu;
141         snd_hwdep_t *hw;
142
143         /*
144          * The MIDI device won't work until we've loaded
145          * its firmware via a hardware-dependent device IOCTL
146          */
147         spinlock_t fwlock;
148         int hw_in_use;
149         unsigned long midi_usage;
150         unsigned char midi_vol;
151 };
152
153 #define INVALID_IRQ  ((unsigned)-1)
154
155
156 static inline struct soundscape *get_card_soundscape(snd_card_t * c)
157 {
158         return (struct soundscape *) (c->private_data);
159 }
160
161 static inline struct soundscape *get_mpu401_soundscape(mpu401_t * mpu)
162 {
163         return (struct soundscape *) (mpu->private_data);
164 }
165
166 static inline struct soundscape *get_hwdep_soundscape(snd_hwdep_t * hw)
167 {
168         return (struct soundscape *) (hw->private_data);
169 }
170
171
172 /*
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.
176  */
177 static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
178 {
179         if (buf) {
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);
186                         return NULL;
187                 }
188         }
189
190         return buf;
191 }
192
193 /*
194  * Release the DMA-able kernel memory ...
195  */
196 static void free_dmabuf(struct snd_dma_buffer *buf)
197 {
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);
204         }
205 }
206
207
208 /*
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" ...
212  */
213 static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
214 {
215         outb(reg, ODIE_ADDR_IO(io_base));
216         outb(val, ODIE_DATA_IO(io_base));
217 }
218
219 /*
220  * Write to the SoundScape's control registers, and do the
221  * necessary locking ...
222  */
223 static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
224 {
225         unsigned long flags;
226
227         spin_lock_irqsave(&s->lock, flags);
228         sscape_write_unsafe(s->io_base, reg, val);
229         spin_unlock_irqrestore(&s->lock, flags);
230 }
231
232 /*
233  * Read from the SoundScape's control registers, but leave any
234  * locking to the caller. This is why the function is "unsafe" ...
235  */
236 static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
237 {
238         outb(reg, ODIE_ADDR_IO(io_base));
239         return inb(ODIE_DATA_IO(io_base));
240 }
241
242 /*
243  * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
244  */
245 static inline void set_host_mode_unsafe(unsigned io_base)
246 {
247         outb(0x0, HOST_CTRL_IO(io_base));
248 }
249
250 /*
251  * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
252  */
253 static inline void set_midi_mode_unsafe(unsigned io_base)
254 {
255         outb(0x3, HOST_CTRL_IO(io_base));
256 }
257
258 /*
259  * Read the SoundScape's host-mode control register, but leave
260  * any locking issues to the caller ...
261  */
262 static inline int host_read_unsafe(unsigned io_base)
263 {
264         int data = -1;
265         if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
266                 data = inb(HOST_DATA_IO(io_base));
267         }
268
269         return data;
270 }
271
272 /*
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 ...
276  */
277 static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
278 {
279         int data;
280
281         while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
282                 udelay(100);
283                 --timeout;
284         } /* while */
285
286         return data;
287 }
288
289 /*
290  * Write to the SoundScape's host-mode control registers, but
291  * leave any locking issues to the caller ...
292  */
293 static inline int host_write_unsafe(unsigned io_base, unsigned char data)
294 {
295         if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
296                 outb(data, HOST_DATA_IO(io_base));
297                 return 1;
298         }
299
300         return 0;
301 }
302
303 /*
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 ...
307  */
308 static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
309                                   unsigned timeout)
310 {
311         int err;
312
313         while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
314                 udelay(100);
315                 --timeout;
316         } /* while */
317
318         return err;
319 }
320
321
322 /*
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 ...
325  *
326  * NOTE: This check is based upon observation, not documentation.
327  */
328 static inline int verify_mpu401(const mpu401_t * mpu)
329 {
330         return ((inb(MIDI_CTRL_IO(mpu->port)) & 0xc0) == 0x80);
331 }
332
333 /*
334  * This is apparently the standard way to initailise an MPU-401
335  */
336 static inline void initialise_mpu401(const mpu401_t * mpu)
337 {
338         outb(0, MIDI_DATA_IO(mpu->port));
339 }
340
341 /*
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 ...
345  */
346 static inline void activate_ad1845_unsafe(unsigned io_base)
347 {
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);
350 }
351
352 /*
353  * Do the necessary ALSA-level cleanup to deallocate our driver ...
354  */
355 static void soundscape_free(snd_card_t * c)
356 {
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);
361 }
362
363 /*
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.
369  */
370 static inline void sleep(long jiffs, int state)
371 {
372         set_current_state(state);
373         schedule_timeout(jiffs);
374 }
375
376 /*
377  * Tell the SoundScape to begin a DMA tranfer using the given channel.
378  * All locking issues are left to the caller.
379  */
380 static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
381 {
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);
384 }
385
386 /*
387  * Wait for a DMA transfer to complete. This is a "limited busy-wait",
388  * and all locking issues are left to the caller.
389  */
390 static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
391 {
392         while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
393                 udelay(100);
394                 --timeout;
395         } /* while */
396
397         return (sscape_read_unsafe(io_base, reg) & 0x01);
398 }
399
400 /*
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.
406  */
407 static int obp_startup_ack(struct soundscape *s, unsigned timeout)
408 {
409         while (timeout != 0) {
410                 unsigned long flags;
411                 unsigned char x;
412
413                 sleep(1, TASK_INTERRUPTIBLE);
414
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)
419                         return 1;
420
421                 --timeout;
422         } /* while */
423
424         return 0;
425 }
426
427 /*
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
432  * this function.
433  */
434 static int host_startup_ack(struct soundscape *s, unsigned timeout)
435 {
436         while (timeout != 0) {
437                 unsigned long flags;
438                 unsigned char x;
439
440                 sleep(1, TASK_INTERRUPTIBLE);
441
442                 spin_lock_irqsave(&s->lock, flags);
443                 x = inb(HOST_DATA_IO(s->io_base));
444                 spin_unlock_irqrestore(&s->lock, flags);
445                 if (x == 0xfe)
446                         return 1;
447
448                 --timeout;
449         } /* while */
450
451         return 0;
452 }
453
454 /*
455  * Upload a byte-stream into the SoundScape using DMA channel A.
456  */
457 static int upload_dma_data(struct soundscape *s,
458                            const unsigned char __user *data,
459                            size_t size)
460 {
461         unsigned long flags;
462         struct snd_dma_buffer dma;
463         int ret;
464
465         if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
466                 return -ENOMEM;
467
468         spin_lock_irqsave(&s->lock, flags);
469
470         /*
471          * Reset the board ...
472          */
473         sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
474
475         /*
476          * Enable the DMA channels and configure them ...
477          */
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);
481
482         /*
483          * Take the board out of reset ...
484          */
485         sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
486
487         /*
488          * Upload the user's data (firmware?) to the SoundScape
489          * board through the DMA channel ...
490          */
491         while (size != 0) {
492                 unsigned long len;
493
494                 /*
495                  * Apparently, copying to/from userspace can sleep.
496                  * We are therefore forbidden from holding any
497                  * spinlocks while we copy ...
498                  */
499                 spin_unlock_irqrestore(&s->lock, flags);
500
501                 /*
502                  * Remember that the data that we want to DMA
503                  * comes from USERSPACE. We have already verified
504                  * the userspace pointer ...
505                  */
506                 len = min(size, dma.bytes);
507                 __copy_from_user(dma.area, data, len);
508                 data += len;
509                 size -= len;
510
511                 /*
512                  * Grab that spinlock again, now that we've
513                  * finished copying!
514                  */
515                 spin_lock_irqsave(&s->lock, flags);
516
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)) {
520                         /*
521                          * Don't forget to release this spinlock we're holding ...
522                          */
523                         spin_unlock_irqrestore(&s->lock, flags);
524
525                         snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
526                         ret = -EAGAIN;
527                         goto _release_dma;
528                 }
529         } /* while */
530
531         set_host_mode_unsafe(s->io_base);
532
533         /*
534          * Boot the board ... (I think)
535          */
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);
538
539         /*
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) ...
543          */
544         ret = 0;
545         if (!obp_startup_ack(s, 5)) {
546                 snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
547                 ret = -EAGAIN;
548         } else if (!host_startup_ack(s, 5)) {
549                 snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
550                 ret = -EAGAIN;
551         }
552
553         _release_dma:
554         /*
555          * NOTE!!! We are NOT holding any spinlocks at this point !!!
556          */
557         sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
558         free_dmabuf(&dma);
559
560         return ret;
561 }
562
563 /*
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.
567  *
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.
571  */
572 static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock __user *bb)
573 {
574         unsigned long flags;
575         int data = 0;
576         int ret;
577
578         ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
579
580         spin_lock_irqsave(&sscape->lock, flags);
581         if (ret == 0) {
582                 data = host_read_ctrl_unsafe(sscape->io_base, 100);
583         }
584         set_midi_mode_unsafe(sscape->io_base);
585         spin_unlock_irqrestore(&sscape->lock, flags);
586
587         if (ret == 0) {
588                 if (data < 0) {
589                         snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
590                         ret = -EAGAIN;
591                 } else {
592                         __copy_to_user(&bb->version, &data, sizeof(bb->version));
593                 }
594         }
595
596         return ret;
597 }
598
599 /*
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.
605  */
606 static int sscape_upload_microcode(struct soundscape *sscape,
607                                    const struct sscape_microcode __user *mc)
608 {
609         unsigned long flags;
610         char __user *code;
611         int err, ret;
612
613         /*
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.
617          *
618          * NOTE: This buffer is 64K long! That's WAY too big to
619          *       copy into a stack-temporary anyway.
620          */
621         if (get_user(code, &mc->code))
622                 return -EFAULT;
623         if ((err = verify_area(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE)) != 0)
624                 return err;
625
626         if ((ret = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
627                 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
628         }
629
630         spin_lock_irqsave(&sscape->lock, flags);
631         set_midi_mode_unsafe(sscape->io_base);
632         spin_unlock_irqrestore(&sscape->lock, flags);
633
634         initialise_mpu401(sscape->mpu);
635
636         return ret;
637 }
638
639 /*
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.
646  */
647 static int sscape_hw_open(snd_hwdep_t * hw, struct file *file)
648 {
649         register struct soundscape *sscape = get_hwdep_soundscape(hw);
650         unsigned long flags;
651         int err;
652
653         spin_lock_irqsave(&sscape->fwlock, flags);
654
655         if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
656                 err = -EBUSY;
657         } else {
658                 sscape->hw_in_use = 1;
659                 err = 0;
660         }
661
662         spin_unlock_irqrestore(&sscape->fwlock, flags);
663         return err;
664 }
665
666 static int sscape_hw_release(snd_hwdep_t * hw, struct file *file)
667 {
668         register struct soundscape *sscape = get_hwdep_soundscape(hw);
669         unsigned long flags;
670
671         spin_lock_irqsave(&sscape->fwlock, flags);
672         sscape->hw_in_use = 0;
673         spin_unlock_irqrestore(&sscape->fwlock, flags);
674         return 0;
675 }
676
677 static int sscape_hw_ioctl(snd_hwdep_t * hw, struct file *file,
678                            unsigned int cmd, unsigned long arg)
679 {
680         struct soundscape *sscape = get_hwdep_soundscape(hw);
681         int err = -EBUSY;
682
683         switch (cmd) {
684         case SND_SSCAPE_LOAD_BOOTB:
685                 {
686                         register struct sscape_bootblock __user *bb = (struct sscape_bootblock __user *) arg;
687
688                         /*
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 ...
692                          */
693                         if ((err = verify_area(VERIFY_READ, bb->code, sizeof(bb->code))) != 0)
694                                 return err;
695
696                         /*
697                          * Now check that we can write the firmware version number too...
698                          */
699                         if ((err = verify_area(VERIFY_WRITE, &bb->version, sizeof(bb->version))) != 0)
700                                 return err;
701
702                         err = sscape_upload_bootblock(sscape, bb);
703                 }
704                 break;
705
706         case SND_SSCAPE_LOAD_MCODE:
707                 {
708                         register const struct sscape_microcode __user *mc = (const struct sscape_microcode __user *) arg;
709
710                         err = sscape_upload_microcode(sscape, mc);
711                 }
712                 break;
713
714         default:
715                 err = -EINVAL;
716                 break;
717         } /* switch */
718
719         return err;
720 }
721
722
723 /*
724  * Mixer control for the SoundScape's MIDI device.
725  */
726 static int sscape_midi_info(snd_kcontrol_t * ctl,
727                             snd_ctl_elem_info_t * uinfo)
728 {
729         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
730         uinfo->count = 1;
731         uinfo->value.integer.min = 0;
732         uinfo->value.integer.max = 127;
733         return 0;
734 }
735
736 static int sscape_midi_get(snd_kcontrol_t * kctl,
737                            snd_ctl_elem_value_t * uctl)
738 {
739         cs4231_t *chip = snd_kcontrol_chip(kctl);
740         snd_card_t *card = chip->card;
741         register struct soundscape *s = get_card_soundscape(card);
742         unsigned long flags;
743
744         spin_lock_irqsave(&s->lock, flags);
745         set_host_mode_unsafe(s->io_base);
746
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);
749         }
750
751         set_midi_mode_unsafe(s->io_base);
752         spin_unlock_irqrestore(&s->lock, flags);
753         return 0;
754 }
755
756 static int sscape_midi_put(snd_kcontrol_t * kctl,
757                            snd_ctl_elem_value_t * uctl)
758 {
759         cs4231_t *chip = snd_kcontrol_chip(kctl);
760         snd_card_t *card = chip->card;
761         register struct soundscape *s = get_card_soundscape(card);
762         unsigned long flags;
763         int change;
764
765         spin_lock_irqsave(&s->lock, flags);
766
767         /*
768          * We need to put the board into HOST mode before we
769          * can send any volume-changing HOST commands ...
770          */
771         set_host_mode_unsafe(s->io_base);
772
773         /*
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"?
778          */
779         if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
780                 change = 0;
781                 goto __skip_change;
782         }
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));
786       __skip_change:
787
788         /*
789          * Take the board out of HOST mode and back into MIDI mode ...
790          */
791         set_midi_mode_unsafe(s->io_base);
792
793         spin_unlock_irqrestore(&s->lock, flags);
794         return change;
795 }
796
797 static snd_kcontrol_new_t midi_mixer_ctl = {
798         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
799         .name = "MIDI",
800         .info = sscape_midi_info,
801         .get = sscape_midi_get,
802         .put = sscape_midi_put
803 };
804
805 /*
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.
809  */
810 static unsigned __devinit get_irq_config(int irq)
811 {
812         static const int valid_irq[] = { 9, 5, 7, 10 };
813         unsigned cfg;
814
815         for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) {
816                 if (irq == valid_irq[cfg])
817                         return cfg;
818         } /* for */
819
820         return INVALID_IRQ;
821 }
822
823
824 /*
825  * Perform certain arcane port-checks to see whether there
826  * is a SoundScape board lurking behind the given ports.
827  */
828 static int __devinit detect_sscape(struct soundscape *s)
829 {
830         unsigned long flags;
831         unsigned d;
832         int retval = 0;
833
834         spin_lock_irqsave(&s->lock, flags);
835
836         /*
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...
840          */
841         if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
842                 goto _done;
843
844         d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
845         if ((d & 0x80) != 0)
846                 goto _done;
847
848         if (d == 0) {
849                 s->codec_type = 1;
850                 s->ic_type = IC_ODIE;
851         } else if ((d & 0x60) != 0) {
852                 s->codec_type = 2;
853                 s->ic_type = IC_OPUS;
854         } else
855                 goto _done;
856
857         outb(0xfa, ODIE_ADDR_IO(s->io_base));
858         if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
859                 goto _done;
860
861         outb(0xfe, ODIE_ADDR_IO(s->io_base));
862         if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
863                 goto _done;
864         if ((inb(ODIE_DATA_IO(s->io_base)) & 0x9f) != 0x0e)
865                 goto _done;
866
867         /*
868          * SoundScape successfully detected!
869          */
870         retval = 1;
871
872         _done:
873         spin_unlock_irqrestore(&s->lock, flags);
874         return retval;
875 }
876
877 /*
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
881  * IOCTL device.
882  */
883 static int mpu401_open(mpu401_t * mpu)
884 {
885         int err;
886
887         if (!verify_mpu401(mpu)) {
888                 snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
889                 err = -ENODEV;
890         } else {
891                 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
892                 unsigned long flags;
893
894                 spin_lock_irqsave(&sscape->fwlock, flags);
895
896                 if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
897                         err = -EBUSY;
898                 } else {
899                         ++(sscape->midi_usage);
900                         err = 0;
901                 }
902
903                 spin_unlock_irqrestore(&sscape->fwlock, flags);
904         }
905
906         return err;
907 }
908
909 static void mpu401_close(mpu401_t * mpu)
910 {
911         register struct soundscape *sscape = get_mpu401_soundscape(mpu);
912         unsigned long flags;
913
914         spin_lock_irqsave(&sscape->fwlock, flags);
915         --(sscape->midi_usage);
916         spin_unlock_irqrestore(&sscape->fwlock, flags);
917 }
918
919 /*
920  * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
921  */
922 static int __devinit create_mpu401(snd_card_t * card, int devnum, unsigned long port, int irq)
923 {
924         struct soundscape *sscape = get_card_soundscape(card);
925         snd_rawmidi_t *rawmidi;
926         int err;
927
928 #define MPU401_SHARE_HARDWARE  1
929         if ((err = snd_mpu401_uart_new(card, devnum,
930                                        MPU401_HW_MPU401,
931                                        port, MPU401_SHARE_HARDWARE,
932                                        irq, SA_INTERRUPT,
933                                        &rawmidi)) == 0) {
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;
940                 sscape->mpu = mpu;
941
942                 initialise_mpu401(mpu);
943         }
944
945         return err;
946 }
947
948
949 /*
950  * Override for the CS4231 playback format function.
951  * The AD1845 has much simpler format and rate selection.
952  */
953 static void ad1845_playback_format(cs4231_t * chip, snd_pcm_hw_params_t * params, unsigned char format)
954 {
955         unsigned long flags;
956         unsigned rate = params_rate(params);
957
958         /*
959          * The AD1845 can't handle sample frequencies
960          * outside of 4 kHZ to 50 kHZ
961          */
962         if (rate > 50000)
963                 rate = 50000;
964         else if (rate < 4000)
965                 rate = 4000;
966
967         spin_lock_irqsave(&chip->reg_lock, flags);
968
969         /*
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
973          * register is set.
974          * 
975          * NOTE: We seem to need to write to the MSB before the LSB
976          *       to get the correct sample frequency.
977          */
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);
981
982         spin_unlock_irqrestore(&chip->reg_lock, flags);
983 }
984
985 /*
986  * Override for the CS4231 capture format function. 
987  * The AD1845 has much simpler format and rate selection.
988  */
989 static void ad1845_capture_format(cs4231_t * chip, snd_pcm_hw_params_t * params, unsigned char format)
990 {
991         unsigned long flags;
992         unsigned rate = params_rate(params);
993
994         /*
995          * The AD1845 can't handle sample frequencies 
996          * outside of 4 kHZ to 50 kHZ
997          */
998         if (rate > 50000)
999                 rate = 50000;
1000         else if (rate < 4000)
1001                 rate = 4000;
1002
1003         spin_lock_irqsave(&chip->reg_lock, flags);
1004
1005         /*
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
1009          * register is set.
1010          *
1011          * NOTE: We seem to need to write to the MSB before the LSB
1012          *       to get the correct sample frequency.
1013          */
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);
1017
1018         spin_unlock_irqrestore(&chip->reg_lock, flags);
1019 }
1020
1021 /*
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.
1026  */
1027 static int __devinit create_ad1845(snd_card_t * card, unsigned port, int irq, int dma1)
1028 {
1029         register struct soundscape *sscape = get_card_soundscape(card);
1030         cs4231_t *chip;
1031         int err;
1032
1033 #define CS4231_SHARE_HARDWARE  (CS4231_HWSHARE_DMA1 | CS4231_HWSHARE_DMA2)
1034         /*
1035          * The AD1845 PCM device is only half-duplex, and so
1036          * we only give it one DMA channel ...
1037          */
1038         if ((err = snd_cs4231_create(card,
1039                                      port, -1, irq, dma1, dma1,
1040                                      CS4231_HW_DETECT,
1041                                      CS4231_HWSHARE_DMA1, &chip)) == 0) {
1042                 unsigned long flags;
1043                 snd_pcm_t *pcm;
1044
1045 #define AD1845_FREQ_SEL_ENABLE  0x08
1046
1047 #define AD1845_PWR_DOWN_CTRL   0x1b
1048 #define AD1845_CRYS_CLOCK_SEL  0x1d
1049
1050 /*
1051  * It turns out that the PLAYBACK_ENABLE bit is set
1052  * by the lowlevel driver ...
1053  *
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);
1061  */
1062
1063                 /*
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 ...
1067                  */
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);
1073
1074                 /*
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)
1078                  */
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);
1083
1084                 if ((err = snd_cs4231_pcm(chip, 0, &pcm)) < 0) {
1085                         snd_printk(KERN_ERR "sscape: No PCM device for AD1845 chip\n");
1086                         goto _error;
1087                 }
1088
1089                 if ((err = snd_cs4231_mixer(chip)) < 0) {
1090                         snd_printk(KERN_ERR "sscape: No mixer device for AD1845 chip\n");
1091                         goto _error;
1092                 }
1093
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");
1096                         goto _error;
1097                 }
1098
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;
1107         }
1108
1109         _error:
1110         return err;
1111 }
1112
1113
1114 struct params
1115 {
1116         int index;
1117         const char *id;
1118         unsigned port;
1119         int irq;
1120         int mpu_irq;
1121         int dma1;
1122 };
1123
1124
1125 static inline struct params*
1126 init_params(struct params *params,
1127             int index,
1128             const char *id,
1129             unsigned port,
1130             int irq,
1131             int mpu_irq,
1132             int dma1)
1133 {
1134         params->index = index;
1135         params->id = id;
1136         params->port = port;
1137         params->irq = irq;
1138         params->mpu_irq = mpu_irq;  
1139         params->dma1 = (dma1 & 0x03);
1140
1141         return params;
1142 }
1143
1144
1145 /*
1146  * Create an ALSA soundcard entry for the SoundScape, using
1147  * the given list of port, IRQ and DMA resources.
1148  */
1149 static int __devinit create_sscape(const struct params *params, snd_card_t **rcardp)
1150 {
1151         snd_card_t *card;
1152         register struct soundscape *sscape;
1153         register unsigned dma_cfg;
1154         unsigned irq_cfg;
1155         unsigned mpu_irq_cfg;
1156         struct resource *io_res;
1157         unsigned long flags;
1158         int err;
1159
1160         /*
1161          * Check that the user didn't pass us garbage data ...
1162          */
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);
1166                 return -ENXIO;
1167         }
1168
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);
1172                 return -ENXIO;
1173         }
1174
1175         /*
1176          * Grab IO ports that we will need to probe so that we
1177          * can detect and control this hardware ...
1178          */
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);
1181                 return -EBUSY;
1182         }
1183
1184         /*
1185          * Grab both DMA channels (OK, only one for now) ...
1186          */
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;
1190         }
1191
1192         /*
1193          * Create a new ALSA sound card entry, in anticipation
1194          * of detecting our hardware ...
1195          */
1196         if ((card = snd_card_new(params->index, params->id, THIS_MODULE, sizeof(struct soundscape))) == NULL) {
1197                 err = -ENOMEM;
1198                 goto _release_dma;
1199         }
1200
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;
1206
1207         if (!detect_sscape(sscape)) {
1208                 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1209                 err = -ENODEV;
1210                 goto _release_card;
1211         }
1212
1213         printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
1214                          sscape->io_base, params->irq, params->dma1);
1215
1216         /*
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.
1221          */
1222         if ((err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw))) < 0) {
1223                 printk(KERN_ERR "sscape: Failed to create firmware device\n");
1224                 goto _release_card;
1225         }
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;
1233
1234         /*
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!)
1237          */
1238         spin_lock_irqsave(&sscape->lock, flags);
1239
1240         activate_ad1845_unsafe(sscape->io_base);
1241
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);
1245
1246         /*
1247          * Enable and configure the DMA channels ...
1248          */
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);
1253
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));
1258
1259         spin_unlock_irqrestore(&sscape->lock, flags);
1260
1261         /*
1262          * We have now enabled the codec chip, and so we should
1263          * detect the AD1845 device ...
1264          */
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);
1268                 goto _release_card;
1269         }
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));
1274                 goto _release_card;
1275         }
1276
1277         /*
1278          * Enable the master IRQ ...
1279          */
1280         sscape_write(sscape, GA_INTENA_REG, 0x80);
1281
1282         if ((err = snd_card_register(card)) < 0) {
1283                 printk(KERN_ERR "sscape: Failed to register sound card\n");
1284                 goto _release_card;
1285         }
1286
1287         /*
1288          * Initialize mixer
1289          */
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);
1294
1295         /*
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.
1300          */
1301         card->private_free = soundscape_free;
1302         *rcardp = card;
1303
1304         return 0;
1305
1306         _release_card:
1307         snd_card_free(card);
1308
1309         _release_dma:
1310         free_dma(params->dma1);
1311
1312         _release_region:
1313         release_resource(io_res);
1314         kfree_nocheck(io_res);
1315
1316         return err;
1317 }
1318
1319
1320 static int sscape_cards __devinitdata;
1321 static struct params sscape_params[SNDRV_CARDS] __devinitdata;
1322
1323 #ifdef CONFIG_PNP
1324 static inline int __devinit get_next_autoindex(int i)
1325 {
1326         while ((i < SNDRV_CARDS) && (port[i] != SNDRV_AUTO_PORT)) {
1327                 ++i;
1328         } /* while */
1329
1330         return i;
1331 }
1332
1333
1334 static inline int __devinit is_port_known(unsigned io, struct params *params, int cards)
1335 {
1336         while (--cards >= 0) {
1337                 if (params[cards].port == io)
1338                         return 1;
1339         } /* while */
1340
1341         return 0;
1342 }
1343
1344 static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1345                                        const struct pnp_card_device_id *pid)
1346 {
1347         struct pnp_dev *dev;
1348         static int idx = 0;
1349         int ret;
1350
1351         /*
1352          * Allow this function to fail *quietly* if all the ISA PnP
1353          * devices were configured using module parameters instead.
1354          */
1355         if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS) {
1356                 return -ENOSPC;
1357         }
1358
1359         /*
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.
1363          *
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.
1370          *
1371          * This strategy is utterly useless if we compile the driver
1372          * into the kernel, of course.
1373          */
1374         // printk(KERN_INFO "sscape: %s\n", card->name);
1375
1376         /*
1377          * Check that we still have room for another sound card ...
1378          */
1379         if (sscape_cards >= SNDRV_CARDS) {
1380                 printk(KERN_ERR "sscape: No room for another ALSA device\n");
1381                 return -ENOSPC;
1382         }
1383
1384         ret = -ENODEV;
1385
1386         dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
1387         if (dev) {
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");
1392                                 return -EBUSY;
1393                         }
1394                 }
1395                 /*
1396                  * Read the correct parameters off the ISA PnP bus ...
1397                  */
1398                 this = init_params(&sscape_params[sscape_cards],
1399                                    index[idx],
1400                                    id[idx],
1401                                    pnp_port_start(dev, 0),
1402                                    pnp_irq(dev, 0),
1403                                    pnp_irq(dev, 1),
1404                                    pnp_dma(dev, 0));
1405
1406                 /*
1407                  * Do we know about this sound card already?
1408                  */
1409                 if ( !is_port_known(this->port, sscape_params, sscape_cards) ) {
1410                         snd_card_t *card;
1411
1412                         ret = create_sscape(this, &card);
1413                         if (ret < 0)
1414                                 return ret;
1415                         snd_card_set_dev(card, &pcard->card->dev);
1416                         pnp_set_card_drvdata(pcard, card);
1417                         ++sscape_cards;
1418                         ++idx;
1419                 }
1420         }
1421
1422         return ret;
1423 }
1424
1425 static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1426 {
1427         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
1428         
1429         pnp_set_card_drvdata(pcard, NULL);
1430         snd_card_disconnect(card);
1431         snd_card_free_in_thread(card);
1432 }
1433
1434 static struct pnp_card_driver sscape_pnpc_driver = {
1435         .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1436         .name = "sscape",
1437         .id_table = sscape_pnpids,
1438         .probe = sscape_pnp_detect,
1439         .remove = __devexit_p(sscape_pnp_remove),
1440 };
1441
1442 #endif /* CONFIG_PNP */
1443
1444 static int __init sscape_manual_probe(struct params *params)
1445 {
1446         int ret;
1447         unsigned i;
1448         snd_card_t *card;
1449
1450         for (i = 0; i < SNDRV_CARDS; ++i) {
1451                 /*
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
1455                  * of parameters.
1456                  */
1457                 if (port[i] == SNDRV_AUTO_PORT)
1458                         continue;
1459
1460                 /*
1461                  * Make sure we were given ALL of the other parameters.
1462                  */
1463                 if ( (irq[i] == SNDRV_AUTO_IRQ) ||
1464                      (mpu_irq[i] == SNDRV_AUTO_IRQ) ||
1465                      (dma[i] == SNDRV_AUTO_DMA) ) {
1466                         printk(KERN_INFO
1467                                "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1468                         return -ENXIO;
1469                 }
1470
1471                 /*
1472                  * This cards looks OK ...
1473                  */
1474                 init_params(params, index[i], id[i], port[i], irq[i], mpu_irq[i], dma[i]);
1475
1476                 ret = create_sscape(params, &card);
1477                 if (ret < 0)
1478                         return ret;
1479
1480                 sscape_card[sscape_cards] = card;
1481                 params++;
1482                 sscape_cards++;
1483         } /* for */
1484
1485         return 0;
1486 }
1487
1488
1489 static void sscape_exit(void)
1490 {
1491         unsigned i;
1492
1493 #ifdef CONFIG_PNP
1494         pnp_unregister_card_driver(&sscape_pnpc_driver);
1495 #endif
1496         for (i = 0; i < ARRAY_SIZE(sscape_card); ++i) {
1497                 snd_card_free(sscape_card[i]);
1498         } /* for */
1499 }
1500
1501
1502 static int __init sscape_init(void)
1503 {
1504         int ret;
1505
1506         /*
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...
1511          */
1512         ret = sscape_manual_probe(sscape_params);
1513         if (ret < 0) {
1514                 int i;
1515                 for (i = 0; i < sscape_cards; ++i)
1516                         snd_card_free(sscape_card[i]);
1517                 return ret;
1518         }
1519
1520 #ifdef CONFIG_PNP
1521         if (sscape_cards < SNDRV_CARDS) {
1522                 ret = pnp_register_card_driver(&sscape_pnpc_driver);
1523                 if (ret < 0) {
1524                         sscape_exit();
1525                         return ret;
1526                 }
1527         }
1528 #endif
1529
1530         return 0;
1531 }
1532
1533 module_init(sscape_init);
1534 module_exit(sscape_exit);