Skip to contentSkip to conversation

USB audio and video class gadget drivers

PATCH In review Laurent Pinchart 2 participants 5 messages Sep 18, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover USB audio and video class gadget drivers

Hi everybody,

here are two new gadget function drivers for USB audio class and USB video class as well as a webcam gadget driver that combines both audio and video. All those drivers are work in progress (though not progressing much for the moment, as I'm busy with other development) and should probably not be applied before (at least) v2, but can still be useful as-is.

The code was developed and tested on TI DM365 hardware using a MUSB controller. I unfortunately don't have access to the hardware anymore for the time being, but I got an OMAP3-based platform in the meantime. If spare time permits I'll test the driver on the OMAP3 platform.

The audio class driver is based on Bryan Wu's work. It requires the "USB
gadget: Handle endpoint requests at the function level" patch that I've posted on the list. Only the microphone use case is supported at the moment. If anyone wants to implement speaker support patches are welcome :-)

The video class driver reuses some of the UVC host driver code, mostly for video buffers queue management. It currently has its own copy of the code, so there's room for improvement there.

If you look closely you will notice that the UVC driver uses the V4L2 device node to forward events (connection/disconnection, UVC request arrival, ...) to userspace. I will soon post an RFC to the linux-media list to document the interface.

The webcam driver combines a UAC microphone (at 16kHz) and a UVC camera (at 360p and 720p in YUYV and MJPEG).

Comments and ideas are welcome.

Signature · 3 lines
Best regards,

Laurent Pinchart
#1 lore raw

PATCH 1/3 USB gadget: audio class function driver

This USB audio class function driver exposes an ALSA interface to userspace to stream audio data from an application over USB.

---
 drivers/usb/gadget/f_uac.c     |  654 ++++++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/uac.h       |   99 ++++++
 drivers/usb/gadget/uac_alsa.c  |  348 +++++++++++++++++++++
 drivers/usb/gadget/uac_audio.c |  238 +++++++++++++++
 4 files changed, 1339 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/f_uac.c
 create mode 100644 drivers/usb/gadget/uac.h
 create mode 100644 drivers/usb/gadget/uac_alsa.c
 create mode 100644 drivers/usb/gadget/uac_audio.c
drivers/usb/gadget/f_uac.c new file +654 -0
new file mode 100644
index 0000000..aaacff1
@@ -0,0 +1,654 @@
+/*
+ *	f_uac.c -- USB Audio class function driver
+ *
+ *	Copyright (C) 2009
+ *         Laurent Pinchart ([email protected])
+ *
+ *	Based on f_audio.c
+ *	Copyright (C) 2008 Bryan Wu <[email protected]>
+ *	Copyright (C) 2008 Analog Devices, Inc
+ *
+ *	This program is free software; you can redistribute it and/or modify
+ *	it under the terms of the GNU General Public License as published by
+ *	the Free Software Foundation; either version 2 of the License, or
+ *	(at your option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <asm/atomic.h>
+
+#include "uac.h"
+
+/* I'd like 68 bytes packets, but for some reason the MUSB controller refuses
+ * to transfer 68 bytes isochronous packets. 64 bytes and 70 bytes work though.
+ * Go figure.
+ */
+#define IN_EP_MAX_PACKET_SIZE	70
+
+static int req_buf_size = IN_EP_MAX_PACKET_SIZE;
+module_param(req_buf_size, int, S_IRUGO);
+MODULE_PARM_DESC(req_buf_size, "ISO IN endpoint request buffer size");
+
+static int req_count = 256;
+module_param(req_count, int, S_IRUGO);
+MODULE_PARM_DESC(req_count, "ISO IN endpoint request count");
+
+static int audio_buf_size = 48000;
+module_param(audio_buf_size, int, S_IRUGO);
+MODULE_PARM_DESC(audio_buf_size, "Audio buffer size");
+
+static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value);
+static int generic_get_cmd(struct usb_audio_control *con, u8 cmd);
+
+/* --------------------------------------------------------------------------
+ * Function descriptors
+ */
+
+#define INPUT_TERMINAL_ID	1
+#define FEATURE_UNIT_ID		2
+#define OUTPUT_TERMINAL_ID	3
+
+static struct usb_interface_assoc_descriptor uac_iad __initdata = {
+	.bLength		= USB_DT_INTERFACE_ASSOCIATION_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
+	.bFirstInterface	= 0,
+	.bInterfaceCount	= 2,
+	.bFunctionClass		= USB_CLASS_AUDIO,
+	.bFunctionSubClass	= USB_SUBCLASS_AUDIOSTREAMING,	/* FIXME Where is this documented ? */
+	.bFunctionProtocol	= 0x00,
+	.iFunction		= 0,
+};
+
+/* B.3.1  Standard AC Interface Descriptor */
+static struct usb_interface_descriptor ac_interface_desc __initdata = {
+	.bLength		= USB_DT_INTERFACE_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE,
+	.bInterfaceNumber	= 0, /* dynamic */
+	.bAlternateSetting	= 0,
+	.bNumEndpoints		= 0,
+	.bInterfaceClass	= USB_CLASS_AUDIO,
+	.bInterfaceSubClass	= USB_SUBCLASS_AUDIOCONTROL,
+};
+
+DECLARE_UAC_AC_HEADER_DESCRIPTOR(2);
+
+/* B.3.2  Class-Specific AC Interface Descriptor */
+static struct uac_ac_header_descriptor_2 ac_header_desc = {
+	.bLength		= UAC_DT_AC_HEADER_SIZE(1),
+	.bDescriptorType	= USB_DT_CS_INTERFACE,
+	.bDescriptorSubtype	= UAC_HEADER,
+	.bcdADC			= cpu_to_le16(0x0100),
+	.wTotalLength		= cpu_to_le16(UAC_DT_AC_HEADER_SIZE(1) +
+					      UAC_DT_INPUT_TERMINAL_SIZE +
+					      UAC_DT_FEATURE_UNIT_SIZE(0)),
+	.bInCollection		= 1,
+	.baInterfaceNr[0]	= 0, /* dynamic */
+};
+
+static struct uac_input_terminal_descriptor input_terminal_desc = {
+	.bLength		= UAC_DT_INPUT_TERMINAL_SIZE,
+	.bDescriptorType	= USB_DT_CS_INTERFACE,
+	.bDescriptorSubtype	= UAC_INPUT_TERMINAL,
+	.bTerminalID		= INPUT_TERMINAL_ID,
+	.wTerminalType		= UAC_INPUT_TERMINAL_MICROPHONE,
+	.bAssocTerminal		= 0,
+	.bNrChannels		= 1, /* TODO make this dynamic */
+	.wChannelConfig		= 0, /* dynamic */
+	.iChannelNames		= 0,
+	.iTerminal		= 0,
+};
+
+DECLARE_UAC_FEATURE_UNIT_DESCRIPTOR(0);
+
+static struct uac_feature_unit_descriptor_0 feature_unit_desc = {
+	.bLength		= UAC_DT_FEATURE_UNIT_SIZE(0),
+	.bDescriptorType	= USB_DT_CS_INTERFACE,
+	.bDescriptorSubtype	= UAC_FEATURE_UNIT,
+	.bUnitID		= FEATURE_UNIT_ID,
+	.bSourceID		= INPUT_TERMINAL_ID,
+	.bControlSize		= 2,
+	.bmaControls[0]		= (UAC_FU_MUTE | UAC_FU_VOLUME),
+	.iFeature		= 0,
+};
+
+static struct uac_output_terminal_descriptor output_terminal_desc = {
+	.bLength		= UAC_DT_OUTPUT_TERMINAL_SIZE,
+	.bDescriptorType	= USB_DT_CS_INTERFACE,
+	.bDescriptorSubtype	= UAC_OUTPUT_TERMINAL,
+	.bTerminalID		= OUTPUT_TERMINAL_ID,
+	.wTerminalType		= UAC_TERMINAL_STREAMING,
+	.bAssocTerminal		= 0,
+	.bSourceID		= FEATURE_UNIT_ID,
+	.iTerminal		= 0,
+};
+
+/* B.4.1  Standard AS Interface Descriptor */
+static struct usb_interface_descriptor as_interface_alt_0_desc = {
+	.bLength		= USB_DT_INTERFACE_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE,
+	.bInterfaceNumber	= 0, /* dynamic */
+	.bAlternateSetting	= 0,
+	.bNumEndpoints		= 0,
+	.bInterfaceClass	= USB_CLASS_AUDIO,
+	.bInterfaceSubClass	= USB_SUBCLASS_AUDIOSTREAMING,
+};
+
+static struct usb_interface_descriptor as_interface_alt_1_desc = {
+	.bLength		= USB_DT_INTERFACE_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE,
+	.bInterfaceNumber	= 0, /* dynamic */
+	.bAlternateSetting	= 1,
+	.bNumEndpoints		= 1,
+	.bInterfaceClass	= USB_CLASS_AUDIO,
+	.bInterfaceSubClass	= USB_SUBCLASS_AUDIOSTREAMING,
+};
+
+/* B.4.2  Class-Specific AS Interface Descriptor */
+static struct uac_as_header_descriptor as_header_desc = {
+	.bLength		= UAC_DT_AS_HEADER_SIZE,
+	.bDescriptorType	= USB_DT_CS_INTERFACE,
+	.bDescriptorSubtype	= UAC_AS_GENERAL,
+	.bTerminalLink		= INPUT_TERMINAL_ID,
+	.bDelay			= 1,
+	.wFormatTag		= UAC_FORMAT_TYPE_I_PCM,
+};
+
+DECLARE_UAC_FORMAT_TYPE_I_DISCRETE_DESC(1);
+
+static struct uac_format_type_i_discrete_descriptor_1 as_type_i_desc = {
+	.bLength		= UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1),
+	.bDescriptorType	= USB_DT_CS_INTERFACE,
+	.bDescriptorSubtype	= UAC_FORMAT_TYPE,
+	.bFormatType		= UAC_FORMAT_TYPE_I,
+	.bNrChannels		= 1, /* TODO make this dynamic */
+	.bSubframeSize		= 2,
+	.bBitResolution		= 16,
+	.bSamFreqType		= 1,
+	.tSamFreq[0]		= { 0x00, 0x00, 0x00 }, /* dynamic */
+};
+
+/* Standard ISO IN Endpoint Descriptor */
+static struct usb_endpoint_descriptor as_in_ep_desc = {
+	.bLength		= USB_DT_ENDPOINT_AUDIO_SIZE,
+	.bDescriptorType	= USB_DT_ENDPOINT,
+	.bEndpointAddress	= USB_DIR_IN,
+	.bmAttributes		= USB_ENDPOINT_SYNC_SYNC
+				| USB_ENDPOINT_XFER_ISOC,
+	.wMaxPacketSize		= cpu_to_le16(IN_EP_MAX_PACKET_SIZE),
+	.bInterval		= 4,
+};
+
+/* Class-specific AS ISO IN Endpoint Descriptor */
+static struct uac_iso_endpoint_descriptor as_iso_in_desc __initdata = {
+	.bLength		= UAC_ISO_ENDPOINT_DESC_SIZE,
+	.bDescriptorType	= USB_DT_CS_ENDPOINT,
+	.bDescriptorSubtype	= UAC_EP_GENERAL,
+	.bmAttributes		= 1,
+	.bLockDelayUnits	= 1,
+	.wLockDelay		= cpu_to_le16(1),
+};
+
+static struct usb_descriptor_header *f_audio_desc[] __initdata = {
+	(struct usb_descriptor_header *)&uac_iad,
+
+	(struct usb_descriptor_header *)&ac_interface_desc,
+	(struct usb_descriptor_header *)&ac_header_desc,
+
+	(struct usb_descriptor_header *)&input_terminal_desc,
+	(struct usb_descriptor_header *)&output_terminal_desc,
+	(struct usb_descriptor_header *)&feature_unit_desc,
+
+	(struct usb_descriptor_header *)&as_interface_alt_0_desc,
+	(struct usb_descriptor_header *)&as_interface_alt_1_desc,
+	(struct usb_descriptor_header *)&as_header_desc,
+
+	(struct usb_descriptor_header *)&as_type_i_desc,
+
+	(struct usb_descriptor_header *)&as_in_ep_desc,
+	(struct usb_descriptor_header *)&as_iso_in_desc,
+	NULL,
+};
+
+/* --------------------------------------------------------------------------
+ * Control requests
+ */
+
+static inline struct uac_device *func_to_audio(struct usb_function *f)
+{
+	return container_of(f, struct uac_device, func);
+}
+
+static struct usb_audio_control mute_control = {
+	.list = LIST_HEAD_INIT(mute_control.list),
+	.name = "Mute Control",
+	.type = UAC_MUTE_CONTROL,
+	/* Todo: add real Mute control code */
+	.set = generic_set_cmd,
+	.get = generic_get_cmd,
+};
+
+static struct usb_audio_control volume_control = {
+	.list = LIST_HEAD_INIT(volume_control.list),
+	.name = "Volume Control",
+	.type = UAC_VOLUME_CONTROL,
+	/* Todo: add real Volume control code */
+	.set = generic_set_cmd,
+	.get = generic_get_cmd,
+};
+
+static struct usb_audio_unit feature_unit = {
+	.list = LIST_HEAD_INIT(feature_unit.list),
+	.id = FEATURE_UNIT_ID,
+	.name = "Mute & Volume Control",
+	.type = UAC_FEATURE_UNIT,
+	.desc = (struct usb_descriptor_header *)&feature_unit_desc,
+};
+
+static void
+uac_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
+{
+	struct uac_device *uac = req->context;
+	struct usb_composite_dev *cdev = uac->func.config->cdev;
+	int value = le16_to_cpup((__le16 *)req->buf);
+
+	if (req->status != 0 || uac->set_con == NULL)
+		return;
+
+	DBG(cdev, "setting control %s to %d\n", uac->set_con->name, value);
+
+	uac->set_con->set(uac->set_con, uac->set_cmd, value);
+	uac->set_con = NULL;
+}
+
+static int
+uac_function_set_endpoint_req(struct uac_device *uac,
+		const struct usb_ctrlrequest *ctrl)
+{
+	struct usb_composite_dev *cdev = uac->func.config->cdev;
+	u8 ep = le16_to_cpu(ctrl->wIndex) & 0xff;
+	u8 cs = le16_to_cpu(ctrl->wValue) >> 8;
+	int value = -EOPNOTSUPP;
+
+	if (cs != UAC_EP_CS_ATTR_SAMPLE_RATE ||
+	    ep != as_in_ep_desc.bEndpointAddress)
+		return -EOPNOTSUPP;
+
+	switch (ctrl->bRequest) {
+	case UAC_SET_CUR:
+		DBG(cdev, "setting sampling frequency\n");
+		value = 3;
+		break;
+
+	case UAC_SET_MIN:
+	case UAC_SET_MAX:
+	case UAC_SET_RES:
+	case UAC_SET_MEM:
+	default:
+		break;
+	}
+
+	return value;
+}
+
+static int
+uac_function_get_endpoint_req(struct uac_device *uac,
+		const struct usb_ctrlrequest *ctrl)
+{
+	struct usb_composite_dev *cdev = uac->func.config->cdev;
+	u8 ep = le16_to_cpu(ctrl->wIndex) & 0xff;
+	u8 cs = le16_to_cpu(ctrl->wValue) >> 8;
+	int value = -EOPNOTSUPP;
+
+	if (cs != UAC_EP_CS_ATTR_SAMPLE_RATE ||
+	    ep != as_in_ep_desc.bEndpointAddress)
+		return -EOPNOTSUPP;
+
+	switch (ctrl->bRequest) {
+	case UAC_GET_CUR:
+	case UAC_GET_MIN:
+	case UAC_GET_MAX:
+	case UAC_GET_RES:
+		DBG(cdev, "getting sampling frequency\n");
+		memcpy(uac->control_buf, as_type_i_desc.tSamFreq[0], 3);
+		value = 3;
+		break;
+
+	case UAC_GET_MEM:
+	default:
+		break;
+	}
+
+	return value;
+}
+
+static struct usb_audio_control *
+uac_function_find_control(struct uac_device *uac, int unit, int cs)
+{
+	struct usb_audio_unit *entity;
+	struct usb_audio_control *ctrl;
+
+	list_for_each_entry(entity, &uac->units, list) {
+		if (entity->id != unit)
+			continue;
+
+		list_for_each_entry(ctrl, &entity->control, list) {
+			if (ctrl->type == cs)
+				return ctrl;
+		}
+	}
+
+	return NULL;
+}
+
+static int
+uac_function_set_intf_req(struct uac_device *uac,
+		const struct usb_ctrlrequest *creq)
+{
+	struct usb_composite_dev *cdev = uac->func.config->cdev;
+	u16 len = le16_to_cpu(creq->wLength);
+	u8 unit = le16_to_cpu(creq->wIndex) >> 8;
+	u8 cs = le16_to_cpu(creq->wValue) >> 8;
+	struct usb_audio_control *ctrl;
+
+	ctrl = uac_function_find_control(uac, unit, cs);
+	if (ctrl == NULL)
+		return -EOPNOTSUPP;
+
+	uac->set_con = ctrl;
+	uac->set_cmd = creq->bRequest & 0x0f;
+
+	DBG(cdev, "setting control %s value\n", ctrl->name);
+	return le16_to_cpu(len);
+}
+
+static int
+uac_function_get_intf_req(struct uac_device *uac,
+		const struct usb_ctrlrequest *creq)
+{
+	struct usb_composite_dev *cdev = uac->func.config->cdev;
+	u16 len = le16_to_cpu(creq->wLength);
+	u8 unit = le16_to_cpu(creq->wIndex) >> 8;
+	u8 cs = le16_to_cpu(creq->wValue) >> 8;
+	struct usb_audio_control *ctrl;
+	int value;
+
+	ctrl = uac_function_find_control(uac, unit, cs);
+	if (ctrl == NULL)
+		return -EOPNOTSUPP;
+
+	value = ctrl->get(ctrl, creq->bRequest & 0x0f);
+	memcpy(uac->control_buf, &value, len);
+
+	DBG(cdev, "getting control %s value (%d)\n", ctrl->name, value);
+	return len;
+}
+
+static int
+uac_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
+{
+	struct uac_device *uac = func_to_audio(f);
+	struct usb_composite_dev *cdev = f->config->cdev;
+	struct usb_request *req = uac->control_req;
+	int value = -EOPNOTSUPP;
+	u16 wIndex = le16_to_cpu(ctrl->wIndex);
+	u16 wValue = le16_to_cpu(ctrl->wValue);
+	u16 wLength = le16_to_cpu(ctrl->wLength);
+
+	/* Composite driver infrastructure handles everything; interface
+	 * activation uses set_alt().
+	 */
+	switch (ctrl->bRequestType) {
+	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
+		value = uac_function_set_intf_req(uac, ctrl);
+		break;
+
+	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE:
+		value = uac_function_get_intf_req(uac, ctrl);
+		break;
+
+	case USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
+		value = uac_function_set_endpoint_req(uac, ctrl);
+		break;
+
+	case USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT:
+		value = uac_function_get_endpoint_req(uac, ctrl);
+		break;
+
+	default:
+		ERROR(cdev, "invalid setup request %02x %02x value %04x index "
+			"%04x %04x\n", ctrl->bRequestType, ctrl->bRequest,
+			wValue, wIndex, wLength);
+		break;
+	}
+
+	if (value >= 0) {
+		req->zero = 0;
+		req->length = value;
+		value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
+	}
+
+	return value;
+}
+
+static int
+uac_function_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
+{
+	struct uac_device *uac = func_to_audio(f);
+
+	DBG(f->config->cdev, "intf %d, alt %d\n", intf, alt);
+
+	if (intf == uac->control_intf) {
+		if (alt)
+			return -EINVAL;
+
+		/* TODO Notify userspace that the device has been connected. */
+		return 0;
+	}
+
+	if (intf != uac->streaming_intf)
+		return -EINVAL;
+
+	switch (alt) {
+	case 0:
+		uac_audio_enable(uac, 0);
+		usb_ep_disable(uac->audio_ep);
+
+		/* TODO Notify userspace that the audio stream should be stopped. */
+		break;
+
+	case 1:
+		usb_ep_enable(uac->audio_ep, &as_in_ep_desc);
+		uac_audio_enable(uac, 1);
+
+		/* TODO Notify userspace that the audio stream should be started. */
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static void
+uac_function_disable(struct usb_function *f)
+{
+	// struct uac_device *uac = func_to_audio(f);
+
+	DBG(f->config->cdev, "uac_function_disable\n");
+
+	/* TODO Notify userspace that the device has been disconnected. */
+	return;
+}
+
+/* --------------------------------------------------------------------------
+ * USB probe and disconnect
+ */
+
+static int generic_set_cmd(struct usb_audio_control *con, u8 cmd, int value)
+{
+	con->data[cmd] = value;
+
+	return 0;
+}
+
+static int generic_get_cmd(struct usb_audio_control *con, u8 cmd)
+{
+	return con->data[cmd];
+}
+
+/* Todo: add more control selectors dynamically */
+static int __init
+control_selector_init(struct uac_device *uac)
+{
+	INIT_LIST_HEAD(&uac->units);
+	list_add(&feature_unit.list, &uac->units);
+
+	INIT_LIST_HEAD(&feature_unit.control);
+	list_add(&mute_control.list, &feature_unit.control);
+	list_add(&volume_control.list, &feature_unit.control);
+
+	volume_control.data[UAC_SET_CUR] = 0xffc0;
+	volume_control.data[UAC_SET_MIN] = 0xe3a0;
+	volume_control.data[UAC_SET_MAX] = 0xfff0;
+	volume_control.data[UAC_SET_RES] = 0x0030;
+
+	return 0;
+}
+
+static void
+uac_function_unbind(struct usb_configuration *c, struct usb_function *f)
+{
+	struct usb_composite_dev *cdev = f->config->cdev;
+	struct uac_device *uac = func_to_audio(f);
+
+	uac_audio_cleanup(uac);
+
+	if (uac->audio_ep)
+		uac->audio_ep->driver_data = NULL;
+
+	if (uac->control_req) {
+		usb_ep_free_request(cdev->gadget->ep0, uac->control_req);
+		kfree(uac->control_buf);
+	}
+
+	usb_free_descriptors(f->descriptors);
+	usb_free_descriptors(f->hs_descriptors);
+
+	kfree(uac);
+}
+
+/* audio function driver setup/binding */
+static int __init
+uac_function_bind(struct usb_configuration *c, struct usb_function *f)
+{
+	struct usb_composite_dev *cdev = c->cdev;
+	struct uac_device	*uac = func_to_audio(f);
+	int			ret;
+	struct usb_ep		*ep;
+
+	/* Allocate endpoints. */
+	ep = usb_ep_autoconfig(cdev->gadget, &as_in_ep_desc);
+	if (!ep) {
+		ret = -ENODEV;
+		goto error;
+	}
+
+	uac->audio_ep = ep;
+	ep->driver_data = uac;
+
+	/* Allocate interface IDs. */
+	if ((ret = usb_interface_id(c, f)) < 0)
+		goto error;
+	ac_interface_desc.bInterfaceNumber = ret;
+	uac->control_intf = ret;
+	uac_iad.bFirstInterface = ret;
+
+	if ((ret = usb_interface_id(c, f)) < 0)
+		goto error;
+	as_interface_alt_0_desc.bInterfaceNumber = ret;
+	as_interface_alt_1_desc.bInterfaceNumber = ret;
+	ac_header_desc.baInterfaceNr[0] = ret;
+	uac->streaming_intf = ret;
+
+	/* Copy descriptors. Support all relevant hardware speeds. We expect
+	 * that when hardware is dual speed, all isochronous-capable endpoints
+	 * work at both speeds.
+	 */
+	as_type_i_desc.tSamFreq[0][0] = (uac->rate >> 0) & 0xff;
+	as_type_i_desc.tSamFreq[0][1] = (uac->rate >> 8) & 0xff;
+	as_type_i_desc.tSamFreq[0][2] = (uac->rate >> 16) & 0xff;
+
+	f->descriptors = usb_copy_descriptors(f_audio_desc);
+	if (gadget_is_dualspeed(c->cdev->gadget))
+		f->hs_descriptors = usb_copy_descriptors(f_audio_desc);
+
+	/* Preallocate control endpoint request. */
+	uac->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
+	uac->control_buf = kmalloc(UAC_MAX_REQUEST_SIZE, GFP_KERNEL);
+	if (uac->control_req == NULL || uac->control_buf == NULL) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	uac->control_req->buf = uac->control_buf;
+	uac->control_req->complete = uac_function_ep0_complete;
+	uac->control_req->context = uac;
+
+	/* Set up ALSA audio devices. */
+	ret = uac_audio_init(uac);
+	if (ret < 0)
+		return ret;
+
+	control_selector_init(uac);
+
+	return 0;
+
+error:
+	uac_function_unbind(c, f);
+	return ret;
+}
+
+/*-------------------------------------------------------------------------*/
+
+/**
+ * audio_bind_config - add USB audio fucntion to a configuration
+ * @c: the configuration to supcard the USB audio function
+ * Context: single threaded during gadget setup
+ *
+ * Returns zero on success, else negative errno.
+ */
+int __init
+audio_bind_config(struct usb_configuration *c, unsigned int rate)
+{
+	struct uac_device *uac;
+	int ret;
+
+	/* Allocate and initialize one new instance. */
+	uac = kzalloc(sizeof *uac, GFP_KERNEL);
+	if (uac == NULL)
+		return -ENOMEM;
+
+	uac->rate = rate;
+	uac->func.name = "g_audio";
+
+	/* Register the function. */
+	uac->func.strings = NULL;
+	uac->func.bind = uac_function_bind;
+	uac->func.unbind = uac_function_unbind;
+	uac->func.set_alt = uac_function_set_alt;
+	uac->func.setup = uac_function_setup;
+	uac->func.disable = uac_function_disable;
+
+	ret = usb_add_function(c, &uac->func);
+	if (ret) {
+		kfree(uac);
+		return ret;
+	}
+
+	INFO(c->cdev, "audio_buf_size %d, req_buf_size %d, req_count %d\n",
+		audio_buf_size, req_buf_size, req_count);
+
+	return 0;
+}
drivers/usb/gadget/uac.h new file +99 -0

99 lines not on this page · raw message

drivers/usb/gadget/uac_alsa.c new file +348 -0

348 lines not on this page · raw message

drivers/usb/gadget/uac_audio.c new file +238 -0

238 lines not on this page · raw message

Signature · 4 lines
1.6.3.3

-- 
Laurent Pinchart
#2 lore raw

Re: [PATCH 1/3] USB gadget: audio class function driver

drivers/usb/gadget/uac_alsa.c line 121

Quoted 7 lines · Laurent Pinchart
drivers/usb/gadget/uac_alsa.c new file +6 -0
+snd_uac_pcm_open(struct snd_pcm_substream *substream, int stream)
...
+	substream->runtime->hw = stream == SNDRV_PCM_STREAM_PLAYBACK
+			       ? snd_uac_playback_hw
+			       : snd_uac_capture_hw;
+	substream->runtime->hw.rate_min = uac->rate;
+	substream->runtime->hw.rate_max = uac->rate;

The .rates bit mask is supposed to be consistent with the min/max values; you can use the snd_pcm_rate_to_rate_bit() helper function for this.

drivers/usb/gadget/uac_alsa.c line 210

Quoted 5 lines
drivers/usb/gadget/uac_alsa.c new file +4 -0
+snd_uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
...
+		spin_lock_irqsave(&subs->lock, flags);
+		subs->streaming = 1;
+		spin_unlock_irqrestore(&subs->lock, flags);

The trigger callback is guaranteed to be called with interrupts disabled; you can use spin_lock/spin_unlock here.

drivers/usb/gadget/uac_alsa.c line 299

Quoted 5 lines
drivers/usb/gadget/uac_alsa.c new file +3 -0
+int __init uac_audio_init(struct uac_device *uac)
...
+	static int dev = 0;
...
+	ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,

Usually, drivers use index/id module parameters for these parameters. But if you don't (which is possible), you don't need to count up the dev variable.

drivers/usb/gadget/uac_audio.c line 38

Quoted 9 lines
drivers/usb/gadget/uac_audio.c new file +8 -0
+uac_audio_encode(struct snd_uac_substream *subs, struct usb_request *req)
...
+	if (!subs->streaming) {
+		spin_unlock_irqrestore(&subs->lock, flags);
+		req->length = 0;
+		return;
+	}
+
+	/* TODO Handle buffer underruns. */

The ALSA framework handles buffer underruns by stopping the stream. AFAICS this will result in the gadget returning empty packets.

It is also possible for the application (not the driver) to configure the ALSA PCM device to continue streaming, so that the device plays either the old contents of the buffer or silence.

The driver doesn't actually get much of an opportunity to handle this.

Quoted 7 lines
drivers/usb/gadget/uac_audio.c new file +6 -0
+uac_audio_complete(struct usb_ep *ep, struct usb_request *req)
...
+	switch (req->status) {
+	case 0:
+		break;
+
+	case -ECONNRESET:

drivers/usb/gadget/uac_audio.c line 93

Quoted 10 lines
drivers/usb/gadget/uac_audio.c new file +10 -0
+	case -ESHUTDOWN:
+		goto requeue;
+
+	default:
+		INFO(uac->func.config->cdev, "AS request completed with "
+			"status %d.\n", req->status);
+		goto requeue;
+	}
+
+	uac_audio_encode(subs, req);

Shouldn't the device continue to send packets even if an isochronous transfer failed?

drivers/usb/gadget/uac_audio.c line 184

Quoted 5 lines
drivers/usb/gadget/uac_audio.c new file +4 -0
+uac_audio_pump(struct uac_device *uac)
...
+	/* FIXME TODO Race between uac_audio_pump and requests completion
+	 * handler ???
+	 */

Indeed. But I guess uac_audio_pump() is called when starting a stream when you don't yet have any completions?

The USB audio host driver copies samples from the ALSA buffer to the request buffers only in the request completion handler; streaming gets started with a bunch of silence packets. This also has the consequence that data gets taken out of the buffer at a constant rate.

Best regards,
Clemens

#3 lore raw

PATCH 2/3 USB gadget: video class function driver

In reply to Laurent Pinchart

This USB video class function driver implements a video capture device from the host's point of view. It creates a V4L2 output device on the gadget's side to transfer data from a userspace application over USB.

The UVC-specific descriptors are passed by the gadget driver to the UVC function driver, making them completely configurable without any modification to the function's driver code.

---
 drivers/usb/gadget/f_uvc.c      |  673 +++++++++++++++++++++++++++++
 drivers/usb/gadget/f_uvc.h      |  376 +++++++++++++++++
 drivers/usb/gadget/uvc.h        |  299 +++++++++++++
 drivers/usb/gadget/uvc_gadget.c |  890 +++++++++++++++++++++++++++++++++++++++
 drivers/usb/gadget/uvc_queue.c  |  583 +++++++++++++++++++++++++
 drivers/usb/gadget/uvc_queue.h  |   90 ++++
 drivers/usb/gadget/uvc_v4l2.c   |  376 +++++++++++++++++
 drivers/usb/gadget/uvc_video.c  |  386 +++++++++++++++++
 8 files changed, 3673 insertions(+), 0 deletions(-)
 create mode 100644 drivers/usb/gadget/f_uvc.c
 create mode 100644 drivers/usb/gadget/f_uvc.h
 create mode 100644 drivers/usb/gadget/uvc.h
 create mode 100644 drivers/usb/gadget/uvc_gadget.c
 create mode 100644 drivers/usb/gadget/uvc_queue.c
 create mode 100644 drivers/usb/gadget/uvc_queue.h
 create mode 100644 drivers/usb/gadget/uvc_v4l2.c
 create mode 100644 drivers/usb/gadget/uvc_video.c
drivers/usb/gadget/f_uvc.c new file +674 -0
new file mode 100644
index 0000000..f323960
@@ -0,0 +1,674 @@
+/*
+ *	uvc_gadget.c  --  USB Video Class Gadget driver
+ *
+ *	Copyright (C) 2009
+ *	    Laurent Pinchart ([email protected])
+ *
+ *	This program is free software; you can redistribute it and/or modify
+ *	it under the terms of the GNU General Public License as published by
+ *	the Free Software Foundation; either version 2 of the License, or
+ *	(at your option) any later version.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/device.h>
+#include <linux/errno.h>
+#include <linux/fs.h>
+#include <linux/list.h>
+#include <linux/mutex.h>
+#include <linux/version.h>
+#include <linux/usb/ch9.h>
+#include <linux/usb/gadget.h>
+#include <linux/usb/video.h>
+#include <linux/vmalloc.h>
+#include <linux/wait.h>
+
+#include <media/v4l2-dev.h>
+
+#include "uvc.h"
+
+unsigned int uvc_trace_param;
+
+/* --------------------------------------------------------------------------
+ * Function descriptors
+ */
+
+/* string IDs are assigned dynamically */
+
+#define UVC_STRING_ASSOCIATION_IDX		0
+#define UVC_STRING_CONTROL_IDX			1
+#define UVC_STRING_STREAMING_IDX		2
+
+static struct usb_string uvc_en_us_strings[] = {
+	[UVC_STRING_ASSOCIATION_IDX].s = "UVC Camera",
+	[UVC_STRING_CONTROL_IDX].s = "Video Control",
+	[UVC_STRING_STREAMING_IDX].s = "Video Streaming",
+	{  }
+};
+
+static struct usb_gadget_strings uvc_stringtab = {
+	.language = 0x0409,	/* en-us */
+	.strings = uvc_en_us_strings,
+};
+
+static struct usb_gadget_strings *uvc_function_strings[] = {
+	&uvc_stringtab,
+	NULL,
+};
+
+#define UVC_INTF_VIDEO_CONTROL			0
+#define UVC_INTF_VIDEO_STREAMING		1
+
+static struct usb_interface_assoc_descriptor uvc_iad __initdata = {
+	.bLength		= USB_DT_INTERFACE_ASSOCIATION_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE_ASSOCIATION,
+	.bFirstInterface	= 0,
+	.bInterfaceCount	= 2,
+	.bFunctionClass		= USB_CLASS_VIDEO,
+	.bFunctionSubClass	= 0x03,
+	.bFunctionProtocol	= 0x00,
+	.iFunction		= 0,
+};
+
+static struct usb_interface_descriptor uvc_control_intf __initdata = {
+	.bLength		= USB_DT_INTERFACE_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE,
+	.bInterfaceNumber	= UVC_INTF_VIDEO_CONTROL,
+	.bAlternateSetting	= 0,
+	.bNumEndpoints		= 1,
+	.bInterfaceClass	= USB_CLASS_VIDEO,
+	.bInterfaceSubClass	= 0x01,
+	.bInterfaceProtocol	= 0x00,
+	.iInterface		= 0,
+};
+
+static struct usb_endpoint_descriptor uvc_control_ep __initdata = {
+	.bLength		= USB_DT_ENDPOINT_SIZE,
+	.bDescriptorType	= USB_DT_ENDPOINT,
+	.bEndpointAddress	= USB_DIR_IN,
+	.bmAttributes		= USB_ENDPOINT_XFER_INT,
+	.wMaxPacketSize		= cpu_to_le16(16),
+	.bInterval		= 8,
+};
+
+static struct uvc_control_endpoint_descriptor uvc_control_cs_ep __initdata = {
+	.bLength		= UVC_DT_CONTROL_ENDPOINT_SIZE,
+	.bDescriptorType	= USB_DT_CS_ENDPOINT,
+	.bDescriptorSubType	= UVC_EP_INTERRUPT,
+	.wMaxTransferSize	= cpu_to_le16(16),
+};
+
+static struct usb_interface_descriptor uvc_streaming_intf_alt0 __initdata = {
+	.bLength		= USB_DT_INTERFACE_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE,
+	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
+	.bAlternateSetting	= 0,
+	.bNumEndpoints		= 0,
+	.bInterfaceClass	= USB_CLASS_VIDEO,
+	.bInterfaceSubClass	= 0x02,
+	.bInterfaceProtocol	= 0x00,
+	.iInterface		= 0,
+};
+
+static struct usb_interface_descriptor uvc_streaming_intf_alt1 __initdata = {
+	.bLength		= USB_DT_INTERFACE_SIZE,
+	.bDescriptorType	= USB_DT_INTERFACE,
+	.bInterfaceNumber	= UVC_INTF_VIDEO_STREAMING,
+	.bAlternateSetting	= 1,
+	.bNumEndpoints		= 1,
+	.bInterfaceClass	= USB_CLASS_VIDEO,
+	.bInterfaceSubClass	= 0x02,
+	.bInterfaceProtocol	= 0x00,
+	.iInterface		= 0,
+};
+
+static struct usb_endpoint_descriptor uvc_streaming_ep = {
+	.bLength		= USB_DT_ENDPOINT_SIZE,
+	.bDescriptorType	= USB_DT_ENDPOINT,
+	.bEndpointAddress	= USB_DIR_IN,
+	.bmAttributes		= USB_ENDPOINT_XFER_ISOC,
+	.wMaxPacketSize		= cpu_to_le16(1024),
+	.bInterval		= 1,
+};
+
+static const struct usb_descriptor_header * const uvc_fs_streaming[] = {
+	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
+	(struct usb_descriptor_header *) &uvc_streaming_ep,
+	NULL,
+};
+
+static const struct usb_descriptor_header * const uvc_hs_streaming[] = {
+	(struct usb_descriptor_header *) &uvc_streaming_intf_alt1,
+	(struct usb_descriptor_header *) &uvc_streaming_ep,
+	NULL,
+};
+
+/* --------------------------------------------------------------------------
+ * Control requests
+ */
+
+static void
+uvc_function_ep0_complete(struct usb_ep *ep, struct usb_request *req)
+{
+	struct uvc_device *uvc = req->context;
+	struct uvc_event event;
+
+	if (uvc->event_setup_out) {
+		uvc->event_setup_out = 0;
+
+		memset(&event, 0, sizeof(event));
+		event.type = UVC_EVENT_DATA;
+		event.data.length = req->actual;
+		memcpy(&event.data.data, req->buf, req->actual);
+		kfifo_put(uvc->events, (unsigned char*)&event, sizeof(event));
+
+		wake_up(&uvc->event_wait);
+	}
+}
+
+static int
+uvc_function_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
+{
+	struct uvc_device *uvc = to_uvc(f);
+	struct uvc_event event;
+
+	/* printk(KERN_INFO "setup request %02x %02x value %04x index %04x %04x\n",
+	 *	ctrl->bRequestType, ctrl->bRequest, le16_to_cpu(ctrl->wValue),
+	 *	le16_to_cpu(ctrl->wIndex), le16_to_cpu(ctrl->wLength));
+	 */
+
+	if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_CLASS) {
+		INFO(f->config->cdev, "invalid request type\n");
+		return -EINVAL;
+	}
+
+	/* Stall too big requests. */
+	if (le16_to_cpu(ctrl->wLength) > UVC_MAX_REQUEST_SIZE)
+		return -EINVAL;
+
+	memset(&event, 0, sizeof(event));
+	event.type = UVC_EVENT_SETUP;
+	memcpy(&event.req, ctrl, sizeof(event.req));
+	kfifo_put(uvc->events, (unsigned char*)&event, sizeof(event));
+
+	wake_up(&uvc->event_wait);
+
+	return 0;
+}
+
+static int
+uvc_function_set_alt(struct usb_function *f, unsigned interface, unsigned alt)
+{
+	struct uvc_device *uvc = to_uvc(f);
+	struct uvc_event event;
+
+	INFO(f->config->cdev, "uvc_function_set_alt(%u, %u)\n", interface, alt);
+
+	if (interface == uvc->control_intf) {
+		if (alt)
+			return -EINVAL;
+
+		if (uvc->state == UVC_STATE_DISCONNECTED) {
+			memset(&event, 0, sizeof(event));
+			event.type = UVC_EVENT_CONNECT;
+			event.speed = f->config->cdev->gadget->speed;
+			kfifo_put(uvc->events, (unsigned char*)&event,
+				  sizeof(event));
+
+			wake_up(&uvc->event_wait);
+
+			uvc->state = UVC_STATE_CONNECTED;
+		}
+
+		return 0;
+	}
+
+	if (interface != uvc->streaming_intf)
+		return -EINVAL;
+
+	/* TODO
+	if (usb_endpoint_xfer_bulk(&uvc->desc.vs_ep))
+		return alt ? -EINVAL : 0;
+	*/
+
+	switch (alt) {
+	case 0:
+		if (uvc->state != UVC_STATE_STREAMING)
+			return 0;
+
+		if (uvc->video.ep)
+			usb_ep_disable(uvc->video.ep);
+
+		memset(&event, 0, sizeof(event));
+		event.type = UVC_EVENT_STREAMOFF;
+		kfifo_put(uvc->events, (unsigned char*)&event, sizeof(event));
+
+		wake_up(&uvc->event_wait);
+
+		uvc->state = UVC_STATE_CONNECTED;
+		break;
+
+	case 1:
+		if (uvc->state != UVC_STATE_CONNECTED)
+			return 0;
+
+		if (uvc->video.ep)
+			usb_ep_enable(uvc->video.ep, &uvc_streaming_ep);
+
+		memset(&event, 0, sizeof(event));
+		event.type = UVC_EVENT_STREAMON;
+		kfifo_put(uvc->events, (unsigned char*)&event, sizeof(event));
+
+		wake_up(&uvc->event_wait);
+
+		uvc->state = UVC_STATE_STREAMING;
+		break;
+
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static void
+uvc_function_disable(struct usb_function *f)
+{
+	struct uvc_device *uvc = to_uvc(f);
+	struct uvc_event event;
+
+	INFO(f->config->cdev, "uvc_function_disable\n");
+
+	memset(&event, 0, sizeof(event));
+	event.type = UVC_EVENT_DISCONNECT;
+	kfifo_put(uvc->events, (unsigned char*)&event, sizeof(event));
+
+	wake_up(&uvc->event_wait);
+
+	uvc->state = UVC_STATE_DISCONNECTED;
+}
+
+/* --------------------------------------------------------------------------
+ * Connection / disconnection
+ */
+
+void
+uvc_function_connect(struct uvc_device *uvc)
+{
+	struct usb_composite_dev *cdev = uvc->func.config->cdev;
+	int ret;
+
+	if ((ret = usb_function_activate(&uvc->func)) < 0)
+		INFO(cdev, "UVC connect failed with %d\n", ret);
+}
+
+void
+uvc_function_disconnect(struct uvc_device *uvc)
+{
+	struct usb_composite_dev *cdev = uvc->func.config->cdev;
+	int ret;
+
+	if ((ret = usb_function_deactivate(&uvc->func)) < 0)
+		INFO(cdev, "UVC disconnect failed with %d\n", ret);
+}
+
+/* --------------------------------------------------------------------------
+ * USB probe and disconnect
+ */
+
+static int
+uvc_register_video(struct uvc_device *uvc)
+{
+	struct usb_composite_dev *cdev = uvc->func.config->cdev;
+	struct video_device *video;
+
+	/* TODO reference counting. */
+	video = video_device_alloc();
+	if (video == NULL)
+		return -ENOMEM;
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
+	video->dev = &cdev->gadget->dev;
+	video->type = 0;
+	video->type2 = 0;
+#else
+	video->parent = &cdev->gadget->dev;
+#endif
+	video->minor = -1;
+	video->fops = &uvc_v4l2_fops;
+	video->release = video_device_release;
+	strncpy(video->name, cdev->gadget->name, sizeof(video->name));
+
+	uvc->vdev = video;
+	video_set_drvdata(video, uvc);
+
+	return video_register_device(video, VFL_TYPE_GRABBER, -1);
+}
+
+#define UVC_COPY_DESCRIPTOR(mem, dst, desc) \
+	do { \
+		memcpy(mem, desc, (desc)->bLength); \
+		*(dst)++ = mem; \
+		mem += (desc)->bLength; \
+	} while (0);
+
+#define UVC_COPY_DESCRIPTORS(mem, dst, src) \
+	do { \
+		const struct usb_descriptor_header * const *__src; \
+		for (__src = src; *__src; ++__src) { \
+			memcpy(mem, *__src, (*__src)->bLength); \
+			*dst++ = mem; \
+			mem += (*__src)->bLength; \
+		} \
+	} while (0)
+
+static struct usb_descriptor_header ** __init
+uvc_copy_descriptors(struct uvc_device *uvc, enum usb_device_speed speed)
+{
+	struct uvc_input_header_descriptor *uvc_streaming_header;
+	struct uvc_header_descriptor *uvc_control_header;
+	const struct uvc_descriptor_header * const *uvc_streaming_cls;
+	const struct usb_descriptor_header * const *uvc_streaming_std;
+	const struct usb_descriptor_header * const *src;
+	struct usb_descriptor_header **dst;
+	struct usb_descriptor_header **hdr;
+	unsigned int control_size;
+	unsigned int streaming_size;
+	unsigned int n_desc;
+	unsigned int bytes;
+	void *mem;
+
+	uvc_streaming_cls = (speed == USB_SPEED_FULL)
+			  ? uvc->desc.fs_streaming : uvc->desc.hs_streaming;
+	uvc_streaming_std = (speed == USB_SPEED_FULL)
+			  ? uvc_fs_streaming : uvc_hs_streaming;
+
+	/* Descriptors layout
+	 *
+	 * uvc_iad
+	 * uvc_control_intf
+	 * Class-specific UVC control descriptors
+	 * uvc_control_ep
+	 * uvc_control_cs_ep
+	 * uvc_streaming_intf_alt0
+	 * Class-specific UVC streaming descriptors
+	 * uvc_{fs|hs}_streaming
+	 */
+
+	/* Count descriptors and compute their size. */
+	control_size = 0;
+	streaming_size = 0;
+	bytes = uvc_iad.bLength + uvc_control_intf.bLength
+	      + uvc_control_ep.bLength + uvc_control_cs_ep.bLength
+	      + uvc_streaming_intf_alt0.bLength;
+	n_desc = 5;
+
+	for (src = (const struct usb_descriptor_header**)uvc->desc.control; *src; ++src) {
+		control_size += (*src)->bLength;
+		bytes += (*src)->bLength;
+		n_desc++;
+	}
+	for (src = (const struct usb_descriptor_header**)uvc_streaming_cls; *src; ++src) {
+		streaming_size += (*src)->bLength;
+		bytes += (*src)->bLength;
+		n_desc++;
+	}
+	for (src = uvc_streaming_std; *src; ++src) {
+		bytes += (*src)->bLength;
+		n_desc++;
+	}
+
+	mem = kmalloc((n_desc + 1) * sizeof(*src) + bytes, GFP_KERNEL);
+	if (mem == NULL)
+		return NULL;
+
+	hdr = mem;
+	dst = mem;
+	mem += (n_desc + 1) * sizeof(*src);
+
+	/* Copy the descriptors. */
+	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_iad);
+	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_intf);
+
+	uvc_control_header = mem;
+	UVC_COPY_DESCRIPTORS(mem, dst,
+		(const struct usb_descriptor_header**)uvc->desc.control);
+	uvc_control_header->wTotalLength = cpu_to_le16(control_size);
+	uvc_control_header->bInCollection = 1;
+	uvc_control_header->baInterfaceNr[0] = uvc->streaming_intf;
+
+	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_ep);
+	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_control_cs_ep);
+	UVC_COPY_DESCRIPTOR(mem, dst, &uvc_streaming_intf_alt0);
+
+	uvc_streaming_header = mem;
+	UVC_COPY_DESCRIPTORS(mem, dst,
+		(const struct usb_descriptor_header**)uvc_streaming_cls);
+	uvc_streaming_header->wTotalLength = cpu_to_le16(streaming_size);
+	uvc_streaming_header->bEndpointAddress = uvc_streaming_ep.bEndpointAddress;
+
+	UVC_COPY_DESCRIPTORS(mem, dst, uvc_streaming_std);
+
+	*dst = NULL;
+	return hdr;
+}
+
+static void
+uvc_function_unbind(struct usb_configuration *c, struct usb_function *f)
+{
+	struct usb_composite_dev *cdev = c->cdev;
+	struct uvc_device *uvc = to_uvc(f);
+
+	INFO(cdev, "uvc_function_unbind\n");
+
+	if (uvc->vdev) {
+		if (uvc->vdev->minor == -1)
+			video_device_release(uvc->vdev);
+		else
+			video_unregister_device(uvc->vdev);
+		uvc->vdev = NULL;
+	}
+
+	if (uvc->control_ep)
+		uvc->control_ep->driver_data = NULL;
+	if (uvc->video.ep)
+		uvc->video.ep->driver_data = NULL;
+
+	if (uvc->control_req) {
+		usb_ep_free_request(cdev->gadget->ep0, uvc->control_req);
+		kfree(uvc->control_buf);
+	}
+
+	kfree(f->descriptors);
+	kfree(f->hs_descriptors);
+
+	if (uvc->events)
+		kfifo_free(uvc->events);
+
+	kfree(uvc);
+}
+
+static int __init
+uvc_function_bind(struct usb_configuration *c, struct usb_function *f)
+{
+	struct usb_composite_dev *cdev = c->cdev;
+	struct uvc_device *uvc = to_uvc(f);
+	struct usb_ep *ep;
+	int ret = -EINVAL;
+
+	INFO(cdev, "uvc_function_bind\n");
+
+	/* Allocate endpoints. */
+	ep = usb_ep_autoconfig(cdev->gadget, &uvc_control_ep);
+	if (!ep) {
+		INFO(cdev, "Unable to allocate control EP\n");
+		goto error;
+	}
+	uvc->control_ep = ep;
+	ep->driver_data = uvc;
+
+	ep = usb_ep_autoconfig(cdev->gadget, &uvc_streaming_ep);
+	if (!ep) {
+		INFO(cdev, "Unable to allocate streaming EP\n");
+		goto error;
+	}
+	uvc->video.ep = ep;
+	ep->driver_data = uvc;
+
+	/* Allocate interface IDs. */
+	if ((ret = usb_interface_id(c, f)) < 0)
+		goto error;
+	uvc_iad.bFirstInterface = ret;
+	uvc_control_intf.bInterfaceNumber = ret;
+	uvc->control_intf = ret;
+
+	if ((ret = usb_interface_id(c, f)) < 0)
+		goto error;
+	uvc_streaming_intf_alt0.bInterfaceNumber = ret;
+	uvc_streaming_intf_alt1.bInterfaceNumber = ret;
+	uvc->streaming_intf = ret;
+
+	/* Copy descriptors. */
+	f->descriptors = uvc_copy_descriptors(uvc, USB_SPEED_FULL);
+	f->hs_descriptors = uvc_copy_descriptors(uvc, USB_SPEED_HIGH);
+
+	/* Preallocate control endpoint request. */
+	uvc->control_req = usb_ep_alloc_request(cdev->gadget->ep0, GFP_KERNEL);
+	uvc->control_buf = kmalloc(UVC_MAX_REQUEST_SIZE, GFP_KERNEL);
+	if (uvc->control_req == NULL || uvc->control_buf == NULL) {
+		ret = -ENOMEM;
+		goto error;
+	}
+
+	uvc->control_req->buf = uvc->control_buf;
+	uvc->control_req->complete = uvc_function_ep0_complete;
+	uvc->control_req->context = uvc;
+
+	/* Avoid letting this gadget enumerate until the userspace server is
+	 * active.
+	 */
+	if ((ret = usb_function_deactivate(f)) < 0)
+		goto error;
+
+	/* Initialise video. */
+	ret = uvc_video_init(&uvc->video);
+	if (ret < 0)
+		goto error;
+
+	/* Register a V4L2 device. */
+	ret = uvc_register_video(uvc);
+	if (ret < 0) {
+		printk(KERN_INFO "Unable to register video device\n");
+		goto error;
+	}
+
+	return 0;
+
+error:
+	uvc_function_unbind(c, f);
+	return ret;
+}
+
+/* --------------------------------------------------------------------------
+ * USB gadget function
+ */
+
+/**
+ * uvc_bind_config - add a UVC function to a configuration
+ * @c: the configuration to support the UVC instance
+ * Context: single threaded during gadget setup
+ *
+ * Returns zero on success, else negative errno.
+ *
+ * Caller must have called @uvc_setup(). Caller is also responsible for
+ * calling @uvc_cleanup() before module unload.
+ */
+int __init
+uvc_bind_config(struct usb_configuration *c,
+		const struct uvc_descriptor_header * const *control,
+		const struct uvc_descriptor_header * const *fs_streaming,
+		const struct uvc_descriptor_header * const *hs_streaming)
+{
+	struct uvc_device *uvc;
+	int ret = 0;
+
+	/* TODO Check if the USB device controller supports the required
+	 * features.
+	 */
+	if (!gadget_is_dualspeed(c->cdev->gadget))
+		return -EINVAL;
+
+	uvc = kzalloc(sizeof(*uvc), GFP_KERNEL);
+	if (uvc == NULL)
+		return -ENOMEM;
+
+	uvc->state = UVC_STATE_DISCONNECTED;
+
+	/* TODO Optimized structure-based kfifo implementation to avoid
+	 * copies.
+	 */
+	init_waitqueue_head(&uvc->event_wait);
+	spin_lock_init(&uvc->event_lock);
+	uvc->events = kfifo_alloc(sizeof(struct uvc_event) * UVC_MAX_EVENTS,
+				  GFP_KERNEL, &uvc->event_lock);
+	if (uvc->events == NULL)
+		goto error;
+
+	/* Validate the descriptors. */
+	if (control == NULL || control[0] == NULL ||
+	    control[0]->bDescriptorSubType != UVC_DT_HEADER)
+		goto error;
+
+	if (fs_streaming == NULL || fs_streaming[0] == NULL ||
+	    fs_streaming[0]->bDescriptorSubType != UVC_DT_INPUT_HEADER)
+		goto error;
+
+	if (hs_streaming == NULL || hs_streaming[0] == NULL ||
+	    hs_streaming[0]->bDescriptorSubType != UVC_DT_INPUT_HEADER)
+		goto error;
+
+	uvc->desc.control = control;
+	uvc->desc.fs_streaming = fs_streaming;
+	uvc->desc.hs_streaming = hs_streaming;
+
+	/* Allocate string descriptor numbers. */
+	if ((ret = usb_string_id(c->cdev)) < 0)
+		goto error;
+	uvc_en_us_strings[UVC_STRING_ASSOCIATION_IDX].id = ret;
+	uvc_iad.iFunction = ret;
+
+	if ((ret = usb_string_id(c->cdev)) < 0)
+		goto error;
+	uvc_en_us_strings[UVC_STRING_CONTROL_IDX].id = ret;
+	uvc_control_intf.iInterface = ret;
+
+	if ((ret = usb_string_id(c->cdev)) < 0)
+		goto error;
+	uvc_en_us_strings[UVC_STRING_STREAMING_IDX].id = ret;
+	uvc_streaming_intf_alt0.iInterface = ret;
+	uvc_streaming_intf_alt1.iInterface = ret;
+
+	/* Register the function. */
+	uvc->func.name = "uvc";
+	uvc->func.strings = uvc_function_strings;
+	uvc->func.bind = uvc_function_bind;
+	uvc->func.unbind = uvc_function_unbind;
+	uvc->func.set_alt = uvc_function_set_alt;
+	uvc->func.disable = uvc_function_disable;
+	uvc->func.setup = uvc_function_setup;
+
+	ret = usb_add_function(c, &uvc->func);
+	if (ret)
+		kfree(uvc);
+
+	return 0;
+
+error:
+	kfree(uvc);
+	return ret;
+}
+
+module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
+MODULE_PARM_DESC(trace, "Trace level bitmask");
+
drivers/usb/gadget/f_uvc.h new file +376 -0

