blob: 323c9325e570f17826537efdebceec034c9bbb05 [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
7 * case means sys_sim.c console (goes via the simulator). The code hereafter
8 * is completely leveraged from the serial.c driver.
9 *
10 * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co
11 * Stephane Eranian <eranian@hpl.hp.com>
12 * David Mosberger-Tang <davidm@hpl.hp.com>
13 *
14 * 02/04/00 D. Mosberger Merged in serial.c bug fixes in rs_close().
15 * 02/25/00 D. Mosberger Synced up with 2.3.99pre-5 version of serial.c.
16 * 07/30/02 D. Mosberger Replace sti()/cli() with explicit spinlocks & local irq masking
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/sched.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/major.h>
25#include <linux/fcntl.h>
26#include <linux/mm.h>
Alexey Dobriyanbf542152009-03-31 15:19:23 -070027#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
Randy Dunlapa9415642006-01-11 12:17:48 -080029#include <linux/capability.h>
Jiri Slaby3c4782d2012-03-05 14:52:31 +010030#include <linux/circ_buf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/console.h>
32#include <linux/module.h>
33#include <linux/serial.h>
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070034#include <linux/sysrq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/irq.h>
Jiri Slaby035cfe52012-03-08 21:01:17 +010037#include <asm/hpsim.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/hw_irq.h>
39#include <asm/uaccess.h>
40
Jiri Slaby035cfe52012-03-08 21:01:17 +010041#include "hpsim_ssc.h"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#undef SIMSERIAL_DEBUG /* define this to get some debug information */
44
45#define KEYBOARD_INTR 3 /* must match with simulator! */
46
47#define NR_PORTS 1 /* only one port for now */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jiri Slaby3c4782d2012-03-05 14:52:31 +010049struct serial_state {
Jiri Slaby7f32f8d2012-03-05 14:52:32 +010050 struct tty_port port;
Jiri Slaby3c4782d2012-03-05 14:52:31 +010051 struct circ_buf xmit;
52 int irq;
53 int x_char;
54};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static char *serial_name = "SimSerial driver";
57static char *serial_version = "0.6";
58
Jiri Slabyfd2d7a62012-03-05 14:52:28 +010059static struct serial_state rs_table[NR_PORTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61struct tty_driver *hp_simserial_driver;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static struct console *console;
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065extern struct console *console_drivers; /* from kernel/printk.c */
66
Jiri Slaby2fcd5ca2012-03-05 14:52:33 +010067static void receive_chars(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 unsigned char ch;
70 static unsigned char seen_esc = 0;
71
72 while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
73 if ( ch == 27 && seen_esc == 0 ) {
74 seen_esc = 1;
75 continue;
76 } else {
77 if ( seen_esc==1 && ch == 'O' ) {
78 seen_esc = 2;
79 continue;
80 } else if ( seen_esc == 2 ) {
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070081 if ( ch == 'P' ) /* F1 */
82 show_state();
83#ifdef CONFIG_MAGIC_SYSRQ
84 if ( ch == 'S' ) { /* F4 */
85 do
86 ch = ia64_ssc(0, 0, 0, 0,
87 SSC_GETCHAR);
88 while (!ch);
Dmitry Torokhovf3353972010-08-17 21:15:47 -070089 handle_sysrq(ch);
David Mosberger-Tang819c67e2005-06-09 22:40:00 -070090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 seen_esc = 0;
93 continue;
94 }
95 }
96 seen_esc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Andreas Schwabd50f5c52006-01-13 23:46:38 +010098 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
99 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 tty_flip_buffer_push(tty);
102}
103
104/*
105 * This is the serial driver's interrupt routine for a single port
106 */
Al Viro5dcded12006-10-08 14:59:19 +0100107static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Jiri Slaby916b7652012-03-05 14:52:20 +0100109 struct serial_state *info = dev_id;
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100110 struct tty_struct *tty = tty_port_tty_get(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100112 if (!tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 printk(KERN_INFO "simrs_interrupt_single: info|tty=0 info=%p problem\n", info);
114 return IRQ_NONE;
115 }
116 /*
117 * pretty simple in our case, because we only get interrupts
118 * on inbound traffic
119 */
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100120 receive_chars(tty);
Jiri Slaby3a5c2422012-03-05 14:52:36 +0100121 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return IRQ_HANDLED;
123}
124
125/*
126 * -------------------------------------------------------------------
127 * Here ends the serial interrupt routines.
128 * -------------------------------------------------------------------
129 */
130
Alan Coxf34d7a52008-04-30 00:54:13 -0700131static int rs_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Jiri Slaby916b7652012-03-05 14:52:20 +0100133 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned long flags;
135
Alan Coxf34d7a52008-04-30 00:54:13 -0700136 if (!tty || !info->xmit.buf)
137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 local_irq_save(flags);
140 if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
141 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 info->xmit.buf[info->xmit.head] = ch;
145 info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
146 local_irq_restore(flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700147 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Jiri Slaby5e99d542012-03-05 14:52:23 +0100150static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
151 int *intr_done)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 int count;
154 unsigned long flags;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 local_irq_save(flags);
157
158 if (info->x_char) {
159 char c = info->x_char;
160
161 console->write(console, &c, 1);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 info->x_char = 0;
164
165 goto out;
166 }
167
Jiri Slaby5e99d542012-03-05 14:52:23 +0100168 if (info->xmit.head == info->xmit.tail || tty->stopped ||
169 tty->hw_stopped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#ifdef SIMSERIAL_DEBUG
171 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
Jiri Slaby5e99d542012-03-05 14:52:23 +0100172 info->xmit.head, info->xmit.tail, tty->stopped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173#endif
174 goto out;
175 }
176 /*
177 * We removed the loop and try to do it in to chunks. We need
178 * 2 operations maximum because it's a ring buffer.
179 *
180 * First from current to tail if possible.
181 * Then from the beginning of the buffer until necessary
182 */
183
184 count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
185 SERIAL_XMIT_SIZE - info->xmit.tail);
186 console->write(console, info->xmit.buf+info->xmit.tail, count);
187
188 info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
189
190 /*
191 * We have more at the beginning of the buffer
192 */
193 count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
194 if (count) {
195 console->write(console, info->xmit.buf, count);
196 info->xmit.tail += count;
197 }
198out:
199 local_irq_restore(flags);
200}
201
202static void rs_flush_chars(struct tty_struct *tty)
203{
Jiri Slaby916b7652012-03-05 14:52:20 +0100204 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
207 !info->xmit.buf)
208 return;
209
Jiri Slaby5e99d542012-03-05 14:52:23 +0100210 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213
214static int rs_write(struct tty_struct * tty,
215 const unsigned char *buf, int count)
216{
Jiri Slaby916b7652012-03-05 14:52:20 +0100217 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int c, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 unsigned long flags;
220
Jiri Slabyd88405d2012-03-05 14:52:29 +0100221 if (!tty || !info->xmit.buf)
222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 local_irq_save(flags);
225 while (1) {
226 c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
227 if (count < c)
228 c = count;
229 if (c <= 0) {
230 break;
231 }
232 memcpy(info->xmit.buf + info->xmit.head, buf, c);
233 info->xmit.head = ((info->xmit.head + c) &
234 (SERIAL_XMIT_SIZE-1));
235 buf += c;
236 count -= c;
237 ret += c;
238 }
239 local_irq_restore(flags);
240 /*
241 * Hey, we transmit directly from here in our case
242 */
243 if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)
244 && !tty->stopped && !tty->hw_stopped) {
Jiri Slaby5e99d542012-03-05 14:52:23 +0100245 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 return ret;
248}
249
250static int rs_write_room(struct tty_struct *tty)
251{
Jiri Slaby916b7652012-03-05 14:52:20 +0100252 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
255}
256
257static int rs_chars_in_buffer(struct tty_struct *tty)
258{
Jiri Slaby916b7652012-03-05 14:52:20 +0100259 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
262}
263
264static void rs_flush_buffer(struct tty_struct *tty)
265{
Jiri Slaby916b7652012-03-05 14:52:20 +0100266 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned long flags;
268
269 local_irq_save(flags);
270 info->xmit.head = info->xmit.tail = 0;
271 local_irq_restore(flags);
272
Alan Cox15648f12008-07-16 21:52:25 +0100273 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276/*
277 * This function is used to send a high-priority XON/XOFF character to
278 * the device
279 */
280static void rs_send_xchar(struct tty_struct *tty, char ch)
281{
Jiri Slaby916b7652012-03-05 14:52:20 +0100282 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 info->x_char = ch;
285 if (ch) {
286 /*
287 * I guess we could call console->write() directly but
288 * let's do that for now.
289 */
Jiri Slaby5e99d542012-03-05 14:52:23 +0100290 transmit_chars(tty, info, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292}
293
294/*
295 * ------------------------------------------------------------
296 * rs_throttle()
297 *
298 * This routine is called by the upper-layer tty layer to signal that
299 * incoming characters should be throttled.
300 * ------------------------------------------------------------
301 */
302static void rs_throttle(struct tty_struct * tty)
303{
304 if (I_IXOFF(tty)) rs_send_xchar(tty, STOP_CHAR(tty));
305
306 printk(KERN_INFO "simrs_throttle called\n");
307}
308
309static void rs_unthrottle(struct tty_struct * tty)
310{
Jiri Slaby916b7652012-03-05 14:52:20 +0100311 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (I_IXOFF(tty)) {
314 if (info->x_char)
315 info->x_char = 0;
316 else
317 rs_send_xchar(tty, START_CHAR(tty));
318 }
319 printk(KERN_INFO "simrs_unthrottle called\n");
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Luck, Tony10e82f62011-02-22 11:47:04 -0800323static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
326 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
Alan Cox05871022010-09-16 18:21:52 +0100327 (cmd != TIOCMIWAIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (tty->flags & (1 << TTY_IO_ERROR))
329 return -EIO;
330 }
331
332 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case TIOCGSERIAL:
334 printk(KERN_INFO "simrs_ioctl TIOCGSERIAL called\n");
335 return 0;
336 case TIOCSSERIAL:
337 printk(KERN_INFO "simrs_ioctl TIOCSSERIAL called\n");
338 return 0;
339 case TIOCSERCONFIG:
340 printk(KERN_INFO "rs_ioctl: TIOCSERCONFIG called\n");
341 return -EINVAL;
342
343 case TIOCSERGETLSR: /* Get line status register */
344 printk(KERN_INFO "rs_ioctl: TIOCSERGETLSR called\n");
345 return -EINVAL;
346
347 case TIOCSERGSTRUCT:
348 printk(KERN_INFO "rs_ioctl: TIOCSERGSTRUCT called\n");
349#if 0
350 if (copy_to_user((struct async_struct *) arg,
351 info, sizeof(struct async_struct)))
352 return -EFAULT;
353#endif
354 return 0;
355
356 /*
357 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
358 * - mask passed in arg for lines of interest
359 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
360 * Caller should use TIOCGICOUNT to see which one it was
361 */
362 case TIOCMIWAIT:
363 printk(KERN_INFO "rs_ioctl: TIOCMIWAIT: called\n");
364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 case TIOCSERGWILD:
366 case TIOCSERSWILD:
367 /* "setserial -W" is called in Debian boot */
368 printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
369 return 0;
370
371 default:
372 return -ENOIOCTLCMD;
373 }
374 return 0;
375}
376
377#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
378
Tony Luckf889a262006-12-12 10:47:36 -0800379static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* Handle turning off CRTSCTS */
382 if ((old_termios->c_cflag & CRTSCTS) &&
383 !(tty->termios->c_cflag & CRTSCTS)) {
384 tty->hw_stopped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386}
387/*
388 * This routine will shutdown a serial port; interrupts are disabled, and
389 * DTR is dropped if the hangup on close termio flag is on.
390 */
Jiri Slaby458cd312012-03-05 14:52:38 +0100391static void shutdown(struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Jiri Slaby458cd312012-03-05 14:52:38 +0100393 struct serial_state *info = container_of(port, struct serial_state,
394 port);
395 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 local_irq_save(flags);
398 {
Jiri Slaby916b7652012-03-05 14:52:20 +0100399 if (info->irq)
400 free_irq(info->irq, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 if (info->xmit.buf) {
403 free_page((unsigned long) info->xmit.buf);
Al Virocfa7fd72006-10-10 22:46:17 +0100404 info->xmit.buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407 local_irq_restore(flags);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static void rs_close(struct tty_struct *tty, struct file * filp)
411{
Jiri Slaby916b7652012-03-05 14:52:20 +0100412 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Jiri Slaby458cd312012-03-05 14:52:38 +0100414 tty_port_close(&info->port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static void rs_hangup(struct tty_struct *tty)
418{
Jiri Slaby916b7652012-03-05 14:52:20 +0100419 struct serial_state *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 rs_flush_buffer(tty);
Jiri Slaby458cd312012-03-05 14:52:38 +0100422 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Jiri Slaby9aead902012-03-05 14:52:37 +0100425static int activate(struct tty_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Jiri Slaby9aead902012-03-05 14:52:37 +0100427 struct serial_state *state = container_of(port, struct serial_state,
428 port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned long flags;
430 int retval=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned long page;
432
433 page = get_zeroed_page(GFP_KERNEL);
434 if (!page)
435 return -ENOMEM;
436
437 local_irq_save(flags);
438
Jiri Slaby916b7652012-03-05 14:52:20 +0100439 if (state->xmit.buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 free_page(page);
441 else
Jiri Slaby916b7652012-03-05 14:52:20 +0100442 state->xmit.buf = (unsigned char *) page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
445 * Allocate the IRQ if necessary
446 */
Jiri Slaby964105b2012-03-05 14:52:17 +0100447 if (state->irq) {
Jiri Slaby2f8c5212012-03-05 14:52:18 +0100448 retval = request_irq(state->irq, rs_interrupt_single, 0,
Jiri Slaby916b7652012-03-05 14:52:20 +0100449 "simserial", state);
Jiri Slaby9e12dd52012-03-08 21:01:19 +0100450 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453
Jiri Slaby916b7652012-03-05 14:52:20 +0100454 state->xmit.head = state->xmit.tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * Set up the tty->alt_speed kludge
458 */
Jiri Slaby01bd7302012-03-05 14:52:27 +0100459 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100460 tty->alt_speed = 57600;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100461 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100462 tty->alt_speed = 115200;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100463 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100464 tty->alt_speed = 230400;
Jiri Slaby01bd7302012-03-05 14:52:27 +0100465 if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
Jiri Slaby5e99d542012-03-05 14:52:23 +0100466 tty->alt_speed = 460800;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468errout:
469 local_irq_restore(flags);
470 return retval;
471}
472
473
474/*
475 * This routine is called whenever a serial port is opened. It
476 * enables interrupts for a serial port, linking in its async structure into
477 * the IRQ chain. It also performs the serial-specific
478 * initialization for the tty structure.
479 */
480static int rs_open(struct tty_struct *tty, struct file * filp)
481{
Jiri Slaby916b7652012-03-05 14:52:20 +0100482 struct serial_state *info = rs_table + tty->index;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100483 struct tty_port *port = &info->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jiri Slaby916b7652012-03-05 14:52:20 +0100485 tty->driver_data = info;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100486 tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * figure out which console to use (should be one already)
490 */
491 console = console_drivers;
492 while (console) {
493 if ((console->flags & CON_ENABLED) && console->write) break;
494 console = console->next;
495 }
496
Jiri Slaby9aead902012-03-05 14:52:37 +0100497 return tty_port_open(port, tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/*
501 * /proc fs routines....
502 */
503
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700504static int rs_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700506 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700508 seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
509 for (i = 0; i < NR_PORTS; i++)
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100510 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
511 i, rs_table[i].irq);
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700515static int rs_proc_open(struct inode *inode, struct file *file)
516{
517 return single_open(file, rs_proc_show, NULL);
518}
519
520static const struct file_operations rs_proc_fops = {
521 .owner = THIS_MODULE,
522 .open = rs_proc_open,
523 .read = seq_read,
524 .llseek = seq_lseek,
525 .release = single_release,
526};
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/*
529 * ---------------------------------------------------------------------
530 * rs_init() and friends
531 *
532 * rs_init() is called at boot-time to initialize the serial driver.
533 * ---------------------------------------------------------------------
534 */
535
536/*
537 * This routine prints out the appropriate serial driver version
538 * number, and identifies which options were configured into this
539 * driver.
540 */
541static inline void show_serial_version(void)
542{
543 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
544 printk(KERN_INFO " no serial options enabled\n");
545}
546
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700547static const struct tty_operations hp_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 .open = rs_open,
549 .close = rs_close,
550 .write = rs_write,
551 .put_char = rs_put_char,
552 .flush_chars = rs_flush_chars,
553 .write_room = rs_write_room,
554 .chars_in_buffer = rs_chars_in_buffer,
555 .flush_buffer = rs_flush_buffer,
556 .ioctl = rs_ioctl,
557 .throttle = rs_throttle,
558 .unthrottle = rs_unthrottle,
559 .send_xchar = rs_send_xchar,
560 .set_termios = rs_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 .hangup = rs_hangup,
Alexey Dobriyanbf542152009-03-31 15:19:23 -0700562 .proc_fops = &rs_proc_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563};
564
Jiri Slaby37343032012-03-05 14:52:34 +0100565static const struct tty_port_operations hp_port_ops = {
Jiri Slaby9aead902012-03-05 14:52:37 +0100566 .activate = activate,
Jiri Slaby458cd312012-03-05 14:52:38 +0100567 .shutdown = shutdown,
Jiri Slaby37343032012-03-05 14:52:34 +0100568};
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/*
571 * The serial driver boot-time initialization code!
572 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100573static int __init simrs_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100575 struct serial_state *state;
576 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (!ia64_platform_is("hpsim"))
579 return -ENODEV;
580
Jiri Slaby410235f2012-03-05 14:52:01 +0100581 hp_simserial_driver = alloc_tty_driver(NR_PORTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!hp_simserial_driver)
583 return -ENOMEM;
584
585 show_serial_version();
586
587 /* Initialize the tty_driver structure */
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 hp_simserial_driver->driver_name = "simserial";
590 hp_simserial_driver->name = "ttyS";
591 hp_simserial_driver->major = TTY_MAJOR;
592 hp_simserial_driver->minor_start = 64;
593 hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
594 hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
595 hp_simserial_driver->init_termios = tty_std_termios;
596 hp_simserial_driver->init_termios.c_cflag =
597 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
598 hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
599 tty_set_operations(hp_simserial_driver, &hp_ops);
600
601 /*
602 * Let's have a little bit of fun !
603 */
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100604 state = rs_table;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100605 tty_port_init(&state->port);
Jiri Slaby37343032012-03-05 14:52:34 +0100606 state->port.ops = &hp_port_ops;
Jiri Slaby7f32f8d2012-03-05 14:52:32 +0100607 state->port.close_delay = 0; /* XXX really 0? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100609 retval = hpsim_get_irq(KEYBOARD_INTR);
610 if (retval < 0) {
611 printk(KERN_ERR "%s: out of interrupt vectors!\n",
612 __func__);
613 goto err_free_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100616 state->irq = retval;
617
618 /* the port is imaginary */
Jiri Slaby98e3a9e2012-03-05 14:52:30 +0100619 printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100620
621 retval = tty_register_driver(hp_simserial_driver);
622 if (retval) {
623 printk(KERN_ERR "Couldn't register simserial driver\n");
624 goto err_free_tty;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 return 0;
Jiri Slabyfd2d7a62012-03-05 14:52:28 +0100628err_free_tty:
629 put_tty_driver(hp_simserial_driver);
630 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633#ifndef MODULE
634__initcall(simrs_init);
635#endif