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