blob: de8cba121013150eadcdfc04fb1ebbbfd7ae4047 [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>
Ingo Molnarb17b0152017-02-08 18:51:35 +010017#include <linux/sched/debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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>
Alexey Dobriyanbf542152009-03-31 15:19:23 -070023#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
Randy Dunlapa9415642006-01-11 12:17:48 -080025#include <linux/capability.h>
Jiri Slaby3c4782d2012-03-05 14:52:31 +010026#include <linux/circ_buf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/console.h>
Jiri Slaby8b916332012-03-05 14:52:40 +010028#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/serial.h>
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070031#include <linux/sysrq.h>
Jiri Slaby8b916332012-03-05 14:52:40 +010032#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jiri Slaby035cfe52012-03-08 21:01:17 +010034#include <asm/hpsim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Jiri Slaby035cfe52012-03-08 21:01:17 +010036#include "hpsim_ssc.h"
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jiri Slaby3c4782d2012-03-05 14:52:31 +010044struct serial_state {
Jiri Slaby7f32f8d2012-03-05 14:52:32 +010045 struct tty_port port;
Jiri Slaby3c4782d2012-03-05 14:52:31 +010046 struct circ_buf xmit;
47 int irq;
48 int x_char;
49};
50
Jiri Slabyfd2d7a62012-03-05 14:52:28 +010051static struct serial_state rs_table[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53struct tty_driver *hp_simserial_driver;
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static struct console *console;
56
Jiri Slaby2e124b42013-01-03 15:53:06 +010057static void receive_chars(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 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 }
Jiri Slaby2e124b42013-01-03 15:53:06 +010088 tty_flip_buffer_push(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jiri Slaby2e124b42013-01-03 15:53:06 +010098 receive_chars(&info->port);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return IRQ_HANDLED;
101}
102
103/*
104 * -------------------------------------------------------------------
105 * Here ends the serial interrupt routines.
106 * -------------------------------------------------------------------
107 */
108
Alan Coxf34d7a52008-04-30 00:54:13 -0700109static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Jiri Slaby916b7652012-03-05 14:52:20 +0100111 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned long flags;
113
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100114 if (!info->xmit.buf)
Alan Coxf34d7a52008-04-30 00:54:13 -0700115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 local_irq_save(flags);
118 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
119 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
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);
Alan Coxf34d7a52008-04-30 00:54:13 -0700125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Jiri Slaby5e99d542012-03-05 14:52:23 +0100128static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
129 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 int count;
132 unsigned long flags;
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 local_irq_save(flags);
135
136 if (info->x_char) {
137 char c = info->x_char;
138
139 console->write(console, &c, 1);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 info->x_char = 0;
142
143 goto out;
144 }
145
Jiri Slabyee797062013-03-07 13:12:34 +0100146 if (info->xmit.head == info->xmit.tail || tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#ifdef SIMSERIAL_DEBUG
148 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100149 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#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 }
175out:
176 local_irq_restore(flags);
177}
178
179static void rs_flush_chars(struct tty_struct *tty)
180{
Jiri Slaby916b7652012-03-05 14:52:20 +0100181 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jiri Slabyf66279c2012-03-05 14:52:41 +0100183 if (info->xmit.head == info->xmit.tail || tty->stopped ||
Jiri Slabyee797062013-03-07 13:12:34 +0100184 !info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return;
186
Jiri Slaby5e99d542012-03-05 14:52:23 +0100187 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static int rs_write(struct tty_struct * tty,
191 const unsigned char *buf, int count)
192{
Jiri Slaby916b7652012-03-05 14:52:20 +0100193 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned long flags;
196
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100197 if (!info->xmit.buf)
Jiri Slabyd88405d2012-03-05 14:52:29 +0100198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
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 */
Jiri Slabyf66279c2012-03-05 14:52:41 +0100219 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
Jiri Slabyee797062013-03-07 13:12:34 +0100220 !tty->stopped)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100221 transmit_chars(tty, info, NULL);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return ret;
224}
225
226static int rs_write_room(struct tty_struct *tty)
227{
Jiri Slaby916b7652012-03-05 14:52:20 +0100228 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
231}
232
233static int rs_chars_in_buffer(struct tty_struct *tty)
234{
Jiri Slaby916b7652012-03-05 14:52:20 +0100235 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
238}
239
240static void rs_flush_buffer(struct tty_struct *tty)
241{
Jiri Slaby916b7652012-03-05 14:52:20 +0100242 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long flags;
244
245 local_irq_save(flags);
246 info->xmit.head = info->xmit.tail = 0;
247 local_irq_restore(flags);
248
Alan Cox15648f12008-07-16 21:52:25 +0100249 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
253 * This function is used to send a high-priority XON/XOFF character to
254 * the device
255 */
256static void rs_send_xchar(struct tty_struct *tty, char ch)
257{
Jiri Slaby916b7652012-03-05 14:52:20 +0100258 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
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 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100266 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
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 */
278static void rs_throttle(struct tty_struct * tty)
279{
Jiri Slabyf66279c2012-03-05 14:52:41 +0100280 if (I_IXOFF(tty))
281 rs_send_xchar(tty, STOP_CHAR(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 printk(KERN_INFO "simrs_throttle called\n");
284}
285
286static void rs_unthrottle(struct tty_struct * tty)
287{
Jiri Slaby916b7652012-03-05 14:52:20 +0100288 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
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
Luck, Tony10e82f62011-02-22 11:47:04 -0800299static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
302 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100303 (cmd != TIOCMIWAIT)) {
Peter Hurley18900ca2016-04-09 17:06:48 -0700304 if (tty_io_error(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return -EIO;
306 }
307
308 switch (cmd) {
Jiri Slabyf66279c2012-03-05 14:52:41 +0100309 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*
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 */
Jiri Slaby458cd312012-03-05 14:52:38 +0100332static void shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Jiri Slaby458cd312012-03-05 14:52:38 +0100334 struct serial_state *info = container_of(port, struct serial_state,
335 port);
336 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 local_irq_save(flags);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100339 if (info->irq)
340 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Jiri Slabyf66279c2012-03-05 14:52:41 +0100342 if (info->xmit.buf) {
343 free_page((unsigned long) info->xmit.buf);
344 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 local_irq_restore(flags);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static void rs_close(struct tty_struct *tty, struct file * filp)
350{
Jiri Slaby916b7652012-03-05 14:52:20 +0100351 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Jiri Slaby458cd312012-03-05 14:52:38 +0100353 tty_port_close(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static void rs_hangup(struct tty_struct *tty)
357{
Jiri Slaby916b7652012-03-05 14:52:20 +0100358 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rs_flush_buffer(tty);
Jiri Slaby458cd312012-03-05 14:52:38 +0100361 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Jiri Slaby9aead902012-03-05 14:52:37 +0100364static int activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Jiri Slaby9aead902012-03-05 14:52:37 +0100366 struct serial_state *state = container_of(port, struct serial_state,
367 port);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100368 unsigned long flags, page;
369 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 page = get_zeroed_page(GFP_KERNEL);
372 if (!page)
373 return -ENOMEM;
374
375 local_irq_save(flags);
376
Jiri Slaby916b7652012-03-05 14:52:20 +0100377 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 free_page(page);
379 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100380 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Jiri Slaby964105b2012-03-05 14:52:17 +0100382 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100383 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100384 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100385 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Jiri Slaby916b7652012-03-05 14:52:20 +0100389 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
392 * Set up the tty->alt_speed kludge
393 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100394 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100395 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100396 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100397 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100398 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100399 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100400 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100401 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403errout:
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 */
415static int rs_open(struct tty_struct *tty, struct file * filp)
416{
Jiri Slaby916b7652012-03-05 14:52:20 +0100417 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100418 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jiri Slaby916b7652012-03-05 14:52:20 +0100420 tty->driver_data = info;
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100421 port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * 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
Jiri Slaby9aead902012-03-05 14:52:37 +0100432 return tty_port_open(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/*
436 * /proc fs routines....
437 */
438
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700439static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700441 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100443 seq_printf(m, "simserinfo:1.0\n");
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700444 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100445 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
446 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700450static int rs_proc_open(struct inode *inode, struct file *file)
451{
452 return single_open(file, rs_proc_show, NULL);
453}
454
455static 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
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700463static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 .hangup = rs_hangup,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700477 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478};
479
Jiri Slaby37343032012-03-05 14:52:34 +0100480static const struct tty_port_operations hp_port_ops = {
Jiri Slaby9aead902012-03-05 14:52:37 +0100481 .activate = activate,
Jiri Slaby458cd312012-03-05 14:52:38 +0100482 .shutdown = shutdown,
Jiri Slaby37343032012-03-05 14:52:34 +0100483};
484
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100485static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100487 struct serial_state *state;
488 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (!ia64_platform_is("hpsim"))
491 return -ENODEV;
492
Jiri Slaby410235f2012-03-05 14:52:01 +0100493 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!hp_simserial_driver)
495 return -ENOMEM;
496
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100497 printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* Initialize the tty_driver structure */
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 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
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100513 state = rs_table;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100514 tty_port_init(&state->port);
Jiri Slaby37343032012-03-05 14:52:34 +0100515 state->port.ops = &hp_port_ops;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100516 state->port.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100518 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100525 state->irq = retval;
526
527 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100528 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100529
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200530 tty_port_link_device(&state->port, hp_simserial_driver, 0);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100531 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100538err_free_tty:
539 put_tty_driver(hp_simserial_driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100540 tty_port_destroy(&state->port);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100541 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544#ifndef MODULE
545__initcall(simrs_init);
546#endif