Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / usb / gadget / rndis.c
blobd3cdffea9c8a33e71dd8fb5bacaf41c7aaf240cd
1 /*
2 * RNDIS MSG parser
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/init.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>
35 #include <asm/io.h>
36 #include <asm/byteorder.h>
37 #include <asm/system.h>
38 #include <asm/unaligned.h>
41 #undef VERBOSE_DEBUG
43 #include "rndis.h"
46 /* The driver for your USB chip needs to support ep0 OUT to work with
47 * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
49 * Windows hosts need an INF file like Documentation/usb/linux.inf
50 * and will be happier if you provide the host_addr module parameter.
53 #if 0
54 static int rndis_debug = 0;
55 module_param (rndis_debug, int, 0);
56 MODULE_PARM_DESC (rndis_debug, "enable debugging");
57 #else
58 #define rndis_debug 0
59 #endif
61 #define RNDIS_MAX_CONFIGS 1
64 static rndis_params rndis_per_dev_params[RNDIS_MAX_CONFIGS];
66 /* Driver Version */
67 static const __le32 rndis_driver_version = cpu_to_le32(1);
69 /* Function Prototypes */
70 static rndis_resp_t *rndis_add_response(int configNr, u32 length);
73 /* supported OIDs */
74 static const u32 oid_supported_list[] =
76 /* the general stuff */
77 OID_GEN_SUPPORTED_LIST,
78 OID_GEN_HARDWARE_STATUS,
79 OID_GEN_MEDIA_SUPPORTED,
80 OID_GEN_MEDIA_IN_USE,
81 OID_GEN_MAXIMUM_FRAME_SIZE,
82 OID_GEN_LINK_SPEED,
83 OID_GEN_TRANSMIT_BLOCK_SIZE,
84 OID_GEN_RECEIVE_BLOCK_SIZE,
85 OID_GEN_VENDOR_ID,
86 OID_GEN_VENDOR_DESCRIPTION,
87 OID_GEN_VENDOR_DRIVER_VERSION,
88 OID_GEN_CURRENT_PACKET_FILTER,
89 OID_GEN_MAXIMUM_TOTAL_SIZE,
90 OID_GEN_MEDIA_CONNECT_STATUS,
91 OID_GEN_PHYSICAL_MEDIUM,
93 /* the statistical stuff */
94 OID_GEN_XMIT_OK,
95 OID_GEN_RCV_OK,
96 OID_GEN_XMIT_ERROR,
97 OID_GEN_RCV_ERROR,
98 OID_GEN_RCV_NO_BUFFER,
99 #ifdef RNDIS_OPTIONAL_STATS
100 OID_GEN_DIRECTED_BYTES_XMIT,
101 OID_GEN_DIRECTED_FRAMES_XMIT,
102 OID_GEN_MULTICAST_BYTES_XMIT,
103 OID_GEN_MULTICAST_FRAMES_XMIT,
104 OID_GEN_BROADCAST_BYTES_XMIT,
105 OID_GEN_BROADCAST_FRAMES_XMIT,
106 OID_GEN_DIRECTED_BYTES_RCV,
107 OID_GEN_DIRECTED_FRAMES_RCV,
108 OID_GEN_MULTICAST_BYTES_RCV,
109 OID_GEN_MULTICAST_FRAMES_RCV,
110 OID_GEN_BROADCAST_BYTES_RCV,
111 OID_GEN_BROADCAST_FRAMES_RCV,
112 OID_GEN_RCV_CRC_ERROR,
113 OID_GEN_TRANSMIT_QUEUE_LENGTH,
114 #endif /* RNDIS_OPTIONAL_STATS */
116 /* mandatory 802.3 */
117 /* the general stuff */
118 OID_802_3_PERMANENT_ADDRESS,
119 OID_802_3_CURRENT_ADDRESS,
120 OID_802_3_MULTICAST_LIST,
121 OID_802_3_MAC_OPTIONS,
122 OID_802_3_MAXIMUM_LIST_SIZE,
124 /* the statistical stuff */
125 OID_802_3_RCV_ERROR_ALIGNMENT,
126 OID_802_3_XMIT_ONE_COLLISION,
127 OID_802_3_XMIT_MORE_COLLISIONS,
128 #ifdef RNDIS_OPTIONAL_STATS
129 OID_802_3_XMIT_DEFERRED,
130 OID_802_3_XMIT_MAX_COLLISIONS,
131 OID_802_3_RCV_OVERRUN,
132 OID_802_3_XMIT_UNDERRUN,
133 OID_802_3_XMIT_HEARTBEAT_FAILURE,
134 OID_802_3_XMIT_TIMES_CRS_LOST,
135 OID_802_3_XMIT_LATE_COLLISIONS,
136 #endif /* RNDIS_OPTIONAL_STATS */
138 #ifdef RNDIS_PM
139 /* PM and wakeup are "mandatory" for USB, but the RNDIS specs
140 * don't say what they mean ... and the NDIS specs are often
141 * confusing and/or ambiguous in this context. (That is, more
142 * so than their specs for the other OIDs.)
144 * FIXME someone who knows what these should do, please
145 * implement them!
148 /* power management */
149 OID_PNP_CAPABILITIES,
150 OID_PNP_QUERY_POWER,
151 OID_PNP_SET_POWER,
153 #ifdef RNDIS_WAKEUP
154 /* wake up host */
155 OID_PNP_ENABLE_WAKE_UP,
156 OID_PNP_ADD_WAKE_UP_PATTERN,
157 OID_PNP_REMOVE_WAKE_UP_PATTERN,
158 #endif /* RNDIS_WAKEUP */
159 #endif /* RNDIS_PM */
163 /* NDIS Functions */
164 static int gen_ndis_query_resp(int configNr, u32 OID, u8 *buf,
165 unsigned buf_len, rndis_resp_t *r)
167 int retval = -ENOTSUPP;
168 u32 length = 4; /* usually */
169 __le32 *outbuf;
170 int i, count;
171 rndis_query_cmplt_type *resp;
172 struct net_device *net;
173 struct rtnl_link_stats64 temp;
174 const struct rtnl_link_stats64 *stats;
176 if (!r) return -ENOMEM;
177 resp = (rndis_query_cmplt_type *)r->buf;
179 if (!resp) return -ENOMEM;
181 if (buf_len && rndis_debug > 1) {
182 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
183 for (i = 0; i < buf_len; i += 16) {
184 pr_debug("%03d: %08x %08x %08x %08x\n", i,
185 get_unaligned_le32(&buf[i]),
186 get_unaligned_le32(&buf[i + 4]),
187 get_unaligned_le32(&buf[i + 8]),
188 get_unaligned_le32(&buf[i + 12]));
192 /* response goes here, right after the header */
193 outbuf = (__le32 *)&resp[1];
194 resp->InformationBufferOffset = cpu_to_le32(16);
196 net = rndis_per_dev_params[configNr].dev;
197 stats = dev_get_stats(net, &temp);
199 switch (OID) {
201 /* general oids (table 4-1) */
203 /* mandatory */
204 case OID_GEN_SUPPORTED_LIST:
205 pr_debug("%s: OID_GEN_SUPPORTED_LIST\n", __func__);
206 length = sizeof(oid_supported_list);
207 count = length / sizeof(u32);
208 for (i = 0; i < count; i++)
209 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
210 retval = 0;
211 break;
213 /* mandatory */
214 case OID_GEN_HARDWARE_STATUS:
215 pr_debug("%s: OID_GEN_HARDWARE_STATUS\n", __func__);
216 /* Bogus question!
217 * Hardware must be ready to receive high level protocols.
218 * BTW:
219 * reddite ergo quae sunt Caesaris Caesari
220 * et quae sunt Dei Deo!
222 *outbuf = cpu_to_le32(0);
223 retval = 0;
224 break;
226 /* mandatory */
227 case OID_GEN_MEDIA_SUPPORTED:
228 pr_debug("%s: OID_GEN_MEDIA_SUPPORTED\n", __func__);
229 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
230 retval = 0;
231 break;
233 /* mandatory */
234 case OID_GEN_MEDIA_IN_USE:
235 pr_debug("%s: OID_GEN_MEDIA_IN_USE\n", __func__);
236 /* one medium, one transport... (maybe you do it better) */
237 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr].medium);
238 retval = 0;
239 break;
241 /* mandatory */
242 case OID_GEN_MAXIMUM_FRAME_SIZE:
243 pr_debug("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
244 if (rndis_per_dev_params[configNr].dev) {
245 *outbuf = cpu_to_le32(
246 rndis_per_dev_params[configNr].dev->mtu);
247 retval = 0;
249 break;
251 /* mandatory */
252 case OID_GEN_LINK_SPEED:
253 if (rndis_debug > 1)
254 pr_debug("%s: OID_GEN_LINK_SPEED\n", __func__);
255 if (rndis_per_dev_params[configNr].media_state
256 == NDIS_MEDIA_STATE_DISCONNECTED)
257 *outbuf = cpu_to_le32(0);
258 else
259 *outbuf = cpu_to_le32(
260 rndis_per_dev_params[configNr].speed);
261 retval = 0;
262 break;
264 /* mandatory */
265 case OID_GEN_TRANSMIT_BLOCK_SIZE:
266 pr_debug("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
267 if (rndis_per_dev_params[configNr].dev) {
268 *outbuf = cpu_to_le32(
269 rndis_per_dev_params[configNr].dev->mtu);
270 retval = 0;
272 break;
274 /* mandatory */
275 case OID_GEN_RECEIVE_BLOCK_SIZE:
276 pr_debug("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
277 if (rndis_per_dev_params[configNr].dev) {
278 *outbuf = cpu_to_le32(
279 rndis_per_dev_params[configNr].dev->mtu);
280 retval = 0;
282 break;
284 /* mandatory */
285 case OID_GEN_VENDOR_ID:
286 pr_debug("%s: OID_GEN_VENDOR_ID\n", __func__);
287 *outbuf = cpu_to_le32(
288 rndis_per_dev_params[configNr].vendorID);
289 retval = 0;
290 break;
292 /* mandatory */
293 case OID_GEN_VENDOR_DESCRIPTION:
294 pr_debug("%s: OID_GEN_VENDOR_DESCRIPTION\n", __func__);
295 if (rndis_per_dev_params[configNr].vendorDescr) {
296 length = strlen(rndis_per_dev_params[configNr].
297 vendorDescr);
298 memcpy(outbuf,
299 rndis_per_dev_params[configNr].vendorDescr,
300 length);
301 } else {
302 outbuf[0] = 0;
304 retval = 0;
305 break;
307 case OID_GEN_VENDOR_DRIVER_VERSION:
308 pr_debug("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
309 /* Created as LE */
310 *outbuf = rndis_driver_version;
311 retval = 0;
312 break;
314 /* mandatory */
315 case OID_GEN_CURRENT_PACKET_FILTER:
316 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
317 *outbuf = cpu_to_le32(*rndis_per_dev_params[configNr].filter);
318 retval = 0;
319 break;
321 /* mandatory */
322 case OID_GEN_MAXIMUM_TOTAL_SIZE:
323 pr_debug("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
324 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
325 retval = 0;
326 break;
328 /* mandatory */
329 case OID_GEN_MEDIA_CONNECT_STATUS:
330 if (rndis_debug > 1)
331 pr_debug("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
332 *outbuf = cpu_to_le32(rndis_per_dev_params[configNr]
333 .media_state);
334 retval = 0;
335 break;
337 case OID_GEN_PHYSICAL_MEDIUM:
338 pr_debug("%s: OID_GEN_PHYSICAL_MEDIUM\n", __func__);
339 *outbuf = cpu_to_le32(0);
340 retval = 0;
341 break;
343 /* The RNDIS specification is incomplete/wrong. Some versions
344 * of MS-Windows expect OIDs that aren't specified there. Other
345 * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
347 case OID_GEN_MAC_OPTIONS: /* from WinME */
348 pr_debug("%s: OID_GEN_MAC_OPTIONS\n", __func__);
349 *outbuf = cpu_to_le32(
350 NDIS_MAC_OPTION_RECEIVE_SERIALIZED
351 | NDIS_MAC_OPTION_FULL_DUPLEX);
352 retval = 0;
353 break;
355 /* statistics OIDs (table 4-2) */
357 /* mandatory */
358 case OID_GEN_XMIT_OK:
359 if (rndis_debug > 1)
360 pr_debug("%s: OID_GEN_XMIT_OK\n", __func__);
361 if (stats) {
362 *outbuf = cpu_to_le32(stats->tx_packets
363 - stats->tx_errors - stats->tx_dropped);
364 retval = 0;
366 break;
368 /* mandatory */
369 case OID_GEN_RCV_OK:
370 if (rndis_debug > 1)
371 pr_debug("%s: OID_GEN_RCV_OK\n", __func__);
372 if (stats) {
373 *outbuf = cpu_to_le32(stats->rx_packets
374 - stats->rx_errors - stats->rx_dropped);
375 retval = 0;
377 break;
379 /* mandatory */
380 case OID_GEN_XMIT_ERROR:
381 if (rndis_debug > 1)
382 pr_debug("%s: OID_GEN_XMIT_ERROR\n", __func__);
383 if (stats) {
384 *outbuf = cpu_to_le32(stats->tx_errors);
385 retval = 0;
387 break;
389 /* mandatory */
390 case OID_GEN_RCV_ERROR:
391 if (rndis_debug > 1)
392 pr_debug("%s: OID_GEN_RCV_ERROR\n", __func__);
393 if (stats) {
394 *outbuf = cpu_to_le32(stats->rx_errors);
395 retval = 0;
397 break;
399 /* mandatory */
400 case OID_GEN_RCV_NO_BUFFER:
401 pr_debug("%s: OID_GEN_RCV_NO_BUFFER\n", __func__);
402 if (stats) {
403 *outbuf = cpu_to_le32(stats->rx_dropped);
404 retval = 0;
406 break;
408 /* ieee802.3 OIDs (table 4-3) */
410 /* mandatory */
411 case OID_802_3_PERMANENT_ADDRESS:
412 pr_debug("%s: OID_802_3_PERMANENT_ADDRESS\n", __func__);
413 if (rndis_per_dev_params[configNr].dev) {
414 length = ETH_ALEN;
415 memcpy(outbuf,
416 rndis_per_dev_params[configNr].host_mac,
417 length);
418 retval = 0;
420 break;
422 /* mandatory */
423 case OID_802_3_CURRENT_ADDRESS:
424 pr_debug("%s: OID_802_3_CURRENT_ADDRESS\n", __func__);
425 if (rndis_per_dev_params[configNr].dev) {
426 length = ETH_ALEN;
427 memcpy(outbuf,
428 rndis_per_dev_params [configNr].host_mac,
429 length);
430 retval = 0;
432 break;
434 /* mandatory */
435 case OID_802_3_MULTICAST_LIST:
436 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
437 /* Multicast base address only */
438 *outbuf = cpu_to_le32(0xE0000000);
439 retval = 0;
440 break;
442 /* mandatory */
443 case OID_802_3_MAXIMUM_LIST_SIZE:
444 pr_debug("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
445 /* Multicast base address only */
446 *outbuf = cpu_to_le32(1);
447 retval = 0;
448 break;
450 case OID_802_3_MAC_OPTIONS:
451 pr_debug("%s: OID_802_3_MAC_OPTIONS\n", __func__);
452 *outbuf = cpu_to_le32(0);
453 retval = 0;
454 break;
456 /* ieee802.3 statistics OIDs (table 4-4) */
458 /* mandatory */
459 case OID_802_3_RCV_ERROR_ALIGNMENT:
460 pr_debug("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
461 if (stats) {
462 *outbuf = cpu_to_le32(stats->rx_frame_errors);
463 retval = 0;
465 break;
467 /* mandatory */
468 case OID_802_3_XMIT_ONE_COLLISION:
469 pr_debug("%s: OID_802_3_XMIT_ONE_COLLISION\n", __func__);
470 *outbuf = cpu_to_le32(0);
471 retval = 0;
472 break;
474 /* mandatory */
475 case OID_802_3_XMIT_MORE_COLLISIONS:
476 pr_debug("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
477 *outbuf = cpu_to_le32(0);
478 retval = 0;
479 break;
481 default:
482 pr_warning("%s: query unknown OID 0x%08X\n",
483 __func__, OID);
485 if (retval < 0)
486 length = 0;
488 resp->InformationBufferLength = cpu_to_le32(length);
489 r->length = length + sizeof(*resp);
490 resp->MessageLength = cpu_to_le32(r->length);
491 return retval;
494 static int gen_ndis_set_resp(u8 configNr, u32 OID, u8 *buf, u32 buf_len,
495 rndis_resp_t *r)
497 rndis_set_cmplt_type *resp;
498 int i, retval = -ENOTSUPP;
499 struct rndis_params *params;
501 if (!r)
502 return -ENOMEM;
503 resp = (rndis_set_cmplt_type *)r->buf;
504 if (!resp)
505 return -ENOMEM;
507 if (buf_len && rndis_debug > 1) {
508 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
509 for (i = 0; i < buf_len; i += 16) {
510 pr_debug("%03d: %08x %08x %08x %08x\n", i,
511 get_unaligned_le32(&buf[i]),
512 get_unaligned_le32(&buf[i + 4]),
513 get_unaligned_le32(&buf[i + 8]),
514 get_unaligned_le32(&buf[i + 12]));
518 params = &rndis_per_dev_params[configNr];
519 switch (OID) {
520 case OID_GEN_CURRENT_PACKET_FILTER:
522 /* these NDIS_PACKET_TYPE_* bitflags are shared with
523 * cdc_filter; it's not RNDIS-specific
524 * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
525 * PROMISCUOUS, DIRECTED,
526 * MULTICAST, ALL_MULTICAST, BROADCAST
528 *params->filter = (u16)get_unaligned_le32(buf);
529 pr_debug("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
530 __func__, *params->filter);
532 /* this call has a significant side effect: it's
533 * what makes the packet flow start and stop, like
534 * activating the CDC Ethernet altsetting.
536 retval = 0;
537 if (*params->filter) {
538 params->state = RNDIS_DATA_INITIALIZED;
539 netif_carrier_on(params->dev);
540 if (netif_running(params->dev))
541 netif_wake_queue(params->dev);
542 } else {
543 params->state = RNDIS_INITIALIZED;
544 netif_carrier_off(params->dev);
545 netif_stop_queue(params->dev);
547 break;
549 case OID_802_3_MULTICAST_LIST:
550 /* I think we can ignore this */
551 pr_debug("%s: OID_802_3_MULTICAST_LIST\n", __func__);
552 retval = 0;
553 break;
555 default:
556 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
557 __func__, OID, buf_len);
560 return retval;
564 * Response Functions
567 static int rndis_init_response(int configNr, rndis_init_msg_type *buf)
569 rndis_init_cmplt_type *resp;
570 rndis_resp_t *r;
571 struct rndis_params *params = rndis_per_dev_params + configNr;
573 if (!params->dev)
574 return -ENOTSUPP;
576 r = rndis_add_response(configNr, sizeof(rndis_init_cmplt_type));
577 if (!r)
578 return -ENOMEM;
579 resp = (rndis_init_cmplt_type *)r->buf;
581 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INITIALIZE_CMPLT);
582 resp->MessageLength = cpu_to_le32(52);
583 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
584 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
585 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
586 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
587 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
588 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
589 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
590 resp->MaxTransferSize = cpu_to_le32(
591 params->dev->mtu
592 + sizeof(struct ethhdr)
593 + sizeof(struct rndis_packet_msg_type)
594 + 22);
595 resp->PacketAlignmentFactor = cpu_to_le32(0);
596 resp->AFListOffset = cpu_to_le32(0);
597 resp->AFListSize = cpu_to_le32(0);
599 params->resp_avail(params->v);
600 return 0;
603 static int rndis_query_response(int configNr, rndis_query_msg_type *buf)
605 rndis_query_cmplt_type *resp;
606 rndis_resp_t *r;
607 struct rndis_params *params = rndis_per_dev_params + configNr;
609 /* pr_debug("%s: OID = %08X\n", __func__, cpu_to_le32(buf->OID)); */
610 if (!params->dev)
611 return -ENOTSUPP;
614 * we need more memory:
615 * gen_ndis_query_resp expects enough space for
616 * rndis_query_cmplt_type followed by data.
617 * oid_supported_list is the largest data reply
619 r = rndis_add_response(configNr,
620 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
621 if (!r)
622 return -ENOMEM;
623 resp = (rndis_query_cmplt_type *)r->buf;
625 resp->MessageType = cpu_to_le32(REMOTE_NDIS_QUERY_CMPLT);
626 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
628 if (gen_ndis_query_resp(configNr, le32_to_cpu(buf->OID),
629 le32_to_cpu(buf->InformationBufferOffset)
630 + 8 + (u8 *)buf,
631 le32_to_cpu(buf->InformationBufferLength),
632 r)) {
633 /* OID not supported */
634 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
635 resp->MessageLength = cpu_to_le32(sizeof *resp);
636 resp->InformationBufferLength = cpu_to_le32(0);
637 resp->InformationBufferOffset = cpu_to_le32(0);
638 } else
639 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
641 params->resp_avail(params->v);
642 return 0;
645 static int rndis_set_response(int configNr, rndis_set_msg_type *buf)
647 u32 BufLength, BufOffset;
648 rndis_set_cmplt_type *resp;
649 rndis_resp_t *r;
650 struct rndis_params *params = rndis_per_dev_params + configNr;
652 r = rndis_add_response(configNr, sizeof(rndis_set_cmplt_type));
653 if (!r)
654 return -ENOMEM;
655 resp = (rndis_set_cmplt_type *)r->buf;
657 BufLength = le32_to_cpu(buf->InformationBufferLength);
658 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
660 #ifdef VERBOSE_DEBUG
661 pr_debug("%s: Length: %d\n", __func__, BufLength);
662 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
663 pr_debug("%s: InfoBuffer: ", __func__);
665 for (i = 0; i < BufLength; i++) {
666 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
669 pr_debug("\n");
670 #endif
672 resp->MessageType = cpu_to_le32(REMOTE_NDIS_SET_CMPLT);
673 resp->MessageLength = cpu_to_le32(16);
674 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
675 if (gen_ndis_set_resp(configNr, le32_to_cpu(buf->OID),
676 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
677 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
678 else
679 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
681 params->resp_avail(params->v);
682 return 0;
685 static int rndis_reset_response(int configNr, rndis_reset_msg_type *buf)
687 rndis_reset_cmplt_type *resp;
688 rndis_resp_t *r;
689 struct rndis_params *params = rndis_per_dev_params + configNr;
691 r = rndis_add_response(configNr, sizeof(rndis_reset_cmplt_type));
692 if (!r)
693 return -ENOMEM;
694 resp = (rndis_reset_cmplt_type *)r->buf;
696 resp->MessageType = cpu_to_le32(REMOTE_NDIS_RESET_CMPLT);
697 resp->MessageLength = cpu_to_le32(16);
698 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
699 /* resent information */
700 resp->AddressingReset = cpu_to_le32(1);
702 params->resp_avail(params->v);
703 return 0;
706 static int rndis_keepalive_response(int configNr,
707 rndis_keepalive_msg_type *buf)
709 rndis_keepalive_cmplt_type *resp;
710 rndis_resp_t *r;
711 struct rndis_params *params = rndis_per_dev_params + configNr;
713 /* host "should" check only in RNDIS_DATA_INITIALIZED state */
715 r = rndis_add_response(configNr, sizeof(rndis_keepalive_cmplt_type));
716 if (!r)
717 return -ENOMEM;
718 resp = (rndis_keepalive_cmplt_type *)r->buf;
720 resp->MessageType = cpu_to_le32(
721 REMOTE_NDIS_KEEPALIVE_CMPLT);
722 resp->MessageLength = cpu_to_le32(16);
723 resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
724 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
726 params->resp_avail(params->v);
727 return 0;
732 * Device to Host Comunication
734 static int rndis_indicate_status_msg(int configNr, u32 status)
736 rndis_indicate_status_msg_type *resp;
737 rndis_resp_t *r;
738 struct rndis_params *params = rndis_per_dev_params + configNr;
740 if (params->state == RNDIS_UNINITIALIZED)
741 return -ENOTSUPP;
743 r = rndis_add_response(configNr,
744 sizeof(rndis_indicate_status_msg_type));
745 if (!r)
746 return -ENOMEM;
747 resp = (rndis_indicate_status_msg_type *)r->buf;
749 resp->MessageType = cpu_to_le32(REMOTE_NDIS_INDICATE_STATUS_MSG);
750 resp->MessageLength = cpu_to_le32(20);
751 resp->Status = cpu_to_le32(status);
752 resp->StatusBufferLength = cpu_to_le32(0);
753 resp->StatusBufferOffset = cpu_to_le32(0);
755 params->resp_avail(params->v);
756 return 0;
759 int rndis_signal_connect(int configNr)
761 rndis_per_dev_params[configNr].media_state
762 = NDIS_MEDIA_STATE_CONNECTED;
763 return rndis_indicate_status_msg(configNr,
764 RNDIS_STATUS_MEDIA_CONNECT);
767 int rndis_signal_disconnect(int configNr)
769 rndis_per_dev_params[configNr].media_state
770 = NDIS_MEDIA_STATE_DISCONNECTED;
771 return rndis_indicate_status_msg(configNr,
772 RNDIS_STATUS_MEDIA_DISCONNECT);
775 void rndis_uninit(int configNr)
777 u8 *buf;
778 u32 length;
780 if (configNr >= RNDIS_MAX_CONFIGS)
781 return;
782 rndis_per_dev_params[configNr].state = RNDIS_UNINITIALIZED;
784 /* drain the response queue */
785 while ((buf = rndis_get_next_response(configNr, &length)))
786 rndis_free_response(configNr, buf);
789 void rndis_set_host_mac(int configNr, const u8 *addr)
791 rndis_per_dev_params[configNr].host_mac = addr;
795 * Message Parser
797 int rndis_msg_parser(u8 configNr, u8 *buf)
799 u32 MsgType, MsgLength;
800 __le32 *tmp;
801 struct rndis_params *params;
803 if (!buf)
804 return -ENOMEM;
806 tmp = (__le32 *)buf;
807 MsgType = get_unaligned_le32(tmp++);
808 MsgLength = get_unaligned_le32(tmp++);
810 if (configNr >= RNDIS_MAX_CONFIGS)
811 return -ENOTSUPP;
812 params = &rndis_per_dev_params[configNr];
814 /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
815 * rx/tx statistics and link status, in addition to KEEPALIVE traffic
816 * and normal HC level polling to see if there's any IN traffic.
819 /* For USB: responses may take up to 10 seconds */
820 switch (MsgType) {
821 case REMOTE_NDIS_INITIALIZE_MSG:
822 pr_debug("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
823 __func__);
824 params->state = RNDIS_INITIALIZED;
825 return rndis_init_response(configNr,
826 (rndis_init_msg_type *)buf);
828 case REMOTE_NDIS_HALT_MSG:
829 pr_debug("%s: REMOTE_NDIS_HALT_MSG\n",
830 __func__);
831 params->state = RNDIS_UNINITIALIZED;
832 if (params->dev) {
833 netif_carrier_off(params->dev);
834 netif_stop_queue(params->dev);
836 return 0;
838 case REMOTE_NDIS_QUERY_MSG:
839 return rndis_query_response(configNr,
840 (rndis_query_msg_type *)buf);
842 case REMOTE_NDIS_SET_MSG:
843 return rndis_set_response(configNr,
844 (rndis_set_msg_type *)buf);
846 case REMOTE_NDIS_RESET_MSG:
847 pr_debug("%s: REMOTE_NDIS_RESET_MSG\n",
848 __func__);
849 return rndis_reset_response(configNr,
850 (rndis_reset_msg_type *)buf);
852 case REMOTE_NDIS_KEEPALIVE_MSG:
853 /* For USB: host does this every 5 seconds */
854 if (rndis_debug > 1)
855 pr_debug("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
856 __func__);
857 return rndis_keepalive_response(configNr,
858 (rndis_keepalive_msg_type *)
859 buf);
861 default:
862 /* At least Windows XP emits some undefined RNDIS messages.
863 * In one case those messages seemed to relate to the host
864 * suspending itself.
866 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
867 __func__, MsgType, MsgLength);
869 unsigned i;
870 for (i = 0; i < MsgLength; i += 16) {
871 pr_debug("%03d: "
872 " %02x %02x %02x %02x"
873 " %02x %02x %02x %02x"
874 " %02x %02x %02x %02x"
875 " %02x %02x %02x %02x"
876 "\n",
878 buf[i], buf [i+1],
879 buf[i+2], buf[i+3],
880 buf[i+4], buf [i+5],
881 buf[i+6], buf[i+7],
882 buf[i+8], buf [i+9],
883 buf[i+10], buf[i+11],
884 buf[i+12], buf [i+13],
885 buf[i+14], buf[i+15]);
888 break;
891 return -ENOTSUPP;
894 int rndis_register(void (*resp_avail)(void *v), void *v)
896 u8 i;
898 if (!resp_avail)
899 return -EINVAL;
901 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
902 if (!rndis_per_dev_params[i].used) {
903 rndis_per_dev_params[i].used = 1;
904 rndis_per_dev_params[i].resp_avail = resp_avail;
905 rndis_per_dev_params[i].v = v;
906 pr_debug("%s: configNr = %d\n", __func__, i);
907 return i;
910 pr_debug("failed\n");
912 return -ENODEV;
915 void rndis_deregister(int configNr)
917 pr_debug("%s:\n", __func__);
919 if (configNr >= RNDIS_MAX_CONFIGS) return;
920 rndis_per_dev_params[configNr].used = 0;
923 int rndis_set_param_dev(u8 configNr, struct net_device *dev, u16 *cdc_filter)
925 pr_debug("%s:\n", __func__);
926 if (!dev)
927 return -EINVAL;
928 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
930 rndis_per_dev_params[configNr].dev = dev;
931 rndis_per_dev_params[configNr].filter = cdc_filter;
933 return 0;
936 int rndis_set_param_vendor(u8 configNr, u32 vendorID, const char *vendorDescr)
938 pr_debug("%s:\n", __func__);
939 if (!vendorDescr) return -1;
940 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
942 rndis_per_dev_params[configNr].vendorID = vendorID;
943 rndis_per_dev_params[configNr].vendorDescr = vendorDescr;
945 return 0;
948 int rndis_set_param_medium(u8 configNr, u32 medium, u32 speed)
950 pr_debug("%s: %u %u\n", __func__, medium, speed);
951 if (configNr >= RNDIS_MAX_CONFIGS) return -1;
953 rndis_per_dev_params[configNr].medium = medium;
954 rndis_per_dev_params[configNr].speed = speed;
956 return 0;
959 void rndis_add_hdr(struct sk_buff *skb)
961 struct rndis_packet_msg_type *header;
963 if (!skb)
964 return;
965 header = (void *)skb_push(skb, sizeof(*header));
966 memset(header, 0, sizeof *header);
967 header->MessageType = cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
968 header->MessageLength = cpu_to_le32(skb->len);
969 header->DataOffset = cpu_to_le32(36);
970 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
973 void rndis_free_response(int configNr, u8 *buf)
975 rndis_resp_t *r;
976 struct list_head *act, *tmp;
978 list_for_each_safe(act, tmp,
979 &(rndis_per_dev_params[configNr].resp_queue))
981 r = list_entry(act, rndis_resp_t, list);
982 if (r && r->buf == buf) {
983 list_del(&r->list);
984 kfree(r);
989 u8 *rndis_get_next_response(int configNr, u32 *length)
991 rndis_resp_t *r;
992 struct list_head *act, *tmp;
994 if (!length) return NULL;
996 list_for_each_safe(act, tmp,
997 &(rndis_per_dev_params[configNr].resp_queue))
999 r = list_entry(act, rndis_resp_t, list);
1000 if (!r->send) {
1001 r->send = 1;
1002 *length = r->length;
1003 return r->buf;
1007 return NULL;
1010 static rndis_resp_t *rndis_add_response(int configNr, u32 length)
1012 rndis_resp_t *r;
1014 /* NOTE: this gets copied into ether.c USB_BUFSIZ bytes ... */
1015 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
1016 if (!r) return NULL;
1018 r->buf = (u8 *)(r + 1);
1019 r->length = length;
1020 r->send = 0;
1022 list_add_tail(&r->list,
1023 &(rndis_per_dev_params[configNr].resp_queue));
1024 return r;
1027 int rndis_rm_hdr(struct gether *port,
1028 struct sk_buff *skb,
1029 struct sk_buff_head *list)
1031 /* tmp points to a struct rndis_packet_msg_type */
1032 __le32 *tmp = (void *)skb->data;
1034 /* MessageType, MessageLength */
1035 if (cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1036 != get_unaligned(tmp++)) {
1037 dev_kfree_skb_any(skb);
1038 return -EINVAL;
1040 tmp++;
1042 /* DataOffset, DataLength */
1043 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1044 dev_kfree_skb_any(skb);
1045 return -EOVERFLOW;
1047 skb_trim(skb, get_unaligned_le32(tmp++));
1049 skb_queue_tail(list, skb);
1050 return 0;
1053 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1055 static int rndis_proc_show(struct seq_file *m, void *v)
1057 rndis_params *param = m->private;
1059 seq_printf(m,
1060 "Config Nr. %d\n"
1061 "used : %s\n"
1062 "state : %s\n"
1063 "medium : 0x%08X\n"
1064 "speed : %d\n"
1065 "cable : %s\n"
1066 "vendor ID : 0x%08X\n"
1067 "vendor : %s\n",
1068 param->confignr, (param->used) ? "y" : "n",
1069 ({ char *s = "?";
1070 switch (param->state) {
1071 case RNDIS_UNINITIALIZED:
1072 s = "RNDIS_UNINITIALIZED"; break;
1073 case RNDIS_INITIALIZED:
1074 s = "RNDIS_INITIALIZED"; break;
1075 case RNDIS_DATA_INITIALIZED:
1076 s = "RNDIS_DATA_INITIALIZED"; break;
1077 }; s; }),
1078 param->medium,
1079 (param->media_state) ? 0 : param->speed*100,
1080 (param->media_state) ? "disconnected" : "connected",
1081 param->vendorID, param->vendorDescr);
1082 return 0;
1085 static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1086 size_t count, loff_t *ppos)
1088 rndis_params *p = PDE(file->f_path.dentry->d_inode)->data;
1089 u32 speed = 0;
1090 int i, fl_speed = 0;
1092 for (i = 0; i < count; i++) {
1093 char c;
1094 if (get_user(c, buffer))
1095 return -EFAULT;
1096 switch (c) {
1097 case '0':
1098 case '1':
1099 case '2':
1100 case '3':
1101 case '4':
1102 case '5':
1103 case '6':
1104 case '7':
1105 case '8':
1106 case '9':
1107 fl_speed = 1;
1108 speed = speed * 10 + c - '0';
1109 break;
1110 case 'C':
1111 case 'c':
1112 rndis_signal_connect(p->confignr);
1113 break;
1114 case 'D':
1115 case 'd':
1116 rndis_signal_disconnect(p->confignr);
1117 break;
1118 default:
1119 if (fl_speed) p->speed = speed;
1120 else pr_debug("%c is not valid\n", c);
1121 break;
1124 buffer++;
1127 return count;
1130 static int rndis_proc_open(struct inode *inode, struct file *file)
1132 return single_open(file, rndis_proc_show, PDE(inode)->data);
1135 static const struct file_operations rndis_proc_fops = {
1136 .owner = THIS_MODULE,
1137 .open = rndis_proc_open,
1138 .read = seq_read,
1139 .llseek = seq_lseek,
1140 .release = single_release,
1141 .write = rndis_proc_write,
1144 #define NAME_TEMPLATE "driver/rndis-%03d"
1146 static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1148 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1151 int rndis_init(void)
1153 u8 i;
1155 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1156 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1157 char name [20];
1159 sprintf(name, NAME_TEMPLATE, i);
1160 rndis_connect_state[i] = proc_create_data(name, 0660, NULL,
1161 &rndis_proc_fops,
1162 (void *)(rndis_per_dev_params + i));
1163 if (!rndis_connect_state[i]) {
1164 pr_debug("%s: remove entries", __func__);
1165 while (i) {
1166 sprintf(name, NAME_TEMPLATE, --i);
1167 remove_proc_entry(name, NULL);
1169 pr_debug("\n");
1170 return -EIO;
1172 #endif
1173 rndis_per_dev_params[i].confignr = i;
1174 rndis_per_dev_params[i].used = 0;
1175 rndis_per_dev_params[i].state = RNDIS_UNINITIALIZED;
1176 rndis_per_dev_params[i].media_state
1177 = NDIS_MEDIA_STATE_DISCONNECTED;
1178 INIT_LIST_HEAD(&(rndis_per_dev_params[i].resp_queue));
1181 return 0;
1184 void rndis_exit(void)
1186 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1187 u8 i;
1188 char name[20];
1190 for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1191 sprintf(name, NAME_TEMPLATE, i);
1192 remove_proc_entry(name, NULL);
1194 #endif