This source file includes following definitions.
- InReg
- OutReg
- wr
- or
- cl
- scc_alloc_buffer_pool
- scc_count_used_buffers
- scc_get_buffer
- scc_return_buffer
- scc_free_chain
- scc_append_to_chain
- scc_enqueue
- scc_isr_dispatch
- scc_isr
- prepare_next_txframe
- scc_txint
- scc_toss_buffer
- flush_FIFO
- scc_exint
- scc_rxint
- kick_rx_timer
- scc_spint
- set_brg
- set_speed
- init_brg
- init_channel
- scc_key_trx
- is_grouped
- dw_slot_timeout
- txdel_timeout
- tail_timeout
- busy_timeout
- maxk_idle_timeout
- check_rcv_queue
- scc_timer
- scc_init_timer
- scc_rx_timer
- kiss_set_param
- kiss_interpret_frame
- kiss_store_byte
- kiss_decode
- kiss_encode
- z8530_init
- scc_paranoia_check
- scc_open
- scc_close
- scc_change_speed
- scc_ioctl
- scc_set_termios
- check_tx_queue
- scc_write
- scc_put_char
- scc_flush_chars
- scc_write_room
- scc_chars_in_buffer
- scc_flush_buffer
- scc_throttle
- scc_unthrottle
- scc_start
- scc_stop
- scc_init
1 #include <linux/autoconf.h>
2 #ifdef CONFIG_SCC
3
4 #define RCS_ID "$Id: scc.c,v 1.25 1995/08/24 21:05:00 jreuter Exp jreuter $"
5
6 #define BANNER "Z8530 SCC driver v1.9.dl1bke (beta) by dl1bke\n"
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 #include <linux/errno.h>
125 #include <linux/signal.h>
126 #include <linux/sched.h>
127 #include <linux/timer.h>
128 #include <linux/tqueue.h>
129 #include <linux/tty.h>
130 #include <linux/tty_driver.h>
131 #include <linux/tty_flip.h>
132 #include <linux/major.h>
133 #include <linux/termios.h>
134 #include <linux/serial.h>
135 #include <linux/interrupt.h>
136 #include <linux/ioport.h>
137 #include <linux/config.h>
138 #include <linux/string.h>
139 #include <linux/fcntl.h>
140 #include <linux/ptrace.h>
141 #include <linux/malloc.h>
142 #include <linux/scc.h>
143 #include <linux/delay.h>
144 #include "scc_config.h"
145
146 #include <asm/system.h>
147 #include <asm/io.h>
148 #include <asm/segment.h>
149 #include <asm/bitops.h>
150
151 #include <stdlib.h>
152 #include <stdio.h>
153 #include <ctype.h>
154 #include <time.h>
155 #include <linux/kernel.h>
156
157 #ifndef Z8530_MAJOR
158 #define Z8530_MAJOR 22
159 #endif
160
161 long scc_init(long kmem_start);
162
163 int scc_open(struct tty_struct *tty, struct file *filp);
164 static void scc_close(struct tty_struct *tty, struct file *filp);
165 int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
166 static void scc_put_char(struct tty_struct *tty, unsigned char ch);
167 static void scc_flush_chars(struct tty_struct *tty);
168 static int scc_write_room(struct tty_struct *tty);
169 static int scc_chars_in_buffer(struct tty_struct *tty);
170 static void scc_flush_buffer(struct tty_struct *tty);
171 static int scc_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
172 static void scc_set_termios(struct tty_struct *tty, struct termios *old_termios);
173 static void scc_throttle(struct tty_struct *tty);
174 static void scc_unthrottle(struct tty_struct *tty);
175 static void scc_start(struct tty_struct *tty);
176 static void scc_stop(struct tty_struct *tty);
177
178 static void z8530_init(void);
179
180 static void scc_change_speed(struct scc_channel *scc);
181 static void kiss_encode(struct scc_channel *scc);
182
183 static void init_channel(struct scc_channel *scc);
184 static void scc_key_trx (struct scc_channel *scc, char tx);
185 static void scc_txint(register struct scc_channel *scc);
186 static void scc_exint(register struct scc_channel *scc);
187 static void scc_rxint(register struct scc_channel *scc);
188 static void scc_spint(register struct scc_channel *scc);
189 static void scc_isr(int irq, struct pt_regs *regs);
190 static void scc_timer(void);
191 static void scc_init_timer(struct scc_channel *scc);
192 static void scc_rx_timer(void);
193
194
195
196 static int baud_table[] = {
197 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
198 9600, 19200, 38400, 57600, 115200, 0 };
199
200
201 struct tty_driver scc_driver;
202 static int scc_refcount;
203 static struct tty_struct *scc_table[2*MAXSCC];
204 static struct termios scc_termios[2 * MAXSCC];
205 static struct termios scc_termios_locked[2 * MAXSCC];
206
207 struct scc_channel SCC_Info[2 * MAXSCC];
208
209 unsigned char Random = 0;
210 unsigned char Driver_Initialized = 0;
211 static struct sccbuf *sccfreelist[MAX_IBUFS] = {0};
212 static int allocated_ibufs = 0;
213
214 static struct rx_timer_CB rx_timer_cb;
215
216
217
218
219
220 static inline unsigned char
221 InReg(register io_port port, register unsigned char reg)
222 {
223 #ifdef SCC_LDELAY
224 register unsigned char r;
225 Outb(port, reg);
226 udelay(5);
227 r=Inb(port);
228 udelay(5);
229 return r;
230 #else
231 Outb(port, reg);
232 return Inb(port);
233 #endif
234 }
235
236 static inline void
237 OutReg(register io_port port, register unsigned char reg, register unsigned char val)
238 {
239 #ifdef SCC_LDELAY
240 Outb(port, reg); udelay(5);
241 Outb(port, val); udelay(5);
242 #else
243 Outb(port, reg);
244 Outb(port, val);
245 #endif
246 }
247
248 static inline void
249 wr(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
250 {
251 OutReg(scc->ctrl, reg, (scc->wreg[reg] = val));
252 }
253
254 static inline void
255 or(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
256 {
257 OutReg(scc->ctrl, reg, (scc->wreg[reg] |= val));
258 }
259
260 static inline void
261 cl(register struct scc_channel *scc, register unsigned char reg, register unsigned char val)
262 {
263 OutReg(scc->ctrl, reg, (scc->wreg[reg] &= ~val));
264 }
265
266
267
268
269
270
271
272
273
274
275
276 void scc_alloc_buffer_pool(void)
277 {
278 int i;
279 struct sccbuf *sccb;
280 struct mbuf *bp;
281
282 for (i = 0 ; i < MAX_IBUFS ; i++)
283 {
284 sccb = (struct sccbuf *)kmalloc(sizeof(struct sccbuf), GFP_ATOMIC);
285 bp = (struct mbuf *)kmalloc(sizeof(struct mbuf), GFP_ATOMIC);
286
287 if ( !(sccb && bp) )
288 {
289 allocated_ibufs = --i;
290
291 if (allocated_ibufs < 0)
292 panic("scc_alloc_buffer_pool() - can't get buffer space");
293 else
294 printk("Warning: scc_alloc_buffer_pool() - allocated only %i buffers\n",i);
295
296 return;
297 }
298
299 sccfreelist[i] = sccb;
300 sccfreelist[i]->bp = bp;
301 memset(sccfreelist[i]->bp ,0,sizeof(struct mbuf));
302 sccfreelist[i]->inuse = 0;
303 sccfreelist[i]->bp->type = 0;
304 sccfreelist[i]->bp->refcnt = 0;
305 sccfreelist[i]->bp->size = BUFSIZE;
306 }
307 allocated_ibufs = MAX_IBUFS;
308 }
309
310 unsigned int scc_count_used_buffers(unsigned int * rx, unsigned int * tx)
311 {
312 unsigned int i, used = 0;
313
314 if (rx) *rx = 0;
315 if (tx) *tx = 0;
316
317 for (i = 0 ; i < allocated_ibufs ; i++)
318 {
319 if (sccfreelist[i]->inuse)
320 {
321 switch (sccfreelist[i]->bp->type)
322 {
323 case BT_RECEIVE:
324 if (rx) (*rx)++; break;
325 case BT_TRANSMIT:
326 if (tx) (*tx)++; break;
327 }
328
329 used++;
330 }
331 }
332
333 return used;
334 }
335
336
337
338 struct mbuf *
339 scc_get_buffer(char type)
340 {
341 int i;
342 unsigned long flags;
343
344 save_flags(flags); cli();
345
346 for (i = 0 ; i < allocated_ibufs ; i++)
347 {
348 if(sccfreelist[i]->inuse == 0)
349 {
350 sccfreelist[i]->inuse = 1;
351 sccfreelist[i]->bp->type = type;
352 sccfreelist[i]->bp->next = NULLBUF;
353 sccfreelist[i]->bp->anext = NULLBUF;
354 sccfreelist[i]->bp->dup = NULLBUF;
355 sccfreelist[i]->bp->size = BUFSIZE;
356 sccfreelist[i]->bp->refcnt = 1;
357 sccfreelist[i]->bp->cnt = 0;
358 sccfreelist[i]->bp->in_use = 0;
359
360 restore_flags(flags);
361 return sccfreelist[i]->bp;
362 }
363 }
364
365 printk("\nSCC scc_get_buffer(): buffer pool empty\n");
366 restore_flags(flags);
367 return NULLBUF;
368 }
369
370
371
372
373
374
375 struct mbuf *
376 scc_return_buffer(register struct mbuf *bp, char type)
377 {
378 struct mbuf *bpnext;
379 int i;
380 unsigned long flags;
381
382 if(!bp)
383 return NULLBUF;
384
385 save_flags(flags); cli();
386 bpnext = bp->next;
387
388 if (bp->dup)
389 {
390 for(i = 0 ; i < allocated_ibufs ; i++)
391 {
392 if(sccfreelist[i]->bp == bp->dup)
393 {
394 if (sccfreelist[i]->bp->type != type)
395 {
396 printk("scc_return_buffer(bp->dup, %i): wrong buffer type %i",
397 type,sccfreelist[i]->bp->type);
398 }
399
400 sccfreelist[i]->bp->cnt = 0;
401 sccfreelist[i]->bp->refcnt = 0;
402 sccfreelist[i]->bp->in_use = 0;
403 sccfreelist[i]->inuse = 0;
404 bp->dup = NULLBUF;
405 }
406 }
407 }
408
409
410 if(--bp->refcnt <= 0)
411 {
412 for(i = 0 ; i < allocated_ibufs ; i++)
413 {
414 if(sccfreelist[i]->bp == bp)
415 {
416 if (sccfreelist[i]->bp->type != type)
417 {
418 printk("scc_return_buffer(bp, %i): wrong buffer type %i",
419 type,sccfreelist[i]->bp->type);
420 }
421
422 sccfreelist[i]->bp->cnt = 0;
423 sccfreelist[i]->bp->refcnt = 0;
424 sccfreelist[i]->inuse = 0;
425 restore_flags(flags);
426 return bpnext;
427 }
428 }
429 }
430
431 printk("\nscc_return_buffer(): bogus pointer %p\n",bp);
432 restore_flags(flags);
433 return bpnext;
434 }
435
436
437
438
439
440 struct mbuf *
441 scc_free_chain(register struct mbuf *bp, char type)
442 {
443 register struct mbuf *abp;
444 unsigned long flags;
445
446 if(!bp)
447 return NULLBUF;
448
449 save_flags(flags); cli();
450
451 abp = bp->anext;
452 while (bp) bp = scc_return_buffer(bp, type);
453
454 restore_flags(flags);
455 return abp;
456 }
457
458
459
460 void
461 scc_append_to_chain(struct mbuf **bph,struct mbuf *bp)
462 {
463 register struct mbuf *p;
464 unsigned long flags;
465
466 if(bph == NULLBUFP || bp == NULLBUF)
467 return;
468
469 save_flags(flags); cli();
470
471 if(*bph == NULLBUF)
472 {
473
474 *bph = bp;
475 } else {
476 for(p = *bph ; p->next != NULLBUF ; p = p->next)
477 ;
478 p->next = bp;
479 }
480
481 restore_flags(flags);
482 }
483
484
485
486 void
487 scc_enqueue(struct mbuf **queue,struct mbuf *bp)
488 {
489 register struct mbuf *p;
490 unsigned long flags;
491
492 if(queue == NULLBUFP || bp == NULLBUF)
493 return;
494
495 save_flags(flags); cli();
496
497 if(*queue == NULLBUF)
498 {
499
500 *queue = bp;
501 } else {
502 for(p = *queue ; p->anext != NULLBUF ; p = p->anext)
503 ;
504 p->anext = bp;
505 }
506 restore_flags(flags);
507 }
508
509
510
511
512
513
514
515
516
517
518 static inline void
519 scc_isr_dispatch(register struct scc_channel *scc, register int vector)
520 {
521 switch (vector & VECTOR_MASK)
522 {
523 case TXINT: scc_txint(scc); break;
524 case EXINT: scc_exint(scc); break;
525 case RXINT: scc_rxint(scc); break;
526 case SPINT: scc_spint(scc); break;
527 default : printk("scc_isr(): unknown interrupt status (addr %4.4x, state %2.2x)\n",scc->ctrl,vector);
528 }
529 }
530
531
532
533
534
535
536
537
538
539
540 static void
541 scc_isr(int irq, struct pt_regs *regs)
542 {
543 register unsigned char vector;
544 register struct scc_channel *scc;
545 register io_port q;
546 register io_port *p;
547 register int k;
548
549
550 cli();
551
552 if (Vector_Latch)
553 {
554 while(1)
555 {
556 Outb(Vector_Latch, 0);
557
558
559 if((vector=Inb(Vector_Latch)) >= 16 * Nchips) break;
560
561
562
563
564
565
566 if (vector & 0x01) break;
567
568 scc=&SCC_Info[(((vector>>1)&0x7c)^0x04) >> 2];
569
570 if (!scc->tty) break;
571
572 scc_isr_dispatch(scc, vector);
573
574 Outb(scc->ctrl,0x38);
575 }
576
577 sti();
578 return;
579 }
580
581
582
583
584
585
586 k = 0;
587 p = SCC_ctrl;
588
589 while (k++ < Nchips)
590 {
591 if (!(q=*p++)) break;
592
593 Outb(q,3);
594
595 if (Inb(q))
596 {
597 if (!(q=*p++)) break;
598
599 Outb(q,2);
600 vector=Inb(q);
601
602
603
604
605
606
607 if (vector & 1) break;
608
609 scc = &SCC_Info[(((vector >> 1) & 0x7c) ^ 0x04) >> 2];
610
611 if (!scc->tty) break;
612
613
614
615 scc_isr_dispatch(scc, vector);
616
617 k = 0;
618 p = SCC_ctrl;
619
620 } else p++;
621 }
622
623 sti();
624 }
625
626
627
628
629
630
631
632 static inline void prepare_next_txframe(register struct scc_channel *scc)
633 {
634 if ((scc->tbp = scc->sndq))
635 {
636 scc->sndq = scc->sndq->anext;
637 scc->stat.tx_state = TXS_NEWFRAME;
638
639 } else {
640 scc->stat.tx_state = TXS_BUSY;
641 scc->t_tail = scc->kiss.tailtime;
642 }
643 }
644
645
646
647 static void
648 scc_txint(register struct scc_channel *scc)
649 {
650 register struct mbuf *bp;
651
652 scc->stat.txints++;
653
654 bp = scc->tbp;
655
656 while (bp && !bp->cnt)
657 bp = scc_return_buffer(bp, BT_TRANSMIT);
658
659 if (bp == NULLBUF)
660 {
661 if (--scc->stat.tx_queued < 0)
662 scc->stat.tx_queued = 0;
663
664 Outb(scc->ctrl,RES_Tx_P);
665 cl(scc,R10,ABUNDER);
666 prepare_next_txframe(scc);
667
668 } else {
669
670 if (scc->stat.tx_state == TXS_NEWFRAME)
671 {
672 Outb(scc->ctrl, RES_Tx_CRC);
673 or(scc,R10,ABUNDER);
674 Outb(scc->data,bp->data[bp->in_use++]);
675
676 if (!scc->enhanced)
677 Outb(scc->ctrl, RES_EOM_L);
678
679 scc->stat.tx_state = TXS_ACTIVE;
680 } else {
681 Outb(scc->data,bp->data[bp->in_use++]);
682 }
683
684 bp->cnt--;
685 scc->tbp=bp;
686 }
687 }
688
689
690
691 static inline void
692 scc_toss_buffer(register struct scc_channel *scc)
693 {
694 register struct mbuf *bp;
695
696 if((bp = scc->rbp) != NULLBUF)
697 {
698 scc_free_chain(bp->next, BT_RECEIVE);
699 bp->next = NULLBUF;
700 scc->rbp1 = bp;
701 bp->cnt = 0;
702 bp->in_use = 0;
703 }
704 }
705
706 static inline void
707 flush_FIFO(register struct scc_channel *scc)
708 {
709 register int k;
710
711 for (k=0; k<3; k++)
712 Inb(scc->data);
713
714 if(scc->rbp != NULLBUF)
715 {
716 if(scc->rbp->next != NULLBUF || scc->rbp->cnt > 0)
717 scc->stat.rxerrs++;
718
719 scc_toss_buffer(scc);
720 }
721 }
722
723
724
725
726 static void
727 scc_exint(register struct scc_channel *scc)
728 {
729 register unsigned char status,changes,chg_and_stat;
730
731 scc->stat.exints++;
732
733 status = InReg(scc->ctrl,R0);
734 changes = status ^ scc->status;
735 chg_and_stat = changes & status;
736
737
738
739 if (chg_and_stat & BRK_ABRT)
740 flush_FIFO(scc);
741
742
743
744
745
746 if(changes & DCD)
747 {
748 if(status & DCD)
749 {
750 if (scc->modem.clocksrc != CLK_EXTERNAL)
751 OutReg(scc->ctrl,R14,SEARCH|scc->wreg[R14]);
752
753 or(scc,R3,ENT_HM|RxENABLE);
754 } else {
755 cl(scc,R3,ENT_HM|RxENABLE);
756 flush_FIFO(scc);
757 }
758 }
759
760
761
762
763 if (chg_and_stat & CTS)
764 {
765 if (!Running(t_txdel) && scc->kiss.txdelay == 0)
766 scc->t_txdel = 0;
767
768 }
769
770 if ((scc->stat.tx_state == TXS_ACTIVE) && (status & TxEOM))
771 {
772 scc->stat.tx_under++;
773 Outb(scc->ctrl, RES_Tx_P);
774 Outb(scc->ctrl, RES_EXT_INT);
775 scc->t_maxk = 1;
776 scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
777
778 if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
779 or(scc,R10,ABUNDER);
780 }
781
782 if (status & ZCOUNT)
783 {
784 scc->stat.tx_under = 9999;
785 Outb(scc->ctrl, RES_Tx_P);
786 scc->t_maxk = 1;
787 scc->tbp = scc_free_chain(scc->tbp, BT_TRANSMIT);
788 if (--scc->stat.tx_queued < 0) scc->stat.tx_queued = 0;
789 scc->kiss.tx_inhibit = 1;
790 }
791
792
793 scc->status = status;
794 Outb(scc->ctrl,RES_EXT_INT);
795 }
796
797
798
799 static void
800 scc_rxint(register struct scc_channel *scc)
801 {
802 register struct mbuf *bp;
803
804 scc->stat.rxints++;
805
806 if( Running(t_maxk) && !(scc->kiss.fulldup))
807 {
808 Inb(scc->data);
809 or(scc,R3,ENT_HM);
810 return;
811 }
812
813 if ((bp = scc->rbp1) == NULLBUF || bp->cnt >= bp->size)
814 {
815 if (scc->rbp == NULLBUF)
816 {
817 if ((bp = scc_get_buffer(BT_RECEIVE)) != NULLBUF)
818 scc->rbp = scc->rbp1 = bp;
819
820 }
821 else if ((bp = scc_get_buffer(BT_RECEIVE)))
822 {
823 scc_append_to_chain(&scc->rbp, bp);
824 scc->rbp1 = bp;
825 }
826
827 if (bp == NULLBUF)
828 {
829 Inb(scc->data);
830 or(scc,R3,ENT_HM);
831 scc_toss_buffer(scc);
832 scc->stat.nospace++;
833 return;
834 }
835 }
836
837
838 bp->data[bp->cnt++] = Inb(scc->data);
839 }
840
841
842
843
844 static inline void
845 kick_rx_timer(register struct scc_channel *scc)
846 {
847 register unsigned long expires;
848
849 if (!rx_timer_cb.lock)
850 {
851 expires = timer_table[SCC_TIMER].expires - jiffies;
852
853 rx_timer_cb.expires = (expires > 1)? expires:1;
854 rx_timer_cb.scc = scc;
855 rx_timer_cb.lock = 1;
856
857 timer_table[SCC_TIMER].fn = scc_rx_timer;
858 timer_table[SCC_TIMER].expires = jiffies + 1;
859 timer_active |= 1 << SCC_TIMER;
860 }
861 }
862
863
864 static void
865 scc_spint(register struct scc_channel *scc)
866 {
867 register unsigned char status;
868 register struct mbuf *bp;
869
870 scc->stat.spints++;
871
872 status = InReg(scc->ctrl,R1);
873
874 Inb(scc->data);
875
876 if(status & Rx_OVR)
877 {
878 scc->stat.rx_over++;
879 or(scc,R3,ENT_HM);
880 scc_toss_buffer(scc);
881 }
882
883 if(status & END_FR && scc->rbp != NULLBUF)
884 {
885
886
887 if (!(status & CRC_ERR) && (status & 0xe) == RES8 && scc->rbp->cnt)
888 {
889
890
891 for (bp = scc->rbp; bp->next != NULLBUF; bp = bp->next) ;
892 bp->cnt--;
893
894 scc_enqueue(&scc->rcvq,scc->rbp);
895 scc->rbp = scc->rbp1 = NULLBUF;
896 scc->stat.rxframes++;
897 scc->stat.rx_queued++;
898 kick_rx_timer(scc);
899 } else {
900 scc_toss_buffer(scc);
901 scc->stat.rxerrs++;
902 }
903 }
904
905 Outb(scc->ctrl,ERR_RES);
906 }
907
908
909
910
911
912
913
914
915
916 static inline void set_brg(register struct scc_channel *scc, unsigned int tc)
917 {
918 unsigned long flags;
919
920 save_flags(flags); cli();
921
922 cl(scc,R14,BRENABL);
923 wr(scc,R12,tc & 255);
924 wr(scc,R13,tc >> 8);
925 or(scc,R14,BRENABL);
926
927 restore_flags(flags);
928 }
929
930 static inline void set_speed(register struct scc_channel *scc)
931 {
932 set_brg(scc, (unsigned) (Clock / (scc->modem.speed * 64)) - 2);
933 }
934
935
936
937
938 static inline void init_brg(register struct scc_channel *scc)
939 {
940 wr(scc, R14, BRSRC);
941 OutReg(scc->ctrl, R14, SSBR|scc->wreg[R14]);
942 OutReg(scc->ctrl, R14, SNRZI|scc->wreg[R14]);
943 }
944
945 static void
946 init_channel(register struct scc_channel *scc)
947 {
948 unsigned long flags;
949
950 save_flags(flags); cli();
951
952 wr(scc,R1,0);
953 wr(scc,R3,Rx8|RxCRC_ENAB);
954 wr(scc,R4,X1CLK|SDLC);
955 wr(scc,R5,Tx8|DTR|TxCRC_ENAB);
956 wr(scc,R6,0);
957 wr(scc,R7,FLAG);
958 wr(scc,R10,(scc->modem.nrz? NRZ : NRZI)|CRCPS|ABUNDER);
959 wr(scc,R14, 0);
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988 switch(scc->modem.clocksrc)
989 {
990 case CLK_DPLL:
991 wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
992 init_brg(scc);
993 break;
994
995 case CLK_DIVIDER:
996 wr(scc, R11, ((Board & BAYCOM)? TRxCDP : TRxCBR) | RCDPLL|TCRTxCP|TRxCOI);
997 init_brg(scc);
998 break;
999
1000 case CLK_EXTERNAL:
1001 wr(scc, R11, (Board & BAYCOM)? RCTRxCP|TCRTxCP : RCRTxCP|TCTRxCP);
1002 OutReg(scc->ctrl, R14, DISDPLL);
1003 break;
1004
1005 }
1006
1007
1008 wr(scc,R15,((Board & BAYCOM) ? 0 : CTSIE)|BRKIE|DCDIE|TxUIE);
1009
1010 if(scc->enhanced)
1011 {
1012 or(scc,R15,SHDLCE|FIFOE);
1013 wr(scc,R7,AUTOEOM);
1014 }
1015
1016 if((InReg(scc->ctrl,R0)) & DCD)
1017 {
1018 if (scc->modem.clocksrc != CLK_EXTERNAL)
1019 or(scc,R14, SEARCH);
1020
1021 or(scc,R3,ENT_HM|RxENABLE);
1022 }
1023
1024 Outb(scc->ctrl,RES_EXT_INT);
1025 Outb(scc->ctrl,RES_EXT_INT);
1026
1027 scc->status = InReg(scc->ctrl,R0);
1028
1029 or(scc,R1,INT_ALL_Rx|TxINT_ENAB|EXT_INT_ENAB);
1030 or(scc,R9,MIE);
1031
1032 restore_flags(flags);
1033
1034 set_speed(scc);
1035 }
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048 static void
1049 scc_key_trx(struct scc_channel *scc, char tx)
1050 {
1051 unsigned int time_const;
1052
1053 if (scc->modem.speed < baud_table[1])
1054 scc->modem.speed = 1200;
1055
1056 if (Board & PRIMUS)
1057 Outb(scc->ctrl + 4, Option | (tx? 0x80 : 0));
1058
1059 time_const = (unsigned) (Clock / (scc->modem.speed * (tx? 2:64))) - 2;
1060
1061 if (scc->modem.clocksrc == CLK_DPLL)
1062 {
1063 if (tx)
1064 {
1065 cl(scc,R3,RxENABLE|ENT_HM);
1066
1067 set_brg(scc, time_const);
1068
1069
1070 wr(scc, R11, RCDPLL|TCBR|TRxCOI|TRxCBR);
1071
1072 or(scc,R5,RTS|TxENAB);
1073 } else {
1074 cl(scc,R5,RTS|TxENAB);
1075
1076 set_brg(scc, time_const);
1077
1078
1079 wr(scc, R11, RCDPLL|TCDPLL|TRxCOI|TRxCDP);
1080
1081 or(scc,R3,RxENABLE|ENT_HM);
1082 }
1083 } else {
1084 if (tx)
1085 or(scc,R5,RTS|TxENAB);
1086 else
1087 cl(scc,R5,RTS|TxENAB);
1088 }
1089 }
1090
1091
1092
1093
1094 static unsigned char Rand = 17;
1095
1096 static inline int is_grouped(register struct scc_channel *scc)
1097 {
1098 int k;
1099 struct scc_channel *scc2;
1100 unsigned char grp1, grp2;
1101
1102 grp1 = scc->kiss.group;
1103
1104 for (k = 0; k < (Nchips * 2); k++)
1105 {
1106 scc2 = &SCC_Info[k];
1107 grp2 = scc2->kiss.group;
1108
1109 if (scc2 == scc || !(scc2->tty && grp2))
1110 return 0;
1111
1112 if ((grp1 & 0x3f) == (grp2 & 0x3f))
1113 {
1114 if ( (grp1 & TXGROUP) && (scc2->wreg[R5] & RTS) )
1115 return 1;
1116
1117 if ( (grp1 & RXGROUP) && (scc2->status & DCD) )
1118 return 1;
1119 }
1120 }
1121 return 0;
1122 }
1123
1124
1125 static inline void dw_slot_timeout(register struct scc_channel *scc)
1126 {
1127 scc->t_dwait = TIMER_STOPPED;
1128 scc->t_slot = TIMER_STOPPED;
1129
1130 if (!scc->kiss.fulldup)
1131 {
1132 Rand = Rand * 17 + 31;
1133
1134 if ( (scc->kiss.softdcd? !(scc->status & SYNC_HUNT):(scc->status & DCD)) || (scc->kiss.persist) < Rand || (scc->kiss.group && is_grouped(scc)) )
1135 {
1136 if (scc->t_mbusy == TIMER_STOPPED)
1137 scc->t_mbusy = TPS * scc->kiss.maxdefer;
1138
1139 scc->t_slot = scc->kiss.slottime;
1140 return ;
1141
1142 }
1143 }
1144
1145 if ( !(scc->wreg[R5] & RTS) )
1146 {
1147 scc->t_txdel = scc->kiss.txdelay;
1148 scc_key_trx(scc, TX_ON);
1149 } else {
1150 scc->t_txdel = 0;
1151 }
1152 }
1153
1154
1155
1156
1157 static inline void txdel_timeout(register struct scc_channel *scc)
1158 {
1159 scc->t_txdel = TIMER_STOPPED;
1160
1161 scc->t_maxk = TPS * scc->kiss.maxkeyup;
1162 prepare_next_txframe(scc);
1163
1164 if (scc->stat.tx_state != TXS_BUSY)
1165 scc_txint(scc);
1166 }
1167
1168
1169
1170 static inline void tail_timeout(register struct scc_channel *scc)
1171 {
1172 scc->t_tail = TIMER_STOPPED;
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182 if (scc->kiss.fulldup < 2)
1183 {
1184 if (scc->sndq)
1185 {
1186 scc->stat.tx_state = TXS_BUSY;
1187 scc->t_dwait = TPS * scc->kiss.mintime;
1188 }
1189
1190 scc->stat.tx_state = TXS_IDLE;
1191 scc->t_maxk = TIMER_STOPPED;
1192 scc_key_trx(scc, TX_OFF);
1193 return;
1194 }
1195
1196 if (scc->sndq)
1197 {
1198 scc->stat.tx_state = TXS_BUSY;
1199 scc->t_txdel = TPS * scc->kiss.waittime;
1200 }
1201 else
1202
1203 scc->t_idle = TPS * scc->kiss.idletime;
1204 }
1205
1206
1207 static inline void busy_timeout(register struct scc_channel *scc)
1208 {
1209 #ifdef THROW_AWAY_AFTER_BUSY_TIMEOUT
1210 register struct mbuf *bp;
1211
1212 bp = scc->sndq;
1213
1214 while (bp) bp = scc_free_chain(bp, BT_TRANSMIT);
1215
1216 scc->sndq = NULLBUF;
1217 scc->stat.tx_state = TXS_IDLE;
1218
1219 #else
1220 scc->t_txdel = scc->kiss.txdelay;
1221 #endif
1222 scc->t_mbusy = TIMER_STOPPED;
1223
1224 }
1225
1226
1227 static inline void maxk_idle_timeout(register struct scc_channel *scc)
1228 {
1229 scc->t_maxk = TIMER_STOPPED;
1230 scc->t_idle = TIMER_STOPPED;
1231
1232 scc->stat.tx_state = TXS_BUSY;
1233 scc->t_tail = scc->kiss.tailtime;
1234 }
1235
1236 static inline void check_rcv_queue(register struct scc_channel *scc)
1237 {
1238 register struct mbuf *bp;
1239
1240 if (scc->stat.rx_queued > QUEUE_THRES)
1241 {
1242 if (scc->rcvq == NULLBUF)
1243 {
1244 printk("z8530drv: Warning - scc->stat.rx_queued shows overflow"
1245 " (%d) but queue is empty\n", scc->stat.rx_queued);
1246
1247 scc->stat.rx_queued = 0;
1248 scc->stat.nospace = 12345;
1249 return;
1250 }
1251
1252 bp = scc->rcvq->anext;
1253
1254 while (bp && (scc->stat.rx_queued > QUEUE_HYST))
1255 {
1256 bp = scc_free_chain(bp, BT_RECEIVE);
1257 scc->stat.rx_queued--;
1258 scc->stat.nospace++;
1259 }
1260
1261 scc->rcvq->anext = bp;
1262 }
1263 }
1264
1265 static void
1266 scc_timer(void)
1267 {
1268 register struct scc_channel *scc;
1269 register int chan;
1270 unsigned long flags;
1271
1272
1273 for (chan = 0; chan < (Nchips * 2); chan++)
1274 {
1275 scc = &SCC_Info[chan];
1276
1277 if (scc->tty && scc->init)
1278 {
1279 kiss_encode(scc);
1280
1281 save_flags(flags); cli();
1282
1283 check_rcv_queue(scc);
1284
1285
1286
1287 if (Expired(t_dwait)) dw_slot_timeout(scc) ; else
1288 if (Expired(t_slot)) dw_slot_timeout(scc) ; else
1289 if (Expired(t_txdel)) txdel_timeout(scc) ; else
1290 if (Expired(t_tail)) tail_timeout(scc) ;
1291
1292
1293
1294 if (Expired(t_mbusy)) busy_timeout(scc);
1295 if (Expired(t_maxk)) maxk_idle_timeout(scc);
1296 if (Expired(t_idle)) maxk_idle_timeout(scc);
1297
1298 restore_flags(flags);
1299 }
1300 }
1301
1302 save_flags(flags); cli();
1303
1304 timer_table[SCC_TIMER].fn = scc_timer;
1305 timer_table[SCC_TIMER].expires = jiffies + HZ/TPS;
1306 timer_active |= 1 << SCC_TIMER;
1307
1308 restore_flags(flags);
1309 }
1310
1311
1312 static void
1313 scc_init_timer(struct scc_channel *scc)
1314 {
1315 unsigned long flags;
1316
1317 save_flags(flags); cli();
1318
1319 Stop_Timer(t_dwait);
1320 Stop_Timer(t_slot);
1321 Stop_Timer(t_txdel);
1322 Stop_Timer(t_tail);
1323 Stop_Timer(t_mbusy);
1324 Stop_Timer(t_maxk);
1325 Stop_Timer(t_idle);
1326 scc->stat.tx_state = TXS_IDLE;
1327
1328 restore_flags(flags);
1329 }
1330
1331
1332 static void
1333 scc_rx_timer(void)
1334 {
1335 unsigned long flags;
1336
1337 kiss_encode(rx_timer_cb.scc);
1338
1339 save_flags(flags); cli();
1340
1341 timer_table[SCC_TIMER].fn = scc_timer;
1342 timer_table[SCC_TIMER].expires = jiffies + rx_timer_cb.expires;
1343 timer_active |= 1 << SCC_TIMER;
1344
1345 rx_timer_cb.lock = 0;
1346
1347 restore_flags(flags);
1348 }
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360 static void
1361 kiss_set_param(struct scc_channel *scc,char cmd, unsigned int val)
1362 {
1363
1364 #define VAL val=val*TPS/100
1365 #define SVAL val? val:TIMER_STOPPED
1366
1367 switch(cmd){
1368 case PARAM_TXDELAY:
1369 scc->kiss.txdelay = VAL; break;
1370 case PARAM_PERSIST:
1371 scc->kiss.persist = val; break;
1372 case PARAM_SLOTTIME:
1373 scc->kiss.slottime = VAL; break;
1374 case PARAM_TXTAIL:
1375 scc->kiss.tailtime = VAL; break;
1376 case PARAM_FULLDUP:
1377 scc->kiss.fulldup = val; break;
1378 case PARAM_WAIT:
1379 scc->kiss.waittime = VAL; break;
1380 case PARAM_MAXKEY:
1381 scc->kiss.maxkeyup = SVAL; break;
1382 case PARAM_MIN:
1383 scc->kiss.mintime = SVAL; break;
1384 case PARAM_IDLE:
1385 scc->kiss.idletime = val; break;
1386 case PARAM_MAXDEFER:
1387 scc->kiss.maxdefer = SVAL; break;
1388 case PARAM_GROUP:
1389 scc->kiss.group = val; break;
1390 case PARAM_TX:
1391 scc->kiss.tx_inhibit = val;
1392 case PARAM_SOFTDCD:
1393 scc->kiss.softdcd = val;
1394 }
1395 return;
1396
1397 #undef VAL
1398 #undef SVAL
1399 }
1400
1401
1402
1403
1404 static void kiss_interpret_frame(struct scc_channel * scc)
1405 {
1406 unsigned char kisscmd;
1407 unsigned long flags;
1408
1409 if (scc->sndq1->cnt < 2)
1410 {
1411 if (scc->sndq1)
1412 scc_free_chain(scc->sndq1, BT_TRANSMIT);
1413 else
1414 scc->sndq1 = NULLBUF;
1415
1416 scc->sndq2 = NULLBUF;
1417 return;
1418 }
1419
1420
1421
1422 if (scc->kiss.not_slip)
1423 {
1424 kisscmd = scc->sndq1->data[scc->sndq1->in_use++];
1425 scc->sndq1->cnt--;
1426 } else {
1427 kisscmd = 0;
1428 }
1429
1430 if (kisscmd & 0xa0)
1431 {
1432 if (scc->sndq1->cnt > 2)
1433 scc->sndq1->cnt -= 2;
1434 else
1435 {
1436 scc_free_chain(scc->sndq1, BT_TRANSMIT);
1437 scc->sndq2 = NULLBUF;
1438 return;
1439 }
1440 }
1441
1442
1443 kisscmd &= 0x1f;
1444
1445
1446 if (kisscmd)
1447 {
1448 kiss_set_param(scc, kisscmd, scc->sndq1->data[scc->sndq1->in_use]);
1449 scc->sndq1->cnt=0;
1450 scc->sndq1->in_use=0;
1451
1452 scc_free_chain(scc->sndq1, BT_TRANSMIT);
1453 scc->sndq2 = NULLBUF;
1454 return;
1455 }
1456
1457 scc_enqueue(&scc->sndq,scc->sndq1);
1458 scc->stat.txframes++;
1459 scc->stat.tx_queued++;
1460 scc->sndq2 = NULLBUF;
1461
1462 save_flags(flags); cli();
1463
1464 if(scc->stat.tx_state == TXS_IDLE)
1465 {
1466 scc_init_timer(scc);
1467 scc->stat.tx_state = TXS_BUSY;
1468 scc->t_dwait = (scc->kiss.fulldup? 0:scc->kiss.waittime);
1469 }
1470
1471 restore_flags(flags);
1472 }
1473
1474 static void kiss_store_byte(struct scc_channel *scc, unsigned char ch)
1475 {
1476 if (scc->sndq2 == NULLBUF) return;
1477
1478 if(scc->sndq2->cnt == scc->sndq2->size)
1479 {
1480 if((scc->sndq2 = scc_get_buffer(BT_TRANSMIT)) == NULLBUF)
1481 {
1482 printk("\nsccdrv: running out of memory\n");
1483 return;
1484 }
1485 scc_append_to_chain(&scc->sndq1,scc->sndq2);
1486 }
1487
1488 scc->sndq2->data[scc->sndq2->cnt++] = ch;
1489 }
1490
1491 static inline int kiss_decode(struct scc_channel *scc, unsigned char ch)
1492 {
1493 switch (scc->stat.tx_kiss_state)
1494 {
1495 case KISS_IDLE:
1496 if (ch == FEND)
1497 {
1498 if (!(scc->sndq2 = scc->sndq1 = scc_get_buffer(BT_TRANSMIT)))
1499 return 0;
1500
1501 scc->stat.tx_kiss_state = KISS_DATA;
1502 } else scc->stat.txerrs++;
1503 break;
1504
1505 case KISS_DATA:
1506 if (ch == FESC)
1507 scc->stat.tx_kiss_state = KISS_ESCAPE;
1508 else if (ch == FEND)
1509 {
1510 kiss_interpret_frame(scc);
1511 scc->stat.tx_kiss_state = KISS_IDLE;
1512 }
1513 else kiss_store_byte(scc, ch);
1514 break;
1515
1516 case KISS_ESCAPE:
1517 if (ch == TFEND)
1518 {
1519 kiss_store_byte(scc, FEND);
1520 scc->stat.tx_kiss_state = KISS_DATA;
1521 }
1522 else if (ch == TFESC)
1523 {
1524 kiss_store_byte(scc, FESC);
1525 scc->stat.tx_kiss_state = KISS_DATA;
1526 }
1527 else
1528 {
1529 scc_free_chain(scc->sndq1, BT_TRANSMIT);
1530 scc->sndq2 = NULLBUF;
1531 scc->stat.txerrs++;
1532 scc->stat.tx_kiss_state = KISS_IDLE;
1533 }
1534 break;
1535 }
1536
1537 return 0;
1538
1539 }
1540
1541
1542
1543
1544 static void
1545 kiss_encode(register struct scc_channel *scc)
1546 {
1547 struct mbuf *bp,*bp2;
1548 struct tty_struct * tty = scc->tty;
1549 unsigned long flags;
1550 unsigned char ch;
1551
1552 if(!scc->rcvq)
1553 {
1554 scc->stat.rx_kiss_state = KISS_IDLE;
1555 return;
1556 }
1557
1558
1559
1560 while(tty->flip.count < TTY_FLIPBUF_SIZE-3)
1561 {
1562 if (scc->rcvq->cnt)
1563 {
1564 bp = scc->rcvq;
1565
1566 if (scc->stat.rx_kiss_state == KISS_IDLE)
1567 {
1568 tty_insert_flip_char(tty, FEND, 0);
1569
1570 if (scc->kiss.not_slip)
1571 tty_insert_flip_char(tty, 0, 0);
1572
1573 scc->stat.rx_kiss_state = KISS_RXFRAME;
1574 }
1575
1576 switch(ch = bp->data[bp->in_use++])
1577 {
1578 case FEND:
1579 tty_insert_flip_char(tty, FESC, 0);
1580 tty_insert_flip_char(tty, TFEND, 0);
1581 break;
1582 case FESC:
1583 tty_insert_flip_char(tty, FESC, 0);
1584 tty_insert_flip_char(tty, TFESC, 0);
1585 break;
1586 default:
1587 tty_insert_flip_char(tty, ch, 0);
1588 }
1589
1590 bp->cnt--;
1591
1592 } else {
1593 save_flags(flags); cli();
1594
1595 while (!scc->rcvq->cnt)
1596 {
1597 bp = scc->rcvq->next;
1598 bp2 = scc->rcvq->anext;
1599
1600
1601 scc_return_buffer(scc->rcvq, BT_RECEIVE);
1602
1603 if (!bp)
1604 {
1605 scc->rcvq = bp2;
1606
1607 if (--scc->stat.rx_queued < 0)
1608 scc->stat.rx_queued = 0;
1609
1610 if (scc->stat.rx_kiss_state == KISS_RXFRAME)
1611 {
1612 tty_insert_flip_char(tty, FEND, 0);
1613 scc->stat.rx_kiss_state = KISS_IDLE;
1614 }
1615
1616 restore_flags(flags);
1617 queue_task(&tty->flip.tqueue, &tq_timer);
1618 return;
1619
1620 } else scc->rcvq = bp;
1621 }
1622
1623 restore_flags(flags);
1624 }
1625
1626 }
1627
1628 queue_task(&tty->flip.tqueue, &tq_timer);
1629 }
1630
1631
1632
1633
1634
1635
1636
1637 static void
1638 z8530_init(void)
1639 {
1640 struct scc_channel *scc;
1641 int chip;
1642 unsigned long flags;
1643
1644
1645 for (chip = 0; chip < Nchips; chip++)
1646 {
1647
1648
1649 if(Board & EAGLE)
1650 Outb(Special_Port,0x08);
1651
1652 if(Board & (PC100 | PRIMUS))
1653 Outb(Special_Port,Option);
1654
1655
1656
1657 scc=&SCC_Info[2*chip];
1658 if (!scc->ctrl) continue;
1659
1660 save_flags(flags); cli();
1661
1662
1663
1664 Outb(scc->ctrl, 0);
1665 OutReg(scc->ctrl,R9,FHWRES);
1666 udelay(100);
1667 wr(scc, R2, chip*16);
1668 wr(scc, R9, VIS);
1669
1670 restore_flags(flags);
1671 }
1672
1673 if (Ivec == 2) Ivec = 9;
1674 request_irq(Ivec, scc_isr, SA_INTERRUPT, "AX.25 SCC");
1675
1676 Driver_Initialized = 1;
1677 }
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688 static inline int scc_paranoia_check(struct scc_channel *scc, dev_t device, const char *routine)
1689 {
1690 #ifdef SCC_PARANOIA_CHECK
1691 static const char *badmagic =
1692 "Warning: bad magic number for Z8530 SCC struct (%d, %d) in %s\n";
1693 static const char *badinfo =
1694 "Warning: Z8530 not found for (%d, %d) in %s\n";
1695
1696 if (!scc->init)
1697 {
1698 printk(badinfo, MAJOR(device), MINOR(device), routine);
1699 return 1;
1700 }
1701 if (scc->magic != SCC_MAGIC) {
1702 printk(badmagic, MAJOR(device), MINOR(device), routine);
1703 return 1;
1704 }
1705 #endif
1706
1707 return 0;
1708 }
1709
1710
1711
1712
1713 int scc_open(struct tty_struct *tty, struct file * filp)
1714 {
1715 struct scc_channel *scc;
1716 int chan;
1717
1718 chan = MINOR(tty->device) - tty->driver.minor_start;
1719 if ((chan < 0) || (chan >= (Nchips * 2)))
1720 return -ENODEV;
1721
1722 scc = &SCC_Info[chan];
1723
1724 tty->driver_data = scc;
1725 tty->termios->c_cflag &= ~CBAUD;
1726
1727 if (!Driver_Initialized)
1728 return 0;
1729
1730 if (scc->magic != SCC_MAGIC)
1731 {
1732 printk("ERROR: scc_open(): bad magic number for device (%d, %d)", MAJOR(tty->device), MINOR(tty->device));
1733 return -ENODEV;
1734 }
1735
1736 if(scc->tty != NULL)
1737 {
1738 scc->tty_opened++;
1739 return 0;
1740 }
1741
1742 if(!scc->init) return 0;
1743
1744 scc->tty = tty;
1745 init_channel(scc);
1746
1747 scc->stat.tx_kiss_state = KISS_IDLE;
1748 scc->stat.rx_kiss_state = KISS_IDLE;
1749
1750 scc_init_timer(scc);
1751
1752 timer_table[SCC_TIMER].fn = scc_timer;
1753 timer_table[SCC_TIMER].expires = 0;
1754 timer_active |= 1 << SCC_TIMER;
1755
1756 return 0;
1757 }
1758
1759
1760
1761
1762 static void
1763 scc_close(struct tty_struct *tty, struct file * filp)
1764 {
1765 struct scc_channel *scc = tty->driver_data;
1766 unsigned long flags;
1767
1768 if (!scc || (scc->magic != SCC_MAGIC))
1769 return;
1770
1771 if(scc->tty_opened)
1772 {
1773 scc->tty_opened--;
1774 return;
1775 }
1776
1777 tty->driver_data = NULLBUF;
1778
1779 save_flags(flags); cli();
1780
1781 Outb(scc->ctrl,0);
1782 wr(scc,R1,0);
1783 wr(scc,R3,0);
1784
1785 scc->tty = NULL;
1786
1787 restore_flags(flags);
1788 tty->stopped = 0;
1789 }
1790
1791
1792
1793
1794
1795
1796
1797
1798 static void
1799 scc_change_speed(struct scc_channel * scc)
1800 {
1801 if (scc->tty == NULL)
1802 return;
1803
1804 scc->modem.speed = baud_table[scc->tty->termios->c_cflag & CBAUD];
1805
1806 if (scc->stat.tx_state == 0)
1807 set_speed(scc);
1808 }
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829 static int
1830 scc_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
1831 {
1832 struct scc_channel * scc = tty->driver_data;
1833 unsigned long flags, r;
1834 unsigned int result;
1835 unsigned int value;
1836 struct ioctl_command kiss_cmd;
1837 int error;
1838
1839 if (scc->magic != SCC_MAGIC)
1840 {
1841 printk("ERROR: scc_ioctl(): bad magic number for device %d,%d",
1842 MAJOR(tty->device), MINOR(tty->device));
1843
1844 return -ENODEV;
1845 }
1846
1847 r = NO_SUCH_PARAM;
1848
1849 if (!Driver_Initialized)
1850 {
1851 if (cmd == TIOCSCCINI)
1852 {
1853 if (!suser())
1854 return -EPERM;
1855
1856 scc_alloc_buffer_pool();
1857 z8530_init();
1858 return 0;
1859 }
1860
1861 return -EINVAL;
1862 }
1863
1864 if (!scc->init)
1865 {
1866
1867 if (cmd == TIOCCHANINI)
1868 {
1869 if (!arg)
1870 return -EFAULT;
1871
1872 if (!suser())
1873 return -EPERM;
1874
1875 memcpy_fromfs(&scc->modem, (void *) arg, sizeof(struct scc_modem));
1876
1877
1878
1879 if (scc->modem.speed < 4800)
1880 {
1881 scc->kiss.txdelay = 36*TPS/100;
1882 scc->kiss.persist = 42;
1883 scc->kiss.slottime = 16*TPS/100;
1884 scc->kiss.tailtime = 4;
1885 scc->kiss.fulldup = 0;
1886 scc->kiss.waittime = 50*TPS/100;
1887 scc->kiss.maxkeyup = 10;
1888 scc->kiss.mintime = 3;
1889 scc->kiss.idletime = 30;
1890 scc->kiss.maxdefer = 120;
1891 scc->kiss.not_slip = 1;
1892 scc->kiss.softdcd = 0;
1893 } else {
1894 scc->kiss.txdelay = 10*TPS/100;
1895 scc->kiss.persist = 64;
1896 scc->kiss.slottime = 8*TPS/100;
1897 scc->kiss.tailtime = 1;
1898 scc->kiss.fulldup = 0;
1899 scc->kiss.waittime = 50*TPS/100;
1900 scc->kiss.maxkeyup = 7;
1901 scc->kiss.mintime = 3;
1902 scc->kiss.idletime = 30;
1903 scc->kiss.maxdefer = 120;
1904 scc->kiss.not_slip = 1;
1905 scc->kiss.softdcd = 0;
1906 }
1907
1908 scc->init = 1;
1909
1910 return 0;
1911 }
1912
1913 return -EINVAL;
1914 }
1915
1916 switch(cmd){
1917 case TCSBRK:
1918 return 0;
1919 case TIOCMGET:
1920 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(unsigned int *));
1921 if (error)
1922 return error;
1923
1924 save_flags(flags); cli();
1925
1926 result = ((scc->wreg[R5] & RTS) ? TIOCM_RTS : 0)
1927 | ((scc->wreg[R5] & DTR) ? TIOCM_DTR : 0)
1928 | ((InReg(scc->ctrl,R0) & DCD) ? TIOCM_CAR : 0)
1929 | ((InReg(scc->ctrl,R0) & CTS) ? TIOCM_CTS : 0);
1930
1931 restore_flags(flags);
1932
1933 put_user_long(result,(unsigned int *) arg);
1934 return 0;
1935 case TIOCMBIS:
1936 case TIOCMBIC:
1937 case TIOCMSET:
1938 switch (cmd) {
1939 case TIOCMBIS:
1940 scc->wreg[R5] |= DTR;
1941 scc->wreg[R5] |= RTS;
1942 break;
1943 case TIOCMBIC:
1944 scc->wreg[R5] &= ~DTR;
1945 scc->wreg[R5] &= ~RTS;
1946 break;
1947 case TIOCMSET:
1948 value = get_user_long((unsigned int *) arg);
1949
1950 if(value & TIOCM_DTR)
1951 scc->wreg[R5] |= DTR;
1952 else
1953 scc->wreg[R5] &= ~DTR;
1954 if(value & TIOCM_RTS)
1955 scc->wreg[R5] |= RTS;
1956 else
1957 scc->wreg[R5] &= ~RTS;
1958 break;
1959 }
1960
1961 save_flags(flags); cli();
1962
1963 if(scc->stat.tx_state == TXS_IDLE && !Running(t_idle))
1964 maxk_idle_timeout(scc);
1965
1966 restore_flags(flags);
1967
1968 return 0;
1969
1970 case TCGETS:
1971 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct termios));
1972 if (error)
1973 return error;
1974 if (!arg)
1975 return -EFAULT;
1976
1977 memcpy_tofs((void *) arg, scc->tty->termios, sizeof(struct termios));
1978 return 0;
1979
1980 case TCSETS:
1981 case TCSETSF:
1982 case TCSETSW:
1983 if (!suser())
1984 return -EPERM;
1985 if (!arg)
1986 return -EFAULT;
1987
1988 memcpy_fromfs(scc->tty->termios, (void *) arg, sizeof(struct termios));
1989 scc_change_speed(scc);
1990 return 0;
1991
1992
1993 case TIOCSCCSTAT:
1994 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(struct scc_stat));
1995 if (error)
1996 return error;
1997
1998 if (!arg)
1999 return -EFAULT;
2000
2001 scc->stat.used_buf = scc_count_used_buffers(&scc->stat.rx_alloc,
2002 &scc->stat.tx_alloc);
2003
2004 memcpy_tofs((void *) arg, &scc->stat, sizeof(struct scc_stat));
2005 return 0;
2006
2007 #define TICKS (100/TPS)
2008 #define CAST(x) (unsigned long)(x)
2009 #define Val kiss_cmd.param
2010 #define VAL kiss_cmd.param*TPS/100
2011 #define SVAL kiss_cmd.param? kiss_cmd.param:TIMER_STOPPED
2012
2013 case TIOCGKISS:
2014 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(struct ioctl_command));
2015 if (error)
2016 return error;
2017
2018 if (!arg)
2019 return -EFAULT;
2020
2021 memcpy_fromfs(&kiss_cmd, (void *) arg, sizeof(struct ioctl_command));
2022
2023 switch (kiss_cmd.command)
2024 {
2025 case PARAM_TXDELAY: r = CAST(scc->kiss.txdelay*TICKS); break;
2026 case PARAM_PERSIST: r = CAST(scc->kiss.persist); break;
2027 case PARAM_SLOTTIME: r = CAST(scc->kiss.slottime*TICKS); break;
2028 case PARAM_TXTAIL: r = CAST(scc->kiss.tailtime*TICKS); break;
2029 case PARAM_FULLDUP: r = CAST(scc->kiss.fulldup); break;
2030 case PARAM_SOFTDCD: r = CAST(scc->kiss.softdcd); break;
2031 case PARAM_DTR: r = CAST((scc->wreg[R5] & DTR)? 1:0); break;
2032 case PARAM_RTS: r = CAST((scc->wreg[R5] & RTS)? 1:0); break;
2033 case PARAM_SPEED: r = CAST(scc->modem.speed); break;
2034 case PARAM_GROUP: r = CAST(scc->kiss.group); break;
2035 case PARAM_IDLE: r = CAST(scc->kiss.idletime); break;
2036 case PARAM_MIN: r = CAST(scc->kiss.mintime); break;
2037 case PARAM_MAXKEY: r = CAST(scc->kiss.maxkeyup); break;
2038 case PARAM_WAIT: r = CAST(scc->kiss.waittime); break;
2039 case PARAM_MAXDEFER: r = CAST(scc->kiss.maxdefer); break;
2040 case PARAM_TX: r = CAST(scc->kiss.tx_inhibit); break;
2041 case PARAM_SLIP: r = CAST(!scc->kiss.not_slip); break;
2042 default: r = NO_SUCH_PARAM;
2043 }
2044
2045 kiss_cmd.param = r;
2046
2047 memcpy_tofs((void *) arg, &kiss_cmd, sizeof(struct ioctl_command));
2048 return 0;
2049 break;
2050
2051 case TIOCSKISS:
2052 if (!arg)
2053 return -EFAULT;
2054
2055 if (!suser())
2056 return -EPERM;
2057
2058 memcpy_fromfs(&kiss_cmd, (void *) arg, sizeof(struct ioctl_command));
2059
2060 switch (kiss_cmd.command)
2061 {
2062 case PARAM_TXDELAY: scc->kiss.txdelay=VAL; break;
2063 case PARAM_PERSIST: scc->kiss.persist=Val; break;
2064 case PARAM_SLOTTIME: scc->kiss.slottime=VAL; break;
2065 case PARAM_TXTAIL: scc->kiss.tailtime=VAL; break;
2066 case PARAM_FULLDUP: scc->kiss.fulldup=Val; break;
2067 case PARAM_SOFTDCD: scc->kiss.softdcd=Val; break;
2068 case PARAM_DTR: break;
2069 case PARAM_RTS: break;
2070 case PARAM_SPEED: scc->modem.speed=Val; break;
2071 case PARAM_GROUP: scc->kiss.group=Val; break;
2072 case PARAM_IDLE: scc->kiss.idletime=Val; break;
2073 case PARAM_MIN: scc->kiss.mintime=SVAL; break;
2074 case PARAM_MAXKEY: scc->kiss.maxkeyup=SVAL; break;
2075 case PARAM_WAIT: scc->kiss.waittime=Val; break;
2076 case PARAM_MAXDEFER: scc->kiss.maxdefer=SVAL; break;
2077 case PARAM_TX: scc->kiss.tx_inhibit=Val; break;
2078 case PARAM_SLIP: scc->kiss.not_slip=!Val; break;
2079 default: return -ENOIOCTLCMD;
2080 }
2081
2082 return 0;
2083 break;
2084 #undef TICKS
2085 #undef CAST
2086 #undef VAL
2087 #undef SVAL
2088 #undef Val
2089
2090 default:
2091 return -ENOIOCTLCMD;
2092 }
2093 }
2094
2095
2096
2097
2098 static void
2099 scc_set_termios(struct tty_struct * tty, struct termios * old_termios)
2100 {
2101 if (tty->termios->c_cflag == old_termios->c_cflag)
2102 return;
2103 scc_change_speed(tty->driver_data);
2104 }
2105
2106
2107 static inline void check_tx_queue(register struct scc_channel *scc)
2108 {
2109 register struct mbuf *bp;
2110
2111 if (scc->stat.tx_queued > QUEUE_THRES)
2112 {
2113 if (scc->sndq1 == NULLBUF)
2114 {
2115 printk("z8530drv: Warning - scc->stat.tx_queued shows overflow"
2116 " (%d) but queue is empty\n", scc->stat.tx_queued);
2117
2118 scc->stat.tx_queued = 0;
2119 scc->stat.nospace = 54321;
2120 return;
2121 }
2122
2123 bp = scc->sndq1->anext;
2124
2125 while (bp && (scc->stat.tx_queued > QUEUE_HYST))
2126 {
2127 bp = scc_free_chain(bp, BT_TRANSMIT);
2128 scc->stat.tx_queued--;
2129 scc->stat.nospace++;
2130 }
2131
2132 scc->sndq1->anext = bp;
2133 }
2134 }
2135
2136
2137
2138
2139
2140
2141 int scc_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
2142 {
2143 struct scc_channel * scc = tty->driver_data;
2144 unsigned char tbuf[BUFSIZE], *p;
2145 int cnt, cnt2;
2146
2147 if (!tty) return count;
2148
2149 if (scc_paranoia_check(scc, tty->device, "scc_write"))
2150 return 0;
2151
2152 if (scc->kiss.tx_inhibit) return count;
2153
2154 check_tx_queue(scc);
2155
2156 cnt2 = count;
2157
2158 while (cnt2)
2159 {
2160 cnt = cnt2 > BUFSIZE? BUFSIZE:cnt2;
2161 cnt2 -= cnt;
2162
2163 if (from_user)
2164 memcpy_fromfs(tbuf, buf, cnt);
2165 else
2166 memcpy(tbuf, buf, cnt);
2167
2168 buf += cnt;
2169
2170 p=tbuf;
2171
2172 while(cnt--)
2173 if (kiss_decode(scc, *p++))
2174 {
2175 scc->stat.nospace++;
2176 return 0;
2177 }
2178
2179 }
2180
2181 return count;
2182 }
2183
2184
2185
2186
2187 static void scc_put_char(struct tty_struct * tty, unsigned char ch)
2188 {
2189 struct scc_channel *scc = tty->driver_data;
2190 unsigned char ch2;
2191
2192 if (scc_paranoia_check(scc, tty->device, "scc_put_char"))
2193 return;
2194
2195 ch2 = ch;
2196 scc_write(tty, 0, &ch2, 1);
2197 }
2198
2199 static void scc_flush_chars(struct tty_struct * tty)
2200 {
2201 struct scc_channel *scc = tty->driver_data;
2202
2203 scc_paranoia_check(scc, tty->device, "scc_flush_chars");
2204
2205 return;
2206 }
2207
2208
2209
2210 static int scc_write_room(struct tty_struct *tty)
2211 {
2212 struct scc_channel *scc = tty->driver_data;
2213
2214 if (scc_paranoia_check(scc, tty->device, "scc_write_room"))
2215 return 0;
2216
2217 if (scc->stat.tx_alloc >= QUEUE_THRES)
2218 {
2219 printk("scc_write_room(): buffer full (ignore)\n");
2220 return 0;
2221 }
2222
2223 return BUFSIZE;
2224 }
2225
2226 static int scc_chars_in_buffer(struct tty_struct *tty)
2227 {
2228 struct scc_channel *scc = tty->driver_data;
2229
2230 if (scc && scc->sndq2)
2231 return scc->sndq2->cnt;
2232 else
2233 return 0;
2234 }
2235
2236 static void scc_flush_buffer(struct tty_struct *tty)
2237 {
2238 struct scc_channel *scc = tty->driver_data;
2239
2240 if (scc_paranoia_check(scc, tty->device, "scc_flush_buffer"))
2241 return;
2242
2243 scc->stat.tx_kiss_state = KISS_IDLE;
2244
2245 wake_up_interruptible(&tty->write_wait);
2246 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2247 tty->ldisc.write_wakeup)
2248 (tty->ldisc.write_wakeup)(tty);
2249 }
2250
2251 static void scc_throttle(struct tty_struct *tty)
2252 {
2253 struct scc_channel *scc = tty->driver_data;
2254
2255 if (scc_paranoia_check(scc, tty->device, "scc_throttle"))
2256 return;
2257
2258
2259
2260 }
2261
2262 static void scc_unthrottle(struct tty_struct *tty)
2263 {
2264 struct scc_channel *scc = tty->driver_data;
2265
2266 if (scc_paranoia_check(scc, tty->device, "scc_unthrottle"))
2267 return;
2268
2269
2270 }
2271
2272 static void scc_start(struct tty_struct *tty)
2273 {
2274 struct scc_channel *scc = tty->driver_data;
2275
2276 if (scc_paranoia_check(scc, tty->device, "scc_start"))
2277 return;
2278
2279
2280 }
2281
2282
2283 static void scc_stop(struct tty_struct *tty)
2284 {
2285 struct scc_channel *scc = tty->driver_data;
2286
2287 if (scc_paranoia_check(scc, tty->device, "scc_stop"))
2288 return;
2289
2290
2291 }
2292
2293
2294
2295
2296
2297
2298 long scc_init (long kmem_start)
2299 {
2300 int chip, chan;
2301 register io_port ctrl;
2302 long flags;
2303
2304
2305 memset(&scc_driver, 0, sizeof(struct tty_driver));
2306 scc_driver.magic = TTY_DRIVER_MAGIC;
2307 scc_driver.name = "sc";
2308 scc_driver.major = Z8530_MAJOR;
2309 scc_driver.minor_start = 0;
2310 scc_driver.num = Nchips*2;
2311 scc_driver.type = TTY_DRIVER_TYPE_SERIAL;
2312 scc_driver.subtype = 0;
2313 scc_driver.init_termios = tty_std_termios;
2314 scc_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2315 scc_driver.flags = TTY_DRIVER_REAL_RAW;
2316 scc_driver.refcount = &scc_refcount;
2317 scc_driver.table = scc_table;
2318 scc_driver.termios = (struct termios **) scc_termios;
2319 scc_driver.termios_locked = (struct termios **) scc_termios_locked;
2320 scc_driver.open = scc_open;
2321 scc_driver.close = scc_close;
2322 scc_driver.write = scc_write;
2323 scc_driver.start = scc_start;
2324 scc_driver.stop = scc_stop;
2325
2326 scc_driver.put_char = scc_put_char;
2327 scc_driver.flush_chars = scc_flush_chars;
2328 scc_driver.write_room = scc_write_room;
2329 scc_driver.chars_in_buffer = scc_chars_in_buffer;
2330 scc_driver.flush_buffer = scc_flush_buffer;
2331
2332 scc_driver.throttle = scc_throttle;
2333 scc_driver.unthrottle = scc_unthrottle;
2334
2335 scc_driver.ioctl = scc_ioctl;
2336 scc_driver.set_termios = scc_set_termios;
2337
2338 if (tty_register_driver(&scc_driver))
2339 panic("Couldn't register Z8530 SCC driver\n");
2340
2341 printk (BANNER);
2342
2343 if (Nchips > MAXSCC) Nchips = MAXSCC;
2344
2345
2346
2347 for (chip = 0; chip < Nchips; chip++)
2348 {
2349 memset((char *) &SCC_Info[2*chip ], 0, sizeof(struct scc_channel));
2350 memset((char *) &SCC_Info[2*chip+1], 0, sizeof(struct scc_channel));
2351
2352 ctrl = SCC_ctrl[chip * 2];
2353 if (!ctrl) continue;
2354
2355 save_flags(flags); cli();
2356
2357
2358
2359
2360
2361
2362 #ifndef DONT_CHECK
2363 check_region(ctrl, 1);
2364
2365 Outb(ctrl, 0);
2366 OutReg(ctrl,R13,0x55);
2367
2368 if (InReg(ctrl,R13) != 0x55 )
2369 {
2370 restore_flags(flags);
2371 continue;
2372 }
2373 #endif
2374
2375 SCC_Info[2*chip ].magic = SCC_MAGIC;
2376 SCC_Info[2*chip ].ctrl = SCC_ctrl[2*chip];
2377 SCC_Info[2*chip ].data = SCC_data[2*chip];
2378 SCC_Info[2*chip ].enhanced = SCC_Enhanced[chip];
2379
2380 SCC_Info[2*chip+1].magic = SCC_MAGIC;
2381 SCC_Info[2*chip+1].ctrl = SCC_ctrl[2*chip+1];
2382 SCC_Info[2*chip+1].data = SCC_data[2*chip+1];
2383 SCC_Info[2*chip+1].enhanced = SCC_Enhanced[chip];
2384
2385
2386 restore_flags(flags);
2387 }
2388
2389 #ifdef DO_FAST_RX
2390 rx_timer_cb.lock = 0;
2391 #else
2392 rx_timer_cb.lock = 1;
2393 #endif
2394
2395 #ifdef VERBOSE_BOOTMSG
2396 printk("Init Z8530 driver: %u channels, using irq %u\n",Nchips*2,Ivec);
2397
2398
2399 for (chan = 0; chan < Nchips * 2 ; chan++)
2400 {
2401 printk("/dev/%s%i: data port = 0x%3.3x control port = 0x%3.3x -- %s\n",
2402 scc_driver.name, chan, SCC_data[chan], SCC_ctrl[chan],
2403 SCC_Info[chan].ctrl? "found" : "missing");
2404
2405 if (SCC_Info[chan].ctrl == 0)
2406 {
2407 SCC_ctrl[chan] = 0;
2408 } else {
2409 request_region(SCC_ctrl[chan], 1, "scc ctrl");
2410 request_region(SCC_data[chan], 1, "scc data");
2411 }
2412 }
2413 #else
2414 printk("Init Z8530 driver: %u channels\n",Nchips*2);
2415 #endif
2416
2417
2418 return kmem_start;
2419 }
2420 #endif