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