blob: 72444f039e3d932ceb685f9d3c40408df08d3e1f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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]
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08007 * 2005 conversion to standalone module:
8 * Ricardo Cerqueira <v4l@cerqueira.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/init.h>
26#include <linux/list.h>
27#include <linux/module.h>
28#include <linux/moduleparam.h>
29#include <linux/kernel.h>
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080030#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/slab.h>
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080032#include <linux/sound.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/soundcard.h>
34
35#include "saa7134-reg.h"
36#include "saa7134.h"
37
38/* ------------------------------------------------------------------ */
39
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080040static unsigned int debug = 0;
41module_param(debug, int, 0644);
42MODULE_PARM_DESC(debug,"enable debug messages [oss]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080044static unsigned int rate = 0;
45module_param(rate, int, 0444);
46MODULE_PARM_DESC(rate,"sample rate (valid are: 32000,48000)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080048static unsigned int dsp_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
49MODULE_PARM_DESC(dsp_nr, "device numbers for SAA7134 capture interface(s).");
50module_param_array(dsp_nr, int, NULL, 0444);
51
52static unsigned int mixer_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
53MODULE_PARM_DESC(mixer_nr, "mixer numbers for SAA7134 capture interface(s).");
54module_param_array(mixer_nr, int, NULL, 0444);
55
56#define dprintk(fmt, arg...) if (debug) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 printk(KERN_DEBUG "%s/oss: " fmt, dev->name , ## arg)
58
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/* ------------------------------------------------------------------ */
61
62static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
63{
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (blksize < 0x100)
65 blksize = 0x100;
66 if (blksize > 0x10000)
67 blksize = 0x10000;
68
69 if (blocks < 2)
70 blocks = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if ((blksize * blocks) > 1024*1024)
72 blocks = 1024*1024 / blksize;
73
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080074 dev->dmasound.blocks = blocks;
75 dev->dmasound.blksize = blksize;
76 dev->dmasound.bufsize = blksize * blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080079 blocks,blksize,blksize * blocks / 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 0;
81}
82
83static int dsp_buffer_init(struct saa7134_dev *dev)
84{
85 int err;
86
Eric Sesterhennae246012006-03-13 13:17:11 -030087 BUG_ON(!dev->dmasound.bufsize);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080088 videobuf_dma_init(&dev->dmasound.dma);
89 err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
90 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (0 != err)
92 return err;
93 return 0;
94}
95
96static int dsp_buffer_free(struct saa7134_dev *dev)
97{
Eric Sesterhennae246012006-03-13 13:17:11 -030098 BUG_ON(!dev->dmasound.blksize);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080099 videobuf_dma_free(&dev->dmasound.dma);
100 dev->dmasound.blocks = 0;
101 dev->dmasound.blksize = 0;
102 dev->dmasound.bufsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104}
105
106static void dsp_dma_start(struct saa7134_dev *dev)
107{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800108 dev->dmasound.dma_blk = 0;
109 dev->dmasound.dma_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 saa7134_set_dmabits(dev);
111}
112
113static void dsp_dma_stop(struct saa7134_dev *dev)
114{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800115 dev->dmasound.dma_blk = -1;
116 dev->dmasound.dma_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 saa7134_set_dmabits(dev);
118}
119
120static int dsp_rec_start(struct saa7134_dev *dev)
121{
122 int err, bswap, sign;
123 u32 fmt, control;
124 unsigned long flags;
125
126 /* prepare buffer */
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300127 if (0 != (err = videobuf_pci_dma_map(dev->pci,&dev->dmasound.dma)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return err;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800129 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto fail1;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800131 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
132 dev->dmasound.dma.sglist,
133 dev->dmasound.dma.sglen,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 0)))
135 goto fail2;
136
137 /* sample format */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800138 switch (dev->dmasound.afmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 case AFMT_U8:
140 case AFMT_S8: fmt = 0x00; break;
141 case AFMT_U16_LE:
142 case AFMT_U16_BE:
143 case AFMT_S16_LE:
144 case AFMT_S16_BE: fmt = 0x01; break;
145 default:
146 err = -EINVAL;
147 goto fail2;
148 }
149
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800150 switch (dev->dmasound.afmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 case AFMT_S8:
152 case AFMT_S16_LE:
153 case AFMT_S16_BE: sign = 1; break;
154 default: sign = 0; break;
155 }
156
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800157 switch (dev->dmasound.afmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case AFMT_U16_BE:
159 case AFMT_S16_BE: bswap = 1; break;
160 default: bswap = 0; break;
161 }
162
163 switch (dev->pci->device) {
164 case PCI_DEVICE_ID_PHILIPS_SAA7134:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800165 if (1 == dev->dmasound.channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 fmt |= (1 << 3);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800167 if (2 == dev->dmasound.channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 fmt |= (3 << 3);
169 if (sign)
170 fmt |= 0x04;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800171 fmt |= (TV == dev->dmasound.input) ? 0xc0 : 0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800173 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
174 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
175 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
Mauro Carvalho Chehabac19ecc2005-06-23 22:05:09 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 case PCI_DEVICE_ID_PHILIPS_SAA7133:
180 case PCI_DEVICE_ID_PHILIPS_SAA7135:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800181 if (1 == dev->dmasound.channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 fmt |= (1 << 4);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800183 if (2 == dev->dmasound.channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 fmt |= (2 << 4);
185 if (!sign)
186 fmt |= 0x04;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800187 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -4);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800188 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 break;
190 }
191 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800192 dev->dmasound.afmt, dev->dmasound.channels, fmt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 bswap ? 'b' : '-');
194
195 /* dma: setup channel 6 (= AUDIO) */
196 control = SAA7134_RS_CONTROL_BURST_16 |
197 SAA7134_RS_CONTROL_ME |
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800198 (dev->dmasound.pt.dma >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (bswap)
200 control |= SAA7134_RS_CONTROL_BSWAP;
201 saa_writel(SAA7134_RS_BA1(6),0);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800202 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 saa_writel(SAA7134_RS_PITCH(6),0);
204 saa_writel(SAA7134_RS_CONTROL(6),control);
205
206 /* start dma */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800207 dev->dmasound.recording_on = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spin_lock_irqsave(&dev->slock,flags);
209 dsp_dma_start(dev);
210 spin_unlock_irqrestore(&dev->slock,flags);
211 return 0;
212
213 fail2:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800214 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 fail1:
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300216 videobuf_pci_dma_unmap(dev->pci,&dev->dmasound.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return err;
218}
219
220static int dsp_rec_stop(struct saa7134_dev *dev)
221{
222 unsigned long flags;
223
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800224 dprintk("rec_stop dma_blk=%d\n",dev->dmasound.dma_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* stop dma */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800227 dev->dmasound.recording_on = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_lock_irqsave(&dev->slock,flags);
229 dsp_dma_stop(dev);
230 spin_unlock_irqrestore(&dev->slock,flags);
231
232 /* unlock buffer */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800233 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -0300234 videobuf_pci_dma_unmap(dev->pci,&dev->dmasound.dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236}
237
238/* ------------------------------------------------------------------ */
239
240static int dsp_open(struct inode *inode, struct file *file)
241{
242 int minor = iminor(inode);
243 struct saa7134_dev *h,*dev = NULL;
244 struct list_head *list;
245 int err;
246
247 list_for_each(list,&saa7134_devlist) {
248 h = list_entry(list, struct saa7134_dev, devlist);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800249 if (h->dmasound.minor_dsp == minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 dev = h;
251 }
252 if (NULL == dev)
253 return -ENODEV;
254
Ingo Molnar3593cab2006-02-07 06:49:14 -0200255 mutex_lock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 err = -EBUSY;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800257 if (dev->dmasound.users_dsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 goto fail1;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800259 dev->dmasound.users_dsp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 file->private_data = dev;
261
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800262 dev->dmasound.afmt = AFMT_U8;
263 dev->dmasound.channels = 1;
264 dev->dmasound.read_count = 0;
265 dev->dmasound.read_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dsp_buffer_conf(dev,PAGE_SIZE,64);
267 err = dsp_buffer_init(dev);
268 if (0 != err)
269 goto fail2;
270
Ingo Molnar3593cab2006-02-07 06:49:14 -0200271 mutex_unlock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273
274 fail2:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800275 dev->dmasound.users_dsp--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 fail1:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200277 mutex_unlock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return err;
279}
280
281static int dsp_release(struct inode *inode, struct file *file)
282{
283 struct saa7134_dev *dev = file->private_data;
284
Ingo Molnar3593cab2006-02-07 06:49:14 -0200285 mutex_lock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800286 if (dev->dmasound.recording_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dsp_rec_stop(dev);
288 dsp_buffer_free(dev);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800289 dev->dmasound.users_dsp--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 file->private_data = NULL;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200291 mutex_unlock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return 0;
293}
294
295static ssize_t dsp_read(struct file *file, char __user *buffer,
296 size_t count, loff_t *ppos)
297{
298 struct saa7134_dev *dev = file->private_data;
299 DECLARE_WAITQUEUE(wait, current);
300 unsigned int bytes;
301 unsigned long flags;
302 int err,ret = 0;
303
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800304 add_wait_queue(&dev->dmasound.wq, &wait);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200305 mutex_lock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 while (count > 0) {
307 /* wait for data if needed */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800308 if (0 == dev->dmasound.read_count) {
309 if (!dev->dmasound.recording_on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 err = dsp_rec_start(dev);
311 if (err < 0) {
312 if (0 == ret)
313 ret = err;
314 break;
315 }
316 }
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800317 if (dev->dmasound.recording_on &&
318 !dev->dmasound.dma_running) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* recover from overruns */
320 spin_lock_irqsave(&dev->slock,flags);
321 dsp_dma_start(dev);
322 spin_unlock_irqrestore(&dev->slock,flags);
323 }
324 if (file->f_flags & O_NONBLOCK) {
325 if (0 == ret)
326 ret = -EAGAIN;
327 break;
328 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200329 mutex_unlock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 set_current_state(TASK_INTERRUPTIBLE);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800331 if (0 == dev->dmasound.read_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 schedule();
333 set_current_state(TASK_RUNNING);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200334 mutex_lock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (signal_pending(current)) {
336 if (0 == ret)
337 ret = -EINTR;
338 break;
339 }
340 }
341
342 /* copy data to userspace */
343 bytes = count;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800344 if (bytes > dev->dmasound.read_count)
345 bytes = dev->dmasound.read_count;
346 if (bytes > dev->dmasound.bufsize - dev->dmasound.read_offset)
347 bytes = dev->dmasound.bufsize - dev->dmasound.read_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (copy_to_user(buffer + ret,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800349 dev->dmasound.dma.vmalloc + dev->dmasound.read_offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 bytes)) {
351 if (0 == ret)
352 ret = -EFAULT;
353 break;
354 }
355
356 ret += bytes;
357 count -= bytes;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800358 dev->dmasound.read_count -= bytes;
359 dev->dmasound.read_offset += bytes;
360 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
361 dev->dmasound.read_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200363 mutex_unlock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800364 remove_wait_queue(&dev->dmasound.wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return ret;
366}
367
368static ssize_t dsp_write(struct file *file, const char __user *buffer,
369 size_t count, loff_t *ppos)
370{
371 return -EINVAL;
372}
373
Michael Krufky5e453dc2006-01-09 15:32:31 -0200374static const char *osspcm_ioctls[] = {
375 "RESET", "SYNC", "SPEED", "STEREO", "GETBLKSIZE", "SETFMT",
376 "CHANNELS", "?", "POST", "SUBDIVIDE", "SETFRAGMENT", "GETFMTS",
377 "GETOSPACE", "GETISPACE", "NONBLOCK", "GETCAPS", "GET/SETTRIGGER",
378 "GETIPTR", "GETOPTR", "MAPINBUF", "MAPOUTBUF", "SETSYNCRO",
379 "SETDUPLEX", "GETODELAY"
380};
381#define OSSPCM_IOCTLS ARRAY_SIZE(osspcm_ioctls)
382
383static void saa7134_oss_print_ioctl(char *name, unsigned int cmd)
384{
385 char *dir;
386
387 switch (_IOC_DIR(cmd)) {
388 case _IOC_NONE: dir = "--"; break;
389 case _IOC_READ: dir = "r-"; break;
390 case _IOC_WRITE: dir = "-w"; break;
391 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
392 default: dir = "??"; break;
393 }
394 switch (_IOC_TYPE(cmd)) {
395 case 'P':
396 printk(KERN_DEBUG "%s: ioctl 0x%08x (oss dsp, %s, SNDCTL_DSP_%s)\n",
397 name, cmd, dir, (_IOC_NR(cmd) < OSSPCM_IOCTLS) ?
398 osspcm_ioctls[_IOC_NR(cmd)] : "???");
399 break;
400 case 'M':
401 printk(KERN_DEBUG "%s: ioctl 0x%08x (oss mixer, %s, #%d)\n",
402 name, cmd, dir, _IOC_NR(cmd));
403 break;
404 default:
405 printk(KERN_DEBUG "%s: ioctl 0x%08x (???, %s, #%d)\n",
406 name, cmd, dir, _IOC_NR(cmd));
407 }
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static int dsp_ioctl(struct inode *inode, struct file *file,
411 unsigned int cmd, unsigned long arg)
412{
413 struct saa7134_dev *dev = file->private_data;
414 void __user *argp = (void __user *) arg;
415 int __user *p = argp;
416 int val = 0;
417
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800418 if (debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -0200419 saa7134_oss_print_ioctl(dev->name,cmd);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800420 switch (cmd) {
421 case OSS_GETVERSION:
422 return put_user(SOUND_VERSION, p);
423 case SNDCTL_DSP_GETCAPS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800426 case SNDCTL_DSP_SPEED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (get_user(val, p))
428 return -EFAULT;
429 /* fall through */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800430 case SOUND_PCM_READ_RATE:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800431 return put_user(dev->dmasound.rate, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800433 case SNDCTL_DSP_STEREO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (get_user(val, p))
435 return -EFAULT;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200436 mutex_lock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800437 dev->dmasound.channels = val ? 2 : 1;
438 if (dev->dmasound.recording_on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 dsp_rec_stop(dev);
440 dsp_rec_start(dev);
441 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200442 mutex_unlock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800443 return put_user(dev->dmasound.channels-1, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800445 case SNDCTL_DSP_CHANNELS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (get_user(val, p))
447 return -EFAULT;
448 if (val != 1 && val != 2)
449 return -EINVAL;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200450 mutex_lock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800451 dev->dmasound.channels = val;
452 if (dev->dmasound.recording_on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 dsp_rec_stop(dev);
454 dsp_rec_start(dev);
455 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200456 mutex_unlock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* fall through */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800458 case SOUND_PCM_READ_CHANNELS:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800459 return put_user(dev->dmasound.channels, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800461 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return put_user(AFMT_U8 | AFMT_S8 |
463 AFMT_U16_LE | AFMT_U16_BE |
464 AFMT_S16_LE | AFMT_S16_BE, p);
465
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800466 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (get_user(val, p))
468 return -EFAULT;
469 switch (val) {
470 case AFMT_QUERY:
471 /* nothing to do */
472 break;
473 case AFMT_U8:
474 case AFMT_S8:
475 case AFMT_U16_LE:
476 case AFMT_U16_BE:
477 case AFMT_S16_LE:
478 case AFMT_S16_BE:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200479 mutex_lock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800480 dev->dmasound.afmt = val;
481 if (dev->dmasound.recording_on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 dsp_rec_stop(dev);
483 dsp_rec_start(dev);
484 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200485 mutex_unlock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800486 return put_user(dev->dmasound.afmt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 default:
488 return -EINVAL;
489 }
490
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800491 case SOUND_PCM_READ_BITS:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800492 switch (dev->dmasound.afmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 case AFMT_U8:
494 case AFMT_S8:
495 return put_user(8, p);
496 case AFMT_U16_LE:
497 case AFMT_U16_BE:
498 case AFMT_S16_LE:
499 case AFMT_S16_BE:
500 return put_user(16, p);
501 default:
502 return -EINVAL;
503 }
504
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800505 case SNDCTL_DSP_NONBLOCK:
506 file->f_flags |= O_NONBLOCK;
507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800509 case SNDCTL_DSP_RESET:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200510 mutex_lock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800511 if (dev->dmasound.recording_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dsp_rec_stop(dev);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200513 mutex_unlock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800515 case SNDCTL_DSP_GETBLKSIZE:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800516 return put_user(dev->dmasound.blksize, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800518 case SNDCTL_DSP_SETFRAGMENT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (get_user(val, p))
520 return -EFAULT;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800521 if (dev->dmasound.recording_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -EBUSY;
523 dsp_buffer_free(dev);
524 /* used to be arg >> 16 instead of val >> 16; fixed */
525 dsp_buffer_conf(dev,1 << (val & 0xffff), (val >> 16) & 0xffff);
526 dsp_buffer_init(dev);
527 return 0;
528
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800529 case SNDCTL_DSP_SYNC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* NOP */
531 return 0;
532
533 case SNDCTL_DSP_GETISPACE:
534 {
535 audio_buf_info info;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800536 info.fragsize = dev->dmasound.blksize;
537 info.fragstotal = dev->dmasound.blocks;
538 info.bytes = dev->dmasound.read_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 info.fragments = info.bytes / info.fragsize;
540 if (copy_to_user(argp, &info, sizeof(info)))
541 return -EFAULT;
542 return 0;
543 }
544 default:
545 return -EINVAL;
546 }
547}
548
549static unsigned int dsp_poll(struct file *file, struct poll_table_struct *wait)
550{
551 struct saa7134_dev *dev = file->private_data;
552 unsigned int mask = 0;
553
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800554 poll_wait(file, &dev->dmasound.wq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800556 if (0 == dev->dmasound.read_count) {
Ingo Molnar3593cab2006-02-07 06:49:14 -0200557 mutex_lock(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800558 if (!dev->dmasound.recording_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 dsp_rec_start(dev);
Ingo Molnar3593cab2006-02-07 06:49:14 -0200560 mutex_unlock(&dev->dmasound.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else
562 mask |= (POLLIN | POLLRDNORM);
563 return mask;
564}
565
Arjan van de Venfa027c22007-02-12 00:55:33 -0800566const struct file_operations saa7134_dsp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 .owner = THIS_MODULE,
568 .open = dsp_open,
569 .release = dsp_release,
570 .read = dsp_read,
571 .write = dsp_write,
572 .ioctl = dsp_ioctl,
573 .poll = dsp_poll,
574 .llseek = no_llseek,
575};
576
577/* ------------------------------------------------------------------ */
578
579static int
580mixer_recsrc_7134(struct saa7134_dev *dev)
581{
582 int analog_io,rate;
583
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800584 switch (dev->dmasound.input) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 case TV:
586 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
587 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
588 break;
589 case LINE1:
590 case LINE2:
591 case LINE2_LEFT:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800592 analog_io = (LINE1 == dev->dmasound.input) ? 0x00 : 0x08;
593 rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
595 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
596 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
597 break;
598 }
599 return 0;
600}
601
602static int
603mixer_recsrc_7133(struct saa7134_dev *dev)
604{
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700605 u32 anabar, xbarin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700607 xbarin = 0x03; // adc
608 anabar = 0;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800609 switch (dev->dmasound.input) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 case TV:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700611 xbarin = 0; // Demodulator
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800612 anabar = 2; // DACs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
614 case LINE1:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700615 anabar = 0; // aux1, aux1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617 case LINE2:
618 case LINE2_LEFT:
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700619 anabar = 9; // aux2, aux2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700622 /* output xbar always main channel */
623 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbb10);
624 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
625 saa_writel(0x594 >> 2, anabar);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return 0;
628}
629
630static int
631mixer_recsrc(struct saa7134_dev *dev, enum saa7134_audio_in src)
632{
633 static const char *iname[] = { "Oops", "TV", "LINE1", "LINE2" };
634
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800635 dev->dmasound.count++;
636 dev->dmasound.input = src;
637 dprintk("mixer input = %s\n",iname[dev->dmasound.input]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 switch (dev->pci->device) {
640 case PCI_DEVICE_ID_PHILIPS_SAA7134:
641 mixer_recsrc_7134(dev);
642 break;
643 case PCI_DEVICE_ID_PHILIPS_SAA7133:
644 case PCI_DEVICE_ID_PHILIPS_SAA7135:
645 mixer_recsrc_7133(dev);
646 break;
647 }
648 return 0;
649}
650
651static int
652mixer_level(struct saa7134_dev *dev, enum saa7134_audio_in src, int level)
653{
654 switch (dev->pci->device) {
655 case PCI_DEVICE_ID_PHILIPS_SAA7134:
656 switch (src) {
657 case TV:
658 /* nothing */
659 break;
660 case LINE1:
661 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
662 (100 == level) ? 0x00 : 0x10);
663 break;
664 case LINE2:
665 case LINE2_LEFT:
666 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
667 (100 == level) ? 0x00 : 0x20);
668 break;
669 }
670 break;
671 case PCI_DEVICE_ID_PHILIPS_SAA7133:
672 case PCI_DEVICE_ID_PHILIPS_SAA7135:
673 /* nothing */
674 break;
675 }
676 return 0;
677}
678
679/* ------------------------------------------------------------------ */
680
681static int mixer_open(struct inode *inode, struct file *file)
682{
683 int minor = iminor(inode);
684 struct saa7134_dev *h,*dev = NULL;
685 struct list_head *list;
686
687 list_for_each(list,&saa7134_devlist) {
688 h = list_entry(list, struct saa7134_dev, devlist);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800689 if (h->dmasound.minor_mixer == minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 dev = h;
691 }
692 if (NULL == dev)
693 return -ENODEV;
694
695 file->private_data = dev;
696 return 0;
697}
698
699static int mixer_release(struct inode *inode, struct file *file)
700{
701 file->private_data = NULL;
702 return 0;
703}
704
705static int mixer_ioctl(struct inode *inode, struct file *file,
706 unsigned int cmd, unsigned long arg)
707{
708 struct saa7134_dev *dev = file->private_data;
709 enum saa7134_audio_in input;
710 int val,ret;
711 void __user *argp = (void __user *) arg;
712 int __user *p = argp;
713
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800714 if (debug > 1)
Michael Krufky5e453dc2006-01-09 15:32:31 -0200715 saa7134_oss_print_ioctl(dev->name,cmd);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800716 switch (cmd) {
717 case OSS_GETVERSION:
718 return put_user(SOUND_VERSION, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 case SOUND_MIXER_INFO:
720 {
721 mixer_info info;
722 memset(&info,0,sizeof(info));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800723 strlcpy(info.id, "TV audio", sizeof(info.id));
724 strlcpy(info.name, dev->name, sizeof(info.name));
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800725 info.modify_counter = dev->dmasound.count;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800726 if (copy_to_user(argp, &info, sizeof(info)))
727 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729 }
730 case SOUND_OLD_MIXER_INFO:
731 {
732 _old_mixer_info info;
733 memset(&info,0,sizeof(info));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800734 strlcpy(info.id, "TV audio", sizeof(info.id));
735 strlcpy(info.name, dev->name, sizeof(info.name));
736 if (copy_to_user(argp, &info, sizeof(info)))
737 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 0;
739 }
740 case MIXER_READ(SOUND_MIXER_CAPS):
741 return put_user(SOUND_CAP_EXCL_INPUT, p);
742 case MIXER_READ(SOUND_MIXER_STEREODEVS):
743 return put_user(0, p);
744 case MIXER_READ(SOUND_MIXER_RECMASK):
745 case MIXER_READ(SOUND_MIXER_DEVMASK):
746 val = SOUND_MASK_LINE1 | SOUND_MASK_LINE2;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800747 if (32000 == dev->dmasound.rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 val |= SOUND_MASK_VIDEO;
749 return put_user(val, p);
750
751 case MIXER_WRITE(SOUND_MIXER_RECSRC):
752 if (get_user(val, p))
753 return -EFAULT;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800754 input = dev->dmasound.input;
755 if (32000 == dev->dmasound.rate &&
756 val & SOUND_MASK_VIDEO && dev->dmasound.input != TV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 input = TV;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800758 if (val & SOUND_MASK_LINE1 && dev->dmasound.input != LINE1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 input = LINE1;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800760 if (val & SOUND_MASK_LINE2 && dev->dmasound.input != LINE2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 input = LINE2;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800762 if (input != dev->dmasound.input)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 mixer_recsrc(dev,input);
764 /* fall throuth */
765 case MIXER_READ(SOUND_MIXER_RECSRC):
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800766 switch (dev->dmasound.input) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 case TV: ret = SOUND_MASK_VIDEO; break;
768 case LINE1: ret = SOUND_MASK_LINE1; break;
769 case LINE2: ret = SOUND_MASK_LINE2; break;
770 default: ret = 0;
771 }
772 return put_user(ret, p);
773
774 case MIXER_WRITE(SOUND_MIXER_VIDEO):
775 case MIXER_READ(SOUND_MIXER_VIDEO):
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800776 if (32000 != dev->dmasound.rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -EINVAL;
778 return put_user(100 | 100 << 8, p);
779
780 case MIXER_WRITE(SOUND_MIXER_LINE1):
781 if (get_user(val, p))
782 return -EFAULT;
783 val &= 0xff;
784 val = (val <= 50) ? 50 : 100;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800785 dev->dmasound.line1 = val;
786 mixer_level(dev,LINE1,dev->dmasound.line1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* fall throuth */
788 case MIXER_READ(SOUND_MIXER_LINE1):
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800789 return put_user(dev->dmasound.line1 | dev->dmasound.line1 << 8, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 case MIXER_WRITE(SOUND_MIXER_LINE2):
792 if (get_user(val, p))
793 return -EFAULT;
794 val &= 0xff;
795 val = (val <= 50) ? 50 : 100;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800796 dev->dmasound.line2 = val;
797 mixer_level(dev,LINE2,dev->dmasound.line2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 /* fall throuth */
799 case MIXER_READ(SOUND_MIXER_LINE2):
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800800 return put_user(dev->dmasound.line2 | dev->dmasound.line2 << 8, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 default:
803 return -EINVAL;
804 }
805}
806
Arjan van de Venfa027c22007-02-12 00:55:33 -0800807const struct file_operations saa7134_mixer_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 .owner = THIS_MODULE,
809 .open = mixer_open,
810 .release = mixer_release,
811 .ioctl = mixer_ioctl,
812 .llseek = no_llseek,
813};
814
815/* ------------------------------------------------------------------ */
816
David Howells7d12e782006-10-05 14:55:46 +0100817static irqreturn_t saa7134_oss_irq(int irq, void *dev_id)
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800818{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800819 struct saa7134_dmasound *dmasound = dev_id;
820 struct saa7134_dev *dev = dmasound->priv_data;
821 unsigned long report, status;
822 int loop, handled = 0;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800823
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800824 for (loop = 0; loop < 10; loop++) {
825 report = saa_readl(SAA7134_IRQ_REPORT);
826 status = saa_readl(SAA7134_IRQ_STATUS);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800827
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800828 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800829 handled = 1;
830 saa_writel(SAA7134_IRQ_REPORT,report);
831 saa7134_irq_oss_done(dev, status);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800832 } else {
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800833 goto out;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800834 }
835 }
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800836
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800837 if (loop == 10) {
838 dprintk("error! looping IRQ!");
839 }
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800840out:
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800841 return IRQ_RETVAL(handled);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844int saa7134_oss_init1(struct saa7134_dev *dev)
845{
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800846
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800847 if ((request_irq(dev->pci->irq, saa7134_oss_irq,
Thomas Gleixner8076fe32006-07-01 19:29:37 -0700848 IRQF_SHARED | IRQF_DISABLED, dev->name,
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800849 (void*) &dev->dmasound)) < 0)
850 return -1;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /* general */
Ingo Molnar3593cab2006-02-07 06:49:14 -0200853 mutex_init(&dev->dmasound.lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800854 init_waitqueue_head(&dev->dmasound.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 switch (dev->pci->device) {
857 case PCI_DEVICE_ID_PHILIPS_SAA7133:
858 case PCI_DEVICE_ID_PHILIPS_SAA7135:
859 saa_writel(0x588 >> 2, 0x00000fff);
860 saa_writel(0x58c >> 2, 0x00543210);
861 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbbbb);
862 break;
863 }
864
865 /* dsp */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800866 dev->dmasound.rate = 32000;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800867 if (rate)
868 dev->dmasound.rate = rate;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800869 dev->dmasound.rate = (dev->dmasound.rate > 40000) ? 48000 : 32000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* mixer */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800872 dev->dmasound.line1 = 50;
873 dev->dmasound.line2 = 50;
874 mixer_level(dev,LINE1,dev->dmasound.line1);
875 mixer_level(dev,LINE2,dev->dmasound.line2);
876 mixer_recsrc(dev, (dev->dmasound.rate == 32000) ? TV : LINE2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return 0;
879}
880
881int saa7134_oss_fini(struct saa7134_dev *dev)
882{
883 /* nothing */
884 return 0;
885}
886
887void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status)
888{
889 int next_blk, reg = 0;
890
891 spin_lock(&dev->slock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800892 if (UNSET == dev->dmasound.dma_blk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 dprintk("irq: recording stopped\n");
894 goto done;
895 }
896 if (0 != (status & 0x0f000000))
897 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
898 if (0 == (status & 0x10000000)) {
899 /* odd */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800900 if (0 == (dev->dmasound.dma_blk & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 reg = SAA7134_RS_BA1(6);
902 } else {
903 /* even */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800904 if (1 == (dev->dmasound.dma_blk & 0x01))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 reg = SAA7134_RS_BA2(6);
906 }
907 if (0 == reg) {
908 dprintk("irq: field oops [%s]\n",
909 (status & 0x10000000) ? "even" : "odd");
910 goto done;
911 }
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800912 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
913 dprintk("irq: overrun [full=%d/%d]\n",dev->dmasound.read_count,
914 dev->dmasound.bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 dsp_dma_stop(dev);
916 goto done;
917 }
918
919 /* next block addr */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800920 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
921 saa_writel(reg,next_blk * dev->dmasound.blksize);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800922 if (debug > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 dprintk("irq: ok, %s, next_blk=%d, addr=%x\n",
924 (status & 0x10000000) ? "even" : "odd ", next_blk,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800925 next_blk * dev->dmasound.blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 /* update status & wake waiting readers */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800928 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
929 dev->dmasound.read_count += dev->dmasound.blksize;
930 wake_up(&dev->dmasound.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 done:
933 spin_unlock(&dev->slock);
934}
935
Adrian Bunk943a4902005-12-01 00:51:35 -0800936static int saa7134_dsp_create(struct saa7134_dev *dev)
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800937{
938 int err;
939
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -0800940 err = dev->dmasound.minor_dsp =
941 register_sound_dsp(&saa7134_dsp_fops,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800942 dsp_nr[dev->nr]);
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -0800943 if (err < 0) {
944 goto fail;
945 }
946 printk(KERN_INFO "%s: registered device dsp%d\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800947 dev->name,dev->dmasound.minor_dsp >> 4);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800948
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -0800949 err = dev->dmasound.minor_mixer =
950 register_sound_mixer(&saa7134_mixer_fops,
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800951 mixer_nr[dev->nr]);
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -0800952 if (err < 0)
953 goto fail;
954 printk(KERN_INFO "%s: registered device mixer%d\n",
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800955 dev->name,dev->dmasound.minor_mixer >> 4);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800956
957 return 0;
958
959fail:
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800960 unregister_sound_dsp(dev->dmasound.minor_dsp);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800961 return 0;
962
963
964}
965
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -0800966static int oss_device_init(struct saa7134_dev *dev)
967{
968 dev->dmasound.priv_data = dev;
969 saa7134_oss_init1(dev);
970 saa7134_dsp_create(dev);
971 return 1;
972}
973
974static int oss_device_exit(struct saa7134_dev *dev)
975{
976
977 unregister_sound_mixer(dev->dmasound.minor_mixer);
978 unregister_sound_dsp(dev->dmasound.minor_dsp);
979
980 saa7134_oss_fini(dev);
981
982 if (dev->pci->irq > 0) {
983 synchronize_irq(dev->pci->irq);
984 free_irq(dev->pci->irq,&dev->dmasound);
985 }
986
987 dev->dmasound.priv_data = NULL;
988 return 1;
989}
990
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800991static int saa7134_oss_init(void)
992{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800993 struct saa7134_dev *dev = NULL;
994 struct list_head *list;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800995
Adrian Bunk78457012006-07-15 15:19:56 -0300996 if (!saa7134_dmasound_init && !saa7134_dmasound_exit) {
997 saa7134_dmasound_init = oss_device_init;
998 saa7134_dmasound_exit = oss_device_exit;
Ricardo Cerqueira7bb95292005-12-20 18:26:26 -0200999 } else {
1000 printk(KERN_WARNING "saa7134 OSS: can't load, DMA sound handler already assigned (probably to ALSA)\n");
1001 return -EBUSY;
1002 }
1003
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001004 printk(KERN_INFO "saa7134 OSS driver for DMA sound loaded\n");
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001005
Ricardo Cerqueira7bb95292005-12-20 18:26:26 -02001006
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001007 list_for_each(list,&saa7134_devlist) {
1008 dev = list_entry(list, struct saa7134_dev, devlist);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001009 if (dev->dmasound.priv_data == NULL) {
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -08001010 oss_device_init(dev);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001011 } else {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001012 printk(KERN_ERR "saa7134 OSS: DMA sound is being handled by ALSA, ignoring %s\n",dev->name);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001013 return -EBUSY;
1014 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001015 }
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001016
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001017 if (dev == NULL)
1018 printk(KERN_INFO "saa7134 OSS: no saa7134 cards found\n");
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001019
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001020 return 0;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001021
1022}
1023
Adrian Bunk943a4902005-12-01 00:51:35 -08001024static void saa7134_oss_exit(void)
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001025{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001026 struct saa7134_dev *dev = NULL;
1027 struct list_head *list;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001028
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001029 list_for_each(list,&saa7134_devlist) {
1030 dev = list_entry(list, struct saa7134_dev, devlist);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001031
1032 /* Device isn't registered by OSS, probably ALSA's */
1033 if (!dev->dmasound.minor_dsp)
1034 continue;
1035
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -08001036 oss_device_exit(dev);
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001037
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001038 }
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001039
Adrian Bunk78457012006-07-15 15:19:56 -03001040 saa7134_dmasound_init = NULL;
1041 saa7134_dmasound_exit = NULL;
Ricardo Cerqueira7bb95292005-12-20 18:26:26 -02001042
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001043 printk(KERN_INFO "saa7134 OSS driver for DMA sound unloaded\n");
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001044
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08001045 return;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001046}
1047
Linus Torvaldsc7ac6b42005-12-21 14:52:32 -08001048/* We initialize this late, to make sure the sound system is up and running */
1049late_initcall(saa7134_oss_init);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001050module_exit(saa7134_oss_exit);
1051MODULE_LICENSE("GPL");
1052MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/* ----------------------------------------------------------- */
1055/*
1056 * Local variables:
1057 * c-basic-offset: 8
1058 * End:
1059 */