[PATCH] Move valid_dma_direction() from x86_64 to generic code
[usb.git] / sound / oss / maui.c
blob05cf194eda6b1168d3f6b0b475788fd8aabbb286
1 /*
2 * sound/maui.c
4 * The low level driver for Turtle Beach Maui and Tropez.
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
11 * for more info.
13 * Changes:
14 * Alan Cox General clean up, use kernel IRQ
15 * system
16 * Christoph Hellwig Adapted to module_init/module_exit
17 * Bartlomiej Zolnierkiewicz
18 * Added __init to download_code()
20 * Status:
21 * Andrew J. Kroll Tested 06/01/1999 with:
22 * * OSWF.MOT File Version: 1.15
23 * * OSWF.MOT File Dated: 09/12/94
24 * * Older versions will cause problems.
27 #include <linux/interrupt.h>
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
32 #define USE_SEQ_MACROS
33 #define USE_SIMPLE_MACROS
35 #include "sound_config.h"
36 #include "sound_firmware.h"
38 #include "mpu401.h"
40 static int maui_base = 0x330;
42 static volatile int irq_ok;
43 static int *maui_osp;
45 #define HOST_DATA_PORT (maui_base + 2)
46 #define HOST_STAT_PORT (maui_base + 3)
47 #define HOST_CTRL_PORT (maui_base + 3)
49 #define STAT_TX_INTR 0x40
50 #define STAT_TX_AVAIL 0x20
51 #define STAT_TX_IENA 0x10
52 #define STAT_RX_INTR 0x04
53 #define STAT_RX_AVAIL 0x02
54 #define STAT_RX_IENA 0x01
56 static int (*orig_load_patch)(int dev, int format, const char __user *addr,
57 int offs, int count, int pmgr_flag) = NULL;
59 #include "maui_boot.h"
61 static int maui_wait(int mask)
63 int i;
66 * Perform a short initial wait without sleeping
69 for (i = 0; i < 100; i++)
70 if (inb(HOST_STAT_PORT) & mask)
71 return 1;
74 * Wait up to 15 seconds with sleeping
77 for (i = 0; i < 150; i++) {
78 if (inb(HOST_STAT_PORT) & mask)
79 return 1;
80 current->state = TASK_INTERRUPTIBLE;
81 schedule_timeout(HZ / 10);
82 if (signal_pending(current))
83 return 0;
85 return 0;
88 static int maui_read(void)
90 if (maui_wait(STAT_RX_AVAIL))
91 return inb(HOST_DATA_PORT);
92 return -1;
95 static int maui_write(unsigned char data)
97 if (maui_wait(STAT_TX_AVAIL)) {
98 outb((data), HOST_DATA_PORT);
99 return 1;
101 printk(KERN_WARNING "Maui: Write timeout\n");
102 return 0;
105 static irqreturn_t mauiintr(int irq, void *dev_id, struct pt_regs *dummy)
107 irq_ok = 1;
108 return IRQ_HANDLED;
111 static int __init download_code(void)
113 int i, lines = 0;
114 int eol_seen = 0, done = 0;
115 int skip = 1;
117 printk(KERN_INFO "Code download (%d bytes): ", maui_osLen);
119 for (i = 0; i < maui_osLen; i++) {
120 if (maui_os[i] != '\r') {
121 if (!skip || (maui_os[i] == 'S' && (i == 0 || maui_os[i - 1] == '\n'))) {
122 skip = 0;
124 if (maui_os[i] == '\n')
125 eol_seen = skip = 1;
126 else if (maui_os[i] == 'S') {
127 if (maui_os[i + 1] == '8')
128 done = 1;
129 if (!maui_write(0xF1))
130 goto failure;
131 if (!maui_write('S'))
132 goto failure;
133 } else {
134 if (!maui_write(maui_os[i]))
135 goto failure;
138 if (eol_seen) {
139 int c = 0;
140 int n;
142 eol_seen = 0;
144 for (n = 0; n < 2; n++) {
145 if (maui_wait(STAT_RX_AVAIL)) {
146 c = inb(HOST_DATA_PORT);
147 break;
150 if (c != 0x80) {
151 printk("Download not acknowledged\n");
152 return 0;
154 else if (!(lines++ % 10))
155 printk(".");
157 if (done) {
158 printk("\n");
159 printk(KERN_INFO "Download complete\n");
160 return 1;
167 failure:
168 printk("\n");
169 printk(KERN_ERR "Download failed!!!\n");
170 return 0;
173 static int __init maui_init(int irq)
175 unsigned char bits;
177 switch (irq) {
178 case 9:
179 bits = 0x00;
180 break;
181 case 5:
182 bits = 0x08;
183 break;
184 case 12:
185 bits = 0x10;
186 break;
187 case 15:
188 bits = 0x18;
189 break;
191 default:
192 printk(KERN_ERR "Maui: Invalid IRQ %d\n", irq);
193 return 0;
195 outb((0x00), HOST_CTRL_PORT); /* Reset */
196 outb((bits), HOST_DATA_PORT); /* Set the IRQ bits */
197 outb((bits | 0x80), HOST_DATA_PORT); /* Set the IRQ bits again? */
198 outb((0x80), HOST_CTRL_PORT); /* Leave reset */
199 outb((0x80), HOST_CTRL_PORT); /* Leave reset */
200 outb((0xD0), HOST_CTRL_PORT); /* Cause interrupt */
202 #ifdef CONFIG_SMP
204 int i;
205 for (i = 0; i < 1000000 && !irq_ok; i++)
207 if (!irq_ok)
208 return 0;
210 #endif
211 outb((0x80), HOST_CTRL_PORT); /* Leave reset */
213 printk(KERN_INFO "Turtle Beach Maui initialization\n");
215 if (!download_code())
216 return 0;
218 outb((0xE0), HOST_CTRL_PORT); /* Normal operation */
220 /* Select mpu401 mode */
222 maui_write(0xf0);
223 maui_write(1);
224 if (maui_read() != 0x80) {
225 maui_write(0xf0);
226 maui_write(1);
227 if (maui_read() != 0x80)
228 printk(KERN_ERR "Maui didn't acknowledge set HW mode command\n");
230 printk(KERN_INFO "Maui initialized OK\n");
231 return 1;
234 static int maui_short_wait(int mask) {
235 int i;
237 for (i = 0; i < 1000; i++) {
238 if (inb(HOST_STAT_PORT) & mask) {
239 return 1;
242 return 0;
245 static int maui_load_patch(int dev, int format, const char __user *addr,
246 int offs, int count, int pmgr_flag)
249 struct sysex_info header;
250 unsigned long left, src_offs;
251 int hdr_size = (unsigned long) &header.data[0] - (unsigned long) &header;
252 int i;
254 if (format == SYSEX_PATCH) /* Handled by midi_synth.c */
255 return orig_load_patch(dev, format, addr, offs, count, pmgr_flag);
257 if (format != MAUI_PATCH)
259 printk(KERN_WARNING "Maui: Unknown patch format\n");
261 if (count < hdr_size) {
262 /* printk("Maui error: Patch header too short\n");*/
263 return -EINVAL;
265 count -= hdr_size;
268 * Copy the header from user space but ignore the first bytes which have
269 * been transferred already.
272 if(copy_from_user(&((char *) &header)[offs], &(addr)[offs], hdr_size - offs))
273 return -EFAULT;
275 if (count < header.len) {
276 printk(KERN_ERR "Maui warning: Host command record too short (%d<%d)\n", count, (int) header.len);
277 header.len = count;
279 left = header.len;
280 src_offs = 0;
282 for (i = 0; i < left; i++) {
283 unsigned char data;
285 if(get_user(*(unsigned char *) &data, (unsigned char __user *) &((addr)[hdr_size + i])))
286 return -EFAULT;
287 if (i == 0 && !(data & 0x80))
288 return -EINVAL;
290 if (maui_write(data) == -1)
291 return -EIO;
294 if ((i = maui_read()) != 0x80) {
295 if (i != -1)
296 printk("Maui: Error status %02x\n", i);
297 return -EIO;
299 return 0;
302 static int __init probe_maui(struct address_info *hw_config)
304 struct resource *ports;
305 int this_dev;
306 int i;
307 int tmp1, tmp2, ret;
309 ports = request_region(hw_config->io_base, 2, "mpu401");
310 if (!ports)
311 return 0;
313 if (!request_region(hw_config->io_base + 2, 6, "Maui"))
314 goto out;
316 maui_base = hw_config->io_base;
317 maui_osp = hw_config->osp;
319 if (request_irq(hw_config->irq, mauiintr, 0, "Maui", NULL) < 0)
320 goto out2;
323 * Initialize the processor if necessary
326 if (maui_osLen > 0) {
327 if (!(inb(HOST_STAT_PORT) & STAT_TX_AVAIL) ||
328 !maui_write(0x9F) || /* Report firmware version */
329 !maui_short_wait(STAT_RX_AVAIL) ||
330 maui_read() == -1 || maui_read() == -1)
331 if (!maui_init(hw_config->irq))
332 goto out3;
334 if (!maui_write(0xCF)) /* Report hardware version */ {
335 printk(KERN_ERR "No WaveFront firmware detected (card uninitialized?)\n");
336 goto out3;
338 if ((tmp1 = maui_read()) == -1 || (tmp2 = maui_read()) == -1) {
339 printk(KERN_ERR "No WaveFront firmware detected (card uninitialized?)\n");
340 goto out3;
342 if (tmp1 == 0xff || tmp2 == 0xff)
343 goto out3;
344 printk(KERN_DEBUG "WaveFront hardware version %d.%d\n", tmp1, tmp2);
346 if (!maui_write(0x9F)) /* Report firmware version */
347 goto out3;
348 if ((tmp1 = maui_read()) == -1 || (tmp2 = maui_read()) == -1)
349 goto out3;
351 printk(KERN_DEBUG "WaveFront firmware version %d.%d\n", tmp1, tmp2);
353 if (!maui_write(0x85)) /* Report free DRAM */
354 goto out3;
355 tmp1 = 0;
356 for (i = 0; i < 4; i++) {
357 tmp1 |= maui_read() << (7 * i);
359 printk(KERN_DEBUG "Available DRAM %dk\n", tmp1 / 1024);
361 for (i = 0; i < 1000; i++)
362 if (probe_mpu401(hw_config, ports))
363 break;
365 ret = probe_mpu401(hw_config, ports);
366 if (!ret)
367 goto out3;
369 conf_printf("Maui", hw_config);
371 hw_config->irq *= -1;
372 hw_config->name = "Maui";
373 attach_mpu401(hw_config, THIS_MODULE);
375 if (hw_config->slots[1] != -1) /* The MPU401 driver installed itself */ {
376 struct synth_operations *synth;
378 this_dev = hw_config->slots[1];
381 * Intercept patch loading calls so that they can be handled
382 * by the Maui driver.
385 synth = midi_devs[this_dev]->converter;
386 if (synth != NULL) {
387 synth->id = "MAUI";
388 orig_load_patch = synth->load_patch;
389 synth->load_patch = &maui_load_patch;
390 } else
391 printk(KERN_ERR "Maui: Can't install patch loader\n");
393 return 1;
395 out3:
396 free_irq(hw_config->irq, NULL);
397 out2:
398 release_region(hw_config->io_base + 2, 6);
399 out:
400 release_region(hw_config->io_base, 2);
401 return 0;
404 static void __exit unload_maui(struct address_info *hw_config)
406 int irq = hw_config->irq;
407 release_region(hw_config->io_base + 2, 6);
408 unload_mpu401(hw_config);
410 if (irq < 0)
411 irq = -irq;
412 if (irq > 0)
413 free_irq(irq, NULL);
416 static int fw_load;
418 static struct address_info cfg;
420 static int __initdata io = -1;
421 static int __initdata irq = -1;
423 module_param(io, int, 0);
424 module_param(irq, int, 0);
427 * Install a Maui card. Needs mpu401 loaded already.
430 static int __init init_maui(void)
432 printk(KERN_INFO "Turtle beach Maui and Tropez driver, Copyright (C) by Hannu Savolainen 1993-1996\n");
434 cfg.io_base = io;
435 cfg.irq = irq;
437 if (cfg.io_base == -1 || cfg.irq == -1) {
438 printk(KERN_INFO "maui: irq and io must be set.\n");
439 return -EINVAL;
442 if (maui_os == NULL) {
443 fw_load = 1;
444 maui_osLen = mod_firmware_load("/etc/sound/oswf.mot", (char **) &maui_os);
446 if (probe_maui(&cfg) == 0)
447 return -ENODEV;
449 return 0;
452 static void __exit cleanup_maui(void)
454 if (fw_load && maui_os)
455 vfree(maui_os);
456 unload_maui(&cfg);
459 module_init(init_maui);
460 module_exit(cleanup_maui);
462 #ifndef MODULE
463 static int __init setup_maui(char *str)
465 /* io, irq */
466 int ints[3];
468 str = get_options(str, ARRAY_SIZE(ints), ints);
470 io = ints[1];
471 irq = ints[2];
473 return 1;
476 __setup("maui=", setup_maui);
477 #endif
478 MODULE_LICENSE("GPL");