1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
4 /* Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <kas@fi.muni.cz>
5 * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
8 /* The driver for the SRP and COSA synchronous serial cards.
12 * Both cards are developed at the Institute of Computer Science,
13 * Masaryk University (https://www.ics.muni.cz/). The hardware is
14 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
15 * and the photo of both cards is available at
16 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
17 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
18 * For Linux-specific utilities, see below in the "Software info" section.
19 * If you want to order the card, contact Jiri Novotny.
21 * The SRP (serial port?, the Czech word "srp" means "sickle") card
22 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
23 * with V.24 interfaces up to 80kb/s each.
25 * The COSA (communication serial adapter?, the Czech word "kosa" means
26 * "scythe") is a next-generation sync/async board with two interfaces
27 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
28 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
29 * The 8-channels version is in development.
31 * Both types have downloadable firmware and communicate via ISA DMA.
32 * COSA can be also a bus-mastering device.
36 * The homepage of the Linux driver is at https://www.fi.muni.cz/~kas/cosa/.
37 * The CVS tree of Linux driver can be viewed there, as well as the
38 * firmware binaries and user-space utilities for downloading the firmware
39 * into the card and setting up the card.
41 * The Linux driver (unlike the present *BSD drivers :-) can work even
42 * for the COSA and SRP in one computer and allows each channel to work
43 * in one of the two modes (character or network device).
47 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
49 * You can mail me bugfixes and even success reports. I am especially
50 * interested in the SMP and/or muliti-channel success/failure reports
51 * (I wonder if I did the locking properly :-).
53 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
55 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
56 * The skeleton.c by Donald Becker
57 * The SDL Riscom/N2 driver by Mike Natale
58 * The Comtrol Hostess SV11 driver by Alan Cox
59 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
62 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
64 #include <linux/module.h>
65 #include <linux/kernel.h>
66 #include <linux/sched/signal.h>
67 #include <linux/slab.h>
68 #include <linux/poll.h>
70 #include <linux/interrupt.h>
71 #include <linux/delay.h>
72 #include <linux/hdlc.h>
73 #include <linux/errno.h>
74 #include <linux/ioport.h>
75 #include <linux/netdevice.h>
76 #include <linux/spinlock.h>
77 #include <linux/mutex.h>
78 #include <linux/device.h>
81 #include <asm/byteorder.h>
83 #undef COSA_SLOW_IO /* for testing purposes only */
87 /* Maximum length of the identification string. */
88 #define COSA_MAX_ID_STRING 128
90 /* Maximum length of the channel name */
91 #define COSA_MAX_NAME (sizeof("cosaXXXcXXX") + 1)
93 /* Per-channel data structure */
96 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
97 int num; /* Number of the channel */
98 struct cosa_data *cosa; /* Pointer to the per-card structure */
99 int txsize; /* Size of transmitted data */
100 char *txbuf; /* Transmit buffer */
101 char name[COSA_MAX_NAME]; /* channel name */
103 /* The HW layer interface */
104 /* routine called from the RX interrupt */
105 char *(*setup_rx)(struct channel_data *channel, int size);
106 /* routine called when the RX is done (from the EOT interrupt) */
107 int (*rx_done)(struct channel_data *channel);
108 /* routine called when the TX is done (from the EOT interrupt) */
109 int (*tx_done)(struct channel_data *channel, int size);
111 /* Character device parts */
113 struct semaphore wsem;
116 wait_queue_head_t txwaitq, rxwaitq;
117 int tx_status, rx_status;
119 /* generic HDLC device parts */
120 struct net_device *netdev;
121 struct sk_buff *rx_skb, *tx_skb;
124 /* cosa->firmware_status bits */
125 #define COSA_FW_RESET BIT(0) /* Is the ROM monitor active? */
126 #define COSA_FW_DOWNLOAD BIT(1) /* Is the microcode downloaded? */
127 #define COSA_FW_START BIT(2) /* Is the microcode running? */
130 int num; /* Card number */
131 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
132 unsigned int datareg, statusreg; /* I/O ports */
133 unsigned short irq, dma; /* IRQ and DMA number */
134 unsigned short startaddr; /* Firmware start address */
135 unsigned short busmaster; /* Use busmastering? */
136 int nchannels; /* # of channels on this card */
137 int driver_status; /* For communicating with firmware */
138 int firmware_status; /* Downloaded, reseted, etc. */
139 unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
140 unsigned long rxtx; /* RX or TX in progress? */
142 int usage; /* usage count */
143 int txchan, txsize, rxsize;
144 struct channel_data *rxchan;
147 struct channel_data *chan;
148 spinlock_t lock; /* For exclusive operations on this structure */
149 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
150 char *type; /* card type */
153 /* Define this if you want all the possible ports to be autoprobed.
154 * It is here but it probably is not a good idea to use this.
156 /* #define COSA_ISA_AUTOPROBE 1*/
158 /* Character device major number. 117 was allocated for us.
159 * The value of 0 means to allocate a first free one.
161 static DEFINE_MUTEX(cosa_chardev_mutex);
162 static int cosa_major = 117;
164 /* Encoding of the minor numbers:
165 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
166 * the highest bits means the card number.
168 #define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
169 * for the single card
171 /* The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
172 * macro doesn't like anything other than the raw number as an argument :-(
175 /* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
177 #define DRIVER_RX_READY 0x0001
178 #define DRIVER_TX_READY 0x0002
179 #define DRIVER_TXMAP_SHIFT 2
180 #define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
182 /* for cosa->rxtx - indicates whether either transmit or receive is
183 * in progress. These values are mean number of the bit.
189 #define COSA_MTU 2000 /* FIXME: I don't know this exactly */
191 #undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
192 #undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
193 #undef DEBUG_IO //1 /* Dump the I/O traffic */
195 #define TX_TIMEOUT (5 * HZ)
197 /* Maybe the following should be allocated dynamically */
198 static struct cosa_data cosa_cards[MAX_CARDS];
201 #ifdef COSA_ISA_AUTOPROBE
202 static int io[MAX_CARDS + 1] = {0x220, 0x228, 0x210, 0x218, 0,};
203 /* NOTE: DMA is not autoprobed!!! */
204 static int dma[MAX_CARDS + 1] = {1, 7, 1, 7, 1, 7, 1, 7, 0,};
206 static int io[MAX_CARDS + 1];
207 static int dma[MAX_CARDS + 1];
209 /* IRQ can be safely autoprobed */
210 static int irq[MAX_CARDS + 1] = {-1, -1, -1, -1, -1, -1, 0,};
213 static struct class *cosa_class;
216 module_param_hw_array(io, int, ioport, NULL, 0);
217 MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
218 module_param_hw_array(irq, int, irq, NULL, 0);
219 MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
220 module_param_hw_array(dma, int, dma, NULL, 0);
221 MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
223 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
224 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
225 MODULE_LICENSE("GPL");
228 /* I use this mainly for testing purposes */
230 #define cosa_outb outb_p
231 #define cosa_outw outw_p
232 #define cosa_inb inb_p
233 #define cosa_inw inw_p
235 #define cosa_outb outb
236 #define cosa_outw outw
241 #define is_8bit(cosa) (!((cosa)->datareg & 0x08))
243 #define cosa_getstatus(cosa) (cosa_inb((cosa)->statusreg))
244 #define cosa_putstatus(cosa, stat) (cosa_outb(stat, (cosa)->statusreg))
245 #define cosa_getdata16(cosa) (cosa_inw((cosa)->datareg))
246 #define cosa_getdata8(cosa) (cosa_inb((cosa)->datareg))
247 #define cosa_putdata16(cosa, dt) (cosa_outw(dt, (cosa)->datareg))
248 #define cosa_putdata8(cosa, dt) (cosa_outb(dt, (cosa)->datareg))
250 /* Initialization stuff */
251 static int cosa_probe(int ioaddr, int irq, int dma);
254 static void cosa_enable_rx(struct channel_data *chan);
255 static void cosa_disable_rx(struct channel_data *chan);
256 static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
257 static void cosa_kick(struct cosa_data *cosa);
258 static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
260 /* Network device stuff */
261 static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
262 unsigned short parity);
263 static int cosa_net_open(struct net_device *d);
264 static int cosa_net_close(struct net_device *d);
265 static void cosa_net_timeout(struct net_device *d, unsigned int txqueue);
266 static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
267 static char *cosa_net_setup_rx(struct channel_data *channel, int size);
268 static int cosa_net_rx_done(struct channel_data *channel);
269 static int cosa_net_tx_done(struct channel_data *channel, int size);
271 /* Character device */
272 static char *chrdev_setup_rx(struct channel_data *channel, int size);
273 static int chrdev_rx_done(struct channel_data *channel);
274 static int chrdev_tx_done(struct channel_data *channel, int size);
275 static ssize_t cosa_read(struct file *file,
276 char __user *buf, size_t count, loff_t *ppos);
277 static ssize_t cosa_write(struct file *file,
278 const char __user *buf, size_t count, loff_t *ppos);
279 static unsigned int cosa_poll(struct file *file, poll_table *poll);
280 static int cosa_open(struct inode *inode, struct file *file);
281 static int cosa_release(struct inode *inode, struct file *file);
282 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
284 #ifdef COSA_FASYNC_WORKING
285 static int cosa_fasync(struct inode *inode, struct file *file, int on);
288 static const struct file_operations cosa_fops = {
289 .owner = THIS_MODULE,
294 .unlocked_ioctl = cosa_chardev_ioctl,
296 .release = cosa_release,
297 #ifdef COSA_FASYNC_WORKING
298 .fasync = cosa_fasync,
303 static int cosa_start(struct cosa_data *cosa, int address);
304 static int cosa_reset(struct cosa_data *cosa);
305 static int cosa_download(struct cosa_data *cosa, void __user *a);
306 static int cosa_readmem(struct cosa_data *cosa, void __user *a);
308 /* COSA/SRP ROM monitor */
309 static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
310 static int startmicrocode(struct cosa_data *cosa, int address);
311 static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
312 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
314 /* Auxiliary functions */
315 static int get_wait_data(struct cosa_data *cosa);
316 static int put_wait_data(struct cosa_data *cosa, int data);
317 static int puthexnumber(struct cosa_data *cosa, int number);
318 static void put_driver_status(struct cosa_data *cosa);
319 static void put_driver_status_nolock(struct cosa_data *cosa);
321 /* Interrupt handling */
322 static irqreturn_t cosa_interrupt(int irq, void *cosa);
324 /* I/O ops debugging */
326 static void debug_data_in(struct cosa_data *cosa, int data);
327 static void debug_data_out(struct cosa_data *cosa, int data);
328 static void debug_data_cmd(struct cosa_data *cosa, int data);
329 static void debug_status_in(struct cosa_data *cosa, int status);
330 static void debug_status_out(struct cosa_data *cosa, int status);
333 static inline struct channel_data *dev_to_chan(struct net_device *dev)
335 return (struct channel_data *)dev_to_hdlc(dev)->priv;
338 /* ---------- Initialization stuff ---------- */
340 static int __init cosa_init(void)
344 if (cosa_major > 0) {
345 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
346 pr_warn("unable to get major %d\n", cosa_major);
351 cosa_major = register_chrdev(0, "cosa", &cosa_fops);
353 pr_warn("unable to register chardev\n");
358 for (i = 0; i < MAX_CARDS; i++)
359 cosa_cards[i].num = -1;
360 for (i = 0; io[i] != 0 && i < MAX_CARDS; i++)
361 cosa_probe(io[i], irq[i], dma[i]);
363 pr_warn("no devices found\n");
364 unregister_chrdev(cosa_major, "cosa");
368 cosa_class = class_create(THIS_MODULE, "cosa");
369 if (IS_ERR(cosa_class)) {
370 err = PTR_ERR(cosa_class);
373 for (i = 0; i < nr_cards; i++)
374 device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
380 unregister_chrdev(cosa_major, "cosa");
384 module_init(cosa_init);
386 static void __exit cosa_exit(void)
388 struct cosa_data *cosa;
391 for (i = 0; i < nr_cards; i++)
392 device_destroy(cosa_class, MKDEV(cosa_major, i));
393 class_destroy(cosa_class);
395 for (cosa = cosa_cards; nr_cards--; cosa++) {
396 /* Clean up the per-channel data */
397 for (i = 0; i < cosa->nchannels; i++) {
398 /* Chardev driver has no alloc'd per-channel data */
399 unregister_hdlc_device(cosa->chan[i].netdev);
400 free_netdev(cosa->chan[i].netdev);
402 /* Clean up the per-card data */
404 kfree(cosa->bouncebuf);
405 free_irq(cosa->irq, cosa);
407 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
409 unregister_chrdev(cosa_major, "cosa");
411 module_exit(cosa_exit);
413 static const struct net_device_ops cosa_ops = {
414 .ndo_open = cosa_net_open,
415 .ndo_stop = cosa_net_close,
416 .ndo_start_xmit = hdlc_start_xmit,
417 .ndo_siocwandev = hdlc_ioctl,
418 .ndo_tx_timeout = cosa_net_timeout,
421 static int cosa_probe(int base, int irq, int dma)
423 struct cosa_data *cosa = cosa_cards + nr_cards;
426 memset(cosa, 0, sizeof(struct cosa_data));
428 /* Checking validity of parameters: */
429 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
430 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
431 pr_info("invalid IRQ %d\n", irq);
434 /* I/O address should be between 0x100 and 0x3ff and should be
437 if (base < 0x100 || base > 0x3ff || base & 0x7) {
438 pr_info("invalid I/O address 0x%x\n", base);
441 /* DMA should be 0,1 or 3-7 */
442 if (dma < 0 || dma == 4 || dma > 7) {
443 pr_info("invalid DMA %d\n", dma);
446 /* and finally, on 16-bit COSA DMA should be 4-7 and
447 * I/O base should not be multiple of 0x10
449 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
450 pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
456 cosa->datareg = base;
457 cosa->statusreg = is_8bit(cosa) ? base + 1 : base + 2;
458 spin_lock_init(&cosa->lock);
460 if (!request_region(base, is_8bit(cosa) ? 2 : 4, "cosa"))
463 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
464 printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
469 /* Test the validity of identification string */
470 if (!strncmp(cosa->id_string, "SRP", 3)) {
472 } else if (!strncmp(cosa->id_string, "COSA", 4)) {
473 cosa->type = is_8bit(cosa) ? "cosa8" : "cosa16";
475 /* Print a warning only if we are not autoprobing */
476 #ifndef COSA_ISA_AUTOPROBE
477 pr_info("valid signature not found at 0x%x\n", base);
482 /* Update the name of the region now we know the type of card */
483 release_region(base, is_8bit(cosa) ? 2 : 4);
484 if (!request_region(base, is_8bit(cosa) ? 2 : 4, cosa->type)) {
485 printk(KERN_DEBUG "changing name at 0x%x failed.\n", base);
489 /* Now do IRQ autoprobe */
492 /* pr_info("IRQ autoprobe\n"); */
493 irqs = probe_irq_on();
494 /* Enable interrupt on tx buffer empty (it sure is)
496 * FIXME: When this code is not used as module, we should
497 * probably call udelay() instead of the interruptible sleep.
499 set_current_state(TASK_INTERRUPTIBLE);
500 cosa_putstatus(cosa, SR_TX_INT_ENA);
501 schedule_timeout(msecs_to_jiffies(300));
502 irq = probe_irq_off(irqs);
503 /* Disable all IRQs from the card */
504 cosa_putstatus(cosa, 0);
505 /* Empty the received data register */
509 pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
515 pr_info("no interrupt obtained (board at 0x%x)\n",
522 cosa->num = nr_cards;
524 cosa->nchannels = 2; /* FIXME: how to determine this? */
526 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
530 if (request_dma(cosa->dma, cosa->type)) {
535 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL | GFP_DMA);
536 if (!cosa->bouncebuf) {
540 sprintf(cosa->name, "cosa%d", cosa->num);
542 /* Initialize the per-channel data */
543 cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
549 for (i = 0; i < cosa->nchannels; i++) {
550 struct channel_data *chan = &cosa->chan[i];
554 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
556 /* Initialize the chardev data structures */
557 mutex_init(&chan->rlock);
558 sema_init(&chan->wsem, 1);
560 /* Register the network interface */
561 chan->netdev = alloc_hdlcdev(chan);
563 pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
567 dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
568 dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
569 chan->netdev->netdev_ops = &cosa_ops;
570 chan->netdev->watchdog_timeo = TX_TIMEOUT;
571 chan->netdev->base_addr = chan->cosa->datareg;
572 chan->netdev->irq = chan->cosa->irq;
573 chan->netdev->dma = chan->cosa->dma;
574 err = register_hdlc_device(chan->netdev);
576 netdev_warn(chan->netdev,
577 "register_hdlc_device() failed\n");
578 free_netdev(chan->netdev);
583 pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
584 cosa->num, cosa->id_string, cosa->type,
585 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
591 unregister_hdlc_device(cosa->chan[i].netdev);
592 free_netdev(cosa->chan[i].netdev);
596 kfree(cosa->bouncebuf);
600 free_irq(cosa->irq, cosa);
602 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
603 pr_notice("cosa%d: allocating resources failed\n", cosa->num);
607 /*---------- network device ---------- */
609 static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
610 unsigned short parity)
612 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
617 static int cosa_net_open(struct net_device *dev)
619 struct channel_data *chan = dev_to_chan(dev);
623 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
624 pr_notice("%s: start the firmware first (status %d)\n",
625 chan->cosa->name, chan->cosa->firmware_status);
628 spin_lock_irqsave(&chan->cosa->lock, flags);
629 if (chan->usage != 0) {
630 pr_warn("%s: cosa_net_open called with usage count %d\n",
631 chan->name, chan->usage);
632 spin_unlock_irqrestore(&chan->cosa->lock, flags);
635 chan->setup_rx = cosa_net_setup_rx;
636 chan->tx_done = cosa_net_tx_done;
637 chan->rx_done = cosa_net_rx_done;
640 spin_unlock_irqrestore(&chan->cosa->lock, flags);
642 err = hdlc_open(dev);
644 spin_lock_irqsave(&chan->cosa->lock, flags);
647 spin_unlock_irqrestore(&chan->cosa->lock, flags);
651 netif_start_queue(dev);
652 cosa_enable_rx(chan);
656 static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
657 struct net_device *dev)
659 struct channel_data *chan = dev_to_chan(dev);
661 netif_stop_queue(dev);
664 cosa_start_tx(chan, skb->data, skb->len);
668 static void cosa_net_timeout(struct net_device *dev, unsigned int txqueue)
670 struct channel_data *chan = dev_to_chan(dev);
672 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
673 chan->netdev->stats.rx_errors++;
674 chan->netdev->stats.rx_missed_errors++;
676 chan->netdev->stats.tx_errors++;
677 chan->netdev->stats.tx_aborted_errors++;
679 cosa_kick(chan->cosa);
681 dev_kfree_skb(chan->tx_skb);
684 netif_wake_queue(dev);
687 static int cosa_net_close(struct net_device *dev)
689 struct channel_data *chan = dev_to_chan(dev);
692 netif_stop_queue(dev);
694 cosa_disable_rx(chan);
695 spin_lock_irqsave(&chan->cosa->lock, flags);
697 kfree_skb(chan->rx_skb);
701 kfree_skb(chan->tx_skb);
706 spin_unlock_irqrestore(&chan->cosa->lock, flags);
710 static char *cosa_net_setup_rx(struct channel_data *chan, int size)
712 /* We can safely fall back to non-dma-able memory, because we have
713 * the cosa->bouncebuf pre-allocated.
715 kfree_skb(chan->rx_skb);
716 chan->rx_skb = dev_alloc_skb(size);
718 pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
719 chan->netdev->stats.rx_dropped++;
722 netif_trans_update(chan->netdev);
723 return skb_put(chan->rx_skb, size);
726 static int cosa_net_rx_done(struct channel_data *chan)
729 pr_warn("%s: rx_done with empty skb!\n", chan->name);
730 chan->netdev->stats.rx_errors++;
731 chan->netdev->stats.rx_frame_errors++;
734 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
735 chan->rx_skb->dev = chan->netdev;
736 skb_reset_mac_header(chan->rx_skb);
737 chan->netdev->stats.rx_packets++;
738 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
739 netif_rx(chan->rx_skb);
745 static int cosa_net_tx_done(struct channel_data *chan, int size)
748 pr_warn("%s: tx_done with empty skb!\n", chan->name);
749 chan->netdev->stats.tx_errors++;
750 chan->netdev->stats.tx_aborted_errors++;
753 dev_consume_skb_irq(chan->tx_skb);
755 chan->netdev->stats.tx_packets++;
756 chan->netdev->stats.tx_bytes += size;
757 netif_wake_queue(chan->netdev);
761 /*---------- Character device ---------- */
763 static ssize_t cosa_read(struct file *file,
764 char __user *buf, size_t count, loff_t *ppos)
766 DECLARE_WAITQUEUE(wait, current);
768 struct channel_data *chan = file->private_data;
769 struct cosa_data *cosa = chan->cosa;
772 if (!(cosa->firmware_status & COSA_FW_START)) {
773 pr_notice("%s: start the firmware first (status %d)\n",
774 cosa->name, cosa->firmware_status);
777 if (mutex_lock_interruptible(&chan->rlock))
780 chan->rxdata = kmalloc(COSA_MTU, GFP_DMA | GFP_KERNEL);
782 mutex_unlock(&chan->rlock);
787 cosa_enable_rx(chan);
788 spin_lock_irqsave(&cosa->lock, flags);
789 add_wait_queue(&chan->rxwaitq, &wait);
790 while (!chan->rx_status) {
791 set_current_state(TASK_INTERRUPTIBLE);
792 spin_unlock_irqrestore(&cosa->lock, flags);
794 spin_lock_irqsave(&cosa->lock, flags);
795 if (signal_pending(current) && chan->rx_status == 0) {
797 remove_wait_queue(&chan->rxwaitq, &wait);
798 __set_current_state(TASK_RUNNING);
799 spin_unlock_irqrestore(&cosa->lock, flags);
800 mutex_unlock(&chan->rlock);
804 remove_wait_queue(&chan->rxwaitq, &wait);
805 __set_current_state(TASK_RUNNING);
807 count = chan->rxsize;
808 spin_unlock_irqrestore(&cosa->lock, flags);
809 mutex_unlock(&chan->rlock);
811 if (copy_to_user(buf, kbuf, count)) {
819 static char *chrdev_setup_rx(struct channel_data *chan, int size)
821 /* Expect size <= COSA_MTU */
826 static int chrdev_rx_done(struct channel_data *chan)
828 if (chan->rx_status) { /* Reader has died */
833 wake_up_interruptible(&chan->rxwaitq);
837 static ssize_t cosa_write(struct file *file,
838 const char __user *buf, size_t count, loff_t *ppos)
840 DECLARE_WAITQUEUE(wait, current);
841 struct channel_data *chan = file->private_data;
842 struct cosa_data *cosa = chan->cosa;
846 if (!(cosa->firmware_status & COSA_FW_START)) {
847 pr_notice("%s: start the firmware first (status %d)\n",
848 cosa->name, cosa->firmware_status);
851 if (down_interruptible(&chan->wsem))
854 if (count > COSA_MTU)
857 /* Allocate the buffer */
858 kbuf = kmalloc(count, GFP_KERNEL | GFP_DMA);
863 if (copy_from_user(kbuf, buf, count)) {
869 cosa_start_tx(chan, kbuf, count);
871 spin_lock_irqsave(&cosa->lock, flags);
872 add_wait_queue(&chan->txwaitq, &wait);
873 while (!chan->tx_status) {
874 set_current_state(TASK_INTERRUPTIBLE);
875 spin_unlock_irqrestore(&cosa->lock, flags);
877 spin_lock_irqsave(&cosa->lock, flags);
878 if (signal_pending(current) && chan->tx_status == 0) {
880 remove_wait_queue(&chan->txwaitq, &wait);
881 __set_current_state(TASK_RUNNING);
883 spin_unlock_irqrestore(&cosa->lock, flags);
889 remove_wait_queue(&chan->txwaitq, &wait);
890 __set_current_state(TASK_RUNNING);
892 spin_unlock_irqrestore(&cosa->lock, flags);
897 static int chrdev_tx_done(struct channel_data *chan, int size)
899 if (chan->tx_status) { /* Writer was interrupted */
904 wake_up_interruptible(&chan->txwaitq);
908 static __poll_t cosa_poll(struct file *file, poll_table *poll)
910 pr_info("cosa_poll is here\n");
914 static int cosa_open(struct inode *inode, struct file *file)
916 struct cosa_data *cosa;
917 struct channel_data *chan;
922 mutex_lock(&cosa_chardev_mutex);
923 n = iminor(file_inode(file)) >> CARD_MINOR_BITS;
928 cosa = cosa_cards + n;
930 n = iminor(file_inode(file)) & ((1 << CARD_MINOR_BITS) - 1);
931 if (n >= cosa->nchannels) {
935 chan = cosa->chan + n;
937 file->private_data = chan;
939 spin_lock_irqsave(&cosa->lock, flags);
941 if (chan->usage < 0) { /* in netdev mode */
942 spin_unlock_irqrestore(&cosa->lock, flags);
949 chan->tx_done = chrdev_tx_done;
950 chan->setup_rx = chrdev_setup_rx;
951 chan->rx_done = chrdev_rx_done;
952 spin_unlock_irqrestore(&cosa->lock, flags);
954 mutex_unlock(&cosa_chardev_mutex);
958 static int cosa_release(struct inode *inode, struct file *file)
960 struct channel_data *channel = file->private_data;
961 struct cosa_data *cosa;
964 cosa = channel->cosa;
965 spin_lock_irqsave(&cosa->lock, flags);
968 spin_unlock_irqrestore(&cosa->lock, flags);
972 #ifdef COSA_FASYNC_WORKING
973 static struct fasync_struct *fasync[256] = { NULL, };
976 static int cosa_fasync(struct inode *inode, struct file *file, int on)
978 int port = iminor(inode);
980 return fasync_helper(inode, file, on, &fasync[port]);
984 /* ---------- Ioctls ---------- */
986 /* Ioctl subroutines can safely be made inline, because they are called
987 * only from cosa_ioctl().
989 static inline int cosa_reset(struct cosa_data *cosa)
991 char idstring[COSA_MAX_ID_STRING];
994 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
995 cosa->num, cosa->usage);
996 cosa->firmware_status &= ~(COSA_FW_RESET | COSA_FW_START);
997 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
998 pr_notice("cosa%d: reset failed\n", cosa->num);
1001 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1002 cosa->firmware_status |= COSA_FW_RESET;
1006 /* High-level function to download data into COSA memory. Calls download() */
1007 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1009 struct cosa_download d;
1012 if (cosa->usage > 1)
1013 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1014 cosa->name, cosa->usage);
1015 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1016 pr_notice("%s: reset the card first (status %d)\n",
1017 cosa->name, cosa->firmware_status);
1021 if (copy_from_user(&d, arg, sizeof(d)))
1024 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1026 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1029 /* If something fails, force the user to reset the card */
1030 cosa->firmware_status &= ~(COSA_FW_RESET | COSA_FW_DOWNLOAD);
1032 i = download(cosa, d.code, d.len, d.addr);
1034 pr_notice("cosa%d: microcode download failed: %d\n",
1038 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1039 cosa->num, d.len, d.addr);
1040 cosa->firmware_status |= COSA_FW_RESET | COSA_FW_DOWNLOAD;
1044 /* High-level function to read COSA memory. Calls readmem() */
1045 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1047 struct cosa_download d;
1050 if (cosa->usage > 1)
1051 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1052 cosa->num, cosa->usage);
1053 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1054 pr_notice("%s: reset the card first (status %d)\n",
1055 cosa->name, cosa->firmware_status);
1059 if (copy_from_user(&d, arg, sizeof(d)))
1062 /* If something fails, force the user to reset the card */
1063 cosa->firmware_status &= ~COSA_FW_RESET;
1065 i = readmem(cosa, d.code, d.len, d.addr);
1067 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1070 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1071 cosa->num, d.len, d.addr);
1072 cosa->firmware_status |= COSA_FW_RESET;
1076 /* High-level function to start microcode. Calls startmicrocode(). */
1077 static inline int cosa_start(struct cosa_data *cosa, int address)
1081 if (cosa->usage > 1)
1082 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1083 cosa->num, cosa->usage);
1085 if ((cosa->firmware_status & (COSA_FW_RESET | COSA_FW_DOWNLOAD))
1086 != (COSA_FW_RESET | COSA_FW_DOWNLOAD)) {
1087 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1088 cosa->name, cosa->firmware_status);
1091 cosa->firmware_status &= ~COSA_FW_RESET;
1092 i = startmicrocode(cosa, address);
1094 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1095 cosa->num, address, i);
1098 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1099 cosa->startaddr = address;
1100 cosa->firmware_status |= COSA_FW_START;
1104 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1105 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1107 int l = strlen(cosa->id_string) + 1;
1109 if (copy_to_user(string, cosa->id_string, l))
1114 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1115 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1117 int l = strlen(cosa->type) + 1;
1119 if (copy_to_user(string, cosa->type, l))
1124 static int cosa_ioctl_common(struct cosa_data *cosa,
1125 struct channel_data *channel, unsigned int cmd,
1128 void __user *argp = (void __user *)arg;
1131 case COSAIORSET: /* Reset the device */
1132 if (!capable(CAP_NET_ADMIN))
1134 return cosa_reset(cosa);
1135 case COSAIOSTRT: /* Start the firmware */
1136 if (!capable(CAP_SYS_RAWIO))
1138 return cosa_start(cosa, arg);
1139 case COSAIODOWNLD: /* Download the firmware */
1140 if (!capable(CAP_SYS_RAWIO))
1143 return cosa_download(cosa, argp);
1145 if (!capable(CAP_SYS_RAWIO))
1147 return cosa_readmem(cosa, argp);
1149 return cosa_gettype(cosa, argp);
1151 return cosa_getidstr(cosa, argp);
1155 return cosa->nchannels;
1157 if (!capable(CAP_SYS_RAWIO))
1161 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1163 cosa->busmaster = arg;
1166 return cosa->busmaster;
1168 return -ENOIOCTLCMD;
1171 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1174 struct channel_data *channel = file->private_data;
1175 struct cosa_data *cosa;
1178 mutex_lock(&cosa_chardev_mutex);
1179 cosa = channel->cosa;
1180 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1181 mutex_unlock(&cosa_chardev_mutex);
1185 /*---------- HW layer interface ---------- */
1187 /* The higher layer can bind itself to the HW layer by setting the callbacks
1188 * in the channel_data structure and by using these routines.
1190 static void cosa_enable_rx(struct channel_data *chan)
1192 struct cosa_data *cosa = chan->cosa;
1194 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1195 put_driver_status(cosa);
1198 static void cosa_disable_rx(struct channel_data *chan)
1200 struct cosa_data *cosa = chan->cosa;
1202 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1203 put_driver_status(cosa);
1206 /* FIXME: This routine probably should check for cosa_start_tx() called when
1207 * the previous transmit is still unfinished. In this case the non-zero
1208 * return value should indicate to the caller that the queuing(sp?) up
1209 * the transmit has failed.
1211 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1213 struct cosa_data *cosa = chan->cosa;
1214 unsigned long flags;
1218 pr_info("cosa%dc%d: starting tx(0x%x)",
1219 chan->cosa->num, chan->num, len);
1220 for (i = 0; i < len; i++)
1221 pr_cont(" %02x", buf[i]&0xff);
1224 spin_lock_irqsave(&cosa->lock, flags);
1228 chan->txsize = COSA_MTU;
1229 spin_unlock_irqrestore(&cosa->lock, flags);
1231 /* Tell the firmware we are ready */
1232 set_bit(chan->num, &cosa->txbitmap);
1233 put_driver_status(cosa);
1238 static void put_driver_status(struct cosa_data *cosa)
1240 unsigned long flags;
1243 spin_lock_irqsave(&cosa->lock, flags);
1245 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1246 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1247 | (cosa->txbitmap ? ~(cosa->txbitmap << DRIVER_TXMAP_SHIFT)
1248 & DRIVER_TXMAP_MASK : 0);
1250 if (cosa->rxbitmap | cosa->txbitmap) {
1251 if (!cosa->enabled) {
1252 cosa_putstatus(cosa, SR_RX_INT_ENA);
1254 debug_status_out(cosa, SR_RX_INT_ENA);
1258 } else if (cosa->enabled) {
1260 cosa_putstatus(cosa, 0);
1262 debug_status_out(cosa, 0);
1265 cosa_putdata8(cosa, status);
1267 debug_data_cmd(cosa, status);
1270 spin_unlock_irqrestore(&cosa->lock, flags);
1273 static void put_driver_status_nolock(struct cosa_data *cosa)
1277 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1278 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1279 | (cosa->txbitmap ? ~(cosa->txbitmap << DRIVER_TXMAP_SHIFT)
1280 & DRIVER_TXMAP_MASK : 0);
1282 if (cosa->rxbitmap | cosa->txbitmap) {
1283 cosa_putstatus(cosa, SR_RX_INT_ENA);
1285 debug_status_out(cosa, SR_RX_INT_ENA);
1289 cosa_putstatus(cosa, 0);
1291 debug_status_out(cosa, 0);
1295 cosa_putdata8(cosa, status);
1297 debug_data_cmd(cosa, status);
1301 /* The "kickme" function: When the DMA times out, this is called to
1302 * clean up the driver status.
1303 * FIXME: Preliminary support, the interface is probably wrong.
1305 static void cosa_kick(struct cosa_data *cosa)
1307 unsigned long flags, flags1;
1308 char *s = "(probably) IRQ";
1310 if (test_bit(RXBIT, &cosa->rxtx))
1312 if (test_bit(TXBIT, &cosa->rxtx))
1315 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1316 spin_lock_irqsave(&cosa->lock, flags);
1319 flags1 = claim_dma_lock();
1320 disable_dma(cosa->dma);
1321 clear_dma_ff(cosa->dma);
1322 release_dma_lock(flags1);
1324 /* FIXME: Anything else? */
1326 cosa_putstatus(cosa, 0);
1328 (void)cosa_getdata8(cosa);
1330 cosa_putdata8(cosa, 0);
1332 put_driver_status_nolock(cosa);
1333 spin_unlock_irqrestore(&cosa->lock, flags);
1336 /* Check if the whole buffer is DMA-able. It means it is below the 16M of
1337 * physical memory and doesn't span the 64k boundary. For now it seems
1338 * SKB's never do this, but we'll check this anyway.
1340 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1343 unsigned long b = (unsigned long)buf;
1345 if (b + len >= MAX_DMA_ADDRESS)
1347 if ((b ^ (b + len)) & 0x10000) {
1349 pr_info("%s: packet spanning a 64k boundary\n",
1356 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1358 /* Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1359 * drivers need to say 4-digit hex number meaning start address of the microcode
1360 * separated by a single space. Monitor replies by saying " =". Now driver
1361 * has to write 4-digit hex number meaning the last byte address ended
1362 * by a single space. Monitor has to reply with a space. Now the download
1363 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1365 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1369 if (put_wait_data(cosa, 'w') == -1)
1371 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1372 if (get_wait_data(cosa) != '=')
1375 if (puthexnumber(cosa, address) < 0)
1377 if (put_wait_data(cosa, ' ') == -1)
1379 if (get_wait_data(cosa) != ' ')
1381 if (get_wait_data(cosa) != '=')
1384 if (puthexnumber(cosa, address + length - 1) < 0)
1386 if (put_wait_data(cosa, ' ') == -1)
1388 if (get_wait_data(cosa) != ' ')
1393 #ifndef SRP_DOWNLOAD_AT_BOOT
1394 if (get_user(c, microcode))
1395 return -23; /* ??? */
1399 if (put_wait_data(cosa, c) == -1)
1404 if (get_wait_data(cosa) != '\r')
1406 if (get_wait_data(cosa) != '\n')
1408 if (get_wait_data(cosa) != '.')
1411 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1416 /* Starting microcode is done via the "g" command of the SRP monitor.
1417 * The chat should be the following: "g" "g=" "<addr><CR>"
1418 * "<CR><CR><LF><CR><LF>".
1420 static int startmicrocode(struct cosa_data *cosa, int address)
1422 if (put_wait_data(cosa, 'g') == -1)
1424 if (get_wait_data(cosa) != 'g')
1426 if (get_wait_data(cosa) != '=')
1429 if (puthexnumber(cosa, address) < 0)
1431 if (put_wait_data(cosa, '\r') == -1)
1434 if (get_wait_data(cosa) != '\r')
1436 if (get_wait_data(cosa) != '\r')
1438 if (get_wait_data(cosa) != '\n')
1440 if (get_wait_data(cosa) != '\r')
1442 if (get_wait_data(cosa) != '\n')
1445 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1450 /* Reading memory is done via the "r" command of the SRP monitor.
1451 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1452 * Then driver can read the data and the conversation is finished
1453 * by SRP monitor sending "<CR><LF>." (dot at the end).
1455 * This routine is not needed during the normal operation and serves
1456 * for debugging purposes only.
1458 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1460 if (put_wait_data(cosa, 'r') == -1)
1462 if ((get_wait_data(cosa)) != 'r')
1464 if ((get_wait_data(cosa)) != '=')
1467 if (puthexnumber(cosa, address) < 0)
1469 if (put_wait_data(cosa, ' ') == -1)
1471 if (get_wait_data(cosa) != ' ')
1473 if (get_wait_data(cosa) != '=')
1476 if (puthexnumber(cosa, address + length - 1) < 0)
1478 if (put_wait_data(cosa, ' ') == -1)
1480 if (get_wait_data(cosa) != ' ')
1487 i = get_wait_data(cosa);
1489 pr_info("0x%04x bytes remaining\n", length);
1494 if (put_user(c, microcode))
1495 return -23; /* ??? */
1502 if (get_wait_data(cosa) != '\r')
1504 if (get_wait_data(cosa) != '\n')
1506 if (get_wait_data(cosa) != '.')
1509 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1514 /* This function resets the device and reads the initial prompt
1515 * of the device's ROM monitor.
1517 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1519 int i = 0, id = 0, prev = 0, curr = 0;
1521 /* Reset the card ... */
1522 cosa_putstatus(cosa, 0);
1523 cosa_getdata8(cosa);
1524 cosa_putstatus(cosa, SR_RST);
1526 /* Disable all IRQs from the card */
1527 cosa_putstatus(cosa, 0);
1529 /* Try to read the ID string. The card then prints out the
1530 * identification string ended by the "\n\x2e".
1532 * The following loop is indexed through i (instead of id)
1533 * to avoid looping forever when for any reason
1534 * the port returns '\r', '\n' or '\x2e' permanently.
1536 for (i = 0; i < COSA_MAX_ID_STRING - 1; i++, prev = curr) {
1537 curr = get_wait_data(cosa);
1542 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1543 idstring[id++] = curr;
1544 if (curr == 0x2e && prev == '\n')
1547 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1548 idstring[id] = '\0';
1552 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1554 /* This routine gets the data byte from the card waiting for the SR_RX_RDY
1555 * bit to be set in a loop. It should be used in the exceptional cases
1556 * only (for example when resetting the card or downloading the firmware.
1558 static int get_wait_data(struct cosa_data *cosa)
1563 /* read data and return them */
1564 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1567 r = cosa_getdata8(cosa);
1569 pr_info("get_wait_data returning after %d retries\n",
1574 /* sleep if not ready to read */
1575 schedule_timeout_interruptible(1);
1577 pr_info("timeout in get_wait_data (status 0x%x)\n",
1578 cosa_getstatus(cosa));
1582 /* This routine puts the data byte to the card waiting for the SR_TX_RDY
1583 * bit to be set in a loop. It should be used in the exceptional cases
1584 * only (for example when resetting the card or downloading the firmware).
1586 static int put_wait_data(struct cosa_data *cosa, int data)
1591 /* read data and return them */
1592 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1593 cosa_putdata8(cosa, data);
1595 pr_info("Putdata: %d retries\n", 999 - retries);
1600 /* sleep if not ready to read */
1601 schedule_timeout_interruptible(1);
1604 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1605 cosa->num, cosa_getstatus(cosa));
1609 /* The following routine puts the hexadecimal number into the SRP monitor
1610 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1611 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1612 * (-2,-4,-6,-8) means that reading echo failed.
1614 static int puthexnumber(struct cosa_data *cosa, int number)
1619 /* Well, I should probably replace this by something faster. */
1620 sprintf(temp, "%04X", number);
1621 for (i = 0; i < 4; i++) {
1622 if (put_wait_data(cosa, temp[i]) == -1) {
1623 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1627 if (get_wait_data(cosa) != temp[i]) {
1628 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1636 /* ---------- Interrupt routines ---------- */
1638 /* There are three types of interrupt:
1639 * At the beginning of transmit - this handled is in tx_interrupt(),
1640 * at the beginning of receive - it is in rx_interrupt() and
1641 * at the end of transmit/receive - it is the eot_interrupt() function.
1642 * These functions are multiplexed by cosa_interrupt() according to the
1643 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1644 * separate functions to make it more readable. These functions are inline,
1645 * so there should be no overhead of function call.
1647 * In the COSA bus-master mode, we need to tell the card the address of a
1648 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1649 * It's time to use the bottom half :-(
1652 /* Transmit interrupt routine - called when COSA is willing to obtain
1653 * data from the OS. The most tricky part of the routine is selection
1654 * of channel we (OS) want to send packet for. For SRP we should probably
1655 * use the round-robin approach. The newer COSA firmwares have a simple
1656 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1657 * channel 0 or 1 doesn't want to receive data.
1659 * It seems there is a bug in COSA firmware (need to trace it further):
1660 * When the driver status says that the kernel has no more data for transmit
1661 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1662 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1663 * the TX interrupt but does not mark the channel as ready-to-transmit.
1664 * The fix seems to be to push the packet to COSA despite its request.
1665 * We first try to obey the card's opinion, and then fall back to forced TX.
1667 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1669 unsigned long flags, flags1;
1671 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1673 spin_lock_irqsave(&cosa->lock, flags);
1674 set_bit(TXBIT, &cosa->rxtx);
1675 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1676 /* flow control, see the comment above */
1679 if (!cosa->txbitmap) {
1680 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1682 put_driver_status_nolock(cosa);
1683 clear_bit(TXBIT, &cosa->rxtx);
1684 spin_unlock_irqrestore(&cosa->lock, flags);
1690 if (cosa->txchan >= cosa->nchannels)
1692 if (!(cosa->txbitmap & (1 << cosa->txchan)))
1695 (1 << (cosa->txchan + DRIVER_TXMAP_SHIFT)))
1697 /* in second pass, accept first ready-to-TX channel */
1698 if (i > cosa->nchannels) {
1699 /* Can be safely ignored */
1701 printk(KERN_DEBUG "%s: Forcing TX "
1702 "to not-ready channel %d\n",
1703 cosa->name, cosa->txchan);
1709 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1710 if (cosa_dma_able(cosa->chan + cosa->txchan,
1711 cosa->chan[cosa->txchan].txbuf,
1713 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1715 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1717 cosa->txbuf = cosa->bouncebuf;
1721 if (is_8bit(cosa)) {
1722 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1723 cosa_putstatus(cosa, SR_TX_INT_ENA);
1724 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0) |
1725 ((cosa->txsize >> 8) & 0x1f));
1727 debug_status_out(cosa, SR_TX_INT_ENA);
1728 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0) |
1729 ((cosa->txsize >> 8) & 0x1f));
1730 debug_data_in(cosa, cosa_getdata8(cosa));
1732 cosa_getdata8(cosa);
1734 set_bit(IRQBIT, &cosa->rxtx);
1735 spin_unlock_irqrestore(&cosa->lock, flags);
1738 clear_bit(IRQBIT, &cosa->rxtx);
1739 cosa_putstatus(cosa, 0);
1740 cosa_putdata8(cosa, cosa->txsize & 0xff);
1742 debug_status_out(cosa, 0);
1743 debug_data_out(cosa, cosa->txsize & 0xff);
1747 cosa_putstatus(cosa, SR_TX_INT_ENA);
1748 cosa_putdata16(cosa, ((cosa->txchan << 13) & 0xe000)
1749 | (cosa->txsize & 0x1fff));
1751 debug_status_out(cosa, SR_TX_INT_ENA);
1752 debug_data_out(cosa, ((cosa->txchan << 13) & 0xe000) |
1753 (cosa->txsize & 0x1fff));
1754 debug_data_in(cosa, cosa_getdata8(cosa));
1755 debug_status_out(cosa, 0);
1757 cosa_getdata8(cosa);
1759 cosa_putstatus(cosa, 0);
1762 if (cosa->busmaster) {
1763 unsigned long addr = virt_to_bus(cosa->txbuf);
1766 pr_info("busmaster IRQ\n");
1767 while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1773 pr_info("status %x\n", cosa_getstatus(cosa));
1774 pr_info("ready after %d loops\n", count);
1775 cosa_putdata16(cosa, (addr >> 16) & 0xffff);
1778 while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1784 pr_info("ready after %d loops\n", count);
1785 cosa_putdata16(cosa, addr & 0xffff);
1786 flags1 = claim_dma_lock();
1787 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1788 enable_dma(cosa->dma);
1789 release_dma_lock(flags1);
1792 flags1 = claim_dma_lock();
1793 disable_dma(cosa->dma);
1794 clear_dma_ff(cosa->dma);
1795 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1796 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1797 set_dma_count(cosa->dma, cosa->txsize);
1798 enable_dma(cosa->dma);
1799 release_dma_lock(flags1);
1801 cosa_putstatus(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1803 debug_status_out(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1805 spin_unlock_irqrestore(&cosa->lock, flags);
1808 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1810 unsigned long flags;
1812 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1815 spin_lock_irqsave(&cosa->lock, flags);
1816 set_bit(RXBIT, &cosa->rxtx);
1818 if (is_8bit(cosa)) {
1819 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1820 set_bit(IRQBIT, &cosa->rxtx);
1821 put_driver_status_nolock(cosa);
1822 cosa->rxsize = cosa_getdata8(cosa) << 8;
1824 debug_data_in(cosa, cosa->rxsize >> 8);
1826 spin_unlock_irqrestore(&cosa->lock, flags);
1829 clear_bit(IRQBIT, &cosa->rxtx);
1830 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1832 debug_data_in(cosa, cosa->rxsize & 0xff);
1835 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1836 cosa->num, cosa->rxsize);
1840 cosa->rxsize = cosa_getdata16(cosa);
1842 debug_data_in(cosa, cosa->rxsize);
1845 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1846 cosa->num, cosa->rxsize);
1849 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1850 pr_warn("%s: rx for unknown channel (0x%04x)\n",
1851 cosa->name, cosa->rxsize);
1852 spin_unlock_irqrestore(&cosa->lock, flags);
1855 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1856 cosa->rxsize &= 0x1fff;
1857 spin_unlock_irqrestore(&cosa->lock, flags);
1860 if (cosa->rxchan->setup_rx)
1861 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1864 reject: /* Reject the packet */
1865 pr_info("cosa%d: rejecting packet on channel %d\n",
1866 cosa->num, cosa->rxchan->num);
1867 cosa->rxbuf = cosa->bouncebuf;
1871 flags = claim_dma_lock();
1872 disable_dma(cosa->dma);
1873 clear_dma_ff(cosa->dma);
1874 set_dma_mode(cosa->dma, DMA_MODE_READ);
1875 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff))
1876 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1878 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1880 set_dma_count(cosa->dma, (cosa->rxsize & 0x1fff));
1881 enable_dma(cosa->dma);
1882 release_dma_lock(flags);
1883 spin_lock_irqsave(&cosa->lock, flags);
1884 cosa_putstatus(cosa, SR_RX_DMA_ENA | SR_USR_INT_ENA);
1885 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1886 cosa_putdata8(cosa, DRIVER_RX_READY);
1888 debug_status_out(cosa, SR_RX_DMA_ENA | SR_USR_INT_ENA);
1889 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1890 debug_data_cmd(cosa, DRIVER_RX_READY);
1892 spin_unlock_irqrestore(&cosa->lock, flags);
1895 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1897 unsigned long flags, flags1;
1899 spin_lock_irqsave(&cosa->lock, flags);
1900 flags1 = claim_dma_lock();
1901 disable_dma(cosa->dma);
1902 clear_dma_ff(cosa->dma);
1903 release_dma_lock(flags1);
1904 if (test_bit(TXBIT, &cosa->rxtx)) {
1905 struct channel_data *chan = cosa->chan + cosa->txchan;
1908 if (chan->tx_done(chan, cosa->txsize))
1909 clear_bit(chan->num, &cosa->txbitmap);
1910 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1915 pr_info("cosa%dc%d: done rx(0x%x)",
1916 cosa->num, cosa->rxchan->num, cosa->rxsize);
1917 for (i = 0; i < cosa->rxsize; i++)
1918 pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1922 /* Packet for unknown channel? */
1923 if (cosa->rxbuf == cosa->bouncebuf)
1925 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1926 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1927 if (cosa->rxchan->rx_done)
1928 if (cosa->rxchan->rx_done(cosa->rxchan))
1929 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1931 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1933 /* Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1934 * cleared anyway). We should do it as soon as possible
1935 * so that we can tell the COSA we are done and to give it a time
1940 put_driver_status_nolock(cosa);
1941 spin_unlock_irqrestore(&cosa->lock, flags);
1944 static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1948 struct cosa_data *cosa = cosa_;
1950 status = cosa_getstatus(cosa);
1952 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1955 debug_status_in(cosa, status);
1957 switch (status & SR_CMD_FROM_SRP_MASK) {
1958 case SR_DOWN_REQUEST:
1959 tx_interrupt(cosa, status);
1962 rx_interrupt(cosa, status);
1964 case SR_END_OF_TRANSFER:
1965 eot_interrupt(cosa, status);
1968 /* We may be too fast for SRP. Try to wait a bit more. */
1969 if (count++ < 100) {
1973 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1974 cosa->num, status & 0xff, count);
1978 pr_info("%s: %d-times got unknown status in IRQ\n",
1981 pr_info("%s: returning from IRQ\n", cosa->name);
1986 /* ---------- I/O debugging routines ---------- */
1987 /* These routines can be used to monitor COSA/SRP I/O and to printk()
1988 * the data being transferred on the data and status I/O port in a
1993 static void debug_status_in(struct cosa_data *cosa, int status)
1997 switch (status & SR_CMD_FROM_SRP_MASK) {
2001 case SR_DOWN_REQUEST:
2004 case SR_END_OF_TRANSFER:
2011 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2014 status & SR_USR_RQ ? "USR_RQ|" : "",
2015 status & SR_TX_RDY ? "TX_RDY|" : "",
2016 status & SR_RX_RDY ? "RX_RDY|" : "",
2020 static void debug_status_out(struct cosa_data *cosa, int status)
2022 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2025 status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2026 status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2027 status & SR_RST ? "RESET|" : "",
2028 status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2029 status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2030 status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
2033 static void debug_data_in(struct cosa_data *cosa, int data)
2035 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2038 static void debug_data_out(struct cosa_data *cosa, int data)
2040 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2043 static void debug_data_cmd(struct cosa_data *cosa, int data)
2045 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2047 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2048 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2052 /* EOF -- this file has not been truncated */