1 // SPDX-License-Identifier: GPL-2.0
3 * Simulated Serial Driver (fake serial)
5 * This driver is mostly used for bringup purposes and will go away.
6 * It has a strong dependency on the system console. All outputs
7 * are rerouted to the same facility as the one used by printk which, in our
8 * case means sys_sim.c console (goes via the simulator).
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>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/sched.h>
18 #include <linux/sched/debug.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/major.h>
22 #include <linux/fcntl.h>
24 #include <linux/seq_file.h>
25 #include <linux/slab.h>
26 #include <linux/capability.h>
27 #include <linux/circ_buf.h>
28 #include <linux/console.h>
29 #include <linux/irq.h>
30 #include <linux/module.h>
31 #include <linux/serial.h>
32 #include <linux/sysrq.h>
33 #include <linux/uaccess.h>
35 #include <asm/hpsim.h>
37 #include "hpsim_ssc.h"
39 #undef SIMSERIAL_DEBUG /* define this to get some debug information */
41 #define KEYBOARD_INTR 3 /* must match with simulator! */
43 #define NR_PORTS 1 /* only one port for now */
52 static struct serial_state rs_table
[NR_PORTS
];
54 struct tty_driver
*hp_simserial_driver
;
56 static struct console
*console
;
58 static void receive_chars(struct tty_port
*port
)
61 static unsigned char seen_esc
= 0;
63 while ( (ch
= ia64_ssc(0, 0, 0, 0, SSC_GETCHAR
)) ) {
64 if (ch
== 27 && seen_esc
== 0) {
67 } else if (seen_esc
== 1 && ch
== 'O') {
70 } else if (seen_esc
== 2) {
71 if (ch
== 'P') /* F1 */
73 #ifdef CONFIG_MAGIC_SYSRQ
74 if (ch
== 'S') { /* F4 */
76 ch
= ia64_ssc(0, 0, 0, 0, SSC_GETCHAR
);
86 if (tty_insert_flip_char(port
, ch
, TTY_NORMAL
) == 0)
89 tty_flip_buffer_push(port
);
93 * This is the serial driver's interrupt routine for a single port
95 static irqreturn_t
rs_interrupt_single(int irq
, void *dev_id
)
97 struct serial_state
*info
= dev_id
;
99 receive_chars(&info
->port
);
105 * -------------------------------------------------------------------
106 * Here ends the serial interrupt routines.
107 * -------------------------------------------------------------------
110 static int rs_put_char(struct tty_struct
*tty
, unsigned char ch
)
112 struct serial_state
*info
= tty
->driver_data
;
118 local_irq_save(flags
);
119 if (CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
) == 0) {
120 local_irq_restore(flags
);
123 info
->xmit
.buf
[info
->xmit
.head
] = ch
;
124 info
->xmit
.head
= (info
->xmit
.head
+ 1) & (SERIAL_XMIT_SIZE
-1);
125 local_irq_restore(flags
);
129 static void transmit_chars(struct tty_struct
*tty
, struct serial_state
*info
,
135 local_irq_save(flags
);
138 char c
= info
->x_char
;
140 console
->write(console
, &c
, 1);
147 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
) {
148 #ifdef SIMSERIAL_DEBUG
149 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
150 info
->xmit
.head
, info
->xmit
.tail
, tty
->stopped
);
155 * We removed the loop and try to do it in to chunks. We need
156 * 2 operations maximum because it's a ring buffer.
158 * First from current to tail if possible.
159 * Then from the beginning of the buffer until necessary
162 count
= min(CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
),
163 SERIAL_XMIT_SIZE
- info
->xmit
.tail
);
164 console
->write(console
, info
->xmit
.buf
+info
->xmit
.tail
, count
);
166 info
->xmit
.tail
= (info
->xmit
.tail
+count
) & (SERIAL_XMIT_SIZE
-1);
169 * We have more at the beginning of the buffer
171 count
= CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
173 console
->write(console
, info
->xmit
.buf
, count
);
174 info
->xmit
.tail
+= count
;
177 local_irq_restore(flags
);
180 static void rs_flush_chars(struct tty_struct
*tty
)
182 struct serial_state
*info
= tty
->driver_data
;
184 if (info
->xmit
.head
== info
->xmit
.tail
|| tty
->stopped
||
188 transmit_chars(tty
, info
, NULL
);
191 static int rs_write(struct tty_struct
* tty
,
192 const unsigned char *buf
, int count
)
194 struct serial_state
*info
= tty
->driver_data
;
201 local_irq_save(flags
);
203 c
= CIRC_SPACE_TO_END(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
209 memcpy(info
->xmit
.buf
+ info
->xmit
.head
, buf
, c
);
210 info
->xmit
.head
= ((info
->xmit
.head
+ c
) &
211 (SERIAL_XMIT_SIZE
-1));
216 local_irq_restore(flags
);
218 * Hey, we transmit directly from here in our case
220 if (CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
) &&
222 transmit_chars(tty
, info
, NULL
);
227 static int rs_write_room(struct tty_struct
*tty
)
229 struct serial_state
*info
= tty
->driver_data
;
231 return CIRC_SPACE(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
234 static int rs_chars_in_buffer(struct tty_struct
*tty
)
236 struct serial_state
*info
= tty
->driver_data
;
238 return CIRC_CNT(info
->xmit
.head
, info
->xmit
.tail
, SERIAL_XMIT_SIZE
);
241 static void rs_flush_buffer(struct tty_struct
*tty
)
243 struct serial_state
*info
= tty
->driver_data
;
246 local_irq_save(flags
);
247 info
->xmit
.head
= info
->xmit
.tail
= 0;
248 local_irq_restore(flags
);
254 * This function is used to send a high-priority XON/XOFF character to
257 static void rs_send_xchar(struct tty_struct
*tty
, char ch
)
259 struct serial_state
*info
= tty
->driver_data
;
264 * I guess we could call console->write() directly but
265 * let's do that for now.
267 transmit_chars(tty
, info
, NULL
);
272 * ------------------------------------------------------------
275 * This routine is called by the upper-layer tty layer to signal that
276 * incoming characters should be throttled.
277 * ------------------------------------------------------------
279 static void rs_throttle(struct tty_struct
* tty
)
282 rs_send_xchar(tty
, STOP_CHAR(tty
));
284 printk(KERN_INFO
"simrs_throttle called\n");
287 static void rs_unthrottle(struct tty_struct
* tty
)
289 struct serial_state
*info
= tty
->driver_data
;
295 rs_send_xchar(tty
, START_CHAR(tty
));
297 printk(KERN_INFO
"simrs_unthrottle called\n");
300 static int rs_ioctl(struct tty_struct
*tty
, unsigned int cmd
, unsigned long arg
)
302 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
303 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGSTRUCT
) &&
304 (cmd
!= TIOCMIWAIT
)) {
305 if (tty_io_error(tty
))
316 case TIOCSERGETLSR
: /* Get line status register */
320 /* "setserial -W" is called in Debian boot */
321 printk (KERN_INFO
"TIOCSER?WILD ioctl obsolete, ignored.\n");
327 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
330 * This routine will shutdown a serial port; interrupts are disabled, and
331 * DTR is dropped if the hangup on close termio flag is on.
333 static void shutdown(struct tty_port
*port
)
335 struct serial_state
*info
= container_of(port
, struct serial_state
,
339 local_irq_save(flags
);
341 free_irq(info
->irq
, info
);
343 if (info
->xmit
.buf
) {
344 free_page((unsigned long) info
->xmit
.buf
);
345 info
->xmit
.buf
= NULL
;
347 local_irq_restore(flags
);
350 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
352 struct serial_state
*info
= tty
->driver_data
;
354 tty_port_close(&info
->port
, tty
, filp
);
357 static void rs_hangup(struct tty_struct
*tty
)
359 struct serial_state
*info
= tty
->driver_data
;
361 rs_flush_buffer(tty
);
362 tty_port_hangup(&info
->port
);
365 static int activate(struct tty_port
*port
, struct tty_struct
*tty
)
367 struct serial_state
*state
= container_of(port
, struct serial_state
,
369 unsigned long flags
, page
;
372 page
= get_zeroed_page(GFP_KERNEL
);
376 local_irq_save(flags
);
381 state
->xmit
.buf
= (unsigned char *) page
;
384 retval
= request_irq(state
->irq
, rs_interrupt_single
, 0,
390 state
->xmit
.head
= state
->xmit
.tail
= 0;
392 local_irq_restore(flags
);
398 * This routine is called whenever a serial port is opened. It
399 * enables interrupts for a serial port, linking in its async structure into
400 * the IRQ chain. It also performs the serial-specific
401 * initialization for the tty structure.
403 static int rs_open(struct tty_struct
*tty
, struct file
* filp
)
405 struct serial_state
*info
= rs_table
+ tty
->index
;
406 struct tty_port
*port
= &info
->port
;
408 tty
->driver_data
= info
;
409 port
->low_latency
= (port
->flags
& ASYNC_LOW_LATENCY
) ? 1 : 0;
412 * figure out which console to use (should be one already)
414 console
= console_drivers
;
416 if ((console
->flags
& CON_ENABLED
) && console
->write
) break;
417 console
= console
->next
;
420 return tty_port_open(port
, tty
, filp
);
424 * /proc fs routines....
427 static int rs_proc_show(struct seq_file
*m
, void *v
)
431 seq_printf(m
, "simserinfo:1.0\n");
432 for (i
= 0; i
< NR_PORTS
; i
++)
433 seq_printf(m
, "%d: uart:16550 port:3F8 irq:%d\n",
438 static int rs_proc_open(struct inode
*inode
, struct file
*file
)
440 return single_open(file
, rs_proc_show
, NULL
);
443 static const struct file_operations rs_proc_fops
= {
444 .owner
= THIS_MODULE
,
445 .open
= rs_proc_open
,
448 .release
= single_release
,
451 static const struct tty_operations hp_ops
= {
455 .put_char
= rs_put_char
,
456 .flush_chars
= rs_flush_chars
,
457 .write_room
= rs_write_room
,
458 .chars_in_buffer
= rs_chars_in_buffer
,
459 .flush_buffer
= rs_flush_buffer
,
461 .throttle
= rs_throttle
,
462 .unthrottle
= rs_unthrottle
,
463 .send_xchar
= rs_send_xchar
,
465 .proc_fops
= &rs_proc_fops
,
468 static const struct tty_port_operations hp_port_ops
= {
469 .activate
= activate
,
470 .shutdown
= shutdown
,
473 static int __init
simrs_init(void)
475 struct serial_state
*state
;
478 if (!ia64_platform_is("hpsim"))
481 hp_simserial_driver
= alloc_tty_driver(NR_PORTS
);
482 if (!hp_simserial_driver
)
485 printk(KERN_INFO
"SimSerial driver with no serial options enabled\n");
487 /* Initialize the tty_driver structure */
489 hp_simserial_driver
->driver_name
= "simserial";
490 hp_simserial_driver
->name
= "ttyS";
491 hp_simserial_driver
->major
= TTY_MAJOR
;
492 hp_simserial_driver
->minor_start
= 64;
493 hp_simserial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
494 hp_simserial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
495 hp_simserial_driver
->init_termios
= tty_std_termios
;
496 hp_simserial_driver
->init_termios
.c_cflag
=
497 B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
498 hp_simserial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
499 tty_set_operations(hp_simserial_driver
, &hp_ops
);
502 tty_port_init(&state
->port
);
503 state
->port
.ops
= &hp_port_ops
;
504 state
->port
.close_delay
= 0; /* XXX really 0? */
506 retval
= hpsim_get_irq(KEYBOARD_INTR
);
508 printk(KERN_ERR
"%s: out of interrupt vectors!\n",
515 /* the port is imaginary */
516 printk(KERN_INFO
"ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state
->irq
);
518 tty_port_link_device(&state
->port
, hp_simserial_driver
, 0);
519 retval
= tty_register_driver(hp_simserial_driver
);
521 printk(KERN_ERR
"Couldn't register simserial driver\n");
527 put_tty_driver(hp_simserial_driver
);
528 tty_port_destroy(&state
->port
);
533 __initcall(simrs_init
);