1 /* $Id: cosa.c,v 1.24 1999/05/28 17:28:34 kas Exp $ */
4 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
6 * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
7 * fixed a deadlock in cosa_sppp_open
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * The driver for the SRP and COSA synchronous serial cards.
29 * Both cards are developed at the Institute of Computer Science,
30 * Masaryk University (http://www.ics.muni.cz/). The hardware is
31 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
32 * and the photo of both cards is available at
33 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
34 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
35 * For Linux-specific utilities, see below in the "Software info" section.
36 * If you want to order the card, contact Jiri Novotny.
38 * The SRP (serial port?, the Czech word "srp" means "sickle") card
39 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
40 * with V.24 interfaces up to 80kb/s each.
42 * The COSA (communication serial adapter?, the Czech word "kosa" means
43 * "scythe") is a next-generation sync/async board with two interfaces
44 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
45 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
46 * The 8-channels version is in development.
48 * Both types have downloadable firmware and communicate via ISA DMA.
49 * COSA can be also a bus-mastering device.
53 * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
54 * The CVS tree of Linux driver can be viewed there, as well as the
55 * firmware binaries and user-space utilities for downloading the firmware
56 * into the card and setting up the card.
58 * The Linux driver (unlike the present *BSD drivers :-) can work even
59 * for the COSA and SRP in one computer and allows each channel to work
60 * in one of the three modes (character device, Cisco HDLC, Sync PPP).
64 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
66 * You can mail me bugfixes and even success reports. I am especially
67 * interested in the SMP and/or muliti-channel success/failure reports
68 * (I wonder if I did the locking properly :-).
70 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
72 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
73 * The skeleton.c by Donald Becker
74 * The SDL Riscom/N2 driver by Mike Natale
75 * The Comtrol Hostess SV11 driver by Alan Cox
76 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
79 * 5/25/1999 : Marcelo Tosatti <marcelo@conectiva.com.br>
80 * fixed a deadlock in cosa_sppp_open
83 /* ---------- Headers, macros, data structures ---------- */
85 #include <linux/module.h>
86 #include <linux/kernel.h>
87 #include <linux/malloc.h>
88 #include <linux/poll.h>
90 #include <linux/sched.h>
91 #include <linux/interrupt.h>
92 #include <linux/delay.h>
93 #include <linux/errno.h>
94 #include <linux/ioport.h>
95 #include <linux/netdevice.h>
97 #undef COSA_SLOW_IO /* for testing purposes only */
102 #include <asm/byteorder.h>
103 #include <asm/spinlock.h>
108 /* Maximum length of the identification string. */
109 #define COSA_MAX_ID_STRING 128
111 /* Maximum length of the channel name */
112 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
114 /* Per-channel data structure */
116 struct channel_data
{
117 int usage
; /* Usage count; >0 for chrdev, -1 for netdev */
118 int num
; /* Number of the channel */
119 struct cosa_data
*cosa
; /* Pointer to the per-card structure */
120 int txsize
; /* Size of transmitted data */
121 char *txbuf
; /* Transmit buffer */
122 char name
[COSA_MAX_NAME
]; /* channel name */
124 /* The HW layer interface */
125 /* routine called from the RX interrupt */
126 char *(*setup_rx
)(struct channel_data
*channel
, int size
);
127 /* routine called when the RX is done (from the EOT interrupt) */
128 int (*rx_done
)(struct channel_data
*channel
);
129 /* routine called when the TX is done (from the EOT interrupt) */
130 int (*tx_done
)(struct channel_data
*channel
, int size
);
132 /* Character device parts */
133 struct semaphore rsem
, wsem
;
136 wait_queue_head_t txwaitq
;
137 wait_queue_head_t rxwaitq
;
138 int tx_status
, rx_status
;
140 /* SPPP/HDLC device parts */
141 struct ppp_device pppdev
;
142 struct sk_buff
*rx_skb
, *tx_skb
;
143 struct net_device_stats stats
;
147 int num
; /* Card number */
148 char name
[COSA_MAX_NAME
]; /* Card name - e.g "cosa0" */
149 unsigned int datareg
, statusreg
; /* I/O ports */
150 unsigned short irq
, dma
; /* IRQ and DMA number */
151 unsigned short startaddr
; /* Firmware start address */
152 unsigned short busmaster
; /* Use busmastering? */
153 int nchannels
; /* # of channels on this card */
154 int driver_status
; /* For communicating with firware */
155 int firmware_status
; /* Downloaded, reseted, etc. */
156 int rxbitmap
, txbitmap
; /* Bitmap of channels who are willing to send/receive data */
157 int rxtx
; /* RX or TX in progress? */
159 int usage
; /* usage count */
160 int txchan
, txsize
, rxsize
;
161 struct channel_data
*rxchan
;
164 struct channel_data
*chan
;
165 spinlock_t lock
; /* For exclusive operations on this structure */
166 char id_string
[COSA_MAX_ID_STRING
]; /* ROM monitor ID string */
167 char *type
; /* card type */
171 * Define this if you want all the possible ports to be autoprobed.
172 * It is here but it probably is not a good idea to use this.
174 /* #define COSA_ISA_AUTOPROBE 1 */
177 * Character device major number. 117 was allocated for us.
178 * The value of 0 means to allocate a first free one.
180 static int cosa_major
= 117;
183 * Encoding of the minor numbers:
184 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
185 * the highest bits means the card number.
187 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
188 * for the single card */
190 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
191 * macro doesn't like anything other than the raw number as an argument :-(
194 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
196 #define DRIVER_RX_READY 0x0001
197 #define DRIVER_TX_READY 0x0002
198 #define DRIVER_TXMAP_SHIFT 2
199 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
202 * for cosa->rxtx - indicates whether either transmit or receive is
203 * in progress. These values are mean number of the bit.
209 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
211 #undef DEBUG_DATA 1 /* Dump the data read or written to the channel */
212 #undef DEBUG_IRQS 1 /* Print the message when the IRQ is received */
213 #undef DEBUG_IO 1 /* Dump the I/O traffic */
215 /* Maybe the following should be allocated dynamically */
216 static struct cosa_data cosa_cards
[MAX_CARDS
];
217 static int nr_cards
= 0;
219 #ifdef COSA_ISA_AUTOPROBE
220 static int io
[MAX_CARDS
+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
221 /* NOTE: DMA is not autoprobed!!! */
222 static int dma
[MAX_CARDS
+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
224 int io
[MAX_CARDS
+1] = { 0, };
225 int dma
[MAX_CARDS
+1] = { 0, };
227 /* IRQ can be safely autoprobed */
228 static int irq
[MAX_CARDS
+1] = { -1, -1, -1, -1, -1, -1, 0, };
231 MODULE_PARM(io
, "1-" __MODULE_STRING(MAX_CARDS
) "i");
232 MODULE_PARM_DESC(io
, "The I/O bases of the COSA or SRP cards");
233 MODULE_PARM(irq
, "1-" __MODULE_STRING(MAX_CARDS
) "i");
234 MODULE_PARM_DESC(irq
, "The IRQ lines of the COSA or SRP cards");
235 MODULE_PARM(dma
, "1-" __MODULE_STRING(MAX_CARDS
) "i");
236 MODULE_PARM_DESC(dma
, "The DMA channels of the COSA or SRP cards");
238 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
239 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
242 /* I use this mainly for testing purposes */
244 #define cosa_outb outb_p
245 #define cosa_outw outw_p
246 #define cosa_inb inb_p
247 #define cosa_inw inw_p
249 #define cosa_outb outb
250 #define cosa_outw outw
255 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
257 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
258 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
259 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
260 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
261 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
262 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
264 /* Initialization stuff */
265 static int cosa_probe(int ioaddr
, int irq
, int dma
);
268 static void cosa_enable_rx(struct channel_data
*chan
);
269 static void cosa_disable_rx(struct channel_data
*chan
);
270 static int cosa_start_tx(struct channel_data
*channel
, char *buf
, int size
);
271 static void cosa_kick(struct cosa_data
*cosa
);
272 static int cosa_dma_able(struct channel_data
*chan
, char *buf
, int data
);
274 /* SPPP/HDLC stuff */
275 static void sppp_channel_init(struct channel_data
*chan
);
276 static void sppp_channel_delete(struct channel_data
*chan
);
277 static int cosa_sppp_open(struct device
*d
);
278 static int cosa_sppp_close(struct device
*d
);
279 static int cosa_sppp_tx(struct sk_buff
*skb
, struct device
*d
);
280 static char *sppp_setup_rx(struct channel_data
*channel
, int size
);
281 static int sppp_rx_done(struct channel_data
*channel
);
282 static int sppp_tx_done(struct channel_data
*channel
, int size
);
283 static int cosa_sppp_ioctl(struct device
*dev
, struct ifreq
*ifr
, int cmd
);
284 static struct net_device_stats
*cosa_net_stats(struct device
*dev
);
286 /* Character device */
287 static void chardev_channel_init(struct channel_data
*chan
);
288 static char *chrdev_setup_rx(struct channel_data
*channel
, int size
);
289 static int chrdev_rx_done(struct channel_data
*channel
);
290 static int chrdev_tx_done(struct channel_data
*channel
, int size
);
291 static long long cosa_lseek(struct file
*file
,
292 long long offset
, int origin
);
293 static ssize_t
cosa_read(struct file
*file
,
294 char *buf
, size_t count
, loff_t
*ppos
);
295 static ssize_t
cosa_write(struct file
*file
,
296 const char *buf
, size_t count
, loff_t
*ppos
);
297 static unsigned int cosa_poll(struct file
*file
, poll_table
*poll
);
298 static int cosa_open(struct inode
*inode
, struct file
*file
);
299 static int cosa_release(struct inode
*inode
, struct file
*file
);
300 static int cosa_chardev_ioctl(struct inode
*inode
, struct file
*file
,
301 unsigned int cmd
, unsigned long arg
);
302 #ifdef COSA_FASYNC_WORKING
303 static int cosa_fasync(struct inode
*inode
, struct file
*file
, int on
);
306 static struct file_operations cosa_fops
= {
318 #ifdef COSA_FASYNC_WORKING
323 NULL
, /* check media change */
324 NULL
, /* revalidate */
329 static int cosa_start(struct cosa_data
*cosa
, int address
);
330 static int cosa_reset(struct cosa_data
*cosa
);
331 static int cosa_download(struct cosa_data
*cosa
, struct cosa_download
*d
);
332 static int cosa_readmem(struct cosa_data
*cosa
, struct cosa_download
*d
);
334 /* COSA/SRP ROM monitor */
335 static int download(struct cosa_data
*cosa
, char *data
, int addr
, int len
);
336 static int startmicrocode(struct cosa_data
*cosa
, int address
);
337 static int readmem(struct cosa_data
*cosa
, char *data
, int addr
, int len
);
338 static int cosa_reset_and_read_id(struct cosa_data
*cosa
, char *id
);
340 /* Auxilliary functions */
341 static int get_wait_data(struct cosa_data
*cosa
);
342 static int put_wait_data(struct cosa_data
*cosa
, int data
);
343 static int puthexnumber(struct cosa_data
*cosa
, int number
);
344 static void put_driver_status(struct cosa_data
*cosa
);
345 static void put_driver_status_nolock(struct cosa_data
*cosa
);
347 /* Interrupt handling */
348 static void cosa_interrupt(int irq
, void *cosa
, struct pt_regs
*regs
);
350 /* I/O ops debugging */
352 static void debug_data_in(struct cosa_data
*cosa
, int data
);
353 static void debug_data_out(struct cosa_data
*cosa
, int data
);
354 static void debug_data_cmd(struct cosa_data
*cosa
, int data
);
355 static void debug_status_in(struct cosa_data
*cosa
, int status
);
356 static void debug_status_out(struct cosa_data
*cosa
, int status
);
360 /* ---------- Initialization stuff ---------- */
363 int init_module(void)
365 static int __init
cosa_init(void)
369 printk(KERN_INFO
"cosa v1.04 (c) 1997-8 Jan Kasprzak <kas@fi.muni.cz>\n");
371 printk(KERN_INFO
"cosa: SMP found. Please mail any success/failure reports to the author.\n");
373 if (cosa_major
> 0) {
374 if (register_chrdev(cosa_major
, "cosa", &cosa_fops
)) {
375 printk(KERN_WARNING
"cosa: unable to get major %d\n",
380 if (!(cosa_major
=register_chrdev(0, "cosa", &cosa_fops
))) {
381 printk(KERN_WARNING
"cosa: unable to register chardev\n");
385 for (i
=0; i
<MAX_CARDS
; i
++)
386 cosa_cards
[i
].num
= -1;
387 for (i
=0; io
[i
] != 0 && i
< MAX_CARDS
; i
++)
388 cosa_probe(io
[i
], irq
[i
], dma
[i
]);
390 printk(KERN_WARNING
"cosa: no devices found.\n");
391 unregister_chrdev(cosa_major
, "cosa");
398 void cleanup_module (void)
400 struct cosa_data
*cosa
;
401 printk(KERN_INFO
"Unloading the cosa module\n");
403 for (cosa
=cosa_cards
; nr_cards
--; cosa
++) {
405 /* Clean up the per-channel data */
406 for (i
=0; i
<cosa
->nchannels
; i
++) {
407 /* Chardev driver has no alloc'd per-channel data */
408 sppp_channel_delete(cosa
->chan
+i
);
410 /* Clean up the per-card data */
412 kfree(cosa
->bouncebuf
);
413 free_irq(cosa
->irq
, cosa
);
415 release_region(cosa
->datareg
,is_8bit(cosa
)?2:4);
417 unregister_chrdev(cosa_major
, "cosa");
422 * This function should register all the net devices needed for the
425 static __inline__
void channel_init(struct channel_data
*chan
)
427 sprintf(chan
->name
, "cosa%dc%d", chan
->cosa
->num
, chan
->num
);
429 /* Initialize the chardev data structures */
430 chardev_channel_init(chan
);
432 /* Register the sppp interface */
433 sppp_channel_init(chan
);
436 static int cosa_probe(int base
, int irq
, int dma
)
438 struct cosa_data
*cosa
= cosa_cards
+nr_cards
;
441 memset(cosa
, 0, sizeof(struct cosa_data
));
443 /* Checking validity of parameters: */
444 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
445 if ((irq
>= 0 && irq
< 2) || irq
> 15 || (irq
< 10 && irq
> 7)) {
446 printk (KERN_INFO
"cosa_probe: invalid IRQ %d\n", irq
);
449 /* I/O address should be between 0x100 and 0x3ff and should be
451 if (base
< 0x100 || base
> 0x3ff || base
& 0x7) {
452 printk (KERN_INFO
"cosa_probe: invalid I/O address 0x%x\n",
456 /* DMA should be 0,1 or 3-7 */
457 if (dma
< 0 || dma
== 4 || dma
> 7) {
458 printk (KERN_INFO
"cosa_probe: invalid DMA %d\n", dma
);
461 /* and finally, on 16-bit COSA DMA should be 4-7 and
462 * I/O base should not be multiple of 0x10 */
463 if (((base
& 0x8) && dma
< 4) || (!(base
& 0x8) && dma
> 3)) {
464 printk (KERN_INFO
"cosa_probe: 8/16 bit base and DMA mismatch"
465 " (base=0x%x, dma=%d)\n", base
, dma
);
470 cosa
->datareg
= base
;
471 cosa
->statusreg
= is_8bit(cosa
)?base
+1:base
+2;
472 spin_lock_init(&cosa
->lock
);
474 if (check_region(base
, is_8bit(cosa
)?2:4))
477 if (cosa_reset_and_read_id(cosa
, cosa
->id_string
) < 0) {
478 printk(KERN_DEBUG
"cosa: probe at 0x%x failed.\n", base
);
482 /* Test the validity of identification string */
483 if (!strncmp(cosa
->id_string
, "SRP", 3))
485 else if (!strncmp(cosa
->id_string
, "COSA", 4))
486 cosa
->type
= is_8bit(cosa
)? "cosa8": "cosa16";
488 /* Print a warning only if we are not autoprobing */
489 #ifndef COSA_ISA_AUTOPROBE
490 printk(KERN_INFO
"cosa: valid signature not found at 0x%x.\n",
496 /* Now do IRQ autoprobe */
499 /* printk(KERN_INFO "IRQ autoprobe\n"); */
501 irqs
= probe_irq_on();
503 * Enable interrupt on tx buffer empty (it sure is)
505 * FIXME: When this code is not used as module, we should
506 * probably call udelay() instead of the interruptible sleep.
508 current
->state
= TASK_INTERRUPTIBLE
;
509 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
510 schedule_timeout(30);
511 current
->state
= TASK_RUNNING
;
512 irq
= probe_irq_off(irqs
);
513 /* Disable all IRQs from the card */
514 cosa_putstatus(cosa
, 0);
515 /* Empty the received data register */
519 printk (KERN_INFO
"cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
524 printk (KERN_INFO
"cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
531 cosa
->num
= nr_cards
;
533 cosa
->nchannels
= 2; /* FIXME: how to determine this? */
535 request_region(base
, is_8bit(cosa
)?2:4, cosa
->type
);
536 if (request_irq(cosa
->irq
, cosa_interrupt
, 0, cosa
->type
, cosa
))
538 if (request_dma(cosa
->dma
, cosa
->type
)) {
539 free_irq(cosa
->irq
, cosa
);
540 bad1
: release_region(cosa
->datareg
,is_8bit(cosa
)?2:4);
541 printk(KERN_NOTICE
"cosa%d: allocating resources failed\n",
546 cosa
->bouncebuf
= kmalloc(COSA_MTU
, GFP_KERNEL
|GFP_DMA
);
547 sprintf(cosa
->name
, "cosa%d", cosa
->num
);
549 /* Initialize the per-channel data */
550 cosa
->chan
= kmalloc(sizeof(struct channel_data
)*cosa
->nchannels
,
552 memset(cosa
->chan
, 0, sizeof(struct channel_data
)*cosa
->nchannels
);
553 for (i
=0; i
<cosa
->nchannels
; i
++) {
554 cosa
->chan
[i
].cosa
= cosa
;
555 cosa
->chan
[i
].num
= i
;
556 channel_init(cosa
->chan
+i
);
559 printk (KERN_INFO
"cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
560 cosa
->num
, cosa
->id_string
, cosa
->type
,
561 cosa
->datareg
, cosa
->irq
, cosa
->dma
, cosa
->nchannels
);
567 /*---------- SPPP/HDLC netdevice ---------- */
569 static void sppp_channel_init(struct channel_data
*chan
)
572 sppp_attach(&chan
->pppdev
);
574 d
->name
= chan
->name
;
575 d
->base_addr
= chan
->cosa
->datareg
;
576 d
->irq
= chan
->cosa
->irq
;
577 d
->dma
= chan
->cosa
->dma
;
580 d
->open
= cosa_sppp_open
;
581 d
->stop
= cosa_sppp_close
;
582 d
->hard_start_xmit
= cosa_sppp_tx
;
583 d
->do_ioctl
= cosa_sppp_ioctl
;
584 d
->get_stats
= cosa_net_stats
;
586 if (register_netdev(d
) == -1) {
587 printk(KERN_WARNING
"%s: register_netdev failed.\n", d
->name
);
588 sppp_detach(&chan
->pppdev
.dev
);
593 static void sppp_channel_delete(struct channel_data
*chan
)
595 sppp_detach(&chan
->pppdev
.dev
);
596 unregister_netdev(&chan
->pppdev
.dev
);
600 static int cosa_sppp_open(struct device
*d
)
602 struct channel_data
*chan
= d
->priv
;
605 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
606 if (chan
->usage
!= 0) {
607 printk(KERN_WARNING
"%s: sppp_open called with usage count %d\n",
608 chan
->name
, chan
->usage
);
609 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
612 chan
->setup_rx
= sppp_setup_rx
;
613 chan
->tx_done
= sppp_tx_done
;
614 chan
->rx_done
= sppp_rx_done
;
618 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
622 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
627 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
632 cosa_enable_rx(chan
);
636 static int cosa_sppp_tx(struct sk_buff
*skb
, struct device
*dev
)
638 struct channel_data
*chan
= dev
->priv
;
641 if (time_before(jiffies
, dev
->trans_start
+2*HZ
))
642 return 1; /* Two seconds timeout */
643 if (test_bit(RXBIT
, &chan
->cosa
->rxtx
)) {
644 chan
->stats
.rx_errors
++;
645 chan
->stats
.rx_missed_errors
++;
647 chan
->stats
.tx_errors
++;
648 chan
->stats
.tx_aborted_errors
++;
650 cosa_kick(chan
->cosa
);
652 dev_kfree_skb(chan
->tx_skb
);
657 if (test_and_set_bit(0, (void*)&dev
->tbusy
) != 0) {
658 printk(KERN_WARNING
"%s: Transmitter access conflict.\n", dev
->name
);
663 dev
->trans_start
= jiffies
;
664 cosa_start_tx(chan
, skb
->data
, skb
->len
);
668 static int cosa_sppp_close(struct device
*d
)
670 struct channel_data
*chan
= d
->priv
;
675 cosa_disable_rx(chan
);
676 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
678 kfree_skb(chan
->rx_skb
);
682 kfree_skb(chan
->tx_skb
);
688 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
692 static char *sppp_setup_rx(struct channel_data
*chan
, int size
)
695 * We can safely fall back to non-dma-able memory, because we have
696 * the cosa->bouncebuf pre-allocated.
699 kfree_skb(chan
->rx_skb
);
700 chan
->rx_skb
= dev_alloc_skb(size
);
701 if (chan
->rx_skb
== NULL
) {
702 printk(KERN_NOTICE
"%s: Memory squeeze, dropping packet\n",
704 chan
->stats
.rx_dropped
++;
707 chan
->pppdev
.dev
.trans_start
= jiffies
;
708 return skb_put(chan
->rx_skb
, size
);
711 static int sppp_rx_done(struct channel_data
*chan
)
714 printk(KERN_WARNING
"%s: rx_done with empty skb!\n",
716 chan
->stats
.rx_errors
++;
717 chan
->stats
.rx_frame_errors
++;
720 chan
->rx_skb
->protocol
= htons(ETH_P_WAN_PPP
);
721 chan
->rx_skb
->dev
= &chan
->pppdev
.dev
;
722 chan
->rx_skb
->mac
.raw
= chan
->rx_skb
->data
;
723 chan
->stats
.rx_packets
++;
724 chan
->stats
.rx_bytes
+= chan
->cosa
->rxsize
;
725 netif_rx(chan
->rx_skb
);
727 chan
->pppdev
.dev
.trans_start
= jiffies
;
732 static int sppp_tx_done(struct channel_data
*chan
, int size
)
735 printk(KERN_WARNING
"%s: tx_done with empty skb!\n",
737 chan
->stats
.tx_errors
++;
738 chan
->stats
.tx_aborted_errors
++;
741 dev_kfree_skb(chan
->tx_skb
);
743 chan
->stats
.tx_packets
++;
744 chan
->stats
.tx_bytes
+= size
;
745 chan
->pppdev
.dev
.tbusy
= 0;
750 static struct net_device_stats
*cosa_net_stats(struct device
*dev
)
752 struct channel_data
*chan
= dev
->priv
;
757 /*---------- Character device ---------- */
759 static void chardev_channel_init(struct channel_data
*chan
)
761 init_MUTEX(&chan
->rsem
);
762 init_MUTEX(&chan
->wsem
);
765 static long long cosa_lseek(struct file
* file
,
766 long long offset
, int origin
)
771 static ssize_t
cosa_read(struct file
*file
,
772 char *buf
, size_t count
, loff_t
*ppos
)
774 DECLARE_WAITQUEUE(wait
, current
);
776 struct channel_data
*chan
= (struct channel_data
*)file
->private_data
;
777 struct cosa_data
*cosa
= chan
->cosa
;
780 if (down_interruptible(&chan
->rsem
))
783 if ((chan
->rxdata
= kmalloc(COSA_MTU
, GFP_DMA
|GFP_KERNEL
)) == NULL
) {
784 printk(KERN_INFO
"%s: cosa_read() - OOM\n", cosa
->name
);
790 cosa_enable_rx(chan
);
791 spin_lock_irqsave(&cosa
->lock
, flags
);
792 add_wait_queue(&chan
->rxwaitq
, &wait
);
793 while(!chan
->rx_status
) {
794 current
->state
= TASK_INTERRUPTIBLE
;
795 spin_unlock_irqrestore(&cosa
->lock
, flags
);
797 spin_lock_irqsave(&cosa
->lock
, flags
);
798 if (signal_pending(current
) && chan
->rx_status
== 0) {
800 remove_wait_queue(&chan
->rxwaitq
, &wait
);
801 current
->state
= TASK_RUNNING
;
802 spin_unlock_irqrestore(&cosa
->lock
, flags
);
807 remove_wait_queue(&chan
->rxwaitq
, &wait
);
808 current
->state
= TASK_RUNNING
;
810 count
= chan
->rxsize
;
811 spin_unlock_irqrestore(&cosa
->lock
, flags
);
814 if (copy_to_user(buf
, kbuf
, count
)) {
822 static char *chrdev_setup_rx(struct channel_data
*chan
, int size
)
824 /* Expect size <= COSA_MTU */
829 static int chrdev_rx_done(struct channel_data
*chan
)
831 if (chan
->rx_status
) { /* Reader has died */
836 wake_up_interruptible(&chan
->rxwaitq
);
841 static ssize_t
cosa_write(struct file
*file
,
842 const char *buf
, size_t count
, loff_t
*ppos
)
844 struct channel_data
*chan
= (struct channel_data
*)file
->private_data
;
845 DECLARE_WAITQUEUE(wait
, current
);
846 struct cosa_data
*cosa
= chan
->cosa
;
850 if (down_interruptible(&chan
->wsem
))
853 if (count
> COSA_MTU
)
856 /* Allocate the buffer */
857 if ((kbuf
= kmalloc(count
, GFP_KERNEL
|GFP_DMA
)) == NULL
) {
858 printk(KERN_NOTICE
"%s: cosa_write() OOM - dropping packet\n",
863 if (copy_from_user(kbuf
, buf
, count
)) {
869 cosa_start_tx(chan
, kbuf
, count
);
871 spin_lock_irqsave(&cosa
->lock
, flags
);
872 add_wait_queue(&chan
->txwaitq
, &wait
);
873 while(!chan
->tx_status
) {
874 current
->state
= TASK_INTERRUPTIBLE
;
875 spin_unlock_irqrestore(&cosa
->lock
, flags
);
877 spin_lock_irqsave(&cosa
->lock
, flags
);
878 if (signal_pending(current
) && chan
->tx_status
== 0) {
880 remove_wait_queue(&chan
->txwaitq
, &wait
);
881 current
->state
= TASK_RUNNING
;
883 spin_unlock_irqrestore(&cosa
->lock
, flags
);
887 remove_wait_queue(&chan
->txwaitq
, &wait
);
888 current
->state
= TASK_RUNNING
;
890 spin_unlock_irqrestore(&cosa
->lock
, flags
);
895 static int chrdev_tx_done(struct channel_data
*chan
, int size
)
897 if (chan
->tx_status
) { /* Writer was interrupted */
902 wake_up_interruptible(&chan
->txwaitq
);
906 static unsigned int cosa_poll(struct file
*file
, poll_table
*poll
)
908 printk(KERN_INFO
"cosa_poll is here\n");
912 static int cosa_open(struct inode
*inode
, struct file
*file
)
914 struct cosa_data
*cosa
;
915 struct channel_data
*chan
;
919 if ((n
=MINOR(file
->f_dentry
->d_inode
->i_rdev
)>>CARD_MINOR_BITS
)
924 if ((n
=MINOR(file
->f_dentry
->d_inode
->i_rdev
)
925 & ((1<<CARD_MINOR_BITS
)-1)) >= cosa
->nchannels
)
927 chan
= cosa
->chan
+ n
;
929 file
->private_data
= chan
;
931 spin_lock_irqsave(&cosa
->lock
, flags
);
933 if (chan
->usage
< 0) { /* in netdev mode */
934 spin_unlock_irqrestore(&cosa
->lock
, flags
);
940 chan
->tx_done
= chrdev_tx_done
;
941 chan
->setup_rx
= chrdev_setup_rx
;
942 chan
->rx_done
= chrdev_rx_done
;
946 spin_unlock_irqrestore(&cosa
->lock
, flags
);
950 static int cosa_release(struct inode
*inode
, struct file
*file
)
952 struct channel_data
*channel
= (struct channel_data
*)file
->private_data
;
953 struct cosa_data
*cosa
= channel
->cosa
;
956 spin_lock_irqsave(&cosa
->lock
, flags
);
962 spin_unlock_irqrestore(&cosa
->lock
, flags
);
966 #ifdef COSA_FASYNC_WORKING
967 static struct fasync_struct
*fasync
[256] = { NULL
, };
970 static int cosa_fasync(struct inode
*inode
, struct file
*file
, int on
)
972 int port
= MINOR(inode
->i_rdev
);
973 int rv
= fasync_helper(inode
, file
, on
, &fasync
[port
]);
974 return rv
< 0 ? rv
: 0;
979 /* ---------- Ioctls ---------- */
982 * Ioctl subroutines can safely be made inline, because they are called
983 * only from cosa_ioctl().
985 static inline int cosa_reset(struct cosa_data
*cosa
)
987 char idstring
[COSA_MAX_ID_STRING
];
989 printk(KERN_INFO
"cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
990 cosa
->num
, cosa
->usage
);
991 if (cosa_reset_and_read_id(cosa
, idstring
) < 0) {
992 printk(KERN_NOTICE
"cosa%d: reset failed\n", cosa
->num
);
995 printk(KERN_INFO
"cosa%d: resetting device: %s\n", cosa
->num
,
1000 /* High-level function to download data into COSA memory. Calls download() */
1001 static inline int cosa_download(struct cosa_data
*cosa
, struct cosa_download
*d
)
1007 if (cosa
->usage
> 1)
1008 printk(KERN_INFO
"cosa%d: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1009 cosa
->num
, cosa
->usage
);
1011 if (cosa
->status
!= CARD_STATUS_RESETED
&& cosa
->status
!= CARD_STATUS_DOWNLOADED
) {
1012 printk(KERN_NOTICE
"cosa%d: reset the card first (status %d).\n",
1013 cosa
->num
, cosa
->status
);
1017 get_user_ret(addr
, &(d
->addr
), -EFAULT
);
1018 get_user_ret(len
, &(d
->len
), -EFAULT
);
1019 get_user_ret(code
, &(d
->code
), -EFAULT
);
1021 if (d
->addr
< 0 || d
->addr
> COSA_MAX_FIRMWARE_SIZE
)
1023 if (d
->len
< 0 || d
->len
> COSA_MAX_FIRMWARE_SIZE
)
1026 if ((i
=download(cosa
, d
->code
, len
, addr
)) < 0) {
1027 printk(KERN_NOTICE
"cosa%d: microcode download failed: %d\n",
1031 printk(KERN_INFO
"cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1032 cosa
->num
, len
, addr
);
1036 /* High-level function to read COSA memory. Calls readmem() */
1037 static inline int cosa_readmem(struct cosa_data
*cosa
, struct cosa_download
*d
)
1043 if (cosa
->usage
> 1)
1044 printk(KERN_INFO
"cosa%d: WARNING: readmem requested with "
1045 "cosa->usage > 1 (%d). Odd things may happen.\n",
1046 cosa
->num
, cosa
->usage
);
1048 if (cosa
->status
!= CARD_STATUS_RESETED
&&
1049 cosa
->status
!= CARD_STATUS_DOWNLOADED
) {
1050 printk(KERN_NOTICE
"cosa%d: reset the card first (status %d).\n",
1051 cosa
->num
, cosa
->status
);
1055 get_user_ret(addr
, &(d
->addr
), -EFAULT
);
1056 get_user_ret(len
, &(d
->len
), -EFAULT
);
1057 get_user_ret(code
, &(d
->code
), -EFAULT
);
1059 if ((i
=readmem(cosa
, d
->code
, len
, addr
)) < 0) {
1060 printk(KERN_NOTICE
"cosa%d: reading memory failed: %d\n",
1064 printk(KERN_INFO
"cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1065 cosa
->num
, len
, addr
);
1069 /* High-level function to start microcode. Calls startmicrocode(). */
1070 static inline int cosa_start(struct cosa_data
*cosa
, int address
)
1074 if (cosa
->usage
> 1)
1075 printk(KERN_INFO
"cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1076 cosa
->num
, cosa
->usage
);
1078 if (cosa
->status
!= CARD_STATUS_DOWNLOADED
) {
1079 printk(KERN_NOTICE
"cosa%d: download the microcode first (status %d).\n",
1080 cosa
->num
, cosa
->status
);
1084 if ((i
=startmicrocode(cosa
, address
)) < 0) {
1085 printk(KERN_NOTICE
"cosa%d: start microcode at 0x%04x failed: %d\n",
1086 cosa
->num
, address
, i
);
1089 printk(KERN_INFO
"cosa%d: starting microcode at 0x%04x\n",
1090 cosa
->num
, address
);
1091 cosa
->startaddr
= address
;
1095 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1096 static inline int cosa_getidstr(struct cosa_data
*cosa
, char *string
)
1098 int l
= strlen(cosa
->id_string
)+1;
1099 copy_to_user_ret(string
, cosa
->id_string
, l
, -EFAULT
);
1103 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1104 static inline int cosa_gettype(struct cosa_data
*cosa
, char *string
)
1106 int l
= strlen(cosa
->type
)+1;
1107 copy_to_user_ret(string
, cosa
->type
, l
, -EFAULT
);
1111 static int cosa_ioctl_common(struct cosa_data
*cosa
,
1112 struct channel_data
*channel
, unsigned int cmd
, unsigned long arg
)
1115 case COSAIORSET
: /* Reset the device */
1118 return cosa_reset(cosa
);
1119 case COSAIOSTRT
: /* Start the firmware */
1122 return cosa_start(cosa
, arg
);
1123 case COSAIODOWNLD
: /* Download the firmware */
1126 return cosa_download(cosa
, (struct cosa_download
*)arg
);
1130 return cosa_readmem(cosa
, (struct cosa_download
*)arg
);
1132 return cosa_gettype(cosa
, (char *)arg
);
1134 return cosa_getidstr(cosa
, (char *)arg
);
1136 * These two are _very_ugly_hack_(tm). Don't even look at this.
1137 * Implementing this saved me few reboots after some process segfaulted
1138 * inside this module.
1153 return cosa
->nchannels
;
1159 if (arg
!= COSA_BM_OFF
&& arg
!= COSA_BM_ON
)
1161 cosa
->busmaster
= arg
;
1164 return cosa
->busmaster
;
1166 return -ENOIOCTLCMD
;
1169 static int cosa_sppp_ioctl(struct device
*dev
, struct ifreq
*ifr
,
1173 struct channel_data
*chan
= (struct channel_data
*)dev
->priv
;
1174 rv
= cosa_ioctl_common(chan
->cosa
, chan
, cmd
, (int)ifr
->ifr_data
);
1175 if (rv
== -ENOIOCTLCMD
) {
1176 return sppp_do_ioctl(dev
, ifr
, cmd
);
1181 static int cosa_chardev_ioctl(struct inode
*inode
, struct file
*file
,
1182 unsigned int cmd
, unsigned long arg
)
1184 struct channel_data
*channel
= (struct channel_data
*)file
->private_data
;
1185 struct cosa_data
*cosa
= channel
->cosa
;
1186 return cosa_ioctl_common(cosa
, channel
, cmd
, arg
);
1190 /*---------- HW layer interface ---------- */
1193 * The higher layer can bind itself to the HW layer by setting the callbacks
1194 * in the channel_data structure and by using these routines.
1196 static void cosa_enable_rx(struct channel_data
*chan
)
1198 struct cosa_data
*cosa
= chan
->cosa
;
1200 if (!test_and_set_bit(chan
->num
, &cosa
->rxbitmap
))
1201 put_driver_status(cosa
);
1204 static void cosa_disable_rx(struct channel_data
*chan
)
1206 struct cosa_data
*cosa
= chan
->cosa
;
1208 if (test_and_clear_bit(chan
->num
, &cosa
->rxbitmap
))
1209 put_driver_status(cosa
);
1213 * FIXME: This routine probably should check for cosa_start_tx() called when
1214 * the previous transmit is still unfinished. In this case the non-zero
1215 * return value should indicate to the caller that the queuing(sp?) up
1216 * the transmit has failed.
1218 static int cosa_start_tx(struct channel_data
*chan
, char *buf
, int len
)
1220 struct cosa_data
*cosa
= chan
->cosa
;
1225 printk(KERN_INFO
"cosa%dc%d: starting tx(0x%x)", chan
->cosa
->num
,
1227 for (i
=0; i
<len
; i
++)
1228 printk(" %02x", buf
[i
]&0xff);
1231 spin_lock_irqsave(&cosa
->lock
, flags
);
1235 chan
->txsize
= COSA_MTU
;
1236 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1238 /* Tell the firmware we are ready */
1239 set_bit(chan
->num
, &cosa
->txbitmap
);
1240 put_driver_status(cosa
);
1245 static void put_driver_status(struct cosa_data
*cosa
)
1250 spin_lock_irqsave(&cosa
->lock
, flags
);
1252 status
= (cosa
->rxbitmap
? DRIVER_RX_READY
: 0)
1253 | (cosa
->txbitmap
? DRIVER_TX_READY
: 0)
1254 | (cosa
->txbitmap
? ~(cosa
->txbitmap
<<DRIVER_TXMAP_SHIFT
)
1255 &DRIVER_TXMAP_MASK
: 0);
1257 if (cosa
->rxbitmap
|cosa
->txbitmap
) {
1258 if (!cosa
->enabled
) {
1259 cosa_putstatus(cosa
, SR_RX_INT_ENA
);
1261 debug_status_out(cosa
, SR_RX_INT_ENA
);
1265 } else if (cosa
->enabled
) {
1267 cosa_putstatus(cosa
, 0);
1269 debug_status_out(cosa
, 0);
1272 cosa_putdata8(cosa
, 0);
1273 cosa_putdata8(cosa
, status
);
1275 debug_data_cmd(cosa
, 0);
1276 debug_data_cmd(cosa
, status
);
1279 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1282 static void put_driver_status_nolock(struct cosa_data
*cosa
)
1286 status
= (cosa
->rxbitmap
? DRIVER_RX_READY
: 0)
1287 | (cosa
->txbitmap
? DRIVER_TX_READY
: 0)
1288 | (cosa
->txbitmap
? ~(cosa
->txbitmap
<<DRIVER_TXMAP_SHIFT
)
1289 &DRIVER_TXMAP_MASK
: 0);
1291 if (cosa
->rxbitmap
|cosa
->txbitmap
) {
1292 cosa_putstatus(cosa
, SR_RX_INT_ENA
);
1294 debug_status_out(cosa
, SR_RX_INT_ENA
);
1298 cosa_putstatus(cosa
, 0);
1300 debug_status_out(cosa
, 0);
1304 cosa_putdata8(cosa
, status
);
1306 debug_data_cmd(cosa
, status
);
1311 * The "kickme" function: When the DMA times out, this is called to
1312 * clean up the driver status.
1313 * FIXME: Preliminary support, the interface is probably wrong.
1315 static void cosa_kick(struct cosa_data
*cosa
)
1317 unsigned flags
, flags1
;
1318 char *s
= "Unknown";
1320 if (test_bit(RXBIT
, &cosa
->rxtx
))
1322 if (test_bit(TXBIT
, &cosa
->rxtx
))
1325 printk(KERN_INFO
"%s: %s DMA timeout - restarting.\n", cosa
->name
, s
);
1326 spin_lock_irqsave(&cosa
->lock
, flags
);
1329 flags1
= claim_dma_lock();
1330 disable_dma(cosa
->dma
);
1331 clear_dma_ff(cosa
->dma
);
1332 release_dma_lock(flags1
);
1334 /* FIXME: Anything else? */
1336 cosa_putstatus(cosa
, 0);
1338 (void) cosa_getdata8(cosa
);
1340 cosa_putdata8(cosa
, 0);
1342 put_driver_status_nolock(cosa
);
1343 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1347 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1348 * physical memory and doesn't span the 64k boundary. For now it seems
1349 * SKB's never do this, but we'll check this anyway.
1351 static int cosa_dma_able(struct channel_data
*chan
, char *buf
, int len
)
1353 static int count
= 0;
1354 unsigned long b
= (unsigned long)buf
;
1355 if (b
+len
>= MAX_DMA_ADDRESS
)
1357 if ((b
^ (b
+len
)) & 0x10000) {
1359 printk(KERN_INFO
"%s: packet spanning a 64k boundary\n",
1367 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1370 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1371 * drivers need to say 4-digit hex number meaning start address of the microcode
1372 * separated by a single space. Monitor replies by saying " =". Now driver
1373 * has to write 4-digit hex number meaning the last byte address ended
1374 * by a single space. Monitor has to reply with a space. Now the download
1375 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1377 static int download(struct cosa_data
*cosa
, char *microcode
, int length
, int address
)
1381 if (put_wait_data(cosa
, 'w') == -1) return -1;
1382 if ((i
=get_wait_data(cosa
)) != 'w') { printk("dnld: 0x%04x\n",i
); return -2;}
1383 if (get_wait_data(cosa
) != '=') return -3;
1385 if (puthexnumber(cosa
, address
) < 0) return -4;
1386 if (put_wait_data(cosa
, ' ') == -1) return -10;
1387 if (get_wait_data(cosa
) != ' ') return -11;
1388 if (get_wait_data(cosa
) != '=') return -12;
1390 if (puthexnumber(cosa
, address
+length
-1) < 0) return -13;
1391 if (put_wait_data(cosa
, ' ') == -1) return -18;
1392 if (get_wait_data(cosa
) != ' ') return -19;
1396 #ifndef SRP_DOWNLOAD_AT_BOOT
1397 get_user_ret(c
,microcode
, -23);
1401 if (put_wait_data(cosa
, c
) == -1)
1406 if (get_wait_data(cosa
) != '\r') return -21;
1407 if (get_wait_data(cosa
) != '\n') return -22;
1408 if (get_wait_data(cosa
) != '.') return -23;
1410 printk(KERN_DEBUG
"cosa%d: download completed.\n", cosa
->num
);
1417 * Starting microcode is done via the "g" command of the SRP monitor.
1418 * The chat should be the following: "g" "g=" "<addr><CR>"
1419 * "<CR><CR><LF><CR><LF>".
1421 static int startmicrocode(struct cosa_data
*cosa
, int address
)
1423 if (put_wait_data(cosa
, 'g') == -1) return -1;
1424 if (get_wait_data(cosa
) != 'g') return -2;
1425 if (get_wait_data(cosa
) != '=') return -3;
1427 if (puthexnumber(cosa
, address
) < 0) return -4;
1428 if (put_wait_data(cosa
, '\r') == -1) return -5;
1430 if (get_wait_data(cosa
) != '\r') return -6;
1431 if (get_wait_data(cosa
) != '\r') return -7;
1432 if (get_wait_data(cosa
) != '\n') return -8;
1433 if (get_wait_data(cosa
) != '\r') return -9;
1434 if (get_wait_data(cosa
) != '\n') return -10;
1436 printk(KERN_DEBUG
"cosa%d: microcode started\n", cosa
->num
);
1442 * Reading memory is done via the "r" command of the SRP monitor.
1443 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1444 * Then driver can read the data and the conversation is finished
1445 * by SRP monitor sending "<CR><LF>." (dot at the end).
1447 * This routine is not needed during the normal operation and serves
1448 * for debugging purposes only.
1450 static int readmem(struct cosa_data
*cosa
, char *microcode
, int length
, int address
)
1452 if (put_wait_data(cosa
, 'r') == -1) return -1;
1453 if ((get_wait_data(cosa
)) != 'r') return -2;
1454 if ((get_wait_data(cosa
)) != '=') return -3;
1456 if (puthexnumber(cosa
, address
) < 0) return -4;
1457 if (put_wait_data(cosa
, ' ') == -1) return -5;
1458 if (get_wait_data(cosa
) != ' ') return -6;
1459 if (get_wait_data(cosa
) != '=') return -7;
1461 if (puthexnumber(cosa
, address
+length
-1) < 0) return -8;
1462 if (put_wait_data(cosa
, ' ') == -1) return -9;
1463 if (get_wait_data(cosa
) != ' ') return -10;
1468 if ((i
=get_wait_data(cosa
)) == -1) {
1469 printk (KERN_INFO
"cosa: 0x%04x bytes remaining\n",
1475 put_user_ret(c
,microcode
, -23);
1482 if (get_wait_data(cosa
) != '\r') return -21;
1483 if (get_wait_data(cosa
) != '\n') return -22;
1484 if (get_wait_data(cosa
) != '.') return -23;
1486 printk(KERN_DEBUG
"cosa%d: readmem completed.\n", cosa
->num
);
1492 * This function resets the device and reads the initial prompt
1493 * of the device's ROM monitor.
1495 static int cosa_reset_and_read_id(struct cosa_data
*cosa
, char *idstring
)
1497 int i
=0, id
=0, prev
=0, curr
=0;
1499 /* Reset the card ... */
1500 cosa_putstatus(cosa
, 0);
1501 cosa_getdata8(cosa
);
1502 cosa_putstatus(cosa
, SR_RST
);
1504 current
->state
= TASK_INTERRUPTIBLE
;
1505 schedule_timeout(HZ
/2);
1506 current
->state
= TASK_RUNNING
;
1510 /* Disable all IRQs from the card */
1511 cosa_putstatus(cosa
, 0);
1514 * Try to read the ID string. The card then prints out the
1515 * identification string ended by the "\n\x2e".
1517 * The following loop is indexed through i (instead of id)
1518 * to avoid looping forever when for any reason
1519 * the port returns '\r', '\n' or '\x2e' permanently.
1521 for (i
=0; i
<COSA_MAX_ID_STRING
-1; i
++, prev
=curr
) {
1522 if ((curr
= get_wait_data(cosa
)) == -1) {
1526 if (curr
!= '\r' && curr
!= '\n' && curr
!= 0x2e)
1527 idstring
[id
++] = curr
;
1528 if (curr
== 0x2e && prev
== '\n')
1531 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1532 idstring
[id
] = '\0';
1537 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1540 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1541 * bit to be set in a loop. It should be used in the exceptional cases
1542 * only (for example when resetting the card or downloading the firmware.
1544 static int get_wait_data(struct cosa_data
*cosa
)
1549 /* read data and return them */
1550 if (cosa_getstatus(cosa
) & SR_RX_RDY
) {
1552 r
= cosa_getdata8(cosa
);
1554 printk(KERN_INFO
"cosa: get_wait_data returning after %d retries\n", 999-retries
);
1558 /* sleep if not ready to read */
1559 current
->state
= TASK_INTERRUPTIBLE
;
1560 schedule_timeout(1);
1561 current
->state
= TASK_RUNNING
;
1563 printk(KERN_INFO
"cosa: timeout in get_wait_data (status 0x%x)\n",
1564 cosa_getstatus(cosa
));
1569 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1570 * bit to be set in a loop. It should be used in the exceptional cases
1571 * only (for example when resetting the card or downloading the firmware).
1573 static int put_wait_data(struct cosa_data
*cosa
, int data
)
1577 /* read data and return them */
1578 if (cosa_getstatus(cosa
) & SR_TX_RDY
) {
1579 cosa_putdata8(cosa
, data
);
1581 printk(KERN_INFO
"Putdata: %d retries\n", 999-retries
);
1586 /* sleep if not ready to read */
1587 current
->state
= TASK_INTERRUPTIBLE
;
1588 schedule_timeout(1);
1589 current
->state
= TASK_RUNNING
;
1592 printk(KERN_INFO
"cosa%d: timeout in put_wait_data (status 0x%x)\n",
1593 cosa
->num
, cosa_getstatus(cosa
));
1598 * The following routine puts the hexadecimal number into the SRP monitor
1599 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1600 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1601 * (-2,-4,-6,-8) means that reading echo failed.
1603 static int puthexnumber(struct cosa_data
*cosa
, int number
)
1608 /* Well, I should probably replace this by something faster. */
1609 sprintf(temp
, "%04X", number
);
1610 for (i
=0; i
<4; i
++) {
1611 if (put_wait_data(cosa
, temp
[i
]) == -1) {
1612 printk(KERN_NOTICE
"cosa%d: puthexnumber failed to write byte %d\n",
1616 if (get_wait_data(cosa
) != temp
[i
]) {
1617 printk(KERN_NOTICE
"cosa%d: puthexhumber failed to read echo of byte %d\n",
1626 /* ---------- Interrupt routines ---------- */
1629 * There are three types of interrupt:
1630 * At the beginning of transmit - this handled is in tx_interrupt(),
1631 * at the beginning of receive - it is in rx_interrupt() and
1632 * at the end of transmit/receive - it is the eot_interrupt() function.
1633 * These functions are multiplexed by cosa_interrupt() according to the
1634 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1635 * separate functions to make it more readable. These functions are inline,
1636 * so there should be no overhead of function call.
1638 * In the COSA bus-master mode, we need to tell the card the address of a
1639 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1640 * It's time to use the bottom half :-(
1644 * Transmit interrupt routine - called when COSA is willing to obtain
1645 * data from the OS. The most tricky part of the routine is selection
1646 * of channel we (OS) want to send packet for. For SRP we should probably
1647 * use the round-robin approach. The newer COSA firmwares have a simple
1648 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1649 * channel 0 or 1 doesn't want to receive data.
1651 static inline void tx_interrupt(struct cosa_data
*cosa
, int status
)
1653 unsigned long flags
, flags1
;
1655 printk(KERN_INFO
"cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1658 spin_lock_irqsave(&cosa
->lock
, flags
);
1659 set_bit(TXBIT
, &cosa
->rxtx
);
1660 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1664 if (i
++ > cosa
->nchannels
) {
1666 "%s: No channel wants data in TX IRQ\n",
1668 put_driver_status_nolock(cosa
);
1669 clear_bit(TXBIT
, &cosa
->rxtx
);
1670 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1674 if (cosa
->txchan
>= cosa
->nchannels
)
1676 } while ((!(cosa
->txbitmap
& (1<<cosa
->txchan
)))
1677 || status
& (1<<(cosa
->txchan
+DRIVER_TXMAP_SHIFT
)));
1679 cosa
->txsize
= cosa
->chan
[cosa
->txchan
].txsize
;
1680 if (cosa_dma_able(cosa
->chan
+cosa
->txchan
,
1681 cosa
->chan
[cosa
->txchan
].txbuf
, cosa
->txsize
)) {
1682 cosa
->txbuf
= cosa
->chan
[cosa
->txchan
].txbuf
;
1684 memcpy(cosa
->bouncebuf
, cosa
->chan
[cosa
->txchan
].txbuf
,
1686 cosa
->txbuf
= cosa
->bouncebuf
;
1690 if (is_8bit(cosa
)) {
1691 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1692 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
1693 cosa_putdata8(cosa
, ((cosa
->txchan
<< 5) & 0xe0)|
1694 ((cosa
->txsize
>> 8) & 0x1f));
1696 debug_status_out(cosa
, SR_TX_INT_ENA
);
1697 debug_data_out(cosa
, ((cosa
->txchan
<< 5) & 0xe0)|
1698 ((cosa
->txsize
>> 8) & 0x1f));
1699 debug_data_in(cosa
, cosa_getdata8(cosa
));
1701 cosa_getdata8(cosa
);
1703 set_bit(IRQBIT
, &cosa
->rxtx
);
1704 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1707 clear_bit(IRQBIT
, &cosa
->rxtx
);
1708 cosa_putstatus(cosa
, 0);
1709 cosa_putdata8(cosa
, cosa
->txsize
&0xff);
1711 debug_status_out(cosa
, 0);
1712 debug_data_out(cosa
, cosa
->txsize
&0xff);
1716 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
1717 cosa_putdata16(cosa
, ((cosa
->txchan
<<13) & 0xe000)
1718 | (cosa
->txsize
& 0x1fff));
1720 debug_status_out(cosa
, SR_TX_INT_ENA
);
1721 debug_data_out(cosa
, ((cosa
->txchan
<<13) & 0xe000)
1722 | (cosa
->txsize
& 0x1fff));
1723 debug_data_in(cosa
, cosa_getdata8(cosa
));
1724 debug_status_out(cosa
, 0);
1726 cosa_getdata8(cosa
);
1728 cosa_putstatus(cosa
, 0);
1731 if (cosa
->busmaster
) {
1732 unsigned long addr
= virt_to_bus(cosa
->txbuf
);
1734 printk(KERN_INFO
"busmaster IRQ\n");
1735 while (!(cosa_getstatus(cosa
)&SR_TX_RDY
)) {
1738 if (count
> 1000) break;
1740 printk(KERN_INFO
"status %x\n", cosa_getstatus(cosa
));
1741 printk(KERN_INFO
"ready after %d loops\n", count
);
1742 cosa_putdata16(cosa
, (addr
>> 16)&0xffff);
1745 while (!(cosa_getstatus(cosa
)&SR_TX_RDY
)) {
1747 if (count
> 1000) break;
1750 printk(KERN_INFO
"ready after %d loops\n", count
);
1751 cosa_putdata16(cosa
, addr
&0xffff);
1752 flags1
= claim_dma_lock();
1753 set_dma_mode(cosa
->dma
, DMA_MODE_CASCADE
);
1754 enable_dma(cosa
->dma
);
1755 release_dma_lock(flags1
);
1758 flags1
= claim_dma_lock();
1759 disable_dma(cosa
->dma
);
1760 clear_dma_ff(cosa
->dma
);
1761 set_dma_mode(cosa
->dma
, DMA_MODE_WRITE
);
1762 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->txbuf
));
1763 set_dma_count(cosa
->dma
, cosa
->txsize
);
1764 enable_dma(cosa
->dma
);
1765 release_dma_lock(flags1
);
1767 cosa_putstatus(cosa
, SR_TX_DMA_ENA
|SR_USR_INT_ENA
);
1769 debug_status_out(cosa
, SR_TX_DMA_ENA
|SR_USR_INT_ENA
);
1771 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1774 static inline void rx_interrupt(struct cosa_data
*cosa
, int status
)
1776 unsigned long flags
;
1778 printk(KERN_INFO
"cosa%d: SR_UP_REQUEST\n", cosa
->num
);
1781 spin_lock_irqsave(&cosa
->lock
, flags
);
1782 set_bit(RXBIT
, &cosa
->rxtx
);
1784 if (is_8bit(cosa
)) {
1785 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1786 set_bit(IRQBIT
, &cosa
->rxtx
);
1787 cosa
->rxsize
= cosa_getdata8(cosa
) <<8;
1789 debug_data_in(cosa
, cosa
->rxsize
>> 8);
1791 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1794 clear_bit(IRQBIT
, &cosa
->rxtx
);
1795 cosa
->rxsize
|= cosa_getdata8(cosa
) & 0xff;
1797 debug_data_in(cosa
, cosa
->rxsize
& 0xff);
1800 printk(KERN_INFO
"cosa%d: receive rxsize = (0x%04x).\n",
1801 cosa
->num
, cosa
->rxsize
);
1805 cosa
->rxsize
= cosa_getdata16(cosa
);
1807 debug_data_in(cosa
, cosa
->rxsize
);
1810 printk(KERN_INFO
"cosa%d: receive rxsize = (0x%04x).\n",
1811 cosa
->num
, cosa
->rxsize
);
1814 if (((cosa
->rxsize
& 0xe000) >> 13) >= cosa
->nchannels
) {
1815 printk(KERN_WARNING
"%s: rx for unknown channel (0x%04x)\n",
1816 cosa
->name
, cosa
->rxsize
);
1817 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1820 cosa
->rxchan
= cosa
->chan
+ ((cosa
->rxsize
& 0xe000) >> 13);
1821 cosa
->rxsize
&= 0x1fff;
1822 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1825 if (cosa
->rxchan
->setup_rx
)
1826 cosa
->rxbuf
= cosa
->rxchan
->setup_rx(cosa
->rxchan
, cosa
->rxsize
);
1829 reject
: /* Reject the packet */
1830 printk(KERN_INFO
"cosa%d: rejecting packet on channel %d\n",
1831 cosa
->num
, cosa
->rxchan
->num
);
1832 cosa
->rxbuf
= cosa
->bouncebuf
;
1836 flags
= claim_dma_lock();
1837 disable_dma(cosa
->dma
);
1838 clear_dma_ff(cosa
->dma
);
1839 set_dma_mode(cosa
->dma
, DMA_MODE_READ
);
1840 if (cosa_dma_able(cosa
->rxchan
, cosa
->rxbuf
, cosa
->rxsize
& 0x1fff)) {
1841 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->rxbuf
));
1843 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->bouncebuf
));
1845 set_dma_count(cosa
->dma
, (cosa
->rxsize
&0x1fff));
1846 enable_dma(cosa
->dma
);
1847 release_dma_lock(flags
);
1848 spin_lock_irqsave(&cosa
->lock
, flags
);
1849 cosa_putstatus(cosa
, SR_RX_DMA_ENA
|SR_USR_INT_ENA
);
1850 if (!is_8bit(cosa
) && (status
& SR_TX_RDY
))
1851 cosa_putdata8(cosa
, DRIVER_RX_READY
);
1853 debug_status_out(cosa
, SR_RX_DMA_ENA
|SR_USR_INT_ENA
);
1854 if (!is_8bit(cosa
) && (status
& SR_TX_RDY
))
1855 debug_data_cmd(cosa
, DRIVER_RX_READY
);
1857 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1860 static void inline eot_interrupt(struct cosa_data
*cosa
, int status
)
1862 unsigned long flags
, flags1
;
1863 spin_lock_irqsave(&cosa
->lock
, flags
);
1864 flags1
= claim_dma_lock();
1865 disable_dma(cosa
->dma
);
1866 clear_dma_ff(cosa
->dma
);
1867 release_dma_lock(flags1
);
1868 if (test_bit(TXBIT
, &cosa
->rxtx
)) {
1869 struct channel_data
*chan
= cosa
->chan
+cosa
->txchan
;
1871 if (chan
->tx_done(chan
, cosa
->txsize
))
1872 clear_bit(chan
->num
, &cosa
->txbitmap
);
1873 } else if (test_bit(RXBIT
, &cosa
->rxtx
)) {
1877 printk(KERN_INFO
"cosa%dc%d: done rx(0x%x)", cosa
->num
,
1878 cosa
->rxchan
->num
, cosa
->rxsize
);
1879 for (i
=0; i
<cosa
->rxsize
; i
++)
1880 printk (" %02x", cosa
->rxbuf
[i
]&0xff);
1884 /* Packet for unknown channel? */
1885 if (cosa
->rxbuf
== cosa
->bouncebuf
)
1887 if (!cosa_dma_able(cosa
->rxchan
, cosa
->rxbuf
, cosa
->rxsize
))
1888 memcpy(cosa
->rxbuf
, cosa
->bouncebuf
, cosa
->rxsize
);
1889 if (cosa
->rxchan
->rx_done
)
1890 if (cosa
->rxchan
->rx_done(cosa
->rxchan
))
1891 clear_bit(cosa
->rxchan
->num
, &cosa
->rxbitmap
);
1893 printk(KERN_NOTICE
"cosa%d: unexpected EOT interrupt\n",
1897 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1898 * cleared anyway). We should do it as soon as possible
1899 * so that we can tell the COSA we are done and to give it a time
1904 put_driver_status_nolock(cosa
);
1905 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1908 static void cosa_interrupt(int irq
, void *cosa_
, struct pt_regs
*regs
)
1912 struct cosa_data
*cosa
= cosa_
;
1914 status
= cosa_getstatus(cosa
);
1916 printk(KERN_INFO
"cosa%d: got IRQ, status 0x%02x\n", cosa
->num
,
1920 debug_status_in(cosa
, status
);
1922 switch (status
& SR_CMD_FROM_SRP_MASK
) {
1923 case SR_DOWN_REQUEST
:
1924 tx_interrupt(cosa
, status
);
1927 rx_interrupt(cosa
, status
);
1929 case SR_END_OF_TRANSFER
:
1930 eot_interrupt(cosa
, status
);
1933 /* We may be too fast for SRP. Try to wait a bit more. */
1934 if (count
++ < 100) {
1938 printk(KERN_INFO
"cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1939 cosa
->num
, status
& 0xff, count
);
1943 printk(KERN_INFO
"%s: %d-times got unknown status in IRQ\n",
1946 printk(KERN_INFO
"%s: returning from IRQ\n", cosa
->name
);
1951 /* ---------- I/O debugging routines ---------- */
1953 * These routines can be used to monitor COSA/SRP I/O and to printk()
1954 * the data being transfered on the data and status I/O port in a
1959 static void debug_status_in(struct cosa_data
*cosa
, int status
)
1962 switch(status
& SR_CMD_FROM_SRP_MASK
) {
1966 case SR_DOWN_REQUEST
:
1969 case SR_END_OF_TRANSFER
:
1976 printk(KERN_INFO
"%s: IO: status -> 0x%02x (%s%s%s%s)\n",
1979 status
& SR_USR_RQ
? "USR_RQ|":"",
1980 status
& SR_TX_RDY
? "TX_RDY|":"",
1981 status
& SR_RX_RDY
? "RX_RDY|":"",
1985 static void debug_status_out(struct cosa_data
*cosa
, int status
)
1987 printk(KERN_INFO
"%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
1990 status
& SR_RX_DMA_ENA
? "RXDMA|":"!rxdma|",
1991 status
& SR_TX_DMA_ENA
? "TXDMA|":"!txdma|",
1992 status
& SR_RST
? "RESET|":"",
1993 status
& SR_USR_INT_ENA
? "USRINT|":"!usrint|",
1994 status
& SR_TX_INT_ENA
? "TXINT|":"!txint|",
1995 status
& SR_RX_INT_ENA
? "RXINT":"!rxint");
1998 static void debug_data_in(struct cosa_data
*cosa
, int data
)
2000 printk(KERN_INFO
"%s: IO: data -> 0x%04x\n", cosa
->name
, data
);
2003 static void debug_data_out(struct cosa_data
*cosa
, int data
)
2005 printk(KERN_INFO
"%s: IO: data <- 0x%04x\n", cosa
->name
, data
);
2008 static void debug_data_cmd(struct cosa_data
*cosa
, int data
)
2010 printk(KERN_INFO
"%s: IO: data <- 0x%04x (%s|%s)\n",
2012 data
& SR_RDY_RCV
? "RX_RDY" : "!rx_rdy",
2013 data
& SR_RDY_SND
? "TX_RDY" : "!tx_rdy");
2017 /* EOF -- this file has not been truncated */