1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
99
100#include "cosa.h"
101
102
103#define COSA_MAX_ID_STRING 128
104
105
106#define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
107
108
109
110struct channel_data {
111 int usage;
112 int num;
113 struct cosa_data *cosa;
114 int txsize;
115 char *txbuf;
116 char name[COSA_MAX_NAME];
117
118
119
120 char *(*setup_rx)(struct channel_data *channel, int size);
121
122 int (*rx_done)(struct channel_data *channel);
123
124 int (*tx_done)(struct channel_data *channel, int size);
125
126
127 struct mutex rlock;
128 struct semaphore wsem;
129 char *rxdata;
130 int rxsize;
131 wait_queue_head_t txwaitq, rxwaitq;
132 int tx_status, rx_status;
133
134
135 struct net_device *netdev;
136 struct sk_buff *rx_skb, *tx_skb;
137};
138
139
140#define COSA_FW_RESET (1<<0)
141#define COSA_FW_DOWNLOAD (1<<1)
142#define COSA_FW_START (1<<2)
143
144struct cosa_data {
145 int num;
146 char name[COSA_MAX_NAME];
147 unsigned int datareg, statusreg;
148 unsigned short irq, dma;
149 unsigned short startaddr;
150 unsigned short busmaster;
151 int nchannels;
152 int driver_status;
153 int firmware_status;
154 unsigned long rxbitmap, txbitmap;
155 unsigned long rxtx;
156 int enabled;
157 int usage;
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;
164 char id_string[COSA_MAX_ID_STRING];
165 char *type;
166};
167
168
169
170
171
172
173
174
175
176
177
178static DEFINE_MUTEX(cosa_chardev_mutex);
179static int cosa_major = 117;
180
181
182
183
184
185
186#define CARD_MINOR_BITS 4
187
188
189
190
191
192#define MAX_CARDS 16
193
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
199
200
201
202
203
204#define TXBIT 0
205#define RXBIT 1
206#define IRQBIT 2
207
208#define COSA_MTU 2000
209
210#undef DEBUG_DATA
211#undef DEBUG_IRQS
212#undef DEBUG_IO
213
214#define TX_TIMEOUT (5*HZ)
215
216
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
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
229static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
230
231
232static struct class *cosa_class;
233
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
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
270static int cosa_probe(int ioaddr, int irq, int dma);
271
272
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
279
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);
285static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
286static 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);
290
291
292static 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);
302static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
303 unsigned long arg);
304#ifdef COSA_FASYNC_WORKING
305static int cosa_fasync(struct inode *inode, struct file *file, int on);
306#endif
307
308static 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
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
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
334
335static 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
342static irqreturn_t cosa_interrupt(int irq, void *cosa);
343
344
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
353static 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
359
360static 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
398out_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;
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
416 for (i = 0; i < cosa->nchannels; i++) {
417
418 unregister_hdlc_device(cosa->chan[i].netdev);
419 free_netdev(cosa->chan[i].netdev);
420 }
421
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}
430module_exit(cosa_exit);
431
432static 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
441static 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
449
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
455
456 if (base < 0x100 || base > 0x3ff || base & 0x7) {
457 pr_info("invalid I/O address 0x%x\n", base);
458 return -1;
459 }
460
461 if (dma < 0 || dma == 4 || dma > 7) {
462 pr_info("invalid DMA %d\n", dma);
463 return -1;
464 }
465
466
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
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
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
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
508 if (irq < 0) {
509 unsigned long irqs;
510
511 irqs = probe_irq_on();
512
513
514
515
516
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
523 cosa_putstatus(cosa, 0);
524
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
537 }
538 }
539
540 cosa->irq = irq;
541 cosa->num = nr_cards;
542 cosa->usage = 0;
543 cosa->nchannels = 2;
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
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
576 mutex_init(&chan->rlock);
577 sema_init(&chan->wsem, 1);
578
579
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
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);
611err_out3:
612 kfree(cosa->bouncebuf);
613err_out2:
614 free_dma(cosa->dma);
615err_out1:
616 free_irq(cosa->irq, cosa);
617err_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
625
626static 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
634static 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
673static 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
685static 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
704static 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
727static char *cosa_net_setup_rx(struct channel_data *chan, int size)
728{
729
730
731
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
744static 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
763static 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
780
781static 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)) {
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 chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL);
799 if (chan->rxdata == NULL) {
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
837static char *chrdev_setup_rx(struct channel_data *chan, int size)
838{
839
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) {
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)) {
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
877 kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA);
878 if (kbuf == NULL) {
879 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);
892 while (!chan->tx_status) {
893 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);
903 up(&chan->wsem);
904 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) {
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{
928 pr_info("cosa_poll is here\n");
929 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;
938 int ret = 0;
939
940 mutex_lock(&cosa_chardev_mutex);
941 if ((n=iminor(file_inode(file))>>CARD_MINOR_BITS)
942 >= nr_cards) {
943 ret = -ENODEV;
944 goto out;
945 }
946 cosa = cosa_cards+n;
947
948 if ((n=iminor(file_inode(file))
949 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
950 ret = -ENODEV;
951 goto out;
952 }
953 chan = cosa->chan + n;
954
955 file->private_data = chan;
956
957 spin_lock_irqsave(&cosa->lock, flags);
958
959 if (chan->usage < 0) {
960 spin_unlock_irqrestore(&cosa->lock, flags);
961 ret = -EBUSY;
962 goto out;
963 }
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);
971out:
972 mutex_unlock(&cosa_chardev_mutex);
973 return ret;
974}
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
994static int cosa_fasync(struct inode *inode, struct file *file, int on)
995{
996 int port = iminor(inode);
997
998 return fasync_helper(inode, file, on, &fasync[port]);
999}
1000#endif
1001
1002
1003
1004
1005
1006
1007
1008
1009static inline int cosa_reset(struct cosa_data *cosa)
1010{
1011 char idstring[COSA_MAX_ID_STRING];
1012 if (cosa->usage > 1)
1013 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1014 cosa->num, cosa->usage);
1015 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1016 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1017 pr_notice("cosa%d: reset failed\n", cosa->num);
1018 return -EIO;
1019 }
1020 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1021 cosa->firmware_status |= COSA_FW_RESET;
1022 return 0;
1023}
1024
1025
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)
1032 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1033 cosa->name, cosa->usage);
1034 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1035 pr_notice("%s: reset the card first (status %d)\n",
1036 cosa->name, cosa->firmware_status);
1037 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
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) {
1054 pr_notice("cosa%d: microcode download failed: %d\n",
1055 cosa->num, i);
1056 return -EIO;
1057 }
1058 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1059 cosa->num, d.len, d.addr);
1060 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1061 return 0;
1062}
1063
1064
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)
1071 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1072 cosa->num, cosa->usage);
1073 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1074 pr_notice("%s: reset the card first (status %d)\n",
1075 cosa->name, cosa->firmware_status);
1076 return -EPERM;
1077 }
1078
1079 if (copy_from_user(&d, arg, sizeof(d)))
1080 return -EFAULT;
1081
1082
1083 cosa->firmware_status &= ~COSA_FW_RESET;
1084
1085 i = readmem(cosa, d.code, d.len, d.addr);
1086 if (i < 0) {
1087 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1088 return -EIO;
1089 }
1090 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1091 cosa->num, d.len, d.addr);
1092 cosa->firmware_status |= COSA_FW_RESET;
1093 return 0;
1094}
1095
1096
1097static inline int cosa_start(struct cosa_data *cosa, int address)
1098{
1099 int i;
1100
1101 if (cosa->usage > 1)
1102 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1103 cosa->num, cosa->usage);
1104
1105 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1106 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1107 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1108 cosa->name, cosa->firmware_status);
1109 return -EPERM;
1110 }
1111 cosa->firmware_status &= ~COSA_FW_RESET;
1112 if ((i=startmicrocode(cosa, address)) < 0) {
1113 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1114 cosa->num, address, i);
1115 return -EIO;
1116 }
1117 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1118 cosa->startaddr = address;
1119 cosa->firmware_status |= COSA_FW_START;
1120 return 0;
1121}
1122
1123
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
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;
1145 switch (cmd) {
1146 case COSAIORSET:
1147 if (!capable(CAP_NET_ADMIN))
1148 return -EACCES;
1149 return cosa_reset(cosa);
1150 case COSAIOSTRT:
1151 if (!capable(CAP_SYS_RAWIO))
1152 return -EACCES;
1153 return cosa_start(cosa, arg);
1154 case COSAIODOWNLD:
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
1186static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1187{
1188 int rv;
1189 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);
1195}
1196
1197static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1198 unsigned long arg)
1199{
1200 struct channel_data *channel = file->private_data;
1201 struct cosa_data *cosa;
1202 long ret;
1203
1204 mutex_lock(&cosa_chardev_mutex);
1205 cosa = channel->cosa;
1206 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1207 mutex_unlock(&cosa_chardev_mutex);
1208 return ret;
1209}
1210
1211
1212
1213
1214
1215
1216
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
1236
1237
1238
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
1247 pr_info("cosa%dc%d: starting tx(0x%x)",
1248 chan->cosa->num, chan->num, len);
1249 for (i=0; i<len; i++)
1250 pr_cont(" %02x", buf[i]&0xff);
1251 pr_cont("\n");
1252#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
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
1332
1333
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
1345 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1346 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
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
1368
1369
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)
1379 pr_info("%s: packet spanning a 64k boundary\n",
1380 chan->name);
1381 return 0;
1382 }
1383 return 1;
1384}
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
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
1439
1440
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
1464
1465
1466
1467
1468
1469
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) {
1490 pr_info("0x%04x bytes remaining\n", length);
1491 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
1514
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
1521 cosa_putstatus(cosa, 0);
1522 cosa_getdata8(cosa);
1523 cosa_putstatus(cosa, SR_RST);
1524 msleep(500);
1525
1526 cosa_putstatus(cosa, 0);
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1537 if ((curr = get_wait_data(cosa)) == -1) {
1538 return -1;
1539 }
1540 curr &= 0xff;
1541 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1542 idstring[id++] = curr;
1543 if (curr == 0x2e && prev == '\n')
1544 break;
1545 }
1546
1547 idstring[id] = '\0';
1548 return id;
1549}
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559static int get_wait_data(struct cosa_data *cosa)
1560{
1561 int retries = 1000;
1562
1563 while (--retries) {
1564
1565 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1566 short r;
1567 r = cosa_getdata8(cosa);
1568#if 0
1569 pr_info("get_wait_data returning after %d retries\n",
1570 999-retries);
1571#endif
1572 return r;
1573 }
1574
1575 schedule_timeout_interruptible(1);
1576 }
1577 pr_info("timeout in get_wait_data (status 0x%x)\n",
1578 cosa_getstatus(cosa));
1579 return -1;
1580}
1581
1582
1583
1584
1585
1586
1587static int put_wait_data(struct cosa_data *cosa, int data)
1588{
1589 int retries = 1000;
1590 while (--retries) {
1591
1592 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1593 cosa_putdata8(cosa, data);
1594#if 0
1595 pr_info("Putdata: %d retries\n", 999-retries);
1596#endif
1597 return 0;
1598 }
1599#if 0
1600
1601 schedule_timeout_interruptible(1);
1602#endif
1603 }
1604 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1605 cosa->num, cosa_getstatus(cosa));
1606 return -1;
1607}
1608
1609
1610
1611
1612
1613
1614
1615static int puthexnumber(struct cosa_data *cosa, int number)
1616{
1617 char temp[5];
1618 int i;
1619
1620
1621 sprintf(temp, "%04X", number);
1622 for (i=0; i<4; i++) {
1623 if (put_wait_data(cosa, temp[i]) == -1) {
1624 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1625 cosa->num, i);
1626 return -1-2*i;
1627 }
1628 if (get_wait_data(cosa) != temp[i]) {
1629 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1630 cosa->num, i);
1631 return -2-2*i;
1632 }
1633 }
1634 return 0;
1635}
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671static inline void tx_interrupt(struct cosa_data *cosa, int status)
1672{
1673 unsigned long flags, flags1;
1674#ifdef DEBUG_IRQS
1675 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1676#endif
1677 spin_lock_irqsave(&cosa->lock, flags);
1678 set_bit(TXBIT, &cosa->rxtx);
1679 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1680
1681 int i=0;
1682 if (!cosa->txbitmap) {
1683 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1684 cosa->name);
1685 put_driver_status_nolock(cosa);
1686 clear_bit(TXBIT, &cosa->rxtx);
1687 spin_unlock_irqrestore(&cosa->lock, flags);
1688 return;
1689 }
1690 while (1) {
1691 cosa->txchan++;
1692 i++;
1693 if (cosa->txchan >= cosa->nchannels)
1694 cosa->txchan = 0;
1695 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1696 continue;
1697 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1698 break;
1699
1700 if (i > cosa->nchannels) {
1701
1702#ifdef DEBUG_IRQS
1703 printk(KERN_DEBUG "%s: Forcing TX "
1704 "to not-ready channel %d\n",
1705 cosa->name, cosa->txchan);
1706#endif
1707 break;
1708 }
1709 }
1710
1711 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1712 if (cosa_dma_able(cosa->chan+cosa->txchan,
1713 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1714 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1715 } else {
1716 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1717 cosa->txsize);
1718 cosa->txbuf = cosa->bouncebuf;
1719 }
1720 }
1721
1722 if (is_8bit(cosa)) {
1723 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1724 cosa_putstatus(cosa, SR_TX_INT_ENA);
1725 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1726 ((cosa->txsize >> 8) & 0x1f));
1727#ifdef DEBUG_IO
1728 debug_status_out(cosa, SR_TX_INT_ENA);
1729 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1730 ((cosa->txsize >> 8) & 0x1f));
1731 debug_data_in(cosa, cosa_getdata8(cosa));
1732#else
1733 cosa_getdata8(cosa);
1734#endif
1735 set_bit(IRQBIT, &cosa->rxtx);
1736 spin_unlock_irqrestore(&cosa->lock, flags);
1737 return;
1738 } else {
1739 clear_bit(IRQBIT, &cosa->rxtx);
1740 cosa_putstatus(cosa, 0);
1741 cosa_putdata8(cosa, cosa->txsize&0xff);
1742#ifdef DEBUG_IO
1743 debug_status_out(cosa, 0);
1744 debug_data_out(cosa, cosa->txsize&0xff);
1745#endif
1746 }
1747 } else {
1748 cosa_putstatus(cosa, SR_TX_INT_ENA);
1749 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1750 | (cosa->txsize & 0x1fff));
1751#ifdef DEBUG_IO
1752 debug_status_out(cosa, SR_TX_INT_ENA);
1753 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1754 | (cosa->txsize & 0x1fff));
1755 debug_data_in(cosa, cosa_getdata8(cosa));
1756 debug_status_out(cosa, 0);
1757#else
1758 cosa_getdata8(cosa);
1759#endif
1760 cosa_putstatus(cosa, 0);
1761 }
1762
1763 if (cosa->busmaster) {
1764 unsigned long addr = virt_to_bus(cosa->txbuf);
1765 int count=0;
1766 pr_info("busmaster IRQ\n");
1767 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1768 count++;
1769 udelay(10);
1770 if (count > 1000) break;
1771 }
1772 pr_info("status %x\n", cosa_getstatus(cosa));
1773 pr_info("ready after %d loops\n", count);
1774 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1775
1776 count = 0;
1777 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1778 count++;
1779 if (count > 1000) break;
1780 udelay(10);
1781 }
1782 pr_info("ready after %d loops\n", count);
1783 cosa_putdata16(cosa, addr &0xffff);
1784 flags1 = claim_dma_lock();
1785 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1786 enable_dma(cosa->dma);
1787 release_dma_lock(flags1);
1788 } else {
1789
1790 flags1 = claim_dma_lock();
1791 disable_dma(cosa->dma);
1792 clear_dma_ff(cosa->dma);
1793 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1794 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1795 set_dma_count(cosa->dma, cosa->txsize);
1796 enable_dma(cosa->dma);
1797 release_dma_lock(flags1);
1798 }
1799 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1800#ifdef DEBUG_IO
1801 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1802#endif
1803 spin_unlock_irqrestore(&cosa->lock, flags);
1804}
1805
1806static inline void rx_interrupt(struct cosa_data *cosa, int status)
1807{
1808 unsigned long flags;
1809#ifdef DEBUG_IRQS
1810 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1811#endif
1812
1813 spin_lock_irqsave(&cosa->lock, flags);
1814 set_bit(RXBIT, &cosa->rxtx);
1815
1816 if (is_8bit(cosa)) {
1817 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1818 set_bit(IRQBIT, &cosa->rxtx);
1819 put_driver_status_nolock(cosa);
1820 cosa->rxsize = cosa_getdata8(cosa) <<8;
1821#ifdef DEBUG_IO
1822 debug_data_in(cosa, cosa->rxsize >> 8);
1823#endif
1824 spin_unlock_irqrestore(&cosa->lock, flags);
1825 return;
1826 } else {
1827 clear_bit(IRQBIT, &cosa->rxtx);
1828 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1829#ifdef DEBUG_IO
1830 debug_data_in(cosa, cosa->rxsize & 0xff);
1831#endif
1832#if 0
1833 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1834 cosa->num, cosa->rxsize);
1835#endif
1836 }
1837 } else {
1838 cosa->rxsize = cosa_getdata16(cosa);
1839#ifdef DEBUG_IO
1840 debug_data_in(cosa, cosa->rxsize);
1841#endif
1842#if 0
1843 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1844 cosa->num, cosa->rxsize);
1845#endif
1846 }
1847 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1848 pr_warn("%s: rx for unknown channel (0x%04x)\n",
1849 cosa->name, cosa->rxsize);
1850 spin_unlock_irqrestore(&cosa->lock, flags);
1851 goto reject;
1852 }
1853 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1854 cosa->rxsize &= 0x1fff;
1855 spin_unlock_irqrestore(&cosa->lock, flags);
1856
1857 cosa->rxbuf = NULL;
1858 if (cosa->rxchan->setup_rx)
1859 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1860
1861 if (!cosa->rxbuf) {
1862reject:
1863 pr_info("cosa%d: rejecting packet on channel %d\n",
1864 cosa->num, cosa->rxchan->num);
1865 cosa->rxbuf = cosa->bouncebuf;
1866 }
1867
1868
1869 flags = claim_dma_lock();
1870 disable_dma(cosa->dma);
1871 clear_dma_ff(cosa->dma);
1872 set_dma_mode(cosa->dma, DMA_MODE_READ);
1873 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1874 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1875 } else {
1876 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1877 }
1878 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1879 enable_dma(cosa->dma);
1880 release_dma_lock(flags);
1881 spin_lock_irqsave(&cosa->lock, flags);
1882 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1883 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1884 cosa_putdata8(cosa, DRIVER_RX_READY);
1885#ifdef DEBUG_IO
1886 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1887 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1888 debug_data_cmd(cosa, DRIVER_RX_READY);
1889#endif
1890 spin_unlock_irqrestore(&cosa->lock, flags);
1891}
1892
1893static inline void eot_interrupt(struct cosa_data *cosa, int status)
1894{
1895 unsigned long flags, flags1;
1896 spin_lock_irqsave(&cosa->lock, flags);
1897 flags1 = claim_dma_lock();
1898 disable_dma(cosa->dma);
1899 clear_dma_ff(cosa->dma);
1900 release_dma_lock(flags1);
1901 if (test_bit(TXBIT, &cosa->rxtx)) {
1902 struct channel_data *chan = cosa->chan+cosa->txchan;
1903 if (chan->tx_done)
1904 if (chan->tx_done(chan, cosa->txsize))
1905 clear_bit(chan->num, &cosa->txbitmap);
1906 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1907#ifdef DEBUG_DATA
1908 {
1909 int i;
1910 pr_info("cosa%dc%d: done rx(0x%x)",
1911 cosa->num, cosa->rxchan->num, cosa->rxsize);
1912 for (i=0; i<cosa->rxsize; i++)
1913 pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1914 pr_cont("\n");
1915 }
1916#endif
1917
1918 if (cosa->rxbuf == cosa->bouncebuf)
1919 goto out;
1920 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1921 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1922 if (cosa->rxchan->rx_done)
1923 if (cosa->rxchan->rx_done(cosa->rxchan))
1924 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1925 } else {
1926 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1927 }
1928
1929
1930
1931
1932
1933
1934out:
1935 cosa->rxtx = 0;
1936 put_driver_status_nolock(cosa);
1937 spin_unlock_irqrestore(&cosa->lock, flags);
1938}
1939
1940static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1941{
1942 unsigned status;
1943 int count = 0;
1944 struct cosa_data *cosa = cosa_;
1945again:
1946 status = cosa_getstatus(cosa);
1947#ifdef DEBUG_IRQS
1948 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1949#endif
1950#ifdef DEBUG_IO
1951 debug_status_in(cosa, status);
1952#endif
1953 switch (status & SR_CMD_FROM_SRP_MASK) {
1954 case SR_DOWN_REQUEST:
1955 tx_interrupt(cosa, status);
1956 break;
1957 case SR_UP_REQUEST:
1958 rx_interrupt(cosa, status);
1959 break;
1960 case SR_END_OF_TRANSFER:
1961 eot_interrupt(cosa, status);
1962 break;
1963 default:
1964
1965 if (count++ < 100) {
1966 udelay(100);
1967 goto again;
1968 }
1969 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1970 cosa->num, status & 0xff, count);
1971 }
1972#ifdef DEBUG_IRQS
1973 if (count)
1974 pr_info("%s: %d-times got unknown status in IRQ\n",
1975 cosa->name, count);
1976 else
1977 pr_info("%s: returning from IRQ\n", cosa->name);
1978#endif
1979 return IRQ_HANDLED;
1980}
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990#ifdef DEBUG_IO
1991static void debug_status_in(struct cosa_data *cosa, int status)
1992{
1993 char *s;
1994 switch (status & SR_CMD_FROM_SRP_MASK) {
1995 case SR_UP_REQUEST:
1996 s = "RX_REQ";
1997 break;
1998 case SR_DOWN_REQUEST:
1999 s = "TX_REQ";
2000 break;
2001 case SR_END_OF_TRANSFER:
2002 s = "ET_REQ";
2003 break;
2004 default:
2005 s = "NO_REQ";
2006 break;
2007 }
2008 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2009 cosa->name,
2010 status,
2011 status & SR_USR_RQ ? "USR_RQ|" : "",
2012 status & SR_TX_RDY ? "TX_RDY|" : "",
2013 status & SR_RX_RDY ? "RX_RDY|" : "",
2014 s);
2015}
2016
2017static void debug_status_out(struct cosa_data *cosa, int status)
2018{
2019 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2020 cosa->name,
2021 status,
2022 status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2023 status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2024 status & SR_RST ? "RESET|" : "",
2025 status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2026 status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2027 status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
2028}
2029
2030static void debug_data_in(struct cosa_data *cosa, int data)
2031{
2032 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2033}
2034
2035static void debug_data_out(struct cosa_data *cosa, int data)
2036{
2037 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2038}
2039
2040static void debug_data_cmd(struct cosa_data *cosa, int data)
2041{
2042 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2043 cosa->name, data,
2044 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2045 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2046}
2047#endif
2048
2049
2050