]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/net/irda/stir4200.c
7ee51487953155b64b9dd3d371e39eff6b85658d
[mirror_ubuntu-artful-kernel.git] / drivers / net / irda / stir4200.c
1 /*****************************************************************************
2 *
3 * Filename: stir4200.c
4 * Version: 0.4
5 * Description: Irda SigmaTel USB Dongle
6 * Status: Experimental
7 * Author: Stephen Hemminger <shemminger@osdl.org>
8 *
9 * Based on earlier driver by Paul Stewart <stewart@parc.com>
10 *
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>
15 *
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.
19 *
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.
24 *
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.
28 *
29 *****************************************************************************/
30
31 /*
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.
37 */
38
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
41
42 #include <linux/kernel.h>
43 #include <linux/sched/signal.h>
44 #include <linux/ktime.h>
45 #include <linux/types.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 <linux/kthread.h>
54 #include <linux/freezer.h>
55 #include <net/irda/irda.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>
61
62 MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");
63 MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200");
64 MODULE_LICENSE("GPL");
65
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");
69
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)");
73
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)");
77
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
83
84 enum FirChars {
85 FIR_CE = 0x7d,
86 FIR_XBOF = 0x7f,
87 FIR_EOF = 0x7e,
88 };
89
90 enum StirRequests {
91 REQ_WRITE_REG = 0x00,
92 REQ_READ_REG = 0x01,
93 REQ_READ_ROM = 0x02,
94 REQ_WRITE_SINGLE = 0x03,
95 };
96
97 /* Register offsets */
98 enum StirRegs {
99 REG_RSVD=0,
100 REG_MODE,
101 REG_PDCLK,
102 REG_CTRL1,
103 REG_CTRL2,
104 REG_FIFOCTL,
105 REG_FIFOLSB,
106 REG_FIFOMSB,
107 REG_DPLL,
108 REG_IRDIG,
109 REG_TEST=15,
110 };
111
112 enum StirModeMask {
113 MODE_FIR = 0x80,
114 MODE_SIR = 0x20,
115 MODE_ASK = 0x10,
116 MODE_FASTRX = 0x08,
117 MODE_FFRSTEN = 0x04,
118 MODE_NRESET = 0x02,
119 MODE_2400 = 0x01,
120 };
121
122 enum StirPdclkMask {
123 PDCLK_4000000 = 0x02,
124 PDCLK_115200 = 0x09,
125 PDCLK_57600 = 0x13,
126 PDCLK_38400 = 0x1D,
127 PDCLK_19200 = 0x3B,
128 PDCLK_9600 = 0x77,
129 PDCLK_2400 = 0xDF,
130 };
131
132 enum StirCtrl1Mask {
133 CTRL1_SDMODE = 0x80,
134 CTRL1_RXSLOW = 0x40,
135 CTRL1_TXPWD = 0x10,
136 CTRL1_RXPWD = 0x08,
137 CTRL1_SRESET = 0x01,
138 };
139
140 enum StirCtrl2Mask {
141 CTRL2_SPWIDTH = 0x08,
142 CTRL2_REVID = 0x03,
143 };
144
145 enum StirFifoCtlMask {
146 FIFOCTL_DIR = 0x10,
147 FIFOCTL_CLR = 0x08,
148 FIFOCTL_EMPTY = 0x04,
149 };
150
151 enum StirDiagMask {
152 IRDIG_RXHIGH = 0x80,
153 IRDIG_RXLOW = 0x40,
154 };
155
156 enum StirTestMask {
157 TEST_PLLDOWN = 0x80,
158 TEST_LOOPIR = 0x40,
159 TEST_LOOPUSB = 0x20,
160 TEST_TSTENA = 0x10,
161 TEST_TSTOSC = 0x0F,
162 };
163
164 struct stir_cb {
165 struct usb_device *usbdev; /* init: probe_irda */
166 struct net_device *netdev; /* network layer */
167 struct irlap_cb *irlap; /* The link layer we are binded to */
168
169 struct qos_info qos;
170 unsigned speed; /* Current speed */
171
172 struct task_struct *thread; /* transmit thread */
173
174 struct sk_buff *tx_pending;
175 void *io_buf; /* transmit/receive buffer */
176 __u8 *fifo_status;
177
178 iobuff_t rx_buff; /* receive unwrap state machine */
179 ktime_t rx_time;
180 int receiving;
181 struct urb *rx_urb;
182 };
183
184
185 /* These are the currently known USB ids */
186 static struct usb_device_id dongles[] = {
187 /* SigmaTel, Inc, STIr4200 IrDA/USB Bridge */
188 { USB_DEVICE(0x066f, 0x4200) },
189 { }
190 };
191
192 MODULE_DEVICE_TABLE(usb, dongles);
193
194 /* Send control message to set dongle register */
195 static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value)
196 {
197 struct usb_device *dev = stir->usbdev;
198
199 pr_debug("%s: write reg %d = 0x%x\n",
200 stir->netdev->name, reg, value);
201 return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
202 REQ_WRITE_SINGLE,
203 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,
204 value, reg, NULL, 0,
205 CTRL_TIMEOUT);
206 }
207
208 /* Send control message to read multiple registers */
209 static inline int read_reg(struct stir_cb *stir, __u16 reg,
210 __u8 *data, __u16 count)
211 {
212 struct usb_device *dev = stir->usbdev;
213
214 return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
215 REQ_READ_REG,
216 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
217 0, reg, data, count,
218 CTRL_TIMEOUT);
219 }
220
221 static inline int isfir(u32 speed)
222 {
223 return speed == 4000000;
224 }
225
226 /*
227 * Prepare a FIR IrDA frame for transmission to the USB dongle. The
228 * FIR transmit frame is documented in the datasheet. It consists of
229 * a two byte 0x55 0xAA sequence, two little-endian length bytes, a
230 * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E,
231 * then the data escaped as follows:
232 *
233 * 0x7D -> 0x7D 0x5D
234 * 0x7E -> 0x7D 0x5E
235 * 0x7F -> 0x7D 0x5F
236 *
237 * Then, 4 bytes of little endian (stuffed) FCS follow, then two
238 * trailing EOF bytes of 0x7E.
239 */
240 static inline __u8 *stuff_fir(__u8 *p, __u8 c)
241 {
242 switch(c) {
243 case 0x7d:
244 case 0x7e:
245 case 0x7f:
246 *p++ = 0x7d;
247 c ^= IRDA_TRANS;
248 /* fall through */
249 default:
250 *p++ = c;
251 }
252 return p;
253 }
254
255 /* Take raw data in skb and put it wrapped into buf */
256 static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf)
257 {
258 __u8 *ptr = buf;
259 __u32 fcs = ~(crc32_le(~0, skb->data, skb->len));
260 __u16 wraplen;
261 int i;
262
263 /* Header */
264 buf[0] = 0x55;
265 buf[1] = 0xAA;
266
267 ptr = buf + STIR_IRDA_HEADER;
268 memset(ptr, 0x7f, 16);
269 ptr += 16;
270
271 /* BOF */
272 *ptr++ = 0x7e;
273 *ptr++ = 0x7e;
274
275 /* Address / Control / Information */
276 for (i = 0; i < skb->len; i++)
277 ptr = stuff_fir(ptr, skb->data[i]);
278
279 /* FCS */
280 ptr = stuff_fir(ptr, fcs & 0xff);
281 ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);
282 ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);
283 ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);
284
285 /* EOFs */
286 *ptr++ = 0x7e;
287 *ptr++ = 0x7e;
288
289 /* Total length, minus the header */
290 wraplen = (ptr - buf) - STIR_IRDA_HEADER;
291 buf[2] = wraplen & 0xff;
292 buf[3] = (wraplen >> 8) & 0xff;
293
294 return wraplen + STIR_IRDA_HEADER;
295 }
296
297 static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf)
298 {
299 __u16 wraplen;
300
301 wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,
302 STIR_FIFO_SIZE - STIR_IRDA_HEADER);
303 buf[0] = 0x55;
304 buf[1] = 0xAA;
305 buf[2] = wraplen & 0xff;
306 buf[3] = (wraplen >> 8) & 0xff;
307
308 return wraplen + STIR_IRDA_HEADER;
309 }
310
311 /*
312 * Frame is fully formed in the rx_buff so check crc
313 * and pass up to irlap
314 * setup for next receive
315 */
316 static void fir_eof(struct stir_cb *stir)
317 {
318 iobuff_t *rx_buff = &stir->rx_buff;
319 int len = rx_buff->len - 4;
320 struct sk_buff *skb, *nskb;
321 __u32 fcs;
322
323 if (unlikely(len <= 0)) {
324 pr_debug("%s: short frame len %d\n",
325 stir->netdev->name, len);
326
327 ++stir->netdev->stats.rx_errors;
328 ++stir->netdev->stats.rx_length_errors;
329 return;
330 }
331
332 fcs = ~(crc32_le(~0, rx_buff->data, len));
333 if (fcs != get_unaligned_le32(rx_buff->data + len)) {
334 pr_debug("crc error calc 0x%x len %d\n", fcs, len);
335 stir->netdev->stats.rx_errors++;
336 stir->netdev->stats.rx_crc_errors++;
337 return;
338 }
339
340 /* if frame is short then just copy it */
341 if (len < IRDA_RX_COPY_THRESHOLD) {
342 nskb = dev_alloc_skb(len + 1);
343 if (unlikely(!nskb)) {
344 ++stir->netdev->stats.rx_dropped;
345 return;
346 }
347 skb_reserve(nskb, 1);
348 skb = nskb;
349 skb_copy_to_linear_data(nskb, rx_buff->data, len);
350 } else {
351 nskb = dev_alloc_skb(rx_buff->truesize);
352 if (unlikely(!nskb)) {
353 ++stir->netdev->stats.rx_dropped;
354 return;
355 }
356 skb_reserve(nskb, 1);
357 skb = rx_buff->skb;
358 rx_buff->skb = nskb;
359 rx_buff->head = nskb->data;
360 }
361
362 skb_put(skb, len);
363
364 skb_reset_mac_header(skb);
365 skb->protocol = htons(ETH_P_IRDA);
366 skb->dev = stir->netdev;
367
368 netif_rx(skb);
369
370 stir->netdev->stats.rx_packets++;
371 stir->netdev->stats.rx_bytes += len;
372
373 rx_buff->data = rx_buff->head;
374 rx_buff->len = 0;
375 }
376
377 /* Unwrap FIR stuffed data and bump it to IrLAP */
378 static void stir_fir_chars(struct stir_cb *stir,
379 const __u8 *bytes, int len)
380 {
381 iobuff_t *rx_buff = &stir->rx_buff;
382 int i;
383
384 for (i = 0; i < len; i++) {
385 __u8 byte = bytes[i];
386
387 switch(rx_buff->state) {
388 case OUTSIDE_FRAME:
389 /* ignore garbage till start of frame */
390 if (unlikely(byte != FIR_EOF))
391 continue;
392 /* Now receiving frame */
393 rx_buff->state = BEGIN_FRAME;
394
395 /* Time to initialize receive buffer */
396 rx_buff->data = rx_buff->head;
397 rx_buff->len = 0;
398 continue;
399
400 case LINK_ESCAPE:
401 if (byte == FIR_EOF) {
402 pr_debug("%s: got EOF after escape\n",
403 stir->netdev->name);
404 goto frame_error;
405 }
406 rx_buff->state = INSIDE_FRAME;
407 byte ^= IRDA_TRANS;
408 break;
409
410 case BEGIN_FRAME:
411 /* ignore multiple BOF/EOF */
412 if (byte == FIR_EOF)
413 continue;
414 rx_buff->state = INSIDE_FRAME;
415 rx_buff->in_frame = TRUE;
416
417 /* fall through */
418 case INSIDE_FRAME:
419 switch(byte) {
420 case FIR_CE:
421 rx_buff->state = LINK_ESCAPE;
422 continue;
423 case FIR_XBOF:
424 /* 0x7f is not used in this framing */
425 pr_debug("%s: got XBOF without escape\n",
426 stir->netdev->name);
427 goto frame_error;
428 case FIR_EOF:
429 rx_buff->state = OUTSIDE_FRAME;
430 rx_buff->in_frame = FALSE;
431 fir_eof(stir);
432 continue;
433 }
434 break;
435 }
436
437 /* add byte to rx buffer */
438 if (unlikely(rx_buff->len >= rx_buff->truesize)) {
439 pr_debug("%s: fir frame exceeds %d\n",
440 stir->netdev->name, rx_buff->truesize);
441 ++stir->netdev->stats.rx_over_errors;
442 goto error_recovery;
443 }
444
445 rx_buff->data[rx_buff->len++] = byte;
446 continue;
447
448 frame_error:
449 ++stir->netdev->stats.rx_frame_errors;
450
451 error_recovery:
452 ++stir->netdev->stats.rx_errors;
453 rx_buff->state = OUTSIDE_FRAME;
454 rx_buff->in_frame = FALSE;
455 }
456 }
457
458 /* Unwrap SIR stuffed data and bump it up to IrLAP */
459 static void stir_sir_chars(struct stir_cb *stir,
460 const __u8 *bytes, int len)
461 {
462 int i;
463
464 for (i = 0; i < len; i++)
465 async_unwrap_char(stir->netdev, &stir->netdev->stats,
466 &stir->rx_buff, bytes[i]);
467 }
468
469 static inline void unwrap_chars(struct stir_cb *stir,
470 const __u8 *bytes, int length)
471 {
472 if (isfir(stir->speed))
473 stir_fir_chars(stir, bytes, length);
474 else
475 stir_sir_chars(stir, bytes, length);
476 }
477
478 /* Mode parameters for each speed */
479 static const struct {
480 unsigned speed;
481 __u8 pdclk;
482 } stir_modes[] = {
483 { 2400, PDCLK_2400 },
484 { 9600, PDCLK_9600 },
485 { 19200, PDCLK_19200 },
486 { 38400, PDCLK_38400 },
487 { 57600, PDCLK_57600 },
488 { 115200, PDCLK_115200 },
489 { 4000000, PDCLK_4000000 },
490 };
491
492
493 /*
494 * Setup chip for speed.
495 * Called at startup to initialize the chip
496 * and on speed changes.
497 *
498 * Note: Write multiple registers doesn't appear to work
499 */
500 static int change_speed(struct stir_cb *stir, unsigned speed)
501 {
502 int i, err;
503 __u8 mode;
504
505 for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {
506 if (speed == stir_modes[i].speed)
507 goto found;
508 }
509
510 dev_warn(&stir->netdev->dev, "invalid speed %d\n", speed);
511 return -EINVAL;
512
513 found:
514 pr_debug("speed change from %d to %d\n", stir->speed, speed);
515
516 /* Reset modulator */
517 err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);
518 if (err)
519 goto out;
520
521 /* Undocumented magic to tweak the DPLL */
522 err = write_reg(stir, REG_DPLL, 0x15);
523 if (err)
524 goto out;
525
526 /* Set clock */
527 err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);
528 if (err)
529 goto out;
530
531 mode = MODE_NRESET | MODE_FASTRX;
532 if (isfir(speed))
533 mode |= MODE_FIR | MODE_FFRSTEN;
534 else
535 mode |= MODE_SIR;
536
537 if (speed == 2400)
538 mode |= MODE_2400;
539
540 err = write_reg(stir, REG_MODE, mode);
541 if (err)
542 goto out;
543
544 /* This resets TEMIC style transceiver if any. */
545 err = write_reg(stir, REG_CTRL1,
546 CTRL1_SDMODE | (tx_power & 3) << 1);
547 if (err)
548 goto out;
549
550 err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);
551 if (err)
552 goto out;
553
554 /* Reset sensitivity */
555 err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5);
556 out:
557 stir->speed = speed;
558 return err;
559 }
560
561 /*
562 * Called from net/core when new frame is available.
563 */
564 static netdev_tx_t stir_hard_xmit(struct sk_buff *skb,
565 struct net_device *netdev)
566 {
567 struct stir_cb *stir = netdev_priv(netdev);
568
569 netif_stop_queue(netdev);
570
571 /* the IRDA wrapping routines don't deal with non linear skb */
572 SKB_LINEAR_ASSERT(skb);
573
574 skb = xchg(&stir->tx_pending, skb);
575 wake_up_process(stir->thread);
576
577 /* this should never happen unless stop/wakeup problem */
578 if (unlikely(skb)) {
579 WARN_ON(1);
580 dev_kfree_skb(skb);
581 }
582
583 return NETDEV_TX_OK;
584 }
585
586 /*
587 * Wait for the transmit FIFO to have space for next data
588 *
589 * If space < 0 then wait till FIFO completely drains.
590 * FYI: can take up to 13 seconds at 2400baud.
591 */
592 static int fifo_txwait(struct stir_cb *stir, int space)
593 {
594 int err;
595 unsigned long count, status;
596 unsigned long prev_count = 0x1fff;
597
598 /* Read FIFO status and count */
599 for (;; prev_count = count) {
600 err = read_reg(stir, REG_FIFOCTL, stir->fifo_status,
601 FIFO_REGS_SIZE);
602 if (unlikely(err != FIFO_REGS_SIZE)) {
603 dev_warn(&stir->netdev->dev,
604 "FIFO register read error: %d\n", err);
605
606 return err;
607 }
608
609 status = stir->fifo_status[0];
610 count = (unsigned)(stir->fifo_status[2] & 0x1f) << 8
611 | stir->fifo_status[1];
612
613 pr_debug("fifo status 0x%lx count %lu\n", status, count);
614
615 /* is fifo receiving already, or empty */
616 if (!(status & FIFOCTL_DIR) ||
617 (status & FIFOCTL_EMPTY))
618 return 0;
619
620 if (signal_pending(current))
621 return -EINTR;
622
623 /* shutting down? */
624 if (!netif_running(stir->netdev) ||
625 !netif_device_present(stir->netdev))
626 return -ESHUTDOWN;
627
628 /* only waiting for some space */
629 if (space >= 0 && STIR_FIFO_SIZE - 4 > space + count)
630 return 0;
631
632 /* queue confused */
633 if (prev_count < count)
634 break;
635
636 /* estimate transfer time for remaining chars */
637 msleep((count * 8000) / stir->speed);
638 }
639
640 err = write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR);
641 if (err)
642 return err;
643 err = write_reg(stir, REG_FIFOCTL, 0);
644 if (err)
645 return err;
646
647 return 0;
648 }
649
650
651 /* Wait for turnaround delay before starting transmit. */
652 static void turnaround_delay(const struct stir_cb *stir, long us)
653 {
654 long ticks;
655
656 if (us <= 0)
657 return;
658
659 us -= ktime_us_delta(ktime_get(), stir->rx_time);
660
661 if (us < 10)
662 return;
663
664 ticks = us / (1000000 / HZ);
665 if (ticks > 0)
666 schedule_timeout_interruptible(1 + ticks);
667 else
668 udelay(us);
669 }
670
671 /*
672 * Start receiver by submitting a request to the receive pipe.
673 * If nothing is available it will return after rx_interval.
674 */
675 static int receive_start(struct stir_cb *stir)
676 {
677 /* reset state */
678 stir->receiving = 1;
679
680 stir->rx_buff.in_frame = FALSE;
681 stir->rx_buff.state = OUTSIDE_FRAME;
682
683 stir->rx_urb->status = 0;
684 return usb_submit_urb(stir->rx_urb, GFP_KERNEL);
685 }
686
687 /* Stop all pending receive Urb's */
688 static void receive_stop(struct stir_cb *stir)
689 {
690 stir->receiving = 0;
691 usb_kill_urb(stir->rx_urb);
692
693 if (stir->rx_buff.in_frame)
694 stir->netdev->stats.collisions++;
695 }
696 /*
697 * Wrap data in socket buffer and send it.
698 */
699 static void stir_send(struct stir_cb *stir, struct sk_buff *skb)
700 {
701 unsigned wraplen;
702 int first_frame = 0;
703
704 /* if receiving, need to turnaround */
705 if (stir->receiving) {
706 receive_stop(stir);
707 turnaround_delay(stir, irda_get_mtt(skb));
708 first_frame = 1;
709 }
710
711 if (isfir(stir->speed))
712 wraplen = wrap_fir_skb(skb, stir->io_buf);
713 else
714 wraplen = wrap_sir_skb(skb, stir->io_buf);
715
716 /* check for space available in fifo */
717 if (!first_frame)
718 fifo_txwait(stir, wraplen);
719
720 stir->netdev->stats.tx_packets++;
721 stir->netdev->stats.tx_bytes += skb->len;
722 netif_trans_update(stir->netdev);
723 pr_debug("send %d (%d)\n", skb->len, wraplen);
724
725 if (usb_bulk_msg(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1),
726 stir->io_buf, wraplen,
727 NULL, TRANSMIT_TIMEOUT))
728 stir->netdev->stats.tx_errors++;
729 }
730
731 /*
732 * Transmit state machine thread
733 */
734 static int stir_transmit_thread(void *arg)
735 {
736 struct stir_cb *stir = arg;
737 struct net_device *dev = stir->netdev;
738 struct sk_buff *skb;
739
740 while (!kthread_should_stop()) {
741 #ifdef CONFIG_PM
742 /* if suspending, then power off and wait */
743 if (unlikely(freezing(current))) {
744 if (stir->receiving)
745 receive_stop(stir);
746 else
747 fifo_txwait(stir, -1);
748
749 write_reg(stir, REG_CTRL1, CTRL1_TXPWD|CTRL1_RXPWD);
750
751 try_to_freeze();
752
753 if (change_speed(stir, stir->speed))
754 break;
755 }
756 #endif
757
758 /* if something to send? */
759 skb = xchg(&stir->tx_pending, NULL);
760 if (skb) {
761 unsigned new_speed = irda_get_next_speed(skb);
762 netif_wake_queue(dev);
763
764 if (skb->len > 0)
765 stir_send(stir, skb);
766 dev_kfree_skb(skb);
767
768 if ((new_speed != -1) && (stir->speed != new_speed)) {
769 if (fifo_txwait(stir, -1) ||
770 change_speed(stir, new_speed))
771 break;
772 }
773 continue;
774 }
775
776 /* nothing to send? start receiving */
777 if (!stir->receiving &&
778 irda_device_txqueue_empty(dev)) {
779 /* Wait otherwise chip gets confused. */
780 if (fifo_txwait(stir, -1))
781 break;
782
783 if (unlikely(receive_start(stir))) {
784 if (net_ratelimit())
785 dev_info(&dev->dev,
786 "%s: receive usb submit failed\n",
787 stir->netdev->name);
788 stir->receiving = 0;
789 msleep(10);
790 continue;
791 }
792 }
793
794 /* sleep if nothing to send */
795 set_current_state(TASK_INTERRUPTIBLE);
796 schedule();
797
798 }
799 return 0;
800 }
801
802
803 /*
804 * USB bulk receive completion callback.
805 * Wakes up every ms (usb round trip) with wrapped
806 * data.
807 */
808 static void stir_rcv_irq(struct urb *urb)
809 {
810 struct stir_cb *stir = urb->context;
811 int err;
812
813 /* in process of stopping, just drop data */
814 if (!netif_running(stir->netdev))
815 return;
816
817 /* unlink, shutdown, unplug, other nasties */
818 if (urb->status != 0)
819 return;
820
821 if (urb->actual_length > 0) {
822 pr_debug("receive %d\n", urb->actual_length);
823 unwrap_chars(stir, urb->transfer_buffer,
824 urb->actual_length);
825
826 stir->rx_time = ktime_get();
827 }
828
829 /* kernel thread is stopping receiver don't resubmit */
830 if (!stir->receiving)
831 return;
832
833 /* resubmit existing urb */
834 err = usb_submit_urb(urb, GFP_ATOMIC);
835
836 /* in case of error, the kernel thread will restart us */
837 if (err) {
838 dev_warn(&stir->netdev->dev, "usb receive submit error: %d\n",
839 err);
840 stir->receiving = 0;
841 wake_up_process(stir->thread);
842 }
843 }
844
845 /*
846 * Function stir_net_open (dev)
847 *
848 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
849 */
850 static int stir_net_open(struct net_device *netdev)
851 {
852 struct stir_cb *stir = netdev_priv(netdev);
853 int err;
854 char hwname[16];
855
856 err = usb_clear_halt(stir->usbdev, usb_sndbulkpipe(stir->usbdev, 1));
857 if (err)
858 goto err_out1;
859 err = usb_clear_halt(stir->usbdev, usb_rcvbulkpipe(stir->usbdev, 2));
860 if (err)
861 goto err_out1;
862
863 err = change_speed(stir, 9600);
864 if (err)
865 goto err_out1;
866
867 err = -ENOMEM;
868
869 /* Initialize for SIR/FIR to copy data directly into skb. */
870 stir->receiving = 0;
871 stir->rx_buff.truesize = IRDA_SKB_MAX_MTU;
872 stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
873 if (!stir->rx_buff.skb)
874 goto err_out1;
875
876 skb_reserve(stir->rx_buff.skb, 1);
877 stir->rx_buff.head = stir->rx_buff.skb->data;
878 stir->rx_time = ktime_get();
879
880 stir->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
881 if (!stir->rx_urb)
882 goto err_out2;
883
884 stir->io_buf = kmalloc(STIR_FIFO_SIZE, GFP_KERNEL);
885 if (!stir->io_buf)
886 goto err_out3;
887
888 usb_fill_bulk_urb(stir->rx_urb, stir->usbdev,
889 usb_rcvbulkpipe(stir->usbdev, 2),
890 stir->io_buf, STIR_FIFO_SIZE,
891 stir_rcv_irq, stir);
892
893 stir->fifo_status = kmalloc(FIFO_REGS_SIZE, GFP_KERNEL);
894 if (!stir->fifo_status)
895 goto err_out4;
896
897 /*
898 * Now that everything should be initialized properly,
899 * Open new IrLAP layer instance to take care of us...
900 * Note : will send immediately a speed change...
901 */
902 sprintf(hwname, "usb#%d", stir->usbdev->devnum);
903 stir->irlap = irlap_open(netdev, &stir->qos, hwname);
904 if (!stir->irlap) {
905 dev_err(&stir->usbdev->dev, "irlap_open failed\n");
906 goto err_out5;
907 }
908
909 /** Start kernel thread for transmit. */
910 stir->thread = kthread_run(stir_transmit_thread, stir,
911 "%s", stir->netdev->name);
912 if (IS_ERR(stir->thread)) {
913 err = PTR_ERR(stir->thread);
914 dev_err(&stir->usbdev->dev, "unable to start kernel thread\n");
915 goto err_out6;
916 }
917
918 netif_start_queue(netdev);
919
920 return 0;
921
922 err_out6:
923 irlap_close(stir->irlap);
924 err_out5:
925 kfree(stir->fifo_status);
926 err_out4:
927 kfree(stir->io_buf);
928 err_out3:
929 usb_free_urb(stir->rx_urb);
930 err_out2:
931 kfree_skb(stir->rx_buff.skb);
932 err_out1:
933 return err;
934 }
935
936 /*
937 * Function stir_net_close (stir)
938 *
939 * Network device is taken down. Usually this is done by
940 * "ifconfig irda0 down"
941 */
942 static int stir_net_close(struct net_device *netdev)
943 {
944 struct stir_cb *stir = netdev_priv(netdev);
945
946 /* Stop transmit processing */
947 netif_stop_queue(netdev);
948
949 /* Kill transmit thread */
950 kthread_stop(stir->thread);
951 kfree(stir->fifo_status);
952
953 /* Mop up receive urb's */
954 usb_kill_urb(stir->rx_urb);
955
956 kfree(stir->io_buf);
957 usb_free_urb(stir->rx_urb);
958 kfree_skb(stir->rx_buff.skb);
959
960 /* Stop and remove instance of IrLAP */
961 if (stir->irlap)
962 irlap_close(stir->irlap);
963
964 stir->irlap = NULL;
965
966 return 0;
967 }
968
969 /*
970 * IOCTLs : Extra out-of-band network commands...
971 */
972 static int stir_net_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
973 {
974 struct if_irda_req *irq = (struct if_irda_req *) rq;
975 struct stir_cb *stir = netdev_priv(netdev);
976 int ret = 0;
977
978 switch (cmd) {
979 case SIOCSBANDWIDTH: /* Set bandwidth */
980 if (!capable(CAP_NET_ADMIN))
981 return -EPERM;
982
983 /* Check if the device is still there */
984 if (netif_device_present(stir->netdev))
985 ret = change_speed(stir, irq->ifr_baudrate);
986 break;
987
988 case SIOCSMEDIABUSY: /* Set media busy */
989 if (!capable(CAP_NET_ADMIN))
990 return -EPERM;
991
992 /* Check if the IrDA stack is still there */
993 if (netif_running(stir->netdev))
994 irda_device_set_media_busy(stir->netdev, TRUE);
995 break;
996
997 case SIOCGRECEIVING:
998 /* Only approximately true */
999 irq->ifr_receiving = stir->receiving;
1000 break;
1001
1002 default:
1003 ret = -EOPNOTSUPP;
1004 }
1005
1006 return ret;
1007 }
1008
1009 static const struct net_device_ops stir_netdev_ops = {
1010 .ndo_open = stir_net_open,
1011 .ndo_stop = stir_net_close,
1012 .ndo_start_xmit = stir_hard_xmit,
1013 .ndo_do_ioctl = stir_net_ioctl,
1014 };
1015
1016 /*
1017 * This routine is called by the USB subsystem for each new device
1018 * in the system. We need to check if the device is ours, and in
1019 * this case start handling it.
1020 * Note : it might be worth protecting this function by a global
1021 * spinlock... Or not, because maybe USB already deal with that...
1022 */
1023 static int stir_probe(struct usb_interface *intf,
1024 const struct usb_device_id *id)
1025 {
1026 struct usb_device *dev = interface_to_usbdev(intf);
1027 struct stir_cb *stir = NULL;
1028 struct net_device *net;
1029 int ret = -ENOMEM;
1030
1031 /* Allocate network device container. */
1032 net = alloc_irdadev(sizeof(*stir));
1033 if(!net)
1034 goto err_out1;
1035
1036 SET_NETDEV_DEV(net, &intf->dev);
1037 stir = netdev_priv(net);
1038 stir->netdev = net;
1039 stir->usbdev = dev;
1040
1041 ret = usb_reset_configuration(dev);
1042 if (ret != 0) {
1043 dev_err(&intf->dev, "usb reset configuration failed\n");
1044 goto err_out2;
1045 }
1046
1047 printk(KERN_INFO "SigmaTel STIr4200 IRDA/USB found at address %d, "
1048 "Vendor: %x, Product: %x\n",
1049 dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
1050 le16_to_cpu(dev->descriptor.idProduct));
1051
1052 /* Initialize QoS for this device */
1053 irda_init_max_qos_capabilies(&stir->qos);
1054
1055 /* That's the Rx capability. */
1056 stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 |
1057 IR_38400 | IR_57600 | IR_115200 |
1058 (IR_4000000 << 8);
1059 stir->qos.min_turn_time.bits &= qos_mtt_bits;
1060 irda_qos_bits_to_value(&stir->qos);
1061
1062 /* Override the network functions we need to use */
1063 net->netdev_ops = &stir_netdev_ops;
1064
1065 ret = register_netdev(net);
1066 if (ret != 0)
1067 goto err_out2;
1068
1069 dev_info(&intf->dev, "IrDA: Registered SigmaTel device %s\n",
1070 net->name);
1071
1072 usb_set_intfdata(intf, stir);
1073
1074 return 0;
1075
1076 err_out2:
1077 free_netdev(net);
1078 err_out1:
1079 return ret;
1080 }
1081
1082 /*
1083 * The current device is removed, the USB layer tell us to shut it down...
1084 */
1085 static void stir_disconnect(struct usb_interface *intf)
1086 {
1087 struct stir_cb *stir = usb_get_intfdata(intf);
1088
1089 if (!stir)
1090 return;
1091
1092 unregister_netdev(stir->netdev);
1093 free_netdev(stir->netdev);
1094
1095 usb_set_intfdata(intf, NULL);
1096 }
1097
1098 #ifdef CONFIG_PM
1099 /* USB suspend, so power off the transmitter/receiver */
1100 static int stir_suspend(struct usb_interface *intf, pm_message_t message)
1101 {
1102 struct stir_cb *stir = usb_get_intfdata(intf);
1103
1104 netif_device_detach(stir->netdev);
1105 return 0;
1106 }
1107
1108 /* Coming out of suspend, so reset hardware */
1109 static int stir_resume(struct usb_interface *intf)
1110 {
1111 struct stir_cb *stir = usb_get_intfdata(intf);
1112
1113 netif_device_attach(stir->netdev);
1114
1115 /* receiver restarted when send thread wakes up */
1116 return 0;
1117 }
1118 #endif
1119
1120 /*
1121 * USB device callbacks
1122 */
1123 static struct usb_driver irda_driver = {
1124 .name = "stir4200",
1125 .probe = stir_probe,
1126 .disconnect = stir_disconnect,
1127 .id_table = dongles,
1128 #ifdef CONFIG_PM
1129 .suspend = stir_suspend,
1130 .resume = stir_resume,
1131 #endif
1132 };
1133
1134 module_usb_driver(irda_driver);