diff -urN -X dontdiff linux-2.5/drivers/net/irda/Kconfig irda-2.5/drivers/net/irda/Kconfig --- linux-2.5/drivers/net/irda/Kconfig 2003-10-09 09:34:13.000000000 -0700 +++ irda-2.5/drivers/net/irda/Kconfig 2003-10-23 15:12:49.000000000 -0700 @@ -223,6 +223,26 @@ Please note that the driver is still experimental. And of course, you will need both USB and IrDA support in your kernel... +config SIGMATEL_FIR + tristate "SigmaTel USB dongles (EXPERIMENTAL)" + depends on IRDA && USB && EXPERIMENTAL + select CRC32 + ---help--- + Say Y here if you want to build support for the SigmaTel STIr4200 + USB IrDA FIR Dongle device driver. + + USB dongles based on the SigmaTel STIr4200 don't conform to the + IrDA-USB device class specification, and therefore need their + own specific driver. Those dongles support SIR and FIR (4Mbps) + speeds. On the other hand, those dongles tend to be less efficient + than a FIR chipset. + + To compile it as a module, choose M here: the module will be called + stir4200. + + Please note that the driver is still experimental. And of course, + you will need both USB and IrDA support in your kernel... + config NSC_FIR tristate "NSC PC87108/PC87338" depends on IRDA && ISA diff -urN -X dontdiff linux-2.5/drivers/net/irda/Makefile irda-2.5/drivers/net/irda/Makefile --- linux-2.5/drivers/net/irda/Makefile 2003-10-09 09:34:14.000000000 -0700 +++ irda-2.5/drivers/net/irda/Makefile 2003-10-22 15:12:09.000000000 -0700 @@ -9,6 +9,7 @@ obj-$(CONFIG_IRPORT_SIR) += irport.o # FIR drivers obj-$(CONFIG_USB_IRDA) += irda-usb.o +obj-$(CONFIG_SIGMATEL_FIR) += stir4200.o obj-$(CONFIG_NSC_FIR) += nsc-ircc.o obj-$(CONFIG_WINBOND_FIR) += w83977af_ir.o obj-$(CONFIG_SA1100_FIR) += sa1100_ir.o diff -urN -X dontdiff linux-2.5/drivers/net/irda/stir4200.c irda-2.5/drivers/net/irda/stir4200.c --- linux-2.5/drivers/net/irda/stir4200.c 1969-12-31 16:00:00.000000000 -0800 +++ irda-2.5/drivers/net/irda/stir4200.c 2003-11-14 15:52:16.557823984 -0800 @@ -0,0 +1,1145 @@ +/***************************************************************************** +* +* Filename: stir4200.c +* Version: 0.3 +* Description: Irda SigmaTel USB Dongle +* Status: Experimental +* Author: Stephen Hemminger +* +* Based on earlier driver by Paul Stewart +* +* Copyright (C) 2000, Roman Weissgaerber +* Copyright (C) 2001, Dag Brattli +* Copyright (C) 2001, Jean Tourrilhes +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +* +*****************************************************************************/ + +/* + * This dongle does no framing, and requires polling to receive the + * data. The STIr4200 has bulk in and out endpoints just like + * usr-irda devices, but the data it sends and receives is raw; like + * irtty, it needs to call the wrap and unwrap functions to add and + * remove SOF/BOF and escape characters to/from the frame. It doesn't + * have an interrupt endpoint like the IrDA-USB devices. + */ + + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "stir4200.h" + +static int qos_mtt_bits = 0; +static int rx_sensitivity = 0; /* 0 = highest ... 7 = lowest */ +static int tx_power = 0; /* 0 = highest ... 3 = lowest */ + +/* These are the currently known IrDA USB dongles. Add new dongles here */ +static struct usb_device_id dongles[] = { + /* SigmaTel, Inc, STIr4200 IrDA/USB Bridge */ + { USB_DEVICE(0x066f, 0x4200), .driver_info = 0 }, + { }, /* The end */ +}; + +MODULE_DEVICE_TABLE(usb, dongles); + +/************************ REGISTER OPERATIONS ************************/ + +static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value) +{ + struct usb_device *dev = stir->usbdev; + + IRDA_DEBUG(9, "%s() write reg %d <= 0x%x\n", __FUNCTION__, reg, value); + + return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), + REQ_WRITE_SINGLE, + USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE, + value, reg, NULL, 0, + MSECS_TO_JIFFIES(CTRL_TIMEOUT)); +} + +static int read_reg(struct stir_cb *stir, __u16 reg, + __u8 *data, __u16 count) +{ + struct usb_device *dev = stir->usbdev; + + return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), + REQ_READ_REG, + USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, + 0, reg, data, count, + MSECS_TO_JIFFIES(CTRL_TIMEOUT)); +} + +#if STIR_FIFO_SIZE == PAGESIZE +/* This is just an optimization of memory usage for the common case */ +static inline void *get_fifo_buf(void) +{ + return (void *) __get_free_page(GFP_KERNEL); +} + +static inline void free_fifo_buf(void *buf) +{ + free_page((unsigned long) self->ctrl_buf); + +} +#else +static inline void *get_fifo_buf(void) +{ + return kmalloc(STIR_FIFO_SIZE, GFP_KERNEL); +} + +static inline void free_fifo_buf(void *buf) +{ + kfree(buf); + +} +#endif + +/*********************** FIR WRAPPER ROUTINES ***********************/ + +/* Prepare a SIR IrDA frame for transmission to the USB dongle. */ +static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf, int size) +{ + __u16 wraplen; + + wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER, + size - STIR_IRDA_HEADER); + + buf[0] = 0x55; + buf[1] = 0xAA; + buf[2] = wraplen & 0xff; + buf[3] = (wraplen >> 8) & 0xff; + + return wraplen + STIR_IRDA_HEADER; +} + +/* + * Prepare a FIR IrDA frame for transmission to the USB dongle. The + * FIR transmit frame is documented in the datasheet. It consists of + * a two byte 0x55 0xAA sequence, two little-endian length bytes, a + * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E, + * then the data escaped as follows: + * + * 0x7D -> 0x7D 0x5D + * 0x7E -> 0x7D 0x5E + * 0x7F -> 0x7D 0x5F + * + * Then, 4 bytes of little endian (stuffed) FCS follow, then two + * trailing EOF bytes of 0x7E. + */ +static inline int stuff_fir_byte(__u8 *buf, __u8 c) +{ + if (c == 0x7d || c == 0x7e || c == 0x7f) { + *buf++ = 0x7d; + *buf = c ^ 0x20; + return 2; + } + *buf = c; + return 1; +} + + +/* Take raw data in skb and put it wrapped into buf */ +static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf, int size) +{ + __u8 *ptr = buf + STIR_IRDA_HEADER; + __u32 fcs = ~(crc32_le(~0, skb->data, skb->len)); + __u16 wraplen; + int i; + + /* Preamble */ + memset(ptr, 0x7f, 16); + ptr += 16; + + /* BOFs */ + *ptr++ = 0x7e; + *ptr++ = 0x7e; + + /* Address / Control / Information */ + for (i = 0; i < skb->len; i++) + ptr += stuff_fir_byte(ptr, skb->data[i]); + + /* FCS */ + ptr += stuff_fir_byte(ptr, fcs & 0xff); + ptr += stuff_fir_byte(ptr, (fcs >> 8) & 0xff); + ptr += stuff_fir_byte(ptr, (fcs >> 16) & 0xff); + ptr += stuff_fir_byte(ptr, (fcs >> 24) & 0xff); + + /* EOFs */ + *ptr++ = 0x7e; + *ptr++ = 0x7e; + + /* Total length, minus the header */ + wraplen = ptr - buf - STIR_IRDA_HEADER; + buf[0] = 0x55; + buf[1] = 0xAA; + buf[2] = wraplen & 0xff; + buf[3] = (wraplen >> 8) & 0xff; + + return (ptr - buf); +} + +/*Got a frame, allocate new one and pass it up the stack! */ +static void fir_bump(struct net_device *dev, + struct net_device_stats *stats, + iobuff_t *rx_buff) +{ + struct sk_buff *oskb = rx_buff->skb; + int len = rx_buff->len - 4; + struct sk_buff *nskb; + + nskb = dev_alloc_skb(IRDA_SKB_MAX_MTU); + /* If can't get new buffer, just drop and reuse */ + if (nskb) { + skb_reserve(nskb, 1); + + /* Set correct length in socket buffer */ + skb_put(oskb, len); + + oskb->mac.raw = oskb->data; + oskb->protocol = htons(ETH_P_IRDA); + oskb->dev = dev; + + netif_rx(oskb); + rx_buff->skb = nskb; + rx_buff->head = nskb->data; + + /* Feed it to IrLAP layer */ + stats->rx_packets++; + stats->rx_bytes += len; + + } + else + ++stats->rx_dropped; + + rx_buff->data = rx_buff->head; + rx_buff->len = 0; +} + +/* Handle End Of Frame character received within a frame */ +static inline void unwrap_fir_eof(struct stir_cb *stir, + iobuff_t *rx_buff, __u8 byte) +{ + __u32 fcs_frame; + __u32 fcs_calc; + + switch(rx_buff->state) { + + case BEGIN_FRAME: + case LINK_ESCAPE: + case INSIDE_FRAME: + default: + /* We receive multiple BOF/EOF */ + if(rx_buff->len == 0) + break; + + /* Note : in the case of BEGIN_FRAME and LINK_ESCAPE, + * the fcs will most likely not match and generate an + * error, as expected - Jean II */ + rx_buff->state = OUTSIDE_FRAME; + rx_buff->in_frame = FALSE; + + /* We can't inline the CRC calculation, as we have + * nowhere to store it in rx_buff... Jean II */ + if(rx_buff->len > 4) { + fcs_calc = ~(crc32_le(~0, rx_buff->data, + rx_buff->len - 4)); + fcs_frame = (rx_buff->data[rx_buff->len - 4] | + (rx_buff->data[rx_buff->len - 3] << 8) | + (rx_buff->data[rx_buff->len - 2] << 16) | + (rx_buff->data[rx_buff->len - 1] << 24)); + IRDA_DEBUG(0, "%s(), crc = 0x%X, crc = 0x%X, len = %d\n", + __FUNCTION__, fcs_calc, fcs_frame, rx_buff->len); + } else { + fcs_calc = 0; + fcs_frame = 1; + } + + /* You may see an abnormal number of CRC failures around + * there... This is due to a nice hardware bug in + * the STIr4200. Quite often, the hardware will + * pass us two or more packets in a URB without any + * BOF/EOF in between. The CRC is the one of the last + * packet, but we will treat those packets as a single + * packet, so it won't match. + * Of course, because there is no separators, there is + * no way we can correct this bug. + * Jean II */ + + /* Test FCS and signal success if the frame is good */ + if (fcs_calc == fcs_frame) { + /* Deliver frame */ + fir_bump(stir->netdev, &stir->stats, rx_buff); + } else { + /* Wrong CRC, discard frame! */ + irda_device_set_media_busy(stir->netdev, TRUE); + IRDA_DEBUG(0, "%s(), crc error\n", __FUNCTION__); + stir->stats.rx_errors++; + stir->stats.rx_crc_errors++; + } + + /* Fall through : We may receive only a single BOF/EOF */ + case OUTSIDE_FRAME: + /* BOF == EOF, so beware... */ + + /* Now receiving frame */ + rx_buff->state = BEGIN_FRAME; + rx_buff->in_frame = TRUE; + + /* Time to initialize receive buffer */ + rx_buff->data = rx_buff->head; + rx_buff->len = 0; + break; + } +} + +/* Handle Character Escape character received within a frame */ +static inline void unwrap_fir_ce(struct stir_cb *stir, + iobuff_t *rx_buff, __u8 byte) +{ + switch(rx_buff->state) { + case OUTSIDE_FRAME: + /* Activate carrier sense */ + irda_device_set_media_busy(stir->netdev, TRUE); + break; + + case LINK_ESCAPE: + WARNING("%s: state not defined\n", __FUNCTION__); + break; + + case BEGIN_FRAME: + case INSIDE_FRAME: + default: + /* Stuffed byte comming */ + rx_buff->state = LINK_ESCAPE; + break; + } +} + +/* Handle other characters received within a frame */ +static inline void unwrap_fir_other(struct stir_cb *stir, + iobuff_t *rx_buff, __u8 byte) +{ + switch(rx_buff->state) { + /* This is on the critical path, case are ordered by + * probability (most frequent first) - Jean II */ + case INSIDE_FRAME: + /* Must be the next byte of the frame */ + if (rx_buff->len < rx_buff->truesize) { + rx_buff->data[rx_buff->len++] = byte; + } else { + IRDA_DEBUG(1, "%s(), Rx buffer overflow, aborting\n", + __FUNCTION__); + rx_buff->state = OUTSIDE_FRAME; + } + break; + + case LINK_ESCAPE: + /* + * Stuffed char, complement bit 5 of byte + * following CE, IrLAP p.114 + */ + byte ^= IRDA_TRANS; + if (rx_buff->len < rx_buff->truesize) { + rx_buff->data[rx_buff->len++] = byte; + rx_buff->state = INSIDE_FRAME; + } else { + IRDA_DEBUG(1, "%s(), Rx buffer overflow, aborting\n", + __FUNCTION__); + rx_buff->state = OUTSIDE_FRAME; + } + break; + + case OUTSIDE_FRAME: + /* Activate carrier sense */ + if(byte != XBOF) + irda_device_set_media_busy(stir->netdev, TRUE); + break; + + case BEGIN_FRAME: + default: + rx_buff->data[rx_buff->len++] = byte; + rx_buff->state = INSIDE_FRAME; + break; + } +} + +/* Unwrap FIR stuffed dataa and bump it to IrLAP */ +static void async_fir_chars(struct stir_cb *stir, + iobuff_t *rx_buff, + const __u8 *bytes, int len) +{ + __u8 byte; + int i; + + /* Having the loop here is more efficient - Jean II */ + for (i = 0; i < len; i++) { + byte = bytes[i]; + switch(byte) { + case FIR_CE: + unwrap_fir_ce(stir, rx_buff, byte); + break; + case FIR_XBOF: + IRDA_DEBUG(0, "%s: saw 0x%x in FIR data?\n", + stir->netdev->name, byte); + continue; + case FIR_EOF: + unwrap_fir_eof(stir, rx_buff, byte); + break; + default: + unwrap_fir_other(stir, rx_buff, byte); + break; + } + } +} + +/* Unwrap SIR stuffed data and bump it up to IrLAP */ +static void async_sir_chars(struct stir_cb *stir, iobuff_t *rx_buff, + const __u8 *bytes, int len) +{ + int i; + + for (i = 0; i < len; i++) + async_unwrap_char(stir->netdev, &stir->stats, rx_buff, bytes[i]); +} + +/************************ TRANSMIT ROUTINES ************************/ + +static struct stir_speed { + int speed; + __u8 mode; + __u8 pdclk; +} stir_modes[] = { + { 2400, MODE_SIR|MODE_2400|MODE_NRESET, PDCLK_2400 }, + { 9600, MODE_SIR|MODE_NRESET, PDCLK_9600 }, + { 19200, MODE_SIR|MODE_NRESET, PDCLK_19200 }, + { 38400, MODE_SIR|MODE_NRESET, PDCLK_38400 }, + { 57600, MODE_SIR|MODE_NRESET, PDCLK_57600 }, + { 115200, MODE_SIR|MODE_NRESET, PDCLK_115200 }, + { 4000000, MODE_FIR|MODE_NRESET, PDCLK_4000000 }, +}; + +static inline int isfir(u32 speed) +{ + return (speed == 4000000); +} + +/* + * This function returns the bytes that should be programmed into the + * MODE and PDCLK registers, respectively, in order to get a desired + * transmit and receive bitrate. + */ +static int change_speed(struct stir_cb *stir, int speed) +{ + int i, ret; + + for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) { + if (speed == stir_modes[i].speed) + goto found; + } + + ERROR("%s: invalid speed %d\n", stir->netdev->name, speed); + return -EINVAL; + + found: + IRDA_DEBUG(0, "%s() old %d new %d\n", __FUNCTION__, + stir->speed, speed); + + stir->speed = speed; + + /* Write multiple registers doesn't appear to work */ + ret = write_reg(stir, REG_MODE, stir_modes[i].mode); + if (ret == 0) + ret = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk); + + return ret; +} + +/* + * Called from net/core when new frame is available. + * Put in queue to send. + * We block and wakeup after each send to try and avoid overrunning FIFO. + */ +static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev) +{ + struct stir_cb *stir = netdev->priv; + + netif_stop_queue(netdev); + + skb_queue_tail(&stir->tx_queue, skb); + wake_up(&stir->thr_wait); + return 0; +} + +/* + * Wait for the FIFO to complete sending last frame before + * changing speed. + */ +static void wait_for_send(struct stir_cb *stir) +{ + __u8 status; + + while (read_reg(stir, REG_FIFOCTL, &status, 1) == 1 && + (status & FIFOCTL_DIR)) { + current->state = TASK_INTERRUPTIBLE; + schedule_timeout(1); + } +} + +/* Wait for turnaround delay before turning around on transmit. */ +static inline void turnaround_delay(unsigned long us) +{ + if (us > 0) { + if (us > 1000) + wait_ms(us/1000); + else if (us > 1) + udelay(us); + } +} + +/* + * Send a frame using the bulk endpoint of the STIr4200. + * Raw wrapping has already been done, and the module is idle. + */ +static void stir_send(struct stir_cb *stir, struct sk_buff *skb) +{ + int new_speed = irda_get_next_speed(skb); + int len; + + IRDA_DEBUG(5, "%s()\n", __FUNCTION__); + + len = isfir(new_speed) + ? wrap_fir_skb(skb, stir->tx_data, STIR_FIFO_SIZE) + : wrap_sir_skb(skb, stir->tx_data, STIR_FIFO_SIZE); + + if (len < 0) + goto error; + + turnaround_delay(irda_get_mtt(skb)); + + stir->netdev->trans_start = jiffies; + + if (usb_bulk_msg(stir->usbdev, + usb_sndbulkpipe(stir->usbdev, + stir->bulk_out_ep), + stir->tx_data, len, NULL, + MSECS_TO_JIFFIES(BULK_TIMEOUT)) != 0) + goto error; + + stir->stats.tx_packets++; + stir->stats.tx_bytes += skb->len; + + /* Speed changes take effect after send is complete. */ + if (new_speed != stir->speed) { + wait_for_send(stir); + change_speed(stir, new_speed); + } + out: + kfree_skb(skb); + return; + error: + stir->stats.tx_errors++; + goto out; +} + +/************************* RECEIVE ROUTINES *************************/ +/* + * Receive wrapped data into rx_data buffer. + * This chip doesn't block until data is available, we just have + * to read the FIFO perodically (ugh). + */ +static int stir_receive(struct stir_cb *stir) +{ + int ret, count = 0; + + IRDA_DEBUG(9, "%s()\n", __FUNCTION__); + + for(;;) { + int length = 0; + + ret = usb_bulk_msg(stir->usbdev, + usb_rcvbulkpipe(stir->usbdev, stir->bulk_in_ep), + stir->rx_data, STIR_FIFO_SIZE, + &length, MSECS_TO_JIFFIES(BULK_TIMEOUT)); + + IRDA_DEBUG(8, "%s() ret=%d len=%d\n", __FUNCTION__, ret, length); + + if (ret != 0) { + WARNING("%s: received status %d\n", stir->netdev->name, ret); + stir->stats.rx_errors++; + return ret; + } + + if (length == 0) /* no more data */ + break; + + ++count; + stir->netdev->last_rx = jiffies; + + if (isfir(stir->speed)) + async_fir_chars(stir, &stir->rx_buff, + stir->rx_data, length); + else + async_sir_chars(stir, &stir->rx_buff, + stir->rx_data, length); + + } + + return count; + +} + +/* + * Fifo has overrun, this will happen if we get too agressive in + * loading data or if too lazy about receive polling + */ +static void fifo_error(struct stir_cb *stir, __u8 status) +{ + IRDA_DEBUG(0, "%s: FIFO overrun\n", __FUNCTION__); + + if (status & FIFOCTL_DIR) + ++stir->stats.tx_fifo_errors; + else + ++stir->stats.rx_fifo_errors; + + if (write_reg(stir, REG_FIFOCTL, FIFOCTL_CLR) < 0) + ERROR("%s: FIFO reset failed\n", stir->netdev->name); + + write_reg(stir, REG_FIFOCTL, 0); + + /* Reset receive state machine */ + stir->rx_buff.state = OUTSIDE_FRAME; + stir->rx_buff.in_frame = FALSE; + stir->rx_buff.len = 0; +} + +/* + * Polling thread. + * + * Since the device doesn't block for data, and is essentially + * half duplex; use a kernel thread to do the transfers. + * This can block and makes SMP much easier. + */ +static int stir_thread(void *arg) +{ + struct stir_cb *stir = arg; + struct net_device *dev = stir->netdev; + __u8 status; + long timeout = 1; + + + daemonize("%s", dev->name); + allow_signal(SIGTERM); + + IRDA_DEBUG(1, "%s() starting\n", __FUNCTION__); + + netif_start_queue(dev); + + while (netif_running(dev) && !signal_pending(current)) { + if (!stir->rx_buff.in_frame && + !skb_queue_empty(&stir->tx_queue)) { + struct sk_buff *skb; + while ( (skb = skb_dequeue(&stir->tx_queue))) + stir_send(stir, skb); + + netif_wake_queue(dev); + continue; + } + + if (read_reg(stir, REG_FIFOCTL, &status, 1) < 0) { + ERROR("%s: status register read error\n", + dev->name); + goto die; + } + + /* Fifo overrun, blow away data and reset */ + if (status & FIFOCTL_OVER) { + fifo_error(stir, status); + continue; + } + + /* if still sending? don't clobber our FIFO data */ + if (status & FIFOCTL_DIR) + timeout = 1; + else if (status & FIFOCTL_EMPTY) { + long maxto = isfir(stir->speed) ? HZ/100 : HZ/10; + timeout = min(timeout * 2, maxto); + } else if (stir_receive(stir) > 0) + timeout = 1; + + interruptible_sleep_on_timeout(&stir->thr_wait, timeout); + } + die: + IRDA_DEBUG(1, "%s() exiting\n", __FUNCTION__); + complete_and_exit (&stir->thr_exited, 0); +} + +/* + * Function stir_net_open (dev) + * + * Network device is taken up. Usually this is done by "ifconfig irda0 up" + */ +static int stir_net_open(struct net_device *netdev) +{ + struct stir_cb *stir = netdev->priv; + char hwname[16]; + int ret; + + IRDA_DEBUG(1, "%s()\n", __FUNCTION__); + + /* set initial speed and come out of reset */ + ret = change_speed(stir, 9600); + if (ret < 0) { + ERROR("%s(): setting initial speed failed\n", __FUNCTION__); + goto err_out; + } + + /* Write out sensitivity and power values */ + ret = write_reg(stir, REG_CTRL1, (tx_power & 0x3) << 1); + if (ret != 0) { + ERROR("%s(): setting power failed %d\n", + __FUNCTION__, ret); + goto err_out; + } + + ret = write_reg(stir, REG_CTRL2, (rx_sensitivity & 0x7) << 5); + if (ret != 0) { + ERROR("%s(): setting sensitivity failed %d\n", + __FUNCTION__, ret); + goto err_out; + } + + /* + * Now that everything should be initialized properly, + * Open new IrLAP layer instance to take care of us... + * Note : will send immediately a speed change... + */ + sprintf(hwname, "usb#%d", stir->usbdev->devnum); + stir->irlap = irlap_open(netdev, &stir->qos, hwname); + if (!stir->irlap) + return -ENOMEM; + + /* + * Start kernel thread for polling. + */ + stir->thr_pid = kernel_thread(stir_thread, stir, CLONE_FS|CLONE_FILES); + if (stir->thr_pid < 0) { + WARNING("%s: unable to start kernel thread\n", + stir->netdev->name); + + irlap_close(stir->irlap); + ret = stir->thr_pid; + goto err_out; + } + + return 0; + err_out: + return ret; +} + +/* + * Function stir_net_close (stir) + * + * Network device is taken down. Usually this is done by + * "ifconfig irda0 down" + */ +static int stir_net_close(struct net_device *netdev) +{ + struct stir_cb *stir = netdev->priv; + + IRDA_DEBUG(1, "%s()\n", __FUNCTION__); + + /* Stop network Tx queue */ + netif_stop_queue(netdev); + + /* Wait for poll process to exit */ + kill_proc(stir->thr_pid, SIGTERM, 1); + wait_for_completion(&stir->thr_exited); + + skb_queue_purge(&stir->tx_queue); + + /* Stop and remove instance of IrLAP */ + if (stir->irlap) + irlap_close(stir->irlap); + + stir->irlap = NULL; + + return 0; +} + +/* + * IOCTLs : Extra out-of-band network commands... + */ +static int stir_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) +{ + struct if_irda_req *irq = (struct if_irda_req *) rq; + struct stir_cb *stir = dev->priv; + int ret = 0; + + IRDA_DEBUG(8, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd); + + switch (cmd) { + case SIOCSBANDWIDTH: /* Set bandwidth */ + if (!capable(CAP_NET_ADMIN)) + return -EPERM; + + /* Check if the device is still there */ + if (netif_device_present(stir->netdev)) + ret = change_speed(stir, irq->ifr_baudrate); + break; + + case SIOCSMEDIABUSY: /* Set media busy */ + if (!capable(CAP_NET_ADMIN)) + return -EPERM; + + /* Check if the IrDA stack is still there */ + if (netif_running(stir->netdev)) + irda_device_set_media_busy(stir->netdev, TRUE); + break; + + case SIOCGRECEIVING: /* Check if we are receiving right now */ + /* + * This call is not appropriate for this driver because: + * made from irlap_timer code so we can't sleep + * which is how to read FIFO status register; + * and the thread is synchronous so it can handle + * transmit at anytime. + */ + irq->ifr_receiving = 0; + break; + + default: + ret = -EOPNOTSUPP; + } + + return ret; +} + +/* + * Get device stats (for /proc/net/dev and ifconfig) + */ +static struct net_device_stats *stir_net_get_stats(struct net_device *dev) +{ + struct stir_cb *stir = dev->priv; + return &stir->stats; +} + +/********************* IRDA CONFIG SUBROUTINES *********************/ +/* + * Various subroutines dealing with IrDA and network stuff we use to + * configure and initialise each irda-usb instance. + * These functions are used below in the main calls of the driver... + */ + +/* + * Set proper values in the IrDA QOS structure + */ +static void stir_init_qos(struct stir_cb *stir) +{ + IRDA_DEBUG(3, "%s()\n", __FUNCTION__); + + /* Initialize QoS for this device */ + irda_init_max_qos_capabilies(&stir->qos); + + /* That's the Rx capability. */ + stir->qos.baud_rate.bits &= IR_2400 | IR_9600 | IR_19200 | + IR_38400 | IR_57600 | IR_115200 | + (IR_4000000 << 8); + stir->qos.min_turn_time.bits &= 0x7f; /* >= .01ms turnaround */ + + /* Module parameter can override the rx window size */ + if (qos_mtt_bits) + stir->qos.min_turn_time.bits = qos_mtt_bits; + /* + * Note : most of those values apply only for the receive path, + * the transmit path will be set differently - Jean II + */ + irda_qos_bits_to_value(&stir->qos); +} + +/* + * Function stir_parse_endpoints(dev, ifnum) + * + * Parse the various endpoints and find the one we need. + * + * The endpoint are the pipes used to communicate with the USB device. + * The spec defines 2 endpoints of type bulk transfer, one in, and one out. + * These are used to pass frames back and forth with the dongle. + */ +static int stir_parse_endpoints(struct stir_cb *stir, + struct usb_host_endpoint *endpoint, int ennum) +{ + int i; /* Endpoint index in table */ + + /* Init : no endpoints */ + stir->bulk_in_ep = 0; + stir->bulk_out_ep = 0; + + /* Let's look at all those endpoints */ + for(i = 0; i < ennum; i++) { + /* All those variables will get optimised by the compiler, + * so let's aim for clarity... - Jean II */ + __u8 ep; /* Endpoint address */ + __u8 dir; /* Endpoint direction */ + __u8 attr; /* Endpoint attribute */ + __u16 psize; /* Endpoint max packet size in bytes */ + + /* Get endpoint address, direction and attribute */ + ep = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; + dir = endpoint[i].desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK; + attr = endpoint[i].desc.bmAttributes; + psize = endpoint[i].desc.wMaxPacketSize; + + /* Is it a bulk endpoint ??? */ + if(attr == USB_ENDPOINT_XFER_BULK) { + /* We need to find an IN and an OUT */ + if(dir == USB_DIR_IN) { + /* This is our Rx endpoint */ + stir->bulk_in_ep = ep; + } else { + /* This is our Tx endpoint */ + stir->bulk_out_ep = ep; + stir->bulk_out_mtu = psize; + } + } else { + ERROR("%s(), Unrecognized endpoint %02X.\n", + __FUNCTION__, ep); + } + } + + IRDA_DEBUG(0, "%s(), And our endpoints are : in=%02X, out=%02X (%d)\n", + __FUNCTION__, stir->bulk_in_ep, stir->bulk_out_ep, + stir->bulk_out_mtu); + /* Should be 8, 16, 32 or 64 bytes */ + WARN_ON(stir->bulk_out_mtu != 64); + + return((stir->bulk_in_ep != 0) && (stir->bulk_out_ep != 0)); +} + +/* + * This routine is called by the USB subsystem for each new device + * in the system. We need to check if the device is ours, and in + * this case start handling it. + * Note : it might be worth protecting this function by a global + * spinlock... Or not, because maybe USB already deal with that... + */ +static int stir_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + struct usb_device *dev = interface_to_usbdev(intf); + struct stir_cb *stir = NULL; + struct net_device *net; + struct usb_host_interface *interface; + int ret = -ENOMEM; + + MESSAGE("SigmaTel STIr4200 IRDA/USB found at address %d, " + "Vendor: %x, Product: %x\n", + dev->devnum, dev->descriptor.idVendor, + dev->descriptor.idProduct); + + /* Allocate network device container. */ + net = alloc_irdadev(sizeof(*stir)); + if(!net) + goto err_out; + + SET_MODULE_OWNER(net); + stir = net->priv; + stir->netdev = net; + + /* Find our endpoints */ + interface = &intf->altsetting[0]; + if(!stir_parse_endpoints(stir, interface->endpoint, + interface->desc.bNumEndpoints)) { + ERROR("%s(), Bogus endpoints...\n", __FUNCTION__); + ret = -EIO; + goto err_out1; + } + + + stir->usbdev = dev; + stir->speed = -1; /* default after reset value */ + + /* Perform soft reset, and make sure device is okay. */ + ret = write_reg(stir, REG_CTRL1, CTRL1_SRESET); + if (ret == 0) + ret = write_reg(stir, REG_CTRL1, 0); + if (ret) { + ERROR("%s(): reset failed...\n", __FUNCTION__); + goto err_out1; + } + + + /* Initialize for SIR/FIR to copy data directly into skb. */ + stir->rx_buff.in_frame = FALSE; + stir->rx_buff.state = OUTSIDE_FRAME; + stir->rx_buff.truesize = IRDA_SKB_MAX_MTU; + stir->rx_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU); + if (!stir->rx_buff.skb) { + ERROR("%s(), dev_alloc_skb() failed for rxbuf!\n", + __FUNCTION__); + goto err_out1; + } + skb_reserve(stir->rx_buff.skb, 1); + stir->rx_buff.head = stir->rx_buff.skb->data; + + stir->rx_data = get_fifo_buf(); + if (!stir->rx_data) { + ERROR("%s(), alloc failed for rxbuf!\n", __FUNCTION__); + goto err_out2; + } + + stir->tx_data = get_fifo_buf(); + if (!stir->tx_data) { + ERROR("%s(), alloc failed for txbuf!\n", __FUNCTION__); + goto err_out3; + } + + stir_init_qos(stir); + + init_completion (&stir->thr_exited); + init_waitqueue_head (&stir->thr_wait); + + skb_queue_head_init(&stir->tx_queue); + + /* Override the network functions we need to use */ + net->hard_start_xmit = stir_hard_xmit; + net->open = stir_net_open; + net->stop = stir_net_close; + net->get_stats = stir_net_get_stats; + net->do_ioctl = stir_net_ioctl; + ret = register_netdev(net); + if (ret != 0) + goto err_out4; + + + MESSAGE("IrDA: Registered SigmaTel device %s\n", net->name); + + usb_set_intfdata(intf, stir); + + return 0; +err_out4: + free_fifo_buf(stir->tx_data); +err_out3: + free_fifo_buf(stir->rx_data); +err_out2: + kfree_skb(stir->rx_buff.skb); +err_out1: + free_netdev(net); +err_out: + return ret; +} + +/* + * The current irda-usb device is removed, the USB layer tell us + * to shut it down... + * One of the constraints is that when we exit this function, + * we cannot use the usb_device no more. Gone. Destroyed. kfree(). + */ +static void stir_disconnect(struct usb_interface *intf) +{ + struct stir_cb *stir = usb_get_intfdata(intf); + struct net_device *net; + + IRDA_DEBUG(1, "%s()\n", __FUNCTION__); + + usb_set_intfdata(intf, NULL); + if (!stir) + return; + + /* Stop transmitter */ + net = stir->netdev; + netif_device_detach(net); + + /* Remove netdevice */ + unregister_netdev(net); + + /* Free any allocated data buffers */ + kfree_skb(stir->rx_buff.skb); + + /* No longer attached to USB bus */ + stir->usbdev = NULL; + + free_fifo_buf(stir->rx_data); + free_fifo_buf(stir->tx_data); + + free_netdev(net); + + IRDA_DEBUG(0, "%s(), SigmaTel Disconnected\n", __FUNCTION__); +} + + +/* + * USB device callbacks + */ +static struct usb_driver irda_driver = { + .name = "stir4200", + .probe = stir_probe, + .disconnect = stir_disconnect, + .id_table = dongles, + .owner = THIS_MODULE, +}; + +/* + * Module insertion + */ +int __init stir_init(void) +{ + if (usb_register(&irda_driver) < 0) + return -1; + + MESSAGE("SigmaTel support registered\n"); + return 0; +} +module_init(stir_init); + +/* + * Module removal + */ +void __exit stir_cleanup(void) +{ + /* Deregister the driver and remove all pending instances */ + usb_deregister(&irda_driver); +} +module_exit(stir_cleanup); + +/* + * Module parameters + */ +MODULE_PARM(qos_mtt_bits, "i"); +MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time"); +MODULE_PARM(rx_sensitivity, "i"); +MODULE_PARM_DESC(rx_sensitivity, "Set Receiver sensitivity (0-7, 0 is most sensitive)"); +MODULE_PARM(tx_power, "i"); +MODULE_PARM_DESC(tx_power, "Set Transmitter power (0-3, 0 is highest power)"); + +MODULE_AUTHOR("Stephen Hemminger "); +MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200"); +MODULE_LICENSE("GPL"); diff -urN -X dontdiff linux-2.5/drivers/net/irda/stir4200.h irda-2.5/drivers/net/irda/stir4200.h --- linux-2.5/drivers/net/irda/stir4200.h 1969-12-31 16:00:00.000000000 -0800 +++ irda-2.5/drivers/net/irda/stir4200.h 2003-11-14 15:52:16.558823832 -0800 @@ -0,0 +1,154 @@ +/***************************************************************************** +* +* Filename: stir4200.h +* Version: 0.2 +* Description: IrDA-USB Driver +* Status: Experimental +* Author: Paul Stewart +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +* +*****************************************************************************/ + +/* These are definitions out of the SigmaTel STIr4200 datasheet, plus a + * sprinkling of additional constants and data structs inspired by irda-usb.h + */ + +#define STIR_IRDA_HEADER 4 +#define CTRL_TIMEOUT 100 /* milliseconds */ +#define BULK_TIMEOUT 200 /* milliseconds */ +#define STIR_FIFO_SIZE 4096 + +#define FIR_CE 0x7d +#define FIR_XBOF 0x7F +#define FIR_EOF 0x7E /* End of frame */ + +enum StirRequests { + REQ_WRITE_REG = 0x00, + REQ_WRITE_SINGLE = 0x03, + REQ_READ_REG = 0x01, + REQ_READ_ROM = 0x02, +}; + +/* Register offsets */ +enum StirRegs { + REG_RSVD=0, + REG_MODE, + REG_PDCLK, + REG_CTRL1, + REG_CTRL2, + REG_FIFOCTL, + REG_FIFOLSB, + REG_FIFOMSB, + REG_DPLL, + REG_IRDIG, + REG_TEST=15, +}; + +enum StirModeMask { + MODE_FIR = 0x80, + MODE_SIR = 0x20, + MODE_ASK = 0x10, + MODE_FASTRX = 0x08, + MODE_FFRSTEN = 0x04, + MODE_NRESET = 0x02, + MODE_2400 = 0x01, +}; + +enum StirPdclkMask { + PDCLK_4000000 = 0x02, + PDCLK_115200 = 0x09, + PDCLK_57600 = 0x13, + PDCLK_38400 = 0x1D, + PDCLK_19200 = 0x3B, + PDCLK_9600 = 0x77, + PDCLK_2400 = 0xDF /* also set bit 0 of REG_MODE */, +}; + +enum StirCtrl1Mask { + CTRL1_SDMODE = 0x80, + CTRL1_RXSLOW = 0x40, + CTRL1_TXPWD = 0x10, + CTRL1_RXPWD = 0x08, + CTRL1_TXPWR0 = 0x00 /* 0 = highest power */, + CTRL1_TXPWR1 = 0x02, + CTRL1_TXPWR2 = 0x04, + CTRL1_TXPWR3 = 0x06 /* 3 = lowest power */, + CTRL1_SRESET = 0x01, +}; + +enum StirCtrl2Mask { + CTRL2_FIR_1 = 0x00, + CTRL2_FIR_2 = 0x20, + CTRL2_FIR_3 = 0x40, + CTRL2_FIR_4 = 0x60, + CTRL2_FIR_5 = 0x80, + CTRL2_SIR_4 = 0x00, + CTRL2_SIR_8 = 0x20, + CTRL2_SIR_12 = 0x40, + CTRL2_SIR_16 = 0x60, + CTRL2_SIR_20 = 0x80, + CTRL2_SIR_24 = 0xA0, + CTRL2_SIR_28 = 0xC0, + CTRL2_SPWITDH = 0x08, + CTRL2_REVID = 0x03, +}; + +enum StirFifoCtlMask { + FIFOCTL_EOF = 0x80, + FIFOCTL_UNDER = 0x40, + FIFOCTL_OVER = 0x20, + FIFOCTL_DIR = 0x10, + FIFOCTL_CLR = 0x08, + FIFOCTL_EMPTY = 0x04, + FIFOCTL_RXERR = 0x02, + FIFOCTL_TXERR = 0x01, +}; + +enum StirDiagMask { + IRDIG_RXHIGH = 0x80, + IRDIG_RXLOW = 0x40, +}; + +enum StirTestMask { + TEST_PLLDOWN = 0x80, + TEST_LOOPIR = 0x40, + TEST_LOOPUSB = 0x20, + TEST_TSTENA = 0x10, + TEST_TSTOSC = 0x0F, +}; + +struct stir_cb { + struct usb_device *usbdev; /* init: probe_irda */ + struct net_device *netdev; /* network layer */ + struct irlap_cb *irlap; /* The link layer we are binded to */ + struct net_device_stats stats; /* network statistics */ + struct qos_info qos; + int speed; /* Current speed */ + + __u8 bulk_in_ep; /* Rx Endpoint assignments */ + __u8 bulk_out_ep; /* Tx Endpoint assignments */ + __u16 bulk_out_mtu; /* Max Tx packet size in bytes */ + + wait_queue_head_t thr_wait; /* poll thread control */ + struct completion thr_exited; + pid_t thr_pid; + + struct sk_buff_head tx_queue; /* wrapped data transmit queue */ + void *tx_data; /* raw data out */ + + iobuff_t rx_buff; /* receive unwrap state */ + void *rx_data; /* raw data in */ +};