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