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