blob: c1ab5522fc46480f513b3d759f16dd8fd3e52e17 [file] [log] [blame]
Ofir Cohenaef90b72012-07-31 12:37:04 +02001/*
2 * f_qc_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
7 * Copyright (C) 2009 Samsung Electronics
8 * Author: Michal Nazarewicz (mina86@mina86.com)
Amit Blayf9b352b2013-03-04 15:01:40 +02009 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Ofir Cohenaef90b72012-07-31 12:37:04 +020010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24/* #define VERBOSE_DEBUG */
25
26#include <linux/slab.h>
27#include <linux/kernel.h>
28#include <linux/device.h>
29#include <linux/etherdevice.h>
30
31#include <linux/atomic.h>
32
33#include "u_ether.h"
34#include "u_qc_ether.h"
35#include "rndis.h"
36
37
38/*
39 * This function is an RNDIS Ethernet port -- a Microsoft protocol that's
40 * been promoted instead of the standard CDC Ethernet. The published RNDIS
41 * spec is ambiguous, incomplete, and needlessly complex. Variants such as
42 * ActiveSync have even worse status in terms of specification.
43 *
44 * In short: it's a protocol controlled by (and for) Microsoft, not for an
45 * Open ecosystem or markets. Linux supports it *only* because Microsoft
46 * doesn't support the CDC Ethernet standard.
47 *
48 * The RNDIS data transfer model is complex, with multiple Ethernet packets
49 * per USB message, and out of band data. The control model is built around
50 * what's essentially an "RNDIS RPC" protocol. It's all wrapped in a CDC ACM
51 * (modem, not Ethernet) veneer, with those ACM descriptors being entirely
52 * useless (they're ignored). RNDIS expects to be the only function in its
53 * configuration, so it's no real help if you need composite devices; and
54 * it expects to be the first configuration too.
55 *
56 * There is a single technical advantage of RNDIS over CDC Ethernet, if you
57 * discount the fluff that its RPC can be made to deliver: it doesn't need
58 * a NOP altsetting for the data interface. That lets it work on some of the
59 * "so smart it's stupid" hardware which takes over configuration changes
60 * from the software, and adds restrictions like "no altsettings".
61 *
62 * Unfortunately MSFT's RNDIS drivers are buggy. They hang or oops, and
63 * have all sorts of contrary-to-specification oddities that can prevent
64 * them from working sanely. Since bugfixes (or accurate specs, letting
65 * Linux work around those bugs) are unlikely to ever come from MSFT, you
66 * may want to avoid using RNDIS on purely operational grounds.
67 *
68 * Omissions from the RNDIS 1.0 specification include:
69 *
70 * - Power management ... references data that's scattered around lots
71 * of other documentation, which is incorrect/incomplete there too.
72 *
73 * - There are various undocumented protocol requirements, like the need
74 * to send garbage in some control-OUT messages.
75 *
76 * - MS-Windows drivers sometimes emit undocumented requests.
77 *
78 * This function is based on RNDIS link function driver and
79 * contains MSM specific implementation.
80 */
81
82struct f_rndis_qc {
83 struct qc_gether port;
84 u8 ctrl_id, data_id;
85 u8 ethaddr[ETH_ALEN];
86 u32 vendorID;
87 u8 max_pkt_per_xfer;
Anna Perelce47ed42012-12-05 14:31:07 +020088 u32 max_pkt_size;
Ofir Cohenaef90b72012-07-31 12:37:04 +020089 const char *manufacturer;
90 int config;
91 atomic_t ioctl_excl;
92 atomic_t open_excl;
93
94 struct usb_ep *notify;
95 struct usb_request *notify_req;
96 atomic_t notify_count;
Anna Perela6d730742012-12-17 09:43:16 +020097 struct data_port bam_port;
Ofir Cohenaef90b72012-07-31 12:37:04 +020098};
99
100static inline struct f_rndis_qc *func_to_rndis_qc(struct usb_function *f)
101{
102 return container_of(f, struct f_rndis_qc, port.func);
103}
104
105/* peak (theoretical) bulk transfer rate in bits-per-second */
106static unsigned int rndis_qc_bitrate(struct usb_gadget *g)
107{
108 if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
109 return 13 * 1024 * 8 * 1000 * 8;
110 else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
111 return 13 * 512 * 8 * 1000 * 8;
112 else
113 return 19 * 64 * 1 * 1000 * 8;
114}
115
116/*-------------------------------------------------------------------------*/
117
118#define RNDIS_QC_LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
119#define RNDIS_QC_STATUS_BYTECOUNT 8 /* 8 bytes data */
120
Anna Perela6d730742012-12-17 09:43:16 +0200121/* currently only one rndis instance is supported - port
122 * index 0.
123 */
124#define RNDIS_QC_NO_PORTS 1
125#define RNDIS_QC_ACTIVE_PORT 0
Ofir Cohenaef90b72012-07-31 12:37:04 +0200126
127/* default max packets per tarnsfer value */
128#define DEFAULT_MAX_PKT_PER_XFER 15
129
130
131#define RNDIS_QC_IOCTL_MAGIC 'i'
132#define RNDIS_QC_GET_MAX_PKT_PER_XFER _IOR(RNDIS_QC_IOCTL_MAGIC, 1, u8)
Anna Perelce47ed42012-12-05 14:31:07 +0200133#define RNDIS_QC_GET_MAX_PKT_SIZE _IOR(RNDIS_QC_IOCTL_MAGIC, 2, u32)
Ofir Cohenaef90b72012-07-31 12:37:04 +0200134
135
136/* interface descriptor: */
137
138static struct usb_interface_descriptor rndis_qc_control_intf = {
139 .bLength = sizeof rndis_qc_control_intf,
140 .bDescriptorType = USB_DT_INTERFACE,
141
142 /* .bInterfaceNumber = DYNAMIC */
143 /* status endpoint is optional; this could be patched later */
144 .bNumEndpoints = 1,
145 .bInterfaceClass = USB_CLASS_COMM,
146 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
147 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
148 /* .iInterface = DYNAMIC */
149};
150
151static struct usb_cdc_header_desc rndis_qc_header_desc = {
152 .bLength = sizeof rndis_qc_header_desc,
153 .bDescriptorType = USB_DT_CS_INTERFACE,
154 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
155
156 .bcdCDC = cpu_to_le16(0x0110),
157};
158
159static struct usb_cdc_call_mgmt_descriptor rndis_qc_call_mgmt_descriptor = {
160 .bLength = sizeof rndis_qc_call_mgmt_descriptor,
161 .bDescriptorType = USB_DT_CS_INTERFACE,
162 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
163
164 .bmCapabilities = 0x00,
165 .bDataInterface = 0x01,
166};
167
168static struct usb_cdc_acm_descriptor rndis_qc_acm_descriptor = {
169 .bLength = sizeof rndis_qc_acm_descriptor,
170 .bDescriptorType = USB_DT_CS_INTERFACE,
171 .bDescriptorSubType = USB_CDC_ACM_TYPE,
172
173 .bmCapabilities = 0x00,
174};
175
176static struct usb_cdc_union_desc rndis_qc_union_desc = {
177 .bLength = sizeof(rndis_qc_union_desc),
178 .bDescriptorType = USB_DT_CS_INTERFACE,
179 .bDescriptorSubType = USB_CDC_UNION_TYPE,
180 /* .bMasterInterface0 = DYNAMIC */
181 /* .bSlaveInterface0 = DYNAMIC */
182};
183
184/* the data interface has two bulk endpoints */
185
186static struct usb_interface_descriptor rndis_qc_data_intf = {
187 .bLength = sizeof rndis_qc_data_intf,
188 .bDescriptorType = USB_DT_INTERFACE,
189
190 /* .bInterfaceNumber = DYNAMIC */
191 .bNumEndpoints = 2,
192 .bInterfaceClass = USB_CLASS_CDC_DATA,
193 .bInterfaceSubClass = 0,
194 .bInterfaceProtocol = 0,
195 /* .iInterface = DYNAMIC */
196};
197
198
199static struct usb_interface_assoc_descriptor
200rndis_qc_iad_descriptor = {
201 .bLength = sizeof rndis_qc_iad_descriptor,
202 .bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
203 .bFirstInterface = 0, /* XXX, hardcoded */
204 .bInterfaceCount = 2, /* control + data */
205 .bFunctionClass = USB_CLASS_COMM,
206 .bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
207 .bFunctionProtocol = USB_CDC_PROTO_NONE,
208 /* .iFunction = DYNAMIC */
209};
210
211/* full speed support: */
212
213static struct usb_endpoint_descriptor rndis_qc_fs_notify_desc = {
214 .bLength = USB_DT_ENDPOINT_SIZE,
215 .bDescriptorType = USB_DT_ENDPOINT,
216
217 .bEndpointAddress = USB_DIR_IN,
218 .bmAttributes = USB_ENDPOINT_XFER_INT,
219 .wMaxPacketSize = cpu_to_le16(RNDIS_QC_STATUS_BYTECOUNT),
220 .bInterval = 1 << RNDIS_QC_LOG2_STATUS_INTERVAL_MSEC,
221};
222
223static struct usb_endpoint_descriptor rndis_qc_fs_in_desc = {
224 .bLength = USB_DT_ENDPOINT_SIZE,
225 .bDescriptorType = USB_DT_ENDPOINT,
226
227 .bEndpointAddress = USB_DIR_IN,
228 .bmAttributes = USB_ENDPOINT_XFER_BULK,
229};
230
231static struct usb_endpoint_descriptor rndis_qc_fs_out_desc = {
232 .bLength = USB_DT_ENDPOINT_SIZE,
233 .bDescriptorType = USB_DT_ENDPOINT,
234
235 .bEndpointAddress = USB_DIR_OUT,
236 .bmAttributes = USB_ENDPOINT_XFER_BULK,
237};
238
239static struct usb_descriptor_header *eth_qc_fs_function[] = {
240 (struct usb_descriptor_header *) &rndis_qc_iad_descriptor,
241 /* control interface matches ACM, not Ethernet */
242 (struct usb_descriptor_header *) &rndis_qc_control_intf,
243 (struct usb_descriptor_header *) &rndis_qc_header_desc,
244 (struct usb_descriptor_header *) &rndis_qc_call_mgmt_descriptor,
245 (struct usb_descriptor_header *) &rndis_qc_acm_descriptor,
246 (struct usb_descriptor_header *) &rndis_qc_union_desc,
247 (struct usb_descriptor_header *) &rndis_qc_fs_notify_desc,
248 /* data interface has no altsetting */
249 (struct usb_descriptor_header *) &rndis_qc_data_intf,
250 (struct usb_descriptor_header *) &rndis_qc_fs_in_desc,
251 (struct usb_descriptor_header *) &rndis_qc_fs_out_desc,
252 NULL,
253};
254
255/* high speed support: */
256
257static struct usb_endpoint_descriptor rndis_qc_hs_notify_desc = {
258 .bLength = USB_DT_ENDPOINT_SIZE,
259 .bDescriptorType = USB_DT_ENDPOINT,
260
261 .bEndpointAddress = USB_DIR_IN,
262 .bmAttributes = USB_ENDPOINT_XFER_INT,
263 .wMaxPacketSize = cpu_to_le16(RNDIS_QC_STATUS_BYTECOUNT),
264 .bInterval = RNDIS_QC_LOG2_STATUS_INTERVAL_MSEC + 4,
265};
266static struct usb_endpoint_descriptor rndis_qc_hs_in_desc = {
267 .bLength = USB_DT_ENDPOINT_SIZE,
268 .bDescriptorType = USB_DT_ENDPOINT,
269
270 .bEndpointAddress = USB_DIR_IN,
271 .bmAttributes = USB_ENDPOINT_XFER_BULK,
272 .wMaxPacketSize = cpu_to_le16(512),
273};
274
275static struct usb_endpoint_descriptor rndis_qc_hs_out_desc = {
276 .bLength = USB_DT_ENDPOINT_SIZE,
277 .bDescriptorType = USB_DT_ENDPOINT,
278
279 .bEndpointAddress = USB_DIR_OUT,
280 .bmAttributes = USB_ENDPOINT_XFER_BULK,
281 .wMaxPacketSize = cpu_to_le16(512),
282};
283
284static struct usb_descriptor_header *eth_qc_hs_function[] = {
285 (struct usb_descriptor_header *) &rndis_qc_iad_descriptor,
286 /* control interface matches ACM, not Ethernet */
287 (struct usb_descriptor_header *) &rndis_qc_control_intf,
288 (struct usb_descriptor_header *) &rndis_qc_header_desc,
289 (struct usb_descriptor_header *) &rndis_qc_call_mgmt_descriptor,
290 (struct usb_descriptor_header *) &rndis_qc_acm_descriptor,
291 (struct usb_descriptor_header *) &rndis_qc_union_desc,
292 (struct usb_descriptor_header *) &rndis_qc_hs_notify_desc,
293 /* data interface has no altsetting */
294 (struct usb_descriptor_header *) &rndis_qc_data_intf,
295 (struct usb_descriptor_header *) &rndis_qc_hs_in_desc,
296 (struct usb_descriptor_header *) &rndis_qc_hs_out_desc,
297 NULL,
298};
299
300/* super speed support: */
301
302static struct usb_endpoint_descriptor rndis_qc_ss_notify_desc = {
303 .bLength = USB_DT_ENDPOINT_SIZE,
304 .bDescriptorType = USB_DT_ENDPOINT,
305
306 .bEndpointAddress = USB_DIR_IN,
307 .bmAttributes = USB_ENDPOINT_XFER_INT,
308 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
309 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
310};
311
312static struct usb_ss_ep_comp_descriptor rndis_qc_ss_intr_comp_desc = {
313 .bLength = sizeof ss_intr_comp_desc,
314 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
315
316 /* the following 3 values can be tweaked if necessary */
317 /* .bMaxBurst = 0, */
318 /* .bmAttributes = 0, */
319 .wBytesPerInterval = cpu_to_le16(STATUS_BYTECOUNT),
320};
321
322static struct usb_endpoint_descriptor rndis_qc_ss_in_desc = {
323 .bLength = USB_DT_ENDPOINT_SIZE,
324 .bDescriptorType = USB_DT_ENDPOINT,
325
326 .bEndpointAddress = USB_DIR_IN,
327 .bmAttributes = USB_ENDPOINT_XFER_BULK,
328 .wMaxPacketSize = cpu_to_le16(1024),
329};
330
331static struct usb_endpoint_descriptor rndis_qc_ss_out_desc = {
332 .bLength = USB_DT_ENDPOINT_SIZE,
333 .bDescriptorType = USB_DT_ENDPOINT,
334
335 .bEndpointAddress = USB_DIR_OUT,
336 .bmAttributes = USB_ENDPOINT_XFER_BULK,
337 .wMaxPacketSize = cpu_to_le16(1024),
338};
339
340static struct usb_ss_ep_comp_descriptor rndis_qc_ss_bulk_comp_desc = {
341 .bLength = sizeof ss_bulk_comp_desc,
342 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
343
344 /* the following 2 values can be tweaked if necessary */
345 /* .bMaxBurst = 0, */
346 /* .bmAttributes = 0, */
347};
348
349static struct usb_descriptor_header *eth_qc_ss_function[] = {
350 (struct usb_descriptor_header *) &rndis_iad_descriptor,
351
352 /* control interface matches ACM, not Ethernet */
353 (struct usb_descriptor_header *) &rndis_qc_control_intf,
354 (struct usb_descriptor_header *) &rndis_qc_header_desc,
355 (struct usb_descriptor_header *) &rndis_qc_call_mgmt_descriptor,
356 (struct usb_descriptor_header *) &rndis_qc_acm_descriptor,
357 (struct usb_descriptor_header *) &rndis_qc_union_desc,
358 (struct usb_descriptor_header *) &rndis_qc_ss_notify_desc,
359 (struct usb_descriptor_header *) &rndis_qc_ss_intr_comp_desc,
360
361 /* data interface has no altsetting */
362 (struct usb_descriptor_header *) &rndis_qc_data_intf,
363 (struct usb_descriptor_header *) &rndis_qc_ss_in_desc,
364 (struct usb_descriptor_header *) &rndis_qc_ss_bulk_comp_desc,
365 (struct usb_descriptor_header *) &rndis_qc_ss_out_desc,
366 (struct usb_descriptor_header *) &rndis_qc_ss_bulk_comp_desc,
367 NULL,
368};
369
370/* string descriptors: */
371
372static struct usb_string rndis_qc_string_defs[] = {
373 [0].s = "RNDIS Communications Control",
374 [1].s = "RNDIS Ethernet Data",
375 [2].s = "RNDIS",
376 { } /* end of list */
377};
378
379static struct usb_gadget_strings rndis_qc_string_table = {
380 .language = 0x0409, /* en-us */
381 .strings = rndis_qc_string_defs,
382};
383
384static struct usb_gadget_strings *rndis_qc_strings[] = {
385 &rndis_qc_string_table,
386 NULL,
387};
388
389struct f_rndis_qc *_rndis_qc;
390
391static inline int rndis_qc_lock(atomic_t *excl)
392{
393 if (atomic_inc_return(excl) == 1) {
394 return 0;
395 } else {
396 atomic_dec(excl);
397 return -EBUSY;
398 }
399}
400
401static inline void rndis_qc_unlock(atomic_t *excl)
402{
403 atomic_dec(excl);
404}
405
406/* MSM bam support */
Ofir Cohenaef90b72012-07-31 12:37:04 +0200407
408static int rndis_qc_bam_setup(void)
409{
410 int ret;
411
412 ret = bam_data_setup(RNDIS_QC_NO_PORTS);
413 if (ret) {
414 pr_err("bam_data_setup failed err: %d\n", ret);
415 return ret;
416 }
417
418 return 0;
419}
420
421static int rndis_qc_bam_connect(struct f_rndis_qc *dev)
422{
423 int ret;
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200424 u8 src_connection_idx, dst_connection_idx;
425 struct usb_composite_dev *cdev = dev->port.func.config->cdev;
426 struct usb_gadget *gadget = cdev->gadget;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200427
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200428 dev->bam_port.cdev = cdev;
Anna Perela83ca802013-05-13 15:11:23 +0300429 dev->bam_port.func = &dev->port.func;
Anna Perela6d730742012-12-17 09:43:16 +0200430 dev->bam_port.in = dev->port.in_ep;
431 dev->bam_port.out = dev->port.out_ep;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200432
433 /* currently we use the first connection */
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200434 src_connection_idx = usb_bam_get_connection_idx(gadget->name, A2_P_BAM,
435 USB_TO_PEER_PERIPHERAL, 0);
436 dst_connection_idx = usb_bam_get_connection_idx(gadget->name, A2_P_BAM,
437 PEER_PERIPHERAL_TO_USB, 0);
438 if (src_connection_idx < 0 || dst_connection_idx < 0) {
439 pr_err("%s: usb_bam_get_connection_idx failed\n", __func__);
440 return ret;
441 }
Amit Blayf9b352b2013-03-04 15:01:40 +0200442 ret = bam_data_connect(&dev->bam_port, 0, USB_GADGET_XPORT_BAM2BAM,
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200443 src_connection_idx, dst_connection_idx, USB_FUNC_RNDIS);
Ofir Cohenaef90b72012-07-31 12:37:04 +0200444 if (ret) {
445 pr_err("bam_data_connect failed: err:%d\n",
446 ret);
447 return ret;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200448 }
449
Shimrit Malichidbf43d72013-03-16 03:32:27 +0200450 pr_info("rndis bam connected\n");
451
Ofir Cohenaef90b72012-07-31 12:37:04 +0200452 return 0;
453}
454
455static int rndis_qc_bam_disconnect(struct f_rndis_qc *dev)
456{
Amit Blay51bebe92012-12-25 18:48:10 +0200457 pr_debug("dev:%p. %s Disconnect BAM.\n", dev, __func__);
458
459 bam_data_disconnect(&dev->bam_port, 0);
Ofir Cohenaef90b72012-07-31 12:37:04 +0200460
461 return 0;
462}
463
464/*-------------------------------------------------------------------------*/
465
466static struct sk_buff *rndis_qc_add_header(struct qc_gether *port,
467 struct sk_buff *skb)
468{
469 struct sk_buff *skb2;
470
471 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
472 if (skb2)
473 rndis_add_hdr(skb2);
474
475 dev_kfree_skb_any(skb);
476 return skb2;
477}
478
479int rndis_qc_rm_hdr(struct qc_gether *port,
480 struct sk_buff *skb,
481 struct sk_buff_head *list)
482{
483 /* tmp points to a struct rndis_packet_msg_type */
484 __le32 *tmp = (void *)skb->data;
485
486 /* MessageType, MessageLength */
487 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
488 != get_unaligned(tmp++)) {
489 dev_kfree_skb_any(skb);
490 return -EINVAL;
491 }
492 tmp++;
493
494 /* DataOffset, DataLength */
495 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
496 dev_kfree_skb_any(skb);
497 return -EOVERFLOW;
498 }
499 skb_trim(skb, get_unaligned_le32(tmp++));
500
501 skb_queue_tail(list, skb);
502 return 0;
503}
504
505
506static void rndis_qc_response_available(void *_rndis)
507{
508 struct f_rndis_qc *rndis = _rndis;
509 struct usb_request *req = rndis->notify_req;
510 __le32 *data = req->buf;
511 int status;
512
513 if (atomic_inc_return(&rndis->notify_count) != 1)
514 return;
515
516 /* Send RNDIS RESPONSE_AVAILABLE notification; a
517 * USB_CDC_NOTIFY_RESPONSE_AVAILABLE "should" work too
518 *
519 * This is the only notification defined by RNDIS.
520 */
521 data[0] = cpu_to_le32(1);
522 data[1] = cpu_to_le32(0);
523
524 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
525 if (status) {
526 atomic_dec(&rndis->notify_count);
527 pr_info("notify/0 --> %d\n", status);
528 }
529}
530
531static void rndis_qc_response_complete(struct usb_ep *ep,
532 struct usb_request *req)
533{
Jack Pham0ad82e62012-09-27 17:31:08 -0700534 struct f_rndis_qc *rndis = req->context;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200535 int status = req->status;
Jack Pham0ad82e62012-09-27 17:31:08 -0700536 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200537
538 /* after TX:
539 * - USB_CDC_GET_ENCAPSULATED_RESPONSE (ep0/control)
540 * - RNDIS_RESPONSE_AVAILABLE (status/irq)
541 */
542 switch (status) {
543 case -ECONNRESET:
544 case -ESHUTDOWN:
545 /* connection gone */
546 atomic_set(&rndis->notify_count, 0);
547 break;
548 default:
549 pr_info("RNDIS %s response error %d, %d/%d\n",
550 ep->name, status,
551 req->actual, req->length);
552 /* FALLTHROUGH */
553 case 0:
554 if (ep != rndis->notify)
555 break;
556
557 /* handle multiple pending RNDIS_RESPONSE_AVAILABLE
558 * notifications by resending until we're done
559 */
560 if (atomic_dec_and_test(&rndis->notify_count))
561 break;
562 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
563 if (status) {
564 atomic_dec(&rndis->notify_count);
565 DBG(cdev, "notify/1 --> %d\n", status);
566 }
567 break;
568 }
569}
570
571static void rndis_qc_command_complete(struct usb_ep *ep,
572 struct usb_request *req)
573{
Anna Perelce47ed42012-12-05 14:31:07 +0200574 struct f_rndis_qc *rndis = req->context;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200575 int status;
Anna Perelce47ed42012-12-05 14:31:07 +0200576 rndis_init_msg_type *buf;
Ofir Cohenaef90b72012-07-31 12:37:04 +0200577
578 /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
579 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
580 if (status < 0)
581 pr_err("RNDIS command error %d, %d/%d\n",
582 status, req->actual, req->length);
Anna Perelce47ed42012-12-05 14:31:07 +0200583
584 buf = (rndis_init_msg_type *)req->buf;
585
586 if (buf->MessageType == REMOTE_NDIS_INITIALIZE_MSG) {
587 rndis->max_pkt_size = buf->MaxTransferSize;
588 pr_debug("MaxTransferSize: %d\n", buf->MaxTransferSize);
589 }
Ofir Cohenaef90b72012-07-31 12:37:04 +0200590}
591
592static int
593rndis_qc_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
594{
595 struct f_rndis_qc *rndis = func_to_rndis_qc(f);
596 struct usb_composite_dev *cdev = f->config->cdev;
597 struct usb_request *req = cdev->req;
598 int value = -EOPNOTSUPP;
599 u16 w_index = le16_to_cpu(ctrl->wIndex);
600 u16 w_value = le16_to_cpu(ctrl->wValue);
601 u16 w_length = le16_to_cpu(ctrl->wLength);
602
603 /* composite driver infrastructure handles everything except
604 * CDC class messages; interface activation uses set_alt().
605 */
606 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
607
608 /* RNDIS uses the CDC command encapsulation mechanism to implement
609 * an RPC scheme, with much getting/setting of attributes by OID.
610 */
611 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
612 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
613 if (w_value || w_index != rndis->ctrl_id)
614 goto invalid;
615 /* read the request; process it later */
616 value = w_length;
617 req->complete = rndis_qc_command_complete;
618 req->context = rndis;
619 /* later, rndis_response_available() sends a notification */
620 break;
621
622 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
623 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
624 if (w_value || w_index != rndis->ctrl_id)
625 goto invalid;
626 else {
627 u8 *buf;
628 u32 n;
629
630 /* return the result */
631 buf = rndis_get_next_response(rndis->config, &n);
632 if (buf) {
633 memcpy(req->buf, buf, n);
634 req->complete = rndis_qc_response_complete;
635 rndis_free_response(rndis->config, buf);
636 value = n;
637 }
638 /* else stalls ... spec says to avoid that */
639 }
640 break;
641
642 default:
643invalid:
644 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
645 ctrl->bRequestType, ctrl->bRequest,
646 w_value, w_index, w_length);
647 }
648
649 /* respond with data transfer or status phase? */
650 if (value >= 0) {
651 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
652 ctrl->bRequestType, ctrl->bRequest,
653 w_value, w_index, w_length);
654 req->zero = (value < w_length);
655 req->length = value;
656 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
657 if (value < 0)
658 pr_err("rndis response on err %d\n", value);
659 }
660
661 /* device either stalls (value < 0) or reports success */
662 return value;
663}
664
665
666static int rndis_qc_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
667{
668 struct f_rndis_qc *rndis = func_to_rndis_qc(f);
669 struct usb_composite_dev *cdev = f->config->cdev;
670
671 /* we know alt == 0 */
672
673 if (intf == rndis->ctrl_id) {
674 if (rndis->notify->driver_data) {
675 VDBG(cdev, "reset rndis control %d\n", intf);
676 usb_ep_disable(rndis->notify);
677 }
678 if (!rndis->notify->desc) {
679 VDBG(cdev, "init rndis ctrl %d\n", intf);
680 if (config_ep_by_speed(cdev->gadget, f, rndis->notify))
681 goto fail;
682 }
683 usb_ep_enable(rndis->notify);
684 rndis->notify->driver_data = rndis;
685
686 } else if (intf == rndis->data_id) {
687 struct net_device *net;
688
689 if (rndis->port.in_ep->driver_data) {
690 DBG(cdev, "reset rndis\n");
Amit Blay51bebe92012-12-25 18:48:10 +0200691 /* rndis->port is needed for disconnecting the BAM data
692 * path. Only after the BAM data path is disconnected,
693 * we can disconnect the port from the network layer.
694 */
Ofir Cohenaef90b72012-07-31 12:37:04 +0200695 rndis_qc_bam_disconnect(rndis);
Amit Blay51bebe92012-12-25 18:48:10 +0200696 gether_qc_disconnect_name(&rndis->port, "rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +0200697 }
698
699 if (!rndis->port.in_ep->desc || !rndis->port.out_ep->desc) {
700 DBG(cdev, "init rndis\n");
701 if (config_ep_by_speed(cdev->gadget, f,
702 rndis->port.in_ep) ||
703 config_ep_by_speed(cdev->gadget, f,
704 rndis->port.out_ep)) {
705 rndis->port.in_ep->desc = NULL;
706 rndis->port.out_ep->desc = NULL;
707 goto fail;
708 }
709 }
710
711 /* Avoid ZLPs; they can be troublesome. */
712 rndis->port.is_zlp_ok = false;
713
714 /* RNDIS should be in the "RNDIS uninitialized" state,
715 * either never activated or after rndis_uninit().
716 *
717 * We don't want data to flow here until a nonzero packet
718 * filter is set, at which point it enters "RNDIS data
719 * initialized" state ... but we do want the endpoints
720 * to be activated. It's a strange little state.
721 *
722 * REVISIT the RNDIS gadget code has done this wrong for a
723 * very long time. We need another call to the link layer
724 * code -- gether_updown(...bool) maybe -- to do it right.
725 */
726 rndis->port.cdc_filter = 0;
727
Bar Weiner189bb3c2013-06-09 14:24:56 +0300728 if (rndis_qc_bam_connect(rndis))
729 goto fail;
730
Ofir Cohenaef90b72012-07-31 12:37:04 +0200731 DBG(cdev, "RNDIS RX/TX early activation ...\n");
Vamsi Krishna20c00b52013-04-26 18:15:13 -0700732 net = gether_qc_connect_name(&rndis->port, "rndis0", false);
Ofir Cohenaef90b72012-07-31 12:37:04 +0200733 if (IS_ERR(net))
734 return PTR_ERR(net);
735
Ofir Cohenaef90b72012-07-31 12:37:04 +0200736 rndis_set_param_dev(rndis->config, net,
737 &rndis->port.cdc_filter);
738 } else
739 goto fail;
740
741 return 0;
742fail:
743 return -EINVAL;
744}
745
746static void rndis_qc_disable(struct usb_function *f)
747{
748 struct f_rndis_qc *rndis = func_to_rndis_qc(f);
749
750 if (!rndis->notify->driver_data)
751 return;
752
753 pr_info("rndis deactivated\n");
754
755 rndis_uninit(rndis->config);
Ofir Cohenaef90b72012-07-31 12:37:04 +0200756 rndis_qc_bam_disconnect(rndis);
Amit Blay51bebe92012-12-25 18:48:10 +0200757 gether_qc_disconnect_name(&rndis->port, "rndis0");
Ofir Cohenaef90b72012-07-31 12:37:04 +0200758
759 usb_ep_disable(rndis->notify);
760 rndis->notify->driver_data = NULL;
761}
762
Anna Perela6d730742012-12-17 09:43:16 +0200763static void rndis_qc_suspend(struct usb_function *f)
764{
765 pr_debug("%s: rndis suspended\n", __func__);
766
767 bam_data_suspend(RNDIS_QC_ACTIVE_PORT);
768}
769
770static void rndis_qc_resume(struct usb_function *f)
771{
772 pr_debug("%s: rndis resumed\n", __func__);
773
774 bam_data_resume(RNDIS_QC_ACTIVE_PORT);
775}
776
Ofir Cohenaef90b72012-07-31 12:37:04 +0200777/*-------------------------------------------------------------------------*/
778
779/*
780 * This isn't quite the same mechanism as CDC Ethernet, since the
781 * notification scheme passes less data, but the same set of link
782 * states must be tested. A key difference is that altsettings are
783 * not used to tell whether the link should send packets or not.
784 */
785
786static void rndis_qc_open(struct qc_gether *geth)
787{
788 struct f_rndis_qc *rndis = func_to_rndis_qc(&geth->func);
789 struct usb_composite_dev *cdev = geth->func.config->cdev;
790
791 DBG(cdev, "%s\n", __func__);
792
793 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
794 rndis_qc_bitrate(cdev->gadget) / 100);
795 rndis_signal_connect(rndis->config);
796}
797
798static void rndis_qc_close(struct qc_gether *geth)
799{
800 struct f_rndis_qc *rndis = func_to_rndis_qc(&geth->func);
801
802 DBG(geth->func.config->cdev, "%s\n", __func__);
803
804 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
805 rndis_signal_disconnect(rndis->config);
806}
807
808/*-------------------------------------------------------------------------*/
809
810/* ethernet function driver setup/binding */
811
812static int
813rndis_qc_bind(struct usb_configuration *c, struct usb_function *f)
814{
815 struct usb_composite_dev *cdev = c->cdev;
816 struct f_rndis_qc *rndis = func_to_rndis_qc(f);
817 int status;
818 struct usb_ep *ep;
819
820 /* allocate instance-specific interface IDs */
821 status = usb_interface_id(c, f);
822 if (status < 0)
823 goto fail;
824 rndis->ctrl_id = status;
825 rndis_qc_iad_descriptor.bFirstInterface = status;
826
827 rndis_qc_control_intf.bInterfaceNumber = status;
828 rndis_qc_union_desc.bMasterInterface0 = status;
829
830 status = usb_interface_id(c, f);
831 if (status < 0)
832 goto fail;
833 rndis->data_id = status;
834
835 rndis_qc_data_intf.bInterfaceNumber = status;
836 rndis_qc_union_desc.bSlaveInterface0 = status;
837
838 status = -ENODEV;
839
840 /* allocate instance-specific endpoints */
841 ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_in_desc);
842 if (!ep)
843 goto fail;
844 rndis->port.in_ep = ep;
845 ep->driver_data = cdev; /* claim */
846
847 ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_out_desc);
848 if (!ep)
849 goto fail;
850 rndis->port.out_ep = ep;
851 ep->driver_data = cdev; /* claim */
852
853 /* NOTE: a status/notification endpoint is, strictly speaking,
854 * optional. We don't treat it that way though! It's simpler,
855 * and some newer profiles don't treat it as optional.
856 */
857 ep = usb_ep_autoconfig(cdev->gadget, &rndis_qc_fs_notify_desc);
858 if (!ep)
859 goto fail;
860 rndis->notify = ep;
861 ep->driver_data = cdev; /* claim */
862
863 status = -ENOMEM;
864
865 /* allocate notification request and buffer */
866 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
867 if (!rndis->notify_req)
868 goto fail;
869 rndis->notify_req->buf = kmalloc(RNDIS_QC_STATUS_BYTECOUNT, GFP_KERNEL);
870 if (!rndis->notify_req->buf)
871 goto fail;
872 rndis->notify_req->length = RNDIS_QC_STATUS_BYTECOUNT;
873 rndis->notify_req->context = rndis;
874 rndis->notify_req->complete = rndis_qc_response_complete;
875
876 /* copy descriptors, and track endpoint copies */
877 f->descriptors = usb_copy_descriptors(eth_qc_fs_function);
878 if (!f->descriptors)
879 goto fail;
880
881 /* support all relevant hardware speeds... we expect that when
882 * hardware is dual speed, all bulk-capable endpoints work at
883 * both speeds
884 */
885 if (gadget_is_dualspeed(c->cdev->gadget)) {
886 rndis_qc_hs_in_desc.bEndpointAddress =
887 rndis_qc_fs_in_desc.bEndpointAddress;
888 rndis_qc_hs_out_desc.bEndpointAddress =
889 rndis_qc_fs_out_desc.bEndpointAddress;
890 rndis_qc_hs_notify_desc.bEndpointAddress =
891 rndis_qc_fs_notify_desc.bEndpointAddress;
892
893 /* copy descriptors, and track endpoint copies */
894 f->hs_descriptors = usb_copy_descriptors(eth_qc_hs_function);
895
896 if (!f->hs_descriptors)
897 goto fail;
898 }
899
900 if (gadget_is_superspeed(c->cdev->gadget)) {
901 rndis_qc_ss_in_desc.bEndpointAddress =
902 rndis_qc_fs_in_desc.bEndpointAddress;
903 rndis_qc_ss_out_desc.bEndpointAddress =
904 rndis_qc_fs_out_desc.bEndpointAddress;
905 rndis_qc_ss_notify_desc.bEndpointAddress =
906 rndis_qc_fs_notify_desc.bEndpointAddress;
907
908 /* copy descriptors, and track endpoint copies */
909 f->ss_descriptors = usb_copy_descriptors(eth_qc_ss_function);
910 if (!f->ss_descriptors)
911 goto fail;
912 }
913
914 rndis->port.open = rndis_qc_open;
915 rndis->port.close = rndis_qc_close;
916
917 status = rndis_register(rndis_qc_response_available, rndis);
918 if (status < 0)
919 goto fail;
920 rndis->config = status;
921
922 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
923 rndis_set_host_mac(rndis->config, rndis->ethaddr);
924
925 if (rndis_set_param_vendor(rndis->config, rndis->vendorID,
926 rndis->manufacturer))
927 goto fail;
928
929 rndis_set_max_pkt_xfer(rndis->config, rndis->max_pkt_per_xfer);
930
Ofir Cohen76624ed2012-09-09 10:27:58 +0300931 /* In case of aggregated packets QC device will request
932 * aliment to 4 (2^2).
933 */
934 rndis_set_pkt_alignment_factor(rndis->config, 2);
935
Ofir Cohenaef90b72012-07-31 12:37:04 +0200936 /* NOTE: all that is done without knowing or caring about
937 * the network link ... which is unavailable to this code
938 * until we're activated via set_alt().
939 */
940
941 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
942 gadget_is_superspeed(c->cdev->gadget) ? "super" :
943 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
944 rndis->port.in_ep->name, rndis->port.out_ep->name,
945 rndis->notify->name);
946 return 0;
947
948fail:
949 if (gadget_is_superspeed(c->cdev->gadget) && f->ss_descriptors)
950 usb_free_descriptors(f->ss_descriptors);
951 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
952 usb_free_descriptors(f->hs_descriptors);
953 if (f->descriptors)
954 usb_free_descriptors(f->descriptors);
955
956 if (rndis->notify_req) {
957 kfree(rndis->notify_req->buf);
958 usb_ep_free_request(rndis->notify, rndis->notify_req);
959 }
960
961 /* we might as well release our claims on endpoints */
962 if (rndis->notify)
963 rndis->notify->driver_data = NULL;
964 if (rndis->port.out_ep->desc)
965 rndis->port.out_ep->driver_data = NULL;
966 if (rndis->port.in_ep->desc)
967 rndis->port.in_ep->driver_data = NULL;
968
969 pr_err("%s: can't bind, err %d\n", f->name, status);
970
971 return status;
972}
973
974static void
975rndis_qc_unbind(struct usb_configuration *c, struct usb_function *f)
976{
977 struct f_rndis_qc *rndis = func_to_rndis_qc(f);
978
Bar Weiner189bb3c2013-06-09 14:24:56 +0300979 pr_debug("rndis_qc_unbind: free");
980 bam_data_destroy(0);
Ofir Cohenaef90b72012-07-31 12:37:04 +0200981 rndis_deregister(rndis->config);
982 rndis_exit();
983
984 if (gadget_is_dualspeed(c->cdev->gadget))
985 usb_free_descriptors(f->hs_descriptors);
986 usb_free_descriptors(f->descriptors);
987
988 kfree(rndis->notify_req->buf);
989 usb_ep_free_request(rndis->notify, rndis->notify_req);
990
991 kfree(rndis);
992}
993
994/* Some controllers can't support RNDIS ... */
995static inline bool can_support_rndis_qc(struct usb_configuration *c)
996{
997 /* everything else is *presumably* fine */
998 return true;
999}
1000
1001/**
1002 * rndis_qc_bind_config - add RNDIS network link to a configuration
1003 * @c: the configuration to support the network link
1004 * @ethaddr: a buffer in which the ethernet address of the host side
1005 * side of the link was recorded
1006 * Context: single threaded during gadget setup
1007 *
1008 * Returns zero on success, else negative errno.
1009 *
1010 * Caller must have called @gether_setup(). Caller is also responsible
1011 * for calling @gether_cleanup() before module unload.
1012 */
1013int
1014rndis_qc_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
1015{
1016 return rndis_qc_bind_config_vendor(c, ethaddr, 0, NULL, 1);
1017}
1018
1019int
1020rndis_qc_bind_config_vendor(struct usb_configuration *c, u8 ethaddr[ETH_ALEN],
1021 u32 vendorID, const char *manufacturer,
1022 u8 max_pkt_per_xfer)
1023{
1024 struct f_rndis_qc *rndis;
1025 int status;
1026
1027 if (!can_support_rndis_qc(c) || !ethaddr)
1028 return -EINVAL;
1029
1030 /* setup RNDIS itself */
1031 status = rndis_init();
1032 if (status < 0)
1033 return status;
1034
1035 status = rndis_qc_bam_setup();
1036 if (status) {
1037 pr_err("bam setup failed");
1038 return status;
1039 }
1040
1041 /* maybe allocate device-global string IDs */
1042 if (rndis_qc_string_defs[0].id == 0) {
1043
1044 /* control interface label */
1045 status = usb_string_id(c->cdev);
1046 if (status < 0)
1047 return status;
1048 rndis_qc_string_defs[0].id = status;
1049 rndis_qc_control_intf.iInterface = status;
1050
1051 /* data interface label */
1052 status = usb_string_id(c->cdev);
1053 if (status < 0)
1054 return status;
1055 rndis_qc_string_defs[1].id = status;
1056 rndis_qc_data_intf.iInterface = status;
1057
1058 /* IAD iFunction label */
1059 status = usb_string_id(c->cdev);
1060 if (status < 0)
1061 return status;
1062 rndis_qc_string_defs[2].id = status;
1063 rndis_qc_iad_descriptor.iFunction = status;
1064 }
1065
1066 /* allocate and initialize one new instance */
1067 status = -ENOMEM;
1068 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
1069 if (!rndis)
1070 goto fail;
1071
1072 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
1073 rndis->vendorID = vendorID;
1074 rndis->manufacturer = manufacturer;
1075
1076 /* if max_pkt_per_xfer was not configured set to default value */
1077 rndis->max_pkt_per_xfer =
1078 max_pkt_per_xfer ? max_pkt_per_xfer : DEFAULT_MAX_PKT_PER_XFER;
1079
1080 /* RNDIS activates when the host changes this filter */
1081 rndis->port.cdc_filter = 0;
1082
1083 /* RNDIS has special (and complex) framing */
1084 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
1085 rndis->port.wrap = rndis_qc_add_header;
1086 rndis->port.unwrap = rndis_qc_rm_hdr;
1087
1088 rndis->port.func.name = "rndis";
1089 rndis->port.func.strings = rndis_qc_strings;
1090 /* descriptors are per-instance copies */
1091 rndis->port.func.bind = rndis_qc_bind;
1092 rndis->port.func.unbind = rndis_qc_unbind;
1093 rndis->port.func.set_alt = rndis_qc_set_alt;
1094 rndis->port.func.setup = rndis_qc_setup;
1095 rndis->port.func.disable = rndis_qc_disable;
Anna Perela6d730742012-12-17 09:43:16 +02001096 rndis->port.func.suspend = rndis_qc_suspend;
1097 rndis->port.func.resume = rndis_qc_resume;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001098
1099 _rndis_qc = rndis;
1100
1101 status = usb_add_function(c, &rndis->port.func);
1102 if (status) {
1103 kfree(rndis);
1104fail:
1105 rndis_exit();
1106 }
1107 return status;
1108}
1109
1110static int rndis_qc_open_dev(struct inode *ip, struct file *fp)
1111{
1112 pr_info("Open rndis QC driver\n");
1113
1114 if (!_rndis_qc) {
1115 pr_err("rndis_qc_dev not created yet\n");
1116 return -ENODEV;
1117 }
1118
1119 if (rndis_qc_lock(&_rndis_qc->open_excl)) {
1120 pr_err("Already opened\n");
1121 return -EBUSY;
1122 }
1123
1124 fp->private_data = _rndis_qc;
1125 pr_info("rndis QC file opened\n");
1126
1127 return 0;
1128}
1129
1130static int rndis_qc_release_dev(struct inode *ip, struct file *fp)
1131{
1132 struct f_rndis_qc *rndis = fp->private_data;
1133
1134 pr_info("Close rndis QC file");
1135 rndis_qc_unlock(&rndis->open_excl);
1136
1137 return 0;
1138}
1139
1140static long rndis_qc_ioctl(struct file *fp, unsigned cmd, unsigned long arg)
1141{
1142 struct f_rndis_qc *rndis = fp->private_data;
1143 int ret = 0;
1144
1145 pr_info("Received command %d", cmd);
1146
1147 if (rndis_qc_lock(&rndis->ioctl_excl))
1148 return -EBUSY;
1149
1150 switch (cmd) {
1151 case RNDIS_QC_GET_MAX_PKT_PER_XFER:
1152 ret = copy_to_user((void __user *)arg,
1153 &rndis->max_pkt_per_xfer,
1154 sizeof(rndis->max_pkt_per_xfer));
1155 if (ret) {
1156 pr_err("copying to user space failed");
1157 ret = -EFAULT;
1158 }
1159 pr_info("Sent max packets per xfer %d",
1160 rndis->max_pkt_per_xfer);
1161 break;
Anna Perelce47ed42012-12-05 14:31:07 +02001162 case RNDIS_QC_GET_MAX_PKT_SIZE:
1163 ret = copy_to_user((void __user *)arg,
1164 &rndis->max_pkt_size,
1165 sizeof(rndis->max_pkt_size));
1166 if (ret) {
1167 pr_err("copying to user space failed");
1168 ret = -EFAULT;
1169 }
1170 pr_debug("Sent max packet size %d",
1171 rndis->max_pkt_size);
1172 break;
Ofir Cohenaef90b72012-07-31 12:37:04 +02001173 default:
1174 pr_err("Unsupported IOCTL");
1175 ret = -EINVAL;
1176 }
1177
1178 rndis_qc_unlock(&rndis->ioctl_excl);
1179
1180 return ret;
1181}
1182
1183static const struct file_operations rndis_qc_fops = {
1184 .owner = THIS_MODULE,
1185 .open = rndis_qc_open_dev,
1186 .release = rndis_qc_release_dev,
1187 .unlocked_ioctl = rndis_qc_ioctl,
1188};
1189
1190static struct miscdevice rndis_qc_device = {
1191 .minor = MISC_DYNAMIC_MINOR,
1192 .name = "android_rndis_qc",
1193 .fops = &rndis_qc_fops,
1194};
1195
1196static int rndis_qc_init(void)
1197{
1198 int ret;
1199
1200 pr_info("initialize rndis QC instance\n");
1201
1202 ret = misc_register(&rndis_qc_device);
1203 if (ret)
1204 pr_err("rndis QC driver failed to register");
1205
1206 return ret;
1207}
1208
1209static void rndis_qc_cleanup(void)
1210{
1211 pr_info("rndis QC cleanup");
1212
1213 misc_deregister(&rndis_qc_device);
1214 _rndis_qc = NULL;
1215}
1216
1217