// SPDX-License-Identifier: GPL-2.0+ // // Empiatech em28x1 audio extension // // Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com> // // Copyright (C) 2007-2016 Mauro Carvalho Chehab // - Port to work with the in-kernel driver // - Cleanups, fixes, alsa-controls, etc. // // This driver is based on my previous au600 usb pstn audio driver // and inherits all the copyrights
.rate_min = 48000,
.rate_max = 48000,
.channels_min = 2,
.channels_max = 2,
.buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
/* * The period is 12.288 bytes. Allow a 10% of variation along its * value, in order to avoid overruns/underruns due to some clock * drift. * * FIXME: This period assumes 64 packets, and a 48000 PCM rate. * Calculate it dynamically.
*/
.period_bytes_min = 11059,
.period_bytes_max = 13516,
staticint snd_em28xx_capture_open(struct snd_pcm_substream *substream)
{ struct em28xx *dev = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; int nonblock, ret = 0;
if (!dev) {
pr_err("em28xx-audio: BUG: em28xx can't find device struct. Can't proceed with open\n"); return -ENODEV;
}
if (dev->disconnected) return -ENODEV;
dprintk("opening device and trying to acquire exclusive lock\n");
nonblock = !!(substream->f_flags & O_NONBLOCK); if (nonblock) { if (!mutex_trylock(&dev->lock)) return -EAGAIN;
} else {
mutex_lock(&dev->lock);
}
runtime->hw = snd_em28xx_hw_capture;
if (dev->adev.users == 0) { if (!dev->alt || dev->is_audio_only) { struct usb_device *udev;
udev = interface_to_usbdev(dev->intf);
if (dev->is_audio_only) /* audio is on a separate interface */
dev->alt = 1; else /* audio is on the same interface as video */
dev->alt = 7; /* * FIXME: The intention seems to be to select * the alt setting with the largest * wMaxPacketSize for the video endpoint. * At least dev->alt should be used instead, but * we should probably not touch it at all if it * is already >0, because wMaxPacketSize of the * audio endpoints seems to be the same for all.
*/
dprintk("changing alternate number on interface %d to %d\n",
dev->ifnum, dev->alt);
usb_set_interface(udev, dev->ifnum, dev->alt);
}
/* Sets volume, mute, etc */
dev->mute = 0;
ret = em28xx_audio_analog_set(dev); if (ret < 0) goto err;
}
staticint snd_em28xx_capture_trigger(struct snd_pcm_substream *substream, int cmd)
{ struct em28xx *dev = snd_pcm_substream_chip(substream); int retval = 0;
if (dev->disconnected) return -ENODEV;
switch (cmd) { case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: case SNDRV_PCM_TRIGGER_RESUME: case SNDRV_PCM_TRIGGER_START:
atomic_set(&dev->adev.stream_started, 1); break; case SNDRV_PCM_TRIGGER_PAUSE_PUSH: case SNDRV_PCM_TRIGGER_SUSPEND: case SNDRV_PCM_TRIGGER_STOP:
atomic_set(&dev->adev.stream_started, 0); break; default:
retval = -EINVAL;
}
schedule_work(&dev->adev.wq_trigger); return retval;
}
staticint em28xx_audio_urb_init(struct em28xx *dev)
{ struct usb_interface *intf; struct usb_endpoint_descriptor *e, *ep = NULL; struct usb_device *udev = interface_to_usbdev(dev->intf); int i, ep_size, interval, num_urb, npackets; int urb_size, bytes_per_transfer;
u8 alt;
if (dev->ifnum)
alt = 1; else
alt = 7;
intf = usb_ifnum_to_if(udev, dev->ifnum);
if (intf->num_altsetting <= alt) {
dev_err(&dev->intf->dev, "alt %d doesn't exist on interface %d\n",
dev->ifnum, alt); return -ENODEV;
}
for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) {
e = &intf->altsetting[alt].endpoint[i].desc; if (!usb_endpoint_dir_in(e)) continue; if (e->bEndpointAddress == EM28XX_EP_AUDIO) {
ep = e; break;
}
}
if (!ep) {
dev_err(&dev->intf->dev, "Couldn't find an audio endpoint"); return -ENODEV;
}
dev_info(&dev->intf->dev, "Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
EM28XX_EP_AUDIO, usb_speed_string(udev->speed),
dev->ifnum, alt, interval, ep_size);
/* Calculate the number and size of URBs to better fit the audio samples */
/* * Estimate the number of bytes per DMA transfer. * * This is given by the bit rate (for now, only 48000 Hz) multiplied * by 2 channels and 2 bytes/sample divided by the number of microframe * intervals and by the microframe rate (125 us)
*/
bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval);
/* * Estimate the number of transfer URBs. Don't let it go past the * maximum number of URBs that is known to be supported by the device.
*/
num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size); if (num_urb > EM28XX_MAX_AUDIO_BUFS)
num_urb = EM28XX_MAX_AUDIO_BUFS;
/* * Now that we know the number of bytes per transfer and the number of * URBs, estimate the typical size of an URB, in order to adjust the * minimal number of packets.
*/
urb_size = bytes_per_transfer / num_urb;
/* * Now, calculate the amount of audio packets to be filled on each * URB. In order to preserve the old behaviour, use a minimal * threshold for this value.
*/
npackets = EM28XX_MIN_AUDIO_PACKETS; if (urb_size > ep_size * npackets)
npackets = DIV_ROUND_UP(urb_size, ep_size);
dev_info(&dev->intf->dev, "Number of URBs: %d, with %d packets and %d size\n",
num_urb, npackets, urb_size);
/* Estimate the bytes per period */
dev->adev.period = urb_size * npackets;
/* Allocate space to store the number of URBs to be used */
dev->adev.transfer_buffer = kcalloc(num_urb, sizeof(*dev->adev.transfer_buffer),
GFP_KERNEL); if (!dev->adev.transfer_buffer) return -ENOMEM;
/* Alloc memory for each URB and for each transfer buffer */
dev->adev.num_urb = num_urb; for (i = 0; i < num_urb; i++) { struct urb *urb; int j, k; void *buf;
if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) { /* * This device does not support the extension (in this case * the device is expecting the snd-usb-audio module or * doesn't have analog audio support at all)
*/ return 0;
}
staticint em28xx_audio_fini(struct em28xx *dev)
{ if (!dev) return 0;
if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) { /* * This device does not support the extension (in this case * the device is expecting the snd-usb-audio module or * doesn't have analog audio support at all)
*/ return 0;
}
staticint em28xx_audio_resume(struct em28xx *dev)
{ if (!dev) return 0;
if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) return 0;
dev_info(&dev->intf->dev, "Resuming audio extension\n"); /* Nothing to do other than schedule_work() ?? */
schedule_work(&dev->adev.wq_trigger); return 0;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.