All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v2 24/26] usb: sandbox: Add a USB emulation driver
Date: Sun,  8 Nov 2015 23:48:06 -0700	[thread overview]
Message-ID: <1447051688-24936-25-git-send-email-sjg@chromium.org> (raw)
In-Reply-To: <1447051688-24936-1-git-send-email-sjg@chromium.org>

Add a simple USB keyboard driver for sandbox. It provides a function to
'load' it with input data, which it will then stream through to the normal
U-Boot input subsystem. When the input data is exhausted, the keyboard stops
providing data.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

Changes in v2: None

 arch/sandbox/include/asm/test.h |   2 +
 drivers/usb/emul/Makefile       |   1 +
 drivers/usb/emul/sandbox_keyb.c | 241 ++++++++++++++++++++++++++++++++++++++++
 include/linux/usb/ch9.h         |  20 ++++
 4 files changed, 264 insertions(+)
 create mode 100644 drivers/usb/emul/sandbox_keyb.c

diff --git a/arch/sandbox/include/asm/test.h b/arch/sandbox/include/asm/test.h
index d3c7851..224b0eb 100644
--- a/arch/sandbox/include/asm/test.h
+++ b/arch/sandbox/include/asm/test.h
@@ -86,4 +86,6 @@ long sandbox_i2c_rtc_set_offset(struct udevice *dev, bool use_system_time,
  */
 long sandbox_i2c_rtc_get_set_base_time(struct udevice *dev, long base_time);
 
+int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str);
+
 #endif
diff --git a/drivers/usb/emul/Makefile b/drivers/usb/emul/Makefile
index 8fd83d5..b64ac6d 100644
--- a/drivers/usb/emul/Makefile
+++ b/drivers/usb/emul/Makefile
@@ -7,4 +7,5 @@
 
 obj-$(CONFIG_USB_EMUL) += sandbox_flash.o
 obj-$(CONFIG_USB_EMUL) += sandbox_hub.o
+obj-$(CONFIG_USB_EMUL) += sandbox_keyb.o
 obj-$(CONFIG_USB_EMUL) += usb-emul-uclass.o
