blob: 84734a8050925054b69c981fc9be2ebb0341335c [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
Joe Perches9cbe50d2011-06-26 19:01:29 +000077#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/module.h>
80#include <linux/kernel.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040081#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/slab.h>
83#include <linux/poll.h>
84#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/interrupt.h>
86#include <linux/delay.h>
Krzysztof Hałasaaca257532008-07-01 23:40:29 +020087#include <linux/hdlc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/errno.h>
89#include <linux/ioport.h>
90#include <linux/netdevice.h>
91#include <linux/spinlock.h>
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -080092#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <asm/io.h>
95#include <asm/dma.h>
96#include <asm/byteorder.h>
97
Krzysztof Hałasaaca257532008-07-01 23:40:29 +020098#undef COSA_SLOW_IO /* for testing purposes only */
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include "cosa.h"
101
102/* Maximum length of the identification string. */
103#define COSA_MAX_ID_STRING 128
104
105/* Maximum length of the channel name */
106#define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
107
108/* Per-channel data structure */
109
110struct channel_data {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int usage; /* Usage count; >0 for chrdev, -1 for netdev */
112 int num; /* Number of the channel */
113 struct cosa_data *cosa; /* Pointer to the per-card structure */
114 int txsize; /* Size of transmitted data */
115 char *txbuf; /* Transmit buffer */
116 char name[COSA_MAX_NAME]; /* channel name */
117
118 /* The HW layer interface */
119 /* routine called from the RX interrupt */
120 char *(*setup_rx)(struct channel_data *channel, int size);
121 /* routine called when the RX is done (from the EOT interrupt) */
122 int (*rx_done)(struct channel_data *channel);
123 /* routine called when the TX is done (from the EOT interrupt) */
124 int (*tx_done)(struct channel_data *channel, int size);
125
126 /* Character device parts */
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800127 struct mutex rlock;
128 struct semaphore wsem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char *rxdata;
130 int rxsize;
131 wait_queue_head_t txwaitq, rxwaitq;
132 int tx_status, rx_status;
133
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200134 /* generic HDLC device parts */
135 struct net_device *netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct sk_buff *rx_skb, *tx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139/* cosa->firmware_status bits */
140#define COSA_FW_RESET (1<<0) /* Is the ROM monitor active? */
141#define COSA_FW_DOWNLOAD (1<<1) /* Is the microcode downloaded? */
142#define COSA_FW_START (1<<2) /* Is the microcode running? */
143
144struct cosa_data {
145 int num; /* Card number */
146 char name[COSA_MAX_NAME]; /* Card name - e.g "cosa0" */
147 unsigned int datareg, statusreg; /* I/O ports */
148 unsigned short irq, dma; /* IRQ and DMA number */
149 unsigned short startaddr; /* Firmware start address */
150 unsigned short busmaster; /* Use busmastering? */
151 int nchannels; /* # of channels on this card */
152 int driver_status; /* For communicating with firmware */
153 int firmware_status; /* Downloaded, reseted, etc. */
Al Viro64b33612007-10-14 19:35:20 +0100154 unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
155 unsigned long rxtx; /* RX or TX in progress? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 int enabled;
157 int usage; /* usage count */
158 int txchan, txsize, rxsize;
159 struct channel_data *rxchan;
160 char *bouncebuf;
161 char *txbuf, *rxbuf;
162 struct channel_data *chan;
163 spinlock_t lock; /* For exclusive operations on this structure */
164 char id_string[COSA_MAX_ID_STRING]; /* ROM monitor ID string */
165 char *type; /* card type */
166};
167
168/*
169 * Define this if you want all the possible ports to be autoprobed.
170 * It is here but it probably is not a good idea to use this.
171 */
172/* #define COSA_ISA_AUTOPROBE 1 */
173
174/*
175 * Character device major number. 117 was allocated for us.
176 * The value of 0 means to allocate a first free one.
177 */
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000178static DEFINE_MUTEX(cosa_chardev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int cosa_major = 117;
180
181/*
182 * Encoding of the minor numbers:
183 * The lowest CARD_MINOR_BITS bits means the channel on the single card,
184 * the highest bits means the card number.
185 */
186#define CARD_MINOR_BITS 4 /* How many bits in minor number are reserved
187 * for the single card */
188/*
189 * The following depends on CARD_MINOR_BITS. Unfortunately, the "MODULE_STRING"
190 * macro doesn't like anything other than the raw number as an argument :-(
191 */
192#define MAX_CARDS 16
193/* #define MAX_CARDS (1 << (8-CARD_MINOR_BITS)) */
194
195#define DRIVER_RX_READY 0x0001
196#define DRIVER_TX_READY 0x0002
197#define DRIVER_TXMAP_SHIFT 2
198#define DRIVER_TXMAP_MASK 0x0c /* FIXME: 0xfc for 8-channel version */
199
200/*
201 * for cosa->rxtx - indicates whether either transmit or receive is
202 * in progress. These values are mean number of the bit.
203 */
204#define TXBIT 0
205#define RXBIT 1
206#define IRQBIT 2
207
208#define COSA_MTU 2000 /* FIXME: I don't know this exactly */
209
210#undef DEBUG_DATA //1 /* Dump the data read or written to the channel */
211#undef DEBUG_IRQS //1 /* Print the message when the IRQ is received */
212#undef DEBUG_IO //1 /* Dump the I/O traffic */
213
214#define TX_TIMEOUT (5*HZ)
215
216/* Maybe the following should be allocated dynamically */
217static struct cosa_data cosa_cards[MAX_CARDS];
218static int nr_cards;
219
220#ifdef COSA_ISA_AUTOPROBE
221static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
222/* NOTE: DMA is not autoprobed!!! */
223static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
224#else
225static int io[MAX_CARDS+1];
226static int dma[MAX_CARDS+1];
227#endif
228/* IRQ can be safely autoprobed */
229static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
230
231/* for class stuff*/
gregkh@suse.de56b22932005-03-23 10:01:41 -0800232static struct class *cosa_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234#ifdef MODULE
235module_param_array(io, int, NULL, 0);
236MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
237module_param_array(irq, int, NULL, 0);
238MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
239module_param_array(dma, int, NULL, 0);
240MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
241
242MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
243MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
244MODULE_LICENSE("GPL");
245#endif
246
247/* I use this mainly for testing purposes */
248#ifdef COSA_SLOW_IO
249#define cosa_outb outb_p
250#define cosa_outw outw_p
251#define cosa_inb inb_p
252#define cosa_inw inw_p
253#else
254#define cosa_outb outb
255#define cosa_outw outw
256#define cosa_inb inb
257#define cosa_inw inw
258#endif
259
260#define is_8bit(cosa) (!(cosa->datareg & 0x08))
261
262#define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
263#define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
264#define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
265#define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
266#define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
267#define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
268
269/* Initialization stuff */
270static int cosa_probe(int ioaddr, int irq, int dma);
271
272/* HW interface */
273static void cosa_enable_rx(struct channel_data *chan);
274static void cosa_disable_rx(struct channel_data *chan);
275static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
276static void cosa_kick(struct cosa_data *cosa);
277static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
278
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200279/* Network device stuff */
280static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
281 unsigned short parity);
282static int cosa_net_open(struct net_device *d);
283static int cosa_net_close(struct net_device *d);
284static void cosa_net_timeout(struct net_device *d);
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000285static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200286static char *cosa_net_setup_rx(struct channel_data *channel, int size);
287static int cosa_net_rx_done(struct channel_data *channel);
288static int cosa_net_tx_done(struct channel_data *channel, int size);
289static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/* Character device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static char *chrdev_setup_rx(struct channel_data *channel, int size);
293static int chrdev_rx_done(struct channel_data *channel);
294static int chrdev_tx_done(struct channel_data *channel, int size);
295static ssize_t cosa_read(struct file *file,
296 char __user *buf, size_t count, loff_t *ppos);
297static ssize_t cosa_write(struct file *file,
298 const char __user *buf, size_t count, loff_t *ppos);
299static unsigned int cosa_poll(struct file *file, poll_table *poll);
300static int cosa_open(struct inode *inode, struct file *file);
301static int cosa_release(struct inode *inode, struct file *file);
Alan Cox2395f0e2009-10-14 05:22:24 +0000302static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
303 unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304#ifdef COSA_FASYNC_WORKING
305static int cosa_fasync(struct inode *inode, struct file *file, int on);
306#endif
307
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800308static const struct file_operations cosa_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 .owner = THIS_MODULE,
310 .llseek = no_llseek,
311 .read = cosa_read,
312 .write = cosa_write,
313 .poll = cosa_poll,
Alan Cox2395f0e2009-10-14 05:22:24 +0000314 .unlocked_ioctl = cosa_chardev_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .open = cosa_open,
316 .release = cosa_release,
317#ifdef COSA_FASYNC_WORKING
318 .fasync = cosa_fasync,
319#endif
320};
321
322/* Ioctls */
323static int cosa_start(struct cosa_data *cosa, int address);
324static int cosa_reset(struct cosa_data *cosa);
325static int cosa_download(struct cosa_data *cosa, void __user *a);
326static int cosa_readmem(struct cosa_data *cosa, void __user *a);
327
328/* COSA/SRP ROM monitor */
329static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
330static int startmicrocode(struct cosa_data *cosa, int address);
331static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
332static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
333
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300334/* Auxiliary functions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static int get_wait_data(struct cosa_data *cosa);
336static int put_wait_data(struct cosa_data *cosa, int data);
337static int puthexnumber(struct cosa_data *cosa, int number);
338static void put_driver_status(struct cosa_data *cosa);
339static void put_driver_status_nolock(struct cosa_data *cosa);
340
341/* Interrupt handling */
David Howells7d12e782006-10-05 14:55:46 +0100342static irqreturn_t cosa_interrupt(int irq, void *cosa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344/* I/O ops debugging */
345#ifdef DEBUG_IO
346static void debug_data_in(struct cosa_data *cosa, int data);
347static void debug_data_out(struct cosa_data *cosa, int data);
348static void debug_data_cmd(struct cosa_data *cosa, int data);
349static void debug_status_in(struct cosa_data *cosa, int status);
350static void debug_status_out(struct cosa_data *cosa, int status);
351#endif
352
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200353static inline struct channel_data* dev_to_chan(struct net_device *dev)
354{
355 return (struct channel_data *)dev_to_hdlc(dev)->priv;
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/* ---------- Initialization stuff ---------- */
359
360static int __init cosa_init(void)
361{
362 int i, err = 0;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (cosa_major > 0) {
365 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000366 pr_warn("unable to get major %d\n", cosa_major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 err = -EIO;
368 goto out;
369 }
370 } else {
371 if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000372 pr_warn("unable to register chardev\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 err = -EIO;
374 goto out;
375 }
376 }
377 for (i=0; i<MAX_CARDS; i++)
378 cosa_cards[i].num = -1;
379 for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
380 cosa_probe(io[i], irq[i], dma[i]);
381 if (!nr_cards) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000382 pr_warn("no devices found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unregister_chrdev(cosa_major, "cosa");
384 err = -ENODEV;
385 goto out;
386 }
gregkh@suse.de56b22932005-03-23 10:01:41 -0800387 cosa_class = class_create(THIS_MODULE, "cosa");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (IS_ERR(cosa_class)) {
389 err = PTR_ERR(cosa_class);
390 goto out_chrdev;
391 }
Greg Kroah-Hartmane17da9c2008-05-21 12:52:33 -0700392 for (i = 0; i < nr_cards; i++)
Greg Kroah-Hartman6e05d6c2008-07-21 20:03:34 -0700393 device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
394 "cosa%d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 err = 0;
396 goto out;
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398out_chrdev:
399 unregister_chrdev(cosa_major, "cosa");
400out:
401 return err;
402}
403module_init(cosa_init);
404
405static void __exit cosa_exit(void)
406{
407 struct cosa_data *cosa;
408 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200410 for (i = 0; i < nr_cards; i++)
Tony Jones02ff82c2007-09-25 02:03:03 +0200411 device_destroy(cosa_class, MKDEV(cosa_major, i));
gregkh@suse.de56b22932005-03-23 10:01:41 -0800412 class_destroy(cosa_class);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200413
414 for (cosa = cosa_cards; nr_cards--; cosa++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* Clean up the per-channel data */
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200416 for (i = 0; i < cosa->nchannels; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Chardev driver has no alloc'd per-channel data */
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200418 unregister_hdlc_device(cosa->chan[i].netdev);
419 free_netdev(cosa->chan[i].netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 /* Clean up the per-card data */
422 kfree(cosa->chan);
423 kfree(cosa->bouncebuf);
424 free_irq(cosa->irq, cosa);
425 free_dma(cosa->dma);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200426 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 unregister_chrdev(cosa_major, "cosa");
429}
430module_exit(cosa_exit);
431
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100432static const struct net_device_ops cosa_ops = {
433 .ndo_open = cosa_net_open,
434 .ndo_stop = cosa_net_close,
435 .ndo_change_mtu = hdlc_change_mtu,
436 .ndo_start_xmit = hdlc_start_xmit,
437 .ndo_do_ioctl = cosa_net_ioctl,
438 .ndo_tx_timeout = cosa_net_timeout,
439};
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441static int cosa_probe(int base, int irq, int dma)
442{
443 struct cosa_data *cosa = cosa_cards+nr_cards;
444 int i, err = 0;
445
446 memset(cosa, 0, sizeof(struct cosa_data));
447
448 /* Checking validity of parameters: */
449 /* IRQ should be 2-7 or 10-15; negative IRQ means autoprobe */
450 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000451 pr_info("invalid IRQ %d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -1;
453 }
454 /* I/O address should be between 0x100 and 0x3ff and should be
455 * multiple of 8. */
456 if (base < 0x100 || base > 0x3ff || base & 0x7) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000457 pr_info("invalid I/O address 0x%x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return -1;
459 }
460 /* DMA should be 0,1 or 3-7 */
461 if (dma < 0 || dma == 4 || dma > 7) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000462 pr_info("invalid DMA %d\n", dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return -1;
464 }
465 /* and finally, on 16-bit COSA DMA should be 4-7 and
466 * I/O base should not be multiple of 0x10 */
467 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000468 pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
469 base, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -1;
471 }
472
473 cosa->dma = dma;
474 cosa->datareg = base;
475 cosa->statusreg = is_8bit(cosa)?base+1:base+2;
476 spin_lock_init(&cosa->lock);
477
478 if (!request_region(base, is_8bit(cosa)?2:4,"cosa"))
479 return -1;
480
481 if (cosa_reset_and_read_id(cosa, cosa->id_string) < 0) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000482 printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 err = -1;
484 goto err_out;
485 }
486
487 /* Test the validity of identification string */
488 if (!strncmp(cosa->id_string, "SRP", 3))
489 cosa->type = "srp";
490 else if (!strncmp(cosa->id_string, "COSA", 4))
491 cosa->type = is_8bit(cosa)? "cosa8": "cosa16";
492 else {
493/* Print a warning only if we are not autoprobing */
494#ifndef COSA_ISA_AUTOPROBE
Joe Perches9cbe50d2011-06-26 19:01:29 +0000495 pr_info("valid signature not found at 0x%x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496#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)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000503 printk(KERN_DEBUG "changing name at 0x%x failed.\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -1;
505 }
506
507 /* Now do IRQ autoprobe */
508 if (irq < 0) {
509 unsigned long irqs;
Joe Perches9cbe50d2011-06-26 19:01:29 +0000510/* pr_info("IRQ autoprobe\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 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) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000528 pr_info("multiple interrupts obtained (%d, board at 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 irq, cosa->datareg);
530 err = -1;
531 goto err_out;
532 }
533 if (irq == 0) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000534 pr_info("no interrupt obtained (board at 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 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);
Thomas Gleixnerd1985502010-09-07 14:32:25 +0000577 sema_init(&chan->wsem, 1);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200578
579 /* Register the network interface */
580 if (!(chan->netdev = alloc_hdlcdev(chan))) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000581 pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200582 goto err_hdlcdev;
583 }
584 dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
585 dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
Krzysztof Hałasa991990a2009-01-08 22:52:11 +0100586 chan->netdev->netdev_ops = &cosa_ops;
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200587 chan->netdev->watchdog_timeo = TX_TIMEOUT;
588 chan->netdev->base_addr = chan->cosa->datareg;
589 chan->netdev->irq = chan->cosa->irq;
590 chan->netdev->dma = chan->cosa->dma;
591 if (register_hdlc_device(chan->netdev)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000592 netdev_warn(chan->netdev,
593 "register_hdlc_device() failed\n");
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200594 free_netdev(chan->netdev);
595 goto err_hdlcdev;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Joe Perches9cbe50d2011-06-26 19:01:29 +0000599 pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 cosa->num, cosa->id_string, cosa->type,
601 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
602
603 return nr_cards++;
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200604
605err_hdlcdev:
606 while (i-- > 0) {
607 unregister_hdlc_device(cosa->chan[i].netdev);
608 free_netdev(cosa->chan[i].netdev);
609 }
610 kfree(cosa->chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611err_out3:
612 kfree(cosa->bouncebuf);
613err_out2:
614 free_dma(cosa->dma);
615err_out1:
616 free_irq(cosa->irq, cosa);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200617err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 release_region(cosa->datareg,is_8bit(cosa)?2:4);
Joe Perches9cbe50d2011-06-26 19:01:29 +0000619 pr_notice("cosa%d: allocating resources failed\n", cosa->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return err;
621}
622
623
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200624/*---------- network device ---------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200626static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
627 unsigned short parity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200629 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
630 return 0;
631 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200634static int cosa_net_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200636 struct channel_data *chan = dev_to_chan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 int err;
638 unsigned long flags;
639
640 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000641 pr_notice("%s: start the firmware first (status %d)\n",
642 chan->cosa->name, chan->cosa->firmware_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EPERM;
644 }
645 spin_lock_irqsave(&chan->cosa->lock, flags);
646 if (chan->usage != 0) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000647 pr_warn("%s: cosa_net_open called with usage count %d\n",
648 chan->name, chan->usage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_unlock_irqrestore(&chan->cosa->lock, flags);
650 return -EBUSY;
651 }
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200652 chan->setup_rx = cosa_net_setup_rx;
653 chan->tx_done = cosa_net_tx_done;
654 chan->rx_done = cosa_net_rx_done;
655 chan->usage = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 chan->cosa->usage++;
657 spin_unlock_irqrestore(&chan->cosa->lock, flags);
658
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200659 err = hdlc_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (err) {
661 spin_lock_irqsave(&chan->cosa->lock, flags);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200662 chan->usage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 chan->cosa->usage--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_unlock_irqrestore(&chan->cosa->lock, flags);
665 return err;
666 }
667
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200668 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 cosa_enable_rx(chan);
670 return 0;
671}
672
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000673static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
674 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200676 struct channel_data *chan = dev_to_chan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 netif_stop_queue(dev);
679
680 chan->tx_skb = skb;
681 cosa_start_tx(chan, skb->data, skb->len);
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000682 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200685static void cosa_net_timeout(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200687 struct channel_data *chan = dev_to_chan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200690 chan->netdev->stats.rx_errors++;
691 chan->netdev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 } else {
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200693 chan->netdev->stats.tx_errors++;
694 chan->netdev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 cosa_kick(chan->cosa);
697 if (chan->tx_skb) {
698 dev_kfree_skb(chan->tx_skb);
699 chan->tx_skb = NULL;
700 }
701 netif_wake_queue(dev);
702}
703
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200704static int cosa_net_close(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200706 struct channel_data *chan = dev_to_chan(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 unsigned long flags;
708
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200709 netif_stop_queue(dev);
710 hdlc_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 cosa_disable_rx(chan);
712 spin_lock_irqsave(&chan->cosa->lock, flags);
713 if (chan->rx_skb) {
714 kfree_skb(chan->rx_skb);
715 chan->rx_skb = NULL;
716 }
717 if (chan->tx_skb) {
718 kfree_skb(chan->tx_skb);
719 chan->tx_skb = NULL;
720 }
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200721 chan->usage = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 chan->cosa->usage--;
723 spin_unlock_irqrestore(&chan->cosa->lock, flags);
724 return 0;
725}
726
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200727static char *cosa_net_setup_rx(struct channel_data *chan, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
729 /*
730 * We can safely fall back to non-dma-able memory, because we have
731 * the cosa->bouncebuf pre-allocated.
732 */
Wei Yongjunfbbfb982009-02-25 00:22:41 +0000733 kfree_skb(chan->rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 chan->rx_skb = dev_alloc_skb(size);
735 if (chan->rx_skb == NULL) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000736 pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200737 chan->netdev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return NULL;
739 }
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200740 chan->netdev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return skb_put(chan->rx_skb, size);
742}
743
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200744static int cosa_net_rx_done(struct channel_data *chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 if (!chan->rx_skb) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000747 pr_warn("%s: rx_done with empty skb!\n", chan->name);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200748 chan->netdev->stats.rx_errors++;
749 chan->netdev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751 }
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200752 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
753 chan->rx_skb->dev = chan->netdev;
David S. Miller2f7826c2007-03-26 02:00:58 -0700754 skb_reset_mac_header(chan->rx_skb);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200755 chan->netdev->stats.rx_packets++;
756 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 netif_rx(chan->rx_skb);
758 chan->rx_skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 0;
760}
761
762/* ARGSUSED */
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200763static int cosa_net_tx_done(struct channel_data *chan, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 if (!chan->tx_skb) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000766 pr_warn("%s: tx_done with empty skb!\n", chan->name);
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200767 chan->netdev->stats.tx_errors++;
768 chan->netdev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return 1;
770 }
771 dev_kfree_skb_irq(chan->tx_skb);
772 chan->tx_skb = NULL;
Krzysztof Hałasaaca257532008-07-01 23:40:29 +0200773 chan->netdev->stats.tx_packets++;
774 chan->netdev->stats.tx_bytes += size;
775 netif_wake_queue(chan->netdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 1;
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779/*---------- Character device ---------- */
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static ssize_t cosa_read(struct file *file,
782 char __user *buf, size_t count, loff_t *ppos)
783{
784 DECLARE_WAITQUEUE(wait, current);
785 unsigned long flags;
786 struct channel_data *chan = file->private_data;
787 struct cosa_data *cosa = chan->cosa;
788 char *kbuf;
789
790 if (!(cosa->firmware_status & COSA_FW_START)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000791 pr_notice("%s: start the firmware first (status %d)\n",
792 cosa->name, cosa->firmware_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return -EPERM;
794 }
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800795 if (mutex_lock_interruptible(&chan->rlock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -ERESTARTSYS;
797
Joe Perches1d5d1fd2013-02-03 17:28:12 +0000798 chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL);
799 if (chan->rxdata == NULL) {
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800800 mutex_unlock(&chan->rlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return -ENOMEM;
802 }
803
804 chan->rx_status = 0;
805 cosa_enable_rx(chan);
806 spin_lock_irqsave(&cosa->lock, flags);
807 add_wait_queue(&chan->rxwaitq, &wait);
Rudy Matela640462c2009-12-09 11:35:40 -0300808 while (!chan->rx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 current->state = TASK_INTERRUPTIBLE;
810 spin_unlock_irqrestore(&cosa->lock, flags);
811 schedule();
812 spin_lock_irqsave(&cosa->lock, flags);
813 if (signal_pending(current) && chan->rx_status == 0) {
814 chan->rx_status = 1;
815 remove_wait_queue(&chan->rxwaitq, &wait);
816 current->state = TASK_RUNNING;
817 spin_unlock_irqrestore(&cosa->lock, flags);
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800818 mutex_unlock(&chan->rlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return -ERESTARTSYS;
820 }
821 }
822 remove_wait_queue(&chan->rxwaitq, &wait);
823 current->state = TASK_RUNNING;
824 kbuf = chan->rxdata;
825 count = chan->rxsize;
826 spin_unlock_irqrestore(&cosa->lock, flags);
Matthias Kaehlcke6387c4b2008-03-05 18:53:01 -0800827 mutex_unlock(&chan->rlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (copy_to_user(buf, kbuf, count)) {
830 kfree(kbuf);
831 return -EFAULT;
832 }
833 kfree(kbuf);
834 return count;
835}
836
837static char *chrdev_setup_rx(struct channel_data *chan, int size)
838{
839 /* Expect size <= COSA_MTU */
840 chan->rxsize = size;
841 return chan->rxdata;
842}
843
844static int chrdev_rx_done(struct channel_data *chan)
845{
846 if (chan->rx_status) { /* Reader has died */
847 kfree(chan->rxdata);
848 up(&chan->wsem);
849 }
850 chan->rx_status = 1;
851 wake_up_interruptible(&chan->rxwaitq);
852 return 1;
853}
854
855
856static ssize_t cosa_write(struct file *file,
857 const char __user *buf, size_t count, loff_t *ppos)
858{
859 DECLARE_WAITQUEUE(wait, current);
860 struct channel_data *chan = file->private_data;
861 struct cosa_data *cosa = chan->cosa;
862 unsigned long flags;
863 char *kbuf;
864
865 if (!(cosa->firmware_status & COSA_FW_START)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +0000866 pr_notice("%s: start the firmware first (status %d)\n",
867 cosa->name, cosa->firmware_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -EPERM;
869 }
870 if (down_interruptible(&chan->wsem))
871 return -ERESTARTSYS;
872
873 if (count > COSA_MTU)
874 count = COSA_MTU;
875
876 /* Allocate the buffer */
Joe Perches1d5d1fd2013-02-03 17:28:12 +0000877 kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA);
878 if (kbuf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 up(&chan->wsem);
880 return -ENOMEM;
881 }
882 if (copy_from_user(kbuf, buf, count)) {
883 up(&chan->wsem);
884 kfree(kbuf);
885 return -EFAULT;
886 }
887 chan->tx_status=0;
888 cosa_start_tx(chan, kbuf, count);
889
890 spin_lock_irqsave(&cosa->lock, flags);
891 add_wait_queue(&chan->txwaitq, &wait);
Rudy Matela640462c2009-12-09 11:35:40 -0300892 while (!chan->tx_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 current->state = TASK_INTERRUPTIBLE;
894 spin_unlock_irqrestore(&cosa->lock, flags);
895 schedule();
896 spin_lock_irqsave(&cosa->lock, flags);
897 if (signal_pending(current) && chan->tx_status == 0) {
898 chan->tx_status = 1;
899 remove_wait_queue(&chan->txwaitq, &wait);
900 current->state = TASK_RUNNING;
901 chan->tx_status = 1;
902 spin_unlock_irqrestore(&cosa->lock, flags);
Dan Carpenter40be2612009-11-29 06:02:55 +0000903 up(&chan->wsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -ERESTARTSYS;
905 }
906 }
907 remove_wait_queue(&chan->txwaitq, &wait);
908 current->state = TASK_RUNNING;
909 up(&chan->wsem);
910 spin_unlock_irqrestore(&cosa->lock, flags);
911 kfree(kbuf);
912 return count;
913}
914
915static int chrdev_tx_done(struct channel_data *chan, int size)
916{
917 if (chan->tx_status) { /* Writer was interrupted */
918 kfree(chan->txbuf);
919 up(&chan->wsem);
920 }
921 chan->tx_status = 1;
922 wake_up_interruptible(&chan->txwaitq);
923 return 1;
924}
925
926static unsigned int cosa_poll(struct file *file, poll_table *poll)
927{
Joe Perches9cbe50d2011-06-26 19:01:29 +0000928 pr_info("cosa_poll is here\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930}
931
932static int cosa_open(struct inode *inode, struct file *file)
933{
934 struct cosa_data *cosa;
935 struct channel_data *chan;
936 unsigned long flags;
937 int n;
Jonathan Corbet09118102008-05-15 09:21:33 -0600938 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000940 mutex_lock(&cosa_chardev_mutex);
Al Viro496ad9a2013-01-23 17:07:38 -0500941 if ((n=iminor(file_inode(file))>>CARD_MINOR_BITS)
Jonathan Corbet09118102008-05-15 09:21:33 -0600942 >= nr_cards) {
943 ret = -ENODEV;
944 goto out;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 cosa = cosa_cards+n;
947
Al Viro496ad9a2013-01-23 17:07:38 -0500948 if ((n=iminor(file_inode(file))
Jonathan Corbet09118102008-05-15 09:21:33 -0600949 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
950 ret = -ENODEV;
951 goto out;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 chan = cosa->chan + n;
954
955 file->private_data = chan;
956
957 spin_lock_irqsave(&cosa->lock, flags);
958
959 if (chan->usage < 0) { /* in netdev mode */
960 spin_unlock_irqrestore(&cosa->lock, flags);
Jonathan Corbet09118102008-05-15 09:21:33 -0600961 ret = -EBUSY;
962 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 cosa->usage++;
965 chan->usage++;
966
967 chan->tx_done = chrdev_tx_done;
968 chan->setup_rx = chrdev_setup_rx;
969 chan->rx_done = chrdev_rx_done;
970 spin_unlock_irqrestore(&cosa->lock, flags);
Jonathan Corbet09118102008-05-15 09:21:33 -0600971out:
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000972 mutex_unlock(&cosa_chardev_mutex);
Jonathan Corbet09118102008-05-15 09:21:33 -0600973 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976static int cosa_release(struct inode *inode, struct file *file)
977{
978 struct channel_data *channel = file->private_data;
979 struct cosa_data *cosa;
980 unsigned long flags;
981
982 cosa = channel->cosa;
983 spin_lock_irqsave(&cosa->lock, flags);
984 cosa->usage--;
985 channel->usage--;
986 spin_unlock_irqrestore(&cosa->lock, flags);
987 return 0;
988}
989
990#ifdef COSA_FASYNC_WORKING
991static struct fasync_struct *fasync[256] = { NULL, };
992
993/* To be done ... */
994static int cosa_fasync(struct inode *inode, struct file *file, int on)
995{
996 int port = iminor(inode);
Jonathan Corbet60aa4922009-02-01 14:52:56 -0700997
998 return fasync_helper(inode, file, on, &fasync[port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000#endif
1001
1002
1003/* ---------- Ioctls ---------- */
1004
1005/*
1006 * Ioctl subroutines can safely be made inline, because they are called
1007 * only from cosa_ioctl().
1008 */
1009static inline int cosa_reset(struct cosa_data *cosa)
1010{
1011 char idstring[COSA_MAX_ID_STRING];
1012 if (cosa->usage > 1)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001013 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 cosa->num, cosa->usage);
1015 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1016 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001017 pr_notice("cosa%d: reset failed\n", cosa->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return -EIO;
1019 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001020 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 cosa->firmware_status |= COSA_FW_RESET;
1022 return 0;
1023}
1024
1025/* High-level function to download data into COSA memory. Calls download() */
1026static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1027{
1028 struct cosa_download d;
1029 int i;
1030
1031 if (cosa->usage > 1)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001032 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 cosa->name, cosa->usage);
1034 if (!(cosa->firmware_status & COSA_FW_RESET)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001035 pr_notice("%s: reset the card first (status %d)\n",
1036 cosa->name, cosa->firmware_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return -EPERM;
1038 }
1039
1040 if (copy_from_user(&d, arg, sizeof(d)))
1041 return -EFAULT;
1042
1043 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1044 return -EINVAL;
1045 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1046 return -EINVAL;
1047
1048
1049 /* If something fails, force the user to reset the card */
1050 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1051
1052 i = download(cosa, d.code, d.len, d.addr);
1053 if (i < 0) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001054 pr_notice("cosa%d: microcode download failed: %d\n",
1055 cosa->num, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -EIO;
1057 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001058 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 cosa->num, d.len, d.addr);
1060 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1061 return 0;
1062}
1063
1064/* High-level function to read COSA memory. Calls readmem() */
1065static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1066{
1067 struct cosa_download d;
1068 int i;
1069
1070 if (cosa->usage > 1)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001071 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 cosa->num, cosa->usage);
1073 if (!(cosa->firmware_status & COSA_FW_RESET)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001074 pr_notice("%s: reset the card first (status %d)\n",
1075 cosa->name, cosa->firmware_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return -EPERM;
1077 }
1078
1079 if (copy_from_user(&d, arg, sizeof(d)))
1080 return -EFAULT;
1081
1082 /* If something fails, force the user to reset the card */
1083 cosa->firmware_status &= ~COSA_FW_RESET;
1084
1085 i = readmem(cosa, d.code, d.len, d.addr);
1086 if (i < 0) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001087 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return -EIO;
1089 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001090 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 cosa->num, d.len, d.addr);
1092 cosa->firmware_status |= COSA_FW_RESET;
1093 return 0;
1094}
1095
1096/* High-level function to start microcode. Calls startmicrocode(). */
1097static inline int cosa_start(struct cosa_data *cosa, int address)
1098{
1099 int i;
1100
1101 if (cosa->usage > 1)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001102 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 cosa->num, cosa->usage);
1104
1105 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1106 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001107 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1108 cosa->name, cosa->firmware_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return -EPERM;
1110 }
1111 cosa->firmware_status &= ~COSA_FW_RESET;
1112 if ((i=startmicrocode(cosa, address)) < 0) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001113 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1114 cosa->num, address, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return -EIO;
1116 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001117 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 cosa->startaddr = address;
1119 cosa->firmware_status |= COSA_FW_START;
1120 return 0;
1121}
1122
1123/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1124static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1125{
1126 int l = strlen(cosa->id_string)+1;
1127 if (copy_to_user(string, cosa->id_string, l))
1128 return -EFAULT;
1129 return l;
1130}
1131
1132/* Buffer of size at least COSA_MAX_ID_STRING is expected */
1133static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1134{
1135 int l = strlen(cosa->type)+1;
1136 if (copy_to_user(string, cosa->type, l))
1137 return -EFAULT;
1138 return l;
1139}
1140
1141static int cosa_ioctl_common(struct cosa_data *cosa,
1142 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1143{
1144 void __user *argp = (void __user *)arg;
Rudy Matela640462c2009-12-09 11:35:40 -03001145 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 case COSAIORSET: /* Reset the device */
1147 if (!capable(CAP_NET_ADMIN))
1148 return -EACCES;
1149 return cosa_reset(cosa);
1150 case COSAIOSTRT: /* Start the firmware */
1151 if (!capable(CAP_SYS_RAWIO))
1152 return -EACCES;
1153 return cosa_start(cosa, arg);
1154 case COSAIODOWNLD: /* Download the firmware */
1155 if (!capable(CAP_SYS_RAWIO))
1156 return -EACCES;
1157
1158 return cosa_download(cosa, argp);
1159 case COSAIORMEM:
1160 if (!capable(CAP_SYS_RAWIO))
1161 return -EACCES;
1162 return cosa_readmem(cosa, argp);
1163 case COSAIORTYPE:
1164 return cosa_gettype(cosa, argp);
1165 case COSAIORIDSTR:
1166 return cosa_getidstr(cosa, argp);
1167 case COSAIONRCARDS:
1168 return nr_cards;
1169 case COSAIONRCHANS:
1170 return cosa->nchannels;
1171 case COSAIOBMSET:
1172 if (!capable(CAP_SYS_RAWIO))
1173 return -EACCES;
1174 if (is_8bit(cosa))
1175 return -EINVAL;
1176 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1177 return -EINVAL;
1178 cosa->busmaster = arg;
1179 return 0;
1180 case COSAIOBMGET:
1181 return cosa->busmaster;
1182 }
1183 return -ENOIOCTLCMD;
1184}
1185
Krzysztof Hałasaaca257532008-07-01 23:40:29 +02001186static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 int rv;
Krzysztof Hałasaaca257532008-07-01 23:40:29 +02001189 struct channel_data *chan = dev_to_chan(dev);
1190 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1191 (unsigned long)ifr->ifr_data);
1192 if (rv != -ENOIOCTLCMD)
1193 return rv;
1194 return hdlc_ioctl(dev, ifr, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Alan Cox2395f0e2009-10-14 05:22:24 +00001197static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1198 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct channel_data *channel = file->private_data;
Alan Cox2395f0e2009-10-14 05:22:24 +00001201 struct cosa_data *cosa;
1202 long ret;
1203
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +00001204 mutex_lock(&cosa_chardev_mutex);
Alan Cox2395f0e2009-10-14 05:22:24 +00001205 cosa = channel->cosa;
1206 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +00001207 mutex_unlock(&cosa_chardev_mutex);
Alan Cox2395f0e2009-10-14 05:22:24 +00001208 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211
1212/*---------- HW layer interface ---------- */
1213
1214/*
1215 * The higher layer can bind itself to the HW layer by setting the callbacks
1216 * in the channel_data structure and by using these routines.
1217 */
1218static void cosa_enable_rx(struct channel_data *chan)
1219{
1220 struct cosa_data *cosa = chan->cosa;
1221
1222 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1223 put_driver_status(cosa);
1224}
1225
1226static void cosa_disable_rx(struct channel_data *chan)
1227{
1228 struct cosa_data *cosa = chan->cosa;
1229
1230 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1231 put_driver_status(cosa);
1232}
1233
1234/*
1235 * FIXME: This routine probably should check for cosa_start_tx() called when
1236 * the previous transmit is still unfinished. In this case the non-zero
1237 * return value should indicate to the caller that the queuing(sp?) up
1238 * the transmit has failed.
1239 */
1240static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1241{
1242 struct cosa_data *cosa = chan->cosa;
1243 unsigned long flags;
1244#ifdef DEBUG_DATA
1245 int i;
1246
Joe Perches9cbe50d2011-06-26 19:01:29 +00001247 pr_info("cosa%dc%d: starting tx(0x%x)",
1248 chan->cosa->num, chan->num, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 for (i=0; i<len; i++)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001250 pr_cont(" %02x", buf[i]&0xff);
1251 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252#endif
1253 spin_lock_irqsave(&cosa->lock, flags);
1254 chan->txbuf = buf;
1255 chan->txsize = len;
1256 if (len > COSA_MTU)
1257 chan->txsize = COSA_MTU;
1258 spin_unlock_irqrestore(&cosa->lock, flags);
1259
1260 /* Tell the firmware we are ready */
1261 set_bit(chan->num, &cosa->txbitmap);
1262 put_driver_status(cosa);
1263
1264 return 0;
1265}
1266
1267static void put_driver_status(struct cosa_data *cosa)
1268{
1269 unsigned long flags;
1270 int status;
1271
1272 spin_lock_irqsave(&cosa->lock, flags);
1273
1274 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1275 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1276 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1277 &DRIVER_TXMAP_MASK : 0);
1278 if (!cosa->rxtx) {
1279 if (cosa->rxbitmap|cosa->txbitmap) {
1280 if (!cosa->enabled) {
1281 cosa_putstatus(cosa, SR_RX_INT_ENA);
1282#ifdef DEBUG_IO
1283 debug_status_out(cosa, SR_RX_INT_ENA);
1284#endif
1285 cosa->enabled = 1;
1286 }
1287 } else if (cosa->enabled) {
1288 cosa->enabled = 0;
1289 cosa_putstatus(cosa, 0);
1290#ifdef DEBUG_IO
1291 debug_status_out(cosa, 0);
1292#endif
1293 }
1294 cosa_putdata8(cosa, status);
1295#ifdef DEBUG_IO
1296 debug_data_cmd(cosa, status);
1297#endif
1298 }
1299 spin_unlock_irqrestore(&cosa->lock, flags);
1300}
1301
1302static void put_driver_status_nolock(struct cosa_data *cosa)
1303{
1304 int status;
1305
1306 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1307 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1308 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1309 &DRIVER_TXMAP_MASK : 0);
1310
1311 if (cosa->rxbitmap|cosa->txbitmap) {
1312 cosa_putstatus(cosa, SR_RX_INT_ENA);
1313#ifdef DEBUG_IO
1314 debug_status_out(cosa, SR_RX_INT_ENA);
1315#endif
1316 cosa->enabled = 1;
1317 } else {
1318 cosa_putstatus(cosa, 0);
1319#ifdef DEBUG_IO
1320 debug_status_out(cosa, 0);
1321#endif
1322 cosa->enabled = 0;
1323 }
1324 cosa_putdata8(cosa, status);
1325#ifdef DEBUG_IO
1326 debug_data_cmd(cosa, status);
1327#endif
1328}
1329
1330/*
1331 * The "kickme" function: When the DMA times out, this is called to
1332 * clean up the driver status.
1333 * FIXME: Preliminary support, the interface is probably wrong.
1334 */
1335static void cosa_kick(struct cosa_data *cosa)
1336{
1337 unsigned long flags, flags1;
1338 char *s = "(probably) IRQ";
1339
1340 if (test_bit(RXBIT, &cosa->rxtx))
1341 s = "RX DMA";
1342 if (test_bit(TXBIT, &cosa->rxtx))
1343 s = "TX DMA";
1344
Joe Perches9cbe50d2011-06-26 19:01:29 +00001345 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 spin_lock_irqsave(&cosa->lock, flags);
1347 cosa->rxtx = 0;
1348
1349 flags1 = claim_dma_lock();
1350 disable_dma(cosa->dma);
1351 clear_dma_ff(cosa->dma);
1352 release_dma_lock(flags1);
1353
1354 /* FIXME: Anything else? */
1355 udelay(100);
1356 cosa_putstatus(cosa, 0);
1357 udelay(100);
1358 (void) cosa_getdata8(cosa);
1359 udelay(100);
1360 cosa_putdata8(cosa, 0);
1361 udelay(100);
1362 put_driver_status_nolock(cosa);
1363 spin_unlock_irqrestore(&cosa->lock, flags);
1364}
1365
1366/*
1367 * Check if the whole buffer is DMA-able. It means it is below the 16M of
1368 * physical memory and doesn't span the 64k boundary. For now it seems
1369 * SKB's never do this, but we'll check this anyway.
1370 */
1371static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1372{
1373 static int count;
1374 unsigned long b = (unsigned long)buf;
1375 if (b+len >= MAX_DMA_ADDRESS)
1376 return 0;
1377 if ((b^ (b+len)) & 0x10000) {
1378 if (count++ < 5)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001379 pr_info("%s: packet spanning a 64k boundary\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 chan->name);
1381 return 0;
1382 }
1383 return 1;
1384}
1385
1386
1387/* ---------- The SRP/COSA ROM monitor functions ---------- */
1388
1389/*
1390 * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1391 * drivers need to say 4-digit hex number meaning start address of the microcode
1392 * separated by a single space. Monitor replies by saying " =". Now driver
1393 * has to write 4-digit hex number meaning the last byte address ended
1394 * by a single space. Monitor has to reply with a space. Now the download
1395 * begins. After the download monitor replies with "\r\n." (CR LF dot).
1396 */
1397static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1398{
1399 int i;
1400
1401 if (put_wait_data(cosa, 'w') == -1) return -1;
1402 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1403 if (get_wait_data(cosa) != '=') return -3;
1404
1405 if (puthexnumber(cosa, address) < 0) return -4;
1406 if (put_wait_data(cosa, ' ') == -1) return -10;
1407 if (get_wait_data(cosa) != ' ') return -11;
1408 if (get_wait_data(cosa) != '=') return -12;
1409
1410 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1411 if (put_wait_data(cosa, ' ') == -1) return -18;
1412 if (get_wait_data(cosa) != ' ') return -19;
1413
1414 while (length--) {
1415 char c;
1416#ifndef SRP_DOWNLOAD_AT_BOOT
1417 if (get_user(c, microcode))
1418 return -23; /* ??? */
1419#else
1420 c = *microcode;
1421#endif
1422 if (put_wait_data(cosa, c) == -1)
1423 return -20;
1424 microcode++;
1425 }
1426
1427 if (get_wait_data(cosa) != '\r') return -21;
1428 if (get_wait_data(cosa) != '\n') return -22;
1429 if (get_wait_data(cosa) != '.') return -23;
1430#if 0
1431 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1432#endif
1433 return 0;
1434}
1435
1436
1437/*
1438 * Starting microcode is done via the "g" command of the SRP monitor.
1439 * The chat should be the following: "g" "g=" "<addr><CR>"
1440 * "<CR><CR><LF><CR><LF>".
1441 */
1442static int startmicrocode(struct cosa_data *cosa, int address)
1443{
1444 if (put_wait_data(cosa, 'g') == -1) return -1;
1445 if (get_wait_data(cosa) != 'g') return -2;
1446 if (get_wait_data(cosa) != '=') return -3;
1447
1448 if (puthexnumber(cosa, address) < 0) return -4;
1449 if (put_wait_data(cosa, '\r') == -1) return -5;
1450
1451 if (get_wait_data(cosa) != '\r') return -6;
1452 if (get_wait_data(cosa) != '\r') return -7;
1453 if (get_wait_data(cosa) != '\n') return -8;
1454 if (get_wait_data(cosa) != '\r') return -9;
1455 if (get_wait_data(cosa) != '\n') return -10;
1456#if 0
1457 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1458#endif
1459 return 0;
1460}
1461
1462/*
1463 * Reading memory is done via the "r" command of the SRP monitor.
1464 * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1465 * Then driver can read the data and the conversation is finished
1466 * by SRP monitor sending "<CR><LF>." (dot at the end).
1467 *
1468 * This routine is not needed during the normal operation and serves
1469 * for debugging purposes only.
1470 */
1471static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1472{
1473 if (put_wait_data(cosa, 'r') == -1) return -1;
1474 if ((get_wait_data(cosa)) != 'r') return -2;
1475 if ((get_wait_data(cosa)) != '=') return -3;
1476
1477 if (puthexnumber(cosa, address) < 0) return -4;
1478 if (put_wait_data(cosa, ' ') == -1) return -5;
1479 if (get_wait_data(cosa) != ' ') return -6;
1480 if (get_wait_data(cosa) != '=') return -7;
1481
1482 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1483 if (put_wait_data(cosa, ' ') == -1) return -9;
1484 if (get_wait_data(cosa) != ' ') return -10;
1485
1486 while (length--) {
1487 char c;
1488 int i;
1489 if ((i=get_wait_data(cosa)) == -1) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001490 pr_info("0x%04x bytes remaining\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -11;
1492 }
1493 c=i;
1494#if 1
1495 if (put_user(c, microcode))
1496 return -23; /* ??? */
1497#else
1498 *microcode = c;
1499#endif
1500 microcode++;
1501 }
1502
1503 if (get_wait_data(cosa) != '\r') return -21;
1504 if (get_wait_data(cosa) != '\n') return -22;
1505 if (get_wait_data(cosa) != '.') return -23;
1506#if 0
1507 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1508#endif
1509 return 0;
1510}
1511
1512/*
1513 * This function resets the device and reads the initial prompt
1514 * of the device's ROM monitor.
1515 */
1516static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1517{
1518 int i=0, id=0, prev=0, curr=0;
1519
1520 /* Reset the card ... */
1521 cosa_putstatus(cosa, 0);
1522 cosa_getdata8(cosa);
1523 cosa_putstatus(cosa, SR_RST);
1524#ifdef MODULE
1525 msleep(500);
1526#else
1527 udelay(5*100000);
1528#endif
1529 /* Disable all IRQs from the card */
1530 cosa_putstatus(cosa, 0);
1531
1532 /*
1533 * Try to read the ID string. The card then prints out the
1534 * identification string ended by the "\n\x2e".
1535 *
1536 * The following loop is indexed through i (instead of id)
1537 * to avoid looping forever when for any reason
1538 * the port returns '\r', '\n' or '\x2e' permanently.
1539 */
1540 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1541 if ((curr = get_wait_data(cosa)) == -1) {
1542 return -1;
1543 }
1544 curr &= 0xff;
1545 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1546 idstring[id++] = curr;
1547 if (curr == 0x2e && prev == '\n')
1548 break;
1549 }
1550 /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1551 idstring[id] = '\0';
1552 return id;
1553}
1554
1555
1556/* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1557
1558/*
1559 * This routine gets the data byte from the card waiting for the SR_RX_RDY
1560 * bit to be set in a loop. It should be used in the exceptional cases
1561 * only (for example when resetting the card or downloading the firmware.
1562 */
1563static int get_wait_data(struct cosa_data *cosa)
1564{
1565 int retries = 1000;
1566
1567 while (--retries) {
1568 /* read data and return them */
1569 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1570 short r;
1571 r = cosa_getdata8(cosa);
1572#if 0
Joe Perches9cbe50d2011-06-26 19:01:29 +00001573 pr_info("get_wait_data returning after %d retries\n",
1574 999-retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575#endif
1576 return r;
1577 }
1578 /* sleep if not ready to read */
Nishanth Aravamudan3173c892005-09-11 02:09:55 -07001579 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001581 pr_info("timeout in get_wait_data (status 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 cosa_getstatus(cosa));
1583 return -1;
1584}
1585
1586/*
1587 * This routine puts the data byte to the card waiting for the SR_TX_RDY
1588 * bit to be set in a loop. It should be used in the exceptional cases
1589 * only (for example when resetting the card or downloading the firmware).
1590 */
1591static int put_wait_data(struct cosa_data *cosa, int data)
1592{
1593 int retries = 1000;
1594 while (--retries) {
1595 /* read data and return them */
1596 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1597 cosa_putdata8(cosa, data);
1598#if 0
Joe Perches9cbe50d2011-06-26 19:01:29 +00001599 pr_info("Putdata: %d retries\n", 999-retries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600#endif
1601 return 0;
1602 }
1603#if 0
1604 /* sleep if not ready to read */
Nishanth Aravamudan3173c892005-09-11 02:09:55 -07001605 schedule_timeout_interruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606#endif
1607 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001608 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 cosa->num, cosa_getstatus(cosa));
1610 return -1;
1611}
1612
1613/*
1614 * The following routine puts the hexadecimal number into the SRP monitor
1615 * and verifies the proper echo of the sent bytes. Returns 0 on success,
1616 * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1617 * (-2,-4,-6,-8) means that reading echo failed.
1618 */
1619static int puthexnumber(struct cosa_data *cosa, int number)
1620{
1621 char temp[5];
1622 int i;
1623
1624 /* Well, I should probably replace this by something faster. */
1625 sprintf(temp, "%04X", number);
1626 for (i=0; i<4; i++) {
1627 if (put_wait_data(cosa, temp[i]) == -1) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001628 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1629 cosa->num, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 return -1-2*i;
1631 }
1632 if (get_wait_data(cosa) != temp[i]) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001633 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1634 cosa->num, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return -2-2*i;
1636 }
1637 }
1638 return 0;
1639}
1640
1641
1642/* ---------- Interrupt routines ---------- */
1643
1644/*
1645 * There are three types of interrupt:
1646 * At the beginning of transmit - this handled is in tx_interrupt(),
1647 * at the beginning of receive - it is in rx_interrupt() and
1648 * at the end of transmit/receive - it is the eot_interrupt() function.
1649 * These functions are multiplexed by cosa_interrupt() according to the
1650 * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1651 * separate functions to make it more readable. These functions are inline,
1652 * so there should be no overhead of function call.
1653 *
1654 * In the COSA bus-master mode, we need to tell the card the address of a
1655 * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1656 * It's time to use the bottom half :-(
1657 */
1658
1659/*
1660 * Transmit interrupt routine - called when COSA is willing to obtain
1661 * data from the OS. The most tricky part of the routine is selection
1662 * of channel we (OS) want to send packet for. For SRP we should probably
1663 * use the round-robin approach. The newer COSA firmwares have a simple
1664 * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1665 * channel 0 or 1 doesn't want to receive data.
1666 *
1667 * It seems there is a bug in COSA firmware (need to trace it further):
1668 * When the driver status says that the kernel has no more data for transmit
1669 * (e.g. at the end of TX DMA) and then the kernel changes its mind
1670 * (e.g. new packet is queued to hard_start_xmit()), the card issues
1671 * the TX interrupt but does not mark the channel as ready-to-transmit.
1672 * The fix seems to be to push the packet to COSA despite its request.
1673 * We first try to obey the card's opinion, and then fall back to forced TX.
1674 */
1675static inline void tx_interrupt(struct cosa_data *cosa, int status)
1676{
1677 unsigned long flags, flags1;
1678#ifdef DEBUG_IRQS
Joe Perches9cbe50d2011-06-26 19:01:29 +00001679 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680#endif
1681 spin_lock_irqsave(&cosa->lock, flags);
1682 set_bit(TXBIT, &cosa->rxtx);
1683 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1684 /* flow control, see the comment above */
1685 int i=0;
1686 if (!cosa->txbitmap) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001687 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 cosa->name);
1689 put_driver_status_nolock(cosa);
1690 clear_bit(TXBIT, &cosa->rxtx);
1691 spin_unlock_irqrestore(&cosa->lock, flags);
1692 return;
1693 }
Rudy Matela640462c2009-12-09 11:35:40 -03001694 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 cosa->txchan++;
1696 i++;
1697 if (cosa->txchan >= cosa->nchannels)
1698 cosa->txchan = 0;
1699 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1700 continue;
1701 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1702 break;
1703 /* in second pass, accept first ready-to-TX channel */
1704 if (i > cosa->nchannels) {
1705 /* Can be safely ignored */
1706#ifdef DEBUG_IRQS
1707 printk(KERN_DEBUG "%s: Forcing TX "
1708 "to not-ready channel %d\n",
1709 cosa->name, cosa->txchan);
1710#endif
1711 break;
1712 }
1713 }
1714
1715 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1716 if (cosa_dma_able(cosa->chan+cosa->txchan,
1717 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1718 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1719 } else {
1720 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1721 cosa->txsize);
1722 cosa->txbuf = cosa->bouncebuf;
1723 }
1724 }
1725
1726 if (is_8bit(cosa)) {
1727 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1728 cosa_putstatus(cosa, SR_TX_INT_ENA);
1729 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1730 ((cosa->txsize >> 8) & 0x1f));
1731#ifdef DEBUG_IO
1732 debug_status_out(cosa, SR_TX_INT_ENA);
1733 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1734 ((cosa->txsize >> 8) & 0x1f));
1735 debug_data_in(cosa, cosa_getdata8(cosa));
1736#else
1737 cosa_getdata8(cosa);
1738#endif
1739 set_bit(IRQBIT, &cosa->rxtx);
1740 spin_unlock_irqrestore(&cosa->lock, flags);
1741 return;
1742 } else {
1743 clear_bit(IRQBIT, &cosa->rxtx);
1744 cosa_putstatus(cosa, 0);
1745 cosa_putdata8(cosa, cosa->txsize&0xff);
1746#ifdef DEBUG_IO
1747 debug_status_out(cosa, 0);
1748 debug_data_out(cosa, cosa->txsize&0xff);
1749#endif
1750 }
1751 } else {
1752 cosa_putstatus(cosa, SR_TX_INT_ENA);
1753 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1754 | (cosa->txsize & 0x1fff));
1755#ifdef DEBUG_IO
1756 debug_status_out(cosa, SR_TX_INT_ENA);
1757 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1758 | (cosa->txsize & 0x1fff));
1759 debug_data_in(cosa, cosa_getdata8(cosa));
1760 debug_status_out(cosa, 0);
1761#else
1762 cosa_getdata8(cosa);
1763#endif
1764 cosa_putstatus(cosa, 0);
1765 }
1766
1767 if (cosa->busmaster) {
1768 unsigned long addr = virt_to_bus(cosa->txbuf);
1769 int count=0;
Joe Perches9cbe50d2011-06-26 19:01:29 +00001770 pr_info("busmaster IRQ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1772 count++;
1773 udelay(10);
1774 if (count > 1000) break;
1775 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001776 pr_info("status %x\n", cosa_getstatus(cosa));
1777 pr_info("ready after %d loops\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1779
1780 count = 0;
1781 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1782 count++;
1783 if (count > 1000) break;
1784 udelay(10);
1785 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001786 pr_info("ready after %d loops\n", count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 cosa_putdata16(cosa, addr &0xffff);
1788 flags1 = claim_dma_lock();
1789 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1790 enable_dma(cosa->dma);
1791 release_dma_lock(flags1);
1792 } else {
1793 /* start the DMA */
1794 flags1 = claim_dma_lock();
1795 disable_dma(cosa->dma);
1796 clear_dma_ff(cosa->dma);
1797 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1798 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1799 set_dma_count(cosa->dma, cosa->txsize);
1800 enable_dma(cosa->dma);
1801 release_dma_lock(flags1);
1802 }
1803 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1804#ifdef DEBUG_IO
1805 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1806#endif
1807 spin_unlock_irqrestore(&cosa->lock, flags);
1808}
1809
1810static inline void rx_interrupt(struct cosa_data *cosa, int status)
1811{
1812 unsigned long flags;
1813#ifdef DEBUG_IRQS
Joe Perches9cbe50d2011-06-26 19:01:29 +00001814 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815#endif
1816
1817 spin_lock_irqsave(&cosa->lock, flags);
1818 set_bit(RXBIT, &cosa->rxtx);
1819
1820 if (is_8bit(cosa)) {
1821 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1822 set_bit(IRQBIT, &cosa->rxtx);
1823 put_driver_status_nolock(cosa);
1824 cosa->rxsize = cosa_getdata8(cosa) <<8;
1825#ifdef DEBUG_IO
1826 debug_data_in(cosa, cosa->rxsize >> 8);
1827#endif
1828 spin_unlock_irqrestore(&cosa->lock, flags);
1829 return;
1830 } else {
1831 clear_bit(IRQBIT, &cosa->rxtx);
1832 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1833#ifdef DEBUG_IO
1834 debug_data_in(cosa, cosa->rxsize & 0xff);
1835#endif
1836#if 0
Joe Perches9cbe50d2011-06-26 19:01:29 +00001837 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 cosa->num, cosa->rxsize);
1839#endif
1840 }
1841 } else {
1842 cosa->rxsize = cosa_getdata16(cosa);
1843#ifdef DEBUG_IO
1844 debug_data_in(cosa, cosa->rxsize);
1845#endif
1846#if 0
Joe Perches9cbe50d2011-06-26 19:01:29 +00001847 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 cosa->num, cosa->rxsize);
1849#endif
1850 }
1851 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001852 pr_warn("%s: rx for unknown channel (0x%04x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 cosa->name, cosa->rxsize);
1854 spin_unlock_irqrestore(&cosa->lock, flags);
1855 goto reject;
1856 }
1857 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1858 cosa->rxsize &= 0x1fff;
1859 spin_unlock_irqrestore(&cosa->lock, flags);
1860
1861 cosa->rxbuf = NULL;
1862 if (cosa->rxchan->setup_rx)
1863 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1864
1865 if (!cosa->rxbuf) {
1866reject: /* Reject the packet */
Joe Perches9cbe50d2011-06-26 19:01:29 +00001867 pr_info("cosa%d: rejecting packet on channel %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 cosa->num, cosa->rxchan->num);
1869 cosa->rxbuf = cosa->bouncebuf;
1870 }
1871
1872 /* start the DMA */
1873 flags = claim_dma_lock();
1874 disable_dma(cosa->dma);
1875 clear_dma_ff(cosa->dma);
1876 set_dma_mode(cosa->dma, DMA_MODE_READ);
1877 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1878 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1879 } else {
1880 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1881 }
1882 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1883 enable_dma(cosa->dma);
1884 release_dma_lock(flags);
1885 spin_lock_irqsave(&cosa->lock, flags);
1886 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1887 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1888 cosa_putdata8(cosa, DRIVER_RX_READY);
1889#ifdef DEBUG_IO
1890 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1891 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1892 debug_data_cmd(cosa, DRIVER_RX_READY);
1893#endif
1894 spin_unlock_irqrestore(&cosa->lock, flags);
1895}
1896
1897static inline void eot_interrupt(struct cosa_data *cosa, int status)
1898{
1899 unsigned long flags, flags1;
1900 spin_lock_irqsave(&cosa->lock, flags);
1901 flags1 = claim_dma_lock();
1902 disable_dma(cosa->dma);
1903 clear_dma_ff(cosa->dma);
1904 release_dma_lock(flags1);
1905 if (test_bit(TXBIT, &cosa->rxtx)) {
1906 struct channel_data *chan = cosa->chan+cosa->txchan;
1907 if (chan->tx_done)
1908 if (chan->tx_done(chan, cosa->txsize))
1909 clear_bit(chan->num, &cosa->txbitmap);
1910 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1911#ifdef DEBUG_DATA
1912 {
1913 int i;
Joe Perches9cbe50d2011-06-26 19:01:29 +00001914 pr_info("cosa%dc%d: done rx(0x%x)",
1915 cosa->num, cosa->rxchan->num, cosa->rxsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 for (i=0; i<cosa->rxsize; i++)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001917 pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1918 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920#endif
1921 /* Packet for unknown channel? */
1922 if (cosa->rxbuf == cosa->bouncebuf)
1923 goto out;
1924 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1925 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1926 if (cosa->rxchan->rx_done)
1927 if (cosa->rxchan->rx_done(cosa->rxchan))
1928 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1929 } else {
Joe Perches9cbe50d2011-06-26 19:01:29 +00001930 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932 /*
1933 * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1934 * cleared anyway). We should do it as soon as possible
1935 * so that we can tell the COSA we are done and to give it a time
1936 * for recovery.
1937 */
1938out:
1939 cosa->rxtx = 0;
1940 put_driver_status_nolock(cosa);
1941 spin_unlock_irqrestore(&cosa->lock, flags);
1942}
1943
David Howells7d12e782006-10-05 14:55:46 +01001944static irqreturn_t cosa_interrupt(int irq, void *cosa_)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 unsigned status;
1947 int count = 0;
1948 struct cosa_data *cosa = cosa_;
1949again:
1950 status = cosa_getstatus(cosa);
1951#ifdef DEBUG_IRQS
Joe Perches9cbe50d2011-06-26 19:01:29 +00001952 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953#endif
1954#ifdef DEBUG_IO
1955 debug_status_in(cosa, status);
1956#endif
1957 switch (status & SR_CMD_FROM_SRP_MASK) {
1958 case SR_DOWN_REQUEST:
1959 tx_interrupt(cosa, status);
1960 break;
1961 case SR_UP_REQUEST:
1962 rx_interrupt(cosa, status);
1963 break;
1964 case SR_END_OF_TRANSFER:
1965 eot_interrupt(cosa, status);
1966 break;
1967 default:
1968 /* We may be too fast for SRP. Try to wait a bit more. */
1969 if (count++ < 100) {
1970 udelay(100);
1971 goto again;
1972 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00001973 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 cosa->num, status & 0xff, count);
1975 }
1976#ifdef DEBUG_IRQS
1977 if (count)
Joe Perches9cbe50d2011-06-26 19:01:29 +00001978 pr_info("%s: %d-times got unknown status in IRQ\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 cosa->name, count);
1980 else
Joe Perches9cbe50d2011-06-26 19:01:29 +00001981 pr_info("%s: returning from IRQ\n", cosa->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982#endif
1983 return IRQ_HANDLED;
1984}
1985
1986
1987/* ---------- I/O debugging routines ---------- */
1988/*
1989 * These routines can be used to monitor COSA/SRP I/O and to printk()
1990 * the data being transferred on the data and status I/O port in a
1991 * readable way.
1992 */
1993
1994#ifdef DEBUG_IO
1995static void debug_status_in(struct cosa_data *cosa, int status)
1996{
1997 char *s;
Rudy Matela640462c2009-12-09 11:35:40 -03001998 switch (status & SR_CMD_FROM_SRP_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 case SR_UP_REQUEST:
2000 s = "RX_REQ";
2001 break;
2002 case SR_DOWN_REQUEST:
2003 s = "TX_REQ";
2004 break;
2005 case SR_END_OF_TRANSFER:
2006 s = "ET_REQ";
2007 break;
2008 default:
2009 s = "NO_REQ";
2010 break;
2011 }
Joe Perches9cbe50d2011-06-26 19:01:29 +00002012 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 cosa->name,
2014 status,
Joe Perches9cbe50d2011-06-26 19:01:29 +00002015 status & SR_USR_RQ ? "USR_RQ|" : "",
2016 status & SR_TX_RDY ? "TX_RDY|" : "",
2017 status & SR_RX_RDY ? "RX_RDY|" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 s);
2019}
2020
2021static void debug_status_out(struct cosa_data *cosa, int status)
2022{
Joe Perches9cbe50d2011-06-26 19:01:29 +00002023 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 cosa->name,
2025 status,
Joe Perches9cbe50d2011-06-26 19:01:29 +00002026 status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2027 status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2028 status & SR_RST ? "RESET|" : "",
2029 status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2030 status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2031 status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034static void debug_data_in(struct cosa_data *cosa, int data)
2035{
Joe Perches9cbe50d2011-06-26 19:01:29 +00002036 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
2039static void debug_data_out(struct cosa_data *cosa, int data)
2040{
Joe Perches9cbe50d2011-06-26 19:01:29 +00002041 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
2044static void debug_data_cmd(struct cosa_data *cosa, int data)
2045{
Joe Perches9cbe50d2011-06-26 19:01:29 +00002046 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 cosa->name, data,
2048 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2049 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2050}
2051#endif
2052
2053/* EOF -- this file has not been truncated */