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, struct pt_regs * regs)
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, struct pt_regs * regs)
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, struct pt_regs * regs)
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, struct pt_regs * regs)
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, struct pt_regs * regs)
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, NULL);
763 } else
764 rs_interrupt_single(i, NULL);
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, NULL);
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, struct pt_regs *);
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 #ifdef __alpha__
945 info->MCR |= UART_MCR_OUT1 | UART_MCR_OUT2;
946 info->MCR_noint |= UART_MCR_OUT1 | UART_MCR_OUT2;
947 #endif
948 if (info->irq == 0)
949 info->MCR = info->MCR_noint;
950 serial_outp(info, UART_MCR, info->MCR);
951
952
953
954
955 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
956 serial_outp(info, UART_IER, info->IER);
957
958 if (info->flags & ASYNC_FOURPORT) {
959
960 ICP = (info->port & 0xFE0) | 0x01F;
961 outb_p(0x80, ICP);
962 (void) inb_p(ICP);
963 }
964
965
966
967
968 (void)serial_inp(info, UART_LSR);
969 (void)serial_inp(info, UART_RX);
970 (void)serial_inp(info, UART_IIR);
971 (void)serial_inp(info, UART_MSR);
972
973 if (info->tty)
974 clear_bit(TTY_IO_ERROR, &info->tty->flags);
975 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
976
977
978
979
980 info->prev_port = 0;
981 info->next_port = IRQ_ports[info->irq];
982 if (info->next_port)
983 info->next_port->prev_port = info;
984 IRQ_ports[info->irq] = info;
985 figure_IRQ_timeout(info->irq);
986
987
988
989
990 timer_table[RS_TIMER].expires = jiffies + 2;
991 timer_active |= 1 << RS_TIMER;
992
993
994
995
996 change_speed(info);
997
998 info->flags |= ASYNC_INITIALIZED;
999 restore_flags(flags);
1000 return 0;
1001 }
1002
1003
1004
1005
1006
1007 static void shutdown(struct async_struct * info)
1008 {
1009 unsigned long flags;
1010 int retval;
1011
1012 if (!(info->flags & ASYNC_INITIALIZED))
1013 return;
1014
1015 #ifdef SERIAL_DEBUG_OPEN
1016 printk("Shutting down serial port %d (irq %d)....", info->line,
1017 info->irq);
1018 #endif
1019
1020 save_flags(flags); cli();
1021
1022
1023
1024
1025 if (info->next_port)
1026 info->next_port->prev_port = info->prev_port;
1027 if (info->prev_port)
1028 info->prev_port->next_port = info->next_port;
1029 else
1030 IRQ_ports[info->irq] = info->next_port;
1031 figure_IRQ_timeout(info->irq);
1032
1033
1034
1035
1036 if (info->irq && (!IRQ_ports[info->irq] ||
1037 !IRQ_ports[info->irq]->next_port)) {
1038 if (IRQ_ports[info->irq]) {
1039 free_irq(info->irq);
1040 retval = request_irq(info->irq, rs_interrupt_single, SA_INTERRUPT, "serial");
1041
1042 if (retval)
1043 printk("serial shutdown: request_irq: error %d"
1044 " Couldn't reacquire IRQ.\n", retval);
1045 } else
1046 free_irq(info->irq);
1047 }
1048
1049 if (info->xmit_buf) {
1050 free_page((unsigned long) info->xmit_buf);
1051 info->xmit_buf = 0;
1052 }
1053
1054 info->IER = 0;
1055 serial_outp(info, UART_IER, 0x00);
1056 if (info->flags & ASYNC_FOURPORT) {
1057
1058 (void) inb((info->port & 0xFE0) | 0x01F);
1059 }
1060
1061 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
1062 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1063 info->MCR_noint &= ~(UART_MCR_DTR|UART_MCR_RTS);
1064 }
1065 serial_outp(info, UART_MCR, info->MCR_noint);
1066
1067
1068 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
1069 UART_FCR_CLEAR_XMIT));
1070 (void)serial_in(info, UART_RX);
1071
1072 if (info->tty)
1073 set_bit(TTY_IO_ERROR, &info->tty->flags);
1074
1075 info->flags &= ~ASYNC_INITIALIZED;
1076 restore_flags(flags);
1077 }
1078
1079
1080
1081
1082
1083 static void change_speed(struct async_struct *info)
1084 {
1085 unsigned short port;
1086 int quot = 0;
1087 unsigned cflag,cval,fcr;
1088 int i;
1089
1090 if (!info->tty || !info->tty->termios)
1091 return;
1092 cflag = info->tty->termios->c_cflag;
1093 if (!(port = info->port))
1094 return;
1095 i = cflag & CBAUD;
1096 if (i & CBAUDEX) {
1097 i &= ~CBAUDEX;
1098 if (i < 1 || i > 2)
1099 info->tty->termios->c_cflag &= ~CBAUDEX;
1100 else
1101 i += 15;
1102 }
1103 if (i == 15) {
1104 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1105 i += 1;
1106 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1107 i += 2;
1108 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
1109 quot = info->custom_divisor;
1110 }
1111 if (quot) {
1112 info->timeout = ((info->xmit_fifo_size*HZ*15*quot) /
1113 info->baud_base) + 2;
1114 } else if (baud_table[i] == 134) {
1115 quot = (2*info->baud_base / 269);
1116 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
1117 } else if (baud_table[i]) {
1118 quot = info->baud_base / baud_table[i];
1119 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
1120 } else {
1121 quot = 0;
1122 info->timeout = 0;
1123 }
1124 if (quot) {
1125 info->MCR |= UART_MCR_DTR;
1126 info->MCR_noint |= UART_MCR_DTR;
1127 cli();
1128 serial_out(info, UART_MCR, info->MCR);
1129 sti();
1130 } else {
1131 info->MCR &= ~UART_MCR_DTR;
1132 info->MCR_noint &= ~UART_MCR_DTR;
1133 cli();
1134 serial_out(info, UART_MCR, info->MCR);
1135 sti();
1136 return;
1137 }
1138
1139 cval = cflag & (CSIZE | CSTOPB);
1140 cval >>= 4;
1141 if (cflag & PARENB)
1142 cval |= UART_LCR_PARITY;
1143 if (!(cflag & PARODD))
1144 cval |= UART_LCR_EPAR;
1145 if (info->type == PORT_16550A) {
1146 if ((info->baud_base / quot) < 2400)
1147 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1148 else
1149 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1150 } else
1151 fcr = 0;
1152
1153
1154 info->IER &= ~UART_IER_MSI;
1155 if (cflag & CRTSCTS) {
1156 info->flags |= ASYNC_CTS_FLOW;
1157 info->IER |= UART_IER_MSI;
1158 } else
1159 info->flags &= ~ASYNC_CTS_FLOW;
1160 if (cflag & CLOCAL)
1161 info->flags &= ~ASYNC_CHECK_CD;
1162 else {
1163 info->flags |= ASYNC_CHECK_CD;
1164 info->IER |= UART_IER_MSI;
1165 }
1166 serial_out(info, UART_IER, info->IER);
1167
1168
1169
1170
1171 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
1172 if (I_INPCK(info->tty))
1173 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1174 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1175 info->read_status_mask |= UART_LSR_BI;
1176
1177 info->ignore_status_mask = 0;
1178 if (I_IGNPAR(info->tty)) {
1179 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1180 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
1181 }
1182 if (I_IGNBRK(info->tty)) {
1183 info->ignore_status_mask |= UART_LSR_BI;
1184 info->read_status_mask |= UART_LSR_BI;
1185
1186
1187
1188
1189 if (I_IGNPAR(info->tty)) {
1190 info->ignore_status_mask |= UART_LSR_OE;
1191 info->read_status_mask |= UART_LSR_OE;
1192 }
1193 }
1194
1195 cli();
1196 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);
1197 serial_outp(info, UART_DLL, quot & 0xff);
1198 serial_outp(info, UART_DLM, quot >> 8);
1199 serial_outp(info, UART_LCR, cval);
1200 serial_outp(info, UART_FCR, fcr);
1201 sti();
1202 }
1203
1204 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1205 {
1206 struct async_struct *info = (struct async_struct *)tty->driver_data;
1207 unsigned long flags;
1208
1209 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1210 return;
1211
1212 if (!tty || !info->xmit_buf)
1213 return;
1214
1215 save_flags(flags); cli();
1216 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1217 restore_flags(flags);
1218 return;
1219 }
1220
1221 info->xmit_buf[info->xmit_head++] = ch;
1222 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1223 info->xmit_cnt++;
1224 restore_flags(flags);
1225 }
1226
1227 static void rs_flush_chars(struct tty_struct *tty)
1228 {
1229 struct async_struct *info = (struct async_struct *)tty->driver_data;
1230 unsigned long flags;
1231
1232 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1233 return;
1234
1235 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1236 !info->xmit_buf)
1237 return;
1238
1239 save_flags(flags); cli();
1240 info->IER |= UART_IER_THRI;
1241 serial_out(info, UART_IER, info->IER);
1242 restore_flags(flags);
1243 }
1244
1245 static int rs_write(struct tty_struct * tty, int from_user,
1246 unsigned char *buf, int count)
1247 {
1248 int c, total = 0;
1249 struct async_struct *info = (struct async_struct *)tty->driver_data;
1250 unsigned long flags;
1251
1252 if (serial_paranoia_check(info, tty->device, "rs_write"))
1253 return 0;
1254
1255 if (!tty || !info->xmit_buf || !tmp_buf)
1256 return 0;
1257
1258 save_flags(flags);
1259 while (1) {
1260 cli();
1261 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1262 SERIAL_XMIT_SIZE - info->xmit_head));
1263 if (c <= 0)
1264 break;
1265
1266 if (from_user) {
1267 down(&tmp_buf_sem);
1268 memcpy_fromfs(tmp_buf, buf, c);
1269 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1270 SERIAL_XMIT_SIZE - info->xmit_head));
1271 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1272 up(&tmp_buf_sem);
1273 } else
1274 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1275 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1276 info->xmit_cnt += c;
1277 restore_flags(flags);
1278 buf += c;
1279 count -= c;
1280 total += c;
1281 }
1282 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1283 !(info->IER & UART_IER_THRI)) {
1284 info->IER |= UART_IER_THRI;
1285 serial_out(info, UART_IER, info->IER);
1286 }
1287 restore_flags(flags);
1288 return total;
1289 }
1290
1291 static int rs_write_room(struct tty_struct *tty)
1292 {
1293 struct async_struct *info = (struct async_struct *)tty->driver_data;
1294 int ret;
1295
1296 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1297 return 0;
1298 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1299 if (ret < 0)
1300 ret = 0;
1301 return ret;
1302 }
1303
1304 static int rs_chars_in_buffer(struct tty_struct *tty)
1305 {
1306 struct async_struct *info = (struct async_struct *)tty->driver_data;
1307
1308 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1309 return 0;
1310 return info->xmit_cnt;
1311 }
1312
1313 static void rs_flush_buffer(struct tty_struct *tty)
1314 {
1315 struct async_struct *info = (struct async_struct *)tty->driver_data;
1316
1317 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1318 return;
1319 cli();
1320 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1321 sti();
1322 wake_up_interruptible(&tty->write_wait);
1323 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1324 tty->ldisc.write_wakeup)
1325 (tty->ldisc.write_wakeup)(tty);
1326 }
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336 static void rs_throttle(struct tty_struct * tty)
1337 {
1338 struct async_struct *info = (struct async_struct *)tty->driver_data;
1339 #ifdef SERIAL_DEBUG_THROTTLE
1340 char buf[64];
1341
1342 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1343 tty->ldisc.chars_in_buffer(tty));
1344 #endif
1345
1346 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1347 return;
1348
1349 if (I_IXOFF(tty))
1350 info->x_char = STOP_CHAR(tty);
1351
1352 info->MCR &= ~UART_MCR_RTS;
1353 info->MCR_noint &= ~UART_MCR_RTS;
1354 cli();
1355 serial_out(info, UART_MCR, info->MCR);
1356 sti();
1357 }
1358
1359 static void rs_unthrottle(struct tty_struct * tty)
1360 {
1361 struct async_struct *info = (struct async_struct *)tty->driver_data;
1362 #ifdef SERIAL_DEBUG_THROTTLE
1363 char buf[64];
1364
1365 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1366 tty->ldisc.chars_in_buffer(tty));
1367 #endif
1368
1369 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1370 return;
1371
1372 if (I_IXOFF(tty)) {
1373 if (info->x_char)
1374 info->x_char = 0;
1375 else
1376 info->x_char = START_CHAR(tty);
1377 }
1378 info->MCR |= UART_MCR_RTS;
1379 info->MCR_noint |= UART_MCR_RTS;
1380 cli();
1381 serial_out(info, UART_MCR, info->MCR);
1382 sti();
1383 }
1384
1385
1386
1387
1388
1389
1390
1391 static int get_serial_info(struct async_struct * info,
1392 struct serial_struct * retinfo)
1393 {
1394 struct serial_struct tmp;
1395
1396 if (!retinfo)
1397 return -EFAULT;
1398 memset(&tmp, 0, sizeof(tmp));
1399 tmp.type = info->type;
1400 tmp.line = info->line;
1401 tmp.port = info->port;
1402 tmp.irq = info->irq;
1403 tmp.flags = info->flags;
1404 tmp.baud_base = info->baud_base;
1405 tmp.close_delay = info->close_delay;
1406 tmp.custom_divisor = info->custom_divisor;
1407 tmp.hub6 = info->hub6;
1408 memcpy_tofs(retinfo,&tmp,sizeof(*retinfo));
1409 return 0;
1410 }
1411
1412 static int set_serial_info(struct async_struct * info,
1413 struct serial_struct * new_info)
1414 {
1415 struct serial_struct new_serial;
1416 struct async_struct old_info;
1417 unsigned int i,change_irq,change_port;
1418 int retval = 0;
1419
1420 if (!new_info)
1421 return -EFAULT;
1422 memcpy_fromfs(&new_serial,new_info,sizeof(new_serial));
1423 old_info = *info;
1424
1425 change_irq = new_serial.irq != info->irq;
1426 change_port = (new_serial.port != info->port) || (new_serial.hub6 != info->hub6);
1427
1428 if (!suser()) {
1429 if (change_irq || change_port ||
1430 (new_serial.baud_base != info->baud_base) ||
1431 (new_serial.type != info->type) ||
1432 (new_serial.close_delay != info->close_delay) ||
1433 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1434 (info->flags & ~ASYNC_USR_MASK)))
1435 return -EPERM;
1436 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1437 (new_serial.flags & ASYNC_USR_MASK));
1438 info->custom_divisor = new_serial.custom_divisor;
1439 goto check_and_exit;
1440 }
1441
1442 if (new_serial.irq == 2)
1443 new_serial.irq = 9;
1444
1445 if ((new_serial.irq > 15) || (new_serial.port > 0xffff) ||
1446 (new_serial.type < PORT_UNKNOWN) || (new_serial.type > PORT_MAX)) {
1447 return -EINVAL;
1448 }
1449
1450
1451 if (new_serial.type) {
1452 for (i = 0 ; i < NR_PORTS; i++)
1453 if ((info != &rs_table[i]) &&
1454 (rs_table[i].port == new_serial.port) &&
1455 rs_table[i].type)
1456 return -EADDRINUSE;
1457 }
1458
1459 if ((change_port || change_irq) && (info->count > 1))
1460 return -EBUSY;
1461
1462
1463
1464
1465
1466
1467 info->baud_base = new_serial.baud_base;
1468 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1469 (new_serial.flags & ASYNC_FLAGS));
1470 info->custom_divisor = new_serial.custom_divisor;
1471 info->type = new_serial.type;
1472 info->close_delay = new_serial.close_delay;
1473
1474 release_region(info->port,8);
1475 if (change_port || change_irq) {
1476
1477
1478
1479
1480 shutdown(info);
1481 info->irq = new_serial.irq;
1482 info->port = new_serial.port;
1483 info->hub6 = new_serial.hub6;
1484 }
1485 if(info->type != PORT_UNKNOWN)
1486 request_region(info->port,8,"serial(set)");
1487
1488
1489 check_and_exit:
1490 if (!info->port || !info->type)
1491 return 0;
1492 if (info->flags & ASYNC_INITIALIZED) {
1493 if (((old_info.flags & ASYNC_SPD_MASK) !=
1494 (info->flags & ASYNC_SPD_MASK)) ||
1495 (old_info.custom_divisor != info->custom_divisor))
1496 change_speed(info);
1497 } else
1498 retval = startup(info);
1499 return retval;
1500 }
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1514 {
1515 unsigned char status;
1516 unsigned int result;
1517
1518 cli();
1519 status = serial_in(info, UART_LSR);
1520 sti();
1521 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1522 put_fs_long(result,(unsigned long *) value);
1523 return 0;
1524 }
1525
1526
1527 static int get_modem_info(struct async_struct * info, unsigned int *value)
1528 {
1529 unsigned char control, status;
1530 unsigned int result;
1531
1532 control = info->MCR;
1533 cli();
1534 status = serial_in(info, UART_MSR);
1535 sti();
1536 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1537 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1538 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1539 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1540 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1541 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1542 put_fs_long(result,(unsigned long *) value);
1543 return 0;
1544 }
1545
1546 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1547 unsigned int *value)
1548 {
1549 int error;
1550 unsigned int arg;
1551
1552 error = verify_area(VERIFY_READ, value, sizeof(int));
1553 if (error)
1554 return error;
1555 arg = get_fs_long((unsigned long *) value);
1556 switch (cmd) {
1557 case TIOCMBIS:
1558 if (arg & TIOCM_RTS) {
1559 info->MCR |= UART_MCR_RTS;
1560 info->MCR_noint |= UART_MCR_RTS;
1561 }
1562 if (arg & TIOCM_DTR) {
1563 info->MCR |= UART_MCR_DTR;
1564 info->MCR_noint |= UART_MCR_DTR;
1565 }
1566 break;
1567 case TIOCMBIC:
1568 if (arg & TIOCM_RTS) {
1569 info->MCR &= ~UART_MCR_RTS;
1570 info->MCR_noint &= ~UART_MCR_RTS;
1571 }
1572 if (arg & TIOCM_DTR) {
1573 info->MCR &= ~UART_MCR_DTR;
1574 info->MCR_noint &= ~UART_MCR_DTR;
1575 }
1576 break;
1577 case TIOCMSET:
1578 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR))
1579 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1580 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1581 info->MCR_noint = ((info->MCR_noint
1582 & ~(UART_MCR_RTS | UART_MCR_DTR))
1583 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1584 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1585 break;
1586 default:
1587 return -EINVAL;
1588 }
1589 cli();
1590 serial_out(info, UART_MCR, info->MCR);
1591 sti();
1592 return 0;
1593 }
1594
1595 static int do_autoconfig(struct async_struct * info)
1596 {
1597 int retval;
1598
1599 if (!suser())
1600 return -EPERM;
1601
1602 if (info->count > 1)
1603 return -EBUSY;
1604
1605 shutdown(info);
1606
1607 cli();
1608 autoconfig(info);
1609 sti();
1610
1611 retval = startup(info);
1612 if (retval)
1613 return retval;
1614 return 0;
1615 }
1616
1617
1618
1619
1620
1621 static void send_break( struct async_struct * info, int duration)
1622 {
1623 if (!info->port)
1624 return;
1625 current->state = TASK_INTERRUPTIBLE;
1626 current->timeout = jiffies + duration;
1627 cli();
1628 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) | UART_LCR_SBC);
1629 schedule();
1630 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1631 sti();
1632 }
1633
1634
1635
1636
1637
1638 static int check_wild_interrupts(int doprint)
1639 {
1640 int i, mask;
1641 int wild_interrupts = 0;
1642 int irq_lines;
1643 unsigned long timeout;
1644 unsigned long flags;
1645
1646
1647 save_flags(flags); sti();
1648
1649 irq_lines = grab_all_interrupts(0);
1650
1651
1652
1653
1654
1655 timeout = jiffies+10;
1656 while (timeout >= jiffies)
1657 ;
1658
1659 rs_triggered = 0;
1660
1661 timeout = jiffies+10;
1662 while (timeout >= jiffies)
1663 ;
1664
1665 for (i = 0, mask = 1; i < 16; i++, mask <<= 1) {
1666 if ((rs_triggered & (1 << i)) &&
1667 (irq_lines & (1 << i))) {
1668 wild_interrupts |= mask;
1669 if (doprint)
1670 printk("Wild interrupt? (IRQ %d)\n", i);
1671 }
1672 }
1673 free_all_interrupts(irq_lines);
1674 restore_flags(flags);
1675 return wild_interrupts;
1676 }
1677
1678 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1679 unsigned int cmd, unsigned long arg)
1680 {
1681 int error;
1682 struct async_struct * info = (struct async_struct *)tty->driver_data;
1683 int retval;
1684
1685 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1686 return -ENODEV;
1687
1688 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1689 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1690 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1691 if (tty->flags & (1 << TTY_IO_ERROR))
1692 return -EIO;
1693 }
1694
1695 switch (cmd) {
1696 case TCSBRK:
1697 retval = tty_check_change(tty);
1698 if (retval)
1699 return retval;
1700 wait_until_sent(tty, 0);
1701 if (!arg)
1702 send_break(info, HZ/4);
1703 return 0;
1704 case TCSBRKP:
1705 retval = tty_check_change(tty);
1706 if (retval)
1707 return retval;
1708 wait_until_sent(tty, 0);
1709 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1710 return 0;
1711 case TIOCGSOFTCAR:
1712 error = verify_area(VERIFY_WRITE, (void *) arg,sizeof(long));
1713 if (error)
1714 return error;
1715 put_fs_long(C_CLOCAL(tty) ? 1 : 0,
1716 (unsigned long *) arg);
1717 return 0;
1718 case TIOCSSOFTCAR:
1719 arg = get_fs_long((unsigned long *) arg);
1720 tty->termios->c_cflag =
1721 ((tty->termios->c_cflag & ~CLOCAL) |
1722 (arg ? CLOCAL : 0));
1723 return 0;
1724 case TIOCMGET:
1725 error = verify_area(VERIFY_WRITE, (void *) arg,
1726 sizeof(unsigned int));
1727 if (error)
1728 return error;
1729 return get_modem_info(info, (unsigned int *) arg);
1730 case TIOCMBIS:
1731 case TIOCMBIC:
1732 case TIOCMSET:
1733 return set_modem_info(info, cmd, (unsigned int *) arg);
1734 case TIOCGSERIAL:
1735 error = verify_area(VERIFY_WRITE, (void *) arg,
1736 sizeof(struct serial_struct));
1737 if (error)
1738 return error;
1739 return get_serial_info(info,
1740 (struct serial_struct *) arg);
1741 case TIOCSSERIAL:
1742 return set_serial_info(info,
1743 (struct serial_struct *) arg);
1744 case TIOCSERCONFIG:
1745 return do_autoconfig(info);
1746
1747 case TIOCSERGWILD:
1748 error = verify_area(VERIFY_WRITE, (void *) arg,
1749 sizeof(int));
1750 if (error)
1751 return error;
1752 put_fs_long(rs_wild_int_mask, (unsigned long *) arg);
1753 return 0;
1754
1755 case TIOCSERGETLSR:
1756 error = verify_area(VERIFY_WRITE, (void *) arg,
1757 sizeof(unsigned int));
1758 if (error)
1759 return error;
1760 else
1761 return get_lsr_info(info, (unsigned int *) arg);
1762
1763 case TIOCSERSWILD:
1764 if (!suser())
1765 return -EPERM;
1766 rs_wild_int_mask = get_fs_long((unsigned long *) arg);
1767 if (rs_wild_int_mask < 0)
1768 rs_wild_int_mask = check_wild_interrupts(0);
1769 return 0;
1770
1771 case TIOCSERGSTRUCT:
1772 error = verify_area(VERIFY_WRITE, (void *) arg,
1773 sizeof(struct async_struct));
1774 if (error)
1775 return error;
1776 memcpy_tofs((struct async_struct *) arg,
1777 info, sizeof(struct async_struct));
1778 return 0;
1779
1780 default:
1781 return -ENOIOCTLCMD;
1782 }
1783 return 0;
1784 }
1785
1786 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1787 {
1788 struct async_struct *info = (struct async_struct *)tty->driver_data;
1789
1790 if (tty->termios->c_cflag == old_termios->c_cflag)
1791 return;
1792
1793 change_speed(info);
1794
1795 if ((old_termios->c_cflag & CRTSCTS) &&
1796 !(tty->termios->c_cflag & CRTSCTS)) {
1797 tty->hw_stopped = 0;
1798 rs_start(tty);
1799 }
1800
1801 #if 0
1802
1803
1804
1805
1806
1807
1808 if (!(old_termios->c_cflag & CLOCAL) &&
1809 (tty->termios->c_cflag & CLOCAL))
1810 wake_up_interruptible(&info->open_wait);
1811 #endif
1812 }
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824 static void rs_close(struct tty_struct *tty, struct file * filp)
1825 {
1826 struct async_struct * info = (struct async_struct *)tty->driver_data;
1827 unsigned long flags;
1828 unsigned long timeout;
1829
1830 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1831 return;
1832
1833 save_flags(flags); cli();
1834
1835 if (tty_hung_up_p(filp)) {
1836 restore_flags(flags);
1837 return;
1838 }
1839
1840 #ifdef SERIAL_DEBUG_OPEN
1841 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1842 #endif
1843 if ((tty->count == 1) && (info->count != 1)) {
1844
1845
1846
1847
1848
1849
1850
1851 printk("rs_close: bad serial port count; tty->count is 1, "
1852 "info->count is %d\n", info->count);
1853 info->count = 1;
1854 }
1855 if (--info->count < 0) {
1856 printk("rs_close: bad serial port count for ttys%d: %d\n",
1857 info->line, info->count);
1858 info->count = 0;
1859 }
1860 if (info->count) {
1861 restore_flags(flags);
1862 return;
1863 }
1864 info->flags |= ASYNC_CLOSING;
1865
1866
1867
1868
1869 if (info->flags & ASYNC_NORMAL_ACTIVE)
1870 info->normal_termios = *tty->termios;
1871 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1872 info->callout_termios = *tty->termios;
1873
1874
1875
1876
1877
1878
1879 info->IER &= ~UART_IER_RLSI;
1880 info->read_status_mask &= ~UART_LSR_DR;
1881 if (info->flags & ASYNC_INITIALIZED) {
1882 serial_out(info, UART_IER, info->IER);
1883 wait_until_sent(tty, 3000);
1884
1885
1886
1887
1888
1889 timeout = jiffies+HZ;
1890 while (!(serial_inp(info, UART_LSR) & UART_LSR_TEMT)) {
1891 current->state = TASK_INTERRUPTIBLE;
1892 current->timeout = jiffies + info->timeout;
1893 schedule();
1894 if (jiffies > timeout)
1895 break;
1896 }
1897 }
1898 shutdown(info);
1899 if (tty->driver.flush_buffer)
1900 tty->driver.flush_buffer(tty);
1901 if (tty->ldisc.flush_buffer)
1902 tty->ldisc.flush_buffer(tty);
1903 info->event = 0;
1904 info->tty = 0;
1905 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1906 if (tty->ldisc.close)
1907 (tty->ldisc.close)(tty);
1908 tty->ldisc = ldiscs[N_TTY];
1909 tty->termios->c_line = N_TTY;
1910 if (tty->ldisc.open)
1911 (tty->ldisc.open)(tty);
1912 }
1913 if (info->blocked_open) {
1914 if (info->close_delay) {
1915 current->state = TASK_INTERRUPTIBLE;
1916 current->timeout = jiffies + info->close_delay;
1917 schedule();
1918 }
1919 wake_up_interruptible(&info->open_wait);
1920 }
1921 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1922 ASYNC_CLOSING);
1923 wake_up_interruptible(&info->close_wait);
1924 restore_flags(flags);
1925 }
1926
1927
1928
1929
1930 void rs_hangup(struct tty_struct *tty)
1931 {
1932 struct async_struct * info = (struct async_struct *)tty->driver_data;
1933
1934 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1935 return;
1936
1937 shutdown(info);
1938 info->event = 0;
1939 info->count = 0;
1940 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1941 info->tty = 0;
1942 wake_up_interruptible(&info->open_wait);
1943 }
1944
1945
1946
1947
1948
1949
1950 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1951 struct async_struct *info)
1952 {
1953 struct wait_queue wait = { current, NULL };
1954 int retval;
1955 int do_clocal = 0;
1956
1957
1958
1959
1960
1961 if (info->flags & ASYNC_CLOSING) {
1962 interruptible_sleep_on(&info->close_wait);
1963 #ifdef SERIAL_DO_RESTART
1964 if (info->flags & ASYNC_HUP_NOTIFY)
1965 return -EAGAIN;
1966 else
1967 return -ERESTARTSYS;
1968 #else
1969 return -EAGAIN;
1970 #endif
1971 }
1972
1973
1974
1975
1976
1977 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1978 if (info->flags & ASYNC_NORMAL_ACTIVE)
1979 return -EBUSY;
1980 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1981 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1982 (info->session != current->session))
1983 return -EBUSY;
1984 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1985 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1986 (info->pgrp != current->pgrp))
1987 return -EBUSY;
1988 info->flags |= ASYNC_CALLOUT_ACTIVE;
1989 return 0;
1990 }
1991
1992
1993
1994
1995
1996 if ((filp->f_flags & O_NONBLOCK) ||
1997 (tty->flags & (1 << TTY_IO_ERROR))) {
1998 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1999 return -EBUSY;
2000 info->flags |= ASYNC_NORMAL_ACTIVE;
2001 return 0;
2002 }
2003
2004 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
2005 if (info->normal_termios.c_cflag & CLOCAL)
2006 do_clocal = 1;
2007 } else {
2008 if (tty->termios->c_cflag & CLOCAL)
2009 do_clocal = 1;
2010 }
2011
2012
2013
2014
2015
2016
2017
2018
2019 retval = 0;
2020 add_wait_queue(&info->open_wait, &wait);
2021 #ifdef SERIAL_DEBUG_OPEN
2022 printk("block_til_ready before block: ttys%d, count = %d\n",
2023 info->line, info->count);
2024 #endif
2025 info->count--;
2026 info->blocked_open++;
2027 while (1) {
2028 cli();
2029 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
2030 serial_out(info, UART_MCR,
2031 serial_inp(info, UART_MCR) |
2032 (UART_MCR_DTR | UART_MCR_RTS));
2033 sti();
2034 current->state = TASK_INTERRUPTIBLE;
2035 if (tty_hung_up_p(filp) ||
2036 !(info->flags & ASYNC_INITIALIZED)) {
2037 #ifdef SERIAL_DO_RESTART
2038 if (info->flags & ASYNC_HUP_NOTIFY)
2039 retval = -EAGAIN;
2040 else
2041 retval = -ERESTARTSYS;
2042 #else
2043 retval = -EAGAIN;
2044 #endif
2045 break;
2046 }
2047 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2048 !(info->flags & ASYNC_CLOSING) &&
2049 (do_clocal || (serial_in(info, UART_MSR) &
2050 UART_MSR_DCD)))
2051 break;
2052 if (current->signal & ~current->blocked) {
2053 retval = -ERESTARTSYS;
2054 break;
2055 }
2056 #ifdef SERIAL_DEBUG_OPEN
2057 printk("block_til_ready blocking: ttys%d, count = %d\n",
2058 info->line, info->count);
2059 #endif
2060 schedule();
2061 }
2062 current->state = TASK_RUNNING;
2063 remove_wait_queue(&info->open_wait, &wait);
2064 if (!tty_hung_up_p(filp))
2065 info->count++;
2066 info->blocked_open--;
2067 #ifdef SERIAL_DEBUG_OPEN
2068 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2069 info->line, info->count);
2070 #endif
2071 if (retval)
2072 return retval;
2073 info->flags |= ASYNC_NORMAL_ACTIVE;
2074 return 0;
2075 }
2076
2077
2078
2079
2080
2081
2082
2083 int rs_open(struct tty_struct *tty, struct file * filp)
2084 {
2085 struct async_struct *info;
2086 int retval, line;
2087
2088 line = MINOR(tty->device) - tty->driver.minor_start;
2089 if ((line < 0) || (line >= NR_PORTS))
2090 return -ENODEV;
2091 info = rs_table + line;
2092 if (serial_paranoia_check(info, tty->device, "rs_open"))
2093 return -ENODEV;
2094
2095 #ifdef SERIAL_DEBUG_OPEN
2096 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2097 info->count);
2098 #endif
2099 info->count++;
2100 tty->driver_data = info;
2101 info->tty = tty;
2102
2103 if (!tmp_buf) {
2104 tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2105 if (!tmp_buf)
2106 return -ENOMEM;
2107 }
2108
2109
2110
2111
2112 retval = startup(info);
2113 if (retval)
2114 return retval;
2115
2116 retval = block_til_ready(tty, filp, info);
2117 if (retval) {
2118 #ifdef SERIAL_DEBUG_OPEN
2119 printk("rs_open returning after block_til_ready with %d\n",
2120 retval);
2121 #endif
2122 return retval;
2123 }
2124
2125 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2126 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2127 *tty->termios = info->normal_termios;
2128 else
2129 *tty->termios = info->callout_termios;
2130 change_speed(info);
2131 }
2132
2133 info->session = current->session;
2134 info->pgrp = current->pgrp;
2135
2136 #ifdef SERIAL_DEBUG_OPEN
2137 printk("rs_open ttys%d successful...", info->line);
2138 #endif
2139 return 0;
2140 }
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155 static void show_serial_version(void)
2156 {
2157 printk("Serial driver version 4.00 with");
2158 #ifdef CONFIG_HUB6
2159 printk(" HUB-6");
2160 #define SERIAL_OPT
2161 #endif
2162 #ifdef SERIAL_OPT
2163 printk(" enabled\n");
2164 #else
2165 printk(" no serial options enabled\n");
2166 #endif
2167 #undef SERIAL_OPT
2168 }
2169
2170
2171
2172
2173
2174
2175 static int get_auto_irq(struct async_struct *info)
2176 {
2177 unsigned char save_MCR, save_IER, save_ICP=0;
2178 unsigned short ICP=0, port = info->port;
2179 unsigned long timeout;
2180
2181
2182
2183
2184 rs_irq_triggered = 0;
2185 cli();
2186 save_IER = serial_inp(info, UART_IER);
2187 save_MCR = serial_inp(info, UART_MCR);
2188 if (info->flags & ASYNC_FOURPORT) {
2189 serial_outp(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2190 serial_outp(info, UART_IER, 0x0f);
2191 ICP = (port & 0xFE0) | 0x01F;
2192 save_ICP = inb_p(ICP);
2193 outb_p(0x80, ICP);
2194 (void) inb_p(ICP);
2195 } else {
2196 serial_outp(info, UART_MCR,
2197 UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2);
2198 serial_outp(info, UART_IER, 0x0f);
2199 }
2200 sti();
2201
2202
2203
2204 (void)serial_inp(info, UART_LSR);
2205 (void)serial_inp(info, UART_RX);
2206 (void)serial_inp(info, UART_IIR);
2207 (void)serial_inp(info, UART_MSR);
2208
2209 timeout = jiffies+2;
2210 while (timeout >= jiffies) {
2211 if (rs_irq_triggered)
2212 break;
2213 }
2214
2215
2216
2217 cli();
2218 serial_outp(info, UART_IER, save_IER);
2219 serial_outp(info, UART_MCR, save_MCR);
2220 if (info->flags & ASYNC_FOURPORT)
2221 outb_p(save_ICP, ICP);
2222 sti();
2223 return(rs_irq_triggered);
2224 }
2225
2226
2227
2228
2229
2230 static int do_auto_irq(struct async_struct * info)
2231 {
2232 unsigned port = info->port;
2233 int irq_lines = 0;
2234 int irq_try_1 = 0, irq_try_2 = 0;
2235 int retries;
2236 unsigned long flags;
2237
2238 if (!port)
2239 return 0;
2240
2241
2242 save_flags(flags); sti();
2243
2244 irq_lines = grab_all_interrupts(rs_wild_int_mask);
2245
2246 for (retries = 0; retries < 5; retries++) {
2247 if (!irq_try_1)
2248 irq_try_1 = get_auto_irq(info);
2249 if (!irq_try_2)
2250 irq_try_2 = get_auto_irq(info);
2251 if (irq_try_1 && irq_try_2) {
2252 if (irq_try_1 == irq_try_2)
2253 break;
2254 irq_try_1 = irq_try_2 = 0;
2255 }
2256 }
2257 restore_flags(flags);
2258 free_all_interrupts(irq_lines);
2259 return (irq_try_1 == irq_try_2) ? irq_try_1 : 0;
2260 }
2261
2262
2263
2264
2265
2266
2267
2268
2269 static void autoconfig(struct async_struct * info)
2270 {
2271 unsigned char status1, status2, scratch, scratch2;
2272 unsigned port = info->port;
2273 unsigned long flags;
2274
2275 info->type = PORT_UNKNOWN;
2276
2277 if (!port)
2278 return;
2279
2280 save_flags(flags); cli();
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291 scratch = serial_inp(info, UART_IER);
2292 serial_outp(info, UART_IER, 0);
2293 outb(0xff, 0x080);
2294 scratch2 = serial_inp(info, UART_IER);
2295 serial_outp(info, UART_IER, scratch);
2296 if (scratch2) {
2297 restore_flags(flags);
2298 return;
2299 }
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310 if (!(info->flags & ASYNC_SKIP_TEST)) {
2311 scratch = serial_inp(info, UART_MCR);
2312 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2313 scratch2 = serial_inp(info, UART_MSR);
2314 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2315 status1 = serial_inp(info, UART_MSR) & 0xF0;
2316 serial_outp(info, UART_MCR, scratch);
2317 serial_outp(info, UART_MSR, scratch2);
2318 if (status1 != 0x90) {
2319 restore_flags(flags);
2320 return;
2321 }
2322 }
2323
2324
2325
2326
2327
2328 if (info->flags & ASYNC_AUTO_IRQ)
2329 info->irq = do_auto_irq(info);
2330
2331 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2332 scratch = serial_in(info, UART_IIR) >> 6;
2333 info->xmit_fifo_size = 1;
2334 switch (scratch) {
2335 case 0:
2336 info->type = PORT_16450;
2337 break;
2338 case 1:
2339 info->type = PORT_UNKNOWN;
2340 break;
2341 case 2:
2342 info->type = PORT_16550;
2343 break;
2344 case 3:
2345 info->type = PORT_16550A;
2346 info->xmit_fifo_size = 16;
2347 break;
2348 }
2349 if (info->type == PORT_16450) {
2350 scratch = serial_in(info, UART_SCR);
2351 serial_outp(info, UART_SCR, 0xa5);
2352 status1 = serial_in(info, UART_SCR);
2353 serial_outp(info, UART_SCR, 0x5a);
2354 status2 = serial_in(info, UART_SCR);
2355 serial_outp(info, UART_SCR, scratch);
2356
2357 if ((status1 != 0xa5) || (status2 != 0x5a))
2358 info->type = PORT_8250;
2359 }
2360 request_region(info->port,8,"serial(auto)");
2361
2362
2363
2364
2365 #ifdef __alpha__
2366
2367
2368
2369
2370 serial_outp(info, UART_MCR, 0x0c);
2371 #else
2372 serial_outp(info, UART_MCR, 0x00);
2373 #endif
2374 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
2375 UART_FCR_CLEAR_XMIT));
2376 (void)serial_in(info, UART_RX);
2377
2378 restore_flags(flags);
2379 }
2380
2381
2382
2383
2384 long rs_init(long kmem_start)
2385 {
2386 int i;
2387 struct async_struct * info;
2388
2389 bh_base[SERIAL_BH].routine = do_serial_bh;
2390 timer_table[RS_TIMER].fn = rs_timer;
2391 timer_table[RS_TIMER].expires = 0;
2392 #ifdef CONFIG_AUTO_IRQ
2393 rs_wild_int_mask = check_wild_interrupts(1);
2394 #endif
2395
2396 for (i = 0; i < 16; i++) {
2397 IRQ_ports[i] = 0;
2398 IRQ_timeout[i] = 0;
2399 }
2400
2401 show_serial_version();
2402
2403
2404
2405 memset(&serial_driver, 0, sizeof(struct tty_driver));
2406 serial_driver.magic = TTY_DRIVER_MAGIC;
2407 serial_driver.name = "ttyS";
2408 serial_driver.major = TTY_MAJOR;
2409 serial_driver.minor_start = 64;
2410 serial_driver.num = NR_PORTS;
2411 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2412 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2413 serial_driver.init_termios = tty_std_termios;
2414 serial_driver.init_termios.c_cflag =
2415 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2416 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2417 serial_driver.refcount = &serial_refcount;
2418 serial_driver.table = serial_table;
2419 serial_driver.termios = serial_termios;
2420 serial_driver.termios_locked = serial_termios_locked;
2421
2422 serial_driver.open = rs_open;
2423 serial_driver.close = rs_close;
2424 serial_driver.write = rs_write;
2425 serial_driver.put_char = rs_put_char;
2426 serial_driver.flush_chars = rs_flush_chars;
2427 serial_driver.write_room = rs_write_room;
2428 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2429 serial_driver.flush_buffer = rs_flush_buffer;
2430 serial_driver.ioctl = rs_ioctl;
2431 serial_driver.throttle = rs_throttle;
2432 serial_driver.unthrottle = rs_unthrottle;
2433 serial_driver.set_termios = rs_set_termios;
2434 serial_driver.stop = rs_stop;
2435 serial_driver.start = rs_start;
2436 serial_driver.hangup = rs_hangup;
2437
2438
2439
2440
2441
2442 callout_driver = serial_driver;
2443 callout_driver.name = "cua";
2444 callout_driver.major = TTYAUX_MAJOR;
2445 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2446
2447 if (tty_register_driver(&serial_driver))
2448 panic("Couldn't register serial driver\n");
2449 if (tty_register_driver(&callout_driver))
2450 panic("Couldn't register callout driver\n");
2451
2452 for (i = 0, info = rs_table; i < NR_PORTS; i++,info++) {
2453 info->magic = SERIAL_MAGIC;
2454 info->line = i;
2455 info->tty = 0;
2456 info->type = PORT_UNKNOWN;
2457 info->custom_divisor = 0;
2458 info->close_delay = 50;
2459 info->x_char = 0;
2460 info->event = 0;
2461 info->count = 0;
2462 info->blocked_open = 0;
2463 info->tqueue.routine = do_softint;
2464 info->tqueue.data = info;
2465 info->callout_termios =callout_driver.init_termios;
2466 info->normal_termios = serial_driver.init_termios;
2467 info->open_wait = 0;
2468 info->close_wait = 0;
2469 info->next_port = 0;
2470 info->prev_port = 0;
2471 if (info->irq == 2)
2472 info->irq = 9;
2473 if (!(info->flags & ASYNC_BOOT_AUTOCONF))
2474 continue;
2475 autoconfig(info);
2476 if (info->type == PORT_UNKNOWN)
2477 continue;
2478 printk("tty%02d%s at 0x%04x (irq = %d)", info->line,
2479 (info->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2480 info->port, info->irq);
2481 switch (info->type) {
2482 case PORT_8250:
2483 printk(" is a 8250\n");
2484 break;
2485 case PORT_16450:
2486 printk(" is a 16450\n");
2487 break;
2488 case PORT_16550:
2489 printk(" is a 16550\n");
2490 break;
2491 case PORT_16550A:
2492 printk(" is a 16550A\n");
2493 break;
2494 default:
2495 printk("\n");
2496 break;
2497 }
2498 }
2499 return kmem_start;
2500 }
2501
2502
2503
2504
2505
2506 int register_serial(struct serial_struct *req)
2507 {
2508 int i;
2509 unsigned long flags;
2510 struct async_struct *info;
2511
2512 save_flags(flags);
2513 cli();
2514 for (i = 0; i < NR_PORTS; i++) {
2515 if (rs_table[i].port == req->port)
2516 break;
2517 }
2518 if (i == NR_PORTS) {
2519 for (i = 0; i < NR_PORTS; i++)
2520 if ((rs_table[i].type == PORT_UNKNOWN) &&
2521 (rs_table[i].count == 0))
2522 break;
2523 }
2524 if (i == NR_PORTS) {
2525 restore_flags(flags);
2526 return -1;
2527 }
2528 info = &rs_table[i];
2529 if (rs_table[i].count) {
2530 restore_flags(flags);
2531 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
2532 "device already open\n", i, req->port, req->irq);
2533 return -1;
2534 }
2535 info->irq = req->irq;
2536 info->port = req->port;
2537 autoconfig(info);
2538 if (info->type == PORT_UNKNOWN) {
2539 restore_flags(flags);
2540 printk("register_serial(): autoconfig failed\n");
2541 return -1;
2542 }
2543 printk("tty%02d at 0x%04x (irq = %d)", info->line,
2544 info->port, info->irq);
2545 switch (info->type) {
2546 case PORT_8250:
2547 printk(" is a 8250\n"); break;
2548 case PORT_16450:
2549 printk(" is a 16450\n"); break;
2550 case PORT_16550:
2551 printk(" is a 16550\n"); break;
2552 case PORT_16550A:
2553 printk(" is a 16550A\n"); break;
2554 default:
2555 printk("\n"); break;
2556 }
2557 restore_flags(flags);
2558 return info->line;
2559 }
2560
2561 void unregister_serial(int line)
2562 {
2563 unsigned long flags;
2564 struct async_struct *info = &rs_table[line];
2565
2566 save_flags(flags);
2567 cli();
2568 if (info->tty)
2569 tty_hangup(info->tty);
2570 info->type = PORT_UNKNOWN;
2571 printk("tty%02d unloaded\n", info->line);
2572 restore_flags(flags);
2573 }