blob: 516ad09f31314afc384466edbe3cb9efef82bfa8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Jiri Slabyadb636f2012-03-05 14:52:39 +01007 * case means sys_sim.c console (goes via the simulator).
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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>
Alexey Dobriyanbf542152009-03-31 15:19:23 -070022#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
Randy Dunlapa9415642006-01-11 12:17:48 -080024#include <linux/capability.h>
Jiri Slaby3c4782d2012-03-05 14:52:31 +010025#include <linux/circ_buf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/console.h>
Jiri Slaby8b916332012-03-05 14:52:40 +010027#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/serial.h>
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070030#include <linux/sysrq.h>
Jiri Slaby8b916332012-03-05 14:52:40 +010031#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jiri Slaby035cfe52012-03-08 21:01:17 +010033#include <asm/hpsim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jiri Slaby035cfe52012-03-08 21:01:17 +010035#include "hpsim_ssc.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jiri Slaby3c4782d2012-03-05 14:52:31 +010043struct serial_state {
Jiri Slaby7f32f8d2012-03-05 14:52:32 +010044 struct tty_port port;
Jiri Slaby3c4782d2012-03-05 14:52:31 +010045 struct circ_buf xmit;
46 int irq;
47 int x_char;
48};
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static char *serial_name = "SimSerial driver";
51static char *serial_version = "0.6";
52
Jiri Slabyfd2d7a62012-03-05 14:52:28 +010053static struct serial_state rs_table[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55struct tty_driver *hp_simserial_driver;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static struct console *console;
58
Jiri Slaby2fcd5ca2012-03-05 14:52:33 +010059static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 unsigned char ch;
62 static unsigned char seen_esc = 0;
63
64 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
Jiri Slabyf66279c2012-03-05 14:52:41 +010065 if (ch == 27 && seen_esc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 seen_esc = 1;
67 continue;
Jiri Slabyf66279c2012-03-05 14:52:41 +010068 } else if (seen_esc == 1 && ch == 'O') {
69 seen_esc = 2;
70 continue;
71 } else if (seen_esc == 2) {
72 if (ch == 'P') /* F1 */
73 show_state();
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070074#ifdef CONFIG_MAGIC_SYSRQ
Jiri Slabyf66279c2012-03-05 14:52:41 +010075 if (ch == 'S') { /* F4 */
76 do {
77 ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR);
78 } while (!ch);
79 handle_sysrq(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Jiri Slabyf66279c2012-03-05 14:52:41 +010081#endif
82 seen_esc = 0;
83 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Andreas Schwabd50f5c52006-01-13 23:46:38 +010087 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
88 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90 tty_flip_buffer_push(tty);
91}
92
93/*
94 * This is the serial driver's interrupt routine for a single port
95 */
Al Viro5dcded12006-10-08 14:59:19 +010096static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Jiri Slaby916b7652012-03-05 14:52:20 +010098 struct serial_state *info = dev_id;
Jiri Slaby3a5c2422012-03-05 14:52:36 +010099 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100101 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
103 return IRQ_NONE;
104 }
105 /*
106 * pretty simple in our case, because we only get interrupts
107 * on inbound traffic
108 */
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100109 receive_chars(tty);
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100110 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return IRQ_HANDLED;
112}
113
114/*
115 * -------------------------------------------------------------------
116 * Here ends the serial interrupt routines.
117 * -------------------------------------------------------------------
118 */
119
Alan Coxf34d7a52008-04-30 00:54:13 -0700120static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jiri Slaby916b7652012-03-05 14:52:20 +0100122 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned long flags;
124
Alan Coxf34d7a52008-04-30 00:54:13 -0700125 if (!tty || !info->xmit.buf)
126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 local_irq_save(flags);
129 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
130 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 info->xmit.buf[info->xmit.head] = ch;
134 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
135 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700136 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Jiri Slaby5e99d542012-03-05 14:52:23 +0100139static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
140 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 int count;
143 unsigned long flags;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 local_irq_save(flags);
146
147 if (info->x_char) {
148 char c = info->x_char;
149
150 console->write(console, &c, 1);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 info->x_char = 0;
153
154 goto out;
155 }
156
Jiri Slaby5e99d542012-03-05 14:52:23 +0100157 if (info->xmit.head == info->xmit.tail || tty->stopped ||
158 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#ifdef SIMSERIAL_DEBUG
160 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100161 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#endif
163 goto out;
164 }
165 /*
166 * We removed the loop and try to do it in to chunks. We need
167 * 2 operations maximum because it's a ring buffer.
168 *
169 * First from current to tail if possible.
170 * Then from the beginning of the buffer until necessary
171 */
172
173 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
174 SERIAL_XMIT_SIZE - info->xmit.tail);
175 console->write(console, info->xmit.buf+info->xmit.tail, count);
176
177 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
178
179 /*
180 * We have more at the beginning of the buffer
181 */
182 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
183 if (count) {
184 console->write(console, info->xmit.buf, count);
185 info->xmit.tail += count;
186 }
187out:
188 local_irq_restore(flags);
189}
190
191static void rs_flush_chars(struct tty_struct *tty)
192{
Jiri Slaby916b7652012-03-05 14:52:20 +0100193 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Jiri Slabyf66279c2012-03-05 14:52:41 +0100195 if (info->xmit.head == info->xmit.tail || tty->stopped ||
196 tty->hw_stopped || !info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return;
198
Jiri Slaby5e99d542012-03-05 14:52:23 +0100199 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202
203static int rs_write(struct tty_struct * tty,
204 const unsigned char *buf, int count)
205{
Jiri Slaby916b7652012-03-05 14:52:20 +0100206 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned long flags;
209
Jiri Slabyd88405d2012-03-05 14:52:29 +0100210 if (!tty || !info->xmit.buf)
211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 local_irq_save(flags);
214 while (1) {
215 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
216 if (count < c)
217 c = count;
218 if (c <= 0) {
219 break;
220 }
221 memcpy(info->xmit.buf + info->xmit.head, buf, c);
222 info->xmit.head = ((info->xmit.head + c) &
223 (SERIAL_XMIT_SIZE-1));
224 buf += c;
225 count -= c;
226 ret += c;
227 }
228 local_irq_restore(flags);
229 /*
230 * Hey, we transmit directly from here in our case
231 */
Jiri Slabyf66279c2012-03-05 14:52:41 +0100232 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
233 !tty->stopped && !tty->hw_stopped)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100234 transmit_chars(tty, info, NULL);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return ret;
237}
238
239static int rs_write_room(struct tty_struct *tty)
240{
Jiri Slaby916b7652012-03-05 14:52:20 +0100241 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
244}
245
246static int rs_chars_in_buffer(struct tty_struct *tty)
247{
Jiri Slaby916b7652012-03-05 14:52:20 +0100248 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
251}
252
253static void rs_flush_buffer(struct tty_struct *tty)
254{
Jiri Slaby916b7652012-03-05 14:52:20 +0100255 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 unsigned long flags;
257
258 local_irq_save(flags);
259 info->xmit.head = info->xmit.tail = 0;
260 local_irq_restore(flags);
261
Alan Cox15648f12008-07-16 21:52:25 +0100262 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
265/*
266 * This function is used to send a high-priority XON/XOFF character to
267 * the device
268 */
269static void rs_send_xchar(struct tty_struct *tty, char ch)
270{
Jiri Slaby916b7652012-03-05 14:52:20 +0100271 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 info->x_char = ch;
274 if (ch) {
275 /*
276 * I guess we could call console->write() directly but
277 * let's do that for now.
278 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100279 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281}
282
283/*
284 * ------------------------------------------------------------
285 * rs_throttle()
286 *
287 * This routine is called by the upper-layer tty layer to signal that
288 * incoming characters should be throttled.
289 * ------------------------------------------------------------
290 */
291static void rs_throttle(struct tty_struct * tty)
292{
Jiri Slabyf66279c2012-03-05 14:52:41 +0100293 if (I_IXOFF(tty))
294 rs_send_xchar(tty, STOP_CHAR(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 printk(KERN_INFO "simrs_throttle called\n");
297}
298
299static void rs_unthrottle(struct tty_struct * tty)
300{
Jiri Slaby916b7652012-03-05 14:52:20 +0100301 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (I_IXOFF(tty)) {
304 if (info->x_char)
305 info->x_char = 0;
306 else
307 rs_send_xchar(tty, START_CHAR(tty));
308 }
309 printk(KERN_INFO "simrs_unthrottle called\n");
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Luck, Tony10e82f62011-02-22 11:47:04 -0800313static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
316 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100317 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (tty->flags & (1 << TTY_IO_ERROR))
319 return -EIO;
320 }
321
322 switch (cmd) {
Jiri Slabyf66279c2012-03-05 14:52:41 +0100323 case TIOCGSERIAL:
324 case TIOCSSERIAL:
325 case TIOCSERGSTRUCT:
326 case TIOCMIWAIT:
327 return 0;
328 case TIOCSERCONFIG:
329 case TIOCSERGETLSR: /* Get line status register */
330 return -EINVAL;
331 case TIOCSERGWILD:
332 case TIOCSERSWILD:
333 /* "setserial -W" is called in Debian boot */
334 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
335 return 0;
336 }
337 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
341
Tony Luckf889a262006-12-12 10:47:36 -0800342static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* Handle turning off CRTSCTS */
345 if ((old_termios->c_cflag & CRTSCTS) &&
346 !(tty->termios->c_cflag & CRTSCTS)) {
347 tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349}
350/*
351 * This routine will shutdown a serial port; interrupts are disabled, and
352 * DTR is dropped if the hangup on close termio flag is on.
353 */
Jiri Slaby458cd312012-03-05 14:52:38 +0100354static void shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Jiri Slaby458cd312012-03-05 14:52:38 +0100356 struct serial_state *info = container_of(port, struct serial_state,
357 port);
358 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 local_irq_save(flags);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100361 if (info->irq)
362 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Jiri Slabyf66279c2012-03-05 14:52:41 +0100364 if (info->xmit.buf) {
365 free_page((unsigned long) info->xmit.buf);
366 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 local_irq_restore(flags);
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void rs_close(struct tty_struct *tty, struct file * filp)
372{
Jiri Slaby916b7652012-03-05 14:52:20 +0100373 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Jiri Slaby458cd312012-03-05 14:52:38 +0100375 tty_port_close(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378static void rs_hangup(struct tty_struct *tty)
379{
Jiri Slaby916b7652012-03-05 14:52:20 +0100380 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 rs_flush_buffer(tty);
Jiri Slaby458cd312012-03-05 14:52:38 +0100383 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Jiri Slaby9aead902012-03-05 14:52:37 +0100386static int activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jiri Slaby9aead902012-03-05 14:52:37 +0100388 struct serial_state *state = container_of(port, struct serial_state,
389 port);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100390 unsigned long flags, page;
391 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 page = get_zeroed_page(GFP_KERNEL);
394 if (!page)
395 return -ENOMEM;
396
397 local_irq_save(flags);
398
Jiri Slaby916b7652012-03-05 14:52:20 +0100399 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 free_page(page);
401 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100402 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jiri Slaby964105b2012-03-05 14:52:17 +0100404 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100405 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100406 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100407 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Jiri Slaby916b7652012-03-05 14:52:20 +0100411 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
414 * Set up the tty->alt_speed kludge
415 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100416 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100417 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100418 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100419 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100420 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100421 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100422 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100423 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425errout:
426 local_irq_restore(flags);
427 return retval;
428}
429
430
431/*
432 * This routine is called whenever a serial port is opened. It
433 * enables interrupts for a serial port, linking in its async structure into
434 * the IRQ chain. It also performs the serial-specific
435 * initialization for the tty structure.
436 */
437static int rs_open(struct tty_struct *tty, struct file * filp)
438{
Jiri Slaby916b7652012-03-05 14:52:20 +0100439 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100440 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jiri Slaby916b7652012-03-05 14:52:20 +0100442 tty->driver_data = info;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100443 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * figure out which console to use (should be one already)
447 */
448 console = console_drivers;
449 while (console) {
450 if ((console->flags & CON_ENABLED) && console->write) break;
451 console = console->next;
452 }
453
Jiri Slaby9aead902012-03-05 14:52:37 +0100454 return tty_port_open(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/*
458 * /proc fs routines....
459 */
460
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700461static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700463 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700465 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
466 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100467 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
468 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700472static int rs_proc_open(struct inode *inode, struct file *file)
473{
474 return single_open(file, rs_proc_show, NULL);
475}
476
477static const struct file_operations rs_proc_fops = {
478 .owner = THIS_MODULE,
479 .open = rs_proc_open,
480 .read = seq_read,
481 .llseek = seq_lseek,
482 .release = single_release,
483};
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485static inline void show_serial_version(void)
486{
487 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
488 printk(KERN_INFO " no serial options enabled\n");
489}
490
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700491static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 .open = rs_open,
493 .close = rs_close,
494 .write = rs_write,
495 .put_char = rs_put_char,
496 .flush_chars = rs_flush_chars,
497 .write_room = rs_write_room,
498 .chars_in_buffer = rs_chars_in_buffer,
499 .flush_buffer = rs_flush_buffer,
500 .ioctl = rs_ioctl,
501 .throttle = rs_throttle,
502 .unthrottle = rs_unthrottle,
503 .send_xchar = rs_send_xchar,
504 .set_termios = rs_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .hangup = rs_hangup,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700506 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507};
508
Jiri Slaby37343032012-03-05 14:52:34 +0100509static const struct tty_port_operations hp_port_ops = {
Jiri Slaby9aead902012-03-05 14:52:37 +0100510 .activate = activate,
Jiri Slaby458cd312012-03-05 14:52:38 +0100511 .shutdown = shutdown,
Jiri Slaby37343032012-03-05 14:52:34 +0100512};
513
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100514static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100516 struct serial_state *state;
517 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!ia64_platform_is("hpsim"))
520 return -ENODEV;
521
Jiri Slaby410235f2012-03-05 14:52:01 +0100522 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!hp_simserial_driver)
524 return -ENOMEM;
525
526 show_serial_version();
527
528 /* Initialize the tty_driver structure */
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 hp_simserial_driver->driver_name = "simserial";
531 hp_simserial_driver->name = "ttyS";
532 hp_simserial_driver->major = TTY_MAJOR;
533 hp_simserial_driver->minor_start = 64;
534 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
535 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
536 hp_simserial_driver->init_termios = tty_std_termios;
537 hp_simserial_driver->init_termios.c_cflag =
538 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
539 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
540 tty_set_operations(hp_simserial_driver, &hp_ops);
541
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100542 state = rs_table;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100543 tty_port_init(&state->port);
Jiri Slaby37343032012-03-05 14:52:34 +0100544 state->port.ops = &hp_port_ops;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100545 state->port.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100547 retval = hpsim_get_irq(KEYBOARD_INTR);
548 if (retval < 0) {
549 printk(KERN_ERR "%s: out of interrupt vectors!\n",
550 __func__);
551 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100554 state->irq = retval;
555
556 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100557 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100558
559 retval = tty_register_driver(hp_simserial_driver);
560 if (retval) {
561 printk(KERN_ERR "Couldn't register simserial driver\n");
562 goto err_free_tty;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100566err_free_tty:
567 put_tty_driver(hp_simserial_driver);
568 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571#ifndef MODULE
572__initcall(simrs_init);
573#endif