[PATCH] devfs: Remove devfs_mk_dir() function from the kernel tree
[pandora-kernel.git] / drivers / net / wan / cosa.c
1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
2
3 /*
4  *  Copyright (C) 1995-1997  Jan "Yenya" Kasprzak <kas@fi.muni.cz>
5  *
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                 err = devfs_mk_cdev(MKDEV(cosa_major, i),
405                                 S_IFCHR|S_IRUSR|S_IWUSR,
406                                 "cosa/%d", i);
407                 if (err) {
408                         class_device_destroy(cosa_class, MKDEV(cosa_major, i));
409                         goto out_chrdev;                
410                 }
411         }
412         err = 0;
413         goto out;
414         
415 out_chrdev:
416         unregister_chrdev(cosa_major, "cosa");
417 out:
418         return err;
419 }
420 module_init(cosa_init);
421
422 static void __exit cosa_exit(void)
423 {
424         struct cosa_data *cosa;
425         int i;
426         printk(KERN_INFO "Unloading the cosa module\n");
427
428         for (i=0; i<nr_cards; i++) {
429                 class_device_destroy(cosa_class, MKDEV(cosa_major, i));
430                 devfs_remove("cosa/%d", i);
431         }
432         class_destroy(cosa_class);
433         devfs_remove("cosa");
434         for (cosa=cosa_cards; nr_cards--; cosa++) {
435                 /* Clean up the per-channel data */
436                 for (i=0; i<cosa->nchannels; i++) {
437                         /* Chardev driver has no alloc'd per-channel data */
438                         sppp_channel_delete(cosa->chan+i);
439                 }
440                 /* Clean up the per-card data */
441                 kfree(cosa->chan);
442                 kfree(cosa->bouncebuf);
443                 free_irq(cosa->irq, cosa);
444                 free_dma(cosa->dma);
445                 release_region(cosa->datareg,is_8bit(cosa)?2:4);
446         }
447         unregister_chrdev(cosa_major, "cosa");
448 }
449 module_exit(cosa_exit);
450
451 /*
452  * This function should register all the net devices needed for the
453  * single channel.
454  */
455 static __inline__ void channel_init(struct channel_data *chan)
456 {
457         sprintf(chan->name, "cosa%dc%d", chan->cosa->num, chan->num);
458
459         /* Initialize the chardev data structures */
460         chardev_channel_init(chan);
461
462         /* Register the sppp interface */
463         sppp_channel_init(chan);
464 }
465         
466 static int cosa_probe(int base, int irq, int dma)
467 {
468         struct cosa_data *cosa = cosa_cards+nr_cards;
469         int i, err = 0;
470
471         memset(cosa, 0, sizeof(struct cosa_data));
472
473         /* Checking validity of parameters: */
474         /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
475         if ((irq >= 0  && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
476                 printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
477                 return -1;
478         }
479         /* I/O address should be between 0x100 and 0x3ff and should be
480          * multiple of 8. */
481         if (base < 0x100 || base > 0x3ff || base & 0x7) {
482                 printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
483                         base);
484                 return -1;
485         }
486         /* DMA should be 0,1 or 3-7 */
487         if (dma < 0 || dma == 4 || dma > 7) {
488                 printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
489                 return -1;
490         }
491         /* and finally, on 16-bit COSA DMA should be 4-7 and 
492          * I/O base should not be multiple of 0x10 */
493         if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
494                 printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
495                         " (base=0x%x, dma=%d)\n", base, dma);
496                 return -1;
497         }
498
499         cosa->dma = dma;
500         cosa->datareg = base;
501         cosa->statusreg = is_8bit(cosa)?base+1:base+2;
502         spin_lock_init(&cosa->lock);
503
504         if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
505                 return -1;
506         
507         if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
508                 printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
509                 err = -1;
510                 goto err_out;
511         }
512
513         /* Test the validity of identification string */
514         if (!strncmp(cosa->id_string, "SRP", 3))
515                 cosa->type = "srp";
516         else if (!strncmp(cosa->id_string, "COSA", 4))
517                 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
518         else {
519 /* Print a warning only if we are not autoprobing */
520 #ifndef COSA_ISA_AUTOPROBE
521                 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
522                         base);
523 #endif
524                 err = -1;
525                 goto err_out;
526         }
527         /* Update the name of the region now we know the type of card */ 
528         release_region(base, is_8bit(cosa)?2:4);
529         if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
530                 printk(KERN_DEBUG "cosa: changing name at 0x%x failed.\n", base);
531                 return -1;
532         }
533
534         /* Now do IRQ autoprobe */
535         if (irq < 0) {
536                 unsigned long irqs;
537 /*              printk(KERN_INFO "IRQ autoprobe\n"); */
538                 irqs = probe_irq_on();
539                 /* 
540                  * Enable interrupt on tx buffer empty (it sure is) 
541                  * really sure ?
542                  * FIXME: When this code is not used as module, we should
543                  * probably call udelay() instead of the interruptible sleep.
544                  */
545                 set_current_state(TASK_INTERRUPTIBLE);
546                 cosa_putstatus(cosa, SR_TX_INT_ENA);
547                 schedule_timeout(30);
548                 irq = probe_irq_off(irqs);
549                 /* Disable all IRQs from the card */
550                 cosa_putstatus(cosa, 0);
551                 /* Empty the received data register */
552                 cosa_getdata8(cosa);
553
554                 if (irq < 0) {
555                         printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
556                                 irq, cosa->datareg);
557                         err = -1;
558                         goto err_out;
559                 }
560                 if (irq == 0) {
561                         printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
562                                 cosa->datareg);
563                 /*      return -1; */
564                 }
565         }
566
567         cosa->irq = irq;
568         cosa->num = nr_cards;
569         cosa->usage = 0;
570         cosa->nchannels = 2;    /* FIXME: how to determine this? */
571
572         if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
573                 err = -1;
574                 goto err_out;
575         }
576         if (request_dma(cosa->dma, cosa->type)) {
577                 err = -1;
578                 goto err_out1;
579         }
580         
581         cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
582         if (!cosa->bouncebuf) {
583                 err = -ENOMEM;
584                 goto err_out2;
585         }
586         sprintf(cosa->name, "cosa%d", cosa->num);
587
588         /* Initialize the per-channel data */
589         cosa->chan = kmalloc(sizeof(struct channel_data)*cosa->nchannels,
590                              GFP_KERNEL);
591         if (!cosa->chan) {
592                 err = -ENOMEM;
593                 goto err_out3;
594         }
595         memset(cosa->chan, 0, sizeof(struct channel_data)*cosa->nchannels);
596         for (i=0; i<cosa->nchannels; i++) {
597                 cosa->chan[i].cosa = cosa;
598                 cosa->chan[i].num = i;
599                 channel_init(cosa->chan+i);
600         }
601
602         printk (KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
603                 cosa->num, cosa->id_string, cosa->type,
604                 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
605
606         return nr_cards++;
607 err_out3:
608         kfree(cosa->bouncebuf);
609 err_out2:
610         free_dma(cosa->dma);
611 err_out1:
612         free_irq(cosa->irq, cosa);
613 err_out:        
614         release_region(cosa->datareg,is_8bit(cosa)?2:4);
615         printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
616                cosa->num);
617         return err;
618 }
619
620 \f
621 /*---------- SPPP/HDLC netdevice ---------- */
622
623 static void cosa_setup(struct net_device *d)
624 {
625         d->open = cosa_sppp_open;
626         d->stop = cosa_sppp_close;
627         d->hard_start_xmit = cosa_sppp_tx;
628         d->do_ioctl = cosa_sppp_ioctl;
629         d->get_stats = cosa_net_stats;
630         d->tx_timeout = cosa_sppp_timeout;
631         d->watchdog_timeo = TX_TIMEOUT;
632 }
633
634 static void sppp_channel_init(struct channel_data *chan)
635 {
636         struct net_device *d;
637         chan->if_ptr = &chan->pppdev;
638         d = alloc_netdev(0, chan->name, cosa_setup);
639         if (!d) {
640                 printk(KERN_WARNING "%s: alloc_netdev failed.\n", chan->name);
641                 return;
642         }
643         chan->pppdev.dev = d;
644         d->base_addr = chan->cosa->datareg;
645         d->irq = chan->cosa->irq;
646         d->dma = chan->cosa->dma;
647         d->priv = chan;
648         sppp_attach(&chan->pppdev);
649         if (register_netdev(d)) {
650                 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
651                 sppp_detach(d);
652                 free_netdev(d);
653                 chan->pppdev.dev = NULL;
654                 return;
655         }
656 }
657
658 static void sppp_channel_delete(struct channel_data *chan)
659 {
660         unregister_netdev(chan->pppdev.dev);
661         sppp_detach(chan->pppdev.dev);
662         free_netdev(chan->pppdev.dev);
663         chan->pppdev.dev = NULL;
664 }
665
666 static int cosa_sppp_open(struct net_device *d)
667 {
668         struct channel_data *chan = d->priv;
669         int err;
670         unsigned long flags;
671
672         if (!(chan->cosa->firmware_status & COSA_FW_START)) {
673                 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
674                         chan->cosa->name, chan->cosa->firmware_status);
675                 return -EPERM;
676         }
677         spin_lock_irqsave(&chan->cosa->lock, flags);
678         if (chan->usage != 0) {
679                 printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
680                         chan->name, chan->usage);
681                 spin_unlock_irqrestore(&chan->cosa->lock, flags);
682                 return -EBUSY;
683         }
684         chan->setup_rx = sppp_setup_rx;
685         chan->tx_done = sppp_tx_done;
686         chan->rx_done = sppp_rx_done;
687         chan->usage=-1;
688         chan->cosa->usage++;
689         spin_unlock_irqrestore(&chan->cosa->lock, flags);
690
691         err = sppp_open(d);
692         if (err) {
693                 spin_lock_irqsave(&chan->cosa->lock, flags);
694                 chan->usage=0;
695                 chan->cosa->usage--;
696                 
697                 spin_unlock_irqrestore(&chan->cosa->lock, flags);
698                 return err;
699         }
700
701         netif_start_queue(d);
702         cosa_enable_rx(chan);
703         return 0;
704 }
705
706 static int cosa_sppp_tx(struct sk_buff *skb, struct net_device *dev)
707 {
708         struct channel_data *chan = dev->priv;
709
710         netif_stop_queue(dev);
711
712         chan->tx_skb = skb;
713         cosa_start_tx(chan, skb->data, skb->len);
714         return 0;
715 }
716
717 static void cosa_sppp_timeout(struct net_device *dev)
718 {
719         struct channel_data *chan = dev->priv;
720
721         if (test_bit(RXBIT, &chan->cosa->rxtx)) {
722                 chan->stats.rx_errors++;
723                 chan->stats.rx_missed_errors++;
724         } else {
725                 chan->stats.tx_errors++;
726                 chan->stats.tx_aborted_errors++;
727         }
728         cosa_kick(chan->cosa);
729         if (chan->tx_skb) {
730                 dev_kfree_skb(chan->tx_skb);
731                 chan->tx_skb = NULL;
732         }
733         netif_wake_queue(dev);
734 }
735
736 static int cosa_sppp_close(struct net_device *d)
737 {
738         struct channel_data *chan = d->priv;
739         unsigned long flags;
740
741         netif_stop_queue(d);
742         sppp_close(d);
743         cosa_disable_rx(chan);
744         spin_lock_irqsave(&chan->cosa->lock, flags);
745         if (chan->rx_skb) {
746                 kfree_skb(chan->rx_skb);
747                 chan->rx_skb = NULL;
748         }
749         if (chan->tx_skb) {
750                 kfree_skb(chan->tx_skb);
751                 chan->tx_skb = NULL;
752         }
753         chan->usage=0;
754         chan->cosa->usage--;
755         spin_unlock_irqrestore(&chan->cosa->lock, flags);
756         return 0;
757 }
758
759 static char *sppp_setup_rx(struct channel_data *chan, int size)
760 {
761         /*
762          * We can safely fall back to non-dma-able memory, because we have
763          * the cosa->bouncebuf pre-allocated.
764          */
765         if (chan->rx_skb)
766                 kfree_skb(chan->rx_skb);
767         chan->rx_skb = dev_alloc_skb(size);
768         if (chan->rx_skb == NULL) {
769                 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
770                         chan->name);
771                 chan->stats.rx_dropped++;
772                 return NULL;
773         }
774         chan->pppdev.dev->trans_start = jiffies;
775         return skb_put(chan->rx_skb, size);
776 }
777
778 static int sppp_rx_done(struct channel_data *chan)
779 {
780         if (!chan->rx_skb) {
781                 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
782                         chan->name);
783                 chan->stats.rx_errors++;
784                 chan->stats.rx_frame_errors++;
785                 return 0;
786         }
787         chan->rx_skb->protocol = htons(ETH_P_WAN_PPP);
788         chan->rx_skb->dev = chan->pppdev.dev;
789         chan->rx_skb->mac.raw = chan->rx_skb->data;
790         chan->stats.rx_packets++;
791         chan->stats.rx_bytes += chan->cosa->rxsize;
792         netif_rx(chan->rx_skb);
793         chan->rx_skb = NULL;
794         chan->pppdev.dev->last_rx = jiffies;
795         return 0;
796 }
797
798 /* ARGSUSED */
799 static int sppp_tx_done(struct channel_data *chan, int size)
800 {
801         if (!chan->tx_skb) {
802                 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
803                         chan->name);
804                 chan->stats.tx_errors++;
805                 chan->stats.tx_aborted_errors++;
806                 return 1;
807         }
808         dev_kfree_skb_irq(chan->tx_skb);
809         chan->tx_skb = NULL;
810         chan->stats.tx_packets++;
811         chan->stats.tx_bytes += size;
812         netif_wake_queue(chan->pppdev.dev);
813         return 1;
814 }
815
816 static struct net_device_stats *cosa_net_stats(struct net_device *dev)
817 {
818         struct channel_data *chan = dev->priv;
819         return &chan->stats;
820 }
821
822 \f
823 /*---------- Character device ---------- */
824
825 static void chardev_channel_init(struct channel_data *chan)
826 {
827         init_MUTEX(&chan->rsem);
828         init_MUTEX(&chan->wsem);
829 }
830
831 static ssize_t cosa_read(struct file *file,
832         char __user *buf, size_t count, loff_t *ppos)
833 {
834         DECLARE_WAITQUEUE(wait, current);
835         unsigned long flags;
836         struct channel_data *chan = file->private_data;
837         struct cosa_data *cosa = chan->cosa;
838         char *kbuf;
839
840         if (!(cosa->firmware_status & COSA_FW_START)) {
841                 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
842                         cosa->name, cosa->firmware_status);
843                 return -EPERM;
844         }
845         if (down_interruptible(&chan->rsem))
846                 return -ERESTARTSYS;
847         
848         if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
849                 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
850                 up(&chan->rsem);
851                 return -ENOMEM;
852         }
853
854         chan->rx_status = 0;
855         cosa_enable_rx(chan);
856         spin_lock_irqsave(&cosa->lock, flags);
857         add_wait_queue(&chan->rxwaitq, &wait);
858         while(!chan->rx_status) {
859                 current->state = TASK_INTERRUPTIBLE;
860                 spin_unlock_irqrestore(&cosa->lock, flags);
861                 schedule();
862                 spin_lock_irqsave(&cosa->lock, flags);
863                 if (signal_pending(current) && chan->rx_status == 0) {
864                         chan->rx_status = 1;
865                         remove_wait_queue(&chan->rxwaitq, &wait);
866                         current->state = TASK_RUNNING;
867                         spin_unlock_irqrestore(&cosa->lock, flags);
868                         up(&chan->rsem);
869                         return -ERESTARTSYS;
870                 }
871         }
872         remove_wait_queue(&chan->rxwaitq, &wait);
873         current->state = TASK_RUNNING;
874         kbuf = chan->rxdata;
875         count = chan->rxsize;
876         spin_unlock_irqrestore(&cosa->lock, flags);
877         up(&chan->rsem);
878
879         if (copy_to_user(buf, kbuf, count)) {
880                 kfree(kbuf);
881                 return -EFAULT;
882         }
883         kfree(kbuf);
884         return count;
885 }
886
887 static char *chrdev_setup_rx(struct channel_data *chan, int size)
888 {
889         /* Expect size <= COSA_MTU */
890         chan->rxsize = size;
891         return chan->rxdata;
892 }
893
894 static int chrdev_rx_done(struct channel_data *chan)
895 {
896         if (chan->rx_status) { /* Reader has died */
897                 kfree(chan->rxdata);
898                 up(&chan->wsem);
899         }
900         chan->rx_status = 1;
901         wake_up_interruptible(&chan->rxwaitq);
902         return 1;
903 }
904
905
906 static ssize_t cosa_write(struct file *file,
907         const char __user *buf, size_t count, loff_t *ppos)
908 {
909         DECLARE_WAITQUEUE(wait, current);
910         struct channel_data *chan = file->private_data;
911         struct cosa_data *cosa = chan->cosa;
912         unsigned long flags;
913         char *kbuf;
914
915         if (!(cosa->firmware_status & COSA_FW_START)) {
916                 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
917                         cosa->name, cosa->firmware_status);
918                 return -EPERM;
919         }
920         if (down_interruptible(&chan->wsem))
921                 return -ERESTARTSYS;
922
923         if (count > COSA_MTU)
924                 count = COSA_MTU;
925         
926         /* Allocate the buffer */
927         if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
928                 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
929                         cosa->name);
930                 up(&chan->wsem);
931                 return -ENOMEM;
932         }
933         if (copy_from_user(kbuf, buf, count)) {
934                 up(&chan->wsem);
935                 kfree(kbuf);
936                 return -EFAULT;
937         }
938         chan->tx_status=0;
939         cosa_start_tx(chan, kbuf, count);
940
941         spin_lock_irqsave(&cosa->lock, flags);
942         add_wait_queue(&chan->txwaitq, &wait);
943         while(!chan->tx_status) {
944                 current->state = TASK_INTERRUPTIBLE;
945                 spin_unlock_irqrestore(&cosa->lock, flags);
946                 schedule();
947                 spin_lock_irqsave(&cosa->lock, flags);
948                 if (signal_pending(current) && chan->tx_status == 0) {
949                         chan->tx_status = 1;
950                         remove_wait_queue(&chan->txwaitq, &wait);
951                         current->state = TASK_RUNNING;
952                         chan->tx_status = 1;
953                         spin_unlock_irqrestore(&cosa->lock, flags);
954                         return -ERESTARTSYS;
955                 }
956         }
957         remove_wait_queue(&chan->txwaitq, &wait);
958         current->state = TASK_RUNNING;
959         up(&chan->wsem);
960         spin_unlock_irqrestore(&cosa->lock, flags);
961         kfree(kbuf);
962         return count;
963 }
964
965 static int chrdev_tx_done(struct channel_data *chan, int size)
966 {
967         if (chan->tx_status) { /* Writer was interrupted */
968                 kfree(chan->txbuf);
969                 up(&chan->wsem);
970         }
971         chan->tx_status = 1;
972         wake_up_interruptible(&chan->txwaitq);
973         return 1;
974 }
975
976 static unsigned int cosa_poll(struct file *file, poll_table *poll)
977 {
978         printk(KERN_INFO "cosa_poll is here\n");
979         return 0;
980 }
981
982 static int cosa_open(struct inode *inode, struct file *file)
983 {
984         struct cosa_data *cosa;
985         struct channel_data *chan;
986         unsigned long flags;
987         int n;
988
989         if ((n=iminor(file->f_dentry->d_inode)>>CARD_MINOR_BITS)
990                 >= nr_cards)
991                 return -ENODEV;
992         cosa = cosa_cards+n;
993
994         if ((n=iminor(file->f_dentry->d_inode)
995                 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
996                 return -ENODEV;
997         chan = cosa->chan + n;
998         
999         file->private_data = chan;
1000
1001         spin_lock_irqsave(&cosa->lock, flags);
1002
1003         if (chan->usage < 0) { /* in netdev mode */
1004                 spin_unlock_irqrestore(&cosa->lock, flags);
1005                 return -EBUSY;
1006         }
1007         cosa->usage++;
1008         chan->usage++;
1009
1010         chan->tx_done = chrdev_tx_done;
1011         chan->setup_rx = chrdev_setup_rx;
1012         chan->rx_done = chrdev_rx_done;
1013         spin_unlock_irqrestore(&cosa->lock, flags);
1014         return 0;
1015 }
1016
1017 static int cosa_release(struct inode *inode, struct file *file)
1018 {
1019         struct channel_data *channel = file->private_data;
1020         struct cosa_data *cosa;
1021         unsigned long flags;
1022
1023         cosa = channel->cosa;
1024         spin_lock_irqsave(&cosa->lock, flags);
1025         cosa->usage--;
1026         channel->usage--;
1027         spin_unlock_irqrestore(&cosa->lock, flags);
1028         return 0;
1029 }
1030
1031 #ifdef COSA_FASYNC_WORKING
1032 static struct fasync_struct *fasync[256] = { NULL, };
1033
1034 /* To be done ... */
1035 static int cosa_fasync(struct inode *inode, struct file *file, int on)
1036 {
1037         int port = iminor(inode);
1038         int rv = fasync_helper(inode, file, on, &fasync[port]);
1039         return rv < 0 ? rv : 0;
1040 }
1041 #endif
1042
1043 \f
1044 /* ---------- Ioctls ---------- */
1045
1046 /*
1047  * Ioctl subroutines can safely be made inline, because they are called
1048  * only from cosa_ioctl().
1049  */
1050 static inline int cosa_reset(struct cosa_data *cosa)
1051 {
1052         char idstring[COSA_MAX_ID_STRING];
1053         if (cosa->usage > 1)
1054                 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1055                         cosa->num, cosa->usage);
1056         cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1057         if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1058                 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1059                 return -EIO;
1060         }
1061         printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1062                 idstring);
1063         cosa->firmware_status |= COSA_FW_RESET;
1064         return 0;
1065 }
1066
1067 /* High-level function to download data into COSA memory. Calls download() */
1068 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1069 {
1070         struct cosa_download d;
1071         int i;
1072
1073         if (cosa->usage > 1)
1074                 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1075                         cosa->name, cosa->usage);
1076         if (!(cosa->firmware_status & COSA_FW_RESET)) {
1077                 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1078                         cosa->name, cosa->firmware_status);
1079                 return -EPERM;
1080         }
1081         
1082         if (copy_from_user(&d, arg, sizeof(d)))
1083                 return -EFAULT;
1084
1085         if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1086                 return -EINVAL;
1087         if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1088                 return -EINVAL;
1089
1090
1091         /* If something fails, force the user to reset the card */
1092         cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1093
1094         i = download(cosa, d.code, d.len, d.addr);
1095         if (i < 0) {
1096                 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1097                         cosa->num, i);
1098                 return -EIO;
1099         }
1100         printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1101                 cosa->num, d.len, d.addr);
1102         cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1103         return 0;
1104 }
1105
1106 /* High-level function to read COSA memory. Calls readmem() */
1107 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1108 {
1109         struct cosa_download d;
1110         int i;
1111
1112         if (cosa->usage > 1)
1113                 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1114                         "cosa->usage > 1 (%d). Odd things may happen.\n",
1115                         cosa->num, cosa->usage);
1116         if (!(cosa->firmware_status & COSA_FW_RESET)) {
1117                 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1118                         cosa->name, cosa->firmware_status);
1119                 return -EPERM;
1120         }
1121
1122         if (copy_from_user(&d, arg, sizeof(d)))
1123                 return -EFAULT;
1124
1125         /* If something fails, force the user to reset the card */
1126         cosa->firmware_status &= ~COSA_FW_RESET;
1127
1128         i = readmem(cosa, d.code, d.len, d.addr);
1129         if (i < 0) {
1130                 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1131                         cosa->num, i);
1132                 return -EIO;
1133         }
1134         printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1135                 cosa->num, d.len, d.addr);
1136         cosa->firmware_status |= COSA_FW_RESET;
1137         return 0;
1138 }
1139
1140 /* High-level function to start microcode. Calls startmicrocode(). */
1141 static inline int cosa_start(struct cosa_data *cosa, int address)
1142 {
1143         int i;
1144
1145         if (cosa->usage > 1)
1146                 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1147                         cosa->num, cosa->usage);
1148
1149         if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1150                 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1151                 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1152                         cosa->name, cosa->firmware_status);
1153                 return -EPERM;
1154         }
1155         cosa->firmware_status &= ~COSA_FW_RESET;
1156         if ((i=startmicrocode(cosa, address)) < 0) {
1157                 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1158                         cosa->num, address, i);
1159                 return -EIO;
1160         }
1161         printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1162                 cosa->num, address);
1163         cosa->startaddr = address;
1164         cosa->firmware_status |= COSA_FW_START;
1165         return 0;
1166 }
1167                 
1168 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1169 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1170 {
1171         int l = strlen(cosa->id_string)+1;
1172         if (copy_to_user(string, cosa->id_string, l))
1173                 return -EFAULT;
1174         return l;
1175 }
1176
1177 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1178 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1179 {
1180         int l = strlen(cosa->type)+1;
1181         if (copy_to_user(string, cosa->type, l))
1182                 return -EFAULT;
1183         return l;
1184 }
1185
1186 static int cosa_ioctl_common(struct cosa_data *cosa,
1187         struct channel_data *channel, unsigned int cmd, unsigned long arg)
1188 {
1189         void __user *argp = (void __user *)arg;
1190         switch(cmd) {
1191         case COSAIORSET:        /* Reset the device */
1192                 if (!capable(CAP_NET_ADMIN))
1193                         return -EACCES;
1194                 return cosa_reset(cosa);
1195         case COSAIOSTRT:        /* Start the firmware */
1196                 if (!capable(CAP_SYS_RAWIO))
1197                         return -EACCES;
1198                 return cosa_start(cosa, arg);
1199         case COSAIODOWNLD:      /* Download the firmware */
1200                 if (!capable(CAP_SYS_RAWIO))
1201                         return -EACCES;
1202                 
1203                 return cosa_download(cosa, argp);
1204         case COSAIORMEM:
1205                 if (!capable(CAP_SYS_RAWIO))
1206                         return -EACCES;
1207                 return cosa_readmem(cosa, argp);
1208         case COSAIORTYPE:
1209                 return cosa_gettype(cosa, argp);
1210         case COSAIORIDSTR:
1211                 return cosa_getidstr(cosa, argp);
1212         case COSAIONRCARDS:
1213                 return nr_cards;
1214         case COSAIONRCHANS:
1215                 return cosa->nchannels;
1216         case COSAIOBMSET:
1217                 if (!capable(CAP_SYS_RAWIO))
1218                         return -EACCES;
1219                 if (is_8bit(cosa))
1220                         return -EINVAL;
1221                 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1222                         return -EINVAL;
1223                 cosa->busmaster = arg;
1224                 return 0;
1225         case COSAIOBMGET:
1226                 return cosa->busmaster;
1227         }
1228         return -ENOIOCTLCMD;
1229 }
1230
1231 static int cosa_sppp_ioctl(struct net_device *dev, struct ifreq *ifr,
1232         int cmd)
1233 {
1234         int rv;
1235         struct channel_data *chan = dev->priv;
1236         rv = cosa_ioctl_common(chan->cosa, chan, cmd, (unsigned long)ifr->ifr_data);
1237         if (rv == -ENOIOCTLCMD) {
1238                 return sppp_do_ioctl(dev, ifr, cmd);
1239         }
1240         return rv;
1241 }
1242
1243 static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1244         unsigned int cmd, unsigned long arg)
1245 {
1246         struct channel_data *channel = file->private_data;
1247         struct cosa_data *cosa = channel->cosa;
1248         return cosa_ioctl_common(cosa, channel, cmd, arg);
1249 }
1250
1251 \f
1252 /*---------- HW layer interface ---------- */
1253
1254 /*
1255  * The higher layer can bind itself to the HW layer by setting the callbacks
1256  * in the channel_data structure and by using these routines.
1257  */
1258 static void cosa_enable_rx(struct channel_data *chan)
1259 {
1260         struct cosa_data *cosa = chan->cosa;
1261
1262         if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1263                 put_driver_status(cosa);
1264 }
1265
1266 static void cosa_disable_rx(struct channel_data *chan)
1267 {
1268         struct cosa_data *cosa = chan->cosa;
1269
1270         if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1271                 put_driver_status(cosa);
1272 }
1273
1274 /*
1275  * FIXME: This routine probably should check for cosa_start_tx() called when
1276  * the previous transmit is still unfinished. In this case the non-zero
1277  * return value should indicate to the caller that the queuing(sp?) up
1278  * the transmit has failed.
1279  */
1280 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1281 {
1282         struct cosa_data *cosa = chan->cosa;
1283         unsigned long flags;
1284 #ifdef DEBUG_DATA
1285         int i;
1286
1287         printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1288                 chan->num, len);
1289         for (i=0; i<len; i++)
1290                 printk(" %02x", buf[i]&0xff);
1291         printk("\n");
1292 #endif
1293         spin_lock_irqsave(&cosa->lock, flags);
1294         chan->txbuf = buf;
1295         chan->txsize = len;
1296         if (len > COSA_MTU)
1297                 chan->txsize = COSA_MTU;
1298         spin_unlock_irqrestore(&cosa->lock, flags);
1299
1300         /* Tell the firmware we are ready */
1301         set_bit(chan->num, &cosa->txbitmap);
1302         put_driver_status(cosa);
1303
1304         return 0;
1305 }
1306
1307 static void put_driver_status(struct cosa_data *cosa)
1308 {
1309         unsigned long flags;
1310         int status;
1311
1312         spin_lock_irqsave(&cosa->lock, flags);
1313
1314         status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1315                 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1316                 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1317                         &DRIVER_TXMAP_MASK : 0);
1318         if (!cosa->rxtx) {
1319                 if (cosa->rxbitmap|cosa->txbitmap) {
1320                         if (!cosa->enabled) {
1321                                 cosa_putstatus(cosa, SR_RX_INT_ENA);
1322 #ifdef DEBUG_IO
1323                                 debug_status_out(cosa, SR_RX_INT_ENA);
1324 #endif
1325                                 cosa->enabled = 1;
1326                         }
1327                 } else if (cosa->enabled) {
1328                         cosa->enabled = 0;
1329                         cosa_putstatus(cosa, 0);
1330 #ifdef DEBUG_IO
1331                         debug_status_out(cosa, 0);
1332 #endif
1333                 }
1334                 cosa_putdata8(cosa, status);
1335 #ifdef DEBUG_IO
1336                 debug_data_cmd(cosa, status);
1337 #endif
1338         }
1339         spin_unlock_irqrestore(&cosa->lock, flags);
1340 }
1341
1342 static void put_driver_status_nolock(struct cosa_data *cosa)
1343 {
1344         int status;
1345
1346         status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1347                 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1348                 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1349                         &DRIVER_TXMAP_MASK : 0);
1350
1351         if (cosa->rxbitmap|cosa->txbitmap) {
1352                 cosa_putstatus(cosa, SR_RX_INT_ENA);
1353 #ifdef DEBUG_IO
1354                 debug_status_out(cosa, SR_RX_INT_ENA);
1355 #endif
1356                 cosa->enabled = 1;
1357         } else {
1358                 cosa_putstatus(cosa, 0);
1359 #ifdef DEBUG_IO
1360                 debug_status_out(cosa, 0);
1361 #endif
1362                 cosa->enabled = 0;
1363         }
1364         cosa_putdata8(cosa, status);
1365 #ifdef DEBUG_IO
1366         debug_data_cmd(cosa, status);
1367 #endif
1368 }
1369
1370 /*
1371  * The "kickme" function: When the DMA times out, this is called to
1372  * clean up the driver status.
1373  * FIXME: Preliminary support, the interface is probably wrong.
1374  */
1375 static void cosa_kick(struct cosa_data *cosa)
1376 {
1377         unsigned long flags, flags1;
1378         char *s = "(probably) IRQ";
1379
1380         if (test_bit(RXBIT, &cosa->rxtx))
1381                 s = "RX DMA";
1382         if (test_bit(TXBIT, &cosa->rxtx))
1383                 s = "TX DMA";
1384
1385         printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s); 
1386         spin_lock_irqsave(&cosa->lock, flags);
1387         cosa->rxtx = 0;
1388
1389         flags1 = claim_dma_lock();
1390         disable_dma(cosa->dma);
1391         clear_dma_ff(cosa->dma);
1392         release_dma_lock(flags1);
1393
1394         /* FIXME: Anything else? */
1395         udelay(100);
1396         cosa_putstatus(cosa, 0);
1397         udelay(100);
1398         (void) cosa_getdata8(cosa);
1399         udelay(100);
1400         cosa_putdata8(cosa, 0);
1401         udelay(100);
1402         put_driver_status_nolock(cosa);
1403         spin_unlock_irqrestore(&cosa->lock, flags);
1404 }
1405
1406 /*
1407  * Check if the whole buffer is DMA-able. It means it is below the 16M of
1408  * physical memory and doesn't span the 64k boundary. For now it seems
1409  * SKB's never do this, but we'll check this anyway.
1410  */
1411 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1412 {
1413         static int count;
1414         unsigned long b = (unsigned long)buf;
1415         if (b+len >= MAX_DMA_ADDRESS)
1416                 return 0;
1417         if ((b^ (b+len)) & 0x10000) {
1418                 if (count++ < 5)
1419                         printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1420                                 chan->name);
1421                 return 0;
1422         }
1423         return 1;
1424 }
1425
1426 \f
1427 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1428
1429 /*
1430  * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1431  * drivers need to say 4-digit hex number meaning start address of the microcode
1432  * separated by a single space. Monitor replies by saying " =". Now driver
1433  * has to write 4-digit hex number meaning the last byte address ended
1434  * by a single space. Monitor has to reply with a space. Now the download
1435  * begins. After the download monitor replies with "\r\n." (CR LF dot).
1436  */
1437 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1438 {
1439         int i;
1440
1441         if (put_wait_data(cosa, 'w') == -1) return -1;
1442         if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1443         if (get_wait_data(cosa) != '=') return -3;
1444
1445         if (puthexnumber(cosa, address) < 0) return -4;
1446         if (put_wait_data(cosa, ' ') == -1) return -10;
1447         if (get_wait_data(cosa) != ' ') return -11;
1448         if (get_wait_data(cosa) != '=') return -12;
1449
1450         if (puthexnumber(cosa, address+length-1) < 0) return -13;
1451         if (put_wait_data(cosa, ' ') == -1) return -18;
1452         if (get_wait_data(cosa) != ' ') return -19;
1453
1454         while (length--) {
1455                 char c;
1456 #ifndef SRP_DOWNLOAD_AT_BOOT
1457                 if (get_user(c, microcode))
1458                         return -23; /* ??? */
1459 #else
1460                 c = *microcode;
1461 #endif
1462                 if (put_wait_data(cosa, c) == -1)
1463                         return -20;
1464                 microcode++;
1465         }
1466
1467         if (get_wait_data(cosa) != '\r') return -21;
1468         if (get_wait_data(cosa) != '\n') return -22;
1469         if (get_wait_data(cosa) != '.') return -23;
1470 #if 0
1471         printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1472 #endif
1473         return 0;
1474 }
1475
1476
1477 /*
1478  * Starting microcode is done via the "g" command of the SRP monitor.
1479  * The chat should be the following: "g" "g=" "<addr><CR>"
1480  * "<CR><CR><LF><CR><LF>".
1481  */
1482 static int startmicrocode(struct cosa_data *cosa, int address)
1483 {
1484         if (put_wait_data(cosa, 'g') == -1) return -1;
1485         if (get_wait_data(cosa) != 'g') return -2;
1486         if (get_wait_data(cosa) != '=') return -3;
1487
1488         if (puthexnumber(cosa, address) < 0) return -4;
1489         if (put_wait_data(cosa, '\r') == -1) return -5;
1490         
1491         if (get_wait_data(cosa) != '\r') return -6;
1492         if (get_wait_data(cosa) != '\r') return -7;
1493         if (get_wait_data(cosa) != '\n') return -8;
1494         if (get_wait_data(cosa) != '\r') return -9;
1495         if (get_wait_data(cosa) != '\n') return -10;
1496 #if 0
1497         printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1498 #endif
1499         return 0;
1500 }
1501
1502 /*
1503  * Reading memory is done via the "r" command of the SRP monitor.
1504  * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1505  * Then driver can read the data and the conversation is finished
1506  * by SRP monitor sending "<CR><LF>." (dot at the end).
1507  *
1508  * This routine is not needed during the normal operation and serves
1509  * for debugging purposes only.
1510  */
1511 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1512 {
1513         if (put_wait_data(cosa, 'r') == -1) return -1;
1514         if ((get_wait_data(cosa)) != 'r') return -2;
1515         if ((get_wait_data(cosa)) != '=') return -3;
1516
1517         if (puthexnumber(cosa, address) < 0) return -4;
1518         if (put_wait_data(cosa, ' ') == -1) return -5;
1519         if (get_wait_data(cosa) != ' ') return -6;
1520         if (get_wait_data(cosa) != '=') return -7;
1521
1522         if (puthexnumber(cosa, address+length-1) < 0) return -8;
1523         if (put_wait_data(cosa, ' ') == -1) return -9;
1524         if (get_wait_data(cosa) != ' ') return -10;
1525
1526         while (length--) {
1527                 char c;
1528                 int i;
1529                 if ((i=get_wait_data(cosa)) == -1) {
1530                         printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1531                                 length);
1532                         return -11;
1533                 }
1534                 c=i;
1535 #if 1
1536                 if (put_user(c, microcode))
1537                         return -23; /* ??? */
1538 #else
1539                 *microcode = c;
1540 #endif
1541                 microcode++;
1542         }
1543
1544         if (get_wait_data(cosa) != '\r') return -21;
1545         if (get_wait_data(cosa) != '\n') return -22;
1546         if (get_wait_data(cosa) != '.') return -23;
1547 #if 0
1548         printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1549 #endif
1550         return 0;
1551 }
1552
1553 /*
1554  * This function resets the device and reads the initial prompt
1555  * of the device's ROM monitor.
1556  */
1557 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1558 {
1559         int i=0, id=0, prev=0, curr=0;
1560
1561         /* Reset the card ... */
1562         cosa_putstatus(cosa, 0);
1563         cosa_getdata8(cosa);
1564         cosa_putstatus(cosa, SR_RST);
1565 #ifdef MODULE
1566         msleep(500);
1567 #else
1568         udelay(5*100000);
1569 #endif
1570         /* Disable all IRQs from the card */
1571         cosa_putstatus(cosa, 0);
1572
1573         /*
1574          * Try to read the ID string. The card then prints out the
1575          * identification string ended by the "\n\x2e".
1576          *
1577          * The following loop is indexed through i (instead of id)
1578          * to avoid looping forever when for any reason
1579          * the port returns '\r', '\n' or '\x2e' permanently.
1580          */
1581         for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1582                 if ((curr = get_wait_data(cosa)) == -1) {
1583                         return -1;
1584                 }
1585                 curr &= 0xff;
1586                 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1587                         idstring[id++] = curr;
1588                 if (curr == 0x2e && prev == '\n')
1589                         break;
1590         }
1591         /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1592         idstring[id] = '\0';
1593         return id;
1594 }
1595
1596 \f
1597 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1598
1599 /*
1600  * This routine gets the data byte from the card waiting for the SR_RX_RDY
1601  * bit to be set in a loop. It should be used in the exceptional cases
1602  * only (for example when resetting the card or downloading the firmware.
1603  */
1604 static int get_wait_data(struct cosa_data *cosa)
1605 {
1606         int retries = 1000;
1607
1608         while (--retries) {
1609                 /* read data and return them */
1610                 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1611                         short r;
1612                         r = cosa_getdata8(cosa);
1613 #if 0
1614                         printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1615 #endif
1616                         return r;
1617                 }
1618                 /* sleep if not ready to read */
1619                 schedule_timeout_interruptible(1);
1620         }
1621         printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1622                 cosa_getstatus(cosa));
1623         return -1;
1624 }
1625
1626 /*
1627  * This routine puts the data byte to the card waiting for the SR_TX_RDY
1628  * bit to be set in a loop. It should be used in the exceptional cases
1629  * only (for example when resetting the card or downloading the firmware).
1630  */
1631 static int put_wait_data(struct cosa_data *cosa, int data)
1632 {
1633         int retries = 1000;
1634         while (--retries) {
1635                 /* read data and return them */
1636                 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1637                         cosa_putdata8(cosa, data);
1638 #if 0
1639                         printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1640 #endif
1641                         return 0;
1642                 }
1643 #if 0
1644                 /* sleep if not ready to read */
1645                 schedule_timeout_interruptible(1);
1646 #endif
1647         }
1648         printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1649                 cosa->num, cosa_getstatus(cosa));
1650         return -1;
1651 }
1652         
1653 /* 
1654  * The following routine puts the hexadecimal number into the SRP monitor
1655  * and verifies the proper echo of the sent bytes. Returns 0 on success,
1656  * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1657  * (-2,-4,-6,-8) means that reading echo failed.
1658  */
1659 static int puthexnumber(struct cosa_data *cosa, int number)
1660 {
1661         char temp[5];
1662         int i;
1663
1664         /* Well, I should probably replace this by something faster. */
1665         sprintf(temp, "%04X", number);
1666         for (i=0; i<4; i++) {
1667                 if (put_wait_data(cosa, temp[i]) == -1) {
1668                         printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1669                                 cosa->num, i);
1670                         return -1-2*i;
1671                 }
1672                 if (get_wait_data(cosa) != temp[i]) {
1673                         printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1674                                 cosa->num, i);
1675                         return -2-2*i;
1676                 }
1677         }
1678         return 0;
1679 }
1680
1681 \f
1682 /* ---------- Interrupt routines ---------- */
1683
1684 /*
1685  * There are three types of interrupt:
1686  * At the beginning of transmit - this handled is in tx_interrupt(),
1687  * at the beginning of receive - it is in rx_interrupt() and
1688  * at the end of transmit/receive - it is the eot_interrupt() function.
1689  * These functions are multiplexed by cosa_interrupt() according to the
1690  * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1691  * separate functions to make it more readable. These functions are inline,
1692  * so there should be no overhead of function call.
1693  * 
1694  * In the COSA bus-master mode, we need to tell the card the address of a
1695  * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1696  * It's time to use the bottom half :-(
1697  */
1698
1699 /*
1700  * Transmit interrupt routine - called when COSA is willing to obtain
1701  * data from the OS. The most tricky part of the routine is selection
1702  * of channel we (OS) want to send packet for. For SRP we should probably
1703  * use the round-robin approach. The newer COSA firmwares have a simple
1704  * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1705  * channel 0 or 1 doesn't want to receive data.
1706  *
1707  * It seems there is a bug in COSA firmware (need to trace it further):
1708  * When the driver status says that the kernel has no more data for transmit
1709  * (e.g. at the end of TX DMA) and then the kernel changes its mind
1710  * (e.g. new packet is queued to hard_start_xmit()), the card issues
1711  * the TX interrupt but does not mark the channel as ready-to-transmit.
1712  * The fix seems to be to push the packet to COSA despite its request.
1713  * We first try to obey the card's opinion, and then fall back to forced TX.
1714  */
1715 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1716 {
1717         unsigned long flags, flags1;
1718 #ifdef DEBUG_IRQS
1719         printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1720                 cosa->num, status);
1721 #endif
1722         spin_lock_irqsave(&cosa->lock, flags);
1723         set_bit(TXBIT, &cosa->rxtx);
1724         if (!test_bit(IRQBIT, &cosa->rxtx)) {
1725                 /* flow control, see the comment above */
1726                 int i=0;
1727                 if (!cosa->txbitmap) {
1728                         printk(KERN_WARNING "%s: No channel wants data "
1729                                 "in TX IRQ. Expect DMA timeout.",
1730                                 cosa->name);
1731                         put_driver_status_nolock(cosa);
1732                         clear_bit(TXBIT, &cosa->rxtx);
1733                         spin_unlock_irqrestore(&cosa->lock, flags);
1734                         return;
1735                 }
1736                 while(1) {
1737                         cosa->txchan++;
1738                         i++;
1739                         if (cosa->txchan >= cosa->nchannels)
1740                                 cosa->txchan = 0;
1741                         if (!(cosa->txbitmap & (1<<cosa->txchan)))
1742                                 continue;
1743                         if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1744                                 break;
1745                         /* in second pass, accept first ready-to-TX channel */
1746                         if (i > cosa->nchannels) {
1747                                 /* Can be safely ignored */
1748 #ifdef DEBUG_IRQS
1749                                 printk(KERN_DEBUG "%s: Forcing TX "
1750                                         "to not-ready channel %d\n",
1751                                         cosa->name, cosa->txchan);
1752 #endif
1753                                 break;
1754                         }
1755                 }
1756
1757                 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1758                 if (cosa_dma_able(cosa->chan+cosa->txchan,
1759                         cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1760                         cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1761                 } else {
1762                         memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1763                                 cosa->txsize);
1764                         cosa->txbuf = cosa->bouncebuf;
1765                 }
1766         }
1767
1768         if (is_8bit(cosa)) {
1769                 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1770                         cosa_putstatus(cosa, SR_TX_INT_ENA);
1771                         cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1772                                 ((cosa->txsize >> 8) & 0x1f));
1773 #ifdef DEBUG_IO
1774                         debug_status_out(cosa, SR_TX_INT_ENA);
1775                         debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1776                                 ((cosa->txsize >> 8) & 0x1f));
1777                         debug_data_in(cosa, cosa_getdata8(cosa));
1778 #else
1779                         cosa_getdata8(cosa);
1780 #endif
1781                         set_bit(IRQBIT, &cosa->rxtx);
1782                         spin_unlock_irqrestore(&cosa->lock, flags);
1783                         return;
1784                 } else {
1785                         clear_bit(IRQBIT, &cosa->rxtx);
1786                         cosa_putstatus(cosa, 0);
1787                         cosa_putdata8(cosa, cosa->txsize&0xff);
1788 #ifdef DEBUG_IO
1789                         debug_status_out(cosa, 0);
1790                         debug_data_out(cosa, cosa->txsize&0xff);
1791 #endif
1792                 }
1793         } else {
1794                 cosa_putstatus(cosa, SR_TX_INT_ENA);
1795                 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1796                         | (cosa->txsize & 0x1fff));
1797 #ifdef DEBUG_IO
1798                 debug_status_out(cosa, SR_TX_INT_ENA);
1799                 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1800                         | (cosa->txsize & 0x1fff));
1801                 debug_data_in(cosa, cosa_getdata8(cosa));
1802                 debug_status_out(cosa, 0);
1803 #else
1804                 cosa_getdata8(cosa);
1805 #endif
1806                 cosa_putstatus(cosa, 0);
1807         }
1808
1809         if (cosa->busmaster) {
1810                 unsigned long addr = virt_to_bus(cosa->txbuf);
1811                 int count=0;
1812                 printk(KERN_INFO "busmaster IRQ\n");
1813                 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1814                         count++;
1815                         udelay(10);
1816                         if (count > 1000) break;
1817                 }
1818                 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1819                 printk(KERN_INFO "ready after %d loops\n", count);
1820                 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1821
1822                 count = 0;
1823                 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1824                         count++;
1825                         if (count > 1000) break;
1826                         udelay(10);
1827                 }
1828                 printk(KERN_INFO "ready after %d loops\n", count);
1829                 cosa_putdata16(cosa, addr &0xffff);
1830                 flags1 = claim_dma_lock();
1831                 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1832                 enable_dma(cosa->dma);
1833                 release_dma_lock(flags1);
1834         } else {
1835                 /* start the DMA */
1836                 flags1 = claim_dma_lock();
1837                 disable_dma(cosa->dma);
1838                 clear_dma_ff(cosa->dma);
1839                 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1840                 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1841                 set_dma_count(cosa->dma, cosa->txsize);
1842                 enable_dma(cosa->dma);
1843                 release_dma_lock(flags1);
1844         }
1845         cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1846 #ifdef DEBUG_IO
1847         debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1848 #endif
1849         spin_unlock_irqrestore(&cosa->lock, flags);
1850 }
1851
1852 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1853 {
1854         unsigned long flags;
1855 #ifdef DEBUG_IRQS
1856         printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1857 #endif
1858
1859         spin_lock_irqsave(&cosa->lock, flags);
1860         set_bit(RXBIT, &cosa->rxtx);
1861
1862         if (is_8bit(cosa)) {
1863                 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1864                         set_bit(IRQBIT, &cosa->rxtx);
1865                         put_driver_status_nolock(cosa);
1866                         cosa->rxsize = cosa_getdata8(cosa) <<8;
1867 #ifdef DEBUG_IO
1868                         debug_data_in(cosa, cosa->rxsize >> 8);
1869 #endif
1870                         spin_unlock_irqrestore(&cosa->lock, flags);
1871                         return;
1872                 } else {
1873                         clear_bit(IRQBIT, &cosa->rxtx);
1874                         cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1875 #ifdef DEBUG_IO
1876                         debug_data_in(cosa, cosa->rxsize & 0xff);
1877 #endif
1878 #if 0
1879                         printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1880                                 cosa->num, cosa->rxsize);
1881 #endif
1882                 }
1883         } else {
1884                 cosa->rxsize = cosa_getdata16(cosa);
1885 #ifdef DEBUG_IO
1886                 debug_data_in(cosa, cosa->rxsize);
1887 #endif
1888 #if 0
1889                 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1890                         cosa->num, cosa->rxsize);
1891 #endif
1892         }
1893         if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1894                 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1895                         cosa->name, cosa->rxsize);
1896                 spin_unlock_irqrestore(&cosa->lock, flags);
1897                 goto reject;
1898         }
1899         cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1900         cosa->rxsize &= 0x1fff;
1901         spin_unlock_irqrestore(&cosa->lock, flags);
1902
1903         cosa->rxbuf = NULL;
1904         if (cosa->rxchan->setup_rx)
1905                 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1906
1907         if (!cosa->rxbuf) {
1908 reject:         /* Reject the packet */
1909                 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1910                         cosa->num, cosa->rxchan->num);
1911                 cosa->rxbuf = cosa->bouncebuf;
1912         }
1913
1914         /* start the DMA */
1915         flags = claim_dma_lock();
1916         disable_dma(cosa->dma);
1917         clear_dma_ff(cosa->dma);
1918         set_dma_mode(cosa->dma, DMA_MODE_READ);
1919         if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1920                 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1921         } else {
1922                 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1923         }
1924         set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1925         enable_dma(cosa->dma);
1926         release_dma_lock(flags);
1927         spin_lock_irqsave(&cosa->lock, flags);
1928         cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1929         if (!is_8bit(cosa) && (status & SR_TX_RDY))
1930                 cosa_putdata8(cosa, DRIVER_RX_READY);
1931 #ifdef DEBUG_IO
1932         debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1933         if (!is_8bit(cosa) && (status & SR_TX_RDY))
1934                 debug_data_cmd(cosa, DRIVER_RX_READY);
1935 #endif
1936         spin_unlock_irqrestore(&cosa->lock, flags);
1937 }
1938
1939 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1940 {
1941         unsigned long flags, flags1;
1942         spin_lock_irqsave(&cosa->lock, flags);
1943         flags1 = claim_dma_lock();
1944         disable_dma(cosa->dma);
1945         clear_dma_ff(cosa->dma);
1946         release_dma_lock(flags1);
1947         if (test_bit(TXBIT, &cosa->rxtx)) {
1948                 struct channel_data *chan = cosa->chan+cosa->txchan;
1949                 if (chan->tx_done)
1950                         if (chan->tx_done(chan, cosa->txsize))
1951                                 clear_bit(chan->num, &cosa->txbitmap);
1952         } else if (test_bit(RXBIT, &cosa->rxtx)) {
1953 #ifdef DEBUG_DATA
1954         {
1955                 int i;
1956                 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num, 
1957                         cosa->rxchan->num, cosa->rxsize);
1958                 for (i=0; i<cosa->rxsize; i++)
1959                         printk (" %02x", cosa->rxbuf[i]&0xff);
1960                 printk("\n");
1961         }
1962 #endif
1963                 /* Packet for unknown channel? */
1964                 if (cosa->rxbuf == cosa->bouncebuf)
1965                         goto out;
1966                 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1967                         memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1968                 if (cosa->rxchan->rx_done)
1969                         if (cosa->rxchan->rx_done(cosa->rxchan))
1970                                 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1971         } else {
1972                 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1973                         cosa->num);
1974         }
1975         /*
1976          * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1977          * cleared anyway). We should do it as soon as possible
1978          * so that we can tell the COSA we are done and to give it a time
1979          * for recovery.
1980          */
1981 out:
1982         cosa->rxtx = 0;
1983         put_driver_status_nolock(cosa);
1984         spin_unlock_irqrestore(&cosa->lock, flags);
1985 }
1986
1987 static irqreturn_t cosa_interrupt(int irq, void *cosa_, struct pt_regs *regs)
1988 {
1989         unsigned status;
1990         int count = 0;
1991         struct cosa_data *cosa = cosa_;
1992 again:
1993         status = cosa_getstatus(cosa);
1994 #ifdef DEBUG_IRQS
1995         printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1996                 status & 0xff);
1997 #endif
1998 #ifdef DEBUG_IO
1999         debug_status_in(cosa, status);
2000 #endif
2001         switch (status & SR_CMD_FROM_SRP_MASK) {
2002         case SR_DOWN_REQUEST:
2003                 tx_interrupt(cosa, status);
2004                 break;
2005         case SR_UP_REQUEST:
2006                 rx_interrupt(cosa, status);
2007                 break;
2008         case SR_END_OF_TRANSFER:
2009                 eot_interrupt(cosa, status);
2010                 break;
2011         default:
2012                 /* We may be too fast for SRP. Try to wait a bit more. */
2013                 if (count++ < 100) {
2014                         udelay(100);
2015                         goto again;
2016                 }
2017                 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
2018                         cosa->num, status & 0xff, count);
2019         }
2020 #ifdef DEBUG_IRQS
2021         if (count)
2022                 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
2023                         cosa->name, count);
2024         else
2025                 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
2026 #endif
2027         return IRQ_HANDLED;
2028 }
2029
2030 \f
2031 /* ---------- I/O debugging routines ---------- */
2032 /*
2033  * These routines can be used to monitor COSA/SRP I/O and to printk()
2034  * the data being transferred on the data and status I/O port in a
2035  * readable way.
2036  */
2037
2038 #ifdef DEBUG_IO
2039 static void debug_status_in(struct cosa_data *cosa, int status)
2040 {
2041         char *s;
2042         switch(status & SR_CMD_FROM_SRP_MASK) {
2043         case SR_UP_REQUEST:
2044                 s = "RX_REQ";
2045                 break;
2046         case SR_DOWN_REQUEST:
2047                 s = "TX_REQ";
2048                 break;
2049         case SR_END_OF_TRANSFER:
2050                 s = "ET_REQ";
2051                 break;
2052         default:
2053                 s = "NO_REQ";
2054                 break;
2055         }
2056         printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2057                 cosa->name,
2058                 status,
2059                 status & SR_USR_RQ ? "USR_RQ|":"",
2060                 status & SR_TX_RDY ? "TX_RDY|":"",
2061                 status & SR_RX_RDY ? "RX_RDY|":"",
2062                 s);
2063 }
2064
2065 static void debug_status_out(struct cosa_data *cosa, int status)
2066 {
2067         printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2068                 cosa->name,
2069                 status,
2070                 status & SR_RX_DMA_ENA  ? "RXDMA|":"!rxdma|",
2071                 status & SR_TX_DMA_ENA  ? "TXDMA|":"!txdma|",
2072                 status & SR_RST         ? "RESET|":"",
2073                 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2074                 status & SR_TX_INT_ENA  ? "TXINT|":"!txint|",
2075                 status & SR_RX_INT_ENA  ? "RXINT":"!rxint");
2076 }
2077
2078 static void debug_data_in(struct cosa_data *cosa, int data)
2079 {
2080         printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2081 }
2082
2083 static void debug_data_out(struct cosa_data *cosa, int data)
2084 {
2085         printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2086 }
2087
2088 static void debug_data_cmd(struct cosa_data *cosa, int data)
2089 {
2090         printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2091                 cosa->name, data,
2092                 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2093                 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2094 }
2095 #endif
2096
2097 /* EOF -- this file has not been truncated */