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