Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 1 | /* |
| 2 | * f_eem.c -- USB CDC Ethernet (EEM) link function driver |
| 3 | * |
| 4 | * Copyright (C) 2003-2005,2008 David Brownell |
| 5 | * Copyright (C) 2008 Nokia Corporation |
| 6 | * Copyright (C) 2009 EF Johnson Technologies |
| 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 as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 15 | #include <linux/module.h> |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 16 | #include <linux/device.h> |
| 17 | #include <linux/etherdevice.h> |
| 18 | #include <linux/crc32.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 20 | |
| 21 | #include "u_ether.h" |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 22 | #include "u_eem.h" |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 23 | |
| 24 | #define EEM_HLEN 2 |
| 25 | |
| 26 | /* |
| 27 | * This function is a "CDC Ethernet Emulation Model" (CDC EEM) |
| 28 | * Ethernet link. |
| 29 | */ |
| 30 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 31 | struct f_eem { |
| 32 | struct gether port; |
| 33 | u8 ctrl_id; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static inline struct f_eem *func_to_eem(struct usb_function *f) |
| 37 | { |
| 38 | return container_of(f, struct f_eem, port.func); |
| 39 | } |
| 40 | |
| 41 | /*-------------------------------------------------------------------------*/ |
| 42 | |
| 43 | /* interface descriptor: */ |
| 44 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 45 | static struct usb_interface_descriptor eem_intf = { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 46 | .bLength = sizeof eem_intf, |
| 47 | .bDescriptorType = USB_DT_INTERFACE, |
| 48 | |
| 49 | /* .bInterfaceNumber = DYNAMIC */ |
| 50 | .bNumEndpoints = 2, |
| 51 | .bInterfaceClass = USB_CLASS_COMM, |
| 52 | .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM, |
| 53 | .bInterfaceProtocol = USB_CDC_PROTO_EEM, |
| 54 | /* .iInterface = DYNAMIC */ |
| 55 | }; |
| 56 | |
| 57 | /* full speed support: */ |
| 58 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 59 | static struct usb_endpoint_descriptor eem_fs_in_desc = { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 60 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 61 | .bDescriptorType = USB_DT_ENDPOINT, |
| 62 | |
| 63 | .bEndpointAddress = USB_DIR_IN, |
| 64 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 65 | }; |
| 66 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 67 | static struct usb_endpoint_descriptor eem_fs_out_desc = { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 68 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 69 | .bDescriptorType = USB_DT_ENDPOINT, |
| 70 | |
| 71 | .bEndpointAddress = USB_DIR_OUT, |
| 72 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 73 | }; |
| 74 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 75 | static struct usb_descriptor_header *eem_fs_function[] = { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 76 | /* CDC EEM control descriptors */ |
| 77 | (struct usb_descriptor_header *) &eem_intf, |
| 78 | (struct usb_descriptor_header *) &eem_fs_in_desc, |
| 79 | (struct usb_descriptor_header *) &eem_fs_out_desc, |
| 80 | NULL, |
| 81 | }; |
| 82 | |
| 83 | /* high speed support: */ |
| 84 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 85 | static struct usb_endpoint_descriptor eem_hs_in_desc = { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 86 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 87 | .bDescriptorType = USB_DT_ENDPOINT, |
| 88 | |
| 89 | .bEndpointAddress = USB_DIR_IN, |
| 90 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 91 | .wMaxPacketSize = cpu_to_le16(512), |
| 92 | }; |
| 93 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 94 | static struct usb_endpoint_descriptor eem_hs_out_desc = { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 95 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 96 | .bDescriptorType = USB_DT_ENDPOINT, |
| 97 | |
| 98 | .bEndpointAddress = USB_DIR_OUT, |
| 99 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 100 | .wMaxPacketSize = cpu_to_le16(512), |
| 101 | }; |
| 102 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 103 | static struct usb_descriptor_header *eem_hs_function[] = { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 104 | /* CDC EEM control descriptors */ |
| 105 | (struct usb_descriptor_header *) &eem_intf, |
| 106 | (struct usb_descriptor_header *) &eem_hs_in_desc, |
| 107 | (struct usb_descriptor_header *) &eem_hs_out_desc, |
| 108 | NULL, |
| 109 | }; |
| 110 | |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 111 | /* super speed support: */ |
| 112 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 113 | static struct usb_endpoint_descriptor eem_ss_in_desc = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 114 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 115 | .bDescriptorType = USB_DT_ENDPOINT, |
| 116 | |
| 117 | .bEndpointAddress = USB_DIR_IN, |
| 118 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 119 | .wMaxPacketSize = cpu_to_le16(1024), |
| 120 | }; |
| 121 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 122 | static struct usb_endpoint_descriptor eem_ss_out_desc = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 123 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 124 | .bDescriptorType = USB_DT_ENDPOINT, |
| 125 | |
| 126 | .bEndpointAddress = USB_DIR_OUT, |
| 127 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 128 | .wMaxPacketSize = cpu_to_le16(1024), |
| 129 | }; |
| 130 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 131 | static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 132 | .bLength = sizeof eem_ss_bulk_comp_desc, |
| 133 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, |
| 134 | |
| 135 | /* the following 2 values can be tweaked if necessary */ |
| 136 | /* .bMaxBurst = 0, */ |
| 137 | /* .bmAttributes = 0, */ |
| 138 | }; |
| 139 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 140 | static struct usb_descriptor_header *eem_ss_function[] = { |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 141 | /* CDC EEM control descriptors */ |
| 142 | (struct usb_descriptor_header *) &eem_intf, |
| 143 | (struct usb_descriptor_header *) &eem_ss_in_desc, |
| 144 | (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc, |
| 145 | (struct usb_descriptor_header *) &eem_ss_out_desc, |
| 146 | (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc, |
| 147 | NULL, |
| 148 | }; |
| 149 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 150 | /* string descriptors: */ |
| 151 | |
| 152 | static struct usb_string eem_string_defs[] = { |
| 153 | [0].s = "CDC Ethernet Emulation Model (EEM)", |
| 154 | { } /* end of list */ |
| 155 | }; |
| 156 | |
| 157 | static struct usb_gadget_strings eem_string_table = { |
| 158 | .language = 0x0409, /* en-us */ |
| 159 | .strings = eem_string_defs, |
| 160 | }; |
| 161 | |
| 162 | static struct usb_gadget_strings *eem_strings[] = { |
| 163 | &eem_string_table, |
| 164 | NULL, |
| 165 | }; |
| 166 | |
| 167 | /*-------------------------------------------------------------------------*/ |
| 168 | |
| 169 | static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 170 | { |
| 171 | struct usb_composite_dev *cdev = f->config->cdev; |
| 172 | int value = -EOPNOTSUPP; |
| 173 | u16 w_index = le16_to_cpu(ctrl->wIndex); |
| 174 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 175 | u16 w_length = le16_to_cpu(ctrl->wLength); |
| 176 | |
| 177 | DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", |
| 178 | ctrl->bRequestType, ctrl->bRequest, |
| 179 | w_value, w_index, w_length); |
| 180 | |
| 181 | /* device either stalls (value < 0) or reports success */ |
| 182 | return value; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 187 | { |
| 188 | struct f_eem *eem = func_to_eem(f); |
| 189 | struct usb_composite_dev *cdev = f->config->cdev; |
| 190 | struct net_device *net; |
| 191 | |
| 192 | /* we know alt == 0, so this is an activation or a reset */ |
| 193 | if (alt != 0) |
| 194 | goto fail; |
| 195 | |
| 196 | if (intf == eem->ctrl_id) { |
| 197 | |
| 198 | if (eem->port.in_ep->driver_data) { |
| 199 | DBG(cdev, "reset eem\n"); |
| 200 | gether_disconnect(&eem->port); |
| 201 | } |
| 202 | |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 203 | if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) { |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 204 | DBG(cdev, "init eem\n"); |
Tatyana Brokhman | ea2a1df | 2011-06-28 16:33:50 +0300 | [diff] [blame] | 205 | if (config_ep_by_speed(cdev->gadget, f, |
| 206 | eem->port.in_ep) || |
| 207 | config_ep_by_speed(cdev->gadget, f, |
| 208 | eem->port.out_ep)) { |
| 209 | eem->port.in_ep->desc = NULL; |
| 210 | eem->port.out_ep->desc = NULL; |
| 211 | goto fail; |
| 212 | } |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* zlps should not occur because zero-length EEM packets |
| 216 | * will be inserted in those cases where they would occur |
| 217 | */ |
| 218 | eem->port.is_zlp_ok = 1; |
| 219 | eem->port.cdc_filter = DEFAULT_FILTER; |
| 220 | DBG(cdev, "activate eem\n"); |
| 221 | net = gether_connect(&eem->port); |
| 222 | if (IS_ERR(net)) |
| 223 | return PTR_ERR(net); |
| 224 | } else |
| 225 | goto fail; |
| 226 | |
| 227 | return 0; |
| 228 | fail: |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | static void eem_disable(struct usb_function *f) |
| 233 | { |
| 234 | struct f_eem *eem = func_to_eem(f); |
| 235 | struct usb_composite_dev *cdev = f->config->cdev; |
| 236 | |
| 237 | DBG(cdev, "eem deactivated\n"); |
| 238 | |
| 239 | if (eem->port.in_ep->driver_data) |
| 240 | gether_disconnect(&eem->port); |
| 241 | } |
| 242 | |
| 243 | /*-------------------------------------------------------------------------*/ |
| 244 | |
| 245 | /* EEM function driver setup/binding */ |
| 246 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 247 | static int eem_bind(struct usb_configuration *c, struct usb_function *f) |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 248 | { |
| 249 | struct usb_composite_dev *cdev = c->cdev; |
| 250 | struct f_eem *eem = func_to_eem(f); |
| 251 | int status; |
| 252 | struct usb_ep *ep; |
| 253 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 254 | #ifndef USB_FEEM_INCLUDED |
| 255 | struct f_eem_opts *eem_opts; |
| 256 | |
| 257 | eem_opts = container_of(f->fi, struct f_eem_opts, func_inst); |
| 258 | /* |
| 259 | * in drivers/usb/gadget/configfs.c:configfs_composite_bind() |
| 260 | * configurations are bound in sequence with list_for_each_entry, |
| 261 | * in each configuration its functions are bound in sequence |
| 262 | * with list_for_each_entry, so we assume no race condition |
| 263 | * with regard to eem_opts->bound access |
| 264 | */ |
| 265 | if (!eem_opts->bound) { |
| 266 | gether_set_gadget(eem_opts->net, cdev->gadget); |
| 267 | status = gether_register_netdev(eem_opts->net); |
| 268 | if (status) |
| 269 | return status; |
| 270 | eem_opts->bound = true; |
| 271 | } |
| 272 | #endif |
| 273 | |
| 274 | /* maybe allocate device-global string IDs */ |
| 275 | if (eem_string_defs[0].id == 0) { |
| 276 | |
| 277 | /* control interface label */ |
| 278 | status = usb_string_id(c->cdev); |
| 279 | if (status < 0) |
| 280 | return status; |
| 281 | eem_string_defs[0].id = status; |
| 282 | eem_intf.iInterface = status; |
| 283 | } |
| 284 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 285 | /* allocate instance-specific interface IDs */ |
| 286 | status = usb_interface_id(c, f); |
| 287 | if (status < 0) |
| 288 | goto fail; |
| 289 | eem->ctrl_id = status; |
| 290 | eem_intf.bInterfaceNumber = status; |
| 291 | |
| 292 | status = -ENODEV; |
| 293 | |
| 294 | /* allocate instance-specific endpoints */ |
| 295 | ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc); |
| 296 | if (!ep) |
| 297 | goto fail; |
| 298 | eem->port.in_ep = ep; |
| 299 | ep->driver_data = cdev; /* claim */ |
| 300 | |
| 301 | ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc); |
| 302 | if (!ep) |
| 303 | goto fail; |
| 304 | eem->port.out_ep = ep; |
| 305 | ep->driver_data = cdev; /* claim */ |
| 306 | |
| 307 | status = -ENOMEM; |
| 308 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 309 | /* support all relevant hardware speeds... we expect that when |
| 310 | * hardware is dual speed, all bulk-capable endpoints work at |
| 311 | * both speeds |
| 312 | */ |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 313 | eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress; |
| 314 | eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 315 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 316 | eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress; |
| 317 | eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 318 | |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 319 | status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function, |
| 320 | eem_ss_function); |
| 321 | if (status) |
| 322 | goto fail; |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 323 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 324 | DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n", |
Paul Zimmerman | 04617db | 2011-06-27 14:13:18 -0700 | [diff] [blame] | 325 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 326 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
| 327 | eem->port.in_ep->name, eem->port.out_ep->name); |
| 328 | return 0; |
| 329 | |
| 330 | fail: |
Sebastian Andrzej Siewior | 10287ba | 2012-10-22 22:15:06 +0200 | [diff] [blame] | 331 | usb_free_all_descriptors(f); |
Sebastian Andrzej Siewior | e79cc61 | 2012-10-22 22:15:00 +0200 | [diff] [blame] | 332 | if (eem->port.out_ep) |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 333 | eem->port.out_ep->driver_data = NULL; |
Sebastian Andrzej Siewior | e79cc61 | 2012-10-22 22:15:00 +0200 | [diff] [blame] | 334 | if (eem->port.in_ep) |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 335 | eem->port.in_ep->driver_data = NULL; |
| 336 | |
| 337 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); |
| 338 | |
| 339 | return status; |
| 340 | } |
| 341 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 342 | static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req) |
| 343 | { |
Yauheni Kaliuta | 505d1f6 | 2011-04-05 16:55:25 +0300 | [diff] [blame] | 344 | struct sk_buff *skb = (struct sk_buff *)req->context; |
| 345 | |
| 346 | dev_kfree_skb_any(skb); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Add the EEM header and ethernet checksum. |
| 351 | * We currently do not attempt to put multiple ethernet frames |
| 352 | * into a single USB transfer |
| 353 | */ |
| 354 | static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb) |
| 355 | { |
| 356 | struct sk_buff *skb2 = NULL; |
| 357 | struct usb_ep *in = port->in_ep; |
| 358 | int padlen = 0; |
| 359 | u16 len = skb->len; |
| 360 | |
| 361 | if (!skb_cloned(skb)) { |
| 362 | int headroom = skb_headroom(skb); |
| 363 | int tailroom = skb_tailroom(skb); |
| 364 | |
| 365 | /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0, |
| 366 | * stick two bytes of zero-length EEM packet on the end. |
| 367 | */ |
| 368 | if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0) |
| 369 | padlen += 2; |
| 370 | |
| 371 | if ((tailroom >= (ETH_FCS_LEN + padlen)) && |
| 372 | (headroom >= EEM_HLEN)) |
| 373 | goto done; |
| 374 | } |
| 375 | |
| 376 | skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC); |
| 377 | dev_kfree_skb_any(skb); |
| 378 | skb = skb2; |
| 379 | if (!skb) |
| 380 | return skb; |
| 381 | |
| 382 | done: |
| 383 | /* use the "no CRC" option */ |
| 384 | put_unaligned_be32(0xdeadbeef, skb_put(skb, 4)); |
| 385 | |
| 386 | /* EEM packet header format: |
| 387 | * b0..13: length of ethernet frame |
| 388 | * b14: bmCRC (0 == sentinel CRC) |
| 389 | * b15: bmType (0 == data) |
| 390 | */ |
| 391 | len = skb->len; |
Brian Niebuhr | 31e5d4a | 2010-01-25 14:45:40 -0600 | [diff] [blame] | 392 | put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2)); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 393 | |
| 394 | /* add a zero-length EEM packet, if needed */ |
| 395 | if (padlen) |
| 396 | put_unaligned_le16(0, skb_put(skb, 2)); |
| 397 | |
| 398 | return skb; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Remove the EEM header. Note that there can be many EEM packets in a single |
| 403 | * USB transfer, so we need to break them out and handle them independently. |
| 404 | */ |
| 405 | static int eem_unwrap(struct gether *port, |
| 406 | struct sk_buff *skb, |
| 407 | struct sk_buff_head *list) |
| 408 | { |
| 409 | struct usb_composite_dev *cdev = port->func.config->cdev; |
| 410 | int status = 0; |
| 411 | |
| 412 | do { |
| 413 | struct sk_buff *skb2; |
| 414 | u16 header; |
| 415 | u16 len = 0; |
| 416 | |
| 417 | if (skb->len < EEM_HLEN) { |
| 418 | status = -EINVAL; |
| 419 | DBG(cdev, "invalid EEM header\n"); |
| 420 | goto error; |
| 421 | } |
| 422 | |
| 423 | /* remove the EEM header */ |
| 424 | header = get_unaligned_le16(skb->data); |
| 425 | skb_pull(skb, EEM_HLEN); |
| 426 | |
| 427 | /* EEM packet header format: |
| 428 | * b0..14: EEM type dependent (data or command) |
| 429 | * b15: bmType (0 == data, 1 == command) |
| 430 | */ |
| 431 | if (header & BIT(15)) { |
| 432 | struct usb_request *req = cdev->req; |
| 433 | u16 bmEEMCmd; |
| 434 | |
| 435 | /* EEM command packet format: |
| 436 | * b0..10: bmEEMCmdParam |
| 437 | * b11..13: bmEEMCmd |
| 438 | * b14: reserved (must be zero) |
| 439 | * b15: bmType (1 == command) |
| 440 | */ |
| 441 | if (header & BIT(14)) |
| 442 | continue; |
| 443 | |
| 444 | bmEEMCmd = (header >> 11) & 0x7; |
| 445 | switch (bmEEMCmd) { |
| 446 | case 0: /* echo */ |
| 447 | len = header & 0x7FF; |
| 448 | if (skb->len < len) { |
| 449 | status = -EOVERFLOW; |
| 450 | goto error; |
| 451 | } |
| 452 | |
| 453 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 454 | if (unlikely(!skb2)) { |
| 455 | DBG(cdev, "EEM echo response error\n"); |
| 456 | goto next; |
| 457 | } |
| 458 | skb_trim(skb2, len); |
| 459 | put_unaligned_le16(BIT(15) | BIT(11) | len, |
| 460 | skb_push(skb2, 2)); |
Yauheni Kaliuta | 505d1f6 | 2011-04-05 16:55:25 +0300 | [diff] [blame] | 461 | skb_copy_bits(skb2, 0, req->buf, skb2->len); |
| 462 | req->length = skb2->len; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 463 | req->complete = eem_cmd_complete; |
| 464 | req->zero = 1; |
Yauheni Kaliuta | 505d1f6 | 2011-04-05 16:55:25 +0300 | [diff] [blame] | 465 | req->context = skb2; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 466 | if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC)) |
| 467 | DBG(cdev, "echo response queue fail\n"); |
| 468 | break; |
| 469 | |
| 470 | case 1: /* echo response */ |
| 471 | case 2: /* suspend hint */ |
| 472 | case 3: /* response hint */ |
| 473 | case 4: /* response complete hint */ |
| 474 | case 5: /* tickle */ |
| 475 | default: /* reserved */ |
| 476 | continue; |
| 477 | } |
| 478 | } else { |
| 479 | u32 crc, crc2; |
| 480 | struct sk_buff *skb3; |
| 481 | |
| 482 | /* check for zero-length EEM packet */ |
| 483 | if (header == 0) |
| 484 | continue; |
| 485 | |
| 486 | /* EEM data packet format: |
| 487 | * b0..13: length of ethernet frame |
| 488 | * b14: bmCRC (0 == sentinel, 1 == calculated) |
| 489 | * b15: bmType (0 == data) |
| 490 | */ |
| 491 | len = header & 0x3FFF; |
| 492 | if ((skb->len < len) |
| 493 | || (len < (ETH_HLEN + ETH_FCS_LEN))) { |
| 494 | status = -EINVAL; |
| 495 | goto error; |
| 496 | } |
| 497 | |
| 498 | /* validate CRC */ |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 499 | if (header & BIT(14)) { |
| 500 | crc = get_unaligned_le32(skb->data + len |
| 501 | - ETH_FCS_LEN); |
| 502 | crc2 = ~crc32_le(~0, |
Jiri Pinkava | 03ab746 | 2010-06-20 20:05:52 +0200 | [diff] [blame] | 503 | skb->data, len - ETH_FCS_LEN); |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 504 | } else { |
| 505 | crc = get_unaligned_be32(skb->data + len |
| 506 | - ETH_FCS_LEN); |
| 507 | crc2 = 0xdeadbeef; |
| 508 | } |
| 509 | if (crc != crc2) { |
| 510 | DBG(cdev, "invalid EEM CRC\n"); |
| 511 | goto next; |
| 512 | } |
| 513 | |
| 514 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 515 | if (unlikely(!skb2)) { |
| 516 | DBG(cdev, "unable to unframe EEM packet\n"); |
| 517 | continue; |
| 518 | } |
| 519 | skb_trim(skb2, len - ETH_FCS_LEN); |
| 520 | |
| 521 | skb3 = skb_copy_expand(skb2, |
| 522 | NET_IP_ALIGN, |
| 523 | 0, |
| 524 | GFP_ATOMIC); |
| 525 | if (unlikely(!skb3)) { |
| 526 | DBG(cdev, "unable to realign EEM packet\n"); |
| 527 | dev_kfree_skb_any(skb2); |
| 528 | continue; |
| 529 | } |
| 530 | dev_kfree_skb_any(skb2); |
| 531 | skb_queue_tail(list, skb3); |
| 532 | } |
| 533 | next: |
| 534 | skb_pull(skb, len); |
| 535 | } while (skb->len); |
| 536 | |
| 537 | error: |
| 538 | dev_kfree_skb_any(skb); |
| 539 | return status; |
| 540 | } |
| 541 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 542 | #ifdef USB_FEEM_INCLUDED |
| 543 | |
| 544 | static void eem_old_unbind(struct usb_configuration *c, struct usb_function *f) |
| 545 | { |
| 546 | struct f_eem *eem = func_to_eem(f); |
| 547 | |
| 548 | DBG(c->cdev, "eem unbind\n"); |
| 549 | |
| 550 | usb_free_all_descriptors(f); |
| 551 | kfree(eem); |
| 552 | } |
| 553 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 554 | /** |
| 555 | * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration |
| 556 | * @c: the configuration to support the network link |
| 557 | * Context: single threaded during gadget setup |
| 558 | * |
| 559 | * Returns zero on success, else negative errno. |
| 560 | * |
| 561 | * Caller must have called @gether_setup(). Caller is also responsible |
| 562 | * for calling @gether_cleanup() before module unload. |
| 563 | */ |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 564 | int __init eem_bind_config(struct usb_configuration *c, struct eth_dev *dev) |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 565 | { |
| 566 | struct f_eem *eem; |
| 567 | int status; |
| 568 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 569 | /* allocate and initialize one new instance */ |
| 570 | eem = kzalloc(sizeof *eem, GFP_KERNEL); |
| 571 | if (!eem) |
| 572 | return -ENOMEM; |
| 573 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 574 | eem->port.ioport = dev; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 575 | eem->port.cdc_filter = DEFAULT_FILTER; |
| 576 | |
| 577 | eem->port.func.name = "cdc_eem"; |
| 578 | eem->port.func.strings = eem_strings; |
| 579 | /* descriptors are per-instance copies */ |
| 580 | eem->port.func.bind = eem_bind; |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 581 | eem->port.func.unbind = eem_old_unbind; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 582 | eem->port.func.set_alt = eem_set_alt; |
| 583 | eem->port.func.setup = eem_setup; |
| 584 | eem->port.func.disable = eem_disable; |
| 585 | eem->port.wrap = eem_wrap; |
| 586 | eem->port.unwrap = eem_unwrap; |
| 587 | eem->port.header_len = EEM_HLEN; |
| 588 | |
| 589 | status = usb_add_function(c, &eem->port.func); |
| 590 | if (status) |
| 591 | kfree(eem); |
| 592 | return status; |
| 593 | } |
| 594 | |
Andrzej Pietrasiewicz | b29002a | 2013-05-28 09:15:47 +0200 | [diff] [blame^] | 595 | #else |
| 596 | |
| 597 | static void eem_free_inst(struct usb_function_instance *f) |
| 598 | { |
| 599 | struct f_eem_opts *opts; |
| 600 | |
| 601 | opts = container_of(f, struct f_eem_opts, func_inst); |
| 602 | if (opts->bound) |
| 603 | gether_cleanup(netdev_priv(opts->net)); |
| 604 | else |
| 605 | free_netdev(opts->net); |
| 606 | kfree(opts); |
| 607 | } |
| 608 | |
| 609 | static struct usb_function_instance *eem_alloc_inst(void) |
| 610 | { |
| 611 | struct f_eem_opts *opts; |
| 612 | |
| 613 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); |
| 614 | if (!opts) |
| 615 | return ERR_PTR(-ENOMEM); |
| 616 | opts->func_inst.free_func_inst = eem_free_inst; |
| 617 | opts->net = gether_setup_default(); |
| 618 | if (IS_ERR(opts->net)) |
| 619 | return ERR_CAST(opts->net); |
| 620 | |
| 621 | return &opts->func_inst; |
| 622 | } |
| 623 | |
| 624 | static void eem_free(struct usb_function *f) |
| 625 | { |
| 626 | struct f_eem *eem; |
| 627 | |
| 628 | eem = func_to_eem(f); |
| 629 | kfree(eem); |
| 630 | } |
| 631 | |
| 632 | static void eem_unbind(struct usb_configuration *c, struct usb_function *f) |
| 633 | { |
| 634 | DBG(c->cdev, "eem unbind\n"); |
| 635 | |
| 636 | usb_free_all_descriptors(f); |
| 637 | } |
| 638 | |
| 639 | struct usb_function *eem_alloc(struct usb_function_instance *fi) |
| 640 | { |
| 641 | struct f_eem *eem; |
| 642 | struct f_eem_opts *opts; |
| 643 | |
| 644 | /* allocate and initialize one new instance */ |
| 645 | eem = kzalloc(sizeof(*eem), GFP_KERNEL); |
| 646 | if (!eem) |
| 647 | return ERR_PTR(-ENOMEM); |
| 648 | |
| 649 | opts = container_of(fi, struct f_eem_opts, func_inst); |
| 650 | |
| 651 | eem->port.ioport = netdev_priv(opts->net); |
| 652 | eem->port.cdc_filter = DEFAULT_FILTER; |
| 653 | |
| 654 | eem->port.func.name = "cdc_eem"; |
| 655 | eem->port.func.strings = eem_strings; |
| 656 | /* descriptors are per-instance copies */ |
| 657 | eem->port.func.bind = eem_bind; |
| 658 | eem->port.func.unbind = eem_unbind; |
| 659 | eem->port.func.set_alt = eem_set_alt; |
| 660 | eem->port.func.setup = eem_setup; |
| 661 | eem->port.func.disable = eem_disable; |
| 662 | eem->port.func.free_func = eem_free; |
| 663 | eem->port.wrap = eem_wrap; |
| 664 | eem->port.unwrap = eem_unwrap; |
| 665 | eem->port.header_len = EEM_HLEN; |
| 666 | |
| 667 | return &eem->port.func; |
| 668 | } |
| 669 | |
| 670 | DECLARE_USB_FUNCTION_INIT(eem, eem_alloc_inst, eem_alloc); |
| 671 | MODULE_LICENSE("GPL"); |
| 672 | MODULE_AUTHOR("David Brownell"); |
| 673 | |
| 674 | #endif |