blob: bc449166d18d6544408cd14cd40c2d9660a5bc5b [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
Takashi Iwai277e9262005-11-17 17:13:12 +010025#include <linux/err.h>
Takashi Iwai5e24c1c2007-02-22 12:50:54 +010026#include <linux/isa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/delay.h>
28#include <linux/pnp.h>
29#include <linux/spinlock.h>
30#include <linux/moduleparam.h>
31#include <asm/dma.h>
32#include <sound/core.h>
33#include <sound/hwdep.h>
Krzysztof Helt61ef19d2008-07-31 21:02:42 +020034#include <sound/wss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/mpu401.h>
36#include <sound/initval.h>
37
38#include <sound/sscape_ioctl.h>
39
40
41MODULE_AUTHOR("Chris Rankin");
42MODULE_DESCRIPTION("ENSONIQ SoundScape PnP driver");
43MODULE_LICENSE("GPL");
44
45static int index[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IDX;
46static char* id[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_STR;
Krzysztof Helt4996bca2007-09-17 16:23:13 +020047static long port[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_PORT;
48static long wss_port[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_PORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
50static int mpu_irq[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_IRQ;
51static int dma[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
Krzysztof Helt4996bca2007-09-17 16:23:13 +020052static int dma2[SNDRV_CARDS] __devinitdata = SNDRV_DEFAULT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54module_param_array(index, int, NULL, 0444);
55MODULE_PARM_DESC(index, "Index number for SoundScape soundcard");
56
57module_param_array(id, charp, NULL, 0444);
58MODULE_PARM_DESC(id, "Description for SoundScape card");
59
60module_param_array(port, long, NULL, 0444);
61MODULE_PARM_DESC(port, "Port # for SoundScape driver.");
62
Krzysztof Helt4996bca2007-09-17 16:23:13 +020063module_param_array(wss_port, long, NULL, 0444);
64MODULE_PARM_DESC(wss_port, "WSS Port # for SoundScape driver.");
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066module_param_array(irq, int, NULL, 0444);
67MODULE_PARM_DESC(irq, "IRQ # for SoundScape driver.");
68
69module_param_array(mpu_irq, int, NULL, 0444);
70MODULE_PARM_DESC(mpu_irq, "MPU401 IRQ # for SoundScape driver.");
71
72module_param_array(dma, int, NULL, 0444);
73MODULE_PARM_DESC(dma, "DMA # for SoundScape driver.");
Clemens Ladischf7a92752005-12-07 09:13:42 +010074
Krzysztof Helt4996bca2007-09-17 16:23:13 +020075module_param_array(dma2, int, NULL, 0444);
76MODULE_PARM_DESC(dma2, "DMA2 # for SoundScape driver.");
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +020079static int isa_registered;
Takashi Iwai59b1b342006-01-04 15:06:44 +010080static int pnp_registered;
Rene Herman609d7692007-05-15 11:42:56 +020081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static struct pnp_card_device_id sscape_pnpids[] = {
Krzysztof Helt4996bca2007-09-17 16:23:13 +020083 { .id = "ENS3081", .devs = { { "ENS0000" } } }, /* Soundscape PnP */
84 { .id = "ENS4081", .devs = { { "ENS1011" } } }, /* VIVO90 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 { .id = "" } /* end */
86};
87
88MODULE_DEVICE_TABLE(pnp_card, sscape_pnpids);
89#endif
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#define MPU401_IO(i) ((i) + 0)
93#define MIDI_DATA_IO(i) ((i) + 0)
94#define MIDI_CTRL_IO(i) ((i) + 1)
95#define HOST_CTRL_IO(i) ((i) + 2)
96#define HOST_DATA_IO(i) ((i) + 3)
97#define ODIE_ADDR_IO(i) ((i) + 4)
98#define ODIE_DATA_IO(i) ((i) + 5)
99#define CODEC_IO(i) ((i) + 8)
100
101#define IC_ODIE 1
102#define IC_OPUS 2
103
104#define RX_READY 0x01
105#define TX_READY 0x02
106
107#define CMD_ACK 0x80
108#define CMD_SET_MIDI_VOL 0x84
109#define CMD_GET_MIDI_VOL 0x85
110#define CMD_XXX_MIDI_VOL 0x86
111#define CMD_SET_EXTMIDI 0x8a
112#define CMD_GET_EXTMIDI 0x8b
113#define CMD_SET_MT32 0x8c
114#define CMD_GET_MT32 0x8d
115
116enum GA_REG {
117 GA_INTSTAT_REG = 0,
118 GA_INTENA_REG,
119 GA_DMAA_REG,
120 GA_DMAB_REG,
121 GA_INTCFG_REG,
122 GA_DMACFG_REG,
123 GA_CDCFG_REG,
124 GA_SMCFGA_REG,
125 GA_SMCFGB_REG,
126 GA_HMCTL_REG
127};
128
129#define DMA_8BIT 0x80
130
131
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200132enum card_type {
133 SSCAPE,
134 SSCAPE_PNP,
135 SSCAPE_VIVO,
136};
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct soundscape {
139 spinlock_t lock;
140 unsigned io_base;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200141 unsigned wss_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int codec_type;
143 int ic_type;
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200144 enum card_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct resource *io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200146 struct resource *wss_res;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200147 struct snd_wss *chip;
Takashi Iwaibe624532005-11-17 14:42:05 +0100148 struct snd_mpu401 *mpu;
149 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /*
152 * The MIDI device won't work until we've loaded
153 * its firmware via a hardware-dependent device IOCTL
154 */
155 spinlock_t fwlock;
156 int hw_in_use;
157 unsigned long midi_usage;
158 unsigned char midi_vol;
159};
160
161#define INVALID_IRQ ((unsigned)-1)
162
163
Takashi Iwaibe624532005-11-17 14:42:05 +0100164static inline struct soundscape *get_card_soundscape(struct snd_card *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 return (struct soundscape *) (c->private_data);
167}
168
Takashi Iwaibe624532005-11-17 14:42:05 +0100169static inline struct soundscape *get_mpu401_soundscape(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 return (struct soundscape *) (mpu->private_data);
172}
173
Takashi Iwaibe624532005-11-17 14:42:05 +0100174static inline struct soundscape *get_hwdep_soundscape(struct snd_hwdep * hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 return (struct soundscape *) (hw->private_data);
177}
178
179
180/*
181 * Allocates some kernel memory that we can use for DMA.
182 * I think this means that the memory has to map to
183 * contiguous pages of physical memory.
184 */
185static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
186{
187 if (buf) {
188 if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
189 size, buf) < 0) {
190 snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", size);
191 return NULL;
192 }
193 }
194
195 return buf;
196}
197
198/*
199 * Release the DMA-able kernel memory ...
200 */
201static void free_dmabuf(struct snd_dma_buffer *buf)
202{
203 if (buf && buf->area)
204 snd_dma_free_pages(buf);
205}
206
207
208/*
209 * This function writes to the SoundScape's control registers,
210 * but doesn't do any locking. It's up to the caller to do that.
211 * This is why this function is "unsafe" ...
212 */
213static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
214{
215 outb(reg, ODIE_ADDR_IO(io_base));
216 outb(val, ODIE_DATA_IO(io_base));
217}
218
219/*
220 * Write to the SoundScape's control registers, and do the
221 * necessary locking ...
222 */
223static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
224{
225 unsigned long flags;
226
227 spin_lock_irqsave(&s->lock, flags);
228 sscape_write_unsafe(s->io_base, reg, val);
229 spin_unlock_irqrestore(&s->lock, flags);
230}
231
232/*
233 * Read from the SoundScape's control registers, but leave any
234 * locking to the caller. This is why the function is "unsafe" ...
235 */
236static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
237{
238 outb(reg, ODIE_ADDR_IO(io_base));
239 return inb(ODIE_DATA_IO(io_base));
240}
241
242/*
243 * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
244 */
245static inline void set_host_mode_unsafe(unsigned io_base)
246{
247 outb(0x0, HOST_CTRL_IO(io_base));
248}
249
250/*
251 * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
252 */
253static inline void set_midi_mode_unsafe(unsigned io_base)
254{
255 outb(0x3, HOST_CTRL_IO(io_base));
256}
257
258/*
259 * Read the SoundScape's host-mode control register, but leave
260 * any locking issues to the caller ...
261 */
262static inline int host_read_unsafe(unsigned io_base)
263{
264 int data = -1;
265 if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
266 data = inb(HOST_DATA_IO(io_base));
267 }
268
269 return data;
270}
271
272/*
273 * Read the SoundScape's host-mode control register, performing
274 * a limited amount of busy-waiting if the register isn't ready.
275 * Also leaves all locking-issues to the caller ...
276 */
277static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
278{
279 int data;
280
281 while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
282 udelay(100);
283 --timeout;
284 } /* while */
285
286 return data;
287}
288
289/*
290 * Write to the SoundScape's host-mode control registers, but
291 * leave any locking issues to the caller ...
292 */
293static inline int host_write_unsafe(unsigned io_base, unsigned char data)
294{
295 if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
296 outb(data, HOST_DATA_IO(io_base));
297 return 1;
298 }
299
300 return 0;
301}
302
303/*
304 * Write to the SoundScape's host-mode control registers, performing
305 * a limited amount of busy-waiting if the register isn't ready.
306 * Also leaves all locking-issues to the caller ...
307 */
308static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
309 unsigned timeout)
310{
311 int err;
312
313 while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
314 udelay(100);
315 --timeout;
316 } /* while */
317
318 return err;
319}
320
321
322/*
323 * Check that the MIDI subsystem is operational. If it isn't,
324 * then we will hang the computer if we try to use it ...
325 *
326 * NOTE: This check is based upon observation, not documentation.
327 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100328static inline int verify_mpu401(const struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 return ((inb(MIDI_CTRL_IO(mpu->port)) & 0xc0) == 0x80);
331}
332
333/*
334 * This is apparently the standard way to initailise an MPU-401
335 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100336static inline void initialise_mpu401(const struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 outb(0, MIDI_DATA_IO(mpu->port));
339}
340
341/*
342 * Tell the SoundScape to activate the AD1845 chip (I think).
343 * The AD1845 detection fails if we *don't* do this, so I
344 * think that this is a good idea ...
345 */
346static inline void activate_ad1845_unsafe(unsigned io_base)
347{
348 sscape_write_unsafe(io_base, GA_HMCTL_REG, (sscape_read_unsafe(io_base, GA_HMCTL_REG) & 0xcf) | 0x10);
349 sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
350}
351
352/*
353 * Do the necessary ALSA-level cleanup to deallocate our driver ...
354 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100355static void soundscape_free(struct snd_card *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200357 struct soundscape *sscape = get_card_soundscape(c);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200358 release_and_free_resource(sscape->io_res);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200359 release_and_free_resource(sscape->wss_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 free_dma(sscape->chip->dma1);
361}
362
363/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * Tell the SoundScape to begin a DMA tranfer using the given channel.
365 * All locking issues are left to the caller.
366 */
367static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
368{
369 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) | 0x01);
370 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) & 0xfe);
371}
372
373/*
374 * Wait for a DMA transfer to complete. This is a "limited busy-wait",
375 * and all locking issues are left to the caller.
376 */
377static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
378{
379 while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
380 udelay(100);
381 --timeout;
382 } /* while */
383
384 return (sscape_read_unsafe(io_base, reg) & 0x01);
385}
386
387/*
388 * Wait for the On-Board Processor to return its start-up
389 * acknowledgement sequence. This wait is too long for
390 * us to perform "busy-waiting", and so we must sleep.
391 * This in turn means that we must not be holding any
392 * spinlocks when we call this function.
393 */
394static int obp_startup_ack(struct soundscape *s, unsigned timeout)
395{
396 while (timeout != 0) {
397 unsigned long flags;
398 unsigned char x;
399
Rene Hermand86d0192007-09-18 18:10:49 +0200400 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 spin_lock_irqsave(&s->lock, flags);
403 x = inb(HOST_DATA_IO(s->io_base));
404 spin_unlock_irqrestore(&s->lock, flags);
405 if ((x & 0xfe) == 0xfe)
406 return 1;
407
408 --timeout;
409 } /* while */
410
411 return 0;
412}
413
414/*
415 * Wait for the host to return its start-up acknowledgement
416 * sequence. This wait is too long for us to perform
417 * "busy-waiting", and so we must sleep. This in turn means
418 * that we must not be holding any spinlocks when we call
419 * this function.
420 */
421static int host_startup_ack(struct soundscape *s, unsigned timeout)
422{
423 while (timeout != 0) {
424 unsigned long flags;
425 unsigned char x;
426
Rene Hermand86d0192007-09-18 18:10:49 +0200427 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 spin_lock_irqsave(&s->lock, flags);
430 x = inb(HOST_DATA_IO(s->io_base));
431 spin_unlock_irqrestore(&s->lock, flags);
432 if (x == 0xfe)
433 return 1;
434
435 --timeout;
436 } /* while */
437
438 return 0;
439}
440
441/*
442 * Upload a byte-stream into the SoundScape using DMA channel A.
443 */
444static int upload_dma_data(struct soundscape *s,
445 const unsigned char __user *data,
446 size_t size)
447{
448 unsigned long flags;
449 struct snd_dma_buffer dma;
450 int ret;
451
452 if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
453 return -ENOMEM;
454
455 spin_lock_irqsave(&s->lock, flags);
456
457 /*
458 * Reset the board ...
459 */
460 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
461
462 /*
463 * Enable the DMA channels and configure them ...
464 */
465 sscape_write_unsafe(s->io_base, GA_DMACFG_REG, 0x50);
466 sscape_write_unsafe(s->io_base, GA_DMAA_REG, (s->chip->dma1 << 4) | DMA_8BIT);
467 sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
468
469 /*
470 * Take the board out of reset ...
471 */
472 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
473
474 /*
475 * Upload the user's data (firmware?) to the SoundScape
476 * board through the DMA channel ...
477 */
478 while (size != 0) {
479 unsigned long len;
480
481 /*
482 * Apparently, copying to/from userspace can sleep.
483 * We are therefore forbidden from holding any
484 * spinlocks while we copy ...
485 */
486 spin_unlock_irqrestore(&s->lock, flags);
487
488 /*
489 * Remember that the data that we want to DMA
490 * comes from USERSPACE. We have already verified
491 * the userspace pointer ...
492 */
493 len = min(size, dma.bytes);
494 len -= __copy_from_user(dma.area, data, len);
495 data += len;
496 size -= len;
497
498 /*
499 * Grab that spinlock again, now that we've
500 * finished copying!
501 */
502 spin_lock_irqsave(&s->lock, flags);
503
504 snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
505 sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
506 if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
507 /*
508 * Don't forget to release this spinlock we're holding ...
509 */
510 spin_unlock_irqrestore(&s->lock, flags);
511
512 snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
513 ret = -EAGAIN;
514 goto _release_dma;
515 }
516 } /* while */
517
518 set_host_mode_unsafe(s->io_base);
519
520 /*
521 * Boot the board ... (I think)
522 */
523 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x40);
524 spin_unlock_irqrestore(&s->lock, flags);
525
526 /*
527 * If all has gone well, then the board should acknowledge
528 * the new upload and tell us that it has rebooted OK. We
529 * give it 5 seconds (max) ...
530 */
531 ret = 0;
532 if (!obp_startup_ack(s, 5)) {
533 snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
534 ret = -EAGAIN;
535 } else if (!host_startup_ack(s, 5)) {
536 snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
537 ret = -EAGAIN;
538 }
539
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200540_release_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
542 * NOTE!!! We are NOT holding any spinlocks at this point !!!
543 */
544 sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
545 free_dmabuf(&dma);
546
547 return ret;
548}
549
550/*
551 * Upload the bootblock(?) into the SoundScape. The only
552 * purpose of this block of code seems to be to tell
553 * us which version of the microcode we should be using.
554 *
555 * NOTE: The boot-block data resides in USER-SPACE!!!
556 * However, we have already verified its memory
557 * addresses by the time we get here.
558 */
559static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock __user *bb)
560{
561 unsigned long flags;
562 int data = 0;
563 int ret;
564
565 ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
566
567 spin_lock_irqsave(&sscape->lock, flags);
568 if (ret == 0) {
569 data = host_read_ctrl_unsafe(sscape->io_base, 100);
570 }
571 set_midi_mode_unsafe(sscape->io_base);
572 spin_unlock_irqrestore(&sscape->lock, flags);
573
574 if (ret == 0) {
575 if (data < 0) {
576 snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
577 ret = -EAGAIN;
578 }
579 else if (__copy_to_user(&bb->version, &data, sizeof(bb->version))) {
580 ret = -EFAULT;
581 }
582 }
583
584 return ret;
585}
586
587/*
588 * Upload the microcode into the SoundScape. The
589 * microcode is 64K of data, and if we try to copy
590 * it into a local variable then we will SMASH THE
591 * KERNEL'S STACK! We therefore leave it in USER
592 * SPACE, and save ourselves from copying it at all.
593 */
594static int sscape_upload_microcode(struct soundscape *sscape,
595 const struct sscape_microcode __user *mc)
596{
597 unsigned long flags;
598 char __user *code;
599 int err;
600
601 /*
602 * We are going to have to copy this data into a special
603 * DMA-able buffer before we can upload it. We shall therefore
604 * just check that the data pointer is valid for now.
605 *
606 * NOTE: This buffer is 64K long! That's WAY too big to
607 * copy into a stack-temporary anyway.
608 */
609 if ( get_user(code, &mc->code) ||
610 !access_ok(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE) )
611 return -EFAULT;
612
613 if ((err = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
614 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
615 }
616
617 spin_lock_irqsave(&sscape->lock, flags);
618 set_midi_mode_unsafe(sscape->io_base);
619 spin_unlock_irqrestore(&sscape->lock, flags);
620
621 initialise_mpu401(sscape->mpu);
622
623 return err;
624}
625
626/*
627 * Hardware-specific device functions, to implement special
628 * IOCTLs for the SoundScape card. This is how we upload
629 * the microcode into the card, for example, and so we
630 * must ensure that no two processes can open this device
631 * simultaneously, and that we can't open it at all if
632 * someone is using the MIDI device.
633 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100634static int sscape_hw_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 register struct soundscape *sscape = get_hwdep_soundscape(hw);
637 unsigned long flags;
638 int err;
639
640 spin_lock_irqsave(&sscape->fwlock, flags);
641
642 if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
643 err = -EBUSY;
644 } else {
645 sscape->hw_in_use = 1;
646 err = 0;
647 }
648
649 spin_unlock_irqrestore(&sscape->fwlock, flags);
650 return err;
651}
652
Takashi Iwaibe624532005-11-17 14:42:05 +0100653static int sscape_hw_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 register struct soundscape *sscape = get_hwdep_soundscape(hw);
656 unsigned long flags;
657
658 spin_lock_irqsave(&sscape->fwlock, flags);
659 sscape->hw_in_use = 0;
660 spin_unlock_irqrestore(&sscape->fwlock, flags);
661 return 0;
662}
663
Takashi Iwaibe624532005-11-17 14:42:05 +0100664static int sscape_hw_ioctl(struct snd_hwdep * hw, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 unsigned int cmd, unsigned long arg)
666{
667 struct soundscape *sscape = get_hwdep_soundscape(hw);
668 int err = -EBUSY;
669
670 switch (cmd) {
671 case SND_SSCAPE_LOAD_BOOTB:
672 {
673 register struct sscape_bootblock __user *bb = (struct sscape_bootblock __user *) arg;
674
675 /*
676 * We are going to have to copy this data into a special
677 * DMA-able buffer before we can upload it. We shall therefore
678 * just check that the data pointer is valid for now ...
679 */
680 if ( !access_ok(VERIFY_READ, bb->code, sizeof(bb->code)) )
681 return -EFAULT;
682
683 /*
684 * Now check that we can write the firmware version number too...
685 */
686 if ( !access_ok(VERIFY_WRITE, &bb->version, sizeof(bb->version)) )
687 return -EFAULT;
688
689 err = sscape_upload_bootblock(sscape, bb);
690 }
691 break;
692
693 case SND_SSCAPE_LOAD_MCODE:
694 {
695 register const struct sscape_microcode __user *mc = (const struct sscape_microcode __user *) arg;
696
697 err = sscape_upload_microcode(sscape, mc);
698 }
699 break;
700
701 default:
702 err = -EINVAL;
703 break;
704 } /* switch */
705
706 return err;
707}
708
709
710/*
711 * Mixer control for the SoundScape's MIDI device.
712 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100713static int sscape_midi_info(struct snd_kcontrol *ctl,
714 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
717 uinfo->count = 1;
718 uinfo->value.integer.min = 0;
719 uinfo->value.integer.max = 127;
720 return 0;
721}
722
Takashi Iwaibe624532005-11-17 14:42:05 +0100723static int sscape_midi_get(struct snd_kcontrol *kctl,
724 struct snd_ctl_elem_value *uctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200726 struct snd_wss *chip = snd_kcontrol_chip(kctl);
Takashi Iwaibe624532005-11-17 14:42:05 +0100727 struct snd_card *card = chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 register struct soundscape *s = get_card_soundscape(card);
729 unsigned long flags;
730
731 spin_lock_irqsave(&s->lock, flags);
732 set_host_mode_unsafe(s->io_base);
733
734 if (host_write_ctrl_unsafe(s->io_base, CMD_GET_MIDI_VOL, 100)) {
735 uctl->value.integer.value[0] = host_read_ctrl_unsafe(s->io_base, 100);
736 }
737
738 set_midi_mode_unsafe(s->io_base);
739 spin_unlock_irqrestore(&s->lock, flags);
740 return 0;
741}
742
Takashi Iwaibe624532005-11-17 14:42:05 +0100743static int sscape_midi_put(struct snd_kcontrol *kctl,
744 struct snd_ctl_elem_value *uctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200746 struct snd_wss *chip = snd_kcontrol_chip(kctl);
Takashi Iwaibe624532005-11-17 14:42:05 +0100747 struct snd_card *card = chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 register struct soundscape *s = get_card_soundscape(card);
749 unsigned long flags;
750 int change;
751
752 spin_lock_irqsave(&s->lock, flags);
753
754 /*
755 * We need to put the board into HOST mode before we
756 * can send any volume-changing HOST commands ...
757 */
758 set_host_mode_unsafe(s->io_base);
759
760 /*
761 * To successfully change the MIDI volume setting, you seem to
762 * have to write a volume command, write the new volume value,
763 * and then perform another volume-related command. Perhaps the
764 * first command is an "open" and the second command is a "close"?
765 */
766 if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
767 change = 0;
768 goto __skip_change;
769 }
770 change = (host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
771 && host_write_ctrl_unsafe(s->io_base, ((unsigned char) uctl->value.integer. value[0]) & 127, 100)
772 && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100));
773 __skip_change:
774
775 /*
776 * Take the board out of HOST mode and back into MIDI mode ...
777 */
778 set_midi_mode_unsafe(s->io_base);
779
780 spin_unlock_irqrestore(&s->lock, flags);
781 return change;
782}
783
Takashi Iwaibe624532005-11-17 14:42:05 +0100784static struct snd_kcontrol_new midi_mixer_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
786 .name = "MIDI",
787 .info = sscape_midi_info,
788 .get = sscape_midi_get,
789 .put = sscape_midi_put
790};
791
792/*
793 * The SoundScape can use two IRQs from a possible set of four.
794 * These IRQs are encoded as bit patterns so that they can be
795 * written to the control registers.
796 */
797static unsigned __devinit get_irq_config(int irq)
798{
799 static const int valid_irq[] = { 9, 5, 7, 10 };
800 unsigned cfg;
801
802 for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) {
803 if (irq == valid_irq[cfg])
804 return cfg;
805 } /* for */
806
807 return INVALID_IRQ;
808}
809
810
811/*
812 * Perform certain arcane port-checks to see whether there
813 * is a SoundScape board lurking behind the given ports.
814 */
815static int __devinit detect_sscape(struct soundscape *s)
816{
817 unsigned long flags;
818 unsigned d;
819 int retval = 0;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200820 int codec = s->wss_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 spin_lock_irqsave(&s->lock, flags);
823
824 /*
825 * The following code is lifted from the original OSS driver,
826 * and as I don't have a datasheet I cannot really comment
827 * on what it is doing...
828 */
829 if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
830 goto _done;
831
832 d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
833 if ((d & 0x80) != 0)
834 goto _done;
835
836 if (d == 0) {
837 s->codec_type = 1;
838 s->ic_type = IC_ODIE;
839 } else if ((d & 0x60) != 0) {
840 s->codec_type = 2;
841 s->ic_type = IC_OPUS;
842 } else
843 goto _done;
844
845 outb(0xfa, ODIE_ADDR_IO(s->io_base));
846 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
847 goto _done;
848
849 outb(0xfe, ODIE_ADDR_IO(s->io_base));
850 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
851 goto _done;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200852
853 outb(0xfe, ODIE_ADDR_IO(s->io_base));
854 d = inb(ODIE_DATA_IO(s->io_base));
855 if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto _done;
857
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200858 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
859 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
860
861 if (s->type == SSCAPE_VIVO)
862 codec += 4;
863 /* wait for WSS codec */
864 for (d = 0; d < 500; d++) {
865 if ((inb(codec) & 0x80) == 0)
866 break;
867 spin_unlock_irqrestore(&s->lock, flags);
868 msleep(1);
869 spin_lock_irqsave(&s->lock, flags);
870 }
871 snd_printd(KERN_INFO "init delay = %d ms\n", d);
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /*
874 * SoundScape successfully detected!
875 */
876 retval = 1;
877
878 _done:
879 spin_unlock_irqrestore(&s->lock, flags);
880 return retval;
881}
882
883/*
884 * ALSA callback function, called when attempting to open the MIDI device.
885 * Check that the MIDI firmware has been loaded, because we don't want
886 * to crash the machine. Also check that someone isn't using the hardware
887 * IOCTL device.
888 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100889static int mpu401_open(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int err;
892
893 if (!verify_mpu401(mpu)) {
894 snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
895 err = -ENODEV;
896 } else {
897 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
898 unsigned long flags;
899
900 spin_lock_irqsave(&sscape->fwlock, flags);
901
902 if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
903 err = -EBUSY;
904 } else {
905 ++(sscape->midi_usage);
906 err = 0;
907 }
908
909 spin_unlock_irqrestore(&sscape->fwlock, flags);
910 }
911
912 return err;
913}
914
Takashi Iwaibe624532005-11-17 14:42:05 +0100915static void mpu401_close(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
918 unsigned long flags;
919
920 spin_lock_irqsave(&sscape->fwlock, flags);
921 --(sscape->midi_usage);
922 spin_unlock_irqrestore(&sscape->fwlock, flags);
923}
924
925/*
926 * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
927 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100928static int __devinit create_mpu401(struct snd_card *card, int devnum, unsigned long port, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
930 struct soundscape *sscape = get_card_soundscape(card);
Takashi Iwaibe624532005-11-17 14:42:05 +0100931 struct snd_rawmidi *rawmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int err;
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if ((err = snd_mpu401_uart_new(card, devnum,
935 MPU401_HW_MPU401,
Takashi Iwai302e4c22006-05-23 13:24:30 +0200936 port, MPU401_INFO_INTEGRATED,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700937 irq, IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 &rawmidi)) == 0) {
Takashi Iwaibe624532005-11-17 14:42:05 +0100939 struct snd_mpu401 *mpu = (struct snd_mpu401 *) rawmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 mpu->open_input = mpu401_open;
941 mpu->open_output = mpu401_open;
942 mpu->close_input = mpu401_close;
943 mpu->close_output = mpu401_close;
944 mpu->private_data = sscape;
945 sscape->mpu = mpu;
946
947 initialise_mpu401(mpu);
948 }
949
950 return err;
951}
952
953
954/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
956 * is very much like a CS4231, with a few extra bits. We will
957 * try to support at least some of the extra bits by overriding
958 * some of the CS4231 callback.
959 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200960static int __devinit create_ad1845(struct snd_card *card, unsigned port,
961 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 register struct soundscape *sscape = get_card_soundscape(card);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200964 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int err;
966
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200967 if (sscape->type == SSCAPE_VIVO)
968 port += 4;
969
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200970 if (dma1 == dma2)
971 dma2 = -1;
972
Krzysztof Helt7779f752008-07-31 21:03:41 +0200973 err = snd_wss_create(card, port, -1, irq, dma1, dma2,
974 WSS_HW_DETECT, WSS_HWSHARE_DMA1, &chip);
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200975 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 unsigned long flags;
Takashi Iwaibe624532005-11-17 14:42:05 +0100977 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/*
980 * It turns out that the PLAYBACK_ENABLE bit is set
981 * by the lowlevel driver ...
982 *
983#define AD1845_IFACE_CONFIG \
984 (CS4231_AUTOCALIB | CS4231_RECORD_ENABLE | CS4231_PLAYBACK_ENABLE)
Krzysztof Helt7779f752008-07-31 21:03:41 +0200985 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200987 snd_wss_out(chip, CS4231_IFACE_CTRL, AD1845_IFACE_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200989 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
991
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200992 if (sscape->type != SSCAPE_VIVO) {
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200993 /*
994 * The input clock frequency on the SoundScape must
995 * be 14.31818 MHz, because we must set this register
996 * to get the playback to sound correct ...
997 */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200998 snd_wss_mce_up(chip);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200999 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt199f7972009-01-09 23:10:52 +01001000 snd_wss_out(chip, AD1845_CLOCK, 0x20);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001001 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001002 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Krzysztof Helt7779f752008-07-31 21:03:41 +02001006 err = snd_wss_pcm(chip, 0, &pcm);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001007 if (err < 0) {
1008 snd_printk(KERN_ERR "sscape: No PCM device "
1009 "for AD1845 chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 goto _error;
1011 }
1012
Krzysztof Helt7779f752008-07-31 21:03:41 +02001013 err = snd_wss_mixer(chip);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001014 if (err < 0) {
1015 snd_printk(KERN_ERR "sscape: No mixer device "
1016 "for AD1845 chip\n");
1017 goto _error;
1018 }
Krzysztof Helt199f7972009-01-09 23:10:52 +01001019 if (chip->hardware != WSS_HW_AD1848) {
1020 err = snd_wss_timer(chip, 0, NULL);
1021 if (err < 0) {
1022 snd_printk(KERN_ERR "sscape: No timer device "
1023 "for AD1845 chip\n");
1024 goto _error;
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001028 if (sscape->type != SSCAPE_VIVO) {
1029 err = snd_ctl_add(card,
1030 snd_ctl_new1(&midi_mixer_ctl, chip));
1031 if (err < 0) {
1032 snd_printk(KERN_ERR "sscape: Could not create "
1033 "MIDI mixer control\n");
1034 goto _error;
1035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
1038 strcpy(card->driver, "SoundScape");
1039 strcpy(card->shortname, pcm->name);
1040 snprintf(card->longname, sizeof(card->longname),
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001041 "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n",
1042 pcm->name, chip->port, chip->irq,
1043 chip->dma1, chip->dma2);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 sscape->chip = chip;
1046 }
1047
1048 _error:
1049 return err;
1050}
1051
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
1054 * Create an ALSA soundcard entry for the SoundScape, using
1055 * the given list of port, IRQ and DMA resources.
1056 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001057static int __devinit create_sscape(int dev, struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001059 struct soundscape *sscape = get_card_soundscape(card);
1060 unsigned dma_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 unsigned irq_cfg;
1062 unsigned mpu_irq_cfg;
Takashi Iwai277e9262005-11-17 17:13:12 +01001063 unsigned xport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct resource *io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001065 struct resource *wss_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 unsigned long flags;
1067 int err;
1068
1069 /*
1070 * Check that the user didn't pass us garbage data ...
1071 */
Takashi Iwai277e9262005-11-17 17:13:12 +01001072 irq_cfg = get_irq_config(irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (irq_cfg == INVALID_IRQ) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001074 snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return -ENXIO;
1076 }
1077
Takashi Iwai277e9262005-11-17 17:13:12 +01001078 mpu_irq_cfg = get_irq_config(mpu_irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (mpu_irq_cfg == INVALID_IRQ) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001080 printk(KERN_ERR "sscape: Invalid IRQ %d\n", mpu_irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -ENXIO;
1082 }
Takashi Iwai277e9262005-11-17 17:13:12 +01001083 xport = port[dev];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /*
1086 * Grab IO ports that we will need to probe so that we
1087 * can detect and control this hardware ...
1088 */
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001089 io_res = request_region(xport, 8, "SoundScape");
1090 if (!io_res) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001091 snd_printk(KERN_ERR "sscape: can't grab port 0x%x\n", xport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EBUSY;
1093 }
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001094 wss_res = NULL;
1095 if (sscape->type == SSCAPE_VIVO) {
1096 wss_res = request_region(wss_port[dev], 4, "SoundScape");
1097 if (!wss_res) {
1098 snd_printk(KERN_ERR "sscape: can't grab port 0x%lx\n",
1099 wss_port[dev]);
1100 err = -EBUSY;
1101 goto _release_region;
1102 }
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 /*
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001106 * Grab one DMA channel ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001108 err = request_dma(dma[dev], "SoundScape");
1109 if (err < 0) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001110 snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 goto _release_region;
1112 }
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 spin_lock_init(&sscape->lock);
1115 spin_lock_init(&sscape->fwlock);
1116 sscape->io_res = io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001117 sscape->wss_res = wss_res;
Takashi Iwai277e9262005-11-17 17:13:12 +01001118 sscape->io_base = xport;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001119 sscape->wss_base = wss_port[dev];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (!detect_sscape(sscape)) {
1122 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1123 err = -ENODEV;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001124 goto _release_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001128 sscape->io_base, irq[dev], dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001130 if (sscape->type != SSCAPE_VIVO) {
1131 /*
1132 * Now create the hardware-specific device so that we can
1133 * load the microcode into the on-board processor.
1134 * We cannot use the MPU-401 MIDI system until this firmware
1135 * has been loaded into the card.
1136 */
1137 err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw));
1138 if (err < 0) {
1139 printk(KERN_ERR "sscape: Failed to create "
1140 "firmware device\n");
1141 goto _release_dma;
1142 }
1143 strlcpy(sscape->hw->name, "SoundScape M68K",
1144 sizeof(sscape->hw->name));
1145 sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
1146 sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
1147 sscape->hw->ops.open = sscape_hw_open;
1148 sscape->hw->ops.release = sscape_hw_release;
1149 sscape->hw->ops.ioctl = sscape_hw_ioctl;
1150 sscape->hw->private_data = sscape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 /*
1154 * Tell the on-board devices where their resources are (I think -
1155 * I can't be sure without a datasheet ... So many magic values!)
1156 */
1157 spin_lock_irqsave(&sscape->lock, flags);
1158
1159 activate_ad1845_unsafe(sscape->io_base);
1160
1161 sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
1162 sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
1163 sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
1164
1165 /*
1166 * Enable and configure the DMA channels ...
1167 */
1168 sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
1169 dma_cfg = (sscape->ic_type == IC_ODIE ? 0x70 : 0x40);
1170 sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
1171 sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
1172
1173 sscape_write_unsafe(sscape->io_base,
1174 GA_INTCFG_REG, 0xf0 | (mpu_irq_cfg << 2) | mpu_irq_cfg);
1175 sscape_write_unsafe(sscape->io_base,
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001176 GA_CDCFG_REG, 0x09 | DMA_8BIT
1177 | (dma[dev] << 4) | (irq_cfg << 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 spin_unlock_irqrestore(&sscape->lock, flags);
1180
1181 /*
1182 * We have now enabled the codec chip, and so we should
1183 * detect the AD1845 device ...
1184 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001185 err = create_ad1845(card, wss_port[dev], irq[dev],
1186 dma[dev], dma2[dev]);
1187 if (err < 0) {
1188 printk(KERN_ERR "sscape: No AD1845 device at 0x%lx, IRQ %d\n",
1189 wss_port[dev], irq[dev]);
1190 goto _release_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192#define MIDI_DEVNUM 0
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001193 if (sscape->type != SSCAPE_VIVO) {
1194 err = create_mpu401(card, MIDI_DEVNUM,
1195 MPU401_IO(xport), mpu_irq[dev]);
1196 if (err < 0) {
1197 printk(KERN_ERR "sscape: Failed to create "
1198 "MPU-401 device at 0x%x\n",
1199 MPU401_IO(xport));
1200 goto _release_dma;
1201 }
1202
1203 /*
1204 * Enable the master IRQ ...
1205 */
1206 sscape_write(sscape, GA_INTENA_REG, 0x80);
1207
1208 /*
1209 * Initialize mixer
1210 */
1211 sscape->midi_vol = 0;
1212 host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
1213 host_write_ctrl_unsafe(sscape->io_base, 0, 100);
1214 host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
1217 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * Now that we have successfully created this sound card,
1219 * it is safe to store the pointer.
1220 * NOTE: we only register the sound card's "destructor"
1221 * function now that our "constructor" has completed.
1222 */
1223 card->private_free = soundscape_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 return 0;
1226
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001227_release_dma:
Takashi Iwai277e9262005-11-17 17:13:12 +01001228 free_dma(dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001230_release_region:
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001231 release_and_free_resource(wss_res);
Takashi Iwaib1d57762005-10-10 11:56:31 +02001232 release_and_free_resource(io_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 return err;
1235}
1236
1237
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001238static int __devinit snd_sscape_match(struct device *pdev, unsigned int i)
Takashi Iwai277e9262005-11-17 17:13:12 +01001239{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001240 /*
1241 * Make sure we were given ALL of the other parameters.
1242 */
1243 if (port[i] == SNDRV_AUTO_PORT)
1244 return 0;
1245
1246 if (irq[i] == SNDRV_AUTO_IRQ ||
1247 mpu_irq[i] == SNDRV_AUTO_IRQ ||
1248 dma[i] == SNDRV_AUTO_DMA) {
1249 printk(KERN_INFO
1250 "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1251 return 0;
1252 }
1253
1254 return 1;
1255}
1256
1257static int __devinit snd_sscape_probe(struct device *pdev, unsigned int dev)
1258{
Takashi Iwai277e9262005-11-17 17:13:12 +01001259 struct snd_card *card;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001260 struct soundscape *sscape;
Takashi Iwai277e9262005-11-17 17:13:12 +01001261 int ret;
1262
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001263 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1264 sizeof(struct soundscape));
1265 if (!card)
1266 return -ENOMEM;
1267
1268 sscape = get_card_soundscape(card);
1269 sscape->type = SSCAPE;
1270
Takashi Iwai277e9262005-11-17 17:13:12 +01001271 dma[dev] &= 0x03;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001272 ret = create_sscape(dev, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001273 if (ret < 0)
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001274 goto _release_card;
1275
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001276 snd_card_set_dev(card, pdev);
Takashi Iwai277e9262005-11-17 17:13:12 +01001277 if ((ret = snd_card_register(card)) < 0) {
1278 printk(KERN_ERR "sscape: Failed to register sound card\n");
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001279 goto _release_card;
Takashi Iwai277e9262005-11-17 17:13:12 +01001280 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001281 dev_set_drvdata(pdev, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001282 return 0;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001283
1284_release_card:
1285 snd_card_free(card);
1286 return ret;
Takashi Iwai277e9262005-11-17 17:13:12 +01001287}
1288
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001289static int __devexit snd_sscape_remove(struct device *devptr, unsigned int dev)
Takashi Iwai277e9262005-11-17 17:13:12 +01001290{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001291 snd_card_free(dev_get_drvdata(devptr));
1292 dev_set_drvdata(devptr, NULL);
Takashi Iwai277e9262005-11-17 17:13:12 +01001293 return 0;
1294}
1295
Rene Herman83c51c02007-03-20 11:33:46 +01001296#define DEV_NAME "sscape"
Takashi Iwai277e9262005-11-17 17:13:12 +01001297
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001298static struct isa_driver snd_sscape_driver = {
1299 .match = snd_sscape_match,
Takashi Iwai277e9262005-11-17 17:13:12 +01001300 .probe = snd_sscape_probe,
1301 .remove = __devexit_p(snd_sscape_remove),
1302 /* FIXME: suspend/resume */
1303 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001304 .name = DEV_NAME
Takashi Iwai277e9262005-11-17 17:13:12 +01001305 },
1306};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308#ifdef CONFIG_PNP
1309static inline int __devinit get_next_autoindex(int i)
1310{
Takashi Iwai277e9262005-11-17 17:13:12 +01001311 while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return i;
1314}
1315
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1318 const struct pnp_card_device_id *pid)
1319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 static int idx = 0;
Takashi Iwai277e9262005-11-17 17:13:12 +01001321 struct pnp_dev *dev;
1322 struct snd_card *card;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001323 struct soundscape *sscape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 int ret;
1325
1326 /*
1327 * Allow this function to fail *quietly* if all the ISA PnP
1328 * devices were configured using module parameters instead.
1329 */
Takashi Iwai277e9262005-11-17 17:13:12 +01001330 if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /*
1334 * We have found a candidate ISA PnP card. Now we
1335 * have to check that it has the devices that we
1336 * expect it to have.
1337 *
1338 * We will NOT try and autoconfigure all of the resources
1339 * needed and then activate the card as we are assuming that
1340 * has already been done at boot-time using /proc/isapnp.
1341 * We shall simply try to give each active card the resources
1342 * that it wants. This is a sensible strategy for a modular
1343 * system where unused modules are unloaded regularly.
1344 *
1345 * This strategy is utterly useless if we compile the driver
1346 * into the kernel, of course.
1347 */
1348 // printk(KERN_INFO "sscape: %s\n", card->name);
1349
1350 /*
1351 * Check that we still have room for another sound card ...
1352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
Takashi Iwai277e9262005-11-17 17:13:12 +01001354 if (! dev)
1355 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Takashi Iwai277e9262005-11-17 17:13:12 +01001357 if (!pnp_is_active(dev)) {
1358 if (pnp_activate_dev(dev) < 0) {
1359 printk(KERN_INFO "sscape: device is inactive\n");
1360 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362 }
1363
Takashi Iwai277e9262005-11-17 17:13:12 +01001364 /*
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001365 * Create a new ALSA sound card entry, in anticipation
1366 * of detecting our hardware ...
1367 */
1368 card = snd_card_new(index[idx], id[idx], THIS_MODULE,
1369 sizeof(struct soundscape));
1370 if (!card)
1371 return -ENOMEM;
1372
1373 sscape = get_card_soundscape(card);
1374
1375 /*
1376 * Identify card model ...
1377 */
1378 if (!strncmp("ENS4081", pid->id, 7))
1379 sscape->type = SSCAPE_VIVO;
1380 else
1381 sscape->type = SSCAPE_PNP;
1382
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001383 /*
Takashi Iwai277e9262005-11-17 17:13:12 +01001384 * Read the correct parameters off the ISA PnP bus ...
1385 */
1386 port[idx] = pnp_port_start(dev, 0);
1387 irq[idx] = pnp_irq(dev, 0);
1388 mpu_irq[idx] = pnp_irq(dev, 1);
1389 dma[idx] = pnp_dma(dev, 0) & 0x03;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001390 if (sscape->type == SSCAPE_PNP) {
1391 dma2[idx] = dma[idx];
1392 wss_port[idx] = CODEC_IO(port[idx]);
1393 } else {
1394 wss_port[idx] = pnp_port_start(dev, 1);
1395 dma2[idx] = pnp_dma(dev, 1);
1396 }
Takashi Iwai277e9262005-11-17 17:13:12 +01001397
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001398 ret = create_sscape(idx, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001399 if (ret < 0)
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001400 goto _release_card;
1401
Takashi Iwai277e9262005-11-17 17:13:12 +01001402 snd_card_set_dev(card, &pcard->card->dev);
1403 if ((ret = snd_card_register(card)) < 0) {
1404 printk(KERN_ERR "sscape: Failed to register sound card\n");
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001405 goto _release_card;
Takashi Iwai277e9262005-11-17 17:13:12 +01001406 }
1407
1408 pnp_set_card_drvdata(pcard, card);
1409 ++idx;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001410 return 0;
Takashi Iwai277e9262005-11-17 17:13:12 +01001411
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001412_release_card:
1413 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return ret;
1415}
1416
1417static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1418{
Takashi Iwai277e9262005-11-17 17:13:12 +01001419 snd_card_free(pnp_get_card_drvdata(pcard));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423static struct pnp_card_driver sscape_pnpc_driver = {
1424 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1425 .name = "sscape",
1426 .id_table = sscape_pnpids,
1427 .probe = sscape_pnp_detect,
1428 .remove = __devexit_p(sscape_pnp_remove),
1429};
1430
1431#endif /* CONFIG_PNP */
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433static int __init sscape_init(void)
1434{
Rene Herman609d7692007-05-15 11:42:56 +02001435 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Rene Herman609d7692007-05-15 11:42:56 +02001437 err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01001438#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02001439 if (!err)
1440 isa_registered = 1;
1441
1442 err = pnp_register_card_driver(&sscape_pnpc_driver);
1443 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01001444 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02001445
1446 if (isa_registered)
1447 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01001448#endif
Rene Herman609d7692007-05-15 11:42:56 +02001449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001452static void __exit sscape_exit(void)
1453{
1454#ifdef CONFIG_PNP
1455 if (pnp_registered)
1456 pnp_unregister_card_driver(&sscape_pnpc_driver);
Rene Herman609d7692007-05-15 11:42:56 +02001457 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001458#endif
Rene Herman609d7692007-05-15 11:42:56 +02001459 isa_unregister_driver(&snd_sscape_driver);
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001460}
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462module_init(sscape_init);
1463module_exit(sscape_exit);