linux-old/drivers/char/pdc_console.c
<<
>>
Prefs
   1/*
   2 *  linux/drivers/char/pdc_console.c
   3 *
   4 *  2001, Christoph Plattner
   5 * 
   6 *  Driver template was linux's serial.c
   7 *
   8 */
   9
  10static char *pdc_drv_version = "0.3";
  11static char *pdc_drv_revdate = "2001-11-17";
  12#define AUTHOR "christoph.plattner@gmx.at"
  13#include <linux/config.h>
  14#include <linux/version.h>
  15
  16#undef PDC_DRV_DEBUG
  17
  18#undef SERIAL_PARANOIA_CHECK
  19#define CONFIG_SERIAL_NOPAUSE_IO
  20#define SERIAL_DO_RESTART
  21
  22#define PDC_POLL_DELAY (30 * HZ / 1000)
  23
  24/*
  25 * End of serial driver configuration section.
  26 */
  27
  28#include <linux/module.h>
  29
  30#include <linux/types.h>
  31#include <linux/serial.h>
  32#include <linux/serialP.h>
  33#include <linux/serial_reg.h>
  34#include <asm/serial.h>
  35#define LOCAL_VERSTRING ""
  36
  37#include <linux/errno.h>
  38#include <linux/signal.h>
  39#include <linux/sched.h>
  40#include <linux/timer.h>
  41#include <linux/interrupt.h>
  42#include <linux/tty.h>
  43#include <linux/tty_flip.h>
  44#include <linux/major.h>
  45#include <linux/string.h>
  46#include <linux/fcntl.h>
  47#include <linux/ptrace.h>
  48#include <linux/ioport.h>
  49#include <linux/mm.h>
  50#include <linux/slab.h>
  51#include <linux/init.h>
  52#include <asm/uaccess.h>
  53#include <linux/delay.h>
  54
  55#include <asm/system.h>
  56#include <asm/io.h>
  57#include <asm/irq.h>
  58#include <asm/bitops.h>
  59
  60#ifdef CONFIG_GSC
  61#include <asm/gsc.h>
  62#endif
  63
  64extern int pdc_console_poll_key(void *);
  65extern void pdc_outc(unsigned char);
  66
  67static char *pdc_drv_name = "PDC Software Console";
  68
  69static struct tty_driver pdc_drv_driver;
  70static int pdc_drv_refcount = 0;
  71static struct async_struct *pdc_drv_info;
  72
  73static struct timer_list pdc_drv_timer;
  74
  75/* serial subtype definitions */
  76#ifndef SERIAL_TYPE_NORMAL
  77#define SERIAL_TYPE_NORMAL      1
  78#define SERIAL_TYPE_CALLOUT     2
  79#endif
  80
  81#define NR_PORTS 1
  82#define PDC_DUMMY_BUF 2048
  83
  84static struct tty_struct *pdc_drv_table[NR_PORTS];
  85static struct termios *pdc_drv_termios[NR_PORTS];
  86static struct termios *pdc_drv_termios_locked[NR_PORTS];
  87
  88/*
  89 * tmp_buf is used as a temporary buffer by serial_write.  We need to
  90 * lock it in case the copy_from_user blocks while swapping in a page,
  91 * and some other program tries to do a serial write at the same time.
  92 * Since the lock will only come under contention when the system is
  93 * swapping and available memory is low, it makes sense to share one
  94 * buffer across all the serial ports, since it significantly saves
  95 * memory if large numbers of serial ports are open.
  96 */
  97static unsigned char *tmp_buf;
  98#ifdef DECLARE_MUTEX
  99static DECLARE_MUTEX(tmp_buf_sem);
 100#else
 101static struct semaphore tmp_buf_sem = MUTEX;
 102#endif
 103
 104/*
 105 * ------------------------------------------------------------
 106 * pdc_stop() and pdc_start()
 107 *
 108 * This routines are called before setting or resetting tty->stopped.
 109 * They enable or disable transmitter interrupts, as necessary.
 110 * ------------------------------------------------------------
 111 */
 112static void
 113pdc_stop(struct tty_struct *tty)
 114{
 115}
 116
 117static void
 118pdc_start(struct tty_struct *tty)
 119{
 120}
 121
 122/*
 123 * ----------------------------------------------------------------------
 124 *
 125 * Here starts the interrupt handling routines.  All of the following
 126 * subroutines are declared as inline and are folded into
 127 * rs_interrupt().  They were separated out for readability's sake.
 128 *
 129 * Note: rs_interrupt() is a "fast" interrupt, which means that it
 130 * runs with interrupts turned off.  People who may want to modify
 131 * rs_interrupt() should try to keep the interrupt handler as fast as
 132 * possible.  After you are done making modifications, it is not a bad
 133 * idea to do:
 134 * 
 135 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
 136 *
 137 * and look at the resulting assemble code in serial.s.
 138 *
 139 *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
 140 * -----------------------------------------------------------------------
 141 */
 142
 143static void
 144receive_chars(struct async_struct *info, int *status, struct pt_regs *regs)
 145{
 146        struct tty_struct *tty = info->tty;
 147        unsigned char ch;
 148        int __ch;
 149
 150        while (1) {
 151                __ch = pdc_console_poll_key(NULL);
 152
 153                if (__ch == -1) /* no character available */
 154                        break;
 155
 156                ch = (unsigned char) ((unsigned) __ch & 0x000000ffu);
 157
 158                if (tty->flip.count >= TTY_FLIPBUF_SIZE)
 159                        continue;
 160
 161                *tty->flip.char_buf_ptr = ch;
 162                *tty->flip.flag_buf_ptr = 0;
 163
 164                tty->flip.flag_buf_ptr++;
 165                tty->flip.char_buf_ptr++;
 166                tty->flip.count++;
 167        }
 168
 169        tty_flip_buffer_push(tty);
 170}
 171
 172static void
 173pdc_drv_poll(unsigned long dummy)
 174{
 175        struct async_struct *info;
 176        int status = 0;
 177
 178        info = pdc_drv_info;
 179
 180        if (!info || !info->tty || (pdc_drv_refcount == 0)) {
 181                /* do nothing */
 182        } else {
 183                receive_chars(info, &status, NULL);
 184                info->last_active = jiffies;
 185        }
 186
 187        mod_timer(&pdc_drv_timer, jiffies + PDC_POLL_DELAY);
 188}
 189
 190static void
 191pdc_put_char(struct tty_struct *tty, unsigned char ch)
 192{
 193#ifdef PDC_DRV_DEBUG
 194        printk(KERN_NOTICE "[%s] %c return\n", __FUNCTION__, ch);
 195#endif
 196        pdc_outc(ch);
 197}
 198
 199static void
 200pdc_flush_chars(struct tty_struct *tty)
 201{
 202        /* PCD console always flushed all characters */
 203
 204#ifdef PDC_DRV_DEBUG
 205        printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
 206#endif
 207
 208        /* nothing to do */
 209}
 210
 211static int
 212pdc_write(struct tty_struct *tty, int from_user,
 213          const unsigned char *buf, int count)
 214{
 215        char pdc_tmp_buf[PDC_DUMMY_BUF];
 216        char *pdc_tmp_buf_ptr;
 217        int len;
 218        int ret = 0;
 219
 220#ifdef PDC_DRV_DEBUG
 221        printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
 222#endif
 223        while (count) {
 224                if (count < PDC_DUMMY_BUF)
 225                        len = count;
 226                else
 227                        len = PDC_DUMMY_BUF;
 228
 229                if (from_user) {
 230                        copy_from_user(pdc_tmp_buf, buf, len);
 231                        pdc_tmp_buf_ptr = pdc_tmp_buf;
 232                } else
 233                        pdc_tmp_buf_ptr = (char *) buf;
 234
 235                while (len) {
 236                        pdc_outc(*pdc_tmp_buf_ptr);
 237                        buf++;
 238                        pdc_tmp_buf_ptr++;
 239                        ret++;
 240                        count--;
 241                        len--;
 242                }
 243        }
 244#ifdef PDC_DRV_DEBUG
 245        printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
 246#endif
 247        return ret;
 248}
 249
 250static int
 251pdc_write_room(struct tty_struct *tty)
 252{
 253#ifdef PDC_DRV_DEBUG
 254        printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
 255#endif
 256        return PDC_DUMMY_BUF;
 257}
 258
 259static int
 260pdc_chars_in_buffer(struct tty_struct *tty)
 261{
 262#ifdef PDC_DRV_DEBUG
 263        printk(KERN_NOTICE "[%s] entry\n", __FUNCTION__);
 264#endif
 265        return 0;               /* no characters in buffer, always flushed ! */
 266}
 267
 268static void
 269pdc_flush_buffer(struct tty_struct *tty)
 270{
 271#ifdef PDC_DRV_DEBUG
 272        printk(KERN_NOTICE "[%s] return\n", __FUNCTION__);
 273#endif
 274}
 275
 276/*
 277 * This function is used to send a high-priority XON/XOFF character to
 278 * the device
 279 */
 280static void
 281pdc_send_xchar(struct tty_struct *tty, char ch)
 282{
 283}
 284
 285/*
 286 * ------------------------------------------------------------
 287 * pdc_throttle()
 288 * 
 289 * This routine is called by the upper-layer tty layer to signal that
 290 * incoming characters should be throttled.
 291 * ------------------------------------------------------------
 292 */
 293static void
 294pdc_throttle(struct tty_struct *tty)
 295{
 296}
 297
 298static void
 299pdc_unthrottle(struct tty_struct *tty)
 300{
 301}
 302
 303/*
 304 * ------------------------------------------------------------
 305 * pdc_ioctl() and friends
 306 * ------------------------------------------------------------
 307 */
 308
 309static void
 310pdc_break(struct tty_struct *tty, int break_state)
 311{
 312}
 313
 314static int
 315get_serial_info(struct async_struct * info,
 316                           struct serial_struct * retinfo)
 317{
 318        struct serial_struct tmp;
 319
 320        if (!retinfo)
 321                return -EFAULT;
 322        memset(&tmp, 0, sizeof(tmp));
 323        tmp.line = info->line;
 324        tmp.port = info->line;
 325        tmp.flags = info->flags;
 326        tmp.close_delay = info->close_delay;
 327        return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
 328}
 329
 330static int get_modem_info(struct async_struct * info, unsigned int *value)
 331{
 332        unsigned int result = TIOCM_DTR|TIOCM_CAR|TIOCM_CTS|TIOCM_RTS;
 333
 334        return copy_to_user(value, &result, sizeof(int)) ? -EFAULT : 0;
 335}
 336
 337static int get_lsr_info(struct async_struct * info, unsigned int *value)
 338{
 339        unsigned int result = TIOCSER_TEMT;
 340
 341        return copy_to_user(value, &result, sizeof(int)) ? -EFAULT : 0;
 342}
 343
 344static int
 345pdc_ioctl(struct tty_struct *tty, struct file *file,
 346          unsigned int cmd, unsigned long arg)
 347{
 348        struct async_struct *info = (struct async_struct *) tty->driver_data;
 349
 350        if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
 351            (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
 352            (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
 353                if (tty->flags & (1 << TTY_IO_ERROR))
 354                        return -EIO;
 355        }
 356
 357        switch (cmd) {
 358        case TIOCMGET:
 359                return get_modem_info(info, (unsigned int *) arg);
 360        case TIOCMBIS:
 361        case TIOCMBIC:
 362        case TIOCMSET:
 363                return 0;
 364        case TIOCGSERIAL:
 365                return get_serial_info(info, (struct serial_struct *) arg);
 366        case TIOCSSERIAL:
 367                return 0;
 368        case TIOCSERCONFIG:
 369                return 0;
 370
 371        case TIOCSERGETLSR:     /* Get line status register */
 372                return get_lsr_info(info, (unsigned int *) arg);
 373
 374        case TIOCSERGSTRUCT:
 375                if (copy_to_user((struct async_struct *) arg,
 376                                 info, sizeof (struct async_struct)))
 377                        return -EFAULT;
 378                return 0;
 379
 380        case TIOCMIWAIT:
 381                return 0;
 382
 383        case TIOCGICOUNT:
 384                return 0;
 385        case TIOCSERGWILD:
 386        case TIOCSERSWILD:
 387                /* "setserial -W" is called in Debian boot */
 388                printk("TIOCSER?WILD ioctl obsolete, ignored.\n");
 389                return 0;
 390
 391        default:
 392                return -ENOIOCTLCMD;
 393        }
 394        return 0;
 395}
 396
 397static void
 398pdc_set_termios(struct tty_struct *tty, struct termios *old_termios)
 399{
 400
 401#if 0                           /* XXX CP, has to be checked, if there is stuff to do */
 402        struct async_struct *info = (struct async_struct *) tty->driver_data;
 403        unsigned long flags;
 404        unsigned int cflag = tty->termios->c_cflag;
 405
 406        if ((cflag == old_termios->c_cflag)
 407            && (RELEVANT_IFLAG(tty->termios->c_iflag)
 408                == RELEVANT_IFLAG(old_termios->c_iflag)))
 409                return;
 410#if 0
 411        change_speed(info, old_termios);
 412#endif
 413        /* Handle turning off CRTSCTS */
 414        if ((old_termios->c_cflag & CRTSCTS) &&
 415            !(tty->termios->c_cflag & CRTSCTS)) {
 416                tty->hw_stopped = 0;
 417                pdc_start(tty);
 418        }
 419#endif
 420}
 421
 422/*
 423 * ------------------------------------------------------------
 424 * pdc_close()
 425 * 
 426 * This routine is called when the serial port gets closed.  First, we
 427 * wait for the last remaining data to be sent.  Then, we unlink its
 428 * async structure from the interrupt chain if necessary, and we free
 429 * that IRQ if nothing is left in the chain.
 430 * ------------------------------------------------------------
 431 */
 432static void
 433pdc_close(struct tty_struct *tty, struct file *filp)
 434{
 435        struct async_struct *info = (struct async_struct *) tty->driver_data;
 436
 437#ifdef PDC_DEBUG_OPEN
 438        printk("pdc_close ttyB%d, count = %d\n", info->line, state->count);
 439#endif
 440        pdc_drv_refcount--;
 441        if (pdc_drv_refcount > 0)
 442                return;
 443
 444        info->flags |= ASYNC_CLOSING;
 445
 446        /*
 447         * Save the termios structure, since this port may have
 448         * separate termios for callout and dialin.
 449         */
 450        if (info->flags & ASYNC_NORMAL_ACTIVE)
 451                info->state->normal_termios = *tty->termios;
 452        if (info->flags & ASYNC_CALLOUT_ACTIVE)
 453                info->state->callout_termios = *tty->termios;
 454
 455        /*
 456         * At this point we stop accepting input.  To do this, we
 457         * disable the receive line status interrupts, and tell the
 458         * interrupt driver to stop checking the data ready bit in the
 459         * line status register.
 460         */
 461
 462        /* XXX CP: make mask for receive !!! */
 463
 464        if (tty->driver.flush_buffer)
 465                tty->driver.flush_buffer(tty);
 466        tty_ldisc_flush(tty);
 467        tty->closing = 0;
 468        info->event = 0;
 469        info->tty = 0;
 470        pdc_drv_info = NULL;
 471        if (info->blocked_open) {
 472                if (info->close_delay) {
 473                        set_current_state(TASK_INTERRUPTIBLE);
 474                        schedule_timeout(info->close_delay);
 475                }
 476                wake_up_interruptible(&info->open_wait);
 477        }
 478        info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
 479                         ASYNC_CLOSING);
 480        wake_up_interruptible(&info->close_wait);
 481        MOD_DEC_USE_COUNT;
 482}
 483
 484/*
 485 * pdc_wait_until_sent() --- wait until the transmitter is empty
 486 */
 487static void
 488pdc_wait_until_sent(struct tty_struct *tty, int timeout)
 489{
 490        /* we always send immideate */
 491}
 492
 493/*
 494 * pdc_hangup() --- called by tty_hangup() when a hangup is signaled.
 495 */
 496static void
 497pdc_hangup(struct tty_struct *tty)
 498{
 499}
 500
 501/*
 502 * ------------------------------------------------------------
 503 * pdc_open() and friends
 504 * ------------------------------------------------------------
 505 */
 506
 507static int
 508get_async_struct(int line, struct async_struct **ret_info)
 509{
 510        struct async_struct *info;
 511
 512        info = kmalloc(sizeof (struct async_struct), GFP_KERNEL);
 513        if (!info) {
 514                return -ENOMEM;
 515        }
 516        memset(info, 0, sizeof (struct async_struct));
 517        init_waitqueue_head(&info->open_wait);
 518        init_waitqueue_head(&info->close_wait);
 519        init_waitqueue_head(&info->delta_msr_wait);
 520        info->magic = SERIAL_MAGIC;
 521        info->port = 0;
 522        info->flags = 0;
 523        info->io_type = 0;
 524        info->iomem_base = 0;
 525        info->iomem_reg_shift = 0;
 526        info->xmit_fifo_size = PDC_DUMMY_BUF;
 527        info->line = line;
 528        info->tqueue.routine = NULL;
 529        info->tqueue.data = info;
 530        info->state = NULL;
 531        *ret_info = info;
 532        return 0;
 533}
 534
 535/*
 536 * This routine is called whenever a serial port is opened.  It
 537 * enables interrupts for a serial port, linking in its async structure into
 538 * the IRQ chain.   It also performs the serial-specific
 539 * initialization for the tty structure.
 540 */
 541static int
 542pdc_open(struct tty_struct *tty, struct file *filp)
 543{
 544        struct async_struct *info;
 545        int retval, line;
 546        unsigned long page;
 547
 548        MOD_INC_USE_COUNT;
 549        line = MINOR(tty->device) - tty->driver.minor_start;
 550        if ((line < 0) || (line >= NR_PORTS)) {
 551                MOD_DEC_USE_COUNT;
 552                return -ENODEV;
 553        }
 554        retval = get_async_struct(line, &info);
 555        if (retval) {
 556                MOD_DEC_USE_COUNT;
 557                return retval;
 558        }
 559        tty->driver_data = info;
 560        info->tty = tty;
 561        pdc_drv_info = info;
 562
 563#ifdef PDC_DEBUG_OPEN
 564        printk("pdc_open %s%d, count = %d\n", tty->driver.name, info->line,
 565               info->state->count);
 566#endif
 567        info->tty->low_latency = 0;
 568        if (!tmp_buf) {
 569                page = get_zeroed_page(GFP_KERNEL);
 570                if (!page) {
 571                        MOD_DEC_USE_COUNT;
 572                        return -ENOMEM;
 573                }
 574                if (tmp_buf)
 575                        free_page(page);
 576                else
 577                        tmp_buf = (unsigned char *) page;
 578        }
 579
 580        info->session = current->session;
 581        info->pgrp = current->pgrp;
 582
 583#ifdef PDC_DEBUG_OPEN
 584        printk("pdc_open ttyB%d successful...", info->line);
 585#endif
 586        pdc_drv_refcount++;
 587        return 0;
 588}
 589
 590/*
 591 * ---------------------------------------------------------------------
 592 * pdc_init() and friends
 593 *
 594 * pdc_init() is called at boot-time to initialize the pdc driver.
 595 * ---------------------------------------------------------------------
 596 */
 597
 598static void
 599show_pdc_drv_version(void)
 600{
 601        printk(KERN_INFO "%s version %s%s (%s), %s\n", pdc_drv_name,
 602               pdc_drv_version, LOCAL_VERSTRING, pdc_drv_revdate, AUTHOR);
 603}
 604
 605/*
 606 * The serial driver boot-time initialization code!
 607 */
 608static int __init
 609pdc_drv_init(void)
 610{
 611        init_timer(&pdc_drv_timer);
 612        pdc_drv_timer.function = pdc_drv_poll;
 613        mod_timer(&pdc_drv_timer, jiffies + PDC_POLL_DELAY);
 614
 615        show_pdc_drv_version();
 616
 617        /* Initialize the tty_driver structure */
 618
 619        memset(&pdc_drv_driver, 0, sizeof (struct tty_driver));
 620        pdc_drv_driver.magic = TTY_DRIVER_MAGIC;
 621        pdc_drv_driver.driver_name = "pdc_console";
 622#ifdef CONFIG_DEVFS_FS
 623        pdc_drv_driver.name = "ttb/%d";
 624#else
 625        pdc_drv_driver.name = "ttyB";
 626#endif
 627        pdc_drv_driver.major = MUX_MAJOR;
 628        pdc_drv_driver.minor_start = 0;
 629        pdc_drv_driver.num = NR_PORTS;
 630        pdc_drv_driver.type = TTY_DRIVER_TYPE_SERIAL;
 631        pdc_drv_driver.subtype = SERIAL_TYPE_NORMAL;
 632        pdc_drv_driver.init_termios = tty_std_termios;
 633        pdc_drv_driver.init_termios.c_cflag =
 634            B9600 | CS8 | CREAD | HUPCL | CLOCAL;
 635        pdc_drv_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
 636        pdc_drv_driver.refcount = &pdc_drv_refcount;
 637        pdc_drv_driver.table = pdc_drv_table;
 638        pdc_drv_driver.termios = pdc_drv_termios;
 639        pdc_drv_driver.termios_locked = pdc_drv_termios_locked;
 640
 641        pdc_drv_driver.open = pdc_open;
 642        pdc_drv_driver.close = pdc_close;
 643        pdc_drv_driver.write = pdc_write;
 644        pdc_drv_driver.put_char = pdc_put_char;
 645        pdc_drv_driver.flush_chars = pdc_flush_chars;
 646        pdc_drv_driver.write_room = pdc_write_room;
 647        pdc_drv_driver.chars_in_buffer = pdc_chars_in_buffer;
 648        pdc_drv_driver.flush_buffer = pdc_flush_buffer;
 649        pdc_drv_driver.ioctl = pdc_ioctl;
 650        pdc_drv_driver.throttle = pdc_throttle;
 651        pdc_drv_driver.unthrottle = pdc_unthrottle;
 652        pdc_drv_driver.set_termios = pdc_set_termios;
 653        pdc_drv_driver.stop = pdc_stop;
 654        pdc_drv_driver.start = pdc_start;
 655        pdc_drv_driver.hangup = pdc_hangup;
 656        pdc_drv_driver.break_ctl = pdc_break;
 657        pdc_drv_driver.send_xchar = pdc_send_xchar;
 658        pdc_drv_driver.wait_until_sent = pdc_wait_until_sent;
 659        pdc_drv_driver.read_proc = NULL;
 660
 661        if (tty_register_driver(&pdc_drv_driver))
 662                panic("Couldn't register pdc_console driver\n");
 663
 664        return 0;
 665}
 666
 667static void __exit
 668pdc_fini(void)
 669{
 670        int e1;
 671
 672        if ((e1 = tty_unregister_driver(&pdc_drv_driver)))
 673                printk("pdc_console: failed to unregister pdc_drv driver (%d)\n",
 674                       e1);
 675}
 676
 677module_init(pdc_drv_init);
 678module_exit(pdc_fini);
 679MODULE_DESCRIPTION("PDC Software Console");
 680MODULE_AUTHOR(AUTHOR);
 681
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.