x86_64: x86_64 - Use non locked version for local_cmpxchg()
[linux-2.6/pdupreez.git] / sound / oss / btaudio.c
blob4d5cf05b8922605421a09a9716c957782c771635
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 <linux/mutex.h>
37 #include <asm/uaccess.h>
38 #include <asm/io.h>
41 /* mmio access */
42 #define btwrite(dat,adr) writel((dat), (bta->mmio+(adr)))
43 #define btread(adr) readl(bta->mmio+(adr))
45 #define btand(dat,adr) btwrite((dat) & btread(adr), adr)
46 #define btor(dat,adr) btwrite((dat) | btread(adr), adr)
47 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
49 /* registers (shifted because bta->mmio is long) */
50 #define REG_INT_STAT (0x100 >> 2)
51 #define REG_INT_MASK (0x104 >> 2)
52 #define REG_GPIO_DMA_CTL (0x10c >> 2)
53 #define REG_PACKET_LEN (0x110 >> 2)
54 #define REG_RISC_STRT_ADD (0x114 >> 2)
55 #define REG_RISC_COUNT (0x120 >> 2)
57 /* IRQ bits - REG_INT_(STAT|MASK) */
58 #define IRQ_SCERR (1 << 19)
59 #define IRQ_OCERR (1 << 18)
60 #define IRQ_PABORT (1 << 17)
61 #define IRQ_RIPERR (1 << 16)
62 #define IRQ_PPERR (1 << 15)
63 #define IRQ_FDSR (1 << 14)
64 #define IRQ_FTRGT (1 << 13)
65 #define IRQ_FBUS (1 << 12)
66 #define IRQ_RISCI (1 << 11)
67 #define IRQ_OFLOW (1 << 3)
69 #define IRQ_BTAUDIO (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\
70 IRQ_PPERR | IRQ_FDSR | IRQ_FTRGT | IRQ_FBUS |\
71 IRQ_RISCI)
73 /* REG_GPIO_DMA_CTL bits */
74 #define DMA_CTL_A_PWRDN (1 << 26)
75 #define DMA_CTL_DA_SBR (1 << 14)
76 #define DMA_CTL_DA_ES2 (1 << 13)
77 #define DMA_CTL_ACAP_EN (1 << 4)
78 #define DMA_CTL_RISC_EN (1 << 1)
79 #define DMA_CTL_FIFO_EN (1 << 0)
81 /* RISC instructions */
82 #define RISC_WRITE (0x01 << 28)
83 #define RISC_JUMP (0x07 << 28)
84 #define RISC_SYNC (0x08 << 28)
86 /* RISC bits */
87 #define RISC_WR_SOL (1 << 27)
88 #define RISC_WR_EOL (1 << 26)
89 #define RISC_IRQ (1 << 24)
90 #define RISC_SYNC_RESYNC (1 << 15)
91 #define RISC_SYNC_FM1 0x06
92 #define RISC_SYNC_VRO 0x0c
94 #define HWBASE_AD (448000)
96 /* -------------------------------------------------------------- */
98 struct btaudio {
99 /* linked list */
100 struct btaudio *next;
102 /* device info */
103 int dsp_digital;
104 int dsp_analog;
105 int mixer_dev;
106 struct pci_dev *pci;
107 unsigned int irq;
108 unsigned long mem;
109 unsigned long __iomem *mmio;
111 /* locking */
112 int users;
113 struct mutex lock;
115 /* risc instructions */
116 unsigned int risc_size;
117 unsigned long *risc_cpu;
118 dma_addr_t risc_dma;
120 /* audio data */
121 unsigned int buf_size;
122 unsigned char *buf_cpu;
123 dma_addr_t buf_dma;
125 /* buffer setup */
126 int line_bytes;
127 int line_count;
128 int block_bytes;
129 int block_count;
131 /* read fifo management */
132 int recording;
133 int dma_block;
134 int read_offset;
135 int read_count;
136 wait_queue_head_t readq;
138 /* settings */
139 int gain[3];
140 int source;
141 int bits;
142 int decimation;
143 int mixcount;
144 int sampleshift;
145 int channels;
146 int analog;
147 int rate;
150 struct cardinfo {
151 char *name;
152 int rate;
155 static struct btaudio *btaudios;
156 static unsigned int debug;
157 static unsigned int irq_debug;
159 /* -------------------------------------------------------------- */
161 #define BUF_DEFAULT 128*1024
162 #define BUF_MIN 8192
164 static int alloc_buffer(struct btaudio *bta)
166 if (NULL == bta->buf_cpu) {
167 for (bta->buf_size = BUF_DEFAULT; bta->buf_size >= BUF_MIN;
168 bta->buf_size = bta->buf_size >> 1) {
169 bta->buf_cpu = pci_alloc_consistent
170 (bta->pci, bta->buf_size, &bta->buf_dma);
171 if (NULL != bta->buf_cpu)
172 break;
174 if (NULL == bta->buf_cpu)
175 return -ENOMEM;
176 memset(bta->buf_cpu,0,bta->buf_size);
178 if (NULL == bta->risc_cpu) {
179 bta->risc_size = PAGE_SIZE;
180 bta->risc_cpu = pci_alloc_consistent
181 (bta->pci, bta->risc_size, &bta->risc_dma);
182 if (NULL == bta->risc_cpu) {
183 pci_free_consistent(bta->pci, bta->buf_size, bta->buf_cpu, bta->buf_dma);
184 bta->buf_cpu = NULL;
185 return -ENOMEM;
188 return 0;
191 static void free_buffer(struct btaudio *bta)
193 if (NULL != bta->buf_cpu) {
194 pci_free_consistent(bta->pci, bta->buf_size,
195 bta->buf_cpu, bta->buf_dma);
196 bta->buf_cpu = NULL;
198 if (NULL != bta->risc_cpu) {
199 pci_free_consistent(bta->pci, bta->risc_size,
200 bta->risc_cpu, bta->risc_dma);
201 bta->risc_cpu = NULL;
205 static int make_risc(struct btaudio *bta)
207 int rp, bp, line, block;
208 unsigned long risc;
210 bta->block_bytes = bta->buf_size >> 4;
211 bta->block_count = 1 << 4;
212 bta->line_bytes = bta->block_bytes;
213 bta->line_count = bta->block_count;
214 while (bta->line_bytes > 4095) {
215 bta->line_bytes >>= 1;
216 bta->line_count <<= 1;
218 if (bta->line_count > 255)
219 return -EINVAL;
220 if (debug)
221 printk(KERN_DEBUG
222 "btaudio: bufsize=%d - bs=%d bc=%d - ls=%d, lc=%d\n",
223 bta->buf_size,bta->block_bytes,bta->block_count,
224 bta->line_bytes,bta->line_count);
225 rp = 0; bp = 0;
226 block = 0;
227 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_FM1);
228 bta->risc_cpu[rp++] = cpu_to_le32(0);
229 for (line = 0; line < bta->line_count; line++) {
230 risc = RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL;
231 risc |= bta->line_bytes;
232 if (0 == (bp & (bta->block_bytes-1))) {
233 risc |= RISC_IRQ;
234 risc |= (block & 0x0f) << 16;
235 risc |= (~block & 0x0f) << 20;
236 block++;
238 bta->risc_cpu[rp++] = cpu_to_le32(risc);
239 bta->risc_cpu[rp++] = cpu_to_le32(bta->buf_dma + bp);
240 bp += bta->line_bytes;
242 bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_VRO);
243 bta->risc_cpu[rp++] = cpu_to_le32(0);
244 bta->risc_cpu[rp++] = cpu_to_le32(RISC_JUMP);
245 bta->risc_cpu[rp++] = cpu_to_le32(bta->risc_dma);
246 return 0;
249 static int start_recording(struct btaudio *bta)
251 int ret;
253 if (0 != (ret = alloc_buffer(bta)))
254 return ret;
255 if (0 != (ret = make_risc(bta)))
256 return ret;
258 btwrite(bta->risc_dma, REG_RISC_STRT_ADD);
259 btwrite((bta->line_count << 16) | bta->line_bytes,
260 REG_PACKET_LEN);
261 btwrite(IRQ_BTAUDIO, REG_INT_MASK);
262 if (bta->analog) {
263 btwrite(DMA_CTL_ACAP_EN |
264 DMA_CTL_RISC_EN |
265 DMA_CTL_FIFO_EN |
266 DMA_CTL_DA_ES2 |
267 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
268 (bta->gain[bta->source] << 28) |
269 (bta->source << 24) |
270 (bta->decimation << 8),
271 REG_GPIO_DMA_CTL);
272 } else {
273 btwrite(DMA_CTL_ACAP_EN |
274 DMA_CTL_RISC_EN |
275 DMA_CTL_FIFO_EN |
276 DMA_CTL_DA_ES2 |
277 DMA_CTL_A_PWRDN |
278 (1 << 6) |
279 ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
280 (bta->gain[bta->source] << 28) |
281 (bta->source << 24) |
282 (bta->decimation << 8),
283 REG_GPIO_DMA_CTL);
285 bta->dma_block = 0;
286 bta->read_offset = 0;
287 bta->read_count = 0;
288 bta->recording = 1;
289 if (debug)
290 printk(KERN_DEBUG "btaudio: recording started\n");
291 return 0;
294 static void stop_recording(struct btaudio *bta)
296 btand(~15, REG_GPIO_DMA_CTL);
297 bta->recording = 0;
298 if (debug)
299 printk(KERN_DEBUG "btaudio: recording stopped\n");
303 /* -------------------------------------------------------------- */
305 static int btaudio_mixer_open(struct inode *inode, struct file *file)
307 int minor = iminor(inode);
308 struct btaudio *bta;
310 for (bta = btaudios; bta != NULL; bta = bta->next)
311 if (bta->mixer_dev == minor)
312 break;
313 if (NULL == bta)
314 return -ENODEV;
316 if (debug)
317 printk("btaudio: open mixer [%d]\n",minor);
318 file->private_data = bta;
319 return 0;
322 static int btaudio_mixer_release(struct inode *inode, struct file *file)
324 return 0;
327 static int btaudio_mixer_ioctl(struct inode *inode, struct file *file,
328 unsigned int cmd, unsigned long arg)
330 struct btaudio *bta = file->private_data;
331 int ret,val=0,i=0;
332 void __user *argp = (void __user *)arg;
334 if (cmd == SOUND_MIXER_INFO) {
335 mixer_info info;
336 memset(&info,0,sizeof(info));
337 strlcpy(info.id,"bt878",sizeof(info.id));
338 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
339 info.modify_counter = bta->mixcount;
340 if (copy_to_user(argp, &info, sizeof(info)))
341 return -EFAULT;
342 return 0;
344 if (cmd == SOUND_OLD_MIXER_INFO) {
345 _old_mixer_info info;
346 memset(&info,0,sizeof(info));
347 strlcpy(info.id, "bt878", sizeof(info.id));
348 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
349 if (copy_to_user(argp, &info, sizeof(info)))
350 return -EFAULT;
351 return 0;
353 if (cmd == OSS_GETVERSION)
354 return put_user(SOUND_VERSION, (int __user *)argp);
356 /* read */
357 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
358 if (get_user(val, (int __user *)argp))
359 return -EFAULT;
361 switch (cmd) {
362 case MIXER_READ(SOUND_MIXER_CAPS):
363 ret = SOUND_CAP_EXCL_INPUT;
364 break;
365 case MIXER_READ(SOUND_MIXER_STEREODEVS):
366 ret = 0;
367 break;
368 case MIXER_READ(SOUND_MIXER_RECMASK):
369 case MIXER_READ(SOUND_MIXER_DEVMASK):
370 ret = SOUND_MASK_LINE1|SOUND_MASK_LINE2|SOUND_MASK_LINE3;
371 break;
373 case MIXER_WRITE(SOUND_MIXER_RECSRC):
374 if (val & SOUND_MASK_LINE1 && bta->source != 0)
375 bta->source = 0;
376 else if (val & SOUND_MASK_LINE2 && bta->source != 1)
377 bta->source = 1;
378 else if (val & SOUND_MASK_LINE3 && bta->source != 2)
379 bta->source = 2;
380 btaor((bta->gain[bta->source] << 28) |
381 (bta->source << 24),
382 0x0cffffff, REG_GPIO_DMA_CTL);
383 case MIXER_READ(SOUND_MIXER_RECSRC):
384 switch (bta->source) {
385 case 0: ret = SOUND_MASK_LINE1; break;
386 case 1: ret = SOUND_MASK_LINE2; break;
387 case 2: ret = SOUND_MASK_LINE3; break;
388 default: ret = 0;
390 break;
392 case MIXER_WRITE(SOUND_MIXER_LINE1):
393 case MIXER_WRITE(SOUND_MIXER_LINE2):
394 case MIXER_WRITE(SOUND_MIXER_LINE3):
395 if (MIXER_WRITE(SOUND_MIXER_LINE1) == cmd)
396 i = 0;
397 if (MIXER_WRITE(SOUND_MIXER_LINE2) == cmd)
398 i = 1;
399 if (MIXER_WRITE(SOUND_MIXER_LINE3) == cmd)
400 i = 2;
401 bta->gain[i] = (val & 0xff) * 15 / 100;
402 if (bta->gain[i] > 15) bta->gain[i] = 15;
403 if (bta->gain[i] < 0) bta->gain[i] = 0;
404 if (i == bta->source)
405 btaor((bta->gain[bta->source]<<28),
406 0x0fffffff, REG_GPIO_DMA_CTL);
407 ret = bta->gain[i] * 100 / 15;
408 ret |= ret << 8;
409 break;
411 case MIXER_READ(SOUND_MIXER_LINE1):
412 case MIXER_READ(SOUND_MIXER_LINE2):
413 case MIXER_READ(SOUND_MIXER_LINE3):
414 if (MIXER_READ(SOUND_MIXER_LINE1) == cmd)
415 i = 0;
416 if (MIXER_READ(SOUND_MIXER_LINE2) == cmd)
417 i = 1;
418 if (MIXER_READ(SOUND_MIXER_LINE3) == cmd)
419 i = 2;
420 ret = bta->gain[i] * 100 / 15;
421 ret |= ret << 8;
422 break;
424 default:
425 return -EINVAL;
427 if (put_user(ret, (int __user *)argp))
428 return -EFAULT;
429 return 0;
432 static const struct file_operations btaudio_mixer_fops = {
433 .owner = THIS_MODULE,
434 .llseek = no_llseek,
435 .open = btaudio_mixer_open,
436 .release = btaudio_mixer_release,
437 .ioctl = btaudio_mixer_ioctl,
440 /* -------------------------------------------------------------- */
442 static int btaudio_dsp_open(struct inode *inode, struct file *file,
443 struct btaudio *bta, int analog)
445 mutex_lock(&bta->lock);
446 if (bta->users)
447 goto busy;
448 bta->users++;
449 file->private_data = bta;
451 bta->analog = analog;
452 bta->dma_block = 0;
453 bta->read_offset = 0;
454 bta->read_count = 0;
455 bta->sampleshift = 0;
457 mutex_unlock(&bta->lock);
458 return 0;
460 busy:
461 mutex_unlock(&bta->lock);
462 return -EBUSY;
465 static int btaudio_dsp_open_digital(struct inode *inode, struct file *file)
467 int minor = iminor(inode);
468 struct btaudio *bta;
470 for (bta = btaudios; bta != NULL; bta = bta->next)
471 if (bta->dsp_digital == minor)
472 break;
473 if (NULL == bta)
474 return -ENODEV;
476 if (debug)
477 printk("btaudio: open digital dsp [%d]\n",minor);
478 return btaudio_dsp_open(inode,file,bta,0);
481 static int btaudio_dsp_open_analog(struct inode *inode, struct file *file)
483 int minor = iminor(inode);
484 struct btaudio *bta;
486 for (bta = btaudios; bta != NULL; bta = bta->next)
487 if (bta->dsp_analog == minor)
488 break;
489 if (NULL == bta)
490 return -ENODEV;
492 if (debug)
493 printk("btaudio: open analog dsp [%d]\n",minor);
494 return btaudio_dsp_open(inode,file,bta,1);
497 static int btaudio_dsp_release(struct inode *inode, struct file *file)
499 struct btaudio *bta = file->private_data;
501 mutex_lock(&bta->lock);
502 if (bta->recording)
503 stop_recording(bta);
504 bta->users--;
505 mutex_unlock(&bta->lock);
506 return 0;
509 static ssize_t btaudio_dsp_read(struct file *file, char __user *buffer,
510 size_t swcount, loff_t *ppos)
512 struct btaudio *bta = file->private_data;
513 int hwcount = swcount << bta->sampleshift;
514 int nsrc, ndst, err, ret = 0;
515 DECLARE_WAITQUEUE(wait, current);
517 add_wait_queue(&bta->readq, &wait);
518 mutex_lock(&bta->lock);
519 while (swcount > 0) {
520 if (0 == bta->read_count) {
521 if (!bta->recording) {
522 if (0 != (err = start_recording(bta))) {
523 if (0 == ret)
524 ret = err;
525 break;
528 if (file->f_flags & O_NONBLOCK) {
529 if (0 == ret)
530 ret = -EAGAIN;
531 break;
533 mutex_unlock(&bta->lock);
534 current->state = TASK_INTERRUPTIBLE;
535 schedule();
536 mutex_lock(&bta->lock);
537 if(signal_pending(current)) {
538 if (0 == ret)
539 ret = -EINTR;
540 break;
543 nsrc = (bta->read_count < hwcount) ? bta->read_count : hwcount;
544 if (nsrc > bta->buf_size - bta->read_offset)
545 nsrc = bta->buf_size - bta->read_offset;
546 ndst = nsrc >> bta->sampleshift;
548 if ((bta->analog && 0 == bta->sampleshift) ||
549 (!bta->analog && 2 == bta->channels)) {
550 /* just copy */
551 if (copy_to_user(buffer + ret, bta->buf_cpu + bta->read_offset, nsrc)) {
552 if (0 == ret)
553 ret = -EFAULT;
554 break;
557 } else if (!bta->analog) {
558 /* stereo => mono (digital audio) */
559 __s16 *src = (__s16*)(bta->buf_cpu + bta->read_offset);
560 __s16 __user *dst = (__s16 __user *)(buffer + ret);
561 __s16 avg;
562 int n = ndst>>1;
563 if (!access_ok(VERIFY_WRITE, dst, ndst)) {
564 if (0 == ret)
565 ret = -EFAULT;
566 break;
568 for (; n; n--, dst++) {
569 avg = (__s16)le16_to_cpu(*src) / 2; src++;
570 avg += (__s16)le16_to_cpu(*src) / 2; src++;
571 __put_user(cpu_to_le16(avg),dst);
574 } else if (8 == bta->bits) {
575 /* copy + byte downsampling (audio A/D) */
576 __u8 *src = bta->buf_cpu + bta->read_offset;
577 __u8 __user *dst = buffer + ret;
578 int n = ndst;
579 if (!access_ok(VERIFY_WRITE, dst, ndst)) {
580 if (0 == ret)
581 ret = -EFAULT;
582 break;
584 for (; n; n--, src += (1 << bta->sampleshift), dst++)
585 __put_user(*src, dst);
587 } else {
588 /* copy + word downsampling (audio A/D) */
589 __u16 *src = (__u16*)(bta->buf_cpu + bta->read_offset);
590 __u16 __user *dst = (__u16 __user *)(buffer + ret);
591 int n = ndst>>1;
592 if (!access_ok(VERIFY_WRITE,dst,ndst)) {
593 if (0 == ret)
594 ret = -EFAULT;
595 break;
597 for (; n; n--, src += (1 << bta->sampleshift), dst++)
598 __put_user(*src, dst);
601 ret += ndst;
602 swcount -= ndst;
603 hwcount -= nsrc;
604 bta->read_count -= nsrc;
605 bta->read_offset += nsrc;
606 if (bta->read_offset == bta->buf_size)
607 bta->read_offset = 0;
609 mutex_unlock(&bta->lock);
610 remove_wait_queue(&bta->readq, &wait);
611 current->state = TASK_RUNNING;
612 return ret;
615 static ssize_t btaudio_dsp_write(struct file *file, const char __user *buffer,
616 size_t count, loff_t *ppos)
618 return -EINVAL;
621 static int btaudio_dsp_ioctl(struct inode *inode, struct file *file,
622 unsigned int cmd, unsigned long arg)
624 struct btaudio *bta = file->private_data;
625 int s, i, ret, val = 0;
626 void __user *argp = (void __user *)arg;
627 int __user *p = argp;
629 switch (cmd) {
630 case OSS_GETVERSION:
631 return put_user(SOUND_VERSION, p);
632 case SNDCTL_DSP_GETCAPS:
633 return 0;
635 case SNDCTL_DSP_SPEED:
636 if (get_user(val, p))
637 return -EFAULT;
638 if (bta->analog) {
639 for (s = 0; s < 16; s++)
640 if (val << s >= HWBASE_AD*4/15)
641 break;
642 for (i = 15; i >= 5; i--)
643 if (val << s <= HWBASE_AD*4/i)
644 break;
645 bta->sampleshift = s;
646 bta->decimation = i;
647 if (debug)
648 printk(KERN_DEBUG "btaudio: rate: req=%d "
649 "dec=%d shift=%d hwrate=%d swrate=%d\n",
650 val,i,s,(HWBASE_AD*4/i),(HWBASE_AD*4/i)>>s);
651 } else {
652 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
653 bta->decimation = 0;
655 if (bta->recording) {
656 mutex_lock(&bta->lock);
657 stop_recording(bta);
658 start_recording(bta);
659 mutex_unlock(&bta->lock);
661 /* fall through */
662 case SOUND_PCM_READ_RATE:
663 if (bta->analog) {
664 return put_user(HWBASE_AD*4/bta->decimation>>bta->sampleshift, p);
665 } else {
666 return put_user(bta->rate, p);
669 case SNDCTL_DSP_STEREO:
670 if (!bta->analog) {
671 if (get_user(val, p))
672 return -EFAULT;
673 bta->channels = (val > 0) ? 2 : 1;
674 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
675 if (debug)
676 printk(KERN_INFO
677 "btaudio: stereo=%d channels=%d\n",
678 val,bta->channels);
679 } else {
680 if (val == 1)
681 return -EFAULT;
682 else {
683 bta->channels = 1;
684 if (debug)
685 printk(KERN_INFO
686 "btaudio: stereo=0 channels=1\n");
689 return put_user((bta->channels)-1, p);
691 case SNDCTL_DSP_CHANNELS:
692 if (!bta->analog) {
693 if (get_user(val, p))
694 return -EFAULT;
695 bta->channels = (val > 1) ? 2 : 1;
696 bta->sampleshift = (bta->channels == 2) ? 0 : 1;
697 if (debug)
698 printk(KERN_DEBUG
699 "btaudio: val=%d channels=%d\n",
700 val,bta->channels);
702 /* fall through */
703 case SOUND_PCM_READ_CHANNELS:
704 return put_user(bta->channels, p);
706 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
707 if (bta->analog)
708 return put_user(AFMT_S16_LE|AFMT_S8, p);
709 else
710 return put_user(AFMT_S16_LE, p);
712 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
713 if (get_user(val, p))
714 return -EFAULT;
715 if (val != AFMT_QUERY) {
716 if (bta->analog)
717 bta->bits = (val == AFMT_S8) ? 8 : 16;
718 else
719 bta->bits = 16;
720 if (bta->recording) {
721 mutex_lock(&bta->lock);
722 stop_recording(bta);
723 start_recording(bta);
724 mutex_unlock(&bta->lock);
727 if (debug)
728 printk(KERN_DEBUG "btaudio: fmt: bits=%d\n",bta->bits);
729 return put_user((bta->bits==16) ? AFMT_S16_LE : AFMT_S8,
731 break;
732 case SOUND_PCM_READ_BITS:
733 return put_user(bta->bits, p);
735 case SNDCTL_DSP_NONBLOCK:
736 file->f_flags |= O_NONBLOCK;
737 return 0;
739 case SNDCTL_DSP_RESET:
740 if (bta->recording) {
741 mutex_lock(&bta->lock);
742 stop_recording(bta);
743 mutex_unlock(&bta->lock);
745 return 0;
746 case SNDCTL_DSP_GETBLKSIZE:
747 if (!bta->recording) {
748 if (0 != (ret = alloc_buffer(bta)))
749 return ret;
750 if (0 != (ret = make_risc(bta)))
751 return ret;
753 return put_user(bta->block_bytes>>bta->sampleshift,p);
755 case SNDCTL_DSP_SYNC:
756 /* NOP */
757 return 0;
758 case SNDCTL_DSP_GETISPACE:
760 audio_buf_info info;
761 if (!bta->recording)
762 return -EINVAL;
763 info.fragsize = bta->block_bytes>>bta->sampleshift;
764 info.fragstotal = bta->block_count;
765 info.bytes = bta->read_count;
766 info.fragments = info.bytes / info.fragsize;
767 if (debug)
768 printk(KERN_DEBUG "btaudio: SNDCTL_DSP_GETISPACE "
769 "returns %d/%d/%d/%d\n",
770 info.fragsize, info.fragstotal,
771 info.bytes, info.fragments);
772 if (copy_to_user(argp, &info, sizeof(info)))
773 return -EFAULT;
774 return 0;
776 #if 0 /* TODO */
777 case SNDCTL_DSP_GETTRIGGER:
778 case SNDCTL_DSP_SETTRIGGER:
779 case SNDCTL_DSP_SETFRAGMENT:
780 #endif
781 default:
782 return -EINVAL;
786 static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait)
788 struct btaudio *bta = file->private_data;
789 unsigned int mask = 0;
791 poll_wait(file, &bta->readq, wait);
793 if (0 != bta->read_count)
794 mask |= (POLLIN | POLLRDNORM);
796 return mask;
799 static const struct file_operations btaudio_digital_dsp_fops = {
800 .owner = THIS_MODULE,
801 .llseek = no_llseek,
802 .open = btaudio_dsp_open_digital,
803 .release = btaudio_dsp_release,
804 .read = btaudio_dsp_read,
805 .write = btaudio_dsp_write,
806 .ioctl = btaudio_dsp_ioctl,
807 .poll = btaudio_dsp_poll,
810 static const struct file_operations btaudio_analog_dsp_fops = {
811 .owner = THIS_MODULE,
812 .llseek = no_llseek,
813 .open = btaudio_dsp_open_analog,
814 .release = btaudio_dsp_release,
815 .read = btaudio_dsp_read,
816 .write = btaudio_dsp_write,
817 .ioctl = btaudio_dsp_ioctl,
818 .poll = btaudio_dsp_poll,
821 /* -------------------------------------------------------------- */
823 static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "",
824 "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
825 "RIPERR", "PABORT", "OCERR", "SCERR" };
827 static irqreturn_t btaudio_irq(int irq, void *dev_id)
829 int count = 0;
830 u32 stat,astat;
831 struct btaudio *bta = dev_id;
832 int handled = 0;
834 for (;;) {
835 count++;
836 stat = btread(REG_INT_STAT);
837 astat = stat & btread(REG_INT_MASK);
838 if (!astat)
839 return IRQ_RETVAL(handled);
840 handled = 1;
841 btwrite(astat,REG_INT_STAT);
843 if (irq_debug) {
844 int i;
845 printk(KERN_DEBUG "btaudio: irq loop=%d risc=%x, bits:",
846 count, stat>>28);
847 for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) {
848 if (stat & (1 << i))
849 printk(" %s",irq_name[i]);
850 if (astat & (1 << i))
851 printk("*");
853 printk("\n");
855 if (stat & IRQ_RISCI) {
856 int blocks;
857 blocks = (stat >> 28) - bta->dma_block;
858 if (blocks < 0)
859 blocks += bta->block_count;
860 bta->dma_block = stat >> 28;
861 if (bta->read_count + 2*bta->block_bytes > bta->buf_size) {
862 stop_recording(bta);
863 printk(KERN_INFO "btaudio: buffer overrun\n");
865 if (blocks > 0) {
866 bta->read_count += blocks * bta->block_bytes;
867 wake_up_interruptible(&bta->readq);
870 if (count > 10) {
871 printk(KERN_WARNING
872 "btaudio: Oops - irq mask cleared\n");
873 btwrite(0, REG_INT_MASK);
876 return IRQ_NONE;
879 /* -------------------------------------------------------------- */
881 static unsigned int dsp1 = -1;
882 static unsigned int dsp2 = -1;
883 static unsigned int mixer = -1;
884 static int latency = -1;
885 static int digital = 1;
886 static int analog = 1;
887 static int rate;
889 #define BTA_OSPREY200 1
891 static struct cardinfo cards[] = {
892 [0] = {
893 .name = "default",
894 .rate = 32000,
896 [BTA_OSPREY200] = {
897 .name = "Osprey 200",
898 .rate = 44100,
902 static int __devinit btaudio_probe(struct pci_dev *pci_dev,
903 const struct pci_device_id *pci_id)
905 struct btaudio *bta;
906 struct cardinfo *card = &cards[pci_id->driver_data];
907 unsigned char revision,lat;
908 int rc = -EBUSY;
910 if (pci_enable_device(pci_dev))
911 return -EIO;
912 if (!request_mem_region(pci_resource_start(pci_dev,0),
913 pci_resource_len(pci_dev,0),
914 "btaudio")) {
915 return -EBUSY;
918 bta = kzalloc(sizeof(*bta),GFP_ATOMIC);
919 if (!bta) {
920 rc = -ENOMEM;
921 goto fail0;
924 bta->pci = pci_dev;
925 bta->irq = pci_dev->irq;
926 bta->mem = pci_resource_start(pci_dev,0);
927 bta->mmio = ioremap(pci_resource_start(pci_dev,0),
928 pci_resource_len(pci_dev,0));
930 bta->source = 1;
931 bta->bits = 8;
932 bta->channels = 1;
933 if (bta->analog) {
934 bta->decimation = 15;
935 } else {
936 bta->decimation = 0;
937 bta->sampleshift = 1;
940 /* sample rate */
941 bta->rate = card->rate;
942 if (rate)
943 bta->rate = rate;
945 mutex_init(&bta->lock);
946 init_waitqueue_head(&bta->readq);
948 if (-1 != latency) {
949 printk(KERN_INFO "btaudio: setting pci latency timer to %d\n",
950 latency);
951 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
953 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
954 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat);
955 printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ",
956 pci_dev->device,revision,pci_dev->bus->number,
957 PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn));
958 printk("irq: %d, latency: %d, mmio: 0x%lx\n",
959 bta->irq, lat, bta->mem);
960 printk("btaudio: using card config \"%s\"\n", card->name);
962 /* init hw */
963 btwrite(0, REG_GPIO_DMA_CTL);
964 btwrite(0, REG_INT_MASK);
965 btwrite(~0U, REG_INT_STAT);
966 pci_set_master(pci_dev);
968 if ((rc = request_irq(bta->irq, btaudio_irq, IRQF_SHARED|IRQF_DISABLED,
969 "btaudio",(void *)bta)) < 0) {
970 printk(KERN_WARNING
971 "btaudio: can't request irq (rc=%d)\n",rc);
972 goto fail1;
975 /* register devices */
976 if (digital) {
977 rc = bta->dsp_digital =
978 register_sound_dsp(&btaudio_digital_dsp_fops,dsp1);
979 if (rc < 0) {
980 printk(KERN_WARNING
981 "btaudio: can't register digital dsp (rc=%d)\n",rc);
982 goto fail2;
984 printk(KERN_INFO "btaudio: registered device dsp%d [digital]\n",
985 bta->dsp_digital >> 4);
987 if (analog) {
988 rc = bta->dsp_analog =
989 register_sound_dsp(&btaudio_analog_dsp_fops,dsp2);
990 if (rc < 0) {
991 printk(KERN_WARNING
992 "btaudio: can't register analog dsp (rc=%d)\n",rc);
993 goto fail3;
995 printk(KERN_INFO "btaudio: registered device dsp%d [analog]\n",
996 bta->dsp_analog >> 4);
997 rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer);
998 if (rc < 0) {
999 printk(KERN_WARNING
1000 "btaudio: can't register mixer (rc=%d)\n",rc);
1001 goto fail4;
1003 printk(KERN_INFO "btaudio: registered device mixer%d\n",
1004 bta->mixer_dev >> 4);
1007 /* hook into linked list */
1008 bta->next = btaudios;
1009 btaudios = bta;
1011 pci_set_drvdata(pci_dev,bta);
1012 return 0;
1014 fail4:
1015 unregister_sound_dsp(bta->dsp_analog);
1016 fail3:
1017 if (digital)
1018 unregister_sound_dsp(bta->dsp_digital);
1019 fail2:
1020 free_irq(bta->irq,bta);
1021 fail1:
1022 iounmap(bta->mmio);
1023 kfree(bta);
1024 fail0:
1025 release_mem_region(pci_resource_start(pci_dev,0),
1026 pci_resource_len(pci_dev,0));
1027 return rc;
1030 static void __devexit btaudio_remove(struct pci_dev *pci_dev)
1032 struct btaudio *bta = pci_get_drvdata(pci_dev);
1033 struct btaudio *walk;
1035 /* turn off all DMA / IRQs */
1036 btand(~15, REG_GPIO_DMA_CTL);
1037 btwrite(0, REG_INT_MASK);
1038 btwrite(~0U, REG_INT_STAT);
1040 /* unregister devices */
1041 if (digital) {
1042 unregister_sound_dsp(bta->dsp_digital);
1044 if (analog) {
1045 unregister_sound_dsp(bta->dsp_analog);
1046 unregister_sound_mixer(bta->mixer_dev);
1049 /* free resources */
1050 free_buffer(bta);
1051 free_irq(bta->irq,bta);
1052 release_mem_region(pci_resource_start(pci_dev,0),
1053 pci_resource_len(pci_dev,0));
1054 iounmap(bta->mmio);
1056 /* remove from linked list */
1057 if (bta == btaudios) {
1058 btaudios = NULL;
1059 } else {
1060 for (walk = btaudios; walk->next != bta; walk = walk->next)
1061 ; /* if (NULL == walk->next) BUG(); */
1062 walk->next = bta->next;
1065 pci_set_drvdata(pci_dev, NULL);
1066 kfree(bta);
1067 return;
1070 /* -------------------------------------------------------------- */
1072 static struct pci_device_id btaudio_pci_tbl[] = {
1074 .vendor = PCI_VENDOR_ID_BROOKTREE,
1075 .device = 0x0878,
1076 .subvendor = 0x0070,
1077 .subdevice = 0xff01,
1078 .driver_data = BTA_OSPREY200,
1080 .vendor = PCI_VENDOR_ID_BROOKTREE,
1081 .device = 0x0878,
1082 .subvendor = PCI_ANY_ID,
1083 .subdevice = PCI_ANY_ID,
1085 .vendor = PCI_VENDOR_ID_BROOKTREE,
1086 .device = 0x0878,
1087 .subvendor = PCI_ANY_ID,
1088 .subdevice = PCI_ANY_ID,
1090 /* --- end of list --- */
1094 static struct pci_driver btaudio_pci_driver = {
1095 .name = "btaudio",
1096 .id_table = btaudio_pci_tbl,
1097 .probe = btaudio_probe,
1098 .remove = __devexit_p(btaudio_remove),
1101 static int btaudio_init_module(void)
1103 printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n",
1104 digital ? "digital" : "",
1105 analog && digital ? "+" : "",
1106 analog ? "analog" : "");
1107 return pci_register_driver(&btaudio_pci_driver);
1110 static void btaudio_cleanup_module(void)
1112 pci_unregister_driver(&btaudio_pci_driver);
1113 return;
1116 module_init(btaudio_init_module);
1117 module_exit(btaudio_cleanup_module);
1119 module_param(dsp1, int, S_IRUGO);
1120 module_param(dsp2, int, S_IRUGO);
1121 module_param(mixer, int, S_IRUGO);
1122 module_param(debug, int, S_IRUGO | S_IWUSR);
1123 module_param(irq_debug, int, S_IRUGO | S_IWUSR);
1124 module_param(digital, int, S_IRUGO);
1125 module_param(analog, int, S_IRUGO);
1126 module_param(rate, int, S_IRUGO);
1127 module_param(latency, int, S_IRUGO);
1128 MODULE_PARM_DESC(latency,"pci latency timer");
1130 MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
1131 MODULE_DESCRIPTION("bt878 audio dma driver");
1132 MODULE_AUTHOR("Gerd Knorr");
1133 MODULE_LICENSE("GPL");
1136 * Local variables:
1137 * c-basic-offset: 8
1138 * End: