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