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