1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
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 (https://www.ics.muni.cz/). The hardware is
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 https://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).
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");
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);
896 remove_wait_queue(&chan->txwaitq, &wait);
897 __set_current_state(TASK_RUNNING);
899 spin_unlock_irqrestore(&cosa->lock, flags);
904 static int chrdev_tx_done(struct channel_data *chan, int size)
906 if (chan->tx_status) { /* Writer was interrupted */
911 wake_up_interruptible(&chan->txwaitq);
915 static __poll_t cosa_poll(struct file *file, poll_table *poll)
917 pr_info("cosa_poll is here\n");
921 static int cosa_open(struct inode *inode, struct file *file)
923 struct cosa_data *cosa;
924 struct channel_data *chan;
929 mutex_lock(&cosa_chardev_mutex);
930 if ((n=iminor(file_inode(file))>>CARD_MINOR_BITS)
937 if ((n=iminor(file_inode(file))
938 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
942 chan = cosa->chan + n;
944 file->private_data = chan;
946 spin_lock_irqsave(&cosa->lock, flags);
948 if (chan->usage < 0) { /* in netdev mode */
949 spin_unlock_irqrestore(&cosa->lock, flags);
956 chan->tx_done = chrdev_tx_done;
957 chan->setup_rx = chrdev_setup_rx;
958 chan->rx_done = chrdev_rx_done;
959 spin_unlock_irqrestore(&cosa->lock, flags);
961 mutex_unlock(&cosa_chardev_mutex);
965 static int cosa_release(struct inode *inode, struct file *file)
967 struct channel_data *channel = file->private_data;
968 struct cosa_data *cosa;
971 cosa = channel->cosa;
972 spin_lock_irqsave(&cosa->lock, flags);
975 spin_unlock_irqrestore(&cosa->lock, flags);
979 #ifdef COSA_FASYNC_WORKING
980 static struct fasync_struct *fasync[256] = { NULL, };
983 static int cosa_fasync(struct inode *inode, struct file *file, int on)
985 int port = iminor(inode);
987 return fasync_helper(inode, file, on, &fasync[port]);
992 /* ---------- Ioctls ---------- */
995 * Ioctl subroutines can safely be made inline, because they are called
996 * only from cosa_ioctl().
998 static inline int cosa_reset(struct cosa_data *cosa)
1000 char idstring[COSA_MAX_ID_STRING];
1001 if (cosa->usage > 1)
1002 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1003 cosa->num, cosa->usage);
1004 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1005 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1006 pr_notice("cosa%d: reset failed\n", cosa->num);
1009 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1010 cosa->firmware_status |= COSA_FW_RESET;
1014 /* High-level function to download data into COSA memory. Calls download() */
1015 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1017 struct cosa_download d;
1020 if (cosa->usage > 1)
1021 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1022 cosa->name, cosa->usage);
1023 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1024 pr_notice("%s: reset the card first (status %d)\n",
1025 cosa->name, cosa->firmware_status);
1029 if (copy_from_user(&d, arg, sizeof(d)))
1032 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1034 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1038 /* If something fails, force the user to reset the card */
1039 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1041 i = download(cosa, d.code, d.len, d.addr);
1043 pr_notice("cosa%d: microcode download failed: %d\n",
1047 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1048 cosa->num, d.len, d.addr);
1049 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1053 /* High-level function to read COSA memory. Calls readmem() */
1054 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1056 struct cosa_download d;
1059 if (cosa->usage > 1)
1060 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1061 cosa->num, cosa->usage);
1062 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1063 pr_notice("%s: reset the card first (status %d)\n",
1064 cosa->name, cosa->firmware_status);
1068 if (copy_from_user(&d, arg, sizeof(d)))
1071 /* If something fails, force the user to reset the card */
1072 cosa->firmware_status &= ~COSA_FW_RESET;
1074 i = readmem(cosa, d.code, d.len, d.addr);
1076 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1079 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1080 cosa->num, d.len, d.addr);
1081 cosa->firmware_status |= COSA_FW_RESET;
1085 /* High-level function to start microcode. Calls startmicrocode(). */
1086 static inline int cosa_start(struct cosa_data *cosa, int address)
1090 if (cosa->usage > 1)
1091 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1092 cosa->num, cosa->usage);
1094 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1095 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1096 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1097 cosa->name, cosa->firmware_status);
1100 cosa->firmware_status &= ~COSA_FW_RESET;
1101 if ((i=startmicrocode(cosa, address)) < 0) {
1102 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1103 cosa->num, address, i);
1106 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1107 cosa->startaddr = address;
1108 cosa->firmware_status |= COSA_FW_START;
1112 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1113 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1115 int l = strlen(cosa->id_string)+1;
1116 if (copy_to_user(string, cosa->id_string, l))
1121 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1122 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1124 int l = strlen(cosa->type)+1;
1125 if (copy_to_user(string, cosa->type, l))
1130 static int cosa_ioctl_common(struct cosa_data *cosa,
1131 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1133 void __user *argp = (void __user *)arg;
1135 case COSAIORSET: /* Reset the device */
1136 if (!capable(CAP_NET_ADMIN))
1138 return cosa_reset(cosa);
1139 case COSAIOSTRT: /* Start the firmware */
1140 if (!capable(CAP_SYS_RAWIO))
1142 return cosa_start(cosa, arg);
1143 case COSAIODOWNLD: /* Download the firmware */
1144 if (!capable(CAP_SYS_RAWIO))
1147 return cosa_download(cosa, argp);
1149 if (!capable(CAP_SYS_RAWIO))
1151 return cosa_readmem(cosa, argp);
1153 return cosa_gettype(cosa, argp);
1155 return cosa_getidstr(cosa, argp);
1159 return cosa->nchannels;
1161 if (!capable(CAP_SYS_RAWIO))
1165 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1167 cosa->busmaster = arg;
1170 return cosa->busmaster;
1172 return -ENOIOCTLCMD;
1175 static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1178 struct channel_data *chan = dev_to_chan(dev);
1179 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1180 (unsigned long)ifr->ifr_data);
1181 if (rv != -ENOIOCTLCMD)
1183 return hdlc_ioctl(dev, ifr, cmd);
1186 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1189 struct channel_data *channel = file->private_data;
1190 struct cosa_data *cosa;
1193 mutex_lock(&cosa_chardev_mutex);
1194 cosa = channel->cosa;
1195 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1196 mutex_unlock(&cosa_chardev_mutex);
1201 /*---------- HW layer interface ---------- */
1204 * The higher layer can bind itself to the HW layer by setting the callbacks
1205 * in the channel_data structure and by using these routines.
1207 static void cosa_enable_rx(struct channel_data *chan)
1209 struct cosa_data *cosa = chan->cosa;
1211 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1212 put_driver_status(cosa);
1215 static void cosa_disable_rx(struct channel_data *chan)
1217 struct cosa_data *cosa = chan->cosa;
1219 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1220 put_driver_status(cosa);
1224 * FIXME: This routine probably should check for cosa_start_tx() called when
1225 * the previous transmit is still unfinished. In this case the non-zero
1226 * return value should indicate to the caller that the queuing(sp?) up
1227 * the transmit has failed.
1229 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1231 struct cosa_data *cosa = chan->cosa;
1232 unsigned long flags;
1236 pr_info("cosa%dc%d: starting tx(0x%x)",
1237 chan->cosa->num, chan->num, len);
1238 for (i=0; i<len; i++)
1239 pr_cont(" %02x", buf[i]&0xff);
1242 spin_lock_irqsave(&cosa->lock, flags);
1246 chan->txsize = COSA_MTU;
1247 spin_unlock_irqrestore(&cosa->lock, flags);
1249 /* Tell the firmware we are ready */
1250 set_bit(chan->num, &cosa->txbitmap);
1251 put_driver_status(cosa);
1256 static void put_driver_status(struct cosa_data *cosa)
1258 unsigned long flags;
1261 spin_lock_irqsave(&cosa->lock, flags);
1263 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1264 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1265 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1266 &DRIVER_TXMAP_MASK : 0);
1268 if (cosa->rxbitmap|cosa->txbitmap) {
1269 if (!cosa->enabled) {
1270 cosa_putstatus(cosa, SR_RX_INT_ENA);
1272 debug_status_out(cosa, SR_RX_INT_ENA);
1276 } else if (cosa->enabled) {
1278 cosa_putstatus(cosa, 0);
1280 debug_status_out(cosa, 0);
1283 cosa_putdata8(cosa, status);
1285 debug_data_cmd(cosa, status);
1288 spin_unlock_irqrestore(&cosa->lock, flags);
1291 static void put_driver_status_nolock(struct cosa_data *cosa)
1295 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1296 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1297 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1298 &DRIVER_TXMAP_MASK : 0);
1300 if (cosa->rxbitmap|cosa->txbitmap) {
1301 cosa_putstatus(cosa, SR_RX_INT_ENA);
1303 debug_status_out(cosa, SR_RX_INT_ENA);
1307 cosa_putstatus(cosa, 0);
1309 debug_status_out(cosa, 0);
1313 cosa_putdata8(cosa, status);
1315 debug_data_cmd(cosa, status);
1320 * The "kickme" function: When the DMA times out, this is called to
1321 * clean up the driver status.
1322 * FIXME: Preliminary support, the interface is probably wrong.
1324 static void cosa_kick(struct cosa_data *cosa)
1326 unsigned long flags, flags1;
1327 char *s = "(probably) IRQ";
1329 if (test_bit(RXBIT, &cosa->rxtx))
1331 if (test_bit(TXBIT, &cosa->rxtx))
1334 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1335 spin_lock_irqsave(&cosa->lock, flags);
1338 flags1 = claim_dma_lock();
1339 disable_dma(cosa->dma);
1340 clear_dma_ff(cosa->dma);
1341 release_dma_lock(flags1);
1343 /* FIXME: Anything else? */
1345 cosa_putstatus(cosa, 0);
1347 (void) cosa_getdata8(cosa);
1349 cosa_putdata8(cosa, 0);
1351 put_driver_status_nolock(cosa);
1352 spin_unlock_irqrestore(&cosa->lock, flags);
1356 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1357 * physical memory and doesn't span the 64k boundary. For now it seems
1358 * SKB's never do this, but we'll check this anyway.
1360 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1363 unsigned long b = (unsigned long)buf;
1364 if (b+len >= MAX_DMA_ADDRESS)
1366 if ((b^ (b+len)) & 0x10000) {
1368 pr_info("%s: packet spanning a 64k boundary\n",
1376 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1379 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1380 * drivers need to say 4-digit hex number meaning start address of the microcode
1381 * separated by a single space. Monitor replies by saying " =". Now driver
1382 * has to write 4-digit hex number meaning the last byte address ended
1383 * by a single space. Monitor has to reply with a space. Now the download
1384 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1386 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1390 if (put_wait_data(cosa, 'w') == -1) return -1;
1391 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1392 if (get_wait_data(cosa) != '=') return -3;
1394 if (puthexnumber(cosa, address) < 0) return -4;
1395 if (put_wait_data(cosa, ' ') == -1) return -10;
1396 if (get_wait_data(cosa) != ' ') return -11;
1397 if (get_wait_data(cosa) != '=') return -12;
1399 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1400 if (put_wait_data(cosa, ' ') == -1) return -18;
1401 if (get_wait_data(cosa) != ' ') return -19;
1405 #ifndef SRP_DOWNLOAD_AT_BOOT
1406 if (get_user(c, microcode))
1407 return -23; /* ??? */
1411 if (put_wait_data(cosa, c) == -1)
1416 if (get_wait_data(cosa) != '\r') return -21;
1417 if (get_wait_data(cosa) != '\n') return -22;
1418 if (get_wait_data(cosa) != '.') return -23;
1420 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1427 * Starting microcode is done via the "g" command of the SRP monitor.
1428 * The chat should be the following: "g" "g=" "<addr><CR>"
1429 * "<CR><CR><LF><CR><LF>".
1431 static int startmicrocode(struct cosa_data *cosa, int address)
1433 if (put_wait_data(cosa, 'g') == -1) return -1;
1434 if (get_wait_data(cosa) != 'g') return -2;
1435 if (get_wait_data(cosa) != '=') return -3;
1437 if (puthexnumber(cosa, address) < 0) return -4;
1438 if (put_wait_data(cosa, '\r') == -1) return -5;
1440 if (get_wait_data(cosa) != '\r') return -6;
1441 if (get_wait_data(cosa) != '\r') return -7;
1442 if (get_wait_data(cosa) != '\n') return -8;
1443 if (get_wait_data(cosa) != '\r') return -9;
1444 if (get_wait_data(cosa) != '\n') return -10;
1446 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1452 * Reading memory is done via the "r" command of the SRP monitor.
1453 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1454 * Then driver can read the data and the conversation is finished
1455 * by SRP monitor sending "<CR><LF>." (dot at the end).
1457 * This routine is not needed during the normal operation and serves
1458 * for debugging purposes only.
1460 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1462 if (put_wait_data(cosa, 'r') == -1) return -1;
1463 if ((get_wait_data(cosa)) != 'r') return -2;
1464 if ((get_wait_data(cosa)) != '=') return -3;
1466 if (puthexnumber(cosa, address) < 0) return -4;
1467 if (put_wait_data(cosa, ' ') == -1) return -5;
1468 if (get_wait_data(cosa) != ' ') return -6;
1469 if (get_wait_data(cosa) != '=') return -7;
1471 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1472 if (put_wait_data(cosa, ' ') == -1) return -9;
1473 if (get_wait_data(cosa) != ' ') return -10;
1478 if ((i=get_wait_data(cosa)) == -1) {
1479 pr_info("0x%04x bytes remaining\n", length);
1484 if (put_user(c, microcode))
1485 return -23; /* ??? */
1492 if (get_wait_data(cosa) != '\r') return -21;
1493 if (get_wait_data(cosa) != '\n') return -22;
1494 if (get_wait_data(cosa) != '.') return -23;
1496 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1502 * This function resets the device and reads the initial prompt
1503 * of the device's ROM monitor.
1505 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1507 int i=0, id=0, prev=0, curr=0;
1509 /* Reset the card ... */
1510 cosa_putstatus(cosa, 0);
1511 cosa_getdata8(cosa);
1512 cosa_putstatus(cosa, SR_RST);
1514 /* Disable all IRQs from the card */
1515 cosa_putstatus(cosa, 0);
1518 * Try to read the ID string. The card then prints out the
1519 * identification string ended by the "\n\x2e".
1521 * The following loop is indexed through i (instead of id)
1522 * to avoid looping forever when for any reason
1523 * the port returns '\r', '\n' or '\x2e' permanently.
1525 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1526 if ((curr = get_wait_data(cosa)) == -1) {
1530 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1531 idstring[id++] = curr;
1532 if (curr == 0x2e && prev == '\n')
1535 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1536 idstring[id] = '\0';
1541 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1544 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1545 * bit to be set in a loop. It should be used in the exceptional cases
1546 * only (for example when resetting the card or downloading the firmware.
1548 static int get_wait_data(struct cosa_data *cosa)
1553 /* read data and return them */
1554 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1556 r = cosa_getdata8(cosa);
1558 pr_info("get_wait_data returning after %d retries\n",
1563 /* sleep if not ready to read */
1564 schedule_timeout_interruptible(1);
1566 pr_info("timeout in get_wait_data (status 0x%x)\n",
1567 cosa_getstatus(cosa));
1572 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1573 * bit to be set in a loop. It should be used in the exceptional cases
1574 * only (for example when resetting the card or downloading the firmware).
1576 static int put_wait_data(struct cosa_data *cosa, int data)
1580 /* read data and return them */
1581 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1582 cosa_putdata8(cosa, data);
1584 pr_info("Putdata: %d retries\n", 999-retries);
1589 /* sleep if not ready to read */
1590 schedule_timeout_interruptible(1);
1593 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1594 cosa->num, cosa_getstatus(cosa));
1599 * The following routine puts the hexadecimal number into the SRP monitor
1600 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1601 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1602 * (-2,-4,-6,-8) means that reading echo failed.
1604 static int puthexnumber(struct cosa_data *cosa, int number)
1609 /* Well, I should probably replace this by something faster. */
1610 sprintf(temp, "%04X", number);
1611 for (i=0; i<4; i++) {
1612 if (put_wait_data(cosa, temp[i]) == -1) {
1613 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1617 if (get_wait_data(cosa) != temp[i]) {
1618 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1627 /* ---------- Interrupt routines ---------- */
1630 * There are three types of interrupt:
1631 * At the beginning of transmit - this handled is in tx_interrupt(),
1632 * at the beginning of receive - it is in rx_interrupt() and
1633 * at the end of transmit/receive - it is the eot_interrupt() function.
1634 * These functions are multiplexed by cosa_interrupt() according to the
1635 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1636 * separate functions to make it more readable. These functions are inline,
1637 * so there should be no overhead of function call.
1639 * In the COSA bus-master mode, we need to tell the card the address of a
1640 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1641 * It's time to use the bottom half :-(
1645 * Transmit interrupt routine - called when COSA is willing to obtain
1646 * data from the OS. The most tricky part of the routine is selection
1647 * of channel we (OS) want to send packet for. For SRP we should probably
1648 * use the round-robin approach. The newer COSA firmwares have a simple
1649 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1650 * channel 0 or 1 doesn't want to receive data.
1652 * It seems there is a bug in COSA firmware (need to trace it further):
1653 * When the driver status says that the kernel has no more data for transmit
1654 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1655 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1656 * the TX interrupt but does not mark the channel as ready-to-transmit.
1657 * The fix seems to be to push the packet to COSA despite its request.
1658 * We first try to obey the card's opinion, and then fall back to forced TX.
1660 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1662 unsigned long flags, flags1;
1664 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1666 spin_lock_irqsave(&cosa->lock, flags);
1667 set_bit(TXBIT, &cosa->rxtx);
1668 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1669 /* flow control, see the comment above */
1671 if (!cosa->txbitmap) {
1672 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1674 put_driver_status_nolock(cosa);
1675 clear_bit(TXBIT, &cosa->rxtx);
1676 spin_unlock_irqrestore(&cosa->lock, flags);
1682 if (cosa->txchan >= cosa->nchannels)
1684 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1686 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1688 /* in second pass, accept first ready-to-TX channel */
1689 if (i > cosa->nchannels) {
1690 /* Can be safely ignored */
1692 printk(KERN_DEBUG "%s: Forcing TX "
1693 "to not-ready channel %d\n",
1694 cosa->name, cosa->txchan);
1700 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1701 if (cosa_dma_able(cosa->chan+cosa->txchan,
1702 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1703 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1705 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1707 cosa->txbuf = cosa->bouncebuf;
1711 if (is_8bit(cosa)) {
1712 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1713 cosa_putstatus(cosa, SR_TX_INT_ENA);
1714 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1715 ((cosa->txsize >> 8) & 0x1f));
1717 debug_status_out(cosa, SR_TX_INT_ENA);
1718 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1719 ((cosa->txsize >> 8) & 0x1f));
1720 debug_data_in(cosa, cosa_getdata8(cosa));
1722 cosa_getdata8(cosa);
1724 set_bit(IRQBIT, &cosa->rxtx);
1725 spin_unlock_irqrestore(&cosa->lock, flags);
1728 clear_bit(IRQBIT, &cosa->rxtx);
1729 cosa_putstatus(cosa, 0);
1730 cosa_putdata8(cosa, cosa->txsize&0xff);
1732 debug_status_out(cosa, 0);
1733 debug_data_out(cosa, cosa->txsize&0xff);
1737 cosa_putstatus(cosa, SR_TX_INT_ENA);
1738 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1739 | (cosa->txsize & 0x1fff));
1741 debug_status_out(cosa, SR_TX_INT_ENA);
1742 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1743 | (cosa->txsize & 0x1fff));
1744 debug_data_in(cosa, cosa_getdata8(cosa));
1745 debug_status_out(cosa, 0);
1747 cosa_getdata8(cosa);
1749 cosa_putstatus(cosa, 0);
1752 if (cosa->busmaster) {
1753 unsigned long addr = virt_to_bus(cosa->txbuf);
1755 pr_info("busmaster IRQ\n");
1756 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1759 if (count > 1000) break;
1761 pr_info("status %x\n", cosa_getstatus(cosa));
1762 pr_info("ready after %d loops\n", count);
1763 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1766 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1768 if (count > 1000) break;
1771 pr_info("ready after %d loops\n", count);
1772 cosa_putdata16(cosa, addr &0xffff);
1773 flags1 = claim_dma_lock();
1774 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1775 enable_dma(cosa->dma);
1776 release_dma_lock(flags1);
1779 flags1 = claim_dma_lock();
1780 disable_dma(cosa->dma);
1781 clear_dma_ff(cosa->dma);
1782 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1783 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1784 set_dma_count(cosa->dma, cosa->txsize);
1785 enable_dma(cosa->dma);
1786 release_dma_lock(flags1);
1788 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1790 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1792 spin_unlock_irqrestore(&cosa->lock, flags);
1795 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1797 unsigned long flags;
1799 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1802 spin_lock_irqsave(&cosa->lock, flags);
1803 set_bit(RXBIT, &cosa->rxtx);
1805 if (is_8bit(cosa)) {
1806 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1807 set_bit(IRQBIT, &cosa->rxtx);
1808 put_driver_status_nolock(cosa);
1809 cosa->rxsize = cosa_getdata8(cosa) <<8;
1811 debug_data_in(cosa, cosa->rxsize >> 8);
1813 spin_unlock_irqrestore(&cosa->lock, flags);
1816 clear_bit(IRQBIT, &cosa->rxtx);
1817 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1819 debug_data_in(cosa, cosa->rxsize & 0xff);
1822 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1823 cosa->num, cosa->rxsize);
1827 cosa->rxsize = cosa_getdata16(cosa);
1829 debug_data_in(cosa, cosa->rxsize);
1832 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1833 cosa->num, cosa->rxsize);
1836 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1837 pr_warn("%s: rx for unknown channel (0x%04x)\n",
1838 cosa->name, cosa->rxsize);
1839 spin_unlock_irqrestore(&cosa->lock, flags);
1842 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1843 cosa->rxsize &= 0x1fff;
1844 spin_unlock_irqrestore(&cosa->lock, flags);
1847 if (cosa->rxchan->setup_rx)
1848 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1851 reject: /* Reject the packet */
1852 pr_info("cosa%d: rejecting packet on channel %d\n",
1853 cosa->num, cosa->rxchan->num);
1854 cosa->rxbuf = cosa->bouncebuf;
1858 flags = claim_dma_lock();
1859 disable_dma(cosa->dma);
1860 clear_dma_ff(cosa->dma);
1861 set_dma_mode(cosa->dma, DMA_MODE_READ);
1862 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1863 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1865 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1867 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1868 enable_dma(cosa->dma);
1869 release_dma_lock(flags);
1870 spin_lock_irqsave(&cosa->lock, flags);
1871 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1872 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1873 cosa_putdata8(cosa, DRIVER_RX_READY);
1875 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1876 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1877 debug_data_cmd(cosa, DRIVER_RX_READY);
1879 spin_unlock_irqrestore(&cosa->lock, flags);
1882 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1884 unsigned long flags, flags1;
1885 spin_lock_irqsave(&cosa->lock, flags);
1886 flags1 = claim_dma_lock();
1887 disable_dma(cosa->dma);
1888 clear_dma_ff(cosa->dma);
1889 release_dma_lock(flags1);
1890 if (test_bit(TXBIT, &cosa->rxtx)) {
1891 struct channel_data *chan = cosa->chan+cosa->txchan;
1893 if (chan->tx_done(chan, cosa->txsize))
1894 clear_bit(chan->num, &cosa->txbitmap);
1895 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1899 pr_info("cosa%dc%d: done rx(0x%x)",
1900 cosa->num, cosa->rxchan->num, cosa->rxsize);
1901 for (i=0; i<cosa->rxsize; i++)
1902 pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1906 /* Packet for unknown channel? */
1907 if (cosa->rxbuf == cosa->bouncebuf)
1909 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1910 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1911 if (cosa->rxchan->rx_done)
1912 if (cosa->rxchan->rx_done(cosa->rxchan))
1913 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1915 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1918 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1919 * cleared anyway). We should do it as soon as possible
1920 * so that we can tell the COSA we are done and to give it a time
1925 put_driver_status_nolock(cosa);
1926 spin_unlock_irqrestore(&cosa->lock, flags);
1929 static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1933 struct cosa_data *cosa = cosa_;
1935 status = cosa_getstatus(cosa);
1937 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1940 debug_status_in(cosa, status);
1942 switch (status & SR_CMD_FROM_SRP_MASK) {
1943 case SR_DOWN_REQUEST:
1944 tx_interrupt(cosa, status);
1947 rx_interrupt(cosa, status);
1949 case SR_END_OF_TRANSFER:
1950 eot_interrupt(cosa, status);
1953 /* We may be too fast for SRP. Try to wait a bit more. */
1954 if (count++ < 100) {
1958 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1959 cosa->num, status & 0xff, count);
1963 pr_info("%s: %d-times got unknown status in IRQ\n",
1966 pr_info("%s: returning from IRQ\n", cosa->name);
1972 /* ---------- I/O debugging routines ---------- */
1974 * These routines can be used to monitor COSA/SRP I/O and to printk()
1975 * the data being transferred on the data and status I/O port in a
1980 static void debug_status_in(struct cosa_data *cosa, int status)
1983 switch (status & SR_CMD_FROM_SRP_MASK) {
1987 case SR_DOWN_REQUEST:
1990 case SR_END_OF_TRANSFER:
1997 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2000 status & SR_USR_RQ ? "USR_RQ|" : "",
2001 status & SR_TX_RDY ? "TX_RDY|" : "",
2002 status & SR_RX_RDY ? "RX_RDY|" : "",
2006 static void debug_status_out(struct cosa_data *cosa, int status)
2008 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2011 status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2012 status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2013 status & SR_RST ? "RESET|" : "",
2014 status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2015 status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2016 status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
2019 static void debug_data_in(struct cosa_data *cosa, int data)
2021 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2024 static void debug_data_out(struct cosa_data *cosa, int data)
2026 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2029 static void debug_data_cmd(struct cosa_data *cosa, int data)
2031 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2033 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2034 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2038 /* EOF -- this file has not been truncated */