From 71adf118946957839a13aa4d1094183e05c6c094 Mon Sep 17 00:00:00 2001 From: Fabien Chouteau Date: Thu, 8 Apr 2010 09:31:15 +0200 Subject: USB: gadget: add HID gadget driver g_hid is a USB gadget driver implementing the Human Interface Device class specification. The driver handles basic HID protocol handling in the kernel, and allows userspace to read/write HID reports trough /dev/hidgX character devices. Signed-off-by: Fabien Chouteau Signed-off-by: Peter Korsgaard Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/Kconfig | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'drivers/usb/gadget/Kconfig') diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 11a3e0fa4331..390c13372bdc 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -863,6 +863,17 @@ config USB_G_MULTI_CDC If unsure, say "y". +config USB_G_HID + tristate "HID Gadget" + help + The HID gadget driver provides generic emulation of USB + Human Interface Devices (HID). + + For more information, see Documentation/usb/gadget_hid.txt which + includes sample code for accessing the device files. + + Say "y" to link the driver statically, or "m" to build a + dynamically linked module called "g_hid". # put drivers that need isochronous transfer support (for audio # or video class gadget drivers), or specific hardware, here. -- cgit v1.2.3 From a9914127e834acf648a96c72b4e271dc0c1c7c74 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 2 May 2010 20:57:42 +0200 Subject: USB gadget: Webcam device This webcam gadget instantiates a UVC camera (360p and 720p resolutions in YUYV and MJPEG). Signed-off-by: Laurent Pinchart Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/Kconfig | 9 +- drivers/usb/gadget/Makefile | 2 + drivers/usb/gadget/webcam.c | 399 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 409 insertions(+), 1 deletion(-) create mode 100644 drivers/usb/gadget/webcam.c (limited to 'drivers/usb/gadget/Kconfig') diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 390c13372bdc..10bfb4197599 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -877,8 +877,15 @@ config USB_G_HID # 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 diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 3075ff9cae62..03a27d921c26 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile @@ -45,6 +45,7 @@ g_cdc-objs := cdc2.o g_multi-objs := multi.o g_hid-objs := hid.o g_nokia-objs := nokia.o +g_webcam-objs := webcam.o obj-$(CONFIG_USB_ZERO) += g_zero.o obj-$(CONFIG_USB_AUDIO) += g_audio.o @@ -59,4 +60,5 @@ obj-$(CONFIG_USB_CDC_COMPOSITE) += g_cdc.o obj-$(CONFIG_USB_G_HID) += g_hid.o obj-$(CONFIG_USB_G_MULTI) += g_multi.o obj-$(CONFIG_USB_G_NOKIA) += g_nokia.o +obj-$(CONFIG_USB_G_WEBCAM) += g_webcam.o diff --git a/drivers/usb/gadget/webcam.c b/drivers/usb/gadget/webcam.c new file mode 100644 index 000000000000..417fd6887698 --- /dev/null +++ b/drivers/usb/gadget/webcam.c @@ -0,0 +1,399 @@ +/* + * webcam.c -- USB webcam gadget driver + * + * Copyright (C) 2009-2010 + * Laurent Pinchart (laurent.pinchart@ideasonboard.com) + * + * 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 +#include +#include + +#include "f_uvc.h" + +/* + * Kbuild is not very cooperative with respect to linking separately + * compiled library objects into one module. So for now we won't use + * separate compilation ... ensuring init/exit sections work to shrink + * the runtime footprint, and giving us at least some parts of what + * a "gcc --combine ... part1.c part2.c part3.c ... " build would. + */ +#include "composite.c" +#include "usbstring.c" +#include "config.c" +#include "epautoconf.c" + +#include "f_uvc.c" +#include "uvc_queue.c" +#include "uvc_v4l2.c" +#include "uvc_video.c" + +/* -------------------------------------------------------------------------- + * Device descriptor + */ + +#define WEBCAM_VENDOR_ID 0x1d6b /* Linux Foundation */ +#define WEBCAM_PRODUCT_ID 0x0102 /* Webcam A/V gadget */ +#define WEBCAM_DEVICE_BCD 0x0010 /* 0.10 */ + +static char webcam_vendor_label[] = "Linux Foundation"; +static char webcam_product_label[] = "Webcam gadget"; +static char webcam_config_label[] = "Video"; + +/* string IDs are assigned dynamically */ + +#define STRING_MANUFACTURER_IDX 0 +#define STRING_PRODUCT_IDX 1 +#define STRING_DESCRIPTION_IDX 2 + +static struct usb_string webcam_strings[] = { + [STRING_MANUFACTURER_IDX].s = webcam_vendor_label, + [STRING_PRODUCT_IDX].s = webcam_product_label, + [STRING_DESCRIPTION_IDX].s = webcam_config_label, + { } +}; + +static struct usb_gadget_strings webcam_stringtab = { + .language = 0x0409, /* en-us */ + .strings = webcam_strings, +}; + +static struct usb_gadget_strings *webcam_device_strings[] = { + &webcam_stringtab, + NULL, +}; + +static struct usb_device_descriptor webcam_device_descriptor = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = cpu_to_le16(0x0200), + .bDeviceClass = USB_CLASS_MISC, + .bDeviceSubClass = 0x02, + .bDeviceProtocol = 0x01, + .bMaxPacketSize0 = 0, /* dynamic */ + .idVendor = cpu_to_le16(WEBCAM_VENDOR_ID), + .idProduct = cpu_to_le16(WEBCAM_PRODUCT_ID), + .bcdDevice = cpu_to_le16(WEBCAM_DEVICE_BCD), + .iManufacturer = 0, /* dynamic */ + .iProduct = 0, /* dynamic */ + .iSerialNumber = 0, /* dynamic */ + .bNumConfigurations = 0, /* dynamic */ +}; + +DECLARE_UVC_HEADER_DESCRIPTOR(1); + +static const struct UVC_HEADER_DESCRIPTOR(1) uvc_control_header = { + .bLength = UVC_DT_HEADER_SIZE(1), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_HEADER, + .bcdUVC = cpu_to_le16(0x0100), + .wTotalLength = 0, /* dynamic */ + .dwClockFrequency = cpu_to_le32(48000000), + .bInCollection = 0, /* dynamic */ + .baInterfaceNr[0] = 0, /* dynamic */ +}; + +static const struct uvc_camera_terminal_descriptor uvc_camera_terminal = { + .bLength = UVC_DT_CAMERA_TERMINAL_SIZE(3), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_INPUT_TERMINAL, + .bTerminalID = 1, + .wTerminalType = cpu_to_le16(0x0201), + .bAssocTerminal = 0, + .iTerminal = 0, + .wObjectiveFocalLengthMin = cpu_to_le16(0), + .wObjectiveFocalLengthMax = cpu_to_le16(0), + .wOcularFocalLength = cpu_to_le16(0), + .bControlSize = 3, + .bmControls[0] = 2, + .bmControls[1] = 0, + .bmControls[2] = 0, +}; + +static const struct uvc_processing_unit_descriptor uvc_processing = { + .bLength = UVC_DT_PROCESSING_UNIT_SIZE(2), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_PROCESSING_UNIT, + .bUnitID = 2, + .bSourceID = 1, + .wMaxMultiplier = cpu_to_le16(16*1024), + .bControlSize = 2, + .bmControls[0] = 1, + .bmControls[1] = 0, + .iProcessing = 0, +}; + +static const struct uvc_output_terminal_descriptor uvc_output_terminal = { + .bLength = UVC_DT_OUTPUT_TERMINAL_SIZE, + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_OUTPUT_TERMINAL, + .bTerminalID = 3, + .wTerminalType = cpu_to_le16(0x0101), + .bAssocTerminal = 0, + .bSourceID = 2, + .iTerminal = 0, +}; + +DECLARE_UVC_INPUT_HEADER_DESCRIPTOR(1, 2); + +static const struct UVC_INPUT_HEADER_DESCRIPTOR(1, 2) uvc_input_header = { + .bLength = UVC_DT_INPUT_HEADER_SIZE(1, 2), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_INPUT_HEADER, + .bNumFormats = 2, + .wTotalLength = 0, /* dynamic */ + .bEndpointAddress = 0, /* dynamic */ + .bmInfo = 0, + .bTerminalLink = 3, + .bStillCaptureMethod = 0, + .bTriggerSupport = 0, + .bTriggerUsage = 0, + .bControlSize = 1, + .bmaControls[0][0] = 0, + .bmaControls[1][0] = 4, +}; + +static const struct uvc_format_uncompressed uvc_format_yuv = { + .bLength = UVC_DT_FORMAT_UNCOMPRESSED_SIZE, + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_FORMAT_UNCOMPRESSED, + .bFormatIndex = 1, + .bNumFrameDescriptors = 2, + .guidFormat = + { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}, + .bBitsPerPixel = 16, + .bDefaultFrameIndex = 1, + .bAspectRatioX = 0, + .bAspectRatioY = 0, + .bmInterfaceFlags = 0, + .bCopyProtect = 0, +}; + +DECLARE_UVC_FRAME_UNCOMPRESSED(1); +DECLARE_UVC_FRAME_UNCOMPRESSED(3); + +static const struct UVC_FRAME_UNCOMPRESSED(3) uvc_frame_yuv_360p = { + .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(3), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_FRAME_UNCOMPRESSED, + .bFrameIndex = 1, + .bmCapabilities = 0, + .wWidth = cpu_to_le16(640), + .wHeight = cpu_to_le16(360), + .dwMinBitRate = cpu_to_le32(18432000), + .dwMaxBitRate = cpu_to_le32(55296000), + .dwMaxVideoFrameBufferSize = cpu_to_le32(460800), + .dwDefaultFrameInterval = cpu_to_le32(666666), + .bFrameIntervalType = 3, + .dwFrameInterval[0] = cpu_to_le32(666666), + .dwFrameInterval[1] = cpu_to_le32(1000000), + .dwFrameInterval[2] = cpu_to_le32(5000000), +}; + +static const struct UVC_FRAME_UNCOMPRESSED(1) uvc_frame_yuv_720p = { + .bLength = UVC_DT_FRAME_UNCOMPRESSED_SIZE(1), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_FRAME_UNCOMPRESSED, + .bFrameIndex = 2, + .bmCapabilities = 0, + .wWidth = cpu_to_le16(1280), + .wHeight = cpu_to_le16(720), + .dwMinBitRate = cpu_to_le32(29491200), + .dwMaxBitRate = cpu_to_le32(29491200), + .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200), + .dwDefaultFrameInterval = cpu_to_le32(5000000), + .bFrameIntervalType = 1, + .dwFrameInterval[0] = cpu_to_le32(5000000), +}; + +static const struct uvc_format_mjpeg uvc_format_mjpg = { + .bLength = UVC_DT_FORMAT_MJPEG_SIZE, + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_FORMAT_MJPEG, + .bFormatIndex = 2, + .bNumFrameDescriptors = 2, + .bmFlags = 0, + .bDefaultFrameIndex = 1, + .bAspectRatioX = 0, + .bAspectRatioY = 0, + .bmInterfaceFlags = 0, + .bCopyProtect = 0, +}; + +DECLARE_UVC_FRAME_MJPEG(1); +DECLARE_UVC_FRAME_MJPEG(3); + +static const struct UVC_FRAME_MJPEG(3) uvc_frame_mjpg_360p = { + .bLength = UVC_DT_FRAME_MJPEG_SIZE(3), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_FRAME_MJPEG, + .bFrameIndex = 1, + .bmCapabilities = 0, + .wWidth = cpu_to_le16(640), + .wHeight = cpu_to_le16(360), + .dwMinBitRate = cpu_to_le32(18432000), + .dwMaxBitRate = cpu_to_le32(55296000), + .dwMaxVideoFrameBufferSize = cpu_to_le32(460800), + .dwDefaultFrameInterval = cpu_to_le32(666666), + .bFrameIntervalType = 3, + .dwFrameInterval[0] = cpu_to_le32(666666), + .dwFrameInterval[1] = cpu_to_le32(1000000), + .dwFrameInterval[2] = cpu_to_le32(5000000), +}; + +static const struct UVC_FRAME_MJPEG(1) uvc_frame_mjpg_720p = { + .bLength = UVC_DT_FRAME_MJPEG_SIZE(1), + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_FRAME_MJPEG, + .bFrameIndex = 2, + .bmCapabilities = 0, + .wWidth = cpu_to_le16(1280), + .wHeight = cpu_to_le16(720), + .dwMinBitRate = cpu_to_le32(29491200), + .dwMaxBitRate = cpu_to_le32(29491200), + .dwMaxVideoFrameBufferSize = cpu_to_le32(1843200), + .dwDefaultFrameInterval = cpu_to_le32(5000000), + .bFrameIntervalType = 1, + .dwFrameInterval[0] = cpu_to_le32(5000000), +}; + +static const struct uvc_color_matching_descriptor uvc_color_matching = { + .bLength = UVC_DT_COLOR_MATCHING_SIZE, + .bDescriptorType = USB_DT_CS_INTERFACE, + .bDescriptorSubType = UVC_DT_COLOR_MATCHING, + .bColorPrimaries = 1, + .bTransferCharacteristics = 1, + .bMatrixCoefficients = 4, +}; + +static const struct uvc_descriptor_header * const uvc_control_cls[] = { + (const struct uvc_descriptor_header *) &uvc_control_header, + (const struct uvc_descriptor_header *) &uvc_camera_terminal, + (const struct uvc_descriptor_header *) &uvc_processing, + (const struct uvc_descriptor_header *) &uvc_output_terminal, + NULL, +}; + +static const struct uvc_descriptor_header * const uvc_fs_streaming_cls[] = { + (const struct uvc_descriptor_header *) &uvc_input_header, + (const struct uvc_descriptor_header *) &uvc_format_yuv, + (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p, + (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p, + (const struct uvc_descriptor_header *) &uvc_format_mjpg, + (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p, + (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p, + (const struct uvc_descriptor_header *) &uvc_color_matching, + NULL, +}; + +static const struct uvc_descriptor_header * const uvc_hs_streaming_cls[] = { + (const struct uvc_descriptor_header *) &uvc_input_header, + (const struct uvc_descriptor_header *) &uvc_format_yuv, + (const struct uvc_descriptor_header *) &uvc_frame_yuv_360p, + (const struct uvc_descriptor_header *) &uvc_frame_yuv_720p, + (const struct uvc_descriptor_header *) &uvc_format_mjpg, + (const struct uvc_descriptor_header *) &uvc_frame_mjpg_360p, + (const struct uvc_descriptor_header *) &uvc_frame_mjpg_720p, + (const struct uvc_descriptor_header *) &uvc_color_matching, + NULL, +}; + +/* -------------------------------------------------------------------------- + * USB configuration + */ + +static int __init +webcam_config_bind(struct usb_configuration *c) +{ + return uvc_bind_config(c, uvc_control_cls, uvc_fs_streaming_cls, + uvc_hs_streaming_cls); +} + +static struct usb_configuration webcam_config_driver = { + .label = webcam_config_label, + .bind = webcam_config_bind, + .bConfigurationValue = 1, + .iConfiguration = 0, /* dynamic */ + .bmAttributes = USB_CONFIG_ATT_SELFPOWER, + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2, +}; + +static int /* __init_or_exit */ +webcam_unbind(struct usb_composite_dev *cdev) +{ + return 0; +} + +static int __init +webcam_bind(struct usb_composite_dev *cdev) +{ + int ret; + + /* Allocate string descriptor numbers ... note that string contents + * can be overridden by the composite_dev glue. + */ + if ((ret = usb_string_id(cdev)) < 0) + goto error; + webcam_strings[STRING_MANUFACTURER_IDX].id = ret; + webcam_device_descriptor.iManufacturer = ret; + + if ((ret = usb_string_id(cdev)) < 0) + goto error; + webcam_strings[STRING_PRODUCT_IDX].id = ret; + webcam_device_descriptor.iProduct = ret; + + if ((ret = usb_string_id(cdev)) < 0) + goto error; + webcam_strings[STRING_DESCRIPTION_IDX].id = ret; + webcam_config_driver.iConfiguration = ret; + + /* Register our configuration. */ + if ((ret = usb_add_config(cdev, &webcam_config_driver)) < 0) + goto error; + + INFO(cdev, "Webcam Video Gadget\n"); + return 0; + +error: + webcam_unbind(cdev); + return ret; +} + +/* -------------------------------------------------------------------------- + * Driver + */ + +static struct usb_composite_driver webcam_driver = { + .name = "g_webcam", + .dev = &webcam_device_descriptor, + .strings = webcam_device_strings, + .bind = webcam_bind, + .unbind = webcam_unbind, +}; + +static int __init +webcam_init(void) +{ + return usb_composite_register(&webcam_driver); +} + +static void __exit +webcam_cleanup(void) +{ + usb_composite_unregister(&webcam_driver); +} + +module_init(webcam_init); +module_exit(webcam_cleanup); + +MODULE_AUTHOR("Laurent Pinchart"); +MODULE_DESCRIPTION("Webcam Video Gadget"); +MODULE_LICENSE("GPL"); +MODULE_VERSION("0.1.0"); + -- cgit v1.2.3 From 24337c133ff92ba8d7c42819db17f7f2b0de3129 Mon Sep 17 00:00:00 2001 From: Randy Dunlap Date: Wed, 5 May 2010 15:46:26 -0700 Subject: USB: gadget webcam: depends on VIDEO_DEV g_webcam uses v4l[2] interfaces, so it should depend on VIDEO_DEV. ERROR: "v4l2_event_unsubscribe" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_event_queue" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "video_device_release" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "video_usercopy" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_event_dequeue" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "video_register_device" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "video_device_alloc" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_event_subscribe" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "video_unregister_device" [drivers/usb/gadget/g_webcam.ko] ndefined! ERROR: "v4l2_event_pending" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_fh_init" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_event_init" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "video_devdata" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_event_alloc" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_fh_add" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_fh_del" [drivers/usb/gadget/g_webcam.ko] undefined! ERROR: "v4l2_fh_exit" [drivers/usb/gadget/g_webcam.ko] undefined! Signed-off-by: Randy Dunlap Acked-by: Laurent Pinchart Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/Kconfig | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers/usb/gadget/Kconfig') diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 10bfb4197599..96e1494723ad 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -879,6 +879,7 @@ config USB_G_HID # or video class gadget drivers), or specific hardware, here. config USB_G_WEBCAM tristate "USB Webcam Gadget" + depends on VIDEO_DEV help The Webcam Gadget acts as a composite USB Audio and Video Class device. It provides a userspace API to process UVC control requests -- cgit v1.2.3 From c6c560085172c16a0141ab12ae765c7d1be68279 Mon Sep 17 00:00:00 2001 From: Michal Nazarewicz Date: Wed, 5 May 2010 12:53:15 +0200 Subject: USB: g_ffs: the FunctionFS gadget driver The Function Filesystem (FunctioFS) lets one create USB composite functions in user space in the same way as GadgetFS lets one create USB gadgets in user space. This allows creation of composite gadgets such that some of the functions are implemented in kernel space (for instance Ethernet, serial or mass storage) and other are implemented in user space. Signed-off-by: Michal Nazarewicz Cc: Kyungmin Park Cc: Marek Szyprowski Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/Kconfig | 37 ++++ drivers/usb/gadget/Makefile | 2 + drivers/usb/gadget/g_ffs.c | 426 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 465 insertions(+) create mode 100644 drivers/usb/gadget/g_ffs.c (limited to 'drivers/usb/gadget/Kconfig') diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 96e1494723ad..0282c50d6404 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -710,6 +710,43 @@ config USB_GADGETFS Say "y" to link the driver statically, or "m" to build a dynamically linked module called "gadgetfs". +config USB_FUNCTIONFS + tristate "Function Filesystem (EXPERIMENTAL)" + depends on EXPERIMENTAL + help + The Function Filesystem (FunctioFS) lets one create USB + composite functions in user space in the same way as GadgetFS + lets one create USB gadgets in user space. This allows creation + of composite gadgets such that some of the functions are + implemented in kernel space (for instance Ethernet, serial or + mass storage) and other are implemented in user space. + + Say "y" to link the driver statically, or "m" to build + a dynamically linked module called "g_ffs". + +config USB_FUNCTIONFS_ETH + bool "Include CDC ECM (Ethernet) function" + depends on USB_FUNCTIONFS + help + Include an CDC ECM (Ethernet) funcion in the CDC ECM (Funcion) + Filesystem. If you also say "y" to the RNDIS query below the + gadget will have two configurations. + +config USB_FUNCTIONFS_RNDIS + bool "Include RNDIS (Ethernet) function" + depends on USB_FUNCTIONFS + help + Include an RNDIS (Ethernet) funcion in the Funcion Filesystem. + If you also say "y" to the CDC ECM query above the gadget will + have two configurations. + +config USB_FUNCTIONFS_GENERIC + bool "Include 'pure' configuration" + depends on USB_FUNCTIONFS && (USB_FUNCTIONFS_ETH || USB_FUNCTIONFS_RNDIS) + help + Include a configuration with FunctionFS and no Ethernet + configuration. + config USB_FILE_STORAGE tristate "File-backed Storage Gadget" depends on BLOCK diff --git a/drivers/usb/gadget/Makefile b/drivers/usb/gadget/Makefile index 03a27d921c26..a55050c17893 100644 --- a/drivers/usb/gadget/Makefile +++ b/drivers/usb/gadget/Makefile @@ -51,6 +51,8 @@ obj-$(CONFIG_USB_ZERO) += g_zero.o obj-$(CONFIG_USB_AUDIO) += g_audio.o obj-$(CONFIG_USB_ETH) += g_ether.o obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o +obj-$(CONFIG_USB_FUNCTIONFS) += g_ffs.o +obj-$(CONFIG_USB_ETH_FUNCTIONFS) += g_eth_ffs.o obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o obj-$(CONFIG_USB_MASS_STORAGE) += g_mass_storage.o obj-$(CONFIG_USB_G_SERIAL) += g_serial.o diff --git a/drivers/usb/gadget/g_ffs.c b/drivers/usb/gadget/g_ffs.c new file mode 100644 index 000000000000..4b0e4a040d6f --- /dev/null +++ b/drivers/usb/gadget/g_ffs.c @@ -0,0 +1,426 @@ +#include +#include + + +/* + * kbuild is not very cooperative with respect to linking separately + * compiled library objects into one module. So for now we won't use + * separate compilation ... ensuring init/exit sections work to shrink + * the runtime footprint, and giving us at least some parts of what + * a "gcc --combine ... part1.c part2.c part3.c ... " build would. + */ + +#include "composite.c" +#include "usbstring.c" +#include "config.c" +#include "epautoconf.c" + +#if defined CONFIG_USB_FUNCTIONFS_ETH || defined CONFIG_USB_FUNCTIONFS_RNDIS +# if defined USB_ETH_RNDIS +# undef USB_ETH_RNDIS +# endif +# ifdef CONFIG_USB_FUNCTIONFS_RNDIS +# define USB_ETH_RNDIS y +# endif + +# include "f_ecm.c" +# include "f_subset.c" +# ifdef USB_ETH_RNDIS +# include "f_rndis.c" +# include "rndis.c" +# endif +# include "u_ether.c" + +static u8 gfs_hostaddr[ETH_ALEN]; +#else +# if !defined CONFIG_USB_FUNCTIONFS_GENERIC +# define CONFIG_USB_FUNCTIONFS_GENERIC +# endif +# define gether_cleanup() do { } while (0) +# define gether_setup(gadget, hostaddr) ((int)0) +#endif + +#include "f_fs.c" + + +#define DRIVER_NAME "g_ffs" +#define DRIVER_DESC "USB Function Filesystem" +#define DRIVER_VERSION "24 Aug 2004" + +MODULE_DESCRIPTION(DRIVER_DESC); +MODULE_AUTHOR("Michal Nazarewicz"); +MODULE_LICENSE("GPL"); + + +static unsigned short gfs_vendor_id = 0x0525; /* XXX NetChip */ +static unsigned short gfs_product_id = 0xa4ac; /* XXX */ + +static struct usb_device_descriptor gfs_dev_desc = { + .bLength = sizeof gfs_dev_desc, + .bDescriptorType = USB_DT_DEVICE, + + .bcdUSB = cpu_to_le16(0x0200), + .bDeviceClass = USB_CLASS_PER_INTERFACE, + + /* Vendor and product id can be overridden by module parameters. */ + /* .idVendor = cpu_to_le16(gfs_vendor_id), */ + /* .idProduct = cpu_to_le16(gfs_product_id), */ + /* .bcdDevice = f(hardware) */ + /* .iManufacturer = DYNAMIC */ + /* .iProduct = DYNAMIC */ + /* NO SERIAL NUMBER */ + .bNumConfigurations = 1, +}; + +#define GFS_MODULE_PARAM_DESC(name, field) \ + MODULE_PARM_DESC(name, "Value of the " #field " field of the device descriptor sent to the host. Takes effect only prior to the user-space driver registering to the FunctionFS.") + +module_param_named(usb_class, gfs_dev_desc.bDeviceClass, byte, 0644); +GFS_MODULE_PARAM_DESC(usb_class, bDeviceClass); +module_param_named(usb_subclass, gfs_dev_desc.bDeviceSubClass, byte, 0644); +GFS_MODULE_PARAM_DESC(usb_subclass, bDeviceSubClass); +module_param_named(usb_protocol, gfs_dev_desc.bDeviceProtocol, byte, 0644); +GFS_MODULE_PARAM_DESC(usb_protocol, bDeviceProtocol); +module_param_named(usb_vendor, gfs_vendor_id, ushort, 0644); +GFS_MODULE_PARAM_DESC(usb_vendor, idVendor); +module_param_named(usb_product, gfs_product_id, ushort, 0644); +GFS_MODULE_PARAM_DESC(usb_product, idProduct); + + + +static const struct usb_descriptor_header *gfs_otg_desc[] = { + (const struct usb_descriptor_header *) + &(const struct usb_otg_descriptor) { + .bLength = sizeof(struct usb_otg_descriptor), + .bDescriptorType = USB_DT_OTG, + + /* REVISIT SRP-only hardware is possible, although + * it would not be called "OTG" ... */ + .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, + }, + + NULL +}; + +/* string IDs are assigned dynamically */ + +enum { + GFS_STRING_MANUFACTURER_IDX, + GFS_STRING_PRODUCT_IDX, +#ifdef CONFIG_USB_FUNCTIONFS_RNDIS + GFS_STRING_RNDIS_CONFIG_IDX, +#endif +#ifdef CONFIG_USB_FUNCTIONFS_ETH + GFS_STRING_ECM_CONFIG_IDX, +#endif +#ifdef CONFIG_USB_FUNCTIONFS_GENERIC + GFS_STRING_GENERIC_CONFIG_IDX, +#endif +}; + +static char gfs_manufacturer[50]; +static const char gfs_driver_desc[] = DRIVER_DESC; +static const char gfs_short_name[] = DRIVER_NAME; + +static struct usb_string gfs_strings[] = { + [GFS_STRING_MANUFACTURER_IDX].s = gfs_manufacturer, + [GFS_STRING_PRODUCT_IDX].s = gfs_driver_desc, +#ifdef CONFIG_USB_FUNCTIONFS_RNDIS + [GFS_STRING_RNDIS_CONFIG_IDX].s = "FunctionFS + RNDIS", +#endif +#ifdef CONFIG_USB_FUNCTIONFS_ETH + [GFS_STRING_ECM_CONFIG_IDX].s = "FunctionFS + ECM", +#endif +#ifdef CONFIG_USB_FUNCTIONFS_GENERIC + [GFS_STRING_GENERIC_CONFIG_IDX].s = "FunctionFS", +#endif + { } /* end of list */ +}; + +static struct usb_gadget_strings *gfs_dev_strings[] = { + &(struct usb_gadget_strings) { + .language = 0x0409, /* en-us */ + .strings = gfs_strings, + }, + NULL, +}; + + +#ifdef CONFIG_USB_FUNCTIONFS_RNDIS +static int gfs_do_rndis_config(struct usb_configuration *c); + +static struct usb_configuration gfs_rndis_config_driver = { + .label = "FunctionFS + RNDIS", + .bind = gfs_do_rndis_config, + .bConfigurationValue = 1, + /* .iConfiguration = DYNAMIC */ + .bmAttributes = USB_CONFIG_ATT_SELFPOWER, +}; +# define gfs_add_rndis_config(cdev) \ + usb_add_config(cdev, &gfs_rndis_config_driver) +#else +# define gfs_add_rndis_config(cdev) 0 +#endif + + +#ifdef CONFIG_USB_FUNCTIONFS_ETH +static int gfs_do_ecm_config(struct usb_configuration *c); + +static struct usb_configuration gfs_ecm_config_driver = { + .label = "FunctionFS + ECM", + .bind = gfs_do_ecm_config, + .bConfigurationValue = 1, + /* .iConfiguration = DYNAMIC */ + .bmAttributes = USB_CONFIG_ATT_SELFPOWER, +}; +# define gfs_add_ecm_config(cdev) \ + usb_add_config(cdev, &gfs_ecm_config_driver) +#else +# define gfs_add_ecm_config(cdev) 0 +#endif + + +#ifdef CONFIG_USB_FUNCTIONFS_GENERIC +static int gfs_do_generic_config(struct usb_configuration *c); + +static struct usb_configuration gfs_generic_config_driver = { + .label = "FunctionFS", + .bind = gfs_do_generic_config, + .bConfigurationValue = 2, + /* .iConfiguration = DYNAMIC */ + .bmAttributes = USB_CONFIG_ATT_SELFPOWER, +}; +# define gfs_add_generic_config(cdev) \ + usb_add_config(cdev, &gfs_generic_config_driver) +#else +# define gfs_add_generic_config(cdev) 0 +#endif + + +static int gfs_bind(struct usb_composite_dev *cdev); +static int gfs_unbind(struct usb_composite_dev *cdev); + +static struct usb_composite_driver gfs_driver = { + .name = gfs_short_name, + .dev = &gfs_dev_desc, + .strings = gfs_dev_strings, + .bind = gfs_bind, + .unbind = gfs_unbind, +}; + + +static struct ffs_data *gfs_ffs_data; +static unsigned long gfs_registered; + + +static int gfs_init(void) +{ + ENTER(); + + return functionfs_init(); +} +module_init(gfs_init); + +static void gfs_exit(void) +{ + ENTER(); + + if (test_and_clear_bit(0, &gfs_registered)) + usb_composite_unregister(&gfs_driver); + + functionfs_cleanup(); +} +module_exit(gfs_exit); + + +static int functionfs_ready_callback(struct ffs_data *ffs) +{ + int ret; + + ENTER(); + + if (WARN_ON(test_and_set_bit(0, &gfs_registered))) + return -EBUSY; + + gfs_ffs_data = ffs; + ret = usb_composite_register(&gfs_driver); + if (unlikely(ret < 0)) + clear_bit(0, &gfs_registered); + return ret; +} + +static void functionfs_closed_callback(struct ffs_data *ffs) +{ + ENTER(); + + if (test_and_clear_bit(0, &gfs_registered)) + usb_composite_unregister(&gfs_driver); +} + + +static int functionfs_check_dev_callback(const char *dev_name) +{ + return 0; +} + + + +static int gfs_bind(struct usb_composite_dev *cdev) +{ + int ret; + + ENTER(); + + if (WARN_ON(!gfs_ffs_data)) + return -ENODEV; + + ret = gether_setup(cdev->gadget, gfs_hostaddr); + if (unlikely(ret < 0)) + goto error_quick; + + gfs_dev_desc.idVendor = cpu_to_le16(gfs_vendor_id); + gfs_dev_desc.idProduct = cpu_to_le16(gfs_product_id); + + snprintf(gfs_manufacturer, sizeof gfs_manufacturer, "%s %s with %s", + init_utsname()->sysname, init_utsname()->release, + cdev->gadget->name); + ret = usb_string_id(cdev); + if (unlikely(ret < 0)) + goto error; + gfs_strings[GFS_STRING_MANUFACTURER_IDX].id = ret; + gfs_dev_desc.iManufacturer = ret; + + ret = usb_string_id(cdev); + if (unlikely(ret < 0)) + goto error; + gfs_strings[GFS_STRING_PRODUCT_IDX].id = ret; + gfs_dev_desc.iProduct = ret; + +#ifdef CONFIG_USB_FUNCTIONFS_RNDIS + ret = usb_string_id(cdev); + if (unlikely(ret < 0)) + goto error; + gfs_strings[GFS_STRING_RNDIS_CONFIG_IDX].id = ret; + gfs_rndis_config_driver.iConfiguration = ret; +#endif + +#ifdef CONFIG_USB_FUNCTIONFS_ETH + ret = usb_string_id(cdev); + if (unlikely(ret < 0)) + goto error; + gfs_strings[GFS_STRING_ECM_CONFIG_IDX].id = ret; + gfs_ecm_config_driver.iConfiguration = ret; +#endif + +#ifdef CONFIG_USB_FUNCTIONFS_GENERIC + ret = usb_string_id(cdev); + if (unlikely(ret < 0)) + goto error; + gfs_strings[GFS_STRING_GENERIC_CONFIG_IDX].id = ret; + gfs_generic_config_driver.iConfiguration = ret; +#endif + + ret = functionfs_bind(gfs_ffs_data, cdev); + if (unlikely(ret < 0)) + goto error; + + ret = gfs_add_rndis_config(cdev); + if (unlikely(ret < 0)) + goto error_unbind; + + ret = gfs_add_ecm_config(cdev); + if (unlikely(ret < 0)) + goto error_unbind; + + ret = gfs_add_generic_config(cdev); + if (unlikely(ret < 0)) + goto error_unbind; + + return 0; + +error_unbind: + functionfs_unbind(gfs_ffs_data); +error: + gether_cleanup(); +error_quick: + gfs_ffs_data = NULL; + return ret; +} + +static int gfs_unbind(struct usb_composite_dev *cdev) +{ + ENTER(); + + /* We may have been called in an error recovery frem + * composite_bind() after gfs_unbind() failure so we need to + * check if gfs_ffs_data is not NULL since gfs_bind() handles + * all error recovery itself. I'd rather we werent called + * from composite on orror recovery, but what you're gonna + * do...? */ + + if (gfs_ffs_data) { + gether_cleanup(); + functionfs_unbind(gfs_ffs_data); + gfs_ffs_data = NULL; + } + + return 0; +} + + +static int __gfs_do_config(struct usb_configuration *c, + int (*eth)(struct usb_configuration *c, u8 *ethaddr), + u8 *ethaddr) +{ + int ret; + + if (WARN_ON(!gfs_ffs_data)) + return -ENODEV; + + if (gadget_is_otg(c->cdev->gadget)) { + c->descriptors = gfs_otg_desc; + c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; + } + + if (eth) { + ret = eth(c, ethaddr); + if (unlikely(ret < 0)) + return ret; + } + + ret = functionfs_add(c->cdev, c, gfs_ffs_data); + if (unlikely(ret < 0)) + return ret; + + return 0; +} + +#ifdef CONFIG_USB_FUNCTIONFS_RNDIS +static int gfs_do_rndis_config(struct usb_configuration *c) +{ + ENTER(); + + return __gfs_do_config(c, rndis_bind_config, gfs_hostaddr); +} +#endif + +#ifdef CONFIG_USB_FUNCTIONFS_ETH +static int gfs_do_ecm_config(struct usb_configuration *c) +{ + ENTER(); + + return __gfs_do_config(c, + can_support_ecm(c->cdev->gadget) + ? ecm_bind_config : geth_bind_config, + gfs_hostaddr); +} +#endif + +#ifdef CONFIG_USB_FUNCTIONFS_GENERIC +static int gfs_do_generic_config(struct usb_configuration *c) +{ + ENTER(); + + return __gfs_do_config(c, NULL, NULL); +} +#endif -- cgit v1.2.3 From 17b2765ef8db8dc00a24a9afd4646bb3decffbd2 Mon Sep 17 00:00:00 2001 From: Randy Dunlap Date: Thu, 13 May 2010 09:41:12 -0700 Subject: USB: fix functionfs for CONFIG_NET disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix functionfs build to handle CONFIG_NET not enabled, to prevent these build errors: ERROR: "netif_carrier_on" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "netif_carrier_off" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "skb_realloc_headroom" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "skb_trim" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "netif_rx" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "alloc_etherdev_mq" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "ethtool_op_get_link" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "free_netdev" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "register_netdev" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "skb_push" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "skb_pull" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "dev_kfree_skb_any" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "skb_queue_tail" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "__alloc_skb" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "eth_type_trans" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "eth_validate_addr" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "skb_dequeue" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "unregister_netdev" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "__netif_schedule" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "skb_put" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "eth_mac_addr" [drivers/usb/gadget/g_ffs.ko] undefined! ERROR: "dev_get_stats" [drivers/usb/gadget/g_ffs.ko] undefined! Signed-off-by: Randy Dunlap Acked-by: MichaƂ Nazarewicz Signed-off-by: Greg Kroah-Hartman --- drivers/usb/gadget/Kconfig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers/usb/gadget/Kconfig') diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 0282c50d6404..649c0c5f7158 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -726,7 +726,7 @@ config USB_FUNCTIONFS config USB_FUNCTIONFS_ETH bool "Include CDC ECM (Ethernet) function" - depends on USB_FUNCTIONFS + depends on USB_FUNCTIONFS && NET help Include an CDC ECM (Ethernet) funcion in the CDC ECM (Funcion) Filesystem. If you also say "y" to the RNDIS query below the @@ -734,7 +734,7 @@ config USB_FUNCTIONFS_ETH config USB_FUNCTIONFS_RNDIS bool "Include RNDIS (Ethernet) function" - depends on USB_FUNCTIONFS + depends on USB_FUNCTIONFS && NET help Include an RNDIS (Ethernet) funcion in the Funcion Filesystem. If you also say "y" to the CDC ECM query above the gadget will -- cgit v1.2.3