usb: gadget: f_eem: remove compatibility layer
[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_eem.h"
23
24 #define EEM_HLEN 2
25
26 /*
27 * This function is a "CDC Ethernet Emulation Model" (CDC EEM)
28 * Ethernet link.
29 */
30
31 struct f_eem {
32 struct gether port;
33 u8 ctrl_id;
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
45 static struct usb_interface_descriptor eem_intf = {
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
59 static struct usb_endpoint_descriptor eem_fs_in_desc = {
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
67 static struct usb_endpoint_descriptor eem_fs_out_desc = {
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
75 static struct usb_descriptor_header *eem_fs_function[] = {
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
85 static struct usb_endpoint_descriptor eem_hs_in_desc = {
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
94 static struct usb_endpoint_descriptor eem_hs_out_desc = {
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
103 static struct usb_descriptor_header *eem_hs_function[] = {
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
111 /* super speed support: */
112
113 static struct usb_endpoint_descriptor eem_ss_in_desc = {
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
122 static struct usb_endpoint_descriptor eem_ss_out_desc = {
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
131 static struct usb_ss_ep_comp_descriptor eem_ss_bulk_comp_desc = {
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
140 static struct usb_descriptor_header *eem_ss_function[] = {
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
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
203 if (!eem->port.in_ep->desc || !eem->port.out_ep->desc) {
204 DBG(cdev, "init eem\n");
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 }
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
247 static int eem_bind(struct usb_configuration *c, struct usb_function *f)
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
254 struct f_eem_opts *eem_opts;
255
256 eem_opts = container_of(f->fi, struct f_eem_opts, func_inst);
257 /*
258 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
259 * configurations are bound in sequence with list_for_each_entry,
260 * in each configuration its functions are bound in sequence
261 * with list_for_each_entry, so we assume no race condition
262 * with regard to eem_opts->bound access
263 */
264 if (!eem_opts->bound) {
265 gether_set_gadget(eem_opts->net, cdev->gadget);
266 status = gether_register_netdev(eem_opts->net);
267 if (status)
268 return status;
269 eem_opts->bound = true;
270 }
271
272 /* maybe allocate device-global string IDs */
273 if (eem_string_defs[0].id == 0) {
274
275 /* control interface label */
276 status = usb_string_id(c->cdev);
277 if (status < 0)
278 return status;
279 eem_string_defs[0].id = status;
280 eem_intf.iInterface = status;
281 }
282
283 /* allocate instance-specific interface IDs */
284 status = usb_interface_id(c, f);
285 if (status < 0)
286 goto fail;
287 eem->ctrl_id = status;
288 eem_intf.bInterfaceNumber = status;
289
290 status = -ENODEV;
291
292 /* allocate instance-specific endpoints */
293 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_in_desc);
294 if (!ep)
295 goto fail;
296 eem->port.in_ep = ep;
297 ep->driver_data = cdev; /* claim */
298
299 ep = usb_ep_autoconfig(cdev->gadget, &eem_fs_out_desc);
300 if (!ep)
301 goto fail;
302 eem->port.out_ep = ep;
303 ep->driver_data = cdev; /* claim */
304
305 status = -ENOMEM;
306
307 /* support all relevant hardware speeds... we expect that when
308 * hardware is dual speed, all bulk-capable endpoints work at
309 * both speeds
310 */
311 eem_hs_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
312 eem_hs_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
313
314 eem_ss_in_desc.bEndpointAddress = eem_fs_in_desc.bEndpointAddress;
315 eem_ss_out_desc.bEndpointAddress = eem_fs_out_desc.bEndpointAddress;
316
317 status = usb_assign_descriptors(f, eem_fs_function, eem_hs_function,
318 eem_ss_function);
319 if (status)
320 goto fail;
321
322 DBG(cdev, "CDC Ethernet (EEM): %s speed IN/%s OUT/%s\n",
323 gadget_is_superspeed(c->cdev->gadget) ? "super" :
324 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
325 eem->port.in_ep->name, eem->port.out_ep->name);
326 return 0;
327
328 fail:
329 usb_free_all_descriptors(f);
330 if (eem->port.out_ep)
331 eem->port.out_ep->driver_data = NULL;
332 if (eem->port.in_ep)
333 eem->port.in_ep->driver_data = NULL;
334
335 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
336
337 return status;
338 }
339
340 static void eem_cmd_complete(struct usb_ep *ep, struct usb_request *req)
341 {
342 struct sk_buff *skb = (struct sk_buff *)req->context;
343
344 dev_kfree_skb_any(skb);
345 }
346
347 /*
348 * Add the EEM header and ethernet checksum.
349 * We currently do not attempt to put multiple ethernet frames
350 * into a single USB transfer
351 */
352 static struct sk_buff *eem_wrap(struct gether *port, struct sk_buff *skb)
353 {
354 struct sk_buff *skb2 = NULL;
355 struct usb_ep *in = port->in_ep;
356 int padlen = 0;
357 u16 len = skb->len;
358
359 if (!skb_cloned(skb)) {
360 int headroom = skb_headroom(skb);
361 int tailroom = skb_tailroom(skb);
362
363 /* When (len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) is 0,
364 * stick two bytes of zero-length EEM packet on the end.
365 */
366 if (((len + EEM_HLEN + ETH_FCS_LEN) % in->maxpacket) == 0)
367 padlen += 2;
368
369 if ((tailroom >= (ETH_FCS_LEN + padlen)) &&
370 (headroom >= EEM_HLEN))
371 goto done;
372 }
373
374 skb2 = skb_copy_expand(skb, EEM_HLEN, ETH_FCS_LEN + padlen, GFP_ATOMIC);
375 dev_kfree_skb_any(skb);
376 skb = skb2;
377 if (!skb)
378 return skb;
379
380 done:
381 /* use the "no CRC" option */
382 put_unaligned_be32(0xdeadbeef, skb_put(skb, 4));
383
384 /* EEM packet header format:
385 * b0..13: length of ethernet frame
386 * b14: bmCRC (0 == sentinel CRC)
387 * b15: bmType (0 == data)
388 */
389 len = skb->len;
390 put_unaligned_le16(len & 0x3FFF, skb_push(skb, 2));
391
392 /* add a zero-length EEM packet, if needed */
393 if (padlen)
394 put_unaligned_le16(0, skb_put(skb, 2));
395
396 return skb;
397 }
398
399 /*
400 * Remove the EEM header. Note that there can be many EEM packets in a single
401 * USB transfer, so we need to break them out and handle them independently.
402 */
403 static int eem_unwrap(struct gether *port,
404 struct sk_buff *skb,
405 struct sk_buff_head *list)
406 {
407 struct usb_composite_dev *cdev = port->func.config->cdev;
408 int status = 0;
409
410 do {
411 struct sk_buff *skb2;
412 u16 header;
413 u16 len = 0;
414
415 if (skb->len < EEM_HLEN) {
416 status = -EINVAL;
417 DBG(cdev, "invalid EEM header\n");
418 goto error;
419 }
420
421 /* remove the EEM header */
422 header = get_unaligned_le16(skb->data);
423 skb_pull(skb, EEM_HLEN);
424
425 /* EEM packet header format:
426 * b0..14: EEM type dependent (data or command)
427 * b15: bmType (0 == data, 1 == command)
428 */
429 if (header & BIT(15)) {
430 struct usb_request *req = cdev->req;
431 u16 bmEEMCmd;
432
433 /* EEM command packet format:
434 * b0..10: bmEEMCmdParam
435 * b11..13: bmEEMCmd
436 * b14: reserved (must be zero)
437 * b15: bmType (1 == command)
438 */
439 if (header & BIT(14))
440 continue;
441
442 bmEEMCmd = (header >> 11) & 0x7;
443 switch (bmEEMCmd) {
444 case 0: /* echo */
445 len = header & 0x7FF;
446 if (skb->len < len) {
447 status = -EOVERFLOW;
448 goto error;
449 }
450
451 skb2 = skb_clone(skb, GFP_ATOMIC);
452 if (unlikely(!skb2)) {
453 DBG(cdev, "EEM echo response error\n");
454 goto next;
455 }
456 skb_trim(skb2, len);
457 put_unaligned_le16(BIT(15) | BIT(11) | len,
458 skb_push(skb2, 2));
459 skb_copy_bits(skb2, 0, req->buf, skb2->len);
460 req->length = skb2->len;
461 req->complete = eem_cmd_complete;
462 req->zero = 1;
463 req->context = skb2;
464 if (usb_ep_queue(port->in_ep, req, GFP_ATOMIC))
465 DBG(cdev, "echo response queue fail\n");
466 break;
467
468 case 1: /* echo response */
469 case 2: /* suspend hint */
470 case 3: /* response hint */
471 case 4: /* response complete hint */
472 case 5: /* tickle */
473 default: /* reserved */
474 continue;
475 }
476 } else {
477 u32 crc, crc2;
478 struct sk_buff *skb3;
479
480 /* check for zero-length EEM packet */
481 if (header == 0)
482 continue;
483
484 /* EEM data packet format:
485 * b0..13: length of ethernet frame
486 * b14: bmCRC (0 == sentinel, 1 == calculated)
487 * b15: bmType (0 == data)
488 */
489 len = header & 0x3FFF;
490 if ((skb->len < len)
491 || (len < (ETH_HLEN + ETH_FCS_LEN))) {
492 status = -EINVAL;
493 goto error;
494 }
495
496 /* validate CRC */
497 if (header & BIT(14)) {
498 crc = get_unaligned_le32(skb->data + len
499 - ETH_FCS_LEN);
500 crc2 = ~crc32_le(~0,
501 skb->data, len - ETH_FCS_LEN);
502 } else {
503 crc = get_unaligned_be32(skb->data + len
504 - ETH_FCS_LEN);
505 crc2 = 0xdeadbeef;
506 }
507 if (crc != crc2) {
508 DBG(cdev, "invalid EEM CRC\n");
509 goto next;
510 }
511
512 skb2 = skb_clone(skb, GFP_ATOMIC);
513 if (unlikely(!skb2)) {
514 DBG(cdev, "unable to unframe EEM packet\n");
515 continue;
516 }
517 skb_trim(skb2, len - ETH_FCS_LEN);
518
519 skb3 = skb_copy_expand(skb2,
520 NET_IP_ALIGN,
521 0,
522 GFP_ATOMIC);
523 if (unlikely(!skb3)) {
524 DBG(cdev, "unable to realign EEM packet\n");
525 dev_kfree_skb_any(skb2);
526 continue;
527 }
528 dev_kfree_skb_any(skb2);
529 skb_queue_tail(list, skb3);
530 }
531 next:
532 skb_pull(skb, len);
533 } while (skb->len);
534
535 error:
536 dev_kfree_skb_any(skb);
537 return status;
538 }
539
540 static void eem_free_inst(struct usb_function_instance *f)
541 {
542 struct f_eem_opts *opts;
543
544 opts = container_of(f, struct f_eem_opts, func_inst);
545 if (opts->bound)
546 gether_cleanup(netdev_priv(opts->net));
547 else
548 free_netdev(opts->net);
549 kfree(opts);
550 }
551
552 static struct usb_function_instance *eem_alloc_inst(void)
553 {
554 struct f_eem_opts *opts;
555
556 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
557 if (!opts)
558 return ERR_PTR(-ENOMEM);
559 opts->func_inst.free_func_inst = eem_free_inst;
560 opts->net = gether_setup_default();
561 if (IS_ERR(opts->net))
562 return ERR_CAST(opts->net);
563
564 return &opts->func_inst;
565 }
566
567 static void eem_free(struct usb_function *f)
568 {
569 struct f_eem *eem;
570
571 eem = func_to_eem(f);
572 kfree(eem);
573 }
574
575 static void eem_unbind(struct usb_configuration *c, struct usb_function *f)
576 {
577 DBG(c->cdev, "eem unbind\n");
578
579 usb_free_all_descriptors(f);
580 }
581
582 struct usb_function *eem_alloc(struct usb_function_instance *fi)
583 {
584 struct f_eem *eem;
585 struct f_eem_opts *opts;
586
587 /* allocate and initialize one new instance */
588 eem = kzalloc(sizeof(*eem), GFP_KERNEL);
589 if (!eem)
590 return ERR_PTR(-ENOMEM);
591
592 opts = container_of(fi, struct f_eem_opts, func_inst);
593
594 eem->port.ioport = netdev_priv(opts->net);
595 eem->port.cdc_filter = DEFAULT_FILTER;
596
597 eem->port.func.name = "cdc_eem";
598 eem->port.func.strings = eem_strings;
599 /* descriptors are per-instance copies */
600 eem->port.func.bind = eem_bind;
601 eem->port.func.unbind = eem_unbind;
602 eem->port.func.set_alt = eem_set_alt;
603 eem->port.func.setup = eem_setup;
604 eem->port.func.disable = eem_disable;
605 eem->port.func.free_func = eem_free;
606 eem->port.wrap = eem_wrap;
607 eem->port.unwrap = eem_unwrap;
608 eem->port.header_len = EEM_HLEN;
609
610 return &eem->port.func;
611 }
612
613 DECLARE_USB_FUNCTION_INIT(eem, eem_alloc_inst, eem_alloc);
614 MODULE_LICENSE("GPL");
615 MODULE_AUTHOR("David Brownell");
This page took 0.049686 seconds and 6 git commands to generate.