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 <asm/io.h>
93#include <asm/dma.h>
94#include <asm/byteorder.h>
95
96#undef COSA_SLOW_IO
97
98#include "cosa.h"
99
100
101#define COSA_MAX_ID_STRING 128
102
103
104#define COSA_MAX_NAME (sizeof("cosaXXXcXXX")+1)
105
106
107
108struct channel_data {
109 int usage;
110 int num;
111 struct cosa_data *cosa;
112 int txsize;
113 char *txbuf;
114 char name[COSA_MAX_NAME];
115
116
117
118 char *(*setup_rx)(struct channel_data *channel, int size);
119
120 int (*rx_done)(struct channel_data *channel);
121
122 int (*tx_done)(struct channel_data *channel, int size);
123
124
125 struct mutex rlock;
126 struct semaphore wsem;
127 char *rxdata;
128 int rxsize;
129 wait_queue_head_t txwaitq, rxwaitq;
130 int tx_status, rx_status;
131
132
133 struct net_device *netdev;
134 struct sk_buff *rx_skb, *tx_skb;
135};
136
137
138#define COSA_FW_RESET (1<<0)
139#define COSA_FW_DOWNLOAD (1<<1)
140#define COSA_FW_START (1<<2)
141
142struct cosa_data {
143 int num;
144 char name[COSA_MAX_NAME];
145 unsigned int datareg, statusreg;
146 unsigned short irq, dma;
147 unsigned short startaddr;
148 unsigned short busmaster;
149 int nchannels;
150 int driver_status;
151 int firmware_status;
152 unsigned long rxbitmap, txbitmap;
153 unsigned long rxtx;
154 int enabled;
155 int usage;
156 int txchan, txsize, rxsize;
157 struct channel_data *rxchan;
158 char *bouncebuf;
159 char *txbuf, *rxbuf;
160 struct channel_data *chan;
161 spinlock_t lock;
162 char id_string[COSA_MAX_ID_STRING];
163 char *type;
164};
165
166
167
168
169
170
171
172
173
174
175
176static DEFINE_MUTEX(cosa_chardev_mutex);
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 long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
301 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 .unlocked_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 sema_init(&chan->wsem, 1);
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 mutex_lock(&cosa_chardev_mutex);
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 mutex_unlock(&cosa_chardev_mutex);
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 long cosa_chardev_ioctl(struct file *file, unsigned int cmd,
1209 unsigned long arg)
1210{
1211 struct channel_data *channel = file->private_data;
1212 struct cosa_data *cosa;
1213 long ret;
1214
1215 mutex_lock(&cosa_chardev_mutex);
1216 cosa = channel->cosa;
1217 ret = cosa_ioctl_common(cosa, channel, cmd, arg);
1218 mutex_unlock(&cosa_chardev_mutex);
1219 return ret;
1220}
1221
1222
1223
1224
1225
1226
1227
1228
1229static void cosa_enable_rx(struct channel_data *chan)
1230{
1231 struct cosa_data *cosa = chan->cosa;
1232
1233 if (!test_and_set_bit(chan->num, &cosa->rxbitmap))
1234 put_driver_status(cosa);
1235}
1236
1237static void cosa_disable_rx(struct channel_data *chan)
1238{
1239 struct cosa_data *cosa = chan->cosa;
1240
1241 if (test_and_clear_bit(chan->num, &cosa->rxbitmap))
1242 put_driver_status(cosa);
1243}
1244
1245
1246
1247
1248
1249
1250
1251static int cosa_start_tx(struct channel_data *chan, char *buf, int len)
1252{
1253 struct cosa_data *cosa = chan->cosa;
1254 unsigned long flags;
1255#ifdef DEBUG_DATA
1256 int i;
1257
1258 printk(KERN_INFO "cosa%dc%d: starting tx(0x%x)", chan->cosa->num,
1259 chan->num, len);
1260 for (i=0; i<len; i++)
1261 printk(" %02x", buf[i]&0xff);
1262 printk("\n");
1263#endif
1264 spin_lock_irqsave(&cosa->lock, flags);
1265 chan->txbuf = buf;
1266 chan->txsize = len;
1267 if (len > COSA_MTU)
1268 chan->txsize = COSA_MTU;
1269 spin_unlock_irqrestore(&cosa->lock, flags);
1270
1271
1272 set_bit(chan->num, &cosa->txbitmap);
1273 put_driver_status(cosa);
1274
1275 return 0;
1276}
1277
1278static void put_driver_status(struct cosa_data *cosa)
1279{
1280 unsigned long flags;
1281 int status;
1282
1283 spin_lock_irqsave(&cosa->lock, flags);
1284
1285 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1286 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1287 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1288 &DRIVER_TXMAP_MASK : 0);
1289 if (!cosa->rxtx) {
1290 if (cosa->rxbitmap|cosa->txbitmap) {
1291 if (!cosa->enabled) {
1292 cosa_putstatus(cosa, SR_RX_INT_ENA);
1293#ifdef DEBUG_IO
1294 debug_status_out(cosa, SR_RX_INT_ENA);
1295#endif
1296 cosa->enabled = 1;
1297 }
1298 } else if (cosa->enabled) {
1299 cosa->enabled = 0;
1300 cosa_putstatus(cosa, 0);
1301#ifdef DEBUG_IO
1302 debug_status_out(cosa, 0);
1303#endif
1304 }
1305 cosa_putdata8(cosa, status);
1306#ifdef DEBUG_IO
1307 debug_data_cmd(cosa, status);
1308#endif
1309 }
1310 spin_unlock_irqrestore(&cosa->lock, flags);
1311}
1312
1313static void put_driver_status_nolock(struct cosa_data *cosa)
1314{
1315 int status;
1316
1317 status = (cosa->rxbitmap ? DRIVER_RX_READY : 0)
1318 | (cosa->txbitmap ? DRIVER_TX_READY : 0)
1319 | (cosa->txbitmap? ~(cosa->txbitmap<<DRIVER_TXMAP_SHIFT)
1320 &DRIVER_TXMAP_MASK : 0);
1321
1322 if (cosa->rxbitmap|cosa->txbitmap) {
1323 cosa_putstatus(cosa, SR_RX_INT_ENA);
1324#ifdef DEBUG_IO
1325 debug_status_out(cosa, SR_RX_INT_ENA);
1326#endif
1327 cosa->enabled = 1;
1328 } else {
1329 cosa_putstatus(cosa, 0);
1330#ifdef DEBUG_IO
1331 debug_status_out(cosa, 0);
1332#endif
1333 cosa->enabled = 0;
1334 }
1335 cosa_putdata8(cosa, status);
1336#ifdef DEBUG_IO
1337 debug_data_cmd(cosa, status);
1338#endif
1339}
1340
1341
1342
1343
1344
1345
1346static void cosa_kick(struct cosa_data *cosa)
1347{
1348 unsigned long flags, flags1;
1349 char *s = "(probably) IRQ";
1350
1351 if (test_bit(RXBIT, &cosa->rxtx))
1352 s = "RX DMA";
1353 if (test_bit(TXBIT, &cosa->rxtx))
1354 s = "TX DMA";
1355
1356 printk(KERN_INFO "%s: %s timeout - restarting.\n", cosa->name, s);
1357 spin_lock_irqsave(&cosa->lock, flags);
1358 cosa->rxtx = 0;
1359
1360 flags1 = claim_dma_lock();
1361 disable_dma(cosa->dma);
1362 clear_dma_ff(cosa->dma);
1363 release_dma_lock(flags1);
1364
1365
1366 udelay(100);
1367 cosa_putstatus(cosa, 0);
1368 udelay(100);
1369 (void) cosa_getdata8(cosa);
1370 udelay(100);
1371 cosa_putdata8(cosa, 0);
1372 udelay(100);
1373 put_driver_status_nolock(cosa);
1374 spin_unlock_irqrestore(&cosa->lock, flags);
1375}
1376
1377
1378
1379
1380
1381
1382static int cosa_dma_able(struct channel_data *chan, char *buf, int len)
1383{
1384 static int count;
1385 unsigned long b = (unsigned long)buf;
1386 if (b+len >= MAX_DMA_ADDRESS)
1387 return 0;
1388 if ((b^ (b+len)) & 0x10000) {
1389 if (count++ < 5)
1390 printk(KERN_INFO "%s: packet spanning a 64k boundary\n",
1391 chan->name);
1392 return 0;
1393 }
1394 return 1;
1395}
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408static int download(struct cosa_data *cosa, const char __user *microcode, int length, int address)
1409{
1410 int i;
1411
1412 if (put_wait_data(cosa, 'w') == -1) return -1;
1413 if ((i=get_wait_data(cosa)) != 'w') { printk("dnld: 0x%04x\n",i); return -2;}
1414 if (get_wait_data(cosa) != '=') return -3;
1415
1416 if (puthexnumber(cosa, address) < 0) return -4;
1417 if (put_wait_data(cosa, ' ') == -1) return -10;
1418 if (get_wait_data(cosa) != ' ') return -11;
1419 if (get_wait_data(cosa) != '=') return -12;
1420
1421 if (puthexnumber(cosa, address+length-1) < 0) return -13;
1422 if (put_wait_data(cosa, ' ') == -1) return -18;
1423 if (get_wait_data(cosa) != ' ') return -19;
1424
1425 while (length--) {
1426 char c;
1427#ifndef SRP_DOWNLOAD_AT_BOOT
1428 if (get_user(c, microcode))
1429 return -23;
1430#else
1431 c = *microcode;
1432#endif
1433 if (put_wait_data(cosa, c) == -1)
1434 return -20;
1435 microcode++;
1436 }
1437
1438 if (get_wait_data(cosa) != '\r') return -21;
1439 if (get_wait_data(cosa) != '\n') return -22;
1440 if (get_wait_data(cosa) != '.') return -23;
1441#if 0
1442 printk(KERN_DEBUG "cosa%d: download completed.\n", cosa->num);
1443#endif
1444 return 0;
1445}
1446
1447
1448
1449
1450
1451
1452
1453static int startmicrocode(struct cosa_data *cosa, int address)
1454{
1455 if (put_wait_data(cosa, 'g') == -1) return -1;
1456 if (get_wait_data(cosa) != 'g') return -2;
1457 if (get_wait_data(cosa) != '=') return -3;
1458
1459 if (puthexnumber(cosa, address) < 0) return -4;
1460 if (put_wait_data(cosa, '\r') == -1) return -5;
1461
1462 if (get_wait_data(cosa) != '\r') return -6;
1463 if (get_wait_data(cosa) != '\r') return -7;
1464 if (get_wait_data(cosa) != '\n') return -8;
1465 if (get_wait_data(cosa) != '\r') return -9;
1466 if (get_wait_data(cosa) != '\n') return -10;
1467#if 0
1468 printk(KERN_DEBUG "cosa%d: microcode started\n", cosa->num);
1469#endif
1470 return 0;
1471}
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482static int readmem(struct cosa_data *cosa, char __user *microcode, int length, int address)
1483{
1484 if (put_wait_data(cosa, 'r') == -1) return -1;
1485 if ((get_wait_data(cosa)) != 'r') return -2;
1486 if ((get_wait_data(cosa)) != '=') return -3;
1487
1488 if (puthexnumber(cosa, address) < 0) return -4;
1489 if (put_wait_data(cosa, ' ') == -1) return -5;
1490 if (get_wait_data(cosa) != ' ') return -6;
1491 if (get_wait_data(cosa) != '=') return -7;
1492
1493 if (puthexnumber(cosa, address+length-1) < 0) return -8;
1494 if (put_wait_data(cosa, ' ') == -1) return -9;
1495 if (get_wait_data(cosa) != ' ') return -10;
1496
1497 while (length--) {
1498 char c;
1499 int i;
1500 if ((i=get_wait_data(cosa)) == -1) {
1501 printk (KERN_INFO "cosa: 0x%04x bytes remaining\n",
1502 length);
1503 return -11;
1504 }
1505 c=i;
1506#if 1
1507 if (put_user(c, microcode))
1508 return -23;
1509#else
1510 *microcode = c;
1511#endif
1512 microcode++;
1513 }
1514
1515 if (get_wait_data(cosa) != '\r') return -21;
1516 if (get_wait_data(cosa) != '\n') return -22;
1517 if (get_wait_data(cosa) != '.') return -23;
1518#if 0
1519 printk(KERN_DEBUG "cosa%d: readmem completed.\n", cosa->num);
1520#endif
1521 return 0;
1522}
1523
1524
1525
1526
1527
1528static int cosa_reset_and_read_id(struct cosa_data *cosa, char *idstring)
1529{
1530 int i=0, id=0, prev=0, curr=0;
1531
1532
1533 cosa_putstatus(cosa, 0);
1534 cosa_getdata8(cosa);
1535 cosa_putstatus(cosa, SR_RST);
1536#ifdef MODULE
1537 msleep(500);
1538#else
1539 udelay(5*100000);
1540#endif
1541
1542 cosa_putstatus(cosa, 0);
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552 for (i=0; i<COSA_MAX_ID_STRING-1; i++, prev=curr) {
1553 if ((curr = get_wait_data(cosa)) == -1) {
1554 return -1;
1555 }
1556 curr &= 0xff;
1557 if (curr != '\r' && curr != '\n' && curr != 0x2e)
1558 idstring[id++] = curr;
1559 if (curr == 0x2e && prev == '\n')
1560 break;
1561 }
1562
1563 idstring[id] = '\0';
1564 return id;
1565}
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575static int get_wait_data(struct cosa_data *cosa)
1576{
1577 int retries = 1000;
1578
1579 while (--retries) {
1580
1581 if (cosa_getstatus(cosa) & SR_RX_RDY) {
1582 short r;
1583 r = cosa_getdata8(cosa);
1584#if 0
1585 printk(KERN_INFO "cosa: get_wait_data returning after %d retries\n", 999-retries);
1586#endif
1587 return r;
1588 }
1589
1590 schedule_timeout_interruptible(1);
1591 }
1592 printk(KERN_INFO "cosa: timeout in get_wait_data (status 0x%x)\n",
1593 cosa_getstatus(cosa));
1594 return -1;
1595}
1596
1597
1598
1599
1600
1601
1602static int put_wait_data(struct cosa_data *cosa, int data)
1603{
1604 int retries = 1000;
1605 while (--retries) {
1606
1607 if (cosa_getstatus(cosa) & SR_TX_RDY) {
1608 cosa_putdata8(cosa, data);
1609#if 0
1610 printk(KERN_INFO "Putdata: %d retries\n", 999-retries);
1611#endif
1612 return 0;
1613 }
1614#if 0
1615
1616 schedule_timeout_interruptible(1);
1617#endif
1618 }
1619 printk(KERN_INFO "cosa%d: timeout in put_wait_data (status 0x%x)\n",
1620 cosa->num, cosa_getstatus(cosa));
1621 return -1;
1622}
1623
1624
1625
1626
1627
1628
1629
1630static int puthexnumber(struct cosa_data *cosa, int number)
1631{
1632 char temp[5];
1633 int i;
1634
1635
1636 sprintf(temp, "%04X", number);
1637 for (i=0; i<4; i++) {
1638 if (put_wait_data(cosa, temp[i]) == -1) {
1639 printk(KERN_NOTICE "cosa%d: puthexnumber failed to write byte %d\n",
1640 cosa->num, i);
1641 return -1-2*i;
1642 }
1643 if (get_wait_data(cosa) != temp[i]) {
1644 printk(KERN_NOTICE "cosa%d: puthexhumber failed to read echo of byte %d\n",
1645 cosa->num, i);
1646 return -2-2*i;
1647 }
1648 }
1649 return 0;
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
1680
1681
1682
1683
1684
1685
1686static inline void tx_interrupt(struct cosa_data *cosa, int status)
1687{
1688 unsigned long flags, flags1;
1689#ifdef DEBUG_IRQS
1690 printk(KERN_INFO "cosa%d: SR_DOWN_REQUEST status=0x%04x\n",
1691 cosa->num, status);
1692#endif
1693 spin_lock_irqsave(&cosa->lock, flags);
1694 set_bit(TXBIT, &cosa->rxtx);
1695 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1696
1697 int i=0;
1698 if (!cosa->txbitmap) {
1699 printk(KERN_WARNING "%s: No channel wants data "
1700 "in TX IRQ. Expect DMA timeout.",
1701 cosa->name);
1702 put_driver_status_nolock(cosa);
1703 clear_bit(TXBIT, &cosa->rxtx);
1704 spin_unlock_irqrestore(&cosa->lock, flags);
1705 return;
1706 }
1707 while (1) {
1708 cosa->txchan++;
1709 i++;
1710 if (cosa->txchan >= cosa->nchannels)
1711 cosa->txchan = 0;
1712 if (!(cosa->txbitmap & (1<<cosa->txchan)))
1713 continue;
1714 if (~status & (1 << (cosa->txchan+DRIVER_TXMAP_SHIFT)))
1715 break;
1716
1717 if (i > cosa->nchannels) {
1718
1719#ifdef DEBUG_IRQS
1720 printk(KERN_DEBUG "%s: Forcing TX "
1721 "to not-ready channel %d\n",
1722 cosa->name, cosa->txchan);
1723#endif
1724 break;
1725 }
1726 }
1727
1728 cosa->txsize = cosa->chan[cosa->txchan].txsize;
1729 if (cosa_dma_able(cosa->chan+cosa->txchan,
1730 cosa->chan[cosa->txchan].txbuf, cosa->txsize)) {
1731 cosa->txbuf = cosa->chan[cosa->txchan].txbuf;
1732 } else {
1733 memcpy(cosa->bouncebuf, cosa->chan[cosa->txchan].txbuf,
1734 cosa->txsize);
1735 cosa->txbuf = cosa->bouncebuf;
1736 }
1737 }
1738
1739 if (is_8bit(cosa)) {
1740 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1741 cosa_putstatus(cosa, SR_TX_INT_ENA);
1742 cosa_putdata8(cosa, ((cosa->txchan << 5) & 0xe0)|
1743 ((cosa->txsize >> 8) & 0x1f));
1744#ifdef DEBUG_IO
1745 debug_status_out(cosa, SR_TX_INT_ENA);
1746 debug_data_out(cosa, ((cosa->txchan << 5) & 0xe0)|
1747 ((cosa->txsize >> 8) & 0x1f));
1748 debug_data_in(cosa, cosa_getdata8(cosa));
1749#else
1750 cosa_getdata8(cosa);
1751#endif
1752 set_bit(IRQBIT, &cosa->rxtx);
1753 spin_unlock_irqrestore(&cosa->lock, flags);
1754 return;
1755 } else {
1756 clear_bit(IRQBIT, &cosa->rxtx);
1757 cosa_putstatus(cosa, 0);
1758 cosa_putdata8(cosa, cosa->txsize&0xff);
1759#ifdef DEBUG_IO
1760 debug_status_out(cosa, 0);
1761 debug_data_out(cosa, cosa->txsize&0xff);
1762#endif
1763 }
1764 } else {
1765 cosa_putstatus(cosa, SR_TX_INT_ENA);
1766 cosa_putdata16(cosa, ((cosa->txchan<<13) & 0xe000)
1767 | (cosa->txsize & 0x1fff));
1768#ifdef DEBUG_IO
1769 debug_status_out(cosa, SR_TX_INT_ENA);
1770 debug_data_out(cosa, ((cosa->txchan<<13) & 0xe000)
1771 | (cosa->txsize & 0x1fff));
1772 debug_data_in(cosa, cosa_getdata8(cosa));
1773 debug_status_out(cosa, 0);
1774#else
1775 cosa_getdata8(cosa);
1776#endif
1777 cosa_putstatus(cosa, 0);
1778 }
1779
1780 if (cosa->busmaster) {
1781 unsigned long addr = virt_to_bus(cosa->txbuf);
1782 int count=0;
1783 printk(KERN_INFO "busmaster IRQ\n");
1784 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1785 count++;
1786 udelay(10);
1787 if (count > 1000) break;
1788 }
1789 printk(KERN_INFO "status %x\n", cosa_getstatus(cosa));
1790 printk(KERN_INFO "ready after %d loops\n", count);
1791 cosa_putdata16(cosa, (addr >> 16)&0xffff);
1792
1793 count = 0;
1794 while (!(cosa_getstatus(cosa)&SR_TX_RDY)) {
1795 count++;
1796 if (count > 1000) break;
1797 udelay(10);
1798 }
1799 printk(KERN_INFO "ready after %d loops\n", count);
1800 cosa_putdata16(cosa, addr &0xffff);
1801 flags1 = claim_dma_lock();
1802 set_dma_mode(cosa->dma, DMA_MODE_CASCADE);
1803 enable_dma(cosa->dma);
1804 release_dma_lock(flags1);
1805 } else {
1806
1807 flags1 = claim_dma_lock();
1808 disable_dma(cosa->dma);
1809 clear_dma_ff(cosa->dma);
1810 set_dma_mode(cosa->dma, DMA_MODE_WRITE);
1811 set_dma_addr(cosa->dma, virt_to_bus(cosa->txbuf));
1812 set_dma_count(cosa->dma, cosa->txsize);
1813 enable_dma(cosa->dma);
1814 release_dma_lock(flags1);
1815 }
1816 cosa_putstatus(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1817#ifdef DEBUG_IO
1818 debug_status_out(cosa, SR_TX_DMA_ENA|SR_USR_INT_ENA);
1819#endif
1820 spin_unlock_irqrestore(&cosa->lock, flags);
1821}
1822
1823static inline void rx_interrupt(struct cosa_data *cosa, int status)
1824{
1825 unsigned long flags;
1826#ifdef DEBUG_IRQS
1827 printk(KERN_INFO "cosa%d: SR_UP_REQUEST\n", cosa->num);
1828#endif
1829
1830 spin_lock_irqsave(&cosa->lock, flags);
1831 set_bit(RXBIT, &cosa->rxtx);
1832
1833 if (is_8bit(cosa)) {
1834 if (!test_bit(IRQBIT, &cosa->rxtx)) {
1835 set_bit(IRQBIT, &cosa->rxtx);
1836 put_driver_status_nolock(cosa);
1837 cosa->rxsize = cosa_getdata8(cosa) <<8;
1838#ifdef DEBUG_IO
1839 debug_data_in(cosa, cosa->rxsize >> 8);
1840#endif
1841 spin_unlock_irqrestore(&cosa->lock, flags);
1842 return;
1843 } else {
1844 clear_bit(IRQBIT, &cosa->rxtx);
1845 cosa->rxsize |= cosa_getdata8(cosa) & 0xff;
1846#ifdef DEBUG_IO
1847 debug_data_in(cosa, cosa->rxsize & 0xff);
1848#endif
1849#if 0
1850 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1851 cosa->num, cosa->rxsize);
1852#endif
1853 }
1854 } else {
1855 cosa->rxsize = cosa_getdata16(cosa);
1856#ifdef DEBUG_IO
1857 debug_data_in(cosa, cosa->rxsize);
1858#endif
1859#if 0
1860 printk(KERN_INFO "cosa%d: receive rxsize = (0x%04x).\n",
1861 cosa->num, cosa->rxsize);
1862#endif
1863 }
1864 if (((cosa->rxsize & 0xe000) >> 13) >= cosa->nchannels) {
1865 printk(KERN_WARNING "%s: rx for unknown channel (0x%04x)\n",
1866 cosa->name, cosa->rxsize);
1867 spin_unlock_irqrestore(&cosa->lock, flags);
1868 goto reject;
1869 }
1870 cosa->rxchan = cosa->chan + ((cosa->rxsize & 0xe000) >> 13);
1871 cosa->rxsize &= 0x1fff;
1872 spin_unlock_irqrestore(&cosa->lock, flags);
1873
1874 cosa->rxbuf = NULL;
1875 if (cosa->rxchan->setup_rx)
1876 cosa->rxbuf = cosa->rxchan->setup_rx(cosa->rxchan, cosa->rxsize);
1877
1878 if (!cosa->rxbuf) {
1879reject:
1880 printk(KERN_INFO "cosa%d: rejecting packet on channel %d\n",
1881 cosa->num, cosa->rxchan->num);
1882 cosa->rxbuf = cosa->bouncebuf;
1883 }
1884
1885
1886 flags = claim_dma_lock();
1887 disable_dma(cosa->dma);
1888 clear_dma_ff(cosa->dma);
1889 set_dma_mode(cosa->dma, DMA_MODE_READ);
1890 if (cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize & 0x1fff)) {
1891 set_dma_addr(cosa->dma, virt_to_bus(cosa->rxbuf));
1892 } else {
1893 set_dma_addr(cosa->dma, virt_to_bus(cosa->bouncebuf));
1894 }
1895 set_dma_count(cosa->dma, (cosa->rxsize&0x1fff));
1896 enable_dma(cosa->dma);
1897 release_dma_lock(flags);
1898 spin_lock_irqsave(&cosa->lock, flags);
1899 cosa_putstatus(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1900 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1901 cosa_putdata8(cosa, DRIVER_RX_READY);
1902#ifdef DEBUG_IO
1903 debug_status_out(cosa, SR_RX_DMA_ENA|SR_USR_INT_ENA);
1904 if (!is_8bit(cosa) && (status & SR_TX_RDY))
1905 debug_data_cmd(cosa, DRIVER_RX_READY);
1906#endif
1907 spin_unlock_irqrestore(&cosa->lock, flags);
1908}
1909
1910static inline void eot_interrupt(struct cosa_data *cosa, int status)
1911{
1912 unsigned long flags, flags1;
1913 spin_lock_irqsave(&cosa->lock, flags);
1914 flags1 = claim_dma_lock();
1915 disable_dma(cosa->dma);
1916 clear_dma_ff(cosa->dma);
1917 release_dma_lock(flags1);
1918 if (test_bit(TXBIT, &cosa->rxtx)) {
1919 struct channel_data *chan = cosa->chan+cosa->txchan;
1920 if (chan->tx_done)
1921 if (chan->tx_done(chan, cosa->txsize))
1922 clear_bit(chan->num, &cosa->txbitmap);
1923 } else if (test_bit(RXBIT, &cosa->rxtx)) {
1924#ifdef DEBUG_DATA
1925 {
1926 int i;
1927 printk(KERN_INFO "cosa%dc%d: done rx(0x%x)", cosa->num,
1928 cosa->rxchan->num, cosa->rxsize);
1929 for (i=0; i<cosa->rxsize; i++)
1930 printk (" %02x", cosa->rxbuf[i]&0xff);
1931 printk("\n");
1932 }
1933#endif
1934
1935 if (cosa->rxbuf == cosa->bouncebuf)
1936 goto out;
1937 if (!cosa_dma_able(cosa->rxchan, cosa->rxbuf, cosa->rxsize))
1938 memcpy(cosa->rxbuf, cosa->bouncebuf, cosa->rxsize);
1939 if (cosa->rxchan->rx_done)
1940 if (cosa->rxchan->rx_done(cosa->rxchan))
1941 clear_bit(cosa->rxchan->num, &cosa->rxbitmap);
1942 } else {
1943 printk(KERN_NOTICE "cosa%d: unexpected EOT interrupt\n",
1944 cosa->num);
1945 }
1946
1947
1948
1949
1950
1951
1952out:
1953 cosa->rxtx = 0;
1954 put_driver_status_nolock(cosa);
1955 spin_unlock_irqrestore(&cosa->lock, flags);
1956}
1957
1958static irqreturn_t cosa_interrupt(int irq, void *cosa_)
1959{
1960 unsigned status;
1961 int count = 0;
1962 struct cosa_data *cosa = cosa_;
1963again:
1964 status = cosa_getstatus(cosa);
1965#ifdef DEBUG_IRQS
1966 printk(KERN_INFO "cosa%d: got IRQ, status 0x%02x\n", cosa->num,
1967 status & 0xff);
1968#endif
1969#ifdef DEBUG_IO
1970 debug_status_in(cosa, status);
1971#endif
1972 switch (status & SR_CMD_FROM_SRP_MASK) {
1973 case SR_DOWN_REQUEST:
1974 tx_interrupt(cosa, status);
1975 break;
1976 case SR_UP_REQUEST:
1977 rx_interrupt(cosa, status);
1978 break;
1979 case SR_END_OF_TRANSFER:
1980 eot_interrupt(cosa, status);
1981 break;
1982 default:
1983
1984 if (count++ < 100) {
1985 udelay(100);
1986 goto again;
1987 }
1988 printk(KERN_INFO "cosa%d: unknown status 0x%02x in IRQ after %d retries\n",
1989 cosa->num, status & 0xff, count);
1990 }
1991#ifdef DEBUG_IRQS
1992 if (count)
1993 printk(KERN_INFO "%s: %d-times got unknown status in IRQ\n",
1994 cosa->name, count);
1995 else
1996 printk(KERN_INFO "%s: returning from IRQ\n", cosa->name);
1997#endif
1998 return IRQ_HANDLED;
1999}
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009#ifdef DEBUG_IO
2010static void debug_status_in(struct cosa_data *cosa, int status)
2011{
2012 char *s;
2013 switch (status & SR_CMD_FROM_SRP_MASK) {
2014 case SR_UP_REQUEST:
2015 s = "RX_REQ";
2016 break;
2017 case SR_DOWN_REQUEST:
2018 s = "TX_REQ";
2019 break;
2020 case SR_END_OF_TRANSFER:
2021 s = "ET_REQ";
2022 break;
2023 default:
2024 s = "NO_REQ";
2025 break;
2026 }
2027 printk(KERN_INFO "%s: IO: status -> 0x%02x (%s%s%s%s)\n",
2028 cosa->name,
2029 status,
2030 status & SR_USR_RQ ? "USR_RQ|":"",
2031 status & SR_TX_RDY ? "TX_RDY|":"",
2032 status & SR_RX_RDY ? "RX_RDY|":"",
2033 s);
2034}
2035
2036static void debug_status_out(struct cosa_data *cosa, int status)
2037{
2038 printk(KERN_INFO "%s: IO: status <- 0x%02x (%s%s%s%s%s%s)\n",
2039 cosa->name,
2040 status,
2041 status & SR_RX_DMA_ENA ? "RXDMA|":"!rxdma|",
2042 status & SR_TX_DMA_ENA ? "TXDMA|":"!txdma|",
2043 status & SR_RST ? "RESET|":"",
2044 status & SR_USR_INT_ENA ? "USRINT|":"!usrint|",
2045 status & SR_TX_INT_ENA ? "TXINT|":"!txint|",
2046 status & SR_RX_INT_ENA ? "RXINT":"!rxint");
2047}
2048
2049static void debug_data_in(struct cosa_data *cosa, int data)
2050{
2051 printk(KERN_INFO "%s: IO: data -> 0x%04x\n", cosa->name, data);
2052}
2053
2054static void debug_data_out(struct cosa_data *cosa, int data)
2055{
2056 printk(KERN_INFO "%s: IO: data <- 0x%04x\n", cosa->name, data);
2057}
2058
2059static void debug_data_cmd(struct cosa_data *cosa, int data)
2060{
2061 printk(KERN_INFO "%s: IO: data <- 0x%04x (%s|%s)\n",
2062 cosa->name, data,
2063 data & SR_RDY_RCV ? "RX_RDY" : "!rx_rdy",
2064 data & SR_RDY_SND ? "TX_RDY" : "!tx_rdy");
2065}
2066#endif
2067
2068
2069