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