remove unused TIF_NOTIFY_RESUME flag
[pv_ops_mirror.git] / drivers / net / irda / stir4200.c
blob755aa444a4ddb91026a25834ade6b59b17efc4d3
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.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *****************************************************************************/
32 * This dongle does no framing, and requires polling to receive the
33 * data. The STIr4200 has bulk in and out endpoints just like
34 * usr-irda devices, but the data it sends and receives is raw; like
35 * irtty, it needs to call the wrap and unwrap functions to add and
36 * remove SOF/BOF and escape characters to/from the frame.
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
42 #include <linux/kernel.h>
43 #include <linux/types.h>
44 #include <linux/init.h>
45 #include <linux/time.h>
46 #include <linux/skbuff.h>
47 #include <linux/netdevice.h>
48 #include <linux/slab.h>
49 #include <linux/delay.h>
50 #include <linux/usb.h>
51 #include <linux/crc32.h>
52 #include <linux/kthread.h>
53 #include <linux/freezer.h>
54 #include <net/irda/irda.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@linux-foundation.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,
153 enum StirDiagMask {
154 IRDIG_RXHIGH = 0x80,
155 IRDIG_RXLOW = 0x40,
158 enum StirTestMask {
159 TEST_PLLDOWN = 0x80,
160 TEST_LOOPIR = 0x40,
161 TEST_LOOPUSB = 0x20,
162 TEST_TSTENA = 0x10,
163 TEST_TSTOSC = 0x0F,
166 struct stir_cb {
167 struct usb_device *usbdev; /* init: probe_irda */
168 struct net_device *netdev; /* network layer */
169 struct irlap_cb *irlap; /* The link layer we are binded to */
170 struct net_device_stats stats; /* network statistics */
171 struct qos_info qos;
172 unsigned speed; /* Current speed */
174 struct task_struct *thread; /* transmit thread */
176 struct sk_buff *tx_pending;
177 void *io_buf; /* transmit/receive buffer */
178 __u8 *fifo_status;
180 iobuff_t rx_buff; /* receive unwrap state machine */
181 struct timeval rx_time;
182 int receiving;
183 struct urb *rx_urb;
187 /* These are the currently known USB ids */
188 static struct usb_device_id dongles[] = {
189 /* SigmaTel, Inc, STIr4200 IrDA/USB Bridge */
190 { USB_DEVICE(0x066f, 0x4200) },
194 MODULE_DEVICE_TABLE(usb, dongles);
196 /* Send control message to set dongle register */
197 static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value)
199 struct usb_device *dev = stir->usbdev;
201 pr_debug("%s: write reg %d = 0x%x\n",
202 stir->netdev->name, reg, value);
203 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
204 REQ_WRITE_SINGLE,
205 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
206 value, reg, NULL, 0,
207 CTRL_TIMEOUT);
210 /* Send control message to read multiple registers */
211 static inline int read_reg(struct stir_cb *stir, __u16 reg,
212 __u8 *data, __u16 count)
214 struct usb_device *dev = stir->usbdev;
216 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
217 REQ_READ_REG,
218 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
219 0, reg, data, count,
220 CTRL_TIMEOUT);
223 static inline int isfir(u32 speed)
225 return (speed == 4000000);
229 * Prepare a FIR IrDA frame for transmission to the USB dongle. The
230 * FIR transmit frame is documented in the datasheet. It consists of
231 * a two byte 0x55 0xAA sequence, two little-endian length bytes, a
232 * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E,
233 * then the data escaped as follows:
235 * 0x7D -> 0x7D 0x5D
236 * 0x7E -> 0x7D 0x5E
237 * 0x7F -> 0x7D 0x5F
239 * Then, 4 bytes of little endian (stuffed) FCS follow, then two
240 * trailing EOF bytes of 0x7E.
242 static inline __u8 *stuff_fir(__u8 *p, __u8 c)
244 switch(c) {
245 case 0x7d:
246 case 0x7e:
247 case 0x7f:
248 *p++ = 0x7d;
249 c ^= IRDA_TRANS;
250 /* fall through */
251 default:
252 *p++ = c;
254 return p;
257 /* Take raw data in skb and put it wrapped into buf */
258 static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
260 __u8 *ptr = buf;
261 __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
262 __u16 wraplen;
263 int i;
265 /* Header */
266 buf[0] = 0x55;
267 buf[1] = 0xAA;
269 ptr = buf + STIR_IRDA_HEADER;
270 memset(ptr, 0x7f, 16);
271 ptr += 16;
273 /* BOF */
274 *ptr++ = 0x7e;
275 *ptr++ = 0x7e;
277 /* Address / Control / Information */
278 for (i = 0; i < skb->len; i++)
279 ptr = stuff_fir(ptr, skb->data[i]);
281 /* FCS */
282 ptr = stuff_fir(ptr, fcs & 0xff);
283 ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
284 ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
285 ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);
287 /* EOFs */
288 *ptr++ = 0x7e;
289 *ptr++ = 0x7e;
291 /* Total length, minus the header */
292 wraplen = (ptr - buf) - STIR_IRDA_HEADER;
293 buf[2] = wraplen & 0xff;
294 buf[3] = (wraplen >> 8) & 0xff;
296 return wraplen + STIR_IRDA_HEADER;
299 static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf)
301 __u16 wraplen;
303 wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,
304 STIR_FIFO_SIZE - STIR_IRDA_HEADER);
305 buf[0] = 0x55;
306 buf[1] = 0xAA;
307 buf[2] = wraplen & 0xff;
308 buf[3] = (wraplen >> 8) & 0xff;
310 return wraplen + STIR_IRDA_HEADER;
314 * Frame is fully formed in the rx_buff so check crc
315 * and pass up to irlap
316 * setup for next receive
318 static void fir_eof(struct stir_cb *stir)
320 iobuff_t *rx_buff = &stir->rx_buff;
321 int len = rx_buff->len - 4;
322 struct sk_buff *skb, *nskb;
323 __u32 fcs;
325 if (unlikely(len <= 0)) {
326 pr_debug("%s: short frame len %d\n",
327 stir->netdev->name, len);
329 ++stir->stats.rx_errors;
330 ++stir->stats.rx_length_errors;
331 return;
334 fcs = ~(crc32_le(~0, rx_buff->data, len));
335 if (fcs != le32_to_cpu(get_unaligned((u32 *)(rx_buff->data+len)))) {
336 pr_debug("crc error calc 0x%x len %d\n", fcs, len);
337 stir->stats.rx_errors++;
338 stir->stats.rx_crc_errors++;
339 return;
342 /* if frame is short then just copy it */
343 if (len < IRDA_RX_COPY_THRESHOLD) {
344 nskb = dev_alloc_skb(len + 1);
345 if (unlikely(!nskb)) {
346 ++stir->stats.rx_dropped;
347 return;
349 skb_reserve(nskb, 1);
350 skb = nskb;
351 skb_copy_to_linear_data(nskb, rx_buff->data, len);
352 } else {
353 nskb = dev_alloc_skb(rx_buff->truesize);
354 if (unlikely(!nskb)) {
355 ++stir->stats.rx_dropped;
356 return;
358 skb_reserve(nskb, 1);
359 skb = rx_buff->skb;
360 rx_buff->skb = nskb;
361 rx_buff->head = nskb->data;
364 skb_put(skb, len);
366 skb_reset_mac_header(skb);
367 skb->protocol = htons(ETH_P_IRDA);
368 skb->dev = stir->netdev;
370 netif_rx(skb);
372 stir->stats.rx_packets++;
373 stir->stats.rx_bytes += len;
375 rx_buff->data = rx_buff->head;
376 rx_buff->len = 0;
379 /* Unwrap FIR stuffed data and bump it to IrLAP */
380 static void stir_fir_chars(struct stir_cb *stir,
381 const __u8 *bytes, int len)
383 iobuff_t *rx_buff = &stir->rx_buff;
384 int i;
386 for (i = 0; i < len; i++) {
387 __u8 byte = bytes[i];
389 switch(rx_buff->state) {
390 case OUTSIDE_FRAME:
391 /* ignore garbage till start of frame */
392 if (unlikely(byte != FIR_EOF))
393 continue;
394 /* Now receiving frame */
395 rx_buff->state = BEGIN_FRAME;
397 /* Time to initialize receive buffer */
398 rx_buff->data = rx_buff->head;
399 rx_buff->len = 0;
400 continue;
402 case LINK_ESCAPE:
403 if (byte == FIR_EOF) {
404 pr_debug("%s: got EOF after escape\n",
405 stir->netdev->name);
406 goto frame_error;
408 rx_buff->state = INSIDE_FRAME;
409 byte ^= IRDA_TRANS;
410 break;
412 case BEGIN_FRAME:
413 /* ignore multiple BOF/EOF */
414 if (byte == FIR_EOF)
415 continue;
416 rx_buff->state = INSIDE_FRAME;
417 rx_buff->in_frame = TRUE;
419 /* fall through */
420 case INSIDE_FRAME:
421 switch(byte) {
422 case FIR_CE:
423 rx_buff->state = LINK_ESCAPE;
424 continue;
425 case FIR_XBOF:
426 /* 0x7f is not used in this framing */
427 pr_debug("%s: got XBOF without escape\n",
428 stir->netdev->name);
429 goto frame_error;
430 case FIR_EOF:
431 rx_buff->state = OUTSIDE_FRAME;
432 rx_buff->in_frame = FALSE;
433 fir_eof(stir);
434 continue;
436 break;
439 /* add byte to rx buffer */
440 if (unlikely(rx_buff->len >= rx_buff->truesize)) {
441 pr_debug("%s: fir frame exceeds %d\n",
442 stir->netdev->name, rx_buff->truesize);
443 ++stir->stats.rx_over_errors;
444 goto error_recovery;
447 rx_buff->data[rx_buff->len++] = byte;
448 continue;
450 frame_error:
451 ++stir->stats.rx_frame_errors;
453 error_recovery:
454 ++stir->stats.rx_errors;
455 rx_buff->state = OUTSIDE_FRAME;
456 rx_buff->in_frame = FALSE;
460 /* Unwrap SIR stuffed data and bump it up to IrLAP */
461 static void stir_sir_chars(struct stir_cb *stir,
462 const __u8 *bytes, int len)
464 int i;
466 for (i = 0; i < len; i++)
467 async_unwrap_char(stir->netdev, &stir->stats,
468 &stir->rx_buff, bytes[i]);
471 static inline void unwrap_chars(struct stir_cb *stir,
472 const __u8 *bytes, int length)
474 if (isfir(stir->speed))
475 stir_fir_chars(stir, bytes, length);
476 else
477 stir_sir_chars(stir, bytes, length);
480 /* Mode parameters for each speed */
481 static const struct {
482 unsigned speed;
483 __u8 pdclk;
484 } stir_modes[] = {
485 { 2400, PDCLK_2400 },
486 { 9600, PDCLK_9600 },
487 { 19200, PDCLK_19200 },
488 { 38400, PDCLK_38400 },
489 { 57600, PDCLK_57600 },
490 { 115200, PDCLK_115200 },
491 { 4000000, PDCLK_4000000 },
496 * Setup chip for speed.
497 * Called at startup to initialize the chip
498 * and on speed changes.
500 * Note: Write multiple registers doesn't appear to work
502 static int change_speed(struct stir_cb *stir, unsigned speed)
504 int i, err;
505 __u8 mode;
507 for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {
508 if (speed == stir_modes[i].speed)
509 goto found;
512 warn("%s: invalid speed %d", stir->netdev->name, speed);
513 return -EINVAL;
515 found:
516 pr_debug("speed change from %d to %d\n", stir->speed, speed);
518 /* Reset modulator */
519 err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);
520 if (err)
521 goto out;
523 /* Undocumented magic to tweak the DPLL */
524 err = write_reg(stir, REG_DPLL, 0x15);
525 if (err)
526 goto out;
528 /* Set clock */
529 err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);
530 if (err)
531 goto out;
533 mode = MODE_NRESET | MODE_FASTRX;
534 if (isfir(speed))
535 mode |= MODE_FIR | MODE_FFRSTEN;
536 else
537 mode |= MODE_SIR;
539 if (speed == 2400)
540 mode |= MODE_2400;
542 err = write_reg(stir, REG_MODE, mode);
543 if (err)
544 goto out;
546 /* This resets TEMIC style transceiver if any. */
547 err = write_reg(stir, REG_CTRL1,
548 CTRL1_SDMODE | (tx_power & 3) << 1);
549 if (err)
550 goto out;
552 err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);
553 if (err)
554 goto out;
556 /* Reset sensitivity */
557 err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5);
558 out:
559 stir->speed = speed;
560 return err;
564 * Called from net/core when new frame is available.
566 static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
568 struct stir_cb *stir = netdev_priv(netdev);
570 netif_stop_queue(netdev);
572 /* the IRDA wrapping routines don't deal with non linear skb */
573 SKB_LINEAR_ASSERT(skb);
575 skb = xchg(&stir->tx_pending, skb);
576 wake_up_process(stir->thread);
578 /* this should never happen unless stop/wakeup problem */
579 if (unlikely(skb)) {
580 WARN_ON(1);
581 dev_kfree_skb(skb);
584 return 0;
588 * Wait for the transmit FIFO to have space for next data
590 * If space < 0 then wait till FIFO completely drains.
591 * FYI: can take up to 13 seconds at 2400baud.
593 static int fifo_txwait(struct stir_cb *stir, int space)
595 int err;
596 unsigned long count, status;
598 /* Read FIFO status and count */
599 for(;;) {
600 err = read_reg(stir, REG_FIFOCTL, stir->fifo_status,
601 FIFO_REGS_SIZE);
602 if (unlikely(err != FIFO_REGS_SIZE)) {
603 warn("%s: FIFO register read error: %d",
604 stir->netdev->name, err);
606 return err;
609 status = stir->fifo_status[0];
610 count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8
611 | stir->fifo_status[1];
613 pr_debug("fifo status 0x%lx count %lu\n", status, count);
615 /* is fifo receiving already, or empty */
616 if (!(status & FIFOCTL_DIR)
617 || (status & FIFOCTL_EMPTY))
618 return 0;
620 if (signal_pending(current))
621 return -EINTR;
623 /* shutting down? */
624 if (!netif_running(stir->netdev)
625 || !netif_device_present(stir->netdev))
626 return -ESHUTDOWN;
628 /* only waiting for some space */
629 if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
630 return 0;
632 /* estimate transfer time for remaining chars */
633 msleep((count * 8000) / stir->speed);
636 err = write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR);
637 if (err)
638 return err;
639 err = write_reg(stir, REG_FIFOCTL, 0);
640 if (err)
641 return err;
643 return 0;
647 /* Wait for turnaround delay before starting transmit. */
648 static void turnaround_delay(const struct stir_cb *stir, long us)
650 long ticks;
651 struct timeval now;
653 if (us <= 0)
654 return;
656 do_gettimeofday(&now);
657 if (now.tv_sec - stir->rx_time.tv_sec > 0)
658 us -= USEC_PER_SEC;
659 us -= now.tv_usec - stir->rx_time.tv_usec;
660 if (us < 10)
661 return;
663 ticks = us / (1000000 / HZ);
664 if (ticks > 0)
665 schedule_timeout_interruptible(1 + ticks);
666 else
667 udelay(us);
671 * Start receiver by submitting a request to the receive pipe.
672 * If nothing is available it will return after rx_interval.
674 static int receive_start(struct stir_cb *stir)
676 /* reset state */
677 stir->receiving = 1;
679 stir->rx_buff.in_frame = FALSE;
680 stir->rx_buff.state = OUTSIDE_FRAME;
682 stir->rx_urb->status = 0;
683 return usb_submit_urb(stir->rx_urb, GFP_KERNEL);
686 /* Stop all pending receive Urb's */
687 static void receive_stop(struct stir_cb *stir)
689 stir->receiving = 0;
690 usb_kill_urb(stir->rx_urb);
692 if (stir->rx_buff.in_frame)
693 stir->stats.collisions++;
696 * Wrap data in socket buffer and send it.
698 static void stir_send(struct stir_cb *stir, struct sk_buff *skb)
700 unsigned wraplen;
701 int first_frame = 0;
703 /* if receiving, need to turnaround */
704 if (stir->receiving) {
705 receive_stop(stir);
706 turnaround_delay(stir, irda_get_mtt(skb));
707 first_frame = 1;
710 if (isfir(stir->speed))
711 wraplen = wrap_fir_skb(skb, stir->io_buf);
712 else
713 wraplen = wrap_sir_skb(skb, stir->io_buf);
715 /* check for space available in fifo */
716 if (!first_frame)
717 fifo_txwait(stir, wraplen);
719 stir->stats.tx_packets++;
720 stir->stats.tx_bytes += skb->len;
721 stir->netdev->trans_start = jiffies;
722 pr_debug("send %d (%d)\n", skb->len, wraplen);
724 if (usb_bulk_msg(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1),
725 stir->io_buf, wraplen,
726 NULL, TRANSMIT_TIMEOUT))
727 stir->stats.tx_errors++;
731 * Transmit state machine thread
733 static int stir_transmit_thread(void *arg)
735 struct stir_cb *stir = arg;
736 struct net_device *dev = stir->netdev;
737 struct sk_buff *skb;
739 while (!kthread_should_stop()) {
740 #ifdef CONFIG_PM
741 /* if suspending, then power off and wait */
742 if (unlikely(freezing(current))) {
743 if (stir->receiving)
744 receive_stop(stir);
745 else
746 fifo_txwait(stir, -1);
748 write_reg(stir, REG_CTRL1, CTRL1_TXPWD|CTRL1_RXPWD);
750 refrigerator();
752 if (change_speed(stir, stir->speed))
753 break;
755 #endif
757 /* if something to send? */
758 skb = xchg(&stir->tx_pending, NULL);
759 if (skb) {
760 unsigned new_speed = irda_get_next_speed(skb);
761 netif_wake_queue(dev);
763 if (skb->len > 0)
764 stir_send(stir, skb);
765 dev_kfree_skb(skb);
767 if ((new_speed != -1) && (stir->speed != new_speed)) {
768 if (fifo_txwait(stir, -1) ||
769 change_speed(stir, new_speed))
770 break;
772 continue;
775 /* nothing to send? start receiving */
776 if (!stir->receiving
777 && irda_device_txqueue_empty(dev)) {
778 /* Wait otherwise chip gets confused. */
779 if (fifo_txwait(stir, -1))
780 break;
782 if (unlikely(receive_start(stir))) {
783 if (net_ratelimit())
784 info("%s: receive usb submit failed",
785 stir->netdev->name);
786 stir->receiving = 0;
787 msleep(10);
788 continue;
792 /* sleep if nothing to send */
793 set_current_state(TASK_INTERRUPTIBLE);
794 schedule();
797 return 0;
802 * USB bulk receive completion callback.
803 * Wakes up every ms (usb round trip) with wrapped
804 * data.
806 static void stir_rcv_irq(struct urb *urb)
808 struct stir_cb *stir = urb->context;
809 int err;
811 /* in process of stopping, just drop data */
812 if (!netif_running(stir->netdev))
813 return;
815 /* unlink, shutdown, unplug, other nasties */
816 if (urb->status != 0)
817 return;
819 if (urb->actual_length > 0) {
820 pr_debug("receive %d\n", urb->actual_length);
821 unwrap_chars(stir, urb->transfer_buffer,
822 urb->actual_length);
824 stir->netdev->last_rx = jiffies;
825 do_gettimeofday(&stir->rx_time);
828 /* kernel thread is stopping receiver don't resubmit */
829 if (!stir->receiving)
830 return;
832 /* resubmit existing urb */
833 err = usb_submit_urb(urb, GFP_ATOMIC);
835 /* in case of error, the kernel thread will restart us */
836 if (err) {
837 warn("%s: usb receive submit error: %d",
838 stir->netdev->name, err);
839 stir->receiving = 0;
840 wake_up_process(stir->thread);
845 * Function stir_net_open (dev)
847 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
849 static int stir_net_open(struct net_device *netdev)
851 struct stir_cb *stir = netdev_priv(netdev);
852 int err;
853 char hwname[16];
855 err = usb_clear_halt(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1));
856 if (err)
857 goto err_out1;
858 err = usb_clear_halt(stir->usbdev, usb_rcvbulkpipe(stir->usbdev, 2));
859 if (err)
860 goto err_out1;
862 err = change_speed(stir, 9600);
863 if (err)
864 goto err_out1;
866 err = -ENOMEM;
868 /* Initialize for SIR/FIR to copy data directly into skb. */
869 stir->receiving = 0;
870 stir->rx_buff.truesize = IRDA_SKB_MAX_MTU;
871 stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
872 if (!stir->rx_buff.skb)
873 goto err_out1;
875 skb_reserve(stir->rx_buff.skb, 1);
876 stir->rx_buff.head = stir->rx_buff.skb->data;
877 do_gettimeofday(&stir->rx_time);
879 stir->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
880 if (!stir->rx_urb)
881 goto err_out2;
883 stir->io_buf = kmalloc(STIR_FIFO_SIZE, GFP_KERNEL);
884 if (!stir->io_buf)
885 goto err_out3;
887 usb_fill_bulk_urb(stir->rx_urb, stir->usbdev,
888 usb_rcvbulkpipe(stir->usbdev, 2),
889 stir->io_buf, STIR_FIFO_SIZE,
890 stir_rcv_irq, stir);
892 stir->fifo_status = kmalloc(FIFO_REGS_SIZE, GFP_KERNEL);
893 if (!stir->fifo_status)
894 goto err_out4;
897 * Now that everything should be initialized properly,
898 * Open new IrLAP layer instance to take care of us...
899 * Note : will send immediately a speed change...
901 sprintf(hwname, "usb#%d", stir->usbdev->devnum);
902 stir->irlap = irlap_open(netdev, &stir->qos, hwname);
903 if (!stir->irlap) {
904 err("stir4200: irlap_open failed");
905 goto err_out5;
908 /** Start kernel thread for transmit. */
909 stir->thread = kthread_run(stir_transmit_thread, stir,
910 "%s", stir->netdev->name);
911 if (IS_ERR(stir->thread)) {
912 err = PTR_ERR(stir->thread);
913 err("stir4200: unable to start kernel thread");
914 goto err_out6;
917 netif_start_queue(netdev);
919 return 0;
921 err_out6:
922 irlap_close(stir->irlap);
923 err_out5:
924 kfree(stir->fifo_status);
925 err_out4:
926 kfree(stir->io_buf);
927 err_out3:
928 usb_free_urb(stir->rx_urb);
929 err_out2:
930 kfree_skb(stir->rx_buff.skb);
931 err_out1:
932 return err;
936 * Function stir_net_close (stir)
938 * Network device is taken down. Usually this is done by
939 * "ifconfig irda0 down"
941 static int stir_net_close(struct net_device *netdev)
943 struct stir_cb *stir = netdev_priv(netdev);
945 /* Stop transmit processing */
946 netif_stop_queue(netdev);
948 /* Kill transmit thread */
949 kthread_stop(stir->thread);
950 kfree(stir->fifo_status);
952 /* Mop up receive urb's */
953 usb_kill_urb(stir->rx_urb);
955 kfree(stir->io_buf);
956 usb_free_urb(stir->rx_urb);
957 kfree_skb(stir->rx_buff.skb);
959 /* Stop and remove instance of IrLAP */
960 if (stir->irlap)
961 irlap_close(stir->irlap);
963 stir->irlap = NULL;
965 return 0;
969 * IOCTLs : Extra out-of-band network commands...
971 static int stir_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
973 struct if_irda_req *irq = (struct if_irda_req *) rq;
974 struct stir_cb *stir = netdev_priv(netdev);
975 int ret = 0;
977 switch (cmd) {
978 case SIOCSBANDWIDTH: /* Set bandwidth */
979 if (!capable(CAP_NET_ADMIN))
980 return -EPERM;
982 /* Check if the device is still there */
983 if (netif_device_present(stir->netdev))
984 ret = change_speed(stir, irq->ifr_baudrate);
985 break;
987 case SIOCSMEDIABUSY: /* Set media busy */
988 if (!capable(CAP_NET_ADMIN))
989 return -EPERM;
991 /* Check if the IrDA stack is still there */
992 if (netif_running(stir->netdev))
993 irda_device_set_media_busy(stir->netdev, TRUE);
994 break;
996 case SIOCGRECEIVING:
997 /* Only approximately true */
998 irq->ifr_receiving = stir->receiving;
999 break;
1001 default:
1002 ret = -EOPNOTSUPP;
1005 return ret;
1009 * Get device stats (for /proc/net/dev and ifconfig)
1011 static struct net_device_stats *stir_net_get_stats(struct net_device *netdev)
1013 struct stir_cb *stir = netdev_priv(netdev);
1014 return &stir->stats;
1018 * This routine is called by the USB subsystem for each new device
1019 * in the system. We need to check if the device is ours, and in
1020 * this case start handling it.
1021 * Note : it might be worth protecting this function by a global
1022 * spinlock... Or not, because maybe USB already deal with that...
1024 static int stir_probe(struct usb_interface *intf,
1025 const struct usb_device_id *id)
1027 struct usb_device *dev = interface_to_usbdev(intf);
1028 struct stir_cb *stir = NULL;
1029 struct net_device *net;
1030 int ret = -ENOMEM;
1032 /* Allocate network device container. */
1033 net = alloc_irdadev(sizeof(*stir));
1034 if(!net)
1035 goto err_out1;
1037 SET_MODULE_OWNER(net);
1038 SET_NETDEV_DEV(net, &intf->dev);
1039 stir = netdev_priv(net);
1040 stir->netdev = net;
1041 stir->usbdev = dev;
1043 ret = usb_reset_configuration(dev);
1044 if (ret != 0) {
1045 err("stir4200: usb reset configuration failed");
1046 goto err_out2;
1049 printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
1050 "Vendor: %x, Product: %x\n",
1051 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1052 le16_to_cpu(dev->descriptor.idProduct));
1054 /* Initialize QoS for this device */
1055 irda_init_max_qos_capabilies(&stir->qos);
1057 /* That's the Rx capability. */
1058 stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 |
1059 IR_38400 | IR_57600 | IR_115200 |
1060 (IR_4000000 << 8);
1061 stir->qos.min_turn_time.bits &= qos_mtt_bits;
1062 irda_qos_bits_to_value(&stir->qos);
1064 /* Override the network functions we need to use */
1065 net->hard_start_xmit = stir_hard_xmit;
1066 net->open = stir_net_open;
1067 net->stop = stir_net_close;
1068 net->get_stats = stir_net_get_stats;
1069 net->do_ioctl = stir_net_ioctl;
1071 ret = register_netdev(net);
1072 if (ret != 0)
1073 goto err_out2;
1075 info("IrDA: Registered SigmaTel device %s", net->name);
1077 usb_set_intfdata(intf, stir);
1079 return 0;
1081 err_out2:
1082 free_netdev(net);
1083 err_out1:
1084 return ret;
1088 * The current device is removed, the USB layer tell us to shut it down...
1090 static void stir_disconnect(struct usb_interface *intf)
1092 struct stir_cb *stir = usb_get_intfdata(intf);
1094 if (!stir)
1095 return;
1097 unregister_netdev(stir->netdev);
1098 free_netdev(stir->netdev);
1100 usb_set_intfdata(intf, NULL);
1103 #ifdef CONFIG_PM
1104 /* USB suspend, so power off the transmitter/receiver */
1105 static int stir_suspend(struct usb_interface *intf, pm_message_t message)
1107 struct stir_cb *stir = usb_get_intfdata(intf);
1109 netif_device_detach(stir->netdev);
1110 return 0;
1113 /* Coming out of suspend, so reset hardware */
1114 static int stir_resume(struct usb_interface *intf)
1116 struct stir_cb *stir = usb_get_intfdata(intf);
1118 netif_device_attach(stir->netdev);
1120 /* receiver restarted when send thread wakes up */
1121 return 0;
1123 #endif
1126 * USB device callbacks
1128 static struct usb_driver irda_driver = {
1129 .name = "stir4200",
1130 .probe = stir_probe,
1131 .disconnect = stir_disconnect,
1132 .id_table = dongles,
1133 #ifdef CONFIG_PM
1134 .suspend = stir_suspend,
1135 .resume = stir_resume,
1136 #endif
1140 * Module insertion
1142 static int __init stir_init(void)
1144 return usb_register(&irda_driver);
1146 module_init(stir_init);
1149 * Module removal
1151 static void __exit stir_cleanup(void)
1153 /* Deregister the driver and remove all pending instances */
1154 usb_deregister(&irda_driver);
1156 module_exit(stir_cleanup);