blob: d8237c3419956f2630521c762256b905715c63db [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>
27#include <linux/module.h>
28#include <linux/serial.h>
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070029#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/irq.h>
Jiri Slaby035cfe52012-03-08 21:01:17 +010032#include <asm/hpsim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/hw_irq.h>
34#include <asm/uaccess.h>
35
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static char *serial_name = "SimSerial driver";
52static char *serial_version = "0.6";
53
Jiri Slabyfd2d7a62012-03-05 14:52:28 +010054static struct serial_state rs_table[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56struct tty_driver *hp_simserial_driver;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct console *console;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060extern struct console *console_drivers; /* from kernel/printk.c */
61
Jiri Slaby2fcd5ca2012-03-05 14:52:33 +010062static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 unsigned char ch;
65 static unsigned char seen_esc = 0;
66
67 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
68 if ( ch == 27 && seen_esc == 0 ) {
69 seen_esc = 1;
70 continue;
71 } else {
72 if ( seen_esc==1 && ch == 'O' ) {
73 seen_esc = 2;
74 continue;
75 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070076 if ( ch == 'P' ) /* F1 */
77 show_state();
78#ifdef CONFIG_MAGIC_SYSRQ
79 if ( ch == 'S' ) { /* F4 */
80 do
81 ch = ia64_ssc(0, 0, 0, 0,
82 SSC_GETCHAR);
83 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -070084 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 seen_esc = 0;
88 continue;
89 }
90 }
91 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Andreas Schwabd50f5c52006-01-13 23:46:38 +010093 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
94 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
96 tty_flip_buffer_push(tty);
97}
98
99/*
100 * This is the serial driver's interrupt routine for a single port
101 */
Al Viro5dcded12006-10-08 14:59:19 +0100102static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Jiri Slaby916b7652012-03-05 14:52:20 +0100104 struct serial_state *info = dev_id;
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100105 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100107 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
109 return IRQ_NONE;
110 }
111 /*
112 * pretty simple in our case, because we only get interrupts
113 * on inbound traffic
114 */
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100115 receive_chars(tty);
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100116 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return IRQ_HANDLED;
118}
119
120/*
121 * -------------------------------------------------------------------
122 * Here ends the serial interrupt routines.
123 * -------------------------------------------------------------------
124 */
125
Alan Coxf34d7a52008-04-30 00:54:13 -0700126static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Jiri Slaby916b7652012-03-05 14:52:20 +0100128 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 unsigned long flags;
130
Alan Coxf34d7a52008-04-30 00:54:13 -0700131 if (!tty || !info->xmit.buf)
132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 local_irq_save(flags);
135 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
136 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 info->xmit.buf[info->xmit.head] = ch;
140 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
141 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Jiri Slaby5e99d542012-03-05 14:52:23 +0100145static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
146 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 int count;
149 unsigned long flags;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 local_irq_save(flags);
152
153 if (info->x_char) {
154 char c = info->x_char;
155
156 console->write(console, &c, 1);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 info->x_char = 0;
159
160 goto out;
161 }
162
Jiri Slaby5e99d542012-03-05 14:52:23 +0100163 if (info->xmit.head == info->xmit.tail || tty->stopped ||
164 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifdef SIMSERIAL_DEBUG
166 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100167 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168#endif
169 goto out;
170 }
171 /*
172 * We removed the loop and try to do it in to chunks. We need
173 * 2 operations maximum because it's a ring buffer.
174 *
175 * First from current to tail if possible.
176 * Then from the beginning of the buffer until necessary
177 */
178
179 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
180 SERIAL_XMIT_SIZE - info->xmit.tail);
181 console->write(console, info->xmit.buf+info->xmit.tail, count);
182
183 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
184
185 /*
186 * We have more at the beginning of the buffer
187 */
188 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
189 if (count) {
190 console->write(console, info->xmit.buf, count);
191 info->xmit.tail += count;
192 }
193out:
194 local_irq_restore(flags);
195}
196
197static void rs_flush_chars(struct tty_struct *tty)
198{
Jiri Slaby916b7652012-03-05 14:52:20 +0100199 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
202 !info->xmit.buf)
203 return;
204
Jiri Slaby5e99d542012-03-05 14:52:23 +0100205 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208
209static int rs_write(struct tty_struct * tty,
210 const unsigned char *buf, int count)
211{
Jiri Slaby916b7652012-03-05 14:52:20 +0100212 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned long flags;
215
Jiri Slabyd88405d2012-03-05 14:52:29 +0100216 if (!tty || !info->xmit.buf)
217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 local_irq_save(flags);
220 while (1) {
221 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
222 if (count < c)
223 c = count;
224 if (c <= 0) {
225 break;
226 }
227 memcpy(info->xmit.buf + info->xmit.head, buf, c);
228 info->xmit.head = ((info->xmit.head + c) &
229 (SERIAL_XMIT_SIZE-1));
230 buf += c;
231 count -= c;
232 ret += c;
233 }
234 local_irq_restore(flags);
235 /*
236 * Hey, we transmit directly from here in our case
237 */
238 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
239 && !tty->stopped && !tty->hw_stopped) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100240 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 return ret;
243}
244
245static int rs_write_room(struct tty_struct *tty)
246{
Jiri Slaby916b7652012-03-05 14:52:20 +0100247 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
250}
251
252static int rs_chars_in_buffer(struct tty_struct *tty)
253{
Jiri Slaby916b7652012-03-05 14:52:20 +0100254 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
257}
258
259static void rs_flush_buffer(struct tty_struct *tty)
260{
Jiri Slaby916b7652012-03-05 14:52:20 +0100261 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned long flags;
263
264 local_irq_save(flags);
265 info->xmit.head = info->xmit.tail = 0;
266 local_irq_restore(flags);
267
Alan Cox15648f12008-07-16 21:52:25 +0100268 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
270
271/*
272 * This function is used to send a high-priority XON/XOFF character to
273 * the device
274 */
275static void rs_send_xchar(struct tty_struct *tty, char ch)
276{
Jiri Slaby916b7652012-03-05 14:52:20 +0100277 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 info->x_char = ch;
280 if (ch) {
281 /*
282 * I guess we could call console->write() directly but
283 * let's do that for now.
284 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100285 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287}
288
289/*
290 * ------------------------------------------------------------
291 * rs_throttle()
292 *
293 * This routine is called by the upper-layer tty layer to signal that
294 * incoming characters should be throttled.
295 * ------------------------------------------------------------
296 */
297static void rs_throttle(struct tty_struct * tty)
298{
299 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
300
301 printk(KERN_INFO "simrs_throttle called\n");
302}
303
304static void rs_unthrottle(struct tty_struct * tty)
305{
Jiri Slaby916b7652012-03-05 14:52:20 +0100306 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (I_IXOFF(tty)) {
309 if (info->x_char)
310 info->x_char = 0;
311 else
312 rs_send_xchar(tty, START_CHAR(tty));
313 }
314 printk(KERN_INFO "simrs_unthrottle called\n");
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Luck, Tony10e82f62011-02-22 11:47:04 -0800318static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
321 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100322 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (tty->flags & (1 << TTY_IO_ERROR))
324 return -EIO;
325 }
326
327 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 case TIOCGSERIAL:
329 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
330 return 0;
331 case TIOCSSERIAL:
332 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
333 return 0;
334 case TIOCSERCONFIG:
335 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
336 return -EINVAL;
337
338 case TIOCSERGETLSR: /* Get line status register */
339 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
340 return -EINVAL;
341
342 case TIOCSERGSTRUCT:
343 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
344#if 0
345 if (copy_to_user((struct async_struct *) arg,
346 info, sizeof(struct async_struct)))
347 return -EFAULT;
348#endif
349 return 0;
350
351 /*
352 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
353 * - mask passed in arg for lines of interest
354 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
355 * Caller should use TIOCGICOUNT to see which one it was
356 */
357 case TIOCMIWAIT:
358 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case TIOCSERGWILD:
361 case TIOCSERSWILD:
362 /* "setserial -W" is called in Debian boot */
363 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
364 return 0;
365
366 default:
367 return -ENOIOCTLCMD;
368 }
369 return 0;
370}
371
372#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
373
Tony Luckf889a262006-12-12 10:47:36 -0800374static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* Handle turning off CRTSCTS */
377 if ((old_termios->c_cflag & CRTSCTS) &&
378 !(tty->termios->c_cflag & CRTSCTS)) {
379 tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381}
382/*
383 * This routine will shutdown a serial port; interrupts are disabled, and
384 * DTR is dropped if the hangup on close termio flag is on.
385 */
Jiri Slaby458cd312012-03-05 14:52:38 +0100386static void shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Jiri Slaby458cd312012-03-05 14:52:38 +0100388 struct serial_state *info = container_of(port, struct serial_state,
389 port);
390 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 local_irq_save(flags);
393 {
Jiri Slaby916b7652012-03-05 14:52:20 +0100394 if (info->irq)
395 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (info->xmit.buf) {
398 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100399 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 local_irq_restore(flags);
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static void rs_close(struct tty_struct *tty, struct file * filp)
406{
Jiri Slaby916b7652012-03-05 14:52:20 +0100407 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Jiri Slaby458cd312012-03-05 14:52:38 +0100409 tty_port_close(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static void rs_hangup(struct tty_struct *tty)
413{
Jiri Slaby916b7652012-03-05 14:52:20 +0100414 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 rs_flush_buffer(tty);
Jiri Slaby458cd312012-03-05 14:52:38 +0100417 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Jiri Slaby9aead902012-03-05 14:52:37 +0100420static int activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Jiri Slaby9aead902012-03-05 14:52:37 +0100422 struct serial_state *state = container_of(port, struct serial_state,
423 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 unsigned long flags;
425 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned long page;
427
428 page = get_zeroed_page(GFP_KERNEL);
429 if (!page)
430 return -ENOMEM;
431
432 local_irq_save(flags);
433
Jiri Slaby916b7652012-03-05 14:52:20 +0100434 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 free_page(page);
436 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100437 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Jiri Slaby964105b2012-03-05 14:52:17 +0100439 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100440 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100441 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100442 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Jiri Slaby916b7652012-03-05 14:52:20 +0100446 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * Set up the tty->alt_speed kludge
450 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100451 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100452 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100453 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100454 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100455 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100456 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100457 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100458 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460errout:
461 local_irq_restore(flags);
462 return retval;
463}
464
465
466/*
467 * This routine is called whenever a serial port is opened. It
468 * enables interrupts for a serial port, linking in its async structure into
469 * the IRQ chain. It also performs the serial-specific
470 * initialization for the tty structure.
471 */
472static int rs_open(struct tty_struct *tty, struct file * filp)
473{
Jiri Slaby916b7652012-03-05 14:52:20 +0100474 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100475 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jiri Slaby916b7652012-03-05 14:52:20 +0100477 tty->driver_data = info;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100478 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * figure out which console to use (should be one already)
482 */
483 console = console_drivers;
484 while (console) {
485 if ((console->flags & CON_ENABLED) && console->write) break;
486 console = console->next;
487 }
488
Jiri Slaby9aead902012-03-05 14:52:37 +0100489 return tty_port_open(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
493 * /proc fs routines....
494 */
495
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700496static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700498 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700500 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
501 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100502 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
503 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700507static int rs_proc_open(struct inode *inode, struct file *file)
508{
509 return single_open(file, rs_proc_show, NULL);
510}
511
512static const struct file_operations rs_proc_fops = {
513 .owner = THIS_MODULE,
514 .open = rs_proc_open,
515 .read = seq_read,
516 .llseek = seq_lseek,
517 .release = single_release,
518};
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static inline void show_serial_version(void)
521{
522 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
523 printk(KERN_INFO " no serial options enabled\n");
524}
525
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700526static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 .open = rs_open,
528 .close = rs_close,
529 .write = rs_write,
530 .put_char = rs_put_char,
531 .flush_chars = rs_flush_chars,
532 .write_room = rs_write_room,
533 .chars_in_buffer = rs_chars_in_buffer,
534 .flush_buffer = rs_flush_buffer,
535 .ioctl = rs_ioctl,
536 .throttle = rs_throttle,
537 .unthrottle = rs_unthrottle,
538 .send_xchar = rs_send_xchar,
539 .set_termios = rs_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 .hangup = rs_hangup,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700541 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542};
543
Jiri Slaby37343032012-03-05 14:52:34 +0100544static const struct tty_port_operations hp_port_ops = {
Jiri Slaby9aead902012-03-05 14:52:37 +0100545 .activate = activate,
Jiri Slaby458cd312012-03-05 14:52:38 +0100546 .shutdown = shutdown,
Jiri Slaby37343032012-03-05 14:52:34 +0100547};
548
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100549static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100551 struct serial_state *state;
552 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (!ia64_platform_is("hpsim"))
555 return -ENODEV;
556
Jiri Slaby410235f2012-03-05 14:52:01 +0100557 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (!hp_simserial_driver)
559 return -ENOMEM;
560
561 show_serial_version();
562
563 /* Initialize the tty_driver structure */
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 hp_simserial_driver->driver_name = "simserial";
566 hp_simserial_driver->name = "ttyS";
567 hp_simserial_driver->major = TTY_MAJOR;
568 hp_simserial_driver->minor_start = 64;
569 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
570 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
571 hp_simserial_driver->init_termios = tty_std_termios;
572 hp_simserial_driver->init_termios.c_cflag =
573 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
574 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
575 tty_set_operations(hp_simserial_driver, &hp_ops);
576
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100577 state = rs_table;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100578 tty_port_init(&state->port);
Jiri Slaby37343032012-03-05 14:52:34 +0100579 state->port.ops = &hp_port_ops;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100580 state->port.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100582 retval = hpsim_get_irq(KEYBOARD_INTR);
583 if (retval < 0) {
584 printk(KERN_ERR "%s: out of interrupt vectors!\n",
585 __func__);
586 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100589 state->irq = retval;
590
591 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100592 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100593
594 retval = tty_register_driver(hp_simserial_driver);
595 if (retval) {
596 printk(KERN_ERR "Couldn't register simserial driver\n");
597 goto err_free_tty;
598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100601err_free_tty:
602 put_tty_driver(hp_simserial_driver);
603 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606#ifndef MODULE
607__initcall(simrs_init);
608#endif