1 /*********************************************************************
5 * Turtle Beach MultiSound Sound Card Driver for Linux
7 * Copyright (C) 1998 Andrew Veliath
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * $Id: msnd.c,v 1.17 1999/03/21 16:50:09 andrewtv Exp $
25 ********************************************************************/
27 #include <linux/version.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
32 #include <linux/types.h>
33 #include <linux/delay.h>
35 #include <linux/init.h>
37 #include <asm/uaccess.h>
38 #include <linux/spinlock.h>
42 #define LOGNAME "msnd"
44 #define MSND_MAX_DEVS 4
46 static multisound_dev_t *devs[MSND_MAX_DEVS];
49 int __init msnd_register(multisound_dev_t *dev)
53 for (i = 0; i < MSND_MAX_DEVS; ++i)
57 if (i == MSND_MAX_DEVS)
65 void msnd_unregister(multisound_dev_t *dev)
69 for (i = 0; i < MSND_MAX_DEVS; ++i)
73 if (i == MSND_MAX_DEVS) {
74 printk(KERN_WARNING LOGNAME ": Unregistering unknown device\n");
82 int msnd_get_num_devs(void)
87 multisound_dev_t *msnd_get_dev(int j)
91 for (i = 0; i < MSND_MAX_DEVS && j; ++i)
95 if (i == MSND_MAX_DEVS || j != 0)
101 void msnd_init_queue(unsigned long base, int start, int size)
103 isa_writew(PCTODSP_BASED(start), base + JQS_wStart);
104 isa_writew(PCTODSP_OFFSET(size) - 1, base + JQS_wSize);
105 isa_writew(0, base + JQS_wHead);
106 isa_writew(0, base + JQS_wTail);
109 void msnd_fifo_init(msnd_fifo *f)
114 void msnd_fifo_free(msnd_fifo *f)
122 int msnd_fifo_alloc(msnd_fifo *f, size_t n)
125 f->data = (char *)vmalloc(n);
137 void msnd_fifo_make_empty(msnd_fifo *f)
139 f->len = f->tail = f->head = 0;
142 int msnd_fifo_write(msnd_fifo *f, const char *buf, size_t len)
146 while ((count < len) && (f->len != f->n)) {
150 if (f->head <= f->tail) {
151 nwritten = len - count;
152 if (nwritten > f->n - f->tail)
153 nwritten = f->n - f->tail;
156 nwritten = f->head - f->tail;
157 if (nwritten > len - count)
158 nwritten = len - count;
161 isa_memcpy_fromio(f->data + f->tail, (unsigned long) buf, nwritten);
173 int msnd_fifo_read(msnd_fifo *f, char *buf, size_t len)
177 while ((count < len) && (f->len > 0)) {
181 if (f->tail <= f->head) {
183 if (nread > f->n - f->head)
184 nread = f->n - f->head;
187 nread = f->tail - f->head;
188 if (nread > len - count)
192 isa_memcpy_toio((unsigned long) buf, f->data + f->head, nread);
204 int msnd_wait_TXDE(multisound_dev_t *dev)
206 register unsigned int io = dev->io;
207 register int timeout = 1000;
210 if (inb(io + HP_ISR) & HPISR_TXDE)
216 int msnd_wait_HC0(multisound_dev_t *dev)
218 register unsigned int io = dev->io;
219 register int timeout = 1000;
222 if (!(inb(io + HP_CVR) & HPCVR_HC))
228 int msnd_send_dsp_cmd(multisound_dev_t *dev, BYTE cmd)
232 spin_lock_irqsave(&dev->lock, flags);
233 if (msnd_wait_HC0(dev) == 0) {
234 outb(cmd, dev->io + HP_CVR);
235 spin_unlock_irqrestore(&dev->lock, flags);
238 spin_unlock_irqrestore(&dev->lock, flags);
240 printk(KERN_DEBUG LOGNAME ": Send DSP command timeout\n");
245 int msnd_send_word(multisound_dev_t *dev, unsigned char high,
246 unsigned char mid, unsigned char low)
248 register unsigned int io = dev->io;
250 if (msnd_wait_TXDE(dev) == 0) {
251 outb(high, io + HP_TXH);
252 outb(mid, io + HP_TXM);
253 outb(low, io + HP_TXL);
257 printk(KERN_DEBUG LOGNAME ": Send host word timeout\n");
262 int msnd_upload_host(multisound_dev_t *dev, char *bin, int len)
267 printk(KERN_WARNING LOGNAME ": Upload host data not multiple of 3!\n");
271 for (i = 0; i < len; i += 3)
272 if (msnd_send_word(dev, bin[i], bin[i + 1], bin[i + 2]) != 0)
275 inb(dev->io + HP_RXL);
276 inb(dev->io + HP_CVR);
281 int msnd_enable_irq(multisound_dev_t *dev)
288 printk(KERN_DEBUG LOGNAME ": Enabling IRQ\n");
290 spin_lock_irqsave(&dev->lock, flags);
291 if (msnd_wait_TXDE(dev) == 0) {
292 outb(inb(dev->io + HP_ICR) | HPICR_TREQ, dev->io + HP_ICR);
293 if (dev->type == msndClassic)
294 outb(dev->irqid, dev->io + HP_IRQM);
295 outb(inb(dev->io + HP_ICR) & ~HPICR_TREQ, dev->io + HP_ICR);
296 outb(inb(dev->io + HP_ICR) | HPICR_RREQ, dev->io + HP_ICR);
297 enable_irq(dev->irq);
298 msnd_init_queue(dev->DSPQ, dev->dspq_data_buff, dev->dspq_buff_size);
299 spin_unlock_irqrestore(&dev->lock, flags);
302 spin_unlock_irqrestore(&dev->lock, flags);
304 printk(KERN_DEBUG LOGNAME ": Enable IRQ failed\n");
309 int msnd_disable_irq(multisound_dev_t *dev)
313 if (--dev->irq_ref > 0)
316 if (dev->irq_ref < 0)
317 printk(KERN_DEBUG LOGNAME ": IRQ ref count is %d\n", dev->irq_ref);
319 printk(KERN_DEBUG LOGNAME ": Disabling IRQ\n");
321 spin_lock_irqsave(&dev->lock, flags);
322 if (msnd_wait_TXDE(dev) == 0) {
323 outb(inb(dev->io + HP_ICR) & ~HPICR_RREQ, dev->io + HP_ICR);
324 if (dev->type == msndClassic)
325 outb(HPIRQ_NONE, dev->io + HP_IRQM);
326 disable_irq(dev->irq);
327 spin_unlock_irqrestore(&dev->lock, flags);
330 spin_unlock_irqrestore(&dev->lock, flags);
332 printk(KERN_DEBUG LOGNAME ": Disable IRQ failed\n");
338 EXPORT_SYMBOL(msnd_register);
339 EXPORT_SYMBOL(msnd_unregister);
340 EXPORT_SYMBOL(msnd_get_num_devs);
341 EXPORT_SYMBOL(msnd_get_dev);
343 EXPORT_SYMBOL(msnd_init_queue);
345 EXPORT_SYMBOL(msnd_fifo_init);
346 EXPORT_SYMBOL(msnd_fifo_free);
347 EXPORT_SYMBOL(msnd_fifo_alloc);
348 EXPORT_SYMBOL(msnd_fifo_make_empty);
349 EXPORT_SYMBOL(msnd_fifo_write);
350 EXPORT_SYMBOL(msnd_fifo_read);
352 EXPORT_SYMBOL(msnd_wait_TXDE);
353 EXPORT_SYMBOL(msnd_wait_HC0);
354 EXPORT_SYMBOL(msnd_send_dsp_cmd);
355 EXPORT_SYMBOL(msnd_send_word);
356 EXPORT_SYMBOL(msnd_upload_host);
358 EXPORT_SYMBOL(msnd_enable_irq);
359 EXPORT_SYMBOL(msnd_disable_irq);
363 MODULE_AUTHOR ("Andrew Veliath <andrewtv@usa.net>");
364 MODULE_DESCRIPTION ("Turtle Beach MultiSound Driver Base");
365 MODULE_LICENSE("GPL");
368 int init_module(void)
373 void cleanup_module(void)