Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / media / video / em28xx / em28xx-audio.c
blob8e4083c0e8c1aea5948a95f289868249cba89870
1 /*
2 * Empiatech em28x1 audio extension
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
6 * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7 * - Port to work with the in-kernel driver
8 * - Several cleanups
10 * This driver is based on my previous au600 usb pstn audio driver
11 * and inherits all the copyrights
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/kernel.h>
29 #include <linux/usb.h>
30 #include <linux/init.h>
31 #include <linux/sound.h>
32 #include <linux/spinlock.h>
33 #include <linux/soundcard.h>
34 #include <linux/slab.h>
35 #include <linux/vmalloc.h>
36 #include <linux/proc_fs.h>
37 #include <linux/module.h>
38 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-audio.c
39 #include <sound/driver.h>
40 =======
41 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-audio.c
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/info.h>
46 #include <sound/initval.h>
47 #include <sound/control.h>
48 #include <media/v4l2-common.h>
49 #include "em28xx.h"
51 static int debug;
52 module_param(debug, int, 0644);
53 MODULE_PARM_DESC(debug, "activates debug info");
55 #define dprintk(fmt, arg...) do { \
56 if (debug) \
57 printk(KERN_INFO "em28xx-audio %s: " fmt, \
58 __FUNCTION__, ##arg); \
59 } while (0)
61 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
63 static int em28xx_isoc_audio_deinit(struct em28xx *dev)
65 int i;
67 dprintk("Stopping isoc\n");
68 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
69 usb_kill_urb(dev->adev->urb[i]);
70 usb_free_urb(dev->adev->urb[i]);
71 dev->adev->urb[i] = NULL;
74 return 0;
77 static void em28xx_audio_isocirq(struct urb *urb)
79 struct em28xx *dev = urb->context;
80 int i;
81 unsigned int oldptr;
82 unsigned long flags;
83 int period_elapsed = 0;
84 int status;
85 unsigned char *cp;
86 unsigned int stride;
87 struct snd_pcm_substream *substream;
88 struct snd_pcm_runtime *runtime;
89 if (dev->adev->capture_pcm_substream) {
90 substream = dev->adev->capture_pcm_substream;
91 runtime = substream->runtime;
92 stride = runtime->frame_bits >> 3;
94 for (i = 0; i < urb->number_of_packets; i++) {
95 int length =
96 urb->iso_frame_desc[i].actual_length / stride;
97 cp = (unsigned char *)urb->transfer_buffer +
98 urb->iso_frame_desc[i].offset;
100 if (!length)
101 continue;
103 spin_lock_irqsave(&dev->adev->slock, flags);
105 oldptr = dev->adev->hwptr_done_capture;
106 dev->adev->hwptr_done_capture += length;
107 if (dev->adev->hwptr_done_capture >=
108 runtime->buffer_size)
109 dev->adev->hwptr_done_capture -=
110 runtime->buffer_size;
112 dev->adev->capture_transfer_done += length;
113 if (dev->adev->capture_transfer_done >=
114 runtime->period_size) {
115 dev->adev->capture_transfer_done -=
116 runtime->period_size;
117 period_elapsed = 1;
120 spin_unlock_irqrestore(&dev->adev->slock, flags);
122 if (oldptr + length >= runtime->buffer_size) {
123 unsigned int cnt =
124 runtime->buffer_size - oldptr - 1;
125 memcpy(runtime->dma_area + oldptr * stride, cp,
126 cnt * stride);
127 memcpy(runtime->dma_area, cp + cnt,
128 length * stride - cnt * stride);
129 } else {
130 memcpy(runtime->dma_area + oldptr * stride, cp,
131 length * stride);
134 if (period_elapsed)
135 snd_pcm_period_elapsed(substream);
137 urb->status = 0;
139 if (dev->adev->shutdown)
140 return;
142 status = usb_submit_urb(urb, GFP_ATOMIC);
143 if (status < 0) {
144 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
145 status);
147 return;
150 static int em28xx_init_audio_isoc(struct em28xx *dev)
152 int i, errCode;
153 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
154 EM28XX_AUDIO_MAX_PACKET_SIZE;
156 dprintk("Starting isoc transfers\n");
158 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
159 struct urb *urb;
160 int j, k;
162 dev->adev->transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
163 if (!dev->adev->transfer_buffer[i])
164 return -ENOMEM;
166 memset(dev->adev->transfer_buffer[i], 0x80, sb_size);
167 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
168 if (!urb)
169 return -ENOMEM;
171 urb->dev = dev->udev;
172 urb->context = dev;
173 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
174 urb->transfer_flags = URB_ISO_ASAP;
175 urb->transfer_buffer = dev->adev->transfer_buffer[i];
176 urb->interval = 1;
177 urb->complete = em28xx_audio_isocirq;
178 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
179 urb->transfer_buffer_length = sb_size;
181 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
182 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
183 urb->iso_frame_desc[j].offset = k;
184 urb->iso_frame_desc[j].length =
185 EM28XX_AUDIO_MAX_PACKET_SIZE;
187 dev->adev->urb[i] = urb;
190 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
191 errCode = usb_submit_urb(dev->adev->urb[i], GFP_ATOMIC);
192 if (errCode) {
193 em28xx_isoc_audio_deinit(dev);
195 return errCode;
199 return 0;
202 static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
204 dprintk("%s transfer\n", (dev->adev->capture_stream == STREAM_ON)?
205 "stop" : "start");
207 switch (cmd) {
208 case EM28XX_CAPTURE_STREAM_EN:
209 if (dev->adev->capture_stream == STREAM_OFF && arg == 1) {
210 dev->adev->capture_stream = STREAM_ON;
211 em28xx_init_audio_isoc(dev);
212 } else if (dev->adev->capture_stream == STREAM_ON && arg == 0) {
213 dev->adev->capture_stream = STREAM_OFF;
214 em28xx_isoc_audio_deinit(dev);
215 } else {
216 printk(KERN_ERR "An underrun very likely occurred. "
217 "Ignoring it.\n");
219 return 0;
220 default:
221 return -EINVAL;
225 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
226 size_t size)
228 struct snd_pcm_runtime *runtime = subs->runtime;
230 dprintk("Alocating vbuffer\n");
231 if (runtime->dma_area) {
232 if (runtime->dma_bytes > size)
233 return 0;
235 vfree(runtime->dma_area);
237 runtime->dma_area = vmalloc(size);
238 if (!runtime->dma_area)
239 return -ENOMEM;
241 runtime->dma_bytes = size;
243 return 0;
246 static struct snd_pcm_hardware snd_em28xx_hw_capture = {
247 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
248 SNDRV_PCM_INFO_MMAP |
249 SNDRV_PCM_INFO_INTERLEAVED |
250 SNDRV_PCM_INFO_MMAP_VALID,
252 .formats = SNDRV_PCM_FMTBIT_S16_LE,
254 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
256 .rate_min = 48000,
257 .rate_max = 48000,
258 .channels_min = 2,
259 .channels_max = 2,
260 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
261 .period_bytes_min = 64, /* 12544/2, */
262 .period_bytes_max = 12544,
263 .periods_min = 2,
264 .periods_max = 98, /* 12544, */
267 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
269 struct em28xx *dev = snd_pcm_substream_chip(substream);
270 struct snd_pcm_runtime *runtime = substream->runtime;
271 int ret = 0;
273 dprintk("opening device and trying to acquire exclusive lock\n");
275 /* Sets volume, mute, etc */
276 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-audio.c
277 =======
279 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-audio.c
280 dev->mute = 0;
281 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-audio.c
282 =======
283 mutex_lock(&dev->lock);
284 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-audio.c
285 ret = em28xx_audio_analog_set(dev);
286 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-audio.c
287 =======
288 mutex_unlock(&dev->lock);
289 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-audio.c
290 if (ret < 0)
291 goto err;
293 runtime->hw = snd_em28xx_hw_capture;
294 if (dev->alt == 0 && dev->adev->users == 0) {
295 int errCode;
296 dev->alt = 7;
297 errCode = usb_set_interface(dev->udev, 0, 7);
298 dprintk("changing alternate number to 7\n");
301 dev->adev->users++;
303 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
304 dev->adev->capture_pcm_substream = substream;
305 runtime->private_data = dev;
307 return 0;
308 err:
309 printk(KERN_ERR "Error while configuring em28xx mixer\n");
310 return ret;
313 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
315 struct em28xx *dev = snd_pcm_substream_chip(substream);
316 dev->adev->users--;
318 dprintk("closing device\n");
320 dev->mute = 1;
321 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-audio.c
322 =======
323 mutex_lock(&dev->lock);
324 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-audio.c
325 em28xx_audio_analog_set(dev);
326 <<<<<<< HEAD:drivers/media/video/em28xx/em28xx-audio.c
327 =======
328 mutex_unlock(&dev->lock);
329 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/media/video/em28xx/em28xx-audio.c
331 if (dev->adev->users == 0 && dev->adev->shutdown == 1) {
332 dprintk("audio users: %d\n", dev->adev->users);
333 dprintk("disabling audio stream!\n");
334 dev->adev->shutdown = 0;
335 dprintk("released lock\n");
336 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
338 return 0;
341 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
342 struct snd_pcm_hw_params *hw_params)
344 unsigned int channels, rate, format;
345 int ret;
347 dprintk("Setting capture parameters\n");
349 ret = snd_pcm_alloc_vmalloc_buffer(substream,
350 params_buffer_bytes(hw_params));
351 format = params_format(hw_params);
352 rate = params_rate(hw_params);
353 channels = params_channels(hw_params);
355 /* TODO: set up em28xx audio chip to deliver the correct audio format,
356 current default is 48000hz multiplexed => 96000hz mono
357 which shouldn't matter since analogue TV only supports mono */
358 return 0;
361 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
363 struct em28xx *dev = snd_pcm_substream_chip(substream);
365 dprintk("Stop capture, if needed\n");
367 if (dev->adev->capture_stream == STREAM_ON)
368 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
370 return 0;
373 static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
375 return 0;
378 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
379 int cmd)
381 struct em28xx *dev = snd_pcm_substream_chip(substream);
383 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
384 "start": "stop");
385 switch (cmd) {
386 case SNDRV_PCM_TRIGGER_START:
387 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
388 return 0;
389 case SNDRV_PCM_TRIGGER_STOP:
390 dev->adev->shutdown = 1;
391 return 0;
392 default:
393 return -EINVAL;
397 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
398 *substream)
400 struct em28xx *dev;
402 snd_pcm_uframes_t hwptr_done;
403 dev = snd_pcm_substream_chip(substream);
404 hwptr_done = dev->adev->hwptr_done_capture;
406 return hwptr_done;
409 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
410 unsigned long offset)
412 void *pageptr = subs->runtime->dma_area + offset;
414 return vmalloc_to_page(pageptr);
417 static struct snd_pcm_ops snd_em28xx_pcm_capture = {
418 .open = snd_em28xx_capture_open,
419 .close = snd_em28xx_pcm_close,
420 .ioctl = snd_pcm_lib_ioctl,
421 .hw_params = snd_em28xx_hw_capture_params,
422 .hw_free = snd_em28xx_hw_capture_free,
423 .prepare = snd_em28xx_prepare,
424 .trigger = snd_em28xx_capture_trigger,
425 .pointer = snd_em28xx_capture_pointer,
426 .page = snd_pcm_get_vmalloc_page,
429 static int em28xx_audio_init(struct em28xx *dev)
431 struct em28xx_audio *adev;
432 struct snd_pcm *pcm;
433 struct snd_card *card;
434 static int devnr;
435 int ret, err;
437 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
438 "non standard usbaudio\n");
439 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
440 "Rechberger\n");
442 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
443 if (!adev) {
444 printk(KERN_ERR "em28xx-audio.c: out of memory\n");
445 return -1;
447 card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
448 if (card == NULL) {
449 kfree(adev);
450 return -ENOMEM;
453 spin_lock_init(&adev->slock);
454 ret = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
455 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
456 pcm->info_flags = 0;
457 pcm->private_data = dev;
458 strcpy(pcm->name, "Empia 28xx Capture");
459 strcpy(card->driver, "Empia Em28xx Audio");
460 strcpy(card->shortname, "Em28xx Audio");
461 strcpy(card->longname, "Empia Em28xx Audio");
463 err = snd_card_register(card);
464 if (err < 0) {
465 snd_card_free(card);
466 return -ENOMEM;
468 adev->sndcard = card;
469 adev->udev = dev->udev;
470 dev->adev = adev;
472 return 0;
475 static int em28xx_audio_fini(struct em28xx *dev)
477 if (dev == NULL)
478 return 0;
480 if (dev->adev) {
481 snd_card_free(dev->adev->sndcard);
482 kfree(dev->adev);
483 dev->adev = NULL;
486 return 0;
489 static struct em28xx_ops audio_ops = {
490 .id = EM28XX_AUDIO,
491 .name = "Em28xx Audio Extension",
492 .init = em28xx_audio_init,
493 .fini = em28xx_audio_fini,
496 static int __init em28xx_alsa_register(void)
498 return em28xx_register_extension(&audio_ops);
501 static void __exit em28xx_alsa_unregister(void)
503 em28xx_unregister_extension(&audio_ops);
506 MODULE_LICENSE("GPL");
507 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
508 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
509 MODULE_DESCRIPTION("Em28xx Audio driver");
511 module_init(em28xx_alsa_register);
512 module_exit(em28xx_alsa_unregister);