1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
4 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
5 * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * The driver for the SRP and COSA synchronous serial cards.
27 * Both cards are developed at the Institute of Computer Science,
28 * Masaryk University (http://www.ics.muni.cz/). The hardware is
29 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
30 * and the photo of both cards is available at
31 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
32 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
33 * For Linux-specific utilities, see below in the "Software info" section.
34 * If you want to order the card, contact Jiri Novotny.
36 * The SRP (serial port?, the Czech word "srp" means "sickle") card
37 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
38 * with V.24 interfaces up to 80kb/s each.
40 * The COSA (communication serial adapter?, the Czech word "kosa" means
41 * "scythe") is a next-generation sync/async board with two interfaces
42 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
43 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
44 * The 8-channels version is in development.
46 * Both types have downloadable firmware and communicate via ISA DMA.
47 * COSA can be also a bus-mastering device.
51 * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
52 * The CVS tree of Linux driver can be viewed there, as well as the
53 * firmware binaries and user-space utilities for downloading the firmware
54 * into the card and setting up the card.
56 * The Linux driver (unlike the present *BSD drivers :-) can work even
57 * for the COSA and SRP in one computer and allows each channel to work
58 * in one of the two modes (character or network device).
62 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
64 * You can mail me bugfixes and even success reports. I am especially
65 * interested in the SMP and/or muliti-channel success/failure reports
66 * (I wonder if I did the locking properly :-).
68 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
70 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
71 * The skeleton.c by Donald Becker
72 * The SDL Riscom/N2 driver by Mike Natale
73 * The Comtrol Hostess SV11 driver by Alan Cox
74 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
77 #include <linux/module.h>
78 #include <linux/kernel.h>
79 #include <linux/slab.h>
80 #include <linux/poll.h>
82 #include <linux/interrupt.h>
83 #include <linux/delay.h>
84 #include <linux/hdlc.h>
85 #include <linux/errno.h>
86 #include <linux/ioport.h>
87 #include <linux/netdevice.h>
88 #include <linux/spinlock.h>
89 #include <linux/mutex.h>
90 #include <linux/device.h>
91 #include <linux/smp_lock.h>
94 #include <asm/byteorder.h>
96 #undef COSA_SLOW_IO /* for testing purposes only */
100 /* Maximum length of the identification string. */
101 #define COSA_MAX_ID_STRING 128
103 /* Maximum length of the channel name */
104 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
106 /* Per-channel data structure */
108 struct channel_data
{
109 int usage
; /* Usage count; >0 for chrdev, -1 for netdev */
110 int num
; /* Number of the channel */
111 struct cosa_data
*cosa
; /* Pointer to the per-card structure */
112 int txsize
; /* Size of transmitted data */
113 char *txbuf
; /* Transmit buffer */
114 char name
[COSA_MAX_NAME
]; /* channel name */
116 /* The HW layer interface */
117 /* routine called from the RX interrupt */
118 char *(*setup_rx
)(struct channel_data
*channel
, int size
);
119 /* routine called when the RX is done (from the EOT interrupt) */
120 int (*rx_done
)(struct channel_data
*channel
);
121 /* routine called when the TX is done (from the EOT interrupt) */
122 int (*tx_done
)(struct channel_data
*channel
, int size
);
124 /* Character device parts */
126 struct semaphore wsem
;
129 wait_queue_head_t txwaitq
, rxwaitq
;
130 int tx_status
, rx_status
;
132 /* generic HDLC device parts */
133 struct net_device
*netdev
;
134 struct sk_buff
*rx_skb
, *tx_skb
;
137 /* cosa->firmware_status bits */
138 #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
139 #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
140 #define COSA_FW_START (1<<2) /* Is the microcode running? */
143 int num
; /* Card number */
144 char name
[COSA_MAX_NAME
]; /* Card name - e.g "cosa0" */
145 unsigned int datareg
, statusreg
; /* I/O ports */
146 unsigned short irq
, dma
; /* IRQ and DMA number */
147 unsigned short startaddr
; /* Firmware start address */
148 unsigned short busmaster
; /* Use busmastering? */
149 int nchannels
; /* # of channels on this card */
150 int driver_status
; /* For communicating with firmware */
151 int firmware_status
; /* Downloaded, reseted, etc. */
152 unsigned long rxbitmap
, txbitmap
;/* Bitmap of channels who are willing to send/receive data */
153 unsigned long rxtx
; /* RX or TX in progress? */
155 int usage
; /* usage count */
156 int txchan
, txsize
, rxsize
;
157 struct channel_data
*rxchan
;
160 struct channel_data
*chan
;
161 spinlock_t lock
; /* For exclusive operations on this structure */
162 char id_string
[COSA_MAX_ID_STRING
]; /* ROM monitor ID string */
163 char *type
; /* card type */
167 * Define this if you want all the possible ports to be autoprobed.
168 * It is here but it probably is not a good idea to use this.
170 /* #define COSA_ISA_AUTOPROBE 1 */
173 * Character device major number. 117 was allocated for us.
174 * The value of 0 means to allocate a first free one.
176 static int cosa_major
= 117;
179 * Encoding of the minor numbers:
180 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
181 * the highest bits means the card number.
183 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
184 * for the single card */
186 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
187 * macro doesn't like anything other than the raw number as an argument :-(
190 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
192 #define DRIVER_RX_READY 0x0001
193 #define DRIVER_TX_READY 0x0002
194 #define DRIVER_TXMAP_SHIFT 2
195 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
198 * for cosa->rxtx - indicates whether either transmit or receive is
199 * in progress. These values are mean number of the bit.
205 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
207 #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
208 #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
209 #undef DEBUG_IO //1 /* Dump the I/O traffic */
211 #define TX_TIMEOUT (5*HZ)
213 /* Maybe the following should be allocated dynamically */
214 static struct cosa_data cosa_cards
[MAX_CARDS
];
217 #ifdef COSA_ISA_AUTOPROBE
218 static int io
[MAX_CARDS
+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
219 /* NOTE: DMA is not autoprobed!!! */
220 static int dma
[MAX_CARDS
+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
222 static int io
[MAX_CARDS
+1];
223 static int dma
[MAX_CARDS
+1];
225 /* IRQ can be safely autoprobed */
226 static int irq
[MAX_CARDS
+1] = { -1, -1, -1, -1, -1, -1, 0, };
229 static struct class *cosa_class
;
232 module_param_array(io
, int, NULL
, 0);
233 MODULE_PARM_DESC(io
, "The I/O bases of the COSA or SRP cards");
234 module_param_array(irq
, int, NULL
, 0);
235 MODULE_PARM_DESC(irq
, "The IRQ lines of the COSA or SRP cards");
236 module_param_array(dma
, int, NULL
, 0);
237 MODULE_PARM_DESC(dma
, "The DMA channels of the COSA or SRP cards");
239 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
240 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
241 MODULE_LICENSE("GPL");
244 /* I use this mainly for testing purposes */
246 #define cosa_outb outb_p
247 #define cosa_outw outw_p
248 #define cosa_inb inb_p
249 #define cosa_inw inw_p
251 #define cosa_outb outb
252 #define cosa_outw outw
257 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
259 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
260 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
261 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
262 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
263 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
264 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
266 /* Initialization stuff */
267 static int cosa_probe(int ioaddr
, int irq
, int dma
);
270 static void cosa_enable_rx(struct channel_data
*chan
);
271 static void cosa_disable_rx(struct channel_data
*chan
);
272 static int cosa_start_tx(struct channel_data
*channel
, char *buf
, int size
);
273 static void cosa_kick(struct cosa_data
*cosa
);
274 static int cosa_dma_able(struct channel_data
*chan
, char *buf
, int data
);
276 /* Network device stuff */
277 static int cosa_net_attach(struct net_device
*dev
, unsigned short encoding
,
278 unsigned short parity
);
279 static int cosa_net_open(struct net_device
*d
);
280 static int cosa_net_close(struct net_device
*d
);
281 static void cosa_net_timeout(struct net_device
*d
);
282 static netdev_tx_t
cosa_net_tx(struct sk_buff
*skb
, struct net_device
*d
);
283 static char *cosa_net_setup_rx(struct channel_data
*channel
, int size
);
284 static int cosa_net_rx_done(struct channel_data
*channel
);
285 static int cosa_net_tx_done(struct channel_data
*channel
, int size
);
286 static int cosa_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
288 /* Character device */
289 static char *chrdev_setup_rx(struct channel_data
*channel
, int size
);
290 static int chrdev_rx_done(struct channel_data
*channel
);
291 static int chrdev_tx_done(struct channel_data
*channel
, int size
);
292 static ssize_t
cosa_read(struct file
*file
,
293 char __user
*buf
, size_t count
, loff_t
*ppos
);
294 static ssize_t
cosa_write(struct file
*file
,
295 const char __user
*buf
, size_t count
, loff_t
*ppos
);
296 static unsigned int cosa_poll(struct file
*file
, poll_table
*poll
);
297 static int cosa_open(struct inode
*inode
, struct file
*file
);
298 static int cosa_release(struct inode
*inode
, struct file
*file
);
299 static int cosa_chardev_ioctl(struct inode
*inode
, struct file
*file
,
300 unsigned int cmd
, unsigned long arg
);
301 #ifdef COSA_FASYNC_WORKING
302 static int cosa_fasync(struct inode
*inode
, struct file
*file
, int on
);
305 static const struct file_operations cosa_fops
= {
306 .owner
= THIS_MODULE
,
311 .ioctl
= cosa_chardev_ioctl
,
313 .release
= cosa_release
,
314 #ifdef COSA_FASYNC_WORKING
315 .fasync
= cosa_fasync
,
320 static int cosa_start(struct cosa_data
*cosa
, int address
);
321 static int cosa_reset(struct cosa_data
*cosa
);
322 static int cosa_download(struct cosa_data
*cosa
, void __user
*a
);
323 static int cosa_readmem(struct cosa_data
*cosa
, void __user
*a
);
325 /* COSA/SRP ROM monitor */
326 static int download(struct cosa_data
*cosa
, const char __user
*data
, int addr
, int len
);
327 static int startmicrocode(struct cosa_data
*cosa
, int address
);
328 static int readmem(struct cosa_data
*cosa
, char __user
*data
, int addr
, int len
);
329 static int cosa_reset_and_read_id(struct cosa_data
*cosa
, char *id
);
331 /* Auxilliary functions */
332 static int get_wait_data(struct cosa_data
*cosa
);
333 static int put_wait_data(struct cosa_data
*cosa
, int data
);
334 static int puthexnumber(struct cosa_data
*cosa
, int number
);
335 static void put_driver_status(struct cosa_data
*cosa
);
336 static void put_driver_status_nolock(struct cosa_data
*cosa
);
338 /* Interrupt handling */
339 static irqreturn_t
cosa_interrupt(int irq
, void *cosa
);
341 /* I/O ops debugging */
343 static void debug_data_in(struct cosa_data
*cosa
, int data
);
344 static void debug_data_out(struct cosa_data
*cosa
, int data
);
345 static void debug_data_cmd(struct cosa_data
*cosa
, int data
);
346 static void debug_status_in(struct cosa_data
*cosa
, int status
);
347 static void debug_status_out(struct cosa_data
*cosa
, int status
);
350 static inline struct channel_data
* dev_to_chan(struct net_device
*dev
)
352 return (struct channel_data
*)dev_to_hdlc(dev
)->priv
;
355 /* ---------- Initialization stuff ---------- */
357 static int __init
cosa_init(void)
361 if (cosa_major
> 0) {
362 if (register_chrdev(cosa_major
, "cosa", &cosa_fops
)) {
363 printk(KERN_WARNING
"cosa: unable to get major %d\n",
369 if (!(cosa_major
=register_chrdev(0, "cosa", &cosa_fops
))) {
370 printk(KERN_WARNING
"cosa: unable to register chardev\n");
375 for (i
=0; i
<MAX_CARDS
; i
++)
376 cosa_cards
[i
].num
= -1;
377 for (i
=0; io
[i
] != 0 && i
< MAX_CARDS
; i
++)
378 cosa_probe(io
[i
], irq
[i
], dma
[i
]);
380 printk(KERN_WARNING
"cosa: no devices found.\n");
381 unregister_chrdev(cosa_major
, "cosa");
385 cosa_class
= class_create(THIS_MODULE
, "cosa");
386 if (IS_ERR(cosa_class
)) {
387 err
= PTR_ERR(cosa_class
);
390 for (i
= 0; i
< nr_cards
; i
++)
391 device_create(cosa_class
, NULL
, MKDEV(cosa_major
, i
), NULL
,
397 unregister_chrdev(cosa_major
, "cosa");
401 module_init(cosa_init
);
403 static void __exit
cosa_exit(void)
405 struct cosa_data
*cosa
;
408 for (i
= 0; i
< nr_cards
; i
++)
409 device_destroy(cosa_class
, MKDEV(cosa_major
, i
));
410 class_destroy(cosa_class
);
412 for (cosa
= cosa_cards
; nr_cards
--; cosa
++) {
413 /* Clean up the per-channel data */
414 for (i
= 0; i
< cosa
->nchannels
; i
++) {
415 /* Chardev driver has no alloc'd per-channel data */
416 unregister_hdlc_device(cosa
->chan
[i
].netdev
);
417 free_netdev(cosa
->chan
[i
].netdev
);
419 /* Clean up the per-card data */
421 kfree(cosa
->bouncebuf
);
422 free_irq(cosa
->irq
, cosa
);
424 release_region(cosa
->datareg
, is_8bit(cosa
) ? 2 : 4);
426 unregister_chrdev(cosa_major
, "cosa");
428 module_exit(cosa_exit
);
430 static const struct net_device_ops cosa_ops
= {
431 .ndo_open
= cosa_net_open
,
432 .ndo_stop
= cosa_net_close
,
433 .ndo_change_mtu
= hdlc_change_mtu
,
434 .ndo_start_xmit
= hdlc_start_xmit
,
435 .ndo_do_ioctl
= cosa_net_ioctl
,
436 .ndo_tx_timeout
= cosa_net_timeout
,
439 static int cosa_probe(int base
, int irq
, int dma
)
441 struct cosa_data
*cosa
= cosa_cards
+nr_cards
;
444 memset(cosa
, 0, sizeof(struct cosa_data
));
446 /* Checking validity of parameters: */
447 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
448 if ((irq
>= 0 && irq
< 2) || irq
> 15 || (irq
< 10 && irq
> 7)) {
449 printk (KERN_INFO
"cosa_probe: invalid IRQ %d\n", irq
);
452 /* I/O address should be between 0x100 and 0x3ff and should be
454 if (base
< 0x100 || base
> 0x3ff || base
& 0x7) {
455 printk (KERN_INFO
"cosa_probe: invalid I/O address 0x%x\n",
459 /* DMA should be 0,1 or 3-7 */
460 if (dma
< 0 || dma
== 4 || dma
> 7) {
461 printk (KERN_INFO
"cosa_probe: invalid DMA %d\n", dma
);
464 /* and finally, on 16-bit COSA DMA should be 4-7 and
465 * I/O base should not be multiple of 0x10 */
466 if (((base
& 0x8) && dma
< 4) || (!(base
& 0x8) && dma
> 3)) {
467 printk (KERN_INFO
"cosa_probe: 8/16 bit base and DMA mismatch"
468 " (base=0x%x, dma=%d)\n", base
, dma
);
473 cosa
->datareg
= base
;
474 cosa
->statusreg
= is_8bit(cosa
)?base
+1:base
+2;
475 spin_lock_init(&cosa
->lock
);
477 if (!request_region(base
, is_8bit(cosa
)?2:4,"cosa"))
480 if (cosa_reset_and_read_id(cosa
, cosa
->id_string
) < 0) {
481 printk(KERN_DEBUG
"cosa: probe at 0x%x failed.\n", base
);
486 /* Test the validity of identification string */
487 if (!strncmp(cosa
->id_string
, "SRP", 3))
489 else if (!strncmp(cosa
->id_string
, "COSA", 4))
490 cosa
->type
= is_8bit(cosa
)? "cosa8": "cosa16";
492 /* Print a warning only if we are not autoprobing */
493 #ifndef COSA_ISA_AUTOPROBE
494 printk(KERN_INFO
"cosa: valid signature not found at 0x%x.\n",
500 /* Update the name of the region now we know the type of card */
501 release_region(base
, is_8bit(cosa
)?2:4);
502 if (!request_region(base
, is_8bit(cosa
)?2:4, cosa
->type
)) {
503 printk(KERN_DEBUG
"cosa: changing name at 0x%x failed.\n", base
);
507 /* Now do IRQ autoprobe */
510 /* printk(KERN_INFO "IRQ autoprobe\n"); */
511 irqs
= probe_irq_on();
513 * Enable interrupt on tx buffer empty (it sure is)
515 * FIXME: When this code is not used as module, we should
516 * probably call udelay() instead of the interruptible sleep.
518 set_current_state(TASK_INTERRUPTIBLE
);
519 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
520 schedule_timeout(30);
521 irq
= probe_irq_off(irqs
);
522 /* Disable all IRQs from the card */
523 cosa_putstatus(cosa
, 0);
524 /* Empty the received data register */
528 printk (KERN_INFO
"cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
534 printk (KERN_INFO
"cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
541 cosa
->num
= nr_cards
;
543 cosa
->nchannels
= 2; /* FIXME: how to determine this? */
545 if (request_irq(cosa
->irq
, cosa_interrupt
, 0, cosa
->type
, cosa
)) {
549 if (request_dma(cosa
->dma
, cosa
->type
)) {
554 cosa
->bouncebuf
= kmalloc(COSA_MTU
, GFP_KERNEL
|GFP_DMA
);
555 if (!cosa
->bouncebuf
) {
559 sprintf(cosa
->name
, "cosa%d", cosa
->num
);
561 /* Initialize the per-channel data */
562 cosa
->chan
= kcalloc(cosa
->nchannels
, sizeof(struct channel_data
), GFP_KERNEL
);
568 for (i
= 0; i
< cosa
->nchannels
; i
++) {
569 struct channel_data
*chan
= &cosa
->chan
[i
];
573 sprintf(chan
->name
, "cosa%dc%d", chan
->cosa
->num
, i
);
575 /* Initialize the chardev data structures */
576 mutex_init(&chan
->rlock
);
577 init_MUTEX(&chan
->wsem
);
579 /* Register the network interface */
580 if (!(chan
->netdev
= alloc_hdlcdev(chan
))) {
581 printk(KERN_WARNING
"%s: alloc_hdlcdev failed.\n",
585 dev_to_hdlc(chan
->netdev
)->attach
= cosa_net_attach
;
586 dev_to_hdlc(chan
->netdev
)->xmit
= cosa_net_tx
;
587 chan
->netdev
->netdev_ops
= &cosa_ops
;
588 chan
->netdev
->watchdog_timeo
= TX_TIMEOUT
;
589 chan
->netdev
->base_addr
= chan
->cosa
->datareg
;
590 chan
->netdev
->irq
= chan
->cosa
->irq
;
591 chan
->netdev
->dma
= chan
->cosa
->dma
;
592 if (register_hdlc_device(chan
->netdev
)) {
593 printk(KERN_WARNING
"%s: register_hdlc_device()"
594 " failed.\n", chan
->netdev
->name
);
595 free_netdev(chan
->netdev
);
600 printk (KERN_INFO
"cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
601 cosa
->num
, cosa
->id_string
, cosa
->type
,
602 cosa
->datareg
, cosa
->irq
, cosa
->dma
, cosa
->nchannels
);
608 unregister_hdlc_device(cosa
->chan
[i
].netdev
);
609 free_netdev(cosa
->chan
[i
].netdev
);
613 kfree(cosa
->bouncebuf
);
617 free_irq(cosa
->irq
, cosa
);
619 release_region(cosa
->datareg
,is_8bit(cosa
)?2:4);
620 printk(KERN_NOTICE
"cosa%d: allocating resources failed\n",
626 /*---------- network device ---------- */
628 static int cosa_net_attach(struct net_device
*dev
, unsigned short encoding
,
629 unsigned short parity
)
631 if (encoding
== ENCODING_NRZ
&& parity
== PARITY_CRC16_PR1_CCITT
)
636 static int cosa_net_open(struct net_device
*dev
)
638 struct channel_data
*chan
= dev_to_chan(dev
);
642 if (!(chan
->cosa
->firmware_status
& COSA_FW_START
)) {
643 printk(KERN_NOTICE
"%s: start the firmware first (status %d)\n",
644 chan
->cosa
->name
, chan
->cosa
->firmware_status
);
647 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
648 if (chan
->usage
!= 0) {
649 printk(KERN_WARNING
"%s: cosa_net_open called with usage count"
650 " %d\n", chan
->name
, chan
->usage
);
651 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
654 chan
->setup_rx
= cosa_net_setup_rx
;
655 chan
->tx_done
= cosa_net_tx_done
;
656 chan
->rx_done
= cosa_net_rx_done
;
659 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
661 err
= hdlc_open(dev
);
663 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
666 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
670 netif_start_queue(dev
);
671 cosa_enable_rx(chan
);
675 static netdev_tx_t
cosa_net_tx(struct sk_buff
*skb
,
676 struct net_device
*dev
)
678 struct channel_data
*chan
= dev_to_chan(dev
);
680 netif_stop_queue(dev
);
683 cosa_start_tx(chan
, skb
->data
, skb
->len
);
687 static void cosa_net_timeout(struct net_device
*dev
)
689 struct channel_data
*chan
= dev_to_chan(dev
);
691 if (test_bit(RXBIT
, &chan
->cosa
->rxtx
)) {
692 chan
->netdev
->stats
.rx_errors
++;
693 chan
->netdev
->stats
.rx_missed_errors
++;
695 chan
->netdev
->stats
.tx_errors
++;
696 chan
->netdev
->stats
.tx_aborted_errors
++;
698 cosa_kick(chan
->cosa
);
700 dev_kfree_skb(chan
->tx_skb
);
703 netif_wake_queue(dev
);
706 static int cosa_net_close(struct net_device
*dev
)
708 struct channel_data
*chan
= dev_to_chan(dev
);
711 netif_stop_queue(dev
);
713 cosa_disable_rx(chan
);
714 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
716 kfree_skb(chan
->rx_skb
);
720 kfree_skb(chan
->tx_skb
);
725 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
729 static char *cosa_net_setup_rx(struct channel_data
*chan
, int size
)
732 * We can safely fall back to non-dma-able memory, because we have
733 * the cosa->bouncebuf pre-allocated.
735 kfree_skb(chan
->rx_skb
);
736 chan
->rx_skb
= dev_alloc_skb(size
);
737 if (chan
->rx_skb
== NULL
) {
738 printk(KERN_NOTICE
"%s: Memory squeeze, dropping packet\n",
740 chan
->netdev
->stats
.rx_dropped
++;
743 chan
->netdev
->trans_start
= jiffies
;
744 return skb_put(chan
->rx_skb
, size
);
747 static int cosa_net_rx_done(struct channel_data
*chan
)
750 printk(KERN_WARNING
"%s: rx_done with empty skb!\n",
752 chan
->netdev
->stats
.rx_errors
++;
753 chan
->netdev
->stats
.rx_frame_errors
++;
756 chan
->rx_skb
->protocol
= hdlc_type_trans(chan
->rx_skb
, chan
->netdev
);
757 chan
->rx_skb
->dev
= chan
->netdev
;
758 skb_reset_mac_header(chan
->rx_skb
);
759 chan
->netdev
->stats
.rx_packets
++;
760 chan
->netdev
->stats
.rx_bytes
+= chan
->cosa
->rxsize
;
761 netif_rx(chan
->rx_skb
);
767 static int cosa_net_tx_done(struct channel_data
*chan
, int size
)
770 printk(KERN_WARNING
"%s: tx_done with empty skb!\n",
772 chan
->netdev
->stats
.tx_errors
++;
773 chan
->netdev
->stats
.tx_aborted_errors
++;
776 dev_kfree_skb_irq(chan
->tx_skb
);
778 chan
->netdev
->stats
.tx_packets
++;
779 chan
->netdev
->stats
.tx_bytes
+= size
;
780 netif_wake_queue(chan
->netdev
);
784 /*---------- Character device ---------- */
786 static ssize_t
cosa_read(struct file
*file
,
787 char __user
*buf
, size_t count
, loff_t
*ppos
)
789 DECLARE_WAITQUEUE(wait
, current
);
791 struct channel_data
*chan
= file
->private_data
;
792 struct cosa_data
*cosa
= chan
->cosa
;
795 if (!(cosa
->firmware_status
& COSA_FW_START
)) {
796 printk(KERN_NOTICE
"%s: start the firmware first (status %d)\n",
797 cosa
->name
, cosa
->firmware_status
);
800 if (mutex_lock_interruptible(&chan
->rlock
))
803 if ((chan
->rxdata
= kmalloc(COSA_MTU
, GFP_DMA
|GFP_KERNEL
)) == NULL
) {
804 printk(KERN_INFO
"%s: cosa_read() - OOM\n", cosa
->name
);
805 mutex_unlock(&chan
->rlock
);
810 cosa_enable_rx(chan
);
811 spin_lock_irqsave(&cosa
->lock
, flags
);
812 add_wait_queue(&chan
->rxwaitq
, &wait
);
813 while(!chan
->rx_status
) {
814 current
->state
= TASK_INTERRUPTIBLE
;
815 spin_unlock_irqrestore(&cosa
->lock
, flags
);
817 spin_lock_irqsave(&cosa
->lock
, flags
);
818 if (signal_pending(current
) && chan
->rx_status
== 0) {
820 remove_wait_queue(&chan
->rxwaitq
, &wait
);
821 current
->state
= TASK_RUNNING
;
822 spin_unlock_irqrestore(&cosa
->lock
, flags
);
823 mutex_unlock(&chan
->rlock
);
827 remove_wait_queue(&chan
->rxwaitq
, &wait
);
828 current
->state
= TASK_RUNNING
;
830 count
= chan
->rxsize
;
831 spin_unlock_irqrestore(&cosa
->lock
, flags
);
832 mutex_unlock(&chan
->rlock
);
834 if (copy_to_user(buf
, kbuf
, count
)) {
842 static char *chrdev_setup_rx(struct channel_data
*chan
, int size
)
844 /* Expect size <= COSA_MTU */
849 static int chrdev_rx_done(struct channel_data
*chan
)
851 if (chan
->rx_status
) { /* Reader has died */
856 wake_up_interruptible(&chan
->rxwaitq
);
861 static ssize_t
cosa_write(struct file
*file
,
862 const char __user
*buf
, size_t count
, loff_t
*ppos
)
864 DECLARE_WAITQUEUE(wait
, current
);
865 struct channel_data
*chan
= file
->private_data
;
866 struct cosa_data
*cosa
= chan
->cosa
;
870 if (!(cosa
->firmware_status
& COSA_FW_START
)) {
871 printk(KERN_NOTICE
"%s: start the firmware first (status %d)\n",
872 cosa
->name
, cosa
->firmware_status
);
875 if (down_interruptible(&chan
->wsem
))
878 if (count
> COSA_MTU
)
881 /* Allocate the buffer */
882 if ((kbuf
= kmalloc(count
, GFP_KERNEL
|GFP_DMA
)) == NULL
) {
883 printk(KERN_NOTICE
"%s: cosa_write() OOM - dropping packet\n",
888 if (copy_from_user(kbuf
, buf
, count
)) {
894 cosa_start_tx(chan
, kbuf
, count
);
896 spin_lock_irqsave(&cosa
->lock
, flags
);
897 add_wait_queue(&chan
->txwaitq
, &wait
);
898 while(!chan
->tx_status
) {
899 current
->state
= TASK_INTERRUPTIBLE
;
900 spin_unlock_irqrestore(&cosa
->lock
, flags
);
902 spin_lock_irqsave(&cosa
->lock
, flags
);
903 if (signal_pending(current
) && chan
->tx_status
== 0) {
905 remove_wait_queue(&chan
->txwaitq
, &wait
);
906 current
->state
= TASK_RUNNING
;
908 spin_unlock_irqrestore(&cosa
->lock
, flags
);
912 remove_wait_queue(&chan
->txwaitq
, &wait
);
913 current
->state
= TASK_RUNNING
;
915 spin_unlock_irqrestore(&cosa
->lock
, flags
);
920 static int chrdev_tx_done(struct channel_data
*chan
, int size
)
922 if (chan
->tx_status
) { /* Writer was interrupted */
927 wake_up_interruptible(&chan
->txwaitq
);
931 static unsigned int cosa_poll(struct file
*file
, poll_table
*poll
)
933 printk(KERN_INFO
"cosa_poll is here\n");
937 static int cosa_open(struct inode
*inode
, struct file
*file
)
939 struct cosa_data
*cosa
;
940 struct channel_data
*chan
;
946 if ((n
=iminor(file
->f_path
.dentry
->d_inode
)>>CARD_MINOR_BITS
)
953 if ((n
=iminor(file
->f_path
.dentry
->d_inode
)
954 & ((1<<CARD_MINOR_BITS
)-1)) >= cosa
->nchannels
) {
958 chan
= cosa
->chan
+ n
;
960 file
->private_data
= chan
;
962 spin_lock_irqsave(&cosa
->lock
, flags
);
964 if (chan
->usage
< 0) { /* in netdev mode */
965 spin_unlock_irqrestore(&cosa
->lock
, flags
);
972 chan
->tx_done
= chrdev_tx_done
;
973 chan
->setup_rx
= chrdev_setup_rx
;
974 chan
->rx_done
= chrdev_rx_done
;
975 spin_unlock_irqrestore(&cosa
->lock
, flags
);
981 static int cosa_release(struct inode
*inode
, struct file
*file
)
983 struct channel_data
*channel
= file
->private_data
;
984 struct cosa_data
*cosa
;
987 cosa
= channel
->cosa
;
988 spin_lock_irqsave(&cosa
->lock
, flags
);
991 spin_unlock_irqrestore(&cosa
->lock
, flags
);
995 #ifdef COSA_FASYNC_WORKING
996 static struct fasync_struct
*fasync
[256] = { NULL
, };
999 static int cosa_fasync(struct inode
*inode
, struct file
*file
, int on
)
1001 int port
= iminor(inode
);
1003 return fasync_helper(inode
, file
, on
, &fasync
[port
]);
1008 /* ---------- Ioctls ---------- */
1011 * Ioctl subroutines can safely be made inline, because they are called
1012 * only from cosa_ioctl().
1014 static inline int cosa_reset(struct cosa_data
*cosa
)
1016 char idstring
[COSA_MAX_ID_STRING
];
1017 if (cosa
->usage
> 1)
1018 printk(KERN_INFO
"cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1019 cosa
->num
, cosa
->usage
);
1020 cosa
->firmware_status
&= ~(COSA_FW_RESET
|COSA_FW_START
);
1021 if (cosa_reset_and_read_id(cosa
, idstring
) < 0) {
1022 printk(KERN_NOTICE
"cosa%d: reset failed\n", cosa
->num
);
1025 printk(KERN_INFO
"cosa%d: resetting device: %s\n", cosa
->num
,
1027 cosa
->firmware_status
|= COSA_FW_RESET
;
1031 /* High-level function to download data into COSA memory. Calls download() */
1032 static inline int cosa_download(struct cosa_data
*cosa
, void __user
*arg
)
1034 struct cosa_download d
;
1037 if (cosa
->usage
> 1)
1038 printk(KERN_INFO
"%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1039 cosa
->name
, cosa
->usage
);
1040 if (!(cosa
->firmware_status
& COSA_FW_RESET
)) {
1041 printk(KERN_NOTICE
"%s: reset the card first (status %d).\n",
1042 cosa
->name
, cosa
->firmware_status
);
1046 if (copy_from_user(&d
, arg
, sizeof(d
)))
1049 if (d
.addr
< 0 || d
.addr
> COSA_MAX_FIRMWARE_SIZE
)
1051 if (d
.len
< 0 || d
.len
> COSA_MAX_FIRMWARE_SIZE
)
1055 /* If something fails, force the user to reset the card */
1056 cosa
->firmware_status
&= ~(COSA_FW_RESET
|COSA_FW_DOWNLOAD
);
1058 i
= download(cosa
, d
.code
, d
.len
, d
.addr
);
1060 printk(KERN_NOTICE
"cosa%d: microcode download failed: %d\n",
1064 printk(KERN_INFO
"cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1065 cosa
->num
, d
.len
, d
.addr
);
1066 cosa
->firmware_status
|= COSA_FW_RESET
|COSA_FW_DOWNLOAD
;
1070 /* High-level function to read COSA memory. Calls readmem() */
1071 static inline int cosa_readmem(struct cosa_data
*cosa
, void __user
*arg
)
1073 struct cosa_download d
;
1076 if (cosa
->usage
> 1)
1077 printk(KERN_INFO
"cosa%d: WARNING: readmem requested with "
1078 "cosa->usage > 1 (%d). Odd things may happen.\n",
1079 cosa
->num
, cosa
->usage
);
1080 if (!(cosa
->firmware_status
& COSA_FW_RESET
)) {
1081 printk(KERN_NOTICE
"%s: reset the card first (status %d).\n",
1082 cosa
->name
, cosa
->firmware_status
);
1086 if (copy_from_user(&d
, arg
, sizeof(d
)))
1089 /* If something fails, force the user to reset the card */
1090 cosa
->firmware_status
&= ~COSA_FW_RESET
;
1092 i
= readmem(cosa
, d
.code
, d
.len
, d
.addr
);
1094 printk(KERN_NOTICE
"cosa%d: reading memory failed: %d\n",
1098 printk(KERN_INFO
"cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1099 cosa
->num
, d
.len
, d
.addr
);
1100 cosa
->firmware_status
|= COSA_FW_RESET
;
1104 /* High-level function to start microcode. Calls startmicrocode(). */
1105 static inline int cosa_start(struct cosa_data
*cosa
, int address
)
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
);
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
);
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
;
1132 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1133 static inline int cosa_getidstr(struct cosa_data
*cosa
, char __user
*string
)
1135 int l
= strlen(cosa
->id_string
)+1;
1136 if (copy_to_user(string
, cosa
->id_string
, l
))
1141 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1142 static inline int cosa_gettype(struct cosa_data
*cosa
, char __user
*string
)
1144 int l
= strlen(cosa
->type
)+1;
1145 if (copy_to_user(string
, cosa
->type
, l
))
1150 static int cosa_ioctl_common(struct cosa_data
*cosa
,
1151 struct channel_data
*channel
, unsigned int cmd
, unsigned long arg
)
1153 void __user
*argp
= (void __user
*)arg
;
1155 case COSAIORSET
: /* Reset the device */
1156 if (!capable(CAP_NET_ADMIN
))
1158 return cosa_reset(cosa
);
1159 case COSAIOSTRT
: /* Start the firmware */
1160 if (!capable(CAP_SYS_RAWIO
))
1162 return cosa_start(cosa
, arg
);
1163 case COSAIODOWNLD
: /* Download the firmware */
1164 if (!capable(CAP_SYS_RAWIO
))
1167 return cosa_download(cosa
, argp
);
1169 if (!capable(CAP_SYS_RAWIO
))
1171 return cosa_readmem(cosa
, argp
);
1173 return cosa_gettype(cosa
, argp
);
1175 return cosa_getidstr(cosa
, argp
);
1179 return cosa
->nchannels
;
1181 if (!capable(CAP_SYS_RAWIO
))
1185 if (arg
!= COSA_BM_OFF
&& arg
!= COSA_BM_ON
)
1187 cosa
->busmaster
= arg
;
1190 return cosa
->busmaster
;
1192 return -ENOIOCTLCMD
;
1195 static int cosa_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1198 struct channel_data
*chan
= dev_to_chan(dev
);
1199 rv
= cosa_ioctl_common(chan
->cosa
, chan
, cmd
,
1200 (unsigned long)ifr
->ifr_data
);
1201 if (rv
!= -ENOIOCTLCMD
)
1203 return hdlc_ioctl(dev
, ifr
, cmd
);
1206 static int cosa_chardev_ioctl(struct inode
*inode
, struct file
*file
,
1207 unsigned int cmd
, unsigned long arg
)
1209 struct channel_data
*channel
= file
->private_data
;
1210 struct cosa_data
*cosa
= channel
->cosa
;
1211 return cosa_ioctl_common(cosa
, channel
, cmd
, arg
);
1215 /*---------- HW layer interface ---------- */
1218 * The higher layer can bind itself to the HW layer by setting the callbacks
1219 * in the channel_data structure and by using these routines.
1221 static void cosa_enable_rx(struct channel_data
*chan
)
1223 struct cosa_data
*cosa
= chan
->cosa
;
1225 if (!test_and_set_bit(chan
->num
, &cosa
->rxbitmap
))
1226 put_driver_status(cosa
);
1229 static void cosa_disable_rx(struct channel_data
*chan
)
1231 struct cosa_data
*cosa
= chan
->cosa
;
1233 if (test_and_clear_bit(chan
->num
, &cosa
->rxbitmap
))
1234 put_driver_status(cosa
);
1238 * FIXME: This routine probably should check for cosa_start_tx() called when
1239 * the previous transmit is still unfinished. In this case the non-zero
1240 * return value should indicate to the caller that the queuing(sp?) up
1241 * the transmit has failed.
1243 static int cosa_start_tx(struct channel_data
*chan
, char *buf
, int len
)
1245 struct cosa_data
*cosa
= chan
->cosa
;
1246 unsigned long flags
;
1250 printk(KERN_INFO
"cosa%dc%d: starting tx(0x%x)", chan
->cosa
->num
,
1252 for (i
=0; i
<len
; i
++)
1253 printk(" %02x", buf
[i
]&0xff);
1256 spin_lock_irqsave(&cosa
->lock
, flags
);
1260 chan
->txsize
= COSA_MTU
;
1261 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1263 /* Tell the firmware we are ready */
1264 set_bit(chan
->num
, &cosa
->txbitmap
);
1265 put_driver_status(cosa
);
1270 static void put_driver_status(struct cosa_data
*cosa
)
1272 unsigned long flags
;
1275 spin_lock_irqsave(&cosa
->lock
, flags
);
1277 status
= (cosa
->rxbitmap
? DRIVER_RX_READY
: 0)
1278 | (cosa
->txbitmap
? DRIVER_TX_READY
: 0)
1279 | (cosa
->txbitmap
? ~(cosa
->txbitmap
<<DRIVER_TXMAP_SHIFT
)
1280 &DRIVER_TXMAP_MASK
: 0);
1282 if (cosa
->rxbitmap
|cosa
->txbitmap
) {
1283 if (!cosa
->enabled
) {
1284 cosa_putstatus(cosa
, SR_RX_INT_ENA
);
1286 debug_status_out(cosa
, SR_RX_INT_ENA
);
1290 } else if (cosa
->enabled
) {
1292 cosa_putstatus(cosa
, 0);
1294 debug_status_out(cosa
, 0);
1297 cosa_putdata8(cosa
, status
);
1299 debug_data_cmd(cosa
, status
);
1302 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1305 static void put_driver_status_nolock(struct cosa_data
*cosa
)
1309 status
= (cosa
->rxbitmap
? DRIVER_RX_READY
: 0)
1310 | (cosa
->txbitmap
? DRIVER_TX_READY
: 0)
1311 | (cosa
->txbitmap
? ~(cosa
->txbitmap
<<DRIVER_TXMAP_SHIFT
)
1312 &DRIVER_TXMAP_MASK
: 0);
1314 if (cosa
->rxbitmap
|cosa
->txbitmap
) {
1315 cosa_putstatus(cosa
, SR_RX_INT_ENA
);
1317 debug_status_out(cosa
, SR_RX_INT_ENA
);
1321 cosa_putstatus(cosa
, 0);
1323 debug_status_out(cosa
, 0);
1327 cosa_putdata8(cosa
, status
);
1329 debug_data_cmd(cosa
, status
);
1334 * The "kickme" function: When the DMA times out, this is called to
1335 * clean up the driver status.
1336 * FIXME: Preliminary support, the interface is probably wrong.
1338 static void cosa_kick(struct cosa_data
*cosa
)
1340 unsigned long flags
, flags1
;
1341 char *s
= "(probably) IRQ";
1343 if (test_bit(RXBIT
, &cosa
->rxtx
))
1345 if (test_bit(TXBIT
, &cosa
->rxtx
))
1348 printk(KERN_INFO
"%s: %s timeout - restarting.\n", cosa
->name
, s
);
1349 spin_lock_irqsave(&cosa
->lock
, flags
);
1352 flags1
= claim_dma_lock();
1353 disable_dma(cosa
->dma
);
1354 clear_dma_ff(cosa
->dma
);
1355 release_dma_lock(flags1
);
1357 /* FIXME: Anything else? */
1359 cosa_putstatus(cosa
, 0);
1361 (void) cosa_getdata8(cosa
);
1363 cosa_putdata8(cosa
, 0);
1365 put_driver_status_nolock(cosa
);
1366 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1370 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1371 * physical memory and doesn't span the 64k boundary. For now it seems
1372 * SKB's never do this, but we'll check this anyway.
1374 static int cosa_dma_able(struct channel_data
*chan
, char *buf
, int len
)
1377 unsigned long b
= (unsigned long)buf
;
1378 if (b
+len
>= MAX_DMA_ADDRESS
)
1380 if ((b
^ (b
+len
)) & 0x10000) {
1382 printk(KERN_INFO
"%s: packet spanning a 64k boundary\n",
1390 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1393 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1394 * drivers need to say 4-digit hex number meaning start address of the microcode
1395 * separated by a single space. Monitor replies by saying " =". Now driver
1396 * has to write 4-digit hex number meaning the last byte address ended
1397 * by a single space. Monitor has to reply with a space. Now the download
1398 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1400 static int download(struct cosa_data
*cosa
, const char __user
*microcode
, int length
, int address
)
1404 if (put_wait_data(cosa
, 'w') == -1) return -1;
1405 if ((i
=get_wait_data(cosa
)) != 'w') { printk("dnld: 0x%04x\n",i
); return -2;}
1406 if (get_wait_data(cosa
) != '=') return -3;
1408 if (puthexnumber(cosa
, address
) < 0) return -4;
1409 if (put_wait_data(cosa
, ' ') == -1) return -10;
1410 if (get_wait_data(cosa
) != ' ') return -11;
1411 if (get_wait_data(cosa
) != '=') return -12;
1413 if (puthexnumber(cosa
, address
+length
-1) < 0) return -13;
1414 if (put_wait_data(cosa
, ' ') == -1) return -18;
1415 if (get_wait_data(cosa
) != ' ') return -19;
1419 #ifndef SRP_DOWNLOAD_AT_BOOT
1420 if (get_user(c
, microcode
))
1421 return -23; /* ??? */
1425 if (put_wait_data(cosa
, c
) == -1)
1430 if (get_wait_data(cosa
) != '\r') return -21;
1431 if (get_wait_data(cosa
) != '\n') return -22;
1432 if (get_wait_data(cosa
) != '.') return -23;
1434 printk(KERN_DEBUG
"cosa%d: download completed.\n", cosa
->num
);
1441 * Starting microcode is done via the "g" command of the SRP monitor.
1442 * The chat should be the following: "g" "g=" "<addr><CR>"
1443 * "<CR><CR><LF><CR><LF>".
1445 static int startmicrocode(struct cosa_data
*cosa
, int address
)
1447 if (put_wait_data(cosa
, 'g') == -1) return -1;
1448 if (get_wait_data(cosa
) != 'g') return -2;
1449 if (get_wait_data(cosa
) != '=') return -3;
1451 if (puthexnumber(cosa
, address
) < 0) return -4;
1452 if (put_wait_data(cosa
, '\r') == -1) return -5;
1454 if (get_wait_data(cosa
) != '\r') return -6;
1455 if (get_wait_data(cosa
) != '\r') return -7;
1456 if (get_wait_data(cosa
) != '\n') return -8;
1457 if (get_wait_data(cosa
) != '\r') return -9;
1458 if (get_wait_data(cosa
) != '\n') return -10;
1460 printk(KERN_DEBUG
"cosa%d: microcode started\n", cosa
->num
);
1466 * Reading memory is done via the "r" command of the SRP monitor.
1467 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1468 * Then driver can read the data and the conversation is finished
1469 * by SRP monitor sending "<CR><LF>." (dot at the end).
1471 * This routine is not needed during the normal operation and serves
1472 * for debugging purposes only.
1474 static int readmem(struct cosa_data
*cosa
, char __user
*microcode
, int length
, int address
)
1476 if (put_wait_data(cosa
, 'r') == -1) return -1;
1477 if ((get_wait_data(cosa
)) != 'r') return -2;
1478 if ((get_wait_data(cosa
)) != '=') return -3;
1480 if (puthexnumber(cosa
, address
) < 0) return -4;
1481 if (put_wait_data(cosa
, ' ') == -1) return -5;
1482 if (get_wait_data(cosa
) != ' ') return -6;
1483 if (get_wait_data(cosa
) != '=') return -7;
1485 if (puthexnumber(cosa
, address
+length
-1) < 0) return -8;
1486 if (put_wait_data(cosa
, ' ') == -1) return -9;
1487 if (get_wait_data(cosa
) != ' ') return -10;
1492 if ((i
=get_wait_data(cosa
)) == -1) {
1493 printk (KERN_INFO
"cosa: 0x%04x bytes remaining\n",
1499 if (put_user(c
, microcode
))
1500 return -23; /* ??? */
1507 if (get_wait_data(cosa
) != '\r') return -21;
1508 if (get_wait_data(cosa
) != '\n') return -22;
1509 if (get_wait_data(cosa
) != '.') return -23;
1511 printk(KERN_DEBUG
"cosa%d: readmem completed.\n", cosa
->num
);
1517 * This function resets the device and reads the initial prompt
1518 * of the device's ROM monitor.
1520 static int cosa_reset_and_read_id(struct cosa_data
*cosa
, char *idstring
)
1522 int i
=0, id
=0, prev
=0, curr
=0;
1524 /* Reset the card ... */
1525 cosa_putstatus(cosa
, 0);
1526 cosa_getdata8(cosa
);
1527 cosa_putstatus(cosa
, SR_RST
);
1533 /* Disable all IRQs from the card */
1534 cosa_putstatus(cosa
, 0);
1537 * Try to read the ID string. The card then prints out the
1538 * identification string ended by the "\n\x2e".
1540 * The following loop is indexed through i (instead of id)
1541 * to avoid looping forever when for any reason
1542 * the port returns '\r', '\n' or '\x2e' permanently.
1544 for (i
=0; i
<COSA_MAX_ID_STRING
-1; i
++, prev
=curr
) {
1545 if ((curr
= get_wait_data(cosa
)) == -1) {
1549 if (curr
!= '\r' && curr
!= '\n' && curr
!= 0x2e)
1550 idstring
[id
++] = curr
;
1551 if (curr
== 0x2e && prev
== '\n')
1554 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1555 idstring
[id
] = '\0';
1560 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1563 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1564 * bit to be set in a loop. It should be used in the exceptional cases
1565 * only (for example when resetting the card or downloading the firmware.
1567 static int get_wait_data(struct cosa_data
*cosa
)
1572 /* read data and return them */
1573 if (cosa_getstatus(cosa
) & SR_RX_RDY
) {
1575 r
= cosa_getdata8(cosa
);
1577 printk(KERN_INFO
"cosa: get_wait_data returning after %d retries\n", 999-retries
);
1581 /* sleep if not ready to read */
1582 schedule_timeout_interruptible(1);
1584 printk(KERN_INFO
"cosa: timeout in get_wait_data (status 0x%x)\n",
1585 cosa_getstatus(cosa
));
1590 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1591 * bit to be set in a loop. It should be used in the exceptional cases
1592 * only (for example when resetting the card or downloading the firmware).
1594 static int put_wait_data(struct cosa_data
*cosa
, int data
)
1598 /* read data and return them */
1599 if (cosa_getstatus(cosa
) & SR_TX_RDY
) {
1600 cosa_putdata8(cosa
, data
);
1602 printk(KERN_INFO
"Putdata: %d retries\n", 999-retries
);
1607 /* sleep if not ready to read */
1608 schedule_timeout_interruptible(1);
1611 printk(KERN_INFO
"cosa%d: timeout in put_wait_data (status 0x%x)\n",
1612 cosa
->num
, cosa_getstatus(cosa
));
1617 * The following routine puts the hexadecimal number into the SRP monitor
1618 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1619 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1620 * (-2,-4,-6,-8) means that reading echo failed.
1622 static int puthexnumber(struct cosa_data
*cosa
, int number
)
1627 /* Well, I should probably replace this by something faster. */
1628 sprintf(temp
, "%04X", number
);
1629 for (i
=0; i
<4; i
++) {
1630 if (put_wait_data(cosa
, temp
[i
]) == -1) {
1631 printk(KERN_NOTICE
"cosa%d: puthexnumber failed to write byte %d\n",
1635 if (get_wait_data(cosa
) != temp
[i
]) {
1636 printk(KERN_NOTICE
"cosa%d: puthexhumber failed to read echo of byte %d\n",
1645 /* ---------- Interrupt routines ---------- */
1648 * There are three types of interrupt:
1649 * At the beginning of transmit - this handled is in tx_interrupt(),
1650 * at the beginning of receive - it is in rx_interrupt() and
1651 * at the end of transmit/receive - it is the eot_interrupt() function.
1652 * These functions are multiplexed by cosa_interrupt() according to the
1653 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1654 * separate functions to make it more readable. These functions are inline,
1655 * so there should be no overhead of function call.
1657 * In the COSA bus-master mode, we need to tell the card the address of a
1658 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1659 * It's time to use the bottom half :-(
1663 * Transmit interrupt routine - called when COSA is willing to obtain
1664 * data from the OS. The most tricky part of the routine is selection
1665 * of channel we (OS) want to send packet for. For SRP we should probably
1666 * use the round-robin approach. The newer COSA firmwares have a simple
1667 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1668 * channel 0 or 1 doesn't want to receive data.
1670 * It seems there is a bug in COSA firmware (need to trace it further):
1671 * When the driver status says that the kernel has no more data for transmit
1672 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1673 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1674 * the TX interrupt but does not mark the channel as ready-to-transmit.
1675 * The fix seems to be to push the packet to COSA despite its request.
1676 * We first try to obey the card's opinion, and then fall back to forced TX.
1678 static inline void tx_interrupt(struct cosa_data
*cosa
, int status
)
1680 unsigned long flags
, flags1
;
1682 printk(KERN_INFO
"cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1685 spin_lock_irqsave(&cosa
->lock
, flags
);
1686 set_bit(TXBIT
, &cosa
->rxtx
);
1687 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1688 /* flow control, see the comment above */
1690 if (!cosa
->txbitmap
) {
1691 printk(KERN_WARNING
"%s: No channel wants data "
1692 "in TX IRQ. Expect DMA timeout.",
1694 put_driver_status_nolock(cosa
);
1695 clear_bit(TXBIT
, &cosa
->rxtx
);
1696 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1702 if (cosa
->txchan
>= cosa
->nchannels
)
1704 if (!(cosa
->txbitmap
& (1<<cosa
->txchan
)))
1706 if (~status
& (1 << (cosa
->txchan
+DRIVER_TXMAP_SHIFT
)))
1708 /* in second pass, accept first ready-to-TX channel */
1709 if (i
> cosa
->nchannels
) {
1710 /* Can be safely ignored */
1712 printk(KERN_DEBUG
"%s: Forcing TX "
1713 "to not-ready channel %d\n",
1714 cosa
->name
, cosa
->txchan
);
1720 cosa
->txsize
= cosa
->chan
[cosa
->txchan
].txsize
;
1721 if (cosa_dma_able(cosa
->chan
+cosa
->txchan
,
1722 cosa
->chan
[cosa
->txchan
].txbuf
, cosa
->txsize
)) {
1723 cosa
->txbuf
= cosa
->chan
[cosa
->txchan
].txbuf
;
1725 memcpy(cosa
->bouncebuf
, cosa
->chan
[cosa
->txchan
].txbuf
,
1727 cosa
->txbuf
= cosa
->bouncebuf
;
1731 if (is_8bit(cosa
)) {
1732 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1733 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
1734 cosa_putdata8(cosa
, ((cosa
->txchan
<< 5) & 0xe0)|
1735 ((cosa
->txsize
>> 8) & 0x1f));
1737 debug_status_out(cosa
, SR_TX_INT_ENA
);
1738 debug_data_out(cosa
, ((cosa
->txchan
<< 5) & 0xe0)|
1739 ((cosa
->txsize
>> 8) & 0x1f));
1740 debug_data_in(cosa
, cosa_getdata8(cosa
));
1742 cosa_getdata8(cosa
);
1744 set_bit(IRQBIT
, &cosa
->rxtx
);
1745 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1748 clear_bit(IRQBIT
, &cosa
->rxtx
);
1749 cosa_putstatus(cosa
, 0);
1750 cosa_putdata8(cosa
, cosa
->txsize
&0xff);
1752 debug_status_out(cosa
, 0);
1753 debug_data_out(cosa
, cosa
->txsize
&0xff);
1757 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
1758 cosa_putdata16(cosa
, ((cosa
->txchan
<<13) & 0xe000)
1759 | (cosa
->txsize
& 0x1fff));
1761 debug_status_out(cosa
, SR_TX_INT_ENA
);
1762 debug_data_out(cosa
, ((cosa
->txchan
<<13) & 0xe000)
1763 | (cosa
->txsize
& 0x1fff));
1764 debug_data_in(cosa
, cosa_getdata8(cosa
));
1765 debug_status_out(cosa
, 0);
1767 cosa_getdata8(cosa
);
1769 cosa_putstatus(cosa
, 0);
1772 if (cosa
->busmaster
) {
1773 unsigned long addr
= virt_to_bus(cosa
->txbuf
);
1775 printk(KERN_INFO
"busmaster IRQ\n");
1776 while (!(cosa_getstatus(cosa
)&SR_TX_RDY
)) {
1779 if (count
> 1000) break;
1781 printk(KERN_INFO
"status %x\n", cosa_getstatus(cosa
));
1782 printk(KERN_INFO
"ready after %d loops\n", count
);
1783 cosa_putdata16(cosa
, (addr
>> 16)&0xffff);
1786 while (!(cosa_getstatus(cosa
)&SR_TX_RDY
)) {
1788 if (count
> 1000) break;
1791 printk(KERN_INFO
"ready after %d loops\n", count
);
1792 cosa_putdata16(cosa
, addr
&0xffff);
1793 flags1
= claim_dma_lock();
1794 set_dma_mode(cosa
->dma
, DMA_MODE_CASCADE
);
1795 enable_dma(cosa
->dma
);
1796 release_dma_lock(flags1
);
1799 flags1
= claim_dma_lock();
1800 disable_dma(cosa
->dma
);
1801 clear_dma_ff(cosa
->dma
);
1802 set_dma_mode(cosa
->dma
, DMA_MODE_WRITE
);
1803 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->txbuf
));
1804 set_dma_count(cosa
->dma
, cosa
->txsize
);
1805 enable_dma(cosa
->dma
);
1806 release_dma_lock(flags1
);
1808 cosa_putstatus(cosa
, SR_TX_DMA_ENA
|SR_USR_INT_ENA
);
1810 debug_status_out(cosa
, SR_TX_DMA_ENA
|SR_USR_INT_ENA
);
1812 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1815 static inline void rx_interrupt(struct cosa_data
*cosa
, int status
)
1817 unsigned long flags
;
1819 printk(KERN_INFO
"cosa%d: SR_UP_REQUEST\n", cosa
->num
);
1822 spin_lock_irqsave(&cosa
->lock
, flags
);
1823 set_bit(RXBIT
, &cosa
->rxtx
);
1825 if (is_8bit(cosa
)) {
1826 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1827 set_bit(IRQBIT
, &cosa
->rxtx
);
1828 put_driver_status_nolock(cosa
);
1829 cosa
->rxsize
= cosa_getdata8(cosa
) <<8;
1831 debug_data_in(cosa
, cosa
->rxsize
>> 8);
1833 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1836 clear_bit(IRQBIT
, &cosa
->rxtx
);
1837 cosa
->rxsize
|= cosa_getdata8(cosa
) & 0xff;
1839 debug_data_in(cosa
, cosa
->rxsize
& 0xff);
1842 printk(KERN_INFO
"cosa%d: receive rxsize = (0x%04x).\n",
1843 cosa
->num
, cosa
->rxsize
);
1847 cosa
->rxsize
= cosa_getdata16(cosa
);
1849 debug_data_in(cosa
, cosa
->rxsize
);
1852 printk(KERN_INFO
"cosa%d: receive rxsize = (0x%04x).\n",
1853 cosa
->num
, cosa
->rxsize
);
1856 if (((cosa
->rxsize
& 0xe000) >> 13) >= cosa
->nchannels
) {
1857 printk(KERN_WARNING
"%s: rx for unknown channel (0x%04x)\n",
1858 cosa
->name
, cosa
->rxsize
);
1859 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1862 cosa
->rxchan
= cosa
->chan
+ ((cosa
->rxsize
& 0xe000) >> 13);
1863 cosa
->rxsize
&= 0x1fff;
1864 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1867 if (cosa
->rxchan
->setup_rx
)
1868 cosa
->rxbuf
= cosa
->rxchan
->setup_rx(cosa
->rxchan
, cosa
->rxsize
);
1871 reject
: /* Reject the packet */
1872 printk(KERN_INFO
"cosa%d: rejecting packet on channel %d\n",
1873 cosa
->num
, cosa
->rxchan
->num
);
1874 cosa
->rxbuf
= cosa
->bouncebuf
;
1878 flags
= claim_dma_lock();
1879 disable_dma(cosa
->dma
);
1880 clear_dma_ff(cosa
->dma
);
1881 set_dma_mode(cosa
->dma
, DMA_MODE_READ
);
1882 if (cosa_dma_able(cosa
->rxchan
, cosa
->rxbuf
, cosa
->rxsize
& 0x1fff)) {
1883 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->rxbuf
));
1885 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->bouncebuf
));
1887 set_dma_count(cosa
->dma
, (cosa
->rxsize
&0x1fff));
1888 enable_dma(cosa
->dma
);
1889 release_dma_lock(flags
);
1890 spin_lock_irqsave(&cosa
->lock
, flags
);
1891 cosa_putstatus(cosa
, SR_RX_DMA_ENA
|SR_USR_INT_ENA
);
1892 if (!is_8bit(cosa
) && (status
& SR_TX_RDY
))
1893 cosa_putdata8(cosa
, DRIVER_RX_READY
);
1895 debug_status_out(cosa
, SR_RX_DMA_ENA
|SR_USR_INT_ENA
);
1896 if (!is_8bit(cosa
) && (status
& SR_TX_RDY
))
1897 debug_data_cmd(cosa
, DRIVER_RX_READY
);
1899 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1902 static inline void eot_interrupt(struct cosa_data
*cosa
, int status
)
1904 unsigned long flags
, flags1
;
1905 spin_lock_irqsave(&cosa
->lock
, flags
);
1906 flags1
= claim_dma_lock();
1907 disable_dma(cosa
->dma
);
1908 clear_dma_ff(cosa
->dma
);
1909 release_dma_lock(flags1
);
1910 if (test_bit(TXBIT
, &cosa
->rxtx
)) {
1911 struct channel_data
*chan
= cosa
->chan
+cosa
->txchan
;
1913 if (chan
->tx_done(chan
, cosa
->txsize
))
1914 clear_bit(chan
->num
, &cosa
->txbitmap
);
1915 } else if (test_bit(RXBIT
, &cosa
->rxtx
)) {
1919 printk(KERN_INFO
"cosa%dc%d: done rx(0x%x)", cosa
->num
,
1920 cosa
->rxchan
->num
, cosa
->rxsize
);
1921 for (i
=0; i
<cosa
->rxsize
; i
++)
1922 printk (" %02x", cosa
->rxbuf
[i
]&0xff);
1926 /* Packet for unknown channel? */
1927 if (cosa
->rxbuf
== cosa
->bouncebuf
)
1929 if (!cosa_dma_able(cosa
->rxchan
, cosa
->rxbuf
, cosa
->rxsize
))
1930 memcpy(cosa
->rxbuf
, cosa
->bouncebuf
, cosa
->rxsize
);
1931 if (cosa
->rxchan
->rx_done
)
1932 if (cosa
->rxchan
->rx_done(cosa
->rxchan
))
1933 clear_bit(cosa
->rxchan
->num
, &cosa
->rxbitmap
);
1935 printk(KERN_NOTICE
"cosa%d: unexpected EOT interrupt\n",
1939 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1940 * cleared anyway). We should do it as soon as possible
1941 * so that we can tell the COSA we are done and to give it a time
1946 put_driver_status_nolock(cosa
);
1947 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1950 static irqreturn_t
cosa_interrupt(int irq
, void *cosa_
)
1954 struct cosa_data
*cosa
= cosa_
;
1956 status
= cosa_getstatus(cosa
);
1958 printk(KERN_INFO
"cosa%d: got IRQ, status 0x%02x\n", cosa
->num
,
1962 debug_status_in(cosa
, status
);
1964 switch (status
& SR_CMD_FROM_SRP_MASK
) {
1965 case SR_DOWN_REQUEST
:
1966 tx_interrupt(cosa
, status
);
1969 rx_interrupt(cosa
, status
);
1971 case SR_END_OF_TRANSFER
:
1972 eot_interrupt(cosa
, status
);
1975 /* We may be too fast for SRP. Try to wait a bit more. */
1976 if (count
++ < 100) {
1980 printk(KERN_INFO
"cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1981 cosa
->num
, status
& 0xff, count
);
1985 printk(KERN_INFO
"%s: %d-times got unknown status in IRQ\n",
1988 printk(KERN_INFO
"%s: returning from IRQ\n", cosa
->name
);
1994 /* ---------- I/O debugging routines ---------- */
1996 * These routines can be used to monitor COSA/SRP I/O and to printk()
1997 * the data being transferred on the data and status I/O port in a
2002 static void debug_status_in(struct cosa_data
*cosa
, int status
)
2005 switch(status
& SR_CMD_FROM_SRP_MASK
) {
2009 case SR_DOWN_REQUEST
:
2012 case SR_END_OF_TRANSFER
:
2019 printk(KERN_INFO
"%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2022 status
& SR_USR_RQ
? "USR_RQ|":"",
2023 status
& SR_TX_RDY
? "TX_RDY|":"",
2024 status
& SR_RX_RDY
? "RX_RDY|":"",
2028 static void debug_status_out(struct cosa_data
*cosa
, int status
)
2030 printk(KERN_INFO
"%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2033 status
& SR_RX_DMA_ENA
? "RXDMA|":"!rxdma|",
2034 status
& SR_TX_DMA_ENA
? "TXDMA|":"!txdma|",
2035 status
& SR_RST
? "RESET|":"",
2036 status
& SR_USR_INT_ENA
? "USRINT|":"!usrint|",
2037 status
& SR_TX_INT_ENA
? "TXINT|":"!txint|",
2038 status
& SR_RX_INT_ENA
? "RXINT":"!rxint");
2041 static void debug_data_in(struct cosa_data
*cosa
, int data
)
2043 printk(KERN_INFO
"%s: IO: data -> 0x%04x\n", cosa
->name
, data
);
2046 static void debug_data_out(struct cosa_data
*cosa
, int data
)
2048 printk(KERN_INFO
"%s: IO: data <- 0x%04x\n", cosa
->name
, data
);
2051 static void debug_data_cmd(struct cosa_data
*cosa
, int data
)
2053 printk(KERN_INFO
"%s: IO: data <- 0x%04x (%s|%s)\n",
2055 data
& SR_RDY_RCV
? "RX_RDY" : "!rx_rdy",
2056 data
& SR_RDY_SND
? "TX_RDY" : "!tx_rdy");
2060 /* EOF -- this file has not been truncated */