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