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" | |
40d133d7 | 27 | #include "u_ncm.h" |
9f6ce424 YK |
28 | |
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 | ||
9f6ce424 YK |
44 | enum 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 | ||
50 | struct f_ncm { | |
51 | struct gether port; | |
52 | u8 ctrl_id, data_id; | |
53 | ||
54 | char ethaddr[14]; | |
55 | ||
9f6ce424 | 56 | struct usb_ep *notify; |
9f6ce424 YK |
57 | struct usb_request *notify_req; |
58 | u8 notify_state; | |
59 | bool is_open; | |
60 | ||
8d640ad3 | 61 | const struct ndp_parser_opts *parser_opts; |
9f6ce424 | 62 | bool is_crc; |
8d640ad3 | 63 | u32 ndp_sign; |
9f6ce424 YK |
64 | |
65 | /* | |
66 | * for notification, it is accessed from both | |
67 | * callback and ethernet open/close | |
68 | */ | |
69 | spinlock_t lock; | |
70 | }; | |
71 | ||
72 | static 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 */ | |
78 | static 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 | /* | |
25985edc | 98 | * skbs of size less than that will not be aligned |
9f6ce424 YK |
99 | * 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 | ||
107 | static struct usb_cdc_ncm_ntb_parameters ntb_parameters = { | |
f72e3b78 | 108 | .wLength = cpu_to_le16(sizeof(ntb_parameters)), |
9f6ce424 YK |
109 | .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 | ||
bcb2f99c | 127 | #define NCM_STATUS_INTERVAL_MS 32 |
9f6ce424 YK |
128 | #define NCM_STATUS_BYTECOUNT 16 /* 8 byte header + data */ |
129 | ||
40d133d7 | 130 | static struct usb_interface_assoc_descriptor ncm_iad_desc = { |
9f6ce424 YK |
131 | .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 | ||
40d133d7 | 144 | static struct usb_interface_descriptor ncm_control_intf = { |
9f6ce424 YK |
145 | .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 | ||
40d133d7 | 156 | static struct usb_cdc_header_desc ncm_header_desc = { |
9f6ce424 YK |
157 | .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 | ||
40d133d7 | 164 | static struct usb_cdc_union_desc ncm_union_desc = { |
9f6ce424 YK |
165 | .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 | ||
40d133d7 | 172 | static struct usb_cdc_ether_desc ecm_desc = { |
9f6ce424 YK |
173 | .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 | ||
40d133d7 | 187 | static struct usb_cdc_ncm_desc ncm_desc = { |
9f6ce424 YK |
188 | .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 | ||
40d133d7 | 199 | static struct usb_interface_descriptor ncm_data_nop_intf = { |
9f6ce424 YK |
200 | .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 | ||
40d133d7 | 214 | static struct usb_interface_descriptor ncm_data_intf = { |
9f6ce424 YK |
215 | .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 | ||
40d133d7 | 229 | static struct usb_endpoint_descriptor fs_ncm_notify_desc = { |
9f6ce424 YK |
230 | .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), | |
bcb2f99c | 236 | .bInterval = NCM_STATUS_INTERVAL_MS, |
9f6ce424 YK |
237 | }; |
238 | ||
40d133d7 | 239 | static struct usb_endpoint_descriptor fs_ncm_in_desc = { |
9f6ce424 YK |
240 | .bLength = USB_DT_ENDPOINT_SIZE, |
241 | .bDescriptorType = USB_DT_ENDPOINT, | |
242 | ||
243 | .bEndpointAddress = USB_DIR_IN, | |
244 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
245 | }; | |
246 | ||
40d133d7 | 247 | static struct usb_endpoint_descriptor fs_ncm_out_desc = { |
9f6ce424 YK |
248 | .bLength = USB_DT_ENDPOINT_SIZE, |
249 | .bDescriptorType = USB_DT_ENDPOINT, | |
250 | ||
251 | .bEndpointAddress = USB_DIR_OUT, | |
252 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
253 | }; | |
254 | ||
40d133d7 | 255 | static struct usb_descriptor_header *ncm_fs_function[] = { |
9f6ce424 YK |
256 | (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 | ||
40d133d7 | 274 | static struct usb_endpoint_descriptor hs_ncm_notify_desc = { |
9f6ce424 YK |
275 | .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), | |
bcb2f99c | 281 | .bInterval = USB_MS_TO_HS_INTERVAL(NCM_STATUS_INTERVAL_MS), |
9f6ce424 | 282 | }; |
40d133d7 | 283 | static struct usb_endpoint_descriptor hs_ncm_in_desc = { |
9f6ce424 YK |
284 | .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 | ||
40d133d7 | 292 | static struct usb_endpoint_descriptor hs_ncm_out_desc = { |
9f6ce424 YK |
293 | .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 | ||
40d133d7 | 301 | static struct usb_descriptor_header *ncm_hs_function[] = { |
9f6ce424 YK |
302 | (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 | ||
325 | static struct usb_string ncm_string_defs[] = { | |
326 | [STRING_CTRL_IDX].s = "CDC Network Control Model (NCM)", | |
1616e99d | 327 | [STRING_MAC_IDX].s = "", |
9f6ce424 YK |
328 | [STRING_DATA_IDX].s = "CDC Network Data", |
329 | [STRING_IAD_IDX].s = "CDC NCM", | |
330 | { } /* end of list */ | |
331 | }; | |
332 | ||
333 | static struct usb_gadget_strings ncm_string_table = { | |
334 | .language = 0x0409, /* en-us */ | |
335 | .strings = ncm_string_defs, | |
336 | }; | |
337 | ||
338 | static 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 | ||
352 | struct 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 | ||
8d640ad3 SAS |
396 | static const struct ndp_parser_opts ndp16_opts = INIT_NDP16_OPTS; |
397 | static const struct ndp_parser_opts ndp32_opts = INIT_NDP32_OPTS; | |
9f6ce424 YK |
398 | |
399 | static 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 | ||
416 | static 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 | ||
437 | static 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 | */ | |
453 | static 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 | */ | |
520 | static 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 | ||
536 | static 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 | ||
562 | static 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 | ||
587 | invalid: | |
588 | usb_ep_set_halt(ep); | |
589 | return; | |
590 | } | |
591 | ||
592 | static 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 | } | |
8d640ad3 | 738 | ncm->ndp_sign = ncm->parser_opts->ndp_sign | ndp_hdr_crc; |
9f6ce424 YK |
739 | 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: | |
750 | invalid: | |
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 | ||
775 | static 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); | |
ea2a1df7 TB |
788 | } |
789 | ||
790 | if (!(ncm->notify->desc)) { | |
9f6ce424 | 791 | DBG(cdev, "init ncm ctrl %d\n", intf); |
ea2a1df7 TB |
792 | if (config_ep_by_speed(cdev->gadget, f, ncm->notify)) |
793 | goto fail; | |
9f6ce424 | 794 | } |
72c973dd | 795 | usb_ep_enable(ncm->notify); |
9f6ce424 YK |
796 | 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 | ||
ea2a1df7 TB |
816 | if (!ncm->port.in_ep->desc || |
817 | !ncm->port.out_ep->desc) { | |
9f6ce424 | 818 | DBG(cdev, "init ncm\n"); |
ea2a1df7 TB |
819 | 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 | } | |
9f6ce424 YK |
827 | } |
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; | |
850 | fail: | |
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 | */ | |
858 | static 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 | ||
867 | static 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; | |
f72e3b78 DM |
874 | int div; |
875 | int rem; | |
9f6ce424 | 876 | int pad; |
f72e3b78 | 877 | int ndp_align; |
9f6ce424 YK |
878 | int ndp_pad; |
879 | unsigned max_size = ncm->port.fixed_in_len; | |
8d640ad3 | 880 | const struct ndp_parser_opts *opts = ncm->parser_opts; |
9f6ce424 YK |
881 | unsigned crc_len = ncm->is_crc ? sizeof(uint32_t) : 0; |
882 | ||
f72e3b78 DM |
883 | 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 | ||
9f6ce424 YK |
887 | 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 */ | |
8d640ad3 | 926 | put_unaligned_le32(ncm->ndp_sign, tmp); /* dwSignature */ |
9f6ce424 YK |
927 | 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 | ||
958 | static 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); | |
8d640ad3 | 970 | const struct ndp_parser_opts *opts = ncm->parser_opts; |
9f6ce424 YK |
971 | 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; | |
8d640ad3 | 1007 | if (get_unaligned_le32(tmp) != ncm->ndp_sign) { |
9f6ce424 YK |
1008 | 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; | |
1086 | err: | |
1087 | skb_queue_purge(list); | |
1088 | dev_kfree_skb_any(skb); | |
1089 | return ret; | |
1090 | } | |
1091 | ||
1092 | static 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; | |
72c973dd | 1105 | ncm->notify->desc = NULL; |
9f6ce424 YK |
1106 | } |
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 | ||
1129 | static 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 | ||
1141 | static 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 | ||
40d133d7 | 1157 | static int ncm_bind(struct usb_configuration *c, struct usb_function *f) |
9f6ce424 YK |
1158 | { |
1159 | struct usb_composite_dev *cdev = c->cdev; | |
1160 | struct f_ncm *ncm = func_to_ncm(f); | |
1161 | int status; | |
1162 | struct usb_ep *ep; | |
1163 | ||
40d133d7 AP |
1164 | #ifndef USB_FNCM_INCLUDED |
1165 | struct f_ncm_opts *ncm_opts; | |
1166 | ||
1167 | if (!can_support_ecm(cdev->gadget)) | |
1168 | return -EINVAL; | |
1169 | ||
1170 | ncm_opts = container_of(f->fi, struct f_ncm_opts, func_inst); | |
1171 | /* | |
1172 | * in drivers/usb/gadget/configfs.c:configfs_composite_bind() | |
1173 | * configurations are bound in sequence with list_for_each_entry, | |
1174 | * in each configuration its functions are bound in sequence | |
1175 | * with list_for_each_entry, so we assume no race condition | |
1176 | * with regard to ncm_opts->bound access | |
1177 | */ | |
1178 | if (!ncm_opts->bound) { | |
1179 | gether_set_gadget(ncm_opts->net, cdev->gadget); | |
1180 | status = gether_register_netdev(ncm_opts->net); | |
1181 | if (status) | |
1182 | return status; | |
1183 | ncm_opts->bound = true; | |
1184 | } | |
1185 | #endif | |
1186 | if (ncm_string_defs[0].id == 0) { | |
1187 | status = usb_string_ids_tab(c->cdev, ncm_string_defs); | |
1188 | if (status < 0) | |
1189 | return status; | |
1190 | ncm_control_intf.iInterface = | |
1191 | ncm_string_defs[STRING_CTRL_IDX].id; | |
1192 | ||
1193 | status = ncm_string_defs[STRING_DATA_IDX].id; | |
1194 | ncm_data_nop_intf.iInterface = status; | |
1195 | ncm_data_intf.iInterface = status; | |
1196 | ||
1197 | ecm_desc.iMACAddress = ncm_string_defs[STRING_MAC_IDX].id; | |
1198 | ncm_iad_desc.iFunction = ncm_string_defs[STRING_IAD_IDX].id; | |
1199 | } | |
1200 | ||
9f6ce424 YK |
1201 | /* allocate instance-specific interface IDs */ |
1202 | status = usb_interface_id(c, f); | |
1203 | if (status < 0) | |
1204 | goto fail; | |
1205 | ncm->ctrl_id = status; | |
1206 | ncm_iad_desc.bFirstInterface = status; | |
1207 | ||
1208 | ncm_control_intf.bInterfaceNumber = status; | |
1209 | ncm_union_desc.bMasterInterface0 = status; | |
1210 | ||
1211 | status = usb_interface_id(c, f); | |
1212 | if (status < 0) | |
1213 | goto fail; | |
1214 | ncm->data_id = status; | |
1215 | ||
1216 | ncm_data_nop_intf.bInterfaceNumber = status; | |
1217 | ncm_data_intf.bInterfaceNumber = status; | |
1218 | ncm_union_desc.bSlaveInterface0 = status; | |
1219 | ||
1220 | status = -ENODEV; | |
1221 | ||
1222 | /* allocate instance-specific endpoints */ | |
1223 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_in_desc); | |
1224 | if (!ep) | |
1225 | goto fail; | |
1226 | ncm->port.in_ep = ep; | |
1227 | ep->driver_data = cdev; /* claim */ | |
1228 | ||
1229 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_out_desc); | |
1230 | if (!ep) | |
1231 | goto fail; | |
1232 | ncm->port.out_ep = ep; | |
1233 | ep->driver_data = cdev; /* claim */ | |
1234 | ||
1235 | ep = usb_ep_autoconfig(cdev->gadget, &fs_ncm_notify_desc); | |
1236 | if (!ep) | |
1237 | goto fail; | |
1238 | ncm->notify = ep; | |
1239 | ep->driver_data = cdev; /* claim */ | |
1240 | ||
1241 | status = -ENOMEM; | |
1242 | ||
1243 | /* allocate notification request and buffer */ | |
1244 | ncm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); | |
1245 | if (!ncm->notify_req) | |
1246 | goto fail; | |
1247 | ncm->notify_req->buf = kmalloc(NCM_STATUS_BYTECOUNT, GFP_KERNEL); | |
1248 | if (!ncm->notify_req->buf) | |
1249 | goto fail; | |
1250 | ncm->notify_req->context = ncm; | |
1251 | ncm->notify_req->complete = ncm_notify_complete; | |
1252 | ||
9f6ce424 YK |
1253 | /* |
1254 | * support all relevant hardware speeds... we expect that when | |
1255 | * hardware is dual speed, all bulk-capable endpoints work at | |
1256 | * both speeds | |
1257 | */ | |
10287bae SAS |
1258 | hs_ncm_in_desc.bEndpointAddress = fs_ncm_in_desc.bEndpointAddress; |
1259 | hs_ncm_out_desc.bEndpointAddress = fs_ncm_out_desc.bEndpointAddress; | |
1260 | hs_ncm_notify_desc.bEndpointAddress = | |
1261 | fs_ncm_notify_desc.bEndpointAddress; | |
9f6ce424 | 1262 | |
10287bae SAS |
1263 | status = usb_assign_descriptors(f, ncm_fs_function, ncm_hs_function, |
1264 | NULL); | |
9f6ce424 YK |
1265 | /* |
1266 | * NOTE: all that is done without knowing or caring about | |
1267 | * the network link ... which is unavailable to this code | |
1268 | * until we're activated via set_alt(). | |
1269 | */ | |
1270 | ||
1271 | ncm->port.open = ncm_open; | |
1272 | ncm->port.close = ncm_close; | |
1273 | ||
1274 | DBG(cdev, "CDC Network: %s speed IN/%s OUT/%s NOTIFY/%s\n", | |
1275 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", | |
1276 | ncm->port.in_ep->name, ncm->port.out_ep->name, | |
1277 | ncm->notify->name); | |
1278 | return 0; | |
1279 | ||
1280 | fail: | |
10287bae | 1281 | usb_free_all_descriptors(f); |
9f6ce424 YK |
1282 | if (ncm->notify_req) { |
1283 | kfree(ncm->notify_req->buf); | |
1284 | usb_ep_free_request(ncm->notify, ncm->notify_req); | |
1285 | } | |
1286 | ||
1287 | /* we might as well release our claims on endpoints */ | |
1288 | if (ncm->notify) | |
1289 | ncm->notify->driver_data = NULL; | |
e79cc615 | 1290 | if (ncm->port.out_ep) |
9f6ce424 | 1291 | ncm->port.out_ep->driver_data = NULL; |
e79cc615 | 1292 | if (ncm->port.in_ep) |
9f6ce424 YK |
1293 | ncm->port.in_ep->driver_data = NULL; |
1294 | ||
1295 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | |
1296 | ||
1297 | return status; | |
1298 | } | |
1299 | ||
40d133d7 AP |
1300 | #ifdef USB_FNCM_INCLUDED |
1301 | ||
9f6ce424 | 1302 | static void |
40d133d7 | 1303 | ncm_old_unbind(struct usb_configuration *c, struct usb_function *f) |
9f6ce424 YK |
1304 | { |
1305 | struct f_ncm *ncm = func_to_ncm(f); | |
1306 | ||
1307 | DBG(c->cdev, "ncm unbind\n"); | |
1308 | ||
1616e99d | 1309 | ncm_string_defs[0].id = 0; |
10287bae | 1310 | usb_free_all_descriptors(f); |
9f6ce424 YK |
1311 | |
1312 | kfree(ncm->notify_req->buf); | |
1313 | usb_ep_free_request(ncm->notify, ncm->notify_req); | |
1314 | ||
9f6ce424 YK |
1315 | kfree(ncm); |
1316 | } | |
1317 | ||
1318 | /** | |
1319 | * ncm_bind_config - add CDC Network link to a configuration | |
1320 | * @c: the configuration to support the network link | |
1321 | * @ethaddr: a buffer in which the ethernet address of the host side | |
1322 | * side of the link was recorded | |
1323 | * Context: single threaded during gadget setup | |
1324 | * | |
1325 | * Returns zero on success, else negative errno. | |
1326 | * | |
1327 | * Caller must have called @gether_setup(). Caller is also responsible | |
1328 | * for calling @gether_cleanup() before module unload. | |
1329 | */ | |
d6a01439 SAS |
1330 | int __init ncm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
1331 | struct eth_dev *dev) | |
9f6ce424 YK |
1332 | { |
1333 | struct f_ncm *ncm; | |
1334 | int status; | |
1335 | ||
1336 | if (!can_support_ecm(c->cdev->gadget) || !ethaddr) | |
1337 | return -EINVAL; | |
1338 | ||
9f6ce424 YK |
1339 | /* allocate and initialize one new instance */ |
1340 | ncm = kzalloc(sizeof *ncm, GFP_KERNEL); | |
1341 | if (!ncm) | |
1342 | return -ENOMEM; | |
1343 | ||
1344 | /* export host's Ethernet address in CDC format */ | |
8c7ca992 | 1345 | snprintf(ncm->ethaddr, sizeof ncm->ethaddr, "%pm", ethaddr); |
1616e99d | 1346 | ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr; |
9f6ce424 YK |
1347 | |
1348 | spin_lock_init(&ncm->lock); | |
1349 | ncm_reset_values(ncm); | |
d6a01439 | 1350 | ncm->port.ioport = dev; |
9f6ce424 YK |
1351 | ncm->port.is_fixed = true; |
1352 | ||
1353 | ncm->port.func.name = "cdc_network"; | |
1354 | ncm->port.func.strings = ncm_strings; | |
1355 | /* descriptors are per-instance copies */ | |
1356 | ncm->port.func.bind = ncm_bind; | |
40d133d7 | 1357 | ncm->port.func.unbind = ncm_old_unbind; |
9f6ce424 YK |
1358 | ncm->port.func.set_alt = ncm_set_alt; |
1359 | ncm->port.func.get_alt = ncm_get_alt; | |
1360 | ncm->port.func.setup = ncm_setup; | |
1361 | ncm->port.func.disable = ncm_disable; | |
1362 | ||
1363 | ncm->port.wrap = ncm_wrap_ntb; | |
1364 | ncm->port.unwrap = ncm_unwrap_ntb; | |
1365 | ||
1366 | status = usb_add_function(c, &ncm->port.func); | |
1616e99d | 1367 | if (status) |
9f6ce424 | 1368 | kfree(ncm); |
9f6ce424 YK |
1369 | return status; |
1370 | } | |
40d133d7 AP |
1371 | |
1372 | #else | |
1373 | ||
1374 | static void ncm_free_inst(struct usb_function_instance *f) | |
1375 | { | |
1376 | struct f_ncm_opts *opts; | |
1377 | ||
1378 | opts = container_of(f, struct f_ncm_opts, func_inst); | |
1379 | if (opts->bound) | |
1380 | gether_cleanup(netdev_priv(opts->net)); | |
1381 | else | |
1382 | free_netdev(opts->net); | |
1383 | kfree(opts); | |
1384 | } | |
1385 | ||
1386 | static struct usb_function_instance *ncm_alloc_inst(void) | |
1387 | { | |
1388 | struct f_ncm_opts *opts; | |
1389 | ||
1390 | opts = kzalloc(sizeof(*opts), GFP_KERNEL); | |
1391 | if (!opts) | |
1392 | return ERR_PTR(-ENOMEM); | |
1393 | opts->func_inst.free_func_inst = ncm_free_inst; | |
1394 | opts->net = gether_setup_default(); | |
1395 | if (IS_ERR(opts->net)) | |
1396 | return ERR_PTR(PTR_ERR(opts->net)); | |
1397 | ||
1398 | return &opts->func_inst; | |
1399 | } | |
1400 | ||
1401 | static void ncm_free(struct usb_function *f) | |
1402 | { | |
1403 | struct f_ncm *ncm; | |
1404 | ||
1405 | ncm = func_to_ncm(f); | |
1406 | kfree(ncm); | |
1407 | } | |
1408 | ||
1409 | static void ncm_unbind(struct usb_configuration *c, struct usb_function *f) | |
1410 | { | |
1411 | struct f_ncm *ncm = func_to_ncm(f); | |
1412 | ||
1413 | DBG(c->cdev, "ncm unbind\n"); | |
1414 | ||
1415 | ncm_string_defs[0].id = 0; | |
1416 | usb_free_all_descriptors(f); | |
1417 | ||
1418 | kfree(ncm->notify_req->buf); | |
1419 | usb_ep_free_request(ncm->notify, ncm->notify_req); | |
1420 | } | |
1421 | ||
1422 | struct usb_function *ncm_alloc(struct usb_function_instance *fi) | |
1423 | { | |
1424 | struct f_ncm *ncm; | |
1425 | struct f_ncm_opts *opts; | |
1426 | int status; | |
1427 | ||
1428 | /* allocate and initialize one new instance */ | |
1429 | ncm = kzalloc(sizeof(*ncm), GFP_KERNEL); | |
1430 | if (!ncm) | |
1431 | return ERR_PTR(-ENOMEM); | |
1432 | ||
1433 | opts = container_of(fi, struct f_ncm_opts, func_inst); | |
1434 | ||
1435 | /* export host's Ethernet address in CDC format */ | |
1436 | status = gether_get_host_addr_cdc(opts->net, ncm->ethaddr, | |
1437 | sizeof(ncm->ethaddr)); | |
1438 | if (status < 12) { /* strlen("01234567890a") */ | |
1439 | kfree(ncm); | |
1440 | return ERR_PTR(-EINVAL); | |
1441 | } | |
1442 | ncm_string_defs[STRING_MAC_IDX].s = ncm->ethaddr; | |
1443 | ||
1444 | spin_lock_init(&ncm->lock); | |
1445 | ncm_reset_values(ncm); | |
1446 | ncm->port.ioport = netdev_priv(opts->net); | |
1447 | ncm->port.is_fixed = true; | |
1448 | ||
1449 | ncm->port.func.name = "cdc_network"; | |
1450 | ncm->port.func.strings = ncm_strings; | |
1451 | /* descriptors are per-instance copies */ | |
1452 | ncm->port.func.bind = ncm_bind; | |
1453 | ncm->port.func.unbind = ncm_unbind; | |
1454 | ncm->port.func.set_alt = ncm_set_alt; | |
1455 | ncm->port.func.get_alt = ncm_get_alt; | |
1456 | ncm->port.func.setup = ncm_setup; | |
1457 | ncm->port.func.disable = ncm_disable; | |
1458 | ncm->port.func.free_func = ncm_free; | |
1459 | ||
1460 | ncm->port.wrap = ncm_wrap_ntb; | |
1461 | ncm->port.unwrap = ncm_unwrap_ntb; | |
1462 | ||
1463 | return &ncm->port.func; | |
1464 | } | |
1465 | ||
1466 | DECLARE_USB_FUNCTION_INIT(ncm, ncm_alloc_inst, ncm_alloc); | |
1467 | MODULE_LICENSE("GPL"); | |
1468 | MODULE_AUTHOR("Yauheni Kaliuta"); | |
1469 | ||
1470 | #endif | |
1471 |