UCC Ether driver: kmalloc casting cleanups
[sfrench/cifs-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 <linux/mutex.h>
36
37 #include <asm/uaccess.h>
38 #include <asm/io.h>
39
40
41 /* mmio access */
42 #define btwrite(dat,adr)    writel((dat), (bta->mmio+(adr)))
43 #define btread(adr)         readl(bta->mmio+(adr))
44
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)
48
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)
56
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)
68
69 #define IRQ_BTAUDIO       (IRQ_SCERR | IRQ_OCERR | IRQ_PABORT | IRQ_RIPERR |\
70                            IRQ_PPERR | IRQ_FDSR  | IRQ_FTRGT  | IRQ_FBUS   |\
71                            IRQ_RISCI)
72
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)
80
81 /* RISC instructions */
82 #define RISC_WRITE        (0x01 << 28)
83 #define RISC_JUMP         (0x07 << 28)
84 #define RISC_SYNC         (0x08 << 28)
85
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
93
94 #define HWBASE_AD (448000)
95
96 /* -------------------------------------------------------------- */
97
98 struct btaudio {
99         /* linked list */
100         struct btaudio *next;
101
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;
110
111         /* locking */
112         int            users;
113         struct mutex lock;
114
115         /* risc instructions */
116         unsigned int   risc_size;
117         unsigned long  *risc_cpu;
118         dma_addr_t     risc_dma;
119
120         /* audio data */
121         unsigned int   buf_size;
122         unsigned char  *buf_cpu;
123         dma_addr_t     buf_dma;
124
125         /* buffer setup */
126         int line_bytes;
127         int line_count;
128         int block_bytes;
129         int block_count;
130
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;
137
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;
148 };
149
150 struct cardinfo {
151         char *name;
152         int rate;
153 };
154
155 static struct btaudio *btaudios;
156 static unsigned int debug;
157 static unsigned int irq_debug;
158
159 /* -------------------------------------------------------------- */
160
161 #define BUF_DEFAULT 128*1024
162 #define BUF_MIN         8192
163
164 static int alloc_buffer(struct btaudio *bta)
165 {
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;
173                 }
174                 if (NULL == bta->buf_cpu)
175                         return -ENOMEM;
176                 memset(bta->buf_cpu,0,bta->buf_size);
177         }
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;
186                 }
187         }
188         return 0;
189 }
190
191 static void free_buffer(struct btaudio *bta)
192 {
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;
197         }
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;
202         }
203 }
204
205 static int make_risc(struct btaudio *bta)
206 {
207         int rp, bp, line, block;
208         unsigned long risc;
209
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;
217         }
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++;
237                 }
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;
241         }
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;
247 }
248
249 static int start_recording(struct btaudio *bta)
250 {
251         int ret;
252
253         if (0 != (ret = alloc_buffer(bta)))
254                 return ret;
255         if (0 != (ret = make_risc(bta)))
256                 return ret;
257
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);
284         }
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;
292 }
293
294 static void stop_recording(struct btaudio *bta)
295 {
296         btand(~15, REG_GPIO_DMA_CTL);
297         bta->recording = 0;
298         if (debug)
299                 printk(KERN_DEBUG "btaudio: recording stopped\n");
300 }
301
302
303 /* -------------------------------------------------------------- */
304
305 static int btaudio_mixer_open(struct inode *inode, struct file *file)
306 {
307         int minor = iminor(inode);
308         struct btaudio *bta;
309
310         for (bta = btaudios; bta != NULL; bta = bta->next)
311                 if (bta->mixer_dev == minor)
312                         break;
313         if (NULL == bta)
314                 return -ENODEV;
315
316         if (debug)
317                 printk("btaudio: open mixer [%d]\n",minor);
318         file->private_data = bta;
319         return 0;
320 }
321
322 static int btaudio_mixer_release(struct inode *inode, struct file *file)
323 {
324         return 0;
325 }
326
327 static int btaudio_mixer_ioctl(struct inode *inode, struct file *file,
328                                unsigned int cmd, unsigned long arg)
329 {
330         struct btaudio *bta = file->private_data;
331         int ret,val=0,i=0;
332         void __user *argp = (void __user *)arg;
333
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;
343         }
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)-1);
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;
352         }
353         if (cmd == OSS_GETVERSION)
354                 return put_user(SOUND_VERSION, (int __user *)argp);
355
356         /* read */
357         if (_SIOC_DIR(cmd) & _SIOC_WRITE)
358                 if (get_user(val, (int __user *)argp))
359                         return -EFAULT;
360
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;
372
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;
389                 }
390                 break;
391
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;
410
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;
423
424         default:
425                 return -EINVAL;
426         }
427         if (put_user(ret, (int __user *)argp))
428                 return -EFAULT;
429         return 0;
430 }
431
432 static 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,
438 };
439
440 /* -------------------------------------------------------------- */
441
442 static int btaudio_dsp_open(struct inode *inode, struct file *file,
443                             struct btaudio *bta, int analog)
444 {
445         mutex_lock(&bta->lock);
446         if (bta->users)
447                 goto busy;
448         bta->users++;
449         file->private_data = bta;
450
451         bta->analog = analog;
452         bta->dma_block = 0;
453         bta->read_offset = 0;
454         bta->read_count = 0;
455         bta->sampleshift = 0;
456
457         mutex_unlock(&bta->lock);
458         return 0;
459
460  busy:
461         mutex_unlock(&bta->lock);
462         return -EBUSY;
463 }
464
465 static int btaudio_dsp_open_digital(struct inode *inode, struct file *file)
466 {
467         int minor = iminor(inode);
468         struct btaudio *bta;
469
470         for (bta = btaudios; bta != NULL; bta = bta->next)
471                 if (bta->dsp_digital == minor)
472                         break;
473         if (NULL == bta)
474                 return -ENODEV;
475         
476         if (debug)
477                 printk("btaudio: open digital dsp [%d]\n",minor);
478         return btaudio_dsp_open(inode,file,bta,0);
479 }
480
481 static int btaudio_dsp_open_analog(struct inode *inode, struct file *file)
482 {
483         int minor = iminor(inode);
484         struct btaudio *bta;
485
486         for (bta = btaudios; bta != NULL; bta = bta->next)
487                 if (bta->dsp_analog == minor)
488                         break;
489         if (NULL == bta)
490                 return -ENODEV;
491
492         if (debug)
493                 printk("btaudio: open analog dsp [%d]\n",minor);
494         return btaudio_dsp_open(inode,file,bta,1);
495 }
496
497 static int btaudio_dsp_release(struct inode *inode, struct file *file)
498 {
499         struct btaudio *bta = file->private_data;
500
501         mutex_lock(&bta->lock);
502         if (bta->recording)
503                 stop_recording(bta);
504         bta->users--;
505         mutex_unlock(&bta->lock);
506         return 0;
507 }
508
509 static ssize_t btaudio_dsp_read(struct file *file, char __user *buffer,
510                                 size_t swcount, loff_t *ppos)
511 {
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);
516
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;
526                                 }
527                         }
528                         if (file->f_flags & O_NONBLOCK) {
529                                 if (0 == ret)
530                                         ret = -EAGAIN;
531                                 break;
532                         }
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;
541                         }
542                 }
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;
547                 
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;
555                         }
556
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;
567                         }
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);
572                         }
573
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;
583                         }
584                         for (; n; n--, src += (1 << bta->sampleshift), dst++)
585                                 __put_user(*src, dst);
586
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;
596                         }
597                         for (; n; n--, src += (1 << bta->sampleshift), dst++)
598                                 __put_user(*src, dst);
599                 }
600
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;
608         }
609         mutex_unlock(&bta->lock);
610         remove_wait_queue(&bta->readq, &wait);
611         current->state = TASK_RUNNING;
612         return ret;
613 }
614
615 static ssize_t btaudio_dsp_write(struct file *file, const char __user *buffer,
616                                  size_t count, loff_t *ppos)
617 {
618         return -EINVAL;
619 }
620
621 static int btaudio_dsp_ioctl(struct inode *inode, struct file *file,
622                              unsigned int cmd, unsigned long arg)
623 {
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;
628         
629         switch (cmd) {
630         case OSS_GETVERSION:
631                 return put_user(SOUND_VERSION, p);
632         case SNDCTL_DSP_GETCAPS:
633                 return 0;
634
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;
654                 }
655                 if (bta->recording) {
656                         mutex_lock(&bta->lock);
657                         stop_recording(bta);
658                         start_recording(bta);
659                         mutex_unlock(&bta->lock);
660                 }
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);
667                 }
668
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");
687                         }
688                 }
689                 return put_user((bta->channels)-1, p);
690
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);
701                 }
702                 /* fall through */
703         case SOUND_PCM_READ_CHANNELS:
704                 return put_user(bta->channels, p);
705                 
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);
711
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);
725                         }
726                 }
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,
730                                 p);
731                 break;
732         case SOUND_PCM_READ_BITS:
733                 return put_user(bta->bits, p);
734
735         case SNDCTL_DSP_NONBLOCK:
736                 file->f_flags |= O_NONBLOCK;
737                 return 0;
738
739         case SNDCTL_DSP_RESET:
740                 if (bta->recording) {
741                         mutex_lock(&bta->lock);
742                         stop_recording(bta);
743                         mutex_unlock(&bta->lock);
744                 }
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;
752                 }
753                 return put_user(bta->block_bytes>>bta->sampleshift,p);
754
755         case SNDCTL_DSP_SYNC:
756                 /* NOP */
757                 return 0;
758         case SNDCTL_DSP_GETISPACE:
759         {
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;
775         }
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;
783         }
784 }
785
786 static unsigned int btaudio_dsp_poll(struct file *file, struct poll_table_struct *wait)
787 {
788         struct btaudio *bta = file->private_data;
789         unsigned int mask = 0;
790
791         poll_wait(file, &bta->readq, wait);
792
793         if (0 != bta->read_count)
794                 mask |= (POLLIN | POLLRDNORM);
795
796         return mask;
797 }
798
799 static 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,
808 };
809
810 static 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,
819 };
820
821 /* -------------------------------------------------------------- */
822
823 static char *irq_name[] = { "", "", "", "OFLOW", "", "", "", "", "", "", "",
824                             "RISCI", "FBUS", "FTRGT", "FDSR", "PPERR",
825                             "RIPERR", "PABORT", "OCERR", "SCERR" };
826
827 static irqreturn_t btaudio_irq(int irq, void *dev_id)
828 {
829         int count = 0;
830         u32 stat,astat;
831         struct btaudio *bta = dev_id;
832         int handled = 0;
833
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);
842
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("*");
852                         }
853                         printk("\n");
854                 }
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");
864                         }
865                         if (blocks > 0) {
866                                 bta->read_count += blocks * bta->block_bytes;
867                                 wake_up_interruptible(&bta->readq);
868                         }
869                 }
870                 if (count > 10) {
871                         printk(KERN_WARNING
872                                "btaudio: Oops - irq mask cleared\n");
873                         btwrite(0, REG_INT_MASK);
874                 }
875         }
876         return IRQ_NONE;
877 }
878
879 /* -------------------------------------------------------------- */
880
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;
888
889 #define BTA_OSPREY200 1
890
891 static struct cardinfo cards[] = {
892         [0] = {
893                 .name   = "default",
894                 .rate   = 32000,
895         },
896         [BTA_OSPREY200] = {
897                 .name   = "Osprey 200",
898                 .rate   = 44100,
899         },
900 };
901
902 static int __devinit btaudio_probe(struct pci_dev *pci_dev,
903                                    const struct pci_device_id *pci_id)
904 {
905         struct btaudio *bta;
906         struct cardinfo *card = &cards[pci_id->driver_data];
907         unsigned char revision,lat;
908         int rc = -EBUSY;
909
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;
916         }
917
918         bta = kmalloc(sizeof(*bta),GFP_ATOMIC);
919         if (!bta) {
920                 rc = -ENOMEM;
921                 goto fail0;
922         }
923         memset(bta,0,sizeof(*bta));
924
925         bta->pci  = pci_dev;
926         bta->irq  = pci_dev->irq;
927         bta->mem  = pci_resource_start(pci_dev,0);
928         bta->mmio = ioremap(pci_resource_start(pci_dev,0),
929                             pci_resource_len(pci_dev,0));
930
931         bta->source     = 1;
932         bta->bits       = 8;
933         bta->channels   = 1;
934         if (bta->analog) {
935                 bta->decimation  = 15;
936         } else {
937                 bta->decimation  = 0;
938                 bta->sampleshift = 1;
939         }
940
941         /* sample rate */
942         bta->rate = card->rate;
943         if (rate)
944                 bta->rate = rate;
945         
946         mutex_init(&bta->lock);
947         init_waitqueue_head(&bta->readq);
948
949         if (-1 != latency) {
950                 printk(KERN_INFO "btaudio: setting pci latency timer to %d\n",
951                        latency);
952                 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
953         }
954         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
955         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat);
956         printk(KERN_INFO "btaudio: Bt%x (rev %d) at %02x:%02x.%x, ",
957                pci_dev->device,revision,pci_dev->bus->number,
958                PCI_SLOT(pci_dev->devfn),PCI_FUNC(pci_dev->devfn));
959         printk("irq: %d, latency: %d, mmio: 0x%lx\n",
960                bta->irq, lat, bta->mem);
961         printk("btaudio: using card config \"%s\"\n", card->name);
962
963         /* init hw */
964         btwrite(0, REG_GPIO_DMA_CTL);
965         btwrite(0, REG_INT_MASK);
966         btwrite(~0U, REG_INT_STAT);
967         pci_set_master(pci_dev);
968
969         if ((rc = request_irq(bta->irq, btaudio_irq, IRQF_SHARED|IRQF_DISABLED,
970                               "btaudio",(void *)bta)) < 0) {
971                 printk(KERN_WARNING
972                        "btaudio: can't request irq (rc=%d)\n",rc);
973                 goto fail1;
974         }
975
976         /* register devices */
977         if (digital) {
978                 rc = bta->dsp_digital =
979                         register_sound_dsp(&btaudio_digital_dsp_fops,dsp1);
980                 if (rc < 0) {
981                         printk(KERN_WARNING
982                                "btaudio: can't register digital dsp (rc=%d)\n",rc);
983                         goto fail2;
984                 }
985                 printk(KERN_INFO "btaudio: registered device dsp%d [digital]\n",
986                        bta->dsp_digital >> 4);
987         }
988         if (analog) {
989                 rc = bta->dsp_analog =
990                         register_sound_dsp(&btaudio_analog_dsp_fops,dsp2);
991                 if (rc < 0) {
992                         printk(KERN_WARNING
993                                "btaudio: can't register analog dsp (rc=%d)\n",rc);
994                         goto fail3;
995                 }
996                 printk(KERN_INFO "btaudio: registered device dsp%d [analog]\n",
997                        bta->dsp_analog >> 4);
998                 rc = bta->mixer_dev = register_sound_mixer(&btaudio_mixer_fops,mixer);
999                 if (rc < 0) {
1000                         printk(KERN_WARNING
1001                                "btaudio: can't register mixer (rc=%d)\n",rc);
1002                         goto fail4;
1003                 }
1004                 printk(KERN_INFO "btaudio: registered device mixer%d\n",
1005                        bta->mixer_dev >> 4);
1006         }
1007
1008         /* hook into linked list */
1009         bta->next = btaudios;
1010         btaudios = bta;
1011
1012         pci_set_drvdata(pci_dev,bta);
1013         return 0;
1014
1015  fail4:
1016         unregister_sound_dsp(bta->dsp_analog);
1017  fail3:
1018         if (digital)
1019                 unregister_sound_dsp(bta->dsp_digital);
1020  fail2:
1021         free_irq(bta->irq,bta); 
1022  fail1:
1023         iounmap(bta->mmio);
1024         kfree(bta);
1025  fail0:
1026         release_mem_region(pci_resource_start(pci_dev,0),
1027                            pci_resource_len(pci_dev,0));
1028         return rc;
1029 }
1030
1031 static void __devexit btaudio_remove(struct pci_dev *pci_dev)
1032 {
1033         struct btaudio *bta = pci_get_drvdata(pci_dev);
1034         struct btaudio *walk;
1035
1036         /* turn off all DMA / IRQs */
1037         btand(~15, REG_GPIO_DMA_CTL);
1038         btwrite(0, REG_INT_MASK);
1039         btwrite(~0U, REG_INT_STAT);
1040
1041         /* unregister devices */
1042         if (digital) {
1043                 unregister_sound_dsp(bta->dsp_digital);
1044         }
1045         if (analog) {
1046                 unregister_sound_dsp(bta->dsp_analog);
1047                 unregister_sound_mixer(bta->mixer_dev);
1048         }
1049
1050         /* free resources */
1051         free_buffer(bta);
1052         free_irq(bta->irq,bta);
1053         release_mem_region(pci_resource_start(pci_dev,0),
1054                            pci_resource_len(pci_dev,0));
1055         iounmap(bta->mmio);
1056
1057         /* remove from linked list */
1058         if (bta == btaudios) {
1059                 btaudios = NULL;
1060         } else {
1061                 for (walk = btaudios; walk->next != bta; walk = walk->next)
1062                         ; /* if (NULL == walk->next) BUG(); */
1063                 walk->next = bta->next;
1064         }
1065
1066         pci_set_drvdata(pci_dev, NULL);
1067         kfree(bta);
1068         return;
1069 }
1070
1071 /* -------------------------------------------------------------- */
1072
1073 static struct pci_device_id btaudio_pci_tbl[] = {
1074         {
1075                 .vendor         = PCI_VENDOR_ID_BROOKTREE,
1076                 .device         = 0x0878,
1077                 .subvendor      = 0x0070,
1078                 .subdevice      = 0xff01,
1079                 .driver_data    = BTA_OSPREY200,
1080         },{
1081                 .vendor         = PCI_VENDOR_ID_BROOKTREE,
1082                 .device         = 0x0878,
1083                 .subvendor      = PCI_ANY_ID,
1084                 .subdevice      = PCI_ANY_ID,
1085         },{
1086                 .vendor         = PCI_VENDOR_ID_BROOKTREE,
1087                 .device         = 0x0878,
1088                 .subvendor      = PCI_ANY_ID,
1089                 .subdevice      = PCI_ANY_ID,
1090         },{
1091                 /* --- end of list --- */
1092         }
1093 };
1094
1095 static struct pci_driver btaudio_pci_driver = {
1096         .name           = "btaudio",
1097         .id_table       = btaudio_pci_tbl,
1098         .probe          = btaudio_probe,
1099         .remove         =  __devexit_p(btaudio_remove),
1100 };
1101
1102 static int btaudio_init_module(void)
1103 {
1104         printk(KERN_INFO "btaudio: driver version 0.7 loaded [%s%s%s]\n",
1105                digital ? "digital" : "",
1106                analog && digital ? "+" : "",
1107                analog ? "analog" : "");
1108         return pci_register_driver(&btaudio_pci_driver);
1109 }
1110
1111 static void btaudio_cleanup_module(void)
1112 {
1113         pci_unregister_driver(&btaudio_pci_driver);
1114         return;
1115 }
1116
1117 module_init(btaudio_init_module);
1118 module_exit(btaudio_cleanup_module);
1119
1120 module_param(dsp1, int, S_IRUGO);
1121 module_param(dsp2, int, S_IRUGO);
1122 module_param(mixer, int, S_IRUGO);
1123 module_param(debug, int, S_IRUGO | S_IWUSR);
1124 module_param(irq_debug, int, S_IRUGO | S_IWUSR);
1125 module_param(digital, int, S_IRUGO);
1126 module_param(analog, int, S_IRUGO);
1127 module_param(rate, int, S_IRUGO);
1128 module_param(latency, int, S_IRUGO);
1129 MODULE_PARM_DESC(latency,"pci latency timer");
1130
1131 MODULE_DEVICE_TABLE(pci, btaudio_pci_tbl);
1132 MODULE_DESCRIPTION("bt878 audio dma driver");
1133 MODULE_AUTHOR("Gerd Knorr");
1134 MODULE_LICENSE("GPL");
1135
1136 /*
1137  * Local variables:
1138  * c-basic-offset: 8
1139  * End:
1140  */