36 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/tty.h>
43 #include <linux/poll.h>
44 #include <linux/sched.h>
46 #include <asm/unaligned.h>
48 #define MYFLIPLEN TBUF_MAX
52 #define TTY_FLIPBUF_SIZE 512
53 #define DEVICE_NAME_SIZE 50
58 static void parity_scan(
struct ch_struct *ch,
unsigned char *
cbuf,
59 unsigned char *fbuf,
int *len);
64 static int dgrp_net_open(
struct inode *,
struct file *);
65 static int dgrp_net_release(
struct inode *,
struct file *);
66 static ssize_t dgrp_net_read(
struct file *,
char __user *,
size_t, loff_t *);
67 static ssize_t dgrp_net_write(
struct file *,
const char __user *,
size_t,
69 static long dgrp_net_ioctl(
struct file *
file,
unsigned int cmd,
71 static unsigned int dgrp_net_select(
struct file *
file,
76 .read = dgrp_net_read,
77 .write = dgrp_net_write,
78 .poll = dgrp_net_select,
79 .unlocked_ioctl = dgrp_net_ioctl,
80 .open = dgrp_net_open,
81 .release = dgrp_net_release,
106 static void dgrp_dump(
u8 *
mem,
int len)
110 pr_debug(
"dgrp dump length = %d, data = ", len);
111 for (i = 0; i < len; ++
i)
122 static void dgrp_read_data_block(
struct ch_struct *ch,
u8 *flipbuf,
128 if (flipbuf_size <= 0)
157 static void dgrp_input(
struct ch_struct *ch)
194 tty = (ch->
ch_tun).un_tty;
202 tty_count = tty->
count;
213 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
226 len =
min(data_len, flip_len);
240 }
else if (!ld->
ops->receive_buf) {
243 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
255 dgrp_read_data_block(ch, myflipbuf, len);
263 parity_scan(ch, myflipbuf, myflipflagbuf, &len);
277 ld->
ops->receive_buf(tty, myflipbuf,
NULL, len);
301 spin_unlock_irqrestore(&nd->
nd_lock, lock_flags);
326 static void parity_scan(
struct ch_struct *ch,
unsigned char *
cbuf,
327 unsigned char *fbuf,
int *len)
395 pr_info(
"%s: parity_scan: error unexpected byte\n",
419 static void dgrp_net_idle(
struct nd_struct *nd)
460 static void increase_channel_count(
struct nd_struct *nd,
int n)
488 pr_info(
"%s - ch_rbuf was not NULL\n",
496 ch->
ch_tun.un_sysfs = classp;
501 &classp->
kobj, name);
509 ch->
ch_pun.un_sysfs = classp;
514 &classp->
kobj, name);
526 static void decrease_channel_count(
struct nd_struct *nd,
int n)
601 static void dgrp_chan_count(
struct nd_struct *nd,
int n)
607 increase_channel_count(nd, n);
609 if (n < nd->nd_chan_count)
610 decrease_channel_count(nd, n);
623 static void dgrp_monitor(
struct nd_struct *nd,
u8 *buf,
int len)
723 static void dgrp_encode_time(
struct nd_struct *nd,
u8 *buf)
732 t = 1000 * (t /
HZ) + 1000 * (t %
HZ) /
HZ;
751 dgrp_encode_time(nd, header + 1);
755 put_unaligned_be16(n, header + 5);
757 dgrp_monitor(nd, header,
sizeof(header));
758 dgrp_monitor(nd, (
u8 *) message, n);
767 static void dgrp_monitor_reset(
struct nd_struct *nd)
773 dgrp_encode_time(nd, header + 1);
775 dgrp_monitor(nd, header,
sizeof(header));
791 dgrp_encode_time(nd, header + 1);
793 put_unaligned_be16(size, header + 5);
795 dgrp_monitor(nd, header,
sizeof(header));
796 dgrp_monitor(nd, buf, size);
799 static int alloc_nd_buffers(
struct nd_struct *nd)
909 nd->
nd_link.lk_fast_delay = 1000;
910 nd->
nd_link.lk_slow_delay = 1000;
912 nd->
nd_link.lk_header_size = 46;
915 rtn = alloc_nd_buffers(nd);
940 dgrp_monitor_message(nd,
"Net Open");
956 static int dgrp_net_release(
struct inode *inode,
struct file *file)
995 pr_info(
"%s - expected waitqueue_active to be false\n",
1032 dgrp_chan_count(nd, 0);
1063 dgrp_monitor_message(nd,
"Net Close");
1075 *b++ = 0xb0 + (port & 0x0f);
1087 static int dgrp_send(
struct nd_struct *nd,
long tmax)
1108 long wanted_sync_port = -1;
1120 memset(tdata, 0,
sizeof(tdata));
1160 maxport = port + 16;
1165 for (; port < maxport; port++, ch++) {
1208 b = set_cmd_header(b, port, 10);
1224 b = set_cmd_header(b, port, 10);
1230 b = set_cmd_header(b, port, 10);
1246 b = set_cmd_header(b, port, 10);
1302 b = set_cmd_header(b, port, 14);
1305 b = set_cmd_header(b, port, 42);
1306 put_unaligned_be16(0x02c0, b);
1312 b = set_cmd_header(b, port, 16);
1315 b = set_cmd_header(b, port, 20);
1318 b = set_cmd_header(b, port, 22);
1328 b = set_cmd_header(b, port, 44);
1371 if (ch->
ch_tun.un_open_count == 0 ||
1409 b = set_cmd_header(b, port, 48);
1429 b = set_cmd_header(b, port, 45);
1453 b = set_cmd_header(b, port, 40);
1489 b = set_cmd_header(b, port, 44);
1505 b = set_cmd_header(b, port, 46);
1520 b = set_cmd_header(b, port, 47);
1549 b = set_cmd_header(b, port, 60);
1558 b = set_cmd_header(b, port, 61);
1577 b = set_cmd_header(b, port, 62);
1582 if (send & RR_RX_FLUSH) {
1587 wanted_sync_port =
port;
1595 b = set_cmd_header(b, port, 63);
1611 b = set_cmd_header(b, port, 64);
1637 b[0] = 0xa0 + (port & 0xf);
1639 put_unaligned_be16(rwin, b + 1);
1659 if (waitqueue_active(&((ch->
ch_tun.un_tty)->write_wait)))
1674 if (ch->
ch_pun.un_open_count &&
1684 ((ch->
ch_tun.un_open_count &&
1685 ch->
ch_tun.un_tty->ops->chars_in_buffer) ?
1686 (ch->
ch_tun.un_tty->ops->chars_in_buffer)(ch->
ch_tun.un_tty) == 0
1692 if (waitqueue_active(&((ch->
ch_pun.un_tty)->write_wait)))
1715 n += (n <= 8 ? 1 : n <= 255 ? 2 : 3);
1728 b = set_cmd_header(b, port, 10);
1749 pr_info(
"%s - unexpected channel state (%i)\n",
1761 mbuf[-1] = 0xf0 |
mod;
1815 if (tmax < 2 * nd->nd_chan_count) {
1818 }
else if (tchan > 1 && ttotal > tmax) {
1832 for (
try = 0;
try < 3;
try++) {
1836 for (i = 0; i <
tc; i++) {
1837 if (tsend < tdata[i])
1838 tdata[c++] = tdata[
i];
1869 tsend -= (tsend <= 9) ? 1 : (tsend <= 257) ? 2 : 3;
1892 maxport = port + 16;
1897 for (; port < maxport; port++, ch++) {
1909 if (n != 0 && used_buffer > 0) {
1920 if (n > used_buffer) {
1935 b[0] = ((n - 1) << 4) + (port & 0xf);
1938 }
else if (n <= 255) {
1940 b[0] = 0x80 + (port & 0xf);
1946 b[0] = 0x90 + (port & 0xf);
1947 put_unaligned_be16(n, b + 1);
1988 if (ch->
ch_tun.un_open_count) {
1997 if (ch->
ch_pun.un_open_count) {
2021 b = set_cmd_header(b, port, 43);
2035 (ch->
ch_send & RR_TX_BREAK) == 0) {
2050 mbuf[-1] = 0xf0 |
mod;
2066 if ((send_sync || nd->
nd_seq_wait[in] != 0) && lastport >= 0) {
2076 if (wanted_sync_port >= 0)
2077 lastport = wanted_sync_port;
2086 mod = lastport >> 4;
2095 bb = set_cmd_header(bb, lastport, 12);
2138 pr_info(
"%s - n(%i) >= tsafe(%i)\n",
2139 __func__, n, tsafe);
2153 static ssize_t dgrp_net_read(
struct file *file,
char __user *buf,
size_t count,
2207 dgrp_monitor_reset(nd);
2248 b = dgrp_send(nd, count) + local_buf;
2268 dgrp_chan_count(nd, 0);
2310 static void dgrp_receive(
struct nd_struct *nd)
2331 ID_TO_CHAR(nd->
nd_ID, ID);
2340 while (remain > 0) {
2342 int n1 = b[0] & 0x0f;
2348 error =
"Improper Port Number";
2404 dlen = get_unaligned_be16(b + 1);
2422 error =
"Data received before RWIN established";
2434 error =
"Receive data overrun";
2453 }
else if (dlen <= 3) {
2472 dlen -= plen - remain;
2492 (ch->
ch_tun.un_open_count != 0) &&
2531 if (waitqueue_active(&ch->
ch_tun.un_tty->read_wait) &&
2552 if (remain < plen) {
2553 dlen = plen - remain;
2557 put_unaligned_be16(dlen, b + 1);
2576 ushort tpos = get_unaligned_be16(b + 1);
2583 error =
"Improper Window Sequence";
2634 port = get_unaligned_be16(b + 4);
2637 error =
"Open channel number out of range";
2763 if (req == 4 && resp == 0) {
2786 error =
"Improper Open Response";
2988 estat = ch->
ch_s_elast = get_unaligned_be16(b + 2);
2994 if (((mstat ^ mlast) &
DM_CD) != 0)
3002 if ((estat & ~elast &
EV_RXB) != 0 &&
3003 (ch->
ch_tun.un_open_count != 0) &&
3019 if ((~estat & elast &
EV_TXB) != 0 &&
3038 error =
"Unrecognized command";
3083 plen = get_unaligned_be16(b + 2);
3085 if (plen < 12 || plen > 1000) {
3086 error =
"Response Packet length error";
3128 plen = get_unaligned_be16(b + 2);
3130 if (plen < 4 || plen > 1000) {
3131 error =
"Response Packet length error";
3161 error =
"Response Packet desclen error";
3178 int nn = get_unaligned_be16(b + 4);
3183 dgrp_chan_count(nd, nn);
3238 plen = get_unaligned_be16(b + 2) + 4;
3241 error =
"Debug Packet too large";
3269 error =
"Client Reset Acknowledge";
3292 if (remain > 0 && b != buf)
3303 error =
"Protocol decode error";
3323 static ssize_t dgrp_net_write(
struct file *file,
const char __user *buf,
3324 size_t count, loff_t *ppos)
3354 dgrp_chan_count(nd, 0);
3371 (
void __user *) buf + total, n);
3409 static unsigned int dgrp_net_select(
struct file *file,
3432 static long dgrp_net_ioctl(
struct file *file,
unsigned int cmd,
3458 if (
link.lk_fast_rate < 9600)
3459 link.lk_fast_rate = 9600;
3461 if (
link.lk_slow_rate < 2400)
3462 link.lk_slow_rate = 2400;
3464 if (
link.lk_fast_rate > 10000000)
3465 link.lk_fast_rate = 10000000;
3467 if (
link.lk_slow_rate >
link.lk_fast_rate)
3468 link.lk_slow_rate =
link.lk_fast_rate;
3470 if (
link.lk_fast_delay > 2000)
3471 link.lk_fast_delay = 2000;
3473 if (
link.lk_slow_delay > 10000)
3474 link.lk_slow_delay = 10000;
3476 if (
link.lk_fast_delay < 60)
3477 link.lk_fast_delay = 60;
3479 if (
link.lk_slow_delay <
link.lk_fast_delay)
3480 link.lk_slow_delay =
link.lk_fast_delay;
3482 if (
link.lk_header_size < 2)
3483 link.lk_header_size = 2;
3485 if (
link.lk_header_size > 128)
3486 link.lk_header_size = 128;
3502 if (
copy_to_user((
void __user *)arg, (
void *)(&nd->nd_link),
3606 if (seq_in == seq_out) {
3671 size = 2 * nd->
nd_link.lk_header_size;
3678 excess = deposit - charge -
size;
3724 poll_time = poll_data->
timer.expires +
3732 poll_data->
timer.expires = poll_time;
3736 spin_unlock_irqrestore(&poll_data->
poll_lock, lock_flags);