Commit | Line | Data |
---|---|---|
9b39e9dd BN |
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. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | */ | |
22 | ||
23 | #include <linux/kernel.h> | |
24 | #include <linux/device.h> | |
25 | #include <linux/etherdevice.h> | |
26 | #include <linux/crc32.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
9b39e9dd BN |
28 | |
29 | #include "u_ether.h" | |
30 | ||
31 | #define EEM_HLEN 2 | |
32 | ||
33 | /* | |
34 | * This function is a "CDC Ethernet Emulation Model" (CDC EEM) | |
35 | * Ethernet link. | |
36 | */ | |
37 | ||
9b39e9dd BN |
38 | struct f_eem { |
39 | struct gether port; | |
40 | u8 ctrl_id; | |
9b39e9dd BN |
41 | }; |
42 | ||
43 | static inline struct f_eem *func_to_eem(struct usb_function *f) | |
44 | { | |
45 | return container_of(f, struct f_eem, port.func); | |
46 | } | |
47 | ||
48 | /*-------------------------------------------------------------------------*/ | |
49 | ||
50 | /* interface descriptor: */ | |
51 | ||
52 | static struct usb_interface_descriptor eem_intf __initdata = { | |
53 | .bLength = sizeof eem_intf, | |
54 | .bDescriptorType = USB_DT_INTERFACE, | |
55 | ||
56 | /* .bInterfaceNumber = DYNAMIC */ | |
57 | .bNumEndpoints = 2, | |
58 | .bInterfaceClass = USB_CLASS_COMM, | |
59 | .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM, | |
60 | .bInterfaceProtocol = USB_CDC_PROTO_EEM, | |
61 | /* .iInterface = DYNAMIC */ | |
62 | }; | |
63 | ||
64 | /* full speed support: */ | |
65 | ||
66 | static struct usb_endpoint_descriptor eem_fs_in_desc __initdata = { | |
67 | .bLength = USB_DT_ENDPOINT_SIZE, | |
68 | .bDescriptorType = USB_DT_ENDPOINT, | |
69 | ||
70 | .bEndpointAddress = USB_DIR_IN, | |
71 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
72 | }; | |
73 | ||
74 | static struct usb_endpoint_descriptor eem_fs_out_desc __initdata = { | |
75 | .bLength = USB_DT_ENDPOINT_SIZE, | |
76 | .bDescriptorType = USB_DT_ENDPOINT, | |
77 | ||
78 | .bEndpointAddress = USB_DIR_OUT, | |
79 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
80 | }; | |
81 | ||
82 | static struct usb_descriptor_header *eem_fs_function[] __initdata = { | |
83 | /* CDC EEM control descriptors */ | |
84 | (struct usb_descriptor_header *) &eem_intf, | |
85 | (struct usb_descriptor_header *) &eem_fs_in_desc, | |
86 | (struct usb_descriptor_header *) &eem_fs_out_desc, | |
87 | NULL, | |
88 | }; | |
89 | ||
90 | /* high speed support: */ | |
91 | ||
92 | static struct usb_endpoint_descriptor eem_hs_in_desc __initdata = { | |
93 | .bLength = USB_DT_ENDPOINT_SIZE, | |
94 | .bDescriptorType = USB_DT_ENDPOINT, | |
95 | ||
96 | .bEndpointAddress = USB_DIR_IN, | |
97 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
98 | .wMaxPacketSize = cpu_to_le16(512), | |
99 | }; | |
100 | ||
101 | static struct usb_endpoint_descriptor eem_hs_out_desc __initdata = { | |
102 | .bLength = USB_DT_ENDPOINT_SIZE, | |
103 | .bDescriptorType = USB_DT_ENDPOINT, | |
104 | ||
105 | .bEndpointAddress = USB_DIR_OUT, | |
106 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
107 | .wMaxPacketSize = cpu_to_le16(512), | |
108 | }; | |
109 | ||
110 | static struct usb_descriptor_header *eem_hs_function[] __initdata = { | |
111 | /* CDC EEM control descriptors */ | |
112 | (struct usb_descriptor_header *) &eem_intf, | |
113 | (struct usb_descriptor_header *) &eem_hs_in_desc, | |
114 | (struct usb_descriptor_header *) &eem_hs_out_desc, | |
115 | NULL, | |
116 | }; | |
117 | ||
118 | /* string descriptors: */ | |
119 | ||
120 | static struct usb_string eem_string_defs[] = { | |
121 | [0].s = "CDC Ethernet Emulation Model (EEM)", | |
122 | { } /* end of list */ | |
123 | }; | |
124 | ||
125 | static struct usb_gadget_strings eem_string_table = { | |
126 | .language = 0x0409, /* en-us */ | |
127 | .strings = eem_string_defs, | |
128 | }; | |
129 | ||
130 | static struct usb_gadget_strings *eem_strings[] = { | |
131 | &eem_string_table, | |
132 | NULL, | |
133 | }; | |
134 | ||
135 | /*-------------------------------------------------------------------------*/ | |
136 | ||
137 | static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | |
138 | { | |
139 | struct usb_composite_dev *cdev = f->config->cdev; | |
140 | int value = -EOPNOTSUPP; | |
141 | u16 w_index = le16_to_cpu(ctrl->wIndex); | |
142 | u16 w_value = le16_to_cpu(ctrl->wValue); | |
143 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
144 | ||
145 | DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", | |
146 | ctrl->bRequestType, ctrl->bRequest, | |
147 | w_value, w_index, w_length); | |
148 | ||
149 | /* device either stalls (value < 0) or reports success */ | |
150 | return value; | |
151 | } | |
152 | ||
153 | ||
154 | static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | |
155 | { | |
156 | struct f_eem *eem = func_to_eem(f); | |
157 | struct usb_composite_dev *cdev = f->config->cdev; | |
158 | struct net_device *net; | |
159 | ||
160 | /* we know alt == 0, so this is an activation or a reset */ | |
161 | if (alt != 0) | |
162 | goto fail; | |
163 | ||
164 | if (intf == eem->ctrl_id) { | |
165 | ||
166 | if (eem->port.in_ep->driver_data) { | |
167 | DBG(cdev, "reset eem\n"); | |
168 | gether_disconnect(&eem->port); | |
169 | } | |
170 | ||
ea2a1df7 | 171 | if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) { |
9b39e9dd | 172 | DBG(cdev, "init eem\n"); |
ea2a1df7 TB |
173 | if (config_ep_by_speed(cdev->gadget, f, |
174 | eem->port.in_ep) || | |
175 | config_ep_by_speed(cdev->gadget, f, | |
176 | eem->port.out_ep)) { | |
177 | eem->port.in_ep->desc = NULL; | |
178 | eem->port.out_ep->desc = NULL; | |
179 | goto fail; | |
180 | } | |
9b39e9dd BN |
181 | } |
182 | ||
183 | /* zlps should not occur because zero-length EEM packets | |
184 | * will be inserted in those cases where they would occur | |
185 | */ | |
186 | eem->port.is_zlp_ok = 1; | |
187 | eem->port.cdc_filter = DEFAULT_FILTER; | |
188 | DBG(cdev, "activate eem\n"); | |
189 | net = gether_connect(&eem->port); | |
190 | if (IS_ERR(net)) | |
191 | return PTR_ERR(net); | |
192 | } else | |
193 | goto fail; | |
194 | ||
195 | return 0; | |
196 | fail: | |
197 | return -EINVAL; | |
198 | } | |
199 | ||
200 | static void eem_disable(struct usb_function *f) | |
201 | { | |
202 | struct f_eem *eem = func_to_eem(f); | |
203 | struct usb_composite_dev *cdev = f->config->cdev; | |
204 | ||
205 | DBG(cdev, "eem deactivated\n"); | |
206 | ||
207 | if (eem->port.in_ep->driver_data) | |
208 | gether_disconnect(&eem->port); | |
209 | } | |
210 | ||
211 | /*-------------------------------------------------------------------------*/ | |
212 | ||
213 | /* EEM function driver setup/binding */ | |
214 | ||
215 | static int __init | |
216 | eem_bind(struct usb_configuration *c, struct usb_function *f) | |
217 | { | |
218 | struct usb_composite_dev *cdev = c->cdev; | |
219 | struct f_eem *eem = func_to_eem(f); | |
220 | int status; | |
221 | struct usb_ep *ep; | |
222 | ||
223 | /* allocate instance-specific interface IDs */ | |
224 | status = usb_interface_id(c, f); | |
225 | if (status < 0) | |
226 | goto fail; | |
227 | eem->ctrl_id = status; | |
228 | eem_intf.bInterfaceNumber = status; | |
229 | ||
230 | status = -ENODEV; | |
231 | ||
232 | /* allocate instance-specific endpoints */ | |
233 | ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc); | |
234 | if (!ep) | |
235 | goto fail; | |
236 | eem->port.in_ep = ep; | |
237 | ep->driver_data = cdev; /* claim */ | |
238 | ||
239 | ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc); | |
240 | if (!ep) | |
241 | goto fail; | |
242 | eem->port.out_ep = ep; | |
243 | ep->driver_data = cdev; /* claim */ | |
244 | ||
245 | status = -ENOMEM; | |
246 | ||
247 | /* copy descriptors, and track endpoint copies */ | |
248 | f->descriptors = usb_copy_descriptors(eem_fs_function); | |
249 | if (!f->descriptors) | |
250 | goto fail; | |
251 | ||
9b39e9dd BN |
252 | /* support all relevant hardware speeds... we expect that when |
253 | * hardware is dual speed, all bulk-capable endpoints work at | |
254 | * both speeds | |
255 | */ | |
256 | if (gadget_is_dualspeed(c->cdev->gadget)) { | |
257 | eem_hs_in_desc.bEndpointAddress = | |
258 | eem_fs_in_desc.bEndpointAddress; | |
259 | eem_hs_out_desc.bEndpointAddress = | |
260 | eem_fs_out_desc.bEndpointAddress; | |
261 | ||
262 | /* copy descriptors, and track endpoint copies */ | |
263 | f->hs_descriptors = usb_copy_descriptors(eem_hs_function); | |
264 | if (!f->hs_descriptors) | |
265 | goto fail; | |
9b39e9dd BN |
266 | } |
267 | ||
268 | DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n", | |
269 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | |
270 | eem->port.in_ep->name, eem->port.out_ep->name); | |
271 | return 0; | |
272 | ||
273 | fail: | |
274 | if (f->descriptors) | |
275 | usb_free_descriptors(f->descriptors); | |
276 | ||
277 | /* we might as well release our claims on endpoints */ | |
72c973dd | 278 | if (eem->port.out_ep->desc) |
9b39e9dd | 279 | eem->port.out_ep->driver_data = NULL; |
72c973dd | 280 | if (eem->port.in_ep->desc) |
9b39e9dd BN |
281 | eem->port.in_ep->driver_data = NULL; |
282 | ||
283 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | |
284 | ||
285 | return status; | |
286 | } | |
287 | ||
288 | static void | |
289 | eem_unbind(struct usb_configuration *c, struct usb_function *f) | |
290 | { | |
291 | struct f_eem *eem = func_to_eem(f); | |
292 | ||
293 | DBG(c->cdev, "eem unbind\n"); | |
294 | ||
295 | if (gadget_is_dualspeed(c->cdev->gadget)) | |
296 | usb_free_descriptors(f->hs_descriptors); | |
297 | usb_free_descriptors(f->descriptors); | |
298 | kfree(eem); | |
299 | } | |
300 | ||
301 | static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req) | |
302 | { | |
505d1f69 YK |
303 | struct sk_buff *skb = (struct sk_buff *)req->context; |
304 | ||
305 | dev_kfree_skb_any(skb); | |
9b39e9dd BN |
306 | } |
307 | ||
308 | /* | |
309 | * Add the EEM header and ethernet checksum. | |
310 | * We currently do not attempt to put multiple ethernet frames | |
311 | * into a single USB transfer | |
312 | */ | |
313 | static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb) | |
314 | { | |
315 | struct sk_buff *skb2 = NULL; | |
316 | struct usb_ep *in = port->in_ep; | |
317 | int padlen = 0; | |
318 | u16 len = skb->len; | |
319 | ||
320 | if (!skb_cloned(skb)) { | |
321 | int headroom = skb_headroom(skb); | |
322 | int tailroom = skb_tailroom(skb); | |
323 | ||
324 | /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0, | |
325 | * stick two bytes of zero-length EEM packet on the end. | |
326 | */ | |
327 | if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0) | |
328 | padlen += 2; | |
329 | ||
330 | if ((tailroom >= (ETH_FCS_LEN + padlen)) && | |
331 | (headroom >= EEM_HLEN)) | |
332 | goto done; | |
333 | } | |
334 | ||
335 | skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC); | |
336 | dev_kfree_skb_any(skb); | |
337 | skb = skb2; | |
338 | if (!skb) | |
339 | return skb; | |
340 | ||
341 | done: | |
342 | /* use the "no CRC" option */ | |
343 | put_unaligned_be32(0xdeadbeef, skb_put(skb, 4)); | |
344 | ||
345 | /* EEM packet header format: | |
346 | * b0..13: length of ethernet frame | |
347 | * b14: bmCRC (0 == sentinel CRC) | |
348 | * b15: bmType (0 == data) | |
349 | */ | |
350 | len = skb->len; | |
31e5d4ab | 351 | put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2)); |
9b39e9dd BN |
352 | |
353 | /* add a zero-length EEM packet, if needed */ | |
354 | if (padlen) | |
355 | put_unaligned_le16(0, skb_put(skb, 2)); | |
356 | ||
357 | return skb; | |
358 | } | |
359 | ||
360 | /* | |
361 | * Remove the EEM header. Note that there can be many EEM packets in a single | |
362 | * USB transfer, so we need to break them out and handle them independently. | |
363 | */ | |
364 | static int eem_unwrap(struct gether *port, | |
365 | struct sk_buff *skb, | |
366 | struct sk_buff_head *list) | |
367 | { | |
368 | struct usb_composite_dev *cdev = port->func.config->cdev; | |
369 | int status = 0; | |
370 | ||
371 | do { | |
372 | struct sk_buff *skb2; | |
373 | u16 header; | |
374 | u16 len = 0; | |
375 | ||
376 | if (skb->len < EEM_HLEN) { | |
377 | status = -EINVAL; | |
378 | DBG(cdev, "invalid EEM header\n"); | |
379 | goto error; | |
380 | } | |
381 | ||
382 | /* remove the EEM header */ | |
383 | header = get_unaligned_le16(skb->data); | |
384 | skb_pull(skb, EEM_HLEN); | |
385 | ||
386 | /* EEM packet header format: | |
387 | * b0..14: EEM type dependent (data or command) | |
388 | * b15: bmType (0 == data, 1 == command) | |
389 | */ | |
390 | if (header & BIT(15)) { | |
391 | struct usb_request *req = cdev->req; | |
392 | u16 bmEEMCmd; | |
393 | ||
394 | /* EEM command packet format: | |
395 | * b0..10: bmEEMCmdParam | |
396 | * b11..13: bmEEMCmd | |
397 | * b14: reserved (must be zero) | |
398 | * b15: bmType (1 == command) | |
399 | */ | |
400 | if (header & BIT(14)) | |
401 | continue; | |
402 | ||
403 | bmEEMCmd = (header >> 11) & 0x7; | |
404 | switch (bmEEMCmd) { | |
405 | case 0: /* echo */ | |
406 | len = header & 0x7FF; | |
407 | if (skb->len < len) { | |
408 | status = -EOVERFLOW; | |
409 | goto error; | |
410 | } | |
411 | ||
412 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
413 | if (unlikely(!skb2)) { | |
414 | DBG(cdev, "EEM echo response error\n"); | |
415 | goto next; | |
416 | } | |
417 | skb_trim(skb2, len); | |
418 | put_unaligned_le16(BIT(15) | BIT(11) | len, | |
419 | skb_push(skb2, 2)); | |
505d1f69 YK |
420 | skb_copy_bits(skb2, 0, req->buf, skb2->len); |
421 | req->length = skb2->len; | |
9b39e9dd BN |
422 | req->complete = eem_cmd_complete; |
423 | req->zero = 1; | |
505d1f69 | 424 | req->context = skb2; |
9b39e9dd BN |
425 | if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC)) |
426 | DBG(cdev, "echo response queue fail\n"); | |
427 | break; | |
428 | ||
429 | case 1: /* echo response */ | |
430 | case 2: /* suspend hint */ | |
431 | case 3: /* response hint */ | |
432 | case 4: /* response complete hint */ | |
433 | case 5: /* tickle */ | |
434 | default: /* reserved */ | |
435 | continue; | |
436 | } | |
437 | } else { | |
438 | u32 crc, crc2; | |
439 | struct sk_buff *skb3; | |
440 | ||
441 | /* check for zero-length EEM packet */ | |
442 | if (header == 0) | |
443 | continue; | |
444 | ||
445 | /* EEM data packet format: | |
446 | * b0..13: length of ethernet frame | |
447 | * b14: bmCRC (0 == sentinel, 1 == calculated) | |
448 | * b15: bmType (0 == data) | |
449 | */ | |
450 | len = header & 0x3FFF; | |
451 | if ((skb->len < len) | |
452 | || (len < (ETH_HLEN + ETH_FCS_LEN))) { | |
453 | status = -EINVAL; | |
454 | goto error; | |
455 | } | |
456 | ||
457 | /* validate CRC */ | |
9b39e9dd BN |
458 | if (header & BIT(14)) { |
459 | crc = get_unaligned_le32(skb->data + len | |
460 | - ETH_FCS_LEN); | |
461 | crc2 = ~crc32_le(~0, | |
03ab7461 | 462 | skb->data, len - ETH_FCS_LEN); |
9b39e9dd BN |
463 | } else { |
464 | crc = get_unaligned_be32(skb->data + len | |
465 | - ETH_FCS_LEN); | |
466 | crc2 = 0xdeadbeef; | |
467 | } | |
468 | if (crc != crc2) { | |
469 | DBG(cdev, "invalid EEM CRC\n"); | |
470 | goto next; | |
471 | } | |
472 | ||
473 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
474 | if (unlikely(!skb2)) { | |
475 | DBG(cdev, "unable to unframe EEM packet\n"); | |
476 | continue; | |
477 | } | |
478 | skb_trim(skb2, len - ETH_FCS_LEN); | |
479 | ||
480 | skb3 = skb_copy_expand(skb2, | |
481 | NET_IP_ALIGN, | |
482 | 0, | |
483 | GFP_ATOMIC); | |
484 | if (unlikely(!skb3)) { | |
485 | DBG(cdev, "unable to realign EEM packet\n"); | |
486 | dev_kfree_skb_any(skb2); | |
487 | continue; | |
488 | } | |
489 | dev_kfree_skb_any(skb2); | |
490 | skb_queue_tail(list, skb3); | |
491 | } | |
492 | next: | |
493 | skb_pull(skb, len); | |
494 | } while (skb->len); | |
495 | ||
496 | error: | |
497 | dev_kfree_skb_any(skb); | |
498 | return status; | |
499 | } | |
500 | ||
501 | /** | |
502 | * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration | |
503 | * @c: the configuration to support the network link | |
504 | * Context: single threaded during gadget setup | |
505 | * | |
506 | * Returns zero on success, else negative errno. | |
507 | * | |
508 | * Caller must have called @gether_setup(). Caller is also responsible | |
509 | * for calling @gether_cleanup() before module unload. | |
510 | */ | |
511 | int __init eem_bind_config(struct usb_configuration *c) | |
512 | { | |
513 | struct f_eem *eem; | |
514 | int status; | |
515 | ||
516 | /* maybe allocate device-global string IDs */ | |
517 | if (eem_string_defs[0].id == 0) { | |
518 | ||
519 | /* control interface label */ | |
520 | status = usb_string_id(c->cdev); | |
521 | if (status < 0) | |
522 | return status; | |
523 | eem_string_defs[0].id = status; | |
524 | eem_intf.iInterface = status; | |
525 | } | |
526 | ||
527 | /* allocate and initialize one new instance */ | |
528 | eem = kzalloc(sizeof *eem, GFP_KERNEL); | |
529 | if (!eem) | |
530 | return -ENOMEM; | |
531 | ||
532 | eem->port.cdc_filter = DEFAULT_FILTER; | |
533 | ||
534 | eem->port.func.name = "cdc_eem"; | |
535 | eem->port.func.strings = eem_strings; | |
536 | /* descriptors are per-instance copies */ | |
537 | eem->port.func.bind = eem_bind; | |
538 | eem->port.func.unbind = eem_unbind; | |
539 | eem->port.func.set_alt = eem_set_alt; | |
540 | eem->port.func.setup = eem_setup; | |
541 | eem->port.func.disable = eem_disable; | |
542 | eem->port.wrap = eem_wrap; | |
543 | eem->port.unwrap = eem_unwrap; | |
544 | eem->port.header_len = EEM_HLEN; | |
545 | ||
546 | status = usb_add_function(c, &eem->port.func); | |
547 | if (status) | |
548 | kfree(eem); | |
549 | return status; | |
550 | } | |
551 |