[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / sound / oss / btaudio.c
bloba85093fec7bed003cee67724e07320123e05c454
1 /*
2 btaudio - bt878 audio dma driver for linux 2.4.x
4 (c) 2000-2002 Gerd Knorr <kraxel@bytesex.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/pci.h>
25 #include <linux/sched.h>
26 #include <linux/signal.h>
27 #include <linux/types.h>
28 #include <linux/interrupt.h>
29 #include <linux/init.h>
30 #include <linux/poll.h>
31 #include <linux/sound.h>
32 #include <linux/soundcard.h>
33 #include <linux/slab.h>
34 #include <linux/kdev_t.h>
35 #include <asm/uaccess.h>
36 #include <asm/io.h>
39 /* mmio access */
40 #define btwrite(dat,adr) writel((dat), (bta->mmio+(adr)))
41 #define btread(adr) readl(bta->mmio+(adr))
43 #define btand(dat,adr) btwrite((dat) & btread(adr), adr)
44 #define btor(dat,adr) btwrite((dat) | btread(adr), adr)
45 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
47 /* registers (shifted because bta->mmio is long) */
48 #define REG_INT_STAT (0x100 >> 2)
49 #define REG_INT_MASK (0x104 >> 2)
50 #define REG_GPIO_DMA_CTL (0x10c >> 2)
51 #define REG_PACKET_LEN (0x110 >> 2)
52 #define REG_RISC_STRT_ADD (0x114 >> 2)
53 #define REG_RISC_COUNT (0x120 >> 2)
55 /* IRQ bits - REG_INT_(STAT|MASK) */
56 #define IRQ_SCERR (1 << 19)
57 #define IRQ_OCERR (1 << 18)
58 #define IRQ_PABORT (1 << 17)
59 #define IRQ_RIPERR (1 << 16)
60 #define IRQ_PPERR (1 << 15)
61 #define IRQ_FDSR (1 << 14)
62 #define IRQ_FTRGT (1 << 13)
63 #define IRQ_FBUS (1 << 12)
64 #define IRQ_RISCI (1 << 11)
65 #define IRQ_OFLOW (1 << 3)
67 #define IRQ_BTAUDIO (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\
68 IRQ_PPERR | IRQ_FDSR | IRQ_FTRGT | IRQ_FBUS |\
69 IRQ_RISCI)
71 /* REG_GPIO_DMA_CTL bits */
72 #define DMA_CTL_A_PWRDN (1 << 26)
73 #define DMA_CTL_DA_SBR (1 << 14)
74 #define DMA_CTL_DA_ES2 (1 << 13)
75 #define DMA_CTL_ACAP_EN (1 << 4)
76 #define DMA_CTL_RISC_EN (1 << 1)
77 #define DMA_CTL_FIFO_EN (1 << 0)
79 /* RISC instructions */
80 #define RISC_WRITE (0x01 << 28)
81 #define RISC_JUMP (0x07 << 28)
82 #define RISC_SYNC (0x08 << 28)
84 /* RISC bits */
85 #define RISC_WR_SOL (1 << 27)
86 #define RISC_WR_EOL (1 << 26)
87 #define RISC_IRQ (1 << 24)
88 #define RISC_SYNC_RESYNC (1 << 15)
89 #define RISC_SYNC_FM1 0x06
90 #define RISC_SYNC_VRO 0x0c
92 #define HWBASE_AD (448000)
94 /* -------------------------------------------------------------- */
96 struct btaudio {
97 /* linked list */
98 struct btaudio *next;
100 /* device info */
101 int dsp_digital;
102 int dsp_analog;
103 int mixer_dev;
104 struct pci_dev *pci;
105 unsigned int irq;
106 unsigned long mem;
107 unsigned long __iomem *mmio;
109 /* locking */
110 int users;
111 struct semaphore lock;
113 /* risc instructions */
114 unsigned int risc_size;
115 unsigned long *risc_cpu;
116 dma_addr_t risc_dma;
118 /* audio data */
119 unsigned int buf_size;
120 unsigned char *buf_cpu;
121 dma_addr_t buf_dma;
123 /* buffer setup */
124 int line_bytes;
125 int line_count;
126 int block_bytes;
127 int block_count;
129 /* read fifo management */
130 int recording;
131 int dma_block;
132 int read_offset;
133 int read_count;
134 wait_queue_head_t readq;
136 /* settings */
137 int gain[3];
138 int source;
139 int bits;
140 int decimation;
141 int mixcount;
142 int sampleshift;
143 int channels;
144 int analog;
145 int rate;
148 struct cardinfo {
149 char *name;
150 int rate;
153 static struct btaudio *btaudios;
154 static unsigned int debug;
155 static unsigned int irq_debug;
157 /* -------------------------------------------------------------- */
159 #define BUF_DEFAULT 128*1024
160 #define BUF_MIN 8192
162 static int alloc_buffer(struct btaudio *bta)
164 if (NULL == bta->buf_cpu) {
165 for (bta->buf_size = BUF_DEFAULT; bta->buf_size >= BUF_MIN;
166 bta->buf_size = bta->buf_size >> 1) {
167 bta->buf_cpu = pci_alloc_consistent
168 (bta->pci, bta->buf_size, &bta->buf_dma);
169 if (NULL != bta->buf_cpu)
170 break;
172 if (NULL == bta->buf_cpu)
173 return -ENOMEM;
174 memset(bta->buf_cpu,0,bta->buf_size);
176 if (NULL == bta->risc_cpu) {
177 bta->risc_size = PAGE_SIZE;
178 bta->risc_cpu = pci_alloc_consistent
179 (bta->pci, bta->risc_size, &bta->risc_dma);
180 if (NULL == bta->risc_cpu) {
181 pci_free_consistent(bta->pci, bta->buf_size, bta->buf_cpu, bta->buf_dma);
182 bta->buf_cpu = NULL;
183 return -ENOMEM;
186 return 0;
189 static void free_buffer(struct btaudio *bta)
191 if (NULL != bta->buf_cpu) {
192 pci_free_consistent(bta->pci, bta->buf_size,
193 bta->buf_cpu, bta->buf_dma);
194 bta->buf_cpu = NULL;
196 if (NULL != bta->risc_cpu) {
197 pci_free_consistent(bta->pci, bta->risc_size,
198 bta->risc_cpu, bta->risc_dma);
199 bta->risc_cpu = NULL;
203 static int make_risc(struct btaudio *bta)
205 int rp, bp, line, block;
206 unsigned long risc;
208 bta->block_bytes = bta->buf_size >> 4;
209 bta->block_count = 1 << 4;
210 bta->line_bytes = bta->block_bytes;
211 bta->line_count = bta->block_count;
212 while (bta->line_bytes > 4095) {
213 bta->line_bytes >>= 1;
214 bta->line_count <<= 1;
216 if (bta->line_count > 255)
217 return -EINVAL;
218 if (debug)
219 printk(KERN_DEBUG
220 "btaudio: bufsize=%d - bs=%d bc=%d - ls=%d, lc=%d\n",
221 bta->buf_size,bta->block_bytes,bta->block_count,
222 bta->line_bytes,bta->line_count);
223 rp = 0; bp = 0;
224 block = 0;
225 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_FM1);
226 bta->risc_cpu[rp++] = cpu_to_le32(0);
227 for (line = 0; line < bta->line_count; line++) {
228 risc = RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL;
229 risc |= bta->line_bytes;
230 if (0 == (bp & (bta->block_bytes-1))) {
231 risc |= RISC_IRQ;
232 risc |= (block & 0x0f) << 16;
233 risc |= (~block & 0x0f) << 20;
234 block++;
236 bta->risc_cpu[rp++] = cpu_to_le32(risc);
237 bta->risc_cpu[rp++] = cpu_to_le32(bta->buf_dma + bp);
238 bp += bta->line_bytes;
240 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_VRO);
241 bta->risc_cpu[rp++] = cpu_to_le32(0);
242 bta->risc_cpu[rp++] = cpu_to_le32(RISC_JUMP);
243 bta->risc_cpu[rp++] = cpu_to_le32(bta->risc_dma);
244 return 0;
247 static int start_recording(struct btaudio *bta)
249 int ret;
251 if (0 != (ret = alloc_buffer(bta)))
252 return ret;
253 if (0 != (ret = make_risc(bta)))
254 return ret;
256 btwrite(bta->risc_dma, REG_RISC_STRT_ADD);
257 btwrite((bta->line_count << 16) | bta->line_bytes,
258 REG_PACKET_LEN);
259 btwrite(IRQ_BTAUDIO, REG_INT_MASK);
260 if (bta->analog) {
261 btwrite(DMA_CTL_ACAP_EN |
262 DMA_CTL_RISC_EN |
263 DMA_CTL_FIFO_EN |
264 DMA_CTL_DA_ES2 |
265 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
266 (bta->gain[bta->source] << 28) |
267 (bta->source << 24) |
268 (bta->decimation << 8),
269 REG_GPIO_DMA_CTL);
270 } else {
271 btwrite(DMA_CTL_ACAP_EN |
272 DMA_CTL_RISC_EN |
273 DMA_CTL_FIFO_EN |
274 DMA_CTL_DA_ES2 |
275 DMA_CTL_A_PWRDN |
276 (1 << 6) |
277 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
278 (bta->gain[bta->source] << 28) |
279 (bta->source << 24) |
280 (bta->decimation << 8),
281 REG_GPIO_DMA_CTL);
283 bta->dma_block = 0;
284 bta->read_offset = 0;
285 bta->read_count = 0;
286 bta->recording = 1;
287 if (debug)
288 printk(KERN_DEBUG "btaudio: recording started\n");
289 return 0;
292 static void stop_recording(struct btaudio *bta)
294 btand(~15, REG_GPIO_DMA_CTL);
295 bta->recording = 0;
296 if (debug)
297 printk(KERN_DEBUG "btaudio: recording stopped\n");
301 /* -------------------------------------------------------------- */
303 static int btaudio_mixer_open(struct inode *inode, struct file *file)
305 int minor = iminor(inode);
306 struct btaudio *bta;
308 for (bta = btaudios; bta != NULL; bta = bta->next)
309 if (bta->mixer_dev == minor)
310 break;
311 if (NULL == bta)
312 return -ENODEV;
314 if (debug)
315 printk("btaudio: open mixer [%d]\n",minor);
316 file->private_data = bta;
317 return 0;
320 static int btaudio_mixer_release(struct inode *inode, struct file *file)
322 return 0;
325 static int btaudio_mixer_ioctl(struct inode *inode, struct file *file,
326 unsigned int cmd, unsigned long arg)
328 struct btaudio *bta = file->private_data;
329 int ret,val=0,i=0;
330 void __user *argp = (void __user *)arg;
332 if (cmd == SOUND_MIXER_INFO) {
333 mixer_info info;
334 memset(&info,0,sizeof(info));
335 strlcpy(info.id,"bt878",sizeof(info.id));
336 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
337 info.modify_counter = bta->mixcount;
338 if (copy_to_user(argp, &info, sizeof(info)))
339 return -EFAULT;
340 return 0;
342 if (cmd == SOUND_OLD_MIXER_INFO) {
343 _old_mixer_info info;
344 memset(&info,0,sizeof(info));
345 strlcpy(info.id,"bt878",sizeof(info.id)-1);
346 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
347 if (copy_to_user(argp, &info, sizeof(info)))
348 return -EFAULT;
349 return 0;
351 if (cmd == OSS_GETVERSION)
352 return put_user(SOUND_VERSION, (int __user *)argp);
354 /* read */
355 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
356 if (get_user(val, (int __user *)argp))
357 return -EFAULT;
359 switch (cmd) {
360 case MIXER_READ(SOUND_MIXER_CAPS):
361 ret = SOUND_CAP_EXCL_INPUT;
362 break;
363 case MIXER_READ(SOUND_MIXER_STEREODEVS):
364 ret = 0;
365 break;
366 case MIXER_READ(SOUND_MIXER_RECMASK):
367 case MIXER_READ(SOUND_MIXER_DEVMASK):
368 ret = SOUND_MASK_LINE1|SOUND_MASK_LINE2|SOUND_MASK_LINE3;
369 break;
371 case MIXER_WRITE(SOUND_MIXER_RECSRC):
372 if (val & SOUND_MASK_LINE1 && bta->source != 0)
373 bta->source = 0;
374 else if (val & SOUND_MASK_LINE2 && bta->source != 1)
375 bta->source = 1;
376 else if (val & SOUND_MASK_LINE3 && bta->source != 2)
377 bta->source = 2;
378 btaor((bta->gain[bta->source] << 28) |
379 (bta->source << 24),
380 0x0cffffff, REG_GPIO_DMA_CTL);
381 case MIXER_READ(SOUND_MIXER_RECSRC):
382 switch (bta->source) {
383 case 0: ret = SOUND_MASK_LINE1; break;
384 case 1: ret = SOUND_MASK_LINE2; break;
385 case 2: ret = SOUND_MASK_LINE3; break;
386 default: ret = 0;
388 break;
390 case MIXER_WRITE(SOUND_MIXER_LINE1):
391 case MIXER_WRITE(SOUND_MIXER_LINE2):
392 case MIXER_WRITE(SOUND_MIXER_LINE3):
393 if (MIXER_WRITE(SOUND_MIXER_LINE1) == cmd)
394 i = 0;
395 if (MIXER_WRITE(SOUND_MIXER_LINE2) == cmd)
396 i = 1;
397 if (MIXER_WRITE(SOUND_MIXER_LINE3) == cmd)
398 i = 2;
399 bta->gain[i] = (val & 0xff) * 15 / 100;
400 if (bta->gain[i] > 15) bta->gain[i] = 15;
401 if (bta->gain[i] < 0) bta->gain[i] = 0;
402 if (i == bta->source)
403 btaor((bta->gain[bta->source]<<28),
404 0x0fffffff, REG_GPIO_DMA_CTL);
405 ret = bta->gain[i] * 100 / 15;
406 ret |= ret << 8;
407 break;
409 case MIXER_READ(SOUND_MIXER_LINE1):
410 case MIXER_READ(SOUND_MIXER_LINE2):
411 case MIXER_READ(SOUND_MIXER_LINE3):
412 if (MIXER_READ(SOUND_MIXER_LINE1) == cmd)
413 i = 0;
414 if (MIXER_READ(SOUND_MIXER_LINE2) == cmd)
415 i = 1;
416 if (MIXER_READ(SOUND_MIXER_LINE3) == cmd)
417 i = 2;
418 ret = bta->gain[i] * 100 / 15;
419 ret |= ret << 8;
420 break;
422 default:
423 return -EINVAL;
425 if (put_user(ret, (int __user *)argp))
426 return -EFAULT;
427 return 0;
430 static struct file_operations btaudio_mixer_fops = {
431 .owner = THIS_MODULE,
432 .llseek = no_llseek,
433 .open = btaudio_mixer_open,
434 .release = btaudio_mixer_release,
435 .ioctl = btaudio_mixer_ioctl,
438 /* -------------------------------------------------------------- */
440 static int btaudio_dsp_open(struct inode *inode, struct file *file,
441 struct btaudio *bta, int analog)
443 down(&bta->lock);
444 if (bta->users)
445 goto busy;
446 bta->users++;
447 file->private_data = bta;
449 bta->analog = analog;
450 bta->dma_block = 0;
451 bta->read_offset = 0;
452 bta->read_count = 0;
453 bta->sampleshift = 0;
455 up(&bta->lock);
456 return 0;
458 busy:
459 up(&bta->lock);
460 return -EBUSY;
463 static int btaudio_dsp_open_digital(struct inode *inode, struct file *file)
465 int minor = iminor(inode);
466 struct btaudio *bta;
468 for (bta = btaudios; bta != NULL; bta = bta->next)
469 if (bta->dsp_digital == minor)
470 break;
471 if (NULL == bta)
472 return -ENODEV;
474 if (debug)
475 printk("btaudio: open digital dsp [%d]\n",minor);
476 return btaudio_dsp_open(inode,file,bta,0);
479 static int btaudio_dsp_open_analog(struct inode *inode, struct file *file)
481 int minor = iminor(inode);
482 struct btaudio *bta;
484 for (bta = btaudios; bta != NULL; bta = bta->next)
485 if (bta->dsp_analog == minor)
486 break;
487 if (NULL == bta)
488 return -ENODEV;
490 if (debug)
491 printk("btaudio: open analog dsp [%d]\n",minor);
492 return btaudio_dsp_open(inode,file,bta,1);
495 static int btaudio_dsp_release(struct inode *inode, struct file *file)
497 struct btaudio *bta = file->private_data;
499 down(&bta->lock);
500 if (bta->recording)
501 stop_recording(bta);
502 bta->users--;
503 up(&bta->lock);
504 return 0;
507 static ssize_t btaudio_dsp_read(struct file *file, char __user *buffer,
508 size_t swcount, loff_t *ppos)
510 struct btaudio *bta = file->private_data;
511 int hwcount = swcount << bta->sampleshift;
512 int nsrc, ndst, err, ret = 0;
513 DECLARE_WAITQUEUE(wait, current);
515 add_wait_queue(&bta->readq, &wait);
516 down(&bta->lock);
517 while (swcount > 0) {
518 if (0 == bta->read_count) {
519 if (!bta->recording) {
520 if (0 != (err = start_recording(bta))) {
521 if (0 == ret)
522 ret = err;
523 break;
526 if (file->f_flags & O_NONBLOCK) {
527 if (0 == ret)
528 ret = -EAGAIN;
529 break;
531 up(&bta->lock);
532 current->state = TASK_INTERRUPTIBLE;
533 schedule();
534 down(&bta->lock);
535 if(signal_pending(current)) {
536 if (0 == ret)
537 ret = -EINTR;
538 break;
541 nsrc = (bta->read_count < hwcount) ? bta->read_count : hwcount;
542 if (nsrc > bta->buf_size - bta->read_offset)
543 nsrc = bta->buf_size - bta->read_offset;
544 ndst = nsrc >> bta->sampleshift;
546 if ((bta->analog && 0 == bta->sampleshift) ||
547 (!bta->analog && 2 == bta->channels)) {
548 /* just copy */
549 if (copy_to_user(buffer + ret, bta->buf_cpu + bta->read_offset, nsrc)) {
550 if (0 == ret)
551 ret = -EFAULT;
552 break;
555 } else if (!bta->analog) {
556 /* stereo => mono (digital audio) */
557 __s16 *src = (__s16*)(bta->buf_cpu + bta->read_offset);
558 __s16 __user *dst = (__s16 __user *)(buffer + ret);
559 __s16 avg;
560 int n = ndst>>1;
561 if (!access_ok(VERIFY_WRITE, dst, ndst)) {
562 if (0 == ret)
563 ret = -EFAULT;
564 break;
566 for (; n; n--, dst++) {
567 avg = (__s16)le16_to_cpu(*src) / 2; src++;
568 avg += (__s16)le16_to_cpu(*src) / 2; src++;
569 __put_user(cpu_to_le16(avg),dst);
572 } else if (8 == bta->bits) {
573 /* copy + byte downsampling (audio A/D) */
574 __u8 *src = bta->buf_cpu + bta->read_offset;
575 __u8 __user *dst = buffer + ret;
576 int n = ndst;
577 if (!access_ok(VERIFY_WRITE, dst, ndst)) {
578 if (0 == ret)
579 ret = -EFAULT;
580 break;
582 for (; n; n--, src += (1 << bta->sampleshift), dst++)
583 __put_user(*src, dst);
585 } else {
586 /* copy + word downsampling (audio A/D) */
587 __u16 *src = (__u16*)(bta->buf_cpu + bta->read_offset);
588 __u16 __user *dst = (__u16 __user *)(buffer + ret);
589 int n = ndst>>1;
590 if (!access_ok(VERIFY_WRITE,dst,ndst)) {
591 if (0 == ret)
592 ret = -EFAULT;
593 break;
595 for (; n; n--, src += (1 << bta->sampleshift), dst++)
596 __put_user(*src, dst);
599 ret += ndst;
600 swcount -= ndst;
601 hwcount -= nsrc;
602 bta->read_count -= nsrc;
603 bta->read_offset += nsrc;
604 if (bta->read_offset == bta->buf_size)
605 bta->read_offset = 0;
607 up(&bta->lock);
608 remove_wait_queue(&bta->readq, &wait);
609 current->state = TASK_RUNNING;
610 return ret;
613 static ssize_t btaudio_dsp_write(struct file *file, const char __user *buffer,
614 size_t count, loff_t *ppos)
616 return -EINVAL;
619 static int btaudio_dsp_ioctl(struct inode *inode, struct file *file,
620 unsigned int cmd, unsigned long arg)
622 struct btaudio *bta = file->private_data;
623 int s, i, ret, val = 0;
624 void __user *argp = (void __user *)arg;
625 int __user *p = argp;
627 switch (cmd) {
628 case OSS_GETVERSION:
629 return put_user(SOUND_VERSION, p);
630 case SNDCTL_DSP_GETCAPS:
631 return 0;
633 case SNDCTL_DSP_SPEED:
634 if (get_user(val, p))
635 return -EFAULT;
636 if (bta->analog) {
637 for (s = 0; s < 16; s++)
638 if (val << s >= HWBASE_AD*4/15)
639 break;
640 for (i = 15; i >= 5; i--)
641 if (val << s <= HWBASE_AD*4/i)
642 break;
643 bta->sampleshift = s;
644 bta->decimation = i;
645 if (debug)
646 printk(KERN_DEBUG "btaudio: rate: req=%d "
647 "dec=%d shift=%d hwrate=%d swrate=%d\n",
648 val,i,s,(HWBASE_AD*4/i),(HWBASE_AD*4/i)>>s);
649 } else {
650 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
651 bta->decimation = 0;
653 if (bta->recording) {
654 down(&bta->lock);
655 stop_recording(bta);
656 start_recording(bta);
657 up(&bta->lock);
659 /* fall through */
660 case SOUND_PCM_READ_RATE:
661 if (bta->analog) {
662 return put_user(HWBASE_AD*4/bta->decimation>>bta->sampleshift, p);
663 } else {
664 return put_user(bta->rate, p);
667 case SNDCTL_DSP_STEREO:
668 if (!bta->analog) {
669 if (get_user(val, p))
670 return -EFAULT;
671 bta->channels = (val > 0) ? 2 : 1;
672 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
673 if (debug)
674 printk(KERN_INFO
675 "btaudio: stereo=%d channels=%d\n",
676 val,bta->channels);
677 } else {
678 if (val == 1)
679 return -EFAULT;
680 else {
681 bta->channels = 1;
682 if (debug)
683 printk(KERN_INFO
684 "btaudio: stereo=0 channels=1\n");
687 return put_user((bta->channels)-1, p);
689 case SNDCTL_DSP_CHANNELS:
690 if (!bta->analog) {
691 if (get_user(val, p))
692 return -EFAULT;
693 bta->channels = (val > 1) ? 2 : 1;
694 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
695 if (debug)
696 printk(KERN_DEBUG
697 "btaudio: val=%d channels=%d\n",
698 val,bta->channels);
700 /* fall through */
701 case SOUND_PCM_READ_CHANNELS:
702 return put_user(bta->channels, p);
704 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
705 if (bta->analog)
706 return put_user(AFMT_S16_LE|AFMT_S8, p);
707 else
708 return put_user(AFMT_S16_LE, p);
710 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
711 if (get_user(val, p))
712 return -EFAULT;
713 if (val != AFMT_QUERY) {
714 if (bta->analog)
715 bta->bits = (val == AFMT_S8) ? 8 : 16;
716 else
717 bta->bits = 16;
718 if (bta->recording) {
719 down(&bta->lock);
720 stop_recording(bta);
721 start_recording(bta);
722 up(&bta->lock);
725 if (debug)
726 printk(KERN_DEBUG "btaudio: fmt: bits=%d\n",bta->bits);
727 return put_user((bta->bits==16) ? AFMT_S16_LE : AFMT_S8,
729 break;
730 case SOUND_PCM_READ_BITS:
731 return put_user(bta->bits, p);
733 case SNDCTL_DSP_NONBLOCK:
734 file->f_flags |= O_NONBLOCK;
735 return 0;
737 case SNDCTL_DSP_RESET:
738 if (bta->recording) {
739 down(&bta->lock);
740 stop_recording(bta);
741 up(&bta->lock);
743 return 0;
744 case SNDCTL_DSP_GETBLKSIZE:
745 if (!bta->recording) {
746 if (0 != (ret = alloc_buffer(bta)))
747 return ret;
748 if (0 != (ret = make_risc(bta)))
749 return ret;
751 return put_user(bta->block_bytes>>bta->sampleshift,p);
753 case SNDCTL_DSP_SYNC:
754 /* NOP */
755 return 0;
756 case SNDCTL_DSP_GETISPACE:
758 audio_buf_info info;
759 if (!bta->recording)
760 return -EINVAL;
761 info.fragsize = bta->block_bytes>>bta->sampleshift;
762 info.fragstotal = bta->block_count;
763 info.bytes = bta->read_count;
764 info.fragments = info.bytes / info.fragsize;
765 if (debug)
766 printk(KERN_DEBUG "btaudio: SNDCTL_DSP_GETISPACE "
767 "returns %d/%d/%d/%d\n",
768 info.fragsize, info.fragstotal,
769 info.bytes, info.fragments);
770 if (copy_to_user(argp, &info, sizeof(info)))
771 return -EFAULT;
772 return 0;
774 #if 0 /* TODO */
775 case SNDCTL_DSP_GETTRIGGER:
776 case SNDCTL_DSP_SETTRIGGER:
777 case SNDCTL_DSP_SETFRAGMENT:
778 #endif
779 default:
780 return -EINVAL;
784 static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait)
786 struct btaudio *bta = file->private_data;
787 unsigned int mask = 0;
789 poll_wait(file, &bta->readq, wait);
791 if (0 != bta->read_count)
792 mask |= (POLLIN | POLLRDNORM);
794 return mask;
797 static struct file_operations btaudio_digital_dsp_fops = {
798 .owner = THIS_MODULE,
799 .llseek = no_llseek,
800 .open = btaudio_dsp_open_digital,
801 .release = btaudio_dsp_release,
802 .read = btaudio_dsp_read,
803 .write = btaudio_dsp_write,
804 .ioctl = btaudio_dsp_ioctl,
805 .poll = btaudio_dsp_poll,
808 static struct file_operations btaudio_analog_dsp_fops = {
809 .owner = THIS_MODULE,
810 .llseek = no_llseek,
811 .open = btaudio_dsp_open_analog,
812 .release = btaudio_dsp_release,
813 .read = btaudio_dsp_read,
814 .write = btaudio_dsp_write,
815 .ioctl = btaudio_dsp_ioctl,
816 .poll = btaudio_dsp_poll,
819 /* -------------------------------------------------------------- */
821 static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "",
822 "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
823 "RIPERR", "PABORT", "OCERR", "SCERR" };
825 static irqreturn_t btaudio_irq(int irq, void *dev_id, struct pt_regs * regs)
827 int count = 0;
828 u32 stat,astat;
829 struct btaudio *bta = dev_id;
830 int handled = 0;
832 for (;;) {
833 count++;
834 stat = btread(REG_INT_STAT);
835 astat = stat & btread(REG_INT_MASK);
836 if (!astat)
837 return IRQ_RETVAL(handled);
838 handled = 1;
839 btwrite(astat,REG_INT_STAT);
841 if (irq_debug) {
842 int i;
843 printk(KERN_DEBUG "btaudio: irq loop=%d risc=%x, bits:",
844 count, stat>>28);
845 for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) {
846 if (stat & (1 << i))
847 printk(" %s",irq_name[i]);
848 if (astat & (1 << i))
849 printk("*");
851 printk("\n");
853 if (stat & IRQ_RISCI) {
854 int blocks;
855 blocks = (stat >> 28) - bta->dma_block;
856 if (blocks < 0)
857 blocks += bta->block_count;
858 bta->dma_block = stat >> 28;
859 if (bta->read_count + 2*bta->block_bytes > bta->buf_size) {
860 stop_recording(bta);
861 printk(KERN_INFO "btaudio: buffer overrun\n");
863 if (blocks > 0) {
864 bta->read_count += blocks * bta->block_bytes;
865 wake_up_interruptible(&bta->readq);
868 if (count > 10) {
869 printk(KERN_WARNING
870 "btaudio: Oops - irq mask cleared\n");
871 btwrite(0, REG_INT_MASK);
874 return IRQ_NONE;
877 /* -------------------------------------------------------------- */
879 static unsigned int dsp1 = -1;
880 static unsigned int dsp2 = -1;
881 static unsigned int mixer = -1;
882 static int latency = -1;
883 static int digital = 1;
884 static int analog = 1;
885 static int rate;
887 #define BTA_OSPREY200 1
889 static struct cardinfo cards[] = {
890 [0] = {
891 .name = "default",
892 .rate = 32000,
894 [BTA_OSPREY200] = {
895 .name = "Osprey 200",
896 .rate = 44100,
900 static int __devinit btaudio_probe(struct pci_dev *pci_dev,
901 const struct pci_device_id *pci_id)
903 struct btaudio *bta;
904 struct cardinfo *card = &cards[pci_id->driver_data];
905 unsigned char revision,lat;
906 int rc = -EBUSY;
908 if (pci_enable_device(pci_dev))
909 return -EIO;
910 if (!request_mem_region(pci_resource_start(pci_dev,0),
911 pci_resource_len(pci_dev,0),
912 "btaudio")) {
913 return -EBUSY;
916 bta = kmalloc(sizeof(*bta),GFP_ATOMIC);
917 if (!bta) {
918 rc = -ENOMEM;
919 goto fail0;
921 memset(bta,0,sizeof(*bta));
923 bta->pci = pci_dev;
924 bta->irq = pci_dev->irq;
925 bta->mem = pci_resource_start(pci_dev,0);
926 bta->mmio = ioremap(pci_resource_start(pci_dev,0),
927 pci_resource_len(pci_dev,0));
929 bta->source = 1;
930 bta->bits = 8;
931 bta->channels = 1;
932 if (bta->analog) {
933 bta->decimation = 15;
934 } else {
935 bta->decimation = 0;
936 bta->sampleshift = 1;
939 /* sample rate */
940 bta->rate = card->rate;
941 if (rate)
942 bta->rate = rate;
944 init_MUTEX(&bta->lock);
945 init_waitqueue_head(&bta->readq);
947 if (-1 != latency) {
948 printk(KERN_INFO "btaudio: setting pci latency timer to %d\n",
949 latency);
950 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
952 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
953 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat);
954 printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ",
955 pci_dev->device,revision,pci_dev->bus->number,
956 PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn));
957 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
958 bta->irq, lat, bta->mem);
959 printk("btaudio: using card config \"%s\"\n", card->name);
961 /* init hw */
962 btwrite(0, REG_GPIO_DMA_CTL);
963 btwrite(0, REG_INT_MASK);
964 btwrite(~0U, REG_INT_STAT);
965 pci_set_master(pci_dev);
967 if ((rc = request_irq(bta->irq, btaudio_irq, SA_SHIRQ|SA_INTERRUPT,
968 "btaudio",(void *)bta)) < 0) {
969 printk(KERN_WARNING
970 "btaudio: can't request irq (rc=%d)\n",rc);
971 goto fail1;
974 /* register devices */
975 if (digital) {
976 rc = bta->dsp_digital =
977 register_sound_dsp(&btaudio_digital_dsp_fops,dsp1);
978 if (rc < 0) {
979 printk(KERN_WARNING
980 "btaudio: can't register digital dsp (rc=%d)\n",rc);
981 goto fail2;
983 printk(KERN_INFO "btaudio: registered device dsp%d [digital]\n",
984 bta->dsp_digital >> 4);
986 if (analog) {
987 rc = bta->dsp_analog =
988 register_sound_dsp(&btaudio_analog_dsp_fops,dsp2);
989 if (rc < 0) {
990 printk(KERN_WARNING
991 "btaudio: can't register analog dsp (rc=%d)\n",rc);
992 goto fail3;
994 printk(KERN_INFO "btaudio: registered device dsp%d [analog]\n",
995 bta->dsp_analog >> 4);
996 rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer);
997 if (rc < 0) {
998 printk(KERN_WARNING
999 "btaudio: can't register mixer (rc=%d)\n",rc);
1000 goto fail4;
1002 printk(KERN_INFO "btaudio: registered device mixer%d\n",
1003 bta->mixer_dev >> 4);
1006 /* hook into linked list */
1007 bta->next = btaudios;
1008 btaudios = bta;
1010 pci_set_drvdata(pci_dev,bta);
1011 return 0;
1013 fail4:
1014 unregister_sound_dsp(bta->dsp_analog);
1015 fail3:
1016 if (digital)
1017 unregister_sound_dsp(bta->dsp_digital);
1018 fail2:
1019 free_irq(bta->irq,bta);
1020 fail1:
1021 kfree(bta);
1022 fail0:
1023 release_mem_region(pci_resource_start(pci_dev,0),
1024 pci_resource_len(pci_dev,0));
1025 return rc;
1028 static void __devexit btaudio_remove(struct pci_dev *pci_dev)
1030 struct btaudio *bta = pci_get_drvdata(pci_dev);
1031 struct btaudio *walk;
1033 /* turn off all DMA / IRQs */
1034 btand(~15, REG_GPIO_DMA_CTL);
1035 btwrite(0, REG_INT_MASK);
1036 btwrite(~0U, REG_INT_STAT);
1038 /* unregister devices */
1039 if (digital) {
1040 unregister_sound_dsp(bta->dsp_digital);
1042 if (analog) {
1043 unregister_sound_dsp(bta->dsp_analog);
1044 unregister_sound_mixer(bta->mixer_dev);
1047 /* free resources */
1048 free_buffer(bta);
1049 free_irq(bta->irq,bta);
1050 release_mem_region(pci_resource_start(pci_dev,0),
1051 pci_resource_len(pci_dev,0));
1053 /* remove from linked list */
1054 if (bta == btaudios) {
1055 btaudios = NULL;
1056 } else {
1057 for (walk = btaudios; walk->next != bta; walk = walk->next)
1058 ; /* if (NULL == walk->next) BUG(); */
1059 walk->next = bta->next;
1062 pci_set_drvdata(pci_dev, NULL);
1063 kfree(bta);
1064 return;
1067 /* -------------------------------------------------------------- */
1069 static struct pci_device_id btaudio_pci_tbl[] = {
1071 .vendor = PCI_VENDOR_ID_BROOKTREE,
1072 .device = 0x0878,
1073 .subvendor = 0x0070,
1074 .subdevice = 0xff01,
1075 .driver_data = BTA_OSPREY200,
1077 .vendor = PCI_VENDOR_ID_BROOKTREE,
1078 .device = 0x0878,
1079 .subvendor = PCI_ANY_ID,
1080 .subdevice = PCI_ANY_ID,
1082 .vendor = PCI_VENDOR_ID_BROOKTREE,
1083 .device = 0x0878,
1084 .subvendor = PCI_ANY_ID,
1085 .subdevice = PCI_ANY_ID,
1087 /* --- end of list --- */
1091 static struct pci_driver btaudio_pci_driver = {
1092 .name = "btaudio",
1093 .id_table = btaudio_pci_tbl,
1094 .probe = btaudio_probe,
1095 .remove = __devexit_p(btaudio_remove),
1098 static int btaudio_init_module(void)
1100 printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n",
1101 digital ? "digital" : "",
1102 analog && digital ? "+" : "",
1103 analog ? "analog" : "");
1104 return pci_module_init(&btaudio_pci_driver);
1107 static void btaudio_cleanup_module(void)
1109 pci_unregister_driver(&btaudio_pci_driver);
1110 return;
1113 module_init(btaudio_init_module);
1114 module_exit(btaudio_cleanup_module);
1116 module_param(dsp1, int, S_IRUGO);
1117 module_param(dsp2, int, S_IRUGO);
1118 module_param(mixer, int, S_IRUGO);
1119 module_param(debug, int, S_IRUGO | S_IWUSR);
1120 module_param(irq_debug, int, S_IRUGO | S_IWUSR);
1121 module_param(digital, int, S_IRUGO);
1122 module_param(analog, int, S_IRUGO);
1123 module_param(rate, int, S_IRUGO);
1124 module_param(latency, int, S_IRUGO);
1125 MODULE_PARM_DESC(latency,"pci latency timer");
1127 MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
1128 MODULE_DESCRIPTION("bt878 audio dma driver");
1129 MODULE_AUTHOR("Gerd Knorr");
1130 MODULE_LICENSE("GPL");
1133 * Local variables:
1134 * c-basic-offset: 8
1135 * End: