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.16 1998/09/08 04:05:56 andrewtv 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/malloc.h>
34 #include <linux/types.h>
35 #include <linux/delay.h>
38 # include <linux/major.h>
39 # include <linux/fs.h>
40 # include <linux/sound.h>
41 # include <asm/segment.h>
42 # include "sound_config.h"
44 # include <linux/init.h>
46 # include <asm/uaccess.h>
47 # include <linux/spinlock.h>
52 #define LOGNAME "msnd"
54 #define MSND_MAX_DEVS 4
56 static multisound_dev_t
*devs
[MSND_MAX_DEVS
];
59 int msnd_register(multisound_dev_t
*dev
)
63 for (i
= 0; i
< MSND_MAX_DEVS
; ++i
)
67 if (i
== MSND_MAX_DEVS
)
78 void msnd_unregister(multisound_dev_t
*dev
)
82 for (i
= 0; i
< MSND_MAX_DEVS
; ++i
)
86 if (i
== MSND_MAX_DEVS
) {
87 printk(KERN_WARNING LOGNAME
": Unregistering unknown device\n");
97 int msnd_get_num_devs(void)
102 multisound_dev_t
*msnd_get_dev(int j
)
106 for (i
= 0; i
< MSND_MAX_DEVS
&& j
; ++i
)
110 if (i
== MSND_MAX_DEVS
|| j
!= 0)
116 void msnd_fifo_init(msnd_fifo
*f
)
121 void msnd_fifo_free(msnd_fifo
*f
)
129 int msnd_fifo_alloc(msnd_fifo
*f
, size_t n
)
132 f
->data
= (char *)vmalloc(n
);
144 void msnd_fifo_make_empty(msnd_fifo
*f
)
146 f
->len
= f
->tail
= f
->head
= 0;
149 int msnd_fifo_write(msnd_fifo
*f
, const char *buf
, size_t len
, int user
)
156 while ((count
< len
) && (f
->len
!= f
->n
)) {
160 if (f
->head
<= f
->tail
) {
161 nwritten
= len
- count
;
162 if (nwritten
> f
->n
- f
->tail
)
163 nwritten
= f
->n
- f
->tail
;
166 nwritten
= f
->head
- f
->tail
;
167 if (nwritten
> len
- count
)
168 nwritten
= len
- count
;
172 if (copy_from_user(f
->data
+ f
->tail
, buf
, nwritten
))
175 memcpy(f
->data
+ f
->tail
, buf
, nwritten
);
187 int msnd_fifo_read(msnd_fifo
*f
, char *buf
, size_t len
, int user
)
194 while ((count
< len
) && (f
->len
> 0)) {
198 if (f
->tail
<= f
->head
) {
200 if (nread
> f
->n
- f
->head
)
201 nread
= f
->n
- f
->head
;
204 nread
= f
->tail
- f
->head
;
205 if (nread
> len
- count
)
210 if (copy_to_user(buf
, f
->data
+ f
->head
, nread
))
213 memcpy(buf
, f
->data
+ f
->head
, nread
);
225 int msnd_wait_TXDE(multisound_dev_t
*dev
)
227 register unsigned int io
= dev
->io
;
228 register int timeout
= 1000;
231 if (inb(io
+ HP_ISR
) & HPISR_TXDE
)
237 int msnd_wait_HC0(multisound_dev_t
*dev
)
239 register unsigned int io
= dev
->io
;
240 register int timeout
= 1000;
243 if (!(inb(io
+ HP_CVR
) & HPCVR_HC
))
249 int msnd_send_dsp_cmd(multisound_dev_t
*dev
, BYTE cmd
)
253 spin_lock_irqsave(&dev
->lock
, flags
);
254 if (msnd_wait_HC0(dev
) == 0) {
255 outb(cmd
, dev
->io
+ HP_CVR
);
256 spin_unlock_irqrestore(&dev
->lock
, flags
);
259 spin_unlock_irqrestore(&dev
->lock
, flags
);
261 printk(KERN_DEBUG LOGNAME
": Send DSP command timeout\n");
266 int msnd_send_word(multisound_dev_t
*dev
, unsigned char high
,
267 unsigned char mid
, unsigned char low
)
269 register unsigned int io
= dev
->io
;
271 if (msnd_wait_TXDE(dev
) == 0) {
272 outb(high
, io
+ HP_TXH
);
273 outb(mid
, io
+ HP_TXM
);
274 outb(low
, io
+ HP_TXL
);
278 printk(KERN_DEBUG LOGNAME
": Send host word timeout\n");
283 int msnd_upload_host(multisound_dev_t
*dev
, char *bin
, int len
)
288 printk(KERN_WARNING LOGNAME
": Upload host data not multiple of 3!\n");
292 for (i
= 0; i
< len
; i
+= 3)
293 if (msnd_send_word(dev
, bin
[i
], bin
[i
+ 1], bin
[i
+ 2]) != 0)
296 inb(dev
->io
+ HP_RXL
);
297 inb(dev
->io
+ HP_CVR
);
302 int msnd_enable_irq(multisound_dev_t
*dev
)
309 printk(KERN_DEBUG LOGNAME
": Enabling IRQ\n");
311 spin_lock_irqsave(&dev
->lock
, flags
);
312 if (msnd_wait_TXDE(dev
) == 0) {
313 outb(inb(dev
->io
+ HP_ICR
) | HPICR_TREQ
, dev
->io
+ HP_ICR
);
314 if (dev
->type
== msndClassic
)
315 outb(dev
->irqid
, dev
->io
+ HP_IRQM
);
316 outb(inb(dev
->io
+ HP_ICR
) & ~HPICR_TREQ
, dev
->io
+ HP_ICR
);
317 outb(inb(dev
->io
+ HP_ICR
) | HPICR_RREQ
, dev
->io
+ HP_ICR
);
318 enable_irq(dev
->irq
);
319 spin_unlock_irqrestore(&dev
->lock
, flags
);
322 spin_unlock_irqrestore(&dev
->lock
, flags
);
324 printk(KERN_DEBUG LOGNAME
": Enable IRQ failed\n");
329 int msnd_disable_irq(multisound_dev_t
*dev
)
333 if (--dev
->irq_ref
> 0)
336 if (dev
->irq_ref
< 0)
337 printk(KERN_DEBUG LOGNAME
": IRQ ref count is %d\n", dev
->irq_ref
);
339 printk(KERN_DEBUG LOGNAME
": Disabling IRQ\n");
341 spin_lock_irqsave(&dev
->lock
, flags
);
342 if (msnd_wait_TXDE(dev
) == 0) {
343 outb(inb(dev
->io
+ HP_ICR
) & ~HPICR_RREQ
, dev
->io
+ HP_ICR
);
344 if (dev
->type
== msndClassic
)
345 outb(HPIRQ_NONE
, dev
->io
+ HP_IRQM
);
346 disable_irq(dev
->irq
);
347 spin_unlock_irqrestore(&dev
->lock
, flags
);
350 spin_unlock_irqrestore(&dev
->lock
, flags
);
352 printk(KERN_DEBUG LOGNAME
": Disable IRQ failed\n");
358 EXPORT_SYMBOL(msnd_register
);
359 EXPORT_SYMBOL(msnd_unregister
);
360 EXPORT_SYMBOL(msnd_get_num_devs
);
361 EXPORT_SYMBOL(msnd_get_dev
);
363 EXPORT_SYMBOL(msnd_fifo_init
);
364 EXPORT_SYMBOL(msnd_fifo_free
);
365 EXPORT_SYMBOL(msnd_fifo_alloc
);
366 EXPORT_SYMBOL(msnd_fifo_make_empty
);
367 EXPORT_SYMBOL(msnd_fifo_write
);
368 EXPORT_SYMBOL(msnd_fifo_read
);
370 EXPORT_SYMBOL(msnd_wait_TXDE
);
371 EXPORT_SYMBOL(msnd_wait_HC0
);
372 EXPORT_SYMBOL(msnd_send_dsp_cmd
);
373 EXPORT_SYMBOL(msnd_send_word
);
374 EXPORT_SYMBOL(msnd_upload_host
);
376 EXPORT_SYMBOL(msnd_enable_irq
);
377 EXPORT_SYMBOL(msnd_disable_irq
);
381 MODULE_AUTHOR ("Andrew Veliath <andrewtv@usa.net>");
382 MODULE_DESCRIPTION ("Turtle Beach MultiSound Driver Base");
384 int init_module(void)
389 void cleanup_module(void)