Import 2.3.18pre1
[davej-history.git] / drivers / net / cosa.c
blob863cd4b59a6ec9f6b25d1aed5f09534e27848bb2
1 /* $Id: cosa.c,v 1.26 1999/07/09 15:02:37 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/module.h>
83 #include <linux/kernel.h>
84 #include <linux/malloc.h>
85 #include <linux/poll.h>
86 #include <linux/fs.h>
87 #include <linux/sched.h>
88 #include <linux/interrupt.h>
89 #include <linux/delay.h>
90 #include <linux/errno.h>
91 #include <linux/ioport.h>
92 #include <linux/netdevice.h>
93 #include <linux/spinlock.h>
95 #undef COSA_SLOW_IO /* for testing purposes only */
96 #undef REALLY_SLOW_IO
98 #include <asm/io.h>
99 #include <asm/dma.h>
100 #include <asm/byteorder.h>
102 #include "syncppp.h"
103 #include "cosa.h"
105 /* Linux version stuff */
106 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1)
107 typedef struct wait_queue *wait_queue_head_t;
108 #define DECLARE_WAITQUEUE(wait, current) \
109 struct wait_queue wait = { current, NULL }
110 #endif
112 /* Maximum length of the identification string. */
113 #define COSA_MAX_ID_STRING 128
115 /* Maximum length of the channel name */
116 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
118 /* Per-channel data structure */
120 struct channel_data {
121 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
122 int num; /* Number of the channel */
123 struct cosa_data *cosa; /* Pointer to the per-card structure */
124 int txsize; /* Size of transmitted data */
125 char *txbuf; /* Transmit buffer */
126 char name[COSA_MAX_NAME]; /* channel name */
128 /* The HW layer interface */
129 /* routine called from the RX interrupt */
130 char *(*setup_rx)(struct channel_data *channel, int size);
131 /* routine called when the RX is done (from the EOT interrupt) */
132 int (*rx_done)(struct channel_data *channel);
133 /* routine called when the TX is done (from the EOT interrupt) */
134 int (*tx_done)(struct channel_data *channel, int size);
136 /* Character device parts */
137 struct semaphore rsem, wsem;
138 char *rxdata;
139 int rxsize;
140 wait_queue_head_t txwaitq;
141 wait_queue_head_t rxwaitq;
142 int tx_status, rx_status;
144 /* SPPP/HDLC device parts */
145 struct ppp_device pppdev;
146 struct sk_buff *rx_skb, *tx_skb;
147 struct net_device_stats stats;
150 struct cosa_data {
151 int num; /* Card number */
152 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
153 unsigned int datareg, statusreg; /* I/O ports */
154 unsigned short irq, dma; /* IRQ and DMA number */
155 unsigned short startaddr; /* Firmware start address */
156 unsigned short busmaster; /* Use busmastering? */
157 int nchannels; /* # of channels on this card */
158 int driver_status; /* For communicating with firware */
159 int firmware_status; /* Downloaded, reseted, etc. */
160 int rxbitmap, txbitmap; /* Bitmap of channels who are willing to send/receive data */
161 int rxtx; /* RX or TX in progress? */
162 int enabled;
163 int usage; /* usage count */
164 int txchan, txsize, rxsize;
165 struct channel_data *rxchan;
166 char *bouncebuf;
167 char *txbuf, *rxbuf;
168 struct channel_data *chan;
169 spinlock_t lock; /* For exclusive operations on this structure */
170 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
171 char *type; /* card type */
175 * Define this if you want all the possible ports to be autoprobed.
176 * It is here but it probably is not a good idea to use this.
178 /* #define COSA_ISA_AUTOPROBE 1 */
181 * Character device major number. 117 was allocated for us.
182 * The value of 0 means to allocate a first free one.
184 static int cosa_major = 117;
187 * Encoding of the minor numbers:
188 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
189 * the highest bits means the card number.
191 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
192 * for the single card */
194 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
195 * macro doesn't like anything other than the raw number as an argument :-(
197 #define MAX_CARDS 16
198 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
200 #define DRIVER_RX_READY 0x0001
201 #define DRIVER_TX_READY 0x0002
202 #define DRIVER_TXMAP_SHIFT 2
203 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
206 * for cosa->rxtx - indicates whether either transmit or receive is
207 * in progress. These values are mean number of the bit.
209 #define TXBIT 0
210 #define RXBIT 1
211 #define IRQBIT 2
213 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
215 #undef DEBUG_DATA 1 /* Dump the data read or written to the channel */
216 #undef DEBUG_IRQS 1 /* Print the message when the IRQ is received */
217 #undef DEBUG_IO 1 /* Dump the I/O traffic */
219 /* Maybe the following should be allocated dynamically */
220 static struct cosa_data cosa_cards[MAX_CARDS];
221 static int nr_cards = 0;
223 #ifdef COSA_ISA_AUTOPROBE
224 static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
225 /* NOTE: DMA is not autoprobed!!! */
226 static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
227 #else
228 int io[MAX_CARDS+1] = { 0, };
229 int dma[MAX_CARDS+1] = { 0, };
230 #endif
231 /* IRQ can be safely autoprobed */
232 static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
234 #ifdef MODULE
235 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");
236 MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
237 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
238 MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
239 MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");
240 MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
242 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
243 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
244 #endif
246 /* I use this mainly for testing purposes */
247 #ifdef COSA_SLOW_IO
248 #define cosa_outb outb_p
249 #define cosa_outw outw_p
250 #define cosa_inb inb_p
251 #define cosa_inw inw_p
252 #else
253 #define cosa_outb outb
254 #define cosa_outw outw
255 #define cosa_inb inb
256 #define cosa_inw inw
257 #endif
259 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
261 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
262 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
263 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
264 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
265 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
266 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
268 /* Initialization stuff */
269 static int cosa_probe(int ioaddr, int irq, int dma);
271 /* HW interface */
272 static void cosa_enable_rx(struct channel_data *chan);
273 static void cosa_disable_rx(struct channel_data *chan);
274 static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
275 static void cosa_kick(struct cosa_data *cosa);
276 static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
278 /* SPPP/HDLC stuff */
279 static void sppp_channel_init(struct channel_data *chan);
280 static void sppp_channel_delete(struct channel_data *chan);
281 static int cosa_sppp_open(struct net_device *d);
282 static int cosa_sppp_close(struct net_device *d);
283 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *d);
284 static char *sppp_setup_rx(struct channel_data *channel, int size);
285 static int sppp_rx_done(struct channel_data *channel);
286 static int sppp_tx_done(struct channel_data *channel, int size);
287 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
288 static struct net_device_stats *cosa_net_stats(struct net_device *dev);
290 /* Character device */
291 static void chardev_channel_init(struct channel_data *chan);
292 static char *chrdev_setup_rx(struct channel_data *channel, int size);
293 static int chrdev_rx_done(struct channel_data *channel);
294 static int chrdev_tx_done(struct channel_data *channel, int size);
295 static long long cosa_lseek(struct file *file,
296 long long offset, int origin);
297 static ssize_t cosa_read(struct file *file,
298 char *buf, size_t count, loff_t *ppos);
299 static ssize_t cosa_write(struct file *file,
300 const char *buf, size_t count, loff_t *ppos);
301 static unsigned int cosa_poll(struct file *file, poll_table *poll);
302 static int cosa_open(struct inode *inode, struct file *file);
303 static int cosa_release(struct inode *inode, struct file *file);
304 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
305 unsigned int cmd, unsigned long arg);
306 #ifdef COSA_FASYNC_WORKING
307 static int cosa_fasync(struct inode *inode, struct file *file, int on);
308 #endif
310 static struct file_operations cosa_fops = {
311 cosa_lseek,
312 cosa_read,
313 cosa_write,
314 NULL, /* readdir */
315 cosa_poll,
316 cosa_chardev_ioctl,
317 NULL, /* mmap */
318 cosa_open,
319 NULL, /* flush */
320 cosa_release,
321 NULL, /* fsync */
322 #ifdef COSA_FASYNC_WORKING
323 cosa_fasync,
324 #else
325 NULL,
326 #endif
327 NULL, /* check media change */
328 NULL, /* revalidate */
329 NULL /* lock */
332 /* Ioctls */
333 static int cosa_start(struct cosa_data *cosa, int address);
334 static int cosa_reset(struct cosa_data *cosa);
335 static int cosa_download(struct cosa_data *cosa, struct cosa_download *d);
336 static int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d);
338 /* COSA/SRP ROM monitor */
339 static int download(struct cosa_data *cosa, char *data, int addr, int len);
340 static int startmicrocode(struct cosa_data *cosa, int address);
341 static int readmem(struct cosa_data *cosa, char *data, int addr, int len);
342 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
344 /* Auxilliary functions */
345 static int get_wait_data(struct cosa_data *cosa);
346 static int put_wait_data(struct cosa_data *cosa, int data);
347 static int puthexnumber(struct cosa_data *cosa, int number);
348 static void put_driver_status(struct cosa_data *cosa);
349 static void put_driver_status_nolock(struct cosa_data *cosa);
351 /* Interrupt handling */
352 static void cosa_interrupt(int irq, void *cosa, struct pt_regs *regs);
354 /* I/O ops debugging */
355 #ifdef DEBUG_IO
356 static void debug_data_in(struct cosa_data *cosa, int data);
357 static void debug_data_out(struct cosa_data *cosa, int data);
358 static void debug_data_cmd(struct cosa_data *cosa, int data);
359 static void debug_status_in(struct cosa_data *cosa, int status);
360 static void debug_status_out(struct cosa_data *cosa, int status);
361 #endif
364 /* ---------- Initialization stuff ---------- */
366 #ifdef MODULE
367 int init_module(void)
368 #else
369 static int __init cosa_init(void)
370 #endif
372 int i;
373 printk(KERN_INFO "cosa v1.06 (c) 1997-8 Jan Kasprzak <kas@fi.muni.cz>\n");
374 #ifdef __SMP__
375 printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
376 #endif
377 if (cosa_major > 0) {
378 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
379 printk(KERN_WARNING "cosa: unable to get major %d\n",
380 cosa_major);
381 return -EIO;
383 } else {
384 if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
385 printk(KERN_WARNING "cosa: unable to register chardev\n");
386 return -EIO;
389 for (i=0; i<MAX_CARDS; i++)
390 cosa_cards[i].num = -1;
391 for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
392 cosa_probe(io[i], irq[i], dma[i]);
393 if (!nr_cards) {
394 printk(KERN_WARNING "cosa: no devices found.\n");
395 unregister_chrdev(cosa_major, "cosa");
396 return -ENODEV;
398 return 0;
401 #ifdef MODULE
402 void cleanup_module (void)
404 struct cosa_data *cosa;
405 printk(KERN_INFO "Unloading the cosa module\n");
407 for (cosa=cosa_cards; nr_cards--; cosa++) {
408 int i;
409 /* Clean up the per-channel data */
410 for (i=0; i<cosa->nchannels; i++) {
411 /* Chardev driver has no alloc'd per-channel data */
412 sppp_channel_delete(cosa->chan+i);
414 /* Clean up the per-card data */
415 kfree(cosa->chan);
416 kfree(cosa->bouncebuf);
417 free_irq(cosa->irq, cosa);
418 free_dma(cosa->dma);
419 release_region(cosa->datareg,is_8bit(cosa)?2:4);
421 unregister_chrdev(cosa_major, "cosa");
423 #endif
426 * This function should register all the net devices needed for the
427 * single channel.
429 static __inline__ void channel_init(struct channel_data *chan)
431 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
433 /* Initialize the chardev data structures */
434 chardev_channel_init(chan);
436 /* Register the sppp interface */
437 sppp_channel_init(chan);
440 static int cosa_probe(int base, int irq, int dma)
442 struct cosa_data *cosa = cosa_cards+nr_cards;
443 int i;
445 memset(cosa, 0, sizeof(struct cosa_data));
447 /* Checking validity of parameters: */
448 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
449 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
450 printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
451 return -1;
453 /* I/O address should be between 0x100 and 0x3ff and should be
454 * multiple of 8. */
455 if (base < 0x100 || base > 0x3ff || base & 0x7) {
456 printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
457 base);
458 return -1;
460 /* DMA should be 0,1 or 3-7 */
461 if (dma < 0 || dma == 4 || dma > 7) {
462 printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
463 return -1;
465 /* and finally, on 16-bit COSA DMA should be 4-7 and
466 * I/O base should not be multiple of 0x10 */
467 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
468 printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
469 " (base=0x%x, dma=%d)\n", base, dma);
470 return -1;
473 cosa->dma = dma;
474 cosa->datareg = base;
475 cosa->statusreg = is_8bit(cosa)?base+1:base+2;
476 spin_lock_init(&cosa->lock);
478 if (check_region(base, is_8bit(cosa)?2:4))
479 return -1;
481 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
482 printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
483 return -1;
486 /* Test the validity of identification string */
487 if (!strncmp(cosa->id_string, "SRP", 3))
488 cosa->type = "srp";
489 else if (!strncmp(cosa->id_string, "COSA", 4))
490 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
491 else {
492 /* Print a warning only if we are not autoprobing */
493 #ifndef COSA_ISA_AUTOPROBE
494 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
495 base);
496 #endif
497 return -1;
500 /* Now do IRQ autoprobe */
501 if (irq < 0) {
502 unsigned long irqs;
503 /* printk(KERN_INFO "IRQ autoprobe\n"); */
504 sti();
505 irqs = probe_irq_on();
507 * Enable interrupt on tx buffer empty (it sure is)
508 * really sure ?
509 * FIXME: When this code is not used as module, we should
510 * probably call udelay() instead of the interruptible sleep.
512 current->state = TASK_INTERRUPTIBLE;
513 cosa_putstatus(cosa, SR_TX_INT_ENA);
514 schedule_timeout(30);
515 current->state = TASK_RUNNING;
516 irq = probe_irq_off(irqs);
517 /* Disable all IRQs from the card */
518 cosa_putstatus(cosa, 0);
519 /* Empty the received data register */
520 cosa_getdata8(cosa);
522 if (irq < 0) {
523 printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
524 irq, cosa->datareg);
525 return -1;
527 if (irq == 0) {
528 printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
529 cosa->datareg);
530 /* return -1; */
534 cosa->irq = irq;
535 cosa->num = nr_cards;
536 cosa->usage = 0;
537 cosa->nchannels = 2; /* FIXME: how to determine this? */
539 request_region(base, is_8bit(cosa)?2:4, cosa->type);
540 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa))
541 goto bad1;
542 if (request_dma(cosa->dma, cosa->type)) {
543 free_irq(cosa->irq, cosa);
544 bad1: release_region(cosa->datareg,is_8bit(cosa)?2:4);
545 printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
546 cosa->num);
547 return -1;
550 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
551 sprintf(cosa->name, "cosa%d", cosa->num);
553 /* Initialize the per-channel data */
554 cosa->chan = kmalloc(sizeof(struct channel_data)*cosa->nchannels,
555 GFP_KERNEL);
556 memset(cosa->chan, 0, sizeof(struct channel_data)*cosa->nchannels);
557 for (i=0; i<cosa->nchannels; i++) {
558 cosa->chan[i].cosa = cosa;
559 cosa->chan[i].num = i;
560 channel_init(cosa->chan+i);
563 printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
564 cosa->num, cosa->id_string, cosa->type,
565 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
567 return nr_cards++;
571 /*---------- SPPP/HDLC netdevice ---------- */
573 static void sppp_channel_init(struct channel_data *chan)
575 struct net_device *d;
576 sppp_attach(&chan->pppdev);
577 d=&chan->pppdev.dev;
578 d->name = chan->name;
579 d->base_addr = chan->cosa->datareg;
580 d->irq = chan->cosa->irq;
581 d->dma = chan->cosa->dma;
582 d->priv = chan;
583 d->init = NULL;
584 d->open = cosa_sppp_open;
585 d->stop = cosa_sppp_close;
586 d->hard_start_xmit = cosa_sppp_tx;
587 d->do_ioctl = cosa_sppp_ioctl;
588 d->get_stats = cosa_net_stats;
589 dev_init_buffers(d);
590 if (register_netdev(d) == -1) {
591 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
592 sppp_detach(&chan->pppdev.dev);
593 return;
597 static void sppp_channel_delete(struct channel_data *chan)
599 sppp_detach(&chan->pppdev.dev);
600 unregister_netdev(&chan->pppdev.dev);
604 static int cosa_sppp_open(struct net_device *d)
606 struct channel_data *chan = d->priv;
607 int err, flags;
609 spin_lock_irqsave(&chan->cosa->lock, flags);
610 if (chan->usage != 0) {
611 printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
612 chan->name, chan->usage);
613 spin_unlock_irqrestore(&chan->cosa->lock, flags);
614 return -EBUSY;
616 chan->setup_rx = sppp_setup_rx;
617 chan->tx_done = sppp_tx_done;
618 chan->rx_done = sppp_rx_done;
619 chan->usage=-1;
620 chan->cosa->usage++;
621 MOD_INC_USE_COUNT;
622 spin_unlock_irqrestore(&chan->cosa->lock, flags);
624 err = sppp_open(d);
625 if (err) {
626 spin_lock_irqsave(&chan->cosa->lock, flags);
627 chan->usage=0;
628 chan->cosa->usage--;
629 MOD_DEC_USE_COUNT;
631 spin_unlock_irqrestore(&chan->cosa->lock, flags);
632 return err;
635 d->tbusy = 0;
636 cosa_enable_rx(chan);
637 return 0;
640 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *dev)
642 struct channel_data *chan = dev->priv;
644 if (dev->tbusy) {
645 if (time_before(jiffies, dev->trans_start+2*HZ))
646 return 1; /* Two seconds timeout */
647 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
648 chan->stats.rx_errors++;
649 chan->stats.rx_missed_errors++;
650 } else {
651 chan->stats.tx_errors++;
652 chan->stats.tx_aborted_errors++;
654 cosa_kick(chan->cosa);
655 if (chan->tx_skb) {
656 dev_kfree_skb(chan->tx_skb);
657 chan->tx_skb = 0;
659 dev->tbusy = 0;
661 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
662 printk(KERN_WARNING "%s: Transmitter access conflict.\n", dev->name);
663 return 1;
666 chan->tx_skb = skb;
667 dev->trans_start = jiffies;
668 cosa_start_tx(chan, skb->data, skb->len);
669 return 0;
672 static int cosa_sppp_close(struct net_device *d)
674 struct channel_data *chan = d->priv;
675 int flags;
677 sppp_close(d);
678 d->tbusy = 1;
679 cosa_disable_rx(chan);
680 spin_lock_irqsave(&chan->cosa->lock, flags);
681 if (chan->rx_skb) {
682 kfree_skb(chan->rx_skb);
683 chan->rx_skb = 0;
685 if (chan->tx_skb) {
686 kfree_skb(chan->tx_skb);
687 chan->tx_skb = 0;
689 chan->usage=0;
690 chan->cosa->usage--;
691 MOD_DEC_USE_COUNT;
692 spin_unlock_irqrestore(&chan->cosa->lock, flags);
693 return 0;
696 static char *sppp_setup_rx(struct channel_data *chan, int size)
699 * We can safely fall back to non-dma-able memory, because we have
700 * the cosa->bouncebuf pre-allocated.
702 if (chan->rx_skb)
703 kfree_skb(chan->rx_skb);
704 chan->rx_skb = dev_alloc_skb(size);
705 if (chan->rx_skb == NULL) {
706 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
707 chan->name);
708 chan->stats.rx_dropped++;
709 return NULL;
711 chan->pppdev.dev.trans_start = jiffies;
712 return skb_put(chan->rx_skb, size);
715 static int sppp_rx_done(struct channel_data *chan)
717 if (!chan->rx_skb) {
718 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
719 chan->name);
720 chan->stats.rx_errors++;
721 chan->stats.rx_frame_errors++;
722 return 0;
724 chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
725 chan->rx_skb->dev = &chan->pppdev.dev;
726 chan->rx_skb->mac.raw = chan->rx_skb->data;
727 chan->stats.rx_packets++;
728 chan->stats.rx_bytes += chan->cosa->rxsize;
729 netif_rx(chan->rx_skb);
730 chan->rx_skb = 0;
731 chan->pppdev.dev.trans_start = jiffies;
732 return 0;
735 /* ARGSUSED */
736 static int sppp_tx_done(struct channel_data *chan, int size)
738 if (!chan->tx_skb) {
739 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
740 chan->name);
741 chan->stats.tx_errors++;
742 chan->stats.tx_aborted_errors++;
743 return 1;
745 dev_kfree_skb(chan->tx_skb);
746 chan->tx_skb = 0;
747 chan->stats.tx_packets++;
748 chan->stats.tx_bytes += size;
749 chan->pppdev.dev.tbusy = 0;
750 mark_bh(NET_BH);
751 return 1;
754 static struct net_device_stats *cosa_net_stats(struct net_device *dev)
756 struct channel_data *chan = dev->priv;
757 return &chan->stats;
761 /*---------- Character device ---------- */
763 static void chardev_channel_init(struct channel_data *chan)
765 init_MUTEX(&chan->rsem);
766 init_MUTEX(&chan->wsem);
769 static long long cosa_lseek(struct file * file,
770 long long offset, int origin)
772 return -ESPIPE;
775 static ssize_t cosa_read(struct file *file,
776 char *buf, size_t count, loff_t *ppos)
778 DECLARE_WAITQUEUE(wait, current);
779 int flags;
780 struct channel_data *chan = (struct channel_data *)file->private_data;
781 struct cosa_data *cosa = chan->cosa;
782 char *kbuf;
784 if (down_interruptible(&chan->rsem))
785 return -ERESTARTSYS;
787 if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
788 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
789 up(&chan->rsem);
790 return -ENOMEM;
793 chan->rx_status = 0;
794 cosa_enable_rx(chan);
795 spin_lock_irqsave(&cosa->lock, flags);
796 add_wait_queue(&chan->rxwaitq, &wait);
797 while(!chan->rx_status) {
798 current->state = TASK_INTERRUPTIBLE;
799 spin_unlock_irqrestore(&cosa->lock, flags);
800 schedule();
801 spin_lock_irqsave(&cosa->lock, flags);
802 if (signal_pending(current) && chan->rx_status == 0) {
803 chan->rx_status = 1;
804 remove_wait_queue(&chan->rxwaitq, &wait);
805 current->state = TASK_RUNNING;
806 spin_unlock_irqrestore(&cosa->lock, flags);
807 up(&chan->rsem);
808 return -ERESTARTSYS;
811 remove_wait_queue(&chan->rxwaitq, &wait);
812 current->state = TASK_RUNNING;
813 kbuf = chan->rxdata;
814 count = chan->rxsize;
815 spin_unlock_irqrestore(&cosa->lock, flags);
816 up(&chan->rsem);
818 if (copy_to_user(buf, kbuf, count)) {
819 kfree(buf);
820 return -EFAULT;
822 kfree(kbuf);
823 return count;
826 static char *chrdev_setup_rx(struct channel_data *chan, int size)
828 /* Expect size <= COSA_MTU */
829 chan->rxsize = size;
830 return chan->rxdata;
833 static int chrdev_rx_done(struct channel_data *chan)
835 if (chan->rx_status) { /* Reader has died */
836 kfree(chan->rxdata);
837 up(&chan->wsem);
839 chan->rx_status = 1;
840 wake_up_interruptible(&chan->rxwaitq);
841 return 1;
845 static ssize_t cosa_write(struct file *file,
846 const char *buf, size_t count, loff_t *ppos)
848 struct channel_data *chan = (struct channel_data *)file->private_data;
849 DECLARE_WAITQUEUE(wait, current);
850 struct cosa_data *cosa = chan->cosa;
851 unsigned int flags;
852 char *kbuf;
854 if (down_interruptible(&chan->wsem))
855 return -ERESTARTSYS;
857 if (count > COSA_MTU)
858 count = COSA_MTU;
860 /* Allocate the buffer */
861 if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
862 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
863 cosa->name);
864 up(&chan->wsem);
865 return -ENOMEM;
867 if (copy_from_user(kbuf, buf, count)) {
868 up(&chan->wsem);
869 kfree(kbuf);
870 return -EFAULT;
872 chan->tx_status=0;
873 cosa_start_tx(chan, kbuf, count);
875 spin_lock_irqsave(&cosa->lock, flags);
876 add_wait_queue(&chan->txwaitq, &wait);
877 while(!chan->tx_status) {
878 current->state = TASK_INTERRUPTIBLE;
879 spin_unlock_irqrestore(&cosa->lock, flags);
880 schedule();
881 spin_lock_irqsave(&cosa->lock, flags);
882 if (signal_pending(current) && chan->tx_status == 0) {
883 chan->tx_status = 1;
884 remove_wait_queue(&chan->txwaitq, &wait);
885 current->state = TASK_RUNNING;
886 chan->tx_status = 1;
887 spin_unlock_irqrestore(&cosa->lock, flags);
888 return -ERESTARTSYS;
891 remove_wait_queue(&chan->txwaitq, &wait);
892 current->state = TASK_RUNNING;
893 up(&chan->wsem);
894 spin_unlock_irqrestore(&cosa->lock, flags);
895 kfree(kbuf);
896 return count;
899 static int chrdev_tx_done(struct channel_data *chan, int size)
901 if (chan->tx_status) { /* Writer was interrupted */
902 kfree(chan->txbuf);
903 up(&chan->wsem);
905 chan->tx_status = 1;
906 wake_up_interruptible(&chan->txwaitq);
907 return 1;
910 static unsigned int cosa_poll(struct file *file, poll_table *poll)
912 printk(KERN_INFO "cosa_poll is here\n");
913 return 0;
916 static int cosa_open(struct inode *inode, struct file *file)
918 struct cosa_data *cosa;
919 struct channel_data *chan;
920 unsigned long flags;
921 int n;
923 if ((n=MINOR(file->f_dentry->d_inode->i_rdev)>>CARD_MINOR_BITS)
924 >= nr_cards)
925 return -ENODEV;
926 cosa = cosa_cards+n;
928 if ((n=MINOR(file->f_dentry->d_inode->i_rdev)
929 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
930 return -ENODEV;
931 chan = cosa->chan + n;
933 file->private_data = chan;
935 spin_lock_irqsave(&cosa->lock, flags);
937 if (chan->usage < 0) { /* in netdev mode */
938 spin_unlock_irqrestore(&cosa->lock, flags);
939 return -EBUSY;
941 cosa->usage++;
942 chan->usage++;
944 chan->tx_done = chrdev_tx_done;
945 chan->setup_rx = chrdev_setup_rx;
946 chan->rx_done = chrdev_rx_done;
947 #ifdef MODULE
948 MOD_INC_USE_COUNT;
949 #endif
950 spin_unlock_irqrestore(&cosa->lock, flags);
951 return 0;
954 static int cosa_release(struct inode *inode, struct file *file)
956 struct channel_data *channel = (struct channel_data *)file->private_data;
957 struct cosa_data *cosa = channel->cosa;
958 unsigned long flags;
960 spin_lock_irqsave(&cosa->lock, flags);
961 cosa->usage--;
962 channel->usage--;
963 #ifdef MODULE
964 MOD_DEC_USE_COUNT;
965 #endif
966 spin_unlock_irqrestore(&cosa->lock, flags);
967 return 0;
970 #ifdef COSA_FASYNC_WORKING
971 static struct fasync_struct *fasync[256] = { NULL, };
973 /* To be done ... */
974 static int cosa_fasync(struct inode *inode, struct file *file, int on)
976 int port = MINOR(inode->i_rdev);
977 int rv = fasync_helper(inode, file, on, &fasync[port]);
978 return rv < 0 ? rv : 0;
980 #endif
983 /* ---------- Ioctls ---------- */
986 * Ioctl subroutines can safely be made inline, because they are called
987 * only from cosa_ioctl().
989 static inline int cosa_reset(struct cosa_data *cosa)
991 char idstring[COSA_MAX_ID_STRING];
992 if (cosa->usage > 1)
993 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
994 cosa->num, cosa->usage);
995 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
996 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
997 return -EIO;
999 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1000 idstring);
1001 return 0;
1004 /* High-level function to download data into COSA memory. Calls download() */
1005 static inline int cosa_download(struct cosa_data *cosa, struct cosa_download *d)
1007 int i;
1008 int addr, len;
1009 char *code;
1011 if (cosa->usage > 1)
1012 printk(KERN_INFO "cosa%d: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1013 cosa->num, cosa->usage);
1014 #if 0
1015 if (cosa->status != CARD_STATUS_RESETED && cosa->status != CARD_STATUS_DOWNLOADED) {
1016 printk(KERN_NOTICE "cosa%d: reset the card first (status %d).\n",
1017 cosa->num, cosa->status);
1018 return -EPERM;
1020 #endif
1021 get_user_ret(addr, &(d->addr), -EFAULT);
1022 get_user_ret(len, &(d->len), -EFAULT);
1023 get_user_ret(code, &(d->code), -EFAULT);
1025 if (d->addr < 0 || d->addr > COSA_MAX_FIRMWARE_SIZE)
1026 return -EINVAL;
1027 if (d->len < 0 || d->len > COSA_MAX_FIRMWARE_SIZE)
1028 return -EINVAL;
1030 if ((i=download(cosa, d->code, len, addr)) < 0) {
1031 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1032 cosa->num, i);
1033 return -EIO;
1035 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1036 cosa->num, len, addr);
1037 return 0;
1040 /* High-level function to read COSA memory. Calls readmem() */
1041 static inline int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d)
1043 int i;
1044 int addr, len;
1045 char *code;
1047 if (cosa->usage > 1)
1048 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1049 "cosa->usage > 1 (%d). Odd things may happen.\n",
1050 cosa->num, cosa->usage);
1051 #if 0
1052 if (cosa->status != CARD_STATUS_RESETED &&
1053 cosa->status != CARD_STATUS_DOWNLOADED) {
1054 printk(KERN_NOTICE "cosa%d: reset the card first (status %d).\n",
1055 cosa->num, cosa->status);
1056 return -EPERM;
1058 #endif
1059 get_user_ret(addr, &(d->addr), -EFAULT);
1060 get_user_ret(len, &(d->len), -EFAULT);
1061 get_user_ret(code, &(d->code), -EFAULT);
1063 if ((i=readmem(cosa, d->code, len, addr)) < 0) {
1064 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1065 cosa->num, i);
1066 return -EIO;
1068 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1069 cosa->num, len, addr);
1070 return 0;
1073 /* High-level function to start microcode. Calls startmicrocode(). */
1074 static inline int cosa_start(struct cosa_data *cosa, int address)
1076 int i;
1078 if (cosa->usage > 1)
1079 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1080 cosa->num, cosa->usage);
1081 #if 0
1082 if (cosa->status != CARD_STATUS_DOWNLOADED) {
1083 printk(KERN_NOTICE "cosa%d: download the microcode first (status %d).\n",
1084 cosa->num, cosa->status);
1085 return -EPERM;
1087 #endif
1088 if ((i=startmicrocode(cosa, address)) < 0) {
1089 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1090 cosa->num, address, i);
1091 return -EIO;
1093 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1094 cosa->num, address);
1095 cosa->startaddr = address;
1096 return 0;
1099 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1100 static inline int cosa_getidstr(struct cosa_data *cosa, char *string)
1102 int l = strlen(cosa->id_string)+1;
1103 copy_to_user_ret(string, cosa->id_string, l, -EFAULT);
1104 return l;
1107 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1108 static inline int cosa_gettype(struct cosa_data *cosa, char *string)
1110 int l = strlen(cosa->type)+1;
1111 copy_to_user_ret(string, cosa->type, l, -EFAULT);
1112 return l;
1115 static int cosa_ioctl_common(struct cosa_data *cosa,
1116 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1118 switch(cmd) {
1119 case COSAIORSET: /* Reset the device */
1120 if (!suser())
1121 return -EACCES;
1122 return cosa_reset(cosa);
1123 case COSAIOSTRT: /* Start the firmware */
1124 if (!suser())
1125 return -EACCES;
1126 return cosa_start(cosa, arg);
1127 case COSAIODOWNLD: /* Download the firmware */
1128 if (!suser())
1129 return -EACCES;
1130 return cosa_download(cosa, (struct cosa_download *)arg);
1131 case COSAIORMEM:
1132 if (!suser())
1133 return -EACCES;
1134 return cosa_readmem(cosa, (struct cosa_download *)arg);
1135 case COSAIORTYPE:
1136 return cosa_gettype(cosa, (char *)arg);
1137 case COSAIORIDSTR:
1138 return cosa_getidstr(cosa, (char *)arg);
1140 * These two are _very_ugly_hack_(tm). Don't even look at this.
1141 * Implementing this saved me few reboots after some process segfaulted
1142 * inside this module.
1144 #ifdef MODULE
1145 #if 0
1146 case COSAIOMINC:
1147 MOD_INC_USE_COUNT;
1148 return 0;
1149 case COSAIOMDEC:
1150 MOD_DEC_USE_COUNT;
1151 return 0;
1152 #endif
1153 #endif
1154 case COSAIONRCARDS:
1155 return nr_cards;
1156 case COSAIONRCHANS:
1157 return cosa->nchannels;
1158 case COSAIOBMSET:
1159 if (!suser())
1160 return -EACCES;
1161 if (is_8bit(cosa))
1162 return -EINVAL;
1163 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1164 return -EINVAL;
1165 cosa->busmaster = arg;
1166 return 0;
1167 case COSAIOBMGET:
1168 return cosa->busmaster;
1170 return -ENOIOCTLCMD;
1173 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
1174 int cmd)
1176 int rv;
1177 struct channel_data *chan = (struct channel_data *)dev->priv;
1178 rv = cosa_ioctl_common(chan->cosa, chan, cmd, (int)ifr->ifr_data);
1179 if (rv == -ENOIOCTLCMD) {
1180 return sppp_do_ioctl(dev, ifr, cmd);
1182 return rv;
1185 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1186 unsigned int cmd, unsigned long arg)
1188 struct channel_data *channel = (struct channel_data *)file->private_data;
1189 struct cosa_data *cosa = channel->cosa;
1190 return cosa_ioctl_common(cosa, channel, cmd, arg);
1194 /*---------- HW layer interface ---------- */
1197 * The higher layer can bind itself to the HW layer by setting the callbacks
1198 * in the channel_data structure and by using these routines.
1200 static void cosa_enable_rx(struct channel_data *chan)
1202 struct cosa_data *cosa = chan->cosa;
1204 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1205 put_driver_status(cosa);
1208 static void cosa_disable_rx(struct channel_data *chan)
1210 struct cosa_data *cosa = chan->cosa;
1212 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1213 put_driver_status(cosa);
1217 * FIXME: This routine probably should check for cosa_start_tx() called when
1218 * the previous transmit is still unfinished. In this case the non-zero
1219 * return value should indicate to the caller that the queuing(sp?) up
1220 * the transmit has failed.
1222 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1224 struct cosa_data *cosa = chan->cosa;
1225 int flags;
1226 #ifdef DEBUG_DATA
1227 int i;
1229 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1230 chan->num, len);
1231 for (i=0; i<len; i++)
1232 printk(" %02x", buf[i]&0xff);
1233 printk("\n");
1234 #endif
1235 spin_lock_irqsave(&cosa->lock, flags);
1236 chan->txbuf = buf;
1237 chan->txsize = len;
1238 if (len > COSA_MTU)
1239 chan->txsize = COSA_MTU;
1240 spin_unlock_irqrestore(&cosa->lock, flags);
1242 /* Tell the firmware we are ready */
1243 set_bit(chan->num, &cosa->txbitmap);
1244 put_driver_status(cosa);
1246 return 0;
1249 static void put_driver_status(struct cosa_data *cosa)
1251 unsigned flags=0;
1252 int status;
1254 spin_lock_irqsave(&cosa->lock, flags);
1256 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1257 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1258 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1259 &DRIVER_TXMAP_MASK : 0);
1260 if (!cosa->rxtx) {
1261 if (cosa->rxbitmap|cosa->txbitmap) {
1262 if (!cosa->enabled) {
1263 cosa_putstatus(cosa, SR_RX_INT_ENA);
1264 #ifdef DEBUG_IO
1265 debug_status_out(cosa, SR_RX_INT_ENA);
1266 #endif
1267 cosa->enabled = 1;
1269 } else if (cosa->enabled) {
1270 cosa->enabled = 0;
1271 cosa_putstatus(cosa, 0);
1272 #ifdef DEBUG_IO
1273 debug_status_out(cosa, 0);
1274 #endif
1276 cosa_putdata8(cosa, status);
1277 #ifdef DEBUG_IO
1278 debug_data_cmd(cosa, status);
1279 #endif
1281 spin_unlock_irqrestore(&cosa->lock, flags);
1284 static void put_driver_status_nolock(struct cosa_data *cosa)
1286 int status;
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);
1293 if (cosa->rxbitmap|cosa->txbitmap) {
1294 cosa_putstatus(cosa, SR_RX_INT_ENA);
1295 #ifdef DEBUG_IO
1296 debug_status_out(cosa, SR_RX_INT_ENA);
1297 #endif
1298 cosa->enabled = 1;
1299 } else {
1300 cosa_putstatus(cosa, 0);
1301 #ifdef DEBUG_IO
1302 debug_status_out(cosa, 0);
1303 #endif
1304 cosa->enabled = 0;
1306 cosa_putdata8(cosa, status);
1307 #ifdef DEBUG_IO
1308 debug_data_cmd(cosa, status);
1309 #endif
1313 * The "kickme" function: When the DMA times out, this is called to
1314 * clean up the driver status.
1315 * FIXME: Preliminary support, the interface is probably wrong.
1317 static void cosa_kick(struct cosa_data *cosa)
1319 unsigned flags, flags1;
1320 char *s = "Unknown";
1322 if (test_bit(RXBIT, &cosa->rxtx))
1323 s = "RX";
1324 if (test_bit(TXBIT, &cosa->rxtx))
1325 s = "TX";
1327 printk(KERN_INFO "%s: %s DMA timeout - restarting.\n", cosa->name, s);
1328 spin_lock_irqsave(&cosa->lock, flags);
1329 cosa->rxtx = 0;
1331 flags1 = claim_dma_lock();
1332 disable_dma(cosa->dma);
1333 clear_dma_ff(cosa->dma);
1334 release_dma_lock(flags1);
1336 /* FIXME: Anything else? */
1337 udelay(100);
1338 cosa_putstatus(cosa, 0);
1339 udelay(100);
1340 (void) cosa_getdata8(cosa);
1341 udelay(100);
1342 cosa_putdata8(cosa, 0);
1343 udelay(100);
1344 put_driver_status_nolock(cosa);
1345 spin_unlock_irqrestore(&cosa->lock, flags);
1349 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1350 * physical memory and doesn't span the 64k boundary. For now it seems
1351 * SKB's never do this, but we'll check this anyway.
1353 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1355 static int count = 0;
1356 unsigned long b = (unsigned long)buf;
1357 if (b+len >= MAX_DMA_ADDRESS)
1358 return 0;
1359 if ((b^ (b+len)) & 0x10000) {
1360 if (count++ < 5)
1361 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1362 chan->name);
1363 return 0;
1365 return 1;
1369 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1372 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1373 * drivers need to say 4-digit hex number meaning start address of the microcode
1374 * separated by a single space. Monitor replies by saying " =". Now driver
1375 * has to write 4-digit hex number meaning the last byte address ended
1376 * by a single space. Monitor has to reply with a space. Now the download
1377 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1379 static int download(struct cosa_data *cosa, char *microcode, int length, int address)
1381 int i;
1383 if (put_wait_data(cosa, 'w') == -1) return -1;
1384 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1385 if (get_wait_data(cosa) != '=') return -3;
1387 if (puthexnumber(cosa, address) < 0) return -4;
1388 if (put_wait_data(cosa, ' ') == -1) return -10;
1389 if (get_wait_data(cosa) != ' ') return -11;
1390 if (get_wait_data(cosa) != '=') return -12;
1392 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1393 if (put_wait_data(cosa, ' ') == -1) return -18;
1394 if (get_wait_data(cosa) != ' ') return -19;
1396 while (length--) {
1397 char c;
1398 #ifndef SRP_DOWNLOAD_AT_BOOT
1399 get_user_ret(c,microcode, -23);
1400 #else
1401 c = *microcode;
1402 #endif
1403 if (put_wait_data(cosa, c) == -1)
1404 return -20;
1405 microcode++;
1408 if (get_wait_data(cosa) != '\r') return -21;
1409 if (get_wait_data(cosa) != '\n') return -22;
1410 if (get_wait_data(cosa) != '.') return -23;
1411 #if 0
1412 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1413 #endif
1414 return 0;
1419 * Starting microcode is done via the "g" command of the SRP monitor.
1420 * The chat should be the following: "g" "g=" "<addr><CR>"
1421 * "<CR><CR><LF><CR><LF>".
1423 static int startmicrocode(struct cosa_data *cosa, int address)
1425 if (put_wait_data(cosa, 'g') == -1) return -1;
1426 if (get_wait_data(cosa) != 'g') return -2;
1427 if (get_wait_data(cosa) != '=') return -3;
1429 if (puthexnumber(cosa, address) < 0) return -4;
1430 if (put_wait_data(cosa, '\r') == -1) return -5;
1432 if (get_wait_data(cosa) != '\r') return -6;
1433 if (get_wait_data(cosa) != '\r') return -7;
1434 if (get_wait_data(cosa) != '\n') return -8;
1435 if (get_wait_data(cosa) != '\r') return -9;
1436 if (get_wait_data(cosa) != '\n') return -10;
1437 #if 0
1438 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1439 #endif
1440 return 0;
1444 * Reading memory is done via the "r" command of the SRP monitor.
1445 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1446 * Then driver can read the data and the conversation is finished
1447 * by SRP monitor sending "<CR><LF>." (dot at the end).
1449 * This routine is not needed during the normal operation and serves
1450 * for debugging purposes only.
1452 static int readmem(struct cosa_data *cosa, char *microcode, int length, int address)
1454 if (put_wait_data(cosa, 'r') == -1) return -1;
1455 if ((get_wait_data(cosa)) != 'r') return -2;
1456 if ((get_wait_data(cosa)) != '=') return -3;
1458 if (puthexnumber(cosa, address) < 0) return -4;
1459 if (put_wait_data(cosa, ' ') == -1) return -5;
1460 if (get_wait_data(cosa) != ' ') return -6;
1461 if (get_wait_data(cosa) != '=') return -7;
1463 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1464 if (put_wait_data(cosa, ' ') == -1) return -9;
1465 if (get_wait_data(cosa) != ' ') return -10;
1467 while (length--) {
1468 char c;
1469 int i;
1470 if ((i=get_wait_data(cosa)) == -1) {
1471 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1472 length);
1473 return -11;
1475 c=i;
1476 #if 1
1477 put_user_ret(c,microcode, -23);
1478 #else
1479 *microcode = c;
1480 #endif
1481 microcode++;
1484 if (get_wait_data(cosa) != '\r') return -21;
1485 if (get_wait_data(cosa) != '\n') return -22;
1486 if (get_wait_data(cosa) != '.') return -23;
1487 #if 0
1488 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1489 #endif
1490 return 0;
1494 * This function resets the device and reads the initial prompt
1495 * of the device's ROM monitor.
1497 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1499 int i=0, id=0, prev=0, curr=0;
1501 /* Reset the card ... */
1502 cosa_putstatus(cosa, 0);
1503 cosa_getdata8(cosa);
1504 cosa_putstatus(cosa, SR_RST);
1505 #ifdef MODULE
1506 current->state = TASK_INTERRUPTIBLE;
1507 schedule_timeout(HZ/2);
1508 current->state = TASK_RUNNING;
1509 #else
1510 udelay(5*100000);
1511 #endif
1512 /* Disable all IRQs from the card */
1513 cosa_putstatus(cosa, 0);
1516 * Try to read the ID string. The card then prints out the
1517 * identification string ended by the "\n\x2e".
1519 * The following loop is indexed through i (instead of id)
1520 * to avoid looping forever when for any reason
1521 * the port returns '\r', '\n' or '\x2e' permanently.
1523 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1524 if ((curr = get_wait_data(cosa)) == -1) {
1525 return -1;
1527 curr &= 0xff;
1528 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1529 idstring[id++] = curr;
1530 if (curr == 0x2e && prev == '\n')
1531 break;
1533 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1534 idstring[id] = '\0';
1535 return id;
1539 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1542 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1543 * bit to be set in a loop. It should be used in the exceptional cases
1544 * only (for example when resetting the card or downloading the firmware.
1546 static int get_wait_data(struct cosa_data *cosa)
1548 int retries = 1000;
1550 while (--retries) {
1551 /* read data and return them */
1552 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1553 short r;
1554 r = cosa_getdata8(cosa);
1555 #if 0
1556 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1557 #endif
1558 return r;
1560 /* sleep if not ready to read */
1561 current->state = TASK_INTERRUPTIBLE;
1562 schedule_timeout(1);
1564 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1565 cosa_getstatus(cosa));
1566 return -1;
1570 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1571 * bit to be set in a loop. It should be used in the exceptional cases
1572 * only (for example when resetting the card or downloading the firmware).
1574 static int put_wait_data(struct cosa_data *cosa, int data)
1576 int retries = 1000;
1577 while (--retries) {
1578 /* read data and return them */
1579 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1580 cosa_putdata8(cosa, data);
1581 #if 0
1582 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1583 #endif
1584 return 0;
1586 #if 0
1587 /* sleep if not ready to read */
1588 current->state = TASK_INTERRUPTIBLE;
1589 schedule_timeout(1);
1590 #endif
1592 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1593 cosa->num, cosa_getstatus(cosa));
1594 return -1;
1598 * The following routine puts the hexadecimal number into the SRP monitor
1599 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1600 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1601 * (-2,-4,-6,-8) means that reading echo failed.
1603 static int puthexnumber(struct cosa_data *cosa, int number)
1605 char temp[5];
1606 int i;
1608 /* Well, I should probably replace this by something faster. */
1609 sprintf(temp, "%04X", number);
1610 for (i=0; i<4; i++) {
1611 if (put_wait_data(cosa, temp[i]) == -1) {
1612 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1613 cosa->num, i);
1614 return -1-2*i;
1616 if (get_wait_data(cosa) != temp[i]) {
1617 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1618 cosa->num, i);
1619 return -2-2*i;
1622 return 0;
1626 /* ---------- Interrupt routines ---------- */
1629 * There are three types of interrupt:
1630 * At the beginning of transmit - this handled is in tx_interrupt(),
1631 * at the beginning of receive - it is in rx_interrupt() and
1632 * at the end of transmit/receive - it is the eot_interrupt() function.
1633 * These functions are multiplexed by cosa_interrupt() according to the
1634 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1635 * separate functions to make it more readable. These functions are inline,
1636 * so there should be no overhead of function call.
1638 * In the COSA bus-master mode, we need to tell the card the address of a
1639 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1640 * It's time to use the bottom half :-(
1644 * Transmit interrupt routine - called when COSA is willing to obtain
1645 * data from the OS. The most tricky part of the routine is selection
1646 * of channel we (OS) want to send packet for. For SRP we should probably
1647 * use the round-robin approach. The newer COSA firmwares have a simple
1648 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1649 * channel 0 or 1 doesn't want to receive data.
1651 * It seems there is a bug in COSA firmware (need to trace it further):
1652 * When the driver status says that the kernel has no more data for transmit
1653 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1654 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1655 * the TX interrupt but does not mark the channel as ready-to-transmit.
1656 * The fix seems to be to push the packet to COSA despite its request.
1657 * We first try to obey the card's opinion, and then fall back to forced TX.
1659 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1661 unsigned long flags, flags1;
1662 #ifdef DEBUG_IRQS
1663 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1664 cosa->num, status);
1665 #endif
1666 spin_lock_irqsave(&cosa->lock, flags);
1667 set_bit(TXBIT, &cosa->rxtx);
1668 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1669 /* flow control, see the comment above */
1670 int i=0;
1671 if (!cosa->txbitmap) {
1672 printk(KERN_WARNING "%s: No channel wants data "
1673 "in TX IRQ. Expect DMA timeout.",
1674 cosa->name);
1675 put_driver_status_nolock(cosa);
1676 clear_bit(TXBIT, &cosa->rxtx);
1677 spin_unlock_irqrestore(&cosa->lock, flags);
1678 return;
1680 while(1) {
1681 cosa->txchan++;
1682 i++;
1683 if (cosa->txchan >= cosa->nchannels)
1684 cosa->txchan = 0;
1685 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1686 continue;
1687 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1688 break;
1689 /* in second pass, accept first ready-to-TX channel */
1690 if (i > cosa->nchannels) {
1691 /* Can be safely ignored */
1692 printk(KERN_DEBUG "%s: Forcing TX "
1693 "to not-ready channel %d\n",
1694 cosa->name, cosa->txchan);
1695 break;
1699 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1700 if (cosa_dma_able(cosa->chan+cosa->txchan,
1701 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1702 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1703 } else {
1704 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1705 cosa->txsize);
1706 cosa->txbuf = cosa->bouncebuf;
1710 if (is_8bit(cosa)) {
1711 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1712 cosa_putstatus(cosa, SR_TX_INT_ENA);
1713 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1714 ((cosa->txsize >> 8) & 0x1f));
1715 #ifdef DEBUG_IO
1716 debug_status_out(cosa, SR_TX_INT_ENA);
1717 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1718 ((cosa->txsize >> 8) & 0x1f));
1719 debug_data_in(cosa, cosa_getdata8(cosa));
1720 #else
1721 cosa_getdata8(cosa);
1722 #endif
1723 set_bit(IRQBIT, &cosa->rxtx);
1724 spin_unlock_irqrestore(&cosa->lock, flags);
1725 return;
1726 } else {
1727 clear_bit(IRQBIT, &cosa->rxtx);
1728 cosa_putstatus(cosa, 0);
1729 cosa_putdata8(cosa, cosa->txsize&0xff);
1730 #ifdef DEBUG_IO
1731 debug_status_out(cosa, 0);
1732 debug_data_out(cosa, cosa->txsize&0xff);
1733 #endif
1735 } else {
1736 cosa_putstatus(cosa, SR_TX_INT_ENA);
1737 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1738 | (cosa->txsize & 0x1fff));
1739 #ifdef DEBUG_IO
1740 debug_status_out(cosa, SR_TX_INT_ENA);
1741 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1742 | (cosa->txsize & 0x1fff));
1743 debug_data_in(cosa, cosa_getdata8(cosa));
1744 debug_status_out(cosa, 0);
1745 #else
1746 cosa_getdata8(cosa);
1747 #endif
1748 cosa_putstatus(cosa, 0);
1751 if (cosa->busmaster) {
1752 unsigned long addr = virt_to_bus(cosa->txbuf);
1753 int count=0;
1754 printk(KERN_INFO "busmaster IRQ\n");
1755 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1756 count++;
1757 udelay(10);
1758 if (count > 1000) break;
1760 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1761 printk(KERN_INFO "ready after %d loops\n", count);
1762 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1764 count = 0;
1765 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1766 count++;
1767 if (count > 1000) break;
1768 udelay(10);
1770 printk(KERN_INFO "ready after %d loops\n", count);
1771 cosa_putdata16(cosa, addr &0xffff);
1772 flags1 = claim_dma_lock();
1773 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1774 enable_dma(cosa->dma);
1775 release_dma_lock(flags1);
1776 } else {
1777 /* start the DMA */
1778 flags1 = claim_dma_lock();
1779 disable_dma(cosa->dma);
1780 clear_dma_ff(cosa->dma);
1781 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1782 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1783 set_dma_count(cosa->dma, cosa->txsize);
1784 enable_dma(cosa->dma);
1785 release_dma_lock(flags1);
1787 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1788 #ifdef DEBUG_IO
1789 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1790 #endif
1791 spin_unlock_irqrestore(&cosa->lock, flags);
1794 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1796 unsigned long flags;
1797 #ifdef DEBUG_IRQS
1798 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1799 #endif
1801 spin_lock_irqsave(&cosa->lock, flags);
1802 set_bit(RXBIT, &cosa->rxtx);
1804 if (is_8bit(cosa)) {
1805 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1806 set_bit(IRQBIT, &cosa->rxtx);
1807 put_driver_status_nolock(cosa);
1808 cosa->rxsize = cosa_getdata8(cosa) <<8;
1809 #ifdef DEBUG_IO
1810 debug_data_in(cosa, cosa->rxsize >> 8);
1811 #endif
1812 spin_unlock_irqrestore(&cosa->lock, flags);
1813 return;
1814 } else {
1815 clear_bit(IRQBIT, &cosa->rxtx);
1816 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1817 #ifdef DEBUG_IO
1818 debug_data_in(cosa, cosa->rxsize & 0xff);
1819 #endif
1820 #if 0
1821 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1822 cosa->num, cosa->rxsize);
1823 #endif
1825 } else {
1826 cosa->rxsize = cosa_getdata16(cosa);
1827 #ifdef DEBUG_IO
1828 debug_data_in(cosa, cosa->rxsize);
1829 #endif
1830 #if 0
1831 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1832 cosa->num, cosa->rxsize);
1833 #endif
1835 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1836 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1837 cosa->name, cosa->rxsize);
1838 spin_unlock_irqrestore(&cosa->lock, flags);
1839 goto reject;
1841 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1842 cosa->rxsize &= 0x1fff;
1843 spin_unlock_irqrestore(&cosa->lock, flags);
1845 cosa->rxbuf = NULL;
1846 if (cosa->rxchan->setup_rx)
1847 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1849 if (!cosa->rxbuf) {
1850 reject: /* Reject the packet */
1851 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1852 cosa->num, cosa->rxchan->num);
1853 cosa->rxbuf = cosa->bouncebuf;
1856 /* start the DMA */
1857 flags = claim_dma_lock();
1858 disable_dma(cosa->dma);
1859 clear_dma_ff(cosa->dma);
1860 set_dma_mode(cosa->dma, DMA_MODE_READ);
1861 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1862 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1863 } else {
1864 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1866 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1867 enable_dma(cosa->dma);
1868 release_dma_lock(flags);
1869 spin_lock_irqsave(&cosa->lock, flags);
1870 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1871 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1872 cosa_putdata8(cosa, DRIVER_RX_READY);
1873 #ifdef DEBUG_IO
1874 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1875 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1876 debug_data_cmd(cosa, DRIVER_RX_READY);
1877 #endif
1878 spin_unlock_irqrestore(&cosa->lock, flags);
1881 static void inline eot_interrupt(struct cosa_data *cosa, int status)
1883 unsigned long flags, flags1;
1884 spin_lock_irqsave(&cosa->lock, flags);
1885 flags1 = claim_dma_lock();
1886 disable_dma(cosa->dma);
1887 clear_dma_ff(cosa->dma);
1888 release_dma_lock(flags1);
1889 if (test_bit(TXBIT, &cosa->rxtx)) {
1890 struct channel_data *chan = cosa->chan+cosa->txchan;
1891 if (chan->tx_done)
1892 if (chan->tx_done(chan, cosa->txsize))
1893 clear_bit(chan->num, &cosa->txbitmap);
1894 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1895 #ifdef DEBUG_DATA
1897 int i;
1898 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1899 cosa->rxchan->num, cosa->rxsize);
1900 for (i=0; i<cosa->rxsize; i++)
1901 printk (" %02x", cosa->rxbuf[i]&0xff);
1902 printk("\n");
1904 #endif
1905 /* Packet for unknown channel? */
1906 if (cosa->rxbuf == cosa->bouncebuf)
1907 goto out;
1908 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1909 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1910 if (cosa->rxchan->rx_done)
1911 if (cosa->rxchan->rx_done(cosa->rxchan))
1912 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1913 } else {
1914 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1915 cosa->num);
1918 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1919 * cleared anyway). We should do it as soon as possible
1920 * so that we can tell the COSA we are done and to give it a time
1921 * for recovery.
1923 out:
1924 cosa->rxtx = 0;
1925 put_driver_status_nolock(cosa);
1926 spin_unlock_irqrestore(&cosa->lock, flags);
1929 static void cosa_interrupt(int irq, void *cosa_, struct pt_regs *regs)
1931 unsigned status;
1932 int count = 0;
1933 struct cosa_data *cosa = cosa_;
1934 again:
1935 status = cosa_getstatus(cosa);
1936 #ifdef DEBUG_IRQS
1937 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1938 status & 0xff);
1939 #endif
1940 #ifdef DEBUG_IO
1941 debug_status_in(cosa, status);
1942 #endif
1943 switch (status & SR_CMD_FROM_SRP_MASK) {
1944 case SR_DOWN_REQUEST:
1945 tx_interrupt(cosa, status);
1946 break;
1947 case SR_UP_REQUEST:
1948 rx_interrupt(cosa, status);
1949 break;
1950 case SR_END_OF_TRANSFER:
1951 eot_interrupt(cosa, status);
1952 break;
1953 default:
1954 /* We may be too fast for SRP. Try to wait a bit more. */
1955 if (count++ < 100) {
1956 udelay(100);
1957 goto again;
1959 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1960 cosa->num, status & 0xff, count);
1962 #ifdef DEBUG_IRQS
1963 if (count)
1964 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1965 cosa->name, count);
1966 else
1967 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
1968 #endif
1972 /* ---------- I/O debugging routines ---------- */
1974 * These routines can be used to monitor COSA/SRP I/O and to printk()
1975 * the data being transfered on the data and status I/O port in a
1976 * readable way.
1979 #ifdef DEBUG_IO
1980 static void debug_status_in(struct cosa_data *cosa, int status)
1982 char *s;
1983 switch(status & SR_CMD_FROM_SRP_MASK) {
1984 case SR_UP_REQUEST:
1985 s = "RX_REQ";
1986 break;
1987 case SR_DOWN_REQUEST:
1988 s = "TX_REQ";
1989 break;
1990 case SR_END_OF_TRANSFER:
1991 s = "ET_REQ";
1992 break;
1993 default:
1994 s = "NO_REQ";
1995 break;
1997 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
1998 cosa->name,
1999 status,
2000 status & SR_USR_RQ ? "USR_RQ|":"",
2001 status & SR_TX_RDY ? "TX_RDY|":"",
2002 status & SR_RX_RDY ? "RX_RDY|":"",
2006 static void debug_status_out(struct cosa_data *cosa, int status)
2008 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2009 cosa->name,
2010 status,
2011 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
2012 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
2013 status & SR_RST ? "RESET|":"",
2014 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2015 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
2016 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
2019 static void debug_data_in(struct cosa_data *cosa, int data)
2021 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2024 static void debug_data_out(struct cosa_data *cosa, int data)
2026 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2029 static void debug_data_cmd(struct cosa_data *cosa, int data)
2031 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2032 cosa->name, data,
2033 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2034 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2036 #endif
2038 /* EOF -- this file has not been truncated */