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