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