2 * Empiatech em28x1 audio extension
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
6 * Copyright (C) 2007-2014 Mauro Carvalho Chehab
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 EM28XX_MAX_AUDIO_BUFS 5
54 #define EM28XX_MIN_AUDIO_PACKETS 64
56 #define dprintk(fmt, arg...) do { \
58 printk(KERN_INFO "em28xx-audio %s: " fmt, \
62 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
64 static int em28xx_deinit_isoc_audio(struct em28xx
*dev
)
68 dprintk("Stopping isoc\n");
69 for (i
= 0; i
< dev
->adev
.num_urb
; i
++) {
70 struct urb
*urb
= dev
->adev
.urb
[i
];
81 static void em28xx_audio_isocirq(struct urb
*urb
)
83 struct em28xx
*dev
= urb
->context
;
86 int period_elapsed
= 0;
90 struct snd_pcm_substream
*substream
;
91 struct snd_pcm_runtime
*runtime
;
93 if (dev
->disconnected
) {
94 dprintk("device disconnected while streaming. URB status=%d.\n", urb
->status
);
95 atomic_set(&dev
->stream_started
, 0);
99 switch (urb
->status
) {
100 case 0: /* success */
101 case -ETIMEDOUT
: /* NAK */
103 case -ECONNRESET
: /* kill */
108 dprintk("urb completition error %d.\n", urb
->status
);
112 if (atomic_read(&dev
->stream_started
) == 0)
115 if (dev
->adev
.capture_pcm_substream
) {
116 substream
= dev
->adev
.capture_pcm_substream
;
117 runtime
= substream
->runtime
;
118 stride
= runtime
->frame_bits
>> 3;
120 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
122 urb
->iso_frame_desc
[i
].actual_length
/ stride
;
123 cp
= (unsigned char *)urb
->transfer_buffer
+
124 urb
->iso_frame_desc
[i
].offset
;
129 oldptr
= dev
->adev
.hwptr_done_capture
;
130 if (oldptr
+ length
>= runtime
->buffer_size
) {
132 runtime
->buffer_size
- oldptr
;
133 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
135 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
136 length
* stride
- cnt
* stride
);
138 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
142 snd_pcm_stream_lock(substream
);
144 dev
->adev
.hwptr_done_capture
+= length
;
145 if (dev
->adev
.hwptr_done_capture
>=
146 runtime
->buffer_size
)
147 dev
->adev
.hwptr_done_capture
-=
148 runtime
->buffer_size
;
150 dev
->adev
.capture_transfer_done
+= length
;
151 if (dev
->adev
.capture_transfer_done
>=
152 runtime
->period_size
) {
153 dev
->adev
.capture_transfer_done
-=
154 runtime
->period_size
;
158 snd_pcm_stream_unlock(substream
);
161 snd_pcm_period_elapsed(substream
);
165 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
167 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
172 static int em28xx_init_audio_isoc(struct em28xx
*dev
)
176 dprintk("Starting isoc transfers\n");
178 /* Start streaming */
179 for (i
= 0; i
< dev
->adev
.num_urb
; i
++) {
180 memset(dev
->adev
.transfer_buffer
[i
], 0x80,
181 dev
->adev
.urb
[i
]->transfer_buffer_length
);
183 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
185 em28xx_errdev("submit of audio urb failed (error=%i)\n",
187 em28xx_deinit_isoc_audio(dev
);
188 atomic_set(&dev
->stream_started
, 0);
197 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
,
200 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
202 dprintk("Allocating vbuffer\n");
203 if (runtime
->dma_area
) {
204 if (runtime
->dma_bytes
> size
)
207 vfree(runtime
->dma_area
);
209 runtime
->dma_area
= vmalloc(size
);
210 if (!runtime
->dma_area
)
213 runtime
->dma_bytes
= size
;
218 static struct snd_pcm_hardware snd_em28xx_hw_capture
= {
219 .info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
|
220 SNDRV_PCM_INFO_MMAP
|
221 SNDRV_PCM_INFO_INTERLEAVED
|
222 SNDRV_PCM_INFO_BATCH
|
223 SNDRV_PCM_INFO_MMAP_VALID
,
225 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
227 .rates
= SNDRV_PCM_RATE_48000
,
233 .buffer_bytes_max
= 62720 * 8, /* just about the value in usbaudio.c */
237 * The period is 12.288 bytes. Allow a 10% of variation along its
238 * value, in order to avoid overruns/underruns due to some clock
241 * FIXME: This period assumes 64 packets, and a 48000 PCM rate.
242 * Calculate it dynamically.
244 .period_bytes_min
= 11059,
245 .period_bytes_max
= 13516,
248 .periods_max
= 98, /* 12544, */
251 static int snd_em28xx_capture_open(struct snd_pcm_substream
*substream
)
253 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
254 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
258 em28xx_err("BUG: em28xx can't find device struct."
259 " Can't proceed with open\n");
263 if (dev
->disconnected
)
266 dprintk("opening device and trying to acquire exclusive lock\n");
268 runtime
->hw
= snd_em28xx_hw_capture
;
269 if ((dev
->alt
== 0 || dev
->is_audio_only
) && dev
->adev
.users
== 0) {
270 int nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
273 if (!mutex_trylock(&dev
->lock
))
276 mutex_lock(&dev
->lock
);
277 if (dev
->is_audio_only
)
278 /* vendor audio is on a separate interface */
281 /* vendor audio is on the same interface as video */
284 * FIXME: The intention seems to be to select the alt
285 * setting with the largest wMaxPacketSize for the video
287 * At least dev->alt should be used instead, but we
288 * should probably not touch it at all if it is
289 * already >0, because wMaxPacketSize of the audio
290 * endpoints seems to be the same for all.
293 dprintk("changing alternate number on interface %d to %d\n",
294 dev
->ifnum
, dev
->alt
);
295 usb_set_interface(dev
->udev
, dev
->ifnum
, dev
->alt
);
297 /* Sets volume, mute, etc */
299 ret
= em28xx_audio_analog_set(dev
);
304 mutex_unlock(&dev
->lock
);
307 /* Dynamically adjust the period size */
308 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
309 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
310 dev
->adev
.period
* 95 / 100,
311 dev
->adev
.period
* 105 / 100);
313 dev
->adev
.capture_pcm_substream
= substream
;
317 mutex_unlock(&dev
->lock
);
319 em28xx_err("Error while configuring em28xx mixer\n");
323 static int snd_em28xx_pcm_close(struct snd_pcm_substream
*substream
)
325 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
327 dprintk("closing device\n");
330 mutex_lock(&dev
->lock
);
332 if (atomic_read(&dev
->stream_started
) > 0) {
333 atomic_set(&dev
->stream_started
, 0);
334 schedule_work(&dev
->wq_trigger
);
337 em28xx_audio_analog_set(dev
);
338 if (substream
->runtime
->dma_area
) {
339 dprintk("freeing\n");
340 vfree(substream
->runtime
->dma_area
);
341 substream
->runtime
->dma_area
= NULL
;
343 mutex_unlock(&dev
->lock
);
348 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream
*substream
,
349 struct snd_pcm_hw_params
*hw_params
)
352 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
354 if (dev
->disconnected
)
357 dprintk("Setting capture parameters\n");
359 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
360 params_buffer_bytes(hw_params
));
364 /* TODO: set up em28xx audio chip to deliver the correct audio format,
365 current default is 48000hz multiplexed => 96000hz mono
366 which shouldn't matter since analogue TV only supports mono */
367 unsigned int channels
, rate
, format
;
369 format
= params_format(hw_params
);
370 rate
= params_rate(hw_params
);
371 channels
= params_channels(hw_params
);
377 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream
*substream
)
379 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
381 dprintk("Stop capture, if needed\n");
383 if (atomic_read(&dev
->stream_started
) > 0) {
384 atomic_set(&dev
->stream_started
, 0);
385 schedule_work(&dev
->wq_trigger
);
391 static int snd_em28xx_prepare(struct snd_pcm_substream
*substream
)
393 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
395 if (dev
->disconnected
)
398 dev
->adev
.hwptr_done_capture
= 0;
399 dev
->adev
.capture_transfer_done
= 0;
404 static void audio_trigger(struct work_struct
*work
)
406 struct em28xx
*dev
= container_of(work
, struct em28xx
, wq_trigger
);
408 if (atomic_read(&dev
->stream_started
)) {
409 dprintk("starting capture");
410 em28xx_init_audio_isoc(dev
);
412 dprintk("stopping capture");
413 em28xx_deinit_isoc_audio(dev
);
417 static int snd_em28xx_capture_trigger(struct snd_pcm_substream
*substream
,
420 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
423 if (dev
->disconnected
)
427 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
: /* fall through */
428 case SNDRV_PCM_TRIGGER_RESUME
: /* fall through */
429 case SNDRV_PCM_TRIGGER_START
:
430 atomic_set(&dev
->stream_started
, 1);
432 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
: /* fall through */
433 case SNDRV_PCM_TRIGGER_SUSPEND
: /* fall through */
434 case SNDRV_PCM_TRIGGER_STOP
:
435 atomic_set(&dev
->stream_started
, 0);
440 schedule_work(&dev
->wq_trigger
);
444 static snd_pcm_uframes_t
snd_em28xx_capture_pointer(struct snd_pcm_substream
449 snd_pcm_uframes_t hwptr_done
;
451 dev
= snd_pcm_substream_chip(substream
);
452 if (dev
->disconnected
)
453 return SNDRV_PCM_POS_XRUN
;
455 spin_lock_irqsave(&dev
->adev
.slock
, flags
);
456 hwptr_done
= dev
->adev
.hwptr_done_capture
;
457 spin_unlock_irqrestore(&dev
->adev
.slock
, flags
);
462 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
463 unsigned long offset
)
465 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
467 return vmalloc_to_page(pageptr
);
471 * AC97 volume control support
473 static int em28xx_vol_info(struct snd_kcontrol
*kcontrol
,
474 struct snd_ctl_elem_info
*info
)
476 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
478 if (dev
->disconnected
)
481 info
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
483 info
->value
.integer
.min
= 0;
484 info
->value
.integer
.max
= 0x1f;
489 static int em28xx_vol_put(struct snd_kcontrol
*kcontrol
,
490 struct snd_ctl_elem_value
*value
)
492 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
493 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
494 u16 val
= (0x1f - (value
->value
.integer
.value
[0] & 0x1f)) |
495 (0x1f - (value
->value
.integer
.value
[1] & 0x1f)) << 8;
499 if (dev
->disconnected
)
503 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
505 if (!mutex_trylock(&dev
->lock
))
508 mutex_lock(&dev
->lock
);
509 rc
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
513 val
|= rc
& 0x8000; /* Preserve the mute flag */
515 rc
= em28xx_write_ac97(dev
, kcontrol
->private_value
, val
);
519 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
520 (val
& 0x8000) ? "muted " : "",
521 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
522 val
, (int)kcontrol
->private_value
);
525 mutex_unlock(&dev
->lock
);
529 static int em28xx_vol_get(struct snd_kcontrol
*kcontrol
,
530 struct snd_ctl_elem_value
*value
)
532 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
533 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
537 if (dev
->disconnected
)
541 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
543 if (!mutex_trylock(&dev
->lock
))
546 mutex_lock(&dev
->lock
);
547 val
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
548 mutex_unlock(&dev
->lock
);
552 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
553 (val
& 0x8000) ? "muted " : "",
554 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
555 val
, (int)kcontrol
->private_value
);
557 value
->value
.integer
.value
[0] = 0x1f - (val
& 0x1f);
558 value
->value
.integer
.value
[1] = 0x1f - ((val
<< 8) & 0x1f);
563 static int em28xx_vol_put_mute(struct snd_kcontrol
*kcontrol
,
564 struct snd_ctl_elem_value
*value
)
566 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
567 u16 val
= value
->value
.integer
.value
[0];
568 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
572 if (dev
->disconnected
)
576 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
578 if (!mutex_trylock(&dev
->lock
))
581 mutex_lock(&dev
->lock
);
582 rc
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
591 rc
= em28xx_write_ac97(dev
, kcontrol
->private_value
, rc
);
595 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
596 (val
& 0x8000) ? "muted " : "",
597 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
598 val
, (int)kcontrol
->private_value
);
601 mutex_unlock(&dev
->lock
);
605 static int em28xx_vol_get_mute(struct snd_kcontrol
*kcontrol
,
606 struct snd_ctl_elem_value
*value
)
608 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
609 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
613 if (dev
->disconnected
)
617 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
619 if (!mutex_trylock(&dev
->lock
))
622 mutex_lock(&dev
->lock
);
623 val
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
624 mutex_unlock(&dev
->lock
);
629 value
->value
.integer
.value
[0] = 0;
631 value
->value
.integer
.value
[0] = 1;
633 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
634 (val
& 0x8000) ? "muted " : "",
635 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
636 val
, (int)kcontrol
->private_value
);
641 static const DECLARE_TLV_DB_SCALE(em28xx_db_scale
, -3450, 150, 0);
643 static int em28xx_cvol_new(struct snd_card
*card
, struct em28xx
*dev
,
648 struct snd_kcontrol
*kctl
;
649 struct snd_kcontrol_new tmp
;
651 memset (&tmp
, 0, sizeof(tmp
));
652 tmp
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
653 tmp
.private_value
= id
,
656 /* Add Mute Control */
657 sprintf(ctl_name
, "%s Switch", name
);
658 tmp
.get
= em28xx_vol_get_mute
;
659 tmp
.put
= em28xx_vol_put_mute
;
660 tmp
.info
= snd_ctl_boolean_mono_info
;
661 kctl
= snd_ctl_new1(&tmp
, dev
);
662 err
= snd_ctl_add(card
, kctl
);
665 dprintk("Added control %s for ac97 volume control 0x%04x\n",
668 memset (&tmp
, 0, sizeof(tmp
));
669 tmp
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
670 tmp
.private_value
= id
,
673 /* Add Volume Control */
674 sprintf(ctl_name
, "%s Volume", name
);
675 tmp
.get
= em28xx_vol_get
;
676 tmp
.put
= em28xx_vol_put
;
677 tmp
.info
= em28xx_vol_info
;
678 tmp
.tlv
.p
= em28xx_db_scale
,
679 kctl
= snd_ctl_new1(&tmp
, dev
);
680 err
= snd_ctl_add(card
, kctl
);
683 dprintk("Added control %s for ac97 volume control 0x%04x\n",
690 * register/unregister code and data
692 static struct snd_pcm_ops snd_em28xx_pcm_capture
= {
693 .open
= snd_em28xx_capture_open
,
694 .close
= snd_em28xx_pcm_close
,
695 .ioctl
= snd_pcm_lib_ioctl
,
696 .hw_params
= snd_em28xx_hw_capture_params
,
697 .hw_free
= snd_em28xx_hw_capture_free
,
698 .prepare
= snd_em28xx_prepare
,
699 .trigger
= snd_em28xx_capture_trigger
,
700 .pointer
= snd_em28xx_capture_pointer
,
701 .page
= snd_pcm_get_vmalloc_page
,
704 static void em28xx_audio_free_urb(struct em28xx
*dev
)
708 for (i
= 0; i
< dev
->adev
.num_urb
; i
++) {
709 struct urb
*urb
= dev
->adev
.urb
[i
];
714 usb_free_coherent(dev
->udev
, urb
->transfer_buffer_length
,
715 dev
->adev
.transfer_buffer
[i
],
720 kfree(dev
->adev
.urb
);
721 kfree(dev
->adev
.transfer_buffer
);
722 dev
->adev
.num_urb
= 0;
725 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
726 static int em28xx_audio_ep_packet_size(struct usb_device
*udev
,
727 struct usb_endpoint_descriptor
*e
)
729 int size
= le16_to_cpu(e
->wMaxPacketSize
);
731 if (udev
->speed
== USB_SPEED_HIGH
)
732 return (size
& 0x7ff) * (1 + (((size
) >> 11) & 0x03));
737 static int em28xx_audio_urb_init(struct em28xx
*dev
)
739 struct usb_interface
*intf
;
740 struct usb_endpoint_descriptor
*e
, *ep
= NULL
;
741 int i
, ep_size
, interval
, num_urb
, npackets
;
742 int urb_size
, bytes_per_transfer
;
750 intf
= usb_ifnum_to_if(dev
->udev
, dev
->ifnum
);
752 if (intf
->num_altsetting
<= alt
) {
753 em28xx_errdev("alt %d doesn't exist on interface %d\n",
758 for (i
= 0; i
< intf
->altsetting
[alt
].desc
.bNumEndpoints
; i
++) {
759 e
= &intf
->altsetting
[alt
].endpoint
[i
].desc
;
760 if (!usb_endpoint_dir_in(e
))
762 if (e
->bEndpointAddress
== EM28XX_EP_AUDIO
) {
769 em28xx_errdev("Couldn't find an audio endpoint");
773 ep_size
= em28xx_audio_ep_packet_size(dev
->udev
, ep
);
774 interval
= 1 << (ep
->bInterval
- 1);
776 em28xx_info("Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
777 EM28XX_EP_AUDIO
, usb_speed_string(dev
->udev
->speed
),
782 /* Calculate the number and size of URBs to better fit the audio samples */
785 * Estimate the number of bytes per DMA transfer.
787 * This is given by the bit rate (for now, only 48000 Hz) multiplied
788 * by 2 channels and 2 bytes/sample divided by the number of microframe
789 * intervals and by the microframe rate (125 us)
791 bytes_per_transfer
= DIV_ROUND_UP(48000 * 2 * 2, 125 * interval
);
794 * Estimate the number of transfer URBs. Don't let it go past the
795 * maximum number of URBs that is known to be supported by the device.
797 num_urb
= DIV_ROUND_UP(bytes_per_transfer
, ep_size
);
798 if (num_urb
> EM28XX_MAX_AUDIO_BUFS
)
799 num_urb
= EM28XX_MAX_AUDIO_BUFS
;
802 * Now that we know the number of bytes per transfer and the number of
803 * URBs, estimate the typical size of an URB, in order to adjust the
804 * minimal number of packets.
806 urb_size
= bytes_per_transfer
/ num_urb
;
809 * Now, calculate the amount of audio packets to be filled on each
810 * URB. In order to preserve the old behaviour, use a minimal
811 * threshold for this value.
813 npackets
= EM28XX_MIN_AUDIO_PACKETS
;
814 if (urb_size
> ep_size
* npackets
)
815 npackets
= DIV_ROUND_UP(urb_size
, ep_size
);
817 em28xx_info("Number of URBs: %d, with %d packets and %d size",
818 num_urb
, npackets
, urb_size
);
820 /* Estimate the bytes per period */
821 dev
->adev
.period
= urb_size
* npackets
;
823 /* Allocate space to store the number of URBs to be used */
825 dev
->adev
.transfer_buffer
= kcalloc(num_urb
,
826 sizeof(*dev
->adev
.transfer_buffer
),
828 if (!dev
->adev
.transfer_buffer
) {
832 dev
->adev
.urb
= kcalloc(num_urb
, sizeof(*dev
->adev
.urb
), GFP_ATOMIC
);
833 if (!dev
->adev
.urb
) {
834 kfree(dev
->adev
.transfer_buffer
);
838 /* Alloc memory for each URB and for each transfer buffer */
839 dev
->adev
.num_urb
= num_urb
;
840 for (i
= 0; i
< num_urb
; i
++) {
845 urb
= usb_alloc_urb(npackets
, GFP_ATOMIC
);
847 em28xx_errdev("usb_alloc_urb failed!\n");
848 em28xx_audio_free_urb(dev
);
851 dev
->adev
.urb
[i
] = urb
;
853 buf
= usb_alloc_coherent(dev
->udev
, npackets
* ep_size
, GFP_ATOMIC
,
856 em28xx_errdev("usb_alloc_coherent failed!\n");
857 em28xx_audio_free_urb(dev
);
860 dev
->adev
.transfer_buffer
[i
] = buf
;
862 urb
->dev
= dev
->udev
;
864 urb
->pipe
= usb_rcvisocpipe(dev
->udev
, EM28XX_EP_AUDIO
);
865 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
866 urb
->transfer_buffer
= buf
;
867 urb
->interval
= interval
;
868 urb
->complete
= em28xx_audio_isocirq
;
869 urb
->number_of_packets
= npackets
;
870 urb
->transfer_buffer_length
= ep_size
* npackets
;
872 for (j
= k
= 0; j
< npackets
; j
++, k
+= ep_size
) {
873 urb
->iso_frame_desc
[j
].offset
= k
;
874 urb
->iso_frame_desc
[j
].length
= ep_size
;
881 static int em28xx_audio_init(struct em28xx
*dev
)
883 struct em28xx_audio
*adev
= &dev
->adev
;
885 struct snd_card
*card
;
889 if (!dev
->has_alsa_audio
) {
890 /* This device does not support the extension (in this case
891 the device is expecting the snd-usb-audio module or
892 doesn't have analog audio support at all) */
896 em28xx_info("Binding audio extension\n");
898 printk(KERN_INFO
"em28xx-audio.c: Copyright (C) 2006 Markus "
901 "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n");
903 err
= snd_card_create(index
[devnr
], "Em28xx Audio", THIS_MODULE
, 0,
908 spin_lock_init(&adev
->slock
);
909 adev
->sndcard
= card
;
910 adev
->udev
= dev
->udev
;
912 err
= snd_pcm_new(card
, "Em28xx Audio", 0, 0, 1, &pcm
);
916 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_em28xx_pcm_capture
);
918 pcm
->private_data
= dev
;
919 strcpy(pcm
->name
, "Empia 28xx Capture");
921 snd_card_set_dev(card
, &dev
->udev
->dev
);
922 strcpy(card
->driver
, "Em28xx-Audio");
923 strcpy(card
->shortname
, "Em28xx Audio");
924 strcpy(card
->longname
, "Empia Em28xx Audio");
926 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
928 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
929 em28xx_cvol_new(card
, dev
, "Video", AC97_VIDEO
);
930 em28xx_cvol_new(card
, dev
, "Line In", AC97_LINE
);
931 em28xx_cvol_new(card
, dev
, "Phone", AC97_PHONE
);
932 em28xx_cvol_new(card
, dev
, "Microphone", AC97_MIC
);
933 em28xx_cvol_new(card
, dev
, "CD", AC97_CD
);
934 em28xx_cvol_new(card
, dev
, "AUX", AC97_AUX
);
935 em28xx_cvol_new(card
, dev
, "PCM", AC97_PCM
);
937 em28xx_cvol_new(card
, dev
, "Master", AC97_MASTER
);
938 em28xx_cvol_new(card
, dev
, "Line", AC97_HEADPHONE
);
939 em28xx_cvol_new(card
, dev
, "Mono", AC97_MASTER_MONO
);
940 em28xx_cvol_new(card
, dev
, "LFE", AC97_CENTER_LFE_MASTER
);
941 em28xx_cvol_new(card
, dev
, "Surround", AC97_SURROUND_MASTER
);
944 err
= em28xx_audio_urb_init(dev
);
948 err
= snd_card_register(card
);
952 em28xx_info("Audio extension successfully initialized\n");
956 em28xx_audio_free_urb(dev
);
960 adev
->sndcard
= NULL
;
965 static int em28xx_audio_fini(struct em28xx
*dev
)
970 if (dev
->has_alsa_audio
!= 1) {
971 /* This device does not support the extension (in this case
972 the device is expecting the snd-usb-audio module or
973 doesn't have analog audio support at all) */
977 em28xx_info("Closing audio extension");
979 if (dev
->adev
.sndcard
) {
980 snd_card_disconnect(dev
->adev
.sndcard
);
981 flush_work(&dev
->wq_trigger
);
983 em28xx_audio_free_urb(dev
);
985 snd_card_free(dev
->adev
.sndcard
);
986 dev
->adev
.sndcard
= NULL
;
992 static struct em28xx_ops audio_ops
= {
994 .name
= "Em28xx Audio Extension",
995 .init
= em28xx_audio_init
,
996 .fini
= em28xx_audio_fini
,
999 static int __init
em28xx_alsa_register(void)
1001 return em28xx_register_extension(&audio_ops
);
1004 static void __exit
em28xx_alsa_unregister(void)
1006 em28xx_unregister_extension(&audio_ops
);
1009 MODULE_LICENSE("GPL");
1010 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
1011 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1012 MODULE_DESCRIPTION(DRIVER_DESC
" - audio interface");
1013 MODULE_VERSION(EM28XX_VERSION
);
1015 module_init(em28xx_alsa_register
);
1016 module_exit(em28xx_alsa_unregister
);