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