376 lines not on this page · raw message

drivers/usb/gadget/uvc.h new file +246 -0

246 lines not on this page · raw message

drivers/usb/gadget/uvc_gadget.c new file +890 -0

890 lines not on this page · raw message

drivers/usb/gadget/uvc_queue.c new file +583 -0

583 lines not on this page · raw message

drivers/usb/gadget/uvc_queue.h new file +90 -0

90 lines not on this page · raw message

drivers/usb/gadget/uvc_v4l2.c new file +376 -0

141 lines not on this page · raw message

drivers/usb/gadget/uvc_video.c new file +386 -0

386 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 4 lines
1.6.3.3

-- 
Laurent Pinchart

Part of the diff is not shown here. View the raw message.

#4 lore raw

PATCH 3/3 USB gadget: Webcam Audio/Video device

In reply to Laurent Pinchart

This webcam gadget driver combines a UAC microphone (sampling rate configurable using a module parameter) and a UVC camera (360p and 720p resolutions in YUYV and MJPEG).

---
 drivers/usb/gadget/Kconfig  |    9 +-
 drivers/usb/gadget/Makefile |    2 +
 drivers/usb/gadget/webcam.c |  419 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 429 insertions(+), 1 deletions(-)
 create mode 100644 drivers/usb/gadget/webcam.c
