1 /*********************************************************************
2 *
3 * msnd.c - Driver Base
4 *
5 * Turtle Beach MultiSound Sound Card Driver for Linux
6 *
7 * Copyright (C) 1998 Andrew Veliath
8 *
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.
13 *
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.
18 *
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.
22 *
23 * $Id: msnd.c,v 1.17 1999/03/21 16:50:09 andrewtv Exp $
24 *
25 ********************************************************************/
26
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/slab.h>
34 #include <linux/vmalloc.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/mm.h>
38 #ifdef LINUX20
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"
44 #else
45 # include <linux/init.h>
46 # include <asm/io.h>
47 # include <asm/uaccess.h>
48 # include <linux/spinlock.h>
49 #endif
50 #include <asm/irq.h>
51 #include "msnd.h"
52
53 #define LOGNAME "msnd"
54
55 #define MSND_MAX_DEVS 4
56
57 static multisound_dev_t *devs[MSND_MAX_DEVS];
58 static int num_devs;
59
msnd_register(multisound_dev_t * dev)60 int __init msnd_register(multisound_dev_t *dev)
61 {
62 int i;
63
64 for (i = 0; i < MSND_MAX_DEVS; ++i)
65 if (devs[i] == NULL)
66 break;
67
68 if (i == MSND_MAX_DEVS)
69 return -ENOMEM;
70
71 devs[i] = dev;
72 ++num_devs;
73
74 MOD_INC_USE_COUNT;
75
76 return 0;
77 }
78
msnd_unregister(multisound_dev_t * dev)79 void msnd_unregister(multisound_dev_t *dev)
80 {
81 int i;
82
83 for (i = 0; i < MSND_MAX_DEVS; ++i)
84 if (devs[i] == dev)
85 break;
86
87 if (i == MSND_MAX_DEVS) {
88 printk(KERN_WARNING LOGNAME ": Unregistering unknown device\n");
89 return;
90 }
91
92 devs[i] = NULL;
93 --num_devs;
94
95 MOD_DEC_USE_COUNT;
96 }
97
msnd_get_num_devs(void)98 int msnd_get_num_devs(void)
99 {
100 return num_devs;
101 }
102
msnd_get_dev(int j)103 multisound_dev_t *msnd_get_dev(int j)
104 {
105 int i;
106
107 for (i = 0; i < MSND_MAX_DEVS && j; ++i)
108 if (devs[i] != NULL)
109 --j;
110
111 if (i == MSND_MAX_DEVS || j != 0)
112 return NULL;
113
114 return devs[i];
115 }
116
msnd_init_queue(unsigned long base,int start,int size)117 void msnd_init_queue(unsigned long base, int start, int size)
118 {
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);
123 }
124
msnd_fifo_init(msnd_fifo * f)125 void msnd_fifo_init(msnd_fifo *f)
126 {
127 f->data = NULL;
128 }
129
msnd_fifo_free(msnd_fifo * f)130 void msnd_fifo_free(msnd_fifo *f)
131 {
132 if (f->data) {
133 vfree(f->data);
134 f->data = NULL;
135 }
136 }
137
msnd_fifo_alloc(msnd_fifo * f,size_t n)138 int msnd_fifo_alloc(msnd_fifo *f, size_t n)
139 {
140 msnd_fifo_free(f);
141 f->data = (char *)vmalloc(n);
142 f->n = n;
143 f->tail = 0;
144 f->head = 0;
145 f->len = 0;
146
147 if (!f->data)
148 return -ENOMEM;
149
150 return 0;
151 }
152
msnd_fifo_make_empty(msnd_fifo * f)153 void msnd_fifo_make_empty(msnd_fifo *f)
154 {
155 f->len = f->tail = f->head = 0;
156 }
157
msnd_fifo_write(msnd_fifo * f,const char * buf,size_t len)158 int msnd_fifo_write(msnd_fifo *f, const char *buf, size_t len)
159 {
160 int count = 0;
161
162 while ((count < len) && (f->len != f->n)) {
163
164 int nwritten;
165
166 if (f->head <= f->tail) {
167 nwritten = len - count;
168 if (nwritten > f->n - f->tail)
169 nwritten = f->n - f->tail;
170 }
171 else {
172 nwritten = f->head - f->tail;
173 if (nwritten > len - count)
174 nwritten = len - count;
175 }
176
177 isa_memcpy_fromio(f->data + f->tail, (unsigned long) buf, nwritten);
178
179 count += nwritten;
180 buf += nwritten;
181 f->len += nwritten;
182 f->tail += nwritten;
183 f->tail %= f->n;
184 }
185
186 return count;
187 }
188
msnd_fifo_read(msnd_fifo * f,char * buf,size_t len)189 int msnd_fifo_read(msnd_fifo *f, char *buf, size_t len)
190 {
191 int count = 0;
192
193 while ((count < len) && (f->len > 0)) {
194
195 int nread;
196
197 if (f->tail <= f->head) {
198 nread = len - count;
199 if (nread > f->n - f->head)
200 nread = f->n - f->head;
201 }
202 else {
203 nread = f->tail - f->head;
204 if (nread > len - count)
205 nread = len - count;
206 }
207
208 isa_memcpy_toio((unsigned long) buf, f->data + f->head, nread);
209
210 count += nread;
211 buf += nread;
212 f->len -= nread;
213 f->head += nread;
214 f->head %= f->n;
215 }
216
217 return count;
218 }
219
msnd_wait_TXDE(multisound_dev_t * dev)220 int msnd_wait_TXDE(multisound_dev_t *dev)
221 {
222 register unsigned int io = dev->io;
223 register int timeout = 1000;
224
225 while(timeout-- > 0)
226 if (inb(io + HP_ISR) & HPISR_TXDE)
227 return 0;
228
229 return -EIO;
230 }
231
msnd_wait_HC0(multisound_dev_t * dev)232 int msnd_wait_HC0(multisound_dev_t *dev)
233 {
234 register unsigned int io = dev->io;
235 register int timeout = 1000;
236
237 while(timeout-- > 0)
238 if (!(inb(io + HP_CVR) & HPCVR_HC))
239 return 0;
240
241 return -EIO;
242 }
243
msnd_send_dsp_cmd(multisound_dev_t * dev,BYTE cmd)244 int msnd_send_dsp_cmd(multisound_dev_t *dev, BYTE cmd)
245 {
246 unsigned long flags;
247
248 spin_lock_irqsave(&dev->lock, flags);
249 if (msnd_wait_HC0(dev) == 0) {
250 outb(cmd, dev->io + HP_CVR);
251 spin_unlock_irqrestore(&dev->lock, flags);
252 return 0;
253 }
254 spin_unlock_irqrestore(&dev->lock, flags);
255
256 printk(KERN_DEBUG LOGNAME ": Send DSP command timeout\n");
257
258 return -EIO;
259 }
260
msnd_send_word(multisound_dev_t * dev,unsigned char high,unsigned char mid,unsigned char low)261 int msnd_send_word(multisound_dev_t *dev, unsigned char high,
262 unsigned char mid, unsigned char low)
263 {
264 register unsigned int io = dev->io;
265
266 if (msnd_wait_TXDE(dev) == 0) {
267 outb(high, io + HP_TXH);
268 outb(mid, io + HP_TXM);
269 outb(low, io + HP_TXL);
270 return 0;
271 }
272
273 printk(KERN_DEBUG LOGNAME ": Send host word timeout\n");
274
275 return -EIO;
276 }
277
msnd_upload_host(multisound_dev_t * dev,char * bin,int len)278 int msnd_upload_host(multisound_dev_t *dev, char *bin, int len)
279 {
280 int i;
281
282 if (len % 3 != 0) {
283 printk(KERN_WARNING LOGNAME ": Upload host data not multiple of 3!\n");
284 return -EINVAL;
285 }
286
287 for (i = 0; i < len; i += 3)
288 if (msnd_send_word(dev, bin[i], bin[i + 1], bin[i + 2]) != 0)
289 return -EIO;
290
291 inb(dev->io + HP_RXL);
292 inb(dev->io + HP_CVR);
293
294 return 0;
295 }
296
msnd_enable_irq(multisound_dev_t * dev)297 int msnd_enable_irq(multisound_dev_t *dev)
298 {
299 unsigned long flags;
300
301 if (dev->irq_ref++)
302 return 0;
303
304 printk(KERN_DEBUG LOGNAME ": Enabling IRQ\n");
305
306 spin_lock_irqsave(&dev->lock, flags);
307 if (msnd_wait_TXDE(dev) == 0) {
308 outb(inb(dev->io + HP_ICR) | HPICR_TREQ, dev->io + HP_ICR);
309 if (dev->type == msndClassic)
310 outb(dev->irqid, dev->io + HP_IRQM);
311 outb(inb(dev->io + HP_ICR) & ~HPICR_TREQ, dev->io + HP_ICR);
312 outb(inb(dev->io + HP_ICR) | HPICR_RREQ, dev->io + HP_ICR);
313 enable_irq(dev->irq);
314 msnd_init_queue(dev->DSPQ, dev->dspq_data_buff, dev->dspq_buff_size);
315 spin_unlock_irqrestore(&dev->lock, flags);
316 return 0;
317 }
318 spin_unlock_irqrestore(&dev->lock, flags);
319
320 printk(KERN_DEBUG LOGNAME ": Enable IRQ failed\n");
321
322 return -EIO;
323 }
324
msnd_disable_irq(multisound_dev_t * dev)325 int msnd_disable_irq(multisound_dev_t *dev)
326 {
327 unsigned long flags;
328
329 if (--dev->irq_ref > 0)
330 return 0;
331
332 if (dev->irq_ref < 0)
333 printk(KERN_DEBUG LOGNAME ": IRQ ref count is %d\n", dev->irq_ref);
334
335 printk(KERN_DEBUG LOGNAME ": Disabling IRQ\n");
336
337 spin_lock_irqsave(&dev->lock, flags);
338 if (msnd_wait_TXDE(dev) == 0) {
339 outb(inb(dev->io + HP_ICR) & ~HPICR_RREQ, dev->io + HP_ICR);
340 if (dev->type == msndClassic)
341 outb(HPIRQ_NONE, dev->io + HP_IRQM);
342 disable_irq(dev->irq);
343 spin_unlock_irqrestore(&dev->lock, flags);
344 return 0;
345 }
346 spin_unlock_irqrestore(&dev->lock, flags);
347
348 printk(KERN_DEBUG LOGNAME ": Disable IRQ failed\n");
349
350 return -EIO;
351 }
352
353 #ifndef LINUX20
354 EXPORT_SYMBOL(msnd_register);
355 EXPORT_SYMBOL(msnd_unregister);
356 EXPORT_SYMBOL(msnd_get_num_devs);
357 EXPORT_SYMBOL(msnd_get_dev);
358
359 EXPORT_SYMBOL(msnd_init_queue);
360
361 EXPORT_SYMBOL(msnd_fifo_init);
362 EXPORT_SYMBOL(msnd_fifo_free);
363 EXPORT_SYMBOL(msnd_fifo_alloc);
364 EXPORT_SYMBOL(msnd_fifo_make_empty);
365 EXPORT_SYMBOL(msnd_fifo_write);
366 EXPORT_SYMBOL(msnd_fifo_read);
367
368 EXPORT_SYMBOL(msnd_wait_TXDE);
369 EXPORT_SYMBOL(msnd_wait_HC0);
370 EXPORT_SYMBOL(msnd_send_dsp_cmd);
371 EXPORT_SYMBOL(msnd_send_word);
372 EXPORT_SYMBOL(msnd_upload_host);
373
374 EXPORT_SYMBOL(msnd_enable_irq);
375 EXPORT_SYMBOL(msnd_disable_irq);
376 #endif
377
378 #ifdef MODULE
379 MODULE_AUTHOR ("Andrew Veliath <andrewtv@usa.net>");
380 MODULE_DESCRIPTION ("Turtle Beach MultiSound Driver Base");
381 MODULE_LICENSE("GPL");
382
383
init_module(void)384 int init_module(void)
385 {
386 return 0;
387 }
388
cleanup_module(void)389 void cleanup_module(void)
390 {
391 }
392 #endif
393