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