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