974cac88f7e7b3a52b76b273c14d5c32366e12be
[deliverable/linux.git] / arch / ia64 / hp / sim / simserial.c
1 /*
2 * Simulated Serial Driver (fake serial)
3 *
4 * This driver is mostly used for bringup purposes and will go away.
5 * It has a strong dependency on the system console. All outputs
6 * are rerouted to the same facility as the one used by printk which, in our
7 * case means sys_sim.c console (goes via the simulator). The code hereafter
8 * is completely leveraged from the serial.c driver.
9 *
10 * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
11 * Stephane Eranian <eranian@hpl.hp.com>
12 * David Mosberger-Tang <davidm@hpl.hp.com>
13 *
14 * 02/04/00 D. Mosberger Merged in serial.c bug fixes in rs_close().
15 * 02/25/00 D. Mosberger Synced up with 2.3.99pre-5 version of serial.c.
16 * 07/30/02 D. Mosberger Replace sti()/cli() with explicit spinlocks & local irq masking
17 */
18
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/sched.h>
22 #include <linux/tty.h>
23 #include <linux/tty_flip.h>
24 #include <linux/major.h>
25 #include <linux/fcntl.h>
26 #include <linux/mm.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include <linux/capability.h>
30 #include <linux/console.h>
31 #include <linux/module.h>
32 #include <linux/serial.h>
33 #include <linux/serialP.h>
34 #include <linux/sysrq.h>
35
36 #include <asm/irq.h>
37 #include <asm/hpsim.h>
38 #include <asm/hw_irq.h>
39 #include <asm/uaccess.h>
40
41 #include "hpsim_ssc.h"
42
43 #undef SIMSERIAL_DEBUG /* define this to get some debug information */
44
45 #define KEYBOARD_INTR 3 /* must match with simulator! */
46
47 #define NR_PORTS 1 /* only one port for now */
48
49 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
50
51 static char *serial_name = "SimSerial driver";
52 static char *serial_version = "0.6";
53
54 /*
55 * This has been extracted from asm/serial.h. We need one eventually but
56 * I don't know exactly what we're going to put in it so just fake one
57 * for now.
58 */
59 #define BASE_BAUD ( 1843200 / 16 )
60
61 #define STD_COM_FLAGS (ASYNC_BOOT_AUTOCONF | ASYNC_SKIP_TEST)
62
63 /*
64 * Most of the values here are meaningless to this particular driver.
65 * However some values must be preserved for the code (leveraged from serial.c
66 * to work correctly).
67 * port must not be 0
68 * type must not be UNKNOWN
69 * So I picked arbitrary (guess from where?) values instead
70 */
71 static struct serial_state rs_table[NR_PORTS]={
72 /* UART CLK PORT IRQ FLAGS */
73 { 0, BASE_BAUD, 0x3F8, 0, STD_COM_FLAGS,0,PORT_16550 } /* ttyS0 */
74 };
75
76 /*
77 * Just for the fun of it !
78 */
79 static struct serial_uart_config uart_config[] = {
80 { "unknown", 1, 0 },
81 { "8250", 1, 0 },
82 { "16450", 1, 0 },
83 { "16550", 1, 0 },
84 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
85 { "cirrus", 1, 0 },
86 { "ST16650", 1, UART_CLEAR_FIFO | UART_STARTECH },
87 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
88 UART_STARTECH },
89 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
90 { NULL, 0}
91 };
92
93 struct tty_driver *hp_simserial_driver;
94
95 static struct async_struct *IRQ_ports[NR_IRQS];
96
97 static struct console *console;
98
99 static unsigned char *tmp_buf;
100
101 extern struct console *console_drivers; /* from kernel/printk.c */
102
103 /*
104 * ------------------------------------------------------------
105 * rs_stop() and rs_start()
106 *
107 * This routines are called before setting or resetting tty->stopped.
108 * They enable or disable transmitter interrupts, as necessary.
109 * ------------------------------------------------------------
110 */
111 static void rs_stop(struct tty_struct *tty)
112 {
113 #ifdef SIMSERIAL_DEBUG
114 printk("rs_stop: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
115 tty->stopped, tty->hw_stopped, tty->flow_stopped);
116 #endif
117
118 }
119
120 static void rs_start(struct tty_struct *tty)
121 {
122 #ifdef SIMSERIAL_DEBUG
123 printk("rs_start: tty->stopped=%d tty->hw_stopped=%d tty->flow_stopped=%d\n",
124 tty->stopped, tty->hw_stopped, tty->flow_stopped);
125 #endif
126 }
127
128 static void receive_chars(struct tty_struct *tty)
129 {
130 unsigned char ch;
131 static unsigned char seen_esc = 0;
132
133 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
134 if ( ch == 27 && seen_esc == 0 ) {
135 seen_esc = 1;
136 continue;
137 } else {
138 if ( seen_esc==1 && ch == 'O' ) {
139 seen_esc = 2;
140 continue;
141 } else if ( seen_esc == 2 ) {
142 if ( ch == 'P' ) /* F1 */
143 show_state();
144 #ifdef CONFIG_MAGIC_SYSRQ
145 if ( ch == 'S' ) { /* F4 */
146 do
147 ch = ia64_ssc(0, 0, 0, 0,
148 SSC_GETCHAR);
149 while (!ch);
150 handle_sysrq(ch);
151 }
152 #endif
153 seen_esc = 0;
154 continue;
155 }
156 }
157 seen_esc = 0;
158
159 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
160 break;
161 }
162 tty_flip_buffer_push(tty);
163 }
164
165 /*
166 * This is the serial driver's interrupt routine for a single port
167 */
168 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
169 {
170 struct async_struct * info;
171
172 /*
173 * I don't know exactly why they don't use the dev_id opaque data
174 * pointer instead of this extra lookup table
175 */
176 info = IRQ_ports[irq];
177 if (!info || !info->tty) {
178 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
179 return IRQ_NONE;
180 }
181 /*
182 * pretty simple in our case, because we only get interrupts
183 * on inbound traffic
184 */
185 receive_chars(info->tty);
186 return IRQ_HANDLED;
187 }
188
189 /*
190 * -------------------------------------------------------------------
191 * Here ends the serial interrupt routines.
192 * -------------------------------------------------------------------
193 */
194
195 static void do_softint(struct work_struct *private_)
196 {
197 printk(KERN_ERR "simserial: do_softint called\n");
198 }
199
200 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
201 {
202 struct async_struct *info = (struct async_struct *)tty->driver_data;
203 unsigned long flags;
204
205 if (!tty || !info->xmit.buf)
206 return 0;
207
208 local_irq_save(flags);
209 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
210 local_irq_restore(flags);
211 return 0;
212 }
213 info->xmit.buf[info->xmit.head] = ch;
214 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
215 local_irq_restore(flags);
216 return 1;
217 }
218
219 static void transmit_chars(struct async_struct *info, int *intr_done)
220 {
221 int count;
222 unsigned long flags;
223
224
225 local_irq_save(flags);
226
227 if (info->x_char) {
228 char c = info->x_char;
229
230 console->write(console, &c, 1);
231
232 info->state->icount.tx++;
233 info->x_char = 0;
234
235 goto out;
236 }
237
238 if (info->xmit.head == info->xmit.tail || info->tty->stopped || info->tty->hw_stopped) {
239 #ifdef SIMSERIAL_DEBUG
240 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
241 info->xmit.head, info->xmit.tail, info->tty->stopped);
242 #endif
243 goto out;
244 }
245 /*
246 * We removed the loop and try to do it in to chunks. We need
247 * 2 operations maximum because it's a ring buffer.
248 *
249 * First from current to tail if possible.
250 * Then from the beginning of the buffer until necessary
251 */
252
253 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
254 SERIAL_XMIT_SIZE - info->xmit.tail);
255 console->write(console, info->xmit.buf+info->xmit.tail, count);
256
257 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
258
259 /*
260 * We have more at the beginning of the buffer
261 */
262 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
263 if (count) {
264 console->write(console, info->xmit.buf, count);
265 info->xmit.tail += count;
266 }
267 out:
268 local_irq_restore(flags);
269 }
270
271 static void rs_flush_chars(struct tty_struct *tty)
272 {
273 struct async_struct *info = (struct async_struct *)tty->driver_data;
274
275 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
276 !info->xmit.buf)
277 return;
278
279 transmit_chars(info, NULL);
280 }
281
282
283 static int rs_write(struct tty_struct * tty,
284 const unsigned char *buf, int count)
285 {
286 int c, ret = 0;
287 struct async_struct *info = (struct async_struct *)tty->driver_data;
288 unsigned long flags;
289
290 if (!tty || !info->xmit.buf || !tmp_buf) return 0;
291
292 local_irq_save(flags);
293 while (1) {
294 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
295 if (count < c)
296 c = count;
297 if (c <= 0) {
298 break;
299 }
300 memcpy(info->xmit.buf + info->xmit.head, buf, c);
301 info->xmit.head = ((info->xmit.head + c) &
302 (SERIAL_XMIT_SIZE-1));
303 buf += c;
304 count -= c;
305 ret += c;
306 }
307 local_irq_restore(flags);
308 /*
309 * Hey, we transmit directly from here in our case
310 */
311 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
312 && !tty->stopped && !tty->hw_stopped) {
313 transmit_chars(info, NULL);
314 }
315 return ret;
316 }
317
318 static int rs_write_room(struct tty_struct *tty)
319 {
320 struct async_struct *info = (struct async_struct *)tty->driver_data;
321
322 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
323 }
324
325 static int rs_chars_in_buffer(struct tty_struct *tty)
326 {
327 struct async_struct *info = (struct async_struct *)tty->driver_data;
328
329 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
330 }
331
332 static void rs_flush_buffer(struct tty_struct *tty)
333 {
334 struct async_struct *info = (struct async_struct *)tty->driver_data;
335 unsigned long flags;
336
337 local_irq_save(flags);
338 info->xmit.head = info->xmit.tail = 0;
339 local_irq_restore(flags);
340
341 tty_wakeup(tty);
342 }
343
344 /*
345 * This function is used to send a high-priority XON/XOFF character to
346 * the device
347 */
348 static void rs_send_xchar(struct tty_struct *tty, char ch)
349 {
350 struct async_struct *info = (struct async_struct *)tty->driver_data;
351
352 info->x_char = ch;
353 if (ch) {
354 /*
355 * I guess we could call console->write() directly but
356 * let's do that for now.
357 */
358 transmit_chars(info, NULL);
359 }
360 }
361
362 /*
363 * ------------------------------------------------------------
364 * rs_throttle()
365 *
366 * This routine is called by the upper-layer tty layer to signal that
367 * incoming characters should be throttled.
368 * ------------------------------------------------------------
369 */
370 static void rs_throttle(struct tty_struct * tty)
371 {
372 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
373
374 printk(KERN_INFO "simrs_throttle called\n");
375 }
376
377 static void rs_unthrottle(struct tty_struct * tty)
378 {
379 struct async_struct *info = (struct async_struct *)tty->driver_data;
380
381 if (I_IXOFF(tty)) {
382 if (info->x_char)
383 info->x_char = 0;
384 else
385 rs_send_xchar(tty, START_CHAR(tty));
386 }
387 printk(KERN_INFO "simrs_unthrottle called\n");
388 }
389
390
391 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
392 {
393 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
394 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
395 (cmd != TIOCMIWAIT)) {
396 if (tty->flags & (1 << TTY_IO_ERROR))
397 return -EIO;
398 }
399
400 switch (cmd) {
401 case TIOCGSERIAL:
402 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
403 return 0;
404 case TIOCSSERIAL:
405 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
406 return 0;
407 case TIOCSERCONFIG:
408 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
409 return -EINVAL;
410
411 case TIOCSERGETLSR: /* Get line status register */
412 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
413 return -EINVAL;
414
415 case TIOCSERGSTRUCT:
416 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
417 #if 0
418 if (copy_to_user((struct async_struct *) arg,
419 info, sizeof(struct async_struct)))
420 return -EFAULT;
421 #endif
422 return 0;
423
424 /*
425 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
426 * - mask passed in arg for lines of interest
427 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
428 * Caller should use TIOCGICOUNT to see which one it was
429 */
430 case TIOCMIWAIT:
431 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
432 return 0;
433 case TIOCSERGWILD:
434 case TIOCSERSWILD:
435 /* "setserial -W" is called in Debian boot */
436 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
437 return 0;
438
439 default:
440 return -ENOIOCTLCMD;
441 }
442 return 0;
443 }
444
445 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
446
447 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
448 {
449 /* Handle turning off CRTSCTS */
450 if ((old_termios->c_cflag & CRTSCTS) &&
451 !(tty->termios->c_cflag & CRTSCTS)) {
452 tty->hw_stopped = 0;
453 rs_start(tty);
454 }
455 }
456 /*
457 * This routine will shutdown a serial port; interrupts are disabled, and
458 * DTR is dropped if the hangup on close termio flag is on.
459 */
460 static void shutdown(struct async_struct * info)
461 {
462 unsigned long flags;
463 struct serial_state *state;
464 int retval;
465
466 if (!(info->flags & ASYNC_INITIALIZED)) return;
467
468 state = info->state;
469
470 #ifdef SIMSERIAL_DEBUG
471 printk("Shutting down serial port %d (irq %d)....", info->line,
472 state->irq);
473 #endif
474
475 local_irq_save(flags);
476 {
477 /*
478 * First unlink the serial port from the IRQ chain...
479 */
480 if (info->next_port)
481 info->next_port->prev_port = info->prev_port;
482 if (info->prev_port)
483 info->prev_port->next_port = info->next_port;
484 else
485 IRQ_ports[state->irq] = info->next_port;
486
487 /*
488 * Free the IRQ, if necessary
489 */
490 if (state->irq && (!IRQ_ports[state->irq] ||
491 !IRQ_ports[state->irq]->next_port)) {
492 if (IRQ_ports[state->irq]) {
493 free_irq(state->irq, NULL);
494 retval = request_irq(state->irq, rs_interrupt_single,
495 IRQ_T(info), "serial", NULL);
496
497 if (retval)
498 printk(KERN_ERR "serial shutdown: request_irq: error %d"
499 " Couldn't reacquire IRQ.\n", retval);
500 } else
501 free_irq(state->irq, NULL);
502 }
503
504 if (info->xmit.buf) {
505 free_page((unsigned long) info->xmit.buf);
506 info->xmit.buf = NULL;
507 }
508
509 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
510
511 info->flags &= ~ASYNC_INITIALIZED;
512 }
513 local_irq_restore(flags);
514 }
515
516 /*
517 * ------------------------------------------------------------
518 * rs_close()
519 *
520 * This routine is called when the serial port gets closed. First, we
521 * wait for the last remaining data to be sent. Then, we unlink its
522 * async structure from the interrupt chain if necessary, and we free
523 * that IRQ if nothing is left in the chain.
524 * ------------------------------------------------------------
525 */
526 static void rs_close(struct tty_struct *tty, struct file * filp)
527 {
528 struct async_struct * info = (struct async_struct *)tty->driver_data;
529 struct serial_state *state;
530 unsigned long flags;
531
532 if (!info ) return;
533
534 state = info->state;
535
536 local_irq_save(flags);
537 if (tty_hung_up_p(filp)) {
538 #ifdef SIMSERIAL_DEBUG
539 printk("rs_close: hung_up\n");
540 #endif
541 local_irq_restore(flags);
542 return;
543 }
544 #ifdef SIMSERIAL_DEBUG
545 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
546 #endif
547 if ((tty->count == 1) && (state->count != 1)) {
548 /*
549 * Uh, oh. tty->count is 1, which means that the tty
550 * structure will be freed. state->count should always
551 * be one in these conditions. If it's greater than
552 * one, we've got real problems, since it means the
553 * serial port won't be shutdown.
554 */
555 printk(KERN_ERR "rs_close: bad serial port count; tty->count is 1, "
556 "state->count is %d\n", state->count);
557 state->count = 1;
558 }
559 if (--state->count < 0) {
560 printk(KERN_ERR "rs_close: bad serial port count for ttys%d: %d\n",
561 info->line, state->count);
562 state->count = 0;
563 }
564 if (state->count) {
565 local_irq_restore(flags);
566 return;
567 }
568 info->flags |= ASYNC_CLOSING;
569 local_irq_restore(flags);
570
571 /*
572 * Now we wait for the transmit buffer to clear; and we notify
573 * the line discipline to only process XON/XOFF characters.
574 */
575 shutdown(info);
576 rs_flush_buffer(tty);
577 tty_ldisc_flush(tty);
578 info->event = 0;
579 info->tty = NULL;
580 if (info->blocked_open) {
581 if (info->close_delay)
582 schedule_timeout_interruptible(info->close_delay);
583 wake_up_interruptible(&info->open_wait);
584 }
585 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
586 wake_up_interruptible(&info->close_wait);
587 }
588
589 /*
590 * rs_wait_until_sent() --- wait until the transmitter is empty
591 */
592 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
593 {
594 }
595
596
597 /*
598 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
599 */
600 static void rs_hangup(struct tty_struct *tty)
601 {
602 struct async_struct * info = (struct async_struct *)tty->driver_data;
603 struct serial_state *state = info->state;
604
605 #ifdef SIMSERIAL_DEBUG
606 printk("rs_hangup: called\n");
607 #endif
608
609 state = info->state;
610
611 rs_flush_buffer(tty);
612 if (info->flags & ASYNC_CLOSING)
613 return;
614 shutdown(info);
615
616 info->event = 0;
617 state->count = 0;
618 info->flags &= ~ASYNC_NORMAL_ACTIVE;
619 info->tty = NULL;
620 wake_up_interruptible(&info->open_wait);
621 }
622
623
624 static int get_async_struct(int line, struct async_struct **ret_info)
625 {
626 struct async_struct *info;
627 struct serial_state *sstate;
628
629 sstate = rs_table + line;
630 sstate->count++;
631 if (sstate->info) {
632 *ret_info = sstate->info;
633 return 0;
634 }
635 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
636 if (!info) {
637 sstate->count--;
638 return -ENOMEM;
639 }
640 init_waitqueue_head(&info->open_wait);
641 init_waitqueue_head(&info->close_wait);
642 init_waitqueue_head(&info->delta_msr_wait);
643 info->magic = SERIAL_MAGIC;
644 info->port = sstate->port;
645 info->flags = sstate->flags;
646 info->xmit_fifo_size = sstate->xmit_fifo_size;
647 info->line = line;
648 INIT_WORK(&info->work, do_softint);
649 info->state = sstate;
650 if (sstate->info) {
651 kfree(info);
652 *ret_info = sstate->info;
653 return 0;
654 }
655 *ret_info = sstate->info = info;
656 return 0;
657 }
658
659 static int
660 startup(struct async_struct *info)
661 {
662 unsigned long flags;
663 int retval=0;
664 irq_handler_t handler;
665 struct serial_state *state= info->state;
666 unsigned long page;
667
668 page = get_zeroed_page(GFP_KERNEL);
669 if (!page)
670 return -ENOMEM;
671
672 local_irq_save(flags);
673
674 if (info->flags & ASYNC_INITIALIZED) {
675 free_page(page);
676 goto errout;
677 }
678
679 if (!state->port || !state->type) {
680 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
681 free_page(page);
682 goto errout;
683 }
684 if (info->xmit.buf)
685 free_page(page);
686 else
687 info->xmit.buf = (unsigned char *) page;
688
689 #ifdef SIMSERIAL_DEBUG
690 printk("startup: ttys%d (irq %d)...", info->line, state->irq);
691 #endif
692
693 /*
694 * Allocate the IRQ if necessary
695 */
696 if (state->irq && (!IRQ_ports[state->irq] ||
697 !IRQ_ports[state->irq]->next_port)) {
698 if (IRQ_ports[state->irq]) {
699 retval = -EBUSY;
700 goto errout;
701 } else
702 handler = rs_interrupt_single;
703
704 retval = request_irq(state->irq, handler, IRQ_T(info), "simserial", NULL);
705 if (retval) {
706 if (capable(CAP_SYS_ADMIN)) {
707 if (info->tty)
708 set_bit(TTY_IO_ERROR,
709 &info->tty->flags);
710 retval = 0;
711 }
712 goto errout;
713 }
714 }
715
716 /*
717 * Insert serial port into IRQ chain.
718 */
719 info->prev_port = NULL;
720 info->next_port = IRQ_ports[state->irq];
721 if (info->next_port)
722 info->next_port->prev_port = info;
723 IRQ_ports[state->irq] = info;
724
725 if (info->tty) clear_bit(TTY_IO_ERROR, &info->tty->flags);
726
727 info->xmit.head = info->xmit.tail = 0;
728
729 #if 0
730 /*
731 * Set up serial timers...
732 */
733 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
734 timer_active |= 1 << RS_TIMER;
735 #endif
736
737 /*
738 * Set up the tty->alt_speed kludge
739 */
740 if (info->tty) {
741 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
742 info->tty->alt_speed = 57600;
743 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
744 info->tty->alt_speed = 115200;
745 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
746 info->tty->alt_speed = 230400;
747 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
748 info->tty->alt_speed = 460800;
749 }
750
751 info->flags |= ASYNC_INITIALIZED;
752 local_irq_restore(flags);
753 return 0;
754
755 errout:
756 local_irq_restore(flags);
757 return retval;
758 }
759
760
761 /*
762 * This routine is called whenever a serial port is opened. It
763 * enables interrupts for a serial port, linking in its async structure into
764 * the IRQ chain. It also performs the serial-specific
765 * initialization for the tty structure.
766 */
767 static int rs_open(struct tty_struct *tty, struct file * filp)
768 {
769 struct async_struct *info;
770 int retval, line;
771 unsigned long page;
772
773 line = tty->index;
774 if ((line < 0) || (line >= NR_PORTS))
775 return -ENODEV;
776 retval = get_async_struct(line, &info);
777 if (retval)
778 return retval;
779 tty->driver_data = info;
780 info->tty = tty;
781
782 #ifdef SIMSERIAL_DEBUG
783 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
784 #endif
785 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
786
787 if (!tmp_buf) {
788 page = get_zeroed_page(GFP_KERNEL);
789 if (!page)
790 return -ENOMEM;
791 if (tmp_buf)
792 free_page(page);
793 else
794 tmp_buf = (unsigned char *) page;
795 }
796
797 /*
798 * If the port is the middle of closing, bail out now
799 */
800 if (tty_hung_up_p(filp) ||
801 (info->flags & ASYNC_CLOSING)) {
802 if (info->flags & ASYNC_CLOSING)
803 interruptible_sleep_on(&info->close_wait);
804 #ifdef SERIAL_DO_RESTART
805 return ((info->flags & ASYNC_HUP_NOTIFY) ?
806 -EAGAIN : -ERESTARTSYS);
807 #else
808 return -EAGAIN;
809 #endif
810 }
811
812 /*
813 * Start up serial port
814 */
815 retval = startup(info);
816 if (retval) {
817 return retval;
818 }
819
820 /*
821 * figure out which console to use (should be one already)
822 */
823 console = console_drivers;
824 while (console) {
825 if ((console->flags & CON_ENABLED) && console->write) break;
826 console = console->next;
827 }
828
829 #ifdef SIMSERIAL_DEBUG
830 printk("rs_open ttys%d successful\n", info->line);
831 #endif
832 return 0;
833 }
834
835 /*
836 * /proc fs routines....
837 */
838
839 static inline void line_info(struct seq_file *m, struct serial_state *state)
840 {
841 seq_printf(m, "%d: uart:%s port:%lX irq:%d\n",
842 state->line, uart_config[state->type].name,
843 state->port, state->irq);
844 }
845
846 static int rs_proc_show(struct seq_file *m, void *v)
847 {
848 int i;
849
850 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
851 for (i = 0; i < NR_PORTS; i++)
852 line_info(m, &rs_table[i]);
853 return 0;
854 }
855
856 static int rs_proc_open(struct inode *inode, struct file *file)
857 {
858 return single_open(file, rs_proc_show, NULL);
859 }
860
861 static const struct file_operations rs_proc_fops = {
862 .owner = THIS_MODULE,
863 .open = rs_proc_open,
864 .read = seq_read,
865 .llseek = seq_lseek,
866 .release = single_release,
867 };
868
869 /*
870 * ---------------------------------------------------------------------
871 * rs_init() and friends
872 *
873 * rs_init() is called at boot-time to initialize the serial driver.
874 * ---------------------------------------------------------------------
875 */
876
877 /*
878 * This routine prints out the appropriate serial driver version
879 * number, and identifies which options were configured into this
880 * driver.
881 */
882 static inline void show_serial_version(void)
883 {
884 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
885 printk(KERN_INFO " no serial options enabled\n");
886 }
887
888 static const struct tty_operations hp_ops = {
889 .open = rs_open,
890 .close = rs_close,
891 .write = rs_write,
892 .put_char = rs_put_char,
893 .flush_chars = rs_flush_chars,
894 .write_room = rs_write_room,
895 .chars_in_buffer = rs_chars_in_buffer,
896 .flush_buffer = rs_flush_buffer,
897 .ioctl = rs_ioctl,
898 .throttle = rs_throttle,
899 .unthrottle = rs_unthrottle,
900 .send_xchar = rs_send_xchar,
901 .set_termios = rs_set_termios,
902 .stop = rs_stop,
903 .start = rs_start,
904 .hangup = rs_hangup,
905 .wait_until_sent = rs_wait_until_sent,
906 .proc_fops = &rs_proc_fops,
907 };
908
909 /*
910 * The serial driver boot-time initialization code!
911 */
912 static int __init
913 simrs_init (void)
914 {
915 int i, rc;
916 struct serial_state *state;
917
918 if (!ia64_platform_is("hpsim"))
919 return -ENODEV;
920
921 hp_simserial_driver = alloc_tty_driver(1);
922 if (!hp_simserial_driver)
923 return -ENOMEM;
924
925 show_serial_version();
926
927 /* Initialize the tty_driver structure */
928
929 hp_simserial_driver->owner = THIS_MODULE;
930 hp_simserial_driver->driver_name = "simserial";
931 hp_simserial_driver->name = "ttyS";
932 hp_simserial_driver->major = TTY_MAJOR;
933 hp_simserial_driver->minor_start = 64;
934 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
935 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
936 hp_simserial_driver->init_termios = tty_std_termios;
937 hp_simserial_driver->init_termios.c_cflag =
938 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
939 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
940 tty_set_operations(hp_simserial_driver, &hp_ops);
941
942 /*
943 * Let's have a little bit of fun !
944 */
945 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
946
947 if (state->type == PORT_UNKNOWN) continue;
948
949 if (!state->irq) {
950 if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
951 panic("%s: out of interrupt vectors!\n",
952 __func__);
953 state->irq = rc;
954 ia64_ssc_connect_irq(KEYBOARD_INTR, state->irq);
955 }
956
957 printk(KERN_INFO "ttyS%d at 0x%04lx (irq = %d) is a %s\n",
958 state->line,
959 state->port, state->irq,
960 uart_config[state->type].name);
961 }
962
963 if (tty_register_driver(hp_simserial_driver))
964 panic("Couldn't register simserial driver\n");
965
966 return 0;
967 }
968
969 #ifndef MODULE
970 __initcall(simrs_init);
971 #endif
This page took 0.070047 seconds and 4 git commands to generate.