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#include <linux/module.h>
78#include <linux/kernel.h>
79#include <linux/sched.h>
80#include <linux/slab.h>
81#include <linux/poll.h>
82#include <linux/fs.h>
83#include <linux/interrupt.h>
84#include <linux/delay.h>
85#include <linux/hdlc.h>
86#include <linux/errno.h>
87#include <linux/ioport.h>
88#include <linux/netdevice.h>
89#include <linux/spinlock.h>
90#include <linux/mutex.h>
91#include <linux/device.h>
92#include <linux/smp_lock.h>
93#include <asm/io.h>
94#include <asm/dma.h>
95#include <asm/byteorder.h>
96
97#undef COSA_SLOW_IO
98
99#include "cosa.h"
100
101
102#define COSA_MAX_ID_STRING 128
103
104
105#define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
106
107
108
109struct channel_data {
110 int usage;
111 int num;
112 struct cosa_data *cosa;
113 int txsize;
114 char *txbuf;
115 char name[COSA_MAX_NAME];
116
117
118
119 char *(*setup_rx)(struct channel_data *channel, int size);
120
121 int (*rx_done)(struct channel_data *channel);
122
123 int (*tx_done)(struct channel_data *channel, int size);
124
125
126 struct mutex rlock;
127 struct semaphore wsem;
128 char *rxdata;
129 int rxsize;
130 wait_queue_head_t txwaitq, rxwaitq;
131 int tx_status, rx_status;
132
133
134 struct net_device *netdev;
135 struct sk_buff *rx_skb, *tx_skb;
136};
137
138
139#define COSA_FW_RESET (1<<0)
140#define COSA_FW_DOWNLOAD (1<<1)
141#define COSA_FW_START (1<<2)
142
143struct cosa_data {
144 int num;
145 char name[COSA_MAX_NAME];
146 unsigned int datareg, statusreg;
147 unsigned short irq, dma;
148 unsigned short startaddr;
149 unsigned short busmaster;
150 int nchannels;
151 int driver_status;
152 int firmware_status;
153 unsigned long rxbitmap, txbitmap;
154 unsigned long rxtx;
155 int enabled;
156 int usage;
157 int txchan, txsize, rxsize;
158 struct channel_data *rxchan;
159 char *bouncebuf;
160 char *txbuf, *rxbuf;
161 struct channel_data *chan;
162 spinlock_t lock;
163 char id_string[COSA_MAX_ID_STRING];
164 char *type;
165};
166
167
168
169
170
171
172
173
174
175
176
177static int cosa_major = 117;
178
179
180
181
182
183
184#define CARD_MINOR_BITS 4
185
186
187
188
189
190#define MAX_CARDS 16
191
192
193#define DRIVER_RX_READY 0x0001
194#define DRIVER_TX_READY 0x0002
195#define DRIVER_TXMAP_SHIFT 2
196#define DRIVER_TXMAP_MASK 0x0c
197
198
199
200
201
202#define TXBIT 0
203#define RXBIT 1
204#define IRQBIT 2
205
206#define COSA_MTU 2000
207
208#undef DEBUG_DATA
209#undef DEBUG_IRQS
210#undef DEBUG_IO
211
212#define TX_TIMEOUT (5*HZ)
213
214
215static struct cosa_data cosa_cards[MAX_CARDS];
216static int nr_cards;
217
218#ifdef COSA_ISA_AUTOPROBE
219static int io[MAX_CARDS+1] = { 0x220, 0x228, 0x210, 0x218, 0, };
220
221static int dma[MAX_CARDS+1] = { 1, 7, 1, 7, 1, 7, 1, 7, 0, };
222#else
223static int io[MAX_CARDS+1];
224static int dma[MAX_CARDS+1];
225#endif
226
227static int irq[MAX_CARDS+1] = { -1, -1, -1, -1, -1, -1, 0, };
228
229
230static struct class *cosa_class;
231
232#ifdef MODULE
233module_param_array(io, int, NULL, 0);
234MODULE_PARM_DESC(io, "The I/O bases of the COSA or SRP cards");
235module_param_array(irq, int, NULL, 0);
236MODULE_PARM_DESC(irq, "The IRQ lines of the COSA or SRP cards");
237module_param_array(dma, int, NULL, 0);
238MODULE_PARM_DESC(dma, "The DMA channels of the COSA or SRP cards");
239
240MODULE_AUTHOR("Jan \"Yenya\" Kasprzak, <kas@fi.muni.cz>");
241MODULE_DESCRIPTION("Modular driver for the COSA or SRP synchronous card");
242MODULE_LICENSE("GPL");
243#endif
244
245
246#ifdef COSA_SLOW_IO
247#define cosa_outb outb_p
248#define cosa_outw outw_p
249#define cosa_inb inb_p
250#define cosa_inw inw_p
251#else
252#define cosa_outb outb
253#define cosa_outw outw
254#define cosa_inb inb
255#define cosa_inw inw
256#endif
257
258#define is_8bit(cosa) (!(cosa->datareg & 0x08))
259
260#define cosa_getstatus(cosa) (cosa_inb(cosa->statusreg))
261#define cosa_putstatus(cosa, stat) (cosa_outb(stat, cosa->statusreg))
262#define cosa_getdata16(cosa) (cosa_inw(cosa->datareg))
263#define cosa_getdata8(cosa) (cosa_inb(cosa->datareg))
264#define cosa_putdata16(cosa, dt) (cosa_outw(dt, cosa->datareg))
265#define cosa_putdata8(cosa, dt) (cosa_outb(dt, cosa->datareg))
266
267
268static int cosa_probe(int ioaddr, int irq, int dma);
269
270
271static void cosa_enable_rx(struct channel_data *chan);
272static void cosa_disable_rx(struct channel_data *chan);
273static int cosa_start_tx(struct channel_data *channel, char *buf, int size);
274static void cosa_kick(struct cosa_data *cosa);
275static int cosa_dma_able(struct channel_data *chan, char *buf, int data);
276
277
278static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
279 unsigned short parity);
280static int cosa_net_open(struct net_device *d);
281static int cosa_net_close(struct net_device *d);
282static void cosa_net_timeout(struct net_device *d);
283static netdev_tx_t cosa_net_tx(struct sk_buff *skb, struct net_device *d);
284static char *cosa_net_setup_rx(struct channel_data *channel, int size);
285static int cosa_net_rx_done(struct channel_data *channel);
286static int cosa_net_tx_done(struct channel_data *channel, int size);
287static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
288
289
290static char *chrdev_setup_rx(struct channel_data *channel, int size);
291static int chrdev_rx_done(struct channel_data *channel);
292static int chrdev_tx_done(struct channel_data *channel, int size);
293static ssize_t cosa_read(struct file *file,
294 char __user *buf, size_t count, loff_t *ppos);
295static ssize_t cosa_write(struct file *file,
296 const char __user *buf, size_t count, loff_t *ppos);
297static unsigned int cosa_poll(struct file *file, poll_table *poll);
298static int cosa_open(struct inode *inode, struct file *file);
299static int cosa_release(struct inode *inode, struct file *file);
300static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
301 unsigned int cmd, unsigned long arg);
302#ifdef COSA_FASYNC_WORKING
303static int cosa_fasync(struct inode *inode, struct file *file, int on);
304#endif
305
306static const struct file_operations cosa_fops = {
307 .owner = THIS_MODULE,
308 .llseek = no_llseek,
309 .read = cosa_read,
310 .write = cosa_write,
311 .poll = cosa_poll,
312 .ioctl = cosa_chardev_ioctl,
313 .open = cosa_open,
314 .release = cosa_release,
315#ifdef COSA_FASYNC_WORKING
316 .fasync = cosa_fasync,
317#endif
318};
319
320
321static int cosa_start(struct cosa_data *cosa, int address);
322static int cosa_reset(struct cosa_data *cosa);
323static int cosa_download(struct cosa_data *cosa, void __user *a);
324static int cosa_readmem(struct cosa_data *cosa, void __user *a);
325
326
327static int download(struct cosa_data *cosa, const char __user *data, int addr, int len);
328static int startmicrocode(struct cosa_data *cosa, int address);
329static int readmem(struct cosa_data *cosa, char __user *data, int addr, int len);
330static int cosa_reset_and_read_id(struct cosa_data *cosa, char *id);
331
332
333static int get_wait_data(struct cosa_data *cosa);
334static int put_wait_data(struct cosa_data *cosa, int data);
335static int puthexnumber(struct cosa_data *cosa, int number);
336static void put_driver_status(struct cosa_data *cosa);
337static void put_driver_status_nolock(struct cosa_data *cosa);
338
339
340static irqreturn_t cosa_interrupt(int irq, void *cosa);
341
342
343#ifdef DEBUG_IO
344static void debug_data_in(struct cosa_data *cosa, int data);
345static void debug_data_out(struct cosa_data *cosa, int data);
346static void debug_data_cmd(struct cosa_data *cosa, int data);
347static void debug_status_in(struct cosa_data *cosa, int status);
348static void debug_status_out(struct cosa_data *cosa, int status);
349#endif
350
351static inline struct channel_data* dev_to_chan(struct net_device *dev)
352{
353 return (struct channel_data *)dev_to_hdlc(dev)->priv;
354}
355
356
357
358static int __init cosa_init(void)
359{
360 int i, err = 0;
361
362 if (cosa_major > 0) {
363 if (register_chrdev(cosa_major, "cosa", &cosa_fops)) {
364 printk(KERN_WARNING "cosa: unable to get major %d\n",
365 cosa_major);
366 err = -EIO;
367 goto out;
368 }
369 } else {
370 if (!(cosa_major=register_chrdev(0, "cosa", &cosa_fops))) {
371 printk(KERN_WARNING "cosa: unable to register chardev\n");
372 err = -EIO;
373 goto out;
374 }
375 }
376 for (i=0; i<MAX_CARDS; i++)
377 cosa_cards[i].num = -1;
378 for (i=0; io[i] != 0 && i < MAX_CARDS; i++)
379 cosa_probe(io[i], irq[i], dma[i]);
380 if (!nr_cards) {
381 printk(KERN_WARNING "cosa: no devices found.\n");
382 unregister_chrdev(cosa_major, "cosa");
383 err = -ENODEV;
384 goto out;
385 }
386 cosa_class = class_create(THIS_MODULE, "cosa");
387 if (IS_ERR(cosa_class)) {
388 err = PTR_ERR(cosa_class);
389 goto out_chrdev;
390 }
391 for (i = 0; i < nr_cards; i++)
392 device_create(cosa_class, NULL, MKDEV(cosa_major, i), NULL,
393 "cosa%d", i);
394 err = 0;
395 goto out;
396
397out_chrdev:
398 unregister_chrdev(cosa_major, "cosa");
399out:
400 return err;
401}
402module_init(cosa_init);
403
404static void __exit cosa_exit(void)
405{
406 struct cosa_data *cosa;
407 int i;
408
409 for (i = 0; i < nr_cards; i++)
410 device_destroy(cosa_class, MKDEV(cosa_major, i));
411 class_destroy(cosa_class);
412
413 for (cosa = cosa_cards; nr_cards--; cosa++) {
414
415 for (i = 0; i < cosa->nchannels; i++) {
416
417 unregister_hdlc_device(cosa->chan[i].netdev);
418 free_netdev(cosa->chan[i].netdev);
419 }
420
421 kfree(cosa->chan);
422 kfree(cosa->bouncebuf);
423 free_irq(cosa->irq, cosa);
424 free_dma(cosa->dma);
425 release_region(cosa->datareg, is_8bit(cosa) ? 2 : 4);
426 }
427 unregister_chrdev(cosa_major, "cosa");
428}
429module_exit(cosa_exit);
430
431static const struct net_device_ops cosa_ops = {
432 .ndo_open = cosa_net_open,
433 .ndo_stop = cosa_net_close,
434 .ndo_change_mtu = hdlc_change_mtu,
435 .ndo_start_xmit = hdlc_start_xmit,
436 .ndo_do_ioctl = cosa_net_ioctl,
437 .ndo_tx_timeout = cosa_net_timeout,
438};
439
440static int cosa_probe(int base, int irq, int dma)
441{
442 struct cosa_data *cosa = cosa_cards+nr_cards;
443 int i, err = 0;
444
445 memset(cosa, 0, sizeof(struct cosa_data));
446
447
448
449 if ((irq >= 0 && irq < 2) || irq > 15 || (irq < 10 && irq > 7)) {
450 printk (KERN_INFO "cosa_probe: invalid IRQ %d\n", irq);
451 return -1;
452 }
453
454
455 if (base < 0x100 || base > 0x3ff || base & 0x7) {
456 printk (KERN_INFO "cosa_probe: invalid I/O address 0x%x\n",
457 base);
458 return -1;
459 }
460
461 if (dma < 0 || dma == 4 || dma > 7) {
462 printk (KERN_INFO "cosa_probe: invalid DMA %d\n", dma);
463 return -1;
464 }
465
466
467 if (((base & 0x8) && dma < 4) || (!(base & 0x8) && dma > 3)) {
468 printk (KERN_INFO "cosa_probe: 8/16 bit base and DMA mismatch"
469 " (base=0x%x, dma=%d)\n", 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 "cosa: 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 printk(KERN_INFO "cosa: valid signature not found at 0x%x.\n",
496 base);
497#endif
498 err = -1;
499 goto err_out;
500 }
501
502 release_region(base, is_8bit(cosa)?2:4);
503 if (!request_region(base, is_8bit(cosa)?2:4, cosa->type)) {
504 printk(KERN_DEBUG "cosa: changing name at 0x%x failed.\n", base);
505 return -1;
506 }
507
508
509 if (irq < 0) {
510 unsigned long irqs;
511
512 irqs = probe_irq_on();
513
514
515
516
517
518
519 set_current_state(TASK_INTERRUPTIBLE);
520 cosa_putstatus(cosa, SR_TX_INT_ENA);
521 schedule_timeout(30);
522 irq = probe_irq_off(irqs);
523
524 cosa_putstatus(cosa, 0);
525
526 cosa_getdata8(cosa);
527
528 if (irq < 0) {
529 printk (KERN_INFO "cosa IRQ autoprobe: multiple interrupts obtained (%d, board at 0x%x)\n",
530 irq, cosa->datareg);
531 err = -1;
532 goto err_out;
533 }
534 if (irq == 0) {
535 printk (KERN_INFO "cosa IRQ autoprobe: no interrupt obtained (board at 0x%x)\n",
536 cosa->datareg);
537
538 }
539 }
540
541 cosa->irq = irq;
542 cosa->num = nr_cards;
543 cosa->usage = 0;
544 cosa->nchannels = 2;
545
546 if (request_irq(cosa->irq, cosa_interrupt, 0, cosa->type, cosa)) {
547 err = -1;
548 goto err_out;
549 }
550 if (request_dma(cosa->dma, cosa->type)) {
551 err = -1;
552 goto err_out1;
553 }
554
555 cosa->bouncebuf = kmalloc(COSA_MTU, GFP_KERNEL|GFP_DMA);
556 if (!cosa->bouncebuf) {
557 err = -ENOMEM;
558 goto err_out2;
559 }
560 sprintf(cosa->name, "cosa%d", cosa->num);
561
562
563 cosa->chan = kcalloc(cosa->nchannels, sizeof(struct channel_data), GFP_KERNEL);
564 if (!cosa->chan) {
565 err = -ENOMEM;
566 goto err_out3;
567 }
568
569 for (i = 0; i < cosa->nchannels; i++) {
570 struct channel_data *chan = &cosa->chan[i];
571
572 chan->cosa = cosa;
573 chan->num = i;
574 sprintf(chan->name, "cosa%dc%d", chan->cosa->num, i);
575
576
577 mutex_init(&chan->rlock);
578 init_MUTEX(&chan->wsem);
579
580
581 if (!(chan->netdev = alloc_hdlcdev(chan))) {
582 printk(KERN_WARNING "%s: alloc_hdlcdev failed.\n",
583 chan->name);
584 goto err_hdlcdev;
585 }
586 dev_to_hdlc(chan->netdev)->attach = cosa_net_attach;
587 dev_to_hdlc(chan->netdev)->xmit = cosa_net_tx;
588 chan->netdev->netdev_ops = &cosa_ops;
589 chan->netdev->watchdog_timeo = TX_TIMEOUT;
590 chan->netdev->base_addr = chan->cosa->datareg;
591 chan->netdev->irq = chan->cosa->irq;
592 chan->netdev->dma = chan->cosa->dma;
593 if (register_hdlc_device(chan->netdev)) {
594 printk(KERN_WARNING "%s: register_hdlc_device()"
595 " failed.\n", chan->netdev->name);
596 free_netdev(chan->netdev);
597 goto err_hdlcdev;
598 }
599 }
600
601 printk (KERN_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 printk(KERN_NOTICE "cosa%d: allocating resources failed\n",
622 cosa->num);
623 return err;
624}
625
626
627
628
629static int cosa_net_attach(struct net_device *dev, unsigned short encoding,
630 unsigned short parity)
631{
632 if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
633 return 0;
634 return -EINVAL;
635}
636
637static int cosa_net_open(struct net_device *dev)
638{
639 struct channel_data *chan = dev_to_chan(dev);
640 int err;
641 unsigned long flags;
642
643 if (!(chan->cosa->firmware_status & COSA_FW_START)) {
644 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
645 chan->cosa->name, chan->cosa->firmware_status);
646 return -EPERM;
647 }
648 spin_lock_irqsave(&chan->cosa->lock, flags);
649 if (chan->usage != 0) {
650 printk(KERN_WARNING "%s: cosa_net_open called with usage count"
651 " %d\n", chan->name, chan->usage);
652 spin_unlock_irqrestore(&chan->cosa->lock, flags);
653 return -EBUSY;
654 }
655 chan->setup_rx = cosa_net_setup_rx;
656 chan->tx_done = cosa_net_tx_done;
657 chan->rx_done = cosa_net_rx_done;
658 chan->usage = -1;
659 chan->cosa->usage++;
660 spin_unlock_irqrestore(&chan->cosa->lock, flags);
661
662 err = hdlc_open(dev);
663 if (err) {
664 spin_lock_irqsave(&chan->cosa->lock, flags);
665 chan->usage = 0;
666 chan->cosa->usage--;
667 spin_unlock_irqrestore(&chan->cosa->lock, flags);
668 return err;
669 }
670
671 netif_start_queue(dev);
672 cosa_enable_rx(chan);
673 return 0;
674}
675
676static netdev_tx_t cosa_net_tx(struct sk_buff *skb,
677 struct net_device *dev)
678{
679 struct channel_data *chan = dev_to_chan(dev);
680
681 netif_stop_queue(dev);
682
683 chan->tx_skb = skb;
684 cosa_start_tx(chan, skb->data, skb->len);
685 return NETDEV_TX_OK;
686}
687
688static void cosa_net_timeout(struct net_device *dev)
689{
690 struct channel_data *chan = dev_to_chan(dev);
691
692 if (test_bit(RXBIT, &chan->cosa->rxtx)) {
693 chan->netdev->stats.rx_errors++;
694 chan->netdev->stats.rx_missed_errors++;
695 } else {
696 chan->netdev->stats.tx_errors++;
697 chan->netdev->stats.tx_aborted_errors++;
698 }
699 cosa_kick(chan->cosa);
700 if (chan->tx_skb) {
701 dev_kfree_skb(chan->tx_skb);
702 chan->tx_skb = NULL;
703 }
704 netif_wake_queue(dev);
705}
706
707static int cosa_net_close(struct net_device *dev)
708{
709 struct channel_data *chan = dev_to_chan(dev);
710 unsigned long flags;
711
712 netif_stop_queue(dev);
713 hdlc_close(dev);
714 cosa_disable_rx(chan);
715 spin_lock_irqsave(&chan->cosa->lock, flags);
716 if (chan->rx_skb) {
717 kfree_skb(chan->rx_skb);
718 chan->rx_skb = NULL;
719 }
720 if (chan->tx_skb) {
721 kfree_skb(chan->tx_skb);
722 chan->tx_skb = NULL;
723 }
724 chan->usage = 0;
725 chan->cosa->usage--;
726 spin_unlock_irqrestore(&chan->cosa->lock, flags);
727 return 0;
728}
729
730static char *cosa_net_setup_rx(struct channel_data *chan, int size)
731{
732
733
734
735
736 kfree_skb(chan->rx_skb);
737 chan->rx_skb = dev_alloc_skb(size);
738 if (chan->rx_skb == NULL) {
739 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet\n",
740 chan->name);
741 chan->netdev->stats.rx_dropped++;
742 return NULL;
743 }
744 chan->netdev->trans_start = jiffies;
745 return skb_put(chan->rx_skb, size);
746}
747
748static int cosa_net_rx_done(struct channel_data *chan)
749{
750 if (!chan->rx_skb) {
751 printk(KERN_WARNING "%s: rx_done with empty skb!\n",
752 chan->name);
753 chan->netdev->stats.rx_errors++;
754 chan->netdev->stats.rx_frame_errors++;
755 return 0;
756 }
757 chan->rx_skb->protocol = hdlc_type_trans(chan->rx_skb, chan->netdev);
758 chan->rx_skb->dev = chan->netdev;
759 skb_reset_mac_header(chan->rx_skb);
760 chan->netdev->stats.rx_packets++;
761 chan->netdev->stats.rx_bytes += chan->cosa->rxsize;
762 netif_rx(chan->rx_skb);
763 chan->rx_skb = NULL;
764 return 0;
765}
766
767
768static int cosa_net_tx_done(struct channel_data *chan, int size)
769{
770 if (!chan->tx_skb) {
771 printk(KERN_WARNING "%s: tx_done with empty skb!\n",
772 chan->name);
773 chan->netdev->stats.tx_errors++;
774 chan->netdev->stats.tx_aborted_errors++;
775 return 1;
776 }
777 dev_kfree_skb_irq(chan->tx_skb);
778 chan->tx_skb = NULL;
779 chan->netdev->stats.tx_packets++;
780 chan->netdev->stats.tx_bytes += size;
781 netif_wake_queue(chan->netdev);
782 return 1;
783}
784
785
786
787static ssize_t cosa_read(struct file *file,
788 char __user *buf, size_t count, loff_t *ppos)
789{
790 DECLARE_WAITQUEUE(wait, current);
791 unsigned long flags;
792 struct channel_data *chan = file->private_data;
793 struct cosa_data *cosa = chan->cosa;
794 char *kbuf;
795
796 if (!(cosa->firmware_status & COSA_FW_START)) {
797 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
798 cosa->name, cosa->firmware_status);
799 return -EPERM;
800 }
801 if (mutex_lock_interruptible(&chan->rlock))
802 return -ERESTARTSYS;
803
804 if ((chan->rxdata = kmalloc(COSA_MTU, GFP_DMA|GFP_KERNEL)) == NULL) {
805 printk(KERN_INFO "%s: cosa_read() - OOM\n", cosa->name);
806 mutex_unlock(&chan->rlock);
807 return -ENOMEM;
808 }
809
810 chan->rx_status = 0;
811 cosa_enable_rx(chan);
812 spin_lock_irqsave(&cosa->lock, flags);
813 add_wait_queue(&chan->rxwaitq, &wait);
814 while(!chan->rx_status) {
815 current->state = TASK_INTERRUPTIBLE;
816 spin_unlock_irqrestore(&cosa->lock, flags);
817 schedule();
818 spin_lock_irqsave(&cosa->lock, flags);
819 if (signal_pending(current) && chan->rx_status == 0) {
820 chan->rx_status = 1;
821 remove_wait_queue(&chan->rxwaitq, &wait);
822 current->state = TASK_RUNNING;
823 spin_unlock_irqrestore(&cosa->lock, flags);
824 mutex_unlock(&chan->rlock);
825 return -ERESTARTSYS;
826 }
827 }
828 remove_wait_queue(&chan->rxwaitq, &wait);
829 current->state = TASK_RUNNING;
830 kbuf = chan->rxdata;
831 count = chan->rxsize;
832 spin_unlock_irqrestore(&cosa->lock, flags);
833 mutex_unlock(&chan->rlock);
834
835 if (copy_to_user(buf, kbuf, count)) {
836 kfree(kbuf);
837 return -EFAULT;
838 }
839 kfree(kbuf);
840 return count;
841}
842
843static char *chrdev_setup_rx(struct channel_data *chan, int size)
844{
845
846 chan->rxsize = size;
847 return chan->rxdata;
848}
849
850static int chrdev_rx_done(struct channel_data *chan)
851{
852 if (chan->rx_status) {
853 kfree(chan->rxdata);
854 up(&chan->wsem);
855 }
856 chan->rx_status = 1;
857 wake_up_interruptible(&chan->rxwaitq);
858 return 1;
859}
860
861
862static ssize_t cosa_write(struct file *file,
863 const char __user *buf, size_t count, loff_t *ppos)
864{
865 DECLARE_WAITQUEUE(wait, current);
866 struct channel_data *chan = file->private_data;
867 struct cosa_data *cosa = chan->cosa;
868 unsigned long flags;
869 char *kbuf;
870
871 if (!(cosa->firmware_status & COSA_FW_START)) {
872 printk(KERN_NOTICE "%s: start the firmware first (status %d)\n",
873 cosa->name, cosa->firmware_status);
874 return -EPERM;
875 }
876 if (down_interruptible(&chan->wsem))
877 return -ERESTARTSYS;
878
879 if (count > COSA_MTU)
880 count = COSA_MTU;
881
882
883 if ((kbuf = kmalloc(count, GFP_KERNEL|GFP_DMA)) == NULL) {
884 printk(KERN_NOTICE "%s: cosa_write() OOM - dropping packet\n",
885 cosa->name);
886 up(&chan->wsem);
887 return -ENOMEM;
888 }
889 if (copy_from_user(kbuf, buf, count)) {
890 up(&chan->wsem);
891 kfree(kbuf);
892 return -EFAULT;
893 }
894 chan->tx_status=0;
895 cosa_start_tx(chan, kbuf, count);
896
897 spin_lock_irqsave(&cosa->lock, flags);
898 add_wait_queue(&chan->txwaitq, &wait);
899 while(!chan->tx_status) {
900 current->state = TASK_INTERRUPTIBLE;
901 spin_unlock_irqrestore(&cosa->lock, flags);
902 schedule();
903 spin_lock_irqsave(&cosa->lock, flags);
904 if (signal_pending(current) && chan->tx_status == 0) {
905 chan->tx_status = 1;
906 remove_wait_queue(&chan->txwaitq, &wait);
907 current->state = TASK_RUNNING;
908 chan->tx_status = 1;
909 spin_unlock_irqrestore(&cosa->lock, flags);
910 up(&chan->wsem);
911 return -ERESTARTSYS;
912 }
913 }
914 remove_wait_queue(&chan->txwaitq, &wait);
915 current->state = TASK_RUNNING;
916 up(&chan->wsem);
917 spin_unlock_irqrestore(&cosa->lock, flags);
918 kfree(kbuf);
919 return count;
920}
921
922static int chrdev_tx_done(struct channel_data *chan, int size)
923{
924 if (chan->tx_status) {
925 kfree(chan->txbuf);
926 up(&chan->wsem);
927 }
928 chan->tx_status = 1;
929 wake_up_interruptible(&chan->txwaitq);
930 return 1;
931}
932
933static unsigned int cosa_poll(struct file *file, poll_table *poll)
934{
935 printk(KERN_INFO "cosa_poll is here\n");
936 return 0;
937}
938
939static int cosa_open(struct inode *inode, struct file *file)
940{
941 struct cosa_data *cosa;
942 struct channel_data *chan;
943 unsigned long flags;
944 int n;
945 int ret = 0;
946
947 lock_kernel();
948 if ((n=iminor(file->f_path.dentry->d_inode)>>CARD_MINOR_BITS)
949 >= nr_cards) {
950 ret = -ENODEV;
951 goto out;
952 }
953 cosa = cosa_cards+n;
954
955 if ((n=iminor(file->f_path.dentry->d_inode)
956 & ((1<<CARD_MINOR_BITS)-1)) >= cosa->nchannels) {
957 ret = -ENODEV;
958 goto out;
959 }
960 chan = cosa->chan + n;
961
962 file->private_data = chan;
963
964 spin_lock_irqsave(&cosa->lock, flags);
965
966 if (chan->usage < 0) {
967 spin_unlock_irqrestore(&cosa->lock, flags);
968 ret = -EBUSY;
969 goto out;
970 }
971 cosa->usage++;
972 chan->usage++;
973
974 chan->tx_done = chrdev_tx_done;
975 chan->setup_rx = chrdev_setup_rx;
976 chan->rx_done = chrdev_rx_done;
977 spin_unlock_irqrestore(&cosa->lock, flags);
978out:
979 unlock_kernel();
980 return ret;
981}
982
983static int cosa_release(struct inode *inode, struct file *file)
984{
985 struct channel_data *channel = file->private_data;
986 struct cosa_data *cosa;
987 unsigned long flags;
988
989 cosa = channel->cosa;
990 spin_lock_irqsave(&cosa->lock, flags);
991 cosa->usage--;
992 channel->usage--;
993 spin_unlock_irqrestore(&cosa->lock, flags);
994 return 0;
995}
996
997#ifdef COSA_FASYNC_WORKING
998static struct fasync_struct *fasync[256] = { NULL, };
999
1000
1001static int cosa_fasync(struct inode *inode, struct file *file, int on)
1002{
1003 int port = iminor(inode);
1004
1005 return fasync_helper(inode, file, on, &fasync[port]);
1006}
1007#endif
1008
1009
1010
1011
1012
1013
1014
1015
1016static inline int cosa_reset(struct cosa_data *cosa)
1017{
1018 char idstring[COSA_MAX_ID_STRING];
1019 if (cosa->usage > 1)
1020 printk(KERN_INFO "cosa%d: WARNING: reset requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1021 cosa->num, cosa->usage);
1022 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_START);
1023 if (cosa_reset_and_read_id(cosa, idstring) < 0) {
1024 printk(KERN_NOTICE "cosa%d: reset failed\n", cosa->num);
1025 return -EIO;
1026 }
1027 printk(KERN_INFO "cosa%d: resetting device: %s\n", cosa->num,
1028 idstring);
1029 cosa->firmware_status |= COSA_FW_RESET;
1030 return 0;
1031}
1032
1033
1034static inline int cosa_download(struct cosa_data *cosa, void __user *arg)
1035{
1036 struct cosa_download d;
1037 int i;
1038
1039 if (cosa->usage > 1)
1040 printk(KERN_INFO "%s: WARNING: download of microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1041 cosa->name, cosa->usage);
1042 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1043 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1044 cosa->name, cosa->firmware_status);
1045 return -EPERM;
1046 }
1047
1048 if (copy_from_user(&d, arg, sizeof(d)))
1049 return -EFAULT;
1050
1051 if (d.addr < 0 || d.addr > COSA_MAX_FIRMWARE_SIZE)
1052 return -EINVAL;
1053 if (d.len < 0 || d.len > COSA_MAX_FIRMWARE_SIZE)
1054 return -EINVAL;
1055
1056
1057
1058 cosa->firmware_status &= ~(COSA_FW_RESET|COSA_FW_DOWNLOAD);
1059
1060 i = download(cosa, d.code, d.len, d.addr);
1061 if (i < 0) {
1062 printk(KERN_NOTICE "cosa%d: microcode download failed: %d\n",
1063 cosa->num, i);
1064 return -EIO;
1065 }
1066 printk(KERN_INFO "cosa%d: downloading microcode - 0x%04x bytes at 0x%04x\n",
1067 cosa->num, d.len, d.addr);
1068 cosa->firmware_status |= COSA_FW_RESET|COSA_FW_DOWNLOAD;
1069 return 0;
1070}
1071
1072
1073static inline int cosa_readmem(struct cosa_data *cosa, void __user *arg)
1074{
1075 struct cosa_download d;
1076 int i;
1077
1078 if (cosa->usage > 1)
1079 printk(KERN_INFO "cosa%d: WARNING: readmem requested with "
1080 "cosa->usage > 1 (%d). Odd things may happen.\n",
1081 cosa->num, cosa->usage);
1082 if (!(cosa->firmware_status & COSA_FW_RESET)) {
1083 printk(KERN_NOTICE "%s: reset the card first (status %d).\n",
1084 cosa->name, cosa->firmware_status);
1085 return -EPERM;
1086 }
1087
1088 if (copy_from_user(&d, arg, sizeof(d)))
1089 return -EFAULT;
1090
1091
1092 cosa->firmware_status &= ~COSA_FW_RESET;
1093
1094 i = readmem(cosa, d.code, d.len, d.addr);
1095 if (i < 0) {
1096 printk(KERN_NOTICE "cosa%d: reading memory failed: %d\n",
1097 cosa->num, i);
1098 return -EIO;
1099 }
1100 printk(KERN_INFO "cosa%d: reading card memory - 0x%04x bytes at 0x%04x\n",
1101 cosa->num, d.len, d.addr);
1102 cosa->firmware_status |= COSA_FW_RESET;
1103 return 0;
1104}
1105
1106
1107static inline int cosa_start(struct cosa_data *cosa, int address)
1108{
1109 int i;
1110
1111 if (cosa->usage > 1)
1112 printk(KERN_INFO "cosa%d: WARNING: start microcode requested with cosa->usage > 1 (%d). Odd things may happen.\n",
1113 cosa->num, cosa->usage);
1114
1115 if ((cosa->firmware_status & (COSA_FW_RESET|COSA_FW_DOWNLOAD))
1116 != (COSA_FW_RESET|COSA_FW_DOWNLOAD)) {
1117 printk(KERN_NOTICE "%s: download the microcode and/or reset the card first (status %d).\n",
1118 cosa->name, cosa->firmware_status);
1119 return -EPERM;
1120 }
1121 cosa->firmware_status &= ~COSA_FW_RESET;
1122 if ((i=startmicrocode(cosa, address)) < 0) {
1123 printk(KERN_NOTICE "cosa%d: start microcode at 0x%04x failed: %d\n",
1124 cosa->num, address, i);
1125 return -EIO;
1126 }
1127 printk(KERN_INFO "cosa%d: starting microcode at 0x%04x\n",
1128 cosa->num, address);
1129 cosa->startaddr = address;
1130 cosa->firmware_status |= COSA_FW_START;
1131 return 0;
1132}
1133
1134
1135static inline int cosa_getidstr(struct cosa_data *cosa, char __user *string)
1136{
1137 int l = strlen(cosa->id_string)+1;
1138 if (copy_to_user(string, cosa->id_string, l))
1139 return -EFAULT;
1140 return l;
1141}
1142
1143
1144static inline int cosa_gettype(struct cosa_data *cosa, char __user *string)
1145{
1146 int l = strlen(cosa->type)+1;
1147 if (copy_to_user(string, cosa->type, l))
1148 return -EFAULT;
1149 return l;
1150}
1151
1152static int cosa_ioctl_common(struct cosa_data *cosa,
1153 struct channel_data *channel, unsigned int cmd, unsigned long arg)
1154{
1155 void __user *argp = (void __user *)arg;
1156 switch(cmd) {
1157 case COSAIORSET:
1158 if (!capable(CAP_NET_ADMIN))
1159 return -EACCES;
1160 return cosa_reset(cosa);
1161 case COSAIOSTRT:
1162 if (!capable(CAP_SYS_RAWIO))
1163 return -EACCES;
1164 return cosa_start(cosa, arg);
1165 case COSAIODOWNLD:
1166 if (!capable(CAP_SYS_RAWIO))
1167 return -EACCES;
1168
1169 return cosa_download(cosa, argp);
1170 case COSAIORMEM:
1171 if (!capable(CAP_SYS_RAWIO))
1172 return -EACCES;
1173 return cosa_readmem(cosa, argp);
1174 case COSAIORTYPE:
1175 return cosa_gettype(cosa, argp);
1176 case COSAIORIDSTR:
1177 return cosa_getidstr(cosa, argp);
1178 case COSAIONRCARDS:
1179 return nr_cards;
1180 case COSAIONRCHANS:
1181 return cosa->nchannels;
1182 case COSAIOBMSET:
1183 if (!capable(CAP_SYS_RAWIO))
1184 return -EACCES;
1185 if (is_8bit(cosa))
1186 return -EINVAL;
1187 if (arg != COSA_BM_OFF && arg != COSA_BM_ON)
1188 return -EINVAL;
1189 cosa->busmaster = arg;
1190 return 0;
1191 case COSAIOBMGET:
1192 return cosa->busmaster;
1193 }
1194 return -ENOIOCTLCMD;
1195}
1196
1197static int cosa_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1198{
1199 int rv;
1200 struct channel_data *chan = dev_to_chan(dev);
1201 rv = cosa_ioctl_common(chan->cosa, chan, cmd,
1202 (unsigned long)ifr->ifr_data);
1203 if (rv != -ENOIOCTLCMD)
1204 return rv;
1205 return hdlc_ioctl(dev, ifr, cmd);
1206}
1207
1208static int cosa_chardev_ioctl(struct inode *inode, struct file *file,
1209 unsigned int cmd, unsigned long arg)
1210{
1211 struct channel_data *channel = file->private_data;
1212 struct cosa_data *cosa = channel->cosa;
1213 return cosa_ioctl_common(cosa, channel, cmd, arg);
1214}
1215
1216
1217
1218
1219
1220
1221
1222
1223static void cosa_enable_rx(struct channel_data *chan)
1224{
1225 struct cosa_data *cosa = chan->cosa;
1226
1227 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1228 put_driver_status(cosa);
1229}
1230
1231static void cosa_disable_rx(struct channel_data *chan)
1232{
1233 struct cosa_data *cosa = chan->cosa;
1234
1235 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1236 put_driver_status(cosa);
1237}
1238
1239
1240
1241
1242
1243
1244
1245static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1246{
1247 struct cosa_data *cosa = chan->cosa;
1248 unsigned long flags;
1249#ifdef DEBUG_DATA
1250 int i;
1251
1252 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1253 chan->num, len);
1254 for (i=0; i<len; i++)
1255 printk(" %02x", buf[i]&0xff);
1256 printk("\n");
1257#endif
1258 spin_lock_irqsave(&cosa->lock, flags);
1259 chan->txbuf = buf;
1260 chan->txsize = len;
1261 if (len > COSA_MTU)
1262 chan->txsize = COSA_MTU;
1263 spin_unlock_irqrestore(&cosa->lock, flags);
1264
1265
1266 set_bit(chan->num, &cosa->txbitmap);
1267 put_driver_status(cosa);
1268
1269 return 0;
1270}
1271
1272static void put_driver_status(struct cosa_data *cosa)
1273{
1274 unsigned long flags;
1275 int status;
1276
1277 spin_lock_irqsave(&cosa->lock, flags);
1278
1279 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1280 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1281 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1282 &DRIVER_TXMAP_MASK : 0);
1283 if (!cosa->rxtx) {
1284 if (cosa->rxbitmap|cosa->txbitmap) {
1285 if (!cosa->enabled) {
1286 cosa_putstatus(cosa, SR_RX_INT_ENA);
1287#ifdef DEBUG_IO
1288 debug_status_out(cosa, SR_RX_INT_ENA);
1289#endif
1290 cosa->enabled = 1;
1291 }
1292 } else if (cosa->enabled) {
1293 cosa->enabled = 0;
1294 cosa_putstatus(cosa, 0);
1295#ifdef DEBUG_IO
1296 debug_status_out(cosa, 0);
1297#endif
1298 }
1299 cosa_putdata8(cosa, status);
1300#ifdef DEBUG_IO
1301 debug_data_cmd(cosa, status);
1302#endif
1303 }
1304 spin_unlock_irqrestore(&cosa->lock, flags);
1305}
1306
1307static void put_driver_status_nolock(struct cosa_data *cosa)
1308{
1309 int status;
1310
1311 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1312 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1313 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1314 &DRIVER_TXMAP_MASK : 0);
1315
1316 if (cosa->rxbitmap|cosa->txbitmap) {
1317 cosa_putstatus(cosa, SR_RX_INT_ENA);
1318#ifdef DEBUG_IO
1319 debug_status_out(cosa, SR_RX_INT_ENA);
1320#endif
1321 cosa->enabled = 1;
1322 } else {
1323 cosa_putstatus(cosa, 0);
1324#ifdef DEBUG_IO
1325 debug_status_out(cosa, 0);
1326#endif
1327 cosa->enabled = 0;
1328 }
1329 cosa_putdata8(cosa, status);
1330#ifdef DEBUG_IO
1331 debug_data_cmd(cosa, status);
1332#endif
1333}
1334
1335
1336
1337
1338
1339
1340static void cosa_kick(struct cosa_data *cosa)
1341{
1342 unsigned long flags, flags1;
1343 char *s = "(probably) IRQ";
1344
1345 if (test_bit(RXBIT, &cosa->rxtx))
1346 s = "RX DMA";
1347 if (test_bit(TXBIT, &cosa->rxtx))
1348 s = "TX DMA";
1349
1350 printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
1351 spin_lock_irqsave(&cosa->lock, flags);
1352 cosa->rxtx = 0;
1353
1354 flags1 = claim_dma_lock();
1355 disable_dma(cosa->dma);
1356 clear_dma_ff(cosa->dma);
1357 release_dma_lock(flags1);
1358
1359
1360 udelay(100);
1361 cosa_putstatus(cosa, 0);
1362 udelay(100);
1363 (void) cosa_getdata8(cosa);
1364 udelay(100);
1365 cosa_putdata8(cosa, 0);
1366 udelay(100);
1367 put_driver_status_nolock(cosa);
1368 spin_unlock_irqrestore(&cosa->lock, flags);
1369}
1370
1371
1372
1373
1374
1375
1376static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1377{
1378 static int count;
1379 unsigned long b = (unsigned long)buf;
1380 if (b+len >= MAX_DMA_ADDRESS)
1381 return 0;
1382 if ((b^ (b+len)) & 0x10000) {
1383 if (count++ < 5)
1384 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1385 chan->name);
1386 return 0;
1387 }
1388 return 1;
1389}
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1403{
1404 int i;
1405
1406 if (put_wait_data(cosa, 'w') == -1) return -1;
1407 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1408 if (get_wait_data(cosa) != '=') return -3;
1409
1410 if (puthexnumber(cosa, address) < 0) return -4;
1411 if (put_wait_data(cosa, ' ') == -1) return -10;
1412 if (get_wait_data(cosa) != ' ') return -11;
1413 if (get_wait_data(cosa) != '=') return -12;
1414
1415 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1416 if (put_wait_data(cosa, ' ') == -1) return -18;
1417 if (get_wait_data(cosa) != ' ') return -19;
1418
1419 while (length--) {
1420 char c;
1421#ifndef SRP_DOWNLOAD_AT_BOOT
1422 if (get_user(c, microcode))
1423 return -23;
1424#else
1425 c = *microcode;
1426#endif
1427 if (put_wait_data(cosa, c) == -1)
1428 return -20;
1429 microcode++;
1430 }
1431
1432 if (get_wait_data(cosa) != '\r') return -21;
1433 if (get_wait_data(cosa) != '\n') return -22;
1434 if (get_wait_data(cosa) != '.') return -23;
1435#if 0
1436 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1437#endif
1438 return 0;
1439}
1440
1441
1442
1443
1444
1445
1446
1447static int startmicrocode(struct cosa_data *cosa, int address)
1448{
1449 if (put_wait_data(cosa, 'g') == -1) return -1;
1450 if (get_wait_data(cosa) != 'g') return -2;
1451 if (get_wait_data(cosa) != '=') return -3;
1452
1453 if (puthexnumber(cosa, address) < 0) return -4;
1454 if (put_wait_data(cosa, '\r') == -1) return -5;
1455
1456 if (get_wait_data(cosa) != '\r') return -6;
1457 if (get_wait_data(cosa) != '\r') return -7;
1458 if (get_wait_data(cosa) != '\n') return -8;
1459 if (get_wait_data(cosa) != '\r') return -9;
1460 if (get_wait_data(cosa) != '\n') return -10;
1461#if 0
1462 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1463#endif
1464 return 0;
1465}
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1477{
1478 if (put_wait_data(cosa, 'r') == -1) return -1;
1479 if ((get_wait_data(cosa)) != 'r') return -2;
1480 if ((get_wait_data(cosa)) != '=') return -3;
1481
1482 if (puthexnumber(cosa, address) < 0) return -4;
1483 if (put_wait_data(cosa, ' ') == -1) return -5;
1484 if (get_wait_data(cosa) != ' ') return -6;
1485 if (get_wait_data(cosa) != '=') return -7;
1486
1487 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1488 if (put_wait_data(cosa, ' ') == -1) return -9;
1489 if (get_wait_data(cosa) != ' ') return -10;
1490
1491 while (length--) {
1492 char c;
1493 int i;
1494 if ((i=get_wait_data(cosa)) == -1) {
1495 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1496 length);
1497 return -11;
1498 }
1499 c=i;
1500#if 1
1501 if (put_user(c, microcode))
1502 return -23;
1503#else
1504 *microcode = c;
1505#endif
1506 microcode++;
1507 }
1508
1509 if (get_wait_data(cosa) != '\r') return -21;
1510 if (get_wait_data(cosa) != '\n') return -22;
1511 if (get_wait_data(cosa) != '.') return -23;
1512#if 0
1513 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1514#endif
1515 return 0;
1516}
1517
1518
1519
1520
1521
1522static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1523{
1524 int i=0, id=0, prev=0, curr=0;
1525
1526
1527 cosa_putstatus(cosa, 0);
1528 cosa_getdata8(cosa);
1529 cosa_putstatus(cosa, SR_RST);
1530#ifdef MODULE
1531 msleep(500);
1532#else
1533 udelay(5*100000);
1534#endif
1535
1536 cosa_putstatus(cosa, 0);
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1547 if ((curr = get_wait_data(cosa)) == -1) {
1548 return -1;
1549 }
1550 curr &= 0xff;
1551 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1552 idstring[id++] = curr;
1553 if (curr == 0x2e && prev == '\n')
1554 break;
1555 }
1556
1557 idstring[id] = '\0';
1558 return id;
1559}
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569static int get_wait_data(struct cosa_data *cosa)
1570{
1571 int retries = 1000;
1572
1573 while (--retries) {
1574
1575 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1576 short r;
1577 r = cosa_getdata8(cosa);
1578#if 0
1579 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1580#endif
1581 return r;
1582 }
1583
1584 schedule_timeout_interruptible(1);
1585 }
1586 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1587 cosa_getstatus(cosa));
1588 return -1;
1589}
1590
1591
1592
1593
1594
1595
1596static int put_wait_data(struct cosa_data *cosa, int data)
1597{
1598 int retries = 1000;
1599 while (--retries) {
1600
1601 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1602 cosa_putdata8(cosa, data);
1603#if 0
1604 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1605#endif
1606 return 0;
1607 }
1608#if 0
1609
1610 schedule_timeout_interruptible(1);
1611#endif
1612 }
1613 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1614 cosa->num, cosa_getstatus(cosa));
1615 return -1;
1616}
1617
1618
1619
1620
1621
1622
1623
1624static int puthexnumber(struct cosa_data *cosa, int number)
1625{
1626 char temp[5];
1627 int i;
1628
1629
1630 sprintf(temp, "%04X", number);
1631 for (i=0; i<4; i++) {
1632 if (put_wait_data(cosa, temp[i]) == -1) {
1633 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1634 cosa->num, i);
1635 return -1-2*i;
1636 }
1637 if (get_wait_data(cosa) != temp[i]) {
1638 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1639 cosa->num, i);
1640 return -2-2*i;
1641 }
1642 }
1643 return 0;
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
1673
1674
1675
1676
1677
1678
1679
1680static inline void tx_interrupt(struct cosa_data *cosa, int status)
1681{
1682 unsigned long flags, flags1;
1683#ifdef DEBUG_IRQS
1684 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1685 cosa->num, status);
1686#endif
1687 spin_lock_irqsave(&cosa->lock, flags);
1688 set_bit(TXBIT, &cosa->rxtx);
1689 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1690
1691 int i=0;
1692 if (!cosa->txbitmap) {
1693 printk(KERN_WARNING "%s: No channel wants data "
1694 "in TX IRQ. Expect DMA timeout.",
1695 cosa->name);
1696 put_driver_status_nolock(cosa);
1697 clear_bit(TXBIT, &cosa->rxtx);
1698 spin_unlock_irqrestore(&cosa->lock, flags);
1699 return;
1700 }
1701 while(1) {
1702 cosa->txchan++;
1703 i++;
1704 if (cosa->txchan >= cosa->nchannels)
1705 cosa->txchan = 0;
1706 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1707 continue;
1708 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1709 break;
1710
1711 if (i > cosa->nchannels) {
1712
1713#ifdef DEBUG_IRQS
1714 printk(KERN_DEBUG "%s: Forcing TX "
1715 "to not-ready channel %d\n",
1716 cosa->name, cosa->txchan);
1717#endif
1718 break;
1719 }
1720 }
1721
1722 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1723 if (cosa_dma_able(cosa->chan+cosa->txchan,
1724 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1725 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1726 } else {
1727 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1728 cosa->txsize);
1729 cosa->txbuf = cosa->bouncebuf;
1730 }
1731 }
1732
1733 if (is_8bit(cosa)) {
1734 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1735 cosa_putstatus(cosa, SR_TX_INT_ENA);
1736 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1737 ((cosa->txsize >> 8) & 0x1f));
1738#ifdef DEBUG_IO
1739 debug_status_out(cosa, SR_TX_INT_ENA);
1740 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1741 ((cosa->txsize >> 8) & 0x1f));
1742 debug_data_in(cosa, cosa_getdata8(cosa));
1743#else
1744 cosa_getdata8(cosa);
1745#endif
1746 set_bit(IRQBIT, &cosa->rxtx);
1747 spin_unlock_irqrestore(&cosa->lock, flags);
1748 return;
1749 } else {
1750 clear_bit(IRQBIT, &cosa->rxtx);
1751 cosa_putstatus(cosa, 0);
1752 cosa_putdata8(cosa, cosa->txsize&0xff);
1753#ifdef DEBUG_IO
1754 debug_status_out(cosa, 0);
1755 debug_data_out(cosa, cosa->txsize&0xff);
1756#endif
1757 }
1758 } else {
1759 cosa_putstatus(cosa, SR_TX_INT_ENA);
1760 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1761 | (cosa->txsize & 0x1fff));
1762#ifdef DEBUG_IO
1763 debug_status_out(cosa, SR_TX_INT_ENA);
1764 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1765 | (cosa->txsize & 0x1fff));
1766 debug_data_in(cosa, cosa_getdata8(cosa));
1767 debug_status_out(cosa, 0);
1768#else
1769 cosa_getdata8(cosa);
1770#endif
1771 cosa_putstatus(cosa, 0);
1772 }
1773
1774 if (cosa->busmaster) {
1775 unsigned long addr = virt_to_bus(cosa->txbuf);
1776 int count=0;
1777 printk(KERN_INFO "busmaster IRQ\n");
1778 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1779 count++;
1780 udelay(10);
1781 if (count > 1000) break;
1782 }
1783 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1784 printk(KERN_INFO "ready after %d loops\n", count);
1785 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1786
1787 count = 0;
1788 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1789 count++;
1790 if (count > 1000) break;
1791 udelay(10);
1792 }
1793 printk(KERN_INFO "ready after %d loops\n", count);
1794 cosa_putdata16(cosa, addr &0xffff);
1795 flags1 = claim_dma_lock();
1796 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1797 enable_dma(cosa->dma);
1798 release_dma_lock(flags1);
1799 } else {
1800
1801 flags1 = claim_dma_lock();
1802 disable_dma(cosa->dma);
1803 clear_dma_ff(cosa->dma);
1804 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1805 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1806 set_dma_count(cosa->dma, cosa->txsize);
1807 enable_dma(cosa->dma);
1808 release_dma_lock(flags1);
1809 }
1810 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1811#ifdef DEBUG_IO
1812 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1813#endif
1814 spin_unlock_irqrestore(&cosa->lock, flags);
1815}
1816
1817static inline void rx_interrupt(struct cosa_data *cosa, int status)
1818{
1819 unsigned long flags;
1820#ifdef DEBUG_IRQS
1821 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1822#endif
1823
1824 spin_lock_irqsave(&cosa->lock, flags);
1825 set_bit(RXBIT, &cosa->rxtx);
1826
1827 if (is_8bit(cosa)) {
1828 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1829 set_bit(IRQBIT, &cosa->rxtx);
1830 put_driver_status_nolock(cosa);
1831 cosa->rxsize = cosa_getdata8(cosa) <<8;
1832#ifdef DEBUG_IO
1833 debug_data_in(cosa, cosa->rxsize >> 8);
1834#endif
1835 spin_unlock_irqrestore(&cosa->lock, flags);
1836 return;
1837 } else {
1838 clear_bit(IRQBIT, &cosa->rxtx);
1839 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1840#ifdef DEBUG_IO
1841 debug_data_in(cosa, cosa->rxsize & 0xff);
1842#endif
1843#if 0
1844 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1845 cosa->num, cosa->rxsize);
1846#endif
1847 }
1848 } else {
1849 cosa->rxsize = cosa_getdata16(cosa);
1850#ifdef DEBUG_IO
1851 debug_data_in(cosa, cosa->rxsize);
1852#endif
1853#if 0
1854 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1855 cosa->num, cosa->rxsize);
1856#endif
1857 }
1858 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1859 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1860 cosa->name, cosa->rxsize);
1861 spin_unlock_irqrestore(&cosa->lock, flags);
1862 goto reject;
1863 }
1864 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1865 cosa->rxsize &= 0x1fff;
1866 spin_unlock_irqrestore(&cosa->lock, flags);
1867
1868 cosa->rxbuf = NULL;
1869 if (cosa->rxchan->setup_rx)
1870 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1871
1872 if (!cosa->rxbuf) {
1873reject:
1874 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1875 cosa->num, cosa->rxchan->num);
1876 cosa->rxbuf = cosa->bouncebuf;
1877 }
1878
1879
1880 flags = claim_dma_lock();
1881 disable_dma(cosa->dma);
1882 clear_dma_ff(cosa->dma);
1883 set_dma_mode(cosa->dma, DMA_MODE_READ);
1884 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1885 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1886 } else {
1887 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1888 }
1889 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1890 enable_dma(cosa->dma);
1891 release_dma_lock(flags);
1892 spin_lock_irqsave(&cosa->lock, flags);
1893 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1894 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1895 cosa_putdata8(cosa, DRIVER_RX_READY);
1896#ifdef DEBUG_IO
1897 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1898 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1899 debug_data_cmd(cosa, DRIVER_RX_READY);
1900#endif
1901 spin_unlock_irqrestore(&cosa->lock, flags);
1902}
1903
1904static inline void eot_interrupt(struct cosa_data *cosa, int status)
1905{
1906 unsigned long flags, flags1;
1907 spin_lock_irqsave(&cosa->lock, flags);
1908 flags1 = claim_dma_lock();
1909 disable_dma(cosa->dma);
1910 clear_dma_ff(cosa->dma);
1911 release_dma_lock(flags1);
1912 if (test_bit(TXBIT, &cosa->rxtx)) {
1913 struct channel_data *chan = cosa->chan+cosa->txchan;
1914 if (chan->tx_done)
1915 if (chan->tx_done(chan, cosa->txsize))
1916 clear_bit(chan->num, &cosa->txbitmap);
1917 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1918#ifdef DEBUG_DATA
1919 {
1920 int i;
1921 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1922 cosa->rxchan->num, cosa->rxsize);
1923 for (i=0; i<cosa->rxsize; i++)
1924 printk (" %02x", cosa->rxbuf[i]&0xff);
1925 printk("\n");
1926 }
1927#endif
1928
1929 if (cosa->rxbuf == cosa->bouncebuf)
1930 goto out;
1931 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1932 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1933 if (cosa->rxchan->rx_done)
1934 if (cosa->rxchan->rx_done(cosa->rxchan))
1935 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1936 } else {
1937 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1938 cosa->num);
1939 }
1940
1941
1942
1943
1944
1945
1946out:
1947 cosa->rxtx = 0;
1948 put_driver_status_nolock(cosa);
1949 spin_unlock_irqrestore(&cosa->lock, flags);
1950}
1951
1952static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1953{
1954 unsigned status;
1955 int count = 0;
1956 struct cosa_data *cosa = cosa_;
1957again:
1958 status = cosa_getstatus(cosa);
1959#ifdef DEBUG_IRQS
1960 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1961 status & 0xff);
1962#endif
1963#ifdef DEBUG_IO
1964 debug_status_in(cosa, status);
1965#endif
1966 switch (status & SR_CMD_FROM_SRP_MASK) {
1967 case SR_DOWN_REQUEST:
1968 tx_interrupt(cosa, status);
1969 break;
1970 case SR_UP_REQUEST:
1971 rx_interrupt(cosa, status);
1972 break;
1973 case SR_END_OF_TRANSFER:
1974 eot_interrupt(cosa, status);
1975 break;
1976 default:
1977
1978 if (count++ < 100) {
1979 udelay(100);
1980 goto again;
1981 }
1982 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1983 cosa->num, status & 0xff, count);
1984 }
1985#ifdef DEBUG_IRQS
1986 if (count)
1987 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1988 cosa->name, count);
1989 else
1990 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
1991#endif
1992 return IRQ_HANDLED;
1993}
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003#ifdef DEBUG_IO
2004static void debug_status_in(struct cosa_data *cosa, int status)
2005{
2006 char *s;
2007 switch(status & SR_CMD_FROM_SRP_MASK) {
2008 case SR_UP_REQUEST:
2009 s = "RX_REQ";
2010 break;
2011 case SR_DOWN_REQUEST:
2012 s = "TX_REQ";
2013 break;
2014 case SR_END_OF_TRANSFER:
2015 s = "ET_REQ";
2016 break;
2017 default:
2018 s = "NO_REQ";
2019 break;
2020 }
2021 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2022 cosa->name,
2023 status,
2024 status & SR_USR_RQ ? "USR_RQ|":"",
2025 status & SR_TX_RDY ? "TX_RDY|":"",
2026 status & SR_RX_RDY ? "RX_RDY|":"",
2027 s);
2028}
2029
2030static void debug_status_out(struct cosa_data *cosa, int status)
2031{
2032 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2033 cosa->name,
2034 status,
2035 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
2036 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
2037 status & SR_RST ? "RESET|":"",
2038 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2039 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
2040 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
2041}
2042
2043static void debug_data_in(struct cosa_data *cosa, int data)
2044{
2045 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2046}
2047
2048static void debug_data_out(struct cosa_data *cosa, int data)
2049{
2050 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2051}
2052
2053static void debug_data_cmd(struct cosa_data *cosa, int data)
2054{
2055 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2056 cosa->name, data,
2057 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2058 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2059}
2060#endif
2061
2062
2063