commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.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  *   FIXME (deadlock for alsa-kernel):
9  *     - use ISA PnP scheme used by all ALSA ISA drivers
10  *     - add non-MODULE build option
11  *
12  *
13  *   This program is free software; you can redistribute it and/or modify
14  *   it under the terms of the GNU General Public License as published by
15  *   the Free Software Foundation; either version 2 of the License, or
16  *   (at your option) any later version.
17  *
18  *   This program is distributed in the hope that it will be useful,
19  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *   GNU General Public License for more details.
22  *
23  *   You should have received a copy of the GNU General Public License
24  *   along with this program; if not, write to the Free Software
25  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26  */
27
28 #include <sound/driver.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/pnp.h>
32 #include <linux/spinlock.h>
33 #include <asm/dma.h>
34 #include <sound/core.h>
35 #include <sound/hwdep.h>
36 #include <sound/cs4231.h>
37 #include <sound/mpu401.h>
38 #include <sound/initval.h>
39
40 #include <sound/sscape_ioctl.h>
41
42 #define chip_t cs4231_t
43
44
45 MODULE_AUTHOR("Chris Rankin");
46 MODULE_DESCRIPTION("ENSONIQ SoundScape PnP driver");
47 MODULE_LICENSE("GPL");
48
49 static int index[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IDX;
50 static char* id[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_STR;
51 static long port[SNDRV_CARDS] __devinitdata = { [0 ... (SNDRV_CARDS-1)] = SNDRV_AUTO_PORT };
52 static int irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
53 static int mpu_irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
54 static int dma[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
55
56 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
57 MODULE_PARM_DESC(index, "Index number for SoundScape soundcard");
58 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
59
60 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
61 MODULE_PARM_DESC(id, "Description for SoundScape card");
62 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
63
64 MODULE_PARM(port, "1-" __MODULE_STRING(SNDRV_CARDS) "l");
65 MODULE_PARM_DESC(port, "Port # for SoundScape driver.");
66 MODULE_PARM_SYNTAX(port, SNDRV_ENABLED);
67
68 MODULE_PARM(irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
69 MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver.");
70 MODULE_PARM_SYNTAX(irq, SNDRV_IRQ_DESC);
71
72 MODULE_PARM(mpu_irq, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
73 MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver.");
74 MODULE_PARM_SYNTAX(mpu_irq, SNDRV_IRQ_DESC);
75
76 MODULE_PARM(dma, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
77 MODULE_PARM_DESC(dma, "DMA # for SoundScape driver.");
78 MODULE_PARM_SYNTAX(dma, SNDRV_DMA8_DESC);
79   
80 #ifdef CONFIG_PNP
81 static struct pnp_card_device_id sscape_pnpids[] = {
82         { .id = "ENS3081", .devs = { { "ENS0000" } } },
83         { .id = "" }    /* end */
84 };
85
86 MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
87 #endif
88
89 static snd_card_t *sscape_card[SNDRV_CARDS];
90
91
92 #define MPU401_IO(i)     ((i) + 0)
93 #define MIDI_DATA_IO(i)  ((i) + 0)
94 #define MIDI_CTRL_IO(i)  ((i) + 1)
95 #define HOST_CTRL_IO(i)  ((i) + 2)
96 #define HOST_DATA_IO(i)  ((i) + 3)
97 #define ODIE_ADDR_IO(i)  ((i) + 4)
98 #define ODIE_DATA_IO(i)  ((i) + 5)
99 #define CODEC_IO(i)      ((i) + 8)
100
101 #define IC_ODIE  1
102 #define IC_OPUS  2
103
104 #define RX_READY 0x01
105 #define TX_READY 0x02
106
107 #define CMD_ACK           0x80
108 #define CMD_SET_MIDI_VOL  0x84
109 #define CMD_GET_MIDI_VOL  0x85
110 #define CMD_XXX_MIDI_VOL  0x86
111 #define CMD_SET_EXTMIDI   0x8a
112 #define CMD_GET_EXTMIDI   0x8b
113 #define CMD_SET_MT32      0x8c
114 #define CMD_GET_MT32      0x8d
115
116 enum GA_REG {
117         GA_INTSTAT_REG = 0,
118         GA_INTENA_REG,
119         GA_DMAA_REG,
120         GA_DMAB_REG,
121         GA_INTCFG_REG,
122         GA_DMACFG_REG,
123         GA_CDCFG_REG,
124         GA_SMCFGA_REG,
125         GA_SMCFGB_REG,
126         GA_HMCTL_REG
127 };
128
129 #define DMA_8BIT  0x80
130
131
132 #define AD1845_FREQ_SEL_MSB    0x16
133 #define AD1845_FREQ_SEL_LSB    0x17
134
135 struct soundscape {
136         spinlock_t lock;
137         unsigned io_base;
138         int codec_type;
139         int ic_type;
140         struct resource *io_res;
141         cs4231_t *chip;
142         mpu401_t *mpu;
143         snd_hwdep_t *hw;
144
145         /*
146          * The MIDI device won't work until we've loaded
147          * its firmware via a hardware-dependent device IOCTL
148          */
149         spinlock_t fwlock;
150         int hw_in_use;
151         unsigned long midi_usage;
152         unsigned char midi_vol;
153 };
154
155 #define INVALID_IRQ  ((unsigned)-1)
156
157
158 static inline struct soundscape *get_card_soundscape(snd_card_t * c)
159 {
160         return (struct soundscape *) (c->private_data);
161 }
162
163 static inline struct soundscape *get_mpu401_soundscape(mpu401_t * mpu)
164 {
165         return (struct soundscape *) (mpu->private_data);
166 }
167
168 static inline struct soundscape *get_hwdep_soundscape(snd_hwdep_t * hw)
169 {
170         return (struct soundscape *) (hw->private_data);
171 }
172
173
174 struct dmabuf {
175         size_t size;
176         unsigned char *data;
177         dma_addr_t addr;
178 };
179
180 /*
181  * Allocates some kernel memory that we can use for DMA.
182  * I think this means that the memory has to map to
183  * contiguous pages of physical memory.
184  */
185 static struct dmabuf *get_dmabuf(struct dmabuf *buf, unsigned long s)
186 {
187         if (buf) {
188                 buf->data = snd_malloc_isa_pages_fallback(s, &buf->addr, &buf->size);
189                 if (!buf->data) {
190                         snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", s);
191                         return NULL;
192                 }
193         }
194
195         return buf;
196 }
197
198 /*
199  * Release the DMA-able kernel memory ...
200  */
201 static void free_dmabuf(struct dmabuf *buf)
202 {
203         if (buf && buf->data)
204                 snd_free_isa_pages(buf->size, buf->data, buf->addr);
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 *data, size_t size,
459                            size_t dmasize)
460 {
461         unsigned long flags;
462         struct dmabuf dma;
463         int ret;
464
465         if (!get_dmabuf(&dma, dmasize))
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.size);
507                 __copy_from_user(dma.data, 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 *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), PAGE_SIZE);
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  * We assume that the caller has already verified the
607  * userspace memory addresses.
608  */
609 static int sscape_upload_microcode(struct soundscape *sscape,
610                                    const struct sscape_microcode *mc)
611 {
612         unsigned long flags;
613         int ret;
614
615         if ((ret = upload_dma_data(sscape, mc->code, sizeof(mc->code), PAGE_SIZE * 16)) == 0) {
616                 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
617         }
618
619         spin_lock_irqsave(&sscape->lock, flags);
620         set_midi_mode_unsafe(sscape->io_base);
621         spin_unlock_irqrestore(&sscape->lock, flags);
622
623         initialise_mpu401(sscape->mpu);
624
625         return ret;
626 }
627
628 /*
629  * Hardware-specific device functions, to implement special
630  * IOCTLs for the SoundScape card. This is how we upload
631  * the microcode into the card, for example, and so we
632  * must ensure that no two processes can open this device
633  * simultaneously, and that we can't open it at all if
634  * someone is using the MIDI device.
635  */
636 static int sscape_hw_open(snd_hwdep_t * hw, struct file *file)
637 {
638         register struct soundscape *sscape = get_hwdep_soundscape(hw);
639         unsigned long flags;
640         int err;
641
642         spin_lock_irqsave(&sscape->fwlock, flags);
643
644         if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
645                 err = -EBUSY;
646         } else {
647                 sscape->hw_in_use = 1;
648                 err = 0;
649         }
650
651         spin_unlock_irqrestore(&sscape->fwlock, flags);
652         return err;
653 }
654
655 static int sscape_hw_release(snd_hwdep_t * hw, struct file *file)
656 {
657         register struct soundscape *sscape = get_hwdep_soundscape(hw);
658         unsigned long flags;
659
660         spin_lock_irqsave(&sscape->fwlock, flags);
661         sscape->hw_in_use = 0;
662         spin_unlock_irqrestore(&sscape->fwlock, flags);
663         return 0;
664 }
665
666 static int sscape_hw_ioctl(snd_hwdep_t * hw, struct file *file,
667                            unsigned int cmd, unsigned long arg)
668 {
669         struct soundscape *sscape = get_hwdep_soundscape(hw);
670         int err = -EBUSY;
671
672         switch (cmd) {
673         case SND_SSCAPE_LOAD_BOOTB:
674                 {
675                         register struct sscape_bootblock *bb = (struct sscape_bootblock *) arg;
676
677                         /*
678                          * We are going to have to copy this data into a special
679                          * DMA-able buffer before we can upload it. We shall therefore
680                          * just check that the data pointer is valid for now ...
681                          */
682                         if ((err = verify_area(VERIFY_READ, bb->code, sizeof(bb->code))) != 0)
683                                 return err;
684
685                         /*
686                          * Now check that we can write the firmware version number too...
687                          */
688                         if ((err = verify_area(VERIFY_WRITE, &bb->version, sizeof(bb->version))) != 0)
689                                 return err;
690
691                         err = sscape_upload_bootblock(sscape, bb);
692                 }
693                 break;
694
695         case SND_SSCAPE_LOAD_MCODE:
696                 {
697                         register const struct sscape_microcode *mc = (const struct sscape_microcode *) arg;
698
699                         /*
700                          * We are going to have to copy this data into a special
701                          * DMA-able buffer before we can upload it. We shall therefore
702                          * just check that the data pointer is valid for now.
703                          *
704                          * NOTE: This buffer is 64K long! That's WAY too big to
705                          *       copy into a stack-temporary anyway.
706                          */
707                         if ((err = verify_area(VERIFY_READ, mc->code, sizeof(mc->code))) != 0)
708                                 return err;
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                 return -EBUSY;
1181         }
1182
1183         /*
1184          * Grab both DMA channels (OK, only one for now) ...
1185          */
1186         if ((err = request_dma(params->dma1, "SoundScape")) < 0) {
1187                 goto _release_region;
1188         }
1189
1190         /*
1191          * Create a new ALSA sound card entry, in anticipation
1192          * of detecting our hardware ...
1193          */
1194         if ((card = snd_card_new(params->index, params->id, THIS_MODULE, sizeof(struct soundscape))) == NULL) {
1195                 err = -ENOMEM;
1196                 goto _release_dma;
1197         }
1198
1199         sscape = get_card_soundscape(card);
1200         spin_lock_init(&sscape->lock);
1201         spin_lock_init(&sscape->fwlock);
1202         sscape->io_res = io_res;
1203         sscape->io_base = params->port;
1204
1205         if (!detect_sscape(sscape)) {
1206                 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1207                 err = -ENODEV;
1208                 goto _release_card;
1209         }
1210
1211         printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
1212                          sscape->io_base, params->irq, params->dma1);
1213
1214         /*
1215          * Now create the hardware-specific device so that we can
1216          * load the microcode into the on-board processor.
1217          * We cannot use the MPU-401 MIDI system until this firmware
1218          * has been loaded into the card.
1219          */
1220         if ((err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw))) < 0) {
1221                 printk(KERN_ERR "sscape: Failed to create firmware device\n");
1222                 goto _release_card;
1223         }
1224         strlcpy(sscape->hw->name, "SoundScape M68K", sizeof(sscape->hw->name));
1225         sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
1226         sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
1227         sscape->hw->ops.open = sscape_hw_open;
1228         sscape->hw->ops.release = sscape_hw_release;
1229         sscape->hw->ops.ioctl = sscape_hw_ioctl;
1230         sscape->hw->private_data = sscape;
1231
1232         /*
1233          * Tell the on-board devices where their resources are (I think -
1234          * I can't be sure without a datasheet ... So many magic values!)
1235          */
1236         spin_lock_irqsave(&sscape->lock, flags);
1237
1238         activate_ad1845_unsafe(sscape->io_base);
1239
1240         sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
1241         sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
1242         sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
1243
1244         /*
1245          * Enable and configure the DMA channels ...
1246          */
1247         sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
1248         dma_cfg = (sscape->ic_type == IC_ODIE ? 0x70 : 0x40);
1249         sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
1250         sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
1251
1252         sscape_write_unsafe(sscape->io_base,
1253                             GA_INTCFG_REG, 0xf0 | (mpu_irq_cfg << 2) | mpu_irq_cfg);
1254         sscape_write_unsafe(sscape->io_base,
1255                             GA_CDCFG_REG, 0x09 | DMA_8BIT | (params->dma1 << 4) | (irq_cfg << 1));
1256
1257         spin_unlock_irqrestore(&sscape->lock, flags);
1258
1259         /*
1260          * We have now enabled the codec chip, and so we should
1261          * detect the AD1845 device ...
1262          */
1263         if ((err = create_ad1845(card, CODEC_IO(params->port), params->irq, params->dma1)) < 0) {
1264                 printk(KERN_ERR "sscape: No AD1845 device at 0x%x, IRQ %d\n",
1265                                 CODEC_IO(params->port), params->irq);
1266                 goto _release_card;
1267         }
1268 #define MIDI_DEVNUM  0
1269         if ((err = create_mpu401(card, MIDI_DEVNUM, MPU401_IO(params->port), params->mpu_irq)) < 0) {
1270                 printk(KERN_ERR "sscape: Failed to create MPU-401 device at 0x%x\n",
1271                                 MPU401_IO(params->port));
1272                 goto _release_card;
1273         }
1274
1275         /*
1276          * Enable the master IRQ ...
1277          */
1278         sscape_write(sscape, GA_INTENA_REG, 0x80);
1279
1280         if ((err = snd_card_register(card)) < 0) {
1281                 printk(KERN_ERR "sscape: Failed to register sound card\n");
1282                 goto _release_card;
1283         }
1284
1285         /*
1286          * Initialize mixer
1287          */
1288         sscape->midi_vol = 0;
1289         host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
1290         host_write_ctrl_unsafe(sscape->io_base, 0, 100);
1291         host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
1292
1293         /*
1294          * Now that we have successfully created this sound card,
1295          * it is safe to store the pointer.
1296          * NOTE: we only register the sound card's "destructor"
1297          *       function now that our "constructor" has completed.
1298          */
1299         card->private_free = soundscape_free;
1300         *rcardp = card;
1301
1302         return 0;
1303
1304         _release_card:
1305         snd_card_free(card);
1306
1307         _release_dma:
1308         free_dma(params->dma1);
1309
1310         _release_region:
1311         release_resource(io_res);
1312         kfree_nocheck(io_res);
1313
1314         return err;
1315 }
1316
1317
1318 static int sscape_cards __devinitdata;
1319 static struct params sscape_params[SNDRV_CARDS] __devinitdata;
1320
1321 #ifdef CONFIG_PNP
1322 static inline int __devinit get_next_autoindex(int i)
1323 {
1324         while ((i < SNDRV_CARDS) && (port[i] != SNDRV_AUTO_PORT)) {
1325                 ++i;
1326         } /* while */
1327
1328         return i;
1329 }
1330
1331
1332 static inline int __devinit is_port_known(unsigned io, struct params *params, int cards)
1333 {
1334         while (--cards >= 0) {
1335                 if (params[cards].port == io)
1336                         return 1;
1337         } /* while */
1338
1339         return 0;
1340 }
1341
1342 static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1343                                        const struct pnp_card_device_id *pid)
1344 {
1345         struct pnp_dev *dev;
1346         static int idx = 0;
1347         int ret;
1348
1349         /*
1350          * Allow this function to fail *quietly* if all the ISA PnP
1351          * devices were configured using module parameters instead.
1352          */
1353         if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS) {
1354                 return -ENOSPC;
1355         }
1356
1357         /*
1358          * We have found a candidate ISA PnP card. Now we
1359          * have to check that it has the devices that we
1360          * expect it to have.
1361          *
1362          * We will NOT try and autoconfigure all of the resources
1363          * needed and then activate the card as we are assuming that
1364          * has already been done at boot-time using /proc/isapnp.
1365          * We shall simply try to give each active card the resources
1366          * that it wants. This is a sensible strategy for a modular
1367          * system where unused modules are unloaded regularly.
1368          *
1369          * This strategy is utterly useless if we compile the driver
1370          * into the kernel, of course.
1371          */
1372         // printk(KERN_INFO "sscape: %s\n", card->name);
1373
1374         /*
1375          * Check that we still have room for another sound card ...
1376          */
1377         if (sscape_cards >= SNDRV_CARDS) {
1378                 printk(KERN_ERR "sscape: No room for another ALSA device\n");
1379                 return -ENOSPC;
1380         }
1381
1382         ret = -ENODEV;
1383
1384         dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
1385         if (dev) {
1386                 struct params *this;
1387                 if (!pnp_is_active(dev)) {
1388                         if (pnp_activate_dev(dev) < 0) {
1389                                 printk(KERN_INFO "sscape: device is inactive\n");
1390                                 return -EBUSY;
1391                         }
1392                 }
1393                 /*
1394                  * Read the correct parameters off the ISA PnP bus ...
1395                  */
1396                 this = init_params(&sscape_params[sscape_cards],
1397                                    index[idx],
1398                                    id[idx],
1399                                    pnp_port_start(dev, 0),
1400                                    pnp_irq(dev, 0),
1401                                    pnp_irq(dev, 1),
1402                                    pnp_dma(dev, 0));
1403
1404                 /*
1405                  * Do we know about this sound card already?
1406                  */
1407                 if ( !is_port_known(this->port, sscape_params, sscape_cards) ) {
1408                         snd_card_t *card;
1409
1410                         ret = create_sscape(this, &card);
1411                         if (ret < 0)
1412                                 return ret;
1413                         pnp_set_card_drvdata(pcard, card);
1414                         ++sscape_cards;
1415                         ++idx;
1416                 }
1417         }
1418
1419         return ret;
1420 }
1421
1422 static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1423 {
1424         snd_card_t *card = (snd_card_t *) pnp_get_card_drvdata(pcard);
1425         
1426         pnp_set_card_drvdata(pcard, NULL);
1427         snd_card_disconnect(card);
1428         snd_card_free_in_thread(card);
1429 }
1430
1431 static struct pnp_card_driver sscape_pnpc_driver = {
1432         .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1433         .name = "sscape",
1434         .id_table = sscape_pnpids,
1435         .probe = sscape_pnp_detect,
1436         .remove = __devexit_p(sscape_pnp_remove),
1437 };
1438
1439 #endif /* CONFIG_PNP */
1440
1441 static int __init sscape_manual_probe(struct params *params)
1442 {
1443         int ret;
1444         unsigned i;
1445         snd_card_t *card;
1446
1447         for (i = 0; i < SNDRV_CARDS; ++i) {
1448                 /*
1449                  * We do NOT probe for ports.
1450                  * If we're not given a port number for this
1451                  * card then we completely ignore this line
1452                  * of parameters.
1453                  */
1454                 if (port[i] == SNDRV_AUTO_PORT)
1455                         continue;
1456
1457                 /*
1458                  * Make sure we were given ALL of the other parameters.
1459                  */
1460                 if ( (irq[i] == SNDRV_AUTO_IRQ) ||
1461                      (mpu_irq[i] == SNDRV_AUTO_IRQ) ||
1462                      (dma[i] == SNDRV_AUTO_DMA) ) {
1463                         printk(KERN_INFO
1464                                "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1465                         return -ENXIO;
1466                 }
1467
1468                 /*
1469                  * This cards looks OK ...
1470                  */
1471                 init_params(params, index[i], id[i], port[i], irq[i], mpu_irq[i], dma[i]);
1472
1473                 ret = create_sscape(params, &card);
1474                 if (ret < 0)
1475                         return ret;
1476
1477                 sscape_card[sscape_cards] = card;
1478                 params++;
1479                 sscape_cards++;
1480         } /* for */
1481
1482         return 0;
1483 }
1484
1485
1486 static void sscape_exit(void)
1487 {
1488         unsigned i;
1489
1490 #ifdef CONFIG_PNP
1491         pnp_unregister_card_driver(&sscape_pnpc_driver);
1492 #endif
1493         for (i = 0; i < ARRAY_SIZE(sscape_card); ++i) {
1494                 snd_card_free(sscape_card[i]);
1495         } /* for */
1496 }
1497
1498
1499 static int __init sscape_init(void)
1500 {
1501         int ret;
1502
1503         /*
1504          * First check whether we were passed any parameters.
1505          * These MUST take precedence over ANY automatic way
1506          * of allocating cards, because the operator is
1507          * S-P-E-L-L-I-N-G it out for us...
1508          */
1509         ret = sscape_manual_probe(sscape_params);
1510         if (ret < 0) {
1511                 int i;
1512                 for (i = 0; i < sscape_cards; ++i)
1513                         snd_card_free(sscape_card[i]);
1514                 return ret;
1515         }
1516
1517 #ifdef CONFIG_PNP
1518         if (sscape_cards < SNDRV_CARDS) {
1519                 ret = pnp_register_card_driver(&sscape_pnpc_driver);
1520                 if (ret < 0) {
1521                         sscape_exit();
1522                         return ret;
1523                 }
1524         }
1525 #endif
1526
1527         return 0;
1528 }
1529
1530 module_init(sscape_init);
1531 module_exit(sscape_exit);
1532
1533 #ifndef MODULE
1534
1535 /* format is: snd-sscape=index,id,port,irq,mpu_irq,dma */
1536
1537 static int __init builtin_sscape_setup(char *str)
1538 {
1539         static unsigned __initdata nr_dev;
1540
1541         if (nr_dev >= SNDRV_CARDS)
1542                 return 0;
1543
1544         (void)((get_option(&str, &index[nr_dev]) == 2) &&
1545                (get_option(&str, (int*)&id[nr_dev]) == 2) &&
1546                (get_option(&str, (int*)&port[nr_dev]) == 2) &&
1547                (get_option(&str, &irq[nr_dev]) == 2) &&
1548                (get_option(&str, &mpu_irq[nr_dev]) == 2) &&
1549                (get_option(&str, &dma[nr_dev]) == 2)); 
1550  
1551         ++nr_dev;
1552         return 1;
1553 }
1554
1555 __setup("snd-sscape=", builtin_sscape_setup);
1556
1557 #endif
1558