linux/drivers/net/wan/cosa.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
   3
   4/*  Copyright (C) 1995-1997  Jan "Yenya" Kasprzak <kas@fi.muni.cz>
   5 *  Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
   6 */
   7
   8/* The driver for the SRP and COSA synchronous serial cards.
   9 *
  10 * HARDWARE INFO
  11 *
  12 * Both cards are developed at the Institute of Computer Science,
  13 * Masaryk University (https://www.ics.muni.cz/). The hardware is
  14 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
  15 * and the photo of both cards is available at
  16 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
  17 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
  18 * For Linux-specific utilities, see below in the "Software info" section.
  19 * If you want to order the card, contact Jiri Novotny.
  20 *
  21 * The SRP (serial port?, the Czech word "srp" means "sickle") card
  22 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
  23 * with V.24 interfaces up to 80kb/s each.
  24 *
  25 * The COSA (communication serial adapter?, the Czech word "kosa" means
  26 * "scythe") is a next-generation sync/async board with two interfaces
  27 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
  28 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
  29 * The 8-channels version is in development.
  30 *
  31 * Both types have downloadable firmware and communicate via ISA DMA.
  32 * COSA can be also a bus-mastering device.
  33 *
  34 * SOFTWARE INFO
  35 *
  36 * The homepage of the Linux driver is at https://www.fi.muni.cz/~kas/cosa/.
  37 * The CVS tree of Linux driver can be viewed there, as well as the
  38 * firmware binaries and user-space utilities for downloading the firmware
  39 * into the card and setting up the card.
  40 *
  41 * The Linux driver (unlike the present *BSD drivers :-) can work even
  42 * for the COSA and SRP in one computer and allows each channel to work
  43 * in one of the two modes (character or network device).
  44 *
  45 * AUTHOR
  46 *
  47 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
  48 *
  49 * You can mail me bugfixes and even success reports. I am especially
  50 * interested in the SMP and/or muliti-channel success/failure reports
  51 * (I wonder if I did the locking properly :-).
  52 *
  53 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
  54 *
  55 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
  56 * The skeleton.c by Donald Becker
  57 * The SDL Riscom/N2 driver by Mike Natale
  58 * The Comtrol Hostess SV11 driver by Alan Cox
  59 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
  60 */
  61
  62#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  63
  64#include <linux/module.h>
  65#include <linux/kernel.h>
  66#include <linux/sched/signal.h>
  67#include <linux/slab.h>
  68#include <linux/poll.h>
  69#include <linux/fs.h>
  70#include <linux/interrupt.h>
  71#include <linux/delay.h>
  72#include <linux/hdlc.h>
  73#include <linux/errno.h>
  74#include <linux/ioport.h>
  75#include <linux/netdevice.h>
  76#include <linux/spinlock.h>
  77#include <linux/mutex.h>
  78#include <linux/device.h>
  79#include <asm/io.h>
  80#include <asm/dma.h>
  81#include <asm/byteorder.h>
  82
  83#undef COSA_SLOW_IO     /* for testing purposes only */
  84
  85#include "cosa.h"
  86
  87/* Maximum length of the identification string. */
  88#define COSA_MAX_ID_STRING      128
  89
  90/* Maximum length of the channel name */
  91#define COSA_MAX_NAME           (sizeof("cosaXXXcXXX") + 1)
  92
  93/* Per-channel data structure */
  94
  95struct channel_data {
  96        int usage;      /* Usage count; >0 for chrdev, -1 for netdev */
  97        int num;        /* Number of the channel */
  98        struct cosa_data *cosa; /* Pointer to the per-card structure */
  99        int txsize;     /* Size of transmitted data */
 100        char *txbuf;    /* Transmit buffer */
 101        char name[COSA_MAX_NAME];       /* channel name */
 102
 103        /* The HW layer interface */
 104        /* routine called from the RX interrupt */
 105        char *(*setup_rx)(struct channel_data *channel, int size);
 106        /* routine called when the RX is done (from the EOT interrupt) */
 107        int (*rx_done)(struct channel_data *channel);
 108        /* routine called when the TX is done (from the EOT interrupt) */
 109        int (*tx_done)(struct channel_data *channel, int size);
 110
 111        /* Character device parts */
 112        struct mutex rlock;
 113        struct semaphore wsem;
 114        char *rxdata;
 115        int rxsize;
 116        wait_queue_head_t txwaitq, rxwaitq;
 117        int tx_status, rx_status;
 118
 119        /* generic HDLC device parts */
 120        struct net_device *netdev;
 121        struct sk_buff *rx_skb, *tx_skb;
 122};
 123
 124/* cosa->firmware_status bits */
 125#define COSA_FW_RESET           BIT(0)  /* Is the ROM monitor active? */
 126#define COSA_FW_DOWNLOAD        BIT(1)  /* Is the microcode downloaded? */
 127#define COSA_FW_START           BIT(2)  /* Is the microcode running? */
 128
 129struct cosa_data {
 130        int num;                        /* Card number */
 131        char name[COSA_MAX_NAME];       /* Card name - e.g "cosa0" */
 132        unsigned int datareg, statusreg;        /* I/O ports */
 133        unsigned short irq, dma;        /* IRQ and DMA number */
 134        unsigned short startaddr;       /* Firmware start address */
 135        unsigned short busmaster;       /* Use busmastering? */
 136        int nchannels;                  /* # of channels on this card */
 137        int driver_status;              /* For communicating with firmware */
 138        int firmware_status;            /* Downloaded, reseted, etc. */
 139        unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
 140        unsigned long rxtx;             /* RX or TX in progress? */
 141        int enabled;
 142        int usage;                              /* usage count */
 143        int txchan, txsize, rxsize;
 144        struct channel_data *rxchan;
 145        char *bouncebuf;
 146        char *txbuf, *rxbuf;
 147        struct channel_data *chan;
 148        spinlock_t lock;        /* For exclusive operations on this structure */
 149        char id_string[COSA_MAX_ID_STRING];     /* ROM monitor ID string */
 150        char *type;                             /* card type */
 151};
 152
 153/* Define this if you want all the possible ports to be autoprobed.
 154 * It is here but it probably is not a good idea to use this.
 155 */
 156/* #define COSA_ISA_AUTOPROBE   1*/
 157
 158/* Character device major number. 117 was allocated for us.
 159 * The value of 0 means to allocate a first free one.
 160 */
 161static DEFINE_MUTEX(cosa_chardev_mutex);
 162static int cosa_major = 117;
 163
 164/* Encoding of the minor numbers:
 165 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
 166 * the highest bits means the card number.
 167 */
 168#define CARD_MINOR_BITS 4       /* How many bits in minor number are reserved
 169                                 * for the single card
 170                                 */
 171/* The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
 172 * macro doesn't like anything other than the raw number as an argument :-(
 173 */
 174#define MAX_CARDS       16
 175/* #define MAX_CARDS    (1 << (8-CARD_MINOR_BITS)) */
 176
 177#define DRIVER_RX_READY         0x0001
 178#define DRIVER_TX_READY         0x0002
 179#define DRIVER_TXMAP_SHIFT      2
 180#define DRIVER_TXMAP_MASK       0x0c    /* FIXME: 0xfc for 8-channel version */
 181
 182/* for cosa->rxtx - indicates whether either transmit or receive is
 183 * in progress. These values are mean number of the bit.
 184 */
 185#define TXBIT 0
 186#define RXBIT 1
 187#define IRQBIT 2
 188
 189#define COSA_MTU 2000   /* FIXME: I don't know this exactly */
 190
 191#undef DEBUG_DATA //1   /* Dump the data read or written to the channel */
 192#undef DEBUG_IRQS //1   /* Print the message when the IRQ is received */
 193#undef DEBUG_IO   //1   /* Dump the I/O traffic */
 194
 195#define TX_TIMEOUT      (5 * HZ)
 196
 197/* Maybe the following should be allocated dynamically */
 198static struct cosa_data cosa_cards[MAX_CARDS];
 199static int nr_cards;
 200
 201#ifdef COSA_ISA_AUTOPROBE
 202static int io[MAX_CARDS + 1]  = {0x220, 0x228, 0x210, 0x218, 0,};
 203/* NOTE: DMA is not autoprobed!!! */
 204static int dma[MAX_CARDS + 1] = {1, 7, 1, 7, 1, 7, 1, 7, 0,};
 205#else
 206static int io[MAX_CARDS + 1];
 207static int dma[MAX_CARDS + 1];
 208#endif
 209/* IRQ can be safely autoprobed */
 210static int irq[MAX_CARDS + 1] = {-1, -1, -1, -1, -1, -1, 0,};
 211
 212/* for class stuff*/
 213static struct class *cosa_class;
 214
 215#ifdef MODULE
 216module_param_hw_array(io, int, ioport, NULL, 0);
 217MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
 218module_param_hw_array(irq, int, irq, NULL, 0);
 219MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
 220module_param_hw_array(dma, int, dma, NULL, 0);
 221MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
 222
 223MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
 224MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
 225MODULE_LICENSE("GPL");
 226#endif
 227
 228/* I use this mainly for testing purposes */
 229#ifdef COSA_SLOW_IO
 230#define cosa_outb outb_p
 231#define cosa_outw outw_p
 232#define cosa_inb  inb_p
 233#define cosa_inw  inw_p
 234#else
 235#define cosa_outb outb
 236#define cosa_outw outw
 237#define cosa_inb  inb
 238#define cosa_inw  inw
 239#endif
 240
 241#define is_8bit(cosa)           (!((cosa)->datareg & 0x08))
 242
 243#define cosa_getstatus(cosa)    (cosa_inb((cosa)->statusreg))
 244#define cosa_putstatus(cosa, stat)      (cosa_outb(stat, (cosa)->statusreg))
 245#define cosa_getdata16(cosa)    (cosa_inw((cosa)->datareg))
 246#define cosa_getdata8(cosa)     (cosa_inb((cosa)->datareg))
 247#define cosa_putdata16(cosa, dt)        (cosa_outw(dt, (cosa)->datareg))
 248#define cosa_putdata8(cosa, dt) (cosa_outb(dt, (cosa)->datareg))
 249
 250/* Initialization stuff */
 251static int cosa_probe(int ioaddr, int irq, int dma);
 252
 253/* HW interface */
 254static void cosa_enable_rx(struct channel_data *chan);
 255static void cosa_disable_rx(struct channel_data *chan);
 256static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
 257static void cosa_kick(struct cosa_data *cosa);
 258static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
 259
 260/* Network device stuff */
 261static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
 262                           unsigned short parity);
 263static int cosa_net_open(struct net_device *d);
 264static int cosa_net_close(struct net_device *d);
 265static void cosa_net_timeout(struct net_device *d, unsigned int txqueue);
 266static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
 267static char *cosa_net_setup_rx(struct channel_data *channel, int size);
 268static int cosa_net_rx_done(struct channel_data *channel);
 269static int cosa_net_tx_done(struct channel_data *channel, int size);
 270
 271/* Character device */
 272static char *chrdev_setup_rx(struct channel_data *channel, int size);
 273static int chrdev_rx_done(struct channel_data *channel);
 274static int chrdev_tx_done(struct channel_data *channel, int size);
 275static ssize_t cosa_read(struct file *file,
 276                         char __user *buf, size_t count, loff_t *ppos);
 277static ssize_t cosa_write(struct file *file,
 278                          const char __user *buf, size_t count, loff_t *ppos);
 279static unsigned int cosa_poll(struct file *file, poll_table *poll);
 280static int cosa_open(struct inode *inode, struct file *file);
 281static int cosa_release(struct inode *inode, struct file *file);
 282static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
 283                               unsigned long arg);
 284#ifdef COSA_FASYNC_WORKING
 285static int cosa_fasync(struct inode *inode, struct file *file, int on);
 286#endif
 287
 288static const struct file_operations cosa_fops = {
 289        .owner          = THIS_MODULE,
 290        .llseek         = no_llseek,
 291        .read           = cosa_read,
 292        .write          = cosa_write,
 293        .poll           = cosa_poll,
 294        .unlocked_ioctl = cosa_chardev_ioctl,
 295        .open           = cosa_open,
 296        .release        = cosa_release,
 297#ifdef COSA_FASYNC_WORKING
 298        .fasync         = cosa_fasync,
 299#endif
 300};
 301
 302/* Ioctls */
 303static int cosa_start(struct cosa_data *cosa, int address);
 304static int cosa_reset(struct cosa_data *cosa);
 305static int cosa_download(struct cosa_data *cosa, void __user *a);
 306static int cosa_readmem(struct cosa_data *cosa, void __user *a);
 307
 308/* COSA/SRP ROM monitor */
 309static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
 310static int startmicrocode(struct cosa_data *cosa, int address);
 311static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
 312static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
 313
 314/* Auxiliary functions */
 315static int get_wait_data(struct cosa_data *cosa);
 316static int put_wait_data(struct cosa_data *cosa, int data);
 317static int puthexnumber(struct cosa_data *cosa, int number);
 318static void put_driver_status(struct cosa_data *cosa);
 319static void put_driver_status_nolock(struct cosa_data *cosa);
 320
 321/* Interrupt handling */
 322static irqreturn_t cosa_interrupt(int irq, void *cosa);
 323
 324/* I/O ops debugging */
 325#ifdef DEBUG_IO
 326static void debug_data_in(struct cosa_data *cosa, int data);
 327static void debug_data_out(struct cosa_data *cosa, int data);
 328static void debug_data_cmd(struct cosa_data *cosa, int data);
 329static void debug_status_in(struct cosa_data *cosa, int status);
 330static void debug_status_out(struct cosa_data *cosa, int status);
 331#endif
 332
 333static inline struct channel_data *dev_to_chan(struct net_device *dev)
 334{
 335        return (struct channel_data *)dev_to_hdlc(dev)->priv;
 336}
 337
 338/* ---------- Initialization stuff ---------- */
 339
 340static int __init cosa_init(void)
 341{
 342        int i, err = 0;
 343
 344        if (cosa_major > 0) {
 345                if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
 346                        pr_warn("unable to get major %d\n", cosa_major);
 347                        err = -EIO;
 348                        goto out;
 349                }
 350        } else {
 351                cosa_major = register_chrdev(0, "cosa", &cosa_fops);
 352                if (!cosa_major) {
 353                        pr_warn("unable to register chardev\n");
 354                        err = -EIO;
 355                        goto out;
 356                }
 357        }
 358        for (i = 0; i < MAX_CARDS; i++)
 359                cosa_cards[i].num = -1;
 360        for (i = 0; io[i] != 0 && i < MAX_CARDS; i++)
 361                cosa_probe(io[i], irq[i], dma[i]);
 362        if (!nr_cards) {
 363                pr_warn("no devices found\n");
 364                unregister_chrdev(cosa_major, "cosa");
 365                err = -ENODEV;
 366                goto out;
 367        }
 368        cosa_class = class_create(THIS_MODULE, "cosa");
 369        if (IS_ERR(cosa_class)) {
 370                err = PTR_ERR(cosa_class);
 371                goto out_chrdev;
 372        }
 373        for (i = 0; i < nr_cards; i++)
 374                device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
 375                              "cosa%d", i);
 376        err = 0;
 377        goto out;
 378
 379out_chrdev:
 380        unregister_chrdev(cosa_major, "cosa");
 381out:
 382        return err;
 383}
 384module_init(cosa_init);
 385
 386static void __exit cosa_exit(void)
 387{
 388        struct cosa_data *cosa;
 389        int i;
 390
 391        for (i = 0; i < nr_cards; i++)
 392                device_destroy(cosa_class, MKDEV(cosa_major, i));
 393        class_destroy(cosa_class);
 394
 395        for (cosa = cosa_cards; nr_cards--; cosa++) {
 396                /* Clean up the per-channel data */
 397                for (i = 0; i < cosa->nchannels; i++) {
 398                        /* Chardev driver has no alloc'd per-channel data */
 399                        unregister_hdlc_device(cosa->chan[i].netdev);
 400                        free_netdev(cosa->chan[i].netdev);
 401                }
 402                /* Clean up the per-card data */
 403                kfree(cosa->chan);
 404                kfree(cosa->bouncebuf);
 405                free_irq(cosa->irq, cosa);
 406                free_dma(cosa->dma);
 407                release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
 408        }
 409        unregister_chrdev(cosa_major, "cosa");
 410}
 411module_exit(cosa_exit);
 412
 413static const struct net_device_ops cosa_ops = {
 414        .ndo_open       = cosa_net_open,
 415        .ndo_stop       = cosa_net_close,
 416        .ndo_start_xmit = hdlc_start_xmit,
 417        .ndo_siocwandev = hdlc_ioctl,
 418        .ndo_tx_timeout = cosa_net_timeout,
 419};
 420
 421static int cosa_probe(int base, int irq, int dma)
 422{
 423        struct cosa_data *cosa = cosa_cards + nr_cards;
 424        int i, err = 0;
 425
 426        memset(cosa, 0, sizeof(struct cosa_data));
 427
 428        /* Checking validity of parameters: */
 429        /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
 430        if ((irq >= 0  && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
 431                pr_info("invalid IRQ %d\n", irq);
 432                return -1;
 433        }
 434        /* I/O address should be between 0x100 and 0x3ff and should be
 435         * multiple of 8.
 436         */
 437        if (base < 0x100 || base > 0x3ff || base & 0x7) {
 438                pr_info("invalid I/O address 0x%x\n", base);
 439                return -1;
 440        }
 441        /* DMA should be 0,1 or 3-7 */
 442        if (dma < 0 || dma == 4 || dma > 7) {
 443                pr_info("invalid DMA %d\n", dma);
 444                return -1;
 445        }
 446        /* and finally, on 16-bit COSA DMA should be 4-7 and
 447         * I/O base should not be multiple of 0x10
 448         */
 449        if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
 450                pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
 451                        base, dma);
 452                return -1;
 453        }
 454
 455        cosa->dma = dma;
 456        cosa->datareg = base;
 457        cosa->statusreg = is_8bit(cosa) ? base + 1 : base + 2;
 458        spin_lock_init(&cosa->lock);
 459
 460        if (!request_region(base, is_8bit(cosa) ? 2 : 4, "cosa"))
 461                return -1;
 462
 463        if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
 464                printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
 465                err = -1;
 466                goto err_out;
 467        }
 468
 469        /* Test the validity of identification string */
 470        if (!strncmp(cosa->id_string, "SRP", 3)) {
 471                cosa->type = "srp";
 472        } else if (!strncmp(cosa->id_string, "COSA", 4)) {
 473                cosa->type = is_8bit(cosa) ? "cosa8" : "cosa16";
 474        } else {
 475/* Print a warning only if we are not autoprobing */
 476#ifndef COSA_ISA_AUTOPROBE
 477                pr_info("valid signature not found at 0x%x\n", base);
 478#endif
 479                err = -1;
 480                goto err_out;
 481        }
 482        /* Update the name of the region now we know the type of card */
 483        release_region(base, is_8bit(cosa) ? 2 : 4);
 484        if (!request_region(base, is_8bit(cosa) ? 2 : 4, cosa->type)) {
 485                printk(KERN_DEBUG "changing name at 0x%x failed.\n", base);
 486                return -1;
 487        }
 488
 489        /* Now do IRQ autoprobe */
 490        if (irq < 0) {
 491                unsigned long irqs;
 492/*              pr_info("IRQ autoprobe\n"); */
 493                irqs = probe_irq_on();
 494                /* Enable interrupt on tx buffer empty (it sure is)
 495                 * really sure ?
 496                 * FIXME: When this code is not used as module, we should
 497                 * probably call udelay() instead of the interruptible sleep.
 498                 */
 499                set_current_state(TASK_INTERRUPTIBLE);
 500                cosa_putstatus(cosa, SR_TX_INT_ENA);
 501                schedule_timeout(msecs_to_jiffies(300));
 502                irq = probe_irq_off(irqs);
 503                /* Disable all IRQs from the card */
 504                cosa_putstatus(cosa, 0);
 505                /* Empty the received data register */
 506                cosa_getdata8(cosa);
 507
 508                if (irq < 0) {
 509                        pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
 510                                irq, cosa->datareg);
 511                        err = -1;
 512                        goto err_out;
 513                }
 514                if (irq == 0) {
 515                        pr_info("no interrupt obtained (board at 0x%x)\n",
 516                                cosa->datareg);
 517                /*      return -1; */
 518                }
 519        }
 520
 521        cosa->irq = irq;
 522        cosa->num = nr_cards;
 523        cosa->usage = 0;
 524        cosa->nchannels = 2;    /* FIXME: how to determine this? */
 525
 526        if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
 527                err = -1;
 528                goto err_out;
 529        }
 530        if (request_dma(cosa->dma, cosa->type)) {
 531                err = -1;
 532                goto err_out1;
 533        }
 534
 535        cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL | GFP_DMA);
 536        if (!cosa->bouncebuf) {
 537                err = -ENOMEM;
 538                goto err_out2;
 539        }
 540        sprintf(cosa->name, "cosa%d", cosa->num);
 541
 542        /* Initialize the per-channel data */
 543        cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
 544        if (!cosa->chan) {
 545                err = -ENOMEM;
 546                goto err_out3;
 547        }
 548
 549        for (i = 0; i < cosa->nchannels; i++) {
 550                struct channel_data *chan = &cosa->chan[i];
 551
 552                chan->cosa = cosa;
 553                chan->num = i;
 554                sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
 555
 556                /* Initialize the chardev data structures */
 557                mutex_init(&chan->rlock);
 558                sema_init(&chan->wsem, 1);
 559
 560                /* Register the network interface */
 561                chan->netdev = alloc_hdlcdev(chan);
 562                if (!chan->netdev) {
 563                        pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
 564                        err = -ENOMEM;
 565                        goto err_hdlcdev;
 566                }
 567                dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
 568                dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
 569                chan->netdev->netdev_ops = &cosa_ops;
 570                chan->netdev->watchdog_timeo = TX_TIMEOUT;
 571                chan->netdev->base_addr = chan->cosa->datareg;
 572                chan->netdev->irq = chan->cosa->irq;
 573                chan->netdev->dma = chan->cosa->dma;
 574                err = register_hdlc_device(chan->netdev);
 575                if (err) {
 576                        netdev_warn(chan->netdev,
 577                                    "register_hdlc_device() failed\n");
 578                        free_netdev(chan->netdev);
 579                        goto err_hdlcdev;
 580                }
 581        }
 582
 583        pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
 584                cosa->num, cosa->id_string, cosa->type,
 585                cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
 586
 587        return nr_cards++;
 588
 589err_hdlcdev:
 590        while (i-- > 0) {
 591                unregister_hdlc_device(cosa->chan[i].netdev);
 592                free_netdev(cosa->chan[i].netdev);
 593        }
 594        kfree(cosa->chan);
 595err_out3:
 596        kfree(cosa->bouncebuf);
 597err_out2:
 598        free_dma(cosa->dma);
 599err_out1:
 600        free_irq(cosa->irq, cosa);
 601err_out:
 602        release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
 603        pr_notice("cosa%d: allocating resources failed\n", cosa->num);
 604        return err;
 605}
 606
 607/*---------- network device ---------- */
 608
 609static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
 610                           unsigned short parity)
 611{
 612        if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
 613                return 0;
 614        return -EINVAL;
 615}
 616
 617static int cosa_net_open(struct net_device *dev)
 618{
 619        struct channel_data *chan = dev_to_chan(dev);
 620        int err;
 621        unsigned long flags;
 622
 623        if (!(chan->cosa->firmware_status & COSA_FW_START)) {
 624                pr_notice("%s: start the firmware first (status %d)\n",
 625                          chan->cosa->name, chan->cosa->firmware_status);
 626                return -EPERM;
 627        }
 628        spin_lock_irqsave(&chan->cosa->lock, flags);
 629        if (chan->usage != 0) {
 630                pr_warn("%s: cosa_net_open called with usage count %d\n",
 631                        chan->name, chan->usage);
 632                spin_unlock_irqrestore(&chan->cosa->lock, flags);
 633                return -EBUSY;
 634        }
 635        chan->setup_rx = cosa_net_setup_rx;
 636        chan->tx_done = cosa_net_tx_done;
 637        chan->rx_done = cosa_net_rx_done;
 638        chan->usage = -1;
 639        chan->cosa->usage++;
 640        spin_unlock_irqrestore(&chan->cosa->lock, flags);
 641
 642        err = hdlc_open(dev);
 643        if (err) {
 644                spin_lock_irqsave(&chan->cosa->lock, flags);
 645                chan->usage = 0;
 646                chan->cosa->usage--;
 647                spin_unlock_irqrestore(&chan->cosa->lock, flags);
 648                return err;
 649        }
 650
 651        netif_start_queue(dev);
 652        cosa_enable_rx(chan);
 653        return 0;
 654}
 655
 656static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
 657                               struct net_device *dev)
 658{
 659        struct channel_data *chan = dev_to_chan(dev);
 660
 661        netif_stop_queue(dev);
 662
 663        chan->tx_skb = skb;
 664        cosa_start_tx(chan, skb->data, skb->len);
 665        return NETDEV_TX_OK;
 666}
 667
 668static void cosa_net_timeout(struct net_device *dev, unsigned int txqueue)
 669{
 670        struct channel_data *chan = dev_to_chan(dev);
 671
 672        if (test_bit(RXBIT, &chan->cosa->rxtx)) {
 673                chan->netdev->stats.rx_errors++;
 674                chan->netdev->stats.rx_missed_errors++;
 675        } else {
 676                chan->netdev->stats.tx_errors++;
 677                chan->netdev->stats.tx_aborted_errors++;
 678        }
 679        cosa_kick(chan->cosa);
 680        if (chan->tx_skb) {
 681                dev_kfree_skb(chan->tx_skb);
 682                chan->tx_skb = NULL;
 683        }
 684        netif_wake_queue(dev);
 685}
 686
 687static int cosa_net_close(struct net_device *dev)
 688{
 689        struct channel_data *chan = dev_to_chan(dev);
 690        unsigned long flags;
 691
 692        netif_stop_queue(dev);
 693        hdlc_close(dev);
 694        cosa_disable_rx(chan);
 695        spin_lock_irqsave(&chan->cosa->lock, flags);
 696        if (chan->rx_skb) {
 697                kfree_skb(chan->rx_skb);
 698                chan->rx_skb = NULL;
 699        }
 700        if (chan->tx_skb) {
 701                kfree_skb(chan->tx_skb);
 702                chan->tx_skb = NULL;
 703        }
 704        chan->usage = 0;
 705        chan->cosa->usage--;
 706        spin_unlock_irqrestore(&chan->cosa->lock, flags);
 707        return 0;
 708}
 709
 710static char *cosa_net_setup_rx(struct channel_data *chan, int size)
 711{
 712        /* We can safely fall back to non-dma-able memory, because we have
 713         * the cosa->bouncebuf pre-allocated.
 714         */
 715        kfree_skb(chan->rx_skb);
 716        chan->rx_skb = dev_alloc_skb(size);
 717        if (!chan->rx_skb) {
 718                pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
 719                chan->netdev->stats.rx_dropped++;
 720                return NULL;
 721        }
 722        netif_trans_update(chan->netdev);
 723        return skb_put(chan->rx_skb, size);
 724}
 725
 726static int cosa_net_rx_done(struct channel_data *chan)
 727{
 728        if (!chan->rx_skb) {
 729                pr_warn("%s: rx_done with empty skb!\n", chan->name);
 730                chan->netdev->stats.rx_errors++;
 731                chan->netdev->stats.rx_frame_errors++;
 732                return 0;
 733        }
 734        chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
 735        chan->rx_skb->dev = chan->netdev;
 736        skb_reset_mac_header(chan->rx_skb);
 737        chan->netdev->stats.rx_packets++;
 738        chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
 739        netif_rx(chan->rx_skb);
 740        chan->rx_skb = NULL;
 741        return 0;
 742}
 743
 744/* ARGSUSED */
 745static int cosa_net_tx_done(struct channel_data *chan, int size)
 746{
 747        if (!chan->tx_skb) {
 748                pr_warn("%s: tx_done with empty skb!\n", chan->name);
 749                chan->netdev->stats.tx_errors++;
 750                chan->netdev->stats.tx_aborted_errors++;
 751                return 1;
 752        }
 753        dev_consume_skb_irq(chan->tx_skb);
 754        chan->tx_skb = NULL;
 755        chan->netdev->stats.tx_packets++;
 756        chan->netdev->stats.tx_bytes += size;
 757        netif_wake_queue(chan->netdev);
 758        return 1;
 759}
 760
 761/*---------- Character device ---------- */
 762
 763static ssize_t cosa_read(struct file *file,
 764                         char __user *buf, size_t count, loff_t *ppos)
 765{
 766        DECLARE_WAITQUEUE(wait, current);
 767        unsigned long flags;
 768        struct channel_data *chan = file->private_data;
 769        struct cosa_data *cosa = chan->cosa;
 770        char *kbuf;
 771
 772        if (!(cosa->firmware_status & COSA_FW_START)) {
 773                pr_notice("%s: start the firmware first (status %d)\n",
 774                          cosa->name, cosa->firmware_status);
 775                return -EPERM;
 776        }
 777        if (mutex_lock_interruptible(&chan->rlock))
 778                return -ERESTARTSYS;
 779
 780        chan->rxdata = kmalloc(COSA_MTU, GFP_DMA | GFP_KERNEL);
 781        if (!chan->rxdata) {
 782                mutex_unlock(&chan->rlock);
 783                return -ENOMEM;
 784        }
 785
 786        chan->rx_status = 0;
 787        cosa_enable_rx(chan);
 788        spin_lock_irqsave(&cosa->lock, flags);
 789        add_wait_queue(&chan->rxwaitq, &wait);
 790        while (!chan->rx_status) {
 791                set_current_state(TASK_INTERRUPTIBLE);
 792                spin_unlock_irqrestore(&cosa->lock, flags);
 793                schedule();
 794                spin_lock_irqsave(&cosa->lock, flags);
 795                if (signal_pending(current) && chan->rx_status == 0) {
 796                        chan->rx_status = 1;
 797                        remove_wait_queue(&chan->rxwaitq, &wait);
 798                        __set_current_state(TASK_RUNNING);
 799                        spin_unlock_irqrestore(&cosa->lock, flags);
 800                        mutex_unlock(&chan->rlock);
 801                        return -ERESTARTSYS;
 802                }
 803        }
 804        remove_wait_queue(&chan->rxwaitq, &wait);
 805        __set_current_state(TASK_RUNNING);
 806        kbuf = chan->rxdata;
 807        count = chan->rxsize;
 808        spin_unlock_irqrestore(&cosa->lock, flags);
 809        mutex_unlock(&chan->rlock);
 810
 811        if (copy_to_user(buf, kbuf, count)) {
 812                kfree(kbuf);
 813                return -EFAULT;
 814        }
 815        kfree(kbuf);
 816        return count;
 817}
 818
 819static char *chrdev_setup_rx(struct channel_data *chan, int size)
 820{
 821        /* Expect size <= COSA_MTU */
 822        chan->rxsize = size;
 823        return chan->rxdata;
 824}
 825
 826static int chrdev_rx_done(struct channel_data *chan)
 827{
 828        if (chan->rx_status) { /* Reader has died */
 829                kfree(chan->rxdata);
 830                up(&chan->wsem);
 831        }
 832        chan->rx_status = 1;
 833        wake_up_interruptible(&chan->rxwaitq);
 834        return 1;
 835}
 836
 837static ssize_t cosa_write(struct file *file,
 838                          const char __user *buf, size_t count, loff_t *ppos)
 839{
 840        DECLARE_WAITQUEUE(wait, current);
 841        struct channel_data *chan = file->private_data;
 842        struct cosa_data *cosa = chan->cosa;
 843        unsigned long flags;
 844        char *kbuf;
 845
 846        if (!(cosa->firmware_status & COSA_FW_START)) {
 847                pr_notice("%s: start the firmware first (status %d)\n",
 848                          cosa->name, cosa->firmware_status);
 849                return -EPERM;
 850        }
 851        if (down_interruptible(&chan->wsem))
 852                return -ERESTARTSYS;
 853
 854        if (count > COSA_MTU)
 855                count = COSA_MTU;
 856
 857        /* Allocate the buffer */
 858        kbuf = kmalloc(count, GFP_KERNEL | GFP_DMA);
 859        if (!kbuf) {
 860                up(&chan->wsem);
 861                return -ENOMEM;
 862        }
 863        if (copy_from_user(kbuf, buf, count)) {
 864                up(&chan->wsem);
 865                kfree(kbuf);
 866                return -EFAULT;
 867        }
 868        chan->tx_status = 0;
 869        cosa_start_tx(chan, kbuf, count);
 870
 871        spin_lock_irqsave(&cosa->lock, flags);
 872        add_wait_queue(&chan->txwaitq, &wait);
 873        while (!chan->tx_status) {
 874                set_current_state(TASK_INTERRUPTIBLE);
 875                spin_unlock_irqrestore(&cosa->lock, flags);
 876                schedule();
 877                spin_lock_irqsave(&cosa->lock, flags);
 878                if (signal_pending(current) && chan->tx_status == 0) {
 879                        chan->tx_status = 1;
 880                        remove_wait_queue(&chan->txwaitq, &wait);
 881                        __set_current_state(TASK_RUNNING);
 882                        chan->tx_status = 1;
 883                        spin_unlock_irqrestore(&cosa->lock, flags);
 884                        up(&chan->wsem);
 885                        kfree(kbuf);
 886                        return -ERESTARTSYS;
 887                }
 888        }
 889        remove_wait_queue(&chan->txwaitq, &wait);
 890        __set_current_state(TASK_RUNNING);
 891        up(&chan->wsem);
 892        spin_unlock_irqrestore(&cosa->lock, flags);
 893        kfree(kbuf);
 894        return count;
 895}
 896
 897static int chrdev_tx_done(struct channel_data *chan, int size)
 898{
 899        if (chan->tx_status) { /* Writer was interrupted */
 900                kfree(chan->txbuf);
 901                up(&chan->wsem);
 902        }
 903        chan->tx_status = 1;
 904        wake_up_interruptible(&chan->txwaitq);
 905        return 1;
 906}
 907
 908static __poll_t cosa_poll(struct file *file, poll_table *poll)
 909{
 910        pr_info("cosa_poll is here\n");
 911        return 0;
 912}
 913
 914static int cosa_open(struct inode *inode, struct file *file)
 915{
 916        struct cosa_data *cosa;
 917        struct channel_data *chan;
 918        unsigned long flags;
 919        int n;
 920        int ret = 0;
 921
 922        mutex_lock(&cosa_chardev_mutex);
 923        n = iminor(file_inode(file)) >> CARD_MINOR_BITS;
 924        if (n >= nr_cards) {
 925                ret = -ENODEV;
 926                goto out;
 927        }
 928        cosa = cosa_cards + n;
 929
 930        n = iminor(file_inode(file)) & ((1 << CARD_MINOR_BITS) - 1);
 931        if (n >= cosa->nchannels) {
 932                ret = -ENODEV;
 933                goto out;
 934        }
 935        chan = cosa->chan + n;
 936
 937        file->private_data = chan;
 938
 939        spin_lock_irqsave(&cosa->lock, flags);
 940
 941        if (chan->usage < 0) { /* in netdev mode */
 942                spin_unlock_irqrestore(&cosa->lock, flags);
 943                ret = -EBUSY;
 944                goto out;
 945        }
 946        cosa->usage++;
 947        chan->usage++;
 948
 949        chan->tx_done = chrdev_tx_done;
 950        chan->setup_rx = chrdev_setup_rx;
 951        chan->rx_done = chrdev_rx_done;
 952        spin_unlock_irqrestore(&cosa->lock, flags);
 953out:
 954        mutex_unlock(&cosa_chardev_mutex);
 955        return ret;
 956}
 957
 958static int cosa_release(struct inode *inode, struct file *file)
 959{
 960        struct channel_data *channel = file->private_data;
 961        struct cosa_data *cosa;
 962        unsigned long flags;
 963
 964        cosa = channel->cosa;
 965        spin_lock_irqsave(&cosa->lock, flags);
 966        cosa->usage--;
 967        channel->usage--;
 968        spin_unlock_irqrestore(&cosa->lock, flags);
 969        return 0;
 970}
 971
 972#ifdef COSA_FASYNC_WORKING
 973static struct fasync_struct *fasync[256] = { NULL, };
 974
 975/* To be done ... */
 976static int cosa_fasync(struct inode *inode, struct file *file, int on)
 977{
 978        int port = iminor(inode);
 979
 980        return fasync_helper(inode, file, on, &fasync[port]);
 981}
 982#endif
 983
 984/* ---------- Ioctls ---------- */
 985
 986/* Ioctl subroutines can safely be made inline, because they are called
 987 * only from cosa_ioctl().
 988 */
 989static inline int cosa_reset(struct cosa_data *cosa)
 990{
 991        char idstring[COSA_MAX_ID_STRING];
 992
 993        if (cosa->usage > 1)
 994                pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
 995                        cosa->num, cosa->usage);
 996        cosa->firmware_status &= ~(COSA_FW_RESET | COSA_FW_START);
 997        if (cosa_reset_and_read_id(cosa, idstring) < 0) {
 998                pr_notice("cosa%d: reset failed\n", cosa->num);
 999                return -EIO;
1000        }
1001        pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1002        cosa->firmware_status |= COSA_FW_RESET;
1003        return 0;
1004}
1005
1006/* High-level function to download data into COSA memory. Calls download() */
1007static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1008{
1009        struct cosa_download d;
1010        int i;
1011
1012        if (cosa->usage > 1)
1013                pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1014                        cosa->name, cosa->usage);
1015        if (!(cosa->firmware_status & COSA_FW_RESET)) {
1016                pr_notice("%s: reset the card first (status %d)\n",
1017                          cosa->name, cosa->firmware_status);
1018                return -EPERM;
1019        }
1020
1021        if (copy_from_user(&d, arg, sizeof(d)))
1022                return -EFAULT;
1023
1024        if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1025                return -EINVAL;
1026        if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1027                return -EINVAL;
1028
1029        /* If something fails, force the user to reset the card */
1030        cosa->firmware_status &= ~(COSA_FW_RESET | COSA_FW_DOWNLOAD);
1031
1032        i = download(cosa, d.code, d.len, d.addr);
1033        if (i < 0) {
1034                pr_notice("cosa%d: microcode download failed: %d\n",
1035                          cosa->num, i);
1036                return -EIO;
1037        }
1038        pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1039                cosa->num, d.len, d.addr);
1040        cosa->firmware_status |= COSA_FW_RESET | COSA_FW_DOWNLOAD;
1041        return 0;
1042}
1043
1044/* High-level function to read COSA memory. Calls readmem() */
1045static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1046{
1047        struct cosa_download d;
1048        int i;
1049
1050        if (cosa->usage > 1)
1051                pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1052                        cosa->num, cosa->usage);
1053        if (!(cosa->firmware_status & COSA_FW_RESET)) {
1054                pr_notice("%s: reset the card first (status %d)\n",
1055                          cosa->name, cosa->firmware_status);
1056                return -EPERM;
1057        }
1058
1059        if (copy_from_user(&d, arg, sizeof(d)))
1060                return -EFAULT;
1061
1062        /* If something fails, force the user to reset the card */
1063        cosa->firmware_status &= ~COSA_FW_RESET;
1064
1065        i = readmem(cosa, d.code, d.len, d.addr);
1066        if (i < 0) {
1067                pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1068                return -EIO;
1069        }
1070        pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1071                cosa->num, d.len, d.addr);
1072        cosa->firmware_status |= COSA_FW_RESET;
1073        return 0;
1074}
1075
1076/* High-level function to start microcode. Calls startmicrocode(). */
1077static inline int cosa_start(struct cosa_data *cosa, int address)
1078{
1079        int i;
1080
1081        if (cosa->usage > 1)
1082                pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1083                        cosa->num, cosa->usage);
1084
1085        if ((cosa->firmware_status & (COSA_FW_RESET | COSA_FW_DOWNLOAD))
1086                != (COSA_FW_RESET | COSA_FW_DOWNLOAD)) {
1087                pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1088                          cosa->name, cosa->firmware_status);
1089                return -EPERM;
1090        }
1091        cosa->firmware_status &= ~COSA_FW_RESET;
1092        i = startmicrocode(cosa, address);
1093        if (i < 0) {
1094                pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1095                          cosa->num, address, i);
1096                return -EIO;
1097        }
1098        pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1099        cosa->startaddr = address;
1100        cosa->firmware_status |= COSA_FW_START;
1101        return 0;
1102}
1103
1104/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1105static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1106{
1107        int l = strlen(cosa->id_string) + 1;
1108
1109        if (copy_to_user(string, cosa->id_string, l))
1110                return -EFAULT;
1111        return l;
1112}
1113
1114/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1115static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1116{
1117        int l = strlen(cosa->type) + 1;
1118
1119        if (copy_to_user(string, cosa->type, l))
1120                return -EFAULT;
1121        return l;
1122}
1123
1124static int cosa_ioctl_common(struct cosa_data *cosa,
1125                             struct channel_data *channel, unsigned int cmd,
1126                             unsigned long arg)
1127{
1128        void __user *argp = (void __user *)arg;
1129
1130        switch (cmd) {
1131        case COSAIORSET:        /* Reset the device */
1132                if (!capable(CAP_NET_ADMIN))
1133                        return -EACCES;
1134                return cosa_reset(cosa);
1135        case COSAIOSTRT:        /* Start the firmware */
1136                if (!capable(CAP_SYS_RAWIO))
1137                        return -EACCES;
1138                return cosa_start(cosa, arg);
1139        case COSAIODOWNLD:      /* Download the firmware */
1140                if (!capable(CAP_SYS_RAWIO))
1141                        return -EACCES;
1142
1143                return cosa_download(cosa, argp);
1144        case COSAIORMEM:
1145                if (!capable(CAP_SYS_RAWIO))
1146                        return -EACCES;
1147                return cosa_readmem(cosa, argp);
1148        case COSAIORTYPE:
1149                return cosa_gettype(cosa, argp);
1150        case COSAIORIDSTR:
1151                return cosa_getidstr(cosa, argp);
1152        case COSAIONRCARDS:
1153                return nr_cards;
1154        case COSAIONRCHANS:
1155                return cosa->nchannels;
1156        case COSAIOBMSET:
1157                if (!capable(CAP_SYS_RAWIO))
1158                        return -EACCES;
1159                if (is_8bit(cosa))
1160                        return -EINVAL;
1161                if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1162                        return -EINVAL;
1163                cosa->busmaster = arg;
1164                return 0;
1165        case COSAIOBMGET:
1166                return cosa->busmaster;
1167        }
1168        return -ENOIOCTLCMD;
1169}
1170
1171static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1172                               unsigned long arg)
1173{
1174        struct channel_data *channel = file->private_data;
1175        struct cosa_data *cosa;
1176        long ret;
1177
1178        mutex_lock(&cosa_chardev_mutex);
1179        cosa = channel->cosa;
1180        ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1181        mutex_unlock(&cosa_chardev_mutex);
1182        return ret;
1183}
1184
1185/*---------- HW layer interface ---------- */
1186
1187/* The higher layer can bind itself to the HW layer by setting the callbacks
1188 * in the channel_data structure and by using these routines.
1189 */
1190static void cosa_enable_rx(struct channel_data *chan)
1191{
1192        struct cosa_data *cosa = chan->cosa;
1193
1194        if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1195                put_driver_status(cosa);
1196}
1197
1198static void cosa_disable_rx(struct channel_data *chan)
1199{
1200        struct cosa_data *cosa = chan->cosa;
1201
1202        if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1203                put_driver_status(cosa);
1204}
1205
1206/* FIXME: This routine probably should check for cosa_start_tx() called when
1207 * the previous transmit is still unfinished. In this case the non-zero
1208 * return value should indicate to the caller that the queuing(sp?) up
1209 * the transmit has failed.
1210 */
1211static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1212{
1213        struct cosa_data *cosa = chan->cosa;
1214        unsigned long flags;
1215#ifdef DEBUG_DATA
1216        int i;
1217
1218        pr_info("cosa%dc%d: starting tx(0x%x)",
1219                chan->cosa->num, chan->num, len);
1220        for (i = 0; i < len; i++)
1221                pr_cont(" %02x", buf[i]&0xff);
1222        pr_cont("\n");
1223#endif
1224        spin_lock_irqsave(&cosa->lock, flags);
1225        chan->txbuf = buf;
1226        chan->txsize = len;
1227        if (len > COSA_MTU)
1228                chan->txsize = COSA_MTU;
1229        spin_unlock_irqrestore(&cosa->lock, flags);
1230
1231        /* Tell the firmware we are ready */
1232        set_bit(chan->num, &cosa->txbitmap);
1233        put_driver_status(cosa);
1234
1235        return 0;
1236}
1237
1238static void put_driver_status(struct cosa_data *cosa)
1239{
1240        unsigned long flags;
1241        int status;
1242
1243        spin_lock_irqsave(&cosa->lock, flags);
1244
1245        status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1246                | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1247                | (cosa->txbitmap ? ~(cosa->txbitmap << DRIVER_TXMAP_SHIFT)
1248                        & DRIVER_TXMAP_MASK : 0);
1249        if (!cosa->rxtx) {
1250                if (cosa->rxbitmap | cosa->txbitmap) {
1251                        if (!cosa->enabled) {
1252                                cosa_putstatus(cosa, SR_RX_INT_ENA);
1253#ifdef DEBUG_IO
1254                                debug_status_out(cosa, SR_RX_INT_ENA);
1255#endif
1256                                cosa->enabled = 1;
1257                        }
1258                } else if (cosa->enabled) {
1259                        cosa->enabled = 0;
1260                        cosa_putstatus(cosa, 0);
1261#ifdef DEBUG_IO
1262                        debug_status_out(cosa, 0);
1263#endif
1264                }
1265                cosa_putdata8(cosa, status);
1266#ifdef DEBUG_IO
1267                debug_data_cmd(cosa, status);
1268#endif
1269        }
1270        spin_unlock_irqrestore(&cosa->lock, flags);
1271}
1272
1273static void put_driver_status_nolock(struct cosa_data *cosa)
1274{
1275        int status;
1276
1277        status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1278                | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1279                | (cosa->txbitmap ? ~(cosa->txbitmap << DRIVER_TXMAP_SHIFT)
1280                        & DRIVER_TXMAP_MASK : 0);
1281
1282        if (cosa->rxbitmap | cosa->txbitmap) {
1283                cosa_putstatus(cosa, SR_RX_INT_ENA);
1284#ifdef DEBUG_IO
1285                debug_status_out(cosa, SR_RX_INT_ENA);
1286#endif
1287                cosa->enabled = 1;
1288        } else {
1289                cosa_putstatus(cosa, 0);
1290#ifdef DEBUG_IO
1291                debug_status_out(cosa, 0);
1292#endif
1293                cosa->enabled = 0;
1294        }
1295        cosa_putdata8(cosa, status);
1296#ifdef DEBUG_IO
1297        debug_data_cmd(cosa, status);
1298#endif
1299}
1300
1301/* The "kickme" function: When the DMA times out, this is called to
1302 * clean up the driver status.
1303 * FIXME: Preliminary support, the interface is probably wrong.
1304 */
1305static void cosa_kick(struct cosa_data *cosa)
1306{
1307        unsigned long flags, flags1;
1308        char *s = "(probably) IRQ";
1309
1310        if (test_bit(RXBIT, &cosa->rxtx))
1311                s = "RX DMA";
1312        if (test_bit(TXBIT, &cosa->rxtx))
1313                s = "TX DMA";
1314
1315        pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1316        spin_lock_irqsave(&cosa->lock, flags);
1317        cosa->rxtx = 0;
1318
1319        flags1 = claim_dma_lock();
1320        disable_dma(cosa->dma);
1321        clear_dma_ff(cosa->dma);
1322        release_dma_lock(flags1);
1323
1324        /* FIXME: Anything else? */
1325        udelay(100);
1326        cosa_putstatus(cosa, 0);
1327        udelay(100);
1328        (void)cosa_getdata8(cosa);
1329        udelay(100);
1330        cosa_putdata8(cosa, 0);
1331        udelay(100);
1332        put_driver_status_nolock(cosa);
1333        spin_unlock_irqrestore(&cosa->lock, flags);
1334}
1335
1336/* Check if the whole buffer is DMA-able. It means it is below the 16M of
1337 * physical memory and doesn't span the 64k boundary. For now it seems
1338 * SKB's never do this, but we'll check this anyway.
1339 */
1340static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1341{
1342        static int count;
1343        unsigned long b = (unsigned long)buf;
1344
1345        if (b + len >= MAX_DMA_ADDRESS)
1346                return 0;
1347        if ((b ^ (b + len)) & 0x10000) {
1348                if (count++ < 5)
1349                        pr_info("%s: packet spanning a 64k boundary\n",
1350                                chan->name);
1351                return 0;
1352        }
1353        return 1;
1354}
1355
1356/* ---------- The SRP/COSA ROM monitor functions ---------- */
1357
1358/* Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1359 * drivers need to say 4-digit hex number meaning start address of the microcode
1360 * separated by a single space. Monitor replies by saying " =". Now driver
1361 * has to write 4-digit hex number meaning the last byte address ended
1362 * by a single space. Monitor has to reply with a space. Now the download
1363 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1364 */
1365static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1366{
1367        int i;
1368
1369        if (put_wait_data(cosa, 'w') == -1)
1370                return -1;
1371        if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1372        if (get_wait_data(cosa) != '=')
1373                return -3;
1374
1375        if (puthexnumber(cosa, address) < 0)
1376                return -4;
1377        if (put_wait_data(cosa, ' ') == -1)
1378                return -10;
1379        if (get_wait_data(cosa) != ' ')
1380                return -11;
1381        if (get_wait_data(cosa) != '=')
1382                return -12;
1383
1384        if (puthexnumber(cosa, address + length - 1) < 0)
1385                return -13;
1386        if (put_wait_data(cosa, ' ') == -1)
1387                return -18;
1388        if (get_wait_data(cosa) != ' ')
1389                return -19;
1390
1391        while (length--) {
1392                char c;
1393#ifndef SRP_DOWNLOAD_AT_BOOT
1394                if (get_user(c, microcode))
1395                        return -23; /* ??? */
1396#else
1397                c = *microcode;
1398#endif
1399                if (put_wait_data(cosa, c) == -1)
1400                        return -20;
1401                microcode++;
1402        }
1403
1404        if (get_wait_data(cosa) != '\r')
1405                return -21;
1406        if (get_wait_data(cosa) != '\n')
1407                return -22;
1408        if (get_wait_data(cosa) != '.')
1409                return -23;
1410#if 0
1411        printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1412#endif
1413        return 0;
1414}
1415
1416/* Starting microcode is done via the "g" command of the SRP monitor.
1417 * The chat should be the following: "g" "g=" "<addr><CR>"
1418 * "<CR><CR><LF><CR><LF>".
1419 */
1420static int startmicrocode(struct cosa_data *cosa, int address)
1421{
1422        if (put_wait_data(cosa, 'g') == -1)
1423                return -1;
1424        if (get_wait_data(cosa) != 'g')
1425                return -2;
1426        if (get_wait_data(cosa) != '=')
1427                return -3;
1428
1429        if (puthexnumber(cosa, address) < 0)
1430                return -4;
1431        if (put_wait_data(cosa, '\r') == -1)
1432                return -5;
1433
1434        if (get_wait_data(cosa) != '\r')
1435                return -6;
1436        if (get_wait_data(cosa) != '\r')
1437                return -7;
1438        if (get_wait_data(cosa) != '\n')
1439                return -8;
1440        if (get_wait_data(cosa) != '\r')
1441                return -9;
1442        if (get_wait_data(cosa) != '\n')
1443                return -10;
1444#if 0
1445        printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1446#endif
1447        return 0;
1448}
1449
1450/* Reading memory is done via the "r" command of the SRP monitor.
1451 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1452 * Then driver can read the data and the conversation is finished
1453 * by SRP monitor sending "<CR><LF>." (dot at the end).
1454 *
1455 * This routine is not needed during the normal operation and serves
1456 * for debugging purposes only.
1457 */
1458static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1459{
1460        if (put_wait_data(cosa, 'r') == -1)
1461                return -1;
1462        if ((get_wait_data(cosa)) != 'r')
1463                return -2;
1464        if ((get_wait_data(cosa)) != '=')
1465                return -3;
1466
1467        if (puthexnumber(cosa, address) < 0)
1468                return -4;
1469        if (put_wait_data(cosa, ' ') == -1)
1470                return -5;
1471        if (get_wait_data(cosa) != ' ')
1472                return -6;
1473        if (get_wait_data(cosa) != '=')
1474                return -7;
1475
1476        if (puthexnumber(cosa, address + length - 1) < 0)
1477                return -8;
1478        if (put_wait_data(cosa, ' ') == -1)
1479                return -9;
1480        if (get_wait_data(cosa) != ' ')
1481                return -10;
1482
1483        while (length--) {
1484                char c;
1485                int i;
1486
1487                i = get_wait_data(cosa);
1488                if (i == -1) {
1489                        pr_info("0x%04x bytes remaining\n", length);
1490                        return -11;
1491                }
1492                c = i;
1493#if 1
1494                if (put_user(c, microcode))
1495                        return -23; /* ??? */
1496#else
1497                *microcode = c;
1498#endif
1499                microcode++;
1500        }
1501
1502        if (get_wait_data(cosa) != '\r')
1503                return -21;
1504        if (get_wait_data(cosa) != '\n')
1505                return -22;
1506        if (get_wait_data(cosa) != '.')
1507                return -23;
1508#if 0
1509        printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1510#endif
1511        return 0;
1512}
1513
1514/* This function resets the device and reads the initial prompt
1515 * of the device's ROM monitor.
1516 */
1517static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1518{
1519        int i = 0, id = 0, prev = 0, curr = 0;
1520
1521        /* Reset the card ... */
1522        cosa_putstatus(cosa, 0);
1523        cosa_getdata8(cosa);
1524        cosa_putstatus(cosa, SR_RST);
1525        msleep(500);
1526        /* Disable all IRQs from the card */
1527        cosa_putstatus(cosa, 0);
1528
1529        /* Try to read the ID string. The card then prints out the
1530         * identification string ended by the "\n\x2e".
1531         *
1532         * The following loop is indexed through i (instead of id)
1533         * to avoid looping forever when for any reason
1534         * the port returns '\r', '\n' or '\x2e' permanently.
1535         */
1536        for (i = 0; i < COSA_MAX_ID_STRING - 1; i++, prev = curr) {
1537                curr = get_wait_data(cosa);
1538                if (curr == -1)
1539                        return -1;
1540
1541                curr &= 0xff;
1542                if (curr != '\r' && curr != '\n' && curr != 0x2e)
1543                        idstring[id++] = curr;
1544                if (curr == 0x2e && prev == '\n')
1545                        break;
1546        }
1547        /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1548        idstring[id] = '\0';
1549        return id;
1550}
1551
1552/* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1553
1554/* This routine gets the data byte from the card waiting for the SR_RX_RDY
1555 * bit to be set in a loop. It should be used in the exceptional cases
1556 * only (for example when resetting the card or downloading the firmware.
1557 */
1558static int get_wait_data(struct cosa_data *cosa)
1559{
1560        int retries = 1000;
1561
1562        while (--retries) {
1563                /* read data and return them */
1564                if (cosa_getstatus(cosa) & SR_RX_RDY) {
1565                        short r;
1566
1567                        r = cosa_getdata8(cosa);
1568#if 0
1569                        pr_info("get_wait_data returning after %d retries\n",
1570                                999 - retries);
1571#endif
1572                        return r;
1573                }
1574                /* sleep if not ready to read */
1575                schedule_timeout_interruptible(1);
1576        }
1577        pr_info("timeout in get_wait_data (status 0x%x)\n",
1578                cosa_getstatus(cosa));
1579        return -1;
1580}
1581
1582/* This routine puts the data byte to the card waiting for the SR_TX_RDY
1583 * bit to be set in a loop. It should be used in the exceptional cases
1584 * only (for example when resetting the card or downloading the firmware).
1585 */
1586static int put_wait_data(struct cosa_data *cosa, int data)
1587{
1588        int retries = 1000;
1589
1590        while (--retries) {
1591                /* read data and return them */
1592                if (cosa_getstatus(cosa) & SR_TX_RDY) {
1593                        cosa_putdata8(cosa, data);
1594#if 0
1595                        pr_info("Putdata: %d retries\n", 999 - retries);
1596#endif
1597                        return 0;
1598                }
1599#if 0
1600                /* sleep if not ready to read */
1601                schedule_timeout_interruptible(1);
1602#endif
1603        }
1604        pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1605                cosa->num, cosa_getstatus(cosa));
1606        return -1;
1607}
1608
1609/* The following routine puts the hexadecimal number into the SRP monitor
1610 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1611 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1612 * (-2,-4,-6,-8) means that reading echo failed.
1613 */
1614static int puthexnumber(struct cosa_data *cosa, int number)
1615{
1616        char temp[5];
1617        int i;
1618
1619        /* Well, I should probably replace this by something faster. */
1620        sprintf(temp, "%04X", number);
1621        for (i = 0; i < 4; i++) {
1622                if (put_wait_data(cosa, temp[i]) == -1) {
1623                        pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1624                                  cosa->num, i);
1625                        return -1 - 2 * i;
1626                }
1627                if (get_wait_data(cosa) != temp[i]) {
1628                        pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1629                                  cosa->num, i);
1630                        return -2 - 2 * i;
1631                }
1632        }
1633        return 0;
1634}
1635
1636/* ---------- Interrupt routines ---------- */
1637
1638/* There are three types of interrupt:
1639 * At the beginning of transmit - this handled is in tx_interrupt(),
1640 * at the beginning of receive - it is in rx_interrupt() and
1641 * at the end of transmit/receive - it is the eot_interrupt() function.
1642 * These functions are multiplexed by cosa_interrupt() according to the
1643 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1644 * separate functions to make it more readable. These functions are inline,
1645 * so there should be no overhead of function call.
1646 *
1647 * In the COSA bus-master mode, we need to tell the card the address of a
1648 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1649 * It's time to use the bottom half :-(
1650 */
1651
1652/* Transmit interrupt routine - called when COSA is willing to obtain
1653 * data from the OS. The most tricky part of the routine is selection
1654 * of channel we (OS) want to send packet for. For SRP we should probably
1655 * use the round-robin approach. The newer COSA firmwares have a simple
1656 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1657 * channel 0 or 1 doesn't want to receive data.
1658 *
1659 * It seems there is a bug in COSA firmware (need to trace it further):
1660 * When the driver status says that the kernel has no more data for transmit
1661 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1662 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1663 * the TX interrupt but does not mark the channel as ready-to-transmit.
1664 * The fix seems to be to push the packet to COSA despite its request.
1665 * We first try to obey the card's opinion, and then fall back to forced TX.
1666 */
1667static inline void tx_interrupt(struct cosa_data *cosa, int status)
1668{
1669        unsigned long flags, flags1;
1670#ifdef DEBUG_IRQS
1671        pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1672#endif
1673        spin_lock_irqsave(&cosa->lock, flags);
1674        set_bit(TXBIT, &cosa->rxtx);
1675        if (!test_bit(IRQBIT, &cosa->rxtx)) {
1676                /* flow control, see the comment above */
1677                int i = 0;
1678
1679                if (!cosa->txbitmap) {
1680                        pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1681                                cosa->name);
1682                        put_driver_status_nolock(cosa);
1683                        clear_bit(TXBIT, &cosa->rxtx);
1684                        spin_unlock_irqrestore(&cosa->lock, flags);
1685                        return;
1686                }
1687                while (1) {
1688                        cosa->txchan++;
1689                        i++;
1690                        if (cosa->txchan >= cosa->nchannels)
1691                                cosa->txchan = 0;
1692                        if (!(cosa->txbitmap & (1 << cosa->txchan)))
1693                                continue;
1694                        if (~status &
1695                            (1 << (cosa->txchan + DRIVER_TXMAP_SHIFT)))
1696                                break;
1697                        /* in second pass, accept first ready-to-TX channel */
1698                        if (i > cosa->nchannels) {
1699                                /* Can be safely ignored */
1700#ifdef DEBUG_IRQS
1701                                printk(KERN_DEBUG "%s: Forcing TX "
1702                                        "to not-ready channel %d\n",
1703                                        cosa->name, cosa->txchan);
1704#endif
1705                                break;
1706                        }
1707                }
1708
1709                cosa->txsize = cosa->chan[cosa->txchan].txsize;
1710                if (cosa_dma_able(cosa->chan + cosa->txchan,
1711                                  cosa->chan[cosa->txchan].txbuf,
1712                                  cosa->txsize)) {
1713                        cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1714                } else {
1715                        memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1716                               cosa->txsize);
1717                        cosa->txbuf = cosa->bouncebuf;
1718                }
1719        }
1720
1721        if (is_8bit(cosa)) {
1722                if (!test_bit(IRQBIT, &cosa->rxtx)) {
1723                        cosa_putstatus(cosa, SR_TX_INT_ENA);
1724                        cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0) |
1725                                ((cosa->txsize >> 8) & 0x1f));
1726#ifdef DEBUG_IO
1727                        debug_status_out(cosa, SR_TX_INT_ENA);
1728                        debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0) |
1729                                       ((cosa->txsize >> 8) & 0x1f));
1730                        debug_data_in(cosa, cosa_getdata8(cosa));
1731#else
1732                        cosa_getdata8(cosa);
1733#endif
1734                        set_bit(IRQBIT, &cosa->rxtx);
1735                        spin_unlock_irqrestore(&cosa->lock, flags);
1736                        return;
1737                } else {
1738                        clear_bit(IRQBIT, &cosa->rxtx);
1739                        cosa_putstatus(cosa, 0);
1740                        cosa_putdata8(cosa, cosa->txsize & 0xff);
1741#ifdef DEBUG_IO
1742                        debug_status_out(cosa, 0);
1743                        debug_data_out(cosa, cosa->txsize & 0xff);
1744#endif
1745                }
1746        } else {
1747                cosa_putstatus(cosa, SR_TX_INT_ENA);
1748                cosa_putdata16(cosa, ((cosa->txchan << 13) & 0xe000)
1749                        | (cosa->txsize & 0x1fff));
1750#ifdef DEBUG_IO
1751                debug_status_out(cosa, SR_TX_INT_ENA);
1752                debug_data_out(cosa, ((cosa->txchan << 13) & 0xe000) |
1753                               (cosa->txsize & 0x1fff));
1754                debug_data_in(cosa, cosa_getdata8(cosa));
1755                debug_status_out(cosa, 0);
1756#else
1757                cosa_getdata8(cosa);
1758#endif
1759                cosa_putstatus(cosa, 0);
1760        }
1761
1762        if (cosa->busmaster) {
1763                unsigned long addr = virt_to_bus(cosa->txbuf);
1764                int count = 0;
1765
1766                pr_info("busmaster IRQ\n");
1767                while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1768                        count++;
1769                        udelay(10);
1770                        if (count > 1000)
1771                                break;
1772                }
1773                pr_info("status %x\n", cosa_getstatus(cosa));
1774                pr_info("ready after %d loops\n", count);
1775                cosa_putdata16(cosa, (addr >> 16) & 0xffff);
1776
1777                count = 0;
1778                while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1779                        count++;
1780                        if (count > 1000)
1781                                break;
1782                        udelay(10);
1783                }
1784                pr_info("ready after %d loops\n", count);
1785                cosa_putdata16(cosa, addr & 0xffff);
1786                flags1 = claim_dma_lock();
1787                set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1788                enable_dma(cosa->dma);
1789                release_dma_lock(flags1);
1790        } else {
1791                /* start the DMA */
1792                flags1 = claim_dma_lock();
1793                disable_dma(cosa->dma);
1794                clear_dma_ff(cosa->dma);
1795                set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1796                set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1797                set_dma_count(cosa->dma, cosa->txsize);
1798                enable_dma(cosa->dma);
1799                release_dma_lock(flags1);
1800        }
1801        cosa_putstatus(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1802#ifdef DEBUG_IO
1803        debug_status_out(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1804#endif
1805        spin_unlock_irqrestore(&cosa->lock, flags);
1806}
1807
1808static inline void rx_interrupt(struct cosa_data *cosa, int status)
1809{
1810        unsigned long flags;
1811#ifdef DEBUG_IRQS
1812        pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1813#endif
1814
1815        spin_lock_irqsave(&cosa->lock, flags);
1816        set_bit(RXBIT, &cosa->rxtx);
1817
1818        if (is_8bit(cosa)) {
1819                if (!test_bit(IRQBIT, &cosa->rxtx)) {
1820                        set_bit(IRQBIT, &cosa->rxtx);
1821                        put_driver_status_nolock(cosa);
1822                        cosa->rxsize = cosa_getdata8(cosa) << 8;
1823#ifdef DEBUG_IO
1824                        debug_data_in(cosa, cosa->rxsize >> 8);
1825#endif
1826                        spin_unlock_irqrestore(&cosa->lock, flags);
1827                        return;
1828                } else {
1829                        clear_bit(IRQBIT, &cosa->rxtx);
1830                        cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1831#ifdef DEBUG_IO
1832                        debug_data_in(cosa, cosa->rxsize & 0xff);
1833#endif
1834#if 0
1835                        pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1836                                cosa->num, cosa->rxsize);
1837#endif
1838                }
1839        } else {
1840                cosa->rxsize = cosa_getdata16(cosa);
1841#ifdef DEBUG_IO
1842                debug_data_in(cosa, cosa->rxsize);
1843#endif
1844#if 0
1845                pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1846                        cosa->num, cosa->rxsize);
1847#endif
1848        }
1849        if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1850                pr_warn("%s: rx for unknown channel (0x%04x)\n",
1851                        cosa->name, cosa->rxsize);
1852                spin_unlock_irqrestore(&cosa->lock, flags);
1853                goto reject;
1854        }
1855        cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1856        cosa->rxsize &= 0x1fff;
1857        spin_unlock_irqrestore(&cosa->lock, flags);
1858
1859        cosa->rxbuf = NULL;
1860        if (cosa->rxchan->setup_rx)
1861                cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1862
1863        if (!cosa->rxbuf) {
1864reject:         /* Reject the packet */
1865                pr_info("cosa%d: rejecting packet on channel %d\n",
1866                        cosa->num, cosa->rxchan->num);
1867                cosa->rxbuf = cosa->bouncebuf;
1868        }
1869
1870        /* start the DMA */
1871        flags = claim_dma_lock();
1872        disable_dma(cosa->dma);
1873        clear_dma_ff(cosa->dma);
1874        set_dma_mode(cosa->dma, DMA_MODE_READ);
1875        if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff))
1876                set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1877        else
1878                set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1879
1880        set_dma_count(cosa->dma, (cosa->rxsize & 0x1fff));
1881        enable_dma(cosa->dma);
1882        release_dma_lock(flags);
1883        spin_lock_irqsave(&cosa->lock, flags);
1884        cosa_putstatus(cosa, SR_RX_DMA_ENA | SR_USR_INT_ENA);
1885        if (!is_8bit(cosa) && (status & SR_TX_RDY))
1886                cosa_putdata8(cosa, DRIVER_RX_READY);
1887#ifdef DEBUG_IO
1888        debug_status_out(cosa, SR_RX_DMA_ENA | SR_USR_INT_ENA);
1889        if (!is_8bit(cosa) && (status & SR_TX_RDY))
1890                debug_data_cmd(cosa, DRIVER_RX_READY);
1891#endif
1892        spin_unlock_irqrestore(&cosa->lock, flags);
1893}
1894
1895static inline void eot_interrupt(struct cosa_data *cosa, int status)
1896{
1897        unsigned long flags, flags1;
1898
1899        spin_lock_irqsave(&cosa->lock, flags);
1900        flags1 = claim_dma_lock();
1901        disable_dma(cosa->dma);
1902        clear_dma_ff(cosa->dma);
1903        release_dma_lock(flags1);
1904        if (test_bit(TXBIT, &cosa->rxtx)) {
1905                struct channel_data *chan = cosa->chan + cosa->txchan;
1906
1907                if (chan->tx_done)
1908                        if (chan->tx_done(chan, cosa->txsize))
1909                                clear_bit(chan->num, &cosa->txbitmap);
1910        } else if (test_bit(RXBIT, &cosa->rxtx)) {
1911#ifdef DEBUG_DATA
1912        {
1913                int i;
1914
1915                pr_info("cosa%dc%d: done rx(0x%x)",
1916                        cosa->num, cosa->rxchan->num, cosa->rxsize);
1917                for (i = 0; i < cosa->rxsize; i++)
1918                        pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1919                pr_cont("\n");
1920        }
1921#endif
1922                /* Packet for unknown channel? */
1923                if (cosa->rxbuf == cosa->bouncebuf)
1924                        goto out;
1925                if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1926                        memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1927                if (cosa->rxchan->rx_done)
1928                        if (cosa->rxchan->rx_done(cosa->rxchan))
1929                                clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1930        } else {
1931                pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1932        }
1933        /* Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1934         * cleared anyway). We should do it as soon as possible
1935         * so that we can tell the COSA we are done and to give it a time
1936         * for recovery.
1937         */
1938out:
1939        cosa->rxtx = 0;
1940        put_driver_status_nolock(cosa);
1941        spin_unlock_irqrestore(&cosa->lock, flags);
1942}
1943
1944static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1945{
1946        unsigned status;
1947        int count = 0;
1948        struct cosa_data *cosa = cosa_;
1949again:
1950        status = cosa_getstatus(cosa);
1951#ifdef DEBUG_IRQS
1952        pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1953#endif
1954#ifdef DEBUG_IO
1955        debug_status_in(cosa, status);
1956#endif
1957        switch (status & SR_CMD_FROM_SRP_MASK) {
1958        case SR_DOWN_REQUEST:
1959                tx_interrupt(cosa, status);
1960                break;
1961        case SR_UP_REQUEST:
1962                rx_interrupt(cosa, status);
1963                break;
1964        case SR_END_OF_TRANSFER:
1965                eot_interrupt(cosa, status);
1966                break;
1967        default:
1968                /* We may be too fast for SRP. Try to wait a bit more. */
1969                if (count++ < 100) {
1970                        udelay(100);
1971                        goto again;
1972                }
1973                pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1974                        cosa->num, status & 0xff, count);
1975        }
1976#ifdef DEBUG_IRQS
1977        if (count)
1978                pr_info("%s: %d-times got unknown status in IRQ\n",
1979                        cosa->name, count);
1980        else
1981                pr_info("%s: returning from IRQ\n", cosa->name);
1982#endif
1983        return IRQ_HANDLED;
1984}
1985
1986/* ---------- I/O debugging routines ---------- */
1987/* These routines can be used to monitor COSA/SRP I/O and to printk()
1988 * the data being transferred on the data and status I/O port in a
1989 * readable way.
1990 */
1991
1992#ifdef DEBUG_IO
1993static void debug_status_in(struct cosa_data *cosa, int status)
1994{
1995        char *s;
1996
1997        switch (status & SR_CMD_FROM_SRP_MASK) {
1998        case SR_UP_REQUEST:
1999                s = "RX_REQ";
2000                break;
2001        case SR_DOWN_REQUEST:
2002                s = "TX_REQ";
2003                break;
2004        case SR_END_OF_TRANSFER:
2005                s = "ET_REQ";
2006                break;
2007        default:
2008                s = "NO_REQ";
2009                break;
2010        }
2011        pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2012                cosa->name,
2013                status,
2014                status & SR_USR_RQ ? "USR_RQ|" : "",
2015                status & SR_TX_RDY ? "TX_RDY|" : "",
2016                status & SR_RX_RDY ? "RX_RDY|" : "",
2017                s);
2018}
2019
2020static void debug_status_out(struct cosa_data *cosa, int status)
2021{
2022        pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2023                cosa->name,
2024                status,
2025                status & SR_RX_DMA_ENA  ? "RXDMA|"  : "!rxdma|",
2026                status & SR_TX_DMA_ENA  ? "TXDMA|"  : "!txdma|",
2027                status & SR_RST         ? "RESET|"  : "",
2028                status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2029                status & SR_TX_INT_ENA  ? "TXINT|"  : "!txint|",
2030                status & SR_RX_INT_ENA  ? "RXINT"   : "!rxint");
2031}
2032
2033static void debug_data_in(struct cosa_data *cosa, int data)
2034{
2035        pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2036}
2037
2038static void debug_data_out(struct cosa_data *cosa, int data)
2039{
2040        pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2041}
2042
2043static void debug_data_cmd(struct cosa_data *cosa, int data)
2044{
2045        pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2046                cosa->name, data,
2047                data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2048                data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2049}
2050#endif
2051
2052/* EOF -- this file has not been truncated */
2053