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