File: /usr/src/linux/drivers/net/wan/cosa.c

1     /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
2     
3     /*
4      *  Copyright (C) 1995-1997  Jan "Yenya" Kasprzak <kas@fi.muni.cz>
5      *
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.
10      *
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.
15      *
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.
19      */
20     
21     /*
22      * The driver for the SRP and COSA synchronous serial cards.
23      *
24      * HARDWARE INFO
25      *
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.
34      *
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.
38      *
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.
44      *
45      * Both types have downloadable firmware and communicate via ISA DMA.
46      * COSA can be also a bus-mastering device.
47      *
48      * SOFTWARE INFO
49      *
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.
54      *
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).
58      *
59      * AUTHOR
60      *
61      * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
62      *
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 :-).
66      *
67      * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
68      *
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
74      */
75     /*
76      *     5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
77      *             fixed a deadlock in cosa_sppp_open
78      */
79     
80     /* ---------- Headers, macros, data structures ---------- */
81     
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/sched.h>
90     #include <linux/interrupt.h>
91     #include <linux/delay.h>
92     #include <linux/errno.h>
93     #include <linux/ioport.h>
94     #include <linux/netdevice.h>
95     #include <linux/spinlock.h>
96     #include <linux/smp_lock.h>
97     
98     #undef COSA_SLOW_IO	/* for testing purposes only */
99     #undef REALLY_SLOW_IO
100     
101     #include <asm/io.h>
102     #include <asm/dma.h>
103     #include <asm/byteorder.h>
104     
105     #include <net/syncppp.h>
106     #include "cosa.h"
107     
108     /* Linux version stuff */
109     #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1)
110     typedef struct wait_queue *wait_queue_head_t;
111     #define DECLARE_WAITQUEUE(wait, current) \
112     	struct wait_queue wait = { current, NULL }
113     #endif
114     
115     /* Maximum length of the identification string. */
116     #define COSA_MAX_ID_STRING	128
117     
118     /* Maximum length of the channel name */
119     #define COSA_MAX_NAME		(sizeof("cosaXXXcXXX")+1)
120     
121     /* Per-channel data structure */
122     
123     struct channel_data {
124     	void *if_ptr;	/* General purpose pointer (used by SPPP) */
125     	int usage;	/* Usage count; >0 for chrdev, -1 for netdev */
126     	int num;	/* Number of the channel */
127     	struct cosa_data *cosa;	/* Pointer to the per-card structure */
128     	int txsize;	/* Size of transmitted data */
129     	char *txbuf;	/* Transmit buffer */
130     	char name[COSA_MAX_NAME];	/* channel name */
131     
132     	/* The HW layer interface */
133     	/* routine called from the RX interrupt */
134     	char *(*setup_rx)(struct channel_data *channel, int size);
135     	/* routine called when the RX is done (from the EOT interrupt) */
136     	int (*rx_done)(struct channel_data *channel);
137     	/* routine called when the TX is done (from the EOT interrupt) */
138     	int (*tx_done)(struct channel_data *channel, int size);
139     
140     	/* Character device parts */
141     	struct semaphore rsem, wsem;
142     	char *rxdata;
143     	int rxsize;
144     	wait_queue_head_t txwaitq, rxwaitq;
145     	int tx_status, rx_status;
146     
147     	/* SPPP/HDLC device parts */
148     	struct ppp_device pppdev;
149     	struct sk_buff *rx_skb, *tx_skb;
150     	struct net_device_stats stats;
151     };
152     
153     /* cosa->firmware_status bits */
154     #define COSA_FW_RESET		(1<<0)	/* Is the ROM monitor active? */
155     #define COSA_FW_DOWNLOAD	(1<<1)	/* Is the microcode downloaded? */
156     #define COSA_FW_START		(1<<2)	/* Is the microcode running? */
157     
158     struct cosa_data {
159     	int num;			/* Card number */
160     	char name[COSA_MAX_NAME];	/* Card name - e.g "cosa0" */
161     	unsigned int datareg, statusreg;	/* I/O ports */
162     	unsigned short irq, dma;	/* IRQ and DMA number */
163     	unsigned short startaddr;	/* Firmware start address */
164     	unsigned short busmaster;	/* Use busmastering? */
165     	int nchannels;			/* # of channels on this card */
166     	int driver_status;		/* For communicating with firware */
167     	int firmware_status;		/* Downloaded, reseted, etc. */
168     	long int rxbitmap, txbitmap;	/* Bitmap of channels who are willing to send/receive data */
169     	long int rxtx;			/* RX or TX in progress? */
170     	int enabled;
171     	int usage;				/* usage count */
172     	int txchan, txsize, rxsize;
173     	struct channel_data *rxchan;
174     	char *bouncebuf;
175     	char *txbuf, *rxbuf;
176     	struct channel_data *chan;
177     	spinlock_t lock;	/* For exclusive operations on this structure */
178     	char id_string[COSA_MAX_ID_STRING];	/* ROM monitor ID string */
179     	char *type;				/* card type */
180     };
181     
182     /*
183      * Define this if you want all the possible ports to be autoprobed.
184      * It is here but it probably is not a good idea to use this.
185      */
186     /* #define COSA_ISA_AUTOPROBE	1 */
187     
188     /*
189      * Character device major number. 117 was allocated for us.
190      * The value of 0 means to allocate a first free one.
191      */
192     static int cosa_major = 117;
193     
194     /*
195      * Encoding of the minor numbers:
196      * The lowest CARD_MINOR_BITS bits means the channel on the single card,
197      * the highest bits means the card number.
198      */
199     #define CARD_MINOR_BITS	4	/* How many bits in minor number are reserved
200     				 * for the single card */
201     /*
202      * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
203      * macro doesn't like anything other than the raw number as an argument :-(
204      */
205     #define MAX_CARDS	16
206     /* #define MAX_CARDS	(1 << (8-CARD_MINOR_BITS)) */
207     
208     #define DRIVER_RX_READY		0x0001
209     #define DRIVER_TX_READY		0x0002
210     #define DRIVER_TXMAP_SHIFT	2
211     #define DRIVER_TXMAP_MASK	0x0c	/* FIXME: 0xfc for 8-channel version */
212     
213     /*
214      * for cosa->rxtx - indicates whether either transmit or receive is
215      * in progress. These values are mean number of the bit.
216      */
217     #define TXBIT 0
218     #define RXBIT 1
219     #define IRQBIT 2
220     
221     #define COSA_MTU 2000	/* FIXME: I don't know this exactly */
222     
223     #undef DEBUG_DATA //1	/* Dump the data read or written to the channel */
224     #undef DEBUG_IRQS //1	/* Print the message when the IRQ is received */
225     #undef DEBUG_IO   //1	/* Dump the I/O traffic */
226     
227     #define TX_TIMEOUT	(5*HZ)
228     
229     /* Maybe the following should be allocated dynamically */
230     static struct cosa_data cosa_cards[MAX_CARDS];
231     static int nr_cards;
232     
233     #ifdef COSA_ISA_AUTOPROBE
234     static int io[MAX_CARDS+1]  = { 0x220, 0x228, 0x210, 0x218, 0, };
235     /* NOTE: DMA is not autoprobed!!! */
236     static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
237     #else
238     int io[MAX_CARDS+1]  = { 0, };
239     int dma[MAX_CARDS+1] = { 0, };
240     #endif
241     /* IRQ can be safely autoprobed */
242     static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
243     
244     #ifdef MODULE
245     MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");
246     MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
247     MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
248     MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
249     MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");
250     MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
251     
252     MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
253     MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
254     MODULE_LICENSE("GPL");
255     #endif
256     
257     /* I use this mainly for testing purposes */
258     #ifdef COSA_SLOW_IO
259     #define cosa_outb outb_p
260     #define cosa_outw outw_p
261     #define cosa_inb  inb_p
262     #define cosa_inw  inw_p
263     #else
264     #define cosa_outb outb
265     #define cosa_outw outw
266     #define cosa_inb  inb
267     #define cosa_inw  inw
268     #endif
269     
270     #define is_8bit(cosa)		(!(cosa->datareg & 0x08))
271     
272     #define cosa_getstatus(cosa)	(cosa_inb(cosa->statusreg))
273     #define cosa_putstatus(cosa, stat)	(cosa_outb(stat, cosa->statusreg))
274     #define cosa_getdata16(cosa)	(cosa_inw(cosa->datareg))
275     #define cosa_getdata8(cosa)	(cosa_inb(cosa->datareg))
276     #define cosa_putdata16(cosa, dt)	(cosa_outw(dt, cosa->datareg))
277     #define cosa_putdata8(cosa, dt)	(cosa_outb(dt, cosa->datareg))
278     
279     /* Initialization stuff */
280     static int cosa_probe(int ioaddr, int irq, int dma);
281     
282     /* HW interface */
283     static void cosa_enable_rx(struct channel_data *chan);
284     static void cosa_disable_rx(struct channel_data *chan);
285     static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
286     static void cosa_kick(struct cosa_data *cosa);
287     static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
288     
289     /* SPPP/HDLC stuff */
290     static void sppp_channel_init(struct channel_data *chan);
291     static void sppp_channel_delete(struct channel_data *chan);
292     static int cosa_sppp_open(struct net_device *d);
293     static int cosa_sppp_close(struct net_device *d);
294     static void cosa_sppp_timeout(struct net_device *d);
295     static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *d);
296     static char *sppp_setup_rx(struct channel_data *channel, int size);
297     static int sppp_rx_done(struct channel_data *channel);
298     static int sppp_tx_done(struct channel_data *channel, int size);
299     static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
300     static struct net_device_stats *cosa_net_stats(struct net_device *dev);
301     
302     /* Character device */
303     static void chardev_channel_init(struct channel_data *chan);
304     static char *chrdev_setup_rx(struct channel_data *channel, int size);
305     static int chrdev_rx_done(struct channel_data *channel);
306     static int chrdev_tx_done(struct channel_data *channel, int size);
307     static ssize_t cosa_read(struct file *file,
308     	char *buf, size_t count, loff_t *ppos);
309     static ssize_t cosa_write(struct file *file,
310     	const char *buf, size_t count, loff_t *ppos);
311     static unsigned int cosa_poll(struct file *file, poll_table *poll);
312     static int cosa_open(struct inode *inode, struct file *file);
313     static int cosa_release(struct inode *inode, struct file *file);
314     static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
315     	unsigned int cmd, unsigned long arg);
316     #ifdef COSA_FASYNC_WORKING
317     static int cosa_fasync(struct inode *inode, struct file *file, int on);
318     #endif
319     
320     static struct file_operations cosa_fops = {
321     	owner:		THIS_MODULE,
322     	llseek:		no_llseek,
323     	read:		cosa_read,
324     	write:		cosa_write,
325     	poll:		cosa_poll,
326     	ioctl:		cosa_chardev_ioctl,
327     	open:		cosa_open,
328     	release:	cosa_release,
329     #ifdef COSA_FASYNC_WORKING
330     	fasync:		cosa_fasync,
331     #endif
332     };
333     
334     /* Ioctls */
335     static int cosa_start(struct cosa_data *cosa, int address);
336     static int cosa_reset(struct cosa_data *cosa);
337     static int cosa_download(struct cosa_data *cosa, struct cosa_download *d);
338     static int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d);
339     
340     /* COSA/SRP ROM monitor */
341     static int download(struct cosa_data *cosa, char *data, int addr, int len);
342     static int startmicrocode(struct cosa_data *cosa, int address);
343     static int readmem(struct cosa_data *cosa, char *data, int addr, int len);
344     static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
345     
346     /* Auxilliary functions */
347     static int get_wait_data(struct cosa_data *cosa);
348     static int put_wait_data(struct cosa_data *cosa, int data);
349     static int puthexnumber(struct cosa_data *cosa, int number);
350     static void put_driver_status(struct cosa_data *cosa);
351     static void put_driver_status_nolock(struct cosa_data *cosa);
352     
353     /* Interrupt handling */
354     static void cosa_interrupt(int irq, void *cosa, struct pt_regs *regs);
355     
356     /* I/O ops debugging */
357     #ifdef DEBUG_IO
358     static void debug_data_in(struct cosa_data *cosa, int data);
359     static void debug_data_out(struct cosa_data *cosa, int data);
360     static void debug_data_cmd(struct cosa_data *cosa, int data);
361     static void debug_status_in(struct cosa_data *cosa, int status);
362     static void debug_status_out(struct cosa_data *cosa, int status);
363     #endif
364     
365     
366     /* ---------- Initialization stuff ---------- */
367     
368     static devfs_handle_t devfs_handle;
369     
370     #ifdef MODULE
371     int init_module(void)
372     #else
373     static int __init cosa_init(void)
374     #endif
375     {
376     	int i;
377     
378     	printk(KERN_INFO "cosa v1.08 (c) 1997-2000 Jan Kasprzak <kas@fi.muni.cz>\n");
379     #ifdef CONFIG_SMP
380     	printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
381     #endif
382     	if (cosa_major > 0) {
383     		if (devfs_register_chrdev(cosa_major, "cosa", &cosa_fops)) {
384     			printk(KERN_WARNING "cosa: unable to get major %d\n",
385     				cosa_major);
386     			return -EIO;
387     		}
388     	} else {
389     		if (!(cosa_major=devfs_register_chrdev(0, "cosa", &cosa_fops))) {
390     			printk(KERN_WARNING "cosa: unable to register chardev\n");
391     			return -EIO;
392     		}
393     	}
394     	for (i=0; i<MAX_CARDS; i++)
395     		cosa_cards[i].num = -1;
396     	for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
397     		cosa_probe(io[i], irq[i], dma[i]);
398     	devfs_handle = devfs_mk_dir (NULL, "cosa", NULL);
399     	devfs_register_series (devfs_handle, "%u", nr_cards, DEVFS_FL_DEFAULT,
400     			       cosa_major, 0,
401     			       S_IFCHR | S_IRUSR | S_IWUSR,
402     			       &cosa_fops, NULL);
403     	if (!nr_cards) {
404     		printk(KERN_WARNING "cosa: no devices found.\n");
405     		devfs_unregister_chrdev(cosa_major, "cosa");
406     		return -ENODEV;
407     	}
408     	return 0;
409     }
410     
411     #ifdef MODULE
412     void cleanup_module (void)
413     {
414     	struct cosa_data *cosa;
415     	printk(KERN_INFO "Unloading the cosa module\n");
416     
417     	devfs_unregister (devfs_handle);
418     	for (cosa=cosa_cards; nr_cards--; cosa++) {
419     		int i;
420     		/* Clean up the per-channel data */
421     		for (i=0; i<cosa->nchannels; i++) {
422     			/* Chardev driver has no alloc'd per-channel data */
423     			sppp_channel_delete(cosa->chan+i);
424     		}
425     		/* Clean up the per-card data */
426     		kfree(cosa->chan);
427     		kfree(cosa->bouncebuf);
428     		free_irq(cosa->irq, cosa);
429     		free_dma(cosa->dma);
430     		release_region(cosa->datareg,is_8bit(cosa)?2:4);
431     	}
432     	devfs_unregister_chrdev(cosa_major, "cosa");
433     }
434     #endif
435     
436     /*
437      * This function should register all the net devices needed for the
438      * single channel.
439      */
440     static __inline__ void channel_init(struct channel_data *chan)
441     {
442     	sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
443     
444     	/* Initialize the chardev data structures */
445     	chardev_channel_init(chan);
446     
447     	/* Register the sppp interface */
448     	sppp_channel_init(chan);
449     }
450     	
451     static int cosa_probe(int base, int irq, int dma)
452     {
453     	struct cosa_data *cosa = cosa_cards+nr_cards;
454     	int i;
455     
456     	memset(cosa, 0, sizeof(struct cosa_data));
457     
458     	/* Checking validity of parameters: */
459     	/* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
460     	if ((irq >= 0  && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
461     		printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
462     		return -1;
463     	}
464     	/* I/O address should be between 0x100 and 0x3ff and should be
465     	 * multiple of 8. */
466     	if (base < 0x100 || base > 0x3ff || base & 0x7) {
467     		printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
468     			base);
469     		return -1;
470     	}
471     	/* DMA should be 0,1 or 3-7 */
472     	if (dma < 0 || dma == 4 || dma > 7) {
473     		printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
474     		return -1;
475     	}
476     	/* and finally, on 16-bit COSA DMA should be 4-7 and 
477     	 * I/O base should not be multiple of 0x10 */
478     	if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
479     		printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
480     			" (base=0x%x, dma=%d)\n", base, dma);
481     		return -1;
482     	}
483     
484     	cosa->dma = dma;
485     	cosa->datareg = base;
486     	cosa->statusreg = is_8bit(cosa)?base+1:base+2;
487     	spin_lock_init(&cosa->lock);
488     
489     	if (check_region(base, is_8bit(cosa)?2:4))
490     		return -1;
491     	
492     	if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
493     		printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
494     		return -1;
495     	}
496     
497     	/* Test the validity of identification string */
498     	if (!strncmp(cosa->id_string, "SRP", 3))
499     		cosa->type = "srp";
500     	else if (!strncmp(cosa->id_string, "COSA", 4))
501     		cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
502     	else {
503     /* Print a warning only if we are not autoprobing */
504     #ifndef COSA_ISA_AUTOPROBE
505     		printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
506     			base);
507     #endif
508     		return -1;
509     	}
510     
511     	/* Now do IRQ autoprobe */
512     	if (irq < 0) {
513     		unsigned long irqs;
514     /*		printk(KERN_INFO "IRQ autoprobe\n"); */
515     		sti();
516     		irqs = probe_irq_on();
517     		/* 
518     		 * Enable interrupt on tx buffer empty (it sure is) 
519     		 * really sure ?
520     		 * FIXME: When this code is not used as module, we should
521     		 * probably call udelay() instead of the interruptible sleep.
522     		 */
523     		current->state = TASK_INTERRUPTIBLE;
524     		cosa_putstatus(cosa, SR_TX_INT_ENA);
525     		schedule_timeout(30);
526     		current->state = TASK_RUNNING;
527     		irq = probe_irq_off(irqs);
528     		/* Disable all IRQs from the card */
529     		cosa_putstatus(cosa, 0);
530     		/* Empty the received data register */
531     		cosa_getdata8(cosa);
532     
533     		if (irq < 0) {
534     			printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
535     				irq, cosa->datareg);
536     			return -1;
537     		}
538     		if (irq == 0) {
539     			printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
540     				cosa->datareg);
541     		/*	return -1; */
542     		}
543     	}
544     
545     	cosa->irq = irq;
546     	cosa->num = nr_cards;
547     	cosa->usage = 0;
548     	cosa->nchannels = 2;	/* FIXME: how to determine this? */
549     
550     	request_region(base, is_8bit(cosa)?2:4, cosa->type);
551     	if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa))
552     		goto bad1;
553     	if (request_dma(cosa->dma, cosa->type)) {
554     		free_irq(cosa->irq, cosa);
555     bad1:		release_region(cosa->datareg,is_8bit(cosa)?2:4);
556     		printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
557     			cosa->num);
558     		return -1;
559     	}
560     	
561     	cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
562     	sprintf(cosa->name, "cosa%d", cosa->num);
563     
564     	/* Initialize the per-channel data */
565     	cosa->chan = kmalloc(sizeof(struct channel_data)*cosa->nchannels,
566     		GFP_KERNEL);
567     	memset(cosa->chan, 0, sizeof(struct channel_data)*cosa->nchannels);
568     	for (i=0; i<cosa->nchannels; i++) {
569     		cosa->chan[i].cosa = cosa;
570     		cosa->chan[i].num = i;
571     		channel_init(cosa->chan+i);
572     	}
573     
574     	printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
575     		cosa->num, cosa->id_string, cosa->type,
576     		cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
577     
578     	return nr_cards++;
579     }
580     
581     
582     /*---------- SPPP/HDLC netdevice ---------- */
583     
584     static void sppp_channel_init(struct channel_data *chan)
585     {
586     	struct net_device *d;
587     	chan->if_ptr = &chan->pppdev;
588     	chan->pppdev.dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
589     	memset(chan->pppdev.dev, 0, sizeof(struct net_device));
590     	sppp_attach(&chan->pppdev);
591     	d=chan->pppdev.dev;
592     	strcpy(d->name, chan->name);
593     	d->base_addr = chan->cosa->datareg;
594     	d->irq = chan->cosa->irq;
595     	d->dma = chan->cosa->dma;
596     	d->priv = chan;
597     	d->init = NULL;
598     	d->open = cosa_sppp_open;
599     	d->stop = cosa_sppp_close;
600     	d->hard_start_xmit = cosa_sppp_tx;
601     	d->do_ioctl = cosa_sppp_ioctl;
602     	d->get_stats = cosa_net_stats;
603     	d->tx_timeout = cosa_sppp_timeout;
604     	d->watchdog_timeo = TX_TIMEOUT;
605     	if (register_netdev(d) == -1) {
606     		printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
607     		sppp_detach(chan->pppdev.dev);
608     		return;
609     	}
610     }
611     
612     static void sppp_channel_delete(struct channel_data *chan)
613     {
614     	sppp_detach(chan->pppdev.dev);
615     	unregister_netdev(chan->pppdev.dev);
616     }
617     
618     static int cosa_sppp_open(struct net_device *d)
619     {
620     	struct channel_data *chan = d->priv;
621     	int err, flags;
622     
623     	if (!(chan->cosa->firmware_status & COSA_FW_START)) {
624     		printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
625     			chan->cosa->name, chan->cosa->firmware_status);
626     		return -EPERM;
627     	}
628     	spin_lock_irqsave(&chan->cosa->lock, flags);
629     	if (chan->usage != 0) {
630     		printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
631     			chan->name, chan->usage);
632     		spin_unlock_irqrestore(&chan->cosa->lock, flags);
633     		return -EBUSY;
634     	}
635     	chan->setup_rx = sppp_setup_rx;
636     	chan->tx_done = sppp_tx_done;
637     	chan->rx_done = sppp_rx_done;
638     	chan->usage=-1;
639     	chan->cosa->usage++;
640     	MOD_INC_USE_COUNT;
641     	spin_unlock_irqrestore(&chan->cosa->lock, flags);
642     
643     	err = sppp_open(d);
644     	if (err) {
645     		spin_lock_irqsave(&chan->cosa->lock, flags);
646     		chan->usage=0;
647     		chan->cosa->usage--;
648     		MOD_DEC_USE_COUNT;
649     		
650     		spin_unlock_irqrestore(&chan->cosa->lock, flags);
651     		return err;
652     	}
653     
654     	netif_start_queue(d);
655     	cosa_enable_rx(chan);
656     	return 0;
657     }
658     
659     static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *dev)
660     {
661     	struct channel_data *chan = dev->priv;
662     
663     	netif_stop_queue(dev);
664     
665     	chan->tx_skb = skb;
666     	cosa_start_tx(chan, skb->data, skb->len);
667     	return 0;
668     }
669     
670     static void cosa_sppp_timeout(struct net_device *dev)
671     {
672     	struct channel_data *chan = dev->priv;
673     
674     	if (test_bit(RXBIT, &chan->cosa->rxtx)) {
675     		chan->stats.rx_errors++;
676     		chan->stats.rx_missed_errors++;
677     	} else {
678     		chan->stats.tx_errors++;
679     		chan->stats.tx_aborted_errors++;
680     	}
681     	cosa_kick(chan->cosa);
682     	if (chan->tx_skb) {
683     		dev_kfree_skb(chan->tx_skb);
684     		chan->tx_skb = 0;
685     	}
686     	netif_wake_queue(dev);
687     }
688     
689     static int cosa_sppp_close(struct net_device *d)
690     {
691     	struct channel_data *chan = d->priv;
692     	int flags;
693     
694     	netif_stop_queue(d);
695     	sppp_close(d);
696     	cosa_disable_rx(chan);
697     	spin_lock_irqsave(&chan->cosa->lock, flags);
698     	if (chan->rx_skb) {
699     		kfree_skb(chan->rx_skb);
700     		chan->rx_skb = 0;
701     	}
702     	if (chan->tx_skb) {
703     		kfree_skb(chan->tx_skb);
704     		chan->tx_skb = 0;
705     	}
706     	chan->usage=0;
707     	chan->cosa->usage--;
708     	MOD_DEC_USE_COUNT;
709     	spin_unlock_irqrestore(&chan->cosa->lock, flags);
710     	return 0;
711     }
712     
713     static char *sppp_setup_rx(struct channel_data *chan, int size)
714     {
715     	/*
716     	 * We can safely fall back to non-dma-able memory, because we have
717     	 * the cosa->bouncebuf pre-allocated.
718     	 */
719     	if (chan->rx_skb)
720     		kfree_skb(chan->rx_skb);
721     	chan->rx_skb = dev_alloc_skb(size);
722     	if (chan->rx_skb == NULL) {
723     		printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
724     			chan->name);
725     		chan->stats.rx_dropped++;
726     		return NULL;
727     	}
728     	chan->pppdev.dev->trans_start = jiffies;
729     	return skb_put(chan->rx_skb, size);
730     }
731     
732     static int sppp_rx_done(struct channel_data *chan)
733     {
734     	if (!chan->rx_skb) {
735     		printk(KERN_WARNING "%s: rx_done with empty skb!\n",
736     			chan->name);
737     		chan->stats.rx_errors++;
738     		chan->stats.rx_frame_errors++;
739     		return 0;
740     	}
741     	chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
742     	chan->rx_skb->dev = chan->pppdev.dev;
743     	chan->rx_skb->mac.raw = chan->rx_skb->data;
744     	chan->stats.rx_packets++;
745     	chan->stats.rx_bytes += chan->cosa->rxsize;
746     	netif_rx(chan->rx_skb);
747     	chan->rx_skb = 0;
748     	chan->pppdev.dev->last_rx = jiffies;
749     	return 0;
750     }
751     
752     /* ARGSUSED */
753     static int sppp_tx_done(struct channel_data *chan, int size)
754     {
755     	if (!chan->tx_skb) {
756     		printk(KERN_WARNING "%s: tx_done with empty skb!\n",
757     			chan->name);
758     		chan->stats.tx_errors++;
759     		chan->stats.tx_aborted_errors++;
760     		return 1;
761     	}
762     	dev_kfree_skb_irq(chan->tx_skb);
763     	chan->tx_skb = 0;
764     	chan->stats.tx_packets++;
765     	chan->stats.tx_bytes += size;
766     	netif_wake_queue(chan->pppdev.dev);
767     	return 1;
768     }
769     
770     static struct net_device_stats *cosa_net_stats(struct net_device *dev)
771     {
772     	struct channel_data *chan = dev->priv;
773     	return &chan->stats;
774     }
775     
776     
777     /*---------- Character device ---------- */
778     
779     static void chardev_channel_init(struct channel_data *chan)
780     {
781     	init_MUTEX(&chan->rsem);
782     	init_MUTEX(&chan->wsem);
783     }
784     
785     static ssize_t cosa_read(struct file *file,
786     	char *buf, size_t count, loff_t *ppos)
787     {
788     	DECLARE_WAITQUEUE(wait, current);
789     	int flags;
790     	struct channel_data *chan = (struct channel_data *)file->private_data;
791     	struct cosa_data *cosa = chan->cosa;
792     	char *kbuf;
793     
794     	if (!(cosa->firmware_status & COSA_FW_START)) {
795     		printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
796     			cosa->name, cosa->firmware_status);
797     		return -EPERM;
798     	}
799     	if (down_interruptible(&chan->rsem))
800     		return -ERESTARTSYS;
801     	
802     	if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
803     		printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
804     		up(&chan->rsem);
805     		return -ENOMEM;
806     	}
807     
808     	chan->rx_status = 0;
809     	cosa_enable_rx(chan);
810     	spin_lock_irqsave(&cosa->lock, flags);
811     	add_wait_queue(&chan->rxwaitq, &wait);
812     	while(!chan->rx_status) {
813     		current->state = TASK_INTERRUPTIBLE;
814     		spin_unlock_irqrestore(&cosa->lock, flags);
815     		schedule();
816     		spin_lock_irqsave(&cosa->lock, flags);
817     		if (signal_pending(current) && chan->rx_status == 0) {
818     			chan->rx_status = 1;
819     			remove_wait_queue(&chan->rxwaitq, &wait);
820     			current->state = TASK_RUNNING;
821     			spin_unlock_irqrestore(&cosa->lock, flags);
822     			up(&chan->rsem);
823     			return -ERESTARTSYS;
824     		}
825     	}
826     	remove_wait_queue(&chan->rxwaitq, &wait);
827     	current->state = TASK_RUNNING;
828     	kbuf = chan->rxdata;
829     	count = chan->rxsize;
830     	spin_unlock_irqrestore(&cosa->lock, flags);
831     	up(&chan->rsem);
832     
833     	if (copy_to_user(buf, kbuf, count)) {
834     		kfree(kbuf);
835     		return -EFAULT;
836     	}
837     	kfree(kbuf);
838     	return count;
839     }
840     
841     static char *chrdev_setup_rx(struct channel_data *chan, int size)
842     {
843     	/* Expect size <= COSA_MTU */
844     	chan->rxsize = size;
845     	return chan->rxdata;
846     }
847     
848     static int chrdev_rx_done(struct channel_data *chan)
849     {
850     	if (chan->rx_status) { /* Reader has died */
851     		kfree(chan->rxdata);
852     		up(&chan->wsem);
853     	}
854     	chan->rx_status = 1;
855     	wake_up_interruptible(&chan->rxwaitq);
856     	return 1;
857     }
858     
859     
860     static ssize_t cosa_write(struct file *file,
861     	const char *buf, size_t count, loff_t *ppos)
862     {
863     	DECLARE_WAITQUEUE(wait, current);
864     	struct channel_data *chan = (struct channel_data *)file->private_data;
865     	struct cosa_data *cosa = chan->cosa;
866     	unsigned int flags;
867     	char *kbuf;
868     
869     	if (!(cosa->firmware_status & COSA_FW_START)) {
870     		printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
871     			cosa->name, cosa->firmware_status);
872     		return -EPERM;
873     	}
874     	if (down_interruptible(&chan->wsem))
875     		return -ERESTARTSYS;
876     
877     	if (count > COSA_MTU)
878     		count = COSA_MTU;
879     	
880     	/* Allocate the buffer */
881     	if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
882     		printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
883     			cosa->name);
884     		up(&chan->wsem);
885     		return -ENOMEM;
886     	}
887     	if (copy_from_user(kbuf, buf, count)) {
888     		up(&chan->wsem);
889     		kfree(kbuf);
890     		return -EFAULT;
891     	}
892     	chan->tx_status=0;
893     	cosa_start_tx(chan, kbuf, count);
894     
895     	spin_lock_irqsave(&cosa->lock, flags);
896     	add_wait_queue(&chan->txwaitq, &wait);
897     	while(!chan->tx_status) {
898     		current->state = TASK_INTERRUPTIBLE;
899     		spin_unlock_irqrestore(&cosa->lock, flags);
900     		schedule();
901     		spin_lock_irqsave(&cosa->lock, flags);
902     		if (signal_pending(current) && chan->tx_status == 0) {
903     			chan->tx_status = 1;
904     			remove_wait_queue(&chan->txwaitq, &wait);
905     			current->state = TASK_RUNNING;
906     			chan->tx_status = 1;
907     			spin_unlock_irqrestore(&cosa->lock, flags);
908     			return -ERESTARTSYS;
909     		}
910     	}
911     	remove_wait_queue(&chan->txwaitq, &wait);
912     	current->state = TASK_RUNNING;
913     	up(&chan->wsem);
914     	spin_unlock_irqrestore(&cosa->lock, flags);
915     	kfree(kbuf);
916     	return count;
917     }
918     
919     static int chrdev_tx_done(struct channel_data *chan, int size)
920     {
921     	if (chan->tx_status) { /* Writer was interrupted */
922     		kfree(chan->txbuf);
923     		up(&chan->wsem);
924     	}
925     	chan->tx_status = 1;
926     	wake_up_interruptible(&chan->txwaitq);
927     	return 1;
928     }
929     
930     static unsigned int cosa_poll(struct file *file, poll_table *poll)
931     {
932     	printk(KERN_INFO "cosa_poll is here\n");
933     	return 0;
934     }
935     
936     static int cosa_open(struct inode *inode, struct file *file)
937     {
938     	struct cosa_data *cosa;
939     	struct channel_data *chan;
940     	unsigned long flags;
941     	int n;
942     
943     	if ((n=MINOR(file->f_dentry->d_inode->i_rdev)>>CARD_MINOR_BITS)
944     		>= nr_cards)
945     		return -ENODEV;
946     	cosa = cosa_cards+n;
947     
948     	if ((n=MINOR(file->f_dentry->d_inode->i_rdev)
949     		& ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
950     		return -ENODEV;
951     	chan = cosa->chan + n;
952     	
953     	file->private_data = chan;
954     
955     	spin_lock_irqsave(&cosa->lock, flags);
956     
957     	if (chan->usage < 0) { /* in netdev mode */
958     		spin_unlock_irqrestore(&cosa->lock, flags);
959     		return -EBUSY;
960     	}
961     	cosa->usage++;
962     	chan->usage++;
963     
964     	chan->tx_done = chrdev_tx_done;
965     	chan->setup_rx = chrdev_setup_rx;
966     	chan->rx_done = chrdev_rx_done;
967     	spin_unlock_irqrestore(&cosa->lock, flags);
968     	return 0;
969     }
970     
971     static int cosa_release(struct inode *inode, struct file *file)
972     {
973     	struct channel_data *channel = (struct channel_data *)file->private_data;
974     	struct cosa_data *cosa;
975     	unsigned long flags;
976     
977     	lock_kernel();
978     	cosa = channel->cosa;
979     	spin_lock_irqsave(&cosa->lock, flags);
980     	cosa->usage--;
981     	channel->usage--;
982     	spin_unlock_irqrestore(&cosa->lock, flags);
983     	unlock_kernel();
984     	return 0;
985     }
986     
987     #ifdef COSA_FASYNC_WORKING
988     static struct fasync_struct *fasync[256] = { NULL, };
989     
990     /* To be done ... */
991     static int cosa_fasync(struct inode *inode, struct file *file, int on)
992     {
993             int port = MINOR(inode->i_rdev);
994             int rv = fasync_helper(inode, file, on, &fasync[port]);
995             return rv < 0 ? rv : 0;
996     }
997     #endif
998     
999     
1000     /* ---------- Ioctls ---------- */
1001     
1002     /*
1003      * Ioctl subroutines can safely be made inline, because they are called
1004      * only from cosa_ioctl().
1005      */
1006     static inline int cosa_reset(struct cosa_data *cosa)
1007     {
1008     	char idstring[COSA_MAX_ID_STRING];
1009     	if (cosa->usage > 1)
1010     		printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1011     			cosa->num, cosa->usage);
1012     	cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1013     	if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1014     		printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1015     		return -EIO;
1016     	}
1017     	printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1018     		idstring);
1019     	cosa->firmware_status |= COSA_FW_RESET;
1020     	return 0;
1021     }
1022     
1023     /* High-level function to download data into COSA memory. Calls download() */
1024     static inline int cosa_download(struct cosa_data *cosa, struct cosa_download *d)
1025     {
1026     	int i;
1027     	int addr, len;
1028     	char *code;
1029     
1030     	if (cosa->usage > 1)
1031     		printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1032     			cosa->name, cosa->usage);
1033     	if (!(cosa->firmware_status & COSA_FW_RESET)) {
1034     		printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1035     			cosa->name, cosa->firmware_status);
1036     		return -EPERM;
1037     	}
1038     
1039     	if (get_user(addr, &(d->addr)) ||
1040     	    __get_user(len, &(d->len)) ||
1041     	    __get_user(code, &(d->code)))
1042     		return -EFAULT;
1043     
1044     	if (addr < 0 || addr > COSA_MAX_FIRMWARE_SIZE)
1045     		return -EINVAL;
1046     	if (len < 0 || len > COSA_MAX_FIRMWARE_SIZE)
1047     		return -EINVAL;
1048     
1049     	/* If something fails, force the user to reset the card */
1050     	cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1051     
1052     	if ((i=download(cosa, code, len, addr)) < 0) {
1053     		printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1054     			cosa->num, i);
1055     		return -EIO;
1056     	}
1057     	printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1058     		cosa->num, len, addr);
1059     	cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1060     	return 0;
1061     }
1062     
1063     /* High-level function to read COSA memory. Calls readmem() */
1064     static inline int cosa_readmem(struct cosa_data *cosa, struct cosa_download *d)
1065     {
1066     	int i;
1067     	int addr, len;
1068     	char *code;
1069     
1070     	if (cosa->usage > 1)
1071     		printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1072     			"cosa->usage > 1 (%d). Odd things may happen.\n",
1073     			cosa->num, cosa->usage);
1074     	if (!(cosa->firmware_status & COSA_FW_RESET)) {
1075     		printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1076     			cosa->name, cosa->firmware_status);
1077     		return -EPERM;
1078     	}
1079     
1080     	if (get_user(addr, &(d->addr)) ||
1081     	    __get_user(len, &(d->len)) ||
1082     	    __get_user(code, &(d->code)))
1083     		return -EFAULT;
1084     
1085     	/* If something fails, force the user to reset the card */
1086     	cosa->firmware_status &= ~COSA_FW_RESET;
1087     
1088     	if ((i=readmem(cosa, d->code, len, addr)) < 0) {
1089     		printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1090     			cosa->num, i);
1091     		return -EIO;
1092     	}
1093     	printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1094     		cosa->num, len, addr);
1095     	cosa->firmware_status |= COSA_FW_RESET;
1096     	return 0;
1097     }
1098     
1099     /* High-level function to start microcode. Calls startmicrocode(). */
1100     static inline int cosa_start(struct cosa_data *cosa, int address)
1101     {
1102     	int i;
1103     
1104     	if (cosa->usage > 1)
1105     		printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1106     			cosa->num, cosa->usage);
1107     
1108     	if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1109     		!= (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1110     		printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1111     			cosa->name, cosa->firmware_status);
1112     		return -EPERM;
1113     	}
1114     	cosa->firmware_status &= ~COSA_FW_RESET;
1115     	if ((i=startmicrocode(cosa, address)) < 0) {
1116     		printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1117     			cosa->num, address, i);
1118     		return -EIO;
1119     	}
1120     	printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1121     		cosa->num, address);
1122     	cosa->startaddr = address;
1123     	cosa->firmware_status |= COSA_FW_START;
1124     	return 0;
1125     }
1126     		
1127     /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1128     static inline int cosa_getidstr(struct cosa_data *cosa, char *string)
1129     {
1130     	int l = strlen(cosa->id_string)+1;
1131     	if (copy_to_user(string, cosa->id_string, l))
1132     		return -EFAULT;
1133     	return l;
1134     }
1135     
1136     /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1137     static inline int cosa_gettype(struct cosa_data *cosa, char *string)
1138     {
1139     	int l = strlen(cosa->type)+1;
1140     	if (copy_to_user(string, cosa->type, l))
1141     		return -EFAULT;
1142     	return l;
1143     }
1144     
1145     static int cosa_ioctl_common(struct cosa_data *cosa,
1146     	struct channel_data *channel, unsigned int cmd, unsigned long arg)
1147     {
1148     	switch(cmd) {
1149     	case COSAIORSET:	/* Reset the device */
1150     		if (!capable(CAP_NET_ADMIN))
1151     			return -EACCES;
1152     		return cosa_reset(cosa);
1153     	case COSAIOSTRT:	/* Start the firmware */
1154     		if (!capable(CAP_SYS_RAWIO))
1155     			return -EACCES;
1156     		return cosa_start(cosa, arg);
1157     	case COSAIODOWNLD:	/* Download the firmware */
1158     		if (!capable(CAP_SYS_RAWIO))
1159     			return -EACCES;
1160     		return cosa_download(cosa, (struct cosa_download *)arg);
1161     	case COSAIORMEM:
1162     		if (!capable(CAP_SYS_RAWIO))
1163     			return -EACCES;
1164     		return cosa_readmem(cosa, (struct cosa_download *)arg);
1165     	case COSAIORTYPE:
1166     		return cosa_gettype(cosa, (char *)arg);
1167     	case COSAIORIDSTR:
1168     		return cosa_getidstr(cosa, (char *)arg);
1169     /*
1170      * These two are _very_ugly_hack_(tm). Don't even look at this.
1171      * Implementing this saved me few reboots after some process segfaulted
1172      * inside this module.
1173      */
1174     #ifdef MODULE
1175     #if 0
1176     	case COSAIOMINC:
1177     		MOD_INC_USE_COUNT;
1178     		return 0;
1179     	case COSAIOMDEC:
1180     		MOD_DEC_USE_COUNT;
1181     		return 0;
1182     #endif
1183     #endif
1184     	case COSAIONRCARDS:
1185     		return nr_cards;
1186     	case COSAIONRCHANS:
1187     		return cosa->nchannels;
1188     	case COSAIOBMSET:
1189     		if (!capable(CAP_SYS_RAWIO))
1190     			return -EACCES;
1191     		if (is_8bit(cosa))
1192     			return -EINVAL;
1193     		if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1194     			return -EINVAL;
1195     		cosa->busmaster = arg;
1196     		return 0;
1197     	case COSAIOBMGET:
1198     		return cosa->busmaster;
1199     	}
1200     	return -ENOIOCTLCMD;
1201     }
1202     
1203     static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
1204     	int cmd)
1205     {
1206     	int rv;
1207     	struct channel_data *chan = (struct channel_data *)dev->priv;
1208     	rv = cosa_ioctl_common(chan->cosa, chan, cmd, (unsigned long)ifr->ifr_data);
1209     	if (rv == -ENOIOCTLCMD) {
1210     		return sppp_do_ioctl(dev, ifr, cmd);
1211     	}
1212     	return rv;
1213     }
1214     
1215     static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1216     	unsigned int cmd, unsigned long arg)
1217     {
1218     	struct channel_data *channel = (struct channel_data *)file->private_data;
1219     	struct cosa_data *cosa = channel->cosa;
1220     	return cosa_ioctl_common(cosa, channel, cmd, arg);
1221     }
1222     
1223     
1224     /*---------- HW layer interface ---------- */
1225     
1226     /*
1227      * The higher layer can bind itself to the HW layer by setting the callbacks
1228      * in the channel_data structure and by using these routines.
1229      */
1230     static void cosa_enable_rx(struct channel_data *chan)
1231     {
1232     	struct cosa_data *cosa = chan->cosa;
1233     
1234     	if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1235     		put_driver_status(cosa);
1236     }
1237     
1238     static void cosa_disable_rx(struct channel_data *chan)
1239     {
1240     	struct cosa_data *cosa = chan->cosa;
1241     
1242     	if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1243     		put_driver_status(cosa);
1244     }
1245     
1246     /*
1247      * FIXME: This routine probably should check for cosa_start_tx() called when
1248      * the previous transmit is still unfinished. In this case the non-zero
1249      * return value should indicate to the caller that the queuing(sp?) up
1250      * the transmit has failed.
1251      */
1252     static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1253     {
1254     	struct cosa_data *cosa = chan->cosa;
1255     	int flags;
1256     #ifdef DEBUG_DATA
1257     	int i;
1258     
1259     	printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1260     		chan->num, len);
1261     	for (i=0; i<len; i++)
1262     		printk(" %02x", buf[i]&0xff);
1263     	printk("\n");
1264     #endif
1265     	spin_lock_irqsave(&cosa->lock, flags);
1266     	chan->txbuf = buf;
1267     	chan->txsize = len;
1268     	if (len > COSA_MTU)
1269     		chan->txsize = COSA_MTU;
1270     	spin_unlock_irqrestore(&cosa->lock, flags);
1271     
1272     	/* Tell the firmware we are ready */
1273     	set_bit(chan->num, &cosa->txbitmap);
1274     	put_driver_status(cosa);
1275     
1276     	return 0;
1277     }
1278     
1279     static void put_driver_status(struct cosa_data *cosa)
1280     {
1281     	unsigned flags=0;
1282     	int status;
1283     
1284     	spin_lock_irqsave(&cosa->lock, flags);
1285     
1286     	status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1287     		| (cosa->txbitmap ? DRIVER_TX_READY : 0)
1288     		| (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1289     			&DRIVER_TXMAP_MASK : 0);
1290     	if (!cosa->rxtx) {
1291     		if (cosa->rxbitmap|cosa->txbitmap) {
1292     			if (!cosa->enabled) {
1293     				cosa_putstatus(cosa, SR_RX_INT_ENA);
1294     #ifdef DEBUG_IO
1295     				debug_status_out(cosa, SR_RX_INT_ENA);
1296     #endif
1297     				cosa->enabled = 1;
1298     			}
1299     		} else if (cosa->enabled) {
1300     			cosa->enabled = 0;
1301     			cosa_putstatus(cosa, 0);
1302     #ifdef DEBUG_IO
1303     			debug_status_out(cosa, 0);
1304     #endif
1305     		}
1306     		cosa_putdata8(cosa, status);
1307     #ifdef DEBUG_IO
1308     		debug_data_cmd(cosa, status);
1309     #endif
1310     	}
1311     	spin_unlock_irqrestore(&cosa->lock, flags);
1312     }
1313     
1314     static void put_driver_status_nolock(struct cosa_data *cosa)
1315     {
1316     	int status;
1317     
1318     	status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1319     		| (cosa->txbitmap ? DRIVER_TX_READY : 0)
1320     		| (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1321     			&DRIVER_TXMAP_MASK : 0);
1322     
1323     	if (cosa->rxbitmap|cosa->txbitmap) {
1324     		cosa_putstatus(cosa, SR_RX_INT_ENA);
1325     #ifdef DEBUG_IO
1326     		debug_status_out(cosa, SR_RX_INT_ENA);
1327     #endif
1328     		cosa->enabled = 1;
1329     	} else {
1330     		cosa_putstatus(cosa, 0);
1331     #ifdef DEBUG_IO
1332     		debug_status_out(cosa, 0);
1333     #endif
1334     		cosa->enabled = 0;
1335     	}
1336     	cosa_putdata8(cosa, status);
1337     #ifdef DEBUG_IO
1338     	debug_data_cmd(cosa, status);
1339     #endif
1340     }
1341     
1342     /*
1343      * The "kickme" function: When the DMA times out, this is called to
1344      * clean up the driver status.
1345      * FIXME: Preliminary support, the interface is probably wrong.
1346      */
1347     static void cosa_kick(struct cosa_data *cosa)
1348     {
1349     	unsigned flags, flags1;
1350     	char *s = "(probably) IRQ";
1351     
1352     	if (test_bit(RXBIT, &cosa->rxtx))
1353     		s = "RX DMA";
1354     	if (test_bit(TXBIT, &cosa->rxtx))
1355     		s = "TX DMA";
1356     
1357     	printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s); 
1358     	spin_lock_irqsave(&cosa->lock, flags);
1359     	cosa->rxtx = 0;
1360     
1361     	flags1 = claim_dma_lock();
1362     	disable_dma(cosa->dma);
1363     	clear_dma_ff(cosa->dma);
1364     	release_dma_lock(flags1);
1365     
1366     	/* FIXME: Anything else? */
1367     	udelay(100);
1368     	cosa_putstatus(cosa, 0);
1369     	udelay(100);
1370     	(void) cosa_getdata8(cosa);
1371     	udelay(100);
1372     	cosa_putdata8(cosa, 0);
1373     	udelay(100);
1374     	put_driver_status_nolock(cosa);
1375     	spin_unlock_irqrestore(&cosa->lock, flags);
1376     }
1377     
1378     /*
1379      * Check if the whole buffer is DMA-able. It means it is below the 16M of
1380      * physical memory and doesn't span the 64k boundary. For now it seems
1381      * SKB's never do this, but we'll check this anyway.
1382      */
1383     static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1384     {
1385     	static int count;
1386     	unsigned long b = (unsigned long)buf;
1387     	if (b+len >= MAX_DMA_ADDRESS)
1388     		return 0;
1389     	if ((b^ (b+len)) & 0x10000) {
1390     		if (count++ < 5)
1391     			printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1392     				chan->name);
1393     		return 0;
1394     	}
1395     	return 1;
1396     }
1397     
1398     
1399     /* ---------- The SRP/COSA ROM monitor functions ---------- */
1400     
1401     /*
1402      * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1403      * drivers need to say 4-digit hex number meaning start address of the microcode
1404      * separated by a single space. Monitor replies by saying " =". Now driver
1405      * has to write 4-digit hex number meaning the last byte address ended
1406      * by a single space. Monitor has to reply with a space. Now the download
1407      * begins. After the download monitor replies with "\r\n." (CR LF dot).
1408      */
1409     static int download(struct cosa_data *cosa, char *microcode, int length, int address)
1410     {
1411     	int i;
1412     
1413     	if (put_wait_data(cosa, 'w') == -1) return -1;
1414     	if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1415     	if (get_wait_data(cosa) != '=') return -3;
1416     
1417     	if (puthexnumber(cosa, address) < 0) return -4;
1418     	if (put_wait_data(cosa, ' ') == -1) return -10;
1419     	if (get_wait_data(cosa) != ' ') return -11;
1420     	if (get_wait_data(cosa) != '=') return -12;
1421     
1422     	if (puthexnumber(cosa, address+length-1) < 0) return -13;
1423     	if (put_wait_data(cosa, ' ') == -1) return -18;
1424     	if (get_wait_data(cosa) != ' ') return -19;
1425     
1426     	while (length--) {
1427     		char c;
1428     #ifndef SRP_DOWNLOAD_AT_BOOT
1429     		if (get_user(c, microcode))
1430     			return -23; /* ??? */
1431     #else
1432     		c = *microcode;
1433     #endif
1434     		if (put_wait_data(cosa, c) == -1)
1435     			return -20;
1436     		microcode++;
1437     	}
1438     
1439     	if (get_wait_data(cosa) != '\r') return -21;
1440     	if (get_wait_data(cosa) != '\n') return -22;
1441     	if (get_wait_data(cosa) != '.') return -23;
1442     #if 0
1443     	printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1444     #endif
1445     	return 0;
1446     }
1447     
1448     
1449     /*
1450      * Starting microcode is done via the "g" command of the SRP monitor.
1451      * The chat should be the following: "g" "g=" "<addr><CR>"
1452      * "<CR><CR><LF><CR><LF>".
1453      */
1454     static int startmicrocode(struct cosa_data *cosa, int address)
1455     {
1456     	if (put_wait_data(cosa, 'g') == -1) return -1;
1457     	if (get_wait_data(cosa) != 'g') return -2;
1458     	if (get_wait_data(cosa) != '=') return -3;
1459     
1460     	if (puthexnumber(cosa, address) < 0) return -4;
1461     	if (put_wait_data(cosa, '\r') == -1) return -5;
1462     	
1463     	if (get_wait_data(cosa) != '\r') return -6;
1464     	if (get_wait_data(cosa) != '\r') return -7;
1465     	if (get_wait_data(cosa) != '\n') return -8;
1466     	if (get_wait_data(cosa) != '\r') return -9;
1467     	if (get_wait_data(cosa) != '\n') return -10;
1468     #if 0
1469     	printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1470     #endif
1471     	return 0;
1472     }
1473     
1474     /*
1475      * Reading memory is done via the "r" command of the SRP monitor.
1476      * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1477      * Then driver can read the data and the conversation is finished
1478      * by SRP monitor sending "<CR><LF>." (dot at the end).
1479      *
1480      * This routine is not needed during the normal operation and serves
1481      * for debugging purposes only.
1482      */
1483     static int readmem(struct cosa_data *cosa, char *microcode, int length, int address)
1484     {
1485     	if (put_wait_data(cosa, 'r') == -1) return -1;
1486     	if ((get_wait_data(cosa)) != 'r') return -2;
1487     	if ((get_wait_data(cosa)) != '=') return -3;
1488     
1489     	if (puthexnumber(cosa, address) < 0) return -4;
1490     	if (put_wait_data(cosa, ' ') == -1) return -5;
1491     	if (get_wait_data(cosa) != ' ') return -6;
1492     	if (get_wait_data(cosa) != '=') return -7;
1493     
1494     	if (puthexnumber(cosa, address+length-1) < 0) return -8;
1495     	if (put_wait_data(cosa, ' ') == -1) return -9;
1496     	if (get_wait_data(cosa) != ' ') return -10;
1497     
1498     	while (length--) {
1499     		char c;
1500     		int i;
1501     		if ((i=get_wait_data(cosa)) == -1) {
1502     			printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1503     				length);
1504     			return -11;
1505     		}
1506     		c=i;
1507     #if 1
1508     		if (put_user(c, microcode))
1509     			return -23; /* ??? */
1510     #else
1511     		*microcode = c;
1512     #endif
1513     		microcode++;
1514     	}
1515     
1516     	if (get_wait_data(cosa) != '\r') return -21;
1517     	if (get_wait_data(cosa) != '\n') return -22;
1518     	if (get_wait_data(cosa) != '.') return -23;
1519     #if 0
1520     	printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1521     #endif
1522     	return 0;
1523     }
1524     
1525     /*
1526      * This function resets the device and reads the initial prompt
1527      * of the device's ROM monitor.
1528      */
1529     static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1530     {
1531     	int i=0, id=0, prev=0, curr=0;
1532     
1533     	/* Reset the card ... */
1534     	cosa_putstatus(cosa, 0);
1535     	cosa_getdata8(cosa);
1536     	cosa_putstatus(cosa, SR_RST);
1537     #ifdef MODULE
1538     	current->state = TASK_INTERRUPTIBLE;
1539     	schedule_timeout(HZ/2);
1540     	current->state = TASK_RUNNING;
1541     #else
1542     	udelay(5*100000);
1543     #endif
1544     	/* Disable all IRQs from the card */
1545     	cosa_putstatus(cosa, 0);
1546     
1547     	/*
1548     	 * Try to read the ID string. The card then prints out the
1549     	 * identification string ended by the "\n\x2e".
1550     	 *
1551     	 * The following loop is indexed through i (instead of id)
1552     	 * to avoid looping forever when for any reason
1553     	 * the port returns '\r', '\n' or '\x2e' permanently.
1554     	 */
1555     	for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1556     		if ((curr = get_wait_data(cosa)) == -1) {
1557     			return -1;
1558     		}
1559     		curr &= 0xff;
1560     		if (curr != '\r' && curr != '\n' && curr != 0x2e)
1561     			idstring[id++] = curr;
1562     		if (curr == 0x2e && prev == '\n')
1563     			break;
1564     	}
1565     	/* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1566     	idstring[id] = '\0';
1567     	return id;
1568     }
1569     
1570     
1571     /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1572     
1573     /*
1574      * This routine gets the data byte from the card waiting for the SR_RX_RDY
1575      * bit to be set in a loop. It should be used in the exceptional cases
1576      * only (for example when resetting the card or downloading the firmware.
1577      */
1578     static int get_wait_data(struct cosa_data *cosa)
1579     {
1580     	int retries = 1000;
1581     
1582     	while (--retries) {
1583     		/* read data and return them */
1584     		if (cosa_getstatus(cosa) & SR_RX_RDY) {
1585     			short r;
1586     			r = cosa_getdata8(cosa);
1587     #if 0
1588     			printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1589     #endif
1590     			return r;
1591     		}
1592     		/* sleep if not ready to read */
1593     		current->state = TASK_INTERRUPTIBLE;
1594     		schedule_timeout(1);
1595     	}
1596     	printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1597     		cosa_getstatus(cosa));
1598     	return -1;
1599     }
1600     
1601     /*
1602      * This routine puts the data byte to the card waiting for the SR_TX_RDY
1603      * bit to be set in a loop. It should be used in the exceptional cases
1604      * only (for example when resetting the card or downloading the firmware).
1605      */
1606     static int put_wait_data(struct cosa_data *cosa, int data)
1607     {
1608     	int retries = 1000;
1609     	while (--retries) {
1610     		/* read data and return them */
1611     		if (cosa_getstatus(cosa) & SR_TX_RDY) {
1612     			cosa_putdata8(cosa, data);
1613     #if 0
1614     			printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1615     #endif
1616     			return 0;
1617     		}
1618     #if 0
1619     		/* sleep if not ready to read */
1620     		current->state = TASK_INTERRUPTIBLE;
1621     		schedule_timeout(1);
1622     #endif
1623     	}
1624     	printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1625     		cosa->num, cosa_getstatus(cosa));
1626     	return -1;
1627     }
1628     	
1629     /* 
1630      * The following routine puts the hexadecimal number into the SRP monitor
1631      * and verifies the proper echo of the sent bytes. Returns 0 on success,
1632      * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1633      * (-2,-4,-6,-8) means that reading echo failed.
1634      */
1635     static int puthexnumber(struct cosa_data *cosa, int number)
1636     {
1637     	char temp[5];
1638     	int i;
1639     
1640     	/* Well, I should probably replace this by something faster. */
1641     	sprintf(temp, "%04X", number);
1642     	for (i=0; i<4; i++) {
1643     		if (put_wait_data(cosa, temp[i]) == -1) {
1644     			printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1645     				cosa->num, i);
1646     			return -1-2*i;
1647     		}
1648     		if (get_wait_data(cosa) != temp[i]) {
1649     			printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1650     				cosa->num, i);
1651     			return -2-2*i;
1652     		}
1653     	}
1654     	return 0;
1655     }
1656     
1657     
1658     /* ---------- Interrupt routines ---------- */
1659     
1660     /*
1661      * There are three types of interrupt:
1662      * At the beginning of transmit - this handled is in tx_interrupt(),
1663      * at the beginning of receive - it is in rx_interrupt() and
1664      * at the end of transmit/receive - it is the eot_interrupt() function.
1665      * These functions are multiplexed by cosa_interrupt() according to the
1666      * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1667      * separate functions to make it more readable. These functions are inline,
1668      * so there should be no overhead of function call.
1669      * 
1670      * In the COSA bus-master mode, we need to tell the card the address of a
1671      * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1672      * It's time to use the bottom half :-(
1673      */
1674     
1675     /*
1676      * Transmit interrupt routine - called when COSA is willing to obtain
1677      * data from the OS. The most tricky part of the routine is selection
1678      * of channel we (OS) want to send packet for. For SRP we should probably
1679      * use the round-robin approach. The newer COSA firmwares have a simple
1680      * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1681      * channel 0 or 1 doesn't want to receive data.
1682      *
1683      * It seems there is a bug in COSA firmware (need to trace it further):
1684      * When the driver status says that the kernel has no more data for transmit
1685      * (e.g. at the end of TX DMA) and then the kernel changes its mind
1686      * (e.g. new packet is queued to hard_start_xmit()), the card issues
1687      * the TX interrupt but does not mark the channel as ready-to-transmit.
1688      * The fix seems to be to push the packet to COSA despite its request.
1689      * We first try to obey the card's opinion, and then fall back to forced TX.
1690      */
1691     static inline void tx_interrupt(struct cosa_data *cosa, int status)
1692     {
1693     	unsigned long flags, flags1;
1694     #ifdef DEBUG_IRQS
1695     	printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1696     		cosa->num, status);
1697     #endif
1698     	spin_lock_irqsave(&cosa->lock, flags);
1699     	set_bit(TXBIT, &cosa->rxtx);
1700     	if (!test_bit(IRQBIT, &cosa->rxtx)) {
1701     		/* flow control, see the comment above */
1702     		int i=0;
1703     		if (!cosa->txbitmap) {
1704     			printk(KERN_WARNING "%s: No channel wants data "
1705     				"in TX IRQ. Expect DMA timeout.",
1706     				cosa->name);
1707     			put_driver_status_nolock(cosa);
1708     			clear_bit(TXBIT, &cosa->rxtx);
1709     			spin_unlock_irqrestore(&cosa->lock, flags);
1710     			return;
1711     		}
1712     		while(1) {
1713     			cosa->txchan++;
1714     			i++;
1715     			if (cosa->txchan >= cosa->nchannels)
1716     				cosa->txchan = 0;
1717     			if (!(cosa->txbitmap & (1<<cosa->txchan)))
1718     				continue;
1719     			if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1720     				break;
1721     			/* in second pass, accept first ready-to-TX channel */
1722     			if (i > cosa->nchannels) {
1723     				/* Can be safely ignored */
1724     #ifdef DEBUG_IRQS
1725     				printk(KERN_DEBUG "%s: Forcing TX "
1726     					"to not-ready channel %d\n",
1727     					cosa->name, cosa->txchan);
1728     #endif
1729     				break;
1730     			}
1731     		}
1732     
1733     		cosa->txsize = cosa->chan[cosa->txchan].txsize;
1734     		if (cosa_dma_able(cosa->chan+cosa->txchan,
1735     			cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1736     			cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1737     		} else {
1738     			memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1739     				cosa->txsize);
1740     			cosa->txbuf = cosa->bouncebuf;
1741     		}
1742     	}
1743     
1744     	if (is_8bit(cosa)) {
1745     		if (!test_bit(IRQBIT, &cosa->rxtx)) {
1746     			cosa_putstatus(cosa, SR_TX_INT_ENA);
1747     			cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1748     				((cosa->txsize >> 8) & 0x1f));
1749     #ifdef DEBUG_IO
1750     			debug_status_out(cosa, SR_TX_INT_ENA);
1751     			debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1752                                     ((cosa->txsize >> 8) & 0x1f));
1753     			debug_data_in(cosa, cosa_getdata8(cosa));
1754     #else
1755     			cosa_getdata8(cosa);
1756     #endif
1757     			set_bit(IRQBIT, &cosa->rxtx);
1758     			spin_unlock_irqrestore(&cosa->lock, flags);
1759     			return;
1760     		} else {
1761     			clear_bit(IRQBIT, &cosa->rxtx);
1762     			cosa_putstatus(cosa, 0);
1763     			cosa_putdata8(cosa, cosa->txsize&0xff);
1764     #ifdef DEBUG_IO
1765     			debug_status_out(cosa, 0);
1766     			debug_data_out(cosa, cosa->txsize&0xff);
1767     #endif
1768     		}
1769     	} else {
1770     		cosa_putstatus(cosa, SR_TX_INT_ENA);
1771     		cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1772     			| (cosa->txsize & 0x1fff));
1773     #ifdef DEBUG_IO
1774     		debug_status_out(cosa, SR_TX_INT_ENA);
1775     		debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1776                             | (cosa->txsize & 0x1fff));
1777     		debug_data_in(cosa, cosa_getdata8(cosa));
1778     		debug_status_out(cosa, 0);
1779     #else
1780     		cosa_getdata8(cosa);
1781     #endif
1782     		cosa_putstatus(cosa, 0);
1783     	}
1784     
1785     	if (cosa->busmaster) {
1786     		unsigned long addr = virt_to_bus(cosa->txbuf);
1787     		int count=0;
1788     		printk(KERN_INFO "busmaster IRQ\n");
1789     		while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1790     			count++;
1791     			udelay(10);
1792     			if (count > 1000) break;
1793     		}
1794     		printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1795     		printk(KERN_INFO "ready after %d loops\n", count);
1796     		cosa_putdata16(cosa, (addr >> 16)&0xffff);
1797     
1798     		count = 0;
1799     		while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1800     			count++;
1801     			if (count > 1000) break;
1802     			udelay(10);
1803     		}
1804     		printk(KERN_INFO "ready after %d loops\n", count);
1805     		cosa_putdata16(cosa, addr &0xffff);
1806     		flags1 = claim_dma_lock();
1807     		set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1808     		enable_dma(cosa->dma);
1809     		release_dma_lock(flags1);
1810     	} else {
1811     		/* start the DMA */
1812     		flags1 = claim_dma_lock();
1813     		disable_dma(cosa->dma);
1814     		clear_dma_ff(cosa->dma);
1815     		set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1816     		set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1817     		set_dma_count(cosa->dma, cosa->txsize);
1818     		enable_dma(cosa->dma);
1819     		release_dma_lock(flags1);
1820     	}
1821     	cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1822     #ifdef DEBUG_IO
1823     	debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1824     #endif
1825     	spin_unlock_irqrestore(&cosa->lock, flags);
1826     }
1827     
1828     static inline void rx_interrupt(struct cosa_data *cosa, int status)
1829     {
1830     	unsigned long flags;
1831     #ifdef DEBUG_IRQS
1832     	printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1833     #endif
1834     
1835     	spin_lock_irqsave(&cosa->lock, flags);
1836     	set_bit(RXBIT, &cosa->rxtx);
1837     
1838     	if (is_8bit(cosa)) {
1839     		if (!test_bit(IRQBIT, &cosa->rxtx)) {
1840     			set_bit(IRQBIT, &cosa->rxtx);
1841     			put_driver_status_nolock(cosa);
1842     			cosa->rxsize = cosa_getdata8(cosa) <<8;
1843     #ifdef DEBUG_IO
1844     			debug_data_in(cosa, cosa->rxsize >> 8);
1845     #endif
1846     			spin_unlock_irqrestore(&cosa->lock, flags);
1847     			return;
1848     		} else {
1849     			clear_bit(IRQBIT, &cosa->rxtx);
1850     			cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1851     #ifdef DEBUG_IO
1852     			debug_data_in(cosa, cosa->rxsize & 0xff);
1853     #endif
1854     #if 0
1855     			printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1856     				cosa->num, cosa->rxsize);
1857     #endif
1858     		}
1859     	} else {
1860     		cosa->rxsize = cosa_getdata16(cosa);
1861     #ifdef DEBUG_IO
1862     		debug_data_in(cosa, cosa->rxsize);
1863     #endif
1864     #if 0
1865     		printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1866     			cosa->num, cosa->rxsize);
1867     #endif
1868     	}
1869     	if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1870     		printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1871     			cosa->name, cosa->rxsize);
1872     		spin_unlock_irqrestore(&cosa->lock, flags);
1873     		goto reject;
1874     	}
1875     	cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1876     	cosa->rxsize &= 0x1fff;
1877     	spin_unlock_irqrestore(&cosa->lock, flags);
1878     
1879     	cosa->rxbuf = NULL;
1880     	if (cosa->rxchan->setup_rx)
1881     		cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1882     
1883     	if (!cosa->rxbuf) {
1884     reject:		/* Reject the packet */
1885     		printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1886     			cosa->num, cosa->rxchan->num);
1887     		cosa->rxbuf = cosa->bouncebuf;
1888     	}
1889     
1890     	/* start the DMA */
1891     	flags = claim_dma_lock();
1892     	disable_dma(cosa->dma);
1893     	clear_dma_ff(cosa->dma);
1894     	set_dma_mode(cosa->dma, DMA_MODE_READ);
1895     	if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1896     		set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1897     	} else {
1898     		set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1899     	}
1900     	set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1901     	enable_dma(cosa->dma);
1902     	release_dma_lock(flags);
1903     	spin_lock_irqsave(&cosa->lock, flags);
1904     	cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1905     	if (!is_8bit(cosa) && (status & SR_TX_RDY))
1906     		cosa_putdata8(cosa, DRIVER_RX_READY);
1907     #ifdef DEBUG_IO
1908     	debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1909     	if (!is_8bit(cosa) && (status & SR_TX_RDY))
1910     		debug_data_cmd(cosa, DRIVER_RX_READY);
1911     #endif
1912     	spin_unlock_irqrestore(&cosa->lock, flags);
1913     }
1914     
1915     static void inline eot_interrupt(struct cosa_data *cosa, int status)
1916     {
1917     	unsigned long flags, flags1;
1918     	spin_lock_irqsave(&cosa->lock, flags);
1919     	flags1 = claim_dma_lock();
1920     	disable_dma(cosa->dma);
1921     	clear_dma_ff(cosa->dma);
1922     	release_dma_lock(flags1);
1923     	if (test_bit(TXBIT, &cosa->rxtx)) {
1924     		struct channel_data *chan = cosa->chan+cosa->txchan;
1925     		if (chan->tx_done)
1926     			if (chan->tx_done(chan, cosa->txsize))
1927     				clear_bit(chan->num, &cosa->txbitmap);
1928     	} else if (test_bit(RXBIT, &cosa->rxtx)) {
1929     #ifdef DEBUG_DATA
1930     	{
1931     		int i;
1932     		printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num, 
1933     			cosa->rxchan->num, cosa->rxsize);
1934     		for (i=0; i<cosa->rxsize; i++)
1935     			printk (" %02x", cosa->rxbuf[i]&0xff);
1936     		printk("\n");
1937     	}
1938     #endif
1939     		/* Packet for unknown channel? */
1940     		if (cosa->rxbuf == cosa->bouncebuf)
1941     			goto out;
1942     		if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1943     			memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1944     		if (cosa->rxchan->rx_done)
1945     			if (cosa->rxchan->rx_done(cosa->rxchan))
1946     				clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1947     	} else {
1948     		printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1949     			cosa->num);
1950     	}
1951     	/*
1952     	 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1953     	 * cleared anyway). We should do it as soon as possible
1954     	 * so that we can tell the COSA we are done and to give it a time
1955     	 * for recovery.
1956     	 */
1957     out:
1958     	cosa->rxtx = 0;
1959     	put_driver_status_nolock(cosa);
1960     	spin_unlock_irqrestore(&cosa->lock, flags);
1961     }
1962     
1963     static void cosa_interrupt(int irq, void *cosa_, struct pt_regs *regs)
1964     {
1965     	unsigned status;
1966     	int count = 0;
1967     	struct cosa_data *cosa = cosa_;
1968     again:
1969     	status = cosa_getstatus(cosa);
1970     #ifdef DEBUG_IRQS
1971     	printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1972     		status & 0xff);
1973     #endif
1974     #ifdef DEBUG_IO
1975     	debug_status_in(cosa, status);
1976     #endif
1977     	switch (status & SR_CMD_FROM_SRP_MASK) {
1978     	case SR_DOWN_REQUEST:
1979     		tx_interrupt(cosa, status);
1980     		break;
1981     	case SR_UP_REQUEST:
1982     		rx_interrupt(cosa, status);
1983     		break;
1984     	case SR_END_OF_TRANSFER:
1985     		eot_interrupt(cosa, status);
1986     		break;
1987     	default:
1988     		/* We may be too fast for SRP. Try to wait a bit more. */
1989     		if (count++ < 100) {
1990     			udelay(100);
1991     			goto again;
1992     		}
1993     		printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1994     			cosa->num, status & 0xff, count);
1995     	}
1996     #ifdef DEBUG_IRQS
1997     	if (count)
1998     		printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1999     			cosa->name, count);
2000     	else
2001     		printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
2002     #endif
2003     }
2004     
2005     
2006     /* ---------- I/O debugging routines ---------- */
2007     /*
2008      * These routines can be used to monitor COSA/SRP I/O and to printk()
2009      * the data being transferred on the data and status I/O port in a
2010      * readable way.
2011      */
2012     
2013     #ifdef DEBUG_IO
2014     static void debug_status_in(struct cosa_data *cosa, int status)
2015     {
2016     	char *s;
2017     	switch(status & SR_CMD_FROM_SRP_MASK) {
2018     	case SR_UP_REQUEST:
2019     		s = "RX_REQ";
2020     		break;
2021     	case SR_DOWN_REQUEST:
2022     		s = "TX_REQ";
2023     		break;
2024     	case SR_END_OF_TRANSFER:
2025     		s = "ET_REQ";
2026     		break;
2027     	default:
2028     		s = "NO_REQ";
2029     		break;
2030     	}
2031     	printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2032     		cosa->name,
2033     		status,
2034     		status & SR_USR_RQ ? "USR_RQ|":"",
2035     		status & SR_TX_RDY ? "TX_RDY|":"",
2036     		status & SR_RX_RDY ? "RX_RDY|":"",
2037     		s);
2038     }
2039     
2040     static void debug_status_out(struct cosa_data *cosa, int status)
2041     {
2042     	printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2043     		cosa->name,
2044     		status,
2045     		status & SR_RX_DMA_ENA  ? "RXDMA|":"!rxdma|",
2046     		status & SR_TX_DMA_ENA  ? "TXDMA|":"!txdma|",
2047     		status & SR_RST         ? "RESET|":"",
2048     		status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2049     		status & SR_TX_INT_ENA  ? "TXINT|":"!txint|",
2050     		status & SR_RX_INT_ENA  ? "RXINT":"!rxint");
2051     }
2052     
2053     static void debug_data_in(struct cosa_data *cosa, int data)
2054     {
2055     	printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2056     }
2057     
2058     static void debug_data_out(struct cosa_data *cosa, int data)
2059     {
2060     	printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2061     }
2062     
2063     static void debug_data_cmd(struct cosa_data *cosa, int data)
2064     {
2065     	printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2066     		cosa->name, data,
2067     		data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2068     		data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2069     }
2070     #endif
2071     
2072     /* EOF -- this file has not been truncated */
2073