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