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