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