1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/sched.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/module.h>
29#include <linux/ethtool.h>
30#include <asm/uaccess.h>
31
32#define DEBUG
33#include <linux/usb.h>
34
35#include "CDCEther.h"
36
37#define SHORT_DRIVER_DESC "CDC Ethernet Class"
38#define DRIVER_VERSION "0.98.6"
39
40static const char driver_name[] = "CDCEther";
41static const char *version = __FILE__ ": " DRIVER_VERSION " 7 Jan 2002 Brad Hards and another";
42
43static struct usb_device_id CDCEther_ids[] = {
44 { USB_INTERFACE_INFO(USB_CLASS_COMM, 6, 0) },
45 { }
46};
47
48
49
50
51
52
53
54
55
56static int multicast_filter_limit = 32767;
57
58
59
60
61
62static void read_bulk_callback( struct urb *urb )
63{
64 ether_dev_t *ether_dev = urb->context;
65 struct net_device *net;
66 int count = urb->actual_length, res;
67 struct sk_buff *skb;
68
69 switch ( urb->status ) {
70 case USB_ST_NOERROR:
71 break;
72 case USB_ST_URB_KILLED:
73 return;
74 default:
75 dbg("rx status %d", urb->status);
76 }
77
78
79 if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
80 dbg("BULK IN callback but driver is not active!");
81 return;
82 }
83
84 net = ether_dev->net;
85 if ( !netif_device_present(net) ) {
86
87 return;
88 }
89
90 if ( ether_dev->flags & CDC_ETHER_RX_BUSY ) {
91
92 ether_dev->stats.rx_errors++;
93 dbg("ether_dev Rx busy");
94 return;
95 }
96
97
98 ether_dev->flags |= CDC_ETHER_RX_BUSY;
99
100 switch ( urb->status ) {
101 case USB_ST_NOERROR:
102 break;
103 case USB_ST_NORESPONSE:
104 dbg( "no repsonse in BULK IN" );
105 ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
106 break;
107 default:
108 dbg( "%s: RX status %d", net->name, urb->status );
109 goto goon;
110 }
111
112
113 if ( !count ) {
114
115 goto goon;
116 }
117
118
119 if ( !(skb = dev_alloc_skb(count)) ) {
120
121 goto goon;
122 }
123
124
125 skb->dev = net;
126
127
128 eth_copy_and_sum(skb, ether_dev->rx_buff, count, 0);
129
130
131 skb_put(skb, count);
132
133 skb->protocol = eth_type_trans(skb, net);
134
135
136 netif_rx(skb);
137
138
139 ether_dev->stats.rx_packets++;
140 ether_dev->stats.rx_bytes += count;
141
142goon:
143
144 FILL_BULK_URB( ðer_dev->rx_urb, ether_dev->usb,
145 usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
146 ether_dev->rx_buff, ether_dev->wMaxSegmentSize,
147 read_bulk_callback, ether_dev );
148
149
150
151 if ( (res = usb_submit_urb(ðer_dev->rx_urb)) ) {
152 warn("%s failed submit rx_urb %d", __FUNCTION__, res);
153 }
154
155
156 ether_dev->flags &= ~CDC_ETHER_RX_BUSY;
157}
158
159static void write_bulk_callback( struct urb *urb )
160{
161 ether_dev_t *ether_dev = urb->context;
162
163
164 if ( !ether_dev || !(ether_dev->flags & CDC_ETHER_RUNNING) ) {
165
166 err( "write_bulk_callback: device not running" );
167 return;
168 }
169
170
171 if ( !netif_device_present(ether_dev->net) ) {
172
173 err( "write_bulk_callback: net device not present" );
174 return;
175 }
176
177
178 if ( urb->status ) {
179 dbg("%s: TX status %d", ether_dev->net->name, urb->status);
180 }
181
182
183
184 ether_dev->net->trans_start = jiffies;
185 netif_wake_queue( ether_dev->net );
186
187}
188
189#if 0
190static void setpktfilter_done( struct urb *urb )
191{
192 ether_dev_t *ether_dev = urb->context;
193 struct net_device *net;
194
195 if ( !ether_dev )
196 return;
197 dbg("got ctrl callback for setting packet filter");
198 switch ( urb->status ) {
199 case USB_ST_NOERROR:
200 break;
201 case USB_ST_URB_KILLED:
202 return;
203 default:
204 dbg("intr status %d", urb->status);
205 }
206}
207#endif
208
209static void intr_callback( struct urb *urb )
210{
211 ether_dev_t *ether_dev = urb->context;
212 struct net_device *net;
213 struct usb_ctrlrequest *event;
214#define bNotification bRequest
215
216 if ( !ether_dev )
217 return;
218 net = ether_dev->net;
219 switch ( urb->status ) {
220 case USB_ST_NOERROR:
221 break;
222 case USB_ST_URB_KILLED:
223 default:
224 dbg("%s intr status %d", net->name, urb->status);
225 return;
226 }
227
228 event = urb->transfer_buffer;
229 if (event->bRequestType != 0xA1)
230 dbg ("%s unknown event type %x", net->name,
231 event->bRequestType);
232 else switch (event->bNotification) {
233 case 0x00:
234 dbg ("%s network %s", net->name,
235 event->wValue ? "connect" : "disconnect");
236 if (event->wValue)
237 netif_carrier_on (net);
238 else
239 netif_carrier_off (net);
240 break;
241 case 0x2A:
242 dbg ("%s speed change", net->name);
243
244 break;
245 case 0x01:
246 default:
247 err ("%s illegal notification %02x", net->name,
248 event->bNotification);
249 }
250}
251
252
253
254
255
256static inline int enable_net_traffic( ether_dev_t *ether_dev )
257{
258 struct usb_device *usb = ether_dev->usb;
259
260
261
262
263 if (usb_set_interface( usb,
264 ether_dev->data_bInterfaceNumber,
265 ether_dev->data_bAlternateSetting_with_traffic ) ) {
266 err("usb_set_interface() failed" );
267 err("Attempted to set interface %d", ether_dev->data_bInterfaceNumber);
268 err("To alternate setting %d", ether_dev->data_bAlternateSetting_with_traffic);
269 return -1;
270 }
271 return 0;
272}
273
274static inline void disable_net_traffic( ether_dev_t *ether_dev )
275{
276
277
278
279 if (ether_dev->data_interface_altset_num_without_traffic >= 0 ) {
280 if (usb_set_interface( ether_dev->usb,
281 ether_dev->data_bInterfaceNumber,
282 ether_dev->data_bAlternateSetting_without_traffic ) ) {
283 err("usb_set_interface() failed");
284 }
285 } else {
286
287 warn("No way to disable net traffic");
288 }
289}
290
291
292
293
294
295static void CDCEther_tx_timeout( struct net_device *net )
296{
297 ether_dev_t *ether_dev = net->priv;
298
299
300 if ( !ether_dev ) {
301
302 return;
303 }
304
305
306 warn("%s: Tx timed out.", net->name);
307
308
309 ether_dev->tx_urb.transfer_flags |= USB_ASYNC_UNLINK;
310 usb_unlink_urb( ðer_dev->tx_urb );
311
312
313 ether_dev->stats.tx_errors++;
314}
315
316static int CDCEther_start_xmit( struct sk_buff *skb, struct net_device *net )
317{
318 ether_dev_t *ether_dev = net->priv;
319 int res;
320
321
322
323 netif_stop_queue( net );
324
325
326 memcpy(ether_dev->tx_buff, skb->data, skb->len);
327
328
329 FILL_BULK_URB( ðer_dev->tx_urb, ether_dev->usb,
330 usb_sndbulkpipe(ether_dev->usb, ether_dev->data_ep_out),
331 ether_dev->tx_buff, ether_dev->wMaxSegmentSize,
332 write_bulk_callback, ether_dev );
333
334
335 ether_dev->tx_urb.transfer_buffer_length = skb->len;
336
337
338 ether_dev->tx_urb.transfer_flags |= USB_ZERO_PACKET;
339
340
341 if ((res = usb_submit_urb(ðer_dev->tx_urb))) {
342
343 warn("failed tx_urb %d", res);
344
345 ether_dev->stats.tx_errors++;
346
347
348 netif_start_queue( net );
349 } else {
350
351
352 ether_dev->stats.tx_packets++;
353 ether_dev->stats.tx_bytes += skb->len;
354
355 net->trans_start = jiffies;
356 }
357
358
359 dev_kfree_skb(skb);
360
361
362 return 0;
363}
364
365
366
367
368static struct net_device_stats *CDCEther_netdev_stats( struct net_device *net )
369{
370
371 return &((ether_dev_t *)net->priv)->stats;
372}
373
374static int CDCEther_open(struct net_device *net)
375{
376 ether_dev_t *ether_dev = (ether_dev_t *)net->priv;
377 int res;
378
379
380 if ( (res = enable_net_traffic( ether_dev )) ) {
381 err("%s can't enable_net_traffic() - %d", __FUNCTION__, res );
382 return -EIO;
383 }
384
385
386 FILL_BULK_URB( ðer_dev->rx_urb, ether_dev->usb,
387 usb_rcvbulkpipe(ether_dev->usb, ether_dev->data_ep_in),
388 ether_dev->rx_buff, ether_dev->wMaxSegmentSize,
389 read_bulk_callback, ether_dev );
390
391
392 if ( (res = usb_submit_urb(ðer_dev->rx_urb)) ) {
393
394 warn( "%s failed rx_urb %d", __FUNCTION__, res );
395 }
396
397 if (ether_dev->properties & HAVE_NOTIFICATION_ELEMENT) {
398
399 FILL_INT_URB( ðer_dev->intr_urb,
400 ether_dev->usb,
401 usb_rcvintpipe(ether_dev->usb, ether_dev->comm_ep_in),
402 ether_dev->intr_buff,
403 sizeof ether_dev->intr_buff,
404 intr_callback,
405 ether_dev,
406 (ether_dev->usb->speed == USB_SPEED_HIGH)
407 ? ( 1 << ether_dev->intr_interval)
408 : ether_dev->intr_interval
409 );
410 if ( (res = usb_submit_urb(ðer_dev->intr_urb)) ) {
411 warn("%s failed intr_urb %d", __FUNCTION__, res );
412 }
413 }
414
415
416 netif_start_queue( net );
417
418
419 ether_dev->flags |= CDC_ETHER_RUNNING;
420
421
422 return 0;
423}
424
425static int CDCEther_close( struct net_device *net )
426{
427 ether_dev_t *ether_dev = net->priv;
428
429
430 ether_dev->flags &= ~CDC_ETHER_RUNNING;
431
432
433 netif_stop_queue( net );
434
435
436
437 if ( !(ether_dev->flags & CDC_ETHER_UNPLUG) ) {
438 disable_net_traffic( ether_dev );
439 }
440
441
442 usb_unlink_urb( ðer_dev->rx_urb );
443 usb_unlink_urb( ðer_dev->tx_urb );
444 usb_unlink_urb( ðer_dev->intr_urb );
445 usb_unlink_urb( ðer_dev->ctrl_urb );
446
447
448 return 0;
449}
450
451static int netdev_ethtool_ioctl(struct net_device *netdev, void *useraddr)
452{
453 ether_dev_t *ether_dev = netdev->priv;
454 u32 cmd;
455 char tmp[40];
456
457 if (get_user(cmd, (u32 *)useraddr))
458 return -EFAULT;
459
460 switch (cmd) {
461
462 case ETHTOOL_GDRVINFO: {
463 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
464 strncpy(info.driver, driver_name, ETHTOOL_BUSINFO_LEN);
465 strncpy(info.version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
466 sprintf(tmp, "usb%d:%d", ether_dev->usb->bus->busnum, ether_dev->usb->devnum);
467 strncpy(info.bus_info, tmp, ETHTOOL_BUSINFO_LEN);
468 sprintf(tmp, "CDC %x.%x", ((ether_dev->bcdCDC & 0xff00)>>8), (ether_dev->bcdCDC & 0x00ff) );
469 strncpy(info.fw_version, tmp, ETHTOOL_BUSINFO_LEN);
470 if (copy_to_user(useraddr, &info, sizeof(info)))
471 return -EFAULT;
472 return 0;
473 }
474
475 case ETHTOOL_GLINK: {
476 struct ethtool_value edata = {ETHTOOL_GLINK};
477 edata.data = netif_carrier_ok(netdev);
478 if (copy_to_user(useraddr, &edata, sizeof(edata)))
479 return -EFAULT;
480 return 0;
481 }
482 }
483 dbg("Got unsupported ioctl: %x", cmd);
484 return -EOPNOTSUPP;
485}
486
487static int CDCEther_ioctl( struct net_device *net, struct ifreq *rq, int cmd )
488{
489 switch(cmd) {
490 case SIOCETHTOOL:
491 return netdev_ethtool_ioctl(net, (void *) rq->ifr_data);
492 default:
493 return -ENOTTY;
494 }
495}
496
497
498
499static void CDC_SetEthernetPacketFilter (ether_dev_t *ether_dev)
500{
501#if 0
502 struct usb_ctrlrequest *dr = ðer_dev->ctrl_dr;
503 int res;
504
505 dr->bRequestType = USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE;
506 dr->bRequest = SET_ETHERNET_PACKET_FILTER;
507 dr->wValue = cpu_to_le16(ether_dev->mode_flags);
508 dr->wIndex = cpu_to_le16((u16)ether_dev->comm_interface);
509 dr->wLength = 0;
510
511 FILL_CONTROL_URB(ðer_dev->ctrl_urb,
512 ether_dev->usb,
513 usb_sndctrlpipe(ether_dev->usb, 0),
514 dr,
515 NULL,
516 NULL,
517 setpktfilter_done,
518 ether_dev);
519 if ( (res = usb_submit_urb(ðer_dev->ctrl_urb)) ) {
520 warn("%s failed submit ctrl_urb %d", __FUNCTION__, res);
521 }
522#endif
523
524}
525
526static void CDCEther_set_multicast( struct net_device *net )
527{
528 ether_dev_t *ether_dev = net->priv;
529 int i;
530 __u8 *buff;
531
532
533
534 netif_stop_queue(net);
535
536
537 if (net->flags & IFF_PROMISC) {
538
539 dbg( "%s: Promiscuous mode enabled", net->name);
540 ether_dev->mode_flags = MODE_FLAG_PROMISCUOUS |
541 MODE_FLAG_ALL_MULTICAST |
542 MODE_FLAG_DIRECTED |
543 MODE_FLAG_BROADCAST |
544 MODE_FLAG_MULTICAST;
545 } else if (net->mc_count > ether_dev->wNumberMCFilters) {
546
547 dbg("%s: too many MC filters for hardware, using allmulti", net->name);
548 ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
549 MODE_FLAG_DIRECTED |
550 MODE_FLAG_BROADCAST |
551 MODE_FLAG_MULTICAST;
552 } else if (net->flags & IFF_ALLMULTI) {
553
554 dbg("%s: using allmulti", net->name);
555 ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
556 MODE_FLAG_DIRECTED |
557 MODE_FLAG_BROADCAST |
558 MODE_FLAG_MULTICAST;
559 } else {
560
561 struct dev_mc_list *mclist;
562 dbg("%s: set multicast filters", net->name);
563 ether_dev->mode_flags = MODE_FLAG_ALL_MULTICAST |
564 MODE_FLAG_DIRECTED |
565 MODE_FLAG_BROADCAST |
566 MODE_FLAG_MULTICAST;
567 buff = kmalloc(6 * net->mc_count, GFP_ATOMIC);
568 for (i = 0, mclist = net->mc_list;
569 mclist && i < net->mc_count;
570 i++, mclist = mclist->next) {
571 memcpy(&mclist->dmi_addr, &buff[i * 6], 6);
572 }
573#if 0
574 usb_control_msg(ether_dev->usb,
575 usb_sndctrlpipe(ether_dev->usb, 0),
576 SET_ETHERNET_MULTICAST_FILTER,
577 USB_TYPE_CLASS | USB_DIR_OUT | USB_RECIP_INTERFACE,
578 cpu_to_le16(net->mc_count),
579 cpu_to_le16((u16)ether_dev->comm_interface),
580 buff,
581 (6* net->mc_count),
582 HZ);
583#endif
584 kfree(buff);
585 }
586
587 CDC_SetEthernetPacketFilter(ether_dev);
588
589
590 netif_wake_queue(net);
591}
592
593
594
595
596
597
598static int parse_header_functional_descriptor( int *bFunctionLength,
599 int bDescriptorType,
600 int bDescriptorSubtype,
601 unsigned char *data,
602 ether_dev_t *ether_dev,
603 int *requirements )
604{
605
606 if ( (~*requirements) & REQ_HDR_FUNC_DESCR ) {
607 err( "Multiple Header Functional Descriptors found." );
608 return -1;
609 }
610
611
612 if (*bFunctionLength != HEADER_FUNC_DESC_LEN) {
613 dbg( "Invalid length in Header Functional Descriptor, working around it." );
614
615
616
617 *bFunctionLength = HEADER_FUNC_DESC_LEN;
618 }
619
620
621
622 ether_dev->bcdCDC = data[0] + (data[1] << 8);
623 dbg( "Found Header descriptor, CDC version %x.", ether_dev->bcdCDC);
624
625
626 *requirements &= ~REQ_HDR_FUNC_DESCR;
627
628
629 return 0;
630}
631
632
633static int parse_union_functional_descriptor( int *bFunctionLength,
634 int bDescriptorType,
635 int bDescriptorSubtype,
636 unsigned char *data,
637 ether_dev_t *ether_dev,
638 int *requirements )
639{
640
641 if ( (~*requirements) & REQ_UNION_FUNC_DESCR ) {
642 err( "Multiple Union Functional Descriptors found." );
643 return -1;
644 }
645
646
647 if (*bFunctionLength != UNION_FUNC_DESC_LEN) {
648
649 err( "Invalid length in Union Functional Descriptor." );
650 return -1;
651 }
652
653
654 if (ether_dev->comm_interface != data[0]) {
655
656
657 if (ether_dev->comm_interface == data[1]) {
658 dbg( "Probably broken Union descriptor, fudging data interface." );
659
660
661
662
663 ether_dev->data_interface = data[0];
664 } else {
665 err( "Union Functional Descriptor is broken beyond repair." );
666 return -1;
667 }
668 } else{
669 ether_dev->data_interface = data[1];
670 }
671
672
673 *requirements &= ~REQ_UNION_FUNC_DESCR;
674
675
676 return 0;
677}
678
679
680static int parse_ethernet_functional_descriptor( int *bFunctionLength,
681 int bDescriptorType,
682 int bDescriptorSubtype,
683 unsigned char *data,
684 ether_dev_t *ether_dev,
685 int *requirements )
686{
687
688 if ( (~*requirements) & REQ_ETH_FUNC_DESCR ) {
689 err( "Multiple Ethernet Functional Descriptors found." );
690 return -1;
691 }
692
693
694 if (*bFunctionLength != ETHERNET_FUNC_DESC_LEN) {
695 err( "Invalid length in Ethernet Networking Functional Descriptor." );
696 return -1;
697 }
698
699
700 ether_dev->iMACAddress = data[0];
701 ether_dev->bmEthernetStatistics = data[1] + (data[2] << 8) + (data[3] << 16) + (data[4] << 24);
702 ether_dev->wMaxSegmentSize = data[5] + (data[6] << 8);
703 ether_dev->wNumberMCFilters = (data[7] + (data[8] << 8));
704 if (ether_dev->wNumberMCFilters & (1 << 15)) {
705 ether_dev->properties |= PERFECT_FILTERING;
706 dbg("Perfect filtering support");
707 } else {
708 dbg("Imperfect filtering support - need sw hashing");
709 }
710 if (0 == (ether_dev->wNumberMCFilters & (0x7f))) {
711 ether_dev->properties |= NO_SET_MULTICAST;
712 dbg("Can't use SetEthernetMulticastFilters request");
713 }
714 if (ether_dev->wNumberMCFilters > multicast_filter_limit) {
715 ether_dev->wNumberMCFilters = multicast_filter_limit;
716 }
717 ether_dev->bNumberPowerFilters = data[9];
718
719
720 *requirements &= ~REQ_ETH_FUNC_DESCR;
721
722
723 return 0;
724}
725
726static int parse_protocol_unit_functional_descriptor( int *bFunctionLength,
727 int bDescriptorType,
728 int bDescriptorSubtype,
729 unsigned char *data,
730 ether_dev_t *ether_dev,
731 int *requirements )
732{
733
734 if (bDescriptorType != CS_INTERFACE) {
735 err( "Invalid bDescriptorType found." );
736 return -1;
737 }
738
739
740 switch (bDescriptorSubtype) {
741 case 0x00:
742 return parse_header_functional_descriptor( bFunctionLength,
743 bDescriptorType,
744 bDescriptorSubtype,
745 data,
746 ether_dev,
747 requirements );
748 break;
749 case 0x06:
750 return parse_union_functional_descriptor( bFunctionLength,
751 bDescriptorType,
752 bDescriptorSubtype,
753 data,
754 ether_dev,
755 requirements );
756 break;
757 case 0x0F:
758 return parse_ethernet_functional_descriptor( bFunctionLength,
759 bDescriptorType,
760 bDescriptorSubtype,
761 data,
762 ether_dev,
763 requirements );
764 break;
765 default:
766
767 dbg( "Unexpected header type %x.", bDescriptorSubtype );
768 return 0;
769 }
770
771 return -1;
772}
773
774static int parse_ethernet_class_information( unsigned char *data, int length, ether_dev_t *ether_dev )
775{
776 int loc = 0;
777 int rc;
778 int bFunctionLength;
779 int bDescriptorType;
780 int bDescriptorSubtype;
781 int requirements = REQUIREMENTS_TOTAL;
782
783
784
785
786 while (loc < length) {
787
788 bFunctionLength = data[loc];
789 loc++;
790
791
792 bDescriptorType = data[loc];
793 loc++;
794
795
796 bDescriptorSubtype = data[loc];
797 loc++;
798
799
800 rc = parse_protocol_unit_functional_descriptor( &bFunctionLength,
801 bDescriptorType,
802 bDescriptorSubtype,
803 &data[loc],
804 ether_dev,
805 &requirements );
806
807 if (rc) {
808
809
810 err("Bad descriptor parsing: %x", rc );
811 return -1;
812 }
813
814
815 loc += (bFunctionLength - 3);
816 }
817
818 if (requirements) {
819
820 err( "Not all required functional descriptors present 0x%08X.", requirements );
821 return -1;
822 }
823
824 return 0;
825}
826
827
828
829
830
831static int find_and_parse_ethernet_class_information( struct usb_device *device, ether_dev_t *ether_dev )
832{
833 struct usb_config_descriptor *conf = NULL;
834 struct usb_interface *comm_intf_group = NULL;
835 struct usb_interface_descriptor *comm_intf = NULL;
836 int rc = -1;
837
838
839
840
841
842 conf = &( device->config[ether_dev->configuration_num] );
843 comm_intf_group = &( conf->interface[ether_dev->comm_interface] );
844 comm_intf = &( comm_intf_group->altsetting[ether_dev->comm_interface_altset_num] );
845
846
847 if (comm_intf->extralen > 0) {
848
849 rc = parse_ethernet_class_information( comm_intf->extra, comm_intf->extralen, ether_dev );
850 } else if (conf->extralen > 0) {
851
852
853
854 dbg( "Ethernet information found at device configuration. Trying to use it anyway." );
855 rc = parse_ethernet_class_information( conf->extra, conf->extralen, ether_dev );
856 } else {
857
858 err( "No ethernet information found." );
859 rc = -1;
860 }
861 return rc;
862}
863
864
865
866
867
868static int get_data_interface_endpoints( struct usb_device *device, ether_dev_t *ether_dev )
869{
870 struct usb_config_descriptor *conf = NULL;
871 struct usb_interface *data_intf_group = NULL;
872 struct usb_interface_descriptor *data_intf = NULL;
873
874
875 conf = &( device->config[ether_dev->configuration_num] );
876 data_intf_group = &( conf->interface[ether_dev->data_interface] );
877 data_intf = &( data_intf_group->altsetting[ether_dev->data_interface_altset_num_with_traffic] );
878
879
880 ether_dev->data_ep_in = 0;
881 ether_dev->data_ep_out = 0;
882
883
884 if ( data_intf->endpoint[0].bmAttributes != USB_ENDPOINT_XFER_BULK ) {
885 return -1;
886 }
887 if ( data_intf->endpoint[1].bmAttributes != USB_ENDPOINT_XFER_BULK ) {
888 return -1;
889 }
890
891
892 if ( data_intf->endpoint[0].bEndpointAddress & USB_DIR_IN ) {
893 ether_dev->data_ep_in = data_intf->endpoint[0].bEndpointAddress & 0x7F;
894 } else {
895 ether_dev->data_ep_out = data_intf->endpoint[0].bEndpointAddress;
896 ether_dev->data_ep_out_size = data_intf->endpoint[0].wMaxPacketSize;
897 }
898
899
900 if ( data_intf->endpoint[1].bEndpointAddress & USB_DIR_IN ) {
901 ether_dev->data_ep_in = data_intf->endpoint[1].bEndpointAddress & 0x7F;
902 } else {
903 ether_dev->data_ep_out = data_intf->endpoint[1].bEndpointAddress;
904 ether_dev->data_ep_out_size = data_intf->endpoint[1].wMaxPacketSize;
905 }
906
907
908 if (ether_dev->data_ep_in && ether_dev->data_ep_out) {
909 dbg( "detected BULK OUT packets of size %d", ether_dev->data_ep_out_size );
910 return 0;
911 }
912 return -1;
913}
914
915static int verify_ethernet_data_interface( struct usb_device *device, ether_dev_t *ether_dev )
916{
917 struct usb_config_descriptor *conf = NULL;
918 struct usb_interface *data_intf_group = NULL;
919 struct usb_interface_descriptor *data_intf = NULL;
920 int rc = -1;
921 int status;
922 int altset_num;
923
924
925
926
927
928 conf = &( device->config[ether_dev->configuration_num] );
929 data_intf_group = &( conf->interface[ether_dev->data_interface] );
930
931
932 ether_dev->data_interface_altset_num_with_traffic = -1;
933 ether_dev->data_bAlternateSetting_with_traffic = -1;
934 ether_dev->data_interface_altset_num_without_traffic = -1;
935 ether_dev->data_bAlternateSetting_without_traffic = -1;
936
937
938
939 for ( altset_num = 0; altset_num < data_intf_group->num_altsetting; altset_num++ ) {
940 data_intf = &( data_intf_group->altsetting[altset_num] );
941
942
943 if ( ( data_intf->bInterfaceClass == 0x0A )
944 && ( data_intf->bInterfaceSubClass == 0x00 )
945 && ( data_intf->bInterfaceProtocol == 0x00 ) ) {
946 if ( data_intf->bNumEndpoints == 2 ) {
947
948
949
950
951
952
953 ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
954 ether_dev->data_interface_altset_num_with_traffic = altset_num;
955 ether_dev->data_bAlternateSetting_with_traffic = data_intf->bAlternateSetting;
956 status = get_data_interface_endpoints( device, ether_dev );
957 if (!status) {
958 rc = 0;
959 }
960 }
961 if ( data_intf->bNumEndpoints == 0 ) {
962
963
964
965
966 ether_dev->data_bInterfaceNumber = data_intf->bInterfaceNumber;
967 ether_dev->data_interface_altset_num_without_traffic = altset_num;
968 ether_dev->data_bAlternateSetting_without_traffic = data_intf->bAlternateSetting;
969 }
970 }
971 }
972 return rc;
973}
974
975
976
977
978
979static int find_ethernet_comm_interface( struct usb_device *device, ether_dev_t *ether_dev )
980{
981 struct usb_config_descriptor *conf = NULL;
982 struct usb_interface *comm_intf_group = NULL;
983 struct usb_interface_descriptor *comm_intf = NULL;
984 int intf_num;
985 int altset_num;
986 int rc;
987
988 conf = &( device->config[ether_dev->configuration_num] );
989
990
991
992 for ( intf_num = 0; intf_num < conf->bNumInterfaces; intf_num++ ) {
993 comm_intf_group = &( conf->interface[intf_num] );
994
995
996 for ( altset_num = 0; altset_num < comm_intf_group->num_altsetting; altset_num++ ) {
997 comm_intf = &( comm_intf_group->altsetting[altset_num] );
998
999
1000
1001 ether_dev->comm_interface = intf_num;
1002 ether_dev->comm_bInterfaceNumber = comm_intf->bInterfaceNumber;
1003 ether_dev->comm_interface_altset_num = altset_num;
1004 ether_dev->comm_bAlternateSetting = comm_intf->bAlternateSetting;
1005
1006
1007 rc = find_and_parse_ethernet_class_information( device, ether_dev );
1008 if (rc) {
1009
1010 continue;
1011 }
1012
1013
1014
1015 rc = verify_ethernet_data_interface( device, ether_dev );
1016 if (rc) {
1017
1018 continue;
1019 }
1020
1021
1022
1023
1024
1025 if ( (comm_intf->bNumEndpoints == 1) &&
1026 (comm_intf->endpoint[0].bEndpointAddress & USB_DIR_IN) &&
1027 (comm_intf->endpoint[0].bmAttributes == USB_ENDPOINT_XFER_INT)) {
1028 ether_dev->properties |= HAVE_NOTIFICATION_ELEMENT;
1029 ether_dev->comm_ep_in = (comm_intf->endpoint[0].bEndpointAddress & 0x7F);
1030 dbg("interrupt address: %x",ether_dev->comm_ep_in);
1031 ether_dev->intr_interval = (comm_intf->endpoint[0].bInterval);
1032 dbg("interrupt interval: %d",ether_dev->intr_interval);
1033 }
1034
1035
1036
1037 return 0;
1038 }
1039 }
1040 return -1;
1041}
1042
1043
1044
1045
1046
1047
1048static int find_valid_configuration( struct usb_device *device, ether_dev_t *ether_dev )
1049{
1050 struct usb_config_descriptor *conf = NULL;
1051 int conf_num;
1052 int rc;
1053
1054
1055 for ( conf_num = 0; conf_num < device->descriptor.bNumConfigurations; conf_num++ ) {
1056 conf = &( device->config[conf_num] );
1057
1058
1059 if ( conf->bNumInterfaces != 2 ) {
1060
1061
1062 continue;
1063 }
1064
1065
1066
1067 ether_dev->configuration_num = conf_num;
1068 ether_dev->bConfigurationValue = conf->bConfigurationValue;
1069
1070
1071
1072 rc = find_ethernet_comm_interface( device, ether_dev );
1073
1074
1075 if ( !rc ) {
1076
1077 return 0;
1078 }
1079 }
1080
1081 return -1;
1082}
1083
1084
1085
1086
1087
1088
1089static int check_for_claimed_interfaces( struct usb_config_descriptor *config )
1090{
1091 struct usb_interface *comm_intf_group;
1092 int intf_num;
1093
1094
1095
1096 for ( intf_num = 0; intf_num < config->bNumInterfaces; intf_num++ ) {
1097 comm_intf_group = &( config->interface[intf_num] );
1098 if ( usb_interface_claimed( comm_intf_group ) ) {
1099
1100
1101
1102 return -1;
1103 }
1104 }
1105
1106
1107 return 0;
1108}
1109
1110
1111
1112
1113
1114
1115static inline unsigned char hex2dec( unsigned char digit )
1116{
1117
1118
1119 if ( (digit >= '0') && (digit <= '9') ) {
1120 return (digit - '0');
1121 }
1122 if ( (digit >= 'a') && (digit <= 'f') ) {
1123 return (digit - 'a' + 10);
1124 }
1125 if ( (digit >= 'A') && (digit <= 'F') ) {
1126 return (digit - 'A' + 10);
1127 }
1128 return 16;
1129}
1130
1131
1132
1133
1134
1135
1136static void set_ethernet_addr( ether_dev_t *ether_dev )
1137{
1138 unsigned char mac_addr[6];
1139 int i;
1140 int len;
1141 unsigned char buffer[13];
1142
1143
1144 mac_addr[0] = 0x00;
1145 mac_addr[1] = 0x00;
1146 mac_addr[2] = 0x00;
1147 mac_addr[3] = 0x00;
1148 mac_addr[4] = 0x00;
1149 mac_addr[5] = 0x00;
1150
1151
1152 if (0 > (len = usb_string(ether_dev->usb, ether_dev->iMACAddress, buffer, 13))) {
1153 err("Attempting to get MAC address failed: %d", -1*len);
1154 return;
1155 }
1156
1157
1158 if (len != 12) {
1159
1160 err("Attempting to get MAC address returned %d bytes", len);
1161 return;
1162 }
1163
1164
1165 for (i = 0; i < 6; i++) {
1166 if ((16 == buffer[2 * i]) || (16 == buffer[2 * i + 1])) {
1167 err("Bad value in MAC address");
1168 }
1169 else {
1170 mac_addr[i] = ( hex2dec( buffer[2 * i] ) << 4 ) + hex2dec( buffer[2 * i + 1] );
1171 }
1172 }
1173
1174
1175 memcpy( ether_dev->net->dev_addr, mac_addr, sizeof(mac_addr) );
1176}
1177
1178
1179
1180
1181
1182
1183void log_device_info(ether_dev_t *ether_dev)
1184{
1185 int len;
1186 int string_num;
1187 unsigned char manu[256];
1188 unsigned char prod[256];
1189 unsigned char sern[256];
1190 unsigned char *mac_addr;
1191
1192
1193 manu[0] = 0x00;
1194 prod[0] = 0x00;
1195 sern[0] = 0x00;
1196
1197
1198 string_num = ether_dev->usb->descriptor.iManufacturer;
1199 if (string_num) {
1200
1201 len = usb_string(ether_dev->usb, string_num, manu, 255);
1202
1203 manu[len] = 0x00;
1204 }
1205
1206
1207 string_num = ether_dev->usb->descriptor.iProduct;
1208 if (string_num) {
1209
1210 len = usb_string(ether_dev->usb, string_num, prod, 255);
1211
1212 prod[len] = 0x00;
1213 }
1214
1215
1216 string_num = ether_dev->usb->descriptor.iSerialNumber;
1217 if (string_num) {
1218
1219 len = usb_string(ether_dev->usb, string_num, sern, 255);
1220
1221 sern[len] = 0x00;
1222 }
1223
1224
1225 mac_addr = ether_dev->net->dev_addr;
1226
1227
1228 info( "%s: %s %s %s", ether_dev->net->name, manu, prod, sern);
1229 dbg( "%s: %02X:%02X:%02X:%02X:%02X:%02X",
1230 ether_dev->net->name,
1231 mac_addr[0],
1232 mac_addr[1],
1233 mac_addr[2],
1234 mac_addr[3],
1235 mac_addr[4],
1236 mac_addr[5] );
1237
1238}
1239
1240
1241static struct usb_driver CDCEther_driver ;
1242
1243
1244
1245
1246
1247
1248static void * CDCEther_probe( struct usb_device *usb, unsigned int ifnum,
1249 const struct usb_device_id *id)
1250{
1251 struct net_device *net;
1252 ether_dev_t *ether_dev;
1253 int rc;
1254
1255
1256
1257 if ( check_for_claimed_interfaces( usb->actconfig ) ) {
1258
1259
1260 return NULL;
1261 }
1262
1263
1264
1265
1266
1267 if(!(ether_dev = kmalloc(sizeof(ether_dev_t), GFP_KERNEL))) {
1268 err("out of memory allocating device structure");
1269 return NULL;
1270 }
1271
1272
1273 memset(ether_dev, 0, sizeof(ether_dev_t));
1274
1275
1276 rc = find_valid_configuration( usb, ether_dev );
1277 if (rc) {
1278
1279
1280 kfree( ether_dev );
1281 return NULL;
1282 }
1283
1284
1285
1286 if ( usb_set_configuration( usb, ether_dev->bConfigurationValue ) ) {
1287 err("usb_set_configuration() failed");
1288 kfree( ether_dev );
1289 return NULL;
1290 }
1291
1292
1293 if (usb_set_interface(usb, ether_dev->comm_bInterfaceNumber, ether_dev->comm_bAlternateSetting)) {
1294 err("usb_set_interface() failed");
1295 kfree( ether_dev );
1296 return NULL;
1297 }
1298
1299
1300 if (ether_dev->data_interface_altset_num_without_traffic >= 0) {
1301
1302
1303
1304 if (usb_set_interface( usb,
1305 ether_dev->data_bInterfaceNumber,
1306 ether_dev->data_bAlternateSetting_without_traffic)) {
1307 err("usb_set_interface() failed");
1308 kfree( ether_dev );
1309 return NULL;
1310 }
1311 } else {
1312
1313
1314
1315 if (usb_set_interface( usb,
1316 ether_dev->data_bInterfaceNumber,
1317 ether_dev->data_bAlternateSetting_with_traffic)) {
1318 err("usb_set_interface() failed");
1319 kfree( ether_dev );
1320 return NULL;
1321 }
1322 }
1323
1324
1325 net = init_etherdev( NULL, 0 );
1326 if ( !net ) {
1327
1328
1329 err( "Unable to initialize ethernet device" );
1330 kfree( ether_dev );
1331 return NULL;
1332 }
1333
1334
1335
1336 net->priv = ether_dev;
1337 SET_MODULE_OWNER(net);
1338 net->open = CDCEther_open;
1339 net->stop = CDCEther_close;
1340 net->watchdog_timeo = CDC_ETHER_TX_TIMEOUT;
1341 net->tx_timeout = CDCEther_tx_timeout;
1342 net->do_ioctl = CDCEther_ioctl;
1343 net->hard_start_xmit = CDCEther_start_xmit;
1344 net->set_multicast_list = CDCEther_set_multicast;
1345 net->get_stats = CDCEther_netdev_stats;
1346 net->mtu = ether_dev->wMaxSegmentSize - 14;
1347
1348
1349 ether_dev->usb = usb;
1350 ether_dev->net = net;
1351
1352
1353 set_ethernet_addr( ether_dev );
1354
1355
1356 log_device_info( ether_dev );
1357
1358
1359 usb_driver_claim_interface( &CDCEther_driver,
1360 &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]),
1361 ether_dev );
1362
1363
1364 usb_inc_dev_use( usb );
1365
1366
1367 return ether_dev;
1368}
1369
1370
1371
1372
1373
1374
1375
1376
1377static void CDCEther_disconnect( struct usb_device *usb, void *ptr )
1378{
1379 ether_dev_t *ether_dev = ptr;
1380
1381
1382 if ( !ether_dev || !ether_dev->usb ) {
1383
1384 warn("unregistering non-existant device");
1385 return;
1386 }
1387
1388
1389 ether_dev->usb = NULL;
1390
1391
1392
1393
1394 ether_dev->flags |= CDC_ETHER_UNPLUG;
1395
1396
1397 unregister_netdev( ether_dev->net );
1398
1399
1400 ether_dev->net = NULL;
1401
1402
1403 usb_dec_dev_use( usb );
1404
1405
1406 usb_driver_release_interface( &CDCEther_driver,
1407 &(usb->config[ether_dev->configuration_num].interface[ether_dev->comm_interface]) );
1408
1409
1410 usb_driver_release_interface( &CDCEther_driver,
1411 &(usb->config[ether_dev->configuration_num].interface[ether_dev->data_interface]) );
1412
1413
1414 kfree( ether_dev );
1415
1416
1417 ether_dev = NULL;
1418}
1419
1420
1421
1422
1423
1424static struct usb_driver CDCEther_driver = {
1425 name: driver_name,
1426 probe: CDCEther_probe,
1427 disconnect: CDCEther_disconnect,
1428 id_table: CDCEther_ids,
1429};
1430
1431
1432
1433
1434
1435int __init CDCEther_init(void)
1436{
1437 dbg( "%s", version );
1438 return usb_register( &CDCEther_driver );
1439}
1440
1441void __exit CDCEther_exit(void)
1442{
1443 usb_deregister( &CDCEther_driver );
1444}
1445
1446
1447
1448
1449
1450module_init( CDCEther_init );
1451module_exit( CDCEther_exit );
1452
1453MODULE_AUTHOR("Brad Hards and another");
1454MODULE_DESCRIPTION("USB CDC Ethernet driver");
1455MODULE_LICENSE("GPL");
1456
1457MODULE_DEVICE_TABLE (usb, CDCEther_ids);
1458MODULE_PARM (multicast_filter_limit, "i");
1459MODULE_PARM_DESC (multicast_filter_limit, "CDCEther maximum number of filtered multicast addresses");
1460
1461
1462
1463
1464