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