1 // SPDX-License-Identifier: GPL-2.0+
3 // Empiatech em28x1 audio extension
5 // Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
7 // Copyright (C) 2007-2016 Mauro Carvalho Chehab
8 // - Port to work with the in-kernel driver
9 // - Cleanups, fixes, alsa-controls, etc.
11 // This driver is based on my previous au600 usb pstn audio driver
12 // and inherits all the copyrights
16 #include <linux/kernel.h>
17 #include <linux/usb.h>
18 #include <linux/init.h>
19 #include <linux/sound.h>
20 #include <linux/spinlock.h>
21 #include <linux/soundcard.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/info.h>
28 #include <sound/initval.h>
29 #include <sound/control.h>
30 #include <sound/tlv.h>
31 #include <sound/ac97_codec.h>
32 #include <media/v4l2-common.h>
35 module_param(debug
, int, 0644);
36 MODULE_PARM_DESC(debug
, "activates debug info");
38 #define EM28XX_MAX_AUDIO_BUFS 5
39 #define EM28XX_MIN_AUDIO_PACKETS 64
41 #define dprintk(fmt, arg...) do { \
43 dev_printk(KERN_DEBUG, &dev->intf->dev, \
44 "video: %s: " fmt, __func__, ## arg); \
47 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
49 static int em28xx_deinit_isoc_audio(struct em28xx
*dev
)
53 dprintk("Stopping isoc\n");
54 for (i
= 0; i
< dev
->adev
.num_urb
; i
++) {
55 struct urb
*urb
= dev
->adev
.urb
[i
];
66 static void em28xx_audio_isocirq(struct urb
*urb
)
68 struct em28xx
*dev
= urb
->context
;
71 int period_elapsed
= 0;
75 struct snd_pcm_substream
*substream
;
76 struct snd_pcm_runtime
*runtime
;
78 if (dev
->disconnected
) {
79 dprintk("device disconnected while streaming. URB status=%d.\n",
81 atomic_set(&dev
->adev
.stream_started
, 0);
85 switch (urb
->status
) {
87 case -ETIMEDOUT
: /* NAK */
89 case -ECONNRESET
: /* kill */
94 dprintk("urb completion error %d.\n", urb
->status
);
98 if (atomic_read(&dev
->adev
.stream_started
) == 0)
101 if (dev
->adev
.capture_pcm_substream
) {
102 substream
= dev
->adev
.capture_pcm_substream
;
103 runtime
= substream
->runtime
;
104 stride
= runtime
->frame_bits
>> 3;
106 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
109 urb
->iso_frame_desc
[i
].actual_length
/ stride
;
110 cp
= (unsigned char *)urb
->transfer_buffer
+
111 urb
->iso_frame_desc
[i
].offset
;
116 oldptr
= dev
->adev
.hwptr_done_capture
;
117 if (oldptr
+ length
>= runtime
->buffer_size
) {
119 runtime
->buffer_size
- oldptr
;
120 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
122 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
123 length
* stride
- cnt
* stride
);
125 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
129 snd_pcm_stream_lock_irqsave(substream
, flags
);
131 dev
->adev
.hwptr_done_capture
+= length
;
132 if (dev
->adev
.hwptr_done_capture
>=
133 runtime
->buffer_size
)
134 dev
->adev
.hwptr_done_capture
-=
135 runtime
->buffer_size
;
137 dev
->adev
.capture_transfer_done
+= length
;
138 if (dev
->adev
.capture_transfer_done
>=
139 runtime
->period_size
) {
140 dev
->adev
.capture_transfer_done
-=
141 runtime
->period_size
;
145 snd_pcm_stream_unlock_irqrestore(substream
, flags
);
148 snd_pcm_period_elapsed(substream
);
152 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
154 dev_err(&dev
->intf
->dev
,
155 "resubmit of audio urb failed (error=%i)\n",
159 static int em28xx_init_audio_isoc(struct em28xx
*dev
)
163 dprintk("Starting isoc transfers\n");
165 /* Start streaming */
166 for (i
= 0; i
< dev
->adev
.num_urb
; i
++) {
167 memset(dev
->adev
.transfer_buffer
[i
], 0x80,
168 dev
->adev
.urb
[i
]->transfer_buffer_length
);
170 err
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
172 dev_err(&dev
->intf
->dev
,
173 "submit of audio urb failed (error=%i)\n",
175 em28xx_deinit_isoc_audio(dev
);
176 atomic_set(&dev
->adev
.stream_started
, 0);
184 static const struct snd_pcm_hardware snd_em28xx_hw_capture
= {
185 .info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
|
186 SNDRV_PCM_INFO_MMAP
|
187 SNDRV_PCM_INFO_INTERLEAVED
|
188 SNDRV_PCM_INFO_BATCH
|
189 SNDRV_PCM_INFO_MMAP_VALID
,
191 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
193 .rates
= SNDRV_PCM_RATE_48000
,
199 .buffer_bytes_max
= 62720 * 8, /* just about the value in usbaudio.c */
202 * The period is 12.288 bytes. Allow a 10% of variation along its
203 * value, in order to avoid overruns/underruns due to some clock
206 * FIXME: This period assumes 64 packets, and a 48000 PCM rate.
207 * Calculate it dynamically.
209 .period_bytes_min
= 11059,
210 .period_bytes_max
= 13516,
213 .periods_max
= 98, /* 12544, */
216 static int snd_em28xx_capture_open(struct snd_pcm_substream
*substream
)
218 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
219 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
220 int nonblock
, ret
= 0;
223 pr_err("em28xx-audio: BUG: em28xx can't find device struct. Can't proceed with open\n");
227 if (dev
->disconnected
)
230 dprintk("opening device and trying to acquire exclusive lock\n");
232 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
234 if (!mutex_trylock(&dev
->lock
))
237 mutex_lock(&dev
->lock
);
240 runtime
->hw
= snd_em28xx_hw_capture
;
242 if (dev
->adev
.users
== 0) {
243 if (!dev
->alt
|| dev
->is_audio_only
) {
244 struct usb_device
*udev
;
246 udev
= interface_to_usbdev(dev
->intf
);
248 if (dev
->is_audio_only
)
249 /* audio is on a separate interface */
252 /* audio is on the same interface as video */
255 * FIXME: The intention seems to be to select
256 * the alt setting with the largest
257 * wMaxPacketSize for the video endpoint.
258 * At least dev->alt should be used instead, but
259 * we should probably not touch it at all if it
260 * is already >0, because wMaxPacketSize of the
261 * audio endpoints seems to be the same for all.
263 dprintk("changing alternate number on interface %d to %d\n",
264 dev
->ifnum
, dev
->alt
);
265 usb_set_interface(udev
, dev
->ifnum
, dev
->alt
);
268 /* Sets volume, mute, etc */
270 ret
= em28xx_audio_analog_set(dev
);
277 mutex_unlock(&dev
->lock
);
279 /* Dynamically adjust the period size */
280 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
281 snd_pcm_hw_constraint_minmax(runtime
, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
,
282 dev
->adev
.period
* 95 / 100,
283 dev
->adev
.period
* 105 / 100);
285 dev
->adev
.capture_pcm_substream
= substream
;
289 mutex_unlock(&dev
->lock
);
291 dev_err(&dev
->intf
->dev
,
292 "Error while configuring em28xx mixer\n");
296 static int snd_em28xx_pcm_close(struct snd_pcm_substream
*substream
)
298 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
300 dprintk("closing device\n");
303 mutex_lock(&dev
->lock
);
305 if (atomic_read(&dev
->adev
.stream_started
) > 0) {
306 atomic_set(&dev
->adev
.stream_started
, 0);
307 schedule_work(&dev
->adev
.wq_trigger
);
310 em28xx_audio_analog_set(dev
);
311 mutex_unlock(&dev
->lock
);
312 kref_put(&dev
->ref
, em28xx_free_device
);
317 static int snd_em28xx_prepare(struct snd_pcm_substream
*substream
)
319 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
321 if (dev
->disconnected
)
324 dev
->adev
.hwptr_done_capture
= 0;
325 dev
->adev
.capture_transfer_done
= 0;
330 static void audio_trigger(struct work_struct
*work
)
332 struct em28xx_audio
*adev
=
333 container_of(work
, struct em28xx_audio
, wq_trigger
);
334 struct em28xx
*dev
= container_of(adev
, struct em28xx
, adev
);
336 if (atomic_read(&adev
->stream_started
)) {
337 dprintk("starting capture");
338 em28xx_init_audio_isoc(dev
);
340 dprintk("stopping capture");
341 em28xx_deinit_isoc_audio(dev
);
345 static int snd_em28xx_capture_trigger(struct snd_pcm_substream
*substream
,
348 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
351 if (dev
->disconnected
)
355 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
356 case SNDRV_PCM_TRIGGER_RESUME
:
357 case SNDRV_PCM_TRIGGER_START
:
358 atomic_set(&dev
->adev
.stream_started
, 1);
360 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
361 case SNDRV_PCM_TRIGGER_SUSPEND
:
362 case SNDRV_PCM_TRIGGER_STOP
:
363 atomic_set(&dev
->adev
.stream_started
, 0);
368 schedule_work(&dev
->adev
.wq_trigger
);
372 static snd_pcm_uframes_t
snd_em28xx_capture_pointer(struct snd_pcm_substream
377 snd_pcm_uframes_t hwptr_done
;
379 dev
= snd_pcm_substream_chip(substream
);
380 if (dev
->disconnected
)
381 return SNDRV_PCM_POS_XRUN
;
383 spin_lock_irqsave(&dev
->adev
.slock
, flags
);
384 hwptr_done
= dev
->adev
.hwptr_done_capture
;
385 spin_unlock_irqrestore(&dev
->adev
.slock
, flags
);
391 * AC97 volume control support
393 static int em28xx_vol_info(struct snd_kcontrol
*kcontrol
,
394 struct snd_ctl_elem_info
*info
)
396 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
398 if (dev
->disconnected
)
401 info
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
403 info
->value
.integer
.min
= 0;
404 info
->value
.integer
.max
= 0x1f;
409 static int em28xx_vol_put(struct snd_kcontrol
*kcontrol
,
410 struct snd_ctl_elem_value
*value
)
412 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
413 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
414 u16 val
= (0x1f - (value
->value
.integer
.value
[0] & 0x1f)) |
415 (0x1f - (value
->value
.integer
.value
[1] & 0x1f)) << 8;
419 if (dev
->disconnected
)
423 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
425 if (!mutex_trylock(&dev
->lock
))
428 mutex_lock(&dev
->lock
);
430 rc
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
434 val
|= rc
& 0x8000; /* Preserve the mute flag */
436 rc
= em28xx_write_ac97(dev
, kcontrol
->private_value
, val
);
440 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
441 (val
& 0x8000) ? "muted " : "",
442 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
443 val
, (int)kcontrol
->private_value
);
446 mutex_unlock(&dev
->lock
);
450 static int em28xx_vol_get(struct snd_kcontrol
*kcontrol
,
451 struct snd_ctl_elem_value
*value
)
453 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
454 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
458 if (dev
->disconnected
)
462 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
464 if (!mutex_trylock(&dev
->lock
))
467 mutex_lock(&dev
->lock
);
469 val
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
470 mutex_unlock(&dev
->lock
);
474 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
475 (val
& 0x8000) ? "muted " : "",
476 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
477 val
, (int)kcontrol
->private_value
);
479 value
->value
.integer
.value
[0] = 0x1f - (val
& 0x1f);
480 value
->value
.integer
.value
[1] = 0x1f - ((val
>> 8) & 0x1f);
485 static int em28xx_vol_put_mute(struct snd_kcontrol
*kcontrol
,
486 struct snd_ctl_elem_value
*value
)
488 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
489 u16 val
= value
->value
.integer
.value
[0];
490 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
494 if (dev
->disconnected
)
498 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
500 if (!mutex_trylock(&dev
->lock
))
503 mutex_lock(&dev
->lock
);
505 rc
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
514 rc
= em28xx_write_ac97(dev
, kcontrol
->private_value
, rc
);
518 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
519 (val
& 0x8000) ? "muted " : "",
520 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
521 val
, (int)kcontrol
->private_value
);
524 mutex_unlock(&dev
->lock
);
528 static int em28xx_vol_get_mute(struct snd_kcontrol
*kcontrol
,
529 struct snd_ctl_elem_value
*value
)
531 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
532 struct snd_pcm_substream
*substream
= dev
->adev
.capture_pcm_substream
;
536 if (dev
->disconnected
)
540 nonblock
= !!(substream
->f_flags
& O_NONBLOCK
);
542 if (!mutex_trylock(&dev
->lock
))
545 mutex_lock(&dev
->lock
);
547 val
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
548 mutex_unlock(&dev
->lock
);
553 value
->value
.integer
.value
[0] = 0;
555 value
->value
.integer
.value
[0] = 1;
557 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
558 (val
& 0x8000) ? "muted " : "",
559 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
560 val
, (int)kcontrol
->private_value
);
565 static const DECLARE_TLV_DB_SCALE(em28xx_db_scale
, -3450, 150, 0);
567 static int em28xx_cvol_new(struct snd_card
*card
, struct em28xx
*dev
,
572 struct snd_kcontrol
*kctl
;
573 struct snd_kcontrol_new tmp
;
575 memset(&tmp
, 0, sizeof(tmp
));
576 tmp
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
577 tmp
.private_value
= id
;
580 /* Add Mute Control */
581 sprintf(ctl_name
, "%s Switch", name
);
582 tmp
.get
= em28xx_vol_get_mute
;
583 tmp
.put
= em28xx_vol_put_mute
;
584 tmp
.info
= snd_ctl_boolean_mono_info
;
585 kctl
= snd_ctl_new1(&tmp
, dev
);
586 err
= snd_ctl_add(card
, kctl
);
589 dprintk("Added control %s for ac97 volume control 0x%04x\n",
592 memset(&tmp
, 0, sizeof(tmp
));
593 tmp
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
594 tmp
.private_value
= id
;
597 /* Add Volume Control */
598 sprintf(ctl_name
, "%s Volume", name
);
599 tmp
.get
= em28xx_vol_get
;
600 tmp
.put
= em28xx_vol_put
;
601 tmp
.info
= em28xx_vol_info
;
602 tmp
.tlv
.p
= em28xx_db_scale
;
603 kctl
= snd_ctl_new1(&tmp
, dev
);
604 err
= snd_ctl_add(card
, kctl
);
607 dprintk("Added control %s for ac97 volume control 0x%04x\n",
614 * register/unregister code and data
616 static const struct snd_pcm_ops snd_em28xx_pcm_capture
= {
617 .open
= snd_em28xx_capture_open
,
618 .close
= snd_em28xx_pcm_close
,
619 .prepare
= snd_em28xx_prepare
,
620 .trigger
= snd_em28xx_capture_trigger
,
621 .pointer
= snd_em28xx_capture_pointer
,
624 static void em28xx_audio_free_urb(struct em28xx
*dev
)
626 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
629 for (i
= 0; i
< dev
->adev
.num_urb
; i
++) {
630 struct urb
*urb
= dev
->adev
.urb
[i
];
635 usb_free_coherent(udev
, urb
->transfer_buffer_length
,
636 dev
->adev
.transfer_buffer
[i
],
641 kfree(dev
->adev
.urb
);
642 kfree(dev
->adev
.transfer_buffer
);
643 dev
->adev
.num_urb
= 0;
646 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
647 static int em28xx_audio_ep_packet_size(struct usb_device
*udev
,
648 struct usb_endpoint_descriptor
*e
)
650 int size
= le16_to_cpu(e
->wMaxPacketSize
);
652 if (udev
->speed
== USB_SPEED_HIGH
)
653 return (size
& 0x7ff) * (1 + (((size
) >> 11) & 0x03));
658 static int em28xx_audio_urb_init(struct em28xx
*dev
)
660 struct usb_interface
*intf
;
661 struct usb_endpoint_descriptor
*e
, *ep
= NULL
;
662 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
663 int i
, ep_size
, interval
, num_urb
, npackets
;
664 int urb_size
, bytes_per_transfer
;
672 intf
= usb_ifnum_to_if(udev
, dev
->ifnum
);
674 if (intf
->num_altsetting
<= alt
) {
675 dev_err(&dev
->intf
->dev
, "alt %d doesn't exist on interface %d\n",
680 for (i
= 0; i
< intf
->altsetting
[alt
].desc
.bNumEndpoints
; i
++) {
681 e
= &intf
->altsetting
[alt
].endpoint
[i
].desc
;
682 if (!usb_endpoint_dir_in(e
))
684 if (e
->bEndpointAddress
== EM28XX_EP_AUDIO
) {
691 dev_err(&dev
->intf
->dev
, "Couldn't find an audio endpoint");
695 ep_size
= em28xx_audio_ep_packet_size(udev
, ep
);
696 interval
= 1 << (ep
->bInterval
- 1);
698 dev_info(&dev
->intf
->dev
,
699 "Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
700 EM28XX_EP_AUDIO
, usb_speed_string(udev
->speed
),
701 dev
->ifnum
, alt
, interval
, ep_size
);
703 /* Calculate the number and size of URBs to better fit the audio samples */
706 * Estimate the number of bytes per DMA transfer.
708 * This is given by the bit rate (for now, only 48000 Hz) multiplied
709 * by 2 channels and 2 bytes/sample divided by the number of microframe
710 * intervals and by the microframe rate (125 us)
712 bytes_per_transfer
= DIV_ROUND_UP(48000 * 2 * 2, 125 * interval
);
715 * Estimate the number of transfer URBs. Don't let it go past the
716 * maximum number of URBs that is known to be supported by the device.
718 num_urb
= DIV_ROUND_UP(bytes_per_transfer
, ep_size
);
719 if (num_urb
> EM28XX_MAX_AUDIO_BUFS
)
720 num_urb
= EM28XX_MAX_AUDIO_BUFS
;
723 * Now that we know the number of bytes per transfer and the number of
724 * URBs, estimate the typical size of an URB, in order to adjust the
725 * minimal number of packets.
727 urb_size
= bytes_per_transfer
/ num_urb
;
730 * Now, calculate the amount of audio packets to be filled on each
731 * URB. In order to preserve the old behaviour, use a minimal
732 * threshold for this value.
734 npackets
= EM28XX_MIN_AUDIO_PACKETS
;
735 if (urb_size
> ep_size
* npackets
)
736 npackets
= DIV_ROUND_UP(urb_size
, ep_size
);
738 dev_info(&dev
->intf
->dev
,
739 "Number of URBs: %d, with %d packets and %d size\n",
740 num_urb
, npackets
, urb_size
);
742 /* Estimate the bytes per period */
743 dev
->adev
.period
= urb_size
* npackets
;
745 /* Allocate space to store the number of URBs to be used */
747 dev
->adev
.transfer_buffer
= kcalloc(num_urb
,
748 sizeof(*dev
->adev
.transfer_buffer
),
750 if (!dev
->adev
.transfer_buffer
)
753 dev
->adev
.urb
= kcalloc(num_urb
, sizeof(*dev
->adev
.urb
), GFP_KERNEL
);
754 if (!dev
->adev
.urb
) {
755 kfree(dev
->adev
.transfer_buffer
);
759 /* Alloc memory for each URB and for each transfer buffer */
760 dev
->adev
.num_urb
= num_urb
;
761 for (i
= 0; i
< num_urb
; i
++) {
766 urb
= usb_alloc_urb(npackets
, GFP_KERNEL
);
768 em28xx_audio_free_urb(dev
);
771 dev
->adev
.urb
[i
] = urb
;
773 buf
= usb_alloc_coherent(udev
, npackets
* ep_size
, GFP_KERNEL
,
776 dev_err(&dev
->intf
->dev
,
777 "usb_alloc_coherent failed!\n");
778 em28xx_audio_free_urb(dev
);
781 dev
->adev
.transfer_buffer
[i
] = buf
;
785 urb
->pipe
= usb_rcvisocpipe(udev
, EM28XX_EP_AUDIO
);
786 urb
->transfer_flags
= URB_ISO_ASAP
| URB_NO_TRANSFER_DMA_MAP
;
787 urb
->transfer_buffer
= buf
;
788 urb
->interval
= interval
;
789 urb
->complete
= em28xx_audio_isocirq
;
790 urb
->number_of_packets
= npackets
;
791 urb
->transfer_buffer_length
= ep_size
* npackets
;
793 for (j
= k
= 0; j
< npackets
; j
++, k
+= ep_size
) {
794 urb
->iso_frame_desc
[j
].offset
= k
;
795 urb
->iso_frame_desc
[j
].length
= ep_size
;
802 static int em28xx_audio_init(struct em28xx
*dev
)
804 struct em28xx_audio
*adev
= &dev
->adev
;
805 struct usb_device
*udev
= interface_to_usbdev(dev
->intf
);
807 struct snd_card
*card
;
811 if (dev
->usb_audio_type
!= EM28XX_USB_AUDIO_VENDOR
) {
813 * This device does not support the extension (in this case
814 * the device is expecting the snd-usb-audio module or
815 * doesn't have analog audio support at all)
820 dev_info(&dev
->intf
->dev
, "Binding audio extension\n");
824 dev_info(&dev
->intf
->dev
,
825 "em28xx-audio.c: Copyright (C) 2006 Markus Rechberger\n");
826 dev_info(&dev
->intf
->dev
,
827 "em28xx-audio.c: Copyright (C) 2007-2016 Mauro Carvalho Chehab\n");
829 err
= snd_card_new(&dev
->intf
->dev
, index
[devnr
], "Em28xx Audio",
830 THIS_MODULE
, 0, &card
);
834 spin_lock_init(&adev
->slock
);
835 adev
->sndcard
= card
;
838 err
= snd_pcm_new(card
, "Em28xx Audio", 0, 0, 1, &pcm
);
842 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_em28xx_pcm_capture
);
843 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_VMALLOC
, NULL
, 0, 0);
845 pcm
->private_data
= dev
;
846 strscpy(pcm
->name
, "Empia 28xx Capture", sizeof(pcm
->name
));
848 strscpy(card
->driver
, "Em28xx-Audio", sizeof(card
->driver
));
849 strscpy(card
->shortname
, "Em28xx Audio", sizeof(card
->shortname
));
850 strscpy(card
->longname
, "Empia Em28xx Audio", sizeof(card
->longname
));
852 INIT_WORK(&adev
->wq_trigger
, audio_trigger
);
854 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
855 em28xx_cvol_new(card
, dev
, "Video", AC97_VIDEO
);
856 em28xx_cvol_new(card
, dev
, "Line In", AC97_LINE
);
857 em28xx_cvol_new(card
, dev
, "Phone", AC97_PHONE
);
858 em28xx_cvol_new(card
, dev
, "Microphone", AC97_MIC
);
859 em28xx_cvol_new(card
, dev
, "CD", AC97_CD
);
860 em28xx_cvol_new(card
, dev
, "AUX", AC97_AUX
);
861 em28xx_cvol_new(card
, dev
, "PCM", AC97_PCM
);
863 em28xx_cvol_new(card
, dev
, "Master", AC97_MASTER
);
864 em28xx_cvol_new(card
, dev
, "Line", AC97_HEADPHONE
);
865 em28xx_cvol_new(card
, dev
, "Mono", AC97_MASTER_MONO
);
866 em28xx_cvol_new(card
, dev
, "LFE", AC97_CENTER_LFE_MASTER
);
867 em28xx_cvol_new(card
, dev
, "Surround", AC97_SURROUND_MASTER
);
870 err
= em28xx_audio_urb_init(dev
);
874 err
= snd_card_register(card
);
878 dev_info(&dev
->intf
->dev
, "Audio extension successfully initialized\n");
882 em28xx_audio_free_urb(dev
);
886 adev
->sndcard
= NULL
;
891 static int em28xx_audio_fini(struct em28xx
*dev
)
896 if (dev
->usb_audio_type
!= EM28XX_USB_AUDIO_VENDOR
) {
898 * This device does not support the extension (in this case
899 * the device is expecting the snd-usb-audio module or
900 * doesn't have analog audio support at all)
905 dev_info(&dev
->intf
->dev
, "Closing audio extension\n");
907 if (dev
->adev
.sndcard
) {
908 snd_card_disconnect(dev
->adev
.sndcard
);
909 flush_work(&dev
->adev
.wq_trigger
);
911 em28xx_audio_free_urb(dev
);
913 snd_card_free(dev
->adev
.sndcard
);
914 dev
->adev
.sndcard
= NULL
;
917 kref_put(&dev
->ref
, em28xx_free_device
);
921 static int em28xx_audio_suspend(struct em28xx
*dev
)
926 if (dev
->usb_audio_type
!= EM28XX_USB_AUDIO_VENDOR
)
929 dev_info(&dev
->intf
->dev
, "Suspending audio extension\n");
930 em28xx_deinit_isoc_audio(dev
);
931 atomic_set(&dev
->adev
.stream_started
, 0);
935 static int em28xx_audio_resume(struct em28xx
*dev
)
940 if (dev
->usb_audio_type
!= EM28XX_USB_AUDIO_VENDOR
)
943 dev_info(&dev
->intf
->dev
, "Resuming audio extension\n");
944 /* Nothing to do other than schedule_work() ?? */
945 schedule_work(&dev
->adev
.wq_trigger
);
949 static struct em28xx_ops audio_ops
= {
951 .name
= "Em28xx Audio Extension",
952 .init
= em28xx_audio_init
,
953 .fini
= em28xx_audio_fini
,
954 .suspend
= em28xx_audio_suspend
,
955 .resume
= em28xx_audio_resume
,
958 static int __init
em28xx_alsa_register(void)
960 return em28xx_register_extension(&audio_ops
);
963 static void __exit
em28xx_alsa_unregister(void)
965 em28xx_unregister_extension(&audio_ops
);
968 MODULE_LICENSE("GPL v2");
969 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
970 MODULE_AUTHOR("Mauro Carvalho Chehab");
971 MODULE_DESCRIPTION(DRIVER_DESC
" - audio interface");
972 MODULE_VERSION(EM28XX_VERSION
);
974 module_init(em28xx_alsa_register
);
975 module_exit(em28xx_alsa_unregister
);