]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - arch/ia64/hp/sim/simserial.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[thirdparty/kernel/linux.git] / arch / ia64 / hp / sim / simserial.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * Simulated Serial Driver (fake serial)
4 *
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
adb636fe 8 * case means sys_sim.c console (goes via the simulator).
1da177e4
LT
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>
1da177e4
LT
13 */
14
1da177e4
LT
15#include <linux/init.h>
16#include <linux/errno.h>
17#include <linux/sched.h>
b17b0153 18#include <linux/sched/debug.h>
1da177e4
LT
19#include <linux/tty.h>
20#include <linux/tty_flip.h>
21#include <linux/major.h>
22#include <linux/fcntl.h>
23#include <linux/mm.h>
bf54215e 24#include <linux/seq_file.h>
1da177e4 25#include <linux/slab.h>
a9415644 26#include <linux/capability.h>
3c4782dc 27#include <linux/circ_buf.h>
1da177e4 28#include <linux/console.h>
8b916330 29#include <linux/irq.h>
1da177e4
LT
30#include <linux/module.h>
31#include <linux/serial.h>
819c67e6 32#include <linux/sysrq.h>
8b916330 33#include <linux/uaccess.h>
1da177e4 34
035cfe5a 35#include <asm/hpsim.h>
1da177e4 36
035cfe5a
JS
37#include "hpsim_ssc.h"
38
1da177e4
LT
39#undef SIMSERIAL_DEBUG /* define this to get some debug information */
40
41#define KEYBOARD_INTR 3 /* must match with simulator! */
42
43#define NR_PORTS 1 /* only one port for now */
1da177e4 44
3c4782dc 45struct serial_state {
7f32f8dd 46 struct tty_port port;
3c4782dc
JS
47 struct circ_buf xmit;
48 int irq;
49 int x_char;
50};
51
fd2d7a6e 52static struct serial_state rs_table[NR_PORTS];
1da177e4
LT
53
54struct tty_driver *hp_simserial_driver;
55
1da177e4
LT
56static struct console *console;
57
2e124b4a 58static void receive_chars(struct tty_port *port)
1da177e4
LT
59{
60 unsigned char ch;
61 static unsigned char seen_esc = 0;
62
63 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
f66279cd 64 if (ch == 27 && seen_esc == 0) {
1da177e4
LT
65 seen_esc = 1;
66 continue;
f66279cd
JS
67 } else if (seen_esc == 1 && ch == 'O') {
68 seen_esc = 2;
69 continue;
70 } else if (seen_esc == 2) {
71 if (ch == 'P') /* F1 */
72 show_state();
819c67e6 73#ifdef CONFIG_MAGIC_SYSRQ
f66279cd
JS
74 if (ch == 'S') { /* F4 */
75 do {
76 ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR);
77 } while (!ch);
78 handle_sysrq(ch);
1da177e4 79 }
f66279cd
JS
80#endif
81 seen_esc = 0;
82 continue;
1da177e4
LT
83 }
84 seen_esc = 0;
1da177e4 85
92a19f9c 86 if (tty_insert_flip_char(port, ch, TTY_NORMAL) == 0)
d50f5c5c 87 break;
1da177e4 88 }
2e124b4a 89 tty_flip_buffer_push(port);
1da177e4
LT
90}
91
92/*
93 * This is the serial driver's interrupt routine for a single port
94 */
5dcded1b 95static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
1da177e4 96{
916b7656 97 struct serial_state *info = dev_id;
1da177e4 98
2e124b4a
JS
99 receive_chars(&info->port);
100
1da177e4
LT
101 return IRQ_HANDLED;
102}
103
104/*
105 * -------------------------------------------------------------------
106 * Here ends the serial interrupt routines.
107 * -------------------------------------------------------------------
108 */
109
f34d7a5b 110static int rs_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4 111{
916b7656 112 struct serial_state *info = tty->driver_data;
1da177e4
LT
113 unsigned long flags;
114
6e9ebcfa 115 if (!info->xmit.buf)
f34d7a5b 116 return 0;
1da177e4
LT
117
118 local_irq_save(flags);
119 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
120 local_irq_restore(flags);
f34d7a5b 121 return 0;
1da177e4
LT
122 }
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);
f34d7a5b 126 return 1;
1da177e4
LT
127}
128
5e99d545
JS
129static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
130 int *intr_done)
1da177e4
LT
131{
132 int count;
133 unsigned long flags;
134
1da177e4
LT
135 local_irq_save(flags);
136
137 if (info->x_char) {
138 char c = info->x_char;
139
140 console->write(console, &c, 1);
141
1da177e4
LT
142 info->x_char = 0;
143
144 goto out;
145 }
146
ee797069 147 if (info->xmit.head == info->xmit.tail || tty->stopped) {
1da177e4
LT
148#ifdef SIMSERIAL_DEBUG
149 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
5e99d545 150 info->xmit.head, info->xmit.tail, tty->stopped);
1da177e4
LT
151#endif
152 goto out;
153 }
154 /*
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.
157 *
158 * First from current to tail if possible.
159 * Then from the beginning of the buffer until necessary
160 */
161
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);
165
166 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
167
168 /*
169 * We have more at the beginning of the buffer
170 */
171 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
172 if (count) {
173 console->write(console, info->xmit.buf, count);
174 info->xmit.tail += count;
175 }
176out:
177 local_irq_restore(flags);
178}
179
180static void rs_flush_chars(struct tty_struct *tty)
181{
916b7656 182 struct serial_state *info = tty->driver_data;
1da177e4 183
f66279cd 184 if (info->xmit.head == info->xmit.tail || tty->stopped ||
ee797069 185 !info->xmit.buf)
1da177e4
LT
186 return;
187
5e99d545 188 transmit_chars(tty, info, NULL);
1da177e4
LT
189}
190
1da177e4
LT
191static int rs_write(struct tty_struct * tty,
192 const unsigned char *buf, int count)
193{
916b7656 194 struct serial_state *info = tty->driver_data;
1da177e4 195 int c, ret = 0;
1da177e4
LT
196 unsigned long flags;
197
6e9ebcfa 198 if (!info->xmit.buf)
d88405d4 199 return 0;
1da177e4
LT
200
201 local_irq_save(flags);
202 while (1) {
203 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
204 if (count < c)
205 c = count;
206 if (c <= 0) {
207 break;
208 }
209 memcpy(info->xmit.buf + info->xmit.head, buf, c);
210 info->xmit.head = ((info->xmit.head + c) &
211 (SERIAL_XMIT_SIZE-1));
212 buf += c;
213 count -= c;
214 ret += c;
215 }
216 local_irq_restore(flags);
217 /*
218 * Hey, we transmit directly from here in our case
219 */
f66279cd 220 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
ee797069 221 !tty->stopped)
5e99d545 222 transmit_chars(tty, info, NULL);
f66279cd 223
1da177e4
LT
224 return ret;
225}
226
227static int rs_write_room(struct tty_struct *tty)
228{
916b7656 229 struct serial_state *info = tty->driver_data;
1da177e4
LT
230
231 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
232}
233
234static int rs_chars_in_buffer(struct tty_struct *tty)
235{
916b7656 236 struct serial_state *info = tty->driver_data;
1da177e4
LT
237
238 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
239}
240
241static void rs_flush_buffer(struct tty_struct *tty)
242{
916b7656 243 struct serial_state *info = tty->driver_data;
1da177e4
LT
244 unsigned long flags;
245
246 local_irq_save(flags);
247 info->xmit.head = info->xmit.tail = 0;
248 local_irq_restore(flags);
249
15648f15 250 tty_wakeup(tty);
1da177e4
LT
251}
252
253/*
254 * This function is used to send a high-priority XON/XOFF character to
255 * the device
256 */
257static void rs_send_xchar(struct tty_struct *tty, char ch)
258{
916b7656 259 struct serial_state *info = tty->driver_data;
1da177e4
LT
260
261 info->x_char = ch;
262 if (ch) {
263 /*
264 * I guess we could call console->write() directly but
265 * let's do that for now.
266 */
5e99d545 267 transmit_chars(tty, info, NULL);
1da177e4
LT
268 }
269}
270
271/*
272 * ------------------------------------------------------------
273 * rs_throttle()
274 *
275 * This routine is called by the upper-layer tty layer to signal that
276 * incoming characters should be throttled.
277 * ------------------------------------------------------------
278 */
279static void rs_throttle(struct tty_struct * tty)
280{
f66279cd
JS
281 if (I_IXOFF(tty))
282 rs_send_xchar(tty, STOP_CHAR(tty));
1da177e4
LT
283
284 printk(KERN_INFO "simrs_throttle called\n");
285}
286
287static void rs_unthrottle(struct tty_struct * tty)
288{
916b7656 289 struct serial_state *info = tty->driver_data;
1da177e4
LT
290
291 if (I_IXOFF(tty)) {
292 if (info->x_char)
293 info->x_char = 0;
294 else
295 rs_send_xchar(tty, START_CHAR(tty));
296 }
297 printk(KERN_INFO "simrs_unthrottle called\n");
298}
299
10e82f6c 300static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
1da177e4
LT
301{
302 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
303 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
0587102c 304 (cmd != TIOCMIWAIT)) {
18900ca6 305 if (tty_io_error(tty))
1da177e4
LT
306 return -EIO;
307 }
308
309 switch (cmd) {
f66279cd
JS
310 case TIOCGSERIAL:
311 case TIOCSSERIAL:
312 case TIOCSERGSTRUCT:
313 case TIOCMIWAIT:
314 return 0;
315 case TIOCSERCONFIG:
316 case TIOCSERGETLSR: /* Get line status register */
317 return -EINVAL;
318 case TIOCSERGWILD:
319 case TIOCSERSWILD:
320 /* "setserial -W" is called in Debian boot */
321 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
322 return 0;
323 }
324 return -ENOIOCTLCMD;
1da177e4
LT
325}
326
327#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
328
1da177e4
LT
329/*
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.
332 */
458cd31a 333static void shutdown(struct tty_port *port)
1da177e4 334{
458cd31a
JS
335 struct serial_state *info = container_of(port, struct serial_state,
336 port);
337 unsigned long flags;
1da177e4
LT
338
339 local_irq_save(flags);
f66279cd
JS
340 if (info->irq)
341 free_irq(info->irq, info);
1da177e4 342
f66279cd
JS
343 if (info->xmit.buf) {
344 free_page((unsigned long) info->xmit.buf);
345 info->xmit.buf = NULL;
1da177e4
LT
346 }
347 local_irq_restore(flags);
348}
349
1da177e4
LT
350static void rs_close(struct tty_struct *tty, struct file * filp)
351{
916b7656 352 struct serial_state *info = tty->driver_data;
37343030 353
458cd31a 354 tty_port_close(&info->port, tty, filp);
1da177e4
LT
355}
356
1da177e4
LT
357static void rs_hangup(struct tty_struct *tty)
358{
916b7656 359 struct serial_state *info = tty->driver_data;
1da177e4 360
1da177e4 361 rs_flush_buffer(tty);
458cd31a 362 tty_port_hangup(&info->port);
1da177e4
LT
363}
364
9aead90a 365static int activate(struct tty_port *port, struct tty_struct *tty)
1da177e4 366{
9aead90a
JS
367 struct serial_state *state = container_of(port, struct serial_state,
368 port);
f66279cd
JS
369 unsigned long flags, page;
370 int retval = 0;
1da177e4
LT
371
372 page = get_zeroed_page(GFP_KERNEL);
373 if (!page)
374 return -ENOMEM;
375
376 local_irq_save(flags);
377
916b7656 378 if (state->xmit.buf)
1da177e4
LT
379 free_page(page);
380 else
916b7656 381 state->xmit.buf = (unsigned char *) page;
1da177e4 382
964105b5 383 if (state->irq) {
2f8c521a 384 retval = request_irq(state->irq, rs_interrupt_single, 0,
916b7656 385 "simserial", state);
9e12dd5f 386 if (retval)
1da177e4 387 goto errout;
1da177e4
LT
388 }
389
916b7656 390 state->xmit.head = state->xmit.tail = 0;
1da177e4
LT
391errout:
392 local_irq_restore(flags);
393 return retval;
394}
395
396
397/*
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.
402 */
403static int rs_open(struct tty_struct *tty, struct file * filp)
404{
916b7656 405 struct serial_state *info = rs_table + tty->index;
7f32f8dd 406 struct tty_port *port = &info->port;
1da177e4 407
916b7656 408 tty->driver_data = info;
d6c53c0e 409 port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1da177e4 410
1da177e4
LT
411 /*
412 * figure out which console to use (should be one already)
413 */
414 console = console_drivers;
415 while (console) {
416 if ((console->flags & CON_ENABLED) && console->write) break;
417 console = console->next;
418 }
419
9aead90a 420 return tty_port_open(port, tty, filp);
1da177e4
LT
421}
422
423/*
424 * /proc fs routines....
425 */
426
bf54215e 427static int rs_proc_show(struct seq_file *m, void *v)
1da177e4 428{
bf54215e
AD
429 int i;
430
6e9ebcfa 431 seq_printf(m, "simserinfo:1.0\n");
bf54215e 432 for (i = 0; i < NR_PORTS; i++)
98e3a9e6
JS
433 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
434 i, rs_table[i].irq);
bf54215e 435 return 0;
1da177e4
LT
436}
437
bf54215e
AD
438static int rs_proc_open(struct inode *inode, struct file *file)
439{
440 return single_open(file, rs_proc_show, NULL);
441}
442
443static const struct file_operations rs_proc_fops = {
444 .owner = THIS_MODULE,
445 .open = rs_proc_open,
446 .read = seq_read,
447 .llseek = seq_lseek,
448 .release = single_release,
449};
450
b68e31d0 451static const struct tty_operations hp_ops = {
1da177e4
LT
452 .open = rs_open,
453 .close = rs_close,
454 .write = rs_write,
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,
460 .ioctl = rs_ioctl,
461 .throttle = rs_throttle,
462 .unthrottle = rs_unthrottle,
463 .send_xchar = rs_send_xchar,
1da177e4 464 .hangup = rs_hangup,
bf54215e 465 .proc_fops = &rs_proc_fops,
1da177e4
LT
466};
467
37343030 468static const struct tty_port_operations hp_port_ops = {
9aead90a 469 .activate = activate,
458cd31a 470 .shutdown = shutdown,
37343030
JS
471};
472
fd2d7a6e 473static int __init simrs_init(void)
1da177e4 474{
fd2d7a6e
JS
475 struct serial_state *state;
476 int retval;
1da177e4
LT
477
478 if (!ia64_platform_is("hpsim"))
479 return -ENODEV;
480
410235fd 481 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
1da177e4
LT
482 if (!hp_simserial_driver)
483 return -ENOMEM;
484
6e9ebcfa 485 printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
1da177e4
LT
486
487 /* Initialize the tty_driver structure */
488
1da177e4
LT
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);
500
fd2d7a6e 501 state = rs_table;
7f32f8dd 502 tty_port_init(&state->port);
37343030 503 state->port.ops = &hp_port_ops;
7f32f8dd 504 state->port.close_delay = 0; /* XXX really 0? */
fd2d7a6e
JS
505
506 retval = hpsim_get_irq(KEYBOARD_INTR);
507 if (retval < 0) {
508 printk(KERN_ERR "%s: out of interrupt vectors!\n",
509 __func__);
510 goto err_free_tty;
511 }
1da177e4 512
fd2d7a6e 513 state->irq = retval;
1da177e4 514
fd2d7a6e 515 /* the port is imaginary */
98e3a9e6 516 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
1da177e4 517
b19e2ca7 518 tty_port_link_device(&state->port, hp_simserial_driver, 0);
fd2d7a6e
JS
519 retval = tty_register_driver(hp_simserial_driver);
520 if (retval) {
521 printk(KERN_ERR "Couldn't register simserial driver\n");
522 goto err_free_tty;
1da177e4
LT
523 }
524
1da177e4 525 return 0;
fd2d7a6e
JS
526err_free_tty:
527 put_tty_driver(hp_simserial_driver);
191c5f10 528 tty_port_destroy(&state->port);
fd2d7a6e 529 return retval;
1da177e4
LT
530}
531
532#ifndef MODULE
533__initcall(simrs_init);
534#endif