Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / net / wan / cosa.c
blob921a573372e945c6d2f0d38f9f844da9d3fe97a6
1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
3 /*
4 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * The driver for the SRP and COSA synchronous serial cards.
24 * HARDWARE INFO
26 * Both cards are developed at the Institute of Computer Science,
27 * Masaryk University (http://www.ics.muni.cz/). The hardware is
28 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
29 * and the photo of both cards is available at
30 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
31 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
32 * For Linux-specific utilities, see below in the "Software info" section.
33 * If you want to order the card, contact Jiri Novotny.
35 * The SRP (serial port?, the Czech word "srp" means "sickle") card
36 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
37 * with V.24 interfaces up to 80kb/s each.
39 * The COSA (communication serial adapter?, the Czech word "kosa" means
40 * "scythe") is a next-generation sync/async board with two interfaces
41 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
42 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
43 * The 8-channels version is in development.
45 * Both types have downloadable firmware and communicate via ISA DMA.
46 * COSA can be also a bus-mastering device.
48 * SOFTWARE INFO
50 * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
51 * The CVS tree of Linux driver can be viewed there, as well as the
52 * firmware binaries and user-space utilities for downloading the firmware
53 * into the card and setting up the card.
55 * The Linux driver (unlike the present *BSD drivers :-) can work even
56 * for the COSA and SRP in one computer and allows each channel to work
57 * in one of the three modes (character device, Cisco HDLC, Sync PPP).
59 * AUTHOR
61 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
63 * You can mail me bugfixes and even success reports. I am especially
64 * interested in the SMP and/or muliti-channel success/failure reports
65 * (I wonder if I did the locking properly :-).
67 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
69 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
70 * The skeleton.c by Donald Becker
71 * The SDL Riscom/N2 driver by Mike Natale
72 * The Comtrol Hostess SV11 driver by Alan Cox
73 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
76 * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
77 * fixed a deadlock in cosa_sppp_open
80 /* ---------- Headers, macros, data structures ---------- */
82 #include <linux/config.h>
83 #include <linux/module.h>
84 #include <linux/kernel.h>
85 #include <linux/slab.h>
86 #include <linux/poll.h>
87 #include <linux/fs.h>
88 #include <linux/devfs_fs_kernel.h>
89 #include <linux/interrupt.h>
90 #include <linux/delay.h>
91 #include <linux/errno.h>
92 #include <linux/ioport.h>
93 #include <linux/netdevice.h>
94 #include <linux/spinlock.h>
95 #include <linux/smp_lock.h>
96 #include <linux/device.h>
98 #undef COSA_SLOW_IO /* for testing purposes only */
99 #undef REALLY_SLOW_IO
101 #include <asm/io.h>
102 #include <asm/dma.h>
103 #include <asm/byteorder.h>
105 #include <net/syncppp.h>
106 #include "cosa.h"
108 /* Maximum length of the identification string. */
109 #define COSA_MAX_ID_STRING 128
111 /* Maximum length of the channel name */
112 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
114 /* Per-channel data structure */
116 struct channel_data {
117 void *if_ptr; /* General purpose pointer (used by SPPP) */
118 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
119 int num; /* Number of the channel */
120 struct cosa_data *cosa; /* Pointer to the per-card structure */
121 int txsize; /* Size of transmitted data */
122 char *txbuf; /* Transmit buffer */
123 char name[COSA_MAX_NAME]; /* channel name */
125 /* The HW layer interface */
126 /* routine called from the RX interrupt */
127 char *(*setup_rx)(struct channel_data *channel, int size);
128 /* routine called when the RX is done (from the EOT interrupt) */
129 int (*rx_done)(struct channel_data *channel);
130 /* routine called when the TX is done (from the EOT interrupt) */
131 int (*tx_done)(struct channel_data *channel, int size);
133 /* Character device parts */
134 struct semaphore rsem, wsem;
135 char *rxdata;
136 int rxsize;
137 wait_queue_head_t txwaitq, rxwaitq;
138 int tx_status, rx_status;
140 /* SPPP/HDLC device parts */
141 struct ppp_device pppdev;
142 struct sk_buff *rx_skb, *tx_skb;
143 struct net_device_stats stats;
146 /* cosa->firmware_status bits */
147 #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
148 #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
149 #define COSA_FW_START (1<<2) /* Is the microcode running? */
151 struct cosa_data {
152 int num; /* Card number */
153 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
154 unsigned int datareg, statusreg; /* I/O ports */
155 unsigned short irq, dma; /* IRQ and DMA number */
156 unsigned short startaddr; /* Firmware start address */
157 unsigned short busmaster; /* Use busmastering? */
158 int nchannels; /* # of channels on this card */
159 int driver_status; /* For communicating with firmware */
160 int firmware_status; /* Downloaded, reseted, etc. */
161 long int rxbitmap, txbitmap; /* Bitmap of channels who are willing to send/receive data */
162 long int rxtx; /* RX or TX in progress? */
163 int enabled;
164 int usage; /* usage count */
165 int txchan, txsize, rxsize;
166 struct channel_data *rxchan;
167 char *bouncebuf;
168 char *txbuf, *rxbuf;
169 struct channel_data *chan;
170 spinlock_t lock; /* For exclusive operations on this structure */
171 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
172 char *type; /* card type */
176 * Define this if you want all the possible ports to be autoprobed.
177 * It is here but it probably is not a good idea to use this.
179 /* #define COSA_ISA_AUTOPROBE 1 */
182 * Character device major number. 117 was allocated for us.
183 * The value of 0 means to allocate a first free one.
185 static int cosa_major = 117;
188 * Encoding of the minor numbers:
189 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
190 * the highest bits means the card number.
192 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
193 * for the single card */
195 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
196 * macro doesn't like anything other than the raw number as an argument :-(
198 #define MAX_CARDS 16
199 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
201 #define DRIVER_RX_READY 0x0001
202 #define DRIVER_TX_READY 0x0002
203 #define DRIVER_TXMAP_SHIFT 2
204 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
207 * for cosa->rxtx - indicates whether either transmit or receive is
208 * in progress. These values are mean number of the bit.
210 #define TXBIT 0
211 #define RXBIT 1
212 #define IRQBIT 2
214 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
216 #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
217 #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
218 #undef DEBUG_IO //1 /* Dump the I/O traffic */
220 #define TX_TIMEOUT (5*HZ)
222 /* Maybe the following should be allocated dynamically */
223 static struct cosa_data cosa_cards[MAX_CARDS];
224 static int nr_cards;
226 #ifdef COSA_ISA_AUTOPROBE
227 static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
228 /* NOTE: DMA is not autoprobed!!! */
229 static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
230 #else
231 static int io[MAX_CARDS+1];
232 static int dma[MAX_CARDS+1];
233 #endif
234 /* IRQ can be safely autoprobed */
235 static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
237 /* for class stuff*/
238 static struct class_simple *cosa_class;
240 #ifdef MODULE
241 module_param_array(io, int, NULL, 0);
242 MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
243 module_param_array(irq, int, NULL, 0);
244 MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
245 module_param_array(dma, int, NULL, 0);
246 MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
248 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
249 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
250 MODULE_LICENSE("GPL");
251 #endif
253 /* I use this mainly for testing purposes */
254 #ifdef COSA_SLOW_IO
255 #define cosa_outb outb_p
256 #define cosa_outw outw_p
257 #define cosa_inb inb_p
258 #define cosa_inw inw_p
259 #else
260 #define cosa_outb outb
261 #define cosa_outw outw
262 #define cosa_inb inb
263 #define cosa_inw inw
264 #endif
266 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
268 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
269 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
270 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
271 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
272 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
273 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
275 /* Initialization stuff */
276 static int cosa_probe(int ioaddr, int irq, int dma);
278 /* HW interface */
279 static void cosa_enable_rx(struct channel_data *chan);
280 static void cosa_disable_rx(struct channel_data *chan);
281 static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
282 static void cosa_kick(struct cosa_data *cosa);
283 static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
285 /* SPPP/HDLC stuff */
286 static void sppp_channel_init(struct channel_data *chan);
287 static void sppp_channel_delete(struct channel_data *chan);
288 static int cosa_sppp_open(struct net_device *d);
289 static int cosa_sppp_close(struct net_device *d);
290 static void cosa_sppp_timeout(struct net_device *d);
291 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *d);
292 static char *sppp_setup_rx(struct channel_data *channel, int size);
293 static int sppp_rx_done(struct channel_data *channel);
294 static int sppp_tx_done(struct channel_data *channel, int size);
295 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
296 static struct net_device_stats *cosa_net_stats(struct net_device *dev);
298 /* Character device */
299 static void chardev_channel_init(struct channel_data *chan);
300 static char *chrdev_setup_rx(struct channel_data *channel, int size);
301 static int chrdev_rx_done(struct channel_data *channel);
302 static int chrdev_tx_done(struct channel_data *channel, int size);
303 static ssize_t cosa_read(struct file *file,
304 char __user *buf, size_t count, loff_t *ppos);
305 static ssize_t cosa_write(struct file *file,
306 const char __user *buf, size_t count, loff_t *ppos);
307 static unsigned int cosa_poll(struct file *file, poll_table *poll);
308 static int cosa_open(struct inode *inode, struct file *file);
309 static int cosa_release(struct inode *inode, struct file *file);
310 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
311 unsigned int cmd, unsigned long arg);
312 #ifdef COSA_FASYNC_WORKING
313 static int cosa_fasync(struct inode *inode, struct file *file, int on);
314 #endif
316 static struct file_operations cosa_fops = {
317 .owner = THIS_MODULE,
318 .llseek = no_llseek,
319 .read = cosa_read,
320 .write = cosa_write,
321 .poll = cosa_poll,
322 .ioctl = cosa_chardev_ioctl,
323 .open = cosa_open,
324 .release = cosa_release,
325 #ifdef COSA_FASYNC_WORKING
326 .fasync = cosa_fasync,
327 #endif
330 /* Ioctls */
331 static int cosa_start(struct cosa_data *cosa, int address);
332 static int cosa_reset(struct cosa_data *cosa);
333 static int cosa_download(struct cosa_data *cosa, void __user *a);
334 static int cosa_readmem(struct cosa_data *cosa, void __user *a);
336 /* COSA/SRP ROM monitor */
337 static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
338 static int startmicrocode(struct cosa_data *cosa, int address);
339 static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
340 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
342 /* Auxilliary functions */
343 static int get_wait_data(struct cosa_data *cosa);
344 static int put_wait_data(struct cosa_data *cosa, int data);
345 static int puthexnumber(struct cosa_data *cosa, int number);
346 static void put_driver_status(struct cosa_data *cosa);
347 static void put_driver_status_nolock(struct cosa_data *cosa);
349 /* Interrupt handling */
350 static irqreturn_t cosa_interrupt(int irq, void *cosa, struct pt_regs *regs);
352 /* I/O ops debugging */
353 #ifdef DEBUG_IO
354 static void debug_data_in(struct cosa_data *cosa, int data);
355 static void debug_data_out(struct cosa_data *cosa, int data);
356 static void debug_data_cmd(struct cosa_data *cosa, int data);
357 static void debug_status_in(struct cosa_data *cosa, int status);
358 static void debug_status_out(struct cosa_data *cosa, int status);
359 #endif
362 /* ---------- Initialization stuff ---------- */
364 static int __init cosa_init(void)
366 int i, err = 0;
368 printk(KERN_INFO "cosa v1.08 (c) 1997-2000 Jan Kasprzak <kas@fi.muni.cz>\n");
369 #ifdef CONFIG_SMP
370 printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
371 #endif
372 if (cosa_major > 0) {
373 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
374 printk(KERN_WARNING "cosa: unable to get major %d\n",
375 cosa_major);
376 err = -EIO;
377 goto out;
379 } else {
380 if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
381 printk(KERN_WARNING "cosa: unable to register chardev\n");
382 err = -EIO;
383 goto out;
386 for (i=0; i<MAX_CARDS; i++)
387 cosa_cards[i].num = -1;
388 for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
389 cosa_probe(io[i], irq[i], dma[i]);
390 if (!nr_cards) {
391 printk(KERN_WARNING "cosa: no devices found.\n");
392 unregister_chrdev(cosa_major, "cosa");
393 err = -ENODEV;
394 goto out;
396 devfs_mk_dir("cosa");
397 cosa_class = class_simple_create(THIS_MODULE, "cosa");
398 if (IS_ERR(cosa_class)) {
399 err = PTR_ERR(cosa_class);
400 goto out_chrdev;
402 for (i=0; i<nr_cards; i++) {
403 class_simple_device_add(cosa_class, MKDEV(cosa_major, i),
404 NULL, "cosa%d", i);
405 err = devfs_mk_cdev(MKDEV(cosa_major, i),
406 S_IFCHR|S_IRUSR|S_IWUSR,
407 "cosa/%d", i);
408 if (err) {
409 class_simple_device_remove(MKDEV(cosa_major, i));
410 goto out_chrdev;
413 err = 0;
414 goto out;
416 out_chrdev:
417 unregister_chrdev(cosa_major, "cosa");
418 out:
419 return err;
421 module_init(cosa_init);
423 static void __exit cosa_exit(void)
425 struct cosa_data *cosa;
426 int i;
427 printk(KERN_INFO "Unloading the cosa module\n");
429 for (i=0; i<nr_cards; i++) {
430 class_simple_device_remove(MKDEV(cosa_major, i));
431 devfs_remove("cosa/%d", i);
433 class_simple_destroy(cosa_class);
434 devfs_remove("cosa");
435 for (cosa=cosa_cards; nr_cards--; cosa++) {
436 /* Clean up the per-channel data */
437 for (i=0; i<cosa->nchannels; i++) {
438 /* Chardev driver has no alloc'd per-channel data */
439 sppp_channel_delete(cosa->chan+i);
441 /* Clean up the per-card data */
442 kfree(cosa->chan);
443 kfree(cosa->bouncebuf);
444 free_irq(cosa->irq, cosa);
445 free_dma(cosa->dma);
446 release_region(cosa->datareg,is_8bit(cosa)?2:4);
448 unregister_chrdev(cosa_major, "cosa");
450 module_exit(cosa_exit);
453 * This function should register all the net devices needed for the
454 * single channel.
456 static __inline__ void channel_init(struct channel_data *chan)
458 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
460 /* Initialize the chardev data structures */
461 chardev_channel_init(chan);
463 /* Register the sppp interface */
464 sppp_channel_init(chan);
467 static int cosa_probe(int base, int irq, int dma)
469 struct cosa_data *cosa = cosa_cards+nr_cards;
470 int i, err = 0;
472 memset(cosa, 0, sizeof(struct cosa_data));
474 /* Checking validity of parameters: */
475 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
476 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
477 printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
478 return -1;
480 /* I/O address should be between 0x100 and 0x3ff and should be
481 * multiple of 8. */
482 if (base < 0x100 || base > 0x3ff || base & 0x7) {
483 printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
484 base);
485 return -1;
487 /* DMA should be 0,1 or 3-7 */
488 if (dma < 0 || dma == 4 || dma > 7) {
489 printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
490 return -1;
492 /* and finally, on 16-bit COSA DMA should be 4-7 and
493 * I/O base should not be multiple of 0x10 */
494 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
495 printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
496 " (base=0x%x, dma=%d)\n", base, dma);
497 return -1;
500 cosa->dma = dma;
501 cosa->datareg = base;
502 cosa->statusreg = is_8bit(cosa)?base+1:base+2;
503 spin_lock_init(&cosa->lock);
505 if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
506 return -1;
508 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
509 printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
510 err = -1;
511 goto err_out;
514 /* Test the validity of identification string */
515 if (!strncmp(cosa->id_string, "SRP", 3))
516 cosa->type = "srp";
517 else if (!strncmp(cosa->id_string, "COSA", 4))
518 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
519 else {
520 /* Print a warning only if we are not autoprobing */
521 #ifndef COSA_ISA_AUTOPROBE
522 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
523 base);
524 #endif
525 err = -1;
526 goto err_out;
528 /* Update the name of the region now we know the type of card */
529 release_region(base, is_8bit(cosa)?2:4);
530 if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
531 printk(KERN_DEBUG "cosa: changing name at 0x%x failed.\n", base);
532 return -1;
535 /* Now do IRQ autoprobe */
536 if (irq < 0) {
537 unsigned long irqs;
538 /* printk(KERN_INFO "IRQ autoprobe\n"); */
539 irqs = probe_irq_on();
541 * Enable interrupt on tx buffer empty (it sure is)
542 * really sure ?
543 * FIXME: When this code is not used as module, we should
544 * probably call udelay() instead of the interruptible sleep.
546 set_current_state(TASK_INTERRUPTIBLE);
547 cosa_putstatus(cosa, SR_TX_INT_ENA);
548 schedule_timeout(30);
549 irq = probe_irq_off(irqs);
550 /* Disable all IRQs from the card */
551 cosa_putstatus(cosa, 0);
552 /* Empty the received data register */
553 cosa_getdata8(cosa);
555 if (irq < 0) {
556 printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
557 irq, cosa->datareg);
558 err = -1;
559 goto err_out;
561 if (irq == 0) {
562 printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
563 cosa->datareg);
564 /* return -1; */
568 cosa->irq = irq;
569 cosa->num = nr_cards;
570 cosa->usage = 0;
571 cosa->nchannels = 2; /* FIXME: how to determine this? */
573 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
574 err = -1;
575 goto err_out;
577 if (request_dma(cosa->dma, cosa->type)) {
578 err = -1;
579 goto err_out1;
582 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
583 if (!cosa->bouncebuf) {
584 err = -ENOMEM;
585 goto err_out2;
587 sprintf(cosa->name, "cosa%d", cosa->num);
589 /* Initialize the per-channel data */
590 cosa->chan = kmalloc(sizeof(struct channel_data)*cosa->nchannels,
591 GFP_KERNEL);
592 if (!cosa->chan) {
593 err = -ENOMEM;
594 goto err_out3;
596 memset(cosa->chan, 0, sizeof(struct channel_data)*cosa->nchannels);
597 for (i=0; i<cosa->nchannels; i++) {
598 cosa->chan[i].cosa = cosa;
599 cosa->chan[i].num = i;
600 channel_init(cosa->chan+i);
603 printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
604 cosa->num, cosa->id_string, cosa->type,
605 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
607 return nr_cards++;
608 err_out3:
609 kfree(cosa->bouncebuf);
610 err_out2:
611 free_dma(cosa->dma);
612 err_out1:
613 free_irq(cosa->irq, cosa);
614 err_out:
615 release_region(cosa->datareg,is_8bit(cosa)?2:4);
616 printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
617 cosa->num);
618 return err;
622 /*---------- SPPP/HDLC netdevice ---------- */
624 static void cosa_setup(struct net_device *d)
626 d->open = cosa_sppp_open;
627 d->stop = cosa_sppp_close;
628 d->hard_start_xmit = cosa_sppp_tx;
629 d->do_ioctl = cosa_sppp_ioctl;
630 d->get_stats = cosa_net_stats;
631 d->tx_timeout = cosa_sppp_timeout;
632 d->watchdog_timeo = TX_TIMEOUT;
635 static void sppp_channel_init(struct channel_data *chan)
637 struct net_device *d;
638 chan->if_ptr = &chan->pppdev;
639 d = alloc_netdev(0, chan->name, cosa_setup);
640 if (!d) {
641 printk(KERN_WARNING "%s: alloc_netdev failed.\n", chan->name);
642 return;
644 chan->pppdev.dev = d;
645 d->base_addr = chan->cosa->datareg;
646 d->irq = chan->cosa->irq;
647 d->dma = chan->cosa->dma;
648 d->priv = chan;
649 sppp_attach(&chan->pppdev);
650 if (register_netdev(d)) {
651 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
652 sppp_detach(d);
653 free_netdev(d);
654 chan->pppdev.dev = NULL;
655 return;
659 static void sppp_channel_delete(struct channel_data *chan)
661 unregister_netdev(chan->pppdev.dev);
662 sppp_detach(chan->pppdev.dev);
663 free_netdev(chan->pppdev.dev);
664 chan->pppdev.dev = NULL;
667 static int cosa_sppp_open(struct net_device *d)
669 struct channel_data *chan = d->priv;
670 int err;
671 unsigned long flags;
673 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
674 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
675 chan->cosa->name, chan->cosa->firmware_status);
676 return -EPERM;
678 spin_lock_irqsave(&chan->cosa->lock, flags);
679 if (chan->usage != 0) {
680 printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
681 chan->name, chan->usage);
682 spin_unlock_irqrestore(&chan->cosa->lock, flags);
683 return -EBUSY;
685 chan->setup_rx = sppp_setup_rx;
686 chan->tx_done = sppp_tx_done;
687 chan->rx_done = sppp_rx_done;
688 chan->usage=-1;
689 chan->cosa->usage++;
690 spin_unlock_irqrestore(&chan->cosa->lock, flags);
692 err = sppp_open(d);
693 if (err) {
694 spin_lock_irqsave(&chan->cosa->lock, flags);
695 chan->usage=0;
696 chan->cosa->usage--;
698 spin_unlock_irqrestore(&chan->cosa->lock, flags);
699 return err;
702 netif_start_queue(d);
703 cosa_enable_rx(chan);
704 return 0;
707 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *dev)
709 struct channel_data *chan = dev->priv;
711 netif_stop_queue(dev);
713 chan->tx_skb = skb;
714 cosa_start_tx(chan, skb->data, skb->len);
715 return 0;
718 static void cosa_sppp_timeout(struct net_device *dev)
720 struct channel_data *chan = dev->priv;
722 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
723 chan->stats.rx_errors++;
724 chan->stats.rx_missed_errors++;
725 } else {
726 chan->stats.tx_errors++;
727 chan->stats.tx_aborted_errors++;
729 cosa_kick(chan->cosa);
730 if (chan->tx_skb) {
731 dev_kfree_skb(chan->tx_skb);
732 chan->tx_skb = NULL;
734 netif_wake_queue(dev);
737 static int cosa_sppp_close(struct net_device *d)
739 struct channel_data *chan = d->priv;
740 unsigned long flags;
742 netif_stop_queue(d);
743 sppp_close(d);
744 cosa_disable_rx(chan);
745 spin_lock_irqsave(&chan->cosa->lock, flags);
746 if (chan->rx_skb) {
747 kfree_skb(chan->rx_skb);
748 chan->rx_skb = NULL;
750 if (chan->tx_skb) {
751 kfree_skb(chan->tx_skb);
752 chan->tx_skb = NULL;
754 chan->usage=0;
755 chan->cosa->usage--;
756 spin_unlock_irqrestore(&chan->cosa->lock, flags);
757 return 0;
760 static char *sppp_setup_rx(struct channel_data *chan, int size)
763 * We can safely fall back to non-dma-able memory, because we have
764 * the cosa->bouncebuf pre-allocated.
766 if (chan->rx_skb)
767 kfree_skb(chan->rx_skb);
768 chan->rx_skb = dev_alloc_skb(size);
769 if (chan->rx_skb == NULL) {
770 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
771 chan->name);
772 chan->stats.rx_dropped++;
773 return NULL;
775 chan->pppdev.dev->trans_start = jiffies;
776 return skb_put(chan->rx_skb, size);
779 static int sppp_rx_done(struct channel_data *chan)
781 if (!chan->rx_skb) {
782 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
783 chan->name);
784 chan->stats.rx_errors++;
785 chan->stats.rx_frame_errors++;
786 return 0;
788 chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
789 chan->rx_skb->dev = chan->pppdev.dev;
790 chan->rx_skb->mac.raw = chan->rx_skb->data;
791 chan->stats.rx_packets++;
792 chan->stats.rx_bytes += chan->cosa->rxsize;
793 netif_rx(chan->rx_skb);
794 chan->rx_skb = NULL;
795 chan->pppdev.dev->last_rx = jiffies;
796 return 0;
799 /* ARGSUSED */
800 static int sppp_tx_done(struct channel_data *chan, int size)
802 if (!chan->tx_skb) {
803 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
804 chan->name);
805 chan->stats.tx_errors++;
806 chan->stats.tx_aborted_errors++;
807 return 1;
809 dev_kfree_skb_irq(chan->tx_skb);
810 chan->tx_skb = NULL;
811 chan->stats.tx_packets++;
812 chan->stats.tx_bytes += size;
813 netif_wake_queue(chan->pppdev.dev);
814 return 1;
817 static struct net_device_stats *cosa_net_stats(struct net_device *dev)
819 struct channel_data *chan = dev->priv;
820 return &chan->stats;
824 /*---------- Character device ---------- */
826 static void chardev_channel_init(struct channel_data *chan)
828 init_MUTEX(&chan->rsem);
829 init_MUTEX(&chan->wsem);
832 static ssize_t cosa_read(struct file *file,
833 char __user *buf, size_t count, loff_t *ppos)
835 DECLARE_WAITQUEUE(wait, current);
836 unsigned long flags;
837 struct channel_data *chan = file->private_data;
838 struct cosa_data *cosa = chan->cosa;
839 char *kbuf;
841 if (!(cosa->firmware_status & COSA_FW_START)) {
842 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
843 cosa->name, cosa->firmware_status);
844 return -EPERM;
846 if (down_interruptible(&chan->rsem))
847 return -ERESTARTSYS;
849 if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
850 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
851 up(&chan->rsem);
852 return -ENOMEM;
855 chan->rx_status = 0;
856 cosa_enable_rx(chan);
857 spin_lock_irqsave(&cosa->lock, flags);
858 add_wait_queue(&chan->rxwaitq, &wait);
859 while(!chan->rx_status) {
860 current->state = TASK_INTERRUPTIBLE;
861 spin_unlock_irqrestore(&cosa->lock, flags);
862 schedule();
863 spin_lock_irqsave(&cosa->lock, flags);
864 if (signal_pending(current) && chan->rx_status == 0) {
865 chan->rx_status = 1;
866 remove_wait_queue(&chan->rxwaitq, &wait);
867 current->state = TASK_RUNNING;
868 spin_unlock_irqrestore(&cosa->lock, flags);
869 up(&chan->rsem);
870 return -ERESTARTSYS;
873 remove_wait_queue(&chan->rxwaitq, &wait);
874 current->state = TASK_RUNNING;
875 kbuf = chan->rxdata;
876 count = chan->rxsize;
877 spin_unlock_irqrestore(&cosa->lock, flags);
878 up(&chan->rsem);
880 if (copy_to_user(buf, kbuf, count)) {
881 kfree(kbuf);
882 return -EFAULT;
884 kfree(kbuf);
885 return count;
888 static char *chrdev_setup_rx(struct channel_data *chan, int size)
890 /* Expect size <= COSA_MTU */
891 chan->rxsize = size;
892 return chan->rxdata;
895 static int chrdev_rx_done(struct channel_data *chan)
897 if (chan->rx_status) { /* Reader has died */
898 kfree(chan->rxdata);
899 up(&chan->wsem);
901 chan->rx_status = 1;
902 wake_up_interruptible(&chan->rxwaitq);
903 return 1;
907 static ssize_t cosa_write(struct file *file,
908 const char __user *buf, size_t count, loff_t *ppos)
910 DECLARE_WAITQUEUE(wait, current);
911 struct channel_data *chan = file->private_data;
912 struct cosa_data *cosa = chan->cosa;
913 unsigned long flags;
914 char *kbuf;
916 if (!(cosa->firmware_status & COSA_FW_START)) {
917 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
918 cosa->name, cosa->firmware_status);
919 return -EPERM;
921 if (down_interruptible(&chan->wsem))
922 return -ERESTARTSYS;
924 if (count > COSA_MTU)
925 count = COSA_MTU;
927 /* Allocate the buffer */
928 if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
929 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
930 cosa->name);
931 up(&chan->wsem);
932 return -ENOMEM;
934 if (copy_from_user(kbuf, buf, count)) {
935 up(&chan->wsem);
936 kfree(kbuf);
937 return -EFAULT;
939 chan->tx_status=0;
940 cosa_start_tx(chan, kbuf, count);
942 spin_lock_irqsave(&cosa->lock, flags);
943 add_wait_queue(&chan->txwaitq, &wait);
944 while(!chan->tx_status) {
945 current->state = TASK_INTERRUPTIBLE;
946 spin_unlock_irqrestore(&cosa->lock, flags);
947 schedule();
948 spin_lock_irqsave(&cosa->lock, flags);
949 if (signal_pending(current) && chan->tx_status == 0) {
950 chan->tx_status = 1;
951 remove_wait_queue(&chan->txwaitq, &wait);
952 current->state = TASK_RUNNING;
953 chan->tx_status = 1;
954 spin_unlock_irqrestore(&cosa->lock, flags);
955 return -ERESTARTSYS;
958 remove_wait_queue(&chan->txwaitq, &wait);
959 current->state = TASK_RUNNING;
960 up(&chan->wsem);
961 spin_unlock_irqrestore(&cosa->lock, flags);
962 kfree(kbuf);
963 return count;
966 static int chrdev_tx_done(struct channel_data *chan, int size)
968 if (chan->tx_status) { /* Writer was interrupted */
969 kfree(chan->txbuf);
970 up(&chan->wsem);
972 chan->tx_status = 1;
973 wake_up_interruptible(&chan->txwaitq);
974 return 1;
977 static unsigned int cosa_poll(struct file *file, poll_table *poll)
979 printk(KERN_INFO "cosa_poll is here\n");
980 return 0;
983 static int cosa_open(struct inode *inode, struct file *file)
985 struct cosa_data *cosa;
986 struct channel_data *chan;
987 unsigned long flags;
988 int n;
990 if ((n=iminor(file->f_dentry->d_inode)>>CARD_MINOR_BITS)
991 >= nr_cards)
992 return -ENODEV;
993 cosa = cosa_cards+n;
995 if ((n=iminor(file->f_dentry->d_inode)
996 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
997 return -ENODEV;
998 chan = cosa->chan + n;
1000 file->private_data = chan;
1002 spin_lock_irqsave(&cosa->lock, flags);
1004 if (chan->usage < 0) { /* in netdev mode */
1005 spin_unlock_irqrestore(&cosa->lock, flags);
1006 return -EBUSY;
1008 cosa->usage++;
1009 chan->usage++;
1011 chan->tx_done = chrdev_tx_done;
1012 chan->setup_rx = chrdev_setup_rx;
1013 chan->rx_done = chrdev_rx_done;
1014 spin_unlock_irqrestore(&cosa->lock, flags);
1015 return 0;
1018 static int cosa_release(struct inode *inode, struct file *file)
1020 struct channel_data *channel = file->private_data;
1021 struct cosa_data *cosa;
1022 unsigned long flags;
1024 cosa = channel->cosa;
1025 spin_lock_irqsave(&cosa->lock, flags);
1026 cosa->usage--;
1027 channel->usage--;
1028 spin_unlock_irqrestore(&cosa->lock, flags);
1029 return 0;
1032 #ifdef COSA_FASYNC_WORKING
1033 static struct fasync_struct *fasync[256] = { NULL, };
1035 /* To be done ... */
1036 static int cosa_fasync(struct inode *inode, struct file *file, int on)
1038 int port = iminor(inode);
1039 int rv = fasync_helper(inode, file, on, &fasync[port]);
1040 return rv < 0 ? rv : 0;
1042 #endif
1045 /* ---------- Ioctls ---------- */
1048 * Ioctl subroutines can safely be made inline, because they are called
1049 * only from cosa_ioctl().
1051 static inline int cosa_reset(struct cosa_data *cosa)
1053 char idstring[COSA_MAX_ID_STRING];
1054 if (cosa->usage > 1)
1055 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1056 cosa->num, cosa->usage);
1057 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1058 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1059 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1060 return -EIO;
1062 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1063 idstring);
1064 cosa->firmware_status |= COSA_FW_RESET;
1065 return 0;
1068 /* High-level function to download data into COSA memory. Calls download() */
1069 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1071 struct cosa_download d;
1072 int i;
1074 if (cosa->usage > 1)
1075 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1076 cosa->name, cosa->usage);
1077 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1078 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1079 cosa->name, cosa->firmware_status);
1080 return -EPERM;
1083 if (copy_from_user(&d, arg, sizeof(d)))
1084 return -EFAULT;
1086 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1087 return -EINVAL;
1088 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1089 return -EINVAL;
1092 /* If something fails, force the user to reset the card */
1093 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1095 i = download(cosa, d.code, d.len, d.addr);
1096 if (i < 0) {
1097 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1098 cosa->num, i);
1099 return -EIO;
1101 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1102 cosa->num, d.len, d.addr);
1103 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1104 return 0;
1107 /* High-level function to read COSA memory. Calls readmem() */
1108 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1110 struct cosa_download d;
1111 int i;
1113 if (cosa->usage > 1)
1114 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1115 "cosa->usage > 1 (%d). Odd things may happen.\n",
1116 cosa->num, cosa->usage);
1117 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1118 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1119 cosa->name, cosa->firmware_status);
1120 return -EPERM;
1123 if (copy_from_user(&d, arg, sizeof(d)))
1124 return -EFAULT;
1126 /* If something fails, force the user to reset the card */
1127 cosa->firmware_status &= ~COSA_FW_RESET;
1129 i = readmem(cosa, d.code, d.len, d.addr);
1130 if (i < 0) {
1131 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1132 cosa->num, i);
1133 return -EIO;
1135 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1136 cosa->num, d.len, d.addr);
1137 cosa->firmware_status |= COSA_FW_RESET;
1138 return 0;
1141 /* High-level function to start microcode. Calls startmicrocode(). */
1142 static inline int cosa_start(struct cosa_data *cosa, int address)
1144 int i;
1146 if (cosa->usage > 1)
1147 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1148 cosa->num, cosa->usage);
1150 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1151 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1152 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1153 cosa->name, cosa->firmware_status);
1154 return -EPERM;
1156 cosa->firmware_status &= ~COSA_FW_RESET;
1157 if ((i=startmicrocode(cosa, address)) < 0) {
1158 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1159 cosa->num, address, i);
1160 return -EIO;
1162 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1163 cosa->num, address);
1164 cosa->startaddr = address;
1165 cosa->firmware_status |= COSA_FW_START;
1166 return 0;
1169 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1170 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1172 int l = strlen(cosa->id_string)+1;
1173 if (copy_to_user(string, cosa->id_string, l))
1174 return -EFAULT;
1175 return l;
1178 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1179 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1181 int l = strlen(cosa->type)+1;
1182 if (copy_to_user(string, cosa->type, l))
1183 return -EFAULT;
1184 return l;
1187 static int cosa_ioctl_common(struct cosa_data *cosa,
1188 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1190 void __user *argp = (void __user *)arg;
1191 switch(cmd) {
1192 case COSAIORSET: /* Reset the device */
1193 if (!capable(CAP_NET_ADMIN))
1194 return -EACCES;
1195 return cosa_reset(cosa);
1196 case COSAIOSTRT: /* Start the firmware */
1197 if (!capable(CAP_SYS_RAWIO))
1198 return -EACCES;
1199 return cosa_start(cosa, arg);
1200 case COSAIODOWNLD: /* Download the firmware */
1201 if (!capable(CAP_SYS_RAWIO))
1202 return -EACCES;
1204 return cosa_download(cosa, argp);
1205 case COSAIORMEM:
1206 if (!capable(CAP_SYS_RAWIO))
1207 return -EACCES;
1208 return cosa_readmem(cosa, argp);
1209 case COSAIORTYPE:
1210 return cosa_gettype(cosa, argp);
1211 case COSAIORIDSTR:
1212 return cosa_getidstr(cosa, argp);
1213 case COSAIONRCARDS:
1214 return nr_cards;
1215 case COSAIONRCHANS:
1216 return cosa->nchannels;
1217 case COSAIOBMSET:
1218 if (!capable(CAP_SYS_RAWIO))
1219 return -EACCES;
1220 if (is_8bit(cosa))
1221 return -EINVAL;
1222 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1223 return -EINVAL;
1224 cosa->busmaster = arg;
1225 return 0;
1226 case COSAIOBMGET:
1227 return cosa->busmaster;
1229 return -ENOIOCTLCMD;
1232 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
1233 int cmd)
1235 int rv;
1236 struct channel_data *chan = dev->priv;
1237 rv = cosa_ioctl_common(chan->cosa, chan, cmd, (unsigned long)ifr->ifr_data);
1238 if (rv == -ENOIOCTLCMD) {
1239 return sppp_do_ioctl(dev, ifr, cmd);
1241 return rv;
1244 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1245 unsigned int cmd, unsigned long arg)
1247 struct channel_data *channel = file->private_data;
1248 struct cosa_data *cosa = channel->cosa;
1249 return cosa_ioctl_common(cosa, channel, cmd, arg);
1253 /*---------- HW layer interface ---------- */
1256 * The higher layer can bind itself to the HW layer by setting the callbacks
1257 * in the channel_data structure and by using these routines.
1259 static void cosa_enable_rx(struct channel_data *chan)
1261 struct cosa_data *cosa = chan->cosa;
1263 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1264 put_driver_status(cosa);
1267 static void cosa_disable_rx(struct channel_data *chan)
1269 struct cosa_data *cosa = chan->cosa;
1271 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1272 put_driver_status(cosa);
1276 * FIXME: This routine probably should check for cosa_start_tx() called when
1277 * the previous transmit is still unfinished. In this case the non-zero
1278 * return value should indicate to the caller that the queuing(sp?) up
1279 * the transmit has failed.
1281 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1283 struct cosa_data *cosa = chan->cosa;
1284 unsigned long flags;
1285 #ifdef DEBUG_DATA
1286 int i;
1288 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1289 chan->num, len);
1290 for (i=0; i<len; i++)
1291 printk(" %02x", buf[i]&0xff);
1292 printk("\n");
1293 #endif
1294 spin_lock_irqsave(&cosa->lock, flags);
1295 chan->txbuf = buf;
1296 chan->txsize = len;
1297 if (len > COSA_MTU)
1298 chan->txsize = COSA_MTU;
1299 spin_unlock_irqrestore(&cosa->lock, flags);
1301 /* Tell the firmware we are ready */
1302 set_bit(chan->num, &cosa->txbitmap);
1303 put_driver_status(cosa);
1305 return 0;
1308 static void put_driver_status(struct cosa_data *cosa)
1310 unsigned long flags;
1311 int status;
1313 spin_lock_irqsave(&cosa->lock, flags);
1315 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1316 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1317 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1318 &DRIVER_TXMAP_MASK : 0);
1319 if (!cosa->rxtx) {
1320 if (cosa->rxbitmap|cosa->txbitmap) {
1321 if (!cosa->enabled) {
1322 cosa_putstatus(cosa, SR_RX_INT_ENA);
1323 #ifdef DEBUG_IO
1324 debug_status_out(cosa, SR_RX_INT_ENA);
1325 #endif
1326 cosa->enabled = 1;
1328 } else if (cosa->enabled) {
1329 cosa->enabled = 0;
1330 cosa_putstatus(cosa, 0);
1331 #ifdef DEBUG_IO
1332 debug_status_out(cosa, 0);
1333 #endif
1335 cosa_putdata8(cosa, status);
1336 #ifdef DEBUG_IO
1337 debug_data_cmd(cosa, status);
1338 #endif
1340 spin_unlock_irqrestore(&cosa->lock, flags);
1343 static void put_driver_status_nolock(struct cosa_data *cosa)
1345 int status;
1347 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1348 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1349 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1350 &DRIVER_TXMAP_MASK : 0);
1352 if (cosa->rxbitmap|cosa->txbitmap) {
1353 cosa_putstatus(cosa, SR_RX_INT_ENA);
1354 #ifdef DEBUG_IO
1355 debug_status_out(cosa, SR_RX_INT_ENA);
1356 #endif
1357 cosa->enabled = 1;
1358 } else {
1359 cosa_putstatus(cosa, 0);
1360 #ifdef DEBUG_IO
1361 debug_status_out(cosa, 0);
1362 #endif
1363 cosa->enabled = 0;
1365 cosa_putdata8(cosa, status);
1366 #ifdef DEBUG_IO
1367 debug_data_cmd(cosa, status);
1368 #endif
1372 * The "kickme" function: When the DMA times out, this is called to
1373 * clean up the driver status.
1374 * FIXME: Preliminary support, the interface is probably wrong.
1376 static void cosa_kick(struct cosa_data *cosa)
1378 unsigned long flags, flags1;
1379 char *s = "(probably) IRQ";
1381 if (test_bit(RXBIT, &cosa->rxtx))
1382 s = "RX DMA";
1383 if (test_bit(TXBIT, &cosa->rxtx))
1384 s = "TX DMA";
1386 printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
1387 spin_lock_irqsave(&cosa->lock, flags);
1388 cosa->rxtx = 0;
1390 flags1 = claim_dma_lock();
1391 disable_dma(cosa->dma);
1392 clear_dma_ff(cosa->dma);
1393 release_dma_lock(flags1);
1395 /* FIXME: Anything else? */
1396 udelay(100);
1397 cosa_putstatus(cosa, 0);
1398 udelay(100);
1399 (void) cosa_getdata8(cosa);
1400 udelay(100);
1401 cosa_putdata8(cosa, 0);
1402 udelay(100);
1403 put_driver_status_nolock(cosa);
1404 spin_unlock_irqrestore(&cosa->lock, flags);
1408 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1409 * physical memory and doesn't span the 64k boundary. For now it seems
1410 * SKB's never do this, but we'll check this anyway.
1412 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1414 static int count;
1415 unsigned long b = (unsigned long)buf;
1416 if (b+len >= MAX_DMA_ADDRESS)
1417 return 0;
1418 if ((b^ (b+len)) & 0x10000) {
1419 if (count++ < 5)
1420 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1421 chan->name);
1422 return 0;
1424 return 1;
1428 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1431 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1432 * drivers need to say 4-digit hex number meaning start address of the microcode
1433 * separated by a single space. Monitor replies by saying " =". Now driver
1434 * has to write 4-digit hex number meaning the last byte address ended
1435 * by a single space. Monitor has to reply with a space. Now the download
1436 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1438 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1440 int i;
1442 if (put_wait_data(cosa, 'w') == -1) return -1;
1443 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1444 if (get_wait_data(cosa) != '=') return -3;
1446 if (puthexnumber(cosa, address) < 0) return -4;
1447 if (put_wait_data(cosa, ' ') == -1) return -10;
1448 if (get_wait_data(cosa) != ' ') return -11;
1449 if (get_wait_data(cosa) != '=') return -12;
1451 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1452 if (put_wait_data(cosa, ' ') == -1) return -18;
1453 if (get_wait_data(cosa) != ' ') return -19;
1455 while (length--) {
1456 char c;
1457 #ifndef SRP_DOWNLOAD_AT_BOOT
1458 if (get_user(c, microcode))
1459 return -23; /* ??? */
1460 #else
1461 c = *microcode;
1462 #endif
1463 if (put_wait_data(cosa, c) == -1)
1464 return -20;
1465 microcode++;
1468 if (get_wait_data(cosa) != '\r') return -21;
1469 if (get_wait_data(cosa) != '\n') return -22;
1470 if (get_wait_data(cosa) != '.') return -23;
1471 #if 0
1472 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1473 #endif
1474 return 0;
1479 * Starting microcode is done via the "g" command of the SRP monitor.
1480 * The chat should be the following: "g" "g=" "<addr><CR>"
1481 * "<CR><CR><LF><CR><LF>".
1483 static int startmicrocode(struct cosa_data *cosa, int address)
1485 if (put_wait_data(cosa, 'g') == -1) return -1;
1486 if (get_wait_data(cosa) != 'g') return -2;
1487 if (get_wait_data(cosa) != '=') return -3;
1489 if (puthexnumber(cosa, address) < 0) return -4;
1490 if (put_wait_data(cosa, '\r') == -1) return -5;
1492 if (get_wait_data(cosa) != '\r') return -6;
1493 if (get_wait_data(cosa) != '\r') return -7;
1494 if (get_wait_data(cosa) != '\n') return -8;
1495 if (get_wait_data(cosa) != '\r') return -9;
1496 if (get_wait_data(cosa) != '\n') return -10;
1497 #if 0
1498 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1499 #endif
1500 return 0;
1504 * Reading memory is done via the "r" command of the SRP monitor.
1505 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1506 * Then driver can read the data and the conversation is finished
1507 * by SRP monitor sending "<CR><LF>." (dot at the end).
1509 * This routine is not needed during the normal operation and serves
1510 * for debugging purposes only.
1512 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1514 if (put_wait_data(cosa, 'r') == -1) return -1;
1515 if ((get_wait_data(cosa)) != 'r') return -2;
1516 if ((get_wait_data(cosa)) != '=') return -3;
1518 if (puthexnumber(cosa, address) < 0) return -4;
1519 if (put_wait_data(cosa, ' ') == -1) return -5;
1520 if (get_wait_data(cosa) != ' ') return -6;
1521 if (get_wait_data(cosa) != '=') return -7;
1523 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1524 if (put_wait_data(cosa, ' ') == -1) return -9;
1525 if (get_wait_data(cosa) != ' ') return -10;
1527 while (length--) {
1528 char c;
1529 int i;
1530 if ((i=get_wait_data(cosa)) == -1) {
1531 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1532 length);
1533 return -11;
1535 c=i;
1536 #if 1
1537 if (put_user(c, microcode))
1538 return -23; /* ??? */
1539 #else
1540 *microcode = c;
1541 #endif
1542 microcode++;
1545 if (get_wait_data(cosa) != '\r') return -21;
1546 if (get_wait_data(cosa) != '\n') return -22;
1547 if (get_wait_data(cosa) != '.') return -23;
1548 #if 0
1549 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1550 #endif
1551 return 0;
1555 * This function resets the device and reads the initial prompt
1556 * of the device's ROM monitor.
1558 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1560 int i=0, id=0, prev=0, curr=0;
1562 /* Reset the card ... */
1563 cosa_putstatus(cosa, 0);
1564 cosa_getdata8(cosa);
1565 cosa_putstatus(cosa, SR_RST);
1566 #ifdef MODULE
1567 msleep(500);
1568 #else
1569 udelay(5*100000);
1570 #endif
1571 /* Disable all IRQs from the card */
1572 cosa_putstatus(cosa, 0);
1575 * Try to read the ID string. The card then prints out the
1576 * identification string ended by the "\n\x2e".
1578 * The following loop is indexed through i (instead of id)
1579 * to avoid looping forever when for any reason
1580 * the port returns '\r', '\n' or '\x2e' permanently.
1582 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1583 if ((curr = get_wait_data(cosa)) == -1) {
1584 return -1;
1586 curr &= 0xff;
1587 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1588 idstring[id++] = curr;
1589 if (curr == 0x2e && prev == '\n')
1590 break;
1592 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1593 idstring[id] = '\0';
1594 return id;
1598 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1601 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1602 * bit to be set in a loop. It should be used in the exceptional cases
1603 * only (for example when resetting the card or downloading the firmware.
1605 static int get_wait_data(struct cosa_data *cosa)
1607 int retries = 1000;
1609 while (--retries) {
1610 /* read data and return them */
1611 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1612 short r;
1613 r = cosa_getdata8(cosa);
1614 #if 0
1615 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1616 #endif
1617 return r;
1619 /* sleep if not ready to read */
1620 set_current_state(TASK_INTERRUPTIBLE);
1621 schedule_timeout(1);
1623 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1624 cosa_getstatus(cosa));
1625 return -1;
1629 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1630 * bit to be set in a loop. It should be used in the exceptional cases
1631 * only (for example when resetting the card or downloading the firmware).
1633 static int put_wait_data(struct cosa_data *cosa, int data)
1635 int retries = 1000;
1636 while (--retries) {
1637 /* read data and return them */
1638 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1639 cosa_putdata8(cosa, data);
1640 #if 0
1641 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1642 #endif
1643 return 0;
1645 #if 0
1646 /* sleep if not ready to read */
1647 current->state = TASK_INTERRUPTIBLE;
1648 schedule_timeout(1);
1649 #endif
1651 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1652 cosa->num, cosa_getstatus(cosa));
1653 return -1;
1657 * The following routine puts the hexadecimal number into the SRP monitor
1658 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1659 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1660 * (-2,-4,-6,-8) means that reading echo failed.
1662 static int puthexnumber(struct cosa_data *cosa, int number)
1664 char temp[5];
1665 int i;
1667 /* Well, I should probably replace this by something faster. */
1668 sprintf(temp, "%04X", number);
1669 for (i=0; i<4; i++) {
1670 if (put_wait_data(cosa, temp[i]) == -1) {
1671 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1672 cosa->num, i);
1673 return -1-2*i;
1675 if (get_wait_data(cosa) != temp[i]) {
1676 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1677 cosa->num, i);
1678 return -2-2*i;
1681 return 0;
1685 /* ---------- Interrupt routines ---------- */
1688 * There are three types of interrupt:
1689 * At the beginning of transmit - this handled is in tx_interrupt(),
1690 * at the beginning of receive - it is in rx_interrupt() and
1691 * at the end of transmit/receive - it is the eot_interrupt() function.
1692 * These functions are multiplexed by cosa_interrupt() according to the
1693 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1694 * separate functions to make it more readable. These functions are inline,
1695 * so there should be no overhead of function call.
1697 * In the COSA bus-master mode, we need to tell the card the address of a
1698 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1699 * It's time to use the bottom half :-(
1703 * Transmit interrupt routine - called when COSA is willing to obtain
1704 * data from the OS. The most tricky part of the routine is selection
1705 * of channel we (OS) want to send packet for. For SRP we should probably
1706 * use the round-robin approach. The newer COSA firmwares have a simple
1707 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1708 * channel 0 or 1 doesn't want to receive data.
1710 * It seems there is a bug in COSA firmware (need to trace it further):
1711 * When the driver status says that the kernel has no more data for transmit
1712 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1713 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1714 * the TX interrupt but does not mark the channel as ready-to-transmit.
1715 * The fix seems to be to push the packet to COSA despite its request.
1716 * We first try to obey the card's opinion, and then fall back to forced TX.
1718 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1720 unsigned long flags, flags1;
1721 #ifdef DEBUG_IRQS
1722 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1723 cosa->num, status);
1724 #endif
1725 spin_lock_irqsave(&cosa->lock, flags);
1726 set_bit(TXBIT, &cosa->rxtx);
1727 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1728 /* flow control, see the comment above */
1729 int i=0;
1730 if (!cosa->txbitmap) {
1731 printk(KERN_WARNING "%s: No channel wants data "
1732 "in TX IRQ. Expect DMA timeout.",
1733 cosa->name);
1734 put_driver_status_nolock(cosa);
1735 clear_bit(TXBIT, &cosa->rxtx);
1736 spin_unlock_irqrestore(&cosa->lock, flags);
1737 return;
1739 while(1) {
1740 cosa->txchan++;
1741 i++;
1742 if (cosa->txchan >= cosa->nchannels)
1743 cosa->txchan = 0;
1744 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1745 continue;
1746 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1747 break;
1748 /* in second pass, accept first ready-to-TX channel */
1749 if (i > cosa->nchannels) {
1750 /* Can be safely ignored */
1751 #ifdef DEBUG_IRQS
1752 printk(KERN_DEBUG "%s: Forcing TX "
1753 "to not-ready channel %d\n",
1754 cosa->name, cosa->txchan);
1755 #endif
1756 break;
1760 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1761 if (cosa_dma_able(cosa->chan+cosa->txchan,
1762 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1763 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1764 } else {
1765 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1766 cosa->txsize);
1767 cosa->txbuf = cosa->bouncebuf;
1771 if (is_8bit(cosa)) {
1772 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1773 cosa_putstatus(cosa, SR_TX_INT_ENA);
1774 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1775 ((cosa->txsize >> 8) & 0x1f));
1776 #ifdef DEBUG_IO
1777 debug_status_out(cosa, SR_TX_INT_ENA);
1778 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1779 ((cosa->txsize >> 8) & 0x1f));
1780 debug_data_in(cosa, cosa_getdata8(cosa));
1781 #else
1782 cosa_getdata8(cosa);
1783 #endif
1784 set_bit(IRQBIT, &cosa->rxtx);
1785 spin_unlock_irqrestore(&cosa->lock, flags);
1786 return;
1787 } else {
1788 clear_bit(IRQBIT, &cosa->rxtx);
1789 cosa_putstatus(cosa, 0);
1790 cosa_putdata8(cosa, cosa->txsize&0xff);
1791 #ifdef DEBUG_IO
1792 debug_status_out(cosa, 0);
1793 debug_data_out(cosa, cosa->txsize&0xff);
1794 #endif
1796 } else {
1797 cosa_putstatus(cosa, SR_TX_INT_ENA);
1798 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1799 | (cosa->txsize & 0x1fff));
1800 #ifdef DEBUG_IO
1801 debug_status_out(cosa, SR_TX_INT_ENA);
1802 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1803 | (cosa->txsize & 0x1fff));
1804 debug_data_in(cosa, cosa_getdata8(cosa));
1805 debug_status_out(cosa, 0);
1806 #else
1807 cosa_getdata8(cosa);
1808 #endif
1809 cosa_putstatus(cosa, 0);
1812 if (cosa->busmaster) {
1813 unsigned long addr = virt_to_bus(cosa->txbuf);
1814 int count=0;
1815 printk(KERN_INFO "busmaster IRQ\n");
1816 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1817 count++;
1818 udelay(10);
1819 if (count > 1000) break;
1821 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1822 printk(KERN_INFO "ready after %d loops\n", count);
1823 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1825 count = 0;
1826 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1827 count++;
1828 if (count > 1000) break;
1829 udelay(10);
1831 printk(KERN_INFO "ready after %d loops\n", count);
1832 cosa_putdata16(cosa, addr &0xffff);
1833 flags1 = claim_dma_lock();
1834 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1835 enable_dma(cosa->dma);
1836 release_dma_lock(flags1);
1837 } else {
1838 /* start the DMA */
1839 flags1 = claim_dma_lock();
1840 disable_dma(cosa->dma);
1841 clear_dma_ff(cosa->dma);
1842 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1843 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1844 set_dma_count(cosa->dma, cosa->txsize);
1845 enable_dma(cosa->dma);
1846 release_dma_lock(flags1);
1848 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1849 #ifdef DEBUG_IO
1850 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1851 #endif
1852 spin_unlock_irqrestore(&cosa->lock, flags);
1855 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1857 unsigned long flags;
1858 #ifdef DEBUG_IRQS
1859 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1860 #endif
1862 spin_lock_irqsave(&cosa->lock, flags);
1863 set_bit(RXBIT, &cosa->rxtx);
1865 if (is_8bit(cosa)) {
1866 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1867 set_bit(IRQBIT, &cosa->rxtx);
1868 put_driver_status_nolock(cosa);
1869 cosa->rxsize = cosa_getdata8(cosa) <<8;
1870 #ifdef DEBUG_IO
1871 debug_data_in(cosa, cosa->rxsize >> 8);
1872 #endif
1873 spin_unlock_irqrestore(&cosa->lock, flags);
1874 return;
1875 } else {
1876 clear_bit(IRQBIT, &cosa->rxtx);
1877 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1878 #ifdef DEBUG_IO
1879 debug_data_in(cosa, cosa->rxsize & 0xff);
1880 #endif
1881 #if 0
1882 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1883 cosa->num, cosa->rxsize);
1884 #endif
1886 } else {
1887 cosa->rxsize = cosa_getdata16(cosa);
1888 #ifdef DEBUG_IO
1889 debug_data_in(cosa, cosa->rxsize);
1890 #endif
1891 #if 0
1892 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1893 cosa->num, cosa->rxsize);
1894 #endif
1896 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1897 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1898 cosa->name, cosa->rxsize);
1899 spin_unlock_irqrestore(&cosa->lock, flags);
1900 goto reject;
1902 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1903 cosa->rxsize &= 0x1fff;
1904 spin_unlock_irqrestore(&cosa->lock, flags);
1906 cosa->rxbuf = NULL;
1907 if (cosa->rxchan->setup_rx)
1908 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1910 if (!cosa->rxbuf) {
1911 reject: /* Reject the packet */
1912 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1913 cosa->num, cosa->rxchan->num);
1914 cosa->rxbuf = cosa->bouncebuf;
1917 /* start the DMA */
1918 flags = claim_dma_lock();
1919 disable_dma(cosa->dma);
1920 clear_dma_ff(cosa->dma);
1921 set_dma_mode(cosa->dma, DMA_MODE_READ);
1922 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1923 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1924 } else {
1925 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1927 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1928 enable_dma(cosa->dma);
1929 release_dma_lock(flags);
1930 spin_lock_irqsave(&cosa->lock, flags);
1931 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1932 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1933 cosa_putdata8(cosa, DRIVER_RX_READY);
1934 #ifdef DEBUG_IO
1935 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1936 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1937 debug_data_cmd(cosa, DRIVER_RX_READY);
1938 #endif
1939 spin_unlock_irqrestore(&cosa->lock, flags);
1942 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1944 unsigned long flags, flags1;
1945 spin_lock_irqsave(&cosa->lock, flags);
1946 flags1 = claim_dma_lock();
1947 disable_dma(cosa->dma);
1948 clear_dma_ff(cosa->dma);
1949 release_dma_lock(flags1);
1950 if (test_bit(TXBIT, &cosa->rxtx)) {
1951 struct channel_data *chan = cosa->chan+cosa->txchan;
1952 if (chan->tx_done)
1953 if (chan->tx_done(chan, cosa->txsize))
1954 clear_bit(chan->num, &cosa->txbitmap);
1955 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1956 #ifdef DEBUG_DATA
1958 int i;
1959 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1960 cosa->rxchan->num, cosa->rxsize);
1961 for (i=0; i<cosa->rxsize; i++)
1962 printk (" %02x", cosa->rxbuf[i]&0xff);
1963 printk("\n");
1965 #endif
1966 /* Packet for unknown channel? */
1967 if (cosa->rxbuf == cosa->bouncebuf)
1968 goto out;
1969 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1970 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1971 if (cosa->rxchan->rx_done)
1972 if (cosa->rxchan->rx_done(cosa->rxchan))
1973 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1974 } else {
1975 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1976 cosa->num);
1979 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1980 * cleared anyway). We should do it as soon as possible
1981 * so that we can tell the COSA we are done and to give it a time
1982 * for recovery.
1984 out:
1985 cosa->rxtx = 0;
1986 put_driver_status_nolock(cosa);
1987 spin_unlock_irqrestore(&cosa->lock, flags);
1990 static irqreturn_t cosa_interrupt(int irq, void *cosa_, struct pt_regs *regs)
1992 unsigned status;
1993 int count = 0;
1994 struct cosa_data *cosa = cosa_;
1995 again:
1996 status = cosa_getstatus(cosa);
1997 #ifdef DEBUG_IRQS
1998 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1999 status & 0xff);
2000 #endif
2001 #ifdef DEBUG_IO
2002 debug_status_in(cosa, status);
2003 #endif
2004 switch (status & SR_CMD_FROM_SRP_MASK) {
2005 case SR_DOWN_REQUEST:
2006 tx_interrupt(cosa, status);
2007 break;
2008 case SR_UP_REQUEST:
2009 rx_interrupt(cosa, status);
2010 break;
2011 case SR_END_OF_TRANSFER:
2012 eot_interrupt(cosa, status);
2013 break;
2014 default:
2015 /* We may be too fast for SRP. Try to wait a bit more. */
2016 if (count++ < 100) {
2017 udelay(100);
2018 goto again;
2020 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
2021 cosa->num, status & 0xff, count);
2023 #ifdef DEBUG_IRQS
2024 if (count)
2025 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
2026 cosa->name, count);
2027 else
2028 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
2029 #endif
2030 return IRQ_HANDLED;
2034 /* ---------- I/O debugging routines ---------- */
2036 * These routines can be used to monitor COSA/SRP I/O and to printk()
2037 * the data being transferred on the data and status I/O port in a
2038 * readable way.
2041 #ifdef DEBUG_IO
2042 static void debug_status_in(struct cosa_data *cosa, int status)
2044 char *s;
2045 switch(status & SR_CMD_FROM_SRP_MASK) {
2046 case SR_UP_REQUEST:
2047 s = "RX_REQ";
2048 break;
2049 case SR_DOWN_REQUEST:
2050 s = "TX_REQ";
2051 break;
2052 case SR_END_OF_TRANSFER:
2053 s = "ET_REQ";
2054 break;
2055 default:
2056 s = "NO_REQ";
2057 break;
2059 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2060 cosa->name,
2061 status,
2062 status & SR_USR_RQ ? "USR_RQ|":"",
2063 status & SR_TX_RDY ? "TX_RDY|":"",
2064 status & SR_RX_RDY ? "RX_RDY|":"",
2068 static void debug_status_out(struct cosa_data *cosa, int status)
2070 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2071 cosa->name,
2072 status,
2073 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
2074 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
2075 status & SR_RST ? "RESET|":"",
2076 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2077 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
2078 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
2081 static void debug_data_in(struct cosa_data *cosa, int data)
2083 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2086 static void debug_data_out(struct cosa_data *cosa, int data)
2088 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2091 static void debug_data_cmd(struct cosa_data *cosa, int data)
2093 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2094 cosa->name, data,
2095 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2096 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2098 #endif
2100 /* EOF -- this file has not been truncated */