1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/idr.h>
29#include <linux/list.h>
30#include <linux/proc_fs.h>
31#include <linux/slab.h>
32#include <linux/seq_file.h>
33#include <linux/netdevice.h>
34
35#include <asm/io.h>
36#include <asm/byteorder.h>
37#include <asm/unaligned.h>
38
39#include "u_rndis.h"
40
41#undef VERBOSE_DEBUG
42
43#include "rndis.h"
44
45
46
47
48
49
50
51
52
53#if 0
54static int rndis_debug = 0;
55module_param (rndis_debug, int, 0);
56MODULE_PARM_DESC (rndis_debug, "enable debugging");
57#else
58#define rndis_debug 0
59#endif
60
61#ifdef CONFIG_USB_GADGET_DEBUG_FILES
62
63#define NAME_TEMPLATE "driver/rndis-%03d"
64
65#endif
66
67static DEFINE_IDA(rndis_ida);
68
69
70static const __le32 rndis_driver_version = cpu_to_le32(1);
71
72
73static rndis_resp_t *rndis_add_response(struct rndis_params *params,
74 u32 length);
75
76#ifdef CONFIG_USB_GADGET_DEBUG_FILES
77
78static const struct file_operations rndis_proc_fops;
79
80#endif
81
82
83static const u32 oid_supported_list[] =
84{
85
86 RNDIS_OID_GEN_SUPPORTED_LIST,
87 RNDIS_OID_GEN_HARDWARE_STATUS,
88 RNDIS_OID_GEN_MEDIA_SUPPORTED,
89 RNDIS_OID_GEN_MEDIA_IN_USE,
90 RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE,
91 RNDIS_OID_GEN_LINK_SPEED,
92 RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE,
93 RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE,
94 RNDIS_OID_GEN_VENDOR_ID,
95 RNDIS_OID_GEN_VENDOR_DESCRIPTION,
96 RNDIS_OID_GEN_VENDOR_DRIVER_VERSION,
97 RNDIS_OID_GEN_CURRENT_PACKET_FILTER,
98 RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE,
99 RNDIS_OID_GEN_MEDIA_CONNECT_STATUS,
100 RNDIS_OID_GEN_PHYSICAL_MEDIUM,
101
102
103 RNDIS_OID_GEN_XMIT_OK,
104 RNDIS_OID_GEN_RCV_OK,
105 RNDIS_OID_GEN_XMIT_ERROR,
106 RNDIS_OID_GEN_RCV_ERROR,
107 RNDIS_OID_GEN_RCV_NO_BUFFER,
108#ifdef RNDIS_OPTIONAL_STATS
109 RNDIS_OID_GEN_DIRECTED_BYTES_XMIT,
110 RNDIS_OID_GEN_DIRECTED_FRAMES_XMIT,
111 RNDIS_OID_GEN_MULTICAST_BYTES_XMIT,
112 RNDIS_OID_GEN_MULTICAST_FRAMES_XMIT,
113 RNDIS_OID_GEN_BROADCAST_BYTES_XMIT,
114 RNDIS_OID_GEN_BROADCAST_FRAMES_XMIT,
115 RNDIS_OID_GEN_DIRECTED_BYTES_RCV,
116 RNDIS_OID_GEN_DIRECTED_FRAMES_RCV,
117 RNDIS_OID_GEN_MULTICAST_BYTES_RCV,
118 RNDIS_OID_GEN_MULTICAST_FRAMES_RCV,
119 RNDIS_OID_GEN_BROADCAST_BYTES_RCV,
120 RNDIS_OID_GEN_BROADCAST_FRAMES_RCV,
121 RNDIS_OID_GEN_RCV_CRC_ERROR,
122 RNDIS_OID_GEN_TRANSMIT_QUEUE_LENGTH,
123#endif
124
125
126
127 RNDIS_OID_802_3_PERMANENT_ADDRESS,
128 RNDIS_OID_802_3_CURRENT_ADDRESS,
129 RNDIS_OID_802_3_MULTICAST_LIST,
130 RNDIS_OID_802_3_MAC_OPTIONS,
131 RNDIS_OID_802_3_MAXIMUM_LIST_SIZE,
132
133
134 RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT,
135 RNDIS_OID_802_3_XMIT_ONE_COLLISION,
136 RNDIS_OID_802_3_XMIT_MORE_COLLISIONS,
137#ifdef RNDIS_OPTIONAL_STATS
138 RNDIS_OID_802_3_XMIT_DEFERRED,
139 RNDIS_OID_802_3_XMIT_MAX_COLLISIONS,
140 RNDIS_OID_802_3_RCV_OVERRUN,
141 RNDIS_OID_802_3_XMIT_UNDERRUN,
142 RNDIS_OID_802_3_XMIT_HEARTBEAT_FAILURE,
143 RNDIS_OID_802_3_XMIT_TIMES_CRS_LOST,
144 RNDIS_OID_802_3_XMIT_LATE_COLLISIONS,
145#endif
146
147#ifdef RNDIS_PM
148
149
150
151
152
153
154
155
156
157
158 OID_PNP_CAPABILITIES,
159 OID_PNP_QUERY_POWER,
160 OID_PNP_SET_POWER,
161
162#ifdef RNDIS_WAKEUP
163
164 OID_PNP_ENABLE_WAKE_UP,
165 OID_PNP_ADD_WAKE_UP_PATTERN,
166 OID_PNP_REMOVE_WAKE_UP_PATTERN,
167#endif
168#endif
169};
170
171
172
173static int gen_ndis_query_resp(struct rndis_params *params, u32 OID, u8 *buf,
174 unsigned buf_len, rndis_resp_t *r)
175{
176 int retval = -ENOTSUPP;
177 u32 length = 4;
178 __le32 *outbuf;
179 int i, count;
180 rndis_query_cmplt_type *resp;
181 struct net_device *net;
182 struct rtnl_link_stats64 temp;
183 const struct rtnl_link_stats64 *stats;
184
185 if (!r) return -ENOMEM;
186 resp = (rndis_query_cmplt_type *)r->buf;
187
188 if (!resp) return -ENOMEM;
189
190 if (buf_len && rndis_debug > 1) {
191 pr_debug("query OID %08x value, len %d:\n", OID, buf_len);
192 for (i = 0; i < buf_len; i += 16) {
193 pr_debug("%03d: %08x %08x %08x %08x\n", i,
194 get_unaligned_le32(&buf[i]),
195 get_unaligned_le32(&buf[i + 4]),
196 get_unaligned_le32(&buf[i + 8]),
197 get_unaligned_le32(&buf[i + 12]));
198 }
199 }
200
201
202 outbuf = (__le32 *)&resp[1];
203 resp->InformationBufferOffset = cpu_to_le32(16);
204
205 net = params->dev;
206 stats = dev_get_stats(net, &temp);
207
208 switch (OID) {
209
210
211
212
213 case RNDIS_OID_GEN_SUPPORTED_LIST:
214 pr_debug("%s: RNDIS_OID_GEN_SUPPORTED_LIST\n", __func__);
215 length = sizeof(oid_supported_list);
216 count = length / sizeof(u32);
217 for (i = 0; i < count; i++)
218 outbuf[i] = cpu_to_le32(oid_supported_list[i]);
219 retval = 0;
220 break;
221
222
223 case RNDIS_OID_GEN_HARDWARE_STATUS:
224 pr_debug("%s: RNDIS_OID_GEN_HARDWARE_STATUS\n", __func__);
225
226
227
228
229
230
231 *outbuf = cpu_to_le32(0);
232 retval = 0;
233 break;
234
235
236 case RNDIS_OID_GEN_MEDIA_SUPPORTED:
237 pr_debug("%s: RNDIS_OID_GEN_MEDIA_SUPPORTED\n", __func__);
238 *outbuf = cpu_to_le32(params->medium);
239 retval = 0;
240 break;
241
242
243 case RNDIS_OID_GEN_MEDIA_IN_USE:
244 pr_debug("%s: RNDIS_OID_GEN_MEDIA_IN_USE\n", __func__);
245
246 *outbuf = cpu_to_le32(params->medium);
247 retval = 0;
248 break;
249
250
251 case RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE:
252 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE\n", __func__);
253 if (params->dev) {
254 *outbuf = cpu_to_le32(params->dev->mtu);
255 retval = 0;
256 }
257 break;
258
259
260 case RNDIS_OID_GEN_LINK_SPEED:
261 if (rndis_debug > 1)
262 pr_debug("%s: RNDIS_OID_GEN_LINK_SPEED\n", __func__);
263 if (params->media_state == RNDIS_MEDIA_STATE_DISCONNECTED)
264 *outbuf = cpu_to_le32(0);
265 else
266 *outbuf = cpu_to_le32(params->speed);
267 retval = 0;
268 break;
269
270
271 case RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE:
272 pr_debug("%s: RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE\n", __func__);
273 if (params->dev) {
274 *outbuf = cpu_to_le32(params->dev->mtu);
275 retval = 0;
276 }
277 break;
278
279
280 case RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE:
281 pr_debug("%s: RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE\n", __func__);
282 if (params->dev) {
283 *outbuf = cpu_to_le32(params->dev->mtu);
284 retval = 0;
285 }
286 break;
287
288
289 case RNDIS_OID_GEN_VENDOR_ID:
290 pr_debug("%s: RNDIS_OID_GEN_VENDOR_ID\n", __func__);
291 *outbuf = cpu_to_le32(params->vendorID);
292 retval = 0;
293 break;
294
295
296 case RNDIS_OID_GEN_VENDOR_DESCRIPTION:
297 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DESCRIPTION\n", __func__);
298 if (params->vendorDescr) {
299 length = strlen(params->vendorDescr);
300 memcpy(outbuf, params->vendorDescr, length);
301 } else {
302 outbuf[0] = 0;
303 }
304 retval = 0;
305 break;
306
307 case RNDIS_OID_GEN_VENDOR_DRIVER_VERSION:
308 pr_debug("%s: RNDIS_OID_GEN_VENDOR_DRIVER_VERSION\n", __func__);
309
310 *outbuf = rndis_driver_version;
311 retval = 0;
312 break;
313
314
315 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
316 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER\n", __func__);
317 *outbuf = cpu_to_le32(*params->filter);
318 retval = 0;
319 break;
320
321
322 case RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE:
323 pr_debug("%s: RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE\n", __func__);
324 *outbuf = cpu_to_le32(RNDIS_MAX_TOTAL_SIZE);
325 retval = 0;
326 break;
327
328
329 case RNDIS_OID_GEN_MEDIA_CONNECT_STATUS:
330 if (rndis_debug > 1)
331 pr_debug("%s: RNDIS_OID_GEN_MEDIA_CONNECT_STATUS\n", __func__);
332 *outbuf = cpu_to_le32(params->media_state);
333 retval = 0;
334 break;
335
336 case RNDIS_OID_GEN_PHYSICAL_MEDIUM:
337 pr_debug("%s: RNDIS_OID_GEN_PHYSICAL_MEDIUM\n", __func__);
338 *outbuf = cpu_to_le32(0);
339 retval = 0;
340 break;
341
342
343
344
345
346 case RNDIS_OID_GEN_MAC_OPTIONS:
347 pr_debug("%s: RNDIS_OID_GEN_MAC_OPTIONS\n", __func__);
348 *outbuf = cpu_to_le32(
349 RNDIS_MAC_OPTION_RECEIVE_SERIALIZED
350 | RNDIS_MAC_OPTION_FULL_DUPLEX);
351 retval = 0;
352 break;
353
354
355
356
357 case RNDIS_OID_GEN_XMIT_OK:
358 if (rndis_debug > 1)
359 pr_debug("%s: RNDIS_OID_GEN_XMIT_OK\n", __func__);
360 if (stats) {
361 *outbuf = cpu_to_le32(stats->tx_packets
362 - stats->tx_errors - stats->tx_dropped);
363 retval = 0;
364 }
365 break;
366
367
368 case RNDIS_OID_GEN_RCV_OK:
369 if (rndis_debug > 1)
370 pr_debug("%s: RNDIS_OID_GEN_RCV_OK\n", __func__);
371 if (stats) {
372 *outbuf = cpu_to_le32(stats->rx_packets
373 - stats->rx_errors - stats->rx_dropped);
374 retval = 0;
375 }
376 break;
377
378
379 case RNDIS_OID_GEN_XMIT_ERROR:
380 if (rndis_debug > 1)
381 pr_debug("%s: RNDIS_OID_GEN_XMIT_ERROR\n", __func__);
382 if (stats) {
383 *outbuf = cpu_to_le32(stats->tx_errors);
384 retval = 0;
385 }
386 break;
387
388
389 case RNDIS_OID_GEN_RCV_ERROR:
390 if (rndis_debug > 1)
391 pr_debug("%s: RNDIS_OID_GEN_RCV_ERROR\n", __func__);
392 if (stats) {
393 *outbuf = cpu_to_le32(stats->rx_errors);
394 retval = 0;
395 }
396 break;
397
398
399 case RNDIS_OID_GEN_RCV_NO_BUFFER:
400 pr_debug("%s: RNDIS_OID_GEN_RCV_NO_BUFFER\n", __func__);
401 if (stats) {
402 *outbuf = cpu_to_le32(stats->rx_dropped);
403 retval = 0;
404 }
405 break;
406
407
408
409
410 case RNDIS_OID_802_3_PERMANENT_ADDRESS:
411 pr_debug("%s: RNDIS_OID_802_3_PERMANENT_ADDRESS\n", __func__);
412 if (params->dev) {
413 length = ETH_ALEN;
414 memcpy(outbuf, params->host_mac, length);
415 retval = 0;
416 }
417 break;
418
419
420 case RNDIS_OID_802_3_CURRENT_ADDRESS:
421 pr_debug("%s: RNDIS_OID_802_3_CURRENT_ADDRESS\n", __func__);
422 if (params->dev) {
423 length = ETH_ALEN;
424 memcpy(outbuf, params->host_mac, length);
425 retval = 0;
426 }
427 break;
428
429
430 case RNDIS_OID_802_3_MULTICAST_LIST:
431 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
432
433 *outbuf = cpu_to_le32(0xE0000000);
434 retval = 0;
435 break;
436
437
438 case RNDIS_OID_802_3_MAXIMUM_LIST_SIZE:
439 pr_debug("%s: RNDIS_OID_802_3_MAXIMUM_LIST_SIZE\n", __func__);
440
441 *outbuf = cpu_to_le32(1);
442 retval = 0;
443 break;
444
445 case RNDIS_OID_802_3_MAC_OPTIONS:
446 pr_debug("%s: RNDIS_OID_802_3_MAC_OPTIONS\n", __func__);
447 *outbuf = cpu_to_le32(0);
448 retval = 0;
449 break;
450
451
452
453
454 case RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT:
455 pr_debug("%s: RNDIS_OID_802_3_RCV_ERROR_ALIGNMENT\n", __func__);
456 if (stats) {
457 *outbuf = cpu_to_le32(stats->rx_frame_errors);
458 retval = 0;
459 }
460 break;
461
462
463 case RNDIS_OID_802_3_XMIT_ONE_COLLISION:
464 pr_debug("%s: RNDIS_OID_802_3_XMIT_ONE_COLLISION\n", __func__);
465 *outbuf = cpu_to_le32(0);
466 retval = 0;
467 break;
468
469
470 case RNDIS_OID_802_3_XMIT_MORE_COLLISIONS:
471 pr_debug("%s: RNDIS_OID_802_3_XMIT_MORE_COLLISIONS\n", __func__);
472 *outbuf = cpu_to_le32(0);
473 retval = 0;
474 break;
475
476 default:
477 pr_warning("%s: query unknown OID 0x%08X\n",
478 __func__, OID);
479 }
480 if (retval < 0)
481 length = 0;
482
483 resp->InformationBufferLength = cpu_to_le32(length);
484 r->length = length + sizeof(*resp);
485 resp->MessageLength = cpu_to_le32(r->length);
486 return retval;
487}
488
489static int gen_ndis_set_resp(struct rndis_params *params, u32 OID,
490 u8 *buf, u32 buf_len, rndis_resp_t *r)
491{
492 rndis_set_cmplt_type *resp;
493 int i, retval = -ENOTSUPP;
494
495 if (!r)
496 return -ENOMEM;
497 resp = (rndis_set_cmplt_type *)r->buf;
498 if (!resp)
499 return -ENOMEM;
500
501 if (buf_len && rndis_debug > 1) {
502 pr_debug("set OID %08x value, len %d:\n", OID, buf_len);
503 for (i = 0; i < buf_len; i += 16) {
504 pr_debug("%03d: %08x %08x %08x %08x\n", i,
505 get_unaligned_le32(&buf[i]),
506 get_unaligned_le32(&buf[i + 4]),
507 get_unaligned_le32(&buf[i + 8]),
508 get_unaligned_le32(&buf[i + 12]));
509 }
510 }
511
512 switch (OID) {
513 case RNDIS_OID_GEN_CURRENT_PACKET_FILTER:
514
515
516
517
518
519
520
521 *params->filter = (u16)get_unaligned_le32(buf);
522 pr_debug("%s: RNDIS_OID_GEN_CURRENT_PACKET_FILTER %08x\n",
523 __func__, *params->filter);
524
525
526
527
528
529 retval = 0;
530 if (*params->filter) {
531 params->state = RNDIS_DATA_INITIALIZED;
532 netif_carrier_on(params->dev);
533 if (netif_running(params->dev))
534 netif_wake_queue(params->dev);
535 } else {
536 params->state = RNDIS_INITIALIZED;
537 netif_carrier_off(params->dev);
538 netif_stop_queue(params->dev);
539 }
540 break;
541
542 case RNDIS_OID_802_3_MULTICAST_LIST:
543
544 pr_debug("%s: RNDIS_OID_802_3_MULTICAST_LIST\n", __func__);
545 retval = 0;
546 break;
547
548 default:
549 pr_warning("%s: set unknown OID 0x%08X, size %d\n",
550 __func__, OID, buf_len);
551 }
552
553 return retval;
554}
555
556
557
558
559
560static int rndis_init_response(struct rndis_params *params,
561 rndis_init_msg_type *buf)
562{
563 rndis_init_cmplt_type *resp;
564 rndis_resp_t *r;
565
566 if (!params->dev)
567 return -ENOTSUPP;
568
569 r = rndis_add_response(params, sizeof(rndis_init_cmplt_type));
570 if (!r)
571 return -ENOMEM;
572 resp = (rndis_init_cmplt_type *)r->buf;
573
574 resp->MessageType = cpu_to_le32(RNDIS_MSG_INIT_C);
575 resp->MessageLength = cpu_to_le32(52);
576 resp->RequestID = buf->RequestID;
577 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
578 resp->MajorVersion = cpu_to_le32(RNDIS_MAJOR_VERSION);
579 resp->MinorVersion = cpu_to_le32(RNDIS_MINOR_VERSION);
580 resp->DeviceFlags = cpu_to_le32(RNDIS_DF_CONNECTIONLESS);
581 resp->Medium = cpu_to_le32(RNDIS_MEDIUM_802_3);
582 resp->MaxPacketsPerTransfer = cpu_to_le32(1);
583 resp->MaxTransferSize = cpu_to_le32(
584 params->dev->mtu
585 + sizeof(struct ethhdr)
586 + sizeof(struct rndis_packet_msg_type)
587 + 22);
588 resp->PacketAlignmentFactor = cpu_to_le32(0);
589 resp->AFListOffset = cpu_to_le32(0);
590 resp->AFListSize = cpu_to_le32(0);
591
592 params->resp_avail(params->v);
593 return 0;
594}
595
596static int rndis_query_response(struct rndis_params *params,
597 rndis_query_msg_type *buf)
598{
599 rndis_query_cmplt_type *resp;
600 rndis_resp_t *r;
601
602
603 if (!params->dev)
604 return -ENOTSUPP;
605
606
607
608
609
610
611
612 r = rndis_add_response(params,
613 sizeof(oid_supported_list) + sizeof(rndis_query_cmplt_type));
614 if (!r)
615 return -ENOMEM;
616 resp = (rndis_query_cmplt_type *)r->buf;
617
618 resp->MessageType = cpu_to_le32(RNDIS_MSG_QUERY_C);
619 resp->RequestID = buf->RequestID;
620
621 if (gen_ndis_query_resp(params, le32_to_cpu(buf->OID),
622 le32_to_cpu(buf->InformationBufferOffset)
623 + 8 + (u8 *)buf,
624 le32_to_cpu(buf->InformationBufferLength),
625 r)) {
626
627 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
628 resp->MessageLength = cpu_to_le32(sizeof *resp);
629 resp->InformationBufferLength = cpu_to_le32(0);
630 resp->InformationBufferOffset = cpu_to_le32(0);
631 } else
632 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
633
634 params->resp_avail(params->v);
635 return 0;
636}
637
638static int rndis_set_response(struct rndis_params *params,
639 rndis_set_msg_type *buf)
640{
641 u32 BufLength, BufOffset;
642 rndis_set_cmplt_type *resp;
643 rndis_resp_t *r;
644
645 r = rndis_add_response(params, sizeof(rndis_set_cmplt_type));
646 if (!r)
647 return -ENOMEM;
648 resp = (rndis_set_cmplt_type *)r->buf;
649
650 BufLength = le32_to_cpu(buf->InformationBufferLength);
651 BufOffset = le32_to_cpu(buf->InformationBufferOffset);
652
653#ifdef VERBOSE_DEBUG
654 pr_debug("%s: Length: %d\n", __func__, BufLength);
655 pr_debug("%s: Offset: %d\n", __func__, BufOffset);
656 pr_debug("%s: InfoBuffer: ", __func__);
657
658 for (i = 0; i < BufLength; i++) {
659 pr_debug("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
660 }
661
662 pr_debug("\n");
663#endif
664
665 resp->MessageType = cpu_to_le32(RNDIS_MSG_SET_C);
666 resp->MessageLength = cpu_to_le32(16);
667 resp->RequestID = buf->RequestID;
668 if (gen_ndis_set_resp(params, le32_to_cpu(buf->OID),
669 ((u8 *)buf) + 8 + BufOffset, BufLength, r))
670 resp->Status = cpu_to_le32(RNDIS_STATUS_NOT_SUPPORTED);
671 else
672 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
673
674 params->resp_avail(params->v);
675 return 0;
676}
677
678static int rndis_reset_response(struct rndis_params *params,
679 rndis_reset_msg_type *buf)
680{
681 rndis_reset_cmplt_type *resp;
682 rndis_resp_t *r;
683 u8 *xbuf;
684 u32 length;
685
686
687 while ((xbuf = rndis_get_next_response(params, &length)))
688 rndis_free_response(params, xbuf);
689
690 r = rndis_add_response(params, sizeof(rndis_reset_cmplt_type));
691 if (!r)
692 return -ENOMEM;
693 resp = (rndis_reset_cmplt_type *)r->buf;
694
695 resp->MessageType = cpu_to_le32(RNDIS_MSG_RESET_C);
696 resp->MessageLength = cpu_to_le32(16);
697 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
698
699 resp->AddressingReset = cpu_to_le32(1);
700
701 params->resp_avail(params->v);
702 return 0;
703}
704
705static int rndis_keepalive_response(struct rndis_params *params,
706 rndis_keepalive_msg_type *buf)
707{
708 rndis_keepalive_cmplt_type *resp;
709 rndis_resp_t *r;
710
711
712
713 r = rndis_add_response(params, sizeof(rndis_keepalive_cmplt_type));
714 if (!r)
715 return -ENOMEM;
716 resp = (rndis_keepalive_cmplt_type *)r->buf;
717
718 resp->MessageType = cpu_to_le32(RNDIS_MSG_KEEPALIVE_C);
719 resp->MessageLength = cpu_to_le32(16);
720 resp->RequestID = buf->RequestID;
721 resp->Status = cpu_to_le32(RNDIS_STATUS_SUCCESS);
722
723 params->resp_avail(params->v);
724 return 0;
725}
726
727
728
729
730
731static int rndis_indicate_status_msg(struct rndis_params *params, u32 status)
732{
733 rndis_indicate_status_msg_type *resp;
734 rndis_resp_t *r;
735
736 if (params->state == RNDIS_UNINITIALIZED)
737 return -ENOTSUPP;
738
739 r = rndis_add_response(params, sizeof(rndis_indicate_status_msg_type));
740 if (!r)
741 return -ENOMEM;
742 resp = (rndis_indicate_status_msg_type *)r->buf;
743
744 resp->MessageType = cpu_to_le32(RNDIS_MSG_INDICATE);
745 resp->MessageLength = cpu_to_le32(20);
746 resp->Status = cpu_to_le32(status);
747 resp->StatusBufferLength = cpu_to_le32(0);
748 resp->StatusBufferOffset = cpu_to_le32(0);
749
750 params->resp_avail(params->v);
751 return 0;
752}
753
754int rndis_signal_connect(struct rndis_params *params)
755{
756 params->media_state = RNDIS_MEDIA_STATE_CONNECTED;
757 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_CONNECT);
758}
759EXPORT_SYMBOL_GPL(rndis_signal_connect);
760
761int rndis_signal_disconnect(struct rndis_params *params)
762{
763 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
764 return rndis_indicate_status_msg(params, RNDIS_STATUS_MEDIA_DISCONNECT);
765}
766EXPORT_SYMBOL_GPL(rndis_signal_disconnect);
767
768void rndis_uninit(struct rndis_params *params)
769{
770 u8 *buf;
771 u32 length;
772
773 if (!params)
774 return;
775 params->state = RNDIS_UNINITIALIZED;
776
777
778 while ((buf = rndis_get_next_response(params, &length)))
779 rndis_free_response(params, buf);
780}
781EXPORT_SYMBOL_GPL(rndis_uninit);
782
783void rndis_set_host_mac(struct rndis_params *params, const u8 *addr)
784{
785 params->host_mac = addr;
786}
787EXPORT_SYMBOL_GPL(rndis_set_host_mac);
788
789
790
791
792int rndis_msg_parser(struct rndis_params *params, u8 *buf)
793{
794 u32 MsgType, MsgLength;
795 __le32 *tmp;
796
797 if (!buf)
798 return -ENOMEM;
799
800 tmp = (__le32 *)buf;
801 MsgType = get_unaligned_le32(tmp++);
802 MsgLength = get_unaligned_le32(tmp++);
803
804 if (!params)
805 return -ENOTSUPP;
806
807
808
809
810
811
812
813 switch (MsgType) {
814 case RNDIS_MSG_INIT:
815 pr_debug("%s: RNDIS_MSG_INIT\n",
816 __func__);
817 params->state = RNDIS_INITIALIZED;
818 return rndis_init_response(params, (rndis_init_msg_type *)buf);
819
820 case RNDIS_MSG_HALT:
821 pr_debug("%s: RNDIS_MSG_HALT\n",
822 __func__);
823 params->state = RNDIS_UNINITIALIZED;
824 if (params->dev) {
825 netif_carrier_off(params->dev);
826 netif_stop_queue(params->dev);
827 }
828 return 0;
829
830 case RNDIS_MSG_QUERY:
831 return rndis_query_response(params,
832 (rndis_query_msg_type *)buf);
833
834 case RNDIS_MSG_SET:
835 return rndis_set_response(params, (rndis_set_msg_type *)buf);
836
837 case RNDIS_MSG_RESET:
838 pr_debug("%s: RNDIS_MSG_RESET\n",
839 __func__);
840 return rndis_reset_response(params,
841 (rndis_reset_msg_type *)buf);
842
843 case RNDIS_MSG_KEEPALIVE:
844
845 if (rndis_debug > 1)
846 pr_debug("%s: RNDIS_MSG_KEEPALIVE\n",
847 __func__);
848 return rndis_keepalive_response(params,
849 (rndis_keepalive_msg_type *)
850 buf);
851
852 default:
853
854
855
856
857 pr_warning("%s: unknown RNDIS message 0x%08X len %d\n",
858 __func__, MsgType, MsgLength);
859 print_hex_dump_bytes(__func__, DUMP_PREFIX_OFFSET,
860 buf, MsgLength);
861 break;
862 }
863
864 return -ENOTSUPP;
865}
866EXPORT_SYMBOL_GPL(rndis_msg_parser);
867
868static inline int rndis_get_nr(void)
869{
870 return ida_simple_get(&rndis_ida, 0, 0, GFP_KERNEL);
871}
872
873static inline void rndis_put_nr(int nr)
874{
875 ida_simple_remove(&rndis_ida, nr);
876}
877
878struct rndis_params *rndis_register(void (*resp_avail)(void *v), void *v)
879{
880 struct rndis_params *params;
881 int i;
882
883 if (!resp_avail)
884 return ERR_PTR(-EINVAL);
885
886 i = rndis_get_nr();
887 if (i < 0) {
888 pr_debug("failed\n");
889
890 return ERR_PTR(-ENODEV);
891 }
892
893 params = kzalloc(sizeof(*params), GFP_KERNEL);
894 if (!params) {
895 rndis_put_nr(i);
896
897 return ERR_PTR(-ENOMEM);
898 }
899
900#ifdef CONFIG_USB_GADGET_DEBUG_FILES
901 {
902 struct proc_dir_entry *proc_entry;
903 char name[20];
904
905 sprintf(name, NAME_TEMPLATE, i);
906 proc_entry = proc_create_data(name, 0660, NULL,
907 &rndis_proc_fops, params);
908 if (!proc_entry) {
909 kfree(params);
910 rndis_put_nr(i);
911
912 return ERR_PTR(-EIO);
913 }
914 }
915#endif
916
917 params->confignr = i;
918 params->used = 1;
919 params->state = RNDIS_UNINITIALIZED;
920 params->media_state = RNDIS_MEDIA_STATE_DISCONNECTED;
921 params->resp_avail = resp_avail;
922 params->v = v;
923 INIT_LIST_HEAD(¶ms->resp_queue);
924 pr_debug("%s: configNr = %d\n", __func__, i);
925
926 return params;
927}
928EXPORT_SYMBOL_GPL(rndis_register);
929
930void rndis_deregister(struct rndis_params *params)
931{
932 int i;
933
934 pr_debug("%s:\n", __func__);
935
936 if (!params)
937 return;
938
939 i = params->confignr;
940
941#ifdef CONFIG_USB_GADGET_DEBUG_FILES
942 {
943 char name[20];
944
945 sprintf(name, NAME_TEMPLATE, i);
946 remove_proc_entry(name, NULL);
947 }
948#endif
949
950 kfree(params);
951 rndis_put_nr(i);
952}
953EXPORT_SYMBOL_GPL(rndis_deregister);
954int rndis_set_param_dev(struct rndis_params *params, struct net_device *dev,
955 u16 *cdc_filter)
956{
957 pr_debug("%s:\n", __func__);
958 if (!dev)
959 return -EINVAL;
960 if (!params)
961 return -1;
962
963 params->dev = dev;
964 params->filter = cdc_filter;
965
966 return 0;
967}
968EXPORT_SYMBOL_GPL(rndis_set_param_dev);
969
970int rndis_set_param_vendor(struct rndis_params *params, u32 vendorID,
971 const char *vendorDescr)
972{
973 pr_debug("%s:\n", __func__);
974 if (!vendorDescr) return -1;
975 if (!params)
976 return -1;
977
978 params->vendorID = vendorID;
979 params->vendorDescr = vendorDescr;
980
981 return 0;
982}
983EXPORT_SYMBOL_GPL(rndis_set_param_vendor);
984
985int rndis_set_param_medium(struct rndis_params *params, u32 medium, u32 speed)
986{
987 pr_debug("%s: %u %u\n", __func__, medium, speed);
988 if (!params)
989 return -1;
990
991 params->medium = medium;
992 params->speed = speed;
993
994 return 0;
995}
996EXPORT_SYMBOL_GPL(rndis_set_param_medium);
997
998void rndis_add_hdr(struct sk_buff *skb)
999{
1000 struct rndis_packet_msg_type *header;
1001
1002 if (!skb)
1003 return;
1004 header = (void *)skb_push(skb, sizeof(*header));
1005 memset(header, 0, sizeof *header);
1006 header->MessageType = cpu_to_le32(RNDIS_MSG_PACKET);
1007 header->MessageLength = cpu_to_le32(skb->len);
1008 header->DataOffset = cpu_to_le32(36);
1009 header->DataLength = cpu_to_le32(skb->len - sizeof(*header));
1010}
1011EXPORT_SYMBOL_GPL(rndis_add_hdr);
1012
1013void rndis_free_response(struct rndis_params *params, u8 *buf)
1014{
1015 rndis_resp_t *r, *n;
1016
1017 list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) {
1018 if (r->buf == buf) {
1019 list_del(&r->list);
1020 kfree(r);
1021 }
1022 }
1023}
1024EXPORT_SYMBOL_GPL(rndis_free_response);
1025
1026u8 *rndis_get_next_response(struct rndis_params *params, u32 *length)
1027{
1028 rndis_resp_t *r, *n;
1029
1030 if (!length) return NULL;
1031
1032 list_for_each_entry_safe(r, n, ¶ms->resp_queue, list) {
1033 if (!r->send) {
1034 r->send = 1;
1035 *length = r->length;
1036 return r->buf;
1037 }
1038 }
1039
1040 return NULL;
1041}
1042EXPORT_SYMBOL_GPL(rndis_get_next_response);
1043
1044static rndis_resp_t *rndis_add_response(struct rndis_params *params, u32 length)
1045{
1046 rndis_resp_t *r;
1047
1048
1049 r = kmalloc(sizeof(rndis_resp_t) + length, GFP_ATOMIC);
1050 if (!r) return NULL;
1051
1052 r->buf = (u8 *)(r + 1);
1053 r->length = length;
1054 r->send = 0;
1055
1056 list_add_tail(&r->list, ¶ms->resp_queue);
1057 return r;
1058}
1059
1060int rndis_rm_hdr(struct gether *port,
1061 struct sk_buff *skb,
1062 struct sk_buff_head *list)
1063{
1064
1065 __le32 *tmp = (void *)skb->data;
1066
1067
1068 if (cpu_to_le32(RNDIS_MSG_PACKET)
1069 != get_unaligned(tmp++)) {
1070 dev_kfree_skb_any(skb);
1071 return -EINVAL;
1072 }
1073 tmp++;
1074
1075
1076 if (!skb_pull(skb, get_unaligned_le32(tmp++) + 8)) {
1077 dev_kfree_skb_any(skb);
1078 return -EOVERFLOW;
1079 }
1080 skb_trim(skb, get_unaligned_le32(tmp++));
1081
1082 skb_queue_tail(list, skb);
1083 return 0;
1084}
1085EXPORT_SYMBOL_GPL(rndis_rm_hdr);
1086
1087#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1088
1089static int rndis_proc_show(struct seq_file *m, void *v)
1090{
1091 rndis_params *param = m->private;
1092
1093 seq_printf(m,
1094 "Config Nr. %d\n"
1095 "used : %s\n"
1096 "state : %s\n"
1097 "medium : 0x%08X\n"
1098 "speed : %d\n"
1099 "cable : %s\n"
1100 "vendor ID : 0x%08X\n"
1101 "vendor : %s\n",
1102 param->confignr, (param->used) ? "y" : "n",
1103 ({ char *s = "?";
1104 switch (param->state) {
1105 case RNDIS_UNINITIALIZED:
1106 s = "RNDIS_UNINITIALIZED"; break;
1107 case RNDIS_INITIALIZED:
1108 s = "RNDIS_INITIALIZED"; break;
1109 case RNDIS_DATA_INITIALIZED:
1110 s = "RNDIS_DATA_INITIALIZED"; break;
1111 } s; }),
1112 param->medium,
1113 (param->media_state) ? 0 : param->speed*100,
1114 (param->media_state) ? "disconnected" : "connected",
1115 param->vendorID, param->vendorDescr);
1116 return 0;
1117}
1118
1119static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
1120 size_t count, loff_t *ppos)
1121{
1122 rndis_params *p = PDE_DATA(file_inode(file));
1123 u32 speed = 0;
1124 int i, fl_speed = 0;
1125
1126 for (i = 0; i < count; i++) {
1127 char c;
1128 if (get_user(c, buffer))
1129 return -EFAULT;
1130 switch (c) {
1131 case '0':
1132 case '1':
1133 case '2':
1134 case '3':
1135 case '4':
1136 case '5':
1137 case '6':
1138 case '7':
1139 case '8':
1140 case '9':
1141 fl_speed = 1;
1142 speed = speed * 10 + c - '0';
1143 break;
1144 case 'C':
1145 case 'c':
1146 rndis_signal_connect(p);
1147 break;
1148 case 'D':
1149 case 'd':
1150 rndis_signal_disconnect(p);
1151 break;
1152 default:
1153 if (fl_speed) p->speed = speed;
1154 else pr_debug("%c is not valid\n", c);
1155 break;
1156 }
1157
1158 buffer++;
1159 }
1160
1161 return count;
1162}
1163
1164static int rndis_proc_open(struct inode *inode, struct file *file)
1165{
1166 return single_open(file, rndis_proc_show, PDE_DATA(inode));
1167}
1168
1169static const struct file_operations rndis_proc_fops = {
1170 .owner = THIS_MODULE,
1171 .open = rndis_proc_open,
1172 .read = seq_read,
1173 .llseek = seq_lseek,
1174 .release = single_release,
1175 .write = rndis_proc_write,
1176};
1177
1178#define NAME_TEMPLATE "driver/rndis-%03d"
1179
1180#endif
1181