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