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