blob: 942022a5bc861fa6deed2695ad6ba35043f01ae9 [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
Jiri Slabyfd2d7a62012-03-05 14:52:28 +010050static struct serial_state rs_table[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52struct tty_driver *hp_simserial_driver;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct console *console;
55
Jiri Slaby2fcd5ca2012-03-05 14:52:33 +010056static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Jiri Slaby92a19f92013-01-03 15:53:03 +010058 struct tty_port *port = tty->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned char ch;
60 static unsigned char seen_esc = 0;
61
62 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
Jiri Slabyf66279c2012-03-05 14:52:41 +010063 if (ch == 27 && seen_esc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 seen_esc = 1;
65 continue;
Jiri Slabyf66279c2012-03-05 14:52:41 +010066 } 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();
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070072#ifdef CONFIG_MAGIC_SYSRQ
Jiri Slabyf66279c2012-03-05 14:52:41 +010073 if (ch == 'S') { /* F4 */
74 do {
75 ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR);
76 } while (!ch);
77 handle_sysrq(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Jiri Slabyf66279c2012-03-05 14:52:41 +010079#endif
80 seen_esc = 0;
81 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Jiri Slaby92a19f92013-01-03 15:53:03 +010085 if (tty_insert_flip_char(port, ch, TTY_NORMAL) == 0)
Andreas Schwabd50f5c52006-01-13 23:46:38 +010086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 tty_flip_buffer_push(tty);
89}
90
91/*
92 * This is the serial driver's interrupt routine for a single port
93 */
Al Viro5dcded12006-10-08 14:59:19 +010094static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Jiri Slaby916b7652012-03-05 14:52:20 +010096 struct serial_state *info = dev_id;
Jiri Slaby3a5c2422012-03-05 14:52:36 +010097 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Jiri Slaby7f32f8d2012-03-05 14:52:32 +010099 if (!tty) {
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100100 printk(KERN_INFO "%s: tty=0 problem\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return IRQ_NONE;
102 }
103 /*
104 * pretty simple in our case, because we only get interrupts
105 * on inbound traffic
106 */
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100107 receive_chars(tty);
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100108 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return IRQ_HANDLED;
110}
111
112/*
113 * -------------------------------------------------------------------
114 * Here ends the serial interrupt routines.
115 * -------------------------------------------------------------------
116 */
117
Alan Coxf34d7a52008-04-30 00:54:13 -0700118static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Jiri Slaby916b7652012-03-05 14:52:20 +0100120 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 unsigned long flags;
122
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100123 if (!info->xmit.buf)
Alan Coxf34d7a52008-04-30 00:54:13 -0700124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 local_irq_save(flags);
127 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
128 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 info->xmit.buf[info->xmit.head] = ch;
132 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
133 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Jiri Slaby5e99d542012-03-05 14:52:23 +0100137static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
138 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int count;
141 unsigned long flags;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 local_irq_save(flags);
144
145 if (info->x_char) {
146 char c = info->x_char;
147
148 console->write(console, &c, 1);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 info->x_char = 0;
151
152 goto out;
153 }
154
Jiri Slaby5e99d542012-03-05 14:52:23 +0100155 if (info->xmit.head == info->xmit.tail || tty->stopped ||
156 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#ifdef SIMSERIAL_DEBUG
158 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100159 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif
161 goto out;
162 }
163 /*
164 * We removed the loop and try to do it in to chunks. We need
165 * 2 operations maximum because it's a ring buffer.
166 *
167 * First from current to tail if possible.
168 * Then from the beginning of the buffer until necessary
169 */
170
171 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
172 SERIAL_XMIT_SIZE - info->xmit.tail);
173 console->write(console, info->xmit.buf+info->xmit.tail, count);
174
175 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
176
177 /*
178 * We have more at the beginning of the buffer
179 */
180 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
181 if (count) {
182 console->write(console, info->xmit.buf, count);
183 info->xmit.tail += count;
184 }
185out:
186 local_irq_restore(flags);
187}
188
189static void rs_flush_chars(struct tty_struct *tty)
190{
Jiri Slaby916b7652012-03-05 14:52:20 +0100191 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Jiri Slabyf66279c2012-03-05 14:52:41 +0100193 if (info->xmit.head == info->xmit.tail || tty->stopped ||
194 tty->hw_stopped || !info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return;
196
Jiri Slaby5e99d542012-03-05 14:52:23 +0100197 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static int rs_write(struct tty_struct * tty,
201 const unsigned char *buf, int count)
202{
Jiri Slaby916b7652012-03-05 14:52:20 +0100203 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned long flags;
206
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100207 if (!info->xmit.buf)
Jiri Slabyd88405d2012-03-05 14:52:29 +0100208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 local_irq_save(flags);
211 while (1) {
212 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
213 if (count < c)
214 c = count;
215 if (c <= 0) {
216 break;
217 }
218 memcpy(info->xmit.buf + info->xmit.head, buf, c);
219 info->xmit.head = ((info->xmit.head + c) &
220 (SERIAL_XMIT_SIZE-1));
221 buf += c;
222 count -= c;
223 ret += c;
224 }
225 local_irq_restore(flags);
226 /*
227 * Hey, we transmit directly from here in our case
228 */
Jiri Slabyf66279c2012-03-05 14:52:41 +0100229 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
230 !tty->stopped && !tty->hw_stopped)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100231 transmit_chars(tty, info, NULL);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return ret;
234}
235
236static int rs_write_room(struct tty_struct *tty)
237{
Jiri Slaby916b7652012-03-05 14:52:20 +0100238 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
241}
242
243static int rs_chars_in_buffer(struct tty_struct *tty)
244{
Jiri Slaby916b7652012-03-05 14:52:20 +0100245 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
248}
249
250static void rs_flush_buffer(struct tty_struct *tty)
251{
Jiri Slaby916b7652012-03-05 14:52:20 +0100252 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned long flags;
254
255 local_irq_save(flags);
256 info->xmit.head = info->xmit.tail = 0;
257 local_irq_restore(flags);
258
Alan Cox15648f12008-07-16 21:52:25 +0100259 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * This function is used to send a high-priority XON/XOFF character to
264 * the device
265 */
266static void rs_send_xchar(struct tty_struct *tty, char ch)
267{
Jiri Slaby916b7652012-03-05 14:52:20 +0100268 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 info->x_char = ch;
271 if (ch) {
272 /*
273 * I guess we could call console->write() directly but
274 * let's do that for now.
275 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100276 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278}
279
280/*
281 * ------------------------------------------------------------
282 * rs_throttle()
283 *
284 * This routine is called by the upper-layer tty layer to signal that
285 * incoming characters should be throttled.
286 * ------------------------------------------------------------
287 */
288static void rs_throttle(struct tty_struct * tty)
289{
Jiri Slabyf66279c2012-03-05 14:52:41 +0100290 if (I_IXOFF(tty))
291 rs_send_xchar(tty, STOP_CHAR(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 printk(KERN_INFO "simrs_throttle called\n");
294}
295
296static void rs_unthrottle(struct tty_struct * tty)
297{
Jiri Slaby916b7652012-03-05 14:52:20 +0100298 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 if (I_IXOFF(tty)) {
301 if (info->x_char)
302 info->x_char = 0;
303 else
304 rs_send_xchar(tty, START_CHAR(tty));
305 }
306 printk(KERN_INFO "simrs_unthrottle called\n");
307}
308
Luck, Tony10e82f62011-02-22 11:47:04 -0800309static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
312 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100313 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (tty->flags & (1 << TTY_IO_ERROR))
315 return -EIO;
316 }
317
318 switch (cmd) {
Jiri Slabyf66279c2012-03-05 14:52:41 +0100319 case TIOCGSERIAL:
320 case TIOCSSERIAL:
321 case TIOCSERGSTRUCT:
322 case TIOCMIWAIT:
323 return 0;
324 case TIOCSERCONFIG:
325 case TIOCSERGETLSR: /* Get line status register */
326 return -EINVAL;
327 case TIOCSERGWILD:
328 case TIOCSERSWILD:
329 /* "setserial -W" is called in Debian boot */
330 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
331 return 0;
332 }
333 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
337
Tony Luckf889a262006-12-12 10:47:36 -0800338static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* Handle turning off CRTSCTS */
341 if ((old_termios->c_cflag & CRTSCTS) &&
Alan Coxadc8d742012-07-14 15:31:47 +0100342 !(tty->termios.c_cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345}
346/*
347 * This routine will shutdown a serial port; interrupts are disabled, and
348 * DTR is dropped if the hangup on close termio flag is on.
349 */
Jiri Slaby458cd312012-03-05 14:52:38 +0100350static void shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Jiri Slaby458cd312012-03-05 14:52:38 +0100352 struct serial_state *info = container_of(port, struct serial_state,
353 port);
354 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 local_irq_save(flags);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100357 if (info->irq)
358 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jiri Slabyf66279c2012-03-05 14:52:41 +0100360 if (info->xmit.buf) {
361 free_page((unsigned long) info->xmit.buf);
362 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364 local_irq_restore(flags);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static void rs_close(struct tty_struct *tty, struct file * filp)
368{
Jiri Slaby916b7652012-03-05 14:52:20 +0100369 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Jiri Slaby458cd312012-03-05 14:52:38 +0100371 tty_port_close(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static void rs_hangup(struct tty_struct *tty)
375{
Jiri Slaby916b7652012-03-05 14:52:20 +0100376 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rs_flush_buffer(tty);
Jiri Slaby458cd312012-03-05 14:52:38 +0100379 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Jiri Slaby9aead902012-03-05 14:52:37 +0100382static int activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Jiri Slaby9aead902012-03-05 14:52:37 +0100384 struct serial_state *state = container_of(port, struct serial_state,
385 port);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100386 unsigned long flags, page;
387 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 page = get_zeroed_page(GFP_KERNEL);
390 if (!page)
391 return -ENOMEM;
392
393 local_irq_save(flags);
394
Jiri Slaby916b7652012-03-05 14:52:20 +0100395 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 free_page(page);
397 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100398 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Jiri Slaby964105b2012-03-05 14:52:17 +0100400 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100401 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100402 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100403 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
Jiri Slaby916b7652012-03-05 14:52:20 +0100407 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Set up the tty->alt_speed kludge
411 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100412 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100413 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100414 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100415 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100416 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100417 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100418 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100419 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421errout:
422 local_irq_restore(flags);
423 return retval;
424}
425
426
427/*
428 * This routine is called whenever a serial port is opened. It
429 * enables interrupts for a serial port, linking in its async structure into
430 * the IRQ chain. It also performs the serial-specific
431 * initialization for the tty structure.
432 */
433static int rs_open(struct tty_struct *tty, struct file * filp)
434{
Jiri Slaby916b7652012-03-05 14:52:20 +0100435 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100436 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Jiri Slaby916b7652012-03-05 14:52:20 +0100438 tty->driver_data = info;
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100439 port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * figure out which console to use (should be one already)
443 */
444 console = console_drivers;
445 while (console) {
446 if ((console->flags & CON_ENABLED) && console->write) break;
447 console = console->next;
448 }
449
Jiri Slaby9aead902012-03-05 14:52:37 +0100450 return tty_port_open(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/*
454 * /proc fs routines....
455 */
456
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700457static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700459 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100461 seq_printf(m, "simserinfo:1.0\n");
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700462 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100463 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
464 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700468static int rs_proc_open(struct inode *inode, struct file *file)
469{
470 return single_open(file, rs_proc_show, NULL);
471}
472
473static const struct file_operations rs_proc_fops = {
474 .owner = THIS_MODULE,
475 .open = rs_proc_open,
476 .read = seq_read,
477 .llseek = seq_lseek,
478 .release = single_release,
479};
480
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700481static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .open = rs_open,
483 .close = rs_close,
484 .write = rs_write,
485 .put_char = rs_put_char,
486 .flush_chars = rs_flush_chars,
487 .write_room = rs_write_room,
488 .chars_in_buffer = rs_chars_in_buffer,
489 .flush_buffer = rs_flush_buffer,
490 .ioctl = rs_ioctl,
491 .throttle = rs_throttle,
492 .unthrottle = rs_unthrottle,
493 .send_xchar = rs_send_xchar,
494 .set_termios = rs_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .hangup = rs_hangup,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700496 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497};
498
Jiri Slaby37343032012-03-05 14:52:34 +0100499static const struct tty_port_operations hp_port_ops = {
Jiri Slaby9aead902012-03-05 14:52:37 +0100500 .activate = activate,
Jiri Slaby458cd312012-03-05 14:52:38 +0100501 .shutdown = shutdown,
Jiri Slaby37343032012-03-05 14:52:34 +0100502};
503
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100504static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100506 struct serial_state *state;
507 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!ia64_platform_is("hpsim"))
510 return -ENODEV;
511
Jiri Slaby410235f2012-03-05 14:52:01 +0100512 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!hp_simserial_driver)
514 return -ENOMEM;
515
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100516 printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* Initialize the tty_driver structure */
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 hp_simserial_driver->driver_name = "simserial";
521 hp_simserial_driver->name = "ttyS";
522 hp_simserial_driver->major = TTY_MAJOR;
523 hp_simserial_driver->minor_start = 64;
524 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
525 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
526 hp_simserial_driver->init_termios = tty_std_termios;
527 hp_simserial_driver->init_termios.c_cflag =
528 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
529 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
530 tty_set_operations(hp_simserial_driver, &hp_ops);
531
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100532 state = rs_table;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100533 tty_port_init(&state->port);
Jiri Slaby37343032012-03-05 14:52:34 +0100534 state->port.ops = &hp_port_ops;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100535 state->port.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100537 retval = hpsim_get_irq(KEYBOARD_INTR);
538 if (retval < 0) {
539 printk(KERN_ERR "%s: out of interrupt vectors!\n",
540 __func__);
541 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100544 state->irq = retval;
545
546 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100547 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100548
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200549 tty_port_link_device(&state->port, hp_simserial_driver, 0);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100550 retval = tty_register_driver(hp_simserial_driver);
551 if (retval) {
552 printk(KERN_ERR "Couldn't register simserial driver\n");
553 goto err_free_tty;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100557err_free_tty:
558 put_tty_driver(hp_simserial_driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100559 tty_port_destroy(&state->port);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100560 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563#ifndef MODULE
564__initcall(simrs_init);
565#endif