1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/kernel.h>
26#include <linux/device.h>
27#include <linux/etherdevice.h>
28
29#include <asm/atomic.h>
30
31#include "u_ether.h"
32#include "rndis.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76struct rndis_ep_descs {
77 struct usb_endpoint_descriptor *in;
78 struct usb_endpoint_descriptor *out;
79 struct usb_endpoint_descriptor *notify;
80};
81
82struct f_rndis {
83 struct gether port;
84 u8 ctrl_id, data_id;
85 u8 ethaddr[ETH_ALEN];
86 int config;
87
88 struct rndis_ep_descs fs;
89 struct rndis_ep_descs hs;
90
91 struct usb_ep *notify;
92 struct usb_endpoint_descriptor *notify_desc;
93 struct usb_request *notify_req;
94 atomic_t notify_count;
95};
96
97static inline struct f_rndis *func_to_rndis(struct usb_function *f)
98{
99 return container_of(f, struct f_rndis, port.func);
100}
101
102
103static unsigned int bitrate(struct usb_gadget *g)
104{
105 if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
106 return 13 * 512 * 8 * 1000 * 8;
107 else
108 return 19 * 64 * 1 * 1000 * 8;
109}
110
111
112
113
114
115
116#define LOG2_STATUS_INTERVAL_MSEC 5
117#define STATUS_BYTECOUNT 8
118
119
120
121
122static struct usb_interface_descriptor rndis_control_intf __initdata = {
123 .bLength = sizeof rndis_control_intf,
124 .bDescriptorType = USB_DT_INTERFACE,
125
126
127
128 .bNumEndpoints = 1,
129 .bInterfaceClass = USB_CLASS_COMM,
130 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
131 .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
132
133};
134
135static struct usb_cdc_header_desc header_desc __initdata = {
136 .bLength = sizeof header_desc,
137 .bDescriptorType = USB_DT_CS_INTERFACE,
138 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
139
140 .bcdCDC = cpu_to_le16(0x0110),
141};
142
143static struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor __initdata = {
144 .bLength = sizeof call_mgmt_descriptor,
145 .bDescriptorType = USB_DT_CS_INTERFACE,
146 .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
147
148 .bmCapabilities = 0x00,
149 .bDataInterface = 0x01,
150};
151
152static struct usb_cdc_acm_descriptor acm_descriptor __initdata = {
153 .bLength = sizeof acm_descriptor,
154 .bDescriptorType = USB_DT_CS_INTERFACE,
155 .bDescriptorSubType = USB_CDC_ACM_TYPE,
156
157 .bmCapabilities = 0x00,
158};
159
160static struct usb_cdc_union_desc rndis_union_desc __initdata = {
161 .bLength = sizeof(rndis_union_desc),
162 .bDescriptorType = USB_DT_CS_INTERFACE,
163 .bDescriptorSubType = USB_CDC_UNION_TYPE,
164
165
166};
167
168
169
170static struct usb_interface_descriptor rndis_data_intf __initdata = {
171 .bLength = sizeof rndis_data_intf,
172 .bDescriptorType = USB_DT_INTERFACE,
173
174
175 .bNumEndpoints = 2,
176 .bInterfaceClass = USB_CLASS_CDC_DATA,
177 .bInterfaceSubClass = 0,
178 .bInterfaceProtocol = 0,
179
180};
181
182
183
184static struct usb_endpoint_descriptor fs_notify_desc __initdata = {
185 .bLength = USB_DT_ENDPOINT_SIZE,
186 .bDescriptorType = USB_DT_ENDPOINT,
187
188 .bEndpointAddress = USB_DIR_IN,
189 .bmAttributes = USB_ENDPOINT_XFER_INT,
190 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
191 .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
192};
193
194static struct usb_endpoint_descriptor fs_in_desc __initdata = {
195 .bLength = USB_DT_ENDPOINT_SIZE,
196 .bDescriptorType = USB_DT_ENDPOINT,
197
198 .bEndpointAddress = USB_DIR_IN,
199 .bmAttributes = USB_ENDPOINT_XFER_BULK,
200};
201
202static struct usb_endpoint_descriptor fs_out_desc __initdata = {
203 .bLength = USB_DT_ENDPOINT_SIZE,
204 .bDescriptorType = USB_DT_ENDPOINT,
205
206 .bEndpointAddress = USB_DIR_OUT,
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
208};
209
210static struct usb_descriptor_header *eth_fs_function[] __initdata = {
211
212 (struct usb_descriptor_header *) &rndis_control_intf,
213 (struct usb_descriptor_header *) &header_desc,
214 (struct usb_descriptor_header *) &call_mgmt_descriptor,
215 (struct usb_descriptor_header *) &acm_descriptor,
216 (struct usb_descriptor_header *) &rndis_union_desc,
217 (struct usb_descriptor_header *) &fs_notify_desc,
218
219 (struct usb_descriptor_header *) &rndis_data_intf,
220 (struct usb_descriptor_header *) &fs_in_desc,
221 (struct usb_descriptor_header *) &fs_out_desc,
222 NULL,
223};
224
225
226
227static struct usb_endpoint_descriptor hs_notify_desc __initdata = {
228 .bLength = USB_DT_ENDPOINT_SIZE,
229 .bDescriptorType = USB_DT_ENDPOINT,
230
231 .bEndpointAddress = USB_DIR_IN,
232 .bmAttributes = USB_ENDPOINT_XFER_INT,
233 .wMaxPacketSize = cpu_to_le16(STATUS_BYTECOUNT),
234 .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
235};
236static struct usb_endpoint_descriptor hs_in_desc __initdata = {
237 .bLength = USB_DT_ENDPOINT_SIZE,
238 .bDescriptorType = USB_DT_ENDPOINT,
239
240 .bEndpointAddress = USB_DIR_IN,
241 .bmAttributes = USB_ENDPOINT_XFER_BULK,
242 .wMaxPacketSize = cpu_to_le16(512),
243};
244
245static struct usb_endpoint_descriptor hs_out_desc __initdata = {
246 .bLength = USB_DT_ENDPOINT_SIZE,
247 .bDescriptorType = USB_DT_ENDPOINT,
248
249 .bEndpointAddress = USB_DIR_OUT,
250 .bmAttributes = USB_ENDPOINT_XFER_BULK,
251 .wMaxPacketSize = cpu_to_le16(512),
252};
253
254static struct usb_descriptor_header *eth_hs_function[] __initdata = {
255
256 (struct usb_descriptor_header *) &rndis_control_intf,
257 (struct usb_descriptor_header *) &header_desc,
258 (struct usb_descriptor_header *) &call_mgmt_descriptor,
259 (struct usb_descriptor_header *) &acm_descriptor,
260 (struct usb_descriptor_header *) &rndis_union_desc,
261 (struct usb_descriptor_header *) &hs_notify_desc,
262
263 (struct usb_descriptor_header *) &rndis_data_intf,
264 (struct usb_descriptor_header *) &hs_in_desc,
265 (struct usb_descriptor_header *) &hs_out_desc,
266 NULL,
267};
268
269
270
271static struct usb_string rndis_string_defs[] = {
272 [0].s = "RNDIS Communications Control",
273 [1].s = "RNDIS Ethernet Data",
274 { }
275};
276
277static struct usb_gadget_strings rndis_string_table = {
278 .language = 0x0409,
279 .strings = rndis_string_defs,
280};
281
282static struct usb_gadget_strings *rndis_strings[] = {
283 &rndis_string_table,
284 NULL,
285};
286
287
288
289static struct sk_buff *rndis_add_header(struct gether *port,
290 struct sk_buff *skb)
291{
292 struct sk_buff *skb2;
293
294 skb2 = skb_realloc_headroom(skb, sizeof(struct rndis_packet_msg_type));
295 if (skb2)
296 rndis_add_hdr(skb2);
297
298 dev_kfree_skb_any(skb);
299 return skb2;
300}
301
302static void rndis_response_available(void *_rndis)
303{
304 struct f_rndis *rndis = _rndis;
305 struct usb_request *req = rndis->notify_req;
306 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
307 __le32 *data = req->buf;
308 int status;
309
310 if (atomic_inc_return(&rndis->notify_count) != 1)
311 return;
312
313
314
315
316
317
318 data[0] = cpu_to_le32(1);
319 data[1] = cpu_to_le32(0);
320
321 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
322 if (status) {
323 atomic_dec(&rndis->notify_count);
324 DBG(cdev, "notify/0 --> %d\n", status);
325 }
326}
327
328static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
329{
330 struct f_rndis *rndis = req->context;
331 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
332 int status = req->status;
333
334
335
336
337
338 switch (status) {
339 case -ECONNRESET:
340 case -ESHUTDOWN:
341
342 atomic_set(&rndis->notify_count, 0);
343 break;
344 default:
345 DBG(cdev, "RNDIS %s response error %d, %d/%d\n",
346 ep->name, status,
347 req->actual, req->length);
348
349 case 0:
350 if (ep != rndis->notify)
351 break;
352
353
354
355
356 if (atomic_dec_and_test(&rndis->notify_count))
357 break;
358 status = usb_ep_queue(rndis->notify, req, GFP_ATOMIC);
359 if (status) {
360 atomic_dec(&rndis->notify_count);
361 DBG(cdev, "notify/1 --> %d\n", status);
362 }
363 break;
364 }
365}
366
367static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
368{
369 struct f_rndis *rndis = req->context;
370 struct usb_composite_dev *cdev = rndis->port.func.config->cdev;
371 int status;
372
373
374
375 status = rndis_msg_parser(rndis->config, (u8 *) req->buf);
376 if (status < 0)
377 ERROR(cdev, "RNDIS command error %d, %d/%d\n",
378 status, req->actual, req->length);
379
380}
381
382static int
383rndis_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
384{
385 struct f_rndis *rndis = func_to_rndis(f);
386 struct usb_composite_dev *cdev = f->config->cdev;
387 struct usb_request *req = cdev->req;
388 int value = -EOPNOTSUPP;
389 u16 w_index = le16_to_cpu(ctrl->wIndex);
390 u16 w_value = le16_to_cpu(ctrl->wValue);
391 u16 w_length = le16_to_cpu(ctrl->wLength);
392
393
394
395
396 switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
397
398
399
400
401 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
402 | USB_CDC_SEND_ENCAPSULATED_COMMAND:
403 if (w_length > req->length || w_value
404 || w_index != rndis->ctrl_id)
405 goto invalid;
406
407 value = w_length;
408 req->complete = rndis_command_complete;
409 req->context = rndis;
410
411 break;
412
413 case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
414 | USB_CDC_GET_ENCAPSULATED_RESPONSE:
415 if (w_value || w_index != rndis->ctrl_id)
416 goto invalid;
417 else {
418 u8 *buf;
419 u32 n;
420
421
422 buf = rndis_get_next_response(rndis->config, &n);
423 if (buf) {
424 memcpy(req->buf, buf, n);
425 req->complete = rndis_response_complete;
426 rndis_free_response(rndis->config, buf);
427 value = n;
428 }
429
430 }
431 break;
432
433 default:
434invalid:
435 VDBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
436 ctrl->bRequestType, ctrl->bRequest,
437 w_value, w_index, w_length);
438 }
439
440
441 if (value >= 0) {
442 DBG(cdev, "rndis req%02x.%02x v%04x i%04x l%d\n",
443 ctrl->bRequestType, ctrl->bRequest,
444 w_value, w_index, w_length);
445 req->zero = (value < w_length);
446 req->length = value;
447 value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
448 if (value < 0)
449 ERROR(cdev, "rndis response on err %d\n", value);
450 }
451
452
453 return value;
454}
455
456
457static int rndis_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
458{
459 struct f_rndis *rndis = func_to_rndis(f);
460 struct usb_composite_dev *cdev = f->config->cdev;
461
462
463
464 if (intf == rndis->ctrl_id) {
465 if (rndis->notify->driver_data) {
466 VDBG(cdev, "reset rndis control %d\n", intf);
467 usb_ep_disable(rndis->notify);
468 } else {
469 VDBG(cdev, "init rndis ctrl %d\n", intf);
470 rndis->notify_desc = ep_choose(cdev->gadget,
471 rndis->hs.notify,
472 rndis->fs.notify);
473 }
474 usb_ep_enable(rndis->notify, rndis->notify_desc);
475 rndis->notify->driver_data = rndis;
476
477 } else if (intf == rndis->data_id) {
478 struct net_device *net;
479
480 if (rndis->port.in_ep->driver_data) {
481 DBG(cdev, "reset rndis\n");
482 gether_disconnect(&rndis->port);
483 }
484
485 if (!rndis->port.in) {
486 DBG(cdev, "init rndis\n");
487 rndis->port.in = ep_choose(cdev->gadget,
488 rndis->hs.in, rndis->fs.in);
489 rndis->port.out = ep_choose(cdev->gadget,
490 rndis->hs.out, rndis->fs.out);
491 }
492
493
494 rndis->port.is_zlp_ok = false;
495
496
497
498
499
500
501
502
503
504
505
506
507
508 rndis->port.cdc_filter = 0;
509
510 DBG(cdev, "RNDIS RX/TX early activation ... \n");
511 net = gether_connect(&rndis->port);
512 if (IS_ERR(net))
513 return PTR_ERR(net);
514
515 rndis_set_param_dev(rndis->config, net,
516 &rndis->port.cdc_filter);
517 } else
518 goto fail;
519
520 return 0;
521fail:
522 return -EINVAL;
523}
524
525static void rndis_disable(struct usb_function *f)
526{
527 struct f_rndis *rndis = func_to_rndis(f);
528 struct usb_composite_dev *cdev = f->config->cdev;
529
530 if (!rndis->notify->driver_data)
531 return;
532
533 DBG(cdev, "rndis deactivated\n");
534
535 rndis_uninit(rndis->config);
536 gether_disconnect(&rndis->port);
537
538 usb_ep_disable(rndis->notify);
539 rndis->notify->driver_data = NULL;
540}
541
542
543
544
545
546
547
548
549
550
551static void rndis_open(struct gether *geth)
552{
553 struct f_rndis *rndis = func_to_rndis(&geth->func);
554 struct usb_composite_dev *cdev = geth->func.config->cdev;
555
556 DBG(cdev, "%s\n", __func__);
557
558 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3,
559 bitrate(cdev->gadget) / 100);
560 rndis_signal_connect(rndis->config);
561}
562
563static void rndis_close(struct gether *geth)
564{
565 struct f_rndis *rndis = func_to_rndis(&geth->func);
566
567 DBG(geth->func.config->cdev, "%s\n", __func__);
568
569 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
570 rndis_signal_disconnect(rndis->config);
571}
572
573
574
575
576
577static int __init
578rndis_bind(struct usb_configuration *c, struct usb_function *f)
579{
580 struct usb_composite_dev *cdev = c->cdev;
581 struct f_rndis *rndis = func_to_rndis(f);
582 int status;
583 struct usb_ep *ep;
584
585
586 status = usb_interface_id(c, f);
587 if (status < 0)
588 goto fail;
589 rndis->ctrl_id = status;
590
591 rndis_control_intf.bInterfaceNumber = status;
592 rndis_union_desc.bMasterInterface0 = status;
593
594 status = usb_interface_id(c, f);
595 if (status < 0)
596 goto fail;
597 rndis->data_id = status;
598
599 rndis_data_intf.bInterfaceNumber = status;
600 rndis_union_desc.bSlaveInterface0 = status;
601
602 status = -ENODEV;
603
604
605 ep = usb_ep_autoconfig(cdev->gadget, &fs_in_desc);
606 if (!ep)
607 goto fail;
608 rndis->port.in_ep = ep;
609 ep->driver_data = cdev;
610
611 ep = usb_ep_autoconfig(cdev->gadget, &fs_out_desc);
612 if (!ep)
613 goto fail;
614 rndis->port.out_ep = ep;
615 ep->driver_data = cdev;
616
617
618
619
620
621 ep = usb_ep_autoconfig(cdev->gadget, &fs_notify_desc);
622 if (!ep)
623 goto fail;
624 rndis->notify = ep;
625 ep->driver_data = cdev;
626
627 status = -ENOMEM;
628
629
630 rndis->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
631 if (!rndis->notify_req)
632 goto fail;
633 rndis->notify_req->buf = kmalloc(STATUS_BYTECOUNT, GFP_KERNEL);
634 if (!rndis->notify_req->buf)
635 goto fail;
636 rndis->notify_req->length = STATUS_BYTECOUNT;
637 rndis->notify_req->context = rndis;
638 rndis->notify_req->complete = rndis_response_complete;
639
640
641 f->descriptors = usb_copy_descriptors(eth_fs_function);
642 if (!f->descriptors)
643 goto fail;
644
645 rndis->fs.in = usb_find_endpoint(eth_fs_function,
646 f->descriptors, &fs_in_desc);
647 rndis->fs.out = usb_find_endpoint(eth_fs_function,
648 f->descriptors, &fs_out_desc);
649 rndis->fs.notify = usb_find_endpoint(eth_fs_function,
650 f->descriptors, &fs_notify_desc);
651
652
653
654
655
656 if (gadget_is_dualspeed(c->cdev->gadget)) {
657 hs_in_desc.bEndpointAddress =
658 fs_in_desc.bEndpointAddress;
659 hs_out_desc.bEndpointAddress =
660 fs_out_desc.bEndpointAddress;
661 hs_notify_desc.bEndpointAddress =
662 fs_notify_desc.bEndpointAddress;
663
664
665 f->hs_descriptors = usb_copy_descriptors(eth_hs_function);
666
667 if (!f->hs_descriptors)
668 goto fail;
669
670 rndis->hs.in = usb_find_endpoint(eth_hs_function,
671 f->hs_descriptors, &hs_in_desc);
672 rndis->hs.out = usb_find_endpoint(eth_hs_function,
673 f->hs_descriptors, &hs_out_desc);
674 rndis->hs.notify = usb_find_endpoint(eth_hs_function,
675 f->hs_descriptors, &hs_notify_desc);
676 }
677
678 rndis->port.open = rndis_open;
679 rndis->port.close = rndis_close;
680
681 status = rndis_register(rndis_response_available, rndis);
682 if (status < 0)
683 goto fail;
684 rndis->config = status;
685
686 rndis_set_param_medium(rndis->config, NDIS_MEDIUM_802_3, 0);
687 rndis_set_host_mac(rndis->config, rndis->ethaddr);
688
689#if 0
690
691 if (rndis_set_param_vendor(rndis->config, vendorID,
692 manufacturer))
693 goto fail0;
694#endif
695
696
697
698
699
700
701 DBG(cdev, "RNDIS: %s speed IN/%s OUT/%s NOTIFY/%s\n",
702 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
703 rndis->port.in_ep->name, rndis->port.out_ep->name,
704 rndis->notify->name);
705 return 0;
706
707fail:
708 if (gadget_is_dualspeed(c->cdev->gadget) && f->hs_descriptors)
709 usb_free_descriptors(f->hs_descriptors);
710 if (f->descriptors)
711 usb_free_descriptors(f->descriptors);
712
713 if (rndis->notify_req) {
714 kfree(rndis->notify_req->buf);
715 usb_ep_free_request(rndis->notify, rndis->notify_req);
716 }
717
718
719 if (rndis->notify)
720 rndis->notify->driver_data = NULL;
721 if (rndis->port.out)
722 rndis->port.out_ep->driver_data = NULL;
723 if (rndis->port.in)
724 rndis->port.in_ep->driver_data = NULL;
725
726 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
727
728 return status;
729}
730
731static void
732rndis_unbind(struct usb_configuration *c, struct usb_function *f)
733{
734 struct f_rndis *rndis = func_to_rndis(f);
735
736 rndis_deregister(rndis->config);
737 rndis_exit();
738
739 if (gadget_is_dualspeed(c->cdev->gadget))
740 usb_free_descriptors(f->hs_descriptors);
741 usb_free_descriptors(f->descriptors);
742
743 kfree(rndis->notify_req->buf);
744 usb_ep_free_request(rndis->notify, rndis->notify_req);
745
746 kfree(rndis);
747}
748
749
750static inline bool can_support_rndis(struct usb_configuration *c)
751{
752
753 if (gadget_is_sa1100(c->cdev->gadget))
754 return false;
755
756
757 return true;
758}
759
760
761
762
763
764
765
766
767
768
769
770
771
772int __init rndis_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
773{
774 struct f_rndis *rndis;
775 int status;
776
777 if (!can_support_rndis(c) || !ethaddr)
778 return -EINVAL;
779
780
781 if (rndis_string_defs[0].id == 0) {
782
783
784 status = rndis_init();
785 if (status < 0)
786 return status;
787
788
789 status = usb_string_id(c->cdev);
790 if (status < 0)
791 return status;
792 rndis_string_defs[0].id = status;
793 rndis_control_intf.iInterface = status;
794
795
796 status = usb_string_id(c->cdev);
797 if (status < 0)
798 return status;
799 rndis_string_defs[1].id = status;
800 rndis_data_intf.iInterface = status;
801 }
802
803
804 status = -ENOMEM;
805 rndis = kzalloc(sizeof *rndis, GFP_KERNEL);
806 if (!rndis)
807 goto fail;
808
809 memcpy(rndis->ethaddr, ethaddr, ETH_ALEN);
810
811
812 rndis->port.cdc_filter = 0;
813
814
815 rndis->port.header_len = sizeof(struct rndis_packet_msg_type);
816 rndis->port.wrap = rndis_add_header;
817 rndis->port.unwrap = rndis_rm_hdr;
818
819 rndis->port.func.name = "rndis";
820 rndis->port.func.strings = rndis_strings;
821
822 rndis->port.func.bind = rndis_bind;
823 rndis->port.func.unbind = rndis_unbind;
824 rndis->port.func.set_alt = rndis_set_alt;
825 rndis->port.func.setup = rndis_setup;
826 rndis->port.func.disable = rndis_disable;
827
828 status = usb_add_function(c, &rndis->port.func);
829 if (status) {
830 kfree(rndis);
831fail:
832 rndis_exit();
833 }
834 return status;
835}
836