Merge http://gkernel.bkbits.net/linus-2.5
[opensuse:kernel.git] / sound / oss / btaudio.c
1 /*
2     btaudio - bt878 audio dma driver for linux 2.4.x
3
4     (c) 2000 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/version.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/pci.h>
26 #include <linux/sched.h>
27 #include <linux/signal.h>
28 #include <linux/types.h>
29 #include <linux/wrapper.h>
30 #include <linux/interrupt.h>
31 #include <linux/init.h>
32 #include <linux/poll.h>
33 #include <linux/sound.h>
34 #include <linux/soundcard.h>
35 #include <linux/slab.h>
36 #include <asm/uaccess.h>
37 #include <asm/io.h>
38
39
40 /* mmio access */
41 #define btwrite(dat,adr)    writel((dat), (bta->mmio+(adr)))
42 #define btread(adr)         readl(bta->mmio+(adr))
43
44 #define btand(dat,adr)      btwrite((dat) & btread(adr), adr)
45 #define btor(dat,adr)       btwrite((dat) | btread(adr), adr)
46 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
47
48 /* registers (shifted because bta->mmio is long) */
49 #define REG_INT_STAT      (0x100 >> 2)
50 #define REG_INT_MASK      (0x104 >> 2)
51 #define REG_GPIO_DMA_CTL  (0x10c >> 2)
52 #define REG_PACKET_LEN    (0x110 >> 2)
53 #define REG_RISC_STRT_ADD (0x114 >> 2)
54 #define REG_RISC_COUNT    (0x120 >> 2)
55
56 /* IRQ bits - REG_INT_(STAT|MASK) */
57 #define IRQ_SCERR         (1 << 19)
58 #define IRQ_OCERR         (1 << 18)
59 #define IRQ_PABORT        (1 << 17)
60 #define IRQ_RIPERR        (1 << 16)
61 #define IRQ_PPERR         (1 << 15)
62 #define IRQ_FDSR          (1 << 14)
63 #define IRQ_FTRGT         (1 << 13)
64 #define IRQ_FBUS          (1 << 12)
65 #define IRQ_RISCI         (1 << 11)
66 #define IRQ_OFLOW         (1 <<  3)
67
68 #define IRQ_BTAUDIO       (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\
69                            IRQ_PPERR | IRQ_FDSR  | IRQ_FTRGT  | IRQ_FBUS   |\
70                            IRQ_RISCI)
71
72 /* REG_GPIO_DMA_CTL bits */
73 #define DMA_CTL_A_PWRDN   (1 << 26)
74 #define DMA_CTL_DA_SBR    (1 << 14)
75 #define DMA_CTL_DA_ES2    (1 << 13)
76 #define DMA_CTL_ACAP_EN   (1 <<  4)
77 #define DMA_CTL_RISC_EN   (1 <<  1)
78 #define DMA_CTL_FIFO_EN   (1 <<  0)
79
80 /* RISC instructions */
81 #define RISC_WRITE        (0x01 << 28)
82 #define RISC_JUMP         (0x07 << 28)
83 #define RISC_SYNC         (0x08 << 28)
84
85 /* RISC bits */
86 #define RISC_WR_SOL       (1 << 27)
87 #define RISC_WR_EOL       (1 << 26)
88 #define RISC_IRQ          (1 << 24)
89 #define RISC_SYNC_RESYNC  (1 << 15)
90 #define RISC_SYNC_FM1     0x06
91 #define RISC_SYNC_VRO     0x0c
92
93 #define HWBASE_AD (448000)
94
95 /* -------------------------------------------------------------- */
96
97 struct btaudio {
98         /* linked list */
99         struct btaudio *next;
100
101         /* device info */
102         int            dsp_digital;
103         int            dsp_analog;
104         int            mixer_dev;
105         struct pci_dev *pci;
106         unsigned int   irq;
107         unsigned long  mem;
108         unsigned long  *mmio;
109
110         /* locking */
111         int            users;
112         struct semaphore lock;
113
114         /* risc instructions */
115         unsigned int   risc_size;
116         unsigned long  *risc_cpu;
117         dma_addr_t     risc_dma;
118
119         /* audio data */
120         unsigned int   buf_size;
121         unsigned char  *buf_cpu;
122         dma_addr_t     buf_dma;
123
124         /* buffer setup */
125         int line_bytes;
126         int line_count;
127         int block_bytes;
128         int block_count;
129
130         /* read fifo management */
131         int recording;
132         int dma_block;
133         int read_offset;
134         int read_count;
135         wait_queue_head_t readq;
136
137         /* settings */
138         int gain[3];
139         int source;
140         int bits;
141         int decimation;
142         int mixcount;
143         int sampleshift;
144         int channels;
145         int analog;
146 };
147
148 static struct btaudio *btaudios = NULL;
149 static unsigned int dsp1 = -1;
150 static unsigned int dsp2 = -1;
151 static unsigned int mixer = -1;
152 static unsigned int debug = 0;
153 static unsigned int irq_debug = 0;
154 static int digital = 1;
155 static int analog = 1;
156 static int rate = 32000;
157
158 /* -------------------------------------------------------------- */
159
160 #define BUF_DEFAULT 128*1024
161 #define BUF_MIN         8192
162
163 static int alloc_buffer(struct btaudio *bta)
164 {
165         if (NULL == bta->buf_cpu) {
166                 for (bta->buf_size = BUF_DEFAULT; bta->buf_size >= BUF_MIN;
167                      bta->buf_size = bta->buf_size >> 1) {
168                         bta->buf_cpu = pci_alloc_consistent
169                                 (bta->pci, bta->buf_size, &bta->buf_dma);
170                         if (NULL != bta->buf_cpu)
171                                 break;
172                 }
173                 if (NULL == bta->buf_cpu)
174                         return -ENOMEM;
175                 memset(bta->buf_cpu,0,bta->buf_size);
176         }
177         if (NULL == bta->risc_cpu) {
178                 bta->risc_size = PAGE_SIZE;
179                 bta->risc_cpu = pci_alloc_consistent
180                         (bta->pci, bta->risc_size, &bta->risc_dma);
181                 if (NULL == bta->risc_cpu)
182                         return -ENOMEM;
183         }
184         return 0;
185 }
186
187 static void free_buffer(struct btaudio *bta)
188 {
189         if (NULL != bta->buf_cpu) {
190                 pci_free_consistent(bta->pci, bta->buf_size,
191                                     bta->buf_cpu, bta->buf_dma);
192                 bta->buf_cpu = NULL;
193         }
194         if (NULL != bta->risc_cpu) {
195                 pci_free_consistent(bta->pci, bta->risc_size,
196                                     bta->risc_cpu, bta->risc_dma);
197                 bta->risc_cpu = NULL;
198         }
199 }
200
201 static int make_risc(struct btaudio *bta)
202 {
203         int rp, bp, line, block;
204         unsigned long risc;
205
206         bta->block_bytes = bta->buf_size >> 4;
207         bta->block_count = 1 << 4;
208         bta->line_bytes  = bta->block_bytes;
209         bta->line_count  = bta->block_count;
210         while (bta->line_bytes > 4095) {
211                 bta->line_bytes >>= 1;
212                 bta->line_count <<= 1;
213         }
214         if (bta->line_count > 255)
215                 return -EINVAL;
216         if (debug)
217                 printk(KERN_DEBUG
218                        "btaudio: bufsize=%d - bs=%d bc=%d - ls=%d, lc=%d\n",
219                        bta->buf_size,bta->block_bytes,bta->block_count,
220                        bta->line_bytes,bta->line_count);
221         rp = 0; bp = 0;
222         block = 0;
223         bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_FM1);
224         bta->risc_cpu[rp++] = cpu_to_le32(0);
225         for (line = 0; line < bta->line_count; line++) {
226                 risc  = RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL;
227                 risc |= bta->line_bytes;
228                 if (0 == (bp & (bta->block_bytes-1))) {
229                         risc |= RISC_IRQ;
230                         risc |= (block  & 0x0f) << 16;
231                         risc |= (~block & 0x0f) << 20;
232                         block++;
233                 }
234                 bta->risc_cpu[rp++] = cpu_to_le32(risc);
235                 bta->risc_cpu[rp++] = cpu_to_le32(bta->buf_dma + bp);
236                 bp += bta->line_bytes;
237         }
238         bta->risc_cpu[rp++] = cpu_to_le32(RISC_SYNC|RISC_SYNC_VRO);
239         bta->risc_cpu[rp++] = cpu_to_le32(0);
240         bta->risc_cpu[rp++] = cpu_to_le32(RISC_JUMP); 
241         bta->risc_cpu[rp++] = cpu_to_le32(bta->risc_dma);
242         return 0;
243 }
244
245 static int start_recording(struct btaudio *bta)
246 {
247         int ret;
248
249         if (0 != (ret = alloc_buffer(bta)))
250                 return ret;
251         if (0 != (ret = make_risc(bta)))
252                 return ret;
253
254         btwrite(bta->risc_dma, REG_RISC_STRT_ADD);
255         btwrite((bta->line_count << 16) | bta->line_bytes,
256                 REG_PACKET_LEN);
257         btwrite(IRQ_BTAUDIO, REG_INT_MASK);
258         if (bta->analog) {
259                 btwrite(DMA_CTL_ACAP_EN |
260                         DMA_CTL_RISC_EN |
261                         DMA_CTL_FIFO_EN |
262                         DMA_CTL_DA_ES2  |
263                         ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
264                         (bta->gain[bta->source] << 28) |
265                         (bta->source            << 24) |
266                         (bta->decimation        <<  8),
267                         REG_GPIO_DMA_CTL);
268         } else {
269                 btwrite(DMA_CTL_ACAP_EN |
270                         DMA_CTL_RISC_EN |
271                         DMA_CTL_FIFO_EN |
272                         DMA_CTL_DA_ES2  |
273                         DMA_CTL_A_PWRDN |
274                         (1 << 6)   |
275                         ((bta->bits == 8) ? DMA_CTL_DA_SBR : 0) |
276                         (bta->gain[bta->source] << 28) |
277                         (bta->source            << 24) |
278                         (bta->decimation        <<  8),
279                         REG_GPIO_DMA_CTL);
280         }
281         bta->dma_block = 0;
282         bta->read_offset = 0;
283         bta->read_count = 0;
284         bta->recording = 1;
285         if (debug)
286                 printk(KERN_DEBUG "btaudio: recording started\n");
287         return 0;
288 }
289
290 static void stop_recording(struct btaudio *bta)
291 {
292         btand(~15, REG_GPIO_DMA_CTL);
293         bta->recording = 0;
294         if (debug)
295                 printk(KERN_DEBUG "btaudio: recording stopped\n");
296 }
297
298
299 /* -------------------------------------------------------------- */
300
301 static int btaudio_mixer_open(struct inode *inode, struct file *file)
302 {
303         int minor = minor(inode->i_rdev);
304         struct btaudio *bta;
305
306         for (bta = btaudios; bta != NULL; bta = bta->next)
307                 if (bta->mixer_dev == minor)
308                         break;
309         if (NULL == bta)
310                 return -ENODEV;
311
312         if (debug)
313                 printk("btaudio: open mixer [%d]\n",minor);
314         file->private_data = bta;
315         return 0;
316 }
317
318 static int btaudio_mixer_release(struct inode *inode, struct file *file)
319 {
320         return 0;
321 }
322
323 static int btaudio_mixer_ioctl(struct inode *inode, struct file *file,
324                                unsigned int cmd, unsigned long arg)
325 {
326         struct btaudio *bta = file->private_data;
327         int ret,val=0,i=0;
328
329         if (cmd == SOUND_MIXER_INFO) {
330                 mixer_info info;
331                 memset(&info,0,sizeof(info));
332                 strncpy(info.id,"bt878",sizeof(info.id)-1);
333                 strncpy(info.name,"Brooktree Bt878 audio",sizeof(info.name)-1);
334                 info.modify_counter = bta->mixcount;
335                 if (copy_to_user((void *)arg, &info, sizeof(info)))
336                         return -EFAULT;
337                 return 0;
338         }
339         if (cmd == SOUND_OLD_MIXER_INFO) {
340                 _old_mixer_info info;
341                 memset(&info,0,sizeof(info));
342                 strncpy(info.id,"bt878",sizeof(info.id)-1);
343                 strncpy(info.name,"Brooktree Bt878 audio",sizeof(info.name)-1);
344                 if (copy_to_user((void *)arg, &info, sizeof(info)))
345                         return -EFAULT;
346                 return 0;
347         }
348         if (cmd == OSS_GETVERSION)
349                 return put_user(SOUND_VERSION, (int *)arg);
350
351         /* read */
352         if (_SIOC_DIR(cmd) & _SIOC_WRITE)
353                 if (get_user(val, (int *)arg))
354                         return -EFAULT;
355
356         switch (cmd) {
357         case MIXER_READ(SOUND_MIXER_CAPS):
358                 ret = SOUND_CAP_EXCL_INPUT;
359                 break;
360         case MIXER_READ(SOUND_MIXER_STEREODEVS):
361                 ret = 0;
362                 break;
363         case MIXER_READ(SOUND_MIXER_RECMASK):
364         case MIXER_READ(SOUND_MIXER_DEVMASK):
365                 ret = SOUND_MASK_LINE1|SOUND_MASK_LINE2|SOUND_MASK_LINE3;
366                 break;
367
368         case MIXER_WRITE(SOUND_MIXER_RECSRC):
369                 if (val & SOUND_MASK_LINE1 && bta->source != 0)
370                         bta->source = 0;
371                 else if (val & SOUND_MASK_LINE2 && bta->source != 1)
372                         bta->source = 1;
373                 else if (val & SOUND_MASK_LINE3 && bta->source != 2)
374                         bta->source = 2;
375                 btaor((bta->gain[bta->source] << 28) |
376                       (bta->source            << 24),
377                       0x0cffffff, REG_GPIO_DMA_CTL);
378         case MIXER_READ(SOUND_MIXER_RECSRC):
379                 switch (bta->source) {
380                 case 0:  ret = SOUND_MASK_LINE1; break;
381                 case 1:  ret = SOUND_MASK_LINE2; break;
382                 case 2:  ret = SOUND_MASK_LINE3; break;
383                 default: ret = 0;
384                 }
385                 break;
386
387         case MIXER_WRITE(SOUND_MIXER_LINE1):
388         case MIXER_WRITE(SOUND_MIXER_LINE2):
389         case MIXER_WRITE(SOUND_MIXER_LINE3):
390                 if (MIXER_WRITE(SOUND_MIXER_LINE1) == cmd)
391                         i = 0;
392                 if (MIXER_WRITE(SOUND_MIXER_LINE2) == cmd)
393                         i = 1;
394                 if (MIXER_WRITE(SOUND_MIXER_LINE3) == cmd)
395                         i = 2;
396                 bta->gain[i] = (val & 0xff) * 15 / 100;
397                 if (bta->gain[i] > 15) bta->gain[i] = 15;
398                 if (bta->gain[i] <  0) bta->gain[i] =  0;
399                 if (i == bta->source)
400                         btaor((bta->gain[bta->source]<<28),
401                               0x0fffffff, REG_GPIO_DMA_CTL);
402                 ret  = bta->gain[i] * 100 / 15;
403                 ret |= ret << 8;
404                 break;
405
406         case MIXER_READ(SOUND_MIXER_LINE1):
407         case MIXER_READ(SOUND_MIXER_LINE2):
408         case MIXER_READ(SOUND_MIXER_LINE3):
409                 if (MIXER_READ(SOUND_MIXER_LINE1) == cmd)
410                         i = 0;
411                 if (MIXER_READ(SOUND_MIXER_LINE2) == cmd)
412                         i = 1;
413                 if (MIXER_READ(SOUND_MIXER_LINE3) == cmd)
414                         i = 2;
415                 ret  = bta->gain[i] * 100 / 15;
416                 ret |= ret << 8;
417                 break;
418
419         default:
420                 return -EINVAL;
421         }
422         if (put_user(ret, (int *)arg))
423                 return -EFAULT;
424         return 0;
425 }
426
427 static struct file_operations btaudio_mixer_fops = {
428         owner:   THIS_MODULE,
429         llseek:  no_llseek,
430         open:    btaudio_mixer_open,
431         release: btaudio_mixer_release,
432         ioctl:   btaudio_mixer_ioctl,
433 };
434
435 /* -------------------------------------------------------------- */
436
437 static int btaudio_dsp_open(struct inode *inode, struct file *file,
438                             struct btaudio *bta, int analog)
439 {
440         down(&bta->lock);
441         if (bta->users)
442                 goto busy;
443         bta->users++;
444         file->private_data = bta;
445
446         bta->analog = analog;
447         bta->dma_block = 0;
448         bta->read_offset = 0;
449         bta->read_count = 0;
450         bta->sampleshift = 0;
451
452         up(&bta->lock);
453         return 0;
454
455  busy:
456         up(&bta->lock);
457         return -EBUSY;
458 }
459
460 static int btaudio_dsp_open_digital(struct inode *inode, struct file *file)
461 {
462         int minor = minor(inode->i_rdev);
463         struct btaudio *bta;
464
465         for (bta = btaudios; bta != NULL; bta = bta->next)
466                 if (bta->dsp_digital == minor)
467                         break;
468         if (NULL == bta)
469                 return -ENODEV;
470         
471         if (debug)
472                 printk("btaudio: open digital dsp [%d]\n",minor);
473         return btaudio_dsp_open(inode,file,bta,0);
474 }
475
476 static int btaudio_dsp_open_analog(struct inode *inode, struct file *file)
477 {
478         int minor = minor(inode->i_rdev);
479         struct btaudio *bta;
480
481         for (bta = btaudios; bta != NULL; bta = bta->next)
482                 if (bta->dsp_analog == minor)
483                         break;
484         if (NULL == bta)
485                 return -ENODEV;
486
487         if (debug)
488                 printk("btaudio: open analog dsp [%d]\n",minor);
489         return btaudio_dsp_open(inode,file,bta,1);
490 }
491
492 static int btaudio_dsp_release(struct inode *inode, struct file *file)
493 {
494         struct btaudio *bta = file->private_data;
495
496         down(&bta->lock);
497         if (bta->recording)
498                 stop_recording(bta);
499         bta->users--;
500         up(&bta->lock);
501         return 0;
502 }
503
504 static ssize_t btaudio_dsp_read(struct file *file, char *buffer,
505                                 size_t swcount, loff_t *ppos)
506 {
507         struct btaudio *bta = file->private_data;
508         int hwcount = swcount << bta->sampleshift;
509         int nsrc, ndst, err, ret = 0;
510         DECLARE_WAITQUEUE(wait, current);
511
512         add_wait_queue(&bta->readq, &wait);
513         down(&bta->lock);
514         while (swcount > 0) {
515                 if (0 == bta->read_count) {
516                         if (!bta->recording) {
517                                 if (0 != (err = start_recording(bta))) {
518                                         if (0 == ret)
519                                                 ret = err;
520                                         break;
521                                 }
522                         }
523                         if (file->f_flags & O_NONBLOCK) {
524                                 if (0 == ret)
525                                         ret = -EAGAIN;
526                                 break;
527                         }
528                         up(&bta->lock);
529                         current->state = TASK_INTERRUPTIBLE;
530                         schedule();
531                         down(&bta->lock);
532                         if(signal_pending(current)) {
533                                 if (0 == ret)
534                                         ret = -EINTR;
535                                 break;
536                         }
537                 }
538                 nsrc = (bta->read_count < hwcount) ? bta->read_count : hwcount;
539                 if (nsrc > bta->buf_size - bta->read_offset)
540                         nsrc = bta->buf_size - bta->read_offset;
541                 ndst = nsrc >> bta->sampleshift;
542                 
543                 if ((bta->analog  && 0 == bta->sampleshift) ||
544                     (!bta->analog && 2 == bta->channels)) {
545                         /* just copy */
546                         if (copy_to_user(buffer + ret, bta->buf_cpu + bta->read_offset, nsrc)) {
547                                 if (0 == ret)
548                                         ret = -EFAULT;
549                                 break;
550                         }
551
552                 } else if (!bta->analog) {
553                         /* stereo => mono (digital audio) */
554                         __s16 *src = (__s16*)(bta->buf_cpu + bta->read_offset);
555                         __s16 *dst = (__s16*)(buffer + ret);
556                         __s16 avg;
557                         int n = ndst>>1;
558                         if (0 != verify_area(VERIFY_WRITE,dst,ndst)) {
559                                 if (0 == ret)
560                                         ret = -EFAULT;
561                                 break;
562                         }
563                         for (; n; n--, dst++) {
564                                 avg  = (__s16)le16_to_cpu(*src) / 2; src++;
565                                 avg += (__s16)le16_to_cpu(*src) / 2; src++;
566                                 __put_user(cpu_to_le16(avg),(__u16*)(dst));
567                         }
568
569                 } else if (8 == bta->bits) {
570                         /* copy + byte downsampling (audio A/D) */
571                         __u8 *src = bta->buf_cpu + bta->read_offset;
572                         __u8 *dst = buffer + ret;
573                         int n = ndst;
574                         if (0 != verify_area(VERIFY_WRITE,dst,ndst)) {
575                                 if (0 == ret)
576                                         ret = -EFAULT;
577                                 break;
578                         }
579                         for (; n; n--, src += (1 << bta->sampleshift), dst++)
580                                 __put_user(*src,(__u8*)(dst));
581
582                 } else {
583                         /* copy + word downsampling (audio A/D) */
584                         __u16 *src = (__u16*)(bta->buf_cpu + bta->read_offset);
585                         __u16 *dst = (__u16*)(buffer + ret);
586                         int n = ndst>>1;
587                         if (0 != verify_area(VERIFY_WRITE,dst,ndst)) {
588                                 if (0 == ret)
589                                         ret = -EFAULT;
590                                 break;
591                         }
592                         for (; n; n--, src += (1 << bta->sampleshift), dst++)
593                                 __put_user(*src,(__u16*)(dst));
594                 }
595
596                 ret     += ndst;
597                 swcount -= ndst;
598                 hwcount -= nsrc;
599                 bta->read_count  -= nsrc;
600                 bta->read_offset += nsrc;
601                 if (bta->read_offset == bta->buf_size)
602                         bta->read_offset = 0;
603         }
604         up(&bta->lock);
605         remove_wait_queue(&bta->readq, &wait);
606         current->state = TASK_RUNNING;
607         return ret;
608 }
609
610 static ssize_t btaudio_dsp_write(struct file *file, const char *buffer,
611                                  size_t count, loff_t *ppos)
612 {
613         return -EINVAL;
614 }
615
616 static int btaudio_dsp_ioctl(struct inode *inode, struct file *file,
617                              unsigned int cmd, unsigned long arg)
618 {
619         struct btaudio *bta = file->private_data;
620         int s, i, ret, val = 0;
621         
622         switch (cmd) {
623         case OSS_GETVERSION:
624                 return put_user(SOUND_VERSION, (int *)arg);
625         case SNDCTL_DSP_GETCAPS:
626                 return 0;
627
628         case SNDCTL_DSP_SPEED:
629                 if (get_user(val, (int*)arg))
630                         return -EFAULT;
631                 if (bta->analog) {
632                         for (s = 0; s < 16; s++)
633                                 if (val << s >= HWBASE_AD*4/15)
634                                         break;
635                         for (i = 15; i >= 5; i--)
636                                 if (val << s <= HWBASE_AD*4/i)
637                                         break;
638                         bta->sampleshift = s;
639                         bta->decimation  = i;
640                         if (debug)
641                                 printk(KERN_DEBUG "btaudio: rate: req=%d  "
642                                        "dec=%d shift=%d hwrate=%d swrate=%d\n",
643                                        val,i,s,(HWBASE_AD*4/i),(HWBASE_AD*4/i)>>s);
644                 } else {
645                         bta->sampleshift = (bta->channels == 2) ? 0 : 1;
646                         bta->decimation  = 0;
647                 }
648                 if (bta->recording) {
649                         down(&bta->lock);
650                         stop_recording(bta);
651                         start_recording(bta);
652                         up(&bta->lock);
653                 }
654                 /* fall through */
655         case SOUND_PCM_READ_RATE:
656                 if (bta->analog) {
657                         return put_user(HWBASE_AD*4/bta->decimation>>bta->sampleshift, (int*)arg);
658                 } else {
659                         return put_user(rate, (int*)arg);
660                 }
661
662         case SNDCTL_DSP_STEREO:
663                 if (!bta->analog) {
664                         if (get_user(val, (int*)arg))
665                                 return -EFAULT;
666                         bta->channels    = (val > 0) ? 2 : 1;
667                         bta->sampleshift = (bta->channels == 2) ? 0 : 1;
668                         if (debug)
669                                 printk(KERN_INFO
670                                        "btaudio: stereo=%d channels=%d\n",
671                                        val,bta->channels);
672                 } else {
673                         if (val == 1)
674                                 return -EFAULT;
675                         else {
676                                 bta->channels = 1;
677                                 if (debug)
678                                         printk(KERN_INFO
679                                                "btaudio: stereo=0 channels=1\n");
680                         }
681                 }
682                 return put_user((bta->channels)-1, (int *)arg);
683
684         case SNDCTL_DSP_CHANNELS:
685                 if (!analog) {
686                         if (get_user(val, (int*)arg))
687                                 return -EFAULT;
688                         bta->channels    = (val > 1) ? 2 : 1;
689                         bta->sampleshift = (bta->channels == 2) ? 0 : 1;
690                         if (debug)
691                                 printk(KERN_DEBUG
692                                        "btaudio: val=%d channels=%d\n",
693                                        val,bta->channels);
694                 }
695                 /* fall through */
696         case SOUND_PCM_READ_CHANNELS:
697                 return put_user(bta->channels, (int *)arg);
698                 
699         case SNDCTL_DSP_GETFMTS: /* Returns a mask */
700                 if (analog)
701                         return put_user(AFMT_S16_LE|AFMT_S8, (int*)arg);
702                 else
703                         return put_user(AFMT_S16_LE, (int*)arg);
704
705         case SNDCTL_DSP_SETFMT: /* Selects ONE fmt*/
706                 if (get_user(val, (int*)arg))
707                         return -EFAULT;
708                 if (val != AFMT_QUERY) {
709                         if (analog)
710                                 bta->bits = (val == AFMT_S8) ? 8 : 16;
711                         else
712                                 bta->bits = 16;
713                         if (bta->recording) {
714                                 down(&bta->lock);
715                                 stop_recording(bta);
716                                 start_recording(bta);
717                                 up(&bta->lock);
718                         }
719                 }
720                 if (debug)
721                         printk(KERN_DEBUG "btaudio: fmt: bits=%d\n",bta->bits);
722                 return put_user((bta->bits==16) ? AFMT_S16_LE : AFMT_S8,
723                                 (int*)arg);
724                 break;
725         case SOUND_PCM_READ_BITS:
726                 return put_user(bta->bits, (int*)arg);
727
728         case SNDCTL_DSP_NONBLOCK:
729                 file->f_flags |= O_NONBLOCK;
730                 return 0;
731
732         case SNDCTL_DSP_RESET:
733                 if (bta->recording) {
734                         down(&bta->lock);
735                         stop_recording(bta);
736                         up(&bta->lock);
737                 }
738                 return 0;
739         case SNDCTL_DSP_GETBLKSIZE:
740                 if (!bta->recording) {
741                         if (0 != (ret = alloc_buffer(bta)))
742                                 return ret;
743                         if (0 != (ret = make_risc(bta)))
744                                 return ret;
745                 }
746                 return put_user(bta->block_bytes>>bta->sampleshift,(int*)arg);
747
748         case SNDCTL_DSP_SYNC:
749                 /* NOP */
750                 return 0;
751         case SNDCTL_DSP_GETISPACE:
752         {
753                 audio_buf_info info;
754                 if (!bta->recording)
755                         return -EINVAL;
756                 info.fragsize = bta->block_bytes>>bta->sampleshift;
757                 info.fragstotal = bta->block_count;
758                 info.bytes = bta->read_count;
759                 info.fragments = info.bytes / info.fragsize;
760                 if (debug)
761                         printk(KERN_DEBUG "btaudio: SNDCTL_DSP_GETISPACE "
762                                "returns %d/%d/%d/%d\n",
763                                info.fragsize, info.fragstotal,
764                                info.bytes, info.fragments);
765                 if (copy_to_user((void *)arg, &info, sizeof(info)))
766                         return -EFAULT;
767                 return 0;
768         }
769 #if 0 /* TODO */
770         case SNDCTL_DSP_GETTRIGGER:
771         case SNDCTL_DSP_SETTRIGGER:
772         case SNDCTL_DSP_SETFRAGMENT:
773 #endif
774         default:
775                 return -EINVAL;
776         }
777 }
778
779 static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait)
780 {
781         struct btaudio *bta = file->private_data;
782         unsigned int mask = 0;
783
784         poll_wait(file, &bta->readq, wait);
785
786         if (0 == bta->read_count)
787                 mask |= (POLLIN | POLLRDNORM);
788
789         return mask;
790 }
791
792 static struct file_operations btaudio_digital_dsp_fops = {
793         owner:   THIS_MODULE,
794         llseek:  no_llseek,
795         open:    btaudio_dsp_open_digital,
796         release: btaudio_dsp_release,
797         read:    btaudio_dsp_read,
798         write:   btaudio_dsp_write,
799         ioctl:   btaudio_dsp_ioctl,
800         poll:    btaudio_dsp_poll,
801 };
802
803 static struct file_operations btaudio_analog_dsp_fops = {
804         owner:   THIS_MODULE,
805         llseek:  no_llseek,
806         open:    btaudio_dsp_open_analog,
807         release: btaudio_dsp_release,
808         read:    btaudio_dsp_read,
809         write:   btaudio_dsp_write,
810         ioctl:   btaudio_dsp_ioctl,
811         poll:    btaudio_dsp_poll,
812 };
813
814 /* -------------------------------------------------------------- */
815
816 static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "",
817                             "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
818                             "RIPERR", "PABORT", "OCERR", "SCERR" };
819
820 static void btaudio_irq(int irq, void *dev_id, struct pt_regs * regs)
821 {
822         int count = 0;
823         u32 stat,astat;
824         struct btaudio *bta = dev_id;
825
826         for (;;) {
827                 count++;
828                 stat  = btread(REG_INT_STAT);
829                 astat = stat & btread(REG_INT_MASK);
830                 if (!astat)
831                         return;
832                 btwrite(astat,REG_INT_STAT);
833
834                 if (irq_debug) {
835                         int i;
836                         printk(KERN_DEBUG "btaudio: irq loop=%d risc=%x, bits:",
837                                count, stat>>28);
838                         for (i = 0; i < (sizeof(irq_name)/sizeof(char*)); i++) {
839                                 if (stat & (1 << i))
840                                         printk(" %s",irq_name[i]);
841                                 if (astat & (1 << i))
842                                         printk("*");
843                         }
844                         printk("\n");
845                 }
846                 if (stat & IRQ_RISCI) {
847                         int blocks;
848                         blocks = (stat >> 28) - bta->dma_block;
849                         if (blocks < 0)
850                                 blocks += bta->block_count;
851                         bta->dma_block = stat >> 28;
852                         if (bta->read_count + 2*bta->block_bytes > bta->buf_size) {
853                                 stop_recording(bta);
854                                 printk(KERN_INFO "btaudio: buffer overrun\n");
855                         }
856                         if (blocks > 0) {
857                                 bta->read_count += blocks * bta->block_bytes;
858                                 wake_up_interruptible(&bta->readq);
859                         }
860                 }
861                 if (count > 10) {
862                         printk(KERN_WARNING
863                                "btaudio: Oops - irq mask cleared\n");
864                         btwrite(0, REG_INT_MASK);
865                 }
866         }
867         return;
868 }
869
870 /* -------------------------------------------------------------- */
871
872 static int __devinit btaudio_probe(struct pci_dev *pci_dev,
873                                    const struct pci_device_id *pci_id)
874 {
875         struct btaudio *bta;
876         unsigned char revision,latency;
877         int rc = -EBUSY;
878
879         if (pci_enable_device(pci_dev))
880                 return -EIO;
881         if (!request_mem_region(pci_resource_start(pci_dev,0),
882                                 pci_resource_len(pci_dev,0),
883                                 "btaudio")) {
884                 return -EBUSY;
885         }
886
887         bta = kmalloc(sizeof(*bta),GFP_ATOMIC);
888         memset(bta,0,sizeof(*bta));
889
890         bta->pci  = pci_dev;
891         bta->irq  = pci_dev->irq;
892         bta->mem  = pci_resource_start(pci_dev,0);
893         bta->mmio = ioremap(pci_resource_start(pci_dev,0),
894                             pci_resource_len(pci_dev,0));
895
896         bta->source     = 1;
897         bta->bits       = 8;
898         bta->channels   = 1;
899         if (bta->analog) {
900                 bta->decimation  = 15;
901         } else {
902                 bta->decimation  = 0;
903                 bta->sampleshift = 1;
904         }
905
906         init_MUTEX(&bta->lock);
907         init_waitqueue_head(&bta->readq);
908
909         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
910         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &latency);
911         printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ",
912                pci_dev->device,revision,pci_dev->bus->number,
913                PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn));
914         printk("irq: %d, latency: %d, memory: 0x%lx\n",
915                bta->irq, latency, bta->mem);
916
917         /* init hw */
918         btwrite(0, REG_GPIO_DMA_CTL);
919         btwrite(0, REG_INT_MASK);
920         btwrite(~0x0UL, REG_INT_STAT);
921         pci_set_master(pci_dev);
922
923         if ((rc = request_irq(bta->irq, btaudio_irq, SA_SHIRQ|SA_INTERRUPT,
924                                "btaudio",(void *)bta)) < 0) {
925                 printk(KERN_WARNING
926                        "btaudio: can't request irq (rc=%d)\n",rc);
927                 goto fail1;
928         }
929
930         /* register devices */
931         if (digital) {
932                 rc = bta->dsp_digital =
933                         register_sound_dsp(&btaudio_digital_dsp_fops,dsp1);
934                 if (rc < 0) {
935                         printk(KERN_WARNING
936                                "btaudio: can't register digital dsp (rc=%d)\n",rc);
937                         goto fail2;
938                 }
939         }
940         if (analog) {
941                 rc = bta->dsp_analog =
942                         register_sound_dsp(&btaudio_analog_dsp_fops,dsp2);
943                 if (rc < 0) {
944                         printk(KERN_WARNING
945                                "btaudio: can't register analog dsp (rc=%d)\n",rc);
946                         goto fail3;
947                 }
948                 rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer);
949                 if (rc < 0) {
950                         printk(KERN_WARNING
951                                "btaudio: can't register mixer (rc=%d)\n",rc);
952                         goto fail4;
953                 }
954         }
955         if (debug)
956                 printk(KERN_DEBUG "btaudio: minors: digital=%d, analog=%d, mixer=%d\n",
957                        bta->dsp_digital, bta->dsp_analog, bta->mixer_dev);
958
959         /* hook into linked list */
960         bta->next = btaudios;
961         btaudios = bta;
962
963         pci_set_drvdata(pci_dev,bta);
964         return 0;
965
966  fail4:
967         unregister_sound_dsp(bta->dsp_analog);
968  fail3:
969         if (digital)
970                 unregister_sound_dsp(bta->dsp_digital);
971  fail2:
972         free_irq(bta->irq,bta); 
973  fail1:
974         release_mem_region(pci_resource_start(pci_dev,0),
975                            pci_resource_len(pci_dev,0));
976         kfree(bta);
977         return rc;
978 }
979
980 static void __devexit btaudio_remove(struct pci_dev *pci_dev)
981 {
982         struct btaudio *bta = pci_get_drvdata(pci_dev);
983         struct btaudio *walk;
984
985         /* turn off all DMA / IRQs */
986         btand(~15, REG_GPIO_DMA_CTL);
987         btwrite(0, REG_INT_MASK);
988         btwrite(~0x0UL, REG_INT_STAT);
989
990         /* unregister devices */
991         if (digital) {
992                 unregister_sound_dsp(bta->dsp_digital);
993         }
994         if (analog) {
995                 unregister_sound_dsp(bta->dsp_analog);
996                 unregister_sound_mixer(bta->mixer_dev);
997         }
998
999         /* free resources */
1000         free_buffer(bta);
1001         free_irq(bta->irq,bta);
1002         release_mem_region(pci_resource_start(pci_dev,0),
1003                            pci_resource_len(pci_dev,0));
1004
1005         /* remove from linked list */
1006         if (bta == btaudios) {
1007                 btaudios = NULL;
1008         } else {
1009                 for (walk = btaudios; walk->next != bta; walk = walk->next)
1010                         ; /* if (NULL == walk->next) BUG(); */
1011                 walk->next = bta->next;
1012         }
1013
1014         pci_set_drvdata(pci_dev, NULL);
1015         kfree(bta);
1016         return;
1017 }
1018
1019 /* -------------------------------------------------------------- */
1020
1021 static struct pci_device_id btaudio_pci_tbl[] __devinitdata = {
1022         { PCI_VENDOR_ID_BROOKTREE, 0x0878,
1023           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1024         { PCI_VENDOR_ID_BROOKTREE, 0x0879,
1025           PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1026         {0,}
1027 };
1028
1029 static struct pci_driver btaudio_pci_driver = {
1030         name:     "btaudio",
1031         id_table: btaudio_pci_tbl,
1032         probe:    btaudio_probe,
1033         remove:   __devexit_p(btaudio_remove),
1034 };
1035
1036 int btaudio_init_module(void)
1037 {
1038         printk(KERN_INFO "btaudio: driver version 0.6 loaded [%s%s%s]\n",
1039                analog ? "analog" : "",
1040                analog && digital ? "+" : "",
1041                digital ? "digital" : "");
1042         return pci_module_init(&btaudio_pci_driver);
1043 }
1044
1045 void btaudio_cleanup_module(void)
1046 {
1047         pci_unregister_driver(&btaudio_pci_driver);
1048         return;
1049 }
1050
1051 module_init(btaudio_init_module);
1052 module_exit(btaudio_cleanup_module);
1053
1054 MODULE_PARM(dsp1,"i");
1055 MODULE_PARM(dsp2,"i");
1056 MODULE_PARM(mixer,"i");
1057 MODULE_PARM(debug,"i");
1058 MODULE_PARM(irq_debug,"i");
1059 MODULE_PARM(digital,"i");
1060 MODULE_PARM(analog,"i");
1061 MODULE_PARM(rate,"i");
1062
1063 MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
1064 MODULE_DESCRIPTION("bt878 audio dma driver");
1065 MODULE_AUTHOR("Gerd Knorr");
1066 MODULE_LICENSE("GPL");
1067
1068 /*
1069  * Local variables:
1070  * c-basic-offset: 8
1071  * End:
1072  */