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