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