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