blob: 24d6ca04de862e76fe053949c11ff83dccae522d [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)) ) {
65 if ( ch == 27 && seen_esc == 0 ) {
66 seen_esc = 1;
67 continue;
68 } else {
69 if ( seen_esc==1 && ch == 'O' ) {
70 seen_esc = 2;
71 continue;
72 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070073 if ( ch == 'P' ) /* F1 */
74 show_state();
75#ifdef CONFIG_MAGIC_SYSRQ
76 if ( ch == 'S' ) { /* F4 */
77 do
78 ch = ia64_ssc(0, 0, 0, 0,
79 SSC_GETCHAR);
80 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -070081 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 seen_esc = 0;
85 continue;
86 }
87 }
88 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Andreas Schwabd50f5c52006-01-13 23:46:38 +010090 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
91 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93 tty_flip_buffer_push(tty);
94}
95
96/*
97 * This is the serial driver's interrupt routine for a single port
98 */
Al Viro5dcded12006-10-08 14:59:19 +010099static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Jiri Slaby916b7652012-03-05 14:52:20 +0100101 struct serial_state *info = dev_id;
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100102 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100104 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
106 return IRQ_NONE;
107 }
108 /*
109 * pretty simple in our case, because we only get interrupts
110 * on inbound traffic
111 */
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100112 receive_chars(tty);
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100113 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return IRQ_HANDLED;
115}
116
117/*
118 * -------------------------------------------------------------------
119 * Here ends the serial interrupt routines.
120 * -------------------------------------------------------------------
121 */
122
Alan Coxf34d7a52008-04-30 00:54:13 -0700123static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Jiri Slaby916b7652012-03-05 14:52:20 +0100125 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long flags;
127
Alan Coxf34d7a52008-04-30 00:54:13 -0700128 if (!tty || !info->xmit.buf)
129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 local_irq_save(flags);
132 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
133 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136 info->xmit.buf[info->xmit.head] = ch;
137 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
138 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700139 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Jiri Slaby5e99d542012-03-05 14:52:23 +0100142static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
143 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int count;
146 unsigned long flags;
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 local_irq_save(flags);
149
150 if (info->x_char) {
151 char c = info->x_char;
152
153 console->write(console, &c, 1);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 info->x_char = 0;
156
157 goto out;
158 }
159
Jiri Slaby5e99d542012-03-05 14:52:23 +0100160 if (info->xmit.head == info->xmit.tail || tty->stopped ||
161 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#ifdef SIMSERIAL_DEBUG
163 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100164 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#endif
166 goto out;
167 }
168 /*
169 * We removed the loop and try to do it in to chunks. We need
170 * 2 operations maximum because it's a ring buffer.
171 *
172 * First from current to tail if possible.
173 * Then from the beginning of the buffer until necessary
174 */
175
176 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
177 SERIAL_XMIT_SIZE - info->xmit.tail);
178 console->write(console, info->xmit.buf+info->xmit.tail, count);
179
180 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
181
182 /*
183 * We have more at the beginning of the buffer
184 */
185 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
186 if (count) {
187 console->write(console, info->xmit.buf, count);
188 info->xmit.tail += count;
189 }
190out:
191 local_irq_restore(flags);
192}
193
194static void rs_flush_chars(struct tty_struct *tty)
195{
Jiri Slaby916b7652012-03-05 14:52:20 +0100196 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
199 !info->xmit.buf)
200 return;
201
Jiri Slaby5e99d542012-03-05 14:52:23 +0100202 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205
206static int rs_write(struct tty_struct * tty,
207 const unsigned char *buf, int count)
208{
Jiri Slaby916b7652012-03-05 14:52:20 +0100209 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 unsigned long flags;
212
Jiri Slabyd88405d2012-03-05 14:52:29 +0100213 if (!tty || !info->xmit.buf)
214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 local_irq_save(flags);
217 while (1) {
218 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
219 if (count < c)
220 c = count;
221 if (c <= 0) {
222 break;
223 }
224 memcpy(info->xmit.buf + info->xmit.head, buf, c);
225 info->xmit.head = ((info->xmit.head + c) &
226 (SERIAL_XMIT_SIZE-1));
227 buf += c;
228 count -= c;
229 ret += c;
230 }
231 local_irq_restore(flags);
232 /*
233 * Hey, we transmit directly from here in our case
234 */
235 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
236 && !tty->stopped && !tty->hw_stopped) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100237 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239 return ret;
240}
241
242static int rs_write_room(struct tty_struct *tty)
243{
Jiri Slaby916b7652012-03-05 14:52:20 +0100244 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
247}
248
249static int rs_chars_in_buffer(struct tty_struct *tty)
250{
Jiri Slaby916b7652012-03-05 14:52:20 +0100251 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
254}
255
256static void rs_flush_buffer(struct tty_struct *tty)
257{
Jiri Slaby916b7652012-03-05 14:52:20 +0100258 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned long flags;
260
261 local_irq_save(flags);
262 info->xmit.head = info->xmit.tail = 0;
263 local_irq_restore(flags);
264
Alan Cox15648f12008-07-16 21:52:25 +0100265 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/*
269 * This function is used to send a high-priority XON/XOFF character to
270 * the device
271 */
272static void rs_send_xchar(struct tty_struct *tty, char ch)
273{
Jiri Slaby916b7652012-03-05 14:52:20 +0100274 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 info->x_char = ch;
277 if (ch) {
278 /*
279 * I guess we could call console->write() directly but
280 * let's do that for now.
281 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100282 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284}
285
286/*
287 * ------------------------------------------------------------
288 * rs_throttle()
289 *
290 * This routine is called by the upper-layer tty layer to signal that
291 * incoming characters should be throttled.
292 * ------------------------------------------------------------
293 */
294static void rs_throttle(struct tty_struct * tty)
295{
296 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
297
298 printk(KERN_INFO "simrs_throttle called\n");
299}
300
301static void rs_unthrottle(struct tty_struct * tty)
302{
Jiri Slaby916b7652012-03-05 14:52:20 +0100303 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 if (I_IXOFF(tty)) {
306 if (info->x_char)
307 info->x_char = 0;
308 else
309 rs_send_xchar(tty, START_CHAR(tty));
310 }
311 printk(KERN_INFO "simrs_unthrottle called\n");
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Luck, Tony10e82f62011-02-22 11:47:04 -0800315static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
318 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100319 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (tty->flags & (1 << TTY_IO_ERROR))
321 return -EIO;
322 }
323
324 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 case TIOCGSERIAL:
326 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
327 return 0;
328 case TIOCSSERIAL:
329 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
330 return 0;
331 case TIOCSERCONFIG:
332 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
333 return -EINVAL;
334
335 case TIOCSERGETLSR: /* Get line status register */
336 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
337 return -EINVAL;
338
339 case TIOCSERGSTRUCT:
340 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
341#if 0
342 if (copy_to_user((struct async_struct *) arg,
343 info, sizeof(struct async_struct)))
344 return -EFAULT;
345#endif
346 return 0;
347
348 /*
349 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
350 * - mask passed in arg for lines of interest
351 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
352 * Caller should use TIOCGICOUNT to see which one it was
353 */
354 case TIOCMIWAIT:
355 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 case TIOCSERGWILD:
358 case TIOCSERSWILD:
359 /* "setserial -W" is called in Debian boot */
360 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
361 return 0;
362
363 default:
364 return -ENOIOCTLCMD;
365 }
366 return 0;
367}
368
369#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
370
Tony Luckf889a262006-12-12 10:47:36 -0800371static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Handle turning off CRTSCTS */
374 if ((old_termios->c_cflag & CRTSCTS) &&
375 !(tty->termios->c_cflag & CRTSCTS)) {
376 tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378}
379/*
380 * This routine will shutdown a serial port; interrupts are disabled, and
381 * DTR is dropped if the hangup on close termio flag is on.
382 */
Jiri Slaby458cd312012-03-05 14:52:38 +0100383static void shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Jiri Slaby458cd312012-03-05 14:52:38 +0100385 struct serial_state *info = container_of(port, struct serial_state,
386 port);
387 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 local_irq_save(flags);
390 {
Jiri Slaby916b7652012-03-05 14:52:20 +0100391 if (info->irq)
392 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (info->xmit.buf) {
395 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100396 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 local_irq_restore(flags);
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static void rs_close(struct tty_struct *tty, struct file * filp)
403{
Jiri Slaby916b7652012-03-05 14:52:20 +0100404 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Jiri Slaby458cd312012-03-05 14:52:38 +0100406 tty_port_close(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409static void rs_hangup(struct tty_struct *tty)
410{
Jiri Slaby916b7652012-03-05 14:52:20 +0100411 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 rs_flush_buffer(tty);
Jiri Slaby458cd312012-03-05 14:52:38 +0100414 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Jiri Slaby9aead902012-03-05 14:52:37 +0100417static int activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Jiri Slaby9aead902012-03-05 14:52:37 +0100419 struct serial_state *state = container_of(port, struct serial_state,
420 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned long flags;
422 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 unsigned long page;
424
425 page = get_zeroed_page(GFP_KERNEL);
426 if (!page)
427 return -ENOMEM;
428
429 local_irq_save(flags);
430
Jiri Slaby916b7652012-03-05 14:52:20 +0100431 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 free_page(page);
433 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100434 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jiri Slaby964105b2012-03-05 14:52:17 +0100436 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100437 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100438 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100439 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Jiri Slaby916b7652012-03-05 14:52:20 +0100443 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * Set up the tty->alt_speed kludge
447 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100448 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100449 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100450 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100451 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100452 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100453 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100454 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100455 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457errout:
458 local_irq_restore(flags);
459 return retval;
460}
461
462
463/*
464 * This routine is called whenever a serial port is opened. It
465 * enables interrupts for a serial port, linking in its async structure into
466 * the IRQ chain. It also performs the serial-specific
467 * initialization for the tty structure.
468 */
469static int rs_open(struct tty_struct *tty, struct file * filp)
470{
Jiri Slaby916b7652012-03-05 14:52:20 +0100471 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100472 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Jiri Slaby916b7652012-03-05 14:52:20 +0100474 tty->driver_data = info;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100475 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * figure out which console to use (should be one already)
479 */
480 console = console_drivers;
481 while (console) {
482 if ((console->flags & CON_ENABLED) && console->write) break;
483 console = console->next;
484 }
485
Jiri Slaby9aead902012-03-05 14:52:37 +0100486 return tty_port_open(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/*
490 * /proc fs routines....
491 */
492
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700493static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700495 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700497 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
498 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100499 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
500 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700504static int rs_proc_open(struct inode *inode, struct file *file)
505{
506 return single_open(file, rs_proc_show, NULL);
507}
508
509static const struct file_operations rs_proc_fops = {
510 .owner = THIS_MODULE,
511 .open = rs_proc_open,
512 .read = seq_read,
513 .llseek = seq_lseek,
514 .release = single_release,
515};
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static inline void show_serial_version(void)
518{
519 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
520 printk(KERN_INFO " no serial options enabled\n");
521}
522
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700523static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .open = rs_open,
525 .close = rs_close,
526 .write = rs_write,
527 .put_char = rs_put_char,
528 .flush_chars = rs_flush_chars,
529 .write_room = rs_write_room,
530 .chars_in_buffer = rs_chars_in_buffer,
531 .flush_buffer = rs_flush_buffer,
532 .ioctl = rs_ioctl,
533 .throttle = rs_throttle,
534 .unthrottle = rs_unthrottle,
535 .send_xchar = rs_send_xchar,
536 .set_termios = rs_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 .hangup = rs_hangup,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700538 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539};
540
Jiri Slaby37343032012-03-05 14:52:34 +0100541static const struct tty_port_operations hp_port_ops = {
Jiri Slaby9aead902012-03-05 14:52:37 +0100542 .activate = activate,
Jiri Slaby458cd312012-03-05 14:52:38 +0100543 .shutdown = shutdown,
Jiri Slaby37343032012-03-05 14:52:34 +0100544};
545
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100546static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100548 struct serial_state *state;
549 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (!ia64_platform_is("hpsim"))
552 return -ENODEV;
553
Jiri Slaby410235f2012-03-05 14:52:01 +0100554 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!hp_simserial_driver)
556 return -ENOMEM;
557
558 show_serial_version();
559
560 /* Initialize the tty_driver structure */
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 hp_simserial_driver->driver_name = "simserial";
563 hp_simserial_driver->name = "ttyS";
564 hp_simserial_driver->major = TTY_MAJOR;
565 hp_simserial_driver->minor_start = 64;
566 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
567 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
568 hp_simserial_driver->init_termios = tty_std_termios;
569 hp_simserial_driver->init_termios.c_cflag =
570 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
571 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
572 tty_set_operations(hp_simserial_driver, &hp_ops);
573
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100574 state = rs_table;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100575 tty_port_init(&state->port);
Jiri Slaby37343032012-03-05 14:52:34 +0100576 state->port.ops = &hp_port_ops;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100577 state->port.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100579 retval = hpsim_get_irq(KEYBOARD_INTR);
580 if (retval < 0) {
581 printk(KERN_ERR "%s: out of interrupt vectors!\n",
582 __func__);
583 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100586 state->irq = retval;
587
588 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100589 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100590
591 retval = tty_register_driver(hp_simserial_driver);
592 if (retval) {
593 printk(KERN_ERR "Couldn't register simserial driver\n");
594 goto err_free_tty;
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100598err_free_tty:
599 put_tty_driver(hp_simserial_driver);
600 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603#ifndef MODULE
604__initcall(simrs_init);
605#endif