[PATCH] USB: remove .owner field from struct usb_driver
[linux-2.6/btrfs-unstable.git] / drivers / net / irda / stir4200.c
blob31867e4b891b1e582e2490638cc417894337e736
1 /*****************************************************************************
3 * Filename: stir4200.c
4 * Version: 0.4
5 * Description: Irda SigmaTel USB Dongle
6 * Status: Experimental
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/slab.h>
50 #include <linux/delay.h>
51 #include <linux/usb.h>
52 #include <linux/crc32.h>
53 #include <net/irda/irda.h>
54 #include <net/irda/irlap.h>
55 #include <net/irda/irda_device.h>
56 #include <net/irda/wrapper.h>
57 #include <net/irda/crc.h>
58 #include <asm/byteorder.h>
59 #include <asm/unaligned.h>
61 MODULE_AUTHOR("Stephen Hemminger <shemminger@osdl.org>");
62 MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200");
63 MODULE_LICENSE("GPL");
65 static int qos_mtt_bits = 0x07; /* 1 ms or more */
66 module_param(qos_mtt_bits, int, 0);
67 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
69 static int rx_sensitivity = 1; /* FIR 0..4, SIR 0..6 */
70 module_param(rx_sensitivity, int, 0);
71 MODULE_PARM_DESC(rx_sensitivity, "Set Receiver sensitivity (0-6, 0 is most sensitive)");
73 static int tx_power = 0; /* 0 = highest ... 3 = lowest */
74 module_param(tx_power, int, 0);
75 MODULE_PARM_DESC(tx_power, "Set Transmitter power (0-3, 0 is highest power)");
77 #define STIR_IRDA_HEADER 4
78 #define CTRL_TIMEOUT 100 /* milliseconds */
79 #define TRANSMIT_TIMEOUT 200 /* milliseconds */
80 #define STIR_FIFO_SIZE 4096
81 #define FIFO_REGS_SIZE 3
83 enum FirChars {
84 FIR_CE = 0x7d,
85 FIR_XBOF = 0x7f,
86 FIR_EOF = 0x7e,
89 enum StirRequests {
90 REQ_WRITE_REG = 0x00,
91 REQ_READ_REG = 0x01,
92 REQ_READ_ROM = 0x02,
93 REQ_WRITE_SINGLE = 0x03,
96 /* Register offsets */
97 enum StirRegs {
98 REG_RSVD=0,
99 REG_MODE,
100 REG_PDCLK,
101 REG_CTRL1,
102 REG_CTRL2,
103 REG_FIFOCTL,
104 REG_FIFOLSB,
105 REG_FIFOMSB,
106 REG_DPLL,
107 REG_IRDIG,
108 REG_TEST=15,
111 enum StirModeMask {
112 MODE_FIR = 0x80,
113 MODE_SIR = 0x20,
114 MODE_ASK = 0x10,
115 MODE_FASTRX = 0x08,
116 MODE_FFRSTEN = 0x04,
117 MODE_NRESET = 0x02,
118 MODE_2400 = 0x01,
121 enum StirPdclkMask {
122 PDCLK_4000000 = 0x02,
123 PDCLK_115200 = 0x09,
124 PDCLK_57600 = 0x13,
125 PDCLK_38400 = 0x1D,
126 PDCLK_19200 = 0x3B,
127 PDCLK_9600 = 0x77,
128 PDCLK_2400 = 0xDF,
131 enum StirCtrl1Mask {
132 CTRL1_SDMODE = 0x80,
133 CTRL1_RXSLOW = 0x40,
134 CTRL1_TXPWD = 0x10,
135 CTRL1_RXPWD = 0x08,
136 CTRL1_SRESET = 0x01,
139 enum StirCtrl2Mask {
140 CTRL2_SPWIDTH = 0x08,
141 CTRL2_REVID = 0x03,
144 enum StirFifoCtlMask {
145 FIFOCTL_EOF = 0x80,
146 FIFOCTL_UNDER = 0x40,
147 FIFOCTL_OVER = 0x20,
148 FIFOCTL_DIR = 0x10,
149 FIFOCTL_CLR = 0x08,
150 FIFOCTL_EMPTY = 0x04,
151 FIFOCTL_RXERR = 0x02,
152 FIFOCTL_TXERR = 0x01,
155 enum StirDiagMask {
156 IRDIG_RXHIGH = 0x80,
157 IRDIG_RXLOW = 0x40,
160 enum StirTestMask {
161 TEST_PLLDOWN = 0x80,
162 TEST_LOOPIR = 0x40,
163 TEST_LOOPUSB = 0x20,
164 TEST_TSTENA = 0x10,
165 TEST_TSTOSC = 0x0F,
168 struct stir_cb {
169 struct usb_device *usbdev; /* init: probe_irda */
170 struct net_device *netdev; /* network layer */
171 struct irlap_cb *irlap; /* The link layer we are binded to */
172 struct net_device_stats stats; /* network statistics */
173 struct qos_info qos;
174 unsigned speed; /* Current speed */
176 wait_queue_head_t thr_wait; /* transmit thread wakeup */
177 struct completion thr_exited;
178 pid_t thr_pid;
180 struct sk_buff *tx_pending;
181 void *io_buf; /* transmit/receive buffer */
182 __u8 *fifo_status;
184 iobuff_t rx_buff; /* receive unwrap state machine */
185 struct timeval rx_time;
186 int receiving;
187 struct urb *rx_urb;
191 /* These are the currently known USB ids */
192 static struct usb_device_id dongles[] = {
193 /* SigmaTel, Inc, STIr4200 IrDA/USB Bridge */
194 { USB_DEVICE(0x066f, 0x4200) },
198 MODULE_DEVICE_TABLE(usb, dongles);
200 /* Send control message to set dongle register */
201 static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value)
203 struct usb_device *dev = stir->usbdev;
205 pr_debug("%s: write reg %d = 0x%x\n",
206 stir->netdev->name, reg, value);
207 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
208 REQ_WRITE_SINGLE,
209 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
210 value, reg, NULL, 0,
211 CTRL_TIMEOUT);
214 /* Send control message to read multiple registers */
215 static inline int read_reg(struct stir_cb *stir, __u16 reg,
216 __u8 *data, __u16 count)
218 struct usb_device *dev = stir->usbdev;
220 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
221 REQ_READ_REG,
222 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
223 0, reg, data, count,
224 CTRL_TIMEOUT);
227 static inline int isfir(u32 speed)
229 return (speed == 4000000);
233 * Prepare a FIR IrDA frame for transmission to the USB dongle. The
234 * FIR transmit frame is documented in the datasheet. It consists of
235 * a two byte 0x55 0xAA sequence, two little-endian length bytes, a
236 * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E,
237 * then the data escaped as follows:
239 * 0x7D -> 0x7D 0x5D
240 * 0x7E -> 0x7D 0x5E
241 * 0x7F -> 0x7D 0x5F
243 * Then, 4 bytes of little endian (stuffed) FCS follow, then two
244 * trailing EOF bytes of 0x7E.
246 static inline __u8 *stuff_fir(__u8 *p, __u8 c)
248 switch(c) {
249 case 0x7d:
250 case 0x7e:
251 case 0x7f:
252 *p++ = 0x7d;
253 c ^= IRDA_TRANS;
254 /* fall through */
255 default:
256 *p++ = c;
258 return p;
261 /* Take raw data in skb and put it wrapped into buf */
262 static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
264 __u8 *ptr = buf;
265 __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
266 __u16 wraplen;
267 int i;
269 /* Header */
270 buf[0] = 0x55;
271 buf[1] = 0xAA;
273 ptr = buf + STIR_IRDA_HEADER;
274 memset(ptr, 0x7f, 16);
275 ptr += 16;
277 /* BOF */
278 *ptr++ = 0x7e;
279 *ptr++ = 0x7e;
281 /* Address / Control / Information */
282 for (i = 0; i < skb->len; i++)
283 ptr = stuff_fir(ptr, skb->data[i]);
285 /* FCS */
286 ptr = stuff_fir(ptr, fcs & 0xff);
287 ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
288 ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
289 ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);
291 /* EOFs */
292 *ptr++ = 0x7e;
293 *ptr++ = 0x7e;
295 /* Total length, minus the header */
296 wraplen = (ptr - buf) - STIR_IRDA_HEADER;
297 buf[2] = wraplen & 0xff;
298 buf[3] = (wraplen >> 8) & 0xff;
300 return wraplen + STIR_IRDA_HEADER;
303 static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf)
305 __u16 wraplen;
307 wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,
308 STIR_FIFO_SIZE - STIR_IRDA_HEADER);
309 buf[0] = 0x55;
310 buf[1] = 0xAA;
311 buf[2] = wraplen & 0xff;
312 buf[3] = (wraplen >> 8) & 0xff;
314 return wraplen + STIR_IRDA_HEADER;
318 * Frame is fully formed in the rx_buff so check crc
319 * and pass up to irlap
320 * setup for next receive
322 static void fir_eof(struct stir_cb *stir)
324 iobuff_t *rx_buff = &stir->rx_buff;
325 int len = rx_buff->len - 4;
326 struct sk_buff *skb, *nskb;
327 __u32 fcs;
329 if (unlikely(len <= 0)) {
330 pr_debug("%s: short frame len %d\n",
331 stir->netdev->name, len);
333 ++stir->stats.rx_errors;
334 ++stir->stats.rx_length_errors;
335 return;
338 fcs = ~(crc32_le(~0, rx_buff->data, len));
339 if (fcs != le32_to_cpu(get_unaligned((u32 *)(rx_buff->data+len)))) {
340 pr_debug("crc error calc 0x%x len %d\n", fcs, len);
341 stir->stats.rx_errors++;
342 stir->stats.rx_crc_errors++;
343 return;
346 /* if frame is short then just copy it */
347 if (len < IRDA_RX_COPY_THRESHOLD) {
348 nskb = dev_alloc_skb(len + 1);
349 if (unlikely(!nskb)) {
350 ++stir->stats.rx_dropped;
351 return;
353 skb_reserve(nskb, 1);
354 skb = nskb;
355 memcpy(nskb->data, rx_buff->data, len);
356 } else {
357 nskb = dev_alloc_skb(rx_buff->truesize);
358 if (unlikely(!nskb)) {
359 ++stir->stats.rx_dropped;
360 return;
362 skb_reserve(nskb, 1);
363 skb = rx_buff->skb;
364 rx_buff->skb = nskb;
365 rx_buff->head = nskb->data;
368 skb_put(skb, len);
370 skb->mac.raw = skb->data;
371 skb->protocol = htons(ETH_P_IRDA);
372 skb->dev = stir->netdev;
374 netif_rx(skb);
376 stir->stats.rx_packets++;
377 stir->stats.rx_bytes += len;
379 rx_buff->data = rx_buff->head;
380 rx_buff->len = 0;
383 /* Unwrap FIR stuffed data and bump it to IrLAP */
384 static void stir_fir_chars(struct stir_cb *stir,
385 const __u8 *bytes, int len)
387 iobuff_t *rx_buff = &stir->rx_buff;
388 int i;
390 for (i = 0; i < len; i++) {
391 __u8 byte = bytes[i];
393 switch(rx_buff->state) {
394 case OUTSIDE_FRAME:
395 /* ignore garbage till start of frame */
396 if (unlikely(byte != FIR_EOF))
397 continue;
398 /* Now receiving frame */
399 rx_buff->state = BEGIN_FRAME;
401 /* Time to initialize receive buffer */
402 rx_buff->data = rx_buff->head;
403 rx_buff->len = 0;
404 continue;
406 case LINK_ESCAPE:
407 if (byte == FIR_EOF) {
408 pr_debug("%s: got EOF after escape\n",
409 stir->netdev->name);
410 goto frame_error;
412 rx_buff->state = INSIDE_FRAME;
413 byte ^= IRDA_TRANS;
414 break;
416 case BEGIN_FRAME:
417 /* ignore multiple BOF/EOF */
418 if (byte == FIR_EOF)
419 continue;
420 rx_buff->state = INSIDE_FRAME;
421 rx_buff->in_frame = TRUE;
423 /* fall through */
424 case INSIDE_FRAME:
425 switch(byte) {
426 case FIR_CE:
427 rx_buff->state = LINK_ESCAPE;
428 continue;
429 case FIR_XBOF:
430 /* 0x7f is not used in this framing */
431 pr_debug("%s: got XBOF without escape\n",
432 stir->netdev->name);
433 goto frame_error;
434 case FIR_EOF:
435 rx_buff->state = OUTSIDE_FRAME;
436 rx_buff->in_frame = FALSE;
437 fir_eof(stir);
438 continue;
440 break;
443 /* add byte to rx buffer */
444 if (unlikely(rx_buff->len >= rx_buff->truesize)) {
445 pr_debug("%s: fir frame exceeds %d\n",
446 stir->netdev->name, rx_buff->truesize);
447 ++stir->stats.rx_over_errors;
448 goto error_recovery;
451 rx_buff->data[rx_buff->len++] = byte;
452 continue;
454 frame_error:
455 ++stir->stats.rx_frame_errors;
457 error_recovery:
458 ++stir->stats.rx_errors;
459 rx_buff->state = OUTSIDE_FRAME;
460 rx_buff->in_frame = FALSE;
464 /* Unwrap SIR stuffed data and bump it up to IrLAP */
465 static void stir_sir_chars(struct stir_cb *stir,
466 const __u8 *bytes, int len)
468 int i;
470 for (i = 0; i < len; i++)
471 async_unwrap_char(stir->netdev, &stir->stats,
472 &stir->rx_buff, bytes[i]);
475 static inline void unwrap_chars(struct stir_cb *stir,
476 const __u8 *bytes, int length)
478 if (isfir(stir->speed))
479 stir_fir_chars(stir, bytes, length);
480 else
481 stir_sir_chars(stir, bytes, length);
484 /* Mode parameters for each speed */
485 static const struct {
486 unsigned speed;
487 __u8 pdclk;
488 } stir_modes[] = {
489 { 2400, PDCLK_2400 },
490 { 9600, PDCLK_9600 },
491 { 19200, PDCLK_19200 },
492 { 38400, PDCLK_38400 },
493 { 57600, PDCLK_57600 },
494 { 115200, PDCLK_115200 },
495 { 4000000, PDCLK_4000000 },
500 * Setup chip for speed.
501 * Called at startup to initialize the chip
502 * and on speed changes.
504 * Note: Write multiple registers doesn't appear to work
506 static int change_speed(struct stir_cb *stir, unsigned speed)
508 int i, err;
509 __u8 mode;
511 for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {
512 if (speed == stir_modes[i].speed)
513 goto found;
516 warn("%s: invalid speed %d", stir->netdev->name, speed);
517 return -EINVAL;
519 found:
520 pr_debug("speed change from %d to %d\n", stir->speed, speed);
522 /* Reset modulator */
523 err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);
524 if (err)
525 goto out;
527 /* Undocumented magic to tweak the DPLL */
528 err = write_reg(stir, REG_DPLL, 0x15);
529 if (err)
530 goto out;
532 /* Set clock */
533 err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);
534 if (err)
535 goto out;
537 mode = MODE_NRESET | MODE_FASTRX;
538 if (isfir(speed))
539 mode |= MODE_FIR | MODE_FFRSTEN;
540 else
541 mode |= MODE_SIR;
543 if (speed == 2400)
544 mode |= MODE_2400;
546 err = write_reg(stir, REG_MODE, mode);
547 if (err)
548 goto out;
550 /* This resets TEMIC style transceiver if any. */
551 err = write_reg(stir, REG_CTRL1,
552 CTRL1_SDMODE | (tx_power & 3) << 1);
553 if (err)
554 goto out;
556 err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);
557 if (err)
558 goto out;
560 /* Reset sensitivity */
561 err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5);
562 out:
563 stir->speed = speed;
564 return err;
568 * Called from net/core when new frame is available.
570 static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
572 struct stir_cb *stir = netdev_priv(netdev);
574 netif_stop_queue(netdev);
576 /* the IRDA wrapping routines don't deal with non linear skb */
577 SKB_LINEAR_ASSERT(skb);
579 skb = xchg(&stir->tx_pending, skb);
580 wake_up(&stir->thr_wait);
582 /* this should never happen unless stop/wakeup problem */
583 if (unlikely(skb)) {
584 WARN_ON(1);
585 dev_kfree_skb(skb);
588 return 0;
592 * Wait for the transmit FIFO to have space for next data
594 * If space < 0 then wait till FIFO completely drains.
595 * FYI: can take up to 13 seconds at 2400baud.
597 static int fifo_txwait(struct stir_cb *stir, int space)
599 int err;
600 unsigned long count, status;
602 /* Read FIFO status and count */
603 for(;;) {
604 err = read_reg(stir, REG_FIFOCTL, stir->fifo_status,
605 FIFO_REGS_SIZE);
606 if (unlikely(err != FIFO_REGS_SIZE)) {
607 warn("%s: FIFO register read error: %d",
608 stir->netdev->name, err);
610 return err;
613 status = stir->fifo_status[0];
614 count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8
615 | stir->fifo_status[1];
617 pr_debug("fifo status 0x%lx count %lu\n", status, count);
619 /* error when receive/transmit fifo gets confused */
620 if (status & FIFOCTL_RXERR) {
621 stir->stats.rx_fifo_errors++;
622 stir->stats.rx_errors++;
623 break;
626 if (status & FIFOCTL_TXERR) {
627 stir->stats.tx_fifo_errors++;
628 stir->stats.tx_errors++;
629 break;
632 /* is fifo receiving already, or empty */
633 if (!(status & FIFOCTL_DIR)
634 || (status & FIFOCTL_EMPTY))
635 return 0;
637 if (signal_pending(current))
638 return -EINTR;
640 /* shutting down? */
641 if (!netif_running(stir->netdev)
642 || !netif_device_present(stir->netdev))
643 return -ESHUTDOWN;
645 /* only waiting for some space */
646 if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
647 return 0;
649 /* estimate transfer time for remaining chars */
650 msleep((count * 8000) / stir->speed);
653 err = write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR);
654 if (err)
655 return err;
656 err = write_reg(stir, REG_FIFOCTL, 0);
657 if (err)
658 return err;
660 return 0;
664 /* Wait for turnaround delay before starting transmit. */
665 static void turnaround_delay(const struct stir_cb *stir, long us)
667 long ticks;
668 struct timeval now;
670 if (us <= 0)
671 return;
673 do_gettimeofday(&now);
674 if (now.tv_sec - stir->rx_time.tv_sec > 0)
675 us -= USEC_PER_SEC;
676 us -= now.tv_usec - stir->rx_time.tv_usec;
677 if (us < 10)
678 return;
680 ticks = us / (1000000 / HZ);
681 if (ticks > 0)
682 schedule_timeout_interruptible(1 + ticks);
683 else
684 udelay(us);
688 * Start receiver by submitting a request to the receive pipe.
689 * If nothing is available it will return after rx_interval.
691 static int receive_start(struct stir_cb *stir)
693 /* reset state */
694 stir->receiving = 1;
696 stir->rx_buff.in_frame = FALSE;
697 stir->rx_buff.state = OUTSIDE_FRAME;
699 stir->rx_urb->status = 0;
700 return usb_submit_urb(stir->rx_urb, GFP_KERNEL);
703 /* Stop all pending receive Urb's */
704 static void receive_stop(struct stir_cb *stir)
706 stir->receiving = 0;
707 usb_kill_urb(stir->rx_urb);
709 if (stir->rx_buff.in_frame)
710 stir->stats.collisions++;
713 * Wrap data in socket buffer and send it.
715 static void stir_send(struct stir_cb *stir, struct sk_buff *skb)
717 unsigned wraplen;
718 int first_frame = 0;
720 /* if receiving, need to turnaround */
721 if (stir->receiving) {
722 receive_stop(stir);
723 turnaround_delay(stir, irda_get_mtt(skb));
724 first_frame = 1;
727 if (isfir(stir->speed))
728 wraplen = wrap_fir_skb(skb, stir->io_buf);
729 else
730 wraplen = wrap_sir_skb(skb, stir->io_buf);
732 /* check for space available in fifo */
733 if (!first_frame)
734 fifo_txwait(stir, wraplen);
736 stir->stats.tx_packets++;
737 stir->stats.tx_bytes += skb->len;
738 stir->netdev->trans_start = jiffies;
739 pr_debug("send %d (%d)\n", skb->len, wraplen);
741 if (usb_bulk_msg(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1),
742 stir->io_buf, wraplen,
743 NULL, TRANSMIT_TIMEOUT))
744 stir->stats.tx_errors++;
748 * Transmit state machine thread
750 static int stir_transmit_thread(void *arg)
752 struct stir_cb *stir = arg;
753 struct net_device *dev = stir->netdev;
754 struct sk_buff *skb;
756 daemonize("%s", dev->name);
757 allow_signal(SIGTERM);
759 while (netif_running(dev)
760 && netif_device_present(dev)
761 && !signal_pending(current))
763 #ifdef CONFIG_PM
764 /* if suspending, then power off and wait */
765 if (unlikely(freezing(current))) {
766 if (stir->receiving)
767 receive_stop(stir);
768 else
769 fifo_txwait(stir, -1);
771 write_reg(stir, REG_CTRL1, CTRL1_TXPWD|CTRL1_RXPWD);
773 refrigerator();
775 if (change_speed(stir, stir->speed))
776 break;
778 #endif
780 /* if something to send? */
781 skb = xchg(&stir->tx_pending, NULL);
782 if (skb) {
783 unsigned new_speed = irda_get_next_speed(skb);
784 netif_wake_queue(dev);
786 if (skb->len > 0)
787 stir_send(stir, skb);
788 dev_kfree_skb(skb);
790 if ((new_speed != -1) && (stir->speed != new_speed)) {
791 if (fifo_txwait(stir, -1) ||
792 change_speed(stir, new_speed))
793 break;
795 continue;
798 /* nothing to send? start receiving */
799 if (!stir->receiving
800 && irda_device_txqueue_empty(dev)) {
801 /* Wait otherwise chip gets confused. */
802 if (fifo_txwait(stir, -1))
803 break;
805 if (unlikely(receive_start(stir))) {
806 if (net_ratelimit())
807 info("%s: receive usb submit failed",
808 stir->netdev->name);
809 stir->receiving = 0;
810 msleep(10);
811 continue;
815 /* sleep if nothing to send */
816 wait_event_interruptible(stir->thr_wait, stir->tx_pending);
819 complete_and_exit (&stir->thr_exited, 0);
824 * USB bulk receive completion callback.
825 * Wakes up every ms (usb round trip) with wrapped
826 * data.
828 static void stir_rcv_irq(struct urb *urb, struct pt_regs *regs)
830 struct stir_cb *stir = urb->context;
831 int err;
833 /* in process of stopping, just drop data */
834 if (!netif_running(stir->netdev))
835 return;
837 /* unlink, shutdown, unplug, other nasties */
838 if (urb->status != 0)
839 return;
841 if (urb->actual_length > 0) {
842 pr_debug("receive %d\n", urb->actual_length);
843 unwrap_chars(stir, urb->transfer_buffer,
844 urb->actual_length);
846 stir->netdev->last_rx = jiffies;
847 do_gettimeofday(&stir->rx_time);
850 /* kernel thread is stopping receiver don't resubmit */
851 if (!stir->receiving)
852 return;
854 /* resubmit existing urb */
855 err = usb_submit_urb(urb, GFP_ATOMIC);
857 /* in case of error, the kernel thread will restart us */
858 if (err) {
859 warn("%s: usb receive submit error: %d",
860 stir->netdev->name, err);
861 stir->receiving = 0;
862 wake_up(&stir->thr_wait);
867 * Function stir_net_open (dev)
869 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
871 static int stir_net_open(struct net_device *netdev)
873 struct stir_cb *stir = netdev_priv(netdev);
874 int err;
875 char hwname[16];
877 err = usb_clear_halt(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1));
878 if (err)
879 goto err_out1;
880 err = usb_clear_halt(stir->usbdev, usb_rcvbulkpipe(stir->usbdev, 2));
881 if (err)
882 goto err_out1;
884 err = change_speed(stir, 9600);
885 if (err)
886 goto err_out1;
888 err = -ENOMEM;
890 /* Initialize for SIR/FIR to copy data directly into skb. */
891 stir->receiving = 0;
892 stir->rx_buff.truesize = IRDA_SKB_MAX_MTU;
893 stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
894 if (!stir->rx_buff.skb)
895 goto err_out1;
897 skb_reserve(stir->rx_buff.skb, 1);
898 stir->rx_buff.head = stir->rx_buff.skb->data;
899 do_gettimeofday(&stir->rx_time);
901 stir->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
902 if (!stir->rx_urb)
903 goto err_out2;
905 stir->io_buf = kmalloc(STIR_FIFO_SIZE, GFP_KERNEL);
906 if (!stir->io_buf)
907 goto err_out3;
909 usb_fill_bulk_urb(stir->rx_urb, stir->usbdev,
910 usb_rcvbulkpipe(stir->usbdev, 2),
911 stir->io_buf, STIR_FIFO_SIZE,
912 stir_rcv_irq, stir);
914 stir->fifo_status = kmalloc(FIFO_REGS_SIZE, GFP_KERNEL);
915 if (!stir->fifo_status)
916 goto err_out4;
919 * Now that everything should be initialized properly,
920 * Open new IrLAP layer instance to take care of us...
921 * Note : will send immediately a speed change...
923 sprintf(hwname, "usb#%d", stir->usbdev->devnum);
924 stir->irlap = irlap_open(netdev, &stir->qos, hwname);
925 if (!stir->irlap) {
926 err("stir4200: irlap_open failed");
927 goto err_out5;
930 /** Start kernel thread for transmit. */
931 stir->thr_pid = kernel_thread(stir_transmit_thread, stir,
932 CLONE_FS|CLONE_FILES);
933 if (stir->thr_pid < 0) {
934 err = stir->thr_pid;
935 err("stir4200: unable to start kernel thread");
936 goto err_out6;
939 netif_start_queue(netdev);
941 return 0;
943 err_out6:
944 irlap_close(stir->irlap);
945 err_out5:
946 kfree(stir->fifo_status);
947 err_out4:
948 kfree(stir->io_buf);
949 err_out3:
950 usb_free_urb(stir->rx_urb);
951 err_out2:
952 kfree_skb(stir->rx_buff.skb);
953 err_out1:
954 return err;
958 * Function stir_net_close (stir)
960 * Network device is taken down. Usually this is done by
961 * "ifconfig irda0 down"
963 static int stir_net_close(struct net_device *netdev)
965 struct stir_cb *stir = netdev_priv(netdev);
967 /* Stop transmit processing */
968 netif_stop_queue(netdev);
970 /* Kill transmit thread */
971 kill_proc(stir->thr_pid, SIGTERM, 1);
972 wait_for_completion(&stir->thr_exited);
973 kfree(stir->fifo_status);
975 /* Mop up receive urb's */
976 usb_kill_urb(stir->rx_urb);
978 kfree(stir->io_buf);
979 usb_free_urb(stir->rx_urb);
980 kfree_skb(stir->rx_buff.skb);
982 /* Stop and remove instance of IrLAP */
983 if (stir->irlap)
984 irlap_close(stir->irlap);
986 stir->irlap = NULL;
988 return 0;
992 * IOCTLs : Extra out-of-band network commands...
994 static int stir_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
996 struct if_irda_req *irq = (struct if_irda_req *) rq;
997 struct stir_cb *stir = netdev_priv(netdev);
998 int ret = 0;
1000 switch (cmd) {
1001 case SIOCSBANDWIDTH: /* Set bandwidth */
1002 if (!capable(CAP_NET_ADMIN))
1003 return -EPERM;
1005 /* Check if the device is still there */
1006 if (netif_device_present(stir->netdev))
1007 ret = change_speed(stir, irq->ifr_baudrate);
1008 break;
1010 case SIOCSMEDIABUSY: /* Set media busy */
1011 if (!capable(CAP_NET_ADMIN))
1012 return -EPERM;
1014 /* Check if the IrDA stack is still there */
1015 if (netif_running(stir->netdev))
1016 irda_device_set_media_busy(stir->netdev, TRUE);
1017 break;
1019 case SIOCGRECEIVING:
1020 /* Only approximately true */
1021 irq->ifr_receiving = stir->receiving;
1022 break;
1024 default:
1025 ret = -EOPNOTSUPP;
1028 return ret;
1032 * Get device stats (for /proc/net/dev and ifconfig)
1034 static struct net_device_stats *stir_net_get_stats(struct net_device *netdev)
1036 struct stir_cb *stir = netdev_priv(netdev);
1037 return &stir->stats;
1041 * This routine is called by the USB subsystem for each new device
1042 * in the system. We need to check if the device is ours, and in
1043 * this case start handling it.
1044 * Note : it might be worth protecting this function by a global
1045 * spinlock... Or not, because maybe USB already deal with that...
1047 static int stir_probe(struct usb_interface *intf,
1048 const struct usb_device_id *id)
1050 struct usb_device *dev = interface_to_usbdev(intf);
1051 struct stir_cb *stir = NULL;
1052 struct net_device *net;
1053 int ret = -ENOMEM;
1055 /* Allocate network device container. */
1056 net = alloc_irdadev(sizeof(*stir));
1057 if(!net)
1058 goto err_out1;
1060 SET_MODULE_OWNER(net);
1061 SET_NETDEV_DEV(net, &intf->dev);
1062 stir = netdev_priv(net);
1063 stir->netdev = net;
1064 stir->usbdev = dev;
1066 ret = usb_reset_configuration(dev);
1067 if (ret != 0) {
1068 err("stir4200: usb reset configuration failed");
1069 goto err_out2;
1072 printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
1073 "Vendor: %x, Product: %x\n",
1074 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1075 le16_to_cpu(dev->descriptor.idProduct));
1077 /* Initialize QoS for this device */
1078 irda_init_max_qos_capabilies(&stir->qos);
1080 /* That's the Rx capability. */
1081 stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 |
1082 IR_38400 | IR_57600 | IR_115200 |
1083 (IR_4000000 << 8);
1084 stir->qos.min_turn_time.bits &= qos_mtt_bits;
1085 irda_qos_bits_to_value(&stir->qos);
1087 init_completion (&stir->thr_exited);
1088 init_waitqueue_head (&stir->thr_wait);
1090 /* Override the network functions we need to use */
1091 net->hard_start_xmit = stir_hard_xmit;
1092 net->open = stir_net_open;
1093 net->stop = stir_net_close;
1094 net->get_stats = stir_net_get_stats;
1095 net->do_ioctl = stir_net_ioctl;
1097 ret = register_netdev(net);
1098 if (ret != 0)
1099 goto err_out2;
1101 info("IrDA: Registered SigmaTel device %s", net->name);
1103 usb_set_intfdata(intf, stir);
1105 return 0;
1107 err_out2:
1108 free_netdev(net);
1109 err_out1:
1110 return ret;
1114 * The current device is removed, the USB layer tell us to shut it down...
1116 static void stir_disconnect(struct usb_interface *intf)
1118 struct stir_cb *stir = usb_get_intfdata(intf);
1120 if (!stir)
1121 return;
1123 unregister_netdev(stir->netdev);
1124 free_netdev(stir->netdev);
1126 usb_set_intfdata(intf, NULL);
1129 #ifdef CONFIG_PM
1130 /* USB suspend, so power off the transmitter/receiver */
1131 static int stir_suspend(struct usb_interface *intf, pm_message_t message)
1133 struct stir_cb *stir = usb_get_intfdata(intf);
1135 netif_device_detach(stir->netdev);
1136 return 0;
1139 /* Coming out of suspend, so reset hardware */
1140 static int stir_resume(struct usb_interface *intf)
1142 struct stir_cb *stir = usb_get_intfdata(intf);
1144 netif_device_attach(stir->netdev);
1146 /* receiver restarted when send thread wakes up */
1147 return 0;
1149 #endif
1152 * USB device callbacks
1154 static struct usb_driver irda_driver = {
1155 .name = "stir4200",
1156 .probe = stir_probe,
1157 .disconnect = stir_disconnect,
1158 .id_table = dongles,
1159 #ifdef CONFIG_PM
1160 .suspend = stir_suspend,
1161 .resume = stir_resume,
1162 #endif
1166 * Module insertion
1168 static int __init stir_init(void)
1170 return usb_register(&irda_driver);
1172 module_init(stir_init);
1175 * Module removal
1177 static void __exit stir_cleanup(void)
1179 /* Deregister the driver and remove all pending instances */
1180 usb_deregister(&irda_driver);
1182 module_exit(stir_cleanup);