This source file includes following definitions.
- serial_in
- serial_inp
- serial_out
- serial_outp
- rs_stop
- rs_start
- rs_probe
- rs_sched_event
- receive_chars
- transmit_chars
- check_modem_status
- figure_RS_timer
- rs_interrupt
- handle_rs_break
- do_softint
- rs_timer
- grab_all_interrupts
- free_all_interrupts
- figure_IRQ_timeout
- startup
- shutdown
- change_speed
- restart_port
- rs_write
- rs_throttle
- get_serial_info
- set_serial_info
- get_modem_info
- set_modem_info
- do_autoconfig
- send_break
- check_wild_interrupts
- rs_ioctl
- rs_set_termios
- rs_close
- rs_hangup
- block_til_ready
- rs_open
- show_serial_version
- get_auto_irq
- do_auto_irq
- autoconfig
- rs_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/tty.h>
25 #include <linux/serial.h>
26 #include <linux/interrupt.h>
27 #include <linux/config.h>
28 #include <linux/string.h>
29 #include <linux/fcntl.h>
30 #include <linux/ptrace.h>
31
32 #include <asm/system.h>
33 #include <asm/io.h>
34 #include <asm/segment.h>
35 #include <asm/bitops.h>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 #undef ISR_HACK
59
60 #define WAKEUP_CHARS (3*TTY_BUF_SIZE/4)
61
62
63
64
65
66
67
68
69
70
71
72
73 static int rs_event[2];
74
75 static struct async_struct *IRQ_ports[16];
76 static int IRQ_active;
77 static unsigned long IRQ_timer[16];
78 static int IRQ_timeout[16];
79 static volatile int rs_irq_triggered;
80 static volatile int rs_triggered;
81 static int rs_wild_int_mask;
82
83 static void autoconfig(struct async_struct * info);
84 static void change_speed(unsigned int line);
85
86
87
88
89
90
91
92
93 #define BASE_BAUD ( 1843200 / 16 )
94
95 #ifdef CONFIG_AUTO_IRQ
96 #define AUTO_IRQ_FLAG ASYNC_AUTO_IRQ
97 #else
98 #define AUTO_IRQ_FLAG 0
99 #endif
100
101
102 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST | AUTO_IRQ_FLAG)
103 #define STD_COM4_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
104
105 #ifdef CONFIG_AST_FOURPORT
106 #define FOURPORT_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_FOURPORT | AUTO_IRQ_FLAG)
107 #else
108 #define FOURPORT_FLAGS (ASYNC_FOURPORT | AUTO_IRQ_FLAG)
109 #endif
110
111 #ifdef CONFIG_ACCENT_ASYNC
112 #define ACCENT_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
113 #else
114 #define ACCENT_FLAGS AUTO_IRQ_FLAG
115 #endif
116
117 #ifdef CONFIG_BOCA
118 #define BOCA_FLAGS (ASYNC_BOOT_AUTOCONF | AUTO_IRQ_FLAG)
119 #else
120 #define BOCA_FLAGS AUTO_IRQ_FLAG
121 #endif
122
123 #ifdef CONFIG_HUB6
124 #define HUB6_FLAGS (ASYNC_BOOT_AUTOCONF)
125 #else
126 #define HUB6_FLAGS 0
127 #endif
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 #define C_P(card,port) (((card)<<6|(port)<<3) + 1)
144
145 struct async_struct rs_table[] = {
146
147 { BASE_BAUD, 0x3F8, 4, STD_COM_FLAGS },
148 { BASE_BAUD, 0x2F8, 3, STD_COM_FLAGS },
149 { BASE_BAUD, 0x3E8, 4, STD_COM_FLAGS },
150 { BASE_BAUD, 0x2E8, 3, STD_COM4_FLAGS },
151
152 { BASE_BAUD, 0x1A0, 9, FOURPORT_FLAGS },
153 { BASE_BAUD, 0x1A8, 9, FOURPORT_FLAGS },
154 { BASE_BAUD, 0x1B0, 9, FOURPORT_FLAGS },
155 { BASE_BAUD, 0x1B8, 9, FOURPORT_FLAGS },
156
157 { BASE_BAUD, 0x2A0, 5, FOURPORT_FLAGS },
158 { BASE_BAUD, 0x2A8, 5, FOURPORT_FLAGS },
159 { BASE_BAUD, 0x2B0, 5, FOURPORT_FLAGS },
160 { BASE_BAUD, 0x2B8, 5, FOURPORT_FLAGS },
161
162 { BASE_BAUD, 0x330, 4, ACCENT_FLAGS },
163 { BASE_BAUD, 0x338, 4, ACCENT_FLAGS },
164 { BASE_BAUD, 0x000, 0, 0 },
165 { BASE_BAUD, 0x000, 0, 0 },
166
167 { BASE_BAUD, 0x100, 12, BOCA_FLAGS },
168 { BASE_BAUD, 0x108, 12, BOCA_FLAGS },
169 { BASE_BAUD, 0x110, 12, BOCA_FLAGS },
170 { BASE_BAUD, 0x118, 12, BOCA_FLAGS },
171 { BASE_BAUD, 0x120, 12, BOCA_FLAGS },
172 { BASE_BAUD, 0x128, 12, BOCA_FLAGS },
173 { BASE_BAUD, 0x130, 12, BOCA_FLAGS },
174 { BASE_BAUD, 0x138, 12, BOCA_FLAGS },
175 { BASE_BAUD, 0x140, 12, BOCA_FLAGS },
176 { BASE_BAUD, 0x148, 12, BOCA_FLAGS },
177 { BASE_BAUD, 0x150, 12, BOCA_FLAGS },
178 { BASE_BAUD, 0x158, 12, BOCA_FLAGS },
179 { BASE_BAUD, 0x160, 12, BOCA_FLAGS },
180 { BASE_BAUD, 0x168, 12, BOCA_FLAGS },
181 { BASE_BAUD, 0x170, 12, BOCA_FLAGS },
182 { BASE_BAUD, 0x178, 12, BOCA_FLAGS },
183
184
185
186
187 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,0) },
188 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,1) },
189 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,2) },
190 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,3) },
191 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,4) },
192 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,5) },
193 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,0) },
194 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,1) },
195 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,2) },
196 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,3) },
197 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,4) },
198 { BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,5) },
199 };
200
201 #define NR_PORTS (sizeof(rs_table)/sizeof(struct async_struct))
202
203
204
205
206 static int baud_table[] = {
207 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
208 9600, 19200, 38400, 57600, 115200, 0 };
209
210 static void rs_throttle(struct tty_struct * tty, int status);
211
212 static inline unsigned int serial_in(struct async_struct *info, int offset)
213 {
214 if (info->hub6) {
215 outb(info->hub6 - 1 + offset, info->port);
216 return inb(info->port+1);
217 } else
218 return inb(info->port + offset);
219 }
220
221 static inline unsigned int serial_inp(struct async_struct *info, int offset)
222 {
223 if (info->hub6) {
224 outb(info->hub6 - 1 + offset, info->port);
225 return inb_p(info->port+1);
226 } else
227 return inb_p(info->port + offset);
228 }
229
230 static inline void serial_out(struct async_struct *info, int offset, int value)
231 {
232 if (info->hub6) {
233 outb(info->hub6 - 1 + offset, info->port);
234 outb(value, info->port+1);
235 } else
236 outb(value, info->port+offset);
237 }
238
239 static inline void serial_outp(struct async_struct *info, int offset,
240 int value)
241 {
242 if (info->hub6) {
243 outb(info->hub6 - 1 + offset, info->port);
244 outb_p(value, info->port+1);
245 } else
246 outb_p(value, info->port+offset);
247 }
248
249
250
251
252
253
254
255
256
257 static void rs_stop(struct tty_struct *tty)
258 {
259 struct async_struct *info;
260
261 info = rs_table + DEV_TO_SL(tty->line);
262
263 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
264 #ifdef ISR_HACK
265 serial_out(info, UART_IER, info->IER);
266 #endif
267 }
268
269 static void rs_start(struct tty_struct *tty)
270 {
271 struct async_struct *info;
272
273 info = rs_table + DEV_TO_SL(tty->line);
274
275 info->IER = (UART_IER_MSI | UART_IER_RLSI |
276 UART_IER_THRI | UART_IER_RDI);
277 #ifdef ISR_HACK
278 serial_out(info, UART_IER, info->IER);
279 #endif
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307 static void rs_probe(int irq)
308 {
309 rs_irq_triggered = irq;
310 rs_triggered |= 1 << irq;
311 return;
312 }
313
314
315
316
317
318 static inline void rs_sched_event(struct async_struct *info,
319 int event)
320 {
321 info->event |= 1 << event;
322 set_bit(info->line, rs_event);
323 mark_bh(SERIAL_BH);
324 }
325
326 static inline void receive_chars(struct async_struct *info,
327 int *status)
328 {
329 struct tty_queue * queue;
330 int head, tail, ch;
331
332
333
334
335
336 #define VLEFT ((tail-head-1)&(TTY_BUF_SIZE-1))
337
338 queue = &info->tty->read_q;
339 head = queue->head;
340 tail = queue->tail;
341 do {
342 ch = serial_inp(info, UART_RX);
343
344
345
346
347 if (VLEFT < 2)
348 break;
349 if (*status & info->read_status_mask) {
350 set_bit(head, &info->tty->readq_flags);
351 if (*status & (UART_LSR_BI)) {
352 queue->buf[head++]= TTY_BREAK;
353 rs_sched_event(info, RS_EVENT_BREAK);
354 } else if (*status & UART_LSR_PE)
355 queue->buf[head++]= TTY_PARITY;
356 else if (*status & UART_LSR_FE)
357 queue->buf[head++]= TTY_FRAME;
358 else if (*status & UART_LSR_OE)
359 queue->buf[head++]= TTY_OVERRUN;
360 head &= TTY_BUF_SIZE-1;
361 }
362 queue->buf[head++] = ch;
363 head &= TTY_BUF_SIZE-1;
364 } while ((*status = serial_inp(info, UART_LSR)) & UART_LSR_DR);
365 queue->head = head;
366 if ((VLEFT < RQ_THRESHOLD_LW) && !set_bit(TTY_RQ_THROTTLED,
367 &info->tty->flags))
368 rs_throttle(info->tty, TTY_THROTTLE_RQ_FULL);
369 rs_sched_event(info, RS_EVENT_READ_PROCESS);
370 #ifdef SERIAL_DEBUG_INTR
371 printk("DR...");
372 #endif
373 }
374
375 static inline void transmit_chars(struct async_struct *info, int *done_work)
376 {
377 struct tty_queue * queue;
378 int head, tail, count;
379
380 queue = &info->tty->write_q;
381 head = queue->head;
382 tail = queue->tail;
383 if (head==tail && !info->x_char) {
384 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
385 #ifdef ISR_HACK
386 serial_out(info, UART_IER, info->IER);
387 #endif
388 return;
389 }
390 count = info->xmit_fifo_size;
391 if (info->x_char) {
392 serial_outp(info, UART_TX, info->x_char);
393 info->x_char = 0;
394 count--;
395 }
396 while (count-- && (tail != head)) {
397 serial_outp(info, UART_TX, queue->buf[tail++]);
398 tail &= TTY_BUF_SIZE-1;
399 }
400 queue->tail = tail;
401 if (VLEFT > WAKEUP_CHARS) {
402 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
403 if (info->tty->write_data_cnt) {
404 set_bit(info->tty->line, &tty_check_write);
405 mark_bh(TTY_BH);
406 }
407 }
408 #ifdef SERIAL_DEBUG_INTR
409 printk("THRE...");
410 #endif
411 (*done_work)++;
412 }
413
414 static inline int check_modem_status(struct async_struct *info)
415 {
416 int status;
417
418 status = serial_in(info, UART_MSR);
419
420 if ((status & UART_MSR_DDCD) && !C_LOCAL(info->tty)) {
421 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
422 printk("ttys%d CD now %s...", info->line,
423 (status & UART_MSR_DCD) ? "on" : "off");
424 #endif
425 if (status & UART_MSR_DCD)
426 rs_sched_event(info, RS_EVENT_OPEN_WAKEUP);
427 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
428 (info->flags & ASYNC_CALLOUT_NOHUP))) {
429 #ifdef SERIAL_DEBUG_OPEN
430 printk("scheduling hangup...");
431 #endif
432 rs_sched_event(info, RS_EVENT_HANGUP);
433 }
434 }
435 if (C_RTSCTS(info->tty)) {
436 if (info->tty->hw_stopped) {
437 if (status & UART_MSR_CTS) {
438 #ifdef SERIAL_DEBUG_INTR
439 printk("CTS tx start...");
440 #endif
441 info->tty->hw_stopped = 0;
442 rs_start(info->tty);
443 return 1;
444 }
445 } else {
446 if (!(status & UART_MSR_CTS)) {
447 #ifdef SERIAL_DEBUG_INTR
448 printk("CTS tx stop...");
449 #endif
450 info->tty->hw_stopped = 1;
451 rs_stop(info->tty);
452 }
453 }
454 }
455 return 0;
456 }
457
458 static inline void figure_RS_timer(void)
459 {
460 int timeout = 6000;
461 int i, mask;
462
463 if (!IRQ_active)
464 return;
465 for (i=0, mask = 1; mask <= IRQ_active; i++, mask <<= 1) {
466 if (!(mask & IRQ_active))
467 continue;
468 if (IRQ_timer[i] < timeout)
469 timeout = IRQ_timer[i];
470 }
471 timer_table[RS_TIMER].expires = jiffies + timeout;
472 timer_active |= 1 << RS_TIMER;
473 }
474
475
476
477
478
479 static void rs_interrupt(int irq)
480 {
481 int status;
482 struct async_struct * info;
483 int done, done_work, pass_number, recheck_count;
484
485 rs_irq_triggered = irq;
486 rs_triggered |= 1 << irq;
487
488 info = IRQ_ports[irq];
489 done = 1;
490 done_work = 0;
491 pass_number = 0;
492 while (info) {
493 if (info->tty &&
494 (!pass_number ||
495 !(serial_inp(info, UART_IIR) & UART_IIR_NO_INT))) {
496 done = 0;
497 status = serial_inp(info, UART_LSR);
498 if (status & UART_LSR_DR) {
499 receive_chars(info, &status);
500 done_work++;
501 }
502 recheck_count = 0;
503 recheck_write:
504 if (status & UART_LSR_THRE) {
505 wake_up_interruptible(&info->xmit_wait);
506 if (!info->tty->stopped &&
507 !info->tty->hw_stopped)
508 transmit_chars(info, &done_work);
509 }
510 if (check_modem_status(info) &&
511 (recheck_count++ <= 64))
512 goto recheck_write;
513 #ifdef SERIAL_DEBUG_INTR
514 if (recheck_count > 16)
515 printk("recheck_count = %d\n", recheck_count);
516 #endif
517 }
518 #ifdef ISR_HACK
519 serial_outp(info, UART_IER, 0);
520 serial_out(info, UART_IER, info->IER);
521 #endif
522
523 info = info->next_port;
524 if (!info && !done) {
525 info = IRQ_ports[irq];
526 done = 1;
527 if (pass_number++ > 64)
528 break;
529 }
530 }
531 if ((info = IRQ_ports[irq]) != NULL) {
532 #ifdef 0
533 do {
534 serial_outp(info, UART_IER, 0);
535 serial_out(info, UART_IER, info->IER);
536 info = info->next_port;
537 } while (info);
538 #endif
539 if (irq && !done_work)
540 IRQ_timer[irq] = jiffies + 1500;
541 else
542 IRQ_timer[irq] = jiffies + IRQ_timeout[irq];
543 IRQ_active |= 1 << irq;
544 }
545 figure_RS_timer();
546 }
547
548
549
550
551
552
553
554
555
556
557
558 static inline void handle_rs_break(struct async_struct *info)
559 {
560 if (info->flags & ASYNC_SAK)
561 do_SAK(info->tty);
562
563 if (I_BRKINT(info->tty)) {
564 flush_input(info->tty);
565 flush_output(info->tty);
566 if (info->tty->pgrp > 0)
567 kill_pg(info->tty->pgrp, SIGINT,1);
568 }
569 }
570
571
572
573
574
575
576
577
578
579
580 static void do_softint(void *unused)
581 {
582 int i;
583 struct async_struct *info;
584
585 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
586 if (clear_bit(i, rs_event)) {
587 if (!info->tty)
588 continue;
589 if (clear_bit(RS_EVENT_READ_PROCESS, &info->event)) {
590 TTY_READ_FLUSH(info->tty);
591 }
592 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
593 wake_up_interruptible(&info->tty->write_q.proc_list);
594 }
595 if (clear_bit(RS_EVENT_HANGUP, &info->event)) {
596 tty_hangup(info->tty);
597 wake_up_interruptible(&info->open_wait);
598 info->flags &= ~(ASYNC_NORMAL_ACTIVE|
599 ASYNC_CALLOUT_ACTIVE);
600 }
601 if (clear_bit(RS_EVENT_BREAK, &info->event))
602 handle_rs_break(info);
603 if (clear_bit(RS_EVENT_OPEN_WAKEUP, &info->event)) {
604 wake_up_interruptible(&info->open_wait);
605 }
606 }
607 }
608 }
609
610
611
612
613
614
615
616
617 static void rs_timer(void)
618 {
619 int i, mask;
620 int timeout = 0;
621
622 for (i = 0, mask = 1; mask <= IRQ_active; i++, mask <<= 1) {
623 if ((mask & IRQ_active) && (IRQ_timer[i] <= jiffies)) {
624 IRQ_active &= ~mask;
625 cli();
626 #ifdef SERIAL_DEBUG_TIMER
627 printk("rs_timer: rs_interrupt(%d)...", i);
628 #endif
629 rs_interrupt(i);
630 sti();
631 }
632 if (mask & IRQ_active) {
633 if (!timeout || (IRQ_timer[i] < timeout))
634 timeout = IRQ_timer[i];
635 }
636 }
637 if (timeout) {
638 timer_table[RS_TIMER].expires = timeout;
639 timer_active |= 1 << RS_TIMER;
640 }
641 }
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658 static int grab_all_interrupts(int dontgrab)
659 {
660 int irq_lines = 0;
661 int i, mask;
662 struct sigaction sa;
663
664 sa.sa_handler = rs_probe;
665 sa.sa_flags = (SA_INTERRUPT);
666 sa.sa_mask = 0;
667 sa.sa_restorer = NULL;
668
669 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
670 if (!(mask & dontgrab) && !irqaction(i, &sa)) {
671 irq_lines |= mask;
672 }
673 }
674 return irq_lines;
675 }
676
677
678
679
680 static void free_all_interrupts(int irq_lines)
681 {
682 int i;
683
684 for (i = 0; i < 16; i++) {
685 if (irq_lines & (1 << i))
686 free_irq(i);
687 }
688 }
689
690
691
692
693
694
695 static void figure_IRQ_timeout(int irq)
696 {
697 struct async_struct *info;
698 int timeout = 6000;
699
700 info = IRQ_ports[irq];
701 if (!info) {
702 IRQ_timeout[irq] = 6000;
703 return;
704 }
705 while (info) {
706 if (info->timeout < timeout)
707 timeout = info->timeout;
708 info = info->next_port;
709 }
710 if (!irq)
711 timeout = timeout / 2;
712 IRQ_timeout[irq] = timeout ? timeout : 1;
713 }
714
715 static int startup(struct async_struct * info, int get_irq)
716 {
717 unsigned short ICP;
718 unsigned long flags;
719 struct sigaction sa;
720 int retval;
721
722 if (info->flags & ASYNC_INITIALIZED)
723 return 0;
724
725 if (!info->port || !info->type) {
726 if (info->tty)
727 set_bit(TTY_IO_ERROR, &info->tty->flags);
728 return 0;
729 }
730
731 save_flags(flags); cli();
732
733 #ifdef SERIAL_DEBUG_OPEN
734 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
735 #endif
736
737
738
739
740 if (get_irq && info->irq && !IRQ_ports[info->irq]) {
741 sa.sa_handler = rs_interrupt;
742 sa.sa_flags = (SA_INTERRUPT);
743 sa.sa_mask = 0;
744 sa.sa_restorer = NULL;
745 retval = irqaction(info->irq,&sa);
746 if (retval) {
747 restore_flags(flags);
748 return retval;
749 }
750 }
751
752
753
754
755
756 if (info->type == PORT_16550A) {
757 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
758 UART_FCR_CLEAR_XMIT));
759 info->xmit_fifo_size = 16;
760 } else
761 info->xmit_fifo_size = 1;
762
763
764
765
766 (void)serial_inp(info, UART_LSR);
767 (void)serial_inp(info, UART_RX);
768 (void)serial_inp(info, UART_IIR);
769 (void)serial_inp(info, UART_MSR);
770
771
772
773
774 serial_outp(info, UART_LCR, UART_LCR_WLEN8);
775 if (info->flags & ASYNC_FOURPORT)
776 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
777 else
778 serial_outp(info, UART_MCR,
779 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
780
781
782
783
784 #ifdef ISR_HACK
785 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
786 serial_outp(info, UART_IER, info->IER);
787 #else
788 info->IER = (UART_IER_MSI | UART_IER_RLSI |
789 UART_IER_THRI | UART_IER_RDI);
790 serial_outp(info, UART_IER, info->IER);
791 #endif
792 if (info->flags & ASYNC_FOURPORT) {
793
794 ICP = (info->port & 0xFE0) | 0x01F;
795 outb_p(0x80, ICP);
796 (void) inb_p(ICP);
797 }
798
799
800
801
802 (void)serial_inp(info, UART_LSR);
803 (void)serial_inp(info, UART_RX);
804 (void)serial_inp(info, UART_IIR);
805 (void)serial_inp(info, UART_MSR);
806
807 if (info->tty)
808 clear_bit(TTY_IO_ERROR, &info->tty->flags);
809
810
811
812 if (info->tty && info->tty->termios && I_INPCK(info->tty))
813 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
814 UART_LSR_FE | UART_LSR_PE);
815 else
816 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
817 UART_LSR_FE);
818
819
820
821
822 info->prev_port = 0;
823 info->next_port = IRQ_ports[info->irq];
824 if (info->next_port)
825 info->next_port->prev_port = info;
826 IRQ_ports[info->irq] = info;
827 figure_IRQ_timeout(info->irq);
828
829
830
831
832 IRQ_active |= 1 << info->irq;
833 figure_RS_timer();
834
835
836
837
838 change_speed(info->line);
839
840 info->flags |= ASYNC_INITIALIZED;
841 restore_flags(flags);
842 return 0;
843 }
844
845
846
847
848
849 static void shutdown(struct async_struct * info, int do_free_irq)
850 {
851 unsigned long flags;
852
853 if (!(info->flags & ASYNC_INITIALIZED))
854 return;
855
856 #ifdef SERIAL_DEBUG_OPEN
857 printk("Shutting down serial port %d (irq %d)....", info->line,
858 info->irq);
859 #endif
860
861 save_flags(flags); cli();
862
863
864
865
866 if (info->next_port)
867 info->next_port->prev_port = info->prev_port;
868 if (info->prev_port)
869 info->prev_port->next_port = info->next_port;
870 else
871 IRQ_ports[info->irq] = info->next_port;
872 figure_IRQ_timeout(info->irq);
873
874
875
876
877 if (do_free_irq && info->irq && !IRQ_ports[info->irq])
878 free_irq(info->irq);
879
880 info->IER = 0;
881 serial_outp(info, UART_IER, 0x00);
882 if (info->flags & ASYNC_FOURPORT) {
883
884 (void) inb((info->port & 0xFE0) | 0x01F);
885 }
886 if (info->tty && !(info->tty->termios->c_cflag & HUPCL))
887 serial_outp(info, UART_MCR, UART_MCR_DTR);
888 else
889
890 serial_outp(info, UART_MCR, 0x00);
891
892
893 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
894 UART_FCR_CLEAR_XMIT));
895 (void)serial_in(info, UART_RX);
896
897 if (info->tty)
898 set_bit(TTY_IO_ERROR, &info->tty->flags);
899
900 info->flags &= ~ASYNC_INITIALIZED;
901 restore_flags(flags);
902 }
903
904
905
906
907
908 static void change_speed(unsigned int line)
909 {
910 struct async_struct * info;
911 unsigned short port;
912 int quot = 0;
913 unsigned cflag,cval,mcr,fcr;
914 int i;
915
916 if (line >= NR_PORTS)
917 return;
918 info = rs_table + line;
919 if (!info->tty || !info->tty->termios)
920 return;
921 cflag = info->tty->termios->c_cflag;
922 if (!(port = info->port))
923 return;
924 i = cflag & CBAUD;
925 if (i == 15) {
926 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
927 i += 1;
928 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
929 i += 2;
930 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
931 quot = info->custom_divisor;
932 }
933 if (quot) {
934 info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
935 info->baud_base) + 2;
936 } else if (baud_table[i] == 134) {
937 quot = (2*info->baud_base / 269);
938 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
939 } else if (baud_table[i]) {
940 quot = info->baud_base / baud_table[i];
941 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
942 } else {
943 quot = 0;
944 info->timeout = 0;
945 }
946 cli();
947 mcr = serial_in(info, UART_MCR);
948 if (quot) {
949 serial_out(info, UART_MCR, mcr | UART_MCR_DTR);
950 } else {
951 serial_out(info, UART_MCR, mcr & ~UART_MCR_DTR);
952 sti();
953 return;
954 }
955 sti();
956
957 cval = cflag & (CSIZE | CSTOPB);
958 cval >>= 4;
959 if (cflag & PARENB)
960 cval |= UART_LCR_PARITY;
961 if (!(cflag & PARODD))
962 cval |= UART_LCR_EPAR;
963 if (info->type == PORT_16550A) {
964 if ((info->baud_base / quot) < 2400)
965 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
966 else
967 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
968 } else
969 fcr = 0;
970
971 cli();
972 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);
973 serial_outp(info, UART_DLL, quot & 0xff);
974 serial_outp(info, UART_DLM, quot >> 8);
975 serial_outp(info, UART_LCR, cval);
976 serial_outp(info, UART_FCR, fcr);
977 sti();
978 }
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993 static inline void restart_port(struct async_struct *info)
994 {
995 struct tty_queue * queue;
996 int head, tail, count;
997
998 if (!info)
999 return;
1000
1001 if (serial_inp(info, UART_LSR) & UART_LSR_THRE) {
1002 if (info->x_char) {
1003 serial_outp(info, UART_TX, info->x_char);
1004 info->x_char = 0;
1005 } else {
1006 queue = &info->tty->write_q;
1007 head = queue->head;
1008 tail = queue->tail;
1009 count = info->xmit_fifo_size;
1010 while (count--) {
1011 if (tail == head)
1012 break;
1013 serial_outp(info, UART_TX, queue->buf[tail++]);
1014 tail &= TTY_BUF_SIZE-1;
1015 }
1016 queue->tail = tail;
1017 }
1018 }
1019 }
1020
1021
1022
1023
1024
1025 void rs_write(struct tty_struct * tty)
1026 {
1027 struct async_struct *info;
1028
1029 if (!tty || tty->stopped || tty->hw_stopped)
1030 return;
1031 info = rs_table + DEV_TO_SL(tty->line);
1032 if (!info || !info->tty || !(info->flags & ASYNC_INITIALIZED))
1033 return;
1034 cli();
1035 restart_port(info);
1036 info->IER = (UART_IER_MSI | UART_IER_RLSI |
1037 UART_IER_THRI | UART_IER_RDI);
1038 #ifdef ISR_HACK
1039 serial_out(info, UART_IER, info->IER);
1040 #endif
1041 sti();
1042 }
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053 static void rs_throttle(struct tty_struct * tty, int status)
1054 {
1055 struct async_struct *info;
1056 unsigned char mcr;
1057 unsigned long flags;
1058
1059 save_flags(flags); cli();
1060 #if SERIAL_DEBUG_THROTTLE
1061 printk("throttle tty%d: %d (%d, %d)....\n", DEV_TO_SL(tty->line),
1062 status, LEFT(&tty->read_q), LEFT(&tty->secondary));
1063 #endif
1064 switch (status) {
1065 case TTY_THROTTLE_RQ_FULL:
1066 info = rs_table + DEV_TO_SL(tty->line);
1067 if (tty->termios->c_iflag & IXOFF) {
1068 info->x_char = STOP_CHAR(tty);
1069 } else {
1070 mcr = serial_inp(info, UART_MCR);
1071 mcr &= ~UART_MCR_RTS;
1072 serial_out(info, UART_MCR, mcr);
1073 }
1074 break;
1075 case TTY_THROTTLE_RQ_AVAIL:
1076 info = rs_table + DEV_TO_SL(tty->line);
1077 if (tty->termios->c_iflag & IXOFF) {
1078 if (info->x_char)
1079 info->x_char = 0;
1080 else
1081 info->x_char = START_CHAR(tty);
1082 } else {
1083 mcr = serial_in(info, UART_MCR);
1084 mcr |= UART_MCR_RTS;
1085 serial_out(info, UART_MCR, mcr);
1086 }
1087 break;
1088 }
1089 restore_flags(flags);
1090 }
1091
1092
1093
1094
1095
1096
1097
1098 static int get_serial_info(struct async_struct * info,
1099 struct serial_struct * retinfo)
1100 {
1101 struct serial_struct tmp;
1102
1103 if (!retinfo)
1104 return -EFAULT;
1105 memset(&tmp, 0, sizeof(tmp));
1106 tmp.type = info->type;
1107 tmp.line = info->line;
1108 tmp.port = info->port;
1109 tmp.irq = info->irq;
1110 tmp.flags = info->flags;
1111 tmp.baud_base = info->baud_base;
1112 tmp.close_delay = info->close_delay;
1113 tmp.custom_divisor = info->custom_divisor;
1114 tmp.hub6 = info->hub6;
1115 memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1116 return 0;
1117 }
1118
1119 static int set_serial_info(struct async_struct * info,
1120 struct serial_struct * new_info)
1121 {
1122 struct serial_struct new_serial;
1123 struct async_struct old_info;
1124 unsigned int i,change_irq,change_port;
1125 int retval;
1126 struct sigaction sa;
1127
1128 if (!new_info)
1129 return -EFAULT;
1130 memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1131 old_info = *info;
1132
1133 change_irq = new_serial.irq != info->irq;
1134 change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1135
1136 if (!suser()) {
1137 if (change_irq || change_port ||
1138 (new_serial.baud_base != info->baud_base) ||
1139 (new_serial.type != info->type) ||
1140 (new_serial.close_delay != info->close_delay) ||
1141 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1142 (info->flags & ~ASYNC_USR_MASK)))
1143 return -EPERM;
1144 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1145 (new_serial.flags & ASYNC_USR_MASK));
1146 info->custom_divisor = new_serial.custom_divisor;
1147 goto check_and_exit;
1148 }
1149
1150 if (new_serial.irq == 2)
1151 new_serial.irq = 9;
1152
1153 if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1154 (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1155 return -EINVAL;
1156 }
1157
1158
1159 for (i = 0 ; i < NR_PORTS; i++)
1160 if ((info != &rs_table[i]) &&
1161 (rs_table[i].port == new_serial.port) && rs_table[i].type)
1162 return -EADDRINUSE;
1163
1164
1165
1166
1167
1168
1169 if (new_serial.port && new_serial.type && new_serial.irq &&
1170 (change_irq || !(info->flags & ASYNC_INITIALIZED))) {
1171 if (!IRQ_ports[new_serial.irq]) {
1172 sa.sa_handler = rs_interrupt;
1173 sa.sa_flags = (SA_INTERRUPT);
1174 sa.sa_mask = 0;
1175 sa.sa_restorer = NULL;
1176 retval = irqaction(new_serial.irq,&sa);
1177 if (retval)
1178 return retval;
1179 }
1180 }
1181
1182 if ((change_port || change_irq) && (info->count > 1))
1183 return -EBUSY;
1184
1185
1186
1187
1188
1189
1190 info->baud_base = new_serial.baud_base;
1191 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1192 (new_serial.flags & ASYNC_FLAGS));
1193 info->custom_divisor = new_serial.custom_divisor;
1194 info->type = new_serial.type;
1195 info->close_delay = new_serial.close_delay;
1196
1197 if (change_port || change_irq) {
1198
1199
1200
1201
1202 shutdown(info, change_irq);
1203 info->irq = new_serial.irq;
1204 info->port = new_serial.port;
1205 info->hub6 = new_serial.hub6;
1206 }
1207
1208 check_and_exit:
1209 if (!info->port || !info->type)
1210 return 0;
1211 if (info->flags & ASYNC_INITIALIZED) {
1212 if (((old_info.flags & ASYNC_SPD_MASK) !=
1213 (info->flags & ASYNC_SPD_MASK)) ||
1214 (old_info.custom_divisor != info->custom_divisor))
1215 change_speed(info->line);
1216 } else
1217 (void) startup(info, 0);
1218 return 0;
1219 }
1220
1221 static int get_modem_info(struct async_struct * info, unsigned int *value)
1222 {
1223 unsigned char control, status;
1224 unsigned int result;
1225
1226 cli();
1227 control = serial_in(info, UART_MCR);
1228 status = serial_in(info, UART_MSR);
1229 sti();
1230 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1231 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1232 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1233 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1234 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1235 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1236 put_fs_long(result,(unsigned long *) value);
1237 return 0;
1238 }
1239
1240 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1241 unsigned int *value)
1242 {
1243 unsigned char control;
1244 unsigned int arg = get_fs_long((unsigned long *) value);
1245
1246 cli();
1247 control = serial_in(info, UART_MCR);
1248 sti();
1249
1250 switch (cmd) {
1251 case TIOCMBIS:
1252 if (arg & TIOCM_RTS)
1253 control |= UART_MCR_RTS;
1254 if (arg & TIOCM_DTR)
1255 control |= UART_MCR_DTR;
1256 break;
1257 case TIOCMBIC:
1258 if (arg & TIOCM_RTS)
1259 control &= ~UART_MCR_RTS;
1260 if (arg & TIOCM_DTR)
1261 control &= ~UART_MCR_DTR;
1262 break;
1263 case TIOCMSET:
1264 control = (control & ~(UART_MCR_RTS | UART_MCR_DTR))
1265 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1266 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1267 break;
1268 default:
1269 return -EINVAL;
1270 }
1271 cli();
1272 serial_out(info, UART_MCR, control);
1273 sti();
1274 return 0;
1275 }
1276
1277 static int do_autoconfig(struct async_struct * info)
1278 {
1279 int retval;
1280
1281 if (!suser())
1282 return -EPERM;
1283
1284 if (info->count > 1)
1285 return -EBUSY;
1286
1287 shutdown(info, 1);
1288
1289 cli();
1290 autoconfig(info);
1291 sti();
1292
1293 retval = startup(info, 1);
1294 if (retval)
1295 return retval;
1296 return 0;
1297 }
1298
1299
1300
1301
1302
1303 static void send_break( struct async_struct * info, int duration)
1304 {
1305 if (!info->port)
1306 return;
1307 current->state = TASK_INTERRUPTIBLE;
1308 current->timeout = jiffies + duration;
1309 cli();
1310 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
1311 schedule();
1312 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1313 sti();
1314 }
1315
1316
1317
1318
1319
1320 static int check_wild_interrupts(int doprint)
1321 {
1322 int i, mask;
1323 int wild_interrupts = 0;
1324 int irq_lines;
1325 unsigned long timeout;
1326 unsigned long flags;
1327
1328
1329 save_flags(flags); sti();
1330
1331 irq_lines = grab_all_interrupts(0);
1332
1333
1334
1335
1336
1337 timeout = jiffies+10;
1338 while (timeout >= jiffies)
1339 ;
1340
1341 rs_triggered = 0;
1342
1343 timeout = jiffies+10;
1344 while (timeout >= jiffies)
1345 ;
1346
1347 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1348 if ((rs_triggered & (1 << i)) &&
1349 (irq_lines & (1 << i))) {
1350 wild_interrupts |= mask;
1351 if (doprint)
1352 printk("Wild interrupt? (IRQ %d)\n", i);
1353 }
1354 }
1355 free_all_interrupts(irq_lines);
1356 restore_flags(flags);
1357 return wild_interrupts;
1358 }
1359
1360 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1361 unsigned int cmd, unsigned long arg)
1362 {
1363 int error, line;
1364 struct async_struct * info;
1365
1366 line = DEV_TO_SL(tty->line);
1367 if (line < 0 || line >= NR_PORTS)
1368 return -ENODEV;
1369 info = rs_table + line;
1370
1371 switch (cmd) {
1372 case TCSBRK:
1373 if (!arg)
1374 send_break(info, HZ/4);
1375 return 0;
1376 case TCSBRKP:
1377 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1378 return 0;
1379 case TIOCGSOFTCAR:
1380 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1381 if (error)
1382 return error;
1383 put_fs_long(C_LOCAL(tty) ? 1 : 0,
1384 (unsigned long *) arg);
1385 return 0;
1386 case TIOCSSOFTCAR:
1387 arg = get_fs_long((unsigned long *) arg);
1388 tty->termios->c_cflag =
1389 ((tty->termios->c_cflag & ~CLOCAL) |
1390 (arg ? CLOCAL : 0));
1391 return 0;
1392 case TIOCMGET:
1393 error = verify_area(VERIFY_WRITE, (void *) arg,
1394 sizeof(unsigned int));
1395 if (error)
1396 return error;
1397 return get_modem_info(info, (unsigned int *) arg);
1398 case TIOCMBIS:
1399 case TIOCMBIC:
1400 case TIOCMSET:
1401 return set_modem_info(info, cmd, (unsigned int *) arg);
1402 case TIOCGSERIAL:
1403 error = verify_area(VERIFY_WRITE, (void *) arg,
1404 sizeof(struct serial_struct));
1405 if (error)
1406 return error;
1407 return get_serial_info(info,
1408 (struct serial_struct *) arg);
1409 case TIOCSSERIAL:
1410 return set_serial_info(info,
1411 (struct serial_struct *) arg);
1412 case TIOCSERCONFIG:
1413 return do_autoconfig(info);
1414
1415 case TIOCSERGWILD:
1416 error = verify_area(VERIFY_WRITE, (void *) arg,
1417 sizeof(int));
1418 if (error)
1419 return error;
1420 put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
1421 return 0;
1422
1423 case TIOCSERSWILD:
1424 if (!suser())
1425 return -EPERM;
1426 rs_wild_int_mask = get_fs_long((unsigned long *) arg);
1427 if (rs_wild_int_mask < 0)
1428 rs_wild_int_mask = check_wild_interrupts(0);
1429 return 0;
1430
1431 default:
1432 return -EINVAL;
1433 }
1434 return 0;
1435 }
1436
1437 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1438 {
1439 struct async_struct *info;
1440
1441 if (tty->termios->c_cflag == old_termios->c_cflag)
1442 return;
1443
1444 info = &rs_table[DEV_TO_SL(tty->line)];
1445
1446 change_speed(DEV_TO_SL(tty->line));
1447
1448 if ((old_termios->c_cflag & CRTSCTS) &&
1449 !(tty->termios->c_cflag & CRTSCTS)) {
1450 tty->hw_stopped = 0;
1451 rs_write(tty);
1452 }
1453
1454 if (!(old_termios->c_cflag & CLOCAL) &&
1455 (tty->termios->c_cflag & CLOCAL))
1456 wake_up_interruptible(&info->open_wait);
1457
1458 if (I_INPCK(tty))
1459 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
1460 UART_LSR_FE | UART_LSR_PE);
1461 else
1462 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
1463 UART_LSR_FE);
1464 }
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476 static void rs_close(struct tty_struct *tty, struct file * filp)
1477 {
1478 struct async_struct * info;
1479 int line;
1480
1481 if (tty_hung_up_p(filp))
1482 return;
1483
1484 line = DEV_TO_SL(tty->line);
1485 if ((line < 0) || (line >= NR_PORTS))
1486 return;
1487 info = rs_table + line;
1488 #ifdef SERIAL_DEBUG_OPEN
1489 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1490 #endif
1491 if (--info->count > 0)
1492 return;
1493 info->flags |= ASYNC_CLOSING;
1494
1495
1496
1497
1498 if (info->flags & ASYNC_NORMAL_ACTIVE)
1499 info->normal_termios = *tty->termios;
1500 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1501 info->callout_termios = *tty->termios;
1502 tty->stopped = 0;
1503 tty->hw_stopped = 0;
1504 rs_start(tty);
1505 wait_until_sent(tty);
1506 cli();
1507
1508
1509
1510
1511 if (!(serial_inp(info, UART_LSR) & UART_LSR_THRE)) {
1512 rs_start(tty);
1513 interruptible_sleep_on(&info->xmit_wait);
1514 }
1515 sti();
1516 shutdown(info, 1);
1517 clear_bit(line, rs_event);
1518 info->event = 0;
1519 info->count = 0;
1520 info->tty = 0;
1521 if (info->blocked_open) {
1522 if (info->close_delay) {
1523 tty->count++;
1524 current->state = TASK_INTERRUPTIBLE;
1525 current->timeout = jiffies + info->close_delay;
1526 schedule();
1527 tty->count--;
1528 }
1529 wake_up_interruptible(&info->open_wait);
1530 }
1531 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1532 ASYNC_CLOSING);
1533 wake_up_interruptible(&info->close_wait);
1534 }
1535
1536
1537
1538
1539 void rs_hangup(struct tty_struct *tty)
1540 {
1541 struct async_struct * info;
1542 int line;
1543
1544 line = DEV_TO_SL(tty->line);
1545 if ((line < 0) || (line >= NR_PORTS))
1546 return;
1547 info = rs_table + line;
1548
1549 shutdown(info, 1);
1550 clear_bit(line, rs_event);
1551 info->event = 0;
1552 info->count = 0;
1553 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1554 info->tty = 0;
1555 wake_up_interruptible(&info->open_wait);
1556 }
1557
1558
1559
1560
1561
1562
1563 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1564 struct async_struct *info)
1565 {
1566 struct wait_queue wait = { current, NULL };
1567 int retval;
1568 int do_clocal = C_LOCAL(tty);
1569
1570
1571
1572
1573
1574 if (info->flags & ASYNC_CLOSING) {
1575 interruptible_sleep_on(&info->close_wait);
1576 return -EAGAIN;
1577 }
1578
1579
1580
1581
1582
1583 if (MAJOR(filp->f_rdev) == 5) {
1584 if (info->flags & ASYNC_NORMAL_ACTIVE)
1585 return -EBUSY;
1586 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1587 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1588 (info->session != current->session))
1589 return -EBUSY;
1590 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1591 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1592 (info->pgrp != current->pgrp))
1593 return -EBUSY;
1594 info->flags |= ASYNC_CALLOUT_ACTIVE;
1595 return 0;
1596 }
1597
1598
1599
1600
1601
1602 if (filp->f_flags & O_NONBLOCK) {
1603 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1604 return -EBUSY;
1605 info->flags |= ASYNC_NORMAL_ACTIVE;
1606 return 0;
1607 }
1608
1609
1610
1611
1612
1613
1614
1615
1616 retval = 0;
1617 add_wait_queue(&info->open_wait, &wait);
1618 #ifdef SERIAL_DEBUG_OPEN
1619 printk("block_til_ready before block: ttys%d, count = %d\n",
1620 info->line, info->count);
1621 #endif
1622 info->count--;
1623 info->blocked_open++;
1624 while (1) {
1625 cli();
1626 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
1627 serial_out(info, UART_MCR,
1628 serial_inp(info, UART_MCR) |
1629 (UART_MCR_DTR | UART_MCR_RTS));
1630 sti();
1631 current->state = TASK_INTERRUPTIBLE;
1632 if (tty_hung_up_p(filp) ||
1633 !(info->flags & ASYNC_INITIALIZED)) {
1634 retval = -EAGAIN;
1635 break;
1636 }
1637 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1638 !(info->flags & ASYNC_CLOSING) &&
1639 (do_clocal || (serial_in(info, UART_MSR) &
1640 UART_MSR_DCD)))
1641 break;
1642 if (current->signal & ~current->blocked) {
1643 retval = -ERESTARTSYS;
1644 break;
1645 }
1646 #ifdef SERIAL_DEBUG_OPEN
1647 printk("block_til_ready blocking: ttys%d, count = %d\n",
1648 info->line, info->count);
1649 #endif
1650 schedule();
1651 }
1652 current->state = TASK_RUNNING;
1653 remove_wait_queue(&info->open_wait, &wait);
1654 if (!tty_hung_up_p(filp))
1655 info->count++;
1656 info->blocked_open--;
1657 #ifdef SERIAL_DEBUG_OPEN
1658 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1659 info->line, info->count);
1660 #endif
1661 if (retval)
1662 return retval;
1663 info->flags |= ASYNC_NORMAL_ACTIVE;
1664 return 0;
1665 }
1666
1667
1668
1669
1670
1671
1672
1673 int rs_open(struct tty_struct *tty, struct file * filp)
1674 {
1675 struct async_struct *info;
1676 int retval, line;
1677
1678 line = DEV_TO_SL(tty->line);
1679 if ((line < 0) || (line >= NR_PORTS))
1680 return -ENODEV;
1681 info = rs_table + line;
1682 #ifdef SERIAL_DEBUG_OPEN
1683 printk("rs_open ttys%d, count = %d\n", info->line, info->count);
1684 #endif
1685 info->count++;
1686 info->tty = tty;
1687
1688 tty->write = rs_write;
1689 tty->close = rs_close;
1690 tty->ioctl = rs_ioctl;
1691 tty->throttle = rs_throttle;
1692 tty->set_termios = rs_set_termios;
1693 tty->stop = rs_stop;
1694 tty->start = rs_start;
1695 tty->hangup = rs_hangup;
1696 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1697 if (MAJOR(filp->f_rdev) == 4)
1698 *tty->termios = info->normal_termios;
1699 else
1700 *tty->termios = info->callout_termios;
1701 }
1702
1703
1704
1705 retval = startup(info, 1);
1706 if (retval)
1707 return retval;
1708
1709 retval = block_til_ready(tty, filp, info);
1710 if (retval) {
1711 #ifdef SERIAL_DEBUG_OPEN
1712 printk("rs_open returning after block_til_ready with %d\n",
1713 retval);
1714 #endif
1715 return retval;
1716 }
1717
1718 info->session = current->session;
1719 info->pgrp = current->pgrp;
1720
1721 #ifdef SERIAL_DEBUG_OPEN
1722 printk("rs_open ttys%d successful...", info->line);
1723 #endif
1724 return 0;
1725 }
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740 static void show_serial_version(void)
1741 {
1742 printk("Serial driver version 3.99a with");
1743 #ifdef CONFIG_AST_FOURPORT
1744 printk(" AST_FOURPORT");
1745 #define SERIAL_OPT
1746 #endif
1747 #ifdef CONFIG_ACCENT_ASYNC
1748 printk(" ACCENT_ASYNC");
1749 #define SERIAL_OPT
1750 #endif
1751 #ifdef CONFIG_HUB6
1752 printk(" HUB-6");
1753 #define SERIAL_OPT
1754 #endif
1755 #ifdef CONFIG_AUTO_IRQ
1756 printk (" AUTO_IRQ");
1757 #define SERIAL_OPT
1758 #endif
1759 #ifdef SERIAL_OPT
1760 printk(" enabled\n");
1761 #else
1762 printk(" no serial options enabled\n");
1763 #endif
1764 #undef SERIAL_OPT
1765 }
1766
1767
1768
1769
1770
1771
1772 static int get_auto_irq(struct async_struct *info)
1773 {
1774 unsigned char save_MCR, save_IER, save_ICP=0;
1775 unsigned short ICP=0, port = info->port;
1776 unsigned long timeout;
1777
1778
1779
1780
1781 rs_irq_triggered = 0;
1782 cli();
1783 save_IER = serial_inp(info, UART_IER);
1784 save_MCR = serial_inp(info, UART_MCR);
1785 if (info->flags & ASYNC_FOURPORT) {
1786 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
1787 serial_outp(info, UART_IER, 0x0f);
1788 ICP = (port & 0xFE0) | 0x01F;
1789 save_ICP = inb_p(ICP);
1790 outb_p(0x80, ICP);
1791 (void) inb_p(ICP);
1792 } else {
1793 serial_outp(info, UART_MCR,
1794 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1795 serial_outp(info, UART_IER, 0x0f);
1796 }
1797 sti();
1798
1799
1800
1801 (void)serial_inp(info, UART_LSR);
1802 (void)serial_inp(info, UART_RX);
1803 (void)serial_inp(info, UART_IIR);
1804 (void)serial_inp(info, UART_MSR);
1805
1806 timeout = jiffies+2;
1807 while (timeout >= jiffies) {
1808 if (rs_irq_triggered)
1809 break;
1810 }
1811
1812
1813
1814 cli();
1815 serial_outp(info, UART_IER, save_IER);
1816 serial_outp(info, UART_MCR, save_MCR);
1817 if (info->flags & ASYNC_FOURPORT)
1818 outb_p(save_ICP, ICP);
1819 sti();
1820 return(rs_irq_triggered);
1821 }
1822
1823
1824
1825
1826
1827 static int do_auto_irq(struct async_struct * info)
1828 {
1829 unsigned port = info->port;
1830 int irq_lines = 0;
1831 int irq_try_1 = 0, irq_try_2 = 0;
1832 int retries;
1833 unsigned long flags;
1834
1835 if (!port)
1836 return 0;
1837
1838
1839 save_flags(flags); sti();
1840
1841 irq_lines = grab_all_interrupts(rs_wild_int_mask);
1842
1843 for (retries = 0; retries < 5; retries++) {
1844 if (!irq_try_1)
1845 irq_try_1 = get_auto_irq(info);
1846 if (!irq_try_2)
1847 irq_try_2 = get_auto_irq(info);
1848 if (irq_try_1 && irq_try_2) {
1849 if (irq_try_1 == irq_try_2)
1850 break;
1851 irq_try_1 = irq_try_2 = 0;
1852 }
1853 }
1854 restore_flags(flags);
1855 free_all_interrupts(irq_lines);
1856 return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
1857 }
1858
1859
1860
1861
1862
1863
1864
1865
1866 static void autoconfig(struct async_struct * info)
1867 {
1868 unsigned char status1, status2, scratch, scratch2;
1869 unsigned port = info->port;
1870 unsigned long flags;
1871
1872 info->type = PORT_UNKNOWN;
1873
1874 if (!port)
1875 return;
1876
1877 save_flags(flags); cli();
1878
1879
1880
1881
1882
1883 scratch = serial_inp(info, UART_IER);
1884 serial_outp(info, UART_IER, 0);
1885 scratch2 = serial_inp(info, UART_IER);
1886 serial_outp(info, UART_IER, scratch);
1887 if (scratch2) {
1888 restore_flags(flags);
1889 return;
1890 }
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901 if (!(info->flags & ASYNC_SKIP_TEST)) {
1902 scratch = serial_inp(info, UART_MCR);
1903 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
1904 scratch2 = serial_inp(info, UART_MSR);
1905 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
1906 status1 = serial_inp(info, UART_MSR) & 0xF0;
1907 serial_outp(info, UART_MCR, scratch);
1908 serial_outp(info, UART_MSR, scratch2);
1909 if (status1 != 0x90) {
1910 restore_flags(flags);
1911 return;
1912 }
1913 }
1914
1915
1916
1917
1918
1919 if (info->flags & ASYNC_AUTO_IRQ)
1920 info->irq = do_auto_irq(info);
1921
1922 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
1923 scratch = serial_in(info, UART_IIR) >> 6;
1924 info->xmit_fifo_size = 1;
1925 switch (scratch) {
1926 case 0:
1927 info->type = PORT_16450;
1928 break;
1929 case 1:
1930 info->type = PORT_UNKNOWN;
1931 break;
1932 case 2:
1933 info->type = PORT_16550;
1934 break;
1935 case 3:
1936 info->type = PORT_16550A;
1937 info->xmit_fifo_size = 16;
1938 break;
1939 }
1940 if (info->type == PORT_16450) {
1941 scratch = serial_in(info, UART_SCR);
1942 serial_outp(info, UART_SCR, 0xa5);
1943 status1 = serial_in(info, UART_SCR);
1944 serial_outp(info, UART_SCR, 0x5a);
1945 status2 = serial_in(info, UART_SCR);
1946 serial_outp(info, UART_SCR, scratch);
1947
1948 if ((status1 != 0xa5) || (status2 != 0x5a))
1949 info->type = PORT_8250;
1950 }
1951
1952
1953
1954
1955 serial_outp(info, UART_MCR, 0x00);
1956 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1957 UART_FCR_CLEAR_XMIT));
1958 (void)serial_in(info, UART_RX);
1959
1960 restore_flags(flags);
1961 }
1962
1963
1964
1965
1966 long rs_init(long kmem_start)
1967 {
1968 int i;
1969 struct async_struct * info;
1970
1971 memset(&rs_event, 0, sizeof(rs_event));
1972 bh_base[SERIAL_BH].routine = do_softint;
1973 timer_table[RS_TIMER].fn = rs_timer;
1974 timer_table[RS_TIMER].expires = 0;
1975 IRQ_active = 0;
1976 #ifdef CONFIG_AUTO_IRQ
1977 rs_wild_int_mask = check_wild_interrupts(1);
1978 #endif
1979
1980 for (i = 0; i < 16; i++) {
1981 IRQ_ports[i] = 0;
1982 IRQ_timeout[i] = 0;
1983 }
1984
1985 show_serial_version();
1986 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
1987 info->line = i;
1988 info->tty = 0;
1989 info->type = PORT_UNKNOWN;
1990 info->custom_divisor = 0;
1991 info->close_delay = 50;
1992 info->x_char = 0;
1993 info->event = 0;
1994 info->count = 0;
1995 info->blocked_open = 0;
1996 memset(&info->callout_termios, 0, sizeof(struct termios));
1997 memset(&info->normal_termios, 0, sizeof(struct termios));
1998 info->open_wait = 0;
1999 info->xmit_wait = 0;
2000 info->close_wait = 0;
2001 info->next_port = 0;
2002 info->prev_port = 0;
2003 if (info->irq == 2)
2004 info->irq = 9;
2005 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2006 continue;
2007 autoconfig(info);
2008 if (info->type == PORT_UNKNOWN)
2009 continue;
2010 printk("tty%02d%s at 0x%04x (irq = %d)", info->line,
2011 (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2012 info->port, info->irq);
2013 switch (info->type) {
2014 case PORT_8250:
2015 printk(" is a 8250\n");
2016 break;
2017 case PORT_16450:
2018 printk(" is a 16450\n");
2019 break;
2020 case PORT_16550:
2021 printk(" is a 16550\n");
2022 break;
2023 case PORT_16550A:
2024 printk(" is a 16550A\n");
2025 break;
2026 default:
2027 printk("\n");
2028 break;
2029 }
2030 }
2031 return kmem_start;
2032 }
2033