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