Linux 3.9-rc4
[linux-2.6/cjktty.git] / drivers / usb / gadget / f_eem.c
blobcf0ebee855636c9412b76f17d484c3f1b86bd3e8
1 /*
2 * f_eem.c -- USB CDC Ethernet (EEM) link function driver
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 * Copyright (C) 2009 EF Johnson Technologies
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.
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <linux/etherdevice.h>
17 #include <linux/crc32.h>
18 #include <linux/slab.h>
20 #include "u_ether.h"
22 #define EEM_HLEN 2
25 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
26 * Ethernet link.
29 struct f_eem {
30 struct gether port;
31 u8 ctrl_id;
34 static inline struct f_eem *func_to_eem(struct usb_function *f)
36 return container_of(f, struct f_eem, port.func);
39 /*-------------------------------------------------------------------------*/
41 /* interface descriptor: */
43 static struct usb_interface_descriptor eem_intf __initdata = {
44 .bLength = sizeof eem_intf,
45 .bDescriptorType = USB_DT_INTERFACE,
47 /* .bInterfaceNumber = DYNAMIC */
48 .bNumEndpoints = 2,
49 .bInterfaceClass = USB_CLASS_COMM,
50 .bInterfaceSubClass = USB_CDC_SUBCLASS_EEM,
51 .bInterfaceProtocol = USB_CDC_PROTO_EEM,
52 /* .iInterface = DYNAMIC */
55 /* full speed support: */
57 static struct usb_endpoint_descriptor eem_fs_in_desc __initdata = {
58 .bLength = USB_DT_ENDPOINT_SIZE,
59 .bDescriptorType = USB_DT_ENDPOINT,
61 .bEndpointAddress = USB_DIR_IN,
62 .bmAttributes = USB_ENDPOINT_XFER_BULK,
65 static struct usb_endpoint_descriptor eem_fs_out_desc __initdata = {
66 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
69 .bEndpointAddress = USB_DIR_OUT,
70 .bmAttributes = USB_ENDPOINT_XFER_BULK,
73 static struct usb_descriptor_header *eem_fs_function[] __initdata = {
74 /* CDC EEM control descriptors */
75 (struct usb_descriptor_header *) &eem_intf,
76 (struct usb_descriptor_header *) &eem_fs_in_desc,
77 (struct usb_descriptor_header *) &eem_fs_out_desc,
78 NULL,
81 /* high speed support: */
83 static struct usb_endpoint_descriptor eem_hs_in_desc __initdata = {
84 .bLength = USB_DT_ENDPOINT_SIZE,
85 .bDescriptorType = USB_DT_ENDPOINT,
87 .bEndpointAddress = USB_DIR_IN,
88 .bmAttributes = USB_ENDPOINT_XFER_BULK,
89 .wMaxPacketSize = cpu_to_le16(512),
92 static struct usb_endpoint_descriptor eem_hs_out_desc __initdata = {
93 .bLength = USB_DT_ENDPOINT_SIZE,
94 .bDescriptorType = USB_DT_ENDPOINT,
96 .bEndpointAddress = USB_DIR_OUT,
97 .bmAttributes = USB_ENDPOINT_XFER_BULK,
98 .wMaxPacketSize = cpu_to_le16(512),
101 static struct usb_descriptor_header *eem_hs_function[] __initdata = {
102 /* CDC EEM control descriptors */
103 (struct usb_descriptor_header *) &eem_intf,
104 (struct usb_descriptor_header *) &eem_hs_in_desc,
105 (struct usb_descriptor_header *) &eem_hs_out_desc,
106 NULL,
109 /* super speed support: */
111 static struct usb_endpoint_descriptor eem_ss_in_desc __initdata = {
112 .bLength = USB_DT_ENDPOINT_SIZE,
113 .bDescriptorType = USB_DT_ENDPOINT,
115 .bEndpointAddress = USB_DIR_IN,
116 .bmAttributes = USB_ENDPOINT_XFER_BULK,
117 .wMaxPacketSize = cpu_to_le16(1024),
120 static struct usb_endpoint_descriptor eem_ss_out_desc __initdata = {
121 .bLength = USB_DT_ENDPOINT_SIZE,
122 .bDescriptorType = USB_DT_ENDPOINT,
124 .bEndpointAddress = USB_DIR_OUT,
125 .bmAttributes = USB_ENDPOINT_XFER_BULK,
126 .wMaxPacketSize = cpu_to_le16(1024),
129 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc __initdata = {
130 .bLength = sizeof eem_ss_bulk_comp_desc,
131 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
133 /* the following 2 values can be tweaked if necessary */
134 /* .bMaxBurst = 0, */
135 /* .bmAttributes = 0, */
138 static struct usb_descriptor_header *eem_ss_function[] __initdata = {
139 /* CDC EEM control descriptors */
140 (struct usb_descriptor_header *) &eem_intf,
141 (struct usb_descriptor_header *) &eem_ss_in_desc,
142 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
143 (struct usb_descriptor_header *) &eem_ss_out_desc,
144 (struct usb_descriptor_header *) &eem_ss_bulk_comp_desc,
145 NULL,
148 /* string descriptors: */
150 static struct usb_string eem_string_defs[] = {
151 [0].s = "CDC Ethernet Emulation Model (EEM)",
152 { } /* end of list */
155 static struct usb_gadget_strings eem_string_table = {
156 .language = 0x0409, /* en-us */
157 .strings = eem_string_defs,
160 static struct usb_gadget_strings *eem_strings[] = {
161 &eem_string_table,
162 NULL,
165 /*-------------------------------------------------------------------------*/
167 static int eem_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
169 struct usb_composite_dev *cdev = f->config->cdev;
170 int value = -EOPNOTSUPP;
171 u16 w_index = le16_to_cpu(ctrl->wIndex);
172 u16 w_value = le16_to_cpu(ctrl->wValue);
173 u16 w_length = le16_to_cpu(ctrl->wLength);
175 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
176 ctrl->bRequestType, ctrl->bRequest,
177 w_value, w_index, w_length);
179 /* device either stalls (value < 0) or reports success */
180 return value;
184 static int eem_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
186 struct f_eem *eem = func_to_eem(f);
187 struct usb_composite_dev *cdev = f->config->cdev;
188 struct net_device *net;
190 /* we know alt == 0, so this is an activation or a reset */
191 if (alt != 0)
192 goto fail;
194 if (intf == eem->ctrl_id) {
196 if (eem->port.in_ep->driver_data) {
197 DBG(cdev, "reset eem\n");
198 gether_disconnect(&eem->port);
201 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
202 DBG(cdev, "init eem\n");
203 if (config_ep_by_speed(cdev->gadget, f,
204 eem->port.in_ep) ||
205 config_ep_by_speed(cdev->gadget, f,
206 eem->port.out_ep)) {
207 eem->port.in_ep->desc = NULL;
208 eem->port.out_ep->desc = NULL;
209 goto fail;
213 /* zlps should not occur because zero-length EEM packets
214 * will be inserted in those cases where they would occur
216 eem->port.is_zlp_ok = 1;
217 eem->port.cdc_filter = DEFAULT_FILTER;
218 DBG(cdev, "activate eem\n");
219 net = gether_connect(&eem->port);
220 if (IS_ERR(net))
221 return PTR_ERR(net);
222 } else
223 goto fail;
225 return 0;
226 fail:
227 return -EINVAL;
230 static void eem_disable(struct usb_function *f)
232 struct f_eem *eem = func_to_eem(f);
233 struct usb_composite_dev *cdev = f->config->cdev;
235 DBG(cdev, "eem deactivated\n");
237 if (eem->port.in_ep->driver_data)
238 gether_disconnect(&eem->port);
241 /*-------------------------------------------------------------------------*/
243 /* EEM function driver setup/binding */
245 static int __init
246 eem_bind(struct usb_configuration *c, struct usb_function *f)
248 struct usb_composite_dev *cdev = c->cdev;
249 struct f_eem *eem = func_to_eem(f);
250 int status;
251 struct usb_ep *ep;
253 /* allocate instance-specific interface IDs */
254 status = usb_interface_id(c, f);
255 if (status < 0)
256 goto fail;
257 eem->ctrl_id = status;
258 eem_intf.bInterfaceNumber = status;
260 status = -ENODEV;
262 /* allocate instance-specific endpoints */
263 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
264 if (!ep)
265 goto fail;
266 eem->port.in_ep = ep;
267 ep->driver_data = cdev; /* claim */
269 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
270 if (!ep)
271 goto fail;
272 eem->port.out_ep = ep;
273 ep->driver_data = cdev; /* claim */
275 status = -ENOMEM;
277 /* support all relevant hardware speeds... we expect that when
278 * hardware is dual speed, all bulk-capable endpoints work at
279 * both speeds
281 eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
282 eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
284 eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
285 eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
287 status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function,
288 eem_ss_function);
289 if (status)
290 goto fail;
292 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
293 gadget_is_superspeed(c->cdev->gadget) ? "super" :
294 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
295 eem->port.in_ep->name, eem->port.out_ep->name);
296 return 0;
298 fail:
299 usb_free_all_descriptors(f);
300 if (eem->port.out_ep)
301 eem->port.out_ep->driver_data = NULL;
302 if (eem->port.in_ep)
303 eem->port.in_ep->driver_data = NULL;
305 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
307 return status;
310 static void
311 eem_unbind(struct usb_configuration *c, struct usb_function *f)
313 struct f_eem *eem = func_to_eem(f);
315 DBG(c->cdev, "eem unbind\n");
317 usb_free_all_descriptors(f);
318 kfree(eem);
321 static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
323 struct sk_buff *skb = (struct sk_buff *)req->context;
325 dev_kfree_skb_any(skb);
329 * Add the EEM header and ethernet checksum.
330 * We currently do not attempt to put multiple ethernet frames
331 * into a single USB transfer
333 static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
335 struct sk_buff *skb2 = NULL;
336 struct usb_ep *in = port->in_ep;
337 int padlen = 0;
338 u16 len = skb->len;
340 if (!skb_cloned(skb)) {
341 int headroom = skb_headroom(skb);
342 int tailroom = skb_tailroom(skb);
344 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
345 * stick two bytes of zero-length EEM packet on the end.
347 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
348 padlen += 2;
350 if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
351 (headroom >= EEM_HLEN))
352 goto done;
355 skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
356 dev_kfree_skb_any(skb);
357 skb = skb2;
358 if (!skb)
359 return skb;
361 done:
362 /* use the "no CRC" option */
363 put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
365 /* EEM packet header format:
366 * b0..13: length of ethernet frame
367 * b14: bmCRC (0 == sentinel CRC)
368 * b15: bmType (0 == data)
370 len = skb->len;
371 put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
373 /* add a zero-length EEM packet, if needed */
374 if (padlen)
375 put_unaligned_le16(0, skb_put(skb, 2));
377 return skb;
381 * Remove the EEM header. Note that there can be many EEM packets in a single
382 * USB transfer, so we need to break them out and handle them independently.
384 static int eem_unwrap(struct gether *port,
385 struct sk_buff *skb,
386 struct sk_buff_head *list)
388 struct usb_composite_dev *cdev = port->func.config->cdev;
389 int status = 0;
391 do {
392 struct sk_buff *skb2;
393 u16 header;
394 u16 len = 0;
396 if (skb->len < EEM_HLEN) {
397 status = -EINVAL;
398 DBG(cdev, "invalid EEM header\n");
399 goto error;
402 /* remove the EEM header */
403 header = get_unaligned_le16(skb->data);
404 skb_pull(skb, EEM_HLEN);
406 /* EEM packet header format:
407 * b0..14: EEM type dependent (data or command)
408 * b15: bmType (0 == data, 1 == command)
410 if (header & BIT(15)) {
411 struct usb_request *req = cdev->req;
412 u16 bmEEMCmd;
414 /* EEM command packet format:
415 * b0..10: bmEEMCmdParam
416 * b11..13: bmEEMCmd
417 * b14: reserved (must be zero)
418 * b15: bmType (1 == command)
420 if (header & BIT(14))
421 continue;
423 bmEEMCmd = (header >> 11) & 0x7;
424 switch (bmEEMCmd) {
425 case 0: /* echo */
426 len = header & 0x7FF;
427 if (skb->len < len) {
428 status = -EOVERFLOW;
429 goto error;
432 skb2 = skb_clone(skb, GFP_ATOMIC);
433 if (unlikely(!skb2)) {
434 DBG(cdev, "EEM echo response error\n");
435 goto next;
437 skb_trim(skb2, len);
438 put_unaligned_le16(BIT(15) | BIT(11) | len,
439 skb_push(skb2, 2));
440 skb_copy_bits(skb2, 0, req->buf, skb2->len);
441 req->length = skb2->len;
442 req->complete = eem_cmd_complete;
443 req->zero = 1;
444 req->context = skb2;
445 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
446 DBG(cdev, "echo response queue fail\n");
447 break;
449 case 1: /* echo response */
450 case 2: /* suspend hint */
451 case 3: /* response hint */
452 case 4: /* response complete hint */
453 case 5: /* tickle */
454 default: /* reserved */
455 continue;
457 } else {
458 u32 crc, crc2;
459 struct sk_buff *skb3;
461 /* check for zero-length EEM packet */
462 if (header == 0)
463 continue;
465 /* EEM data packet format:
466 * b0..13: length of ethernet frame
467 * b14: bmCRC (0 == sentinel, 1 == calculated)
468 * b15: bmType (0 == data)
470 len = header & 0x3FFF;
471 if ((skb->len < len)
472 || (len < (ETH_HLEN + ETH_FCS_LEN))) {
473 status = -EINVAL;
474 goto error;
477 /* validate CRC */
478 if (header & BIT(14)) {
479 crc = get_unaligned_le32(skb->data + len
480 - ETH_FCS_LEN);
481 crc2 = ~crc32_le(~0,
482 skb->data, len - ETH_FCS_LEN);
483 } else {
484 crc = get_unaligned_be32(skb->data + len
485 - ETH_FCS_LEN);
486 crc2 = 0xdeadbeef;
488 if (crc != crc2) {
489 DBG(cdev, "invalid EEM CRC\n");
490 goto next;
493 skb2 = skb_clone(skb, GFP_ATOMIC);
494 if (unlikely(!skb2)) {
495 DBG(cdev, "unable to unframe EEM packet\n");
496 continue;
498 skb_trim(skb2, len - ETH_FCS_LEN);
500 skb3 = skb_copy_expand(skb2,
501 NET_IP_ALIGN,
503 GFP_ATOMIC);
504 if (unlikely(!skb3)) {
505 DBG(cdev, "unable to realign EEM packet\n");
506 dev_kfree_skb_any(skb2);
507 continue;
509 dev_kfree_skb_any(skb2);
510 skb_queue_tail(list, skb3);
512 next:
513 skb_pull(skb, len);
514 } while (skb->len);
516 error:
517 dev_kfree_skb_any(skb);
518 return status;
522 * eem_bind_config - add CDC Ethernet (EEM) network link to a configuration
523 * @c: the configuration to support the network link
524 * Context: single threaded during gadget setup
526 * Returns zero on success, else negative errno.
528 * Caller must have called @gether_setup(). Caller is also responsible
529 * for calling @gether_cleanup() before module unload.
531 int __init eem_bind_config(struct usb_configuration *c)
533 struct f_eem *eem;
534 int status;
536 /* maybe allocate device-global string IDs */
537 if (eem_string_defs[0].id == 0) {
539 /* control interface label */
540 status = usb_string_id(c->cdev);
541 if (status < 0)
542 return status;
543 eem_string_defs[0].id = status;
544 eem_intf.iInterface = status;
547 /* allocate and initialize one new instance */
548 eem = kzalloc(sizeof *eem, GFP_KERNEL);
549 if (!eem)
550 return -ENOMEM;
552 eem->port.cdc_filter = DEFAULT_FILTER;
554 eem->port.func.name = "cdc_eem";
555 eem->port.func.strings = eem_strings;
556 /* descriptors are per-instance copies */
557 eem->port.func.bind = eem_bind;
558 eem->port.func.unbind = eem_unbind;
559 eem->port.func.set_alt = eem_set_alt;
560 eem->port.func.setup = eem_setup;
561 eem->port.func.disable = eem_disable;
562 eem->port.wrap = eem_wrap;
563 eem->port.unwrap = eem_unwrap;
564 eem->port.header_len = EEM_HLEN;
566 status = usb_add_function(c, &eem->port.func);
567 if (status)
568 kfree(eem);
569 return status;