Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cosa.c
Go to the documentation of this file.
1 /* $Id: cosa.c,v 1.31 2000/03/08 17:47:16 kas Exp $ */
2 
3 /*
4  * Copyright (C) 1995-1997 Jan "Yenya" Kasprzak <[email protected]>
5  * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <[email protected]>
6  *
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 <[email protected]>. 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
58  * in one of the two modes (character or network device).
59  *
60  * AUTHOR
61  *
62  * The Linux driver was written by Jan "Yenya" Kasprzak <[email protected]>.
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  */
76 
77 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78 
79 #include <linux/module.h>
80 #include <linux/kernel.h>
81 #include <linux/sched.h>
82 #include <linux/slab.h>
83 #include <linux/poll.h>
84 #include <linux/fs.h>
85 #include <linux/interrupt.h>
86 #include <linux/delay.h>
87 #include <linux/hdlc.h>
88 #include <linux/errno.h>
89 #include <linux/ioport.h>
90 #include <linux/netdevice.h>
91 #include <linux/spinlock.h>
92 #include <linux/mutex.h>
93 #include <linux/device.h>
94 #include <asm/io.h>
95 #include <asm/dma.h>
96 #include <asm/byteorder.h>
97 
98 #undef COSA_SLOW_IO /* for testing purposes only */
99 
100 #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 
110 struct channel_data {
111  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) */
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 */
127  struct mutex rlock;
128  struct semaphore wsem;
129  char *rxdata;
130  int rxsize;
133 
134  /* generic HDLC device parts */
136  struct sk_buff *rx_skb, *tx_skb;
137 };
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 
144 struct 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. */
154  unsigned long rxbitmap, txbitmap;/* Bitmap of channels who are willing to send/receive data */
155  unsigned long rxtx; /* RX or TX in progress? */
156  int enabled;
157  int usage; /* usage count */
160  char *bouncebuf;
161  char *txbuf, *rxbuf;
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  */
178 static DEFINE_MUTEX(cosa_chardev_mutex);
179 static 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)) */
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
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 */
214 #define TX_TIMEOUT (5*HZ)
215 
216 /* Maybe the following should be allocated dynamically */
217 static struct cosa_data cosa_cards[MAX_CARDS];
218 static int nr_cards;
219 
220 #ifdef COSA_ISA_AUTOPROBE
221 static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
222 /* NOTE: DMA is not autoprobed!!! */
223 static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
224 #else
225 static int io[MAX_CARDS+1];
226 static int dma[MAX_CARDS+1];
227 #endif
228 /* IRQ can be safely autoprobed */
229 static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
230 
231 /* for class stuff*/
232 static struct class *cosa_class;
233 
234 #ifdef MODULE
235 module_param_array(io, int, NULL, 0);
236 MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
237 module_param_array(irq, int, NULL, 0);
238 MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
239 module_param_array(dma, int, NULL, 0);
240 MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
241 
242 MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <[email protected]>");
243 MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
244 MODULE_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
260 #define is_8bit(cosa) (!(cosa->datareg & 0x08))
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 */
270 static int cosa_probe(int ioaddr, int irq, int dma);
271 
272 /* HW interface */
273 static void cosa_enable_rx(struct channel_data *chan);
274 static void cosa_disable_rx(struct channel_data *chan);
275 static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
276 static void cosa_kick(struct cosa_data *cosa);
277 static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
278 
279 /* Network device stuff */
280 static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
281  unsigned short parity);
282 static int cosa_net_open(struct net_device *d);
283 static int cosa_net_close(struct net_device *d);
284 static void cosa_net_timeout(struct net_device *d);
285 static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
286 static char *cosa_net_setup_rx(struct channel_data *channel, int size);
287 static int cosa_net_rx_done(struct channel_data *channel);
288 static int cosa_net_tx_done(struct channel_data *channel, int size);
289 static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
290 
291 /* Character device */
292 static char *chrdev_setup_rx(struct channel_data *channel, int size);
293 static int chrdev_rx_done(struct channel_data *channel);
294 static int chrdev_tx_done(struct channel_data *channel, int size);
295 static ssize_t cosa_read(struct file *file,
296  char __user *buf, size_t count, loff_t *ppos);
297 static ssize_t cosa_write(struct file *file,
298  const char __user *buf, size_t count, loff_t *ppos);
299 static unsigned int cosa_poll(struct file *file, poll_table *poll);
300 static int cosa_open(struct inode *inode, struct file *file);
301 static int cosa_release(struct inode *inode, struct file *file);
302 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
303  unsigned long arg);
304 #ifdef COSA_FASYNC_WORKING
305 static int cosa_fasync(struct inode *inode, struct file *file, int on);
306 #endif
307 
308 static const struct file_operations cosa_fops = {
309  .owner = THIS_MODULE,
310  .llseek = no_llseek,
311  .read = cosa_read,
312  .write = cosa_write,
313  .poll = cosa_poll,
314  .unlocked_ioctl = cosa_chardev_ioctl,
315  .open = cosa_open,
316  .release = cosa_release,
317 #ifdef COSA_FASYNC_WORKING
318  .fasync = cosa_fasync,
319 #endif
320 };
321 
322 /* Ioctls */
323 static int cosa_start(struct cosa_data *cosa, int address);
324 static int cosa_reset(struct cosa_data *cosa);
325 static int cosa_download(struct cosa_data *cosa, void __user *a);
326 static int cosa_readmem(struct cosa_data *cosa, void __user *a);
327 
328 /* COSA/SRP ROM monitor */
329 static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
330 static int startmicrocode(struct cosa_data *cosa, int address);
331 static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
332 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
333 
334 /* Auxiliary functions */
335 static int get_wait_data(struct cosa_data *cosa);
336 static int put_wait_data(struct cosa_data *cosa, int data);
337 static int puthexnumber(struct cosa_data *cosa, int number);
338 static void put_driver_status(struct cosa_data *cosa);
339 static void put_driver_status_nolock(struct cosa_data *cosa);
340 
341 /* Interrupt handling */
342 static irqreturn_t cosa_interrupt(int irq, void *cosa);
343 
344 /* I/O ops debugging */
345 #ifdef DEBUG_IO
346 static void debug_data_in(struct cosa_data *cosa, int data);
347 static void debug_data_out(struct cosa_data *cosa, int data);
348 static void debug_data_cmd(struct cosa_data *cosa, int data);
349 static void debug_status_in(struct cosa_data *cosa, int status);
350 static void debug_status_out(struct cosa_data *cosa, int status);
351 #endif
352 
353 static inline struct channel_data* dev_to_chan(struct net_device *dev)
354 {
355  return (struct channel_data *)dev_to_hdlc(dev)->priv;
356 }
357 
358 /* ---------- Initialization stuff ---------- */
359 
360 static int __init cosa_init(void)
361 {
362  int i, err = 0;
363 
364  if (cosa_major > 0) {
365  if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
366  pr_warn("unable to get major %d\n", cosa_major);
367  err = -EIO;
368  goto out;
369  }
370  } else {
371  if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
372  pr_warn("unable to register chardev\n");
373  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) {
382  pr_warn("no devices found\n");
383  unregister_chrdev(cosa_major, "cosa");
384  err = -ENODEV;
385  goto out;
386  }
387  cosa_class = class_create(THIS_MODULE, "cosa");
388  if (IS_ERR(cosa_class)) {
389  err = PTR_ERR(cosa_class);
390  goto out_chrdev;
391  }
392  for (i = 0; i < nr_cards; i++)
393  device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
394  "cosa%d", i);
395  err = 0;
396  goto out;
397 
398 out_chrdev:
399  unregister_chrdev(cosa_major, "cosa");
400 out:
401  return err;
402 }
403 module_init(cosa_init);
404 
405 static void __exit cosa_exit(void)
406 {
407  struct cosa_data *cosa;
408  int i;
409 
410  for (i = 0; i < nr_cards; i++)
411  device_destroy(cosa_class, MKDEV(cosa_major, i));
412  class_destroy(cosa_class);
413 
414  for (cosa = cosa_cards; nr_cards--; cosa++) {
415  /* Clean up the per-channel data */
416  for (i = 0; i < cosa->nchannels; i++) {
417  /* Chardev driver has no alloc'd per-channel data */
418  unregister_hdlc_device(cosa->chan[i].netdev);
419  free_netdev(cosa->chan[i].netdev);
420  }
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);
426  release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
427  }
428  unregister_chrdev(cosa_major, "cosa");
429 }
430 module_exit(cosa_exit);
431 
432 static 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 
441 static 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)) {
451  pr_info("invalid IRQ %d\n", irq);
452  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) {
457  pr_info("invalid I/O address 0x%x\n", base);
458  return -1;
459  }
460  /* DMA should be 0,1 or 3-7 */
461  if (dma < 0 || dma == 4 || dma > 7) {
462  pr_info("invalid DMA %d\n", dma);
463  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)) {
468  pr_info("8/16 bit base and DMA mismatch (base=0x%x, dma=%d)\n",
469  base, dma);
470  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) {
482  printk(KERN_DEBUG "probe at 0x%x failed.\n", base);
483  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
495  pr_info("valid signature not found at 0x%x\n", 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 "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 /* pr_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  */
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  pr_info("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  pr_info("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 
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 */
562  cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
563  if (!cosa->chan) {
564  err = -ENOMEM;
565  goto err_out3;
566  }
567 
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  sema_init(&chan->wsem, 1);
578 
579  /* Register the network interface */
580  if (!(chan->netdev = alloc_hdlcdev(chan))) {
581  pr_warn("%s: alloc_hdlcdev failed\n", chan->name);
582  goto err_hdlcdev;
583  }
584  dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
585  dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
586  chan->netdev->netdev_ops = &cosa_ops;
587  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)) {
592  netdev_warn(chan->netdev,
593  "register_hdlc_device() failed\n");
594  free_netdev(chan->netdev);
595  goto err_hdlcdev;
596  }
597  }
598 
599  pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
600  cosa->num, cosa->id_string, cosa->type,
601  cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
602 
603  return nr_cards++;
604 
605 err_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);
611 err_out3:
612  kfree(cosa->bouncebuf);
613 err_out2:
614  free_dma(cosa->dma);
615 err_out1:
616  free_irq(cosa->irq, cosa);
617 err_out:
618  release_region(cosa->datareg,is_8bit(cosa)?2:4);
619  pr_notice("cosa%d: allocating resources failed\n", cosa->num);
620  return err;
621 }
622 
623 
624 /*---------- network device ---------- */
625 
626 static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
627  unsigned short parity)
628 {
629  if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
630  return 0;
631  return -EINVAL;
632 }
633 
634 static int cosa_net_open(struct net_device *dev)
635 {
636  struct channel_data *chan = dev_to_chan(dev);
637  int err;
638  unsigned long flags;
639 
640  if (!(chan->cosa->firmware_status & COSA_FW_START)) {
641  pr_notice("%s: start the firmware first (status %d)\n",
642  chan->cosa->name, chan->cosa->firmware_status);
643  return -EPERM;
644  }
645  spin_lock_irqsave(&chan->cosa->lock, flags);
646  if (chan->usage != 0) {
647  pr_warn("%s: cosa_net_open called with usage count %d\n",
648  chan->name, chan->usage);
649  spin_unlock_irqrestore(&chan->cosa->lock, flags);
650  return -EBUSY;
651  }
652  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;
656  chan->cosa->usage++;
657  spin_unlock_irqrestore(&chan->cosa->lock, flags);
658 
659  err = hdlc_open(dev);
660  if (err) {
661  spin_lock_irqsave(&chan->cosa->lock, flags);
662  chan->usage = 0;
663  chan->cosa->usage--;
664  spin_unlock_irqrestore(&chan->cosa->lock, flags);
665  return err;
666  }
667 
668  netif_start_queue(dev);
669  cosa_enable_rx(chan);
670  return 0;
671 }
672 
673 static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
674  struct net_device *dev)
675 {
676  struct channel_data *chan = dev_to_chan(dev);
677 
678  netif_stop_queue(dev);
679 
680  chan->tx_skb = skb;
681  cosa_start_tx(chan, skb->data, skb->len);
682  return NETDEV_TX_OK;
683 }
684 
685 static void cosa_net_timeout(struct net_device *dev)
686 {
687  struct channel_data *chan = dev_to_chan(dev);
688 
689  if (test_bit(RXBIT, &chan->cosa->rxtx)) {
690  chan->netdev->stats.rx_errors++;
691  chan->netdev->stats.rx_missed_errors++;
692  } else {
693  chan->netdev->stats.tx_errors++;
694  chan->netdev->stats.tx_aborted_errors++;
695  }
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 
704 static int cosa_net_close(struct net_device *dev)
705 {
706  struct channel_data *chan = dev_to_chan(dev);
707  unsigned long flags;
708 
709  netif_stop_queue(dev);
710  hdlc_close(dev);
711  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  }
721  chan->usage = 0;
722  chan->cosa->usage--;
723  spin_unlock_irqrestore(&chan->cosa->lock, flags);
724  return 0;
725 }
726 
727 static char *cosa_net_setup_rx(struct channel_data *chan, int size)
728 {
729  /*
730  * We can safely fall back to non-dma-able memory, because we have
731  * the cosa->bouncebuf pre-allocated.
732  */
733  kfree_skb(chan->rx_skb);
734  chan->rx_skb = dev_alloc_skb(size);
735  if (chan->rx_skb == NULL) {
736  pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
737  chan->netdev->stats.rx_dropped++;
738  return NULL;
739  }
740  chan->netdev->trans_start = jiffies;
741  return skb_put(chan->rx_skb, size);
742 }
743 
744 static int cosa_net_rx_done(struct channel_data *chan)
745 {
746  if (!chan->rx_skb) {
747  pr_warn("%s: rx_done with empty skb!\n", chan->name);
748  chan->netdev->stats.rx_errors++;
749  chan->netdev->stats.rx_frame_errors++;
750  return 0;
751  }
752  chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
753  chan->rx_skb->dev = chan->netdev;
754  skb_reset_mac_header(chan->rx_skb);
755  chan->netdev->stats.rx_packets++;
756  chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
757  netif_rx(chan->rx_skb);
758  chan->rx_skb = NULL;
759  return 0;
760 }
761 
762 /* ARGSUSED */
763 static int cosa_net_tx_done(struct channel_data *chan, int size)
764 {
765  if (!chan->tx_skb) {
766  pr_warn("%s: tx_done with empty skb!\n", chan->name);
767  chan->netdev->stats.tx_errors++;
768  chan->netdev->stats.tx_aborted_errors++;
769  return 1;
770  }
771  dev_kfree_skb_irq(chan->tx_skb);
772  chan->tx_skb = NULL;
773  chan->netdev->stats.tx_packets++;
774  chan->netdev->stats.tx_bytes += size;
775  netif_wake_queue(chan->netdev);
776  return 1;
777 }
778 
779 /*---------- Character device ---------- */
780 
781 static ssize_t cosa_read(struct file *file,
782  char __user *buf, size_t count, loff_t *ppos)
783 {
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)) {
791  pr_notice("%s: start the firmware first (status %d)\n",
792  cosa->name, cosa->firmware_status);
793  return -EPERM;
794  }
795  if (mutex_lock_interruptible(&chan->rlock))
796  return -ERESTARTSYS;
797 
798  if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
799  pr_info("%s: cosa_read() - OOM\n", cosa->name);
800  mutex_unlock(&chan->rlock);
801  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);
808  while (!chan->rx_status) {
809  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);
818  mutex_unlock(&chan->rlock);
819  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);
827  mutex_unlock(&chan->rlock);
828 
829  if (copy_to_user(buf, kbuf, count)) {
830  kfree(kbuf);
831  return -EFAULT;
832  }
833  kfree(kbuf);
834  return count;
835 }
836 
837 static 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 
844 static 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;
852  return 1;
853 }
854 
855 
856 static ssize_t cosa_write(struct file *file,
857  const char __user *buf, size_t count, loff_t *ppos)
858 {
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)) {
866  pr_notice("%s: start the firmware first (status %d)\n",
867  cosa->name, cosa->firmware_status);
868  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 */
877  if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
878  pr_notice("%s: cosa_write() OOM - dropping packet\n",
879  cosa->name);
880  up(&chan->wsem);
881  return -ENOMEM;
882  }
883  if (copy_from_user(kbuf, buf, count)) {
884  up(&chan->wsem);
885  kfree(kbuf);
886  return -EFAULT;
887  }
888  chan->tx_status=0;
889  cosa_start_tx(chan, kbuf, count);
890 
891  spin_lock_irqsave(&cosa->lock, flags);
892  add_wait_queue(&chan->txwaitq, &wait);
893  while (!chan->tx_status) {
894  current->state = TASK_INTERRUPTIBLE;
895  spin_unlock_irqrestore(&cosa->lock, flags);
896  schedule();
897  spin_lock_irqsave(&cosa->lock, flags);
898  if (signal_pending(current) && chan->tx_status == 0) {
899  chan->tx_status = 1;
900  remove_wait_queue(&chan->txwaitq, &wait);
901  current->state = TASK_RUNNING;
902  chan->tx_status = 1;
903  spin_unlock_irqrestore(&cosa->lock, flags);
904  up(&chan->wsem);
905  return -ERESTARTSYS;
906  }
907  }
908  remove_wait_queue(&chan->txwaitq, &wait);
909  current->state = TASK_RUNNING;
910  up(&chan->wsem);
911  spin_unlock_irqrestore(&cosa->lock, flags);
912  kfree(kbuf);
913  return count;
914 }
915 
916 static int chrdev_tx_done(struct channel_data *chan, int size)
917 {
918  if (chan->tx_status) { /* Writer was interrupted */
919  kfree(chan->txbuf);
920  up(&chan->wsem);
921  }
922  chan->tx_status = 1;
924  return 1;
925 }
926 
927 static unsigned int cosa_poll(struct file *file, poll_table *poll)
928 {
929  pr_info("cosa_poll is here\n");
930  return 0;
931 }
932 
933 static int cosa_open(struct inode *inode, struct file *file)
934 {
935  struct cosa_data *cosa;
936  struct channel_data *chan;
937  unsigned long flags;
938  int n;
939  int ret = 0;
940 
941  mutex_lock(&cosa_chardev_mutex);
942  if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
943  >= nr_cards) {
944  ret = -ENODEV;
945  goto out;
946  }
947  cosa = cosa_cards+n;
948 
949  if ((n=iminor(file->f_path.dentry->d_inode)
950  & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
951  ret = -ENODEV;
952  goto out;
953  }
954  chan = cosa->chan + n;
955 
956  file->private_data = chan;
957 
958  spin_lock_irqsave(&cosa->lock, flags);
959 
960  if (chan->usage < 0) { /* in netdev mode */
961  spin_unlock_irqrestore(&cosa->lock, flags);
962  ret = -EBUSY;
963  goto out;
964  }
965  cosa->usage++;
966  chan->usage++;
967 
968  chan->tx_done = chrdev_tx_done;
969  chan->setup_rx = chrdev_setup_rx;
970  chan->rx_done = chrdev_rx_done;
971  spin_unlock_irqrestore(&cosa->lock, flags);
972 out:
973  mutex_unlock(&cosa_chardev_mutex);
974  return ret;
975 }
976 
977 static int cosa_release(struct inode *inode, struct file *file)
978 {
979  struct channel_data *channel = file->private_data;
980  struct cosa_data *cosa;
981  unsigned long flags;
982 
983  cosa = channel->cosa;
984  spin_lock_irqsave(&cosa->lock, flags);
985  cosa->usage--;
986  channel->usage--;
987  spin_unlock_irqrestore(&cosa->lock, flags);
988  return 0;
989 }
990 
991 #ifdef COSA_FASYNC_WORKING
992 static struct fasync_struct *fasync[256] = { NULL, };
993 
994 /* To be done ... */
995 static int cosa_fasync(struct inode *inode, struct file *file, int on)
996 {
997  int port = iminor(inode);
998 
999  return fasync_helper(inode, file, on, &fasync[port]);
1000 }
1001 #endif
1002 
1003 
1004 /* ---------- Ioctls ---------- */
1005 
1006 /*
1007  * Ioctl subroutines can safely be made inline, because they are called
1008  * only from cosa_ioctl().
1009  */
1010 static inline int cosa_reset(struct cosa_data *cosa)
1011 {
1012  char idstring[COSA_MAX_ID_STRING];
1013  if (cosa->usage > 1)
1014  pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1015  cosa->num, cosa->usage);
1017  if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1018  pr_notice("cosa%d: reset failed\n", cosa->num);
1019  return -EIO;
1020  }
1021  pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1022  cosa->firmware_status |= COSA_FW_RESET;
1023  return 0;
1024 }
1025 
1026 /* High-level function to download data into COSA memory. Calls download() */
1027 static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1028 {
1029  struct cosa_download d;
1030  int i;
1031 
1032  if (cosa->usage > 1)
1033  pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1034  cosa->name, cosa->usage);
1035  if (!(cosa->firmware_status & COSA_FW_RESET)) {
1036  pr_notice("%s: reset the card first (status %d)\n",
1037  cosa->name, cosa->firmware_status);
1038  return -EPERM;
1039  }
1040 
1041  if (copy_from_user(&d, arg, sizeof(d)))
1042  return -EFAULT;
1043 
1044  if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1045  return -EINVAL;
1046  if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1047  return -EINVAL;
1048 
1049 
1050  /* If something fails, force the user to reset the card */
1052 
1053  i = download(cosa, d.code, d.len, d.addr);
1054  if (i < 0) {
1055  pr_notice("cosa%d: microcode download failed: %d\n",
1056  cosa->num, i);
1057  return -EIO;
1058  }
1059  pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1060  cosa->num, d.len, d.addr);
1062  return 0;
1063 }
1064 
1065 /* High-level function to read COSA memory. Calls readmem() */
1066 static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1067 {
1068  struct cosa_download d;
1069  int i;
1070 
1071  if (cosa->usage > 1)
1072  pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1073  cosa->num, cosa->usage);
1074  if (!(cosa->firmware_status & COSA_FW_RESET)) {
1075  pr_notice("%s: reset the card first (status %d)\n",
1076  cosa->name, cosa->firmware_status);
1077  return -EPERM;
1078  }
1079 
1080  if (copy_from_user(&d, arg, sizeof(d)))
1081  return -EFAULT;
1082 
1083  /* If something fails, force the user to reset the card */
1084  cosa->firmware_status &= ~COSA_FW_RESET;
1085 
1086  i = readmem(cosa, d.code, d.len, d.addr);
1087  if (i < 0) {
1088  pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1089  return -EIO;
1090  }
1091  pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1092  cosa->num, d.len, d.addr);
1093  cosa->firmware_status |= COSA_FW_RESET;
1094  return 0;
1095 }
1096 
1097 /* High-level function to start microcode. Calls startmicrocode(). */
1098 static inline int cosa_start(struct cosa_data *cosa, int address)
1099 {
1100  int i;
1101 
1102  if (cosa->usage > 1)
1103  pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1104  cosa->num, cosa->usage);
1105 
1108  pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1109  cosa->name, cosa->firmware_status);
1110  return -EPERM;
1111  }
1112  cosa->firmware_status &= ~COSA_FW_RESET;
1113  if ((i=startmicrocode(cosa, address)) < 0) {
1114  pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1115  cosa->num, address, i);
1116  return -EIO;
1117  }
1118  pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1119  cosa->startaddr = address;
1120  cosa->firmware_status |= COSA_FW_START;
1121  return 0;
1122 }
1123 
1124 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1125 static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1126 {
1127  int l = strlen(cosa->id_string)+1;
1128  if (copy_to_user(string, cosa->id_string, l))
1129  return -EFAULT;
1130  return l;
1131 }
1132 
1133 /* Buffer of size at least COSA_MAX_ID_STRING is expected */
1134 static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1135 {
1136  int l = strlen(cosa->type)+1;
1137  if (copy_to_user(string, cosa->type, l))
1138  return -EFAULT;
1139  return l;
1140 }
1141 
1142 static int cosa_ioctl_common(struct cosa_data *cosa,
1143  struct channel_data *channel, unsigned int cmd, unsigned long arg)
1144 {
1145  void __user *argp = (void __user *)arg;
1146  switch (cmd) {
1147  case COSAIORSET: /* Reset the device */
1148  if (!capable(CAP_NET_ADMIN))
1149  return -EACCES;
1150  return cosa_reset(cosa);
1151  case COSAIOSTRT: /* Start the firmware */
1152  if (!capable(CAP_SYS_RAWIO))
1153  return -EACCES;
1154  return cosa_start(cosa, arg);
1155  case COSAIODOWNLD: /* Download the firmware */
1156  if (!capable(CAP_SYS_RAWIO))
1157  return -EACCES;
1158 
1159  return cosa_download(cosa, argp);
1160  case COSAIORMEM:
1161  if (!capable(CAP_SYS_RAWIO))
1162  return -EACCES;
1163  return cosa_readmem(cosa, argp);
1164  case COSAIORTYPE:
1165  return cosa_gettype(cosa, argp);
1166  case COSAIORIDSTR:
1167  return cosa_getidstr(cosa, argp);
1168  case COSAIONRCARDS:
1169  return nr_cards;
1170  case COSAIONRCHANS:
1171  return cosa->nchannels;
1172  case COSAIOBMSET:
1173  if (!capable(CAP_SYS_RAWIO))
1174  return -EACCES;
1175  if (is_8bit(cosa))
1176  return -EINVAL;
1177  if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1178  return -EINVAL;
1179  cosa->busmaster = arg;
1180  return 0;
1181  case COSAIOBMGET:
1182  return cosa->busmaster;
1183  }
1184  return -ENOIOCTLCMD;
1185 }
1186 
1187 static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1188 {
1189  int rv;
1190  struct channel_data *chan = dev_to_chan(dev);
1191  rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1192  (unsigned long)ifr->ifr_data);
1193  if (rv != -ENOIOCTLCMD)
1194  return rv;
1195  return hdlc_ioctl(dev, ifr, cmd);
1196 }
1197 
1198 static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1199  unsigned long arg)
1200 {
1201  struct channel_data *channel = file->private_data;
1202  struct cosa_data *cosa;
1203  long ret;
1204 
1205  mutex_lock(&cosa_chardev_mutex);
1206  cosa = channel->cosa;
1207  ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1208  mutex_unlock(&cosa_chardev_mutex);
1209  return ret;
1210 }
1211 
1212 
1213 /*---------- HW layer interface ---------- */
1214 
1215 /*
1216  * The higher layer can bind itself to the HW layer by setting the callbacks
1217  * in the channel_data structure and by using these routines.
1218  */
1219 static void cosa_enable_rx(struct channel_data *chan)
1220 {
1221  struct cosa_data *cosa = chan->cosa;
1222 
1223  if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1224  put_driver_status(cosa);
1225 }
1226 
1227 static void cosa_disable_rx(struct channel_data *chan)
1228 {
1229  struct cosa_data *cosa = chan->cosa;
1230 
1231  if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1232  put_driver_status(cosa);
1233 }
1234 
1235 /*
1236  * FIXME: This routine probably should check for cosa_start_tx() called when
1237  * the previous transmit is still unfinished. In this case the non-zero
1238  * return value should indicate to the caller that the queuing(sp?) up
1239  * the transmit has failed.
1240  */
1241 static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1242 {
1243  struct cosa_data *cosa = chan->cosa;
1244  unsigned long flags;
1245 #ifdef DEBUG_DATA
1246  int i;
1247 
1248  pr_info("cosa%dc%d: starting tx(0x%x)",
1249  chan->cosa->num, chan->num, len);
1250  for (i=0; i<len; i++)
1251  pr_cont(" %02x", buf[i]&0xff);
1252  pr_cont("\n");
1253 #endif
1254  spin_lock_irqsave(&cosa->lock, flags);
1255  chan->txbuf = buf;
1256  chan->txsize = len;
1257  if (len > COSA_MTU)
1258  chan->txsize = COSA_MTU;
1259  spin_unlock_irqrestore(&cosa->lock, flags);
1260 
1261  /* Tell the firmware we are ready */
1262  set_bit(chan->num, &cosa->txbitmap);
1263  put_driver_status(cosa);
1264 
1265  return 0;
1266 }
1267 
1268 static void put_driver_status(struct cosa_data *cosa)
1269 {
1270  unsigned long flags;
1271  int status;
1272 
1273  spin_lock_irqsave(&cosa->lock, flags);
1274 
1275  status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1276  | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1277  | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1278  &DRIVER_TXMAP_MASK : 0);
1279  if (!cosa->rxtx) {
1280  if (cosa->rxbitmap|cosa->txbitmap) {
1281  if (!cosa->enabled) {
1282  cosa_putstatus(cosa, SR_RX_INT_ENA);
1283 #ifdef DEBUG_IO
1284  debug_status_out(cosa, SR_RX_INT_ENA);
1285 #endif
1286  cosa->enabled = 1;
1287  }
1288  } else if (cosa->enabled) {
1289  cosa->enabled = 0;
1290  cosa_putstatus(cosa, 0);
1291 #ifdef DEBUG_IO
1292  debug_status_out(cosa, 0);
1293 #endif
1294  }
1295  cosa_putdata8(cosa, status);
1296 #ifdef DEBUG_IO
1297  debug_data_cmd(cosa, status);
1298 #endif
1299  }
1300  spin_unlock_irqrestore(&cosa->lock, flags);
1301 }
1302 
1303 static void put_driver_status_nolock(struct cosa_data *cosa)
1304 {
1305  int status;
1306 
1307  status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1308  | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1309  | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1310  &DRIVER_TXMAP_MASK : 0);
1311 
1312  if (cosa->rxbitmap|cosa->txbitmap) {
1313  cosa_putstatus(cosa, SR_RX_INT_ENA);
1314 #ifdef DEBUG_IO
1315  debug_status_out(cosa, SR_RX_INT_ENA);
1316 #endif
1317  cosa->enabled = 1;
1318  } else {
1319  cosa_putstatus(cosa, 0);
1320 #ifdef DEBUG_IO
1321  debug_status_out(cosa, 0);
1322 #endif
1323  cosa->enabled = 0;
1324  }
1325  cosa_putdata8(cosa, status);
1326 #ifdef DEBUG_IO
1327  debug_data_cmd(cosa, status);
1328 #endif
1329 }
1330 
1331 /*
1332  * The "kickme" function: When the DMA times out, this is called to
1333  * clean up the driver status.
1334  * FIXME: Preliminary support, the interface is probably wrong.
1335  */
1336 static void cosa_kick(struct cosa_data *cosa)
1337 {
1338  unsigned long flags, flags1;
1339  char *s = "(probably) IRQ";
1340 
1341  if (test_bit(RXBIT, &cosa->rxtx))
1342  s = "RX DMA";
1343  if (test_bit(TXBIT, &cosa->rxtx))
1344  s = "TX DMA";
1345 
1346  pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1347  spin_lock_irqsave(&cosa->lock, flags);
1348  cosa->rxtx = 0;
1349 
1350  flags1 = claim_dma_lock();
1351  disable_dma(cosa->dma);
1352  clear_dma_ff(cosa->dma);
1353  release_dma_lock(flags1);
1354 
1355  /* FIXME: Anything else? */
1356  udelay(100);
1357  cosa_putstatus(cosa, 0);
1358  udelay(100);
1359  (void) cosa_getdata8(cosa);
1360  udelay(100);
1361  cosa_putdata8(cosa, 0);
1362  udelay(100);
1363  put_driver_status_nolock(cosa);
1364  spin_unlock_irqrestore(&cosa->lock, flags);
1365 }
1366 
1367 /*
1368  * Check if the whole buffer is DMA-able. It means it is below the 16M of
1369  * physical memory and doesn't span the 64k boundary. For now it seems
1370  * SKB's never do this, but we'll check this anyway.
1371  */
1372 static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1373 {
1374  static int count;
1375  unsigned long b = (unsigned long)buf;
1376  if (b+len >= MAX_DMA_ADDRESS)
1377  return 0;
1378  if ((b^ (b+len)) & 0x10000) {
1379  if (count++ < 5)
1380  pr_info("%s: packet spanning a 64k boundary\n",
1381  chan->name);
1382  return 0;
1383  }
1384  return 1;
1385 }
1386 
1387 
1388 /* ---------- The SRP/COSA ROM monitor functions ---------- */
1389 
1390 /*
1391  * Downloading SRP microcode: say "w" to SRP monitor, it answers by "w=",
1392  * drivers need to say 4-digit hex number meaning start address of the microcode
1393  * separated by a single space. Monitor replies by saying " =". Now driver
1394  * has to write 4-digit hex number meaning the last byte address ended
1395  * by a single space. Monitor has to reply with a space. Now the download
1396  * begins. After the download monitor replies with "\r\n." (CR LF dot).
1397  */
1398 static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1399 {
1400  int i;
1401 
1402  if (put_wait_data(cosa, 'w') == -1) return -1;
1403  if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1404  if (get_wait_data(cosa) != '=') return -3;
1405 
1406  if (puthexnumber(cosa, address) < 0) return -4;
1407  if (put_wait_data(cosa, ' ') == -1) return -10;
1408  if (get_wait_data(cosa) != ' ') return -11;
1409  if (get_wait_data(cosa) != '=') return -12;
1410 
1411  if (puthexnumber(cosa, address+length-1) < 0) return -13;
1412  if (put_wait_data(cosa, ' ') == -1) return -18;
1413  if (get_wait_data(cosa) != ' ') return -19;
1414 
1415  while (length--) {
1416  char c;
1417 #ifndef SRP_DOWNLOAD_AT_BOOT
1418  if (get_user(c, microcode))
1419  return -23; /* ??? */
1420 #else
1421  c = *microcode;
1422 #endif
1423  if (put_wait_data(cosa, c) == -1)
1424  return -20;
1425  microcode++;
1426  }
1427 
1428  if (get_wait_data(cosa) != '\r') return -21;
1429  if (get_wait_data(cosa) != '\n') return -22;
1430  if (get_wait_data(cosa) != '.') return -23;
1431 #if 0
1432  printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1433 #endif
1434  return 0;
1435 }
1436 
1437 
1438 /*
1439  * Starting microcode is done via the "g" command of the SRP monitor.
1440  * The chat should be the following: "g" "g=" "<addr><CR>"
1441  * "<CR><CR><LF><CR><LF>".
1442  */
1443 static int startmicrocode(struct cosa_data *cosa, int address)
1444 {
1445  if (put_wait_data(cosa, 'g') == -1) return -1;
1446  if (get_wait_data(cosa) != 'g') return -2;
1447  if (get_wait_data(cosa) != '=') return -3;
1448 
1449  if (puthexnumber(cosa, address) < 0) return -4;
1450  if (put_wait_data(cosa, '\r') == -1) return -5;
1451 
1452  if (get_wait_data(cosa) != '\r') return -6;
1453  if (get_wait_data(cosa) != '\r') return -7;
1454  if (get_wait_data(cosa) != '\n') return -8;
1455  if (get_wait_data(cosa) != '\r') return -9;
1456  if (get_wait_data(cosa) != '\n') return -10;
1457 #if 0
1458  printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1459 #endif
1460  return 0;
1461 }
1462 
1463 /*
1464  * Reading memory is done via the "r" command of the SRP monitor.
1465  * The chat is the following "r" "r=" "<addr> " " =" "<last_byte> " " "
1466  * Then driver can read the data and the conversation is finished
1467  * by SRP monitor sending "<CR><LF>." (dot at the end).
1468  *
1469  * This routine is not needed during the normal operation and serves
1470  * for debugging purposes only.
1471  */
1472 static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1473 {
1474  if (put_wait_data(cosa, 'r') == -1) return -1;
1475  if ((get_wait_data(cosa)) != 'r') return -2;
1476  if ((get_wait_data(cosa)) != '=') return -3;
1477 
1478  if (puthexnumber(cosa, address) < 0) return -4;
1479  if (put_wait_data(cosa, ' ') == -1) return -5;
1480  if (get_wait_data(cosa) != ' ') return -6;
1481  if (get_wait_data(cosa) != '=') return -7;
1482 
1483  if (puthexnumber(cosa, address+length-1) < 0) return -8;
1484  if (put_wait_data(cosa, ' ') == -1) return -9;
1485  if (get_wait_data(cosa) != ' ') return -10;
1486 
1487  while (length--) {
1488  char c;
1489  int i;
1490  if ((i=get_wait_data(cosa)) == -1) {
1491  pr_info("0x%04x bytes remaining\n", length);
1492  return -11;
1493  }
1494  c=i;
1495 #if 1
1496  if (put_user(c, microcode))
1497  return -23; /* ??? */
1498 #else
1499  *microcode = c;
1500 #endif
1501  microcode++;
1502  }
1503 
1504  if (get_wait_data(cosa) != '\r') return -21;
1505  if (get_wait_data(cosa) != '\n') return -22;
1506  if (get_wait_data(cosa) != '.') return -23;
1507 #if 0
1508  printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1509 #endif
1510  return 0;
1511 }
1512 
1513 /*
1514  * This function resets the device and reads the initial prompt
1515  * of the device's ROM monitor.
1516  */
1517 static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1518 {
1519  int i=0, id=0, prev=0, curr=0;
1520 
1521  /* Reset the card ... */
1522  cosa_putstatus(cosa, 0);
1523  cosa_getdata8(cosa);
1524  cosa_putstatus(cosa, SR_RST);
1525 #ifdef MODULE
1526  msleep(500);
1527 #else
1528  udelay(5*100000);
1529 #endif
1530  /* Disable all IRQs from the card */
1531  cosa_putstatus(cosa, 0);
1532 
1533  /*
1534  * Try to read the ID string. The card then prints out the
1535  * identification string ended by the "\n\x2e".
1536  *
1537  * The following loop is indexed through i (instead of id)
1538  * to avoid looping forever when for any reason
1539  * the port returns '\r', '\n' or '\x2e' permanently.
1540  */
1541  for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1542  if ((curr = get_wait_data(cosa)) == -1) {
1543  return -1;
1544  }
1545  curr &= 0xff;
1546  if (curr != '\r' && curr != '\n' && curr != 0x2e)
1547  idstring[id++] = curr;
1548  if (curr == 0x2e && prev == '\n')
1549  break;
1550  }
1551  /* Perhaps we should fail when i==COSA_MAX_ID_STRING-1 ? */
1552  idstring[id] = '\0';
1553  return id;
1554 }
1555 
1556 
1557 /* ---------- Auxiliary routines for COSA/SRP monitor ---------- */
1558 
1559 /*
1560  * This routine gets the data byte from the card waiting for the SR_RX_RDY
1561  * bit to be set in a loop. It should be used in the exceptional cases
1562  * only (for example when resetting the card or downloading the firmware.
1563  */
1564 static int get_wait_data(struct cosa_data *cosa)
1565 {
1566  int retries = 1000;
1567 
1568  while (--retries) {
1569  /* read data and return them */
1570  if (cosa_getstatus(cosa) & SR_RX_RDY) {
1571  short r;
1572  r = cosa_getdata8(cosa);
1573 #if 0
1574  pr_info("get_wait_data returning after %d retries\n",
1575  999-retries);
1576 #endif
1577  return r;
1578  }
1579  /* sleep if not ready to read */
1581  }
1582  pr_info("timeout in get_wait_data (status 0x%x)\n",
1583  cosa_getstatus(cosa));
1584  return -1;
1585 }
1586 
1587 /*
1588  * This routine puts the data byte to the card waiting for the SR_TX_RDY
1589  * bit to be set in a loop. It should be used in the exceptional cases
1590  * only (for example when resetting the card or downloading the firmware).
1591  */
1592 static int put_wait_data(struct cosa_data *cosa, int data)
1593 {
1594  int retries = 1000;
1595  while (--retries) {
1596  /* read data and return them */
1597  if (cosa_getstatus(cosa) & SR_TX_RDY) {
1598  cosa_putdata8(cosa, data);
1599 #if 0
1600  pr_info("Putdata: %d retries\n", 999-retries);
1601 #endif
1602  return 0;
1603  }
1604 #if 0
1605  /* sleep if not ready to read */
1607 #endif
1608  }
1609  pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1610  cosa->num, cosa_getstatus(cosa));
1611  return -1;
1612 }
1613 
1614 /*
1615  * The following routine puts the hexadecimal number into the SRP monitor
1616  * and verifies the proper echo of the sent bytes. Returns 0 on success,
1617  * negative number on failure (-1,-3,-5,-7) means that put_wait_data() failed,
1618  * (-2,-4,-6,-8) means that reading echo failed.
1619  */
1620 static int puthexnumber(struct cosa_data *cosa, int number)
1621 {
1622  char temp[5];
1623  int i;
1624 
1625  /* Well, I should probably replace this by something faster. */
1626  sprintf(temp, "%04X", number);
1627  for (i=0; i<4; i++) {
1628  if (put_wait_data(cosa, temp[i]) == -1) {
1629  pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1630  cosa->num, i);
1631  return -1-2*i;
1632  }
1633  if (get_wait_data(cosa) != temp[i]) {
1634  pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1635  cosa->num, i);
1636  return -2-2*i;
1637  }
1638  }
1639  return 0;
1640 }
1641 
1642 
1643 /* ---------- Interrupt routines ---------- */
1644 
1645 /*
1646  * There are three types of interrupt:
1647  * At the beginning of transmit - this handled is in tx_interrupt(),
1648  * at the beginning of receive - it is in rx_interrupt() and
1649  * at the end of transmit/receive - it is the eot_interrupt() function.
1650  * These functions are multiplexed by cosa_interrupt() according to the
1651  * COSA status byte. I have moved the rx/tx/eot interrupt handling into
1652  * separate functions to make it more readable. These functions are inline,
1653  * so there should be no overhead of function call.
1654  *
1655  * In the COSA bus-master mode, we need to tell the card the address of a
1656  * buffer. Unfortunately, COSA may be too slow for us, so we must busy-wait.
1657  * It's time to use the bottom half :-(
1658  */
1659 
1660 /*
1661  * Transmit interrupt routine - called when COSA is willing to obtain
1662  * data from the OS. The most tricky part of the routine is selection
1663  * of channel we (OS) want to send packet for. For SRP we should probably
1664  * use the round-robin approach. The newer COSA firmwares have a simple
1665  * flow-control - in the status word has bits 2 and 3 set to 1 means that the
1666  * channel 0 or 1 doesn't want to receive data.
1667  *
1668  * It seems there is a bug in COSA firmware (need to trace it further):
1669  * When the driver status says that the kernel has no more data for transmit
1670  * (e.g. at the end of TX DMA) and then the kernel changes its mind
1671  * (e.g. new packet is queued to hard_start_xmit()), the card issues
1672  * the TX interrupt but does not mark the channel as ready-to-transmit.
1673  * The fix seems to be to push the packet to COSA despite its request.
1674  * We first try to obey the card's opinion, and then fall back to forced TX.
1675  */
1676 static inline void tx_interrupt(struct cosa_data *cosa, int status)
1677 {
1678  unsigned long flags, flags1;
1679 #ifdef DEBUG_IRQS
1680  pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1681 #endif
1682  spin_lock_irqsave(&cosa->lock, flags);
1683  set_bit(TXBIT, &cosa->rxtx);
1684  if (!test_bit(IRQBIT, &cosa->rxtx)) {
1685  /* flow control, see the comment above */
1686  int i=0;
1687  if (!cosa->txbitmap) {
1688  pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1689  cosa->name);
1690  put_driver_status_nolock(cosa);
1691  clear_bit(TXBIT, &cosa->rxtx);
1692  spin_unlock_irqrestore(&cosa->lock, flags);
1693  return;
1694  }
1695  while (1) {
1696  cosa->txchan++;
1697  i++;
1698  if (cosa->txchan >= cosa->nchannels)
1699  cosa->txchan = 0;
1700  if (!(cosa->txbitmap & (1<<cosa->txchan)))
1701  continue;
1702  if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1703  break;
1704  /* in second pass, accept first ready-to-TX channel */
1705  if (i > cosa->nchannels) {
1706  /* Can be safely ignored */
1707 #ifdef DEBUG_IRQS
1708  printk(KERN_DEBUG "%s: Forcing TX "
1709  "to not-ready channel %d\n",
1710  cosa->name, cosa->txchan);
1711 #endif
1712  break;
1713  }
1714  }
1715 
1716  cosa->txsize = cosa->chan[cosa->txchan].txsize;
1717  if (cosa_dma_able(cosa->chan+cosa->txchan,
1718  cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1719  cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1720  } else {
1721  memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1722  cosa->txsize);
1723  cosa->txbuf = cosa->bouncebuf;
1724  }
1725  }
1726 
1727  if (is_8bit(cosa)) {
1728  if (!test_bit(IRQBIT, &cosa->rxtx)) {
1729  cosa_putstatus(cosa, SR_TX_INT_ENA);
1730  cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1731  ((cosa->txsize >> 8) & 0x1f));
1732 #ifdef DEBUG_IO
1733  debug_status_out(cosa, SR_TX_INT_ENA);
1734  debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1735  ((cosa->txsize >> 8) & 0x1f));
1736  debug_data_in(cosa, cosa_getdata8(cosa));
1737 #else
1738  cosa_getdata8(cosa);
1739 #endif
1740  set_bit(IRQBIT, &cosa->rxtx);
1741  spin_unlock_irqrestore(&cosa->lock, flags);
1742  return;
1743  } else {
1744  clear_bit(IRQBIT, &cosa->rxtx);
1745  cosa_putstatus(cosa, 0);
1746  cosa_putdata8(cosa, cosa->txsize&0xff);
1747 #ifdef DEBUG_IO
1748  debug_status_out(cosa, 0);
1749  debug_data_out(cosa, cosa->txsize&0xff);
1750 #endif
1751  }
1752  } else {
1753  cosa_putstatus(cosa, SR_TX_INT_ENA);
1754  cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1755  | (cosa->txsize & 0x1fff));
1756 #ifdef DEBUG_IO
1757  debug_status_out(cosa, SR_TX_INT_ENA);
1758  debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1759  | (cosa->txsize & 0x1fff));
1760  debug_data_in(cosa, cosa_getdata8(cosa));
1761  debug_status_out(cosa, 0);
1762 #else
1763  cosa_getdata8(cosa);
1764 #endif
1765  cosa_putstatus(cosa, 0);
1766  }
1767 
1768  if (cosa->busmaster) {
1769  unsigned long addr = virt_to_bus(cosa->txbuf);
1770  int count=0;
1771  pr_info("busmaster IRQ\n");
1772  while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1773  count++;
1774  udelay(10);
1775  if (count > 1000) break;
1776  }
1777  pr_info("status %x\n", cosa_getstatus(cosa));
1778  pr_info("ready after %d loops\n", count);
1779  cosa_putdata16(cosa, (addr >> 16)&0xffff);
1780 
1781  count = 0;
1782  while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1783  count++;
1784  if (count > 1000) break;
1785  udelay(10);
1786  }
1787  pr_info("ready after %d loops\n", count);
1788  cosa_putdata16(cosa, addr &0xffff);
1789  flags1 = claim_dma_lock();
1791  enable_dma(cosa->dma);
1792  release_dma_lock(flags1);
1793  } else {
1794  /* start the DMA */
1795  flags1 = claim_dma_lock();
1796  disable_dma(cosa->dma);
1797  clear_dma_ff(cosa->dma);
1799  set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1800  set_dma_count(cosa->dma, cosa->txsize);
1801  enable_dma(cosa->dma);
1802  release_dma_lock(flags1);
1803  }
1804  cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1805 #ifdef DEBUG_IO
1806  debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1807 #endif
1808  spin_unlock_irqrestore(&cosa->lock, flags);
1809 }
1810 
1811 static inline void rx_interrupt(struct cosa_data *cosa, int status)
1812 {
1813  unsigned long flags;
1814 #ifdef DEBUG_IRQS
1815  pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1816 #endif
1817 
1818  spin_lock_irqsave(&cosa->lock, flags);
1819  set_bit(RXBIT, &cosa->rxtx);
1820 
1821  if (is_8bit(cosa)) {
1822  if (!test_bit(IRQBIT, &cosa->rxtx)) {
1823  set_bit(IRQBIT, &cosa->rxtx);
1824  put_driver_status_nolock(cosa);
1825  cosa->rxsize = cosa_getdata8(cosa) <<8;
1826 #ifdef DEBUG_IO
1827  debug_data_in(cosa, cosa->rxsize >> 8);
1828 #endif
1829  spin_unlock_irqrestore(&cosa->lock, flags);
1830  return;
1831  } else {
1832  clear_bit(IRQBIT, &cosa->rxtx);
1833  cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1834 #ifdef DEBUG_IO
1835  debug_data_in(cosa, cosa->rxsize & 0xff);
1836 #endif
1837 #if 0
1838  pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1839  cosa->num, cosa->rxsize);
1840 #endif
1841  }
1842  } else {
1843  cosa->rxsize = cosa_getdata16(cosa);
1844 #ifdef DEBUG_IO
1845  debug_data_in(cosa, cosa->rxsize);
1846 #endif
1847 #if 0
1848  pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1849  cosa->num, cosa->rxsize);
1850 #endif
1851  }
1852  if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1853  pr_warn("%s: rx for unknown channel (0x%04x)\n",
1854  cosa->name, cosa->rxsize);
1855  spin_unlock_irqrestore(&cosa->lock, flags);
1856  goto reject;
1857  }
1858  cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1859  cosa->rxsize &= 0x1fff;
1860  spin_unlock_irqrestore(&cosa->lock, flags);
1861 
1862  cosa->rxbuf = NULL;
1863  if (cosa->rxchan->setup_rx)
1864  cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1865 
1866  if (!cosa->rxbuf) {
1867 reject: /* Reject the packet */
1868  pr_info("cosa%d: rejecting packet on channel %d\n",
1869  cosa->num, cosa->rxchan->num);
1870  cosa->rxbuf = cosa->bouncebuf;
1871  }
1872 
1873  /* start the DMA */
1874  flags = claim_dma_lock();
1875  disable_dma(cosa->dma);
1876  clear_dma_ff(cosa->dma);
1877  set_dma_mode(cosa->dma, DMA_MODE_READ);
1878  if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1879  set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1880  } else {
1881  set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1882  }
1883  set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1884  enable_dma(cosa->dma);
1885  release_dma_lock(flags);
1886  spin_lock_irqsave(&cosa->lock, flags);
1887  cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1888  if (!is_8bit(cosa) && (status & SR_TX_RDY))
1890 #ifdef DEBUG_IO
1891  debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1892  if (!is_8bit(cosa) && (status & SR_TX_RDY))
1893  debug_data_cmd(cosa, DRIVER_RX_READY);
1894 #endif
1895  spin_unlock_irqrestore(&cosa->lock, flags);
1896 }
1897 
1898 static inline void eot_interrupt(struct cosa_data *cosa, int status)
1899 {
1900  unsigned long flags, flags1;
1901  spin_lock_irqsave(&cosa->lock, flags);
1902  flags1 = claim_dma_lock();
1903  disable_dma(cosa->dma);
1904  clear_dma_ff(cosa->dma);
1905  release_dma_lock(flags1);
1906  if (test_bit(TXBIT, &cosa->rxtx)) {
1907  struct channel_data *chan = cosa->chan+cosa->txchan;
1908  if (chan->tx_done)
1909  if (chan->tx_done(chan, cosa->txsize))
1910  clear_bit(chan->num, &cosa->txbitmap);
1911  } else if (test_bit(RXBIT, &cosa->rxtx)) {
1912 #ifdef DEBUG_DATA
1913  {
1914  int i;
1915  pr_info("cosa%dc%d: done rx(0x%x)",
1916  cosa->num, cosa->rxchan->num, cosa->rxsize);
1917  for (i=0; i<cosa->rxsize; i++)
1918  pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1919  pr_cont("\n");
1920  }
1921 #endif
1922  /* Packet for unknown channel? */
1923  if (cosa->rxbuf == cosa->bouncebuf)
1924  goto out;
1925  if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1926  memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1927  if (cosa->rxchan->rx_done)
1928  if (cosa->rxchan->rx_done(cosa->rxchan))
1929  clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1930  } else {
1931  pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1932  }
1933  /*
1934  * Clear the RXBIT, TXBIT and IRQBIT (the latest should be
1935  * cleared anyway). We should do it as soon as possible
1936  * so that we can tell the COSA we are done and to give it a time
1937  * for recovery.
1938  */
1939 out:
1940  cosa->rxtx = 0;
1941  put_driver_status_nolock(cosa);
1942  spin_unlock_irqrestore(&cosa->lock, flags);
1943 }
1944 
1945 static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1946 {
1947  unsigned status;
1948  int count = 0;
1949  struct cosa_data *cosa = cosa_;
1950 again:
1951  status = cosa_getstatus(cosa);
1952 #ifdef DEBUG_IRQS
1953  pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1954 #endif
1955 #ifdef DEBUG_IO
1956  debug_status_in(cosa, status);
1957 #endif
1958  switch (status & SR_CMD_FROM_SRP_MASK) {
1959  case SR_DOWN_REQUEST:
1960  tx_interrupt(cosa, status);
1961  break;
1962  case SR_UP_REQUEST:
1963  rx_interrupt(cosa, status);
1964  break;
1965  case SR_END_OF_TRANSFER:
1966  eot_interrupt(cosa, status);
1967  break;
1968  default:
1969  /* We may be too fast for SRP. Try to wait a bit more. */
1970  if (count++ < 100) {
1971  udelay(100);
1972  goto again;
1973  }
1974  pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1975  cosa->num, status & 0xff, count);
1976  }
1977 #ifdef DEBUG_IRQS
1978  if (count)
1979  pr_info("%s: %d-times got unknown status in IRQ\n",
1980  cosa->name, count);
1981  else
1982  pr_info("%s: returning from IRQ\n", cosa->name);
1983 #endif
1984  return IRQ_HANDLED;
1985 }
1986 
1987 
1988 /* ---------- I/O debugging routines ---------- */
1989 /*
1990  * These routines can be used to monitor COSA/SRP I/O and to printk()
1991  * the data being transferred on the data and status I/O port in a
1992  * readable way.
1993  */
1994 
1995 #ifdef DEBUG_IO
1996 static void debug_status_in(struct cosa_data *cosa, int status)
1997 {
1998  char *s;
1999  switch (status & SR_CMD_FROM_SRP_MASK) {
2000  case SR_UP_REQUEST:
2001  s = "RX_REQ";
2002  break;
2003  case SR_DOWN_REQUEST:
2004  s = "TX_REQ";
2005  break;
2006  case SR_END_OF_TRANSFER:
2007  s = "ET_REQ";
2008  break;
2009  default:
2010  s = "NO_REQ";
2011  break;
2012  }
2013  pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2014  cosa->name,
2015  status,
2016  status & SR_USR_RQ ? "USR_RQ|" : "",
2017  status & SR_TX_RDY ? "TX_RDY|" : "",
2018  status & SR_RX_RDY ? "RX_RDY|" : "",
2019  s);
2020 }
2021 
2022 static void debug_status_out(struct cosa_data *cosa, int status)
2023 {
2024  pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2025  cosa->name,
2026  status,
2027  status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2028  status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2029  status & SR_RST ? "RESET|" : "",
2030  status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2031  status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2032  status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
2033 }
2034 
2035 static void debug_data_in(struct cosa_data *cosa, int data)
2036 {
2037  pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2038 }
2039 
2040 static void debug_data_out(struct cosa_data *cosa, int data)
2041 {
2042  pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2043 }
2044 
2045 static void debug_data_cmd(struct cosa_data *cosa, int data)
2046 {
2047  pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2048  cosa->name, data,
2049  data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2050  data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2051 }
2052 #endif
2053 
2054 /* EOF -- this file has not been truncated */