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