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