1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
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
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).
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");
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);
270 static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
272 /* Character device */
273 static char *chrdev_setup_rx(struct channel_data *channel, int size);
274 static int chrdev_rx_done(struct channel_data *channel);
275 static int chrdev_tx_done(struct channel_data *channel, int size);
276 static ssize_t cosa_read(struct file *file,
277 char __user *buf, size_t count, loff_t *ppos);
278 static ssize_t cosa_write(struct file *file,
279 const char __user *buf, size_t count, loff_t *ppos);
280 static unsigned int cosa_poll(struct file *file, poll_table *poll);
281 static int cosa_open(struct inode *inode, struct file *file);
282 static int cosa_release(struct inode *inode, struct file *file);
283 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
285 #ifdef COSA_FASYNC_WORKING
286 static int cosa_fasync(struct inode *inode, struct file *file, int on);
289 static const struct file_operations cosa_fops = {
290 .owner = THIS_MODULE,
295 .unlocked_ioctl = cosa_chardev_ioctl,
297 .release = cosa_release,
298 #ifdef COSA_FASYNC_WORKING
299 .fasync = cosa_fasync,
304 static int cosa_start(struct cosa_data *cosa, int address);
305 static int cosa_reset(struct cosa_data *cosa);
306 static int cosa_download(struct cosa_data *cosa, void __user *a);
307 static int cosa_readmem(struct cosa_data *cosa, void __user *a);
309 /* COSA/SRP ROM monitor */
310 static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
311 static int startmicrocode(struct cosa_data *cosa, int address);
312 static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
313 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
315 /* Auxiliary functions */
316 static int get_wait_data(struct cosa_data *cosa);
317 static int put_wait_data(struct cosa_data *cosa, int data);
318 static int puthexnumber(struct cosa_data *cosa, int number);
319 static void put_driver_status(struct cosa_data *cosa);
320 static void put_driver_status_nolock(struct cosa_data *cosa);
322 /* Interrupt handling */
323 static irqreturn_t cosa_interrupt(int irq, void *cosa);
325 /* I/O ops debugging */
327 static void debug_data_in(struct cosa_data *cosa, int data);
328 static void debug_data_out(struct cosa_data *cosa, int data);
329 static void debug_data_cmd(struct cosa_data *cosa, int data);
330 static void debug_status_in(struct cosa_data *cosa, int status);
331 static void debug_status_out(struct cosa_data *cosa, int status);
334 static inline struct channel_data *dev_to_chan(struct net_device *dev)
336 return (struct channel_data *)dev_to_hdlc(dev)->priv;
339 /* ---------- Initialization stuff ---------- */
341 static int __init cosa_init(void)
345 if (cosa_major > 0) {
346 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
347 pr_warn("unable to get major %d\n", cosa_major);
352 cosa_major = register_chrdev(0, "cosa", &cosa_fops);
354 pr_warn("unable to register chardev\n");
359 for (i = 0; i < MAX_CARDS; i++)
360 cosa_cards[i].num = -1;
361 for (i = 0; io[i] != 0 && i < MAX_CARDS; i++)
362 cosa_probe(io[i], irq[i], dma[i]);
364 pr_warn("no devices found\n");
365 unregister_chrdev(cosa_major, "cosa");
369 cosa_class = class_create(THIS_MODULE, "cosa");
370 if (IS_ERR(cosa_class)) {
371 err = PTR_ERR(cosa_class);
374 for (i = 0; i < nr_cards; i++)
375 device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
381 unregister_chrdev(cosa_major, "cosa");
385 module_init(cosa_init);
387 static void __exit cosa_exit(void)
389 struct cosa_data *cosa;
392 for (i = 0; i < nr_cards; i++)
393 device_destroy(cosa_class, MKDEV(cosa_major, i));
394 class_destroy(cosa_class);
396 for (cosa = cosa_cards; nr_cards--; cosa++) {
397 /* Clean up the per-channel data */
398 for (i = 0; i < cosa->nchannels; i++) {
399 /* Chardev driver has no alloc'd per-channel data */
400 unregister_hdlc_device(cosa->chan[i].netdev);
401 free_netdev(cosa->chan[i].netdev);
403 /* Clean up the per-card data */
405 kfree(cosa->bouncebuf);
406 free_irq(cosa->irq, cosa);
408 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
410 unregister_chrdev(cosa_major, "cosa");
412 module_exit(cosa_exit);
414 static const struct net_device_ops cosa_ops = {
415 .ndo_open = cosa_net_open,
416 .ndo_stop = cosa_net_close,
417 .ndo_start_xmit = hdlc_start_xmit,
418 .ndo_do_ioctl = cosa_net_ioctl,
419 .ndo_tx_timeout = cosa_net_timeout,
422 static int cosa_probe(int base, int irq, int dma)
424 struct cosa_data *cosa = cosa_cards + nr_cards;
427 memset(cosa, 0, sizeof(struct cosa_data));
429 /* Checking validity of parameters: */
430 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
431 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
432 pr_info("invalid IRQ %d\n", irq);
435 /* I/O address should be between 0x100 and 0x3ff and should be
438 if (base < 0x100 || base > 0x3ff || base & 0x7) {
439 pr_info("invalid I/O address 0x%x\n", base);
442 /* DMA should be 0,1 or 3-7 */
443 if (dma < 0 || dma == 4 || dma > 7) {
444 pr_info("invalid DMA %d\n", dma);
447 /* and finally, on 16-bit COSA DMA should be 4-7 and
448 * I/O base should not be multiple of 0x10
450 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
451 pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
457 cosa->datareg = base;
458 cosa->statusreg = is_8bit(cosa) ? base + 1 : base + 2;
459 spin_lock_init(&cosa->lock);
461 if (!request_region(base, is_8bit(cosa) ? 2 : 4, "cosa"))
464 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
465 printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
470 /* Test the validity of identification string */
471 if (!strncmp(cosa->id_string, "SRP", 3)) {
473 } else if (!strncmp(cosa->id_string, "COSA", 4)) {
474 cosa->type = is_8bit(cosa) ? "cosa8" : "cosa16";
476 /* Print a warning only if we are not autoprobing */
477 #ifndef COSA_ISA_AUTOPROBE
478 pr_info("valid signature not found at 0x%x\n", base);
483 /* Update the name of the region now we know the type of card */
484 release_region(base, is_8bit(cosa) ? 2 : 4);
485 if (!request_region(base, is_8bit(cosa) ? 2 : 4, cosa->type)) {
486 printk(KERN_DEBUG "changing name at 0x%x failed.\n", base);
490 /* Now do IRQ autoprobe */
493 /* pr_info("IRQ autoprobe\n"); */
494 irqs = probe_irq_on();
495 /* Enable interrupt on tx buffer empty (it sure is)
497 * FIXME: When this code is not used as module, we should
498 * probably call udelay() instead of the interruptible sleep.
500 set_current_state(TASK_INTERRUPTIBLE);
501 cosa_putstatus(cosa, SR_TX_INT_ENA);
502 schedule_timeout(msecs_to_jiffies(300));
503 irq = probe_irq_off(irqs);
504 /* Disable all IRQs from the card */
505 cosa_putstatus(cosa, 0);
506 /* Empty the received data register */
510 pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
516 pr_info("no interrupt obtained (board at 0x%x)\n",
523 cosa->num = nr_cards;
525 cosa->nchannels = 2; /* FIXME: how to determine this? */
527 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
531 if (request_dma(cosa->dma, cosa->type)) {
536 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL | GFP_DMA);
537 if (!cosa->bouncebuf) {
541 sprintf(cosa->name, "cosa%d", cosa->num);
543 /* Initialize the per-channel data */
544 cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
550 for (i = 0; i < cosa->nchannels; i++) {
551 struct channel_data *chan = &cosa->chan[i];
555 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
557 /* Initialize the chardev data structures */
558 mutex_init(&chan->rlock);
559 sema_init(&chan->wsem, 1);
561 /* Register the network interface */
562 chan->netdev = alloc_hdlcdev(chan);
564 pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
568 dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
569 dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
570 chan->netdev->netdev_ops = &cosa_ops;
571 chan->netdev->watchdog_timeo = TX_TIMEOUT;
572 chan->netdev->base_addr = chan->cosa->datareg;
573 chan->netdev->irq = chan->cosa->irq;
574 chan->netdev->dma = chan->cosa->dma;
575 err = register_hdlc_device(chan->netdev);
577 netdev_warn(chan->netdev,
578 "register_hdlc_device() failed\n");
579 free_netdev(chan->netdev);
584 pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
585 cosa->num, cosa->id_string, cosa->type,
586 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
592 unregister_hdlc_device(cosa->chan[i].netdev);
593 free_netdev(cosa->chan[i].netdev);
597 kfree(cosa->bouncebuf);
601 free_irq(cosa->irq, cosa);
603 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
604 pr_notice("cosa%d: allocating resources failed\n", cosa->num);
608 /*---------- network device ---------- */
610 static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
611 unsigned short parity)
613 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
618 static int cosa_net_open(struct net_device *dev)
620 struct channel_data *chan = dev_to_chan(dev);
624 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
625 pr_notice("%s: start the firmware first (status %d)\n",
626 chan->cosa->name, chan->cosa->firmware_status);
629 spin_lock_irqsave(&chan->cosa->lock, flags);
630 if (chan->usage != 0) {
631 pr_warn("%s: cosa_net_open called with usage count %d\n",
632 chan->name, chan->usage);
633 spin_unlock_irqrestore(&chan->cosa->lock, flags);
636 chan->setup_rx = cosa_net_setup_rx;
637 chan->tx_done = cosa_net_tx_done;
638 chan->rx_done = cosa_net_rx_done;
641 spin_unlock_irqrestore(&chan->cosa->lock, flags);
643 err = hdlc_open(dev);
645 spin_lock_irqsave(&chan->cosa->lock, flags);
648 spin_unlock_irqrestore(&chan->cosa->lock, flags);
652 netif_start_queue(dev);
653 cosa_enable_rx(chan);
657 static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
658 struct net_device *dev)
660 struct channel_data *chan = dev_to_chan(dev);
662 netif_stop_queue(dev);
665 cosa_start_tx(chan, skb->data, skb->len);
669 static void cosa_net_timeout(struct net_device *dev, unsigned int txqueue)
671 struct channel_data *chan = dev_to_chan(dev);
673 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
674 chan->netdev->stats.rx_errors++;
675 chan->netdev->stats.rx_missed_errors++;
677 chan->netdev->stats.tx_errors++;
678 chan->netdev->stats.tx_aborted_errors++;
680 cosa_kick(chan->cosa);
682 dev_kfree_skb(chan->tx_skb);
685 netif_wake_queue(dev);
688 static int cosa_net_close(struct net_device *dev)
690 struct channel_data *chan = dev_to_chan(dev);
693 netif_stop_queue(dev);
695 cosa_disable_rx(chan);
696 spin_lock_irqsave(&chan->cosa->lock, flags);
698 kfree_skb(chan->rx_skb);
702 kfree_skb(chan->tx_skb);
707 spin_unlock_irqrestore(&chan->cosa->lock, flags);
711 static char *cosa_net_setup_rx(struct channel_data *chan, int size)
713 /* We can safely fall back to non-dma-able memory, because we have
714 * the cosa->bouncebuf pre-allocated.
716 kfree_skb(chan->rx_skb);
717 chan->rx_skb = dev_alloc_skb(size);
719 pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
720 chan->netdev->stats.rx_dropped++;
723 netif_trans_update(chan->netdev);
724 return skb_put(chan->rx_skb, size);
727 static int cosa_net_rx_done(struct channel_data *chan)
730 pr_warn("%s: rx_done with empty skb!\n", chan->name);
731 chan->netdev->stats.rx_errors++;
732 chan->netdev->stats.rx_frame_errors++;
735 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
736 chan->rx_skb->dev = chan->netdev;
737 skb_reset_mac_header(chan->rx_skb);
738 chan->netdev->stats.rx_packets++;
739 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
740 netif_rx(chan->rx_skb);
746 static int cosa_net_tx_done(struct channel_data *chan, int size)
749 pr_warn("%s: tx_done with empty skb!\n", chan->name);
750 chan->netdev->stats.tx_errors++;
751 chan->netdev->stats.tx_aborted_errors++;
754 dev_consume_skb_irq(chan->tx_skb);
756 chan->netdev->stats.tx_packets++;
757 chan->netdev->stats.tx_bytes += size;
758 netif_wake_queue(chan->netdev);
762 /*---------- Character device ---------- */
764 static ssize_t cosa_read(struct file *file,
765 char __user *buf, size_t count, loff_t *ppos)
767 DECLARE_WAITQUEUE(wait, current);
769 struct channel_data *chan = file->private_data;
770 struct cosa_data *cosa = chan->cosa;
773 if (!(cosa->firmware_status & COSA_FW_START)) {
774 pr_notice("%s: start the firmware first (status %d)\n",
775 cosa->name, cosa->firmware_status);
778 if (mutex_lock_interruptible(&chan->rlock))
781 chan->rxdata = kmalloc(COSA_MTU, GFP_DMA | GFP_KERNEL);
783 mutex_unlock(&chan->rlock);
788 cosa_enable_rx(chan);
789 spin_lock_irqsave(&cosa->lock, flags);
790 add_wait_queue(&chan->rxwaitq, &wait);
791 while (!chan->rx_status) {
792 set_current_state(TASK_INTERRUPTIBLE);
793 spin_unlock_irqrestore(&cosa->lock, flags);
795 spin_lock_irqsave(&cosa->lock, flags);
796 if (signal_pending(current) && chan->rx_status == 0) {
798 remove_wait_queue(&chan->rxwaitq, &wait);
799 __set_current_state(TASK_RUNNING);
800 spin_unlock_irqrestore(&cosa->lock, flags);
801 mutex_unlock(&chan->rlock);
805 remove_wait_queue(&chan->rxwaitq, &wait);
806 __set_current_state(TASK_RUNNING);
808 count = chan->rxsize;
809 spin_unlock_irqrestore(&cosa->lock, flags);
810 mutex_unlock(&chan->rlock);
812 if (copy_to_user(buf, kbuf, count)) {
820 static char *chrdev_setup_rx(struct channel_data *chan, int size)
822 /* Expect size <= COSA_MTU */
827 static int chrdev_rx_done(struct channel_data *chan)
829 if (chan->rx_status) { /* Reader has died */
834 wake_up_interruptible(&chan->rxwaitq);
838 static ssize_t cosa_write(struct file *file,
839 const char __user *buf, size_t count, loff_t *ppos)
841 DECLARE_WAITQUEUE(wait, current);
842 struct channel_data *chan = file->private_data;
843 struct cosa_data *cosa = chan->cosa;
847 if (!(cosa->firmware_status & COSA_FW_START)) {
848 pr_notice("%s: start the firmware first (status %d)\n",
849 cosa->name, cosa->firmware_status);
852 if (down_interruptible(&chan->wsem))
855 if (count > COSA_MTU)
858 /* Allocate the buffer */
859 kbuf = kmalloc(count, GFP_KERNEL | GFP_DMA);
864 if (copy_from_user(kbuf, buf, count)) {
870 cosa_start_tx(chan, kbuf, count);
872 spin_lock_irqsave(&cosa->lock, flags);
873 add_wait_queue(&chan->txwaitq, &wait);
874 while (!chan->tx_status) {
875 set_current_state(TASK_INTERRUPTIBLE);
876 spin_unlock_irqrestore(&cosa->lock, flags);
878 spin_lock_irqsave(&cosa->lock, flags);
879 if (signal_pending(current) && chan->tx_status == 0) {
881 remove_wait_queue(&chan->txwaitq, &wait);
882 __set_current_state(TASK_RUNNING);
884 spin_unlock_irqrestore(&cosa->lock, flags);
890 remove_wait_queue(&chan->txwaitq, &wait);
891 __set_current_state(TASK_RUNNING);
893 spin_unlock_irqrestore(&cosa->lock, flags);
898 static int chrdev_tx_done(struct channel_data *chan, int size)
900 if (chan->tx_status) { /* Writer was interrupted */
905 wake_up_interruptible(&chan->txwaitq);
909 static __poll_t cosa_poll(struct file *file, poll_table *poll)
911 pr_info("cosa_poll is here\n");
915 static int cosa_open(struct inode *inode, struct file *file)
917 struct cosa_data *cosa;
918 struct channel_data *chan;
923 mutex_lock(&cosa_chardev_mutex);
924 n = iminor(file_inode(file)) >> CARD_MINOR_BITS;
929 cosa = cosa_cards + n;
931 n = iminor(file_inode(file)) & ((1 << CARD_MINOR_BITS) - 1);
932 if (n >= cosa->nchannels) {
936 chan = cosa->chan + n;
938 file->private_data = chan;
940 spin_lock_irqsave(&cosa->lock, flags);
942 if (chan->usage < 0) { /* in netdev mode */
943 spin_unlock_irqrestore(&cosa->lock, flags);
950 chan->tx_done = chrdev_tx_done;
951 chan->setup_rx = chrdev_setup_rx;
952 chan->rx_done = chrdev_rx_done;
953 spin_unlock_irqrestore(&cosa->lock, flags);
955 mutex_unlock(&cosa_chardev_mutex);
959 static int cosa_release(struct inode *inode, struct file *file)
961 struct channel_data *channel = file->private_data;
962 struct cosa_data *cosa;
965 cosa = channel->cosa;
966 spin_lock_irqsave(&cosa->lock, flags);
969 spin_unlock_irqrestore(&cosa->lock, flags);
973 #ifdef COSA_FASYNC_WORKING
974 static struct fasync_struct *fasync[256] = { NULL, };
977 static int cosa_fasync(struct inode *inode, struct file *file, int on)
979 int port = iminor(inode);
981 return fasync_helper(inode, file, on, &fasync[port]);
985 /* ---------- Ioctls ---------- */
987 /* Ioctl subroutines can safely be made inline, because they are called
988 * only from cosa_ioctl().
990 static inline int cosa_reset(struct cosa_data *cosa)
992 char idstring[COSA_MAX_ID_STRING];
995 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
996 cosa->num, cosa->usage);
997 cosa->firmware_status &= ~(COSA_FW_RESET | COSA_FW_START);
998 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
999 pr_notice("cosa%d: reset failed\n", cosa->num);
1002 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1003 cosa->firmware_status |= COSA_FW_RESET;
1007 /* High-level function to download data into COSA memory. Calls download() */
1008 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1010 struct cosa_download d;
1013 if (cosa->usage > 1)
1014 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1015 cosa->name, cosa->usage);
1016 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1017 pr_notice("%s: reset the card first (status %d)\n",
1018 cosa->name, cosa->firmware_status);
1022 if (copy_from_user(&d, arg, sizeof(d)))
1025 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1027 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1030 /* If something fails, force the user to reset the card */
1031 cosa->firmware_status &= ~(COSA_FW_RESET | COSA_FW_DOWNLOAD);
1033 i = download(cosa, d.code, d.len, d.addr);
1035 pr_notice("cosa%d: microcode download failed: %d\n",
1039 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1040 cosa->num, d.len, d.addr);
1041 cosa->firmware_status |= COSA_FW_RESET | COSA_FW_DOWNLOAD;
1045 /* High-level function to read COSA memory. Calls readmem() */
1046 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1048 struct cosa_download d;
1051 if (cosa->usage > 1)
1052 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1053 cosa->num, cosa->usage);
1054 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1055 pr_notice("%s: reset the card first (status %d)\n",
1056 cosa->name, cosa->firmware_status);
1060 if (copy_from_user(&d, arg, sizeof(d)))
1063 /* If something fails, force the user to reset the card */
1064 cosa->firmware_status &= ~COSA_FW_RESET;
1066 i = readmem(cosa, d.code, d.len, d.addr);
1068 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1071 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1072 cosa->num, d.len, d.addr);
1073 cosa->firmware_status |= COSA_FW_RESET;
1077 /* High-level function to start microcode. Calls startmicrocode(). */
1078 static inline int cosa_start(struct cosa_data *cosa, int address)
1082 if (cosa->usage > 1)
1083 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1084 cosa->num, cosa->usage);
1086 if ((cosa->firmware_status & (COSA_FW_RESET | COSA_FW_DOWNLOAD))
1087 != (COSA_FW_RESET | COSA_FW_DOWNLOAD)) {
1088 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1089 cosa->name, cosa->firmware_status);
1092 cosa->firmware_status &= ~COSA_FW_RESET;
1093 i = startmicrocode(cosa, address);
1095 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1096 cosa->num, address, i);
1099 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1100 cosa->startaddr = address;
1101 cosa->firmware_status |= COSA_FW_START;
1105 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1106 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1108 int l = strlen(cosa->id_string) + 1;
1110 if (copy_to_user(string, cosa->id_string, l))
1115 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1116 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1118 int l = strlen(cosa->type) + 1;
1120 if (copy_to_user(string, cosa->type, l))
1125 static int cosa_ioctl_common(struct cosa_data *cosa,
1126 struct channel_data *channel, unsigned int cmd,
1129 void __user *argp = (void __user *)arg;
1132 case COSAIORSET: /* Reset the device */
1133 if (!capable(CAP_NET_ADMIN))
1135 return cosa_reset(cosa);
1136 case COSAIOSTRT: /* Start the firmware */
1137 if (!capable(CAP_SYS_RAWIO))
1139 return cosa_start(cosa, arg);
1140 case COSAIODOWNLD: /* Download the firmware */
1141 if (!capable(CAP_SYS_RAWIO))
1144 return cosa_download(cosa, argp);
1146 if (!capable(CAP_SYS_RAWIO))
1148 return cosa_readmem(cosa, argp);
1150 return cosa_gettype(cosa, argp);
1152 return cosa_getidstr(cosa, argp);
1156 return cosa->nchannels;
1158 if (!capable(CAP_SYS_RAWIO))
1162 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1164 cosa->busmaster = arg;
1167 return cosa->busmaster;
1169 return -ENOIOCTLCMD;
1172 static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1175 struct channel_data *chan = dev_to_chan(dev);
1177 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1178 (unsigned long)ifr->ifr_data);
1179 if (rv != -ENOIOCTLCMD)
1181 return hdlc_ioctl(dev, ifr, cmd);
1184 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1187 struct channel_data *channel = file->private_data;
1188 struct cosa_data *cosa;
1191 mutex_lock(&cosa_chardev_mutex);
1192 cosa = channel->cosa;
1193 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1194 mutex_unlock(&cosa_chardev_mutex);
1198 /*---------- HW layer interface ---------- */
1200 /* The higher layer can bind itself to the HW layer by setting the callbacks
1201 * in the channel_data structure and by using these routines.
1203 static void cosa_enable_rx(struct channel_data *chan)
1205 struct cosa_data *cosa = chan->cosa;
1207 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1208 put_driver_status(cosa);
1211 static void cosa_disable_rx(struct channel_data *chan)
1213 struct cosa_data *cosa = chan->cosa;
1215 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1216 put_driver_status(cosa);
1219 /* FIXME: This routine probably should check for cosa_start_tx() called when
1220 * the previous transmit is still unfinished. In this case the non-zero
1221 * return value should indicate to the caller that the queuing(sp?) up
1222 * the transmit has failed.
1224 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1226 struct cosa_data *cosa = chan->cosa;
1227 unsigned long flags;
1231 pr_info("cosa%dc%d: starting tx(0x%x)",
1232 chan->cosa->num, chan->num, len);
1233 for (i = 0; i < len; i++)
1234 pr_cont(" %02x", buf[i]&0xff);
1237 spin_lock_irqsave(&cosa->lock, flags);
1241 chan->txsize = COSA_MTU;
1242 spin_unlock_irqrestore(&cosa->lock, flags);
1244 /* Tell the firmware we are ready */
1245 set_bit(chan->num, &cosa->txbitmap);
1246 put_driver_status(cosa);
1251 static void put_driver_status(struct cosa_data *cosa)
1253 unsigned long flags;
1256 spin_lock_irqsave(&cosa->lock, flags);
1258 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1259 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1260 | (cosa->txbitmap ? ~(cosa->txbitmap << DRIVER_TXMAP_SHIFT)
1261 & DRIVER_TXMAP_MASK : 0);
1263 if (cosa->rxbitmap | cosa->txbitmap) {
1264 if (!cosa->enabled) {
1265 cosa_putstatus(cosa, SR_RX_INT_ENA);
1267 debug_status_out(cosa, SR_RX_INT_ENA);
1271 } else if (cosa->enabled) {
1273 cosa_putstatus(cosa, 0);
1275 debug_status_out(cosa, 0);
1278 cosa_putdata8(cosa, status);
1280 debug_data_cmd(cosa, status);
1283 spin_unlock_irqrestore(&cosa->lock, flags);
1286 static void put_driver_status_nolock(struct cosa_data *cosa)
1290 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1291 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1292 | (cosa->txbitmap ? ~(cosa->txbitmap << DRIVER_TXMAP_SHIFT)
1293 & DRIVER_TXMAP_MASK : 0);
1295 if (cosa->rxbitmap | cosa->txbitmap) {
1296 cosa_putstatus(cosa, SR_RX_INT_ENA);
1298 debug_status_out(cosa, SR_RX_INT_ENA);
1302 cosa_putstatus(cosa, 0);
1304 debug_status_out(cosa, 0);
1308 cosa_putdata8(cosa, status);
1310 debug_data_cmd(cosa, status);
1314 /* The "kickme" function: When the DMA times out, this is called to
1315 * clean up the driver status.
1316 * FIXME: Preliminary support, the interface is probably wrong.
1318 static void cosa_kick(struct cosa_data *cosa)
1320 unsigned long flags, flags1;
1321 char *s = "(probably) IRQ";
1323 if (test_bit(RXBIT, &cosa->rxtx))
1325 if (test_bit(TXBIT, &cosa->rxtx))
1328 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1329 spin_lock_irqsave(&cosa->lock, flags);
1332 flags1 = claim_dma_lock();
1333 disable_dma(cosa->dma);
1334 clear_dma_ff(cosa->dma);
1335 release_dma_lock(flags1);
1337 /* FIXME: Anything else? */
1339 cosa_putstatus(cosa, 0);
1341 (void)cosa_getdata8(cosa);
1343 cosa_putdata8(cosa, 0);
1345 put_driver_status_nolock(cosa);
1346 spin_unlock_irqrestore(&cosa->lock, flags);
1349 /* Check if the whole buffer is DMA-able. It means it is below the 16M of
1350 * physical memory and doesn't span the 64k boundary. For now it seems
1351 * SKB's never do this, but we'll check this anyway.
1353 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1356 unsigned long b = (unsigned long)buf;
1358 if (b + len >= MAX_DMA_ADDRESS)
1360 if ((b ^ (b + len)) & 0x10000) {
1362 pr_info("%s: packet spanning a 64k boundary\n",
1369 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1371 /* Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1372 * drivers need to say 4-digit hex number meaning start address of the microcode
1373 * separated by a single space. Monitor replies by saying " =". Now driver
1374 * has to write 4-digit hex number meaning the last byte address ended
1375 * by a single space. Monitor has to reply with a space. Now the download
1376 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1378 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1382 if (put_wait_data(cosa, 'w') == -1)
1384 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1385 if (get_wait_data(cosa) != '=')
1388 if (puthexnumber(cosa, address) < 0)
1390 if (put_wait_data(cosa, ' ') == -1)
1392 if (get_wait_data(cosa) != ' ')
1394 if (get_wait_data(cosa) != '=')
1397 if (puthexnumber(cosa, address + length - 1) < 0)
1399 if (put_wait_data(cosa, ' ') == -1)
1401 if (get_wait_data(cosa) != ' ')
1406 #ifndef SRP_DOWNLOAD_AT_BOOT
1407 if (get_user(c, microcode))
1408 return -23; /* ??? */
1412 if (put_wait_data(cosa, c) == -1)
1417 if (get_wait_data(cosa) != '\r')
1419 if (get_wait_data(cosa) != '\n')
1421 if (get_wait_data(cosa) != '.')
1424 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1429 /* Starting microcode is done via the "g" command of the SRP monitor.
1430 * The chat should be the following: "g" "g=" "<addr><CR>"
1431 * "<CR><CR><LF><CR><LF>".
1433 static int startmicrocode(struct cosa_data *cosa, int address)
1435 if (put_wait_data(cosa, 'g') == -1)
1437 if (get_wait_data(cosa) != 'g')
1439 if (get_wait_data(cosa) != '=')
1442 if (puthexnumber(cosa, address) < 0)
1444 if (put_wait_data(cosa, '\r') == -1)
1447 if (get_wait_data(cosa) != '\r')
1449 if (get_wait_data(cosa) != '\r')
1451 if (get_wait_data(cosa) != '\n')
1453 if (get_wait_data(cosa) != '\r')
1455 if (get_wait_data(cosa) != '\n')
1458 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1463 /* Reading memory is done via the "r" command of the SRP monitor.
1464 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1465 * Then driver can read the data and the conversation is finished
1466 * by SRP monitor sending "<CR><LF>." (dot at the end).
1468 * This routine is not needed during the normal operation and serves
1469 * for debugging purposes only.
1471 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1473 if (put_wait_data(cosa, 'r') == -1)
1475 if ((get_wait_data(cosa)) != 'r')
1477 if ((get_wait_data(cosa)) != '=')
1480 if (puthexnumber(cosa, address) < 0)
1482 if (put_wait_data(cosa, ' ') == -1)
1484 if (get_wait_data(cosa) != ' ')
1486 if (get_wait_data(cosa) != '=')
1489 if (puthexnumber(cosa, address + length - 1) < 0)
1491 if (put_wait_data(cosa, ' ') == -1)
1493 if (get_wait_data(cosa) != ' ')
1500 i = get_wait_data(cosa);
1502 pr_info("0x%04x bytes remaining\n", length);
1507 if (put_user(c, microcode))
1508 return -23; /* ??? */
1515 if (get_wait_data(cosa) != '\r')
1517 if (get_wait_data(cosa) != '\n')
1519 if (get_wait_data(cosa) != '.')
1522 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1527 /* This function resets the device and reads the initial prompt
1528 * of the device's ROM monitor.
1530 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1532 int i = 0, id = 0, prev = 0, curr = 0;
1534 /* Reset the card ... */
1535 cosa_putstatus(cosa, 0);
1536 cosa_getdata8(cosa);
1537 cosa_putstatus(cosa, SR_RST);
1539 /* Disable all IRQs from the card */
1540 cosa_putstatus(cosa, 0);
1542 /* Try to read the ID string. The card then prints out the
1543 * identification string ended by the "\n\x2e".
1545 * The following loop is indexed through i (instead of id)
1546 * to avoid looping forever when for any reason
1547 * the port returns '\r', '\n' or '\x2e' permanently.
1549 for (i = 0; i < COSA_MAX_ID_STRING - 1; i++, prev = curr) {
1550 curr = get_wait_data(cosa);
1555 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1556 idstring[id++] = curr;
1557 if (curr == 0x2e && prev == '\n')
1560 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1561 idstring[id] = '\0';
1565 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1567 /* This routine gets the data byte from the card waiting for the SR_RX_RDY
1568 * bit to be set in a loop. It should be used in the exceptional cases
1569 * only (for example when resetting the card or downloading the firmware.
1571 static int get_wait_data(struct cosa_data *cosa)
1576 /* read data and return them */
1577 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1580 r = cosa_getdata8(cosa);
1582 pr_info("get_wait_data returning after %d retries\n",
1587 /* sleep if not ready to read */
1588 schedule_timeout_interruptible(1);
1590 pr_info("timeout in get_wait_data (status 0x%x)\n",
1591 cosa_getstatus(cosa));
1595 /* This routine puts the data byte to the card waiting for the SR_TX_RDY
1596 * bit to be set in a loop. It should be used in the exceptional cases
1597 * only (for example when resetting the card or downloading the firmware).
1599 static int put_wait_data(struct cosa_data *cosa, int data)
1604 /* read data and return them */
1605 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1606 cosa_putdata8(cosa, data);
1608 pr_info("Putdata: %d retries\n", 999 - retries);
1613 /* sleep if not ready to read */
1614 schedule_timeout_interruptible(1);
1617 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1618 cosa->num, cosa_getstatus(cosa));
1622 /* The following routine puts the hexadecimal number into the SRP monitor
1623 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1624 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1625 * (-2,-4,-6,-8) means that reading echo failed.
1627 static int puthexnumber(struct cosa_data *cosa, int number)
1632 /* Well, I should probably replace this by something faster. */
1633 sprintf(temp, "%04X", number);
1634 for (i = 0; i < 4; i++) {
1635 if (put_wait_data(cosa, temp[i]) == -1) {
1636 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1640 if (get_wait_data(cosa) != temp[i]) {
1641 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1649 /* ---------- Interrupt routines ---------- */
1651 /* There are three types of interrupt:
1652 * At the beginning of transmit - this handled is in tx_interrupt(),
1653 * at the beginning of receive - it is in rx_interrupt() and
1654 * at the end of transmit/receive - it is the eot_interrupt() function.
1655 * These functions are multiplexed by cosa_interrupt() according to the
1656 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1657 * separate functions to make it more readable. These functions are inline,
1658 * so there should be no overhead of function call.
1660 * In the COSA bus-master mode, we need to tell the card the address of a
1661 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1662 * It's time to use the bottom half :-(
1665 /* Transmit interrupt routine - called when COSA is willing to obtain
1666 * data from the OS. The most tricky part of the routine is selection
1667 * of channel we (OS) want to send packet for. For SRP we should probably
1668 * use the round-robin approach. The newer COSA firmwares have a simple
1669 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1670 * channel 0 or 1 doesn't want to receive data.
1672 * It seems there is a bug in COSA firmware (need to trace it further):
1673 * When the driver status says that the kernel has no more data for transmit
1674 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1675 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1676 * the TX interrupt but does not mark the channel as ready-to-transmit.
1677 * The fix seems to be to push the packet to COSA despite its request.
1678 * We first try to obey the card's opinion, and then fall back to forced TX.
1680 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1682 unsigned long flags, flags1;
1684 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1686 spin_lock_irqsave(&cosa->lock, flags);
1687 set_bit(TXBIT, &cosa->rxtx);
1688 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1689 /* flow control, see the comment above */
1692 if (!cosa->txbitmap) {
1693 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1695 put_driver_status_nolock(cosa);
1696 clear_bit(TXBIT, &cosa->rxtx);
1697 spin_unlock_irqrestore(&cosa->lock, flags);
1703 if (cosa->txchan >= cosa->nchannels)
1705 if (!(cosa->txbitmap & (1 << cosa->txchan)))
1708 (1 << (cosa->txchan + DRIVER_TXMAP_SHIFT)))
1710 /* in second pass, accept first ready-to-TX channel */
1711 if (i > cosa->nchannels) {
1712 /* Can be safely ignored */
1714 printk(KERN_DEBUG "%s: Forcing TX "
1715 "to not-ready channel %d\n",
1716 cosa->name, cosa->txchan);
1722 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1723 if (cosa_dma_able(cosa->chan + cosa->txchan,
1724 cosa->chan[cosa->txchan].txbuf,
1726 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1728 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1730 cosa->txbuf = cosa->bouncebuf;
1734 if (is_8bit(cosa)) {
1735 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1736 cosa_putstatus(cosa, SR_TX_INT_ENA);
1737 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0) |
1738 ((cosa->txsize >> 8) & 0x1f));
1740 debug_status_out(cosa, SR_TX_INT_ENA);
1741 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0) |
1742 ((cosa->txsize >> 8) & 0x1f));
1743 debug_data_in(cosa, cosa_getdata8(cosa));
1745 cosa_getdata8(cosa);
1747 set_bit(IRQBIT, &cosa->rxtx);
1748 spin_unlock_irqrestore(&cosa->lock, flags);
1751 clear_bit(IRQBIT, &cosa->rxtx);
1752 cosa_putstatus(cosa, 0);
1753 cosa_putdata8(cosa, cosa->txsize & 0xff);
1755 debug_status_out(cosa, 0);
1756 debug_data_out(cosa, cosa->txsize & 0xff);
1760 cosa_putstatus(cosa, SR_TX_INT_ENA);
1761 cosa_putdata16(cosa, ((cosa->txchan << 13) & 0xe000)
1762 | (cosa->txsize & 0x1fff));
1764 debug_status_out(cosa, SR_TX_INT_ENA);
1765 debug_data_out(cosa, ((cosa->txchan << 13) & 0xe000) |
1766 (cosa->txsize & 0x1fff));
1767 debug_data_in(cosa, cosa_getdata8(cosa));
1768 debug_status_out(cosa, 0);
1770 cosa_getdata8(cosa);
1772 cosa_putstatus(cosa, 0);
1775 if (cosa->busmaster) {
1776 unsigned long addr = virt_to_bus(cosa->txbuf);
1779 pr_info("busmaster IRQ\n");
1780 while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1786 pr_info("status %x\n", cosa_getstatus(cosa));
1787 pr_info("ready after %d loops\n", count);
1788 cosa_putdata16(cosa, (addr >> 16) & 0xffff);
1791 while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1797 pr_info("ready after %d loops\n", count);
1798 cosa_putdata16(cosa, addr & 0xffff);
1799 flags1 = claim_dma_lock();
1800 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1801 enable_dma(cosa->dma);
1802 release_dma_lock(flags1);
1805 flags1 = claim_dma_lock();
1806 disable_dma(cosa->dma);
1807 clear_dma_ff(cosa->dma);
1808 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1809 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1810 set_dma_count(cosa->dma, cosa->txsize);
1811 enable_dma(cosa->dma);
1812 release_dma_lock(flags1);
1814 cosa_putstatus(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1816 debug_status_out(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1818 spin_unlock_irqrestore(&cosa->lock, flags);
1821 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1823 unsigned long flags;
1825 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1828 spin_lock_irqsave(&cosa->lock, flags);
1829 set_bit(RXBIT, &cosa->rxtx);
1831 if (is_8bit(cosa)) {
1832 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1833 set_bit(IRQBIT, &cosa->rxtx);
1834 put_driver_status_nolock(cosa);
1835 cosa->rxsize = cosa_getdata8(cosa) << 8;
1837 debug_data_in(cosa, cosa->rxsize >> 8);
1839 spin_unlock_irqrestore(&cosa->lock, flags);
1842 clear_bit(IRQBIT, &cosa->rxtx);
1843 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1845 debug_data_in(cosa, cosa->rxsize & 0xff);
1848 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1849 cosa->num, cosa->rxsize);
1853 cosa->rxsize = cosa_getdata16(cosa);
1855 debug_data_in(cosa, cosa->rxsize);
1858 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1859 cosa->num, cosa->rxsize);
1862 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1863 pr_warn("%s: rx for unknown channel (0x%04x)\n",
1864 cosa->name, cosa->rxsize);
1865 spin_unlock_irqrestore(&cosa->lock, flags);
1868 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1869 cosa->rxsize &= 0x1fff;
1870 spin_unlock_irqrestore(&cosa->lock, flags);
1873 if (cosa->rxchan->setup_rx)
1874 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1877 reject: /* Reject the packet */
1878 pr_info("cosa%d: rejecting packet on channel %d\n",
1879 cosa->num, cosa->rxchan->num);
1880 cosa->rxbuf = cosa->bouncebuf;
1884 flags = claim_dma_lock();
1885 disable_dma(cosa->dma);
1886 clear_dma_ff(cosa->dma);
1887 set_dma_mode(cosa->dma, DMA_MODE_READ);
1888 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff))
1889 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1891 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1893 set_dma_count(cosa->dma, (cosa->rxsize & 0x1fff));
1894 enable_dma(cosa->dma);
1895 release_dma_lock(flags);
1896 spin_lock_irqsave(&cosa->lock, flags);
1897 cosa_putstatus(cosa, SR_RX_DMA_ENA | SR_USR_INT_ENA);
1898 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1899 cosa_putdata8(cosa, DRIVER_RX_READY);
1901 debug_status_out(cosa, SR_RX_DMA_ENA | SR_USR_INT_ENA);
1902 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1903 debug_data_cmd(cosa, DRIVER_RX_READY);
1905 spin_unlock_irqrestore(&cosa->lock, flags);
1908 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1910 unsigned long flags, flags1;
1912 spin_lock_irqsave(&cosa->lock, flags);
1913 flags1 = claim_dma_lock();
1914 disable_dma(cosa->dma);
1915 clear_dma_ff(cosa->dma);
1916 release_dma_lock(flags1);
1917 if (test_bit(TXBIT, &cosa->rxtx)) {
1918 struct channel_data *chan = cosa->chan + cosa->txchan;
1921 if (chan->tx_done(chan, cosa->txsize))
1922 clear_bit(chan->num, &cosa->txbitmap);
1923 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1928 pr_info("cosa%dc%d: done rx(0x%x)",
1929 cosa->num, cosa->rxchan->num, cosa->rxsize);
1930 for (i = 0; i < cosa->rxsize; i++)
1931 pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1935 /* Packet for unknown channel? */
1936 if (cosa->rxbuf == cosa->bouncebuf)
1938 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1939 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1940 if (cosa->rxchan->rx_done)
1941 if (cosa->rxchan->rx_done(cosa->rxchan))
1942 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1944 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1946 /* Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1947 * cleared anyway). We should do it as soon as possible
1948 * so that we can tell the COSA we are done and to give it a time
1953 put_driver_status_nolock(cosa);
1954 spin_unlock_irqrestore(&cosa->lock, flags);
1957 static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1961 struct cosa_data *cosa = cosa_;
1963 status = cosa_getstatus(cosa);
1965 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1968 debug_status_in(cosa, status);
1970 switch (status & SR_CMD_FROM_SRP_MASK) {
1971 case SR_DOWN_REQUEST:
1972 tx_interrupt(cosa, status);
1975 rx_interrupt(cosa, status);
1977 case SR_END_OF_TRANSFER:
1978 eot_interrupt(cosa, status);
1981 /* We may be too fast for SRP. Try to wait a bit more. */
1982 if (count++ < 100) {
1986 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1987 cosa->num, status & 0xff, count);
1991 pr_info("%s: %d-times got unknown status in IRQ\n",
1994 pr_info("%s: returning from IRQ\n", cosa->name);
1999 /* ---------- I/O debugging routines ---------- */
2000 /* These routines can be used to monitor COSA/SRP I/O and to printk()
2001 * the data being transferred on the data and status I/O port in a
2006 static void debug_status_in(struct cosa_data *cosa, int status)
2010 switch (status & SR_CMD_FROM_SRP_MASK) {
2014 case SR_DOWN_REQUEST:
2017 case SR_END_OF_TRANSFER:
2024 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2027 status & SR_USR_RQ ? "USR_RQ|" : "",
2028 status & SR_TX_RDY ? "TX_RDY|" : "",
2029 status & SR_RX_RDY ? "RX_RDY|" : "",
2033 static void debug_status_out(struct cosa_data *cosa, int status)
2035 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2038 status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2039 status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2040 status & SR_RST ? "RESET|" : "",
2041 status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2042 status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2043 status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
2046 static void debug_data_in(struct cosa_data *cosa, int data)
2048 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2051 static void debug_data_out(struct cosa_data *cosa, int data)
2053 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2056 static void debug_data_cmd(struct cosa_data *cosa, int data)
2058 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2060 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2061 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2065 /* EOF -- this file has not been truncated */