Pull altix-fpga-reset into release branch
[linux-2.6] / drivers / media / video / saa7134 / saa7134-oss.c
1 /*
2  *
3  * device driver for philips saa7134 based TV cards
4  * oss dsp interface
5  *
6  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/soundcard.h>
30
31 #include "saa7134-reg.h"
32 #include "saa7134.h"
33
34 /* ------------------------------------------------------------------ */
35
36 static unsigned int oss_debug  = 0;
37 module_param(oss_debug, int, 0644);
38 MODULE_PARM_DESC(oss_debug,"enable debug messages [oss]");
39
40 static unsigned int oss_rate  = 0;
41 module_param(oss_rate, int, 0444);
42 MODULE_PARM_DESC(oss_rate,"sample rate (valid are: 32000,48000)");
43
44 #define dprintk(fmt, arg...)    if (oss_debug) \
45         printk(KERN_DEBUG "%s/oss: " fmt, dev->name , ## arg)
46
47 /* ------------------------------------------------------------------ */
48
49 static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
50 {
51         if (blksize < 0x100)
52                 blksize = 0x100;
53         if (blksize > 0x10000)
54                 blksize = 0x10000;
55
56         if (blocks < 2)
57                 blocks = 2;
58         if ((blksize * blocks) > 1024*1024)
59                 blocks = 1024*1024 / blksize;
60
61         dev->oss.blocks  = blocks;
62         dev->oss.blksize = blksize;
63         dev->oss.bufsize = blksize * blocks;
64
65         dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
66                 blocks,blksize,blksize * blocks / 1024);
67         return 0;
68 }
69
70 static int dsp_buffer_init(struct saa7134_dev *dev)
71 {
72         int err;
73
74         if (!dev->oss.bufsize)
75                 BUG();
76         videobuf_dma_init(&dev->oss.dma);
77         err = videobuf_dma_init_kernel(&dev->oss.dma, PCI_DMA_FROMDEVICE,
78                                        (dev->oss.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
79         if (0 != err)
80                 return err;
81         return 0;
82 }
83
84 static int dsp_buffer_free(struct saa7134_dev *dev)
85 {
86         if (!dev->oss.blksize)
87                 BUG();
88         videobuf_dma_free(&dev->oss.dma);
89         dev->oss.blocks  = 0;
90         dev->oss.blksize = 0;
91         dev->oss.bufsize = 0;
92         return 0;
93 }
94
95 static void dsp_dma_start(struct saa7134_dev *dev)
96 {
97         dev->oss.dma_blk     = 0;
98         dev->oss.dma_running = 1;
99         saa7134_set_dmabits(dev);
100 }
101
102 static void dsp_dma_stop(struct saa7134_dev *dev)
103 {
104         dev->oss.dma_blk     = -1;
105         dev->oss.dma_running = 0;
106         saa7134_set_dmabits(dev);
107 }
108
109 static int dsp_rec_start(struct saa7134_dev *dev)
110 {
111         int err, bswap, sign;
112         u32 fmt, control;
113         unsigned long flags;
114
115         /* prepare buffer */
116         if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->oss.dma)))
117                 return err;
118         if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->oss.pt)))
119                 goto fail1;
120         if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->oss.pt,
121                                               dev->oss.dma.sglist,
122                                               dev->oss.dma.sglen,
123                                               0)))
124                 goto fail2;
125
126         /* sample format */
127         switch (dev->oss.afmt) {
128         case AFMT_U8:
129         case AFMT_S8:     fmt = 0x00;  break;
130         case AFMT_U16_LE:
131         case AFMT_U16_BE:
132         case AFMT_S16_LE:
133         case AFMT_S16_BE: fmt = 0x01;  break;
134         default:
135                 err = -EINVAL;
136                 goto fail2;
137         }
138
139         switch (dev->oss.afmt) {
140         case AFMT_S8:
141         case AFMT_S16_LE:
142         case AFMT_S16_BE: sign = 1; break;
143         default:          sign = 0; break;
144         }
145
146         switch (dev->oss.afmt) {
147         case AFMT_U16_BE:
148         case AFMT_S16_BE: bswap = 1; break;
149         default:          bswap = 0; break;
150         }
151
152         switch (dev->pci->device) {
153         case PCI_DEVICE_ID_PHILIPS_SAA7134:
154                 if (1 == dev->oss.channels)
155                         fmt |= (1 << 3);
156                 if (2 == dev->oss.channels)
157                         fmt |= (3 << 3);
158                 if (sign)
159                         fmt |= 0x04;
160                 fmt |= (TV == dev->oss.input) ? 0xc0 : 0x80;
161
162                 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->oss.blksize - 1) & 0x0000ff));
163                 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->oss.blksize - 1) & 0x00ff00) >>  8);
164                 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->oss.blksize - 1) & 0xff0000) >> 16);
165                 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
166
167                 break;
168         case PCI_DEVICE_ID_PHILIPS_SAA7133:
169         case PCI_DEVICE_ID_PHILIPS_SAA7135:
170                 if (1 == dev->oss.channels)
171                         fmt |= (1 << 4);
172                 if (2 == dev->oss.channels)
173                         fmt |= (2 << 4);
174                 if (!sign)
175                         fmt |= 0x04;
176                 saa_writel(0x588 >> 2, dev->oss.blksize -4);
177                 saa_writel(0x58c >> 2, 0x543210 | (fmt << 24));
178                 break;
179         }
180         dprintk("rec_start: afmt=%d ch=%d  =>  fmt=0x%x swap=%c\n",
181                 dev->oss.afmt, dev->oss.channels, fmt,
182                 bswap ? 'b' : '-');
183
184         /* dma: setup channel 6 (= AUDIO) */
185         control = SAA7134_RS_CONTROL_BURST_16 |
186                 SAA7134_RS_CONTROL_ME |
187                 (dev->oss.pt.dma >> 12);
188         if (bswap)
189                 control |= SAA7134_RS_CONTROL_BSWAP;
190         saa_writel(SAA7134_RS_BA1(6),0);
191         saa_writel(SAA7134_RS_BA2(6),dev->oss.blksize);
192         saa_writel(SAA7134_RS_PITCH(6),0);
193         saa_writel(SAA7134_RS_CONTROL(6),control);
194
195         /* start dma */
196         dev->oss.recording_on = 1;
197         spin_lock_irqsave(&dev->slock,flags);
198         dsp_dma_start(dev);
199         spin_unlock_irqrestore(&dev->slock,flags);
200         return 0;
201
202  fail2:
203         saa7134_pgtable_free(dev->pci,&dev->oss.pt);
204  fail1:
205         videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
206         return err;
207 }
208
209 static int dsp_rec_stop(struct saa7134_dev *dev)
210 {
211         unsigned long flags;
212
213         dprintk("rec_stop dma_blk=%d\n",dev->oss.dma_blk);
214
215         /* stop dma */
216         dev->oss.recording_on = 0;
217         spin_lock_irqsave(&dev->slock,flags);
218         dsp_dma_stop(dev);
219         spin_unlock_irqrestore(&dev->slock,flags);
220
221         /* unlock buffer */
222         saa7134_pgtable_free(dev->pci,&dev->oss.pt);
223         videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
224         return 0;
225 }
226
227 /* ------------------------------------------------------------------ */
228
229 static int dsp_open(struct inode *inode, struct file *file)
230 {
231         int minor = iminor(inode);
232         struct saa7134_dev *h,*dev = NULL;
233         struct list_head *list;
234         int err;
235
236         list_for_each(list,&saa7134_devlist) {
237                 h = list_entry(list, struct saa7134_dev, devlist);
238                 if (h->oss.minor_dsp == minor)
239                         dev = h;
240         }
241         if (NULL == dev)
242                 return -ENODEV;
243
244         down(&dev->oss.lock);
245         err = -EBUSY;
246         if (dev->oss.users_dsp)
247                 goto fail1;
248         dev->oss.users_dsp++;
249         file->private_data = dev;
250
251         dev->oss.afmt        = AFMT_U8;
252         dev->oss.channels    = 1;
253         dev->oss.read_count  = 0;
254         dev->oss.read_offset = 0;
255         dsp_buffer_conf(dev,PAGE_SIZE,64);
256         err = dsp_buffer_init(dev);
257         if (0 != err)
258                 goto fail2;
259
260         up(&dev->oss.lock);
261         return 0;
262
263  fail2:
264         dev->oss.users_dsp--;
265  fail1:
266         up(&dev->oss.lock);
267         return err;
268 }
269
270 static int dsp_release(struct inode *inode, struct file *file)
271 {
272         struct saa7134_dev *dev = file->private_data;
273
274         down(&dev->oss.lock);
275         if (dev->oss.recording_on)
276                 dsp_rec_stop(dev);
277         dsp_buffer_free(dev);
278         dev->oss.users_dsp--;
279         file->private_data = NULL;
280         up(&dev->oss.lock);
281         return 0;
282 }
283
284 static ssize_t dsp_read(struct file *file, char __user *buffer,
285                         size_t count, loff_t *ppos)
286 {
287         struct saa7134_dev *dev = file->private_data;
288         DECLARE_WAITQUEUE(wait, current);
289         unsigned int bytes;
290         unsigned long flags;
291         int err,ret = 0;
292
293         add_wait_queue(&dev->oss.wq, &wait);
294         down(&dev->oss.lock);
295         while (count > 0) {
296                 /* wait for data if needed */
297                 if (0 == dev->oss.read_count) {
298                         if (!dev->oss.recording_on) {
299                                 err = dsp_rec_start(dev);
300                                 if (err < 0) {
301                                         if (0 == ret)
302                                                 ret = err;
303                                         break;
304                                 }
305                         }
306                         if (dev->oss.recording_on &&
307                             !dev->oss.dma_running) {
308                                 /* recover from overruns */
309                                 spin_lock_irqsave(&dev->slock,flags);
310                                 dsp_dma_start(dev);
311                                 spin_unlock_irqrestore(&dev->slock,flags);
312                         }
313                         if (file->f_flags & O_NONBLOCK) {
314                                 if (0 == ret)
315                                         ret = -EAGAIN;
316                                 break;
317                         }
318                         up(&dev->oss.lock);
319                         set_current_state(TASK_INTERRUPTIBLE);
320                         if (0 == dev->oss.read_count)
321                                 schedule();
322                         set_current_state(TASK_RUNNING);
323                         down(&dev->oss.lock);
324                         if (signal_pending(current)) {
325                                 if (0 == ret)
326                                         ret = -EINTR;
327                                 break;
328                         }
329                 }
330
331                 /* copy data to userspace */
332                 bytes = count;
333                 if (bytes > dev->oss.read_count)
334                         bytes = dev->oss.read_count;
335                 if (bytes > dev->oss.bufsize - dev->oss.read_offset)
336                         bytes = dev->oss.bufsize - dev->oss.read_offset;
337                 if (copy_to_user(buffer + ret,
338                                  dev->oss.dma.vmalloc + dev->oss.read_offset,
339                                  bytes)) {
340                         if (0 == ret)
341                                 ret = -EFAULT;
342                         break;
343                 }
344
345                 ret   += bytes;
346                 count -= bytes;
347                 dev->oss.read_count  -= bytes;
348                 dev->oss.read_offset += bytes;
349                 if (dev->oss.read_offset == dev->oss.bufsize)
350                         dev->oss.read_offset = 0;
351         }
352         up(&dev->oss.lock);
353         remove_wait_queue(&dev->oss.wq, &wait);
354         return ret;
355 }
356
357 static ssize_t dsp_write(struct file *file, const char __user *buffer,
358                          size_t count, loff_t *ppos)
359 {
360         return -EINVAL;
361 }
362
363 static int dsp_ioctl(struct inode *inode, struct file *file,
364                      unsigned int cmd, unsigned long arg)
365 {
366         struct saa7134_dev *dev = file->private_data;
367         void __user *argp = (void __user *) arg;
368         int __user *p = argp;
369         int val = 0;
370
371         if (oss_debug > 1)
372                 saa7134_print_ioctl(dev->name,cmd);
373         switch (cmd) {
374         case OSS_GETVERSION:
375                 return put_user(SOUND_VERSION, p);
376         case SNDCTL_DSP_GETCAPS:
377                 return 0;
378
379         case SNDCTL_DSP_SPEED:
380                 if (get_user(val, p))
381                         return -EFAULT;
382                 /* fall through */
383         case SOUND_PCM_READ_RATE:
384                 return put_user(dev->oss.rate, p);
385
386         case SNDCTL_DSP_STEREO:
387                 if (get_user(val, p))
388                         return -EFAULT;
389                 down(&dev->oss.lock);
390                 dev->oss.channels = val ? 2 : 1;
391                 if (dev->oss.recording_on) {
392                         dsp_rec_stop(dev);
393                         dsp_rec_start(dev);
394                 }
395                 up(&dev->oss.lock);
396                 return put_user(dev->oss.channels-1, p);
397
398         case SNDCTL_DSP_CHANNELS:
399                 if (get_user(val, p))
400                         return -EFAULT;
401                 if (val != 1 && val != 2)
402                         return -EINVAL;
403                 down(&dev->oss.lock);
404                 dev->oss.channels = val;
405                 if (dev->oss.recording_on) {
406                         dsp_rec_stop(dev);
407                         dsp_rec_start(dev);
408                 }
409                 up(&dev->oss.lock);
410                 /* fall through */
411         case SOUND_PCM_READ_CHANNELS:
412                 return put_user(dev->oss.channels, p);
413
414         case SNDCTL_DSP_GETFMTS: /* Returns a mask */
415                 return put_user(AFMT_U8     | AFMT_S8     |
416                                 AFMT_U16_LE | AFMT_U16_BE |
417                                 AFMT_S16_LE | AFMT_S16_BE, p);
418
419         case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
420                 if (get_user(val, p))
421                         return -EFAULT;
422                 switch (val) {
423                 case AFMT_QUERY:
424                         /* nothing to do */
425                         break;
426                 case AFMT_U8:
427                 case AFMT_S8:
428                 case AFMT_U16_LE:
429                 case AFMT_U16_BE:
430                 case AFMT_S16_LE:
431                 case AFMT_S16_BE:
432                         down(&dev->oss.lock);
433                         dev->oss.afmt = val;
434                         if (dev->oss.recording_on) {
435                                 dsp_rec_stop(dev);
436                                 dsp_rec_start(dev);
437                         }
438                         up(&dev->oss.lock);
439                         return put_user(dev->oss.afmt, p);
440                 default:
441                         return -EINVAL;
442                 }
443
444         case SOUND_PCM_READ_BITS:
445                 switch (dev->oss.afmt) {
446                 case AFMT_U8:
447                 case AFMT_S8:
448                         return put_user(8, p);
449                 case AFMT_U16_LE:
450                 case AFMT_U16_BE:
451                 case AFMT_S16_LE:
452                 case AFMT_S16_BE:
453                         return put_user(16, p);
454                 default:
455                         return -EINVAL;
456                 }
457
458         case SNDCTL_DSP_NONBLOCK:
459                 file->f_flags |= O_NONBLOCK;
460                 return 0;
461
462         case SNDCTL_DSP_RESET:
463                 down(&dev->oss.lock);
464                 if (dev->oss.recording_on)
465                         dsp_rec_stop(dev);
466                 up(&dev->oss.lock);
467                 return 0;
468         case SNDCTL_DSP_GETBLKSIZE:
469                 return put_user(dev->oss.blksize, p);
470
471         case SNDCTL_DSP_SETFRAGMENT:
472                 if (get_user(val, p))
473                         return -EFAULT;
474                 if (dev->oss.recording_on)
475                         return -EBUSY;
476                 dsp_buffer_free(dev);
477                 /* used to be arg >> 16 instead of val >> 16; fixed */
478                 dsp_buffer_conf(dev,1 << (val & 0xffff), (val >> 16) & 0xffff);
479                 dsp_buffer_init(dev);
480                 return 0;
481
482         case SNDCTL_DSP_SYNC:
483                 /* NOP */
484                 return 0;
485
486         case SNDCTL_DSP_GETISPACE:
487         {
488                 audio_buf_info info;
489                 info.fragsize   = dev->oss.blksize;
490                 info.fragstotal = dev->oss.blocks;
491                 info.bytes      = dev->oss.read_count;
492                 info.fragments  = info.bytes / info.fragsize;
493                 if (copy_to_user(argp, &info, sizeof(info)))
494                         return -EFAULT;
495                 return 0;
496         }
497         default:
498                 return -EINVAL;
499         }
500 }
501
502 static unsigned int dsp_poll(struct file *file, struct poll_table_struct *wait)
503 {
504         struct saa7134_dev *dev = file->private_data;
505         unsigned int mask = 0;
506
507         poll_wait(file, &dev->oss.wq, wait);
508
509         if (0 == dev->oss.read_count) {
510                 down(&dev->oss.lock);
511                 if (!dev->oss.recording_on)
512                         dsp_rec_start(dev);
513                 up(&dev->oss.lock);
514         } else
515                 mask |= (POLLIN | POLLRDNORM);
516         return mask;
517 }
518
519 struct file_operations saa7134_dsp_fops = {
520         .owner   = THIS_MODULE,
521         .open    = dsp_open,
522         .release = dsp_release,
523         .read    = dsp_read,
524         .write   = dsp_write,
525         .ioctl   = dsp_ioctl,
526         .poll    = dsp_poll,
527         .llseek  = no_llseek,
528 };
529
530 /* ------------------------------------------------------------------ */
531
532 static int
533 mixer_recsrc_7134(struct saa7134_dev *dev)
534 {
535         int analog_io,rate;
536
537         switch (dev->oss.input) {
538         case TV:
539                 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
540                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x00);
541                 break;
542         case LINE1:
543         case LINE2:
544         case LINE2_LEFT:
545                 analog_io = (LINE1 == dev->oss.input) ? 0x00 : 0x08;
546                 rate = (32000 == dev->oss.rate) ? 0x01 : 0x03;
547                 saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x08, analog_io);
548                 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
549                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, rate);
550                 break;
551         }
552         return 0;
553 }
554
555 static int
556 mixer_recsrc_7133(struct saa7134_dev *dev)
557 {
558         u32 anabar, xbarin;
559
560         xbarin = 0x03; // adc
561     anabar = 0;
562         switch (dev->oss.input) {
563         case TV:
564                 xbarin = 0; // Demodulator
565         anabar = 2; // DACs
566                 break;
567         case LINE1:
568                 anabar = 0;  // aux1, aux1
569                 break;
570         case LINE2:
571         case LINE2_LEFT:
572                 anabar = 9;  // aux2, aux2
573                 break;
574         }
575     /* output xbar always main channel */
576         saa_dsp_writel(dev, 0x46c >> 2, 0xbbbb10);
577         saa_dsp_writel(dev, 0x464 >> 2, xbarin);
578         saa_writel(0x594 >> 2, anabar);
579
580         return 0;
581 }
582
583 static int
584 mixer_recsrc(struct saa7134_dev *dev, enum saa7134_audio_in src)
585 {
586         static const char *iname[] = { "Oops", "TV", "LINE1", "LINE2" };
587
588         dev->oss.count++;
589         dev->oss.input = src;
590         dprintk("mixer input = %s\n",iname[dev->oss.input]);
591
592         switch (dev->pci->device) {
593         case PCI_DEVICE_ID_PHILIPS_SAA7134:
594                 mixer_recsrc_7134(dev);
595                 break;
596         case PCI_DEVICE_ID_PHILIPS_SAA7133:
597         case PCI_DEVICE_ID_PHILIPS_SAA7135:
598                 mixer_recsrc_7133(dev);
599                 break;
600         }
601         return 0;
602 }
603
604 static int
605 mixer_level(struct saa7134_dev *dev, enum saa7134_audio_in src, int level)
606 {
607         switch (dev->pci->device) {
608         case PCI_DEVICE_ID_PHILIPS_SAA7134:
609                 switch (src) {
610                 case TV:
611                         /* nothing */
612                         break;
613                 case LINE1:
614                         saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x10,
615                                    (100 == level) ? 0x00 : 0x10);
616                         break;
617                 case LINE2:
618                 case LINE2_LEFT:
619                         saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x20,
620                                    (100 == level) ? 0x00 : 0x20);
621                         break;
622                 }
623                 break;
624         case PCI_DEVICE_ID_PHILIPS_SAA7133:
625         case PCI_DEVICE_ID_PHILIPS_SAA7135:
626                 /* nothing */
627                 break;
628         }
629         return 0;
630 }
631
632 /* ------------------------------------------------------------------ */
633
634 static int mixer_open(struct inode *inode, struct file *file)
635 {
636         int minor = iminor(inode);
637         struct saa7134_dev *h,*dev = NULL;
638         struct list_head *list;
639
640         list_for_each(list,&saa7134_devlist) {
641                 h = list_entry(list, struct saa7134_dev, devlist);
642                 if (h->oss.minor_mixer == minor)
643                         dev = h;
644         }
645         if (NULL == dev)
646                 return -ENODEV;
647
648         file->private_data = dev;
649         return 0;
650 }
651
652 static int mixer_release(struct inode *inode, struct file *file)
653 {
654         file->private_data = NULL;
655         return 0;
656 }
657
658 static int mixer_ioctl(struct inode *inode, struct file *file,
659                      unsigned int cmd, unsigned long arg)
660 {
661         struct saa7134_dev *dev = file->private_data;
662         enum saa7134_audio_in input;
663         int val,ret;
664         void __user *argp = (void __user *) arg;
665         int __user *p = argp;
666
667         if (oss_debug > 1)
668                 saa7134_print_ioctl(dev->name,cmd);
669         switch (cmd) {
670         case OSS_GETVERSION:
671                 return put_user(SOUND_VERSION, p);
672         case SOUND_MIXER_INFO:
673         {
674                 mixer_info info;
675                 memset(&info,0,sizeof(info));
676                 strlcpy(info.id,   "TV audio", sizeof(info.id));
677                 strlcpy(info.name, dev->name,  sizeof(info.name));
678                 info.modify_counter = dev->oss.count;
679                 if (copy_to_user(argp, &info, sizeof(info)))
680                         return -EFAULT;
681                 return 0;
682         }
683         case SOUND_OLD_MIXER_INFO:
684         {
685                 _old_mixer_info info;
686                 memset(&info,0,sizeof(info));
687                 strlcpy(info.id,   "TV audio", sizeof(info.id));
688                 strlcpy(info.name, dev->name,  sizeof(info.name));
689                 if (copy_to_user(argp, &info, sizeof(info)))
690                         return -EFAULT;
691                 return 0;
692         }
693         case MIXER_READ(SOUND_MIXER_CAPS):
694                 return put_user(SOUND_CAP_EXCL_INPUT, p);
695         case MIXER_READ(SOUND_MIXER_STEREODEVS):
696                 return put_user(0, p);
697         case MIXER_READ(SOUND_MIXER_RECMASK):
698         case MIXER_READ(SOUND_MIXER_DEVMASK):
699                 val = SOUND_MASK_LINE1 | SOUND_MASK_LINE2;
700                 if (32000 == dev->oss.rate)
701                         val |= SOUND_MASK_VIDEO;
702                 return put_user(val, p);
703
704         case MIXER_WRITE(SOUND_MIXER_RECSRC):
705                 if (get_user(val, p))
706                         return -EFAULT;
707                 input = dev->oss.input;
708                 if (32000 == dev->oss.rate  &&
709                     val & SOUND_MASK_VIDEO  &&  dev->oss.input != TV)
710                         input = TV;
711                 if (val & SOUND_MASK_LINE1  &&  dev->oss.input != LINE1)
712                         input = LINE1;
713                 if (val & SOUND_MASK_LINE2  &&  dev->oss.input != LINE2)
714                         input = LINE2;
715                 if (input != dev->oss.input)
716                         mixer_recsrc(dev,input);
717                 /* fall throuth */
718         case MIXER_READ(SOUND_MIXER_RECSRC):
719                 switch (dev->oss.input) {
720                 case TV:    ret = SOUND_MASK_VIDEO; break;
721                 case LINE1: ret = SOUND_MASK_LINE1; break;
722                 case LINE2: ret = SOUND_MASK_LINE2; break;
723                 default:    ret = 0;
724                 }
725                 return put_user(ret, p);
726
727         case MIXER_WRITE(SOUND_MIXER_VIDEO):
728         case MIXER_READ(SOUND_MIXER_VIDEO):
729                 if (32000 != dev->oss.rate)
730                         return -EINVAL;
731                 return put_user(100 | 100 << 8, p);
732
733         case MIXER_WRITE(SOUND_MIXER_LINE1):
734                 if (get_user(val, p))
735                         return -EFAULT;
736                 val &= 0xff;
737                 val = (val <= 50) ? 50 : 100;
738                 dev->oss.line1 = val;
739                 mixer_level(dev,LINE1,dev->oss.line1);
740                 /* fall throuth */
741         case MIXER_READ(SOUND_MIXER_LINE1):
742                 return put_user(dev->oss.line1 | dev->oss.line1 << 8, p);
743
744         case MIXER_WRITE(SOUND_MIXER_LINE2):
745                 if (get_user(val, p))
746                         return -EFAULT;
747                 val &= 0xff;
748                 val = (val <= 50) ? 50 : 100;
749                 dev->oss.line2 = val;
750                 mixer_level(dev,LINE2,dev->oss.line2);
751                 /* fall throuth */
752         case MIXER_READ(SOUND_MIXER_LINE2):
753                 return put_user(dev->oss.line2 | dev->oss.line2 << 8, p);
754
755         default:
756                 return -EINVAL;
757         }
758 }
759
760 struct file_operations saa7134_mixer_fops = {
761         .owner   = THIS_MODULE,
762         .open    = mixer_open,
763         .release = mixer_release,
764         .ioctl   = mixer_ioctl,
765         .llseek  = no_llseek,
766 };
767
768 /* ------------------------------------------------------------------ */
769
770 int saa7134_oss_init1(struct saa7134_dev *dev)
771 {
772         /* general */
773         init_MUTEX(&dev->oss.lock);
774         init_waitqueue_head(&dev->oss.wq);
775
776         switch (dev->pci->device) {
777         case PCI_DEVICE_ID_PHILIPS_SAA7133:
778         case PCI_DEVICE_ID_PHILIPS_SAA7135:
779                 saa_writel(0x588 >> 2, 0x00000fff);
780                 saa_writel(0x58c >> 2, 0x00543210);
781                 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbbbb);
782                 break;
783         }
784
785         /* dsp */
786         dev->oss.rate = 32000;
787         if (oss_rate)
788                 dev->oss.rate = oss_rate;
789         dev->oss.rate = (dev->oss.rate > 40000) ? 48000 : 32000;
790
791         /* mixer */
792         dev->oss.line1 = 50;
793         dev->oss.line2 = 50;
794         mixer_level(dev,LINE1,dev->oss.line1);
795         mixer_level(dev,LINE2,dev->oss.line2);
796         mixer_recsrc(dev, (dev->oss.rate == 32000) ? TV : LINE2);
797
798         return 0;
799 }
800
801 int saa7134_oss_fini(struct saa7134_dev *dev)
802 {
803         /* nothing */
804         return 0;
805 }
806
807 void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status)
808 {
809         int next_blk, reg = 0;
810
811         spin_lock(&dev->slock);
812         if (UNSET == dev->oss.dma_blk) {
813                 dprintk("irq: recording stopped\n");
814                 goto done;
815         }
816         if (0 != (status & 0x0f000000))
817                 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
818         if (0 == (status & 0x10000000)) {
819                 /* odd */
820                 if (0 == (dev->oss.dma_blk & 0x01))
821                         reg = SAA7134_RS_BA1(6);
822         } else {
823                 /* even */
824                 if (1 == (dev->oss.dma_blk & 0x01))
825                         reg = SAA7134_RS_BA2(6);
826         }
827         if (0 == reg) {
828                 dprintk("irq: field oops [%s]\n",
829                         (status & 0x10000000) ? "even" : "odd");
830                 goto done;
831         }
832         if (dev->oss.read_count >= dev->oss.blksize * (dev->oss.blocks-2)) {
833                 dprintk("irq: overrun [full=%d/%d]\n",dev->oss.read_count,
834                         dev->oss.bufsize);
835                 dsp_dma_stop(dev);
836                 goto done;
837         }
838
839         /* next block addr */
840         next_blk = (dev->oss.dma_blk + 2) % dev->oss.blocks;
841         saa_writel(reg,next_blk * dev->oss.blksize);
842         if (oss_debug > 2)
843                 dprintk("irq: ok, %s, next_blk=%d, addr=%x\n",
844                         (status & 0x10000000) ? "even" : "odd ", next_blk,
845                         next_blk * dev->oss.blksize);
846
847         /* update status & wake waiting readers */
848         dev->oss.dma_blk = (dev->oss.dma_blk + 1) % dev->oss.blocks;
849         dev->oss.read_count += dev->oss.blksize;
850         wake_up(&dev->oss.wq);
851
852  done:
853         spin_unlock(&dev->slock);
854 }
855
856 /* ----------------------------------------------------------- */
857 /*
858  * Local variables:
859  * c-basic-offset: 8
860  * End:
861  */