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