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