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