Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / net / wan / cosa.c
blob9e73fc2704a83976bfd41ae1bd9620a4336760d8
1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
3 /*
4 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
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.
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.
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.
22 * The driver for the SRP and COSA synchronous serial cards.
24 * HARDWARE INFO
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.
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.
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.
45 * Both types have downloadable firmware and communicate via ISA DMA.
46 * COSA can be also a bus-mastering device.
48 * SOFTWARE INFO
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.
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).
59 * AUTHOR
61 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
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 :-).
67 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
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
76 * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
77 * fixed a deadlock in cosa_sppp_open
80 /* ---------- Headers, macros, data structures ---------- */
82 #include <linux/config.h>
83 #include <linux/module.h>
84 #include <linux/kernel.h>
85 #include <linux/malloc.h>
86 #include <linux/poll.h>
87 #include <linux/fs.h>
88 #include <linux/devfs_fs_kernel.h>
89 #include <linux/sched.h>
90 #include <linux/interrupt.h>
91 #include <linux/delay.h>
92 #include <linux/errno.h>
93 #include <linux/ioport.h>
94 #include <linux/netdevice.h>
95 #include <linux/spinlock.h>
96 #include <linux/smp_lock.h>
98 #undef COSA_SLOW_IO/* for testing purposes only */
99 #undef REALLY_SLOW_IO
101 #include <asm/io.h>
102 #include <asm/dma.h>
103 #include <asm/byteorder.h>
105 #include"syncppp.h"
106 #include"cosa.h"
108 /* Linux version stuff */
109 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1)
110 typedefstruct wait_queue *wait_queue_head_t;
111 #define DECLARE_WAITQUEUE(wait, current) \
112 struct wait_queue wait = { current, NULL }
113 #endif
115 /* Maximum length of the identification string. */
116 #define COSA_MAX_ID_STRING 128
118 /* Maximum length of the channel name */
119 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
121 /* Per-channel data structure */
123 struct channel_data {
124 void*if_ptr;/* General purpose pointer (used by SPPP) */
125 int usage;/* Usage count; >0 for chrdev, -1 for netdev */
126 int num;/* Number of the channel */
127 struct cosa_data *cosa;/* Pointer to the per-card structure */
128 int txsize;/* Size of transmitted data */
129 char*txbuf;/* Transmit buffer */
130 char name[COSA_MAX_NAME];/* channel name */
132 /* The HW layer interface */
133 /* routine called from the RX interrupt */
134 char*(*setup_rx)(struct channel_data *channel,int size);
135 /* routine called when the RX is done (from the EOT interrupt) */
136 int(*rx_done)(struct channel_data *channel);
137 /* routine called when the TX is done (from the EOT interrupt) */
138 int(*tx_done)(struct channel_data *channel,int size);
140 /* Character device parts */
141 struct semaphore rsem, wsem;
142 char*rxdata;
143 int rxsize;
144 wait_queue_head_t txwaitq, rxwaitq;
145 int tx_status, rx_status;
147 /* SPPP/HDLC device parts */
148 struct ppp_device pppdev;
149 struct sk_buff *rx_skb, *tx_skb;
150 struct net_device_stats stats;
153 /* cosa->firmware_status bits */
154 #define COSA_FW_RESET (1<<0)/* Is the ROM monitor active? */
155 #define COSA_FW_DOWNLOAD (1<<1)/* Is the microcode downloaded? */
156 #define COSA_FW_START (1<<2)/* Is the microcode running? */
158 struct cosa_data {
159 int num;/* Card number */
160 char name[COSA_MAX_NAME];/* Card name - e.g "cosa0" */
161 unsigned int datareg, statusreg;/* I/O ports */
162 unsigned short irq, dma;/* IRQ and DMA number */
163 unsigned short startaddr;/* Firmware start address */
164 unsigned short busmaster;/* Use busmastering? */
165 int nchannels;/* # of channels on this card */
166 int driver_status;/* For communicating with firware */
167 int firmware_status;/* Downloaded, reseted, etc. */
168 long int rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
169 long int rxtx;/* RX or TX in progress? */
170 int enabled;
171 int usage;/* usage count */
172 int txchan, txsize, rxsize;
173 struct channel_data *rxchan;
174 char*bouncebuf;
175 char*txbuf, *rxbuf;
176 struct channel_data *chan;
177 spinlock_t lock;/* For exclusive operations on this structure */
178 char id_string[COSA_MAX_ID_STRING];/* ROM monitor ID string */
179 char*type;/* card type */
183 * Define this if you want all the possible ports to be autoprobed.
184 * It is here but it probably is not a good idea to use this.
186 /* #define COSA_ISA_AUTOPROBE 1 */
189 * Character device major number. 117 was allocated for us.
190 * The value of 0 means to allocate a first free one.
192 static int cosa_major =117;
195 * Encoding of the minor numbers:
196 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
197 * the highest bits means the card number.
199 #define CARD_MINOR_BITS 4/* How many bits in minor number are reserved
200 * for the single card */
202 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
203 * macro doesn't like anything other than the raw number as an argument :-(
205 #define MAX_CARDS 16
206 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
208 #define DRIVER_RX_READY 0x0001
209 #define DRIVER_TX_READY 0x0002
210 #define DRIVER_TXMAP_SHIFT 2
211 #define DRIVER_TXMAP_MASK 0x0c/* FIXME: 0xfc for 8-channel version */
214 * for cosa->rxtx - indicates whether either transmit or receive is
215 * in progress. These values are mean number of the bit.
217 #define TXBIT 0
218 #define RXBIT 1
219 #define IRQBIT 2
221 #define COSA_MTU 2000/* FIXME: I don't know this exactly */
223 #undef DEBUG_DATA 1/* Dump the data read or written to the channel */
224 #undef DEBUG_IRQS 1/* Print the message when the IRQ is received */
225 #undef DEBUG_IO 1/* Dump the I/O traffic */
227 #define TX_TIMEOUT (5*HZ)
229 /* Maybe the following should be allocated dynamically */
230 static struct cosa_data cosa_cards[MAX_CARDS];
231 static int nr_cards;
233 #ifdef COSA_ISA_AUTOPROBE
234 static int io[MAX_CARDS+1] = {0x220,0x228,0x210,0x218,0, };
235 /* NOTE: DMA is not autoprobed!!! */
236 static int dma[MAX_CARDS+1] = {1,7,1,7,1,7,1,7,0, };
237 #else
238 int io[MAX_CARDS+1] = {0, };
239 int dma[MAX_CARDS+1] = {0, };
240 #endif
241 /* IRQ can be safely autoprobed */
242 static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1,0, };
244 #ifdef MODULE
245 MODULE_PARM(io,"1-"__MODULE_STRING(MAX_CARDS)"i");
246 MODULE_PARM_DESC(io,"The I/O bases of the COSA or SRP cards");
247 MODULE_PARM(irq,"1-"__MODULE_STRING(MAX_CARDS)"i");
248 MODULE_PARM_DESC(irq,"The IRQ lines of the COSA or SRP cards");
249 MODULE_PARM(dma,"1-"__MODULE_STRING(MAX_CARDS)"i");
250 MODULE_PARM_DESC(dma,"The DMA channels of the COSA or SRP cards");
252 MODULE_AUTHOR("Jan\"Yenya\"Kasprzak, <kas@fi.muni.cz>");
253 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
254 #endif
256 /* I use this mainly for testing purposes */
257 #ifdef COSA_SLOW_IO
258 #define cosa_outb outb_p
259 #define cosa_outw outw_p
260 #define cosa_inb inb_p
261 #define cosa_inw inw_p
262 #else
263 #define cosa_outb outb
264 #define cosa_outw outw
265 #define cosa_inb inb
266 #define cosa_inw inw
267 #endif
269 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
271 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
272 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
273 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
274 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
275 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
276 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
278 /* Initialization stuff */
279 static intcosa_probe(int ioaddr,int irq,int dma);
281 /* HW interface */
282 static voidcosa_enable_rx(struct channel_data *chan);
283 static voidcosa_disable_rx(struct channel_data *chan);
284 static intcosa_start_tx(struct channel_data *channel,char*buf,int size);
285 static voidcosa_kick(struct cosa_data *cosa);
286 static intcosa_dma_able(struct channel_data *chan,char*buf,int data);
288 /* SPPP/HDLC stuff */
289 static voidsppp_channel_init(struct channel_data *chan);
290 static voidsppp_channel_delete(struct channel_data *chan);
291 static intcosa_sppp_open(struct net_device *d);
292 static intcosa_sppp_close(struct net_device *d);
293 static voidcosa_sppp_timeout(struct net_device *d);
294 static intcosa_sppp_tx(struct sk_buff *skb,struct net_device *d);
295 static char*sppp_setup_rx(struct channel_data *channel,int size);
296 static intsppp_rx_done(struct channel_data *channel);
297 static intsppp_tx_done(struct channel_data *channel,int size);
298 static intcosa_sppp_ioctl(struct net_device *dev,struct ifreq *ifr,int cmd);
299 static struct net_device_stats *cosa_net_stats(struct net_device *dev);
301 /* Character device */
302 static voidchardev_channel_init(struct channel_data *chan);
303 static char*chrdev_setup_rx(struct channel_data *channel,int size);
304 static intchrdev_rx_done(struct channel_data *channel);
305 static intchrdev_tx_done(struct channel_data *channel,int size);
306 static loff_t cosa_lseek(struct file *file, loff_t offset,int origin);
307 static ssize_t cosa_read(struct file *file,
308 char*buf,size_t count, loff_t *ppos);
309 static ssize_t cosa_write(struct file *file,
310 const char*buf,size_t count, loff_t *ppos);
311 static unsigned intcosa_poll(struct file *file, poll_table *poll);
312 static intcosa_open(struct inode *inode,struct file *file);
313 static intcosa_release(struct inode *inode,struct file *file);
314 static intcosa_chardev_ioctl(struct inode *inode,struct file *file,
315 unsigned int cmd,unsigned long arg);
316 #ifdef COSA_FASYNC_WORKING
317 static intcosa_fasync(struct inode *inode,struct file *file,int on);
318 #endif
320 static struct file_operations cosa_fops = {
321 owner: THIS_MODULE,
322 llseek: cosa_lseek,
323 read: cosa_read,
324 write: cosa_write,
325 poll: cosa_poll,
326 ioctl: cosa_chardev_ioctl,
327 open: cosa_open,
328 release: cosa_release,
329 #ifdef COSA_FASYNC_WORKING
330 fasync: cosa_fasync,
331 #endif
334 /* Ioctls */
335 static intcosa_start(struct cosa_data *cosa,int address);
336 static intcosa_reset(struct cosa_data *cosa);
337 static intcosa_download(struct cosa_data *cosa,struct cosa_download *d);
338 static intcosa_readmem(struct cosa_data *cosa,struct cosa_download *d);
340 /* COSA/SRP ROM monitor */
341 static intdownload(struct cosa_data *cosa,char*data,int addr,int len);
342 static intstartmicrocode(struct cosa_data *cosa,int address);
343 static intreadmem(struct cosa_data *cosa,char*data,int addr,int len);
344 static intcosa_reset_and_read_id(struct cosa_data *cosa,char*id);
346 /* Auxilliary functions */
347 static intget_wait_data(struct cosa_data *cosa);
348 static intput_wait_data(struct cosa_data *cosa,int data);
349 static intputhexnumber(struct cosa_data *cosa,int number);
350 static voidput_driver_status(struct cosa_data *cosa);
351 static voidput_driver_status_nolock(struct cosa_data *cosa);
353 /* Interrupt handling */
354 static voidcosa_interrupt(int irq,void*cosa,struct pt_regs *regs);
356 /* I/O ops debugging */
357 #ifdef DEBUG_IO
358 static voiddebug_data_in(struct cosa_data *cosa,int data);
359 static voiddebug_data_out(struct cosa_data *cosa,int data);
360 static voiddebug_data_cmd(struct cosa_data *cosa,int data);
361 static voiddebug_status_in(struct cosa_data *cosa,int status);
362 static voiddebug_status_out(struct cosa_data *cosa,int status);
363 #endif
366 /* ---------- Initialization stuff ---------- */
368 static devfs_handle_t devfs_handle;
370 #ifdef MODULE
371 intinit_module(void)
372 #else
373 static int __init cosa_init(void)
374 #endif
376 int i;
378 printk(KERN_INFO "cosa v1.08 (c) 1997-2000 Jan Kasprzak <kas@fi.muni.cz>\n");
379 #ifdef CONFIG_SMP
380 printk(KERN_INFO "cosa: SMP found. Please mail any success/failure reports to the author.\n");
381 #endif
382 if(cosa_major >0) {
383 if(devfs_register_chrdev(cosa_major,"cosa", &cosa_fops)) {
384 printk(KERN_WARNING "cosa: unable to get major %d\n",
385 cosa_major);
386 return-EIO;
388 }else{
389 if(!(cosa_major=devfs_register_chrdev(0,"cosa", &cosa_fops))) {
390 printk(KERN_WARNING "cosa: unable to register chardev\n");
391 return-EIO;
394 for(i=0; i<MAX_CARDS; i++)
395 cosa_cards[i].num = -1;
396 for(i=0; io[i] !=0&& i < MAX_CARDS; i++)
397 cosa_probe(io[i], irq[i], dma[i]);
398 devfs_handle =devfs_mk_dir(NULL,"cosa", NULL);
399 devfs_register_series(devfs_handle,"%u", nr_cards, DEVFS_FL_DEFAULT,
400 cosa_major,0,
401 S_IFCHR | S_IRUSR | S_IWUSR,
402 &cosa_fops, NULL);
403 if(!nr_cards) {
404 printk(KERN_WARNING "cosa: no devices found.\n");
405 devfs_unregister_chrdev(cosa_major,"cosa");
406 return-ENODEV;
408 return0;
411 #ifdef MODULE
412 voidcleanup_module(void)
414 struct cosa_data *cosa;
415 printk(KERN_INFO "Unloading the cosa module\n");
417 devfs_unregister(devfs_handle);
418 for(cosa=cosa_cards; nr_cards--; cosa++) {
419 int i;
420 /* Clean up the per-channel data */
421 for(i=0; i<cosa->nchannels; i++) {
422 /* Chardev driver has no alloc'd per-channel data */
423 sppp_channel_delete(cosa->chan+i);
425 /* Clean up the per-card data */
426 kfree(cosa->chan);
427 kfree(cosa->bouncebuf);
428 free_irq(cosa->irq, cosa);
429 free_dma(cosa->dma);
430 release_region(cosa->datareg,is_8bit(cosa)?2:4);
432 devfs_unregister_chrdev(cosa_major,"cosa");
434 #endif
437 * This function should register all the net devices needed for the
438 * single channel.
440 static __inline__ voidchannel_init(struct channel_data *chan)
442 sprintf(chan->name,"cosa%dc%d", chan->cosa->num, chan->num);
444 /* Initialize the chardev data structures */
445 chardev_channel_init(chan);
447 /* Register the sppp interface */
448 sppp_channel_init(chan);
451 static intcosa_probe(int base,int irq,int dma)
453 struct cosa_data *cosa = cosa_cards+nr_cards;
454 int i;
456 memset(cosa,0,sizeof(struct cosa_data));
458 /* Checking validity of parameters: */
459 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
460 if((irq >=0&& irq <2) || irq >15|| (irq <10&& irq >7)) {
461 printk(KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
462 return-1;
464 /* I/O address should be between 0x100 and 0x3ff and should be
465 * multiple of 8. */
466 if(base <0x100|| base >0x3ff|| base &0x7) {
467 printk(KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
468 base);
469 return-1;
471 /* DMA should be 0,1 or 3-7 */
472 if(dma <0|| dma ==4|| dma >7) {
473 printk(KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
474 return-1;
476 /* and finally, on 16-bit COSA DMA should be 4-7 and
477 * I/O base should not be multiple of 0x10 */
478 if(((base &0x8) && dma <4) || (!(base &0x8) && dma >3)) {
479 printk(KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
480 " (base=0x%x, dma=%d)\n", base, dma);
481 return-1;
484 cosa->dma = dma;
485 cosa->datareg = base;
486 cosa->statusreg =is_8bit(cosa)?base+1:base+2;
487 spin_lock_init(&cosa->lock);
489 if(check_region(base,is_8bit(cosa)?2:4))
490 return-1;
492 if(cosa_reset_and_read_id(cosa, cosa->id_string) <0) {
493 printk(KERN_DEBUG "cosa: probe at 0x%x failed.\n", base);
494 return-1;
497 /* Test the validity of identification string */
498 if(!strncmp(cosa->id_string,"SRP",3))
499 cosa->type ="srp";
500 else if(!strncmp(cosa->id_string,"COSA",4))
501 cosa->type =is_8bit(cosa)?"cosa8":"cosa16";
502 else{
503 /* Print a warning only if we are not autoprobing */
504 #ifndef COSA_ISA_AUTOPROBE
505 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
506 base);
507 #endif
508 return-1;
511 /* Now do IRQ autoprobe */
512 if(irq <0) {
513 unsigned long irqs;
514 /* printk(KERN_INFO "IRQ autoprobe\n"); */
515 sti();
516 irqs =probe_irq_on();
518 * Enable interrupt on tx buffer empty (it sure is)
519 * really sure ?
520 * FIXME: When this code is not used as module, we should
521 * probably call udelay() instead of the interruptible sleep.
523 current->state = TASK_INTERRUPTIBLE;
524 cosa_putstatus(cosa, SR_TX_INT_ENA);
525 schedule_timeout(30);
526 current->state = TASK_RUNNING;
527 irq =probe_irq_off(irqs);
528 /* Disable all IRQs from the card */
529 cosa_putstatus(cosa,0);
530 /* Empty the received data register */
531 cosa_getdata8(cosa);
533 if(irq <0) {
534 printk(KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
535 irq, cosa->datareg);
536 return-1;
538 if(irq ==0) {
539 printk(KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
540 cosa->datareg);
541 /* return -1; */
545 cosa->irq = irq;
546 cosa->num = nr_cards;
547 cosa->usage =0;
548 cosa->nchannels =2;/* FIXME: how to determine this? */
550 request_region(base,is_8bit(cosa)?2:4, cosa->type);
551 if(request_irq(cosa->irq, cosa_interrupt,0, cosa->type, cosa))
552 goto bad1;
553 if(request_dma(cosa->dma, cosa->type)) {
554 free_irq(cosa->irq, cosa);
555 bad1:release_region(cosa->datareg,is_8bit(cosa)?2:4);
556 printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
557 cosa->num);
558 return-1;
561 cosa->bouncebuf =kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
562 sprintf(cosa->name,"cosa%d", cosa->num);
564 /* Initialize the per-channel data */
565 cosa->chan =kmalloc(sizeof(struct channel_data)*cosa->nchannels,
566 GFP_KERNEL);
567 memset(cosa->chan,0,sizeof(struct channel_data)*cosa->nchannels);
568 for(i=0; i<cosa->nchannels; i++) {
569 cosa->chan[i].cosa = cosa;
570 cosa->chan[i].num = i;
571 channel_init(cosa->chan+i);
574 printk(KERN_INFO "cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
575 cosa->num, cosa->id_string, cosa->type,
576 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
578 return nr_cards++;
582 /*---------- SPPP/HDLC netdevice ---------- */
584 static voidsppp_channel_init(struct channel_data *chan)
586 struct net_device *d;
587 chan->if_ptr = &chan->pppdev;
588 chan->pppdev.dev =kmalloc(sizeof(struct net_device), GFP_KERNEL);
589 memset(chan->pppdev.dev,0,sizeof(struct net_device));
590 sppp_attach(&chan->pppdev);
591 d=chan->pppdev.dev;
592 strcpy(d->name, chan->name);
593 d->base_addr = chan->cosa->datareg;
594 d->irq = chan->cosa->irq;
595 d->dma = chan->cosa->dma;
596 d->priv = chan;
597 d->init = NULL;
598 d->open = cosa_sppp_open;
599 d->stop = cosa_sppp_close;
600 d->hard_start_xmit = cosa_sppp_tx;
601 d->do_ioctl = cosa_sppp_ioctl;
602 d->get_stats = cosa_net_stats;
603 d->tx_timeout = cosa_sppp_timeout;
604 d->watchdog_timeo = TX_TIMEOUT;
605 if(register_netdev(d) == -1) {
606 printk(KERN_WARNING "%s: register_netdev failed.\n", d->name);
607 sppp_detach(chan->pppdev.dev);
608 return;
612 static voidsppp_channel_delete(struct channel_data *chan)
614 sppp_detach(chan->pppdev.dev);
615 unregister_netdev(chan->pppdev.dev);
618 static intcosa_sppp_open(struct net_device *d)
620 struct channel_data *chan = d->priv;
621 int err, flags;
623 if(!(chan->cosa->firmware_status & COSA_FW_START)) {
624 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
625 chan->cosa->name, chan->cosa->firmware_status);
626 return-EPERM;
628 spin_lock_irqsave(&chan->cosa->lock, flags);
629 if(chan->usage !=0) {
630 printk(KERN_WARNING "%s: sppp_open called with usage count %d\n",
631 chan->name, chan->usage);
632 spin_unlock_irqrestore(&chan->cosa->lock, flags);
633 return-EBUSY;
635 chan->setup_rx = sppp_setup_rx;
636 chan->tx_done = sppp_tx_done;
637 chan->rx_done = sppp_rx_done;
638 chan->usage=-1;
639 chan->cosa->usage++;
640 MOD_INC_USE_COUNT;
641 spin_unlock_irqrestore(&chan->cosa->lock, flags);
643 err =sppp_open(d);
644 if(err) {
645 spin_lock_irqsave(&chan->cosa->lock, flags);
646 chan->usage=0;
647 chan->cosa->usage--;
648 MOD_DEC_USE_COUNT;
650 spin_unlock_irqrestore(&chan->cosa->lock, flags);
651 return err;
654 netif_start_queue(d);
655 cosa_enable_rx(chan);
656 return0;
659 static intcosa_sppp_tx(struct sk_buff *skb,struct net_device *dev)
661 struct channel_data *chan = dev->priv;
663 netif_stop_queue(dev);
665 chan->tx_skb = skb;
666 cosa_start_tx(chan, skb->data, skb->len);
667 return0;
670 static voidcosa_sppp_timeout(struct net_device *dev)
672 struct channel_data *chan = dev->priv;
674 if(test_bit(RXBIT, &chan->cosa->rxtx)) {
675 chan->stats.rx_errors++;
676 chan->stats.rx_missed_errors++;
677 }else{
678 chan->stats.tx_errors++;
679 chan->stats.tx_aborted_errors++;
681 cosa_kick(chan->cosa);
682 if(chan->tx_skb) {
683 dev_kfree_skb(chan->tx_skb);
684 chan->tx_skb =0;
686 netif_wake_queue(dev);
689 static intcosa_sppp_close(struct net_device *d)
691 struct channel_data *chan = d->priv;
692 int flags;
694 netif_stop_queue(d);
695 sppp_close(d);
696 cosa_disable_rx(chan);
697 spin_lock_irqsave(&chan->cosa->lock, flags);
698 if(chan->rx_skb) {
699 kfree_skb(chan->rx_skb);
700 chan->rx_skb =0;
702 if(chan->tx_skb) {
703 kfree_skb(chan->tx_skb);
704 chan->tx_skb =0;
706 chan->usage=0;
707 chan->cosa->usage--;
708 MOD_DEC_USE_COUNT;
709 spin_unlock_irqrestore(&chan->cosa->lock, flags);
710 return0;
713 static char*sppp_setup_rx(struct channel_data *chan,int size)
716 * We can safely fall back to non-dma-able memory, because we have
717 * the cosa->bouncebuf pre-allocated.
719 if(chan->rx_skb)
720 kfree_skb(chan->rx_skb);
721 chan->rx_skb =dev_alloc_skb(size);
722 if(chan->rx_skb == NULL) {
723 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
724 chan->name);
725 chan->stats.rx_dropped++;
726 return NULL;
728 chan->pppdev.dev->trans_start = jiffies;
729 returnskb_put(chan->rx_skb, size);
732 static intsppp_rx_done(struct channel_data *chan)
734 if(!chan->rx_skb) {
735 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
736 chan->name);
737 chan->stats.rx_errors++;
738 chan->stats.rx_frame_errors++;
739 return0;
741 chan->rx_skb->protocol =htons(ETH_P_WAN_PPP);
742 chan->rx_skb->dev = chan->pppdev.dev;
743 chan->rx_skb->mac.raw = chan->rx_skb->data;
744 chan->stats.rx_packets++;
745 chan->stats.rx_bytes += chan->cosa->rxsize;
746 netif_rx(chan->rx_skb);
747 chan->rx_skb =0;
748 chan->pppdev.dev->trans_start = jiffies;
749 return0;
752 /* ARGSUSED */
753 static intsppp_tx_done(struct channel_data *chan,int size)
755 if(!chan->tx_skb) {
756 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
757 chan->name);
758 chan->stats.tx_errors++;
759 chan->stats.tx_aborted_errors++;
760 return1;
762 dev_kfree_skb_irq(chan->tx_skb);
763 chan->tx_skb =0;
764 chan->stats.tx_packets++;
765 chan->stats.tx_bytes += size;
766 netif_wake_queue(chan->pppdev.dev);
767 return1;
770 static struct net_device_stats *cosa_net_stats(struct net_device *dev)
772 struct channel_data *chan = dev->priv;
773 return&chan->stats;
777 /*---------- Character device ---------- */
779 static voidchardev_channel_init(struct channel_data *chan)
781 init_MUTEX(&chan->rsem);
782 init_MUTEX(&chan->wsem);
785 static loff_t cosa_lseek(struct file * file, loff_t offset,int origin)
787 return-ESPIPE;
790 static ssize_t cosa_read(struct file *file,
791 char*buf,size_t count, loff_t *ppos)
793 DECLARE_WAITQUEUE(wait, current);
794 int flags;
795 struct channel_data *chan = (struct channel_data *)file->private_data;
796 struct cosa_data *cosa = chan->cosa;
797 char*kbuf;
799 if(!(cosa->firmware_status & COSA_FW_START)) {
800 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
801 cosa->name, cosa->firmware_status);
802 return-EPERM;
804 if(down_interruptible(&chan->rsem))
805 return-ERESTARTSYS;
807 if((chan->rxdata =kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
808 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
809 up(&chan->rsem);
810 return-ENOMEM;
813 chan->rx_status =0;
814 cosa_enable_rx(chan);
815 spin_lock_irqsave(&cosa->lock, flags);
816 add_wait_queue(&chan->rxwaitq, &wait);
817 while(!chan->rx_status) {
818 current->state = TASK_INTERRUPTIBLE;
819 spin_unlock_irqrestore(&cosa->lock, flags);
820 schedule();
821 spin_lock_irqsave(&cosa->lock, flags);
822 if(signal_pending(current) && chan->rx_status ==0) {
823 chan->rx_status =1;
824 remove_wait_queue(&chan->rxwaitq, &wait);
825 current->state = TASK_RUNNING;
826 spin_unlock_irqrestore(&cosa->lock, flags);
827 up(&chan->rsem);
828 return-ERESTARTSYS;
831 remove_wait_queue(&chan->rxwaitq, &wait);
832 current->state = TASK_RUNNING;
833 kbuf = chan->rxdata;
834 count = chan->rxsize;
835 spin_unlock_irqrestore(&cosa->lock, flags);
836 up(&chan->rsem);
838 if(copy_to_user(buf, kbuf, count)) {
839 kfree(buf);
840 return-EFAULT;
842 kfree(kbuf);
843 return count;
846 static char*chrdev_setup_rx(struct channel_data *chan,int size)
848 /* Expect size <= COSA_MTU */
849 chan->rxsize = size;
850 return chan->rxdata;
853 static intchrdev_rx_done(struct channel_data *chan)
855 if(chan->rx_status) {/* Reader has died */
856 kfree(chan->rxdata);
857 up(&chan->wsem);
859 chan->rx_status =1;
860 wake_up_interruptible(&chan->rxwaitq);
861 return1;
865 static ssize_t cosa_write(struct file *file,
866 const char*buf,size_t count, loff_t *ppos)
868 DECLARE_WAITQUEUE(wait, current);
869 struct channel_data *chan = (struct channel_data *)file->private_data;
870 struct cosa_data *cosa = chan->cosa;
871 unsigned int flags;
872 char*kbuf;
874 if(!(cosa->firmware_status & COSA_FW_START)) {
875 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
876 cosa->name, cosa->firmware_status);
877 return-EPERM;
879 if(down_interruptible(&chan->wsem))
880 return-ERESTARTSYS;
882 if(count > COSA_MTU)
883 count = COSA_MTU;
885 /* Allocate the buffer */
886 if((kbuf =kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
887 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
888 cosa->name);
889 up(&chan->wsem);
890 return-ENOMEM;
892 if(copy_from_user(kbuf, buf, count)) {
893 up(&chan->wsem);
894 kfree(kbuf);
895 return-EFAULT;
897 chan->tx_status=0;
898 cosa_start_tx(chan, kbuf, count);
900 spin_lock_irqsave(&cosa->lock, flags);
901 add_wait_queue(&chan->txwaitq, &wait);
902 while(!chan->tx_status) {
903 current->state = TASK_INTERRUPTIBLE;
904 spin_unlock_irqrestore(&cosa->lock, flags);
905 schedule();
906 spin_lock_irqsave(&cosa->lock, flags);
907 if(signal_pending(current) && chan->tx_status ==0) {
908 chan->tx_status =1;
909 remove_wait_queue(&chan->txwaitq, &wait);
910 current->state = TASK_RUNNING;
911 chan->tx_status =1;
912 spin_unlock_irqrestore(&cosa->lock, flags);
913 return-ERESTARTSYS;
916 remove_wait_queue(&chan->txwaitq, &wait);
917 current->state = TASK_RUNNING;
918 up(&chan->wsem);
919 spin_unlock_irqrestore(&cosa->lock, flags);
920 kfree(kbuf);
921 return count;
924 static intchrdev_tx_done(struct channel_data *chan,int size)
926 if(chan->tx_status) {/* Writer was interrupted */
927 kfree(chan->txbuf);
928 up(&chan->wsem);
930 chan->tx_status =1;
931 wake_up_interruptible(&chan->txwaitq);
932 return1;
935 static unsigned intcosa_poll(struct file *file, poll_table *poll)
937 printk(KERN_INFO "cosa_poll is here\n");
938 return0;
941 static intcosa_open(struct inode *inode,struct file *file)
943 struct cosa_data *cosa;
944 struct channel_data *chan;
945 unsigned long flags;
946 int n;
948 if((n=MINOR(file->f_dentry->d_inode->i_rdev)>>CARD_MINOR_BITS)
949 >= nr_cards)
950 return-ENODEV;
951 cosa = cosa_cards+n;
953 if((n=MINOR(file->f_dentry->d_inode->i_rdev)
954 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels)
955 return-ENODEV;
956 chan = cosa->chan + n;
958 file->private_data = chan;
960 spin_lock_irqsave(&cosa->lock, flags);
962 if(chan->usage <0) {/* in netdev mode */
963 spin_unlock_irqrestore(&cosa->lock, flags);
964 return-EBUSY;
966 cosa->usage++;
967 chan->usage++;
969 chan->tx_done = chrdev_tx_done;
970 chan->setup_rx = chrdev_setup_rx;
971 chan->rx_done = chrdev_rx_done;
972 spin_unlock_irqrestore(&cosa->lock, flags);
973 return0;
976 static intcosa_release(struct inode *inode,struct file *file)
978 struct channel_data *channel = (struct channel_data *)file->private_data;
979 struct cosa_data *cosa;
980 unsigned long flags;
982 lock_kernel();
983 cosa = channel->cosa;
984 spin_lock_irqsave(&cosa->lock, flags);
985 cosa->usage--;
986 channel->usage--;
987 spin_unlock_irqrestore(&cosa->lock, flags);
988 unlock_kernel();
989 return0;
992 #ifdef COSA_FASYNC_WORKING
993 static struct fasync_struct *fasync[256] = { NULL, };
995 /* To be done ... */
996 static intcosa_fasync(struct inode *inode,struct file *file,int on)
998 int port =MINOR(inode->i_rdev);
999 int rv =fasync_helper(inode, file, on, &fasync[port]);
1000 return rv <0? rv :0;
1002 #endif
1005 /* ---------- Ioctls ---------- */
1008 * Ioctl subroutines can safely be made inline, because they are called
1009 * only from cosa_ioctl().
1011 staticinlineintcosa_reset(struct cosa_data *cosa)
1013 char idstring[COSA_MAX_ID_STRING];
1014 if(cosa->usage >1)
1015 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1016 cosa->num, cosa->usage);
1017 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1018 if(cosa_reset_and_read_id(cosa, idstring) <0) {
1019 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1020 return-EIO;
1022 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1023 idstring);
1024 cosa->firmware_status |= COSA_FW_RESET;
1025 return0;
1028 /* High-level function to download data into COSA memory. Calls download() */
1029 staticinlineintcosa_download(struct cosa_data *cosa,struct cosa_download *d)
1031 int i;
1032 int addr, len;
1033 char*code;
1035 if(cosa->usage >1)
1036 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1037 cosa->name, cosa->usage);
1038 if(!(cosa->firmware_status & COSA_FW_RESET)) {
1039 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1040 cosa->name, cosa->firmware_status);
1041 return-EPERM;
1044 if(get_user(addr, &(d->addr)) ||
1045 __get_user(len, &(d->len)) ||
1046 __get_user(code, &(d->code)))
1047 return-EFAULT;
1049 if(d->addr <0|| d->addr > COSA_MAX_FIRMWARE_SIZE)
1050 return-EINVAL;
1051 if(d->len <0|| d->len > COSA_MAX_FIRMWARE_SIZE)
1052 return-EINVAL;
1054 /* If something fails, force the user to reset the card */
1055 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1057 if((i=download(cosa, d->code, len, addr)) <0) {
1058 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1059 cosa->num, i);
1060 return-EIO;
1062 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1063 cosa->num, len, addr);
1064 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1065 return0;
1068 /* High-level function to read COSA memory. Calls readmem() */
1069 staticinlineintcosa_readmem(struct cosa_data *cosa,struct cosa_download *d)
1071 int i;
1072 int addr, len;
1073 char*code;
1075 if(cosa->usage >1)
1076 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1077 "cosa->usage > 1 (%d). Odd things may happen.\n",
1078 cosa->num, cosa->usage);
1079 if(!(cosa->firmware_status & COSA_FW_RESET)) {
1080 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1081 cosa->name, cosa->firmware_status);
1082 return-EPERM;
1085 if(get_user(addr, &(d->addr)) ||
1086 __get_user(len, &(d->len)) ||
1087 __get_user(code, &(d->code)))
1088 return-EFAULT;
1090 /* If something fails, force the user to reset the card */
1091 cosa->firmware_status &= ~COSA_FW_RESET;
1093 if((i=readmem(cosa, d->code, len, addr)) <0) {
1094 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1095 cosa->num, i);
1096 return-EIO;
1098 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1099 cosa->num, len, addr);
1100 cosa->firmware_status |= COSA_FW_RESET;
1101 return0;
1104 /* High-level function to start microcode. Calls startmicrocode(). */
1105 staticinlineintcosa_start(struct cosa_data *cosa,int address)
1107 int i;
1109 if(cosa->usage >1)
1110 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1111 cosa->num, cosa->usage);
1113 if((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1114 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1115 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1116 cosa->name, cosa->firmware_status);
1117 return-EPERM;
1119 cosa->firmware_status &= ~COSA_FW_RESET;
1120 if((i=startmicrocode(cosa, address)) <0) {
1121 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1122 cosa->num, address, i);
1123 return-EIO;
1125 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1126 cosa->num, address);
1127 cosa->startaddr = address;
1128 cosa->firmware_status |= COSA_FW_START;
1129 return0;
1132 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1133 staticinlineintcosa_getidstr(struct cosa_data *cosa,char*string)
1135 int l =strlen(cosa->id_string)+1;
1136 if(copy_to_user(string, cosa->id_string, l))
1137 return-EFAULT;
1138 return l;
1141 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1142 staticinlineintcosa_gettype(struct cosa_data *cosa,char*string)
1144 int l =strlen(cosa->type)+1;
1145 if(copy_to_user(string, cosa->type, l))
1146 return-EFAULT;
1147 return l;
1150 static intcosa_ioctl_common(struct cosa_data *cosa,
1151 struct channel_data *channel,unsigned int cmd,unsigned long arg)
1153 switch(cmd) {
1154 case COSAIORSET:/* Reset the device */
1155 if(!capable(CAP_NET_ADMIN))
1156 return-EACCES;
1157 returncosa_reset(cosa);
1158 case COSAIOSTRT:/* Start the firmware */
1159 if(!capable(CAP_SYS_RAWIO))
1160 return-EACCES;
1161 returncosa_start(cosa, arg);
1162 case COSAIODOWNLD:/* Download the firmware */
1163 if(!capable(CAP_SYS_RAWIO))
1164 return-EACCES;
1165 returncosa_download(cosa, (struct cosa_download *)arg);
1166 case COSAIORMEM:
1167 if(!capable(CAP_SYS_RAWIO))
1168 return-EACCES;
1169 returncosa_readmem(cosa, (struct cosa_download *)arg);
1170 case COSAIORTYPE:
1171 returncosa_gettype(cosa, (char*)arg);
1172 case COSAIORIDSTR:
1173 returncosa_getidstr(cosa, (char*)arg);
1175 * These two are _very_ugly_hack_(tm). Don't even look at this.
1176 * Implementing this saved me few reboots after some process segfaulted
1177 * inside this module.
1179 #ifdef MODULE
1180 #if 0
1181 case COSAIOMINC:
1182 MOD_INC_USE_COUNT;
1183 return0;
1184 case COSAIOMDEC:
1185 MOD_DEC_USE_COUNT;
1186 return0;
1187 #endif
1188 #endif
1189 case COSAIONRCARDS:
1190 return nr_cards;
1191 case COSAIONRCHANS:
1192 return cosa->nchannels;
1193 case COSAIOBMSET:
1194 if(!capable(CAP_SYS_RAWIO))
1195 return-EACCES;
1196 if(is_8bit(cosa))
1197 return-EINVAL;
1198 if(arg != COSA_BM_OFF && arg != COSA_BM_ON)
1199 return-EINVAL;
1200 cosa->busmaster = arg;
1201 return0;
1202 case COSAIOBMGET:
1203 return cosa->busmaster;
1205 return-ENOIOCTLCMD;
1208 static intcosa_sppp_ioctl(struct net_device *dev,struct ifreq *ifr,
1209 int cmd)
1211 int rv;
1212 struct channel_data *chan = (struct channel_data *)dev->priv;
1213 rv =cosa_ioctl_common(chan->cosa, chan, cmd, (unsigned long)ifr->ifr_data);
1214 if(rv == -ENOIOCTLCMD) {
1215 returnsppp_do_ioctl(dev, ifr, cmd);
1217 return rv;
1220 static intcosa_chardev_ioctl(struct inode *inode,struct file *file,
1221 unsigned int cmd,unsigned long arg)
1223 struct channel_data *channel = (struct channel_data *)file->private_data;
1224 struct cosa_data *cosa = channel->cosa;
1225 returncosa_ioctl_common(cosa, channel, cmd, arg);
1229 /*---------- HW layer interface ---------- */
1232 * The higher layer can bind itself to the HW layer by setting the callbacks
1233 * in the channel_data structure and by using these routines.
1235 static voidcosa_enable_rx(struct channel_data *chan)
1237 struct cosa_data *cosa = chan->cosa;
1239 if(!test_and_set_bit(chan->num, &cosa->rxbitmap))
1240 put_driver_status(cosa);
1243 static voidcosa_disable_rx(struct channel_data *chan)
1245 struct cosa_data *cosa = chan->cosa;
1247 if(test_and_clear_bit(chan->num, &cosa->rxbitmap))
1248 put_driver_status(cosa);
1252 * FIXME: This routine probably should check for cosa_start_tx() called when
1253 * the previous transmit is still unfinished. In this case the non-zero
1254 * return value should indicate to the caller that the queuing(sp?) up
1255 * the transmit has failed.
1257 static intcosa_start_tx(struct channel_data *chan,char*buf,int len)
1259 struct cosa_data *cosa = chan->cosa;
1260 int flags;
1261 #ifdef DEBUG_DATA
1262 int i;
1264 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1265 chan->num, len);
1266 for(i=0; i<len; i++)
1267 printk(" %02x", buf[i]&0xff);
1268 printk("\n");
1269 #endif
1270 spin_lock_irqsave(&cosa->lock, flags);
1271 chan->txbuf = buf;
1272 chan->txsize = len;
1273 if(len > COSA_MTU)
1274 chan->txsize = COSA_MTU;
1275 spin_unlock_irqrestore(&cosa->lock, flags);
1277 /* Tell the firmware we are ready */
1278 set_bit(chan->num, &cosa->txbitmap);
1279 put_driver_status(cosa);
1281 return0;
1284 static voidput_driver_status(struct cosa_data *cosa)
1286 unsigned flags=0;
1287 int status;
1289 spin_lock_irqsave(&cosa->lock, flags);
1291 status = (cosa->rxbitmap ? DRIVER_RX_READY :0)
1292 | (cosa->txbitmap ? DRIVER_TX_READY :0)
1293 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1294 &DRIVER_TXMAP_MASK :0);
1295 if(!cosa->rxtx) {
1296 if(cosa->rxbitmap|cosa->txbitmap) {
1297 if(!cosa->enabled) {
1298 cosa_putstatus(cosa, SR_RX_INT_ENA);
1299 #ifdef DEBUG_IO
1300 debug_status_out(cosa, SR_RX_INT_ENA);
1301 #endif
1302 cosa->enabled =1;
1304 }else if(cosa->enabled) {
1305 cosa->enabled =0;
1306 cosa_putstatus(cosa,0);
1307 #ifdef DEBUG_IO
1308 debug_status_out(cosa,0);
1309 #endif
1311 cosa_putdata8(cosa, status);
1312 #ifdef DEBUG_IO
1313 debug_data_cmd(cosa, status);
1314 #endif
1316 spin_unlock_irqrestore(&cosa->lock, flags);
1319 static voidput_driver_status_nolock(struct cosa_data *cosa)
1321 int status;
1323 status = (cosa->rxbitmap ? DRIVER_RX_READY :0)
1324 | (cosa->txbitmap ? DRIVER_TX_READY :0)
1325 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1326 &DRIVER_TXMAP_MASK :0);
1328 if(cosa->rxbitmap|cosa->txbitmap) {
1329 cosa_putstatus(cosa, SR_RX_INT_ENA);
1330 #ifdef DEBUG_IO
1331 debug_status_out(cosa, SR_RX_INT_ENA);
1332 #endif
1333 cosa->enabled =1;
1334 }else{
1335 cosa_putstatus(cosa,0);
1336 #ifdef DEBUG_IO
1337 debug_status_out(cosa,0);
1338 #endif
1339 cosa->enabled =0;
1341 cosa_putdata8(cosa, status);
1342 #ifdef DEBUG_IO
1343 debug_data_cmd(cosa, status);
1344 #endif
1348 * The "kickme" function: When the DMA times out, this is called to
1349 * clean up the driver status.
1350 * FIXME: Preliminary support, the interface is probably wrong.
1352 static voidcosa_kick(struct cosa_data *cosa)
1354 unsigned flags, flags1;
1355 char*s ="(probably) IRQ";
1357 if(test_bit(RXBIT, &cosa->rxtx))
1358 s ="RX DMA";
1359 if(test_bit(TXBIT, &cosa->rxtx))
1360 s ="TX DMA";
1362 printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
1363 spin_lock_irqsave(&cosa->lock, flags);
1364 cosa->rxtx =0;
1366 flags1 =claim_dma_lock();
1367 disable_dma(cosa->dma);
1368 clear_dma_ff(cosa->dma);
1369 release_dma_lock(flags1);
1371 /* FIXME: Anything else? */
1372 udelay(100);
1373 cosa_putstatus(cosa,0);
1374 udelay(100);
1375 (void)cosa_getdata8(cosa);
1376 udelay(100);
1377 cosa_putdata8(cosa,0);
1378 udelay(100);
1379 put_driver_status_nolock(cosa);
1380 spin_unlock_irqrestore(&cosa->lock, flags);
1384 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1385 * physical memory and doesn't span the 64k boundary. For now it seems
1386 * SKB's never do this, but we'll check this anyway.
1388 static intcosa_dma_able(struct channel_data *chan,char*buf,int len)
1390 static int count;
1391 unsigned long b = (unsigned long)buf;
1392 if(b+len >= MAX_DMA_ADDRESS)
1393 return0;
1394 if((b^ (b+len)) &0x10000) {
1395 if(count++ <5)
1396 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1397 chan->name);
1398 return0;
1400 return1;
1404 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1407 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1408 * drivers need to say 4-digit hex number meaning start address of the microcode
1409 * separated by a single space. Monitor replies by saying " =". Now driver
1410 * has to write 4-digit hex number meaning the last byte address ended
1411 * by a single space. Monitor has to reply with a space. Now the download
1412 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1414 static intdownload(struct cosa_data *cosa,char*microcode,int length,int address)
1416 int i;
1418 if(put_wait_data(cosa,'w') == -1)return-1;
1419 if((i=get_wait_data(cosa)) !='w') {printk("dnld: 0x%04x\n",i);return-2;}
1420 if(get_wait_data(cosa) !='=')return-3;
1422 if(puthexnumber(cosa, address) <0)return-4;
1423 if(put_wait_data(cosa,' ') == -1)return-10;
1424 if(get_wait_data(cosa) !=' ')return-11;
1425 if(get_wait_data(cosa) !='=')return-12;
1427 if(puthexnumber(cosa, address+length-1) <0)return-13;
1428 if(put_wait_data(cosa,' ') == -1)return-18;
1429 if(get_wait_data(cosa) !=' ')return-19;
1431 while(length--) {
1432 char c;
1433 #ifndef SRP_DOWNLOAD_AT_BOOT
1434 if(get_user(c, microcode))
1435 return-23;/* ??? */
1436 #else
1437 c = *microcode;
1438 #endif
1439 if(put_wait_data(cosa, c) == -1)
1440 return-20;
1441 microcode++;
1444 if(get_wait_data(cosa) !='\r')return-21;
1445 if(get_wait_data(cosa) !='\n')return-22;
1446 if(get_wait_data(cosa) !='.')return-23;
1447 #if 0
1448 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1449 #endif
1450 return0;
1455 * Starting microcode is done via the "g" command of the SRP monitor.
1456 * The chat should be the following: "g" "g=" "<addr><CR>"
1457 * "<CR><CR><LF><CR><LF>".
1459 static intstartmicrocode(struct cosa_data *cosa,int address)
1461 if(put_wait_data(cosa,'g') == -1)return-1;
1462 if(get_wait_data(cosa) !='g')return-2;
1463 if(get_wait_data(cosa) !='=')return-3;
1465 if(puthexnumber(cosa, address) <0)return-4;
1466 if(put_wait_data(cosa,'\r') == -1)return-5;
1468 if(get_wait_data(cosa) !='\r')return-6;
1469 if(get_wait_data(cosa) !='\r')return-7;
1470 if(get_wait_data(cosa) !='\n')return-8;
1471 if(get_wait_data(cosa) !='\r')return-9;
1472 if(get_wait_data(cosa) !='\n')return-10;
1473 #if 0
1474 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1475 #endif
1476 return0;
1480 * Reading memory is done via the "r" command of the SRP monitor.
1481 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1482 * Then driver can read the data and the conversation is finished
1483 * by SRP monitor sending "<CR><LF>." (dot at the end).
1485 * This routine is not needed during the normal operation and serves
1486 * for debugging purposes only.
1488 static intreadmem(struct cosa_data *cosa,char*microcode,int length,int address)
1490 if(put_wait_data(cosa,'r') == -1)return-1;
1491 if((get_wait_data(cosa)) !='r')return-2;
1492 if((get_wait_data(cosa)) !='=')return-3;
1494 if(puthexnumber(cosa, address) <0)return-4;
1495 if(put_wait_data(cosa,' ') == -1)return-5;
1496 if(get_wait_data(cosa) !=' ')return-6;
1497 if(get_wait_data(cosa) !='=')return-7;
1499 if(puthexnumber(cosa, address+length-1) <0)return-8;
1500 if(put_wait_data(cosa,' ') == -1)return-9;
1501 if(get_wait_data(cosa) !=' ')return-10;
1503 while(length--) {
1504 char c;
1505 int i;
1506 if((i=get_wait_data(cosa)) == -1) {
1507 printk(KERN_INFO "cosa: 0x%04x bytes remaining\n",
1508 length);
1509 return-11;
1511 c=i;
1512 #if 1
1513 if(put_user(c, microcode))
1514 return-23;/* ??? */
1515 #else
1516 *microcode = c;
1517 #endif
1518 microcode++;
1521 if(get_wait_data(cosa) !='\r')return-21;
1522 if(get_wait_data(cosa) !='\n')return-22;
1523 if(get_wait_data(cosa) !='.')return-23;
1524 #if 0
1525 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1526 #endif
1527 return0;
1531 * This function resets the device and reads the initial prompt
1532 * of the device's ROM monitor.
1534 static intcosa_reset_and_read_id(struct cosa_data *cosa,char*idstring)
1536 int i=0, id=0, prev=0, curr=0;
1538 /* Reset the card ... */
1539 cosa_putstatus(cosa,0);
1540 cosa_getdata8(cosa);
1541 cosa_putstatus(cosa, SR_RST);
1542 #ifdef MODULE
1543 current->state = TASK_INTERRUPTIBLE;
1544 schedule_timeout(HZ/2);
1545 current->state = TASK_RUNNING;
1546 #else
1547 udelay(5*100000);
1548 #endif
1549 /* Disable all IRQs from the card */
1550 cosa_putstatus(cosa,0);
1553 * Try to read the ID string. The card then prints out the
1554 * identification string ended by the "\n\x2e".
1556 * The following loop is indexed through i (instead of id)
1557 * to avoid looping forever when for any reason
1558 * the port returns '\r', '\n' or '\x2e' permanently.
1560 for(i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1561 if((curr =get_wait_data(cosa)) == -1) {
1562 return-1;
1564 curr &=0xff;
1565 if(curr !='\r'&& curr !='\n'&& curr !=0x2e)
1566 idstring[id++] = curr;
1567 if(curr ==0x2e&& prev =='\n')
1568 break;
1570 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1571 idstring[id] ='\0';
1572 return id;
1576 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1579 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1580 * bit to be set in a loop. It should be used in the exceptional cases
1581 * only (for example when resetting the card or downloading the firmware.
1583 static intget_wait_data(struct cosa_data *cosa)
1585 int retries =1000;
1587 while(--retries) {
1588 /* read data and return them */
1589 if(cosa_getstatus(cosa) & SR_RX_RDY) {
1590 short r;
1591 r =cosa_getdata8(cosa);
1592 #if 0
1593 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n",999-retries);
1594 #endif
1595 return r;
1597 /* sleep if not ready to read */
1598 current->state = TASK_INTERRUPTIBLE;
1599 schedule_timeout(1);
1601 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1602 cosa_getstatus(cosa));
1603 return-1;
1607 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1608 * bit to be set in a loop. It should be used in the exceptional cases
1609 * only (for example when resetting the card or downloading the firmware).
1611 static intput_wait_data(struct cosa_data *cosa,int data)
1613 int retries =1000;
1614 while(--retries) {
1615 /* read data and return them */
1616 if(cosa_getstatus(cosa) & SR_TX_RDY) {
1617 cosa_putdata8(cosa, data);
1618 #if 0
1619 printk(KERN_INFO "Putdata: %d retries\n",999-retries);
1620 #endif
1621 return0;
1623 #if 0
1624 /* sleep if not ready to read */
1625 current->state = TASK_INTERRUPTIBLE;
1626 schedule_timeout(1);
1627 #endif
1629 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1630 cosa->num,cosa_getstatus(cosa));
1631 return-1;
1635 * The following routine puts the hexadecimal number into the SRP monitor
1636 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1637 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1638 * (-2,-4,-6,-8) means that reading echo failed.
1640 static intputhexnumber(struct cosa_data *cosa,int number)
1642 char temp[5];
1643 int i;
1645 /* Well, I should probably replace this by something faster. */
1646 sprintf(temp,"%04X", number);
1647 for(i=0; i<4; i++) {
1648 if(put_wait_data(cosa, temp[i]) == -1) {
1649 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1650 cosa->num, i);
1651 return-1-2*i;
1653 if(get_wait_data(cosa) != temp[i]) {
1654 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1655 cosa->num, i);
1656 return-2-2*i;
1659 return0;
1663 /* ---------- Interrupt routines ---------- */
1666 * There are three types of interrupt:
1667 * At the beginning of transmit - this handled is in tx_interrupt(),
1668 * at the beginning of receive - it is in rx_interrupt() and
1669 * at the end of transmit/receive - it is the eot_interrupt() function.
1670 * These functions are multiplexed by cosa_interrupt() according to the
1671 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1672 * separate functions to make it more readable. These functions are inline,
1673 * so there should be no overhead of function call.
1675 * In the COSA bus-master mode, we need to tell the card the address of a
1676 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1677 * It's time to use the bottom half :-(
1681 * Transmit interrupt routine - called when COSA is willing to obtain
1682 * data from the OS. The most tricky part of the routine is selection
1683 * of channel we (OS) want to send packet for. For SRP we should probably
1684 * use the round-robin approach. The newer COSA firmwares have a simple
1685 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1686 * channel 0 or 1 doesn't want to receive data.
1688 * It seems there is a bug in COSA firmware (need to trace it further):
1689 * When the driver status says that the kernel has no more data for transmit
1690 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1691 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1692 * the TX interrupt but does not mark the channel as ready-to-transmit.
1693 * The fix seems to be to push the packet to COSA despite its request.
1694 * We first try to obey the card's opinion, and then fall back to forced TX.
1696 staticinlinevoidtx_interrupt(struct cosa_data *cosa,int status)
1698 unsigned long flags, flags1;
1699 #ifdef DEBUG_IRQS
1700 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1701 cosa->num, status);
1702 #endif
1703 spin_lock_irqsave(&cosa->lock, flags);
1704 set_bit(TXBIT, &cosa->rxtx);
1705 if(!test_bit(IRQBIT, &cosa->rxtx)) {
1706 /* flow control, see the comment above */
1707 int i=0;
1708 if(!cosa->txbitmap) {
1709 printk(KERN_WARNING "%s: No channel wants data "
1710 "in TX IRQ. Expect DMA timeout.",
1711 cosa->name);
1712 put_driver_status_nolock(cosa);
1713 clear_bit(TXBIT, &cosa->rxtx);
1714 spin_unlock_irqrestore(&cosa->lock, flags);
1715 return;
1717 while(1) {
1718 cosa->txchan++;
1719 i++;
1720 if(cosa->txchan >= cosa->nchannels)
1721 cosa->txchan =0;
1722 if(!(cosa->txbitmap & (1<<cosa->txchan)))
1723 continue;
1724 if(~status & (1<< (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1725 break;
1726 /* in second pass, accept first ready-to-TX channel */
1727 if(i > cosa->nchannels) {
1728 /* Can be safely ignored */
1729 #ifdef DEBUG_IRQS
1730 printk(KERN_DEBUG "%s: Forcing TX "
1731 "to not-ready channel %d\n",
1732 cosa->name, cosa->txchan);
1733 #endif
1734 break;
1738 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1739 if(cosa_dma_able(cosa->chan+cosa->txchan,
1740 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1741 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1742 }else{
1743 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1744 cosa->txsize);
1745 cosa->txbuf = cosa->bouncebuf;
1749 if(is_8bit(cosa)) {
1750 if(!test_bit(IRQBIT, &cosa->rxtx)) {
1751 cosa_putstatus(cosa, SR_TX_INT_ENA);
1752 cosa_putdata8(cosa, ((cosa->txchan <<5) &0xe0)|
1753 ((cosa->txsize >>8) &0x1f));
1754 #ifdef DEBUG_IO
1755 debug_status_out(cosa, SR_TX_INT_ENA);
1756 debug_data_out(cosa, ((cosa->txchan <<5) &0xe0)|
1757 ((cosa->txsize >>8) &0x1f));
1758 debug_data_in(cosa,cosa_getdata8(cosa));
1759 #else
1760 cosa_getdata8(cosa);
1761 #endif
1762 set_bit(IRQBIT, &cosa->rxtx);
1763 spin_unlock_irqrestore(&cosa->lock, flags);
1764 return;
1765 }else{
1766 clear_bit(IRQBIT, &cosa->rxtx);
1767 cosa_putstatus(cosa,0);
1768 cosa_putdata8(cosa, cosa->txsize&0xff);
1769 #ifdef DEBUG_IO
1770 debug_status_out(cosa,0);
1771 debug_data_out(cosa, cosa->txsize&0xff);
1772 #endif
1774 }else{
1775 cosa_putstatus(cosa, SR_TX_INT_ENA);
1776 cosa_putdata16(cosa, ((cosa->txchan<<13) &0xe000)
1777 | (cosa->txsize &0x1fff));
1778 #ifdef DEBUG_IO
1779 debug_status_out(cosa, SR_TX_INT_ENA);
1780 debug_data_out(cosa, ((cosa->txchan<<13) &0xe000)
1781 | (cosa->txsize &0x1fff));
1782 debug_data_in(cosa,cosa_getdata8(cosa));
1783 debug_status_out(cosa,0);
1784 #else
1785 cosa_getdata8(cosa);
1786 #endif
1787 cosa_putstatus(cosa,0);
1790 if(cosa->busmaster) {
1791 unsigned long addr =virt_to_bus(cosa->txbuf);
1792 int count=0;
1793 printk(KERN_INFO "busmaster IRQ\n");
1794 while(!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1795 count++;
1796 udelay(10);
1797 if(count >1000)break;
1799 printk(KERN_INFO "status %x\n",cosa_getstatus(cosa));
1800 printk(KERN_INFO "ready after %d loops\n", count);
1801 cosa_putdata16(cosa, (addr >>16)&0xffff);
1803 count =0;
1804 while(!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1805 count++;
1806 if(count >1000)break;
1807 udelay(10);
1809 printk(KERN_INFO "ready after %d loops\n", count);
1810 cosa_putdata16(cosa, addr &0xffff);
1811 flags1 =claim_dma_lock();
1812 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1813 enable_dma(cosa->dma);
1814 release_dma_lock(flags1);
1815 }else{
1816 /* start the DMA */
1817 flags1 =claim_dma_lock();
1818 disable_dma(cosa->dma);
1819 clear_dma_ff(cosa->dma);
1820 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1821 set_dma_addr(cosa->dma,virt_to_bus(cosa->txbuf));
1822 set_dma_count(cosa->dma, cosa->txsize);
1823 enable_dma(cosa->dma);
1824 release_dma_lock(flags1);
1826 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1827 #ifdef DEBUG_IO
1828 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1829 #endif
1830 spin_unlock_irqrestore(&cosa->lock, flags);
1833 staticinlinevoidrx_interrupt(struct cosa_data *cosa,int status)
1835 unsigned long flags;
1836 #ifdef DEBUG_IRQS
1837 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1838 #endif
1840 spin_lock_irqsave(&cosa->lock, flags);
1841 set_bit(RXBIT, &cosa->rxtx);
1843 if(is_8bit(cosa)) {
1844 if(!test_bit(IRQBIT, &cosa->rxtx)) {
1845 set_bit(IRQBIT, &cosa->rxtx);
1846 put_driver_status_nolock(cosa);
1847 cosa->rxsize =cosa_getdata8(cosa) <<8;
1848 #ifdef DEBUG_IO
1849 debug_data_in(cosa, cosa->rxsize >>8);
1850 #endif
1851 spin_unlock_irqrestore(&cosa->lock, flags);
1852 return;
1853 }else{
1854 clear_bit(IRQBIT, &cosa->rxtx);
1855 cosa->rxsize |=cosa_getdata8(cosa) &0xff;
1856 #ifdef DEBUG_IO
1857 debug_data_in(cosa, cosa->rxsize &0xff);
1858 #endif
1859 #if 0
1860 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1861 cosa->num, cosa->rxsize);
1862 #endif
1864 }else{
1865 cosa->rxsize =cosa_getdata16(cosa);
1866 #ifdef DEBUG_IO
1867 debug_data_in(cosa, cosa->rxsize);
1868 #endif
1869 #if 0
1870 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1871 cosa->num, cosa->rxsize);
1872 #endif
1874 if(((cosa->rxsize &0xe000) >>13) >= cosa->nchannels) {
1875 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1876 cosa->name, cosa->rxsize);
1877 spin_unlock_irqrestore(&cosa->lock, flags);
1878 goto reject;
1880 cosa->rxchan = cosa->chan + ((cosa->rxsize &0xe000) >>13);
1881 cosa->rxsize &=0x1fff;
1882 spin_unlock_irqrestore(&cosa->lock, flags);
1884 cosa->rxbuf = NULL;
1885 if(cosa->rxchan->setup_rx)
1886 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1888 if(!cosa->rxbuf) {
1889 reject:/* Reject the packet */
1890 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1891 cosa->num, cosa->rxchan->num);
1892 cosa->rxbuf = cosa->bouncebuf;
1895 /* start the DMA */
1896 flags =claim_dma_lock();
1897 disable_dma(cosa->dma);
1898 clear_dma_ff(cosa->dma);
1899 set_dma_mode(cosa->dma, DMA_MODE_READ);
1900 if(cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize &0x1fff)) {
1901 set_dma_addr(cosa->dma,virt_to_bus(cosa->rxbuf));
1902 }else{
1903 set_dma_addr(cosa->dma,virt_to_bus(cosa->bouncebuf));
1905 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1906 enable_dma(cosa->dma);
1907 release_dma_lock(flags);
1908 spin_lock_irqsave(&cosa->lock, flags);
1909 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1910 if(!is_8bit(cosa) && (status & SR_TX_RDY))
1911 cosa_putdata8(cosa, DRIVER_RX_READY);
1912 #ifdef DEBUG_IO
1913 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1914 if(!is_8bit(cosa) && (status & SR_TX_RDY))
1915 debug_data_cmd(cosa, DRIVER_RX_READY);
1916 #endif
1917 spin_unlock_irqrestore(&cosa->lock, flags);
1920 static voidinlineeot_interrupt(struct cosa_data *cosa,int status)
1922 unsigned long flags, flags1;
1923 spin_lock_irqsave(&cosa->lock, flags);
1924 flags1 =claim_dma_lock();
1925 disable_dma(cosa->dma);
1926 clear_dma_ff(cosa->dma);
1927 release_dma_lock(flags1);
1928 if(test_bit(TXBIT, &cosa->rxtx)) {
1929 struct channel_data *chan = cosa->chan+cosa->txchan;
1930 if(chan->tx_done)
1931 if(chan->tx_done(chan, cosa->txsize))
1932 clear_bit(chan->num, &cosa->txbitmap);
1933 }else if(test_bit(RXBIT, &cosa->rxtx)) {
1934 #ifdef DEBUG_DATA
1936 int i;
1937 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1938 cosa->rxchan->num, cosa->rxsize);
1939 for(i=0; i<cosa->rxsize; i++)
1940 printk(" %02x", cosa->rxbuf[i]&0xff);
1941 printk("\n");
1943 #endif
1944 /* Packet for unknown channel? */
1945 if(cosa->rxbuf == cosa->bouncebuf)
1946 goto out;
1947 if(!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1948 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1949 if(cosa->rxchan->rx_done)
1950 if(cosa->rxchan->rx_done(cosa->rxchan))
1951 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1952 }else{
1953 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1954 cosa->num);
1957 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1958 * cleared anyway). We should do it as soon as possible
1959 * so that we can tell the COSA we are done and to give it a time
1960 * for recovery.
1962 out:
1963 cosa->rxtx =0;
1964 put_driver_status_nolock(cosa);
1965 spin_unlock_irqrestore(&cosa->lock, flags);
1968 static voidcosa_interrupt(int irq,void*cosa_,struct pt_regs *regs)
1970 unsigned status;
1971 int count =0;
1972 struct cosa_data *cosa = cosa_;
1973 again:
1974 status =cosa_getstatus(cosa);
1975 #ifdef DEBUG_IRQS
1976 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1977 status &0xff);
1978 #endif
1979 #ifdef DEBUG_IO
1980 debug_status_in(cosa, status);
1981 #endif
1982 switch(status & SR_CMD_FROM_SRP_MASK) {
1983 case SR_DOWN_REQUEST:
1984 tx_interrupt(cosa, status);
1985 break;
1986 case SR_UP_REQUEST:
1987 rx_interrupt(cosa, status);
1988 break;
1989 case SR_END_OF_TRANSFER:
1990 eot_interrupt(cosa, status);
1991 break;
1992 default:
1993 /* We may be too fast for SRP. Try to wait a bit more. */
1994 if(count++ <100) {
1995 udelay(100);
1996 goto again;
1998 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1999 cosa->num, status &0xff, count);
2001 #ifdef DEBUG_IRQS
2002 if(count)
2003 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
2004 cosa->name, count);
2005 else
2006 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
2007 #endif
2011 /* ---------- I/O debugging routines ---------- */
2013 * These routines can be used to monitor COSA/SRP I/O and to printk()
2014 * the data being transfered on the data and status I/O port in a
2015 * readable way.
2018 #ifdef DEBUG_IO
2019 static voiddebug_status_in(struct cosa_data *cosa,int status)
2021 char*s;
2022 switch(status & SR_CMD_FROM_SRP_MASK) {
2023 case SR_UP_REQUEST:
2024 s ="RX_REQ";
2025 break;
2026 case SR_DOWN_REQUEST:
2027 s ="TX_REQ";
2028 break;
2029 case SR_END_OF_TRANSFER:
2030 s ="ET_REQ";
2031 break;
2032 default:
2033 s ="NO_REQ";
2034 break;
2036 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2037 cosa->name,
2038 status,
2039 status & SR_USR_RQ ?"USR_RQ|":"",
2040 status & SR_TX_RDY ?"TX_RDY|":"",
2041 status & SR_RX_RDY ?"RX_RDY|":"",
2045 static voiddebug_status_out(struct cosa_data *cosa,int status)
2047 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2048 cosa->name,
2049 status,
2050 status & SR_RX_DMA_ENA ?"RXDMA|":"!rxdma|",
2051 status & SR_TX_DMA_ENA ?"TXDMA|":"!txdma|",
2052 status & SR_RST ?"RESET|":"",
2053 status & SR_USR_INT_ENA ?"USRINT|":"!usrint|",
2054 status & SR_TX_INT_ENA ?"TXINT|":"!txint|",
2055 status & SR_RX_INT_ENA ?"RXINT":"!rxint");
2058 static voiddebug_data_in(struct cosa_data *cosa,int data)
2060 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2063 static voiddebug_data_out(struct cosa_data *cosa,int data)
2065 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2068 static voiddebug_data_cmd(struct cosa_data *cosa,int data)
2070 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2071 cosa->name, data,
2072 data & SR_RDY_RCV ?"RX_RDY":"!rx_rdy",
2073 data & SR_RDY_SND ?"TX_RDY":"!tx_rdy");
2075 #endif
2077 /* EOF -- this file has not been truncated */
close