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