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