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