linux/audit.h: move ptrace.h include to kernel header
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / dgrp / dgrp_net_ops.c
blob2d1bbfd5b67c6771db7dff7eea8208a0610ef918
1 /*
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)
9 * any later version.
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.
20 * Filename:
22 * dgrp_net_ops.c
24 * Description:
26 * Handle the file operations required for the "network" devices.
27 * Includes those functions required to register the "net" devices
28 * in "/proc".
30 * Author:
32 * James A. Puzzo
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,
68 loff_t *);
69 static long dgrp_net_ioctl(struct file *file, unsigned int cmd,
70 unsigned long arg);
71 static unsigned int dgrp_net_select(struct file *file,
72 struct poll_table_struct *table);
74 static const struct file_operations net_ops = {
75 .owner = THIS_MODULE,
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;
94 node->nd_net_de = de;
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)
108 int i;
110 pr_debug("dgrp dump length = %d, data = ", len);
111 for (i = 0; i < len; ++i)
112 pr_debug("%.2x ", mem[i]);
113 pr_debug("\n");
117 * dgrp_read_data_block() -- Read a data block
118 * @ch: struct ch_struct *
119 * @flipbuf: u8 *
120 * @flipbuf_size: size of flipbuf
122 static void dgrp_read_data_block(struct ch_struct *ch, u8 *flipbuf,
123 int flipbuf_size)
125 int t;
126 int n;
128 if (flipbuf_size <= 0)
129 return;
131 t = RBUF_MAX - ch->ch_rout;
132 n = flipbuf_size;
134 if (n >= t) {
135 memcpy(flipbuf, ch->ch_rbuf + ch->ch_rout, t);
136 flipbuf += t;
137 n -= t;
138 ch->ch_rout = 0;
141 memcpy(flipbuf, ch->ch_rbuf + ch->ch_rout, n);
142 flipbuf += n;
143 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.
155 static void dgrp_input(struct ch_struct *ch)
157 struct nd_struct *nd;
158 struct tty_struct *tty;
159 int data_len;
160 int len;
161 int tty_count;
162 ulong lock_flags;
163 u8 *myflipbuf;
164 u8 *myflipflagbuf;
166 if (!ch)
167 return;
169 nd = ch->ch_nd;
171 if (!nd)
172 return;
174 spin_lock_irqsave(&nd->nd_lock, lock_flags);
176 myflipbuf = nd->nd_inputbuf;
177 myflipflagbuf = nd->nd_inputflagbuf;
179 if (!ch->ch_open_count) {
180 ch->ch_rout = ch->ch_rin;
181 goto out;
184 if (ch->ch_tun.un_flag & UN_CLOSING) {
185 ch->ch_rout = ch->ch_rin;
186 goto out;
189 tty = (ch->ch_tun).un_tty;
192 if (!tty || tty->magic != TTY_MAGIC) {
193 ch->ch_rout = ch->ch_rin;
194 goto out;
197 tty_count = tty->count;
198 if (!tty_count) {
199 ch->ch_rout = ch->ch_rin;
200 goto out;
203 if (tty->closing || test_bit(TTY_CLOSING, &tty->flags)) {
204 ch->ch_rout = ch->ch_rin;
205 goto out;
208 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
210 /* data_len should be the number of chars that we read in */
211 data_len = (ch->ch_rin - ch->ch_rout) & RBUF_MASK;
213 /* len is the amount of data we are going to transfer here */
214 len = tty_buffer_request_room(tty, data_len);
216 /* Check DPA flow control */
217 if ((nd->nd_dpa_debug) &&
218 (nd->nd_dpa_flag & DPA_WAIT_SPACE) &&
219 (nd->nd_dpa_port == MINOR(tty_devnum(ch->ch_tun.un_tty))))
220 len = 0;
222 if ((len) && !(ch->ch_flag & CH_RXSTOP)) {
224 dgrp_read_data_block(ch, myflipbuf, len);
226 if (I_PARMRK(tty) || I_BRKINT(tty) || I_INPCK(tty))
227 parity_scan(ch, myflipbuf, myflipflagbuf, &len);
228 else
229 memset(myflipflagbuf, TTY_NORMAL, len);
231 if ((nd->nd_dpa_debug) &&
232 (nd->nd_dpa_port == PORT_NUM(MINOR(tty_devnum(tty)))))
233 dgrp_dpa_data(nd, 1, myflipbuf, len);
235 tty_insert_flip_string_flags(tty, myflipbuf,
236 myflipflagbuf, len);
237 tty_flip_buffer_push(tty);
239 ch->ch_rxcount += len;
243 * Wake up any sleepers (maybe dgrp close) that might be waiting
244 * for a channel flag state change.
246 wake_up_interruptible(&ch->ch_flag_wait);
247 return;
249 out:
250 spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
255 * parity_scan
257 * Loop to inspect each single character or 0xFF escape.
259 * if PARMRK & ~DOSMODE:
260 * 0xFF 0xFF Normal 0xFF character, escaped
261 * to eliminate confusion.
262 * 0xFF 0x00 0x00 Break
263 * 0xFF 0x00 CC Error character CC.
264 * CC Normal character CC.
266 * if PARMRK & DOSMODE:
267 * 0xFF 0x18 0x00 Break
268 * 0xFF 0x08 0x00 Framing Error
269 * 0xFF 0x04 0x00 Parity error
270 * 0xFF 0x0C 0x00 Both Framing and Parity error
272 * TODO: do we need to do the XMODEM, XOFF, XON, XANY processing??
273 * as per protocol
275 static void parity_scan(struct ch_struct *ch, unsigned char *cbuf,
276 unsigned char *fbuf, int *len)
278 int l = *len;
279 int count = 0;
280 int DOS = ((ch->ch_iflag & IF_DOSMODE) == 0 ? 0 : 1);
281 unsigned char *cout; /* character buffer */
282 unsigned char *fout; /* flag buffer */
283 unsigned char *in;
284 unsigned char c;
286 in = cbuf;
287 cout = cbuf;
288 fout = fbuf;
290 while (l--) {
291 c = *in;
292 in++;
294 switch (ch->ch_pscan_state) {
295 default:
296 /* reset to sanity and fall through */
297 ch->ch_pscan_state = 0 ;
299 case 0:
300 /* No FF seen yet */
301 if (c == 0xff) /* delete this character from stream */
302 ch->ch_pscan_state = 1;
303 else {
304 *cout++ = c;
305 *fout++ = TTY_NORMAL;
306 count += 1;
308 break;
310 case 1:
311 /* first FF seen */
312 if (c == 0xff) {
313 /* doubled ff, transform to single ff */
314 *cout++ = c;
315 *fout++ = TTY_NORMAL;
316 count += 1;
317 ch->ch_pscan_state = 0;
318 } else {
319 /* save value examination in next state */
320 ch->ch_pscan_savechar = c;
321 ch->ch_pscan_state = 2;
323 break;
325 case 2:
326 /* third character of ff sequence */
327 *cout++ = c;
328 if (DOS) {
329 if (ch->ch_pscan_savechar & 0x10)
330 *fout++ = TTY_BREAK;
331 else if (ch->ch_pscan_savechar & 0x08)
332 *fout++ = TTY_FRAME;
333 else
335 * either marked as a parity error,
336 * indeterminate, or not in DOSMODE
337 * call it a parity error
339 *fout++ = TTY_PARITY;
340 } else {
341 /* case FF XX ?? where XX is not 00 */
342 if (ch->ch_pscan_savechar & 0xff) {
343 /* this should not happen */
344 pr_info("%s: parity_scan: error unexpected byte\n",
345 __func__);
346 *fout++ = TTY_PARITY;
348 /* case FF 00 XX where XX is not 00 */
349 else if (c == 0xff)
350 *fout++ = TTY_PARITY;
351 /* case FF 00 00 */
352 else
353 *fout++ = TTY_BREAK;
356 count += 1;
357 ch->ch_pscan_state = 0;
360 *len = count;
365 * dgrp_net_idle() -- Idle the network connection
366 * @nd: pointer to node structure to idle
368 static void dgrp_net_idle(struct nd_struct *nd)
370 struct ch_struct *ch;
371 int i;
373 nd->nd_tx_work = 1;
375 nd->nd_state = NS_IDLE;
376 nd->nd_flag = 0;
378 for (i = nd->nd_seq_out; ; i = (i + 1) & SEQ_MASK) {
379 if (!nd->nd_seq_wait[i]) {
380 nd->nd_seq_wait[i] = 0;
381 wake_up_interruptible(&nd->nd_seq_wque[i]);
384 if (i == nd->nd_seq_in)
385 break;
388 nd->nd_seq_out = nd->nd_seq_in;
390 nd->nd_unack = 0;
391 nd->nd_remain = 0;
393 nd->nd_tx_module = 0x10;
394 nd->nd_rx_module = 0x00;
396 for (i = 0, ch = nd->nd_chan; i < CHAN_MAX; i++, ch++) {
397 ch->ch_state = CS_IDLE;
399 ch->ch_otype = 0;
400 ch->ch_otype_waiting = 0;
405 * Increase the number of channels, waking up any
406 * threads that might be waiting for the channels
407 * to appear.
409 static void increase_channel_count(struct nd_struct *nd, int n)
411 struct ch_struct *ch;
412 struct device *classp;
413 char name[DEVICE_NAME_SIZE];
414 int ret;
415 u8 *buf;
416 int i;
418 for (i = nd->nd_chan_count; i < n; ++i) {
419 ch = nd->nd_chan + i;
421 /* FIXME: return a useful error instead! */
422 buf = kmalloc(TBUF_MAX, GFP_KERNEL);
423 if (!buf)
424 return;
426 if (ch->ch_tbuf)
427 pr_info_ratelimited("%s - ch_tbuf was not NULL\n",
428 __func__);
430 ch->ch_tbuf = buf;
432 buf = kmalloc(RBUF_MAX, GFP_KERNEL);
433 if (!buf)
434 return;
436 if (ch->ch_rbuf)
437 pr_info("%s - ch_rbuf was not NULL\n",
438 __func__);
439 ch->ch_rbuf = buf;
441 classp = tty_port_register_device(&ch->port,
442 nd->nd_serial_ttdriver, i,
443 NULL);
445 ch->ch_tun.un_sysfs = classp;
446 snprintf(name, DEVICE_NAME_SIZE, "tty_%d", i);
448 dgrp_create_tty_sysfs(&ch->ch_tun, classp);
449 ret = sysfs_create_link(&nd->nd_class_dev->kobj,
450 &classp->kobj, name);
452 /* NOTE: We don't support "cu" devices anymore,
453 * so you will notice we don't register them
454 * here anymore. */
455 if (dgrp_register_prdevices) {
456 classp = tty_register_device(nd->nd_xprint_ttdriver,
457 i, NULL);
458 ch->ch_pun.un_sysfs = classp;
459 snprintf(name, DEVICE_NAME_SIZE, "pr_%d", i);
461 dgrp_create_tty_sysfs(&ch->ch_pun, classp);
462 ret = sysfs_create_link(&nd->nd_class_dev->kobj,
463 &classp->kobj, name);
466 nd->nd_chan_count = i + 1;
467 wake_up_interruptible(&ch->ch_flag_wait);
472 * Decrease the number of channels, and wake up any threads that might
473 * be waiting on the channels that vanished.
475 static void decrease_channel_count(struct nd_struct *nd, int n)
477 struct ch_struct *ch;
478 char name[DEVICE_NAME_SIZE];
479 int i;
481 for (i = nd->nd_chan_count - 1; i >= n; --i) {
482 ch = nd->nd_chan + i;
485 * Make any open ports inoperative.
487 ch->ch_state = CS_IDLE;
489 ch->ch_otype = 0;
490 ch->ch_otype_waiting = 0;
493 * Only "HANGUP" if we care about carrier
494 * transitions and we are already open.
496 if (ch->ch_open_count != 0) {
497 ch->ch_flag |= CH_HANGUP;
498 dgrp_carrier(ch);
502 * Unlike the CH_HANGUP flag above, use another
503 * flag to indicate to the RealPort state machine
504 * that this port has disappeared.
506 if (ch->ch_open_count != 0)
507 ch->ch_flag |= CH_PORT_GONE;
509 wake_up_interruptible(&ch->ch_flag_wait);
511 nd->nd_chan_count = i;
513 kfree(ch->ch_tbuf);
514 ch->ch_tbuf = NULL;
516 kfree(ch->ch_rbuf);
517 ch->ch_rbuf = NULL;
519 nd->nd_chan_count = i;
521 dgrp_remove_tty_sysfs(ch->ch_tun.un_sysfs);
522 snprintf(name, DEVICE_NAME_SIZE, "tty_%d", i);
523 sysfs_remove_link(&nd->nd_class_dev->kobj, name);
524 tty_unregister_device(nd->nd_serial_ttdriver, i);
527 * NOTE: We don't support "cu" devices anymore, so don't
528 * unregister them here anymore.
531 if (dgrp_register_prdevices) {
532 dgrp_remove_tty_sysfs(ch->ch_pun.un_sysfs);
533 snprintf(name, DEVICE_NAME_SIZE, "pr_%d", i);
534 sysfs_remove_link(&nd->nd_class_dev->kobj, name);
535 tty_unregister_device(nd->nd_xprint_ttdriver, i);
541 * dgrp_chan_count() -- Adjust the node channel count.
542 * @nd: pointer to a node structure
543 * @n: new value for channel count
545 * Adjusts the node channel count. If new ports have appeared, it tries
546 * to signal those processes that might have been waiting for ports to
547 * appear. If ports have disappeared it tries to signal those processes
548 * that might be hung waiting for a response for the now non-existant port.
550 static void dgrp_chan_count(struct nd_struct *nd, int n)
552 if (n == nd->nd_chan_count)
553 return;
555 if (n > nd->nd_chan_count)
556 increase_channel_count(nd, n);
558 if (n < nd->nd_chan_count)
559 decrease_channel_count(nd, n);
563 * dgrp_monitor() -- send data to the device monitor queue
564 * @nd: pointer to a node structure
565 * @buf: data to copy to the monitoring buffer
566 * @len: number of bytes to transfer to the buffer
568 * Called by the net device routines to send data to the device
569 * monitor queue. If the device monitor buffer is too full to
570 * accept the data, it waits until the buffer is ready.
572 static void dgrp_monitor(struct nd_struct *nd, u8 *buf, int len)
574 int n;
575 int r;
576 int rtn;
579 * Grab monitor lock.
581 down(&nd->nd_mon_semaphore);
584 * Loop while data remains.
586 while ((len > 0) && (nd->nd_mon_buf)) {
588 * Determine the amount of available space left in the
589 * buffer. If there's none, wait until some appears.
592 n = (nd->nd_mon_out - nd->nd_mon_in - 1) & MON_MASK;
594 if (!n) {
595 nd->nd_mon_flag |= MON_WAIT_SPACE;
597 up(&nd->nd_mon_semaphore);
600 * Go to sleep waiting until the condition becomes true.
602 rtn = wait_event_interruptible(nd->nd_mon_wqueue,
603 ((nd->nd_mon_flag & MON_WAIT_SPACE) == 0));
605 /* FIXME: really ignore rtn? */
608 * We can't exit here if we receive a signal, since
609 * to do so would trash the debug stream.
612 down(&nd->nd_mon_semaphore);
614 continue;
618 * Copy as much data as will fit.
621 if (n > len)
622 n = len;
624 r = MON_MAX - nd->nd_mon_in;
626 if (r <= n) {
627 memcpy(nd->nd_mon_buf + nd->nd_mon_in, buf, r);
629 n -= r;
631 nd->nd_mon_in = 0;
633 buf += r;
634 len -= r;
637 memcpy(nd->nd_mon_buf + nd->nd_mon_in, buf, n);
639 nd->nd_mon_in += n;
641 buf += n;
642 len -= n;
644 if (nd->nd_mon_in >= MON_MAX)
645 pr_info_ratelimited("%s - nd_mon_in (%i) >= MON_MAX\n",
646 __func__, nd->nd_mon_in);
649 * Wakeup any thread waiting for data
652 if (nd->nd_mon_flag & MON_WAIT_DATA) {
653 nd->nd_mon_flag &= ~MON_WAIT_DATA;
654 wake_up_interruptible(&nd->nd_mon_wqueue);
659 * Release the monitor lock.
661 up(&nd->nd_mon_semaphore);
665 * dgrp_encode_time() -- Encodes rpdump time into a 4-byte quantity.
666 * @nd: pointer to a node structure
667 * @buf: destination buffer
669 * Encodes "rpdump" time into a 4-byte quantity. Time is measured since
670 * open.
672 static void dgrp_encode_time(struct nd_struct *nd, u8 *buf)
674 ulong t;
677 * Convert time in HZ since open to time in milliseconds
678 * since open.
680 t = jiffies - nd->nd_mon_lbolt;
681 t = 1000 * (t / HZ) + 1000 * (t % HZ) / HZ;
683 put_unaligned_be32((uint)(t & 0xffffffff), buf);
689 * dgrp_monitor_message() -- Builds a rpdump style message.
690 * @nd: pointer to a node structure
691 * @message: destination buffer
693 static void dgrp_monitor_message(struct nd_struct *nd, char *message)
695 u8 header[7];
696 int n;
698 header[0] = RPDUMP_MESSAGE;
700 dgrp_encode_time(nd, header + 1);
702 n = strlen(message);
704 put_unaligned_be16(n, header + 5);
706 dgrp_monitor(nd, header, sizeof(header));
707 dgrp_monitor(nd, (u8 *) message, n);
713 * dgrp_monitor_reset() -- Note a reset in the monitoring buffer.
714 * @nd: pointer to a node structure
716 static void dgrp_monitor_reset(struct nd_struct *nd)
718 u8 header[5];
720 header[0] = RPDUMP_RESET;
722 dgrp_encode_time(nd, header + 1);
724 dgrp_monitor(nd, header, sizeof(header));
728 * dgrp_monitor_data() -- builds a monitor data packet
729 * @nd: pointer to a node structure
730 * @type: type of message to be logged
731 * @buf: data to be logged
732 * @size: number of bytes in the buffer
734 static void dgrp_monitor_data(struct nd_struct *nd, u8 type, u8 *buf, int size)
736 u8 header[7];
738 header[0] = type;
740 dgrp_encode_time(nd, header + 1);
742 put_unaligned_be16(size, header + 5);
744 dgrp_monitor(nd, header, sizeof(header));
745 dgrp_monitor(nd, buf, size);
748 static int alloc_nd_buffers(struct nd_struct *nd)
751 nd->nd_iobuf = NULL;
752 nd->nd_writebuf = NULL;
753 nd->nd_inputbuf = NULL;
754 nd->nd_inputflagbuf = NULL;
757 * Allocate the network read/write buffer.
759 nd->nd_iobuf = kzalloc(UIO_MAX + 10, GFP_KERNEL);
760 if (!nd->nd_iobuf)
761 goto out_err;
764 * Allocate a buffer for doing the copy from user space to
765 * kernel space in the write routines.
767 nd->nd_writebuf = kzalloc(WRITEBUFLEN, GFP_KERNEL);
768 if (!nd->nd_writebuf)
769 goto out_err;
772 * Allocate a buffer for doing the copy from kernel space to
773 * tty buffer space in the read routines.
775 nd->nd_inputbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
776 if (!nd->nd_inputbuf)
777 goto out_err;
780 * Allocate a buffer for doing the copy from kernel space to
781 * tty buffer space in the read routines.
783 nd->nd_inputflagbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
784 if (!nd->nd_inputflagbuf)
785 goto out_err;
787 return 0;
789 out_err:
790 kfree(nd->nd_iobuf);
791 kfree(nd->nd_writebuf);
792 kfree(nd->nd_inputbuf);
793 kfree(nd->nd_inputflagbuf);
794 return -ENOMEM;
798 * dgrp_net_open() -- Open the NET device for a particular PortServer
800 static int dgrp_net_open(struct inode *inode, struct file *file)
802 struct nd_struct *nd;
803 struct proc_dir_entry *de;
804 ulong lock_flags;
805 int rtn;
807 rtn = try_module_get(THIS_MODULE);
808 if (!rtn)
809 return -EAGAIN;
811 if (!capable(CAP_SYS_ADMIN)) {
812 rtn = -EPERM;
813 goto done;
817 * Make sure that the "private_data" field hasn't already been used.
819 if (file->private_data) {
820 rtn = -EINVAL;
821 goto done;
825 * Get the node pointer, and fail if it doesn't exist.
827 de = PDE(inode);
828 if (!de) {
829 rtn = -ENXIO;
830 goto done;
833 nd = (struct nd_struct *) de->data;
834 if (!nd) {
835 rtn = -ENXIO;
836 goto done;
839 file->private_data = (void *) nd;
842 * Grab the NET lock.
844 down(&nd->nd_net_semaphore);
846 if (nd->nd_state != NS_CLOSED) {
847 rtn = -EBUSY;
848 goto unlock;
852 * Initialize the link speed parameters.
855 nd->nd_link.lk_fast_rate = UIO_MAX;
856 nd->nd_link.lk_slow_rate = UIO_MAX;
858 nd->nd_link.lk_fast_delay = 1000;
859 nd->nd_link.lk_slow_delay = 1000;
861 nd->nd_link.lk_header_size = 46;
864 rtn = alloc_nd_buffers(nd);
865 if (rtn)
866 goto unlock;
869 * The port is now open, so move it to the IDLE state
871 dgrp_net_idle(nd);
873 nd->nd_tx_time = jiffies;
876 * If the polling routing is not running, start it running here
878 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
880 if (!dgrp_poll_data.node_active_count) {
881 dgrp_poll_data.node_active_count = 2;
882 dgrp_poll_data.timer.expires = jiffies +
883 dgrp_poll_tick * HZ / 1000;
884 add_timer(&dgrp_poll_data.timer);
887 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
889 dgrp_monitor_message(nd, "Net Open");
891 unlock:
893 * Release the NET lock.
895 up(&nd->nd_net_semaphore);
897 done:
898 if (rtn)
899 module_put(THIS_MODULE);
901 return rtn;
904 /* dgrp_net_release() -- close the NET device for a particular PortServer */
905 static int dgrp_net_release(struct inode *inode, struct file *file)
907 struct nd_struct *nd;
908 ulong lock_flags;
910 nd = (struct nd_struct *)(file->private_data);
911 if (!nd)
912 goto done;
914 /* TODO : historical locking placeholder */
916 * In the HPUX version of the RealPort driver (which served as a basis
917 * for this driver) this locking code was used. Saved if ever we need
918 * to review the locking under Linux.
920 /* spinlock(&nd->nd_lock); */
924 * Grab the NET lock.
926 down(&nd->nd_net_semaphore);
929 * Before "closing" the internal connection, make sure all
930 * ports are "idle".
932 dgrp_net_idle(nd);
934 nd->nd_state = NS_CLOSED;
935 nd->nd_flag = 0;
938 * TODO ... must the wait queue be reset on close?
939 * should any pending waiters be reset?
940 * Let's decide to assert that the waitq is empty... and see
941 * how soon we break.
943 if (waitqueue_active(&nd->nd_tx_waitq))
944 pr_info("%s - expected waitqueue_active to be false\n",
945 __func__);
947 nd->nd_send = 0;
949 kfree(nd->nd_iobuf);
950 nd->nd_iobuf = NULL;
952 /* TODO : historical locking placeholder */
954 * In the HPUX version of the RealPort driver (which served as a basis
955 * for this driver) this locking code was used. Saved if ever we need
956 * to review the locking under Linux.
958 /* spinunlock( &nd->nd_lock ); */
961 kfree(nd->nd_writebuf);
962 nd->nd_writebuf = NULL;
964 kfree(nd->nd_inputbuf);
965 nd->nd_inputbuf = NULL;
967 kfree(nd->nd_inputflagbuf);
968 nd->nd_inputflagbuf = NULL;
970 /* TODO : historical locking placeholder */
972 * In the HPUX version of the RealPort driver (which served as a basis
973 * for this driver) this locking code was used. Saved if ever we need
974 * to review the locking under Linux.
976 /* spinlock(&nd->nd_lock); */
979 * Set the active port count to zero.
981 dgrp_chan_count(nd, 0);
983 /* TODO : historical locking placeholder */
985 * In the HPUX version of the RealPort driver (which served as a basis
986 * for this driver) this locking code was used. Saved if ever we need
987 * to review the locking under Linux.
989 /* spinunlock(&nd->nd_lock); */
992 * Release the NET lock.
994 up(&nd->nd_net_semaphore);
997 * Cause the poller to stop scheduling itself if this is
998 * the last active node.
1000 spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
1002 if (dgrp_poll_data.node_active_count == 2) {
1003 del_timer(&dgrp_poll_data.timer);
1004 dgrp_poll_data.node_active_count = 0;
1007 spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
1009 down(&nd->nd_net_semaphore);
1011 dgrp_monitor_message(nd, "Net Close");
1013 up(&nd->nd_net_semaphore);
1015 done:
1016 module_put(THIS_MODULE);
1017 file->private_data = NULL;
1018 return 0;
1021 /* used in dgrp_send to setup command header */
1022 static inline u8 *set_cmd_header(u8 *b, u8 port, u8 cmd)
1024 *b++ = 0xb0 + (port & 0x0f);
1025 *b++ = cmd;
1026 return b;
1030 * dgrp_send() -- build a packet for transmission to the server
1031 * @nd: pointer to a node structure
1032 * @tmax: maximum bytes to transmit
1034 * returns number of bytes sent
1036 static int dgrp_send(struct nd_struct *nd, long tmax)
1038 struct ch_struct *ch = nd->nd_chan;
1039 u8 *b;
1040 u8 *buf;
1041 u8 *mbuf;
1042 u8 port;
1043 int mod;
1044 long send;
1045 int maxport;
1046 long lastport = -1;
1047 ushort rwin;
1048 long in;
1049 ushort n;
1050 long t;
1051 long ttotal;
1052 long tchan;
1053 long tsend;
1054 ushort tsafe;
1055 long work;
1056 long send_sync;
1057 long wanted_sync_port = -1;
1058 ushort tdata[CHAN_MAX];
1059 long used_buffer;
1061 mbuf = nd->nd_iobuf + UIO_BASE;
1062 buf = b = mbuf;
1064 send_sync = nd->nd_link.lk_slow_rate < UIO_MAX;
1066 ttotal = 0;
1067 tchan = 0;
1069 memset(tdata, 0, sizeof(tdata));
1073 * If there are any outstanding requests to be serviced,
1074 * service them here.
1076 if (nd->nd_send & NR_PASSWORD) {
1079 * Send Password response.
1082 b[0] = 0xfc;
1083 b[1] = 0x20;
1084 put_unaligned_be16(strlen(nd->password), b + 2);
1085 b += 4;
1086 b += strlen(nd->password);
1087 nd->nd_send &= ~(NR_PASSWORD);
1092 * Loop over all modules to generate commands, and determine
1093 * the amount of data queued for transmit.
1096 for (mod = 0, port = 0; port < nd->nd_chan_count; mod++) {
1098 * If this is not the current module, enter a module select
1099 * code in the buffer.
1102 if (mod != nd->nd_tx_module)
1103 mbuf = ++b;
1106 * Loop to process one module.
1109 maxport = port + 16;
1111 if (maxport > nd->nd_chan_count)
1112 maxport = nd->nd_chan_count;
1114 for (; port < maxport; port++, ch++) {
1116 * Switch based on channel state.
1119 switch (ch->ch_state) {
1121 * Send requests when the port is closed, and there
1122 * are no Open, Close or Cancel requests expected.
1125 case CS_IDLE:
1127 * Wait until any open error code
1128 * has been delivered to all
1129 * associated ports.
1132 if (ch->ch_open_error) {
1133 if (ch->ch_wait_count[ch->ch_otype]) {
1134 work = 1;
1135 break;
1138 ch->ch_open_error = 0;
1142 * Wait until the channel HANGUP flag is reset
1143 * before sending the first open. We can only
1144 * get to this state after a server disconnect.
1147 if ((ch->ch_flag & CH_HANGUP) != 0)
1148 break;
1151 * If recovering from a TCP disconnect, or if
1152 * there is an immediate open pending, send an
1153 * Immediate Open request.
1155 if ((ch->ch_flag & CH_PORT_GONE) ||
1156 ch->ch_wait_count[OTYPE_IMMEDIATE] != 0) {
1157 b = set_cmd_header(b, port, 10);
1158 *b++ = 0;
1160 ch->ch_state = CS_WAIT_OPEN;
1161 ch->ch_otype = OTYPE_IMMEDIATE;
1162 break;
1166 * If there is no Persistent or Incoming Open on the wait
1167 * list in the server, and a thread is waiting for a
1168 * Persistent or Incoming Open, send a Persistent or Incoming
1169 * Open Request.
1171 if (ch->ch_otype_waiting == 0) {
1172 if (ch->ch_wait_count[OTYPE_PERSISTENT] != 0) {
1173 b = set_cmd_header(b, port, 10);
1174 *b++ = 1;
1176 ch->ch_state = CS_WAIT_OPEN;
1177 ch->ch_otype = OTYPE_PERSISTENT;
1178 } else if (ch->ch_wait_count[OTYPE_INCOMING] != 0) {
1179 b = set_cmd_header(b, port, 10);
1180 *b++ = 2;
1182 ch->ch_state = CS_WAIT_OPEN;
1183 ch->ch_otype = OTYPE_INCOMING;
1185 break;
1189 * If a Persistent or Incoming Open is pending in
1190 * the server, but there is no longer an open
1191 * thread waiting for it, cancel the request.
1194 if (ch->ch_wait_count[ch->ch_otype_waiting] == 0) {
1195 b = set_cmd_header(b, port, 10);
1196 *b++ = 4;
1198 ch->ch_state = CS_WAIT_CANCEL;
1199 ch->ch_otype = ch->ch_otype_waiting;
1201 break;
1204 * Send port parameter queries.
1206 case CS_SEND_QUERY:
1208 * Clear out all FEP state that might remain
1209 * from the last connection.
1212 ch->ch_flag |= CH_PARAM;
1214 ch->ch_flag &= ~CH_RX_FLUSH;
1216 ch->ch_expect = 0;
1218 ch->ch_s_tin = 0;
1219 ch->ch_s_tpos = 0;
1220 ch->ch_s_tsize = 0;
1221 ch->ch_s_treq = 0;
1222 ch->ch_s_elast = 0;
1224 ch->ch_s_rin = 0;
1225 ch->ch_s_rwin = 0;
1226 ch->ch_s_rsize = 0;
1228 ch->ch_s_tmax = 0;
1229 ch->ch_s_ttime = 0;
1230 ch->ch_s_rmax = 0;
1231 ch->ch_s_rtime = 0;
1232 ch->ch_s_rlow = 0;
1233 ch->ch_s_rhigh = 0;
1235 ch->ch_s_brate = 0;
1236 ch->ch_s_iflag = 0;
1237 ch->ch_s_cflag = 0;
1238 ch->ch_s_oflag = 0;
1239 ch->ch_s_xflag = 0;
1241 ch->ch_s_mout = 0;
1242 ch->ch_s_mflow = 0;
1243 ch->ch_s_mctrl = 0;
1244 ch->ch_s_xon = 0;
1245 ch->ch_s_xoff = 0;
1246 ch->ch_s_lnext = 0;
1247 ch->ch_s_xxon = 0;
1248 ch->ch_s_xxoff = 0;
1250 /* Send Sequence Request */
1251 b = set_cmd_header(b, port, 14);
1253 /* Configure Event Conditions Packet */
1254 b = set_cmd_header(b, port, 42);
1255 put_unaligned_be16(0x02c0, b);
1256 b += 2;
1257 *b++ = (DM_DTR | DM_RTS | DM_CTS |
1258 DM_DSR | DM_RI | DM_CD);
1260 /* Send Status Request */
1261 b = set_cmd_header(b, port, 16);
1263 /* Send Buffer Request */
1264 b = set_cmd_header(b, port, 20);
1266 /* Send Port Capability Request */
1267 b = set_cmd_header(b, port, 22);
1269 ch->ch_expect = (RR_SEQUENCE |
1270 RR_STATUS |
1271 RR_BUFFER |
1272 RR_CAPABILITY);
1274 ch->ch_state = CS_WAIT_QUERY;
1276 /* Raise modem signals */
1277 b = set_cmd_header(b, port, 44);
1279 if (ch->ch_flag & CH_PORT_GONE)
1280 ch->ch_s_mout = ch->ch_mout;
1281 else
1282 ch->ch_s_mout = ch->ch_mout = DM_DTR | DM_RTS;
1284 *b++ = ch->ch_mout;
1285 *b++ = ch->ch_s_mflow = 0;
1286 *b++ = ch->ch_s_mctrl = ch->ch_mctrl = 0;
1288 if (ch->ch_flag & CH_PORT_GONE)
1289 ch->ch_flag &= ~CH_PORT_GONE;
1291 break;
1294 * Handle normal open and ready mode.
1297 case CS_READY:
1300 * If the port is not open, and there are no
1301 * no longer any ports requesting an open,
1302 * then close the port.
1305 if (ch->ch_open_count == 0 &&
1306 ch->ch_wait_count[ch->ch_otype] == 0) {
1307 goto send_close;
1311 * Process waiting input.
1313 * If there is no one to read it, discard the data.
1315 * Otherwise if we are not in fastcook mode, or if there is a
1316 * fastcook thread waiting for data, send the data to the
1317 * line discipline.
1319 if (ch->ch_rin != ch->ch_rout) {
1320 if (ch->ch_tun.un_open_count == 0 ||
1321 (ch->ch_tun.un_flag & UN_CLOSING) ||
1322 (ch->ch_cflag & CF_CREAD) == 0) {
1323 ch->ch_rout = ch->ch_rin;
1324 } else if ((ch->ch_flag & CH_FAST_READ) == 0 ||
1325 ch->ch_inwait != 0) {
1326 dgrp_input(ch);
1328 if (ch->ch_rin != ch->ch_rout)
1329 work = 1;
1334 * Handle receive flush, and changes to
1335 * server port parameters.
1338 if (ch->ch_flag & (CH_RX_FLUSH | CH_PARAM)) {
1340 * If we are in receive flush mode,
1341 * and enough data has gone by, reset
1342 * receive flush mode.
1344 if (ch->ch_flag & CH_RX_FLUSH) {
1345 if (((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >
1346 ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK))
1347 ch->ch_flag &= ~CH_RX_FLUSH;
1348 else
1349 work = 1;
1353 * Send TMAX, TTIME.
1356 if (ch->ch_s_tmax != ch->ch_tmax ||
1357 ch->ch_s_ttime != ch->ch_ttime) {
1358 b = set_cmd_header(b, port, 48);
1360 ch->ch_s_tmax = ch->ch_tmax;
1361 ch->ch_s_ttime = ch->ch_ttime;
1363 put_unaligned_be16(ch->ch_s_tmax,
1365 b += 2;
1367 put_unaligned_be16(ch->ch_s_ttime,
1369 b += 2;
1373 * Send RLOW, RHIGH.
1376 if (ch->ch_s_rlow != ch->ch_rlow ||
1377 ch->ch_s_rhigh != ch->ch_rhigh) {
1378 b = set_cmd_header(b, port, 45);
1380 ch->ch_s_rlow = ch->ch_rlow;
1381 ch->ch_s_rhigh = ch->ch_rhigh;
1383 put_unaligned_be16(ch->ch_s_rlow,
1385 b += 2;
1387 put_unaligned_be16(ch->ch_s_rhigh,
1389 b += 2;
1393 * Send BRATE, CFLAG, IFLAG,
1394 * OFLAG, XFLAG.
1397 if (ch->ch_s_brate != ch->ch_brate ||
1398 ch->ch_s_cflag != ch->ch_cflag ||
1399 ch->ch_s_iflag != ch->ch_iflag ||
1400 ch->ch_s_oflag != ch->ch_oflag ||
1401 ch->ch_s_xflag != ch->ch_xflag) {
1402 b = set_cmd_header(b, port, 40);
1404 ch->ch_s_brate = ch->ch_brate;
1405 ch->ch_s_cflag = ch->ch_cflag;
1406 ch->ch_s_iflag = ch->ch_iflag;
1407 ch->ch_s_oflag = ch->ch_oflag;
1408 ch->ch_s_xflag = ch->ch_xflag;
1410 put_unaligned_be16(ch->ch_s_brate,
1412 b += 2;
1414 put_unaligned_be16(ch->ch_s_cflag,
1416 b += 2;
1418 put_unaligned_be16(ch->ch_s_iflag,
1420 b += 2;
1422 put_unaligned_be16(ch->ch_s_oflag,
1424 b += 2;
1426 put_unaligned_be16(ch->ch_s_xflag,
1428 b += 2;
1432 * Send MOUT, MFLOW, MCTRL.
1435 if (ch->ch_s_mout != ch->ch_mout ||
1436 ch->ch_s_mflow != ch->ch_mflow ||
1437 ch->ch_s_mctrl != ch->ch_mctrl) {
1438 b = set_cmd_header(b, port, 44);
1440 *b++ = ch->ch_s_mout = ch->ch_mout;
1441 *b++ = ch->ch_s_mflow = ch->ch_mflow;
1442 *b++ = ch->ch_s_mctrl = ch->ch_mctrl;
1446 * Send Flow control characters.
1449 if (ch->ch_s_xon != ch->ch_xon ||
1450 ch->ch_s_xoff != ch->ch_xoff ||
1451 ch->ch_s_lnext != ch->ch_lnext ||
1452 ch->ch_s_xxon != ch->ch_xxon ||
1453 ch->ch_s_xxoff != ch->ch_xxoff) {
1454 b = set_cmd_header(b, port, 46);
1456 *b++ = ch->ch_s_xon = ch->ch_xon;
1457 *b++ = ch->ch_s_xoff = ch->ch_xoff;
1458 *b++ = ch->ch_s_lnext = ch->ch_lnext;
1459 *b++ = ch->ch_s_xxon = ch->ch_xxon;
1460 *b++ = ch->ch_s_xxoff = ch->ch_xxoff;
1464 * Send RMAX, RTIME.
1467 if (ch->ch_s_rmax != ch->ch_rmax ||
1468 ch->ch_s_rtime != ch->ch_rtime) {
1469 b = set_cmd_header(b, port, 47);
1471 ch->ch_s_rmax = ch->ch_rmax;
1472 ch->ch_s_rtime = ch->ch_rtime;
1474 put_unaligned_be16(ch->ch_s_rmax,
1476 b += 2;
1478 put_unaligned_be16(ch->ch_s_rtime,
1480 b += 2;
1483 ch->ch_flag &= ~CH_PARAM;
1484 wake_up_interruptible(&ch->ch_flag_wait);
1489 * Handle action commands.
1492 if (ch->ch_send != 0) {
1493 /* int send = ch->ch_send & ~ch->ch_expect; */
1494 send = ch->ch_send & ~ch->ch_expect;
1496 /* Send character immediate */
1497 if ((send & RR_TX_ICHAR) != 0) {
1498 b = set_cmd_header(b, port, 60);
1500 *b++ = ch->ch_xon;
1501 ch->ch_expect |= RR_TX_ICHAR;
1504 /* BREAK request */
1505 if ((send & RR_TX_BREAK) != 0) {
1506 if (ch->ch_break_time != 0) {
1507 b = set_cmd_header(b, port, 61);
1508 put_unaligned_be16(ch->ch_break_time,
1510 b += 2;
1512 ch->ch_expect |= RR_TX_BREAK;
1513 ch->ch_break_time = 0;
1514 } else {
1515 ch->ch_send &= ~RR_TX_BREAK;
1516 ch->ch_flag &= ~CH_TX_BREAK;
1517 wake_up_interruptible(&ch->ch_flag_wait);
1522 * Flush input/output buffers.
1525 if ((send & (RR_RX_FLUSH | RR_TX_FLUSH)) != 0) {
1526 b = set_cmd_header(b, port, 62);
1528 *b++ = ((send & RR_TX_FLUSH) == 0 ? 1 :
1529 (send & RR_RX_FLUSH) == 0 ? 2 : 3);
1531 if (send & RR_RX_FLUSH) {
1532 ch->ch_flush_seq = nd->nd_seq_in;
1533 ch->ch_flag |= CH_RX_FLUSH;
1534 work = 1;
1535 send_sync = 1;
1536 wanted_sync_port = port;
1539 ch->ch_send &= ~(RR_RX_FLUSH | RR_TX_FLUSH);
1542 /* Pause input/output */
1543 if ((send & (RR_RX_STOP | RR_TX_STOP)) != 0) {
1544 b = set_cmd_header(b, port, 63);
1545 *b = 0;
1547 if ((send & RR_TX_STOP) != 0)
1548 *b |= EV_OPU;
1550 if ((send & RR_RX_STOP) != 0)
1551 *b |= EV_IPU;
1553 b++;
1555 ch->ch_send &= ~(RR_RX_STOP | RR_TX_STOP);
1558 /* Start input/output */
1559 if ((send & (RR_RX_START | RR_TX_START)) != 0) {
1560 b = set_cmd_header(b, port, 64);
1561 *b = 0;
1563 if ((send & RR_TX_START) != 0)
1564 *b |= EV_OPU | EV_OPS | EV_OPX;
1566 if ((send & RR_RX_START) != 0)
1567 *b |= EV_IPU | EV_IPS;
1569 b++;
1571 ch->ch_send &= ~(RR_RX_START | RR_TX_START);
1577 * Send a window sequence to acknowledge received data.
1580 rwin = (ch->ch_s_rin +
1581 ((ch->ch_rout - ch->ch_rin - 1) & RBUF_MASK));
1583 n = (rwin - ch->ch_s_rwin) & 0xffff;
1585 if (n >= RBUF_MAX / 4) {
1586 b[0] = 0xa0 + (port & 0xf);
1587 ch->ch_s_rwin = rwin;
1588 put_unaligned_be16(rwin, b + 1);
1589 b += 3;
1593 * If the terminal is waiting on LOW
1594 * water or EMPTY, and the condition
1595 * is now satisfied, call the line
1596 * discipline to put more data in the
1597 * buffer.
1600 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1602 if ((ch->ch_tun.un_flag & (UN_EMPTY|UN_LOW)) != 0) {
1603 if ((ch->ch_tun.un_flag & UN_LOW) != 0 ?
1604 (n <= TBUF_LOW) :
1605 (n == 0 && ch->ch_s_tpos == ch->ch_s_tin)) {
1606 ch->ch_tun.un_flag &= ~(UN_EMPTY|UN_LOW);
1608 if (waitqueue_active(&((ch->ch_tun.un_tty)->write_wait)))
1609 wake_up_interruptible(&((ch->ch_tun.un_tty)->write_wait));
1610 tty_wakeup(ch->ch_tun.un_tty);
1611 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1616 * If the printer is waiting on LOW
1617 * water, TIME, EMPTY or PWAIT, and is
1618 * now ready to put more data in the
1619 * buffer, call the line discipline to
1620 * do the job.
1623 /* FIXME: jiffies - ch->ch_waketime can never
1624 be < 0. Someone needs to work out what is
1625 actually intended here */
1626 if (ch->ch_pun.un_open_count &&
1627 (ch->ch_pun.un_flag &
1628 (UN_EMPTY|UN_TIME|UN_LOW|UN_PWAIT)) != 0) {
1630 if ((ch->ch_pun.un_flag & UN_LOW) != 0 ?
1631 (n <= TBUF_LOW) :
1632 (ch->ch_pun.un_flag & UN_TIME) != 0 ?
1633 ((jiffies - ch->ch_waketime) >= 0) :
1634 (n == 0 && ch->ch_s_tpos == ch->ch_s_tin) &&
1635 ((ch->ch_pun.un_flag & UN_EMPTY) != 0 ||
1636 ((ch->ch_tun.un_open_count &&
1637 ch->ch_tun.un_tty->ops->chars_in_buffer) ?
1638 (ch->ch_tun.un_tty->ops->chars_in_buffer)(ch->ch_tun.un_tty) == 0
1641 )) {
1642 ch->ch_pun.un_flag &= ~(UN_EMPTY | UN_TIME | UN_LOW | UN_PWAIT);
1644 if (waitqueue_active(&((ch->ch_pun.un_tty)->write_wait)))
1645 wake_up_interruptible(&((ch->ch_pun.un_tty)->write_wait));
1646 tty_wakeup(ch->ch_pun.un_tty);
1647 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1649 } else if ((ch->ch_pun.un_flag & UN_TIME) != 0) {
1650 work = 1;
1656 * Determine the max number of bytes
1657 * this port can send, including
1658 * packet header overhead.
1661 t = ((ch->ch_s_tsize + ch->ch_s_tpos - ch->ch_s_tin) & 0xffff);
1663 if (n > t)
1664 n = t;
1666 if (n != 0) {
1667 n += (n <= 8 ? 1 : n <= 255 ? 2 : 3);
1669 tdata[tchan++] = n;
1670 ttotal += n;
1672 break;
1675 * Close the port.
1678 send_close:
1679 case CS_SEND_CLOSE:
1680 b = set_cmd_header(b, port, 10);
1681 if (ch->ch_otype == OTYPE_IMMEDIATE)
1682 *b++ = 3;
1683 else
1684 *b++ = 4;
1686 ch->ch_state = CS_WAIT_CLOSE;
1687 break;
1690 * Wait for a previous server request.
1693 case CS_WAIT_OPEN:
1694 case CS_WAIT_CANCEL:
1695 case CS_WAIT_FAIL:
1696 case CS_WAIT_QUERY:
1697 case CS_WAIT_CLOSE:
1698 break;
1700 default:
1701 pr_info("%s - unexpected channel state (%i)\n",
1702 __func__, ch->ch_state);
1707 * If a module select code is needed, drop one in. If space
1708 * was reserved for one, but none is needed, recover the space.
1711 if (mod != nd->nd_tx_module) {
1712 if (b != mbuf) {
1713 mbuf[-1] = 0xf0 | mod;
1714 nd->nd_tx_module = mod;
1715 } else {
1716 b--;
1722 * Adjust "tmax" so that under worst case conditions we do
1723 * not overflow either the daemon buffer or the internal
1724 * buffer in the loop that follows. Leave a safe area
1725 * of 64 bytes so we start getting asserts before we start
1726 * losing data or clobbering memory.
1729 n = UIO_MAX - UIO_BASE;
1731 if (tmax > n)
1732 tmax = n;
1734 tmax -= 64;
1736 tsafe = tmax;
1739 * Allocate space for 5 Module Selects, 1 Sequence Request,
1740 * and 1 Set TREQ for each active channel.
1743 tmax -= 5 + 3 + 4 * nd->nd_chan_count;
1746 * Further reduce "tmax" to the available transmit credit.
1747 * Note that this is a soft constraint; The transmit credit
1748 * can go negative for a time and then recover.
1751 n = nd->nd_tx_deposit - nd->nd_tx_charge - nd->nd_link.lk_header_size;
1753 if (tmax > n)
1754 tmax = n;
1757 * Finally reduce tmax by the number of bytes already in
1758 * the buffer.
1761 tmax -= b - buf;
1764 * Suspend data transmit unless every ready channel can send
1765 * at least 1 character.
1767 if (tmax < 2 * nd->nd_chan_count) {
1768 tsend = 1;
1770 } else if (tchan > 1 && ttotal > tmax) {
1773 * If transmit is limited by the credit budget, find the
1774 * largest number of characters we can send without driving
1775 * the credit negative.
1778 long tm = tmax;
1779 int tc = tchan;
1780 int try;
1782 tsend = tm / tc;
1784 for (try = 0; try < 3; try++) {
1785 int i;
1786 int c = 0;
1788 for (i = 0; i < tc; i++) {
1789 if (tsend < tdata[i])
1790 tdata[c++] = tdata[i];
1791 else
1792 tm -= tdata[i];
1795 if (c == tc)
1796 break;
1798 tsend = tm / c;
1800 if (c == 1)
1801 break;
1803 tc = c;
1806 tsend = tm / nd->nd_chan_count;
1808 if (tsend < 2)
1809 tsend = 1;
1811 } else {
1813 * If no budgetary constraints, or only one channel ready
1814 * to send, set the character limit to the remaining
1815 * buffer size.
1818 tsend = tmax;
1821 tsend -= (tsend <= 9) ? 1 : (tsend <= 257) ? 2 : 3;
1824 * Loop over all channels, sending queued data.
1827 port = 0;
1828 ch = nd->nd_chan;
1829 used_buffer = tmax;
1831 for (mod = 0; port < nd->nd_chan_count; mod++) {
1833 * If this is not the current module, enter a module select
1834 * code in the buffer.
1837 if (mod != nd->nd_tx_module)
1838 mbuf = ++b;
1841 * Loop to process one module.
1844 maxport = port + 16;
1846 if (maxport > nd->nd_chan_count)
1847 maxport = nd->nd_chan_count;
1849 for (; port < maxport; port++, ch++) {
1850 if (ch->ch_state != CS_READY)
1851 continue;
1853 lastport = port;
1855 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1858 * If there is data that can be sent, send it.
1861 if (n != 0 && used_buffer > 0) {
1862 t = (ch->ch_s_tsize + ch->ch_s_tpos - ch->ch_s_tin) & 0xffff;
1864 if (n > t)
1865 n = t;
1867 if (n > tsend) {
1868 work = 1;
1869 n = tsend;
1872 if (n > used_buffer) {
1873 work = 1;
1874 n = used_buffer;
1877 if (n <= 0)
1878 continue;
1881 * Create the correct size transmit header,
1882 * depending on the amount of data to transmit.
1885 if (n <= 8) {
1887 b[0] = ((n - 1) << 4) + (port & 0xf);
1888 b += 1;
1890 } else if (n <= 255) {
1892 b[0] = 0x80 + (port & 0xf);
1893 b[1] = n;
1894 b += 2;
1896 } else {
1898 b[0] = 0x90 + (port & 0xf);
1899 put_unaligned_be16(n, b + 1);
1900 b += 3;
1903 ch->ch_s_tin = (ch->ch_s_tin + n) & 0xffff;
1906 * Copy transmit data to the packet.
1909 t = TBUF_MAX - ch->ch_tout;
1911 if (n >= t) {
1912 memcpy(b, ch->ch_tbuf + ch->ch_tout, t);
1913 b += t;
1914 n -= t;
1915 used_buffer -= t;
1916 ch->ch_tout = 0;
1919 memcpy(b, ch->ch_tbuf + ch->ch_tout, n);
1920 b += n;
1921 used_buffer -= n;
1922 ch->ch_tout += n;
1923 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1927 * Wake any terminal unit process waiting in the
1928 * dgrp_write routine for low water.
1931 if (n > TBUF_LOW)
1932 continue;
1934 if ((ch->ch_flag & CH_LOW) != 0) {
1935 ch->ch_flag &= ~CH_LOW;
1936 wake_up_interruptible(&ch->ch_flag_wait);
1939 /* selwakeup tty_sel */
1940 if (ch->ch_tun.un_open_count) {
1941 struct tty_struct *tty = (ch->ch_tun.un_tty);
1943 if (waitqueue_active(&tty->write_wait))
1944 wake_up_interruptible(&tty->write_wait);
1946 tty_wakeup(tty);
1949 if (ch->ch_pun.un_open_count) {
1950 struct tty_struct *tty = (ch->ch_pun.un_tty);
1952 if (waitqueue_active(&tty->write_wait))
1953 wake_up_interruptible(&tty->write_wait);
1955 tty_wakeup(tty);
1959 * Do EMPTY processing.
1962 if (n != 0)
1963 continue;
1965 if ((ch->ch_flag & (CH_EMPTY | CH_DRAIN)) != 0 ||
1966 (ch->ch_pun.un_flag & UN_EMPTY) != 0) {
1968 * If there is still data in the server, ask the server
1969 * to notify us when its all gone.
1972 if (ch->ch_s_treq != ch->ch_s_tin) {
1973 b = set_cmd_header(b, port, 43);
1975 ch->ch_s_treq = ch->ch_s_tin;
1976 put_unaligned_be16(ch->ch_s_treq,
1978 b += 2;
1982 * If there is a thread waiting for buffer empty,
1983 * and we are truly empty, wake the thread.
1986 else if ((ch->ch_flag & CH_EMPTY) != 0 &&
1987 (ch->ch_send & RR_TX_BREAK) == 0) {
1988 ch->ch_flag &= ~CH_EMPTY;
1990 wake_up_interruptible(&ch->ch_flag_wait);
1996 * If a module select code is needed, drop one in. If space
1997 * was reserved for one, but none is needed, recover the space.
2000 if (mod != nd->nd_tx_module) {
2001 if (b != mbuf) {
2002 mbuf[-1] = 0xf0 | mod;
2003 nd->nd_tx_module = mod;
2004 } else {
2005 b--;
2011 * Send a synchronization sequence associated with the last open
2012 * channel that sent data, and remember the time when the data was
2013 * sent.
2016 in = nd->nd_seq_in;
2018 if ((send_sync || nd->nd_seq_wait[in] != 0) && lastport >= 0) {
2019 u8 *bb = b;
2022 * Attempt the use the port that really wanted the sync.
2023 * This gets around a race condition where the "lastport" is in
2024 * the middle of the close() routine, and by the time we
2025 * send this command, it will have already acked the close, and
2026 * thus not send the sync response.
2028 if (wanted_sync_port >= 0)
2029 lastport = wanted_sync_port;
2031 * Set a flag just in case the port is in the middle of a close,
2032 * it will not be permitted to actually close until we get an
2033 * sync response, and clear the flag there.
2035 ch = nd->nd_chan + lastport;
2036 ch->ch_flag |= CH_WAITING_SYNC;
2038 mod = lastport >> 4;
2040 if (mod != nd->nd_tx_module) {
2041 bb[0] = 0xf0 + mod;
2042 bb += 1;
2044 nd->nd_tx_module = mod;
2047 bb = set_cmd_header(bb, lastport, 12);
2048 *bb++ = in;
2050 nd->nd_seq_size[in] = bb - buf;
2051 nd->nd_seq_time[in] = jiffies;
2053 if (++in >= SEQ_MAX)
2054 in = 0;
2056 if (in != nd->nd_seq_out) {
2057 b = bb;
2058 nd->nd_seq_in = in;
2059 nd->nd_unack += b - buf;
2064 * If there are no open ports, a sync cannot be sent.
2065 * There is nothing left to wait for anyway, so wake any
2066 * thread waiting for an acknowledgement.
2069 else if (nd->nd_seq_wait[in] != 0) {
2070 nd->nd_seq_wait[in] = 0;
2072 wake_up_interruptible(&nd->nd_seq_wque[in]);
2076 * If there is no traffic for an interval of IDLE_MAX, then
2077 * send a single byte packet.
2080 if (b != buf) {
2081 nd->nd_tx_time = jiffies;
2082 } else if ((ulong)(jiffies - nd->nd_tx_time) >= IDLE_MAX) {
2083 *b++ = 0xf0 | nd->nd_tx_module;
2084 nd->nd_tx_time = jiffies;
2087 n = b - buf;
2089 if (n >= tsafe)
2090 pr_info("%s - n(%i) >= tsafe(%i)\n",
2091 __func__, n, tsafe);
2093 if (tsend < 0)
2094 dgrp_dump(buf, n);
2096 nd->nd_tx_work = work;
2098 return n;
2102 * dgrp_net_read()
2103 * Data to be sent TO the PortServer from the "async." half of the driver.
2105 static ssize_t dgrp_net_read(struct file *file, char __user *buf, size_t count,
2106 loff_t *ppos)
2108 struct nd_struct *nd;
2109 long n;
2110 u8 *local_buf;
2111 u8 *b;
2112 ssize_t rtn;
2115 * Get the node pointer, and quit if it doesn't exist.
2117 nd = (struct nd_struct *)(file->private_data);
2118 if (!nd)
2119 return -ENXIO;
2121 if (count < UIO_MIN)
2122 return -EINVAL;
2125 * Only one read/write operation may be in progress at
2126 * any given time.
2130 * Grab the NET lock.
2132 down(&nd->nd_net_semaphore);
2134 nd->nd_read_count++;
2136 nd->nd_tx_ready = 0;
2139 * Determine the effective size of the buffer.
2142 if (nd->nd_remain > UIO_BASE)
2143 pr_info_ratelimited("%s - nd_remain(%i) > UIO_BASE\n",
2144 __func__, nd->nd_remain);
2146 b = local_buf = nd->nd_iobuf + UIO_BASE;
2149 * Generate data according to the node state.
2152 switch (nd->nd_state) {
2154 * Initialize the connection.
2157 case NS_IDLE:
2158 if (nd->nd_mon_buf)
2159 dgrp_monitor_reset(nd);
2162 * Request a Product ID Packet.
2165 b[0] = 0xfb;
2166 b[1] = 0x01;
2167 b += 2;
2169 nd->nd_expect |= NR_IDENT;
2172 * Request a Server Capability ID Response.
2175 b[0] = 0xfb;
2176 b[1] = 0x02;
2177 b += 2;
2179 nd->nd_expect |= NR_CAPABILITY;
2182 * Request a Server VPD Response.
2185 b[0] = 0xfb;
2186 b[1] = 0x18;
2187 b += 2;
2189 nd->nd_expect |= NR_VPD;
2191 nd->nd_state = NS_WAIT_QUERY;
2192 break;
2195 * We do serious communication with the server only in
2196 * the READY state.
2199 case NS_READY:
2200 b = dgrp_send(nd, count) + local_buf;
2201 break;
2204 * Send off an error after receiving a bogus message
2205 * from the server.
2208 case NS_SEND_ERROR:
2209 n = strlen(nd->nd_error);
2211 b[0] = 0xff;
2212 b[1] = n;
2213 memcpy(b + 2, nd->nd_error, n);
2214 b += 2 + n;
2216 dgrp_net_idle(nd);
2218 * Set the active port count to zero.
2220 dgrp_chan_count(nd, 0);
2221 break;
2223 default:
2224 break;
2227 n = b - local_buf;
2229 if (n != 0) {
2230 nd->nd_send_count++;
2232 nd->nd_tx_byte += n + nd->nd_link.lk_header_size;
2233 nd->nd_tx_charge += n + nd->nd_link.lk_header_size;
2236 rtn = copy_to_user((void __user *)buf, local_buf, n);
2237 if (rtn) {
2238 rtn = -EFAULT;
2239 goto done;
2242 *ppos += n;
2244 rtn = n;
2246 if (nd->nd_mon_buf)
2247 dgrp_monitor_data(nd, RPDUMP_CLIENT, local_buf, n);
2250 * Release the NET lock.
2252 done:
2253 up(&nd->nd_net_semaphore);
2255 return rtn;
2259 * dgrp_receive() -- decode data packets received from the remote PortServer.
2260 * @nd: pointer to a node structure
2262 static void dgrp_receive(struct nd_struct *nd)
2264 struct ch_struct *ch;
2265 u8 *buf;
2266 u8 *b;
2267 u8 *dbuf;
2268 char *error;
2269 long port;
2270 long dlen;
2271 long plen;
2272 long remain;
2273 long n;
2274 long mlast;
2275 long elast;
2276 long mstat;
2277 long estat;
2279 char ID[3];
2281 nd->nd_tx_time = jiffies;
2283 ID_TO_CHAR(nd->nd_ID, ID);
2285 b = buf = nd->nd_iobuf;
2286 remain = nd->nd_remain;
2289 * Loop to process Realport protocol packets.
2292 while (remain > 0) {
2293 int n0 = b[0] >> 4;
2294 int n1 = b[0] & 0x0f;
2296 if (n0 <= 12) {
2297 port = (nd->nd_rx_module << 4) + n1;
2299 if (port >= nd->nd_chan_count) {
2300 error = "Improper Port Number";
2301 goto prot_error;
2304 ch = nd->nd_chan + port;
2305 } else {
2306 port = -1;
2307 ch = NULL;
2311 * Process by major packet type.
2314 switch (n0) {
2317 * Process 1-byte header data packet.
2320 case 0:
2321 case 1:
2322 case 2:
2323 case 3:
2324 case 4:
2325 case 5:
2326 case 6:
2327 case 7:
2328 dlen = n0 + 1;
2329 plen = dlen + 1;
2331 dbuf = b + 1;
2332 goto data;
2335 * Process 2-byte header data packet.
2338 case 8:
2339 if (remain < 3)
2340 goto done;
2342 dlen = b[1];
2343 plen = dlen + 2;
2345 dbuf = b + 2;
2346 goto data;
2349 * Process 3-byte header data packet.
2352 case 9:
2353 if (remain < 4)
2354 goto done;
2356 dlen = get_unaligned_be16(b + 1);
2357 plen = dlen + 3;
2359 dbuf = b + 3;
2362 * Common packet handling code.
2365 data:
2366 nd->nd_tx_work = 1;
2369 * Otherwise data should appear only when we are
2370 * in the CS_READY state.
2373 if (ch->ch_state < CS_READY) {
2374 error = "Data received before RWIN established";
2375 goto prot_error;
2379 * Assure that the data received is within the
2380 * allowable window.
2383 n = (ch->ch_s_rwin - ch->ch_s_rin) & 0xffff;
2385 if (dlen > n) {
2386 error = "Receive data overrun";
2387 goto prot_error;
2391 * If we received 3 or less characters,
2392 * assume it is a human typing, and set RTIME
2393 * to 10 milliseconds.
2395 * If we receive 10 or more characters,
2396 * assume its not a human typing, and set RTIME
2397 * to 100 milliseconds.
2400 if (ch->ch_edelay != DGRP_RTIME) {
2401 if (ch->ch_rtime != ch->ch_edelay) {
2402 ch->ch_rtime = ch->ch_edelay;
2403 ch->ch_flag |= CH_PARAM;
2405 } else if (dlen <= 3) {
2406 if (ch->ch_rtime != 10) {
2407 ch->ch_rtime = 10;
2408 ch->ch_flag |= CH_PARAM;
2410 } else {
2411 if (ch->ch_rtime != DGRP_RTIME) {
2412 ch->ch_rtime = DGRP_RTIME;
2413 ch->ch_flag |= CH_PARAM;
2418 * If a portion of the packet is outside the
2419 * buffer, shorten the effective length of the
2420 * data packet to be the amount of data received.
2423 if (remain < plen)
2424 dlen -= plen - remain;
2427 * Detect if receive flush is now complete.
2430 if ((ch->ch_flag & CH_RX_FLUSH) != 0 &&
2431 ((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >=
2432 ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK)) {
2433 ch->ch_flag &= ~CH_RX_FLUSH;
2437 * If we are ready to receive, move the data into
2438 * the receive buffer.
2441 ch->ch_s_rin = (ch->ch_s_rin + dlen) & 0xffff;
2443 if (ch->ch_state == CS_READY &&
2444 (ch->ch_tun.un_open_count != 0) &&
2445 (ch->ch_tun.un_flag & UN_CLOSING) == 0 &&
2446 (ch->ch_cflag & CF_CREAD) != 0 &&
2447 (ch->ch_flag & (CH_BAUD0 | CH_RX_FLUSH)) == 0 &&
2448 (ch->ch_send & RR_RX_FLUSH) == 0) {
2450 if (ch->ch_rin + dlen >= RBUF_MAX) {
2451 n = RBUF_MAX - ch->ch_rin;
2453 memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, n);
2455 ch->ch_rin = 0;
2456 dbuf += n;
2457 dlen -= n;
2460 memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, dlen);
2462 ch->ch_rin += dlen;
2466 * If we are not in fastcook mode, or
2467 * if there is a fastcook thread
2468 * waiting for data, send the data to
2469 * the line discipline.
2472 if ((ch->ch_flag & CH_FAST_READ) == 0 ||
2473 ch->ch_inwait != 0) {
2474 dgrp_input(ch);
2478 * If there is a read thread waiting
2479 * in select, and we are in fastcook
2480 * mode, wake him up.
2483 if (waitqueue_active(&ch->ch_tun.un_tty->read_wait) &&
2484 (ch->ch_flag & CH_FAST_READ) != 0)
2485 wake_up_interruptible(&ch->ch_tun.un_tty->read_wait);
2488 * Wake any thread waiting in the
2489 * fastcook loop.
2492 if ((ch->ch_flag & CH_INPUT) != 0) {
2493 ch->ch_flag &= ~CH_INPUT;
2495 wake_up_interruptible(&ch->ch_flag_wait);
2500 * Fabricate and insert a data packet header to
2501 * preced the remaining data when it comes in.
2504 if (remain < plen) {
2505 dlen = plen - remain;
2506 b = buf;
2508 b[0] = 0x90 + n1;
2509 put_unaligned_be16(dlen, b + 1);
2511 remain = 3;
2512 goto done;
2514 break;
2517 * Handle Window Sequence packets.
2520 case 10:
2521 plen = 3;
2522 if (remain < plen)
2523 goto done;
2525 nd->nd_tx_work = 1;
2528 ushort tpos = get_unaligned_be16(b + 1);
2530 ushort ack = (tpos - ch->ch_s_tpos) & 0xffff;
2531 ushort unack = (ch->ch_s_tin - ch->ch_s_tpos) & 0xffff;
2532 ushort notify = (ch->ch_s_treq - ch->ch_s_tpos) & 0xffff;
2534 if (ch->ch_state < CS_READY || ack > unack) {
2535 error = "Improper Window Sequence";
2536 goto prot_error;
2539 ch->ch_s_tpos = tpos;
2541 if (notify <= ack)
2542 ch->ch_s_treq = tpos;
2544 break;
2547 * Handle Command response packets.
2550 case 11:
2553 * RealPort engine fix - 03/11/2004
2555 * This check did not used to be here.
2557 * We were using b[1] without verifying that the data
2558 * is actually there and valid. On a split packet, it
2559 * might not be yet.
2561 * NOTE: I have never actually seen the failure happen
2562 * under Linux, but since I have seen it occur
2563 * under both Solaris and HP-UX, the assumption
2564 * is that it *could* happen here as well...
2566 if (remain < 2)
2567 goto done;
2570 switch (b[1]) {
2573 * Handle Open Response.
2576 case 11:
2577 plen = 6;
2578 if (remain < plen)
2579 goto done;
2581 nd->nd_tx_work = 1;
2584 int req = b[2];
2585 int resp = b[3];
2586 port = get_unaligned_be16(b + 4);
2588 if (port >= nd->nd_chan_count) {
2589 error = "Open channel number out of range";
2590 goto prot_error;
2593 ch = nd->nd_chan + port;
2596 * How we handle an open response depends primarily
2597 * on our current channel state.
2600 switch (ch->ch_state) {
2601 case CS_IDLE:
2604 * Handle a delayed open.
2607 if (ch->ch_otype_waiting != 0 &&
2608 req == ch->ch_otype_waiting &&
2609 resp == 0) {
2610 ch->ch_otype = req;
2611 ch->ch_otype_waiting = 0;
2612 ch->ch_state = CS_SEND_QUERY;
2613 break;
2615 goto open_error;
2617 case CS_WAIT_OPEN:
2620 * Handle the open response.
2623 if (req == ch->ch_otype) {
2624 switch (resp) {
2627 * On successful response, open the
2628 * port and proceed normally.
2631 case 0:
2632 ch->ch_state = CS_SEND_QUERY;
2633 break;
2636 * On a busy response to a persistent open,
2637 * remember that the open is pending.
2640 case 1:
2641 case 2:
2642 if (req != OTYPE_IMMEDIATE) {
2643 ch->ch_otype_waiting = req;
2644 ch->ch_state = CS_IDLE;
2645 break;
2649 * Otherwise the server open failed. If
2650 * the Unix port is open, hang it up.
2653 default:
2654 if (ch->ch_open_count != 0) {
2655 ch->ch_flag |= CH_HANGUP;
2656 dgrp_carrier(ch);
2657 ch->ch_state = CS_IDLE;
2658 break;
2661 ch->ch_open_error = resp;
2662 ch->ch_state = CS_IDLE;
2664 wake_up_interruptible(&ch->ch_flag_wait);
2666 break;
2670 * Handle delayed response arrival preceding
2671 * the open response we are waiting for.
2674 if (ch->ch_otype_waiting != 0 &&
2675 req == ch->ch_otype_waiting &&
2676 resp == 0) {
2677 ch->ch_otype = ch->ch_otype_waiting;
2678 ch->ch_otype_waiting = 0;
2679 ch->ch_state = CS_WAIT_FAIL;
2680 break;
2682 goto open_error;
2685 case CS_WAIT_FAIL:
2688 * Handle response to immediate open arriving
2689 * after a delayed open success.
2692 if (req == OTYPE_IMMEDIATE) {
2693 ch->ch_state = CS_SEND_QUERY;
2694 break;
2696 goto open_error;
2699 case CS_WAIT_CANCEL:
2701 * Handle delayed open response arriving before
2702 * the cancel response.
2705 if (req == ch->ch_otype_waiting &&
2706 resp == 0) {
2707 ch->ch_otype_waiting = 0;
2708 break;
2712 * Handle cancel response.
2715 if (req == 4 && resp == 0) {
2716 ch->ch_otype_waiting = 0;
2717 ch->ch_state = CS_IDLE;
2718 break;
2720 goto open_error;
2723 case CS_WAIT_CLOSE:
2725 * Handle a successful response to a port
2726 * close.
2729 if (req >= 3) {
2730 ch->ch_state = CS_IDLE;
2731 break;
2733 goto open_error;
2735 open_error:
2736 default:
2738 error = "Improper Open Response";
2739 goto prot_error;
2743 break;
2746 * Handle Synchronize Response.
2749 case 13:
2750 plen = 3;
2751 if (remain < plen)
2752 goto done;
2754 int seq = b[2];
2755 int s;
2758 * If channel was waiting for this sync response,
2759 * unset the flag, and wake up anyone waiting
2760 * on the event.
2762 if (ch->ch_flag & CH_WAITING_SYNC) {
2763 ch->ch_flag &= ~(CH_WAITING_SYNC);
2764 wake_up_interruptible(&ch->ch_flag_wait);
2767 if (((seq - nd->nd_seq_out) & SEQ_MASK) >=
2768 ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK)) {
2769 break;
2772 for (s = nd->nd_seq_out;; s = (s + 1) & SEQ_MASK) {
2773 if (nd->nd_seq_wait[s] != 0) {
2774 nd->nd_seq_wait[s] = 0;
2776 wake_up_interruptible(&nd->nd_seq_wque[s]);
2779 nd->nd_unack -= nd->nd_seq_size[s];
2781 if (s == seq)
2782 break;
2785 nd->nd_seq_out = (seq + 1) & SEQ_MASK;
2787 break;
2790 * Handle Sequence Response.
2793 case 15:
2794 plen = 6;
2795 if (remain < plen)
2796 goto done;
2799 /* Record that we have received the Sequence
2800 * Response, but we aren't interested in the
2801 * sequence numbers. We were using RIN like it
2802 * was ROUT and that was causing problems,
2803 * fixed 7-13-2001 David Fries. See comment in
2804 * drp.h for ch_s_rin variable.
2805 int rin = get_unaligned_be16(b + 2);
2806 int tpos = get_unaligned_be16(b + 4);
2809 ch->ch_send &= ~RR_SEQUENCE;
2810 ch->ch_expect &= ~RR_SEQUENCE;
2812 goto check_query;
2815 * Handle Status Response.
2818 case 17:
2819 plen = 5;
2820 if (remain < plen)
2821 goto done;
2824 ch->ch_s_elast = get_unaligned_be16(b + 2);
2825 ch->ch_s_mlast = b[4];
2827 ch->ch_expect &= ~RR_STATUS;
2828 ch->ch_send &= ~RR_STATUS;
2831 * CH_PHYS_CD is cleared because something _could_ be
2832 * waiting for the initial sense of carrier... and if
2833 * carrier is high immediately, we want to be sure to
2834 * wake them as soon as possible.
2836 ch->ch_flag &= ~CH_PHYS_CD;
2838 dgrp_carrier(ch);
2840 goto check_query;
2843 * Handle Line Error Response.
2846 case 19:
2847 plen = 14;
2848 if (remain < plen)
2849 goto done;
2851 break;
2854 * Handle Buffer Response.
2857 case 21:
2858 plen = 6;
2859 if (remain < plen)
2860 goto done;
2863 ch->ch_s_rsize = get_unaligned_be16(b + 2);
2864 ch->ch_s_tsize = get_unaligned_be16(b + 4);
2866 ch->ch_send &= ~RR_BUFFER;
2867 ch->ch_expect &= ~RR_BUFFER;
2869 goto check_query;
2872 * Handle Port Capability Response.
2875 case 23:
2876 plen = 32;
2877 if (remain < plen)
2878 goto done;
2881 ch->ch_send &= ~RR_CAPABILITY;
2882 ch->ch_expect &= ~RR_CAPABILITY;
2886 * When all queries are complete, set those parameters
2887 * derived from the query results, then transition
2888 * to the READY state.
2891 check_query:
2892 if (ch->ch_state == CS_WAIT_QUERY &&
2893 (ch->ch_expect & (RR_SEQUENCE |
2894 RR_STATUS |
2895 RR_BUFFER |
2896 RR_CAPABILITY)) == 0) {
2897 ch->ch_tmax = ch->ch_s_tsize / 4;
2899 if (ch->ch_edelay == DGRP_TTIME)
2900 ch->ch_ttime = DGRP_TTIME;
2901 else
2902 ch->ch_ttime = ch->ch_edelay;
2904 ch->ch_rmax = ch->ch_s_rsize / 4;
2906 if (ch->ch_edelay == DGRP_RTIME)
2907 ch->ch_rtime = DGRP_RTIME;
2908 else
2909 ch->ch_rtime = ch->ch_edelay;
2911 ch->ch_rlow = 2 * ch->ch_s_rsize / 8;
2912 ch->ch_rhigh = 6 * ch->ch_s_rsize / 8;
2914 ch->ch_state = CS_READY;
2916 nd->nd_tx_work = 1;
2917 wake_up_interruptible(&ch->ch_flag_wait);
2920 break;
2922 default:
2923 goto decode_error;
2925 break;
2928 * Handle Events.
2931 case 12:
2932 plen = 4;
2933 if (remain < plen)
2934 goto done;
2936 mlast = ch->ch_s_mlast;
2937 elast = ch->ch_s_elast;
2939 mstat = ch->ch_s_mlast = b[1];
2940 estat = ch->ch_s_elast = get_unaligned_be16(b + 2);
2943 * Handle modem changes.
2946 if (((mstat ^ mlast) & DM_CD) != 0)
2947 dgrp_carrier(ch);
2951 * Handle received break.
2954 if ((estat & ~elast & EV_RXB) != 0 &&
2955 (ch->ch_tun.un_open_count != 0) &&
2956 I_BRKINT(ch->ch_tun.un_tty) &&
2957 !(I_IGNBRK(ch->ch_tun.un_tty))) {
2959 tty_buffer_request_room(ch->ch_tun.un_tty, 1);
2960 tty_insert_flip_char(ch->ch_tun.un_tty, 0, TTY_BREAK);
2961 tty_flip_buffer_push(ch->ch_tun.un_tty);
2966 * On transmit break complete, if more break traffic
2967 * is waiting then send it. Otherwise wake any threads
2968 * waiting for transmitter empty.
2971 if ((~estat & elast & EV_TXB) != 0 &&
2972 (ch->ch_expect & RR_TX_BREAK) != 0) {
2974 nd->nd_tx_work = 1;
2976 ch->ch_expect &= ~RR_TX_BREAK;
2978 if (ch->ch_break_time != 0) {
2979 ch->ch_send |= RR_TX_BREAK;
2980 } else {
2981 ch->ch_send &= ~RR_TX_BREAK;
2982 ch->ch_flag &= ~CH_TX_BREAK;
2983 wake_up_interruptible(&ch->ch_flag_wait);
2986 break;
2988 case 13:
2989 case 14:
2990 error = "Unrecognized command";
2991 goto prot_error;
2994 * Decode Special Codes.
2997 case 15:
2998 switch (n1) {
3000 * One byte module select.
3003 case 0:
3004 case 1:
3005 case 2:
3006 case 3:
3007 case 4:
3008 case 5:
3009 case 6:
3010 case 7:
3011 plen = 1;
3012 nd->nd_rx_module = n1;
3013 break;
3016 * Two byte module select.
3019 case 8:
3020 plen = 2;
3021 if (remain < plen)
3022 goto done;
3024 nd->nd_rx_module = b[1];
3025 break;
3028 * ID Request packet.
3031 case 11:
3032 if (remain < 4)
3033 goto done;
3035 plen = get_unaligned_be16(b + 2);
3037 if (plen < 12 || plen > 1000) {
3038 error = "Response Packet length error";
3039 goto prot_error;
3042 nd->nd_tx_work = 1;
3044 switch (b[1]) {
3046 * Echo packet.
3049 case 0:
3050 nd->nd_send |= NR_ECHO;
3051 break;
3054 * ID Response packet.
3057 case 1:
3058 nd->nd_send |= NR_IDENT;
3059 break;
3062 * ID Response packet.
3065 case 32:
3066 nd->nd_send |= NR_PASSWORD;
3067 break;
3070 break;
3073 * Various node-level response packets.
3076 case 12:
3077 if (remain < 4)
3078 goto done;
3080 plen = get_unaligned_be16(b + 2);
3082 if (plen < 4 || plen > 1000) {
3083 error = "Response Packet length error";
3084 goto prot_error;
3087 nd->nd_tx_work = 1;
3089 switch (b[1]) {
3091 * Echo packet.
3094 case 0:
3095 nd->nd_expect &= ~NR_ECHO;
3096 break;
3099 * Product Response Packet.
3102 case 1:
3104 int desclen;
3106 nd->nd_hw_ver = (b[8] << 8) | b[9];
3107 nd->nd_sw_ver = (b[10] << 8) | b[11];
3108 nd->nd_hw_id = b[6];
3109 desclen = ((plen - 12) > MAX_DESC_LEN) ? MAX_DESC_LEN :
3110 plen - 12;
3112 if (desclen <= 0) {
3113 error = "Response Packet desclen error";
3114 goto prot_error;
3117 strncpy(nd->nd_ps_desc, b + 12, desclen);
3118 nd->nd_ps_desc[desclen] = 0;
3121 nd->nd_expect &= ~NR_IDENT;
3122 break;
3125 * Capability Response Packet.
3128 case 2:
3130 int nn = get_unaligned_be16(b + 4);
3132 if (nn > CHAN_MAX)
3133 nn = CHAN_MAX;
3135 dgrp_chan_count(nd, nn);
3138 nd->nd_expect &= ~NR_CAPABILITY;
3139 break;
3142 * VPD Response Packet.
3145 case 15:
3147 * NOTE: case 15 is here ONLY because the EtherLite
3148 * is broken, and sends a response to 24 back as 15.
3149 * To resolve this, the EtherLite firmware is now
3150 * fixed to send back 24 correctly, but, for backwards
3151 * compatibility, we now have reserved 15 for the
3152 * bad EtherLite response to 24 as well.
3155 /* Fallthru! */
3157 case 24:
3160 * If the product doesn't support VPD,
3161 * it will send back a null IDRESP,
3162 * which is a length of 4 bytes.
3164 if (plen > 4) {
3165 memcpy(nd->nd_vpd, b + 4, min(plen - 4, (long) VPDSIZE));
3166 nd->nd_vpd_len = min(plen - 4, (long) VPDSIZE);
3169 nd->nd_expect &= ~NR_VPD;
3170 break;
3172 default:
3173 goto decode_error;
3176 if (nd->nd_expect == 0 &&
3177 nd->nd_state == NS_WAIT_QUERY) {
3178 nd->nd_state = NS_READY;
3180 break;
3183 * Debug packet.
3186 case 14:
3187 if (remain < 4)
3188 goto done;
3190 plen = get_unaligned_be16(b + 2) + 4;
3192 if (plen > 1000) {
3193 error = "Debug Packet too large";
3194 goto prot_error;
3197 if (remain < plen)
3198 goto done;
3199 break;
3202 * Handle reset packet.
3205 case 15:
3206 if (remain < 2)
3207 goto done;
3209 plen = 2 + b[1];
3211 if (remain < plen)
3212 goto done;
3214 nd->nd_tx_work = 1;
3216 n = b[plen];
3217 b[plen] = 0;
3219 b[plen] = n;
3221 error = "Client Reset Acknowledge";
3222 goto prot_error;
3224 default:
3225 goto decode_error;
3227 break;
3229 default:
3230 goto decode_error;
3233 b += plen;
3234 remain -= plen;
3238 * When the buffer is exhausted, copy any data left at the
3239 * top of the buffer back down to the bottom for the next
3240 * read request.
3243 done:
3244 if (remain > 0 && b != buf)
3245 memcpy(buf, b, remain);
3247 nd->nd_remain = remain;
3248 return;
3251 * Handle a decode error.
3254 decode_error:
3255 error = "Protocol decode error";
3258 * Handle a general protocol error.
3261 prot_error:
3262 nd->nd_remain = 0;
3263 nd->nd_state = NS_SEND_ERROR;
3264 nd->nd_error = error;
3268 * dgrp_net_write() -- write data to the network device.
3270 * A zero byte write indicates that the connection to the RealPort
3271 * device has been broken.
3273 * A non-zero write indicates data from the RealPort device.
3275 static ssize_t dgrp_net_write(struct file *file, const char __user *buf,
3276 size_t count, loff_t *ppos)
3278 struct nd_struct *nd;
3279 ssize_t rtn = 0;
3280 long n;
3281 long total = 0;
3284 * Get the node pointer, and quit if it doesn't exist.
3286 nd = (struct nd_struct *)(file->private_data);
3287 if (!nd)
3288 return -ENXIO;
3291 * Grab the NET lock.
3293 down(&nd->nd_net_semaphore);
3295 nd->nd_write_count++;
3298 * Handle disconnect.
3301 if (count == 0) {
3302 dgrp_net_idle(nd);
3304 * Set the active port count to zero.
3306 dgrp_chan_count(nd, 0);
3307 goto unlock;
3311 * Loop to process entire receive packet.
3314 while (count > 0) {
3315 n = UIO_MAX - nd->nd_remain;
3317 if (n > count)
3318 n = count;
3320 nd->nd_rx_byte += n + nd->nd_link.lk_header_size;
3322 rtn = copy_from_user(nd->nd_iobuf + nd->nd_remain,
3323 (void __user *) buf + total, n);
3324 if (rtn) {
3325 rtn = -EFAULT;
3326 goto unlock;
3329 *ppos += n;
3331 total += n;
3333 count -= n;
3335 if (nd->nd_mon_buf)
3336 dgrp_monitor_data(nd, RPDUMP_SERVER,
3337 nd->nd_iobuf + nd->nd_remain, n);
3339 nd->nd_remain += n;
3341 dgrp_receive(nd);
3344 rtn = total;
3346 unlock:
3348 * Release the NET lock.
3350 up(&nd->nd_net_semaphore);
3352 return rtn;
3357 * dgrp_net_select()
3358 * Determine whether a device is ready to be read or written to, and
3359 * sleep if not.
3361 static unsigned int dgrp_net_select(struct file *file,
3362 struct poll_table_struct *table)
3364 unsigned int retval = 0;
3365 struct nd_struct *nd = file->private_data;
3367 poll_wait(file, &nd->nd_tx_waitq, table);
3369 if (nd->nd_tx_ready)
3370 retval |= POLLIN | POLLRDNORM; /* Conditionally readable */
3372 retval |= POLLOUT | POLLWRNORM; /* Always writeable */
3374 return retval;
3378 * dgrp_net_ioctl
3380 * Implement those functions which allow the network daemon to control
3381 * the network parameters in the driver. The ioctls include ones to
3382 * get and set the link speed parameters for the PortServer.
3384 static long dgrp_net_ioctl(struct file *file, unsigned int cmd,
3385 unsigned long arg)
3387 struct nd_struct *nd;
3388 int rtn = 0;
3389 long size = _IOC_SIZE(cmd);
3390 struct link_struct link;
3392 nd = file->private_data;
3394 if (_IOC_DIR(cmd) & _IOC_READ)
3395 rtn = access_ok(VERIFY_WRITE, (void __user *) arg, size);
3396 else if (_IOC_DIR(cmd) & _IOC_WRITE)
3397 rtn = access_ok(VERIFY_READ, (void __user *) arg, size);
3399 if (!rtn)
3400 return rtn;
3402 switch (cmd) {
3403 case DIGI_SETLINK:
3404 if (size != sizeof(struct link_struct))
3405 return -EINVAL;
3407 if (copy_from_user((void *)(&link), (void __user *) arg, size))
3408 return -EFAULT;
3410 if (link.lk_fast_rate < 9600)
3411 link.lk_fast_rate = 9600;
3413 if (link.lk_slow_rate < 2400)
3414 link.lk_slow_rate = 2400;
3416 if (link.lk_fast_rate > 10000000)
3417 link.lk_fast_rate = 10000000;
3419 if (link.lk_slow_rate > link.lk_fast_rate)
3420 link.lk_slow_rate = link.lk_fast_rate;
3422 if (link.lk_fast_delay > 2000)
3423 link.lk_fast_delay = 2000;
3425 if (link.lk_slow_delay > 10000)
3426 link.lk_slow_delay = 10000;
3428 if (link.lk_fast_delay < 60)
3429 link.lk_fast_delay = 60;
3431 if (link.lk_slow_delay < link.lk_fast_delay)
3432 link.lk_slow_delay = link.lk_fast_delay;
3434 if (link.lk_header_size < 2)
3435 link.lk_header_size = 2;
3437 if (link.lk_header_size > 128)
3438 link.lk_header_size = 128;
3440 link.lk_fast_rate /= 8 * 1000 / dgrp_poll_tick;
3441 link.lk_slow_rate /= 8 * 1000 / dgrp_poll_tick;
3443 link.lk_fast_delay /= dgrp_poll_tick;
3444 link.lk_slow_delay /= dgrp_poll_tick;
3446 nd->nd_link = link;
3448 break;
3450 case DIGI_GETLINK:
3451 if (size != sizeof(struct link_struct))
3452 return -EINVAL;
3454 if (copy_to_user((void __user *)arg, (void *)(&nd->nd_link),
3455 size))
3456 return -EFAULT;
3458 break;
3460 default:
3461 return -EINVAL;
3465 return 0;
3469 * dgrp_poll_handler() -- handler for poll timer
3471 * As each timer expires, it determines (a) whether the "transmit"
3472 * waiter needs to be woken up, and (b) whether the poller needs to
3473 * be rescheduled.
3475 void dgrp_poll_handler(unsigned long arg)
3477 struct dgrp_poll_data *poll_data;
3478 struct nd_struct *nd;
3479 struct link_struct *lk;
3480 ulong time;
3481 ulong poll_time;
3482 ulong freq;
3483 ulong lock_flags;
3485 poll_data = (struct dgrp_poll_data *) arg;
3486 freq = 1000 / poll_data->poll_tick;
3487 poll_data->poll_round += 17;
3489 if (poll_data->poll_round >= freq)
3490 poll_data->poll_round -= freq;
3493 * Loop to process all open nodes.
3495 * For each node, determine the rate at which it should
3496 * be transmitting data. Then if the node should wake up
3497 * and transmit data now, enable the net receive select
3498 * to get the transmit going.
3501 list_for_each_entry(nd, &nd_struct_list, list) {
3503 lk = &nd->nd_link;
3506 * Decrement statistics. These are only for use with
3507 * KME, so don't worry that the operations are done
3508 * unlocked, and so the results are occasionally wrong.
3511 nd->nd_read_count -= (nd->nd_read_count +
3512 poll_data->poll_round) / freq;
3513 nd->nd_write_count -= (nd->nd_write_count +
3514 poll_data->poll_round) / freq;
3515 nd->nd_send_count -= (nd->nd_send_count +
3516 poll_data->poll_round) / freq;
3517 nd->nd_tx_byte -= (nd->nd_tx_byte +
3518 poll_data->poll_round) / freq;
3519 nd->nd_rx_byte -= (nd->nd_rx_byte +
3520 poll_data->poll_round) / freq;
3523 * Wake the daemon to transmit data only when there is
3524 * enough byte credit to send data.
3526 * The results are approximate because the operations
3527 * are performed unlocked, and we are inspecting
3528 * data asynchronously updated elsewhere. The whole
3529 * thing is just approximation anyway, so that should
3530 * be okay.
3533 if (lk->lk_slow_rate >= UIO_MAX) {
3535 nd->nd_delay = 0;
3536 nd->nd_rate = UIO_MAX;
3538 nd->nd_tx_deposit = nd->nd_tx_charge + 3 * UIO_MAX;
3539 nd->nd_tx_credit = 3 * UIO_MAX;
3541 } else {
3543 long rate;
3544 long delay;
3545 long deposit;
3546 long charge;
3547 long size;
3548 long excess;
3550 long seq_in = nd->nd_seq_in;
3551 long seq_out = nd->nd_seq_out;
3554 * If there are no outstanding packets, run at the
3555 * fastest rate.
3558 if (seq_in == seq_out) {
3559 delay = 0;
3560 rate = lk->lk_fast_rate;
3564 * Otherwise compute the transmit rate based on the
3565 * delay since the oldest packet.
3568 else {
3570 * The actual delay is computed as the
3571 * time since the oldest unacknowledged
3572 * packet was sent, minus the time it
3573 * took to send that packet to the server.
3576 delay = ((jiffies - nd->nd_seq_time[seq_out])
3577 - (nd->nd_seq_size[seq_out] /
3578 lk->lk_fast_rate));
3581 * If the delay is less than the "fast"
3582 * delay, transmit full speed. If greater
3583 * than the "slow" delay, transmit at the
3584 * "slow" speed. In between, interpolate
3585 * between the fast and slow speeds.
3588 rate =
3589 (delay <= lk->lk_fast_delay ?
3590 lk->lk_fast_rate :
3591 delay >= lk->lk_slow_delay ?
3592 lk->lk_slow_rate :
3593 (lk->lk_slow_rate +
3594 (lk->lk_slow_delay - delay) *
3595 (lk->lk_fast_rate - lk->lk_slow_rate) /
3596 (lk->lk_slow_delay - lk->lk_fast_delay)
3601 nd->nd_delay = delay;
3602 nd->nd_rate = rate;
3605 * Increase the transmit credit by depositing the
3606 * current transmit rate.
3609 deposit = nd->nd_tx_deposit;
3610 charge = nd->nd_tx_charge;
3612 deposit += rate;
3615 * If the available transmit credit becomes too large,
3616 * reduce the deposit to correct the value.
3618 * Too large is the max of:
3619 * 6 times the header size
3620 * 3 times the current transmit rate.
3623 size = 2 * nd->nd_link.lk_header_size;
3625 if (size < rate)
3626 size = rate;
3628 size *= 3;
3630 excess = deposit - charge - size;
3632 if (excess > 0)
3633 deposit -= excess;
3635 nd->nd_tx_deposit = deposit;
3636 nd->nd_tx_credit = deposit - charge;
3639 * Wake the transmit task only if the transmit credit
3640 * is at least 3 times the transmit header size.
3643 size = 3 * lk->lk_header_size;
3645 if (nd->nd_tx_credit < size)
3646 continue;
3651 * Enable the READ select to wake the daemon if there
3652 * is useful work for the drp_read routine to perform.
3655 if (waitqueue_active(&nd->nd_tx_waitq) &&
3656 (nd->nd_tx_work != 0 ||
3657 (ulong)(jiffies - nd->nd_tx_time) >= IDLE_MAX)) {
3658 nd->nd_tx_ready = 1;
3660 wake_up_interruptible(&nd->nd_tx_waitq);
3662 /* not needed */
3663 /* nd->nd_flag &= ~ND_SELECT; */
3669 * Schedule ourself back at the nominal wakeup interval.
3671 spin_lock_irqsave(&poll_data->poll_lock, lock_flags);
3673 poll_data->node_active_count--;
3674 if (poll_data->node_active_count > 0) {
3675 poll_data->node_active_count++;
3676 poll_time = poll_data->timer.expires +
3677 poll_data->poll_tick * HZ / 1000;
3679 time = poll_time - jiffies;
3681 if (time >= 2 * poll_data->poll_tick)
3682 poll_time = jiffies + dgrp_poll_tick * HZ / 1000;
3684 poll_data->timer.expires = poll_time;
3685 add_timer(&poll_data->timer);
3688 spin_unlock_irqrestore(&poll_data->poll_lock, lock_flags);