Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / drivers / net / wan / cosa.c
1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
2
3 /*
4  *  Copyright (C) 1995-1997  Jan "Yenya" Kasprzak <kas@fi.muni.cz>
5  *  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                         up(&chan->wsem);
911                         return -ERESTARTSYS;
912                 }
913         }
914         remove_wait_queue(&chan->txwaitq, &wait);
915         current->state = TASK_RUNNING;
916         up(&chan->wsem);
917         spin_unlock_irqrestore(&cosa->lock, flags);
918         kfree(kbuf);
919         return count;
920 }
921
922 static int chrdev_tx_done(struct channel_data *chan, int size)
923 {
924         if (chan->tx_status) { /* Writer was interrupted */
925                 kfree(chan->txbuf);
926                 up(&chan->wsem);
927         }
928         chan->tx_status = 1;
929         wake_up_interruptible(&chan->txwaitq);
930         return 1;
931 }
932
933 static unsigned int cosa_poll(struct file *file, poll_table *poll)
934 {
935         printk(KERN_INFO "cosa_poll is here\n");
936         return 0;
937 }
938
939 static int cosa_open(struct inode *inode, struct file *file)
940 {
941         struct cosa_data *cosa;
942         struct channel_data *chan;
943         unsigned long flags;
944         int n;
945         int ret = 0;
946
947         lock_kernel();
948         if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
949                 >= nr_cards) {
950                 ret = -ENODEV;
951                 goto out;
952         }
953         cosa = cosa_cards+n;
954
955         if ((n=iminor(file->f_path.dentry->d_inode)
956                 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
957                 ret = -ENODEV;
958                 goto out;
959         }
960         chan = cosa->chan + n;
961         
962         file->private_data = chan;
963
964         spin_lock_irqsave(&cosa->lock, flags);
965
966         if (chan->usage < 0) { /* in netdev mode */
967                 spin_unlock_irqrestore(&cosa->lock, flags);
968                 ret = -EBUSY;
969                 goto out;
970         }
971         cosa->usage++;
972         chan->usage++;
973
974         chan->tx_done = chrdev_tx_done;
975         chan->setup_rx = chrdev_setup_rx;
976         chan->rx_done = chrdev_rx_done;
977         spin_unlock_irqrestore(&cosa->lock, flags);
978 out:
979         unlock_kernel();
980         return ret;
981 }
982
983 static int cosa_release(struct inode *inode, struct file *file)
984 {
985         struct channel_data *channel = file->private_data;
986         struct cosa_data *cosa;
987         unsigned long flags;
988
989         cosa = channel->cosa;
990         spin_lock_irqsave(&cosa->lock, flags);
991         cosa->usage--;
992         channel->usage--;
993         spin_unlock_irqrestore(&cosa->lock, flags);
994         return 0;
995 }
996
997 #ifdef COSA_FASYNC_WORKING
998 static struct fasync_struct *fasync[256] = { NULL, };
999
1000 /* To be done ... */
1001 static int cosa_fasync(struct inode *inode, struct file *file, int on)
1002 {
1003         int port = iminor(inode);
1004
1005         return fasync_helper(inode, file, on, &fasync[port]);
1006 }
1007 #endif
1008
1009 \f
1010 /* ---------- Ioctls ---------- */
1011
1012 /*
1013  * Ioctl subroutines can safely be made inline, because they are called
1014  * only from cosa_ioctl().
1015  */
1016 static inline int cosa_reset(struct cosa_data *cosa)
1017 {
1018         char idstring[COSA_MAX_ID_STRING];
1019         if (cosa->usage > 1)
1020                 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1021                         cosa->num, cosa->usage);
1022         cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1023         if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1024                 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1025                 return -EIO;
1026         }
1027         printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1028                 idstring);
1029         cosa->firmware_status |= COSA_FW_RESET;
1030         return 0;
1031 }
1032
1033 /* High-level function to download data into COSA memory. Calls download() */
1034 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1035 {
1036         struct cosa_download d;
1037         int i;
1038
1039         if (cosa->usage > 1)
1040                 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1041                         cosa->name, cosa->usage);
1042         if (!(cosa->firmware_status & COSA_FW_RESET)) {
1043                 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1044                         cosa->name, cosa->firmware_status);
1045                 return -EPERM;
1046         }
1047         
1048         if (copy_from_user(&d, arg, sizeof(d)))
1049                 return -EFAULT;
1050
1051         if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1052                 return -EINVAL;
1053         if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1054                 return -EINVAL;
1055
1056
1057         /* If something fails, force the user to reset the card */
1058         cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1059
1060         i = download(cosa, d.code, d.len, d.addr);
1061         if (i < 0) {
1062                 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1063                         cosa->num, i);
1064                 return -EIO;
1065         }
1066         printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1067                 cosa->num, d.len, d.addr);
1068         cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1069         return 0;
1070 }
1071
1072 /* High-level function to read COSA memory. Calls readmem() */
1073 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1074 {
1075         struct cosa_download d;
1076         int i;
1077
1078         if (cosa->usage > 1)
1079                 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1080                         "cosa->usage > 1 (%d). Odd things may happen.\n",
1081                         cosa->num, cosa->usage);
1082         if (!(cosa->firmware_status & COSA_FW_RESET)) {
1083                 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1084                         cosa->name, cosa->firmware_status);
1085                 return -EPERM;
1086         }
1087
1088         if (copy_from_user(&d, arg, sizeof(d)))
1089                 return -EFAULT;
1090
1091         /* If something fails, force the user to reset the card */
1092         cosa->firmware_status &= ~COSA_FW_RESET;
1093
1094         i = readmem(cosa, d.code, d.len, d.addr);
1095         if (i < 0) {
1096                 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1097                         cosa->num, i);
1098                 return -EIO;
1099         }
1100         printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1101                 cosa->num, d.len, d.addr);
1102         cosa->firmware_status |= COSA_FW_RESET;
1103         return 0;
1104 }
1105
1106 /* High-level function to start microcode. Calls startmicrocode(). */
1107 static inline int cosa_start(struct cosa_data *cosa, int address)
1108 {
1109         int i;
1110
1111         if (cosa->usage > 1)
1112                 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1113                         cosa->num, cosa->usage);
1114
1115         if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1116                 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1117                 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1118                         cosa->name, cosa->firmware_status);
1119                 return -EPERM;
1120         }
1121         cosa->firmware_status &= ~COSA_FW_RESET;
1122         if ((i=startmicrocode(cosa, address)) < 0) {
1123                 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1124                         cosa->num, address, i);
1125                 return -EIO;
1126         }
1127         printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1128                 cosa->num, address);
1129         cosa->startaddr = address;
1130         cosa->firmware_status |= COSA_FW_START;
1131         return 0;
1132 }
1133                 
1134 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1135 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1136 {
1137         int l = strlen(cosa->id_string)+1;
1138         if (copy_to_user(string, cosa->id_string, l))
1139                 return -EFAULT;
1140         return l;
1141 }
1142
1143 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1144 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1145 {
1146         int l = strlen(cosa->type)+1;
1147         if (copy_to_user(string, cosa->type, l))
1148                 return -EFAULT;
1149         return l;
1150 }
1151
1152 static int cosa_ioctl_common(struct cosa_data *cosa,
1153         struct channel_data *channel, unsigned int cmd, unsigned long arg)
1154 {
1155         void __user *argp = (void __user *)arg;
1156         switch(cmd) {
1157         case COSAIORSET:        /* Reset the device */
1158                 if (!capable(CAP_NET_ADMIN))
1159                         return -EACCES;
1160                 return cosa_reset(cosa);
1161         case COSAIOSTRT:        /* Start the firmware */
1162                 if (!capable(CAP_SYS_RAWIO))
1163                         return -EACCES;
1164                 return cosa_start(cosa, arg);
1165         case COSAIODOWNLD:      /* Download the firmware */
1166                 if (!capable(CAP_SYS_RAWIO))
1167                         return -EACCES;
1168                 
1169                 return cosa_download(cosa, argp);
1170         case COSAIORMEM:
1171                 if (!capable(CAP_SYS_RAWIO))
1172                         return -EACCES;
1173                 return cosa_readmem(cosa, argp);
1174         case COSAIORTYPE:
1175                 return cosa_gettype(cosa, argp);
1176         case COSAIORIDSTR:
1177                 return cosa_getidstr(cosa, argp);
1178         case COSAIONRCARDS:
1179                 return nr_cards;
1180         case COSAIONRCHANS:
1181                 return cosa->nchannels;
1182         case COSAIOBMSET:
1183                 if (!capable(CAP_SYS_RAWIO))
1184                         return -EACCES;
1185                 if (is_8bit(cosa))
1186                         return -EINVAL;
1187                 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1188                         return -EINVAL;
1189                 cosa->busmaster = arg;
1190                 return 0;
1191         case COSAIOBMGET:
1192                 return cosa->busmaster;
1193         }
1194         return -ENOIOCTLCMD;
1195 }
1196
1197 static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1198 {
1199         int rv;
1200         struct channel_data *chan = dev_to_chan(dev);
1201         rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1202                                (unsigned long)ifr->ifr_data);
1203         if (rv != -ENOIOCTLCMD)
1204                 return rv;
1205         return hdlc_ioctl(dev, ifr, cmd);
1206 }
1207
1208 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1209                                                         unsigned long arg)
1210 {
1211         struct channel_data *channel = file->private_data;
1212         struct cosa_data *cosa;
1213         long ret;
1214
1215         lock_kernel();
1216         cosa = channel->cosa;
1217         ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1218         unlock_kernel();
1219         return ret;
1220 }
1221
1222 \f
1223 /*---------- HW layer interface ---------- */
1224
1225 /*
1226  * The higher layer can bind itself to the HW layer by setting the callbacks
1227  * in the channel_data structure and by using these routines.
1228  */
1229 static void cosa_enable_rx(struct channel_data *chan)
1230 {
1231         struct cosa_data *cosa = chan->cosa;
1232
1233         if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1234                 put_driver_status(cosa);
1235 }
1236
1237 static void cosa_disable_rx(struct channel_data *chan)
1238 {
1239         struct cosa_data *cosa = chan->cosa;
1240
1241         if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1242                 put_driver_status(cosa);
1243 }
1244
1245 /*
1246  * FIXME: This routine probably should check for cosa_start_tx() called when
1247  * the previous transmit is still unfinished. In this case the non-zero
1248  * return value should indicate to the caller that the queuing(sp?) up
1249  * the transmit has failed.
1250  */
1251 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1252 {
1253         struct cosa_data *cosa = chan->cosa;
1254         unsigned long flags;
1255 #ifdef DEBUG_DATA
1256         int i;
1257
1258         printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1259                 chan->num, len);
1260         for (i=0; i<len; i++)
1261                 printk(" %02x", buf[i]&0xff);
1262         printk("\n");
1263 #endif
1264         spin_lock_irqsave(&cosa->lock, flags);
1265         chan->txbuf = buf;
1266         chan->txsize = len;
1267         if (len > COSA_MTU)
1268                 chan->txsize = COSA_MTU;
1269         spin_unlock_irqrestore(&cosa->lock, flags);
1270
1271         /* Tell the firmware we are ready */
1272         set_bit(chan->num, &cosa->txbitmap);
1273         put_driver_status(cosa);
1274
1275         return 0;
1276 }
1277
1278 static void put_driver_status(struct cosa_data *cosa)
1279 {
1280         unsigned long flags;
1281         int status;
1282
1283         spin_lock_irqsave(&cosa->lock, flags);
1284
1285         status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1286                 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1287                 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1288                         &DRIVER_TXMAP_MASK : 0);
1289         if (!cosa->rxtx) {
1290                 if (cosa->rxbitmap|cosa->txbitmap) {
1291                         if (!cosa->enabled) {
1292                                 cosa_putstatus(cosa, SR_RX_INT_ENA);
1293 #ifdef DEBUG_IO
1294                                 debug_status_out(cosa, SR_RX_INT_ENA);
1295 #endif
1296                                 cosa->enabled = 1;
1297                         }
1298                 } else if (cosa->enabled) {
1299                         cosa->enabled = 0;
1300                         cosa_putstatus(cosa, 0);
1301 #ifdef DEBUG_IO
1302                         debug_status_out(cosa, 0);
1303 #endif
1304                 }
1305                 cosa_putdata8(cosa, status);
1306 #ifdef DEBUG_IO
1307                 debug_data_cmd(cosa, status);
1308 #endif
1309         }
1310         spin_unlock_irqrestore(&cosa->lock, flags);
1311 }
1312
1313 static void put_driver_status_nolock(struct cosa_data *cosa)
1314 {
1315         int status;
1316
1317         status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1318                 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1319                 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1320                         &DRIVER_TXMAP_MASK : 0);
1321
1322         if (cosa->rxbitmap|cosa->txbitmap) {
1323                 cosa_putstatus(cosa, SR_RX_INT_ENA);
1324 #ifdef DEBUG_IO
1325                 debug_status_out(cosa, SR_RX_INT_ENA);
1326 #endif
1327                 cosa->enabled = 1;
1328         } else {
1329                 cosa_putstatus(cosa, 0);
1330 #ifdef DEBUG_IO
1331                 debug_status_out(cosa, 0);
1332 #endif
1333                 cosa->enabled = 0;
1334         }
1335         cosa_putdata8(cosa, status);
1336 #ifdef DEBUG_IO
1337         debug_data_cmd(cosa, status);
1338 #endif
1339 }
1340
1341 /*
1342  * The "kickme" function: When the DMA times out, this is called to
1343  * clean up the driver status.
1344  * FIXME: Preliminary support, the interface is probably wrong.
1345  */
1346 static void cosa_kick(struct cosa_data *cosa)
1347 {
1348         unsigned long flags, flags1;
1349         char *s = "(probably) IRQ";
1350
1351         if (test_bit(RXBIT, &cosa->rxtx))
1352                 s = "RX DMA";
1353         if (test_bit(TXBIT, &cosa->rxtx))
1354                 s = "TX DMA";
1355
1356         printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s); 
1357         spin_lock_irqsave(&cosa->lock, flags);
1358         cosa->rxtx = 0;
1359
1360         flags1 = claim_dma_lock();
1361         disable_dma(cosa->dma);
1362         clear_dma_ff(cosa->dma);
1363         release_dma_lock(flags1);
1364
1365         /* FIXME: Anything else? */
1366         udelay(100);
1367         cosa_putstatus(cosa, 0);
1368         udelay(100);
1369         (void) cosa_getdata8(cosa);
1370         udelay(100);
1371         cosa_putdata8(cosa, 0);
1372         udelay(100);
1373         put_driver_status_nolock(cosa);
1374         spin_unlock_irqrestore(&cosa->lock, flags);
1375 }
1376
1377 /*
1378  * Check if the whole buffer is DMA-able. It means it is below the 16M of
1379  * physical memory and doesn't span the 64k boundary. For now it seems
1380  * SKB's never do this, but we'll check this anyway.
1381  */
1382 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1383 {
1384         static int count;
1385         unsigned long b = (unsigned long)buf;
1386         if (b+len >= MAX_DMA_ADDRESS)
1387                 return 0;
1388         if ((b^ (b+len)) & 0x10000) {
1389                 if (count++ < 5)
1390                         printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1391                                 chan->name);
1392                 return 0;
1393         }
1394         return 1;
1395 }
1396
1397 \f
1398 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1399
1400 /*
1401  * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1402  * drivers need to say 4-digit hex number meaning start address of the microcode
1403  * separated by a single space. Monitor replies by saying " =". Now driver
1404  * has to write 4-digit hex number meaning the last byte address ended
1405  * by a single space. Monitor has to reply with a space. Now the download
1406  * begins. After the download monitor replies with "\r\n." (CR LF dot).
1407  */
1408 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1409 {
1410         int i;
1411
1412         if (put_wait_data(cosa, 'w') == -1) return -1;
1413         if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1414         if (get_wait_data(cosa) != '=') return -3;
1415
1416         if (puthexnumber(cosa, address) < 0) return -4;
1417         if (put_wait_data(cosa, ' ') == -1) return -10;
1418         if (get_wait_data(cosa) != ' ') return -11;
1419         if (get_wait_data(cosa) != '=') return -12;
1420
1421         if (puthexnumber(cosa, address+length-1) < 0) return -13;
1422         if (put_wait_data(cosa, ' ') == -1) return -18;
1423         if (get_wait_data(cosa) != ' ') return -19;
1424
1425         while (length--) {
1426                 char c;
1427 #ifndef SRP_DOWNLOAD_AT_BOOT
1428                 if (get_user(c, microcode))
1429                         return -23; /* ??? */
1430 #else
1431                 c = *microcode;
1432 #endif
1433                 if (put_wait_data(cosa, c) == -1)
1434                         return -20;
1435                 microcode++;
1436         }
1437
1438         if (get_wait_data(cosa) != '\r') return -21;
1439         if (get_wait_data(cosa) != '\n') return -22;
1440         if (get_wait_data(cosa) != '.') return -23;
1441 #if 0
1442         printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1443 #endif
1444         return 0;
1445 }
1446
1447
1448 /*
1449  * Starting microcode is done via the "g" command of the SRP monitor.
1450  * The chat should be the following: "g" "g=" "<addr><CR>"
1451  * "<CR><CR><LF><CR><LF>".
1452  */
1453 static int startmicrocode(struct cosa_data *cosa, int address)
1454 {
1455         if (put_wait_data(cosa, 'g') == -1) return -1;
1456         if (get_wait_data(cosa) != 'g') return -2;
1457         if (get_wait_data(cosa) != '=') return -3;
1458
1459         if (puthexnumber(cosa, address) < 0) return -4;
1460         if (put_wait_data(cosa, '\r') == -1) return -5;
1461         
1462         if (get_wait_data(cosa) != '\r') return -6;
1463         if (get_wait_data(cosa) != '\r') return -7;
1464         if (get_wait_data(cosa) != '\n') return -8;
1465         if (get_wait_data(cosa) != '\r') return -9;
1466         if (get_wait_data(cosa) != '\n') return -10;
1467 #if 0
1468         printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1469 #endif
1470         return 0;
1471 }
1472
1473 /*
1474  * Reading memory is done via the "r" command of the SRP monitor.
1475  * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1476  * Then driver can read the data and the conversation is finished
1477  * by SRP monitor sending "<CR><LF>." (dot at the end).
1478  *
1479  * This routine is not needed during the normal operation and serves
1480  * for debugging purposes only.
1481  */
1482 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1483 {
1484         if (put_wait_data(cosa, 'r') == -1) return -1;
1485         if ((get_wait_data(cosa)) != 'r') return -2;
1486         if ((get_wait_data(cosa)) != '=') return -3;
1487
1488         if (puthexnumber(cosa, address) < 0) return -4;
1489         if (put_wait_data(cosa, ' ') == -1) return -5;
1490         if (get_wait_data(cosa) != ' ') return -6;
1491         if (get_wait_data(cosa) != '=') return -7;
1492
1493         if (puthexnumber(cosa, address+length-1) < 0) return -8;
1494         if (put_wait_data(cosa, ' ') == -1) return -9;
1495         if (get_wait_data(cosa) != ' ') return -10;
1496
1497         while (length--) {
1498                 char c;
1499                 int i;
1500                 if ((i=get_wait_data(cosa)) == -1) {
1501                         printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1502                                 length);
1503                         return -11;
1504                 }
1505                 c=i;
1506 #if 1
1507                 if (put_user(c, microcode))
1508                         return -23; /* ??? */
1509 #else
1510                 *microcode = c;
1511 #endif
1512                 microcode++;
1513         }
1514
1515         if (get_wait_data(cosa) != '\r') return -21;
1516         if (get_wait_data(cosa) != '\n') return -22;
1517         if (get_wait_data(cosa) != '.') return -23;
1518 #if 0
1519         printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1520 #endif
1521         return 0;
1522 }
1523
1524 /*
1525  * This function resets the device and reads the initial prompt
1526  * of the device's ROM monitor.
1527  */
1528 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1529 {
1530         int i=0, id=0, prev=0, curr=0;
1531
1532         /* Reset the card ... */
1533         cosa_putstatus(cosa, 0);
1534         cosa_getdata8(cosa);
1535         cosa_putstatus(cosa, SR_RST);
1536 #ifdef MODULE
1537         msleep(500);
1538 #else
1539         udelay(5*100000);
1540 #endif
1541         /* Disable all IRQs from the card */
1542         cosa_putstatus(cosa, 0);
1543
1544         /*
1545          * Try to read the ID string. The card then prints out the
1546          * identification string ended by the "\n\x2e".
1547          *
1548          * The following loop is indexed through i (instead of id)
1549          * to avoid looping forever when for any reason
1550          * the port returns '\r', '\n' or '\x2e' permanently.
1551          */
1552         for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1553                 if ((curr = get_wait_data(cosa)) == -1) {
1554                         return -1;
1555                 }
1556                 curr &= 0xff;
1557                 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1558                         idstring[id++] = curr;
1559                 if (curr == 0x2e && prev == '\n')
1560                         break;
1561         }
1562         /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1563         idstring[id] = '\0';
1564         return id;
1565 }
1566
1567 \f
1568 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1569
1570 /*
1571  * This routine gets the data byte from the card waiting for the SR_RX_RDY
1572  * bit to be set in a loop. It should be used in the exceptional cases
1573  * only (for example when resetting the card or downloading the firmware.
1574  */
1575 static int get_wait_data(struct cosa_data *cosa)
1576 {
1577         int retries = 1000;
1578
1579         while (--retries) {
1580                 /* read data and return them */
1581                 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1582                         short r;
1583                         r = cosa_getdata8(cosa);
1584 #if 0
1585                         printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1586 #endif
1587                         return r;
1588                 }
1589                 /* sleep if not ready to read */
1590                 schedule_timeout_interruptible(1);
1591         }
1592         printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1593                 cosa_getstatus(cosa));
1594         return -1;
1595 }
1596
1597 /*
1598  * This routine puts the data byte to the card waiting for the SR_TX_RDY
1599  * bit to be set in a loop. It should be used in the exceptional cases
1600  * only (for example when resetting the card or downloading the firmware).
1601  */
1602 static int put_wait_data(struct cosa_data *cosa, int data)
1603 {
1604         int retries = 1000;
1605         while (--retries) {
1606                 /* read data and return them */
1607                 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1608                         cosa_putdata8(cosa, data);
1609 #if 0
1610                         printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1611 #endif
1612                         return 0;
1613                 }
1614 #if 0
1615                 /* sleep if not ready to read */
1616                 schedule_timeout_interruptible(1);
1617 #endif
1618         }
1619         printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1620                 cosa->num, cosa_getstatus(cosa));
1621         return -1;
1622 }
1623         
1624 /* 
1625  * The following routine puts the hexadecimal number into the SRP monitor
1626  * and verifies the proper echo of the sent bytes. Returns 0 on success,
1627  * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1628  * (-2,-4,-6,-8) means that reading echo failed.
1629  */
1630 static int puthexnumber(struct cosa_data *cosa, int number)
1631 {
1632         char temp[5];
1633         int i;
1634
1635         /* Well, I should probably replace this by something faster. */
1636         sprintf(temp, "%04X", number);
1637         for (i=0; i<4; i++) {
1638                 if (put_wait_data(cosa, temp[i]) == -1) {
1639                         printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1640                                 cosa->num, i);
1641                         return -1-2*i;
1642                 }
1643                 if (get_wait_data(cosa) != temp[i]) {
1644                         printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1645                                 cosa->num, i);
1646                         return -2-2*i;
1647                 }
1648         }
1649         return 0;
1650 }
1651
1652 \f
1653 /* ---------- Interrupt routines ---------- */
1654
1655 /*
1656  * There are three types of interrupt:
1657  * At the beginning of transmit - this handled is in tx_interrupt(),
1658  * at the beginning of receive - it is in rx_interrupt() and
1659  * at the end of transmit/receive - it is the eot_interrupt() function.
1660  * These functions are multiplexed by cosa_interrupt() according to the
1661  * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1662  * separate functions to make it more readable. These functions are inline,
1663  * so there should be no overhead of function call.
1664  * 
1665  * In the COSA bus-master mode, we need to tell the card the address of a
1666  * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1667  * It's time to use the bottom half :-(
1668  */
1669
1670 /*
1671  * Transmit interrupt routine - called when COSA is willing to obtain
1672  * data from the OS. The most tricky part of the routine is selection
1673  * of channel we (OS) want to send packet for. For SRP we should probably
1674  * use the round-robin approach. The newer COSA firmwares have a simple
1675  * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1676  * channel 0 or 1 doesn't want to receive data.
1677  *
1678  * It seems there is a bug in COSA firmware (need to trace it further):
1679  * When the driver status says that the kernel has no more data for transmit
1680  * (e.g. at the end of TX DMA) and then the kernel changes its mind
1681  * (e.g. new packet is queued to hard_start_xmit()), the card issues
1682  * the TX interrupt but does not mark the channel as ready-to-transmit.
1683  * The fix seems to be to push the packet to COSA despite its request.
1684  * We first try to obey the card's opinion, and then fall back to forced TX.
1685  */
1686 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1687 {
1688         unsigned long flags, flags1;
1689 #ifdef DEBUG_IRQS
1690         printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1691                 cosa->num, status);
1692 #endif
1693         spin_lock_irqsave(&cosa->lock, flags);
1694         set_bit(TXBIT, &cosa->rxtx);
1695         if (!test_bit(IRQBIT, &cosa->rxtx)) {
1696                 /* flow control, see the comment above */
1697                 int i=0;
1698                 if (!cosa->txbitmap) {
1699                         printk(KERN_WARNING "%s: No channel wants data "
1700                                 "in TX IRQ. Expect DMA timeout.",
1701                                 cosa->name);
1702                         put_driver_status_nolock(cosa);
1703                         clear_bit(TXBIT, &cosa->rxtx);
1704                         spin_unlock_irqrestore(&cosa->lock, flags);
1705                         return;
1706                 }
1707                 while(1) {
1708                         cosa->txchan++;
1709                         i++;
1710                         if (cosa->txchan >= cosa->nchannels)
1711                                 cosa->txchan = 0;
1712                         if (!(cosa->txbitmap & (1<<cosa->txchan)))
1713                                 continue;
1714                         if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1715                                 break;
1716                         /* in second pass, accept first ready-to-TX channel */
1717                         if (i > cosa->nchannels) {
1718                                 /* Can be safely ignored */
1719 #ifdef DEBUG_IRQS
1720                                 printk(KERN_DEBUG "%s: Forcing TX "
1721                                         "to not-ready channel %d\n",
1722                                         cosa->name, cosa->txchan);
1723 #endif
1724                                 break;
1725                         }
1726                 }
1727
1728                 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1729                 if (cosa_dma_able(cosa->chan+cosa->txchan,
1730                         cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1731                         cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1732                 } else {
1733                         memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1734                                 cosa->txsize);
1735                         cosa->txbuf = cosa->bouncebuf;
1736                 }
1737         }
1738
1739         if (is_8bit(cosa)) {
1740                 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1741                         cosa_putstatus(cosa, SR_TX_INT_ENA);
1742                         cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1743                                 ((cosa->txsize >> 8) & 0x1f));
1744 #ifdef DEBUG_IO
1745                         debug_status_out(cosa, SR_TX_INT_ENA);
1746                         debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1747                                 ((cosa->txsize >> 8) & 0x1f));
1748                         debug_data_in(cosa, cosa_getdata8(cosa));
1749 #else
1750                         cosa_getdata8(cosa);
1751 #endif
1752                         set_bit(IRQBIT, &cosa->rxtx);
1753                         spin_unlock_irqrestore(&cosa->lock, flags);
1754                         return;
1755                 } else {
1756                         clear_bit(IRQBIT, &cosa->rxtx);
1757                         cosa_putstatus(cosa, 0);
1758                         cosa_putdata8(cosa, cosa->txsize&0xff);
1759 #ifdef DEBUG_IO
1760                         debug_status_out(cosa, 0);
1761                         debug_data_out(cosa, cosa->txsize&0xff);
1762 #endif
1763                 }
1764         } else {
1765                 cosa_putstatus(cosa, SR_TX_INT_ENA);
1766                 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1767                         | (cosa->txsize & 0x1fff));
1768 #ifdef DEBUG_IO
1769                 debug_status_out(cosa, SR_TX_INT_ENA);
1770                 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1771                         | (cosa->txsize & 0x1fff));
1772                 debug_data_in(cosa, cosa_getdata8(cosa));
1773                 debug_status_out(cosa, 0);
1774 #else
1775                 cosa_getdata8(cosa);
1776 #endif
1777                 cosa_putstatus(cosa, 0);
1778         }
1779
1780         if (cosa->busmaster) {
1781                 unsigned long addr = virt_to_bus(cosa->txbuf);
1782                 int count=0;
1783                 printk(KERN_INFO "busmaster IRQ\n");
1784                 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1785                         count++;
1786                         udelay(10);
1787                         if (count > 1000) break;
1788                 }
1789                 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1790                 printk(KERN_INFO "ready after %d loops\n", count);
1791                 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1792
1793                 count = 0;
1794                 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1795                         count++;
1796                         if (count > 1000) break;
1797                         udelay(10);
1798                 }
1799                 printk(KERN_INFO "ready after %d loops\n", count);
1800                 cosa_putdata16(cosa, addr &0xffff);
1801                 flags1 = claim_dma_lock();
1802                 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1803                 enable_dma(cosa->dma);
1804                 release_dma_lock(flags1);
1805         } else {
1806                 /* start the DMA */
1807                 flags1 = claim_dma_lock();
1808                 disable_dma(cosa->dma);
1809                 clear_dma_ff(cosa->dma);
1810                 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1811                 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1812                 set_dma_count(cosa->dma, cosa->txsize);
1813                 enable_dma(cosa->dma);
1814                 release_dma_lock(flags1);
1815         }
1816         cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1817 #ifdef DEBUG_IO
1818         debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1819 #endif
1820         spin_unlock_irqrestore(&cosa->lock, flags);
1821 }
1822
1823 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1824 {
1825         unsigned long flags;
1826 #ifdef DEBUG_IRQS
1827         printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1828 #endif
1829
1830         spin_lock_irqsave(&cosa->lock, flags);
1831         set_bit(RXBIT, &cosa->rxtx);
1832
1833         if (is_8bit(cosa)) {
1834                 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1835                         set_bit(IRQBIT, &cosa->rxtx);
1836                         put_driver_status_nolock(cosa);
1837                         cosa->rxsize = cosa_getdata8(cosa) <<8;
1838 #ifdef DEBUG_IO
1839                         debug_data_in(cosa, cosa->rxsize >> 8);
1840 #endif
1841                         spin_unlock_irqrestore(&cosa->lock, flags);
1842                         return;
1843                 } else {
1844                         clear_bit(IRQBIT, &cosa->rxtx);
1845                         cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1846 #ifdef DEBUG_IO
1847                         debug_data_in(cosa, cosa->rxsize & 0xff);
1848 #endif
1849 #if 0
1850                         printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1851                                 cosa->num, cosa->rxsize);
1852 #endif
1853                 }
1854         } else {
1855                 cosa->rxsize = cosa_getdata16(cosa);
1856 #ifdef DEBUG_IO
1857                 debug_data_in(cosa, cosa->rxsize);
1858 #endif
1859 #if 0
1860                 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1861                         cosa->num, cosa->rxsize);
1862 #endif
1863         }
1864         if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1865                 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1866                         cosa->name, cosa->rxsize);
1867                 spin_unlock_irqrestore(&cosa->lock, flags);
1868                 goto reject;
1869         }
1870         cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1871         cosa->rxsize &= 0x1fff;
1872         spin_unlock_irqrestore(&cosa->lock, flags);
1873
1874         cosa->rxbuf = NULL;
1875         if (cosa->rxchan->setup_rx)
1876                 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1877
1878         if (!cosa->rxbuf) {
1879 reject:         /* Reject the packet */
1880                 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1881                         cosa->num, cosa->rxchan->num);
1882                 cosa->rxbuf = cosa->bouncebuf;
1883         }
1884
1885         /* start the DMA */
1886         flags = claim_dma_lock();
1887         disable_dma(cosa->dma);
1888         clear_dma_ff(cosa->dma);
1889         set_dma_mode(cosa->dma, DMA_MODE_READ);
1890         if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1891                 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1892         } else {
1893                 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1894         }
1895         set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1896         enable_dma(cosa->dma);
1897         release_dma_lock(flags);
1898         spin_lock_irqsave(&cosa->lock, flags);
1899         cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1900         if (!is_8bit(cosa) && (status & SR_TX_RDY))
1901                 cosa_putdata8(cosa, DRIVER_RX_READY);
1902 #ifdef DEBUG_IO
1903         debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1904         if (!is_8bit(cosa) && (status & SR_TX_RDY))
1905                 debug_data_cmd(cosa, DRIVER_RX_READY);
1906 #endif
1907         spin_unlock_irqrestore(&cosa->lock, flags);
1908 }
1909
1910 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1911 {
1912         unsigned long flags, flags1;
1913         spin_lock_irqsave(&cosa->lock, flags);
1914         flags1 = claim_dma_lock();
1915         disable_dma(cosa->dma);
1916         clear_dma_ff(cosa->dma);
1917         release_dma_lock(flags1);
1918         if (test_bit(TXBIT, &cosa->rxtx)) {
1919                 struct channel_data *chan = cosa->chan+cosa->txchan;
1920                 if (chan->tx_done)
1921                         if (chan->tx_done(chan, cosa->txsize))
1922                                 clear_bit(chan->num, &cosa->txbitmap);
1923         } else if (test_bit(RXBIT, &cosa->rxtx)) {
1924 #ifdef DEBUG_DATA
1925         {
1926                 int i;
1927                 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num, 
1928                         cosa->rxchan->num, cosa->rxsize);
1929                 for (i=0; i<cosa->rxsize; i++)
1930                         printk (" %02x", cosa->rxbuf[i]&0xff);
1931                 printk("\n");
1932         }
1933 #endif
1934                 /* Packet for unknown channel? */
1935                 if (cosa->rxbuf == cosa->bouncebuf)
1936                         goto out;
1937                 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1938                         memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1939                 if (cosa->rxchan->rx_done)
1940                         if (cosa->rxchan->rx_done(cosa->rxchan))
1941                                 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1942         } else {
1943                 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1944                         cosa->num);
1945         }
1946         /*
1947          * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1948          * cleared anyway). We should do it as soon as possible
1949          * so that we can tell the COSA we are done and to give it a time
1950          * for recovery.
1951          */
1952 out:
1953         cosa->rxtx = 0;
1954         put_driver_status_nolock(cosa);
1955         spin_unlock_irqrestore(&cosa->lock, flags);
1956 }
1957
1958 static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1959 {
1960         unsigned status;
1961         int count = 0;
1962         struct cosa_data *cosa = cosa_;
1963 again:
1964         status = cosa_getstatus(cosa);
1965 #ifdef DEBUG_IRQS
1966         printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1967                 status & 0xff);
1968 #endif
1969 #ifdef DEBUG_IO
1970         debug_status_in(cosa, status);
1971 #endif
1972         switch (status & SR_CMD_FROM_SRP_MASK) {
1973         case SR_DOWN_REQUEST:
1974                 tx_interrupt(cosa, status);
1975                 break;
1976         case SR_UP_REQUEST:
1977                 rx_interrupt(cosa, status);
1978                 break;
1979         case SR_END_OF_TRANSFER:
1980                 eot_interrupt(cosa, status);
1981                 break;
1982         default:
1983                 /* We may be too fast for SRP. Try to wait a bit more. */
1984                 if (count++ < 100) {
1985                         udelay(100);
1986                         goto again;
1987                 }
1988                 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1989                         cosa->num, status & 0xff, count);
1990         }
1991 #ifdef DEBUG_IRQS
1992         if (count)
1993                 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1994                         cosa->name, count);
1995         else
1996                 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
1997 #endif
1998         return IRQ_HANDLED;
1999 }
2000
2001 \f
2002 /* ---------- I/O debugging routines ---------- */
2003 /*
2004  * These routines can be used to monitor COSA/SRP I/O and to printk()
2005  * the data being transferred on the data and status I/O port in a
2006  * readable way.
2007  */
2008
2009 #ifdef DEBUG_IO
2010 static void debug_status_in(struct cosa_data *cosa, int status)
2011 {
2012         char *s;
2013         switch(status & SR_CMD_FROM_SRP_MASK) {
2014         case SR_UP_REQUEST:
2015                 s = "RX_REQ";
2016                 break;
2017         case SR_DOWN_REQUEST:
2018                 s = "TX_REQ";
2019                 break;
2020         case SR_END_OF_TRANSFER:
2021                 s = "ET_REQ";
2022                 break;
2023         default:
2024                 s = "NO_REQ";
2025                 break;
2026         }
2027         printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2028                 cosa->name,
2029                 status,
2030                 status & SR_USR_RQ ? "USR_RQ|":"",
2031                 status & SR_TX_RDY ? "TX_RDY|":"",
2032                 status & SR_RX_RDY ? "RX_RDY|":"",
2033                 s);
2034 }
2035
2036 static void debug_status_out(struct cosa_data *cosa, int status)
2037 {
2038         printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2039                 cosa->name,
2040                 status,
2041                 status & SR_RX_DMA_ENA  ? "RXDMA|":"!rxdma|",
2042                 status & SR_TX_DMA_ENA  ? "TXDMA|":"!txdma|",
2043                 status & SR_RST         ? "RESET|":"",
2044                 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2045                 status & SR_TX_INT_ENA  ? "TXINT|":"!txint|",
2046                 status & SR_RX_INT_ENA  ? "RXINT":"!rxint");
2047 }
2048
2049 static void debug_data_in(struct cosa_data *cosa, int data)
2050 {
2051         printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2052 }
2053
2054 static void debug_data_out(struct cosa_data *cosa, int data)
2055 {
2056         printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2057 }
2058
2059 static void debug_data_cmd(struct cosa_data *cosa, int data)
2060 {
2061         printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2062                 cosa->name, data,
2063                 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2064                 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2065 }
2066 #endif
2067
2068 /* EOF -- this file has not been truncated */