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