Merge branch 'timer/cleanup' into late/mvebu2
[deliverable/linux.git] / drivers / usb / gadget / f_ncm.c
1 /*
2 * f_ncm.c -- USB CDC Network (NCM) link function driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
6 *
7 * The driver borrows from f_ecm.c which is:
8 *
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2008 Nokia Corporation
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 */
17
18 #include <linux/kernel.h>
19 #include <linux/device.h>
20 #include <linux/etherdevice.h>
21 #include <linux/crc32.h>
22
23 #include <linux/usb/cdc.h>
24
25 #include "u_ether.h"
26
27 /*
28 * This function is a "CDC Network Control Model" (CDC NCM) Ethernet link.
29 * NCM is intended to be used with high-speed network attachments.
30 *
31 * Note that NCM requires the use of "alternate settings" for its data
32 * interface. This means that the set_alt() method has real work to do,
33 * and also means that a get_alt() method is required.
34 */
35
36 /* to trigger crc/non-crc ndp signature */
37
38 #define NCM_NDP_HDR_CRC_MASK 0x01000000
39 #define NCM_NDP_HDR_CRC 0x01000000
40 #define NCM_NDP_HDR_NOCRC 0x00000000
41
42 enum ncm_notify_state {
43 NCM_NOTIFY_NONE, /* don't notify */
44 NCM_NOTIFY_CONNECT, /* issue CONNECT next */
45 NCM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
46 };
47
48 struct f_ncm {
49 struct gether port;
50 u8 ctrl_id, data_id;
51
52 char ethaddr[14];
53
54 struct usb_ep *notify;
55 struct usb_request *notify_req;
56 u8 notify_state;
57 bool is_open;
58
59 struct ndp_parser_opts *parser_opts;
60 bool is_crc;
61
62 /*
63 * for notification, it is accessed from both
64 * callback and ethernet open/close
65 */
66 spinlock_t lock;
67 };
68
69 static inline struct f_ncm *func_to_ncm(struct usb_function *f)
70 {
71 return container_of(f, struct f_ncm, port.func);
72 }
73
74 /* peak (theoretical) bulk transfer rate in bits-per-second */
75 static inline unsigned ncm_bitrate(struct usb_gadget *g)
76 {
77 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
78 return 13 * 512 * 8 * 1000 * 8;
79 else
80 return 19 * 64 * 1 * 1000 * 8;
81 }
82
83 /*-------------------------------------------------------------------------*/
84
85 /*
86 * We cannot group frames so use just the minimal size which ok to put
87 * one max-size ethernet frame.
88 * If the host can group frames, allow it to do that, 16K is selected,
89 * because it's used by default by the current linux host driver
90 */
91 #define NTB_DEFAULT_IN_SIZE USB_CDC_NCM_NTB_MIN_IN_SIZE
92 #define NTB_OUT_SIZE 16384
93
94 /*
95 * skbs of size less than that will not be aligned
96 * to NCM's dwNtbInMaxSize to save bus bandwidth
97 */
98
99 #define MAX_TX_NONFIXED (512 * 3)
100
101 #define FORMATS_SUPPORTED (USB_CDC_NCM_NTB16_SUPPORTED | \
102 USB_CDC_NCM_NTB32_SUPPORTED)
103
104 static struct usb_cdc_ncm_ntb_parameters ntb_parameters = {
105 .wLength = cpu_to_le16(sizeof(ntb_parameters)),
106 .bmNtbFormatsSupported = cpu_to_le16(FORMATS_SUPPORTED),
107 .dwNtbInMaxSize = cpu_to_le32(NTB_DEFAULT_IN_SIZE),
108 .wNdpInDivisor = cpu_to_le16(4),
109 .wNdpInPayloadRemainder = cpu_to_le16(0),
110 .wNdpInAlignment = cpu_to_le16(4),
111
112 .dwNtbOutMaxSize = cpu_to_le32(NTB_OUT_SIZE),
113 .wNdpOutDivisor = cpu_to_le16(4),
114 .wNdpOutPayloadRemainder = cpu_to_le16(0),
115 .wNdpOutAlignment = cpu_to_le16(4),
116 };
117
118 /*
119 * Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
120 * packet, to simplify cancellation; and a big transfer interval, to
121 * waste less bandwidth.
122 */
123
124 #define NCM_STATUS_INTERVAL_MS 32
125 #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
126
127 static struct usb_interface_assoc_descriptor ncm_iad_desc __initdata = {
128 .bLength = sizeof ncm_iad_desc,
129 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
130
131 /* .bFirstInterface = DYNAMIC, */
132 .bInterfaceCount = 2, /* control + data */
133 .bFunctionClass = USB_CLASS_COMM,
134 .bFunctionSubClass = USB_CDC_SUBCLASS_NCM,
135 .bFunctionProtocol = USB_CDC_PROTO_NONE,
136 /* .iFunction = DYNAMIC */
137 };
138
139 /* interface descriptor: */
140
141 static struct usb_interface_descriptor ncm_control_intf __initdata = {
142 .bLength = sizeof ncm_control_intf,
143 .bDescriptorType = USB_DT_INTERFACE,
144
145 /* .bInterfaceNumber = DYNAMIC */
146 .bNumEndpoints = 1,
147 .bInterfaceClass = USB_CLASS_COMM,
148 .bInterfaceSubClass = USB_CDC_SUBCLASS_NCM,
149 .bInterfaceProtocol = USB_CDC_PROTO_NONE,
150 /* .iInterface = DYNAMIC */
151 };
152
153 static struct usb_cdc_header_desc ncm_header_desc __initdata = {
154 .bLength = sizeof ncm_header_desc,
155 .bDescriptorType = USB_DT_CS_INTERFACE,
156 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
157
158 .bcdCDC = cpu_to_le16(0x0110),
159 };
160
161 static struct usb_cdc_union_desc ncm_union_desc __initdata = {
162 .bLength = sizeof(ncm_union_desc),
163 .bDescriptorType = USB_DT_CS_INTERFACE,
164 .bDescriptorSubType = USB_CDC_UNION_TYPE,
165 /* .bMasterInterface0 = DYNAMIC */
166 /* .bSlaveInterface0 = DYNAMIC */
167 };
168
169 static struct usb_cdc_ether_desc ecm_desc __initdata = {
170 .bLength = sizeof ecm_desc,
171 .bDescriptorType = USB_DT_CS_INTERFACE,
172 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
173
174 /* this descriptor actually adds value, surprise! */
175 /* .iMACAddress = DYNAMIC */
176 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
177 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
178 .wNumberMCFilters = cpu_to_le16(0),
179 .bNumberPowerFilters = 0,
180 };
181
182 #define NCAPS (USB_CDC_NCM_NCAP_ETH_FILTER | USB_CDC_NCM_NCAP_CRC_MODE)
183
184 static struct usb_cdc_ncm_desc ncm_desc __initdata = {
185 .bLength = sizeof ncm_desc,
186 .bDescriptorType = USB_DT_CS_INTERFACE,
187 .bDescriptorSubType = USB_CDC_NCM_TYPE,
188
189 .bcdNcmVersion = cpu_to_le16(0x0100),
190 /* can process SetEthernetPacketFilter */
191 .bmNetworkCapabilities = NCAPS,
192 };
193
194 /* the default data interface has no endpoints ... */
195
196 static struct usb_interface_descriptor ncm_data_nop_intf __initdata = {
197 .bLength = sizeof ncm_data_nop_intf,
198 .bDescriptorType = USB_DT_INTERFACE,
199
200 .bInterfaceNumber = 1,
201 .bAlternateSetting = 0,
202 .bNumEndpoints = 0,
203 .bInterfaceClass = USB_CLASS_CDC_DATA,
204 .bInterfaceSubClass = 0,
205 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
206 /* .iInterface = DYNAMIC */
207 };
208
209 /* ... but the "real" data interface has two bulk endpoints */
210
211 static struct usb_interface_descriptor ncm_data_intf __initdata = {
212 .bLength = sizeof ncm_data_intf,
213 .bDescriptorType = USB_DT_INTERFACE,
214
215 .bInterfaceNumber = 1,
216 .bAlternateSetting = 1,
217 .bNumEndpoints = 2,
218 .bInterfaceClass = USB_CLASS_CDC_DATA,
219 .bInterfaceSubClass = 0,
220 .bInterfaceProtocol = USB_CDC_NCM_PROTO_NTB,
221 /* .iInterface = DYNAMIC */
222 };
223
224 /* full speed support: */
225
226 static struct usb_endpoint_descriptor fs_ncm_notify_desc __initdata = {
227 .bLength = USB_DT_ENDPOINT_SIZE,
228 .bDescriptorType = USB_DT_ENDPOINT,
229
230 .bEndpointAddress = USB_DIR_IN,
231 .bmAttributes = USB_ENDPOINT_XFER_INT,
232 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
233 .bInterval = NCM_STATUS_INTERVAL_MS,
234 };
235
236 static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata = {
237 .bLength = USB_DT_ENDPOINT_SIZE,
238 .bDescriptorType = USB_DT_ENDPOINT,
239
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_XFER_BULK,
242 };
243
244 static struct usb_endpoint_descriptor fs_ncm_out_desc __initdata = {
245 .bLength = USB_DT_ENDPOINT_SIZE,
246 .bDescriptorType = USB_DT_ENDPOINT,
247
248 .bEndpointAddress = USB_DIR_OUT,
249 .bmAttributes = USB_ENDPOINT_XFER_BULK,
250 };
251
252 static struct usb_descriptor_header *ncm_fs_function[] __initdata = {
253 (struct usb_descriptor_header *) &ncm_iad_desc,
254 /* CDC NCM control descriptors */
255 (struct usb_descriptor_header *) &ncm_control_intf,
256 (struct usb_descriptor_header *) &ncm_header_desc,
257 (struct usb_descriptor_header *) &ncm_union_desc,
258 (struct usb_descriptor_header *) &ecm_desc,
259 (struct usb_descriptor_header *) &ncm_desc,
260 (struct usb_descriptor_header *) &fs_ncm_notify_desc,
261 /* data interface, altsettings 0 and 1 */
262 (struct usb_descriptor_header *) &ncm_data_nop_intf,
263 (struct usb_descriptor_header *) &ncm_data_intf,
264 (struct usb_descriptor_header *) &fs_ncm_in_desc,
265 (struct usb_descriptor_header *) &fs_ncm_out_desc,
266 NULL,
267 };
268
269 /* high speed support: */
270
271 static struct usb_endpoint_descriptor hs_ncm_notify_desc __initdata = {
272 .bLength = USB_DT_ENDPOINT_SIZE,
273 .bDescriptorType = USB_DT_ENDPOINT,
274
275 .bEndpointAddress = USB_DIR_IN,
276 .bmAttributes = USB_ENDPOINT_XFER_INT,
277 .wMaxPacketSize = cpu_to_le16(NCM_STATUS_BYTECOUNT),
278 .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS),
279 };
280 static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata = {
281 .bLength = USB_DT_ENDPOINT_SIZE,
282 .bDescriptorType = USB_DT_ENDPOINT,
283
284 .bEndpointAddress = USB_DIR_IN,
285 .bmAttributes = USB_ENDPOINT_XFER_BULK,
286 .wMaxPacketSize = cpu_to_le16(512),
287 };
288
289 static struct usb_endpoint_descriptor hs_ncm_out_desc __initdata = {
290 .bLength = USB_DT_ENDPOINT_SIZE,
291 .bDescriptorType = USB_DT_ENDPOINT,
292
293 .bEndpointAddress = USB_DIR_OUT,
294 .bmAttributes = USB_ENDPOINT_XFER_BULK,
295 .wMaxPacketSize = cpu_to_le16(512),
296 };
297
298 static struct usb_descriptor_header *ncm_hs_function[] __initdata = {
299 (struct usb_descriptor_header *) &ncm_iad_desc,
300 /* CDC NCM control descriptors */
301 (struct usb_descriptor_header *) &ncm_control_intf,
302 (struct usb_descriptor_header *) &ncm_header_desc,
303 (struct usb_descriptor_header *) &ncm_union_desc,
304 (struct usb_descriptor_header *) &ecm_desc,
305 (struct usb_descriptor_header *) &ncm_desc,
306 (struct usb_descriptor_header *) &hs_ncm_notify_desc,
307 /* data interface, altsettings 0 and 1 */
308 (struct usb_descriptor_header *) &ncm_data_nop_intf,
309 (struct usb_descriptor_header *) &ncm_data_intf,
310 (struct usb_descriptor_header *) &hs_ncm_in_desc,
311 (struct usb_descriptor_header *) &hs_ncm_out_desc,
312 NULL,
313 };
314
315 /* string descriptors: */
316
317 #define STRING_CTRL_IDX 0
318 #define STRING_MAC_IDX 1
319 #define STRING_DATA_IDX 2
320 #define STRING_IAD_IDX 3
321
322 static struct usb_string ncm_string_defs[] = {
323 [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)",
324 [STRING_MAC_IDX].s = "",
325 [STRING_DATA_IDX].s = "CDC Network Data",
326 [STRING_IAD_IDX].s = "CDC NCM",
327 { } /* end of list */
328 };
329
330 static struct usb_gadget_strings ncm_string_table = {
331 .language = 0x0409, /* en-us */
332 .strings = ncm_string_defs,
333 };
334
335 static struct usb_gadget_strings *ncm_strings[] = {
336 &ncm_string_table,
337 NULL,
338 };
339
340 /*
341 * Here are options for NCM Datagram Pointer table (NDP) parser.
342 * There are 2 different formats: NDP16 and NDP32 in the spec (ch. 3),
343 * in NDP16 offsets and sizes fields are 1 16bit word wide,
344 * in NDP32 -- 2 16bit words wide. Also signatures are different.
345 * To make the parser code the same, put the differences in the structure,
346 * and switch pointers to the structures when the format is changed.
347 */
348
349 struct ndp_parser_opts {
350 u32 nth_sign;
351 u32 ndp_sign;
352 unsigned nth_size;
353 unsigned ndp_size;
354 unsigned ndplen_align;
355 /* sizes in u16 units */
356 unsigned dgram_item_len; /* index or length */
357 unsigned block_length;
358 unsigned fp_index;
359 unsigned reserved1;
360 unsigned reserved2;
361 unsigned next_fp_index;
362 };
363
364 #define INIT_NDP16_OPTS { \
365 .nth_sign = USB_CDC_NCM_NTH16_SIGN, \
366 .ndp_sign = USB_CDC_NCM_NDP16_NOCRC_SIGN, \
367 .nth_size = sizeof(struct usb_cdc_ncm_nth16), \
368 .ndp_size = sizeof(struct usb_cdc_ncm_ndp16), \
369 .ndplen_align = 4, \
370 .dgram_item_len = 1, \
371 .block_length = 1, \
372 .fp_index = 1, \
373 .reserved1 = 0, \
374 .reserved2 = 0, \
375 .next_fp_index = 1, \
376 }
377
378
379 #define INIT_NDP32_OPTS { \
380 .nth_sign = USB_CDC_NCM_NTH32_SIGN, \
381 .ndp_sign = USB_CDC_NCM_NDP32_NOCRC_SIGN, \
382 .nth_size = sizeof(struct usb_cdc_ncm_nth32), \
383 .ndp_size = sizeof(struct usb_cdc_ncm_ndp32), \
384 .ndplen_align = 8, \
385 .dgram_item_len = 2, \
386 .block_length = 2, \
387 .fp_index = 2, \
388 .reserved1 = 1, \
389 .reserved2 = 2, \
390 .next_fp_index = 2, \
391 }
392
393 static struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS;
394 static struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS;
395
396 static inline void put_ncm(__le16 **p, unsigned size, unsigned val)
397 {
398 switch (size) {
399 case 1:
400 put_unaligned_le16((u16)val, *p);
401 break;
402 case 2:
403 put_unaligned_le32((u32)val, *p);
404
405 break;
406 default:
407 BUG();
408 }
409
410 *p += size;
411 }
412
413 static inline unsigned get_ncm(__le16 **p, unsigned size)
414 {
415 unsigned tmp;
416
417 switch (size) {
418 case 1:
419 tmp = get_unaligned_le16(*p);
420 break;
421 case 2:
422 tmp = get_unaligned_le32(*p);
423 break;
424 default:
425 BUG();
426 }
427
428 *p += size;
429 return tmp;
430 }
431
432 /*-------------------------------------------------------------------------*/
433
434 static inline void ncm_reset_values(struct f_ncm *ncm)
435 {
436 ncm->parser_opts = &ndp16_opts;
437 ncm->is_crc = false;
438 ncm->port.cdc_filter = DEFAULT_FILTER;
439
440 /* doesn't make sense for ncm, fixed size used */
441 ncm->port.header_len = 0;
442
443 ncm->port.fixed_out_len = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
444 ncm->port.fixed_in_len = NTB_DEFAULT_IN_SIZE;
445 }
446
447 /*
448 * Context: ncm->lock held
449 */
450 static void ncm_do_notify(struct f_ncm *ncm)
451 {
452 struct usb_request *req = ncm->notify_req;
453 struct usb_cdc_notification *event;
454 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
455 __le32 *data;
456 int status;
457
458 /* notification already in flight? */
459 if (!req)
460 return;
461
462 event = req->buf;
463 switch (ncm->notify_state) {
464 case NCM_NOTIFY_NONE:
465 return;
466
467 case NCM_NOTIFY_CONNECT:
468 event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
469 if (ncm->is_open)
470 event->wValue = cpu_to_le16(1);
471 else
472 event->wValue = cpu_to_le16(0);
473 event->wLength = 0;
474 req->length = sizeof *event;
475
476 DBG(cdev, "notify connect %s\n",
477 ncm->is_open ? "true" : "false");
478 ncm->notify_state = NCM_NOTIFY_NONE;
479 break;
480
481 case NCM_NOTIFY_SPEED:
482 event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
483 event->wValue = cpu_to_le16(0);
484 event->wLength = cpu_to_le16(8);
485 req->length = NCM_STATUS_BYTECOUNT;
486
487 /* SPEED_CHANGE data is up/down speeds in bits/sec */
488 data = req->buf + sizeof *event;
489 data[0] = cpu_to_le32(ncm_bitrate(cdev->gadget));
490 data[1] = data[0];
491
492 DBG(cdev, "notify speed %d\n", ncm_bitrate(cdev->gadget));
493 ncm->notify_state = NCM_NOTIFY_CONNECT;
494 break;
495 }
496 event->bmRequestType = 0xA1;
497 event->wIndex = cpu_to_le16(ncm->ctrl_id);
498
499 ncm->notify_req = NULL;
500 /*
501 * In double buffering if there is a space in FIFO,
502 * completion callback can be called right after the call,
503 * so unlocking
504 */
505 spin_unlock(&ncm->lock);
506 status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
507 spin_lock(&ncm->lock);
508 if (status < 0) {
509 ncm->notify_req = req;
510 DBG(cdev, "notify --> %d\n", status);
511 }
512 }
513
514 /*
515 * Context: ncm->lock held
516 */
517 static void ncm_notify(struct f_ncm *ncm)
518 {
519 /*
520 * NOTE on most versions of Linux, host side cdc-ethernet
521 * won't listen for notifications until its netdevice opens.
522 * The first notification then sits in the FIFO for a long
523 * time, and the second one is queued.
524 *
525 * If ncm_notify() is called before the second (CONNECT)
526 * notification is sent, then it will reset to send the SPEED
527 * notificaion again (and again, and again), but it's not a problem
528 */
529 ncm->notify_state = NCM_NOTIFY_SPEED;
530 ncm_do_notify(ncm);
531 }
532
533 static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
534 {
535 struct f_ncm *ncm = req->context;
536 struct usb_composite_dev *cdev = ncm->port.func.config->cdev;
537 struct usb_cdc_notification *event = req->buf;
538
539 spin_lock(&ncm->lock);
540 switch (req->status) {
541 case 0:
542 VDBG(cdev, "Notification %02x sent\n",
543 event->bNotificationType);
544 break;
545 case -ECONNRESET:
546 case -ESHUTDOWN:
547 ncm->notify_state = NCM_NOTIFY_NONE;
548 break;
549 default:
550 DBG(cdev, "event %02x --> %d\n",
551 event->bNotificationType, req->status);
552 break;
553 }
554 ncm->notify_req = req;
555 ncm_do_notify(ncm);
556 spin_unlock(&ncm->lock);
557 }
558
559 static void ncm_ep0out_complete(struct usb_ep *ep, struct usb_request *req)
560 {
561 /* now for SET_NTB_INPUT_SIZE only */
562 unsigned in_size;
563 struct usb_function *f = req->context;
564 struct f_ncm *ncm = func_to_ncm(f);
565 struct usb_composite_dev *cdev = ep->driver_data;
566
567 req->context = NULL;
568 if (req->status || req->actual != req->length) {
569 DBG(cdev, "Bad control-OUT transfer\n");
570 goto invalid;
571 }
572
573 in_size = get_unaligned_le32(req->buf);
574 if (in_size < USB_CDC_NCM_NTB_MIN_IN_SIZE ||
575 in_size > le32_to_cpu(ntb_parameters.dwNtbInMaxSize)) {
576 DBG(cdev, "Got wrong INPUT SIZE (%d) from host\n", in_size);
577 goto invalid;
578 }
579
580 ncm->port.fixed_in_len = in_size;
581 VDBG(cdev, "Set NTB INPUT SIZE %d\n", in_size);
582 return;
583
584 invalid:
585 usb_ep_set_halt(ep);
586 return;
587 }
588
589 static int ncm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
590 {
591 struct f_ncm *ncm = func_to_ncm(f);
592 struct usb_composite_dev *cdev = f->config->cdev;
593 struct usb_request *req = cdev->req;
594 int value = -EOPNOTSUPP;
595 u16 w_index = le16_to_cpu(ctrl->wIndex);
596 u16 w_value = le16_to_cpu(ctrl->wValue);
597 u16 w_length = le16_to_cpu(ctrl->wLength);
598
599 /*
600 * composite driver infrastructure handles everything except
601 * CDC class messages; interface activation uses set_alt().
602 */
603 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
604 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
605 | USB_CDC_SET_ETHERNET_PACKET_FILTER:
606 /*
607 * see 6.2.30: no data, wIndex = interface,
608 * wValue = packet filter bitmap
609 */
610 if (w_length != 0 || w_index != ncm->ctrl_id)
611 goto invalid;
612 DBG(cdev, "packet filter %02x\n", w_value);
613 /*
614 * REVISIT locking of cdc_filter. This assumes the UDC
615 * driver won't have a concurrent packet TX irq running on
616 * another CPU; or that if it does, this write is atomic...
617 */
618 ncm->port.cdc_filter = w_value;
619 value = 0;
620 break;
621 /*
622 * and optionally:
623 * case USB_CDC_SEND_ENCAPSULATED_COMMAND:
624 * case USB_CDC_GET_ENCAPSULATED_RESPONSE:
625 * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
626 * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
627 * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
628 * case USB_CDC_GET_ETHERNET_STATISTIC:
629 */
630
631 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
632 | USB_CDC_GET_NTB_PARAMETERS:
633
634 if (w_length == 0 || w_value != 0 || w_index != ncm->ctrl_id)
635 goto invalid;
636 value = w_length > sizeof ntb_parameters ?
637 sizeof ntb_parameters : w_length;
638 memcpy(req->buf, &ntb_parameters, value);
639 VDBG(cdev, "Host asked NTB parameters\n");
640 break;
641
642 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
643 | USB_CDC_GET_NTB_INPUT_SIZE:
644
645 if (w_length < 4 || w_value != 0 || w_index != ncm->ctrl_id)
646 goto invalid;
647 put_unaligned_le32(ncm->port.fixed_in_len, req->buf);
648 value = 4;
649 VDBG(cdev, "Host asked INPUT SIZE, sending %d\n",
650 ncm->port.fixed_in_len);
651 break;
652
653 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
654 | USB_CDC_SET_NTB_INPUT_SIZE:
655 {
656 if (w_length != 4 || w_value != 0 || w_index != ncm->ctrl_id)
657 goto invalid;
658 req->complete = ncm_ep0out_complete;
659 req->length = w_length;
660 req->context = f;
661
662 value = req->length;
663 break;
664 }
665
666 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
667 | USB_CDC_GET_NTB_FORMAT:
668 {
669 uint16_t format;
670
671 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
672 goto invalid;
673 format = (ncm->parser_opts == &ndp16_opts) ? 0x0000 : 0x0001;
674 put_unaligned_le16(format, req->buf);
675 value = 2;
676 VDBG(cdev, "Host asked NTB FORMAT, sending %d\n", format);
677 break;
678 }
679
680 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
681 | USB_CDC_SET_NTB_FORMAT:
682 {
683 if (w_length != 0 || w_index != ncm->ctrl_id)
684 goto invalid;
685 switch (w_value) {
686 case 0x0000:
687 ncm->parser_opts = &ndp16_opts;
688 DBG(cdev, "NCM16 selected\n");
689 break;
690 case 0x0001:
691 ncm->parser_opts = &ndp32_opts;
692 DBG(cdev, "NCM32 selected\n");
693 break;
694 default:
695 goto invalid;
696 }
697 value = 0;
698 break;
699 }
700 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
701 | USB_CDC_GET_CRC_MODE:
702 {
703 uint16_t is_crc;
704
705 if (w_length < 2 || w_value != 0 || w_index != ncm->ctrl_id)
706 goto invalid;
707 is_crc = ncm->is_crc ? 0x0001 : 0x0000;
708 put_unaligned_le16(is_crc, req->buf);
709 value = 2;
710 VDBG(cdev, "Host asked CRC MODE, sending %d\n", is_crc);
711 break;
712 }
713
714 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
715 | USB_CDC_SET_CRC_MODE:
716 {
717 int ndp_hdr_crc = 0;
718
719 if (w_length != 0 || w_index != ncm->ctrl_id)
720 goto invalid;
721 switch (w_value) {
722 case 0x0000:
723 ncm->is_crc = false;
724 ndp_hdr_crc = NCM_NDP_HDR_NOCRC;
725 DBG(cdev, "non-CRC mode selected\n");
726 break;
727 case 0x0001:
728 ncm->is_crc = true;
729 ndp_hdr_crc = NCM_NDP_HDR_CRC;
730 DBG(cdev, "CRC mode selected\n");
731 break;
732 default:
733 goto invalid;
734 }
735 ncm->parser_opts->ndp_sign &= ~NCM_NDP_HDR_CRC_MASK;
736 ncm->parser_opts->ndp_sign |= ndp_hdr_crc;
737 value = 0;
738 break;
739 }
740
741 /* and disabled in ncm descriptor: */
742 /* case USB_CDC_GET_NET_ADDRESS: */
743 /* case USB_CDC_SET_NET_ADDRESS: */
744 /* case USB_CDC_GET_MAX_DATAGRAM_SIZE: */
745 /* case USB_CDC_SET_MAX_DATAGRAM_SIZE: */
746
747 default:
748 invalid:
749 DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
750 ctrl->bRequestType, ctrl->bRequest,
751 w_value, w_index, w_length);
752 }
753
754 /* respond with data transfer or status phase? */
755 if (value >= 0) {
756 DBG(cdev, "ncm req%02x.%02x v%04x i%04x l%d\n",
757 ctrl->bRequestType, ctrl->bRequest,
758 w_value, w_index, w_length);
759 req->zero = 0;
760 req->length = value;
761 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
762 if (value < 0)
763 ERROR(cdev, "ncm req %02x.%02x response err %d\n",
764 ctrl->bRequestType, ctrl->bRequest,
765 value);
766 }
767
768 /* device either stalls (value < 0) or reports success */
769 return value;
770 }
771
772
773 static int ncm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
774 {
775 struct f_ncm *ncm = func_to_ncm(f);
776 struct usb_composite_dev *cdev = f->config->cdev;
777
778 /* Control interface has only altsetting 0 */
779 if (intf == ncm->ctrl_id) {
780 if (alt != 0)
781 goto fail;
782
783 if (ncm->notify->driver_data) {
784 DBG(cdev, "reset ncm control %d\n", intf);
785 usb_ep_disable(ncm->notify);
786 }
787
788 if (!(ncm->notify->desc)) {
789 DBG(cdev, "init ncm ctrl %d\n", intf);
790 if (config_ep_by_speed(cdev->gadget, f, ncm->notify))
791 goto fail;
792 }
793 usb_ep_enable(ncm->notify);
794 ncm->notify->driver_data = ncm;
795
796 /* Data interface has two altsettings, 0 and 1 */
797 } else if (intf == ncm->data_id) {
798 if (alt > 1)
799 goto fail;
800
801 if (ncm->port.in_ep->driver_data) {
802 DBG(cdev, "reset ncm\n");
803 gether_disconnect(&ncm->port);
804 ncm_reset_values(ncm);
805 }
806
807 /*
808 * CDC Network only sends data in non-default altsettings.
809 * Changing altsettings resets filters, statistics, etc.
810 */
811 if (alt == 1) {
812 struct net_device *net;
813
814 if (!ncm->port.in_ep->desc ||
815 !ncm->port.out_ep->desc) {
816 DBG(cdev, "init ncm\n");
817 if (config_ep_by_speed(cdev->gadget, f,
818 ncm->port.in_ep) ||
819 config_ep_by_speed(cdev->gadget, f,
820 ncm->port.out_ep)) {
821 ncm->port.in_ep->desc = NULL;
822 ncm->port.out_ep->desc = NULL;
823 goto fail;
824 }
825 }
826
827 /* TODO */
828 /* Enable zlps by default for NCM conformance;
829 * override for musb_hdrc (avoids txdma ovhead)
830 */
831 ncm->port.is_zlp_ok = !(
832 gadget_is_musbhdrc(cdev->gadget)
833 );
834 ncm->port.cdc_filter = DEFAULT_FILTER;
835 DBG(cdev, "activate ncm\n");
836 net = gether_connect(&ncm->port);
837 if (IS_ERR(net))
838 return PTR_ERR(net);
839 }
840
841 spin_lock(&ncm->lock);
842 ncm_notify(ncm);
843 spin_unlock(&ncm->lock);
844 } else
845 goto fail;
846
847 return 0;
848 fail:
849 return -EINVAL;
850 }
851
852 /*
853 * Because the data interface supports multiple altsettings,
854 * this NCM function *MUST* implement a get_alt() method.
855 */
856 static int ncm_get_alt(struct usb_function *f, unsigned intf)
857 {
858 struct f_ncm *ncm = func_to_ncm(f);
859
860 if (intf == ncm->ctrl_id)
861 return 0;
862 return ncm->port.in_ep->driver_data ? 1 : 0;
863 }
864
865 static struct sk_buff *ncm_wrap_ntb(struct gether *port,
866 struct sk_buff *skb)
867 {
868 struct f_ncm *ncm = func_to_ncm(&port->func);
869 struct sk_buff *skb2;
870 int ncb_len = 0;
871 __le16 *tmp;
872 int div;
873 int rem;
874 int pad;
875 int ndp_align;
876 int ndp_pad;
877 unsigned max_size = ncm->port.fixed_in_len;
878 struct ndp_parser_opts *opts = ncm->parser_opts;
879 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
880
881 div = le16_to_cpu(ntb_parameters.wNdpInDivisor);
882 rem = le16_to_cpu(ntb_parameters.wNdpInPayloadRemainder);
883 ndp_align = le16_to_cpu(ntb_parameters.wNdpInAlignment);
884
885 ncb_len += opts->nth_size;
886 ndp_pad = ALIGN(ncb_len, ndp_align) - ncb_len;
887 ncb_len += ndp_pad;
888 ncb_len += opts->ndp_size;
889 ncb_len += 2 * 2 * opts->dgram_item_len; /* Datagram entry */
890 ncb_len += 2 * 2 * opts->dgram_item_len; /* Zero datagram entry */
891 pad = ALIGN(ncb_len, div) + rem - ncb_len;
892 ncb_len += pad;
893
894 if (ncb_len + skb->len + crc_len > max_size) {
895 dev_kfree_skb_any(skb);
896 return NULL;
897 }
898
899 skb2 = skb_copy_expand(skb, ncb_len,
900 max_size - skb->len - ncb_len - crc_len,
901 GFP_ATOMIC);
902 dev_kfree_skb_any(skb);
903 if (!skb2)
904 return NULL;
905
906 skb = skb2;
907
908 tmp = (void *) skb_push(skb, ncb_len);
909 memset(tmp, 0, ncb_len);
910
911 put_unaligned_le32(opts->nth_sign, tmp); /* dwSignature */
912 tmp += 2;
913 /* wHeaderLength */
914 put_unaligned_le16(opts->nth_size, tmp++);
915 tmp++; /* skip wSequence */
916 put_ncm(&tmp, opts->block_length, skb->len); /* (d)wBlockLength */
917 /* (d)wFpIndex */
918 /* the first pointer is right after the NTH + align */
919 put_ncm(&tmp, opts->fp_index, opts->nth_size + ndp_pad);
920
921 tmp = (void *)tmp + ndp_pad;
922
923 /* NDP */
924 put_unaligned_le32(opts->ndp_sign, tmp); /* dwSignature */
925 tmp += 2;
926 /* wLength */
927 put_unaligned_le16(ncb_len - opts->nth_size - pad, tmp++);
928
929 tmp += opts->reserved1;
930 tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
931 tmp += opts->reserved2;
932
933 if (ncm->is_crc) {
934 uint32_t crc;
935
936 crc = ~crc32_le(~0,
937 skb->data + ncb_len,
938 skb->len - ncb_len);
939 put_unaligned_le32(crc, skb->data + skb->len);
940 skb_put(skb, crc_len);
941 }
942
943 /* (d)wDatagramIndex[0] */
944 put_ncm(&tmp, opts->dgram_item_len, ncb_len);
945 /* (d)wDatagramLength[0] */
946 put_ncm(&tmp, opts->dgram_item_len, skb->len - ncb_len);
947 /* (d)wDatagramIndex[1] and (d)wDatagramLength[1] already zeroed */
948
949 if (skb->len > MAX_TX_NONFIXED)
950 memset(skb_put(skb, max_size - skb->len),
951 0, max_size - skb->len);
952
953 return skb;
954 }
955
956 static int ncm_unwrap_ntb(struct gether *port,
957 struct sk_buff *skb,
958 struct sk_buff_head *list)
959 {
960 struct f_ncm *ncm = func_to_ncm(&port->func);
961 __le16 *tmp = (void *) skb->data;
962 unsigned index, index2;
963 unsigned dg_len, dg_len2;
964 unsigned ndp_len;
965 struct sk_buff *skb2;
966 int ret = -EINVAL;
967 unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
968 struct ndp_parser_opts *opts = ncm->parser_opts;
969 unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
970 int dgram_counter;
971
972 /* dwSignature */
973 if (get_unaligned_le32(tmp) != opts->nth_sign) {
974 INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
975 skb->len);
976 print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
977 skb->data, 32, false);
978
979 goto err;
980 }
981 tmp += 2;
982 /* wHeaderLength */
983 if (get_unaligned_le16(tmp++) != opts->nth_size) {
984 INFO(port->func.config->cdev, "Wrong NTB headersize\n");
985 goto err;
986 }
987 tmp++; /* skip wSequence */
988
989 /* (d)wBlockLength */
990 if (get_ncm(&tmp, opts->block_length) > max_size) {
991 INFO(port->func.config->cdev, "OUT size exceeded\n");
992 goto err;
993 }
994
995 index = get_ncm(&tmp, opts->fp_index);
996 /* NCM 3.2 */
997 if (((index % 4) != 0) && (index < opts->nth_size)) {
998 INFO(port->func.config->cdev, "Bad index: %x\n",
999 index);
1000 goto err;
1001 }
1002
1003 /* walk through NDP */
1004 tmp = ((void *)skb->data) + index;
1005 if (get_unaligned_le32(tmp) != opts->ndp_sign) {
1006 INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1007 goto err;
1008 }
1009 tmp += 2;
1010
1011 ndp_len = get_unaligned_le16(tmp++);
1012 /*
1013 * NCM 3.3.1
1014 * entry is 2 items
1015 * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1016 * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1017 */
1018 if ((ndp_len < opts->ndp_size + 2 * 2 * (opts->dgram_item_len * 2))
1019 || (ndp_len % opts->ndplen_align != 0)) {
1020 INFO(port->func.config->cdev, "Bad NDP length: %x\n", ndp_len);
1021 goto err;
1022 }
1023 tmp += opts->reserved1;
1024 tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
1025 tmp += opts->reserved2;
1026
1027 ndp_len -= opts->ndp_size;
1028 index2 = get_ncm(&tmp, opts->dgram_item_len);
1029 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1030 dgram_counter = 0;
1031
1032 do {
1033 index = index2;
1034 dg_len = dg_len2;
1035 if (dg_len < 14 + crc_len) { /* ethernet header + crc */
1036 INFO(port->func.config->cdev, "Bad dgram length: %x\n",
1037 dg_len);
1038 goto err;
1039 }
1040 if (ncm->is_crc) {
1041 uint32_t crc, crc2;
1042
1043 crc = get_unaligned_le32(skb->data +
1044 index + dg_len - crc_len);
1045 crc2 = ~crc32_le(~0,
1046 skb->data + index,
1047 dg_len - crc_len);
1048 if (crc != crc2) {
1049 INFO(port->func.config->cdev, "Bad CRC\n");
1050 goto err;
1051 }
1052 }
1053
1054 index2 = get_ncm(&tmp, opts->dgram_item_len);
1055 dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1056
1057 if (index2 == 0 || dg_len2 == 0) {
1058 skb2 = skb;
1059 } else {
1060 skb2 = skb_clone(skb, GFP_ATOMIC);
1061 if (skb2 == NULL)
1062 goto err;
1063 }
1064
1065 if (!skb_pull(skb2, index)) {
1066 ret = -EOVERFLOW;
1067 goto err;
1068 }
1069
1070 skb_trim(skb2, dg_len - crc_len);
1071 skb_queue_tail(list, skb2);
1072
1073 ndp_len -= 2 * (opts->dgram_item_len * 2);
1074
1075 dgram_counter++;
1076
1077 if (index2 == 0 || dg_len2 == 0)
1078 break;
1079 } while (ndp_len > 2 * (opts->dgram_item_len * 2)); /* zero entry */
1080
1081 VDBG(port->func.config->cdev,
1082 "Parsed NTB with %d frames\n", dgram_counter);
1083 return 0;
1084 err:
1085 skb_queue_purge(list);
1086 dev_kfree_skb_any(skb);
1087 return ret;
1088 }
1089
1090 static void ncm_disable(struct usb_function *f)
1091 {
1092 struct f_ncm *ncm = func_to_ncm(f);
1093 struct usb_composite_dev *cdev = f->config->cdev;
1094
1095 DBG(cdev, "ncm deactivated\n");
1096
1097 if (ncm->port.in_ep->driver_data)
1098 gether_disconnect(&ncm->port);
1099
1100 if (ncm->notify->driver_data) {
1101 usb_ep_disable(ncm->notify);
1102 ncm->notify->driver_data = NULL;
1103 ncm->notify->desc = NULL;
1104 }
1105 }
1106
1107 /*-------------------------------------------------------------------------*/
1108
1109 /*
1110 * Callbacks let us notify the host about connect/disconnect when the
1111 * net device is opened or closed.
1112 *
1113 * For testing, note that link states on this side include both opened
1114 * and closed variants of:
1115 *
1116 * - disconnected/unconfigured
1117 * - configured but inactive (data alt 0)
1118 * - configured and active (data alt 1)
1119 *
1120 * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1121 * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1122 * imply the host is actually polling the notification endpoint, and
1123 * likewise that "active" doesn't imply it's actually using the data
1124 * endpoints for traffic.
1125 */
1126
1127 static void ncm_open(struct gether *geth)
1128 {
1129 struct f_ncm *ncm = func_to_ncm(&geth->func);
1130
1131 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1132
1133 spin_lock(&ncm->lock);
1134 ncm->is_open = true;
1135 ncm_notify(ncm);
1136 spin_unlock(&ncm->lock);
1137 }
1138
1139 static void ncm_close(struct gether *geth)
1140 {
1141 struct f_ncm *ncm = func_to_ncm(&geth->func);
1142
1143 DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1144
1145 spin_lock(&ncm->lock);
1146 ncm->is_open = false;
1147 ncm_notify(ncm);
1148 spin_unlock(&ncm->lock);
1149 }
1150
1151 /*-------------------------------------------------------------------------*/
1152
1153 /* ethernet function driver setup/binding */
1154
1155 static int __init
1156 ncm_bind(struct usb_configuration *c, struct usb_function *f)
1157 {
1158 struct usb_composite_dev *cdev = c->cdev;
1159 struct f_ncm *ncm = func_to_ncm(f);
1160 int status;
1161 struct usb_ep *ep;
1162
1163 /* allocate instance-specific interface IDs */
1164 status = usb_interface_id(c, f);
1165 if (status < 0)
1166 goto fail;
1167 ncm->ctrl_id = status;
1168 ncm_iad_desc.bFirstInterface = status;
1169
1170 ncm_control_intf.bInterfaceNumber = status;
1171 ncm_union_desc.bMasterInterface0 = status;
1172
1173 status = usb_interface_id(c, f);
1174 if (status < 0)
1175 goto fail;
1176 ncm->data_id = status;
1177
1178 ncm_data_nop_intf.bInterfaceNumber = status;
1179 ncm_data_intf.bInterfaceNumber = status;
1180 ncm_union_desc.bSlaveInterface0 = status;
1181
1182 status = -ENODEV;
1183
1184 /* allocate instance-specific endpoints */
1185 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1186 if (!ep)
1187 goto fail;
1188 ncm->port.in_ep = ep;
1189 ep->driver_data = cdev; /* claim */
1190
1191 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1192 if (!ep)
1193 goto fail;
1194 ncm->port.out_ep = ep;
1195 ep->driver_data = cdev; /* claim */
1196
1197 ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1198 if (!ep)
1199 goto fail;
1200 ncm->notify = ep;
1201 ep->driver_data = cdev; /* claim */
1202
1203 status = -ENOMEM;
1204
1205 /* allocate notification request and buffer */
1206 ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1207 if (!ncm->notify_req)
1208 goto fail;
1209 ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL);
1210 if (!ncm->notify_req->buf)
1211 goto fail;
1212 ncm->notify_req->context = ncm;
1213 ncm->notify_req->complete = ncm_notify_complete;
1214
1215 /*
1216 * support all relevant hardware speeds... we expect that when
1217 * hardware is dual speed, all bulk-capable endpoints work at
1218 * both speeds
1219 */
1220 hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress;
1221 hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress;
1222 hs_ncm_notify_desc.bEndpointAddress =
1223 fs_ncm_notify_desc.bEndpointAddress;
1224
1225 status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function,
1226 NULL);
1227 /*
1228 * NOTE: all that is done without knowing or caring about
1229 * the network link ... which is unavailable to this code
1230 * until we're activated via set_alt().
1231 */
1232
1233 ncm->port.open = ncm_open;
1234 ncm->port.close = ncm_close;
1235
1236 DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1237 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1238 ncm->port.in_ep->name, ncm->port.out_ep->name,
1239 ncm->notify->name);
1240 return 0;
1241
1242 fail:
1243 usb_free_all_descriptors(f);
1244 if (ncm->notify_req) {
1245 kfree(ncm->notify_req->buf);
1246 usb_ep_free_request(ncm->notify, ncm->notify_req);
1247 }
1248
1249 /* we might as well release our claims on endpoints */
1250 if (ncm->notify)
1251 ncm->notify->driver_data = NULL;
1252 if (ncm->port.out_ep)
1253 ncm->port.out_ep->driver_data = NULL;
1254 if (ncm->port.in_ep)
1255 ncm->port.in_ep->driver_data = NULL;
1256
1257 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1258
1259 return status;
1260 }
1261
1262 static void
1263 ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1264 {
1265 struct f_ncm *ncm = func_to_ncm(f);
1266
1267 DBG(c->cdev, "ncm unbind\n");
1268
1269 ncm_string_defs[0].id = 0;
1270 usb_free_all_descriptors(f);
1271
1272 kfree(ncm->notify_req->buf);
1273 usb_ep_free_request(ncm->notify, ncm->notify_req);
1274
1275 kfree(ncm);
1276 }
1277
1278 /**
1279 * ncm_bind_config - add CDC Network link to a configuration
1280 * @c: the configuration to support the network link
1281 * @ethaddr: a buffer in which the ethernet address of the host side
1282 * side of the link was recorded
1283 * Context: single threaded during gadget setup
1284 *
1285 * Returns zero on success, else negative errno.
1286 *
1287 * Caller must have called @gether_setup(). Caller is also responsible
1288 * for calling @gether_cleanup() before module unload.
1289 */
1290 int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
1291 {
1292 struct f_ncm *ncm;
1293 int status;
1294
1295 if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
1296 return -EINVAL;
1297
1298 if (ncm_string_defs[0].id == 0) {
1299 status = usb_string_ids_tab(c->cdev, ncm_string_defs);
1300 if (status < 0)
1301 return status;
1302 ncm_control_intf.iInterface =
1303 ncm_string_defs[STRING_CTRL_IDX].id;
1304
1305 status = ncm_string_defs[STRING_DATA_IDX].id;
1306 ncm_data_nop_intf.iInterface = status;
1307 ncm_data_intf.iInterface = status;
1308
1309 ecm_desc.iMACAddress = ncm_string_defs[STRING_MAC_IDX].id;
1310 ncm_iad_desc.iFunction = ncm_string_defs[STRING_IAD_IDX].id;
1311 }
1312
1313 /* allocate and initialize one new instance */
1314 ncm = kzalloc(sizeof *ncm, GFP_KERNEL);
1315 if (!ncm)
1316 return -ENOMEM;
1317
1318 /* export host's Ethernet address in CDC format */
1319 snprintf(ncm->ethaddr, sizeof ncm->ethaddr, "%pm", ethaddr);
1320 ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr;
1321
1322 spin_lock_init(&ncm->lock);
1323 ncm_reset_values(ncm);
1324 ncm->port.is_fixed = true;
1325
1326 ncm->port.func.name = "cdc_network";
1327 ncm->port.func.strings = ncm_strings;
1328 /* descriptors are per-instance copies */
1329 ncm->port.func.bind = ncm_bind;
1330 ncm->port.func.unbind = ncm_unbind;
1331 ncm->port.func.set_alt = ncm_set_alt;
1332 ncm->port.func.get_alt = ncm_get_alt;
1333 ncm->port.func.setup = ncm_setup;
1334 ncm->port.func.disable = ncm_disable;
1335
1336 ncm->port.wrap = ncm_wrap_ntb;
1337 ncm->port.unwrap = ncm_unwrap_ntb;
1338
1339 status = usb_add_function(c, &ncm->port.func);
1340 if (status)
1341 kfree(ncm);
1342 return status;
1343 }
This page took 0.077633 seconds and 5 git commands to generate.