3 * Copyright 1999 Digi International (www.digi.com)
4 * James Puzzo <jamesp at digi dot com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
26 * Handle the file operations required for the "network" devices.
27 * Includes those functions required to register the "net" devices
36 #include <linux/module.h>
37 #include <linux/proc_fs.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/spinlock.h>
43 #include <linux/poll.h>
44 #include <linux/sched.h>
45 #include <linux/ratelimit.h>
46 #include <asm/unaligned.h>
48 #define MYFLIPLEN TBUF_MAX
50 #include "dgrp_common.h"
52 #define TTY_FLIPBUF_SIZE 512
53 #define DEVICE_NAME_SIZE 50
56 * Generic helper function declarations
58 static void parity_scan(struct ch_struct *ch, unsigned char *cbuf,
59 unsigned char *fbuf, int *len);
62 * File operation declarations
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,
72 struct poll_table_struct *table);
74 static const struct file_operations net_ops = {
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,
84 static struct inode_operations net_inode_ops = {
85 .permission = dgrp_inode_permission
88 void dgrp_register_net_hook(struct proc_dir_entry *de)
90 struct nd_struct *node = de->data;
92 de->proc_iops = &net_inode_ops;
93 de->proc_fops = &net_ops;
95 sema_init(&node->nd_net_semaphore, 1);
96 node->nd_state = NS_CLOSED;
97 dgrp_create_node_class_sysfs_files(node);
102 * dgrp_dump() -- prints memory for debugging purposes.
103 * @mem: Memory location which should be printed to the console
104 * @len: Number of bytes to be dumped
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)
112 pr_debug("%.2x ", mem[i]);
117 * dgrp_read_data_block() -- Read a data block
118 * @ch: struct ch_struct *
120 * @flipbuf_size: size of flipbuf
122 static void dgrp_read_data_block(struct ch_struct *ch, u8 *flipbuf,
128 if (flipbuf_size <= 0)
131 t = RBUF_MAX - ch->ch_rout;
135 memcpy(flipbuf, ch->ch_rbuf + ch->ch_rout, t);
141 memcpy(flipbuf, ch->ch_rbuf + ch->ch_rout, n);
148 * dgrp_input() -- send data to the line disipline
149 * @ch: pointer to channel struct
151 * Copys the rbuf to the flipbuf and sends to line discipline.
152 * Sends input buffer data to the line discipline.
154 * There are several modes to consider here:
155 * rawreadok, tty->real_raw, and IF_PARMRK
157 static void dgrp_input(struct ch_struct *ch)
159 struct nd_struct *nd;
160 struct tty_struct *tty;
167 struct tty_ldisc *ld;
179 spin_lock_irqsave(&nd->nd_lock, lock_flags);
181 myflipbuf = nd->nd_inputbuf;
182 myflipflagbuf = nd->nd_inputflagbuf;
184 if (!ch->ch_open_count) {
185 ch->ch_rout = ch->ch_rin;
189 if (ch->ch_tun.un_flag & UN_CLOSING) {
190 ch->ch_rout = ch->ch_rin;
194 tty = (ch->ch_tun).un_tty;
197 if (!tty || tty->magic != TTY_MAGIC) {
198 ch->ch_rout = ch->ch_rin;
202 tty_count = tty->count;
204 ch->ch_rout = ch->ch_rin;
208 if (tty->closing || test_bit(TTY_CLOSING, &tty->flags)) {
209 ch->ch_rout = ch->ch_rin;
213 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
215 /* Decide how much data we can send into the tty layer */
216 if (dgrp_rawreadok && tty->real_raw)
217 flip_len = MYFLIPLEN;
219 flip_len = TTY_FLIPBUF_SIZE;
221 /* data_len should be the number of chars that we read in */
222 data_len = (ch->ch_rin - ch->ch_rout) & RBUF_MASK;
225 /* len is the amount of data we are going to transfer here */
226 len = min(data_len, flip_len);
228 /* take into consideration length of ldisc */
229 len = min(len, (N_TTY_BUF_SIZE - 1) - tty->read_cnt);
231 ld = tty_ldisc_ref(tty);
234 * If we were unable to get a reference to the ld,
235 * don't flush our buffer, and act like the ld doesn't
236 * have any space to put the data right now.
240 } else if (!ld->ops->receive_buf) {
241 spin_lock_irqsave(&nd->nd_lock, lock_flags);
242 ch->ch_rout = ch->ch_rin;
243 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
247 /* Check DPA flow control */
248 if ((nd->nd_dpa_debug) &&
249 (nd->nd_dpa_flag & DPA_WAIT_SPACE) &&
250 (nd->nd_dpa_port == MINOR(tty_devnum(ch->ch_tun.un_tty))))
253 if ((len) && !(ch->ch_flag & CH_RXSTOP)) {
255 dgrp_read_data_block(ch, myflipbuf, len);
258 * In high performance mode, we don't have to update
259 * flag_buf or any of the counts or pointers into flip buf.
261 if (!dgrp_rawreadok || !tty->real_raw) {
262 if (I_PARMRK(tty) || I_BRKINT(tty) || I_INPCK(tty))
263 parity_scan(ch, myflipbuf, myflipflagbuf, &len);
265 memset(myflipflagbuf, TTY_NORMAL, len);
268 if ((nd->nd_dpa_debug) &&
269 (nd->nd_dpa_port == PORT_NUM(MINOR(tty_devnum(tty)))))
270 dgrp_dpa_data(nd, 1, myflipbuf, len);
273 * If we're doing raw reads, jam it right into the
274 * line disc bypassing the flip buffers.
276 if (dgrp_rawreadok && tty->real_raw)
277 ld->ops->receive_buf(tty, myflipbuf, NULL, len);
279 len = tty_buffer_request_room(tty, len);
280 tty_insert_flip_string_flags(tty, myflipbuf,
283 /* Tell the tty layer its okay to "eat" the data now */
284 tty_flip_buffer_push(tty);
287 ch->ch_rxcount += len;
294 * Wake up any sleepers (maybe dgrp close) that might be waiting
295 * for a channel flag state change.
297 wake_up_interruptible(&ch->ch_flag_wait);
301 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
308 * Loop to inspect each single character or 0xFF escape.
310 * if PARMRK & ~DOSMODE:
311 * 0xFF 0xFF Normal 0xFF character, escaped
312 * to eliminate confusion.
313 * 0xFF 0x00 0x00 Break
314 * 0xFF 0x00 CC Error character CC.
315 * CC Normal character CC.
317 * if PARMRK & DOSMODE:
318 * 0xFF 0x18 0x00 Break
319 * 0xFF 0x08 0x00 Framing Error
320 * 0xFF 0x04 0x00 Parity error
321 * 0xFF 0x0C 0x00 Both Framing and Parity error
323 * TODO: do we need to do the XMODEM, XOFF, XON, XANY processing??
326 static void parity_scan(struct ch_struct *ch, unsigned char *cbuf,
327 unsigned char *fbuf, int *len)
331 int DOS = ((ch->ch_iflag & IF_DOSMODE) == 0 ? 0 : 1);
332 unsigned char *cout; /* character buffer */
333 unsigned char *fout; /* flag buffer */
345 switch (ch->ch_pscan_state) {
347 /* reset to sanity and fall through */
348 ch->ch_pscan_state = 0 ;
352 if (c == 0xff) /* delete this character from stream */
353 ch->ch_pscan_state = 1;
356 *fout++ = TTY_NORMAL;
364 /* doubled ff, transform to single ff */
366 *fout++ = TTY_NORMAL;
368 ch->ch_pscan_state = 0;
370 /* save value examination in next state */
371 ch->ch_pscan_savechar = c;
372 ch->ch_pscan_state = 2;
377 /* third character of ff sequence */
380 if (ch->ch_pscan_savechar & 0x10)
382 else if (ch->ch_pscan_savechar & 0x08)
386 * either marked as a parity error,
387 * indeterminate, or not in DOSMODE
388 * call it a parity error
390 *fout++ = TTY_PARITY;
392 /* case FF XX ?? where XX is not 00 */
393 if (ch->ch_pscan_savechar & 0xff) {
394 /* this should not happen */
395 pr_info("%s: parity_scan: error unexpected byte\n",
397 *fout++ = TTY_PARITY;
399 /* case FF 00 XX where XX is not 00 */
401 *fout++ = TTY_PARITY;
408 ch->ch_pscan_state = 0;
416 * dgrp_net_idle() -- Idle the network connection
417 * @nd: pointer to node structure to idle
419 static void dgrp_net_idle(struct nd_struct *nd)
421 struct ch_struct *ch;
426 nd->nd_state = NS_IDLE;
429 for (i = nd->nd_seq_out; ; i = (i + 1) & SEQ_MASK) {
430 if (!nd->nd_seq_wait[i]) {
431 nd->nd_seq_wait[i] = 0;
432 wake_up_interruptible(&nd->nd_seq_wque[i]);
435 if (i == nd->nd_seq_in)
439 nd->nd_seq_out = nd->nd_seq_in;
444 nd->nd_tx_module = 0x10;
445 nd->nd_rx_module = 0x00;
447 for (i = 0, ch = nd->nd_chan; i < CHAN_MAX; i++, ch++) {
448 ch->ch_state = CS_IDLE;
451 ch->ch_otype_waiting = 0;
456 * Increase the number of channels, waking up any
457 * threads that might be waiting for the channels
460 static void increase_channel_count(struct nd_struct *nd, int n)
462 struct ch_struct *ch;
463 struct device *classp;
464 char name[DEVICE_NAME_SIZE];
469 for (i = nd->nd_chan_count; i < n; ++i) {
470 ch = nd->nd_chan + i;
472 /* FIXME: return a useful error instead! */
473 buf = kmalloc(TBUF_MAX, GFP_KERNEL);
478 pr_info_ratelimited("%s - ch_tbuf was not NULL\n",
483 buf = kmalloc(RBUF_MAX, GFP_KERNEL);
488 pr_info("%s - ch_rbuf was not NULL\n",
492 classp = tty_port_register_device(&ch->port,
493 nd->nd_serial_ttdriver, i,
496 ch->ch_tun.un_sysfs = classp;
497 snprintf(name, DEVICE_NAME_SIZE, "tty_%d", i);
499 dgrp_create_tty_sysfs(&ch->ch_tun, classp);
500 ret = sysfs_create_link(&nd->nd_class_dev->kobj,
501 &classp->kobj, name);
503 /* NOTE: We don't support "cu" devices anymore,
504 * so you will notice we don't register them
506 if (dgrp_register_prdevices) {
507 classp = tty_register_device(nd->nd_xprint_ttdriver,
509 ch->ch_pun.un_sysfs = classp;
510 snprintf(name, DEVICE_NAME_SIZE, "pr_%d", i);
512 dgrp_create_tty_sysfs(&ch->ch_pun, classp);
513 ret = sysfs_create_link(&nd->nd_class_dev->kobj,
514 &classp->kobj, name);
517 nd->nd_chan_count = i + 1;
518 wake_up_interruptible(&ch->ch_flag_wait);
523 * Decrease the number of channels, and wake up any threads that might
524 * be waiting on the channels that vanished.
526 static void decrease_channel_count(struct nd_struct *nd, int n)
528 struct ch_struct *ch;
529 char name[DEVICE_NAME_SIZE];
532 for (i = nd->nd_chan_count - 1; i >= n; --i) {
533 ch = nd->nd_chan + i;
536 * Make any open ports inoperative.
538 ch->ch_state = CS_IDLE;
541 ch->ch_otype_waiting = 0;
544 * Only "HANGUP" if we care about carrier
545 * transitions and we are already open.
547 if (ch->ch_open_count != 0) {
548 ch->ch_flag |= CH_HANGUP;
553 * Unlike the CH_HANGUP flag above, use another
554 * flag to indicate to the RealPort state machine
555 * that this port has disappeared.
557 if (ch->ch_open_count != 0)
558 ch->ch_flag |= CH_PORT_GONE;
560 wake_up_interruptible(&ch->ch_flag_wait);
562 nd->nd_chan_count = i;
570 nd->nd_chan_count = i;
572 dgrp_remove_tty_sysfs(ch->ch_tun.un_sysfs);
573 snprintf(name, DEVICE_NAME_SIZE, "tty_%d", i);
574 sysfs_remove_link(&nd->nd_class_dev->kobj, name);
575 tty_unregister_device(nd->nd_serial_ttdriver, i);
578 * NOTE: We don't support "cu" devices anymore, so don't
579 * unregister them here anymore.
582 if (dgrp_register_prdevices) {
583 dgrp_remove_tty_sysfs(ch->ch_pun.un_sysfs);
584 snprintf(name, DEVICE_NAME_SIZE, "pr_%d", i);
585 sysfs_remove_link(&nd->nd_class_dev->kobj, name);
586 tty_unregister_device(nd->nd_xprint_ttdriver, i);
592 * dgrp_chan_count() -- Adjust the node channel count.
593 * @nd: pointer to a node structure
594 * @n: new value for channel count
596 * Adjusts the node channel count. If new ports have appeared, it tries
597 * to signal those processes that might have been waiting for ports to
598 * appear. If ports have disappeared it tries to signal those processes
599 * that might be hung waiting for a response for the now non-existant port.
601 static void dgrp_chan_count(struct nd_struct *nd, int n)
603 if (n == nd->nd_chan_count)
606 if (n > nd->nd_chan_count)
607 increase_channel_count(nd, n);
609 if (n < nd->nd_chan_count)
610 decrease_channel_count(nd, n);
614 * dgrp_monitor() -- send data to the device monitor queue
615 * @nd: pointer to a node structure
616 * @buf: data to copy to the monitoring buffer
617 * @len: number of bytes to transfer to the buffer
619 * Called by the net device routines to send data to the device
620 * monitor queue. If the device monitor buffer is too full to
621 * accept the data, it waits until the buffer is ready.
623 static void dgrp_monitor(struct nd_struct *nd, u8 *buf, int len)
632 down(&nd->nd_mon_semaphore);
635 * Loop while data remains.
637 while ((len > 0) && (nd->nd_mon_buf)) {
639 * Determine the amount of available space left in the
640 * buffer. If there's none, wait until some appears.
643 n = (nd->nd_mon_out - nd->nd_mon_in - 1) & MON_MASK;
646 nd->nd_mon_flag |= MON_WAIT_SPACE;
648 up(&nd->nd_mon_semaphore);
651 * Go to sleep waiting until the condition becomes true.
653 rtn = wait_event_interruptible(nd->nd_mon_wqueue,
654 ((nd->nd_mon_flag & MON_WAIT_SPACE) == 0));
656 /* FIXME: really ignore rtn? */
659 * We can't exit here if we receive a signal, since
660 * to do so would trash the debug stream.
663 down(&nd->nd_mon_semaphore);
669 * Copy as much data as will fit.
675 r = MON_MAX - nd->nd_mon_in;
678 memcpy(nd->nd_mon_buf + nd->nd_mon_in, buf, r);
688 memcpy(nd->nd_mon_buf + nd->nd_mon_in, buf, n);
695 if (nd->nd_mon_in >= MON_MAX)
696 pr_info_ratelimited("%s - nd_mon_in (%i) >= MON_MAX\n",
697 __func__, nd->nd_mon_in);
700 * Wakeup any thread waiting for data
703 if (nd->nd_mon_flag & MON_WAIT_DATA) {
704 nd->nd_mon_flag &= ~MON_WAIT_DATA;
705 wake_up_interruptible(&nd->nd_mon_wqueue);
710 * Release the monitor lock.
712 up(&nd->nd_mon_semaphore);
716 * dgrp_encode_time() -- Encodes rpdump time into a 4-byte quantity.
717 * @nd: pointer to a node structure
718 * @buf: destination buffer
720 * Encodes "rpdump" time into a 4-byte quantity. Time is measured since
723 static void dgrp_encode_time(struct nd_struct *nd, u8 *buf)
728 * Convert time in HZ since open to time in milliseconds
731 t = jiffies - nd->nd_mon_lbolt;
732 t = 1000 * (t / HZ) + 1000 * (t % HZ) / HZ;
734 put_unaligned_be32((uint)(t & 0xffffffff), buf);
740 * dgrp_monitor_message() -- Builds a rpdump style message.
741 * @nd: pointer to a node structure
742 * @message: destination buffer
744 static void dgrp_monitor_message(struct nd_struct *nd, char *message)
749 header[0] = RPDUMP_MESSAGE;
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);
764 * dgrp_monitor_reset() -- Note a reset in the monitoring buffer.
765 * @nd: pointer to a node structure
767 static void dgrp_monitor_reset(struct nd_struct *nd)
771 header[0] = RPDUMP_RESET;
773 dgrp_encode_time(nd, header + 1);
775 dgrp_monitor(nd, header, sizeof(header));
779 * dgrp_monitor_data() -- builds a monitor data packet
780 * @nd: pointer to a node structure
781 * @type: type of message to be logged
782 * @buf: data to be logged
783 * @size: number of bytes in the buffer
785 static void dgrp_monitor_data(struct nd_struct *nd, u8 type, u8 *buf, int size)
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)
803 nd->nd_writebuf = NULL;
804 nd->nd_inputbuf = NULL;
805 nd->nd_inputflagbuf = NULL;
808 * Allocate the network read/write buffer.
810 nd->nd_iobuf = kzalloc(UIO_MAX + 10, GFP_KERNEL);
815 * Allocate a buffer for doing the copy from user space to
816 * kernel space in the write routines.
818 nd->nd_writebuf = kzalloc(WRITEBUFLEN, GFP_KERNEL);
819 if (!nd->nd_writebuf)
823 * Allocate a buffer for doing the copy from kernel space to
824 * tty buffer space in the read routines.
826 nd->nd_inputbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
827 if (!nd->nd_inputbuf)
831 * Allocate a buffer for doing the copy from kernel space to
832 * tty buffer space in the read routines.
834 nd->nd_inputflagbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
835 if (!nd->nd_inputflagbuf)
842 kfree(nd->nd_writebuf);
843 kfree(nd->nd_inputbuf);
844 kfree(nd->nd_inputflagbuf);
849 * dgrp_net_open() -- Open the NET device for a particular PortServer
851 static int dgrp_net_open(struct inode *inode, struct file *file)
853 struct nd_struct *nd;
854 struct proc_dir_entry *de;
858 rtn = try_module_get(THIS_MODULE);
862 if (!capable(CAP_SYS_ADMIN)) {
868 * Make sure that the "private_data" field hasn't already been used.
870 if (file->private_data) {
876 * Get the node pointer, and fail if it doesn't exist.
884 nd = (struct nd_struct *) de->data;
890 file->private_data = (void *) nd;
895 down(&nd->nd_net_semaphore);
897 if (nd->nd_state != NS_CLOSED) {
903 * Initialize the link speed parameters.
906 nd->nd_link.lk_fast_rate = UIO_MAX;
907 nd->nd_link.lk_slow_rate = UIO_MAX;
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);
920 * The port is now open, so move it to the IDLE state
924 nd->nd_tx_time = jiffies;
927 * If the polling routing is not running, start it running here
929 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
931 if (!dgrp_poll_data.node_active_count) {
932 dgrp_poll_data.node_active_count = 2;
933 dgrp_poll_data.timer.expires = jiffies +
934 dgrp_poll_tick * HZ / 1000;
935 add_timer(&dgrp_poll_data.timer);
938 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
940 dgrp_monitor_message(nd, "Net Open");
944 * Release the NET lock.
946 up(&nd->nd_net_semaphore);
950 module_put(THIS_MODULE);
955 /* dgrp_net_release() -- close the NET device for a particular PortServer */
956 static int dgrp_net_release(struct inode *inode, struct file *file)
958 struct nd_struct *nd;
961 nd = (struct nd_struct *)(file->private_data);
965 /* TODO : historical locking placeholder */
967 * In the HPUX version of the RealPort driver (which served as a basis
968 * for this driver) this locking code was used. Saved if ever we need
969 * to review the locking under Linux.
971 /* spinlock(&nd->nd_lock); */
977 down(&nd->nd_net_semaphore);
980 * Before "closing" the internal connection, make sure all
985 nd->nd_state = NS_CLOSED;
989 * TODO ... must the wait queue be reset on close?
990 * should any pending waiters be reset?
991 * Let's decide to assert that the waitq is empty... and see
994 if (waitqueue_active(&nd->nd_tx_waitq))
995 pr_info("%s - expected waitqueue_active to be false\n",
1000 kfree(nd->nd_iobuf);
1001 nd->nd_iobuf = NULL;
1003 /* TODO : historical locking placeholder */
1005 * In the HPUX version of the RealPort driver (which served as a basis
1006 * for this driver) this locking code was used. Saved if ever we need
1007 * to review the locking under Linux.
1009 /* spinunlock( &nd->nd_lock ); */
1012 kfree(nd->nd_writebuf);
1013 nd->nd_writebuf = NULL;
1015 kfree(nd->nd_inputbuf);
1016 nd->nd_inputbuf = NULL;
1018 kfree(nd->nd_inputflagbuf);
1019 nd->nd_inputflagbuf = NULL;
1021 /* TODO : historical locking placeholder */
1023 * In the HPUX version of the RealPort driver (which served as a basis
1024 * for this driver) this locking code was used. Saved if ever we need
1025 * to review the locking under Linux.
1027 /* spinlock(&nd->nd_lock); */
1030 * Set the active port count to zero.
1032 dgrp_chan_count(nd, 0);
1034 /* TODO : historical locking placeholder */
1036 * In the HPUX version of the RealPort driver (which served as a basis
1037 * for this driver) this locking code was used. Saved if ever we need
1038 * to review the locking under Linux.
1040 /* spinunlock(&nd->nd_lock); */
1043 * Release the NET lock.
1045 up(&nd->nd_net_semaphore);
1048 * Cause the poller to stop scheduling itself if this is
1049 * the last active node.
1051 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1053 if (dgrp_poll_data.node_active_count == 2) {
1054 del_timer(&dgrp_poll_data.timer);
1055 dgrp_poll_data.node_active_count = 0;
1058 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1061 down(&nd->nd_net_semaphore);
1063 dgrp_monitor_message(nd, "Net Close");
1065 up(&nd->nd_net_semaphore);
1067 module_put(THIS_MODULE);
1068 file->private_data = NULL;
1072 /* used in dgrp_send to setup command header */
1073 static inline u8 *set_cmd_header(u8 *b, u8 port, u8 cmd)
1075 *b++ = 0xb0 + (port & 0x0f);
1081 * dgrp_send() -- build a packet for transmission to the server
1082 * @nd: pointer to a node structure
1083 * @tmax: maximum bytes to transmit
1085 * returns number of bytes sent
1087 static int dgrp_send(struct nd_struct *nd, long tmax)
1089 struct ch_struct *ch = nd->nd_chan;
1108 long wanted_sync_port = -1;
1109 ushort tdata[CHAN_MAX];
1112 mbuf = nd->nd_iobuf + UIO_BASE;
1115 send_sync = nd->nd_link.lk_slow_rate < UIO_MAX;
1120 memset(tdata, 0, sizeof(tdata));
1124 * If there are any outstanding requests to be serviced,
1125 * service them here.
1127 if (nd->nd_send & NR_PASSWORD) {
1130 * Send Password response.
1135 put_unaligned_be16(strlen(nd->password), b + 2);
1137 b += strlen(nd->password);
1138 nd->nd_send &= ~(NR_PASSWORD);
1143 * Loop over all modules to generate commands, and determine
1144 * the amount of data queued for transmit.
1147 for (mod = 0, port = 0; port < nd->nd_chan_count; mod++) {
1149 * If this is not the current module, enter a module select
1150 * code in the buffer.
1153 if (mod != nd->nd_tx_module)
1157 * Loop to process one module.
1160 maxport = port + 16;
1162 if (maxport > nd->nd_chan_count)
1163 maxport = nd->nd_chan_count;
1165 for (; port < maxport; port++, ch++) {
1167 * Switch based on channel state.
1170 switch (ch->ch_state) {
1172 * Send requests when the port is closed, and there
1173 * are no Open, Close or Cancel requests expected.
1178 * Wait until any open error code
1179 * has been delivered to all
1183 if (ch->ch_open_error) {
1184 if (ch->ch_wait_count[ch->ch_otype]) {
1189 ch->ch_open_error = 0;
1193 * Wait until the channel HANGUP flag is reset
1194 * before sending the first open. We can only
1195 * get to this state after a server disconnect.
1198 if ((ch->ch_flag & CH_HANGUP) != 0)
1202 * If recovering from a TCP disconnect, or if
1203 * there is an immediate open pending, send an
1204 * Immediate Open request.
1206 if ((ch->ch_flag & CH_PORT_GONE) ||
1207 ch->ch_wait_count[OTYPE_IMMEDIATE] != 0) {
1208 b = set_cmd_header(b, port, 10);
1211 ch->ch_state = CS_WAIT_OPEN;
1212 ch->ch_otype = OTYPE_IMMEDIATE;
1217 * If there is no Persistent or Incoming Open on the wait
1218 * list in the server, and a thread is waiting for a
1219 * Persistent or Incoming Open, send a Persistent or Incoming
1222 if (ch->ch_otype_waiting == 0) {
1223 if (ch->ch_wait_count[OTYPE_PERSISTENT] != 0) {
1224 b = set_cmd_header(b, port, 10);
1227 ch->ch_state = CS_WAIT_OPEN;
1228 ch->ch_otype = OTYPE_PERSISTENT;
1229 } else if (ch->ch_wait_count[OTYPE_INCOMING] != 0) {
1230 b = set_cmd_header(b, port, 10);
1233 ch->ch_state = CS_WAIT_OPEN;
1234 ch->ch_otype = OTYPE_INCOMING;
1240 * If a Persistent or Incoming Open is pending in
1241 * the server, but there is no longer an open
1242 * thread waiting for it, cancel the request.
1245 if (ch->ch_wait_count[ch->ch_otype_waiting] == 0) {
1246 b = set_cmd_header(b, port, 10);
1249 ch->ch_state = CS_WAIT_CANCEL;
1250 ch->ch_otype = ch->ch_otype_waiting;
1255 * Send port parameter queries.
1259 * Clear out all FEP state that might remain
1260 * from the last connection.
1263 ch->ch_flag |= CH_PARAM;
1265 ch->ch_flag &= ~CH_RX_FLUSH;
1301 /* Send Sequence Request */
1302 b = set_cmd_header(b, port, 14);
1304 /* Configure Event Conditions Packet */
1305 b = set_cmd_header(b, port, 42);
1306 put_unaligned_be16(0x02c0, b);
1308 *b++ = (DM_DTR | DM_RTS | DM_CTS |
1309 DM_DSR | DM_RI | DM_CD);
1311 /* Send Status Request */
1312 b = set_cmd_header(b, port, 16);
1314 /* Send Buffer Request */
1315 b = set_cmd_header(b, port, 20);
1317 /* Send Port Capability Request */
1318 b = set_cmd_header(b, port, 22);
1320 ch->ch_expect = (RR_SEQUENCE |
1325 ch->ch_state = CS_WAIT_QUERY;
1327 /* Raise modem signals */
1328 b = set_cmd_header(b, port, 44);
1330 if (ch->ch_flag & CH_PORT_GONE)
1331 ch->ch_s_mout = ch->ch_mout;
1333 ch->ch_s_mout = ch->ch_mout = DM_DTR | DM_RTS;
1336 *b++ = ch->ch_s_mflow = 0;
1337 *b++ = ch->ch_s_mctrl = ch->ch_mctrl = 0;
1339 if (ch->ch_flag & CH_PORT_GONE)
1340 ch->ch_flag &= ~CH_PORT_GONE;
1345 * Handle normal open and ready mode.
1351 * If the port is not open, and there are no
1352 * no longer any ports requesting an open,
1353 * then close the port.
1356 if (ch->ch_open_count == 0 &&
1357 ch->ch_wait_count[ch->ch_otype] == 0) {
1362 * Process waiting input.
1364 * If there is no one to read it, discard the data.
1366 * Otherwise if we are not in fastcook mode, or if there is a
1367 * fastcook thread waiting for data, send the data to the
1370 if (ch->ch_rin != ch->ch_rout) {
1371 if (ch->ch_tun.un_open_count == 0 ||
1372 (ch->ch_tun.un_flag & UN_CLOSING) ||
1373 (ch->ch_cflag & CF_CREAD) == 0) {
1374 ch->ch_rout = ch->ch_rin;
1375 } else if ((ch->ch_flag & CH_FAST_READ) == 0 ||
1376 ch->ch_inwait != 0) {
1379 if (ch->ch_rin != ch->ch_rout)
1385 * Handle receive flush, and changes to
1386 * server port parameters.
1389 if (ch->ch_flag & (CH_RX_FLUSH | CH_PARAM)) {
1391 * If we are in receive flush mode,
1392 * and enough data has gone by, reset
1393 * receive flush mode.
1395 if (ch->ch_flag & CH_RX_FLUSH) {
1396 if (((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >
1397 ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK))
1398 ch->ch_flag &= ~CH_RX_FLUSH;
1407 if (ch->ch_s_tmax != ch->ch_tmax ||
1408 ch->ch_s_ttime != ch->ch_ttime) {
1409 b = set_cmd_header(b, port, 48);
1411 ch->ch_s_tmax = ch->ch_tmax;
1412 ch->ch_s_ttime = ch->ch_ttime;
1414 put_unaligned_be16(ch->ch_s_tmax,
1418 put_unaligned_be16(ch->ch_s_ttime,
1427 if (ch->ch_s_rlow != ch->ch_rlow ||
1428 ch->ch_s_rhigh != ch->ch_rhigh) {
1429 b = set_cmd_header(b, port, 45);
1431 ch->ch_s_rlow = ch->ch_rlow;
1432 ch->ch_s_rhigh = ch->ch_rhigh;
1434 put_unaligned_be16(ch->ch_s_rlow,
1438 put_unaligned_be16(ch->ch_s_rhigh,
1444 * Send BRATE, CFLAG, IFLAG,
1448 if (ch->ch_s_brate != ch->ch_brate ||
1449 ch->ch_s_cflag != ch->ch_cflag ||
1450 ch->ch_s_iflag != ch->ch_iflag ||
1451 ch->ch_s_oflag != ch->ch_oflag ||
1452 ch->ch_s_xflag != ch->ch_xflag) {
1453 b = set_cmd_header(b, port, 40);
1455 ch->ch_s_brate = ch->ch_brate;
1456 ch->ch_s_cflag = ch->ch_cflag;
1457 ch->ch_s_iflag = ch->ch_iflag;
1458 ch->ch_s_oflag = ch->ch_oflag;
1459 ch->ch_s_xflag = ch->ch_xflag;
1461 put_unaligned_be16(ch->ch_s_brate,
1465 put_unaligned_be16(ch->ch_s_cflag,
1469 put_unaligned_be16(ch->ch_s_iflag,
1473 put_unaligned_be16(ch->ch_s_oflag,
1477 put_unaligned_be16(ch->ch_s_xflag,
1483 * Send MOUT, MFLOW, MCTRL.
1486 if (ch->ch_s_mout != ch->ch_mout ||
1487 ch->ch_s_mflow != ch->ch_mflow ||
1488 ch->ch_s_mctrl != ch->ch_mctrl) {
1489 b = set_cmd_header(b, port, 44);
1491 *b++ = ch->ch_s_mout = ch->ch_mout;
1492 *b++ = ch->ch_s_mflow = ch->ch_mflow;
1493 *b++ = ch->ch_s_mctrl = ch->ch_mctrl;
1497 * Send Flow control characters.
1500 if (ch->ch_s_xon != ch->ch_xon ||
1501 ch->ch_s_xoff != ch->ch_xoff ||
1502 ch->ch_s_lnext != ch->ch_lnext ||
1503 ch->ch_s_xxon != ch->ch_xxon ||
1504 ch->ch_s_xxoff != ch->ch_xxoff) {
1505 b = set_cmd_header(b, port, 46);
1507 *b++ = ch->ch_s_xon = ch->ch_xon;
1508 *b++ = ch->ch_s_xoff = ch->ch_xoff;
1509 *b++ = ch->ch_s_lnext = ch->ch_lnext;
1510 *b++ = ch->ch_s_xxon = ch->ch_xxon;
1511 *b++ = ch->ch_s_xxoff = ch->ch_xxoff;
1518 if (ch->ch_s_rmax != ch->ch_rmax ||
1519 ch->ch_s_rtime != ch->ch_rtime) {
1520 b = set_cmd_header(b, port, 47);
1522 ch->ch_s_rmax = ch->ch_rmax;
1523 ch->ch_s_rtime = ch->ch_rtime;
1525 put_unaligned_be16(ch->ch_s_rmax,
1529 put_unaligned_be16(ch->ch_s_rtime,
1534 ch->ch_flag &= ~CH_PARAM;
1535 wake_up_interruptible(&ch->ch_flag_wait);
1540 * Handle action commands.
1543 if (ch->ch_send != 0) {
1544 /* int send = ch->ch_send & ~ch->ch_expect; */
1545 send = ch->ch_send & ~ch->ch_expect;
1547 /* Send character immediate */
1548 if ((send & RR_TX_ICHAR) != 0) {
1549 b = set_cmd_header(b, port, 60);
1552 ch->ch_expect |= RR_TX_ICHAR;
1556 if ((send & RR_TX_BREAK) != 0) {
1557 if (ch->ch_break_time != 0) {
1558 b = set_cmd_header(b, port, 61);
1559 put_unaligned_be16(ch->ch_break_time,
1563 ch->ch_expect |= RR_TX_BREAK;
1564 ch->ch_break_time = 0;
1566 ch->ch_send &= ~RR_TX_BREAK;
1567 ch->ch_flag &= ~CH_TX_BREAK;
1568 wake_up_interruptible(&ch->ch_flag_wait);
1573 * Flush input/output buffers.
1576 if ((send & (RR_RX_FLUSH | RR_TX_FLUSH)) != 0) {
1577 b = set_cmd_header(b, port, 62);
1579 *b++ = ((send & RR_TX_FLUSH) == 0 ? 1 :
1580 (send & RR_RX_FLUSH) == 0 ? 2 : 3);
1582 if (send & RR_RX_FLUSH) {
1583 ch->ch_flush_seq = nd->nd_seq_in;
1584 ch->ch_flag |= CH_RX_FLUSH;
1587 wanted_sync_port = port;
1590 ch->ch_send &= ~(RR_RX_FLUSH | RR_TX_FLUSH);
1593 /* Pause input/output */
1594 if ((send & (RR_RX_STOP | RR_TX_STOP)) != 0) {
1595 b = set_cmd_header(b, port, 63);
1598 if ((send & RR_TX_STOP) != 0)
1601 if ((send & RR_RX_STOP) != 0)
1606 ch->ch_send &= ~(RR_RX_STOP | RR_TX_STOP);
1609 /* Start input/output */
1610 if ((send & (RR_RX_START | RR_TX_START)) != 0) {
1611 b = set_cmd_header(b, port, 64);
1614 if ((send & RR_TX_START) != 0)
1615 *b |= EV_OPU | EV_OPS | EV_OPX;
1617 if ((send & RR_RX_START) != 0)
1618 *b |= EV_IPU | EV_IPS;
1622 ch->ch_send &= ~(RR_RX_START | RR_TX_START);
1628 * Send a window sequence to acknowledge received data.
1631 rwin = (ch->ch_s_rin +
1632 ((ch->ch_rout - ch->ch_rin - 1) & RBUF_MASK));
1634 n = (rwin - ch->ch_s_rwin) & 0xffff;
1636 if (n >= RBUF_MAX / 4) {
1637 b[0] = 0xa0 + (port & 0xf);
1638 ch->ch_s_rwin = rwin;
1639 put_unaligned_be16(rwin, b + 1);
1644 * If the terminal is waiting on LOW
1645 * water or EMPTY, and the condition
1646 * is now satisfied, call the line
1647 * discipline to put more data in the
1651 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1653 if ((ch->ch_tun.un_flag & (UN_EMPTY|UN_LOW)) != 0) {
1654 if ((ch->ch_tun.un_flag & UN_LOW) != 0 ?
1656 (n == 0 && ch->ch_s_tpos == ch->ch_s_tin)) {
1657 ch->ch_tun.un_flag &= ~(UN_EMPTY|UN_LOW);
1659 if (waitqueue_active(&((ch->ch_tun.un_tty)->write_wait)))
1660 wake_up_interruptible(&((ch->ch_tun.un_tty)->write_wait));
1661 tty_wakeup(ch->ch_tun.un_tty);
1662 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1667 * If the printer is waiting on LOW
1668 * water, TIME, EMPTY or PWAIT, and is
1669 * now ready to put more data in the
1670 * buffer, call the line discipline to
1674 if (ch->ch_pun.un_open_count &&
1675 (ch->ch_pun.un_flag &
1676 (UN_EMPTY|UN_TIME|UN_LOW|UN_PWAIT)) != 0) {
1678 if ((ch->ch_pun.un_flag & UN_LOW) != 0 ?
1680 (ch->ch_pun.un_flag & UN_TIME) != 0 ?
1681 ((jiffies - ch->ch_waketime) >= 0) :
1682 (n == 0 && ch->ch_s_tpos == ch->ch_s_tin) &&
1683 ((ch->ch_pun.un_flag & UN_EMPTY) != 0 ||
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
1690 ch->ch_pun.un_flag &= ~(UN_EMPTY | UN_TIME | UN_LOW | UN_PWAIT);
1692 if (waitqueue_active(&((ch->ch_pun.un_tty)->write_wait)))
1693 wake_up_interruptible(&((ch->ch_pun.un_tty)->write_wait));
1694 tty_wakeup(ch->ch_pun.un_tty);
1695 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1697 } else if ((ch->ch_pun.un_flag & UN_TIME) != 0) {
1704 * Determine the max number of bytes
1705 * this port can send, including
1706 * packet header overhead.
1709 t = ((ch->ch_s_tsize + ch->ch_s_tpos - ch->ch_s_tin) & 0xffff);
1715 n += (n <= 8 ? 1 : n <= 255 ? 2 : 3);
1728 b = set_cmd_header(b, port, 10);
1729 if (ch->ch_otype == OTYPE_IMMEDIATE)
1734 ch->ch_state = CS_WAIT_CLOSE;
1738 * Wait for a previous server request.
1742 case CS_WAIT_CANCEL:
1749 pr_info("%s - unexpected channel state (%i)\n",
1750 __func__, ch->ch_state);
1755 * If a module select code is needed, drop one in. If space
1756 * was reserved for one, but none is needed, recover the space.
1759 if (mod != nd->nd_tx_module) {
1761 mbuf[-1] = 0xf0 | mod;
1762 nd->nd_tx_module = mod;
1770 * Adjust "tmax" so that under worst case conditions we do
1771 * not overflow either the daemon buffer or the internal
1772 * buffer in the loop that follows. Leave a safe area
1773 * of 64 bytes so we start getting asserts before we start
1774 * losing data or clobbering memory.
1777 n = UIO_MAX - UIO_BASE;
1787 * Allocate space for 5 Module Selects, 1 Sequence Request,
1788 * and 1 Set TREQ for each active channel.
1791 tmax -= 5 + 3 + 4 * nd->nd_chan_count;
1794 * Further reduce "tmax" to the available transmit credit.
1795 * Note that this is a soft constraint; The transmit credit
1796 * can go negative for a time and then recover.
1799 n = nd->nd_tx_deposit - nd->nd_tx_charge - nd->nd_link.lk_header_size;
1805 * Finally reduce tmax by the number of bytes already in
1812 * Suspend data transmit unless every ready channel can send
1813 * at least 1 character.
1815 if (tmax < 2 * nd->nd_chan_count) {
1818 } else if (tchan > 1 && ttotal > tmax) {
1821 * If transmit is limited by the credit budget, find the
1822 * largest number of characters we can send without driving
1823 * the credit negative.
1832 for (try = 0; try < 3; try++) {
1836 for (i = 0; i < tc; i++) {
1837 if (tsend < tdata[i])
1838 tdata[c++] = tdata[i];
1854 tsend = tm / nd->nd_chan_count;
1861 * If no budgetary constraints, or only one channel ready
1862 * to send, set the character limit to the remaining
1869 tsend -= (tsend <= 9) ? 1 : (tsend <= 257) ? 2 : 3;
1872 * Loop over all channels, sending queued data.
1879 for (mod = 0; port < nd->nd_chan_count; mod++) {
1881 * If this is not the current module, enter a module select
1882 * code in the buffer.
1885 if (mod != nd->nd_tx_module)
1889 * Loop to process one module.
1892 maxport = port + 16;
1894 if (maxport > nd->nd_chan_count)
1895 maxport = nd->nd_chan_count;
1897 for (; port < maxport; port++, ch++) {
1898 if (ch->ch_state != CS_READY)
1903 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1906 * If there is data that can be sent, send it.
1909 if (n != 0 && used_buffer > 0) {
1910 t = (ch->ch_s_tsize + ch->ch_s_tpos - ch->ch_s_tin) & 0xffff;
1920 if (n > used_buffer) {
1929 * Create the correct size transmit header,
1930 * depending on the amount of data to transmit.
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);
1951 ch->ch_s_tin = (ch->ch_s_tin + n) & 0xffff;
1954 * Copy transmit data to the packet.
1957 t = TBUF_MAX - ch->ch_tout;
1960 memcpy(b, ch->ch_tbuf + ch->ch_tout, t);
1967 memcpy(b, ch->ch_tbuf + ch->ch_tout, n);
1971 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1975 * Wake any terminal unit process waiting in the
1976 * dgrp_write routine for low water.
1982 if ((ch->ch_flag & CH_LOW) != 0) {
1983 ch->ch_flag &= ~CH_LOW;
1984 wake_up_interruptible(&ch->ch_flag_wait);
1987 /* selwakeup tty_sel */
1988 if (ch->ch_tun.un_open_count) {
1989 struct tty_struct *tty = (ch->ch_tun.un_tty);
1991 if (waitqueue_active(&tty->write_wait))
1992 wake_up_interruptible(&tty->write_wait);
1997 if (ch->ch_pun.un_open_count) {
1998 struct tty_struct *tty = (ch->ch_pun.un_tty);
2000 if (waitqueue_active(&tty->write_wait))
2001 wake_up_interruptible(&tty->write_wait);
2007 * Do EMPTY processing.
2013 if ((ch->ch_flag & (CH_EMPTY | CH_DRAIN)) != 0 ||
2014 (ch->ch_pun.un_flag & UN_EMPTY) != 0) {
2016 * If there is still data in the server, ask the server
2017 * to notify us when its all gone.
2020 if (ch->ch_s_treq != ch->ch_s_tin) {
2021 b = set_cmd_header(b, port, 43);
2023 ch->ch_s_treq = ch->ch_s_tin;
2024 put_unaligned_be16(ch->ch_s_treq,
2030 * If there is a thread waiting for buffer empty,
2031 * and we are truly empty, wake the thread.
2034 else if ((ch->ch_flag & CH_EMPTY) != 0 &&
2035 (ch->ch_send & RR_TX_BREAK) == 0) {
2036 ch->ch_flag &= ~CH_EMPTY;
2038 wake_up_interruptible(&ch->ch_flag_wait);
2044 * If a module select code is needed, drop one in. If space
2045 * was reserved for one, but none is needed, recover the space.
2048 if (mod != nd->nd_tx_module) {
2050 mbuf[-1] = 0xf0 | mod;
2051 nd->nd_tx_module = mod;
2059 * Send a synchronization sequence associated with the last open
2060 * channel that sent data, and remember the time when the data was
2066 if ((send_sync || nd->nd_seq_wait[in] != 0) && lastport >= 0) {
2070 * Attempt the use the port that really wanted the sync.
2071 * This gets around a race condition where the "lastport" is in
2072 * the middle of the close() routine, and by the time we
2073 * send this command, it will have already acked the close, and
2074 * thus not send the sync response.
2076 if (wanted_sync_port >= 0)
2077 lastport = wanted_sync_port;
2079 * Set a flag just in case the port is in the middle of a close,
2080 * it will not be permitted to actually close until we get an
2081 * sync response, and clear the flag there.
2083 ch = nd->nd_chan + lastport;
2084 ch->ch_flag |= CH_WAITING_SYNC;
2086 mod = lastport >> 4;
2088 if (mod != nd->nd_tx_module) {
2092 nd->nd_tx_module = mod;
2095 bb = set_cmd_header(bb, lastport, 12);
2098 nd->nd_seq_size[in] = bb - buf;
2099 nd->nd_seq_time[in] = jiffies;
2101 if (++in >= SEQ_MAX)
2104 if (in != nd->nd_seq_out) {
2107 nd->nd_unack += b - buf;
2112 * If there are no open ports, a sync cannot be sent.
2113 * There is nothing left to wait for anyway, so wake any
2114 * thread waiting for an acknowledgement.
2117 else if (nd->nd_seq_wait[in] != 0) {
2118 nd->nd_seq_wait[in] = 0;
2120 wake_up_interruptible(&nd->nd_seq_wque[in]);
2124 * If there is no traffic for an interval of IDLE_MAX, then
2125 * send a single byte packet.
2129 nd->nd_tx_time = jiffies;
2130 } else if ((ulong)(jiffies - nd->nd_tx_time) >= IDLE_MAX) {
2131 *b++ = 0xf0 | nd->nd_tx_module;
2132 nd->nd_tx_time = jiffies;
2138 pr_info("%s - n(%i) >= tsafe(%i)\n",
2139 __func__, n, tsafe);
2144 nd->nd_tx_work = work;
2151 * Data to be sent TO the PortServer from the "async." half of the driver.
2153 static ssize_t dgrp_net_read(struct file *file, char __user *buf, size_t count,
2156 struct nd_struct *nd;
2163 * Get the node pointer, and quit if it doesn't exist.
2165 nd = (struct nd_struct *)(file->private_data);
2169 if (count < UIO_MIN)
2173 * Only one read/write operation may be in progress at
2178 * Grab the NET lock.
2180 down(&nd->nd_net_semaphore);
2182 nd->nd_read_count++;
2184 nd->nd_tx_ready = 0;
2187 * Determine the effective size of the buffer.
2190 if (nd->nd_remain > UIO_BASE)
2191 pr_info_ratelimited("%s - nd_remain(%i) > UIO_BASE\n",
2192 __func__, nd->nd_remain);
2194 b = local_buf = nd->nd_iobuf + UIO_BASE;
2197 * Generate data according to the node state.
2200 switch (nd->nd_state) {
2202 * Initialize the connection.
2207 dgrp_monitor_reset(nd);
2210 * Request a Product ID Packet.
2217 nd->nd_expect |= NR_IDENT;
2220 * Request a Server Capability ID Response.
2227 nd->nd_expect |= NR_CAPABILITY;
2230 * Request a Server VPD Response.
2237 nd->nd_expect |= NR_VPD;
2239 nd->nd_state = NS_WAIT_QUERY;
2243 * We do serious communication with the server only in
2248 b = dgrp_send(nd, count) + local_buf;
2252 * Send off an error after receiving a bogus message
2257 n = strlen(nd->nd_error);
2261 memcpy(b + 2, nd->nd_error, n);
2266 * Set the active port count to zero.
2268 dgrp_chan_count(nd, 0);
2278 nd->nd_send_count++;
2280 nd->nd_tx_byte += n + nd->nd_link.lk_header_size;
2281 nd->nd_tx_charge += n + nd->nd_link.lk_header_size;
2284 rtn = copy_to_user((void __user *)buf, local_buf, n);
2295 dgrp_monitor_data(nd, RPDUMP_CLIENT, local_buf, n);
2298 * Release the NET lock.
2301 up(&nd->nd_net_semaphore);
2307 * dgrp_receive() -- decode data packets received from the remote PortServer.
2308 * @nd: pointer to a node structure
2310 static void dgrp_receive(struct nd_struct *nd)
2312 struct ch_struct *ch;
2329 nd->nd_tx_time = jiffies;
2331 ID_TO_CHAR(nd->nd_ID, ID);
2333 b = buf = nd->nd_iobuf;
2334 remain = nd->nd_remain;
2337 * Loop to process Realport protocol packets.
2340 while (remain > 0) {
2342 int n1 = b[0] & 0x0f;
2345 port = (nd->nd_rx_module << 4) + n1;
2347 if (port >= nd->nd_chan_count) {
2348 error = "Improper Port Number";
2352 ch = nd->nd_chan + port;
2359 * Process by major packet type.
2365 * Process 1-byte header data packet.
2383 * Process 2-byte header data packet.
2397 * Process 3-byte header data packet.
2404 dlen = get_unaligned_be16(b + 1);
2410 * Common packet handling code.
2417 * Otherwise data should appear only when we are
2418 * in the CS_READY state.
2421 if (ch->ch_state < CS_READY) {
2422 error = "Data received before RWIN established";
2427 * Assure that the data received is within the
2431 n = (ch->ch_s_rwin - ch->ch_s_rin) & 0xffff;
2434 error = "Receive data overrun";
2439 * If we received 3 or less characters,
2440 * assume it is a human typing, and set RTIME
2441 * to 10 milliseconds.
2443 * If we receive 10 or more characters,
2444 * assume its not a human typing, and set RTIME
2445 * to 100 milliseconds.
2448 if (ch->ch_edelay != DGRP_RTIME) {
2449 if (ch->ch_rtime != ch->ch_edelay) {
2450 ch->ch_rtime = ch->ch_edelay;
2451 ch->ch_flag |= CH_PARAM;
2453 } else if (dlen <= 3) {
2454 if (ch->ch_rtime != 10) {
2456 ch->ch_flag |= CH_PARAM;
2459 if (ch->ch_rtime != DGRP_RTIME) {
2460 ch->ch_rtime = DGRP_RTIME;
2461 ch->ch_flag |= CH_PARAM;
2466 * If a portion of the packet is outside the
2467 * buffer, shorten the effective length of the
2468 * data packet to be the amount of data received.
2472 dlen -= plen - remain;
2475 * Detect if receive flush is now complete.
2478 if ((ch->ch_flag & CH_RX_FLUSH) != 0 &&
2479 ((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >=
2480 ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK)) {
2481 ch->ch_flag &= ~CH_RX_FLUSH;
2485 * If we are ready to receive, move the data into
2486 * the receive buffer.
2489 ch->ch_s_rin = (ch->ch_s_rin + dlen) & 0xffff;
2491 if (ch->ch_state == CS_READY &&
2492 (ch->ch_tun.un_open_count != 0) &&
2493 (ch->ch_tun.un_flag & UN_CLOSING) == 0 &&
2494 (ch->ch_cflag & CF_CREAD) != 0 &&
2495 (ch->ch_flag & (CH_BAUD0 | CH_RX_FLUSH)) == 0 &&
2496 (ch->ch_send & RR_RX_FLUSH) == 0) {
2498 if (ch->ch_rin + dlen >= RBUF_MAX) {
2499 n = RBUF_MAX - ch->ch_rin;
2501 memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, n);
2508 memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, dlen);
2514 * If we are not in fastcook mode, or
2515 * if there is a fastcook thread
2516 * waiting for data, send the data to
2517 * the line discipline.
2520 if ((ch->ch_flag & CH_FAST_READ) == 0 ||
2521 ch->ch_inwait != 0) {
2526 * If there is a read thread waiting
2527 * in select, and we are in fastcook
2528 * mode, wake him up.
2531 if (waitqueue_active(&ch->ch_tun.un_tty->read_wait) &&
2532 (ch->ch_flag & CH_FAST_READ) != 0)
2533 wake_up_interruptible(&ch->ch_tun.un_tty->read_wait);
2536 * Wake any thread waiting in the
2540 if ((ch->ch_flag & CH_INPUT) != 0) {
2541 ch->ch_flag &= ~CH_INPUT;
2543 wake_up_interruptible(&ch->ch_flag_wait);
2548 * Fabricate and insert a data packet header to
2549 * preceed the remaining data when it comes in.
2552 if (remain < plen) {
2553 dlen = plen - remain;
2557 put_unaligned_be16(dlen, b + 1);
2565 * Handle Window Sequence packets.
2576 ushort tpos = get_unaligned_be16(b + 1);
2578 ushort ack = (tpos - ch->ch_s_tpos) & 0xffff;
2579 ushort unack = (ch->ch_s_tin - ch->ch_s_tpos) & 0xffff;
2580 ushort notify = (ch->ch_s_treq - ch->ch_s_tpos) & 0xffff;
2582 if (ch->ch_state < CS_READY || ack > unack) {
2583 error = "Improper Window Sequence";
2587 ch->ch_s_tpos = tpos;
2590 ch->ch_s_treq = tpos;
2595 * Handle Command response packets.
2601 * RealPort engine fix - 03/11/2004
2603 * This check did not used to be here.
2605 * We were using b[1] without verifying that the data
2606 * is actually there and valid. On a split packet, it
2609 * NOTE: I have never actually seen the failure happen
2610 * under Linux, but since I have seen it occur
2611 * under both Solaris and HP-UX, the assumption
2612 * is that it *could* happen here as well...
2621 * Handle Open Response.
2634 port = get_unaligned_be16(b + 4);
2636 if (port >= nd->nd_chan_count) {
2637 error = "Open channel number out of range";
2641 ch = nd->nd_chan + port;
2644 * How we handle an open response depends primarily
2645 * on our current channel state.
2648 switch (ch->ch_state) {
2652 * Handle a delayed open.
2655 if (ch->ch_otype_waiting != 0 &&
2656 req == ch->ch_otype_waiting &&
2659 ch->ch_otype_waiting = 0;
2660 ch->ch_state = CS_SEND_QUERY;
2668 * Handle the open response.
2671 if (req == ch->ch_otype) {
2675 * On successful response, open the
2676 * port and proceed normally.
2680 ch->ch_state = CS_SEND_QUERY;
2684 * On a busy response to a persistent open,
2685 * remember that the open is pending.
2690 if (req != OTYPE_IMMEDIATE) {
2691 ch->ch_otype_waiting = req;
2692 ch->ch_state = CS_IDLE;
2697 * Otherwise the server open failed. If
2698 * the Unix port is open, hang it up.
2702 if (ch->ch_open_count != 0) {
2703 ch->ch_flag |= CH_HANGUP;
2705 ch->ch_state = CS_IDLE;
2709 ch->ch_open_error = resp;
2710 ch->ch_state = CS_IDLE;
2712 wake_up_interruptible(&ch->ch_flag_wait);
2718 * Handle delayed response arrival preceeding
2719 * the open response we are waiting for.
2722 if (ch->ch_otype_waiting != 0 &&
2723 req == ch->ch_otype_waiting &&
2725 ch->ch_otype = ch->ch_otype_waiting;
2726 ch->ch_otype_waiting = 0;
2727 ch->ch_state = CS_WAIT_FAIL;
2736 * Handle response to immediate open arriving
2737 * after a delayed open success.
2740 if (req == OTYPE_IMMEDIATE) {
2741 ch->ch_state = CS_SEND_QUERY;
2747 case CS_WAIT_CANCEL:
2749 * Handle delayed open response arriving before
2750 * the cancel response.
2753 if (req == ch->ch_otype_waiting &&
2755 ch->ch_otype_waiting = 0;
2760 * Handle cancel response.
2763 if (req == 4 && resp == 0) {
2764 ch->ch_otype_waiting = 0;
2765 ch->ch_state = CS_IDLE;
2773 * Handle a successful response to a port
2778 ch->ch_state = CS_IDLE;
2786 error = "Improper Open Response";
2794 * Handle Synchronize Response.
2806 * If channel was waiting for this sync response,
2807 * unset the flag, and wake up anyone waiting
2810 if (ch->ch_flag & CH_WAITING_SYNC) {
2811 ch->ch_flag &= ~(CH_WAITING_SYNC);
2812 wake_up_interruptible(&ch->ch_flag_wait);
2815 if (((seq - nd->nd_seq_out) & SEQ_MASK) >=
2816 ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK)) {
2820 for (s = nd->nd_seq_out;; s = (s + 1) & SEQ_MASK) {
2821 if (nd->nd_seq_wait[s] != 0) {
2822 nd->nd_seq_wait[s] = 0;
2824 wake_up_interruptible(&nd->nd_seq_wque[s]);
2827 nd->nd_unack -= nd->nd_seq_size[s];
2833 nd->nd_seq_out = (seq + 1) & SEQ_MASK;
2838 * Handle Sequence Response.
2847 /* Record that we have received the Sequence
2848 * Response, but we aren't interested in the
2849 * sequence numbers. We were using RIN like it
2850 * was ROUT and that was causing problems,
2851 * fixed 7-13-2001 David Fries. See comment in
2852 * drp.h for ch_s_rin variable.
2853 int rin = get_unaligned_be16(b + 2);
2854 int tpos = get_unaligned_be16(b + 4);
2857 ch->ch_send &= ~RR_SEQUENCE;
2858 ch->ch_expect &= ~RR_SEQUENCE;
2863 * Handle Status Response.
2872 ch->ch_s_elast = get_unaligned_be16(b + 2);
2873 ch->ch_s_mlast = b[4];
2875 ch->ch_expect &= ~RR_STATUS;
2876 ch->ch_send &= ~RR_STATUS;
2879 * CH_PHYS_CD is cleared because something _could_ be
2880 * waiting for the initial sense of carrier... and if
2881 * carrier is high immediately, we want to be sure to
2882 * wake them as soon as possible.
2884 ch->ch_flag &= ~CH_PHYS_CD;
2891 * Handle Line Error Response.
2902 * Handle Buffer Response.
2911 ch->ch_s_rsize = get_unaligned_be16(b + 2);
2912 ch->ch_s_tsize = get_unaligned_be16(b + 4);
2914 ch->ch_send &= ~RR_BUFFER;
2915 ch->ch_expect &= ~RR_BUFFER;
2920 * Handle Port Capability Response.
2929 ch->ch_send &= ~RR_CAPABILITY;
2930 ch->ch_expect &= ~RR_CAPABILITY;
2934 * When all queries are complete, set those parameters
2935 * derived from the query results, then transition
2936 * to the READY state.
2940 if (ch->ch_state == CS_WAIT_QUERY &&
2941 (ch->ch_expect & (RR_SEQUENCE |
2944 RR_CAPABILITY)) == 0) {
2945 ch->ch_tmax = ch->ch_s_tsize / 4;
2947 if (ch->ch_edelay == DGRP_TTIME)
2948 ch->ch_ttime = DGRP_TTIME;
2950 ch->ch_ttime = ch->ch_edelay;
2952 ch->ch_rmax = ch->ch_s_rsize / 4;
2954 if (ch->ch_edelay == DGRP_RTIME)
2955 ch->ch_rtime = DGRP_RTIME;
2957 ch->ch_rtime = ch->ch_edelay;
2959 ch->ch_rlow = 2 * ch->ch_s_rsize / 8;
2960 ch->ch_rhigh = 6 * ch->ch_s_rsize / 8;
2962 ch->ch_state = CS_READY;
2965 wake_up_interruptible(&ch->ch_flag_wait);
2984 mlast = ch->ch_s_mlast;
2985 elast = ch->ch_s_elast;
2987 mstat = ch->ch_s_mlast = b[1];
2988 estat = ch->ch_s_elast = get_unaligned_be16(b + 2);
2991 * Handle modem changes.
2994 if (((mstat ^ mlast) & DM_CD) != 0)
2999 * Handle received break.
3002 if ((estat & ~elast & EV_RXB) != 0 &&
3003 (ch->ch_tun.un_open_count != 0) &&
3004 I_BRKINT(ch->ch_tun.un_tty) &&
3005 !(I_IGNBRK(ch->ch_tun.un_tty))) {
3007 tty_buffer_request_room(ch->ch_tun.un_tty, 1);
3008 tty_insert_flip_char(ch->ch_tun.un_tty, 0, TTY_BREAK);
3009 tty_flip_buffer_push(ch->ch_tun.un_tty);
3014 * On transmit break complete, if more break traffic
3015 * is waiting then send it. Otherwise wake any threads
3016 * waiting for transmitter empty.
3019 if ((~estat & elast & EV_TXB) != 0 &&
3020 (ch->ch_expect & RR_TX_BREAK) != 0) {
3024 ch->ch_expect &= ~RR_TX_BREAK;
3026 if (ch->ch_break_time != 0) {
3027 ch->ch_send |= RR_TX_BREAK;
3029 ch->ch_send &= ~RR_TX_BREAK;
3030 ch->ch_flag &= ~CH_TX_BREAK;
3031 wake_up_interruptible(&ch->ch_flag_wait);
3038 error = "Unrecognized command";
3042 * Decode Special Codes.
3048 * One byte module select.
3060 nd->nd_rx_module = n1;
3064 * Two byte module select.
3072 nd->nd_rx_module = b[1];
3076 * ID Request packet.
3083 plen = get_unaligned_be16(b + 2);
3085 if (plen < 12 || plen > 1000) {
3086 error = "Response Packet length error";
3098 nd->nd_send |= NR_ECHO;
3102 * ID Response packet.
3106 nd->nd_send |= NR_IDENT;
3110 * ID Response packet.
3114 nd->nd_send |= NR_PASSWORD;
3121 * Various node-level response packets.
3128 plen = get_unaligned_be16(b + 2);
3130 if (plen < 4 || plen > 1000) {
3131 error = "Response Packet length error";
3143 nd->nd_expect &= ~NR_ECHO;
3147 * Product Response Packet.
3154 nd->nd_hw_ver = (b[8] << 8) | b[9];
3155 nd->nd_sw_ver = (b[10] << 8) | b[11];
3156 nd->nd_hw_id = b[6];
3157 desclen = ((plen - 12) > MAX_DESC_LEN) ? MAX_DESC_LEN :
3161 error = "Response Packet desclen error";
3165 strncpy(nd->nd_ps_desc, b + 12, desclen);
3166 nd->nd_ps_desc[desclen] = 0;
3169 nd->nd_expect &= ~NR_IDENT;
3173 * Capability Response Packet.
3178 int nn = get_unaligned_be16(b + 4);
3183 dgrp_chan_count(nd, nn);
3186 nd->nd_expect &= ~NR_CAPABILITY;
3190 * VPD Response Packet.
3195 * NOTE: case 15 is here ONLY because the EtherLite
3196 * is broken, and sends a response to 24 back as 15.
3197 * To resolve this, the EtherLite firmware is now
3198 * fixed to send back 24 correctly, but, for backwards
3199 * compatibility, we now have reserved 15 for the
3200 * bad EtherLite response to 24 as well.
3208 * If the product doesn't support VPD,
3209 * it will send back a null IDRESP,
3210 * which is a length of 4 bytes.
3213 memcpy(nd->nd_vpd, b + 4, min(plen - 4, (long) VPDSIZE));
3214 nd->nd_vpd_len = min(plen - 4, (long) VPDSIZE);
3217 nd->nd_expect &= ~NR_VPD;
3224 if (nd->nd_expect == 0 &&
3225 nd->nd_state == NS_WAIT_QUERY) {
3226 nd->nd_state = NS_READY;
3238 plen = get_unaligned_be16(b + 2) + 4;
3241 error = "Debug Packet too large";
3250 * Handle reset packet.
3269 error = "Client Reset Acknowledge";
3286 * When the buffer is exhausted, copy any data left at the
3287 * top of the buffer back down to the bottom for the next
3292 if (remain > 0 && b != buf)
3293 memcpy(buf, b, remain);
3295 nd->nd_remain = remain;
3299 * Handle a decode error.
3303 error = "Protocol decode error";
3306 * Handle a general protocol error.
3311 nd->nd_state = NS_SEND_ERROR;
3312 nd->nd_error = error;
3316 * dgrp_net_write() -- write data to the network device.
3318 * A zero byte write indicates that the connection to the RealPort
3319 * device has been broken.
3321 * A non-zero write indicates data from the RealPort device.
3323 static ssize_t dgrp_net_write(struct file *file, const char __user *buf,
3324 size_t count, loff_t *ppos)
3326 struct nd_struct *nd;
3332 * Get the node pointer, and quit if it doesn't exist.
3334 nd = (struct nd_struct *)(file->private_data);
3339 * Grab the NET lock.
3341 down(&nd->nd_net_semaphore);
3343 nd->nd_write_count++;
3346 * Handle disconnect.
3352 * Set the active port count to zero.
3354 dgrp_chan_count(nd, 0);
3359 * Loop to process entire receive packet.
3363 n = UIO_MAX - nd->nd_remain;
3368 nd->nd_rx_byte += n + nd->nd_link.lk_header_size;
3370 rtn = copy_from_user(nd->nd_iobuf + nd->nd_remain,
3371 (void __user *) buf + total, n);
3384 dgrp_monitor_data(nd, RPDUMP_SERVER,
3385 nd->nd_iobuf + nd->nd_remain, n);
3396 * Release the NET lock.
3398 up(&nd->nd_net_semaphore);
3406 * Determine whether a device is ready to be read or written to, and
3409 static unsigned int dgrp_net_select(struct file *file,
3410 struct poll_table_struct *table)
3412 unsigned int retval = 0;
3413 struct nd_struct *nd = file->private_data;
3415 poll_wait(file, &nd->nd_tx_waitq, table);
3417 if (nd->nd_tx_ready)
3418 retval |= POLLIN | POLLRDNORM; /* Conditionally readable */
3420 retval |= POLLOUT | POLLWRNORM; /* Always writeable */
3428 * Implement those functions which allow the network daemon to control
3429 * the network parameters in the driver. The ioctls include ones to
3430 * get and set the link speed parameters for the PortServer.
3432 static long dgrp_net_ioctl(struct file *file, unsigned int cmd,
3435 struct nd_struct *nd;
3437 long size = _IOC_SIZE(cmd);
3438 struct link_struct link;
3440 nd = file->private_data;
3442 if (_IOC_DIR(cmd) & _IOC_READ)
3443 rtn = access_ok(VERIFY_WRITE, (void __user *) arg, size);
3444 else if (_IOC_DIR(cmd) & _IOC_WRITE)
3445 rtn = access_ok(VERIFY_READ, (void __user *) arg, size);
3452 if (size != sizeof(struct link_struct))
3455 if (copy_from_user((void *)(&link), (void __user *) arg, size))
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;
3488 link.lk_fast_rate /= 8 * 1000 / dgrp_poll_tick;
3489 link.lk_slow_rate /= 8 * 1000 / dgrp_poll_tick;
3491 link.lk_fast_delay /= dgrp_poll_tick;
3492 link.lk_slow_delay /= dgrp_poll_tick;
3499 if (size != sizeof(struct link_struct))
3502 if (copy_to_user((void __user *)arg, (void *)(&nd->nd_link),
3517 * dgrp_poll_handler() -- handler for poll timer
3519 * As each timer expires, it determines (a) whether the "transmit"
3520 * waiter needs to be woken up, and (b) whether the poller needs to
3523 void dgrp_poll_handler(unsigned long arg)
3525 struct dgrp_poll_data *poll_data;
3526 struct nd_struct *nd;
3527 struct link_struct *lk;
3533 poll_data = (struct dgrp_poll_data *) arg;
3534 freq = 1000 / poll_data->poll_tick;
3535 poll_data->poll_round += 17;
3537 if (poll_data->poll_round >= freq)
3538 poll_data->poll_round -= freq;
3541 * Loop to process all open nodes.
3543 * For each node, determine the rate at which it should
3544 * be transmitting data. Then if the node should wake up
3545 * and transmit data now, enable the net receive select
3546 * to get the transmit going.
3549 list_for_each_entry(nd, &nd_struct_list, list) {
3554 * Decrement statistics. These are only for use with
3555 * KME, so don't worry that the operations are done
3556 * unlocked, and so the results are occassionally wrong.
3559 nd->nd_read_count -= (nd->nd_read_count +
3560 poll_data->poll_round) / freq;
3561 nd->nd_write_count -= (nd->nd_write_count +
3562 poll_data->poll_round) / freq;
3563 nd->nd_send_count -= (nd->nd_send_count +
3564 poll_data->poll_round) / freq;
3565 nd->nd_tx_byte -= (nd->nd_tx_byte +
3566 poll_data->poll_round) / freq;
3567 nd->nd_rx_byte -= (nd->nd_rx_byte +
3568 poll_data->poll_round) / freq;
3571 * Wake the daemon to transmit data only when there is
3572 * enough byte credit to send data.
3574 * The results are approximate because the operations
3575 * are performed unlocked, and we are inspecting
3576 * data asynchronously updated elsewhere. The whole
3577 * thing is just approximation anyway, so that should
3581 if (lk->lk_slow_rate >= UIO_MAX) {
3584 nd->nd_rate = UIO_MAX;
3586 nd->nd_tx_deposit = nd->nd_tx_charge + 3 * UIO_MAX;
3587 nd->nd_tx_credit = 3 * UIO_MAX;
3598 long seq_in = nd->nd_seq_in;
3599 long seq_out = nd->nd_seq_out;
3602 * If there are no outstanding packets, run at the
3606 if (seq_in == seq_out) {
3608 rate = lk->lk_fast_rate;
3612 * Otherwise compute the transmit rate based on the
3613 * delay since the oldest packet.
3618 * The actual delay is computed as the
3619 * time since the oldest unacknowledged
3620 * packet was sent, minus the time it
3621 * took to send that packet to the server.
3624 delay = ((jiffies - nd->nd_seq_time[seq_out])
3625 - (nd->nd_seq_size[seq_out] /
3629 * If the delay is less than the "fast"
3630 * delay, transmit full speed. If greater
3631 * than the "slow" delay, transmit at the
3632 * "slow" speed. In between, interpolate
3633 * between the fast and slow speeds.
3637 (delay <= lk->lk_fast_delay ?
3639 delay >= lk->lk_slow_delay ?
3642 (lk->lk_slow_delay - delay) *
3643 (lk->lk_fast_rate - lk->lk_slow_rate) /
3644 (lk->lk_slow_delay - lk->lk_fast_delay)
3649 nd->nd_delay = delay;
3653 * Increase the transmit credit by depositing the
3654 * current transmit rate.
3657 deposit = nd->nd_tx_deposit;
3658 charge = nd->nd_tx_charge;
3663 * If the available transmit credit becomes too large,
3664 * reduce the deposit to correct the value.
3666 * Too large is the max of:
3667 * 6 times the header size
3668 * 3 times the current transmit rate.
3671 size = 2 * nd->nd_link.lk_header_size;
3678 excess = deposit - charge - size;
3683 nd->nd_tx_deposit = deposit;
3684 nd->nd_tx_credit = deposit - charge;
3687 * Wake the transmit task only if the transmit credit
3688 * is at least 3 times the transmit header size.
3691 size = 3 * lk->lk_header_size;
3693 if (nd->nd_tx_credit < size)
3699 * Enable the READ select to wake the daemon if there
3700 * is useful work for the drp_read routine to perform.
3703 if (waitqueue_active(&nd->nd_tx_waitq) &&
3704 (nd->nd_tx_work != 0 ||
3705 (ulong)(jiffies - nd->nd_tx_time) >= IDLE_MAX)) {
3706 nd->nd_tx_ready = 1;
3708 wake_up_interruptible(&nd->nd_tx_waitq);
3711 /* nd->nd_flag &= ~ND_SELECT; */
3717 * Schedule ourself back at the nominal wakeup interval.
3719 spin_lock_irqsave(&poll_data->poll_lock, lock_flags);
3721 poll_data->node_active_count--;
3722 if (poll_data->node_active_count > 0) {
3723 poll_data->node_active_count++;
3724 poll_time = poll_data->timer.expires +
3725 poll_data->poll_tick * HZ / 1000;
3727 time = poll_time - jiffies;
3729 if (time >= 2 * poll_data->poll_tick)
3730 poll_time = jiffies + dgrp_poll_tick * HZ / 1000;
3732 poll_data->timer.expires = poll_time;
3733 add_timer(&poll_data->timer);
3736 spin_unlock_irqrestore(&poll_data->poll_lock, lock_flags);