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