diff --git a/drivers/usb/emul/sandbox_keyb.c b/drivers/usb/emul/sandbox_keyb.c
new file mode 100644
index 0000000..2735985
--- /dev/null
+++ b/drivers/usb/emul/sandbox_keyb.c
@@ -0,0 +1,241 @@
+/*
+ * (C) Copyright 2015 Google, Inc
+ * Written by Simon Glass <sjg@chromium.org>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <os.h>
+#include <scsi.h>
+#include <usb.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/*
+ * This driver emulates a USB keyboard using the USB HID specification (boot
+ * protocol)
+ */
+
+enum {
+	SANDBOX_KEYB_EP_IN		= 1,	/* endpoints */
+};
+
+enum cmd_phase {
+	PHASE_START,
+	PHASE_DATA,
+	PHASE_STATUS,
+};
+
+enum {
+	STRINGID_MANUFACTURER = 1,
+	STRINGID_PRODUCT,
+	STRINGID_SERIAL,
+
+	STRINGID_COUNT,
+};
+
+/**
+ * struct sandbox_keyb_priv - private state for this driver
+ *
+ */
+struct sandbox_keyb_priv {
+	struct membuff in;
+};
+
+struct sandbox_keyb_plat {
+	struct usb_string keyb_strings[STRINGID_COUNT];
+};
+
+static struct usb_device_descriptor keyb_device_desc = {
+	.bLength =		sizeof(keyb_device_desc),
+	.bDescriptorType =	USB_DT_DEVICE,
+
+	.bcdUSB =		__constant_cpu_to_le16(0x0100),
+
+	.bDeviceClass =		0,
+	.bDeviceSubClass =	0,
+	.bDeviceProtocol =	0,
+
+	.idVendor =		__constant_cpu_to_le16(0x1234),
+	.idProduct =		__constant_cpu_to_le16(0x5679),
+	.iManufacturer =	STRINGID_MANUFACTURER,
+	.iProduct =		STRINGID_PRODUCT,
+	.iSerialNumber =	STRINGID_SERIAL,
+	.bNumConfigurations =	1,
+};
+
+static struct usb_config_descriptor keyb_config0 = {
+	.bLength		= sizeof(keyb_config0),
+	.bDescriptorType	= USB_DT_CONFIG,
+
+	/* wTotalLength is set up by usb-emul-uclass */
+	.bNumInterfaces		= 2,
+	.bConfigurationValue	= 0,
+	.iConfiguration		= 0,
+	.bmAttributes		= 1 << 7 | 1 << 5,
+	.bMaxPower		= 50,
+};
+
+static struct usb_interface_descriptor keyb_interface0 = {
+	.bLength		= sizeof(keyb_interface0),
+	.bDescriptorType	= USB_DT_INTERFACE,
+
+	.bInterfaceNumber	= 0,
+	.bAlternateSetting	= 0,
+	.bNumEndpoints		= 1,
+	.bInterfaceClass	= USB_CLASS_HID,
+	.bInterfaceSubClass	= USB_SUB_HID_BOOT,
+	.bInterfaceProtocol	= USB_PROT_HID_KEYBOARD,
+	.iInterface		= 0,
+};
+
+static struct usb_class_hid_descriptor keyb_report0 = {
+	.bLength		= sizeof(keyb_report0),
+	.bDescriptorType	= USB_DT_HID,
+	.bcdCDC			= 0x101,
+	.bCountryCode		= 0,
+	.bNumDescriptors	= 1,
+	.bDescriptorType0	= USB_DT_HID_REPORT,
+	.wDescriptorLength0	= 0x3f,
+};
+
+static struct usb_endpoint_descriptor keyb_endpoint0_in = {
+	.bLength		= USB_DT_ENDPOINT_SIZE,
+	.bDescriptorType	= USB_DT_ENDPOINT,
+
+	.bEndpointAddress	= SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
+	.bmAttributes		= USB_ENDPOINT_XFER_BULK |
+					USB_ENDPOINT_XFER_ISOC,
+	.wMaxPacketSize		= __constant_cpu_to_le16(8),
+	.bInterval		= 0xa,
+};
+
+static struct usb_interface_descriptor keyb_interface1 = {
+	.bLength		= sizeof(keyb_interface1),
+	.bDescriptorType	= USB_DT_INTERFACE,
+
+	.bInterfaceNumber	= 1,
+	.bAlternateSetting	= 0,
+	.bNumEndpoints		= 1,
+	.bInterfaceClass	= USB_CLASS_HID,
+	.bInterfaceSubClass	= USB_SUB_HID_BOOT,
+	.bInterfaceProtocol	= USB_PROT_HID_MOUSE,
+	.iInterface		= 0,
+};
+
+static struct usb_class_hid_descriptor keyb_report1 = {
+	.bLength		= sizeof(struct usb_class_hid_descriptor),
+	.bDescriptorType	= USB_DT_HID,
+	.bcdCDC			= 0x101,
+	.bCountryCode		= 0,
+	.bNumDescriptors	= 1,
+	.bDescriptorType0	= USB_DT_HID_REPORT,
+	.wDescriptorLength0	= 0x32,
+};
+
+static struct usb_endpoint_descriptor keyb_endpoint1_in = {
+	.bLength		= USB_DT_ENDPOINT_SIZE,
+	.bDescriptorType	= USB_DT_ENDPOINT,
+
+	.bEndpointAddress	= SANDBOX_KEYB_EP_IN | USB_ENDPOINT_DIR_MASK,
+	.bmAttributes		= USB_ENDPOINT_XFER_BULK |
+					USB_ENDPOINT_XFER_ISOC,
+	.wMaxPacketSize		= __constant_cpu_to_le16(8),
+	.bInterval		= 0xa,
+};
+
+static void *keyb_desc_list[] = {
+	&keyb_device_desc,
+	&keyb_config0,
+	&keyb_interface0,
+	&keyb_report0,
+	&keyb_endpoint0_in,
+	&keyb_interface1,
+	&keyb_report1,
+	&keyb_endpoint1_in,
+	NULL,
+};
+
+int sandbox_usb_keyb_add_string(struct udevice *dev, const char *str)
+{
+	struct sandbox_keyb_priv *priv = dev_get_priv(dev);
+	int len, ret;
+
+	len = strlen(str);
+	ret = membuff_put(&priv->in, str, len);
+	if (ret != len)
+		return -ENOSPC;
+
+	return 0;
+}
+
+static int sandbox_keyb_control(struct udevice *dev, struct usb_device *udev,
+				unsigned long pipe, void *buff, int len,
+				struct devrequest *setup)
+{
+	debug("pipe=%lx\n", pipe);
+
+	return -EIO;
+}
+
+static int sandbox_keyb_interrupt(struct udevice *dev, struct usb_device *udev,
+		unsigned long pipe, void *buffer, int length, int interval)
+{
+	struct sandbox_keyb_priv *priv = dev_get_priv(dev);
+	uint8_t *data = buffer;
+	int ch;
+
+	memset(data, '\0', length);
+	ch = membuff_getbyte(&priv->in);
+	if (ch != -1)
+		data[2] = 4 + ch - 'a';
+
+	return 0;
+}
+
+static int sandbox_keyb_bind(struct udevice *dev)
+{
+	struct sandbox_keyb_plat *plat = dev_get_platdata(dev);
+	struct usb_string *fs;
+
+	fs = plat->keyb_strings;
+	fs[0].id = STRINGID_MANUFACTURER;
+	fs[0].s = "sandbox";
+	fs[1].id = STRINGID_PRODUCT;
+	fs[1].s = "keyboard";
+	fs[2].id = STRINGID_SERIAL;
+	fs[2].s = dev->name;
+
+	return usb_emul_setup_device(dev, PACKET_SIZE_8, plat->keyb_strings,
+				     keyb_desc_list);
+}
+
+static int sandbox_keyb_probe(struct udevice *dev)
+{
+	struct sandbox_keyb_priv *priv = dev_get_priv(dev);
+
+	return membuff_new(&priv->in, 256);
+}
+
+static const struct dm_usb_ops sandbox_usb_keyb_ops = {
+	.control	= sandbox_keyb_control,
+	.interrupt	= sandbox_keyb_interrupt,
+};
+
+static const struct udevice_id sandbox_usb_keyb_ids[] = {
+	{ .compatible = "sandbox,usb-keyb" },
+	{ }
+};
+
+U_BOOT_DRIVER(usb_sandbox_keyb) = {
+	.name	= "usb_sandbox_keyb",
+	.id	= UCLASS_USB_EMUL,
+	.of_match = sandbox_usb_keyb_ids,
+	.bind	= sandbox_keyb_bind,
+	.probe	= sandbox_keyb_probe,
+	.ops	= &sandbox_usb_keyb_ops,
+	.priv_auto_alloc_size = sizeof(struct sandbox_keyb_priv),
+	.platdata_auto_alloc_size = sizeof(struct sandbox_keyb_plat),
+};
diff --git a/include/linux/usb/ch9.h b/include/linux/usb/ch9.h
index 822fca0..0ad4782 100644
--- a/include/linux/usb/ch9.h
+++ b/include/linux/usb/ch9.h
@@ -229,6 +229,8 @@ struct usb_ctrlrequest {
 #define USB_DT_PIPE_USAGE		0x24
 /* From the USB 3.0 spec */
 #define	USB_DT_SS_ENDPOINT_COMP		0x30
+/* From HID 1.11 spec */
+#define USB_DT_HID_REPORT		0x22
 
 /* Conventional codes for class-specific descriptors.  The convention is
  * defined in the USB "Common Class" Spec (3.11).  Individual class specs
@@ -385,6 +387,24 @@ struct usb_generic_descriptor {
 	__u8  bDescriptorType;
 };
 
+struct __packed usb_class_hid_descriptor {
+	u8 bLength;
+	u8 bDescriptorType;
+	u16 bcdCDC;
+	u8 bCountryCode;
+	u8 bNumDescriptors;	/* 0x01 */
+	u8 bDescriptorType0;
+	u16 wDescriptorLength0;
+	/* optional descriptors are not supported. */
+};
+
+struct __packed usb_class_report_descriptor {
+	u8 bLength;	/* dummy */
+	u8 bDescriptorType;
+	u16 wLength;
+	u8 bData[0];
+};
+
 /*
  * Endpoints
  */
