22d19483e101352f471c4f3f1c389dbb85016b7d
[deliverable/linux.git] / drivers / usb / gadget / f_ecm.c
1 /*
2 * f_ecm.c -- USB CDC Ethernet (ECM) link function driver
3 *
4 * Copyright (C) 2003-2005,2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13 /* #define VERBOSE_DEBUG */
14
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/device.h>
19 #include <linux/etherdevice.h>
20
21 #include "u_ether.h"
22 #include "u_ecm.h"
23
24
25 /*
26 * This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
27 * Ethernet link. The data transfer model is simple (packets sent and
28 * received over bulk endpoints using normal short packet termination),
29 * and the control model exposes various data and optional notifications.
30 *
31 * ECM is well standardized and (except for Microsoft) supported by most
32 * operating systems with USB host support. It's the preferred interop
33 * solution for Ethernet over USB, at least for firmware based solutions.
34 * (Hardware solutions tend to be more minimalist.) A newer and simpler
35 * "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
36 *
37 * Note that ECM requires the use of "alternate settings" for its data
38 * interface. This means that the set_alt() method has real work to do,
39 * and also means that a get_alt() method is required.
40 */
41
42
43 enum ecm_notify_state {
44 ECM_NOTIFY_NONE, /* don't notify */
45 ECM_NOTIFY_CONNECT, /* issue CONNECT next */
46 ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
47 };
48
49 struct f_ecm {
50 struct gether port;
51 u8 ctrl_id, data_id;
52
53 char ethaddr[14];
54
55 struct usb_ep *notify;
56 struct usb_request *notify_req;
57 u8 notify_state;
58 bool is_open;
59
60 /* FIXME is_open needs some irq-ish locking
61 * ... possibly the same as port.ioport
62 */
63 };
64
65 static inline struct f_ecm *func_to_ecm(struct usb_function *f)
66 {
67 return container_of(f, struct f_ecm, port.func);
68 }
69
70 /* peak (theoretical) bulk transfer rate in bits-per-second */
71 static inline unsigned ecm_bitrate(struct usb_gadget *g)
72 {
73 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
74 return 13 * 1024 * 8 * 1000 * 8;
75 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
76 return 13 * 512 * 8 * 1000 * 8;
77 else
78 return 19 * 64 * 1 * 1000 * 8;
79 }
80
81 /*-------------------------------------------------------------------------*/
82
83 /*
84 * Include the status endpoint if we can, even though it's optional.
85 *
86 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
87 * packet, to simplify cancellation; and a big transfer interval, to
88 * waste less bandwidth.
89 *
90 * Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
91 * if they ignore the connect/disconnect notifications that real aether
92 * can provide. More advanced cdc configurations might want to support
93 * encapsulated commands (vendor-specific, using control-OUT).
94 */
95
96 #define ECM_STATUS_INTERVAL_MS 32
97 #define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
98
99
100 /* interface descriptor: */
101
102 static struct usb_interface_assoc_descriptor
103 ecm_iad_descriptor = {
104 .bLength = sizeof ecm_iad_descriptor,
105 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
106
107 /* .bFirstInterface = DYNAMIC, */
108 .bInterfaceCount = 2, /* control + data */
109 .bFunctionClass = USB_CLASS_COMM,
110 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
111 .bFunctionProtocol = USB_CDC_PROTO_NONE,
112 /* .iFunction = DYNAMIC */
113 };
114
115
116 static struct usb_interface_descriptor ecm_control_intf = {
117 .bLength = sizeof ecm_control_intf,
118 .bDescriptorType = USB_DT_INTERFACE,
119
120 /* .bInterfaceNumber = DYNAMIC */
121 /* status endpoint is optional; this could be patched later */
122 .bNumEndpoints = 1,
123 .bInterfaceClass = USB_CLASS_COMM,
124 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
125 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
126 /* .iInterface = DYNAMIC */
127 };
128
129 static struct usb_cdc_header_desc ecm_header_desc = {
130 .bLength = sizeof ecm_header_desc,
131 .bDescriptorType = USB_DT_CS_INTERFACE,
132 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
133
134 .bcdCDC = cpu_to_le16(0x0110),
135 };
136
137 static struct usb_cdc_union_desc ecm_union_desc = {
138 .bLength = sizeof(ecm_union_desc),
139 .bDescriptorType = USB_DT_CS_INTERFACE,
140 .bDescriptorSubType = USB_CDC_UNION_TYPE,
141 /* .bMasterInterface0 = DYNAMIC */
142 /* .bSlaveInterface0 = DYNAMIC */
143 };
144
145 static struct usb_cdc_ether_desc ecm_desc = {
146 .bLength = sizeof ecm_desc,
147 .bDescriptorType = USB_DT_CS_INTERFACE,
148 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
149
150 /* this descriptor actually adds value, surprise! */
151 /* .iMACAddress = DYNAMIC */
152 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
153 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
154 .wNumberMCFilters = cpu_to_le16(0),
155 .bNumberPowerFilters = 0,
156 };
157
158 /* the default data interface has no endpoints ... */
159
160 static struct usb_interface_descriptor ecm_data_nop_intf = {
161 .bLength = sizeof ecm_data_nop_intf,
162 .bDescriptorType = USB_DT_INTERFACE,
163
164 .bInterfaceNumber = 1,
165 .bAlternateSetting = 0,
166 .bNumEndpoints = 0,
167 .bInterfaceClass = USB_CLASS_CDC_DATA,
168 .bInterfaceSubClass = 0,
169 .bInterfaceProtocol = 0,
170 /* .iInterface = DYNAMIC */
171 };
172
173 /* ... but the "real" data interface has two bulk endpoints */
174
175 static struct usb_interface_descriptor ecm_data_intf = {
176 .bLength = sizeof ecm_data_intf,
177 .bDescriptorType = USB_DT_INTERFACE,
178
179 .bInterfaceNumber = 1,
180 .bAlternateSetting = 1,
181 .bNumEndpoints = 2,
182 .bInterfaceClass = USB_CLASS_CDC_DATA,
183 .bInterfaceSubClass = 0,
184 .bInterfaceProtocol = 0,
185 /* .iInterface = DYNAMIC */
186 };
187
188 /* full speed support: */
189
190 static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
191 .bLength = USB_DT_ENDPOINT_SIZE,
192 .bDescriptorType = USB_DT_ENDPOINT,
193
194 .bEndpointAddress = USB_DIR_IN,
195 .bmAttributes = USB_ENDPOINT_XFER_INT,
196 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
197 .bInterval = ECM_STATUS_INTERVAL_MS,
198 };
199
200 static struct usb_endpoint_descriptor fs_ecm_in_desc = {
201 .bLength = USB_DT_ENDPOINT_SIZE,
202 .bDescriptorType = USB_DT_ENDPOINT,
203
204 .bEndpointAddress = USB_DIR_IN,
205 .bmAttributes = USB_ENDPOINT_XFER_BULK,
206 };
207
208 static struct usb_endpoint_descriptor fs_ecm_out_desc = {
209 .bLength = USB_DT_ENDPOINT_SIZE,
210 .bDescriptorType = USB_DT_ENDPOINT,
211
212 .bEndpointAddress = USB_DIR_OUT,
213 .bmAttributes = USB_ENDPOINT_XFER_BULK,
214 };
215
216 static struct usb_descriptor_header *ecm_fs_function[] = {
217 /* CDC ECM control descriptors */
218 (struct usb_descriptor_header *) &ecm_iad_descriptor,
219 (struct usb_descriptor_header *) &ecm_control_intf,
220 (struct usb_descriptor_header *) &ecm_header_desc,
221 (struct usb_descriptor_header *) &ecm_union_desc,
222 (struct usb_descriptor_header *) &ecm_desc,
223
224 /* NOTE: status endpoint might need to be removed */
225 (struct usb_descriptor_header *) &fs_ecm_notify_desc,
226
227 /* data interface, altsettings 0 and 1 */
228 (struct usb_descriptor_header *) &ecm_data_nop_intf,
229 (struct usb_descriptor_header *) &ecm_data_intf,
230 (struct usb_descriptor_header *) &fs_ecm_in_desc,
231 (struct usb_descriptor_header *) &fs_ecm_out_desc,
232 NULL,
233 };
234
235 /* high speed support: */
236
237 static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
238 .bLength = USB_DT_ENDPOINT_SIZE,
239 .bDescriptorType = USB_DT_ENDPOINT,
240
241 .bEndpointAddress = USB_DIR_IN,
242 .bmAttributes = USB_ENDPOINT_XFER_INT,
243 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
244 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
245 };
246
247 static struct usb_endpoint_descriptor hs_ecm_in_desc = {
248 .bLength = USB_DT_ENDPOINT_SIZE,
249 .bDescriptorType = USB_DT_ENDPOINT,
250
251 .bEndpointAddress = USB_DIR_IN,
252 .bmAttributes = USB_ENDPOINT_XFER_BULK,
253 .wMaxPacketSize = cpu_to_le16(512),
254 };
255
256 static struct usb_endpoint_descriptor hs_ecm_out_desc = {
257 .bLength = USB_DT_ENDPOINT_SIZE,
258 .bDescriptorType = USB_DT_ENDPOINT,
259
260 .bEndpointAddress = USB_DIR_OUT,
261 .bmAttributes = USB_ENDPOINT_XFER_BULK,
262 .wMaxPacketSize = cpu_to_le16(512),
263 };
264
265 static struct usb_descriptor_header *ecm_hs_function[] = {
266 /* CDC ECM control descriptors */
267 (struct usb_descriptor_header *) &ecm_iad_descriptor,
268 (struct usb_descriptor_header *) &ecm_control_intf,
269 (struct usb_descriptor_header *) &ecm_header_desc,
270 (struct usb_descriptor_header *) &ecm_union_desc,
271 (struct usb_descriptor_header *) &ecm_desc,
272
273 /* NOTE: status endpoint might need to be removed */
274 (struct usb_descriptor_header *) &hs_ecm_notify_desc,
275
276 /* data interface, altsettings 0 and 1 */
277 (struct usb_descriptor_header *) &ecm_data_nop_intf,
278 (struct usb_descriptor_header *) &ecm_data_intf,
279 (struct usb_descriptor_header *) &hs_ecm_in_desc,
280 (struct usb_descriptor_header *) &hs_ecm_out_desc,
281 NULL,
282 };
283
284 /* super speed support: */
285
286 static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
287 .bLength = USB_DT_ENDPOINT_SIZE,
288 .bDescriptorType = USB_DT_ENDPOINT,
289
290 .bEndpointAddress = USB_DIR_IN,
291 .bmAttributes = USB_ENDPOINT_XFER_INT,
292 .wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
293 .bInterval = USB_MS_TO_HS_INTERVAL(ECM_STATUS_INTERVAL_MS),
294 };
295
296 static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
297 .bLength = sizeof ss_ecm_intr_comp_desc,
298 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
299
300 /* the following 3 values can be tweaked if necessary */
301 /* .bMaxBurst = 0, */
302 /* .bmAttributes = 0, */
303 .wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
304 };
305
306 static struct usb_endpoint_descriptor ss_ecm_in_desc = {
307 .bLength = USB_DT_ENDPOINT_SIZE,
308 .bDescriptorType = USB_DT_ENDPOINT,
309
310 .bEndpointAddress = USB_DIR_IN,
311 .bmAttributes = USB_ENDPOINT_XFER_BULK,
312 .wMaxPacketSize = cpu_to_le16(1024),
313 };
314
315 static struct usb_endpoint_descriptor ss_ecm_out_desc = {
316 .bLength = USB_DT_ENDPOINT_SIZE,
317 .bDescriptorType = USB_DT_ENDPOINT,
318
319 .bEndpointAddress = USB_DIR_OUT,
320 .bmAttributes = USB_ENDPOINT_XFER_BULK,
321 .wMaxPacketSize = cpu_to_le16(1024),
322 };
323
324 static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
325 .bLength = sizeof ss_ecm_bulk_comp_desc,
326 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
327
328 /* the following 2 values can be tweaked if necessary */
329 /* .bMaxBurst = 0, */
330 /* .bmAttributes = 0, */
331 };
332
333 static struct usb_descriptor_header *ecm_ss_function[] = {
334 /* CDC ECM control descriptors */
335 (struct usb_descriptor_header *) &ecm_iad_descriptor,
336 (struct usb_descriptor_header *) &ecm_control_intf,
337 (struct usb_descriptor_header *) &ecm_header_desc,
338 (struct usb_descriptor_header *) &ecm_union_desc,
339 (struct usb_descriptor_header *) &ecm_desc,
340
341 /* NOTE: status endpoint might need to be removed */
342 (struct usb_descriptor_header *) &ss_ecm_notify_desc,
343 (struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
344
345 /* data interface, altsettings 0 and 1 */
346 (struct usb_descriptor_header *) &ecm_data_nop_intf,
347 (struct usb_descriptor_header *) &ecm_data_intf,
348 (struct usb_descriptor_header *) &ss_ecm_in_desc,
349 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
350 (struct usb_descriptor_header *) &ss_ecm_out_desc,
351 (struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
352 NULL,
353 };
354
355 /* string descriptors: */
356
357 static struct usb_string ecm_string_defs[] = {
358 [0].s = "CDC Ethernet Control Model (ECM)",
359 [1].s = "",
360 [2].s = "CDC Ethernet Data",
361 [3].s = "CDC ECM",
362 { } /* end of list */
363 };
364
365 static struct usb_gadget_strings ecm_string_table = {
366 .language = 0x0409, /* en-us */
367 .strings = ecm_string_defs,
368 };
369
370 static struct usb_gadget_strings *ecm_strings[] = {
371 &ecm_string_table,
372 NULL,
373 };
374
375 /*-------------------------------------------------------------------------*/
376
377 static void ecm_do_notify(struct f_ecm *ecm)
378 {
379 struct usb_request *req = ecm->notify_req;
380 struct usb_cdc_notification *event;
381 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
382 __le32 *data;
383 int status;
384
385 /* notification already in flight? */
386 if (!req)
387 return;
388
389 event = req->buf;
390 switch (ecm->notify_state) {
391 case ECM_NOTIFY_NONE:
392 return;
393
394 case ECM_NOTIFY_CONNECT:
395 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
396 if (ecm->is_open)
397 event->wValue = cpu_to_le16(1);
398 else
399 event->wValue = cpu_to_le16(0);
400 event->wLength = 0;
401 req->length = sizeof *event;
402
403 DBG(cdev, "notify connect %s\n",
404 ecm->is_open ? "true" : "false");
405 ecm->notify_state = ECM_NOTIFY_SPEED;
406 break;
407
408 case ECM_NOTIFY_SPEED:
409 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
410 event->wValue = cpu_to_le16(0);
411 event->wLength = cpu_to_le16(8);
412 req->length = ECM_STATUS_BYTECOUNT;
413
414 /* SPEED_CHANGE data is up/down speeds in bits/sec */
415 data = req->buf + sizeof *event;
416 data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
417 data[1] = data[0];
418
419 DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
420 ecm->notify_state = ECM_NOTIFY_NONE;
421 break;
422 }
423 event->bmRequestType = 0xA1;
424 event->wIndex = cpu_to_le16(ecm->ctrl_id);
425
426 ecm->notify_req = NULL;
427 status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
428 if (status < 0) {
429 ecm->notify_req = req;
430 DBG(cdev, "notify --> %d\n", status);
431 }
432 }
433
434 static void ecm_notify(struct f_ecm *ecm)
435 {
436 /* NOTE on most versions of Linux, host side cdc-ethernet
437 * won't listen for notifications until its netdevice opens.
438 * The first notification then sits in the FIFO for a long
439 * time, and the second one is queued.
440 */
441 ecm->notify_state = ECM_NOTIFY_CONNECT;
442 ecm_do_notify(ecm);
443 }
444
445 static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
446 {
447 struct f_ecm *ecm = req->context;
448 struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
449 struct usb_cdc_notification *event = req->buf;
450
451 switch (req->status) {
452 case 0:
453 /* no fault */
454 break;
455 case -ECONNRESET:
456 case -ESHUTDOWN:
457 ecm->notify_state = ECM_NOTIFY_NONE;
458 break;
459 default:
460 DBG(cdev, "event %02x --> %d\n",
461 event->bNotificationType, req->status);
462 break;
463 }
464 ecm->notify_req = req;
465 ecm_do_notify(ecm);
466 }
467
468 static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
469 {
470 struct f_ecm *ecm = func_to_ecm(f);
471 struct usb_composite_dev *cdev = f->config->cdev;
472 struct usb_request *req = cdev->req;
473 int value = -EOPNOTSUPP;
474 u16 w_index = le16_to_cpu(ctrl->wIndex);
475 u16 w_value = le16_to_cpu(ctrl->wValue);
476 u16 w_length = le16_to_cpu(ctrl->wLength);
477
478 /* composite driver infrastructure handles everything except
479 * CDC class messages; interface activation uses set_alt().
480 */
481 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
482 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
483 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
484 /* see 6.2.30: no data, wIndex = interface,
485 * wValue = packet filter bitmap
486 */
487 if (w_length != 0 || w_index != ecm->ctrl_id)
488 goto invalid;
489 DBG(cdev, "packet filter %02x\n", w_value);
490 /* REVISIT locking of cdc_filter. This assumes the UDC
491 * driver won't have a concurrent packet TX irq running on
492 * another CPU; or that if it does, this write is atomic...
493 */
494 ecm->port.cdc_filter = w_value;
495 value = 0;
496 break;
497
498 /* and optionally:
499 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
500 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
501 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
502 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
503 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
504 * case USB_CDC_GET_ETHERNET_STATISTIC:
505 */
506
507 default:
508 invalid:
509 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
510 ctrl->bRequestType, ctrl->bRequest,
511 w_value, w_index, w_length);
512 }
513
514 /* respond with data transfer or status phase? */
515 if (value >= 0) {
516 DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
517 ctrl->bRequestType, ctrl->bRequest,
518 w_value, w_index, w_length);
519 req->zero = 0;
520 req->length = value;
521 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
522 if (value < 0)
523 ERROR(cdev, "ecm req %02x.%02x response err %d\n",
524 ctrl->bRequestType, ctrl->bRequest,
525 value);
526 }
527
528 /* device either stalls (value < 0) or reports success */
529 return value;
530 }
531
532
533 static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
534 {
535 struct f_ecm *ecm = func_to_ecm(f);
536 struct usb_composite_dev *cdev = f->config->cdev;
537
538 /* Control interface has only altsetting 0 */
539 if (intf == ecm->ctrl_id) {
540 if (alt != 0)
541 goto fail;
542
543 if (ecm->notify->driver_data) {
544 VDBG(cdev, "reset ecm control %d\n", intf);
545 usb_ep_disable(ecm->notify);
546 }
547 if (!(ecm->notify->desc)) {
548 VDBG(cdev, "init ecm ctrl %d\n", intf);
549 if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
550 goto fail;
551 }
552 usb_ep_enable(ecm->notify);
553 ecm->notify->driver_data = ecm;
554
555 /* Data interface has two altsettings, 0 and 1 */
556 } else if (intf == ecm->data_id) {
557 if (alt > 1)
558 goto fail;
559
560 if (ecm->port.in_ep->driver_data) {
561 DBG(cdev, "reset ecm\n");
562 gether_disconnect(&ecm->port);
563 }
564
565 if (!ecm->port.in_ep->desc ||
566 !ecm->port.out_ep->desc) {
567 DBG(cdev, "init ecm\n");
568 if (config_ep_by_speed(cdev->gadget, f,
569 ecm->port.in_ep) ||
570 config_ep_by_speed(cdev->gadget, f,
571 ecm->port.out_ep)) {
572 ecm->port.in_ep->desc = NULL;
573 ecm->port.out_ep->desc = NULL;
574 goto fail;
575 }
576 }
577
578 /* CDC Ethernet only sends data in non-default altsettings.
579 * Changing altsettings resets filters, statistics, etc.
580 */
581 if (alt == 1) {
582 struct net_device *net;
583
584 /* Enable zlps by default for ECM conformance;
585 * override for musb_hdrc (avoids txdma ovhead).
586 */
587 ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
588 );
589 ecm->port.cdc_filter = DEFAULT_FILTER;
590 DBG(cdev, "activate ecm\n");
591 net = gether_connect(&ecm->port);
592 if (IS_ERR(net))
593 return PTR_ERR(net);
594 }
595
596 /* NOTE this can be a minor disagreement with the ECM spec,
597 * which says speed notifications will "always" follow
598 * connection notifications. But we allow one connect to
599 * follow another (if the first is in flight), and instead
600 * just guarantee that a speed notification is always sent.
601 */
602 ecm_notify(ecm);
603 } else
604 goto fail;
605
606 return 0;
607 fail:
608 return -EINVAL;
609 }
610
611 /* Because the data interface supports multiple altsettings,
612 * this ECM function *MUST* implement a get_alt() method.
613 */
614 static int ecm_get_alt(struct usb_function *f, unsigned intf)
615 {
616 struct f_ecm *ecm = func_to_ecm(f);
617
618 if (intf == ecm->ctrl_id)
619 return 0;
620 return ecm->port.in_ep->driver_data ? 1 : 0;
621 }
622
623 static void ecm_disable(struct usb_function *f)
624 {
625 struct f_ecm *ecm = func_to_ecm(f);
626 struct usb_composite_dev *cdev = f->config->cdev;
627
628 DBG(cdev, "ecm deactivated\n");
629
630 if (ecm->port.in_ep->driver_data)
631 gether_disconnect(&ecm->port);
632
633 if (ecm->notify->driver_data) {
634 usb_ep_disable(ecm->notify);
635 ecm->notify->driver_data = NULL;
636 ecm->notify->desc = NULL;
637 }
638 }
639
640 /*-------------------------------------------------------------------------*/
641
642 /*
643 * Callbacks let us notify the host about connect/disconnect when the
644 * net device is opened or closed.
645 *
646 * For testing, note that link states on this side include both opened
647 * and closed variants of:
648 *
649 * - disconnected/unconfigured
650 * - configured but inactive (data alt 0)
651 * - configured and active (data alt 1)
652 *
653 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
654 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
655 * imply the host is actually polling the notification endpoint, and
656 * likewise that "active" doesn't imply it's actually using the data
657 * endpoints for traffic.
658 */
659
660 static void ecm_open(struct gether *geth)
661 {
662 struct f_ecm *ecm = func_to_ecm(&geth->func);
663
664 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
665
666 ecm->is_open = true;
667 ecm_notify(ecm);
668 }
669
670 static void ecm_close(struct gether *geth)
671 {
672 struct f_ecm *ecm = func_to_ecm(&geth->func);
673
674 DBG(ecm->port.func.config->cdev, "%s\n", __func__);
675
676 ecm->is_open = false;
677 ecm_notify(ecm);
678 }
679
680 /*-------------------------------------------------------------------------*/
681
682 /* ethernet function driver setup/binding */
683
684 static int
685 ecm_bind(struct usb_configuration *c, struct usb_function *f)
686 {
687 struct usb_composite_dev *cdev = c->cdev;
688 struct f_ecm *ecm = func_to_ecm(f);
689 struct usb_string *us;
690 int status;
691 struct usb_ep *ep;
692
693 #ifndef USBF_ECM_INCLUDED
694 struct f_ecm_opts *ecm_opts;
695
696 if (!can_support_ecm(cdev->gadget))
697 return -EINVAL;
698
699 ecm_opts = container_of(f->fi, struct f_ecm_opts, func_inst);
700
701 /*
702 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
703 * configurations are bound in sequence with list_for_each_entry,
704 * in each configuration its functions are bound in sequence
705 * with list_for_each_entry, so we assume no race condition
706 * with regard to ecm_opts->bound access
707 */
708 if (!ecm_opts->bound) {
709 gether_set_gadget(ecm_opts->net, cdev->gadget);
710 status = gether_register_netdev(ecm_opts->net);
711 if (status)
712 return status;
713 ecm_opts->bound = true;
714 }
715 #endif
716 us = usb_gstrings_attach(cdev, ecm_strings,
717 ARRAY_SIZE(ecm_string_defs));
718 if (IS_ERR(us))
719 return PTR_ERR(us);
720 ecm_control_intf.iInterface = us[0].id;
721 ecm_data_intf.iInterface = us[2].id;
722 ecm_desc.iMACAddress = us[1].id;
723 ecm_iad_descriptor.iFunction = us[3].id;
724
725 /* allocate instance-specific interface IDs */
726 status = usb_interface_id(c, f);
727 if (status < 0)
728 goto fail;
729 ecm->ctrl_id = status;
730 ecm_iad_descriptor.bFirstInterface = status;
731
732 ecm_control_intf.bInterfaceNumber = status;
733 ecm_union_desc.bMasterInterface0 = status;
734
735 status = usb_interface_id(c, f);
736 if (status < 0)
737 goto fail;
738 ecm->data_id = status;
739
740 ecm_data_nop_intf.bInterfaceNumber = status;
741 ecm_data_intf.bInterfaceNumber = status;
742 ecm_union_desc.bSlaveInterface0 = status;
743
744 status = -ENODEV;
745
746 /* allocate instance-specific endpoints */
747 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
748 if (!ep)
749 goto fail;
750 ecm->port.in_ep = ep;
751 ep->driver_data = cdev; /* claim */
752
753 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
754 if (!ep)
755 goto fail;
756 ecm->port.out_ep = ep;
757 ep->driver_data = cdev; /* claim */
758
759 /* NOTE: a status/notification endpoint is *OPTIONAL* but we
760 * don't treat it that way. It's simpler, and some newer CDC
761 * profiles (wireless handsets) no longer treat it as optional.
762 */
763 ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
764 if (!ep)
765 goto fail;
766 ecm->notify = ep;
767 ep->driver_data = cdev; /* claim */
768
769 status = -ENOMEM;
770
771 /* allocate notification request and buffer */
772 ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
773 if (!ecm->notify_req)
774 goto fail;
775 ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
776 if (!ecm->notify_req->buf)
777 goto fail;
778 ecm->notify_req->context = ecm;
779 ecm->notify_req->complete = ecm_notify_complete;
780
781 /* support all relevant hardware speeds... we expect that when
782 * hardware is dual speed, all bulk-capable endpoints work at
783 * both speeds
784 */
785 hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
786 hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
787 hs_ecm_notify_desc.bEndpointAddress =
788 fs_ecm_notify_desc.bEndpointAddress;
789
790 ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
791 ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
792 ss_ecm_notify_desc.bEndpointAddress =
793 fs_ecm_notify_desc.bEndpointAddress;
794
795 status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
796 ecm_ss_function);
797 if (status)
798 goto fail;
799
800 /* NOTE: all that is done without knowing or caring about
801 * the network link ... which is unavailable to this code
802 * until we're activated via set_alt().
803 */
804
805 ecm->port.open = ecm_open;
806 ecm->port.close = ecm_close;
807
808 DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
809 gadget_is_superspeed(c->cdev->gadget) ? "super" :
810 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
811 ecm->port.in_ep->name, ecm->port.out_ep->name,
812 ecm->notify->name);
813 return 0;
814
815 fail:
816 if (ecm->notify_req) {
817 kfree(ecm->notify_req->buf);
818 usb_ep_free_request(ecm->notify, ecm->notify_req);
819 }
820
821 /* we might as well release our claims on endpoints */
822 if (ecm->notify)
823 ecm->notify->driver_data = NULL;
824 if (ecm->port.out_ep)
825 ecm->port.out_ep->driver_data = NULL;
826 if (ecm->port.in_ep)
827 ecm->port.in_ep->driver_data = NULL;
828
829 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
830
831 return status;
832 }
833
834 #ifdef USBF_ECM_INCLUDED
835
836 static void
837 ecm_old_unbind(struct usb_configuration *c, struct usb_function *f)
838 {
839 struct f_ecm *ecm = func_to_ecm(f);
840
841 DBG(c->cdev, "ecm unbind\n");
842
843 usb_free_all_descriptors(f);
844
845 kfree(ecm->notify_req->buf);
846 usb_ep_free_request(ecm->notify, ecm->notify_req);
847 kfree(ecm);
848 }
849
850 /**
851 * ecm_bind_config - add CDC Ethernet network link to a configuration
852 * @c: the configuration to support the network link
853 * @ethaddr: a buffer in which the ethernet address of the host side
854 * side of the link was recorded
855 * @dev: eth_dev structure
856 * Context: single threaded during gadget setup
857 *
858 * Returns zero on success, else negative errno.
859 *
860 * Caller must have called @gether_setup(). Caller is also responsible
861 * for calling @gether_cleanup() before module unload.
862 */
863 int
864 ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
865 struct eth_dev *dev)
866 {
867 struct f_ecm *ecm;
868 int status;
869
870 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
871 return -EINVAL;
872
873 /* allocate and initialize one new instance */
874 ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
875 if (!ecm)
876 return -ENOMEM;
877
878 /* export host's Ethernet address in CDC format */
879 snprintf(ecm->ethaddr, sizeof ecm->ethaddr, "%pm", ethaddr);
880 ecm_string_defs[1].s = ecm->ethaddr;
881
882 ecm->port.ioport = dev;
883 ecm->port.cdc_filter = DEFAULT_FILTER;
884
885 ecm->port.func.name = "cdc_ethernet";
886 /* descriptors are per-instance copies */
887 ecm->port.func.bind = ecm_bind;
888 ecm->port.func.unbind = ecm_old_unbind;
889 ecm->port.func.set_alt = ecm_set_alt;
890 ecm->port.func.get_alt = ecm_get_alt;
891 ecm->port.func.setup = ecm_setup;
892 ecm->port.func.disable = ecm_disable;
893
894 status = usb_add_function(c, &ecm->port.func);
895 if (status)
896 kfree(ecm);
897 return status;
898 }
899
900 #else
901
902 static void ecm_free_inst(struct usb_function_instance *f)
903 {
904 struct f_ecm_opts *opts;
905
906 opts = container_of(f, struct f_ecm_opts, func_inst);
907 if (opts->bound)
908 gether_cleanup(netdev_priv(opts->net));
909 else
910 free_netdev(opts->net);
911 kfree(opts);
912 }
913
914 static struct usb_function_instance *ecm_alloc_inst(void)
915 {
916 struct f_ecm_opts *opts;
917
918 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
919 if (!opts)
920 return ERR_PTR(-ENOMEM);
921
922 opts->func_inst.free_func_inst = ecm_free_inst;
923 opts->net = gether_setup_default();
924 if (IS_ERR(opts->net))
925 return ERR_PTR(PTR_ERR(opts->net));
926
927 return &opts->func_inst;
928 }
929
930 static void ecm_free(struct usb_function *f)
931 {
932 struct f_ecm *ecm;
933
934 ecm = func_to_ecm(f);
935 kfree(ecm);
936 }
937
938 static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
939 {
940 struct f_ecm *ecm = func_to_ecm(f);
941
942 DBG(c->cdev, "ecm unbind\n");
943
944 usb_free_all_descriptors(f);
945
946 kfree(ecm->notify_req->buf);
947 usb_ep_free_request(ecm->notify, ecm->notify_req);
948 }
949
950 struct usb_function *ecm_alloc(struct usb_function_instance *fi)
951 {
952 struct f_ecm *ecm;
953 struct f_ecm_opts *opts;
954 int status;
955
956 /* allocate and initialize one new instance */
957 ecm = kzalloc(sizeof(*ecm), GFP_KERNEL);
958 if (!ecm)
959 return ERR_PTR(-ENOMEM);
960
961 opts = container_of(fi, struct f_ecm_opts, func_inst);
962
963 /* export host's Ethernet address in CDC format */
964 status = gether_get_host_addr_cdc(opts->net, ecm->ethaddr,
965 sizeof(ecm->ethaddr));
966 if (status < 12) {
967 kfree(ecm);
968 return ERR_PTR(-EINVAL);
969 }
970 ecm_string_defs[1].s = ecm->ethaddr;
971
972 ecm->port.ioport = netdev_priv(opts->net);
973 ecm->port.cdc_filter = DEFAULT_FILTER;
974
975 ecm->port.func.name = "cdc_ethernet";
976 /* descriptors are per-instance copies */
977 ecm->port.func.bind = ecm_bind;
978 ecm->port.func.unbind = ecm_unbind;
979 ecm->port.func.set_alt = ecm_set_alt;
980 ecm->port.func.get_alt = ecm_get_alt;
981 ecm->port.func.setup = ecm_setup;
982 ecm->port.func.disable = ecm_disable;
983 ecm->port.func.free_func = ecm_free;
984
985 return &ecm->port.func;
986 }
987
988 DECLARE_USB_FUNCTION_INIT(ecm, ecm_alloc_inst, ecm_alloc);
989 MODULE_LICENSE("GPL");
990 MODULE_AUTHOR("David Brownell");
991
992 #endif
This page took 0.06579 seconds and 5 git commands to generate.