blob: e70cadec7ce68feaa895108254b5d776227b97b9 [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 Slaby2e124b42013-01-03 15:53:06 +010056static void receive_chars(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 unsigned char ch;
59 static unsigned char seen_esc = 0;
60
61 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
Jiri Slabyf66279c2012-03-05 14:52:41 +010062 if (ch == 27 && seen_esc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 seen_esc = 1;
64 continue;
Jiri Slabyf66279c2012-03-05 14:52:41 +010065 } 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();
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070071#ifdef CONFIG_MAGIC_SYSRQ
Jiri Slabyf66279c2012-03-05 14:52:41 +010072 if (ch == 'S') { /* F4 */
73 do {
74 ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR);
75 } while (!ch);
76 handle_sysrq(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Jiri Slabyf66279c2012-03-05 14:52:41 +010078#endif
79 seen_esc = 0;
80 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Jiri Slaby92a19f92013-01-03 15:53:03 +010084 if (tty_insert_flip_char(port, ch, TTY_NORMAL) == 0)
Andreas Schwabd50f5c52006-01-13 23:46:38 +010085 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Jiri Slaby2e124b42013-01-03 15:53:06 +010087 tty_flip_buffer_push(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * This is the serial driver's interrupt routine for a single port
92 */
Al Viro5dcded12006-10-08 14:59:19 +010093static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Jiri Slaby916b7652012-03-05 14:52:20 +010095 struct serial_state *info = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Jiri Slaby2e124b42013-01-03 15:53:06 +010097 receive_chars(&info->port);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return IRQ_HANDLED;
100}
101
102/*
103 * -------------------------------------------------------------------
104 * Here ends the serial interrupt routines.
105 * -------------------------------------------------------------------
106 */
107
Alan Coxf34d7a52008-04-30 00:54:13 -0700108static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Jiri Slaby916b7652012-03-05 14:52:20 +0100110 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long flags;
112
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100113 if (!info->xmit.buf)
Alan Coxf34d7a52008-04-30 00:54:13 -0700114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 local_irq_save(flags);
117 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
118 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 info->xmit.buf[info->xmit.head] = ch;
122 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
123 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700124 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Jiri Slaby5e99d542012-03-05 14:52:23 +0100127static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
128 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 int count;
131 unsigned long flags;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 local_irq_save(flags);
134
135 if (info->x_char) {
136 char c = info->x_char;
137
138 console->write(console, &c, 1);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 info->x_char = 0;
141
142 goto out;
143 }
144
Jiri Slabyee797062013-03-07 13:12:34 +0100145 if (info->xmit.head == info->xmit.tail || tty->stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#ifdef SIMSERIAL_DEBUG
147 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100148 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150 goto out;
151 }
152 /*
153 * We removed the loop and try to do it in to chunks. We need
154 * 2 operations maximum because it's a ring buffer.
155 *
156 * First from current to tail if possible.
157 * Then from the beginning of the buffer until necessary
158 */
159
160 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
161 SERIAL_XMIT_SIZE - info->xmit.tail);
162 console->write(console, info->xmit.buf+info->xmit.tail, count);
163
164 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
165
166 /*
167 * We have more at the beginning of the buffer
168 */
169 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
170 if (count) {
171 console->write(console, info->xmit.buf, count);
172 info->xmit.tail += count;
173 }
174out:
175 local_irq_restore(flags);
176}
177
178static void rs_flush_chars(struct tty_struct *tty)
179{
Jiri Slaby916b7652012-03-05 14:52:20 +0100180 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Jiri Slabyf66279c2012-03-05 14:52:41 +0100182 if (info->xmit.head == info->xmit.tail || tty->stopped ||
Jiri Slabyee797062013-03-07 13:12:34 +0100183 !info->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return;
185
Jiri Slaby5e99d542012-03-05 14:52:23 +0100186 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static int rs_write(struct tty_struct * tty,
190 const unsigned char *buf, int count)
191{
Jiri Slaby916b7652012-03-05 14:52:20 +0100192 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned long flags;
195
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100196 if (!info->xmit.buf)
Jiri Slabyd88405d2012-03-05 14:52:29 +0100197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 local_irq_save(flags);
200 while (1) {
201 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
202 if (count < c)
203 c = count;
204 if (c <= 0) {
205 break;
206 }
207 memcpy(info->xmit.buf + info->xmit.head, buf, c);
208 info->xmit.head = ((info->xmit.head + c) &
209 (SERIAL_XMIT_SIZE-1));
210 buf += c;
211 count -= c;
212 ret += c;
213 }
214 local_irq_restore(flags);
215 /*
216 * Hey, we transmit directly from here in our case
217 */
Jiri Slabyf66279c2012-03-05 14:52:41 +0100218 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
Jiri Slabyee797062013-03-07 13:12:34 +0100219 !tty->stopped)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100220 transmit_chars(tty, info, NULL);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return ret;
223}
224
225static int rs_write_room(struct tty_struct *tty)
226{
Jiri Slaby916b7652012-03-05 14:52:20 +0100227 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
230}
231
232static int rs_chars_in_buffer(struct tty_struct *tty)
233{
Jiri Slaby916b7652012-03-05 14:52:20 +0100234 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
237}
238
239static void rs_flush_buffer(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 unsigned long flags;
243
244 local_irq_save(flags);
245 info->xmit.head = info->xmit.tail = 0;
246 local_irq_restore(flags);
247
Alan Cox15648f12008-07-16 21:52:25 +0100248 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/*
252 * This function is used to send a high-priority XON/XOFF character to
253 * the device
254 */
255static void rs_send_xchar(struct tty_struct *tty, char ch)
256{
Jiri Slaby916b7652012-03-05 14:52:20 +0100257 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 info->x_char = ch;
260 if (ch) {
261 /*
262 * I guess we could call console->write() directly but
263 * let's do that for now.
264 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100265 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267}
268
269/*
270 * ------------------------------------------------------------
271 * rs_throttle()
272 *
273 * This routine is called by the upper-layer tty layer to signal that
274 * incoming characters should be throttled.
275 * ------------------------------------------------------------
276 */
277static void rs_throttle(struct tty_struct * tty)
278{
Jiri Slabyf66279c2012-03-05 14:52:41 +0100279 if (I_IXOFF(tty))
280 rs_send_xchar(tty, STOP_CHAR(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 printk(KERN_INFO "simrs_throttle called\n");
283}
284
285static void rs_unthrottle(struct tty_struct * tty)
286{
Jiri Slaby916b7652012-03-05 14:52:20 +0100287 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 if (I_IXOFF(tty)) {
290 if (info->x_char)
291 info->x_char = 0;
292 else
293 rs_send_xchar(tty, START_CHAR(tty));
294 }
295 printk(KERN_INFO "simrs_unthrottle called\n");
296}
297
Luck, Tony10e82f62011-02-22 11:47:04 -0800298static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
301 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100302 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (tty->flags & (1 << TTY_IO_ERROR))
304 return -EIO;
305 }
306
307 switch (cmd) {
Jiri Slabyf66279c2012-03-05 14:52:41 +0100308 case TIOCGSERIAL:
309 case TIOCSSERIAL:
310 case TIOCSERGSTRUCT:
311 case TIOCMIWAIT:
312 return 0;
313 case TIOCSERCONFIG:
314 case TIOCSERGETLSR: /* Get line status register */
315 return -EINVAL;
316 case TIOCSERGWILD:
317 case TIOCSERSWILD:
318 /* "setserial -W" is called in Debian boot */
319 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
320 return 0;
321 }
322 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * This routine will shutdown a serial port; interrupts are disabled, and
329 * DTR is dropped if the hangup on close termio flag is on.
330 */
Jiri Slaby458cd312012-03-05 14:52:38 +0100331static void shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Jiri Slaby458cd312012-03-05 14:52:38 +0100333 struct serial_state *info = container_of(port, struct serial_state,
334 port);
335 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 local_irq_save(flags);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100338 if (info->irq)
339 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jiri Slabyf66279c2012-03-05 14:52:41 +0100341 if (info->xmit.buf) {
342 free_page((unsigned long) info->xmit.buf);
343 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 local_irq_restore(flags);
346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static void rs_close(struct tty_struct *tty, struct file * filp)
349{
Jiri Slaby916b7652012-03-05 14:52:20 +0100350 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Jiri Slaby458cd312012-03-05 14:52:38 +0100352 tty_port_close(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static void rs_hangup(struct tty_struct *tty)
356{
Jiri Slaby916b7652012-03-05 14:52:20 +0100357 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 rs_flush_buffer(tty);
Jiri Slaby458cd312012-03-05 14:52:38 +0100360 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Jiri Slaby9aead902012-03-05 14:52:37 +0100363static int activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Jiri Slaby9aead902012-03-05 14:52:37 +0100365 struct serial_state *state = container_of(port, struct serial_state,
366 port);
Jiri Slabyf66279c2012-03-05 14:52:41 +0100367 unsigned long flags, page;
368 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 page = get_zeroed_page(GFP_KERNEL);
371 if (!page)
372 return -ENOMEM;
373
374 local_irq_save(flags);
375
Jiri Slaby916b7652012-03-05 14:52:20 +0100376 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 free_page(page);
378 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100379 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jiri Slaby964105b2012-03-05 14:52:17 +0100381 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100382 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100383 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100384 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Jiri Slaby916b7652012-03-05 14:52:20 +0100388 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
391 * Set up the tty->alt_speed kludge
392 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100393 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100394 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100395 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100396 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100397 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100398 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100399 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100400 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402errout:
403 local_irq_restore(flags);
404 return retval;
405}
406
407
408/*
409 * This routine is called whenever a serial port is opened. It
410 * enables interrupts for a serial port, linking in its async structure into
411 * the IRQ chain. It also performs the serial-specific
412 * initialization for the tty structure.
413 */
414static int rs_open(struct tty_struct *tty, struct file * filp)
415{
Jiri Slaby916b7652012-03-05 14:52:20 +0100416 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100417 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Jiri Slaby916b7652012-03-05 14:52:20 +0100419 tty->driver_data = info;
Jiri Slabyd6c53c02013-01-03 15:53:05 +0100420 port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 * figure out which console to use (should be one already)
424 */
425 console = console_drivers;
426 while (console) {
427 if ((console->flags & CON_ENABLED) && console->write) break;
428 console = console->next;
429 }
430
Jiri Slaby9aead902012-03-05 14:52:37 +0100431 return tty_port_open(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * /proc fs routines....
436 */
437
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700438static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700440 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100442 seq_printf(m, "simserinfo:1.0\n");
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700443 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100444 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
445 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700449static int rs_proc_open(struct inode *inode, struct file *file)
450{
451 return single_open(file, rs_proc_show, NULL);
452}
453
454static const struct file_operations rs_proc_fops = {
455 .owner = THIS_MODULE,
456 .open = rs_proc_open,
457 .read = seq_read,
458 .llseek = seq_lseek,
459 .release = single_release,
460};
461
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700462static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 .open = rs_open,
464 .close = rs_close,
465 .write = rs_write,
466 .put_char = rs_put_char,
467 .flush_chars = rs_flush_chars,
468 .write_room = rs_write_room,
469 .chars_in_buffer = rs_chars_in_buffer,
470 .flush_buffer = rs_flush_buffer,
471 .ioctl = rs_ioctl,
472 .throttle = rs_throttle,
473 .unthrottle = rs_unthrottle,
474 .send_xchar = rs_send_xchar,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 .hangup = rs_hangup,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700476 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477};
478
Jiri Slaby37343032012-03-05 14:52:34 +0100479static const struct tty_port_operations hp_port_ops = {
Jiri Slaby9aead902012-03-05 14:52:37 +0100480 .activate = activate,
Jiri Slaby458cd312012-03-05 14:52:38 +0100481 .shutdown = shutdown,
Jiri Slaby37343032012-03-05 14:52:34 +0100482};
483
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100484static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100486 struct serial_state *state;
487 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (!ia64_platform_is("hpsim"))
490 return -ENODEV;
491
Jiri Slaby410235f2012-03-05 14:52:01 +0100492 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (!hp_simserial_driver)
494 return -ENOMEM;
495
Jiri Slaby6e9ebcf2012-03-05 14:52:42 +0100496 printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* Initialize the tty_driver structure */
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 hp_simserial_driver->driver_name = "simserial";
501 hp_simserial_driver->name = "ttyS";
502 hp_simserial_driver->major = TTY_MAJOR;
503 hp_simserial_driver->minor_start = 64;
504 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
505 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
506 hp_simserial_driver->init_termios = tty_std_termios;
507 hp_simserial_driver->init_termios.c_cflag =
508 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
509 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
510 tty_set_operations(hp_simserial_driver, &hp_ops);
511
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100512 state = rs_table;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100513 tty_port_init(&state->port);
Jiri Slaby37343032012-03-05 14:52:34 +0100514 state->port.ops = &hp_port_ops;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100515 state->port.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100517 retval = hpsim_get_irq(KEYBOARD_INTR);
518 if (retval < 0) {
519 printk(KERN_ERR "%s: out of interrupt vectors!\n",
520 __func__);
521 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100524 state->irq = retval;
525
526 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100527 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100528
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200529 tty_port_link_device(&state->port, hp_simserial_driver, 0);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100530 retval = tty_register_driver(hp_simserial_driver);
531 if (retval) {
532 printk(KERN_ERR "Couldn't register simserial driver\n");
533 goto err_free_tty;
534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100537err_free_tty:
538 put_tty_driver(hp_simserial_driver);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100539 tty_port_destroy(&state->port);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100540 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543#ifndef MODULE
544__initcall(simrs_init);
545#endif