Advertisement
Guest User

Samsung VDLinux n_tty.c

a guest
Nov 11th, 2012
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 61.87 KB | None | 0 0
  1. /*
  2.  * n_tty.c --- implements the N_TTY line discipline.
  3.  *
  4.  * This code used to be in tty_io.c, but things are getting hairy
  5.  * enough that it made sense to split things off.  (The N_TTY
  6.  * processing has changed so much that it's hardly recognizable,
  7.  * anyway...)
  8.  *
  9.  * Note that the open routine for N_TTY is guaranteed never to return
  10.  * an error.  This is because Linux will fall back to setting a line
  11.  * to N_TTY if it can not switch to any other line discipline.
  12.  *
  13.  * Written by Theodore Ts'o, Copyright 1994.
  14.  *
  15.  * This file also contains code originally written by Linus Torvalds,
  16.  * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
  17.  *
  18.  * This file may be redistributed under the terms of the GNU General Public
  19.  * License.
  20.  *
  21.  * Reduced memory usage for older ARM systems  - Russell King.
  22.  *
  23.  * 2000/01/20   Fixed SMP locking on put_tty_queue using bits of
  24.  *      the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
  25.  *      who actually finally proved there really was a race.
  26.  *
  27.  * 2002/03/18   Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
  28.  *      waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
  29.  *      Also fixed a bug in BLOCKING mode where n_tty_write returns
  30.  *      EAGAIN
  31.  */
  32.  
  33. #include <linux/types.h>
  34. #include <linux/major.h>
  35. #include <linux/errno.h>
  36. #include <linux/signal.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/sched.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/tty.h>
  41. #include <linux/timer.h>
  42. #include <linux/ctype.h>
  43. #include <linux/mm.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/poll.h>
  47. #include <linux/bitops.h>
  48. #include <linux/audit.h>
  49. #include <linux/file.h>
  50. #include <linux/uaccess.h>
  51. #include <linux/module.h>
  52.  
  53. #include <asm/system.h>
  54.  
  55. #ifdef CONFIG_KDEBUGD
  56. #include <kdebugd/kdebugd.h>
  57. #endif
  58.  
  59. /* number of characters left in xmit buffer before select has we have room */
  60. #define WAKEUP_CHARS 256
  61.  
  62. /*
  63.  * This defines the low- and high-watermarks for throttling and
  64.  * unthrottling the TTY driver.  These watermarks are used for
  65.  * controlling the space in the read buffer.
  66.  */
  67. #define TTY_THRESHOLD_THROTTLE      128 /* now based on remaining room */
  68. #define TTY_THRESHOLD_UNTHROTTLE    128
  69.  
  70. /*
  71.  * Special byte codes used in the echo buffer to represent operations
  72.  * or special handling of characters.  Bytes in the echo buffer that
  73.  * are not part of such special blocks are treated as normal character
  74.  * codes.
  75.  */
  76. #define ECHO_OP_START 0xff
  77. #define ECHO_OP_MOVE_BACK_COL 0x80
  78. #define ECHO_OP_SET_CANON_COL 0x81
  79. #define ECHO_OP_ERASE_TAB 0x82
  80.  
  81. /* VDLinux, based SELP.Mstar default patch No.15,
  82.    n_tty serial input disable, SP Team 2010-01-29 */
  83. #ifdef CONFIG_SERIAL_INPUT_MANIPULATION
  84.  
  85. #define MAX_STRING_SIZE 10
  86. static unsigned char enable_serial[MAX_STRING_SIZE];
  87. static unsigned char disable_serial[MAX_STRING_SIZE];
  88.  
  89. static int enable_string_size=0;
  90. static int disable_string_size=0;
  91.  
  92. static int enable_index = 0;
  93. static int disable_index = 0;
  94.  
  95. #define SERIAL_INPUT_DISABLE 0
  96. #define SERIAL_INPUT_ENABLE 1
  97.  
  98. #ifdef CONFIG_SERIAL_INPUT_DEFAULT_SETUP_ENABLE
  99. static int serial_enable = SERIAL_INPUT_ENABLE;
  100. #else
  101. static int serial_enable = SERIAL_INPUT_DISABLE;
  102. #endif
  103.  
  104. struct tty_struct *INPUT_tty;
  105. #endif
  106.  
  107. #ifdef CONFIG_SERIAL_INPUT_ENABLE_ONLY_NUMBER
  108. #define MAX_ARRAY 19
  109. int allow_char[] = { 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 /* 0~9 */,
  110.                                                 65, 66, 67, 68, 69, 70 /* A~F */,
  111.                                                 32 /*space*/,
  112.                                                 13 /*enter*/,
  113.                                                 8  /*backspace*/ };
  114. #endif
  115.  
  116. static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
  117.                    unsigned char __user *ptr)
  118. {
  119.     tty_audit_add_data(tty, &x, 1);
  120.     return put_user(x, ptr);
  121. }
  122.  
  123. /**
  124.  *  n_tty_set__room -   receive space
  125.  *  @tty: terminal
  126.  *
  127.  *  Called by the driver to find out how much data it is
  128.  *  permitted to feed to the line discipline without any being lost
  129.  *  and thus to manage flow control. Not serialized. Answers for the
  130.  *  "instant".
  131.  */
  132.  
  133. static void n_tty_set_room(struct tty_struct *tty)
  134. {
  135.     /* tty->read_cnt is not read locked ? */
  136.     int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
  137.  
  138.     /*
  139.      * If we are doing input canonicalization, and there are no
  140.      * pending newlines, let characters through without limit, so
  141.      * that erase characters will be handled.  Other excess
  142.      * characters will be beeped.
  143.      */
  144.     if (left <= 0)
  145.         left = tty->icanon && !tty->canon_data;
  146.     tty->receive_room = left;
  147. }
  148.  
  149. static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
  150. {
  151.     if (tty->read_cnt < N_TTY_BUF_SIZE) {
  152.         tty->read_buf[tty->read_head] = c;
  153.         tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
  154.         tty->read_cnt++;
  155.     }
  156. }
  157.  
  158. /**
  159.  *  put_tty_queue       -   add character to tty
  160.  *  @c: character
  161.  *  @tty: tty device
  162.  *
  163.  *  Add a character to the tty read_buf queue. This is done under the
  164.  *  read_lock to serialize character addition and also to protect us
  165.  *  against parallel reads or flushes
  166.  */
  167.  
  168. static void put_tty_queue(unsigned char c, struct tty_struct *tty)
  169. {
  170.     unsigned long flags;
  171.     /*
  172.      *  The problem of stomping on the buffers ends here.
  173.      *  Why didn't anyone see this one coming? --AJK
  174.     */
  175.     spin_lock_irqsave(&tty->read_lock, flags);
  176.     put_tty_queue_nolock(c, tty);
  177.     spin_unlock_irqrestore(&tty->read_lock, flags);
  178. }
  179.  
  180. /**
  181.  *  check_unthrottle    -   allow new receive data
  182.  *  @tty; tty device
  183.  *
  184.  *  Check whether to call the driver unthrottle functions
  185.  *
  186.  *  Can sleep, may be called under the atomic_read_lock mutex but
  187.  *  this is not guaranteed.
  188.  */
  189. static void check_unthrottle(struct tty_struct *tty)
  190. {
  191.     if (tty->count)
  192.         tty_unthrottle(tty);
  193. }
  194.  
  195. /**
  196.  *  reset_buffer_flags  -   reset buffer state
  197.  *  @tty: terminal to reset
  198.  *
  199.  *  Reset the read buffer counters, clear the flags,
  200.  *  and make sure the driver is unthrottled. Called
  201.  *  from n_tty_open() and n_tty_flush_buffer().
  202.  *
  203.  *  Locking: tty_read_lock for read fields.
  204.  */
  205.  
  206. static void reset_buffer_flags(struct tty_struct *tty)
  207. {
  208.     unsigned long flags;
  209.  
  210.     spin_lock_irqsave(&tty->read_lock, flags);
  211.     tty->read_head = tty->read_tail = tty->read_cnt = 0;
  212.     spin_unlock_irqrestore(&tty->read_lock, flags);
  213.  
  214.     mutex_lock(&tty->echo_lock);
  215.     tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
  216.     mutex_unlock(&tty->echo_lock);
  217.  
  218.     tty->canon_head = tty->canon_data = tty->erasing = 0;
  219.     memset(&tty->read_flags, 0, sizeof tty->read_flags);
  220.     n_tty_set_room(tty);
  221.     check_unthrottle(tty);
  222. }
  223.  
  224. /**
  225.  *  n_tty_flush_buffer  -   clean input queue
  226.  *  @tty:   terminal device
  227.  *
  228.  *  Flush the input buffer. Called when the line discipline is
  229.  *  being closed, when the tty layer wants the buffer flushed (eg
  230.  *  at hangup) or when the N_TTY line discipline internally has to
  231.  *  clean the pending queue (for example some signals).
  232.  *
  233.  *  Locking: ctrl_lock, read_lock.
  234.  */
  235.  
  236. static void n_tty_flush_buffer(struct tty_struct *tty)
  237. {
  238.     unsigned long flags;
  239.     /* clear everything and unthrottle the driver */
  240.     reset_buffer_flags(tty);
  241.  
  242.     if (!tty->link)
  243.         return;
  244.  
  245.     spin_lock_irqsave(&tty->ctrl_lock, flags);
  246.     if (tty->link->packet) {
  247.         tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  248.         wake_up_interruptible(&tty->link->read_wait);
  249.     }
  250.     spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  251. }
  252.  
  253. /**
  254.  *  n_tty_chars_in_buffer   -   report available bytes
  255.  *  @tty: tty device
  256.  *
  257.  *  Report the number of characters buffered to be delivered to user
  258.  *  at this instant in time.
  259.  *
  260.  *  Locking: read_lock
  261.  */
  262.  
  263. static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  264. {
  265.     unsigned long flags;
  266.     ssize_t n = 0;
  267.  
  268.     spin_lock_irqsave(&tty->read_lock, flags);
  269.     if (!tty->icanon) {
  270.         n = tty->read_cnt;
  271.     } else if (tty->canon_data) {
  272.         n = (tty->canon_head > tty->read_tail) ?
  273.             tty->canon_head - tty->read_tail :
  274.             tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
  275.     }
  276.     spin_unlock_irqrestore(&tty->read_lock, flags);
  277.     return n;
  278. }
  279.  
  280. /**
  281.  *  is_utf8_continuation    -   utf8 multibyte check
  282.  *  @c: byte to check
  283.  *
  284.  *  Returns true if the utf8 character 'c' is a multibyte continuation
  285.  *  character. We use this to correctly compute the on screen size
  286.  *  of the character when printing
  287.  */
  288.  
  289. static inline int is_utf8_continuation(unsigned char c)
  290. {
  291.     return (c & 0xc0) == 0x80;
  292. }
  293.  
  294. /**
  295.  *  is_continuation     -   multibyte check
  296.  *  @c: byte to check
  297.  *
  298.  *  Returns true if the utf8 character 'c' is a multibyte continuation
  299.  *  character and the terminal is in unicode mode.
  300.  */
  301.  
  302. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  303. {
  304.     return I_IUTF8(tty) && is_utf8_continuation(c);
  305. }
  306.  
  307. /**
  308.  *  do_output_char          -   output one character
  309.  *  @c: character (or partial unicode symbol)
  310.  *  @tty: terminal device
  311.  *  @space: space available in tty driver write buffer
  312.  *
  313.  *  This is a helper function that handles one output character
  314.  *  (including special characters like TAB, CR, LF, etc.),
  315.  *  doing OPOST processing and putting the results in the
  316.  *  tty driver's write buffer.
  317.  *
  318.  *  Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
  319.  *  and NLDLY.  They simply aren't relevant in the world today.
  320.  *  If you ever need them, add them here.
  321.  *
  322.  *  Returns the number of bytes of buffer space used or -1 if
  323.  *  no space left.
  324.  *
  325.  *  Locking: should be called under the output_lock to protect
  326.  *       the column state and space left in the buffer
  327.  */
  328.  
  329. static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
  330. {
  331.     int spaces;
  332.  
  333.     if (!space)
  334.         return -1;
  335.  
  336.     switch (c) {
  337.     case '\n':
  338.         if (O_ONLRET(tty))
  339.             tty->column = 0;
  340.         if (O_ONLCR(tty)) {
  341.             if (space < 2)
  342.                 return -1;
  343.             tty->canon_column = tty->column = 0;
  344.             tty->ops->write(tty, "\r\n", 2);
  345.             return 2;
  346.         }
  347.         tty->canon_column = tty->column;
  348.         break;
  349.     case '\r':
  350.         if (O_ONOCR(tty) && tty->column == 0)
  351.             return 0;
  352.         if (O_OCRNL(tty)) {
  353.             c = '\n';
  354.             if (O_ONLRET(tty))
  355.                 tty->canon_column = tty->column = 0;
  356.             break;
  357.         }
  358.         tty->canon_column = tty->column = 0;
  359.         break;
  360.     case '\t':
  361.         spaces = 8 - (tty->column & 7);
  362.         if (O_TABDLY(tty) == XTABS) {
  363.             if (space < spaces)
  364.                 return -1;
  365.             tty->column += spaces;
  366.             tty->ops->write(tty, "        ", spaces);
  367.             return spaces;
  368.         }
  369.         tty->column += spaces;
  370.         break;
  371.     case '\b':
  372.         if (tty->column > 0)
  373.             tty->column--;
  374.         break;
  375.     default:
  376.         if (!iscntrl(c)) {
  377.             if (O_OLCUC(tty))
  378.                 c = toupper(c);
  379.             if (!is_continuation(c, tty))
  380.                 tty->column++;
  381.         }
  382.         break;
  383.     }
  384.  
  385.     tty_put_char(tty, c);
  386.     return 1;
  387. }
  388.  
  389. /**
  390.  *  process_output          -   output post processor
  391.  *  @c: character (or partial unicode symbol)
  392.  *  @tty: terminal device
  393.  *
  394.  *  Output one character with OPOST processing.
  395.  *  Returns -1 when the output device is full and the character
  396.  *  must be retried.
  397.  *
  398.  *  Locking: output_lock to protect column state and space left
  399.  *       (also, this is called from n_tty_write under the
  400.  *        tty layer write lock)
  401.  */
  402.  
  403. static int process_output(unsigned char c, struct tty_struct *tty)
  404. {
  405.     int space, retval;
  406.  
  407.     mutex_lock(&tty->output_lock);
  408.  
  409.     space = tty_write_room(tty);
  410.     retval = do_output_char(c, tty, space);
  411.  
  412.     mutex_unlock(&tty->output_lock);
  413.     if (retval < 0)
  414.         return -1;
  415.     else
  416.         return 0;
  417. }
  418.  
  419. /**
  420.  *  process_output_block        -   block post processor
  421.  *  @tty: terminal device
  422.  *  @buf: character buffer
  423.  *  @nr: number of bytes to output
  424.  *
  425.  *  Output a block of characters with OPOST processing.
  426.  *  Returns the number of characters output.
  427.  *
  428.  *  This path is used to speed up block console writes, among other
  429.  *  things when processing blocks of output data. It handles only
  430.  *  the simple cases normally found and helps to generate blocks of
  431.  *  symbols for the console driver and thus improve performance.
  432.  *
  433.  *  Locking: output_lock to protect column state and space left
  434.  *       (also, this is called from n_tty_write under the
  435.  *        tty layer write lock)
  436.  */
  437.  
  438. static ssize_t process_output_block(struct tty_struct *tty,
  439.                     const unsigned char *buf, unsigned int nr)
  440. {
  441.     int space;
  442.     int     i;
  443.     const unsigned char *cp;
  444.  
  445.     mutex_lock(&tty->output_lock);
  446.  
  447.     space = tty_write_room(tty);
  448.     if (!space) {
  449.         mutex_unlock(&tty->output_lock);
  450.         return 0;
  451.     }
  452.     if (nr > space)
  453.         nr = space;
  454.  
  455.     for (i = 0, cp = buf; i < nr; i++, cp++) {
  456.         unsigned char c = *cp;
  457.  
  458.         switch (c) {
  459.         case '\n':
  460.             if (O_ONLRET(tty))
  461.                 tty->column = 0;
  462.             if (O_ONLCR(tty))
  463.                 goto break_out;
  464.             tty->canon_column = tty->column;
  465.             break;
  466.         case '\r':
  467.             if (O_ONOCR(tty) && tty->column == 0)
  468.                 goto break_out;
  469.             if (O_OCRNL(tty))
  470.                 goto break_out;
  471.             tty->canon_column = tty->column = 0;
  472.             break;
  473.         case '\t':
  474.             goto break_out;
  475.         case '\b':
  476.             if (tty->column > 0)
  477.                 tty->column--;
  478.             break;
  479.         default:
  480.             if (!iscntrl(c)) {
  481.                 if (O_OLCUC(tty))
  482.                     goto break_out;
  483.                 if (!is_continuation(c, tty))
  484.                     tty->column++;
  485.             }
  486.             break;
  487.         }
  488.     }
  489. break_out:
  490.     i = tty->ops->write(tty, buf, i);
  491.  
  492.     mutex_unlock(&tty->output_lock);
  493.     return i;
  494. }
  495.  
  496. /**
  497.  *  process_echoes  -   write pending echo characters
  498.  *  @tty: terminal device
  499.  *
  500.  *  Write previously buffered echo (and other ldisc-generated)
  501.  *  characters to the tty.
  502.  *
  503.  *  Characters generated by the ldisc (including echoes) need to
  504.  *  be buffered because the driver's write buffer can fill during
  505.  *  heavy program output.  Echoing straight to the driver will
  506.  *  often fail under these conditions, causing lost characters and
  507.  *  resulting mismatches of ldisc state information.
  508.  *
  509.  *  Since the ldisc state must represent the characters actually sent
  510.  *  to the driver at the time of the write, operations like certain
  511.  *  changes in column state are also saved in the buffer and executed
  512.  *  here.
  513.  *
  514.  *  A circular fifo buffer is used so that the most recent characters
  515.  *  are prioritized.  Also, when control characters are echoed with a
  516.  *  prefixed "^", the pair is treated atomically and thus not separated.
  517.  *
  518.  *  Locking: output_lock to protect column state and space left,
  519.  *       echo_lock to protect the echo buffer
  520.  */
  521.  
  522. static void process_echoes(struct tty_struct *tty)
  523. {
  524.     int space, nr;
  525.     unsigned char c;
  526.     unsigned char *cp, *buf_end;
  527.  
  528.     if (!tty->echo_cnt)
  529.         return;
  530.  
  531.     mutex_lock(&tty->output_lock);
  532.     mutex_lock(&tty->echo_lock);
  533.  
  534.     space = tty_write_room(tty);
  535.  
  536.     buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
  537.     cp = tty->echo_buf + tty->echo_pos;
  538.     nr = tty->echo_cnt;
  539.     while (nr > 0) {
  540.         c = *cp;
  541.         if (c == ECHO_OP_START) {
  542.             unsigned char op;
  543.             unsigned char *opp;
  544.             int no_space_left = 0;
  545.  
  546.             /*
  547.              * If the buffer byte is the start of a multi-byte
  548.              * operation, get the next byte, which is either the
  549.              * op code or a control character value.
  550.              */
  551.             opp = cp + 1;
  552.             if (opp == buf_end)
  553.                 opp -= N_TTY_BUF_SIZE;
  554.             op = *opp;
  555.  
  556.             switch (op) {
  557.                 unsigned int num_chars, num_bs;
  558.  
  559.             case ECHO_OP_ERASE_TAB:
  560.                 if (++opp == buf_end)
  561.                     opp -= N_TTY_BUF_SIZE;
  562.                 num_chars = *opp;
  563.  
  564.                 /*
  565.                  * Determine how many columns to go back
  566.                  * in order to erase the tab.
  567.                  * This depends on the number of columns
  568.                  * used by other characters within the tab
  569.                  * area.  If this (modulo 8) count is from
  570.                  * the start of input rather than from a
  571.                  * previous tab, we offset by canon column.
  572.                  * Otherwise, tab spacing is normal.
  573.                  */
  574.                 if (!(num_chars & 0x80))
  575.                     num_chars += tty->canon_column;
  576.                 num_bs = 8 - (num_chars & 7);
  577.  
  578.                 if (num_bs > space) {
  579.                     no_space_left = 1;
  580.                     break;
  581.                 }
  582.                 space -= num_bs;
  583.                 while (num_bs--) {
  584.                     tty_put_char(tty, '\b');
  585.                     if (tty->column > 0)
  586.                         tty->column--;
  587.                 }
  588.                 cp += 3;
  589.                 nr -= 3;
  590.                 break;
  591.  
  592.             case ECHO_OP_SET_CANON_COL:
  593.                 tty->canon_column = tty->column;
  594.                 cp += 2;
  595.                 nr -= 2;
  596.                 break;
  597.  
  598.             case ECHO_OP_MOVE_BACK_COL:
  599.                 if (tty->column > 0)
  600.                     tty->column--;
  601.                 cp += 2;
  602.                 nr -= 2;
  603.                 break;
  604.  
  605.             case ECHO_OP_START:
  606.                 /* This is an escaped echo op start code */
  607.                 if (!space) {
  608.                     no_space_left = 1;
  609.                     break;
  610.                 }
  611.                 tty_put_char(tty, ECHO_OP_START);
  612.                 tty->column++;
  613.                 space--;
  614.                 cp += 2;
  615.                 nr -= 2;
  616.                 break;
  617.  
  618.             default:
  619.                 /*
  620.                  * If the op is not a special byte code,
  621.                  * it is a ctrl char tagged to be echoed
  622.                  * as "^X" (where X is the letter
  623.                  * representing the control char).
  624.                  * Note that we must ensure there is
  625.                  * enough space for the whole ctrl pair.
  626.                  *
  627.                  */
  628.                 if (space < 2) {
  629.                     no_space_left = 1;
  630.                     break;
  631.                 }
  632.                 tty_put_char(tty, '^');
  633.                 tty_put_char(tty, op ^ 0100);
  634.                 tty->column += 2;
  635.                 space -= 2;
  636.                 cp += 2;
  637.                 nr -= 2;
  638.             }
  639.  
  640.             if (no_space_left)
  641.                 break;
  642.         } else {
  643.             if (O_OPOST(tty) &&
  644.                 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  645.                 int retval = do_output_char(c, tty, space);
  646.                 if (retval < 0)
  647.                     break;
  648.                 space -= retval;
  649.             } else {
  650.                 if (!space)
  651.                     break;
  652.                 tty_put_char(tty, c);
  653.                 space -= 1;
  654.             }
  655.             cp += 1;
  656.             nr -= 1;
  657.         }
  658.  
  659.         /* When end of circular buffer reached, wrap around */
  660.         if (cp >= buf_end)
  661.             cp -= N_TTY_BUF_SIZE;
  662.     }
  663.  
  664.     if (nr == 0) {
  665.         tty->echo_pos = 0;
  666.         tty->echo_cnt = 0;
  667.         tty->echo_overrun = 0;
  668.     } else {
  669.         int num_processed = tty->echo_cnt - nr;
  670.         tty->echo_pos += num_processed;
  671.         tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  672.         tty->echo_cnt = nr;
  673.         if (num_processed > 0)
  674.             tty->echo_overrun = 0;
  675.     }
  676.  
  677.     mutex_unlock(&tty->echo_lock);
  678.     mutex_unlock(&tty->output_lock);
  679.  
  680.     if (tty->ops->flush_chars)
  681.         tty->ops->flush_chars(tty);
  682. }
  683.  
  684. /**
  685.  *  add_echo_byte   -   add a byte to the echo buffer
  686.  *  @c: unicode byte to echo
  687.  *  @tty: terminal device
  688.  *
  689.  *  Add a character or operation byte to the echo buffer.
  690.  *
  691.  *  Should be called under the echo lock to protect the echo buffer.
  692.  */
  693.  
  694. static void add_echo_byte(unsigned char c, struct tty_struct *tty)
  695. {
  696.     int new_byte_pos;
  697.  
  698.     if (tty->echo_cnt == N_TTY_BUF_SIZE) {
  699.         /* Circular buffer is already at capacity */
  700.         new_byte_pos = tty->echo_pos;
  701.  
  702.         /*
  703.          * Since the buffer start position needs to be advanced,
  704.          * be sure to step by a whole operation byte group.
  705.          */
  706.         if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
  707.             if (tty->echo_buf[(tty->echo_pos + 1) &
  708.                       (N_TTY_BUF_SIZE - 1)] ==
  709.                         ECHO_OP_ERASE_TAB) {
  710.                 tty->echo_pos += 3;
  711.                 tty->echo_cnt -= 2;
  712.             } else {
  713.                 tty->echo_pos += 2;
  714.                 tty->echo_cnt -= 1;
  715.             }
  716.         } else {
  717.             tty->echo_pos++;
  718.         }
  719.         tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  720.  
  721.         tty->echo_overrun = 1;
  722.     } else {
  723.         new_byte_pos = tty->echo_pos + tty->echo_cnt;
  724.         new_byte_pos &= N_TTY_BUF_SIZE - 1;
  725.         tty->echo_cnt++;
  726.     }
  727.  
  728.     tty->echo_buf[new_byte_pos] = c;
  729. }
  730.  
  731. /**
  732.  *  echo_move_back_col  -   add operation to move back a column
  733.  *  @tty: terminal device
  734.  *
  735.  *  Add an operation to the echo buffer to move back one column.
  736.  *
  737.  *  Locking: echo_lock to protect the echo buffer
  738.  */
  739.  
  740. static void echo_move_back_col(struct tty_struct *tty)
  741. {
  742.     mutex_lock(&tty->echo_lock);
  743.  
  744.     add_echo_byte(ECHO_OP_START, tty);
  745.     add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
  746.  
  747.     mutex_unlock(&tty->echo_lock);
  748. }
  749.  
  750. /**
  751.  *  echo_set_canon_col  -   add operation to set the canon column
  752.  *  @tty: terminal device
  753.  *
  754.  *  Add an operation to the echo buffer to set the canon column
  755.  *  to the current column.
  756.  *
  757.  *  Locking: echo_lock to protect the echo buffer
  758.  */
  759.  
  760. static void echo_set_canon_col(struct tty_struct *tty)
  761. {
  762.     mutex_lock(&tty->echo_lock);
  763.  
  764.     add_echo_byte(ECHO_OP_START, tty);
  765.     add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
  766.  
  767.     mutex_unlock(&tty->echo_lock);
  768. }
  769.  
  770. /**
  771.  *  echo_erase_tab  -   add operation to erase a tab
  772.  *  @num_chars: number of character columns already used
  773.  *  @after_tab: true if num_chars starts after a previous tab
  774.  *  @tty: terminal device
  775.  *
  776.  *  Add an operation to the echo buffer to erase a tab.
  777.  *
  778.  *  Called by the eraser function, which knows how many character
  779.  *  columns have been used since either a previous tab or the start
  780.  *  of input.  This information will be used later, along with
  781.  *  canon column (if applicable), to go back the correct number
  782.  *  of columns.
  783.  *
  784.  *  Locking: echo_lock to protect the echo buffer
  785.  */
  786.  
  787. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  788.                struct tty_struct *tty)
  789. {
  790.     mutex_lock(&tty->echo_lock);
  791.  
  792.     add_echo_byte(ECHO_OP_START, tty);
  793.     add_echo_byte(ECHO_OP_ERASE_TAB, tty);
  794.  
  795.     /* We only need to know this modulo 8 (tab spacing) */
  796.     num_chars &= 7;
  797.  
  798.     /* Set the high bit as a flag if num_chars is after a previous tab */
  799.     if (after_tab)
  800.         num_chars |= 0x80;
  801.  
  802.     add_echo_byte(num_chars, tty);
  803.  
  804.     mutex_unlock(&tty->echo_lock);
  805. }
  806.  
  807. /**
  808.  *  echo_char_raw   -   echo a character raw
  809.  *  @c: unicode byte to echo
  810.  *  @tty: terminal device
  811.  *
  812.  *  Echo user input back onto the screen. This must be called only when
  813.  *  L_ECHO(tty) is true. Called from the driver receive_buf path.
  814.  *
  815.  *  This variant does not treat control characters specially.
  816.  *
  817.  *  Locking: echo_lock to protect the echo buffer
  818.  */
  819.  
  820. static void echo_char_raw(unsigned char c, struct tty_struct *tty)
  821. {
  822.     mutex_lock(&tty->echo_lock);
  823.  
  824.     if (c == ECHO_OP_START) {
  825.         add_echo_byte(ECHO_OP_START, tty);
  826.         add_echo_byte(ECHO_OP_START, tty);
  827.     } else {
  828.         add_echo_byte(c, tty);
  829.     }
  830.  
  831.     mutex_unlock(&tty->echo_lock);
  832. }
  833.  
  834. /**
  835.  *  echo_char   -   echo a character
  836.  *  @c: unicode byte to echo
  837.  *  @tty: terminal device
  838.  *
  839.  *  Echo user input back onto the screen. This must be called only when
  840.  *  L_ECHO(tty) is true. Called from the driver receive_buf path.
  841.  *
  842.  *  This variant tags control characters to be echoed as "^X"
  843.  *  (where X is the letter representing the control char).
  844.  *
  845.  *  Locking: echo_lock to protect the echo buffer
  846.  */
  847.  
  848. static void echo_char(unsigned char c, struct tty_struct *tty)
  849. {
  850.     mutex_lock(&tty->echo_lock);
  851.  
  852.     if (c == ECHO_OP_START) {
  853.         add_echo_byte(ECHO_OP_START, tty);
  854.         add_echo_byte(ECHO_OP_START, tty);
  855.     } else {
  856.         if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  857.             add_echo_byte(ECHO_OP_START, tty);
  858.         add_echo_byte(c, tty);
  859.     }
  860.  
  861.     mutex_unlock(&tty->echo_lock);
  862. }
  863.  
  864. /**
  865.  *  finish_erasing      -   complete erase
  866.  *  @tty: tty doing the erase
  867.  */
  868.  
  869. static inline void finish_erasing(struct tty_struct *tty)
  870. {
  871.     if (tty->erasing) {
  872.         echo_char_raw('/', tty);
  873.         tty->erasing = 0;
  874.     }
  875. }
  876.  
  877. /**
  878.  *  eraser      -   handle erase function
  879.  *  @c: character input
  880.  *  @tty: terminal device
  881.  *
  882.  *  Perform erase and necessary output when an erase character is
  883.  *  present in the stream from the driver layer. Handles the complexities
  884.  *  of UTF-8 multibyte symbols.
  885.  *
  886.  *  Locking: read_lock for tty buffers
  887.  */
  888.  
  889. static void eraser(unsigned char c, struct tty_struct *tty)
  890. {
  891.     enum { ERASE, WERASE, KILL } kill_type;
  892.     int head, seen_alnums, cnt;
  893.     unsigned long flags;
  894.  
  895.     /* FIXME: locking needed ? */
  896.     if (tty->read_head == tty->canon_head) {
  897.         /* process_output('\a', tty); */ /* what do you think? */
  898.         return;
  899.     }
  900.     if (c == ERASE_CHAR(tty))
  901.         kill_type = ERASE;
  902.     else if (c == WERASE_CHAR(tty))
  903.         kill_type = WERASE;
  904.     else {
  905.         if (!L_ECHO(tty)) {
  906.             spin_lock_irqsave(&tty->read_lock, flags);
  907.             tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  908.                       (N_TTY_BUF_SIZE - 1));
  909.             tty->read_head = tty->canon_head;
  910.             spin_unlock_irqrestore(&tty->read_lock, flags);
  911.             return;
  912.         }
  913.         if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  914.             spin_lock_irqsave(&tty->read_lock, flags);
  915.             tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  916.                       (N_TTY_BUF_SIZE - 1));
  917.             tty->read_head = tty->canon_head;
  918.             spin_unlock_irqrestore(&tty->read_lock, flags);
  919.             finish_erasing(tty);
  920.             echo_char(KILL_CHAR(tty), tty);
  921.             /* Add a newline if ECHOK is on and ECHOKE is off. */
  922.             if (L_ECHOK(tty))
  923.                 echo_char_raw('\n', tty);
  924.             return;
  925.         }
  926.         kill_type = KILL;
  927.     }
  928.  
  929.     seen_alnums = 0;
  930.     /* FIXME: Locking ?? */
  931.     while (tty->read_head != tty->canon_head) {
  932.         head = tty->read_head;
  933.  
  934.         /* erase a single possibly multibyte character */
  935.         do {
  936.             head = (head - 1) & (N_TTY_BUF_SIZE-1);
  937.             c = tty->read_buf[head];
  938.         } while (is_continuation(c, tty) && head != tty->canon_head);
  939.  
  940.         /* do not partially erase */
  941.         if (is_continuation(c, tty))
  942.             break;
  943.  
  944.         if (kill_type == WERASE) {
  945.             /* Equivalent to BSD's ALTWERASE. */
  946.             if (isalnum(c) || c == '_')
  947.                 seen_alnums++;
  948.             else if (seen_alnums)
  949.                 break;
  950.         }
  951.         cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
  952.         spin_lock_irqsave(&tty->read_lock, flags);
  953.         tty->read_head = head;
  954.         tty->read_cnt -= cnt;
  955.         spin_unlock_irqrestore(&tty->read_lock, flags);
  956.         if (L_ECHO(tty)) {
  957.             if (L_ECHOPRT(tty)) {
  958.                 if (!tty->erasing) {
  959.                     echo_char_raw('\\', tty);
  960.                     tty->erasing = 1;
  961.                 }
  962.                 /* if cnt > 1, output a multi-byte character */
  963.                 echo_char(c, tty);
  964.                 while (--cnt > 0) {
  965.                     head = (head+1) & (N_TTY_BUF_SIZE-1);
  966.                     echo_char_raw(tty->read_buf[head], tty);
  967.                     echo_move_back_col(tty);
  968.                 }
  969.             } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  970.                 echo_char(ERASE_CHAR(tty), tty);
  971.             } else if (c == '\t') {
  972.                 unsigned int num_chars = 0;
  973.                 int after_tab = 0;
  974.                 unsigned long tail = tty->read_head;
  975.  
  976.                 /*
  977.                  * Count the columns used for characters
  978.                  * since the start of input or after a
  979.                  * previous tab.
  980.                  * This info is used to go back the correct
  981.                  * number of columns.
  982.                  */
  983.                 while (tail != tty->canon_head) {
  984.                     tail = (tail-1) & (N_TTY_BUF_SIZE-1);
  985.                     c = tty->read_buf[tail];
  986.                     if (c == '\t') {
  987.                         after_tab = 1;
  988.                         break;
  989.                     } else if (iscntrl(c)) {
  990.                         if (L_ECHOCTL(tty))
  991.                             num_chars += 2;
  992.                     } else if (!is_continuation(c, tty)) {
  993.                         num_chars++;
  994.                     }
  995.                 }
  996.                 echo_erase_tab(num_chars, after_tab, tty);
  997.             } else {
  998.                 if (iscntrl(c) && L_ECHOCTL(tty)) {
  999.                     echo_char_raw('\b', tty);
  1000.                     echo_char_raw(' ', tty);
  1001.                     echo_char_raw('\b', tty);
  1002.                 }
  1003.                 if (!iscntrl(c) || L_ECHOCTL(tty)) {
  1004.                     echo_char_raw('\b', tty);
  1005.                     echo_char_raw(' ', tty);
  1006.                     echo_char_raw('\b', tty);
  1007.                 }
  1008.             }
  1009.         }
  1010.         if (kill_type == ERASE)
  1011.             break;
  1012.     }
  1013.     if (tty->read_head == tty->canon_head && L_ECHO(tty))
  1014.         finish_erasing(tty);
  1015. }
  1016.  
  1017. /**
  1018.  *  isig        -   handle the ISIG optio
  1019.  *  @sig: signal
  1020.  *  @tty: terminal
  1021.  *  @flush: force flush
  1022.  *
  1023.  *  Called when a signal is being sent due to terminal input. This
  1024.  *  may caus terminal flushing to take place according to the termios
  1025.  *  settings and character used. Called from the driver receive_buf
  1026.  *  path so serialized.
  1027.  *
  1028.  *  Locking: ctrl_lock, read_lock (both via flush buffer)
  1029.  */
  1030.  
  1031. static inline void isig(int sig, struct tty_struct *tty, int flush)
  1032. {
  1033.     if (tty->pgrp)
  1034.         kill_pgrp(tty->pgrp, sig, 1);
  1035.     if (flush || !L_NOFLSH(tty)) {
  1036.         n_tty_flush_buffer(tty);
  1037.         tty_driver_flush_buffer(tty);
  1038.     }
  1039. }
  1040.  
  1041. /**
  1042.  *  n_tty_receive_break -   handle break
  1043.  *  @tty: terminal
  1044.  *
  1045.  *  An RS232 break event has been hit in the incoming bitstream. This
  1046.  *  can cause a variety of events depending upon the termios settings.
  1047.  *
  1048.  *  Called from the receive_buf path so single threaded.
  1049.  */
  1050.  
  1051. static inline void n_tty_receive_break(struct tty_struct *tty)
  1052. {
  1053.     if (I_IGNBRK(tty))
  1054.         return;
  1055.     if (I_BRKINT(tty)) {
  1056.         isig(SIGINT, tty, 1);
  1057.         return;
  1058.     }
  1059.     if (I_PARMRK(tty)) {
  1060.         put_tty_queue('\377', tty);
  1061.         put_tty_queue('\0', tty);
  1062.     }
  1063.     put_tty_queue('\0', tty);
  1064.     wake_up_interruptible(&tty->read_wait);
  1065. }
  1066.  
  1067. /**
  1068.  *  n_tty_receive_overrun   -   handle overrun reporting
  1069.  *  @tty: terminal
  1070.  *
  1071.  *  Data arrived faster than we could process it. While the tty
  1072.  *  driver has flagged this the bits that were missed are gone
  1073.  *  forever.
  1074.  *
  1075.  *  Called from the receive_buf path so single threaded. Does not
  1076.  *  need locking as num_overrun and overrun_time are function
  1077.  *  private.
  1078.  */
  1079.  
  1080. static inline void n_tty_receive_overrun(struct tty_struct *tty)
  1081. {
  1082.     char buf[64];
  1083.  
  1084.     tty->num_overrun++;
  1085.     if (time_before(tty->overrun_time, jiffies - HZ) ||
  1086.             time_after(tty->overrun_time, jiffies)) {
  1087.         printk(KERN_WARNING "%s: %d input overrun(s)\n",
  1088.             tty_name(tty, buf),
  1089.             tty->num_overrun);
  1090.         tty->overrun_time = jiffies;
  1091.         tty->num_overrun = 0;
  1092.     }
  1093. }
  1094.  
  1095. /**
  1096.  *  n_tty_receive_parity_error  -   error notifier
  1097.  *  @tty: terminal device
  1098.  *  @c: character
  1099.  *
  1100.  *  Process a parity error and queue the right data to indicate
  1101.  *  the error case if necessary. Locking as per n_tty_receive_buf.
  1102.  */
  1103. static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  1104.                           unsigned char c)
  1105. {
  1106.     if (I_IGNPAR(tty))
  1107.         return;
  1108.     if (I_PARMRK(tty)) {
  1109.         put_tty_queue('\377', tty);
  1110.         put_tty_queue('\0', tty);
  1111.         put_tty_queue(c, tty);
  1112.     } else  if (I_INPCK(tty))
  1113.         put_tty_queue('\0', tty);
  1114.     else
  1115.         put_tty_queue(c, tty);
  1116.     wake_up_interruptible(&tty->read_wait);
  1117. }
  1118.  
  1119. #ifdef CONFIG_KDEBUGD
  1120. /* kdebugd handling code */
  1121.  
  1122. char kdbg_buf[DEBUG_MAX_RAW_CHARS]="";
  1123. char input_buf[2]={'\0',};
  1124.  
  1125. char sched_serial[10] = "";
  1126. int  sched_serial_idx = 0;
  1127.  
  1128. static inline int check_debug_mode(struct tty_struct *tty, unsigned char c)
  1129. {
  1130.     debugd_event_t event;
  1131.     int ret = 0;
  1132.     static unsigned char prev_ch = 0;
  1133.     static unsigned int history_idx = 0;
  1134.     char * ptr = NULL;
  1135.  
  1136.  
  1137.     if( tty != INPUT_tty)
  1138.         return ret;
  1139.  
  1140.     if(!kdebugd_running)
  1141.     {
  1142.         if( c == sched_serial[sched_serial_idx]){
  1143.  
  1144.             sched_serial_idx ++;
  1145.             //printk(KERN_EMERG "[SP Kernel Debugger] ");
  1146.             //printk("%d-th ENABLE Magic serial input match!\n"
  1147.             //         ,sched_serial_idx);
  1148.             if(sched_serial_idx == strlen(sched_serial))
  1149.             {
  1150.                 printk(KERN_EMERG "[SP Kernel Debugger] ");
  1151.                 printk("Enable Debug Mode!\n");
  1152.                 kdebugd_start();
  1153.                 kdebugd_running = 1;
  1154.             }
  1155.  
  1156.         }else
  1157.             sched_serial_idx = 0;
  1158.  
  1159.         return ret;
  1160.     }
  1161.  
  1162.     /* Now, kdebugd is running */
  1163.  
  1164.     ret = 1;
  1165.  
  1166.     switch (c)
  1167.     {
  1168.         case 0xd : /* \n */
  1169.  
  1170.             /* remove leading and trailing whitespaces */
  1171.             ptr = strstrip(kdbg_buf);  
  1172.            
  1173.             /* copy kdbg_buf to event */
  1174.             strncpy(event.input_string, ptr, sizeof(event.input_string) - 1);
  1175.             event.input_string[sizeof(event.input_string) - 1] = '\0';
  1176.  
  1177.             queue_add_event(&kdebugd_queue, &event);
  1178.  
  1179.             /* Fix Me:
  1180.                Actually, history_idx need to be set at everyevent_tail modifying code.
  1181.                Now, We just set history_idx as event_tail plus 2..
  1182.              */
  1183.             history_idx = kdebugd_queue.event_tail + 2;
  1184.  
  1185.             kdbg_buf[0]='\0';
  1186.  
  1187.             break;
  1188.  
  1189.             /* When arrow key is pressed, the serial driver sends below character sequently. */
  1190.             /* e.g.
  1191.                0x1b  => 0x5b => 0x41(up arrow)
  1192.                0x1b  => 0x5b => 0x42(down arrow)
  1193.              */
  1194.         case 0x1b: /* Special Key */
  1195.         case 0x5b: /* Special Key */
  1196.             prev_ch = c;
  1197.             break;
  1198.  
  1199.         case 0x41: /* Up arrow */
  1200.             if( prev_ch == 0x5b && history_idx > 0) { /* Special Key */
  1201.                 history_idx--;
  1202.                 printk("\n%s #%d> %s",sched_serial,history_idx,kdebugd_queue.events[history_idx].input_string);
  1203.                 snprintf(kdbg_buf, DEBUG_MAX_RAW_CHARS, "%s",kdebugd_queue.events[history_idx].input_string);
  1204.                 prev_ch =c;
  1205.             }
  1206.             break;
  1207.  
  1208.         case 0x42: /* Down arrow */
  1209.             if( prev_ch == 0x5b && history_idx <= kdebugd_queue.event_tail) { /* Special Key */
  1210.                 printk("\n%s #%d> %s",sched_serial,history_idx,kdebugd_queue.events[history_idx].input_string);
  1211.                 snprintf(kdbg_buf, DEBUG_MAX_RAW_CHARS, "%s",kdebugd_queue.events[history_idx].input_string);
  1212.                 history_idx++;
  1213.                 prev_ch =c;
  1214.             }
  1215.             break;
  1216.  
  1217.         case 0x8: /* Backspace */
  1218.         case 0x7F: /* Backspace */
  1219.             if( strlen(kdbg_buf) > 0) {
  1220.                 kdbg_buf[strlen(kdbg_buf) - 1]='\0';
  1221.                 /* print backspace, space, backspace to remove current character */
  1222.                 printk("%c %c",c,c);
  1223.             }
  1224.             break;
  1225.  
  1226.         default:
  1227.             input_buf[0] = c;
  1228.             if(strlen(kdbg_buf) + 1 < DEBUG_MAX_RAW_CHARS ){
  1229.                 strncat(kdbg_buf,input_buf,1);
  1230.             }
  1231.             else
  1232.             {
  1233.                 printk("\n[ALERT]Can't enter more input than %d size..", DEBUG_MAX_RAW_CHARS - 1);
  1234.             }
  1235.             printk("%c",c);
  1236.             break;
  1237.  
  1238.     }
  1239.     return ret;
  1240. }
  1241. #endif
  1242.  
  1243. /**
  1244.  *  n_tty_receive_char  -   perform processing
  1245.  *  @tty: terminal device
  1246.  *  @c: character
  1247.  *
  1248.  *  Process an individual character of input received from the driver.
  1249.  *  This is serialized with respect to itself by the rules for the
  1250.  *  driver above.
  1251.  */
  1252.  
  1253. static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  1254. {
  1255.     unsigned long flags;
  1256.     int parmrk;
  1257.  
  1258. #ifdef CONFIG_SERIAL_INPUT_ENABLE_ONLY_NUMBER
  1259.         int check, i;
  1260. #endif
  1261.  
  1262. #ifdef CONFIG_KDEBUGD
  1263.     if(check_debug_mode(tty,c))
  1264.         return;
  1265. #endif
  1266.  
  1267. #ifdef CONFIG_SERIAL_INPUT_MANIPULATION
  1268.         if( tty == INPUT_tty )
  1269.         {
  1270.                 /* skip enable check if serial_enable == 1 */
  1271.                 if( serial_enable == SERIAL_INPUT_ENABLE )
  1272.                         goto enable_check_skip;
  1273.  
  1274.                 /* for debug enable */
  1275.                 if( c == enable_serial[enable_index] )
  1276.                 {
  1277.                         enable_index++;
  1278.  
  1279. #ifdef CONFIG_SERIAL_INPUT_ENABLE_HELP_MSG
  1280.                         printk(KERN_ALERT "\n" );
  1281.                         printk(KERN_ALERT "[SERIAL INPUT MANAGE] %d-th ENABLE Magic serial input match!\n",
  1282.                                                                 enable_index );
  1283. #endif
  1284.                         if( enable_index == enable_string_size )
  1285.                         {
  1286.                                 serial_enable = SERIAL_INPUT_ENABLE;
  1287.                                 disable_index = 0;
  1288.  
  1289. #ifdef CONFIG_SERIAL_INPUT_ENABLE_HELP_MSG
  1290.                                 printk(KERN_ALERT "\n" );
  1291.                                 printk(KERN_ALERT "[SERIAL INPUT MANAGE] serial input ENABLE!!!!!\n");
  1292. #endif
  1293.                         }
  1294.                         return;
  1295.                 }
  1296.                 else
  1297.                         enable_index = 0;
  1298.  
  1299. enable_check_skip:
  1300.                 /* skip disable check if serial_enable == 0 */
  1301.                 if( serial_enable == SERIAL_INPUT_DISABLE )
  1302.                         return;
  1303.  
  1304.                 /* for debug disable */
  1305.                 if( c == disable_serial[disable_index] )
  1306.                 {
  1307.                         disable_index++;
  1308.  
  1309. #ifdef CONFIG_SERIAL_INPUT_ENABLE_HELP_MSG
  1310.                         printk(KERN_ALERT "\n" );
  1311.                         printk(KERN_ALERT "[SERIAL INPUT MANAGE] %d-th DISABLE Magic serial input match!\n",
  1312.                                                                 disable_index );
  1313. #endif
  1314.                         if( disable_index == disable_string_size )
  1315.                         {
  1316.                                 serial_enable = SERIAL_INPUT_DISABLE;
  1317.                                 enable_index = 0;
  1318.  
  1319. #ifdef CONFIG_SERIAL_INPUT_ENABLE_HELP_MSG
  1320.                                 printk(KERN_ALERT "\n" );
  1321.                                 printk(KERN_ALERT "[SERIAL INPUT MANAGE] serial input DISABLE!!!!!\n");
  1322. #endif
  1323.                                 return;
  1324.                         }
  1325.                 }
  1326.                 else
  1327.                         disable_index = 0;
  1328. #ifdef CONFIG_SERIAL_INPUT_ENABLE_ONLY_NUMBER
  1329.                 // additional checking 0~9, space, enter, backspace, SPTEAM 2010-02-07
  1330.                 check = i = 0;
  1331.                 while(i < MAX_ARRAY )
  1332.                 {
  1333.                         if( allow_char[i] == c )
  1334.                         {
  1335.                                 check = 1;
  1336.                                 break;
  1337.                         }
  1338.                         i++;
  1339.                 }
  1340.  
  1341.                 if( !check )
  1342.                         return;
  1343. #endif
  1344.         }
  1345. #endif
  1346.  
  1347.     if (tty->raw) {
  1348.         put_tty_queue(c, tty);
  1349.         return;
  1350.     }
  1351.  
  1352.     if (I_ISTRIP(tty))
  1353.         c &= 0x7f;
  1354.     if (I_IUCLC(tty) && L_IEXTEN(tty))
  1355.         c = tolower(c);
  1356.  
  1357.     if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  1358.         I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
  1359.         c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
  1360.         start_tty(tty);
  1361.         process_echoes(tty);
  1362.     }
  1363.  
  1364.     if (tty->closing) {
  1365.         if (I_IXON(tty)) {
  1366.             if (c == START_CHAR(tty)) {
  1367.                 start_tty(tty);
  1368.                 process_echoes(tty);
  1369.             } else if (c == STOP_CHAR(tty))
  1370.                 stop_tty(tty);
  1371.         }
  1372.         return;
  1373.     }
  1374.  
  1375.     /*
  1376.      * If the previous character was LNEXT, or we know that this
  1377.      * character is not one of the characters that we'll have to
  1378.      * handle specially, do shortcut processing to speed things
  1379.      * up.
  1380.      */
  1381.     if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  1382.         tty->lnext = 0;
  1383.         parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1384.         if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1385.             /* beep if no space */
  1386.             if (L_ECHO(tty))
  1387.                 process_output('\a', tty);
  1388.             return;
  1389.         }
  1390.         if (L_ECHO(tty)) {
  1391.             finish_erasing(tty);
  1392.             /* Record the column of first canon char. */
  1393.             if (tty->canon_head == tty->read_head)
  1394.                 echo_set_canon_col(tty);
  1395.             echo_char(c, tty);
  1396.             process_echoes(tty);
  1397.         }
  1398.         if (parmrk)
  1399.             put_tty_queue(c, tty);
  1400.         put_tty_queue(c, tty);
  1401.         return;
  1402.     }
  1403.  
  1404.     if (I_IXON(tty)) {
  1405.         if (c == START_CHAR(tty)) {
  1406.             start_tty(tty);
  1407.             process_echoes(tty);
  1408.             return;
  1409.         }
  1410.         if (c == STOP_CHAR(tty)) {
  1411.             stop_tty(tty);
  1412.             return;
  1413.         }
  1414.     }
  1415.  
  1416.     if (L_ISIG(tty)) {
  1417.         int signal;
  1418.         signal = SIGINT;
  1419.         if (c == INTR_CHAR(tty))
  1420.             goto send_signal;
  1421.         signal = SIGQUIT;
  1422.         if (c == QUIT_CHAR(tty))
  1423.             goto send_signal;
  1424.         signal = SIGTSTP;
  1425.         if (c == SUSP_CHAR(tty)) {
  1426. send_signal:
  1427.             /*
  1428.              * Note that we do not use isig() here because we want
  1429.              * the order to be:
  1430.              * 1) flush, 2) echo, 3) signal
  1431.              */
  1432.             if (!L_NOFLSH(tty)) {
  1433.                 n_tty_flush_buffer(tty);
  1434.                 tty_driver_flush_buffer(tty);
  1435.             }
  1436.             if (I_IXON(tty))
  1437.                 start_tty(tty);
  1438.             if (L_ECHO(tty)) {
  1439.                 echo_char(c, tty);
  1440.                 process_echoes(tty);
  1441.             }
  1442.             if (tty->pgrp)
  1443.                 kill_pgrp(tty->pgrp, signal, 1);
  1444.             return;
  1445.         }
  1446.     }
  1447.  
  1448.     if (c == '\r') {
  1449.         if (I_IGNCR(tty))
  1450.             return;
  1451.         if (I_ICRNL(tty))
  1452.             c = '\n';
  1453.     } else if (c == '\n' && I_INLCR(tty))
  1454.         c = '\r';
  1455.  
  1456.     if (tty->icanon) {
  1457.         if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1458.             (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1459.             eraser(c, tty);
  1460.             process_echoes(tty);
  1461.             return;
  1462.         }
  1463.         if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1464.             tty->lnext = 1;
  1465.             if (L_ECHO(tty)) {
  1466.                 finish_erasing(tty);
  1467.                 if (L_ECHOCTL(tty)) {
  1468.                     echo_char_raw('^', tty);
  1469.                     echo_char_raw('\b', tty);
  1470.                     process_echoes(tty);
  1471.                 }
  1472.             }
  1473.             return;
  1474.         }
  1475.         if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  1476.             L_IEXTEN(tty)) {
  1477.             unsigned long tail = tty->canon_head;
  1478.  
  1479.             finish_erasing(tty);
  1480.             echo_char(c, tty);
  1481.             echo_char_raw('\n', tty);
  1482.             while (tail != tty->read_head) {
  1483.                 echo_char(tty->read_buf[tail], tty);
  1484.                 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1485.             }
  1486.             process_echoes(tty);
  1487.             return;
  1488.         }
  1489.         if (c == '\n') {
  1490.             if (tty->read_cnt >= N_TTY_BUF_SIZE) {
  1491.                 if (L_ECHO(tty))
  1492.                     process_output('\a', tty);
  1493.                 return;
  1494.             }
  1495.             if (L_ECHO(tty) || L_ECHONL(tty)) {
  1496.                 echo_char_raw('\n', tty);
  1497.                 process_echoes(tty);
  1498.             }
  1499.             goto handle_newline;
  1500.         }
  1501.         if (c == EOF_CHAR(tty)) {
  1502.             if (tty->read_cnt >= N_TTY_BUF_SIZE)
  1503.                 return;
  1504.             if (tty->canon_head != tty->read_head)
  1505.                 set_bit(TTY_PUSH, &tty->flags);
  1506.             c = __DISABLED_CHAR;
  1507.             goto handle_newline;
  1508.         }
  1509.         if ((c == EOL_CHAR(tty)) ||
  1510.             (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1511.             parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
  1512.                  ? 1 : 0;
  1513.             if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
  1514.                 if (L_ECHO(tty))
  1515.                     process_output('\a', tty);
  1516.                 return;
  1517.             }
  1518.             /*
  1519.              * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1520.              */
  1521.             if (L_ECHO(tty)) {
  1522.                 /* Record the column of first canon char. */
  1523.                 if (tty->canon_head == tty->read_head)
  1524.                     echo_set_canon_col(tty);
  1525.                 echo_char(c, tty);
  1526.                 process_echoes(tty);
  1527.             }
  1528.             /*
  1529.              * XXX does PARMRK doubling happen for
  1530.              * EOL_CHAR and EOL2_CHAR?
  1531.              */
  1532.             if (parmrk)
  1533.                 put_tty_queue(c, tty);
  1534.  
  1535. handle_newline:
  1536.             spin_lock_irqsave(&tty->read_lock, flags);
  1537.             set_bit(tty->read_head, tty->read_flags);
  1538.             put_tty_queue_nolock(c, tty);
  1539.             tty->canon_head = tty->read_head;
  1540.             tty->canon_data++;
  1541.             spin_unlock_irqrestore(&tty->read_lock, flags);
  1542.             kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1543.             if (waitqueue_active(&tty->read_wait))
  1544.                 wake_up_interruptible(&tty->read_wait);
  1545.             return;
  1546.         }
  1547.     }
  1548.  
  1549.     parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1550.     if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1551.         /* beep if no space */
  1552.         if (L_ECHO(tty))
  1553.             process_output('\a', tty);
  1554.         return;
  1555.     }
  1556.     if (L_ECHO(tty)) {
  1557.         finish_erasing(tty);
  1558.         if (c == '\n')
  1559.             echo_char_raw('\n', tty);
  1560.         else {
  1561.             /* Record the column of first canon char. */
  1562.             if (tty->canon_head == tty->read_head)
  1563.                 echo_set_canon_col(tty);
  1564.             echo_char(c, tty);
  1565.         }
  1566.         process_echoes(tty);
  1567.     }
  1568.  
  1569.     if (parmrk)
  1570.         put_tty_queue(c, tty);
  1571.  
  1572.     put_tty_queue(c, tty);
  1573. }
  1574.  
  1575.  
  1576. /**
  1577.  *  n_tty_write_wakeup  -   asynchronous I/O notifier
  1578.  *  @tty: tty device
  1579.  *
  1580.  *  Required for the ptys, serial driver etc. since processes
  1581.  *  that attach themselves to the master and rely on ASYNC
  1582.  *  IO must be woken up
  1583.  */
  1584.  
  1585. static void n_tty_write_wakeup(struct tty_struct *tty)
  1586. {
  1587.     if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
  1588.         kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  1589. }
  1590.  
  1591. /**
  1592.  *  n_tty_receive_buf   -   data receive
  1593.  *  @tty: terminal device
  1594.  *  @cp: buffer
  1595.  *  @fp: flag buffer
  1596.  *  @count: characters
  1597.  *
  1598.  *  Called by the terminal driver when a block of characters has
  1599.  *  been received. This function must be called from soft contexts
  1600.  *  not from interrupt context. The driver is responsible for making
  1601.  *  calls one at a time and in order (or using flush_to_ldisc)
  1602.  */
  1603.  
  1604. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1605.                   char *fp, int count)
  1606. {
  1607.     const unsigned char *p;
  1608.     char *f, flags = TTY_NORMAL;
  1609.     int i;
  1610.     char    buf[64];
  1611.     unsigned long cpuflags;
  1612.  
  1613.     if (!tty->read_buf)
  1614.         return;
  1615.  
  1616.     if (tty->real_raw) {
  1617.         spin_lock_irqsave(&tty->read_lock, cpuflags);
  1618.         i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1619.             N_TTY_BUF_SIZE - tty->read_head);
  1620.         i = min(count, i);
  1621.         memcpy(tty->read_buf + tty->read_head, cp, i);
  1622.         tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1623.         tty->read_cnt += i;
  1624.         cp += i;
  1625.         count -= i;
  1626.  
  1627.         i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1628.             N_TTY_BUF_SIZE - tty->read_head);
  1629.         i = min(count, i);
  1630.         memcpy(tty->read_buf + tty->read_head, cp, i);
  1631.         tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1632.         tty->read_cnt += i;
  1633.         spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  1634.     } else {
  1635.         for (i = count, p = cp, f = fp; i; i--, p++) {
  1636.             if (f)
  1637.                 flags = *f++;
  1638.             switch (flags) {
  1639.             case TTY_NORMAL:
  1640.                 n_tty_receive_char(tty, *p);
  1641.                 break;
  1642.             case TTY_BREAK:
  1643.                 n_tty_receive_break(tty);
  1644.                 break;
  1645.             case TTY_PARITY:
  1646.             case TTY_FRAME:
  1647.                 n_tty_receive_parity_error(tty, *p);
  1648.                 break;
  1649.             case TTY_OVERRUN:
  1650.                 n_tty_receive_overrun(tty);
  1651.                 break;
  1652.             default:
  1653.                 printk(KERN_ERR "%s: unknown flag %d\n",
  1654.                        tty_name(tty, buf), flags);
  1655.                 break;
  1656.             }
  1657.         }
  1658.         if (tty->ops->flush_chars)
  1659.             tty->ops->flush_chars(tty);
  1660.     }
  1661.  
  1662.     n_tty_set_room(tty);
  1663.  
  1664.     if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
  1665.         kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1666.         if (waitqueue_active(&tty->read_wait))
  1667.             wake_up_interruptible(&tty->read_wait);
  1668.     }
  1669.  
  1670.     /*
  1671.      * Check the remaining room for the input canonicalization
  1672.      * mode.  We don't want to throttle the driver if we're in
  1673.      * canonical mode and don't have a newline yet!
  1674.      */
  1675.     if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
  1676.         tty_throttle(tty);
  1677. }
  1678.  
  1679. int is_ignored(int sig)
  1680. {
  1681.     return (sigismember(&current->blocked, sig) ||
  1682.         current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1683. }
  1684.  
  1685. /**
  1686.  *  n_tty_set_termios   -   termios data changed
  1687.  *  @tty: terminal
  1688.  *  @old: previous data
  1689.  *
  1690.  *  Called by the tty layer when the user changes termios flags so
  1691.  *  that the line discipline can plan ahead. This function cannot sleep
  1692.  *  and is protected from re-entry by the tty layer. The user is
  1693.  *  guaranteed that this function will not be re-entered or in progress
  1694.  *  when the ldisc is closed.
  1695.  *
  1696.  *  Locking: Caller holds tty->termios_mutex
  1697.  */
  1698.  
  1699. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1700. {
  1701.     int canon_change = 1;
  1702.     BUG_ON(!tty);
  1703.  
  1704.     if (old)
  1705.         canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  1706.     if (canon_change) {
  1707.         memset(&tty->read_flags, 0, sizeof tty->read_flags);
  1708.         tty->canon_head = tty->read_tail;
  1709.         tty->canon_data = 0;
  1710.         tty->erasing = 0;
  1711.     }
  1712.  
  1713.     if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  1714.         wake_up_interruptible(&tty->read_wait);
  1715.  
  1716.     tty->icanon = (L_ICANON(tty) != 0);
  1717.     if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  1718.         tty->raw = 1;
  1719.         tty->real_raw = 1;
  1720.         n_tty_set_room(tty);
  1721.         return;
  1722.     }
  1723.     if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1724.         I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1725.         I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1726.         I_PARMRK(tty)) {
  1727.         memset(tty->process_char_map, 0, 256/8);
  1728.  
  1729.         if (I_IGNCR(tty) || I_ICRNL(tty))
  1730.             set_bit('\r', tty->process_char_map);
  1731.         if (I_INLCR(tty))
  1732.             set_bit('\n', tty->process_char_map);
  1733.  
  1734.         if (L_ICANON(tty)) {
  1735.             set_bit(ERASE_CHAR(tty), tty->process_char_map);
  1736.             set_bit(KILL_CHAR(tty), tty->process_char_map);
  1737.             set_bit(EOF_CHAR(tty), tty->process_char_map);
  1738.             set_bit('\n', tty->process_char_map);
  1739.             set_bit(EOL_CHAR(tty), tty->process_char_map);
  1740.             if (L_IEXTEN(tty)) {
  1741.                 set_bit(WERASE_CHAR(tty),
  1742.                     tty->process_char_map);
  1743.                 set_bit(LNEXT_CHAR(tty),
  1744.                     tty->process_char_map);
  1745.                 set_bit(EOL2_CHAR(tty),
  1746.                     tty->process_char_map);
  1747.                 if (L_ECHO(tty))
  1748.                     set_bit(REPRINT_CHAR(tty),
  1749.                         tty->process_char_map);
  1750.             }
  1751.         }
  1752.         if (I_IXON(tty)) {
  1753.             set_bit(START_CHAR(tty), tty->process_char_map);
  1754.             set_bit(STOP_CHAR(tty), tty->process_char_map);
  1755.         }
  1756.         if (L_ISIG(tty)) {
  1757.             set_bit(INTR_CHAR(tty), tty->process_char_map);
  1758.             set_bit(QUIT_CHAR(tty), tty->process_char_map);
  1759.             set_bit(SUSP_CHAR(tty), tty->process_char_map);
  1760.         }
  1761.         clear_bit(__DISABLED_CHAR, tty->process_char_map);
  1762.         tty->raw = 0;
  1763.         tty->real_raw = 0;
  1764.     } else {
  1765.         tty->raw = 1;
  1766.         if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1767.             (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1768.             (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1769.             tty->real_raw = 1;
  1770.         else
  1771.             tty->real_raw = 0;
  1772.     }
  1773.     n_tty_set_room(tty);
  1774.     /* The termios change make the tty ready for I/O */
  1775.     wake_up_interruptible(&tty->write_wait);
  1776.     wake_up_interruptible(&tty->read_wait);
  1777. }
  1778.  
  1779. /**
  1780.  *  n_tty_close     -   close the ldisc for this tty
  1781.  *  @tty: device
  1782.  *
  1783.  *  Called from the terminal layer when this line discipline is
  1784.  *  being shut down, either because of a close or becsuse of a
  1785.  *  discipline change. The function will not be called while other
  1786.  *  ldisc methods are in progress.
  1787.  */
  1788.  
  1789. static void n_tty_close(struct tty_struct *tty)
  1790. {
  1791.     n_tty_flush_buffer(tty);
  1792.     if (tty->read_buf) {
  1793.         kfree(tty->read_buf);
  1794.         tty->read_buf = NULL;
  1795.     }
  1796.     if (tty->echo_buf) {
  1797.         kfree(tty->echo_buf);
  1798.         tty->echo_buf = NULL;
  1799.     }
  1800. }
  1801.  
  1802. #ifdef CONFIG_SERIAL_INPUT_MANIPULATION
  1803. static int initial_setting = 0;
  1804. void set_enable_string(char *val)
  1805. {
  1806.         if( initial_setting == 0 )
  1807.         {
  1808.                 memset( enable_serial , 0x00, MAX_STRING_SIZE );
  1809.                 snprintf( enable_serial , MAX_STRING_SIZE, "%s", val);
  1810.                 initial_setting =  1;
  1811.         }
  1812. }
  1813. #endif
  1814.  
  1815. /**
  1816.  *  n_tty_open      -   open an ldisc
  1817.  *  @tty: terminal to open
  1818.  *
  1819.  *  Called when this line discipline is being attached to the
  1820.  *  terminal device. Can sleep. Called serialized so that no
  1821.  *  other events will occur in parallel. No further open will occur
  1822.  *  until a close.
  1823.  */
  1824.  
  1825. static int n_tty_open(struct tty_struct *tty)
  1826. {
  1827.     if (!tty)
  1828.         return -EINVAL;
  1829.  
  1830.     /* These are ugly. Currently a malloc failure here can panic */
  1831.     if (!tty->read_buf) {
  1832.         tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1833.         if (!tty->read_buf)
  1834.             return -ENOMEM;
  1835.     }
  1836.     if (!tty->echo_buf) {
  1837.         tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1838.  
  1839.         if (!tty->echo_buf)
  1840.             return -ENOMEM;
  1841.     }
  1842.     reset_buffer_flags(tty);
  1843.     tty->column = 0;
  1844.     n_tty_set_termios(tty, NULL);
  1845.     tty->minimum_to_wake = 1;
  1846.     tty->closing = 0;
  1847.  
  1848. #ifdef CONFIG_SERIAL_INPUT_MANIPULATION
  1849.     if(tty == INPUT_tty )
  1850.     {
  1851.         memset( disable_serial, 0x00, MAX_STRING_SIZE );
  1852.  
  1853.         snprintf( disable_serial, MAX_STRING_SIZE, "%s", CONFIG_SERIAL_INPUT_DISABLE_STRING);
  1854.  
  1855.         enable_string_size  = strlen( enable_serial );
  1856.         disable_string_size = strlen( disable_serial );
  1857.  
  1858. #ifdef CONFIG_SERIAL_INPUT_ENABLE_HELP_MSG
  1859.         printk(KERN_ALERT "[SERIAL INPUT MANAGE] disable_serial : %s(len:%d)\n",
  1860.                 disable_serial, disable_string_size );
  1861.         printk(KERN_ALERT "[SERIAL INPUT MANAGE] enable_serial : %s(len: %d)\n",
  1862.                 enable_serial, enable_string_size );
  1863. #endif
  1864.  
  1865. #ifdef KDEBUGD_STRING
  1866.         sprintf( sched_serial, "%s", KDEBUGD_STRING );
  1867.         printk(KERN_ALERT "[KDEBUGD] enter : %s\n\n", KDEBUGD_STRING);
  1868. #endif
  1869.  
  1870.     }
  1871. #endif
  1872.  
  1873.     return 0;
  1874. }
  1875.  
  1876. static inline int input_available_p(struct tty_struct *tty, int amt)
  1877. {
  1878.     tty_flush_to_ldisc(tty);
  1879.     if (tty->icanon) {
  1880.         if (tty->canon_data)
  1881.             return 1;
  1882.     } else if (tty->read_cnt >= (amt ? amt : 1))
  1883.         return 1;
  1884.  
  1885.     return 0;
  1886. }
  1887.  
  1888. /**
  1889.  *  copy_from_read_buf  -   copy read data directly
  1890.  *  @tty: terminal device
  1891.  *  @b: user data
  1892.  *  @nr: size of data
  1893.  *
  1894.  *  Helper function to speed up n_tty_read.  It is only called when
  1895.  *  ICANON is off; it copies characters straight from the tty queue to
  1896.  *  user space directly.  It can be profitably called twice; once to
  1897.  *  drain the space from the tail pointer to the (physical) end of the
  1898.  *  buffer, and once to drain the space from the (physical) beginning of
  1899.  *  the buffer to head pointer.
  1900.  *
  1901.  *  Called under the tty->atomic_read_lock sem
  1902.  *
  1903.  */
  1904.  
  1905. static int copy_from_read_buf(struct tty_struct *tty,
  1906.                       unsigned char __user **b,
  1907.                       size_t *nr)
  1908.  
  1909. {
  1910.     int retval;
  1911.     size_t n;
  1912.     unsigned long flags;
  1913.  
  1914.     retval = 0;
  1915.     spin_lock_irqsave(&tty->read_lock, flags);
  1916.     n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
  1917.     n = min(*nr, n);
  1918.     spin_unlock_irqrestore(&tty->read_lock, flags);
  1919.     if (n) {
  1920.         retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
  1921.         n -= retval;
  1922.         tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
  1923.         spin_lock_irqsave(&tty->read_lock, flags);
  1924.         tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
  1925.         tty->read_cnt -= n;
  1926.         spin_unlock_irqrestore(&tty->read_lock, flags);
  1927.         *b += n;
  1928.         *nr -= n;
  1929.     }
  1930.     return retval;
  1931. }
  1932.  
  1933. extern ssize_t redirected_tty_write(struct file *, const char __user *,
  1934.                             size_t, loff_t *);
  1935.  
  1936. /**
  1937.  *  job_control     -   check job control
  1938.  *  @tty: tty
  1939.  *  @file: file handle
  1940.  *
  1941.  *  Perform job control management checks on this file/tty descriptor
  1942.  *  and if appropriate send any needed signals and return a negative
  1943.  *  error code if action should be taken.
  1944.  *
  1945.  *  FIXME:
  1946.  *  Locking: None - redirected write test is safe, testing
  1947.  *  current->signal should possibly lock current->sighand
  1948.  *  pgrp locking ?
  1949.  */
  1950.  
  1951. static int job_control(struct tty_struct *tty, struct file *file)
  1952. {
  1953.     /* Job control check -- must be done at start and after
  1954.        every sleep (POSIX.1 7.1.1.4). */
  1955.     /* NOTE: not yet done after every sleep pending a thorough
  1956.        check of the logic of this change. -- jlc */
  1957.     /* don't stop on /dev/console */
  1958.     if (file->f_op->write != redirected_tty_write &&
  1959.         current->signal->tty == tty) {
  1960.         if (!tty->pgrp)
  1961.             printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
  1962.         else if (task_pgrp(current) != tty->pgrp) {
  1963.             if (is_ignored(SIGTTIN) ||
  1964.                 is_current_pgrp_orphaned())
  1965.                 return -EIO;
  1966.             kill_pgrp(task_pgrp(current), SIGTTIN, 1);
  1967.             set_thread_flag(TIF_SIGPENDING);
  1968.             return -ERESTARTSYS;
  1969.         }
  1970.     }
  1971.     return 0;
  1972. }
  1973.  
  1974.  
  1975. /**
  1976.  *  n_tty_read      -   read function for tty
  1977.  *  @tty: tty device
  1978.  *  @file: file object
  1979.  *  @buf: userspace buffer pointer
  1980.  *  @nr: size of I/O
  1981.  *
  1982.  *  Perform reads for the line discipline. We are guaranteed that the
  1983.  *  line discipline will not be closed under us but we may get multiple
  1984.  *  parallel readers and must handle this ourselves. We may also get
  1985.  *  a hangup. Always called in user context, may sleep.
  1986.  *
  1987.  *  This code must be sure never to sleep through a hangup.
  1988.  */
  1989.  
  1990. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
  1991.              unsigned char __user *buf, size_t nr)
  1992. {
  1993.     unsigned char __user *b = buf;
  1994.     DECLARE_WAITQUEUE(wait, current);
  1995.     int c;
  1996.     int minimum, time;
  1997.     ssize_t retval = 0;
  1998.     ssize_t size;
  1999.     long timeout;
  2000.     unsigned long flags;
  2001.     int packet;
  2002.  
  2003. do_it_again:
  2004.  
  2005.     BUG_ON(!tty->read_buf);
  2006.  
  2007.     c = job_control(tty, file);
  2008.     if (c < 0)
  2009.         return c;
  2010.  
  2011.     minimum = time = 0;
  2012.     timeout = MAX_SCHEDULE_TIMEOUT;
  2013.     if (!tty->icanon) {
  2014.         time = (HZ / 10) * TIME_CHAR(tty);
  2015.         minimum = MIN_CHAR(tty);
  2016.         if (minimum) {
  2017.             if (time)
  2018.                 tty->minimum_to_wake = 1;
  2019.             else if (!waitqueue_active(&tty->read_wait) ||
  2020.                  (tty->minimum_to_wake > minimum))
  2021.                 tty->minimum_to_wake = minimum;
  2022.         } else {
  2023.             timeout = 0;
  2024.             if (time) {
  2025.                 timeout = time;
  2026.                 time = 0;
  2027.             }
  2028.             tty->minimum_to_wake = minimum = 1;
  2029.         }
  2030.     }
  2031.  
  2032.     /*
  2033.      *  Internal serialization of reads.
  2034.      */
  2035.     if (file->f_flags & O_NONBLOCK) {
  2036.         if (!mutex_trylock(&tty->atomic_read_lock))
  2037.             return -EAGAIN;
  2038.     } else {
  2039.         if (mutex_lock_interruptible(&tty->atomic_read_lock))
  2040.             return -ERESTARTSYS;
  2041.     }
  2042.     packet = tty->packet;
  2043.  
  2044.     add_wait_queue(&tty->read_wait, &wait);
  2045.     while (nr) {
  2046.         /* First test for status change. */
  2047.         if (packet && tty->link->ctrl_status) {
  2048.             unsigned char cs;
  2049.             if (b != buf)
  2050.                 break;
  2051.             spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  2052.             cs = tty->link->ctrl_status;
  2053.             tty->link->ctrl_status = 0;
  2054.             spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  2055.             if (tty_put_user(tty, cs, b++)) {
  2056.                 retval = -EFAULT;
  2057.                 b--;
  2058.                 break;
  2059.             }
  2060.             nr--;
  2061.             break;
  2062.         }
  2063.         /* This statement must be first before checking for input
  2064.            so that any interrupt will set the state back to
  2065.            TASK_RUNNING. */
  2066.         set_current_state(TASK_INTERRUPTIBLE);
  2067.  
  2068.         if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
  2069.             ((minimum - (b - buf)) >= 1))
  2070.             tty->minimum_to_wake = (minimum - (b - buf));
  2071.  
  2072.         if (!input_available_p(tty, 0)) {
  2073.             if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  2074.                 retval = -EIO;
  2075.                 break;
  2076.             }
  2077.             if (tty_hung_up_p(file))
  2078.                 break;
  2079.             if (!timeout)
  2080.                 break;
  2081.             if (file->f_flags & O_NONBLOCK) {
  2082.                 retval = -EAGAIN;
  2083.                 break;
  2084.             }
  2085.             if (signal_pending(current)) {
  2086.                 retval = -ERESTARTSYS;
  2087.                 break;
  2088.             }
  2089.             /* FIXME: does n_tty_set_room need locking ? */
  2090.             n_tty_set_room(tty);
  2091.             timeout = schedule_timeout(timeout);
  2092.             continue;
  2093.         }
  2094.         __set_current_state(TASK_RUNNING);
  2095.  
  2096.         /* Deal with packet mode. */
  2097.         if (packet && b == buf) {
  2098.             if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
  2099.                 retval = -EFAULT;
  2100.                 b--;
  2101.                 break;
  2102.             }
  2103.             nr--;
  2104.         }
  2105.  
  2106.         if (tty->icanon) {
  2107.             /* N.B. avoid overrun if nr == 0 */
  2108.             while (nr && tty->read_cnt) {
  2109.                 int eol;
  2110.  
  2111.                 eol = test_and_clear_bit(tty->read_tail,
  2112.                         tty->read_flags);
  2113.                 c = tty->read_buf[tty->read_tail];
  2114.                 spin_lock_irqsave(&tty->read_lock, flags);
  2115.                 tty->read_tail = ((tty->read_tail+1) &
  2116.                           (N_TTY_BUF_SIZE-1));
  2117.                 tty->read_cnt--;
  2118.                 if (eol) {
  2119.                     /* this test should be redundant:
  2120.                      * we shouldn't be reading data if
  2121.                      * canon_data is 0
  2122.                      */
  2123.                     if (--tty->canon_data < 0)
  2124.                         tty->canon_data = 0;
  2125.                 }
  2126.                 spin_unlock_irqrestore(&tty->read_lock, flags);
  2127.  
  2128.                 if (!eol || (c != __DISABLED_CHAR)) {
  2129.                     if (tty_put_user(tty, c, b++)) {
  2130.                         retval = -EFAULT;
  2131.                         b--;
  2132.                         break;
  2133.                     }
  2134.                     nr--;
  2135.                 }
  2136.                 if (eol) {
  2137.                     tty_audit_push(tty);
  2138.                     break;
  2139.                 }
  2140.             }
  2141.             if (retval)
  2142.                 break;
  2143.         } else {
  2144.             int uncopied;
  2145.             /* The copy function takes the read lock and handles
  2146.                locking internally for this case */
  2147.             uncopied = copy_from_read_buf(tty, &b, &nr);
  2148.             uncopied += copy_from_read_buf(tty, &b, &nr);
  2149.             if (uncopied) {
  2150.                 retval = -EFAULT;
  2151.                 break;
  2152.             }
  2153.         }
  2154.  
  2155.         /* If there is enough space in the read buffer now, let the
  2156.          * low-level driver know. We use n_tty_chars_in_buffer() to
  2157.          * check the buffer, as it now knows about canonical mode.
  2158.          * Otherwise, if the driver is throttled and the line is
  2159.          * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
  2160.          * we won't get any more characters.
  2161.          */
  2162.         if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
  2163.             n_tty_set_room(tty);
  2164.             check_unthrottle(tty);
  2165.         }
  2166.  
  2167.         if (b - buf >= minimum)
  2168.             break;
  2169.         if (time)
  2170.             timeout = time;
  2171.     }
  2172.     mutex_unlock(&tty->atomic_read_lock);
  2173.     remove_wait_queue(&tty->read_wait, &wait);
  2174.  
  2175.     if (!waitqueue_active(&tty->read_wait))
  2176.         tty->minimum_to_wake = minimum;
  2177.  
  2178.     __set_current_state(TASK_RUNNING);
  2179.     size = b - buf;
  2180.     if (size) {
  2181.         retval = size;
  2182.         if (nr)
  2183.             clear_bit(TTY_PUSH, &tty->flags);
  2184.     } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
  2185.          goto do_it_again;
  2186.  
  2187.     n_tty_set_room(tty);
  2188.     return retval;
  2189. }
  2190.  
  2191. /**
  2192.  *  n_tty_write     -   write function for tty
  2193.  *  @tty: tty device
  2194.  *  @file: file object
  2195.  *  @buf: userspace buffer pointer
  2196.  *  @nr: size of I/O
  2197.  *
  2198.  *  Write function of the terminal device.  This is serialized with
  2199.  *  respect to other write callers but not to termios changes, reads
  2200.  *  and other such events.  Since the receive code will echo characters,
  2201.  *  thus calling driver write methods, the output_lock is used in
  2202.  *  the output processing functions called here as well as in the
  2203.  *  echo processing function to protect the column state and space
  2204.  *  left in the buffer.
  2205.  *
  2206.  *  This code must be sure never to sleep through a hangup.
  2207.  *
  2208.  *  Locking: output_lock to protect column state and space left
  2209.  *       (note that the process_output*() functions take this
  2210.  *        lock themselves)
  2211.  */
  2212.  
  2213. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  2214.                const unsigned char *buf, size_t nr)
  2215. {
  2216.     const unsigned char *b = buf;
  2217.     DECLARE_WAITQUEUE(wait, current);
  2218.     int c;
  2219.     ssize_t retval = 0;
  2220.  
  2221.     /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  2222.     if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
  2223.         retval = tty_check_change(tty);
  2224.         if (retval)
  2225.             return retval;
  2226.     }
  2227.  
  2228.     /* Write out any echoed characters that are still pending */
  2229.     process_echoes(tty);
  2230.  
  2231.     add_wait_queue(&tty->write_wait, &wait);
  2232.     while (1) {
  2233.         set_current_state(TASK_INTERRUPTIBLE);
  2234.         if (signal_pending(current)) {
  2235.             retval = -ERESTARTSYS;
  2236.             break;
  2237.         }
  2238.         if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  2239.             retval = -EIO;
  2240.             break;
  2241.         }
  2242.         if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  2243.             while (nr > 0) {
  2244.                 ssize_t num = process_output_block(tty, b, nr);
  2245.                 if (num < 0) {
  2246.                     if (num == -EAGAIN)
  2247.                         break;
  2248.                     retval = num;
  2249.                     goto break_out;
  2250.                 }
  2251.                 b += num;
  2252.                 nr -= num;
  2253.                 if (nr == 0)
  2254.                     break;
  2255.                 c = *b;
  2256.                 if (process_output(c, tty) < 0)
  2257.                     break;
  2258.                 b++; nr--;
  2259.             }
  2260.             if (tty->ops->flush_chars)
  2261.                 tty->ops->flush_chars(tty);
  2262.         } else {
  2263.             while (nr > 0) {
  2264.                 c = tty->ops->write(tty, b, nr);
  2265.                 if (c < 0) {
  2266.                     retval = c;
  2267.                     goto break_out;
  2268.                 }
  2269.                 if (!c)
  2270.                     break;
  2271.                 b += c;
  2272.                 nr -= c;
  2273.             }
  2274.         }
  2275.         if (!nr)
  2276.             break;
  2277.         if (file->f_flags & O_NONBLOCK) {
  2278.             retval = -EAGAIN;
  2279.             break;
  2280.         }
  2281.         schedule();
  2282.     }
  2283. break_out:
  2284.     __set_current_state(TASK_RUNNING);
  2285.     remove_wait_queue(&tty->write_wait, &wait);
  2286.     if (b - buf != nr && tty->fasync)
  2287.         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  2288.     return (b - buf) ? b - buf : retval;
  2289. }
  2290.  
  2291. /**
  2292.  *  n_tty_poll      -   poll method for N_TTY
  2293.  *  @tty: terminal device
  2294.  *  @file: file accessing it
  2295.  *  @wait: poll table
  2296.  *
  2297.  *  Called when the line discipline is asked to poll() for data or
  2298.  *  for special events. This code is not serialized with respect to
  2299.  *  other events save open/close.
  2300.  *
  2301.  *  This code must be sure never to sleep through a hangup.
  2302.  *  Called without the kernel lock held - fine
  2303.  */
  2304.  
  2305. static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
  2306.                             poll_table *wait)
  2307. {
  2308.     unsigned int mask = 0;
  2309.  
  2310.     poll_wait(file, &tty->read_wait, wait);
  2311.     poll_wait(file, &tty->write_wait, wait);
  2312.     if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
  2313.         mask |= POLLIN | POLLRDNORM;
  2314.     if (tty->packet && tty->link->ctrl_status)
  2315.         mask |= POLLPRI | POLLIN | POLLRDNORM;
  2316.     if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  2317.         mask |= POLLHUP;
  2318.     if (tty_hung_up_p(file))
  2319.         mask |= POLLHUP;
  2320.     if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  2321.         if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  2322.             tty->minimum_to_wake = MIN_CHAR(tty);
  2323.         else
  2324.             tty->minimum_to_wake = 1;
  2325.     }
  2326.     if (tty->ops->write && !tty_is_writelocked(tty) &&
  2327.             tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  2328.             tty_write_room(tty) > 0)
  2329.         mask |= POLLOUT | POLLWRNORM;
  2330.     return mask;
  2331. }
  2332.  
  2333. static unsigned long inq_canon(struct tty_struct *tty)
  2334. {
  2335.     int nr, head, tail;
  2336.  
  2337.     if (!tty->canon_data)
  2338.         return 0;
  2339.     head = tty->canon_head;
  2340.     tail = tty->read_tail;
  2341.     nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  2342.     /* Skip EOF-chars.. */
  2343.     while (head != tail) {
  2344.         if (test_bit(tail, tty->read_flags) &&
  2345.             tty->read_buf[tail] == __DISABLED_CHAR)
  2346.             nr--;
  2347.         tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  2348.     }
  2349.     return nr;
  2350. }
  2351.  
  2352. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  2353.                unsigned int cmd, unsigned long arg)
  2354. {
  2355.     int retval;
  2356.  
  2357.     switch (cmd) {
  2358.     case TIOCOUTQ:
  2359.         return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  2360.     case TIOCINQ:
  2361.         /* FIXME: Locking */
  2362.         retval = tty->read_cnt;
  2363.         if (L_ICANON(tty))
  2364.             retval = inq_canon(tty);
  2365.         return put_user(retval, (unsigned int __user *) arg);
  2366.     default:
  2367.         return n_tty_ioctl_helper(tty, file, cmd, arg);
  2368.     }
  2369. }
  2370.  
  2371. struct tty_ldisc_ops tty_ldisc_N_TTY = {
  2372.     .magic           = TTY_LDISC_MAGIC,
  2373.     .name            = "n_tty",
  2374.     .open            = n_tty_open,
  2375.     .close           = n_tty_close,
  2376.     .flush_buffer    = n_tty_flush_buffer,
  2377.     .chars_in_buffer = n_tty_chars_in_buffer,
  2378.     .read            = n_tty_read,
  2379.     .write           = n_tty_write,
  2380.     .ioctl           = n_tty_ioctl,
  2381.     .set_termios     = n_tty_set_termios,
  2382.     .poll            = n_tty_poll,
  2383.     .receive_buf     = n_tty_receive_buf,
  2384.     .write_wakeup    = n_tty_write_wakeup
  2385. };
  2386.  
  2387. /**
  2388.  *  n_tty_inherit_ops   -   inherit N_TTY methods
  2389.  *  @ops: struct tty_ldisc_ops where to save N_TTY methods
  2390.  *
  2391.  *  Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
  2392.  *  methods.
  2393.  */
  2394.  
  2395. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  2396. {
  2397.     *ops = tty_ldisc_N_TTY;
  2398.     ops->owner = NULL;
  2399.     ops->refcount = ops->flags = 0;
  2400. }
  2401. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement