- pre2
[davej-history.git] / drivers / net / wan / cosa.c
blob130f6b27d8305447cb72dccb8b471407c1951653
1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
3 /*
4 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
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.
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.
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.
22 * The driver for the SRP and COSA synchronous serial cards.
24 * HARDWARE INFO
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.
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.
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.
45 * Both types have downloadable firmware and communicate via ISA DMA.
46 * COSA can be also a bus-mastering device.
48 * SOFTWARE INFO
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.
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).
59 * AUTHOR
61 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
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 :-).
67 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
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
76 * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
77 * fixed a deadlock in cosa_sppp_open
80 /* ---------- Headers, macros, data structures ---------- */
82 #include <linux/config.h>
83 #include <linux/module.h>
84 #include <linux/kernel.h>
85 #include <linux/malloc.h>
86 #include <linux/poll.h>
87 #include <linux/fs.h>
88 #include <linux/devfs_fs_kernel.h>
89 #include <linux/sched.h>
90 #include <linux/interrupt.h>
91 #include <linux/delay.h>
92 #include <linux/errno.h>
93 #include <linux/ioport.h>
94 #include <linux/netdevice.h>
95 #include <linux/spinlock.h>
96 #include <linux/smp_lock.h>
98 #undef COSA_SLOW_IO /* for testing purposes only */
99 #undef REALLY_SLOW_IO
101 #include <asm/io.h>
102 #include <asm/dma.h>
103 #include <asm/byteorder.h>
105 #include "syncppp.h"
106 #include "cosa.h"
108 /* Linux version stuff */
109 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1)
110 typedef struct wait_queue *wait_queue_head_t;
111 #define DECLARE_WAITQUEUE(wait, current) \
112 struct wait_queue wait = { current, NULL }
113 #endif
115 /* Maximum length of the identification string. */
116 #define COSA_MAX_ID_STRING 128
118 /* Maximum length of the channel name */
119 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
121 /* Per-channel data structure */
123 struct channel_data {
124 void *if_ptr; /* General purpose pointer (used by SPPP) */
125 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
126 int num; /* Number of the channel */
127 struct cosa_data *cosa; /* Pointer to the per-card structure */
128 int txsize; /* Size of transmitted data */
129 char *txbuf; /* Transmit buffer */
130 char name[COSA_MAX_NAME]; /* channel name */
132 /* The HW layer interface */
133 /* routine called from the RX interrupt */
134 char *(*setup_rx)(struct channel_data *channel, int size);
135 /* routine called when the RX is done (from the EOT interrupt) */
136 int (*rx_done)(struct channel_data *channel);
137 /* routine called when the TX is done (from the EOT interrupt) */
138 int (*tx_done)(struct channel_data *channel, int size);
140 /* Character device parts */
141 struct semaphore rsem, wsem;
142 char *rxdata;
143 int rxsize;
144 wait_queue_head_t txwaitq, rxwaitq;
145 int tx_status, rx_status;
147 /* SPPP/HDLC device parts */
148 struct ppp_device pppdev;
149 struct sk_buff *rx_skb, *tx_skb;
150 struct net_device_stats stats;
153 /* cosa->firmware_status bits */
154 #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
155 #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
156 #define COSA_FW_START (1<<2) /* Is the microcode running? */
158 struct cosa_data {
159 int num; /* Card number */
160 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
161 unsigned int datareg, statusreg; /* I/O ports */
162 unsigned short irq, dma; /* IRQ and DMA number */
163 unsigned short startaddr; /* Firmware start address */
164 unsigned short busmaster; /* Use busmastering? */
165 int nchannels; /* # of channels on this card */
166 int driver_status; /* For communicating with firware */
167 int firmware_status; /* Downloaded, reseted, etc. */
168 int rxbitmap, txbitmap; /* Bitmap of channels who are willing to send/receive data */
169 int rxtx; /* RX or TX in progress? */
170 int enabled;
171 int usage; /* usage count */
172 int txchan, txsize, rxsize;
173 struct channel_data *rxchan;
174 char *bouncebuf;
175 char *txbuf, *rxbuf;
176 struct channel_data *chan;
177 spinlock_t lock; /* For exclusive operations on this structure */
178 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
179 char *type; /* card type */
183 * Define this if you want all the possible ports to be autoprobed.
184 * It is here but it probably is not a good idea to use this.
186 /* #define COSA_ISA_AUTOPROBE 1 */
189 * Character device major number. 117 was allocated for us.
190 * The value of 0 means to allocate a first free one.
192 static int cosa_major = 117;
195 * Encoding of the minor numbers:
196 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
197 * the highest bits means the card number.
199 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
200 * for the single card */
202 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
203 * macro doesn't like anything other than the raw number as an argument :-(
205 #define MAX_CARDS 16
206 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
208 #define DRIVER_RX_READY 0x0001
209 #define DRIVER_TX_READY 0x0002
210 #define DRIVER_TXMAP_SHIFT 2
211 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
214 * for cosa->rxtx - indicates whether either transmit or receive is
215 * in progress. These values are mean number of the bit.
217 #define TXBIT 0
218 #define RXBIT 1
219 #define IRQBIT 2
221 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
223 #undef DEBUG_DATA 1 /* Dump the data read or written to the channel */
224 #undef DEBUG_IRQS 1 /* Print the message when the IRQ is received */
225 #undef DEBUG_IO 1 /* Dump the I/O traffic */
227 #define TX_TIMEOUT (5*HZ)
229 /* Maybe the following should be allocated dynamically */
230 static struct cosa_data cosa_cards[MAX_CARDS];
231 static int nr_cards = 0;
233 #ifdef COSA_ISA_AUTOPROBE
234 static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
235 /* NOTE: DMA is not autoprobed!!! */
236 static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
237 #else
238 int io[MAX_CARDS+1] = { 0, };
239 int dma[MAX_CARDS+1] = { 0, };
240 #endif
241 /* IRQ can be safely autoprobed */
242 static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
244 #ifdef MODULE
245 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");
246 MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
247 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
248 MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
249 MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");
250 MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
252 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
253 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
254 #endif
256 /* I use this mainly for testing purposes */
257 #ifdef COSA_SLOW_IO
258 #define cosa_outb outb_p
259 #define cosa_outw outw_p
260 #define cosa_inb inb_p
261 #define cosa_inw inw_p
262 #else
263 #define cosa_outb outb
264 #define cosa_outw outw
265 #define cosa_inb inb
266 #define cosa_inw inw
267 #endif
269 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
271 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
272 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
273 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
274 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
275 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
276 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
278 /* Initialization stuff */
279 static int cosa_probe(int ioaddr, int irq, int dma);
281 /* HW interface */
282 static void cosa_enable_rx(struct channel_data *chan);
283 static void cosa_disable_rx(struct channel_data *chan);
284 static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
285 static void cosa_kick(struct cosa_data *cosa);
286 static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
288 /* SPPP/HDLC stuff */
289 static void sppp_channel_init(struct channel_data *chan);
290 static void sppp_channel_delete(struct channel_data *chan);
291 static int cosa_sppp_open(struct net_device *d);
292 static int cosa_sppp_close(struct net_device *d);
293 static void cosa_sppp_timeout(struct net_device *d);
294 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *d);
295 static char *sppp_setup_rx(struct channel_data *channel, int size);
296 static int sppp_rx_done(struct channel_data *channel);
297 static int sppp_tx_done(struct channel_data *channel, int size);
298 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
299 static struct net_device_stats *cosa_net_stats(struct net_device *dev);
301 /* Character device */
302 static void chardev_channel_init(struct channel_data *chan);
303 static char *chrdev_setup_rx(struct channel_data *channel, int size);
304 static int chrdev_rx_done(struct channel_data *channel);
305 static int chrdev_tx_done(struct channel_data *channel, int size);
306 static long long cosa_lseek(struct file *file,
307 long long offset, int origin);
308 static ssize_t cosa_read(struct file *file,
309 char *buf, size_t count, loff_t *ppos);
310 static ssize_t cosa_write(struct file *file,
311 const char *buf, size_t count, loff_t *ppos);
312 static unsigned int cosa_poll(struct file *file, poll_table *poll);
313 static int cosa_open(struct inode *inode, struct file *file);
314 static int cosa_release(struct inode *inode, struct file *file);
315 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
316 unsigned int cmd, unsigned long arg);
317 #ifdef COSA_FASYNC_WORKING
318 static int cosa_fasync(struct inode *inode, struct file *file, int on);
319 #endif
321 static struct file_operations cosa_fops = {
322 owner: THIS_MODULE,
323 llseek: cosa_lseek,
324 read: cosa_read,
325 write: cosa_write,
326 poll: cosa_poll,
327 ioctl: cosa_chardev_ioctl,
328 open: cosa_open,
329 release: cosa_release,
330 #ifdef COSA_FASYNC_WORKING
331 fasync: cosa_fasync,
332 #endif
335 /* Ioctls */
336 static int cosa_start(struct cosa_data *cosa, int address);
337 static int cosa_reset(struct cosa_data *cosa);
338 static int cosa_download(struct cosa_data *cosa, struct cosa_download *d);
339 static int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d);
341 /* COSA/SRP ROM monitor */
342 static int download(struct cosa_data *cosa, char *data, int addr, int len);
343 static int startmicrocode(struct cosa_data *cosa, int address);
344 static int readmem(struct cosa_data *cosa, char *data, int addr, int len);
345 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
347 /* Auxilliary functions */
348 static int get_wait_data(struct cosa_data *cosa);
349 static int put_wait_data(struct cosa_data *cosa, int data);
350 static int puthexnumber(struct cosa_data *cosa, int number);
351 static void put_driver_status(struct cosa_data *cosa);
352 static void put_driver_status_nolock(struct cosa_data *cosa);
354 /* Interrupt handling */
355 static void cosa_interrupt(int irq, void *cosa, struct pt_regs *regs);
357 /* I/O ops debugging */
358 #ifdef DEBUG_IO
359 static void debug_data_in(struct cosa_data *cosa, int data);
360 static void debug_data_out(struct cosa_data *cosa, int data);
361 static void debug_data_cmd(struct cosa_data *cosa, int data);
362 static void debug_status_in(struct cosa_data *cosa, int status);
363 static void debug_status_out(struct cosa_data *cosa, int status);
364 #endif
367 /* ---------- Initialization stuff ---------- */
369 static devfs_handle_t devfs_handle = NULL;
371 #ifdef MODULE
372 int init_module(void)
373 #else
374 static int __init cosa_init(void)
375 #endif
377 int i;
379 printk(KERN_INFO "cosa v1.08 (c) 1997-2000 Jan Kasprzak <kas@fi.muni.cz>\n");
380 #ifdef CONFIG_SMP
381 printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
382 #endif
383 if (cosa_major > 0) {
384 if (devfs_register_chrdev(cosa_major, "cosa", &cosa_fops)) {
385 printk(KERN_WARNING "cosa: unable to get major %d\n",
386 cosa_major);
387 return -EIO;
389 } else {
390 if (!(cosa_major=devfs_register_chrdev(0, "cosa", &cosa_fops))) {
391 printk(KERN_WARNING "cosa: unable to register chardev\n");
392 return -EIO;
395 for (i=0; i<MAX_CARDS; i++)
396 cosa_cards[i].num = -1;
397 for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
398 cosa_probe(io[i], irq[i], dma[i]);
399 devfs_handle = devfs_mk_dir (NULL, "cosa", NULL);
400 devfs_register_series (devfs_handle, "%u", nr_cards, DEVFS_FL_DEFAULT,
401 cosa_major, 0,
402 S_IFCHR | S_IRUSR | S_IWUSR,
403 &cosa_fops, NULL);
404 if (!nr_cards) {
405 printk(KERN_WARNING "cosa: no devices found.\n");
406 devfs_unregister_chrdev(cosa_major, "cosa");
407 return -ENODEV;
409 return 0;
412 #ifdef MODULE
413 void cleanup_module (void)
415 struct cosa_data *cosa;
416 printk(KERN_INFO "Unloading the cosa module\n");
418 devfs_unregister (devfs_handle);
419 for (cosa=cosa_cards; nr_cards--; cosa++) {
420 int i;
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);
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);
433 devfs_unregister_chrdev(cosa_major, "cosa");
435 #endif
438 * This function should register all the net devices needed for the
439 * single channel.
441 static __inline__ void channel_init(struct channel_data *chan)
443 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
445 /* Initialize the chardev data structures */
446 chardev_channel_init(chan);
448 /* Register the sppp interface */
449 sppp_channel_init(chan);
452 static int cosa_probe(int base, int irq, int dma)
454 struct cosa_data *cosa = cosa_cards+nr_cards;
455 int i;
457 memset(cosa, 0, sizeof(struct cosa_data));
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;
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;
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;
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;
485 cosa->dma = dma;
486 cosa->datareg = base;
487 cosa->statusreg = is_8bit(cosa)?base+1:base+2;
488 spin_lock_init(&cosa->lock);
490 if (check_region(base, is_8bit(cosa)?2:4))
491 return -1;
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 return -1;
498 /* Test the validity of identification string */
499 if (!strncmp(cosa->id_string, "SRP", 3))
500 cosa->type = "srp";
501 else if (!strncmp(cosa->id_string, "COSA", 4))
502 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
503 else {
504 /* Print a warning only if we are not autoprobing */
505 #ifndef COSA_ISA_AUTOPROBE
506 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
507 base);
508 #endif
509 return -1;
512 /* Now do IRQ autoprobe */
513 if (irq < 0) {
514 unsigned long irqs;
515 /* printk(KERN_INFO "IRQ autoprobe\n"); */
516 sti();
517 irqs = probe_irq_on();
519 * Enable interrupt on tx buffer empty (it sure is)
520 * really sure ?
521 * FIXME: When this code is not used as module, we should
522 * probably call udelay() instead of the interruptible sleep.
524 current->state = TASK_INTERRUPTIBLE;
525 cosa_putstatus(cosa, SR_TX_INT_ENA);
526 schedule_timeout(30);
527 current->state = TASK_RUNNING;
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);
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 return -1;
539 if (irq == 0) {
540 printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
541 cosa->datareg);
542 /* return -1; */
546 cosa->irq = irq;
547 cosa->num = nr_cards;
548 cosa->usage = 0;
549 cosa->nchannels = 2; /* FIXME: how to determine this? */
551 request_region(base, is_8bit(cosa)?2:4, cosa->type);
552 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa))
553 goto bad1;
554 if (request_dma(cosa->dma, cosa->type)) {
555 free_irq(cosa->irq, cosa);
556 bad1: release_region(cosa->datareg,is_8bit(cosa)?2:4);
557 printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
558 cosa->num);
559 return -1;
562 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
563 sprintf(cosa->name, "cosa%d", cosa->num);
565 /* Initialize the per-channel data */
566 cosa->chan = kmalloc(sizeof(struct channel_data)*cosa->nchannels,
567 GFP_KERNEL);
568 memset(cosa->chan, 0, sizeof(struct channel_data)*cosa->nchannels);
569 for (i=0; i<cosa->nchannels; i++) {
570 cosa->chan[i].cosa = cosa;
571 cosa->chan[i].num = i;
572 channel_init(cosa->chan+i);
575 printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
576 cosa->num, cosa->id_string, cosa->type,
577 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
579 return nr_cards++;
583 /*---------- SPPP/HDLC netdevice ---------- */
585 static void sppp_channel_init(struct channel_data *chan)
587 struct net_device *d;
588 chan->if_ptr = &chan->pppdev;
589 chan->pppdev.dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
590 memset(chan->pppdev.dev, 0, sizeof(struct net_device));
591 sppp_attach(&chan->pppdev);
592 d=chan->pppdev.dev;
593 strcpy(d->name, chan->name);
594 d->base_addr = chan->cosa->datareg;
595 d->irq = chan->cosa->irq;
596 d->dma = chan->cosa->dma;
597 d->priv = chan;
598 d->init = NULL;
599 d->open = cosa_sppp_open;
600 d->stop = cosa_sppp_close;
601 d->hard_start_xmit = cosa_sppp_tx;
602 d->do_ioctl = cosa_sppp_ioctl;
603 d->get_stats = cosa_net_stats;
604 d->tx_timeout = cosa_sppp_timeout;
605 d->watchdog_timeo = TX_TIMEOUT;
606 if (register_netdev(d) == -1) {
607 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
608 sppp_detach(chan->pppdev.dev);
609 return;
613 static void sppp_channel_delete(struct channel_data *chan)
615 sppp_detach(chan->pppdev.dev);
616 unregister_netdev(chan->pppdev.dev);
619 static int cosa_sppp_open(struct net_device *d)
621 struct channel_data *chan = d->priv;
622 int err, flags;
624 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
625 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
626 chan->cosa->name, chan->cosa->firmware_status);
627 return -EPERM;
629 spin_lock_irqsave(&chan->cosa->lock, flags);
630 if (chan->usage != 0) {
631 printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
632 chan->name, chan->usage);
633 spin_unlock_irqrestore(&chan->cosa->lock, flags);
634 return -EBUSY;
636 chan->setup_rx = sppp_setup_rx;
637 chan->tx_done = sppp_tx_done;
638 chan->rx_done = sppp_rx_done;
639 chan->usage=-1;
640 chan->cosa->usage++;
641 MOD_INC_USE_COUNT;
642 spin_unlock_irqrestore(&chan->cosa->lock, flags);
644 err = sppp_open(d);
645 if (err) {
646 spin_lock_irqsave(&chan->cosa->lock, flags);
647 chan->usage=0;
648 chan->cosa->usage--;
649 MOD_DEC_USE_COUNT;
651 spin_unlock_irqrestore(&chan->cosa->lock, flags);
652 return err;
655 netif_start_queue(d);
656 cosa_enable_rx(chan);
657 return 0;
660 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *dev)
662 struct channel_data *chan = dev->priv;
664 netif_stop_queue(dev);
666 chan->tx_skb = skb;
667 cosa_start_tx(chan, skb->data, skb->len);
668 return 0;
671 static void cosa_sppp_timeout(struct net_device *dev)
673 struct channel_data *chan = dev->priv;
675 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
676 chan->stats.rx_errors++;
677 chan->stats.rx_missed_errors++;
678 } else {
679 chan->stats.tx_errors++;
680 chan->stats.tx_aborted_errors++;
682 cosa_kick(chan->cosa);
683 if (chan->tx_skb) {
684 dev_kfree_skb(chan->tx_skb);
685 chan->tx_skb = 0;
687 netif_wake_queue(dev);
690 static int cosa_sppp_close(struct net_device *d)
692 struct channel_data *chan = d->priv;
693 int flags;
695 netif_stop_queue(d);
696 sppp_close(d);
697 cosa_disable_rx(chan);
698 spin_lock_irqsave(&chan->cosa->lock, flags);
699 if (chan->rx_skb) {
700 kfree_skb(chan->rx_skb);
701 chan->rx_skb = 0;
703 if (chan->tx_skb) {
704 kfree_skb(chan->tx_skb);
705 chan->tx_skb = 0;
707 chan->usage=0;
708 chan->cosa->usage--;
709 MOD_DEC_USE_COUNT;
710 spin_unlock_irqrestore(&chan->cosa->lock, flags);
711 return 0;
714 static char *sppp_setup_rx(struct channel_data *chan, int size)
717 * We can safely fall back to non-dma-able memory, because we have
718 * the cosa->bouncebuf pre-allocated.
720 if (chan->rx_skb)
721 kfree_skb(chan->rx_skb);
722 chan->rx_skb = dev_alloc_skb(size);
723 if (chan->rx_skb == NULL) {
724 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
725 chan->name);
726 chan->stats.rx_dropped++;
727 return NULL;
729 chan->pppdev.dev->trans_start = jiffies;
730 return skb_put(chan->rx_skb, size);
733 static int sppp_rx_done(struct channel_data *chan)
735 if (!chan->rx_skb) {
736 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
737 chan->name);
738 chan->stats.rx_errors++;
739 chan->stats.rx_frame_errors++;
740 return 0;
742 chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
743 chan->rx_skb->dev = chan->pppdev.dev;
744 chan->rx_skb->mac.raw = chan->rx_skb->data;
745 chan->stats.rx_packets++;
746 chan->stats.rx_bytes += chan->cosa->rxsize;
747 netif_rx(chan->rx_skb);
748 chan->rx_skb = 0;
749 chan->pppdev.dev->trans_start = jiffies;
750 return 0;
753 /* ARGSUSED */
754 static int sppp_tx_done(struct channel_data *chan, int size)
756 if (!chan->tx_skb) {
757 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
758 chan->name);
759 chan->stats.tx_errors++;
760 chan->stats.tx_aborted_errors++;
761 return 1;
763 dev_kfree_skb_irq(chan->tx_skb);
764 chan->tx_skb = 0;
765 chan->stats.tx_packets++;
766 chan->stats.tx_bytes += size;
767 netif_wake_queue(chan->pppdev.dev);
768 return 1;
771 static struct net_device_stats *cosa_net_stats(struct net_device *dev)
773 struct channel_data *chan = dev->priv;
774 return &chan->stats;
778 /*---------- Character device ---------- */
780 static void chardev_channel_init(struct channel_data *chan)
782 init_MUTEX(&chan->rsem);
783 init_MUTEX(&chan->wsem);
786 static long long cosa_lseek(struct file * file,
787 long long offset, int origin)
789 return -ESPIPE;
792 static ssize_t cosa_read(struct file *file,
793 char *buf, size_t count, loff_t *ppos)
795 DECLARE_WAITQUEUE(wait, current);
796 int flags;
797 struct channel_data *chan = (struct channel_data *)file->private_data;
798 struct cosa_data *cosa = chan->cosa;
799 char *kbuf;
801 if (!(cosa->firmware_status & COSA_FW_START)) {
802 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
803 cosa->name, cosa->firmware_status);
804 return -EPERM;
806 if (down_interruptible(&chan->rsem))
807 return -ERESTARTSYS;
809 if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
810 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
811 up(&chan->rsem);
812 return -ENOMEM;
815 chan->rx_status = 0;
816 cosa_enable_rx(chan);
817 spin_lock_irqsave(&cosa->lock, flags);
818 add_wait_queue(&chan->rxwaitq, &wait);
819 while(!chan->rx_status) {
820 current->state = TASK_INTERRUPTIBLE;
821 spin_unlock_irqrestore(&cosa->lock, flags);
822 schedule();
823 spin_lock_irqsave(&cosa->lock, flags);
824 if (signal_pending(current) && chan->rx_status == 0) {
825 chan->rx_status = 1;
826 remove_wait_queue(&chan->rxwaitq, &wait);
827 current->state = TASK_RUNNING;
828 spin_unlock_irqrestore(&cosa->lock, flags);
829 up(&chan->rsem);
830 return -ERESTARTSYS;
833 remove_wait_queue(&chan->rxwaitq, &wait);
834 current->state = TASK_RUNNING;
835 kbuf = chan->rxdata;
836 count = chan->rxsize;
837 spin_unlock_irqrestore(&cosa->lock, flags);
838 up(&chan->rsem);
840 if (copy_to_user(buf, kbuf, count)) {
841 kfree(buf);
842 return -EFAULT;
844 kfree(kbuf);
845 return count;
848 static char *chrdev_setup_rx(struct channel_data *chan, int size)
850 /* Expect size <= COSA_MTU */
851 chan->rxsize = size;
852 return chan->rxdata;
855 static int chrdev_rx_done(struct channel_data *chan)
857 if (chan->rx_status) { /* Reader has died */
858 kfree(chan->rxdata);
859 up(&chan->wsem);
861 chan->rx_status = 1;
862 wake_up_interruptible(&chan->rxwaitq);
863 return 1;
867 static ssize_t cosa_write(struct file *file,
868 const char *buf, size_t count, loff_t *ppos)
870 DECLARE_WAITQUEUE(wait, current);
871 struct channel_data *chan = (struct channel_data *)file->private_data;
872 struct cosa_data *cosa = chan->cosa;
873 unsigned int flags;
874 char *kbuf;
876 if (!(cosa->firmware_status & COSA_FW_START)) {
877 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
878 cosa->name, cosa->firmware_status);
879 return -EPERM;
881 if (down_interruptible(&chan->wsem))
882 return -ERESTARTSYS;
884 if (count > COSA_MTU)
885 count = COSA_MTU;
887 /* Allocate the buffer */
888 if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
889 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
890 cosa->name);
891 up(&chan->wsem);
892 return -ENOMEM;
894 if (copy_from_user(kbuf, buf, count)) {
895 up(&chan->wsem);
896 kfree(kbuf);
897 return -EFAULT;
899 chan->tx_status=0;
900 cosa_start_tx(chan, kbuf, count);
902 spin_lock_irqsave(&cosa->lock, flags);
903 add_wait_queue(&chan->txwaitq, &wait);
904 while(!chan->tx_status) {
905 current->state = TASK_INTERRUPTIBLE;
906 spin_unlock_irqrestore(&cosa->lock, flags);
907 schedule();
908 spin_lock_irqsave(&cosa->lock, flags);
909 if (signal_pending(current) && chan->tx_status == 0) {
910 chan->tx_status = 1;
911 remove_wait_queue(&chan->txwaitq, &wait);
912 current->state = TASK_RUNNING;
913 chan->tx_status = 1;
914 spin_unlock_irqrestore(&cosa->lock, flags);
915 return -ERESTARTSYS;
918 remove_wait_queue(&chan->txwaitq, &wait);
919 current->state = TASK_RUNNING;
920 up(&chan->wsem);
921 spin_unlock_irqrestore(&cosa->lock, flags);
922 kfree(kbuf);
923 return count;
926 static int chrdev_tx_done(struct channel_data *chan, int size)
928 if (chan->tx_status) { /* Writer was interrupted */
929 kfree(chan->txbuf);
930 up(&chan->wsem);
932 chan->tx_status = 1;
933 wake_up_interruptible(&chan->txwaitq);
934 return 1;
937 static unsigned int cosa_poll(struct file *file, poll_table *poll)
939 printk(KERN_INFO "cosa_poll is here\n");
940 return 0;
943 static int cosa_open(struct inode *inode, struct file *file)
945 struct cosa_data *cosa;
946 struct channel_data *chan;
947 unsigned long flags;
948 int n;
950 if ((n=MINOR(file->f_dentry->d_inode->i_rdev)>>CARD_MINOR_BITS)
951 >= nr_cards)
952 return -ENODEV;
953 cosa = cosa_cards+n;
955 if ((n=MINOR(file->f_dentry->d_inode->i_rdev)
956 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
957 return -ENODEV;
958 chan = cosa->chan + n;
960 file->private_data = chan;
962 spin_lock_irqsave(&cosa->lock, flags);
964 if (chan->usage < 0) { /* in netdev mode */
965 spin_unlock_irqrestore(&cosa->lock, flags);
966 return -EBUSY;
968 cosa->usage++;
969 chan->usage++;
971 chan->tx_done = chrdev_tx_done;
972 chan->setup_rx = chrdev_setup_rx;
973 chan->rx_done = chrdev_rx_done;
974 spin_unlock_irqrestore(&cosa->lock, flags);
975 return 0;
978 static int cosa_release(struct inode *inode, struct file *file)
980 struct channel_data *channel = (struct channel_data *)file->private_data;
981 struct cosa_data *cosa;
982 unsigned long flags;
984 lock_kernel();
985 cosa = channel->cosa;
986 spin_lock_irqsave(&cosa->lock, flags);
987 cosa->usage--;
988 channel->usage--;
989 spin_unlock_irqrestore(&cosa->lock, flags);
990 unlock_kernel();
991 return 0;
994 #ifdef COSA_FASYNC_WORKING
995 static struct fasync_struct *fasync[256] = { NULL, };
997 /* To be done ... */
998 static int cosa_fasync(struct inode *inode, struct file *file, int on)
1000 int port = MINOR(inode->i_rdev);
1001 int rv = fasync_helper(inode, file, on, &fasync[port]);
1002 return rv < 0 ? rv : 0;
1004 #endif
1007 /* ---------- Ioctls ---------- */
1010 * Ioctl subroutines can safely be made inline, because they are called
1011 * only from cosa_ioctl().
1013 static inline int cosa_reset(struct cosa_data *cosa)
1015 char idstring[COSA_MAX_ID_STRING];
1016 if (cosa->usage > 1)
1017 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1018 cosa->num, cosa->usage);
1019 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1020 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1021 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1022 return -EIO;
1024 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1025 idstring);
1026 cosa->firmware_status |= COSA_FW_RESET;
1027 return 0;
1030 /* High-level function to download data into COSA memory. Calls download() */
1031 static inline int cosa_download(struct cosa_data *cosa, struct cosa_download *d)
1033 int i;
1034 int addr, len;
1035 char *code;
1037 if (cosa->usage > 1)
1038 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1039 cosa->name, cosa->usage);
1040 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1041 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1042 cosa->name, cosa->firmware_status);
1043 return -EPERM;
1046 if (get_user(addr, &(d->addr)) ||
1047 __get_user(len, &(d->len)) ||
1048 __get_user(code, &(d->code)))
1049 return -EFAULT;
1051 if (d->addr < 0 || d->addr > COSA_MAX_FIRMWARE_SIZE)
1052 return -EINVAL;
1053 if (d->len < 0 || d->len > COSA_MAX_FIRMWARE_SIZE)
1054 return -EINVAL;
1056 /* If something fails, force the user to reset the card */
1057 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1059 if ((i=download(cosa, d->code, len, addr)) < 0) {
1060 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1061 cosa->num, i);
1062 return -EIO;
1064 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1065 cosa->num, len, addr);
1066 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1067 return 0;
1070 /* High-level function to read COSA memory. Calls readmem() */
1071 static inline int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d)
1073 int i;
1074 int addr, len;
1075 char *code;
1077 if (cosa->usage > 1)
1078 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1079 "cosa->usage > 1 (%d). Odd things may happen.\n",
1080 cosa->num, cosa->usage);
1081 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1082 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1083 cosa->name, cosa->firmware_status);
1084 return -EPERM;
1087 if (get_user(addr, &(d->addr)) ||
1088 __get_user(len, &(d->len)) ||
1089 __get_user(code, &(d->code)))
1090 return -EFAULT;
1092 /* If something fails, force the user to reset the card */
1093 cosa->firmware_status &= ~COSA_FW_RESET;
1095 if ((i=readmem(cosa, d->code, len, addr)) < 0) {
1096 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1097 cosa->num, i);
1098 return -EIO;
1100 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1101 cosa->num, len, addr);
1102 cosa->firmware_status |= COSA_FW_RESET;
1103 return 0;
1106 /* High-level function to start microcode. Calls startmicrocode(). */
1107 static inline int cosa_start(struct cosa_data *cosa, int address)
1109 int i;
1111 if (cosa->usage > 1)
1112 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1113 cosa->num, cosa->usage);
1115 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1116 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1117 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1118 cosa->name, cosa->firmware_status);
1119 return -EPERM;
1121 cosa->firmware_status &= ~COSA_FW_RESET;
1122 if ((i=startmicrocode(cosa, address)) < 0) {
1123 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1124 cosa->num, address, i);
1125 return -EIO;
1127 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1128 cosa->num, address);
1129 cosa->startaddr = address;
1130 cosa->firmware_status |= COSA_FW_START;
1131 return 0;
1134 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1135 static inline int cosa_getidstr(struct cosa_data *cosa, char *string)
1137 int l = strlen(cosa->id_string)+1;
1138 if (copy_to_user(string, cosa->id_string, l))
1139 return -EFAULT;
1140 return l;
1143 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1144 static inline int cosa_gettype(struct cosa_data *cosa, char *string)
1146 int l = strlen(cosa->type)+1;
1147 if (copy_to_user(string, cosa->type, l))
1148 return -EFAULT;
1149 return l;
1152 static int cosa_ioctl_common(struct cosa_data *cosa,
1153 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1155 switch(cmd) {
1156 case COSAIORSET: /* Reset the device */
1157 if (!capable(CAP_NET_ADMIN))
1158 return -EACCES;
1159 return cosa_reset(cosa);
1160 case COSAIOSTRT: /* Start the firmware */
1161 if (!capable(CAP_SYS_RAWIO))
1162 return -EACCES;
1163 return cosa_start(cosa, arg);
1164 case COSAIODOWNLD: /* Download the firmware */
1165 if (!capable(CAP_SYS_RAWIO))
1166 return -EACCES;
1167 return cosa_download(cosa, (struct cosa_download *)arg);
1168 case COSAIORMEM:
1169 if (!capable(CAP_SYS_RAWIO))
1170 return -EACCES;
1171 return cosa_readmem(cosa, (struct cosa_download *)arg);
1172 case COSAIORTYPE:
1173 return cosa_gettype(cosa, (char *)arg);
1174 case COSAIORIDSTR:
1175 return cosa_getidstr(cosa, (char *)arg);
1177 * These two are _very_ugly_hack_(tm). Don't even look at this.
1178 * Implementing this saved me few reboots after some process segfaulted
1179 * inside this module.
1181 #ifdef MODULE
1182 #if 0
1183 case COSAIOMINC:
1184 MOD_INC_USE_COUNT;
1185 return 0;
1186 case COSAIOMDEC:
1187 MOD_DEC_USE_COUNT;
1188 return 0;
1189 #endif
1190 #endif
1191 case COSAIONRCARDS:
1192 return nr_cards;
1193 case COSAIONRCHANS:
1194 return cosa->nchannels;
1195 case COSAIOBMSET:
1196 if (!capable(CAP_SYS_RAWIO))
1197 return -EACCES;
1198 if (is_8bit(cosa))
1199 return -EINVAL;
1200 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1201 return -EINVAL;
1202 cosa->busmaster = arg;
1203 return 0;
1204 case COSAIOBMGET:
1205 return cosa->busmaster;
1207 return -ENOIOCTLCMD;
1210 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
1211 int cmd)
1213 int rv;
1214 struct channel_data *chan = (struct channel_data *)dev->priv;
1215 rv = cosa_ioctl_common(chan->cosa, chan, cmd, (int)ifr->ifr_data);
1216 if (rv == -ENOIOCTLCMD) {
1217 return sppp_do_ioctl(dev, ifr, cmd);
1219 return rv;
1222 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1223 unsigned int cmd, unsigned long arg)
1225 struct channel_data *channel = (struct channel_data *)file->private_data;
1226 struct cosa_data *cosa = channel->cosa;
1227 return cosa_ioctl_common(cosa, channel, cmd, arg);
1231 /*---------- HW layer interface ---------- */
1234 * The higher layer can bind itself to the HW layer by setting the callbacks
1235 * in the channel_data structure and by using these routines.
1237 static void cosa_enable_rx(struct channel_data *chan)
1239 struct cosa_data *cosa = chan->cosa;
1241 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1242 put_driver_status(cosa);
1245 static void cosa_disable_rx(struct channel_data *chan)
1247 struct cosa_data *cosa = chan->cosa;
1249 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1250 put_driver_status(cosa);
1254 * FIXME: This routine probably should check for cosa_start_tx() called when
1255 * the previous transmit is still unfinished. In this case the non-zero
1256 * return value should indicate to the caller that the queuing(sp?) up
1257 * the transmit has failed.
1259 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1261 struct cosa_data *cosa = chan->cosa;
1262 int flags;
1263 #ifdef DEBUG_DATA
1264 int i;
1266 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1267 chan->num, len);
1268 for (i=0; i<len; i++)
1269 printk(" %02x", buf[i]&0xff);
1270 printk("\n");
1271 #endif
1272 spin_lock_irqsave(&cosa->lock, flags);
1273 chan->txbuf = buf;
1274 chan->txsize = len;
1275 if (len > COSA_MTU)
1276 chan->txsize = COSA_MTU;
1277 spin_unlock_irqrestore(&cosa->lock, flags);
1279 /* Tell the firmware we are ready */
1280 set_bit(chan->num, &cosa->txbitmap);
1281 put_driver_status(cosa);
1283 return 0;
1286 static void put_driver_status(struct cosa_data *cosa)
1288 unsigned flags=0;
1289 int status;
1291 spin_lock_irqsave(&cosa->lock, flags);
1293 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1294 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1295 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1296 &DRIVER_TXMAP_MASK : 0);
1297 if (!cosa->rxtx) {
1298 if (cosa->rxbitmap|cosa->txbitmap) {
1299 if (!cosa->enabled) {
1300 cosa_putstatus(cosa, SR_RX_INT_ENA);
1301 #ifdef DEBUG_IO
1302 debug_status_out(cosa, SR_RX_INT_ENA);
1303 #endif
1304 cosa->enabled = 1;
1306 } else if (cosa->enabled) {
1307 cosa->enabled = 0;
1308 cosa_putstatus(cosa, 0);
1309 #ifdef DEBUG_IO
1310 debug_status_out(cosa, 0);
1311 #endif
1313 cosa_putdata8(cosa, status);
1314 #ifdef DEBUG_IO
1315 debug_data_cmd(cosa, status);
1316 #endif
1318 spin_unlock_irqrestore(&cosa->lock, flags);
1321 static void put_driver_status_nolock(struct cosa_data *cosa)
1323 int status;
1325 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1326 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1327 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1328 &DRIVER_TXMAP_MASK : 0);
1330 if (cosa->rxbitmap|cosa->txbitmap) {
1331 cosa_putstatus(cosa, SR_RX_INT_ENA);
1332 #ifdef DEBUG_IO
1333 debug_status_out(cosa, SR_RX_INT_ENA);
1334 #endif
1335 cosa->enabled = 1;
1336 } else {
1337 cosa_putstatus(cosa, 0);
1338 #ifdef DEBUG_IO
1339 debug_status_out(cosa, 0);
1340 #endif
1341 cosa->enabled = 0;
1343 cosa_putdata8(cosa, status);
1344 #ifdef DEBUG_IO
1345 debug_data_cmd(cosa, status);
1346 #endif
1350 * The "kickme" function: When the DMA times out, this is called to
1351 * clean up the driver status.
1352 * FIXME: Preliminary support, the interface is probably wrong.
1354 static void cosa_kick(struct cosa_data *cosa)
1356 unsigned flags, flags1;
1357 char *s = "(probably) IRQ";
1359 if (test_bit(RXBIT, &cosa->rxtx))
1360 s = "RX DMA";
1361 if (test_bit(TXBIT, &cosa->rxtx))
1362 s = "TX DMA";
1364 printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
1365 spin_lock_irqsave(&cosa->lock, flags);
1366 cosa->rxtx = 0;
1368 flags1 = claim_dma_lock();
1369 disable_dma(cosa->dma);
1370 clear_dma_ff(cosa->dma);
1371 release_dma_lock(flags1);
1373 /* FIXME: Anything else? */
1374 udelay(100);
1375 cosa_putstatus(cosa, 0);
1376 udelay(100);
1377 (void) cosa_getdata8(cosa);
1378 udelay(100);
1379 cosa_putdata8(cosa, 0);
1380 udelay(100);
1381 put_driver_status_nolock(cosa);
1382 spin_unlock_irqrestore(&cosa->lock, flags);
1386 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1387 * physical memory and doesn't span the 64k boundary. For now it seems
1388 * SKB's never do this, but we'll check this anyway.
1390 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1392 static int count = 0;
1393 unsigned long b = (unsigned long)buf;
1394 if (b+len >= MAX_DMA_ADDRESS)
1395 return 0;
1396 if ((b^ (b+len)) & 0x10000) {
1397 if (count++ < 5)
1398 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1399 chan->name);
1400 return 0;
1402 return 1;
1406 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1409 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1410 * drivers need to say 4-digit hex number meaning start address of the microcode
1411 * separated by a single space. Monitor replies by saying " =". Now driver
1412 * has to write 4-digit hex number meaning the last byte address ended
1413 * by a single space. Monitor has to reply with a space. Now the download
1414 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1416 static int download(struct cosa_data *cosa, char *microcode, int length, int address)
1418 int i;
1420 if (put_wait_data(cosa, 'w') == -1) return -1;
1421 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1422 if (get_wait_data(cosa) != '=') return -3;
1424 if (puthexnumber(cosa, address) < 0) return -4;
1425 if (put_wait_data(cosa, ' ') == -1) return -10;
1426 if (get_wait_data(cosa) != ' ') return -11;
1427 if (get_wait_data(cosa) != '=') return -12;
1429 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1430 if (put_wait_data(cosa, ' ') == -1) return -18;
1431 if (get_wait_data(cosa) != ' ') return -19;
1433 while (length--) {
1434 char c;
1435 #ifndef SRP_DOWNLOAD_AT_BOOT
1436 if (get_user(c, microcode))
1437 return -23; /* ??? */
1438 #else
1439 c = *microcode;
1440 #endif
1441 if (put_wait_data(cosa, c) == -1)
1442 return -20;
1443 microcode++;
1446 if (get_wait_data(cosa) != '\r') return -21;
1447 if (get_wait_data(cosa) != '\n') return -22;
1448 if (get_wait_data(cosa) != '.') return -23;
1449 #if 0
1450 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1451 #endif
1452 return 0;
1457 * Starting microcode is done via the "g" command of the SRP monitor.
1458 * The chat should be the following: "g" "g=" "<addr><CR>"
1459 * "<CR><CR><LF><CR><LF>".
1461 static int startmicrocode(struct cosa_data *cosa, int address)
1463 if (put_wait_data(cosa, 'g') == -1) return -1;
1464 if (get_wait_data(cosa) != 'g') return -2;
1465 if (get_wait_data(cosa) != '=') return -3;
1467 if (puthexnumber(cosa, address) < 0) return -4;
1468 if (put_wait_data(cosa, '\r') == -1) return -5;
1470 if (get_wait_data(cosa) != '\r') return -6;
1471 if (get_wait_data(cosa) != '\r') return -7;
1472 if (get_wait_data(cosa) != '\n') return -8;
1473 if (get_wait_data(cosa) != '\r') return -9;
1474 if (get_wait_data(cosa) != '\n') return -10;
1475 #if 0
1476 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1477 #endif
1478 return 0;
1482 * Reading memory is done via the "r" command of the SRP monitor.
1483 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1484 * Then driver can read the data and the conversation is finished
1485 * by SRP monitor sending "<CR><LF>." (dot at the end).
1487 * This routine is not needed during the normal operation and serves
1488 * for debugging purposes only.
1490 static int readmem(struct cosa_data *cosa, char *microcode, int length, int address)
1492 if (put_wait_data(cosa, 'r') == -1) return -1;
1493 if ((get_wait_data(cosa)) != 'r') return -2;
1494 if ((get_wait_data(cosa)) != '=') return -3;
1496 if (puthexnumber(cosa, address) < 0) return -4;
1497 if (put_wait_data(cosa, ' ') == -1) return -5;
1498 if (get_wait_data(cosa) != ' ') return -6;
1499 if (get_wait_data(cosa) != '=') return -7;
1501 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1502 if (put_wait_data(cosa, ' ') == -1) return -9;
1503 if (get_wait_data(cosa) != ' ') return -10;
1505 while (length--) {
1506 char c;
1507 int i;
1508 if ((i=get_wait_data(cosa)) == -1) {
1509 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1510 length);
1511 return -11;
1513 c=i;
1514 #if 1
1515 if (put_user(c, microcode))
1516 return -23; /* ??? */
1517 #else
1518 *microcode = c;
1519 #endif
1520 microcode++;
1523 if (get_wait_data(cosa) != '\r') return -21;
1524 if (get_wait_data(cosa) != '\n') return -22;
1525 if (get_wait_data(cosa) != '.') return -23;
1526 #if 0
1527 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1528 #endif
1529 return 0;
1533 * This function resets the device and reads the initial prompt
1534 * of the device's ROM monitor.
1536 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1538 int i=0, id=0, prev=0, curr=0;
1540 /* Reset the card ... */
1541 cosa_putstatus(cosa, 0);
1542 cosa_getdata8(cosa);
1543 cosa_putstatus(cosa, SR_RST);
1544 #ifdef MODULE
1545 current->state = TASK_INTERRUPTIBLE;
1546 schedule_timeout(HZ/2);
1547 current->state = TASK_RUNNING;
1548 #else
1549 udelay(5*100000);
1550 #endif
1551 /* Disable all IRQs from the card */
1552 cosa_putstatus(cosa, 0);
1555 * Try to read the ID string. The card then prints out the
1556 * identification string ended by the "\n\x2e".
1558 * The following loop is indexed through i (instead of id)
1559 * to avoid looping forever when for any reason
1560 * the port returns '\r', '\n' or '\x2e' permanently.
1562 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1563 if ((curr = get_wait_data(cosa)) == -1) {
1564 return -1;
1566 curr &= 0xff;
1567 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1568 idstring[id++] = curr;
1569 if (curr == 0x2e && prev == '\n')
1570 break;
1572 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1573 idstring[id] = '\0';
1574 return id;
1578 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1581 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1582 * bit to be set in a loop. It should be used in the exceptional cases
1583 * only (for example when resetting the card or downloading the firmware.
1585 static int get_wait_data(struct cosa_data *cosa)
1587 int retries = 1000;
1589 while (--retries) {
1590 /* read data and return them */
1591 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1592 short r;
1593 r = cosa_getdata8(cosa);
1594 #if 0
1595 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1596 #endif
1597 return r;
1599 /* sleep if not ready to read */
1600 current->state = TASK_INTERRUPTIBLE;
1601 schedule_timeout(1);
1603 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1604 cosa_getstatus(cosa));
1605 return -1;
1609 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1610 * bit to be set in a loop. It should be used in the exceptional cases
1611 * only (for example when resetting the card or downloading the firmware).
1613 static int put_wait_data(struct cosa_data *cosa, int data)
1615 int retries = 1000;
1616 while (--retries) {
1617 /* read data and return them */
1618 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1619 cosa_putdata8(cosa, data);
1620 #if 0
1621 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1622 #endif
1623 return 0;
1625 #if 0
1626 /* sleep if not ready to read */
1627 current->state = TASK_INTERRUPTIBLE;
1628 schedule_timeout(1);
1629 #endif
1631 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1632 cosa->num, cosa_getstatus(cosa));
1633 return -1;
1637 * The following routine puts the hexadecimal number into the SRP monitor
1638 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1639 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1640 * (-2,-4,-6,-8) means that reading echo failed.
1642 static int puthexnumber(struct cosa_data *cosa, int number)
1644 char temp[5];
1645 int i;
1647 /* Well, I should probably replace this by something faster. */
1648 sprintf(temp, "%04X", number);
1649 for (i=0; i<4; i++) {
1650 if (put_wait_data(cosa, temp[i]) == -1) {
1651 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1652 cosa->num, i);
1653 return -1-2*i;
1655 if (get_wait_data(cosa) != temp[i]) {
1656 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1657 cosa->num, i);
1658 return -2-2*i;
1661 return 0;
1665 /* ---------- Interrupt routines ---------- */
1668 * There are three types of interrupt:
1669 * At the beginning of transmit - this handled is in tx_interrupt(),
1670 * at the beginning of receive - it is in rx_interrupt() and
1671 * at the end of transmit/receive - it is the eot_interrupt() function.
1672 * These functions are multiplexed by cosa_interrupt() according to the
1673 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1674 * separate functions to make it more readable. These functions are inline,
1675 * so there should be no overhead of function call.
1677 * In the COSA bus-master mode, we need to tell the card the address of a
1678 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1679 * It's time to use the bottom half :-(
1683 * Transmit interrupt routine - called when COSA is willing to obtain
1684 * data from the OS. The most tricky part of the routine is selection
1685 * of channel we (OS) want to send packet for. For SRP we should probably
1686 * use the round-robin approach. The newer COSA firmwares have a simple
1687 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1688 * channel 0 or 1 doesn't want to receive data.
1690 * It seems there is a bug in COSA firmware (need to trace it further):
1691 * When the driver status says that the kernel has no more data for transmit
1692 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1693 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1694 * the TX interrupt but does not mark the channel as ready-to-transmit.
1695 * The fix seems to be to push the packet to COSA despite its request.
1696 * We first try to obey the card's opinion, and then fall back to forced TX.
1698 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1700 unsigned long flags, flags1;
1701 #ifdef DEBUG_IRQS
1702 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1703 cosa->num, status);
1704 #endif
1705 spin_lock_irqsave(&cosa->lock, flags);
1706 set_bit(TXBIT, &cosa->rxtx);
1707 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1708 /* flow control, see the comment above */
1709 int i=0;
1710 if (!cosa->txbitmap) {
1711 printk(KERN_WARNING "%s: No channel wants data "
1712 "in TX IRQ. Expect DMA timeout.",
1713 cosa->name);
1714 put_driver_status_nolock(cosa);
1715 clear_bit(TXBIT, &cosa->rxtx);
1716 spin_unlock_irqrestore(&cosa->lock, flags);
1717 return;
1719 while(1) {
1720 cosa->txchan++;
1721 i++;
1722 if (cosa->txchan >= cosa->nchannels)
1723 cosa->txchan = 0;
1724 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1725 continue;
1726 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1727 break;
1728 /* in second pass, accept first ready-to-TX channel */
1729 if (i > cosa->nchannels) {
1730 /* Can be safely ignored */
1731 #ifdef DEBUG_IRQS
1732 printk(KERN_DEBUG "%s: Forcing TX "
1733 "to not-ready channel %d\n",
1734 cosa->name, cosa->txchan);
1735 #endif
1736 break;
1740 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1741 if (cosa_dma_able(cosa->chan+cosa->txchan,
1742 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1743 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1744 } else {
1745 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1746 cosa->txsize);
1747 cosa->txbuf = cosa->bouncebuf;
1751 if (is_8bit(cosa)) {
1752 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1753 cosa_putstatus(cosa, SR_TX_INT_ENA);
1754 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1755 ((cosa->txsize >> 8) & 0x1f));
1756 #ifdef DEBUG_IO
1757 debug_status_out(cosa, SR_TX_INT_ENA);
1758 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1759 ((cosa->txsize >> 8) & 0x1f));
1760 debug_data_in(cosa, cosa_getdata8(cosa));
1761 #else
1762 cosa_getdata8(cosa);
1763 #endif
1764 set_bit(IRQBIT, &cosa->rxtx);
1765 spin_unlock_irqrestore(&cosa->lock, flags);
1766 return;
1767 } else {
1768 clear_bit(IRQBIT, &cosa->rxtx);
1769 cosa_putstatus(cosa, 0);
1770 cosa_putdata8(cosa, cosa->txsize&0xff);
1771 #ifdef DEBUG_IO
1772 debug_status_out(cosa, 0);
1773 debug_data_out(cosa, cosa->txsize&0xff);
1774 #endif
1776 } else {
1777 cosa_putstatus(cosa, SR_TX_INT_ENA);
1778 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1779 | (cosa->txsize & 0x1fff));
1780 #ifdef DEBUG_IO
1781 debug_status_out(cosa, SR_TX_INT_ENA);
1782 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1783 | (cosa->txsize & 0x1fff));
1784 debug_data_in(cosa, cosa_getdata8(cosa));
1785 debug_status_out(cosa, 0);
1786 #else
1787 cosa_getdata8(cosa);
1788 #endif
1789 cosa_putstatus(cosa, 0);
1792 if (cosa->busmaster) {
1793 unsigned long addr = virt_to_bus(cosa->txbuf);
1794 int count=0;
1795 printk(KERN_INFO "busmaster IRQ\n");
1796 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1797 count++;
1798 udelay(10);
1799 if (count > 1000) break;
1801 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1802 printk(KERN_INFO "ready after %d loops\n", count);
1803 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1805 count = 0;
1806 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1807 count++;
1808 if (count > 1000) break;
1809 udelay(10);
1811 printk(KERN_INFO "ready after %d loops\n", count);
1812 cosa_putdata16(cosa, addr &0xffff);
1813 flags1 = claim_dma_lock();
1814 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1815 enable_dma(cosa->dma);
1816 release_dma_lock(flags1);
1817 } else {
1818 /* start the DMA */
1819 flags1 = claim_dma_lock();
1820 disable_dma(cosa->dma);
1821 clear_dma_ff(cosa->dma);
1822 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1823 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1824 set_dma_count(cosa->dma, cosa->txsize);
1825 enable_dma(cosa->dma);
1826 release_dma_lock(flags1);
1828 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1829 #ifdef DEBUG_IO
1830 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1831 #endif
1832 spin_unlock_irqrestore(&cosa->lock, flags);
1835 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1837 unsigned long flags;
1838 #ifdef DEBUG_IRQS
1839 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1840 #endif
1842 spin_lock_irqsave(&cosa->lock, flags);
1843 set_bit(RXBIT, &cosa->rxtx);
1845 if (is_8bit(cosa)) {
1846 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1847 set_bit(IRQBIT, &cosa->rxtx);
1848 put_driver_status_nolock(cosa);
1849 cosa->rxsize = cosa_getdata8(cosa) <<8;
1850 #ifdef DEBUG_IO
1851 debug_data_in(cosa, cosa->rxsize >> 8);
1852 #endif
1853 spin_unlock_irqrestore(&cosa->lock, flags);
1854 return;
1855 } else {
1856 clear_bit(IRQBIT, &cosa->rxtx);
1857 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1858 #ifdef DEBUG_IO
1859 debug_data_in(cosa, cosa->rxsize & 0xff);
1860 #endif
1861 #if 0
1862 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1863 cosa->num, cosa->rxsize);
1864 #endif
1866 } else {
1867 cosa->rxsize = cosa_getdata16(cosa);
1868 #ifdef DEBUG_IO
1869 debug_data_in(cosa, cosa->rxsize);
1870 #endif
1871 #if 0
1872 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1873 cosa->num, cosa->rxsize);
1874 #endif
1876 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1877 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1878 cosa->name, cosa->rxsize);
1879 spin_unlock_irqrestore(&cosa->lock, flags);
1880 goto reject;
1882 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1883 cosa->rxsize &= 0x1fff;
1884 spin_unlock_irqrestore(&cosa->lock, flags);
1886 cosa->rxbuf = NULL;
1887 if (cosa->rxchan->setup_rx)
1888 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1890 if (!cosa->rxbuf) {
1891 reject: /* Reject the packet */
1892 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1893 cosa->num, cosa->rxchan->num);
1894 cosa->rxbuf = cosa->bouncebuf;
1897 /* start the DMA */
1898 flags = claim_dma_lock();
1899 disable_dma(cosa->dma);
1900 clear_dma_ff(cosa->dma);
1901 set_dma_mode(cosa->dma, DMA_MODE_READ);
1902 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1903 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1904 } else {
1905 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1907 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1908 enable_dma(cosa->dma);
1909 release_dma_lock(flags);
1910 spin_lock_irqsave(&cosa->lock, flags);
1911 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1912 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1913 cosa_putdata8(cosa, DRIVER_RX_READY);
1914 #ifdef DEBUG_IO
1915 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1916 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1917 debug_data_cmd(cosa, DRIVER_RX_READY);
1918 #endif
1919 spin_unlock_irqrestore(&cosa->lock, flags);
1922 static void inline eot_interrupt(struct cosa_data *cosa, int status)
1924 unsigned long flags, flags1;
1925 spin_lock_irqsave(&cosa->lock, flags);
1926 flags1 = claim_dma_lock();
1927 disable_dma(cosa->dma);
1928 clear_dma_ff(cosa->dma);
1929 release_dma_lock(flags1);
1930 if (test_bit(TXBIT, &cosa->rxtx)) {
1931 struct channel_data *chan = cosa->chan+cosa->txchan;
1932 if (chan->tx_done)
1933 if (chan->tx_done(chan, cosa->txsize))
1934 clear_bit(chan->num, &cosa->txbitmap);
1935 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1936 #ifdef DEBUG_DATA
1938 int i;
1939 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1940 cosa->rxchan->num, cosa->rxsize);
1941 for (i=0; i<cosa->rxsize; i++)
1942 printk (" %02x", cosa->rxbuf[i]&0xff);
1943 printk("\n");
1945 #endif
1946 /* Packet for unknown channel? */
1947 if (cosa->rxbuf == cosa->bouncebuf)
1948 goto out;
1949 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1950 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1951 if (cosa->rxchan->rx_done)
1952 if (cosa->rxchan->rx_done(cosa->rxchan))
1953 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1954 } else {
1955 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1956 cosa->num);
1959 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1960 * cleared anyway). We should do it as soon as possible
1961 * so that we can tell the COSA we are done and to give it a time
1962 * for recovery.
1964 out:
1965 cosa->rxtx = 0;
1966 put_driver_status_nolock(cosa);
1967 spin_unlock_irqrestore(&cosa->lock, flags);
1970 static void cosa_interrupt(int irq, void *cosa_, struct pt_regs *regs)
1972 unsigned status;
1973 int count = 0;
1974 struct cosa_data *cosa = cosa_;
1975 again:
1976 status = cosa_getstatus(cosa);
1977 #ifdef DEBUG_IRQS
1978 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1979 status & 0xff);
1980 #endif
1981 #ifdef DEBUG_IO
1982 debug_status_in(cosa, status);
1983 #endif
1984 switch (status & SR_CMD_FROM_SRP_MASK) {
1985 case SR_DOWN_REQUEST:
1986 tx_interrupt(cosa, status);
1987 break;
1988 case SR_UP_REQUEST:
1989 rx_interrupt(cosa, status);
1990 break;
1991 case SR_END_OF_TRANSFER:
1992 eot_interrupt(cosa, status);
1993 break;
1994 default:
1995 /* We may be too fast for SRP. Try to wait a bit more. */
1996 if (count++ < 100) {
1997 udelay(100);
1998 goto again;
2000 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
2001 cosa->num, status & 0xff, count);
2003 #ifdef DEBUG_IRQS
2004 if (count)
2005 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
2006 cosa->name, count);
2007 else
2008 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
2009 #endif
2013 /* ---------- I/O debugging routines ---------- */
2015 * These routines can be used to monitor COSA/SRP I/O and to printk()
2016 * the data being transfered on the data and status I/O port in a
2017 * readable way.
2020 #ifdef DEBUG_IO
2021 static void debug_status_in(struct cosa_data *cosa, int status)
2023 char *s;
2024 switch(status & SR_CMD_FROM_SRP_MASK) {
2025 case SR_UP_REQUEST:
2026 s = "RX_REQ";
2027 break;
2028 case SR_DOWN_REQUEST:
2029 s = "TX_REQ";
2030 break;
2031 case SR_END_OF_TRANSFER:
2032 s = "ET_REQ";
2033 break;
2034 default:
2035 s = "NO_REQ";
2036 break;
2038 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2039 cosa->name,
2040 status,
2041 status & SR_USR_RQ ? "USR_RQ|":"",
2042 status & SR_TX_RDY ? "TX_RDY|":"",
2043 status & SR_RX_RDY ? "RX_RDY|":"",
2047 static void debug_status_out(struct cosa_data *cosa, int status)
2049 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2050 cosa->name,
2051 status,
2052 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
2053 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
2054 status & SR_RST ? "RESET|":"",
2055 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2056 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
2057 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
2060 static void debug_data_in(struct cosa_data *cosa, int data)
2062 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2065 static void debug_data_out(struct cosa_data *cosa, int data)
2067 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2070 static void debug_data_cmd(struct cosa_data *cosa, int data)
2072 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2073 cosa->name, data,
2074 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2075 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2077 #endif
2079 /* EOF -- this file has not been truncated */