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