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