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 (!(old_termios->c_cflag & CLOCAL) &&
1777 (tty->termios->c_cflag & CLOCAL))
1778 wake_up_interruptible(&info->open_wait);
1779 }
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791 static void rs_close(struct tty_struct *tty, struct file * filp)
1792 {
1793 struct async_struct * info = (struct async_struct *)tty->driver_data;
1794 unsigned long flags;
1795 unsigned long timeout;
1796
1797 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1798 return;
1799
1800 save_flags(flags); cli();
1801
1802 if (tty_hung_up_p(filp)) {
1803 restore_flags(flags);
1804 return;
1805 }
1806
1807 #ifdef SERIAL_DEBUG_OPEN
1808 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1809 #endif
1810 if ((tty->count == 1) && (info->count != 1)) {
1811
1812
1813
1814
1815
1816
1817
1818 printk("rs_close: bad serial port count; tty->count is 1, "
1819 "info->count is %d\n", info->count);
1820 info->count = 1;
1821 }
1822 if (--info->count < 0) {
1823 printk("rs_close: bad serial port count for ttys%d: %d\n",
1824 info->line, info->count);
1825 info->count = 0;
1826 }
1827 if (info->count) {
1828 restore_flags(flags);
1829 return;
1830 }
1831 info->flags |= ASYNC_CLOSING;
1832
1833
1834
1835
1836 if (info->flags & ASYNC_NORMAL_ACTIVE)
1837 info->normal_termios = *tty->termios;
1838 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1839 info->callout_termios = *tty->termios;
1840
1841
1842
1843
1844
1845
1846 info->IER &= ~UART_IER_RLSI;
1847 serial_out(info, UART_IER, info->IER);
1848 info->read_status_mask &= ~UART_LSR_DR;
1849 if (info->flags & ASYNC_INITIALIZED) {
1850 wait_until_sent(tty, 3000);
1851
1852
1853
1854
1855
1856 timeout = jiffies+HZ;
1857 while (!(serial_inp(info, UART_LSR) & UART_LSR_TEMT)) {
1858 current->state = TASK_INTERRUPTIBLE;
1859 current->timeout = jiffies + info->timeout;
1860 schedule();
1861 if (jiffies > timeout)
1862 break;
1863 }
1864 }
1865 shutdown(info);
1866 if (tty->driver.flush_buffer)
1867 tty->driver.flush_buffer(tty);
1868 if (tty->ldisc.flush_buffer)
1869 tty->ldisc.flush_buffer(tty);
1870 info->event = 0;
1871 info->tty = 0;
1872 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1873 if (tty->ldisc.close)
1874 (tty->ldisc.close)(tty);
1875 tty->ldisc = ldiscs[N_TTY];
1876 tty->termios->c_line = N_TTY;
1877 if (tty->ldisc.open)
1878 (tty->ldisc.open)(tty);
1879 }
1880 if (info->blocked_open) {
1881 if (info->close_delay) {
1882 current->state = TASK_INTERRUPTIBLE;
1883 current->timeout = jiffies + info->close_delay;
1884 schedule();
1885 }
1886 wake_up_interruptible(&info->open_wait);
1887 }
1888 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1889 ASYNC_CLOSING);
1890 wake_up_interruptible(&info->close_wait);
1891 restore_flags(flags);
1892 }
1893
1894
1895
1896
1897 void rs_hangup(struct tty_struct *tty)
1898 {
1899 struct async_struct * info = (struct async_struct *)tty->driver_data;
1900
1901 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1902 return;
1903
1904 shutdown(info);
1905 info->event = 0;
1906 info->count = 0;
1907 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1908 info->tty = 0;
1909 wake_up_interruptible(&info->open_wait);
1910 }
1911
1912
1913
1914
1915
1916
1917 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1918 struct async_struct *info)
1919 {
1920 struct wait_queue wait = { current, NULL };
1921 int retval;
1922 int do_clocal = 0;
1923
1924
1925
1926
1927
1928 if (info->flags & ASYNC_CLOSING) {
1929 interruptible_sleep_on(&info->close_wait);
1930 #ifdef SERIAL_DO_RESTART
1931 if (info->flags & ASYNC_HUP_NOTIFY)
1932 return -EAGAIN;
1933 else
1934 return -ERESTARTSYS;
1935 #else
1936 return -EAGAIN;
1937 #endif
1938 }
1939
1940
1941
1942
1943
1944 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1945 if (info->flags & ASYNC_NORMAL_ACTIVE)
1946 return -EBUSY;
1947 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1948 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1949 (info->session != current->session))
1950 return -EBUSY;
1951 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1952 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1953 (info->pgrp != current->pgrp))
1954 return -EBUSY;
1955 info->flags |= ASYNC_CALLOUT_ACTIVE;
1956 return 0;
1957 }
1958
1959
1960
1961
1962
1963 if (filp->f_flags & O_NONBLOCK) {
1964 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1965 return -EBUSY;
1966 info->flags |= ASYNC_NORMAL_ACTIVE;
1967 return 0;
1968 }
1969
1970 if (info->normal_termios.c_cflag & CLOCAL)
1971 do_clocal = 1;
1972
1973
1974
1975
1976
1977
1978
1979
1980 retval = 0;
1981 add_wait_queue(&info->open_wait, &wait);
1982 #ifdef SERIAL_DEBUG_OPEN
1983 printk("block_til_ready before block: ttys%d, count = %d\n",
1984 info->line, info->count);
1985 #endif
1986 info->count--;
1987 info->blocked_open++;
1988 while (1) {
1989 cli();
1990 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
1991 serial_out(info, UART_MCR,
1992 serial_inp(info, UART_MCR) |
1993 (UART_MCR_DTR | UART_MCR_RTS));
1994 sti();
1995 current->state = TASK_INTERRUPTIBLE;
1996 if (tty_hung_up_p(filp) ||
1997 !(info->flags & ASYNC_INITIALIZED)) {
1998 #ifdef SERIAL_DO_RESTART
1999 if (info->flags & ASYNC_HUP_NOTIFY)
2000 retval = -EAGAIN;
2001 else
2002 retval = -ERESTARTSYS;
2003 #else
2004 retval = -EAGAIN;
2005 #endif
2006 break;
2007 }
2008 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2009 !(info->flags & ASYNC_CLOSING) &&
2010 (do_clocal || (serial_in(info, UART_MSR) &
2011 UART_MSR_DCD)))
2012 break;
2013 if (current->signal & ~current->blocked) {
2014 retval = -ERESTARTSYS;
2015 break;
2016 }
2017 #ifdef SERIAL_DEBUG_OPEN
2018 printk("block_til_ready blocking: ttys%d, count = %d\n",
2019 info->line, info->count);
2020 #endif
2021 schedule();
2022 }
2023 current->state = TASK_RUNNING;
2024 remove_wait_queue(&info->open_wait, &wait);
2025 if (!tty_hung_up_p(filp))
2026 info->count++;
2027 info->blocked_open--;
2028 #ifdef SERIAL_DEBUG_OPEN
2029 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2030 info->line, info->count);
2031 #endif
2032 if (retval)
2033 return retval;
2034 info->flags |= ASYNC_NORMAL_ACTIVE;
2035 return 0;
2036 }
2037
2038
2039
2040
2041
2042
2043
2044 int rs_open(struct tty_struct *tty, struct file * filp)
2045 {
2046 struct async_struct *info;
2047 int retval, line;
2048
2049 line = MINOR(tty->device) - tty->driver.minor_start;
2050 if ((line < 0) || (line >= NR_PORTS))
2051 return -ENODEV;
2052 info = rs_table + line;
2053 if (serial_paranoia_check(info, tty->device, "rs_open"))
2054 return -ENODEV;
2055
2056 #ifdef SERIAL_DEBUG_OPEN
2057 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2058 info->count);
2059 #endif
2060 info->count++;
2061 tty->driver_data = info;
2062 info->tty = tty;
2063
2064 if (!tmp_buf) {
2065 tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2066 if (!tmp_buf)
2067 return -ENOMEM;
2068 }
2069
2070
2071
2072
2073 retval = startup(info);
2074 if (retval)
2075 return retval;
2076
2077 retval = block_til_ready(tty, filp, info);
2078 if (retval) {
2079 #ifdef SERIAL_DEBUG_OPEN
2080 printk("rs_open returning after block_til_ready with %d\n",
2081 retval);
2082 #endif
2083 return retval;
2084 }
2085
2086 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2087 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2088 *tty->termios = info->normal_termios;
2089 else
2090 *tty->termios = info->callout_termios;
2091 change_speed(info);
2092 }
2093
2094 info->session = current->session;
2095 info->pgrp = current->pgrp;
2096
2097 #ifdef SERIAL_DEBUG_OPEN
2098 printk("rs_open ttys%d successful...", info->line);
2099 #endif
2100 return 0;
2101 }
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116 static void show_serial_version(void)
2117 {
2118 printk("Serial driver version 4.00 with");
2119 #ifdef CONFIG_HUB6
2120 printk(" HUB-6");
2121 #define SERIAL_OPT
2122 #endif
2123 #ifdef SERIAL_OPT
2124 printk(" enabled\n");
2125 #else
2126 printk(" no serial options enabled\n");
2127 #endif
2128 #undef SERIAL_OPT
2129 }
2130
2131
2132
2133
2134
2135
2136 static int get_auto_irq(struct async_struct *info)
2137 {
2138 unsigned char save_MCR, save_IER, save_ICP=0;
2139 unsigned short ICP=0, port = info->port;
2140 unsigned long timeout;
2141
2142
2143
2144
2145 rs_irq_triggered = 0;
2146 cli();
2147 save_IER = serial_inp(info, UART_IER);
2148 save_MCR = serial_inp(info, UART_MCR);
2149 if (info->flags & ASYNC_FOURPORT) {
2150 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2151 serial_outp(info, UART_IER, 0x0f);
2152 ICP = (port & 0xFE0) | 0x01F;
2153 save_ICP = inb_p(ICP);
2154 outb_p(0x80, ICP);
2155 (void) inb_p(ICP);
2156 } else {
2157 serial_outp(info, UART_MCR,
2158 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2159 serial_outp(info, UART_IER, 0x0f);
2160 }
2161 sti();
2162
2163
2164
2165 (void)serial_inp(info, UART_LSR);
2166 (void)serial_inp(info, UART_RX);
2167 (void)serial_inp(info, UART_IIR);
2168 (void)serial_inp(info, UART_MSR);
2169
2170 timeout = jiffies+2;
2171 while (timeout >= jiffies) {
2172 if (rs_irq_triggered)
2173 break;
2174 }
2175
2176
2177
2178 cli();
2179 serial_outp(info, UART_IER, save_IER);
2180 serial_outp(info, UART_MCR, save_MCR);
2181 if (info->flags & ASYNC_FOURPORT)
2182 outb_p(save_ICP, ICP);
2183 sti();
2184 return(rs_irq_triggered);
2185 }
2186
2187
2188
2189
2190
2191 static int do_auto_irq(struct async_struct * info)
2192 {
2193 unsigned port = info->port;
2194 int irq_lines = 0;
2195 int irq_try_1 = 0, irq_try_2 = 0;
2196 int retries;
2197 unsigned long flags;
2198
2199 if (!port)
2200 return 0;
2201
2202
2203 save_flags(flags); sti();
2204
2205 irq_lines = grab_all_interrupts(rs_wild_int_mask);
2206
2207 for (retries = 0; retries < 5; retries++) {
2208 if (!irq_try_1)
2209 irq_try_1 = get_auto_irq(info);
2210 if (!irq_try_2)
2211 irq_try_2 = get_auto_irq(info);
2212 if (irq_try_1 && irq_try_2) {
2213 if (irq_try_1 == irq_try_2)
2214 break;
2215 irq_try_1 = irq_try_2 = 0;
2216 }
2217 }
2218 restore_flags(flags);
2219 free_all_interrupts(irq_lines);
2220 return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
2221 }
2222
2223
2224
2225
2226
2227
2228
2229
2230 static void autoconfig(struct async_struct * info)
2231 {
2232 unsigned char status1, status2, scratch, scratch2;
2233 unsigned port = info->port;
2234 unsigned long flags;
2235
2236 info->type = PORT_UNKNOWN;
2237
2238 if (!port)
2239 return;
2240
2241 save_flags(flags); cli();
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252 scratch = serial_inp(info, UART_IER);
2253 serial_outp(info, UART_IER, 0);
2254 outb(0xff, 0x080);
2255 scratch2 = serial_inp(info, UART_IER);
2256 serial_outp(info, UART_IER, scratch);
2257 if (scratch2) {
2258 restore_flags(flags);
2259 return;
2260 }
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271 if (!(info->flags & ASYNC_SKIP_TEST)) {
2272 scratch = serial_inp(info, UART_MCR);
2273 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2274 scratch2 = serial_inp(info, UART_MSR);
2275 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2276 status1 = serial_inp(info, UART_MSR) & 0xF0;
2277 serial_outp(info, UART_MCR, scratch);
2278 serial_outp(info, UART_MSR, scratch2);
2279 if (status1 != 0x90) {
2280 restore_flags(flags);
2281 return;
2282 }
2283 }
2284
2285
2286
2287
2288
2289 if (info->flags & ASYNC_AUTO_IRQ)
2290 info->irq = do_auto_irq(info);
2291
2292 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2293 scratch = serial_in(info, UART_IIR) >> 6;
2294 info->xmit_fifo_size = 1;
2295 switch (scratch) {
2296 case 0:
2297 info->type = PORT_16450;
2298 break;
2299 case 1:
2300 info->type = PORT_UNKNOWN;
2301 break;
2302 case 2:
2303 info->type = PORT_16550;
2304 break;
2305 case 3:
2306 info->type = PORT_16550A;
2307 info->xmit_fifo_size = 16;
2308 break;
2309 }
2310 if (info->type == PORT_16450) {
2311 scratch = serial_in(info, UART_SCR);
2312 serial_outp(info, UART_SCR, 0xa5);
2313 status1 = serial_in(info, UART_SCR);
2314 serial_outp(info, UART_SCR, 0x5a);
2315 status2 = serial_in(info, UART_SCR);
2316 serial_outp(info, UART_SCR, scratch);
2317
2318 if ((status1 != 0xa5) || (status2 != 0x5a))
2319 info->type = PORT_8250;
2320 }
2321
2322
2323
2324
2325 serial_outp(info, UART_MCR, 0x00);
2326 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
2327 UART_FCR_CLEAR_XMIT));
2328 (void)serial_in(info, UART_RX);
2329
2330 restore_flags(flags);
2331 }
2332
2333
2334
2335
2336 long rs_init(long kmem_start)
2337 {
2338 int i;
2339 struct async_struct * info;
2340
2341 bh_base[SERIAL_BH].routine = do_serial_bh;
2342 timer_table[RS_TIMER].fn = rs_timer;
2343 timer_table[RS_TIMER].expires = 0;
2344 #ifdef CONFIG_AUTO_IRQ
2345 rs_wild_int_mask = check_wild_interrupts(1);
2346 #endif
2347
2348 for (i = 0; i < 16; i++) {
2349 IRQ_ports[i] = 0;
2350 IRQ_timeout[i] = 0;
2351 }
2352
2353 show_serial_version();
2354
2355
2356
2357 memset(&serial_driver, 0, sizeof(struct tty_driver));
2358 serial_driver.magic = TTY_DRIVER_MAGIC;
2359 serial_driver.name = "ttyS";
2360 serial_driver.major = TTY_MAJOR;
2361 serial_driver.minor_start = 64;
2362 serial_driver.num = NR_PORTS;
2363 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2364 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2365 serial_driver.init_termios = tty_std_termios;
2366 serial_driver.init_termios.c_cflag =
2367 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2368 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2369 serial_driver.refcount = &serial_refcount;
2370 serial_driver.table = serial_table;
2371 serial_driver.termios = serial_termios;
2372 serial_driver.termios_locked = serial_termios_locked;
2373
2374 serial_driver.open = rs_open;
2375 serial_driver.close = rs_close;
2376 serial_driver.write = rs_write;
2377 serial_driver.put_char = rs_put_char;
2378 serial_driver.flush_chars = rs_flush_chars;
2379 serial_driver.write_room = rs_write_room;
2380 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2381 serial_driver.flush_buffer = rs_flush_buffer;
2382 serial_driver.ioctl = rs_ioctl;
2383 serial_driver.throttle = rs_throttle;
2384 serial_driver.unthrottle = rs_unthrottle;
2385 serial_driver.set_termios = rs_set_termios;
2386 serial_driver.stop = rs_stop;
2387 serial_driver.start = rs_start;
2388 serial_driver.hangup = rs_hangup;
2389
2390
2391
2392
2393
2394 callout_driver = serial_driver;
2395 callout_driver.name = "cua";
2396 callout_driver.major = TTYAUX_MAJOR;
2397 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2398
2399 if (tty_register_driver(&serial_driver))
2400 panic("Couldn't register serial driver\n");
2401 if (tty_register_driver(&callout_driver))
2402 panic("Couldn't register callout driver\n");
2403
2404 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
2405 info->magic = SERIAL_MAGIC;
2406 info->line = i;
2407 info->tty = 0;
2408 info->type = PORT_UNKNOWN;
2409 info->custom_divisor = 0;
2410 info->close_delay = 50;
2411 info->x_char = 0;
2412 info->event = 0;
2413 info->count = 0;
2414 info->blocked_open = 0;
2415 info->tqueue.routine = do_softint;
2416 info->tqueue.data = info;
2417 info->callout_termios =callout_driver.init_termios;
2418 info->normal_termios = serial_driver.init_termios;
2419 info->open_wait = 0;
2420 info->close_wait = 0;
2421 info->next_port = 0;
2422 info->prev_port = 0;
2423 if (info->irq == 2)
2424 info->irq = 9;
2425 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2426 continue;
2427 autoconfig(info);
2428 if (info->type == PORT_UNKNOWN)
2429 continue;
2430 printk("tty%02d%s at 0x%04x (irq = %d)", info->line,
2431 (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2432 info->port, info->irq);
2433 switch (info->type) {
2434 case PORT_8250:
2435 printk(" is a 8250\n");
2436 break;
2437 case PORT_16450:
2438 printk(" is a 16450\n");
2439 break;
2440 case PORT_16550:
2441 printk(" is a 16550\n");
2442 break;
2443 case PORT_16550A:
2444 printk(" is a 16550A\n");
2445 break;
2446 default:
2447 printk("\n");
2448 break;
2449 }
2450 }
2451 return kmem_start;
2452 }
2453
2454
2455
2456
2457
2458 int register_serial(struct serial_struct *req)
2459 {
2460 int i;
2461 unsigned long flags;
2462 struct async_struct *info;
2463
2464 save_flags(flags);
2465 cli();
2466 for (i = 0; i < NR_PORTS; i++) {
2467 if (rs_table[i].port == req->port)
2468 break;
2469 }
2470 if (i == NR_PORTS) {
2471 for (i = 0; i < NR_PORTS; i++)
2472 if ((rs_table[i].type == PORT_UNKNOWN) &&
2473 (rs_table[i].count == 0))
2474 break;
2475 }
2476 if (i == NR_PORTS) {
2477 restore_flags(flags);
2478 return -1;
2479 }
2480 info = &rs_table[i];
2481 if (rs_table[i].count) {
2482 restore_flags(flags);
2483 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
2484 "device already open\n", i, req->port, req->irq);
2485 return -1;
2486 }
2487 info->irq = req->irq;
2488 info->port = req->port;
2489 autoconfig(info);
2490 if (info->type == PORT_UNKNOWN) {
2491 restore_flags(flags);
2492 printk("register_serial(): autoconfig failed\n");
2493 return -1;
2494 }
2495 printk("tty%02d at 0x%04x (irq = %d)", info->line,
2496 info->port, info->irq);
2497 switch (info->type) {
2498 case PORT_8250:
2499 printk(" is a 8250\n"); break;
2500 case PORT_16450:
2501 printk(" is a 16450\n"); break;
2502 case PORT_16550:
2503 printk(" is a 16550\n"); break;
2504 case PORT_16550A:
2505 printk(" is a 16550A\n"); break;
2506 default:
2507 printk("\n"); break;
2508 }
2509 restore_flags(flags);
2510 return info->line;
2511 }
2512
2513 void unregister_serial(int line)
2514 {
2515 unsigned long flags;
2516 struct async_struct *info = &rs_table[line];
2517
2518 save_flags(flags);
2519 cli();
2520 if (info->tty)
2521 tty_hangup(info->tty);
2522 info->type = PORT_UNKNOWN;
2523 printk("tty%02d unloaded\n", info->line);
2524 restore_flags(flags);
2525 }