1 /* $Id: su.c,v 1.54 2001/11/07 14:52:30 davem Exp $
2 * su.c: Small serial driver for keyboard/mouse interface on sparc32/PCI
3 *
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1998-1999 Pete Zaitcev (zaitcev@yahoo.com)
6 *
7 * This is mainly a variation of drivers/char/serial.c,
8 * credits go to authors mentioned therein.
9 *
10 * Fixed to use tty_get_baud_rate().
11 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
12 */
13
14 /*
15 * Configuration section.
16 */
17 #undef SERIAL_PARANOIA_CHECK
18 #define CONFIG_SERIAL_NOPAUSE_IO /* Unused on sparc */
19 #define SERIAL_DO_RESTART
20
21 /* Set of debugging defines */
22
23 #undef SERIAL_DEBUG_INTR
24 #undef SERIAL_DEBUG_OPEN
25 #undef SERIAL_DEBUG_FLOW
26 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
27 #undef SERIAL_DEBUG_THROTTLE
28
29 #define RS_ISR_PASS_LIMIT 256
30
31 /*
32 * 0x20 is sun4m thing, Dave Redman heritage.
33 * See arch/sparc/kernel/irq.c.
34 */
35 #define IRQ_4M(n) ((n)|0x20)
36
37 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
38 #define DBG_CNT(s) \
39 do { \
40 printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
41 kdevname(tty->device), (info->flags), serial_refcount, \
42 info->count,tty->count,s); \
43 } while (0)
44 #else
45 #define DBG_CNT(s)
46 #endif
47
48 /*
49 * End of serial driver configuration section.
50 */
51 #include <linux/config.h>
52 #include <linux/module.h>
53 #include <linux/errno.h>
54 #include <linux/signal.h>
55 #include <linux/sched.h>
56 #include <linux/interrupt.h>
57 #include <linux/tty.h>
58 #include <linux/tty_flip.h>
59 #include <linux/serial.h>
60 #include <linux/serialP.h>
61 #include <linux/serial_reg.h>
62 #include <linux/string.h>
63 #include <linux/fcntl.h>
64 #include <linux/ptrace.h>
65 #include <linux/ioport.h>
66 #include <linux/mm.h>
67 #include <linux/slab.h>
68 #include <linux/init.h>
69 #include <linux/bootmem.h>
70 #include <linux/delay.h>
71 #ifdef CONFIG_SERIAL_CONSOLE
72 #include <linux/console.h>
73 #include <linux/major.h>
74 #endif
75 #include <linux/sysrq.h>
76
77 #include <asm/system.h>
78 #include <asm/oplib.h>
79 #include <asm/io.h>
80 #include <asm/ebus.h>
81 #ifdef CONFIG_SPARC64
82 #include <asm/isa.h>
83 #endif
84 #include <asm/irq.h>
85 #include <asm/uaccess.h>
86 #include <asm/bitops.h>
87
88 #include "sunserial.h"
89 #include "sunkbd.h"
90 #include "sunmouse.h"
91
92 /* We are on a NS PC87303 clocked with 24.0 MHz, which results
93 * in a UART clock of 1.8462 MHz.
94 */
95 #define BAUD_BASE (1846200 / 16)
96
97 #ifdef CONFIG_SERIAL_CONSOLE
98 extern int serial_console;
99 static struct console sercons;
100 int su_serial_console_init(void);
101 #endif
102
103 enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
104 static char *su_typev[] = { "???", "mouse", "kbd", "serial" };
105
106 #define SU_PROPSIZE 128
107
108 /*
109 * serial.c saves memory when it allocates async_info upon first open.
110 * We have parts of state structure together because we do call startup
111 * for keyboard and mouse.
112 */
113 struct su_struct {
114 int magic;
115 unsigned long port;
116 int baud_base;
117 int type; /* Hardware type: e.g. 16550 */
118 int irq;
119 int flags;
120 int line;
121 int cflag;
122
123 enum su_type port_type; /* Hookup type: e.g. mouse */
124 int is_console;
125 int port_node;
126
127 char name[16];
128
129 int xmit_fifo_size;
130 int custom_divisor;
131 unsigned short close_delay;
132 unsigned short closing_wait; /* time to wait before closing */
133
134 struct tty_struct *tty;
135 int read_status_mask;
136 int ignore_status_mask;
137 int timeout;
138 int quot;
139 int x_char; /* xon/xoff character */
140 int IER; /* Interrupt Enable Register */
141 int MCR; /* Modem control register */
142 unsigned long event;
143 int blocked_open; /* # of blocked opens */
144 long session; /* Session of opening process */
145 long pgrp; /* pgrp of opening process */
146 unsigned char *xmit_buf;
147 int xmit_head;
148 int xmit_tail;
149 int xmit_cnt;
150 struct tq_struct tqueue;
151 wait_queue_head_t open_wait;
152 wait_queue_head_t close_wait;
153 wait_queue_head_t delta_msr_wait;
154
155 int count;
156 struct async_icount icount;
157 struct termios normal_termios, callout_termios;
158 unsigned long last_active; /* For async_struct, to be */
159 };
160
161 /*
162 * Scan status structure.
163 * "prop" is a local variable but it eats stack to keep it in each
164 * stack frame of a recursive procedure.
165 */
166 struct su_probe_scan {
167 int msnode, kbnode; /* PROM nodes for mouse and keyboard */
168 int msx, kbx; /* minors for mouse and keyboard */
169 int devices; /* scan index */
170 char prop[SU_PROPSIZE];
171 };
172
173 static char *serial_name = "PCIO serial driver";
174 static char serial_version[16];
175
176 static DECLARE_TASK_QUEUE(tq_serial);
177
178 static struct tty_driver serial_driver, callout_driver;
179 static int serial_refcount;
180
181 /* number of characters left in xmit buffer before we ask for more */
182 #define WAKEUP_CHARS 256
183
184 static void autoconfig(struct su_struct *info);
185 static void change_speed(struct su_struct *info, struct termios *old);
186 static void su_wait_until_sent(struct tty_struct *tty, int timeout);
187
188 /*
189 * Here we define the default xmit fifo size used for each type of
190 * UART
191 */
192 static struct serial_uart_config uart_config[] = {
193 { "unknown", 1, 0 },
194 { "8250", 1, 0 },
195 { "16450", 1, 0 },
196 { "16550", 1, 0 },
197 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
198 { "cirrus", 1, 0 },
199 { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
200 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
201 UART_STARTECH },
202 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
203 { 0, 0}
204 };
205
206
207 #define NR_PORTS 4
208
209 static struct su_struct su_table[NR_PORTS];
210 static struct tty_struct *serial_table[NR_PORTS];
211 static struct termios *serial_termios[NR_PORTS];
212 static struct termios *serial_termios_locked[NR_PORTS];
213
214 #ifndef MIN
215 #define MIN(a,b) ((a) < (b) ? (a) : (b))
216 #endif
217
218 /*
219 * tmp_buf is used as a temporary buffer by serial_write. We need to
220 * lock it in case the copy_from_user blocks while swapping in a page,
221 * and some other program tries to do a serial write at the same time.
222 * Since the lock will only come under contention when the system is
223 * swapping and available memory is low, it makes sense to share one
224 * buffer across all the serial ports, since it significantly saves
225 * memory if large numbers of serial ports are open.
226 */
227 static unsigned char *tmp_buf;
228 static DECLARE_MUTEX(tmp_buf_sem);
229
serial_paranoia_check(struct su_struct * info,kdev_t device,const char * routine)230 static inline int serial_paranoia_check(struct su_struct *info,
231 kdev_t device, const char *routine)
232 {
233 #ifdef SERIAL_PARANOIA_CHECK
234 static const char *badmagic = KERN_WARNING
235 "Warning: bad magic number for serial struct (%s) in %s\n";
236 static const char *badinfo = KERN_WARNING
237 "Warning: null su_struct for (%s) in %s\n";
238
239 if (!info) {
240 printk(badinfo, kdevname(device), routine);
241 return 1;
242 }
243 if (info->magic != SERIAL_MAGIC) {
244 printk(badmagic, kdevname(device), routine);
245 return 1;
246 }
247 #endif
248 return 0;
249 }
250
251 static inline
su_inb(struct su_struct * info,unsigned long offset)252 unsigned int su_inb(struct su_struct *info, unsigned long offset)
253 {
254 return inb(info->port + offset);
255 }
256
257 static inline void
su_outb(struct su_struct * info,unsigned long offset,int value)258 su_outb(struct su_struct *info, unsigned long offset, int value)
259 {
260 #ifndef __sparc_v9__
261 /*
262 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
263 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
264 * gate outputs a logical one. Since we use level triggered interrupts
265 * we have lockup and watchdog reset. We cannot mask IRQ because
266 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
267 * This problem is similar to what Alpha people suffer, see serial.c.
268 */
269 if (offset == UART_MCR) value |= UART_MCR_OUT2;
270 #endif
271 outb(value, info->port + offset);
272 }
273
274 #define serial_in(info, off) su_inb(info, off)
275 #define serial_inp(info, off) su_inb(info, off)
276 #define serial_out(info, off, val) su_outb(info, off, val)
277 #define serial_outp(info, off, val) su_outb(info, off, val)
278
279 /*
280 * ------------------------------------------------------------
281 * su_stop() and su_start()
282 *
283 * This routines are called before setting or resetting tty->stopped.
284 * They enable or disable transmitter interrupts, as necessary.
285 * ------------------------------------------------------------
286 */
su_stop(struct tty_struct * tty)287 static void su_stop(struct tty_struct *tty)
288 {
289 struct su_struct *info = (struct su_struct *)tty->driver_data;
290 unsigned long flags;
291
292 if (serial_paranoia_check(info, tty->device, "su_stop"))
293 return;
294
295 save_flags(flags); cli();
296 if (info->IER & UART_IER_THRI) {
297 info->IER &= ~UART_IER_THRI;
298 serial_out(info, UART_IER, info->IER);
299 }
300 restore_flags(flags);
301 }
302
su_start(struct tty_struct * tty)303 static void su_start(struct tty_struct *tty)
304 {
305 struct su_struct *info = (struct su_struct *)tty->driver_data;
306 unsigned long flags;
307
308 if (serial_paranoia_check(info, tty->device, "su_start"))
309 return;
310
311 save_flags(flags); cli();
312 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
313 info->IER |= UART_IER_THRI;
314 serial_out(info, UART_IER, info->IER);
315 }
316 restore_flags(flags);
317 }
318
319 /*
320 * ----------------------------------------------------------------------
321 *
322 * Here starts the interrupt handling routines. All of the following
323 * subroutines are declared as inline and are folded into
324 * su_interrupt(). They were separated out for readability's sake.
325 *
326 * Note: rs_interrupt() is a "fast" interrupt, which means that it
327 * runs with interrupts turned off. People who may want to modify
328 * rs_interrupt() should try to keep the interrupt handler as fast as
329 * possible. After you are done making modifications, it is not a bad
330 * idea to do:
331 *
332 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
333 *
334 * and look at the resulting assemble code in serial.s.
335 *
336 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
337 * -----------------------------------------------------------------------
338 */
339
340 /*
341 * This routine is used by the interrupt handler to schedule
342 * processing in the software interrupt portion of the driver.
343 */
344 static void
su_sched_event(struct su_struct * info,int event)345 su_sched_event(struct su_struct *info, int event)
346 {
347 info->event |= 1 << event;
348 queue_task(&info->tqueue, &tq_serial);
349 mark_bh(SERIAL_BH);
350 }
351
352 static void
receive_kbd_ms_chars(struct su_struct * info,struct pt_regs * regs,int is_brk)353 receive_kbd_ms_chars(struct su_struct *info, struct pt_regs *regs, int is_brk)
354 {
355 unsigned char status = 0;
356 unsigned char ch;
357
358 do {
359 ch = serial_inp(info, UART_RX);
360 if (info->port_type == SU_PORT_KBD) {
361 if (ch == SUNKBD_RESET) {
362 l1a_state.kbd_id = 1;
363 l1a_state.l1_down = 0;
364 } else if (l1a_state.kbd_id) {
365 l1a_state.kbd_id = 0;
366 } else if (ch == SUNKBD_L1) {
367 l1a_state.l1_down = 1;
368 } else if (ch == (SUNKBD_L1|SUNKBD_UP)) {
369 l1a_state.l1_down = 0;
370 } else if (ch == SUNKBD_A && l1a_state.l1_down) {
371 /* whee... */
372 batten_down_hatches();
373 /* Continue execution... */
374 l1a_state.l1_down = 0;
375 l1a_state.kbd_id = 0;
376 return;
377 }
378 sunkbd_inchar(ch, regs);
379 } else {
380 sun_mouse_inbyte(ch, is_brk);
381 }
382
383 status = su_inb(info, UART_LSR);
384 } while (status & UART_LSR_DR);
385 }
386
387 static void
receive_serial_chars(struct su_struct * info,int * status,struct pt_regs * regs)388 receive_serial_chars(struct su_struct *info, int *status, struct pt_regs *regs)
389 {
390 struct tty_struct *tty = info->tty;
391 unsigned char ch;
392 int ignored = 0, saw_console_brk = 0;
393 struct async_icount *icount;
394
395 icount = &info->icount;
396 do {
397 ch = serial_inp(info, UART_RX);
398 if (info->is_console &&
399 (ch == 0 || (*status &UART_LSR_BI)))
400 saw_console_brk = 1;
401 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
402 break;
403 *tty->flip.char_buf_ptr = ch;
404 icount->rx++;
405
406 #ifdef SERIAL_DEBUG_INTR
407 printk("D%02x:%02x.", ch, *status);
408 #endif
409 *tty->flip.flag_buf_ptr = 0;
410 if (*status & (UART_LSR_BI | UART_LSR_PE |
411 UART_LSR_FE | UART_LSR_OE)) {
412 /*
413 * For statistics only
414 */
415 if (*status & UART_LSR_BI) {
416 *status &= ~(UART_LSR_FE | UART_LSR_PE);
417 icount->brk++;
418 } else if (*status & UART_LSR_PE)
419 icount->parity++;
420 else if (*status & UART_LSR_FE)
421 icount->frame++;
422 if (*status & UART_LSR_OE)
423 icount->overrun++;
424
425 /*
426 * Now check to see if character should be
427 * ignored, and mask off conditions which
428 * should be ignored.
429 */
430 if (*status & info->ignore_status_mask) {
431 if (++ignored > 100) {
432 #ifdef SERIAL_DEBUG_INTR
433 printk("ign100..");
434 #endif
435 break;
436 }
437 goto ignore_char;
438 }
439 *status &= info->read_status_mask;
440
441 if (*status & (UART_LSR_BI)) {
442 #ifdef SERIAL_DEBUG_INTR
443 printk("handling break....");
444 #endif
445 *tty->flip.flag_buf_ptr = TTY_BREAK;
446 if (info->flags & ASYNC_SAK)
447 do_SAK(tty);
448 } else if (*status & UART_LSR_PE)
449 *tty->flip.flag_buf_ptr = TTY_PARITY;
450 else if (*status & UART_LSR_FE)
451 *tty->flip.flag_buf_ptr = TTY_FRAME;
452 if (*status & UART_LSR_OE) {
453 /*
454 * Overrun is special, since it's
455 * reported immediately, and doesn't
456 * affect the current character
457 */
458 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
459 tty->flip.count++;
460 tty->flip.flag_buf_ptr++;
461 tty->flip.char_buf_ptr++;
462 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
463 }
464 }
465 }
466 tty->flip.flag_buf_ptr++;
467 tty->flip.char_buf_ptr++;
468 tty->flip.count++;
469 ignore_char:
470 *status = serial_inp(info, UART_LSR);
471 } while (*status & UART_LSR_DR);
472 #ifdef SERIAL_DEBUG_INTR
473 printk("E%02x.R%d", *status, tty->flip.count);
474 #endif
475 tty_flip_buffer_push(tty);
476 if (saw_console_brk != 0)
477 batten_down_hatches();
478 }
479
480 static void
transmit_chars(struct su_struct * info,int * intr_done)481 transmit_chars(struct su_struct *info, int *intr_done)
482 {
483 int count;
484
485 if (info->x_char) {
486 serial_outp(info, UART_TX, info->x_char);
487 info->icount.tx++;
488 info->x_char = 0;
489 if (intr_done)
490 *intr_done = 0;
491 return;
492 }
493 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
494 info->tty->hw_stopped) {
495 info->IER &= ~UART_IER_THRI;
496 serial_out(info, UART_IER, info->IER);
497 return;
498 }
499
500 count = info->xmit_fifo_size;
501 do {
502 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
503 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
504 info->icount.tx++;
505 if (--info->xmit_cnt <= 0)
506 break;
507 } while (--count > 0);
508
509 if (info->xmit_cnt < WAKEUP_CHARS)
510 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
511
512 #ifdef SERIAL_DEBUG_INTR
513 printk("T%d...", info->xmit_cnt);
514 #endif
515 if (intr_done)
516 *intr_done = 0;
517
518 if (info->xmit_cnt <= 0) {
519 info->IER &= ~UART_IER_THRI;
520 serial_out(info, UART_IER, info->IER);
521 }
522 }
523
524 static void
check_modem_status(struct su_struct * info)525 check_modem_status(struct su_struct *info)
526 {
527 int status;
528 struct async_icount *icount;
529
530 status = serial_in(info, UART_MSR);
531
532 if (status & UART_MSR_ANY_DELTA) {
533 icount = &info->icount;
534 /* update input line counters */
535 if (status & UART_MSR_TERI)
536 icount->rng++;
537 if (status & UART_MSR_DDSR)
538 icount->dsr++;
539 if (status & UART_MSR_DDCD) {
540 icount->dcd++;
541 #ifdef CONFIG_HARD_PPS
542 if ((info->flags & ASYNC_HARDPPS_CD) &&
543 (status & UART_MSR_DCD))
544 hardpps();
545 #endif
546 }
547 if (status & UART_MSR_DCTS)
548 icount->cts++;
549 wake_up_interruptible(&info->delta_msr_wait);
550 }
551
552 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
553 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
554 printk("ttys%d CD now %s...", info->line,
555 (status & UART_MSR_DCD) ? "on" : "off");
556 #endif
557 if (status & UART_MSR_DCD)
558 wake_up_interruptible(&info->open_wait);
559 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
560 (info->flags & ASYNC_CALLOUT_NOHUP))) {
561 #ifdef SERIAL_DEBUG_OPEN
562 printk("doing serial hangup...");
563 #endif
564 if (info->tty)
565 tty_hangup(info->tty);
566 }
567 }
568 if (info->flags & ASYNC_CTS_FLOW) {
569 if (info->tty->hw_stopped) {
570 if (status & UART_MSR_CTS) {
571 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
572 printk("CTS tx start...");
573 #endif
574 info->tty->hw_stopped = 0;
575 info->IER |= UART_IER_THRI;
576 serial_out(info, UART_IER, info->IER);
577 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
578 return;
579 }
580 } else {
581 if (!(status & UART_MSR_CTS)) {
582 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
583 printk("CTS tx stop...");
584 #endif
585 info->tty->hw_stopped = 1;
586 info->IER &= ~UART_IER_THRI;
587 serial_out(info, UART_IER, info->IER);
588 }
589 }
590 }
591 }
592
593 /*
594 * This is the kbd/mouse serial driver's interrupt routine
595 */
596 static void
su_kbd_ms_interrupt(int irq,void * dev_id,struct pt_regs * regs)597 su_kbd_ms_interrupt(int irq, void *dev_id, struct pt_regs * regs)
598 {
599 struct su_struct *info = (struct su_struct *)dev_id;
600 unsigned char status;
601
602 #ifdef SERIAL_DEBUG_INTR
603 printk("su_kbd_ms_interrupt(%s)...", __irq_itoa(irq));
604 #endif
605 if (!info)
606 return;
607
608 if (serial_in(info, UART_IIR) & UART_IIR_NO_INT)
609 return;
610
611 status = serial_inp(info, UART_LSR);
612 #ifdef SERIAL_DEBUG_INTR
613 printk("status = %x...", status);
614 #endif
615 if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
616 receive_kbd_ms_chars(info, regs,
617 (status & UART_LSR_BI) != 0);
618
619 #ifdef SERIAL_DEBUG_INTR
620 printk("end.\n");
621 #endif
622 }
623
624 /*
625 * This is the serial driver's generic interrupt routine
626 */
627 static void
su_serial_interrupt(int irq,void * dev_id,struct pt_regs * regs)628 su_serial_interrupt(int irq, void *dev_id, struct pt_regs * regs)
629 {
630 int status;
631 struct su_struct *info;
632 int pass_counter = 0;
633
634 #ifdef SERIAL_DEBUG_INTR
635 printk("su_serial_interrupt(%s)...", __irq_itoa(irq));
636 #endif
637 info = (struct su_struct *)dev_id;
638 if (!info || !info->tty) {
639 #ifdef SERIAL_DEBUG_INTR
640 printk("strain\n");
641 #endif
642 return;
643 }
644
645 do {
646 status = serial_inp(info, UART_LSR);
647 #ifdef SERIAL_DEBUG_INTR
648 printk("status = %x...", status);
649 #endif
650 if (status & UART_LSR_DR)
651 receive_serial_chars(info, &status, regs);
652 check_modem_status(info);
653 if (status & UART_LSR_THRE)
654 transmit_chars(info, 0);
655
656 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
657 #ifdef SERIAL_DEBUG_INTR
658 printk("rs loop break");
659 #endif
660 break; /* Prevent infinite loops */
661 }
662 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
663
664 info->last_active = jiffies;
665
666 #ifdef SERIAL_DEBUG_INTR
667 printk("end.\n");
668 #endif
669 }
670
671 /*
672 * -------------------------------------------------------------------
673 * Here ends the serial interrupt routines.
674 * -------------------------------------------------------------------
675 */
676
677 /*
678 * This routine is used to handle the "bottom half" processing for the
679 * serial driver, known also the "software interrupt" processing.
680 * This processing is done at the kernel interrupt level, after the
681 * su_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
682 * is where time-consuming activities which can not be done in the
683 * interrupt driver proper are done; the interrupt driver schedules
684 * them using su_sched_event(), and they get done here.
685 */
do_serial_bh(void)686 static void do_serial_bh(void)
687 {
688 run_task_queue(&tq_serial);
689 }
690
do_softint(void * private_)691 static void do_softint(void *private_)
692 {
693 struct su_struct *info = (struct su_struct *) private_;
694 struct tty_struct *tty;
695
696 tty = info->tty;
697 if (!tty)
698 return;
699
700 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
701 tty_wakeup(tty);
702 }
703 }
704
705 /*
706 * ---------------------------------------------------------------
707 * Low level utility subroutines for the serial driver: routines to
708 * figure out the appropriate timeout for an interrupt chain, routines
709 * to initialize and startup a serial port, and routines to shutdown a
710 * serial port. Useful stuff like that.
711 * ---------------------------------------------------------------
712 */
713
714 static int
startup(struct su_struct * info)715 startup(struct su_struct *info)
716 {
717 unsigned long flags;
718 int retval=0;
719 unsigned long page;
720
721 save_flags(flags);
722 if (info->tty) {
723 page = get_free_page(GFP_KERNEL);
724 if (!page)
725 return -ENOMEM;
726
727 cli();
728
729 if (info->flags & ASYNC_INITIALIZED) {
730 free_page(page);
731 goto errout;
732 }
733
734 if (info->port == 0 || info->type == PORT_UNKNOWN) {
735 set_bit(TTY_IO_ERROR, &info->tty->flags);
736 free_page(page);
737 goto errout;
738 }
739 if (info->xmit_buf)
740 free_page(page);
741 else
742 info->xmit_buf = (unsigned char *) page;
743 }
744 cli();
745
746 #ifdef SERIAL_DEBUG_OPEN
747 printk("starting up ttys%d (irq %s)...", info->line,
748 __irq_itoa(info->irq));
749 #endif
750
751 if (uart_config[info->type].flags & UART_STARTECH) {
752 /* Wake up UART */
753 serial_outp(info, UART_LCR, 0xBF);
754 serial_outp(info, UART_EFR, UART_EFR_ECB);
755 serial_outp(info, UART_IER, 0);
756 serial_outp(info, UART_EFR, 0);
757 serial_outp(info, UART_LCR, 0);
758 }
759
760 if (info->type == PORT_16750) {
761 /* Wake up UART */
762 serial_outp(info, UART_IER, 0);
763 }
764
765 /*
766 * Clear the FIFO buffers and disable them
767 * (they will be reenabled in change_speed())
768 */
769 if (uart_config[info->type].flags & UART_CLEAR_FIFO)
770 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
771 UART_FCR_CLEAR_XMIT));
772
773 /*
774 * At this point there's no way the LSR could still be 0xFF;
775 * if it is, then bail out, because there's likely no UART
776 * here.
777 */
778 if (serial_inp(info, UART_LSR) == 0xff) {
779 if (capable(CAP_SYS_ADMIN)) {
780 if (info->tty)
781 set_bit(TTY_IO_ERROR, &info->tty->flags);
782 } else
783 retval = -ENODEV;
784 goto errout;
785 }
786
787 /*
788 * Allocate the IRQ if necessary
789 */
790 if (info->port_type != SU_PORT_PORT) {
791 retval = request_irq(info->irq, su_kbd_ms_interrupt,
792 SA_SHIRQ, info->name, info);
793 } else {
794 retval = request_irq(info->irq, su_serial_interrupt,
795 SA_SHIRQ, info->name, info);
796 }
797 if (retval) {
798 if (capable(CAP_SYS_ADMIN)) {
799 if (info->tty)
800 set_bit(TTY_IO_ERROR, &info->tty->flags);
801 retval = 0;
802 }
803 goto errout;
804 }
805
806 /*
807 * Clear the interrupt registers.
808 */
809 (void) serial_inp(info, UART_RX);
810 (void) serial_inp(info, UART_IIR);
811 (void) serial_inp(info, UART_MSR);
812
813 /*
814 * Now, initialize the UART
815 */
816 serial_outp(info, UART_LCR, UART_LCR_WLEN8); /* reset DLAB */
817
818 info->MCR = 0;
819 if (info->tty && info->tty->termios->c_cflag & CBAUD)
820 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
821 if (info->irq != 0)
822 info->MCR |= UART_MCR_OUT2;
823 serial_outp(info, UART_MCR, info->MCR);
824
825 /*
826 * Finally, enable interrupts
827 */
828 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
829 serial_outp(info, UART_IER, info->IER); /* enable interrupts */
830
831 /*
832 * And clear the interrupt registers again for luck.
833 */
834 (void)serial_inp(info, UART_LSR);
835 (void)serial_inp(info, UART_RX);
836 (void)serial_inp(info, UART_IIR);
837 (void)serial_inp(info, UART_MSR);
838
839 if (info->tty)
840 clear_bit(TTY_IO_ERROR, &info->tty->flags);
841 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
842
843 /*
844 * Set up the tty->alt_speed kludge
845 */
846 if (info->tty) {
847 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
848 info->tty->alt_speed = 57600;
849 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
850 info->tty->alt_speed = 115200;
851 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
852 info->tty->alt_speed = 230400;
853 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
854 info->tty->alt_speed = 460800;
855 }
856
857 /*
858 * and set the speed of the serial port
859 */
860 change_speed(info, 0);
861
862 info->flags |= ASYNC_INITIALIZED;
863 restore_flags(flags);
864 return 0;
865
866 errout:
867 restore_flags(flags);
868 return retval;
869 }
870
871 /*
872 * This routine will shutdown a serial port; interrupts are disabled, and
873 * DTR is dropped if the hangup on close termio flag is on.
874 */
875 static void
shutdown(struct su_struct * info)876 shutdown(struct su_struct *info)
877 {
878 unsigned long flags;
879
880 if (!(info->flags & ASYNC_INITIALIZED))
881 return;
882
883 save_flags(flags); cli(); /* Disable interrupts */
884
885 /*
886 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
887 * here so the queue might never be waken up
888 */
889 wake_up_interruptible(&info->delta_msr_wait);
890
891 /*
892 * Free the IRQ, if necessary
893 */
894 free_irq(info->irq, info);
895
896 if (info->xmit_buf) {
897 free_page((unsigned long) info->xmit_buf);
898 info->xmit_buf = 0;
899 }
900
901 info->IER = 0;
902 serial_outp(info, UART_IER, 0x00); /* disable all intrs */
903 info->MCR &= ~UART_MCR_OUT2;
904
905 /* disable break condition */
906 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
907
908 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
909 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
910 serial_outp(info, UART_MCR, info->MCR);
911
912 /* disable FIFO's */
913 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
914 UART_FCR_CLEAR_XMIT));
915 (void)serial_in(info, UART_RX); /* read data port to reset things */
916
917 if (info->tty)
918 set_bit(TTY_IO_ERROR, &info->tty->flags);
919
920 if (uart_config[info->type].flags & UART_STARTECH) {
921 /* Arrange to enter sleep mode */
922 serial_outp(info, UART_LCR, 0xBF);
923 serial_outp(info, UART_EFR, UART_EFR_ECB);
924 serial_outp(info, UART_IER, UART_IERX_SLEEP);
925 serial_outp(info, UART_LCR, 0);
926 }
927 if (info->type == PORT_16750) {
928 /* Arrange to enter sleep mode */
929 serial_outp(info, UART_IER, UART_IERX_SLEEP);
930 }
931 info->flags &= ~ASYNC_INITIALIZED;
932 restore_flags(flags);
933 }
934
935 static int
su_get_baud_rate(struct su_struct * info)936 su_get_baud_rate(struct su_struct *info)
937 {
938 static struct tty_struct c_tty;
939 static struct termios c_termios;
940
941 if (info->tty)
942 return tty_get_baud_rate(info->tty);
943
944 memset(&c_tty, 0, sizeof(c_tty));
945 memset(&c_termios, 0, sizeof(c_termios));
946 c_tty.termios = &c_termios;
947 c_termios.c_cflag = info->cflag;
948
949 return tty_get_baud_rate(&c_tty);
950 }
951
952 /*
953 * This routine is called to set the UART divisor registers to match
954 * the specified baud rate for a serial port.
955 */
956 static void
change_speed(struct su_struct * info,struct termios * old_termios)957 change_speed(struct su_struct *info,
958 struct termios *old_termios)
959 {
960 int quot = 0, baud;
961 unsigned int cval, fcr = 0;
962 int bits;
963 unsigned long flags;
964
965 if (info->port_type == SU_PORT_PORT) {
966 if (!info->tty || !info->tty->termios)
967 return;
968 if (!info->port)
969 return;
970 info->cflag = info->tty->termios->c_cflag;
971 }
972
973 /* byte size and parity */
974 switch (info->cflag & CSIZE) {
975 case CS5: cval = 0x00; bits = 7; break;
976 case CS6: cval = 0x01; bits = 8; break;
977 case CS7: cval = 0x02; bits = 9; break;
978 case CS8: cval = 0x03; bits = 10; break;
979 /* Never happens, but GCC is too dumb to figure it out */
980 default: cval = 0x00; bits = 7; break;
981 }
982 if (info->cflag & CSTOPB) {
983 cval |= 0x04;
984 bits++;
985 }
986 if (info->cflag & PARENB) {
987 cval |= UART_LCR_PARITY;
988 bits++;
989 }
990 if (!(info->cflag & PARODD))
991 cval |= UART_LCR_EPAR;
992 #ifdef CMSPAR
993 if (info->cflag & CMSPAR)
994 cval |= UART_LCR_SPAR;
995 #endif
996
997 /* Determine divisor based on baud rate */
998 baud = su_get_baud_rate(info);
999 if (baud == 38400 &&
1000 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1001 quot = info->custom_divisor;
1002 else {
1003 if (baud == 134)
1004 /* Special case since 134 is really 134.5 */
1005 quot = (2 * info->baud_base / 269);
1006 else if (baud)
1007 quot = info->baud_base / baud;
1008 }
1009 /* If the quotient is zero refuse the change */
1010 if (!quot && old_termios) {
1011 info->tty->termios->c_cflag &= ~CBAUD;
1012 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1013 baud = tty_get_baud_rate(info->tty);
1014 if (!baud)
1015 baud = 9600;
1016 if (baud == 38400 &&
1017 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1018 quot = info->custom_divisor;
1019 else {
1020 if (baud == 134)
1021 /* Special case since 134 is really 134.5 */
1022 quot = (2*info->baud_base / 269);
1023 else if (baud)
1024 quot = info->baud_base / baud;
1025 }
1026 }
1027 /* As a last resort, if the quotient is zero, default to 9600 bps */
1028 if (!quot)
1029 quot = info->baud_base / 9600;
1030 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / info->baud_base);
1031 info->timeout += HZ/50; /* Add .02 seconds of slop */
1032
1033 /* Set up FIFO's */
1034 if (uart_config[info->type].flags & UART_USE_FIFO) {
1035 if ((info->baud_base / quot) < 9600)
1036 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1037 else
1038 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1039 }
1040 if (info->type == PORT_16750)
1041 fcr |= UART_FCR7_64BYTE;
1042
1043 /* CTS flow control flag and modem status interrupts */
1044 info->IER &= ~UART_IER_MSI;
1045 if (info->flags & ASYNC_HARDPPS_CD)
1046 info->IER |= UART_IER_MSI;
1047 if (info->cflag & CRTSCTS) {
1048 info->flags |= ASYNC_CTS_FLOW;
1049 info->IER |= UART_IER_MSI;
1050 } else
1051 info->flags &= ~ASYNC_CTS_FLOW;
1052 if (info->cflag & CLOCAL)
1053 info->flags &= ~ASYNC_CHECK_CD;
1054 else {
1055 info->flags |= ASYNC_CHECK_CD;
1056 info->IER |= UART_IER_MSI;
1057 }
1058 serial_out(info, UART_IER, info->IER);
1059
1060 /*
1061 * Set up parity check flag
1062 */
1063 if (info->tty) {
1064 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1065
1066 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE |
1067 UART_LSR_DR;
1068 if (I_INPCK(info->tty))
1069 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1070 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1071 info->read_status_mask |= UART_LSR_BI;
1072
1073 /*
1074 * Characters to ignore
1075 */
1076 info->ignore_status_mask = 0;
1077 if (I_IGNPAR(info->tty))
1078 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1079 if (I_IGNBRK(info->tty)) {
1080 info->ignore_status_mask |= UART_LSR_BI;
1081 /*
1082 * If we're ignore parity and break indicators, ignore
1083 * overruns too. (For real raw support).
1084 */
1085 if (I_IGNPAR(info->tty))
1086 info->ignore_status_mask |= UART_LSR_OE;
1087 }
1088 /*
1089 * !!! ignore all characters if CREAD is not set
1090 */
1091 if ((info->cflag & CREAD) == 0)
1092 info->ignore_status_mask |= UART_LSR_DR;
1093 }
1094
1095 save_flags(flags); cli();
1096 if (uart_config[info->type].flags & UART_STARTECH) {
1097 serial_outp(info, UART_LCR, 0xBF);
1098 serial_outp(info, UART_EFR,
1099 (info->cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1100 }
1101 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1102 serial_outp(info, UART_DLL, quot & 0xff); /* LS of divisor */
1103 serial_outp(info, UART_DLM, quot >> 8); /* MS of divisor */
1104 if (info->type == PORT_16750)
1105 serial_outp(info, UART_FCR, fcr); /* set fcr */
1106 serial_outp(info, UART_LCR, cval); /* reset DLAB */
1107 if (info->type != PORT_16750)
1108 serial_outp(info, UART_FCR, fcr); /* set fcr */
1109 restore_flags(flags);
1110 info->quot = quot;
1111 }
1112
1113 static void
su_put_char(struct tty_struct * tty,unsigned char ch)1114 su_put_char(struct tty_struct *tty, unsigned char ch)
1115 {
1116 struct su_struct *info = (struct su_struct *)tty->driver_data;
1117 unsigned long flags;
1118
1119 if (serial_paranoia_check(info, tty->device, "su_put_char"))
1120 return;
1121
1122 if (!tty || !info->xmit_buf)
1123 return;
1124
1125 save_flags(flags); cli();
1126 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1127 restore_flags(flags);
1128 return;
1129 }
1130
1131 info->xmit_buf[info->xmit_head++] = ch;
1132 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1133 info->xmit_cnt++;
1134 restore_flags(flags);
1135 }
1136
su_put_char_kbd(unsigned char c)1137 static void su_put_char_kbd(unsigned char c)
1138 {
1139 struct su_struct *info = su_table;
1140 int lsr;
1141
1142 if (info->port_type != SU_PORT_KBD)
1143 ++info;
1144 if (info->port_type != SU_PORT_KBD)
1145 return;
1146
1147 do {
1148 lsr = serial_in(info, UART_LSR);
1149 } while (!(lsr & UART_LSR_THRE));
1150
1151 /* Send the character out. */
1152 su_outb(info, UART_TX, c);
1153 }
1154
1155 static void
su_change_mouse_baud(int baud)1156 su_change_mouse_baud(int baud)
1157 {
1158 struct su_struct *info = su_table;
1159
1160 if (info->port_type != SU_PORT_MS)
1161 ++info;
1162 if (info->port_type != SU_PORT_MS)
1163 return;
1164
1165 info->cflag &= ~CBAUD;
1166 switch (baud) {
1167 case 1200:
1168 info->cflag |= B1200;
1169 break;
1170 case 2400:
1171 info->cflag |= B2400;
1172 break;
1173 case 4800:
1174 info->cflag |= B4800;
1175 break;
1176 case 9600:
1177 info->cflag |= B9600;
1178 break;
1179 default:
1180 printk("su_change_mouse_baud: unknown baud rate %d, "
1181 "defaulting to 1200\n", baud);
1182 info->cflag |= 1200;
1183 break;
1184 }
1185 change_speed(info, 0);
1186 }
1187
1188 static void
su_flush_chars(struct tty_struct * tty)1189 su_flush_chars(struct tty_struct *tty)
1190 {
1191 struct su_struct *info = (struct su_struct *)tty->driver_data;
1192 unsigned long flags;
1193
1194 if (serial_paranoia_check(info, tty->device, "su_flush_chars"))
1195 return;
1196
1197 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1198 !info->xmit_buf)
1199 return;
1200
1201 save_flags(flags); cli();
1202 info->IER |= UART_IER_THRI;
1203 serial_out(info, UART_IER, info->IER);
1204 restore_flags(flags);
1205 }
1206
1207 static int
su_write(struct tty_struct * tty,int from_user,const unsigned char * buf,int count)1208 su_write(struct tty_struct * tty, int from_user,
1209 const unsigned char *buf, int count)
1210 {
1211 int c, ret = 0;
1212 struct su_struct *info = (struct su_struct *)tty->driver_data;
1213 unsigned long flags;
1214
1215 if (serial_paranoia_check(info, tty->device, "su_write"))
1216 return 0;
1217
1218 if (!tty || !info->xmit_buf || !tmp_buf)
1219 return 0;
1220
1221 save_flags(flags);
1222 if (from_user) {
1223 down(&tmp_buf_sem);
1224 while (1) {
1225 c = MIN(count,
1226 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1227 SERIAL_XMIT_SIZE - info->xmit_head));
1228 if (c <= 0)
1229 break;
1230
1231 c -= copy_from_user(tmp_buf, buf, c);
1232 if (!c) {
1233 if (!ret)
1234 ret = -EFAULT;
1235 break;
1236 }
1237 cli();
1238 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1239 SERIAL_XMIT_SIZE - info->xmit_head));
1240 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1241 info->xmit_head = ((info->xmit_head + c) &
1242 (SERIAL_XMIT_SIZE-1));
1243 info->xmit_cnt += c;
1244 restore_flags(flags);
1245 buf += c;
1246 count -= c;
1247 ret += c;
1248 }
1249 up(&tmp_buf_sem);
1250 } else {
1251 while (1) {
1252 cli();
1253 c = MIN(count,
1254 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1255 SERIAL_XMIT_SIZE - info->xmit_head));
1256 if (c <= 0) {
1257 restore_flags(flags);
1258 break;
1259 }
1260 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1261 info->xmit_head = ((info->xmit_head + c) &
1262 (SERIAL_XMIT_SIZE-1));
1263 info->xmit_cnt += c;
1264 restore_flags(flags);
1265 buf += c;
1266 count -= c;
1267 ret += c;
1268 }
1269 }
1270 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1271 !(info->IER & UART_IER_THRI)) {
1272 info->IER |= UART_IER_THRI;
1273 serial_out(info, UART_IER, info->IER);
1274 }
1275 return ret;
1276 }
1277
1278 static int
su_write_room(struct tty_struct * tty)1279 su_write_room(struct tty_struct *tty)
1280 {
1281 struct su_struct *info = (struct su_struct *)tty->driver_data;
1282 int ret;
1283
1284 if (serial_paranoia_check(info, tty->device, "su_write_room"))
1285 return 0;
1286 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1287 if (ret < 0)
1288 ret = 0;
1289 return ret;
1290 }
1291
1292 static int
su_chars_in_buffer(struct tty_struct * tty)1293 su_chars_in_buffer(struct tty_struct *tty)
1294 {
1295 struct su_struct *info = (struct su_struct *)tty->driver_data;
1296
1297 if (serial_paranoia_check(info, tty->device, "su_chars_in_buffer"))
1298 return 0;
1299 return info->xmit_cnt;
1300 }
1301
1302 static void
su_flush_buffer(struct tty_struct * tty)1303 su_flush_buffer(struct tty_struct *tty)
1304 {
1305 struct su_struct *info = (struct su_struct *)tty->driver_data;
1306 unsigned long flags;
1307
1308 if (serial_paranoia_check(info, tty->device, "su_flush_buffer"))
1309 return;
1310 save_flags(flags); cli();
1311 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1312 restore_flags(flags);
1313 tty_wakeup(tty);
1314 }
1315
1316 /*
1317 * This function is used to send a high-priority XON/XOFF character to
1318 * the device
1319 */
1320 static void
su_send_xchar(struct tty_struct * tty,char ch)1321 su_send_xchar(struct tty_struct *tty, char ch)
1322 {
1323 struct su_struct *info = (struct su_struct *)tty->driver_data;
1324
1325 if (serial_paranoia_check(info, tty->device, "su_send_char"))
1326 return;
1327
1328 if (!(info->flags & ASYNC_INITIALIZED))
1329 return;
1330
1331 info->x_char = ch;
1332 if (ch) {
1333 /* Make sure transmit interrupts are on */
1334 info->IER |= UART_IER_THRI;
1335 serial_out(info, UART_IER, info->IER);
1336 }
1337 }
1338
1339 /*
1340 * ------------------------------------------------------------
1341 * su_throttle()
1342 *
1343 * This routine is called by the upper-layer tty layer to signal that
1344 * incoming characters should be throttled.
1345 * ------------------------------------------------------------
1346 */
1347 static void
su_throttle(struct tty_struct * tty)1348 su_throttle(struct tty_struct * tty)
1349 {
1350 struct su_struct *info = (struct su_struct *)tty->driver_data;
1351 unsigned long flags;
1352 #ifdef SERIAL_DEBUG_THROTTLE
1353 char buf[64];
1354
1355 printk("throttle %s: %d....\n", tty_name(tty, buf),
1356 tty->ldisc.chars_in_buffer(tty));
1357 #endif
1358
1359 if (serial_paranoia_check(info, tty->device, "su_throttle"))
1360 return;
1361
1362 if (I_IXOFF(tty))
1363 su_send_xchar(tty, STOP_CHAR(tty));
1364
1365 if (tty->termios->c_cflag & CRTSCTS)
1366 info->MCR &= ~UART_MCR_RTS;
1367
1368 save_flags(flags); cli();
1369 serial_out(info, UART_MCR, info->MCR);
1370 restore_flags(flags);
1371 }
1372
1373 static void
su_unthrottle(struct tty_struct * tty)1374 su_unthrottle(struct tty_struct * tty)
1375 {
1376 struct su_struct *info = (struct su_struct *)tty->driver_data;
1377 unsigned long flags;
1378 #ifdef SERIAL_DEBUG_THROTTLE
1379 char buf[64];
1380
1381 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1382 tty->ldisc.chars_in_buffer(tty));
1383 #endif
1384
1385 if (serial_paranoia_check(info, tty->device, "su_unthrottle"))
1386 return;
1387
1388 if (I_IXOFF(tty)) {
1389 if (info->x_char)
1390 info->x_char = 0;
1391 else
1392 su_send_xchar(tty, START_CHAR(tty));
1393 }
1394 if (tty->termios->c_cflag & CRTSCTS)
1395 info->MCR |= UART_MCR_RTS;
1396 save_flags(flags); cli();
1397 serial_out(info, UART_MCR, info->MCR);
1398 restore_flags(flags);
1399 }
1400
1401 /*
1402 * ------------------------------------------------------------
1403 * su_ioctl() and friends
1404 * ------------------------------------------------------------
1405 */
1406
1407 /*
1408 * get_serial_info - handle TIOCGSERIAL ioctl()
1409 *
1410 * Purpose: Return standard serial struct information about
1411 * a serial port handled by this driver.
1412 *
1413 * Added: 11-May-2001 Lars Kellogg-Stedman <lars@larsshack.org>
1414 */
get_serial_info(struct su_struct * info,struct serial_struct * retinfo)1415 static int get_serial_info(struct su_struct * info,
1416 struct serial_struct * retinfo)
1417 {
1418 struct serial_struct tmp;
1419
1420 if (!retinfo)
1421 return -EFAULT;
1422 memset(&tmp, 0, sizeof(tmp));
1423
1424 tmp.type = info->type;
1425 tmp.line = info->line;
1426 tmp.port = info->port;
1427 tmp.irq = info->irq;
1428 tmp.flags = info->flags;
1429 tmp.xmit_fifo_size = info->xmit_fifo_size;
1430 tmp.baud_base = info->baud_base;
1431 tmp.close_delay = info->close_delay;
1432 tmp.closing_wait = info->closing_wait;
1433 tmp.custom_divisor = info->custom_divisor;
1434 tmp.hub6 = 0;
1435
1436 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1437 return -EFAULT;
1438
1439 return 0;
1440 }
1441
1442 /*
1443 * get_lsr_info - get line status register info
1444 *
1445 * Purpose: Let user call ioctl() to get info when the UART physically
1446 * is emptied. On bus types like RS485, the transmitter must
1447 * release the bus after transmitting. This must be done when
1448 * the transmit shift register is empty, not be done when the
1449 * transmit holding register is empty. This functionality
1450 * allows an RS485 driver to be written in user space.
1451 */
1452 static int
get_lsr_info(struct su_struct * info,unsigned int * value)1453 get_lsr_info(struct su_struct * info, unsigned int *value)
1454 {
1455 unsigned char status;
1456 unsigned int result;
1457 unsigned long flags;
1458
1459 save_flags(flags); cli();
1460 status = serial_in(info, UART_LSR);
1461 restore_flags(flags);
1462 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1463 return put_user(result,value);
1464 }
1465
1466
1467 static int
get_modem_info(struct su_struct * info,unsigned int * value)1468 get_modem_info(struct su_struct * info, unsigned int *value)
1469 {
1470 unsigned char control, status;
1471 unsigned int result;
1472 unsigned long flags;
1473
1474 control = info->MCR;
1475 save_flags(flags); cli();
1476 status = serial_in(info, UART_MSR);
1477 restore_flags(flags);
1478 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1479 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1480 #ifdef TIOCM_OUT1
1481 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1482 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1483 #endif
1484 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1485 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1486 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1487 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1488 return put_user(result,value);
1489 }
1490
1491 static int
set_modem_info(struct su_struct * info,unsigned int cmd,unsigned int * value)1492 set_modem_info(struct su_struct * info, unsigned int cmd, unsigned int *value)
1493 {
1494 unsigned int arg;
1495 unsigned long flags;
1496
1497 if (get_user(arg, value))
1498 return -EFAULT;
1499 switch (cmd) {
1500 case TIOCMBIS:
1501 if (arg & TIOCM_RTS)
1502 info->MCR |= UART_MCR_RTS;
1503 if (arg & TIOCM_DTR)
1504 info->MCR |= UART_MCR_DTR;
1505 #ifdef TIOCM_OUT1
1506 if (arg & TIOCM_OUT1)
1507 info->MCR |= UART_MCR_OUT1;
1508 if (arg & TIOCM_OUT2)
1509 info->MCR |= UART_MCR_OUT2;
1510 #endif
1511 break;
1512 case TIOCMBIC:
1513 if (arg & TIOCM_RTS)
1514 info->MCR &= ~UART_MCR_RTS;
1515 if (arg & TIOCM_DTR)
1516 info->MCR &= ~UART_MCR_DTR;
1517 #ifdef TIOCM_OUT1
1518 if (arg & TIOCM_OUT1)
1519 info->MCR &= ~UART_MCR_OUT1;
1520 if (arg & TIOCM_OUT2)
1521 info->MCR &= ~UART_MCR_OUT2;
1522 #endif
1523 break;
1524 case TIOCMSET:
1525 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1526 #ifdef TIOCM_OUT1
1527 UART_MCR_OUT1 |
1528 UART_MCR_OUT2 |
1529 #endif
1530 UART_MCR_DTR))
1531 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1532 #ifdef TIOCM_OUT1
1533 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1534 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1535 #endif
1536 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1537 break;
1538 default:
1539 return -EINVAL;
1540 }
1541 save_flags(flags); cli();
1542 serial_out(info, UART_MCR, info->MCR);
1543 restore_flags(flags);
1544 return 0;
1545 }
1546
1547 /*
1548 * su_break() --- routine which turns the break handling on or off
1549 */
1550 static void
su_break(struct tty_struct * tty,int break_state)1551 su_break(struct tty_struct *tty, int break_state)
1552 {
1553 struct su_struct * info = (struct su_struct *)tty->driver_data;
1554 unsigned long flags;
1555
1556 if (serial_paranoia_check(info, tty->device, "su_break"))
1557 return;
1558
1559 if (!info->port)
1560 return;
1561 save_flags(flags); cli();
1562 if (break_state == -1)
1563 serial_out(info, UART_LCR,
1564 serial_inp(info, UART_LCR) | UART_LCR_SBC);
1565 else
1566 serial_out(info, UART_LCR,
1567 serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1568 restore_flags(flags);
1569 }
1570
1571 static int
su_ioctl(struct tty_struct * tty,struct file * file,unsigned int cmd,unsigned long arg)1572 su_ioctl(struct tty_struct *tty, struct file * file,
1573 unsigned int cmd, unsigned long arg)
1574 {
1575 struct su_struct * info = (struct su_struct *)tty->driver_data;
1576 struct async_icount cprev, cnow; /* kernel counter temps */
1577 struct serial_icounter_struct *p_cuser; /* user space */
1578
1579 if (serial_paranoia_check(info, tty->device, "su_ioctl"))
1580 return -ENODEV;
1581
1582 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1583 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1584 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1585 if (tty->flags & (1 << TTY_IO_ERROR))
1586 return -EIO;
1587 }
1588
1589 switch (cmd) {
1590 case TIOCMGET:
1591 return get_modem_info(info, (unsigned int *) arg);
1592 case TIOCMBIS:
1593 case TIOCMBIC:
1594 case TIOCMSET:
1595 return set_modem_info(info, cmd, (unsigned int *) arg);
1596
1597 case TIOCGSERIAL:
1598 return get_serial_info(info, (struct serial_struct *)arg);
1599
1600 case TIOCSERGETLSR: /* Get line status register */
1601 return get_lsr_info(info, (unsigned int *) arg);
1602
1603 #if 0
1604 case TIOCSERGSTRUCT:
1605 if (copy_to_user((struct async_struct *) arg,
1606 info, sizeof(struct async_struct)))
1607 return -EFAULT;
1608 return 0;
1609 #endif
1610
1611 /*
1612 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1613 * - mask passed in arg for lines of interest
1614 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1615 * Caller should use TIOCGICOUNT to see which one it was
1616 */
1617 case TIOCMIWAIT:
1618 cli();
1619 /* note the counters on entry */
1620 cprev = info->icount;
1621 sti();
1622 while (1) {
1623 interruptible_sleep_on(&info->delta_msr_wait);
1624 /* see if a signal did it */
1625 if (signal_pending(current))
1626 return -ERESTARTSYS;
1627 cli();
1628 cnow = info->icount; /* atomic copy */
1629 sti();
1630 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1631 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1632 return -EIO; /* no change => error */
1633 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1634 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1635 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1636 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1637 return 0;
1638 }
1639 cprev = cnow;
1640 }
1641 /* NOTREACHED */
1642
1643 /*
1644 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1645 * Return: write counters to the user passed counter struct
1646 * NB: both 1->0 and 0->1 transitions are counted except for
1647 * RI where only 0->1 is counted.
1648 */
1649 case TIOCGICOUNT:
1650 cli();
1651 cnow = info->icount;
1652 sti();
1653 p_cuser = (struct serial_icounter_struct *) arg;
1654 if (put_user(cnow.cts, &p_cuser->cts) ||
1655 put_user(cnow.dsr, &p_cuser->dsr) ||
1656 put_user(cnow.rng, &p_cuser->rng) ||
1657 put_user(cnow.dcd, &p_cuser->dcd))
1658 return -EFAULT;
1659 return 0;
1660
1661 default:
1662 return -ENOIOCTLCMD;
1663 }
1664 /* return 0; */ /* Trigger warnings if fall through by a chance. */
1665 }
1666
1667 static void
su_set_termios(struct tty_struct * tty,struct termios * old_termios)1668 su_set_termios(struct tty_struct *tty, struct termios *old_termios)
1669 {
1670 struct su_struct *info = (struct su_struct *)tty->driver_data;
1671 unsigned long flags;
1672
1673 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1674 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1675 == RELEVANT_IFLAG(old_termios->c_iflag)))
1676 return;
1677
1678 change_speed(info, old_termios);
1679
1680 /* Handle transition to B0 status */
1681 if ((old_termios->c_cflag & CBAUD) &&
1682 !(tty->termios->c_cflag & CBAUD)) {
1683 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1684 save_flags(flags); cli();
1685 serial_out(info, UART_MCR, info->MCR);
1686 restore_flags(flags);
1687 }
1688
1689 /* Handle transition away from B0 status */
1690 if (!(old_termios->c_cflag & CBAUD) &&
1691 (tty->termios->c_cflag & CBAUD)) {
1692 info->MCR |= UART_MCR_DTR;
1693 if (!(tty->termios->c_cflag & CRTSCTS) ||
1694 !test_bit(TTY_THROTTLED, &tty->flags)) {
1695 info->MCR |= UART_MCR_RTS;
1696 }
1697 save_flags(flags); cli();
1698 serial_out(info, UART_MCR, info->MCR);
1699 restore_flags(flags);
1700 }
1701
1702 /* Handle turning off CRTSCTS */
1703 if ((old_termios->c_cflag & CRTSCTS) &&
1704 !(tty->termios->c_cflag & CRTSCTS)) {
1705 tty->hw_stopped = 0;
1706 su_start(tty);
1707 }
1708
1709 #if 0
1710 /*
1711 * No need to wake up processes in open wait, since they
1712 * sample the CLOCAL flag once, and don't recheck it.
1713 * XXX It's not clear whether the current behavior is correct
1714 * or not. Hence, this may change.....
1715 */
1716 if (!(old_termios->c_cflag & CLOCAL) &&
1717 (tty->termios->c_cflag & CLOCAL))
1718 wake_up_interruptible(&info->open_wait);
1719 #endif
1720 }
1721
1722 /*
1723 * ------------------------------------------------------------
1724 * su_close()
1725 *
1726 * This routine is called when the serial port gets closed. First, we
1727 * wait for the last remaining data to be sent. Then, we unlink its
1728 * async structure from the interrupt chain if necessary, and we free
1729 * that IRQ if nothing is left in the chain.
1730 * ------------------------------------------------------------
1731 */
1732 static void
su_close(struct tty_struct * tty,struct file * filp)1733 su_close(struct tty_struct *tty, struct file * filp)
1734 {
1735 struct su_struct *info = (struct su_struct *)tty->driver_data;
1736 unsigned long flags;
1737
1738 if (!info || serial_paranoia_check(info, tty->device, "su_close"))
1739 return;
1740
1741 save_flags(flags); cli();
1742
1743 if (tty_hung_up_p(filp)) {
1744 DBG_CNT("before DEC-hung");
1745 MOD_DEC_USE_COUNT;
1746 restore_flags(flags);
1747 return;
1748 }
1749
1750 #ifdef SERIAL_DEBUG_OPEN
1751 printk("su_close ttys%d, count = %d\n", info->line, info->count);
1752 #endif
1753 if ((tty->count == 1) && (info->count != 1)) {
1754 /*
1755 * Uh, oh. tty->count is 1, which means that the tty
1756 * structure will be freed. info->count should always
1757 * be one in these conditions. If it's greater than
1758 * one, we've got real problems, since it means the
1759 * serial port won't be shutdown.
1760 */
1761 printk("su_close: bad serial port count; tty->count is 1, "
1762 "info->count is %d\n", info->count);
1763 info->count = 1;
1764 }
1765 if (--info->count < 0) {
1766 printk("su_close: bad serial port count for ttys%d: %d\n",
1767 info->line, info->count);
1768 info->count = 0;
1769 }
1770 if (info->count) {
1771 DBG_CNT("before DEC-2");
1772 MOD_DEC_USE_COUNT;
1773 restore_flags(flags);
1774 return;
1775 }
1776 info->flags |= ASYNC_CLOSING;
1777 /*
1778 * Save the termios structure, since this port may have
1779 * separate termios for callout and dialin.
1780 */
1781 if (info->flags & ASYNC_NORMAL_ACTIVE)
1782 info->normal_termios = *tty->termios;
1783 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1784 info->callout_termios = *tty->termios;
1785 /*
1786 * Now we wait for the transmit buffer to clear; and we notify
1787 * the line discipline to only process XON/XOFF characters.
1788 */
1789 tty->closing = 1;
1790 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1791 tty_wait_until_sent(tty, info->closing_wait);
1792 /*
1793 * At this point we stop accepting input. To do this, we
1794 * disable the receive line status interrupts, and tell the
1795 * interrupt driver to stop checking the data ready bit in the
1796 * line status register.
1797 */
1798 info->IER &= ~UART_IER_RLSI;
1799 info->read_status_mask &= ~UART_LSR_DR;
1800 if (info->flags & ASYNC_INITIALIZED) {
1801 serial_out(info, UART_IER, info->IER);
1802 /*
1803 * Before we drop DTR, make sure the UART transmitter
1804 * has completely drained; this is especially
1805 * important if there is a transmit FIFO!
1806 */
1807 su_wait_until_sent(tty, info->timeout);
1808 }
1809 shutdown(info);
1810 if (tty->driver.flush_buffer)
1811 tty->driver.flush_buffer(tty);
1812 tty_ldisc_flush(tty);
1813 tty->closing = 0;
1814 info->event = 0;
1815 info->tty = 0;
1816 if (info->blocked_open) {
1817 if (info->close_delay) {
1818 current->state = TASK_INTERRUPTIBLE;
1819 schedule_timeout(info->close_delay);
1820 }
1821 wake_up_interruptible(&info->open_wait);
1822 }
1823 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1824 ASYNC_CLOSING);
1825 wake_up_interruptible(&info->close_wait);
1826 MOD_DEC_USE_COUNT;
1827 restore_flags(flags);
1828 }
1829
1830 /*
1831 * su_wait_until_sent() --- wait until the transmitter is empty
1832 */
1833 static void
su_wait_until_sent(struct tty_struct * tty,int timeout)1834 su_wait_until_sent(struct tty_struct *tty, int timeout)
1835 {
1836 struct su_struct * info = (struct su_struct *)tty->driver_data;
1837 unsigned long orig_jiffies, char_time;
1838 int lsr;
1839
1840 if (serial_paranoia_check(info, tty->device, "su_wait_until_sent"))
1841 return;
1842
1843 if (info->type == PORT_UNKNOWN)
1844 return;
1845
1846 if (info->xmit_fifo_size == 0)
1847 return; /* Just in case ... */
1848
1849 orig_jiffies = jiffies;
1850 /*
1851 * Set the check interval to be 1/5 of the estimated time to
1852 * send a single character, and make it at least 1. The check
1853 * interval should also be less than the timeout.
1854 *
1855 * Note: we have to use pretty tight timings here to satisfy
1856 * the NIST-PCTS.
1857 */
1858 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1859 char_time = char_time / 5;
1860 if (char_time == 0)
1861 char_time = 1;
1862 if (timeout)
1863 char_time = MIN(char_time, timeout);
1864 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1865 printk("In su_wait_until_sent(%d) check=%lu...", timeout, char_time);
1866 printk("jiff=%lu...", jiffies);
1867 #endif
1868 while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
1869 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1870 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1871 #endif
1872 current->state = TASK_INTERRUPTIBLE;
1873 schedule_timeout(char_time);
1874 if (signal_pending(current))
1875 break;
1876 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1877 break;
1878 }
1879 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1880 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1881 #endif
1882 }
1883
1884 /*
1885 * su_hangup() --- called by tty_hangup() when a hangup is signaled.
1886 */
1887 static void
su_hangup(struct tty_struct * tty)1888 su_hangup(struct tty_struct *tty)
1889 {
1890 struct su_struct * info = (struct su_struct *)tty->driver_data;
1891
1892 if (serial_paranoia_check(info, tty->device, "su_hangup"))
1893 return;
1894
1895 su_flush_buffer(tty);
1896 shutdown(info);
1897 info->event = 0;
1898 info->count = 0;
1899 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1900 info->tty = 0;
1901 wake_up_interruptible(&info->open_wait);
1902 }
1903
1904 /*
1905 * ------------------------------------------------------------
1906 * su_open() and friends
1907 * ------------------------------------------------------------
1908 */
1909 static int
block_til_ready(struct tty_struct * tty,struct file * filp,struct su_struct * info)1910 block_til_ready(struct tty_struct *tty, struct file * filp,
1911 struct su_struct *info)
1912 {
1913 DECLARE_WAITQUEUE(wait, current);
1914 int retval;
1915 int do_clocal = 0, extra_count = 0;
1916 unsigned long flags;
1917
1918 /*
1919 * If the device is in the middle of being closed, then block
1920 * until it's done, and then try again.
1921 */
1922 if (tty_hung_up_p(filp) ||
1923 (info->flags & ASYNC_CLOSING)) {
1924 if (info->flags & ASYNC_CLOSING)
1925 interruptible_sleep_on(&info->close_wait);
1926 #ifdef SERIAL_DO_RESTART
1927 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1928 -EAGAIN : -ERESTARTSYS);
1929 #else
1930 return -EAGAIN;
1931 #endif
1932 }
1933
1934 /*
1935 * If this is a callout device, then just make sure the normal
1936 * device isn't being used.
1937 */
1938 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1939 if (info->flags & ASYNC_NORMAL_ACTIVE)
1940 return -EBUSY;
1941 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1942 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1943 (info->session != current->session))
1944 return -EBUSY;
1945 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1946 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1947 (info->pgrp != current->pgrp))
1948 return -EBUSY;
1949 info->flags |= ASYNC_CALLOUT_ACTIVE;
1950 return 0;
1951 }
1952
1953 /*
1954 * If non-blocking mode is set, or the port is not enabled,
1955 * then make the check up front and then exit.
1956 */
1957 if ((filp->f_flags & O_NONBLOCK) ||
1958 (tty->flags & (1 << TTY_IO_ERROR))) {
1959 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1960 return -EBUSY;
1961 info->flags |= ASYNC_NORMAL_ACTIVE;
1962 return 0;
1963 }
1964
1965 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1966 if (info->normal_termios.c_cflag & CLOCAL)
1967 do_clocal = 1;
1968 } else {
1969 if (tty->termios->c_cflag & CLOCAL)
1970 do_clocal = 1;
1971 }
1972
1973 /*
1974 * Block waiting for the carrier detect and the line to become
1975 * free (i.e., not in use by the callout). While we are in
1976 * this loop, info->count is dropped by one, so that
1977 * su_close() knows when to free things. We restore it upon
1978 * exit, either normal or abnormal.
1979 */
1980 retval = 0;
1981 add_wait_queue(&info->open_wait, &wait);
1982 #ifdef SERIAL_DEBUG_OPEN
1983 printk("block_til_ready before block: ttys%d, count = %d\n",
1984 info->line, info->count);
1985 #endif
1986 save_flags(flags); cli();
1987 if (!tty_hung_up_p(filp)) {
1988 extra_count = 1;
1989 info->count--;
1990 }
1991 restore_flags(flags);
1992 info->blocked_open++;
1993 while (1) {
1994 save_flags(flags); cli();
1995 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1996 (tty->termios->c_cflag & CBAUD))
1997 serial_out(info, UART_MCR,
1998 serial_inp(info, UART_MCR) |
1999 (UART_MCR_DTR | UART_MCR_RTS));
2000 restore_flags(flags);
2001 set_current_state(TASK_INTERRUPTIBLE);
2002 if (tty_hung_up_p(filp) ||
2003 !(info->flags & ASYNC_INITIALIZED)) {
2004 #ifdef SERIAL_DO_RESTART
2005 if (info->flags & ASYNC_HUP_NOTIFY)
2006 retval = -EAGAIN;
2007 else
2008 retval = -ERESTARTSYS;
2009 #else
2010 retval = -EAGAIN;
2011 #endif
2012 break;
2013 }
2014 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2015 !(info->flags & ASYNC_CLOSING) &&
2016 (do_clocal || (serial_in(info, UART_MSR) &
2017 UART_MSR_DCD)))
2018 break;
2019 if (signal_pending(current)) {
2020 retval = -ERESTARTSYS;
2021 break;
2022 }
2023 #ifdef SERIAL_DEBUG_OPEN
2024 printk("block_til_ready blocking: ttys%d, count = %d\n",
2025 info->line, info->count);
2026 #endif
2027 schedule();
2028 }
2029 current->state = TASK_RUNNING;
2030 remove_wait_queue(&info->open_wait, &wait);
2031 if (extra_count)
2032 info->count++;
2033 info->blocked_open--;
2034 #ifdef SERIAL_DEBUG_OPEN
2035 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2036 info->line, info->count);
2037 #endif
2038 if (retval)
2039 return retval;
2040 info->flags |= ASYNC_NORMAL_ACTIVE;
2041 return 0;
2042 }
2043
2044 /*
2045 * This routine is called whenever a serial port is opened. It
2046 * enables interrupts for a serial port, linking in its async structure into
2047 * the IRQ chain. It also performs the serial-specific
2048 * initialization for the tty structure.
2049 */
2050 static int
su_open(struct tty_struct * tty,struct file * filp)2051 su_open(struct tty_struct *tty, struct file * filp)
2052 {
2053 struct su_struct *info;
2054 int retval, line;
2055 unsigned long page;
2056
2057 line = MINOR(tty->device) - tty->driver.minor_start;
2058 if ((line < 0) || (line >= NR_PORTS))
2059 return -ENODEV;
2060 info = su_table + line;
2061 info->count++;
2062 tty->driver_data = info;
2063 info->tty = tty;
2064
2065 if (serial_paranoia_check(info, tty->device, "su_open")) {
2066 info->count--;
2067 return -ENODEV;
2068 }
2069
2070 #ifdef SERIAL_DEBUG_OPEN
2071 printk("su_open %s%d, count = %d\n", tty->driver.name, info->line,
2072 info->count);
2073 #endif
2074 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2075
2076 if (!tmp_buf) {
2077 page = get_free_page(GFP_KERNEL);
2078 if (!page)
2079 return -ENOMEM;
2080 if (tmp_buf)
2081 free_page(page);
2082 else
2083 tmp_buf = (unsigned char *) page;
2084 }
2085
2086 /*
2087 * If the port is the middle of closing, bail out now
2088 */
2089 if (tty_hung_up_p(filp) ||
2090 (info->flags & ASYNC_CLOSING)) {
2091 if (info->flags & ASYNC_CLOSING)
2092 interruptible_sleep_on(&info->close_wait);
2093 #ifdef SERIAL_DO_RESTART
2094 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2095 -EAGAIN : -ERESTARTSYS);
2096 #else
2097 return -EAGAIN;
2098 #endif
2099 }
2100
2101 /*
2102 * Start up serial port
2103 */
2104 retval = startup(info);
2105 if (retval)
2106 return retval;
2107
2108 MOD_INC_USE_COUNT;
2109 retval = block_til_ready(tty, filp, info);
2110 if (retval) {
2111 #ifdef SERIAL_DEBUG_OPEN
2112 printk("su_open returning after block_til_ready with %d\n",
2113 retval);
2114 #endif
2115 return retval;
2116 }
2117
2118 if ((info->count == 1) &&
2119 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2120 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2121 *tty->termios = info->normal_termios;
2122 else
2123 *tty->termios = info->callout_termios;
2124 change_speed(info, 0);
2125 }
2126 #ifdef CONFIG_SERIAL_CONSOLE
2127 if (sercons.cflag && sercons.index == line) {
2128 tty->termios->c_cflag = sercons.cflag;
2129 sercons.cflag = 0;
2130 change_speed(info, 0);
2131 }
2132 #endif
2133 info->session = current->session;
2134 info->pgrp = current->pgrp;
2135
2136 #ifdef SERIAL_DEBUG_OPEN
2137 printk("su_open ttys%d successful...", info->line);
2138 #endif
2139 return 0;
2140 }
2141
2142 /*
2143 * /proc fs routines....
2144 */
2145 static int
line_info(char * buf,struct su_struct * info)2146 line_info(char *buf, struct su_struct *info)
2147 {
2148 char stat_buf[30], control, status;
2149 int ret;
2150 unsigned long flags;
2151
2152 if (info->port == 0 || info->type == PORT_UNKNOWN)
2153 return 0;
2154
2155 ret = sprintf(buf, "%u: uart:%s port:%lX irq:%s",
2156 info->line, uart_config[info->type].name,
2157 (unsigned long)info->port, __irq_itoa(info->irq));
2158
2159 /*
2160 * Figure out the current RS-232 lines
2161 */
2162 save_flags(flags); cli();
2163 status = serial_in(info, UART_MSR);
2164 control = info ? info->MCR : serial_in(info, UART_MCR);
2165 restore_flags(flags);
2166
2167 stat_buf[0] = 0;
2168 stat_buf[1] = 0;
2169 if (control & UART_MCR_RTS)
2170 strcat(stat_buf, "|RTS");
2171 if (status & UART_MSR_CTS)
2172 strcat(stat_buf, "|CTS");
2173 if (control & UART_MCR_DTR)
2174 strcat(stat_buf, "|DTR");
2175 if (status & UART_MSR_DSR)
2176 strcat(stat_buf, "|DSR");
2177 if (status & UART_MSR_DCD)
2178 strcat(stat_buf, "|CD");
2179 if (status & UART_MSR_RI)
2180 strcat(stat_buf, "|RI");
2181
2182 if (info->quot) {
2183 ret += sprintf(buf+ret, " baud:%u",
2184 info->baud_base / info->quot);
2185 }
2186
2187 ret += sprintf(buf+ret, " tx:%u rx:%u",
2188 info->icount.tx, info->icount.rx);
2189
2190 if (info->icount.frame)
2191 ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2192
2193 if (info->icount.parity)
2194 ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2195
2196 if (info->icount.brk)
2197 ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2198
2199 if (info->icount.overrun)
2200 ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2201
2202 /*
2203 * Last thing is the RS-232 status lines
2204 */
2205 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2206 return ret;
2207 }
2208
su_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)2209 int su_read_proc(char *page, char **start, off_t off, int count,
2210 int *eof, void *data)
2211 {
2212 int i, len = 0;
2213 off_t begin = 0;
2214
2215 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2216 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2217 len += line_info(page + len, &su_table[i]);
2218 if (len+begin > off+count)
2219 goto done;
2220 if (len+begin < off) {
2221 begin += len;
2222 len = 0;
2223 }
2224 }
2225 *eof = 1;
2226 done:
2227 if (off >= len+begin)
2228 return 0;
2229 *start = page + (off-begin);
2230 return ((count < begin+len-off) ? count : begin+len-off);
2231 }
2232
2233 /*
2234 * ---------------------------------------------------------------------
2235 * su_XXX_init() and friends
2236 *
2237 * su_XXX_init() is called at boot-time to initialize the serial driver.
2238 * ---------------------------------------------------------------------
2239 */
2240
2241 /*
2242 * This routine prints out the appropriate serial driver version
2243 * number, and identifies which options were configured into this
2244 * driver.
2245 */
show_su_version(void)2246 static __inline__ void __init show_su_version(void)
2247 {
2248 char *revision = "$Revision: 1.54 $";
2249 char *version, *p;
2250
2251 version = strchr(revision, ' ');
2252 strcpy(serial_version, ++version);
2253 p = strchr(serial_version, ' ');
2254 *p = '\0';
2255 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2256 }
2257
2258 /*
2259 * This routine is called by su_{serial|kbd_ms}_init() to initialize a specific
2260 * serial port. It determines what type of UART chip this serial port is
2261 * using: 8250, 16450, 16550, 16550A. The important question is
2262 * whether or not this UART is a 16550A, since this will determine
2263 * whether or not we can use its FIFO features.
2264 */
2265 static void
autoconfig(struct su_struct * info)2266 autoconfig(struct su_struct *info)
2267 {
2268 unsigned char status1, status2, scratch, scratch2;
2269 struct linux_ebus_device *dev = 0;
2270 struct linux_ebus *ebus;
2271 #ifdef CONFIG_SPARC64
2272 struct isa_bridge *isa_br;
2273 struct isa_device *isa_dev;
2274 #endif
2275 #ifndef __sparc_v9__
2276 struct linux_prom_registers reg0;
2277 #endif
2278 unsigned long flags;
2279
2280 if (!info->port_node || !info->port_type)
2281 return;
2282
2283 /*
2284 * First we look for Ebus-bases su's
2285 */
2286 for_each_ebus(ebus) {
2287 for_each_ebusdev(dev, ebus) {
2288 if (dev->prom_node == info->port_node) {
2289 info->port = dev->resource[0].start;
2290 info->irq = dev->irqs[0];
2291 goto ebus_done;
2292 }
2293 }
2294 }
2295
2296 #ifdef CONFIG_SPARC64
2297 for_each_isa(isa_br) {
2298 for_each_isadev(isa_dev, isa_br) {
2299 if (isa_dev->prom_node == info->port_node) {
2300 info->port = isa_dev->resource.start;
2301 info->irq = isa_dev->irq;
2302 goto ebus_done;
2303 }
2304 }
2305 }
2306 #endif
2307
2308 #ifdef __sparc_v9__
2309 /*
2310 * Not on Ebus, bailing.
2311 */
2312 return;
2313 #else
2314 /*
2315 * Not on Ebus, must be OBIO.
2316 */
2317 if (prom_getproperty(info->port_node, "reg",
2318 (char *)®0, sizeof(reg0)) == -1) {
2319 prom_printf("su: no \"reg\" property\n");
2320 return;
2321 }
2322 prom_apply_obio_ranges(®0, 1);
2323 if (reg0.which_io != 0) { /* Just in case... */
2324 prom_printf("su: bus number nonzero: 0x%x:%x\n",
2325 reg0.which_io, reg0.phys_addr);
2326 return;
2327 }
2328 if ((info->port = (unsigned long) ioremap(reg0.phys_addr,
2329 reg0.reg_size)) == 0) {
2330 prom_printf("su: cannot map\n");
2331 return;
2332 }
2333
2334 /*
2335 * There is no intr property on MrCoffee, so hardwire it.
2336 */
2337 info->irq = IRQ_4M(13);
2338 #endif
2339
2340 ebus_done:
2341
2342 #ifdef SERIAL_DEBUG_OPEN
2343 printk("Found 'su' at %016lx IRQ %s\n", info->port,
2344 __irq_itoa(info->irq));
2345 #endif
2346
2347 info->magic = SERIAL_MAGIC;
2348
2349 save_flags(flags); cli();
2350
2351 /*
2352 * Do a simple existence test first; if we fail this, there's
2353 * no point trying anything else.
2354 *
2355 * 0x80 is used as a nonsense port to prevent against false
2356 * positives due to ISA bus float. The assumption is that
2357 * 0x80 is a non-existent port; which should be safe since
2358 * include/asm/io.h also makes this assumption.
2359 */
2360 scratch = serial_inp(info, UART_IER);
2361 serial_outp(info, UART_IER, 0);
2362 scratch2 = serial_inp(info, UART_IER);
2363 serial_outp(info, UART_IER, scratch);
2364 if (scratch2) {
2365 restore_flags(flags);
2366 return; /* We failed; there's nothing here */
2367 }
2368
2369 scratch = serial_inp(info, UART_MCR);
2370 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2371 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2372 status1 = serial_inp(info, UART_MSR) & 0xF0;
2373 serial_outp(info, UART_MCR, scratch);
2374 if (status1 != 0x90) {
2375 /*
2376 * This code fragment used to fail, now it fixed itself.
2377 * We keep the printout for a case.
2378 */
2379 printk("su: loopback returned status 0x%02x\n", status1);
2380 restore_flags(flags);
2381 return;
2382 }
2383
2384 scratch2 = serial_in(info, UART_LCR);
2385 serial_outp(info, UART_LCR, 0xBF); /* set up for StarTech test */
2386 serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
2387 serial_outp(info, UART_LCR, 0);
2388 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2389 scratch = serial_in(info, UART_IIR) >> 6;
2390 switch (scratch) {
2391 case 0:
2392 info->type = PORT_16450;
2393 break;
2394 case 1:
2395 info->type = PORT_UNKNOWN;
2396 break;
2397 case 2:
2398 info->type = PORT_16550;
2399 break;
2400 case 3:
2401 info->type = PORT_16550A;
2402 break;
2403 }
2404 if (info->type == PORT_16550A) {
2405 /* Check for Startech UART's */
2406 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2407 if (serial_in(info, UART_EFR) == 0) {
2408 info->type = PORT_16650;
2409 } else {
2410 serial_outp(info, UART_LCR, 0xBF);
2411 if (serial_in(info, UART_EFR) == 0)
2412 info->type = PORT_16650V2;
2413 }
2414 }
2415 if (info->type == PORT_16550A) {
2416 /* Check for TI 16750 */
2417 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2418 serial_outp(info, UART_FCR,
2419 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
2420 scratch = serial_in(info, UART_IIR) >> 5;
2421 if (scratch == 7) {
2422 serial_outp(info, UART_LCR, 0);
2423 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2424 scratch = serial_in(info, UART_IIR) >> 5;
2425 if (scratch == 6)
2426 info->type = PORT_16750;
2427 }
2428 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2429 }
2430 serial_outp(info, UART_LCR, scratch2);
2431 if (info->type == PORT_16450) {
2432 scratch = serial_in(info, UART_SCR);
2433 serial_outp(info, UART_SCR, 0xa5);
2434 status1 = serial_in(info, UART_SCR);
2435 serial_outp(info, UART_SCR, 0x5a);
2436 status2 = serial_in(info, UART_SCR);
2437 serial_outp(info, UART_SCR, scratch);
2438
2439 if ((status1 != 0xa5) || (status2 != 0x5a))
2440 info->type = PORT_8250;
2441 }
2442 info->xmit_fifo_size = uart_config[info->type].dfl_xmit_fifo_size;
2443
2444 if (info->type == PORT_UNKNOWN) {
2445 restore_flags(flags);
2446 return;
2447 }
2448
2449 sprintf(info->name, "su(%s)", su_typev[info->port_type]);
2450
2451 /*
2452 * Reset the UART.
2453 */
2454 serial_outp(info, UART_MCR, 0x00);
2455 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT));
2456 (void)serial_in(info, UART_RX);
2457 serial_outp(info, UART_IER, 0x00);
2458
2459 restore_flags(flags);
2460 }
2461
2462 /* This is used by the SAB driver to adjust where its minor
2463 * numbers start, we always are probed for first.
2464 */
2465 int su_num_ports = 0;
2466 EXPORT_SYMBOL(su_num_ports);
2467
2468 /*
2469 * The serial driver boot-time initialization code!
2470 */
su_serial_init(void)2471 int __init su_serial_init(void)
2472 {
2473 int i;
2474 struct su_struct *info;
2475
2476 init_bh(SERIAL_BH, do_serial_bh);
2477 show_su_version();
2478
2479 /* Initialize the tty_driver structure */
2480
2481 memset(&serial_driver, 0, sizeof(struct tty_driver));
2482 serial_driver.magic = TTY_DRIVER_MAGIC;
2483 serial_driver.driver_name = "su";
2484 #ifdef CONFIG_DEVFS_FS
2485 serial_driver.name = "tts/%d";
2486 #else
2487 serial_driver.name = "ttyS";
2488 #endif
2489 serial_driver.major = TTY_MAJOR;
2490 serial_driver.minor_start = 64;
2491 serial_driver.num = NR_PORTS;
2492 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2493 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2494 serial_driver.init_termios = tty_std_termios;
2495 serial_driver.init_termios.c_cflag =
2496 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2497 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2498 serial_driver.refcount = &serial_refcount;
2499 serial_driver.table = serial_table;
2500 serial_driver.termios = serial_termios;
2501 serial_driver.termios_locked = serial_termios_locked;
2502
2503 serial_driver.open = su_open;
2504 serial_driver.close = su_close;
2505 serial_driver.write = su_write;
2506 serial_driver.put_char = su_put_char;
2507 serial_driver.flush_chars = su_flush_chars;
2508 serial_driver.write_room = su_write_room;
2509 serial_driver.chars_in_buffer = su_chars_in_buffer;
2510 serial_driver.flush_buffer = su_flush_buffer;
2511 serial_driver.ioctl = su_ioctl;
2512 serial_driver.throttle = su_throttle;
2513 serial_driver.unthrottle = su_unthrottle;
2514 serial_driver.send_xchar = su_send_xchar;
2515 serial_driver.set_termios = su_set_termios;
2516 serial_driver.stop = su_stop;
2517 serial_driver.start = su_start;
2518 serial_driver.hangup = su_hangup;
2519 serial_driver.break_ctl = su_break;
2520 serial_driver.wait_until_sent = su_wait_until_sent;
2521 serial_driver.read_proc = su_read_proc;
2522
2523 /*
2524 * The callout device is just like normal device except for
2525 * major number and the subtype code.
2526 */
2527 callout_driver = serial_driver;
2528 #ifdef CONFIG_DEVFS_FS
2529 callout_driver.name = "cua/%d";
2530 #else
2531 callout_driver.name = "cua";
2532 #endif
2533 callout_driver.major = TTYAUX_MAJOR;
2534 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2535 callout_driver.read_proc = 0;
2536 callout_driver.proc_entry = 0;
2537
2538 if (tty_register_driver(&serial_driver))
2539 panic("Couldn't register regular su\n");
2540 if (tty_register_driver(&callout_driver))
2541 panic("Couldn't register callout su\n");
2542
2543 for (i = 0, info = su_table; i < NR_PORTS; i++, info++) {
2544 info->line = i;
2545 info->type = PORT_UNKNOWN;
2546 info->baud_base = BAUD_BASE;
2547 /* info->flags = 0; */
2548 info->custom_divisor = 0;
2549 info->close_delay = 5*HZ/10;
2550 info->closing_wait = 30*HZ;
2551 info->callout_termios = callout_driver.init_termios;
2552 info->normal_termios = serial_driver.init_termios;
2553 info->icount.cts = info->icount.dsr =
2554 info->icount.rng = info->icount.dcd = 0;
2555 info->icount.rx = info->icount.tx = 0;
2556 info->icount.frame = info->icount.parity = 0;
2557 info->icount.overrun = info->icount.brk = 0;
2558 info->tqueue.routine = do_softint;
2559 info->tqueue.data = info;
2560 info->cflag = serial_driver.init_termios.c_cflag;
2561 init_waitqueue_head(&info->open_wait);
2562 init_waitqueue_head(&info->close_wait);
2563 init_waitqueue_head(&info->delta_msr_wait);
2564
2565 autoconfig(info);
2566 if (info->type == PORT_UNKNOWN)
2567 continue;
2568
2569 printk(KERN_INFO "%s at 0x%lx (tty %d irq %s) is a %s\n",
2570 info->name, (long)info->port, i, __irq_itoa(info->irq),
2571 uart_config[info->type].name);
2572 }
2573
2574 for (i = 0, info = su_table; i < NR_PORTS; i++, info++)
2575 if (info->type == PORT_UNKNOWN)
2576 break;
2577
2578 su_num_ports = i;
2579 serial_driver.num = callout_driver.num = i;
2580
2581 return 0;
2582 }
2583
su_kbd_ms_init(void)2584 int __init su_kbd_ms_init(void)
2585 {
2586 int i;
2587 struct su_struct *info;
2588
2589 show_su_version();
2590
2591 for (i = 0, info = su_table; i < 2; i++, info++) {
2592 info->line = i;
2593 info->type = PORT_UNKNOWN;
2594 info->baud_base = BAUD_BASE;
2595
2596 if (info->port_type == SU_PORT_KBD)
2597 info->cflag = B1200 | CS8 | CLOCAL | CREAD;
2598 else
2599 info->cflag = B4800 | CS8 | CLOCAL | CREAD;
2600
2601 init_waitqueue_head(&info->open_wait);
2602 init_waitqueue_head(&info->close_wait);
2603 init_waitqueue_head(&info->delta_msr_wait);
2604
2605 autoconfig(info);
2606 if (info->type == PORT_UNKNOWN)
2607 continue;
2608
2609 printk(KERN_INFO "%s at 0x%lx (irq = %s) is a %s\n",
2610 info->name, info->port, __irq_itoa(info->irq),
2611 uart_config[info->type].name);
2612
2613 startup(info);
2614 if (info->port_type == SU_PORT_KBD)
2615 keyboard_zsinit(su_put_char_kbd);
2616 else
2617 sun_mouse_zsinit();
2618 }
2619 return 0;
2620 }
2621
su_node_ok(int node,char * name,int namelen)2622 static int su_node_ok(int node, char *name, int namelen)
2623 {
2624 if (strncmp(name, "su", namelen) == 0 ||
2625 strncmp(name, "su_pnp", namelen) == 0)
2626 return 1;
2627
2628 if (strncmp(name, "serial", namelen) == 0) {
2629 char compat[32];
2630 int clen;
2631
2632 /* Is it _really_ a 'su' device? */
2633 clen = prom_getproperty(node, "compatible", compat, sizeof(compat));
2634 if (clen > 0) {
2635 if (strncmp(compat, "sab82532", 8) == 0) {
2636 /* Nope, Siemens serial, not for us. */
2637 return 0;
2638 }
2639 }
2640 return 1;
2641 }
2642
2643 return 0;
2644 }
2645
2646 /*
2647 * We got several platforms which present 'su' in different parts
2648 * of device tree. 'su' may be found under obio, ebus, isa and pci.
2649 * We walk over the tree and find them wherever PROM hides them.
2650 */
su_probe_any(struct su_probe_scan * t,int sunode)2651 void __init su_probe_any(struct su_probe_scan *t, int sunode)
2652 {
2653 struct su_struct *info;
2654 int len;
2655
2656 if (t->devices >= NR_PORTS) return;
2657
2658 for (; sunode != 0; sunode = prom_getsibling(sunode)) {
2659 len = prom_getproperty(sunode, "name", t->prop, SU_PROPSIZE);
2660 if (len <= 1) continue; /* Broken PROM node */
2661 if (su_node_ok(sunode, t->prop, len)) {
2662 info = &su_table[t->devices];
2663 if (t->kbnode != 0 && sunode == t->kbnode) {
2664 t->kbx = t->devices;
2665 info->port_type = SU_PORT_KBD;
2666 } else if (t->msnode != 0 && sunode == t->msnode) {
2667 t->msx = t->devices;
2668 info->port_type = SU_PORT_MS;
2669 } else {
2670 #ifdef __sparc_v9__
2671 /*
2672 * Do not attempt to use the truncated
2673 * keyboard/mouse ports as serial ports
2674 * on Ultras with PC keyboard attached.
2675 */
2676 if (prom_getbool(sunode, "mouse"))
2677 continue;
2678 if (prom_getbool(sunode, "keyboard"))
2679 continue;
2680 #endif
2681 info->port_type = SU_PORT_PORT;
2682 }
2683 info->is_console = 0;
2684 info->port_node = sunode;
2685 ++t->devices;
2686 } else {
2687 su_probe_any(t, prom_getchild(sunode));
2688 }
2689 }
2690 }
2691
su_probe(void)2692 int __init su_probe(void)
2693 {
2694 int node;
2695 int len;
2696 struct su_probe_scan scan;
2697
2698 /*
2699 * First, we scan the tree.
2700 */
2701 scan.devices = 0;
2702 scan.msx = -1;
2703 scan.kbx = -1;
2704 scan.kbnode = 0;
2705 scan.msnode = 0;
2706
2707 /*
2708 * Get the nodes for keyboard and mouse from 'aliases'...
2709 */
2710 node = prom_getchild(prom_root_node);
2711 node = prom_searchsiblings(node, "aliases");
2712 if (node != 0) {
2713
2714 len = prom_getproperty(node, "keyboard", scan.prop,SU_PROPSIZE);
2715 if (len > 0) {
2716 scan.prop[len] = 0;
2717 scan.kbnode = prom_finddevice(scan.prop);
2718 }
2719
2720 len = prom_getproperty(node, "mouse", scan.prop, SU_PROPSIZE);
2721 if (len > 0) {
2722 scan.prop[len] = 0;
2723 scan.msnode = prom_finddevice(scan.prop);
2724 }
2725 }
2726
2727 su_probe_any(&scan, prom_getchild(prom_root_node));
2728
2729 /*
2730 * Second, we process the special case of keyboard and mouse.
2731 *
2732 * Currently if we got keyboard and mouse hooked to "su" ports
2733 * we do not use any possible remaining "su" as a serial port.
2734 * Thus, we ignore values of .msx and .kbx, then compact ports.
2735 * Those who want to address this issue need to merge
2736 * su_serial_init() and su_ms_kbd_init().
2737 */
2738 if (scan.msx != -1 && scan.kbx != -1) {
2739 su_table[0].port_type = SU_PORT_MS;
2740 su_table[0].is_console = 0;
2741 su_table[0].port_node = scan.msnode;
2742 su_table[1].port_type = SU_PORT_KBD;
2743 su_table[1].is_console = 0;
2744 su_table[1].port_node = scan.kbnode;
2745
2746 sunserial_setinitfunc(su_kbd_ms_init);
2747 rs_ops.rs_change_mouse_baud = su_change_mouse_baud;
2748 sunkbd_setinitfunc(sun_kbd_init);
2749 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2750 kbd_ops.setledstate = sun_setledstate;
2751 kbd_ops.getledstate = sun_getledstate;
2752 kbd_ops.setkeycode = sun_setkeycode;
2753 kbd_ops.getkeycode = sun_getkeycode;
2754 #ifdef CONFIG_PCI
2755 sunkbd_install_keymaps(sun_key_maps,
2756 sun_keymap_count, sun_func_buf, sun_func_table,
2757 sun_funcbufsize, sun_funcbufleft,
2758 sun_accent_table, sun_accent_table_size);
2759 #endif
2760 return 0;
2761 }
2762 if (scan.msx != -1 || scan.kbx != -1) {
2763 printk("su_probe: cannot match keyboard and mouse, confused\n");
2764 return -ENODEV;
2765 }
2766
2767 if (scan.devices == 0)
2768 return -ENODEV;
2769
2770 #ifdef CONFIG_SERIAL_CONSOLE
2771 /*
2772 * Console must be initiated after the generic initialization.
2773 * sunserial_setinitfunc inverts order, so call this before next one.
2774 */
2775 sunserial_setinitfunc(su_serial_console_init);
2776 #endif
2777 sunserial_setinitfunc(su_serial_init);
2778 return 0;
2779 }
2780
2781 /*
2782 * ------------------------------------------------------------
2783 * Serial console driver
2784 * ------------------------------------------------------------
2785 */
2786 #ifdef CONFIG_SERIAL_CONSOLE
2787
2788 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2789
2790 /*
2791 * Wait for transmitter & holding register to empty
2792 */
2793 static __inline__ void
wait_for_xmitr(struct su_struct * info)2794 wait_for_xmitr(struct su_struct *info)
2795 {
2796 int lsr;
2797 unsigned int tmout = 1000000;
2798
2799 do {
2800 lsr = su_inb(info, UART_LSR);
2801 if (--tmout == 0)
2802 break;
2803 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
2804 }
2805
2806 /*
2807 * Print a string to the serial port trying not to disturb
2808 * any possible real use of the port...
2809 */
2810 static void
serial_console_write(struct console * co,const char * s,unsigned count)2811 serial_console_write(struct console *co, const char *s,
2812 unsigned count)
2813 {
2814 struct su_struct *info;
2815 int ier;
2816 unsigned i;
2817
2818 info = su_table + co->index;
2819 /*
2820 * First save the IER then disable the interrupts
2821 */
2822 ier = su_inb(info, UART_IER);
2823 su_outb(info, UART_IER, 0x00);
2824
2825 /*
2826 * Now, do each character
2827 */
2828 for (i = 0; i < count; i++, s++) {
2829 wait_for_xmitr(info);
2830
2831 /*
2832 * Send the character out.
2833 * If a LF, also do CR...
2834 */
2835 su_outb(info, UART_TX, *s);
2836 if (*s == 10) {
2837 wait_for_xmitr(info);
2838 su_outb(info, UART_TX, 13);
2839 }
2840 }
2841
2842 /*
2843 * Finally, Wait for transmitter & holding register to empty
2844 * and restore the IER
2845 */
2846 wait_for_xmitr(info);
2847 su_outb(info, UART_IER, ier);
2848 }
2849
2850 static kdev_t
serial_console_device(struct console * c)2851 serial_console_device(struct console *c)
2852 {
2853 return MKDEV(TTY_MAJOR, 64 + c->index);
2854 }
2855
2856 /*
2857 * Setup initial baud/bits/parity. We do two things here:
2858 * - construct a cflag setting for the first su_open()
2859 * - initialize the serial port
2860 * Return non-zero if we didn't find a serial port.
2861 */
serial_console_setup(struct console * co,char * options)2862 static int __init serial_console_setup(struct console *co, char *options)
2863 {
2864 struct su_struct *info;
2865 unsigned cval;
2866 int baud = 9600;
2867 int bits = 8;
2868 int parity = 'n';
2869 int cflag = CREAD | HUPCL | CLOCAL;
2870 int quot = 0;
2871 char *s;
2872
2873 if (options) {
2874 baud = simple_strtoul(options, NULL, 10);
2875 s = options;
2876 while (*s >= '0' && *s <= '9')
2877 s++;
2878 if (*s) parity = *s++;
2879 if (*s) bits = *s - '0';
2880 }
2881
2882 /*
2883 * Now construct a cflag setting.
2884 */
2885 switch (baud) {
2886 case 1200:
2887 cflag |= B1200;
2888 break;
2889 case 2400:
2890 cflag |= B2400;
2891 break;
2892 case 4800:
2893 cflag |= B4800;
2894 break;
2895 case 19200:
2896 cflag |= B19200;
2897 break;
2898 case 38400:
2899 cflag |= B38400;
2900 break;
2901 case 57600:
2902 cflag |= B57600;
2903 break;
2904 case 115200:
2905 cflag |= B115200;
2906 break;
2907 case 9600:
2908 default:
2909 cflag |= B9600;
2910 baud = 9600;
2911 break;
2912 }
2913 switch (bits) {
2914 case 7:
2915 cflag |= CS7;
2916 break;
2917 default:
2918 case 8:
2919 cflag |= CS8;
2920 break;
2921 }
2922 switch (parity) {
2923 case 'o': case 'O':
2924 cflag |= PARODD;
2925 break;
2926 case 'e': case 'E':
2927 cflag |= PARENB;
2928 break;
2929 }
2930 co->cflag = cflag;
2931
2932 /*
2933 * Divisor, bytesize and parity
2934 */
2935 info = su_table + co->index;
2936 quot = BAUD_BASE / baud;
2937 cval = cflag & (CSIZE | CSTOPB);
2938 #if defined(__powerpc__) || defined(__alpha__)
2939 cval >>= 8;
2940 #else /* !__powerpc__ && !__alpha__ */
2941 cval >>= 4;
2942 #endif /* !__powerpc__ && !__alpha__ */
2943 if (cflag & PARENB)
2944 cval |= UART_LCR_PARITY;
2945 if (!(cflag & PARODD))
2946 cval |= UART_LCR_EPAR;
2947
2948 /*
2949 * Disable UART interrupts, set DTR and RTS high
2950 * and set speed.
2951 */
2952 su_outb(info, UART_IER, 0);
2953 su_outb(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2954 su_outb(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
2955 su_outb(info, UART_DLL, quot & 0xff); /* LS of divisor */
2956 su_outb(info, UART_DLM, quot >> 8); /* MS of divisor */
2957 su_outb(info, UART_LCR, cval); /* reset DLAB */
2958 info->quot = quot;
2959
2960 /*
2961 * If we read 0xff from the LSR, there is no UART here.
2962 */
2963 if (su_inb(info, UART_LSR) == 0xff)
2964 return -1;
2965
2966 info->is_console = 1;
2967
2968 return 0;
2969 }
2970
2971 static struct console sercons = {
2972 name: "ttyS",
2973 write: serial_console_write,
2974 device: serial_console_device,
2975 setup: serial_console_setup,
2976 flags: CON_PRINTBUFFER,
2977 index: -1,
2978 };
2979
2980 int su_console_registered = 0;
2981
2982 /*
2983 * Register console.
2984 */
su_serial_console_init(void)2985 int __init su_serial_console_init(void)
2986 {
2987 extern int con_is_present(void);
2988 int index;
2989
2990 if (con_is_present())
2991 return 0;
2992 if (serial_console == 0)
2993 return 0;
2994 index = serial_console - 1;
2995 if (su_table[index].port == 0 || su_table[index].port_node == 0)
2996 return 0;
2997 sercons.index = index;
2998 register_console(&sercons);
2999 su_console_registered = 1;
3000 return 0;
3001 }
3002
3003 #endif /* CONFIG_SERIAL_CONSOLE */
3004