1 /*****************************************************************************
5 * Description: Irda SigmaTel USB Dongle
7 * Author: Stephen Hemminger <shemminger@osdl.org>
9 * Based on earlier driver by Paul Stewart <stewart@parc.com>
11 * Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>
12 * Copyright (C) 2001, Dag Brattli <dag@brattli.net>
13 * Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>
14 * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 *****************************************************************************/
33 * This dongle does no framing, and requires polling to receive the
34 * data. The STIr4200 has bulk in and out endpoints just like
35 * usr-irda devices, but the data it sends and receives is raw; like
36 * irtty, it needs to call the wrap and unwrap functions to add and
37 * remove SOF/BOF and escape characters to/from the frame.
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
43 #include <linux/kernel.h>
44 #include <linux/types.h>
45 #include <linux/init.h>
46 #include <linux/time.h>
47 #include <linux/skbuff.h>
48 #include <linux/netdevice.h>
49 #include <linux/suspend.h>
50 #include <linux/slab.h>
51 #include <linux/delay.h>
52 #include <linux/usb.h>
53 #include <linux/crc32.h>
54 #include <net/irda/irda.h>
55 #include <net/irda/irlap.h>
56 #include <net/irda/irda_device.h>
57 #include <net/irda/wrapper.h>
58 #include <net/irda/crc.h>
59 #include <asm/byteorder.h>
60 #include <asm/unaligned.h>
62 MODULE_AUTHOR("Stephen Hemminger <shemminger@osdl.org>");
63 MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200");
64 MODULE_LICENSE("GPL");
66 static int qos_mtt_bits = 0x07; /* 1 ms or more */
67 module_param(qos_mtt_bits, int, 0);
68 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
70 static int rx_sensitivity = 1; /* FIR 0..4, SIR 0..6 */
71 module_param(rx_sensitivity, int, 0);
72 MODULE_PARM_DESC(rx_sensitivity, "Set Receiver sensitivity (0-6, 0 is most sensitive)");
74 static int tx_power = 0; /* 0 = highest ... 3 = lowest */
75 module_param(tx_power, int, 0);
76 MODULE_PARM_DESC(tx_power, "Set Transmitter power (0-3, 0 is highest power)");
78 #define STIR_IRDA_HEADER 4
79 #define CTRL_TIMEOUT 100 /* milliseconds */
80 #define TRANSMIT_TIMEOUT 200 /* milliseconds */
81 #define STIR_FIFO_SIZE 4096
82 #define FIFO_REGS_SIZE 3
94 REQ_WRITE_SINGLE = 0x03,
97 /* Register offsets */
123 PDCLK_4000000 = 0x02,
141 CTRL2_SPWIDTH = 0x08,
145 enum StirFifoCtlMask {
147 FIFOCTL_UNDER = 0x40,
151 FIFOCTL_EMPTY = 0x04,
152 FIFOCTL_RXERR = 0x02,
153 FIFOCTL_TXERR = 0x01,
170 struct usb_device *usbdev; /* init: probe_irda */
171 struct net_device *netdev; /* network layer */
172 struct irlap_cb *irlap; /* The link layer we are binded to */
173 struct net_device_stats stats; /* network statistics */
175 unsigned speed; /* Current speed */
177 wait_queue_head_t thr_wait; /* transmit thread wakeup */
178 struct completion thr_exited;
181 struct sk_buff *tx_pending;
182 void *io_buf; /* transmit/receive buffer */
185 iobuff_t rx_buff; /* receive unwrap state machine */
186 struct timeval rx_time;
192 /* These are the currently known USB ids */
193 static struct usb_device_id dongles[] = {
194 /* SigmaTel, Inc, STIr4200 IrDA/USB Bridge */
195 { USB_DEVICE(0x066f, 0x4200) },
199 MODULE_DEVICE_TABLE(usb, dongles);
201 /* Send control message to set dongle register */
202 static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value)
204 struct usb_device *dev = stir->usbdev;
206 pr_debug("%s: write reg %d = 0x%x\n",
207 stir->netdev->name, reg, value);
208 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
210 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
212 msecs_to_jiffies(CTRL_TIMEOUT));
215 /* Send control message to read multiple registers */
216 static inline int read_reg(struct stir_cb *stir, __u16 reg,
217 __u8 *data, __u16 count)
219 struct usb_device *dev = stir->usbdev;
221 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
223 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
225 msecs_to_jiffies(CTRL_TIMEOUT));
228 static inline int isfir(u32 speed)
230 return (speed == 4000000);
234 * Prepare a FIR IrDA frame for transmission to the USB dongle. The
235 * FIR transmit frame is documented in the datasheet. It consists of
236 * a two byte 0x55 0xAA sequence, two little-endian length bytes, a
237 * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E,
238 * then the data escaped as follows:
244 * Then, 4 bytes of little endian (stuffed) FCS follow, then two
245 * trailing EOF bytes of 0x7E.
247 static inline __u8 *stuff_fir(__u8 *p, __u8 c)
262 /* Take raw data in skb and put it wrapped into buf */
263 static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
266 __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
274 ptr = buf + STIR_IRDA_HEADER;
275 memset(ptr, 0x7f, 16);
282 /* Address / Control / Information */
283 for (i = 0; i < skb->len; i++)
284 ptr = stuff_fir(ptr, skb->data[i]);
287 ptr = stuff_fir(ptr, fcs & 0xff);
288 ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
289 ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
290 ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);
296 /* Total length, minus the header */
297 wraplen = (ptr - buf) - STIR_IRDA_HEADER;
298 buf[2] = wraplen & 0xff;
299 buf[3] = (wraplen >> 8) & 0xff;
301 return wraplen + STIR_IRDA_HEADER;
304 static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf)
308 wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,
309 STIR_FIFO_SIZE - STIR_IRDA_HEADER);
312 buf[2] = wraplen & 0xff;
313 buf[3] = (wraplen >> 8) & 0xff;
315 return wraplen + STIR_IRDA_HEADER;
319 * Frame is fully formed in the rx_buff so check crc
320 * and pass up to irlap
321 * setup for next receive
323 static void fir_eof(struct stir_cb *stir)
325 iobuff_t *rx_buff = &stir->rx_buff;
326 int len = rx_buff->len - 4;
327 struct sk_buff *skb, *nskb;
330 if (unlikely(len <= 0)) {
331 pr_debug("%s: short frame len %d\n",
332 stir->netdev->name, len);
334 ++stir->stats.rx_errors;
335 ++stir->stats.rx_length_errors;
339 fcs = ~(crc32_le(~0, rx_buff->data, len));
340 if (fcs != le32_to_cpu(get_unaligned((u32 *)(rx_buff->data+len)))) {
341 pr_debug("crc error calc 0x%x len %d\n", fcs, len);
342 stir->stats.rx_errors++;
343 stir->stats.rx_crc_errors++;
347 /* if frame is short then just copy it */
348 if (len < IRDA_RX_COPY_THRESHOLD) {
349 nskb = dev_alloc_skb(len + 1);
350 if (unlikely(!nskb)) {
351 ++stir->stats.rx_dropped;
354 skb_reserve(nskb, 1);
356 memcpy(nskb->data, rx_buff->data, len);
358 nskb = dev_alloc_skb(rx_buff->truesize);
359 if (unlikely(!nskb)) {
360 ++stir->stats.rx_dropped;
363 skb_reserve(nskb, 1);
366 rx_buff->head = nskb->data;
371 skb->mac.raw = skb->data;
372 skb->protocol = htons(ETH_P_IRDA);
373 skb->dev = stir->netdev;
377 stir->stats.rx_packets++;
378 stir->stats.rx_bytes += len;
380 rx_buff->data = rx_buff->head;
384 /* Unwrap FIR stuffed data and bump it to IrLAP */
385 static void stir_fir_chars(struct stir_cb *stir,
386 const __u8 *bytes, int len)
388 iobuff_t *rx_buff = &stir->rx_buff;
391 for (i = 0; i < len; i++) {
392 __u8 byte = bytes[i];
394 switch(rx_buff->state) {
396 /* ignore garbage till start of frame */
397 if (unlikely(byte != FIR_EOF))
399 /* Now receiving frame */
400 rx_buff->state = BEGIN_FRAME;
402 /* Time to initialize receive buffer */
403 rx_buff->data = rx_buff->head;
408 if (byte == FIR_EOF) {
409 pr_debug("%s: got EOF after escape\n",
413 rx_buff->state = INSIDE_FRAME;
418 /* ignore multiple BOF/EOF */
421 rx_buff->state = INSIDE_FRAME;
422 rx_buff->in_frame = TRUE;
428 rx_buff->state = LINK_ESCAPE;
431 /* 0x7f is not used in this framing */
432 pr_debug("%s: got XBOF without escape\n",
436 rx_buff->state = OUTSIDE_FRAME;
437 rx_buff->in_frame = FALSE;
444 /* add byte to rx buffer */
445 if (unlikely(rx_buff->len >= rx_buff->truesize)) {
446 pr_debug("%s: fir frame exceeds %d\n",
447 stir->netdev->name, rx_buff->truesize);
448 ++stir->stats.rx_over_errors;
452 rx_buff->data[rx_buff->len++] = byte;
456 ++stir->stats.rx_frame_errors;
459 ++stir->stats.rx_errors;
460 rx_buff->state = OUTSIDE_FRAME;
461 rx_buff->in_frame = FALSE;
465 /* Unwrap SIR stuffed data and bump it up to IrLAP */
466 static void stir_sir_chars(struct stir_cb *stir,
467 const __u8 *bytes, int len)
471 for (i = 0; i < len; i++)
472 async_unwrap_char(stir->netdev, &stir->stats,
473 &stir->rx_buff, bytes[i]);
476 static inline void unwrap_chars(struct stir_cb *stir,
477 const __u8 *bytes, int length)
479 if (isfir(stir->speed))
480 stir_fir_chars(stir, bytes, length);
482 stir_sir_chars(stir, bytes, length);
485 /* Mode parameters for each speed */
486 static const struct {
490 { 2400, PDCLK_2400 },
491 { 9600, PDCLK_9600 },
492 { 19200, PDCLK_19200 },
493 { 38400, PDCLK_38400 },
494 { 57600, PDCLK_57600 },
495 { 115200, PDCLK_115200 },
496 { 4000000, PDCLK_4000000 },
501 * Setup chip for speed.
502 * Called at startup to initialize the chip
503 * and on speed changes.
505 * Note: Write multiple registers doesn't appear to work
507 static int change_speed(struct stir_cb *stir, unsigned speed)
512 for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {
513 if (speed == stir_modes[i].speed)
517 warn("%s: invalid speed %d", stir->netdev->name, speed);
521 pr_debug("speed change from %d to %d\n", stir->speed, speed);
523 /* sometimes needed to get chip out of stuck state */
524 err = usb_reset_device(stir->usbdev);
528 /* Reset modulator */
529 err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);
533 /* Undocumented magic to tweak the DPLL */
534 err = write_reg(stir, REG_DPLL, 0x15);
539 err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);
543 mode = MODE_NRESET | MODE_FASTRX;
545 mode |= MODE_FIR | MODE_FFRSTEN;
552 err = write_reg(stir, REG_MODE, mode);
556 /* This resets TEMIC style transceiver if any. */
557 err = write_reg(stir, REG_CTRL1,
558 CTRL1_SDMODE | (tx_power & 3) << 1);
562 err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);
566 /* Reset sensitivity */
567 err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5);
574 * Called from net/core when new frame is available.
576 static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
578 struct stir_cb *stir = netdev->priv;
580 netif_stop_queue(netdev);
582 /* the IRDA wrapping routines don't deal with non linear skb */
583 SKB_LINEAR_ASSERT(skb);
585 skb = xchg(&stir->tx_pending, skb);
586 wake_up(&stir->thr_wait);
588 /* this should never happen unless stop/wakeup problem */
598 * Wait for the transmit FIFO to have space for next data
600 * If space < 0 then wait till FIFO completely drains.
601 * FYI: can take up to 13 seconds at 2400baud.
603 static int fifo_txwait(struct stir_cb *stir, int space)
606 unsigned long count, status;
608 /* Read FIFO status and count */
610 err = read_reg(stir, REG_FIFOCTL, stir->fifo_status,
612 if (unlikely(err != FIFO_REGS_SIZE)) {
613 warn("%s: FIFO register read error: %d",
614 stir->netdev->name, err);
619 status = stir->fifo_status[0];
620 count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8
621 | stir->fifo_status[1];
623 pr_debug("fifo status 0x%lx count %lu\n", status, count);
625 /* error when receive/transmit fifo gets confused */
626 if (status & FIFOCTL_RXERR) {
627 stir->stats.rx_fifo_errors++;
628 stir->stats.rx_errors++;
632 if (status & FIFOCTL_TXERR) {
633 stir->stats.tx_fifo_errors++;
634 stir->stats.tx_errors++;
638 /* is fifo receiving already, or empty */
639 if (!(status & FIFOCTL_DIR)
640 || (status & FIFOCTL_EMPTY))
643 if (signal_pending(current))
647 if (!netif_running(stir->netdev)
648 || !netif_device_present(stir->netdev))
651 /* only waiting for some space */
652 if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
655 /* estimate transfer time for remaining chars */
656 msleep((count * 8000) / stir->speed);
659 err = write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR);
662 err = write_reg(stir, REG_FIFOCTL, 0);
670 /* Wait for turnaround delay before starting transmit. */
671 static void turnaround_delay(const struct stir_cb *stir, long us)
679 do_gettimeofday(&now);
680 us -= (now.tv_sec - stir->rx_time.tv_sec) * USEC_PER_SEC;
681 us -= now.tv_usec - stir->rx_time.tv_usec;
685 ticks = us / (1000000 / HZ);
687 current->state = TASK_INTERRUPTIBLE;
688 schedule_timeout(1 + ticks);
694 * Start receiver by submitting a request to the receive pipe.
695 * If nothing is available it will return after rx_interval.
697 static int receive_start(struct stir_cb *stir)
702 stir->rx_buff.in_frame = FALSE;
703 stir->rx_buff.state = OUTSIDE_FRAME;
705 stir->rx_urb->status = 0;
706 return usb_submit_urb(stir->rx_urb, GFP_KERNEL);
709 /* Stop all pending receive Urb's */
710 static void receive_stop(struct stir_cb *stir)
713 usb_unlink_urb(stir->rx_urb);
715 if (stir->rx_buff.in_frame)
716 stir->stats.collisions++;
719 * Wrap data in socket buffer and send it.
721 static void stir_send(struct stir_cb *stir, struct sk_buff *skb)
726 /* if receiving, need to turnaround */
727 if (stir->receiving) {
729 turnaround_delay(stir, irda_get_mtt(skb));
733 if (isfir(stir->speed))
734 wraplen = wrap_fir_skb(skb, stir->io_buf);
736 wraplen = wrap_sir_skb(skb, stir->io_buf);
738 /* check for space available in fifo */
740 fifo_txwait(stir, wraplen);
742 stir->stats.tx_packets++;
743 stir->stats.tx_bytes += skb->len;
744 stir->netdev->trans_start = jiffies;
745 pr_debug("send %d (%d)\n", skb->len, wraplen);
747 if (usb_bulk_msg(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1),
748 stir->io_buf, wraplen,
749 NULL, msecs_to_jiffies(TRANSMIT_TIMEOUT)))
750 stir->stats.tx_errors++;
754 * Transmit state machine thread
756 static int stir_transmit_thread(void *arg)
758 struct stir_cb *stir = arg;
759 struct net_device *dev = stir->netdev;
762 daemonize("%s", dev->name);
763 allow_signal(SIGTERM);
765 while (netif_running(dev)
766 && netif_device_present(dev)
767 && !signal_pending(current))
769 /* if suspending, then power off and wait */
770 if (current->flags & PF_FREEZE) {
774 fifo_txwait(stir, -1);
776 write_reg(stir, REG_CTRL1, CTRL1_TXPWD|CTRL1_RXPWD);
778 refrigerator(PF_FREEZE);
780 if (change_speed(stir, stir->speed))
784 /* if something to send? */
785 skb = xchg(&stir->tx_pending, NULL);
787 unsigned new_speed = irda_get_next_speed(skb);
788 netif_wake_queue(dev);
791 stir_send(stir, skb);
794 if (stir->speed != new_speed) {
795 if (fifo_txwait(stir, -1) ||
796 change_speed(stir, new_speed))
802 /* nothing to send? start receiving */
804 && irda_device_txqueue_empty(dev)) {
805 /* Wait otherwise chip gets confused. */
806 if (fifo_txwait(stir, -1))
809 if (unlikely(receive_start(stir))) {
811 info("%s: receive usb submit failed",
819 /* sleep if nothing to send */
820 wait_event_interruptible(stir->thr_wait, stir->tx_pending);
823 complete_and_exit (&stir->thr_exited, 0);
828 * USB bulk receive completion callback.
829 * Wakes up every ms (usb round trip) with wrapped
832 static void stir_rcv_irq(struct urb *urb, struct pt_regs *regs)
834 struct stir_cb *stir = urb->context;
837 /* in process of stopping, just drop data */
838 if (!netif_running(stir->netdev))
841 /* unlink, shutdown, unplug, other nasties */
842 if (urb->status != 0)
845 if (urb->actual_length > 0) {
846 pr_debug("receive %d\n", urb->actual_length);
847 unwrap_chars(stir, urb->transfer_buffer,
850 stir->netdev->last_rx = jiffies;
851 do_gettimeofday(&stir->rx_time);
854 /* kernel thread is stopping receiver don't resubmit */
855 if (!stir->receiving)
858 /* resubmit existing urb */
859 err = usb_submit_urb(urb, GFP_ATOMIC);
861 /* in case of error, the kernel thread will restart us */
863 warn("%s: usb receive submit error: %d",
864 stir->netdev->name, err);
866 wake_up(&stir->thr_wait);
871 * Function stir_net_open (dev)
873 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
875 static int stir_net_open(struct net_device *netdev)
877 struct stir_cb *stir = netdev->priv;
881 err = usb_clear_halt(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1));
884 err = usb_clear_halt(stir->usbdev, usb_rcvbulkpipe(stir->usbdev, 2));
888 err = change_speed(stir, 9600);
894 /* Initialize for SIR/FIR to copy data directly into skb. */
896 stir->rx_buff.truesize = IRDA_SKB_MAX_MTU;
897 stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
898 if (!stir->rx_buff.skb)
901 skb_reserve(stir->rx_buff.skb, 1);
902 stir->rx_buff.head = stir->rx_buff.skb->data;
903 do_gettimeofday(&stir->rx_time);
905 stir->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
909 stir->io_buf = kmalloc(STIR_FIFO_SIZE, GFP_KERNEL);
913 usb_fill_bulk_urb(stir->rx_urb, stir->usbdev,
914 usb_rcvbulkpipe(stir->usbdev, 2),
915 stir->io_buf, STIR_FIFO_SIZE,
918 stir->fifo_status = kmalloc(FIFO_REGS_SIZE, GFP_KERNEL);
919 if (!stir->fifo_status)
923 * Now that everything should be initialized properly,
924 * Open new IrLAP layer instance to take care of us...
925 * Note : will send immediately a speed change...
927 sprintf(hwname, "usb#%d", stir->usbdev->devnum);
928 stir->irlap = irlap_open(netdev, &stir->qos, hwname);
930 err("irlap_open failed");
934 /** Start kernel thread for transmit. */
935 stir->thr_pid = kernel_thread(stir_transmit_thread, stir,
936 CLONE_FS|CLONE_FILES);
937 if (stir->thr_pid < 0) {
939 err("unable to start kernel thread");
943 netif_start_queue(netdev);
948 irlap_close(stir->irlap);
950 kfree(stir->fifo_status);
954 usb_free_urb(stir->rx_urb);
956 kfree_skb(stir->rx_buff.skb);
962 * Function stir_net_close (stir)
964 * Network device is taken down. Usually this is done by
965 * "ifconfig irda0 down"
967 static int stir_net_close(struct net_device *netdev)
969 struct stir_cb *stir = netdev->priv;
971 /* Stop transmit processing */
972 netif_stop_queue(netdev);
974 /* Kill transmit thread */
975 kill_proc(stir->thr_pid, SIGTERM, 1);
976 wait_for_completion(&stir->thr_exited);
977 kfree(stir->fifo_status);
979 /* Mop up receive urb's */
980 usb_unlink_urb(stir->rx_urb);
983 usb_free_urb(stir->rx_urb);
984 kfree_skb(stir->rx_buff.skb);
986 /* Stop and remove instance of IrLAP */
988 irlap_close(stir->irlap);
996 * IOCTLs : Extra out-of-band network commands...
998 static int stir_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1000 struct if_irda_req *irq = (struct if_irda_req *) rq;
1001 struct stir_cb *stir = dev->priv;
1005 case SIOCSBANDWIDTH: /* Set bandwidth */
1006 if (!capable(CAP_NET_ADMIN))
1009 /* Check if the device is still there */
1010 if (netif_device_present(stir->netdev))
1011 ret = change_speed(stir, irq->ifr_baudrate);
1014 case SIOCSMEDIABUSY: /* Set media busy */
1015 if (!capable(CAP_NET_ADMIN))
1018 /* Check if the IrDA stack is still there */
1019 if (netif_running(stir->netdev))
1020 irda_device_set_media_busy(stir->netdev, TRUE);
1023 case SIOCGRECEIVING:
1024 /* Only approximately true */
1025 irq->ifr_receiving = stir->receiving;
1036 * Get device stats (for /proc/net/dev and ifconfig)
1038 static struct net_device_stats *stir_net_get_stats(struct net_device *dev)
1040 struct stir_cb *stir = dev->priv;
1041 return &stir->stats;
1045 * This routine is called by the USB subsystem for each new device
1046 * in the system. We need to check if the device is ours, and in
1047 * this case start handling it.
1048 * Note : it might be worth protecting this function by a global
1049 * spinlock... Or not, because maybe USB already deal with that...
1051 static int stir_probe(struct usb_interface *intf,
1052 const struct usb_device_id *id)
1054 struct usb_device *dev = interface_to_usbdev(intf);
1055 struct stir_cb *stir = NULL;
1056 struct net_device *net;
1059 /* Allocate network device container. */
1060 net = alloc_irdadev(sizeof(*stir));
1064 SET_MODULE_OWNER(net);
1065 SET_NETDEV_DEV(net, &intf->dev);
1070 ret = usb_reset_configuration(dev);
1072 err("usb reset configuration failed");
1076 printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
1077 "Vendor: %x, Product: %x\n",
1078 dev->devnum, dev->descriptor.idVendor,
1079 dev->descriptor.idProduct);
1081 /* Initialize QoS for this device */
1082 irda_init_max_qos_capabilies(&stir->qos);
1084 /* That's the Rx capability. */
1085 stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 |
1086 IR_38400 | IR_57600 | IR_115200 |
1088 stir->qos.min_turn_time.bits &= qos_mtt_bits;
1089 irda_qos_bits_to_value(&stir->qos);
1091 init_completion (&stir->thr_exited);
1092 init_waitqueue_head (&stir->thr_wait);
1094 /* Override the network functions we need to use */
1095 net->hard_start_xmit = stir_hard_xmit;
1096 net->open = stir_net_open;
1097 net->stop = stir_net_close;
1098 net->get_stats = stir_net_get_stats;
1099 net->do_ioctl = stir_net_ioctl;
1101 ret = register_netdev(net);
1105 MESSAGE("IrDA: Registered SigmaTel device %s\n", net->name);
1107 usb_set_intfdata(intf, stir);
1118 * The current device is removed, the USB layer tell us to shut it down...
1120 static void stir_disconnect(struct usb_interface *intf)
1122 struct stir_cb *stir = usb_get_intfdata(intf);
1127 unregister_netdev(stir->netdev);
1128 free_netdev(stir->netdev);
1130 usb_set_intfdata(intf, NULL);
1134 /* Power management suspend, so power off the transmitter/receiver */
1135 static int stir_suspend(struct usb_interface *intf, u32 state)
1137 struct stir_cb *stir = usb_get_intfdata(intf);
1139 netif_device_detach(stir->netdev);
1143 /* Coming out of suspend, so reset hardware */
1144 static int stir_resume(struct usb_interface *intf)
1146 struct stir_cb *stir = usb_get_intfdata(intf);
1148 netif_device_attach(stir->netdev);
1150 /* receiver restarted when send thread wakes up */
1155 * USB device callbacks
1157 static struct usb_driver irda_driver = {
1158 .owner = THIS_MODULE,
1160 .probe = stir_probe,
1161 .disconnect = stir_disconnect,
1162 .id_table = dongles,
1163 .suspend = stir_suspend,
1164 .resume = stir_resume,
1170 static int __init stir_init(void)
1172 if (usb_register(&irda_driver) < 0)
1175 MESSAGE("SigmaTel support registered\n");
1178 module_init(stir_init);
1183 static void __exit stir_cleanup(void)
1185 /* Deregister the driver and remove all pending instances */
1186 usb_deregister(&irda_driver);
1188 module_exit(stir_cleanup);