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