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