blob: b11c35f6aefe35e257b51ece923d8e2c964bfe80 [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 {
Krzysztof Heltf0968e32009-09-27 23:08:40 +0200130 MEDIA_FX, /* Sequoia S-1000 */
131 SSCAPE, /* Sequoia S-2000 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200132 SSCAPE_PNP,
133 SSCAPE_VIVO,
134};
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136struct soundscape {
137 spinlock_t lock;
138 unsigned io_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int ic_type;
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200140 enum card_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct resource *io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200142 struct resource *wss_res;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200143 struct snd_wss *chip;
Takashi Iwaibe624532005-11-17 14:42:05 +0100144 struct snd_mpu401 *mpu;
145 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /*
148 * The MIDI device won't work until we've loaded
149 * its firmware via a hardware-dependent device IOCTL
150 */
151 spinlock_t fwlock;
152 int hw_in_use;
153 unsigned long midi_usage;
154 unsigned char midi_vol;
155};
156
157#define INVALID_IRQ ((unsigned)-1)
158
159
Takashi Iwaibe624532005-11-17 14:42:05 +0100160static inline struct soundscape *get_card_soundscape(struct snd_card *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 return (struct soundscape *) (c->private_data);
163}
164
Takashi Iwaibe624532005-11-17 14:42:05 +0100165static inline struct soundscape *get_mpu401_soundscape(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 return (struct soundscape *) (mpu->private_data);
168}
169
Takashi Iwaibe624532005-11-17 14:42:05 +0100170static inline struct soundscape *get_hwdep_soundscape(struct snd_hwdep * hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 return (struct soundscape *) (hw->private_data);
173}
174
175
176/*
177 * Allocates some kernel memory that we can use for DMA.
178 * I think this means that the memory has to map to
179 * contiguous pages of physical memory.
180 */
181static struct snd_dma_buffer *get_dmabuf(struct snd_dma_buffer *buf, unsigned long size)
182{
183 if (buf) {
184 if (snd_dma_alloc_pages_fallback(SNDRV_DMA_TYPE_DEV, snd_dma_isa_data(),
185 size, buf) < 0) {
186 snd_printk(KERN_ERR "sscape: Failed to allocate %lu bytes for DMA\n", size);
187 return NULL;
188 }
189 }
190
191 return buf;
192}
193
194/*
195 * Release the DMA-able kernel memory ...
196 */
197static void free_dmabuf(struct snd_dma_buffer *buf)
198{
199 if (buf && buf->area)
200 snd_dma_free_pages(buf);
201}
202
203
204/*
205 * This function writes to the SoundScape's control registers,
206 * but doesn't do any locking. It's up to the caller to do that.
207 * This is why this function is "unsafe" ...
208 */
209static inline void sscape_write_unsafe(unsigned io_base, enum GA_REG reg, unsigned char val)
210{
211 outb(reg, ODIE_ADDR_IO(io_base));
212 outb(val, ODIE_DATA_IO(io_base));
213}
214
215/*
216 * Write to the SoundScape's control registers, and do the
217 * necessary locking ...
218 */
219static void sscape_write(struct soundscape *s, enum GA_REG reg, unsigned char val)
220{
221 unsigned long flags;
222
223 spin_lock_irqsave(&s->lock, flags);
224 sscape_write_unsafe(s->io_base, reg, val);
225 spin_unlock_irqrestore(&s->lock, flags);
226}
227
228/*
229 * Read from the SoundScape's control registers, but leave any
230 * locking to the caller. This is why the function is "unsafe" ...
231 */
232static inline unsigned char sscape_read_unsafe(unsigned io_base, enum GA_REG reg)
233{
234 outb(reg, ODIE_ADDR_IO(io_base));
235 return inb(ODIE_DATA_IO(io_base));
236}
237
238/*
239 * Puts the SoundScape into "host" mode, as compared to "MIDI" mode
240 */
241static inline void set_host_mode_unsafe(unsigned io_base)
242{
243 outb(0x0, HOST_CTRL_IO(io_base));
244}
245
246/*
247 * Puts the SoundScape into "MIDI" mode, as compared to "host" mode
248 */
249static inline void set_midi_mode_unsafe(unsigned io_base)
250{
251 outb(0x3, HOST_CTRL_IO(io_base));
252}
253
254/*
255 * Read the SoundScape's host-mode control register, but leave
256 * any locking issues to the caller ...
257 */
258static inline int host_read_unsafe(unsigned io_base)
259{
260 int data = -1;
261 if ((inb(HOST_CTRL_IO(io_base)) & RX_READY) != 0) {
262 data = inb(HOST_DATA_IO(io_base));
263 }
264
265 return data;
266}
267
268/*
269 * Read the SoundScape's host-mode control register, performing
270 * a limited amount of busy-waiting if the register isn't ready.
271 * Also leaves all locking-issues to the caller ...
272 */
273static int host_read_ctrl_unsafe(unsigned io_base, unsigned timeout)
274{
275 int data;
276
277 while (((data = host_read_unsafe(io_base)) < 0) && (timeout != 0)) {
278 udelay(100);
279 --timeout;
280 } /* while */
281
282 return data;
283}
284
285/*
286 * Write to the SoundScape's host-mode control registers, but
287 * leave any locking issues to the caller ...
288 */
289static inline int host_write_unsafe(unsigned io_base, unsigned char data)
290{
291 if ((inb(HOST_CTRL_IO(io_base)) & TX_READY) != 0) {
292 outb(data, HOST_DATA_IO(io_base));
293 return 1;
294 }
295
296 return 0;
297}
298
299/*
300 * Write to the SoundScape's host-mode control registers, performing
301 * a limited amount of busy-waiting if the register isn't ready.
302 * Also leaves all locking-issues to the caller ...
303 */
304static int host_write_ctrl_unsafe(unsigned io_base, unsigned char data,
305 unsigned timeout)
306{
307 int err;
308
309 while (!(err = host_write_unsafe(io_base, data)) && (timeout != 0)) {
310 udelay(100);
311 --timeout;
312 } /* while */
313
314 return err;
315}
316
317
318/*
319 * Check that the MIDI subsystem is operational. If it isn't,
320 * then we will hang the computer if we try to use it ...
321 *
322 * NOTE: This check is based upon observation, not documentation.
323 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100324static inline int verify_mpu401(const struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Krzysztof Heltc9864fd2009-01-21 08:19:27 +0100326 return ((inb(MPU401C(mpu)) & 0xc0) == 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/*
330 * This is apparently the standard way to initailise an MPU-401
331 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100332static inline void initialise_mpu401(const struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Krzysztof Heltc9864fd2009-01-21 08:19:27 +0100334 outb(0, MPU401D(mpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/*
338 * Tell the SoundScape to activate the AD1845 chip (I think).
339 * The AD1845 detection fails if we *don't* do this, so I
340 * think that this is a good idea ...
341 */
342static inline void activate_ad1845_unsafe(unsigned io_base)
343{
344 sscape_write_unsafe(io_base, GA_HMCTL_REG, (sscape_read_unsafe(io_base, GA_HMCTL_REG) & 0xcf) | 0x10);
345 sscape_write_unsafe(io_base, GA_CDCFG_REG, 0x80);
346}
347
348/*
349 * Do the necessary ALSA-level cleanup to deallocate our driver ...
350 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100351static void soundscape_free(struct snd_card *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200353 struct soundscape *sscape = get_card_soundscape(c);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200354 release_and_free_resource(sscape->io_res);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200355 release_and_free_resource(sscape->wss_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 free_dma(sscape->chip->dma1);
357}
358
359/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * Tell the SoundScape to begin a DMA tranfer using the given channel.
361 * All locking issues are left to the caller.
362 */
363static inline void sscape_start_dma_unsafe(unsigned io_base, enum GA_REG reg)
364{
365 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) | 0x01);
366 sscape_write_unsafe(io_base, reg, sscape_read_unsafe(io_base, reg) & 0xfe);
367}
368
369/*
370 * Wait for a DMA transfer to complete. This is a "limited busy-wait",
371 * and all locking issues are left to the caller.
372 */
373static int sscape_wait_dma_unsafe(unsigned io_base, enum GA_REG reg, unsigned timeout)
374{
375 while (!(sscape_read_unsafe(io_base, reg) & 0x01) && (timeout != 0)) {
376 udelay(100);
377 --timeout;
378 } /* while */
379
380 return (sscape_read_unsafe(io_base, reg) & 0x01);
381}
382
383/*
384 * Wait for the On-Board Processor to return its start-up
385 * acknowledgement sequence. This wait is too long for
386 * us to perform "busy-waiting", and so we must sleep.
387 * This in turn means that we must not be holding any
388 * spinlocks when we call this function.
389 */
390static int obp_startup_ack(struct soundscape *s, unsigned timeout)
391{
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100392 unsigned long end_time = jiffies + msecs_to_jiffies(timeout);
393
394 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 unsigned long flags;
396 unsigned char x;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_lock_irqsave(&s->lock, flags);
399 x = inb(HOST_DATA_IO(s->io_base));
400 spin_unlock_irqrestore(&s->lock, flags);
401 if ((x & 0xfe) == 0xfe)
402 return 1;
403
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100404 msleep(10);
405 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 return 0;
408}
409
410/*
411 * Wait for the host to return its start-up acknowledgement
412 * sequence. This wait is too long for us to perform
413 * "busy-waiting", and so we must sleep. This in turn means
414 * that we must not be holding any spinlocks when we call
415 * this function.
416 */
417static int host_startup_ack(struct soundscape *s, unsigned timeout)
418{
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100419 unsigned long end_time = jiffies + msecs_to_jiffies(timeout);
420
421 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned long flags;
423 unsigned char x;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_lock_irqsave(&s->lock, flags);
426 x = inb(HOST_DATA_IO(s->io_base));
427 spin_unlock_irqrestore(&s->lock, flags);
428 if (x == 0xfe)
429 return 1;
430
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100431 msleep(10);
432 } while (time_before(jiffies, end_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 return 0;
435}
436
437/*
438 * Upload a byte-stream into the SoundScape using DMA channel A.
439 */
440static int upload_dma_data(struct soundscape *s,
441 const unsigned char __user *data,
442 size_t size)
443{
444 unsigned long flags;
445 struct snd_dma_buffer dma;
446 int ret;
447
448 if (!get_dmabuf(&dma, PAGE_ALIGN(size)))
449 return -ENOMEM;
450
451 spin_lock_irqsave(&s->lock, flags);
452
453 /*
454 * Reset the board ...
455 */
456 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f);
457
458 /*
459 * Enable the DMA channels and configure them ...
460 */
461 sscape_write_unsafe(s->io_base, GA_DMACFG_REG, 0x50);
462 sscape_write_unsafe(s->io_base, GA_DMAA_REG, (s->chip->dma1 << 4) | DMA_8BIT);
463 sscape_write_unsafe(s->io_base, GA_DMAB_REG, 0x20);
464
465 /*
466 * Take the board out of reset ...
467 */
468 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x80);
469
470 /*
471 * Upload the user's data (firmware?) to the SoundScape
472 * board through the DMA channel ...
473 */
474 while (size != 0) {
475 unsigned long len;
476
477 /*
478 * Apparently, copying to/from userspace can sleep.
479 * We are therefore forbidden from holding any
480 * spinlocks while we copy ...
481 */
482 spin_unlock_irqrestore(&s->lock, flags);
483
484 /*
485 * Remember that the data that we want to DMA
486 * comes from USERSPACE. We have already verified
487 * the userspace pointer ...
488 */
489 len = min(size, dma.bytes);
490 len -= __copy_from_user(dma.area, data, len);
491 data += len;
492 size -= len;
493
494 /*
495 * Grab that spinlock again, now that we've
496 * finished copying!
497 */
498 spin_lock_irqsave(&s->lock, flags);
499
500 snd_dma_program(s->chip->dma1, dma.addr, len, DMA_MODE_WRITE);
501 sscape_start_dma_unsafe(s->io_base, GA_DMAA_REG);
502 if (!sscape_wait_dma_unsafe(s->io_base, GA_DMAA_REG, 5000)) {
503 /*
504 * Don't forget to release this spinlock we're holding ...
505 */
506 spin_unlock_irqrestore(&s->lock, flags);
507
508 snd_printk(KERN_ERR "sscape: DMA upload has timed out\n");
509 ret = -EAGAIN;
510 goto _release_dma;
511 }
512 } /* while */
513
514 set_host_mode_unsafe(s->io_base);
515
516 /*
517 * Boot the board ... (I think)
518 */
519 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, sscape_read_unsafe(s->io_base, GA_HMCTL_REG) | 0x40);
520 spin_unlock_irqrestore(&s->lock, flags);
521
522 /*
523 * If all has gone well, then the board should acknowledge
524 * the new upload and tell us that it has rebooted OK. We
525 * give it 5 seconds (max) ...
526 */
527 ret = 0;
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100528 if (!obp_startup_ack(s, 5000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 snd_printk(KERN_ERR "sscape: No response from on-board processor after upload\n");
530 ret = -EAGAIN;
Krzysztof Helt554b91e2009-01-12 21:25:04 +0100531 } else if (!host_startup_ack(s, 5000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 snd_printk(KERN_ERR "sscape: SoundScape failed to initialise\n");
533 ret = -EAGAIN;
534 }
535
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200536_release_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * NOTE!!! We are NOT holding any spinlocks at this point !!!
539 */
540 sscape_write(s, GA_DMAA_REG, (s->ic_type == IC_ODIE ? 0x70 : 0x40));
541 free_dmabuf(&dma);
542
543 return ret;
544}
545
546/*
547 * Upload the bootblock(?) into the SoundScape. The only
548 * purpose of this block of code seems to be to tell
549 * us which version of the microcode we should be using.
550 *
551 * NOTE: The boot-block data resides in USER-SPACE!!!
552 * However, we have already verified its memory
553 * addresses by the time we get here.
554 */
555static int sscape_upload_bootblock(struct soundscape *sscape, struct sscape_bootblock __user *bb)
556{
557 unsigned long flags;
558 int data = 0;
559 int ret;
560
561 ret = upload_dma_data(sscape, bb->code, sizeof(bb->code));
562
563 spin_lock_irqsave(&sscape->lock, flags);
564 if (ret == 0) {
565 data = host_read_ctrl_unsafe(sscape->io_base, 100);
566 }
567 set_midi_mode_unsafe(sscape->io_base);
568 spin_unlock_irqrestore(&sscape->lock, flags);
569
570 if (ret == 0) {
571 if (data < 0) {
572 snd_printk(KERN_ERR "sscape: timeout reading firmware version\n");
573 ret = -EAGAIN;
574 }
575 else if (__copy_to_user(&bb->version, &data, sizeof(bb->version))) {
576 ret = -EFAULT;
577 }
578 }
579
580 return ret;
581}
582
583/*
584 * Upload the microcode into the SoundScape. The
585 * microcode is 64K of data, and if we try to copy
586 * it into a local variable then we will SMASH THE
587 * KERNEL'S STACK! We therefore leave it in USER
588 * SPACE, and save ourselves from copying it at all.
589 */
590static int sscape_upload_microcode(struct soundscape *sscape,
591 const struct sscape_microcode __user *mc)
592{
593 unsigned long flags;
594 char __user *code;
595 int err;
596
597 /*
598 * We are going to have to copy this data into a special
599 * DMA-able buffer before we can upload it. We shall therefore
600 * just check that the data pointer is valid for now.
601 *
602 * NOTE: This buffer is 64K long! That's WAY too big to
603 * copy into a stack-temporary anyway.
604 */
605 if ( get_user(code, &mc->code) ||
606 !access_ok(VERIFY_READ, code, SSCAPE_MICROCODE_SIZE) )
607 return -EFAULT;
608
609 if ((err = upload_dma_data(sscape, code, SSCAPE_MICROCODE_SIZE)) == 0) {
610 snd_printk(KERN_INFO "sscape: MIDI firmware loaded\n");
611 }
612
613 spin_lock_irqsave(&sscape->lock, flags);
614 set_midi_mode_unsafe(sscape->io_base);
615 spin_unlock_irqrestore(&sscape->lock, flags);
616
617 initialise_mpu401(sscape->mpu);
618
619 return err;
620}
621
622/*
623 * Hardware-specific device functions, to implement special
624 * IOCTLs for the SoundScape card. This is how we upload
625 * the microcode into the card, for example, and so we
626 * must ensure that no two processes can open this device
627 * simultaneously, and that we can't open it at all if
628 * someone is using the MIDI device.
629 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100630static int sscape_hw_open(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 register struct soundscape *sscape = get_hwdep_soundscape(hw);
633 unsigned long flags;
634 int err;
635
636 spin_lock_irqsave(&sscape->fwlock, flags);
637
638 if ((sscape->midi_usage != 0) || sscape->hw_in_use) {
639 err = -EBUSY;
640 } else {
641 sscape->hw_in_use = 1;
642 err = 0;
643 }
644
645 spin_unlock_irqrestore(&sscape->fwlock, flags);
646 return err;
647}
648
Takashi Iwaibe624532005-11-17 14:42:05 +0100649static int sscape_hw_release(struct snd_hwdep * hw, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 register struct soundscape *sscape = get_hwdep_soundscape(hw);
652 unsigned long flags;
653
654 spin_lock_irqsave(&sscape->fwlock, flags);
655 sscape->hw_in_use = 0;
656 spin_unlock_irqrestore(&sscape->fwlock, flags);
657 return 0;
658}
659
Takashi Iwaibe624532005-11-17 14:42:05 +0100660static int sscape_hw_ioctl(struct snd_hwdep * hw, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 unsigned int cmd, unsigned long arg)
662{
663 struct soundscape *sscape = get_hwdep_soundscape(hw);
664 int err = -EBUSY;
665
666 switch (cmd) {
667 case SND_SSCAPE_LOAD_BOOTB:
668 {
669 register struct sscape_bootblock __user *bb = (struct sscape_bootblock __user *) arg;
670
671 /*
672 * We are going to have to copy this data into a special
673 * DMA-able buffer before we can upload it. We shall therefore
674 * just check that the data pointer is valid for now ...
675 */
676 if ( !access_ok(VERIFY_READ, bb->code, sizeof(bb->code)) )
677 return -EFAULT;
678
679 /*
680 * Now check that we can write the firmware version number too...
681 */
682 if ( !access_ok(VERIFY_WRITE, &bb->version, sizeof(bb->version)) )
683 return -EFAULT;
684
685 err = sscape_upload_bootblock(sscape, bb);
686 }
687 break;
688
689 case SND_SSCAPE_LOAD_MCODE:
690 {
691 register const struct sscape_microcode __user *mc = (const struct sscape_microcode __user *) arg;
692
693 err = sscape_upload_microcode(sscape, mc);
694 }
695 break;
696
697 default:
698 err = -EINVAL;
699 break;
700 } /* switch */
701
702 return err;
703}
704
705
706/*
707 * Mixer control for the SoundScape's MIDI device.
708 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100709static int sscape_midi_info(struct snd_kcontrol *ctl,
710 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
713 uinfo->count = 1;
714 uinfo->value.integer.min = 0;
715 uinfo->value.integer.max = 127;
716 return 0;
717}
718
Takashi Iwaibe624532005-11-17 14:42:05 +0100719static int sscape_midi_get(struct snd_kcontrol *kctl,
720 struct snd_ctl_elem_value *uctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200722 struct snd_wss *chip = snd_kcontrol_chip(kctl);
Takashi Iwaibe624532005-11-17 14:42:05 +0100723 struct snd_card *card = chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 register struct soundscape *s = get_card_soundscape(card);
725 unsigned long flags;
726
727 spin_lock_irqsave(&s->lock, flags);
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100728 uctl->value.integer.value[0] = s->midi_vol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 spin_unlock_irqrestore(&s->lock, flags);
730 return 0;
731}
732
Takashi Iwaibe624532005-11-17 14:42:05 +0100733static int sscape_midi_put(struct snd_kcontrol *kctl,
734 struct snd_ctl_elem_value *uctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200736 struct snd_wss *chip = snd_kcontrol_chip(kctl);
Takashi Iwaibe624532005-11-17 14:42:05 +0100737 struct snd_card *card = chip->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 register struct soundscape *s = get_card_soundscape(card);
739 unsigned long flags;
740 int change;
741
742 spin_lock_irqsave(&s->lock, flags);
743
744 /*
745 * We need to put the board into HOST mode before we
746 * can send any volume-changing HOST commands ...
747 */
748 set_host_mode_unsafe(s->io_base);
749
750 /*
751 * To successfully change the MIDI volume setting, you seem to
752 * have to write a volume command, write the new volume value,
753 * and then perform another volume-related command. Perhaps the
754 * first command is an "open" and the second command is a "close"?
755 */
756 if (s->midi_vol == ((unsigned char) uctl->value.integer. value[0] & 127)) {
757 change = 0;
758 goto __skip_change;
759 }
760 change = (host_write_ctrl_unsafe(s->io_base, CMD_SET_MIDI_VOL, 100)
761 && host_write_ctrl_unsafe(s->io_base, ((unsigned char) uctl->value.integer. value[0]) & 127, 100)
762 && host_write_ctrl_unsafe(s->io_base, CMD_XXX_MIDI_VOL, 100));
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100763 s->midi_vol = (unsigned char) uctl->value.integer.value[0] & 127;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 __skip_change:
765
766 /*
767 * Take the board out of HOST mode and back into MIDI mode ...
768 */
769 set_midi_mode_unsafe(s->io_base);
770
771 spin_unlock_irqrestore(&s->lock, flags);
772 return change;
773}
774
Takashi Iwaibe624532005-11-17 14:42:05 +0100775static struct snd_kcontrol_new midi_mixer_ctl = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
777 .name = "MIDI",
778 .info = sscape_midi_info,
779 .get = sscape_midi_get,
780 .put = sscape_midi_put
781};
782
783/*
784 * The SoundScape can use two IRQs from a possible set of four.
785 * These IRQs are encoded as bit patterns so that they can be
786 * written to the control registers.
787 */
Krzysztof Heltf0968e32009-09-27 23:08:40 +0200788static unsigned __devinit get_irq_config(int sscape_type, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 static const int valid_irq[] = { 9, 5, 7, 10 };
Krzysztof Heltf0968e32009-09-27 23:08:40 +0200791 static const int old_irq[] = { 9, 7, 5, 15 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unsigned cfg;
793
Krzysztof Heltf0968e32009-09-27 23:08:40 +0200794 if (sscape_type == MEDIA_FX) {
795 for (cfg = 0; cfg < ARRAY_SIZE(old_irq); ++cfg)
796 if (irq == old_irq[cfg])
797 return cfg;
798 } else {
799 for (cfg = 0; cfg < ARRAY_SIZE(valid_irq); ++cfg)
800 if (irq == valid_irq[cfg])
801 return cfg;
802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 return INVALID_IRQ;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
808 * Perform certain arcane port-checks to see whether there
809 * is a SoundScape board lurking behind the given ports.
810 */
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100811static int __devinit detect_sscape(struct soundscape *s, long wss_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 unsigned long flags;
814 unsigned d;
815 int retval = 0;
816
817 spin_lock_irqsave(&s->lock, flags);
818
819 /*
820 * The following code is lifted from the original OSS driver,
821 * and as I don't have a datasheet I cannot really comment
822 * on what it is doing...
823 */
824 if ((inb(HOST_CTRL_IO(s->io_base)) & 0x78) != 0)
825 goto _done;
826
827 d = inb(ODIE_ADDR_IO(s->io_base)) & 0xf0;
828 if ((d & 0x80) != 0)
829 goto _done;
830
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100831 if (d == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 s->ic_type = IC_ODIE;
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100833 else if ((d & 0x60) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 s->ic_type = IC_OPUS;
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100835 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto _done;
837
838 outb(0xfa, ODIE_ADDR_IO(s->io_base));
839 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0a)
840 goto _done;
841
842 outb(0xfe, ODIE_ADDR_IO(s->io_base));
843 if ((inb(ODIE_ADDR_IO(s->io_base)) & 0x9f) != 0x0e)
844 goto _done;
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200845
846 outb(0xfe, ODIE_ADDR_IO(s->io_base));
847 d = inb(ODIE_DATA_IO(s->io_base));
848 if (s->type != SSCAPE_VIVO && (d & 0x9f) != 0x0e)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto _done;
850
Krzysztof Heltf0968e32009-09-27 23:08:40 +0200851 if (s->ic_type == IC_OPUS)
852 activate_ad1845_unsafe(s->io_base);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200853
854 if (s->type == SSCAPE_VIVO)
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100855 wss_io += 4;
Krzysztof Heltf0968e32009-09-27 23:08:40 +0200856
857 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
858 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
859
860 /* wait for WSS codec */
861 for (d = 0; d < 500; d++) {
862 if ((inb(wss_io) & 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
870 if ((inb(wss_io) & 0x80) != 0)
871 goto _done;
872
873 if (inb(wss_io + 2) == 0xff)
874 goto _done;
875
876 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
877 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d);
878
879 if ((inb(wss_io) & 0x80) != 0)
880 s->type = MEDIA_FX;
881
882 d = sscape_read_unsafe(s->io_base, GA_HMCTL_REG) & 0x3f;
883 sscape_write_unsafe(s->io_base, GA_HMCTL_REG, d | 0xc0);
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200884 /* wait for WSS codec */
885 for (d = 0; d < 500; d++) {
Krzysztof Helt453e37b2009-02-04 17:41:32 +0100886 if ((inb(wss_io) & 0x80) == 0)
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200887 break;
888 spin_unlock_irqrestore(&s->lock, flags);
889 msleep(1);
890 spin_lock_irqsave(&s->lock, flags);
891 }
892 snd_printd(KERN_INFO "init delay = %d ms\n", d);
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * SoundScape successfully detected!
896 */
897 retval = 1;
898
899 _done:
900 spin_unlock_irqrestore(&s->lock, flags);
901 return retval;
902}
903
904/*
905 * ALSA callback function, called when attempting to open the MIDI device.
906 * Check that the MIDI firmware has been loaded, because we don't want
907 * to crash the machine. Also check that someone isn't using the hardware
908 * IOCTL device.
909 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100910static int mpu401_open(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 int err;
913
914 if (!verify_mpu401(mpu)) {
915 snd_printk(KERN_ERR "sscape: MIDI disabled, please load firmware\n");
916 err = -ENODEV;
917 } else {
918 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
919 unsigned long flags;
920
921 spin_lock_irqsave(&sscape->fwlock, flags);
922
923 if (sscape->hw_in_use || (sscape->midi_usage == ULONG_MAX)) {
924 err = -EBUSY;
925 } else {
926 ++(sscape->midi_usage);
927 err = 0;
928 }
929
930 spin_unlock_irqrestore(&sscape->fwlock, flags);
931 }
932
933 return err;
934}
935
Takashi Iwaibe624532005-11-17 14:42:05 +0100936static void mpu401_close(struct snd_mpu401 * mpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 register struct soundscape *sscape = get_mpu401_soundscape(mpu);
939 unsigned long flags;
940
941 spin_lock_irqsave(&sscape->fwlock, flags);
942 --(sscape->midi_usage);
943 spin_unlock_irqrestore(&sscape->fwlock, flags);
944}
945
946/*
947 * Initialse an MPU-401 subdevice for MIDI support on the SoundScape.
948 */
Takashi Iwaibe624532005-11-17 14:42:05 +0100949static int __devinit create_mpu401(struct snd_card *card, int devnum, unsigned long port, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 struct soundscape *sscape = get_card_soundscape(card);
Takashi Iwaibe624532005-11-17 14:42:05 +0100952 struct snd_rawmidi *rawmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int err;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if ((err = snd_mpu401_uart_new(card, devnum,
956 MPU401_HW_MPU401,
Takashi Iwai302e4c22006-05-23 13:24:30 +0200957 port, MPU401_INFO_INTEGRATED,
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700958 irq, IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 &rawmidi)) == 0) {
Takashi Iwaibe624532005-11-17 14:42:05 +0100960 struct snd_mpu401 *mpu = (struct snd_mpu401 *) rawmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 mpu->open_input = mpu401_open;
962 mpu->open_output = mpu401_open;
963 mpu->close_input = mpu401_close;
964 mpu->close_output = mpu401_close;
965 mpu->private_data = sscape;
966 sscape->mpu = mpu;
967
968 initialise_mpu401(mpu);
969 }
970
971 return err;
972}
973
974
975/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * Create an AD1845 PCM subdevice on the SoundScape. The AD1845
977 * is very much like a CS4231, with a few extra bits. We will
978 * try to support at least some of the extra bits by overriding
979 * some of the CS4231 callback.
980 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200981static int __devinit create_ad1845(struct snd_card *card, unsigned port,
982 int irq, int dma1, int dma2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 register struct soundscape *sscape = get_card_soundscape(card);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200985 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int err;
987
Krzysztof Heltec1e7942007-09-17 17:57:37 +0200988 if (sscape->type == SSCAPE_VIVO)
989 port += 4;
990
Krzysztof Helt7779f752008-07-31 21:03:41 +0200991 err = snd_wss_create(card, port, -1, irq, dma1, dma2,
992 WSS_HW_DETECT, WSS_HWSHARE_DMA1, &chip);
Krzysztof Helt4996bca2007-09-17 16:23:13 +0200993 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 unsigned long flags;
Takashi Iwaibe624532005-11-17 14:42:05 +0100995 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/*
998 * It turns out that the PLAYBACK_ENABLE bit is set
999 * by the lowlevel driver ...
1000 *
1001#define AD1845_IFACE_CONFIG \
1002 (CS4231_AUTOCALIB | CS4231_RECORD_ENABLE | CS4231_PLAYBACK_ENABLE)
Krzysztof Helt7779f752008-07-31 21:03:41 +02001003 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001005 snd_wss_out(chip, CS4231_IFACE_CTRL, AD1845_IFACE_CONFIG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001007 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
1009
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001010 if (sscape->type != SSCAPE_VIVO) {
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001011 /*
1012 * The input clock frequency on the SoundScape must
1013 * be 14.31818 MHz, because we must set this register
1014 * to get the playback to sound correct ...
1015 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001016 snd_wss_mce_up(chip);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001017 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt199f7972009-01-09 23:10:52 +01001018 snd_wss_out(chip, AD1845_CLOCK, 0x20);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001019 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001020 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Krzysztof Helt7779f752008-07-31 21:03:41 +02001024 err = snd_wss_pcm(chip, 0, &pcm);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001025 if (err < 0) {
1026 snd_printk(KERN_ERR "sscape: No PCM device "
1027 "for AD1845 chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto _error;
1029 }
1030
Krzysztof Helt7779f752008-07-31 21:03:41 +02001031 err = snd_wss_mixer(chip);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001032 if (err < 0) {
1033 snd_printk(KERN_ERR "sscape: No mixer device "
1034 "for AD1845 chip\n");
1035 goto _error;
1036 }
Krzysztof Helt199f7972009-01-09 23:10:52 +01001037 if (chip->hardware != WSS_HW_AD1848) {
1038 err = snd_wss_timer(chip, 0, NULL);
1039 if (err < 0) {
1040 snd_printk(KERN_ERR "sscape: No timer device "
1041 "for AD1845 chip\n");
1042 goto _error;
1043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001046 if (sscape->type != SSCAPE_VIVO) {
1047 err = snd_ctl_add(card,
1048 snd_ctl_new1(&midi_mixer_ctl, chip));
1049 if (err < 0) {
1050 snd_printk(KERN_ERR "sscape: Could not create "
1051 "MIDI mixer control\n");
1052 goto _error;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
1056 strcpy(card->driver, "SoundScape");
1057 strcpy(card->shortname, pcm->name);
1058 snprintf(card->longname, sizeof(card->longname),
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001059 "%s at 0x%lx, IRQ %d, DMA1 %d, DMA2 %d\n",
1060 pcm->name, chip->port, chip->irq,
1061 chip->dma1, chip->dma2);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 sscape->chip = chip;
1064 }
1065
1066 _error:
1067 return err;
1068}
1069
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * Create an ALSA soundcard entry for the SoundScape, using
1073 * the given list of port, IRQ and DMA resources.
1074 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001075static int __devinit create_sscape(int dev, struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001077 struct soundscape *sscape = get_card_soundscape(card);
1078 unsigned dma_cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 unsigned irq_cfg;
1080 unsigned mpu_irq_cfg;
1081 struct resource *io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001082 struct resource *wss_res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 unsigned long flags;
1084 int err;
Krzysztof Heltf0968e32009-09-27 23:08:40 +02001085 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /*
1088 * Grab IO ports that we will need to probe so that we
1089 * can detect and control this hardware ...
1090 */
Krzysztof Helt453e37b2009-02-04 17:41:32 +01001091 io_res = request_region(port[dev], 8, "SoundScape");
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001092 if (!io_res) {
Krzysztof Helt453e37b2009-02-04 17:41:32 +01001093 snd_printk(KERN_ERR
1094 "sscape: can't grab port 0x%lx\n", port[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -EBUSY;
1096 }
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001097 wss_res = NULL;
1098 if (sscape->type == SSCAPE_VIVO) {
1099 wss_res = request_region(wss_port[dev], 4, "SoundScape");
1100 if (!wss_res) {
1101 snd_printk(KERN_ERR "sscape: can't grab port 0x%lx\n",
1102 wss_port[dev]);
1103 err = -EBUSY;
1104 goto _release_region;
1105 }
1106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /*
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001109 * Grab one DMA channel ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001111 err = request_dma(dma[dev], "SoundScape");
1112 if (err < 0) {
Takashi Iwai277e9262005-11-17 17:13:12 +01001113 snd_printk(KERN_ERR "sscape: can't grab DMA %d\n", dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 goto _release_region;
1115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 spin_lock_init(&sscape->lock);
1118 spin_lock_init(&sscape->fwlock);
1119 sscape->io_res = io_res;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001120 sscape->wss_res = wss_res;
Krzysztof Helt453e37b2009-02-04 17:41:32 +01001121 sscape->io_base = port[dev];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Krzysztof Helt453e37b2009-02-04 17:41:32 +01001123 if (!detect_sscape(sscape, wss_port[dev])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 printk(KERN_ERR "sscape: hardware not detected at 0x%x\n", sscape->io_base);
1125 err = -ENODEV;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001126 goto _release_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
Krzysztof Heltf0968e32009-09-27 23:08:40 +02001129 switch (sscape->type) {
1130 case MEDIA_FX:
1131 name = "MediaFX/SoundFX";
1132 break;
1133 case SSCAPE:
1134 name = "Soundscape";
1135 break;
1136 case SSCAPE_PNP:
1137 name = "Soundscape PnP";
1138 break;
1139 case SSCAPE_VIVO:
1140 name = "Soundscape VIVO";
1141 break;
1142 default:
1143 name = "unknown Soundscape";
1144 break;
1145 }
1146
1147 printk(KERN_INFO "sscape: %s card detected at 0x%x, using IRQ %d, DMA %d\n",
1148 name, sscape->io_base, irq[dev], dma[dev]);
1149
1150 /*
1151 * Check that the user didn't pass us garbage data ...
1152 */
1153 irq_cfg = get_irq_config(sscape->type, irq[dev]);
1154 if (irq_cfg == INVALID_IRQ) {
1155 snd_printk(KERN_ERR "sscape: Invalid IRQ %d\n", irq[dev]);
1156 return -ENXIO;
1157 }
1158
1159 mpu_irq_cfg = get_irq_config(sscape->type, mpu_irq[dev]);
1160 if (mpu_irq_cfg == INVALID_IRQ) {
1161 printk(KERN_ERR "sscape: Invalid IRQ %d\n", mpu_irq[dev]);
1162 return -ENXIO;
1163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001165 if (sscape->type != SSCAPE_VIVO) {
1166 /*
1167 * Now create the hardware-specific device so that we can
1168 * load the microcode into the on-board processor.
1169 * We cannot use the MPU-401 MIDI system until this firmware
1170 * has been loaded into the card.
1171 */
1172 err = snd_hwdep_new(card, "MC68EC000", 0, &(sscape->hw));
1173 if (err < 0) {
1174 printk(KERN_ERR "sscape: Failed to create "
1175 "firmware device\n");
1176 goto _release_dma;
1177 }
1178 strlcpy(sscape->hw->name, "SoundScape M68K",
1179 sizeof(sscape->hw->name));
1180 sscape->hw->name[sizeof(sscape->hw->name) - 1] = '\0';
1181 sscape->hw->iface = SNDRV_HWDEP_IFACE_SSCAPE;
1182 sscape->hw->ops.open = sscape_hw_open;
1183 sscape->hw->ops.release = sscape_hw_release;
1184 sscape->hw->ops.ioctl = sscape_hw_ioctl;
1185 sscape->hw->private_data = sscape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /*
1189 * Tell the on-board devices where their resources are (I think -
1190 * I can't be sure without a datasheet ... So many magic values!)
1191 */
1192 spin_lock_irqsave(&sscape->lock, flags);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sscape_write_unsafe(sscape->io_base, GA_INTENA_REG, 0x00); /* disable */
1195 sscape_write_unsafe(sscape->io_base, GA_SMCFGA_REG, 0x2e);
1196 sscape_write_unsafe(sscape->io_base, GA_SMCFGB_REG, 0x00);
1197
1198 /*
1199 * Enable and configure the DMA channels ...
1200 */
1201 sscape_write_unsafe(sscape->io_base, GA_DMACFG_REG, 0x50);
Krzysztof Heltf0968e32009-09-27 23:08:40 +02001202 dma_cfg = (sscape->ic_type == IC_OPUS ? 0x40 : 0x70);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 sscape_write_unsafe(sscape->io_base, GA_DMAA_REG, dma_cfg);
1204 sscape_write_unsafe(sscape->io_base, GA_DMAB_REG, 0x20);
1205
Krzysztof Heltf0968e32009-09-27 23:08:40 +02001206 mpu_irq_cfg |= mpu_irq_cfg << 2;
1207 sscape_write_unsafe(sscape->io_base, GA_INTCFG_REG, 0xf0 | mpu_irq_cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 sscape_write_unsafe(sscape->io_base,
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001209 GA_CDCFG_REG, 0x09 | DMA_8BIT
1210 | (dma[dev] << 4) | (irq_cfg << 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 spin_unlock_irqrestore(&sscape->lock, flags);
1213
1214 /*
1215 * We have now enabled the codec chip, and so we should
1216 * detect the AD1845 device ...
1217 */
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001218 err = create_ad1845(card, wss_port[dev], irq[dev],
1219 dma[dev], dma2[dev]);
1220 if (err < 0) {
1221 printk(KERN_ERR "sscape: No AD1845 device at 0x%lx, IRQ %d\n",
1222 wss_port[dev], irq[dev]);
1223 goto _release_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225#define MIDI_DEVNUM 0
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001226 if (sscape->type != SSCAPE_VIVO) {
Krzysztof Helt453e37b2009-02-04 17:41:32 +01001227 err = create_mpu401(card, MIDI_DEVNUM, port[dev], mpu_irq[dev]);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001228 if (err < 0) {
1229 printk(KERN_ERR "sscape: Failed to create "
Krzysztof Helt453e37b2009-02-04 17:41:32 +01001230 "MPU-401 device at 0x%lx\n",
1231 port[dev]);
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001232 goto _release_dma;
1233 }
1234
1235 /*
1236 * Enable the master IRQ ...
1237 */
1238 sscape_write(sscape, GA_INTENA_REG, 0x80);
1239
1240 /*
1241 * Initialize mixer
1242 */
1243 sscape->midi_vol = 0;
1244 host_write_ctrl_unsafe(sscape->io_base, CMD_SET_MIDI_VOL, 100);
1245 host_write_ctrl_unsafe(sscape->io_base, 0, 100);
1246 host_write_ctrl_unsafe(sscape->io_base, CMD_XXX_MIDI_VOL, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
1249 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 * Now that we have successfully created this sound card,
1251 * it is safe to store the pointer.
1252 * NOTE: we only register the sound card's "destructor"
1253 * function now that our "constructor" has completed.
1254 */
1255 card->private_free = soundscape_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 return 0;
1258
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001259_release_dma:
Takashi Iwai277e9262005-11-17 17:13:12 +01001260 free_dma(dma[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001262_release_region:
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001263 release_and_free_resource(wss_res);
Takashi Iwaib1d57762005-10-10 11:56:31 +02001264 release_and_free_resource(io_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 return err;
1267}
1268
1269
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001270static int __devinit snd_sscape_match(struct device *pdev, unsigned int i)
Takashi Iwai277e9262005-11-17 17:13:12 +01001271{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001272 /*
1273 * Make sure we were given ALL of the other parameters.
1274 */
1275 if (port[i] == SNDRV_AUTO_PORT)
1276 return 0;
1277
1278 if (irq[i] == SNDRV_AUTO_IRQ ||
1279 mpu_irq[i] == SNDRV_AUTO_IRQ ||
1280 dma[i] == SNDRV_AUTO_DMA) {
1281 printk(KERN_INFO
1282 "sscape: insufficient parameters, need IO, IRQ, MPU-IRQ and DMA\n");
1283 return 0;
1284 }
1285
1286 return 1;
1287}
1288
1289static int __devinit snd_sscape_probe(struct device *pdev, unsigned int dev)
1290{
Takashi Iwai277e9262005-11-17 17:13:12 +01001291 struct snd_card *card;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001292 struct soundscape *sscape;
Takashi Iwai277e9262005-11-17 17:13:12 +01001293 int ret;
1294
Takashi Iwaic95eadd2008-12-28 16:43:35 +01001295 ret = snd_card_create(index[dev], id[dev], THIS_MODULE,
1296 sizeof(struct soundscape), &card);
1297 if (ret < 0)
1298 return ret;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001299
1300 sscape = get_card_soundscape(card);
1301 sscape->type = SSCAPE;
1302
Takashi Iwai277e9262005-11-17 17:13:12 +01001303 dma[dev] &= 0x03;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001304 ret = create_sscape(dev, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001305 if (ret < 0)
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001306 goto _release_card;
1307
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001308 snd_card_set_dev(card, pdev);
Takashi Iwai277e9262005-11-17 17:13:12 +01001309 if ((ret = snd_card_register(card)) < 0) {
1310 printk(KERN_ERR "sscape: Failed to register sound card\n");
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001311 goto _release_card;
Takashi Iwai277e9262005-11-17 17:13:12 +01001312 }
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001313 dev_set_drvdata(pdev, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001314 return 0;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001315
1316_release_card:
1317 snd_card_free(card);
1318 return ret;
Takashi Iwai277e9262005-11-17 17:13:12 +01001319}
1320
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001321static int __devexit snd_sscape_remove(struct device *devptr, unsigned int dev)
Takashi Iwai277e9262005-11-17 17:13:12 +01001322{
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001323 snd_card_free(dev_get_drvdata(devptr));
1324 dev_set_drvdata(devptr, NULL);
Takashi Iwai277e9262005-11-17 17:13:12 +01001325 return 0;
1326}
1327
Rene Herman83c51c02007-03-20 11:33:46 +01001328#define DEV_NAME "sscape"
Takashi Iwai277e9262005-11-17 17:13:12 +01001329
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001330static struct isa_driver snd_sscape_driver = {
1331 .match = snd_sscape_match,
Takashi Iwai277e9262005-11-17 17:13:12 +01001332 .probe = snd_sscape_probe,
1333 .remove = __devexit_p(snd_sscape_remove),
1334 /* FIXME: suspend/resume */
1335 .driver = {
Rene Herman83c51c02007-03-20 11:33:46 +01001336 .name = DEV_NAME
Takashi Iwai277e9262005-11-17 17:13:12 +01001337 },
1338};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340#ifdef CONFIG_PNP
1341static inline int __devinit get_next_autoindex(int i)
1342{
Takashi Iwai277e9262005-11-17 17:13:12 +01001343 while (i < SNDRV_CARDS && port[i] != SNDRV_AUTO_PORT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 ++i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return i;
1346}
1347
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349static int __devinit sscape_pnp_detect(struct pnp_card_link *pcard,
1350 const struct pnp_card_device_id *pid)
1351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 static int idx = 0;
Takashi Iwai277e9262005-11-17 17:13:12 +01001353 struct pnp_dev *dev;
1354 struct snd_card *card;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001355 struct soundscape *sscape;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int ret;
1357
1358 /*
1359 * Allow this function to fail *quietly* if all the ISA PnP
1360 * devices were configured using module parameters instead.
1361 */
Takashi Iwai277e9262005-11-17 17:13:12 +01001362 if ((idx = get_next_autoindex(idx)) >= SNDRV_CARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
1366 * We have found a candidate ISA PnP card. Now we
1367 * have to check that it has the devices that we
1368 * expect it to have.
1369 *
1370 * We will NOT try and autoconfigure all of the resources
1371 * needed and then activate the card as we are assuming that
1372 * has already been done at boot-time using /proc/isapnp.
1373 * We shall simply try to give each active card the resources
1374 * that it wants. This is a sensible strategy for a modular
1375 * system where unused modules are unloaded regularly.
1376 *
1377 * This strategy is utterly useless if we compile the driver
1378 * into the kernel, of course.
1379 */
1380 // printk(KERN_INFO "sscape: %s\n", card->name);
1381
1382 /*
1383 * Check that we still have room for another sound card ...
1384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 dev = pnp_request_card_device(pcard, pid->devs[0].id, NULL);
Takashi Iwai277e9262005-11-17 17:13:12 +01001386 if (! dev)
1387 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Takashi Iwai277e9262005-11-17 17:13:12 +01001389 if (!pnp_is_active(dev)) {
1390 if (pnp_activate_dev(dev) < 0) {
1391 printk(KERN_INFO "sscape: device is inactive\n");
1392 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 }
1395
Takashi Iwai277e9262005-11-17 17:13:12 +01001396 /*
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001397 * Create a new ALSA sound card entry, in anticipation
1398 * of detecting our hardware ...
1399 */
Takashi Iwaic95eadd2008-12-28 16:43:35 +01001400 ret = snd_card_create(index[idx], id[idx], THIS_MODULE,
1401 sizeof(struct soundscape), &card);
1402 if (ret < 0)
1403 return ret;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001404
1405 sscape = get_card_soundscape(card);
1406
1407 /*
1408 * Identify card model ...
1409 */
1410 if (!strncmp("ENS4081", pid->id, 7))
1411 sscape->type = SSCAPE_VIVO;
1412 else
1413 sscape->type = SSCAPE_PNP;
1414
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001415 /*
Takashi Iwai277e9262005-11-17 17:13:12 +01001416 * Read the correct parameters off the ISA PnP bus ...
1417 */
1418 port[idx] = pnp_port_start(dev, 0);
1419 irq[idx] = pnp_irq(dev, 0);
1420 mpu_irq[idx] = pnp_irq(dev, 1);
1421 dma[idx] = pnp_dma(dev, 0) & 0x03;
Krzysztof Heltec1e7942007-09-17 17:57:37 +02001422 if (sscape->type == SSCAPE_PNP) {
1423 dma2[idx] = dma[idx];
1424 wss_port[idx] = CODEC_IO(port[idx]);
1425 } else {
1426 wss_port[idx] = pnp_port_start(dev, 1);
1427 dma2[idx] = pnp_dma(dev, 1);
1428 }
Takashi Iwai277e9262005-11-17 17:13:12 +01001429
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001430 ret = create_sscape(idx, card);
Takashi Iwai277e9262005-11-17 17:13:12 +01001431 if (ret < 0)
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001432 goto _release_card;
1433
Takashi Iwai277e9262005-11-17 17:13:12 +01001434 snd_card_set_dev(card, &pcard->card->dev);
1435 if ((ret = snd_card_register(card)) < 0) {
1436 printk(KERN_ERR "sscape: Failed to register sound card\n");
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001437 goto _release_card;
Takashi Iwai277e9262005-11-17 17:13:12 +01001438 }
1439
1440 pnp_set_card_drvdata(pcard, card);
1441 ++idx;
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001442 return 0;
Takashi Iwai277e9262005-11-17 17:13:12 +01001443
Krzysztof Helt4996bca2007-09-17 16:23:13 +02001444_release_card:
1445 snd_card_free(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return ret;
1447}
1448
1449static void __devexit sscape_pnp_remove(struct pnp_card_link * pcard)
1450{
Takashi Iwai277e9262005-11-17 17:13:12 +01001451 snd_card_free(pnp_get_card_drvdata(pcard));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455static struct pnp_card_driver sscape_pnpc_driver = {
1456 .flags = PNP_DRIVER_RES_DO_NOT_CHANGE,
1457 .name = "sscape",
1458 .id_table = sscape_pnpids,
1459 .probe = sscape_pnp_detect,
1460 .remove = __devexit_p(sscape_pnp_remove),
1461};
1462
1463#endif /* CONFIG_PNP */
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465static int __init sscape_init(void)
1466{
Rene Herman609d7692007-05-15 11:42:56 +02001467 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Rene Herman609d7692007-05-15 11:42:56 +02001469 err = isa_register_driver(&snd_sscape_driver, SNDRV_CARDS);
Takashi Iwai59b1b342006-01-04 15:06:44 +01001470#ifdef CONFIG_PNP
Rene Herman609d7692007-05-15 11:42:56 +02001471 if (!err)
1472 isa_registered = 1;
1473
1474 err = pnp_register_card_driver(&sscape_pnpc_driver);
1475 if (!err)
Clemens Ladischf7a92752005-12-07 09:13:42 +01001476 pnp_registered = 1;
Rene Herman609d7692007-05-15 11:42:56 +02001477
1478 if (isa_registered)
1479 err = 0;
Takashi Iwai59b1b342006-01-04 15:06:44 +01001480#endif
Rene Herman609d7692007-05-15 11:42:56 +02001481 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001484static void __exit sscape_exit(void)
1485{
1486#ifdef CONFIG_PNP
1487 if (pnp_registered)
1488 pnp_unregister_card_driver(&sscape_pnpc_driver);
Rene Herman609d7692007-05-15 11:42:56 +02001489 if (isa_registered)
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001490#endif
Rene Herman609d7692007-05-15 11:42:56 +02001491 isa_unregister_driver(&snd_sscape_driver);
Takashi Iwai5e24c1c2007-02-22 12:50:54 +01001492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494module_init(sscape_init);
1495module_exit(sscape_exit);