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