Loading...
1/* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
2
3/*
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>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22/*
23 * The driver for the SRP and COSA synchronous serial cards.
24 *
25 * HARDWARE INFO
26 *
27 * Both cards are developed at the Institute of Computer Science,
28 * Masaryk University (http://www.ics.muni.cz/). The hardware is
29 * developed by Jiri Novotny <novotny@ics.muni.cz>. More information
30 * and the photo of both cards is available at
31 * http://www.pavoucek.cz/cosa.html. The card documentation, firmwares
32 * and other goods can be downloaded from ftp://ftp.ics.muni.cz/pub/cosa/.
33 * For Linux-specific utilities, see below in the "Software info" section.
34 * If you want to order the card, contact Jiri Novotny.
35 *
36 * The SRP (serial port?, the Czech word "srp" means "sickle") card
37 * is a 2-port intelligent (with its own 8-bit CPU) synchronous serial card
38 * with V.24 interfaces up to 80kb/s each.
39 *
40 * The COSA (communication serial adapter?, the Czech word "kosa" means
41 * "scythe") is a next-generation sync/async board with two interfaces
42 * - currently any of V.24, X.21, V.35 and V.36 can be selected.
43 * It has a 16-bit SAB80166 CPU and can do up to 10 Mb/s per channel.
44 * The 8-channels version is in development.
45 *
46 * Both types have downloadable firmware and communicate via ISA DMA.
47 * COSA can be also a bus-mastering device.
48 *
49 * SOFTWARE INFO
50 *
51 * The homepage of the Linux driver is at http://www.fi.muni.cz/~kas/cosa/.
52 * The CVS tree of Linux driver can be viewed there, as well as the
53 * firmware binaries and user-space utilities for downloading the firmware
54 * into the card and setting up the card.
55 *
56 * The Linux driver (unlike the present *BSD drivers :-) can work even
57 * for the COSA and SRP in one computer and allows each channel to work
58 * in one of the two modes (character or network device).
59 *
60 * AUTHOR
61 *
62 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
63 *
64 * You can mail me bugfixes and even success reports. I am especially
65 * interested in the SMP and/or muliti-channel success/failure reports
66 * (I wonder if I did the locking properly :-).
67 *
68 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
69 *
70 * The COSA/SRP NetBSD driver by Zdenek Salvet and Ivos Cernohlavek
71 * The skeleton.c by Donald Becker
72 * The SDL Riscom/N2 driver by Mike Natale
73 * The Comtrol Hostess SV11 driver by Alan Cox
74 * The Sync PPP/Cisco HDLC layer (syncppp.c) ported to Linux by Alan Cox
75 */
76
77#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78
79#include <linux/module.h>
80#include <linux/kernel.h>
81#include <linux/sched/signal.h>
82#include <linux/slab.h>
83#include <linux/poll.h>
84#include <linux/fs.h>
85#include <linux/interrupt.h>
86#include <linux/delay.h>
87#include <linux/hdlc.h>
88#include <linux/errno.h>
89#include <linux/ioport.h>
90#include <linux/netdevice.h>
91#include <linux/spinlock.h>
92#include <linux/mutex.h>
93#include <linux/device.h>
94#include <asm/io.h>
95#include <asm/dma.h>
96#include <asm/byteorder.h>
97
98#undef COSA_SLOW_IO /* for testing purposes only */
99
100#include "cosa.h"
101
102/* Maximum length of the identification string. */
103#define COSA_MAX_ID_STRING 128
104
105/* Maximum length of the channel name */
106#define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
107
108/* Per-channel data structure */
109
110struct channel_data {
111 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
112 int num; /* Number of the channel */
113 struct cosa_data *cosa; /* Pointer to the per-card structure */
114 int txsize; /* Size of transmitted data */
115 char *txbuf; /* Transmit buffer */
116 char name[COSA_MAX_NAME]; /* channel name */
117
118 /* The HW layer interface */
119 /* routine called from the RX interrupt */
120 char *(*setup_rx)(struct channel_data *channel, int size);
121 /* routine called when the RX is done (from the EOT interrupt) */
122 int (*rx_done)(struct channel_data *channel);
123 /* routine called when the TX is done (from the EOT interrupt) */
124 int (*tx_done)(struct channel_data *channel, int size);
125
126 /* Character device parts */
127 struct mutex rlock;
128 struct semaphore wsem;
129 char *rxdata;
130 int rxsize;
131 wait_queue_head_t txwaitq, rxwaitq;
132 int tx_status, rx_status;
133
134 /* generic HDLC device parts */
135 struct net_device *netdev;
136 struct sk_buff *rx_skb, *tx_skb;
137};
138
139/* cosa->firmware_status bits */
140#define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
141#define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
142#define COSA_FW_START (1<<2) /* Is the microcode running? */
143
144struct cosa_data {
145 int num; /* Card number */
146 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
147 unsigned int datareg, statusreg; /* I/O ports */
148 unsigned short irq, dma; /* IRQ and DMA number */
149 unsigned short startaddr; /* Firmware start address */
150 unsigned short busmaster; /* Use busmastering? */
151 int nchannels; /* # of channels on this card */
152 int driver_status; /* For communicating with firmware */
153 int firmware_status; /* Downloaded, reseted, etc. */
154 unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
155 unsigned long rxtx; /* RX or TX in progress? */
156 int enabled;
157 int usage; /* usage count */
158 int txchan, txsize, rxsize;
159 struct channel_data *rxchan;
160 char *bouncebuf;
161 char *txbuf, *rxbuf;
162 struct channel_data *chan;
163 spinlock_t lock; /* For exclusive operations on this structure */
164 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
165 char *type; /* card type */
166};
167
168/*
169 * Define this if you want all the possible ports to be autoprobed.
170 * It is here but it probably is not a good idea to use this.
171 */
172/* #define COSA_ISA_AUTOPROBE 1 */
173
174/*
175 * Character device major number. 117 was allocated for us.
176 * The value of 0 means to allocate a first free one.
177 */
178static DEFINE_MUTEX(cosa_chardev_mutex);
179static int cosa_major = 117;
180
181/*
182 * Encoding of the minor numbers:
183 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
184 * the highest bits means the card number.
185 */
186#define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
187 * for the single card */
188/*
189 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
190 * macro doesn't like anything other than the raw number as an argument :-(
191 */
192#define MAX_CARDS 16
193/* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
194
195#define DRIVER_RX_READY 0x0001
196#define DRIVER_TX_READY 0x0002
197#define DRIVER_TXMAP_SHIFT 2
198#define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
199
200/*
201 * for cosa->rxtx - indicates whether either transmit or receive is
202 * in progress. These values are mean number of the bit.
203 */
204#define TXBIT 0
205#define RXBIT 1
206#define IRQBIT 2
207
208#define COSA_MTU 2000 /* FIXME: I don't know this exactly */
209
210#undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
211#undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
212#undef DEBUG_IO //1 /* Dump the I/O traffic */
213
214#define TX_TIMEOUT (5*HZ)
215
216/* Maybe the following should be allocated dynamically */
217static struct cosa_data cosa_cards[MAX_CARDS];
218static int nr_cards;
219
220#ifdef COSA_ISA_AUTOPROBE
221static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
222/* NOTE: DMA is not autoprobed!!! */
223static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
224#else
225static int io[MAX_CARDS+1];
226static int dma[MAX_CARDS+1];
227#endif
228/* IRQ can be safely autoprobed */
229static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
230
231/* for class stuff*/
232static struct class *cosa_class;
233
234#ifdef MODULE
235module_param_hw_array(io, int, ioport, NULL, 0);
236MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
237module_param_hw_array(irq, int, irq, NULL, 0);
238MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
239module_param_hw_array(dma, int, dma, NULL, 0);
240MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
241
242MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
243MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
244MODULE_LICENSE("GPL");
245#endif
246
247/* I use this mainly for testing purposes */
248#ifdef COSA_SLOW_IO
249#define cosa_outb outb_p
250#define cosa_outw outw_p
251#define cosa_inb inb_p
252#define cosa_inw inw_p
253#else
254#define cosa_outb outb
255#define cosa_outw outw
256#define cosa_inb inb
257#define cosa_inw inw
258#endif
259
260#define is_8bit(cosa) (!(cosa->datareg & 0x08))
261
262#define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
263#define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
264#define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
265#define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
266#define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
267#define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
268
269/* Initialization stuff */
270static int cosa_probe(int ioaddr, int irq, int dma);
271
272/* HW interface */
273static void cosa_enable_rx(struct channel_data *chan);
274static void cosa_disable_rx(struct channel_data *chan);
275static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
276static void cosa_kick(struct cosa_data *cosa);
277static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
278
279/* Network device stuff */
280static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
281 unsigned short parity);
282static int cosa_net_open(struct net_device *d);
283static int cosa_net_close(struct net_device *d);
284static void cosa_net_timeout(struct net_device *d);
285static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
286static char *cosa_net_setup_rx(struct channel_data *channel, int size);
287static int cosa_net_rx_done(struct channel_data *channel);
288static int cosa_net_tx_done(struct channel_data *channel, int size);
289static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
290
291/* Character device */
292static char *chrdev_setup_rx(struct channel_data *channel, int size);
293static int chrdev_rx_done(struct channel_data *channel);
294static int chrdev_tx_done(struct channel_data *channel, int size);
295static ssize_t cosa_read(struct file *file,
296 char __user *buf, size_t count, loff_t *ppos);
297static ssize_t cosa_write(struct file *file,
298 const char __user *buf, size_t count, loff_t *ppos);
299static unsigned int cosa_poll(struct file *file, poll_table *poll);
300static int cosa_open(struct inode *inode, struct file *file);
301static int cosa_release(struct inode *inode, struct file *file);
302static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
303 unsigned long arg);
304#ifdef COSA_FASYNC_WORKING
305static int cosa_fasync(struct inode *inode, struct file *file, int on);
306#endif
307
308static const struct file_operations cosa_fops = {
309 .owner = THIS_MODULE,
310 .llseek = no_llseek,
311 .read = cosa_read,
312 .write = cosa_write,
313 .poll = cosa_poll,
314 .unlocked_ioctl = cosa_chardev_ioctl,
315 .open = cosa_open,
316 .release = cosa_release,
317#ifdef COSA_FASYNC_WORKING
318 .fasync = cosa_fasync,
319#endif
320};
321
322/* Ioctls */
323static int cosa_start(struct cosa_data *cosa, int address);
324static int cosa_reset(struct cosa_data *cosa);
325static int cosa_download(struct cosa_data *cosa, void __user *a);
326static int cosa_readmem(struct cosa_data *cosa, void __user *a);
327
328/* COSA/SRP ROM monitor */
329static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
330static int startmicrocode(struct cosa_data *cosa, int address);
331static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
332static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
333
334/* Auxiliary functions */
335static int get_wait_data(struct cosa_data *cosa);
336static int put_wait_data(struct cosa_data *cosa, int data);
337static int puthexnumber(struct cosa_data *cosa, int number);
338static void put_driver_status(struct cosa_data *cosa);
339static void put_driver_status_nolock(struct cosa_data *cosa);
340
341/* Interrupt handling */
342static irqreturn_t cosa_interrupt(int irq, void *cosa);
343
344/* I/O ops debugging */
345#ifdef DEBUG_IO
346static void debug_data_in(struct cosa_data *cosa, int data);
347static void debug_data_out(struct cosa_data *cosa, int data);
348static void debug_data_cmd(struct cosa_data *cosa, int data);
349static void debug_status_in(struct cosa_data *cosa, int status);
350static void debug_status_out(struct cosa_data *cosa, int status);
351#endif
352
353static inline struct channel_data* dev_to_chan(struct net_device *dev)
354{
355 return (struct channel_data *)dev_to_hdlc(dev)->priv;
356}
357
358/* ---------- Initialization stuff ---------- */
359
360static int __init cosa_init(void)
361{
362 int i, err = 0;
363
364 if (cosa_major > 0) {
365 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
366 pr_warn("unable to get major %d\n", cosa_major);
367 err = -EIO;
368 goto out;
369 }
370 } else {
371 if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
372 pr_warn("unable to register chardev\n");
373 err = -EIO;
374 goto out;
375 }
376 }
377 for (i=0; i<MAX_CARDS; i++)
378 cosa_cards[i].num = -1;
379 for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
380 cosa_probe(io[i], irq[i], dma[i]);
381 if (!nr_cards) {
382 pr_warn("no devices found\n");
383 unregister_chrdev(cosa_major, "cosa");
384 err = -ENODEV;
385 goto out;
386 }
387 cosa_class = class_create(THIS_MODULE, "cosa");
388 if (IS_ERR(cosa_class)) {
389 err = PTR_ERR(cosa_class);
390 goto out_chrdev;
391 }
392 for (i = 0; i < nr_cards; i++)
393 device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
394 "cosa%d", i);
395 err = 0;
396 goto out;
397
398out_chrdev:
399 unregister_chrdev(cosa_major, "cosa");
400out:
401 return err;
402}
403module_init(cosa_init);
404
405static void __exit cosa_exit(void)
406{
407 struct cosa_data *cosa;
408 int i;
409
410 for (i = 0; i < nr_cards; i++)
411 device_destroy(cosa_class, MKDEV(cosa_major, i));
412 class_destroy(cosa_class);
413
414 for (cosa = cosa_cards; nr_cards--; cosa++) {
415 /* Clean up the per-channel data */
416 for (i = 0; i < cosa->nchannels; i++) {
417 /* Chardev driver has no alloc'd per-channel data */
418 unregister_hdlc_device(cosa->chan[i].netdev);
419 free_netdev(cosa->chan[i].netdev);
420 }
421 /* Clean up the per-card data */
422 kfree(cosa->chan);
423 kfree(cosa->bouncebuf);
424 free_irq(cosa->irq, cosa);
425 free_dma(cosa->dma);
426 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
427 }
428 unregister_chrdev(cosa_major, "cosa");
429}
430module_exit(cosa_exit);
431
432static const struct net_device_ops cosa_ops = {
433 .ndo_open = cosa_net_open,
434 .ndo_stop = cosa_net_close,
435 .ndo_start_xmit = hdlc_start_xmit,
436 .ndo_do_ioctl = cosa_net_ioctl,
437 .ndo_tx_timeout = cosa_net_timeout,
438};
439
440static int cosa_probe(int base, int irq, int dma)
441{
442 struct cosa_data *cosa = cosa_cards+nr_cards;
443 int i, err = 0;
444
445 memset(cosa, 0, sizeof(struct cosa_data));
446
447 /* Checking validity of parameters: */
448 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
449 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
450 pr_info("invalid IRQ %d\n", irq);
451 return -1;
452 }
453 /* I/O address should be between 0x100 and 0x3ff and should be
454 * multiple of 8. */
455 if (base < 0x100 || base > 0x3ff || base & 0x7) {
456 pr_info("invalid I/O address 0x%x\n", base);
457 return -1;
458 }
459 /* DMA should be 0,1 or 3-7 */
460 if (dma < 0 || dma == 4 || dma > 7) {
461 pr_info("invalid DMA %d\n", dma);
462 return -1;
463 }
464 /* and finally, on 16-bit COSA DMA should be 4-7 and
465 * I/O base should not be multiple of 0x10 */
466 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
467 pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
468 base, dma);
469 return -1;
470 }
471
472 cosa->dma = dma;
473 cosa->datareg = base;
474 cosa->statusreg = is_8bit(cosa)?base+1:base+2;
475 spin_lock_init(&cosa->lock);
476
477 if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
478 return -1;
479
480 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
481 printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
482 err = -1;
483 goto err_out;
484 }
485
486 /* Test the validity of identification string */
487 if (!strncmp(cosa->id_string, "SRP", 3))
488 cosa->type = "srp";
489 else if (!strncmp(cosa->id_string, "COSA", 4))
490 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
491 else {
492/* Print a warning only if we are not autoprobing */
493#ifndef COSA_ISA_AUTOPROBE
494 pr_info("valid signature not found at 0x%x\n", base);
495#endif
496 err = -1;
497 goto err_out;
498 }
499 /* Update the name of the region now we know the type of card */
500 release_region(base, is_8bit(cosa)?2:4);
501 if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
502 printk(KERN_DEBUG "changing name at 0x%x failed.\n", base);
503 return -1;
504 }
505
506 /* Now do IRQ autoprobe */
507 if (irq < 0) {
508 unsigned long irqs;
509/* pr_info("IRQ autoprobe\n"); */
510 irqs = probe_irq_on();
511 /*
512 * Enable interrupt on tx buffer empty (it sure is)
513 * really sure ?
514 * FIXME: When this code is not used as module, we should
515 * probably call udelay() instead of the interruptible sleep.
516 */
517 set_current_state(TASK_INTERRUPTIBLE);
518 cosa_putstatus(cosa, SR_TX_INT_ENA);
519 schedule_timeout(msecs_to_jiffies(300));
520 irq = probe_irq_off(irqs);
521 /* Disable all IRQs from the card */
522 cosa_putstatus(cosa, 0);
523 /* Empty the received data register */
524 cosa_getdata8(cosa);
525
526 if (irq < 0) {
527 pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
528 irq, cosa->datareg);
529 err = -1;
530 goto err_out;
531 }
532 if (irq == 0) {
533 pr_info("no interrupt obtained (board at 0x%x)\n",
534 cosa->datareg);
535 /* return -1; */
536 }
537 }
538
539 cosa->irq = irq;
540 cosa->num = nr_cards;
541 cosa->usage = 0;
542 cosa->nchannels = 2; /* FIXME: how to determine this? */
543
544 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
545 err = -1;
546 goto err_out;
547 }
548 if (request_dma(cosa->dma, cosa->type)) {
549 err = -1;
550 goto err_out1;
551 }
552
553 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
554 if (!cosa->bouncebuf) {
555 err = -ENOMEM;
556 goto err_out2;
557 }
558 sprintf(cosa->name, "cosa%d", cosa->num);
559
560 /* Initialize the per-channel data */
561 cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
562 if (!cosa->chan) {
563 err = -ENOMEM;
564 goto err_out3;
565 }
566
567 for (i = 0; i < cosa->nchannels; i++) {
568 struct channel_data *chan = &cosa->chan[i];
569
570 chan->cosa = cosa;
571 chan->num = i;
572 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
573
574 /* Initialize the chardev data structures */
575 mutex_init(&chan->rlock);
576 sema_init(&chan->wsem, 1);
577
578 /* Register the network interface */
579 if (!(chan->netdev = alloc_hdlcdev(chan))) {
580 pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
581 err = -ENOMEM;
582 goto err_hdlcdev;
583 }
584 dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
585 dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
586 chan->netdev->netdev_ops = &cosa_ops;
587 chan->netdev->watchdog_timeo = TX_TIMEOUT;
588 chan->netdev->base_addr = chan->cosa->datareg;
589 chan->netdev->irq = chan->cosa->irq;
590 chan->netdev->dma = chan->cosa->dma;
591 err = register_hdlc_device(chan->netdev);
592 if (err) {
593 netdev_warn(chan->netdev,
594 "register_hdlc_device() failed\n");
595 free_netdev(chan->netdev);
596 goto err_hdlcdev;
597 }
598 }
599
600 pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
601 cosa->num, cosa->id_string, cosa->type,
602 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
603
604 return nr_cards++;
605
606err_hdlcdev:
607 while (i-- > 0) {
608 unregister_hdlc_device(cosa->chan[i].netdev);
609 free_netdev(cosa->chan[i].netdev);
610 }
611 kfree(cosa->chan);
612err_out3:
613 kfree(cosa->bouncebuf);
614err_out2:
615 free_dma(cosa->dma);
616err_out1:
617 free_irq(cosa->irq, cosa);
618err_out:
619 release_region(cosa->datareg,is_8bit(cosa)?2:4);
620 pr_notice("cosa%d: allocating resources failed\n", cosa->num);
621 return err;
622}
623
624
625/*---------- network device ---------- */
626
627static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
628 unsigned short parity)
629{
630 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
631 return 0;
632 return -EINVAL;
633}
634
635static int cosa_net_open(struct net_device *dev)
636{
637 struct channel_data *chan = dev_to_chan(dev);
638 int err;
639 unsigned long flags;
640
641 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
642 pr_notice("%s: start the firmware first (status %d)\n",
643 chan->cosa->name, chan->cosa->firmware_status);
644 return -EPERM;
645 }
646 spin_lock_irqsave(&chan->cosa->lock, flags);
647 if (chan->usage != 0) {
648 pr_warn("%s: cosa_net_open called with usage count %d\n",
649 chan->name, chan->usage);
650 spin_unlock_irqrestore(&chan->cosa->lock, flags);
651 return -EBUSY;
652 }
653 chan->setup_rx = cosa_net_setup_rx;
654 chan->tx_done = cosa_net_tx_done;
655 chan->rx_done = cosa_net_rx_done;
656 chan->usage = -1;
657 chan->cosa->usage++;
658 spin_unlock_irqrestore(&chan->cosa->lock, flags);
659
660 err = hdlc_open(dev);
661 if (err) {
662 spin_lock_irqsave(&chan->cosa->lock, flags);
663 chan->usage = 0;
664 chan->cosa->usage--;
665 spin_unlock_irqrestore(&chan->cosa->lock, flags);
666 return err;
667 }
668
669 netif_start_queue(dev);
670 cosa_enable_rx(chan);
671 return 0;
672}
673
674static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
675 struct net_device *dev)
676{
677 struct channel_data *chan = dev_to_chan(dev);
678
679 netif_stop_queue(dev);
680
681 chan->tx_skb = skb;
682 cosa_start_tx(chan, skb->data, skb->len);
683 return NETDEV_TX_OK;
684}
685
686static void cosa_net_timeout(struct net_device *dev)
687{
688 struct channel_data *chan = dev_to_chan(dev);
689
690 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
691 chan->netdev->stats.rx_errors++;
692 chan->netdev->stats.rx_missed_errors++;
693 } else {
694 chan->netdev->stats.tx_errors++;
695 chan->netdev->stats.tx_aborted_errors++;
696 }
697 cosa_kick(chan->cosa);
698 if (chan->tx_skb) {
699 dev_kfree_skb(chan->tx_skb);
700 chan->tx_skb = NULL;
701 }
702 netif_wake_queue(dev);
703}
704
705static int cosa_net_close(struct net_device *dev)
706{
707 struct channel_data *chan = dev_to_chan(dev);
708 unsigned long flags;
709
710 netif_stop_queue(dev);
711 hdlc_close(dev);
712 cosa_disable_rx(chan);
713 spin_lock_irqsave(&chan->cosa->lock, flags);
714 if (chan->rx_skb) {
715 kfree_skb(chan->rx_skb);
716 chan->rx_skb = NULL;
717 }
718 if (chan->tx_skb) {
719 kfree_skb(chan->tx_skb);
720 chan->tx_skb = NULL;
721 }
722 chan->usage = 0;
723 chan->cosa->usage--;
724 spin_unlock_irqrestore(&chan->cosa->lock, flags);
725 return 0;
726}
727
728static char *cosa_net_setup_rx(struct channel_data *chan, int size)
729{
730 /*
731 * We can safely fall back to non-dma-able memory, because we have
732 * the cosa->bouncebuf pre-allocated.
733 */
734 kfree_skb(chan->rx_skb);
735 chan->rx_skb = dev_alloc_skb(size);
736 if (chan->rx_skb == NULL) {
737 pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
738 chan->netdev->stats.rx_dropped++;
739 return NULL;
740 }
741 netif_trans_update(chan->netdev);
742 return skb_put(chan->rx_skb, size);
743}
744
745static int cosa_net_rx_done(struct channel_data *chan)
746{
747 if (!chan->rx_skb) {
748 pr_warn("%s: rx_done with empty skb!\n", chan->name);
749 chan->netdev->stats.rx_errors++;
750 chan->netdev->stats.rx_frame_errors++;
751 return 0;
752 }
753 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
754 chan->rx_skb->dev = chan->netdev;
755 skb_reset_mac_header(chan->rx_skb);
756 chan->netdev->stats.rx_packets++;
757 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
758 netif_rx(chan->rx_skb);
759 chan->rx_skb = NULL;
760 return 0;
761}
762
763/* ARGSUSED */
764static int cosa_net_tx_done(struct channel_data *chan, int size)
765{
766 if (!chan->tx_skb) {
767 pr_warn("%s: tx_done with empty skb!\n", chan->name);
768 chan->netdev->stats.tx_errors++;
769 chan->netdev->stats.tx_aborted_errors++;
770 return 1;
771 }
772 dev_kfree_skb_irq(chan->tx_skb);
773 chan->tx_skb = NULL;
774 chan->netdev->stats.tx_packets++;
775 chan->netdev->stats.tx_bytes += size;
776 netif_wake_queue(chan->netdev);
777 return 1;
778}
779
780/*---------- Character device ---------- */
781
782static ssize_t cosa_read(struct file *file,
783 char __user *buf, size_t count, loff_t *ppos)
784{
785 DECLARE_WAITQUEUE(wait, current);
786 unsigned long flags;
787 struct channel_data *chan = file->private_data;
788 struct cosa_data *cosa = chan->cosa;
789 char *kbuf;
790
791 if (!(cosa->firmware_status & COSA_FW_START)) {
792 pr_notice("%s: start the firmware first (status %d)\n",
793 cosa->name, cosa->firmware_status);
794 return -EPERM;
795 }
796 if (mutex_lock_interruptible(&chan->rlock))
797 return -ERESTARTSYS;
798
799 chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL);
800 if (chan->rxdata == NULL) {
801 mutex_unlock(&chan->rlock);
802 return -ENOMEM;
803 }
804
805 chan->rx_status = 0;
806 cosa_enable_rx(chan);
807 spin_lock_irqsave(&cosa->lock, flags);
808 add_wait_queue(&chan->rxwaitq, &wait);
809 while (!chan->rx_status) {
810 set_current_state(TASK_INTERRUPTIBLE);
811 spin_unlock_irqrestore(&cosa->lock, flags);
812 schedule();
813 spin_lock_irqsave(&cosa->lock, flags);
814 if (signal_pending(current) && chan->rx_status == 0) {
815 chan->rx_status = 1;
816 remove_wait_queue(&chan->rxwaitq, &wait);
817 __set_current_state(TASK_RUNNING);
818 spin_unlock_irqrestore(&cosa->lock, flags);
819 mutex_unlock(&chan->rlock);
820 return -ERESTARTSYS;
821 }
822 }
823 remove_wait_queue(&chan->rxwaitq, &wait);
824 __set_current_state(TASK_RUNNING);
825 kbuf = chan->rxdata;
826 count = chan->rxsize;
827 spin_unlock_irqrestore(&cosa->lock, flags);
828 mutex_unlock(&chan->rlock);
829
830 if (copy_to_user(buf, kbuf, count)) {
831 kfree(kbuf);
832 return -EFAULT;
833 }
834 kfree(kbuf);
835 return count;
836}
837
838static char *chrdev_setup_rx(struct channel_data *chan, int size)
839{
840 /* Expect size <= COSA_MTU */
841 chan->rxsize = size;
842 return chan->rxdata;
843}
844
845static int chrdev_rx_done(struct channel_data *chan)
846{
847 if (chan->rx_status) { /* Reader has died */
848 kfree(chan->rxdata);
849 up(&chan->wsem);
850 }
851 chan->rx_status = 1;
852 wake_up_interruptible(&chan->rxwaitq);
853 return 1;
854}
855
856
857static ssize_t cosa_write(struct file *file,
858 const char __user *buf, size_t count, loff_t *ppos)
859{
860 DECLARE_WAITQUEUE(wait, current);
861 struct channel_data *chan = file->private_data;
862 struct cosa_data *cosa = chan->cosa;
863 unsigned long flags;
864 char *kbuf;
865
866 if (!(cosa->firmware_status & COSA_FW_START)) {
867 pr_notice("%s: start the firmware first (status %d)\n",
868 cosa->name, cosa->firmware_status);
869 return -EPERM;
870 }
871 if (down_interruptible(&chan->wsem))
872 return -ERESTARTSYS;
873
874 if (count > COSA_MTU)
875 count = COSA_MTU;
876
877 /* Allocate the buffer */
878 kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA);
879 if (kbuf == NULL) {
880 up(&chan->wsem);
881 return -ENOMEM;
882 }
883 if (copy_from_user(kbuf, buf, count)) {
884 up(&chan->wsem);
885 kfree(kbuf);
886 return -EFAULT;
887 }
888 chan->tx_status=0;
889 cosa_start_tx(chan, kbuf, count);
890
891 spin_lock_irqsave(&cosa->lock, flags);
892 add_wait_queue(&chan->txwaitq, &wait);
893 while (!chan->tx_status) {
894 set_current_state(TASK_INTERRUPTIBLE);
895 spin_unlock_irqrestore(&cosa->lock, flags);
896 schedule();
897 spin_lock_irqsave(&cosa->lock, flags);
898 if (signal_pending(current) && chan->tx_status == 0) {
899 chan->tx_status = 1;
900 remove_wait_queue(&chan->txwaitq, &wait);
901 __set_current_state(TASK_RUNNING);
902 chan->tx_status = 1;
903 spin_unlock_irqrestore(&cosa->lock, flags);
904 up(&chan->wsem);
905 return -ERESTARTSYS;
906 }
907 }
908 remove_wait_queue(&chan->txwaitq, &wait);
909 __set_current_state(TASK_RUNNING);
910 up(&chan->wsem);
911 spin_unlock_irqrestore(&cosa->lock, flags);
912 kfree(kbuf);
913 return count;
914}
915
916static int chrdev_tx_done(struct channel_data *chan, int size)
917{
918 if (chan->tx_status) { /* Writer was interrupted */
919 kfree(chan->txbuf);
920 up(&chan->wsem);
921 }
922 chan->tx_status = 1;
923 wake_up_interruptible(&chan->txwaitq);
924 return 1;
925}
926
927static __poll_t cosa_poll(struct file *file, poll_table *poll)
928{
929 pr_info("cosa_poll is here\n");
930 return 0;
931}
932
933static int cosa_open(struct inode *inode, struct file *file)
934{
935 struct cosa_data *cosa;
936 struct channel_data *chan;
937 unsigned long flags;
938 int n;
939 int ret = 0;
940
941 mutex_lock(&cosa_chardev_mutex);
942 if ((n=iminor(file_inode(file))>>CARD_MINOR_BITS)
943 >= nr_cards) {
944 ret = -ENODEV;
945 goto out;
946 }
947 cosa = cosa_cards+n;
948
949 if ((n=iminor(file_inode(file))
950 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
951 ret = -ENODEV;
952 goto out;
953 }
954 chan = cosa->chan + n;
955
956 file->private_data = chan;
957
958 spin_lock_irqsave(&cosa->lock, flags);
959
960 if (chan->usage < 0) { /* in netdev mode */
961 spin_unlock_irqrestore(&cosa->lock, flags);
962 ret = -EBUSY;
963 goto out;
964 }
965 cosa->usage++;
966 chan->usage++;
967
968 chan->tx_done = chrdev_tx_done;
969 chan->setup_rx = chrdev_setup_rx;
970 chan->rx_done = chrdev_rx_done;
971 spin_unlock_irqrestore(&cosa->lock, flags);
972out:
973 mutex_unlock(&cosa_chardev_mutex);
974 return ret;
975}
976
977static int cosa_release(struct inode *inode, struct file *file)
978{
979 struct channel_data *channel = file->private_data;
980 struct cosa_data *cosa;
981 unsigned long flags;
982
983 cosa = channel->cosa;
984 spin_lock_irqsave(&cosa->lock, flags);
985 cosa->usage--;
986 channel->usage--;
987 spin_unlock_irqrestore(&cosa->lock, flags);
988 return 0;
989}
990
991#ifdef COSA_FASYNC_WORKING
992static struct fasync_struct *fasync[256] = { NULL, };
993
994/* To be done ... */
995static int cosa_fasync(struct inode *inode, struct file *file, int on)
996{
997 int port = iminor(inode);
998
999 return fasync_helper(inode, file, on, &fasync[port]);
1000}
1001#endif
1002
1003
1004/* ---------- Ioctls ---------- */
1005
1006/*
1007 * Ioctl subroutines can safely be made inline, because they are called
1008 * only from cosa_ioctl().
1009 */
1010static inline int cosa_reset(struct cosa_data *cosa)
1011{
1012 char idstring[COSA_MAX_ID_STRING];
1013 if (cosa->usage > 1)
1014 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1015 cosa->num, cosa->usage);
1016 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1017 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1018 pr_notice("cosa%d: reset failed\n", cosa->num);
1019 return -EIO;
1020 }
1021 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1022 cosa->firmware_status |= COSA_FW_RESET;
1023 return 0;
1024}
1025
1026/* High-level function to download data into COSA memory. Calls download() */
1027static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1028{
1029 struct cosa_download d;
1030 int i;
1031
1032 if (cosa->usage > 1)
1033 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1034 cosa->name, cosa->usage);
1035 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1036 pr_notice("%s: reset the card first (status %d)\n",
1037 cosa->name, cosa->firmware_status);
1038 return -EPERM;
1039 }
1040
1041 if (copy_from_user(&d, arg, sizeof(d)))
1042 return -EFAULT;
1043
1044 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1045 return -EINVAL;
1046 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1047 return -EINVAL;
1048
1049
1050 /* If something fails, force the user to reset the card */
1051 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1052
1053 i = download(cosa, d.code, d.len, d.addr);
1054 if (i < 0) {
1055 pr_notice("cosa%d: microcode download failed: %d\n",
1056 cosa->num, i);
1057 return -EIO;
1058 }
1059 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1060 cosa->num, d.len, d.addr);
1061 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1062 return 0;
1063}
1064
1065/* High-level function to read COSA memory. Calls readmem() */
1066static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1067{
1068 struct cosa_download d;
1069 int i;
1070
1071 if (cosa->usage > 1)
1072 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1073 cosa->num, cosa->usage);
1074 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1075 pr_notice("%s: reset the card first (status %d)\n",
1076 cosa->name, cosa->firmware_status);
1077 return -EPERM;
1078 }
1079
1080 if (copy_from_user(&d, arg, sizeof(d)))
1081 return -EFAULT;
1082
1083 /* If something fails, force the user to reset the card */
1084 cosa->firmware_status &= ~COSA_FW_RESET;
1085
1086 i = readmem(cosa, d.code, d.len, d.addr);
1087 if (i < 0) {
1088 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1089 return -EIO;
1090 }
1091 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1092 cosa->num, d.len, d.addr);
1093 cosa->firmware_status |= COSA_FW_RESET;
1094 return 0;
1095}
1096
1097/* High-level function to start microcode. Calls startmicrocode(). */
1098static inline int cosa_start(struct cosa_data *cosa, int address)
1099{
1100 int i;
1101
1102 if (cosa->usage > 1)
1103 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1104 cosa->num, cosa->usage);
1105
1106 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1107 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1108 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1109 cosa->name, cosa->firmware_status);
1110 return -EPERM;
1111 }
1112 cosa->firmware_status &= ~COSA_FW_RESET;
1113 if ((i=startmicrocode(cosa, address)) < 0) {
1114 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1115 cosa->num, address, i);
1116 return -EIO;
1117 }
1118 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1119 cosa->startaddr = address;
1120 cosa->firmware_status |= COSA_FW_START;
1121 return 0;
1122}
1123
1124/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1125static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1126{
1127 int l = strlen(cosa->id_string)+1;
1128 if (copy_to_user(string, cosa->id_string, l))
1129 return -EFAULT;
1130 return l;
1131}
1132
1133/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1134static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1135{
1136 int l = strlen(cosa->type)+1;
1137 if (copy_to_user(string, cosa->type, l))
1138 return -EFAULT;
1139 return l;
1140}
1141
1142static int cosa_ioctl_common(struct cosa_data *cosa,
1143 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1144{
1145 void __user *argp = (void __user *)arg;
1146 switch (cmd) {
1147 case COSAIORSET: /* Reset the device */
1148 if (!capable(CAP_NET_ADMIN))
1149 return -EACCES;
1150 return cosa_reset(cosa);
1151 case COSAIOSTRT: /* Start the firmware */
1152 if (!capable(CAP_SYS_RAWIO))
1153 return -EACCES;
1154 return cosa_start(cosa, arg);
1155 case COSAIODOWNLD: /* Download the firmware */
1156 if (!capable(CAP_SYS_RAWIO))
1157 return -EACCES;
1158
1159 return cosa_download(cosa, argp);
1160 case COSAIORMEM:
1161 if (!capable(CAP_SYS_RAWIO))
1162 return -EACCES;
1163 return cosa_readmem(cosa, argp);
1164 case COSAIORTYPE:
1165 return cosa_gettype(cosa, argp);
1166 case COSAIORIDSTR:
1167 return cosa_getidstr(cosa, argp);
1168 case COSAIONRCARDS:
1169 return nr_cards;
1170 case COSAIONRCHANS:
1171 return cosa->nchannels;
1172 case COSAIOBMSET:
1173 if (!capable(CAP_SYS_RAWIO))
1174 return -EACCES;
1175 if (is_8bit(cosa))
1176 return -EINVAL;
1177 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1178 return -EINVAL;
1179 cosa->busmaster = arg;
1180 return 0;
1181 case COSAIOBMGET:
1182 return cosa->busmaster;
1183 }
1184 return -ENOIOCTLCMD;
1185}
1186
1187static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1188{
1189 int rv;
1190 struct channel_data *chan = dev_to_chan(dev);
1191 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1192 (unsigned long)ifr->ifr_data);
1193 if (rv != -ENOIOCTLCMD)
1194 return rv;
1195 return hdlc_ioctl(dev, ifr, cmd);
1196}
1197
1198static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1199 unsigned long arg)
1200{
1201 struct channel_data *channel = file->private_data;
1202 struct cosa_data *cosa;
1203 long ret;
1204
1205 mutex_lock(&cosa_chardev_mutex);
1206 cosa = channel->cosa;
1207 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1208 mutex_unlock(&cosa_chardev_mutex);
1209 return ret;
1210}
1211
1212
1213/*---------- HW layer interface ---------- */
1214
1215/*
1216 * The higher layer can bind itself to the HW layer by setting the callbacks
1217 * in the channel_data structure and by using these routines.
1218 */
1219static void cosa_enable_rx(struct channel_data *chan)
1220{
1221 struct cosa_data *cosa = chan->cosa;
1222
1223 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1224 put_driver_status(cosa);
1225}
1226
1227static void cosa_disable_rx(struct channel_data *chan)
1228{
1229 struct cosa_data *cosa = chan->cosa;
1230
1231 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1232 put_driver_status(cosa);
1233}
1234
1235/*
1236 * FIXME: This routine probably should check for cosa_start_tx() called when
1237 * the previous transmit is still unfinished. In this case the non-zero
1238 * return value should indicate to the caller that the queuing(sp?) up
1239 * the transmit has failed.
1240 */
1241static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1242{
1243 struct cosa_data *cosa = chan->cosa;
1244 unsigned long flags;
1245#ifdef DEBUG_DATA
1246 int i;
1247
1248 pr_info("cosa%dc%d: starting tx(0x%x)",
1249 chan->cosa->num, chan->num, len);
1250 for (i=0; i<len; i++)
1251 pr_cont(" %02x", buf[i]&0xff);
1252 pr_cont("\n");
1253#endif
1254 spin_lock_irqsave(&cosa->lock, flags);
1255 chan->txbuf = buf;
1256 chan->txsize = len;
1257 if (len > COSA_MTU)
1258 chan->txsize = COSA_MTU;
1259 spin_unlock_irqrestore(&cosa->lock, flags);
1260
1261 /* Tell the firmware we are ready */
1262 set_bit(chan->num, &cosa->txbitmap);
1263 put_driver_status(cosa);
1264
1265 return 0;
1266}
1267
1268static void put_driver_status(struct cosa_data *cosa)
1269{
1270 unsigned long flags;
1271 int status;
1272
1273 spin_lock_irqsave(&cosa->lock, flags);
1274
1275 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1276 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1277 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1278 &DRIVER_TXMAP_MASK : 0);
1279 if (!cosa->rxtx) {
1280 if (cosa->rxbitmap|cosa->txbitmap) {
1281 if (!cosa->enabled) {
1282 cosa_putstatus(cosa, SR_RX_INT_ENA);
1283#ifdef DEBUG_IO
1284 debug_status_out(cosa, SR_RX_INT_ENA);
1285#endif
1286 cosa->enabled = 1;
1287 }
1288 } else if (cosa->enabled) {
1289 cosa->enabled = 0;
1290 cosa_putstatus(cosa, 0);
1291#ifdef DEBUG_IO
1292 debug_status_out(cosa, 0);
1293#endif
1294 }
1295 cosa_putdata8(cosa, status);
1296#ifdef DEBUG_IO
1297 debug_data_cmd(cosa, status);
1298#endif
1299 }
1300 spin_unlock_irqrestore(&cosa->lock, flags);
1301}
1302
1303static void put_driver_status_nolock(struct cosa_data *cosa)
1304{
1305 int status;
1306
1307 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1308 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1309 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1310 &DRIVER_TXMAP_MASK : 0);
1311
1312 if (cosa->rxbitmap|cosa->txbitmap) {
1313 cosa_putstatus(cosa, SR_RX_INT_ENA);
1314#ifdef DEBUG_IO
1315 debug_status_out(cosa, SR_RX_INT_ENA);
1316#endif
1317 cosa->enabled = 1;
1318 } else {
1319 cosa_putstatus(cosa, 0);
1320#ifdef DEBUG_IO
1321 debug_status_out(cosa, 0);
1322#endif
1323 cosa->enabled = 0;
1324 }
1325 cosa_putdata8(cosa, status);
1326#ifdef DEBUG_IO
1327 debug_data_cmd(cosa, status);
1328#endif
1329}
1330
1331/*
1332 * The "kickme" function: When the DMA times out, this is called to
1333 * clean up the driver status.
1334 * FIXME: Preliminary support, the interface is probably wrong.
1335 */
1336static void cosa_kick(struct cosa_data *cosa)
1337{
1338 unsigned long flags, flags1;
1339 char *s = "(probably) IRQ";
1340
1341 if (test_bit(RXBIT, &cosa->rxtx))
1342 s = "RX DMA";
1343 if (test_bit(TXBIT, &cosa->rxtx))
1344 s = "TX DMA";
1345
1346 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1347 spin_lock_irqsave(&cosa->lock, flags);
1348 cosa->rxtx = 0;
1349
1350 flags1 = claim_dma_lock();
1351 disable_dma(cosa->dma);
1352 clear_dma_ff(cosa->dma);
1353 release_dma_lock(flags1);
1354
1355 /* FIXME: Anything else? */
1356 udelay(100);
1357 cosa_putstatus(cosa, 0);
1358 udelay(100);
1359 (void) cosa_getdata8(cosa);
1360 udelay(100);
1361 cosa_putdata8(cosa, 0);
1362 udelay(100);
1363 put_driver_status_nolock(cosa);
1364 spin_unlock_irqrestore(&cosa->lock, flags);
1365}
1366
1367/*
1368 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1369 * physical memory and doesn't span the 64k boundary. For now it seems
1370 * SKB's never do this, but we'll check this anyway.
1371 */
1372static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1373{
1374 static int count;
1375 unsigned long b = (unsigned long)buf;
1376 if (b+len >= MAX_DMA_ADDRESS)
1377 return 0;
1378 if ((b^ (b+len)) & 0x10000) {
1379 if (count++ < 5)
1380 pr_info("%s: packet spanning a 64k boundary\n",
1381 chan->name);
1382 return 0;
1383 }
1384 return 1;
1385}
1386
1387
1388/* ---------- The SRP/COSA ROM monitor functions ---------- */
1389
1390/*
1391 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1392 * drivers need to say 4-digit hex number meaning start address of the microcode
1393 * separated by a single space. Monitor replies by saying " =". Now driver
1394 * has to write 4-digit hex number meaning the last byte address ended
1395 * by a single space. Monitor has to reply with a space. Now the download
1396 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1397 */
1398static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1399{
1400 int i;
1401
1402 if (put_wait_data(cosa, 'w') == -1) return -1;
1403 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1404 if (get_wait_data(cosa) != '=') return -3;
1405
1406 if (puthexnumber(cosa, address) < 0) return -4;
1407 if (put_wait_data(cosa, ' ') == -1) return -10;
1408 if (get_wait_data(cosa) != ' ') return -11;
1409 if (get_wait_data(cosa) != '=') return -12;
1410
1411 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1412 if (put_wait_data(cosa, ' ') == -1) return -18;
1413 if (get_wait_data(cosa) != ' ') return -19;
1414
1415 while (length--) {
1416 char c;
1417#ifndef SRP_DOWNLOAD_AT_BOOT
1418 if (get_user(c, microcode))
1419 return -23; /* ??? */
1420#else
1421 c = *microcode;
1422#endif
1423 if (put_wait_data(cosa, c) == -1)
1424 return -20;
1425 microcode++;
1426 }
1427
1428 if (get_wait_data(cosa) != '\r') return -21;
1429 if (get_wait_data(cosa) != '\n') return -22;
1430 if (get_wait_data(cosa) != '.') return -23;
1431#if 0
1432 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1433#endif
1434 return 0;
1435}
1436
1437
1438/*
1439 * Starting microcode is done via the "g" command of the SRP monitor.
1440 * The chat should be the following: "g" "g=" "<addr><CR>"
1441 * "<CR><CR><LF><CR><LF>".
1442 */
1443static int startmicrocode(struct cosa_data *cosa, int address)
1444{
1445 if (put_wait_data(cosa, 'g') == -1) return -1;
1446 if (get_wait_data(cosa) != 'g') return -2;
1447 if (get_wait_data(cosa) != '=') return -3;
1448
1449 if (puthexnumber(cosa, address) < 0) return -4;
1450 if (put_wait_data(cosa, '\r') == -1) return -5;
1451
1452 if (get_wait_data(cosa) != '\r') return -6;
1453 if (get_wait_data(cosa) != '\r') return -7;
1454 if (get_wait_data(cosa) != '\n') return -8;
1455 if (get_wait_data(cosa) != '\r') return -9;
1456 if (get_wait_data(cosa) != '\n') return -10;
1457#if 0
1458 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1459#endif
1460 return 0;
1461}
1462
1463/*
1464 * Reading memory is done via the "r" command of the SRP monitor.
1465 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1466 * Then driver can read the data and the conversation is finished
1467 * by SRP monitor sending "<CR><LF>." (dot at the end).
1468 *
1469 * This routine is not needed during the normal operation and serves
1470 * for debugging purposes only.
1471 */
1472static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1473{
1474 if (put_wait_data(cosa, 'r') == -1) return -1;
1475 if ((get_wait_data(cosa)) != 'r') return -2;
1476 if ((get_wait_data(cosa)) != '=') return -3;
1477
1478 if (puthexnumber(cosa, address) < 0) return -4;
1479 if (put_wait_data(cosa, ' ') == -1) return -5;
1480 if (get_wait_data(cosa) != ' ') return -6;
1481 if (get_wait_data(cosa) != '=') return -7;
1482
1483 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1484 if (put_wait_data(cosa, ' ') == -1) return -9;
1485 if (get_wait_data(cosa) != ' ') return -10;
1486
1487 while (length--) {
1488 char c;
1489 int i;
1490 if ((i=get_wait_data(cosa)) == -1) {
1491 pr_info("0x%04x bytes remaining\n", length);
1492 return -11;
1493 }
1494 c=i;
1495#if 1
1496 if (put_user(c, microcode))
1497 return -23; /* ??? */
1498#else
1499 *microcode = c;
1500#endif
1501 microcode++;
1502 }
1503
1504 if (get_wait_data(cosa) != '\r') return -21;
1505 if (get_wait_data(cosa) != '\n') return -22;
1506 if (get_wait_data(cosa) != '.') return -23;
1507#if 0
1508 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1509#endif
1510 return 0;
1511}
1512
1513/*
1514 * This function resets the device and reads the initial prompt
1515 * of the device's ROM monitor.
1516 */
1517static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1518{
1519 int i=0, id=0, prev=0, curr=0;
1520
1521 /* Reset the card ... */
1522 cosa_putstatus(cosa, 0);
1523 cosa_getdata8(cosa);
1524 cosa_putstatus(cosa, SR_RST);
1525 msleep(500);
1526 /* Disable all IRQs from the card */
1527 cosa_putstatus(cosa, 0);
1528
1529 /*
1530 * Try to read the ID string. The card then prints out the
1531 * identification string ended by the "\n\x2e".
1532 *
1533 * The following loop is indexed through i (instead of id)
1534 * to avoid looping forever when for any reason
1535 * the port returns '\r', '\n' or '\x2e' permanently.
1536 */
1537 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1538 if ((curr = get_wait_data(cosa)) == -1) {
1539 return -1;
1540 }
1541 curr &= 0xff;
1542 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1543 idstring[id++] = curr;
1544 if (curr == 0x2e && prev == '\n')
1545 break;
1546 }
1547 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1548 idstring[id] = '\0';
1549 return id;
1550}
1551
1552
1553/* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1554
1555/*
1556 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1557 * bit to be set in a loop. It should be used in the exceptional cases
1558 * only (for example when resetting the card or downloading the firmware.
1559 */
1560static int get_wait_data(struct cosa_data *cosa)
1561{
1562 int retries = 1000;
1563
1564 while (--retries) {
1565 /* read data and return them */
1566 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1567 short r;
1568 r = cosa_getdata8(cosa);
1569#if 0
1570 pr_info("get_wait_data returning after %d retries\n",
1571 999-retries);
1572#endif
1573 return r;
1574 }
1575 /* sleep if not ready to read */
1576 schedule_timeout_interruptible(1);
1577 }
1578 pr_info("timeout in get_wait_data (status 0x%x)\n",
1579 cosa_getstatus(cosa));
1580 return -1;
1581}
1582
1583/*
1584 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1585 * bit to be set in a loop. It should be used in the exceptional cases
1586 * only (for example when resetting the card or downloading the firmware).
1587 */
1588static int put_wait_data(struct cosa_data *cosa, int data)
1589{
1590 int retries = 1000;
1591 while (--retries) {
1592 /* read data and return them */
1593 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1594 cosa_putdata8(cosa, data);
1595#if 0
1596 pr_info("Putdata: %d retries\n", 999-retries);
1597#endif
1598 return 0;
1599 }
1600#if 0
1601 /* sleep if not ready to read */
1602 schedule_timeout_interruptible(1);
1603#endif
1604 }
1605 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1606 cosa->num, cosa_getstatus(cosa));
1607 return -1;
1608}
1609
1610/*
1611 * The following routine puts the hexadecimal number into the SRP monitor
1612 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1613 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1614 * (-2,-4,-6,-8) means that reading echo failed.
1615 */
1616static int puthexnumber(struct cosa_data *cosa, int number)
1617{
1618 char temp[5];
1619 int i;
1620
1621 /* Well, I should probably replace this by something faster. */
1622 sprintf(temp, "%04X", number);
1623 for (i=0; i<4; i++) {
1624 if (put_wait_data(cosa, temp[i]) == -1) {
1625 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1626 cosa->num, i);
1627 return -1-2*i;
1628 }
1629 if (get_wait_data(cosa) != temp[i]) {
1630 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1631 cosa->num, i);
1632 return -2-2*i;
1633 }
1634 }
1635 return 0;
1636}
1637
1638
1639/* ---------- Interrupt routines ---------- */
1640
1641/*
1642 * There are three types of interrupt:
1643 * At the beginning of transmit - this handled is in tx_interrupt(),
1644 * at the beginning of receive - it is in rx_interrupt() and
1645 * at the end of transmit/receive - it is the eot_interrupt() function.
1646 * These functions are multiplexed by cosa_interrupt() according to the
1647 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1648 * separate functions to make it more readable. These functions are inline,
1649 * so there should be no overhead of function call.
1650 *
1651 * In the COSA bus-master mode, we need to tell the card the address of a
1652 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1653 * It's time to use the bottom half :-(
1654 */
1655
1656/*
1657 * Transmit interrupt routine - called when COSA is willing to obtain
1658 * data from the OS. The most tricky part of the routine is selection
1659 * of channel we (OS) want to send packet for. For SRP we should probably
1660 * use the round-robin approach. The newer COSA firmwares have a simple
1661 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1662 * channel 0 or 1 doesn't want to receive data.
1663 *
1664 * It seems there is a bug in COSA firmware (need to trace it further):
1665 * When the driver status says that the kernel has no more data for transmit
1666 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1667 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1668 * the TX interrupt but does not mark the channel as ready-to-transmit.
1669 * The fix seems to be to push the packet to COSA despite its request.
1670 * We first try to obey the card's opinion, and then fall back to forced TX.
1671 */
1672static inline void tx_interrupt(struct cosa_data *cosa, int status)
1673{
1674 unsigned long flags, flags1;
1675#ifdef DEBUG_IRQS
1676 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1677#endif
1678 spin_lock_irqsave(&cosa->lock, flags);
1679 set_bit(TXBIT, &cosa->rxtx);
1680 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1681 /* flow control, see the comment above */
1682 int i=0;
1683 if (!cosa->txbitmap) {
1684 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1685 cosa->name);
1686 put_driver_status_nolock(cosa);
1687 clear_bit(TXBIT, &cosa->rxtx);
1688 spin_unlock_irqrestore(&cosa->lock, flags);
1689 return;
1690 }
1691 while (1) {
1692 cosa->txchan++;
1693 i++;
1694 if (cosa->txchan >= cosa->nchannels)
1695 cosa->txchan = 0;
1696 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1697 continue;
1698 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1699 break;
1700 /* in second pass, accept first ready-to-TX channel */
1701 if (i > cosa->nchannels) {
1702 /* Can be safely ignored */
1703#ifdef DEBUG_IRQS
1704 printk(KERN_DEBUG "%s: Forcing TX "
1705 "to not-ready channel %d\n",
1706 cosa->name, cosa->txchan);
1707#endif
1708 break;
1709 }
1710 }
1711
1712 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1713 if (cosa_dma_able(cosa->chan+cosa->txchan,
1714 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1715 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1716 } else {
1717 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1718 cosa->txsize);
1719 cosa->txbuf = cosa->bouncebuf;
1720 }
1721 }
1722
1723 if (is_8bit(cosa)) {
1724 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1725 cosa_putstatus(cosa, SR_TX_INT_ENA);
1726 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1727 ((cosa->txsize >> 8) & 0x1f));
1728#ifdef DEBUG_IO
1729 debug_status_out(cosa, SR_TX_INT_ENA);
1730 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1731 ((cosa->txsize >> 8) & 0x1f));
1732 debug_data_in(cosa, cosa_getdata8(cosa));
1733#else
1734 cosa_getdata8(cosa);
1735#endif
1736 set_bit(IRQBIT, &cosa->rxtx);
1737 spin_unlock_irqrestore(&cosa->lock, flags);
1738 return;
1739 } else {
1740 clear_bit(IRQBIT, &cosa->rxtx);
1741 cosa_putstatus(cosa, 0);
1742 cosa_putdata8(cosa, cosa->txsize&0xff);
1743#ifdef DEBUG_IO
1744 debug_status_out(cosa, 0);
1745 debug_data_out(cosa, cosa->txsize&0xff);
1746#endif
1747 }
1748 } else {
1749 cosa_putstatus(cosa, SR_TX_INT_ENA);
1750 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1751 | (cosa->txsize & 0x1fff));
1752#ifdef DEBUG_IO
1753 debug_status_out(cosa, SR_TX_INT_ENA);
1754 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1755 | (cosa->txsize & 0x1fff));
1756 debug_data_in(cosa, cosa_getdata8(cosa));
1757 debug_status_out(cosa, 0);
1758#else
1759 cosa_getdata8(cosa);
1760#endif
1761 cosa_putstatus(cosa, 0);
1762 }
1763
1764 if (cosa->busmaster) {
1765 unsigned long addr = virt_to_bus(cosa->txbuf);
1766 int count=0;
1767 pr_info("busmaster IRQ\n");
1768 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1769 count++;
1770 udelay(10);
1771 if (count > 1000) break;
1772 }
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);
1776
1777 count = 0;
1778 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1779 count++;
1780 if (count > 1000) break;
1781 udelay(10);
1782 }
1783 pr_info("ready after %d loops\n", count);
1784 cosa_putdata16(cosa, addr &0xffff);
1785 flags1 = claim_dma_lock();
1786 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1787 enable_dma(cosa->dma);
1788 release_dma_lock(flags1);
1789 } else {
1790 /* start the DMA */
1791 flags1 = claim_dma_lock();
1792 disable_dma(cosa->dma);
1793 clear_dma_ff(cosa->dma);
1794 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1795 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1796 set_dma_count(cosa->dma, cosa->txsize);
1797 enable_dma(cosa->dma);
1798 release_dma_lock(flags1);
1799 }
1800 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1801#ifdef DEBUG_IO
1802 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1803#endif
1804 spin_unlock_irqrestore(&cosa->lock, flags);
1805}
1806
1807static inline void rx_interrupt(struct cosa_data *cosa, int status)
1808{
1809 unsigned long flags;
1810#ifdef DEBUG_IRQS
1811 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1812#endif
1813
1814 spin_lock_irqsave(&cosa->lock, flags);
1815 set_bit(RXBIT, &cosa->rxtx);
1816
1817 if (is_8bit(cosa)) {
1818 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1819 set_bit(IRQBIT, &cosa->rxtx);
1820 put_driver_status_nolock(cosa);
1821 cosa->rxsize = cosa_getdata8(cosa) <<8;
1822#ifdef DEBUG_IO
1823 debug_data_in(cosa, cosa->rxsize >> 8);
1824#endif
1825 spin_unlock_irqrestore(&cosa->lock, flags);
1826 return;
1827 } else {
1828 clear_bit(IRQBIT, &cosa->rxtx);
1829 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1830#ifdef DEBUG_IO
1831 debug_data_in(cosa, cosa->rxsize & 0xff);
1832#endif
1833#if 0
1834 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1835 cosa->num, cosa->rxsize);
1836#endif
1837 }
1838 } else {
1839 cosa->rxsize = cosa_getdata16(cosa);
1840#ifdef DEBUG_IO
1841 debug_data_in(cosa, cosa->rxsize);
1842#endif
1843#if 0
1844 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1845 cosa->num, cosa->rxsize);
1846#endif
1847 }
1848 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1849 pr_warn("%s: rx for unknown channel (0x%04x)\n",
1850 cosa->name, cosa->rxsize);
1851 spin_unlock_irqrestore(&cosa->lock, flags);
1852 goto reject;
1853 }
1854 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1855 cosa->rxsize &= 0x1fff;
1856 spin_unlock_irqrestore(&cosa->lock, flags);
1857
1858 cosa->rxbuf = NULL;
1859 if (cosa->rxchan->setup_rx)
1860 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1861
1862 if (!cosa->rxbuf) {
1863reject: /* Reject the packet */
1864 pr_info("cosa%d: rejecting packet on channel %d\n",
1865 cosa->num, cosa->rxchan->num);
1866 cosa->rxbuf = cosa->bouncebuf;
1867 }
1868
1869 /* start the DMA */
1870 flags = claim_dma_lock();
1871 disable_dma(cosa->dma);
1872 clear_dma_ff(cosa->dma);
1873 set_dma_mode(cosa->dma, DMA_MODE_READ);
1874 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1875 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1876 } else {
1877 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1878 }
1879 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1880 enable_dma(cosa->dma);
1881 release_dma_lock(flags);
1882 spin_lock_irqsave(&cosa->lock, flags);
1883 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1884 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1885 cosa_putdata8(cosa, DRIVER_RX_READY);
1886#ifdef DEBUG_IO
1887 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1888 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1889 debug_data_cmd(cosa, DRIVER_RX_READY);
1890#endif
1891 spin_unlock_irqrestore(&cosa->lock, flags);
1892}
1893
1894static inline void eot_interrupt(struct cosa_data *cosa, int status)
1895{
1896 unsigned long flags, flags1;
1897 spin_lock_irqsave(&cosa->lock, flags);
1898 flags1 = claim_dma_lock();
1899 disable_dma(cosa->dma);
1900 clear_dma_ff(cosa->dma);
1901 release_dma_lock(flags1);
1902 if (test_bit(TXBIT, &cosa->rxtx)) {
1903 struct channel_data *chan = cosa->chan+cosa->txchan;
1904 if (chan->tx_done)
1905 if (chan->tx_done(chan, cosa->txsize))
1906 clear_bit(chan->num, &cosa->txbitmap);
1907 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1908#ifdef DEBUG_DATA
1909 {
1910 int i;
1911 pr_info("cosa%dc%d: done rx(0x%x)",
1912 cosa->num, cosa->rxchan->num, cosa->rxsize);
1913 for (i=0; i<cosa->rxsize; i++)
1914 pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1915 pr_cont("\n");
1916 }
1917#endif
1918 /* Packet for unknown channel? */
1919 if (cosa->rxbuf == cosa->bouncebuf)
1920 goto out;
1921 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1922 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1923 if (cosa->rxchan->rx_done)
1924 if (cosa->rxchan->rx_done(cosa->rxchan))
1925 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1926 } else {
1927 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1928 }
1929 /*
1930 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1931 * cleared anyway). We should do it as soon as possible
1932 * so that we can tell the COSA we are done and to give it a time
1933 * for recovery.
1934 */
1935out:
1936 cosa->rxtx = 0;
1937 put_driver_status_nolock(cosa);
1938 spin_unlock_irqrestore(&cosa->lock, flags);
1939}
1940
1941static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1942{
1943 unsigned status;
1944 int count = 0;
1945 struct cosa_data *cosa = cosa_;
1946again:
1947 status = cosa_getstatus(cosa);
1948#ifdef DEBUG_IRQS
1949 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1950#endif
1951#ifdef DEBUG_IO
1952 debug_status_in(cosa, status);
1953#endif
1954 switch (status & SR_CMD_FROM_SRP_MASK) {
1955 case SR_DOWN_REQUEST:
1956 tx_interrupt(cosa, status);
1957 break;
1958 case SR_UP_REQUEST:
1959 rx_interrupt(cosa, status);
1960 break;
1961 case SR_END_OF_TRANSFER:
1962 eot_interrupt(cosa, status);
1963 break;
1964 default:
1965 /* We may be too fast for SRP. Try to wait a bit more. */
1966 if (count++ < 100) {
1967 udelay(100);
1968 goto again;
1969 }
1970 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1971 cosa->num, status & 0xff, count);
1972 }
1973#ifdef DEBUG_IRQS
1974 if (count)
1975 pr_info("%s: %d-times got unknown status in IRQ\n",
1976 cosa->name, count);
1977 else
1978 pr_info("%s: returning from IRQ\n", cosa->name);
1979#endif
1980 return IRQ_HANDLED;
1981}
1982
1983
1984/* ---------- I/O debugging routines ---------- */
1985/*
1986 * These routines can be used to monitor COSA/SRP I/O and to printk()
1987 * the data being transferred on the data and status I/O port in a
1988 * readable way.
1989 */
1990
1991#ifdef DEBUG_IO
1992static void debug_status_in(struct cosa_data *cosa, int status)
1993{
1994 char *s;
1995 switch (status & SR_CMD_FROM_SRP_MASK) {
1996 case SR_UP_REQUEST:
1997 s = "RX_REQ";
1998 break;
1999 case SR_DOWN_REQUEST:
2000 s = "TX_REQ";
2001 break;
2002 case SR_END_OF_TRANSFER:
2003 s = "ET_REQ";
2004 break;
2005 default:
2006 s = "NO_REQ";
2007 break;
2008 }
2009 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2010 cosa->name,
2011 status,
2012 status & SR_USR_RQ ? "USR_RQ|" : "",
2013 status & SR_TX_RDY ? "TX_RDY|" : "",
2014 status & SR_RX_RDY ? "RX_RDY|" : "",
2015 s);
2016}
2017
2018static void debug_status_out(struct cosa_data *cosa, int status)
2019{
2020 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2021 cosa->name,
2022 status,
2023 status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2024 status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2025 status & SR_RST ? "RESET|" : "",
2026 status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2027 status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2028 status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
2029}
2030
2031static void debug_data_in(struct cosa_data *cosa, int data)
2032{
2033 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2034}
2035
2036static void debug_data_out(struct cosa_data *cosa, int data)
2037{
2038 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2039}
2040
2041static void debug_data_cmd(struct cosa_data *cosa, int data)
2042{
2043 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2044 cosa->name, data,
2045 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2046 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2047}
2048#endif
2049
2050/* EOF -- this file has not been truncated */
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
3
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>
6 */
7
8/* The driver for the SRP and COSA synchronous serial cards.
9 *
10 * HARDWARE INFO
11 *
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.
20 *
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.
24 *
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.
30 *
31 * Both types have downloadable firmware and communicate via ISA DMA.
32 * COSA can be also a bus-mastering device.
33 *
34 * SOFTWARE INFO
35 *
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.
40 *
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).
44 *
45 * AUTHOR
46 *
47 * The Linux driver was written by Jan "Yenya" Kasprzak <kas@fi.muni.cz>.
48 *
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 :-).
52 *
53 * THE AUTHOR USED THE FOLLOWING SOURCES WHEN PROGRAMMING THE DRIVER
54 *
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
60 */
61
62#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
63
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>
69#include <linux/fs.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>
79#include <asm/io.h>
80#include <asm/dma.h>
81#include <asm/byteorder.h>
82
83#undef COSA_SLOW_IO /* for testing purposes only */
84
85#include "cosa.h"
86
87/* Maximum length of the identification string. */
88#define COSA_MAX_ID_STRING 128
89
90/* Maximum length of the channel name */
91#define COSA_MAX_NAME (sizeof("cosaXXXcXXX") + 1)
92
93/* Per-channel data structure */
94
95struct channel_data {
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 */
102
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);
110
111 /* Character device parts */
112 struct mutex rlock;
113 struct semaphore wsem;
114 char *rxdata;
115 int rxsize;
116 wait_queue_head_t txwaitq, rxwaitq;
117 int tx_status, rx_status;
118
119 /* generic HDLC device parts */
120 struct net_device *netdev;
121 struct sk_buff *rx_skb, *tx_skb;
122};
123
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? */
128
129struct cosa_data {
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? */
141 int enabled;
142 int usage; /* usage count */
143 int txchan, txsize, rxsize;
144 struct channel_data *rxchan;
145 char *bouncebuf;
146 char *txbuf, *rxbuf;
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 */
151};
152
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.
155 */
156/* #define COSA_ISA_AUTOPROBE 1*/
157
158/* Character device major number. 117 was allocated for us.
159 * The value of 0 means to allocate a first free one.
160 */
161static DEFINE_MUTEX(cosa_chardev_mutex);
162static int cosa_major = 117;
163
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.
167 */
168#define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
169 * for the single card
170 */
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 :-(
173 */
174#define MAX_CARDS 16
175/* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
176
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 */
181
182/* for cosa->rxtx - indicates whether either transmit or receive is
183 * in progress. These values are mean number of the bit.
184 */
185#define TXBIT 0
186#define RXBIT 1
187#define IRQBIT 2
188
189#define COSA_MTU 2000 /* FIXME: I don't know this exactly */
190
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 */
194
195#define TX_TIMEOUT (5 * HZ)
196
197/* Maybe the following should be allocated dynamically */
198static struct cosa_data cosa_cards[MAX_CARDS];
199static int nr_cards;
200
201#ifdef COSA_ISA_AUTOPROBE
202static int io[MAX_CARDS + 1] = {0x220, 0x228, 0x210, 0x218, 0,};
203/* NOTE: DMA is not autoprobed!!! */
204static int dma[MAX_CARDS + 1] = {1, 7, 1, 7, 1, 7, 1, 7, 0,};
205#else
206static int io[MAX_CARDS + 1];
207static int dma[MAX_CARDS + 1];
208#endif
209/* IRQ can be safely autoprobed */
210static int irq[MAX_CARDS + 1] = {-1, -1, -1, -1, -1, -1, 0,};
211
212/* for class stuff*/
213static struct class *cosa_class;
214
215#ifdef MODULE
216module_param_hw_array(io, int, ioport, NULL, 0);
217MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
218module_param_hw_array(irq, int, irq, NULL, 0);
219MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
220module_param_hw_array(dma, int, dma, NULL, 0);
221MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
222
223MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
224MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
225MODULE_LICENSE("GPL");
226#endif
227
228/* I use this mainly for testing purposes */
229#ifdef COSA_SLOW_IO
230#define cosa_outb outb_p
231#define cosa_outw outw_p
232#define cosa_inb inb_p
233#define cosa_inw inw_p
234#else
235#define cosa_outb outb
236#define cosa_outw outw
237#define cosa_inb inb
238#define cosa_inw inw
239#endif
240
241#define is_8bit(cosa) (!((cosa)->datareg & 0x08))
242
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))
249
250/* Initialization stuff */
251static int cosa_probe(int ioaddr, int irq, int dma);
252
253/* HW interface */
254static void cosa_enable_rx(struct channel_data *chan);
255static void cosa_disable_rx(struct channel_data *chan);
256static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
257static void cosa_kick(struct cosa_data *cosa);
258static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
259
260/* Network device stuff */
261static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
262 unsigned short parity);
263static int cosa_net_open(struct net_device *d);
264static int cosa_net_close(struct net_device *d);
265static void cosa_net_timeout(struct net_device *d, unsigned int txqueue);
266static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
267static char *cosa_net_setup_rx(struct channel_data *channel, int size);
268static int cosa_net_rx_done(struct channel_data *channel);
269static int cosa_net_tx_done(struct channel_data *channel, int size);
270static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
271
272/* Character device */
273static char *chrdev_setup_rx(struct channel_data *channel, int size);
274static int chrdev_rx_done(struct channel_data *channel);
275static int chrdev_tx_done(struct channel_data *channel, int size);
276static ssize_t cosa_read(struct file *file,
277 char __user *buf, size_t count, loff_t *ppos);
278static ssize_t cosa_write(struct file *file,
279 const char __user *buf, size_t count, loff_t *ppos);
280static unsigned int cosa_poll(struct file *file, poll_table *poll);
281static int cosa_open(struct inode *inode, struct file *file);
282static int cosa_release(struct inode *inode, struct file *file);
283static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
284 unsigned long arg);
285#ifdef COSA_FASYNC_WORKING
286static int cosa_fasync(struct inode *inode, struct file *file, int on);
287#endif
288
289static const struct file_operations cosa_fops = {
290 .owner = THIS_MODULE,
291 .llseek = no_llseek,
292 .read = cosa_read,
293 .write = cosa_write,
294 .poll = cosa_poll,
295 .unlocked_ioctl = cosa_chardev_ioctl,
296 .open = cosa_open,
297 .release = cosa_release,
298#ifdef COSA_FASYNC_WORKING
299 .fasync = cosa_fasync,
300#endif
301};
302
303/* Ioctls */
304static int cosa_start(struct cosa_data *cosa, int address);
305static int cosa_reset(struct cosa_data *cosa);
306static int cosa_download(struct cosa_data *cosa, void __user *a);
307static int cosa_readmem(struct cosa_data *cosa, void __user *a);
308
309/* COSA/SRP ROM monitor */
310static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
311static int startmicrocode(struct cosa_data *cosa, int address);
312static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
313static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
314
315/* Auxiliary functions */
316static int get_wait_data(struct cosa_data *cosa);
317static int put_wait_data(struct cosa_data *cosa, int data);
318static int puthexnumber(struct cosa_data *cosa, int number);
319static void put_driver_status(struct cosa_data *cosa);
320static void put_driver_status_nolock(struct cosa_data *cosa);
321
322/* Interrupt handling */
323static irqreturn_t cosa_interrupt(int irq, void *cosa);
324
325/* I/O ops debugging */
326#ifdef DEBUG_IO
327static void debug_data_in(struct cosa_data *cosa, int data);
328static void debug_data_out(struct cosa_data *cosa, int data);
329static void debug_data_cmd(struct cosa_data *cosa, int data);
330static void debug_status_in(struct cosa_data *cosa, int status);
331static void debug_status_out(struct cosa_data *cosa, int status);
332#endif
333
334static inline struct channel_data *dev_to_chan(struct net_device *dev)
335{
336 return (struct channel_data *)dev_to_hdlc(dev)->priv;
337}
338
339/* ---------- Initialization stuff ---------- */
340
341static int __init cosa_init(void)
342{
343 int i, err = 0;
344
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);
348 err = -EIO;
349 goto out;
350 }
351 } else {
352 cosa_major = register_chrdev(0, "cosa", &cosa_fops);
353 if (!cosa_major) {
354 pr_warn("unable to register chardev\n");
355 err = -EIO;
356 goto out;
357 }
358 }
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]);
363 if (!nr_cards) {
364 pr_warn("no devices found\n");
365 unregister_chrdev(cosa_major, "cosa");
366 err = -ENODEV;
367 goto out;
368 }
369 cosa_class = class_create(THIS_MODULE, "cosa");
370 if (IS_ERR(cosa_class)) {
371 err = PTR_ERR(cosa_class);
372 goto out_chrdev;
373 }
374 for (i = 0; i < nr_cards; i++)
375 device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
376 "cosa%d", i);
377 err = 0;
378 goto out;
379
380out_chrdev:
381 unregister_chrdev(cosa_major, "cosa");
382out:
383 return err;
384}
385module_init(cosa_init);
386
387static void __exit cosa_exit(void)
388{
389 struct cosa_data *cosa;
390 int i;
391
392 for (i = 0; i < nr_cards; i++)
393 device_destroy(cosa_class, MKDEV(cosa_major, i));
394 class_destroy(cosa_class);
395
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);
402 }
403 /* Clean up the per-card data */
404 kfree(cosa->chan);
405 kfree(cosa->bouncebuf);
406 free_irq(cosa->irq, cosa);
407 free_dma(cosa->dma);
408 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
409 }
410 unregister_chrdev(cosa_major, "cosa");
411}
412module_exit(cosa_exit);
413
414static 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,
420};
421
422static int cosa_probe(int base, int irq, int dma)
423{
424 struct cosa_data *cosa = cosa_cards + nr_cards;
425 int i, err = 0;
426
427 memset(cosa, 0, sizeof(struct cosa_data));
428
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);
433 return -1;
434 }
435 /* I/O address should be between 0x100 and 0x3ff and should be
436 * multiple of 8.
437 */
438 if (base < 0x100 || base > 0x3ff || base & 0x7) {
439 pr_info("invalid I/O address 0x%x\n", base);
440 return -1;
441 }
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);
445 return -1;
446 }
447 /* and finally, on 16-bit COSA DMA should be 4-7 and
448 * I/O base should not be multiple of 0x10
449 */
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",
452 base, dma);
453 return -1;
454 }
455
456 cosa->dma = dma;
457 cosa->datareg = base;
458 cosa->statusreg = is_8bit(cosa) ? base + 1 : base + 2;
459 spin_lock_init(&cosa->lock);
460
461 if (!request_region(base, is_8bit(cosa) ? 2 : 4, "cosa"))
462 return -1;
463
464 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
465 printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
466 err = -1;
467 goto err_out;
468 }
469
470 /* Test the validity of identification string */
471 if (!strncmp(cosa->id_string, "SRP", 3)) {
472 cosa->type = "srp";
473 } else if (!strncmp(cosa->id_string, "COSA", 4)) {
474 cosa->type = is_8bit(cosa) ? "cosa8" : "cosa16";
475 } else {
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);
479#endif
480 err = -1;
481 goto err_out;
482 }
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);
487 return -1;
488 }
489
490 /* Now do IRQ autoprobe */
491 if (irq < 0) {
492 unsigned long irqs;
493/* pr_info("IRQ autoprobe\n"); */
494 irqs = probe_irq_on();
495 /* Enable interrupt on tx buffer empty (it sure is)
496 * really sure ?
497 * FIXME: When this code is not used as module, we should
498 * probably call udelay() instead of the interruptible sleep.
499 */
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 */
507 cosa_getdata8(cosa);
508
509 if (irq < 0) {
510 pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
511 irq, cosa->datareg);
512 err = -1;
513 goto err_out;
514 }
515 if (irq == 0) {
516 pr_info("no interrupt obtained (board at 0x%x)\n",
517 cosa->datareg);
518 /* return -1; */
519 }
520 }
521
522 cosa->irq = irq;
523 cosa->num = nr_cards;
524 cosa->usage = 0;
525 cosa->nchannels = 2; /* FIXME: how to determine this? */
526
527 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
528 err = -1;
529 goto err_out;
530 }
531 if (request_dma(cosa->dma, cosa->type)) {
532 err = -1;
533 goto err_out1;
534 }
535
536 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL | GFP_DMA);
537 if (!cosa->bouncebuf) {
538 err = -ENOMEM;
539 goto err_out2;
540 }
541 sprintf(cosa->name, "cosa%d", cosa->num);
542
543 /* Initialize the per-channel data */
544 cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
545 if (!cosa->chan) {
546 err = -ENOMEM;
547 goto err_out3;
548 }
549
550 for (i = 0; i < cosa->nchannels; i++) {
551 struct channel_data *chan = &cosa->chan[i];
552
553 chan->cosa = cosa;
554 chan->num = i;
555 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
556
557 /* Initialize the chardev data structures */
558 mutex_init(&chan->rlock);
559 sema_init(&chan->wsem, 1);
560
561 /* Register the network interface */
562 chan->netdev = alloc_hdlcdev(chan);
563 if (!chan->netdev) {
564 pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
565 err = -ENOMEM;
566 goto err_hdlcdev;
567 }
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);
576 if (err) {
577 netdev_warn(chan->netdev,
578 "register_hdlc_device() failed\n");
579 free_netdev(chan->netdev);
580 goto err_hdlcdev;
581 }
582 }
583
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);
587
588 return nr_cards++;
589
590err_hdlcdev:
591 while (i-- > 0) {
592 unregister_hdlc_device(cosa->chan[i].netdev);
593 free_netdev(cosa->chan[i].netdev);
594 }
595 kfree(cosa->chan);
596err_out3:
597 kfree(cosa->bouncebuf);
598err_out2:
599 free_dma(cosa->dma);
600err_out1:
601 free_irq(cosa->irq, cosa);
602err_out:
603 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
604 pr_notice("cosa%d: allocating resources failed\n", cosa->num);
605 return err;
606}
607
608/*---------- network device ---------- */
609
610static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
611 unsigned short parity)
612{
613 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
614 return 0;
615 return -EINVAL;
616}
617
618static int cosa_net_open(struct net_device *dev)
619{
620 struct channel_data *chan = dev_to_chan(dev);
621 int err;
622 unsigned long flags;
623
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);
627 return -EPERM;
628 }
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);
634 return -EBUSY;
635 }
636 chan->setup_rx = cosa_net_setup_rx;
637 chan->tx_done = cosa_net_tx_done;
638 chan->rx_done = cosa_net_rx_done;
639 chan->usage = -1;
640 chan->cosa->usage++;
641 spin_unlock_irqrestore(&chan->cosa->lock, flags);
642
643 err = hdlc_open(dev);
644 if (err) {
645 spin_lock_irqsave(&chan->cosa->lock, flags);
646 chan->usage = 0;
647 chan->cosa->usage--;
648 spin_unlock_irqrestore(&chan->cosa->lock, flags);
649 return err;
650 }
651
652 netif_start_queue(dev);
653 cosa_enable_rx(chan);
654 return 0;
655}
656
657static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
658 struct net_device *dev)
659{
660 struct channel_data *chan = dev_to_chan(dev);
661
662 netif_stop_queue(dev);
663
664 chan->tx_skb = skb;
665 cosa_start_tx(chan, skb->data, skb->len);
666 return NETDEV_TX_OK;
667}
668
669static void cosa_net_timeout(struct net_device *dev, unsigned int txqueue)
670{
671 struct channel_data *chan = dev_to_chan(dev);
672
673 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
674 chan->netdev->stats.rx_errors++;
675 chan->netdev->stats.rx_missed_errors++;
676 } else {
677 chan->netdev->stats.tx_errors++;
678 chan->netdev->stats.tx_aborted_errors++;
679 }
680 cosa_kick(chan->cosa);
681 if (chan->tx_skb) {
682 dev_kfree_skb(chan->tx_skb);
683 chan->tx_skb = NULL;
684 }
685 netif_wake_queue(dev);
686}
687
688static int cosa_net_close(struct net_device *dev)
689{
690 struct channel_data *chan = dev_to_chan(dev);
691 unsigned long flags;
692
693 netif_stop_queue(dev);
694 hdlc_close(dev);
695 cosa_disable_rx(chan);
696 spin_lock_irqsave(&chan->cosa->lock, flags);
697 if (chan->rx_skb) {
698 kfree_skb(chan->rx_skb);
699 chan->rx_skb = NULL;
700 }
701 if (chan->tx_skb) {
702 kfree_skb(chan->tx_skb);
703 chan->tx_skb = NULL;
704 }
705 chan->usage = 0;
706 chan->cosa->usage--;
707 spin_unlock_irqrestore(&chan->cosa->lock, flags);
708 return 0;
709}
710
711static char *cosa_net_setup_rx(struct channel_data *chan, int size)
712{
713 /* We can safely fall back to non-dma-able memory, because we have
714 * the cosa->bouncebuf pre-allocated.
715 */
716 kfree_skb(chan->rx_skb);
717 chan->rx_skb = dev_alloc_skb(size);
718 if (!chan->rx_skb) {
719 pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
720 chan->netdev->stats.rx_dropped++;
721 return NULL;
722 }
723 netif_trans_update(chan->netdev);
724 return skb_put(chan->rx_skb, size);
725}
726
727static int cosa_net_rx_done(struct channel_data *chan)
728{
729 if (!chan->rx_skb) {
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++;
733 return 0;
734 }
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);
741 chan->rx_skb = NULL;
742 return 0;
743}
744
745/* ARGSUSED */
746static int cosa_net_tx_done(struct channel_data *chan, int size)
747{
748 if (!chan->tx_skb) {
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++;
752 return 1;
753 }
754 dev_consume_skb_irq(chan->tx_skb);
755 chan->tx_skb = NULL;
756 chan->netdev->stats.tx_packets++;
757 chan->netdev->stats.tx_bytes += size;
758 netif_wake_queue(chan->netdev);
759 return 1;
760}
761
762/*---------- Character device ---------- */
763
764static ssize_t cosa_read(struct file *file,
765 char __user *buf, size_t count, loff_t *ppos)
766{
767 DECLARE_WAITQUEUE(wait, current);
768 unsigned long flags;
769 struct channel_data *chan = file->private_data;
770 struct cosa_data *cosa = chan->cosa;
771 char *kbuf;
772
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);
776 return -EPERM;
777 }
778 if (mutex_lock_interruptible(&chan->rlock))
779 return -ERESTARTSYS;
780
781 chan->rxdata = kmalloc(COSA_MTU, GFP_DMA | GFP_KERNEL);
782 if (!chan->rxdata) {
783 mutex_unlock(&chan->rlock);
784 return -ENOMEM;
785 }
786
787 chan->rx_status = 0;
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);
794 schedule();
795 spin_lock_irqsave(&cosa->lock, flags);
796 if (signal_pending(current) && chan->rx_status == 0) {
797 chan->rx_status = 1;
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);
802 return -ERESTARTSYS;
803 }
804 }
805 remove_wait_queue(&chan->rxwaitq, &wait);
806 __set_current_state(TASK_RUNNING);
807 kbuf = chan->rxdata;
808 count = chan->rxsize;
809 spin_unlock_irqrestore(&cosa->lock, flags);
810 mutex_unlock(&chan->rlock);
811
812 if (copy_to_user(buf, kbuf, count)) {
813 kfree(kbuf);
814 return -EFAULT;
815 }
816 kfree(kbuf);
817 return count;
818}
819
820static char *chrdev_setup_rx(struct channel_data *chan, int size)
821{
822 /* Expect size <= COSA_MTU */
823 chan->rxsize = size;
824 return chan->rxdata;
825}
826
827static int chrdev_rx_done(struct channel_data *chan)
828{
829 if (chan->rx_status) { /* Reader has died */
830 kfree(chan->rxdata);
831 up(&chan->wsem);
832 }
833 chan->rx_status = 1;
834 wake_up_interruptible(&chan->rxwaitq);
835 return 1;
836}
837
838static ssize_t cosa_write(struct file *file,
839 const char __user *buf, size_t count, loff_t *ppos)
840{
841 DECLARE_WAITQUEUE(wait, current);
842 struct channel_data *chan = file->private_data;
843 struct cosa_data *cosa = chan->cosa;
844 unsigned long flags;
845 char *kbuf;
846
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);
850 return -EPERM;
851 }
852 if (down_interruptible(&chan->wsem))
853 return -ERESTARTSYS;
854
855 if (count > COSA_MTU)
856 count = COSA_MTU;
857
858 /* Allocate the buffer */
859 kbuf = kmalloc(count, GFP_KERNEL | GFP_DMA);
860 if (!kbuf) {
861 up(&chan->wsem);
862 return -ENOMEM;
863 }
864 if (copy_from_user(kbuf, buf, count)) {
865 up(&chan->wsem);
866 kfree(kbuf);
867 return -EFAULT;
868 }
869 chan->tx_status = 0;
870 cosa_start_tx(chan, kbuf, count);
871
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);
877 schedule();
878 spin_lock_irqsave(&cosa->lock, flags);
879 if (signal_pending(current) && chan->tx_status == 0) {
880 chan->tx_status = 1;
881 remove_wait_queue(&chan->txwaitq, &wait);
882 __set_current_state(TASK_RUNNING);
883 chan->tx_status = 1;
884 spin_unlock_irqrestore(&cosa->lock, flags);
885 up(&chan->wsem);
886 kfree(kbuf);
887 return -ERESTARTSYS;
888 }
889 }
890 remove_wait_queue(&chan->txwaitq, &wait);
891 __set_current_state(TASK_RUNNING);
892 up(&chan->wsem);
893 spin_unlock_irqrestore(&cosa->lock, flags);
894 kfree(kbuf);
895 return count;
896}
897
898static int chrdev_tx_done(struct channel_data *chan, int size)
899{
900 if (chan->tx_status) { /* Writer was interrupted */
901 kfree(chan->txbuf);
902 up(&chan->wsem);
903 }
904 chan->tx_status = 1;
905 wake_up_interruptible(&chan->txwaitq);
906 return 1;
907}
908
909static __poll_t cosa_poll(struct file *file, poll_table *poll)
910{
911 pr_info("cosa_poll is here\n");
912 return 0;
913}
914
915static int cosa_open(struct inode *inode, struct file *file)
916{
917 struct cosa_data *cosa;
918 struct channel_data *chan;
919 unsigned long flags;
920 int n;
921 int ret = 0;
922
923 mutex_lock(&cosa_chardev_mutex);
924 n = iminor(file_inode(file)) >> CARD_MINOR_BITS;
925 if (n >= nr_cards) {
926 ret = -ENODEV;
927 goto out;
928 }
929 cosa = cosa_cards + n;
930
931 n = iminor(file_inode(file)) & ((1 << CARD_MINOR_BITS) - 1);
932 if (n >= cosa->nchannels) {
933 ret = -ENODEV;
934 goto out;
935 }
936 chan = cosa->chan + n;
937
938 file->private_data = chan;
939
940 spin_lock_irqsave(&cosa->lock, flags);
941
942 if (chan->usage < 0) { /* in netdev mode */
943 spin_unlock_irqrestore(&cosa->lock, flags);
944 ret = -EBUSY;
945 goto out;
946 }
947 cosa->usage++;
948 chan->usage++;
949
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);
954out:
955 mutex_unlock(&cosa_chardev_mutex);
956 return ret;
957}
958
959static int cosa_release(struct inode *inode, struct file *file)
960{
961 struct channel_data *channel = file->private_data;
962 struct cosa_data *cosa;
963 unsigned long flags;
964
965 cosa = channel->cosa;
966 spin_lock_irqsave(&cosa->lock, flags);
967 cosa->usage--;
968 channel->usage--;
969 spin_unlock_irqrestore(&cosa->lock, flags);
970 return 0;
971}
972
973#ifdef COSA_FASYNC_WORKING
974static struct fasync_struct *fasync[256] = { NULL, };
975
976/* To be done ... */
977static int cosa_fasync(struct inode *inode, struct file *file, int on)
978{
979 int port = iminor(inode);
980
981 return fasync_helper(inode, file, on, &fasync[port]);
982}
983#endif
984
985/* ---------- Ioctls ---------- */
986
987/* Ioctl subroutines can safely be made inline, because they are called
988 * only from cosa_ioctl().
989 */
990static inline int cosa_reset(struct cosa_data *cosa)
991{
992 char idstring[COSA_MAX_ID_STRING];
993
994 if (cosa->usage > 1)
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);
1000 return -EIO;
1001 }
1002 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1003 cosa->firmware_status |= COSA_FW_RESET;
1004 return 0;
1005}
1006
1007/* High-level function to download data into COSA memory. Calls download() */
1008static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1009{
1010 struct cosa_download d;
1011 int i;
1012
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);
1019 return -EPERM;
1020 }
1021
1022 if (copy_from_user(&d, arg, sizeof(d)))
1023 return -EFAULT;
1024
1025 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1026 return -EINVAL;
1027 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1028 return -EINVAL;
1029
1030 /* If something fails, force the user to reset the card */
1031 cosa->firmware_status &= ~(COSA_FW_RESET | COSA_FW_DOWNLOAD);
1032
1033 i = download(cosa, d.code, d.len, d.addr);
1034 if (i < 0) {
1035 pr_notice("cosa%d: microcode download failed: %d\n",
1036 cosa->num, i);
1037 return -EIO;
1038 }
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;
1042 return 0;
1043}
1044
1045/* High-level function to read COSA memory. Calls readmem() */
1046static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1047{
1048 struct cosa_download d;
1049 int i;
1050
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);
1057 return -EPERM;
1058 }
1059
1060 if (copy_from_user(&d, arg, sizeof(d)))
1061 return -EFAULT;
1062
1063 /* If something fails, force the user to reset the card */
1064 cosa->firmware_status &= ~COSA_FW_RESET;
1065
1066 i = readmem(cosa, d.code, d.len, d.addr);
1067 if (i < 0) {
1068 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1069 return -EIO;
1070 }
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;
1074 return 0;
1075}
1076
1077/* High-level function to start microcode. Calls startmicrocode(). */
1078static inline int cosa_start(struct cosa_data *cosa, int address)
1079{
1080 int i;
1081
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);
1085
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);
1090 return -EPERM;
1091 }
1092 cosa->firmware_status &= ~COSA_FW_RESET;
1093 i = startmicrocode(cosa, address);
1094 if (i < 0) {
1095 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1096 cosa->num, address, i);
1097 return -EIO;
1098 }
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;
1102 return 0;
1103}
1104
1105/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1106static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1107{
1108 int l = strlen(cosa->id_string) + 1;
1109
1110 if (copy_to_user(string, cosa->id_string, l))
1111 return -EFAULT;
1112 return l;
1113}
1114
1115/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1116static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1117{
1118 int l = strlen(cosa->type) + 1;
1119
1120 if (copy_to_user(string, cosa->type, l))
1121 return -EFAULT;
1122 return l;
1123}
1124
1125static int cosa_ioctl_common(struct cosa_data *cosa,
1126 struct channel_data *channel, unsigned int cmd,
1127 unsigned long arg)
1128{
1129 void __user *argp = (void __user *)arg;
1130
1131 switch (cmd) {
1132 case COSAIORSET: /* Reset the device */
1133 if (!capable(CAP_NET_ADMIN))
1134 return -EACCES;
1135 return cosa_reset(cosa);
1136 case COSAIOSTRT: /* Start the firmware */
1137 if (!capable(CAP_SYS_RAWIO))
1138 return -EACCES;
1139 return cosa_start(cosa, arg);
1140 case COSAIODOWNLD: /* Download the firmware */
1141 if (!capable(CAP_SYS_RAWIO))
1142 return -EACCES;
1143
1144 return cosa_download(cosa, argp);
1145 case COSAIORMEM:
1146 if (!capable(CAP_SYS_RAWIO))
1147 return -EACCES;
1148 return cosa_readmem(cosa, argp);
1149 case COSAIORTYPE:
1150 return cosa_gettype(cosa, argp);
1151 case COSAIORIDSTR:
1152 return cosa_getidstr(cosa, argp);
1153 case COSAIONRCARDS:
1154 return nr_cards;
1155 case COSAIONRCHANS:
1156 return cosa->nchannels;
1157 case COSAIOBMSET:
1158 if (!capable(CAP_SYS_RAWIO))
1159 return -EACCES;
1160 if (is_8bit(cosa))
1161 return -EINVAL;
1162 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1163 return -EINVAL;
1164 cosa->busmaster = arg;
1165 return 0;
1166 case COSAIOBMGET:
1167 return cosa->busmaster;
1168 }
1169 return -ENOIOCTLCMD;
1170}
1171
1172static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1173{
1174 int rv;
1175 struct channel_data *chan = dev_to_chan(dev);
1176
1177 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1178 (unsigned long)ifr->ifr_data);
1179 if (rv != -ENOIOCTLCMD)
1180 return rv;
1181 return hdlc_ioctl(dev, ifr, cmd);
1182}
1183
1184static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1185 unsigned long arg)
1186{
1187 struct channel_data *channel = file->private_data;
1188 struct cosa_data *cosa;
1189 long ret;
1190
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);
1195 return ret;
1196}
1197
1198/*---------- HW layer interface ---------- */
1199
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.
1202 */
1203static void cosa_enable_rx(struct channel_data *chan)
1204{
1205 struct cosa_data *cosa = chan->cosa;
1206
1207 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1208 put_driver_status(cosa);
1209}
1210
1211static void cosa_disable_rx(struct channel_data *chan)
1212{
1213 struct cosa_data *cosa = chan->cosa;
1214
1215 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1216 put_driver_status(cosa);
1217}
1218
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.
1223 */
1224static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1225{
1226 struct cosa_data *cosa = chan->cosa;
1227 unsigned long flags;
1228#ifdef DEBUG_DATA
1229 int i;
1230
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);
1235 pr_cont("\n");
1236#endif
1237 spin_lock_irqsave(&cosa->lock, flags);
1238 chan->txbuf = buf;
1239 chan->txsize = len;
1240 if (len > COSA_MTU)
1241 chan->txsize = COSA_MTU;
1242 spin_unlock_irqrestore(&cosa->lock, flags);
1243
1244 /* Tell the firmware we are ready */
1245 set_bit(chan->num, &cosa->txbitmap);
1246 put_driver_status(cosa);
1247
1248 return 0;
1249}
1250
1251static void put_driver_status(struct cosa_data *cosa)
1252{
1253 unsigned long flags;
1254 int status;
1255
1256 spin_lock_irqsave(&cosa->lock, flags);
1257
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);
1262 if (!cosa->rxtx) {
1263 if (cosa->rxbitmap | cosa->txbitmap) {
1264 if (!cosa->enabled) {
1265 cosa_putstatus(cosa, SR_RX_INT_ENA);
1266#ifdef DEBUG_IO
1267 debug_status_out(cosa, SR_RX_INT_ENA);
1268#endif
1269 cosa->enabled = 1;
1270 }
1271 } else if (cosa->enabled) {
1272 cosa->enabled = 0;
1273 cosa_putstatus(cosa, 0);
1274#ifdef DEBUG_IO
1275 debug_status_out(cosa, 0);
1276#endif
1277 }
1278 cosa_putdata8(cosa, status);
1279#ifdef DEBUG_IO
1280 debug_data_cmd(cosa, status);
1281#endif
1282 }
1283 spin_unlock_irqrestore(&cosa->lock, flags);
1284}
1285
1286static void put_driver_status_nolock(struct cosa_data *cosa)
1287{
1288 int status;
1289
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);
1294
1295 if (cosa->rxbitmap | cosa->txbitmap) {
1296 cosa_putstatus(cosa, SR_RX_INT_ENA);
1297#ifdef DEBUG_IO
1298 debug_status_out(cosa, SR_RX_INT_ENA);
1299#endif
1300 cosa->enabled = 1;
1301 } else {
1302 cosa_putstatus(cosa, 0);
1303#ifdef DEBUG_IO
1304 debug_status_out(cosa, 0);
1305#endif
1306 cosa->enabled = 0;
1307 }
1308 cosa_putdata8(cosa, status);
1309#ifdef DEBUG_IO
1310 debug_data_cmd(cosa, status);
1311#endif
1312}
1313
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.
1317 */
1318static void cosa_kick(struct cosa_data *cosa)
1319{
1320 unsigned long flags, flags1;
1321 char *s = "(probably) IRQ";
1322
1323 if (test_bit(RXBIT, &cosa->rxtx))
1324 s = "RX DMA";
1325 if (test_bit(TXBIT, &cosa->rxtx))
1326 s = "TX DMA";
1327
1328 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1329 spin_lock_irqsave(&cosa->lock, flags);
1330 cosa->rxtx = 0;
1331
1332 flags1 = claim_dma_lock();
1333 disable_dma(cosa->dma);
1334 clear_dma_ff(cosa->dma);
1335 release_dma_lock(flags1);
1336
1337 /* FIXME: Anything else? */
1338 udelay(100);
1339 cosa_putstatus(cosa, 0);
1340 udelay(100);
1341 (void)cosa_getdata8(cosa);
1342 udelay(100);
1343 cosa_putdata8(cosa, 0);
1344 udelay(100);
1345 put_driver_status_nolock(cosa);
1346 spin_unlock_irqrestore(&cosa->lock, flags);
1347}
1348
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.
1352 */
1353static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1354{
1355 static int count;
1356 unsigned long b = (unsigned long)buf;
1357
1358 if (b + len >= MAX_DMA_ADDRESS)
1359 return 0;
1360 if ((b ^ (b + len)) & 0x10000) {
1361 if (count++ < 5)
1362 pr_info("%s: packet spanning a 64k boundary\n",
1363 chan->name);
1364 return 0;
1365 }
1366 return 1;
1367}
1368
1369/* ---------- The SRP/COSA ROM monitor functions ---------- */
1370
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).
1377 */
1378static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1379{
1380 int i;
1381
1382 if (put_wait_data(cosa, 'w') == -1)
1383 return -1;
1384 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1385 if (get_wait_data(cosa) != '=')
1386 return -3;
1387
1388 if (puthexnumber(cosa, address) < 0)
1389 return -4;
1390 if (put_wait_data(cosa, ' ') == -1)
1391 return -10;
1392 if (get_wait_data(cosa) != ' ')
1393 return -11;
1394 if (get_wait_data(cosa) != '=')
1395 return -12;
1396
1397 if (puthexnumber(cosa, address + length - 1) < 0)
1398 return -13;
1399 if (put_wait_data(cosa, ' ') == -1)
1400 return -18;
1401 if (get_wait_data(cosa) != ' ')
1402 return -19;
1403
1404 while (length--) {
1405 char c;
1406#ifndef SRP_DOWNLOAD_AT_BOOT
1407 if (get_user(c, microcode))
1408 return -23; /* ??? */
1409#else
1410 c = *microcode;
1411#endif
1412 if (put_wait_data(cosa, c) == -1)
1413 return -20;
1414 microcode++;
1415 }
1416
1417 if (get_wait_data(cosa) != '\r')
1418 return -21;
1419 if (get_wait_data(cosa) != '\n')
1420 return -22;
1421 if (get_wait_data(cosa) != '.')
1422 return -23;
1423#if 0
1424 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1425#endif
1426 return 0;
1427}
1428
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>".
1432 */
1433static int startmicrocode(struct cosa_data *cosa, int address)
1434{
1435 if (put_wait_data(cosa, 'g') == -1)
1436 return -1;
1437 if (get_wait_data(cosa) != 'g')
1438 return -2;
1439 if (get_wait_data(cosa) != '=')
1440 return -3;
1441
1442 if (puthexnumber(cosa, address) < 0)
1443 return -4;
1444 if (put_wait_data(cosa, '\r') == -1)
1445 return -5;
1446
1447 if (get_wait_data(cosa) != '\r')
1448 return -6;
1449 if (get_wait_data(cosa) != '\r')
1450 return -7;
1451 if (get_wait_data(cosa) != '\n')
1452 return -8;
1453 if (get_wait_data(cosa) != '\r')
1454 return -9;
1455 if (get_wait_data(cosa) != '\n')
1456 return -10;
1457#if 0
1458 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1459#endif
1460 return 0;
1461}
1462
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).
1467 *
1468 * This routine is not needed during the normal operation and serves
1469 * for debugging purposes only.
1470 */
1471static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1472{
1473 if (put_wait_data(cosa, 'r') == -1)
1474 return -1;
1475 if ((get_wait_data(cosa)) != 'r')
1476 return -2;
1477 if ((get_wait_data(cosa)) != '=')
1478 return -3;
1479
1480 if (puthexnumber(cosa, address) < 0)
1481 return -4;
1482 if (put_wait_data(cosa, ' ') == -1)
1483 return -5;
1484 if (get_wait_data(cosa) != ' ')
1485 return -6;
1486 if (get_wait_data(cosa) != '=')
1487 return -7;
1488
1489 if (puthexnumber(cosa, address + length - 1) < 0)
1490 return -8;
1491 if (put_wait_data(cosa, ' ') == -1)
1492 return -9;
1493 if (get_wait_data(cosa) != ' ')
1494 return -10;
1495
1496 while (length--) {
1497 char c;
1498 int i;
1499
1500 i = get_wait_data(cosa);
1501 if (i == -1) {
1502 pr_info("0x%04x bytes remaining\n", length);
1503 return -11;
1504 }
1505 c = i;
1506#if 1
1507 if (put_user(c, microcode))
1508 return -23; /* ??? */
1509#else
1510 *microcode = c;
1511#endif
1512 microcode++;
1513 }
1514
1515 if (get_wait_data(cosa) != '\r')
1516 return -21;
1517 if (get_wait_data(cosa) != '\n')
1518 return -22;
1519 if (get_wait_data(cosa) != '.')
1520 return -23;
1521#if 0
1522 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1523#endif
1524 return 0;
1525}
1526
1527/* This function resets the device and reads the initial prompt
1528 * of the device's ROM monitor.
1529 */
1530static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1531{
1532 int i = 0, id = 0, prev = 0, curr = 0;
1533
1534 /* Reset the card ... */
1535 cosa_putstatus(cosa, 0);
1536 cosa_getdata8(cosa);
1537 cosa_putstatus(cosa, SR_RST);
1538 msleep(500);
1539 /* Disable all IRQs from the card */
1540 cosa_putstatus(cosa, 0);
1541
1542 /* Try to read the ID string. The card then prints out the
1543 * identification string ended by the "\n\x2e".
1544 *
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.
1548 */
1549 for (i = 0; i < COSA_MAX_ID_STRING - 1; i++, prev = curr) {
1550 curr = get_wait_data(cosa);
1551 if (curr == -1)
1552 return -1;
1553
1554 curr &= 0xff;
1555 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1556 idstring[id++] = curr;
1557 if (curr == 0x2e && prev == '\n')
1558 break;
1559 }
1560 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1561 idstring[id] = '\0';
1562 return id;
1563}
1564
1565/* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1566
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.
1570 */
1571static int get_wait_data(struct cosa_data *cosa)
1572{
1573 int retries = 1000;
1574
1575 while (--retries) {
1576 /* read data and return them */
1577 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1578 short r;
1579
1580 r = cosa_getdata8(cosa);
1581#if 0
1582 pr_info("get_wait_data returning after %d retries\n",
1583 999 - retries);
1584#endif
1585 return r;
1586 }
1587 /* sleep if not ready to read */
1588 schedule_timeout_interruptible(1);
1589 }
1590 pr_info("timeout in get_wait_data (status 0x%x)\n",
1591 cosa_getstatus(cosa));
1592 return -1;
1593}
1594
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).
1598 */
1599static int put_wait_data(struct cosa_data *cosa, int data)
1600{
1601 int retries = 1000;
1602
1603 while (--retries) {
1604 /* read data and return them */
1605 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1606 cosa_putdata8(cosa, data);
1607#if 0
1608 pr_info("Putdata: %d retries\n", 999 - retries);
1609#endif
1610 return 0;
1611 }
1612#if 0
1613 /* sleep if not ready to read */
1614 schedule_timeout_interruptible(1);
1615#endif
1616 }
1617 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1618 cosa->num, cosa_getstatus(cosa));
1619 return -1;
1620}
1621
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.
1626 */
1627static int puthexnumber(struct cosa_data *cosa, int number)
1628{
1629 char temp[5];
1630 int i;
1631
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",
1637 cosa->num, i);
1638 return -1 - 2 * i;
1639 }
1640 if (get_wait_data(cosa) != temp[i]) {
1641 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1642 cosa->num, i);
1643 return -2 - 2 * i;
1644 }
1645 }
1646 return 0;
1647}
1648
1649/* ---------- Interrupt routines ---------- */
1650
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.
1659 *
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 :-(
1663 */
1664
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.
1671 *
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.
1679 */
1680static inline void tx_interrupt(struct cosa_data *cosa, int status)
1681{
1682 unsigned long flags, flags1;
1683#ifdef DEBUG_IRQS
1684 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1685#endif
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 */
1690 int i = 0;
1691
1692 if (!cosa->txbitmap) {
1693 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1694 cosa->name);
1695 put_driver_status_nolock(cosa);
1696 clear_bit(TXBIT, &cosa->rxtx);
1697 spin_unlock_irqrestore(&cosa->lock, flags);
1698 return;
1699 }
1700 while (1) {
1701 cosa->txchan++;
1702 i++;
1703 if (cosa->txchan >= cosa->nchannels)
1704 cosa->txchan = 0;
1705 if (!(cosa->txbitmap & (1 << cosa->txchan)))
1706 continue;
1707 if (~status &
1708 (1 << (cosa->txchan + DRIVER_TXMAP_SHIFT)))
1709 break;
1710 /* in second pass, accept first ready-to-TX channel */
1711 if (i > cosa->nchannels) {
1712 /* Can be safely ignored */
1713#ifdef DEBUG_IRQS
1714 printk(KERN_DEBUG "%s: Forcing TX "
1715 "to not-ready channel %d\n",
1716 cosa->name, cosa->txchan);
1717#endif
1718 break;
1719 }
1720 }
1721
1722 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1723 if (cosa_dma_able(cosa->chan + cosa->txchan,
1724 cosa->chan[cosa->txchan].txbuf,
1725 cosa->txsize)) {
1726 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1727 } else {
1728 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1729 cosa->txsize);
1730 cosa->txbuf = cosa->bouncebuf;
1731 }
1732 }
1733
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));
1739#ifdef DEBUG_IO
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));
1744#else
1745 cosa_getdata8(cosa);
1746#endif
1747 set_bit(IRQBIT, &cosa->rxtx);
1748 spin_unlock_irqrestore(&cosa->lock, flags);
1749 return;
1750 } else {
1751 clear_bit(IRQBIT, &cosa->rxtx);
1752 cosa_putstatus(cosa, 0);
1753 cosa_putdata8(cosa, cosa->txsize & 0xff);
1754#ifdef DEBUG_IO
1755 debug_status_out(cosa, 0);
1756 debug_data_out(cosa, cosa->txsize & 0xff);
1757#endif
1758 }
1759 } else {
1760 cosa_putstatus(cosa, SR_TX_INT_ENA);
1761 cosa_putdata16(cosa, ((cosa->txchan << 13) & 0xe000)
1762 | (cosa->txsize & 0x1fff));
1763#ifdef DEBUG_IO
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);
1769#else
1770 cosa_getdata8(cosa);
1771#endif
1772 cosa_putstatus(cosa, 0);
1773 }
1774
1775 if (cosa->busmaster) {
1776 unsigned long addr = virt_to_bus(cosa->txbuf);
1777 int count = 0;
1778
1779 pr_info("busmaster IRQ\n");
1780 while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1781 count++;
1782 udelay(10);
1783 if (count > 1000)
1784 break;
1785 }
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);
1789
1790 count = 0;
1791 while (!(cosa_getstatus(cosa) & SR_TX_RDY)) {
1792 count++;
1793 if (count > 1000)
1794 break;
1795 udelay(10);
1796 }
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);
1803 } else {
1804 /* start the DMA */
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);
1813 }
1814 cosa_putstatus(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1815#ifdef DEBUG_IO
1816 debug_status_out(cosa, SR_TX_DMA_ENA | SR_USR_INT_ENA);
1817#endif
1818 spin_unlock_irqrestore(&cosa->lock, flags);
1819}
1820
1821static inline void rx_interrupt(struct cosa_data *cosa, int status)
1822{
1823 unsigned long flags;
1824#ifdef DEBUG_IRQS
1825 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1826#endif
1827
1828 spin_lock_irqsave(&cosa->lock, flags);
1829 set_bit(RXBIT, &cosa->rxtx);
1830
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;
1836#ifdef DEBUG_IO
1837 debug_data_in(cosa, cosa->rxsize >> 8);
1838#endif
1839 spin_unlock_irqrestore(&cosa->lock, flags);
1840 return;
1841 } else {
1842 clear_bit(IRQBIT, &cosa->rxtx);
1843 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1844#ifdef DEBUG_IO
1845 debug_data_in(cosa, cosa->rxsize & 0xff);
1846#endif
1847#if 0
1848 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1849 cosa->num, cosa->rxsize);
1850#endif
1851 }
1852 } else {
1853 cosa->rxsize = cosa_getdata16(cosa);
1854#ifdef DEBUG_IO
1855 debug_data_in(cosa, cosa->rxsize);
1856#endif
1857#if 0
1858 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1859 cosa->num, cosa->rxsize);
1860#endif
1861 }
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);
1866 goto reject;
1867 }
1868 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1869 cosa->rxsize &= 0x1fff;
1870 spin_unlock_irqrestore(&cosa->lock, flags);
1871
1872 cosa->rxbuf = NULL;
1873 if (cosa->rxchan->setup_rx)
1874 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1875
1876 if (!cosa->rxbuf) {
1877reject: /* 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;
1881 }
1882
1883 /* start the DMA */
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));
1890 else
1891 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1892
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);
1900#ifdef DEBUG_IO
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);
1904#endif
1905 spin_unlock_irqrestore(&cosa->lock, flags);
1906}
1907
1908static inline void eot_interrupt(struct cosa_data *cosa, int status)
1909{
1910 unsigned long flags, flags1;
1911
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;
1919
1920 if (chan->tx_done)
1921 if (chan->tx_done(chan, cosa->txsize))
1922 clear_bit(chan->num, &cosa->txbitmap);
1923 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1924#ifdef DEBUG_DATA
1925 {
1926 int i;
1927
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);
1932 pr_cont("\n");
1933 }
1934#endif
1935 /* Packet for unknown channel? */
1936 if (cosa->rxbuf == cosa->bouncebuf)
1937 goto out;
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);
1943 } else {
1944 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1945 }
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
1949 * for recovery.
1950 */
1951out:
1952 cosa->rxtx = 0;
1953 put_driver_status_nolock(cosa);
1954 spin_unlock_irqrestore(&cosa->lock, flags);
1955}
1956
1957static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1958{
1959 unsigned status;
1960 int count = 0;
1961 struct cosa_data *cosa = cosa_;
1962again:
1963 status = cosa_getstatus(cosa);
1964#ifdef DEBUG_IRQS
1965 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1966#endif
1967#ifdef DEBUG_IO
1968 debug_status_in(cosa, status);
1969#endif
1970 switch (status & SR_CMD_FROM_SRP_MASK) {
1971 case SR_DOWN_REQUEST:
1972 tx_interrupt(cosa, status);
1973 break;
1974 case SR_UP_REQUEST:
1975 rx_interrupt(cosa, status);
1976 break;
1977 case SR_END_OF_TRANSFER:
1978 eot_interrupt(cosa, status);
1979 break;
1980 default:
1981 /* We may be too fast for SRP. Try to wait a bit more. */
1982 if (count++ < 100) {
1983 udelay(100);
1984 goto again;
1985 }
1986 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1987 cosa->num, status & 0xff, count);
1988 }
1989#ifdef DEBUG_IRQS
1990 if (count)
1991 pr_info("%s: %d-times got unknown status in IRQ\n",
1992 cosa->name, count);
1993 else
1994 pr_info("%s: returning from IRQ\n", cosa->name);
1995#endif
1996 return IRQ_HANDLED;
1997}
1998
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
2002 * readable way.
2003 */
2004
2005#ifdef DEBUG_IO
2006static void debug_status_in(struct cosa_data *cosa, int status)
2007{
2008 char *s;
2009
2010 switch (status & SR_CMD_FROM_SRP_MASK) {
2011 case SR_UP_REQUEST:
2012 s = "RX_REQ";
2013 break;
2014 case SR_DOWN_REQUEST:
2015 s = "TX_REQ";
2016 break;
2017 case SR_END_OF_TRANSFER:
2018 s = "ET_REQ";
2019 break;
2020 default:
2021 s = "NO_REQ";
2022 break;
2023 }
2024 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2025 cosa->name,
2026 status,
2027 status & SR_USR_RQ ? "USR_RQ|" : "",
2028 status & SR_TX_RDY ? "TX_RDY|" : "",
2029 status & SR_RX_RDY ? "RX_RDY|" : "",
2030 s);
2031}
2032
2033static void debug_status_out(struct cosa_data *cosa, int status)
2034{
2035 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2036 cosa->name,
2037 status,
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");
2044}
2045
2046static void debug_data_in(struct cosa_data *cosa, int data)
2047{
2048 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2049}
2050
2051static void debug_data_out(struct cosa_data *cosa, int data)
2052{
2053 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2054}
2055
2056static void debug_data_cmd(struct cosa_data *cosa, int data)
2057{
2058 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2059 cosa->name, data,
2060 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2061 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2062}
2063#endif
2064
2065/* EOF -- this file has not been truncated */