Import 2.3.10pre5
[davej-history.git] / drivers / net / cosa.c
blobfdfa991b90bfc7882e79f2a101797a3c3b8a9e0d
1 /* $Id: cosa.c,v 1.24 1999/05/28 17:28:34 kas Exp $ */
3 /*
4 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
5 *
6 * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
7 * fixed a deadlock in cosa_sppp_open
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * The driver for the SRP and COSA synchronous serial cards.
27 * HARDWARE INFO
29 * Both cards are developed at the Institute of Computer Science,
30 * Masaryk University (http://www.ics.muni.cz/). The hardware is
31 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
32 * and the photo of both cards is available at
33 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
34 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
35 * For Linux-specific utilities, see below in the "Software info" section.
36 * If you want to order the card, contact Jiri Novotny.
38 * The SRP (serial port?, the Czech word "srp" means "sickle") card
39 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
40 * with V.24 interfaces up to 80kb/s each.
42 * The COSA (communication serial adapter?, the Czech word "kosa" means
43 * "scythe") is a next-generation sync/async board with two interfaces
44 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
45 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
46 * The 8-channels version is in development.
48 * Both types have downloadable firmware and communicate via ISA DMA.
49 * COSA can be also a bus-mastering device.
51 * SOFTWARE INFO
53 * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
54 * The CVS tree of Linux driver can be viewed there, as well as the
55 * firmware binaries and user-space utilities for downloading the firmware
56 * into the card and setting up the card.
58 * The Linux driver (unlike the present *BSD drivers :-) can work even
59 * for the COSA and SRP in one computer and allows each channel to work
60 * in one of the three modes (character device, Cisco HDLC, Sync PPP).
62 * AUTHOR
64 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
66 * You can mail me bugfixes and even success reports. I am especially
67 * interested in the SMP and/or muliti-channel success/failure reports
68 * (I wonder if I did the locking properly :-).
70 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
72 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
73 * The skeleton.c by Donald Becker
74 * The SDL Riscom/N2 driver by Mike Natale
75 * The Comtrol Hostess SV11 driver by Alan Cox
76 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
79 * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
80 * fixed a deadlock in cosa_sppp_open
83 /* ---------- Headers, macros, data structures ---------- */
85 #include <linux/module.h>
86 #include <linux/kernel.h>
87 #include <linux/malloc.h>
88 #include <linux/poll.h>
89 #include <linux/fs.h>
90 #include <linux/sched.h>
91 #include <linux/interrupt.h>
92 #include <linux/delay.h>
93 #include <linux/errno.h>
94 #include <linux/ioport.h>
95 #include <linux/netdevice.h>
97 #undef COSA_SLOW_IO /* for testing purposes only */
98 #undef REALLY_SLOW_IO
100 #include <asm/io.h>
101 #include <asm/dma.h>
102 #include <asm/byteorder.h>
103 #include <asm/spinlock.h>
105 #include "syncppp.h"
106 #include "cosa.h"
108 /* Maximum length of the identification string. */
109 #define COSA_MAX_ID_STRING 128
111 /* Maximum length of the channel name */
112 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
114 /* Per-channel data structure */
116 struct channel_data {
117 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
118 int num; /* Number of the channel */
119 struct cosa_data *cosa; /* Pointer to the per-card structure */
120 int txsize; /* Size of transmitted data */
121 char *txbuf; /* Transmit buffer */
122 char name[COSA_MAX_NAME]; /* channel name */
124 /* The HW layer interface */
125 /* routine called from the RX interrupt */
126 char *(*setup_rx)(struct channel_data *channel, int size);
127 /* routine called when the RX is done (from the EOT interrupt) */
128 int (*rx_done)(struct channel_data *channel);
129 /* routine called when the TX is done (from the EOT interrupt) */
130 int (*tx_done)(struct channel_data *channel, int size);
132 /* Character device parts */
133 struct semaphore rsem, wsem;
134 char *rxdata;
135 int rxsize;
136 wait_queue_head_t txwaitq;
137 wait_queue_head_t rxwaitq;
138 int tx_status, rx_status;
140 /* SPPP/HDLC device parts */
141 struct ppp_device pppdev;
142 struct sk_buff *rx_skb, *tx_skb;
143 struct net_device_stats stats;
146 struct cosa_data {
147 int num; /* Card number */
148 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
149 unsigned int datareg, statusreg; /* I/O ports */
150 unsigned short irq, dma; /* IRQ and DMA number */
151 unsigned short startaddr; /* Firmware start address */
152 unsigned short busmaster; /* Use busmastering? */
153 int nchannels; /* # of channels on this card */
154 int driver_status; /* For communicating with firware */
155 int firmware_status; /* Downloaded, reseted, etc. */
156 int rxbitmap, txbitmap; /* Bitmap of channels who are willing to send/receive data */
157 int rxtx; /* RX or TX in progress? */
158 int enabled;
159 int usage; /* usage count */
160 int txchan, txsize, rxsize;
161 struct channel_data *rxchan;
162 char *bouncebuf;
163 char *txbuf, *rxbuf;
164 struct channel_data *chan;
165 spinlock_t lock; /* For exclusive operations on this structure */
166 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
167 char *type; /* card type */
171 * Define this if you want all the possible ports to be autoprobed.
172 * It is here but it probably is not a good idea to use this.
174 /* #define COSA_ISA_AUTOPROBE 1 */
177 * Character device major number. 117 was allocated for us.
178 * The value of 0 means to allocate a first free one.
180 static int cosa_major = 117;
183 * Encoding of the minor numbers:
184 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
185 * the highest bits means the card number.
187 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
188 * for the single card */
190 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
191 * macro doesn't like anything other than the raw number as an argument :-(
193 #define MAX_CARDS 16
194 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
196 #define DRIVER_RX_READY 0x0001
197 #define DRIVER_TX_READY 0x0002
198 #define DRIVER_TXMAP_SHIFT 2
199 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
202 * for cosa->rxtx - indicates whether either transmit or receive is
203 * in progress. These values are mean number of the bit.
205 #define TXBIT 0
206 #define RXBIT 1
207 #define IRQBIT 2
209 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
211 #undef DEBUG_DATA 1 /* Dump the data read or written to the channel */
212 #undef DEBUG_IRQS 1 /* Print the message when the IRQ is received */
213 #undef DEBUG_IO 1 /* Dump the I/O traffic */
215 /* Maybe the following should be allocated dynamically */
216 static struct cosa_data cosa_cards[MAX_CARDS];
217 static int nr_cards = 0;
219 #ifdef COSA_ISA_AUTOPROBE
220 static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
221 /* NOTE: DMA is not autoprobed!!! */
222 static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
223 #else
224 int io[MAX_CARDS+1] = { 0, };
225 int dma[MAX_CARDS+1] = { 0, };
226 #endif
227 /* IRQ can be safely autoprobed */
228 static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
230 #ifdef MODULE
231 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");
232 MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
233 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
234 MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
235 MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");
236 MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
238 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
239 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
240 #endif
242 /* I use this mainly for testing purposes */
243 #ifdef COSA_SLOW_IO
244 #define cosa_outb outb_p
245 #define cosa_outw outw_p
246 #define cosa_inb inb_p
247 #define cosa_inw inw_p
248 #else
249 #define cosa_outb outb
250 #define cosa_outw outw
251 #define cosa_inb inb
252 #define cosa_inw inw
253 #endif
255 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
257 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
258 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
259 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
260 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
261 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
262 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
264 /* Initialization stuff */
265 static int cosa_probe(int ioaddr, int irq, int dma);
267 /* HW interface */
268 static void cosa_enable_rx(struct channel_data *chan);
269 static void cosa_disable_rx(struct channel_data *chan);
270 static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
271 static void cosa_kick(struct cosa_data *cosa);
272 static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
274 /* SPPP/HDLC stuff */
275 static void sppp_channel_init(struct channel_data *chan);
276 static void sppp_channel_delete(struct channel_data *chan);
277 static int cosa_sppp_open(struct device *d);
278 static int cosa_sppp_close(struct device *d);
279 static int cosa_sppp_tx(struct sk_buff *skb, struct device *d);
280 static char *sppp_setup_rx(struct channel_data *channel, int size);
281 static int sppp_rx_done(struct channel_data *channel);
282 static int sppp_tx_done(struct channel_data *channel, int size);
283 static int cosa_sppp_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
284 static struct net_device_stats *cosa_net_stats(struct device *dev);
286 /* Character device */
287 static void chardev_channel_init(struct channel_data *chan);
288 static char *chrdev_setup_rx(struct channel_data *channel, int size);
289 static int chrdev_rx_done(struct channel_data *channel);
290 static int chrdev_tx_done(struct channel_data *channel, int size);
291 static long long cosa_lseek(struct file *file,
292 long long offset, int origin);
293 static ssize_t cosa_read(struct file *file,
294 char *buf, size_t count, loff_t *ppos);
295 static ssize_t cosa_write(struct file *file,
296 const char *buf, size_t count, loff_t *ppos);
297 static unsigned int cosa_poll(struct file *file, poll_table *poll);
298 static int cosa_open(struct inode *inode, struct file *file);
299 static int cosa_release(struct inode *inode, struct file *file);
300 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
301 unsigned int cmd, unsigned long arg);
302 #ifdef COSA_FASYNC_WORKING
303 static int cosa_fasync(struct inode *inode, struct file *file, int on);
304 #endif
306 static struct file_operations cosa_fops = {
307 cosa_lseek,
308 cosa_read,
309 cosa_write,
310 NULL, /* readdir */
311 cosa_poll,
312 cosa_chardev_ioctl,
313 NULL, /* mmap */
314 cosa_open,
315 NULL, /* flush */
316 cosa_release,
317 NULL, /* fsync */
318 #ifdef COSA_FASYNC_WORKING
319 cosa_fasync,
320 #else
321 NULL,
322 #endif
323 NULL, /* check media change */
324 NULL, /* revalidate */
325 NULL /* lock */
328 /* Ioctls */
329 static int cosa_start(struct cosa_data *cosa, int address);
330 static int cosa_reset(struct cosa_data *cosa);
331 static int cosa_download(struct cosa_data *cosa, struct cosa_download *d);
332 static int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d);
334 /* COSA/SRP ROM monitor */
335 static int download(struct cosa_data *cosa, char *data, int addr, int len);
336 static int startmicrocode(struct cosa_data *cosa, int address);
337 static int readmem(struct cosa_data *cosa, char *data, int addr, int len);
338 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
340 /* Auxilliary functions */
341 static int get_wait_data(struct cosa_data *cosa);
342 static int put_wait_data(struct cosa_data *cosa, int data);
343 static int puthexnumber(struct cosa_data *cosa, int number);
344 static void put_driver_status(struct cosa_data *cosa);
345 static void put_driver_status_nolock(struct cosa_data *cosa);
347 /* Interrupt handling */
348 static void cosa_interrupt(int irq, void *cosa, struct pt_regs *regs);
350 /* I/O ops debugging */
351 #ifdef DEBUG_IO
352 static void debug_data_in(struct cosa_data *cosa, int data);
353 static void debug_data_out(struct cosa_data *cosa, int data);
354 static void debug_data_cmd(struct cosa_data *cosa, int data);
355 static void debug_status_in(struct cosa_data *cosa, int status);
356 static void debug_status_out(struct cosa_data *cosa, int status);
357 #endif
360 /* ---------- Initialization stuff ---------- */
362 #ifdef MODULE
363 int init_module(void)
364 #else
365 static int __init cosa_init(void)
366 #endif
368 int i;
369 printk(KERN_INFO "cosa v1.04 (c) 1997-8 Jan Kasprzak <kas@fi.muni.cz>\n");
370 #ifdef __SMP__
371 printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
372 #endif
373 if (cosa_major > 0) {
374 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
375 printk(KERN_WARNING "cosa: unable to get major %d\n",
376 cosa_major);
377 return -EIO;
379 } else {
380 if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
381 printk(KERN_WARNING "cosa: unable to register chardev\n");
382 return -EIO;
385 for (i=0; i<MAX_CARDS; i++)
386 cosa_cards[i].num = -1;
387 for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
388 cosa_probe(io[i], irq[i], dma[i]);
389 if (!nr_cards) {
390 printk(KERN_WARNING "cosa: no devices found.\n");
391 unregister_chrdev(cosa_major, "cosa");
392 return -ENODEV;
394 return 0;
397 #ifdef MODULE
398 void cleanup_module (void)
400 struct cosa_data *cosa;
401 printk(KERN_INFO "Unloading the cosa module\n");
403 for (cosa=cosa_cards; nr_cards--; cosa++) {
404 int i;
405 /* Clean up the per-channel data */
406 for (i=0; i<cosa->nchannels; i++) {
407 /* Chardev driver has no alloc'd per-channel data */
408 sppp_channel_delete(cosa->chan+i);
410 /* Clean up the per-card data */
411 kfree(cosa->chan);
412 kfree(cosa->bouncebuf);
413 free_irq(cosa->irq, cosa);
414 free_dma(cosa->dma);
415 release_region(cosa->datareg,is_8bit(cosa)?2:4);
417 unregister_chrdev(cosa_major, "cosa");
419 #endif
422 * This function should register all the net devices needed for the
423 * single channel.
425 static __inline__ void channel_init(struct channel_data *chan)
427 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
429 /* Initialize the chardev data structures */
430 chardev_channel_init(chan);
432 /* Register the sppp interface */
433 sppp_channel_init(chan);
436 static int cosa_probe(int base, int irq, int dma)
438 struct cosa_data *cosa = cosa_cards+nr_cards;
439 int i;
441 memset(cosa, 0, sizeof(struct cosa_data));
443 /* Checking validity of parameters: */
444 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
445 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
446 printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
447 return -1;
449 /* I/O address should be between 0x100 and 0x3ff and should be
450 * multiple of 8. */
451 if (base < 0x100 || base > 0x3ff || base & 0x7) {
452 printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
453 base);
454 return -1;
456 /* DMA should be 0,1 or 3-7 */
457 if (dma < 0 || dma == 4 || dma > 7) {
458 printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
459 return -1;
461 /* and finally, on 16-bit COSA DMA should be 4-7 and
462 * I/O base should not be multiple of 0x10 */
463 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
464 printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
465 " (base=0x%x, dma=%d)\n", base, dma);
466 return -1;
469 cosa->dma = dma;
470 cosa->datareg = base;
471 cosa->statusreg = is_8bit(cosa)?base+1:base+2;
472 spin_lock_init(&cosa->lock);
474 if (check_region(base, is_8bit(cosa)?2:4))
475 return -1;
477 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
478 printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
479 return -1;
482 /* Test the validity of identification string */
483 if (!strncmp(cosa->id_string, "SRP", 3))
484 cosa->type = "srp";
485 else if (!strncmp(cosa->id_string, "COSA", 4))
486 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
487 else {
488 /* Print a warning only if we are not autoprobing */
489 #ifndef COSA_ISA_AUTOPROBE
490 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
491 base);
492 #endif
493 return -1;
496 /* Now do IRQ autoprobe */
497 if (irq < 0) {
498 unsigned long irqs;
499 /* printk(KERN_INFO "IRQ autoprobe\n"); */
500 sti();
501 irqs = probe_irq_on();
503 * Enable interrupt on tx buffer empty (it sure is)
504 * really sure ?
505 * FIXME: When this code is not used as module, we should
506 * probably call udelay() instead of the interruptible sleep.
508 current->state = TASK_INTERRUPTIBLE;
509 cosa_putstatus(cosa, SR_TX_INT_ENA);
510 schedule_timeout(30);
511 current->state = TASK_RUNNING;
512 irq = probe_irq_off(irqs);
513 /* Disable all IRQs from the card */
514 cosa_putstatus(cosa, 0);
515 /* Empty the received data register */
516 cosa_getdata8(cosa);
518 if (irq < 0) {
519 printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
520 irq, cosa->datareg);
521 return -1;
523 if (irq == 0) {
524 printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
525 cosa->datareg);
526 /* return -1; */
530 cosa->irq = irq;
531 cosa->num = nr_cards;
532 cosa->usage = 0;
533 cosa->nchannels = 2; /* FIXME: how to determine this? */
535 request_region(base, is_8bit(cosa)?2:4, cosa->type);
536 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa))
537 goto bad1;
538 if (request_dma(cosa->dma, cosa->type)) {
539 free_irq(cosa->irq, cosa);
540 bad1: release_region(cosa->datareg,is_8bit(cosa)?2:4);
541 printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
542 cosa->num);
543 return -1;
546 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
547 sprintf(cosa->name, "cosa%d", cosa->num);
549 /* Initialize the per-channel data */
550 cosa->chan = kmalloc(sizeof(struct channel_data)*cosa->nchannels,
551 GFP_KERNEL);
552 memset(cosa->chan, 0, sizeof(struct channel_data)*cosa->nchannels);
553 for (i=0; i<cosa->nchannels; i++) {
554 cosa->chan[i].cosa = cosa;
555 cosa->chan[i].num = i;
556 channel_init(cosa->chan+i);
559 printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
560 cosa->num, cosa->id_string, cosa->type,
561 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
563 return nr_cards++;
567 /*---------- SPPP/HDLC netdevice ---------- */
569 static void sppp_channel_init(struct channel_data *chan)
571 struct device *d;
572 sppp_attach(&chan->pppdev);
573 d=&chan->pppdev.dev;
574 d->name = chan->name;
575 d->base_addr = chan->cosa->datareg;
576 d->irq = chan->cosa->irq;
577 d->dma = chan->cosa->dma;
578 d->priv = chan;
579 d->init = NULL;
580 d->open = cosa_sppp_open;
581 d->stop = cosa_sppp_close;
582 d->hard_start_xmit = cosa_sppp_tx;
583 d->do_ioctl = cosa_sppp_ioctl;
584 d->get_stats = cosa_net_stats;
585 dev_init_buffers(d);
586 if (register_netdev(d) == -1) {
587 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
588 sppp_detach(&chan->pppdev.dev);
589 return;
593 static void sppp_channel_delete(struct channel_data *chan)
595 sppp_detach(&chan->pppdev.dev);
596 unregister_netdev(&chan->pppdev.dev);
600 static int cosa_sppp_open(struct device *d)
602 struct channel_data *chan = d->priv;
603 int err, flags;
605 spin_lock_irqsave(&chan->cosa->lock, flags);
606 if (chan->usage != 0) {
607 printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
608 chan->name, chan->usage);
609 spin_unlock_irqrestore(&chan->cosa->lock, flags);
610 return -EBUSY;
612 chan->setup_rx = sppp_setup_rx;
613 chan->tx_done = sppp_tx_done;
614 chan->rx_done = sppp_rx_done;
615 chan->usage=-1;
616 chan->cosa->usage++;
617 MOD_INC_USE_COUNT;
618 spin_unlock_irqrestore(&chan->cosa->lock, flags);
620 err = sppp_open(d);
621 if (err) {
622 spin_lock_irqsave(&chan->cosa->lock, flags);
623 chan->usage=0;
624 chan->cosa->usage--;
625 MOD_DEC_USE_COUNT;
627 spin_unlock_irqrestore(&chan->cosa->lock, flags);
628 return err;
631 d->tbusy = 0;
632 cosa_enable_rx(chan);
633 return 0;
636 static int cosa_sppp_tx(struct sk_buff *skb, struct device *dev)
638 struct channel_data *chan = dev->priv;
640 if (dev->tbusy) {
641 if (time_before(jiffies, dev->trans_start+2*HZ))
642 return 1; /* Two seconds timeout */
643 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
644 chan->stats.rx_errors++;
645 chan->stats.rx_missed_errors++;
646 } else {
647 chan->stats.tx_errors++;
648 chan->stats.tx_aborted_errors++;
650 cosa_kick(chan->cosa);
651 if (chan->tx_skb) {
652 dev_kfree_skb(chan->tx_skb);
653 chan->tx_skb = 0;
655 dev->tbusy = 0;
657 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
658 printk(KERN_WARNING "%s: Transmitter access conflict.\n", dev->name);
659 return 1;
662 chan->tx_skb = skb;
663 dev->trans_start = jiffies;
664 cosa_start_tx(chan, skb->data, skb->len);
665 return 0;
668 static int cosa_sppp_close(struct device *d)
670 struct channel_data *chan = d->priv;
671 int flags;
673 sppp_close(d);
674 d->tbusy = 1;
675 cosa_disable_rx(chan);
676 spin_lock_irqsave(&chan->cosa->lock, flags);
677 if (chan->rx_skb) {
678 kfree_skb(chan->rx_skb);
679 chan->rx_skb = 0;
681 if (chan->tx_skb) {
682 kfree_skb(chan->tx_skb);
683 chan->tx_skb = 0;
685 chan->usage=0;
686 chan->cosa->usage--;
687 MOD_DEC_USE_COUNT;
688 spin_unlock_irqrestore(&chan->cosa->lock, flags);
689 return 0;
692 static char *sppp_setup_rx(struct channel_data *chan, int size)
695 * We can safely fall back to non-dma-able memory, because we have
696 * the cosa->bouncebuf pre-allocated.
698 if (chan->rx_skb)
699 kfree_skb(chan->rx_skb);
700 chan->rx_skb = dev_alloc_skb(size);
701 if (chan->rx_skb == NULL) {
702 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
703 chan->name);
704 chan->stats.rx_dropped++;
705 return NULL;
707 chan->pppdev.dev.trans_start = jiffies;
708 return skb_put(chan->rx_skb, size);
711 static int sppp_rx_done(struct channel_data *chan)
713 if (!chan->rx_skb) {
714 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
715 chan->name);
716 chan->stats.rx_errors++;
717 chan->stats.rx_frame_errors++;
718 return 0;
720 chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
721 chan->rx_skb->dev = &chan->pppdev.dev;
722 chan->rx_skb->mac.raw = chan->rx_skb->data;
723 chan->stats.rx_packets++;
724 chan->stats.rx_bytes += chan->cosa->rxsize;
725 netif_rx(chan->rx_skb);
726 chan->rx_skb = 0;
727 chan->pppdev.dev.trans_start = jiffies;
728 return 0;
731 /* ARGSUSED */
732 static int sppp_tx_done(struct channel_data *chan, int size)
734 if (!chan->tx_skb) {
735 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
736 chan->name);
737 chan->stats.tx_errors++;
738 chan->stats.tx_aborted_errors++;
739 return 1;
741 dev_kfree_skb(chan->tx_skb);
742 chan->tx_skb = 0;
743 chan->stats.tx_packets++;
744 chan->stats.tx_bytes += size;
745 chan->pppdev.dev.tbusy = 0;
746 mark_bh(NET_BH);
747 return 1;
750 static struct net_device_stats *cosa_net_stats(struct device *dev)
752 struct channel_data *chan = dev->priv;
753 return &chan->stats;
757 /*---------- Character device ---------- */
759 static void chardev_channel_init(struct channel_data *chan)
761 init_MUTEX(&chan->rsem);
762 init_MUTEX(&chan->wsem);
765 static long long cosa_lseek(struct file * file,
766 long long offset, int origin)
768 return -ESPIPE;
771 static ssize_t cosa_read(struct file *file,
772 char *buf, size_t count, loff_t *ppos)
774 DECLARE_WAITQUEUE(wait, current);
775 int flags;
776 struct channel_data *chan = (struct channel_data *)file->private_data;
777 struct cosa_data *cosa = chan->cosa;
778 char *kbuf;
780 if (down_interruptible(&chan->rsem))
781 return -ERESTARTSYS;
783 if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
784 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
785 up(&chan->rsem);
786 return -ENOMEM;
789 chan->rx_status = 0;
790 cosa_enable_rx(chan);
791 spin_lock_irqsave(&cosa->lock, flags);
792 add_wait_queue(&chan->rxwaitq, &wait);
793 while(!chan->rx_status) {
794 current->state = TASK_INTERRUPTIBLE;
795 spin_unlock_irqrestore(&cosa->lock, flags);
796 schedule();
797 spin_lock_irqsave(&cosa->lock, flags);
798 if (signal_pending(current) && chan->rx_status == 0) {
799 chan->rx_status = 1;
800 remove_wait_queue(&chan->rxwaitq, &wait);
801 current->state = TASK_RUNNING;
802 spin_unlock_irqrestore(&cosa->lock, flags);
803 up(&chan->rsem);
804 return -ERESTARTSYS;
807 remove_wait_queue(&chan->rxwaitq, &wait);
808 current->state = TASK_RUNNING;
809 kbuf = chan->rxdata;
810 count = chan->rxsize;
811 spin_unlock_irqrestore(&cosa->lock, flags);
812 up(&chan->rsem);
814 if (copy_to_user(buf, kbuf, count)) {
815 kfree(buf);
816 return -EFAULT;
818 kfree(kbuf);
819 return count;
822 static char *chrdev_setup_rx(struct channel_data *chan, int size)
824 /* Expect size <= COSA_MTU */
825 chan->rxsize = size;
826 return chan->rxdata;
829 static int chrdev_rx_done(struct channel_data *chan)
831 if (chan->rx_status) { /* Reader has died */
832 kfree(chan->rxdata);
833 up(&chan->wsem);
835 chan->rx_status = 1;
836 wake_up_interruptible(&chan->rxwaitq);
837 return 1;
841 static ssize_t cosa_write(struct file *file,
842 const char *buf, size_t count, loff_t *ppos)
844 struct channel_data *chan = (struct channel_data *)file->private_data;
845 DECLARE_WAITQUEUE(wait, current);
846 struct cosa_data *cosa = chan->cosa;
847 unsigned int flags;
848 char *kbuf;
850 if (down_interruptible(&chan->wsem))
851 return -ERESTARTSYS;
853 if (count > COSA_MTU)
854 count = COSA_MTU;
856 /* Allocate the buffer */
857 if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
858 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
859 cosa->name);
860 up(&chan->wsem);
861 return -ENOMEM;
863 if (copy_from_user(kbuf, buf, count)) {
864 up(&chan->wsem);
865 kfree(kbuf);
866 return -EFAULT;
868 chan->tx_status=0;
869 cosa_start_tx(chan, kbuf, count);
871 spin_lock_irqsave(&cosa->lock, flags);
872 add_wait_queue(&chan->txwaitq, &wait);
873 while(!chan->tx_status) {
874 current->state = TASK_INTERRUPTIBLE;
875 spin_unlock_irqrestore(&cosa->lock, flags);
876 schedule();
877 spin_lock_irqsave(&cosa->lock, flags);
878 if (signal_pending(current) && chan->tx_status == 0) {
879 chan->tx_status = 1;
880 remove_wait_queue(&chan->txwaitq, &wait);
881 current->state = TASK_RUNNING;
882 chan->tx_status = 1;
883 spin_unlock_irqrestore(&cosa->lock, flags);
884 return -ERESTARTSYS;
887 remove_wait_queue(&chan->txwaitq, &wait);
888 current->state = TASK_RUNNING;
889 up(&chan->wsem);
890 spin_unlock_irqrestore(&cosa->lock, flags);
891 kfree(kbuf);
892 return count;
895 static int chrdev_tx_done(struct channel_data *chan, int size)
897 if (chan->tx_status) { /* Writer was interrupted */
898 kfree(chan->txbuf);
899 up(&chan->wsem);
901 chan->tx_status = 1;
902 wake_up_interruptible(&chan->txwaitq);
903 return 1;
906 static unsigned int cosa_poll(struct file *file, poll_table *poll)
908 printk(KERN_INFO "cosa_poll is here\n");
909 return 0;
912 static int cosa_open(struct inode *inode, struct file *file)
914 struct cosa_data *cosa;
915 struct channel_data *chan;
916 unsigned long flags;
917 int n;
919 if ((n=MINOR(file->f_dentry->d_inode->i_rdev)>>CARD_MINOR_BITS)
920 >= nr_cards)
921 return -ENODEV;
922 cosa = cosa_cards+n;
924 if ((n=MINOR(file->f_dentry->d_inode->i_rdev)
925 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
926 return -ENODEV;
927 chan = cosa->chan + n;
929 file->private_data = chan;
931 spin_lock_irqsave(&cosa->lock, flags);
933 if (chan->usage < 0) { /* in netdev mode */
934 spin_unlock_irqrestore(&cosa->lock, flags);
935 return -EBUSY;
937 cosa->usage++;
938 chan->usage++;
940 chan->tx_done = chrdev_tx_done;
941 chan->setup_rx = chrdev_setup_rx;
942 chan->rx_done = chrdev_rx_done;
943 #ifdef MODULE
944 MOD_INC_USE_COUNT;
945 #endif
946 spin_unlock_irqrestore(&cosa->lock, flags);
947 return 0;
950 static int cosa_release(struct inode *inode, struct file *file)
952 struct channel_data *channel = (struct channel_data *)file->private_data;
953 struct cosa_data *cosa = channel->cosa;
954 unsigned long flags;
956 spin_lock_irqsave(&cosa->lock, flags);
957 cosa->usage--;
958 channel->usage--;
959 #ifdef MODULE
960 MOD_DEC_USE_COUNT;
961 #endif
962 spin_unlock_irqrestore(&cosa->lock, flags);
963 return 0;
966 #ifdef COSA_FASYNC_WORKING
967 static struct fasync_struct *fasync[256] = { NULL, };
969 /* To be done ... */
970 static int cosa_fasync(struct inode *inode, struct file *file, int on)
972 int port = MINOR(inode->i_rdev);
973 int rv = fasync_helper(inode, file, on, &fasync[port]);
974 return rv < 0 ? rv : 0;
976 #endif
979 /* ---------- Ioctls ---------- */
982 * Ioctl subroutines can safely be made inline, because they are called
983 * only from cosa_ioctl().
985 static inline int cosa_reset(struct cosa_data *cosa)
987 char idstring[COSA_MAX_ID_STRING];
988 if (cosa->usage > 1)
989 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
990 cosa->num, cosa->usage);
991 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
992 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
993 return -EIO;
995 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
996 idstring);
997 return 0;
1000 /* High-level function to download data into COSA memory. Calls download() */
1001 static inline int cosa_download(struct cosa_data *cosa, struct cosa_download *d)
1003 int i;
1004 int addr, len;
1005 char *code;
1007 if (cosa->usage > 1)
1008 printk(KERN_INFO "cosa%d: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1009 cosa->num, cosa->usage);
1010 #if 0
1011 if (cosa->status != CARD_STATUS_RESETED && cosa->status != CARD_STATUS_DOWNLOADED) {
1012 printk(KERN_NOTICE "cosa%d: reset the card first (status %d).\n",
1013 cosa->num, cosa->status);
1014 return -EPERM;
1016 #endif
1017 get_user_ret(addr, &(d->addr), -EFAULT);
1018 get_user_ret(len, &(d->len), -EFAULT);
1019 get_user_ret(code, &(d->code), -EFAULT);
1021 if (d->addr < 0 || d->addr > COSA_MAX_FIRMWARE_SIZE)
1022 return -EINVAL;
1023 if (d->len < 0 || d->len > COSA_MAX_FIRMWARE_SIZE)
1024 return -EINVAL;
1026 if ((i=download(cosa, d->code, len, addr)) < 0) {
1027 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1028 cosa->num, i);
1029 return -EIO;
1031 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1032 cosa->num, len, addr);
1033 return 0;
1036 /* High-level function to read COSA memory. Calls readmem() */
1037 static inline int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d)
1039 int i;
1040 int addr, len;
1041 char *code;
1043 if (cosa->usage > 1)
1044 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1045 "cosa->usage > 1 (%d). Odd things may happen.\n",
1046 cosa->num, cosa->usage);
1047 #if 0
1048 if (cosa->status != CARD_STATUS_RESETED &&
1049 cosa->status != CARD_STATUS_DOWNLOADED) {
1050 printk(KERN_NOTICE "cosa%d: reset the card first (status %d).\n",
1051 cosa->num, cosa->status);
1052 return -EPERM;
1054 #endif
1055 get_user_ret(addr, &(d->addr), -EFAULT);
1056 get_user_ret(len, &(d->len), -EFAULT);
1057 get_user_ret(code, &(d->code), -EFAULT);
1059 if ((i=readmem(cosa, d->code, len, addr)) < 0) {
1060 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1061 cosa->num, i);
1062 return -EIO;
1064 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1065 cosa->num, len, addr);
1066 return 0;
1069 /* High-level function to start microcode. Calls startmicrocode(). */
1070 static inline int cosa_start(struct cosa_data *cosa, int address)
1072 int i;
1074 if (cosa->usage > 1)
1075 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1076 cosa->num, cosa->usage);
1077 #if 0
1078 if (cosa->status != CARD_STATUS_DOWNLOADED) {
1079 printk(KERN_NOTICE "cosa%d: download the microcode first (status %d).\n",
1080 cosa->num, cosa->status);
1081 return -EPERM;
1083 #endif
1084 if ((i=startmicrocode(cosa, address)) < 0) {
1085 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1086 cosa->num, address, i);
1087 return -EIO;
1089 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1090 cosa->num, address);
1091 cosa->startaddr = address;
1092 return 0;
1095 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1096 static inline int cosa_getidstr(struct cosa_data *cosa, char *string)
1098 int l = strlen(cosa->id_string)+1;
1099 copy_to_user_ret(string, cosa->id_string, l, -EFAULT);
1100 return l;
1103 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1104 static inline int cosa_gettype(struct cosa_data *cosa, char *string)
1106 int l = strlen(cosa->type)+1;
1107 copy_to_user_ret(string, cosa->type, l, -EFAULT);
1108 return l;
1111 static int cosa_ioctl_common(struct cosa_data *cosa,
1112 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1114 switch(cmd) {
1115 case COSAIORSET: /* Reset the device */
1116 if (!suser())
1117 return -EACCES;
1118 return cosa_reset(cosa);
1119 case COSAIOSTRT: /* Start the firmware */
1120 if (!suser())
1121 return -EACCES;
1122 return cosa_start(cosa, arg);
1123 case COSAIODOWNLD: /* Download the firmware */
1124 if (!suser())
1125 return -EACCES;
1126 return cosa_download(cosa, (struct cosa_download *)arg);
1127 case COSAIORMEM:
1128 if (!suser())
1129 return -EACCES;
1130 return cosa_readmem(cosa, (struct cosa_download *)arg);
1131 case COSAIORTYPE:
1132 return cosa_gettype(cosa, (char *)arg);
1133 case COSAIORIDSTR:
1134 return cosa_getidstr(cosa, (char *)arg);
1136 * These two are _very_ugly_hack_(tm). Don't even look at this.
1137 * Implementing this saved me few reboots after some process segfaulted
1138 * inside this module.
1140 #ifdef MODULE
1141 #if 0
1142 case COSAIOMINC:
1143 MOD_INC_USE_COUNT;
1144 return 0;
1145 case COSAIOMDEC:
1146 MOD_DEC_USE_COUNT;
1147 return 0;
1148 #endif
1149 #endif
1150 case COSAIONRCARDS:
1151 return nr_cards;
1152 case COSAIONRCHANS:
1153 return cosa->nchannels;
1154 case COSAIOBMSET:
1155 if (!suser())
1156 return -EACCES;
1157 if (is_8bit(cosa))
1158 return -EINVAL;
1159 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1160 return -EINVAL;
1161 cosa->busmaster = arg;
1162 return 0;
1163 case COSAIOBMGET:
1164 return cosa->busmaster;
1166 return -ENOIOCTLCMD;
1169 static int cosa_sppp_ioctl(struct device *dev, struct ifreq *ifr,
1170 int cmd)
1172 int rv;
1173 struct channel_data *chan = (struct channel_data *)dev->priv;
1174 rv = cosa_ioctl_common(chan->cosa, chan, cmd, (int)ifr->ifr_data);
1175 if (rv == -ENOIOCTLCMD) {
1176 return sppp_do_ioctl(dev, ifr, cmd);
1178 return rv;
1181 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1182 unsigned int cmd, unsigned long arg)
1184 struct channel_data *channel = (struct channel_data *)file->private_data;
1185 struct cosa_data *cosa = channel->cosa;
1186 return cosa_ioctl_common(cosa, channel, cmd, arg);
1190 /*---------- HW layer interface ---------- */
1193 * The higher layer can bind itself to the HW layer by setting the callbacks
1194 * in the channel_data structure and by using these routines.
1196 static void cosa_enable_rx(struct channel_data *chan)
1198 struct cosa_data *cosa = chan->cosa;
1200 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1201 put_driver_status(cosa);
1204 static void cosa_disable_rx(struct channel_data *chan)
1206 struct cosa_data *cosa = chan->cosa;
1208 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1209 put_driver_status(cosa);
1213 * FIXME: This routine probably should check for cosa_start_tx() called when
1214 * the previous transmit is still unfinished. In this case the non-zero
1215 * return value should indicate to the caller that the queuing(sp?) up
1216 * the transmit has failed.
1218 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1220 struct cosa_data *cosa = chan->cosa;
1221 int flags;
1222 #ifdef DEBUG_DATA
1223 int i;
1225 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1226 chan->num, len);
1227 for (i=0; i<len; i++)
1228 printk(" %02x", buf[i]&0xff);
1229 printk("\n");
1230 #endif
1231 spin_lock_irqsave(&cosa->lock, flags);
1232 chan->txbuf = buf;
1233 chan->txsize = len;
1234 if (len > COSA_MTU)
1235 chan->txsize = COSA_MTU;
1236 spin_unlock_irqrestore(&cosa->lock, flags);
1238 /* Tell the firmware we are ready */
1239 set_bit(chan->num, &cosa->txbitmap);
1240 put_driver_status(cosa);
1242 return 0;
1245 static void put_driver_status(struct cosa_data *cosa)
1247 unsigned flags=0;
1248 int status;
1250 spin_lock_irqsave(&cosa->lock, flags);
1252 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1253 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1254 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1255 &DRIVER_TXMAP_MASK : 0);
1256 if (!cosa->rxtx) {
1257 if (cosa->rxbitmap|cosa->txbitmap) {
1258 if (!cosa->enabled) {
1259 cosa_putstatus(cosa, SR_RX_INT_ENA);
1260 #ifdef DEBUG_IO
1261 debug_status_out(cosa, SR_RX_INT_ENA);
1262 #endif
1263 cosa->enabled = 1;
1265 } else if (cosa->enabled) {
1266 cosa->enabled = 0;
1267 cosa_putstatus(cosa, 0);
1268 #ifdef DEBUG_IO
1269 debug_status_out(cosa, 0);
1270 #endif
1272 cosa_putdata8(cosa, 0);
1273 cosa_putdata8(cosa, status);
1274 #ifdef DEBUG_IO
1275 debug_data_cmd(cosa, 0);
1276 debug_data_cmd(cosa, status);
1277 #endif
1279 spin_unlock_irqrestore(&cosa->lock, flags);
1282 static void put_driver_status_nolock(struct cosa_data *cosa)
1284 int status;
1286 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1287 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1288 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1289 &DRIVER_TXMAP_MASK : 0);
1291 if (cosa->rxbitmap|cosa->txbitmap) {
1292 cosa_putstatus(cosa, SR_RX_INT_ENA);
1293 #ifdef DEBUG_IO
1294 debug_status_out(cosa, SR_RX_INT_ENA);
1295 #endif
1296 cosa->enabled = 1;
1297 } else {
1298 cosa_putstatus(cosa, 0);
1299 #ifdef DEBUG_IO
1300 debug_status_out(cosa, 0);
1301 #endif
1302 cosa->enabled = 0;
1304 cosa_putdata8(cosa, status);
1305 #ifdef DEBUG_IO
1306 debug_data_cmd(cosa, status);
1307 #endif
1311 * The "kickme" function: When the DMA times out, this is called to
1312 * clean up the driver status.
1313 * FIXME: Preliminary support, the interface is probably wrong.
1315 static void cosa_kick(struct cosa_data *cosa)
1317 unsigned flags, flags1;
1318 char *s = "Unknown";
1320 if (test_bit(RXBIT, &cosa->rxtx))
1321 s = "RX";
1322 if (test_bit(TXBIT, &cosa->rxtx))
1323 s = "TX";
1325 printk(KERN_INFO "%s: %s DMA timeout - restarting.\n", cosa->name, s);
1326 spin_lock_irqsave(&cosa->lock, flags);
1327 cosa->rxtx = 0;
1329 flags1 = claim_dma_lock();
1330 disable_dma(cosa->dma);
1331 clear_dma_ff(cosa->dma);
1332 release_dma_lock(flags1);
1334 /* FIXME: Anything else? */
1335 udelay(100);
1336 cosa_putstatus(cosa, 0);
1337 udelay(100);
1338 (void) cosa_getdata8(cosa);
1339 udelay(100);
1340 cosa_putdata8(cosa, 0);
1341 udelay(100);
1342 put_driver_status_nolock(cosa);
1343 spin_unlock_irqrestore(&cosa->lock, flags);
1347 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1348 * physical memory and doesn't span the 64k boundary. For now it seems
1349 * SKB's never do this, but we'll check this anyway.
1351 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1353 static int count = 0;
1354 unsigned long b = (unsigned long)buf;
1355 if (b+len >= MAX_DMA_ADDRESS)
1356 return 0;
1357 if ((b^ (b+len)) & 0x10000) {
1358 if (count++ < 5)
1359 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1360 chan->name);
1361 return 0;
1363 return 1;
1367 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1370 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1371 * drivers need to say 4-digit hex number meaning start address of the microcode
1372 * separated by a single space. Monitor replies by saying " =". Now driver
1373 * has to write 4-digit hex number meaning the last byte address ended
1374 * by a single space. Monitor has to reply with a space. Now the download
1375 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1377 static int download(struct cosa_data *cosa, char *microcode, int length, int address)
1379 int i;
1381 if (put_wait_data(cosa, 'w') == -1) return -1;
1382 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1383 if (get_wait_data(cosa) != '=') return -3;
1385 if (puthexnumber(cosa, address) < 0) return -4;
1386 if (put_wait_data(cosa, ' ') == -1) return -10;
1387 if (get_wait_data(cosa) != ' ') return -11;
1388 if (get_wait_data(cosa) != '=') return -12;
1390 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1391 if (put_wait_data(cosa, ' ') == -1) return -18;
1392 if (get_wait_data(cosa) != ' ') return -19;
1394 while (length--) {
1395 char c;
1396 #ifndef SRP_DOWNLOAD_AT_BOOT
1397 get_user_ret(c,microcode, -23);
1398 #else
1399 c = *microcode;
1400 #endif
1401 if (put_wait_data(cosa, c) == -1)
1402 return -20;
1403 microcode++;
1406 if (get_wait_data(cosa) != '\r') return -21;
1407 if (get_wait_data(cosa) != '\n') return -22;
1408 if (get_wait_data(cosa) != '.') return -23;
1409 #if 0
1410 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1411 #endif
1412 return 0;
1417 * Starting microcode is done via the "g" command of the SRP monitor.
1418 * The chat should be the following: "g" "g=" "<addr><CR>"
1419 * "<CR><CR><LF><CR><LF>".
1421 static int startmicrocode(struct cosa_data *cosa, int address)
1423 if (put_wait_data(cosa, 'g') == -1) return -1;
1424 if (get_wait_data(cosa) != 'g') return -2;
1425 if (get_wait_data(cosa) != '=') return -3;
1427 if (puthexnumber(cosa, address) < 0) return -4;
1428 if (put_wait_data(cosa, '\r') == -1) return -5;
1430 if (get_wait_data(cosa) != '\r') return -6;
1431 if (get_wait_data(cosa) != '\r') return -7;
1432 if (get_wait_data(cosa) != '\n') return -8;
1433 if (get_wait_data(cosa) != '\r') return -9;
1434 if (get_wait_data(cosa) != '\n') return -10;
1435 #if 0
1436 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1437 #endif
1438 return 0;
1442 * Reading memory is done via the "r" command of the SRP monitor.
1443 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1444 * Then driver can read the data and the conversation is finished
1445 * by SRP monitor sending "<CR><LF>." (dot at the end).
1447 * This routine is not needed during the normal operation and serves
1448 * for debugging purposes only.
1450 static int readmem(struct cosa_data *cosa, char *microcode, int length, int address)
1452 if (put_wait_data(cosa, 'r') == -1) return -1;
1453 if ((get_wait_data(cosa)) != 'r') return -2;
1454 if ((get_wait_data(cosa)) != '=') return -3;
1456 if (puthexnumber(cosa, address) < 0) return -4;
1457 if (put_wait_data(cosa, ' ') == -1) return -5;
1458 if (get_wait_data(cosa) != ' ') return -6;
1459 if (get_wait_data(cosa) != '=') return -7;
1461 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1462 if (put_wait_data(cosa, ' ') == -1) return -9;
1463 if (get_wait_data(cosa) != ' ') return -10;
1465 while (length--) {
1466 char c;
1467 int i;
1468 if ((i=get_wait_data(cosa)) == -1) {
1469 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1470 length);
1471 return -11;
1473 c=i;
1474 #if 1
1475 put_user_ret(c,microcode, -23);
1476 #else
1477 *microcode = c;
1478 #endif
1479 microcode++;
1482 if (get_wait_data(cosa) != '\r') return -21;
1483 if (get_wait_data(cosa) != '\n') return -22;
1484 if (get_wait_data(cosa) != '.') return -23;
1485 #if 0
1486 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1487 #endif
1488 return 0;
1492 * This function resets the device and reads the initial prompt
1493 * of the device's ROM monitor.
1495 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1497 int i=0, id=0, prev=0, curr=0;
1499 /* Reset the card ... */
1500 cosa_putstatus(cosa, 0);
1501 cosa_getdata8(cosa);
1502 cosa_putstatus(cosa, SR_RST);
1503 #ifdef MODULE
1504 current->state = TASK_INTERRUPTIBLE;
1505 schedule_timeout(HZ/2);
1506 current->state = TASK_RUNNING;
1507 #else
1508 udelay(5*100000);
1509 #endif
1510 /* Disable all IRQs from the card */
1511 cosa_putstatus(cosa, 0);
1514 * Try to read the ID string. The card then prints out the
1515 * identification string ended by the "\n\x2e".
1517 * The following loop is indexed through i (instead of id)
1518 * to avoid looping forever when for any reason
1519 * the port returns '\r', '\n' or '\x2e' permanently.
1521 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1522 if ((curr = get_wait_data(cosa)) == -1) {
1523 return -1;
1525 curr &= 0xff;
1526 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1527 idstring[id++] = curr;
1528 if (curr == 0x2e && prev == '\n')
1529 break;
1531 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1532 idstring[id] = '\0';
1533 return id;
1537 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1540 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1541 * bit to be set in a loop. It should be used in the exceptional cases
1542 * only (for example when resetting the card or downloading the firmware.
1544 static int get_wait_data(struct cosa_data *cosa)
1546 int retries = 1000;
1548 while (--retries) {
1549 /* read data and return them */
1550 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1551 short r;
1552 r = cosa_getdata8(cosa);
1553 #if 0
1554 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1555 #endif
1556 return r;
1558 /* sleep if not ready to read */
1559 current->state = TASK_INTERRUPTIBLE;
1560 schedule_timeout(1);
1561 current->state = TASK_RUNNING;
1563 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1564 cosa_getstatus(cosa));
1565 return -1;
1569 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1570 * bit to be set in a loop. It should be used in the exceptional cases
1571 * only (for example when resetting the card or downloading the firmware).
1573 static int put_wait_data(struct cosa_data *cosa, int data)
1575 int retries = 1000;
1576 while (--retries) {
1577 /* read data and return them */
1578 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1579 cosa_putdata8(cosa, data);
1580 #if 0
1581 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1582 #endif
1583 return 0;
1585 #if 0
1586 /* sleep if not ready to read */
1587 current->state = TASK_INTERRUPTIBLE;
1588 schedule_timeout(1);
1589 current->state = TASK_RUNNING;
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 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1653 unsigned long flags, flags1;
1654 #ifdef DEBUG_IRQS
1655 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1656 cosa->num, status);
1657 #endif
1658 spin_lock_irqsave(&cosa->lock, flags);
1659 set_bit(TXBIT, &cosa->rxtx);
1660 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1661 /* flow control */
1662 int i=0;
1663 do {
1664 if (i++ > cosa->nchannels) {
1665 printk(KERN_WARNING
1666 "%s: No channel wants data in TX IRQ\n",
1667 cosa->name);
1668 put_driver_status_nolock(cosa);
1669 clear_bit(TXBIT, &cosa->rxtx);
1670 spin_unlock_irqrestore(&cosa->lock, flags);
1671 return;
1673 cosa->txchan++;
1674 if (cosa->txchan >= cosa->nchannels)
1675 cosa->txchan = 0;
1676 } while ((!(cosa->txbitmap & (1<<cosa->txchan)))
1677 || status & (1<<(cosa->txchan+DRIVER_TXMAP_SHIFT)));
1679 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1680 if (cosa_dma_able(cosa->chan+cosa->txchan,
1681 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1682 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1683 } else {
1684 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1685 cosa->txsize);
1686 cosa->txbuf = cosa->bouncebuf;
1690 if (is_8bit(cosa)) {
1691 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1692 cosa_putstatus(cosa, SR_TX_INT_ENA);
1693 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1694 ((cosa->txsize >> 8) & 0x1f));
1695 #ifdef DEBUG_IO
1696 debug_status_out(cosa, SR_TX_INT_ENA);
1697 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1698 ((cosa->txsize >> 8) & 0x1f));
1699 debug_data_in(cosa, cosa_getdata8(cosa));
1700 #else
1701 cosa_getdata8(cosa);
1702 #endif
1703 set_bit(IRQBIT, &cosa->rxtx);
1704 spin_unlock_irqrestore(&cosa->lock, flags);
1705 return;
1706 } else {
1707 clear_bit(IRQBIT, &cosa->rxtx);
1708 cosa_putstatus(cosa, 0);
1709 cosa_putdata8(cosa, cosa->txsize&0xff);
1710 #ifdef DEBUG_IO
1711 debug_status_out(cosa, 0);
1712 debug_data_out(cosa, cosa->txsize&0xff);
1713 #endif
1715 } else {
1716 cosa_putstatus(cosa, SR_TX_INT_ENA);
1717 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1718 | (cosa->txsize & 0x1fff));
1719 #ifdef DEBUG_IO
1720 debug_status_out(cosa, SR_TX_INT_ENA);
1721 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1722 | (cosa->txsize & 0x1fff));
1723 debug_data_in(cosa, cosa_getdata8(cosa));
1724 debug_status_out(cosa, 0);
1725 #else
1726 cosa_getdata8(cosa);
1727 #endif
1728 cosa_putstatus(cosa, 0);
1731 if (cosa->busmaster) {
1732 unsigned long addr = virt_to_bus(cosa->txbuf);
1733 int count=0;
1734 printk(KERN_INFO "busmaster IRQ\n");
1735 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1736 count++;
1737 udelay(10);
1738 if (count > 1000) break;
1740 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1741 printk(KERN_INFO "ready after %d loops\n", count);
1742 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1744 count = 0;
1745 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1746 count++;
1747 if (count > 1000) break;
1748 udelay(10);
1750 printk(KERN_INFO "ready after %d loops\n", count);
1751 cosa_putdata16(cosa, addr &0xffff);
1752 flags1 = claim_dma_lock();
1753 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1754 enable_dma(cosa->dma);
1755 release_dma_lock(flags1);
1756 } else {
1757 /* start the DMA */
1758 flags1 = claim_dma_lock();
1759 disable_dma(cosa->dma);
1760 clear_dma_ff(cosa->dma);
1761 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1762 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1763 set_dma_count(cosa->dma, cosa->txsize);
1764 enable_dma(cosa->dma);
1765 release_dma_lock(flags1);
1767 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1768 #ifdef DEBUG_IO
1769 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1770 #endif
1771 spin_unlock_irqrestore(&cosa->lock, flags);
1774 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1776 unsigned long flags;
1777 #ifdef DEBUG_IRQS
1778 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1779 #endif
1781 spin_lock_irqsave(&cosa->lock, flags);
1782 set_bit(RXBIT, &cosa->rxtx);
1784 if (is_8bit(cosa)) {
1785 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1786 set_bit(IRQBIT, &cosa->rxtx);
1787 cosa->rxsize = cosa_getdata8(cosa) <<8;
1788 #ifdef DEBUG_IO
1789 debug_data_in(cosa, cosa->rxsize >> 8);
1790 #endif
1791 spin_unlock_irqrestore(&cosa->lock, flags);
1792 return;
1793 } else {
1794 clear_bit(IRQBIT, &cosa->rxtx);
1795 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1796 #ifdef DEBUG_IO
1797 debug_data_in(cosa, cosa->rxsize & 0xff);
1798 #endif
1799 #if 0
1800 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1801 cosa->num, cosa->rxsize);
1802 #endif
1804 } else {
1805 cosa->rxsize = cosa_getdata16(cosa);
1806 #ifdef DEBUG_IO
1807 debug_data_in(cosa, cosa->rxsize);
1808 #endif
1809 #if 0
1810 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1811 cosa->num, cosa->rxsize);
1812 #endif
1814 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1815 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1816 cosa->name, cosa->rxsize);
1817 spin_unlock_irqrestore(&cosa->lock, flags);
1818 goto reject;
1820 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1821 cosa->rxsize &= 0x1fff;
1822 spin_unlock_irqrestore(&cosa->lock, flags);
1824 cosa->rxbuf = NULL;
1825 if (cosa->rxchan->setup_rx)
1826 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1828 if (!cosa->rxbuf) {
1829 reject: /* Reject the packet */
1830 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1831 cosa->num, cosa->rxchan->num);
1832 cosa->rxbuf = cosa->bouncebuf;
1835 /* start the DMA */
1836 flags = claim_dma_lock();
1837 disable_dma(cosa->dma);
1838 clear_dma_ff(cosa->dma);
1839 set_dma_mode(cosa->dma, DMA_MODE_READ);
1840 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1841 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1842 } else {
1843 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1845 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1846 enable_dma(cosa->dma);
1847 release_dma_lock(flags);
1848 spin_lock_irqsave(&cosa->lock, flags);
1849 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1850 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1851 cosa_putdata8(cosa, DRIVER_RX_READY);
1852 #ifdef DEBUG_IO
1853 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1854 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1855 debug_data_cmd(cosa, DRIVER_RX_READY);
1856 #endif
1857 spin_unlock_irqrestore(&cosa->lock, flags);
1860 static void inline eot_interrupt(struct cosa_data *cosa, int status)
1862 unsigned long flags, flags1;
1863 spin_lock_irqsave(&cosa->lock, flags);
1864 flags1 = claim_dma_lock();
1865 disable_dma(cosa->dma);
1866 clear_dma_ff(cosa->dma);
1867 release_dma_lock(flags1);
1868 if (test_bit(TXBIT, &cosa->rxtx)) {
1869 struct channel_data *chan = cosa->chan+cosa->txchan;
1870 if (chan->tx_done)
1871 if (chan->tx_done(chan, cosa->txsize))
1872 clear_bit(chan->num, &cosa->txbitmap);
1873 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1874 #ifdef DEBUG_DATA
1876 int i;
1877 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1878 cosa->rxchan->num, cosa->rxsize);
1879 for (i=0; i<cosa->rxsize; i++)
1880 printk (" %02x", cosa->rxbuf[i]&0xff);
1881 printk("\n");
1883 #endif
1884 /* Packet for unknown channel? */
1885 if (cosa->rxbuf == cosa->bouncebuf)
1886 goto out;
1887 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1888 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1889 if (cosa->rxchan->rx_done)
1890 if (cosa->rxchan->rx_done(cosa->rxchan))
1891 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1892 } else {
1893 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1894 cosa->num);
1897 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1898 * cleared anyway). We should do it as soon as possible
1899 * so that we can tell the COSA we are done and to give it a time
1900 * for recovery.
1902 out:
1903 cosa->rxtx = 0;
1904 put_driver_status_nolock(cosa);
1905 spin_unlock_irqrestore(&cosa->lock, flags);
1908 static void cosa_interrupt(int irq, void *cosa_, struct pt_regs *regs)
1910 unsigned status;
1911 int count = 0;
1912 struct cosa_data *cosa = cosa_;
1913 again:
1914 status = cosa_getstatus(cosa);
1915 #ifdef DEBUG_IRQS
1916 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1917 status & 0xff);
1918 #endif
1919 #ifdef DEBUG_IO
1920 debug_status_in(cosa, status);
1921 #endif
1922 switch (status & SR_CMD_FROM_SRP_MASK) {
1923 case SR_DOWN_REQUEST:
1924 tx_interrupt(cosa, status);
1925 break;
1926 case SR_UP_REQUEST:
1927 rx_interrupt(cosa, status);
1928 break;
1929 case SR_END_OF_TRANSFER:
1930 eot_interrupt(cosa, status);
1931 break;
1932 default:
1933 /* We may be too fast for SRP. Try to wait a bit more. */
1934 if (count++ < 100) {
1935 udelay(100);
1936 goto again;
1938 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1939 cosa->num, status & 0xff, count);
1941 #ifdef DEBUG_IRQS
1942 if (count)
1943 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1944 cosa->name, count);
1945 else
1946 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
1947 #endif
1951 /* ---------- I/O debugging routines ---------- */
1953 * These routines can be used to monitor COSA/SRP I/O and to printk()
1954 * the data being transfered on the data and status I/O port in a
1955 * readable way.
1958 #ifdef DEBUG_IO
1959 static void debug_status_in(struct cosa_data *cosa, int status)
1961 char *s;
1962 switch(status & SR_CMD_FROM_SRP_MASK) {
1963 case SR_UP_REQUEST:
1964 s = "RX_REQ";
1965 break;
1966 case SR_DOWN_REQUEST:
1967 s = "TX_REQ";
1968 break;
1969 case SR_END_OF_TRANSFER:
1970 s = "ET_REQ";
1971 break;
1972 default:
1973 s = "NO_REQ";
1974 break;
1976 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
1977 cosa->name,
1978 status,
1979 status & SR_USR_RQ ? "USR_RQ|":"",
1980 status & SR_TX_RDY ? "TX_RDY|":"",
1981 status & SR_RX_RDY ? "RX_RDY|":"",
1985 static void debug_status_out(struct cosa_data *cosa, int status)
1987 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
1988 cosa->name,
1989 status,
1990 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
1991 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
1992 status & SR_RST ? "RESET|":"",
1993 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
1994 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
1995 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
1998 static void debug_data_in(struct cosa_data *cosa, int data)
2000 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2003 static void debug_data_out(struct cosa_data *cosa, int data)
2005 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2008 static void debug_data_cmd(struct cosa_data *cosa, int data)
2010 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2011 cosa->name, data,
2012 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2013 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2015 #endif
2017 /* EOF -- this file has not been truncated */