diff options
| author | David Brownell <dbrownell@users.sourceforge.net> | 2008-06-19 21:20:26 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@suse.de> | 2008-07-21 18:16:16 -0400 |
| commit | 19e2068015d4a66f62a0a19be2130d2948ba8024 (patch) | |
| tree | 4ab41b622e137d089e5fed72110a9051829c88fc /drivers/usb/gadget/cdc2.c | |
| parent | 45fe3b8e5342cd1ce307099459c74011d8e01986 (diff) | |
usb gadget: new "CDC Composite" gadget driver
This is a simple example of a composite gadget, combining two
Communications Class Device (CDC) functions: ECM and ACM.
This provides a clear example of how the composite gadget framework
is intended to work. It's surprising that MS-Windows (or at least,
XP and previous) won't "just work" with something this simple...
One /proc/bus/usb/devices listing looks like:
T: Bus=03 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 46 Spd=480 MxCh= 0
D: Ver= 2.00 Cls=02(comm.) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=0525 ProdID=a4aa Rev= 3.01
S: Manufacturer=Linux 2.6.26-rc6-pnut with net2280
S: Product=CDC Composite Gadget
C:* #Ifs= 4 Cfg#= 1 Atr=c0 MxPwr= 2mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=02(comm.) Sub=06 Prot=00 Driver=cdc_ether
E: Ad=83(I) Atr=03(Int.) MxPS= 16 Ivl=32ms
I: If#= 1 Alt= 0 #EPs= 0 Cls=0a(data ) Sub=00 Prot=00 Driver=cdc_ether
I:* If#= 1 Alt= 1 #EPs= 2 Cls=0a(data ) Sub=00 Prot=00 Driver=cdc_ether
E: Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
I:* If#= 2 Alt= 0 #EPs= 1 Cls=02(comm.) Sub=02 Prot=01 Driver=cdc_acm
E: Ad=86(I) Atr=03(Int.) MxPS= 8 Ivl=32ms
I:* If#= 3 Alt= 0 #EPs= 2 Cls=0a(data ) Sub=00 Prot=00 Driver=cdc_acm
E: Ad=84(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E: Ad=05(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms
Not all USB peripheral controller hardware can support this driver.
All the highspeed-capable peripheral controllers with drivers now in
the mainline kernel seem to support this, as does omap_udc. But
many full speed controllers don't have enough endpoints, or (as with
the PXA controllers) don't support altsettings.
Lightly tested.
Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/gadget/cdc2.c')
| -rw-r--r-- | drivers/usb/gadget/cdc2.c | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/drivers/usb/gadget/cdc2.c b/drivers/usb/gadget/cdc2.c new file mode 100644 index 000000000000..289c75277135 --- /dev/null +++ b/drivers/usb/gadget/cdc2.c | |||
| @@ -0,0 +1,246 @@ | |||
| 1 | /* | ||
| 2 | * cdc2.c -- CDC Composite driver, with ECM and ACM support | ||
| 3 | * | ||
| 4 | * Copyright (C) 2008 David Brownell | ||
| 5 | * Copyright (C) 2008 Nokia Corporation | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License as published by | ||
| 9 | * the Free Software Foundation; either version 2 of the License, or | ||
| 10 | * (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This program is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 | * GNU General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License | ||
| 18 | * along with this program; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include <linux/kernel.h> | ||
| 23 | #include <linux/utsname.h> | ||
| 24 | |||
| 25 | #include "u_ether.h" | ||
| 26 | #include "u_serial.h" | ||
| 27 | |||
| 28 | |||
| 29 | #define DRIVER_DESC "CDC Composite Gadget" | ||
| 30 | #define DRIVER_VERSION "King Kamehameha Day 2008" | ||
| 31 | |||
| 32 | /*-------------------------------------------------------------------------*/ | ||
| 33 | |||
| 34 | /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!! | ||
| 35 | * Instead: allocate your own, using normal USB-IF procedures. | ||
| 36 | */ | ||
| 37 | |||
| 38 | /* Thanks to NetChip Technologies for donating this product ID. | ||
| 39 | * It's for devices with only this composite CDC configuration. | ||
| 40 | */ | ||
| 41 | #define CDC_VENDOR_NUM 0x0525 /* NetChip */ | ||
| 42 | #define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */ | ||
| 43 | |||
| 44 | /*-------------------------------------------------------------------------*/ | ||
| 45 | |||
| 46 | static struct usb_device_descriptor device_desc = { | ||
| 47 | .bLength = sizeof device_desc, | ||
| 48 | .bDescriptorType = USB_DT_DEVICE, | ||
| 49 | |||
| 50 | .bcdUSB = __constant_cpu_to_le16(0x0200), | ||
| 51 | |||
| 52 | .bDeviceClass = USB_CLASS_COMM, | ||
| 53 | .bDeviceSubClass = 0, | ||
| 54 | .bDeviceProtocol = 0, | ||
| 55 | /* .bMaxPacketSize0 = f(hardware) */ | ||
| 56 | |||
| 57 | /* Vendor and product id can be overridden by module parameters. */ | ||
| 58 | .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM), | ||
| 59 | .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM), | ||
| 60 | /* .bcdDevice = f(hardware) */ | ||
| 61 | /* .iManufacturer = DYNAMIC */ | ||
| 62 | /* .iProduct = DYNAMIC */ | ||
| 63 | /* NO SERIAL NUMBER */ | ||
| 64 | .bNumConfigurations = 1, | ||
| 65 | }; | ||
| 66 | |||
| 67 | static struct usb_otg_descriptor otg_descriptor = { | ||
| 68 | .bLength = sizeof otg_descriptor, | ||
| 69 | .bDescriptorType = USB_DT_OTG, | ||
| 70 | |||
| 71 | /* REVISIT SRP-only hardware is possible, although | ||
| 72 | * it would not be called "OTG" ... | ||
| 73 | */ | ||
| 74 | .bmAttributes = USB_OTG_SRP | USB_OTG_HNP, | ||
| 75 | }; | ||
| 76 | |||
| 77 | static const struct usb_descriptor_header *otg_desc[] = { | ||
| 78 | (struct usb_descriptor_header *) &otg_descriptor, | ||
| 79 | NULL, | ||
| 80 | }; | ||
| 81 | |||
| 82 | |||
| 83 | /* string IDs are assigned dynamically */ | ||
| 84 | |||
| 85 | #define STRING_MANUFACTURER_IDX 0 | ||
| 86 | #define STRING_PRODUCT_IDX 1 | ||
| 87 | |||
| 88 | static char manufacturer[50]; | ||
| 89 | |||
| 90 | static struct usb_string strings_dev[] = { | ||
| 91 | [STRING_MANUFACTURER_IDX].s = manufacturer, | ||
| 92 | [STRING_PRODUCT_IDX].s = DRIVER_DESC, | ||
| 93 | { } /* end of list */ | ||
| 94 | }; | ||
| 95 | |||
| 96 | static struct usb_gadget_strings stringtab_dev = { | ||
| 97 | .language = 0x0409, /* en-us */ | ||
| 98 | .strings = strings_dev, | ||
| 99 | }; | ||
| 100 | |||
| 101 | static struct usb_gadget_strings *dev_strings[] = { | ||
| 102 | &stringtab_dev, | ||
| 103 | NULL, | ||
| 104 | }; | ||
| 105 | |||
| 106 | static u8 hostaddr[ETH_ALEN]; | ||
| 107 | |||
| 108 | /*-------------------------------------------------------------------------*/ | ||
| 109 | |||
| 110 | /* | ||
| 111 | * We _always_ have both CDC ECM and CDC ACM functions. | ||
| 112 | */ | ||
| 113 | static int __init cdc_do_config(struct usb_configuration *c) | ||
| 114 | { | ||
| 115 | int status; | ||
| 116 | |||
| 117 | if (gadget_is_otg(c->cdev->gadget)) { | ||
| 118 | c->descriptors = otg_desc; | ||
| 119 | c->bmAttributes |= USB_CONFIG_ATT_WAKEUP; | ||
| 120 | } | ||
| 121 | |||
| 122 | status = ecm_bind_config(c, hostaddr); | ||
| 123 | if (status < 0) | ||
| 124 | return status; | ||
| 125 | |||
| 126 | status = acm_bind_config(c, 0); | ||
| 127 | if (status == 0) | ||
| 128 | return status; | ||
| 129 | |||
| 130 | return 0; | ||
| 131 | } | ||
| 132 | |||
| 133 | static struct usb_configuration cdc_config_driver = { | ||
| 134 | .label = "CDC Composite (ECM + ACM)", | ||
| 135 | .bind = cdc_do_config, | ||
| 136 | .bConfigurationValue = 1, | ||
| 137 | /* .iConfiguration = DYNAMIC */ | ||
| 138 | .bmAttributes = USB_CONFIG_ATT_SELFPOWER, | ||
| 139 | .bMaxPower = 1, /* 2 mA, minimal */ | ||
| 140 | }; | ||
| 141 | |||
| 142 | /*-------------------------------------------------------------------------*/ | ||
| 143 | |||
| 144 | static int __init cdc_bind(struct usb_composite_dev *cdev) | ||
| 145 | { | ||
| 146 | int gcnum; | ||
| 147 | struct usb_gadget *gadget = cdev->gadget; | ||
| 148 | int status; | ||
| 149 | |||
| 150 | if (!can_support_ecm(cdev->gadget)) { | ||
| 151 | ERROR(cdev, "controller '%s' not usable\n", gadget->name); | ||
| 152 | return -EINVAL; | ||
| 153 | } | ||
| 154 | |||
| 155 | /* set up network link layer */ | ||
| 156 | status = gether_setup(cdev->gadget, hostaddr); | ||
| 157 | if (status < 0) | ||
| 158 | return status; | ||
| 159 | |||
| 160 | /* set up serial link layer */ | ||
| 161 | status = gserial_setup(cdev->gadget, 1); | ||
| 162 | if (status < 0) | ||
| 163 | goto fail0; | ||
| 164 | |||
| 165 | gcnum = usb_gadget_controller_number(gadget); | ||
| 166 | if (gcnum >= 0) | ||
| 167 | device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum); | ||
| 168 | else { | ||
| 169 | /* We assume that can_support_ecm() tells the truth; | ||
| 170 | * but if the controller isn't recognized at all then | ||
| 171 | * that assumption is a bit more likely to be wrong. | ||
| 172 | */ | ||
| 173 | WARN(cdev, "controller '%s' not recognized; trying %s\n", | ||
| 174 | gadget->name, | ||
| 175 | cdc_config_driver.label); | ||
| 176 | device_desc.bcdDevice = | ||
| 177 | __constant_cpu_to_le16(0x0300 | 0x0099); | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | /* Allocate string descriptor numbers ... note that string | ||
| 182 | * contents can be overridden by the composite_dev glue. | ||
| 183 | */ | ||
| 184 | |||
| 185 | /* device descriptor strings: manufacturer, product */ | ||
| 186 | snprintf(manufacturer, sizeof manufacturer, "%s %s with %s", | ||
| 187 | init_utsname()->sysname, init_utsname()->release, | ||
| 188 | gadget->name); | ||
| 189 | status = usb_string_id(cdev); | ||
| 190 | if (status < 0) | ||
| 191 | goto fail1; | ||
| 192 | strings_dev[STRING_MANUFACTURER_IDX].id = status; | ||
| 193 | device_desc.iManufacturer = status; | ||
| 194 | |||
| 195 | status = usb_string_id(cdev); | ||
| 196 | if (status < 0) | ||
| 197 | goto fail1; | ||
| 198 | strings_dev[STRING_PRODUCT_IDX].id = status; | ||
| 199 | device_desc.iProduct = status; | ||
| 200 | |||
| 201 | /* register our configuration */ | ||
| 202 | status = usb_add_config(cdev, &cdc_config_driver); | ||
| 203 | if (status < 0) | ||
| 204 | goto fail1; | ||
| 205 | |||
| 206 | INFO(cdev, "%s, version: " DRIVER_VERSION "\n", DRIVER_DESC); | ||
| 207 | |||
| 208 | return 0; | ||
| 209 | |||
| 210 | fail1: | ||
| 211 | gserial_cleanup(); | ||
| 212 | fail0: | ||
| 213 | gether_cleanup(); | ||
| 214 | return status; | ||
| 215 | } | ||
| 216 | |||
| 217 | static int __exit cdc_unbind(struct usb_composite_dev *cdev) | ||
| 218 | { | ||
| 219 | gserial_cleanup(); | ||
| 220 | gether_cleanup(); | ||
| 221 | return 0; | ||
| 222 | } | ||
| 223 | |||
| 224 | static struct usb_composite_driver cdc_driver = { | ||
| 225 | .name = "g_cdc", | ||
| 226 | .dev = &device_desc, | ||
| 227 | .strings = dev_strings, | ||
| 228 | .bind = cdc_bind, | ||
| 229 | .unbind = __exit_p(cdc_unbind), | ||
| 230 | }; | ||
| 231 | |||
| 232 | MODULE_DESCRIPTION(DRIVER_DESC); | ||
| 233 | MODULE_AUTHOR("David Brownell"); | ||
| 234 | MODULE_LICENSE("GPL"); | ||
| 235 | |||
| 236 | static int __init init(void) | ||
| 237 | { | ||
| 238 | return usb_composite_register(&cdc_driver); | ||
| 239 | } | ||
| 240 | module_init(init); | ||
| 241 | |||
| 242 | static void __exit cleanup(void) | ||
| 243 | { | ||
| 244 | usb_composite_unregister(&cdc_driver); | ||
| 245 | } | ||
| 246 | module_exit(cleanup); | ||
