ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / oss / btaudio.c
1 /*
2     btaudio - bt878 audio dma driver for linux 2.4.x
3
4     (c) 2000-2002 Gerd Knorr <kraxel@bytesex.org>
5
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.
10
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.
15
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.
19
20 */
21
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>
37
38
39 /* mmio access */
40 #define btwrite(dat,adr)    writel((dat), (bta->mmio+(adr)))
41 #define btread(adr)         readl(bta->mmio+(adr))
42
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)
46
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)
54
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)
66
67 #define IRQ_BTAUDIO       (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\
68                            IRQ_PPERR | IRQ_FDSR  | IRQ_FTRGT  | IRQ_FBUS   |\
69                            IRQ_RISCI)
70
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)
78
79 /* RISC instructions */
80 #define RISC_WRITE        (0x01 << 28)
81 #define RISC_JUMP         (0x07 << 28)
82 #define RISC_SYNC         (0x08 << 28)
83
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
91
92 #define HWBASE_AD (448000)
93
94 /* -------------------------------------------------------------- */
95
96 struct btaudio {
97         /* linked list */
98         struct btaudio *next;
99
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  *mmio;
108
109         /* locking */
110         int            users;
111         struct semaphore lock;
112
113         /* risc instructions */
114         unsigned int   risc_size;
115         unsigned long  *risc_cpu;
116         dma_addr_t     risc_dma;
117
118         /* audio data */
119         unsigned int   buf_size;
120         unsigned char  *buf_cpu;
121         dma_addr_t     buf_dma;
122
123         /* buffer setup */
124         int line_bytes;
125         int line_count;
126         int block_bytes;
127         int block_count;
128
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;
135
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;
146 };
147
148 struct cardinfo {
149         char *name;
150         int rate;
151 };
152
153 static struct btaudio *btaudios;
154 static unsigned int debug;
155 static unsigned int irq_debug;
156
157 /* -------------------------------------------------------------- */
158
159 #define BUF_DEFAULT 128*1024
160 #define BUF_MIN         8192
161
162 static int alloc_buffer(struct btaudio *bta)
163 {
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;
171                 }
172                 if (NULL == bta->buf_cpu)
173                         return -ENOMEM;
174                 memset(bta->buf_cpu,0,bta->buf_size);
175         }
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;
184                 }
185         }
186         return 0;
187 }
188
189 static void free_buffer(struct btaudio *bta)
190 {
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;
195         }
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;
200         }
201 }
202
203 static int make_risc(struct btaudio *bta)
204 {
205         int rp, bp, line, block;
206         unsigned long risc;
207
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;
215         }
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++;
235                 }
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;
239         }
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;
245 }
246
247 static int start_recording(struct btaudio *bta)
248 {
249         int ret;
250
251         if (0 != (ret = alloc_buffer(bta)))
252                 return ret;
253         if (0 != (ret = make_risc(bta)))
254                 return ret;
255
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);
282         }
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;
290 }
291
292 static void stop_recording(struct btaudio *bta)
293 {
294         btand(~15, REG_GPIO_DMA_CTL);
295         bta->recording = 0;
296         if (debug)
297                 printk(KERN_DEBUG "btaudio: recording stopped\n");
298 }
299
300
301 /* -------------------------------------------------------------- */
302
303 static int btaudio_mixer_open(struct inode *inode, struct file *file)
304 {
305         int minor = iminor(inode);
306         struct btaudio *bta;
307
308         for (bta = btaudios; bta != NULL; bta = bta->next)
309                 if (bta->mixer_dev == minor)
310                         break;
311         if (NULL == bta)
312                 return -ENODEV;
313
314         if (debug)
315                 printk("btaudio: open mixer [%d]\n",minor);
316         file->private_data = bta;
317         return 0;
318 }
319
320 static int btaudio_mixer_release(struct inode *inode, struct file *file)
321 {
322         return 0;
323 }
324
325 static int btaudio_mixer_ioctl(struct inode *inode, struct file *file,
326                                unsigned int cmd, unsigned long arg)
327 {
328         struct btaudio *bta = file->private_data;
329         int ret,val=0,i=0;
330
331         if (cmd == SOUND_MIXER_INFO) {
332                 mixer_info info;
333                 memset(&info,0,sizeof(info));
334                 strlcpy(info.id,"bt878",sizeof(info.id));
335                 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
336                 info.modify_counter = bta->mixcount;
337                 if (copy_to_user((void *)arg, &info, sizeof(info)))
338                         return -EFAULT;
339                 return 0;
340         }
341         if (cmd == SOUND_OLD_MIXER_INFO) {
342                 _old_mixer_info info;
343                 memset(&info,0,sizeof(info));
344                 strlcpy(info.id,"bt878",sizeof(info.id)-1);
345                 strlcpy(info.name,"Brooktree Bt878 audio",sizeof(info.name));
346                 if (copy_to_user((void *)arg, &info, sizeof(info)))
347                         return -EFAULT;
348                 return 0;
349         }
350         if (cmd == OSS_GETVERSION)
351                 return put_user(SOUND_VERSION, (int *)arg);
352
353         /* read */
354         if (_SIOC_DIR(cmd) & _SIOC_WRITE)
355                 if (get_user(val, (int *)arg))
356                         return -EFAULT;
357
358         switch (cmd) {
359         case MIXER_READ(SOUND_MIXER_CAPS):
360                 ret = SOUND_CAP_EXCL_INPUT;
361                 break;
362         case MIXER_READ(SOUND_MIXER_STEREODEVS):
363                 ret = 0;
364                 break;
365         case MIXER_READ(SOUND_MIXER_RECMASK):
366         case MIXER_READ(SOUND_MIXER_DEVMASK):
367                 ret = SOUND_MASK_LINE1|SOUND_MASK_LINE2|SOUND_MASK_LINE3;
368                 break;
369
370         case MIXER_WRITE(SOUND_MIXER_RECSRC):
371                 if (val & SOUND_MASK_LINE1 && bta->source != 0)
372                         bta->source = 0;
373                 else if (val & SOUND_MASK_LINE2 && bta->source != 1)
374                         bta->source = 1;
375                 else if (val & SOUND_MASK_LINE3 && bta->source != 2)
376                         bta->source = 2;
377                 btaor((bta->gain[bta->source] << 28) |
378                       (bta->source            << 24),
379                       0x0cffffff, REG_GPIO_DMA_CTL);
380         case MIXER_READ(SOUND_MIXER_RECSRC):
381                 switch (bta->source) {
382                 case 0:  ret = SOUND_MASK_LINE1; break;
383                 case 1:  ret = SOUND_MASK_LINE2; break;
384                 case 2:  ret = SOUND_MASK_LINE3; break;
385                 default: ret = 0;
386                 }
387                 break;
388
389         case MIXER_WRITE(SOUND_MIXER_LINE1):
390         case MIXER_WRITE(SOUND_MIXER_LINE2):
391         case MIXER_WRITE(SOUND_MIXER_LINE3):
392                 if (MIXER_WRITE(SOUND_MIXER_LINE1) == cmd)
393                         i = 0;
394                 if (MIXER_WRITE(SOUND_MIXER_LINE2) == cmd)
395                         i = 1;
396                 if (MIXER_WRITE(SOUND_MIXER_LINE3) == cmd)
397                         i = 2;
398                 bta->gain[i] = (val & 0xff) * 15 / 100;
399                 if (bta->gain[i] > 15) bta->gain[i] = 15;
400                 if (bta->gain[i] <  0) bta->gain[i] =  0;
401                 if (i == bta->source)
402                         btaor((bta->gain[bta->source]<<28),
403                               0x0fffffff, REG_GPIO_DMA_CTL);
404                 ret  = bta->gain[i] * 100 / 15;
405                 ret |= ret << 8;
406                 break;
407
408         case MIXER_READ(SOUND_MIXER_LINE1):
409         case MIXER_READ(SOUND_MIXER_LINE2):
410         case MIXER_READ(SOUND_MIXER_LINE3):
411                 if (MIXER_READ(SOUND_MIXER_LINE1) == cmd)
412                         i = 0;
413                 if (MIXER_READ(SOUND_MIXER_LINE2) == cmd)
414                         i = 1;
415                 if (MIXER_READ(SOUND_MIXER_LINE3) == cmd)
416                         i = 2;
417                 ret  = bta->gain[i] * 100 / 15;
418                 ret |= ret << 8;
419                 break;
420
421         default:
422                 return -EINVAL;
423         }
424         if (put_user(ret, (int *)arg))
425                 return -EFAULT;
426         return 0;
427 }
428
429 static struct file_operations btaudio_mixer_fops = {
430         .owner          = THIS_MODULE,
431         .llseek         = no_llseek,
432         .open           = btaudio_mixer_open,
433         .release        = btaudio_mixer_release,
434         .ioctl          = btaudio_mixer_ioctl,
435 };
436
437 /* -------------------------------------------------------------- */
438
439 static int btaudio_dsp_open(struct inode *inode, struct file *file,
440                             struct btaudio *bta, int analog)
441 {
442         down(&bta->lock);
443         if (bta->users)
444                 goto busy;
445         bta->users++;
446         file->private_data = bta;
447
448         bta->analog = analog;
449         bta->dma_block = 0;
450         bta->read_offset = 0;
451         bta->read_count = 0;
452         bta->sampleshift = 0;
453
454         up(&bta->lock);
455         return 0;
456
457  busy:
458         up(&bta->lock);
459         return -EBUSY;
460 }
461
462 static int btaudio_dsp_open_digital(struct inode *inode, struct file *file)
463 {
464         int minor = iminor(inode);
465         struct btaudio *bta;
466
467         for (bta = btaudios; bta != NULL; bta = bta->next)
468                 if (bta->dsp_digital == minor)
469                         break;
470         if (NULL == bta)
471                 return -ENODEV;
472         
473         if (debug)
474                 printk("btaudio: open digital dsp [%d]\n",minor);
475         return btaudio_dsp_open(inode,file,bta,0);
476 }
477
478 static int btaudio_dsp_open_analog(struct inode *inode, struct file *file)
479 {
480         int minor = iminor(inode);
481         struct btaudio *bta;
482
483         for (bta = btaudios; bta != NULL; bta = bta->next)
484                 if (bta->dsp_analog == minor)
485                         break;
486         if (NULL == bta)
487                 return -ENODEV;
488
489         if (debug)
490                 printk("btaudio: open analog dsp [%d]\n",minor);
491         return btaudio_dsp_open(inode,file,bta,1);
492 }
493
494 static int btaudio_dsp_release(struct inode *inode, struct file *file)
495 {
496         struct btaudio *bta = file->private_data;
497
498         down(&bta->lock);
499         if (bta->recording)
500                 stop_recording(bta);
501         bta->users--;
502         up(&bta->lock);
503         return 0;
504 }
505
506 static ssize_t btaudio_dsp_read(struct file *file, char *buffer,
507                                 size_t swcount, loff_t *ppos)
508 {
509         struct btaudio *bta = file->private_data;
510         int hwcount = swcount << bta->sampleshift;
511         int nsrc, ndst, err, ret = 0;
512         DECLARE_WAITQUEUE(wait, current);
513
514         add_wait_queue(&bta->readq, &wait);
515         down(&bta->lock);
516         while (swcount > 0) {
517                 if (0 == bta->read_count) {
518                         if (!bta->recording) {
519                                 if (0 != (err = start_recording(bta))) {
520                                         if (0 == ret)
521                                                 ret = err;
522                                         break;
523                                 }
524                         }
525                         if (file->f_flags & O_NONBLOCK) {
526                                 if (0 == ret)
527                                         ret = -EAGAIN;
528                                 break;
529                         }
530                         up(&bta->lock);
531                         current->state = TASK_INTERRUPTIBLE;
532                         schedule();
533                         down(&bta->lock);
534                         if(signal_pending(current)) {
535                                 if (0 == ret)
536                                         ret = -EINTR;
537                                 break;
538                         }
539                 }
540                 nsrc = (bta->read_count < hwcount) ? bta->read_count : hwcount;
541                 if (nsrc > bta->buf_size - bta->read_offset)
542                         nsrc = bta->buf_size - bta->read_offset;
543                 ndst = nsrc >> bta->sampleshift;
544                 
545                 if ((bta->analog  && 0 == bta->sampleshift) ||
546                     (!bta->analog && 2 == bta->channels)) {
547                         /* just copy */
548                         if (copy_to_user(buffer + ret, bta->buf_cpu + bta->read_offset, nsrc)) {
549                                 if (0 == ret)
550                                         ret = -EFAULT;
551                                 break;
552                         }
553
554                 } else if (!bta->analog) {
555                         /* stereo => mono (digital audio) */
556                         __s16 *src = (__s16*)(bta->buf_cpu + bta->read_offset);
557                         __s16 *dst = (__s16*)(buffer + ret);
558                         __s16 avg;
559                         int n = ndst>>1;
560                         if (0 != verify_area(VERIFY_WRITE,dst,ndst)) {
561                                 if (0 == ret)
562                                         ret = -EFAULT;
563                                 break;
564                         }
565                         for (; n; n--, dst++) {
566                                 avg  = (__s16)le16_to_cpu(*src) / 2; src++;
567                                 avg += (__s16)le16_to_cpu(*src) / 2; src++;
568                                 __put_user(cpu_to_le16(avg),(__u16*)(dst));
569                         }
570
571                 } else if (8 == bta->bits) {
572                         /* copy + byte downsampling (audio A/D) */
573                         __u8 *src = bta->buf_cpu + bta->read_offset;
574                         __u8 *dst = buffer + ret;
575                         int n = ndst;
576                         if (0 != verify_area(VERIFY_WRITE,dst,ndst)) {
577                                 if (0 == ret)
578                                         ret = -EFAULT;
579                                 break;
580                         }
581                         for (; n; n--, src += (1 << bta->sampleshift), dst++)
582                                 __put_user(*src,(__u8*)(dst));
583
584                 } else {
585                         /* copy + word downsampling (audio A/D) */
586                         __u16 *src = (__u16*)(bta->buf_cpu + bta->read_offset);
587                         __u16 *dst = (__u16*)(buffer + ret);
588                         int n = ndst>>1;
589                         if (0 != verify_area(VERIFY_WRITE,dst,ndst)) {
590                                 if (0 == ret)
591                                         ret = -EFAULT;
592                                 break;
593                         }
594                         for (; n; n--, src += (1 << bta->sampleshift), dst++)
595                                 __put_user(*src,(__u16*)(dst));
596                 }
597
598                 ret     += ndst;
599                 swcount -= ndst;
600                 hwcount -= nsrc;
601                 bta->read_count  -= nsrc;
602                 bta->read_offset += nsrc;
603                 if (bta->read_offset == bta->buf_size)
604                         bta->read_offset = 0;
605         }
606         up(&bta->lock);
607         remove_wait_queue(&bta->readq, &wait);
608         current->state = TASK_RUNNING;
609         return ret;
610 }
611
612 static ssize_t btaudio_dsp_write(struct file *file, const char *buffer,
613                                  size_t count, loff_t *ppos)
614 {
615         return -EINVAL;
616 }
617
618 static int btaudio_dsp_ioctl(struct inode *inode, struct file *file,
619                              unsigned int cmd, unsigned long arg)
620 {
621         struct btaudio *bta = file->private_data;
622         int s, i, ret, val = 0;
623         
624         switch (cmd) {
625         case OSS_GETVERSION:
626                 return put_user(SOUND_VERSION, (int *)arg);
627         case SNDCTL_DSP_GETCAPS:
628                 return 0;
629
630         case SNDCTL_DSP_SPEED:
631                 if (get_user(val, (int*)arg))
632                         return -EFAULT;
633                 if (bta->analog) {
634                         for (s = 0; s < 16; s++)
635                                 if (val << s >= HWBASE_AD*4/15)
636                                         break;
637                         for (i = 15; i >= 5; i--)
638                                 if (val << s <= HWBASE_AD*4/i)
639                                         break;
640                         bta->sampleshift = s;
641                         bta->decimation  = i;
642                         if (debug)
643                                 printk(KERN_DEBUG "btaudio: rate: req=%d  "
644                                        "dec=%d shift=%d hwrate=%d swrate=%d\n",
645                                        val,i,s,(HWBASE_AD*4/i),(HWBASE_AD*4/i)>>s);
646                 } else {
647                         bta->sampleshift = (bta->channels == 2) ? 0 : 1;
648                         bta->decimation  = 0;
649                 }
650                 if (bta->recording) {
651                         down(&bta->lock);
652                         stop_recording(bta);
653                         start_recording(bta);
654                         up(&bta->lock);
655                 }
656                 /* fall through */
657         case SOUND_PCM_READ_RATE:
658                 if (bta->analog) {
659                         return put_user(HWBASE_AD*4/bta->decimation>>bta->sampleshift, (int*)arg);
660                 } else {
661                         return put_user(bta->rate, (int*)arg);
662                 }
663
664         case SNDCTL_DSP_STEREO:
665                 if (!bta->analog) {
666                         if (get_user(val, (int*)arg))
667                                 return -EFAULT;
668                         bta->channels    = (val > 0) ? 2 : 1;
669                         bta->sampleshift = (bta->channels == 2) ? 0 : 1;
670                         if (debug)
671                                 printk(KERN_INFO
672                                        "btaudio: stereo=%d channels=%d\n",
673                                        val,bta->channels);
674                 } else {
675                         if (val == 1)
676                                 return -EFAULT;
677                         else {
678                                 bta->channels = 1;
679                                 if (debug)
680                                         printk(KERN_INFO
681                                                "btaudio: stereo=0 channels=1\n");
682                         }
683                 }
684                 return put_user((bta->channels)-1, (int *)arg);
685
686         case SNDCTL_DSP_CHANNELS:
687                 if (!bta->analog) {
688                         if (get_user(val, (int*)arg))
689                                 return -EFAULT;
690                         bta->channels    = (val > 1) ? 2 : 1;
691                         bta->sampleshift = (bta->channels == 2) ? 0 : 1;
692                         if (debug)
693                                 printk(KERN_DEBUG
694                                        "btaudio: val=%d channels=%d\n",
695                                        val,bta->channels);
696                 }
697                 /* fall through */
698         case SOUND_PCM_READ_CHANNELS:
699                 return put_user(bta->channels, (int *)arg);
700                 
701         case SNDCTL_DSP_GETFMTS: /* Returns a mask */
702                 if (bta->analog)
703                         return put_user(AFMT_S16_LE|AFMT_S8, (int*)arg);
704                 else
705                         return put_user(AFMT_S16_LE, (int*)arg);
706
707         case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
708                 if (get_user(val, (int*)arg))
709                         return -EFAULT;
710                 if (val != AFMT_QUERY) {
711                         if (bta->analog)
712                                 bta->bits = (val == AFMT_S8) ? 8 : 16;
713                         else
714                                 bta->bits = 16;
715                         if (bta->recording) {
716                                 down(&bta->lock);
717                                 stop_recording(bta);
718                                 start_recording(bta);
719                                 up(&bta->lock);
720                         }
721                 }
722                 if (debug)
723                         printk(KERN_DEBUG "btaudio: fmt: bits=%d\n",bta->bits);
724                 return put_user((bta->bits==16) ? AFMT_S16_LE : AFMT_S8,
725                                 (int*)arg);
726                 break;
727         case SOUND_PCM_READ_BITS:
728                 return put_user(bta->bits, (int*)arg);
729
730         case SNDCTL_DSP_NONBLOCK:
731                 file->f_flags |= O_NONBLOCK;
732                 return 0;
733
734         case SNDCTL_DSP_RESET:
735                 if (bta->recording) {
736                         down(&bta->lock);
737                         stop_recording(bta);
738                         up(&bta->lock);
739                 }
740                 return 0;
741         case SNDCTL_DSP_GETBLKSIZE:
742                 if (!bta->recording) {
743                         if (0 != (ret = alloc_buffer(bta)))
744                                 return ret;
745                         if (0 != (ret = make_risc(bta)))
746                                 return ret;
747                 }
748                 return put_user(bta->block_bytes>>bta->sampleshift,(int*)arg);
749
750         case SNDCTL_DSP_SYNC:
751                 /* NOP */
752                 return 0;
753         case SNDCTL_DSP_GETISPACE:
754         {
755                 audio_buf_info info;
756                 if (!bta->recording)
757                         return -EINVAL;
758                 info.fragsize = bta->block_bytes>>bta->sampleshift;
759                 info.fragstotal = bta->block_count;
760                 info.bytes = bta->read_count;
761                 info.fragments = info.bytes / info.fragsize;
762                 if (debug)
763                         printk(KERN_DEBUG "btaudio: SNDCTL_DSP_GETISPACE "
764                                "returns %d/%d/%d/%d\n",
765                                info.fragsize, info.fragstotal,
766                                info.bytes, info.fragments);
767                 if (copy_to_user((void *)arg, &info, sizeof(info)))
768                         return -EFAULT;
769                 return 0;
770         }
771 #if 0 /* TODO */
772         case SNDCTL_DSP_GETTRIGGER:
773         case SNDCTL_DSP_SETTRIGGER:
774         case SNDCTL_DSP_SETFRAGMENT:
775 #endif
776         default:
777                 return -EINVAL;
778         }
779 }
780
781 static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait)
782 {
783         struct btaudio *bta = file->private_data;
784         unsigned int mask = 0;
785
786         poll_wait(file, &bta->readq, wait);
787
788         if (0 != bta->read_count)
789                 mask |= (POLLIN | POLLRDNORM);
790
791         return mask;
792 }
793
794 static struct file_operations btaudio_digital_dsp_fops = {
795         .owner          = THIS_MODULE,
796         .llseek         = no_llseek,
797         .open           = btaudio_dsp_open_digital,
798         .release        = btaudio_dsp_release,
799         .read           = btaudio_dsp_read,
800         .write          = btaudio_dsp_write,
801         .ioctl          = btaudio_dsp_ioctl,
802         .poll           = btaudio_dsp_poll,
803 };
804
805 static struct file_operations btaudio_analog_dsp_fops = {
806         .owner          = THIS_MODULE,
807         .llseek         = no_llseek,
808         .open           = btaudio_dsp_open_analog,
809         .release        = btaudio_dsp_release,
810         .read           = btaudio_dsp_read,
811         .write          = btaudio_dsp_write,
812         .ioctl          = btaudio_dsp_ioctl,
813         .poll           = btaudio_dsp_poll,
814 };
815
816 /* -------------------------------------------------------------- */
817
818 static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "",
819                             "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
820                             "RIPERR", "PABORT", "OCERR", "SCERR" };
821
822 static irqreturn_t btaudio_irq(int irq, void *dev_id, struct pt_regs * regs)
823 {
824         int count = 0;
825         u32 stat,astat;
826         struct btaudio *bta = dev_id;
827         int handled = 0;
828
829         for (;;) {
830                 count++;
831                 stat  = btread(REG_INT_STAT);
832                 astat = stat & btread(REG_INT_MASK);
833                 if (!astat)
834                         return IRQ_RETVAL(handled);
835                 handled = 1;
836                 btwrite(astat,REG_INT_STAT);
837
838                 if (irq_debug) {
839                         int i;
840                         printk(KERN_DEBUG "btaudio: irq loop=%d risc=%x, bits:",
841                                count, stat>>28);
842                         for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) {
843                                 if (stat & (1 << i))
844                                         printk(" %s",irq_name[i]);
845                                 if (astat & (1 << i))
846                                         printk("*");
847                         }
848                         printk("\n");
849                 }
850                 if (stat & IRQ_RISCI) {
851                         int blocks;
852                         blocks = (stat >> 28) - bta->dma_block;
853                         if (blocks < 0)
854                                 blocks += bta->block_count;
855                         bta->dma_block = stat >> 28;
856                         if (bta->read_count + 2*bta->block_bytes > bta->buf_size) {
857                                 stop_recording(bta);
858                                 printk(KERN_INFO "btaudio: buffer overrun\n");
859                         }
860                         if (blocks > 0) {
861                                 bta->read_count += blocks * bta->block_bytes;
862                                 wake_up_interruptible(&bta->readq);
863                         }
864                 }
865                 if (count > 10) {
866                         printk(KERN_WARNING
867                                "btaudio: Oops - irq mask cleared\n");
868                         btwrite(0, REG_INT_MASK);
869                 }
870         }
871         return IRQ_NONE;
872 }
873
874 /* -------------------------------------------------------------- */
875
876 static unsigned int dsp1 = -1;
877 static unsigned int dsp2 = -1;
878 static unsigned int mixer = -1;
879 static int latency = -1;
880 static int digital = 1;
881 static int analog = 1;
882 static int rate;
883
884 #define BTA_OSPREY200 1
885
886 static struct cardinfo cards[] = {
887         [0] = {
888                 .name   = "default",
889                 .rate   = 32000,
890         },
891         [BTA_OSPREY200] = {
892                 .name   = "Osprey 200",
893                 .rate   = 44100,
894         },
895 };
896
897 static int __devinit btaudio_probe(struct pci_dev *pci_dev,
898                                    const struct pci_device_id *pci_id)
899 {
900         struct btaudio *bta;
901         struct cardinfo *card = &cards[pci_id->driver_data];
902         unsigned char revision,lat;
903         int rc = -EBUSY;
904
905         if (pci_enable_device(pci_dev))
906                 return -EIO;
907         if (!request_mem_region(pci_resource_start(pci_dev,0),
908                                 pci_resource_len(pci_dev,0),
909                                 "btaudio")) {
910                 return -EBUSY;
911         }
912
913         bta = kmalloc(sizeof(*bta),GFP_ATOMIC);
914         if (!bta) {
915                 rc = -ENOMEM;
916                 goto fail0;
917         }
918         memset(bta,0,sizeof(*bta));
919
920         bta->pci  = pci_dev;
921         bta->irq  = pci_dev->irq;
922         bta->mem  = pci_resource_start(pci_dev,0);
923         bta->mmio = ioremap(pci_resource_start(pci_dev,0),
924                             pci_resource_len(pci_dev,0));
925
926         bta->source     = 1;
927         bta->bits       = 8;
928         bta->channels   = 1;
929         if (bta->analog) {
930                 bta->decimation  = 15;
931         } else {
932                 bta->decimation  = 0;
933                 bta->sampleshift = 1;
934         }
935
936         /* sample rate */
937         bta->rate = card->rate;
938         if (rate)
939                 bta->rate = rate;
940         
941         init_MUTEX(&bta->lock);
942         init_waitqueue_head(&bta->readq);
943
944         if (-1 != latency) {
945                 printk(KERN_INFO "btaudio: setting pci latency timer to %d\n",
946                        latency);
947                 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
948         }
949         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
950         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat);
951         printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ",
952                pci_dev->device,revision,pci_dev->bus->number,
953                PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn));
954         printk("irq: %d, latency: %d, mmio: 0x%lx\n",
955                bta->irq, lat, bta->mem);
956         printk("btaudio: using card config \"%s\"\n", card->name);
957
958         /* init hw */
959         btwrite(0, REG_GPIO_DMA_CTL);
960         btwrite(0, REG_INT_MASK);
961         btwrite(~0x0UL, REG_INT_STAT);
962         pci_set_master(pci_dev);
963
964         if ((rc = request_irq(bta->irq, btaudio_irq, SA_SHIRQ|SA_INTERRUPT,
965                               "btaudio",(void *)bta)) < 0) {
966                 printk(KERN_WARNING
967                        "btaudio: can't request irq (rc=%d)\n",rc);
968                 goto fail1;
969         }
970
971         /* register devices */
972         if (digital) {
973                 rc = bta->dsp_digital =
974                         register_sound_dsp(&btaudio_digital_dsp_fops,dsp1);
975                 if (rc < 0) {
976                         printk(KERN_WARNING
977                                "btaudio: can't register digital dsp (rc=%d)\n",rc);
978                         goto fail2;
979                 }
980                 printk(KERN_INFO "btaudio: registered device dsp%d [digital]\n",
981                        bta->dsp_digital >> 4);
982         }
983         if (analog) {
984                 rc = bta->dsp_analog =
985                         register_sound_dsp(&btaudio_analog_dsp_fops,dsp2);
986                 if (rc < 0) {
987                         printk(KERN_WARNING
988                                "btaudio: can't register analog dsp (rc=%d)\n",rc);
989                         goto fail3;
990                 }
991                 printk(KERN_INFO "btaudio: registered device dsp%d [analog]\n",
992                        bta->dsp_analog >> 4);
993                 rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer);
994                 if (rc < 0) {
995                         printk(KERN_WARNING
996                                "btaudio: can't register mixer (rc=%d)\n",rc);
997                         goto fail4;
998                 }
999                 printk(KERN_INFO "btaudio: registered device mixer%d\n",
1000                        bta->mixer_dev >> 4);
1001         }
1002
1003         /* hook into linked list */
1004         bta->next = btaudios;
1005         btaudios = bta;
1006
1007         pci_set_drvdata(pci_dev,bta);
1008         return 0;
1009
1010  fail4:
1011         unregister_sound_dsp(bta->dsp_analog);
1012  fail3:
1013         if (digital)
1014                 unregister_sound_dsp(bta->dsp_digital);
1015  fail2:
1016         free_irq(bta->irq,bta); 
1017  fail1:
1018         kfree(bta);
1019  fail0:
1020         release_mem_region(pci_resource_start(pci_dev,0),
1021                            pci_resource_len(pci_dev,0));
1022         return rc;
1023 }
1024
1025 static void __devexit btaudio_remove(struct pci_dev *pci_dev)
1026 {
1027         struct btaudio *bta = pci_get_drvdata(pci_dev);
1028         struct btaudio *walk;
1029
1030         /* turn off all DMA / IRQs */
1031         btand(~15, REG_GPIO_DMA_CTL);
1032         btwrite(0, REG_INT_MASK);
1033         btwrite(~0x0UL, REG_INT_STAT);
1034
1035         /* unregister devices */
1036         if (digital) {
1037                 unregister_sound_dsp(bta->dsp_digital);
1038         }
1039         if (analog) {
1040                 unregister_sound_dsp(bta->dsp_analog);
1041                 unregister_sound_mixer(bta->mixer_dev);
1042         }
1043
1044         /* free resources */
1045         free_buffer(bta);
1046         free_irq(bta->irq,bta);
1047         release_mem_region(pci_resource_start(pci_dev,0),
1048                            pci_resource_len(pci_dev,0));
1049
1050         /* remove from linked list */
1051         if (bta == btaudios) {
1052                 btaudios = NULL;
1053         } else {
1054                 for (walk = btaudios; walk->next != bta; walk = walk->next)
1055                         ; /* if (NULL == walk->next) BUG(); */
1056                 walk->next = bta->next;
1057         }
1058
1059         pci_set_drvdata(pci_dev, NULL);
1060         kfree(bta);
1061         return;
1062 }
1063
1064 /* -------------------------------------------------------------- */
1065
1066 static struct pci_device_id btaudio_pci_tbl[] = {
1067         {
1068                 .vendor         = PCI_VENDOR_ID_BROOKTREE,
1069                 .device         = 0x0878,
1070                 .subvendor      = 0x0070,
1071                 .subdevice      = 0xff01,
1072                 .driver_data    = BTA_OSPREY200,
1073         },{
1074                 .vendor         = PCI_VENDOR_ID_BROOKTREE,
1075                 .device         = 0x0878,
1076                 .subvendor      = PCI_ANY_ID,
1077                 .subdevice      = PCI_ANY_ID,
1078         },{
1079                 .vendor         = PCI_VENDOR_ID_BROOKTREE,
1080                 .device         = 0x0878,
1081                 .subvendor      = PCI_ANY_ID,
1082                 .subdevice      = PCI_ANY_ID,
1083         },{
1084                 /* --- end of list --- */
1085         }
1086 };
1087
1088 static struct pci_driver btaudio_pci_driver = {
1089         .name           = "btaudio",
1090         .id_table       = btaudio_pci_tbl,
1091         .probe          = btaudio_probe,
1092         .remove         =  __devexit_p(btaudio_remove),
1093 };
1094
1095 static int btaudio_init_module(void)
1096 {
1097         printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n",
1098                digital ? "digital" : "",
1099                analog && digital ? "+" : "",
1100                analog ? "analog" : "");
1101         return pci_module_init(&btaudio_pci_driver);
1102 }
1103
1104 static void btaudio_cleanup_module(void)
1105 {
1106         pci_unregister_driver(&btaudio_pci_driver);
1107         return;
1108 }
1109
1110 module_init(btaudio_init_module);
1111 module_exit(btaudio_cleanup_module);
1112
1113 MODULE_PARM(dsp1,"i");
1114 MODULE_PARM(dsp2,"i");
1115 MODULE_PARM(mixer,"i");
1116 MODULE_PARM(debug,"i");
1117 MODULE_PARM(irq_debug,"i");
1118 MODULE_PARM(digital,"i");
1119 MODULE_PARM(analog,"i");
1120 MODULE_PARM(rate,"i");
1121 MODULE_PARM(latency,"i");
1122 MODULE_PARM_DESC(latency,"pci latency timer");
1123
1124 MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
1125 MODULE_DESCRIPTION("bt878 audio dma driver");
1126 MODULE_AUTHOR("Gerd Knorr");
1127 MODULE_LICENSE("GPL");
1128
1129 /*
1130  * Local variables:
1131  * c-basic-offset: 8
1132  * End:
1133  */