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.1.1.1 2005/04/11 02:50:39 jack Exp $
25 ********************************************************************/
27 #include <linux/version.h>
28 #if LINUX_VERSION_CODE < 0x020101
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/vmalloc.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
39 # include <linux/major.h>
40 # include <linux/fs.h>
41 # include <linux/sound.h>
42 # include <asm/segment.h>
43 # include "sound_config.h"
45 # include <linux/init.h>
47 # include <asm/uaccess.h>
48 # include <linux/spinlock.h>
53 #define LOGNAME "msnd"
55 #define MSND_MAX_DEVS 4
57 static multisound_dev_t *devs[MSND_MAX_DEVS];
60 int __init msnd_register(multisound_dev_t *dev)
64 for (i = 0; i < MSND_MAX_DEVS; ++i)
68 if (i == MSND_MAX_DEVS)
79 void msnd_unregister(multisound_dev_t *dev)
83 for (i = 0; i < MSND_MAX_DEVS; ++i)
87 if (i == MSND_MAX_DEVS) {
88 printk(KERN_WARNING LOGNAME ": Unregistering unknown device\n");
98 int msnd_get_num_devs(void)
103 multisound_dev_t *msnd_get_dev(int j)
107 for (i = 0; i < MSND_MAX_DEVS && j; ++i)
111 if (i == MSND_MAX_DEVS || j != 0)
117 void msnd_init_queue(unsigned long base, int start, int size)
119 isa_writew(PCTODSP_BASED(start), base + JQS_wStart);
120 isa_writew(PCTODSP_OFFSET(size) - 1, base + JQS_wSize);
121 isa_writew(0, base + JQS_wHead);
122 isa_writew(0, base + JQS_wTail);
125 void msnd_fifo_init(msnd_fifo *f)
130 void msnd_fifo_free(msnd_fifo *f)
138 int msnd_fifo_alloc(msnd_fifo *f, size_t n)
141 f->data = (char *)vmalloc(n);
153 void msnd_fifo_make_empty(msnd_fifo *f)
155 f->len = f->tail = f->head = 0;
158 int msnd_fifo_write(msnd_fifo *f, const char *buf, size_t len, int user)
165 while ((count < len) && (f->len != f->n)) {
169 if (f->head <= f->tail) {
170 nwritten = len - count;
171 if (nwritten > f->n - f->tail)
172 nwritten = f->n - f->tail;
175 nwritten = f->head - f->tail;
176 if (nwritten > len - count)
177 nwritten = len - count;
181 if (copy_from_user(f->data + f->tail, buf, nwritten))
184 isa_memcpy_fromio(f->data + f->tail, (unsigned long) buf, nwritten);
196 int msnd_fifo_read(msnd_fifo *f, char *buf, size_t len, int user)
203 while ((count < len) && (f->len > 0)) {
207 if (f->tail <= f->head) {
209 if (nread > f->n - f->head)
210 nread = f->n - f->head;
213 nread = f->tail - f->head;
214 if (nread > len - count)
219 if (copy_to_user(buf, f->data + f->head, nread))
222 isa_memcpy_toio((unsigned long) buf, f->data + f->head, nread);
234 int msnd_wait_TXDE(multisound_dev_t *dev)
236 register unsigned int io = dev->io;
237 register int timeout = 1000;
240 if (inb(io + HP_ISR) & HPISR_TXDE)
246 int msnd_wait_HC0(multisound_dev_t *dev)
248 register unsigned int io = dev->io;
249 register int timeout = 1000;
252 if (!(inb(io + HP_CVR) & HPCVR_HC))
258 int msnd_send_dsp_cmd(multisound_dev_t *dev, BYTE cmd)
262 spin_lock_irqsave(&dev->lock, flags);
263 if (msnd_wait_HC0(dev) == 0) {
264 outb(cmd, dev->io + HP_CVR);
265 spin_unlock_irqrestore(&dev->lock, flags);
268 spin_unlock_irqrestore(&dev->lock, flags);
270 printk(KERN_DEBUG LOGNAME ": Send DSP command timeout\n");
275 int msnd_send_word(multisound_dev_t *dev, unsigned char high,
276 unsigned char mid, unsigned char low)
278 register unsigned int io = dev->io;
280 if (msnd_wait_TXDE(dev) == 0) {
281 outb(high, io + HP_TXH);
282 outb(mid, io + HP_TXM);
283 outb(low, io + HP_TXL);
287 printk(KERN_DEBUG LOGNAME ": Send host word timeout\n");
292 int msnd_upload_host(multisound_dev_t *dev, char *bin, int len)
297 printk(KERN_WARNING LOGNAME ": Upload host data not multiple of 3!\n");
301 for (i = 0; i < len; i += 3)
302 if (msnd_send_word(dev, bin[i], bin[i + 1], bin[i + 2]) != 0)
305 inb(dev->io + HP_RXL);
306 inb(dev->io + HP_CVR);
311 int msnd_enable_irq(multisound_dev_t *dev)
318 printk(KERN_DEBUG LOGNAME ": Enabling IRQ\n");
320 spin_lock_irqsave(&dev->lock, flags);
321 if (msnd_wait_TXDE(dev) == 0) {
322 outb(inb(dev->io + HP_ICR) | HPICR_TREQ, dev->io + HP_ICR);
323 if (dev->type == msndClassic)
324 outb(dev->irqid, dev->io + HP_IRQM);
325 outb(inb(dev->io + HP_ICR) & ~HPICR_TREQ, dev->io + HP_ICR);
326 outb(inb(dev->io + HP_ICR) | HPICR_RREQ, dev->io + HP_ICR);
327 enable_irq(dev->irq);
328 msnd_init_queue(dev->DSPQ, dev->dspq_data_buff, dev->dspq_buff_size);
329 spin_unlock_irqrestore(&dev->lock, flags);
332 spin_unlock_irqrestore(&dev->lock, flags);
334 printk(KERN_DEBUG LOGNAME ": Enable IRQ failed\n");
339 int msnd_disable_irq(multisound_dev_t *dev)
343 if (--dev->irq_ref > 0)
346 if (dev->irq_ref < 0)
347 printk(KERN_DEBUG LOGNAME ": IRQ ref count is %d\n", dev->irq_ref);
349 printk(KERN_DEBUG LOGNAME ": Disabling IRQ\n");
351 spin_lock_irqsave(&dev->lock, flags);
352 if (msnd_wait_TXDE(dev) == 0) {
353 outb(inb(dev->io + HP_ICR) & ~HPICR_RREQ, dev->io + HP_ICR);
354 if (dev->type == msndClassic)
355 outb(HPIRQ_NONE, dev->io + HP_IRQM);
356 disable_irq(dev->irq);
357 spin_unlock_irqrestore(&dev->lock, flags);
360 spin_unlock_irqrestore(&dev->lock, flags);
362 printk(KERN_DEBUG LOGNAME ": Disable IRQ failed\n");
368 EXPORT_SYMBOL(msnd_register);
369 EXPORT_SYMBOL(msnd_unregister);
370 EXPORT_SYMBOL(msnd_get_num_devs);
371 EXPORT_SYMBOL(msnd_get_dev);
373 EXPORT_SYMBOL(msnd_init_queue);
375 EXPORT_SYMBOL(msnd_fifo_init);
376 EXPORT_SYMBOL(msnd_fifo_free);
377 EXPORT_SYMBOL(msnd_fifo_alloc);
378 EXPORT_SYMBOL(msnd_fifo_make_empty);
379 EXPORT_SYMBOL(msnd_fifo_write);
380 EXPORT_SYMBOL(msnd_fifo_read);
382 EXPORT_SYMBOL(msnd_wait_TXDE);
383 EXPORT_SYMBOL(msnd_wait_HC0);
384 EXPORT_SYMBOL(msnd_send_dsp_cmd);
385 EXPORT_SYMBOL(msnd_send_word);
386 EXPORT_SYMBOL(msnd_upload_host);
388 EXPORT_SYMBOL(msnd_enable_irq);
389 EXPORT_SYMBOL(msnd_disable_irq);
393 MODULE_AUTHOR ("Andrew Veliath <andrewtv@usa.net>");
394 MODULE_DESCRIPTION ("Turtle Beach MultiSound Driver Base");
395 MODULE_LICENSE("GPL");
398 int init_module(void)
403 void cleanup_module(void)