4 * Authors: Benedikt Spranger, Pengutronix
5 * Robert Schwebel, Pengutronix
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2, as published by the Free Software Foundation.
11 * This software was originally developed in conformance with
12 * Microsoft's Remote NDIS Specification License Agreement.
14 * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
15 * Fixed message length bug in init_response
17 * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
18 * Fixed rndis_rm_hdr length bug.
20 * Copyright (C) 2004 by David Brownell
21 * updates to merge with Linux 2.6, better match RNDIS spec
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/idr.h>
29 #include <linux/list.h>
30 #include <linux/proc_fs.h>
31 #include <linux/slab.h>
32 #include <linux/seq_file.h>
33 #include <linux/netdevice.h>
36 #include <asm/byteorder.h>
37 #include <asm/unaligned.h>
45 int rndis_ul_max_pkt_per_xfer_rcvd;
46 module_param(rndis_ul_max_pkt_per_xfer_rcvd, int, S_IRUGO);
47 MODULE_PARM_DESC(rndis_ul_max_pkt_per_xfer_rcvd,
48 "Max num of REMOTE_NDIS_PACKET_MSGs received in a single transfer");
50 int rndis_ul_max_xfer_size_rcvd;
51 module_param(rndis_ul_max_xfer_size_rcvd, int, S_IRUGO);
52 MODULE_PARM_DESC(rndis_ul_max_xfer_size_rcvd,
53 "Max size of bus transfer received");
56 /* The driver for your USB chip needs to support ep0 OUT to work with
57 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
59 * Windows hosts need an INF file like Documentation/usb/linux.inf
60 * and will be happier if you provide the host_addr module parameter.
64 static int rndis_debug = 0;
65 module_param (rndis_debug, int, 0);
66 MODULE_PARM_DESC (rndis_debug, "enable debugging");
71 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
73 #define NAME_TEMPLATE "driver/rndis-%03d"
75 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
77 static DEFINE_IDA(rndis_ida);
80 static const __le32 rndis_driver_version = cpu_to_le32(1);
82 /* Function Prototypes */
83 static rndis_resp_t *rndis_add_response(struct rndis_params *params,
86 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
88 static const struct file_operations rndis_proc_fops;
90 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
93 static const u32 oid_supported_list[] =
95 /* the general stuff */
96 RNDIS_OID_GEN_SUPPORTED_LIST,
97 RNDIS_OID_GEN_HARDWARE_STATUS,
98 RNDIS_OID_GEN_MEDIA_SUPPORTED,
99 RNDIS_OID_GEN_MEDIA_IN_USE,
100 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
101 RNDIS_OID_GEN_LINK_SPEED,
102 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
103 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
104 RNDIS_OID_GEN_VENDOR_ID,
105 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
106 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
107 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
108 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
109 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
110 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
112 /* the statistical stuff */
113 RNDIS_OID_GEN_XMIT_OK,
114 RNDIS_OID_GEN_RCV_OK,
115 RNDIS_OID_GEN_XMIT_ERROR,
116 RNDIS_OID_GEN_RCV_ERROR,
117 RNDIS_OID_GEN_RCV_NO_BUFFER,
118 #ifdef RNDIS_OPTIONAL_STATS
119 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
120 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
121 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
122 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
123 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
124 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
125 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
126 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
127 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
128 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
129 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
130 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
131 RNDIS_OID_GEN_RCV_CRC_ERROR,
132 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
133 #endif /* RNDIS_OPTIONAL_STATS */
135 /* mandatory 802.3 */
136 /* the general stuff */
137 RNDIS_OID_802_3_PERMANENT_ADDRESS,
138 RNDIS_OID_802_3_CURRENT_ADDRESS,
139 RNDIS_OID_802_3_MULTICAST_LIST,
140 RNDIS_OID_802_3_MAC_OPTIONS,
141 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
143 /* the statistical stuff */
144 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
145 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
146 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
147 #ifdef RNDIS_OPTIONAL_STATS
148 RNDIS_OID_802_3_XMIT_DEFERRED,
149 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
150 RNDIS_OID_802_3_RCV_OVERRUN,
151 RNDIS_OID_802_3_XMIT_UNDERRUN,
152 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
153 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
154 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
155 #endif /* RNDIS_OPTIONAL_STATS */
158 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
159 * don't say what they mean ... and the NDIS specs are often
160 * confusing and/or ambiguous in this context. (That is, more
161 * so than their specs for the other OIDs.)
163 * FIXME someone who knows what these should do, please
167 /* power management */
168 OID_PNP_CAPABILITIES,
174 OID_PNP_ENABLE_WAKE_UP,
175 OID_PNP_ADD_WAKE_UP_PATTERN,
176 OID_PNP_REMOVE_WAKE_UP_PATTERN,
177 #endif /* RNDIS_WAKEUP */
178 #endif /* RNDIS_PM */
183 static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
184 unsigned buf_len, rndis_resp_t *r)
186 int retval = -ENOTSUPP;
187 u32 length = 4; /* usually */
190 rndis_query_cmplt_type *resp;
191 struct net_device *net;
192 struct rtnl_link_stats64 temp;
193 const struct rtnl_link_stats64 *stats;
195 if (!r) return -ENOMEM;
196 resp = (rndis_query_cmplt_type *)r->buf;
198 if (!resp) return -ENOMEM;
200 if (buf_len && rndis_debug > 1) {
201 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
202 for (i = 0; i < buf_len; i += 16) {
203 pr_debug("%03d: %08x %08x %08x %08x\n", i,
204 get_unaligned_le32(&buf[i]),
205 get_unaligned_le32(&buf[i + 4]),
206 get_unaligned_le32(&buf[i + 8]),
207 get_unaligned_le32(&buf[i + 12]));
211 /* response goes here, right after the header */
212 outbuf = (__le32 *)&resp[1];
213 resp->InformationBufferOffset = cpu_to_le32(16);
216 stats = dev_get_stats(net, &temp);
220 /* general oids (table 4-1) */
223 case RNDIS_OID_GEN_SUPPORTED_LIST:
224 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
225 length = sizeof(oid_supported_list);
226 count = length / sizeof(u32);
227 for (i = 0; i < count; i++)
228 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
233 case RNDIS_OID_GEN_HARDWARE_STATUS:
234 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
236 * Hardware must be ready to receive high level protocols.
238 * reddite ergo quae sunt Caesaris Caesari
239 * et quae sunt Dei Deo!
241 *outbuf = cpu_to_le32(0);
246 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
247 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
248 *outbuf = cpu_to_le32(params->medium);
253 case RNDIS_OID_GEN_MEDIA_IN_USE:
254 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
255 /* one medium, one transport... (maybe you do it better) */
256 *outbuf = cpu_to_le32(params->medium);
261 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
262 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
264 *outbuf = cpu_to_le32(params->dev->mtu);
270 case RNDIS_OID_GEN_LINK_SPEED:
272 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
273 if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
274 *outbuf = cpu_to_le32(0);
276 *outbuf = cpu_to_le32(params->speed);
281 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
282 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
284 *outbuf = cpu_to_le32(params->dev->mtu);
290 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
291 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
293 *outbuf = cpu_to_le32(params->dev->mtu);
299 case RNDIS_OID_GEN_VENDOR_ID:
300 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
301 *outbuf = cpu_to_le32(params->vendorID);
306 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
307 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
308 if (params->vendorDescr) {
309 length = strlen(params->vendorDescr);
310 memcpy(outbuf, params->vendorDescr, length);
317 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
318 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
320 *outbuf = rndis_driver_version;
325 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
326 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
327 *outbuf = cpu_to_le32(*params->filter);
332 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
333 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
334 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
339 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
341 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
342 *outbuf = cpu_to_le32(params->media_state);
346 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
347 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
348 *outbuf = cpu_to_le32(0);
352 /* The RNDIS specification is incomplete/wrong. Some versions
353 * of MS-Windows expect OIDs that aren't specified there. Other
354 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
356 case RNDIS_OID_GEN_MAC_OPTIONS: /* from WinME */
357 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
358 *outbuf = cpu_to_le32(
359 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
360 | RNDIS_MAC_OPTION_FULL_DUPLEX);
364 /* statistics OIDs (table 4-2) */
367 case RNDIS_OID_GEN_XMIT_OK:
369 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
371 *outbuf = cpu_to_le32(stats->tx_packets
372 - stats->tx_errors - stats->tx_dropped);
378 case RNDIS_OID_GEN_RCV_OK:
380 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
382 *outbuf = cpu_to_le32(stats->rx_packets
383 - stats->rx_errors - stats->rx_dropped);
389 case RNDIS_OID_GEN_XMIT_ERROR:
391 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
393 *outbuf = cpu_to_le32(stats->tx_errors);
399 case RNDIS_OID_GEN_RCV_ERROR:
401 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
403 *outbuf = cpu_to_le32(stats->rx_errors);
409 case RNDIS_OID_GEN_RCV_NO_BUFFER:
410 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
412 *outbuf = cpu_to_le32(stats->rx_dropped);
417 /* ieee802.3 OIDs (table 4-3) */
420 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
421 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
424 memcpy(outbuf, params->host_mac, length);
430 case RNDIS_OID_802_3_CURRENT_ADDRESS:
431 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
434 memcpy(outbuf, params->host_mac, length);
440 case RNDIS_OID_802_3_MULTICAST_LIST:
441 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
442 /* Multicast base address only */
443 *outbuf = cpu_to_le32(0xE0000000);
448 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
449 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
450 /* Multicast base address only */
451 *outbuf = cpu_to_le32(1);
455 case RNDIS_OID_802_3_MAC_OPTIONS:
456 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
457 *outbuf = cpu_to_le32(0);
461 /* ieee802.3 statistics OIDs (table 4-4) */
464 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
465 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
467 *outbuf = cpu_to_le32(stats->rx_frame_errors);
473 case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
474 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
475 *outbuf = cpu_to_le32(0);
480 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
481 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
482 *outbuf = cpu_to_le32(0);
487 pr_warning("%s: query unknown OID 0x%08X\n",
493 resp->InformationBufferLength = cpu_to_le32(length);
494 r->length = length + sizeof(*resp);
495 resp->MessageLength = cpu_to_le32(r->length);
499 static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
500 u8 *buf, u32 buf_len, rndis_resp_t *r)
502 rndis_set_cmplt_type *resp;
503 int i, retval = -ENOTSUPP;
507 resp = (rndis_set_cmplt_type *)r->buf;
511 if (buf_len && rndis_debug > 1) {
512 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
513 for (i = 0; i < buf_len; i += 16) {
514 pr_debug("%03d: %08x %08x %08x %08x\n", i,
515 get_unaligned_le32(&buf[i]),
516 get_unaligned_le32(&buf[i + 4]),
517 get_unaligned_le32(&buf[i + 8]),
518 get_unaligned_le32(&buf[i + 12]));
523 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
525 /* these NDIS_PACKET_TYPE_* bitflags are shared with
526 * cdc_filter; it's not RNDIS-specific
527 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
528 * PROMISCUOUS, DIRECTED,
529 * MULTICAST, ALL_MULTICAST, BROADCAST
531 *params->filter = (u16)get_unaligned_le32(buf);
532 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
533 __func__, *params->filter);
535 /* this call has a significant side effect: it's
536 * what makes the packet flow start and stop, like
537 * activating the CDC Ethernet altsetting.
540 if (*params->filter) {
541 params->state = RNDIS_DATA_INITIALIZED;
542 netif_carrier_on(params->dev);
543 if (netif_running(params->dev))
544 netif_wake_queue(params->dev);
546 params->state = RNDIS_INITIALIZED;
547 netif_carrier_off(params->dev);
548 netif_stop_queue(params->dev);
552 case RNDIS_OID_802_3_MULTICAST_LIST:
553 /* I think we can ignore this */
554 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
559 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
560 __func__, OID, buf_len);
570 static int rndis_init_response(struct rndis_params *params,
571 rndis_init_msg_type *buf)
573 rndis_init_cmplt_type *resp;
579 r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
582 resp = (rndis_init_cmplt_type *)r->buf;
584 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
585 resp->MessageLength = cpu_to_le32(52);
586 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
587 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
588 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
589 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
590 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
591 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
592 resp->MaxPacketsPerTransfer = cpu_to_le32(params->max_pkt_per_xfer);
593 resp->MaxTransferSize = cpu_to_le32(params->max_pkt_per_xfer *
595 + sizeof(struct ethhdr)
596 + sizeof(struct rndis_packet_msg_type)
598 resp->PacketAlignmentFactor = cpu_to_le32(0);
599 resp->AFListOffset = cpu_to_le32(0);
600 resp->AFListSize = cpu_to_le32(0);
602 params->resp_avail(params->v);
606 static int rndis_query_response(struct rndis_params *params,
607 rndis_query_msg_type *buf)
609 rndis_query_cmplt_type *resp;
612 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
617 * we need more memory:
618 * gen_ndis_query_resp expects enough space for
619 * rndis_query_cmplt_type followed by data.
620 * oid_supported_list is the largest data reply
622 r = rndis_add_response(params,
623 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
626 resp = (rndis_query_cmplt_type *)r->buf;
628 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
629 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
631 if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
632 le32_to_cpu(buf->InformationBufferOffset)
634 le32_to_cpu(buf->InformationBufferLength),
636 /* OID not supported */
637 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
638 resp->MessageLength = cpu_to_le32(sizeof *resp);
639 resp->InformationBufferLength = cpu_to_le32(0);
640 resp->InformationBufferOffset = cpu_to_le32(0);
642 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
644 params->resp_avail(params->v);
648 static int rndis_set_response(struct rndis_params *params,
649 rndis_set_msg_type *buf)
651 u32 BufLength, BufOffset;
652 rndis_set_cmplt_type *resp;
655 r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
658 resp = (rndis_set_cmplt_type *)r->buf;
660 BufLength = le32_to_cpu(buf->InformationBufferLength);
661 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
664 pr_debug("%s: Length: %d\n", __func__, BufLength);
665 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
666 pr_debug("%s: InfoBuffer: ", __func__);
668 for (i = 0; i < BufLength; i++) {
669 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
675 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
676 resp->MessageLength = cpu_to_le32(16);
677 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
678 if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
679 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
680 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
682 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
684 params->resp_avail(params->v);
688 static int rndis_reset_response(struct rndis_params *params,
689 rndis_reset_msg_type *buf)
691 rndis_reset_cmplt_type *resp;
697 /* drain the response queue */
698 while ((xbuf = rndis_get_next_response(params, &length)))
699 rndis_free_response(params, xbuf);
701 r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
704 resp = (rndis_reset_cmplt_type *)r->buf;
706 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
707 resp->MessageLength = cpu_to_le32(16);
708 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
709 /* resent information */
710 resp->AddressingReset = cpu_to_le32(1);
712 params->resp_avail(params->v);
716 static int rndis_keepalive_response(struct rndis_params *params,
717 rndis_keepalive_msg_type *buf)
719 rndis_keepalive_cmplt_type *resp;
722 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
724 r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
727 resp = (rndis_keepalive_cmplt_type *)r->buf;
729 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
730 resp->MessageLength = cpu_to_le32(16);
731 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
732 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
734 params->resp_avail(params->v);
740 * Device to Host Comunication
742 static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
744 rndis_indicate_status_msg_type *resp;
747 if (params->state == RNDIS_UNINITIALIZED)
750 r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
753 resp = (rndis_indicate_status_msg_type *)r->buf;
755 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
756 resp->MessageLength = cpu_to_le32(20);
757 resp->Status = cpu_to_le32(status);
758 resp->StatusBufferLength = cpu_to_le32(0);
759 resp->StatusBufferOffset = cpu_to_le32(0);
761 params->resp_avail(params->v);
765 int rndis_signal_connect(struct rndis_params *params)
767 params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
768 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
770 EXPORT_SYMBOL_GPL(rndis_signal_connect);
772 int rndis_signal_disconnect(struct rndis_params *params)
774 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
775 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
777 EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
779 void rndis_uninit(struct rndis_params *params)
786 params->state = RNDIS_UNINITIALIZED;
788 /* drain the response queue */
789 while ((buf = rndis_get_next_response(params, &length)))
790 rndis_free_response(params, buf);
792 EXPORT_SYMBOL_GPL(rndis_uninit);
794 void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
796 params->host_mac = addr;
798 EXPORT_SYMBOL_GPL(rndis_set_host_mac);
803 int rndis_msg_parser(struct rndis_params *params, u8 *buf)
805 u32 MsgType, MsgLength;
812 MsgType = get_unaligned_le32(tmp++);
813 MsgLength = get_unaligned_le32(tmp++);
818 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
819 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
820 * and normal HC level polling to see if there's any IN traffic.
823 /* For USB: responses may take up to 10 seconds */
826 pr_debug("%s: RNDIS_MSG_INIT\n",
828 params->state = RNDIS_INITIALIZED;
829 return rndis_init_response(params, (rndis_init_msg_type *)buf);
832 pr_debug("%s: RNDIS_MSG_HALT\n",
834 params->state = RNDIS_UNINITIALIZED;
836 netif_carrier_off(params->dev);
837 netif_stop_queue(params->dev);
841 case RNDIS_MSG_QUERY:
842 return rndis_query_response(params,
843 (rndis_query_msg_type *)buf);
846 return rndis_set_response(params, (rndis_set_msg_type *)buf);
848 case RNDIS_MSG_RESET:
849 pr_debug("%s: RNDIS_MSG_RESET\n",
851 return rndis_reset_response(params,
852 (rndis_reset_msg_type *)buf);
854 case RNDIS_MSG_KEEPALIVE:
855 /* For USB: host does this every 5 seconds */
857 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
859 return rndis_keepalive_response(params,
860 (rndis_keepalive_msg_type *)
864 /* At least Windows XP emits some undefined RNDIS messages.
865 * In one case those messages seemed to relate to the host
868 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
869 __func__, MsgType, MsgLength);
870 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
877 EXPORT_SYMBOL_GPL(rndis_msg_parser);
879 static inline int rndis_get_nr(void)
881 return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL);
884 static inline void rndis_put_nr(int nr)
886 ida_simple_remove(&rndis_ida, nr);
889 struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
891 struct rndis_params *params;
895 return ERR_PTR(-EINVAL);
899 pr_debug("failed\n");
901 return ERR_PTR(-ENODEV);
904 params = kzalloc(sizeof(*params), GFP_KERNEL);
908 return ERR_PTR(-ENOMEM);
911 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
913 struct proc_dir_entry *proc_entry;
916 sprintf(name, NAME_TEMPLATE, i);
917 proc_entry = proc_create_data(name, 0660, NULL,
918 &rndis_proc_fops, params);
923 return ERR_PTR(-EIO);
928 params->confignr = i;
930 params->state = RNDIS_UNINITIALIZED;
931 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
932 params->resp_avail = resp_avail;
934 INIT_LIST_HEAD(&(params->resp_queue));
935 pr_debug("%s: configNr = %d\n", __func__, i);
939 EXPORT_SYMBOL_GPL(rndis_register);
941 void rndis_deregister(struct rndis_params *params)
945 pr_debug("%s:\n", __func__);
950 i = params->confignr;
952 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
956 sprintf(name, NAME_TEMPLATE, i);
957 remove_proc_entry(name, NULL);
964 EXPORT_SYMBOL_GPL(rndis_deregister);
965 int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
968 pr_debug("%s:\n", __func__);
975 params->filter = cdc_filter;
977 rndis_ul_max_xfer_size_rcvd = 0;
978 rndis_ul_max_pkt_per_xfer_rcvd = 0;
981 EXPORT_SYMBOL_GPL(rndis_set_param_dev);
983 int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
984 const char *vendorDescr)
986 pr_debug("%s:\n", __func__);
987 if (!vendorDescr) return -1;
991 params->vendorID = vendorID;
992 params->vendorDescr = vendorDescr;
996 EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
998 int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
1000 pr_debug("%s: %u %u\n", __func__, medium, speed);
1004 params->medium = medium;
1005 params->speed = speed;
1009 EXPORT_SYMBOL_GPL(rndis_set_param_medium);
1011 void rndis_set_max_pkt_xfer(struct rndis_params *params, u8 max_pkt_per_xfer)
1013 pr_debug("%s:\n", __func__);
1015 params->max_pkt_per_xfer = max_pkt_per_xfer;
1018 void rndis_add_hdr(struct sk_buff *skb)
1020 struct rndis_packet_msg_type *header;
1024 header = (void *)skb_push(skb, sizeof(*header));
1025 memset(header, 0, sizeof *header);
1026 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
1027 header->MessageLength = cpu_to_le32(skb->len);
1028 header->DataOffset = cpu_to_le32(36);
1029 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
1031 EXPORT_SYMBOL_GPL(rndis_add_hdr);
1033 void rndis_free_response(struct rndis_params *params, u8 *buf)
1036 struct list_head *act, *tmp;
1038 list_for_each_safe(act, tmp, &(params->resp_queue))
1040 r = list_entry(act, rndis_resp_t, list);
1041 if (r && r->buf == buf) {
1047 EXPORT_SYMBOL_GPL(rndis_free_response);
1049 u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
1052 struct list_head *act, *tmp;
1054 if (!length) return NULL;
1056 list_for_each_safe(act, tmp, &(params->resp_queue))
1058 r = list_entry(act, rndis_resp_t, list);
1061 *length = r->length;
1068 EXPORT_SYMBOL_GPL(rndis_get_next_response);
1070 static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
1074 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1075 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
1076 if (!r) return NULL;
1078 r->buf = (u8 *)(r + 1);
1082 list_add_tail(&r->list, &(params->resp_queue));
1086 int rndis_rm_hdr(struct gether *port,
1087 struct sk_buff *skb,
1088 struct sk_buff_head *list)
1092 if (skb->len > rndis_ul_max_xfer_size_rcvd)
1093 rndis_ul_max_xfer_size_rcvd = skb->len;
1096 struct rndis_packet_msg_type *hdr;
1097 struct sk_buff *skb2;
1098 u32 msg_len, data_offset, data_len;
1100 /* some rndis hosts send extra byte to avoid zlp, ignore it */
1101 if (skb->len == 1) {
1102 dev_kfree_skb_any(skb);
1106 if (skb->len < sizeof *hdr) {
1107 pr_err("invalid rndis pkt: skblen:%u hdr_len:%zu",
1108 skb->len, sizeof *hdr);
1109 dev_kfree_skb_any(skb);
1113 hdr = (void *)skb->data;
1114 msg_len = le32_to_cpu(hdr->MessageLength);
1115 data_offset = le32_to_cpu(hdr->DataOffset);
1116 data_len = le32_to_cpu(hdr->DataLength);
1118 if (skb->len < msg_len ||
1119 ((data_offset + data_len + 8) > msg_len)) {
1120 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1121 le32_to_cpu(hdr->MessageType),
1122 msg_len, data_offset, data_len, skb->len);
1123 dev_kfree_skb_any(skb);
1126 if (le32_to_cpu(hdr->MessageType) != RNDIS_MSG_PACKET) {
1127 pr_err("invalid rndis message: %d/%d/%d/%d, len:%d\n",
1128 le32_to_cpu(hdr->MessageType),
1129 msg_len, data_offset, data_len, skb->len);
1130 dev_kfree_skb_any(skb);
1134 skb_pull(skb, data_offset + 8);
1136 if (msg_len == skb->len) {
1137 skb_trim(skb, data_len);
1141 skb2 = skb_clone(skb, GFP_ATOMIC);
1143 pr_err("%s:skb clone failed\n", __func__);
1144 dev_kfree_skb_any(skb);
1148 skb_pull(skb, msg_len - sizeof *hdr);
1149 skb_trim(skb2, data_len);
1150 skb_queue_tail(list, skb2);
1155 if (num_pkts > rndis_ul_max_pkt_per_xfer_rcvd)
1156 rndis_ul_max_pkt_per_xfer_rcvd = num_pkts;
1158 skb_queue_tail(list, skb);
1161 EXPORT_SYMBOL_GPL(rndis_rm_hdr);
1163 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1165 static int rndis_proc_show(struct seq_file *m, void *v)
1167 rndis_params *param = m->private;
1176 "vendor ID : 0x%08X\n"
1178 param->confignr, (param->used) ? "y" : "n",
1180 switch (param->state) {
1181 case RNDIS_UNINITIALIZED:
1182 s = "RNDIS_UNINITIALIZED"; break;
1183 case RNDIS_INITIALIZED:
1184 s = "RNDIS_INITIALIZED"; break;
1185 case RNDIS_DATA_INITIALIZED:
1186 s = "RNDIS_DATA_INITIALIZED"; break;
1189 (param->media_state) ? 0 : param->speed*100,
1190 (param->media_state) ? "disconnected" : "connected",
1191 param->vendorID, param->vendorDescr);
1195 static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1196 size_t count, loff_t *ppos)
1198 rndis_params *p = PDE_DATA(file_inode(file));
1200 int i, fl_speed = 0;
1202 for (i = 0; i < count; i++) {
1204 if (get_user(c, buffer))
1218 speed = speed * 10 + c - '0';
1222 rndis_signal_connect(p);
1226 rndis_signal_disconnect(p);
1229 if (fl_speed) p->speed = speed;
1230 else pr_debug("%c is not valid\n", c);
1240 static int rndis_proc_open(struct inode *inode, struct file *file)
1242 return single_open(file, rndis_proc_show, PDE_DATA(inode));
1245 static const struct file_operations rndis_proc_fops = {
1246 .owner = THIS_MODULE,
1247 .open = rndis_proc_open,
1249 .llseek = seq_lseek,
1250 .release = single_release,
1251 .write = rndis_proc_write,
1254 #define NAME_TEMPLATE "driver/rndis-%03d"
1256 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */