39 #include <linux/slab.h>
40 #include <linux/tty.h>
42 #include <linux/sched.h>
47 #ifndef _POSIX_VDISABLE
48 #define _POSIX_VDISABLE ('\0')
55 static void drp_param(
struct ch_struct *);
59 static int set_modem_info(
struct ch_struct *,
unsigned int,
unsigned int *);
60 static int get_modem_info(
struct ch_struct *,
unsigned int *);
61 static void dgrp_set_custom_speed(
struct ch_struct *,
int);
62 static int dgrp_tty_digigetedelay(
struct tty_struct *,
int *);
63 static int dgrp_tty_digisetedelay(
struct tty_struct *,
int *);
64 static int dgrp_send_break(
struct ch_struct *,
int);
67 static tcflag_t ch_to_tty_flags(
unsigned short,
char);
69 static void dgrp_tty_input_start(
struct tty_struct *);
70 static void dgrp_tty_input_stop(
struct tty_struct *);
72 static void drp_wmove(
struct ch_struct *,
int,
void*,
int);
76 static int dgrp_tty_write(
struct tty_struct *,
const unsigned char *,
int);
77 static int dgrp_tty_write_room(
struct tty_struct *);
78 static void dgrp_tty_flush_buffer(
struct tty_struct *);
79 static int dgrp_tty_chars_in_buffer(
struct tty_struct *);
80 static int dgrp_tty_ioctl(
struct tty_struct *,
unsigned int,
unsigned long);
82 static void dgrp_tty_stop(
struct tty_struct *);
83 static void dgrp_tty_start(
struct tty_struct *);
84 static void dgrp_tty_throttle(
struct tty_struct *);
85 static void dgrp_tty_unthrottle(
struct tty_struct *);
86 static void dgrp_tty_hangup(
struct tty_struct *);
87 static int dgrp_tty_put_char(
struct tty_struct *,
unsigned char);
88 static int dgrp_tty_tiocmget(
struct tty_struct *);
89 static int dgrp_tty_tiocmset(
struct tty_struct *,
unsigned int,
unsigned int);
90 static int dgrp_tty_send_break(
struct tty_struct *,
int);
91 static void dgrp_tty_send_xchar(
struct tty_struct *,
char);
96 #define SERIAL_TYPE_NORMAL 1
97 #define SERIAL_TYPE_CALLOUT 2
98 #define SERIAL_TYPE_XPRINT 3
106 #define PORTSERVER_DIVIDEND 1843200
118 .digi_onstr =
"\033[5i",
119 .digi_offstr =
"\033[4i",
130 static struct ktermios DefaultTermios = {
142 .open = dgrp_tty_open,
143 .close = dgrp_tty_close,
144 .write = dgrp_tty_write,
145 .write_room = dgrp_tty_write_room,
146 .flush_buffer = dgrp_tty_flush_buffer,
147 .chars_in_buffer = dgrp_tty_chars_in_buffer,
149 .ioctl = dgrp_tty_ioctl,
150 .set_termios = dgrp_tty_set_termios,
151 .stop = dgrp_tty_stop,
152 .start = dgrp_tty_start,
153 .throttle = dgrp_tty_throttle,
154 .unthrottle = dgrp_tty_unthrottle,
155 .hangup = dgrp_tty_hangup,
156 .put_char = dgrp_tty_put_char,
157 .tiocmget = dgrp_tty_tiocmget,
158 .tiocmset = dgrp_tty_tiocmset,
159 .break_ctl = dgrp_tty_send_break,
160 .send_xchar = dgrp_tty_send_xchar
164 static int calc_baud_rate(
struct un_struct *un)
174 static struct baud_rates baud_rates[] = {
199 for (i = 0; baud_rates[
i].rate; i++) {
200 if (baud_rates[i].
cflag == brate)
204 return baud_rates[
i].rate;
212 ulong bauds[2][16] = {
214 0, 57600, 76800, 115200,
215 131657, 153600, 230400, 460800,
216 921600, 1200, 1800, 2400,
217 4800, 9600, 19200, 38400 },
219 0, 57600, 115200, 230400,
220 460800, 150, 200, 921600,
221 600, 1200, 1800, 2400,
222 4800, 9600, 19200, 38400 }
230 if ((i >= 0) && (i < 2) && (brate >= 0) && (brate < 16))
231 brate = bauds[i][brate];
246 static void drp_param(
struct ch_struct *ch)
263 if (ch->
ch_tun.un_open_count) {
266 tts = &ch->
ch_tun.un_tty->termios;
275 if (ch->
ch_pun.un_open_count) {
277 pts = &ch->
ch_pun.un_tty->termios;
292 }
else if (ch->
ch_pun.un_open_count == 0) {
293 pr_warn(
"%s - ch_pun.un_open_count shouldn't be 0\n",
300 uts = &un->
un_tty->termios;
307 (ch->
ch_tun.un_open_count != 0) &&
322 int oflag = tty_to_ch_flags(un->
un_tty,
'o');
337 oflag = ch_to_tty_flags(ch->
ch_ocook,
'o');
342 int oflag = ch_to_tty_flags(ch->
ch_ocook,
'o');
393 brate = calc_baud_rate(un);
395 brate = calc_fastbaud_rate(un, uts);
419 iflag = (
int) tty_to_ch_flags(un->
un_tty,
'i');
527 (ch->
ch_nd)->nd_tx_work = 1;
537 static void wake_up_drp_sleep_timer(
unsigned long ptr)
549 static void drp_my_sleep(
struct ch_struct *ch)
567 drp_wakeup_timer.function = wake_up_drp_sleep_timer;
568 drp_wakeup_timer.data = (
unsigned long) ch;
569 drp_wakeup_timer.expires =
jiffies + (1 *
HZ);
605 int counts_were_incremented = 0;
717 delay_error = -
EBUSY;
754 delay_error : -
ENXIO ;
827 retval = delay_error;
850 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
879 counts_were_incremented = 1;
940 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
946 if (!counts_were_incremented) {
952 dev_err(tty->
dev,
"tty open bad return (%i)\n", retval);
963 static void dgrp_tty_close(
struct tty_struct *tty,
struct file *file)
974 int sent_printer_offstr = 0;
1033 drp_wmove(ch, 0, ch->
ch_digi.digi_offstr,
1036 sent_printer_offstr = 1;
1060 if (signal_pending(
current)) {
1081 if ((un->
un_tty)->ops->chars_in_buffer ?
1082 ((un->
un_tty)->ops->chars_in_buffer)(un->
un_tty) == 0 : 1) {
1111 ((
long)(jiffies - waketime) >= 0 &&
1119 if (!sent_printer_offstr)
1120 dgrp_tty_flush_buffer(tty);
1132 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
1158 if (sent_printer_offstr) {
1159 spin_unlock_irqrestore(&nd->
nd_lock,
1214 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
1252 "%s - unexpected value for ch->ch_open_count: %i\n",
1259 "%s - unexpected value for un->un_open_count: %i\n",
1268 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
1274 static void drp_wmove(
struct ch_struct *ch,
int from_user,
void *
buf,
int count)
1279 ch->
ch_nd->nd_tx_work = 1;
1286 (
void __user *) buf, n);
1290 buf = (
char *) buf + n;
1297 (
void __user *) buf, count);
1305 static int dgrp_calculate_txprint_bounds(
struct ch_struct *ch,
int space,
1310 unsigned short tmax = 0;
1317 if (ch->
ch_tun.un_open_count != 0 &&
1318 ch->
ch_tun.un_tty->ops->chars_in_buffer &&
1319 ((ch->
ch_tun.un_tty->ops->chars_in_buffer)(ch->
ch_tun.un_tty) != 0)) {
1329 space -= ch->
ch_digi.digi_offlen;
1375 tmax = ch->
ch_digi.digi_bufsize;
1378 tmax = ch->
ch_digi.digi_maxcps * tt /
HZ;
1398 tmax = (ch->
ch_digi.digi_maxchar -
1421 static int dgrp_tty_write(
struct tty_struct *tty,
1422 const unsigned char *buf,
1493 unsigned short tmax = 0;
1500 if (ch->
ch_tun.un_open_count != 0 &&
1501 ((ch->
ch_tun.un_tty->ops->chars_in_buffer)(ch->
ch_tun.un_tty) != 0)) {
1512 space -= ch->
ch_digi.digi_offlen;
1519 space -= ch->
ch_digi.digi_onlen;
1523 (ch->
ch_nd)->nd_tx_work = 1;
1528 drp_wmove(ch, 0, ch->
ch_digi.digi_onstr,
1573 tmax = ch->
ch_digi.digi_bufsize;
1576 tmax = ch->
ch_digi.digi_maxcps * tt /
HZ;
1596 tmax = (ch->
ch_digi.digi_maxchar -
1624 space -= ch->
ch_digi.digi_offlen;
1626 drp_wmove(ch, 0, ch->
ch_digi.digi_offstr,
1645 (ch->
ch_nd)->nd_tx_work = 1;
1650 count =
min(count, space);
1682 (ch->
ch_nd)->nd_tx_work = 1;
1684 (ch->
ch_nd)->nd_tx_ready = 1;
1698 if (sendcount > 0) {
1711 if ((un_flag &
UN_TIME) != 0) {
1713 (ch->
ch_digi.digi_maxchar * HZ /
1724 (ch->
ch_nd)->nd_tx_work = 1;
1739 static int dgrp_tty_put_char(
struct tty_struct *tty,
unsigned char new_char)
1778 if (space < ch->
ch_digi.digi_onlen) {
1782 space -= ch->
ch_digi.digi_onlen;
1783 drp_wmove(ch, 0, ch->
ch_digi.digi_onstr,
1794 if (space < ch->
ch_digi.digi_offlen) {
1799 space -= ch->
ch_digi.digi_offlen;
1800 drp_wmove(ch, 0, ch->
ch_digi.digi_offstr,
1836 (ch->
ch_digi.digi_maxchar * HZ /
1842 (ch->
ch_nd)->nd_tx_work = 1;
1857 static void dgrp_tty_flush_buffer(
struct tty_struct *tty)
1878 (ch->
ch_nd)->nd_tx_ready = 1;
1879 (ch->
ch_nd)->nd_tx_work = 1;
1893 static int dgrp_tty_write_room(
struct tty_struct *tty)
1919 count = dgrp_calculate_txprint_bounds(ch, count, &un_flag);
1923 ch->
ch_pun.un_flag |= un_flag;
1924 (ch->
ch_nd)->nd_tx_work = 1;
1940 static int dgrp_tty_chars_in_buffer(
struct tty_struct *tty)
2067 tflag = tty->termios.c_iflag;
2079 tflag = tty->termios.c_oflag;
2098 tflag = tty->termios.c_cflag;
2151 dgrp_send_break(ch, msec);
2161 static int dgrp_send_break(
struct ch_struct *ch,
int msec)
2170 (ch->
ch_nd)->nd_tx_work = 1;
2172 x = (msec *
HZ) / 1000;
2182 static int dgrp_tty_tiocmget(
struct tty_struct *tty)
2213 static int dgrp_tty_tiocmset(
struct tty_struct *tty,
2214 unsigned int set,
unsigned int clear)
2241 (ch->
ch_nd)->nd_tx_work = 1;
2244 spin_unlock_irqrestore(&(ch->
ch_nd)->nd_lock, lock_flags);
2254 static int get_modem_info(
struct ch_struct *ch,
unsigned int *
value)
2262 mlast = ((mlast &
DM_RTS) ? TIOCM_RTS : 0)
2263 | ((mlast &
DM_DTR) ? TIOCM_DTR : 0)
2268 put_user(mlast, (
unsigned int __user *) value);
2277 unsigned int *value)
2288 get_user(arg, (
unsigned int __user *) value);
2309 (ch->
ch_nd)->nd_tx_work = 1;
2312 spin_unlock_irqrestore(&(ch->
ch_nd)->nd_lock, lock_flags);
2321 static void dgrp_set_custom_speed(
struct ch_struct *ch,
int newrate)
2327 int deltahigh, deltalow;
2342 while (newrate > 0) {
2359 if (testrate_high == newrate)
2366 deltahigh = testrate_high - newrate;
2367 deltalow = newrate - testrate_low;
2369 if (deltahigh < deltalow)
2370 newrate = testrate_high;
2372 newrate = testrate_low;
2392 static int dgrp_tty_digiseta(
struct tty_struct *tty,
2411 tty->termios.c_cflag |=
CRTSCTS;
2413 tty->termios.c_cflag &= ~
CRTSCTS;
2415 if (ch->
ch_digi.digi_maxcps < 1)
2418 if (ch->
ch_digi.digi_maxcps > 10000)
2419 ch->
ch_digi.digi_maxcps = 10000;
2421 if (ch->
ch_digi.digi_bufsize < 10)
2422 ch->
ch_digi.digi_bufsize = 10;
2424 if (ch->
ch_digi.digi_maxchar < 1)
2452 static int dgrp_tty_digigetedelay(
struct tty_struct *tty,
int *retinfo)
2475 if (
copy_to_user((
void __user *) retinfo, &tmp,
sizeof(*retinfo)))
2488 static int dgrp_tty_digisetedelay(
struct tty_struct *tty,
int *new_info)
2506 if (
copy_from_user(&new_digi, (
void __user *)new_info,
sizeof(
int)))
2527 static int dgrp_tty_ioctl(
struct tty_struct *tty,
unsigned int cmd,
2567 rc = dgrp_send_break(ch, 250);
2569 if (dgrp_tty_chars_in_buffer(tty) != 0)
2586 rc = dgrp_send_break(ch, arg ? arg*250 : 250);
2588 if (dgrp_tty_chars_in_buffer(tty) != 0)
2603 rc = dgrp_send_break(ch, 250);
2605 if (dgrp_tty_chars_in_buffer(tty) != 0)
2627 get_user(arg, (
unsigned long __user *) arg);
2628 tty->termios.c_cflag =
2629 ((tty->termios.c_cflag & ~
CLOCAL) |
2635 sizeof(
unsigned int));
2638 return get_modem_info(ch, (
unsigned int *) arg);
2643 return set_modem_info(ch, cmd, (
unsigned int *) arg);
2670 (ch->
ch_nd)->nd_tx_work = 1;
2671 (ch->
ch_nd)->nd_tx_ready = 1;
2738 !ch->
ch_tun.un_open_count) {
2751 (ch->
ch_nd)->nd_tx_ready = 1;
2752 (ch->
ch_nd)->nd_tx_work = 1;
2775 dgrp_tty_start(tty);
2781 dgrp_tty_input_start(tty);
2784 dgrp_tty_input_stop(tty);
2806 (ch->
ch_nd)->nd_tx_ready = 1;
2807 (ch->
ch_nd)->nd_tx_work = 1;
2816 return dgrp_tty_digiseta(tty, (
struct digi_struct *) arg);
2819 return dgrp_tty_digisetedelay(tty, (
int *) arg);
2822 return dgrp_tty_digigetedelay(tty, (
int *) arg);
2827 dflow.startc = tty->termios.c_cc[
VSTART];
2828 dflow.stopc = tty->termios.c_cc[
VSTOP];
2834 if (
copy_to_user((
char __user *)arg, &dflow,
sizeof(dflow)))
2845 tty->termios.c_cc[
VSTART] = dflow.startc;
2846 tty->termios.c_cc[
VSTOP] = dflow.stopc;
2864 get_user(new_rate, (
unsigned int __user *) arg);
2865 dgrp_set_custom_speed(ch, new_rate);
2919 static void dgrp_tty_throttle(
struct tty_struct *tty)
2934 static void dgrp_tty_unthrottle(
struct tty_struct *tty)
2951 static void dgrp_tty_stop(
struct tty_struct *tty)
2966 (ch->
ch_nd)->nd_tx_ready = 1;
2967 if (waitqueue_active(&(ch->
ch_nd)->nd_tx_waitq))
2974 static void dgrp_tty_start(
struct tty_struct *tty)
2991 (ch->
ch_nd)->nd_tx_ready = 1;
2992 (ch->
ch_nd)->nd_tx_work = 1;
2993 if (waitqueue_active(&(ch->
ch_nd)->nd_tx_waitq))
3001 static void dgrp_tty_input_stop(
struct tty_struct *tty)
3014 (ch->
ch_nd)->nd_tx_ready = 1;
3015 if (waitqueue_active(&(ch->
ch_nd)->nd_tx_waitq))
3021 static void dgrp_tty_send_xchar(
struct tty_struct *tty,
char c)
3041 ch->
ch_nd->nd_tx_ready = 1;
3042 ch->
ch_nd->nd_tx_work = 1;
3048 static void dgrp_tty_input_start(
struct tty_struct *tty)
3061 (ch->
ch_nd)->nd_tx_ready = 1;
3062 (ch->
ch_nd)->nd_tx_work = 1;
3063 if (waitqueue_active(&(ch->
ch_nd)->nd_tx_waitq))
3075 static void dgrp_tty_hangup(
struct tty_struct *tty)
3099 ch->
ch_nd->nd_tx_ready = 1;
3100 ch->
ch_nd->nd_tx_work = 1;
3124 ID_TO_CHAR(nd->
nd_ID,
id);
3169 ID_TO_CHAR(nd->
nd_ID,
id);
3219 for (i = 256; i < max_majors; i++) {
3227 if (i == max_majors)