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