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