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(msecs_to_jiffies(300));
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 err = -ENOMEM;
583 goto err_hdlcdev;
584 }
585 dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
586 dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
587 chan->netdev->netdev_ops = &cosa_ops;
588 chan->netdev->watchdog_timeo = TX_TIMEOUT;
589 chan->netdev->base_addr = chan->cosa->datareg;
590 chan->netdev->irq = chan->cosa->irq;
591 chan->netdev->dma = chan->cosa->dma;
592 err = register_hdlc_device(chan->netdev);
593 if (err) {
594 netdev_warn(chan->netdev,
595 "register_hdlc_device() failed\n");
596 free_netdev(chan->netdev);
597 goto err_hdlcdev;
598 }
599 }
600
601 pr_info("cosa%d: %s (%s at 0x%x irq %d dma %d), %d channels\n",
602 cosa->num, cosa->id_string, cosa->type,
603 cosa->datareg, cosa->irq, cosa->dma, cosa->nchannels);
604
605 return nr_cards++;
606
607err_hdlcdev:
608 while (i-- > 0) {
609 unregister_hdlc_device(cosa->chan[i].netdev);
610 free_netdev(cosa->chan[i].netdev);
611 }
612 kfree(cosa->chan);
613err_out3:
614 kfree(cosa->bouncebuf);
615err_out2:
616 free_dma(cosa->dma);
617err_out1:
618 free_irq(cosa->irq, cosa);
619err_out:
620 release_region(cosa->datareg,is_8bit(cosa)?2:4);
621 pr_notice("cosa%d: allocating resources failed\n", cosa->num);
622 return err;
623}
624
625
626
627
628static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
629 unsigned short parity)
630{
631 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
632 return 0;
633 return -EINVAL;
634}
635
636static int cosa_net_open(struct net_device *dev)
637{
638 struct channel_data *chan = dev_to_chan(dev);
639 int err;
640 unsigned long flags;
641
642 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
643 pr_notice("%s: start the firmware first (status %d)\n",
644 chan->cosa->name, chan->cosa->firmware_status);
645 return -EPERM;
646 }
647 spin_lock_irqsave(&chan->cosa->lock, flags);
648 if (chan->usage != 0) {
649 pr_warn("%s: cosa_net_open called with usage count %d\n",
650 chan->name, chan->usage);
651 spin_unlock_irqrestore(&chan->cosa->lock, flags);
652 return -EBUSY;
653 }
654 chan->setup_rx = cosa_net_setup_rx;
655 chan->tx_done = cosa_net_tx_done;
656 chan->rx_done = cosa_net_rx_done;
657 chan->usage = -1;
658 chan->cosa->usage++;
659 spin_unlock_irqrestore(&chan->cosa->lock, flags);
660
661 err = hdlc_open(dev);
662 if (err) {
663 spin_lock_irqsave(&chan->cosa->lock, flags);
664 chan->usage = 0;
665 chan->cosa->usage--;
666 spin_unlock_irqrestore(&chan->cosa->lock, flags);
667 return err;
668 }
669
670 netif_start_queue(dev);
671 cosa_enable_rx(chan);
672 return 0;
673}
674
675static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
676 struct net_device *dev)
677{
678 struct channel_data *chan = dev_to_chan(dev);
679
680 netif_stop_queue(dev);
681
682 chan->tx_skb = skb;
683 cosa_start_tx(chan, skb->data, skb->len);
684 return NETDEV_TX_OK;
685}
686
687static void cosa_net_timeout(struct net_device *dev)
688{
689 struct channel_data *chan = dev_to_chan(dev);
690
691 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
692 chan->netdev->stats.rx_errors++;
693 chan->netdev->stats.rx_missed_errors++;
694 } else {
695 chan->netdev->stats.tx_errors++;
696 chan->netdev->stats.tx_aborted_errors++;
697 }
698 cosa_kick(chan->cosa);
699 if (chan->tx_skb) {
700 dev_kfree_skb(chan->tx_skb);
701 chan->tx_skb = NULL;
702 }
703 netif_wake_queue(dev);
704}
705
706static int cosa_net_close(struct net_device *dev)
707{
708 struct channel_data *chan = dev_to_chan(dev);
709 unsigned long flags;
710
711 netif_stop_queue(dev);
712 hdlc_close(dev);
713 cosa_disable_rx(chan);
714 spin_lock_irqsave(&chan->cosa->lock, flags);
715 if (chan->rx_skb) {
716 kfree_skb(chan->rx_skb);
717 chan->rx_skb = NULL;
718 }
719 if (chan->tx_skb) {
720 kfree_skb(chan->tx_skb);
721 chan->tx_skb = NULL;
722 }
723 chan->usage = 0;
724 chan->cosa->usage--;
725 spin_unlock_irqrestore(&chan->cosa->lock, flags);
726 return 0;
727}
728
729static char *cosa_net_setup_rx(struct channel_data *chan, int size)
730{
731
732
733
734
735 kfree_skb(chan->rx_skb);
736 chan->rx_skb = dev_alloc_skb(size);
737 if (chan->rx_skb == NULL) {
738 pr_notice("%s: Memory squeeze, dropping packet\n", chan->name);
739 chan->netdev->stats.rx_dropped++;
740 return NULL;
741 }
742 chan->netdev->trans_start = jiffies;
743 return skb_put(chan->rx_skb, size);
744}
745
746static int cosa_net_rx_done(struct channel_data *chan)
747{
748 if (!chan->rx_skb) {
749 pr_warn("%s: rx_done with empty skb!\n", chan->name);
750 chan->netdev->stats.rx_errors++;
751 chan->netdev->stats.rx_frame_errors++;
752 return 0;
753 }
754 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
755 chan->rx_skb->dev = chan->netdev;
756 skb_reset_mac_header(chan->rx_skb);
757 chan->netdev->stats.rx_packets++;
758 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
759 netif_rx(chan->rx_skb);
760 chan->rx_skb = NULL;
761 return 0;
762}
763
764
765static int cosa_net_tx_done(struct channel_data *chan, int size)
766{
767 if (!chan->tx_skb) {
768 pr_warn("%s: tx_done with empty skb!\n", chan->name);
769 chan->netdev->stats.tx_errors++;
770 chan->netdev->stats.tx_aborted_errors++;
771 return 1;
772 }
773 dev_kfree_skb_irq(chan->tx_skb);
774 chan->tx_skb = NULL;
775 chan->netdev->stats.tx_packets++;
776 chan->netdev->stats.tx_bytes += size;
777 netif_wake_queue(chan->netdev);
778 return 1;
779}
780
781
782
783static ssize_t cosa_read(struct file *file,
784 char __user *buf, size_t count, loff_t *ppos)
785{
786 DECLARE_WAITQUEUE(wait, current);
787 unsigned long flags;
788 struct channel_data *chan = file->private_data;
789 struct cosa_data *cosa = chan->cosa;
790 char *kbuf;
791
792 if (!(cosa->firmware_status & COSA_FW_START)) {
793 pr_notice("%s: start the firmware first (status %d)\n",
794 cosa->name, cosa->firmware_status);
795 return -EPERM;
796 }
797 if (mutex_lock_interruptible(&chan->rlock))
798 return -ERESTARTSYS;
799
800 chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL);
801 if (chan->rxdata == NULL) {
802 mutex_unlock(&chan->rlock);
803 return -ENOMEM;
804 }
805
806 chan->rx_status = 0;
807 cosa_enable_rx(chan);
808 spin_lock_irqsave(&cosa->lock, flags);
809 add_wait_queue(&chan->rxwaitq, &wait);
810 while (!chan->rx_status) {
811 set_current_state(TASK_INTERRUPTIBLE);
812 spin_unlock_irqrestore(&cosa->lock, flags);
813 schedule();
814 spin_lock_irqsave(&cosa->lock, flags);
815 if (signal_pending(current) && chan->rx_status == 0) {
816 chan->rx_status = 1;
817 remove_wait_queue(&chan->rxwaitq, &wait);
818 __set_current_state(TASK_RUNNING);
819 spin_unlock_irqrestore(&cosa->lock, flags);
820 mutex_unlock(&chan->rlock);
821 return -ERESTARTSYS;
822 }
823 }
824 remove_wait_queue(&chan->rxwaitq, &wait);
825 __set_current_state(TASK_RUNNING);
826 kbuf = chan->rxdata;
827 count = chan->rxsize;
828 spin_unlock_irqrestore(&cosa->lock, flags);
829 mutex_unlock(&chan->rlock);
830
831 if (copy_to_user(buf, kbuf, count)) {
832 kfree(kbuf);
833 return -EFAULT;
834 }
835 kfree(kbuf);
836 return count;
837}
838
839static char *chrdev_setup_rx(struct channel_data *chan, int size)
840{
841
842 chan->rxsize = size;
843 return chan->rxdata;
844}
845
846static int chrdev_rx_done(struct channel_data *chan)
847{
848 if (chan->rx_status) {
849 kfree(chan->rxdata);
850 up(&chan->wsem);
851 }
852 chan->rx_status = 1;
853 wake_up_interruptible(&chan->rxwaitq);
854 return 1;
855}
856
857
858static ssize_t cosa_write(struct file *file,
859 const char __user *buf, size_t count, loff_t *ppos)
860{
861 DECLARE_WAITQUEUE(wait, current);
862 struct channel_data *chan = file->private_data;
863 struct cosa_data *cosa = chan->cosa;
864 unsigned long flags;
865 char *kbuf;
866
867 if (!(cosa->firmware_status & COSA_FW_START)) {
868 pr_notice("%s: start the firmware first (status %d)\n",
869 cosa->name, cosa->firmware_status);
870 return -EPERM;
871 }
872 if (down_interruptible(&chan->wsem))
873 return -ERESTARTSYS;
874
875 if (count > COSA_MTU)
876 count = COSA_MTU;
877
878
879 kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA);
880 if (kbuf == NULL) {
881 up(&chan->wsem);
882 return -ENOMEM;
883 }
884 if (copy_from_user(kbuf, buf, count)) {
885 up(&chan->wsem);
886 kfree(kbuf);
887 return -EFAULT;
888 }
889 chan->tx_status=0;
890 cosa_start_tx(chan, kbuf, count);
891
892 spin_lock_irqsave(&cosa->lock, flags);
893 add_wait_queue(&chan->txwaitq, &wait);
894 while (!chan->tx_status) {
895 set_current_state(TASK_INTERRUPTIBLE);
896 spin_unlock_irqrestore(&cosa->lock, flags);
897 schedule();
898 spin_lock_irqsave(&cosa->lock, flags);
899 if (signal_pending(current) && chan->tx_status == 0) {
900 chan->tx_status = 1;
901 remove_wait_queue(&chan->txwaitq, &wait);
902 __set_current_state(TASK_RUNNING);
903 chan->tx_status = 1;
904 spin_unlock_irqrestore(&cosa->lock, flags);
905 up(&chan->wsem);
906 return -ERESTARTSYS;
907 }
908 }
909 remove_wait_queue(&chan->txwaitq, &wait);
910 __set_current_state(TASK_RUNNING);
911 up(&chan->wsem);
912 spin_unlock_irqrestore(&cosa->lock, flags);
913 kfree(kbuf);
914 return count;
915}
916
917static int chrdev_tx_done(struct channel_data *chan, int size)
918{
919 if (chan->tx_status) {
920 kfree(chan->txbuf);
921 up(&chan->wsem);
922 }
923 chan->tx_status = 1;
924 wake_up_interruptible(&chan->txwaitq);
925 return 1;
926}
927
928static unsigned int cosa_poll(struct file *file, poll_table *poll)
929{
930 pr_info("cosa_poll is here\n");
931 return 0;
932}
933
934static int cosa_open(struct inode *inode, struct file *file)
935{
936 struct cosa_data *cosa;
937 struct channel_data *chan;
938 unsigned long flags;
939 int n;
940 int ret = 0;
941
942 mutex_lock(&cosa_chardev_mutex);
943 if ((n=iminor(file_inode(file))>>CARD_MINOR_BITS)
944 >= nr_cards) {
945 ret = -ENODEV;
946 goto out;
947 }
948 cosa = cosa_cards+n;
949
950 if ((n=iminor(file_inode(file))
951 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
952 ret = -ENODEV;
953 goto out;
954 }
955 chan = cosa->chan + n;
956
957 file->private_data = chan;
958
959 spin_lock_irqsave(&cosa->lock, flags);
960
961 if (chan->usage < 0) {
962 spin_unlock_irqrestore(&cosa->lock, flags);
963 ret = -EBUSY;
964 goto out;
965 }
966 cosa->usage++;
967 chan->usage++;
968
969 chan->tx_done = chrdev_tx_done;
970 chan->setup_rx = chrdev_setup_rx;
971 chan->rx_done = chrdev_rx_done;
972 spin_unlock_irqrestore(&cosa->lock, flags);
973out:
974 mutex_unlock(&cosa_chardev_mutex);
975 return ret;
976}
977
978static int cosa_release(struct inode *inode, struct file *file)
979{
980 struct channel_data *channel = file->private_data;
981 struct cosa_data *cosa;
982 unsigned long flags;
983
984 cosa = channel->cosa;
985 spin_lock_irqsave(&cosa->lock, flags);
986 cosa->usage--;
987 channel->usage--;
988 spin_unlock_irqrestore(&cosa->lock, flags);
989 return 0;
990}
991
992#ifdef COSA_FASYNC_WORKING
993static struct fasync_struct *fasync[256] = { NULL, };
994
995
996static int cosa_fasync(struct inode *inode, struct file *file, int on)
997{
998 int port = iminor(inode);
999
1000 return fasync_helper(inode, file, on, &fasync[port]);
1001}
1002#endif
1003
1004
1005
1006
1007
1008
1009
1010
1011static inline int cosa_reset(struct cosa_data *cosa)
1012{
1013 char idstring[COSA_MAX_ID_STRING];
1014 if (cosa->usage > 1)
1015 pr_info("cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1016 cosa->num, cosa->usage);
1017 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1018 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1019 pr_notice("cosa%d: reset failed\n", cosa->num);
1020 return -EIO;
1021 }
1022 pr_info("cosa%d: resetting device: %s\n", cosa->num, idstring);
1023 cosa->firmware_status |= COSA_FW_RESET;
1024 return 0;
1025}
1026
1027
1028static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1029{
1030 struct cosa_download d;
1031 int i;
1032
1033 if (cosa->usage > 1)
1034 pr_info("%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1035 cosa->name, cosa->usage);
1036 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1037 pr_notice("%s: reset the card first (status %d)\n",
1038 cosa->name, cosa->firmware_status);
1039 return -EPERM;
1040 }
1041
1042 if (copy_from_user(&d, arg, sizeof(d)))
1043 return -EFAULT;
1044
1045 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1046 return -EINVAL;
1047 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1048 return -EINVAL;
1049
1050
1051
1052 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1053
1054 i = download(cosa, d.code, d.len, d.addr);
1055 if (i < 0) {
1056 pr_notice("cosa%d: microcode download failed: %d\n",
1057 cosa->num, i);
1058 return -EIO;
1059 }
1060 pr_info("cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1061 cosa->num, d.len, d.addr);
1062 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1063 return 0;
1064}
1065
1066
1067static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1068{
1069 struct cosa_download d;
1070 int i;
1071
1072 if (cosa->usage > 1)
1073 pr_info("cosa%d: WARNING: readmem requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1074 cosa->num, cosa->usage);
1075 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1076 pr_notice("%s: reset the card first (status %d)\n",
1077 cosa->name, cosa->firmware_status);
1078 return -EPERM;
1079 }
1080
1081 if (copy_from_user(&d, arg, sizeof(d)))
1082 return -EFAULT;
1083
1084
1085 cosa->firmware_status &= ~COSA_FW_RESET;
1086
1087 i = readmem(cosa, d.code, d.len, d.addr);
1088 if (i < 0) {
1089 pr_notice("cosa%d: reading memory failed: %d\n", cosa->num, i);
1090 return -EIO;
1091 }
1092 pr_info("cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1093 cosa->num, d.len, d.addr);
1094 cosa->firmware_status |= COSA_FW_RESET;
1095 return 0;
1096}
1097
1098
1099static inline int cosa_start(struct cosa_data *cosa, int address)
1100{
1101 int i;
1102
1103 if (cosa->usage > 1)
1104 pr_info("cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1105 cosa->num, cosa->usage);
1106
1107 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1108 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1109 pr_notice("%s: download the microcode and/or reset the card first (status %d)\n",
1110 cosa->name, cosa->firmware_status);
1111 return -EPERM;
1112 }
1113 cosa->firmware_status &= ~COSA_FW_RESET;
1114 if ((i=startmicrocode(cosa, address)) < 0) {
1115 pr_notice("cosa%d: start microcode at 0x%04x failed: %d\n",
1116 cosa->num, address, i);
1117 return -EIO;
1118 }
1119 pr_info("cosa%d: starting microcode at 0x%04x\n", cosa->num, address);
1120 cosa->startaddr = address;
1121 cosa->firmware_status |= COSA_FW_START;
1122 return 0;
1123}
1124
1125
1126static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1127{
1128 int l = strlen(cosa->id_string)+1;
1129 if (copy_to_user(string, cosa->id_string, l))
1130 return -EFAULT;
1131 return l;
1132}
1133
1134
1135static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1136{
1137 int l = strlen(cosa->type)+1;
1138 if (copy_to_user(string, cosa->type, l))
1139 return -EFAULT;
1140 return l;
1141}
1142
1143static int cosa_ioctl_common(struct cosa_data *cosa,
1144 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1145{
1146 void __user *argp = (void __user *)arg;
1147 switch (cmd) {
1148 case COSAIORSET:
1149 if (!capable(CAP_NET_ADMIN))
1150 return -EACCES;
1151 return cosa_reset(cosa);
1152 case COSAIOSTRT:
1153 if (!capable(CAP_SYS_RAWIO))
1154 return -EACCES;
1155 return cosa_start(cosa, arg);
1156 case COSAIODOWNLD:
1157 if (!capable(CAP_SYS_RAWIO))
1158 return -EACCES;
1159
1160 return cosa_download(cosa, argp);
1161 case COSAIORMEM:
1162 if (!capable(CAP_SYS_RAWIO))
1163 return -EACCES;
1164 return cosa_readmem(cosa, argp);
1165 case COSAIORTYPE:
1166 return cosa_gettype(cosa, argp);
1167 case COSAIORIDSTR:
1168 return cosa_getidstr(cosa, argp);
1169 case COSAIONRCARDS:
1170 return nr_cards;
1171 case COSAIONRCHANS:
1172 return cosa->nchannels;
1173 case COSAIOBMSET:
1174 if (!capable(CAP_SYS_RAWIO))
1175 return -EACCES;
1176 if (is_8bit(cosa))
1177 return -EINVAL;
1178 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1179 return -EINVAL;
1180 cosa->busmaster = arg;
1181 return 0;
1182 case COSAIOBMGET:
1183 return cosa->busmaster;
1184 }
1185 return -ENOIOCTLCMD;
1186}
1187
1188static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1189{
1190 int rv;
1191 struct channel_data *chan = dev_to_chan(dev);
1192 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1193 (unsigned long)ifr->ifr_data);
1194 if (rv != -ENOIOCTLCMD)
1195 return rv;
1196 return hdlc_ioctl(dev, ifr, cmd);
1197}
1198
1199static long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1200 unsigned long arg)
1201{
1202 struct channel_data *channel = file->private_data;
1203 struct cosa_data *cosa;
1204 long ret;
1205
1206 mutex_lock(&cosa_chardev_mutex);
1207 cosa = channel->cosa;
1208 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1209 mutex_unlock(&cosa_chardev_mutex);
1210 return ret;
1211}
1212
1213
1214
1215
1216
1217
1218
1219
1220static void cosa_enable_rx(struct channel_data *chan)
1221{
1222 struct cosa_data *cosa = chan->cosa;
1223
1224 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1225 put_driver_status(cosa);
1226}
1227
1228static void cosa_disable_rx(struct channel_data *chan)
1229{
1230 struct cosa_data *cosa = chan->cosa;
1231
1232 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1233 put_driver_status(cosa);
1234}
1235
1236
1237
1238
1239
1240
1241
1242static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1243{
1244 struct cosa_data *cosa = chan->cosa;
1245 unsigned long flags;
1246#ifdef DEBUG_DATA
1247 int i;
1248
1249 pr_info("cosa%dc%d: starting tx(0x%x)",
1250 chan->cosa->num, chan->num, len);
1251 for (i=0; i<len; i++)
1252 pr_cont(" %02x", buf[i]&0xff);
1253 pr_cont("\n");
1254#endif
1255 spin_lock_irqsave(&cosa->lock, flags);
1256 chan->txbuf = buf;
1257 chan->txsize = len;
1258 if (len > COSA_MTU)
1259 chan->txsize = COSA_MTU;
1260 spin_unlock_irqrestore(&cosa->lock, flags);
1261
1262
1263 set_bit(chan->num, &cosa->txbitmap);
1264 put_driver_status(cosa);
1265
1266 return 0;
1267}
1268
1269static void put_driver_status(struct cosa_data *cosa)
1270{
1271 unsigned long flags;
1272 int status;
1273
1274 spin_lock_irqsave(&cosa->lock, flags);
1275
1276 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1277 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1278 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1279 &DRIVER_TXMAP_MASK : 0);
1280 if (!cosa->rxtx) {
1281 if (cosa->rxbitmap|cosa->txbitmap) {
1282 if (!cosa->enabled) {
1283 cosa_putstatus(cosa, SR_RX_INT_ENA);
1284#ifdef DEBUG_IO
1285 debug_status_out(cosa, SR_RX_INT_ENA);
1286#endif
1287 cosa->enabled = 1;
1288 }
1289 } else if (cosa->enabled) {
1290 cosa->enabled = 0;
1291 cosa_putstatus(cosa, 0);
1292#ifdef DEBUG_IO
1293 debug_status_out(cosa, 0);
1294#endif
1295 }
1296 cosa_putdata8(cosa, status);
1297#ifdef DEBUG_IO
1298 debug_data_cmd(cosa, status);
1299#endif
1300 }
1301 spin_unlock_irqrestore(&cosa->lock, flags);
1302}
1303
1304static void put_driver_status_nolock(struct cosa_data *cosa)
1305{
1306 int status;
1307
1308 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1309 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1310 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1311 &DRIVER_TXMAP_MASK : 0);
1312
1313 if (cosa->rxbitmap|cosa->txbitmap) {
1314 cosa_putstatus(cosa, SR_RX_INT_ENA);
1315#ifdef DEBUG_IO
1316 debug_status_out(cosa, SR_RX_INT_ENA);
1317#endif
1318 cosa->enabled = 1;
1319 } else {
1320 cosa_putstatus(cosa, 0);
1321#ifdef DEBUG_IO
1322 debug_status_out(cosa, 0);
1323#endif
1324 cosa->enabled = 0;
1325 }
1326 cosa_putdata8(cosa, status);
1327#ifdef DEBUG_IO
1328 debug_data_cmd(cosa, status);
1329#endif
1330}
1331
1332
1333
1334
1335
1336
1337static void cosa_kick(struct cosa_data *cosa)
1338{
1339 unsigned long flags, flags1;
1340 char *s = "(probably) IRQ";
1341
1342 if (test_bit(RXBIT, &cosa->rxtx))
1343 s = "RX DMA";
1344 if (test_bit(TXBIT, &cosa->rxtx))
1345 s = "TX DMA";
1346
1347 pr_info("%s: %s timeout - restarting\n", cosa->name, s);
1348 spin_lock_irqsave(&cosa->lock, flags);
1349 cosa->rxtx = 0;
1350
1351 flags1 = claim_dma_lock();
1352 disable_dma(cosa->dma);
1353 clear_dma_ff(cosa->dma);
1354 release_dma_lock(flags1);
1355
1356
1357 udelay(100);
1358 cosa_putstatus(cosa, 0);
1359 udelay(100);
1360 (void) cosa_getdata8(cosa);
1361 udelay(100);
1362 cosa_putdata8(cosa, 0);
1363 udelay(100);
1364 put_driver_status_nolock(cosa);
1365 spin_unlock_irqrestore(&cosa->lock, flags);
1366}
1367
1368
1369
1370
1371
1372
1373static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1374{
1375 static int count;
1376 unsigned long b = (unsigned long)buf;
1377 if (b+len >= MAX_DMA_ADDRESS)
1378 return 0;
1379 if ((b^ (b+len)) & 0x10000) {
1380 if (count++ < 5)
1381 pr_info("%s: packet spanning a 64k boundary\n",
1382 chan->name);
1383 return 0;
1384 }
1385 return 1;
1386}
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1400{
1401 int i;
1402
1403 if (put_wait_data(cosa, 'w') == -1) return -1;
1404 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1405 if (get_wait_data(cosa) != '=') return -3;
1406
1407 if (puthexnumber(cosa, address) < 0) return -4;
1408 if (put_wait_data(cosa, ' ') == -1) return -10;
1409 if (get_wait_data(cosa) != ' ') return -11;
1410 if (get_wait_data(cosa) != '=') return -12;
1411
1412 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1413 if (put_wait_data(cosa, ' ') == -1) return -18;
1414 if (get_wait_data(cosa) != ' ') return -19;
1415
1416 while (length--) {
1417 char c;
1418#ifndef SRP_DOWNLOAD_AT_BOOT
1419 if (get_user(c, microcode))
1420 return -23;
1421#else
1422 c = *microcode;
1423#endif
1424 if (put_wait_data(cosa, c) == -1)
1425 return -20;
1426 microcode++;
1427 }
1428
1429 if (get_wait_data(cosa) != '\r') return -21;
1430 if (get_wait_data(cosa) != '\n') return -22;
1431 if (get_wait_data(cosa) != '.') return -23;
1432#if 0
1433 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1434#endif
1435 return 0;
1436}
1437
1438
1439
1440
1441
1442
1443
1444static int startmicrocode(struct cosa_data *cosa, int address)
1445{
1446 if (put_wait_data(cosa, 'g') == -1) return -1;
1447 if (get_wait_data(cosa) != 'g') return -2;
1448 if (get_wait_data(cosa) != '=') return -3;
1449
1450 if (puthexnumber(cosa, address) < 0) return -4;
1451 if (put_wait_data(cosa, '\r') == -1) return -5;
1452
1453 if (get_wait_data(cosa) != '\r') return -6;
1454 if (get_wait_data(cosa) != '\r') return -7;
1455 if (get_wait_data(cosa) != '\n') return -8;
1456 if (get_wait_data(cosa) != '\r') return -9;
1457 if (get_wait_data(cosa) != '\n') return -10;
1458#if 0
1459 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1460#endif
1461 return 0;
1462}
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1474{
1475 if (put_wait_data(cosa, 'r') == -1) return -1;
1476 if ((get_wait_data(cosa)) != 'r') return -2;
1477 if ((get_wait_data(cosa)) != '=') return -3;
1478
1479 if (puthexnumber(cosa, address) < 0) return -4;
1480 if (put_wait_data(cosa, ' ') == -1) return -5;
1481 if (get_wait_data(cosa) != ' ') return -6;
1482 if (get_wait_data(cosa) != '=') return -7;
1483
1484 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1485 if (put_wait_data(cosa, ' ') == -1) return -9;
1486 if (get_wait_data(cosa) != ' ') return -10;
1487
1488 while (length--) {
1489 char c;
1490 int i;
1491 if ((i=get_wait_data(cosa)) == -1) {
1492 pr_info("0x%04x bytes remaining\n", length);
1493 return -11;
1494 }
1495 c=i;
1496#if 1
1497 if (put_user(c, microcode))
1498 return -23;
1499#else
1500 *microcode = c;
1501#endif
1502 microcode++;
1503 }
1504
1505 if (get_wait_data(cosa) != '\r') return -21;
1506 if (get_wait_data(cosa) != '\n') return -22;
1507 if (get_wait_data(cosa) != '.') return -23;
1508#if 0
1509 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1510#endif
1511 return 0;
1512}
1513
1514
1515
1516
1517
1518static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1519{
1520 int i=0, id=0, prev=0, curr=0;
1521
1522
1523 cosa_putstatus(cosa, 0);
1524 cosa_getdata8(cosa);
1525 cosa_putstatus(cosa, SR_RST);
1526 msleep(500);
1527
1528 cosa_putstatus(cosa, 0);
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1539 if ((curr = get_wait_data(cosa)) == -1) {
1540 return -1;
1541 }
1542 curr &= 0xff;
1543 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1544 idstring[id++] = curr;
1545 if (curr == 0x2e && prev == '\n')
1546 break;
1547 }
1548
1549 idstring[id] = '\0';
1550 return id;
1551}
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561static int get_wait_data(struct cosa_data *cosa)
1562{
1563 int retries = 1000;
1564
1565 while (--retries) {
1566
1567 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1568 short r;
1569 r = cosa_getdata8(cosa);
1570#if 0
1571 pr_info("get_wait_data returning after %d retries\n",
1572 999-retries);
1573#endif
1574 return r;
1575 }
1576
1577 schedule_timeout_interruptible(1);
1578 }
1579 pr_info("timeout in get_wait_data (status 0x%x)\n",
1580 cosa_getstatus(cosa));
1581 return -1;
1582}
1583
1584
1585
1586
1587
1588
1589static int put_wait_data(struct cosa_data *cosa, int data)
1590{
1591 int retries = 1000;
1592 while (--retries) {
1593
1594 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1595 cosa_putdata8(cosa, data);
1596#if 0
1597 pr_info("Putdata: %d retries\n", 999-retries);
1598#endif
1599 return 0;
1600 }
1601#if 0
1602
1603 schedule_timeout_interruptible(1);
1604#endif
1605 }
1606 pr_info("cosa%d: timeout in put_wait_data (status 0x%x)\n",
1607 cosa->num, cosa_getstatus(cosa));
1608 return -1;
1609}
1610
1611
1612
1613
1614
1615
1616
1617static int puthexnumber(struct cosa_data *cosa, int number)
1618{
1619 char temp[5];
1620 int i;
1621
1622
1623 sprintf(temp, "%04X", number);
1624 for (i=0; i<4; i++) {
1625 if (put_wait_data(cosa, temp[i]) == -1) {
1626 pr_notice("cosa%d: puthexnumber failed to write byte %d\n",
1627 cosa->num, i);
1628 return -1-2*i;
1629 }
1630 if (get_wait_data(cosa) != temp[i]) {
1631 pr_notice("cosa%d: puthexhumber failed to read echo of byte %d\n",
1632 cosa->num, i);
1633 return -2-2*i;
1634 }
1635 }
1636 return 0;
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
1671
1672
1673static inline void tx_interrupt(struct cosa_data *cosa, int status)
1674{
1675 unsigned long flags, flags1;
1676#ifdef DEBUG_IRQS
1677 pr_info("cosa%d: SR_DOWN_REQUEST status=0x%04x\n", cosa->num, status);
1678#endif
1679 spin_lock_irqsave(&cosa->lock, flags);
1680 set_bit(TXBIT, &cosa->rxtx);
1681 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1682
1683 int i=0;
1684 if (!cosa->txbitmap) {
1685 pr_warn("%s: No channel wants data in TX IRQ. Expect DMA timeout.\n",
1686 cosa->name);
1687 put_driver_status_nolock(cosa);
1688 clear_bit(TXBIT, &cosa->rxtx);
1689 spin_unlock_irqrestore(&cosa->lock, flags);
1690 return;
1691 }
1692 while (1) {
1693 cosa->txchan++;
1694 i++;
1695 if (cosa->txchan >= cosa->nchannels)
1696 cosa->txchan = 0;
1697 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1698 continue;
1699 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1700 break;
1701
1702 if (i > cosa->nchannels) {
1703
1704#ifdef DEBUG_IRQS
1705 printk(KERN_DEBUG "%s: Forcing TX "
1706 "to not-ready channel %d\n",
1707 cosa->name, cosa->txchan);
1708#endif
1709 break;
1710 }
1711 }
1712
1713 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1714 if (cosa_dma_able(cosa->chan+cosa->txchan,
1715 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1716 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1717 } else {
1718 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1719 cosa->txsize);
1720 cosa->txbuf = cosa->bouncebuf;
1721 }
1722 }
1723
1724 if (is_8bit(cosa)) {
1725 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1726 cosa_putstatus(cosa, SR_TX_INT_ENA);
1727 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1728 ((cosa->txsize >> 8) & 0x1f));
1729#ifdef DEBUG_IO
1730 debug_status_out(cosa, SR_TX_INT_ENA);
1731 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1732 ((cosa->txsize >> 8) & 0x1f));
1733 debug_data_in(cosa, cosa_getdata8(cosa));
1734#else
1735 cosa_getdata8(cosa);
1736#endif
1737 set_bit(IRQBIT, &cosa->rxtx);
1738 spin_unlock_irqrestore(&cosa->lock, flags);
1739 return;
1740 } else {
1741 clear_bit(IRQBIT, &cosa->rxtx);
1742 cosa_putstatus(cosa, 0);
1743 cosa_putdata8(cosa, cosa->txsize&0xff);
1744#ifdef DEBUG_IO
1745 debug_status_out(cosa, 0);
1746 debug_data_out(cosa, cosa->txsize&0xff);
1747#endif
1748 }
1749 } else {
1750 cosa_putstatus(cosa, SR_TX_INT_ENA);
1751 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1752 | (cosa->txsize & 0x1fff));
1753#ifdef DEBUG_IO
1754 debug_status_out(cosa, SR_TX_INT_ENA);
1755 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1756 | (cosa->txsize & 0x1fff));
1757 debug_data_in(cosa, cosa_getdata8(cosa));
1758 debug_status_out(cosa, 0);
1759#else
1760 cosa_getdata8(cosa);
1761#endif
1762 cosa_putstatus(cosa, 0);
1763 }
1764
1765 if (cosa->busmaster) {
1766 unsigned long addr = virt_to_bus(cosa->txbuf);
1767 int count=0;
1768 pr_info("busmaster IRQ\n");
1769 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1770 count++;
1771 udelay(10);
1772 if (count > 1000) break;
1773 }
1774 pr_info("status %x\n", cosa_getstatus(cosa));
1775 pr_info("ready after %d loops\n", count);
1776 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1777
1778 count = 0;
1779 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1780 count++;
1781 if (count > 1000) break;
1782 udelay(10);
1783 }
1784 pr_info("ready after %d loops\n", count);
1785 cosa_putdata16(cosa, addr &0xffff);
1786 flags1 = claim_dma_lock();
1787 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1788 enable_dma(cosa->dma);
1789 release_dma_lock(flags1);
1790 } else {
1791
1792 flags1 = claim_dma_lock();
1793 disable_dma(cosa->dma);
1794 clear_dma_ff(cosa->dma);
1795 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1796 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1797 set_dma_count(cosa->dma, cosa->txsize);
1798 enable_dma(cosa->dma);
1799 release_dma_lock(flags1);
1800 }
1801 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1802#ifdef DEBUG_IO
1803 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1804#endif
1805 spin_unlock_irqrestore(&cosa->lock, flags);
1806}
1807
1808static inline void rx_interrupt(struct cosa_data *cosa, int status)
1809{
1810 unsigned long flags;
1811#ifdef DEBUG_IRQS
1812 pr_info("cosa%d: SR_UP_REQUEST\n", cosa->num);
1813#endif
1814
1815 spin_lock_irqsave(&cosa->lock, flags);
1816 set_bit(RXBIT, &cosa->rxtx);
1817
1818 if (is_8bit(cosa)) {
1819 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1820 set_bit(IRQBIT, &cosa->rxtx);
1821 put_driver_status_nolock(cosa);
1822 cosa->rxsize = cosa_getdata8(cosa) <<8;
1823#ifdef DEBUG_IO
1824 debug_data_in(cosa, cosa->rxsize >> 8);
1825#endif
1826 spin_unlock_irqrestore(&cosa->lock, flags);
1827 return;
1828 } else {
1829 clear_bit(IRQBIT, &cosa->rxtx);
1830 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1831#ifdef DEBUG_IO
1832 debug_data_in(cosa, cosa->rxsize & 0xff);
1833#endif
1834#if 0
1835 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1836 cosa->num, cosa->rxsize);
1837#endif
1838 }
1839 } else {
1840 cosa->rxsize = cosa_getdata16(cosa);
1841#ifdef DEBUG_IO
1842 debug_data_in(cosa, cosa->rxsize);
1843#endif
1844#if 0
1845 pr_info("cosa%d: receive rxsize = (0x%04x)\n",
1846 cosa->num, cosa->rxsize);
1847#endif
1848 }
1849 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1850 pr_warn("%s: rx for unknown channel (0x%04x)\n",
1851 cosa->name, cosa->rxsize);
1852 spin_unlock_irqrestore(&cosa->lock, flags);
1853 goto reject;
1854 }
1855 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1856 cosa->rxsize &= 0x1fff;
1857 spin_unlock_irqrestore(&cosa->lock, flags);
1858
1859 cosa->rxbuf = NULL;
1860 if (cosa->rxchan->setup_rx)
1861 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1862
1863 if (!cosa->rxbuf) {
1864reject:
1865 pr_info("cosa%d: rejecting packet on channel %d\n",
1866 cosa->num, cosa->rxchan->num);
1867 cosa->rxbuf = cosa->bouncebuf;
1868 }
1869
1870
1871 flags = claim_dma_lock();
1872 disable_dma(cosa->dma);
1873 clear_dma_ff(cosa->dma);
1874 set_dma_mode(cosa->dma, DMA_MODE_READ);
1875 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1876 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1877 } else {
1878 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1879 }
1880 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1881 enable_dma(cosa->dma);
1882 release_dma_lock(flags);
1883 spin_lock_irqsave(&cosa->lock, flags);
1884 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1885 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1886 cosa_putdata8(cosa, DRIVER_RX_READY);
1887#ifdef DEBUG_IO
1888 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1889 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1890 debug_data_cmd(cosa, DRIVER_RX_READY);
1891#endif
1892 spin_unlock_irqrestore(&cosa->lock, flags);
1893}
1894
1895static inline void eot_interrupt(struct cosa_data *cosa, int status)
1896{
1897 unsigned long flags, flags1;
1898 spin_lock_irqsave(&cosa->lock, flags);
1899 flags1 = claim_dma_lock();
1900 disable_dma(cosa->dma);
1901 clear_dma_ff(cosa->dma);
1902 release_dma_lock(flags1);
1903 if (test_bit(TXBIT, &cosa->rxtx)) {
1904 struct channel_data *chan = cosa->chan+cosa->txchan;
1905 if (chan->tx_done)
1906 if (chan->tx_done(chan, cosa->txsize))
1907 clear_bit(chan->num, &cosa->txbitmap);
1908 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1909#ifdef DEBUG_DATA
1910 {
1911 int i;
1912 pr_info("cosa%dc%d: done rx(0x%x)",
1913 cosa->num, cosa->rxchan->num, cosa->rxsize);
1914 for (i=0; i<cosa->rxsize; i++)
1915 pr_cont(" %02x", cosa->rxbuf[i]&0xff);
1916 pr_cont("\n");
1917 }
1918#endif
1919
1920 if (cosa->rxbuf == cosa->bouncebuf)
1921 goto out;
1922 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1923 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1924 if (cosa->rxchan->rx_done)
1925 if (cosa->rxchan->rx_done(cosa->rxchan))
1926 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1927 } else {
1928 pr_notice("cosa%d: unexpected EOT interrupt\n", cosa->num);
1929 }
1930
1931
1932
1933
1934
1935
1936out:
1937 cosa->rxtx = 0;
1938 put_driver_status_nolock(cosa);
1939 spin_unlock_irqrestore(&cosa->lock, flags);
1940}
1941
1942static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1943{
1944 unsigned status;
1945 int count = 0;
1946 struct cosa_data *cosa = cosa_;
1947again:
1948 status = cosa_getstatus(cosa);
1949#ifdef DEBUG_IRQS
1950 pr_info("cosa%d: got IRQ, status 0x%02x\n", cosa->num, status & 0xff);
1951#endif
1952#ifdef DEBUG_IO
1953 debug_status_in(cosa, status);
1954#endif
1955 switch (status & SR_CMD_FROM_SRP_MASK) {
1956 case SR_DOWN_REQUEST:
1957 tx_interrupt(cosa, status);
1958 break;
1959 case SR_UP_REQUEST:
1960 rx_interrupt(cosa, status);
1961 break;
1962 case SR_END_OF_TRANSFER:
1963 eot_interrupt(cosa, status);
1964 break;
1965 default:
1966
1967 if (count++ < 100) {
1968 udelay(100);
1969 goto again;
1970 }
1971 pr_info("cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1972 cosa->num, status & 0xff, count);
1973 }
1974#ifdef DEBUG_IRQS
1975 if (count)
1976 pr_info("%s: %d-times got unknown status in IRQ\n",
1977 cosa->name, count);
1978 else
1979 pr_info("%s: returning from IRQ\n", cosa->name);
1980#endif
1981 return IRQ_HANDLED;
1982}
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992#ifdef DEBUG_IO
1993static void debug_status_in(struct cosa_data *cosa, int status)
1994{
1995 char *s;
1996 switch (status & SR_CMD_FROM_SRP_MASK) {
1997 case SR_UP_REQUEST:
1998 s = "RX_REQ";
1999 break;
2000 case SR_DOWN_REQUEST:
2001 s = "TX_REQ";
2002 break;
2003 case SR_END_OF_TRANSFER:
2004 s = "ET_REQ";
2005 break;
2006 default:
2007 s = "NO_REQ";
2008 break;
2009 }
2010 pr_info("%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2011 cosa->name,
2012 status,
2013 status & SR_USR_RQ ? "USR_RQ|" : "",
2014 status & SR_TX_RDY ? "TX_RDY|" : "",
2015 status & SR_RX_RDY ? "RX_RDY|" : "",
2016 s);
2017}
2018
2019static void debug_status_out(struct cosa_data *cosa, int status)
2020{
2021 pr_info("%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2022 cosa->name,
2023 status,
2024 status & SR_RX_DMA_ENA ? "RXDMA|" : "!rxdma|",
2025 status & SR_TX_DMA_ENA ? "TXDMA|" : "!txdma|",
2026 status & SR_RST ? "RESET|" : "",
2027 status & SR_USR_INT_ENA ? "USRINT|" : "!usrint|",
2028 status & SR_TX_INT_ENA ? "TXINT|" : "!txint|",
2029 status & SR_RX_INT_ENA ? "RXINT" : "!rxint");
2030}
2031
2032static void debug_data_in(struct cosa_data *cosa, int data)
2033{
2034 pr_info("%s: IO: data -> 0x%04x\n", cosa->name, data);
2035}
2036
2037static void debug_data_out(struct cosa_data *cosa, int data)
2038{
2039 pr_info("%s: IO: data <- 0x%04x\n", cosa->name, data);
2040}
2041
2042static void debug_data_cmd(struct cosa_data *cosa, int data)
2043{
2044 pr_info("%s: IO: data <- 0x%04x (%s|%s)\n",
2045 cosa->name, data,
2046 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2047 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2048}
2049#endif
2050
2051
2052