39 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/types.h>
45 #include <linux/time.h>
47 #include <linux/netdevice.h>
48 #include <linux/slab.h>
58 #include <asm/byteorder.h>
59 #include <asm/unaligned.h>
65 static int qos_mtt_bits = 0x07;
69 static int rx_sensitivity = 1;
71 MODULE_PARM_DESC(rx_sensitivity,
"Set Receiver sensitivity (0-6, 0 is most sensitive)");
77 #define STIR_IRDA_HEADER 4
78 #define CTRL_TIMEOUT 100
79 #define TRANSMIT_TIMEOUT 200
80 #define STIR_FIFO_SIZE 4096
81 #define FIFO_REGS_SIZE 3
187 { USB_DEVICE(0x066f, 0x4200) },
198 pr_debug(
"%s: write reg %d = 0x%x\n",
199 stir->
netdev->name, reg, value);
211 struct usb_device *dev = stir->
usbdev;
220 static inline int isfir(
u32 speed)
222 return speed == 4000000;
275 for (i = 0; i < skb->
len; i++)
276 ptr = stuff_fir(ptr, skb->
data[i]);
279 ptr = stuff_fir(ptr, fcs & 0xff);
280 ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
281 ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
282 ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);
289 wraplen = (ptr -
buf) - STIR_IRDA_HEADER;
290 buf[2] = wraplen & 0xff;
291 buf[3] = (wraplen >> 8) & 0xff;
296 static unsigned wrap_sir_skb(
struct sk_buff *skb,
__u8 *buf)
304 buf[2] = wraplen & 0xff;
305 buf[3] = (wraplen >> 8) & 0xff;
315 static void fir_eof(
struct stir_cb *stir)
318 int len = rx_buff->
len - 4;
323 pr_debug(
"%s: short frame len %d\n",
326 ++stir->
netdev->stats.rx_errors;
327 ++stir->
netdev->stats.rx_length_errors;
333 pr_debug(
"crc error calc 0x%x len %d\n", fcs, len);
334 stir->
netdev->stats.rx_errors++;
335 stir->
netdev->stats.rx_crc_errors++;
341 nskb = dev_alloc_skb(len + 1);
343 ++stir->
netdev->stats.rx_dropped;
346 skb_reserve(nskb, 1);
348 skb_copy_to_linear_data(nskb, rx_buff->
data, len);
350 nskb = dev_alloc_skb(rx_buff->
truesize);
352 ++stir->
netdev->stats.rx_dropped;
355 skb_reserve(nskb, 1);
363 skb_reset_mac_header(skb);
369 stir->
netdev->stats.rx_packets++;
377 static void stir_fir_chars(
struct stir_cb *stir,
383 for (i = 0; i <
len; i++) {
386 switch(rx_buff->
state) {
401 pr_debug(
"%s: got EOF after escape\n",
424 pr_debug(
"%s: got XBOF without escape\n",
438 pr_debug(
"%s: fir frame exceeds %d\n",
440 ++stir->
netdev->stats.rx_over_errors;
448 ++stir->
netdev->stats.rx_frame_errors;
451 ++stir->
netdev->stats.rx_errors;
458 static void stir_sir_chars(
struct stir_cb *stir,
459 const __u8 *bytes,
int len)
463 for (i = 0; i <
len; i++)
468 static inline void unwrap_chars(
struct stir_cb *stir,
471 if (isfir(stir->
speed))
472 stir_fir_chars(stir, bytes, length);
474 stir_sir_chars(stir, bytes, length);
478 static const struct {
499 static int change_speed(
struct stir_cb *stir,
unsigned speed)
505 if (speed == stir_modes[i].speed)
513 pr_debug(
"speed change from %d to %d\n", stir->
speed, speed);
566 struct stir_cb *stir = netdev_priv(netdev);
568 netif_stop_queue(netdev);
571 SKB_LINEAR_ASSERT(skb);
591 static int fifo_txwait(
struct stir_cb *stir,
int space)
595 unsigned long prev_count = 0x1fff;
598 for (;; prev_count =
count) {
603 "FIFO register read error: %d\n", err);
609 count = (unsigned)(stir->
fifo_status[2] & 0x1f) << 8
612 pr_debug(
"fifo status 0x%lx count %lu\n", status, count);
623 if (!netif_running(stir->
netdev) ||
624 !netif_device_present(stir->
netdev))
632 if (prev_count < count)
651 static void turnaround_delay(
const struct stir_cb *stir,
long us)
660 if (now.tv_sec - stir->
rx_time.tv_sec > 0)
662 us -= now.tv_usec - stir->
rx_time.tv_usec;
666 ticks = us / (1000000 /
HZ);
677 static int receive_start(
struct stir_cb *stir)
690 static void receive_stop(
struct stir_cb *stir)
696 stir->
netdev->stats.collisions++;
709 turnaround_delay(stir, irda_get_mtt(skb));
713 if (isfir(stir->
speed))
714 wraplen = wrap_fir_skb(skb, stir->
io_buf);
716 wraplen = wrap_sir_skb(skb, stir->
io_buf);
720 fifo_txwait(stir, wraplen);
722 stir->
netdev->stats.tx_packets++;
723 stir->
netdev->stats.tx_bytes += skb->
len;
730 stir->
netdev->stats.tx_errors++;
736 static int stir_transmit_thread(
void *
arg)
749 fifo_txwait(stir, -1);
755 if (change_speed(stir, stir->
speed))
763 unsigned new_speed = irda_get_next_speed(skb);
764 netif_wake_queue(dev);
767 stir_send(stir, skb);
770 if ((new_speed != -1) && (stir->
speed != new_speed)) {
771 if (fifo_txwait(stir, -1) ||
772 change_speed(stir, new_speed))
780 irda_device_txqueue_empty(dev)) {
782 if (fifo_txwait(stir, -1))
785 if (
unlikely(receive_start(stir))) {
788 "%s: receive usb submit failed\n",
810 static void stir_rcv_irq(
struct urb *
urb)
812 struct stir_cb *stir = urb->context;
816 if (!netif_running(stir->
netdev))
820 if (urb->status != 0)
823 if (urb->actual_length > 0) {
824 pr_debug(
"receive %d\n", urb->actual_length);
825 unwrap_chars(stir, urb->transfer_buffer,
852 static int stir_net_open(
struct net_device *netdev)
854 struct stir_cb *stir = netdev_priv(netdev);
865 err = change_speed(stir, 9600);
891 usb_rcvbulkpipe(stir->
usbdev, 2),
913 "%s", stir->
netdev->name);
914 if (IS_ERR(stir->
thread)) {
915 err = PTR_ERR(stir->
thread);
916 dev_err(&stir->
usbdev->dev,
"unable to start kernel thread\n");
920 netif_start_queue(netdev);
944 static int stir_net_close(
struct net_device *netdev)
946 struct stir_cb *stir = netdev_priv(netdev);
949 netif_stop_queue(netdev);
977 struct stir_cb *stir = netdev_priv(netdev);
986 if (netif_device_present(stir->
netdev))
987 ret = change_speed(stir, irq->ifr_baudrate);
995 if (netif_running(stir->
netdev))
1012 .ndo_open = stir_net_open,
1013 .ndo_stop = stir_net_close,
1014 .ndo_start_xmit = stir_hard_xmit,
1015 .ndo_do_ioctl = stir_net_ioctl,
1028 struct usb_device *dev = interface_to_usbdev(intf);
1039 stir = netdev_priv(net);
1045 dev_err(&intf->dev,
"usb reset configuration failed\n");
1050 "Vendor: %x, Product: %x\n",
1051 dev->devnum,
le16_to_cpu(dev->descriptor.idVendor),
1061 stir->
qos.min_turn_time.bits &= qos_mtt_bits;
1071 dev_info(&intf->dev,
"IrDA: Registered SigmaTel device %s\n",
1074 usb_set_intfdata(intf, stir);
1089 struct stir_cb *stir = usb_get_intfdata(intf);
1097 usb_set_intfdata(intf,
NULL);
1104 struct stir_cb *stir = usb_get_intfdata(intf);
1113 struct stir_cb *stir = usb_get_intfdata(intf);
1125 static struct usb_driver irda_driver = {
1127 .probe = stir_probe,
1128 .disconnect = stir_disconnect,
1129 .id_table = dongles,
1131 .suspend = stir_suspend,
1132 .resume = stir_resume,