blob: 2eceb1a24df223f2b44098346806f427f707741f [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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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);
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000282static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200283static 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);
Alan Cox2395f0e2009-10-14 05:22:24 +0000299static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
300 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#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,
Alan Cox2395f0e2009-10-14 05:22:24 +0000311 .unlocked_ioctl = cosa_chardev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-07-01 23:40:29 +0200564 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto err_out3;
566 }
Krzysztof Hałasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-07-01 23:40:29 +0200626/*---------- network device ---------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Krzysztof Hałasaaca25752008-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łasaaca25752008-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łasaaca25752008-07-01 23:40:29 +0200636static int cosa_net_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Krzysztof Hałasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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łasaaca25752008-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
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000675static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
676 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200678 struct channel_data *chan = dev_to_chan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 netif_stop_queue(dev);
681
682 chan->tx_skb = skb;
683 cosa_start_tx(chan, skb->data, skb->len);
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000684 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200687static void cosa_net_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200689 struct channel_data *chan = dev_to_chan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200692 chan->netdev->stats.rx_errors++;
693 chan->netdev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 } else {
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200695 chan->netdev->stats.tx_errors++;
696 chan->netdev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 cosa_kick(chan->cosa);
699 if (chan->tx_skb) {
700 dev_kfree_skb(chan->tx_skb);
701 chan->tx_skb = NULL;
702 }
703 netif_wake_queue(dev);
704}
705
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200706static int cosa_net_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200708 struct channel_data *chan = dev_to_chan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 unsigned long flags;
710
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200711 netif_stop_queue(dev);
712 hdlc_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 cosa_disable_rx(chan);
714 spin_lock_irqsave(&chan->cosa->lock, flags);
715 if (chan->rx_skb) {
716 kfree_skb(chan->rx_skb);
717 chan->rx_skb = NULL;
718 }
719 if (chan->tx_skb) {
720 kfree_skb(chan->tx_skb);
721 chan->tx_skb = NULL;
722 }
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200723 chan->usage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 chan->cosa->usage--;
725 spin_unlock_irqrestore(&chan->cosa->lock, flags);
726 return 0;
727}
728
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200729static char *cosa_net_setup_rx(struct channel_data *chan, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 /*
732 * We can safely fall back to non-dma-able memory, because we have
733 * the cosa->bouncebuf pre-allocated.
734 */
Wei Yongjunfbbfb982009-02-25 00:22:41 +0000735 kfree_skb(chan->rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 chan->rx_skb = dev_alloc_skb(size);
737 if (chan->rx_skb == NULL) {
738 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
739 chan->name);
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200740 chan->netdev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return NULL;
742 }
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200743 chan->netdev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return skb_put(chan->rx_skb, size);
745}
746
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200747static int cosa_net_rx_done(struct channel_data *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 if (!chan->rx_skb) {
750 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
751 chan->name);
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200752 chan->netdev->stats.rx_errors++;
753 chan->netdev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755 }
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200756 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
757 chan->rx_skb->dev = chan->netdev;
David S. Miller2f7826c2007-03-26 02:00:58 -0700758 skb_reset_mac_header(chan->rx_skb);
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200759 chan->netdev->stats.rx_packets++;
760 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 netif_rx(chan->rx_skb);
762 chan->rx_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
764}
765
766/* ARGSUSED */
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200767static int cosa_net_tx_done(struct channel_data *chan, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 if (!chan->tx_skb) {
770 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
771 chan->name);
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200772 chan->netdev->stats.tx_errors++;
773 chan->netdev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return 1;
775 }
776 dev_kfree_skb_irq(chan->tx_skb);
777 chan->tx_skb = NULL;
Krzysztof Hałasaaca25752008-07-01 23:40:29 +0200778 chan->netdev->stats.tx_packets++;
779 chan->netdev->stats.tx_bytes += size;
780 netif_wake_queue(chan->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 1;
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*---------- Character device ---------- */
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static ssize_t cosa_read(struct file *file,
787 char __user *buf, size_t count, loff_t *ppos)
788{
789 DECLARE_WAITQUEUE(wait, current);
790 unsigned long flags;
791 struct channel_data *chan = file->private_data;
792 struct cosa_data *cosa = chan->cosa;
793 char *kbuf;
794
795 if (!(cosa->firmware_status & COSA_FW_START)) {
796 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
797 cosa->name, cosa->firmware_status);
798 return -EPERM;
799 }
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800800 if (mutex_lock_interruptible(&chan->rlock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -ERESTARTSYS;
802
803 if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
804 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800805 mutex_unlock(&chan->rlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return -ENOMEM;
807 }
808
809 chan->rx_status = 0;
810 cosa_enable_rx(chan);
811 spin_lock_irqsave(&cosa->lock, flags);
812 add_wait_queue(&chan->rxwaitq, &wait);
813 while(!chan->rx_status) {
814 current->state = TASK_INTERRUPTIBLE;
815 spin_unlock_irqrestore(&cosa->lock, flags);
816 schedule();
817 spin_lock_irqsave(&cosa->lock, flags);
818 if (signal_pending(current) && chan->rx_status == 0) {
819 chan->rx_status = 1;
820 remove_wait_queue(&chan->rxwaitq, &wait);
821 current->state = TASK_RUNNING;
822 spin_unlock_irqrestore(&cosa->lock, flags);
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800823 mutex_unlock(&chan->rlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -ERESTARTSYS;
825 }
826 }
827 remove_wait_queue(&chan->rxwaitq, &wait);
828 current->state = TASK_RUNNING;
829 kbuf = chan->rxdata;
830 count = chan->rxsize;
831 spin_unlock_irqrestore(&cosa->lock, flags);
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800832 mutex_unlock(&chan->rlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 if (copy_to_user(buf, kbuf, count)) {
835 kfree(kbuf);
836 return -EFAULT;
837 }
838 kfree(kbuf);
839 return count;
840}
841
842static char *chrdev_setup_rx(struct channel_data *chan, int size)
843{
844 /* Expect size <= COSA_MTU */
845 chan->rxsize = size;
846 return chan->rxdata;
847}
848
849static int chrdev_rx_done(struct channel_data *chan)
850{
851 if (chan->rx_status) { /* Reader has died */
852 kfree(chan->rxdata);
853 up(&chan->wsem);
854 }
855 chan->rx_status = 1;
856 wake_up_interruptible(&chan->rxwaitq);
857 return 1;
858}
859
860
861static ssize_t cosa_write(struct file *file,
862 const char __user *buf, size_t count, loff_t *ppos)
863{
864 DECLARE_WAITQUEUE(wait, current);
865 struct channel_data *chan = file->private_data;
866 struct cosa_data *cosa = chan->cosa;
867 unsigned long flags;
868 char *kbuf;
869
870 if (!(cosa->firmware_status & COSA_FW_START)) {
871 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
872 cosa->name, cosa->firmware_status);
873 return -EPERM;
874 }
875 if (down_interruptible(&chan->wsem))
876 return -ERESTARTSYS;
877
878 if (count > COSA_MTU)
879 count = COSA_MTU;
880
881 /* Allocate the buffer */
882 if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
883 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
884 cosa->name);
885 up(&chan->wsem);
886 return -ENOMEM;
887 }
888 if (copy_from_user(kbuf, buf, count)) {
889 up(&chan->wsem);
890 kfree(kbuf);
891 return -EFAULT;
892 }
893 chan->tx_status=0;
894 cosa_start_tx(chan, kbuf, count);
895
896 spin_lock_irqsave(&cosa->lock, flags);
897 add_wait_queue(&chan->txwaitq, &wait);
898 while(!chan->tx_status) {
899 current->state = TASK_INTERRUPTIBLE;
900 spin_unlock_irqrestore(&cosa->lock, flags);
901 schedule();
902 spin_lock_irqsave(&cosa->lock, flags);
903 if (signal_pending(current) && chan->tx_status == 0) {
904 chan->tx_status = 1;
905 remove_wait_queue(&chan->txwaitq, &wait);
906 current->state = TASK_RUNNING;
907 chan->tx_status = 1;
908 spin_unlock_irqrestore(&cosa->lock, flags);
909 return -ERESTARTSYS;
910 }
911 }
912 remove_wait_queue(&chan->txwaitq, &wait);
913 current->state = TASK_RUNNING;
914 up(&chan->wsem);
915 spin_unlock_irqrestore(&cosa->lock, flags);
916 kfree(kbuf);
917 return count;
918}
919
920static int chrdev_tx_done(struct channel_data *chan, int size)
921{
922 if (chan->tx_status) { /* Writer was interrupted */
923 kfree(chan->txbuf);
924 up(&chan->wsem);
925 }
926 chan->tx_status = 1;
927 wake_up_interruptible(&chan->txwaitq);
928 return 1;
929}
930
931static unsigned int cosa_poll(struct file *file, poll_table *poll)
932{
933 printk(KERN_INFO "cosa_poll is here\n");
934 return 0;
935}
936
937static int cosa_open(struct inode *inode, struct file *file)
938{
939 struct cosa_data *cosa;
940 struct channel_data *chan;
941 unsigned long flags;
942 int n;
Jonathan Corbet09118102008-05-15 09:21:33 -0600943 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Jonathan Corbet09118102008-05-15 09:21:33 -0600945 lock_kernel();
Josef Sipekf04538c2006-12-08 02:36:57 -0800946 if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
Jonathan Corbet09118102008-05-15 09:21:33 -0600947 >= nr_cards) {
948 ret = -ENODEV;
949 goto out;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 cosa = cosa_cards+n;
952
Josef Sipekf04538c2006-12-08 02:36:57 -0800953 if ((n=iminor(file->f_path.dentry->d_inode)
Jonathan Corbet09118102008-05-15 09:21:33 -0600954 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
955 ret = -ENODEV;
956 goto out;
957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 chan = cosa->chan + n;
959
960 file->private_data = chan;
961
962 spin_lock_irqsave(&cosa->lock, flags);
963
964 if (chan->usage < 0) { /* in netdev mode */
965 spin_unlock_irqrestore(&cosa->lock, flags);
Jonathan Corbet09118102008-05-15 09:21:33 -0600966 ret = -EBUSY;
967 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 cosa->usage++;
970 chan->usage++;
971
972 chan->tx_done = chrdev_tx_done;
973 chan->setup_rx = chrdev_setup_rx;
974 chan->rx_done = chrdev_rx_done;
975 spin_unlock_irqrestore(&cosa->lock, flags);
Jonathan Corbet09118102008-05-15 09:21:33 -0600976out:
977 unlock_kernel();
978 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
981static int cosa_release(struct inode *inode, struct file *file)
982{
983 struct channel_data *channel = file->private_data;
984 struct cosa_data *cosa;
985 unsigned long flags;
986
987 cosa = channel->cosa;
988 spin_lock_irqsave(&cosa->lock, flags);
989 cosa->usage--;
990 channel->usage--;
991 spin_unlock_irqrestore(&cosa->lock, flags);
992 return 0;
993}
994
995#ifdef COSA_FASYNC_WORKING
996static struct fasync_struct *fasync[256] = { NULL, };
997
998/* To be done ... */
999static int cosa_fasync(struct inode *inode, struct file *file, int on)
1000{
1001 int port = iminor(inode);
Jonathan Corbet60aa4922009-02-01 14:52:56 -07001002
1003 return fasync_helper(inode, file, on, &fasync[port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005#endif
1006
1007
1008/* ---------- Ioctls ---------- */
1009
1010/*
1011 * Ioctl subroutines can safely be made inline, because they are called
1012 * only from cosa_ioctl().
1013 */
1014static inline int cosa_reset(struct cosa_data *cosa)
1015{
1016 char idstring[COSA_MAX_ID_STRING];
1017 if (cosa->usage > 1)
1018 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1019 cosa->num, cosa->usage);
1020 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1021 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1022 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1023 return -EIO;
1024 }
1025 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1026 idstring);
1027 cosa->firmware_status |= COSA_FW_RESET;
1028 return 0;
1029}
1030
1031/* High-level function to download data into COSA memory. Calls download() */
1032static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1033{
1034 struct cosa_download d;
1035 int i;
1036
1037 if (cosa->usage > 1)
1038 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1039 cosa->name, cosa->usage);
1040 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1041 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1042 cosa->name, cosa->firmware_status);
1043 return -EPERM;
1044 }
1045
1046 if (copy_from_user(&d, arg, sizeof(d)))
1047 return -EFAULT;
1048
1049 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1050 return -EINVAL;
1051 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1052 return -EINVAL;
1053
1054
1055 /* If something fails, force the user to reset the card */
1056 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1057
1058 i = download(cosa, d.code, d.len, d.addr);
1059 if (i < 0) {
1060 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1061 cosa->num, i);
1062 return -EIO;
1063 }
1064 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1065 cosa->num, d.len, d.addr);
1066 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1067 return 0;
1068}
1069
1070/* High-level function to read COSA memory. Calls readmem() */
1071static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1072{
1073 struct cosa_download d;
1074 int i;
1075
1076 if (cosa->usage > 1)
1077 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1078 "cosa->usage > 1 (%d). Odd things may happen.\n",
1079 cosa->num, cosa->usage);
1080 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1081 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1082 cosa->name, cosa->firmware_status);
1083 return -EPERM;
1084 }
1085
1086 if (copy_from_user(&d, arg, sizeof(d)))
1087 return -EFAULT;
1088
1089 /* If something fails, force the user to reset the card */
1090 cosa->firmware_status &= ~COSA_FW_RESET;
1091
1092 i = readmem(cosa, d.code, d.len, d.addr);
1093 if (i < 0) {
1094 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1095 cosa->num, i);
1096 return -EIO;
1097 }
1098 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1099 cosa->num, d.len, d.addr);
1100 cosa->firmware_status |= COSA_FW_RESET;
1101 return 0;
1102}
1103
1104/* High-level function to start microcode. Calls startmicrocode(). */
1105static inline int cosa_start(struct cosa_data *cosa, int address)
1106{
1107 int i;
1108
1109 if (cosa->usage > 1)
1110 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1111 cosa->num, cosa->usage);
1112
1113 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1114 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1115 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1116 cosa->name, cosa->firmware_status);
1117 return -EPERM;
1118 }
1119 cosa->firmware_status &= ~COSA_FW_RESET;
1120 if ((i=startmicrocode(cosa, address)) < 0) {
1121 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1122 cosa->num, address, i);
1123 return -EIO;
1124 }
1125 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1126 cosa->num, address);
1127 cosa->startaddr = address;
1128 cosa->firmware_status |= COSA_FW_START;
1129 return 0;
1130}
1131
1132/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1133static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1134{
1135 int l = strlen(cosa->id_string)+1;
1136 if (copy_to_user(string, cosa->id_string, l))
1137 return -EFAULT;
1138 return l;
1139}
1140
1141/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1142static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1143{
1144 int l = strlen(cosa->type)+1;
1145 if (copy_to_user(string, cosa->type, l))
1146 return -EFAULT;
1147 return l;
1148}
1149
1150static int cosa_ioctl_common(struct cosa_data *cosa,
1151 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1152{
1153 void __user *argp = (void __user *)arg;
1154 switch(cmd) {
1155 case COSAIORSET: /* Reset the device */
1156 if (!capable(CAP_NET_ADMIN))
1157 return -EACCES;
1158 return cosa_reset(cosa);
1159 case COSAIOSTRT: /* Start the firmware */
1160 if (!capable(CAP_SYS_RAWIO))
1161 return -EACCES;
1162 return cosa_start(cosa, arg);
1163 case COSAIODOWNLD: /* Download the firmware */
1164 if (!capable(CAP_SYS_RAWIO))
1165 return -EACCES;
1166
1167 return cosa_download(cosa, argp);
1168 case COSAIORMEM:
1169 if (!capable(CAP_SYS_RAWIO))
1170 return -EACCES;
1171 return cosa_readmem(cosa, argp);
1172 case COSAIORTYPE:
1173 return cosa_gettype(cosa, argp);
1174 case COSAIORIDSTR:
1175 return cosa_getidstr(cosa, argp);
1176 case COSAIONRCARDS:
1177 return nr_cards;
1178 case COSAIONRCHANS:
1179 return cosa->nchannels;
1180 case COSAIOBMSET:
1181 if (!capable(CAP_SYS_RAWIO))
1182 return -EACCES;
1183 if (is_8bit(cosa))
1184 return -EINVAL;
1185 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1186 return -EINVAL;
1187 cosa->busmaster = arg;
1188 return 0;
1189 case COSAIOBMGET:
1190 return cosa->busmaster;
1191 }
1192 return -ENOIOCTLCMD;
1193}
1194
Krzysztof Hałasaaca25752008-07-01 23:40:29 +02001195static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 int rv;
Krzysztof Hałasaaca25752008-07-01 23:40:29 +02001198 struct channel_data *chan = dev_to_chan(dev);
1199 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1200 (unsigned long)ifr->ifr_data);
1201 if (rv != -ENOIOCTLCMD)
1202 return rv;
1203 return hdlc_ioctl(dev, ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Alan Cox2395f0e2009-10-14 05:22:24 +00001206static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1207 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 struct channel_data *channel = file->private_data;
Alan Cox2395f0e2009-10-14 05:22:24 +00001210 struct cosa_data *cosa;
1211 long ret;
1212
1213 lock_kernel();
1214 cosa = channel->cosa;
1215 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1216 unlock_kernel();
1217 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220
1221/*---------- HW layer interface ---------- */
1222
1223/*
1224 * The higher layer can bind itself to the HW layer by setting the callbacks
1225 * in the channel_data structure and by using these routines.
1226 */
1227static void cosa_enable_rx(struct channel_data *chan)
1228{
1229 struct cosa_data *cosa = chan->cosa;
1230
1231 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1232 put_driver_status(cosa);
1233}
1234
1235static void cosa_disable_rx(struct channel_data *chan)
1236{
1237 struct cosa_data *cosa = chan->cosa;
1238
1239 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1240 put_driver_status(cosa);
1241}
1242
1243/*
1244 * FIXME: This routine probably should check for cosa_start_tx() called when
1245 * the previous transmit is still unfinished. In this case the non-zero
1246 * return value should indicate to the caller that the queuing(sp?) up
1247 * the transmit has failed.
1248 */
1249static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1250{
1251 struct cosa_data *cosa = chan->cosa;
1252 unsigned long flags;
1253#ifdef DEBUG_DATA
1254 int i;
1255
1256 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1257 chan->num, len);
1258 for (i=0; i<len; i++)
1259 printk(" %02x", buf[i]&0xff);
1260 printk("\n");
1261#endif
1262 spin_lock_irqsave(&cosa->lock, flags);
1263 chan->txbuf = buf;
1264 chan->txsize = len;
1265 if (len > COSA_MTU)
1266 chan->txsize = COSA_MTU;
1267 spin_unlock_irqrestore(&cosa->lock, flags);
1268
1269 /* Tell the firmware we are ready */
1270 set_bit(chan->num, &cosa->txbitmap);
1271 put_driver_status(cosa);
1272
1273 return 0;
1274}
1275
1276static void put_driver_status(struct cosa_data *cosa)
1277{
1278 unsigned long flags;
1279 int status;
1280
1281 spin_lock_irqsave(&cosa->lock, flags);
1282
1283 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1284 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1285 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1286 &DRIVER_TXMAP_MASK : 0);
1287 if (!cosa->rxtx) {
1288 if (cosa->rxbitmap|cosa->txbitmap) {
1289 if (!cosa->enabled) {
1290 cosa_putstatus(cosa, SR_RX_INT_ENA);
1291#ifdef DEBUG_IO
1292 debug_status_out(cosa, SR_RX_INT_ENA);
1293#endif
1294 cosa->enabled = 1;
1295 }
1296 } else if (cosa->enabled) {
1297 cosa->enabled = 0;
1298 cosa_putstatus(cosa, 0);
1299#ifdef DEBUG_IO
1300 debug_status_out(cosa, 0);
1301#endif
1302 }
1303 cosa_putdata8(cosa, status);
1304#ifdef DEBUG_IO
1305 debug_data_cmd(cosa, status);
1306#endif
1307 }
1308 spin_unlock_irqrestore(&cosa->lock, flags);
1309}
1310
1311static void put_driver_status_nolock(struct cosa_data *cosa)
1312{
1313 int status;
1314
1315 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1316 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1317 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1318 &DRIVER_TXMAP_MASK : 0);
1319
1320 if (cosa->rxbitmap|cosa->txbitmap) {
1321 cosa_putstatus(cosa, SR_RX_INT_ENA);
1322#ifdef DEBUG_IO
1323 debug_status_out(cosa, SR_RX_INT_ENA);
1324#endif
1325 cosa->enabled = 1;
1326 } else {
1327 cosa_putstatus(cosa, 0);
1328#ifdef DEBUG_IO
1329 debug_status_out(cosa, 0);
1330#endif
1331 cosa->enabled = 0;
1332 }
1333 cosa_putdata8(cosa, status);
1334#ifdef DEBUG_IO
1335 debug_data_cmd(cosa, status);
1336#endif
1337}
1338
1339/*
1340 * The "kickme" function: When the DMA times out, this is called to
1341 * clean up the driver status.
1342 * FIXME: Preliminary support, the interface is probably wrong.
1343 */
1344static void cosa_kick(struct cosa_data *cosa)
1345{
1346 unsigned long flags, flags1;
1347 char *s = "(probably) IRQ";
1348
1349 if (test_bit(RXBIT, &cosa->rxtx))
1350 s = "RX DMA";
1351 if (test_bit(TXBIT, &cosa->rxtx))
1352 s = "TX DMA";
1353
1354 printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
1355 spin_lock_irqsave(&cosa->lock, flags);
1356 cosa->rxtx = 0;
1357
1358 flags1 = claim_dma_lock();
1359 disable_dma(cosa->dma);
1360 clear_dma_ff(cosa->dma);
1361 release_dma_lock(flags1);
1362
1363 /* FIXME: Anything else? */
1364 udelay(100);
1365 cosa_putstatus(cosa, 0);
1366 udelay(100);
1367 (void) cosa_getdata8(cosa);
1368 udelay(100);
1369 cosa_putdata8(cosa, 0);
1370 udelay(100);
1371 put_driver_status_nolock(cosa);
1372 spin_unlock_irqrestore(&cosa->lock, flags);
1373}
1374
1375/*
1376 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1377 * physical memory and doesn't span the 64k boundary. For now it seems
1378 * SKB's never do this, but we'll check this anyway.
1379 */
1380static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1381{
1382 static int count;
1383 unsigned long b = (unsigned long)buf;
1384 if (b+len >= MAX_DMA_ADDRESS)
1385 return 0;
1386 if ((b^ (b+len)) & 0x10000) {
1387 if (count++ < 5)
1388 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1389 chan->name);
1390 return 0;
1391 }
1392 return 1;
1393}
1394
1395
1396/* ---------- The SRP/COSA ROM monitor functions ---------- */
1397
1398/*
1399 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1400 * drivers need to say 4-digit hex number meaning start address of the microcode
1401 * separated by a single space. Monitor replies by saying " =". Now driver
1402 * has to write 4-digit hex number meaning the last byte address ended
1403 * by a single space. Monitor has to reply with a space. Now the download
1404 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1405 */
1406static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1407{
1408 int i;
1409
1410 if (put_wait_data(cosa, 'w') == -1) return -1;
1411 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1412 if (get_wait_data(cosa) != '=') return -3;
1413
1414 if (puthexnumber(cosa, address) < 0) return -4;
1415 if (put_wait_data(cosa, ' ') == -1) return -10;
1416 if (get_wait_data(cosa) != ' ') return -11;
1417 if (get_wait_data(cosa) != '=') return -12;
1418
1419 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1420 if (put_wait_data(cosa, ' ') == -1) return -18;
1421 if (get_wait_data(cosa) != ' ') return -19;
1422
1423 while (length--) {
1424 char c;
1425#ifndef SRP_DOWNLOAD_AT_BOOT
1426 if (get_user(c, microcode))
1427 return -23; /* ??? */
1428#else
1429 c = *microcode;
1430#endif
1431 if (put_wait_data(cosa, c) == -1)
1432 return -20;
1433 microcode++;
1434 }
1435
1436 if (get_wait_data(cosa) != '\r') return -21;
1437 if (get_wait_data(cosa) != '\n') return -22;
1438 if (get_wait_data(cosa) != '.') return -23;
1439#if 0
1440 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1441#endif
1442 return 0;
1443}
1444
1445
1446/*
1447 * Starting microcode is done via the "g" command of the SRP monitor.
1448 * The chat should be the following: "g" "g=" "<addr><CR>"
1449 * "<CR><CR><LF><CR><LF>".
1450 */
1451static int startmicrocode(struct cosa_data *cosa, int address)
1452{
1453 if (put_wait_data(cosa, 'g') == -1) return -1;
1454 if (get_wait_data(cosa) != 'g') return -2;
1455 if (get_wait_data(cosa) != '=') return -3;
1456
1457 if (puthexnumber(cosa, address) < 0) return -4;
1458 if (put_wait_data(cosa, '\r') == -1) return -5;
1459
1460 if (get_wait_data(cosa) != '\r') return -6;
1461 if (get_wait_data(cosa) != '\r') return -7;
1462 if (get_wait_data(cosa) != '\n') return -8;
1463 if (get_wait_data(cosa) != '\r') return -9;
1464 if (get_wait_data(cosa) != '\n') return -10;
1465#if 0
1466 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1467#endif
1468 return 0;
1469}
1470
1471/*
1472 * Reading memory is done via the "r" command of the SRP monitor.
1473 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1474 * Then driver can read the data and the conversation is finished
1475 * by SRP monitor sending "<CR><LF>." (dot at the end).
1476 *
1477 * This routine is not needed during the normal operation and serves
1478 * for debugging purposes only.
1479 */
1480static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1481{
1482 if (put_wait_data(cosa, 'r') == -1) return -1;
1483 if ((get_wait_data(cosa)) != 'r') return -2;
1484 if ((get_wait_data(cosa)) != '=') return -3;
1485
1486 if (puthexnumber(cosa, address) < 0) return -4;
1487 if (put_wait_data(cosa, ' ') == -1) return -5;
1488 if (get_wait_data(cosa) != ' ') return -6;
1489 if (get_wait_data(cosa) != '=') return -7;
1490
1491 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1492 if (put_wait_data(cosa, ' ') == -1) return -9;
1493 if (get_wait_data(cosa) != ' ') return -10;
1494
1495 while (length--) {
1496 char c;
1497 int i;
1498 if ((i=get_wait_data(cosa)) == -1) {
1499 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1500 length);
1501 return -11;
1502 }
1503 c=i;
1504#if 1
1505 if (put_user(c, microcode))
1506 return -23; /* ??? */
1507#else
1508 *microcode = c;
1509#endif
1510 microcode++;
1511 }
1512
1513 if (get_wait_data(cosa) != '\r') return -21;
1514 if (get_wait_data(cosa) != '\n') return -22;
1515 if (get_wait_data(cosa) != '.') return -23;
1516#if 0
1517 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1518#endif
1519 return 0;
1520}
1521
1522/*
1523 * This function resets the device and reads the initial prompt
1524 * of the device's ROM monitor.
1525 */
1526static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1527{
1528 int i=0, id=0, prev=0, curr=0;
1529
1530 /* Reset the card ... */
1531 cosa_putstatus(cosa, 0);
1532 cosa_getdata8(cosa);
1533 cosa_putstatus(cosa, SR_RST);
1534#ifdef MODULE
1535 msleep(500);
1536#else
1537 udelay(5*100000);
1538#endif
1539 /* Disable all IRQs from the card */
1540 cosa_putstatus(cosa, 0);
1541
1542 /*
1543 * Try to read the ID string. The card then prints out the
1544 * identification string ended by the "\n\x2e".
1545 *
1546 * The following loop is indexed through i (instead of id)
1547 * to avoid looping forever when for any reason
1548 * the port returns '\r', '\n' or '\x2e' permanently.
1549 */
1550 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1551 if ((curr = get_wait_data(cosa)) == -1) {
1552 return -1;
1553 }
1554 curr &= 0xff;
1555 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1556 idstring[id++] = curr;
1557 if (curr == 0x2e && prev == '\n')
1558 break;
1559 }
1560 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1561 idstring[id] = '\0';
1562 return id;
1563}
1564
1565
1566/* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1567
1568/*
1569 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1570 * bit to be set in a loop. It should be used in the exceptional cases
1571 * only (for example when resetting the card or downloading the firmware.
1572 */
1573static int get_wait_data(struct cosa_data *cosa)
1574{
1575 int retries = 1000;
1576
1577 while (--retries) {
1578 /* read data and return them */
1579 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1580 short r;
1581 r = cosa_getdata8(cosa);
1582#if 0
1583 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1584#endif
1585 return r;
1586 }
1587 /* sleep if not ready to read */
Nishanth Aravamudan3173c892005-09-11 02:09:55 -07001588 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1591 cosa_getstatus(cosa));
1592 return -1;
1593}
1594
1595/*
1596 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1597 * bit to be set in a loop. It should be used in the exceptional cases
1598 * only (for example when resetting the card or downloading the firmware).
1599 */
1600static int put_wait_data(struct cosa_data *cosa, int data)
1601{
1602 int retries = 1000;
1603 while (--retries) {
1604 /* read data and return them */
1605 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1606 cosa_putdata8(cosa, data);
1607#if 0
1608 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1609#endif
1610 return 0;
1611 }
1612#if 0
1613 /* sleep if not ready to read */
Nishanth Aravamudan3173c892005-09-11 02:09:55 -07001614 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615#endif
1616 }
1617 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1618 cosa->num, cosa_getstatus(cosa));
1619 return -1;
1620}
1621
1622/*
1623 * The following routine puts the hexadecimal number into the SRP monitor
1624 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1625 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1626 * (-2,-4,-6,-8) means that reading echo failed.
1627 */
1628static int puthexnumber(struct cosa_data *cosa, int number)
1629{
1630 char temp[5];
1631 int i;
1632
1633 /* Well, I should probably replace this by something faster. */
1634 sprintf(temp, "%04X", number);
1635 for (i=0; i<4; i++) {
1636 if (put_wait_data(cosa, temp[i]) == -1) {
1637 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1638 cosa->num, i);
1639 return -1-2*i;
1640 }
1641 if (get_wait_data(cosa) != temp[i]) {
1642 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1643 cosa->num, i);
1644 return -2-2*i;
1645 }
1646 }
1647 return 0;
1648}
1649
1650
1651/* ---------- Interrupt routines ---------- */
1652
1653/*
1654 * There are three types of interrupt:
1655 * At the beginning of transmit - this handled is in tx_interrupt(),
1656 * at the beginning of receive - it is in rx_interrupt() and
1657 * at the end of transmit/receive - it is the eot_interrupt() function.
1658 * These functions are multiplexed by cosa_interrupt() according to the
1659 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1660 * separate functions to make it more readable. These functions are inline,
1661 * so there should be no overhead of function call.
1662 *
1663 * In the COSA bus-master mode, we need to tell the card the address of a
1664 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1665 * It's time to use the bottom half :-(
1666 */
1667
1668/*
1669 * Transmit interrupt routine - called when COSA is willing to obtain
1670 * data from the OS. The most tricky part of the routine is selection
1671 * of channel we (OS) want to send packet for. For SRP we should probably
1672 * use the round-robin approach. The newer COSA firmwares have a simple
1673 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1674 * channel 0 or 1 doesn't want to receive data.
1675 *
1676 * It seems there is a bug in COSA firmware (need to trace it further):
1677 * When the driver status says that the kernel has no more data for transmit
1678 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1679 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1680 * the TX interrupt but does not mark the channel as ready-to-transmit.
1681 * The fix seems to be to push the packet to COSA despite its request.
1682 * We first try to obey the card's opinion, and then fall back to forced TX.
1683 */
1684static inline void tx_interrupt(struct cosa_data *cosa, int status)
1685{
1686 unsigned long flags, flags1;
1687#ifdef DEBUG_IRQS
1688 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1689 cosa->num, status);
1690#endif
1691 spin_lock_irqsave(&cosa->lock, flags);
1692 set_bit(TXBIT, &cosa->rxtx);
1693 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1694 /* flow control, see the comment above */
1695 int i=0;
1696 if (!cosa->txbitmap) {
1697 printk(KERN_WARNING "%s: No channel wants data "
1698 "in TX IRQ. Expect DMA timeout.",
1699 cosa->name);
1700 put_driver_status_nolock(cosa);
1701 clear_bit(TXBIT, &cosa->rxtx);
1702 spin_unlock_irqrestore(&cosa->lock, flags);
1703 return;
1704 }
1705 while(1) {
1706 cosa->txchan++;
1707 i++;
1708 if (cosa->txchan >= cosa->nchannels)
1709 cosa->txchan = 0;
1710 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1711 continue;
1712 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1713 break;
1714 /* in second pass, accept first ready-to-TX channel */
1715 if (i > cosa->nchannels) {
1716 /* Can be safely ignored */
1717#ifdef DEBUG_IRQS
1718 printk(KERN_DEBUG "%s: Forcing TX "
1719 "to not-ready channel %d\n",
1720 cosa->name, cosa->txchan);
1721#endif
1722 break;
1723 }
1724 }
1725
1726 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1727 if (cosa_dma_able(cosa->chan+cosa->txchan,
1728 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1729 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1730 } else {
1731 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1732 cosa->txsize);
1733 cosa->txbuf = cosa->bouncebuf;
1734 }
1735 }
1736
1737 if (is_8bit(cosa)) {
1738 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1739 cosa_putstatus(cosa, SR_TX_INT_ENA);
1740 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1741 ((cosa->txsize >> 8) & 0x1f));
1742#ifdef DEBUG_IO
1743 debug_status_out(cosa, SR_TX_INT_ENA);
1744 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1745 ((cosa->txsize >> 8) & 0x1f));
1746 debug_data_in(cosa, cosa_getdata8(cosa));
1747#else
1748 cosa_getdata8(cosa);
1749#endif
1750 set_bit(IRQBIT, &cosa->rxtx);
1751 spin_unlock_irqrestore(&cosa->lock, flags);
1752 return;
1753 } else {
1754 clear_bit(IRQBIT, &cosa->rxtx);
1755 cosa_putstatus(cosa, 0);
1756 cosa_putdata8(cosa, cosa->txsize&0xff);
1757#ifdef DEBUG_IO
1758 debug_status_out(cosa, 0);
1759 debug_data_out(cosa, cosa->txsize&0xff);
1760#endif
1761 }
1762 } else {
1763 cosa_putstatus(cosa, SR_TX_INT_ENA);
1764 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1765 | (cosa->txsize & 0x1fff));
1766#ifdef DEBUG_IO
1767 debug_status_out(cosa, SR_TX_INT_ENA);
1768 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1769 | (cosa->txsize & 0x1fff));
1770 debug_data_in(cosa, cosa_getdata8(cosa));
1771 debug_status_out(cosa, 0);
1772#else
1773 cosa_getdata8(cosa);
1774#endif
1775 cosa_putstatus(cosa, 0);
1776 }
1777
1778 if (cosa->busmaster) {
1779 unsigned long addr = virt_to_bus(cosa->txbuf);
1780 int count=0;
1781 printk(KERN_INFO "busmaster IRQ\n");
1782 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1783 count++;
1784 udelay(10);
1785 if (count > 1000) break;
1786 }
1787 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1788 printk(KERN_INFO "ready after %d loops\n", count);
1789 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1790
1791 count = 0;
1792 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1793 count++;
1794 if (count > 1000) break;
1795 udelay(10);
1796 }
1797 printk(KERN_INFO "ready after %d loops\n", count);
1798 cosa_putdata16(cosa, addr &0xffff);
1799 flags1 = claim_dma_lock();
1800 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1801 enable_dma(cosa->dma);
1802 release_dma_lock(flags1);
1803 } else {
1804 /* start the DMA */
1805 flags1 = claim_dma_lock();
1806 disable_dma(cosa->dma);
1807 clear_dma_ff(cosa->dma);
1808 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1809 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1810 set_dma_count(cosa->dma, cosa->txsize);
1811 enable_dma(cosa->dma);
1812 release_dma_lock(flags1);
1813 }
1814 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1815#ifdef DEBUG_IO
1816 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1817#endif
1818 spin_unlock_irqrestore(&cosa->lock, flags);
1819}
1820
1821static inline void rx_interrupt(struct cosa_data *cosa, int status)
1822{
1823 unsigned long flags;
1824#ifdef DEBUG_IRQS
1825 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1826#endif
1827
1828 spin_lock_irqsave(&cosa->lock, flags);
1829 set_bit(RXBIT, &cosa->rxtx);
1830
1831 if (is_8bit(cosa)) {
1832 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1833 set_bit(IRQBIT, &cosa->rxtx);
1834 put_driver_status_nolock(cosa);
1835 cosa->rxsize = cosa_getdata8(cosa) <<8;
1836#ifdef DEBUG_IO
1837 debug_data_in(cosa, cosa->rxsize >> 8);
1838#endif
1839 spin_unlock_irqrestore(&cosa->lock, flags);
1840 return;
1841 } else {
1842 clear_bit(IRQBIT, &cosa->rxtx);
1843 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1844#ifdef DEBUG_IO
1845 debug_data_in(cosa, cosa->rxsize & 0xff);
1846#endif
1847#if 0
1848 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1849 cosa->num, cosa->rxsize);
1850#endif
1851 }
1852 } else {
1853 cosa->rxsize = cosa_getdata16(cosa);
1854#ifdef DEBUG_IO
1855 debug_data_in(cosa, cosa->rxsize);
1856#endif
1857#if 0
1858 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1859 cosa->num, cosa->rxsize);
1860#endif
1861 }
1862 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1863 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1864 cosa->name, cosa->rxsize);
1865 spin_unlock_irqrestore(&cosa->lock, flags);
1866 goto reject;
1867 }
1868 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1869 cosa->rxsize &= 0x1fff;
1870 spin_unlock_irqrestore(&cosa->lock, flags);
1871
1872 cosa->rxbuf = NULL;
1873 if (cosa->rxchan->setup_rx)
1874 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1875
1876 if (!cosa->rxbuf) {
1877reject: /* Reject the packet */
1878 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1879 cosa->num, cosa->rxchan->num);
1880 cosa->rxbuf = cosa->bouncebuf;
1881 }
1882
1883 /* start the DMA */
1884 flags = claim_dma_lock();
1885 disable_dma(cosa->dma);
1886 clear_dma_ff(cosa->dma);
1887 set_dma_mode(cosa->dma, DMA_MODE_READ);
1888 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1889 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1890 } else {
1891 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1892 }
1893 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1894 enable_dma(cosa->dma);
1895 release_dma_lock(flags);
1896 spin_lock_irqsave(&cosa->lock, flags);
1897 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1898 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1899 cosa_putdata8(cosa, DRIVER_RX_READY);
1900#ifdef DEBUG_IO
1901 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1902 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1903 debug_data_cmd(cosa, DRIVER_RX_READY);
1904#endif
1905 spin_unlock_irqrestore(&cosa->lock, flags);
1906}
1907
1908static inline void eot_interrupt(struct cosa_data *cosa, int status)
1909{
1910 unsigned long flags, flags1;
1911 spin_lock_irqsave(&cosa->lock, flags);
1912 flags1 = claim_dma_lock();
1913 disable_dma(cosa->dma);
1914 clear_dma_ff(cosa->dma);
1915 release_dma_lock(flags1);
1916 if (test_bit(TXBIT, &cosa->rxtx)) {
1917 struct channel_data *chan = cosa->chan+cosa->txchan;
1918 if (chan->tx_done)
1919 if (chan->tx_done(chan, cosa->txsize))
1920 clear_bit(chan->num, &cosa->txbitmap);
1921 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1922#ifdef DEBUG_DATA
1923 {
1924 int i;
1925 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1926 cosa->rxchan->num, cosa->rxsize);
1927 for (i=0; i<cosa->rxsize; i++)
1928 printk (" %02x", cosa->rxbuf[i]&0xff);
1929 printk("\n");
1930 }
1931#endif
1932 /* Packet for unknown channel? */
1933 if (cosa->rxbuf == cosa->bouncebuf)
1934 goto out;
1935 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1936 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1937 if (cosa->rxchan->rx_done)
1938 if (cosa->rxchan->rx_done(cosa->rxchan))
1939 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1940 } else {
1941 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1942 cosa->num);
1943 }
1944 /*
1945 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1946 * cleared anyway). We should do it as soon as possible
1947 * so that we can tell the COSA we are done and to give it a time
1948 * for recovery.
1949 */
1950out:
1951 cosa->rxtx = 0;
1952 put_driver_status_nolock(cosa);
1953 spin_unlock_irqrestore(&cosa->lock, flags);
1954}
1955
David Howells7d12e782006-10-05 14:55:46 +01001956static irqreturn_t cosa_interrupt(int irq, void *cosa_)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 unsigned status;
1959 int count = 0;
1960 struct cosa_data *cosa = cosa_;
1961again:
1962 status = cosa_getstatus(cosa);
1963#ifdef DEBUG_IRQS
1964 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1965 status & 0xff);
1966#endif
1967#ifdef DEBUG_IO
1968 debug_status_in(cosa, status);
1969#endif
1970 switch (status & SR_CMD_FROM_SRP_MASK) {
1971 case SR_DOWN_REQUEST:
1972 tx_interrupt(cosa, status);
1973 break;
1974 case SR_UP_REQUEST:
1975 rx_interrupt(cosa, status);
1976 break;
1977 case SR_END_OF_TRANSFER:
1978 eot_interrupt(cosa, status);
1979 break;
1980 default:
1981 /* We may be too fast for SRP. Try to wait a bit more. */
1982 if (count++ < 100) {
1983 udelay(100);
1984 goto again;
1985 }
1986 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1987 cosa->num, status & 0xff, count);
1988 }
1989#ifdef DEBUG_IRQS
1990 if (count)
1991 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1992 cosa->name, count);
1993 else
1994 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
1995#endif
1996 return IRQ_HANDLED;
1997}
1998
1999
2000/* ---------- I/O debugging routines ---------- */
2001/*
2002 * These routines can be used to monitor COSA/SRP I/O and to printk()
2003 * the data being transferred on the data and status I/O port in a
2004 * readable way.
2005 */
2006
2007#ifdef DEBUG_IO
2008static void debug_status_in(struct cosa_data *cosa, int status)
2009{
2010 char *s;
2011 switch(status & SR_CMD_FROM_SRP_MASK) {
2012 case SR_UP_REQUEST:
2013 s = "RX_REQ";
2014 break;
2015 case SR_DOWN_REQUEST:
2016 s = "TX_REQ";
2017 break;
2018 case SR_END_OF_TRANSFER:
2019 s = "ET_REQ";
2020 break;
2021 default:
2022 s = "NO_REQ";
2023 break;
2024 }
2025 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2026 cosa->name,
2027 status,
2028 status & SR_USR_RQ ? "USR_RQ|":"",
2029 status & SR_TX_RDY ? "TX_RDY|":"",
2030 status & SR_RX_RDY ? "RX_RDY|":"",
2031 s);
2032}
2033
2034static void debug_status_out(struct cosa_data *cosa, int status)
2035{
2036 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2037 cosa->name,
2038 status,
2039 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
2040 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
2041 status & SR_RST ? "RESET|":"",
2042 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2043 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
2044 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
2045}
2046
2047static void debug_data_in(struct cosa_data *cosa, int data)
2048{
2049 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2050}
2051
2052static void debug_data_out(struct cosa_data *cosa, int data)
2053{
2054 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2055}
2056
2057static void debug_data_cmd(struct cosa_data *cosa, int data)
2058{
2059 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2060 cosa->name, data,
2061 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2062 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2063}
2064#endif
2065
2066/* EOF -- this file has not been truncated */