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