]> err.no Git - linux-2.6/blob - drivers/usb/gadget/rndis.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / usb / gadget / rndis.c
1 /*
2  * RNDIS MSG parser
3  *
4  * Version:     $Id: rndis.c,v 1.19 2004/03/25 21:33:46 robert Exp $
5  *
6  * Authors:     Benedikt Spranger, Pengutronix
7  *              Robert Schwebel, Pengutronix
8  *
9  *              This program is free software; you can redistribute it and/or
10  *              modify it under the terms of the GNU General Public License
11  *              version 2, as published by the Free Software Foundation.
12  *
13  *              This software was originally developed in conformance with
14  *              Microsoft's Remote NDIS Specification License Agreement.
15  *
16  * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
17  *              Fixed message length bug in init_response
18  *
19  * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
20  *              Fixed rndis_rm_hdr length bug.
21  *
22  * Copyright (C) 2004 by David Brownell
23  *              updates to merge with Linux 2.6, better match RNDIS spec
24  */
25
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/proc_fs.h>
33 #include <linux/netdevice.h>
34
35 #include <asm/io.h>
36 #include <asm/byteorder.h>
37 #include <asm/system.h>
38 #include <asm/unaligned.h>
39
40
41 #undef  RNDIS_PM
42 #undef  RNDIS_WAKEUP
43 #undef  VERBOSE
44
45 #include "rndis.h"
46
47
48 /* The driver for your USB chip needs to support ep0 OUT to work with
49  * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
50  *
51  * Windows hosts need an INF file like Documentation/usb/linux.inf
52  * and will be happier if you provide the host_addr module parameter.
53  */
54
55 #if 0
56 #define DBG(str,args...) do { \
57         if (rndis_debug) \
58                 printk(KERN_DEBUG str , ## args ); \
59         } while (0)
60 static int rndis_debug = 0;
61
62 module_param (rndis_debug, int, 0);
63 MODULE_PARM_DESC (rndis_debug, "enable debugging");
64
65 #else
66
67 #define rndis_debug             0
68 #define DBG(str,args...)        do{}while(0)
69 #endif
70
71 #define RNDIS_MAX_CONFIGS       1
72
73
74 static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
75
76 /* Driver Version */
77 static const __le32 rndis_driver_version = __constant_cpu_to_le32 (1);
78
79 /* Function Prototypes */
80 static rndis_resp_t *rndis_add_response (int configNr, u32 length);
81
82
83 /* supported OIDs */
84 static const u32 oid_supported_list [] =
85 {
86         /* the general stuff */
87         OID_GEN_SUPPORTED_LIST,
88         OID_GEN_HARDWARE_STATUS,
89         OID_GEN_MEDIA_SUPPORTED,
90         OID_GEN_MEDIA_IN_USE,
91         OID_GEN_MAXIMUM_FRAME_SIZE,
92         OID_GEN_LINK_SPEED,
93         OID_GEN_TRANSMIT_BLOCK_SIZE,
94         OID_GEN_RECEIVE_BLOCK_SIZE,
95         OID_GEN_VENDOR_ID,
96         OID_GEN_VENDOR_DESCRIPTION,
97         OID_GEN_VENDOR_DRIVER_VERSION,
98         OID_GEN_CURRENT_PACKET_FILTER,
99         OID_GEN_MAXIMUM_TOTAL_SIZE,
100         OID_GEN_MEDIA_CONNECT_STATUS,
101         OID_GEN_PHYSICAL_MEDIUM,
102 #if 0
103         OID_GEN_RNDIS_CONFIG_PARAMETER,
104 #endif
105
106         /* the statistical stuff */
107         OID_GEN_XMIT_OK,
108         OID_GEN_RCV_OK,
109         OID_GEN_XMIT_ERROR,
110         OID_GEN_RCV_ERROR,
111         OID_GEN_RCV_NO_BUFFER,
112 #ifdef  RNDIS_OPTIONAL_STATS
113         OID_GEN_DIRECTED_BYTES_XMIT,
114         OID_GEN_DIRECTED_FRAMES_XMIT,
115         OID_GEN_MULTICAST_BYTES_XMIT,
116         OID_GEN_MULTICAST_FRAMES_XMIT,
117         OID_GEN_BROADCAST_BYTES_XMIT,
118         OID_GEN_BROADCAST_FRAMES_XMIT,
119         OID_GEN_DIRECTED_BYTES_RCV,
120         OID_GEN_DIRECTED_FRAMES_RCV,
121         OID_GEN_MULTICAST_BYTES_RCV,
122         OID_GEN_MULTICAST_FRAMES_RCV,
123         OID_GEN_BROADCAST_BYTES_RCV,
124         OID_GEN_BROADCAST_FRAMES_RCV,
125         OID_GEN_RCV_CRC_ERROR,
126         OID_GEN_TRANSMIT_QUEUE_LENGTH,
127 #endif  /* RNDIS_OPTIONAL_STATS */
128
129         /* mandatory 802.3 */
130         /* the general stuff */
131         OID_802_3_PERMANENT_ADDRESS,
132         OID_802_3_CURRENT_ADDRESS,
133         OID_802_3_MULTICAST_LIST,
134         OID_802_3_MAC_OPTIONS,
135         OID_802_3_MAXIMUM_LIST_SIZE,
136
137         /* the statistical stuff */
138         OID_802_3_RCV_ERROR_ALIGNMENT,
139         OID_802_3_XMIT_ONE_COLLISION,
140         OID_802_3_XMIT_MORE_COLLISIONS,
141 #ifdef  RNDIS_OPTIONAL_STATS
142         OID_802_3_XMIT_DEFERRED,
143         OID_802_3_XMIT_MAX_COLLISIONS,
144         OID_802_3_RCV_OVERRUN,
145         OID_802_3_XMIT_UNDERRUN,
146         OID_802_3_XMIT_HEARTBEAT_FAILURE,
147         OID_802_3_XMIT_TIMES_CRS_LOST,
148         OID_802_3_XMIT_LATE_COLLISIONS,
149 #endif  /* RNDIS_OPTIONAL_STATS */
150
151 #ifdef  RNDIS_PM
152         /* PM and wakeup are mandatory for USB: */
153
154         /* power management */
155         OID_PNP_CAPABILITIES,
156         OID_PNP_QUERY_POWER,
157         OID_PNP_SET_POWER,
158
159 #ifdef  RNDIS_WAKEUP
160         /* wake up host */
161         OID_PNP_ENABLE_WAKE_UP,
162         OID_PNP_ADD_WAKE_UP_PATTERN,
163         OID_PNP_REMOVE_WAKE_UP_PATTERN,
164 #endif  /* RNDIS_WAKEUP */
165 #endif  /* RNDIS_PM */
166 };
167
168
169 /* NDIS Functions */
170 static int
171 gen_ndis_query_resp (int configNr, u32 OID, u8 *buf, unsigned buf_len,
172                 rndis_resp_t *r)
173 {
174         int                     retval = -ENOTSUPP;
175         u32                     length = 4;     /* usually */
176         __le32                  *outbuf;
177         int                     i, count;
178         rndis_query_cmplt_type  *resp;
179
180         if (!r) return -ENOMEM;
181         resp = (rndis_query_cmplt_type *) r->buf;
182
183         if (!resp) return -ENOMEM;
184
185         if (buf_len && rndis_debug > 1) {
186                 DBG("query OID %08x value, len %d:\n", OID, buf_len);
187                 for (i = 0; i < buf_len; i += 16) {
188                         DBG("%03d: %08x %08x %08x %08x\n", i,
189                                 le32_to_cpu(get_unaligned((__le32 *)
190                                         &buf[i])),
191                                 le32_to_cpu(get_unaligned((__le32 *)
192                                         &buf[i + 4])),
193                                 le32_to_cpu(get_unaligned((__le32 *)
194                                         &buf[i + 8])),
195                                 le32_to_cpu(get_unaligned((__le32 *)
196                                         &buf[i + 12])));
197                 }
198         }
199
200         /* response goes here, right after the header */
201         outbuf = (__le32 *) &resp[1];
202         resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
203
204         switch (OID) {
205
206         /* general oids (table 4-1) */
207
208         /* mandatory */
209         case OID_GEN_SUPPORTED_LIST:
210                 DBG("%s: OID_GEN_SUPPORTED_LIST\n", __FUNCTION__);
211                 length = sizeof (oid_supported_list);
212                 count  = length / sizeof (u32);
213                 for (i = 0; i < count; i++)
214                         outbuf[i] = cpu_to_le32 (oid_supported_list[i]);
215                 retval = 0;
216                 break;
217
218         /* mandatory */
219         case OID_GEN_HARDWARE_STATUS:
220                 DBG("%s: OID_GEN_HARDWARE_STATUS\n", __FUNCTION__);
221                 /* Bogus question!
222                  * Hardware must be ready to receive high level protocols.
223                  * BTW:
224                  * reddite ergo quae sunt Caesaris Caesari
225                  * et quae sunt Dei Deo!
226                  */
227                 *outbuf = __constant_cpu_to_le32 (0);
228                 retval = 0;
229                 break;
230
231         /* mandatory */
232         case OID_GEN_MEDIA_SUPPORTED:
233                 DBG("%s: OID_GEN_MEDIA_SUPPORTED\n", __FUNCTION__);
234                 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
235                 retval = 0;
236                 break;
237
238         /* mandatory */
239         case OID_GEN_MEDIA_IN_USE:
240                 DBG("%s: OID_GEN_MEDIA_IN_USE\n", __FUNCTION__);
241                 /* one medium, one transport... (maybe you do it better) */
242                 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr].medium);
243                 retval = 0;
244                 break;
245
246         /* mandatory */
247         case OID_GEN_MAXIMUM_FRAME_SIZE:
248                 DBG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __FUNCTION__);
249                 if (rndis_per_dev_params [configNr].dev) {
250                         *outbuf = cpu_to_le32 (
251                                 rndis_per_dev_params [configNr].dev->mtu);
252                         retval = 0;
253                 }
254                 break;
255
256         /* mandatory */
257         case OID_GEN_LINK_SPEED:
258                 if (rndis_debug > 1)
259                         DBG("%s: OID_GEN_LINK_SPEED\n", __FUNCTION__);
260                 if (rndis_per_dev_params [configNr].media_state
261                                 == NDIS_MEDIA_STATE_DISCONNECTED)
262                         *outbuf = __constant_cpu_to_le32 (0);
263                 else
264                         *outbuf = cpu_to_le32 (
265                                 rndis_per_dev_params [configNr].speed);
266                 retval = 0;
267                 break;
268
269         /* mandatory */
270         case OID_GEN_TRANSMIT_BLOCK_SIZE:
271                 DBG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __FUNCTION__);
272                 if (rndis_per_dev_params [configNr].dev) {
273                         *outbuf = cpu_to_le32 (
274                                 rndis_per_dev_params [configNr].dev->mtu);
275                         retval = 0;
276                 }
277                 break;
278
279         /* mandatory */
280         case OID_GEN_RECEIVE_BLOCK_SIZE:
281                 DBG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __FUNCTION__);
282                 if (rndis_per_dev_params [configNr].dev) {
283                         *outbuf = cpu_to_le32 (
284                                 rndis_per_dev_params [configNr].dev->mtu);
285                         retval = 0;
286                 }
287                 break;
288
289         /* mandatory */
290         case OID_GEN_VENDOR_ID:
291                 DBG("%s: OID_GEN_VENDOR_ID\n", __FUNCTION__);
292                 *outbuf = cpu_to_le32 (
293                         rndis_per_dev_params [configNr].vendorID);
294                 retval = 0;
295                 break;
296
297         /* mandatory */
298         case OID_GEN_VENDOR_DESCRIPTION:
299                 DBG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __FUNCTION__);
300                 length = strlen (rndis_per_dev_params [configNr].vendorDescr);
301                 memcpy (outbuf,
302                         rndis_per_dev_params [configNr].vendorDescr, length);
303                 retval = 0;
304                 break;
305
306         case OID_GEN_VENDOR_DRIVER_VERSION:
307                 DBG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __FUNCTION__);
308                 /* Created as LE */
309                 *outbuf = rndis_driver_version;
310                 retval = 0;
311                 break;
312
313         /* mandatory */
314         case OID_GEN_CURRENT_PACKET_FILTER:
315                 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __FUNCTION__);
316                 *outbuf = cpu_to_le32 (*rndis_per_dev_params[configNr].filter);
317                 retval = 0;
318                 break;
319
320         /* mandatory */
321         case OID_GEN_MAXIMUM_TOTAL_SIZE:
322                 DBG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __FUNCTION__);
323                 *outbuf = __constant_cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
324                 retval = 0;
325                 break;
326
327         /* mandatory */
328         case OID_GEN_MEDIA_CONNECT_STATUS:
329                 if (rndis_debug > 1)
330                         DBG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __FUNCTION__);
331                 *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
332                                                 .media_state);
333                 retval = 0;
334                 break;
335
336         case OID_GEN_PHYSICAL_MEDIUM:
337                 DBG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __FUNCTION__);
338                 *outbuf = __constant_cpu_to_le32 (0);
339                 retval = 0;
340                 break;
341
342         /* The RNDIS specification is incomplete/wrong.   Some versions
343          * of MS-Windows expect OIDs that aren't specified there.  Other
344          * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
345          */
346         case OID_GEN_MAC_OPTIONS:               /* from WinME */
347                 DBG("%s: OID_GEN_MAC_OPTIONS\n", __FUNCTION__);
348                 *outbuf = __constant_cpu_to_le32(
349                           NDIS_MAC_OPTION_RECEIVE_SERIALIZED
350                         | NDIS_MAC_OPTION_FULL_DUPLEX);
351                 retval = 0;
352                 break;
353
354         /* statistics OIDs (table 4-2) */
355
356         /* mandatory */
357         case OID_GEN_XMIT_OK:
358                 if (rndis_debug > 1)
359                         DBG("%s: OID_GEN_XMIT_OK\n", __FUNCTION__);
360                 if (rndis_per_dev_params [configNr].stats) {
361                         *outbuf = cpu_to_le32 (
362                             rndis_per_dev_params [configNr].stats->tx_packets -
363                             rndis_per_dev_params [configNr].stats->tx_errors -
364                             rndis_per_dev_params [configNr].stats->tx_dropped);
365                         retval = 0;
366                 }
367                 break;
368
369         /* mandatory */
370         case OID_GEN_RCV_OK:
371                 if (rndis_debug > 1)
372                         DBG("%s: OID_GEN_RCV_OK\n", __FUNCTION__);
373                 if (rndis_per_dev_params [configNr].stats) {
374                         *outbuf = cpu_to_le32 (
375                             rndis_per_dev_params [configNr].stats->rx_packets -
376                             rndis_per_dev_params [configNr].stats->rx_errors -
377                             rndis_per_dev_params [configNr].stats->rx_dropped);
378                         retval = 0;
379                 }
380                 break;
381
382         /* mandatory */
383         case OID_GEN_XMIT_ERROR:
384                 if (rndis_debug > 1)
385                         DBG("%s: OID_GEN_XMIT_ERROR\n", __FUNCTION__);
386                 if (rndis_per_dev_params [configNr].stats) {
387                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
388                                         .stats->tx_errors);
389                         retval = 0;
390                 }
391                 break;
392
393         /* mandatory */
394         case OID_GEN_RCV_ERROR:
395                 if (rndis_debug > 1)
396                         DBG("%s: OID_GEN_RCV_ERROR\n", __FUNCTION__);
397                 if (rndis_per_dev_params [configNr].stats) {
398                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
399                                         .stats->rx_errors);
400                         retval = 0;
401                 }
402                 break;
403
404         /* mandatory */
405         case OID_GEN_RCV_NO_BUFFER:
406                 DBG("%s: OID_GEN_RCV_NO_BUFFER\n", __FUNCTION__);
407                 if (rndis_per_dev_params [configNr].stats) {
408                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
409                                         .stats->rx_dropped);
410                         retval = 0;
411                 }
412                 break;
413
414 #ifdef  RNDIS_OPTIONAL_STATS
415         case OID_GEN_DIRECTED_BYTES_XMIT:
416                 DBG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __FUNCTION__);
417                 /*
418                  * Aunt Tilly's size of shoes
419                  * minus antarctica count of penguins
420                  * divided by weight of Alpha Centauri
421                  */
422                 if (rndis_per_dev_params [configNr].stats) {
423                         *outbuf = cpu_to_le32 (
424                                 (rndis_per_dev_params [configNr]
425                                         .stats->tx_packets -
426                                  rndis_per_dev_params [configNr]
427                                          .stats->tx_errors -
428                                  rndis_per_dev_params [configNr]
429                                          .stats->tx_dropped)
430                                 * 123);
431                         retval = 0;
432                 }
433                 break;
434
435         case OID_GEN_DIRECTED_FRAMES_XMIT:
436                 DBG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __FUNCTION__);
437                 /* dito */
438                 if (rndis_per_dev_params [configNr].stats) {
439                         *outbuf = cpu_to_le32 (
440                                 (rndis_per_dev_params [configNr]
441                                         .stats->tx_packets -
442                                  rndis_per_dev_params [configNr]
443                                          .stats->tx_errors -
444                                  rndis_per_dev_params [configNr]
445                                          .stats->tx_dropped)
446                                 / 123);
447                         retval = 0;
448                 }
449                 break;
450
451         case OID_GEN_MULTICAST_BYTES_XMIT:
452                 DBG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __FUNCTION__);
453                 if (rndis_per_dev_params [configNr].stats) {
454                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
455                                         .stats->multicast*1234);
456                         retval = 0;
457                 }
458                 break;
459
460         case OID_GEN_MULTICAST_FRAMES_XMIT:
461                 DBG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __FUNCTION__);
462                 if (rndis_per_dev_params [configNr].stats) {
463                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
464                                         .stats->multicast);
465                         retval = 0;
466                 }
467                 break;
468
469         case OID_GEN_BROADCAST_BYTES_XMIT:
470                 DBG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __FUNCTION__);
471                 if (rndis_per_dev_params [configNr].stats) {
472                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
473                                         .stats->tx_packets/42*255);
474                         retval = 0;
475                 }
476                 break;
477
478         case OID_GEN_BROADCAST_FRAMES_XMIT:
479                 DBG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __FUNCTION__);
480                 if (rndis_per_dev_params [configNr].stats) {
481                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
482                                         .stats->tx_packets/42);
483                         retval = 0;
484                 }
485                 break;
486
487         case OID_GEN_DIRECTED_BYTES_RCV:
488                 DBG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __FUNCTION__);
489                 *outbuf = __constant_cpu_to_le32 (0);
490                 retval = 0;
491                 break;
492
493         case OID_GEN_DIRECTED_FRAMES_RCV:
494                 DBG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __FUNCTION__);
495                 *outbuf = __constant_cpu_to_le32 (0);
496                 retval = 0;
497                 break;
498
499         case OID_GEN_MULTICAST_BYTES_RCV:
500                 DBG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __FUNCTION__);
501                 if (rndis_per_dev_params [configNr].stats) {
502                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
503                                         .stats->multicast * 1111);
504                         retval = 0;
505                 }
506                 break;
507
508         case OID_GEN_MULTICAST_FRAMES_RCV:
509                 DBG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __FUNCTION__);
510                 if (rndis_per_dev_params [configNr].stats) {
511                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
512                                         .stats->multicast);
513                         retval = 0;
514                 }
515                 break;
516
517         case OID_GEN_BROADCAST_BYTES_RCV:
518                 DBG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __FUNCTION__);
519                 if (rndis_per_dev_params [configNr].stats) {
520                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
521                                         .stats->rx_packets/42*255);
522                         retval = 0;
523                 }
524                 break;
525
526         case OID_GEN_BROADCAST_FRAMES_RCV:
527                 DBG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __FUNCTION__);
528                 if (rndis_per_dev_params [configNr].stats) {
529                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
530                                         .stats->rx_packets/42);
531                         retval = 0;
532                 }
533                 break;
534
535         case OID_GEN_RCV_CRC_ERROR:
536                 DBG("%s: OID_GEN_RCV_CRC_ERROR\n", __FUNCTION__);
537                 if (rndis_per_dev_params [configNr].stats) {
538                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
539                                         .stats->rx_crc_errors);
540                         retval = 0;
541                 }
542                 break;
543
544         case OID_GEN_TRANSMIT_QUEUE_LENGTH:
545                 DBG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __FUNCTION__);
546                 *outbuf = __constant_cpu_to_le32 (0);
547                 retval = 0;
548                 break;
549 #endif  /* RNDIS_OPTIONAL_STATS */
550
551         /* ieee802.3 OIDs (table 4-3) */
552
553         /* mandatory */
554         case OID_802_3_PERMANENT_ADDRESS:
555                 DBG("%s: OID_802_3_PERMANENT_ADDRESS\n", __FUNCTION__);
556                 if (rndis_per_dev_params [configNr].dev) {
557                         length = ETH_ALEN;
558                         memcpy (outbuf,
559                                 rndis_per_dev_params [configNr].host_mac,
560                                 length);
561                         retval = 0;
562                 }
563                 break;
564
565         /* mandatory */
566         case OID_802_3_CURRENT_ADDRESS:
567                 DBG("%s: OID_802_3_CURRENT_ADDRESS\n", __FUNCTION__);
568                 if (rndis_per_dev_params [configNr].dev) {
569                         length = ETH_ALEN;
570                         memcpy (outbuf,
571                                 rndis_per_dev_params [configNr].host_mac,
572                                 length);
573                         retval = 0;
574                 }
575                 break;
576
577         /* mandatory */
578         case OID_802_3_MULTICAST_LIST:
579                 DBG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
580                 /* Multicast base address only */
581                 *outbuf = __constant_cpu_to_le32 (0xE0000000);
582                 retval = 0;
583                 break;
584
585         /* mandatory */
586         case OID_802_3_MAXIMUM_LIST_SIZE:
587                 DBG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __FUNCTION__);
588                 /* Multicast base address only */
589                 *outbuf = __constant_cpu_to_le32 (1);
590                 retval = 0;
591                 break;
592
593         case OID_802_3_MAC_OPTIONS:
594                 DBG("%s: OID_802_3_MAC_OPTIONS\n", __FUNCTION__);
595                 break;
596
597         /* ieee802.3 statistics OIDs (table 4-4) */
598
599         /* mandatory */
600         case OID_802_3_RCV_ERROR_ALIGNMENT:
601                 DBG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __FUNCTION__);
602                 if (rndis_per_dev_params [configNr].stats) {
603                         *outbuf = cpu_to_le32 (rndis_per_dev_params [configNr]
604                                         .stats->rx_frame_errors);
605                         retval = 0;
606                 }
607                 break;
608
609         /* mandatory */
610         case OID_802_3_XMIT_ONE_COLLISION:
611                 DBG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __FUNCTION__);
612                 *outbuf = __constant_cpu_to_le32 (0);
613                 retval = 0;
614                 break;
615
616         /* mandatory */
617         case OID_802_3_XMIT_MORE_COLLISIONS:
618                 DBG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __FUNCTION__);
619                 *outbuf = __constant_cpu_to_le32 (0);
620                 retval = 0;
621                 break;
622
623 #ifdef  RNDIS_OPTIONAL_STATS
624         case OID_802_3_XMIT_DEFERRED:
625                 DBG("%s: OID_802_3_XMIT_DEFERRED\n", __FUNCTION__);
626                 /* TODO */
627                 break;
628
629         case OID_802_3_XMIT_MAX_COLLISIONS:
630                 DBG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __FUNCTION__);
631                 /* TODO */
632                 break;
633
634         case OID_802_3_RCV_OVERRUN:
635                 DBG("%s: OID_802_3_RCV_OVERRUN\n", __FUNCTION__);
636                 /* TODO */
637                 break;
638
639         case OID_802_3_XMIT_UNDERRUN:
640                 DBG("%s: OID_802_3_XMIT_UNDERRUN\n", __FUNCTION__);
641                 /* TODO */
642                 break;
643
644         case OID_802_3_XMIT_HEARTBEAT_FAILURE:
645                 DBG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __FUNCTION__);
646                 /* TODO */
647                 break;
648
649         case OID_802_3_XMIT_TIMES_CRS_LOST:
650                 DBG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __FUNCTION__);
651                 /* TODO */
652                 break;
653
654         case OID_802_3_XMIT_LATE_COLLISIONS:
655                 DBG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __FUNCTION__);
656                 /* TODO */
657                 break;
658 #endif  /* RNDIS_OPTIONAL_STATS */
659
660 #ifdef  RNDIS_PM
661         /* power management OIDs (table 4-5) */
662         case OID_PNP_CAPABILITIES:
663                 DBG("%s: OID_PNP_CAPABILITIES\n", __FUNCTION__);
664
665                 /* for now, no wakeup capabilities */
666                 length = sizeof (struct NDIS_PNP_CAPABILITIES);
667                 memset(outbuf, 0, length);
668                 retval = 0;
669                 break;
670         case OID_PNP_QUERY_POWER:
671                 DBG("%s: OID_PNP_QUERY_POWER D%d\n", __FUNCTION__,
672                                 le32_to_cpu(get_unaligned((__le32 *)buf)) - 1);
673                 /* only suspend is a real power state, and
674                  * it can't be entered by OID_PNP_SET_POWER...
675                  */
676                 length = 0;
677                 retval = 0;
678                 break;
679 #endif
680
681         default:
682                 printk (KERN_WARNING "%s: query unknown OID 0x%08X\n",
683                          __FUNCTION__, OID);
684         }
685         if (retval < 0)
686                 length = 0;
687
688         resp->InformationBufferLength = cpu_to_le32 (length);
689         r->length = length + sizeof *resp;
690         resp->MessageLength = cpu_to_le32 (r->length);
691         return retval;
692 }
693
694 static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
695                         rndis_resp_t *r)
696 {
697         rndis_set_cmplt_type            *resp;
698         int                             i, retval = -ENOTSUPP;
699         struct rndis_params             *params;
700
701         if (!r)
702                 return -ENOMEM;
703         resp = (rndis_set_cmplt_type *) r->buf;
704         if (!resp)
705                 return -ENOMEM;
706
707         if (buf_len && rndis_debug > 1) {
708                 DBG("set OID %08x value, len %d:\n", OID, buf_len);
709                 for (i = 0; i < buf_len; i += 16) {
710                         DBG("%03d: %08x %08x %08x %08x\n", i,
711                                 le32_to_cpu(get_unaligned((__le32 *)
712                                         &buf[i])),
713                                 le32_to_cpu(get_unaligned((__le32 *)
714                                         &buf[i + 4])),
715                                 le32_to_cpu(get_unaligned((__le32 *)
716                                         &buf[i + 8])),
717                                 le32_to_cpu(get_unaligned((__le32 *)
718                                         &buf[i + 12])));
719                 }
720         }
721
722         params = &rndis_per_dev_params [configNr];
723         switch (OID) {
724         case OID_GEN_CURRENT_PACKET_FILTER:
725
726                 /* these NDIS_PACKET_TYPE_* bitflags are shared with
727                  * cdc_filter; it's not RNDIS-specific
728                  * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
729                  *      PROMISCUOUS, DIRECTED,
730                  *      MULTICAST, ALL_MULTICAST, BROADCAST
731                  */
732                 *params->filter = (u16) le32_to_cpu(get_unaligned(
733                                 (__le32 *)buf));
734                 DBG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
735                         __FUNCTION__, *params->filter);
736
737                 /* this call has a significant side effect:  it's
738                  * what makes the packet flow start and stop, like
739                  * activating the CDC Ethernet altsetting.
740                  */
741 #ifdef  RNDIS_PM
742 update_linkstate:
743 #endif
744                 retval = 0;
745                 if (*params->filter) {
746                         params->state = RNDIS_DATA_INITIALIZED;
747                         netif_carrier_on(params->dev);
748                         if (netif_running(params->dev))
749                                 netif_wake_queue (params->dev);
750                 } else {
751                         params->state = RNDIS_INITIALIZED;
752                         netif_carrier_off (params->dev);
753                         netif_stop_queue (params->dev);
754                 }
755                 break;
756
757         case OID_802_3_MULTICAST_LIST:
758                 /* I think we can ignore this */
759                 DBG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
760                 retval = 0;
761                 break;
762 #if 0
763         case OID_GEN_RNDIS_CONFIG_PARAMETER:
764                 {
765                 struct rndis_config_parameter   *param;
766                 param = (struct rndis_config_parameter *) buf;
767                 DBG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
768                         __FUNCTION__,
769                         min(cpu_to_le32(param->ParameterNameLength),80),
770                         buf + param->ParameterNameOffset);
771                 retval = 0;
772                 }
773                 break;
774 #endif
775
776 #ifdef  RNDIS_PM
777         case OID_PNP_SET_POWER:
778                 /* The only real power state is USB suspend, and RNDIS requests
779                  * can't enter it; this one isn't really about power.  After
780                  * resuming, Windows forces a reset, and then SET_POWER D0.
781                  * FIXME ... then things go batty; Windows wedges itself.
782                  */
783                 i = le32_to_cpu(get_unaligned((__le32 *)buf));
784                 DBG("%s: OID_PNP_SET_POWER D%d\n", __FUNCTION__, i - 1);
785                 switch (i) {
786                 case NdisDeviceStateD0:
787                         *params->filter = params->saved_filter;
788                         goto update_linkstate;
789                 case NdisDeviceStateD3:
790                 case NdisDeviceStateD2:
791                 case NdisDeviceStateD1:
792                         params->saved_filter = *params->filter;
793                         retval = 0;
794                         break;
795                 }
796                 break;
797
798 #ifdef  RNDIS_WAKEUP
799         // no wakeup support advertised, so wakeup OIDs always fail:
800         //  - OID_PNP_ENABLE_WAKE_UP
801         //  - OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN
802 #endif
803
804 #endif  /* RNDIS_PM */
805
806         default:
807                 printk (KERN_WARNING "%s: set unknown OID 0x%08X, size %d\n",
808                          __FUNCTION__, OID, buf_len);
809         }
810
811         return retval;
812 }
813
814 /*
815  * Response Functions
816  */
817
818 static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
819 {
820         rndis_init_cmplt_type   *resp;
821         rndis_resp_t            *r;
822
823         if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
824
825         r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
826         if (!r)
827                 return -ENOMEM;
828         resp = (rndis_init_cmplt_type *) r->buf;
829
830         resp->MessageType = __constant_cpu_to_le32 (
831                         REMOTE_NDIS_INITIALIZE_CMPLT);
832         resp->MessageLength = __constant_cpu_to_le32 (52);
833         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
834         resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
835         resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
836         resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
837         resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
838         resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
839         resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
840         resp->MaxTransferSize = cpu_to_le32 (
841                   rndis_per_dev_params [configNr].dev->mtu
842                 + sizeof (struct ethhdr)
843                 + sizeof (struct rndis_packet_msg_type)
844                 + 22);
845         resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
846         resp->AFListOffset = __constant_cpu_to_le32 (0);
847         resp->AFListSize = __constant_cpu_to_le32 (0);
848
849         if (rndis_per_dev_params [configNr].ack)
850                 rndis_per_dev_params [configNr].ack (
851                         rndis_per_dev_params [configNr].dev);
852
853         return 0;
854 }
855
856 static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
857 {
858         rndis_query_cmplt_type *resp;
859         rndis_resp_t            *r;
860
861         // DBG("%s: OID = %08X\n", __FUNCTION__, cpu_to_le32(buf->OID));
862         if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
863
864         /*
865          * we need more memory:
866          * gen_ndis_query_resp expects enough space for
867          * rndis_query_cmplt_type followed by data.
868          * oid_supported_list is the largest data reply
869          */
870         r = rndis_add_response (configNr,
871                 sizeof (oid_supported_list) + sizeof(rndis_query_cmplt_type));
872         if (!r)
873                 return -ENOMEM;
874         resp = (rndis_query_cmplt_type *) r->buf;
875
876         resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
877         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
878
879         if (gen_ndis_query_resp (configNr, le32_to_cpu (buf->OID),
880                         le32_to_cpu(buf->InformationBufferOffset)
881                                         + 8 + (u8 *) buf,
882                         le32_to_cpu(buf->InformationBufferLength),
883                         r)) {
884                 /* OID not supported */
885                 resp->Status = __constant_cpu_to_le32 (
886                                 RNDIS_STATUS_NOT_SUPPORTED);
887                 resp->MessageLength = __constant_cpu_to_le32 (sizeof *resp);
888                 resp->InformationBufferLength = __constant_cpu_to_le32 (0);
889                 resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
890         } else
891                 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
892
893         if (rndis_per_dev_params [configNr].ack)
894                 rndis_per_dev_params [configNr].ack (
895                         rndis_per_dev_params [configNr].dev);
896         return 0;
897 }
898
899 static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
900 {
901         u32                     BufLength, BufOffset;
902         rndis_set_cmplt_type    *resp;
903         rndis_resp_t            *r;
904
905         r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
906         if (!r)
907                 return -ENOMEM;
908         resp = (rndis_set_cmplt_type *) r->buf;
909
910         BufLength = le32_to_cpu (buf->InformationBufferLength);
911         BufOffset = le32_to_cpu (buf->InformationBufferOffset);
912
913 #ifdef  VERBOSE
914         DBG("%s: Length: %d\n", __FUNCTION__, BufLength);
915         DBG("%s: Offset: %d\n", __FUNCTION__, BufOffset);
916         DBG("%s: InfoBuffer: ", __FUNCTION__);
917
918         for (i = 0; i < BufLength; i++) {
919                 DBG("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
920         }
921
922         DBG("\n");
923 #endif
924
925         resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
926         resp->MessageLength = __constant_cpu_to_le32 (16);
927         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
928         if (gen_ndis_set_resp (configNr, le32_to_cpu (buf->OID),
929                         ((u8 *) buf) + 8 + BufOffset, BufLength, r))
930                 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
931         else
932                 resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
933
934         if (rndis_per_dev_params [configNr].ack)
935                 rndis_per_dev_params [configNr].ack (
936                         rndis_per_dev_params [configNr].dev);
937
938         return 0;
939 }
940
941 static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
942 {
943         rndis_reset_cmplt_type  *resp;
944         rndis_resp_t            *r;
945
946         r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
947         if (!r)
948                 return -ENOMEM;
949         resp = (rndis_reset_cmplt_type *) r->buf;
950
951         resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
952         resp->MessageLength = __constant_cpu_to_le32 (16);
953         resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
954         /* resent information */
955         resp->AddressingReset = __constant_cpu_to_le32 (1);
956
957         if (rndis_per_dev_params [configNr].ack)
958                 rndis_per_dev_params [configNr].ack (
959                         rndis_per_dev_params [configNr].dev);
960
961         return 0;
962 }
963
964 static int rndis_keepalive_response (int configNr,
965                                 rndis_keepalive_msg_type *buf)
966 {
967         rndis_keepalive_cmplt_type      *resp;
968         rndis_resp_t                    *r;
969
970         /* host "should" check only in RNDIS_DATA_INITIALIZED state */
971
972         r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
973         if (!r)
974                 return -ENOMEM;
975         resp = (rndis_keepalive_cmplt_type *) r->buf;
976
977         resp->MessageType = __constant_cpu_to_le32 (
978                         REMOTE_NDIS_KEEPALIVE_CMPLT);
979         resp->MessageLength = __constant_cpu_to_le32 (16);
980         resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
981         resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
982
983         if (rndis_per_dev_params [configNr].ack)
984                 rndis_per_dev_params [configNr].ack (
985                         rndis_per_dev_params [configNr].dev);
986
987         return 0;
988 }
989
990
991 /*
992  * Device to Host Comunication
993  */
994 static int rndis_indicate_status_msg (int configNr, u32 status)
995 {
996         rndis_indicate_status_msg_type  *resp;
997         rndis_resp_t                    *r;
998
999         if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED)
1000                 return -ENOTSUPP;
1001
1002         r = rndis_add_response (configNr,
1003                                 sizeof (rndis_indicate_status_msg_type));
1004         if (!r)
1005                 return -ENOMEM;
1006         resp = (rndis_indicate_status_msg_type *) r->buf;
1007
1008         resp->MessageType = __constant_cpu_to_le32 (
1009                         REMOTE_NDIS_INDICATE_STATUS_MSG);
1010         resp->MessageLength = __constant_cpu_to_le32 (20);
1011         resp->Status = cpu_to_le32 (status);
1012         resp->StatusBufferLength = __constant_cpu_to_le32 (0);
1013         resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
1014
1015         if (rndis_per_dev_params [configNr].ack)
1016                 rndis_per_dev_params [configNr].ack (
1017                         rndis_per_dev_params [configNr].dev);
1018         return 0;
1019 }
1020
1021 int rndis_signal_connect (int configNr)
1022 {
1023         rndis_per_dev_params [configNr].media_state
1024                         = NDIS_MEDIA_STATE_CONNECTED;
1025         return rndis_indicate_status_msg (configNr,
1026                                           RNDIS_STATUS_MEDIA_CONNECT);
1027 }
1028
1029 int rndis_signal_disconnect (int configNr)
1030 {
1031         rndis_per_dev_params [configNr].media_state
1032                         = NDIS_MEDIA_STATE_DISCONNECTED;
1033         return rndis_indicate_status_msg (configNr,
1034                                           RNDIS_STATUS_MEDIA_DISCONNECT);
1035 }
1036
1037 void rndis_uninit (int configNr)
1038 {
1039         u8 *buf;
1040         u32 length;
1041
1042         if (configNr >= RNDIS_MAX_CONFIGS)
1043                 return;
1044         rndis_per_dev_params [configNr].used = 0;
1045         rndis_per_dev_params [configNr].state = RNDIS_UNINITIALIZED;
1046
1047         /* drain the response queue */
1048         while ((buf = rndis_get_next_response(configNr, &length)))
1049                 rndis_free_response(configNr, buf);
1050 }
1051
1052 void rndis_set_host_mac (int configNr, const u8 *addr)
1053 {
1054         rndis_per_dev_params [configNr].host_mac = addr;
1055 }
1056
1057 /*
1058  * Message Parser
1059  */
1060 int rndis_msg_parser (u8 configNr, u8 *buf)
1061 {
1062         u32 MsgType, MsgLength;
1063         __le32 *tmp;
1064         struct rndis_params             *params;
1065
1066         if (!buf)
1067                 return -ENOMEM;
1068
1069         tmp = (__le32 *) buf;
1070         MsgType   = le32_to_cpu(get_unaligned(tmp++));
1071         MsgLength = le32_to_cpu(get_unaligned(tmp++));
1072
1073         if (configNr >= RNDIS_MAX_CONFIGS)
1074                 return -ENOTSUPP;
1075         params = &rndis_per_dev_params [configNr];
1076
1077         /* NOTE: RNDIS is *EXTREMELY* chatty ... Windows constantly polls for
1078          * rx/tx statistics and link status, in addition to KEEPALIVE traffic
1079          * and normal HC level polling to see if there's any IN traffic.
1080          */
1081
1082         /* For USB: responses may take up to 10 seconds */
1083         switch (MsgType) {
1084         case REMOTE_NDIS_INITIALIZE_MSG:
1085                 DBG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
1086                         __FUNCTION__ );
1087                 params->state = RNDIS_INITIALIZED;
1088                 return  rndis_init_response (configNr,
1089                                         (rndis_init_msg_type *) buf);
1090
1091         case REMOTE_NDIS_HALT_MSG:
1092                 DBG("%s: REMOTE_NDIS_HALT_MSG\n",
1093                         __FUNCTION__ );
1094                 params->state = RNDIS_UNINITIALIZED;
1095                 if (params->dev) {
1096                         netif_carrier_off (params->dev);
1097                         netif_stop_queue (params->dev);
1098                 }
1099                 return 0;
1100
1101         case REMOTE_NDIS_QUERY_MSG:
1102                 return rndis_query_response (configNr,
1103                                         (rndis_query_msg_type *) buf);
1104
1105         case REMOTE_NDIS_SET_MSG:
1106                 return rndis_set_response (configNr,
1107                                         (rndis_set_msg_type *) buf);
1108
1109         case REMOTE_NDIS_RESET_MSG:
1110                 DBG("%s: REMOTE_NDIS_RESET_MSG\n",
1111                         __FUNCTION__ );
1112                 return rndis_reset_response (configNr,
1113                                         (rndis_reset_msg_type *) buf);
1114
1115         case REMOTE_NDIS_KEEPALIVE_MSG:
1116                 /* For USB: host does this every 5 seconds */
1117                 if (rndis_debug > 1)
1118                         DBG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
1119                                 __FUNCTION__ );
1120                 return rndis_keepalive_response (configNr,
1121                                                  (rndis_keepalive_msg_type *)
1122                                                  buf);
1123
1124         default:
1125                 /* At least Windows XP emits some undefined RNDIS messages.
1126                  * In one case those messages seemed to relate to the host
1127                  * suspending itself.
1128                  */
1129                 printk (KERN_WARNING
1130                         "%s: unknown RNDIS message 0x%08X len %d\n",
1131                         __FUNCTION__ , MsgType, MsgLength);
1132                 {
1133                         unsigned i;
1134                         for (i = 0; i < MsgLength; i += 16) {
1135                                 DBG("%03d: "
1136                                         " %02x %02x %02x %02x"
1137                                         " %02x %02x %02x %02x"
1138                                         " %02x %02x %02x %02x"
1139                                         " %02x %02x %02x %02x"
1140                                         "\n",
1141                                         i,
1142                                         buf[i], buf [i+1],
1143                                                 buf[i+2], buf[i+3],
1144                                         buf[i+4], buf [i+5],
1145                                                 buf[i+6], buf[i+7],
1146                                         buf[i+8], buf [i+9],
1147                                                 buf[i+10], buf[i+11],
1148                                         buf[i+12], buf [i+13],
1149                                                 buf[i+14], buf[i+15]);
1150                         }
1151                 }
1152                 break;
1153         }
1154
1155         return -ENOTSUPP;
1156 }
1157
1158 int rndis_register (int (* rndis_control_ack) (struct net_device *))
1159 {
1160         u8 i;
1161
1162         for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1163                 if (!rndis_per_dev_params [i].used) {
1164                         rndis_per_dev_params [i].used = 1;
1165                         rndis_per_dev_params [i].ack = rndis_control_ack;
1166                         DBG("%s: configNr = %d\n", __FUNCTION__, i);
1167                         return i;
1168                 }
1169         }
1170         DBG("failed\n");
1171
1172         return -1;
1173 }
1174
1175 void rndis_deregister (int configNr)
1176 {
1177         DBG("%s: \n", __FUNCTION__ );
1178
1179         if (configNr >= RNDIS_MAX_CONFIGS) return;
1180         rndis_per_dev_params [configNr].used = 0;
1181
1182         return;
1183 }
1184
1185 int rndis_set_param_dev (u8 configNr, struct net_device *dev,
1186                          struct net_device_stats *stats,
1187                          u16 *cdc_filter)
1188 {
1189         DBG("%s:\n", __FUNCTION__ );
1190         if (!dev || !stats) return -1;
1191         if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1192
1193         rndis_per_dev_params [configNr].dev = dev;
1194         rndis_per_dev_params [configNr].stats = stats;
1195         rndis_per_dev_params [configNr].filter = cdc_filter;
1196
1197         return 0;
1198 }
1199
1200 int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
1201 {
1202         DBG("%s:\n", __FUNCTION__ );
1203         if (!vendorDescr) return -1;
1204         if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1205
1206         rndis_per_dev_params [configNr].vendorID = vendorID;
1207         rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
1208
1209         return 0;
1210 }
1211
1212 int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
1213 {
1214         DBG("%s: %u %u\n", __FUNCTION__, medium, speed);
1215         if (configNr >= RNDIS_MAX_CONFIGS) return -1;
1216
1217         rndis_per_dev_params [configNr].medium = medium;
1218         rndis_per_dev_params [configNr].speed = speed;
1219
1220         return 0;
1221 }
1222
1223 void rndis_add_hdr (struct sk_buff *skb)
1224 {
1225         struct rndis_packet_msg_type    *header;
1226
1227         if (!skb)
1228                 return;
1229         header = (void *) skb_push (skb, sizeof *header);
1230         memset (header, 0, sizeof *header);
1231         header->MessageType = __constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG);
1232         header->MessageLength = cpu_to_le32(skb->len);
1233         header->DataOffset = __constant_cpu_to_le32 (36);
1234         header->DataLength = cpu_to_le32(skb->len - sizeof *header);
1235 }
1236
1237 void rndis_free_response (int configNr, u8 *buf)
1238 {
1239         rndis_resp_t            *r;
1240         struct list_head        *act, *tmp;
1241
1242         list_for_each_safe (act, tmp,
1243                         &(rndis_per_dev_params [configNr].resp_queue))
1244         {
1245                 r = list_entry (act, rndis_resp_t, list);
1246                 if (r && r->buf == buf) {
1247                         list_del (&r->list);
1248                         kfree (r);
1249                 }
1250         }
1251 }
1252
1253 u8 *rndis_get_next_response (int configNr, u32 *length)
1254 {
1255         rndis_resp_t            *r;
1256         struct list_head        *act, *tmp;
1257
1258         if (!length) return NULL;
1259
1260         list_for_each_safe (act, tmp,
1261                         &(rndis_per_dev_params [configNr].resp_queue))
1262         {
1263                 r = list_entry (act, rndis_resp_t, list);
1264                 if (!r->send) {
1265                         r->send = 1;
1266                         *length = r->length;
1267                         return r->buf;
1268                 }
1269         }
1270
1271         return NULL;
1272 }
1273
1274 static rndis_resp_t *rndis_add_response (int configNr, u32 length)
1275 {
1276         rndis_resp_t    *r;
1277
1278         /* NOTE:  this gets copied into ether.c USB_BUFSIZ bytes ... */
1279         r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
1280         if (!r) return NULL;
1281
1282         r->buf = (u8 *) (r + 1);
1283         r->length = length;
1284         r->send = 0;
1285
1286         list_add_tail (&r->list,
1287                 &(rndis_per_dev_params [configNr].resp_queue));
1288         return r;
1289 }
1290
1291 int rndis_rm_hdr(struct sk_buff *skb)
1292 {
1293         /* tmp points to a struct rndis_packet_msg_type */
1294         __le32          *tmp = (void *) skb->data;
1295
1296         /* MessageType, MessageLength */
1297         if (__constant_cpu_to_le32(REMOTE_NDIS_PACKET_MSG)
1298                         != get_unaligned(tmp++))
1299                 return -EINVAL;
1300         tmp++;
1301
1302         /* DataOffset, DataLength */
1303         if (!skb_pull(skb, le32_to_cpu(get_unaligned(tmp++))
1304                         + 8 /* offset of DataOffset */))
1305                 return -EOVERFLOW;
1306         skb_trim(skb, le32_to_cpu(get_unaligned(tmp++)));
1307
1308         return 0;
1309 }
1310
1311 #ifdef  CONFIG_USB_GADGET_DEBUG_FILES
1312
1313 static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof,
1314                 void *data)
1315 {
1316         char *out = page;
1317         int len;
1318         rndis_params *param = (rndis_params *) data;
1319
1320         out += snprintf (out, count,
1321                          "Config Nr. %d\n"
1322                          "used      : %s\n"
1323                          "state     : %s\n"
1324                          "medium    : 0x%08X\n"
1325                          "speed     : %d\n"
1326                          "cable     : %s\n"
1327                          "vendor ID : 0x%08X\n"
1328                          "vendor    : %s\n",
1329                          param->confignr, (param->used) ? "y" : "n",
1330                          ({ char *s = "?";
1331                          switch (param->state) {
1332                          case RNDIS_UNINITIALIZED:
1333                                 s = "RNDIS_UNINITIALIZED"; break;
1334                          case RNDIS_INITIALIZED:
1335                                 s = "RNDIS_INITIALIZED"; break;
1336                          case RNDIS_DATA_INITIALIZED:
1337                                 s = "RNDIS_DATA_INITIALIZED"; break;
1338                         }; s; }),
1339                          param->medium,
1340                          (param->media_state) ? 0 : param->speed*100,
1341                          (param->media_state) ? "disconnected" : "connected",
1342                          param->vendorID, param->vendorDescr);
1343
1344         len = out - page;
1345         len -= off;
1346
1347         if (len < count) {
1348                 *eof = 1;
1349                 if (len <= 0)
1350                         return 0;
1351         } else
1352                 len = count;
1353
1354         *start = page + off;
1355         return len;
1356 }
1357
1358 static int rndis_proc_write (struct file *file, const char __user *buffer,
1359                 unsigned long count, void *data)
1360 {
1361         rndis_params *p = data;
1362         u32 speed = 0;
1363         int i, fl_speed = 0;
1364
1365         for (i = 0; i < count; i++) {
1366                 char c;
1367                 if (get_user(c, buffer))
1368                         return -EFAULT;
1369                 switch (c) {
1370                 case '0':
1371                 case '1':
1372                 case '2':
1373                 case '3':
1374                 case '4':
1375                 case '5':
1376                 case '6':
1377                 case '7':
1378                 case '8':
1379                 case '9':
1380                         fl_speed = 1;
1381                         speed = speed*10 + c - '0';
1382                         break;
1383                 case 'C':
1384                 case 'c':
1385                         rndis_signal_connect (p->confignr);
1386                         break;
1387                 case 'D':
1388                 case 'd':
1389                         rndis_signal_disconnect(p->confignr);
1390                         break;
1391                 default:
1392                         if (fl_speed) p->speed = speed;
1393                         else DBG("%c is not valid\n", c);
1394                         break;
1395                 }
1396
1397                 buffer++;
1398         }
1399
1400         return count;
1401 }
1402
1403 #define NAME_TEMPLATE   "driver/rndis-%03d"
1404
1405 static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
1406
1407 #endif  /* CONFIG_USB_GADGET_DEBUG_FILES */
1408
1409
1410 int __devinit rndis_init (void)
1411 {
1412         u8 i;
1413
1414         for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1415 #ifdef  CONFIG_USB_GADGET_DEBUG_FILES
1416                 char name [20];
1417
1418                 sprintf (name, NAME_TEMPLATE, i);
1419                 if (!(rndis_connect_state [i]
1420                                 = create_proc_entry (name, 0660, NULL)))
1421                 {
1422                         DBG("%s :remove entries", __FUNCTION__);
1423                         while (i) {
1424                                 sprintf (name, NAME_TEMPLATE, --i);
1425                                 remove_proc_entry (name, NULL);
1426                         }
1427                         DBG("\n");
1428                         return -EIO;
1429                 }
1430
1431                 rndis_connect_state [i]->write_proc = rndis_proc_write;
1432                 rndis_connect_state [i]->read_proc = rndis_proc_read;
1433                 rndis_connect_state [i]->data = (void *)
1434                                 (rndis_per_dev_params + i);
1435 #endif
1436                 rndis_per_dev_params [i].confignr = i;
1437                 rndis_per_dev_params [i].used = 0;
1438                 rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
1439                 rndis_per_dev_params [i].media_state
1440                                 = NDIS_MEDIA_STATE_DISCONNECTED;
1441                 INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
1442         }
1443
1444         return 0;
1445 }
1446
1447 void rndis_exit (void)
1448 {
1449 #ifdef  CONFIG_USB_GADGET_DEBUG_FILES
1450         u8 i;
1451         char name [20];
1452
1453         for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
1454                 sprintf (name, NAME_TEMPLATE, i);
1455                 remove_proc_entry (name, NULL);
1456         }
1457 #endif
1458 }
1459