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