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
14 * Alan Cox General clean up, use kernel IRQ
16 * Christoph Hellwig Adapted to module_init/module_exit
17 * Bartlomiej Zolnierkiewicz
18 * Added __init to download_code()
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/module.h>
29 #include <linux/init.h>
31 #define USE_SEQ_MACROS
32 #define USE_SIMPLE_MACROS
34 #include "sound_config.h"
35 #include "sound_firmware.h"
39 static int maui_base = 0x330;
41 static volatile int irq_ok;
44 #define HOST_DATA_PORT (maui_base + 2)
45 #define HOST_STAT_PORT (maui_base + 3)
46 #define HOST_CTRL_PORT (maui_base + 3)
48 #define STAT_TX_INTR 0x40
49 #define STAT_TX_AVAIL 0x20
50 #define STAT_TX_IENA 0x10
51 #define STAT_RX_INTR 0x04
52 #define STAT_RX_AVAIL 0x02
53 #define STAT_RX_IENA 0x01
55 static int (*orig_load_patch)(int dev, int format, const char __user *addr,
56 int offs, int count, int pmgr_flag) = NULL;
58 #include "maui_boot.h"
60 static int maui_wait(int mask)
65 * Perform a short initial wait without sleeping
68 for (i = 0; i < 100; i++)
69 if (inb(HOST_STAT_PORT) & mask)
73 * Wait up to 15 seconds with sleeping
76 for (i = 0; i < 150; i++) {
77 if (inb(HOST_STAT_PORT) & mask)
79 current->state = TASK_INTERRUPTIBLE;
80 schedule_timeout(HZ / 10);
81 if (signal_pending(current))
87 static int maui_read(void)
89 if (maui_wait(STAT_RX_AVAIL))
90 return inb(HOST_DATA_PORT);
94 static int maui_write(unsigned char data)
96 if (maui_wait(STAT_TX_AVAIL)) {
97 outb((data), HOST_DATA_PORT);
100 printk(KERN_WARNING "Maui: Write timeout\n");
104 static irqreturn_t mauiintr(int irq, void *dev_id, struct pt_regs *dummy)
110 static int __init download_code(void)
113 int eol_seen = 0, done = 0;
116 printk(KERN_INFO "Code download (%d bytes): ", maui_osLen);
118 for (i = 0; i < maui_osLen; i++) {
119 if (maui_os[i] != '\r') {
120 if (!skip || (maui_os[i] == 'S' && (i == 0 || maui_os[i - 1] == '\n'))) {
123 if (maui_os[i] == '\n')
125 else if (maui_os[i] == 'S') {
126 if (maui_os[i + 1] == '8')
128 if (!maui_write(0xF1))
130 if (!maui_write('S'))
133 if (!maui_write(maui_os[i]))
143 for (n = 0; n < 2; n++) {
144 if (maui_wait(STAT_RX_AVAIL)) {
145 c = inb(HOST_DATA_PORT);
150 printk("Download not acknowledged\n");
153 else if (!(lines++ % 10))
158 printk(KERN_INFO "Download complete\n");
168 printk(KERN_ERR "Download failed!!!\n");
172 static int __init maui_init(int irq)
191 printk(KERN_ERR "Maui: Invalid IRQ %d\n", irq);
194 outb((0x00), HOST_CTRL_PORT); /* Reset */
195 outb((bits), HOST_DATA_PORT); /* Set the IRQ bits */
196 outb((bits | 0x80), HOST_DATA_PORT); /* Set the IRQ bits again? */
197 outb((0x80), HOST_CTRL_PORT); /* Leave reset */
198 outb((0x80), HOST_CTRL_PORT); /* Leave reset */
199 outb((0xD0), HOST_CTRL_PORT); /* Cause interrupt */
204 for (i = 0; i < 1000000 && !irq_ok; i++)
210 outb((0x80), HOST_CTRL_PORT); /* Leave reset */
212 printk(KERN_INFO "Turtle Beach Maui initialization\n");
214 if (!download_code())
217 outb((0xE0), HOST_CTRL_PORT); /* Normal operation */
219 /* Select mpu401 mode */
223 if (maui_read() != 0x80) {
226 if (maui_read() != 0x80)
227 printk(KERN_ERR "Maui didn't acknowledge set HW mode command\n");
229 printk(KERN_INFO "Maui initialized OK\n");
233 static int maui_short_wait(int mask) {
236 for (i = 0; i < 1000; i++) {
237 if (inb(HOST_STAT_PORT) & mask) {
244 static int maui_load_patch(int dev, int format, const char __user *addr,
245 int offs, int count, int pmgr_flag)
248 struct sysex_info header;
249 unsigned long left, src_offs;
250 int hdr_size = (unsigned long) &header.data[0] - (unsigned long) &header;
253 if (format == SYSEX_PATCH) /* Handled by midi_synth.c */
254 return orig_load_patch(dev, format, addr, offs, count, pmgr_flag);
256 if (format != MAUI_PATCH)
258 printk(KERN_WARNING "Maui: Unknown patch format\n");
260 if (count < hdr_size) {
261 /* printk("Maui error: Patch header too short\n");*/
267 * Copy the header from user space but ignore the first bytes which have
268 * been transferred already.
271 if(copy_from_user(&((char *) &header)[offs], &(addr)[offs], hdr_size - offs))
274 if (count < header.len) {
275 printk(KERN_ERR "Maui warning: Host command record too short (%d<%d)\n", count, (int) header.len);
281 for (i = 0; i < left; i++) {
284 if(get_user(*(unsigned char *) &data, (unsigned char __user *) &((addr)[hdr_size + i])))
286 if (i == 0 && !(data & 0x80))
289 if (maui_write(data) == -1)
293 if ((i = maui_read()) != 0x80) {
295 printk("Maui: Error status %02x\n", i);
301 static int __init probe_maui(struct address_info *hw_config)
303 struct resource *ports;
308 ports = request_region(hw_config->io_base, 2, "mpu401");
312 if (!request_region(hw_config->io_base + 2, 6, "Maui"))
315 maui_base = hw_config->io_base;
316 maui_osp = hw_config->osp;
318 if (request_irq(hw_config->irq, mauiintr, 0, "Maui", NULL) < 0)
322 * Initialize the processor if necessary
325 if (maui_osLen > 0) {
326 if (!(inb(HOST_STAT_PORT) & STAT_TX_AVAIL) ||
327 !maui_write(0x9F) || /* Report firmware version */
328 !maui_short_wait(STAT_RX_AVAIL) ||
329 maui_read() == -1 || maui_read() == -1)
330 if (!maui_init(hw_config->irq))
333 if (!maui_write(0xCF)) /* Report hardware version */ {
334 printk(KERN_ERR "No WaveFront firmware detected (card uninitialized?)\n");
337 if ((tmp1 = maui_read()) == -1 || (tmp2 = maui_read()) == -1) {
338 printk(KERN_ERR "No WaveFront firmware detected (card uninitialized?)\n");
341 if (tmp1 == 0xff || tmp2 == 0xff)
343 printk(KERN_DEBUG "WaveFront hardware version %d.%d\n", tmp1, tmp2);
345 if (!maui_write(0x9F)) /* Report firmware version */
347 if ((tmp1 = maui_read()) == -1 || (tmp2 = maui_read()) == -1)
350 printk(KERN_DEBUG "WaveFront firmware version %d.%d\n", tmp1, tmp2);
352 if (!maui_write(0x85)) /* Report free DRAM */
355 for (i = 0; i < 4; i++) {
356 tmp1 |= maui_read() << (7 * i);
358 printk(KERN_DEBUG "Available DRAM %dk\n", tmp1 / 1024);
360 for (i = 0; i < 1000; i++)
361 if (probe_mpu401(hw_config, ports))
364 ret = probe_mpu401(hw_config, ports);
368 conf_printf("Maui", hw_config);
370 hw_config->irq *= -1;
371 hw_config->name = "Maui";
372 attach_mpu401(hw_config, THIS_MODULE);
374 if (hw_config->slots[1] != -1) /* The MPU401 driver installed itself */ {
375 struct synth_operations *synth;
377 this_dev = hw_config->slots[1];
380 * Intercept patch loading calls so that they can be handled
381 * by the Maui driver.
384 synth = midi_devs[this_dev]->converter;
387 orig_load_patch = synth->load_patch;
388 synth->load_patch = &maui_load_patch;
390 printk(KERN_ERR "Maui: Can't install patch loader\n");
395 free_irq(hw_config->irq, NULL);
397 release_region(hw_config->io_base + 2, 6);
399 release_region(hw_config->io_base, 2);
403 static void __exit unload_maui(struct address_info *hw_config)
405 int irq = hw_config->irq;
406 release_region(hw_config->io_base + 2, 6);
407 unload_mpu401(hw_config);
417 static struct address_info cfg;
419 static int __initdata io = -1;
420 static int __initdata irq = -1;
422 module_param(io, int, 0);
423 module_param(irq, int, 0);
426 * Install a Maui card. Needs mpu401 loaded already.
429 static int __init init_maui(void)
431 printk(KERN_INFO "Turtle beach Maui and Tropez driver, Copyright (C) by Hannu Savolainen 1993-1996\n");
436 if (cfg.io_base == -1 || cfg.irq == -1) {
437 printk(KERN_INFO "maui: irq and io must be set.\n");
441 if (maui_os == NULL) {
443 maui_osLen = mod_firmware_load("/etc/sound/oswf.mot", (char **) &maui_os);
445 if (probe_maui(&cfg) == 0)
451 static void __exit cleanup_maui(void)
453 if (fw_load && maui_os)
458 module_init(init_maui);
459 module_exit(cleanup_maui);
462 static int __init setup_maui(char *str)
467 str = get_options(str, ARRAY_SIZE(ints), ints);
475 __setup("maui=", setup_maui);
477 MODULE_LICENSE("GPL");