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 info->tty->termios &&
496 (!pass_number ||
497 !(serial_inp(info, UART_IIR) & UART_IIR_NO_INT))) {
498 done = 0;
499 status = serial_inp(info, UART_LSR);
500 if (status & UART_LSR_DR) {
501 receive_chars(info, &status);
502 done_work++;
503 }
504 recheck_count = 0;
505 recheck_write:
506 if (status & UART_LSR_THRE) {
507 wake_up_interruptible(&info->xmit_wait);
508 if (!info->tty->stopped &&
509 !info->tty->hw_stopped)
510 transmit_chars(info, &done_work);
511 }
512 if (check_modem_status(info) &&
513 (recheck_count++ <= 64))
514 goto recheck_write;
515 #ifdef SERIAL_DEBUG_INTR
516 if (recheck_count > 16)
517 printk("recheck_count = %d\n", recheck_count);
518 #endif
519 }
520 #ifdef ISR_HACK
521 serial_outp(info, UART_IER, 0);
522 serial_out(info, UART_IER, info->IER);
523 #endif
524
525 info = info->next_port;
526 if (!info && !done) {
527 info = IRQ_ports[irq];
528 done = 1;
529 if (pass_number++ > 64)
530 break;
531 }
532 }
533 if ((info = IRQ_ports[irq]) != NULL) {
534 #ifdef 0
535 do {
536 serial_outp(info, UART_IER, 0);
537 serial_out(info, UART_IER, info->IER);
538 info = info->next_port;
539 } while (info);
540 #endif
541 if (irq && !done_work)
542 IRQ_timer[irq] = jiffies + 1500;
543 else
544 IRQ_timer[irq] = jiffies + IRQ_timeout[irq];
545 IRQ_active |= 1 << irq;
546 }
547 figure_RS_timer();
548 }
549
550
551
552
553
554
555
556
557
558
559
560 static inline void handle_rs_break(struct async_struct *info)
561 {
562 if (info->flags & ASYNC_SAK)
563 do_SAK(info->tty);
564
565 if (I_BRKINT(info->tty)) {
566 flush_input(info->tty);
567 flush_output(info->tty);
568 if (info->tty->pgrp > 0)
569 kill_pg(info->tty->pgrp, SIGINT,1);
570 }
571 }
572
573
574
575
576
577
578
579
580
581
582 static void do_softint(void *unused)
583 {
584 int i;
585 struct async_struct *info;
586
587 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
588 if (clear_bit(i, rs_event)) {
589 if (!info->tty)
590 continue;
591 if (clear_bit(RS_EVENT_READ_PROCESS, &info->event)) {
592 TTY_READ_FLUSH(info->tty);
593 }
594 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
595 wake_up_interruptible(&info->tty->write_q.proc_list);
596 }
597 if (clear_bit(RS_EVENT_HANGUP, &info->event)) {
598 tty_hangup(info->tty);
599 wake_up_interruptible(&info->open_wait);
600 info->flags &= ~(ASYNC_NORMAL_ACTIVE|
601 ASYNC_CALLOUT_ACTIVE);
602 }
603 if (clear_bit(RS_EVENT_BREAK, &info->event))
604 handle_rs_break(info);
605 if (clear_bit(RS_EVENT_OPEN_WAKEUP, &info->event)) {
606 wake_up_interruptible(&info->open_wait);
607 }
608 }
609 }
610 }
611
612
613
614
615
616
617
618
619 static void rs_timer(void)
620 {
621 int i, mask;
622 int timeout = 0;
623
624 for (i = 0, mask = 1; mask <= IRQ_active; i++, mask <<= 1) {
625 if ((mask & IRQ_active) && (IRQ_timer[i] <= jiffies)) {
626 IRQ_active &= ~mask;
627 cli();
628 #ifdef SERIAL_DEBUG_TIMER
629 printk("rs_timer: rs_interrupt(%d)...", i);
630 #endif
631 rs_interrupt(i);
632 sti();
633 }
634 if (mask & IRQ_active) {
635 if (!timeout || (IRQ_timer[i] < timeout))
636 timeout = IRQ_timer[i];
637 }
638 }
639 if (timeout) {
640 timer_table[RS_TIMER].expires = timeout;
641 timer_active |= 1 << RS_TIMER;
642 }
643 }
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660 static int grab_all_interrupts(int dontgrab)
661 {
662 int irq_lines = 0;
663 int i, mask;
664 struct sigaction sa;
665
666 sa.sa_handler = rs_probe;
667 sa.sa_flags = (SA_INTERRUPT);
668 sa.sa_mask = 0;
669 sa.sa_restorer = NULL;
670
671 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
672 if (!(mask & dontgrab) && !irqaction(i, &sa)) {
673 irq_lines |= mask;
674 }
675 }
676 return irq_lines;
677 }
678
679
680
681
682 static void free_all_interrupts(int irq_lines)
683 {
684 int i;
685
686 for (i = 0; i < 16; i++) {
687 if (irq_lines & (1 << i))
688 free_irq(i);
689 }
690 }
691
692
693
694
695
696
697 static void figure_IRQ_timeout(int irq)
698 {
699 struct async_struct *info;
700 int timeout = 6000;
701
702 info = IRQ_ports[irq];
703 if (!info) {
704 IRQ_timeout[irq] = 6000;
705 return;
706 }
707 while (info) {
708 if (info->timeout < timeout)
709 timeout = info->timeout;
710 info = info->next_port;
711 }
712 if (!irq)
713 timeout = timeout / 2;
714 IRQ_timeout[irq] = timeout ? timeout : 1;
715 }
716
717 static int startup(struct async_struct * info, int get_irq)
718 {
719 unsigned short ICP;
720 unsigned long flags;
721 struct sigaction sa;
722 int retval;
723
724 if (info->flags & ASYNC_INITIALIZED)
725 return 0;
726
727 if (!info->port || !info->type) {
728 if (info->tty)
729 set_bit(TTY_IO_ERROR, &info->tty->flags);
730 return 0;
731 }
732
733 save_flags(flags); cli();
734
735 #ifdef SERIAL_DEBUG_OPEN
736 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
737 #endif
738
739
740
741
742 if (get_irq && info->irq && !IRQ_ports[info->irq]) {
743 sa.sa_handler = rs_interrupt;
744 sa.sa_flags = (SA_INTERRUPT);
745 sa.sa_mask = 0;
746 sa.sa_restorer = NULL;
747 retval = irqaction(info->irq,&sa);
748 if (retval) {
749 restore_flags(flags);
750 return retval;
751 }
752 }
753
754
755
756
757
758 if (info->type == PORT_16550A) {
759 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
760 UART_FCR_CLEAR_XMIT));
761 info->xmit_fifo_size = 16;
762 } else
763 info->xmit_fifo_size = 1;
764
765
766
767
768 (void)serial_inp(info, UART_LSR);
769 (void)serial_inp(info, UART_RX);
770 (void)serial_inp(info, UART_IIR);
771 (void)serial_inp(info, UART_MSR);
772
773
774
775
776 serial_outp(info, UART_LCR, UART_LCR_WLEN8);
777 if (info->flags & ASYNC_FOURPORT)
778 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
779 else
780 serial_outp(info, UART_MCR,
781 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
782
783
784
785
786 #ifdef ISR_HACK
787 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
788 serial_outp(info, UART_IER, info->IER);
789 #else
790 info->IER = (UART_IER_MSI | UART_IER_RLSI |
791 UART_IER_THRI | UART_IER_RDI);
792 serial_outp(info, UART_IER, info->IER);
793 #endif
794 if (info->flags & ASYNC_FOURPORT) {
795
796 ICP = (info->port & 0xFE0) | 0x01F;
797 outb_p(0x80, ICP);
798 (void) inb_p(ICP);
799 }
800
801
802
803
804 (void)serial_inp(info, UART_LSR);
805 (void)serial_inp(info, UART_RX);
806 (void)serial_inp(info, UART_IIR);
807 (void)serial_inp(info, UART_MSR);
808
809 if (info->tty)
810 clear_bit(TTY_IO_ERROR, &info->tty->flags);
811
812
813
814 if (info->tty && info->tty->termios && I_INPCK(info->tty))
815 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
816 UART_LSR_FE | UART_LSR_PE);
817 else
818 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
819 UART_LSR_FE);
820
821
822
823
824 info->prev_port = 0;
825 info->next_port = IRQ_ports[info->irq];
826 if (info->next_port)
827 info->next_port->prev_port = info;
828 IRQ_ports[info->irq] = info;
829 figure_IRQ_timeout(info->irq);
830
831
832
833
834 IRQ_active |= 1 << info->irq;
835 figure_RS_timer();
836
837
838
839
840 change_speed(info->line);
841
842 info->flags |= ASYNC_INITIALIZED;
843 restore_flags(flags);
844 return 0;
845 }
846
847
848
849
850
851 static void shutdown(struct async_struct * info, int do_free_irq)
852 {
853 unsigned long flags;
854
855 if (!(info->flags & ASYNC_INITIALIZED))
856 return;
857
858 #ifdef SERIAL_DEBUG_OPEN
859 printk("Shutting down serial port %d (irq %d)....", info->line,
860 info->irq);
861 #endif
862
863 save_flags(flags); cli();
864
865
866
867
868 if (info->next_port)
869 info->next_port->prev_port = info->prev_port;
870 if (info->prev_port)
871 info->prev_port->next_port = info->next_port;
872 else
873 IRQ_ports[info->irq] = info->next_port;
874 figure_IRQ_timeout(info->irq);
875
876
877
878
879 if (do_free_irq && info->irq && !IRQ_ports[info->irq])
880 free_irq(info->irq);
881
882 info->IER = 0;
883 serial_outp(info, UART_IER, 0x00);
884 if (info->flags & ASYNC_FOURPORT) {
885
886 (void) inb((info->port & 0xFE0) | 0x01F);
887 }
888 if (info->tty && !(info->tty->termios->c_cflag & HUPCL))
889 serial_outp(info, UART_MCR, UART_MCR_DTR);
890 else
891
892 serial_outp(info, UART_MCR, 0x00);
893
894
895 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
896 UART_FCR_CLEAR_XMIT));
897 (void)serial_in(info, UART_RX);
898
899 if (info->tty)
900 set_bit(TTY_IO_ERROR, &info->tty->flags);
901
902 info->flags &= ~ASYNC_INITIALIZED;
903 restore_flags(flags);
904 }
905
906
907
908
909
910 static void change_speed(unsigned int line)
911 {
912 struct async_struct * info;
913 unsigned short port;
914 int quot = 0;
915 unsigned cflag,cval,mcr,fcr;
916 int i;
917
918 if (line >= NR_PORTS)
919 return;
920 info = rs_table + line;
921 if (!info->tty || !info->tty->termios)
922 return;
923 cflag = info->tty->termios->c_cflag;
924 if (!(port = info->port))
925 return;
926 i = cflag & CBAUD;
927 if (i == 15) {
928 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
929 i += 1;
930 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
931 i += 2;
932 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
933 quot = info->custom_divisor;
934 }
935 if (quot) {
936 info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
937 info->baud_base) + 2;
938 } else if (baud_table[i] == 134) {
939 quot = (2*info->baud_base / 269);
940 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
941 } else if (baud_table[i]) {
942 quot = info->baud_base / baud_table[i];
943 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
944 } else {
945 quot = 0;
946 info->timeout = 0;
947 }
948 cli();
949 mcr = serial_in(info, UART_MCR);
950 if (quot) {
951 serial_out(info, UART_MCR, mcr | UART_MCR_DTR);
952 } else {
953 serial_out(info, UART_MCR, mcr & ~UART_MCR_DTR);
954 sti();
955 return;
956 }
957 sti();
958
959 cval = cflag & (CSIZE | CSTOPB);
960 cval >>= 4;
961 if (cflag & PARENB)
962 cval |= UART_LCR_PARITY;
963 if (!(cflag & PARODD))
964 cval |= UART_LCR_EPAR;
965 if (info->type == PORT_16550A) {
966 if ((info->baud_base / quot) < 2400)
967 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
968 else
969 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
970 } else
971 fcr = 0;
972
973 cli();
974 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);
975 serial_outp(info, UART_DLL, quot & 0xff);
976 serial_outp(info, UART_DLM, quot >> 8);
977 serial_outp(info, UART_LCR, cval);
978 serial_outp(info, UART_FCR, fcr);
979 sti();
980 }
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995 static inline void restart_port(struct async_struct *info)
996 {
997 struct tty_queue * queue;
998 int head, tail, count;
999
1000 if (!info)
1001 return;
1002
1003 if (serial_inp(info, UART_LSR) & UART_LSR_THRE) {
1004 if (info->x_char) {
1005 serial_outp(info, UART_TX, info->x_char);
1006 info->x_char = 0;
1007 } else {
1008 queue = &info->tty->write_q;
1009 head = queue->head;
1010 tail = queue->tail;
1011 count = info->xmit_fifo_size;
1012 while (count--) {
1013 if (tail == head)
1014 break;
1015 serial_outp(info, UART_TX, queue->buf[tail++]);
1016 tail &= TTY_BUF_SIZE-1;
1017 }
1018 queue->tail = tail;
1019 }
1020 }
1021 }
1022
1023
1024
1025
1026
1027 void rs_write(struct tty_struct * tty)
1028 {
1029 struct async_struct *info;
1030
1031 if (!tty || tty->stopped || tty->hw_stopped)
1032 return;
1033 info = rs_table + DEV_TO_SL(tty->line);
1034 if (!info || !info->tty || !(info->flags & ASYNC_INITIALIZED))
1035 return;
1036 cli();
1037 restart_port(info);
1038 info->IER = (UART_IER_MSI | UART_IER_RLSI |
1039 UART_IER_THRI | UART_IER_RDI);
1040 #ifdef ISR_HACK
1041 serial_out(info, UART_IER, info->IER);
1042 #endif
1043 sti();
1044 }
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055 static void rs_throttle(struct tty_struct * tty, int status)
1056 {
1057 struct async_struct *info;
1058 unsigned char mcr;
1059 unsigned long flags;
1060
1061 save_flags(flags); cli();
1062 #if SERIAL_DEBUG_THROTTLE
1063 printk("throttle tty%d: %d (%d, %d)....\n", DEV_TO_SL(tty->line),
1064 status, LEFT(&tty->read_q), LEFT(&tty->secondary));
1065 #endif
1066 switch (status) {
1067 case TTY_THROTTLE_RQ_FULL:
1068 info = rs_table + DEV_TO_SL(tty->line);
1069 if (tty->termios->c_iflag & IXOFF) {
1070 info->x_char = STOP_CHAR(tty);
1071 } else {
1072 mcr = serial_inp(info, UART_MCR);
1073 mcr &= ~UART_MCR_RTS;
1074 serial_out(info, UART_MCR, mcr);
1075 }
1076 break;
1077 case TTY_THROTTLE_RQ_AVAIL:
1078 info = rs_table + DEV_TO_SL(tty->line);
1079 if (tty->termios->c_iflag & IXOFF) {
1080 if (info->x_char)
1081 info->x_char = 0;
1082 else
1083 info->x_char = START_CHAR(tty);
1084 } else {
1085 mcr = serial_in(info, UART_MCR);
1086 mcr |= UART_MCR_RTS;
1087 serial_out(info, UART_MCR, mcr);
1088 }
1089 break;
1090 }
1091 restore_flags(flags);
1092 }
1093
1094
1095
1096
1097
1098
1099
1100 static int get_serial_info(struct async_struct * info,
1101 struct serial_struct * retinfo)
1102 {
1103 struct serial_struct tmp;
1104
1105 if (!retinfo)
1106 return -EFAULT;
1107 memset(&tmp, 0, sizeof(tmp));
1108 tmp.type = info->type;
1109 tmp.line = info->line;
1110 tmp.port = info->port;
1111 tmp.irq = info->irq;
1112 tmp.flags = info->flags;
1113 tmp.baud_base = info->baud_base;
1114 tmp.close_delay = info->close_delay;
1115 tmp.custom_divisor = info->custom_divisor;
1116 tmp.hub6 = info->hub6;
1117 memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1118 return 0;
1119 }
1120
1121 static int set_serial_info(struct async_struct * info,
1122 struct serial_struct * new_info)
1123 {
1124 struct serial_struct new_serial;
1125 struct async_struct old_info;
1126 unsigned int i,change_irq,change_port;
1127 int retval;
1128 struct sigaction sa;
1129
1130 if (!new_info)
1131 return -EFAULT;
1132 memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1133 old_info = *info;
1134
1135 change_irq = new_serial.irq != info->irq;
1136 change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1137
1138 if (!suser()) {
1139 if (change_irq || change_port ||
1140 (new_serial.baud_base != info->baud_base) ||
1141 (new_serial.type != info->type) ||
1142 (new_serial.close_delay != info->close_delay) ||
1143 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1144 (info->flags & ~ASYNC_USR_MASK)))
1145 return -EPERM;
1146 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1147 (new_serial.flags & ASYNC_USR_MASK));
1148 info->custom_divisor = new_serial.custom_divisor;
1149 goto check_and_exit;
1150 }
1151
1152 if (new_serial.irq == 2)
1153 new_serial.irq = 9;
1154
1155 if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1156 (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1157 return -EINVAL;
1158 }
1159
1160
1161 for (i = 0 ; i < NR_PORTS; i++)
1162 if ((info != &rs_table[i]) &&
1163 (rs_table[i].port == new_serial.port) && rs_table[i].type)
1164 return -EADDRINUSE;
1165
1166
1167
1168
1169
1170
1171 if (new_serial.port && new_serial.type && new_serial.irq &&
1172 (change_irq || !(info->flags & ASYNC_INITIALIZED))) {
1173 if (!IRQ_ports[new_serial.irq]) {
1174 sa.sa_handler = rs_interrupt;
1175 sa.sa_flags = (SA_INTERRUPT);
1176 sa.sa_mask = 0;
1177 sa.sa_restorer = NULL;
1178 retval = irqaction(new_serial.irq,&sa);
1179 if (retval)
1180 return retval;
1181 }
1182 }
1183
1184 if ((change_port || change_irq) && (info->count > 1))
1185 return -EBUSY;
1186
1187
1188
1189
1190
1191
1192 info->baud_base = new_serial.baud_base;
1193 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1194 (new_serial.flags & ASYNC_FLAGS));
1195 info->custom_divisor = new_serial.custom_divisor;
1196 info->type = new_serial.type;
1197 info->close_delay = new_serial.close_delay;
1198
1199 if (change_port || change_irq) {
1200
1201
1202
1203
1204 shutdown(info, change_irq);
1205 info->irq = new_serial.irq;
1206 info->port = new_serial.port;
1207 info->hub6 = new_serial.hub6;
1208 }
1209
1210 check_and_exit:
1211 if (!info->port || !info->type)
1212 return 0;
1213 if (info->flags & ASYNC_INITIALIZED) {
1214 if (((old_info.flags & ASYNC_SPD_MASK) !=
1215 (info->flags & ASYNC_SPD_MASK)) ||
1216 (old_info.custom_divisor != info->custom_divisor))
1217 change_speed(info->line);
1218 } else
1219 (void) startup(info, 0);
1220 return 0;
1221 }
1222
1223 static int get_modem_info(struct async_struct * info, unsigned int *value)
1224 {
1225 unsigned char control, status;
1226 unsigned int result;
1227
1228 cli();
1229 control = serial_in(info, UART_MCR);
1230 status = serial_in(info, UART_MSR);
1231 sti();
1232 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1233 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1234 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1235 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1236 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1237 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1238 put_fs_long(result,(unsigned long *) value);
1239 return 0;
1240 }
1241
1242 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1243 unsigned int *value)
1244 {
1245 unsigned char control;
1246 unsigned int arg = get_fs_long((unsigned long *) value);
1247
1248 cli();
1249 control = serial_in(info, UART_MCR);
1250 sti();
1251
1252 switch (cmd) {
1253 case TIOCMBIS:
1254 if (arg & TIOCM_RTS)
1255 control |= UART_MCR_RTS;
1256 if (arg & TIOCM_DTR)
1257 control |= UART_MCR_DTR;
1258 break;
1259 case TIOCMBIC:
1260 if (arg & TIOCM_RTS)
1261 control &= ~UART_MCR_RTS;
1262 if (arg & TIOCM_DTR)
1263 control &= ~UART_MCR_DTR;
1264 break;
1265 case TIOCMSET:
1266 control = (control & ~(UART_MCR_RTS | UART_MCR_DTR))
1267 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1268 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0);
1269 break;
1270 default:
1271 return -EINVAL;
1272 }
1273 cli();
1274 serial_out(info, UART_MCR, control);
1275 sti();
1276 return 0;
1277 }
1278
1279 static int do_autoconfig(struct async_struct * info)
1280 {
1281 int retval;
1282
1283 if (!suser())
1284 return -EPERM;
1285
1286 if (info->count > 1)
1287 return -EBUSY;
1288
1289 shutdown(info, 1);
1290
1291 cli();
1292 autoconfig(info);
1293 sti();
1294
1295 retval = startup(info, 1);
1296 if (retval)
1297 return retval;
1298 return 0;
1299 }
1300
1301
1302
1303
1304
1305 static void send_break( struct async_struct * info, int duration)
1306 {
1307 if (!info->port)
1308 return;
1309 current->state = TASK_INTERRUPTIBLE;
1310 current->timeout = jiffies + duration;
1311 cli();
1312 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
1313 schedule();
1314 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1315 sti();
1316 }
1317
1318
1319
1320
1321
1322 static int check_wild_interrupts(int doprint)
1323 {
1324 int i, mask;
1325 int wild_interrupts = 0;
1326 int irq_lines;
1327 unsigned long timeout;
1328 unsigned long flags;
1329
1330
1331 save_flags(flags); sti();
1332
1333 irq_lines = grab_all_interrupts(0);
1334
1335
1336
1337
1338
1339 timeout = jiffies+10;
1340 while (timeout >= jiffies)
1341 ;
1342
1343 rs_triggered = 0;
1344
1345 timeout = jiffies+10;
1346 while (timeout >= jiffies)
1347 ;
1348
1349 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1350 if ((rs_triggered & (1 << i)) &&
1351 (irq_lines & (1 << i))) {
1352 wild_interrupts |= mask;
1353 if (doprint)
1354 printk("Wild interrupt? (IRQ %d)\n", i);
1355 }
1356 }
1357 free_all_interrupts(irq_lines);
1358 restore_flags(flags);
1359 return wild_interrupts;
1360 }
1361
1362 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1363 unsigned int cmd, unsigned long arg)
1364 {
1365 int error, line;
1366 struct async_struct * info;
1367
1368 line = DEV_TO_SL(tty->line);
1369 if (line < 0 || line >= NR_PORTS)
1370 return -ENODEV;
1371 info = rs_table + line;
1372
1373 switch (cmd) {
1374 case TCSBRK:
1375 if (!arg)
1376 send_break(info, HZ/4);
1377 return 0;
1378 case TCSBRKP:
1379 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1380 return 0;
1381 case TIOCGSOFTCAR:
1382 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1383 if (error)
1384 return error;
1385 put_fs_long(C_LOCAL(tty) ? 1 : 0,
1386 (unsigned long *) arg);
1387 return 0;
1388 case TIOCSSOFTCAR:
1389 arg = get_fs_long((unsigned long *) arg);
1390 tty->termios->c_cflag =
1391 ((tty->termios->c_cflag & ~CLOCAL) |
1392 (arg ? CLOCAL : 0));
1393 return 0;
1394 case TIOCMGET:
1395 error = verify_area(VERIFY_WRITE, (void *) arg,
1396 sizeof(unsigned int));
1397 if (error)
1398 return error;
1399 return get_modem_info(info, (unsigned int *) arg);
1400 case TIOCMBIS:
1401 case TIOCMBIC:
1402 case TIOCMSET:
1403 return set_modem_info(info, cmd, (unsigned int *) arg);
1404 case TIOCGSERIAL:
1405 error = verify_area(VERIFY_WRITE, (void *) arg,
1406 sizeof(struct serial_struct));
1407 if (error)
1408 return error;
1409 return get_serial_info(info,
1410 (struct serial_struct *) arg);
1411 case TIOCSSERIAL:
1412 return set_serial_info(info,
1413 (struct serial_struct *) arg);
1414 case TIOCSERCONFIG:
1415 return do_autoconfig(info);
1416
1417 case TIOCSERGWILD:
1418 error = verify_area(VERIFY_WRITE, (void *) arg,
1419 sizeof(int));
1420 if (error)
1421 return error;
1422 put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
1423 return 0;
1424
1425 case TIOCSERSWILD:
1426 if (!suser())
1427 return -EPERM;
1428 rs_wild_int_mask = get_fs_long((unsigned long *) arg);
1429 if (rs_wild_int_mask < 0)
1430 rs_wild_int_mask = check_wild_interrupts(0);
1431 return 0;
1432
1433 default:
1434 return -EINVAL;
1435 }
1436 return 0;
1437 }
1438
1439 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1440 {
1441 struct async_struct *info;
1442
1443 if (tty->termios->c_cflag == old_termios->c_cflag)
1444 return;
1445
1446 info = &rs_table[DEV_TO_SL(tty->line)];
1447
1448 change_speed(DEV_TO_SL(tty->line));
1449
1450 if ((old_termios->c_cflag & CRTSCTS) &&
1451 !(tty->termios->c_cflag & CRTSCTS)) {
1452 tty->hw_stopped = 0;
1453 rs_write(tty);
1454 }
1455
1456 if (!(old_termios->c_cflag & CLOCAL) &&
1457 (tty->termios->c_cflag & CLOCAL))
1458 wake_up_interruptible(&info->open_wait);
1459
1460 if (I_INPCK(tty))
1461 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
1462 UART_LSR_FE | UART_LSR_PE);
1463 else
1464 info->read_status_mask = (UART_LSR_OE | UART_LSR_BI |
1465 UART_LSR_FE);
1466 }
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478 static void rs_close(struct tty_struct *tty, struct file * filp)
1479 {
1480 struct async_struct * info;
1481 int line;
1482
1483 if (tty_hung_up_p(filp))
1484 return;
1485
1486 line = DEV_TO_SL(tty->line);
1487 if ((line < 0) || (line >= NR_PORTS))
1488 return;
1489 info = rs_table + line;
1490 #ifdef SERIAL_DEBUG_OPEN
1491 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1492 #endif
1493 if ((tty->count == 1) && (info->count != 1)) {
1494
1495
1496
1497
1498
1499
1500
1501 printk("rs_close: bad serial port count; tty->count is 1, "
1502 "info->count is %d\n", info->count);
1503 info->count = 1;
1504 }
1505 if (--info->count < 0) {
1506 printk("rs_close: bad serial port count for ttys%d: %d\n",
1507 info->line, info->count);
1508 info->count = 0;
1509 }
1510 if (info->count)
1511 return;
1512 info->flags |= ASYNC_CLOSING;
1513
1514
1515
1516
1517 if (info->flags & ASYNC_NORMAL_ACTIVE)
1518 info->normal_termios = *tty->termios;
1519 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1520 info->callout_termios = *tty->termios;
1521 tty->stopped = 0;
1522 tty->hw_stopped = 0;
1523 if (info->flags & ASYNC_INITIALIZED) {
1524 rs_start(tty);
1525
1526
1527
1528
1529 wait_until_sent(tty);
1530 } else
1531 flush_output(tty);
1532 flush_input(tty);
1533 cli();
1534
1535
1536
1537
1538 if (!(serial_inp(info, UART_LSR) & UART_LSR_THRE)) {
1539 rs_start(tty);
1540 interruptible_sleep_on(&info->xmit_wait);
1541 }
1542 sti();
1543 shutdown(info, 1);
1544 clear_bit(line, rs_event);
1545 info->event = 0;
1546 info->tty = 0;
1547 if (info->blocked_open) {
1548 if (info->close_delay) {
1549 tty->count++;
1550 current->state = TASK_INTERRUPTIBLE;
1551 current->timeout = jiffies + info->close_delay;
1552 schedule();
1553 tty->count--;
1554 }
1555 wake_up_interruptible(&info->open_wait);
1556 }
1557 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1558 ASYNC_CLOSING);
1559 wake_up_interruptible(&info->close_wait);
1560 }
1561
1562
1563
1564
1565 void rs_hangup(struct tty_struct *tty)
1566 {
1567 struct async_struct * info;
1568 int line;
1569
1570 line = DEV_TO_SL(tty->line);
1571 if ((line < 0) || (line >= NR_PORTS))
1572 return;
1573 info = rs_table + line;
1574
1575 shutdown(info, 1);
1576 clear_bit(line, rs_event);
1577 info->event = 0;
1578 info->count = 0;
1579 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1580 info->tty = 0;
1581 wake_up_interruptible(&info->open_wait);
1582 }
1583
1584
1585
1586
1587
1588
1589 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1590 struct async_struct *info)
1591 {
1592 struct wait_queue wait = { current, NULL };
1593 int retval;
1594 int do_clocal = C_LOCAL(tty);
1595
1596
1597
1598
1599
1600 if (info->flags & ASYNC_CLOSING) {
1601 interruptible_sleep_on(&info->close_wait);
1602 #ifdef SERIAL_DO_RESTART
1603 if (info->flags & ASYNC_HUP_NOTIFY)
1604 return -EAGAIN;
1605 else
1606 return -ERESTARTSYS;
1607 #else
1608 return -EAGAIN;
1609 #endif
1610 }
1611
1612
1613
1614
1615
1616 if (MAJOR(filp->f_rdev) == TTYAUX_MAJOR) {
1617 if (info->flags & ASYNC_NORMAL_ACTIVE)
1618 return -EBUSY;
1619 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1620 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1621 (info->session != current->session))
1622 return -EBUSY;
1623 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1624 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1625 (info->pgrp != current->pgrp))
1626 return -EBUSY;
1627 info->flags |= ASYNC_CALLOUT_ACTIVE;
1628 return 0;
1629 }
1630
1631
1632
1633
1634
1635 if (filp->f_flags & O_NONBLOCK) {
1636 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1637 return -EBUSY;
1638 info->flags |= ASYNC_NORMAL_ACTIVE;
1639 return 0;
1640 }
1641
1642
1643
1644
1645
1646
1647
1648
1649 retval = 0;
1650 add_wait_queue(&info->open_wait, &wait);
1651 #ifdef SERIAL_DEBUG_OPEN
1652 printk("block_til_ready before block: ttys%d, count = %d\n",
1653 info->line, info->count);
1654 #endif
1655 info->count--;
1656 info->blocked_open++;
1657 while (1) {
1658 cli();
1659 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
1660 serial_out(info, UART_MCR,
1661 serial_inp(info, UART_MCR) |
1662 (UART_MCR_DTR | UART_MCR_RTS));
1663 sti();
1664 current->state = TASK_INTERRUPTIBLE;
1665 if (tty_hung_up_p(filp) ||
1666 !(info->flags & ASYNC_INITIALIZED)) {
1667 #ifdef SERIAL_DO_RESTART
1668 if (info->flags & ASYNC_HUP_NOTIFY)
1669 retval = -EAGAIN;
1670 else
1671 retval = -ERESTARTSYS;
1672 #else
1673 retval = -EAGAIN;
1674 #endif
1675 break;
1676 }
1677 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1678 !(info->flags & ASYNC_CLOSING) &&
1679 (do_clocal || (serial_in(info, UART_MSR) &
1680 UART_MSR_DCD)))
1681 break;
1682 if (current->signal & ~current->blocked) {
1683 retval = -ERESTARTSYS;
1684 break;
1685 }
1686 #ifdef SERIAL_DEBUG_OPEN
1687 printk("block_til_ready blocking: ttys%d, count = %d\n",
1688 info->line, info->count);
1689 #endif
1690 schedule();
1691 }
1692 current->state = TASK_RUNNING;
1693 remove_wait_queue(&info->open_wait, &wait);
1694 if (!tty_hung_up_p(filp))
1695 info->count++;
1696 info->blocked_open--;
1697 #ifdef SERIAL_DEBUG_OPEN
1698 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1699 info->line, info->count);
1700 #endif
1701 if (retval)
1702 return retval;
1703 info->flags |= ASYNC_NORMAL_ACTIVE;
1704 return 0;
1705 }
1706
1707
1708
1709
1710
1711
1712
1713 int rs_open(struct tty_struct *tty, struct file * filp)
1714 {
1715 struct async_struct *info;
1716 int retval, line;
1717
1718 line = DEV_TO_SL(tty->line);
1719 if ((line < 0) || (line >= NR_PORTS))
1720 return -ENODEV;
1721 info = rs_table + line;
1722 #ifdef SERIAL_DEBUG_OPEN
1723 printk("rs_open ttys%d, count = %d\n", info->line, info->count);
1724 #endif
1725 info->count++;
1726 info->tty = tty;
1727
1728 tty->write = rs_write;
1729 tty->close = rs_close;
1730 tty->ioctl = rs_ioctl;
1731 tty->throttle = rs_throttle;
1732 tty->set_termios = rs_set_termios;
1733 tty->stop = rs_stop;
1734 tty->start = rs_start;
1735 tty->hangup = rs_hangup;
1736 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1737 if (MAJOR(filp->f_rdev) == TTY_MAJOR)
1738 *tty->termios = info->normal_termios;
1739 else
1740 *tty->termios = info->callout_termios;
1741 }
1742
1743
1744
1745 retval = startup(info, 1);
1746 if (retval)
1747 return retval;
1748
1749 retval = block_til_ready(tty, filp, info);
1750 if (retval) {
1751 #ifdef SERIAL_DEBUG_OPEN
1752 printk("rs_open returning after block_til_ready with %d\n",
1753 retval);
1754 #endif
1755 return retval;
1756 }
1757
1758 info->session = current->session;
1759 info->pgrp = current->pgrp;
1760
1761 #ifdef SERIAL_DEBUG_OPEN
1762 printk("rs_open ttys%d successful...", info->line);
1763 #endif
1764 return 0;
1765 }
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780 static void show_serial_version(void)
1781 {
1782 printk("Serial driver version 3.99a with");
1783 #ifdef CONFIG_AST_FOURPORT
1784 printk(" AST_FOURPORT");
1785 #define SERIAL_OPT
1786 #endif
1787 #ifdef CONFIG_ACCENT_ASYNC
1788 printk(" ACCENT_ASYNC");
1789 #define SERIAL_OPT
1790 #endif
1791 #ifdef CONFIG_HUB6
1792 printk(" HUB-6");
1793 #define SERIAL_OPT
1794 #endif
1795 #ifdef CONFIG_AUTO_IRQ
1796 printk (" AUTO_IRQ");
1797 #define SERIAL_OPT
1798 #endif
1799 #ifdef SERIAL_OPT
1800 printk(" enabled\n");
1801 #else
1802 printk(" no serial options enabled\n");
1803 #endif
1804 #undef SERIAL_OPT
1805 }
1806
1807
1808
1809
1810
1811
1812 static int get_auto_irq(struct async_struct *info)
1813 {
1814 unsigned char save_MCR, save_IER, save_ICP=0;
1815 unsigned short ICP=0, port = info->port;
1816 unsigned long timeout;
1817
1818
1819
1820
1821 rs_irq_triggered = 0;
1822 cli();
1823 save_IER = serial_inp(info, UART_IER);
1824 save_MCR = serial_inp(info, UART_MCR);
1825 if (info->flags & ASYNC_FOURPORT) {
1826 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
1827 serial_outp(info, UART_IER, 0x0f);
1828 ICP = (port & 0xFE0) | 0x01F;
1829 save_ICP = inb_p(ICP);
1830 outb_p(0x80, ICP);
1831 (void) inb_p(ICP);
1832 } else {
1833 serial_outp(info, UART_MCR,
1834 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
1835 serial_outp(info, UART_IER, 0x0f);
1836 }
1837 sti();
1838
1839
1840
1841 (void)serial_inp(info, UART_LSR);
1842 (void)serial_inp(info, UART_RX);
1843 (void)serial_inp(info, UART_IIR);
1844 (void)serial_inp(info, UART_MSR);
1845
1846 timeout = jiffies+2;
1847 while (timeout >= jiffies) {
1848 if (rs_irq_triggered)
1849 break;
1850 }
1851
1852
1853
1854 cli();
1855 serial_outp(info, UART_IER, save_IER);
1856 serial_outp(info, UART_MCR, save_MCR);
1857 if (info->flags & ASYNC_FOURPORT)
1858 outb_p(save_ICP, ICP);
1859 sti();
1860 return(rs_irq_triggered);
1861 }
1862
1863
1864
1865
1866
1867 static int do_auto_irq(struct async_struct * info)
1868 {
1869 unsigned port = info->port;
1870 int irq_lines = 0;
1871 int irq_try_1 = 0, irq_try_2 = 0;
1872 int retries;
1873 unsigned long flags;
1874
1875 if (!port)
1876 return 0;
1877
1878
1879 save_flags(flags); sti();
1880
1881 irq_lines = grab_all_interrupts(rs_wild_int_mask);
1882
1883 for (retries = 0; retries < 5; retries++) {
1884 if (!irq_try_1)
1885 irq_try_1 = get_auto_irq(info);
1886 if (!irq_try_2)
1887 irq_try_2 = get_auto_irq(info);
1888 if (irq_try_1 && irq_try_2) {
1889 if (irq_try_1 == irq_try_2)
1890 break;
1891 irq_try_1 = irq_try_2 = 0;
1892 }
1893 }
1894 restore_flags(flags);
1895 free_all_interrupts(irq_lines);
1896 return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
1897 }
1898
1899
1900
1901
1902
1903
1904
1905
1906 static void autoconfig(struct async_struct * info)
1907 {
1908 unsigned char status1, status2, scratch, scratch2;
1909 unsigned port = info->port;
1910 unsigned long flags;
1911
1912 info->type = PORT_UNKNOWN;
1913
1914 if (!port)
1915 return;
1916
1917 save_flags(flags); cli();
1918
1919
1920
1921
1922
1923 scratch = serial_inp(info, UART_IER);
1924 serial_outp(info, UART_IER, 0);
1925 scratch2 = serial_inp(info, UART_IER);
1926 serial_outp(info, UART_IER, scratch);
1927 if (scratch2) {
1928 restore_flags(flags);
1929 return;
1930 }
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941 if (!(info->flags & ASYNC_SKIP_TEST)) {
1942 scratch = serial_inp(info, UART_MCR);
1943 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
1944 scratch2 = serial_inp(info, UART_MSR);
1945 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
1946 status1 = serial_inp(info, UART_MSR) & 0xF0;
1947 serial_outp(info, UART_MCR, scratch);
1948 serial_outp(info, UART_MSR, scratch2);
1949 if (status1 != 0x90) {
1950 restore_flags(flags);
1951 return;
1952 }
1953 }
1954
1955
1956
1957
1958
1959 if (info->flags & ASYNC_AUTO_IRQ)
1960 info->irq = do_auto_irq(info);
1961
1962 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
1963 scratch = serial_in(info, UART_IIR) >> 6;
1964 info->xmit_fifo_size = 1;
1965 switch (scratch) {
1966 case 0:
1967 info->type = PORT_16450;
1968 break;
1969 case 1:
1970 info->type = PORT_UNKNOWN;
1971 break;
1972 case 2:
1973 info->type = PORT_16550;
1974 break;
1975 case 3:
1976 info->type = PORT_16550A;
1977 info->xmit_fifo_size = 16;
1978 break;
1979 }
1980 if (info->type == PORT_16450) {
1981 scratch = serial_in(info, UART_SCR);
1982 serial_outp(info, UART_SCR, 0xa5);
1983 status1 = serial_in(info, UART_SCR);
1984 serial_outp(info, UART_SCR, 0x5a);
1985 status2 = serial_in(info, UART_SCR);
1986 serial_outp(info, UART_SCR, scratch);
1987
1988 if ((status1 != 0xa5) || (status2 != 0x5a))
1989 info->type = PORT_8250;
1990 }
1991
1992
1993
1994
1995 serial_outp(info, UART_MCR, 0x00);
1996 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1997 UART_FCR_CLEAR_XMIT));
1998 (void)serial_in(info, UART_RX);
1999
2000 restore_flags(flags);
2001 }
2002
2003
2004
2005
2006 long rs_init(long kmem_start)
2007 {
2008 int i;
2009 struct async_struct * info;
2010
2011 memset(&rs_event, 0, sizeof(rs_event));
2012 bh_base[SERIAL_BH].routine = do_softint;
2013 timer_table[RS_TIMER].fn = rs_timer;
2014 timer_table[RS_TIMER].expires = 0;
2015 IRQ_active = 0;
2016 #ifdef CONFIG_AUTO_IRQ
2017 rs_wild_int_mask = check_wild_interrupts(1);
2018 #endif
2019
2020 for (i = 0; i < 16; i++) {
2021 IRQ_ports[i] = 0;
2022 IRQ_timeout[i] = 0;
2023 }
2024
2025 show_serial_version();
2026 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
2027 info->line = i;
2028 info->tty = 0;
2029 info->type = PORT_UNKNOWN;
2030 info->custom_divisor = 0;
2031 info->close_delay = 50;
2032 info->x_char = 0;
2033 info->event = 0;
2034 info->count = 0;
2035 info->blocked_open = 0;
2036 memset(&info->callout_termios, 0, sizeof(struct termios));
2037 memset(&info->normal_termios, 0, sizeof(struct termios));
2038 info->open_wait = 0;
2039 info->xmit_wait = 0;
2040 info->close_wait = 0;
2041 info->next_port = 0;
2042 info->prev_port = 0;
2043 if (info->irq == 2)
2044 info->irq = 9;
2045 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2046 continue;
2047 autoconfig(info);
2048 if (info->type == PORT_UNKNOWN)
2049 continue;
2050 printk("tty%02d%s at 0x%04x (irq = %d)", info->line,
2051 (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2052 info->port, info->irq);
2053 switch (info->type) {
2054 case PORT_8250:
2055 printk(" is a 8250\n");
2056 break;
2057 case PORT_16450:
2058 printk(" is a 16450\n");
2059 break;
2060 case PORT_16550:
2061 printk(" is a 16550\n");
2062 break;
2063 case PORT_16550A:
2064 printk(" is a 16550A\n");
2065 break;
2066 default:
2067 printk("\n");
2068 break;
2069 }
2070 }
2071 return kmem_start;
2072 }
2073