Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
f_ncm.c
Go to the documentation of this file.
1 /*
2  * f_ncm.c -- USB CDC Network (NCM) link function driver
3  *
4  * Copyright (C) 2010 Nokia Corporation
5  * Contact: Yauheni Kaliuta <[email protected]>
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 
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;
51 
52  char ethaddr[14];
53 
54  struct usb_ep *notify;
57  bool is_open;
58 
60  bool is_crc;
61 
62  /*
63  * for notification, it is accessed from both
64  * callback and ethernet open/close
65  */
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 = sizeof ntb_parameters,
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 LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
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),
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 = {
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 = 1 << LOG2_STATUS_INTERVAL_MSEC,
234 };
235 
236 static struct usb_endpoint_descriptor fs_ncm_in_desc __initdata = {
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 = {
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 = {
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 = LOG2_STATUS_INTERVAL_MSEC + 4,
279 };
280 static struct usb_endpoint_descriptor hs_ncm_in_desc __initdata = {
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 = {
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 = NULL /* DYNAMIC */,
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 
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");
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);
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));
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  */
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:
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) {
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 
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 
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 
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 
668  {
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 
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  }
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 
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 = !(
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 = ntb_parameters.wNdpInDivisor;
873  int rem = ntb_parameters.wNdpInPayloadRemainder;
874  int pad;
875  int ndp_align = ntb_parameters.wNdpInAlignment;
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  ncb_len += opts->nth_size;
882  ndp_pad = ALIGN(ncb_len, ndp_align) - ncb_len;
883  ncb_len += ndp_pad;
884  ncb_len += opts->ndp_size;
885  ncb_len += 2 * 2 * opts->dgram_item_len; /* Datagram entry */
886  ncb_len += 2 * 2 * opts->dgram_item_len; /* Zero datagram entry */
887  pad = ALIGN(ncb_len, div) + rem - ncb_len;
888  ncb_len += pad;
889 
890  if (ncb_len + skb->len + crc_len > max_size) {
891  dev_kfree_skb_any(skb);
892  return NULL;
893  }
894 
895  skb2 = skb_copy_expand(skb, ncb_len,
896  max_size - skb->len - ncb_len - crc_len,
897  GFP_ATOMIC);
898  dev_kfree_skb_any(skb);
899  if (!skb2)
900  return NULL;
901 
902  skb = skb2;
903 
904  tmp = (void *) skb_push(skb, ncb_len);
905  memset(tmp, 0, ncb_len);
906 
907  put_unaligned_le32(opts->nth_sign, tmp); /* dwSignature */
908  tmp += 2;
909  /* wHeaderLength */
910  put_unaligned_le16(opts->nth_size, tmp++);
911  tmp++; /* skip wSequence */
912  put_ncm(&tmp, opts->block_length, skb->len); /* (d)wBlockLength */
913  /* (d)wFpIndex */
914  /* the first pointer is right after the NTH + align */
915  put_ncm(&tmp, opts->fp_index, opts->nth_size + ndp_pad);
916 
917  tmp = (void *)tmp + ndp_pad;
918 
919  /* NDP */
920  put_unaligned_le32(opts->ndp_sign, tmp); /* dwSignature */
921  tmp += 2;
922  /* wLength */
923  put_unaligned_le16(ncb_len - opts->nth_size - pad, tmp++);
924 
925  tmp += opts->reserved1;
926  tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
927  tmp += opts->reserved2;
928 
929  if (ncm->is_crc) {
930  uint32_t crc;
931 
932  crc = ~crc32_le(~0,
933  skb->data + ncb_len,
934  skb->len - ncb_len);
935  put_unaligned_le32(crc, skb->data + skb->len);
936  skb_put(skb, crc_len);
937  }
938 
939  /* (d)wDatagramIndex[0] */
940  put_ncm(&tmp, opts->dgram_item_len, ncb_len);
941  /* (d)wDatagramLength[0] */
942  put_ncm(&tmp, opts->dgram_item_len, skb->len - ncb_len);
943  /* (d)wDatagramIndex[1] and (d)wDatagramLength[1] already zeroed */
944 
945  if (skb->len > MAX_TX_NONFIXED)
946  memset(skb_put(skb, max_size - skb->len),
947  0, max_size - skb->len);
948 
949  return skb;
950 }
951 
952 static int ncm_unwrap_ntb(struct gether *port,
953  struct sk_buff *skb,
954  struct sk_buff_head *list)
955 {
956  struct f_ncm *ncm = func_to_ncm(&port->func);
957  __le16 *tmp = (void *) skb->data;
958  unsigned index, index2;
959  unsigned dg_len, dg_len2;
960  unsigned ndp_len;
961  struct sk_buff *skb2;
962  int ret = -EINVAL;
963  unsigned max_size = le32_to_cpu(ntb_parameters.dwNtbOutMaxSize);
964  struct ndp_parser_opts *opts = ncm->parser_opts;
965  unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0;
966  int dgram_counter;
967 
968  /* dwSignature */
969  if (get_unaligned_le32(tmp) != opts->nth_sign) {
970  INFO(port->func.config->cdev, "Wrong NTH SIGN, skblen %d\n",
971  skb->len);
972  print_hex_dump(KERN_INFO, "HEAD:", DUMP_PREFIX_ADDRESS, 32, 1,
973  skb->data, 32, false);
974 
975  goto err;
976  }
977  tmp += 2;
978  /* wHeaderLength */
979  if (get_unaligned_le16(tmp++) != opts->nth_size) {
980  INFO(port->func.config->cdev, "Wrong NTB headersize\n");
981  goto err;
982  }
983  tmp++; /* skip wSequence */
984 
985  /* (d)wBlockLength */
986  if (get_ncm(&tmp, opts->block_length) > max_size) {
987  INFO(port->func.config->cdev, "OUT size exceeded\n");
988  goto err;
989  }
990 
991  index = get_ncm(&tmp, opts->fp_index);
992  /* NCM 3.2 */
993  if (((index % 4) != 0) && (index < opts->nth_size)) {
994  INFO(port->func.config->cdev, "Bad index: %x\n",
995  index);
996  goto err;
997  }
998 
999  /* walk through NDP */
1000  tmp = ((void *)skb->data) + index;
1001  if (get_unaligned_le32(tmp) != opts->ndp_sign) {
1002  INFO(port->func.config->cdev, "Wrong NDP SIGN\n");
1003  goto err;
1004  }
1005  tmp += 2;
1006 
1007  ndp_len = get_unaligned_le16(tmp++);
1008  /*
1009  * NCM 3.3.1
1010  * entry is 2 items
1011  * item size is 16/32 bits, opts->dgram_item_len * 2 bytes
1012  * minimal: struct usb_cdc_ncm_ndpX + normal entry + zero entry
1013  */
1014  if ((ndp_len < opts->ndp_size + 2 * 2 * (opts->dgram_item_len * 2))
1015  || (ndp_len % opts->ndplen_align != 0)) {
1016  INFO(port->func.config->cdev, "Bad NDP length: %x\n", ndp_len);
1017  goto err;
1018  }
1019  tmp += opts->reserved1;
1020  tmp += opts->next_fp_index; /* skip reserved (d)wNextFpIndex */
1021  tmp += opts->reserved2;
1022 
1023  ndp_len -= opts->ndp_size;
1024  index2 = get_ncm(&tmp, opts->dgram_item_len);
1025  dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1026  dgram_counter = 0;
1027 
1028  do {
1029  index = index2;
1030  dg_len = dg_len2;
1031  if (dg_len < 14 + crc_len) { /* ethernet header + crc */
1032  INFO(port->func.config->cdev, "Bad dgram length: %x\n",
1033  dg_len);
1034  goto err;
1035  }
1036  if (ncm->is_crc) {
1037  uint32_t crc, crc2;
1038 
1039  crc = get_unaligned_le32(skb->data +
1040  index + dg_len - crc_len);
1041  crc2 = ~crc32_le(~0,
1042  skb->data + index,
1043  dg_len - crc_len);
1044  if (crc != crc2) {
1045  INFO(port->func.config->cdev, "Bad CRC\n");
1046  goto err;
1047  }
1048  }
1049 
1050  index2 = get_ncm(&tmp, opts->dgram_item_len);
1051  dg_len2 = get_ncm(&tmp, opts->dgram_item_len);
1052 
1053  if (index2 == 0 || dg_len2 == 0) {
1054  skb2 = skb;
1055  } else {
1056  skb2 = skb_clone(skb, GFP_ATOMIC);
1057  if (skb2 == NULL)
1058  goto err;
1059  }
1060 
1061  if (!skb_pull(skb2, index)) {
1062  ret = -EOVERFLOW;
1063  goto err;
1064  }
1065 
1066  skb_trim(skb2, dg_len - crc_len);
1067  skb_queue_tail(list, skb2);
1068 
1069  ndp_len -= 2 * (opts->dgram_item_len * 2);
1070 
1071  dgram_counter++;
1072 
1073  if (index2 == 0 || dg_len2 == 0)
1074  break;
1075  } while (ndp_len > 2 * (opts->dgram_item_len * 2)); /* zero entry */
1076 
1077  VDBG(port->func.config->cdev,
1078  "Parsed NTB with %d frames\n", dgram_counter);
1079  return 0;
1080 err:
1081  skb_queue_purge(list);
1082  dev_kfree_skb_any(skb);
1083  return ret;
1084 }
1085 
1086 static void ncm_disable(struct usb_function *f)
1087 {
1088  struct f_ncm *ncm = func_to_ncm(f);
1089  struct usb_composite_dev *cdev = f->config->cdev;
1090 
1091  DBG(cdev, "ncm deactivated\n");
1092 
1093  if (ncm->port.in_ep->driver_data)
1094  gether_disconnect(&ncm->port);
1095 
1096  if (ncm->notify->driver_data) {
1097  usb_ep_disable(ncm->notify);
1098  ncm->notify->driver_data = NULL;
1099  ncm->notify->desc = NULL;
1100  }
1101 }
1102 
1103 /*-------------------------------------------------------------------------*/
1104 
1105 /*
1106  * Callbacks let us notify the host about connect/disconnect when the
1107  * net device is opened or closed.
1108  *
1109  * For testing, note that link states on this side include both opened
1110  * and closed variants of:
1111  *
1112  * - disconnected/unconfigured
1113  * - configured but inactive (data alt 0)
1114  * - configured and active (data alt 1)
1115  *
1116  * Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
1117  * SET_INTERFACE (altsetting). Remember also that "configured" doesn't
1118  * imply the host is actually polling the notification endpoint, and
1119  * likewise that "active" doesn't imply it's actually using the data
1120  * endpoints for traffic.
1121  */
1122 
1123 static void ncm_open(struct gether *geth)
1124 {
1125  struct f_ncm *ncm = func_to_ncm(&geth->func);
1126 
1127  DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1128 
1129  spin_lock(&ncm->lock);
1130  ncm->is_open = true;
1131  ncm_notify(ncm);
1132  spin_unlock(&ncm->lock);
1133 }
1134 
1135 static void ncm_close(struct gether *geth)
1136 {
1137  struct f_ncm *ncm = func_to_ncm(&geth->func);
1138 
1139  DBG(ncm->port.func.config->cdev, "%s\n", __func__);
1140 
1141  spin_lock(&ncm->lock);
1142  ncm->is_open = false;
1143  ncm_notify(ncm);
1144  spin_unlock(&ncm->lock);
1145 }
1146 
1147 /*-------------------------------------------------------------------------*/
1148 
1149 /* ethernet function driver setup/binding */
1150 
1151 static int __init
1152 ncm_bind(struct usb_configuration *c, struct usb_function *f)
1153 {
1154  struct usb_composite_dev *cdev = c->cdev;
1155  struct f_ncm *ncm = func_to_ncm(f);
1156  int status;
1157  struct usb_ep *ep;
1158 
1159  /* allocate instance-specific interface IDs */
1160  status = usb_interface_id(c, f);
1161  if (status < 0)
1162  goto fail;
1163  ncm->ctrl_id = status;
1164  ncm_iad_desc.bFirstInterface = status;
1165 
1166  ncm_control_intf.bInterfaceNumber = status;
1167  ncm_union_desc.bMasterInterface0 = status;
1168 
1169  status = usb_interface_id(c, f);
1170  if (status < 0)
1171  goto fail;
1172  ncm->data_id = status;
1173 
1174  ncm_data_nop_intf.bInterfaceNumber = status;
1175  ncm_data_intf.bInterfaceNumber = status;
1176  ncm_union_desc.bSlaveInterface0 = status;
1177 
1178  status = -ENODEV;
1179 
1180  /* allocate instance-specific endpoints */
1181  ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc);
1182  if (!ep)
1183  goto fail;
1184  ncm->port.in_ep = ep;
1185  ep->driver_data = cdev; /* claim */
1186 
1187  ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc);
1188  if (!ep)
1189  goto fail;
1190  ncm->port.out_ep = ep;
1191  ep->driver_data = cdev; /* claim */
1192 
1193  ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc);
1194  if (!ep)
1195  goto fail;
1196  ncm->notify = ep;
1197  ep->driver_data = cdev; /* claim */
1198 
1199  status = -ENOMEM;
1200 
1201  /* allocate notification request and buffer */
1202  ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
1203  if (!ncm->notify_req)
1204  goto fail;
1206  if (!ncm->notify_req->buf)
1207  goto fail;
1208  ncm->notify_req->context = ncm;
1209  ncm->notify_req->complete = ncm_notify_complete;
1210 
1211  /* copy descriptors, and track endpoint copies */
1212  f->descriptors = usb_copy_descriptors(ncm_fs_function);
1213  if (!f->descriptors)
1214  goto fail;
1215 
1216  /*
1217  * support all relevant hardware speeds... we expect that when
1218  * hardware is dual speed, all bulk-capable endpoints work at
1219  * both speeds
1220  */
1221  if (gadget_is_dualspeed(c->cdev->gadget)) {
1222  hs_ncm_in_desc.bEndpointAddress =
1223  fs_ncm_in_desc.bEndpointAddress;
1224  hs_ncm_out_desc.bEndpointAddress =
1225  fs_ncm_out_desc.bEndpointAddress;
1226  hs_ncm_notify_desc.bEndpointAddress =
1227  fs_ncm_notify_desc.bEndpointAddress;
1228 
1229  /* copy descriptors, and track endpoint copies */
1230  f->hs_descriptors = usb_copy_descriptors(ncm_hs_function);
1231  if (!f->hs_descriptors)
1232  goto fail;
1233  }
1234 
1235  /*
1236  * NOTE: all that is done without knowing or caring about
1237  * the network link ... which is unavailable to this code
1238  * until we're activated via set_alt().
1239  */
1240 
1241  ncm->port.open = ncm_open;
1242  ncm->port.close = ncm_close;
1243 
1244  DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n",
1245  gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
1246  ncm->port.in_ep->name, ncm->port.out_ep->name,
1247  ncm->notify->name);
1248  return 0;
1249 
1250 fail:
1251  if (f->descriptors)
1252  usb_free_descriptors(f->descriptors);
1253 
1254  if (ncm->notify_req) {
1255  kfree(ncm->notify_req->buf);
1256  usb_ep_free_request(ncm->notify, ncm->notify_req);
1257  }
1258 
1259  /* we might as well release our claims on endpoints */
1260  if (ncm->notify)
1261  ncm->notify->driver_data = NULL;
1262  if (ncm->port.out_ep->desc)
1263  ncm->port.out_ep->driver_data = NULL;
1264  if (ncm->port.in_ep->desc)
1265  ncm->port.in_ep->driver_data = NULL;
1266 
1267  ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
1268 
1269  return status;
1270 }
1271 
1272 static void
1273 ncm_unbind(struct usb_configuration *c, struct usb_function *f)
1274 {
1275  struct f_ncm *ncm = func_to_ncm(f);
1276 
1277  DBG(c->cdev, "ncm unbind\n");
1278 
1279  if (gadget_is_dualspeed(c->cdev->gadget))
1280  usb_free_descriptors(f->hs_descriptors);
1281  usb_free_descriptors(f->descriptors);
1282 
1283  kfree(ncm->notify_req->buf);
1284  usb_ep_free_request(ncm->notify, ncm->notify_req);
1285 
1286  ncm_string_defs[1].s = NULL;
1287  kfree(ncm);
1288 }
1289 
1303 {
1304  struct f_ncm *ncm;
1305  int status;
1306 
1307  if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
1308  return -EINVAL;
1309 
1310  /* maybe allocate device-global string IDs */
1311  if (ncm_string_defs[0].id == 0) {
1312 
1313  /* control interface label */
1314  status = usb_string_id(c->cdev);
1315  if (status < 0)
1316  return status;
1317  ncm_string_defs[STRING_CTRL_IDX].id = status;
1318  ncm_control_intf.iInterface = status;
1319 
1320  /* data interface label */
1321  status = usb_string_id(c->cdev);
1322  if (status < 0)
1323  return status;
1324  ncm_string_defs[STRING_DATA_IDX].id = status;
1325  ncm_data_nop_intf.iInterface = status;
1326  ncm_data_intf.iInterface = status;
1327 
1328  /* MAC address */
1329  status = usb_string_id(c->cdev);
1330  if (status < 0)
1331  return status;
1332  ncm_string_defs[STRING_MAC_IDX].id = status;
1333  ecm_desc.iMACAddress = status;
1334 
1335  /* IAD */
1336  status = usb_string_id(c->cdev);
1337  if (status < 0)
1338  return status;
1339  ncm_string_defs[STRING_IAD_IDX].id = status;
1340  ncm_iad_desc.iFunction = status;
1341  }
1342 
1343  /* allocate and initialize one new instance */
1344  ncm = kzalloc(sizeof *ncm, GFP_KERNEL);
1345  if (!ncm)
1346  return -ENOMEM;
1347 
1348  /* export host's Ethernet address in CDC format */
1349  snprintf(ncm->ethaddr, sizeof ncm->ethaddr, "%pm", ethaddr);
1350  ncm_string_defs[1].s = ncm->ethaddr;
1351 
1352  spin_lock_init(&ncm->lock);
1353  ncm_reset_values(ncm);
1354  ncm->port.is_fixed = true;
1355 
1356  ncm->port.func.name = "cdc_network";
1357  ncm->port.func.strings = ncm_strings;
1358  /* descriptors are per-instance copies */
1359  ncm->port.func.bind = ncm_bind;
1360  ncm->port.func.unbind = ncm_unbind;
1361  ncm->port.func.set_alt = ncm_set_alt;
1362  ncm->port.func.get_alt = ncm_get_alt;
1363  ncm->port.func.setup = ncm_setup;
1364  ncm->port.func.disable = ncm_disable;
1365 
1366  ncm->port.wrap = ncm_wrap_ntb;
1367  ncm->port.unwrap = ncm_unwrap_ntb;
1368 
1369  status = usb_add_function(c, &ncm->port.func);
1370  if (status) {
1371  ncm_string_defs[1].s = NULL;
1372  kfree(ncm);
1373  }
1374  return status;
1375 }