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