2 * Empiatech em28x1 audio extension
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
6 * Copyright (C) 2007-2011 Mauro Carvalho Chehab <mchehab@redhat.com>
7 * - Port to work with the in-kernel driver
8 * - Cleanups, fixes, alsa-controls, etc.
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 <sound/tlv.h>
45 #include <sound/ac97_codec.h>
46 #include <media/v4l2-common.h>
50 module_param(debug, int, 0644);
51 MODULE_PARM_DESC(debug, "activates debug info");
53 #define dprintk(fmt, arg...) do { \
55 printk(KERN_INFO "em28xx-audio %s: " fmt, \
59 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
61 static int em28xx_deinit_isoc_audio(struct em28xx *dev)
65 dprintk("Stopping isoc\n");
66 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
68 usb_kill_urb(dev->adev.urb[i]);
70 usb_unlink_urb(dev->adev.urb[i]);
72 usb_free_urb(dev->adev.urb[i]);
73 dev->adev.urb[i] = NULL;
75 kfree(dev->adev.transfer_buffer[i]);
76 dev->adev.transfer_buffer[i] = NULL;
82 static void em28xx_audio_isocirq(struct urb *urb)
84 struct em28xx *dev = urb->context;
87 int period_elapsed = 0;
91 struct snd_pcm_substream *substream;
92 struct snd_pcm_runtime *runtime;
94 switch (urb->status) {
96 case -ETIMEDOUT: /* NAK */
98 case -ECONNRESET: /* kill */
103 dprintk("urb completition error %d.\n", urb->status);
107 if (atomic_read(&dev->stream_started) == 0)
110 if (dev->adev.capture_pcm_substream) {
111 substream = dev->adev.capture_pcm_substream;
112 runtime = substream->runtime;
113 stride = runtime->frame_bits >> 3;
115 for (i = 0; i < urb->number_of_packets; i++) {
117 urb->iso_frame_desc[i].actual_length / stride;
118 cp = (unsigned char *)urb->transfer_buffer +
119 urb->iso_frame_desc[i].offset;
124 oldptr = dev->adev.hwptr_done_capture;
125 if (oldptr + length >= runtime->buffer_size) {
127 runtime->buffer_size - oldptr;
128 memcpy(runtime->dma_area + oldptr * stride, cp,
130 memcpy(runtime->dma_area, cp + cnt * stride,
131 length * stride - cnt * stride);
133 memcpy(runtime->dma_area + oldptr * stride, cp,
137 snd_pcm_stream_lock(substream);
139 dev->adev.hwptr_done_capture += length;
140 if (dev->adev.hwptr_done_capture >=
141 runtime->buffer_size)
142 dev->adev.hwptr_done_capture -=
143 runtime->buffer_size;
145 dev->adev.capture_transfer_done += length;
146 if (dev->adev.capture_transfer_done >=
147 runtime->period_size) {
148 dev->adev.capture_transfer_done -=
149 runtime->period_size;
153 snd_pcm_stream_unlock(substream);
156 snd_pcm_period_elapsed(substream);
160 status = usb_submit_urb(urb, GFP_ATOMIC);
162 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
168 static int em28xx_init_audio_isoc(struct em28xx *dev)
171 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
172 EM28XX_AUDIO_MAX_PACKET_SIZE;
174 dprintk("Starting isoc transfers\n");
176 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
180 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
181 if (!dev->adev.transfer_buffer[i])
184 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
185 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
187 em28xx_errdev("usb_alloc_urb failed!\n");
188 for (j = 0; j < i; j++) {
189 usb_free_urb(dev->adev.urb[j]);
190 kfree(dev->adev.transfer_buffer[j]);
195 urb->dev = dev->udev;
197 urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO);
198 urb->transfer_flags = URB_ISO_ASAP;
199 urb->transfer_buffer = dev->adev.transfer_buffer[i];
201 urb->complete = em28xx_audio_isocirq;
202 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
203 urb->transfer_buffer_length = sb_size;
205 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
206 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
207 urb->iso_frame_desc[j].offset = k;
208 urb->iso_frame_desc[j].length =
209 EM28XX_AUDIO_MAX_PACKET_SIZE;
211 dev->adev.urb[i] = urb;
214 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
215 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
217 em28xx_errdev("submit of audio urb failed\n");
218 em28xx_deinit_isoc_audio(dev);
219 atomic_set(&dev->stream_started, 0);
228 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
231 struct snd_pcm_runtime *runtime = subs->runtime;
233 dprintk("Allocating vbuffer\n");
234 if (runtime->dma_area) {
235 if (runtime->dma_bytes > size)
238 vfree(runtime->dma_area);
240 runtime->dma_area = vmalloc(size);
241 if (!runtime->dma_area)
244 runtime->dma_bytes = size;
249 static struct snd_pcm_hardware snd_em28xx_hw_capture = {
250 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
251 SNDRV_PCM_INFO_MMAP |
252 SNDRV_PCM_INFO_INTERLEAVED |
253 SNDRV_PCM_INFO_BATCH |
254 SNDRV_PCM_INFO_MMAP_VALID,
256 .formats = SNDRV_PCM_FMTBIT_S16_LE,
258 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
264 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
265 .period_bytes_min = 64, /* 12544/2, */
266 .period_bytes_max = 12544,
268 .periods_max = 98, /* 12544, */
271 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
273 struct em28xx *dev = snd_pcm_substream_chip(substream);
274 struct snd_pcm_runtime *runtime = substream->runtime;
277 dprintk("opening device and trying to acquire exclusive lock\n");
280 em28xx_err("BUG: em28xx can't find device struct."
281 " Can't proceed with open\n");
285 runtime->hw = snd_em28xx_hw_capture;
286 if ((dev->alt == 0 || dev->audio_ifnum) && dev->adev.users == 0) {
287 if (dev->audio_ifnum)
292 dprintk("changing alternate number on interface %d to %d\n",
293 dev->audio_ifnum, dev->alt);
294 usb_set_interface(dev->udev, dev->audio_ifnum, dev->alt);
296 /* Sets volume, mute, etc */
298 mutex_lock(&dev->lock);
299 ret = em28xx_audio_analog_set(dev);
304 mutex_unlock(&dev->lock);
307 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
308 dev->adev.capture_pcm_substream = substream;
312 mutex_unlock(&dev->lock);
314 em28xx_err("Error while configuring em28xx mixer\n");
318 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
320 struct em28xx *dev = snd_pcm_substream_chip(substream);
322 dprintk("closing device\n");
325 mutex_lock(&dev->lock);
327 if (atomic_read(&dev->stream_started) > 0) {
328 atomic_set(&dev->stream_started, 0);
329 schedule_work(&dev->wq_trigger);
332 em28xx_audio_analog_set(dev);
333 if (substream->runtime->dma_area) {
334 dprintk("freeing\n");
335 vfree(substream->runtime->dma_area);
336 substream->runtime->dma_area = NULL;
338 mutex_unlock(&dev->lock);
343 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
344 struct snd_pcm_hw_params *hw_params)
348 dprintk("Setting capture parameters\n");
350 ret = snd_pcm_alloc_vmalloc_buffer(substream,
351 params_buffer_bytes(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 unsigned int channels, rate, format;
360 format = params_format(hw_params);
361 rate = params_rate(hw_params);
362 channels = params_channels(hw_params);
368 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
370 struct em28xx *dev = snd_pcm_substream_chip(substream);
372 dprintk("Stop capture, if needed\n");
374 if (atomic_read(&dev->stream_started) > 0) {
375 atomic_set(&dev->stream_started, 0);
376 schedule_work(&dev->wq_trigger);
382 static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
384 struct em28xx *dev = snd_pcm_substream_chip(substream);
386 dev->adev.hwptr_done_capture = 0;
387 dev->adev.capture_transfer_done = 0;
392 static void audio_trigger(struct work_struct *work)
394 struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
396 if (atomic_read(&dev->stream_started)) {
397 dprintk("starting capture");
398 em28xx_init_audio_isoc(dev);
400 dprintk("stopping capture");
401 em28xx_deinit_isoc_audio(dev);
405 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
408 struct em28xx *dev = snd_pcm_substream_chip(substream);
412 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
413 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
414 case SNDRV_PCM_TRIGGER_START:
415 atomic_set(&dev->stream_started, 1);
417 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
418 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
419 case SNDRV_PCM_TRIGGER_STOP:
420 atomic_set(&dev->stream_started, 0);
425 schedule_work(&dev->wq_trigger);
429 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
434 snd_pcm_uframes_t hwptr_done;
436 dev = snd_pcm_substream_chip(substream);
437 spin_lock_irqsave(&dev->adev.slock, flags);
438 hwptr_done = dev->adev.hwptr_done_capture;
439 spin_unlock_irqrestore(&dev->adev.slock, flags);
444 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
445 unsigned long offset)
447 void *pageptr = subs->runtime->dma_area + offset;
449 return vmalloc_to_page(pageptr);
453 * AC97 volume control support
455 static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
456 struct snd_ctl_elem_info *info)
458 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
460 info->value.integer.min = 0;
461 info->value.integer.max = 0x1f;
466 static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
467 struct snd_ctl_elem_value *value)
469 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
470 u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
471 (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
474 mutex_lock(&dev->lock);
475 rc = em28xx_read_ac97(dev, kcontrol->private_value);
479 val |= rc & 0x8000; /* Preserve the mute flag */
481 rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
485 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
486 (val & 0x8000) ? "muted " : "",
487 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
488 val, (int)kcontrol->private_value);
491 mutex_unlock(&dev->lock);
495 static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
496 struct snd_ctl_elem_value *value)
498 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
501 mutex_lock(&dev->lock);
502 val = em28xx_read_ac97(dev, kcontrol->private_value);
503 mutex_unlock(&dev->lock);
507 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
508 (val & 0x8000) ? "muted " : "",
509 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
510 val, (int)kcontrol->private_value);
512 value->value.integer.value[0] = 0x1f - (val & 0x1f);
513 value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
518 static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
519 struct snd_ctl_elem_value *value)
521 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
522 u16 val = value->value.integer.value[0];
525 mutex_lock(&dev->lock);
526 rc = em28xx_read_ac97(dev, kcontrol->private_value);
535 rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
539 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
540 (val & 0x8000) ? "muted " : "",
541 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
542 val, (int)kcontrol->private_value);
545 mutex_unlock(&dev->lock);
549 static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
550 struct snd_ctl_elem_value *value)
552 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
555 mutex_lock(&dev->lock);
556 val = em28xx_read_ac97(dev, kcontrol->private_value);
557 mutex_unlock(&dev->lock);
562 value->value.integer.value[0] = 0;
564 value->value.integer.value[0] = 1;
566 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
567 (val & 0x8000) ? "muted " : "",
568 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
569 val, (int)kcontrol->private_value);
574 static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
576 static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
581 struct snd_kcontrol *kctl;
582 struct snd_kcontrol_new tmp;
584 memset (&tmp, 0, sizeof(tmp));
585 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
586 tmp.private_value = id,
589 /* Add Mute Control */
590 sprintf(ctl_name, "%s Switch", name);
591 tmp.get = em28xx_vol_get_mute;
592 tmp.put = em28xx_vol_put_mute;
593 tmp.info = snd_ctl_boolean_mono_info;
594 kctl = snd_ctl_new1(&tmp, dev);
595 err = snd_ctl_add(card, kctl);
598 dprintk("Added control %s for ac97 volume control 0x%04x\n",
601 memset (&tmp, 0, sizeof(tmp));
602 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
603 tmp.private_value = id,
606 /* Add Volume Control */
607 sprintf(ctl_name, "%s Volume", name);
608 tmp.get = em28xx_vol_get;
609 tmp.put = em28xx_vol_put;
610 tmp.info = em28xx_vol_info;
611 tmp.tlv.p = em28xx_db_scale,
612 kctl = snd_ctl_new1(&tmp, dev);
613 err = snd_ctl_add(card, kctl);
616 dprintk("Added control %s for ac97 volume control 0x%04x\n",
623 * register/unregister code and data
625 static struct snd_pcm_ops snd_em28xx_pcm_capture = {
626 .open = snd_em28xx_capture_open,
627 .close = snd_em28xx_pcm_close,
628 .ioctl = snd_pcm_lib_ioctl,
629 .hw_params = snd_em28xx_hw_capture_params,
630 .hw_free = snd_em28xx_hw_capture_free,
631 .prepare = snd_em28xx_prepare,
632 .trigger = snd_em28xx_capture_trigger,
633 .pointer = snd_em28xx_capture_pointer,
634 .page = snd_pcm_get_vmalloc_page,
637 static int em28xx_audio_init(struct em28xx *dev)
639 struct em28xx_audio *adev = &dev->adev;
641 struct snd_card *card;
645 if (!dev->has_alsa_audio || dev->audio_ifnum < 0) {
646 /* This device does not support the extension (in this case
647 the device is expecting the snd-usb-audio module or
648 doesn't have analog audio support at all) */
652 printk(KERN_INFO "em28xx-audio.c: probing for em28xx Audio Vendor Class\n");
653 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
655 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2007-2011 Mauro Carvalho Chehab\n");
657 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
662 spin_lock_init(&adev->slock);
663 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
669 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
671 pcm->private_data = dev;
672 strcpy(pcm->name, "Empia 28xx Capture");
674 snd_card_set_dev(card, &dev->udev->dev);
675 strcpy(card->driver, "Em28xx-Audio");
676 strcpy(card->shortname, "Em28xx Audio");
677 strcpy(card->longname, "Empia Em28xx Audio");
679 INIT_WORK(&dev->wq_trigger, audio_trigger);
681 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
682 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
683 em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
684 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
685 em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
686 em28xx_cvol_new(card, dev, "CD", AC97_CD);
687 em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
688 em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
690 em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
691 em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
692 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
693 em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
694 em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
697 err = snd_card_register(card);
702 adev->sndcard = card;
703 adev->udev = dev->udev;
708 static int em28xx_audio_fini(struct em28xx *dev)
713 if (dev->has_alsa_audio != 1) {
714 /* This device does not support the extension (in this case
715 the device is expecting the snd-usb-audio module or
716 doesn't have analog audio support at all) */
720 if (dev->adev.sndcard) {
721 snd_card_free(dev->adev.sndcard);
722 dev->adev.sndcard = NULL;
728 static struct em28xx_ops audio_ops = {
730 .name = "Em28xx Audio Extension",
731 .init = em28xx_audio_init,
732 .fini = em28xx_audio_fini,
735 static int __init em28xx_alsa_register(void)
737 return em28xx_register_extension(&audio_ops);
740 static void __exit em28xx_alsa_unregister(void)
742 em28xx_unregister_extension(&audio_ops);
745 MODULE_LICENSE("GPL");
746 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
747 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
748 MODULE_DESCRIPTION("Em28xx Audio driver");
750 module_init(em28xx_alsa_register);
751 module_exit(em28xx_alsa_unregister);