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