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