* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / sound / msnd.c
blob4bfca087526762ae67482fd29ea3441b95b1f780
1 /*********************************************************************
3 * msnd.c - Driver Base
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
29 # define LINUX20
30 #endif
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>
36 #include <linux/mm.h>
37 #ifdef LINUX20
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"
43 #else
44 # include <linux/init.h>
45 # include <asm/io.h>
46 # include <asm/uaccess.h>
47 # include <linux/spinlock.h>
48 #endif
49 #include <asm/irq.h>
50 #include "msnd.h"
52 #define LOGNAME "msnd"
54 #define MSND_MAX_DEVS 4
56 static multisound_dev_t *devs[MSND_MAX_DEVS];
57 static int num_devs;
59 int msnd_register(multisound_dev_t *dev)
61 int i;
63 for (i = 0; i < MSND_MAX_DEVS; ++i)
64 if (devs[i] == NULL)
65 break;
67 if (i == MSND_MAX_DEVS)
68 return -ENOMEM;
70 devs[i] = dev;
71 ++num_devs;
73 MOD_INC_USE_COUNT;
75 return 0;
78 void msnd_unregister(multisound_dev_t *dev)
80 int i;
82 for (i = 0; i < MSND_MAX_DEVS; ++i)
83 if (devs[i] == dev)
84 break;
86 if (i == MSND_MAX_DEVS) {
87 printk(KERN_WARNING LOGNAME ": Unregistering unknown device\n");
88 return;
91 devs[i] = NULL;
92 --num_devs;
94 MOD_DEC_USE_COUNT;
97 int msnd_get_num_devs(void)
99 return num_devs;
102 multisound_dev_t *msnd_get_dev(int j)
104 int i;
106 for (i = 0; i < MSND_MAX_DEVS && j; ++i)
107 if (devs[i] != NULL)
108 --j;
110 if (i == MSND_MAX_DEVS || j != 0)
111 return NULL;
113 return devs[i];
116 void msnd_fifo_init(msnd_fifo *f)
118 f->data = NULL;
121 void msnd_fifo_free(msnd_fifo *f)
123 if (f->data) {
124 vfree(f->data);
125 f->data = NULL;
129 int msnd_fifo_alloc(msnd_fifo *f, size_t n)
131 msnd_fifo_free(f);
132 f->data = (char *)vmalloc(n);
133 f->n = n;
134 f->tail = 0;
135 f->head = 0;
136 f->len = 0;
138 if (!f->data)
139 return -ENOMEM;
141 return 0;
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)
151 int count = 0;
153 if (f->len == f->n)
154 return 0;
156 while ((count < len) && (f->len != f->n)) {
158 int nwritten;
160 if (f->head <= f->tail) {
161 nwritten = len - count;
162 if (nwritten > f->n - f->tail)
163 nwritten = f->n - f->tail;
165 else {
166 nwritten = f->head - f->tail;
167 if (nwritten > len - count)
168 nwritten = len - count;
171 if (user) {
172 if (copy_from_user(f->data + f->tail, buf, nwritten))
173 return -EFAULT;
174 } else
175 memcpy(f->data + f->tail, buf, nwritten);
177 count += nwritten;
178 buf += nwritten;
179 f->len += nwritten;
180 f->tail += nwritten;
181 f->tail %= f->n;
184 return count;
187 int msnd_fifo_read(msnd_fifo *f, char *buf, size_t len, int user)
189 int count = 0;
191 if (f->len == 0)
192 return f->len;
194 while ((count < len) && (f->len > 0)) {
196 int nread;
198 if (f->tail <= f->head) {
199 nread = len - count;
200 if (nread > f->n - f->head)
201 nread = f->n - f->head;
203 else {
204 nread = f->tail - f->head;
205 if (nread > len - count)
206 nread = len - count;
209 if (user) {
210 if (copy_to_user(buf, f->data + f->head, nread))
211 return -EFAULT;
212 } else
213 memcpy(buf, f->data + f->head, nread);
215 count += nread;
216 buf += nread;
217 f->len -= nread;
218 f->head += nread;
219 f->head %= f->n;
222 return count;
225 int msnd_wait_TXDE(multisound_dev_t *dev)
227 register unsigned int io = dev->io;
228 register int timeout = 1000;
230 while(timeout-- > 0)
231 if (inb(io + HP_ISR) & HPISR_TXDE)
232 return 0;
234 return -EIO;
237 int msnd_wait_HC0(multisound_dev_t *dev)
239 register unsigned int io = dev->io;
240 register int timeout = 1000;
242 while(timeout-- > 0)
243 if (!(inb(io + HP_CVR) & HPCVR_HC))
244 return 0;
246 return -EIO;
249 int msnd_send_dsp_cmd(multisound_dev_t *dev, BYTE cmd)
251 unsigned long flags;
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);
257 return 0;
259 spin_unlock_irqrestore(&dev->lock, flags);
261 printk(KERN_DEBUG LOGNAME ": Send DSP command timeout\n");
263 return -EIO;
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);
275 return 0;
278 printk(KERN_DEBUG LOGNAME ": Send host word timeout\n");
280 return -EIO;
283 int msnd_upload_host(multisound_dev_t *dev, char *bin, int len)
285 int i;
287 if (len % 3 != 0) {
288 printk(KERN_WARNING LOGNAME ": Upload host data not multiple of 3!\n");
289 return -EINVAL;
292 for (i = 0; i < len; i += 3)
293 if (msnd_send_word(dev, bin[i], bin[i + 1], bin[i + 2]) != 0)
294 return -EIO;
296 inb(dev->io + HP_RXL);
297 inb(dev->io + HP_CVR);
299 return 0;
302 int msnd_enable_irq(multisound_dev_t *dev)
304 unsigned long flags;
306 if (dev->irq_ref++)
307 return 0;
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);
320 return 0;
322 spin_unlock_irqrestore(&dev->lock, flags);
324 printk(KERN_DEBUG LOGNAME ": Enable IRQ failed\n");
326 return -EIO;
329 int msnd_disable_irq(multisound_dev_t *dev)
331 unsigned long flags;
333 if (--dev->irq_ref > 0)
334 return 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);
348 return 0;
350 spin_unlock_irqrestore(&dev->lock, flags);
352 printk(KERN_DEBUG LOGNAME ": Disable IRQ failed\n");
354 return -EIO;
357 #ifndef LINUX20
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);
378 #endif
380 #ifdef MODULE
381 MODULE_AUTHOR ("Andrew Veliath <andrewtv@usa.net>");
382 MODULE_DESCRIPTION ("Turtle Beach MultiSound Driver Base");
384 int init_module(void)
386 return 0;
389 void cleanup_module(void)
392 #endif