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