linux/arch/ia64/hp/sim/simserial.c
<<
>>
Prefs
   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/irq.h>
  28#include <linux/module.h>
  29#include <linux/serial.h>
  30#include <linux/sysrq.h>
  31#include <linux/uaccess.h>
  32
  33#include <asm/hpsim.h>
  34
  35#include "hpsim_ssc.h"
  36
  37#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 */
  42
  43struct serial_state {
  44        struct tty_port port;
  45        struct circ_buf xmit;
  46        int irq;
  47        int x_char;
  48};
  49
  50static struct serial_state rs_table[NR_PORTS];
  51
  52struct tty_driver *hp_simserial_driver;
  53
  54static struct console *console;
  55
  56static void receive_chars(struct tty_struct *tty)
  57{
  58        unsigned char ch;
  59        static unsigned char seen_esc = 0;
  60
  61        while ( (ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR)) ) {
  62                if (ch == 27 && seen_esc == 0) {
  63                        seen_esc = 1;
  64                        continue;
  65                } else if (seen_esc == 1 && ch == 'O') {
  66                        seen_esc = 2;
  67                        continue;
  68                } else if (seen_esc == 2) {
  69                        if (ch == 'P') /* F1 */
  70                                show_state();
  71#ifdef CONFIG_MAGIC_SYSRQ
  72                        if (ch == 'S') { /* F4 */
  73                                do {
  74                                        ch = ia64_ssc(0, 0, 0, 0, SSC_GETCHAR);
  75                                } while (!ch);
  76                                handle_sysrq(ch);
  77                        }
  78#endif
  79                        seen_esc = 0;
  80                        continue;
  81                }
  82                seen_esc = 0;
  83
  84                if (tty_insert_flip_char(tty, ch, TTY_NORMAL) == 0)
  85                        break;
  86        }
  87        tty_flip_buffer_push(tty);
  88}
  89
  90/*
  91 * This is the serial driver's interrupt routine for a single port
  92 */
  93static irqreturn_t rs_interrupt_single(int irq, void *dev_id)
  94{
  95        struct serial_state *info = dev_id;
  96        struct tty_struct *tty = tty_port_tty_get(&info->port);
  97
  98        if (!tty) {
  99                printk(KERN_INFO "%s: tty=0 problem\n", __func__);
 100                return IRQ_NONE;
 101        }
 102        /*
 103         * pretty simple in our case, because we only get interrupts
 104         * on inbound traffic
 105         */
 106        receive_chars(tty);
 107        tty_kref_put(tty);
 108        return IRQ_HANDLED;
 109}
 110
 111/*
 112 * -------------------------------------------------------------------
 113 * Here ends the serial interrupt routines.
 114 * -------------------------------------------------------------------
 115 */
 116
 117static int rs_put_char(struct tty_struct *tty, unsigned char ch)
 118{
 119        struct serial_state *info = tty->driver_data;
 120        unsigned long flags;
 121
 122        if (!info->xmit.buf)
 123                return 0;
 124
 125        local_irq_save(flags);
 126        if (CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) == 0) {
 127                local_irq_restore(flags);
 128                return 0;
 129        }
 130        info->xmit.buf[info->xmit.head] = ch;
 131        info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
 132        local_irq_restore(flags);
 133        return 1;
 134}
 135
 136static void transmit_chars(struct tty_struct *tty, struct serial_state *info,
 137                int *intr_done)
 138{
 139        int count;
 140        unsigned long flags;
 141
 142        local_irq_save(flags);
 143
 144        if (info->x_char) {
 145                char c = info->x_char;
 146
 147                console->write(console, &c, 1);
 148
 149                info->x_char = 0;
 150
 151                goto out;
 152        }
 153
 154        if (info->xmit.head == info->xmit.tail || tty->stopped ||
 155                        tty->hw_stopped) {
 156#ifdef SIMSERIAL_DEBUG
 157                printk("transmit_chars: head=%d, tail=%d, stopped=%d\n",
 158                       info->xmit.head, info->xmit.tail, tty->stopped);
 159#endif
 160                goto out;
 161        }
 162        /*
 163         * We removed the loop and try to do it in to chunks. We need
 164         * 2 operations maximum because it's a ring buffer.
 165         *
 166         * First from current to tail if possible.
 167         * Then from the beginning of the buffer until necessary
 168         */
 169
 170        count = min(CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
 171                    SERIAL_XMIT_SIZE - info->xmit.tail);
 172        console->write(console, info->xmit.buf+info->xmit.tail, count);
 173
 174        info->xmit.tail = (info->xmit.tail+count) & (SERIAL_XMIT_SIZE-1);
 175
 176        /*
 177         * We have more at the beginning of the buffer
 178         */
 179        count = CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
 180        if (count) {
 181                console->write(console, info->xmit.buf, count);
 182                info->xmit.tail += count;
 183        }
 184out:
 185        local_irq_restore(flags);
 186}
 187
 188static void rs_flush_chars(struct tty_struct *tty)
 189{
 190        struct serial_state *info = tty->driver_data;
 191
 192        if (info->xmit.head == info->xmit.tail || tty->stopped ||
 193                        tty->hw_stopped || !info->xmit.buf)
 194                return;
 195
 196        transmit_chars(tty, info, NULL);
 197}
 198
 199static int rs_write(struct tty_struct * tty,
 200                    const unsigned char *buf, int count)
 201{
 202        struct serial_state *info = tty->driver_data;
 203        int     c, ret = 0;
 204        unsigned long flags;
 205
 206        if (!info->xmit.buf)
 207                return 0;
 208
 209        local_irq_save(flags);
 210        while (1) {
 211                c = CIRC_SPACE_TO_END(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
 212                if (count < c)
 213                        c = count;
 214                if (c <= 0) {
 215                        break;
 216                }
 217                memcpy(info->xmit.buf + info->xmit.head, buf, c);
 218                info->xmit.head = ((info->xmit.head + c) &
 219                                   (SERIAL_XMIT_SIZE-1));
 220                buf += c;
 221                count -= c;
 222                ret += c;
 223        }
 224        local_irq_restore(flags);
 225        /*
 226         * Hey, we transmit directly from here in our case
 227         */
 228        if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) &&
 229                        !tty->stopped && !tty->hw_stopped)
 230                transmit_chars(tty, info, NULL);
 231
 232        return ret;
 233}
 234
 235static int rs_write_room(struct tty_struct *tty)
 236{
 237        struct serial_state *info = tty->driver_data;
 238
 239        return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
 240}
 241
 242static int rs_chars_in_buffer(struct tty_struct *tty)
 243{
 244        struct serial_state *info = tty->driver_data;
 245
 246        return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
 247}
 248
 249static void rs_flush_buffer(struct tty_struct *tty)
 250{
 251        struct serial_state *info = tty->driver_data;
 252        unsigned long flags;
 253
 254        local_irq_save(flags);
 255        info->xmit.head = info->xmit.tail = 0;
 256        local_irq_restore(flags);
 257
 258        tty_wakeup(tty);
 259}
 260
 261/*
 262 * This function is used to send a high-priority XON/XOFF character to
 263 * the device
 264 */
 265static void rs_send_xchar(struct tty_struct *tty, char ch)
 266{
 267        struct serial_state *info = tty->driver_data;
 268
 269        info->x_char = ch;
 270        if (ch) {
 271                /*
 272                 * I guess we could call console->write() directly but
 273                 * let's do that for now.
 274                 */
 275                transmit_chars(tty, info, NULL);
 276        }
 277}
 278
 279/*
 280 * ------------------------------------------------------------
 281 * rs_throttle()
 282 *
 283 * This routine is called by the upper-layer tty layer to signal that
 284 * incoming characters should be throttled.
 285 * ------------------------------------------------------------
 286 */
 287static void rs_throttle(struct tty_struct * tty)
 288{
 289        if (I_IXOFF(tty))
 290                rs_send_xchar(tty, STOP_CHAR(tty));
 291
 292        printk(KERN_INFO "simrs_throttle called\n");
 293}
 294
 295static void rs_unthrottle(struct tty_struct * tty)
 296{
 297        struct serial_state *info = tty->driver_data;
 298
 299        if (I_IXOFF(tty)) {
 300                if (info->x_char)
 301                        info->x_char = 0;
 302                else
 303                        rs_send_xchar(tty, START_CHAR(tty));
 304        }
 305        printk(KERN_INFO "simrs_unthrottle called\n");
 306}
 307
 308static int rs_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
 309{
 310        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
 311            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
 312            (cmd != TIOCMIWAIT)) {
 313                if (tty->flags & (1 << TTY_IO_ERROR))
 314                    return -EIO;
 315        }
 316
 317        switch (cmd) {
 318        case TIOCGSERIAL:
 319        case TIOCSSERIAL:
 320        case TIOCSERGSTRUCT:
 321        case TIOCMIWAIT:
 322                return 0;
 323        case TIOCSERCONFIG:
 324        case TIOCSERGETLSR: /* Get line status register */
 325                return -EINVAL;
 326        case TIOCSERGWILD:
 327        case TIOCSERSWILD:
 328                /* "setserial -W" is called in Debian boot */
 329                printk (KERN_INFO "TIOCSER?WILD ioctl obsolete, ignored.\n");
 330                return 0;
 331        }
 332        return -ENOIOCTLCMD;
 333}
 334
 335#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
 336
 337static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
 338{
 339        /* Handle turning off CRTSCTS */
 340        if ((old_termios->c_cflag & CRTSCTS) &&
 341            !(tty->termios->c_cflag & CRTSCTS)) {
 342                tty->hw_stopped = 0;
 343        }
 344}
 345/*
 346 * This routine will shutdown a serial port; interrupts are disabled, and
 347 * DTR is dropped if the hangup on close termio flag is on.
 348 */
 349static void shutdown(struct tty_port *port)
 350{
 351        struct serial_state *info = container_of(port, struct serial_state,
 352                        port);
 353        unsigned long flags;
 354
 355        local_irq_save(flags);
 356        if (info->irq)
 357                free_irq(info->irq, info);
 358
 359        if (info->xmit.buf) {
 360                free_page((unsigned long) info->xmit.buf);
 361                info->xmit.buf = NULL;
 362        }
 363        local_irq_restore(flags);
 364}
 365
 366static void rs_close(struct tty_struct *tty, struct file * filp)
 367{
 368        struct serial_state *info = tty->driver_data;
 369
 370        tty_port_close(&info->port, tty, filp);
 371}
 372
 373static void rs_hangup(struct tty_struct *tty)
 374{
 375        struct serial_state *info = tty->driver_data;
 376
 377        rs_flush_buffer(tty);
 378        tty_port_hangup(&info->port);
 379}
 380
 381static int activate(struct tty_port *port, struct tty_struct *tty)
 382{
 383        struct serial_state *state = container_of(port, struct serial_state,
 384                        port);
 385        unsigned long flags, page;
 386        int retval = 0;
 387
 388        page = get_zeroed_page(GFP_KERNEL);
 389        if (!page)
 390                return -ENOMEM;
 391
 392        local_irq_save(flags);
 393
 394        if (state->xmit.buf)
 395                free_page(page);
 396        else
 397                state->xmit.buf = (unsigned char *) page;
 398
 399        if (state->irq) {
 400                retval = request_irq(state->irq, rs_interrupt_single, 0,
 401                                "simserial", state);
 402                if (retval)
 403                        goto errout;
 404        }
 405
 406        state->xmit.head = state->xmit.tail = 0;
 407
 408        /*
 409         * Set up the tty->alt_speed kludge
 410         */
 411        if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
 412                tty->alt_speed = 57600;
 413        if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
 414                tty->alt_speed = 115200;
 415        if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
 416                tty->alt_speed = 230400;
 417        if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
 418                tty->alt_speed = 460800;
 419
 420errout:
 421        local_irq_restore(flags);
 422        return retval;
 423}
 424
 425
 426/*
 427 * This routine is called whenever a serial port is opened.  It
 428 * enables interrupts for a serial port, linking in its async structure into
 429 * the IRQ chain.   It also performs the serial-specific
 430 * initialization for the tty structure.
 431 */
 432static int rs_open(struct tty_struct *tty, struct file * filp)
 433{
 434        struct serial_state *info = rs_table + tty->index;
 435        struct tty_port *port = &info->port;
 436
 437        tty->driver_data = info;
 438        tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
 439
 440        /*
 441         * figure out which console to use (should be one already)
 442         */
 443        console = console_drivers;
 444        while (console) {
 445                if ((console->flags & CON_ENABLED) && console->write) break;
 446                console = console->next;
 447        }
 448
 449        return tty_port_open(port, tty, filp);
 450}
 451
 452/*
 453 * /proc fs routines....
 454 */
 455
 456static int rs_proc_show(struct seq_file *m, void *v)
 457{
 458        int i;
 459
 460        seq_printf(m, "simserinfo:1.0\n");
 461        for (i = 0; i < NR_PORTS; i++)
 462                seq_printf(m, "%d: uart:16550 port:3F8 irq:%d\n",
 463                       i, rs_table[i].irq);
 464        return 0;
 465}
 466
 467static int rs_proc_open(struct inode *inode, struct file *file)
 468{
 469        return single_open(file, rs_proc_show, NULL);
 470}
 471
 472static const struct file_operations rs_proc_fops = {
 473        .owner          = THIS_MODULE,
 474        .open           = rs_proc_open,
 475        .read           = seq_read,
 476        .llseek         = seq_lseek,
 477        .release        = single_release,
 478};
 479
 480static const struct tty_operations hp_ops = {
 481        .open = rs_open,
 482        .close = rs_close,
 483        .write = rs_write,
 484        .put_char = rs_put_char,
 485        .flush_chars = rs_flush_chars,
 486        .write_room = rs_write_room,
 487        .chars_in_buffer = rs_chars_in_buffer,
 488        .flush_buffer = rs_flush_buffer,
 489        .ioctl = rs_ioctl,
 490        .throttle = rs_throttle,
 491        .unthrottle = rs_unthrottle,
 492        .send_xchar = rs_send_xchar,
 493        .set_termios = rs_set_termios,
 494        .hangup = rs_hangup,
 495        .proc_fops = &rs_proc_fops,
 496};
 497
 498static const struct tty_port_operations hp_port_ops = {
 499        .activate = activate,
 500        .shutdown = shutdown,
 501};
 502
 503static int __init simrs_init(void)
 504{
 505        struct serial_state *state;
 506        int retval;
 507
 508        if (!ia64_platform_is("hpsim"))
 509                return -ENODEV;
 510
 511        hp_simserial_driver = alloc_tty_driver(NR_PORTS);
 512        if (!hp_simserial_driver)
 513                return -ENOMEM;
 514
 515        printk(KERN_INFO "SimSerial driver with no serial options enabled\n");
 516
 517        /* Initialize the tty_driver structure */
 518
 519        hp_simserial_driver->driver_name = "simserial";
 520        hp_simserial_driver->name = "ttyS";
 521        hp_simserial_driver->major = TTY_MAJOR;
 522        hp_simserial_driver->minor_start = 64;
 523        hp_simserial_driver->type = TTY_DRIVER_TYPE_SERIAL;
 524        hp_simserial_driver->subtype = SERIAL_TYPE_NORMAL;
 525        hp_simserial_driver->init_termios = tty_std_termios;
 526        hp_simserial_driver->init_termios.c_cflag =
 527                B9600 | CS8 | CREAD | HUPCL | CLOCAL;
 528        hp_simserial_driver->flags = TTY_DRIVER_REAL_RAW;
 529        tty_set_operations(hp_simserial_driver, &hp_ops);
 530
 531        state = rs_table;
 532        tty_port_init(&state->port);
 533        state->port.ops = &hp_port_ops;
 534        state->port.close_delay = 0; /* XXX really 0? */
 535
 536        retval = hpsim_get_irq(KEYBOARD_INTR);
 537        if (retval < 0) {
 538                printk(KERN_ERR "%s: out of interrupt vectors!\n",
 539                                __func__);
 540                goto err_free_tty;
 541        }
 542
 543        state->irq = retval;
 544
 545        /* the port is imaginary */
 546        printk(KERN_INFO "ttyS0 at 0x03f8 (irq = %d) is a 16550\n", state->irq);
 547
 548        retval = tty_register_driver(hp_simserial_driver);
 549        if (retval) {
 550                printk(KERN_ERR "Couldn't register simserial driver\n");
 551                goto err_free_tty;
 552        }
 553
 554        return 0;
 555err_free_tty:
 556        put_tty_driver(hp_simserial_driver);
 557        return retval;
 558}
 559
 560#ifndef MODULE
 561__initcall(simrs_init);
 562#endif
 563