drivers/usb/gadget/Kconfig +8 -1
index 7f8e83a..38b9481 100644
@@ -766,8 +766,15 @@ config USB_CDC_COMPOSITE
 
 # put drivers that need isochronous transfer support (for audio
 # or video class gadget drivers), or specific hardware, here.
+config USB_G_WEBCAM
+	tristate "USB Webcam Gadget"
+	help
+	  The Webcam Gadget acts as a composite USB Audio and Video Class
+	  device. It provides a userspace API to process UVC control requests
+	  and stream video data to the host.
 
-# - none yet
+	  Say "y" to link the driver statically, or "m" to build a
+	  dynamically linked module called "g_webcam".
 
 endchoice
 
drivers/usb/gadget/Makefile +2 -0
index e6017e6..4956992 100644
@@ -40,6 +40,7 @@ gadgetfs-objs			:= inode.o
 g_file_storage-objs		:= file_storage.o
 g_printer-objs			:= printer.o
 g_cdc-objs			:= cdc2.o
+g_webcam-objs			:= webcam.o
 
 obj-$(CONFIG_USB_ZERO)		+= g_zero.o
 obj-$(CONFIG_USB_AUDIO)		+= g_audio.o
@@ -50,4 +51,5 @@ obj-$(CONFIG_USB_G_SERIAL)	+= g_serial.o
 obj-$(CONFIG_USB_G_PRINTER)	+= g_printer.o
 obj-$(CONFIG_USB_MIDI_GADGET)	+= g_midi.o
 obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o
+obj-$(CONFIG_USB_G_WEBCAM)	+= g_webcam.o
 
drivers/usb/gadget/webcam.c new file +420 -0

420 lines not on this page · raw message

Signature · 4 lines
1.6.3.3

-- 
Laurent Pinchart