ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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 \f
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/interrupt.h>
90 #include <linux/delay.h>
91 #include <linux/errno.h>
92 #include <linux/ioport.h>
93 #include <linux/netdevice.h>
94 #include <linux/spinlock.h>
95 #include <linux/smp_lock.h>
96
97 #undef COSA_SLOW_IO     /* for testing purposes only */
98 #undef REALLY_SLOW_IO
99
100 #include <asm/io.h>
101 #include <asm/dma.h>
102 #include <asm/byteorder.h>
103
104 #include <net/syncppp.h>
105 #include "cosa.h"
106
107 /* Maximum length of the identification string. */
108 #define COSA_MAX_ID_STRING      128
109
110 /* Maximum length of the channel name */
111 #define COSA_MAX_NAME           (sizeof("cosaXXXcXXX")+1)
112
113 /* Per-channel data structure */
114
115 struct channel_data {
116         void *if_ptr;   /* General purpose pointer (used by SPPP) */
117         int usage;      /* Usage count; >0 for chrdev, -1 for netdev */
118         int num;        /* Number of the channel */
119         struct cosa_data *cosa; /* Pointer to the per-card structure */
120         int txsize;     /* Size of transmitted data */
121         char *txbuf;    /* Transmit buffer */
122         char name[COSA_MAX_NAME];       /* channel name */
123
124         /* The HW layer interface */
125         /* routine called from the RX interrupt */
126         char *(*setup_rx)(struct channel_data *channel, int size);
127         /* routine called when the RX is done (from the EOT interrupt) */
128         int (*rx_done)(struct channel_data *channel);
129         /* routine called when the TX is done (from the EOT interrupt) */
130         int (*tx_done)(struct channel_data *channel, int size);
131
132         /* Character device parts */
133         struct semaphore rsem, wsem;
134         char *rxdata;
135         int rxsize;
136         wait_queue_head_t txwaitq, rxwaitq;
137         int tx_status, rx_status;
138
139         /* SPPP/HDLC device parts */
140         struct ppp_device pppdev;
141         struct sk_buff *rx_skb, *tx_skb;
142         struct net_device_stats stats;
143 };
144
145 /* cosa->firmware_status bits */
146 #define COSA_FW_RESET           (1<<0)  /* Is the ROM monitor active? */
147 #define COSA_FW_DOWNLOAD        (1<<1)  /* Is the microcode downloaded? */
148 #define COSA_FW_START           (1<<2)  /* Is the microcode running? */
149
150 struct cosa_data {
151         int num;                        /* Card number */
152         char name[COSA_MAX_NAME];       /* Card name - e.g "cosa0" */
153         unsigned int datareg, statusreg;        /* I/O ports */
154         unsigned short irq, dma;        /* IRQ and DMA number */
155         unsigned short startaddr;       /* Firmware start address */
156         unsigned short busmaster;       /* Use busmastering? */
157         int nchannels;                  /* # of channels on this card */
158         int driver_status;              /* For communicating with firware */
159         int firmware_status;            /* Downloaded, reseted, etc. */
160         long int rxbitmap, txbitmap;    /* Bitmap of channels who are willing to send/receive data */
161         long int rxtx;                  /* RX or TX in progress? */
162         int enabled;
163         int usage;                              /* usage count */
164         int txchan, txsize, rxsize;
165         struct channel_data *rxchan;
166         char *bouncebuf;
167         char *txbuf, *rxbuf;
168         struct channel_data *chan;
169         spinlock_t lock;        /* For exclusive operations on this structure */
170         char id_string[COSA_MAX_ID_STRING];     /* ROM monitor ID string */
171         char *type;                             /* card type */
172 };
173
174 /*
175  * Define this if you want all the possible ports to be autoprobed.
176  * It is here but it probably is not a good idea to use this.
177  */
178 /* #define COSA_ISA_AUTOPROBE   1 */
179
180 /*
181  * Character device major number. 117 was allocated for us.
182  * The value of 0 means to allocate a first free one.
183  */
184 static int cosa_major = 117;
185
186 /*
187  * Encoding of the minor numbers:
188  * The lowest CARD_MINOR_BITS bits means the channel on the single card,
189  * the highest bits means the card number.
190  */
191 #define CARD_MINOR_BITS 4       /* How many bits in minor number are reserved
192                                  * for the single card */
193 /*
194  * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
195  * macro doesn't like anything other than the raw number as an argument :-(
196  */
197 #define MAX_CARDS       16
198 /* #define MAX_CARDS    (1 << (8-CARD_MINOR_BITS)) */
199
200 #define DRIVER_RX_READY         0x0001
201 #define DRIVER_TX_READY         0x0002
202 #define DRIVER_TXMAP_SHIFT      2
203 #define DRIVER_TXMAP_MASK       0x0c    /* FIXME: 0xfc for 8-channel version */
204
205 /*
206  * for cosa->rxtx - indicates whether either transmit or receive is
207  * in progress. These values are mean number of the bit.
208  */
209 #define TXBIT 0
210 #define RXBIT 1
211 #define IRQBIT 2
212
213 #define COSA_MTU 2000   /* FIXME: I don't know this exactly */
214
215 #undef DEBUG_DATA //1   /* Dump the data read or written to the channel */
216 #undef DEBUG_IRQS //1   /* Print the message when the IRQ is received */
217 #undef DEBUG_IO   //1   /* Dump the I/O traffic */
218
219 #define TX_TIMEOUT      (5*HZ)
220
221 /* Maybe the following should be allocated dynamically */
222 static struct cosa_data cosa_cards[MAX_CARDS];
223 static int nr_cards;
224
225 #ifdef COSA_ISA_AUTOPROBE
226 static int io[MAX_CARDS+1]  = { 0x220, 0x228, 0x210, 0x218, 0, };
227 /* NOTE: DMA is not autoprobed!!! */
228 static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
229 #else
230 static int io[MAX_CARDS+1];
231 static int dma[MAX_CARDS+1];
232 #endif
233 /* IRQ can be safely autoprobed */
234 static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
235
236 #ifdef MODULE
237 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_CARDS) "i");
238 MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
239 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
240 MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
241 MODULE_PARM(dma, "1-" __MODULE_STRING(MAX_CARDS) "i");
242 MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
243
244 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
245 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
246 MODULE_LICENSE("GPL");
247 #endif
248
249 /* I use this mainly for testing purposes */
250 #ifdef COSA_SLOW_IO
251 #define cosa_outb outb_p
252 #define cosa_outw outw_p
253 #define cosa_inb  inb_p
254 #define cosa_inw  inw_p
255 #else
256 #define cosa_outb outb
257 #define cosa_outw outw
258 #define cosa_inb  inb
259 #define cosa_inw  inw
260 #endif
261
262 #define is_8bit(cosa)           (!(cosa->datareg & 0x08))
263
264 #define cosa_getstatus(cosa)    (cosa_inb(cosa->statusreg))
265 #define cosa_putstatus(cosa, stat)      (cosa_outb(stat, cosa->statusreg))
266 #define cosa_getdata16(cosa)    (cosa_inw(cosa->datareg))
267 #define cosa_getdata8(cosa)     (cosa_inb(cosa->datareg))
268 #define cosa_putdata16(cosa, dt)        (cosa_outw(dt, cosa->datareg))
269 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
270
271 /* Initialization stuff */
272 static int cosa_probe(int ioaddr, int irq, int dma);
273
274 /* HW interface */
275 static void cosa_enable_rx(struct channel_data *chan);
276 static void cosa_disable_rx(struct channel_data *chan);
277 static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
278 static void cosa_kick(struct cosa_data *cosa);
279 static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
280
281 /* SPPP/HDLC stuff */
282 static void sppp_channel_init(struct channel_data *chan);
283 static void sppp_channel_delete(struct channel_data *chan);
284 static int cosa_sppp_open(struct net_device *d);
285 static int cosa_sppp_close(struct net_device *d);
286 static void cosa_sppp_timeout(struct net_device *d);
287 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *d);
288 static char *sppp_setup_rx(struct channel_data *channel, int size);
289 static int sppp_rx_done(struct channel_data *channel);
290 static int sppp_tx_done(struct channel_data *channel, int size);
291 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
292 static struct net_device_stats *cosa_net_stats(struct net_device *dev);
293
294 /* Character device */
295 static void chardev_channel_init(struct channel_data *chan);
296 static char *chrdev_setup_rx(struct channel_data *channel, int size);
297 static int chrdev_rx_done(struct channel_data *channel);
298 static int chrdev_tx_done(struct channel_data *channel, int size);
299 static ssize_t cosa_read(struct file *file,
300         char *buf, size_t count, loff_t *ppos);
301 static ssize_t cosa_write(struct file *file,
302         const char *buf, size_t count, loff_t *ppos);
303 static unsigned int cosa_poll(struct file *file, poll_table *poll);
304 static int cosa_open(struct inode *inode, struct file *file);
305 static int cosa_release(struct inode *inode, struct file *file);
306 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
307         unsigned int cmd, unsigned long arg);
308 #ifdef COSA_FASYNC_WORKING
309 static int cosa_fasync(struct inode *inode, struct file *file, int on);
310 #endif
311
312 static struct file_operations cosa_fops = {
313         .owner          = THIS_MODULE,
314         .llseek         = no_llseek,
315         .read           = cosa_read,
316         .write          = cosa_write,
317         .poll           = cosa_poll,
318         .ioctl          = cosa_chardev_ioctl,
319         .open           = cosa_open,
320         .release        = cosa_release,
321 #ifdef COSA_FASYNC_WORKING
322         .fasync         = cosa_fasync,
323 #endif
324 };
325
326 /* Ioctls */
327 static int cosa_start(struct cosa_data *cosa, int address);
328 static int cosa_reset(struct cosa_data *cosa);
329 static int cosa_download(struct cosa_data *cosa, unsigned long a);
330 static int cosa_readmem(struct cosa_data *cosa, unsigned long a);
331
332 /* COSA/SRP ROM monitor */
333 static int download(struct cosa_data *cosa, const char *data, int addr, int len);
334 static int startmicrocode(struct cosa_data *cosa, int address);
335 static int readmem(struct cosa_data *cosa, char *data, int addr, int len);
336 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
337
338 /* Auxilliary functions */
339 static int get_wait_data(struct cosa_data *cosa);
340 static int put_wait_data(struct cosa_data *cosa, int data);
341 static int puthexnumber(struct cosa_data *cosa, int number);
342 static void put_driver_status(struct cosa_data *cosa);
343 static void put_driver_status_nolock(struct cosa_data *cosa);
344
345 /* Interrupt handling */
346 static irqreturn_t cosa_interrupt(int irq, void *cosa, struct pt_regs *regs);
347
348 /* I/O ops debugging */
349 #ifdef DEBUG_IO
350 static void debug_data_in(struct cosa_data *cosa, int data);
351 static void debug_data_out(struct cosa_data *cosa, int data);
352 static void debug_data_cmd(struct cosa_data *cosa, int data);
353 static void debug_status_in(struct cosa_data *cosa, int status);
354 static void debug_status_out(struct cosa_data *cosa, int status);
355 #endif
356
357 \f
358 /* ---------- Initialization stuff ---------- */
359
360 static int __init cosa_init(void)
361 {
362         int i;
363
364         printk(KERN_INFO "cosa v1.08 (c) 1997-2000 Jan Kasprzak <kas@fi.muni.cz>\n");
365 #ifdef CONFIG_SMP
366         printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
367 #endif
368         if (cosa_major > 0) {
369                 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
370                         printk(KERN_WARNING "cosa: unable to get major %d\n",
371                                 cosa_major);
372                         return -EIO;
373                 }
374         } else {
375                 if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
376                         printk(KERN_WARNING "cosa: unable to register chardev\n");
377                         return -EIO;
378                 }
379         }
380         for (i=0; i<MAX_CARDS; i++)
381                 cosa_cards[i].num = -1;
382         for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
383                 cosa_probe(io[i], irq[i], dma[i]);
384         if (!nr_cards) {
385                 printk(KERN_WARNING "cosa: no devices found.\n");
386                 unregister_chrdev(cosa_major, "cosa");
387                 return -ENODEV;
388         }
389         devfs_mk_dir("cosa");
390         for (i=0; i<nr_cards; i++) {
391                 devfs_mk_cdev(MKDEV(cosa_major, i),
392                                 S_IFCHR|S_IRUSR|S_IWUSR,
393                                 "cosa/%d", i);
394         }
395         return 0;
396 }
397 module_init(cosa_init);
398
399 static void __exit cosa_exit(void)
400 {
401         struct cosa_data *cosa;
402         int i;
403         printk(KERN_INFO "Unloading the cosa module\n");
404
405         for (i=0; i<nr_cards; i++)
406                 devfs_remove("cosa/%d", i);
407         devfs_remove("cosa");
408         for (cosa=cosa_cards; nr_cards--; cosa++) {
409                 /* Clean up the per-channel data */
410                 for (i=0; i<cosa->nchannels; i++) {
411                         /* Chardev driver has no alloc'd per-channel data */
412                         sppp_channel_delete(cosa->chan+i);
413                 }
414                 /* Clean up the per-card data */
415                 kfree(cosa->chan);
416                 kfree(cosa->bouncebuf);
417                 free_irq(cosa->irq, cosa);
418                 free_dma(cosa->dma);
419                 release_region(cosa->datareg,is_8bit(cosa)?2:4);
420         }
421         unregister_chrdev(cosa_major, "cosa");
422 }
423 module_exit(cosa_exit);
424
425 /*
426  * This function should register all the net devices needed for the
427  * single channel.
428  */
429 static __inline__ void channel_init(struct channel_data *chan)
430 {
431         sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
432
433         /* Initialize the chardev data structures */
434         chardev_channel_init(chan);
435
436         /* Register the sppp interface */
437         sppp_channel_init(chan);
438 }
439         
440 static int cosa_probe(int base, int irq, int dma)
441 {
442         struct cosa_data *cosa = cosa_cards+nr_cards;
443         int i, err = 0;
444
445         memset(cosa, 0, sizeof(struct cosa_data));
446
447         /* Checking validity of parameters: */
448         /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
449         if ((irq >= 0  && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
450                 printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
451                 return -1;
452         }
453         /* I/O address should be between 0x100 and 0x3ff and should be
454          * multiple of 8. */
455         if (base < 0x100 || base > 0x3ff || base & 0x7) {
456                 printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
457                         base);
458                 return -1;
459         }
460         /* DMA should be 0,1 or 3-7 */
461         if (dma < 0 || dma == 4 || dma > 7) {
462                 printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
463                 return -1;
464         }
465         /* and finally, on 16-bit COSA DMA should be 4-7 and 
466          * I/O base should not be multiple of 0x10 */
467         if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
468                 printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
469                         " (base=0x%x, dma=%d)\n", base, dma);
470                 return -1;
471         }
472
473         cosa->dma = dma;
474         cosa->datareg = base;
475         cosa->statusreg = is_8bit(cosa)?base+1:base+2;
476         spin_lock_init(&cosa->lock);
477
478         if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
479                 return -1;
480         
481         if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
482                 printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
483                 err = -1;
484                 goto err_out;
485         }
486
487         /* Test the validity of identification string */
488         if (!strncmp(cosa->id_string, "SRP", 3))
489                 cosa->type = "srp";
490         else if (!strncmp(cosa->id_string, "COSA", 4))
491                 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
492         else {
493 /* Print a warning only if we are not autoprobing */
494 #ifndef COSA_ISA_AUTOPROBE
495                 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
496                         base);
497 #endif
498                 err = -1;
499                 goto err_out;
500         }
501         /* Update the name of the region now we know the type of card */ 
502         release_region(base, is_8bit(cosa)?2:4);
503         if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
504                 printk(KERN_DEBUG "cosa: changing name at 0x%x failed.\n", base);
505                 return -1;
506         }
507
508         /* Now do IRQ autoprobe */
509         if (irq < 0) {
510                 unsigned long irqs;
511 /*              printk(KERN_INFO "IRQ autoprobe\n"); */
512                 irqs = probe_irq_on();
513                 /* 
514                  * Enable interrupt on tx buffer empty (it sure is) 
515                  * really sure ?
516                  * FIXME: When this code is not used as module, we should
517                  * probably call udelay() instead of the interruptible sleep.
518                  */
519                 current->state = TASK_INTERRUPTIBLE;
520                 cosa_putstatus(cosa, SR_TX_INT_ENA);
521                 schedule_timeout(30);
522                 irq = probe_irq_off(irqs);
523                 /* Disable all IRQs from the card */
524                 cosa_putstatus(cosa, 0);
525                 /* Empty the received data register */
526                 cosa_getdata8(cosa);
527
528                 if (irq < 0) {
529                         printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
530                                 irq, cosa->datareg);
531                         err = -1;
532                         goto err_out;
533                 }
534                 if (irq == 0) {
535                         printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
536                                 cosa->datareg);
537                 /*      return -1; */
538                 }
539         }
540
541         cosa->irq = irq;
542         cosa->num = nr_cards;
543         cosa->usage = 0;
544         cosa->nchannels = 2;    /* FIXME: how to determine this? */
545
546         if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
547                 err = -1;
548                 goto err_out;
549         }
550         if (request_dma(cosa->dma, cosa->type)) {
551                 err = -1;
552                 goto err_out1;
553         }
554         
555         cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
556         if (!cosa->bouncebuf) {
557                 err = -ENOMEM;
558                 goto err_out2;
559         }
560         sprintf(cosa->name, "cosa%d", cosa->num);
561
562         /* Initialize the per-channel data */
563         cosa->chan = kmalloc(sizeof(struct channel_data)*cosa->nchannels,
564                              GFP_KERNEL);
565         if (!cosa->chan) {
566                 err = -ENOMEM;
567                 goto err_out3;
568         }
569         memset(cosa->chan, 0, sizeof(struct channel_data)*cosa->nchannels);
570         for (i=0; i<cosa->nchannels; i++) {
571                 cosa->chan[i].cosa = cosa;
572                 cosa->chan[i].num = i;
573                 channel_init(cosa->chan+i);
574         }
575
576         printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
577                 cosa->num, cosa->id_string, cosa->type,
578                 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
579
580         return nr_cards++;
581 err_out3:
582         kfree(cosa->bouncebuf);
583 err_out2:
584         free_dma(cosa->dma);
585 err_out1:
586         free_irq(cosa->irq, cosa);
587 err_out:        
588         release_region(cosa->datareg,is_8bit(cosa)?2:4);
589         printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
590                cosa->num);
591         return err;
592 }
593
594 \f
595 /*---------- SPPP/HDLC netdevice ---------- */
596
597 static void cosa_setup(struct net_device *d)
598 {
599         d->open = cosa_sppp_open;
600         d->stop = cosa_sppp_close;
601         d->hard_start_xmit = cosa_sppp_tx;
602         d->do_ioctl = cosa_sppp_ioctl;
603         d->get_stats = cosa_net_stats;
604         d->tx_timeout = cosa_sppp_timeout;
605         d->watchdog_timeo = TX_TIMEOUT;
606 }
607
608 static void sppp_channel_init(struct channel_data *chan)
609 {
610         struct net_device *d;
611         chan->if_ptr = &chan->pppdev;
612         d = alloc_netdev(0, chan->name, cosa_setup);
613         if (!d) {
614                 printk(KERN_WARNING "%s: alloc_netdev failed.\n", chan->name);
615                 return;
616         }
617         chan->pppdev.dev = d;
618         sppp_attach(&chan->pppdev);
619         d->base_addr = chan->cosa->datareg;
620         d->irq = chan->cosa->irq;
621         d->dma = chan->cosa->dma;
622         d->priv = chan;
623         if (register_netdev(d)) {
624                 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
625                 sppp_detach(d);
626                 free_netdev(d);
627                 chan->pppdev.dev = NULL;
628                 return;
629         }
630 }
631
632 static void sppp_channel_delete(struct channel_data *chan)
633 {
634         unregister_netdev(chan->pppdev.dev);
635         sppp_detach(chan->pppdev.dev);
636         free_netdev(chan->pppdev.dev);
637         chan->pppdev.dev = NULL;
638 }
639
640 static int cosa_sppp_open(struct net_device *d)
641 {
642         struct channel_data *chan = d->priv;
643         int err;
644         unsigned long flags;
645
646         if (!(chan->cosa->firmware_status & COSA_FW_START)) {
647                 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
648                         chan->cosa->name, chan->cosa->firmware_status);
649                 return -EPERM;
650         }
651         spin_lock_irqsave(&chan->cosa->lock, flags);
652         if (chan->usage != 0) {
653                 printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
654                         chan->name, chan->usage);
655                 spin_unlock_irqrestore(&chan->cosa->lock, flags);
656                 return -EBUSY;
657         }
658         chan->setup_rx = sppp_setup_rx;
659         chan->tx_done = sppp_tx_done;
660         chan->rx_done = sppp_rx_done;
661         chan->usage=-1;
662         chan->cosa->usage++;
663         spin_unlock_irqrestore(&chan->cosa->lock, flags);
664
665         err = sppp_open(d);
666         if (err) {
667                 spin_lock_irqsave(&chan->cosa->lock, flags);
668                 chan->usage=0;
669                 chan->cosa->usage--;
670                 
671                 spin_unlock_irqrestore(&chan->cosa->lock, flags);
672                 return err;
673         }
674
675         netif_start_queue(d);
676         cosa_enable_rx(chan);
677         return 0;
678 }
679
680 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *dev)
681 {
682         struct channel_data *chan = dev->priv;
683
684         netif_stop_queue(dev);
685
686         chan->tx_skb = skb;
687         cosa_start_tx(chan, skb->data, skb->len);
688         return 0;
689 }
690
691 static void cosa_sppp_timeout(struct net_device *dev)
692 {
693         struct channel_data *chan = dev->priv;
694
695         if (test_bit(RXBIT, &chan->cosa->rxtx)) {
696                 chan->stats.rx_errors++;
697                 chan->stats.rx_missed_errors++;
698         } else {
699                 chan->stats.tx_errors++;
700                 chan->stats.tx_aborted_errors++;
701         }
702         cosa_kick(chan->cosa);
703         if (chan->tx_skb) {
704                 dev_kfree_skb(chan->tx_skb);
705                 chan->tx_skb = 0;
706         }
707         netif_wake_queue(dev);
708 }
709
710 static int cosa_sppp_close(struct net_device *d)
711 {
712         struct channel_data *chan = d->priv;
713         unsigned long flags;
714
715         netif_stop_queue(d);
716         sppp_close(d);
717         cosa_disable_rx(chan);
718         spin_lock_irqsave(&chan->cosa->lock, flags);
719         if (chan->rx_skb) {
720                 kfree_skb(chan->rx_skb);
721                 chan->rx_skb = 0;
722         }
723         if (chan->tx_skb) {
724                 kfree_skb(chan->tx_skb);
725                 chan->tx_skb = 0;
726         }
727         chan->usage=0;
728         chan->cosa->usage--;
729         spin_unlock_irqrestore(&chan->cosa->lock, flags);
730         return 0;
731 }
732
733 static char *sppp_setup_rx(struct channel_data *chan, int size)
734 {
735         /*
736          * We can safely fall back to non-dma-able memory, because we have
737          * the cosa->bouncebuf pre-allocated.
738          */
739         if (chan->rx_skb)
740                 kfree_skb(chan->rx_skb);
741         chan->rx_skb = dev_alloc_skb(size);
742         if (chan->rx_skb == NULL) {
743                 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
744                         chan->name);
745                 chan->stats.rx_dropped++;
746                 return NULL;
747         }
748         chan->pppdev.dev->trans_start = jiffies;
749         return skb_put(chan->rx_skb, size);
750 }
751
752 static int sppp_rx_done(struct channel_data *chan)
753 {
754         if (!chan->rx_skb) {
755                 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
756                         chan->name);
757                 chan->stats.rx_errors++;
758                 chan->stats.rx_frame_errors++;
759                 return 0;
760         }
761         chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
762         chan->rx_skb->dev = chan->pppdev.dev;
763         chan->rx_skb->mac.raw = chan->rx_skb->data;
764         chan->stats.rx_packets++;
765         chan->stats.rx_bytes += chan->cosa->rxsize;
766         netif_rx(chan->rx_skb);
767         chan->rx_skb = 0;
768         chan->pppdev.dev->last_rx = jiffies;
769         return 0;
770 }
771
772 /* ARGSUSED */
773 static int sppp_tx_done(struct channel_data *chan, int size)
774 {
775         if (!chan->tx_skb) {
776                 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
777                         chan->name);
778                 chan->stats.tx_errors++;
779                 chan->stats.tx_aborted_errors++;
780                 return 1;
781         }
782         dev_kfree_skb_irq(chan->tx_skb);
783         chan->tx_skb = 0;
784         chan->stats.tx_packets++;
785         chan->stats.tx_bytes += size;
786         netif_wake_queue(chan->pppdev.dev);
787         return 1;
788 }
789
790 static struct net_device_stats *cosa_net_stats(struct net_device *dev)
791 {
792         struct channel_data *chan = dev->priv;
793         return &chan->stats;
794 }
795
796 \f
797 /*---------- Character device ---------- */
798
799 static void chardev_channel_init(struct channel_data *chan)
800 {
801         init_MUTEX(&chan->rsem);
802         init_MUTEX(&chan->wsem);
803 }
804
805 static ssize_t cosa_read(struct file *file,
806         char *buf, size_t count, loff_t *ppos)
807 {
808         DECLARE_WAITQUEUE(wait, current);
809         unsigned long flags;
810         struct channel_data *chan = file->private_data;
811         struct cosa_data *cosa = chan->cosa;
812         char *kbuf;
813
814         if (!(cosa->firmware_status & COSA_FW_START)) {
815                 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
816                         cosa->name, cosa->firmware_status);
817                 return -EPERM;
818         }
819         if (down_interruptible(&chan->rsem))
820                 return -ERESTARTSYS;
821         
822         if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
823                 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
824                 up(&chan->rsem);
825                 return -ENOMEM;
826         }
827
828         chan->rx_status = 0;
829         cosa_enable_rx(chan);
830         spin_lock_irqsave(&cosa->lock, flags);
831         add_wait_queue(&chan->rxwaitq, &wait);
832         while(!chan->rx_status) {
833                 current->state = TASK_INTERRUPTIBLE;
834                 spin_unlock_irqrestore(&cosa->lock, flags);
835                 schedule();
836                 spin_lock_irqsave(&cosa->lock, flags);
837                 if (signal_pending(current) && chan->rx_status == 0) {
838                         chan->rx_status = 1;
839                         remove_wait_queue(&chan->rxwaitq, &wait);
840                         current->state = TASK_RUNNING;
841                         spin_unlock_irqrestore(&cosa->lock, flags);
842                         up(&chan->rsem);
843                         return -ERESTARTSYS;
844                 }
845         }
846         remove_wait_queue(&chan->rxwaitq, &wait);
847         current->state = TASK_RUNNING;
848         kbuf = chan->rxdata;
849         count = chan->rxsize;
850         spin_unlock_irqrestore(&cosa->lock, flags);
851         up(&chan->rsem);
852
853         if (copy_to_user(buf, kbuf, count)) {
854                 kfree(kbuf);
855                 return -EFAULT;
856         }
857         kfree(kbuf);
858         return count;
859 }
860
861 static char *chrdev_setup_rx(struct channel_data *chan, int size)
862 {
863         /* Expect size <= COSA_MTU */
864         chan->rxsize = size;
865         return chan->rxdata;
866 }
867
868 static int chrdev_rx_done(struct channel_data *chan)
869 {
870         if (chan->rx_status) { /* Reader has died */
871                 kfree(chan->rxdata);
872                 up(&chan->wsem);
873         }
874         chan->rx_status = 1;
875         wake_up_interruptible(&chan->rxwaitq);
876         return 1;
877 }
878
879
880 static ssize_t cosa_write(struct file *file,
881         const char *buf, size_t count, loff_t *ppos)
882 {
883         DECLARE_WAITQUEUE(wait, current);
884         struct channel_data *chan = file->private_data;
885         struct cosa_data *cosa = chan->cosa;
886         unsigned long flags;
887         char *kbuf;
888
889         if (!(cosa->firmware_status & COSA_FW_START)) {
890                 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
891                         cosa->name, cosa->firmware_status);
892                 return -EPERM;
893         }
894         if (down_interruptible(&chan->wsem))
895                 return -ERESTARTSYS;
896
897         if (count > COSA_MTU)
898                 count = COSA_MTU;
899         
900         /* Allocate the buffer */
901         if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
902                 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
903                         cosa->name);
904                 up(&chan->wsem);
905                 return -ENOMEM;
906         }
907         if (copy_from_user(kbuf, buf, count)) {
908                 up(&chan->wsem);
909                 kfree(kbuf);
910                 return -EFAULT;
911         }
912         chan->tx_status=0;
913         cosa_start_tx(chan, kbuf, count);
914
915         spin_lock_irqsave(&cosa->lock, flags);
916         add_wait_queue(&chan->txwaitq, &wait);
917         while(!chan->tx_status) {
918                 current->state = TASK_INTERRUPTIBLE;
919                 spin_unlock_irqrestore(&cosa->lock, flags);
920                 schedule();
921                 spin_lock_irqsave(&cosa->lock, flags);
922                 if (signal_pending(current) && chan->tx_status == 0) {
923                         chan->tx_status = 1;
924                         remove_wait_queue(&chan->txwaitq, &wait);
925                         current->state = TASK_RUNNING;
926                         chan->tx_status = 1;
927                         spin_unlock_irqrestore(&cosa->lock, flags);
928                         return -ERESTARTSYS;
929                 }
930         }
931         remove_wait_queue(&chan->txwaitq, &wait);
932         current->state = TASK_RUNNING;
933         up(&chan->wsem);
934         spin_unlock_irqrestore(&cosa->lock, flags);
935         kfree(kbuf);
936         return count;
937 }
938
939 static int chrdev_tx_done(struct channel_data *chan, int size)
940 {
941         if (chan->tx_status) { /* Writer was interrupted */
942                 kfree(chan->txbuf);
943                 up(&chan->wsem);
944         }
945         chan->tx_status = 1;
946         wake_up_interruptible(&chan->txwaitq);
947         return 1;
948 }
949
950 static unsigned int cosa_poll(struct file *file, poll_table *poll)
951 {
952         printk(KERN_INFO "cosa_poll is here\n");
953         return 0;
954 }
955
956 static int cosa_open(struct inode *inode, struct file *file)
957 {
958         struct cosa_data *cosa;
959         struct channel_data *chan;
960         unsigned long flags;
961         int n;
962
963         if ((n=iminor(file->f_dentry->d_inode)>>CARD_MINOR_BITS)
964                 >= nr_cards)
965                 return -ENODEV;
966         cosa = cosa_cards+n;
967
968         if ((n=iminor(file->f_dentry->d_inode)
969                 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
970                 return -ENODEV;
971         chan = cosa->chan + n;
972         
973         file->private_data = chan;
974
975         spin_lock_irqsave(&cosa->lock, flags);
976
977         if (chan->usage < 0) { /* in netdev mode */
978                 spin_unlock_irqrestore(&cosa->lock, flags);
979                 return -EBUSY;
980         }
981         cosa->usage++;
982         chan->usage++;
983
984         chan->tx_done = chrdev_tx_done;
985         chan->setup_rx = chrdev_setup_rx;
986         chan->rx_done = chrdev_rx_done;
987         spin_unlock_irqrestore(&cosa->lock, flags);
988         return 0;
989 }
990
991 static int cosa_release(struct inode *inode, struct file *file)
992 {
993         struct channel_data *channel = file->private_data;
994         struct cosa_data *cosa;
995         unsigned long flags;
996
997         cosa = channel->cosa;
998         spin_lock_irqsave(&cosa->lock, flags);
999         cosa->usage--;
1000         channel->usage--;
1001         spin_unlock_irqrestore(&cosa->lock, flags);
1002         return 0;
1003 }
1004
1005 #ifdef COSA_FASYNC_WORKING
1006 static struct fasync_struct *fasync[256] = { NULL, };
1007
1008 /* To be done ... */
1009 static int cosa_fasync(struct inode *inode, struct file *file, int on)
1010 {
1011         int port = iminor(inode);
1012         int rv = fasync_helper(inode, file, on, &fasync[port]);
1013         return rv < 0 ? rv : 0;
1014 }
1015 #endif
1016
1017 \f
1018 /* ---------- Ioctls ---------- */
1019
1020 /*
1021  * Ioctl subroutines can safely be made inline, because they are called
1022  * only from cosa_ioctl().
1023  */
1024 static inline int cosa_reset(struct cosa_data *cosa)
1025 {
1026         char idstring[COSA_MAX_ID_STRING];
1027         if (cosa->usage > 1)
1028                 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1029                         cosa->num, cosa->usage);
1030         cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1031         if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1032                 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1033                 return -EIO;
1034         }
1035         printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1036                 idstring);
1037         cosa->firmware_status |= COSA_FW_RESET;
1038         return 0;
1039 }
1040
1041 /* High-level function to download data into COSA memory. Calls download() */
1042 static inline int cosa_download(struct cosa_data *cosa, unsigned long arg)
1043 {
1044         struct cosa_download d;
1045         int i;
1046
1047         if (cosa->usage > 1)
1048                 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1049                         cosa->name, cosa->usage);
1050         if (!(cosa->firmware_status & COSA_FW_RESET)) {
1051                 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1052                         cosa->name, cosa->firmware_status);
1053                 return -EPERM;
1054         }
1055         
1056         if (copy_from_user(&d, (void __user *) arg, sizeof(d)))
1057                 return -EFAULT;
1058
1059         if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1060                 return -EINVAL;
1061         if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1062                 return -EINVAL;
1063
1064
1065         /* If something fails, force the user to reset the card */
1066         cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1067
1068         i = download(cosa, d.code, d.len, d.addr);
1069         if (i < 0) {
1070                 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1071                         cosa->num, i);
1072                 return -EIO;
1073         }
1074         printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1075                 cosa->num, d.len, d.addr);
1076         cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1077         return 0;
1078 }
1079
1080 /* High-level function to read COSA memory. Calls readmem() */
1081 static inline int cosa_readmem(struct cosa_data *cosa, unsigned long arg)
1082 {
1083         struct cosa_download d;
1084         int i;
1085
1086         if (cosa->usage > 1)
1087                 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1088                         "cosa->usage > 1 (%d). Odd things may happen.\n",
1089                         cosa->num, cosa->usage);
1090         if (!(cosa->firmware_status & COSA_FW_RESET)) {
1091                 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1092                         cosa->name, cosa->firmware_status);
1093                 return -EPERM;
1094         }
1095
1096         if (copy_from_user(&d, (void __user *) arg, sizeof(d)))
1097                 return -EFAULT;
1098
1099         /* If something fails, force the user to reset the card */
1100         cosa->firmware_status &= ~COSA_FW_RESET;
1101
1102         i = readmem(cosa, d.code, d.len, d.addr);
1103         if (i < 0) {
1104                 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1105                         cosa->num, i);
1106                 return -EIO;
1107         }
1108         printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1109                 cosa->num, d.len, d.addr);
1110         cosa->firmware_status |= COSA_FW_RESET;
1111         return 0;
1112 }
1113
1114 /* High-level function to start microcode. Calls startmicrocode(). */
1115 static inline int cosa_start(struct cosa_data *cosa, int address)
1116 {
1117         int i;
1118
1119         if (cosa->usage > 1)
1120                 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1121                         cosa->num, cosa->usage);
1122
1123         if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1124                 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1125                 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1126                         cosa->name, cosa->firmware_status);
1127                 return -EPERM;
1128         }
1129         cosa->firmware_status &= ~COSA_FW_RESET;
1130         if ((i=startmicrocode(cosa, address)) < 0) {
1131                 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1132                         cosa->num, address, i);
1133                 return -EIO;
1134         }
1135         printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1136                 cosa->num, address);
1137         cosa->startaddr = address;
1138         cosa->firmware_status |= COSA_FW_START;
1139         return 0;
1140 }
1141                 
1142 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1143 static inline int cosa_getidstr(struct cosa_data *cosa, char *string)
1144 {
1145         int l = strlen(cosa->id_string)+1;
1146         if (copy_to_user(string, cosa->id_string, l))
1147                 return -EFAULT;
1148         return l;
1149 }
1150
1151 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1152 static inline int cosa_gettype(struct cosa_data *cosa, char *string)
1153 {
1154         int l = strlen(cosa->type)+1;
1155         if (copy_to_user(string, cosa->type, l))
1156                 return -EFAULT;
1157         return l;
1158 }
1159
1160 static int cosa_ioctl_common(struct cosa_data *cosa,
1161         struct channel_data *channel, unsigned int cmd, unsigned long arg)
1162 {
1163         switch(cmd) {
1164         case COSAIORSET:        /* Reset the device */
1165                 if (!capable(CAP_NET_ADMIN))
1166                         return -EACCES;
1167                 return cosa_reset(cosa);
1168         case COSAIOSTRT:        /* Start the firmware */
1169                 if (!capable(CAP_SYS_RAWIO))
1170                         return -EACCES;
1171                 return cosa_start(cosa, arg);
1172         case COSAIODOWNLD:      /* Download the firmware */
1173                 if (!capable(CAP_SYS_RAWIO))
1174                         return -EACCES;
1175                 
1176                 return cosa_download(cosa, arg);
1177         case COSAIORMEM:
1178                 if (!capable(CAP_SYS_RAWIO))
1179                         return -EACCES;
1180                 return cosa_readmem(cosa, arg);
1181         case COSAIORTYPE:
1182                 return cosa_gettype(cosa, (char *)arg);
1183         case COSAIORIDSTR:
1184                 return cosa_getidstr(cosa, (char *)arg);
1185         case COSAIONRCARDS:
1186                 return nr_cards;
1187         case COSAIONRCHANS:
1188                 return cosa->nchannels;
1189         case COSAIOBMSET:
1190                 if (!capable(CAP_SYS_RAWIO))
1191                         return -EACCES;
1192                 if (is_8bit(cosa))
1193                         return -EINVAL;
1194                 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1195                         return -EINVAL;
1196                 cosa->busmaster = arg;
1197                 return 0;
1198         case COSAIOBMGET:
1199                 return cosa->busmaster;
1200         }
1201         return -ENOIOCTLCMD;
1202 }
1203
1204 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
1205         int cmd)
1206 {
1207         int rv;
1208         struct channel_data *chan = dev->priv;
1209         rv = cosa_ioctl_common(chan->cosa, chan, cmd, (unsigned long)ifr->ifr_data);
1210         if (rv == -ENOIOCTLCMD) {
1211                 return sppp_do_ioctl(dev, ifr, cmd);
1212         }
1213         return rv;
1214 }
1215
1216 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1217         unsigned int cmd, unsigned long arg)
1218 {
1219         struct channel_data *channel = file->private_data;
1220         struct cosa_data *cosa = channel->cosa;
1221         return cosa_ioctl_common(cosa, channel, cmd, arg);
1222 }
1223
1224 \f
1225 /*---------- HW layer interface ---------- */
1226
1227 /*
1228  * The higher layer can bind itself to the HW layer by setting the callbacks
1229  * in the channel_data structure and by using these routines.
1230  */
1231 static void cosa_enable_rx(struct channel_data *chan)
1232 {
1233         struct cosa_data *cosa = chan->cosa;
1234
1235         if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1236                 put_driver_status(cosa);
1237 }
1238
1239 static void cosa_disable_rx(struct channel_data *chan)
1240 {
1241         struct cosa_data *cosa = chan->cosa;
1242
1243         if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1244                 put_driver_status(cosa);
1245 }
1246
1247 /*
1248  * FIXME: This routine probably should check for cosa_start_tx() called when
1249  * the previous transmit is still unfinished. In this case the non-zero
1250  * return value should indicate to the caller that the queuing(sp?) up
1251  * the transmit has failed.
1252  */
1253 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1254 {
1255         struct cosa_data *cosa = chan->cosa;
1256         unsigned long flags;
1257 #ifdef DEBUG_DATA
1258         int i;
1259
1260         printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1261                 chan->num, len);
1262         for (i=0; i<len; i++)
1263                 printk(" %02x", buf[i]&0xff);
1264         printk("\n");
1265 #endif
1266         spin_lock_irqsave(&cosa->lock, flags);
1267         chan->txbuf = buf;
1268         chan->txsize = len;
1269         if (len > COSA_MTU)
1270                 chan->txsize = COSA_MTU;
1271         spin_unlock_irqrestore(&cosa->lock, flags);
1272
1273         /* Tell the firmware we are ready */
1274         set_bit(chan->num, &cosa->txbitmap);
1275         put_driver_status(cosa);
1276
1277         return 0;
1278 }
1279
1280 static void put_driver_status(struct cosa_data *cosa)
1281 {
1282         unsigned long flags;
1283         int status;
1284
1285         spin_lock_irqsave(&cosa->lock, flags);
1286
1287         status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1288                 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1289                 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1290                         &DRIVER_TXMAP_MASK : 0);
1291         if (!cosa->rxtx) {
1292                 if (cosa->rxbitmap|cosa->txbitmap) {
1293                         if (!cosa->enabled) {
1294                                 cosa_putstatus(cosa, SR_RX_INT_ENA);
1295 #ifdef DEBUG_IO
1296                                 debug_status_out(cosa, SR_RX_INT_ENA);
1297 #endif
1298                                 cosa->enabled = 1;
1299                         }
1300                 } else if (cosa->enabled) {
1301                         cosa->enabled = 0;
1302                         cosa_putstatus(cosa, 0);
1303 #ifdef DEBUG_IO
1304                         debug_status_out(cosa, 0);
1305 #endif
1306                 }
1307                 cosa_putdata8(cosa, status);
1308 #ifdef DEBUG_IO
1309                 debug_data_cmd(cosa, status);
1310 #endif
1311         }
1312         spin_unlock_irqrestore(&cosa->lock, flags);
1313 }
1314
1315 static void put_driver_status_nolock(struct cosa_data *cosa)
1316 {
1317         int status;
1318
1319         status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1320                 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1321                 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1322                         &DRIVER_TXMAP_MASK : 0);
1323
1324         if (cosa->rxbitmap|cosa->txbitmap) {
1325                 cosa_putstatus(cosa, SR_RX_INT_ENA);
1326 #ifdef DEBUG_IO
1327                 debug_status_out(cosa, SR_RX_INT_ENA);
1328 #endif
1329                 cosa->enabled = 1;
1330         } else {
1331                 cosa_putstatus(cosa, 0);
1332 #ifdef DEBUG_IO
1333                 debug_status_out(cosa, 0);
1334 #endif
1335                 cosa->enabled = 0;
1336         }
1337         cosa_putdata8(cosa, status);
1338 #ifdef DEBUG_IO
1339         debug_data_cmd(cosa, status);
1340 #endif
1341 }
1342
1343 /*
1344  * The "kickme" function: When the DMA times out, this is called to
1345  * clean up the driver status.
1346  * FIXME: Preliminary support, the interface is probably wrong.
1347  */
1348 static void cosa_kick(struct cosa_data *cosa)
1349 {
1350         unsigned long flags, flags1;
1351         char *s = "(probably) IRQ";
1352
1353         if (test_bit(RXBIT, &cosa->rxtx))
1354                 s = "RX DMA";
1355         if (test_bit(TXBIT, &cosa->rxtx))
1356                 s = "TX DMA";
1357
1358         printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s); 
1359         spin_lock_irqsave(&cosa->lock, flags);
1360         cosa->rxtx = 0;
1361
1362         flags1 = claim_dma_lock();
1363         disable_dma(cosa->dma);
1364         clear_dma_ff(cosa->dma);
1365         release_dma_lock(flags1);
1366
1367         /* FIXME: Anything else? */
1368         udelay(100);
1369         cosa_putstatus(cosa, 0);
1370         udelay(100);
1371         (void) cosa_getdata8(cosa);
1372         udelay(100);
1373         cosa_putdata8(cosa, 0);
1374         udelay(100);
1375         put_driver_status_nolock(cosa);
1376         spin_unlock_irqrestore(&cosa->lock, flags);
1377 }
1378
1379 /*
1380  * Check if the whole buffer is DMA-able. It means it is below the 16M of
1381  * physical memory and doesn't span the 64k boundary. For now it seems
1382  * SKB's never do this, but we'll check this anyway.
1383  */
1384 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1385 {
1386         static int count;
1387         unsigned long b = (unsigned long)buf;
1388         if (b+len >= MAX_DMA_ADDRESS)
1389                 return 0;
1390         if ((b^ (b+len)) & 0x10000) {
1391                 if (count++ < 5)
1392                         printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1393                                 chan->name);
1394                 return 0;
1395         }
1396         return 1;
1397 }
1398
1399 \f
1400 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1401
1402 /*
1403  * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1404  * drivers need to say 4-digit hex number meaning start address of the microcode
1405  * separated by a single space. Monitor replies by saying " =". Now driver
1406  * has to write 4-digit hex number meaning the last byte address ended
1407  * by a single space. Monitor has to reply with a space. Now the download
1408  * begins. After the download monitor replies with "\r\n." (CR LF dot).
1409  */
1410 static int download(struct cosa_data *cosa, const char *microcode, int length, int address)
1411 {
1412         int i;
1413
1414         if (put_wait_data(cosa, 'w') == -1) return -1;
1415         if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1416         if (get_wait_data(cosa) != '=') return -3;
1417
1418         if (puthexnumber(cosa, address) < 0) return -4;
1419         if (put_wait_data(cosa, ' ') == -1) return -10;
1420         if (get_wait_data(cosa) != ' ') return -11;
1421         if (get_wait_data(cosa) != '=') return -12;
1422
1423         if (puthexnumber(cosa, address+length-1) < 0) return -13;
1424         if (put_wait_data(cosa, ' ') == -1) return -18;
1425         if (get_wait_data(cosa) != ' ') return -19;
1426
1427         while (length--) {
1428                 char c;
1429 #ifndef SRP_DOWNLOAD_AT_BOOT
1430                 if (get_user(c, microcode))
1431                         return -23; /* ??? */
1432 #else
1433                 c = *microcode;
1434 #endif
1435                 if (put_wait_data(cosa, c) == -1)
1436                         return -20;
1437                 microcode++;
1438         }
1439
1440         if (get_wait_data(cosa) != '\r') return -21;
1441         if (get_wait_data(cosa) != '\n') return -22;
1442         if (get_wait_data(cosa) != '.') return -23;
1443 #if 0
1444         printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1445 #endif
1446         return 0;
1447 }
1448
1449
1450 /*
1451  * Starting microcode is done via the "g" command of the SRP monitor.
1452  * The chat should be the following: "g" "g=" "<addr><CR>"
1453  * "<CR><CR><LF><CR><LF>".
1454  */
1455 static int startmicrocode(struct cosa_data *cosa, int address)
1456 {
1457         if (put_wait_data(cosa, 'g') == -1) return -1;
1458         if (get_wait_data(cosa) != 'g') return -2;
1459         if (get_wait_data(cosa) != '=') return -3;
1460
1461         if (puthexnumber(cosa, address) < 0) return -4;
1462         if (put_wait_data(cosa, '\r') == -1) return -5;
1463         
1464         if (get_wait_data(cosa) != '\r') return -6;
1465         if (get_wait_data(cosa) != '\r') return -7;
1466         if (get_wait_data(cosa) != '\n') return -8;
1467         if (get_wait_data(cosa) != '\r') return -9;
1468         if (get_wait_data(cosa) != '\n') return -10;
1469 #if 0
1470         printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1471 #endif
1472         return 0;
1473 }
1474
1475 /*
1476  * Reading memory is done via the "r" command of the SRP monitor.
1477  * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1478  * Then driver can read the data and the conversation is finished
1479  * by SRP monitor sending "<CR><LF>." (dot at the end).
1480  *
1481  * This routine is not needed during the normal operation and serves
1482  * for debugging purposes only.
1483  */
1484 static int readmem(struct cosa_data *cosa, char *microcode, int length, int address)
1485 {
1486         if (put_wait_data(cosa, 'r') == -1) return -1;
1487         if ((get_wait_data(cosa)) != 'r') return -2;
1488         if ((get_wait_data(cosa)) != '=') return -3;
1489
1490         if (puthexnumber(cosa, address) < 0) return -4;
1491         if (put_wait_data(cosa, ' ') == -1) return -5;
1492         if (get_wait_data(cosa) != ' ') return -6;
1493         if (get_wait_data(cosa) != '=') return -7;
1494
1495         if (puthexnumber(cosa, address+length-1) < 0) return -8;
1496         if (put_wait_data(cosa, ' ') == -1) return -9;
1497         if (get_wait_data(cosa) != ' ') return -10;
1498
1499         while (length--) {
1500                 char c;
1501                 int i;
1502                 if ((i=get_wait_data(cosa)) == -1) {
1503                         printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1504                                 length);
1505                         return -11;
1506                 }
1507                 c=i;
1508 #if 1
1509                 if (put_user(c, microcode))
1510                         return -23; /* ??? */
1511 #else
1512                 *microcode = c;
1513 #endif
1514                 microcode++;
1515         }
1516
1517         if (get_wait_data(cosa) != '\r') return -21;
1518         if (get_wait_data(cosa) != '\n') return -22;
1519         if (get_wait_data(cosa) != '.') return -23;
1520 #if 0
1521         printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1522 #endif
1523         return 0;
1524 }
1525
1526 /*
1527  * This function resets the device and reads the initial prompt
1528  * of the device's ROM monitor.
1529  */
1530 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1531 {
1532         int i=0, id=0, prev=0, curr=0;
1533
1534         /* Reset the card ... */
1535         cosa_putstatus(cosa, 0);
1536         cosa_getdata8(cosa);
1537         cosa_putstatus(cosa, SR_RST);
1538 #ifdef MODULE
1539         current->state = TASK_INTERRUPTIBLE;
1540         schedule_timeout(HZ/2);
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 \f
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 \f
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 inline void 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 irqreturn_t 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         return IRQ_HANDLED;
2004 }
2005
2006 \f
2007 /* ---------- I/O debugging routines ---------- */
2008 /*
2009  * These routines can be used to monitor COSA/SRP I/O and to printk()
2010  * the data being transferred on the data and status I/O port in a
2011  * readable way.
2012  */
2013
2014 #ifdef DEBUG_IO
2015 static void debug_status_in(struct cosa_data *cosa, int status)
2016 {
2017         char *s;
2018         switch(status & SR_CMD_FROM_SRP_MASK) {
2019         case SR_UP_REQUEST:
2020                 s = "RX_REQ";
2021                 break;
2022         case SR_DOWN_REQUEST:
2023                 s = "TX_REQ";
2024                 break;
2025         case SR_END_OF_TRANSFER:
2026                 s = "ET_REQ";
2027                 break;
2028         default:
2029                 s = "NO_REQ";
2030                 break;
2031         }
2032         printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2033                 cosa->name,
2034                 status,
2035                 status & SR_USR_RQ ? "USR_RQ|":"",
2036                 status & SR_TX_RDY ? "TX_RDY|":"",
2037                 status & SR_RX_RDY ? "RX_RDY|":"",
2038                 s);
2039 }
2040
2041 static void debug_status_out(struct cosa_data *cosa, int status)
2042 {
2043         printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2044                 cosa->name,
2045                 status,
2046                 status & SR_RX_DMA_ENA  ? "RXDMA|":"!rxdma|",
2047                 status & SR_TX_DMA_ENA  ? "TXDMA|":"!txdma|",
2048                 status & SR_RST         ? "RESET|":"",
2049                 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2050                 status & SR_TX_INT_ENA  ? "TXINT|":"!txint|",
2051                 status & SR_RX_INT_ENA  ? "RXINT":"!rxint");
2052 }
2053
2054 static void debug_data_in(struct cosa_data *cosa, int data)
2055 {
2056         printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2057 }
2058
2059 static void debug_data_out(struct cosa_data *cosa, int data)
2060 {
2061         printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2062 }
2063
2064 static void debug_data_cmd(struct cosa_data *cosa, int data)
2065 {
2066         printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2067                 cosa->name, data,
2068                 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2069                 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2070 }
2071 #endif
2072
2073 /* EOF -- this file has not been truncated */