Commit | Line | Data |
---|---|---|
45fe3b8e DB |
1 | /* |
2 | * f_rndis.c -- RNDIS link function driver | |
3 | * | |
4 | * Copyright (C) 2003-2005,2008 David Brownell | |
5 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger | |
6 | * Copyright (C) 2008 Nokia Corporation | |
b97503ff | 7 | * Copyright (C) 2009 Samsung Electronics |
54b8360f | 8 | * Author: Michal Nazarewicz (mina86@mina86.com) |
45fe3b8e DB |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
45fe3b8e DB |
14 | */ |
15 | ||
16 | /* #define VERBOSE_DEBUG */ | |
17 | ||
5a0e3ad6 | 18 | #include <linux/slab.h> |
45fe3b8e DB |
19 | #include <linux/kernel.h> |
20 | #include <linux/device.h> | |
21 | #include <linux/etherdevice.h> | |
22 | ||
60063497 | 23 | #include <linux/atomic.h> |
45fe3b8e DB |
24 | |
25 | #include "u_ether.h" | |
26 | #include "rndis.h" | |
27 | ||
28 | ||
29 | /* | |
30 | * This function is an RNDIS Ethernet port -- a Microsoft protocol that's | |
31 | * been promoted instead of the standard CDC Ethernet. The published RNDIS | |
32 | * spec is ambiguous, incomplete, and needlessly complex. Variants such as | |
33 | * ActiveSync have even worse status in terms of specification. | |
34 | * | |
35 | * In short: it's a protocol controlled by (and for) Microsoft, not for an | |
36 | * Open ecosystem or markets. Linux supports it *only* because Microsoft | |
37 | * doesn't support the CDC Ethernet standard. | |
38 | * | |
39 | * The RNDIS data transfer model is complex, with multiple Ethernet packets | |
40 | * per USB message, and out of band data. The control model is built around | |
41 | * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM | |
42 | * (modem, not Ethernet) veneer, with those ACM descriptors being entirely | |
43 | * useless (they're ignored). RNDIS expects to be the only function in its | |
44 | * configuration, so it's no real help if you need composite devices; and | |
45 | * it expects to be the first configuration too. | |
46 | * | |
47 | * There is a single technical advantage of RNDIS over CDC Ethernet, if you | |
48 | * discount the fluff that its RPC can be made to deliver: it doesn't need | |
49 | * a NOP altsetting for the data interface. That lets it work on some of the | |
50 | * "so smart it's stupid" hardware which takes over configuration changes | |
51 | * from the software, and adds restrictions like "no altsettings". | |
52 | * | |
53 | * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and | |
54 | * have all sorts of contrary-to-specification oddities that can prevent | |
55 | * them from working sanely. Since bugfixes (or accurate specs, letting | |
56 | * Linux work around those bugs) are unlikely to ever come from MSFT, you | |
57 | * may want to avoid using RNDIS on purely operational grounds. | |
58 | * | |
59 | * Omissions from the RNDIS 1.0 specification include: | |
60 | * | |
61 | * - Power management ... references data that's scattered around lots | |
62 | * of other documentation, which is incorrect/incomplete there too. | |
63 | * | |
64 | * - There are various undocumented protocol requirements, like the need | |
65 | * to send garbage in some control-OUT messages. | |
66 | * | |
67 | * - MS-Windows drivers sometimes emit undocumented requests. | |
68 | */ | |
69 | ||
45fe3b8e DB |
70 | struct f_rndis { |
71 | struct gether port; | |
72 | u8 ctrl_id, data_id; | |
73 | u8 ethaddr[ETH_ALEN]; | |
1fbfeff9 BG |
74 | u32 vendorID; |
75 | const char *manufacturer; | |
45fe3b8e DB |
76 | int config; |
77 | ||
45fe3b8e | 78 | struct usb_ep *notify; |
45fe3b8e DB |
79 | struct usb_request *notify_req; |
80 | atomic_t notify_count; | |
81 | }; | |
82 | ||
83 | static inline struct f_rndis *func_to_rndis(struct usb_function *f) | |
84 | { | |
85 | return container_of(f, struct f_rndis, port.func); | |
86 | } | |
87 | ||
88 | /* peak (theoretical) bulk transfer rate in bits-per-second */ | |
89 | static unsigned int bitrate(struct usb_gadget *g) | |
90 | { | |
04617db7 PZ |
91 | if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER) |
92 | return 13 * 1024 * 8 * 1000 * 8; | |
93 | else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH) | |
45fe3b8e DB |
94 | return 13 * 512 * 8 * 1000 * 8; |
95 | else | |
04617db7 | 96 | return 19 * 64 * 1 * 1000 * 8; |
45fe3b8e DB |
97 | } |
98 | ||
99 | /*-------------------------------------------------------------------------*/ | |
100 | ||
101 | /* | |
102 | */ | |
103 | ||
bcb2f99c | 104 | #define RNDIS_STATUS_INTERVAL_MS 32 |
45fe3b8e DB |
105 | #define STATUS_BYTECOUNT 8 /* 8 bytes data */ |
106 | ||
107 | ||
108 | /* interface descriptor: */ | |
109 | ||
28824b18 | 110 | static struct usb_interface_descriptor rndis_control_intf = { |
45fe3b8e DB |
111 | .bLength = sizeof rndis_control_intf, |
112 | .bDescriptorType = USB_DT_INTERFACE, | |
113 | ||
114 | /* .bInterfaceNumber = DYNAMIC */ | |
115 | /* status endpoint is optional; this could be patched later */ | |
116 | .bNumEndpoints = 1, | |
117 | .bInterfaceClass = USB_CLASS_COMM, | |
118 | .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, | |
119 | .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR, | |
120 | /* .iInterface = DYNAMIC */ | |
121 | }; | |
122 | ||
28824b18 | 123 | static struct usb_cdc_header_desc header_desc = { |
45fe3b8e DB |
124 | .bLength = sizeof header_desc, |
125 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
126 | .bDescriptorSubType = USB_CDC_HEADER_TYPE, | |
127 | ||
551509d2 | 128 | .bcdCDC = cpu_to_le16(0x0110), |
45fe3b8e DB |
129 | }; |
130 | ||
28824b18 | 131 | static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = { |
45fe3b8e DB |
132 | .bLength = sizeof call_mgmt_descriptor, |
133 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
134 | .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE, | |
135 | ||
136 | .bmCapabilities = 0x00, | |
137 | .bDataInterface = 0x01, | |
138 | }; | |
139 | ||
28824b18 | 140 | static struct usb_cdc_acm_descriptor rndis_acm_descriptor = { |
b97503ff | 141 | .bLength = sizeof rndis_acm_descriptor, |
45fe3b8e DB |
142 | .bDescriptorType = USB_DT_CS_INTERFACE, |
143 | .bDescriptorSubType = USB_CDC_ACM_TYPE, | |
144 | ||
145 | .bmCapabilities = 0x00, | |
146 | }; | |
147 | ||
28824b18 | 148 | static struct usb_cdc_union_desc rndis_union_desc = { |
45fe3b8e DB |
149 | .bLength = sizeof(rndis_union_desc), |
150 | .bDescriptorType = USB_DT_CS_INTERFACE, | |
151 | .bDescriptorSubType = USB_CDC_UNION_TYPE, | |
152 | /* .bMasterInterface0 = DYNAMIC */ | |
153 | /* .bSlaveInterface0 = DYNAMIC */ | |
154 | }; | |
155 | ||
156 | /* the data interface has two bulk endpoints */ | |
157 | ||
28824b18 | 158 | static struct usb_interface_descriptor rndis_data_intf = { |
45fe3b8e DB |
159 | .bLength = sizeof rndis_data_intf, |
160 | .bDescriptorType = USB_DT_INTERFACE, | |
161 | ||
162 | /* .bInterfaceNumber = DYNAMIC */ | |
45fe3b8e DB |
163 | .bNumEndpoints = 2, |
164 | .bInterfaceClass = USB_CLASS_CDC_DATA, | |
165 | .bInterfaceSubClass = 0, | |
166 | .bInterfaceProtocol = 0, | |
167 | /* .iInterface = DYNAMIC */ | |
168 | }; | |
169 | ||
b97503ff MN |
170 | |
171 | static struct usb_interface_assoc_descriptor | |
172 | rndis_iad_descriptor = { | |
173 | .bLength = sizeof rndis_iad_descriptor, | |
174 | .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION, | |
175 | ||
176 | .bFirstInterface = 0, /* XXX, hardcoded */ | |
177 | .bInterfaceCount = 2, // control + data | |
178 | .bFunctionClass = USB_CLASS_COMM, | |
179 | .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET, | |
180 | .bFunctionProtocol = USB_CDC_PROTO_NONE, | |
181 | /* .iFunction = DYNAMIC */ | |
182 | }; | |
183 | ||
45fe3b8e DB |
184 | /* full speed support: */ |
185 | ||
28824b18 | 186 | static struct usb_endpoint_descriptor fs_notify_desc = { |
45fe3b8e DB |
187 | .bLength = USB_DT_ENDPOINT_SIZE, |
188 | .bDescriptorType = USB_DT_ENDPOINT, | |
189 | ||
190 | .bEndpointAddress = USB_DIR_IN, | |
191 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
551509d2 | 192 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), |
bcb2f99c | 193 | .bInterval = RNDIS_STATUS_INTERVAL_MS, |
45fe3b8e DB |
194 | }; |
195 | ||
28824b18 | 196 | static struct usb_endpoint_descriptor fs_in_desc = { |
45fe3b8e DB |
197 | .bLength = USB_DT_ENDPOINT_SIZE, |
198 | .bDescriptorType = USB_DT_ENDPOINT, | |
199 | ||
200 | .bEndpointAddress = USB_DIR_IN, | |
201 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
202 | }; | |
203 | ||
28824b18 | 204 | static struct usb_endpoint_descriptor fs_out_desc = { |
45fe3b8e DB |
205 | .bLength = USB_DT_ENDPOINT_SIZE, |
206 | .bDescriptorType = USB_DT_ENDPOINT, | |
207 | ||
208 | .bEndpointAddress = USB_DIR_OUT, | |
209 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
210 | }; | |
211 | ||
28824b18 | 212 | static struct usb_descriptor_header *eth_fs_function[] = { |
b97503ff | 213 | (struct usb_descriptor_header *) &rndis_iad_descriptor, |
04617db7 | 214 | |
45fe3b8e DB |
215 | /* control interface matches ACM, not Ethernet */ |
216 | (struct usb_descriptor_header *) &rndis_control_intf, | |
217 | (struct usb_descriptor_header *) &header_desc, | |
218 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | |
b97503ff | 219 | (struct usb_descriptor_header *) &rndis_acm_descriptor, |
45fe3b8e DB |
220 | (struct usb_descriptor_header *) &rndis_union_desc, |
221 | (struct usb_descriptor_header *) &fs_notify_desc, | |
04617db7 | 222 | |
45fe3b8e DB |
223 | /* data interface has no altsetting */ |
224 | (struct usb_descriptor_header *) &rndis_data_intf, | |
225 | (struct usb_descriptor_header *) &fs_in_desc, | |
226 | (struct usb_descriptor_header *) &fs_out_desc, | |
227 | NULL, | |
228 | }; | |
229 | ||
230 | /* high speed support: */ | |
231 | ||
28824b18 | 232 | static struct usb_endpoint_descriptor hs_notify_desc = { |
45fe3b8e DB |
233 | .bLength = USB_DT_ENDPOINT_SIZE, |
234 | .bDescriptorType = USB_DT_ENDPOINT, | |
235 | ||
236 | .bEndpointAddress = USB_DIR_IN, | |
237 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
551509d2 | 238 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), |
bcb2f99c | 239 | .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS) |
45fe3b8e | 240 | }; |
04617db7 | 241 | |
28824b18 | 242 | static struct usb_endpoint_descriptor hs_in_desc = { |
45fe3b8e DB |
243 | .bLength = USB_DT_ENDPOINT_SIZE, |
244 | .bDescriptorType = USB_DT_ENDPOINT, | |
245 | ||
246 | .bEndpointAddress = USB_DIR_IN, | |
247 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 248 | .wMaxPacketSize = cpu_to_le16(512), |
45fe3b8e DB |
249 | }; |
250 | ||
28824b18 | 251 | static struct usb_endpoint_descriptor hs_out_desc = { |
45fe3b8e DB |
252 | .bLength = USB_DT_ENDPOINT_SIZE, |
253 | .bDescriptorType = USB_DT_ENDPOINT, | |
254 | ||
255 | .bEndpointAddress = USB_DIR_OUT, | |
256 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
551509d2 | 257 | .wMaxPacketSize = cpu_to_le16(512), |
45fe3b8e DB |
258 | }; |
259 | ||
28824b18 | 260 | static struct usb_descriptor_header *eth_hs_function[] = { |
b97503ff | 261 | (struct usb_descriptor_header *) &rndis_iad_descriptor, |
04617db7 | 262 | |
45fe3b8e DB |
263 | /* control interface matches ACM, not Ethernet */ |
264 | (struct usb_descriptor_header *) &rndis_control_intf, | |
265 | (struct usb_descriptor_header *) &header_desc, | |
266 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | |
b97503ff | 267 | (struct usb_descriptor_header *) &rndis_acm_descriptor, |
45fe3b8e DB |
268 | (struct usb_descriptor_header *) &rndis_union_desc, |
269 | (struct usb_descriptor_header *) &hs_notify_desc, | |
04617db7 | 270 | |
45fe3b8e DB |
271 | /* data interface has no altsetting */ |
272 | (struct usb_descriptor_header *) &rndis_data_intf, | |
273 | (struct usb_descriptor_header *) &hs_in_desc, | |
274 | (struct usb_descriptor_header *) &hs_out_desc, | |
275 | NULL, | |
276 | }; | |
277 | ||
04617db7 PZ |
278 | /* super speed support: */ |
279 | ||
280 | static struct usb_endpoint_descriptor ss_notify_desc = { | |
281 | .bLength = USB_DT_ENDPOINT_SIZE, | |
282 | .bDescriptorType = USB_DT_ENDPOINT, | |
283 | ||
284 | .bEndpointAddress = USB_DIR_IN, | |
285 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
286 | .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT), | |
bcb2f99c | 287 | .bInterval = USB_MS_TO_HS_INTERVAL(RNDIS_STATUS_INTERVAL_MS) |
04617db7 PZ |
288 | }; |
289 | ||
290 | static struct usb_ss_ep_comp_descriptor ss_intr_comp_desc = { | |
291 | .bLength = sizeof ss_intr_comp_desc, | |
292 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
293 | ||
294 | /* the following 3 values can be tweaked if necessary */ | |
295 | /* .bMaxBurst = 0, */ | |
296 | /* .bmAttributes = 0, */ | |
297 | .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT), | |
298 | }; | |
299 | ||
300 | static struct usb_endpoint_descriptor ss_in_desc = { | |
301 | .bLength = USB_DT_ENDPOINT_SIZE, | |
302 | .bDescriptorType = USB_DT_ENDPOINT, | |
303 | ||
304 | .bEndpointAddress = USB_DIR_IN, | |
305 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
306 | .wMaxPacketSize = cpu_to_le16(1024), | |
307 | }; | |
308 | ||
309 | static struct usb_endpoint_descriptor ss_out_desc = { | |
310 | .bLength = USB_DT_ENDPOINT_SIZE, | |
311 | .bDescriptorType = USB_DT_ENDPOINT, | |
312 | ||
313 | .bEndpointAddress = USB_DIR_OUT, | |
314 | .bmAttributes = USB_ENDPOINT_XFER_BULK, | |
315 | .wMaxPacketSize = cpu_to_le16(1024), | |
316 | }; | |
317 | ||
318 | static struct usb_ss_ep_comp_descriptor ss_bulk_comp_desc = { | |
319 | .bLength = sizeof ss_bulk_comp_desc, | |
320 | .bDescriptorType = USB_DT_SS_ENDPOINT_COMP, | |
321 | ||
322 | /* the following 2 values can be tweaked if necessary */ | |
323 | /* .bMaxBurst = 0, */ | |
324 | /* .bmAttributes = 0, */ | |
325 | }; | |
326 | ||
327 | static struct usb_descriptor_header *eth_ss_function[] = { | |
328 | (struct usb_descriptor_header *) &rndis_iad_descriptor, | |
329 | ||
330 | /* control interface matches ACM, not Ethernet */ | |
331 | (struct usb_descriptor_header *) &rndis_control_intf, | |
332 | (struct usb_descriptor_header *) &header_desc, | |
333 | (struct usb_descriptor_header *) &call_mgmt_descriptor, | |
334 | (struct usb_descriptor_header *) &rndis_acm_descriptor, | |
335 | (struct usb_descriptor_header *) &rndis_union_desc, | |
336 | (struct usb_descriptor_header *) &ss_notify_desc, | |
337 | (struct usb_descriptor_header *) &ss_intr_comp_desc, | |
338 | ||
339 | /* data interface has no altsetting */ | |
340 | (struct usb_descriptor_header *) &rndis_data_intf, | |
341 | (struct usb_descriptor_header *) &ss_in_desc, | |
342 | (struct usb_descriptor_header *) &ss_bulk_comp_desc, | |
343 | (struct usb_descriptor_header *) &ss_out_desc, | |
344 | (struct usb_descriptor_header *) &ss_bulk_comp_desc, | |
345 | NULL, | |
346 | }; | |
347 | ||
45fe3b8e DB |
348 | /* string descriptors: */ |
349 | ||
350 | static struct usb_string rndis_string_defs[] = { | |
351 | [0].s = "RNDIS Communications Control", | |
352 | [1].s = "RNDIS Ethernet Data", | |
b97503ff | 353 | [2].s = "RNDIS", |
45fe3b8e DB |
354 | { } /* end of list */ |
355 | }; | |
356 | ||
357 | static struct usb_gadget_strings rndis_string_table = { | |
358 | .language = 0x0409, /* en-us */ | |
359 | .strings = rndis_string_defs, | |
360 | }; | |
361 | ||
362 | static struct usb_gadget_strings *rndis_strings[] = { | |
363 | &rndis_string_table, | |
364 | NULL, | |
365 | }; | |
366 | ||
367 | /*-------------------------------------------------------------------------*/ | |
368 | ||
9b39e9dd BN |
369 | static struct sk_buff *rndis_add_header(struct gether *port, |
370 | struct sk_buff *skb) | |
45fe3b8e | 371 | { |
9b39e9dd BN |
372 | struct sk_buff *skb2; |
373 | ||
374 | skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type)); | |
375 | if (skb2) | |
376 | rndis_add_hdr(skb2); | |
377 | ||
378 | dev_kfree_skb_any(skb); | |
379 | return skb2; | |
45fe3b8e DB |
380 | } |
381 | ||
382 | static void rndis_response_available(void *_rndis) | |
383 | { | |
384 | struct f_rndis *rndis = _rndis; | |
385 | struct usb_request *req = rndis->notify_req; | |
386 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; | |
387 | __le32 *data = req->buf; | |
388 | int status; | |
389 | ||
ff349505 | 390 | if (atomic_inc_return(&rndis->notify_count) != 1) |
45fe3b8e DB |
391 | return; |
392 | ||
393 | /* Send RNDIS RESPONSE_AVAILABLE notification; a | |
394 | * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too | |
395 | * | |
396 | * This is the only notification defined by RNDIS. | |
397 | */ | |
398 | data[0] = cpu_to_le32(1); | |
399 | data[1] = cpu_to_le32(0); | |
400 | ||
401 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); | |
402 | if (status) { | |
403 | atomic_dec(&rndis->notify_count); | |
404 | DBG(cdev, "notify/0 --> %d\n", status); | |
405 | } | |
406 | } | |
407 | ||
408 | static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req) | |
409 | { | |
410 | struct f_rndis *rndis = req->context; | |
411 | struct usb_composite_dev *cdev = rndis->port.func.config->cdev; | |
412 | int status = req->status; | |
413 | ||
414 | /* after TX: | |
415 | * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control) | |
416 | * - RNDIS_RESPONSE_AVAILABLE (status/irq) | |
417 | */ | |
418 | switch (status) { | |
419 | case -ECONNRESET: | |
420 | case -ESHUTDOWN: | |
421 | /* connection gone */ | |
422 | atomic_set(&rndis->notify_count, 0); | |
423 | break; | |
424 | default: | |
425 | DBG(cdev, "RNDIS %s response error %d, %d/%d\n", | |
426 | ep->name, status, | |
427 | req->actual, req->length); | |
428 | /* FALLTHROUGH */ | |
429 | case 0: | |
430 | if (ep != rndis->notify) | |
431 | break; | |
432 | ||
433 | /* handle multiple pending RNDIS_RESPONSE_AVAILABLE | |
434 | * notifications by resending until we're done | |
435 | */ | |
436 | if (atomic_dec_and_test(&rndis->notify_count)) | |
437 | break; | |
438 | status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC); | |
439 | if (status) { | |
440 | atomic_dec(&rndis->notify_count); | |
441 | DBG(cdev, "notify/1 --> %d\n", status); | |
442 | } | |
443 | break; | |
444 | } | |
445 | } | |
446 | ||
447 | static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req) | |
448 | { | |
449 | struct f_rndis *rndis = req->context; | |
45fe3b8e DB |
450 | int status; |
451 | ||
452 | /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */ | |
453 | // spin_lock(&dev->lock); | |
454 | status = rndis_msg_parser(rndis->config, (u8 *) req->buf); | |
455 | if (status < 0) | |
967baed4 | 456 | pr_err("RNDIS command error %d, %d/%d\n", |
45fe3b8e DB |
457 | status, req->actual, req->length); |
458 | // spin_unlock(&dev->lock); | |
459 | } | |
460 | ||
461 | static int | |
462 | rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) | |
463 | { | |
464 | struct f_rndis *rndis = func_to_rndis(f); | |
465 | struct usb_composite_dev *cdev = f->config->cdev; | |
466 | struct usb_request *req = cdev->req; | |
467 | int value = -EOPNOTSUPP; | |
468 | u16 w_index = le16_to_cpu(ctrl->wIndex); | |
469 | u16 w_value = le16_to_cpu(ctrl->wValue); | |
470 | u16 w_length = le16_to_cpu(ctrl->wLength); | |
471 | ||
472 | /* composite driver infrastructure handles everything except | |
473 | * CDC class messages; interface activation uses set_alt(). | |
474 | */ | |
475 | switch ((ctrl->bRequestType << 8) | ctrl->bRequest) { | |
476 | ||
477 | /* RNDIS uses the CDC command encapsulation mechanism to implement | |
478 | * an RPC scheme, with much getting/setting of attributes by OID. | |
479 | */ | |
480 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | |
481 | | USB_CDC_SEND_ENCAPSULATED_COMMAND: | |
472b9127 | 482 | if (w_value || w_index != rndis->ctrl_id) |
45fe3b8e DB |
483 | goto invalid; |
484 | /* read the request; process it later */ | |
485 | value = w_length; | |
486 | req->complete = rndis_command_complete; | |
487 | req->context = rndis; | |
488 | /* later, rndis_response_available() sends a notification */ | |
489 | break; | |
490 | ||
491 | case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8) | |
492 | | USB_CDC_GET_ENCAPSULATED_RESPONSE: | |
493 | if (w_value || w_index != rndis->ctrl_id) | |
494 | goto invalid; | |
495 | else { | |
496 | u8 *buf; | |
497 | u32 n; | |
498 | ||
499 | /* return the result */ | |
500 | buf = rndis_get_next_response(rndis->config, &n); | |
501 | if (buf) { | |
502 | memcpy(req->buf, buf, n); | |
503 | req->complete = rndis_response_complete; | |
f1356172 | 504 | req->context = rndis; |
45fe3b8e DB |
505 | rndis_free_response(rndis->config, buf); |
506 | value = n; | |
507 | } | |
508 | /* else stalls ... spec says to avoid that */ | |
509 | } | |
510 | break; | |
511 | ||
512 | default: | |
513 | invalid: | |
514 | VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n", | |
515 | ctrl->bRequestType, ctrl->bRequest, | |
516 | w_value, w_index, w_length); | |
517 | } | |
518 | ||
519 | /* respond with data transfer or status phase? */ | |
520 | if (value >= 0) { | |
521 | DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n", | |
522 | ctrl->bRequestType, ctrl->bRequest, | |
523 | w_value, w_index, w_length); | |
090b9011 | 524 | req->zero = (value < w_length); |
45fe3b8e DB |
525 | req->length = value; |
526 | value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC); | |
527 | if (value < 0) | |
528 | ERROR(cdev, "rndis response on err %d\n", value); | |
529 | } | |
530 | ||
531 | /* device either stalls (value < 0) or reports success */ | |
532 | return value; | |
533 | } | |
534 | ||
535 | ||
536 | static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt) | |
537 | { | |
538 | struct f_rndis *rndis = func_to_rndis(f); | |
539 | struct usb_composite_dev *cdev = f->config->cdev; | |
540 | ||
541 | /* we know alt == 0 */ | |
542 | ||
543 | if (intf == rndis->ctrl_id) { | |
544 | if (rndis->notify->driver_data) { | |
545 | VDBG(cdev, "reset rndis control %d\n", intf); | |
546 | usb_ep_disable(rndis->notify); | |
ea2a1df7 TB |
547 | } |
548 | if (!rndis->notify->desc) { | |
45fe3b8e | 549 | VDBG(cdev, "init rndis ctrl %d\n", intf); |
ea2a1df7 TB |
550 | if (config_ep_by_speed(cdev->gadget, f, rndis->notify)) |
551 | goto fail; | |
45fe3b8e | 552 | } |
72c973dd | 553 | usb_ep_enable(rndis->notify); |
45fe3b8e DB |
554 | rndis->notify->driver_data = rndis; |
555 | ||
556 | } else if (intf == rndis->data_id) { | |
557 | struct net_device *net; | |
558 | ||
559 | if (rndis->port.in_ep->driver_data) { | |
560 | DBG(cdev, "reset rndis\n"); | |
561 | gether_disconnect(&rndis->port); | |
830d1b18 MM |
562 | } |
563 | ||
ea2a1df7 | 564 | if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) { |
45fe3b8e | 565 | DBG(cdev, "init rndis\n"); |
ea2a1df7 TB |
566 | if (config_ep_by_speed(cdev->gadget, f, |
567 | rndis->port.in_ep) || | |
568 | config_ep_by_speed(cdev->gadget, f, | |
569 | rndis->port.out_ep)) { | |
570 | rndis->port.in_ep->desc = NULL; | |
571 | rndis->port.out_ep->desc = NULL; | |
572 | goto fail; | |
573 | } | |
45fe3b8e DB |
574 | } |
575 | ||
576 | /* Avoid ZLPs; they can be troublesome. */ | |
577 | rndis->port.is_zlp_ok = false; | |
578 | ||
579 | /* RNDIS should be in the "RNDIS uninitialized" state, | |
580 | * either never activated or after rndis_uninit(). | |
581 | * | |
582 | * We don't want data to flow here until a nonzero packet | |
583 | * filter is set, at which point it enters "RNDIS data | |
584 | * initialized" state ... but we do want the endpoints | |
585 | * to be activated. It's a strange little state. | |
586 | * | |
587 | * REVISIT the RNDIS gadget code has done this wrong for a | |
588 | * very long time. We need another call to the link layer | |
589 | * code -- gether_updown(...bool) maybe -- to do it right. | |
590 | */ | |
591 | rndis->port.cdc_filter = 0; | |
592 | ||
593 | DBG(cdev, "RNDIS RX/TX early activation ... \n"); | |
594 | net = gether_connect(&rndis->port); | |
595 | if (IS_ERR(net)) | |
596 | return PTR_ERR(net); | |
597 | ||
598 | rndis_set_param_dev(rndis->config, net, | |
599 | &rndis->port.cdc_filter); | |
600 | } else | |
601 | goto fail; | |
602 | ||
603 | return 0; | |
604 | fail: | |
605 | return -EINVAL; | |
606 | } | |
607 | ||
608 | static void rndis_disable(struct usb_function *f) | |
609 | { | |
610 | struct f_rndis *rndis = func_to_rndis(f); | |
611 | struct usb_composite_dev *cdev = f->config->cdev; | |
612 | ||
613 | if (!rndis->notify->driver_data) | |
614 | return; | |
615 | ||
616 | DBG(cdev, "rndis deactivated\n"); | |
617 | ||
618 | rndis_uninit(rndis->config); | |
619 | gether_disconnect(&rndis->port); | |
620 | ||
621 | usb_ep_disable(rndis->notify); | |
622 | rndis->notify->driver_data = NULL; | |
623 | } | |
624 | ||
625 | /*-------------------------------------------------------------------------*/ | |
626 | ||
627 | /* | |
628 | * This isn't quite the same mechanism as CDC Ethernet, since the | |
629 | * notification scheme passes less data, but the same set of link | |
630 | * states must be tested. A key difference is that altsettings are | |
631 | * not used to tell whether the link should send packets or not. | |
632 | */ | |
633 | ||
634 | static void rndis_open(struct gether *geth) | |
635 | { | |
636 | struct f_rndis *rndis = func_to_rndis(&geth->func); | |
637 | struct usb_composite_dev *cdev = geth->func.config->cdev; | |
638 | ||
639 | DBG(cdev, "%s\n", __func__); | |
640 | ||
17c51b6c | 641 | rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, |
45fe3b8e DB |
642 | bitrate(cdev->gadget) / 100); |
643 | rndis_signal_connect(rndis->config); | |
644 | } | |
645 | ||
646 | static void rndis_close(struct gether *geth) | |
647 | { | |
648 | struct f_rndis *rndis = func_to_rndis(&geth->func); | |
649 | ||
650 | DBG(geth->func.config->cdev, "%s\n", __func__); | |
651 | ||
17c51b6c | 652 | rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0); |
45fe3b8e DB |
653 | rndis_signal_disconnect(rndis->config); |
654 | } | |
655 | ||
656 | /*-------------------------------------------------------------------------*/ | |
657 | ||
658 | /* ethernet function driver setup/binding */ | |
659 | ||
28824b18 | 660 | static int |
45fe3b8e DB |
661 | rndis_bind(struct usb_configuration *c, struct usb_function *f) |
662 | { | |
663 | struct usb_composite_dev *cdev = c->cdev; | |
664 | struct f_rndis *rndis = func_to_rndis(f); | |
665 | int status; | |
666 | struct usb_ep *ep; | |
667 | ||
668 | /* allocate instance-specific interface IDs */ | |
669 | status = usb_interface_id(c, f); | |
670 | if (status < 0) | |
671 | goto fail; | |
672 | rndis->ctrl_id = status; | |
b97503ff | 673 | rndis_iad_descriptor.bFirstInterface = status; |
45fe3b8e DB |
674 | |
675 | rndis_control_intf.bInterfaceNumber = status; | |
676 | rndis_union_desc.bMasterInterface0 = status; | |
677 | ||
678 | status = usb_interface_id(c, f); | |
679 | if (status < 0) | |
680 | goto fail; | |
681 | rndis->data_id = status; | |
682 | ||
683 | rndis_data_intf.bInterfaceNumber = status; | |
684 | rndis_union_desc.bSlaveInterface0 = status; | |
685 | ||
686 | status = -ENODEV; | |
687 | ||
688 | /* allocate instance-specific endpoints */ | |
689 | ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc); | |
690 | if (!ep) | |
691 | goto fail; | |
692 | rndis->port.in_ep = ep; | |
693 | ep->driver_data = cdev; /* claim */ | |
694 | ||
695 | ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc); | |
696 | if (!ep) | |
697 | goto fail; | |
698 | rndis->port.out_ep = ep; | |
699 | ep->driver_data = cdev; /* claim */ | |
700 | ||
701 | /* NOTE: a status/notification endpoint is, strictly speaking, | |
702 | * optional. We don't treat it that way though! It's simpler, | |
703 | * and some newer profiles don't treat it as optional. | |
704 | */ | |
705 | ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc); | |
706 | if (!ep) | |
707 | goto fail; | |
708 | rndis->notify = ep; | |
709 | ep->driver_data = cdev; /* claim */ | |
710 | ||
711 | status = -ENOMEM; | |
712 | ||
713 | /* allocate notification request and buffer */ | |
714 | rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL); | |
715 | if (!rndis->notify_req) | |
716 | goto fail; | |
717 | rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL); | |
718 | if (!rndis->notify_req->buf) | |
719 | goto fail; | |
720 | rndis->notify_req->length = STATUS_BYTECOUNT; | |
721 | rndis->notify_req->context = rndis; | |
722 | rndis->notify_req->complete = rndis_response_complete; | |
723 | ||
45fe3b8e DB |
724 | /* support all relevant hardware speeds... we expect that when |
725 | * hardware is dual speed, all bulk-capable endpoints work at | |
726 | * both speeds | |
727 | */ | |
10287bae SAS |
728 | hs_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress; |
729 | hs_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress; | |
730 | hs_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress; | |
45fe3b8e | 731 | |
10287bae SAS |
732 | ss_in_desc.bEndpointAddress = fs_in_desc.bEndpointAddress; |
733 | ss_out_desc.bEndpointAddress = fs_out_desc.bEndpointAddress; | |
734 | ss_notify_desc.bEndpointAddress = fs_notify_desc.bEndpointAddress; | |
735 | ||
736 | status = usb_assign_descriptors(f, eth_fs_function, eth_hs_function, | |
737 | eth_ss_function); | |
738 | if (status) | |
739 | goto fail; | |
04617db7 | 740 | |
45fe3b8e DB |
741 | rndis->port.open = rndis_open; |
742 | rndis->port.close = rndis_close; | |
743 | ||
744 | status = rndis_register(rndis_response_available, rndis); | |
745 | if (status < 0) | |
746 | goto fail; | |
747 | rndis->config = status; | |
748 | ||
17c51b6c | 749 | rndis_set_param_medium(rndis->config, RNDIS_MEDIUM_802_3, 0); |
45fe3b8e DB |
750 | rndis_set_host_mac(rndis->config, rndis->ethaddr); |
751 | ||
1fbfeff9 BG |
752 | if (rndis->manufacturer && rndis->vendorID && |
753 | rndis_set_param_vendor(rndis->config, rndis->vendorID, | |
754 | rndis->manufacturer)) | |
755 | goto fail; | |
45fe3b8e DB |
756 | |
757 | /* NOTE: all that is done without knowing or caring about | |
758 | * the network link ... which is unavailable to this code | |
759 | * until we're activated via set_alt(). | |
760 | */ | |
761 | ||
762 | DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n", | |
04617db7 | 763 | gadget_is_superspeed(c->cdev->gadget) ? "super" : |
45fe3b8e DB |
764 | gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full", |
765 | rndis->port.in_ep->name, rndis->port.out_ep->name, | |
766 | rndis->notify->name); | |
767 | return 0; | |
768 | ||
769 | fail: | |
10287bae | 770 | usb_free_all_descriptors(f); |
45fe3b8e DB |
771 | |
772 | if (rndis->notify_req) { | |
773 | kfree(rndis->notify_req->buf); | |
774 | usb_ep_free_request(rndis->notify, rndis->notify_req); | |
775 | } | |
776 | ||
777 | /* we might as well release our claims on endpoints */ | |
778 | if (rndis->notify) | |
779 | rndis->notify->driver_data = NULL; | |
e79cc615 | 780 | if (rndis->port.out_ep) |
45fe3b8e | 781 | rndis->port.out_ep->driver_data = NULL; |
e79cc615 | 782 | if (rndis->port.in_ep) |
45fe3b8e DB |
783 | rndis->port.in_ep->driver_data = NULL; |
784 | ||
785 | ERROR(cdev, "%s: can't bind, err %d\n", f->name, status); | |
786 | ||
787 | return status; | |
788 | } | |
789 | ||
790 | static void | |
791 | rndis_unbind(struct usb_configuration *c, struct usb_function *f) | |
792 | { | |
793 | struct f_rndis *rndis = func_to_rndis(f); | |
794 | ||
795 | rndis_deregister(rndis->config); | |
796 | rndis_exit(); | |
797 | ||
1616e99d | 798 | rndis_string_defs[0].id = 0; |
10287bae | 799 | usb_free_all_descriptors(f); |
45fe3b8e DB |
800 | |
801 | kfree(rndis->notify_req->buf); | |
802 | usb_ep_free_request(rndis->notify, rndis->notify_req); | |
803 | ||
804 | kfree(rndis); | |
805 | } | |
806 | ||
807 | /* Some controllers can't support RNDIS ... */ | |
808 | static inline bool can_support_rndis(struct usb_configuration *c) | |
809 | { | |
45fe3b8e DB |
810 | /* everything else is *presumably* fine */ |
811 | return true; | |
812 | } | |
813 | ||
28824b18 | 814 | int |
1fbfeff9 | 815 | rndis_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN], |
d6a01439 | 816 | u32 vendorID, const char *manufacturer, struct eth_dev *dev) |
45fe3b8e DB |
817 | { |
818 | struct f_rndis *rndis; | |
819 | int status; | |
820 | ||
821 | if (!can_support_rndis(c) || !ethaddr) | |
822 | return -EINVAL; | |
823 | ||
45fe3b8e | 824 | if (rndis_string_defs[0].id == 0) { |
45fe3b8e DB |
825 | /* ... and setup RNDIS itself */ |
826 | status = rndis_init(); | |
827 | if (status < 0) | |
828 | return status; | |
829 | ||
1616e99d SAS |
830 | status = usb_string_ids_tab(c->cdev, rndis_string_defs); |
831 | if (status) | |
45fe3b8e | 832 | return status; |
45fe3b8e | 833 | |
1616e99d SAS |
834 | rndis_control_intf.iInterface = rndis_string_defs[0].id; |
835 | rndis_data_intf.iInterface = rndis_string_defs[1].id; | |
836 | rndis_iad_descriptor.iFunction = rndis_string_defs[2].id; | |
45fe3b8e DB |
837 | } |
838 | ||
839 | /* allocate and initialize one new instance */ | |
840 | status = -ENOMEM; | |
841 | rndis = kzalloc(sizeof *rndis, GFP_KERNEL); | |
842 | if (!rndis) | |
843 | goto fail; | |
844 | ||
845 | memcpy(rndis->ethaddr, ethaddr, ETH_ALEN); | |
1fbfeff9 BG |
846 | rndis->vendorID = vendorID; |
847 | rndis->manufacturer = manufacturer; | |
45fe3b8e | 848 | |
d6a01439 | 849 | rndis->port.ioport = dev; |
45fe3b8e DB |
850 | /* RNDIS activates when the host changes this filter */ |
851 | rndis->port.cdc_filter = 0; | |
852 | ||
853 | /* RNDIS has special (and complex) framing */ | |
854 | rndis->port.header_len = sizeof(struct rndis_packet_msg_type); | |
855 | rndis->port.wrap = rndis_add_header; | |
856 | rndis->port.unwrap = rndis_rm_hdr; | |
857 | ||
858 | rndis->port.func.name = "rndis"; | |
859 | rndis->port.func.strings = rndis_strings; | |
860 | /* descriptors are per-instance copies */ | |
861 | rndis->port.func.bind = rndis_bind; | |
862 | rndis->port.func.unbind = rndis_unbind; | |
863 | rndis->port.func.set_alt = rndis_set_alt; | |
864 | rndis->port.func.setup = rndis_setup; | |
865 | rndis->port.func.disable = rndis_disable; | |
866 | ||
867 | status = usb_add_function(c, &rndis->port.func); | |
868 | if (status) { | |
869 | kfree(rndis); | |
870 | fail: | |
871 | rndis_exit(); | |
872 | } | |
873 | return status; | |
874 | } |