| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * f_ecm.c -- USB CDC Ethernet (ECM) link function driver | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2003-2005,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. | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 11 | */ | 
|  | 12 |  | 
|  | 13 | /* #define VERBOSE_DEBUG */ | 
|  | 14 |  | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> | 
|  | 17 | #include <linux/device.h> | 
|  | 18 | #include <linux/etherdevice.h> | 
|  | 19 |  | 
|  | 20 | #include "u_ether.h" | 
|  | 21 |  | 
|  | 22 |  | 
|  | 23 | /* | 
|  | 24 | * This function is a "CDC Ethernet Networking Control Model" (CDC ECM) | 
|  | 25 | * Ethernet link.  The data transfer model is simple (packets sent and | 
|  | 26 | * received over bulk endpoints using normal short packet termination), | 
|  | 27 | * and the control model exposes various data and optional notifications. | 
|  | 28 | * | 
|  | 29 | * ECM is well standardized and (except for Microsoft) supported by most | 
|  | 30 | * operating systems with USB host support.  It's the preferred interop | 
|  | 31 | * solution for Ethernet over USB, at least for firmware based solutions. | 
|  | 32 | * (Hardware solutions tend to be more minimalist.)  A newer and simpler | 
|  | 33 | * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on. | 
|  | 34 | * | 
|  | 35 | * Note that ECM requires the use of "alternate settings" for its data | 
|  | 36 | * interface.  This means that the set_alt() method has real work to do, | 
|  | 37 | * and also means that a get_alt() method is required. | 
|  | 38 | */ | 
|  | 39 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | enum ecm_notify_state { | 
|  | 42 | ECM_NOTIFY_NONE,		/* don't notify */ | 
|  | 43 | ECM_NOTIFY_CONNECT,		/* issue CONNECT next */ | 
|  | 44 | ECM_NOTIFY_SPEED,		/* issue SPEED_CHANGE next */ | 
|  | 45 | }; | 
|  | 46 |  | 
|  | 47 | struct f_ecm { | 
|  | 48 | struct gether			port; | 
|  | 49 | u8				ctrl_id, data_id; | 
|  | 50 |  | 
|  | 51 | char				ethaddr[14]; | 
|  | 52 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 53 | struct usb_ep			*notify; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 54 | struct usb_request		*notify_req; | 
|  | 55 | u8				notify_state; | 
|  | 56 | bool				is_open; | 
|  | 57 |  | 
|  | 58 | /* FIXME is_open needs some irq-ish locking | 
|  | 59 | * ... possibly the same as port.ioport | 
|  | 60 | */ | 
|  | 61 | }; | 
|  | 62 |  | 
|  | 63 | static inline struct f_ecm *func_to_ecm(struct usb_function *f) | 
|  | 64 | { | 
|  | 65 | return container_of(f, struct f_ecm, port.func); | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | /* peak (theoretical) bulk transfer rate in bits-per-second */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 69 | static inline unsigned ecm_bitrate(struct usb_gadget *g) | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 70 | { | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 71 | if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER) | 
|  | 72 | return 13 * 1024 * 8 * 1000 * 8; | 
|  | 73 | else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 74 | return 13 * 512 * 8 * 1000 * 8; | 
|  | 75 | else | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 76 | return 19 * 64 * 1 * 1000 * 8; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | /*-------------------------------------------------------------------------*/ | 
|  | 80 |  | 
|  | 81 | /* | 
|  | 82 | * Include the status endpoint if we can, even though it's optional. | 
|  | 83 | * | 
|  | 84 | * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one | 
|  | 85 | * packet, to simplify cancellation; and a big transfer interval, to | 
|  | 86 | * waste less bandwidth. | 
|  | 87 | * | 
|  | 88 | * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even | 
|  | 89 | * if they ignore the connect/disconnect notifications that real aether | 
|  | 90 | * can provide.  More advanced cdc configurations might want to support | 
|  | 91 | * encapsulated commands (vendor-specific, using control-OUT). | 
|  | 92 | */ | 
|  | 93 |  | 
|  | 94 | #define LOG2_STATUS_INTERVAL_MSEC	5	/* 1 << 5 == 32 msec */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 95 | #define ECM_STATUS_BYTECOUNT		16	/* 8 byte header + data */ | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 96 |  | 
|  | 97 |  | 
|  | 98 | /* interface descriptor: */ | 
|  | 99 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 100 | static struct usb_interface_descriptor ecm_control_intf = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 101 | .bLength =		sizeof ecm_control_intf, | 
|  | 102 | .bDescriptorType =	USB_DT_INTERFACE, | 
|  | 103 |  | 
|  | 104 | /* .bInterfaceNumber = DYNAMIC */ | 
|  | 105 | /* status endpoint is optional; this could be patched later */ | 
|  | 106 | .bNumEndpoints =	1, | 
|  | 107 | .bInterfaceClass =	USB_CLASS_COMM, | 
|  | 108 | .bInterfaceSubClass =	USB_CDC_SUBCLASS_ETHERNET, | 
|  | 109 | .bInterfaceProtocol =	USB_CDC_PROTO_NONE, | 
|  | 110 | /* .iInterface = DYNAMIC */ | 
|  | 111 | }; | 
|  | 112 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 113 | static struct usb_cdc_header_desc ecm_header_desc = { | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 114 | .bLength =		sizeof ecm_header_desc, | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 115 | .bDescriptorType =	USB_DT_CS_INTERFACE, | 
|  | 116 | .bDescriptorSubType =	USB_CDC_HEADER_TYPE, | 
|  | 117 |  | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 118 | .bcdCDC =		cpu_to_le16(0x0110), | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 119 | }; | 
|  | 120 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 121 | static struct usb_cdc_union_desc ecm_union_desc = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 122 | .bLength =		sizeof(ecm_union_desc), | 
|  | 123 | .bDescriptorType =	USB_DT_CS_INTERFACE, | 
|  | 124 | .bDescriptorSubType =	USB_CDC_UNION_TYPE, | 
|  | 125 | /* .bMasterInterface0 =	DYNAMIC */ | 
|  | 126 | /* .bSlaveInterface0 =	DYNAMIC */ | 
|  | 127 | }; | 
|  | 128 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 129 | static struct usb_cdc_ether_desc ecm_desc = { | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 130 | .bLength =		sizeof ecm_desc, | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 131 | .bDescriptorType =	USB_DT_CS_INTERFACE, | 
|  | 132 | .bDescriptorSubType =	USB_CDC_ETHERNET_TYPE, | 
|  | 133 |  | 
|  | 134 | /* this descriptor actually adds value, surprise! */ | 
|  | 135 | /* .iMACAddress = DYNAMIC */ | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 136 | .bmEthernetStatistics =	cpu_to_le32(0), /* no statistics */ | 
|  | 137 | .wMaxSegmentSize =	cpu_to_le16(ETH_FRAME_LEN), | 
|  | 138 | .wNumberMCFilters =	cpu_to_le16(0), | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 139 | .bNumberPowerFilters =	0, | 
|  | 140 | }; | 
|  | 141 |  | 
|  | 142 | /* the default data interface has no endpoints ... */ | 
|  | 143 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 144 | static struct usb_interface_descriptor ecm_data_nop_intf = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 145 | .bLength =		sizeof ecm_data_nop_intf, | 
|  | 146 | .bDescriptorType =	USB_DT_INTERFACE, | 
|  | 147 |  | 
|  | 148 | .bInterfaceNumber =	1, | 
|  | 149 | .bAlternateSetting =	0, | 
|  | 150 | .bNumEndpoints =	0, | 
|  | 151 | .bInterfaceClass =	USB_CLASS_CDC_DATA, | 
|  | 152 | .bInterfaceSubClass =	0, | 
|  | 153 | .bInterfaceProtocol =	0, | 
|  | 154 | /* .iInterface = DYNAMIC */ | 
|  | 155 | }; | 
|  | 156 |  | 
|  | 157 | /* ... but the "real" data interface has two bulk endpoints */ | 
|  | 158 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 159 | static struct usb_interface_descriptor ecm_data_intf = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 160 | .bLength =		sizeof ecm_data_intf, | 
|  | 161 | .bDescriptorType =	USB_DT_INTERFACE, | 
|  | 162 |  | 
|  | 163 | .bInterfaceNumber =	1, | 
|  | 164 | .bAlternateSetting =	1, | 
|  | 165 | .bNumEndpoints =	2, | 
|  | 166 | .bInterfaceClass =	USB_CLASS_CDC_DATA, | 
|  | 167 | .bInterfaceSubClass =	0, | 
|  | 168 | .bInterfaceProtocol =	0, | 
|  | 169 | /* .iInterface = DYNAMIC */ | 
|  | 170 | }; | 
|  | 171 |  | 
|  | 172 | /* full speed support: */ | 
|  | 173 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 174 | static struct usb_endpoint_descriptor fs_ecm_notify_desc = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 175 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 176 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 177 |  | 
|  | 178 | .bEndpointAddress =	USB_DIR_IN, | 
|  | 179 | .bmAttributes =		USB_ENDPOINT_XFER_INT, | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 180 | .wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT), | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 181 | .bInterval =		1 << LOG2_STATUS_INTERVAL_MSEC, | 
|  | 182 | }; | 
|  | 183 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 184 | static struct usb_endpoint_descriptor fs_ecm_in_desc = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 185 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 186 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 187 |  | 
|  | 188 | .bEndpointAddress =	USB_DIR_IN, | 
|  | 189 | .bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
|  | 190 | }; | 
|  | 191 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 192 | static struct usb_endpoint_descriptor fs_ecm_out_desc = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 193 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 194 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 195 |  | 
|  | 196 | .bEndpointAddress =	USB_DIR_OUT, | 
|  | 197 | .bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
|  | 198 | }; | 
|  | 199 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 200 | static struct usb_descriptor_header *ecm_fs_function[] = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 201 | /* CDC ECM control descriptors */ | 
|  | 202 | (struct usb_descriptor_header *) &ecm_control_intf, | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 203 | (struct usb_descriptor_header *) &ecm_header_desc, | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 204 | (struct usb_descriptor_header *) &ecm_union_desc, | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 205 | (struct usb_descriptor_header *) &ecm_desc, | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 206 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 207 | /* NOTE: status endpoint might need to be removed */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 208 | (struct usb_descriptor_header *) &fs_ecm_notify_desc, | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 209 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 210 | /* data interface, altsettings 0 and 1 */ | 
|  | 211 | (struct usb_descriptor_header *) &ecm_data_nop_intf, | 
|  | 212 | (struct usb_descriptor_header *) &ecm_data_intf, | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 213 | (struct usb_descriptor_header *) &fs_ecm_in_desc, | 
|  | 214 | (struct usb_descriptor_header *) &fs_ecm_out_desc, | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 215 | NULL, | 
|  | 216 | }; | 
|  | 217 |  | 
|  | 218 | /* high speed support: */ | 
|  | 219 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 220 | static struct usb_endpoint_descriptor hs_ecm_notify_desc = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 221 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 222 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 223 |  | 
|  | 224 | .bEndpointAddress =	USB_DIR_IN, | 
|  | 225 | .bmAttributes =		USB_ENDPOINT_XFER_INT, | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 226 | .wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT), | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 227 | .bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4, | 
|  | 228 | }; | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 229 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 230 | static struct usb_endpoint_descriptor hs_ecm_in_desc = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 231 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 232 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 233 |  | 
|  | 234 | .bEndpointAddress =	USB_DIR_IN, | 
|  | 235 | .bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 236 | .wMaxPacketSize =	cpu_to_le16(512), | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 237 | }; | 
|  | 238 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 239 | static struct usb_endpoint_descriptor hs_ecm_out_desc = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 240 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 241 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 242 |  | 
|  | 243 | .bEndpointAddress =	USB_DIR_OUT, | 
|  | 244 | .bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
| Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 245 | .wMaxPacketSize =	cpu_to_le16(512), | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 246 | }; | 
|  | 247 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 248 | static struct usb_descriptor_header *ecm_hs_function[] = { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 249 | /* CDC ECM control descriptors */ | 
|  | 250 | (struct usb_descriptor_header *) &ecm_control_intf, | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 251 | (struct usb_descriptor_header *) &ecm_header_desc, | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 252 | (struct usb_descriptor_header *) &ecm_union_desc, | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 253 | (struct usb_descriptor_header *) &ecm_desc, | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 254 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 255 | /* NOTE: status endpoint might need to be removed */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 256 | (struct usb_descriptor_header *) &hs_ecm_notify_desc, | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 257 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 258 | /* data interface, altsettings 0 and 1 */ | 
|  | 259 | (struct usb_descriptor_header *) &ecm_data_nop_intf, | 
|  | 260 | (struct usb_descriptor_header *) &ecm_data_intf, | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 261 | (struct usb_descriptor_header *) &hs_ecm_in_desc, | 
|  | 262 | (struct usb_descriptor_header *) &hs_ecm_out_desc, | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 263 | NULL, | 
|  | 264 | }; | 
|  | 265 |  | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 266 | /* super speed support: */ | 
|  | 267 |  | 
|  | 268 | static struct usb_endpoint_descriptor ss_ecm_notify_desc = { | 
|  | 269 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 270 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 271 |  | 
|  | 272 | .bEndpointAddress =	USB_DIR_IN, | 
|  | 273 | .bmAttributes =		USB_ENDPOINT_XFER_INT, | 
|  | 274 | .wMaxPacketSize =	cpu_to_le16(ECM_STATUS_BYTECOUNT), | 
|  | 275 | .bInterval =		LOG2_STATUS_INTERVAL_MSEC + 4, | 
|  | 276 | }; | 
|  | 277 |  | 
|  | 278 | static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = { | 
|  | 279 | .bLength =		sizeof ss_ecm_intr_comp_desc, | 
|  | 280 | .bDescriptorType =	USB_DT_SS_ENDPOINT_COMP, | 
|  | 281 |  | 
|  | 282 | /* the following 3 values can be tweaked if necessary */ | 
|  | 283 | /* .bMaxBurst =		0, */ | 
|  | 284 | /* .bmAttributes =	0, */ | 
|  | 285 | .wBytesPerInterval =	cpu_to_le16(ECM_STATUS_BYTECOUNT), | 
|  | 286 | }; | 
|  | 287 |  | 
|  | 288 | static struct usb_endpoint_descriptor ss_ecm_in_desc = { | 
|  | 289 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 290 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 291 |  | 
|  | 292 | .bEndpointAddress =	USB_DIR_IN, | 
|  | 293 | .bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
|  | 294 | .wMaxPacketSize =	cpu_to_le16(1024), | 
|  | 295 | }; | 
|  | 296 |  | 
|  | 297 | static struct usb_endpoint_descriptor ss_ecm_out_desc = { | 
|  | 298 | .bLength =		USB_DT_ENDPOINT_SIZE, | 
|  | 299 | .bDescriptorType =	USB_DT_ENDPOINT, | 
|  | 300 |  | 
|  | 301 | .bEndpointAddress =	USB_DIR_OUT, | 
|  | 302 | .bmAttributes =		USB_ENDPOINT_XFER_BULK, | 
|  | 303 | .wMaxPacketSize =	cpu_to_le16(1024), | 
|  | 304 | }; | 
|  | 305 |  | 
|  | 306 | static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = { | 
|  | 307 | .bLength =		sizeof ss_ecm_bulk_comp_desc, | 
|  | 308 | .bDescriptorType =	USB_DT_SS_ENDPOINT_COMP, | 
|  | 309 |  | 
|  | 310 | /* the following 2 values can be tweaked if necessary */ | 
|  | 311 | /* .bMaxBurst =		0, */ | 
|  | 312 | /* .bmAttributes =	0, */ | 
|  | 313 | }; | 
|  | 314 |  | 
|  | 315 | static struct usb_descriptor_header *ecm_ss_function[] = { | 
|  | 316 | /* CDC ECM control descriptors */ | 
|  | 317 | (struct usb_descriptor_header *) &ecm_control_intf, | 
|  | 318 | (struct usb_descriptor_header *) &ecm_header_desc, | 
|  | 319 | (struct usb_descriptor_header *) &ecm_union_desc, | 
|  | 320 | (struct usb_descriptor_header *) &ecm_desc, | 
|  | 321 |  | 
|  | 322 | /* NOTE: status endpoint might need to be removed */ | 
|  | 323 | (struct usb_descriptor_header *) &ss_ecm_notify_desc, | 
|  | 324 | (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc, | 
|  | 325 |  | 
|  | 326 | /* data interface, altsettings 0 and 1 */ | 
|  | 327 | (struct usb_descriptor_header *) &ecm_data_nop_intf, | 
|  | 328 | (struct usb_descriptor_header *) &ecm_data_intf, | 
|  | 329 | (struct usb_descriptor_header *) &ss_ecm_in_desc, | 
|  | 330 | (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc, | 
|  | 331 | (struct usb_descriptor_header *) &ss_ecm_out_desc, | 
|  | 332 | (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc, | 
|  | 333 | NULL, | 
|  | 334 | }; | 
|  | 335 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 336 | /* string descriptors: */ | 
|  | 337 |  | 
|  | 338 | static struct usb_string ecm_string_defs[] = { | 
|  | 339 | [0].s = "CDC Ethernet Control Model (ECM)", | 
|  | 340 | [1].s = NULL /* DYNAMIC */, | 
|  | 341 | [2].s = "CDC Ethernet Data", | 
|  | 342 | {  } /* end of list */ | 
|  | 343 | }; | 
|  | 344 |  | 
|  | 345 | static struct usb_gadget_strings ecm_string_table = { | 
|  | 346 | .language =		0x0409,	/* en-us */ | 
|  | 347 | .strings =		ecm_string_defs, | 
|  | 348 | }; | 
|  | 349 |  | 
|  | 350 | static struct usb_gadget_strings *ecm_strings[] = { | 
|  | 351 | &ecm_string_table, | 
|  | 352 | NULL, | 
|  | 353 | }; | 
|  | 354 |  | 
|  | 355 | /*-------------------------------------------------------------------------*/ | 
|  | 356 |  | 
|  | 357 | static void ecm_do_notify(struct f_ecm *ecm) | 
|  | 358 | { | 
|  | 359 | struct usb_request		*req = ecm->notify_req; | 
|  | 360 | struct usb_cdc_notification	*event; | 
|  | 361 | struct usb_composite_dev	*cdev = ecm->port.func.config->cdev; | 
|  | 362 | __le32				*data; | 
|  | 363 | int				status; | 
|  | 364 |  | 
|  | 365 | /* notification already in flight? */ | 
|  | 366 | if (!req) | 
|  | 367 | return; | 
|  | 368 |  | 
|  | 369 | event = req->buf; | 
|  | 370 | switch (ecm->notify_state) { | 
|  | 371 | case ECM_NOTIFY_NONE: | 
|  | 372 | return; | 
|  | 373 |  | 
|  | 374 | case ECM_NOTIFY_CONNECT: | 
|  | 375 | event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION; | 
|  | 376 | if (ecm->is_open) | 
|  | 377 | event->wValue = cpu_to_le16(1); | 
|  | 378 | else | 
|  | 379 | event->wValue = cpu_to_le16(0); | 
|  | 380 | event->wLength = 0; | 
|  | 381 | req->length = sizeof *event; | 
|  | 382 |  | 
|  | 383 | DBG(cdev, "notify connect %s\n", | 
|  | 384 | ecm->is_open ? "true" : "false"); | 
|  | 385 | ecm->notify_state = ECM_NOTIFY_SPEED; | 
|  | 386 | break; | 
|  | 387 |  | 
|  | 388 | case ECM_NOTIFY_SPEED: | 
|  | 389 | event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE; | 
|  | 390 | event->wValue = cpu_to_le16(0); | 
|  | 391 | event->wLength = cpu_to_le16(8); | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 392 | req->length = ECM_STATUS_BYTECOUNT; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 393 |  | 
|  | 394 | /* SPEED_CHANGE data is up/down speeds in bits/sec */ | 
|  | 395 | data = req->buf + sizeof *event; | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 396 | data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget)); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 397 | data[1] = data[0]; | 
|  | 398 |  | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 399 | DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget)); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 400 | ecm->notify_state = ECM_NOTIFY_NONE; | 
|  | 401 | break; | 
|  | 402 | } | 
|  | 403 | event->bmRequestType = 0xA1; | 
|  | 404 | event->wIndex = cpu_to_le16(ecm->ctrl_id); | 
|  | 405 |  | 
|  | 406 | ecm->notify_req = NULL; | 
|  | 407 | status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC); | 
|  | 408 | if (status < 0) { | 
|  | 409 | ecm->notify_req = req; | 
|  | 410 | DBG(cdev, "notify --> %d\n", status); | 
|  | 411 | } | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | static void ecm_notify(struct f_ecm *ecm) | 
|  | 415 | { | 
|  | 416 | /* NOTE on most versions of Linux, host side cdc-ethernet | 
|  | 417 | * won't listen for notifications until its netdevice opens. | 
|  | 418 | * The first notification then sits in the FIFO for a long | 
|  | 419 | * time, and the second one is queued. | 
|  | 420 | */ | 
|  | 421 | ecm->notify_state = ECM_NOTIFY_CONNECT; | 
|  | 422 | ecm_do_notify(ecm); | 
|  | 423 | } | 
|  | 424 |  | 
|  | 425 | static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req) | 
|  | 426 | { | 
|  | 427 | struct f_ecm			*ecm = req->context; | 
|  | 428 | struct usb_composite_dev	*cdev = ecm->port.func.config->cdev; | 
|  | 429 | struct usb_cdc_notification	*event = req->buf; | 
|  | 430 |  | 
|  | 431 | switch (req->status) { | 
|  | 432 | case 0: | 
|  | 433 | /* no fault */ | 
|  | 434 | break; | 
|  | 435 | case -ECONNRESET: | 
|  | 436 | case -ESHUTDOWN: | 
|  | 437 | ecm->notify_state = ECM_NOTIFY_NONE; | 
|  | 438 | break; | 
|  | 439 | default: | 
|  | 440 | DBG(cdev, "event %02x --> %d\n", | 
|  | 441 | event->bNotificationType, req->status); | 
|  | 442 | break; | 
|  | 443 | } | 
|  | 444 | ecm->notify_req = req; | 
|  | 445 | ecm_do_notify(ecm); | 
|  | 446 | } | 
|  | 447 |  | 
|  | 448 | static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | 
|  | 449 | { | 
|  | 450 | struct f_ecm		*ecm = func_to_ecm(f); | 
|  | 451 | struct usb_composite_dev *cdev = f->config->cdev; | 
|  | 452 | struct usb_request	*req = cdev->req; | 
|  | 453 | int			value = -EOPNOTSUPP; | 
|  | 454 | u16			w_index = le16_to_cpu(ctrl->wIndex); | 
|  | 455 | u16			w_value = le16_to_cpu(ctrl->wValue); | 
|  | 456 | u16			w_length = le16_to_cpu(ctrl->wLength); | 
|  | 457 |  | 
|  | 458 | /* composite driver infrastructure handles everything except | 
|  | 459 | * CDC class messages; interface activation uses set_alt(). | 
|  | 460 | */ | 
|  | 461 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { | 
|  | 462 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | 
|  | 463 | | USB_CDC_SET_ETHERNET_PACKET_FILTER: | 
|  | 464 | /* see 6.2.30: no data, wIndex = interface, | 
|  | 465 | * wValue = packet filter bitmap | 
|  | 466 | */ | 
|  | 467 | if (w_length != 0 || w_index != ecm->ctrl_id) | 
|  | 468 | goto invalid; | 
|  | 469 | DBG(cdev, "packet filter %02x\n", w_value); | 
|  | 470 | /* REVISIT locking of cdc_filter.  This assumes the UDC | 
|  | 471 | * driver won't have a concurrent packet TX irq running on | 
|  | 472 | * another CPU; or that if it does, this write is atomic... | 
|  | 473 | */ | 
|  | 474 | ecm->port.cdc_filter = w_value; | 
|  | 475 | value = 0; | 
|  | 476 | break; | 
|  | 477 |  | 
|  | 478 | /* and optionally: | 
|  | 479 | * case USB_CDC_SEND_ENCAPSULATED_COMMAND: | 
|  | 480 | * case USB_CDC_GET_ENCAPSULATED_RESPONSE: | 
|  | 481 | * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS: | 
|  | 482 | * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER: | 
|  | 483 | * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER: | 
|  | 484 | * case USB_CDC_GET_ETHERNET_STATISTIC: | 
|  | 485 | */ | 
|  | 486 |  | 
|  | 487 | default: | 
|  | 488 | invalid: | 
|  | 489 | DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", | 
|  | 490 | ctrl->bRequestType, ctrl->bRequest, | 
|  | 491 | w_value, w_index, w_length); | 
|  | 492 | } | 
|  | 493 |  | 
|  | 494 | /* respond with data transfer or status phase? */ | 
|  | 495 | if (value >= 0) { | 
|  | 496 | DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n", | 
|  | 497 | ctrl->bRequestType, ctrl->bRequest, | 
|  | 498 | w_value, w_index, w_length); | 
|  | 499 | req->zero = 0; | 
|  | 500 | req->length = value; | 
|  | 501 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | 
|  | 502 | if (value < 0) | 
|  | 503 | ERROR(cdev, "ecm req %02x.%02x response err %d\n", | 
|  | 504 | ctrl->bRequestType, ctrl->bRequest, | 
|  | 505 | value); | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | /* device either stalls (value < 0) or reports success */ | 
|  | 509 | return value; | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 |  | 
|  | 513 | static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | 
|  | 514 | { | 
|  | 515 | struct f_ecm		*ecm = func_to_ecm(f); | 
|  | 516 | struct usb_composite_dev *cdev = f->config->cdev; | 
|  | 517 |  | 
|  | 518 | /* Control interface has only altsetting 0 */ | 
|  | 519 | if (intf == ecm->ctrl_id) { | 
|  | 520 | if (alt != 0) | 
|  | 521 | goto fail; | 
|  | 522 |  | 
|  | 523 | if (ecm->notify->driver_data) { | 
|  | 524 | VDBG(cdev, "reset ecm control %d\n", intf); | 
|  | 525 | usb_ep_disable(ecm->notify); | 
| Tatyana Brokhman | ea2a1df7 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 526 | } | 
|  | 527 | if (!(ecm->notify->desc)) { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 528 | VDBG(cdev, "init ecm ctrl %d\n", intf); | 
| Tatyana Brokhman | ea2a1df7 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 529 | if (config_ep_by_speed(cdev->gadget, f, ecm->notify)) | 
|  | 530 | goto fail; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 531 | } | 
| Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 532 | usb_ep_enable(ecm->notify); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 533 | ecm->notify->driver_data = ecm; | 
|  | 534 |  | 
|  | 535 | /* Data interface has two altsettings, 0 and 1 */ | 
|  | 536 | } else if (intf == ecm->data_id) { | 
|  | 537 | if (alt > 1) | 
|  | 538 | goto fail; | 
|  | 539 |  | 
|  | 540 | if (ecm->port.in_ep->driver_data) { | 
|  | 541 | DBG(cdev, "reset ecm\n"); | 
|  | 542 | gether_disconnect(&ecm->port); | 
|  | 543 | } | 
|  | 544 |  | 
| Tatyana Brokhman | ea2a1df7 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 545 | if (!ecm->port.in_ep->desc || | 
|  | 546 | !ecm->port.out_ep->desc) { | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 547 | DBG(cdev, "init ecm\n"); | 
| Tatyana Brokhman | ea2a1df7 | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 548 | if (config_ep_by_speed(cdev->gadget, f, | 
|  | 549 | ecm->port.in_ep) || | 
|  | 550 | config_ep_by_speed(cdev->gadget, f, | 
|  | 551 | ecm->port.out_ep)) { | 
|  | 552 | ecm->port.in_ep->desc = NULL; | 
|  | 553 | ecm->port.out_ep->desc = NULL; | 
|  | 554 | goto fail; | 
|  | 555 | } | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 556 | } | 
|  | 557 |  | 
|  | 558 | /* CDC Ethernet only sends data in non-default altsettings. | 
|  | 559 | * Changing altsettings resets filters, statistics, etc. | 
|  | 560 | */ | 
|  | 561 | if (alt == 1) { | 
|  | 562 | struct net_device	*net; | 
|  | 563 |  | 
|  | 564 | /* Enable zlps by default for ECM conformance; | 
| Christoph Egger | 90f7976 | 2010-02-05 13:24:12 +0100 | [diff] [blame] | 565 | * override for musb_hdrc (avoids txdma ovhead). | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 566 | */ | 
| Christoph Egger | 90f7976 | 2010-02-05 13:24:12 +0100 | [diff] [blame] | 567 | ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget) | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 568 | ); | 
|  | 569 | ecm->port.cdc_filter = DEFAULT_FILTER; | 
|  | 570 | DBG(cdev, "activate ecm\n"); | 
|  | 571 | net = gether_connect(&ecm->port); | 
|  | 572 | if (IS_ERR(net)) | 
|  | 573 | return PTR_ERR(net); | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | /* NOTE this can be a minor disagreement with the ECM spec, | 
|  | 577 | * which says speed notifications will "always" follow | 
|  | 578 | * connection notifications.  But we allow one connect to | 
|  | 579 | * follow another (if the first is in flight), and instead | 
|  | 580 | * just guarantee that a speed notification is always sent. | 
|  | 581 | */ | 
|  | 582 | ecm_notify(ecm); | 
|  | 583 | } else | 
|  | 584 | goto fail; | 
|  | 585 |  | 
|  | 586 | return 0; | 
|  | 587 | fail: | 
|  | 588 | return -EINVAL; | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | /* Because the data interface supports multiple altsettings, | 
|  | 592 | * this ECM function *MUST* implement a get_alt() method. | 
|  | 593 | */ | 
|  | 594 | static int ecm_get_alt(struct usb_function *f, unsigned intf) | 
|  | 595 | { | 
|  | 596 | struct f_ecm		*ecm = func_to_ecm(f); | 
|  | 597 |  | 
|  | 598 | if (intf == ecm->ctrl_id) | 
|  | 599 | return 0; | 
|  | 600 | return ecm->port.in_ep->driver_data ? 1 : 0; | 
|  | 601 | } | 
|  | 602 |  | 
|  | 603 | static void ecm_disable(struct usb_function *f) | 
|  | 604 | { | 
|  | 605 | struct f_ecm		*ecm = func_to_ecm(f); | 
|  | 606 | struct usb_composite_dev *cdev = f->config->cdev; | 
|  | 607 |  | 
|  | 608 | DBG(cdev, "ecm deactivated\n"); | 
|  | 609 |  | 
|  | 610 | if (ecm->port.in_ep->driver_data) | 
|  | 611 | gether_disconnect(&ecm->port); | 
|  | 612 |  | 
|  | 613 | if (ecm->notify->driver_data) { | 
|  | 614 | usb_ep_disable(ecm->notify); | 
|  | 615 | ecm->notify->driver_data = NULL; | 
| Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 616 | ecm->notify->desc = NULL; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 617 | } | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | /*-------------------------------------------------------------------------*/ | 
|  | 621 |  | 
|  | 622 | /* | 
|  | 623 | * Callbacks let us notify the host about connect/disconnect when the | 
|  | 624 | * net device is opened or closed. | 
|  | 625 | * | 
|  | 626 | * For testing, note that link states on this side include both opened | 
|  | 627 | * and closed variants of: | 
|  | 628 | * | 
|  | 629 | *   - disconnected/unconfigured | 
|  | 630 | *   - configured but inactive (data alt 0) | 
|  | 631 | *   - configured and active (data alt 1) | 
|  | 632 | * | 
|  | 633 | * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and | 
|  | 634 | * SET_INTERFACE (altsetting).  Remember also that "configured" doesn't | 
|  | 635 | * imply the host is actually polling the notification endpoint, and | 
|  | 636 | * likewise that "active" doesn't imply it's actually using the data | 
|  | 637 | * endpoints for traffic. | 
|  | 638 | */ | 
|  | 639 |  | 
|  | 640 | static void ecm_open(struct gether *geth) | 
|  | 641 | { | 
|  | 642 | struct f_ecm		*ecm = func_to_ecm(&geth->func); | 
|  | 643 |  | 
|  | 644 | DBG(ecm->port.func.config->cdev, "%s\n", __func__); | 
|  | 645 |  | 
|  | 646 | ecm->is_open = true; | 
|  | 647 | ecm_notify(ecm); | 
|  | 648 | } | 
|  | 649 |  | 
|  | 650 | static void ecm_close(struct gether *geth) | 
|  | 651 | { | 
|  | 652 | struct f_ecm		*ecm = func_to_ecm(&geth->func); | 
|  | 653 |  | 
|  | 654 | DBG(ecm->port.func.config->cdev, "%s\n", __func__); | 
|  | 655 |  | 
|  | 656 | ecm->is_open = false; | 
|  | 657 | ecm_notify(ecm); | 
|  | 658 | } | 
|  | 659 |  | 
|  | 660 | /*-------------------------------------------------------------------------*/ | 
|  | 661 |  | 
|  | 662 | /* ethernet function driver setup/binding */ | 
|  | 663 |  | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 664 | static int | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 665 | ecm_bind(struct usb_configuration *c, struct usb_function *f) | 
|  | 666 | { | 
|  | 667 | struct usb_composite_dev *cdev = c->cdev; | 
|  | 668 | struct f_ecm		*ecm = func_to_ecm(f); | 
|  | 669 | int			status; | 
|  | 670 | struct usb_ep		*ep; | 
|  | 671 |  | 
|  | 672 | /* allocate instance-specific interface IDs */ | 
|  | 673 | status = usb_interface_id(c, f); | 
|  | 674 | if (status < 0) | 
|  | 675 | goto fail; | 
|  | 676 | ecm->ctrl_id = status; | 
|  | 677 |  | 
|  | 678 | ecm_control_intf.bInterfaceNumber = status; | 
|  | 679 | ecm_union_desc.bMasterInterface0 = status; | 
|  | 680 |  | 
|  | 681 | status = usb_interface_id(c, f); | 
|  | 682 | if (status < 0) | 
|  | 683 | goto fail; | 
|  | 684 | ecm->data_id = status; | 
|  | 685 |  | 
|  | 686 | ecm_data_nop_intf.bInterfaceNumber = status; | 
|  | 687 | ecm_data_intf.bInterfaceNumber = status; | 
|  | 688 | ecm_union_desc.bSlaveInterface0 = status; | 
|  | 689 |  | 
|  | 690 | status = -ENODEV; | 
|  | 691 |  | 
|  | 692 | /* allocate instance-specific endpoints */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 693 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 694 | if (!ep) | 
|  | 695 | goto fail; | 
|  | 696 | ecm->port.in_ep = ep; | 
|  | 697 | ep->driver_data = cdev;	/* claim */ | 
|  | 698 |  | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 699 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 700 | if (!ep) | 
|  | 701 | goto fail; | 
|  | 702 | ecm->port.out_ep = ep; | 
|  | 703 | ep->driver_data = cdev;	/* claim */ | 
|  | 704 |  | 
|  | 705 | /* NOTE:  a status/notification endpoint is *OPTIONAL* but we | 
|  | 706 | * don't treat it that way.  It's simpler, and some newer CDC | 
|  | 707 | * profiles (wireless handsets) no longer treat it as optional. | 
|  | 708 | */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 709 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 710 | if (!ep) | 
|  | 711 | goto fail; | 
|  | 712 | ecm->notify = ep; | 
|  | 713 | ep->driver_data = cdev;	/* claim */ | 
|  | 714 |  | 
|  | 715 | status = -ENOMEM; | 
|  | 716 |  | 
|  | 717 | /* allocate notification request and buffer */ | 
|  | 718 | ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); | 
|  | 719 | if (!ecm->notify_req) | 
|  | 720 | goto fail; | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 721 | ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 722 | if (!ecm->notify_req->buf) | 
|  | 723 | goto fail; | 
|  | 724 | ecm->notify_req->context = ecm; | 
|  | 725 | ecm->notify_req->complete = ecm_notify_complete; | 
|  | 726 |  | 
|  | 727 | /* copy descriptors, and track endpoint copies */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 728 | f->descriptors = usb_copy_descriptors(ecm_fs_function); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 729 | if (!f->descriptors) | 
|  | 730 | goto fail; | 
|  | 731 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 732 | /* support all relevant hardware speeds... we expect that when | 
|  | 733 | * hardware is dual speed, all bulk-capable endpoints work at | 
|  | 734 | * both speeds | 
|  | 735 | */ | 
|  | 736 | if (gadget_is_dualspeed(c->cdev->gadget)) { | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 737 | hs_ecm_in_desc.bEndpointAddress = | 
|  | 738 | fs_ecm_in_desc.bEndpointAddress; | 
|  | 739 | hs_ecm_out_desc.bEndpointAddress = | 
|  | 740 | fs_ecm_out_desc.bEndpointAddress; | 
|  | 741 | hs_ecm_notify_desc.bEndpointAddress = | 
|  | 742 | fs_ecm_notify_desc.bEndpointAddress; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 743 |  | 
|  | 744 | /* copy descriptors, and track endpoint copies */ | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 745 | f->hs_descriptors = usb_copy_descriptors(ecm_hs_function); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 746 | if (!f->hs_descriptors) | 
|  | 747 | goto fail; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 748 | } | 
|  | 749 |  | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 750 | if (gadget_is_superspeed(c->cdev->gadget)) { | 
|  | 751 | ss_ecm_in_desc.bEndpointAddress = | 
|  | 752 | fs_ecm_in_desc.bEndpointAddress; | 
|  | 753 | ss_ecm_out_desc.bEndpointAddress = | 
|  | 754 | fs_ecm_out_desc.bEndpointAddress; | 
|  | 755 | ss_ecm_notify_desc.bEndpointAddress = | 
|  | 756 | fs_ecm_notify_desc.bEndpointAddress; | 
|  | 757 |  | 
|  | 758 | /* copy descriptors, and track endpoint copies */ | 
|  | 759 | f->ss_descriptors = usb_copy_descriptors(ecm_ss_function); | 
|  | 760 | if (!f->ss_descriptors) | 
|  | 761 | goto fail; | 
|  | 762 | } | 
|  | 763 |  | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 764 | /* NOTE:  all that is done without knowing or caring about | 
|  | 765 | * the network link ... which is unavailable to this code | 
|  | 766 | * until we're activated via set_alt(). | 
|  | 767 | */ | 
|  | 768 |  | 
|  | 769 | ecm->port.open = ecm_open; | 
|  | 770 | ecm->port.close = ecm_close; | 
|  | 771 |  | 
|  | 772 | DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n", | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 773 | gadget_is_superspeed(c->cdev->gadget) ? "super" : | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 774 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | 
|  | 775 | ecm->port.in_ep->name, ecm->port.out_ep->name, | 
|  | 776 | ecm->notify->name); | 
|  | 777 | return 0; | 
|  | 778 |  | 
|  | 779 | fail: | 
|  | 780 | if (f->descriptors) | 
|  | 781 | usb_free_descriptors(f->descriptors); | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 782 | if (f->hs_descriptors) | 
|  | 783 | usb_free_descriptors(f->hs_descriptors); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 784 |  | 
|  | 785 | if (ecm->notify_req) { | 
|  | 786 | kfree(ecm->notify_req->buf); | 
|  | 787 | usb_ep_free_request(ecm->notify, ecm->notify_req); | 
|  | 788 | } | 
|  | 789 |  | 
|  | 790 | /* we might as well release our claims on endpoints */ | 
|  | 791 | if (ecm->notify) | 
|  | 792 | ecm->notify->driver_data = NULL; | 
| Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 793 | if (ecm->port.out_ep->desc) | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 794 | ecm->port.out_ep->driver_data = NULL; | 
| Tatyana Brokhman | 72c973d | 2011-06-28 16:33:48 +0300 | [diff] [blame] | 795 | if (ecm->port.in_ep->desc) | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 796 | ecm->port.in_ep->driver_data = NULL; | 
|  | 797 |  | 
|  | 798 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | 
|  | 799 |  | 
|  | 800 | return status; | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | static void | 
|  | 804 | ecm_unbind(struct usb_configuration *c, struct usb_function *f) | 
|  | 805 | { | 
|  | 806 | struct f_ecm		*ecm = func_to_ecm(f); | 
|  | 807 |  | 
|  | 808 | DBG(c->cdev, "ecm unbind\n"); | 
|  | 809 |  | 
| Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 810 | if (gadget_is_superspeed(c->cdev->gadget)) | 
|  | 811 | usb_free_descriptors(f->ss_descriptors); | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 812 | if (gadget_is_dualspeed(c->cdev->gadget)) | 
|  | 813 | usb_free_descriptors(f->hs_descriptors); | 
|  | 814 | usb_free_descriptors(f->descriptors); | 
|  | 815 |  | 
|  | 816 | kfree(ecm->notify_req->buf); | 
|  | 817 | usb_ep_free_request(ecm->notify, ecm->notify_req); | 
|  | 818 |  | 
|  | 819 | ecm_string_defs[1].s = NULL; | 
|  | 820 | kfree(ecm); | 
|  | 821 | } | 
|  | 822 |  | 
|  | 823 | /** | 
|  | 824 | * ecm_bind_config - add CDC Ethernet network link to a configuration | 
|  | 825 | * @c: the configuration to support the network link | 
|  | 826 | * @ethaddr: a buffer in which the ethernet address of the host side | 
|  | 827 | *	side of the link was recorded | 
|  | 828 | * Context: single threaded during gadget setup | 
|  | 829 | * | 
|  | 830 | * Returns zero on success, else negative errno. | 
|  | 831 | * | 
|  | 832 | * Caller must have called @gether_setup().  Caller is also responsible | 
|  | 833 | * for calling @gether_cleanup() before module unload. | 
|  | 834 | */ | 
| Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 835 | int | 
|  | 836 | ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN]) | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 837 | { | 
|  | 838 | struct f_ecm	*ecm; | 
|  | 839 | int		status; | 
|  | 840 |  | 
|  | 841 | if (!can_support_ecm(c->cdev->gadget) || !ethaddr) | 
|  | 842 | return -EINVAL; | 
|  | 843 |  | 
|  | 844 | /* maybe allocate device-global string IDs */ | 
|  | 845 | if (ecm_string_defs[0].id == 0) { | 
|  | 846 |  | 
|  | 847 | /* control interface label */ | 
|  | 848 | status = usb_string_id(c->cdev); | 
|  | 849 | if (status < 0) | 
|  | 850 | return status; | 
|  | 851 | ecm_string_defs[0].id = status; | 
|  | 852 | ecm_control_intf.iInterface = status; | 
|  | 853 |  | 
|  | 854 | /* data interface label */ | 
|  | 855 | status = usb_string_id(c->cdev); | 
|  | 856 | if (status < 0) | 
|  | 857 | return status; | 
|  | 858 | ecm_string_defs[2].id = status; | 
|  | 859 | ecm_data_intf.iInterface = status; | 
|  | 860 |  | 
|  | 861 | /* MAC address */ | 
|  | 862 | status = usb_string_id(c->cdev); | 
|  | 863 | if (status < 0) | 
|  | 864 | return status; | 
|  | 865 | ecm_string_defs[1].id = status; | 
| David Brownell | 33376c1 | 2008-08-18 17:45:07 -0700 | [diff] [blame] | 866 | ecm_desc.iMACAddress = status; | 
| David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 867 | } | 
|  | 868 |  | 
|  | 869 | /* allocate and initialize one new instance */ | 
|  | 870 | ecm = kzalloc(sizeof *ecm, GFP_KERNEL); | 
|  | 871 | if (!ecm) | 
|  | 872 | return -ENOMEM; | 
|  | 873 |  | 
|  | 874 | /* export host's Ethernet address in CDC format */ | 
|  | 875 | snprintf(ecm->ethaddr, sizeof ecm->ethaddr, | 
|  | 876 | "%02X%02X%02X%02X%02X%02X", | 
|  | 877 | ethaddr[0], ethaddr[1], ethaddr[2], | 
|  | 878 | ethaddr[3], ethaddr[4], ethaddr[5]); | 
|  | 879 | ecm_string_defs[1].s = ecm->ethaddr; | 
|  | 880 |  | 
|  | 881 | ecm->port.cdc_filter = DEFAULT_FILTER; | 
|  | 882 |  | 
|  | 883 | ecm->port.func.name = "cdc_ethernet"; | 
|  | 884 | ecm->port.func.strings = ecm_strings; | 
|  | 885 | /* descriptors are per-instance copies */ | 
|  | 886 | ecm->port.func.bind = ecm_bind; | 
|  | 887 | ecm->port.func.unbind = ecm_unbind; | 
|  | 888 | ecm->port.func.set_alt = ecm_set_alt; | 
|  | 889 | ecm->port.func.get_alt = ecm_get_alt; | 
|  | 890 | ecm->port.func.setup = ecm_setup; | 
|  | 891 | ecm->port.func.disable = ecm_disable; | 
|  | 892 |  | 
|  | 893 | status = usb_add_function(c, &ecm->port.func); | 
|  | 894 | if (status) { | 
|  | 895 | ecm_string_defs[1].s = NULL; | 
|  | 896 | kfree(ecm); | 
|  | 897 | } | 
|  | 898 | return status; | 
|  | 899 | } |