headers: remove sched.h from interrupt.h
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wan / cosa.c
blobe2c33c06190bb1fa0975b634b765de70d22ad71f
1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
3 /*
4 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
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 <linux/smp_lock.h>
93 #include <asm/io.h>
94 #include <asm/dma.h>
95 #include <asm/byteorder.h>
97 #undef COSA_SLOW_IO /* for testing purposes only */
99 #include "cosa.h"
101 /* Maximum length of the identification string. */
102 #define COSA_MAX_ID_STRING 128
104 /* Maximum length of the channel name */
105 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
107 /* Per-channel data structure */
109 struct channel_data {
110 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
111 int num; /* Number of the channel */
112 struct cosa_data *cosa; /* Pointer to the per-card structure */
113 int txsize; /* Size of transmitted data */
114 char *txbuf; /* Transmit buffer */
115 char name[COSA_MAX_NAME]; /* channel name */
117 /* The HW layer interface */
118 /* routine called from the RX interrupt */
119 char *(*setup_rx)(struct channel_data *channel, int size);
120 /* routine called when the RX is done (from the EOT interrupt) */
121 int (*rx_done)(struct channel_data *channel);
122 /* routine called when the TX is done (from the EOT interrupt) */
123 int (*tx_done)(struct channel_data *channel, int size);
125 /* Character device parts */
126 struct mutex rlock;
127 struct semaphore wsem;
128 char *rxdata;
129 int rxsize;
130 wait_queue_head_t txwaitq, rxwaitq;
131 int tx_status, rx_status;
133 /* generic HDLC device parts */
134 struct net_device *netdev;
135 struct sk_buff *rx_skb, *tx_skb;
138 /* cosa->firmware_status bits */
139 #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
140 #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
141 #define COSA_FW_START (1<<2) /* Is the microcode running? */
143 struct cosa_data {
144 int num; /* Card number */
145 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
146 unsigned int datareg, statusreg; /* I/O ports */
147 unsigned short irq, dma; /* IRQ and DMA number */
148 unsigned short startaddr; /* Firmware start address */
149 unsigned short busmaster; /* Use busmastering? */
150 int nchannels; /* # of channels on this card */
151 int driver_status; /* For communicating with firmware */
152 int firmware_status; /* Downloaded, reseted, etc. */
153 unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
154 unsigned long rxtx; /* RX or TX in progress? */
155 int enabled;
156 int usage; /* usage count */
157 int txchan, txsize, rxsize;
158 struct channel_data *rxchan;
159 char *bouncebuf;
160 char *txbuf, *rxbuf;
161 struct channel_data *chan;
162 spinlock_t lock; /* For exclusive operations on this structure */
163 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
164 char *type; /* card type */
168 * Define this if you want all the possible ports to be autoprobed.
169 * It is here but it probably is not a good idea to use this.
171 /* #define COSA_ISA_AUTOPROBE 1 */
174 * Character device major number. 117 was allocated for us.
175 * The value of 0 means to allocate a first free one.
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 /* FIXME: 0xfc for 8-channel version */
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 /* FIXME: I don't know this exactly */
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 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 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 .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();
514 * Enable interrupt on tx buffer empty (it sure is)
515 * really sure ?
516 * FIXME: When this code is not used as module, we should
517 * probably call udelay() instead of the interruptible sleep.
519 set_current_state(TASK_INTERRUPTIBLE);
520 cosa_putstatus(cosa, SR_TX_INT_ENA);
521 schedule_timeout(30);
522 irq = probe_irq_off(irqs);
523 /* Disable all IRQs from the card */
524 cosa_putstatus(cosa, 0);
525 /* Empty the received data register */
526 cosa_getdata8(cosa);
528 if (irq < 0) {
529 printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
530 irq, cosa->datareg);
531 err = -1;
532 goto err_out;
534 if (irq == 0) {
535 printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
536 cosa->datareg);
537 /* return -1; */
541 cosa->irq = irq;
542 cosa->num = nr_cards;
543 cosa->usage = 0;
544 cosa->nchannels = 2; /* FIXME: how to determine this? */
546 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
547 err = -1;
548 goto err_out;
550 if (request_dma(cosa->dma, cosa->type)) {
551 err = -1;
552 goto err_out1;
555 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
556 if (!cosa->bouncebuf) {
557 err = -ENOMEM;
558 goto err_out2;
560 sprintf(cosa->name, "cosa%d", cosa->num);
562 /* Initialize the per-channel data */
563 cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
564 if (!cosa->chan) {
565 err = -ENOMEM;
566 goto err_out3;
569 for (i = 0; i < cosa->nchannels; i++) {
570 struct channel_data *chan = &cosa->chan[i];
572 chan->cosa = cosa;
573 chan->num = i;
574 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
576 /* Initialize the chardev data structures */
577 mutex_init(&chan->rlock);
578 init_MUTEX(&chan->wsem);
580 /* Register the network interface */
581 if (!(chan->netdev = alloc_hdlcdev(chan))) {
582 printk(KERN_WARNING "%s: alloc_hdlcdev failed.\n",
583 chan->name);
584 goto err_hdlcdev;
586 dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
587 dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
588 chan->netdev->netdev_ops = &cosa_ops;
589 chan->netdev->watchdog_timeo = TX_TIMEOUT;
590 chan->netdev->base_addr = chan->cosa->datareg;
591 chan->netdev->irq = chan->cosa->irq;
592 chan->netdev->dma = chan->cosa->dma;
593 if (register_hdlc_device(chan->netdev)) {
594 printk(KERN_WARNING "%s: register_hdlc_device()"
595 " failed.\n", chan->netdev->name);
596 free_netdev(chan->netdev);
597 goto err_hdlcdev;
601 printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
602 cosa->num, cosa->id_string, cosa->type,
603 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
605 return nr_cards++;
607 err_hdlcdev:
608 while (i-- > 0) {
609 unregister_hdlc_device(cosa->chan[i].netdev);
610 free_netdev(cosa->chan[i].netdev);
612 kfree(cosa->chan);
613 err_out3:
614 kfree(cosa->bouncebuf);
615 err_out2:
616 free_dma(cosa->dma);
617 err_out1:
618 free_irq(cosa->irq, cosa);
619 err_out:
620 release_region(cosa->datareg,is_8bit(cosa)?2:4);
621 printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
622 cosa->num);
623 return err;
627 /*---------- network device ---------- */
629 static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
630 unsigned short parity)
632 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
633 return 0;
634 return -EINVAL;
637 static int cosa_net_open(struct net_device *dev)
639 struct channel_data *chan = dev_to_chan(dev);
640 int err;
641 unsigned long flags;
643 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
644 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
645 chan->cosa->name, chan->cosa->firmware_status);
646 return -EPERM;
648 spin_lock_irqsave(&chan->cosa->lock, flags);
649 if (chan->usage != 0) {
650 printk(KERN_WARNING "%s: cosa_net_open called with usage count"
651 " %d\n", chan->name, chan->usage);
652 spin_unlock_irqrestore(&chan->cosa->lock, flags);
653 return -EBUSY;
655 chan->setup_rx = cosa_net_setup_rx;
656 chan->tx_done = cosa_net_tx_done;
657 chan->rx_done = cosa_net_rx_done;
658 chan->usage = -1;
659 chan->cosa->usage++;
660 spin_unlock_irqrestore(&chan->cosa->lock, flags);
662 err = hdlc_open(dev);
663 if (err) {
664 spin_lock_irqsave(&chan->cosa->lock, flags);
665 chan->usage = 0;
666 chan->cosa->usage--;
667 spin_unlock_irqrestore(&chan->cosa->lock, flags);
668 return err;
671 netif_start_queue(dev);
672 cosa_enable_rx(chan);
673 return 0;
676 static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
677 struct net_device *dev)
679 struct channel_data *chan = dev_to_chan(dev);
681 netif_stop_queue(dev);
683 chan->tx_skb = skb;
684 cosa_start_tx(chan, skb->data, skb->len);
685 return NETDEV_TX_OK;
688 static void cosa_net_timeout(struct net_device *dev)
690 struct channel_data *chan = dev_to_chan(dev);
692 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
693 chan->netdev->stats.rx_errors++;
694 chan->netdev->stats.rx_missed_errors++;
695 } else {
696 chan->netdev->stats.tx_errors++;
697 chan->netdev->stats.tx_aborted_errors++;
699 cosa_kick(chan->cosa);
700 if (chan->tx_skb) {
701 dev_kfree_skb(chan->tx_skb);
702 chan->tx_skb = NULL;
704 netif_wake_queue(dev);
707 static int cosa_net_close(struct net_device *dev)
709 struct channel_data *chan = dev_to_chan(dev);
710 unsigned long flags;
712 netif_stop_queue(dev);
713 hdlc_close(dev);
714 cosa_disable_rx(chan);
715 spin_lock_irqsave(&chan->cosa->lock, flags);
716 if (chan->rx_skb) {
717 kfree_skb(chan->rx_skb);
718 chan->rx_skb = NULL;
720 if (chan->tx_skb) {
721 kfree_skb(chan->tx_skb);
722 chan->tx_skb = NULL;
724 chan->usage = 0;
725 chan->cosa->usage--;
726 spin_unlock_irqrestore(&chan->cosa->lock, flags);
727 return 0;
730 static char *cosa_net_setup_rx(struct channel_data *chan, int size)
733 * We can safely fall back to non-dma-able memory, because we have
734 * the cosa->bouncebuf pre-allocated.
736 kfree_skb(chan->rx_skb);
737 chan->rx_skb = dev_alloc_skb(size);
738 if (chan->rx_skb == NULL) {
739 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
740 chan->name);
741 chan->netdev->stats.rx_dropped++;
742 return NULL;
744 chan->netdev->trans_start = jiffies;
745 return skb_put(chan->rx_skb, size);
748 static int cosa_net_rx_done(struct channel_data *chan)
750 if (!chan->rx_skb) {
751 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
752 chan->name);
753 chan->netdev->stats.rx_errors++;
754 chan->netdev->stats.rx_frame_errors++;
755 return 0;
757 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
758 chan->rx_skb->dev = chan->netdev;
759 skb_reset_mac_header(chan->rx_skb);
760 chan->netdev->stats.rx_packets++;
761 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
762 netif_rx(chan->rx_skb);
763 chan->rx_skb = NULL;
764 return 0;
767 /* ARGSUSED */
768 static int cosa_net_tx_done(struct channel_data *chan, int size)
770 if (!chan->tx_skb) {
771 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
772 chan->name);
773 chan->netdev->stats.tx_errors++;
774 chan->netdev->stats.tx_aborted_errors++;
775 return 1;
777 dev_kfree_skb_irq(chan->tx_skb);
778 chan->tx_skb = NULL;
779 chan->netdev->stats.tx_packets++;
780 chan->netdev->stats.tx_bytes += size;
781 netif_wake_queue(chan->netdev);
782 return 1;
785 /*---------- Character device ---------- */
787 static ssize_t cosa_read(struct file *file,
788 char __user *buf, size_t count, loff_t *ppos)
790 DECLARE_WAITQUEUE(wait, current);
791 unsigned long flags;
792 struct channel_data *chan = file->private_data;
793 struct cosa_data *cosa = chan->cosa;
794 char *kbuf;
796 if (!(cosa->firmware_status & COSA_FW_START)) {
797 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
798 cosa->name, cosa->firmware_status);
799 return -EPERM;
801 if (mutex_lock_interruptible(&chan->rlock))
802 return -ERESTARTSYS;
804 if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
805 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
806 mutex_unlock(&chan->rlock);
807 return -ENOMEM;
810 chan->rx_status = 0;
811 cosa_enable_rx(chan);
812 spin_lock_irqsave(&cosa->lock, flags);
813 add_wait_queue(&chan->rxwaitq, &wait);
814 while(!chan->rx_status) {
815 current->state = TASK_INTERRUPTIBLE;
816 spin_unlock_irqrestore(&cosa->lock, flags);
817 schedule();
818 spin_lock_irqsave(&cosa->lock, flags);
819 if (signal_pending(current) && chan->rx_status == 0) {
820 chan->rx_status = 1;
821 remove_wait_queue(&chan->rxwaitq, &wait);
822 current->state = TASK_RUNNING;
823 spin_unlock_irqrestore(&cosa->lock, flags);
824 mutex_unlock(&chan->rlock);
825 return -ERESTARTSYS;
828 remove_wait_queue(&chan->rxwaitq, &wait);
829 current->state = TASK_RUNNING;
830 kbuf = chan->rxdata;
831 count = chan->rxsize;
832 spin_unlock_irqrestore(&cosa->lock, flags);
833 mutex_unlock(&chan->rlock);
835 if (copy_to_user(buf, kbuf, count)) {
836 kfree(kbuf);
837 return -EFAULT;
839 kfree(kbuf);
840 return count;
843 static char *chrdev_setup_rx(struct channel_data *chan, int size)
845 /* Expect size <= COSA_MTU */
846 chan->rxsize = size;
847 return chan->rxdata;
850 static int chrdev_rx_done(struct channel_data *chan)
852 if (chan->rx_status) { /* Reader has died */
853 kfree(chan->rxdata);
854 up(&chan->wsem);
856 chan->rx_status = 1;
857 wake_up_interruptible(&chan->rxwaitq);
858 return 1;
862 static ssize_t cosa_write(struct file *file,
863 const char __user *buf, size_t count, loff_t *ppos)
865 DECLARE_WAITQUEUE(wait, current);
866 struct channel_data *chan = file->private_data;
867 struct cosa_data *cosa = chan->cosa;
868 unsigned long flags;
869 char *kbuf;
871 if (!(cosa->firmware_status & COSA_FW_START)) {
872 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
873 cosa->name, cosa->firmware_status);
874 return -EPERM;
876 if (down_interruptible(&chan->wsem))
877 return -ERESTARTSYS;
879 if (count > COSA_MTU)
880 count = COSA_MTU;
882 /* Allocate the buffer */
883 if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
884 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
885 cosa->name);
886 up(&chan->wsem);
887 return -ENOMEM;
889 if (copy_from_user(kbuf, buf, count)) {
890 up(&chan->wsem);
891 kfree(kbuf);
892 return -EFAULT;
894 chan->tx_status=0;
895 cosa_start_tx(chan, kbuf, count);
897 spin_lock_irqsave(&cosa->lock, flags);
898 add_wait_queue(&chan->txwaitq, &wait);
899 while(!chan->tx_status) {
900 current->state = TASK_INTERRUPTIBLE;
901 spin_unlock_irqrestore(&cosa->lock, flags);
902 schedule();
903 spin_lock_irqsave(&cosa->lock, flags);
904 if (signal_pending(current) && chan->tx_status == 0) {
905 chan->tx_status = 1;
906 remove_wait_queue(&chan->txwaitq, &wait);
907 current->state = TASK_RUNNING;
908 chan->tx_status = 1;
909 spin_unlock_irqrestore(&cosa->lock, flags);
910 return -ERESTARTSYS;
913 remove_wait_queue(&chan->txwaitq, &wait);
914 current->state = TASK_RUNNING;
915 up(&chan->wsem);
916 spin_unlock_irqrestore(&cosa->lock, flags);
917 kfree(kbuf);
918 return count;
921 static int chrdev_tx_done(struct channel_data *chan, int size)
923 if (chan->tx_status) { /* Writer was interrupted */
924 kfree(chan->txbuf);
925 up(&chan->wsem);
927 chan->tx_status = 1;
928 wake_up_interruptible(&chan->txwaitq);
929 return 1;
932 static unsigned int cosa_poll(struct file *file, poll_table *poll)
934 printk(KERN_INFO "cosa_poll is here\n");
935 return 0;
938 static int cosa_open(struct inode *inode, struct file *file)
940 struct cosa_data *cosa;
941 struct channel_data *chan;
942 unsigned long flags;
943 int n;
944 int ret = 0;
946 lock_kernel();
947 if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
948 >= nr_cards) {
949 ret = -ENODEV;
950 goto out;
952 cosa = cosa_cards+n;
954 if ((n=iminor(file->f_path.dentry->d_inode)
955 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
956 ret = -ENODEV;
957 goto out;
959 chan = cosa->chan + n;
961 file->private_data = chan;
963 spin_lock_irqsave(&cosa->lock, flags);
965 if (chan->usage < 0) { /* in netdev mode */
966 spin_unlock_irqrestore(&cosa->lock, flags);
967 ret = -EBUSY;
968 goto out;
970 cosa->usage++;
971 chan->usage++;
973 chan->tx_done = chrdev_tx_done;
974 chan->setup_rx = chrdev_setup_rx;
975 chan->rx_done = chrdev_rx_done;
976 spin_unlock_irqrestore(&cosa->lock, flags);
977 out:
978 unlock_kernel();
979 return ret;
982 static int cosa_release(struct inode *inode, struct file *file)
984 struct channel_data *channel = file->private_data;
985 struct cosa_data *cosa;
986 unsigned long flags;
988 cosa = channel->cosa;
989 spin_lock_irqsave(&cosa->lock, flags);
990 cosa->usage--;
991 channel->usage--;
992 spin_unlock_irqrestore(&cosa->lock, flags);
993 return 0;
996 #ifdef COSA_FASYNC_WORKING
997 static struct fasync_struct *fasync[256] = { NULL, };
999 /* To be done ... */
1000 static int cosa_fasync(struct inode *inode, struct file *file, int on)
1002 int port = iminor(inode);
1004 return fasync_helper(inode, file, on, &fasync[port]);
1006 #endif
1009 /* ---------- Ioctls ---------- */
1012 * Ioctl subroutines can safely be made inline, because they are called
1013 * only from cosa_ioctl().
1015 static inline int cosa_reset(struct cosa_data *cosa)
1017 char idstring[COSA_MAX_ID_STRING];
1018 if (cosa->usage > 1)
1019 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1020 cosa->num, cosa->usage);
1021 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1022 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1023 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1024 return -EIO;
1026 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1027 idstring);
1028 cosa->firmware_status |= COSA_FW_RESET;
1029 return 0;
1032 /* High-level function to download data into COSA memory. Calls download() */
1033 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1035 struct cosa_download d;
1036 int i;
1038 if (cosa->usage > 1)
1039 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1040 cosa->name, cosa->usage);
1041 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1042 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1043 cosa->name, cosa->firmware_status);
1044 return -EPERM;
1047 if (copy_from_user(&d, arg, sizeof(d)))
1048 return -EFAULT;
1050 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1051 return -EINVAL;
1052 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1053 return -EINVAL;
1056 /* If something fails, force the user to reset the card */
1057 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1059 i = download(cosa, d.code, d.len, d.addr);
1060 if (i < 0) {
1061 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1062 cosa->num, i);
1063 return -EIO;
1065 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1066 cosa->num, d.len, d.addr);
1067 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1068 return 0;
1071 /* High-level function to read COSA memory. Calls readmem() */
1072 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1074 struct cosa_download d;
1075 int i;
1077 if (cosa->usage > 1)
1078 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1079 "cosa->usage > 1 (%d). Odd things may happen.\n",
1080 cosa->num, cosa->usage);
1081 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1082 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1083 cosa->name, cosa->firmware_status);
1084 return -EPERM;
1087 if (copy_from_user(&d, arg, sizeof(d)))
1088 return -EFAULT;
1090 /* If something fails, force the user to reset the card */
1091 cosa->firmware_status &= ~COSA_FW_RESET;
1093 i = readmem(cosa, d.code, d.len, d.addr);
1094 if (i < 0) {
1095 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1096 cosa->num, i);
1097 return -EIO;
1099 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1100 cosa->num, d.len, d.addr);
1101 cosa->firmware_status |= COSA_FW_RESET;
1102 return 0;
1105 /* High-level function to start microcode. Calls startmicrocode(). */
1106 static inline int cosa_start(struct cosa_data *cosa, int address)
1108 int i;
1110 if (cosa->usage > 1)
1111 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1112 cosa->num, cosa->usage);
1114 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1115 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1116 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1117 cosa->name, cosa->firmware_status);
1118 return -EPERM;
1120 cosa->firmware_status &= ~COSA_FW_RESET;
1121 if ((i=startmicrocode(cosa, address)) < 0) {
1122 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1123 cosa->num, address, i);
1124 return -EIO;
1126 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1127 cosa->num, address);
1128 cosa->startaddr = address;
1129 cosa->firmware_status |= COSA_FW_START;
1130 return 0;
1133 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1134 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1136 int l = strlen(cosa->id_string)+1;
1137 if (copy_to_user(string, cosa->id_string, l))
1138 return -EFAULT;
1139 return l;
1142 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1143 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1145 int l = strlen(cosa->type)+1;
1146 if (copy_to_user(string, cosa->type, l))
1147 return -EFAULT;
1148 return l;
1151 static int cosa_ioctl_common(struct cosa_data *cosa,
1152 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1154 void __user *argp = (void __user *)arg;
1155 switch(cmd) {
1156 case COSAIORSET: /* Reset the device */
1157 if (!capable(CAP_NET_ADMIN))
1158 return -EACCES;
1159 return cosa_reset(cosa);
1160 case COSAIOSTRT: /* Start the firmware */
1161 if (!capable(CAP_SYS_RAWIO))
1162 return -EACCES;
1163 return cosa_start(cosa, arg);
1164 case COSAIODOWNLD: /* Download the firmware */
1165 if (!capable(CAP_SYS_RAWIO))
1166 return -EACCES;
1168 return cosa_download(cosa, argp);
1169 case COSAIORMEM:
1170 if (!capable(CAP_SYS_RAWIO))
1171 return -EACCES;
1172 return cosa_readmem(cosa, argp);
1173 case COSAIORTYPE:
1174 return cosa_gettype(cosa, argp);
1175 case COSAIORIDSTR:
1176 return cosa_getidstr(cosa, argp);
1177 case COSAIONRCARDS:
1178 return nr_cards;
1179 case COSAIONRCHANS:
1180 return cosa->nchannels;
1181 case COSAIOBMSET:
1182 if (!capable(CAP_SYS_RAWIO))
1183 return -EACCES;
1184 if (is_8bit(cosa))
1185 return -EINVAL;
1186 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1187 return -EINVAL;
1188 cosa->busmaster = arg;
1189 return 0;
1190 case COSAIOBMGET:
1191 return cosa->busmaster;
1193 return -ENOIOCTLCMD;
1196 static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1198 int rv;
1199 struct channel_data *chan = dev_to_chan(dev);
1200 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1201 (unsigned long)ifr->ifr_data);
1202 if (rv != -ENOIOCTLCMD)
1203 return rv;
1204 return hdlc_ioctl(dev, ifr, cmd);
1207 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1208 unsigned int cmd, unsigned long arg)
1210 struct channel_data *channel = file->private_data;
1211 struct cosa_data *cosa = channel->cosa;
1212 return cosa_ioctl_common(cosa, channel, cmd, arg);
1216 /*---------- HW layer interface ---------- */
1219 * The higher layer can bind itself to the HW layer by setting the callbacks
1220 * in the channel_data structure and by using these routines.
1222 static void cosa_enable_rx(struct channel_data *chan)
1224 struct cosa_data *cosa = chan->cosa;
1226 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1227 put_driver_status(cosa);
1230 static void cosa_disable_rx(struct channel_data *chan)
1232 struct cosa_data *cosa = chan->cosa;
1234 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1235 put_driver_status(cosa);
1239 * FIXME: This routine probably should check for cosa_start_tx() called when
1240 * the previous transmit is still unfinished. In this case the non-zero
1241 * return value should indicate to the caller that the queuing(sp?) up
1242 * the transmit has failed.
1244 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1246 struct cosa_data *cosa = chan->cosa;
1247 unsigned long flags;
1248 #ifdef DEBUG_DATA
1249 int i;
1251 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1252 chan->num, len);
1253 for (i=0; i<len; i++)
1254 printk(" %02x", buf[i]&0xff);
1255 printk("\n");
1256 #endif
1257 spin_lock_irqsave(&cosa->lock, flags);
1258 chan->txbuf = buf;
1259 chan->txsize = len;
1260 if (len > COSA_MTU)
1261 chan->txsize = COSA_MTU;
1262 spin_unlock_irqrestore(&cosa->lock, flags);
1264 /* Tell the firmware we are ready */
1265 set_bit(chan->num, &cosa->txbitmap);
1266 put_driver_status(cosa);
1268 return 0;
1271 static void put_driver_status(struct cosa_data *cosa)
1273 unsigned long flags;
1274 int status;
1276 spin_lock_irqsave(&cosa->lock, flags);
1278 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1279 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1280 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1281 &DRIVER_TXMAP_MASK : 0);
1282 if (!cosa->rxtx) {
1283 if (cosa->rxbitmap|cosa->txbitmap) {
1284 if (!cosa->enabled) {
1285 cosa_putstatus(cosa, SR_RX_INT_ENA);
1286 #ifdef DEBUG_IO
1287 debug_status_out(cosa, SR_RX_INT_ENA);
1288 #endif
1289 cosa->enabled = 1;
1291 } else if (cosa->enabled) {
1292 cosa->enabled = 0;
1293 cosa_putstatus(cosa, 0);
1294 #ifdef DEBUG_IO
1295 debug_status_out(cosa, 0);
1296 #endif
1298 cosa_putdata8(cosa, status);
1299 #ifdef DEBUG_IO
1300 debug_data_cmd(cosa, status);
1301 #endif
1303 spin_unlock_irqrestore(&cosa->lock, flags);
1306 static void put_driver_status_nolock(struct cosa_data *cosa)
1308 int status;
1310 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1311 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1312 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1313 &DRIVER_TXMAP_MASK : 0);
1315 if (cosa->rxbitmap|cosa->txbitmap) {
1316 cosa_putstatus(cosa, SR_RX_INT_ENA);
1317 #ifdef DEBUG_IO
1318 debug_status_out(cosa, SR_RX_INT_ENA);
1319 #endif
1320 cosa->enabled = 1;
1321 } else {
1322 cosa_putstatus(cosa, 0);
1323 #ifdef DEBUG_IO
1324 debug_status_out(cosa, 0);
1325 #endif
1326 cosa->enabled = 0;
1328 cosa_putdata8(cosa, status);
1329 #ifdef DEBUG_IO
1330 debug_data_cmd(cosa, status);
1331 #endif
1335 * The "kickme" function: When the DMA times out, this is called to
1336 * clean up the driver status.
1337 * FIXME: Preliminary support, the interface is probably wrong.
1339 static void cosa_kick(struct cosa_data *cosa)
1341 unsigned long flags, flags1;
1342 char *s = "(probably) IRQ";
1344 if (test_bit(RXBIT, &cosa->rxtx))
1345 s = "RX DMA";
1346 if (test_bit(TXBIT, &cosa->rxtx))
1347 s = "TX DMA";
1349 printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
1350 spin_lock_irqsave(&cosa->lock, flags);
1351 cosa->rxtx = 0;
1353 flags1 = claim_dma_lock();
1354 disable_dma(cosa->dma);
1355 clear_dma_ff(cosa->dma);
1356 release_dma_lock(flags1);
1358 /* FIXME: Anything else? */
1359 udelay(100);
1360 cosa_putstatus(cosa, 0);
1361 udelay(100);
1362 (void) cosa_getdata8(cosa);
1363 udelay(100);
1364 cosa_putdata8(cosa, 0);
1365 udelay(100);
1366 put_driver_status_nolock(cosa);
1367 spin_unlock_irqrestore(&cosa->lock, flags);
1371 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1372 * physical memory and doesn't span the 64k boundary. For now it seems
1373 * SKB's never do this, but we'll check this anyway.
1375 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1377 static int count;
1378 unsigned long b = (unsigned long)buf;
1379 if (b+len >= MAX_DMA_ADDRESS)
1380 return 0;
1381 if ((b^ (b+len)) & 0x10000) {
1382 if (count++ < 5)
1383 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1384 chan->name);
1385 return 0;
1387 return 1;
1391 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1394 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1395 * drivers need to say 4-digit hex number meaning start address of the microcode
1396 * separated by a single space. Monitor replies by saying " =". Now driver
1397 * has to write 4-digit hex number meaning the last byte address ended
1398 * by a single space. Monitor has to reply with a space. Now the download
1399 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1401 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1403 int i;
1405 if (put_wait_data(cosa, 'w') == -1) return -1;
1406 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1407 if (get_wait_data(cosa) != '=') return -3;
1409 if (puthexnumber(cosa, address) < 0) return -4;
1410 if (put_wait_data(cosa, ' ') == -1) return -10;
1411 if (get_wait_data(cosa) != ' ') return -11;
1412 if (get_wait_data(cosa) != '=') return -12;
1414 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1415 if (put_wait_data(cosa, ' ') == -1) return -18;
1416 if (get_wait_data(cosa) != ' ') return -19;
1418 while (length--) {
1419 char c;
1420 #ifndef SRP_DOWNLOAD_AT_BOOT
1421 if (get_user(c, microcode))
1422 return -23; /* ??? */
1423 #else
1424 c = *microcode;
1425 #endif
1426 if (put_wait_data(cosa, c) == -1)
1427 return -20;
1428 microcode++;
1431 if (get_wait_data(cosa) != '\r') return -21;
1432 if (get_wait_data(cosa) != '\n') return -22;
1433 if (get_wait_data(cosa) != '.') return -23;
1434 #if 0
1435 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1436 #endif
1437 return 0;
1442 * Starting microcode is done via the "g" command of the SRP monitor.
1443 * The chat should be the following: "g" "g=" "<addr><CR>"
1444 * "<CR><CR><LF><CR><LF>".
1446 static int startmicrocode(struct cosa_data *cosa, int address)
1448 if (put_wait_data(cosa, 'g') == -1) return -1;
1449 if (get_wait_data(cosa) != 'g') return -2;
1450 if (get_wait_data(cosa) != '=') return -3;
1452 if (puthexnumber(cosa, address) < 0) return -4;
1453 if (put_wait_data(cosa, '\r') == -1) return -5;
1455 if (get_wait_data(cosa) != '\r') return -6;
1456 if (get_wait_data(cosa) != '\r') return -7;
1457 if (get_wait_data(cosa) != '\n') return -8;
1458 if (get_wait_data(cosa) != '\r') return -9;
1459 if (get_wait_data(cosa) != '\n') return -10;
1460 #if 0
1461 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1462 #endif
1463 return 0;
1467 * Reading memory is done via the "r" command of the SRP monitor.
1468 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1469 * Then driver can read the data and the conversation is finished
1470 * by SRP monitor sending "<CR><LF>." (dot at the end).
1472 * This routine is not needed during the normal operation and serves
1473 * for debugging purposes only.
1475 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1477 if (put_wait_data(cosa, 'r') == -1) return -1;
1478 if ((get_wait_data(cosa)) != 'r') return -2;
1479 if ((get_wait_data(cosa)) != '=') return -3;
1481 if (puthexnumber(cosa, address) < 0) return -4;
1482 if (put_wait_data(cosa, ' ') == -1) return -5;
1483 if (get_wait_data(cosa) != ' ') return -6;
1484 if (get_wait_data(cosa) != '=') return -7;
1486 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1487 if (put_wait_data(cosa, ' ') == -1) return -9;
1488 if (get_wait_data(cosa) != ' ') return -10;
1490 while (length--) {
1491 char c;
1492 int i;
1493 if ((i=get_wait_data(cosa)) == -1) {
1494 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1495 length);
1496 return -11;
1498 c=i;
1499 #if 1
1500 if (put_user(c, microcode))
1501 return -23; /* ??? */
1502 #else
1503 *microcode = c;
1504 #endif
1505 microcode++;
1508 if (get_wait_data(cosa) != '\r') return -21;
1509 if (get_wait_data(cosa) != '\n') return -22;
1510 if (get_wait_data(cosa) != '.') return -23;
1511 #if 0
1512 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1513 #endif
1514 return 0;
1518 * This function resets the device and reads the initial prompt
1519 * of the device's ROM monitor.
1521 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1523 int i=0, id=0, prev=0, curr=0;
1525 /* Reset the card ... */
1526 cosa_putstatus(cosa, 0);
1527 cosa_getdata8(cosa);
1528 cosa_putstatus(cosa, SR_RST);
1529 #ifdef MODULE
1530 msleep(500);
1531 #else
1532 udelay(5*100000);
1533 #endif
1534 /* Disable all IRQs from the card */
1535 cosa_putstatus(cosa, 0);
1538 * Try to read the ID string. The card then prints out the
1539 * identification string ended by the "\n\x2e".
1541 * The following loop is indexed through i (instead of id)
1542 * to avoid looping forever when for any reason
1543 * the port returns '\r', '\n' or '\x2e' permanently.
1545 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1546 if ((curr = get_wait_data(cosa)) == -1) {
1547 return -1;
1549 curr &= 0xff;
1550 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1551 idstring[id++] = curr;
1552 if (curr == 0x2e && prev == '\n')
1553 break;
1555 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1556 idstring[id] = '\0';
1557 return id;
1561 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1564 * This routine gets the data byte from the card waiting for the SR_RX_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 get_wait_data(struct cosa_data *cosa)
1570 int retries = 1000;
1572 while (--retries) {
1573 /* read data and return them */
1574 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1575 short r;
1576 r = cosa_getdata8(cosa);
1577 #if 0
1578 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1579 #endif
1580 return r;
1582 /* sleep if not ready to read */
1583 schedule_timeout_interruptible(1);
1585 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1586 cosa_getstatus(cosa));
1587 return -1;
1591 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1592 * bit to be set in a loop. It should be used in the exceptional cases
1593 * only (for example when resetting the card or downloading the firmware).
1595 static int put_wait_data(struct cosa_data *cosa, int data)
1597 int retries = 1000;
1598 while (--retries) {
1599 /* read data and return them */
1600 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1601 cosa_putdata8(cosa, data);
1602 #if 0
1603 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1604 #endif
1605 return 0;
1607 #if 0
1608 /* sleep if not ready to read */
1609 schedule_timeout_interruptible(1);
1610 #endif
1612 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1613 cosa->num, cosa_getstatus(cosa));
1614 return -1;
1618 * The following routine puts the hexadecimal number into the SRP monitor
1619 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1620 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1621 * (-2,-4,-6,-8) means that reading echo failed.
1623 static int puthexnumber(struct cosa_data *cosa, int number)
1625 char temp[5];
1626 int i;
1628 /* Well, I should probably replace this by something faster. */
1629 sprintf(temp, "%04X", number);
1630 for (i=0; i<4; i++) {
1631 if (put_wait_data(cosa, temp[i]) == -1) {
1632 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1633 cosa->num, i);
1634 return -1-2*i;
1636 if (get_wait_data(cosa) != temp[i]) {
1637 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1638 cosa->num, i);
1639 return -2-2*i;
1642 return 0;
1646 /* ---------- Interrupt routines ---------- */
1649 * There are three types of interrupt:
1650 * At the beginning of transmit - this handled is in tx_interrupt(),
1651 * at the beginning of receive - it is in rx_interrupt() and
1652 * at the end of transmit/receive - it is the eot_interrupt() function.
1653 * These functions are multiplexed by cosa_interrupt() according to the
1654 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1655 * separate functions to make it more readable. These functions are inline,
1656 * so there should be no overhead of function call.
1658 * In the COSA bus-master mode, we need to tell the card the address of a
1659 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1660 * It's time to use the bottom half :-(
1664 * Transmit interrupt routine - called when COSA is willing to obtain
1665 * data from the OS. The most tricky part of the routine is selection
1666 * of channel we (OS) want to send packet for. For SRP we should probably
1667 * use the round-robin approach. The newer COSA firmwares have a simple
1668 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1669 * channel 0 or 1 doesn't want to receive data.
1671 * It seems there is a bug in COSA firmware (need to trace it further):
1672 * When the driver status says that the kernel has no more data for transmit
1673 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1674 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1675 * the TX interrupt but does not mark the channel as ready-to-transmit.
1676 * The fix seems to be to push the packet to COSA despite its request.
1677 * We first try to obey the card's opinion, and then fall back to forced TX.
1679 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1681 unsigned long flags, flags1;
1682 #ifdef DEBUG_IRQS
1683 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1684 cosa->num, status);
1685 #endif
1686 spin_lock_irqsave(&cosa->lock, flags);
1687 set_bit(TXBIT, &cosa->rxtx);
1688 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1689 /* flow control, see the comment above */
1690 int i=0;
1691 if (!cosa->txbitmap) {
1692 printk(KERN_WARNING "%s: No channel wants data "
1693 "in TX IRQ. Expect DMA timeout.",
1694 cosa->name);
1695 put_driver_status_nolock(cosa);
1696 clear_bit(TXBIT, &cosa->rxtx);
1697 spin_unlock_irqrestore(&cosa->lock, flags);
1698 return;
1700 while(1) {
1701 cosa->txchan++;
1702 i++;
1703 if (cosa->txchan >= cosa->nchannels)
1704 cosa->txchan = 0;
1705 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1706 continue;
1707 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1708 break;
1709 /* in second pass, accept first ready-to-TX channel */
1710 if (i > cosa->nchannels) {
1711 /* Can be safely ignored */
1712 #ifdef DEBUG_IRQS
1713 printk(KERN_DEBUG "%s: Forcing TX "
1714 "to not-ready channel %d\n",
1715 cosa->name, cosa->txchan);
1716 #endif
1717 break;
1721 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1722 if (cosa_dma_able(cosa->chan+cosa->txchan,
1723 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1724 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1725 } else {
1726 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1727 cosa->txsize);
1728 cosa->txbuf = cosa->bouncebuf;
1732 if (is_8bit(cosa)) {
1733 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1734 cosa_putstatus(cosa, SR_TX_INT_ENA);
1735 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1736 ((cosa->txsize >> 8) & 0x1f));
1737 #ifdef DEBUG_IO
1738 debug_status_out(cosa, SR_TX_INT_ENA);
1739 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1740 ((cosa->txsize >> 8) & 0x1f));
1741 debug_data_in(cosa, cosa_getdata8(cosa));
1742 #else
1743 cosa_getdata8(cosa);
1744 #endif
1745 set_bit(IRQBIT, &cosa->rxtx);
1746 spin_unlock_irqrestore(&cosa->lock, flags);
1747 return;
1748 } else {
1749 clear_bit(IRQBIT, &cosa->rxtx);
1750 cosa_putstatus(cosa, 0);
1751 cosa_putdata8(cosa, cosa->txsize&0xff);
1752 #ifdef DEBUG_IO
1753 debug_status_out(cosa, 0);
1754 debug_data_out(cosa, cosa->txsize&0xff);
1755 #endif
1757 } else {
1758 cosa_putstatus(cosa, SR_TX_INT_ENA);
1759 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1760 | (cosa->txsize & 0x1fff));
1761 #ifdef DEBUG_IO
1762 debug_status_out(cosa, SR_TX_INT_ENA);
1763 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1764 | (cosa->txsize & 0x1fff));
1765 debug_data_in(cosa, cosa_getdata8(cosa));
1766 debug_status_out(cosa, 0);
1767 #else
1768 cosa_getdata8(cosa);
1769 #endif
1770 cosa_putstatus(cosa, 0);
1773 if (cosa->busmaster) {
1774 unsigned long addr = virt_to_bus(cosa->txbuf);
1775 int count=0;
1776 printk(KERN_INFO "busmaster IRQ\n");
1777 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1778 count++;
1779 udelay(10);
1780 if (count > 1000) break;
1782 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1783 printk(KERN_INFO "ready after %d loops\n", count);
1784 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1786 count = 0;
1787 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1788 count++;
1789 if (count > 1000) break;
1790 udelay(10);
1792 printk(KERN_INFO "ready after %d loops\n", count);
1793 cosa_putdata16(cosa, addr &0xffff);
1794 flags1 = claim_dma_lock();
1795 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1796 enable_dma(cosa->dma);
1797 release_dma_lock(flags1);
1798 } else {
1799 /* start the DMA */
1800 flags1 = claim_dma_lock();
1801 disable_dma(cosa->dma);
1802 clear_dma_ff(cosa->dma);
1803 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1804 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1805 set_dma_count(cosa->dma, cosa->txsize);
1806 enable_dma(cosa->dma);
1807 release_dma_lock(flags1);
1809 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1810 #ifdef DEBUG_IO
1811 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1812 #endif
1813 spin_unlock_irqrestore(&cosa->lock, flags);
1816 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1818 unsigned long flags;
1819 #ifdef DEBUG_IRQS
1820 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1821 #endif
1823 spin_lock_irqsave(&cosa->lock, flags);
1824 set_bit(RXBIT, &cosa->rxtx);
1826 if (is_8bit(cosa)) {
1827 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1828 set_bit(IRQBIT, &cosa->rxtx);
1829 put_driver_status_nolock(cosa);
1830 cosa->rxsize = cosa_getdata8(cosa) <<8;
1831 #ifdef DEBUG_IO
1832 debug_data_in(cosa, cosa->rxsize >> 8);
1833 #endif
1834 spin_unlock_irqrestore(&cosa->lock, flags);
1835 return;
1836 } else {
1837 clear_bit(IRQBIT, &cosa->rxtx);
1838 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1839 #ifdef DEBUG_IO
1840 debug_data_in(cosa, cosa->rxsize & 0xff);
1841 #endif
1842 #if 0
1843 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1844 cosa->num, cosa->rxsize);
1845 #endif
1847 } else {
1848 cosa->rxsize = cosa_getdata16(cosa);
1849 #ifdef DEBUG_IO
1850 debug_data_in(cosa, cosa->rxsize);
1851 #endif
1852 #if 0
1853 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1854 cosa->num, cosa->rxsize);
1855 #endif
1857 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1858 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1859 cosa->name, cosa->rxsize);
1860 spin_unlock_irqrestore(&cosa->lock, flags);
1861 goto reject;
1863 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1864 cosa->rxsize &= 0x1fff;
1865 spin_unlock_irqrestore(&cosa->lock, flags);
1867 cosa->rxbuf = NULL;
1868 if (cosa->rxchan->setup_rx)
1869 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1871 if (!cosa->rxbuf) {
1872 reject: /* Reject the packet */
1873 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1874 cosa->num, cosa->rxchan->num);
1875 cosa->rxbuf = cosa->bouncebuf;
1878 /* start the DMA */
1879 flags = claim_dma_lock();
1880 disable_dma(cosa->dma);
1881 clear_dma_ff(cosa->dma);
1882 set_dma_mode(cosa->dma, DMA_MODE_READ);
1883 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1884 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1885 } else {
1886 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1888 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1889 enable_dma(cosa->dma);
1890 release_dma_lock(flags);
1891 spin_lock_irqsave(&cosa->lock, flags);
1892 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1893 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1894 cosa_putdata8(cosa, DRIVER_RX_READY);
1895 #ifdef DEBUG_IO
1896 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1897 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1898 debug_data_cmd(cosa, DRIVER_RX_READY);
1899 #endif
1900 spin_unlock_irqrestore(&cosa->lock, flags);
1903 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1905 unsigned long flags, flags1;
1906 spin_lock_irqsave(&cosa->lock, flags);
1907 flags1 = claim_dma_lock();
1908 disable_dma(cosa->dma);
1909 clear_dma_ff(cosa->dma);
1910 release_dma_lock(flags1);
1911 if (test_bit(TXBIT, &cosa->rxtx)) {
1912 struct channel_data *chan = cosa->chan+cosa->txchan;
1913 if (chan->tx_done)
1914 if (chan->tx_done(chan, cosa->txsize))
1915 clear_bit(chan->num, &cosa->txbitmap);
1916 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1917 #ifdef DEBUG_DATA
1919 int i;
1920 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1921 cosa->rxchan->num, cosa->rxsize);
1922 for (i=0; i<cosa->rxsize; i++)
1923 printk (" %02x", cosa->rxbuf[i]&0xff);
1924 printk("\n");
1926 #endif
1927 /* Packet for unknown channel? */
1928 if (cosa->rxbuf == cosa->bouncebuf)
1929 goto out;
1930 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1931 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1932 if (cosa->rxchan->rx_done)
1933 if (cosa->rxchan->rx_done(cosa->rxchan))
1934 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1935 } else {
1936 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1937 cosa->num);
1940 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1941 * cleared anyway). We should do it as soon as possible
1942 * so that we can tell the COSA we are done and to give it a time
1943 * for recovery.
1945 out:
1946 cosa->rxtx = 0;
1947 put_driver_status_nolock(cosa);
1948 spin_unlock_irqrestore(&cosa->lock, flags);
1951 static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1953 unsigned status;
1954 int count = 0;
1955 struct cosa_data *cosa = cosa_;
1956 again:
1957 status = cosa_getstatus(cosa);
1958 #ifdef DEBUG_IRQS
1959 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1960 status & 0xff);
1961 #endif
1962 #ifdef DEBUG_IO
1963 debug_status_in(cosa, status);
1964 #endif
1965 switch (status & SR_CMD_FROM_SRP_MASK) {
1966 case SR_DOWN_REQUEST:
1967 tx_interrupt(cosa, status);
1968 break;
1969 case SR_UP_REQUEST:
1970 rx_interrupt(cosa, status);
1971 break;
1972 case SR_END_OF_TRANSFER:
1973 eot_interrupt(cosa, status);
1974 break;
1975 default:
1976 /* We may be too fast for SRP. Try to wait a bit more. */
1977 if (count++ < 100) {
1978 udelay(100);
1979 goto again;
1981 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1982 cosa->num, status & 0xff, count);
1984 #ifdef DEBUG_IRQS
1985 if (count)
1986 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1987 cosa->name, count);
1988 else
1989 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
1990 #endif
1991 return IRQ_HANDLED;
1995 /* ---------- I/O debugging routines ---------- */
1997 * These routines can be used to monitor COSA/SRP I/O and to printk()
1998 * the data being transferred on the data and status I/O port in a
1999 * readable way.
2002 #ifdef DEBUG_IO
2003 static void debug_status_in(struct cosa_data *cosa, int status)
2005 char *s;
2006 switch(status & SR_CMD_FROM_SRP_MASK) {
2007 case SR_UP_REQUEST:
2008 s = "RX_REQ";
2009 break;
2010 case SR_DOWN_REQUEST:
2011 s = "TX_REQ";
2012 break;
2013 case SR_END_OF_TRANSFER:
2014 s = "ET_REQ";
2015 break;
2016 default:
2017 s = "NO_REQ";
2018 break;
2020 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2021 cosa->name,
2022 status,
2023 status & SR_USR_RQ ? "USR_RQ|":"",
2024 status & SR_TX_RDY ? "TX_RDY|":"",
2025 status & SR_RX_RDY ? "RX_RDY|":"",
2029 static void debug_status_out(struct cosa_data *cosa, int status)
2031 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2032 cosa->name,
2033 status,
2034 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
2035 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
2036 status & SR_RST ? "RESET|":"",
2037 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2038 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
2039 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
2042 static void debug_data_in(struct cosa_data *cosa, int data)
2044 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2047 static void debug_data_out(struct cosa_data *cosa, int data)
2049 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2052 static void debug_data_cmd(struct cosa_data *cosa, int data)
2054 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2055 cosa->name, data,
2056 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2057 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2059 #endif
2061 /* EOF -- this file has not been truncated */