1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
5 * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
6 * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
10 * The driver for the SRP and COSA synchronous serial cards.
14 * Both cards are developed at the Institute of Computer Science,
15 * Masaryk University (http://www.ics.muni.cz/). The hardware is
16 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
17 * and the photo of both cards is available at
18 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
19 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
20 * For Linux-specific utilities, see below in the "Software info" section.
21 * If you want to order the card, contact Jiri Novotny.
23 * The SRP (serial port?, the Czech word "srp" means "sickle") card
24 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
25 * with V.24 interfaces up to 80kb/s each.
27 * The COSA (communication serial adapter?, the Czech word "kosa" means
28 * "scythe") is a next-generation sync/async board with two interfaces
29 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
30 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
31 * The 8-channels version is in development.
33 * Both types have downloadable firmware and communicate via ISA DMA.
34 * COSA can be also a bus-mastering device.
38 * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
39 * The CVS tree of Linux driver can be viewed there, as well as the
40 * firmware binaries and user-space utilities for downloading the firmware
41 * into the card and setting up the card.
43 * The Linux driver (unlike the present *BSD drivers :-) can work even
44 * for the COSA and SRP in one computer and allows each channel to work
45 * in one of the two modes (character or network device).
49 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
51 * You can mail me bugfixes and even success reports. I am especially
52 * interested in the SMP and/or muliti-channel success/failure reports
53 * (I wonder if I did the locking properly :-).
55 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
57 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
58 * The skeleton.c by Donald Becker
59 * The SDL Riscom/N2 driver by Mike Natale
60 * The Comtrol Hostess SV11 driver by Alan Cox
61 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
64 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
66 #include <linux/module.h>
67 #include <linux/kernel.h>
68 #include <linux/sched/signal.h>
69 #include <linux/slab.h>
70 #include <linux/poll.h>
72 #include <linux/interrupt.h>
73 #include <linux/delay.h>
74 #include <linux/hdlc.h>
75 #include <linux/errno.h>
76 #include <linux/ioport.h>
77 #include <linux/netdevice.h>
78 #include <linux/spinlock.h>
79 #include <linux/mutex.h>
80 #include <linux/device.h>
83 #include <asm/byteorder.h>
85 #undef COSA_SLOW_IO /* for testing purposes only */
89 /* Maximum length of the identification string. */
90 #define COSA_MAX_ID_STRING 128
92 /* Maximum length of the channel name */
93 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
95 /* Per-channel data structure */
98 int usage
; /* Usage count; >0 for chrdev, -1 for netdev */
99 int num
; /* Number of the channel */
100 struct cosa_data
*cosa
; /* Pointer to the per-card structure */
101 int txsize
; /* Size of transmitted data */
102 char *txbuf
; /* Transmit buffer */
103 char name
[COSA_MAX_NAME
]; /* channel name */
105 /* The HW layer interface */
106 /* routine called from the RX interrupt */
107 char *(*setup_rx
)(struct channel_data
*channel
, int size
);
108 /* routine called when the RX is done (from the EOT interrupt) */
109 int (*rx_done
)(struct channel_data
*channel
);
110 /* routine called when the TX is done (from the EOT interrupt) */
111 int (*tx_done
)(struct channel_data
*channel
, int size
);
113 /* Character device parts */
115 struct semaphore wsem
;
118 wait_queue_head_t txwaitq
, rxwaitq
;
119 int tx_status
, rx_status
;
121 /* generic HDLC device parts */
122 struct net_device
*netdev
;
123 struct sk_buff
*rx_skb
, *tx_skb
;
126 /* cosa->firmware_status bits */
127 #define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
128 #define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
129 #define COSA_FW_START (1<<2) /* Is the microcode running? */
132 int num
; /* Card number */
133 char name
[COSA_MAX_NAME
]; /* Card name - e.g "cosa0" */
134 unsigned int datareg
, statusreg
; /* I/O ports */
135 unsigned short irq
, dma
; /* IRQ and DMA number */
136 unsigned short startaddr
; /* Firmware start address */
137 unsigned short busmaster
; /* Use busmastering? */
138 int nchannels
; /* # of channels on this card */
139 int driver_status
; /* For communicating with firmware */
140 int firmware_status
; /* Downloaded, reseted, etc. */
141 unsigned long rxbitmap
, txbitmap
;/* Bitmap of channels who are willing to send/receive data */
142 unsigned long rxtx
; /* RX or TX in progress? */
144 int usage
; /* usage count */
145 int txchan
, txsize
, rxsize
;
146 struct channel_data
*rxchan
;
149 struct channel_data
*chan
;
150 spinlock_t lock
; /* For exclusive operations on this structure */
151 char id_string
[COSA_MAX_ID_STRING
]; /* ROM monitor ID string */
152 char *type
; /* card type */
156 * Define this if you want all the possible ports to be autoprobed.
157 * It is here but it probably is not a good idea to use this.
159 /* #define COSA_ISA_AUTOPROBE 1 */
162 * Character device major number. 117 was allocated for us.
163 * The value of 0 means to allocate a first free one.
165 static DEFINE_MUTEX(cosa_chardev_mutex
);
166 static int cosa_major
= 117;
169 * Encoding of the minor numbers:
170 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
171 * the highest bits means the card number.
173 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
174 * for the single card */
176 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
177 * macro doesn't like anything other than the raw number as an argument :-(
180 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
182 #define DRIVER_RX_READY 0x0001
183 #define DRIVER_TX_READY 0x0002
184 #define DRIVER_TXMAP_SHIFT 2
185 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
188 * for cosa->rxtx - indicates whether either transmit or receive is
189 * in progress. These values are mean number of the bit.
195 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
197 #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
198 #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
199 #undef DEBUG_IO //1 /* Dump the I/O traffic */
201 #define TX_TIMEOUT (5*HZ)
203 /* Maybe the following should be allocated dynamically */
204 static struct cosa_data cosa_cards
[MAX_CARDS
];
207 #ifdef COSA_ISA_AUTOPROBE
208 static int io
[MAX_CARDS
+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
209 /* NOTE: DMA is not autoprobed!!! */
210 static int dma
[MAX_CARDS
+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
212 static int io
[MAX_CARDS
+1];
213 static int dma
[MAX_CARDS
+1];
215 /* IRQ can be safely autoprobed */
216 static int irq
[MAX_CARDS
+1] = { -1, -1, -1, -1, -1, -1, 0, };
219 static struct class *cosa_class
;
222 module_param_hw_array(io
, int, ioport
, NULL
, 0);
223 MODULE_PARM_DESC(io
, "The I/O bases of the COSA or SRP cards");
224 module_param_hw_array(irq
, int, irq
, NULL
, 0);
225 MODULE_PARM_DESC(irq
, "The IRQ lines of the COSA or SRP cards");
226 module_param_hw_array(dma
, int, dma
, NULL
, 0);
227 MODULE_PARM_DESC(dma
, "The DMA channels of the COSA or SRP cards");
229 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
230 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
231 MODULE_LICENSE("GPL");
234 /* I use this mainly for testing purposes */
236 #define cosa_outb outb_p
237 #define cosa_outw outw_p
238 #define cosa_inb inb_p
239 #define cosa_inw inw_p
241 #define cosa_outb outb
242 #define cosa_outw outw
247 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
249 #define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
250 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
251 #define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
252 #define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
253 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
254 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
256 /* Initialization stuff */
257 static int cosa_probe(int ioaddr
, int irq
, int dma
);
260 static void cosa_enable_rx(struct channel_data
*chan
);
261 static void cosa_disable_rx(struct channel_data
*chan
);
262 static int cosa_start_tx(struct channel_data
*channel
, char *buf
, int size
);
263 static void cosa_kick(struct cosa_data
*cosa
);
264 static int cosa_dma_able(struct channel_data
*chan
, char *buf
, int data
);
266 /* Network device stuff */
267 static int cosa_net_attach(struct net_device
*dev
, unsigned short encoding
,
268 unsigned short parity
);
269 static int cosa_net_open(struct net_device
*d
);
270 static int cosa_net_close(struct net_device
*d
);
271 static void cosa_net_timeout(struct net_device
*d
, unsigned int txqueue
);
272 static netdev_tx_t
cosa_net_tx(struct sk_buff
*skb
, struct net_device
*d
);
273 static char *cosa_net_setup_rx(struct channel_data
*channel
, int size
);
274 static int cosa_net_rx_done(struct channel_data
*channel
);
275 static int cosa_net_tx_done(struct channel_data
*channel
, int size
);
276 static int cosa_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
);
278 /* Character device */
279 static char *chrdev_setup_rx(struct channel_data
*channel
, int size
);
280 static int chrdev_rx_done(struct channel_data
*channel
);
281 static int chrdev_tx_done(struct channel_data
*channel
, int size
);
282 static ssize_t
cosa_read(struct file
*file
,
283 char __user
*buf
, size_t count
, loff_t
*ppos
);
284 static ssize_t
cosa_write(struct file
*file
,
285 const char __user
*buf
, size_t count
, loff_t
*ppos
);
286 static unsigned int cosa_poll(struct file
*file
, poll_table
*poll
);
287 static int cosa_open(struct inode
*inode
, struct file
*file
);
288 static int cosa_release(struct inode
*inode
, struct file
*file
);
289 static long cosa_chardev_ioctl(struct file
*file
, unsigned int cmd
,
291 #ifdef COSA_FASYNC_WORKING
292 static int cosa_fasync(struct inode
*inode
, struct file
*file
, int on
);
295 static const struct file_operations cosa_fops
= {
296 .owner
= THIS_MODULE
,
301 .unlocked_ioctl
= cosa_chardev_ioctl
,
303 .release
= cosa_release
,
304 #ifdef COSA_FASYNC_WORKING
305 .fasync
= cosa_fasync
,
310 static int cosa_start(struct cosa_data
*cosa
, int address
);
311 static int cosa_reset(struct cosa_data
*cosa
);
312 static int cosa_download(struct cosa_data
*cosa
, void __user
*a
);
313 static int cosa_readmem(struct cosa_data
*cosa
, void __user
*a
);
315 /* COSA/SRP ROM monitor */
316 static int download(struct cosa_data
*cosa
, const char __user
*data
, int addr
, int len
);
317 static int startmicrocode(struct cosa_data
*cosa
, int address
);
318 static int readmem(struct cosa_data
*cosa
, char __user
*data
, int addr
, int len
);
319 static int cosa_reset_and_read_id(struct cosa_data
*cosa
, char *id
);
321 /* Auxiliary functions */
322 static int get_wait_data(struct cosa_data
*cosa
);
323 static int put_wait_data(struct cosa_data
*cosa
, int data
);
324 static int puthexnumber(struct cosa_data
*cosa
, int number
);
325 static void put_driver_status(struct cosa_data
*cosa
);
326 static void put_driver_status_nolock(struct cosa_data
*cosa
);
328 /* Interrupt handling */
329 static irqreturn_t
cosa_interrupt(int irq
, void *cosa
);
331 /* I/O ops debugging */
333 static void debug_data_in(struct cosa_data
*cosa
, int data
);
334 static void debug_data_out(struct cosa_data
*cosa
, int data
);
335 static void debug_data_cmd(struct cosa_data
*cosa
, int data
);
336 static void debug_status_in(struct cosa_data
*cosa
, int status
);
337 static void debug_status_out(struct cosa_data
*cosa
, int status
);
340 static inline struct channel_data
* dev_to_chan(struct net_device
*dev
)
342 return (struct channel_data
*)dev_to_hdlc(dev
)->priv
;
345 /* ---------- Initialization stuff ---------- */
347 static int __init
cosa_init(void)
351 if (cosa_major
> 0) {
352 if (register_chrdev(cosa_major
, "cosa", &cosa_fops
)) {
353 pr_warn("unable to get major %d\n", cosa_major
);
358 if (!(cosa_major
=register_chrdev(0, "cosa", &cosa_fops
))) {
359 pr_warn("unable to register chardev\n");
364 for (i
=0; i
<MAX_CARDS
; i
++)
365 cosa_cards
[i
].num
= -1;
366 for (i
=0; io
[i
] != 0 && i
< MAX_CARDS
; i
++)
367 cosa_probe(io
[i
], irq
[i
], dma
[i
]);
369 pr_warn("no devices found\n");
370 unregister_chrdev(cosa_major
, "cosa");
374 cosa_class
= class_create(THIS_MODULE
, "cosa");
375 if (IS_ERR(cosa_class
)) {
376 err
= PTR_ERR(cosa_class
);
379 for (i
= 0; i
< nr_cards
; i
++)
380 device_create(cosa_class
, NULL
, MKDEV(cosa_major
, i
), NULL
,
386 unregister_chrdev(cosa_major
, "cosa");
390 module_init(cosa_init
);
392 static void __exit
cosa_exit(void)
394 struct cosa_data
*cosa
;
397 for (i
= 0; i
< nr_cards
; i
++)
398 device_destroy(cosa_class
, MKDEV(cosa_major
, i
));
399 class_destroy(cosa_class
);
401 for (cosa
= cosa_cards
; nr_cards
--; cosa
++) {
402 /* Clean up the per-channel data */
403 for (i
= 0; i
< cosa
->nchannels
; i
++) {
404 /* Chardev driver has no alloc'd per-channel data */
405 unregister_hdlc_device(cosa
->chan
[i
].netdev
);
406 free_netdev(cosa
->chan
[i
].netdev
);
408 /* Clean up the per-card data */
410 kfree(cosa
->bouncebuf
);
411 free_irq(cosa
->irq
, cosa
);
413 release_region(cosa
->datareg
, is_8bit(cosa
) ? 2 : 4);
415 unregister_chrdev(cosa_major
, "cosa");
417 module_exit(cosa_exit
);
419 static const struct net_device_ops cosa_ops
= {
420 .ndo_open
= cosa_net_open
,
421 .ndo_stop
= cosa_net_close
,
422 .ndo_start_xmit
= hdlc_start_xmit
,
423 .ndo_do_ioctl
= cosa_net_ioctl
,
424 .ndo_tx_timeout
= cosa_net_timeout
,
427 static int cosa_probe(int base
, int irq
, int dma
)
429 struct cosa_data
*cosa
= cosa_cards
+nr_cards
;
432 memset(cosa
, 0, sizeof(struct cosa_data
));
434 /* Checking validity of parameters: */
435 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
436 if ((irq
>= 0 && irq
< 2) || irq
> 15 || (irq
< 10 && irq
> 7)) {
437 pr_info("invalid IRQ %d\n", irq
);
440 /* I/O address should be between 0x100 and 0x3ff and should be
442 if (base
< 0x100 || base
> 0x3ff || base
& 0x7) {
443 pr_info("invalid I/O address 0x%x\n", base
);
446 /* DMA should be 0,1 or 3-7 */
447 if (dma
< 0 || dma
== 4 || dma
> 7) {
448 pr_info("invalid DMA %d\n", dma
);
451 /* and finally, on 16-bit COSA DMA should be 4-7 and
452 * I/O base should not be multiple of 0x10 */
453 if (((base
& 0x8) && dma
< 4) || (!(base
& 0x8) && dma
> 3)) {
454 pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
460 cosa
->datareg
= base
;
461 cosa
->statusreg
= is_8bit(cosa
)?base
+1:base
+2;
462 spin_lock_init(&cosa
->lock
);
464 if (!request_region(base
, is_8bit(cosa
)?2:4,"cosa"))
467 if (cosa_reset_and_read_id(cosa
, cosa
->id_string
) < 0) {
468 printk(KERN_DEBUG
"probe at 0x%x failed.\n", base
);
473 /* Test the validity of identification string */
474 if (!strncmp(cosa
->id_string
, "SRP", 3))
476 else if (!strncmp(cosa
->id_string
, "COSA", 4))
477 cosa
->type
= is_8bit(cosa
)? "cosa8": "cosa16";
479 /* Print a warning only if we are not autoprobing */
480 #ifndef COSA_ISA_AUTOPROBE
481 pr_info("valid signature not found at 0x%x\n", base
);
486 /* Update the name of the region now we know the type of card */
487 release_region(base
, is_8bit(cosa
)?2:4);
488 if (!request_region(base
, is_8bit(cosa
)?2:4, cosa
->type
)) {
489 printk(KERN_DEBUG
"changing name at 0x%x failed.\n", base
);
493 /* Now do IRQ autoprobe */
496 /* pr_info("IRQ autoprobe\n"); */
497 irqs
= probe_irq_on();
499 * Enable interrupt on tx buffer empty (it sure is)
501 * FIXME: When this code is not used as module, we should
502 * probably call udelay() instead of the interruptible sleep.
504 set_current_state(TASK_INTERRUPTIBLE
);
505 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
506 schedule_timeout(msecs_to_jiffies(300));
507 irq
= probe_irq_off(irqs
);
508 /* Disable all IRQs from the card */
509 cosa_putstatus(cosa
, 0);
510 /* Empty the received data register */
514 pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
520 pr_info("no interrupt obtained (board at 0x%x)\n",
527 cosa
->num
= nr_cards
;
529 cosa
->nchannels
= 2; /* FIXME: how to determine this? */
531 if (request_irq(cosa
->irq
, cosa_interrupt
, 0, cosa
->type
, cosa
)) {
535 if (request_dma(cosa
->dma
, cosa
->type
)) {
540 cosa
->bouncebuf
= kmalloc(COSA_MTU
, GFP_KERNEL
|GFP_DMA
);
541 if (!cosa
->bouncebuf
) {
545 sprintf(cosa
->name
, "cosa%d", cosa
->num
);
547 /* Initialize the per-channel data */
548 cosa
->chan
= kcalloc(cosa
->nchannels
, sizeof(struct channel_data
), GFP_KERNEL
);
554 for (i
= 0; i
< cosa
->nchannels
; i
++) {
555 struct channel_data
*chan
= &cosa
->chan
[i
];
559 sprintf(chan
->name
, "cosa%dc%d", chan
->cosa
->num
, i
);
561 /* Initialize the chardev data structures */
562 mutex_init(&chan
->rlock
);
563 sema_init(&chan
->wsem
, 1);
565 /* Register the network interface */
566 if (!(chan
->netdev
= alloc_hdlcdev(chan
))) {
567 pr_warn("%s: alloc_hdlcdev failed\n", chan
->name
);
571 dev_to_hdlc(chan
->netdev
)->attach
= cosa_net_attach
;
572 dev_to_hdlc(chan
->netdev
)->xmit
= cosa_net_tx
;
573 chan
->netdev
->netdev_ops
= &cosa_ops
;
574 chan
->netdev
->watchdog_timeo
= TX_TIMEOUT
;
575 chan
->netdev
->base_addr
= chan
->cosa
->datareg
;
576 chan
->netdev
->irq
= chan
->cosa
->irq
;
577 chan
->netdev
->dma
= chan
->cosa
->dma
;
578 err
= register_hdlc_device(chan
->netdev
);
580 netdev_warn(chan
->netdev
,
581 "register_hdlc_device() failed\n");
582 free_netdev(chan
->netdev
);
587 pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
588 cosa
->num
, cosa
->id_string
, cosa
->type
,
589 cosa
->datareg
, cosa
->irq
, cosa
->dma
, cosa
->nchannels
);
595 unregister_hdlc_device(cosa
->chan
[i
].netdev
);
596 free_netdev(cosa
->chan
[i
].netdev
);
600 kfree(cosa
->bouncebuf
);
604 free_irq(cosa
->irq
, cosa
);
606 release_region(cosa
->datareg
,is_8bit(cosa
)?2:4);
607 pr_notice("cosa%d: allocating resources failed\n", cosa
->num
);
612 /*---------- network device ---------- */
614 static int cosa_net_attach(struct net_device
*dev
, unsigned short encoding
,
615 unsigned short parity
)
617 if (encoding
== ENCODING_NRZ
&& parity
== PARITY_CRC16_PR1_CCITT
)
622 static int cosa_net_open(struct net_device
*dev
)
624 struct channel_data
*chan
= dev_to_chan(dev
);
628 if (!(chan
->cosa
->firmware_status
& COSA_FW_START
)) {
629 pr_notice("%s: start the firmware first (status %d)\n",
630 chan
->cosa
->name
, chan
->cosa
->firmware_status
);
633 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
634 if (chan
->usage
!= 0) {
635 pr_warn("%s: cosa_net_open called with usage count %d\n",
636 chan
->name
, chan
->usage
);
637 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
640 chan
->setup_rx
= cosa_net_setup_rx
;
641 chan
->tx_done
= cosa_net_tx_done
;
642 chan
->rx_done
= cosa_net_rx_done
;
645 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
647 err
= hdlc_open(dev
);
649 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
652 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
656 netif_start_queue(dev
);
657 cosa_enable_rx(chan
);
661 static netdev_tx_t
cosa_net_tx(struct sk_buff
*skb
,
662 struct net_device
*dev
)
664 struct channel_data
*chan
= dev_to_chan(dev
);
666 netif_stop_queue(dev
);
669 cosa_start_tx(chan
, skb
->data
, skb
->len
);
673 static void cosa_net_timeout(struct net_device
*dev
, unsigned int txqueue
)
675 struct channel_data
*chan
= dev_to_chan(dev
);
677 if (test_bit(RXBIT
, &chan
->cosa
->rxtx
)) {
678 chan
->netdev
->stats
.rx_errors
++;
679 chan
->netdev
->stats
.rx_missed_errors
++;
681 chan
->netdev
->stats
.tx_errors
++;
682 chan
->netdev
->stats
.tx_aborted_errors
++;
684 cosa_kick(chan
->cosa
);
686 dev_kfree_skb(chan
->tx_skb
);
689 netif_wake_queue(dev
);
692 static int cosa_net_close(struct net_device
*dev
)
694 struct channel_data
*chan
= dev_to_chan(dev
);
697 netif_stop_queue(dev
);
699 cosa_disable_rx(chan
);
700 spin_lock_irqsave(&chan
->cosa
->lock
, flags
);
702 kfree_skb(chan
->rx_skb
);
706 kfree_skb(chan
->tx_skb
);
711 spin_unlock_irqrestore(&chan
->cosa
->lock
, flags
);
715 static char *cosa_net_setup_rx(struct channel_data
*chan
, int size
)
718 * We can safely fall back to non-dma-able memory, because we have
719 * the cosa->bouncebuf pre-allocated.
721 kfree_skb(chan
->rx_skb
);
722 chan
->rx_skb
= dev_alloc_skb(size
);
723 if (chan
->rx_skb
== NULL
) {
724 pr_notice("%s: Memory squeeze, dropping packet\n", chan
->name
);
725 chan
->netdev
->stats
.rx_dropped
++;
728 netif_trans_update(chan
->netdev
);
729 return skb_put(chan
->rx_skb
, size
);
732 static int cosa_net_rx_done(struct channel_data
*chan
)
735 pr_warn("%s: rx_done with empty skb!\n", chan
->name
);
736 chan
->netdev
->stats
.rx_errors
++;
737 chan
->netdev
->stats
.rx_frame_errors
++;
740 chan
->rx_skb
->protocol
= hdlc_type_trans(chan
->rx_skb
, chan
->netdev
);
741 chan
->rx_skb
->dev
= chan
->netdev
;
742 skb_reset_mac_header(chan
->rx_skb
);
743 chan
->netdev
->stats
.rx_packets
++;
744 chan
->netdev
->stats
.rx_bytes
+= chan
->cosa
->rxsize
;
745 netif_rx(chan
->rx_skb
);
751 static int cosa_net_tx_done(struct channel_data
*chan
, int size
)
754 pr_warn("%s: tx_done with empty skb!\n", chan
->name
);
755 chan
->netdev
->stats
.tx_errors
++;
756 chan
->netdev
->stats
.tx_aborted_errors
++;
759 dev_consume_skb_irq(chan
->tx_skb
);
761 chan
->netdev
->stats
.tx_packets
++;
762 chan
->netdev
->stats
.tx_bytes
+= size
;
763 netif_wake_queue(chan
->netdev
);
767 /*---------- Character device ---------- */
769 static ssize_t
cosa_read(struct file
*file
,
770 char __user
*buf
, size_t count
, loff_t
*ppos
)
772 DECLARE_WAITQUEUE(wait
, current
);
774 struct channel_data
*chan
= file
->private_data
;
775 struct cosa_data
*cosa
= chan
->cosa
;
778 if (!(cosa
->firmware_status
& COSA_FW_START
)) {
779 pr_notice("%s: start the firmware first (status %d)\n",
780 cosa
->name
, cosa
->firmware_status
);
783 if (mutex_lock_interruptible(&chan
->rlock
))
786 chan
->rxdata
= kmalloc(COSA_MTU
, GFP_DMA
|GFP_KERNEL
);
787 if (chan
->rxdata
== NULL
) {
788 mutex_unlock(&chan
->rlock
);
793 cosa_enable_rx(chan
);
794 spin_lock_irqsave(&cosa
->lock
, flags
);
795 add_wait_queue(&chan
->rxwaitq
, &wait
);
796 while (!chan
->rx_status
) {
797 set_current_state(TASK_INTERRUPTIBLE
);
798 spin_unlock_irqrestore(&cosa
->lock
, flags
);
800 spin_lock_irqsave(&cosa
->lock
, flags
);
801 if (signal_pending(current
) && chan
->rx_status
== 0) {
803 remove_wait_queue(&chan
->rxwaitq
, &wait
);
804 __set_current_state(TASK_RUNNING
);
805 spin_unlock_irqrestore(&cosa
->lock
, flags
);
806 mutex_unlock(&chan
->rlock
);
810 remove_wait_queue(&chan
->rxwaitq
, &wait
);
811 __set_current_state(TASK_RUNNING
);
813 count
= chan
->rxsize
;
814 spin_unlock_irqrestore(&cosa
->lock
, flags
);
815 mutex_unlock(&chan
->rlock
);
817 if (copy_to_user(buf
, kbuf
, count
)) {
825 static char *chrdev_setup_rx(struct channel_data
*chan
, int size
)
827 /* Expect size <= COSA_MTU */
832 static int chrdev_rx_done(struct channel_data
*chan
)
834 if (chan
->rx_status
) { /* Reader has died */
839 wake_up_interruptible(&chan
->rxwaitq
);
844 static ssize_t
cosa_write(struct file
*file
,
845 const char __user
*buf
, size_t count
, loff_t
*ppos
)
847 DECLARE_WAITQUEUE(wait
, current
);
848 struct channel_data
*chan
= file
->private_data
;
849 struct cosa_data
*cosa
= chan
->cosa
;
853 if (!(cosa
->firmware_status
& COSA_FW_START
)) {
854 pr_notice("%s: start the firmware first (status %d)\n",
855 cosa
->name
, cosa
->firmware_status
);
858 if (down_interruptible(&chan
->wsem
))
861 if (count
> COSA_MTU
)
864 /* Allocate the buffer */
865 kbuf
= kmalloc(count
, GFP_KERNEL
|GFP_DMA
);
870 if (copy_from_user(kbuf
, buf
, count
)) {
876 cosa_start_tx(chan
, kbuf
, count
);
878 spin_lock_irqsave(&cosa
->lock
, flags
);
879 add_wait_queue(&chan
->txwaitq
, &wait
);
880 while (!chan
->tx_status
) {
881 set_current_state(TASK_INTERRUPTIBLE
);
882 spin_unlock_irqrestore(&cosa
->lock
, flags
);
884 spin_lock_irqsave(&cosa
->lock
, flags
);
885 if (signal_pending(current
) && chan
->tx_status
== 0) {
887 remove_wait_queue(&chan
->txwaitq
, &wait
);
888 __set_current_state(TASK_RUNNING
);
890 spin_unlock_irqrestore(&cosa
->lock
, flags
);
895 remove_wait_queue(&chan
->txwaitq
, &wait
);
896 __set_current_state(TASK_RUNNING
);
898 spin_unlock_irqrestore(&cosa
->lock
, flags
);
903 static int chrdev_tx_done(struct channel_data
*chan
, int size
)
905 if (chan
->tx_status
) { /* Writer was interrupted */
910 wake_up_interruptible(&chan
->txwaitq
);
914 static __poll_t
cosa_poll(struct file
*file
, poll_table
*poll
)
916 pr_info("cosa_poll is here\n");
920 static int cosa_open(struct inode
*inode
, struct file
*file
)
922 struct cosa_data
*cosa
;
923 struct channel_data
*chan
;
928 mutex_lock(&cosa_chardev_mutex
);
929 if ((n
=iminor(file_inode(file
))>>CARD_MINOR_BITS
)
936 if ((n
=iminor(file_inode(file
))
937 & ((1<<CARD_MINOR_BITS
)-1)) >= cosa
->nchannels
) {
941 chan
= cosa
->chan
+ n
;
943 file
->private_data
= chan
;
945 spin_lock_irqsave(&cosa
->lock
, flags
);
947 if (chan
->usage
< 0) { /* in netdev mode */
948 spin_unlock_irqrestore(&cosa
->lock
, flags
);
955 chan
->tx_done
= chrdev_tx_done
;
956 chan
->setup_rx
= chrdev_setup_rx
;
957 chan
->rx_done
= chrdev_rx_done
;
958 spin_unlock_irqrestore(&cosa
->lock
, flags
);
960 mutex_unlock(&cosa_chardev_mutex
);
964 static int cosa_release(struct inode
*inode
, struct file
*file
)
966 struct channel_data
*channel
= file
->private_data
;
967 struct cosa_data
*cosa
;
970 cosa
= channel
->cosa
;
971 spin_lock_irqsave(&cosa
->lock
, flags
);
974 spin_unlock_irqrestore(&cosa
->lock
, flags
);
978 #ifdef COSA_FASYNC_WORKING
979 static struct fasync_struct
*fasync
[256] = { NULL
, };
982 static int cosa_fasync(struct inode
*inode
, struct file
*file
, int on
)
984 int port
= iminor(inode
);
986 return fasync_helper(inode
, file
, on
, &fasync
[port
]);
991 /* ---------- Ioctls ---------- */
994 * Ioctl subroutines can safely be made inline, because they are called
995 * only from cosa_ioctl().
997 static inline int cosa_reset(struct cosa_data
*cosa
)
999 char idstring
[COSA_MAX_ID_STRING
];
1000 if (cosa
->usage
> 1)
1001 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1002 cosa
->num
, cosa
->usage
);
1003 cosa
->firmware_status
&= ~(COSA_FW_RESET
|COSA_FW_START
);
1004 if (cosa_reset_and_read_id(cosa
, idstring
) < 0) {
1005 pr_notice("cosa%d: reset failed\n", cosa
->num
);
1008 pr_info("cosa%d: resetting device: %s\n", cosa
->num
, idstring
);
1009 cosa
->firmware_status
|= COSA_FW_RESET
;
1013 /* High-level function to download data into COSA memory. Calls download() */
1014 static inline int cosa_download(struct cosa_data
*cosa
, void __user
*arg
)
1016 struct cosa_download d
;
1019 if (cosa
->usage
> 1)
1020 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1021 cosa
->name
, cosa
->usage
);
1022 if (!(cosa
->firmware_status
& COSA_FW_RESET
)) {
1023 pr_notice("%s: reset the card first (status %d)\n",
1024 cosa
->name
, cosa
->firmware_status
);
1028 if (copy_from_user(&d
, arg
, sizeof(d
)))
1031 if (d
.addr
< 0 || d
.addr
> COSA_MAX_FIRMWARE_SIZE
)
1033 if (d
.len
< 0 || d
.len
> COSA_MAX_FIRMWARE_SIZE
)
1037 /* If something fails, force the user to reset the card */
1038 cosa
->firmware_status
&= ~(COSA_FW_RESET
|COSA_FW_DOWNLOAD
);
1040 i
= download(cosa
, d
.code
, d
.len
, d
.addr
);
1042 pr_notice("cosa%d: microcode download failed: %d\n",
1046 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1047 cosa
->num
, d
.len
, d
.addr
);
1048 cosa
->firmware_status
|= COSA_FW_RESET
|COSA_FW_DOWNLOAD
;
1052 /* High-level function to read COSA memory. Calls readmem() */
1053 static inline int cosa_readmem(struct cosa_data
*cosa
, void __user
*arg
)
1055 struct cosa_download d
;
1058 if (cosa
->usage
> 1)
1059 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1060 cosa
->num
, cosa
->usage
);
1061 if (!(cosa
->firmware_status
& COSA_FW_RESET
)) {
1062 pr_notice("%s: reset the card first (status %d)\n",
1063 cosa
->name
, cosa
->firmware_status
);
1067 if (copy_from_user(&d
, arg
, sizeof(d
)))
1070 /* If something fails, force the user to reset the card */
1071 cosa
->firmware_status
&= ~COSA_FW_RESET
;
1073 i
= readmem(cosa
, d
.code
, d
.len
, d
.addr
);
1075 pr_notice("cosa%d: reading memory failed: %d\n", cosa
->num
, i
);
1078 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1079 cosa
->num
, d
.len
, d
.addr
);
1080 cosa
->firmware_status
|= COSA_FW_RESET
;
1084 /* High-level function to start microcode. Calls startmicrocode(). */
1085 static inline int cosa_start(struct cosa_data
*cosa
, int address
)
1089 if (cosa
->usage
> 1)
1090 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1091 cosa
->num
, cosa
->usage
);
1093 if ((cosa
->firmware_status
& (COSA_FW_RESET
|COSA_FW_DOWNLOAD
))
1094 != (COSA_FW_RESET
|COSA_FW_DOWNLOAD
)) {
1095 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1096 cosa
->name
, cosa
->firmware_status
);
1099 cosa
->firmware_status
&= ~COSA_FW_RESET
;
1100 if ((i
=startmicrocode(cosa
, address
)) < 0) {
1101 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1102 cosa
->num
, address
, i
);
1105 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa
->num
, address
);
1106 cosa
->startaddr
= address
;
1107 cosa
->firmware_status
|= COSA_FW_START
;
1111 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1112 static inline int cosa_getidstr(struct cosa_data
*cosa
, char __user
*string
)
1114 int l
= strlen(cosa
->id_string
)+1;
1115 if (copy_to_user(string
, cosa
->id_string
, l
))
1120 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1121 static inline int cosa_gettype(struct cosa_data
*cosa
, char __user
*string
)
1123 int l
= strlen(cosa
->type
)+1;
1124 if (copy_to_user(string
, cosa
->type
, l
))
1129 static int cosa_ioctl_common(struct cosa_data
*cosa
,
1130 struct channel_data
*channel
, unsigned int cmd
, unsigned long arg
)
1132 void __user
*argp
= (void __user
*)arg
;
1134 case COSAIORSET
: /* Reset the device */
1135 if (!capable(CAP_NET_ADMIN
))
1137 return cosa_reset(cosa
);
1138 case COSAIOSTRT
: /* Start the firmware */
1139 if (!capable(CAP_SYS_RAWIO
))
1141 return cosa_start(cosa
, arg
);
1142 case COSAIODOWNLD
: /* Download the firmware */
1143 if (!capable(CAP_SYS_RAWIO
))
1146 return cosa_download(cosa
, argp
);
1148 if (!capable(CAP_SYS_RAWIO
))
1150 return cosa_readmem(cosa
, argp
);
1152 return cosa_gettype(cosa
, argp
);
1154 return cosa_getidstr(cosa
, argp
);
1158 return cosa
->nchannels
;
1160 if (!capable(CAP_SYS_RAWIO
))
1164 if (arg
!= COSA_BM_OFF
&& arg
!= COSA_BM_ON
)
1166 cosa
->busmaster
= arg
;
1169 return cosa
->busmaster
;
1171 return -ENOIOCTLCMD
;
1174 static int cosa_net_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1177 struct channel_data
*chan
= dev_to_chan(dev
);
1178 rv
= cosa_ioctl_common(chan
->cosa
, chan
, cmd
,
1179 (unsigned long)ifr
->ifr_data
);
1180 if (rv
!= -ENOIOCTLCMD
)
1182 return hdlc_ioctl(dev
, ifr
, cmd
);
1185 static long cosa_chardev_ioctl(struct file
*file
, unsigned int cmd
,
1188 struct channel_data
*channel
= file
->private_data
;
1189 struct cosa_data
*cosa
;
1192 mutex_lock(&cosa_chardev_mutex
);
1193 cosa
= channel
->cosa
;
1194 ret
= cosa_ioctl_common(cosa
, channel
, cmd
, arg
);
1195 mutex_unlock(&cosa_chardev_mutex
);
1200 /*---------- HW layer interface ---------- */
1203 * The higher layer can bind itself to the HW layer by setting the callbacks
1204 * in the channel_data structure and by using these routines.
1206 static void cosa_enable_rx(struct channel_data
*chan
)
1208 struct cosa_data
*cosa
= chan
->cosa
;
1210 if (!test_and_set_bit(chan
->num
, &cosa
->rxbitmap
))
1211 put_driver_status(cosa
);
1214 static void cosa_disable_rx(struct channel_data
*chan
)
1216 struct cosa_data
*cosa
= chan
->cosa
;
1218 if (test_and_clear_bit(chan
->num
, &cosa
->rxbitmap
))
1219 put_driver_status(cosa
);
1223 * FIXME: This routine probably should check for cosa_start_tx() called when
1224 * the previous transmit is still unfinished. In this case the non-zero
1225 * return value should indicate to the caller that the queuing(sp?) up
1226 * the transmit has failed.
1228 static int cosa_start_tx(struct channel_data
*chan
, char *buf
, int len
)
1230 struct cosa_data
*cosa
= chan
->cosa
;
1231 unsigned long flags
;
1235 pr_info("cosa%dc%d: starting tx(0x%x)",
1236 chan
->cosa
->num
, chan
->num
, len
);
1237 for (i
=0; i
<len
; i
++)
1238 pr_cont(" %02x", buf
[i
]&0xff);
1241 spin_lock_irqsave(&cosa
->lock
, flags
);
1245 chan
->txsize
= COSA_MTU
;
1246 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1248 /* Tell the firmware we are ready */
1249 set_bit(chan
->num
, &cosa
->txbitmap
);
1250 put_driver_status(cosa
);
1255 static void put_driver_status(struct cosa_data
*cosa
)
1257 unsigned long flags
;
1260 spin_lock_irqsave(&cosa
->lock
, flags
);
1262 status
= (cosa
->rxbitmap
? DRIVER_RX_READY
: 0)
1263 | (cosa
->txbitmap
? DRIVER_TX_READY
: 0)
1264 | (cosa
->txbitmap
? ~(cosa
->txbitmap
<<DRIVER_TXMAP_SHIFT
)
1265 &DRIVER_TXMAP_MASK
: 0);
1267 if (cosa
->rxbitmap
|cosa
->txbitmap
) {
1268 if (!cosa
->enabled
) {
1269 cosa_putstatus(cosa
, SR_RX_INT_ENA
);
1271 debug_status_out(cosa
, SR_RX_INT_ENA
);
1275 } else if (cosa
->enabled
) {
1277 cosa_putstatus(cosa
, 0);
1279 debug_status_out(cosa
, 0);
1282 cosa_putdata8(cosa
, status
);
1284 debug_data_cmd(cosa
, status
);
1287 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1290 static void put_driver_status_nolock(struct cosa_data
*cosa
)
1294 status
= (cosa
->rxbitmap
? DRIVER_RX_READY
: 0)
1295 | (cosa
->txbitmap
? DRIVER_TX_READY
: 0)
1296 | (cosa
->txbitmap
? ~(cosa
->txbitmap
<<DRIVER_TXMAP_SHIFT
)
1297 &DRIVER_TXMAP_MASK
: 0);
1299 if (cosa
->rxbitmap
|cosa
->txbitmap
) {
1300 cosa_putstatus(cosa
, SR_RX_INT_ENA
);
1302 debug_status_out(cosa
, SR_RX_INT_ENA
);
1306 cosa_putstatus(cosa
, 0);
1308 debug_status_out(cosa
, 0);
1312 cosa_putdata8(cosa
, status
);
1314 debug_data_cmd(cosa
, status
);
1319 * The "kickme" function: When the DMA times out, this is called to
1320 * clean up the driver status.
1321 * FIXME: Preliminary support, the interface is probably wrong.
1323 static void cosa_kick(struct cosa_data
*cosa
)
1325 unsigned long flags
, flags1
;
1326 char *s
= "(probably) IRQ";
1328 if (test_bit(RXBIT
, &cosa
->rxtx
))
1330 if (test_bit(TXBIT
, &cosa
->rxtx
))
1333 pr_info("%s: %s timeout - restarting\n", cosa
->name
, s
);
1334 spin_lock_irqsave(&cosa
->lock
, flags
);
1337 flags1
= claim_dma_lock();
1338 disable_dma(cosa
->dma
);
1339 clear_dma_ff(cosa
->dma
);
1340 release_dma_lock(flags1
);
1342 /* FIXME: Anything else? */
1344 cosa_putstatus(cosa
, 0);
1346 (void) cosa_getdata8(cosa
);
1348 cosa_putdata8(cosa
, 0);
1350 put_driver_status_nolock(cosa
);
1351 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1355 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1356 * physical memory and doesn't span the 64k boundary. For now it seems
1357 * SKB's never do this, but we'll check this anyway.
1359 static int cosa_dma_able(struct channel_data
*chan
, char *buf
, int len
)
1362 unsigned long b
= (unsigned long)buf
;
1363 if (b
+len
>= MAX_DMA_ADDRESS
)
1365 if ((b
^ (b
+len
)) & 0x10000) {
1367 pr_info("%s: packet spanning a 64k boundary\n",
1375 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1378 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1379 * drivers need to say 4-digit hex number meaning start address of the microcode
1380 * separated by a single space. Monitor replies by saying " =". Now driver
1381 * has to write 4-digit hex number meaning the last byte address ended
1382 * by a single space. Monitor has to reply with a space. Now the download
1383 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1385 static int download(struct cosa_data
*cosa
, const char __user
*microcode
, int length
, int address
)
1389 if (put_wait_data(cosa
, 'w') == -1) return -1;
1390 if ((i
=get_wait_data(cosa
)) != 'w') { printk("dnld: 0x%04x\n",i
); return -2;}
1391 if (get_wait_data(cosa
) != '=') return -3;
1393 if (puthexnumber(cosa
, address
) < 0) return -4;
1394 if (put_wait_data(cosa
, ' ') == -1) return -10;
1395 if (get_wait_data(cosa
) != ' ') return -11;
1396 if (get_wait_data(cosa
) != '=') return -12;
1398 if (puthexnumber(cosa
, address
+length
-1) < 0) return -13;
1399 if (put_wait_data(cosa
, ' ') == -1) return -18;
1400 if (get_wait_data(cosa
) != ' ') return -19;
1404 #ifndef SRP_DOWNLOAD_AT_BOOT
1405 if (get_user(c
, microcode
))
1406 return -23; /* ??? */
1410 if (put_wait_data(cosa
, c
) == -1)
1415 if (get_wait_data(cosa
) != '\r') return -21;
1416 if (get_wait_data(cosa
) != '\n') return -22;
1417 if (get_wait_data(cosa
) != '.') return -23;
1419 printk(KERN_DEBUG
"cosa%d: download completed.\n", cosa
->num
);
1426 * Starting microcode is done via the "g" command of the SRP monitor.
1427 * The chat should be the following: "g" "g=" "<addr><CR>"
1428 * "<CR><CR><LF><CR><LF>".
1430 static int startmicrocode(struct cosa_data
*cosa
, int address
)
1432 if (put_wait_data(cosa
, 'g') == -1) return -1;
1433 if (get_wait_data(cosa
) != 'g') return -2;
1434 if (get_wait_data(cosa
) != '=') return -3;
1436 if (puthexnumber(cosa
, address
) < 0) return -4;
1437 if (put_wait_data(cosa
, '\r') == -1) return -5;
1439 if (get_wait_data(cosa
) != '\r') return -6;
1440 if (get_wait_data(cosa
) != '\r') return -7;
1441 if (get_wait_data(cosa
) != '\n') return -8;
1442 if (get_wait_data(cosa
) != '\r') return -9;
1443 if (get_wait_data(cosa
) != '\n') return -10;
1445 printk(KERN_DEBUG
"cosa%d: microcode started\n", cosa
->num
);
1451 * Reading memory is done via the "r" command of the SRP monitor.
1452 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1453 * Then driver can read the data and the conversation is finished
1454 * by SRP monitor sending "<CR><LF>." (dot at the end).
1456 * This routine is not needed during the normal operation and serves
1457 * for debugging purposes only.
1459 static int readmem(struct cosa_data
*cosa
, char __user
*microcode
, int length
, int address
)
1461 if (put_wait_data(cosa
, 'r') == -1) return -1;
1462 if ((get_wait_data(cosa
)) != 'r') return -2;
1463 if ((get_wait_data(cosa
)) != '=') return -3;
1465 if (puthexnumber(cosa
, address
) < 0) return -4;
1466 if (put_wait_data(cosa
, ' ') == -1) return -5;
1467 if (get_wait_data(cosa
) != ' ') return -6;
1468 if (get_wait_data(cosa
) != '=') return -7;
1470 if (puthexnumber(cosa
, address
+length
-1) < 0) return -8;
1471 if (put_wait_data(cosa
, ' ') == -1) return -9;
1472 if (get_wait_data(cosa
) != ' ') return -10;
1477 if ((i
=get_wait_data(cosa
)) == -1) {
1478 pr_info("0x%04x bytes remaining\n", length
);
1483 if (put_user(c
, microcode
))
1484 return -23; /* ??? */
1491 if (get_wait_data(cosa
) != '\r') return -21;
1492 if (get_wait_data(cosa
) != '\n') return -22;
1493 if (get_wait_data(cosa
) != '.') return -23;
1495 printk(KERN_DEBUG
"cosa%d: readmem completed.\n", cosa
->num
);
1501 * This function resets the device and reads the initial prompt
1502 * of the device's ROM monitor.
1504 static int cosa_reset_and_read_id(struct cosa_data
*cosa
, char *idstring
)
1506 int i
=0, id
=0, prev
=0, curr
=0;
1508 /* Reset the card ... */
1509 cosa_putstatus(cosa
, 0);
1510 cosa_getdata8(cosa
);
1511 cosa_putstatus(cosa
, SR_RST
);
1513 /* Disable all IRQs from the card */
1514 cosa_putstatus(cosa
, 0);
1517 * Try to read the ID string. The card then prints out the
1518 * identification string ended by the "\n\x2e".
1520 * The following loop is indexed through i (instead of id)
1521 * to avoid looping forever when for any reason
1522 * the port returns '\r', '\n' or '\x2e' permanently.
1524 for (i
=0; i
<COSA_MAX_ID_STRING
-1; i
++, prev
=curr
) {
1525 if ((curr
= get_wait_data(cosa
)) == -1) {
1529 if (curr
!= '\r' && curr
!= '\n' && curr
!= 0x2e)
1530 idstring
[id
++] = curr
;
1531 if (curr
== 0x2e && prev
== '\n')
1534 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1535 idstring
[id
] = '\0';
1540 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1543 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1544 * bit to be set in a loop. It should be used in the exceptional cases
1545 * only (for example when resetting the card or downloading the firmware.
1547 static int get_wait_data(struct cosa_data
*cosa
)
1552 /* read data and return them */
1553 if (cosa_getstatus(cosa
) & SR_RX_RDY
) {
1555 r
= cosa_getdata8(cosa
);
1557 pr_info("get_wait_data returning after %d retries\n",
1562 /* sleep if not ready to read */
1563 schedule_timeout_interruptible(1);
1565 pr_info("timeout in get_wait_data (status 0x%x)\n",
1566 cosa_getstatus(cosa
));
1571 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1572 * bit to be set in a loop. It should be used in the exceptional cases
1573 * only (for example when resetting the card or downloading the firmware).
1575 static int put_wait_data(struct cosa_data
*cosa
, int data
)
1579 /* read data and return them */
1580 if (cosa_getstatus(cosa
) & SR_TX_RDY
) {
1581 cosa_putdata8(cosa
, data
);
1583 pr_info("Putdata: %d retries\n", 999-retries
);
1588 /* sleep if not ready to read */
1589 schedule_timeout_interruptible(1);
1592 pr_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 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1616 if (get_wait_data(cosa
) != temp
[i
]) {
1617 pr_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 * It seems there is a bug in COSA firmware (need to trace it further):
1652 * When the driver status says that the kernel has no more data for transmit
1653 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1654 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1655 * the TX interrupt but does not mark the channel as ready-to-transmit.
1656 * The fix seems to be to push the packet to COSA despite its request.
1657 * We first try to obey the card's opinion, and then fall back to forced TX.
1659 static inline void tx_interrupt(struct cosa_data
*cosa
, int status
)
1661 unsigned long flags
, flags1
;
1663 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa
->num
, status
);
1665 spin_lock_irqsave(&cosa
->lock
, flags
);
1666 set_bit(TXBIT
, &cosa
->rxtx
);
1667 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1668 /* flow control, see the comment above */
1670 if (!cosa
->txbitmap
) {
1671 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1673 put_driver_status_nolock(cosa
);
1674 clear_bit(TXBIT
, &cosa
->rxtx
);
1675 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1681 if (cosa
->txchan
>= cosa
->nchannels
)
1683 if (!(cosa
->txbitmap
& (1<<cosa
->txchan
)))
1685 if (~status
& (1 << (cosa
->txchan
+DRIVER_TXMAP_SHIFT
)))
1687 /* in second pass, accept first ready-to-TX channel */
1688 if (i
> cosa
->nchannels
) {
1689 /* Can be safely ignored */
1691 printk(KERN_DEBUG
"%s: Forcing TX "
1692 "to not-ready channel %d\n",
1693 cosa
->name
, cosa
->txchan
);
1699 cosa
->txsize
= cosa
->chan
[cosa
->txchan
].txsize
;
1700 if (cosa_dma_able(cosa
->chan
+cosa
->txchan
,
1701 cosa
->chan
[cosa
->txchan
].txbuf
, cosa
->txsize
)) {
1702 cosa
->txbuf
= cosa
->chan
[cosa
->txchan
].txbuf
;
1704 memcpy(cosa
->bouncebuf
, cosa
->chan
[cosa
->txchan
].txbuf
,
1706 cosa
->txbuf
= cosa
->bouncebuf
;
1710 if (is_8bit(cosa
)) {
1711 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1712 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
1713 cosa_putdata8(cosa
, ((cosa
->txchan
<< 5) & 0xe0)|
1714 ((cosa
->txsize
>> 8) & 0x1f));
1716 debug_status_out(cosa
, SR_TX_INT_ENA
);
1717 debug_data_out(cosa
, ((cosa
->txchan
<< 5) & 0xe0)|
1718 ((cosa
->txsize
>> 8) & 0x1f));
1719 debug_data_in(cosa
, cosa_getdata8(cosa
));
1721 cosa_getdata8(cosa
);
1723 set_bit(IRQBIT
, &cosa
->rxtx
);
1724 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1727 clear_bit(IRQBIT
, &cosa
->rxtx
);
1728 cosa_putstatus(cosa
, 0);
1729 cosa_putdata8(cosa
, cosa
->txsize
&0xff);
1731 debug_status_out(cosa
, 0);
1732 debug_data_out(cosa
, cosa
->txsize
&0xff);
1736 cosa_putstatus(cosa
, SR_TX_INT_ENA
);
1737 cosa_putdata16(cosa
, ((cosa
->txchan
<<13) & 0xe000)
1738 | (cosa
->txsize
& 0x1fff));
1740 debug_status_out(cosa
, SR_TX_INT_ENA
);
1741 debug_data_out(cosa
, ((cosa
->txchan
<<13) & 0xe000)
1742 | (cosa
->txsize
& 0x1fff));
1743 debug_data_in(cosa
, cosa_getdata8(cosa
));
1744 debug_status_out(cosa
, 0);
1746 cosa_getdata8(cosa
);
1748 cosa_putstatus(cosa
, 0);
1751 if (cosa
->busmaster
) {
1752 unsigned long addr
= virt_to_bus(cosa
->txbuf
);
1754 pr_info("busmaster IRQ\n");
1755 while (!(cosa_getstatus(cosa
)&SR_TX_RDY
)) {
1758 if (count
> 1000) break;
1760 pr_info("status %x\n", cosa_getstatus(cosa
));
1761 pr_info("ready after %d loops\n", count
);
1762 cosa_putdata16(cosa
, (addr
>> 16)&0xffff);
1765 while (!(cosa_getstatus(cosa
)&SR_TX_RDY
)) {
1767 if (count
> 1000) break;
1770 pr_info("ready after %d loops\n", count
);
1771 cosa_putdata16(cosa
, addr
&0xffff);
1772 flags1
= claim_dma_lock();
1773 set_dma_mode(cosa
->dma
, DMA_MODE_CASCADE
);
1774 enable_dma(cosa
->dma
);
1775 release_dma_lock(flags1
);
1778 flags1
= claim_dma_lock();
1779 disable_dma(cosa
->dma
);
1780 clear_dma_ff(cosa
->dma
);
1781 set_dma_mode(cosa
->dma
, DMA_MODE_WRITE
);
1782 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->txbuf
));
1783 set_dma_count(cosa
->dma
, cosa
->txsize
);
1784 enable_dma(cosa
->dma
);
1785 release_dma_lock(flags1
);
1787 cosa_putstatus(cosa
, SR_TX_DMA_ENA
|SR_USR_INT_ENA
);
1789 debug_status_out(cosa
, SR_TX_DMA_ENA
|SR_USR_INT_ENA
);
1791 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1794 static inline void rx_interrupt(struct cosa_data
*cosa
, int status
)
1796 unsigned long flags
;
1798 pr_info("cosa%d: SR_UP_REQUEST\n", cosa
->num
);
1801 spin_lock_irqsave(&cosa
->lock
, flags
);
1802 set_bit(RXBIT
, &cosa
->rxtx
);
1804 if (is_8bit(cosa
)) {
1805 if (!test_bit(IRQBIT
, &cosa
->rxtx
)) {
1806 set_bit(IRQBIT
, &cosa
->rxtx
);
1807 put_driver_status_nolock(cosa
);
1808 cosa
->rxsize
= cosa_getdata8(cosa
) <<8;
1810 debug_data_in(cosa
, cosa
->rxsize
>> 8);
1812 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1815 clear_bit(IRQBIT
, &cosa
->rxtx
);
1816 cosa
->rxsize
|= cosa_getdata8(cosa
) & 0xff;
1818 debug_data_in(cosa
, cosa
->rxsize
& 0xff);
1821 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1822 cosa
->num
, cosa
->rxsize
);
1826 cosa
->rxsize
= cosa_getdata16(cosa
);
1828 debug_data_in(cosa
, cosa
->rxsize
);
1831 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1832 cosa
->num
, cosa
->rxsize
);
1835 if (((cosa
->rxsize
& 0xe000) >> 13) >= cosa
->nchannels
) {
1836 pr_warn("%s: rx for unknown channel (0x%04x)\n",
1837 cosa
->name
, cosa
->rxsize
);
1838 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1841 cosa
->rxchan
= cosa
->chan
+ ((cosa
->rxsize
& 0xe000) >> 13);
1842 cosa
->rxsize
&= 0x1fff;
1843 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1846 if (cosa
->rxchan
->setup_rx
)
1847 cosa
->rxbuf
= cosa
->rxchan
->setup_rx(cosa
->rxchan
, cosa
->rxsize
);
1850 reject
: /* Reject the packet */
1851 pr_info("cosa%d: rejecting packet on channel %d\n",
1852 cosa
->num
, cosa
->rxchan
->num
);
1853 cosa
->rxbuf
= cosa
->bouncebuf
;
1857 flags
= claim_dma_lock();
1858 disable_dma(cosa
->dma
);
1859 clear_dma_ff(cosa
->dma
);
1860 set_dma_mode(cosa
->dma
, DMA_MODE_READ
);
1861 if (cosa_dma_able(cosa
->rxchan
, cosa
->rxbuf
, cosa
->rxsize
& 0x1fff)) {
1862 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->rxbuf
));
1864 set_dma_addr(cosa
->dma
, virt_to_bus(cosa
->bouncebuf
));
1866 set_dma_count(cosa
->dma
, (cosa
->rxsize
&0x1fff));
1867 enable_dma(cosa
->dma
);
1868 release_dma_lock(flags
);
1869 spin_lock_irqsave(&cosa
->lock
, flags
);
1870 cosa_putstatus(cosa
, SR_RX_DMA_ENA
|SR_USR_INT_ENA
);
1871 if (!is_8bit(cosa
) && (status
& SR_TX_RDY
))
1872 cosa_putdata8(cosa
, DRIVER_RX_READY
);
1874 debug_status_out(cosa
, SR_RX_DMA_ENA
|SR_USR_INT_ENA
);
1875 if (!is_8bit(cosa
) && (status
& SR_TX_RDY
))
1876 debug_data_cmd(cosa
, DRIVER_RX_READY
);
1878 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1881 static inline void eot_interrupt(struct cosa_data
*cosa
, int status
)
1883 unsigned long flags
, flags1
;
1884 spin_lock_irqsave(&cosa
->lock
, flags
);
1885 flags1
= claim_dma_lock();
1886 disable_dma(cosa
->dma
);
1887 clear_dma_ff(cosa
->dma
);
1888 release_dma_lock(flags1
);
1889 if (test_bit(TXBIT
, &cosa
->rxtx
)) {
1890 struct channel_data
*chan
= cosa
->chan
+cosa
->txchan
;
1892 if (chan
->tx_done(chan
, cosa
->txsize
))
1893 clear_bit(chan
->num
, &cosa
->txbitmap
);
1894 } else if (test_bit(RXBIT
, &cosa
->rxtx
)) {
1898 pr_info("cosa%dc%d: done rx(0x%x)",
1899 cosa
->num
, cosa
->rxchan
->num
, cosa
->rxsize
);
1900 for (i
=0; i
<cosa
->rxsize
; i
++)
1901 pr_cont(" %02x", cosa
->rxbuf
[i
]&0xff);
1905 /* Packet for unknown channel? */
1906 if (cosa
->rxbuf
== cosa
->bouncebuf
)
1908 if (!cosa_dma_able(cosa
->rxchan
, cosa
->rxbuf
, cosa
->rxsize
))
1909 memcpy(cosa
->rxbuf
, cosa
->bouncebuf
, cosa
->rxsize
);
1910 if (cosa
->rxchan
->rx_done
)
1911 if (cosa
->rxchan
->rx_done(cosa
->rxchan
))
1912 clear_bit(cosa
->rxchan
->num
, &cosa
->rxbitmap
);
1914 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa
->num
);
1917 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1918 * cleared anyway). We should do it as soon as possible
1919 * so that we can tell the COSA we are done and to give it a time
1924 put_driver_status_nolock(cosa
);
1925 spin_unlock_irqrestore(&cosa
->lock
, flags
);
1928 static irqreturn_t
cosa_interrupt(int irq
, void *cosa_
)
1932 struct cosa_data
*cosa
= cosa_
;
1934 status
= cosa_getstatus(cosa
);
1936 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa
->num
, status
& 0xff);
1939 debug_status_in(cosa
, status
);
1941 switch (status
& SR_CMD_FROM_SRP_MASK
) {
1942 case SR_DOWN_REQUEST
:
1943 tx_interrupt(cosa
, status
);
1946 rx_interrupt(cosa
, status
);
1948 case SR_END_OF_TRANSFER
:
1949 eot_interrupt(cosa
, status
);
1952 /* We may be too fast for SRP. Try to wait a bit more. */
1953 if (count
++ < 100) {
1957 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1958 cosa
->num
, status
& 0xff, count
);
1962 pr_info("%s: %d-times got unknown status in IRQ\n",
1965 pr_info("%s: returning from IRQ\n", cosa
->name
);
1971 /* ---------- I/O debugging routines ---------- */
1973 * These routines can be used to monitor COSA/SRP I/O and to printk()
1974 * the data being transferred on the data and status I/O port in a
1979 static void debug_status_in(struct cosa_data
*cosa
, int status
)
1982 switch (status
& SR_CMD_FROM_SRP_MASK
) {
1986 case SR_DOWN_REQUEST
:
1989 case SR_END_OF_TRANSFER
:
1996 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
1999 status
& SR_USR_RQ
? "USR_RQ|" : "",
2000 status
& SR_TX_RDY
? "TX_RDY|" : "",
2001 status
& SR_RX_RDY
? "RX_RDY|" : "",
2005 static void debug_status_out(struct cosa_data
*cosa
, int status
)
2007 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2010 status
& SR_RX_DMA_ENA
? "RXDMA|" : "!rxdma|",
2011 status
& SR_TX_DMA_ENA
? "TXDMA|" : "!txdma|",
2012 status
& SR_RST
? "RESET|" : "",
2013 status
& SR_USR_INT_ENA
? "USRINT|" : "!usrint|",
2014 status
& SR_TX_INT_ENA
? "TXINT|" : "!txint|",
2015 status
& SR_RX_INT_ENA
? "RXINT" : "!rxint");
2018 static void debug_data_in(struct cosa_data
*cosa
, int data
)
2020 pr_info("%s: IO: data -> 0x%04x\n", cosa
->name
, data
);
2023 static void debug_data_out(struct cosa_data
*cosa
, int data
)
2025 pr_info("%s: IO: data <- 0x%04x\n", cosa
->name
, data
);
2028 static void debug_data_cmd(struct cosa_data
*cosa
, int data
)
2030 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2032 data
& SR_RDY_RCV
? "RX_RDY" : "!rx_rdy",
2033 data
& SR_RDY_SND
? "TX_RDY" : "!tx_rdy");
2037 /* EOF -- this file has not been truncated */