blob: 681e2237acb7e6d95f03c972e23b7e431adcec72 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#define HOST_CTRL_IO(i) ((i) + 2)
93#define HOST_DATA_IO(i) ((i) + 3)
94#define ODIE_ADDR_IO(i) ((i) + 4)
95#define ODIE_DATA_IO(i) ((i) + 5)
96#define CODEC_IO(i) ((i) + 8)
97
98#define IC_ODIE 1
99#define IC_OPUS 2
100
101#define RX_READY 0x01
102#define TX_READY 0x02
103
104#define CMD_ACK 0x80
105#define CMD_SET_MIDI_VOL 0x84
106#define CMD_GET_MIDI_VOL 0x85
107#define CMD_XXX_MIDI_VOL 0x86
108#define CMD_SET_EXTMIDI 0x8a
109#define CMD_GET_EXTMIDI 0x8b
110#define CMD_SET_MT32 0x8c
111#define CMD_GET_MT32 0x8d
112
113enum GA_REG {
114 GA_INTSTAT_REG = 0,
115 GA_INTENA_REG,
116 GA_DMAA_REG,
117 GA_DMAB_REG,
118 GA_INTCFG_REG,
119 GA_DMACFG_REG,
120 GA_CDCFG_REG,
121 GA_SMCFGA_REG,
122 GA_SMCFGB_REG,
123 GA_HMCTL_REG
124};
125
126#define DMA_8BIT 0x80
127
128
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200129enum card_type {
130 SSCAPE,
131 SSCAPE_PNP,
132 SSCAPE_VIVO,
133};
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135struct soundscape {
136 spinlock_t lock;
137 unsigned io_base;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200138 unsigned wss_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int codec_type;
140 int ic_type;
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200141 enum card_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct resource *io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200143 struct resource *wss_res;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200144 struct snd_wss *chip;
Takashi Iwaibe624532005-11-17 14:42:05 +0100145 struct snd_mpu401 *mpu;
146 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /*
149 * The MIDI device won't work until we've loaded
150 * its firmware via a hardware-dependent device IOCTL
151 */
152 spinlock_t fwlock;
153 int hw_in_use;
154 unsigned long midi_usage;
155 unsigned char midi_vol;
156};
157
158#define INVALID_IRQ ((unsigned)-1)
159
160
Takashi Iwaibe624532005-11-17 14:42:05 +0100161static inline struct soundscape *get_card_soundscape(struct snd_card *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 return (struct soundscape *) (c->private_data);
164}
165
Takashi Iwaibe624532005-11-17 14:42:05 +0100166static inline struct soundscape *get_mpu401_soundscape(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 return (struct soundscape *) (mpu->private_data);
169}
170
Takashi Iwaibe624532005-11-17 14:42:05 +0100171static inline struct soundscape *get_hwdep_soundscape(struct snd_hwdep * hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 return (struct soundscape *) (hw->private_data);
174}
175
176
177/*
178 * Allocates some kernel memory that we can use for DMA.
179 * I think this means that the memory has to map to
180 * contiguous pages of physical memory.
181 */
182static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
183{
184 if (buf) {
185 if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
186 size, buf) < 0) {
187 snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", size);
188 return NULL;
189 }
190 }
191
192 return buf;
193}
194
195/*
196 * Release the DMA-able kernel memory ...
197 */
198static void free_dmabuf(struct snd_dma_buffer *buf)
199{
200 if (buf && buf->area)
201 snd_dma_free_pages(buf);
202}
203
204
205/*
206 * This function writes to the SoundScape's control registers,
207 * but doesn't do any locking. It's up to the caller to do that.
208 * This is why this function is "unsafe" ...
209 */
210static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
211{
212 outb(reg, ODIE_ADDR_IO(io_base));
213 outb(val, ODIE_DATA_IO(io_base));
214}
215
216/*
217 * Write to the SoundScape's control registers, and do the
218 * necessary locking ...
219 */
220static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
221{
222 unsigned long flags;
223
224 spin_lock_irqsave(&s->lock, flags);
225 sscape_write_unsafe(s->io_base, reg, val);
226 spin_unlock_irqrestore(&s->lock, flags);
227}
228
229/*
230 * Read from the SoundScape's control registers, but leave any
231 * locking to the caller. This is why the function is "unsafe" ...
232 */
233static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
234{
235 outb(reg, ODIE_ADDR_IO(io_base));
236 return inb(ODIE_DATA_IO(io_base));
237}
238
239/*
240 * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
241 */
242static inline void set_host_mode_unsafe(unsigned io_base)
243{
244 outb(0x0, HOST_CTRL_IO(io_base));
245}
246
247/*
248 * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
249 */
250static inline void set_midi_mode_unsafe(unsigned io_base)
251{
252 outb(0x3, HOST_CTRL_IO(io_base));
253}
254
255/*
256 * Read the SoundScape's host-mode control register, but leave
257 * any locking issues to the caller ...
258 */
259static inline int host_read_unsafe(unsigned io_base)
260{
261 int data = -1;
262 if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
263 data = inb(HOST_DATA_IO(io_base));
264 }
265
266 return data;
267}
268
269/*
270 * Read the SoundScape's host-mode control register, performing
271 * a limited amount of busy-waiting if the register isn't ready.
272 * Also leaves all locking-issues to the caller ...
273 */
274static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
275{
276 int data;
277
278 while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
279 udelay(100);
280 --timeout;
281 } /* while */
282
283 return data;
284}
285
286/*
287 * Write to the SoundScape's host-mode control registers, but
288 * leave any locking issues to the caller ...
289 */
290static inline int host_write_unsafe(unsigned io_base, unsigned char data)
291{
292 if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
293 outb(data, HOST_DATA_IO(io_base));
294 return 1;
295 }
296
297 return 0;
298}
299
300/*
301 * Write to the SoundScape's host-mode control registers, performing
302 * a limited amount of busy-waiting if the register isn't ready.
303 * Also leaves all locking-issues to the caller ...
304 */
305static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
306 unsigned timeout)
307{
308 int err;
309
310 while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
311 udelay(100);
312 --timeout;
313 } /* while */
314
315 return err;
316}
317
318
319/*
320 * Check that the MIDI subsystem is operational. If it isn't,
321 * then we will hang the computer if we try to use it ...
322 *
323 * NOTE: This check is based upon observation, not documentation.
324 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100325static inline int verify_mpu401(const struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Krzysztof Heltc9864fd2009-01-21 08:19:27 +0100327 return ((inb(MPU401C(mpu)) & 0xc0) == 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/*
331 * This is apparently the standard way to initailise an MPU-401
332 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100333static inline void initialise_mpu401(const struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Krzysztof Heltc9864fd2009-01-21 08:19:27 +0100335 outb(0, MPU401D(mpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/*
339 * Tell the SoundScape to activate the AD1845 chip (I think).
340 * The AD1845 detection fails if we *don't* do this, so I
341 * think that this is a good idea ...
342 */
343static inline void activate_ad1845_unsafe(unsigned io_base)
344{
345 sscape_write_unsafe(io_base, GA_HMCTL_REG, (sscape_read_unsafe(io_base, GA_HMCTL_REG) & 0xcf) | 0x10);
346 sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
347}
348
349/*
350 * Do the necessary ALSA-level cleanup to deallocate our driver ...
351 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100352static void soundscape_free(struct snd_card *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200354 struct soundscape *sscape = get_card_soundscape(c);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200355 release_and_free_resource(sscape->io_res);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200356 release_and_free_resource(sscape->wss_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 free_dma(sscape->chip->dma1);
358}
359
360/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * Tell the SoundScape to begin a DMA tranfer using the given channel.
362 * All locking issues are left to the caller.
363 */
364static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
365{
366 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) | 0x01);
367 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) & 0xfe);
368}
369
370/*
371 * Wait for a DMA transfer to complete. This is a "limited busy-wait",
372 * and all locking issues are left to the caller.
373 */
374static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
375{
376 while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
377 udelay(100);
378 --timeout;
379 } /* while */
380
381 return (sscape_read_unsafe(io_base, reg) & 0x01);
382}
383
384/*
385 * Wait for the On-Board Processor to return its start-up
386 * acknowledgement sequence. This wait is too long for
387 * us to perform "busy-waiting", and so we must sleep.
388 * This in turn means that we must not be holding any
389 * spinlocks when we call this function.
390 */
391static int obp_startup_ack(struct soundscape *s, unsigned timeout)
392{
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100393 unsigned long end_time = jiffies + msecs_to_jiffies(timeout);
394
395 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 unsigned long flags;
397 unsigned char x;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 spin_lock_irqsave(&s->lock, flags);
400 x = inb(HOST_DATA_IO(s->io_base));
401 spin_unlock_irqrestore(&s->lock, flags);
402 if ((x & 0xfe) == 0xfe)
403 return 1;
404
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100405 msleep(10);
406 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 return 0;
409}
410
411/*
412 * Wait for the host to return its start-up acknowledgement
413 * sequence. This wait is too long for us to perform
414 * "busy-waiting", and so we must sleep. This in turn means
415 * that we must not be holding any spinlocks when we call
416 * this function.
417 */
418static int host_startup_ack(struct soundscape *s, unsigned timeout)
419{
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100420 unsigned long end_time = jiffies + msecs_to_jiffies(timeout);
421
422 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 unsigned long flags;
424 unsigned char x;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 spin_lock_irqsave(&s->lock, flags);
427 x = inb(HOST_DATA_IO(s->io_base));
428 spin_unlock_irqrestore(&s->lock, flags);
429 if (x == 0xfe)
430 return 1;
431
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100432 msleep(10);
433 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 return 0;
436}
437
438/*
439 * Upload a byte-stream into the SoundScape using DMA channel A.
440 */
441static int upload_dma_data(struct soundscape *s,
442 const unsigned char __user *data,
443 size_t size)
444{
445 unsigned long flags;
446 struct snd_dma_buffer dma;
447 int ret;
448
449 if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
450 return -ENOMEM;
451
452 spin_lock_irqsave(&s->lock, flags);
453
454 /*
455 * Reset the board ...
456 */
457 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
458
459 /*
460 * Enable the DMA channels and configure them ...
461 */
462 sscape_write_unsafe(s->io_base, GA_DMACFG_REG, 0x50);
463 sscape_write_unsafe(s->io_base, GA_DMAA_REG, (s->chip->dma1 << 4) | DMA_8BIT);
464 sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
465
466 /*
467 * Take the board out of reset ...
468 */
469 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
470
471 /*
472 * Upload the user's data (firmware?) to the SoundScape
473 * board through the DMA channel ...
474 */
475 while (size != 0) {
476 unsigned long len;
477
478 /*
479 * Apparently, copying to/from userspace can sleep.
480 * We are therefore forbidden from holding any
481 * spinlocks while we copy ...
482 */
483 spin_unlock_irqrestore(&s->lock, flags);
484
485 /*
486 * Remember that the data that we want to DMA
487 * comes from USERSPACE. We have already verified
488 * the userspace pointer ...
489 */
490 len = min(size, dma.bytes);
491 len -= __copy_from_user(dma.area, data, len);
492 data += len;
493 size -= len;
494
495 /*
496 * Grab that spinlock again, now that we've
497 * finished copying!
498 */
499 spin_lock_irqsave(&s->lock, flags);
500
501 snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
502 sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
503 if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
504 /*
505 * Don't forget to release this spinlock we're holding ...
506 */
507 spin_unlock_irqrestore(&s->lock, flags);
508
509 snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
510 ret = -EAGAIN;
511 goto _release_dma;
512 }
513 } /* while */
514
515 set_host_mode_unsafe(s->io_base);
516
517 /*
518 * Boot the board ... (I think)
519 */
520 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x40);
521 spin_unlock_irqrestore(&s->lock, flags);
522
523 /*
524 * If all has gone well, then the board should acknowledge
525 * the new upload and tell us that it has rebooted OK. We
526 * give it 5 seconds (max) ...
527 */
528 ret = 0;
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100529 if (!obp_startup_ack(s, 5000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
531 ret = -EAGAIN;
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100532 } else if (!host_startup_ack(s, 5000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
534 ret = -EAGAIN;
535 }
536
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200537_release_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * NOTE!!! We are NOT holding any spinlocks at this point !!!
540 */
541 sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
542 free_dmabuf(&dma);
543
544 return ret;
545}
546
547/*
548 * Upload the bootblock(?) into the SoundScape. The only
549 * purpose of this block of code seems to be to tell
550 * us which version of the microcode we should be using.
551 *
552 * NOTE: The boot-block data resides in USER-SPACE!!!
553 * However, we have already verified its memory
554 * addresses by the time we get here.
555 */
556static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock __user *bb)
557{
558 unsigned long flags;
559 int data = 0;
560 int ret;
561
562 ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
563
564 spin_lock_irqsave(&sscape->lock, flags);
565 if (ret == 0) {
566 data = host_read_ctrl_unsafe(sscape->io_base, 100);
567 }
568 set_midi_mode_unsafe(sscape->io_base);
569 spin_unlock_irqrestore(&sscape->lock, flags);
570
571 if (ret == 0) {
572 if (data < 0) {
573 snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
574 ret = -EAGAIN;
575 }
576 else if (__copy_to_user(&bb->version, &data, sizeof(bb->version))) {
577 ret = -EFAULT;
578 }
579 }
580
581 return ret;
582}
583
584/*
585 * Upload the microcode into the SoundScape. The
586 * microcode is 64K of data, and if we try to copy
587 * it into a local variable then we will SMASH THE
588 * KERNEL'S STACK! We therefore leave it in USER
589 * SPACE, and save ourselves from copying it at all.
590 */
591static int sscape_upload_microcode(struct soundscape *sscape,
592 const struct sscape_microcode __user *mc)
593{
594 unsigned long flags;
595 char __user *code;
596 int err;
597
598 /*
599 * We are going to have to copy this data into a special
600 * DMA-able buffer before we can upload it. We shall therefore
601 * just check that the data pointer is valid for now.
602 *
603 * NOTE: This buffer is 64K long! That's WAY too big to
604 * copy into a stack-temporary anyway.
605 */
606 if ( get_user(code, &mc->code) ||
607 !access_ok(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE) )
608 return -EFAULT;
609
610 if ((err = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
611 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
612 }
613
614 spin_lock_irqsave(&sscape->lock, flags);
615 set_midi_mode_unsafe(sscape->io_base);
616 spin_unlock_irqrestore(&sscape->lock, flags);
617
618 initialise_mpu401(sscape->mpu);
619
620 return err;
621}
622
623/*
624 * Hardware-specific device functions, to implement special
625 * IOCTLs for the SoundScape card. This is how we upload
626 * the microcode into the card, for example, and so we
627 * must ensure that no two processes can open this device
628 * simultaneously, and that we can't open it at all if
629 * someone is using the MIDI device.
630 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100631static int sscape_hw_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 register struct soundscape *sscape = get_hwdep_soundscape(hw);
634 unsigned long flags;
635 int err;
636
637 spin_lock_irqsave(&sscape->fwlock, flags);
638
639 if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
640 err = -EBUSY;
641 } else {
642 sscape->hw_in_use = 1;
643 err = 0;
644 }
645
646 spin_unlock_irqrestore(&sscape->fwlock, flags);
647 return err;
648}
649
Takashi Iwaibe624532005-11-17 14:42:05 +0100650static int sscape_hw_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 register struct soundscape *sscape = get_hwdep_soundscape(hw);
653 unsigned long flags;
654
655 spin_lock_irqsave(&sscape->fwlock, flags);
656 sscape->hw_in_use = 0;
657 spin_unlock_irqrestore(&sscape->fwlock, flags);
658 return 0;
659}
660
Takashi Iwaibe624532005-11-17 14:42:05 +0100661static int sscape_hw_ioctl(struct snd_hwdep * hw, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 unsigned int cmd, unsigned long arg)
663{
664 struct soundscape *sscape = get_hwdep_soundscape(hw);
665 int err = -EBUSY;
666
667 switch (cmd) {
668 case SND_SSCAPE_LOAD_BOOTB:
669 {
670 register struct sscape_bootblock __user *bb = (struct sscape_bootblock __user *) arg;
671
672 /*
673 * We are going to have to copy this data into a special
674 * DMA-able buffer before we can upload it. We shall therefore
675 * just check that the data pointer is valid for now ...
676 */
677 if ( !access_ok(VERIFY_READ, bb->code, sizeof(bb->code)) )
678 return -EFAULT;
679
680 /*
681 * Now check that we can write the firmware version number too...
682 */
683 if ( !access_ok(VERIFY_WRITE, &bb->version, sizeof(bb->version)) )
684 return -EFAULT;
685
686 err = sscape_upload_bootblock(sscape, bb);
687 }
688 break;
689
690 case SND_SSCAPE_LOAD_MCODE:
691 {
692 register const struct sscape_microcode __user *mc = (const struct sscape_microcode __user *) arg;
693
694 err = sscape_upload_microcode(sscape, mc);
695 }
696 break;
697
698 default:
699 err = -EINVAL;
700 break;
701 } /* switch */
702
703 return err;
704}
705
706
707/*
708 * Mixer control for the SoundScape's MIDI device.
709 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100710static int sscape_midi_info(struct snd_kcontrol *ctl,
711 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
714 uinfo->count = 1;
715 uinfo->value.integer.min = 0;
716 uinfo->value.integer.max = 127;
717 return 0;
718}
719
Takashi Iwaibe624532005-11-17 14:42:05 +0100720static int sscape_midi_get(struct snd_kcontrol *kctl,
721 struct snd_ctl_elem_value *uctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200723 struct snd_wss *chip = snd_kcontrol_chip(kctl);
Takashi Iwaibe624532005-11-17 14:42:05 +0100724 struct snd_card *card = chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 register struct soundscape *s = get_card_soundscape(card);
726 unsigned long flags;
727
728 spin_lock_irqsave(&s->lock, flags);
729 set_host_mode_unsafe(s->io_base);
730
731 if (host_write_ctrl_unsafe(s->io_base, CMD_GET_MIDI_VOL, 100)) {
732 uctl->value.integer.value[0] = host_read_ctrl_unsafe(s->io_base, 100);
733 }
734
735 set_midi_mode_unsafe(s->io_base);
736 spin_unlock_irqrestore(&s->lock, flags);
737 return 0;
738}
739
Takashi Iwaibe624532005-11-17 14:42:05 +0100740static int sscape_midi_put(struct snd_kcontrol *kctl,
741 struct snd_ctl_elem_value *uctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200743 struct snd_wss *chip = snd_kcontrol_chip(kctl);
Takashi Iwaibe624532005-11-17 14:42:05 +0100744 struct snd_card *card = chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 register struct soundscape *s = get_card_soundscape(card);
746 unsigned long flags;
747 int change;
748
749 spin_lock_irqsave(&s->lock, flags);
750
751 /*
752 * We need to put the board into HOST mode before we
753 * can send any volume-changing HOST commands ...
754 */
755 set_host_mode_unsafe(s->io_base);
756
757 /*
758 * To successfully change the MIDI volume setting, you seem to
759 * have to write a volume command, write the new volume value,
760 * and then perform another volume-related command. Perhaps the
761 * first command is an "open" and the second command is a "close"?
762 */
763 if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
764 change = 0;
765 goto __skip_change;
766 }
767 change = (host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
768 && host_write_ctrl_unsafe(s->io_base, ((unsigned char) uctl->value.integer. value[0]) & 127, 100)
769 && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100));
770 __skip_change:
771
772 /*
773 * Take the board out of HOST mode and back into MIDI mode ...
774 */
775 set_midi_mode_unsafe(s->io_base);
776
777 spin_unlock_irqrestore(&s->lock, flags);
778 return change;
779}
780
Takashi Iwaibe624532005-11-17 14:42:05 +0100781static struct snd_kcontrol_new midi_mixer_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
783 .name = "MIDI",
784 .info = sscape_midi_info,
785 .get = sscape_midi_get,
786 .put = sscape_midi_put
787};
788
789/*
790 * The SoundScape can use two IRQs from a possible set of four.
791 * These IRQs are encoded as bit patterns so that they can be
792 * written to the control registers.
793 */
794static unsigned __devinit get_irq_config(int irq)
795{
796 static const int valid_irq[] = { 9, 5, 7, 10 };
797 unsigned cfg;
798
799 for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg) {
800 if (irq == valid_irq[cfg])
801 return cfg;
802 } /* for */
803
804 return INVALID_IRQ;
805}
806
807
808/*
809 * Perform certain arcane port-checks to see whether there
810 * is a SoundScape board lurking behind the given ports.
811 */
812static int __devinit detect_sscape(struct soundscape *s)
813{
814 unsigned long flags;
815 unsigned d;
816 int retval = 0;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200817 int codec = s->wss_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 spin_lock_irqsave(&s->lock, flags);
820
821 /*
822 * The following code is lifted from the original OSS driver,
823 * and as I don't have a datasheet I cannot really comment
824 * on what it is doing...
825 */
826 if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
827 goto _done;
828
829 d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
830 if ((d & 0x80) != 0)
831 goto _done;
832
833 if (d == 0) {
834 s->codec_type = 1;
835 s->ic_type = IC_ODIE;
836 } else if ((d & 0x60) != 0) {
837 s->codec_type = 2;
838 s->ic_type = IC_OPUS;
839 } else
840 goto _done;
841
842 outb(0xfa, ODIE_ADDR_IO(s->io_base));
843 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
844 goto _done;
845
846 outb(0xfe, ODIE_ADDR_IO(s->io_base));
847 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
848 goto _done;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200849
850 outb(0xfe, ODIE_ADDR_IO(s->io_base));
851 d = inb(ODIE_DATA_IO(s->io_base));
852 if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto _done;
854
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200855 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
856 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
857
858 if (s->type == SSCAPE_VIVO)
859 codec += 4;
860 /* wait for WSS codec */
861 for (d = 0; d < 500; d++) {
862 if ((inb(codec) & 0x80) == 0)
863 break;
864 spin_unlock_irqrestore(&s->lock, flags);
865 msleep(1);
866 spin_lock_irqsave(&s->lock, flags);
867 }
868 snd_printd(KERN_INFO "init delay = %d ms\n", d);
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /*
871 * SoundScape successfully detected!
872 */
873 retval = 1;
874
875 _done:
876 spin_unlock_irqrestore(&s->lock, flags);
877 return retval;
878}
879
880/*
881 * ALSA callback function, called when attempting to open the MIDI device.
882 * Check that the MIDI firmware has been loaded, because we don't want
883 * to crash the machine. Also check that someone isn't using the hardware
884 * IOCTL device.
885 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100886static int mpu401_open(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 int err;
889
890 if (!verify_mpu401(mpu)) {
891 snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
892 err = -ENODEV;
893 } else {
894 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
895 unsigned long flags;
896
897 spin_lock_irqsave(&sscape->fwlock, flags);
898
899 if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
900 err = -EBUSY;
901 } else {
902 ++(sscape->midi_usage);
903 err = 0;
904 }
905
906 spin_unlock_irqrestore(&sscape->fwlock, flags);
907 }
908
909 return err;
910}
911
Takashi Iwaibe624532005-11-17 14:42:05 +0100912static void mpu401_close(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
915 unsigned long flags;
916
917 spin_lock_irqsave(&sscape->fwlock, flags);
918 --(sscape->midi_usage);
919 spin_unlock_irqrestore(&sscape->fwlock, flags);
920}
921
922/*
923 * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
924 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100925static int __devinit create_mpu401(struct snd_card *card, int devnum, unsigned long port, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 struct soundscape *sscape = get_card_soundscape(card);
Takashi Iwaibe624532005-11-17 14:42:05 +0100928 struct snd_rawmidi *rawmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 int err;
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if ((err = snd_mpu401_uart_new(card, devnum,
932 MPU401_HW_MPU401,
Takashi Iwai302e4c22006-05-23 13:24:30 +0200933 port, MPU401_INFO_INTEGRATED,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700934 irq, IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 &rawmidi)) == 0) {
Takashi Iwaibe624532005-11-17 14:42:05 +0100936 struct snd_mpu401 *mpu = (struct snd_mpu401 *) rawmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 mpu->open_input = mpu401_open;
938 mpu->open_output = mpu401_open;
939 mpu->close_input = mpu401_close;
940 mpu->close_output = mpu401_close;
941 mpu->private_data = sscape;
942 sscape->mpu = mpu;
943
944 initialise_mpu401(mpu);
945 }
946
947 return err;
948}
949
950
951/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
953 * is very much like a CS4231, with a few extra bits. We will
954 * try to support at least some of the extra bits by overriding
955 * some of the CS4231 callback.
956 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200957static int __devinit create_ad1845(struct snd_card *card, unsigned port,
958 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 register struct soundscape *sscape = get_card_soundscape(card);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200961 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int err;
963
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200964 if (sscape->type == SSCAPE_VIVO)
965 port += 4;
966
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200967 if (dma1 == dma2)
968 dma2 = -1;
969
Krzysztof Helt7779f752008-07-31 21:03:41 +0200970 err = snd_wss_create(card, port, -1, irq, dma1, dma2,
971 WSS_HW_DETECT, WSS_HWSHARE_DMA1, &chip);
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200972 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 unsigned long flags;
Takashi Iwaibe624532005-11-17 14:42:05 +0100974 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976/*
977 * It turns out that the PLAYBACK_ENABLE bit is set
978 * by the lowlevel driver ...
979 *
980#define AD1845_IFACE_CONFIG \
981 (CS4231_AUTOCALIB | CS4231_RECORD_ENABLE | CS4231_PLAYBACK_ENABLE)
Krzysztof Helt7779f752008-07-31 21:03:41 +0200982 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200984 snd_wss_out(chip, CS4231_IFACE_CTRL, AD1845_IFACE_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200986 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
988
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200989 if (sscape->type != SSCAPE_VIVO) {
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200990 /*
991 * The input clock frequency on the SoundScape must
992 * be 14.31818 MHz, because we must set this register
993 * to get the playback to sound correct ...
994 */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200995 snd_wss_mce_up(chip);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200996 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt199f7972009-01-09 23:10:52 +0100997 snd_wss_out(chip, AD1845_CLOCK, 0x20);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200998 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200999 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Krzysztof Helt7779f752008-07-31 21:03:41 +02001003 err = snd_wss_pcm(chip, 0, &pcm);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001004 if (err < 0) {
1005 snd_printk(KERN_ERR "sscape: No PCM device "
1006 "for AD1845 chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto _error;
1008 }
1009
Krzysztof Helt7779f752008-07-31 21:03:41 +02001010 err = snd_wss_mixer(chip);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001011 if (err < 0) {
1012 snd_printk(KERN_ERR "sscape: No mixer device "
1013 "for AD1845 chip\n");
1014 goto _error;
1015 }
Krzysztof Helt199f7972009-01-09 23:10:52 +01001016 if (chip->hardware != WSS_HW_AD1848) {
1017 err = snd_wss_timer(chip, 0, NULL);
1018 if (err < 0) {
1019 snd_printk(KERN_ERR "sscape: No timer device "
1020 "for AD1845 chip\n");
1021 goto _error;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001025 if (sscape->type != SSCAPE_VIVO) {
1026 err = snd_ctl_add(card,
1027 snd_ctl_new1(&midi_mixer_ctl, chip));
1028 if (err < 0) {
1029 snd_printk(KERN_ERR "sscape: Could not create "
1030 "MIDI mixer control\n");
1031 goto _error;
1032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
1035 strcpy(card->driver, "SoundScape");
1036 strcpy(card->shortname, pcm->name);
1037 snprintf(card->longname, sizeof(card->longname),
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001038 "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n",
1039 pcm->name, chip->port, chip->irq,
1040 chip->dma1, chip->dma2);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 sscape->chip = chip;
1043 }
1044
1045 _error:
1046 return err;
1047}
1048
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050/*
1051 * Create an ALSA soundcard entry for the SoundScape, using
1052 * the given list of port, IRQ and DMA resources.
1053 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001054static int __devinit create_sscape(int dev, struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001056 struct soundscape *sscape = get_card_soundscape(card);
1057 unsigned dma_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 unsigned irq_cfg;
1059 unsigned mpu_irq_cfg;
Takashi Iwai277e9262005-11-17 17:13:12 +01001060 unsigned xport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct resource *io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001062 struct resource *wss_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 unsigned long flags;
1064 int err;
1065
1066 /*
1067 * Check that the user didn't pass us garbage data ...
1068 */
Takashi Iwai277e9262005-11-17 17:13:12 +01001069 irq_cfg = get_irq_config(irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (irq_cfg == INVALID_IRQ) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001071 snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -ENXIO;
1073 }
1074
Takashi Iwai277e9262005-11-17 17:13:12 +01001075 mpu_irq_cfg = get_irq_config(mpu_irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (mpu_irq_cfg == INVALID_IRQ) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001077 printk(KERN_ERR "sscape: Invalid IRQ %d\n", mpu_irq[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -ENXIO;
1079 }
Takashi Iwai277e9262005-11-17 17:13:12 +01001080 xport = port[dev];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
1083 * Grab IO ports that we will need to probe so that we
1084 * can detect and control this hardware ...
1085 */
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001086 io_res = request_region(xport, 8, "SoundScape");
1087 if (!io_res) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001088 snd_printk(KERN_ERR "sscape: can't grab port 0x%x\n", xport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return -EBUSY;
1090 }
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001091 wss_res = NULL;
1092 if (sscape->type == SSCAPE_VIVO) {
1093 wss_res = request_region(wss_port[dev], 4, "SoundScape");
1094 if (!wss_res) {
1095 snd_printk(KERN_ERR "sscape: can't grab port 0x%lx\n",
1096 wss_port[dev]);
1097 err = -EBUSY;
1098 goto _release_region;
1099 }
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /*
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001103 * Grab one DMA channel ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001105 err = request_dma(dma[dev], "SoundScape");
1106 if (err < 0) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001107 snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto _release_region;
1109 }
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 spin_lock_init(&sscape->lock);
1112 spin_lock_init(&sscape->fwlock);
1113 sscape->io_res = io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001114 sscape->wss_res = wss_res;
Takashi Iwai277e9262005-11-17 17:13:12 +01001115 sscape->io_base = xport;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001116 sscape->wss_base = wss_port[dev];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (!detect_sscape(sscape)) {
1119 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1120 err = -ENODEV;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001121 goto _release_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
1124 printk(KERN_INFO "sscape: hardware detected at 0x%x, using IRQ %d, DMA %d\n",
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001125 sscape->io_base, irq[dev], dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001127 if (sscape->type != SSCAPE_VIVO) {
1128 /*
1129 * Now create the hardware-specific device so that we can
1130 * load the microcode into the on-board processor.
1131 * We cannot use the MPU-401 MIDI system until this firmware
1132 * has been loaded into the card.
1133 */
1134 err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw));
1135 if (err < 0) {
1136 printk(KERN_ERR "sscape: Failed to create "
1137 "firmware device\n");
1138 goto _release_dma;
1139 }
1140 strlcpy(sscape->hw->name, "SoundScape M68K",
1141 sizeof(sscape->hw->name));
1142 sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
1143 sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
1144 sscape->hw->ops.open = sscape_hw_open;
1145 sscape->hw->ops.release = sscape_hw_release;
1146 sscape->hw->ops.ioctl = sscape_hw_ioctl;
1147 sscape->hw->private_data = sscape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /*
1151 * Tell the on-board devices where their resources are (I think -
1152 * I can't be sure without a datasheet ... So many magic values!)
1153 */
1154 spin_lock_irqsave(&sscape->lock, flags);
1155
1156 activate_ad1845_unsafe(sscape->io_base);
1157
1158 sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
1159 sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
1160 sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
1161
1162 /*
1163 * Enable and configure the DMA channels ...
1164 */
1165 sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
1166 dma_cfg = (sscape->ic_type == IC_ODIE ? 0x70 : 0x40);
1167 sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
1168 sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
1169
1170 sscape_write_unsafe(sscape->io_base,
1171 GA_INTCFG_REG, 0xf0 | (mpu_irq_cfg << 2) | mpu_irq_cfg);
1172 sscape_write_unsafe(sscape->io_base,
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001173 GA_CDCFG_REG, 0x09 | DMA_8BIT
1174 | (dma[dev] << 4) | (irq_cfg << 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 spin_unlock_irqrestore(&sscape->lock, flags);
1177
1178 /*
1179 * We have now enabled the codec chip, and so we should
1180 * detect the AD1845 device ...
1181 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001182 err = create_ad1845(card, wss_port[dev], irq[dev],
1183 dma[dev], dma2[dev]);
1184 if (err < 0) {
1185 printk(KERN_ERR "sscape: No AD1845 device at 0x%lx, IRQ %d\n",
1186 wss_port[dev], irq[dev]);
1187 goto _release_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189#define MIDI_DEVNUM 0
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001190 if (sscape->type != SSCAPE_VIVO) {
Krzysztof Heltc9864fd2009-01-21 08:19:27 +01001191 err = create_mpu401(card, MIDI_DEVNUM, xport, mpu_irq[dev]);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001192 if (err < 0) {
1193 printk(KERN_ERR "sscape: Failed to create "
1194 "MPU-401 device at 0x%x\n",
Krzysztof Heltc9864fd2009-01-21 08:19:27 +01001195 xport);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001196 goto _release_dma;
1197 }
1198
1199 /*
1200 * Enable the master IRQ ...
1201 */
1202 sscape_write(sscape, GA_INTENA_REG, 0x80);
1203
1204 /*
1205 * Initialize mixer
1206 */
1207 sscape->midi_vol = 0;
1208 host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
1209 host_write_ctrl_unsafe(sscape->io_base, 0, 100);
1210 host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
1213 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 * Now that we have successfully created this sound card,
1215 * it is safe to store the pointer.
1216 * NOTE: we only register the sound card's "destructor"
1217 * function now that our "constructor" has completed.
1218 */
1219 card->private_free = soundscape_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 return 0;
1222
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001223_release_dma:
Takashi Iwai277e9262005-11-17 17:13:12 +01001224 free_dma(dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001226_release_region:
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001227 release_and_free_resource(wss_res);
Takashi Iwaib1d57762005-10-10 11:56:31 +02001228 release_and_free_resource(io_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 return err;
1231}
1232
1233
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001234static int __devinit snd_sscape_match(struct device *pdev, unsigned int i)
Takashi Iwai277e9262005-11-17 17:13:12 +01001235{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001236 /*
1237 * Make sure we were given ALL of the other parameters.
1238 */
1239 if (port[i] == SNDRV_AUTO_PORT)
1240 return 0;
1241
1242 if (irq[i] == SNDRV_AUTO_IRQ ||
1243 mpu_irq[i] == SNDRV_AUTO_IRQ ||
1244 dma[i] == SNDRV_AUTO_DMA) {
1245 printk(KERN_INFO
1246 "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1247 return 0;
1248 }
1249
1250 return 1;
1251}
1252
1253static int __devinit snd_sscape_probe(struct device *pdev, unsigned int dev)
1254{
Takashi Iwai277e9262005-11-17 17:13:12 +01001255 struct snd_card *card;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001256 struct soundscape *sscape;
Takashi Iwai277e9262005-11-17 17:13:12 +01001257 int ret;
1258
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001259 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
1260 sizeof(struct soundscape));
1261 if (!card)
1262 return -ENOMEM;
1263
1264 sscape = get_card_soundscape(card);
1265 sscape->type = SSCAPE;
1266
Takashi Iwai277e9262005-11-17 17:13:12 +01001267 dma[dev] &= 0x03;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001268 ret = create_sscape(dev, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001269 if (ret < 0)
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001270 goto _release_card;
1271
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001272 snd_card_set_dev(card, pdev);
Takashi Iwai277e9262005-11-17 17:13:12 +01001273 if ((ret = snd_card_register(card)) < 0) {
1274 printk(KERN_ERR "sscape: Failed to register sound card\n");
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001275 goto _release_card;
Takashi Iwai277e9262005-11-17 17:13:12 +01001276 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001277 dev_set_drvdata(pdev, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001278 return 0;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001279
1280_release_card:
1281 snd_card_free(card);
1282 return ret;
Takashi Iwai277e9262005-11-17 17:13:12 +01001283}
1284
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001285static int __devexit snd_sscape_remove(struct device *devptr, unsigned int dev)
Takashi Iwai277e9262005-11-17 17:13:12 +01001286{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001287 snd_card_free(dev_get_drvdata(devptr));
1288 dev_set_drvdata(devptr, NULL);
Takashi Iwai277e9262005-11-17 17:13:12 +01001289 return 0;
1290}
1291
Rene Herman83c51c02007-03-20 11:33:46 +01001292#define DEV_NAME "sscape"
Takashi Iwai277e9262005-11-17 17:13:12 +01001293
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001294static struct isa_driver snd_sscape_driver = {
1295 .match = snd_sscape_match,
Takashi Iwai277e9262005-11-17 17:13:12 +01001296 .probe = snd_sscape_probe,
1297 .remove = __devexit_p(snd_sscape_remove),
1298 /* FIXME: suspend/resume */
1299 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001300 .name = DEV_NAME
Takashi Iwai277e9262005-11-17 17:13:12 +01001301 },
1302};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304#ifdef CONFIG_PNP
1305static inline int __devinit get_next_autoindex(int i)
1306{
Takashi Iwai277e9262005-11-17 17:13:12 +01001307 while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return i;
1310}
1311
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1314 const struct pnp_card_device_id *pid)
1315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 static int idx = 0;
Takashi Iwai277e9262005-11-17 17:13:12 +01001317 struct pnp_dev *dev;
1318 struct snd_card *card;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001319 struct soundscape *sscape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int ret;
1321
1322 /*
1323 * Allow this function to fail *quietly* if all the ISA PnP
1324 * devices were configured using module parameters instead.
1325 */
Takashi Iwai277e9262005-11-17 17:13:12 +01001326 if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /*
1330 * We have found a candidate ISA PnP card. Now we
1331 * have to check that it has the devices that we
1332 * expect it to have.
1333 *
1334 * We will NOT try and autoconfigure all of the resources
1335 * needed and then activate the card as we are assuming that
1336 * has already been done at boot-time using /proc/isapnp.
1337 * We shall simply try to give each active card the resources
1338 * that it wants. This is a sensible strategy for a modular
1339 * system where unused modules are unloaded regularly.
1340 *
1341 * This strategy is utterly useless if we compile the driver
1342 * into the kernel, of course.
1343 */
1344 // printk(KERN_INFO "sscape: %s\n", card->name);
1345
1346 /*
1347 * Check that we still have room for another sound card ...
1348 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
Takashi Iwai277e9262005-11-17 17:13:12 +01001350 if (! dev)
1351 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Takashi Iwai277e9262005-11-17 17:13:12 +01001353 if (!pnp_is_active(dev)) {
1354 if (pnp_activate_dev(dev) < 0) {
1355 printk(KERN_INFO "sscape: device is inactive\n");
1356 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358 }
1359
Takashi Iwai277e9262005-11-17 17:13:12 +01001360 /*
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001361 * Create a new ALSA sound card entry, in anticipation
1362 * of detecting our hardware ...
1363 */
1364 card = snd_card_new(index[idx], id[idx], THIS_MODULE,
1365 sizeof(struct soundscape));
1366 if (!card)
1367 return -ENOMEM;
1368
1369 sscape = get_card_soundscape(card);
1370
1371 /*
1372 * Identify card model ...
1373 */
1374 if (!strncmp("ENS4081", pid->id, 7))
1375 sscape->type = SSCAPE_VIVO;
1376 else
1377 sscape->type = SSCAPE_PNP;
1378
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001379 /*
Takashi Iwai277e9262005-11-17 17:13:12 +01001380 * Read the correct parameters off the ISA PnP bus ...
1381 */
1382 port[idx] = pnp_port_start(dev, 0);
1383 irq[idx] = pnp_irq(dev, 0);
1384 mpu_irq[idx] = pnp_irq(dev, 1);
1385 dma[idx] = pnp_dma(dev, 0) & 0x03;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001386 if (sscape->type == SSCAPE_PNP) {
1387 dma2[idx] = dma[idx];
1388 wss_port[idx] = CODEC_IO(port[idx]);
1389 } else {
1390 wss_port[idx] = pnp_port_start(dev, 1);
1391 dma2[idx] = pnp_dma(dev, 1);
1392 }
Takashi Iwai277e9262005-11-17 17:13:12 +01001393
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001394 ret = create_sscape(idx, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001395 if (ret < 0)
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001396 goto _release_card;
1397
Takashi Iwai277e9262005-11-17 17:13:12 +01001398 snd_card_set_dev(card, &pcard->card->dev);
1399 if ((ret = snd_card_register(card)) < 0) {
1400 printk(KERN_ERR "sscape: Failed to register sound card\n");
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001401 goto _release_card;
Takashi Iwai277e9262005-11-17 17:13:12 +01001402 }
1403
1404 pnp_set_card_drvdata(pcard, card);
1405 ++idx;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001406 return 0;
Takashi Iwai277e9262005-11-17 17:13:12 +01001407
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001408_release_card:
1409 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return ret;
1411}
1412
1413static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1414{
Takashi Iwai277e9262005-11-17 17:13:12 +01001415 snd_card_free(pnp_get_card_drvdata(pcard));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429static int __init sscape_init(void)
1430{
Rene Herman609d7692007-05-15 11:42:56 +02001431 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Rene Herman609d7692007-05-15 11:42:56 +02001433 err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01001434#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02001435 if (!err)
1436 isa_registered = 1;
1437
1438 err = pnp_register_card_driver(&sscape_pnpc_driver);
1439 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01001440 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02001441
1442 if (isa_registered)
1443 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01001444#endif
Rene Herman609d7692007-05-15 11:42:56 +02001445 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001448static void __exit sscape_exit(void)
1449{
1450#ifdef CONFIG_PNP
1451 if (pnp_registered)
1452 pnp_unregister_card_driver(&sscape_pnpc_driver);
Rene Herman609d7692007-05-15 11:42:56 +02001453 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001454#endif
Rene Herman609d7692007-05-15 11:42:56 +02001455 isa_unregister_driver(&snd_sscape_driver);
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001456}
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458module_init(sscape_init);
1459module_exit(sscape_exit);