Linux Audio

Check our new training course

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