d8237c3419956f2630521c762256b905715c63db
[firefly-linux-kernel-4.4.55.git] / arch / ia64 / hp / sim / simserial.c
1 /*
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).
8  *
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>
12  */
13
14 #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>
22 #include <linux/seq_file.h>
23 #include <linux/slab.h>
24 #include <linux/capability.h>
25 #include <linux/circ_buf.h>
26 #include <linux/console.h>
27 #include <linux/module.h>
28 #include <linux/serial.h>
29 #include <linux/sysrq.h>
30
31 #include <asm/irq.h>
32 #include <asm/hpsim.h>
33 #include <asm/hw_irq.h>
34 #include <asm/uaccess.h>
35
36 #include "hpsim_ssc.h"
37
38 #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 */
43
44 struct serial_state {
45         struct tty_port port;
46         struct circ_buf xmit;
47         int irq;
48         int x_char;
49 };
50
51 static char *serial_name = "SimSerial driver";
52 static char *serial_version = "0.6";
53
54 static struct serial_state rs_table[NR_PORTS];
55
56 struct tty_driver *hp_simserial_driver;
57
58 static struct console *console;
59
60 extern struct console *console_drivers; /* from kernel/printk.c */
61
62 static void receive_chars(struct tty_struct *tty)
63 {
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 ) {
76                                 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);
84                                         handle_sysrq(ch);
85                                 }
86 #endif
87                                 seen_esc = 0;
88                                 continue;
89                         }
90                 }
91                 seen_esc = 0;
92
93                 if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
94                         break;
95         }
96         tty_flip_buffer_push(tty);
97 }
98
99 /*
100  * This is the serial driver's interrupt routine for a single port
101  */
102 static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
103 {
104         struct serial_state *info = dev_id;
105         struct tty_struct *tty = tty_port_tty_get(&info->port);
106
107         if (!tty) {
108                 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          */
115         receive_chars(tty);
116         tty_kref_put(tty);
117         return IRQ_HANDLED;
118 }
119
120 /*
121  * -------------------------------------------------------------------
122  * Here ends the serial interrupt routines.
123  * -------------------------------------------------------------------
124  */
125
126 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
127 {
128         struct serial_state *info = tty->driver_data;
129         unsigned long flags;
130
131         if (!tty || !info->xmit.buf)
132                 return 0;
133
134         local_irq_save(flags);
135         if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
136                 local_irq_restore(flags);
137                 return 0;
138         }
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);
142         return 1;
143 }
144
145 static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
146                 int *intr_done)
147 {
148         int count;
149         unsigned long flags;
150
151         local_irq_save(flags);
152
153         if (info->x_char) {
154                 char c = info->x_char;
155
156                 console->write(console, &c, 1);
157
158                 info->x_char = 0;
159
160                 goto out;
161         }
162
163         if (info->xmit.head == info->xmit.tail || tty->stopped ||
164                         tty->hw_stopped) {
165 #ifdef SIMSERIAL_DEBUG
166                 printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
167                        info->xmit.head, info->xmit.tail, tty->stopped);
168 #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         }
193 out:
194         local_irq_restore(flags);
195 }
196
197 static void rs_flush_chars(struct tty_struct *tty)
198 {
199         struct serial_state *info = tty->driver_data;
200
201         if (info->xmit.head == info->xmit.tail || tty->stopped || tty->hw_stopped ||
202             !info->xmit.buf)
203                 return;
204
205         transmit_chars(tty, info, NULL);
206 }
207
208
209 static int rs_write(struct tty_struct * tty,
210                     const unsigned char *buf, int count)
211 {
212         struct serial_state *info = tty->driver_data;
213         int     c, ret = 0;
214         unsigned long flags;
215
216         if (!tty || !info->xmit.buf)
217                 return 0;
218
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) {
240                 transmit_chars(tty, info, NULL);
241         }
242         return ret;
243 }
244
245 static int rs_write_room(struct tty_struct *tty)
246 {
247         struct serial_state *info = tty->driver_data;
248
249         return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
250 }
251
252 static int rs_chars_in_buffer(struct tty_struct *tty)
253 {
254         struct serial_state *info = tty->driver_data;
255
256         return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
257 }
258
259 static void rs_flush_buffer(struct tty_struct *tty)
260 {
261         struct serial_state *info = tty->driver_data;
262         unsigned long flags;
263
264         local_irq_save(flags);
265         info->xmit.head = info->xmit.tail = 0;
266         local_irq_restore(flags);
267
268         tty_wakeup(tty);
269 }
270
271 /*
272  * This function is used to send a high-priority XON/XOFF character to
273  * the device
274  */
275 static void rs_send_xchar(struct tty_struct *tty, char ch)
276 {
277         struct serial_state *info = tty->driver_data;
278
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                  */
285                 transmit_chars(tty, info, NULL);
286         }
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  */
297 static 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
304 static void rs_unthrottle(struct tty_struct * tty)
305 {
306         struct serial_state *info = tty->driver_data;
307
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
317
318 static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
319 {
320         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
321             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
322             (cmd != TIOCMIWAIT)) {
323                 if (tty->flags & (1 << TTY_IO_ERROR))
324                     return -EIO;
325         }
326
327         switch (cmd) {
328                 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;
360                 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
374 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
375 {
376         /* Handle turning off CRTSCTS */
377         if ((old_termios->c_cflag & CRTSCTS) &&
378             !(tty->termios->c_cflag & CRTSCTS)) {
379                 tty->hw_stopped = 0;
380         }
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  */
386 static void shutdown(struct tty_port *port)
387 {
388         struct serial_state *info = container_of(port, struct serial_state,
389                         port);
390         unsigned long flags;
391
392         local_irq_save(flags);
393         {
394                 if (info->irq)
395                         free_irq(info->irq, info);
396
397                 if (info->xmit.buf) {
398                         free_page((unsigned long) info->xmit.buf);
399                         info->xmit.buf = NULL;
400                 }
401         }
402         local_irq_restore(flags);
403 }
404
405 static void rs_close(struct tty_struct *tty, struct file * filp)
406 {
407         struct serial_state *info = tty->driver_data;
408
409         tty_port_close(&info->port, tty, filp);
410 }
411
412 static void rs_hangup(struct tty_struct *tty)
413 {
414         struct serial_state *info = tty->driver_data;
415
416         rs_flush_buffer(tty);
417         tty_port_hangup(&info->port);
418 }
419
420 static int activate(struct tty_port *port, struct tty_struct *tty)
421 {
422         struct serial_state *state = container_of(port, struct serial_state,
423                         port);
424         unsigned long flags;
425         int     retval=0;
426         unsigned long page;
427
428         page = get_zeroed_page(GFP_KERNEL);
429         if (!page)
430                 return -ENOMEM;
431
432         local_irq_save(flags);
433
434         if (state->xmit.buf)
435                 free_page(page);
436         else
437                 state->xmit.buf = (unsigned char *) page;
438
439         if (state->irq) {
440                 retval = request_irq(state->irq, rs_interrupt_single, 0,
441                                 "simserial", state);
442                 if (retval)
443                         goto errout;
444         }
445
446         state->xmit.head = state->xmit.tail = 0;
447
448         /*
449          * Set up the tty->alt_speed kludge
450          */
451         if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
452                 tty->alt_speed = 57600;
453         if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
454                 tty->alt_speed = 115200;
455         if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
456                 tty->alt_speed = 230400;
457         if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
458                 tty->alt_speed = 460800;
459
460 errout:
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  */
472 static int rs_open(struct tty_struct *tty, struct file * filp)
473 {
474         struct serial_state *info = rs_table + tty->index;
475         struct tty_port *port = &info->port;
476
477         tty->driver_data = info;
478         tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
479
480         /*
481          * 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
489         return tty_port_open(port, tty, filp);
490 }
491
492 /*
493  * /proc fs routines....
494  */
495
496 static int rs_proc_show(struct seq_file *m, void *v)
497 {
498         int i;
499
500         seq_printf(m, "simserinfo:1.0 driver:%s\n", serial_version);
501         for (i = 0; i < NR_PORTS; i++)
502                 seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
503                        i, rs_table[i].irq);
504         return 0;
505 }
506
507 static int rs_proc_open(struct inode *inode, struct file *file)
508 {
509         return single_open(file, rs_proc_show, NULL);
510 }
511
512 static 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
520 static 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
526 static const struct tty_operations hp_ops = {
527         .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,
540         .hangup = rs_hangup,
541         .proc_fops = &rs_proc_fops,
542 };
543
544 static const struct tty_port_operations hp_port_ops = {
545         .activate = activate,
546         .shutdown = shutdown,
547 };
548
549 static int __init simrs_init(void)
550 {
551         struct serial_state *state;
552         int retval;
553
554         if (!ia64_platform_is("hpsim"))
555                 return -ENODEV;
556
557         hp_simserial_driver = alloc_tty_driver(NR_PORTS);
558         if (!hp_simserial_driver)
559                 return -ENOMEM;
560
561         show_serial_version();
562
563         /* Initialize the tty_driver structure */
564
565         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
577         state = rs_table;
578         tty_port_init(&state->port);
579         state->port.ops = &hp_port_ops;
580         state->port.close_delay = 0; /* XXX really 0? */
581
582         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;
587         }
588
589         state->irq = retval;
590
591         /* the port is imaginary */
592         printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
593
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         }
599
600         return 0;
601 err_free_tty:
602         put_tty_driver(hp_simserial_driver);
603         return retval;
604 }
605
606 #ifndef MODULE
607 __initcall(simrs_init);
608 #endif