57 #include <linux/module.h>
59 #include <linux/kernel.h>
60 #include <linux/types.h>
63 #include <linux/netdevice.h>
64 #include <linux/slab.h>
65 #include <linux/rtnetlink.h>
73 static int qos_mtt_bits = 0;
110 static void irda_usb_init_qos(
struct irda_usb_cb *
self) ;
113 static void irda_usb_change_speed_xbofs(
struct irda_usb_cb *
self);
116 static int irda_usb_open(
struct irda_usb_cb *
self);
117 static void irda_usb_close(
struct irda_usb_cb *
self);
118 static void speed_bulk_callback(
struct urb *
urb);
119 static void write_bulk_callback(
struct urb *
urb);
120 static void irda_usb_receive(
struct urb *
urb);
121 static void irda_usb_rx_defer_expired(
unsigned long data);
149 static void irda_usb_build_header(
struct irda_usb_cb *
self,
158 ((self->new_speed != -1) || (self->new_xbofs != -1))) {
163 if (self->new_speed == -1)
164 self->new_speed =
self->speed ;
166 if (self->new_xbofs == -1)
167 self->new_xbofs =
self->xbofs ;
171 if (self->new_speed != -1) {
177 (!force) && (self->speed != -1)) {
180 IRDA_DEBUG(2,
"%s(), not changing speed yet\n", __func__);
185 IRDA_DEBUG(2,
"%s(), changing speed to %d\n", __func__, self->new_speed);
186 self->speed =
self->new_speed;
190 switch (self->speed) {
230 if (self->new_xbofs != -1) {
231 IRDA_DEBUG(2,
"%s(), changing xbofs to %d\n", __func__, self->new_xbofs);
232 self->xbofs =
self->new_xbofs;
236 switch (self->xbofs) {
273 int turnaround_time = irda_get_mtt(skb);
275 if ( turnaround_time == 0 )
277 else if ( turnaround_time <= 10 )
279 else if ( turnaround_time <= 50 )
281 else if ( turnaround_time <= 100 )
283 else if ( turnaround_time <= 500 )
285 else if ( turnaround_time <= 1000 )
287 else if ( turnaround_time <= 5000 )
299 static void irda_usb_change_speed_xbofs(
struct irda_usb_cb *
self)
305 IRDA_DEBUG(2,
"%s(), speed=%d, xbofs=%d\n", __func__,
306 self->new_speed, self->new_xbofs);
309 urb =
self->speed_urb;
310 if (urb->status != 0) {
316 frame =
self->speed_buff;
319 irda_usb_build_header(
self, frame, 1);
322 if (frame[0] == 0)
return ;
328 usb_fill_bulk_urb(urb, self->usbdev,
329 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
331 speed_bulk_callback,
self);
332 urb->transfer_buffer_length =
self->header_length;
333 urb->transfer_flags = 0;
346 static void speed_bulk_callback(
struct urb *urb)
358 if (urb->status != 0) {
360 IRDA_DEBUG(0,
"%s(), URB complete status %d, transfer_flags 0x%04X\n", __func__, urb->status, urb->transfer_flags);
374 self->new_speed = -1;
375 self->new_xbofs = -1;
378 netif_wake_queue(self->netdev);
389 struct urb *urb =
self->tx_urb;
397 netif_stop_queue(netdev);
405 if (!self->present) {
406 IRDA_DEBUG(0,
"%s(), Device is gone...\n", __func__);
411 xbofs = irda_get_next_xbofs(skb);
412 if ((xbofs != self->xbofs) && (xbofs != -1)) {
413 self->new_xbofs = xbofs;
417 speed = irda_get_next_speed(skb);
418 if ((speed != self->speed) && (speed != -1)) {
420 self->new_speed = speed;
431 irda_usb_change_speed_xbofs(
self);
438 if (urb->status != 0) {
443 skb_copy_from_linear_data(skb, self->tx_buff + self->header_length, skb->
len);
447 __u8 turnaround_time;
448 __u8* frame =
self->tx_buff;
449 turnaround_time = get_turnaround_time( skb );
450 irda_usb_build_header(
self, frame, 0);
451 frame[2] = turnaround_time;
452 if ((skb->
len != 0) &&
453 ((skb->
len % 128) == 0) &&
454 ((skb->
len % 512) != 0)) {
462 irda_usb_build_header(
self, self->tx_buff, 0);
468 usb_fill_bulk_urb(urb, self->usbdev,
469 usb_sndbulkpipe(self->usbdev, self->bulk_out_ep),
470 self->tx_buff, skb->
len + self->header_length,
471 write_bulk_callback, skb);
478 urb->transfer_flags = URB_ZERO_PACKET;
495 mtt = irda_get_mtt(skb);
499 diff =
self->now.tv_usec -
self->stamp.tv_usec;
500 #ifdef IU_USB_MIN_RTT
527 netdev->
stats.tx_errors++;
532 netdev->
stats.tx_packets++;
537 spin_unlock_irqrestore(&self->lock, flags);
544 spin_unlock_irqrestore(&self->lock, flags);
552 static void write_bulk_callback(
struct urb *urb)
555 struct sk_buff *skb = urb->context;
570 if (urb->status != 0) {
572 IRDA_DEBUG(0,
"%s(), URB complete status %d, transfer_flags 0x%04X\n", __func__, urb->status, urb->transfer_flags);
590 if ((!self->netopen) || (!self->present)) {
591 IRDA_DEBUG(0,
"%s(), Network is gone...\n", __func__);
592 spin_unlock_irqrestore(&self->lock, flags);
597 if ((self->new_speed != -1) || (self->new_xbofs != -1)) {
598 if ((self->new_speed != self->speed) ||
599 (self->new_xbofs != self->xbofs)) {
602 IRDA_DEBUG(1,
"%s(), Changing speed now...\n", __func__);
603 irda_usb_change_speed_xbofs(
self);
606 self->new_speed = -1;
607 self->new_xbofs = -1;
609 netif_wake_queue(self->netdev);
613 netif_wake_queue(self->netdev);
615 spin_unlock_irqrestore(&self->lock, flags);
627 static void irda_usb_net_timeout(
struct net_device *netdev)
634 IRDA_DEBUG(0,
"%s(), Network layer thinks we timed out!\n", __func__);
641 if (!self->present) {
643 netif_stop_queue(netdev);
644 spin_unlock_irqrestore(&self->lock, flags);
649 urb =
self->speed_urb;
650 if (urb->status != 0) {
651 IRDA_DEBUG(0,
"%s: Speed change timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->
name, urb->status, urb->transfer_flags);
653 switch (urb->status) {
665 netif_wake_queue(self->netdev);
673 if (urb->status != 0) {
674 struct sk_buff *skb = urb->context;
676 IRDA_DEBUG(0,
"%s: Tx timed out, urb->status=%d, urb->transfer_flags=0x%04X\n", netdev->
name, urb->status, urb->transfer_flags);
679 netdev->
stats.tx_errors++;
681 #ifdef IU_BUG_KICK_TIMEOUT
683 if(self->new_speed == -1)
684 self->new_speed =
self->speed;
685 if(self->new_xbofs == -1)
686 self->new_xbofs =
self->xbofs;
687 irda_usb_change_speed_xbofs(
self);
690 switch (urb->status) {
708 netif_wake_queue(self->netdev);
713 spin_unlock_irqrestore(&self->lock, flags);
760 static void irda_usb_submit(
struct irda_usb_cb *
self,
struct sk_buff *skb,
struct urb *urb)
776 usb_fill_bulk_urb(urb, self->usbdev,
777 usb_rcvbulkpipe(self->usbdev, self->bulk_in_ep),
779 irda_usb_receive,
skb);
799 static void irda_usb_receive(
struct urb *urb)
806 struct urb *next_urb;
807 unsigned int len, docopy;
809 IRDA_DEBUG(2,
"%s(), len=%d\n", __func__, urb->actual_length);
818 if ((!self->netopen) || (!
self->present)) {
819 IRDA_DEBUG(0,
"%s(), Network is gone!\n", __func__);
825 if (urb->status != 0) {
826 switch (urb->status) {
828 self->netdev->stats.rx_crc_errors++;
841 self->netdev->stats.rx_errors++;
842 IRDA_DEBUG(0,
"%s(), RX status %d, transfer_flags 0x%04X\n", __func__, urb->status, urb->transfer_flags);
855 self->rx_defer_timer.function = irda_usb_rx_defer_expired;
856 self->rx_defer_timer.data = (
unsigned long) urb;
857 mod_timer(&self->rx_defer_timer, jiffies + (10 *
HZ / 1000));
862 if (urb->actual_length <= self->header_length) {
884 newskb = dev_alloc_skb(docopy ? urb->actual_length :
888 newskb = dev_alloc_skb(docopy ? urb->actual_length :
892 self->netdev->stats.rx_dropped++;
904 skb_copy_from_linear_data(skb, newskb->
data, urb->actual_length);
918 skb_put(dataskb, urb->actual_length);
919 skb_pull(dataskb, self->header_length);
922 dataskb->dev =
self->netdev;
923 skb_reset_mac_header(dataskb);
929 self->netdev->stats.rx_bytes += len;
930 self->netdev->stats.rx_packets++;
944 next_urb =
self->idle_rx_urb;
948 self->idle_rx_urb =
urb;
952 irda_usb_submit(
self, skb, next_urb);
960 static void irda_usb_rx_defer_expired(
unsigned long data)
962 struct urb *urb = (
struct urb *) data;
966 struct urb *next_urb;
977 next_urb =
self->idle_rx_urb;
979 self->idle_rx_urb =
urb;
980 irda_usb_submit(
self, skb, next_urb);
988 static int irda_usb_is_receiving(
struct irda_usb_cb *
self)
1010 #define STIR421X_PATCH_PRODUCT_VER "Product Version: "
1011 #define STIR421X_PATCH_STMP_TAG "STMP"
1012 #define STIR421X_PATCH_CODE_OFFSET 512
1014 #define STIR421X_PATCH_END_OF_HDR_TAG 0x1A
1015 #define STIR421X_PATCH_BLOCK_SIZE 1023
1024 static int stir421x_fw_upload(
struct irda_usb_cb *
self,
1025 const unsigned char *
patch,
1026 const unsigned int patch_len)
1032 unsigned char *patch_block;
1035 if (patch_block ==
NULL)
1039 for (i = 0; i < patch_len; i +=
block_size) {
1040 block_size = patch_len -
i;
1046 memcpy(patch_block, patch + i, block_size);
1049 usb_sndbulkpipe(self->usbdev,
1051 patch_block, block_size,
1053 IRDA_DEBUG(3,
"%s(): Bulk send %u bytes, ret=%d\n",
1054 __func__, actual_len, ret);
1072 static int stir421x_patch_device(
struct irda_usb_cb *
self)
1076 char stir421x_fw_name[12];
1078 const unsigned char *fw_version_ptr;
1085 sprintf(stir421x_fw_name,
"4210%4X.sb",
1086 self->usbdev->descriptor.bcdDevice);
1092 IRDA_MESSAGE(
"%s(): Received firmware %s (%zu bytes)\n",
1093 __func__, stir421x_fw_name, fw->
size);
1100 fw_version_ptr = fw->
data +
1104 if (fw_version_ptr[3] ==
'.' &&
1105 fw_version_ptr[7] ==
'.') {
1111 fw_version = (major << 12)
1113 + ((build / 10) << 4)
1116 IRDA_DEBUG(3,
"%s(): Firmware Product version %ld\n",
1117 __func__, fw_version);
1121 if (self->usbdev->descriptor.bcdDevice ==
cpu_to_le16(fw_version)) {
1127 for (i = 0; i < fw->
size && fw->
data[
i] !=
1137 ret = stir421x_fw_upload(
self, &fw->
data[i],
1166 static int irda_usb_net_open(
struct net_device *netdev)
1169 unsigned long flags;
1176 self = netdev_priv(netdev);
1181 if(!self->present) {
1182 spin_unlock_irqrestore(&self->lock, flags);
1187 if(self->needspatch) {
1188 spin_unlock_irqrestore(&self->lock, flags);
1197 self->new_speed = -1;
1198 self->new_xbofs = -1;
1203 spin_unlock_irqrestore(&self->lock, flags);
1210 sprintf(hwname,
"usb#%d", self->usbdev->devnum);
1211 self->irlap =
irlap_open(netdev, &self->qos, hwname);
1215 netif_start_queue(netdev);
1222 self->idle_rx_urb->context =
NULL;
1236 irda_usb_submit(
self, skb, self->rx_urb[i]);
1250 static int irda_usb_net_close(
struct net_device *netdev)
1258 self = netdev_priv(netdev);
1266 netif_stop_queue(netdev);
1272 for (i = 0; i <
self->max_rx_urb; i++) {
1273 struct urb *urb =
self->rx_urb[
i];
1280 urb->context =
NULL;
1301 unsigned long flags;
1307 self = netdev_priv(dev);
1310 IRDA_DEBUG(2,
"%s(), %s, (cmd=0x%X)\n", __func__, dev->
name, cmd);
1321 self->new_speed = irq->ifr_baudrate;
1322 irda_usb_change_speed_xbofs(
self);
1324 spin_unlock_irqrestore(&self->lock, flags);
1334 irq->ifr_receiving = irda_usb_is_receiving(
self);
1356 static inline void irda_usb_init_qos(
struct irda_usb_cb *
self)
1362 desc =
self->irda_desc;
1376 IRDA_DEBUG(0,
"%s(), dongle says speed=0x%X, size=0x%X, window=0x%X, bofs=0x%X, turn=0x%X\n",
1377 __func__, self->qos.baud_rate.bits, self->qos.data_size.bits, self->qos.window_size.bits, self->qos.additional_bofs.bits, self->qos.min_turn_time.bits);
1381 self->qos.baud_rate.bits &= 0x00ff;
1383 self->qos.data_size.bits = 0x07;
1385 self->qos.window_size.bits = 0x01;
1387 self->qos.window_size.bits = 0x7f;
1389 self->qos.additional_bofs.bits = 0x01;
1394 self->qos.min_turn_time.bits = qos_mtt_bits;
1405 .ndo_open = irda_usb_net_open,
1406 .ndo_stop = irda_usb_net_close,
1407 .ndo_do_ioctl = irda_usb_net_ioctl,
1408 .ndo_start_xmit = irda_usb_hard_xmit,
1409 .ndo_tx_timeout = irda_usb_net_timeout,
1416 static inline int irda_usb_open(
struct irda_usb_cb *
self)
1424 irda_usb_init_qos(
self);
1434 static inline void irda_usb_close(
struct irda_usb_cb *
self)
1442 kfree(self->speed_buff);
1443 self->speed_buff =
NULL;
1445 kfree(self->tx_buff);
1446 self->tx_buff =
NULL;
1468 static inline int irda_usb_parse_endpoints(
struct irda_usb_cb *
self,
struct usb_host_endpoint *
endpoint,
int ennum)
1473 self->bulk_in_ep = 0;
1474 self->bulk_out_ep = 0;
1475 self->bulk_int_ep = 0;
1478 for(i = 0; i < ennum; i++) {
1489 attr = endpoint[
i].desc.bmAttributes;
1490 psize =
le16_to_cpu(endpoint[i].desc.wMaxPacketSize);
1497 self->bulk_in_ep = ep;
1500 self->bulk_out_ep = ep;
1501 self->bulk_out_mtu = psize;
1507 self->bulk_int_ep = ep;
1509 IRDA_ERROR(
"%s(), Unrecognised endpoint %02X.\n", __func__, ep);
1514 IRDA_DEBUG(0,
"%s(), And our endpoints are : in=%02X, out=%02X (%d), int=%02X\n",
1515 __func__, self->bulk_in_ep, self->bulk_out_ep, self->bulk_out_mtu, self->bulk_int_ep);
1517 return (self->bulk_in_ep != 0) && (
self->bulk_out_ep != 0);
1520 #ifdef IU_DUMP_CLASS_DESC
1528 static inline void irda_usb_dump_class_desc(
struct irda_class_desc *desc)
1556 struct usb_device *dev = interface_to_usbdev (intf);
1574 0, intf->altsetting->desc.bInterfaceNumber, desc,
1575 sizeof(*desc), 500);
1577 IRDA_DEBUG(1,
"%s(), ret=%d\n", __func__, ret);
1578 if (ret <
sizeof(*desc)) {
1579 IRDA_WARNING(
"usb-irda: class_descriptor read %s (%d)\n",
1580 (ret<0) ?
"failed" :
"too short", ret);
1586 #ifdef IU_DUMP_CLASS_DESC
1587 irda_usb_dump_class_desc(desc);
1614 struct usb_device *dev = interface_to_usbdev(intf);
1626 IRDA_MESSAGE(
"IRDA-USB found at address %d, Vendor: %x, Product: %x\n",
1627 dev->devnum,
le16_to_cpu(dev->descriptor.idVendor),
1635 self = netdev_priv(net);
1640 self->capability =
id->driver_info;
1641 self->needspatch = ((
self->capability &
IUC_STIR421X) != 0);
1652 self->rx_urb = kcalloc(self->max_rx_urb,
sizeof(
struct urb *),
1657 for (i = 0; i <
self->max_rx_urb; i++) {
1659 if (!self->rx_urb[i]) {
1664 if (!self->tx_urb) {
1668 if (!self->speed_urb) {
1674 dev_err(&intf->dev,
"reset_configuration failed\n");
1684 IRDA_DEBUG(1,
"usb-irda: set interface %d result %d\n", intf->altsetting->desc.bInterfaceNumber, ret);
1692 IRDA_DEBUG(0,
"%s(), Received -EPIPE, ignoring...\n", __func__);
1695 IRDA_DEBUG(0,
"%s(), Unknown error %d\n", __func__, ret);
1701 interface = intf->cur_altsetting;
1702 if(!irda_usb_parse_endpoints(
self, interface->endpoint,
1703 interface->desc.bNumEndpoints)) {
1704 IRDA_ERROR(
"%s(), Bogus endpoints...\n", __func__);
1712 irda_desc = irda_usb_find_class_desc(intf);
1717 if (self->needspatch) {
1718 ret =
usb_control_msg (self->usbdev, usb_sndctrlpipe (self->usbdev, 0),
1719 0x02, 0x40, 0, 0,
NULL, 0, 500);
1721 IRDA_DEBUG (0,
"usb_control_msg failed %d\n", ret);
1728 self->irda_desc = irda_desc;
1731 self->usbintf =
intf;
1738 if (!self->speed_buff)
1746 ret = irda_usb_open(
self);
1751 usb_set_intfdata(intf,
self);
1753 if (self->needspatch) {
1755 ret = stir421x_patch_device(
self);
1756 self->needspatch = (ret < 0);
1757 if (self->needspatch) {
1758 IRDA_ERROR(
"STIR421X: Couldn't upload patch\n");
1763 irda_desc = irda_usb_find_class_desc (self->usbintf);
1768 kfree(self->irda_desc);
1769 self->irda_desc = irda_desc;
1770 irda_usb_init_qos(
self);
1777 kfree(self->tx_buff);
1779 kfree(self->speed_buff);
1786 for (i = 0; i <
self->max_rx_urb; i++)
1788 kfree(self->rx_urb);
1809 unsigned long flags;
1810 struct irda_usb_cb *
self = usb_get_intfdata(intf);
1815 usb_set_intfdata(intf,
NULL);
1831 spin_unlock_irqrestore(&self->lock, flags);
1834 if((self->netopen) || (self->irlap)) {
1837 netif_stop_queue(self->netdev);
1839 for (i = 0; i <
self->max_rx_urb; i++)
1848 irda_usb_close(
self);
1850 self->usbdev =
NULL;
1851 self->usbintf =
NULL;
1854 for (i = 0; i <
self->max_rx_urb; i++)
1856 kfree(self->rx_urb);
1863 IRDA_DEBUG(0,
"%s(), USB IrDA Disconnected\n", __func__);
1870 struct irda_usb_cb *
self = usb_get_intfdata(intf);
1875 if (self->tx_urb !=
NULL)
1877 if (self->speed_urb !=
NULL)
1879 for (i = 0; i <
self->max_rx_urb; i++) {
1880 if (self->rx_urb[i] !=
NULL)
1889 struct irda_usb_cb *
self = usb_get_intfdata(intf);
1892 for (i = 0; i <
self->max_rx_urb; i++) {
1893 if (self->rx_urb[i] !=
NULL)
1906 static struct usb_driver irda_driver = {
1908 .probe = irda_usb_probe,
1909 .disconnect = irda_usb_disconnect,
1910 .id_table = dongles,
1912 .suspend = irda_usb_suspend,
1913 .resume = irda_usb_resume,