This source file includes following definitions.
- serial_paranoia_check
- serial_in
- serial_inp
- serial_out
- serial_outp
- rs_stop
- rs_start
- rs_probe
- rs_sched_event
- receive_chars
- transmit_chars
- check_modem_status
- rs_interrupt
- rs_interrupt_single
- rs_interrupt
- rs_interrupt_single
- do_serial_bh
- do_softint
- rs_timer
- grab_all_interrupts
- free_all_interrupts
- figure_IRQ_timeout
- startup
- shutdown
- change_speed
- rs_put_char
- rs_flush_chars
- rs_write
- rs_write_room
- rs_chars_in_buffer
- rs_flush_buffer
- rs_throttle
- rs_unthrottle
- get_serial_info
- set_serial_info
- get_lsr_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
- register_serial
- unregister_serial
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/interrupt.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/serial.h>
28 #include <linux/serial_reg.h>
29 #include <linux/config.h>
30 #include <linux/major.h>
31 #include <linux/string.h>
32 #include <linux/fcntl.h>
33 #include <linux/ptrace.h>
34 #include <linux/major.h>
35
36 #include <asm/system.h>
37 #include <asm/io.h>
38 #include <asm/segment.h>
39 #include <asm/bitops.h>
40
41 DECLARE_TASK_QUEUE(tq_serial);
42
43 struct tty_driver serial_driver, callout_driver;
44 static int serial_refcount;
45
46
47 #define SERIAL_TYPE_NORMAL 1
48 #define SERIAL_TYPE_CALLOUT 2
49
50
51 #define WAKEUP_CHARS 256
52
53
54
55
56
57
58
59
60
61
62
63
64
65 #define SERIAL_PARANOIA_CHECK
66 #define CONFIG_SERIAL_NOPAUSE_IO
67 #define SERIAL_DO_RESTART
68 #define CONFIG_SERIAL_NEW_ISR
69
70 #undef SERIAL_DEBUG_INTR
71 #undef SERIAL_DEBUG_OPEN
72 #undef SERIAL_DEBUG_FLOW
73
74 #define _INLINE_ inline
75
76
77
78
79
80
81 static struct async_struct *IRQ_ports[16];
82 static int IRQ_timeout[16];
83 static volatile int rs_irq_triggered;
84 static volatile int rs_triggered;
85 static int rs_wild_int_mask;
86
87 static void autoconfig(struct async_struct * info);
88 static void change_speed(struct async_struct *info);
89
90
91
92
93
94
95
96
97 #define BASE_BAUD ( 1843200 / 16 )
98
99
100 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST )
101 #define STD_COM4_FLAGS ASYNC_BOOT_AUTOCONF
102
103 #define FOURPORT_FLAGS ASYNC_FOURPORT
104 #define ACCENT_FLAGS 0
105 #define BOCA_FLAGS 0
106 #define HUB6_FLAGS 0
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 #define C_P(card,port) (((card)<<6|(port)<<3) + 1)
123
124 struct async_struct rs_table[] = {
125
126 { 0, BASE_BAUD, 0x3F8, 4, STD_COM_FLAGS },
127 { 0, BASE_BAUD, 0x2F8, 3, STD_COM_FLAGS },
128 { 0, BASE_BAUD, 0x3E8, 4, STD_COM_FLAGS },
129 { 0, BASE_BAUD, 0x2E8, 3, STD_COM4_FLAGS },
130
131 { 0, BASE_BAUD, 0x1A0, 9, FOURPORT_FLAGS },
132 { 0, BASE_BAUD, 0x1A8, 9, FOURPORT_FLAGS },
133 { 0, BASE_BAUD, 0x1B0, 9, FOURPORT_FLAGS },
134 { 0, BASE_BAUD, 0x1B8, 9, FOURPORT_FLAGS },
135
136 { 0, BASE_BAUD, 0x2A0, 5, FOURPORT_FLAGS },
137 { 0, BASE_BAUD, 0x2A8, 5, FOURPORT_FLAGS },
138 { 0, BASE_BAUD, 0x2B0, 5, FOURPORT_FLAGS },
139 { 0, BASE_BAUD, 0x2B8, 5, FOURPORT_FLAGS },
140
141 { 0, BASE_BAUD, 0x330, 4, ACCENT_FLAGS },
142 { 0, BASE_BAUD, 0x338, 4, ACCENT_FLAGS },
143 { 0, BASE_BAUD, 0x000, 0, 0 },
144 { 0, BASE_BAUD, 0x000, 0, 0 },
145
146 { 0, BASE_BAUD, 0x100, 12, BOCA_FLAGS },
147 { 0, BASE_BAUD, 0x108, 12, BOCA_FLAGS },
148 { 0, BASE_BAUD, 0x110, 12, BOCA_FLAGS },
149 { 0, BASE_BAUD, 0x118, 12, BOCA_FLAGS },
150 { 0, BASE_BAUD, 0x120, 12, BOCA_FLAGS },
151 { 0, BASE_BAUD, 0x128, 12, BOCA_FLAGS },
152 { 0, BASE_BAUD, 0x130, 12, BOCA_FLAGS },
153 { 0, BASE_BAUD, 0x138, 12, BOCA_FLAGS },
154 { 0, BASE_BAUD, 0x140, 12, BOCA_FLAGS },
155 { 0, BASE_BAUD, 0x148, 12, BOCA_FLAGS },
156 { 0, BASE_BAUD, 0x150, 12, BOCA_FLAGS },
157 { 0, BASE_BAUD, 0x158, 12, BOCA_FLAGS },
158 { 0, BASE_BAUD, 0x160, 12, BOCA_FLAGS },
159 { 0, BASE_BAUD, 0x168, 12, BOCA_FLAGS },
160 { 0, BASE_BAUD, 0x170, 12, BOCA_FLAGS },
161 { 0, BASE_BAUD, 0x178, 12, BOCA_FLAGS },
162
163
164
165
166 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,0) },
167 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,1) },
168 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,2) },
169 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,3) },
170 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,4) },
171 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(0,5) },
172 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,0) },
173 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,1) },
174 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,2) },
175 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,3) },
176 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,4) },
177 { 0, BASE_BAUD, 0x302, 3, HUB6_FLAGS, C_P(1,5) },
178 };
179
180 #define NR_PORTS (sizeof(rs_table)/sizeof(struct async_struct))
181
182 static struct tty_struct *serial_table[NR_PORTS];
183 static struct termios *serial_termios[NR_PORTS];
184 static struct termios *serial_termios_locked[NR_PORTS];
185
186 #ifndef MIN
187 #define MIN(a,b) ((a) < (b) ? (a) : (b))
188 #endif
189
190
191
192
193
194
195
196
197
198
199 static unsigned char *tmp_buf = 0;
200 static struct semaphore tmp_buf_sem = MUTEX;
201
202 static inline int serial_paranoia_check(struct async_struct *info,
203 dev_t device, const char *routine)
204 {
205 #ifdef SERIAL_PARANOIA_CHECK
206 static const char *badmagic =
207 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
208 static const char *badinfo =
209 "Warning: null async_struct for (%d, %d) in %s\n";
210
211 if (!info) {
212 printk(badinfo, MAJOR(device), MINOR(device), routine);
213 return 1;
214 }
215 if (info->magic != SERIAL_MAGIC) {
216 printk(badmagic, MAJOR(device), MINOR(device), routine);
217 return 1;
218 }
219 #endif
220 return 0;
221 }
222
223
224
225
226 static int baud_table[] = {
227 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
228 9600, 19200, 38400, 57600, 115200, 0 };
229
230 static inline unsigned int serial_in(struct async_struct *info, int offset)
231 {
232 #ifdef CONFIG_HUB6
233 if (info->hub6) {
234 outb(info->hub6 - 1 + offset, info->port);
235 return inb(info->port+1);
236 } else
237 #endif
238 return inb(info->port + offset);
239 }
240
241 static inline unsigned int serial_inp(struct async_struct *info, int offset)
242 {
243 #ifdef CONFIG_HUB6
244 if (info->hub6) {
245 outb(info->hub6 - 1 + offset, info->port);
246 return inb_p(info->port+1);
247 } else
248 #endif
249 #ifdef CONFIG_SERIAL_NOPAUSE_IO
250 return inb(info->port + offset);
251 #else
252 return inb_p(info->port + offset);
253 #endif
254 }
255
256 static inline void serial_out(struct async_struct *info, int offset, int value)
257 {
258 #ifdef CONFIG_HUB6
259 if (info->hub6) {
260 outb(info->hub6 - 1 + offset, info->port);
261 outb(value, info->port+1);
262 } else
263 #endif
264 outb(value, info->port+offset);
265 }
266
267 static inline void serial_outp(struct async_struct *info, int offset,
268 int value)
269 {
270 #ifdef CONFIG_HUB6
271 if (info->hub6) {
272 outb(info->hub6 - 1 + offset, info->port);
273 outb_p(value, info->port+1);
274 } else
275 #endif
276 #ifdef CONFIG_SERIAL_NOPAUSE_IO
277 outb(value, info->port+offset);
278 #else
279 outb_p(value, info->port+offset);
280 #endif
281 }
282
283
284
285
286
287
288
289
290
291 static void rs_stop(struct tty_struct *tty)
292 {
293 struct async_struct *info = (struct async_struct *)tty->driver_data;
294 unsigned long flags;
295
296 if (serial_paranoia_check(info, tty->device, "rs_stop"))
297 return;
298
299 save_flags(flags); cli();
300 if (info->IER & UART_IER_THRI) {
301 info->IER &= ~UART_IER_THRI;
302 serial_out(info, UART_IER, info->IER);
303 }
304 restore_flags(flags);
305 }
306
307 static void rs_start(struct tty_struct *tty)
308 {
309 struct async_struct *info = (struct async_struct *)tty->driver_data;
310 unsigned long flags;
311
312 if (serial_paranoia_check(info, tty->device, "rs_start"))
313 return;
314
315 save_flags(flags); cli();
316 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
317 info->IER |= UART_IER_THRI;
318 serial_out(info, UART_IER, info->IER);
319 }
320 restore_flags(flags);
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 static void rs_probe(int irq)
349 {
350 rs_irq_triggered = irq;
351 rs_triggered |= 1 << irq;
352 return;
353 }
354
355
356
357
358
359 static _INLINE_ void rs_sched_event(struct async_struct *info,
360 int event)
361 {
362 info->event |= 1 << event;
363 queue_task_irq_off(&info->tqueue, &tq_serial);
364 mark_bh(SERIAL_BH);
365 }
366
367 static _INLINE_ void receive_chars(struct async_struct *info,
368 int *status)
369 {
370 struct tty_struct *tty = info->tty;
371 unsigned char ch;
372
373 do {
374 ch = serial_inp(info, UART_RX);
375 if (*status & info->ignore_status_mask)
376 goto ignore_char;
377 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
378 break;
379 tty->flip.count++;
380 if (*status & (UART_LSR_BI)) {
381 printk("handling break....");
382 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
383 if (info->flags & ASYNC_SAK)
384 do_SAK(tty);
385 } else if (*status & UART_LSR_PE)
386 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
387 else if (*status & UART_LSR_FE)
388 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
389 else if (*status & UART_LSR_OE)
390 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
391 else
392 *tty->flip.flag_buf_ptr++ = 0;
393 *tty->flip.char_buf_ptr++ = ch;
394 ignore_char:
395 *status = serial_inp(info, UART_LSR) & info->read_status_mask;
396 } while (*status & UART_LSR_DR);
397 queue_task_irq_off(&tty->flip.tqueue, &tq_timer);
398 #ifdef SERIAL_DEBUG_INTR
399 printk("DR...");
400 #endif
401 }
402
403 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
404 {
405 int count;
406
407 if (info->x_char) {
408 serial_outp(info, UART_TX, info->x_char);
409 info->x_char = 0;
410 if (intr_done)
411 *intr_done = 0;
412 return;
413 }
414 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
415 info->tty->hw_stopped) {
416 info->IER &= ~UART_IER_THRI;
417 #ifdef CONFIG_SERIAL_NEW_ISR
418 serial_out(info, UART_IER, info->IER);
419 #endif
420 return;
421 }
422
423 count = info->xmit_fifo_size;
424 do {
425 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
426 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
427 if (--info->xmit_cnt <= 0)
428 break;
429 } while (--count > 0);
430
431 if (info->xmit_cnt < WAKEUP_CHARS)
432 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
433
434 #ifdef SERIAL_DEBUG_INTR
435 printk("THRE...");
436 #endif
437 if (intr_done)
438 *intr_done = 0;
439
440 if (info->xmit_cnt <= 0) {
441 info->IER &= ~UART_IER_THRI;
442 #ifdef CONFIG_SERIAL_NEW_ISR
443 serial_out(info, UART_IER, info->IER);
444 #endif
445 }
446 }
447
448 static _INLINE_ void check_modem_status(struct async_struct *info)
449 {
450 int status;
451
452 status = serial_in(info, UART_MSR);
453
454 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
455 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
456 printk("ttys%d CD now %s...", info->line,
457 (status & UART_MSR_DCD) ? "on" : "off");
458 #endif
459 if (status & UART_MSR_DCD)
460 wake_up_interruptible(&info->open_wait);
461 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
462 (info->flags & ASYNC_CALLOUT_NOHUP))) {
463 #ifdef SERIAL_DEBUG_OPEN
464 printk("scheduling hangup...");
465 #endif
466 rs_sched_event(info, RS_EVENT_HANGUP);
467 }
468 }
469 if (info->flags & ASYNC_CTS_FLOW) {
470 if (info->tty->hw_stopped) {
471 if (status & UART_MSR_CTS) {
472 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
473 printk("CTS tx start...");
474 #endif
475 info->tty->hw_stopped = 0;
476 info->IER |= UART_IER_THRI;
477 #ifdef CONFIG_SERIAL_NEW_ISR
478 serial_out(info, UART_IER, info->IER);
479 #endif
480 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
481 return;
482 }
483 } else {
484 if (!(status & UART_MSR_CTS)) {
485 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
486 printk("CTS tx stop...");
487 #endif
488 info->tty->hw_stopped = 1;
489 info->IER &= ~UART_IER_THRI;
490 #ifdef CONFIG_SERIAL_NEW_ISR
491 serial_out(info, UART_IER, info->IER);
492 #endif
493 }
494 }
495 }
496 }
497
498 #ifdef CONFIG_SERIAL_NEW_ISR
499
500
501
502 static void rs_interrupt(int irq)
503 {
504 int status;
505 struct async_struct * info;
506 int pass_counter = 0;
507 struct async_struct *end_mark = 0;
508
509 #ifdef SERIAL_DEBUG_INTR
510 printk("rs_interrupt(%d)...", irq);
511 #endif
512
513 info = IRQ_ports[irq];
514 if (!info)
515 return;
516
517 do {
518 if (!info->tty ||
519 (serial_in(info, UART_IIR) & UART_IIR_NO_INT)) {
520 if (!end_mark)
521 end_mark = info;
522 goto next;
523 }
524 end_mark = 0;
525
526 info->last_active = jiffies;
527
528 status = serial_inp(info, UART_LSR) & info->read_status_mask;
529 #ifdef SERIAL_DEBUG_INTR
530 printk("status = %x...", status);
531 #endif
532 if (status & UART_LSR_DR)
533 receive_chars(info, &status);
534 check_modem_status(info);
535 if (status & UART_LSR_THRE)
536 transmit_chars(info, 0);
537
538 next:
539 info = info->next_port;
540 if (!info) {
541 info = IRQ_ports[irq];
542 if (pass_counter++ > 64) {
543 #if 0
544 printk("rs loop break\n");
545 #endif
546 break;
547 }
548 continue;
549 }
550 } while (end_mark != info);
551 #ifdef SERIAL_DEBUG_INTR
552 printk("end.\n");
553 #endif
554 }
555
556
557
558
559 static void rs_interrupt_single(int irq)
560 {
561 int status;
562 int pass_counter = 0;
563 struct async_struct * info;
564
565 #ifdef SERIAL_DEBUG_INTR
566 printk("rs_interrupt_single(%d)...", irq);
567 #endif
568
569 info = IRQ_ports[irq];
570 if (!info || !info->tty)
571 return;
572
573 do {
574 status = serial_inp(info, UART_LSR) & info->read_status_mask;
575 #ifdef SERIAL_DEBUG_INTR
576 printk("status = %x...", status);
577 #endif
578 if (status & UART_LSR_DR)
579 receive_chars(info, &status);
580 check_modem_status(info);
581 if (status & UART_LSR_THRE)
582 transmit_chars(info, 0);
583 if (pass_counter++ > 64) {
584 #if 0
585 printk("rs_single loop break.\n");
586 #endif
587 break;
588 }
589 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
590 info->last_active = jiffies;
591 #ifdef SERIAL_DEBUG_INTR
592 printk("end.\n");
593 #endif
594 }
595
596 #else
597
598
599
600
601 static void rs_interrupt(int irq)
602 {
603 int status;
604 struct async_struct * info;
605 int done = 1, pass_counter = 0;
606
607
608 #ifdef SERIAL_DEBUG_INTR
609 printk("rs_interrupt(%d)...", irq);
610 #endif
611
612 info = IRQ_ports[irq];
613 if (!info)
614 return;
615
616 while (1) {
617 if (!info->tty)
618 goto next;
619
620 serial_outp(info, UART_IER, 0);
621 status = serial_inp(info, UART_LSR) & info->read_status_mask;
622 if (status & UART_LSR_DR) {
623 receive_chars(info, &status);
624 done = 0;
625 }
626 check_modem_status(info);
627 if (status & UART_LSR_THRE)
628 transmit_chars(info, &done);
629
630 next:
631 info = info->next_port;
632 if (!info) {
633 info = IRQ_ports[irq];
634 if (done)
635 break;
636 done = 1;
637 if (pass_counter++ > 64) {
638 #if 0
639 printk("rs loop break\n");
640 #endif
641 break;
642 }
643 }
644 }
645
646
647
648
649
650 do
651 serial_outp(info, UART_IER, info->IER);
652 while ((info = info->next_port) != NULL);
653 }
654
655
656
657
658 static void rs_interrupt_single(int irq)
659 {
660 int status;
661 struct async_struct * info;
662
663
664 #ifdef SERIAL_DEBUG_INTR
665 printk("rs_interrupt_single(%d)...", irq);
666 #endif
667
668 info = IRQ_ports[irq];
669 if (!info || !info->tty)
670 return;
671
672 serial_outp(info, UART_IER, 0);
673 status = serial_inp(info, UART_LSR) & info->read_status_mask;
674 if (status & UART_LSR_DR)
675 receive_chars(info, &status);
676 check_modem_status(info);
677 if (status & UART_LSR_THRE)
678 transmit_chars(info, 0);
679
680
681
682
683 serial_outp(info, UART_IER, info->IER);
684 }
685
686 #endif
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703 static void do_serial_bh(void *unused)
704 {
705 run_task_queue(&tq_serial);
706 }
707
708 static void do_softint(void *private_)
709 {
710 struct async_struct *info = (struct async_struct *) private_;
711 struct tty_struct *tty;
712
713 tty = info->tty;
714 if (!tty)
715 return;
716
717 if (clear_bit(RS_EVENT_HANGUP, &info->event)) {
718 tty_hangup(tty);
719 wake_up_interruptible(&info->open_wait);
720 info->flags &= ~(ASYNC_NORMAL_ACTIVE|
721 ASYNC_CALLOUT_ACTIVE);
722 }
723 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
724 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
725 tty->ldisc.write_wakeup)
726 (tty->ldisc.write_wakeup)(tty);
727 wake_up_interruptible(&tty->write_wait);
728 }
729 }
730
731
732
733
734
735
736
737
738 static void rs_timer(void)
739 {
740 static unsigned long last_strobe = 0;
741 struct async_struct *info;
742 unsigned int i;
743
744 if ((jiffies - last_strobe) >= 60*HZ) {
745 for (i=1; i < 16; i++) {
746 info = IRQ_ports[i];
747 if (!info)
748 continue;
749 cli();
750 if (info->next_port) {
751 do {
752 serial_out(info, UART_IER, 0);
753 info->IER |= UART_IER_THRI;
754 serial_out(info, UART_IER, info->IER);
755 info = info->next_port;
756 } while (info);
757 rs_interrupt(i);
758 } else
759 rs_interrupt_single(i);
760 sti();
761 }
762 }
763 last_strobe = jiffies;
764 timer_table[RS_TIMER].expires = jiffies + 60 * HZ;
765 timer_active |= 1 << RS_TIMER;
766
767 if (IRQ_ports[0]) {
768 cli();
769 rs_interrupt(0);
770 sti();
771
772 timer_table[RS_TIMER].expires = jiffies + IRQ_timeout[0] - 2;
773 }
774 }
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791 static int grab_all_interrupts(int dontgrab)
792 {
793 int irq_lines = 0;
794 int i, mask;
795
796 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
797 if (!(mask & dontgrab) && !request_irq(i, rs_probe, SA_INTERRUPT, "serial probe")) {
798 irq_lines |= mask;
799 }
800 }
801 return irq_lines;
802 }
803
804
805
806
807 static void free_all_interrupts(int irq_lines)
808 {
809 int i;
810
811 for (i = 0; i < 16; i++) {
812 if (irq_lines & (1 << i))
813 free_irq(i);
814 }
815 }
816
817
818
819
820
821
822 static void figure_IRQ_timeout(int irq)
823 {
824 struct async_struct *info;
825 int timeout = 6000;
826
827 info = IRQ_ports[irq];
828 if (!info) {
829 IRQ_timeout[irq] = 6000;
830 return;
831 }
832 while (info) {
833 if (info->timeout < timeout)
834 timeout = info->timeout;
835 info = info->next_port;
836 }
837 if (!irq)
838 timeout = timeout / 2;
839 IRQ_timeout[irq] = timeout ? timeout : 1;
840 }
841
842 static int startup(struct async_struct * info)
843 {
844 unsigned short ICP;
845 unsigned long flags;
846 int retval;
847 void (*handler)(int);
848
849 if (info->flags & ASYNC_INITIALIZED)
850 return 0;
851
852 if (!info->port || !info->type) {
853 if (info->tty)
854 set_bit(TTY_IO_ERROR, &info->tty->flags);
855 return 0;
856 }
857
858 if (!info->xmit_buf) {
859 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
860 if (!info->xmit_buf)
861 return -ENOMEM;
862 }
863
864 save_flags(flags); cli();
865
866 #ifdef SERIAL_DEBUG_OPEN
867 printk("starting up ttys%d (irq %d)...", info->line, info->irq);
868 #endif
869
870
871
872
873
874 if (info->type == PORT_16550A) {
875 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
876 UART_FCR_CLEAR_XMIT));
877 info->xmit_fifo_size = 16;
878 } else
879 info->xmit_fifo_size = 1;
880
881
882
883
884
885
886 if (serial_inp(info, UART_LSR) == 0xff) {
887 restore_flags(flags);
888 if (suser()) {
889 if (info->tty)
890 set_bit(TTY_IO_ERROR, &info->tty->flags);
891 return 0;
892 } else
893 return -ENODEV;
894 }
895
896
897
898
899 if (info->irq && (!IRQ_ports[info->irq] ||
900 !IRQ_ports[info->irq]->next_port)) {
901 if (IRQ_ports[info->irq]) {
902 free_irq(info->irq);
903 handler = rs_interrupt;
904 } else
905 handler = rs_interrupt_single;
906
907 retval = request_irq(info->irq, handler, SA_INTERRUPT, "serial");
908 if (retval) {
909 restore_flags(flags);
910 if (suser()) {
911 if (info->tty)
912 set_bit(TTY_IO_ERROR,
913 &info->tty->flags);
914 return 0;
915 } else
916 return retval;
917 }
918 }
919
920
921
922
923
924 (void) serial_inp(info, UART_RX);
925 (void) serial_inp(info, UART_IIR);
926 (void) serial_inp(info, UART_MSR);
927
928
929
930
931 serial_outp(info, UART_LCR, UART_LCR_WLEN8);
932 if (info->flags & ASYNC_FOURPORT) {
933 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
934 info->MCR_noint = UART_MCR_DTR | UART_MCR_OUT1;
935 } else {
936 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
937 info->MCR_noint = UART_MCR_DTR | UART_MCR_RTS;
938 }
939 if (info->irq == 0)
940 info->MCR = info->MCR_noint;
941 serial_outp(info, UART_MCR, info->MCR);
942
943
944
945
946 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
947 serial_outp(info, UART_IER, info->IER);
948
949 if (info->flags & ASYNC_FOURPORT) {
950
951 ICP = (info->port & 0xFE0) | 0x01F;
952 outb_p(0x80, ICP);
953 (void) inb_p(ICP);
954 }
955
956
957
958
959 (void)serial_inp(info, UART_LSR);
960 (void)serial_inp(info, UART_RX);
961 (void)serial_inp(info, UART_IIR);
962 (void)serial_inp(info, UART_MSR);
963
964 if (info->tty)
965 clear_bit(TTY_IO_ERROR, &info->tty->flags);
966 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
967
968
969
970
971 info->prev_port = 0;
972 info->next_port = IRQ_ports[info->irq];
973 if (info->next_port)
974 info->next_port->prev_port = info;
975 IRQ_ports[info->irq] = info;
976 figure_IRQ_timeout(info->irq);
977
978
979
980
981 timer_table[RS_TIMER].expires = jiffies + 2;
982 timer_active |= 1 << RS_TIMER;
983
984
985
986
987 change_speed(info);
988
989 info->flags |= ASYNC_INITIALIZED;
990 restore_flags(flags);
991 return 0;
992 }
993
994
995
996
997
998 static void shutdown(struct async_struct * info)
999 {
1000 unsigned long flags;
1001 int retval;
1002
1003 if (!(info->flags & ASYNC_INITIALIZED))
1004 return;
1005
1006 #ifdef SERIAL_DEBUG_OPEN
1007 printk("Shutting down serial port %d (irq %d)....", info->line,
1008 info->irq);
1009 #endif
1010
1011 save_flags(flags); cli();
1012
1013
1014
1015
1016 if (info->next_port)
1017 info->next_port->prev_port = info->prev_port;
1018 if (info->prev_port)
1019 info->prev_port->next_port = info->next_port;
1020 else
1021 IRQ_ports[info->irq] = info->next_port;
1022 figure_IRQ_timeout(info->irq);
1023
1024
1025
1026
1027 if (info->irq && (!IRQ_ports[info->irq] ||
1028 !IRQ_ports[info->irq]->next_port)) {
1029 if (IRQ_ports[info->irq]) {
1030 free_irq(info->irq);
1031 retval = request_irq(info->irq, rs_interrupt_single, SA_INTERRUPT, "serial");
1032
1033 if (retval)
1034 printk("serial shutdown: request_irq: error %d"
1035 " Couldn't reacquire IRQ.\n", retval);
1036 } else
1037 free_irq(info->irq);
1038 }
1039
1040 if (info->xmit_buf) {
1041 free_page((unsigned long) info->xmit_buf);
1042 info->xmit_buf = 0;
1043 }
1044
1045 info->IER = 0;
1046 serial_outp(info, UART_IER, 0x00);
1047 if (info->flags & ASYNC_FOURPORT) {
1048
1049 (void) inb((info->port & 0xFE0) | 0x01F);
1050 }
1051
1052 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1053 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1054 info->MCR_noint &= ~(UART_MCR_DTR|UART_MCR_RTS);
1055 }
1056 serial_outp(info, UART_MCR, info->MCR_noint);
1057
1058
1059 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1060 UART_FCR_CLEAR_XMIT));
1061 (void)serial_in(info, UART_RX);
1062
1063 if (info->tty)
1064 set_bit(TTY_IO_ERROR, &info->tty->flags);
1065
1066 info->flags &= ~ASYNC_INITIALIZED;
1067 restore_flags(flags);
1068 }
1069
1070
1071
1072
1073
1074 static void change_speed(struct async_struct *info)
1075 {
1076 unsigned short port;
1077 int quot = 0;
1078 unsigned cflag,cval,fcr;
1079 int i;
1080
1081 if (!info->tty || !info->tty->termios)
1082 return;
1083 cflag = info->tty->termios->c_cflag;
1084 if (!(port = info->port))
1085 return;
1086 i = cflag & CBAUD;
1087 if (i & CBAUDEX) {
1088 i &= ~CBAUDEX;
1089 if (i < 1 || i > 2)
1090 info->tty->termios->c_cflag &= ~CBAUDEX;
1091 else
1092 i += 15;
1093 }
1094 if (i == 15) {
1095 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1096 i += 1;
1097 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1098 i += 2;
1099 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
1100 quot = info->custom_divisor;
1101 }
1102 if (quot) {
1103 info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
1104 info->baud_base) + 2;
1105 } else if (baud_table[i] == 134) {
1106 quot = (2*info->baud_base / 269);
1107 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
1108 } else if (baud_table[i]) {
1109 quot = info->baud_base / baud_table[i];
1110 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
1111 } else {
1112 quot = 0;
1113 info->timeout = 0;
1114 }
1115 if (quot) {
1116 info->MCR |= UART_MCR_DTR;
1117 info->MCR_noint |= UART_MCR_DTR;
1118 cli();
1119 serial_out(info, UART_MCR, info->MCR);
1120 sti();
1121 } else {
1122 info->MCR &= ~UART_MCR_DTR;
1123 info->MCR_noint &= ~UART_MCR_DTR;
1124 cli();
1125 serial_out(info, UART_MCR, info->MCR);
1126 sti();
1127 return;
1128 }
1129
1130 cval = cflag & (CSIZE | CSTOPB);
1131 cval >>= 4;
1132 if (cflag & PARENB)
1133 cval |= UART_LCR_PARITY;
1134 if (!(cflag & PARODD))
1135 cval |= UART_LCR_EPAR;
1136 if (info->type == PORT_16550A) {
1137 if ((info->baud_base / quot) < 2400)
1138 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1139 else
1140 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1141 } else
1142 fcr = 0;
1143
1144
1145 info->IER &= ~UART_IER_MSI;
1146 if (cflag & CRTSCTS) {
1147 info->flags |= ASYNC_CTS_FLOW;
1148 info->IER |= UART_IER_MSI;
1149 } else
1150 info->flags &= ~ASYNC_CTS_FLOW;
1151 if (cflag & CLOCAL)
1152 info->flags &= ~ASYNC_CHECK_CD;
1153 else {
1154 info->flags |= ASYNC_CHECK_CD;
1155 info->IER |= UART_IER_MSI;
1156 }
1157 serial_out(info, UART_IER, info->IER);
1158
1159
1160
1161
1162 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1163 if (I_INPCK(info->tty))
1164 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1165 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1166 info->read_status_mask |= UART_LSR_BI;
1167
1168 info->ignore_status_mask = 0;
1169 if (I_IGNPAR(info->tty)) {
1170 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1171 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1172 }
1173 if (I_IGNBRK(info->tty)) {
1174 info->ignore_status_mask |= UART_LSR_BI;
1175 info->read_status_mask |= UART_LSR_BI;
1176
1177
1178
1179
1180 if (I_IGNPAR(info->tty)) {
1181 info->ignore_status_mask |= UART_LSR_OE;
1182 info->read_status_mask |= UART_LSR_OE;
1183 }
1184 }
1185
1186 cli();
1187 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);
1188 serial_outp(info, UART_DLL, quot & 0xff);
1189 serial_outp(info, UART_DLM, quot >> 8);
1190 serial_outp(info, UART_LCR, cval);
1191 serial_outp(info, UART_FCR, fcr);
1192 sti();
1193 }
1194
1195 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1196 {
1197 struct async_struct *info = (struct async_struct *)tty->driver_data;
1198 unsigned long flags;
1199
1200 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1201 return;
1202
1203 if (!tty || !info->xmit_buf)
1204 return;
1205
1206 save_flags(flags); cli();
1207 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1208 restore_flags(flags);
1209 return;
1210 }
1211
1212 info->xmit_buf[info->xmit_head++] = ch;
1213 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1214 info->xmit_cnt++;
1215 restore_flags(flags);
1216 }
1217
1218 static void rs_flush_chars(struct tty_struct *tty)
1219 {
1220 struct async_struct *info = (struct async_struct *)tty->driver_data;
1221 unsigned long flags;
1222
1223 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1224 return;
1225
1226 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1227 !info->xmit_buf)
1228 return;
1229
1230 save_flags(flags); cli();
1231 info->IER |= UART_IER_THRI;
1232 serial_out(info, UART_IER, info->IER);
1233 restore_flags(flags);
1234 }
1235
1236 static int rs_write(struct tty_struct * tty, int from_user,
1237 unsigned char *buf, int count)
1238 {
1239 int c, total = 0;
1240 struct async_struct *info = (struct async_struct *)tty->driver_data;
1241 unsigned long flags;
1242
1243 if (serial_paranoia_check(info, tty->device, "rs_write"))
1244 return 0;
1245
1246 if (!tty || !info->xmit_buf || !tmp_buf)
1247 return 0;
1248
1249 save_flags(flags);
1250 while (1) {
1251 cli();
1252 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1253 SERIAL_XMIT_SIZE - info->xmit_head));
1254 if (c <= 0)
1255 break;
1256
1257 if (from_user) {
1258 down(&tmp_buf_sem);
1259 memcpy_fromfs(tmp_buf, buf, c);
1260 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1261 SERIAL_XMIT_SIZE - info->xmit_head));
1262 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1263 up(&tmp_buf_sem);
1264 } else
1265 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1266 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1267 info->xmit_cnt += c;
1268 restore_flags(flags);
1269 buf += c;
1270 count -= c;
1271 total += c;
1272 }
1273 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1274 !(info->IER & UART_IER_THRI)) {
1275 info->IER |= UART_IER_THRI;
1276 serial_out(info, UART_IER, info->IER);
1277 }
1278 restore_flags(flags);
1279 return total;
1280 }
1281
1282 static int rs_write_room(struct tty_struct *tty)
1283 {
1284 struct async_struct *info = (struct async_struct *)tty->driver_data;
1285 int ret;
1286
1287 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1288 return 0;
1289 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1290 if (ret < 0)
1291 ret = 0;
1292 return ret;
1293 }
1294
1295 static int rs_chars_in_buffer(struct tty_struct *tty)
1296 {
1297 struct async_struct *info = (struct async_struct *)tty->driver_data;
1298
1299 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1300 return 0;
1301 return info->xmit_cnt;
1302 }
1303
1304 static void rs_flush_buffer(struct tty_struct *tty)
1305 {
1306 struct async_struct *info = (struct async_struct *)tty->driver_data;
1307
1308 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1309 return;
1310 cli();
1311 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1312 sti();
1313 wake_up_interruptible(&tty->write_wait);
1314 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1315 tty->ldisc.write_wakeup)
1316 (tty->ldisc.write_wakeup)(tty);
1317 }
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327 static void rs_throttle(struct tty_struct * tty)
1328 {
1329 struct async_struct *info = (struct async_struct *)tty->driver_data;
1330 #ifdef SERIAL_DEBUG_THROTTLE
1331 char buf[64];
1332
1333 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1334 tty->ldisc.chars_in_buffer(tty));
1335 #endif
1336
1337 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1338 return;
1339
1340 if (I_IXOFF(tty))
1341 info->x_char = STOP_CHAR(tty);
1342
1343 info->MCR &= ~UART_MCR_RTS;
1344 info->MCR_noint &= ~UART_MCR_RTS;
1345 cli();
1346 serial_out(info, UART_MCR, info->MCR);
1347 sti();
1348 }
1349
1350 static void rs_unthrottle(struct tty_struct * tty)
1351 {
1352 struct async_struct *info = (struct async_struct *)tty->driver_data;
1353 #ifdef SERIAL_DEBUG_THROTTLE
1354 char buf[64];
1355
1356 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1357 tty->ldisc.chars_in_buffer(tty));
1358 #endif
1359
1360 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1361 return;
1362
1363 if (I_IXOFF(tty)) {
1364 if (info->x_char)
1365 info->x_char = 0;
1366 else
1367 info->x_char = START_CHAR(tty);
1368 }
1369 info->MCR |= UART_MCR_RTS;
1370 info->MCR_noint |= UART_MCR_RTS;
1371 cli();
1372 serial_out(info, UART_MCR, info->MCR);
1373 sti();
1374 }
1375
1376
1377
1378
1379
1380
1381
1382 static int get_serial_info(struct async_struct * info,
1383 struct serial_struct * retinfo)
1384 {
1385 struct serial_struct tmp;
1386
1387 if (!retinfo)
1388 return -EFAULT;
1389 memset(&tmp, 0, sizeof(tmp));
1390 tmp.type = info->type;
1391 tmp.line = info->line;
1392 tmp.port = info->port;
1393 tmp.irq = info->irq;
1394 tmp.flags = info->flags;
1395 tmp.baud_base = info->baud_base;
1396 tmp.close_delay = info->close_delay;
1397 tmp.custom_divisor = info->custom_divisor;
1398 tmp.hub6 = info->hub6;
1399 memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1400 return 0;
1401 }
1402
1403 static int set_serial_info(struct async_struct * info,
1404 struct serial_struct * new_info)
1405 {
1406 struct serial_struct new_serial;
1407 struct async_struct old_info;
1408 unsigned int i,change_irq,change_port;
1409 int retval = 0;
1410
1411 if (!new_info)
1412 return -EFAULT;
1413 memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1414 old_info = *info;
1415
1416 change_irq = new_serial.irq != info->irq;
1417 change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1418
1419 if (!suser()) {
1420 if (change_irq || change_port ||
1421 (new_serial.baud_base != info->baud_base) ||
1422 (new_serial.type != info->type) ||
1423 (new_serial.close_delay != info->close_delay) ||
1424 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1425 (info->flags & ~ASYNC_USR_MASK)))
1426 return -EPERM;
1427 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1428 (new_serial.flags & ASYNC_USR_MASK));
1429 info->custom_divisor = new_serial.custom_divisor;
1430 goto check_and_exit;
1431 }
1432
1433 if (new_serial.irq == 2)
1434 new_serial.irq = 9;
1435
1436 if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1437 (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1438 return -EINVAL;
1439 }
1440
1441
1442 if (new_serial.type) {
1443 for (i = 0 ; i < NR_PORTS; i++)
1444 if ((info != &rs_table[i]) &&
1445 (rs_table[i].port == new_serial.port) &&
1446 rs_table[i].type)
1447 return -EADDRINUSE;
1448 }
1449
1450 if ((change_port || change_irq) && (info->count > 1))
1451 return -EBUSY;
1452
1453
1454
1455
1456
1457
1458 info->baud_base = new_serial.baud_base;
1459 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1460 (new_serial.flags & ASYNC_FLAGS));
1461 info->custom_divisor = new_serial.custom_divisor;
1462 info->type = new_serial.type;
1463 info->close_delay = new_serial.close_delay;
1464
1465 if (change_port || change_irq) {
1466
1467
1468
1469
1470 shutdown(info);
1471 info->irq = new_serial.irq;
1472 info->port = new_serial.port;
1473 info->hub6 = new_serial.hub6;
1474 }
1475
1476 check_and_exit:
1477 if (!info->port || !info->type)
1478 return 0;
1479 if (info->flags & ASYNC_INITIALIZED) {
1480 if (((old_info.flags & ASYNC_SPD_MASK) !=
1481 (info->flags & ASYNC_SPD_MASK)) ||
1482 (old_info.custom_divisor != info->custom_divisor))
1483 change_speed(info);
1484 } else
1485 retval = startup(info);
1486 return retval;
1487 }
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1501 {
1502 unsigned char status;
1503 unsigned int result;
1504
1505 cli();
1506 status = serial_in(info, UART_LSR);
1507 sti();
1508 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1509 put_fs_long(result,(unsigned long *) value);
1510 return 0;
1511 }
1512
1513
1514 static int get_modem_info(struct async_struct * info, unsigned int *value)
1515 {
1516 unsigned char control, status;
1517 unsigned int result;
1518
1519 control = info->MCR;
1520 cli();
1521 status = serial_in(info, UART_MSR);
1522 sti();
1523 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1524 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1525 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1526 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1527 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1528 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1529 put_fs_long(result,(unsigned long *) value);
1530 return 0;
1531 }
1532
1533 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1534 unsigned int *value)
1535 {
1536 unsigned int arg = get_fs_long((unsigned long *) value);
1537
1538 switch (cmd) {
1539 case TIOCMBIS:
1540 if (arg & TIOCM_RTS) {
1541 info->MCR |= UART_MCR_RTS;
1542 info->MCR_noint |= UART_MCR_RTS;
1543 }
1544 if (arg & TIOCM_DTR) {
1545 info->MCR |= UART_MCR_DTR;
1546 info->MCR_noint |= UART_MCR_DTR;
1547 }
1548 break;
1549 case TIOCMBIC:
1550 if (arg & TIOCM_RTS) {
1551 info->MCR &= ~UART_MCR_RTS;
1552 info->MCR_noint &= ~UART_MCR_RTS;
1553 }
1554 if (arg & TIOCM_DTR) {
1555 info->MCR &= ~UART_MCR_DTR;
1556 info->MCR_noint &= ~UART_MCR_DTR;
1557 }
1558 break;
1559 case TIOCMSET:
1560 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1561 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1562 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1563 info->MCR_noint = ((info->MCR_noint
1564 & ~(UART_MCR_RTS | UART_MCR_DTR))
1565 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1566 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1567 break;
1568 default:
1569 return -EINVAL;
1570 }
1571 cli();
1572 serial_out(info, UART_MCR, info->MCR);
1573 sti();
1574 return 0;
1575 }
1576
1577 static int do_autoconfig(struct async_struct * info)
1578 {
1579 int retval;
1580
1581 if (!suser())
1582 return -EPERM;
1583
1584 if (info->count > 1)
1585 return -EBUSY;
1586
1587 shutdown(info);
1588
1589 cli();
1590 autoconfig(info);
1591 sti();
1592
1593 retval = startup(info);
1594 if (retval)
1595 return retval;
1596 return 0;
1597 }
1598
1599
1600
1601
1602
1603 static void send_break( struct async_struct * info, int duration)
1604 {
1605 if (!info->port)
1606 return;
1607 current->state = TASK_INTERRUPTIBLE;
1608 current->timeout = jiffies + duration;
1609 cli();
1610 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
1611 schedule();
1612 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1613 sti();
1614 }
1615
1616
1617
1618
1619
1620 static int check_wild_interrupts(int doprint)
1621 {
1622 int i, mask;
1623 int wild_interrupts = 0;
1624 int irq_lines;
1625 unsigned long timeout;
1626 unsigned long flags;
1627
1628
1629 save_flags(flags); sti();
1630
1631 irq_lines = grab_all_interrupts(0);
1632
1633
1634
1635
1636
1637 timeout = jiffies+10;
1638 while (timeout >= jiffies)
1639 ;
1640
1641 rs_triggered = 0;
1642
1643 timeout = jiffies+10;
1644 while (timeout >= jiffies)
1645 ;
1646
1647 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1648 if ((rs_triggered & (1 << i)) &&
1649 (irq_lines & (1 << i))) {
1650 wild_interrupts |= mask;
1651 if (doprint)
1652 printk("Wild interrupt? (IRQ %d)\n", i);
1653 }
1654 }
1655 free_all_interrupts(irq_lines);
1656 restore_flags(flags);
1657 return wild_interrupts;
1658 }
1659
1660 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1661 unsigned int cmd, unsigned long arg)
1662 {
1663 int error;
1664 struct async_struct * info = (struct async_struct *)tty->driver_data;
1665 int retval;
1666
1667 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1668 return -ENODEV;
1669
1670 switch (cmd) {
1671 case TCSBRK:
1672 retval = tty_check_change(tty);
1673 if (retval)
1674 return retval;
1675 wait_until_sent(tty, 0);
1676 if (!arg)
1677 send_break(info, HZ/4);
1678 return 0;
1679 case TCSBRKP:
1680 retval = tty_check_change(tty);
1681 if (retval)
1682 return retval;
1683 wait_until_sent(tty, 0);
1684 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1685 return 0;
1686 case TIOCGSOFTCAR:
1687 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1688 if (error)
1689 return error;
1690 put_fs_long(C_CLOCAL(tty) ? 1 : 0,
1691 (unsigned long *) arg);
1692 return 0;
1693 case TIOCSSOFTCAR:
1694 arg = get_fs_long((unsigned long *) arg);
1695 tty->termios->c_cflag =
1696 ((tty->termios->c_cflag & ~CLOCAL) |
1697 (arg ? CLOCAL : 0));
1698 return 0;
1699 case TIOCMGET:
1700 error = verify_area(VERIFY_WRITE, (void *) arg,
1701 sizeof(unsigned int));
1702 if (error)
1703 return error;
1704 return get_modem_info(info, (unsigned int *) arg);
1705 case TIOCMBIS:
1706 case TIOCMBIC:
1707 case TIOCMSET:
1708 return set_modem_info(info, cmd, (unsigned int *) arg);
1709 case TIOCGSERIAL:
1710 error = verify_area(VERIFY_WRITE, (void *) arg,
1711 sizeof(struct serial_struct));
1712 if (error)
1713 return error;
1714 return get_serial_info(info,
1715 (struct serial_struct *) arg);
1716 case TIOCSSERIAL:
1717 return set_serial_info(info,
1718 (struct serial_struct *) arg);
1719 case TIOCSERCONFIG:
1720 return do_autoconfig(info);
1721
1722 case TIOCSERGWILD:
1723 error = verify_area(VERIFY_WRITE, (void *) arg,
1724 sizeof(int));
1725 if (error)
1726 return error;
1727 put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
1728 return 0;
1729
1730 case TIOCSERGETLSR:
1731 error = verify_area(VERIFY_WRITE, (void *) arg,
1732 sizeof(unsigned int));
1733 if (error)
1734 return error;
1735 else
1736 return get_lsr_info(info, (unsigned int *) arg);
1737
1738 case TIOCSERSWILD:
1739 if (!suser())
1740 return -EPERM;
1741 rs_wild_int_mask = get_fs_long((unsigned long *) arg);
1742 if (rs_wild_int_mask < 0)
1743 rs_wild_int_mask = check_wild_interrupts(0);
1744 return 0;
1745
1746 case TIOCSERGSTRUCT:
1747 error = verify_area(VERIFY_WRITE, (void *) arg,
1748 sizeof(struct async_struct));
1749 if (error)
1750 return error;
1751 memcpy_tofs((struct async_struct *) arg,
1752 info, sizeof(struct async_struct));
1753 return 0;
1754
1755 default:
1756 return -ENOIOCTLCMD;
1757 }
1758 return 0;
1759 }
1760
1761 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1762 {
1763 struct async_struct *info = (struct async_struct *)tty->driver_data;
1764
1765 if (tty->termios->c_cflag == old_termios->c_cflag)
1766 return;
1767
1768 change_speed(info);
1769
1770 if ((old_termios->c_cflag & CRTSCTS) &&
1771 !(tty->termios->c_cflag & CRTSCTS)) {
1772 tty->hw_stopped = 0;
1773 rs_start(tty);
1774 }
1775
1776 #if 0
1777
1778
1779
1780
1781
1782
1783 if (!(old_termios->c_cflag & CLOCAL) &&
1784 (tty->termios->c_cflag & CLOCAL))
1785 wake_up_interruptible(&info->open_wait);
1786 #endif
1787 }
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799 static void rs_close(struct tty_struct *tty, struct file * filp)
1800 {
1801 struct async_struct * info = (struct async_struct *)tty->driver_data;
1802 unsigned long flags;
1803 unsigned long timeout;
1804
1805 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1806 return;
1807
1808 save_flags(flags); cli();
1809
1810 if (tty_hung_up_p(filp)) {
1811 restore_flags(flags);
1812 return;
1813 }
1814
1815 #ifdef SERIAL_DEBUG_OPEN
1816 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1817 #endif
1818 if ((tty->count == 1) && (info->count != 1)) {
1819
1820
1821
1822
1823
1824
1825
1826 printk("rs_close: bad serial port count; tty->count is 1, "
1827 "info->count is %d\n", info->count);
1828 info->count = 1;
1829 }
1830 if (--info->count < 0) {
1831 printk("rs_close: bad serial port count for ttys%d: %d\n",
1832 info->line, info->count);
1833 info->count = 0;
1834 }
1835 if (info->count) {
1836 restore_flags(flags);
1837 return;
1838 }
1839 info->flags |= ASYNC_CLOSING;
1840
1841
1842
1843
1844 if (info->flags & ASYNC_NORMAL_ACTIVE)
1845 info->normal_termios = *tty->termios;
1846 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1847 info->callout_termios = *tty->termios;
1848
1849
1850
1851
1852
1853
1854 info->IER &= ~UART_IER_RLSI;
1855 serial_out(info, UART_IER, info->IER);
1856 info->read_status_mask &= ~UART_LSR_DR;
1857 if (info->flags & ASYNC_INITIALIZED) {
1858 wait_until_sent(tty, 3000);
1859
1860
1861
1862
1863
1864 timeout = jiffies+HZ;
1865 while (!(serial_inp(info, UART_LSR) & UART_LSR_TEMT)) {
1866 current->state = TASK_INTERRUPTIBLE;
1867 current->timeout = jiffies + info->timeout;
1868 schedule();
1869 if (jiffies > timeout)
1870 break;
1871 }
1872 }
1873 shutdown(info);
1874 if (tty->driver.flush_buffer)
1875 tty->driver.flush_buffer(tty);
1876 if (tty->ldisc.flush_buffer)
1877 tty->ldisc.flush_buffer(tty);
1878 info->event = 0;
1879 info->tty = 0;
1880 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1881 if (tty->ldisc.close)
1882 (tty->ldisc.close)(tty);
1883 tty->ldisc = ldiscs[N_TTY];
1884 tty->termios->c_line = N_TTY;
1885 if (tty->ldisc.open)
1886 (tty->ldisc.open)(tty);
1887 }
1888 if (info->blocked_open) {
1889 if (info->close_delay) {
1890 current->state = TASK_INTERRUPTIBLE;
1891 current->timeout = jiffies + info->close_delay;
1892 schedule();
1893 }
1894 wake_up_interruptible(&info->open_wait);
1895 }
1896 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1897 ASYNC_CLOSING);
1898 wake_up_interruptible(&info->close_wait);
1899 restore_flags(flags);
1900 }
1901
1902
1903
1904
1905 void rs_hangup(struct tty_struct *tty)
1906 {
1907 struct async_struct * info = (struct async_struct *)tty->driver_data;
1908
1909 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1910 return;
1911
1912 shutdown(info);
1913 info->event = 0;
1914 info->count = 0;
1915 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1916 info->tty = 0;
1917 wake_up_interruptible(&info->open_wait);
1918 }
1919
1920
1921
1922
1923
1924
1925 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1926 struct async_struct *info)
1927 {
1928 struct wait_queue wait = { current, NULL };
1929 int retval;
1930 int do_clocal = 0;
1931
1932
1933
1934
1935
1936 if (info->flags & ASYNC_CLOSING) {
1937 interruptible_sleep_on(&info->close_wait);
1938 #ifdef SERIAL_DO_RESTART
1939 if (info->flags & ASYNC_HUP_NOTIFY)
1940 return -EAGAIN;
1941 else
1942 return -ERESTARTSYS;
1943 #else
1944 return -EAGAIN;
1945 #endif
1946 }
1947
1948
1949
1950
1951
1952 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1953 if (info->flags & ASYNC_NORMAL_ACTIVE)
1954 return -EBUSY;
1955 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1956 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1957 (info->session != current->session))
1958 return -EBUSY;
1959 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1960 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1961 (info->pgrp != current->pgrp))
1962 return -EBUSY;
1963 info->flags |= ASYNC_CALLOUT_ACTIVE;
1964 return 0;
1965 }
1966
1967
1968
1969
1970
1971 if (filp->f_flags & O_NONBLOCK) {
1972 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1973 return -EBUSY;
1974 info->flags |= ASYNC_NORMAL_ACTIVE;
1975 return 0;
1976 }
1977
1978 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1979 if (info->normal_termios.c_cflag & CLOCAL)
1980 do_clocal = 1;
1981 } else {
1982 if (tty->termios->c_cflag & CLOCAL)
1983 do_clocal = 1;
1984 }
1985
1986
1987
1988
1989
1990
1991
1992
1993 retval = 0;
1994 add_wait_queue(&info->open_wait, &wait);
1995 #ifdef SERIAL_DEBUG_OPEN
1996 printk("block_til_ready before block: ttys%d, count = %d\n",
1997 info->line, info->count);
1998 #endif
1999 info->count--;
2000 info->blocked_open++;
2001 while (1) {
2002 cli();
2003 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
2004 serial_out(info, UART_MCR,
2005 serial_inp(info, UART_MCR) |
2006 (UART_MCR_DTR | UART_MCR_RTS));
2007 sti();
2008 current->state = TASK_INTERRUPTIBLE;
2009 if (tty_hung_up_p(filp) ||
2010 !(info->flags & ASYNC_INITIALIZED)) {
2011 #ifdef SERIAL_DO_RESTART
2012 if (info->flags & ASYNC_HUP_NOTIFY)
2013 retval = -EAGAIN;
2014 else
2015 retval = -ERESTARTSYS;
2016 #else
2017 retval = -EAGAIN;
2018 #endif
2019 break;
2020 }
2021 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2022 !(info->flags & ASYNC_CLOSING) &&
2023 (do_clocal || (serial_in(info, UART_MSR) &
2024 UART_MSR_DCD)))
2025 break;
2026 if (current->signal & ~current->blocked) {
2027 retval = -ERESTARTSYS;
2028 break;
2029 }
2030 #ifdef SERIAL_DEBUG_OPEN
2031 printk("block_til_ready blocking: ttys%d, count = %d\n",
2032 info->line, info->count);
2033 #endif
2034 schedule();
2035 }
2036 current->state = TASK_RUNNING;
2037 remove_wait_queue(&info->open_wait, &wait);
2038 if (!tty_hung_up_p(filp))
2039 info->count++;
2040 info->blocked_open--;
2041 #ifdef SERIAL_DEBUG_OPEN
2042 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2043 info->line, info->count);
2044 #endif
2045 if (retval)
2046 return retval;
2047 info->flags |= ASYNC_NORMAL_ACTIVE;
2048 return 0;
2049 }
2050
2051
2052
2053
2054
2055
2056
2057 int rs_open(struct tty_struct *tty, struct file * filp)
2058 {
2059 struct async_struct *info;
2060 int retval, line;
2061
2062 line = MINOR(tty->device) - tty->driver.minor_start;
2063 if ((line < 0) || (line >= NR_PORTS))
2064 return -ENODEV;
2065 info = rs_table + line;
2066 if (serial_paranoia_check(info, tty->device, "rs_open"))
2067 return -ENODEV;
2068
2069 #ifdef SERIAL_DEBUG_OPEN
2070 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2071 info->count);
2072 #endif
2073 info->count++;
2074 tty->driver_data = info;
2075 info->tty = tty;
2076
2077 if (!tmp_buf) {
2078 tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2079 if (!tmp_buf)
2080 return -ENOMEM;
2081 }
2082
2083
2084
2085
2086 retval = startup(info);
2087 if (retval)
2088 return retval;
2089
2090 retval = block_til_ready(tty, filp, info);
2091 if (retval) {
2092 #ifdef SERIAL_DEBUG_OPEN
2093 printk("rs_open returning after block_til_ready with %d\n",
2094 retval);
2095 #endif
2096 return retval;
2097 }
2098
2099 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2100 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2101 *tty->termios = info->normal_termios;
2102 else
2103 *tty->termios = info->callout_termios;
2104 change_speed(info);
2105 }
2106
2107 info->session = current->session;
2108 info->pgrp = current->pgrp;
2109
2110 #ifdef SERIAL_DEBUG_OPEN
2111 printk("rs_open ttys%d successful...", info->line);
2112 #endif
2113 return 0;
2114 }
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129 static void show_serial_version(void)
2130 {
2131 printk("Serial driver version 4.00 with");
2132 #ifdef CONFIG_HUB6
2133 printk(" HUB-6");
2134 #define SERIAL_OPT
2135 #endif
2136 #ifdef SERIAL_OPT
2137 printk(" enabled\n");
2138 #else
2139 printk(" no serial options enabled\n");
2140 #endif
2141 #undef SERIAL_OPT
2142 }
2143
2144
2145
2146
2147
2148
2149 static int get_auto_irq(struct async_struct *info)
2150 {
2151 unsigned char save_MCR, save_IER, save_ICP=0;
2152 unsigned short ICP=0, port = info->port;
2153 unsigned long timeout;
2154
2155
2156
2157
2158 rs_irq_triggered = 0;
2159 cli();
2160 save_IER = serial_inp(info, UART_IER);
2161 save_MCR = serial_inp(info, UART_MCR);
2162 if (info->flags & ASYNC_FOURPORT) {
2163 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2164 serial_outp(info, UART_IER, 0x0f);
2165 ICP = (port & 0xFE0) | 0x01F;
2166 save_ICP = inb_p(ICP);
2167 outb_p(0x80, ICP);
2168 (void) inb_p(ICP);
2169 } else {
2170 serial_outp(info, UART_MCR,
2171 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2172 serial_outp(info, UART_IER, 0x0f);
2173 }
2174 sti();
2175
2176
2177
2178 (void)serial_inp(info, UART_LSR);
2179 (void)serial_inp(info, UART_RX);
2180 (void)serial_inp(info, UART_IIR);
2181 (void)serial_inp(info, UART_MSR);
2182
2183 timeout = jiffies+2;
2184 while (timeout >= jiffies) {
2185 if (rs_irq_triggered)
2186 break;
2187 }
2188
2189
2190
2191 cli();
2192 serial_outp(info, UART_IER, save_IER);
2193 serial_outp(info, UART_MCR, save_MCR);
2194 if (info->flags & ASYNC_FOURPORT)
2195 outb_p(save_ICP, ICP);
2196 sti();
2197 return(rs_irq_triggered);
2198 }
2199
2200
2201
2202
2203
2204 static int do_auto_irq(struct async_struct * info)
2205 {
2206 unsigned port = info->port;
2207 int irq_lines = 0;
2208 int irq_try_1 = 0, irq_try_2 = 0;
2209 int retries;
2210 unsigned long flags;
2211
2212 if (!port)
2213 return 0;
2214
2215
2216 save_flags(flags); sti();
2217
2218 irq_lines = grab_all_interrupts(rs_wild_int_mask);
2219
2220 for (retries = 0; retries < 5; retries++) {
2221 if (!irq_try_1)
2222 irq_try_1 = get_auto_irq(info);
2223 if (!irq_try_2)
2224 irq_try_2 = get_auto_irq(info);
2225 if (irq_try_1 && irq_try_2) {
2226 if (irq_try_1 == irq_try_2)
2227 break;
2228 irq_try_1 = irq_try_2 = 0;
2229 }
2230 }
2231 restore_flags(flags);
2232 free_all_interrupts(irq_lines);
2233 return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
2234 }
2235
2236
2237
2238
2239
2240
2241
2242
2243 static void autoconfig(struct async_struct * info)
2244 {
2245 unsigned char status1, status2, scratch, scratch2;
2246 unsigned port = info->port;
2247 unsigned long flags;
2248
2249 info->type = PORT_UNKNOWN;
2250
2251 if (!port)
2252 return;
2253
2254 save_flags(flags); cli();
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265 scratch = serial_inp(info, UART_IER);
2266 serial_outp(info, UART_IER, 0);
2267 outb(0xff, 0x080);
2268 scratch2 = serial_inp(info, UART_IER);
2269 serial_outp(info, UART_IER, scratch);
2270 if (scratch2) {
2271 restore_flags(flags);
2272 return;
2273 }
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284 if (!(info->flags & ASYNC_SKIP_TEST)) {
2285 scratch = serial_inp(info, UART_MCR);
2286 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2287 scratch2 = serial_inp(info, UART_MSR);
2288 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2289 status1 = serial_inp(info, UART_MSR) & 0xF0;
2290 serial_outp(info, UART_MCR, scratch);
2291 serial_outp(info, UART_MSR, scratch2);
2292 if (status1 != 0x90) {
2293 restore_flags(flags);
2294 return;
2295 }
2296 }
2297
2298
2299
2300
2301
2302 if (info->flags & ASYNC_AUTO_IRQ)
2303 info->irq = do_auto_irq(info);
2304
2305 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2306 scratch = serial_in(info, UART_IIR) >> 6;
2307 info->xmit_fifo_size = 1;
2308 switch (scratch) {
2309 case 0:
2310 info->type = PORT_16450;
2311 break;
2312 case 1:
2313 info->type = PORT_UNKNOWN;
2314 break;
2315 case 2:
2316 info->type = PORT_16550;
2317 break;
2318 case 3:
2319 info->type = PORT_16550A;
2320 info->xmit_fifo_size = 16;
2321 break;
2322 }
2323 if (info->type == PORT_16450) {
2324 scratch = serial_in(info, UART_SCR);
2325 serial_outp(info, UART_SCR, 0xa5);
2326 status1 = serial_in(info, UART_SCR);
2327 serial_outp(info, UART_SCR, 0x5a);
2328 status2 = serial_in(info, UART_SCR);
2329 serial_outp(info, UART_SCR, scratch);
2330
2331 if ((status1 != 0xa5) || (status2 != 0x5a))
2332 info->type = PORT_8250;
2333 }
2334
2335
2336
2337
2338 serial_outp(info, UART_MCR, 0x00);
2339 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
2340 UART_FCR_CLEAR_XMIT));
2341 (void)serial_in(info, UART_RX);
2342
2343 restore_flags(flags);
2344 }
2345
2346
2347
2348
2349 long rs_init(long kmem_start)
2350 {
2351 int i;
2352 struct async_struct * info;
2353
2354 bh_base[SERIAL_BH].routine = do_serial_bh;
2355 timer_table[RS_TIMER].fn = rs_timer;
2356 timer_table[RS_TIMER].expires = 0;
2357 #ifdef CONFIG_AUTO_IRQ
2358 rs_wild_int_mask = check_wild_interrupts(1);
2359 #endif
2360
2361 for (i = 0; i < 16; i++) {
2362 IRQ_ports[i] = 0;
2363 IRQ_timeout[i] = 0;
2364 }
2365
2366 show_serial_version();
2367
2368
2369
2370 memset(&serial_driver, 0, sizeof(struct tty_driver));
2371 serial_driver.magic = TTY_DRIVER_MAGIC;
2372 serial_driver.name = "ttyS";
2373 serial_driver.major = TTY_MAJOR;
2374 serial_driver.minor_start = 64;
2375 serial_driver.num = NR_PORTS;
2376 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2377 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2378 serial_driver.init_termios = tty_std_termios;
2379 serial_driver.init_termios.c_cflag =
2380 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2381 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2382 serial_driver.refcount = &serial_refcount;
2383 serial_driver.table = serial_table;
2384 serial_driver.termios = serial_termios;
2385 serial_driver.termios_locked = serial_termios_locked;
2386
2387 serial_driver.open = rs_open;
2388 serial_driver.close = rs_close;
2389 serial_driver.write = rs_write;
2390 serial_driver.put_char = rs_put_char;
2391 serial_driver.flush_chars = rs_flush_chars;
2392 serial_driver.write_room = rs_write_room;
2393 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2394 serial_driver.flush_buffer = rs_flush_buffer;
2395 serial_driver.ioctl = rs_ioctl;
2396 serial_driver.throttle = rs_throttle;
2397 serial_driver.unthrottle = rs_unthrottle;
2398 serial_driver.set_termios = rs_set_termios;
2399 serial_driver.stop = rs_stop;
2400 serial_driver.start = rs_start;
2401 serial_driver.hangup = rs_hangup;
2402
2403
2404
2405
2406
2407 callout_driver = serial_driver;
2408 callout_driver.name = "cua";
2409 callout_driver.major = TTYAUX_MAJOR;
2410 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2411
2412 if (tty_register_driver(&serial_driver))
2413 panic("Couldn't register serial driver\n");
2414 if (tty_register_driver(&callout_driver))
2415 panic("Couldn't register callout driver\n");
2416
2417 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
2418 info->magic = SERIAL_MAGIC;
2419 info->line = i;
2420 info->tty = 0;
2421 info->type = PORT_UNKNOWN;
2422 info->custom_divisor = 0;
2423 info->close_delay = 50;
2424 info->x_char = 0;
2425 info->event = 0;
2426 info->count = 0;
2427 info->blocked_open = 0;
2428 info->tqueue.routine = do_softint;
2429 info->tqueue.data = info;
2430 info->callout_termios =callout_driver.init_termios;
2431 info->normal_termios = serial_driver.init_termios;
2432 info->open_wait = 0;
2433 info->close_wait = 0;
2434 info->next_port = 0;
2435 info->prev_port = 0;
2436 if (info->irq == 2)
2437 info->irq = 9;
2438 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2439 continue;
2440 autoconfig(info);
2441 if (info->type == PORT_UNKNOWN)
2442 continue;
2443 printk("tty%02d%s at 0x%04x (irq = %d)", info->line,
2444 (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2445 info->port, info->irq);
2446 switch (info->type) {
2447 case PORT_8250:
2448 printk(" is a 8250\n");
2449 break;
2450 case PORT_16450:
2451 printk(" is a 16450\n");
2452 break;
2453 case PORT_16550:
2454 printk(" is a 16550\n");
2455 break;
2456 case PORT_16550A:
2457 printk(" is a 16550A\n");
2458 break;
2459 default:
2460 printk("\n");
2461 break;
2462 }
2463 }
2464 return kmem_start;
2465 }
2466
2467
2468
2469
2470
2471 int register_serial(struct serial_struct *req)
2472 {
2473 int i;
2474 unsigned long flags;
2475 struct async_struct *info;
2476
2477 save_flags(flags);
2478 cli();
2479 for (i = 0; i < NR_PORTS; i++) {
2480 if (rs_table[i].port == req->port)
2481 break;
2482 }
2483 if (i == NR_PORTS) {
2484 for (i = 0; i < NR_PORTS; i++)
2485 if ((rs_table[i].type == PORT_UNKNOWN) &&
2486 (rs_table[i].count == 0))
2487 break;
2488 }
2489 if (i == NR_PORTS) {
2490 restore_flags(flags);
2491 return -1;
2492 }
2493 info = &rs_table[i];
2494 if (rs_table[i].count) {
2495 restore_flags(flags);
2496 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
2497 "device already open\n", i, req->port, req->irq);
2498 return -1;
2499 }
2500 info->irq = req->irq;
2501 info->port = req->port;
2502 autoconfig(info);
2503 if (info->type == PORT_UNKNOWN) {
2504 restore_flags(flags);
2505 printk("register_serial(): autoconfig failed\n");
2506 return -1;
2507 }
2508 printk("tty%02d at 0x%04x (irq = %d)", info->line,
2509 info->port, info->irq);
2510 switch (info->type) {
2511 case PORT_8250:
2512 printk(" is a 8250\n"); break;
2513 case PORT_16450:
2514 printk(" is a 16450\n"); break;
2515 case PORT_16550:
2516 printk(" is a 16550\n"); break;
2517 case PORT_16550A:
2518 printk(" is a 16550A\n"); break;
2519 default:
2520 printk("\n"); break;
2521 }
2522 restore_flags(flags);
2523 return info->line;
2524 }
2525
2526 void unregister_serial(int line)
2527 {
2528 unsigned long flags;
2529 struct async_struct *info = &rs_table[line];
2530
2531 save_flags(flags);
2532 cli();
2533 if (info->tty)
2534 tty_hangup(info->tty);
2535 info->type = PORT_UNKNOWN;
2536 printk("tty%02d unloaded\n", info->line);
2537 restore_flags(flags);
2538 }