aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/cx231xx/cx231xx-audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/video/cx231xx/cx231xx-audio.c')
-rw-r--r--drivers/media/video/cx231xx/cx231xx-audio.c581
1 files changed, 581 insertions, 0 deletions
diff --git a/drivers/media/video/cx231xx/cx231xx-audio.c b/drivers/media/video/cx231xx/cx231xx-audio.c
new file mode 100644
index 000000000000..e4335e2a4103
--- /dev/null
+++ b/drivers/media/video/cx231xx/cx231xx-audio.c
@@ -0,0 +1,581 @@
1/*
2 * Conexant Cx231xx audio extension
3 *
4 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5 * Based on em28xx driver
6 *
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/kernel.h>
24#include <linux/usb.h>
25#include <linux/init.h>
26#include <linux/sound.h>
27#include <linux/spinlock.h>
28#include <linux/soundcard.h>
29#include <linux/slab.h>
30#include <linux/vmalloc.h>
31#include <linux/proc_fs.h>
32#include <linux/module.h>
33#include <sound/core.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/info.h>
37#include <sound/initval.h>
38#include <sound/control.h>
39#include <media/v4l2-common.h>
40#include "cx231xx.h"
41#include "cx231xx-pcb-config.h"
42
43static int debug;
44module_param(debug, int, 0644);
45MODULE_PARM_DESC(debug, "activates debug info");
46
47#define dprintk(fmt, arg...) do { \
48 if (debug) \
49 printk(KERN_INFO "cx231xx-audio %s: " fmt, \
50 __func__, ##arg); \
51 } while (0)
52
53static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
54
55static int cx231xx_isoc_audio_deinit(struct cx231xx *dev)
56{
57 int i;
58
59 dprintk("Stopping isoc\n");
60
61
62 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
63 if(dev->adev.urb[i]) {
64 if (!irqs_disabled())
65 usb_kill_urb(dev->adev.urb[i]);
66 else
67 usb_unlink_urb(dev->adev.urb[i]);
68
69 usb_free_urb(dev->adev.urb[i]);
70 dev->adev.urb[i] = NULL;
71
72 kfree(dev->adev.transfer_buffer[i]);
73 dev->adev.transfer_buffer[i] = NULL;
74
75 }
76 }
77
78 return 0;
79}
80
81static void cx231xx_audio_isocirq(struct urb *urb)
82{
83 struct cx231xx *dev = urb->context;
84 int i;
85 unsigned int oldptr;
86 int period_elapsed = 0;
87 int status;
88 unsigned char *cp;
89 unsigned int stride;
90 struct snd_pcm_substream *substream;
91 struct snd_pcm_runtime *runtime;
92
93 switch (urb->status) {
94 case 0: /* success */
95 case -ETIMEDOUT: /* NAK */
96 break;
97 case -ECONNRESET: /* kill */
98 case -ENOENT:
99 case -ESHUTDOWN:
100 return;
101 default: /* error */
102 dprintk("urb completition error %d.\n", urb->status);
103 break;
104 }
105
106 if (dev->adev.capture_pcm_substream) {
107 substream = dev->adev.capture_pcm_substream;
108 runtime = substream->runtime;
109 stride = runtime->frame_bits >> 3;
110
111 for (i = 0; i < urb->number_of_packets; i++) {
112 int length =
113 urb->iso_frame_desc[i].actual_length / stride;
114 cp = (unsigned char *)urb->transfer_buffer +
115 urb->iso_frame_desc[i].offset;
116
117 if (!length)
118 continue;
119
120 oldptr = dev->adev.hwptr_done_capture;
121 if (oldptr + length >= runtime->buffer_size) {
122 unsigned int cnt =
123 runtime->buffer_size - oldptr;
124 memcpy(runtime->dma_area + oldptr * stride, cp,
125 cnt * stride);
126 memcpy(runtime->dma_area, cp + cnt * stride,
127 length * stride - cnt * stride);
128 } else {
129 memcpy(runtime->dma_area + oldptr * stride, cp,
130 length * stride);
131 }
132
133 snd_pcm_stream_lock(substream);
134
135 dev->adev.hwptr_done_capture += length;
136 if (dev->adev.hwptr_done_capture >=
137 runtime->buffer_size)
138 dev->adev.hwptr_done_capture -=
139 runtime->buffer_size;
140
141 dev->adev.capture_transfer_done += length;
142 if (dev->adev.capture_transfer_done >=
143 runtime->period_size) {
144 dev->adev.capture_transfer_done -=
145 runtime->period_size;
146 period_elapsed = 1;
147 }
148
149 snd_pcm_stream_unlock(substream);
150 }
151 if (period_elapsed)
152 snd_pcm_period_elapsed(substream);
153 }
154 urb->status = 0;
155
156 status = usb_submit_urb(urb, GFP_ATOMIC);
157 if (status < 0) {
158 cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
159 status);
160 }
161 return;
162}
163
164static int cx231xx_init_audio_isoc(struct cx231xx *dev)
165{
166 int i, errCode;
167 int sb_size;
168
169 cx231xx_info("%s: Starting AUDIO transfers\n",__func__);
170
171 sb_size = CX231XX_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
172
173 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
174 struct urb *urb;
175 int j, k;
176
177 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
178 if (!dev->adev.transfer_buffer[i])
179 return -ENOMEM;
180
181 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
182 urb = usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
183 if (!urb) {
184 cx231xx_errdev("usb_alloc_urb failed!\n");
185 for (j = 0; j < i; j++) {
186 usb_free_urb(dev->adev.urb[j]);
187 kfree(dev->adev.transfer_buffer[j]);
188 }
189 return -ENOMEM;
190 }
191
192 urb->dev = dev->udev;
193 urb->context = dev;
194 urb->pipe = usb_rcvisocpipe(dev->udev, dev->adev.end_point_addr);
195 urb->transfer_flags = URB_ISO_ASAP;
196 urb->transfer_buffer = dev->adev.transfer_buffer[i];
197 urb->interval = 1;
198 urb->complete = cx231xx_audio_isocirq;
199 urb->number_of_packets = CX231XX_NUM_AUDIO_PACKETS;
200 urb->transfer_buffer_length = sb_size;
201
202 for (j = k = 0; j < CX231XX_NUM_AUDIO_PACKETS;
203 j++, k += dev->adev.max_pkt_size) {
204 urb->iso_frame_desc[j].offset = k;
205 urb->iso_frame_desc[j].length =
206 dev->adev.max_pkt_size;
207 }
208 dev->adev.urb[i] = urb;
209 }
210
211 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
212 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
213 if (errCode < 0) {
214 cx231xx_isoc_audio_deinit(dev);
215 return errCode;
216 }
217 }
218
219 return errCode;
220}
221
222static int cx231xx_cmd(struct cx231xx *dev, int cmd, int arg)
223{
224 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON)?
225 "stop" : "start");
226
227 switch (cmd) {
228 case CX231XX_CAPTURE_STREAM_EN:
229 if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
230 dev->adev.capture_stream = STREAM_ON;
231 cx231xx_init_audio_isoc(dev);
232 } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
233 dev->adev.capture_stream = STREAM_OFF;
234 cx231xx_isoc_audio_deinit(dev);
235 } else {
236 cx231xx_errdev( "An underrun very likely occurred. "
237 "Ignoring it.\n");
238 }
239 return 0;
240 default:
241 return -EINVAL;
242 }
243}
244
245static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
246 size_t size)
247{
248 struct snd_pcm_runtime *runtime = subs->runtime;
249
250 dprintk("Allocating vbuffer\n");
251 if (runtime->dma_area) {
252 if (runtime->dma_bytes > size)
253 return 0;
254
255 vfree(runtime->dma_area);
256 }
257 runtime->dma_area = vmalloc(size);
258 if (!runtime->dma_area)
259 return -ENOMEM;
260
261 runtime->dma_bytes = size;
262
263 return 0;
264}
265
266static struct snd_pcm_hardware snd_cx231xx_hw_capture = {
267 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
268 SNDRV_PCM_INFO_MMAP |
269 SNDRV_PCM_INFO_INTERLEAVED |
270 SNDRV_PCM_INFO_MMAP_VALID,
271
272 .formats = SNDRV_PCM_FMTBIT_S16_LE,
273
274 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
275
276 .rate_min = 48000,
277 .rate_max = 48000,
278 .channels_min = 2,
279 .channels_max = 2,
280 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
281 .period_bytes_min = 64, /* 12544/2, */
282 .period_bytes_max = 12544,
283 .periods_min = 2,
284 .periods_max = 98, /* 12544, */
285};
286
287static int snd_cx231xx_capture_open(struct snd_pcm_substream *substream)
288{
289 struct cx231xx *dev = snd_pcm_substream_chip(substream);
290 struct snd_pcm_runtime *runtime = substream->runtime;
291 int ret = 0;
292
293 dprintk("opening device and trying to acquire exclusive lock\n");
294
295 if (!dev) {
296 cx231xx_errdev("BUG: cx231xx can't find device struct."
297 " Can't proceed with open\n");
298 return -ENODEV;
299 }
300
301 /* Sets volume, mute, etc */
302 dev->mute = 0;
303
304 /* set alternate setting for audio interface */
305 ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 1); /* 1 - 48000 samples per sec */
306 if (ret < 0) {
307 cx231xx_errdev("failed to set alternate setting !\n");
308
309 return ret;
310 }
311
312 /* inform hardware to start streaming */
313 ret = cx231xx_capture_start(dev, 1, Audio);
314
315 runtime->hw = snd_cx231xx_hw_capture;
316
317 mutex_lock(&dev->lock);
318 dev->adev.users++;
319 mutex_unlock(&dev->lock);
320
321 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
322 dev->adev.capture_pcm_substream = substream;
323 runtime->private_data = dev;
324
325 return 0;
326}
327
328static int snd_cx231xx_pcm_close(struct snd_pcm_substream *substream)
329{
330 int ret;
331 struct cx231xx *dev = snd_pcm_substream_chip(substream);
332
333
334 dprintk("closing device\n");
335
336 /* set alternate setting for audio interface */
337 ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0); /* 1 - 48000 samples per sec */
338 if (ret < 0) {
339 cx231xx_errdev("failed to set alternate setting !\n");
340
341 return ret;
342 }
343
344 /* inform hardware to start streaming */
345 ret = cx231xx_capture_start(dev, 0, Audio);
346
347 dev->mute = 1;
348 mutex_lock(&dev->lock);
349 dev->adev.users--;
350 mutex_unlock(&dev->lock);
351
352 if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
353 dprintk("audio users: %d\n", dev->adev.users);
354 dprintk("disabling audio stream!\n");
355 dev->adev.shutdown = 0;
356 dprintk("released lock\n");
357 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, 0);
358 }
359 return 0;
360}
361
362static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream *substream,
363 struct snd_pcm_hw_params *hw_params)
364{
365 unsigned int channels, rate, format;
366 int ret;
367
368 dprintk("Setting capture parameters\n");
369
370 ret = snd_pcm_alloc_vmalloc_buffer(substream,
371 params_buffer_bytes(hw_params));
372 format = params_format(hw_params);
373 rate = params_rate(hw_params);
374 channels = params_channels(hw_params);
375
376 /* TODO: set up cx231xx audio chip to deliver the correct audio format,
377 current default is 48000hz multiplexed => 96000hz mono
378 which shouldn't matter since analogue TV only supports mono */
379 return 0;
380}
381
382static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream *substream)
383{
384 struct cx231xx *dev = snd_pcm_substream_chip(substream);
385
386 dprintk("Stop capture, if needed\n");
387
388 if (dev->adev.capture_stream == STREAM_ON)
389 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, CX231XX_STOP_AUDIO);
390
391 return 0;
392}
393
394static int snd_cx231xx_prepare(struct snd_pcm_substream *substream)
395{
396 return 0;
397}
398
399static int snd_cx231xx_capture_trigger(struct snd_pcm_substream *substream,
400 int cmd)
401{
402 struct cx231xx *dev = snd_pcm_substream_chip(substream);
403 int retval;
404
405
406 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
407 "start": "stop");
408
409 spin_lock(&dev->adev.slock);
410 switch (cmd) {
411 case SNDRV_PCM_TRIGGER_START:
412 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, CX231XX_START_AUDIO);
413 retval = 0;
414 break;
415 case SNDRV_PCM_TRIGGER_STOP:
416 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, CX231XX_STOP_AUDIO);
417 retval = 0;
418 break;
419 default:
420 retval = -EINVAL;
421 }
422
423 spin_unlock(&dev->adev.slock);
424 return retval;
425}
426
427static snd_pcm_uframes_t snd_cx231xx_capture_pointer(struct snd_pcm_substream
428 *substream)
429{
430 struct cx231xx *dev;
431 unsigned long flags;
432 snd_pcm_uframes_t hwptr_done;
433
434 dev = snd_pcm_substream_chip(substream);
435
436 spin_lock_irqsave(&dev->adev.slock, flags);
437 hwptr_done = dev->adev.hwptr_done_capture;
438 spin_unlock_irqrestore(&dev->adev.slock, flags);
439
440 return hwptr_done;
441}
442
443static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
444 unsigned long offset)
445{
446 void *pageptr = subs->runtime->dma_area + offset;
447
448 return vmalloc_to_page(pageptr);
449}
450
451static struct snd_pcm_ops snd_cx231xx_pcm_capture = {
452 .open = snd_cx231xx_capture_open,
453 .close = snd_cx231xx_pcm_close,
454 .ioctl = snd_pcm_lib_ioctl,
455 .hw_params = snd_cx231xx_hw_capture_params,
456 .hw_free = snd_cx231xx_hw_capture_free,
457 .prepare = snd_cx231xx_prepare,
458 .trigger = snd_cx231xx_capture_trigger,
459 .pointer = snd_cx231xx_capture_pointer,
460 .page = snd_pcm_get_vmalloc_page,
461};
462
463static int cx231xx_audio_init(struct cx231xx *dev)
464{
465 struct cx231xx_audio *adev = &dev->adev;
466 struct snd_pcm *pcm;
467 struct snd_card *card;
468 static int devnr;
469 int err;
470 struct usb_interface *uif;
471 int i, isoc_pipe = 0;
472
473 if (dev->has_alsa_audio != 1) {
474 /* This device does not support the extension (in this case
475 the device is expecting the snd-usb-audio module or
476 doesn't have analog audio support at all) */
477 return 0;
478 }
479
480 cx231xx_info("cx231xx-audio.c: probing for cx231xx "
481 "non standard usbaudio\n");
482
483 card = snd_card_new(index[devnr], "Cx231xx Audio", THIS_MODULE, 0);
484 if (card == NULL) {
485 return -ENOMEM;
486 }
487
488 spin_lock_init(&adev->slock);
489 err = snd_pcm_new(card, "Cx231xx Audio", 0, 0, 1, &pcm);
490 if (err < 0) {
491 snd_card_free(card);
492 return err;
493 }
494
495 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx231xx_pcm_capture);
496 pcm->info_flags = 0;
497 pcm->private_data = dev;
498 strcpy(pcm->name, "Conexant cx231xx Capture");
499 strcpy(card->driver, "Conexant cx231xx Audio");
500 strcpy(card->shortname, "Cx231xx Audio");
501 strcpy(card->longname, "Conexant cx231xx Audio");
502
503 err = snd_card_register(card);
504 if (err < 0) {
505 snd_card_free(card);
506 return err;
507 }
508 adev->sndcard = card;
509 adev->udev = dev->udev;
510
511 /* compute alternate max packet sizes for Audio */
512 uif = dev->udev->actconfig->interface[dev->current_pcb_config.hs_config_info[0].interface_info.audio_index+1];
513
514 adev->end_point_addr = le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.bEndpointAddress);
515
516 adev->num_alt = uif->num_altsetting;
517 cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n", adev->end_point_addr,
518 adev->num_alt);
519 adev->alt_max_pkt_size = kmalloc(32 * adev->num_alt, GFP_KERNEL);
520
521 if (adev->alt_max_pkt_size == NULL) {
522 cx231xx_errdev("out of memory!\n");
523 return -ENOMEM;
524 }
525
526 for (i = 0; i < adev->num_alt ; i++) {
527 u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
528 wMaxPacketSize);
529 adev->alt_max_pkt_size[i] =
530 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
531 cx231xx_info("Alternate setting %i, max size= %i\n", i,
532 adev->alt_max_pkt_size[i]);
533 }
534
535 return 0;
536}
537
538static int cx231xx_audio_fini(struct cx231xx *dev)
539{
540 if (dev == NULL)
541 return 0;
542
543 if (dev->has_alsa_audio != 1) {
544 /* This device does not support the extension (in this case
545 the device is expecting the snd-usb-audio module or
546 doesn't have analog audio support at all) */
547 return 0;
548 }
549
550 if (dev->adev.sndcard) {
551 snd_card_free(dev->adev.sndcard);
552 kfree(dev->adev.alt_max_pkt_size);
553 dev->adev.sndcard = NULL;
554 }
555
556 return 0;
557}
558
559static struct cx231xx_ops audio_ops = {
560 .id = CX231XX_AUDIO,
561 .name = "Cx231xx Audio Extension",
562 .init = cx231xx_audio_init,
563 .fini = cx231xx_audio_fini,
564};
565
566static int __init cx231xx_alsa_register(void)
567{
568 return cx231xx_register_extension(&audio_ops);
569}
570
571static void __exit cx231xx_alsa_unregister(void)
572{
573 cx231xx_unregister_extension(&audio_ops);
574}
575
576MODULE_LICENSE("GPL");
577MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
578MODULE_DESCRIPTION("Cx231xx Audio driver");
579
580module_init(cx231xx_alsa_register);
581module_exit(cx231xx_alsa_unregister);