-- 
2.6.0.rc2.230.g3dd15c0

  parent reply	other threads:[~2015-11-09  6:48 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-09  6:47 [U-Boot] [PATCH v2 00/26] usb: Drop requirement for USB unbinding, add tests Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 01/26] sandbox: Add a way to skip time delays Simon Glass
2015-11-20  3:30   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 02/26] dm: usb: Avoid time delays in sandbox tests Simon Glass
2015-11-20  3:30   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 03/26] Move console definitions into a new console.h file Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 04/26] Drop config.h header from display_options.c Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 05/26] Add a circular memory buffer implementation Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 06/26] console: Add a console buffer Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 07/26] sandbox: Enable console recording and silent console Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 08/26] test: Record and silence console in tests Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 09/26] usb: Refactor USB tree output code for testing Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 10/26] dm: core: Add safe device iteration macros Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 11/26] sandbox: usb: Allow dynamic emulated USB device descriptors Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 12/26] sandbox: usb: Allow up to 4 emulated devices on a hub Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 13/26] sandbox: usb: Allow finding a USB emulator for a device Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 14/26] Revert "dm: usb: Rename usb_find_child to usb_find_emul_child" Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 15/26] Revert "dm: usb: Use device_unbind_children to clean up usb devs on stop" Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 16/26] Revert "dm: Export device_remove_children / device_unbind_children" Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:47 ` [U-Boot] [PATCH v2 17/26] dm: usb: Deprecate usb_get_dev_index() Simon Glass
2015-11-20  3:31   ` Simon Glass
2015-11-09  6:48 ` [U-Boot] [PATCH v2 18/26] dm: usb: Remove inactive children after a bus scan Simon Glass
2015-11-09  8:22   ` Hans de Goede
2015-11-09 20:25     ` Simon Glass
2015-11-10 23:30       ` Simon Glass
2015-11-11 17:02         ` Hans de Goede
2015-11-11 18:15           ` Simon Glass
2015-11-12 14:08             ` Hans de Goede
2015-11-13 21:58               ` Simon Glass
2015-11-15 19:35                 ` Hans de Goede
2015-11-16 21:08                   ` Simon Glass
2015-11-20  3:32                     ` Simon Glass
2015-11-11 17:03         ` Hans de Goede
2015-11-09  6:48 ` [U-Boot] [PATCH v2 19/26] dm: test: usb: Add tests for the 'usb tree' command Simon Glass
2015-11-20  3:32   ` Simon Glass
2015-11-09  6:48 ` [U-Boot] [PATCH v2 20/26] dm: test: usb: Add a test for device reordering Simon Glass
2015-11-20  3:32   ` Simon Glass
2015-11-09  6:48 ` [U-Boot] [PATCH v2 21/26] usb: Drop unused code in usb_kbd.c Simon Glass
2015-11-20  3:32   ` Simon Glass
2015-11-09  6:48 ` [U-Boot] [PATCH v2 22/26] usb: Avoid open-coded USB constants " Simon Glass
2015-11-20  3:32   ` Simon Glass
2015-11-09  6:48 ` [U-Boot] [PATCH v2 23/26] usb: sandbox: Add support for interrupt operations Simon Glass
2015-11-20  3:32   ` Simon Glass
2015-11-09  6:48 ` Simon Glass [this message]
2015-11-20  3:32   ` [U-Boot] [PATCH v2 24/26] usb: sandbox: Add a USB emulation driver Simon Glass
2015-11-09  6:48 ` [U-Boot] [PATCH v2 25/26] sandbox: Enable USB keyboard Simon Glass
2015-11-20  3:32   ` Simon Glass
2015-11-09  6:48 ` [U-Boot] [PATCH v2 26/26] dm: test: usb: sandbox: Add keyboard tests for sandbox Simon Glass
2015-11-20  3:32   ` Simon Glass
2015-11-09  8:14 ` [U-Boot] [PATCH v2 00/26] usb: Drop requirement for USB unbinding, add tests Hans de Goede
2015-11-09 16:54   ` Simon Glass
2015-11-18 15:53     ` Simon Glass
2015-11-09 14:17 ` Marek Vasut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1447051688-24936-25-git-send-email-sjg@chromium.org \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.