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
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 #include <sound/core.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/info.h>
42 #include <sound/initval.h>
43 #include <sound/control.h>
44 #include <media/v4l2-common.h>
48 module_param(debug, int, 0644);
49 MODULE_PARM_DESC(debug, "activates debug info");
51 #define dprintk(fmt, arg...) do { \
53 printk(KERN_INFO "em28xx-audio %s: " fmt, \
57 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
59 static int em28xx_isoc_audio_deinit(struct em28xx *dev)
63 dprintk("Stopping isoc\n");
64 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
66 usb_kill_urb(dev->adev.urb[i]);
68 usb_unlink_urb(dev->adev.urb[i]);
69 usb_free_urb(dev->adev.urb[i]);
70 dev->adev.urb[i] = NULL;
72 kfree(dev->adev.transfer_buffer[i]);
73 dev->adev.transfer_buffer[i] = NULL;
79 static void em28xx_audio_isocirq(struct urb *urb)
81 struct em28xx *dev = urb->context;
84 int period_elapsed = 0;
88 struct snd_pcm_substream *substream;
89 struct snd_pcm_runtime *runtime;
90 if (dev->adev.capture_pcm_substream) {
91 substream = dev->adev.capture_pcm_substream;
92 runtime = substream->runtime;
93 stride = runtime->frame_bits >> 3;
95 for (i = 0; i < urb->number_of_packets; i++) {
97 urb->iso_frame_desc[i].actual_length / stride;
98 cp = (unsigned char *)urb->transfer_buffer +
99 urb->iso_frame_desc[i].offset;
104 oldptr = dev->adev.hwptr_done_capture;
105 if (oldptr + length >= runtime->buffer_size) {
107 runtime->buffer_size - oldptr;
108 memcpy(runtime->dma_area + oldptr * stride, cp,
110 memcpy(runtime->dma_area, cp + cnt * stride,
111 length * stride - cnt * stride);
113 memcpy(runtime->dma_area + oldptr * stride, cp,
117 snd_pcm_stream_lock(substream);
119 dev->adev.hwptr_done_capture += length;
120 if (dev->adev.hwptr_done_capture >=
121 runtime->buffer_size)
122 dev->adev.hwptr_done_capture -=
123 runtime->buffer_size;
125 dev->adev.capture_transfer_done += length;
126 if (dev->adev.capture_transfer_done >=
127 runtime->period_size) {
128 dev->adev.capture_transfer_done -=
129 runtime->period_size;
133 snd_pcm_stream_unlock(substream);
136 snd_pcm_period_elapsed(substream);
140 if (dev->adev.shutdown)
143 status = usb_submit_urb(urb, GFP_ATOMIC);
145 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
151 static int em28xx_init_audio_isoc(struct em28xx *dev)
154 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
155 EM28XX_AUDIO_MAX_PACKET_SIZE;
157 dprintk("Starting isoc transfers\n");
159 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
163 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
164 if (!dev->adev.transfer_buffer[i])
167 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
168 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
170 em28xx_errdev("usb_alloc_urb failed!\n");
171 for (j = 0; j < i; j++) {
172 usb_free_urb(dev->adev.urb[j]);
173 kfree(dev->adev.transfer_buffer[j]);
178 urb->dev = dev->udev;
180 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
181 urb->transfer_flags = URB_ISO_ASAP;
182 urb->transfer_buffer = dev->adev.transfer_buffer[i];
184 urb->complete = em28xx_audio_isocirq;
185 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
186 urb->transfer_buffer_length = sb_size;
188 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
189 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
190 urb->iso_frame_desc[j].offset = k;
191 urb->iso_frame_desc[j].length =
192 EM28XX_AUDIO_MAX_PACKET_SIZE;
194 dev->adev.urb[i] = urb;
197 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
198 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
200 em28xx_isoc_audio_deinit(dev);
209 static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
211 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
215 case EM28XX_CAPTURE_STREAM_EN:
216 if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
217 dev->adev.capture_stream = STREAM_ON;
218 em28xx_init_audio_isoc(dev);
219 } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
220 dev->adev.capture_stream = STREAM_OFF;
221 em28xx_isoc_audio_deinit(dev);
223 printk(KERN_ERR "An underrun very likely occurred. "
232 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
235 struct snd_pcm_runtime *runtime = subs->runtime;
237 dprintk("Alocating vbuffer\n");
238 if (runtime->dma_area) {
239 if (runtime->dma_bytes > size)
242 vfree(runtime->dma_area);
244 runtime->dma_area = vmalloc(size);
245 if (!runtime->dma_area)
248 runtime->dma_bytes = size;
253 static struct snd_pcm_hardware snd_em28xx_hw_capture = {
254 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
255 SNDRV_PCM_INFO_MMAP |
256 SNDRV_PCM_INFO_INTERLEAVED |
257 SNDRV_PCM_INFO_MMAP_VALID,
259 .formats = SNDRV_PCM_FMTBIT_S16_LE,
261 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
267 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
268 .period_bytes_min = 64, /* 12544/2, */
269 .period_bytes_max = 12544,
271 .periods_max = 98, /* 12544, */
274 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
276 struct em28xx *dev = snd_pcm_substream_chip(substream);
277 struct snd_pcm_runtime *runtime = substream->runtime;
280 dprintk("opening device and trying to acquire exclusive lock\n");
283 printk(KERN_ERR "BUG: em28xx can't find device struct."
284 " Can't proceed with open\n");
288 /* Sets volume, mute, etc */
291 mutex_lock(&dev->lock);
292 ret = em28xx_audio_analog_set(dev);
293 mutex_unlock(&dev->lock);
297 runtime->hw = snd_em28xx_hw_capture;
298 if (dev->alt == 0 && dev->adev.users == 0) {
301 errCode = usb_set_interface(dev->udev, 0, 7);
302 dprintk("changing alternate number to 7\n");
307 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
308 dev->adev.capture_pcm_substream = substream;
309 runtime->private_data = dev;
313 printk(KERN_ERR "Error while configuring em28xx mixer\n");
317 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
319 struct em28xx *dev = snd_pcm_substream_chip(substream);
322 dprintk("closing device\n");
325 mutex_lock(&dev->lock);
326 em28xx_audio_analog_set(dev);
327 mutex_unlock(&dev->lock);
329 if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
330 dprintk("audio users: %d\n", dev->adev.users);
331 dprintk("disabling audio stream!\n");
332 dev->adev.shutdown = 0;
333 dprintk("released lock\n");
334 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
339 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
340 struct snd_pcm_hw_params *hw_params)
342 unsigned int channels, rate, format;
345 dprintk("Setting capture parameters\n");
347 ret = snd_pcm_alloc_vmalloc_buffer(substream,
348 params_buffer_bytes(hw_params));
349 format = params_format(hw_params);
350 rate = params_rate(hw_params);
351 channels = params_channels(hw_params);
353 /* TODO: set up em28xx audio chip to deliver the correct audio format,
354 current default is 48000hz multiplexed => 96000hz mono
355 which shouldn't matter since analogue TV only supports mono */
359 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
361 struct em28xx *dev = snd_pcm_substream_chip(substream);
363 dprintk("Stop capture, if needed\n");
365 if (dev->adev.capture_stream == STREAM_ON)
366 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
371 static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
376 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
379 struct em28xx *dev = snd_pcm_substream_chip(substream);
381 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
384 case SNDRV_PCM_TRIGGER_START:
385 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
387 case SNDRV_PCM_TRIGGER_STOP:
388 dev->adev.shutdown = 1;
395 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
401 snd_pcm_uframes_t hwptr_done;
403 dev = snd_pcm_substream_chip(substream);
404 spin_lock_irqsave(&dev->adev.slock, flags);
405 hwptr_done = dev->adev.hwptr_done_capture;
406 spin_unlock_irqrestore(&dev->adev.slock, flags);
411 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
412 unsigned long offset)
414 void *pageptr = subs->runtime->dma_area + offset;
416 return vmalloc_to_page(pageptr);
419 static struct snd_pcm_ops snd_em28xx_pcm_capture = {
420 .open = snd_em28xx_capture_open,
421 .close = snd_em28xx_pcm_close,
422 .ioctl = snd_pcm_lib_ioctl,
423 .hw_params = snd_em28xx_hw_capture_params,
424 .hw_free = snd_em28xx_hw_capture_free,
425 .prepare = snd_em28xx_prepare,
426 .trigger = snd_em28xx_capture_trigger,
427 .pointer = snd_em28xx_capture_pointer,
428 .page = snd_pcm_get_vmalloc_page,
431 static int em28xx_audio_init(struct em28xx *dev)
433 struct em28xx_audio *adev = &dev->adev;
435 struct snd_card *card;
439 if (dev->has_alsa_audio != 1) {
440 /* This device does not support the extension (in this case
441 the device is expecting the snd-usb-audio module or
442 doesn't have analog audio support at all) */
446 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
447 "non standard usbaudio\n");
448 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
451 card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
455 spin_lock_init(&adev->slock);
456 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
462 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
464 pcm->private_data = dev;
465 strcpy(pcm->name, "Empia 28xx Capture");
467 snd_card_set_dev(card, &dev->udev->dev);
468 strcpy(card->driver, "Empia Em28xx Audio");
469 strcpy(card->shortname, "Em28xx Audio");
470 strcpy(card->longname, "Empia Em28xx Audio");
472 err = snd_card_register(card);
477 adev->sndcard = card;
478 adev->udev = dev->udev;
483 static int em28xx_audio_fini(struct em28xx *dev)
488 if (dev->has_alsa_audio != 1) {
489 /* This device does not support the extension (in this case
490 the device is expecting the snd-usb-audio module or
491 doesn't have analog audio support at all) */
495 if (dev->adev.sndcard) {
496 snd_card_free(dev->adev.sndcard);
497 dev->adev.sndcard = NULL;
503 static struct em28xx_ops audio_ops = {
505 .name = "Em28xx Audio Extension",
506 .init = em28xx_audio_init,
507 .fini = em28xx_audio_fini,
510 static int __init em28xx_alsa_register(void)
512 return em28xx_register_extension(&audio_ops);
515 static void __exit em28xx_alsa_unregister(void)
517 em28xx_unregister_extension(&audio_ops);
520 MODULE_LICENSE("GPL");
521 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
522 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
523 MODULE_DESCRIPTION("Em28xx Audio driver");
525 module_init(em28xx_alsa_register);
526 module_exit(em28xx_alsa_unregister);