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