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