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