]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/dgrp/dgrp_net_ops.c
dgrp procfs fixes, part 5: per-node files
[karo-tx-linux.git] / drivers / staging / dgrp / dgrp_net_ops.c
1 /*
2  *
3  * Copyright 1999 Digi International (www.digi.com)
4  *     James Puzzo  <jamesp at digi dot com>
5  *
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.
10  *
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.
15  *
16  */
17
18 /*
19  *
20  *  Filename:
21  *
22  *     dgrp_net_ops.c
23  *
24  *  Description:
25  *
26  *     Handle the file operations required for the "network" devices.
27  *     Includes those functions required to register the "net" devices
28  *     in "/proc".
29  *
30  *  Author:
31  *
32  *     James A. Puzzo
33  *
34  */
35
36 #include <linux/module.h>
37 #include <linux/proc_fs.h>
38 #include <linux/types.h>
39 #include <linux/string.h>
40 #include <linux/device.h>
41 #include <linux/tty.h>
42 #include <linux/tty_flip.h>
43 #include <linux/spinlock.h>
44 #include <linux/poll.h>
45 #include <linux/sched.h>
46 #include <linux/ratelimit.h>
47 #include <asm/unaligned.h>
48
49 #define MYFLIPLEN       TBUF_MAX
50
51 #include "dgrp_common.h"
52
53 #define TTY_FLIPBUF_SIZE 512
54 #define DEVICE_NAME_SIZE 50
55
56 /*
57  *  Generic helper function declarations
58  */
59 static void   parity_scan(struct ch_struct *ch, unsigned char *cbuf,
60                                 unsigned char *fbuf, int *len);
61
62 /*
63  *  File operation declarations
64  */
65 static int dgrp_net_open(struct inode *, struct file *);
66 static int dgrp_net_release(struct inode *, struct file *);
67 static ssize_t dgrp_net_read(struct file *, char __user *, size_t, loff_t *);
68 static ssize_t dgrp_net_write(struct file *, const char __user *, size_t,
69                               loff_t *);
70 static long dgrp_net_ioctl(struct file *file, unsigned int cmd,
71                            unsigned long arg);
72 static unsigned int dgrp_net_select(struct file *file,
73                                     struct poll_table_struct *table);
74
75 const struct file_operations dgrp_net_ops = {
76         .owner   =  THIS_MODULE,
77         .read    =  dgrp_net_read,
78         .write   =  dgrp_net_write,
79         .poll    =  dgrp_net_select,
80         .unlocked_ioctl =  dgrp_net_ioctl,
81         .open    =  dgrp_net_open,
82         .release =  dgrp_net_release,
83 };
84
85 /**
86  * dgrp_dump() -- prints memory for debugging purposes.
87  * @mem: Memory location which should be printed to the console
88  * @len: Number of bytes to be dumped
89  */
90 static void dgrp_dump(u8 *mem, int len)
91 {
92         int i;
93
94         pr_debug("dgrp dump length = %d, data = ", len);
95         for (i = 0; i < len; ++i)
96                 pr_debug("%.2x ", mem[i]);
97         pr_debug("\n");
98 }
99
100 /**
101  * dgrp_read_data_block() -- Read a data block
102  * @ch: struct ch_struct *
103  * @flipbuf: u8 *
104  * @flipbuf_size: size of flipbuf
105  */
106 static void dgrp_read_data_block(struct ch_struct *ch, u8 *flipbuf,
107                                  int flipbuf_size)
108 {
109         int t;
110         int n;
111
112         if (flipbuf_size <= 0)
113                 return;
114
115         t = RBUF_MAX - ch->ch_rout;
116         n = flipbuf_size;
117
118         if (n >= t) {
119                 memcpy(flipbuf, ch->ch_rbuf + ch->ch_rout, t);
120                 flipbuf += t;
121                 n -= t;
122                 ch->ch_rout = 0;
123         }
124
125         memcpy(flipbuf, ch->ch_rbuf + ch->ch_rout, n);
126         flipbuf += n;
127         ch->ch_rout += n;
128 }
129
130
131 /**
132  * dgrp_input() -- send data to the line disipline
133  * @ch: pointer to channel struct
134  *
135  * Copys the rbuf to the flipbuf and sends to line discipline.
136  * Sends input buffer data to the line discipline.
137  *
138  */
139 static void dgrp_input(struct ch_struct *ch)
140 {
141         struct nd_struct *nd;
142         struct tty_struct *tty;
143         int data_len;
144         int len;
145         int tty_count;
146         ulong lock_flags;
147         u8  *myflipbuf;
148         u8  *myflipflagbuf;
149
150         if (!ch)
151                 return;
152
153         nd = ch->ch_nd;
154
155         if (!nd)
156                 return;
157
158         spin_lock_irqsave(&nd->nd_lock, lock_flags);
159
160         myflipbuf = nd->nd_inputbuf;
161         myflipflagbuf = nd->nd_inputflagbuf;
162
163         if (!ch->ch_open_count) {
164                 ch->ch_rout = ch->ch_rin;
165                 goto out;
166         }
167
168         if (ch->ch_tun.un_flag & UN_CLOSING) {
169                 ch->ch_rout = ch->ch_rin;
170                 goto out;
171         }
172
173         tty = (ch->ch_tun).un_tty;
174
175
176         if (!tty || tty->magic != TTY_MAGIC) {
177                 ch->ch_rout = ch->ch_rin;
178                 goto out;
179         }
180
181         tty_count = tty->count;
182         if (!tty_count) {
183                 ch->ch_rout = ch->ch_rin;
184                 goto out;
185         }
186
187         if (tty->closing || test_bit(TTY_CLOSING, &tty->flags)) {
188                 ch->ch_rout = ch->ch_rin;
189                 goto out;
190         }
191
192         spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
193
194         /* data_len should be the number of chars that we read in */
195         data_len = (ch->ch_rin - ch->ch_rout) & RBUF_MASK;
196
197         /* len is the amount of data we are going to transfer here */
198         len = tty_buffer_request_room(&ch->port, data_len);
199
200         /* Check DPA flow control */
201         if ((nd->nd_dpa_debug) &&
202             (nd->nd_dpa_flag & DPA_WAIT_SPACE) &&
203             (nd->nd_dpa_port == MINOR(tty_devnum(ch->ch_tun.un_tty))))
204                 len = 0;
205
206         if ((len) && !(ch->ch_flag & CH_RXSTOP)) {
207
208                 dgrp_read_data_block(ch, myflipbuf, len);
209
210                 if (I_PARMRK(tty) || I_BRKINT(tty) || I_INPCK(tty))
211                         parity_scan(ch, myflipbuf, myflipflagbuf, &len);
212                 else
213                         memset(myflipflagbuf, TTY_NORMAL, len);
214
215                 if ((nd->nd_dpa_debug) &&
216                     (nd->nd_dpa_port == PORT_NUM(MINOR(tty_devnum(tty)))))
217                         dgrp_dpa_data(nd, 1, myflipbuf, len);
218
219                 tty_insert_flip_string_flags(&ch->port, myflipbuf,
220                                              myflipflagbuf, len);
221                 tty_flip_buffer_push(&ch->port);
222
223                 ch->ch_rxcount += len;
224         }
225
226         /*
227          * Wake up any sleepers (maybe dgrp close) that might be waiting
228          * for a channel flag state change.
229          */
230         wake_up_interruptible(&ch->ch_flag_wait);
231         return;
232
233 out:
234         spin_unlock_irqrestore(&nd->nd_lock, lock_flags);
235 }
236
237
238 /*
239  *  parity_scan
240  *
241  *  Loop to inspect each single character or 0xFF escape.
242  *
243  *  if PARMRK & ~DOSMODE:
244  *     0xFF  0xFF           Normal 0xFF character, escaped
245  *                          to eliminate confusion.
246  *     0xFF  0x00  0x00     Break
247  *     0xFF  0x00  CC       Error character CC.
248  *     CC                   Normal character CC.
249  *
250  *  if PARMRK & DOSMODE:
251  *     0xFF  0x18  0x00     Break
252  *     0xFF  0x08  0x00     Framing Error
253  *     0xFF  0x04  0x00     Parity error
254  *     0xFF  0x0C  0x00     Both Framing and Parity error
255  *
256  *  TODO:  do we need to do the XMODEM, XOFF, XON, XANY processing??
257  *         as per protocol
258  */
259 static void parity_scan(struct ch_struct *ch, unsigned char *cbuf,
260                         unsigned char *fbuf, int *len)
261 {
262         int l = *len;
263         int count = 0;
264         int DOS = ((ch->ch_iflag & IF_DOSMODE) == 0 ? 0 : 1);
265         unsigned char *cout; /* character buffer */
266         unsigned char *fout; /* flag buffer */
267         unsigned char *in;
268         unsigned char c;
269
270         in = cbuf;
271         cout = cbuf;
272         fout = fbuf;
273
274         while (l--) {
275                 c = *in;
276                 in++;
277
278                 switch (ch->ch_pscan_state) {
279                 default:
280                         /* reset to sanity and fall through */
281                         ch->ch_pscan_state = 0 ;
282
283                 case 0:
284                         /* No FF seen yet */
285                         if (c == 0xff) /* delete this character from stream */
286                                 ch->ch_pscan_state = 1;
287                         else {
288                                 *cout++ = c;
289                                 *fout++ = TTY_NORMAL;
290                                 count += 1;
291                         }
292                         break;
293
294                 case 1:
295                         /* first FF seen */
296                         if (c == 0xff) {
297                                 /* doubled ff, transform to single ff */
298                                 *cout++ = c;
299                                 *fout++ = TTY_NORMAL;
300                                 count += 1;
301                                 ch->ch_pscan_state = 0;
302                         } else {
303                                 /* save value examination in next state */
304                                 ch->ch_pscan_savechar = c;
305                                 ch->ch_pscan_state = 2;
306                         }
307                         break;
308
309                 case 2:
310                         /* third character of ff sequence */
311                         *cout++ = c;
312                         if (DOS) {
313                                 if (ch->ch_pscan_savechar & 0x10)
314                                         *fout++ = TTY_BREAK;
315                                 else if (ch->ch_pscan_savechar & 0x08)
316                                         *fout++ = TTY_FRAME;
317                                 else
318                                         /*
319                                          * either marked as a parity error,
320                                          * indeterminate, or not in DOSMODE
321                                          * call it a parity error
322                                          */
323                                         *fout++ = TTY_PARITY;
324                         } else {
325                                 /* case FF XX ?? where XX is not 00 */
326                                 if (ch->ch_pscan_savechar & 0xff) {
327                                         /* this should not happen */
328                                         pr_info("%s: parity_scan: error unexpected byte\n",
329                                                 __func__);
330                                         *fout++ = TTY_PARITY;
331                                 }
332                                 /* case FF 00 XX where XX is not 00 */
333                                 else if (c == 0xff)
334                                         *fout++ = TTY_PARITY;
335                                 /* case FF 00 00 */
336                                 else
337                                         *fout++ = TTY_BREAK;
338
339                         }
340                         count += 1;
341                         ch->ch_pscan_state = 0;
342                 }
343         }
344         *len = count;
345 }
346
347
348 /**
349  * dgrp_net_idle() -- Idle the network connection
350  * @nd: pointer to node structure to idle
351  */
352 static void dgrp_net_idle(struct nd_struct *nd)
353 {
354         struct ch_struct *ch;
355         int i;
356
357         nd->nd_tx_work = 1;
358
359         nd->nd_state = NS_IDLE;
360         nd->nd_flag = 0;
361
362         for (i = nd->nd_seq_out; ; i = (i + 1) & SEQ_MASK) {
363                 if (!nd->nd_seq_wait[i]) {
364                         nd->nd_seq_wait[i] = 0;
365                         wake_up_interruptible(&nd->nd_seq_wque[i]);
366                 }
367
368                 if (i == nd->nd_seq_in)
369                         break;
370         }
371
372         nd->nd_seq_out = nd->nd_seq_in;
373
374         nd->nd_unack = 0;
375         nd->nd_remain = 0;
376
377         nd->nd_tx_module = 0x10;
378         nd->nd_rx_module = 0x00;
379
380         for (i = 0, ch = nd->nd_chan; i < CHAN_MAX; i++, ch++) {
381                 ch->ch_state = CS_IDLE;
382
383                 ch->ch_otype = 0;
384                 ch->ch_otype_waiting = 0;
385         }
386 }
387
388 /*
389  *  Increase the number of channels, waking up any
390  *  threads that might be waiting for the channels
391  *  to appear.
392  */
393 static void increase_channel_count(struct nd_struct *nd, int n)
394 {
395         struct ch_struct *ch;
396         struct device *classp;
397         char name[DEVICE_NAME_SIZE];
398         int ret;
399         u8 *buf;
400         int i;
401
402         for (i = nd->nd_chan_count; i < n; ++i) {
403                 ch = nd->nd_chan + i;
404
405                 /* FIXME: return a useful error instead! */
406                 buf = kmalloc(TBUF_MAX, GFP_KERNEL);
407                 if (!buf)
408                         return;
409
410                 if (ch->ch_tbuf)
411                         pr_info_ratelimited("%s - ch_tbuf was not NULL\n",
412                                             __func__);
413
414                 ch->ch_tbuf = buf;
415
416                 buf = kmalloc(RBUF_MAX, GFP_KERNEL);
417                 if (!buf)
418                         return;
419
420                 if (ch->ch_rbuf)
421                         pr_info("%s - ch_rbuf was not NULL\n",
422                                 __func__);
423                 ch->ch_rbuf = buf;
424
425                 classp = tty_port_register_device(&ch->port,
426                                                   nd->nd_serial_ttdriver, i,
427                                                   NULL);
428
429                 ch->ch_tun.un_sysfs = classp;
430                 snprintf(name, DEVICE_NAME_SIZE, "tty_%d", i);
431
432                 dgrp_create_tty_sysfs(&ch->ch_tun, classp);
433                 ret = sysfs_create_link(&nd->nd_class_dev->kobj,
434                                         &classp->kobj, name);
435
436                 /* NOTE: We don't support "cu" devices anymore,
437                  * so you will notice we don't register them
438                  * here anymore. */
439                 if (dgrp_register_prdevices) {
440                         classp = tty_register_device(nd->nd_xprint_ttdriver,
441                                                      i, NULL);
442                         ch->ch_pun.un_sysfs = classp;
443                         snprintf(name, DEVICE_NAME_SIZE, "pr_%d", i);
444
445                         dgrp_create_tty_sysfs(&ch->ch_pun, classp);
446                         ret = sysfs_create_link(&nd->nd_class_dev->kobj,
447                                                 &classp->kobj, name);
448                 }
449
450                 nd->nd_chan_count = i + 1;
451                 wake_up_interruptible(&ch->ch_flag_wait);
452         }
453 }
454
455 /*
456  * Decrease the number of channels, and wake up any threads that might
457  * be waiting on the channels that vanished.
458  */
459 static void decrease_channel_count(struct nd_struct *nd, int n)
460 {
461         struct ch_struct *ch;
462         char name[DEVICE_NAME_SIZE];
463         int i;
464
465         for (i = nd->nd_chan_count - 1; i >= n; --i) {
466                 ch = nd->nd_chan + i;
467
468                 /*
469                  *  Make any open ports inoperative.
470                  */
471                 ch->ch_state = CS_IDLE;
472
473                 ch->ch_otype = 0;
474                 ch->ch_otype_waiting = 0;
475
476                 /*
477                  *  Only "HANGUP" if we care about carrier
478                  *  transitions and we are already open.
479                  */
480                 if (ch->ch_open_count != 0) {
481                         ch->ch_flag |= CH_HANGUP;
482                         dgrp_carrier(ch);
483                 }
484
485                 /*
486                  * Unlike the CH_HANGUP flag above, use another
487                  * flag to indicate to the RealPort state machine
488                  * that this port has disappeared.
489                  */
490                 if (ch->ch_open_count != 0)
491                         ch->ch_flag |= CH_PORT_GONE;
492
493                 wake_up_interruptible(&ch->ch_flag_wait);
494
495                 nd->nd_chan_count = i;
496
497                 kfree(ch->ch_tbuf);
498                 ch->ch_tbuf = NULL;
499
500                 kfree(ch->ch_rbuf);
501                 ch->ch_rbuf = NULL;
502
503                 nd->nd_chan_count = i;
504
505                 dgrp_remove_tty_sysfs(ch->ch_tun.un_sysfs);
506                 snprintf(name, DEVICE_NAME_SIZE, "tty_%d", i);
507                 sysfs_remove_link(&nd->nd_class_dev->kobj, name);
508                 tty_unregister_device(nd->nd_serial_ttdriver, i);
509
510                 /*
511                  * NOTE: We don't support "cu" devices anymore, so don't
512                  * unregister them here anymore.
513                  */
514
515                 if (dgrp_register_prdevices) {
516                         dgrp_remove_tty_sysfs(ch->ch_pun.un_sysfs);
517                         snprintf(name, DEVICE_NAME_SIZE, "pr_%d", i);
518                         sysfs_remove_link(&nd->nd_class_dev->kobj, name);
519                         tty_unregister_device(nd->nd_xprint_ttdriver, i);
520                 }
521         }
522 }
523
524 /**
525  * dgrp_chan_count() -- Adjust the node channel count.
526  * @nd: pointer to a node structure
527  * @n: new value for channel count
528  *
529  * Adjusts the node channel count.  If new ports have appeared, it tries
530  * to signal those processes that might have been waiting for ports to
531  * appear.  If ports have disappeared it tries to signal those processes
532  * that might be hung waiting for a response for the now non-existant port.
533  */
534 static void dgrp_chan_count(struct nd_struct *nd, int n)
535 {
536         if (n == nd->nd_chan_count)
537                 return;
538
539         if (n > nd->nd_chan_count)
540                 increase_channel_count(nd, n);
541
542         if (n < nd->nd_chan_count)
543                 decrease_channel_count(nd, n);
544 }
545
546 /**
547  * dgrp_monitor() -- send data to the device monitor queue
548  * @nd: pointer to a node structure
549  * @buf: data to copy to the monitoring buffer
550  * @len: number of bytes to transfer to the buffer
551  *
552  * Called by the net device routines to send data to the device
553  * monitor queue.  If the device monitor buffer is too full to
554  * accept the data, it waits until the buffer is ready.
555  */
556 static void dgrp_monitor(struct nd_struct *nd, u8 *buf, int len)
557 {
558         int n;
559         int r;
560         int rtn;
561
562         /*
563          *  Grab monitor lock.
564          */
565         down(&nd->nd_mon_semaphore);
566
567         /*
568          *  Loop while data remains.
569          */
570         while ((len > 0) && (nd->nd_mon_buf)) {
571                 /*
572                  *  Determine the amount of available space left in the
573                  *  buffer.  If there's none, wait until some appears.
574                  */
575
576                 n = (nd->nd_mon_out - nd->nd_mon_in - 1) & MON_MASK;
577
578                 if (!n) {
579                         nd->nd_mon_flag |= MON_WAIT_SPACE;
580
581                         up(&nd->nd_mon_semaphore);
582
583                         /*
584                          * Go to sleep waiting until the condition becomes true.
585                          */
586                         rtn = wait_event_interruptible(nd->nd_mon_wqueue,
587                                                        ((nd->nd_mon_flag & MON_WAIT_SPACE) == 0));
588
589 /* FIXME: really ignore rtn? */
590
591                         /*
592                          *  We can't exit here if we receive a signal, since
593                          *  to do so would trash the debug stream.
594                          */
595
596                         down(&nd->nd_mon_semaphore);
597
598                         continue;
599                 }
600
601                 /*
602                  * Copy as much data as will fit.
603                  */
604
605                 if (n > len)
606                         n = len;
607
608                 r = MON_MAX - nd->nd_mon_in;
609
610                 if (r <= n) {
611                         memcpy(nd->nd_mon_buf + nd->nd_mon_in, buf, r);
612
613                         n -= r;
614
615                         nd->nd_mon_in = 0;
616
617                         buf += r;
618                         len -= r;
619                 }
620
621                 memcpy(nd->nd_mon_buf + nd->nd_mon_in, buf, n);
622
623                 nd->nd_mon_in += n;
624
625                 buf += n;
626                 len -= n;
627
628                 if (nd->nd_mon_in >= MON_MAX)
629                         pr_info_ratelimited("%s - nd_mon_in (%i) >= MON_MAX\n",
630                                             __func__, nd->nd_mon_in);
631
632                 /*
633                  *  Wakeup any thread waiting for data
634                  */
635
636                 if (nd->nd_mon_flag & MON_WAIT_DATA) {
637                         nd->nd_mon_flag &= ~MON_WAIT_DATA;
638                         wake_up_interruptible(&nd->nd_mon_wqueue);
639                 }
640         }
641
642         /*
643          *  Release the monitor lock.
644          */
645         up(&nd->nd_mon_semaphore);
646 }
647
648 /**
649  * dgrp_encode_time() -- Encodes rpdump time into a 4-byte quantity.
650  * @nd: pointer to a node structure
651  * @buf: destination buffer
652  *
653  * Encodes "rpdump" time into a 4-byte quantity.  Time is measured since
654  * open.
655  */
656 static void dgrp_encode_time(struct nd_struct *nd, u8 *buf)
657 {
658         ulong t;
659
660         /*
661          *  Convert time in HZ since open to time in milliseconds
662          *  since open.
663          */
664         t = jiffies - nd->nd_mon_lbolt;
665         t = 1000 * (t / HZ) + 1000 * (t % HZ) / HZ;
666
667         put_unaligned_be32((uint)(t & 0xffffffff), buf);
668 }
669
670
671
672 /**
673  * dgrp_monitor_message() -- Builds a rpdump style message.
674  * @nd: pointer to a node structure
675  * @message: destination buffer
676  */
677 static void dgrp_monitor_message(struct nd_struct *nd, char *message)
678 {
679         u8 header[7];
680         int n;
681
682         header[0] = RPDUMP_MESSAGE;
683
684         dgrp_encode_time(nd, header + 1);
685
686         n = strlen(message);
687
688         put_unaligned_be16(n, header + 5);
689
690         dgrp_monitor(nd, header, sizeof(header));
691         dgrp_monitor(nd, (u8 *) message, n);
692 }
693
694
695
696 /**
697  * dgrp_monitor_reset() -- Note a reset in the monitoring buffer.
698  * @nd: pointer to a node structure
699  */
700 static void dgrp_monitor_reset(struct nd_struct *nd)
701 {
702         u8 header[5];
703
704         header[0] = RPDUMP_RESET;
705
706         dgrp_encode_time(nd, header + 1);
707
708         dgrp_monitor(nd, header, sizeof(header));
709 }
710
711 /**
712  * dgrp_monitor_data() -- builds a monitor data packet
713  * @nd: pointer to a node structure
714  * @type: type of message to be logged
715  * @buf: data to be logged
716  * @size: number of bytes in the buffer
717  */
718 static void dgrp_monitor_data(struct nd_struct *nd, u8 type, u8 *buf, int size)
719 {
720         u8 header[7];
721
722         header[0] = type;
723
724         dgrp_encode_time(nd, header + 1);
725
726         put_unaligned_be16(size, header + 5);
727
728         dgrp_monitor(nd, header, sizeof(header));
729         dgrp_monitor(nd, buf, size);
730 }
731
732 static int alloc_nd_buffers(struct nd_struct *nd)
733 {
734
735         nd->nd_iobuf = NULL;
736         nd->nd_writebuf = NULL;
737         nd->nd_inputbuf = NULL;
738         nd->nd_inputflagbuf = NULL;
739
740         /*
741          *  Allocate the network read/write buffer.
742          */
743         nd->nd_iobuf = kzalloc(UIO_MAX + 10, GFP_KERNEL);
744         if (!nd->nd_iobuf)
745                 goto out_err;
746
747         /*
748          * Allocate a buffer for doing the copy from user space to
749          * kernel space in the write routines.
750          */
751         nd->nd_writebuf = kzalloc(WRITEBUFLEN, GFP_KERNEL);
752         if (!nd->nd_writebuf)
753                 goto out_err;
754
755         /*
756          * Allocate a buffer for doing the copy from kernel space to
757          * tty buffer space in the read routines.
758          */
759         nd->nd_inputbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
760         if (!nd->nd_inputbuf)
761                 goto out_err;
762
763         /*
764          * Allocate a buffer for doing the copy from kernel space to
765          * tty buffer space in the read routines.
766          */
767         nd->nd_inputflagbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
768         if (!nd->nd_inputflagbuf)
769                 goto out_err;
770
771         return 0;
772
773 out_err:
774         kfree(nd->nd_iobuf);
775         kfree(nd->nd_writebuf);
776         kfree(nd->nd_inputbuf);
777         kfree(nd->nd_inputflagbuf);
778         return -ENOMEM;
779 }
780
781 /*
782  * dgrp_net_open() -- Open the NET device for a particular PortServer
783  */
784 static int dgrp_net_open(struct inode *inode, struct file *file)
785 {
786         struct nd_struct *nd;
787         struct proc_dir_entry *de;
788         ulong  lock_flags;
789         int rtn;
790
791         rtn = try_module_get(THIS_MODULE);
792         if (!rtn)
793                 return -EAGAIN;
794
795         if (!capable(CAP_SYS_ADMIN)) {
796                 rtn = -EPERM;
797                 goto done;
798         }
799
800         /*
801          *  Make sure that the "private_data" field hasn't already been used.
802          */
803         if (file->private_data) {
804                 rtn = -EINVAL;
805                 goto done;
806         }
807
808         /*
809          *  Get the node pointer, and fail if it doesn't exist.
810          */
811         de = PDE(inode);
812         if (!de) {
813                 rtn = -ENXIO;
814                 goto done;
815         }
816
817         nd = (struct nd_struct *) de->data;
818         if (!nd) {
819                 rtn = -ENXIO;
820                 goto done;
821         }
822
823         file->private_data = (void *) nd;
824
825         /*
826          *  Grab the NET lock.
827          */
828         down(&nd->nd_net_semaphore);
829
830         if (nd->nd_state != NS_CLOSED) {
831                 rtn = -EBUSY;
832                 goto unlock;
833         }
834
835         /*
836          *  Initialize the link speed parameters.
837          */
838
839         nd->nd_link.lk_fast_rate = UIO_MAX;
840         nd->nd_link.lk_slow_rate = UIO_MAX;
841
842         nd->nd_link.lk_fast_delay = 1000;
843         nd->nd_link.lk_slow_delay = 1000;
844
845         nd->nd_link.lk_header_size = 46;
846
847
848         rtn = alloc_nd_buffers(nd);
849         if (rtn)
850                 goto unlock;
851
852         /*
853          *  The port is now open, so move it to the IDLE state
854          */
855         dgrp_net_idle(nd);
856
857         nd->nd_tx_time = jiffies;
858
859         /*
860          *  If the polling routing is not running, start it running here
861          */
862         spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
863
864         if (!dgrp_poll_data.node_active_count) {
865                 dgrp_poll_data.node_active_count = 2;
866                 dgrp_poll_data.timer.expires = jiffies +
867                         dgrp_poll_tick * HZ / 1000;
868                 add_timer(&dgrp_poll_data.timer);
869         }
870
871         spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
872
873         dgrp_monitor_message(nd, "Net Open");
874
875 unlock:
876         /*
877          *  Release the NET lock.
878          */
879         up(&nd->nd_net_semaphore);
880
881 done:
882         if (rtn)
883                 module_put(THIS_MODULE);
884
885         return rtn;
886 }
887
888 /* dgrp_net_release() -- close the NET device for a particular PortServer */
889 static int dgrp_net_release(struct inode *inode, struct file *file)
890 {
891         struct nd_struct *nd;
892         ulong  lock_flags;
893
894         nd = (struct nd_struct *)(file->private_data);
895         if (!nd)
896                 goto done;
897
898 /* TODO : historical locking placeholder */
899 /*
900  *  In the HPUX version of the RealPort driver (which served as a basis
901  *  for this driver) this locking code was used.  Saved if ever we need
902  *  to review the locking under Linux.
903  */
904 /*      spinlock(&nd->nd_lock); */
905
906
907         /*
908          *  Grab the NET lock.
909          */
910         down(&nd->nd_net_semaphore);
911
912         /*
913          *  Before "closing" the internal connection, make sure all
914          *  ports are "idle".
915          */
916         dgrp_net_idle(nd);
917
918         nd->nd_state = NS_CLOSED;
919         nd->nd_flag = 0;
920
921         /*
922          *  TODO ... must the wait queue be reset on close?
923          *  should any pending waiters be reset?
924          *  Let's decide to assert that the waitq is empty... and see
925          *  how soon we break.
926          */
927         if (waitqueue_active(&nd->nd_tx_waitq))
928                 pr_info("%s - expected waitqueue_active to be false\n",
929                         __func__);
930
931         nd->nd_send = 0;
932
933         kfree(nd->nd_iobuf);
934         nd->nd_iobuf = NULL;
935
936 /* TODO : historical locking placeholder */
937 /*
938  *  In the HPUX version of the RealPort driver (which served as a basis
939  *  for this driver) this locking code was used.  Saved if ever we need
940  *  to review the locking under Linux.
941  */
942 /*      spinunlock( &nd->nd_lock ); */
943
944
945         kfree(nd->nd_writebuf);
946         nd->nd_writebuf = NULL;
947
948         kfree(nd->nd_inputbuf);
949         nd->nd_inputbuf = NULL;
950
951         kfree(nd->nd_inputflagbuf);
952         nd->nd_inputflagbuf = NULL;
953
954 /* TODO : historical locking placeholder */
955 /*
956  *  In the HPUX version of the RealPort driver (which served as a basis
957  *  for this driver) this locking code was used.  Saved if ever we need
958  *  to review the locking under Linux.
959  */
960 /*      spinlock(&nd->nd_lock); */
961
962         /*
963          *  Set the active port count to zero.
964          */
965         dgrp_chan_count(nd, 0);
966
967 /* TODO : historical locking placeholder */
968 /*
969  *  In the HPUX version of the RealPort driver (which served as a basis
970  *  for this driver) this locking code was used.  Saved if ever we need
971  *  to review the locking under Linux.
972  */
973 /*      spinunlock(&nd->nd_lock); */
974
975         /*
976          *  Release the NET lock.
977          */
978         up(&nd->nd_net_semaphore);
979
980         /*
981          *  Cause the poller to stop scheduling itself if this is
982          *  the last active node.
983          */
984         spin_lock_irqsave(&dgrp_poll_data.poll_lock, lock_flags);
985
986         if (dgrp_poll_data.node_active_count == 2) {
987                 del_timer(&dgrp_poll_data.timer);
988                 dgrp_poll_data.node_active_count = 0;
989         }
990
991         spin_unlock_irqrestore(&dgrp_poll_data.poll_lock, lock_flags);
992
993         down(&nd->nd_net_semaphore);
994
995         dgrp_monitor_message(nd, "Net Close");
996
997         up(&nd->nd_net_semaphore);
998
999 done:
1000         module_put(THIS_MODULE);
1001         file->private_data = NULL;
1002         return 0;
1003 }
1004
1005 /* used in dgrp_send to setup command header */
1006 static inline u8 *set_cmd_header(u8 *b, u8 port, u8 cmd)
1007 {
1008         *b++ = 0xb0 + (port & 0x0f);
1009         *b++ = cmd;
1010         return b;
1011 }
1012
1013 /**
1014  * dgrp_send() -- build a packet for transmission to the server
1015  * @nd: pointer to a node structure
1016  * @tmax: maximum bytes to transmit
1017  *
1018  * returns number of bytes sent
1019  */
1020 static int dgrp_send(struct nd_struct *nd, long tmax)
1021 {
1022         struct ch_struct *ch = nd->nd_chan;
1023         u8 *b;
1024         u8 *buf;
1025         u8 *mbuf;
1026         u8 port;
1027         int mod;
1028         long send;
1029         int maxport;
1030         long lastport = -1;
1031         ushort rwin;
1032         long in;
1033         ushort n;
1034         long t;
1035         long ttotal;
1036         long tchan;
1037         long tsend;
1038         ushort tsafe;
1039         long work;
1040         long send_sync;
1041         long wanted_sync_port = -1;
1042         ushort tdata[CHAN_MAX];
1043         long used_buffer;
1044
1045         mbuf = nd->nd_iobuf + UIO_BASE;
1046         buf = b = mbuf;
1047
1048         send_sync = nd->nd_link.lk_slow_rate < UIO_MAX;
1049
1050         ttotal = 0;
1051         tchan = 0;
1052
1053         memset(tdata, 0, sizeof(tdata));
1054
1055
1056         /*
1057          * If there are any outstanding requests to be serviced,
1058          * service them here.
1059          */
1060         if (nd->nd_send & NR_PASSWORD) {
1061
1062                 /*
1063                  *  Send Password response.
1064                  */
1065
1066                 b[0] = 0xfc;
1067                 b[1] = 0x20;
1068                 put_unaligned_be16(strlen(nd->password), b + 2);
1069                 b += 4;
1070                 b += strlen(nd->password);
1071                 nd->nd_send &= ~(NR_PASSWORD);
1072         }
1073
1074
1075         /*
1076          *  Loop over all modules to generate commands, and determine
1077          *  the amount of data queued for transmit.
1078          */
1079
1080         for (mod = 0, port = 0; port < nd->nd_chan_count; mod++) {
1081                 /*
1082                  *  If this is not the current module, enter a module select
1083                  *  code in the buffer.
1084                  */
1085
1086                 if (mod != nd->nd_tx_module)
1087                         mbuf = ++b;
1088
1089                 /*
1090                  *  Loop to process one module.
1091                  */
1092
1093                 maxport = port + 16;
1094
1095                 if (maxport > nd->nd_chan_count)
1096                         maxport = nd->nd_chan_count;
1097
1098                 for (; port < maxport; port++, ch++) {
1099                         /*
1100                          *  Switch based on channel state.
1101                          */
1102
1103                         switch (ch->ch_state) {
1104                         /*
1105                          *  Send requests when the port is closed, and there
1106                          *  are no Open, Close or Cancel requests expected.
1107                          */
1108
1109                         case CS_IDLE:
1110                                 /*
1111                                  * Wait until any open error code
1112                                  * has been delivered to all
1113                                  * associated ports.
1114                                  */
1115
1116                                 if (ch->ch_open_error) {
1117                                         if (ch->ch_wait_count[ch->ch_otype]) {
1118                                                 work = 1;
1119                                                 break;
1120                                         }
1121
1122                                         ch->ch_open_error = 0;
1123                                 }
1124
1125                                 /*
1126                                  *  Wait until the channel HANGUP flag is reset
1127                                  *  before sending the first open.  We can only
1128                                  *  get to this state after a server disconnect.
1129                                  */
1130
1131                                 if ((ch->ch_flag & CH_HANGUP) != 0)
1132                                         break;
1133
1134                                 /*
1135                                  *  If recovering from a TCP disconnect, or if
1136                                  *  there is an immediate open pending, send an
1137                                  *  Immediate Open request.
1138                                  */
1139                                 if ((ch->ch_flag & CH_PORT_GONE) ||
1140                                     ch->ch_wait_count[OTYPE_IMMEDIATE] != 0) {
1141                                         b = set_cmd_header(b, port, 10);
1142                                         *b++ = 0;
1143
1144                                         ch->ch_state = CS_WAIT_OPEN;
1145                                         ch->ch_otype = OTYPE_IMMEDIATE;
1146                                         break;
1147                                 }
1148
1149         /*
1150          *  If there is no Persistent or Incoming Open on the wait
1151          *  list in the server, and a thread is waiting for a
1152          *  Persistent or Incoming Open, send a Persistent or Incoming
1153          *  Open Request.
1154          */
1155                                 if (ch->ch_otype_waiting == 0) {
1156                                         if (ch->ch_wait_count[OTYPE_PERSISTENT] != 0) {
1157                                                 b = set_cmd_header(b, port, 10);
1158                                                 *b++ = 1;
1159
1160                                                 ch->ch_state = CS_WAIT_OPEN;
1161                                                 ch->ch_otype = OTYPE_PERSISTENT;
1162                                         } else if (ch->ch_wait_count[OTYPE_INCOMING] != 0) {
1163                                                 b = set_cmd_header(b, port, 10);
1164                                                 *b++ = 2;
1165
1166                                                 ch->ch_state = CS_WAIT_OPEN;
1167                                                 ch->ch_otype = OTYPE_INCOMING;
1168                                         }
1169                                         break;
1170                                 }
1171
1172                                 /*
1173                                  *  If a Persistent or Incoming Open is pending in
1174                                  *  the server, but there is no longer an open
1175                                  *  thread waiting for it, cancel the request.
1176                                  */
1177
1178                                 if (ch->ch_wait_count[ch->ch_otype_waiting] == 0) {
1179                                         b = set_cmd_header(b, port, 10);
1180                                         *b++ = 4;
1181
1182                                         ch->ch_state = CS_WAIT_CANCEL;
1183                                         ch->ch_otype = ch->ch_otype_waiting;
1184                                 }
1185                                 break;
1186
1187                                 /*
1188                                  *  Send port parameter queries.
1189                                  */
1190                         case CS_SEND_QUERY:
1191                                 /*
1192                                  *  Clear out all FEP state that might remain
1193                                  *  from the last connection.
1194                                  */
1195
1196                                 ch->ch_flag |= CH_PARAM;
1197
1198                                 ch->ch_flag &= ~CH_RX_FLUSH;
1199
1200                                 ch->ch_expect = 0;
1201
1202                                 ch->ch_s_tin   = 0;
1203                                 ch->ch_s_tpos  = 0;
1204                                 ch->ch_s_tsize = 0;
1205                                 ch->ch_s_treq  = 0;
1206                                 ch->ch_s_elast = 0;
1207
1208                                 ch->ch_s_rin   = 0;
1209                                 ch->ch_s_rwin  = 0;
1210                                 ch->ch_s_rsize = 0;
1211
1212                                 ch->ch_s_tmax  = 0;
1213                                 ch->ch_s_ttime = 0;
1214                                 ch->ch_s_rmax  = 0;
1215                                 ch->ch_s_rtime = 0;
1216                                 ch->ch_s_rlow  = 0;
1217                                 ch->ch_s_rhigh = 0;
1218
1219                                 ch->ch_s_brate = 0;
1220                                 ch->ch_s_iflag = 0;
1221                                 ch->ch_s_cflag = 0;
1222                                 ch->ch_s_oflag = 0;
1223                                 ch->ch_s_xflag = 0;
1224
1225                                 ch->ch_s_mout  = 0;
1226                                 ch->ch_s_mflow = 0;
1227                                 ch->ch_s_mctrl = 0;
1228                                 ch->ch_s_xon   = 0;
1229                                 ch->ch_s_xoff  = 0;
1230                                 ch->ch_s_lnext = 0;
1231                                 ch->ch_s_xxon  = 0;
1232                                 ch->ch_s_xxoff = 0;
1233
1234                                 /* Send Sequence Request */
1235                                 b = set_cmd_header(b, port, 14);
1236
1237                                 /* Configure Event Conditions Packet */
1238                                 b = set_cmd_header(b, port, 42);
1239                                 put_unaligned_be16(0x02c0, b);
1240                                 b += 2;
1241                                 *b++ = (DM_DTR | DM_RTS | DM_CTS |
1242                                         DM_DSR | DM_RI | DM_CD);
1243
1244                                 /* Send Status Request */
1245                                 b = set_cmd_header(b, port, 16);
1246
1247                                 /* Send Buffer Request  */
1248                                 b = set_cmd_header(b, port, 20);
1249
1250                                 /* Send Port Capability Request */
1251                                 b = set_cmd_header(b, port, 22);
1252
1253                                 ch->ch_expect = (RR_SEQUENCE |
1254                                                  RR_STATUS  |
1255                                                  RR_BUFFER |
1256                                                  RR_CAPABILITY);
1257
1258                                 ch->ch_state = CS_WAIT_QUERY;
1259
1260                                 /* Raise modem signals */
1261                                 b = set_cmd_header(b, port, 44);
1262
1263                                 if (ch->ch_flag & CH_PORT_GONE)
1264                                         ch->ch_s_mout = ch->ch_mout;
1265                                 else
1266                                         ch->ch_s_mout = ch->ch_mout = DM_DTR | DM_RTS;
1267
1268                                 *b++ = ch->ch_mout;
1269                                 *b++ = ch->ch_s_mflow = 0;
1270                                 *b++ = ch->ch_s_mctrl = ch->ch_mctrl = 0;
1271
1272                                 if (ch->ch_flag & CH_PORT_GONE)
1273                                         ch->ch_flag &= ~CH_PORT_GONE;
1274
1275                                 break;
1276
1277                         /*
1278                          *  Handle normal open and ready mode.
1279                          */
1280
1281                         case CS_READY:
1282
1283                                 /*
1284                                  *  If the port is not open, and there are no
1285                                  *  no longer any ports requesting an open,
1286                                  *  then close the port.
1287                                  */
1288
1289                                 if (ch->ch_open_count == 0 &&
1290                                     ch->ch_wait_count[ch->ch_otype] == 0) {
1291                                         goto send_close;
1292                                 }
1293
1294         /*
1295          *  Process waiting input.
1296          *
1297          *  If there is no one to read it, discard the data.
1298          *
1299          *  Otherwise if we are not in fastcook mode, or if there is a
1300          *  fastcook thread waiting for data, send the data to the
1301          *  line discipline.
1302          */
1303                                 if (ch->ch_rin != ch->ch_rout) {
1304                                         if (ch->ch_tun.un_open_count == 0 ||
1305                                              (ch->ch_tun.un_flag & UN_CLOSING) ||
1306                                             (ch->ch_cflag & CF_CREAD) == 0) {
1307                                                 ch->ch_rout = ch->ch_rin;
1308                                         } else if ((ch->ch_flag & CH_FAST_READ) == 0 ||
1309                                                         ch->ch_inwait != 0) {
1310                                                 dgrp_input(ch);
1311
1312                                                 if (ch->ch_rin != ch->ch_rout)
1313                                                         work = 1;
1314                                         }
1315                                 }
1316
1317                                 /*
1318                                  *  Handle receive flush, and changes to
1319                                  *  server port parameters.
1320                                  */
1321
1322                                 if (ch->ch_flag & (CH_RX_FLUSH | CH_PARAM)) {
1323                                 /*
1324                                  *  If we are in receive flush mode,
1325                                  *  and enough data has gone by, reset
1326                                  *  receive flush mode.
1327                                  */
1328                                         if (ch->ch_flag & CH_RX_FLUSH) {
1329                                                 if (((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >
1330                                                     ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK))
1331                                                         ch->ch_flag &= ~CH_RX_FLUSH;
1332                                                 else
1333                                                         work = 1;
1334                                         }
1335
1336                                         /*
1337                                          *  Send TMAX, TTIME.
1338                                          */
1339
1340                                         if (ch->ch_s_tmax  != ch->ch_tmax ||
1341                                             ch->ch_s_ttime != ch->ch_ttime) {
1342                                                 b = set_cmd_header(b, port, 48);
1343
1344                                                 ch->ch_s_tmax = ch->ch_tmax;
1345                                                 ch->ch_s_ttime = ch->ch_ttime;
1346
1347                                                 put_unaligned_be16(ch->ch_s_tmax,
1348                                                                    b);
1349                                                 b += 2;
1350
1351                                                 put_unaligned_be16(ch->ch_s_ttime,
1352                                                                    b);
1353                                                 b += 2;
1354                                         }
1355
1356                                         /*
1357                                          *  Send RLOW, RHIGH.
1358                                          */
1359
1360                                         if (ch->ch_s_rlow  != ch->ch_rlow ||
1361                                             ch->ch_s_rhigh != ch->ch_rhigh) {
1362                                                 b = set_cmd_header(b, port, 45);
1363
1364                                                 ch->ch_s_rlow  = ch->ch_rlow;
1365                                                 ch->ch_s_rhigh = ch->ch_rhigh;
1366
1367                                                 put_unaligned_be16(ch->ch_s_rlow,
1368                                                                    b);
1369                                                 b += 2;
1370
1371                                                 put_unaligned_be16(ch->ch_s_rhigh,
1372                                                                    b);
1373                                                 b += 2;
1374                                         }
1375
1376                                         /*
1377                                          *  Send BRATE, CFLAG, IFLAG,
1378                                          *  OFLAG, XFLAG.
1379                                          */
1380
1381                                         if (ch->ch_s_brate != ch->ch_brate ||
1382                                             ch->ch_s_cflag != ch->ch_cflag ||
1383                                             ch->ch_s_iflag != ch->ch_iflag ||
1384                                             ch->ch_s_oflag != ch->ch_oflag ||
1385                                             ch->ch_s_xflag != ch->ch_xflag) {
1386                                                 b = set_cmd_header(b, port, 40);
1387
1388                                                 ch->ch_s_brate = ch->ch_brate;
1389                                                 ch->ch_s_cflag = ch->ch_cflag;
1390                                                 ch->ch_s_iflag = ch->ch_iflag;
1391                                                 ch->ch_s_oflag = ch->ch_oflag;
1392                                                 ch->ch_s_xflag = ch->ch_xflag;
1393
1394                                                 put_unaligned_be16(ch->ch_s_brate,
1395                                                                    b);
1396                                                 b += 2;
1397
1398                                                 put_unaligned_be16(ch->ch_s_cflag,
1399                                                                    b);
1400                                                 b += 2;
1401
1402                                                 put_unaligned_be16(ch->ch_s_iflag,
1403                                                                    b);
1404                                                 b += 2;
1405
1406                                                 put_unaligned_be16(ch->ch_s_oflag,
1407                                                                    b);
1408                                                 b += 2;
1409
1410                                                 put_unaligned_be16(ch->ch_s_xflag,
1411                                                                    b);
1412                                                 b += 2;
1413                                         }
1414
1415                                         /*
1416                                          *  Send MOUT, MFLOW, MCTRL.
1417                                          */
1418
1419                                         if (ch->ch_s_mout  != ch->ch_mout  ||
1420                                             ch->ch_s_mflow != ch->ch_mflow ||
1421                                             ch->ch_s_mctrl != ch->ch_mctrl) {
1422                                                 b = set_cmd_header(b, port, 44);
1423
1424                                                 *b++ = ch->ch_s_mout  = ch->ch_mout;
1425                                                 *b++ = ch->ch_s_mflow = ch->ch_mflow;
1426                                                 *b++ = ch->ch_s_mctrl = ch->ch_mctrl;
1427                                         }
1428
1429                                         /*
1430                                          *  Send Flow control characters.
1431                                          */
1432
1433                                         if (ch->ch_s_xon   != ch->ch_xon   ||
1434                                             ch->ch_s_xoff  != ch->ch_xoff  ||
1435                                             ch->ch_s_lnext != ch->ch_lnext ||
1436                                             ch->ch_s_xxon  != ch->ch_xxon  ||
1437                                             ch->ch_s_xxoff != ch->ch_xxoff) {
1438                                                 b = set_cmd_header(b, port, 46);
1439
1440                                                 *b++ = ch->ch_s_xon   = ch->ch_xon;
1441                                                 *b++ = ch->ch_s_xoff  = ch->ch_xoff;
1442                                                 *b++ = ch->ch_s_lnext = ch->ch_lnext;
1443                                                 *b++ = ch->ch_s_xxon  = ch->ch_xxon;
1444                                                 *b++ = ch->ch_s_xxoff = ch->ch_xxoff;
1445                                         }
1446
1447                                         /*
1448                                          *  Send RMAX, RTIME.
1449                                          */
1450
1451                                         if (ch->ch_s_rmax != ch->ch_rmax ||
1452                                             ch->ch_s_rtime != ch->ch_rtime) {
1453                                                 b = set_cmd_header(b, port, 47);
1454
1455                                                 ch->ch_s_rmax  = ch->ch_rmax;
1456                                                 ch->ch_s_rtime = ch->ch_rtime;
1457
1458                                                 put_unaligned_be16(ch->ch_s_rmax,
1459                                                                    b);
1460                                                 b += 2;
1461
1462                                                 put_unaligned_be16(ch->ch_s_rtime,
1463                                                                    b);
1464                                                 b += 2;
1465                                         }
1466
1467                                         ch->ch_flag &= ~CH_PARAM;
1468                                         wake_up_interruptible(&ch->ch_flag_wait);
1469                                 }
1470
1471
1472                                 /*
1473                                  *  Handle action commands.
1474                                  */
1475
1476                                 if (ch->ch_send != 0) {
1477                                         /* int send = ch->ch_send & ~ch->ch_expect; */
1478                                         send = ch->ch_send & ~ch->ch_expect;
1479
1480                                         /* Send character immediate */
1481                                         if ((send & RR_TX_ICHAR) != 0) {
1482                                                 b = set_cmd_header(b, port, 60);
1483
1484                                                 *b++ = ch->ch_xon;
1485                                                 ch->ch_expect |= RR_TX_ICHAR;
1486                                         }
1487
1488                                         /* BREAK request */
1489                                         if ((send & RR_TX_BREAK) != 0) {
1490                                                 if (ch->ch_break_time != 0) {
1491                                                         b = set_cmd_header(b, port, 61);
1492                                                         put_unaligned_be16(ch->ch_break_time,
1493                                                                            b);
1494                                                         b += 2;
1495
1496                                                         ch->ch_expect |= RR_TX_BREAK;
1497                                                         ch->ch_break_time = 0;
1498                                                 } else {
1499                                                         ch->ch_send &= ~RR_TX_BREAK;
1500                                                         ch->ch_flag &= ~CH_TX_BREAK;
1501                                                         wake_up_interruptible(&ch->ch_flag_wait);
1502                                                 }
1503                                         }
1504
1505                                         /*
1506                                          *  Flush input/output buffers.
1507                                          */
1508
1509                                         if ((send & (RR_RX_FLUSH | RR_TX_FLUSH)) != 0) {
1510                                                 b = set_cmd_header(b, port, 62);
1511
1512                                                 *b++ = ((send & RR_TX_FLUSH) == 0 ? 1 :
1513                                                         (send & RR_RX_FLUSH) == 0 ? 2 : 3);
1514
1515                                                 if (send & RR_RX_FLUSH) {
1516                                                         ch->ch_flush_seq = nd->nd_seq_in;
1517                                                         ch->ch_flag |= CH_RX_FLUSH;
1518                                                         work = 1;
1519                                                         send_sync = 1;
1520                                                         wanted_sync_port = port;
1521                                                 }
1522
1523                                                 ch->ch_send &= ~(RR_RX_FLUSH | RR_TX_FLUSH);
1524                                         }
1525
1526                                         /*  Pause input/output */
1527                                         if ((send & (RR_RX_STOP | RR_TX_STOP)) != 0) {
1528                                                 b = set_cmd_header(b, port, 63);
1529                                                 *b = 0;
1530
1531                                                 if ((send & RR_TX_STOP) != 0)
1532                                                         *b |= EV_OPU;
1533
1534                                                 if ((send & RR_RX_STOP) != 0)
1535                                                         *b |= EV_IPU;
1536
1537                                                 b++;
1538
1539                                                 ch->ch_send &= ~(RR_RX_STOP | RR_TX_STOP);
1540                                         }
1541
1542                                         /* Start input/output */
1543                                         if ((send & (RR_RX_START | RR_TX_START)) != 0) {
1544                                                 b = set_cmd_header(b, port, 64);
1545                                                 *b = 0;
1546
1547                                                 if ((send & RR_TX_START) != 0)
1548                                                         *b |= EV_OPU | EV_OPS | EV_OPX;
1549
1550                                                 if ((send & RR_RX_START) != 0)
1551                                                         *b |= EV_IPU | EV_IPS;
1552
1553                                                 b++;
1554
1555                                                 ch->ch_send &= ~(RR_RX_START | RR_TX_START);
1556                                         }
1557                                 }
1558
1559
1560                                 /*
1561                                  *  Send a window sequence to acknowledge received data.
1562                                  */
1563
1564                                 rwin = (ch->ch_s_rin +
1565                                         ((ch->ch_rout - ch->ch_rin - 1) & RBUF_MASK));
1566
1567                                 n = (rwin - ch->ch_s_rwin) & 0xffff;
1568
1569                                 if (n >= RBUF_MAX / 4) {
1570                                         b[0] = 0xa0 + (port & 0xf);
1571                                         ch->ch_s_rwin = rwin;
1572                                         put_unaligned_be16(rwin, b + 1);
1573                                         b += 3;
1574                                 }
1575
1576                                 /*
1577                                  *  If the terminal is waiting on LOW
1578                                  *  water or EMPTY, and the condition
1579                                  *  is now satisfied, call the line
1580                                  *  discipline to put more data in the
1581                                  *  buffer.
1582                                  */
1583
1584                                 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1585
1586                                 if ((ch->ch_tun.un_flag & (UN_EMPTY|UN_LOW)) != 0) {
1587                                         if ((ch->ch_tun.un_flag & UN_LOW) != 0 ?
1588                                             (n <= TBUF_LOW) :
1589                                             (n == 0 && ch->ch_s_tpos == ch->ch_s_tin)) {
1590                                                 ch->ch_tun.un_flag &= ~(UN_EMPTY|UN_LOW);
1591
1592                                                 if (waitqueue_active(&((ch->ch_tun.un_tty)->write_wait)))
1593                                                         wake_up_interruptible(&((ch->ch_tun.un_tty)->write_wait));
1594                                                 tty_wakeup(ch->ch_tun.un_tty);
1595                                                 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1596                                         }
1597                                 }
1598
1599                                 /*
1600                                  * If the printer is waiting on LOW
1601                                  * water, TIME, EMPTY or PWAIT, and is
1602                                  * now ready to put more data in the
1603                                  * buffer, call the line discipline to
1604                                  * do the job.
1605                                  */
1606
1607                                 /* FIXME: jiffies - ch->ch_waketime can never
1608                                    be < 0. Someone needs to work out what is
1609                                    actually intended here */
1610                                 if (ch->ch_pun.un_open_count &&
1611                                     (ch->ch_pun.un_flag &
1612                                     (UN_EMPTY|UN_TIME|UN_LOW|UN_PWAIT)) != 0) {
1613
1614                                         if ((ch->ch_pun.un_flag & UN_LOW) != 0 ?
1615                                             (n <= TBUF_LOW) :
1616                                             (ch->ch_pun.un_flag & UN_TIME) != 0 ?
1617                                             ((jiffies - ch->ch_waketime) >= 0) :
1618                                             (n == 0 && ch->ch_s_tpos == ch->ch_s_tin) &&
1619                                             ((ch->ch_pun.un_flag & UN_EMPTY) != 0 ||
1620                                             ((ch->ch_tun.un_open_count &&
1621                                               ch->ch_tun.un_tty->ops->chars_in_buffer) ?
1622                                              (ch->ch_tun.un_tty->ops->chars_in_buffer)(ch->ch_tun.un_tty) == 0
1623                                              : 1
1624                                             )
1625                                             )) {
1626                                                 ch->ch_pun.un_flag &= ~(UN_EMPTY | UN_TIME | UN_LOW | UN_PWAIT);
1627
1628                                                 if (waitqueue_active(&((ch->ch_pun.un_tty)->write_wait)))
1629                                                         wake_up_interruptible(&((ch->ch_pun.un_tty)->write_wait));
1630                                                 tty_wakeup(ch->ch_pun.un_tty);
1631                                                 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1632
1633                                         } else if ((ch->ch_pun.un_flag & UN_TIME) != 0) {
1634                                                 work = 1;
1635                                         }
1636                                 }
1637
1638
1639                                 /*
1640                                  *  Determine the max number of bytes
1641                                  *  this port can send, including
1642                                  *  packet header overhead.
1643                                  */
1644
1645                                 t = ((ch->ch_s_tsize + ch->ch_s_tpos - ch->ch_s_tin) & 0xffff);
1646
1647                                 if (n > t)
1648                                         n = t;
1649
1650                                 if (n != 0) {
1651                                         n += (n <= 8 ? 1 : n <= 255 ? 2 : 3);
1652
1653                                         tdata[tchan++] = n;
1654                                         ttotal += n;
1655                                 }
1656                                 break;
1657
1658                         /*
1659                          *  Close the port.
1660                          */
1661
1662 send_close:
1663                         case CS_SEND_CLOSE:
1664                                 b = set_cmd_header(b, port, 10);
1665                                 if (ch->ch_otype == OTYPE_IMMEDIATE)
1666                                         *b++ = 3;
1667                                 else
1668                                         *b++ = 4;
1669
1670                                 ch->ch_state = CS_WAIT_CLOSE;
1671                                 break;
1672
1673                         /*
1674                          *  Wait for a previous server request.
1675                          */
1676
1677                         case CS_WAIT_OPEN:
1678                         case CS_WAIT_CANCEL:
1679                         case CS_WAIT_FAIL:
1680                         case CS_WAIT_QUERY:
1681                         case CS_WAIT_CLOSE:
1682                                 break;
1683
1684                         default:
1685                                 pr_info("%s - unexpected channel state (%i)\n",
1686                                         __func__, ch->ch_state);
1687                         }
1688                 }
1689
1690                 /*
1691                  *  If a module select code is needed, drop one in.  If space
1692                  *  was reserved for one, but none is needed, recover the space.
1693                  */
1694
1695                 if (mod != nd->nd_tx_module) {
1696                         if (b != mbuf) {
1697                                 mbuf[-1] = 0xf0 | mod;
1698                                 nd->nd_tx_module = mod;
1699                         } else {
1700                                 b--;
1701                         }
1702                 }
1703         }
1704
1705         /*
1706          *  Adjust "tmax" so that under worst case conditions we do
1707          *  not overflow either the daemon buffer or the internal
1708          *  buffer in the loop that follows.   Leave a safe area
1709          *  of 64 bytes so we start getting asserts before we start
1710          *  losing data or clobbering memory.
1711          */
1712
1713         n = UIO_MAX - UIO_BASE;
1714
1715         if (tmax > n)
1716                 tmax = n;
1717
1718         tmax -= 64;
1719
1720         tsafe = tmax;
1721
1722         /*
1723          *  Allocate space for 5 Module Selects, 1 Sequence Request,
1724          *  and 1 Set TREQ for each active channel.
1725          */
1726
1727         tmax -= 5 + 3 + 4 * nd->nd_chan_count;
1728
1729         /*
1730          *  Further reduce "tmax" to the available transmit credit.
1731          *  Note that this is a soft constraint;  The transmit credit
1732          *  can go negative for a time and then recover.
1733          */
1734
1735         n = nd->nd_tx_deposit - nd->nd_tx_charge - nd->nd_link.lk_header_size;
1736
1737         if (tmax > n)
1738                 tmax = n;
1739
1740         /*
1741          *  Finally reduce tmax by the number of bytes already in
1742          *  the buffer.
1743          */
1744
1745         tmax -= b - buf;
1746
1747         /*
1748          *  Suspend data transmit unless every ready channel can send
1749          *  at least 1 character.
1750          */
1751         if (tmax < 2 * nd->nd_chan_count) {
1752                 tsend = 1;
1753
1754         } else if (tchan > 1 && ttotal > tmax) {
1755
1756                 /*
1757                  *  If transmit is limited by the credit budget, find the
1758                  *  largest number of characters we can send without driving
1759                  *  the credit negative.
1760                  */
1761
1762                 long tm = tmax;
1763                 int tc = tchan;
1764                 int try;
1765
1766                 tsend = tm / tc;
1767
1768                 for (try = 0; try < 3; try++) {
1769                         int i;
1770                         int c = 0;
1771
1772                         for (i = 0; i < tc; i++) {
1773                                 if (tsend < tdata[i])
1774                                         tdata[c++] = tdata[i];
1775                                 else
1776                                         tm -= tdata[i];
1777                         }
1778
1779                         if (c == tc)
1780                                 break;
1781
1782                         tsend = tm / c;
1783
1784                         if (c == 1)
1785                                 break;
1786
1787                         tc = c;
1788                 }
1789
1790                 tsend = tm / nd->nd_chan_count;
1791
1792                 if (tsend < 2)
1793                         tsend = 1;
1794
1795         } else {
1796                 /*
1797                  *  If no budgetary constraints, or only one channel ready
1798                  *  to send, set the character limit to the remaining
1799                  *  buffer size.
1800                  */
1801
1802                 tsend = tmax;
1803         }
1804
1805         tsend -= (tsend <= 9) ? 1 : (tsend <= 257) ? 2 : 3;
1806
1807         /*
1808          *  Loop over all channels, sending queued data.
1809          */
1810
1811         port = 0;
1812         ch = nd->nd_chan;
1813         used_buffer = tmax;
1814
1815         for (mod = 0; port < nd->nd_chan_count; mod++) {
1816                 /*
1817                  *  If this is not the current module, enter a module select
1818                  *  code in the buffer.
1819                  */
1820
1821                 if (mod != nd->nd_tx_module)
1822                         mbuf = ++b;
1823
1824                 /*
1825                  *  Loop to process one module.
1826                  */
1827
1828                 maxport = port + 16;
1829
1830                 if (maxport > nd->nd_chan_count)
1831                         maxport = nd->nd_chan_count;
1832
1833                 for (; port < maxport; port++, ch++) {
1834                         if (ch->ch_state != CS_READY)
1835                                 continue;
1836
1837                         lastport = port;
1838
1839                         n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1840
1841                         /*
1842                          *  If there is data that can be sent, send it.
1843                          */
1844
1845                         if (n != 0 && used_buffer > 0) {
1846                                 t = (ch->ch_s_tsize + ch->ch_s_tpos - ch->ch_s_tin) & 0xffff;
1847
1848                                 if (n > t)
1849                                         n = t;
1850
1851                                 if (n > tsend) {
1852                                         work = 1;
1853                                         n = tsend;
1854                                 }
1855
1856                                 if (n > used_buffer) {
1857                                         work = 1;
1858                                         n = used_buffer;
1859                                 }
1860
1861                                 if (n <= 0)
1862                                         continue;
1863
1864                                 /*
1865                                  *  Create the correct size transmit header,
1866                                  *  depending on the amount of data to transmit.
1867                                  */
1868
1869                                 if (n <= 8) {
1870
1871                                         b[0] = ((n - 1) << 4) + (port & 0xf);
1872                                         b += 1;
1873
1874                                 } else if (n <= 255) {
1875
1876                                         b[0] = 0x80 + (port & 0xf);
1877                                         b[1] = n;
1878                                         b += 2;
1879
1880                                 } else {
1881
1882                                         b[0] = 0x90 + (port & 0xf);
1883                                         put_unaligned_be16(n, b + 1);
1884                                         b += 3;
1885                                 }
1886
1887                                 ch->ch_s_tin = (ch->ch_s_tin + n) & 0xffff;
1888
1889                                 /*
1890                                  *  Copy transmit data to the packet.
1891                                  */
1892
1893                                 t = TBUF_MAX - ch->ch_tout;
1894
1895                                 if (n >= t) {
1896                                         memcpy(b, ch->ch_tbuf + ch->ch_tout, t);
1897                                         b += t;
1898                                         n -= t;
1899                                         used_buffer -= t;
1900                                         ch->ch_tout = 0;
1901                                 }
1902
1903                                 memcpy(b, ch->ch_tbuf + ch->ch_tout, n);
1904                                 b += n;
1905                                 used_buffer -= n;
1906                                 ch->ch_tout += n;
1907                                 n = (ch->ch_tin - ch->ch_tout) & TBUF_MASK;
1908                         }
1909
1910                         /*
1911                          *  Wake any terminal unit process waiting in the
1912                          *  dgrp_write routine for low water.
1913                          */
1914
1915                         if (n > TBUF_LOW)
1916                                 continue;
1917
1918                         if ((ch->ch_flag & CH_LOW) != 0) {
1919                                 ch->ch_flag &= ~CH_LOW;
1920                                 wake_up_interruptible(&ch->ch_flag_wait);
1921                         }
1922
1923                         /* selwakeup tty_sel */
1924                         if (ch->ch_tun.un_open_count) {
1925                                 struct tty_struct *tty = (ch->ch_tun.un_tty);
1926
1927                                 if (waitqueue_active(&tty->write_wait))
1928                                         wake_up_interruptible(&tty->write_wait);
1929
1930                                 tty_wakeup(tty);
1931                         }
1932
1933                         if (ch->ch_pun.un_open_count) {
1934                                 struct tty_struct *tty = (ch->ch_pun.un_tty);
1935
1936                                 if (waitqueue_active(&tty->write_wait))
1937                                         wake_up_interruptible(&tty->write_wait);
1938
1939                                 tty_wakeup(tty);
1940                         }
1941
1942                         /*
1943                          *  Do EMPTY processing.
1944                          */
1945
1946                         if (n != 0)
1947                                 continue;
1948
1949                         if ((ch->ch_flag & (CH_EMPTY | CH_DRAIN)) != 0 ||
1950                             (ch->ch_pun.un_flag & UN_EMPTY) != 0) {
1951                                 /*
1952                                  *  If there is still data in the server, ask the server
1953                                  *  to notify us when its all gone.
1954                                  */
1955
1956                                 if (ch->ch_s_treq != ch->ch_s_tin) {
1957                                         b = set_cmd_header(b, port, 43);
1958
1959                                         ch->ch_s_treq = ch->ch_s_tin;
1960                                         put_unaligned_be16(ch->ch_s_treq,
1961                                                            b);
1962                                         b += 2;
1963                                 }
1964
1965                                 /*
1966                                  *  If there is a thread waiting for buffer empty,
1967                                  *  and we are truly empty, wake the thread.
1968                                  */
1969
1970                                 else if ((ch->ch_flag & CH_EMPTY) != 0 &&
1971                                         (ch->ch_send & RR_TX_BREAK) == 0) {
1972                                         ch->ch_flag &= ~CH_EMPTY;
1973
1974                                         wake_up_interruptible(&ch->ch_flag_wait);
1975                                 }
1976                         }
1977                 }
1978
1979                 /*
1980                  *  If a module select code is needed, drop one in.  If space
1981                  *  was reserved for one, but none is needed, recover the space.
1982                  */
1983
1984                 if (mod != nd->nd_tx_module) {
1985                         if (b != mbuf) {
1986                                 mbuf[-1] = 0xf0 | mod;
1987                                 nd->nd_tx_module = mod;
1988                         } else {
1989                                 b--;
1990                         }
1991                 }
1992         }
1993
1994         /*
1995          *  Send a synchronization sequence associated with the last open
1996          *  channel that sent data, and remember the time when the data was
1997          *  sent.
1998          */
1999
2000         in = nd->nd_seq_in;
2001
2002         if ((send_sync || nd->nd_seq_wait[in] != 0) && lastport >= 0) {
2003                 u8 *bb = b;
2004
2005                 /*
2006                  * Attempt the use the port that really wanted the sync.
2007                  * This gets around a race condition where the "lastport" is in
2008                  * the middle of the close() routine, and by the time we
2009                  * send this command, it will have already acked the close, and
2010                  * thus not send the sync response.
2011                  */
2012                 if (wanted_sync_port >= 0)
2013                         lastport = wanted_sync_port;
2014                 /*
2015                  * Set a flag just in case the port is in the middle of a close,
2016                  * it will not be permitted to actually close until we get an
2017                  * sync response, and clear the flag there.
2018                  */
2019                 ch = nd->nd_chan + lastport;
2020                 ch->ch_flag |= CH_WAITING_SYNC;
2021
2022                 mod = lastport >> 4;
2023
2024                 if (mod != nd->nd_tx_module) {
2025                         bb[0] = 0xf0 + mod;
2026                         bb += 1;
2027
2028                         nd->nd_tx_module = mod;
2029                 }
2030
2031                 bb = set_cmd_header(bb, lastport, 12);
2032                 *bb++ = in;
2033
2034                 nd->nd_seq_size[in] = bb - buf;
2035                 nd->nd_seq_time[in] = jiffies;
2036
2037                 if (++in >= SEQ_MAX)
2038                         in = 0;
2039
2040                 if (in != nd->nd_seq_out) {
2041                         b = bb;
2042                         nd->nd_seq_in = in;
2043                         nd->nd_unack += b - buf;
2044                 }
2045         }
2046
2047         /*
2048          *  If there are no open ports, a sync cannot be sent.
2049          *  There is nothing left to wait for anyway, so wake any
2050          *  thread waiting for an acknowledgement.
2051          */
2052
2053         else if (nd->nd_seq_wait[in] != 0) {
2054                 nd->nd_seq_wait[in] = 0;
2055
2056                 wake_up_interruptible(&nd->nd_seq_wque[in]);
2057         }
2058
2059         /*
2060          *  If there is no traffic for an interval of IDLE_MAX, then
2061          *  send a single byte packet.
2062          */
2063
2064         if (b != buf) {
2065                 nd->nd_tx_time = jiffies;
2066         } else if ((ulong)(jiffies - nd->nd_tx_time) >= IDLE_MAX) {
2067                 *b++ = 0xf0 | nd->nd_tx_module;
2068                 nd->nd_tx_time = jiffies;
2069         }
2070
2071         n = b - buf;
2072
2073         if (n >= tsafe)
2074                 pr_info("%s - n(%i) >= tsafe(%i)\n",
2075                         __func__, n, tsafe);
2076
2077         if (tsend < 0)
2078                 dgrp_dump(buf, n);
2079
2080         nd->nd_tx_work = work;
2081
2082         return n;
2083 }
2084
2085 /*
2086  * dgrp_net_read()
2087  * Data to be sent TO the PortServer from the "async." half of the driver.
2088  */
2089 static ssize_t dgrp_net_read(struct file *file, char __user *buf, size_t count,
2090                              loff_t *ppos)
2091 {
2092         struct nd_struct *nd;
2093         long n;
2094         u8 *local_buf;
2095         u8 *b;
2096         ssize_t rtn;
2097
2098         /*
2099          *  Get the node pointer, and quit if it doesn't exist.
2100          */
2101         nd = (struct nd_struct *)(file->private_data);
2102         if (!nd)
2103                 return -ENXIO;
2104
2105         if (count < UIO_MIN)
2106                 return -EINVAL;
2107
2108         /*
2109          *  Only one read/write operation may be in progress at
2110          *  any given time.
2111          */
2112
2113         /*
2114          *  Grab the NET lock.
2115          */
2116         down(&nd->nd_net_semaphore);
2117
2118         nd->nd_read_count++;
2119
2120         nd->nd_tx_ready = 0;
2121
2122         /*
2123          *  Determine the effective size of the buffer.
2124          */
2125
2126         if (nd->nd_remain > UIO_BASE)
2127                 pr_info_ratelimited("%s - nd_remain(%i) > UIO_BASE\n",
2128                                     __func__, nd->nd_remain);
2129
2130         b = local_buf = nd->nd_iobuf + UIO_BASE;
2131
2132         /*
2133          *  Generate data according to the node state.
2134          */
2135
2136         switch (nd->nd_state) {
2137         /*
2138          *  Initialize the connection.
2139          */
2140
2141         case NS_IDLE:
2142                 if (nd->nd_mon_buf)
2143                         dgrp_monitor_reset(nd);
2144
2145                 /*
2146                  *  Request a Product ID Packet.
2147                  */
2148
2149                 b[0] = 0xfb;
2150                 b[1] = 0x01;
2151                 b += 2;
2152
2153                 nd->nd_expect |= NR_IDENT;
2154
2155                 /*
2156                  *  Request a Server Capability ID Response.
2157                  */
2158
2159                 b[0] = 0xfb;
2160                 b[1] = 0x02;
2161                 b += 2;
2162
2163                 nd->nd_expect |= NR_CAPABILITY;
2164
2165                 /*
2166                  *  Request a Server VPD Response.
2167                  */
2168
2169                 b[0] = 0xfb;
2170                 b[1] = 0x18;
2171                 b += 2;
2172
2173                 nd->nd_expect |= NR_VPD;
2174
2175                 nd->nd_state = NS_WAIT_QUERY;
2176                 break;
2177
2178         /*
2179          *  We do serious communication with the server only in
2180          *  the READY state.
2181          */
2182
2183         case NS_READY:
2184                 b = dgrp_send(nd, count) + local_buf;
2185                 break;
2186
2187         /*
2188          *  Send off an error after receiving a bogus message
2189          *  from the server.
2190          */
2191
2192         case NS_SEND_ERROR:
2193                 n = strlen(nd->nd_error);
2194
2195                 b[0] = 0xff;
2196                 b[1] = n;
2197                 memcpy(b + 2, nd->nd_error, n);
2198                 b += 2 + n;
2199
2200                 dgrp_net_idle(nd);
2201                 /*
2202                  *  Set the active port count to zero.
2203                  */
2204                 dgrp_chan_count(nd, 0);
2205                 break;
2206
2207         default:
2208                 break;
2209         }
2210
2211         n = b - local_buf;
2212
2213         if (n != 0) {
2214                 nd->nd_send_count++;
2215
2216                 nd->nd_tx_byte   += n + nd->nd_link.lk_header_size;
2217                 nd->nd_tx_charge += n + nd->nd_link.lk_header_size;
2218         }
2219
2220         rtn = copy_to_user((void __user *)buf, local_buf, n);
2221         if (rtn) {
2222                 rtn = -EFAULT;
2223                 goto done;
2224         }
2225
2226         *ppos += n;
2227
2228         rtn = n;
2229
2230         if (nd->nd_mon_buf)
2231                 dgrp_monitor_data(nd, RPDUMP_CLIENT, local_buf, n);
2232
2233         /*
2234          *  Release the NET lock.
2235          */
2236 done:
2237         up(&nd->nd_net_semaphore);
2238
2239         return rtn;
2240 }
2241
2242 /**
2243  * dgrp_receive() -- decode data packets received from the remote PortServer.
2244  * @nd: pointer to a node structure
2245  */
2246 static void dgrp_receive(struct nd_struct *nd)
2247 {
2248         struct ch_struct *ch;
2249         u8 *buf;
2250         u8 *b;
2251         u8 *dbuf;
2252         char *error;
2253         long port;
2254         long dlen;
2255         long plen;
2256         long remain;
2257         long n;
2258         long mlast;
2259         long elast;
2260         long mstat;
2261         long estat;
2262
2263         char ID[3];
2264
2265         nd->nd_tx_time = jiffies;
2266
2267         ID_TO_CHAR(nd->nd_ID, ID);
2268
2269         b = buf = nd->nd_iobuf;
2270         remain = nd->nd_remain;
2271
2272         /*
2273          *  Loop to process Realport protocol packets.
2274          */
2275
2276         while (remain > 0) {
2277                 int n0 = b[0] >> 4;
2278                 int n1 = b[0] & 0x0f;
2279
2280                 if (n0 <= 12) {
2281                         port = (nd->nd_rx_module << 4) + n1;
2282
2283                         if (port >= nd->nd_chan_count) {
2284                                 error = "Improper Port Number";
2285                                 goto prot_error;
2286                         }
2287
2288                         ch = nd->nd_chan + port;
2289                 } else {
2290                         port = -1;
2291                         ch = NULL;
2292                 }
2293
2294                 /*
2295                  *  Process by major packet type.
2296                  */
2297
2298                 switch (n0) {
2299
2300                 /*
2301                  *  Process 1-byte header data packet.
2302                  */
2303
2304                 case 0:
2305                 case 1:
2306                 case 2:
2307                 case 3:
2308                 case 4:
2309                 case 5:
2310                 case 6:
2311                 case 7:
2312                         dlen = n0 + 1;
2313                         plen = dlen + 1;
2314
2315                         dbuf = b + 1;
2316                         goto data;
2317
2318                 /*
2319                  *  Process 2-byte header data packet.
2320                  */
2321
2322                 case 8:
2323                         if (remain < 3)
2324                                 goto done;
2325
2326                         dlen = b[1];
2327                         plen = dlen + 2;
2328
2329                         dbuf = b + 2;
2330                         goto data;
2331
2332                 /*
2333                  *  Process 3-byte header data packet.
2334                  */
2335
2336                 case 9:
2337                         if (remain < 4)
2338                                 goto done;
2339
2340                         dlen = get_unaligned_be16(b + 1);
2341                         plen = dlen + 3;
2342
2343                         dbuf = b + 3;
2344
2345                 /*
2346                  *  Common packet handling code.
2347                  */
2348
2349 data:
2350                         nd->nd_tx_work = 1;
2351
2352                         /*
2353                          *  Otherwise data should appear only when we are
2354                          *  in the CS_READY state.
2355                          */
2356
2357                         if (ch->ch_state < CS_READY) {
2358                                 error = "Data received before RWIN established";
2359                                 goto prot_error;
2360                         }
2361
2362                         /*
2363                          *  Assure that the data received is within the
2364                          *  allowable window.
2365                          */
2366
2367                         n = (ch->ch_s_rwin - ch->ch_s_rin) & 0xffff;
2368
2369                         if (dlen > n) {
2370                                 error = "Receive data overrun";
2371                                 goto prot_error;
2372                         }
2373
2374                         /*
2375                          *  If we received 3 or less characters,
2376                          *  assume it is a human typing, and set RTIME
2377                          *  to 10 milliseconds.
2378                          *
2379                          *  If we receive 10 or more characters,
2380                          *  assume its not a human typing, and set RTIME
2381                          *  to 100 milliseconds.
2382                          */
2383
2384                         if (ch->ch_edelay != DGRP_RTIME) {
2385                                 if (ch->ch_rtime != ch->ch_edelay) {
2386                                         ch->ch_rtime = ch->ch_edelay;
2387                                         ch->ch_flag |= CH_PARAM;
2388                                 }
2389                         } else if (dlen <= 3) {
2390                                 if (ch->ch_rtime != 10) {
2391                                         ch->ch_rtime = 10;
2392                                         ch->ch_flag |= CH_PARAM;
2393                                 }
2394                         } else {
2395                                 if (ch->ch_rtime != DGRP_RTIME) {
2396                                         ch->ch_rtime = DGRP_RTIME;
2397                                         ch->ch_flag |= CH_PARAM;
2398                                 }
2399                         }
2400
2401                         /*
2402                          *  If a portion of the packet is outside the
2403                          *  buffer, shorten the effective length of the
2404                          *  data packet to be the amount of data received.
2405                          */
2406
2407                         if (remain < plen)
2408                                 dlen -= plen - remain;
2409
2410                         /*
2411                          *  Detect if receive flush is now complete.
2412                          */
2413
2414                         if ((ch->ch_flag & CH_RX_FLUSH) != 0 &&
2415                             ((ch->ch_flush_seq - nd->nd_seq_out) & SEQ_MASK) >=
2416                             ((nd->nd_seq_in    - nd->nd_seq_out) & SEQ_MASK)) {
2417                                 ch->ch_flag &= ~CH_RX_FLUSH;
2418                         }
2419
2420                         /*
2421                          *  If we are ready to receive, move the data into
2422                          *  the receive buffer.
2423                          */
2424
2425                         ch->ch_s_rin = (ch->ch_s_rin + dlen) & 0xffff;
2426
2427                         if (ch->ch_state == CS_READY &&
2428                             (ch->ch_tun.un_open_count != 0) &&
2429                             (ch->ch_tun.un_flag & UN_CLOSING) == 0 &&
2430                             (ch->ch_cflag & CF_CREAD) != 0 &&
2431                             (ch->ch_flag & (CH_BAUD0 | CH_RX_FLUSH)) == 0 &&
2432                             (ch->ch_send & RR_RX_FLUSH) == 0) {
2433
2434                                 if (ch->ch_rin + dlen >= RBUF_MAX) {
2435                                         n = RBUF_MAX - ch->ch_rin;
2436
2437                                         memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, n);
2438
2439                                         ch->ch_rin = 0;
2440                                         dbuf += n;
2441                                         dlen -= n;
2442                                 }
2443
2444                                 memcpy(ch->ch_rbuf + ch->ch_rin, dbuf, dlen);
2445
2446                                 ch->ch_rin += dlen;
2447
2448
2449                                 /*
2450                                  *  If we are not in fastcook mode, or
2451                                  *  if there is a fastcook thread
2452                                  *  waiting for data, send the data to
2453                                  *  the line discipline.
2454                                  */
2455
2456                                 if ((ch->ch_flag & CH_FAST_READ) == 0 ||
2457                                     ch->ch_inwait != 0) {
2458                                         dgrp_input(ch);
2459                                 }
2460
2461                                 /*
2462                                  *  If there is a read thread waiting
2463                                  *  in select, and we are in fastcook
2464                                  *  mode, wake him up.
2465                                  */
2466
2467                                 if (waitqueue_active(&ch->ch_tun.un_tty->read_wait) &&
2468                                     (ch->ch_flag & CH_FAST_READ) != 0)
2469                                         wake_up_interruptible(&ch->ch_tun.un_tty->read_wait);
2470
2471                                 /*
2472                                  * Wake any thread waiting in the
2473                                  * fastcook loop.
2474                                  */
2475
2476                                 if ((ch->ch_flag & CH_INPUT) != 0) {
2477                                         ch->ch_flag &= ~CH_INPUT;
2478
2479                                         wake_up_interruptible(&ch->ch_flag_wait);
2480                                 }
2481                         }
2482
2483                         /*
2484                          *  Fabricate and insert a data packet header to
2485                          *  preced the remaining data when it comes in.
2486                          */
2487
2488                         if (remain < plen) {
2489                                 dlen = plen - remain;
2490                                 b = buf;
2491
2492                                 b[0] = 0x90 + n1;
2493                                 put_unaligned_be16(dlen, b + 1);
2494
2495                                 remain = 3;
2496                                 goto done;
2497                         }
2498                         break;
2499
2500                 /*
2501                  *  Handle Window Sequence packets.
2502                  */
2503
2504                 case 10:
2505                         plen = 3;
2506                         if (remain < plen)
2507                                 goto done;
2508
2509                         nd->nd_tx_work = 1;
2510
2511                         {
2512                                 ushort tpos   = get_unaligned_be16(b + 1);
2513
2514                                 ushort ack    = (tpos          - ch->ch_s_tpos) & 0xffff;
2515                                 ushort unack  = (ch->ch_s_tin  - ch->ch_s_tpos) & 0xffff;
2516                                 ushort notify = (ch->ch_s_treq - ch->ch_s_tpos) & 0xffff;
2517
2518                                 if (ch->ch_state < CS_READY || ack > unack) {
2519                                         error = "Improper Window Sequence";
2520                                         goto prot_error;
2521                                 }
2522
2523                                 ch->ch_s_tpos = tpos;
2524
2525                                 if (notify <= ack)
2526                                         ch->ch_s_treq = tpos;
2527                         }
2528                         break;
2529
2530                 /*
2531                  *  Handle Command response packets.
2532                  */
2533
2534                 case 11:
2535
2536                         /*
2537                          * RealPort engine fix - 03/11/2004
2538                          *
2539                          * This check did not used to be here.
2540                          *
2541                          * We were using b[1] without verifying that the data
2542                          * is actually there and valid. On a split packet, it
2543                          * might not be yet.
2544                          *
2545                          * NOTE:  I have never actually seen the failure happen
2546                          *        under Linux,  but since I have seen it occur
2547                          *        under both Solaris and HP-UX,  the assumption
2548                          *        is that it *could* happen here as well...
2549                          */
2550                         if (remain < 2)
2551                                 goto done;
2552
2553
2554                         switch (b[1]) {
2555
2556                         /*
2557                          *  Handle Open Response.
2558                          */
2559
2560                         case 11:
2561                                 plen = 6;
2562                                 if (remain < plen)
2563                                         goto done;
2564
2565                                 nd->nd_tx_work = 1;
2566
2567                                 {
2568                                         int req = b[2];
2569                                         int resp = b[3];
2570                                         port = get_unaligned_be16(b + 4);
2571
2572                                         if (port >= nd->nd_chan_count) {
2573                                                 error = "Open channel number out of range";
2574                                                 goto prot_error;
2575                                         }
2576
2577                                         ch = nd->nd_chan + port;
2578
2579                                         /*
2580                                          *  How we handle an open response depends primarily
2581                                          *  on our current channel state.
2582                                          */
2583
2584                                         switch (ch->ch_state) {
2585                                         case CS_IDLE:
2586
2587                                                 /*
2588                                                  *  Handle a delayed open.
2589                                                  */
2590
2591                                                 if (ch->ch_otype_waiting != 0 &&
2592                                                     req == ch->ch_otype_waiting &&
2593                                                     resp == 0) {
2594                                                         ch->ch_otype = req;
2595                                                         ch->ch_otype_waiting = 0;
2596                                                         ch->ch_state = CS_SEND_QUERY;
2597                                                         break;
2598                                                 }
2599                                                 goto open_error;
2600
2601                                         case CS_WAIT_OPEN:
2602
2603                                                 /*
2604                                                  *  Handle the open response.
2605                                                  */
2606
2607                                                 if (req == ch->ch_otype) {
2608                                                         switch (resp) {
2609
2610                                                         /*
2611                                                          *  On successful response, open the
2612                                                          *  port and proceed normally.
2613                                                          */
2614
2615                                                         case 0:
2616                                                                 ch->ch_state = CS_SEND_QUERY;
2617                                                                 break;
2618
2619                                                         /*
2620                                                          *  On a busy response to a persistent open,
2621                                                          *  remember that the open is pending.
2622                                                          */
2623
2624                                                         case 1:
2625                                                         case 2:
2626                                                                 if (req != OTYPE_IMMEDIATE) {
2627                                                                         ch->ch_otype_waiting = req;
2628                                                                         ch->ch_state = CS_IDLE;
2629                                                                         break;
2630                                                                 }
2631
2632                                                         /*
2633                                                          *  Otherwise the server open failed.  If
2634                                                          *  the Unix port is open, hang it up.
2635                                                          */
2636
2637                                                         default:
2638                                                                 if (ch->ch_open_count != 0) {
2639                                                                         ch->ch_flag |= CH_HANGUP;
2640                                                                         dgrp_carrier(ch);
2641                                                                         ch->ch_state = CS_IDLE;
2642                                                                         break;
2643                                                                 }
2644
2645                                                                 ch->ch_open_error = resp;
2646                                                                 ch->ch_state = CS_IDLE;
2647
2648                                                                 wake_up_interruptible(&ch->ch_flag_wait);
2649                                                         }
2650                                                         break;
2651                                                 }
2652
2653                                                 /*
2654                                                  *  Handle delayed response arrival preceding
2655                                                  *  the open response we are waiting for.
2656                                                  */
2657
2658                                                 if (ch->ch_otype_waiting != 0 &&
2659                                                     req == ch->ch_otype_waiting &&
2660                                                     resp == 0) {
2661                                                         ch->ch_otype = ch->ch_otype_waiting;
2662                                                         ch->ch_otype_waiting = 0;
2663                                                         ch->ch_state = CS_WAIT_FAIL;
2664                                                         break;
2665                                                 }
2666                                                 goto open_error;
2667
2668
2669                                         case CS_WAIT_FAIL:
2670
2671                                                 /*
2672                                                  *  Handle response to immediate open arriving
2673                                                  *  after a delayed open success.
2674                                                  */
2675
2676                                                 if (req == OTYPE_IMMEDIATE) {
2677                                                         ch->ch_state = CS_SEND_QUERY;
2678                                                         break;
2679                                                 }
2680                                                 goto open_error;
2681
2682
2683                                         case CS_WAIT_CANCEL:
2684                                                 /*
2685                                                  *  Handle delayed open response arriving before
2686                                                  *  the cancel response.
2687                                                  */
2688
2689                                                 if (req == ch->ch_otype_waiting &&
2690                                                     resp == 0) {
2691                                                         ch->ch_otype_waiting = 0;
2692                                                         break;
2693                                                 }
2694
2695                                                 /*
2696                                                  *  Handle cancel response.
2697                                                  */
2698
2699                                                 if (req == 4 && resp == 0) {
2700                                                         ch->ch_otype_waiting = 0;
2701                                                         ch->ch_state = CS_IDLE;
2702                                                         break;
2703                                                 }
2704                                                 goto open_error;
2705
2706
2707                                         case CS_WAIT_CLOSE:
2708                                                 /*
2709                                                  *  Handle a successful response to a port
2710                                                  *  close.
2711                                                  */
2712
2713                                                 if (req >= 3) {
2714                                                         ch->ch_state = CS_IDLE;
2715                                                         break;
2716                                                 }
2717                                                 goto open_error;
2718
2719 open_error:
2720                                         default:
2721                                                 {
2722                                                         error = "Improper Open Response";
2723                                                         goto prot_error;
2724                                                 }
2725                                         }
2726                                 }
2727                                 break;
2728
2729                         /*
2730                          *  Handle Synchronize Response.
2731                          */
2732
2733                         case 13:
2734                                 plen = 3;
2735                                 if (remain < plen)
2736                                         goto done;
2737                                 {
2738                                         int seq = b[2];
2739                                         int s;
2740
2741                                         /*
2742                                          * If channel was waiting for this sync response,
2743                                          * unset the flag, and wake up anyone waiting
2744                                          * on the event.
2745                                          */
2746                                         if (ch->ch_flag & CH_WAITING_SYNC) {
2747                                                 ch->ch_flag &= ~(CH_WAITING_SYNC);
2748                                                 wake_up_interruptible(&ch->ch_flag_wait);
2749                                         }
2750
2751                                         if (((seq - nd->nd_seq_out) & SEQ_MASK) >=
2752                                             ((nd->nd_seq_in - nd->nd_seq_out) & SEQ_MASK)) {
2753                                                 break;
2754                                         }
2755
2756                                         for (s = nd->nd_seq_out;; s = (s + 1) & SEQ_MASK) {
2757                                                 if (nd->nd_seq_wait[s] != 0) {
2758                                                         nd->nd_seq_wait[s] = 0;
2759
2760                                                         wake_up_interruptible(&nd->nd_seq_wque[s]);
2761                                                 }
2762
2763                                                 nd->nd_unack -= nd->nd_seq_size[s];
2764
2765                                                 if (s == seq)
2766                                                         break;
2767                                         }
2768
2769                                         nd->nd_seq_out = (seq + 1) & SEQ_MASK;
2770                                 }
2771                                 break;
2772
2773                         /*
2774                          *  Handle Sequence Response.
2775                          */
2776
2777                         case 15:
2778                                 plen = 6;
2779                                 if (remain < plen)
2780                                         goto done;
2781
2782                                 {
2783                                 /* Record that we have received the Sequence
2784                                  * Response, but we aren't interested in the
2785                                  * sequence numbers.  We were using RIN like it
2786                                  * was ROUT and that was causing problems,
2787                                  * fixed 7-13-2001 David Fries. See comment in
2788                                  * drp.h for ch_s_rin variable.
2789                                         int rin = get_unaligned_be16(b + 2);
2790                                         int tpos = get_unaligned_be16(b + 4);
2791                                 */
2792
2793                                         ch->ch_send   &= ~RR_SEQUENCE;
2794                                         ch->ch_expect &= ~RR_SEQUENCE;
2795                                 }
2796                                 goto check_query;
2797
2798                         /*
2799                          *  Handle Status Response.
2800                          */
2801
2802                         case 17:
2803                                 plen = 5;
2804                                 if (remain < plen)
2805                                         goto done;
2806
2807                                 {
2808                                         ch->ch_s_elast = get_unaligned_be16(b + 2);
2809                                         ch->ch_s_mlast = b[4];
2810
2811                                         ch->ch_expect &= ~RR_STATUS;
2812                                         ch->ch_send   &= ~RR_STATUS;
2813
2814                                         /*
2815                                          *  CH_PHYS_CD is cleared because something _could_ be
2816                                          *  waiting for the initial sense of carrier... and if
2817                                          *  carrier is high immediately, we want to be sure to
2818                                          *  wake them as soon as possible.
2819                                          */
2820                                         ch->ch_flag &= ~CH_PHYS_CD;
2821
2822                                         dgrp_carrier(ch);
2823                                 }
2824                                 goto check_query;
2825
2826                         /*
2827                          *  Handle Line Error Response.
2828                          */
2829
2830                         case 19:
2831                                 plen = 14;
2832                                 if (remain < plen)
2833                                         goto done;
2834
2835                                 break;
2836
2837                         /*
2838                          *  Handle Buffer Response.
2839                          */
2840
2841                         case 21:
2842                                 plen = 6;
2843                                 if (remain < plen)
2844                                         goto done;
2845
2846                                 {
2847                                         ch->ch_s_rsize = get_unaligned_be16(b + 2);
2848                                         ch->ch_s_tsize = get_unaligned_be16(b + 4);
2849
2850                                         ch->ch_send   &= ~RR_BUFFER;
2851                                         ch->ch_expect &= ~RR_BUFFER;
2852                                 }
2853                                 goto check_query;
2854
2855                         /*
2856                          *  Handle Port Capability Response.
2857                          */
2858
2859                         case 23:
2860                                 plen = 32;
2861                                 if (remain < plen)
2862                                         goto done;
2863
2864                                 {
2865                                         ch->ch_send   &= ~RR_CAPABILITY;
2866                                         ch->ch_expect &= ~RR_CAPABILITY;
2867                                 }
2868
2869                         /*
2870                          *  When all queries are complete, set those parameters
2871                          *  derived from the query results, then transition
2872                          *  to the READY state.
2873                          */
2874
2875 check_query:
2876                                 if (ch->ch_state == CS_WAIT_QUERY &&
2877                                     (ch->ch_expect & (RR_SEQUENCE |
2878                                                         RR_STATUS |
2879                                                         RR_BUFFER |
2880                                                         RR_CAPABILITY)) == 0) {
2881                                         ch->ch_tmax  = ch->ch_s_tsize / 4;
2882
2883                                         if (ch->ch_edelay == DGRP_TTIME)
2884                                                 ch->ch_ttime = DGRP_TTIME;
2885                                         else
2886                                                 ch->ch_ttime = ch->ch_edelay;
2887
2888                                         ch->ch_rmax = ch->ch_s_rsize / 4;
2889
2890                                         if (ch->ch_edelay == DGRP_RTIME)
2891                                                 ch->ch_rtime = DGRP_RTIME;
2892                                         else
2893                                                 ch->ch_rtime = ch->ch_edelay;
2894
2895                                         ch->ch_rlow  = 2 * ch->ch_s_rsize / 8;
2896                                         ch->ch_rhigh = 6 * ch->ch_s_rsize / 8;
2897
2898                                         ch->ch_state = CS_READY;
2899
2900                                         nd->nd_tx_work = 1;
2901                                         wake_up_interruptible(&ch->ch_flag_wait);
2902
2903                                 }
2904                                 break;
2905
2906                         default:
2907                                 goto decode_error;
2908                         }
2909                         break;
2910
2911                 /*
2912                  *  Handle Events.
2913                  */
2914
2915                 case 12:
2916                         plen = 4;
2917                         if (remain < plen)
2918                                 goto done;
2919
2920                         mlast = ch->ch_s_mlast;
2921                         elast = ch->ch_s_elast;
2922
2923                         mstat = ch->ch_s_mlast = b[1];
2924                         estat = ch->ch_s_elast = get_unaligned_be16(b + 2);
2925
2926                         /*
2927                          *  Handle modem changes.
2928                          */
2929
2930                         if (((mstat ^ mlast) & DM_CD) != 0)
2931                                 dgrp_carrier(ch);
2932
2933
2934                         /*
2935                          *  Handle received break.
2936                          */
2937
2938                         if ((estat & ~elast & EV_RXB) != 0 &&
2939                             (ch->ch_tun.un_open_count != 0) &&
2940                             I_BRKINT(ch->ch_tun.un_tty) &&
2941                             !(I_IGNBRK(ch->ch_tun.un_tty))) {
2942
2943                                 tty_buffer_request_room(&ch->port, 1);
2944                                 tty_insert_flip_char(&ch->port, 0, TTY_BREAK);
2945                                 tty_flip_buffer_push(&ch->port);
2946
2947                         }
2948
2949                         /*
2950                          *  On transmit break complete, if more break traffic
2951                          *  is waiting then send it.  Otherwise wake any threads
2952                          *  waiting for transmitter empty.
2953                          */
2954
2955                         if ((~estat & elast & EV_TXB) != 0 &&
2956                             (ch->ch_expect & RR_TX_BREAK) != 0) {
2957
2958                                 nd->nd_tx_work = 1;
2959
2960                                 ch->ch_expect &= ~RR_TX_BREAK;
2961
2962                                 if (ch->ch_break_time != 0) {
2963                                         ch->ch_send |= RR_TX_BREAK;
2964                                 } else {
2965                                         ch->ch_send &= ~RR_TX_BREAK;
2966                                         ch->ch_flag &= ~CH_TX_BREAK;
2967                                         wake_up_interruptible(&ch->ch_flag_wait);
2968                                 }
2969                         }
2970                         break;
2971
2972                 case 13:
2973                 case 14:
2974                         error = "Unrecognized command";
2975                         goto prot_error;
2976
2977                 /*
2978                  *  Decode Special Codes.
2979                  */
2980
2981                 case 15:
2982                         switch (n1) {
2983                         /*
2984                          *  One byte module select.
2985                          */
2986
2987                         case 0:
2988                         case 1:
2989                         case 2:
2990                         case 3:
2991                         case 4:
2992                         case 5:
2993                         case 6:
2994                         case 7:
2995                                 plen = 1;
2996                                 nd->nd_rx_module = n1;
2997                                 break;
2998
2999                         /*
3000                          *  Two byte module select.
3001                          */
3002
3003                         case 8:
3004                                 plen = 2;
3005                                 if (remain < plen)
3006                                         goto done;
3007
3008                                 nd->nd_rx_module = b[1];
3009                                 break;
3010
3011                         /*
3012                          *  ID Request packet.
3013                          */
3014
3015                         case 11:
3016                                 if (remain < 4)
3017                                         goto done;
3018
3019                                 plen = get_unaligned_be16(b + 2);
3020
3021                                 if (plen < 12 || plen > 1000) {
3022                                         error = "Response Packet length error";
3023                                         goto prot_error;
3024                                 }
3025
3026                                 nd->nd_tx_work = 1;
3027
3028                                 switch (b[1]) {
3029                                 /*
3030                                  *  Echo packet.
3031                                  */
3032
3033                                 case 0:
3034                                         nd->nd_send |= NR_ECHO;
3035                                         break;
3036
3037                                 /*
3038                                  *  ID Response packet.
3039                                  */
3040
3041                                 case 1:
3042                                         nd->nd_send |= NR_IDENT;
3043                                         break;
3044
3045                                 /*
3046                                  *  ID Response packet.
3047                                  */
3048
3049                                 case 32:
3050                                         nd->nd_send |= NR_PASSWORD;
3051                                         break;
3052
3053                                 }
3054                                 break;
3055
3056                         /*
3057                          *  Various node-level response packets.
3058                          */
3059
3060                         case 12:
3061                                 if (remain < 4)
3062                                         goto done;
3063
3064                                 plen = get_unaligned_be16(b + 2);
3065
3066                                 if (plen < 4 || plen > 1000) {
3067                                         error = "Response Packet length error";
3068                                         goto prot_error;
3069                                 }
3070
3071                                 nd->nd_tx_work = 1;
3072
3073                                 switch (b[1]) {
3074                                 /*
3075                                  *  Echo packet.
3076                                  */
3077
3078                                 case 0:
3079                                         nd->nd_expect &= ~NR_ECHO;
3080                                         break;
3081
3082                                 /*
3083                                  *  Product Response Packet.
3084                                  */
3085
3086                                 case 1:
3087                                         {
3088                                                 int desclen;
3089
3090                                                 nd->nd_hw_ver = (b[8] << 8) | b[9];
3091                                                 nd->nd_sw_ver = (b[10] << 8) | b[11];
3092                                                 nd->nd_hw_id = b[6];
3093                                                 desclen = ((plen - 12) > MAX_DESC_LEN) ? MAX_DESC_LEN :
3094                                                         plen - 12;
3095
3096                                                 if (desclen <= 0) {
3097                                                         error = "Response Packet desclen error";
3098                                                         goto prot_error;
3099                                                 }
3100
3101                                                 strncpy(nd->nd_ps_desc, b + 12, desclen);
3102                                                 nd->nd_ps_desc[desclen] = 0;
3103                                         }
3104
3105                                         nd->nd_expect &= ~NR_IDENT;
3106                                         break;
3107
3108                                 /*
3109                                  *  Capability Response Packet.
3110                                  */
3111
3112                                 case 2:
3113                                         {
3114                                                 int nn = get_unaligned_be16(b + 4);
3115
3116                                                 if (nn > CHAN_MAX)
3117                                                         nn = CHAN_MAX;
3118
3119                                                 dgrp_chan_count(nd, nn);
3120                                         }
3121
3122                                         nd->nd_expect &= ~NR_CAPABILITY;
3123                                         break;
3124
3125                                 /*
3126                                  *  VPD Response Packet.
3127                                  */
3128
3129                                 case 15:
3130                                         /*
3131                                          * NOTE: case 15 is here ONLY because the EtherLite
3132                                          * is broken, and sends a response to 24 back as 15.
3133                                          * To resolve this, the EtherLite firmware is now
3134                                          * fixed to send back 24 correctly, but, for backwards
3135                                          * compatibility, we now have reserved 15 for the
3136                                          * bad EtherLite response to 24 as well.
3137                                          */
3138
3139                                         /* Fallthru! */
3140
3141                                 case 24:
3142
3143                                         /*
3144                                          * If the product doesn't support VPD,
3145                                          * it will send back a null IDRESP,
3146                                          * which is a length of 4 bytes.
3147                                          */
3148                                         if (plen > 4) {
3149                                                 memcpy(nd->nd_vpd, b + 4, min(plen - 4, (long) VPDSIZE));
3150                                                 nd->nd_vpd_len = min(plen - 4, (long) VPDSIZE);
3151                                         }
3152
3153                                         nd->nd_expect &= ~NR_VPD;
3154                                         break;
3155
3156                                 default:
3157                                         goto decode_error;
3158                                 }
3159
3160                                 if (nd->nd_expect == 0 &&
3161                                     nd->nd_state == NS_WAIT_QUERY) {
3162                                         nd->nd_state = NS_READY;
3163                                 }
3164                                 break;
3165
3166                         /*
3167                          *  Debug packet.
3168                          */
3169
3170                         case 14:
3171                                 if (remain < 4)
3172                                         goto done;
3173
3174                                 plen = get_unaligned_be16(b + 2) + 4;
3175
3176                                 if (plen > 1000) {
3177                                         error = "Debug Packet too large";
3178                                         goto prot_error;
3179                                 }
3180
3181                                 if (remain < plen)
3182                                         goto done;
3183                                 break;
3184
3185                         /*
3186                          *  Handle reset packet.
3187                          */
3188
3189                         case 15:
3190                                 if (remain < 2)
3191                                         goto done;
3192
3193                                 plen = 2 + b[1];
3194
3195                                 if (remain < plen)
3196                                         goto done;
3197
3198                                 nd->nd_tx_work = 1;
3199
3200                                 n = b[plen];
3201                                 b[plen] = 0;
3202
3203                                 b[plen] = n;
3204
3205                                 error = "Client Reset Acknowledge";
3206                                 goto prot_error;
3207
3208                         default:
3209                                 goto decode_error;
3210                         }
3211                         break;
3212
3213                 default:
3214                         goto decode_error;
3215                 }
3216
3217                 b += plen;
3218                 remain -= plen;
3219         }
3220
3221         /*
3222          *  When the buffer is exhausted, copy any data left at the
3223          *  top of the buffer back down to the bottom for the next
3224          *  read request.
3225          */
3226
3227 done:
3228         if (remain > 0 && b != buf)
3229                 memcpy(buf, b, remain);
3230
3231         nd->nd_remain = remain;
3232         return;
3233
3234 /*
3235  *  Handle a decode error.
3236  */
3237
3238 decode_error:
3239         error = "Protocol decode error";
3240
3241 /*
3242  *  Handle a general protocol error.
3243  */
3244
3245 prot_error:
3246         nd->nd_remain = 0;
3247         nd->nd_state = NS_SEND_ERROR;
3248         nd->nd_error = error;
3249 }
3250
3251 /*
3252  * dgrp_net_write() -- write data to the network device.
3253  *
3254  * A zero byte write indicates that the connection to the RealPort
3255  * device has been broken.
3256  *
3257  * A non-zero write indicates data from the RealPort device.
3258  */
3259 static ssize_t dgrp_net_write(struct file *file, const char __user *buf,
3260                               size_t count, loff_t *ppos)
3261 {
3262         struct nd_struct *nd;
3263         ssize_t rtn = 0;
3264         long n;
3265         long total = 0;
3266
3267         /*
3268          *  Get the node pointer, and quit if it doesn't exist.
3269          */
3270         nd = (struct nd_struct *)(file->private_data);
3271         if (!nd)
3272                 return -ENXIO;
3273
3274         /*
3275          *  Grab the NET lock.
3276          */
3277         down(&nd->nd_net_semaphore);
3278
3279         nd->nd_write_count++;
3280
3281         /*
3282          *  Handle disconnect.
3283          */
3284
3285         if (count == 0) {
3286                 dgrp_net_idle(nd);
3287                 /*
3288                  *  Set the active port count to zero.
3289                  */
3290                 dgrp_chan_count(nd, 0);
3291                 goto unlock;
3292         }
3293
3294         /*
3295          *  Loop to process entire receive packet.
3296          */
3297
3298         while (count > 0) {
3299                 n = UIO_MAX - nd->nd_remain;
3300
3301                 if (n > count)
3302                         n = count;
3303
3304                 nd->nd_rx_byte += n + nd->nd_link.lk_header_size;
3305
3306                 rtn = copy_from_user(nd->nd_iobuf + nd->nd_remain,
3307                                      (void __user *) buf + total, n);
3308                 if (rtn) {
3309                         rtn = -EFAULT;
3310                         goto unlock;
3311                 }
3312
3313                 *ppos += n;
3314
3315                 total += n;
3316
3317                 count -= n;
3318
3319                 if (nd->nd_mon_buf)
3320                         dgrp_monitor_data(nd, RPDUMP_SERVER,
3321                                           nd->nd_iobuf + nd->nd_remain, n);
3322
3323                 nd->nd_remain += n;
3324
3325                 dgrp_receive(nd);
3326         }
3327
3328         rtn = total;
3329
3330 unlock:
3331         /*
3332          *  Release the NET lock.
3333          */
3334         up(&nd->nd_net_semaphore);
3335
3336         return rtn;
3337 }
3338
3339
3340 /*
3341  * dgrp_net_select()
3342  *  Determine whether a device is ready to be read or written to, and
3343  *  sleep if not.
3344  */
3345 static unsigned int dgrp_net_select(struct file *file,
3346                                     struct poll_table_struct *table)
3347 {
3348         unsigned int retval = 0;
3349         struct nd_struct *nd = file->private_data;
3350
3351         poll_wait(file, &nd->nd_tx_waitq, table);
3352
3353         if (nd->nd_tx_ready)
3354                 retval |= POLLIN | POLLRDNORM; /* Conditionally readable */
3355
3356         retval |= POLLOUT | POLLWRNORM;        /* Always writeable */
3357
3358         return retval;
3359 }
3360
3361 /*
3362  * dgrp_net_ioctl
3363  *
3364  * Implement those functions which allow the network daemon to control
3365  * the network parameters in the driver.  The ioctls include ones to
3366  * get and set the link speed parameters for the PortServer.
3367  */
3368 static long dgrp_net_ioctl(struct file *file, unsigned int cmd,
3369                            unsigned long arg)
3370 {
3371         struct nd_struct  *nd;
3372         int    rtn = 0;
3373         long   size = _IOC_SIZE(cmd);
3374         struct link_struct link;
3375
3376         nd = file->private_data;
3377
3378         if (_IOC_DIR(cmd) & _IOC_READ)
3379                 rtn = access_ok(VERIFY_WRITE, (void __user *) arg, size);
3380         else if (_IOC_DIR(cmd) & _IOC_WRITE)
3381                 rtn = access_ok(VERIFY_READ,  (void __user *) arg, size);
3382
3383         if (!rtn)
3384                 return rtn;
3385
3386         switch (cmd) {
3387         case DIGI_SETLINK:
3388                 if (size != sizeof(struct link_struct))
3389                         return -EINVAL;
3390
3391                 if (copy_from_user((void *)(&link), (void __user *) arg, size))
3392                         return -EFAULT;
3393
3394                 if (link.lk_fast_rate < 9600)
3395                         link.lk_fast_rate = 9600;
3396
3397                 if (link.lk_slow_rate < 2400)
3398                         link.lk_slow_rate = 2400;
3399
3400                 if (link.lk_fast_rate > 10000000)
3401                         link.lk_fast_rate = 10000000;
3402
3403                 if (link.lk_slow_rate > link.lk_fast_rate)
3404                         link.lk_slow_rate = link.lk_fast_rate;
3405
3406                 if (link.lk_fast_delay > 2000)
3407                         link.lk_fast_delay = 2000;
3408
3409                 if (link.lk_slow_delay > 10000)
3410                         link.lk_slow_delay = 10000;
3411
3412                 if (link.lk_fast_delay < 60)
3413                         link.lk_fast_delay = 60;
3414
3415                 if (link.lk_slow_delay < link.lk_fast_delay)
3416                         link.lk_slow_delay = link.lk_fast_delay;
3417
3418                 if (link.lk_header_size < 2)
3419                         link.lk_header_size = 2;
3420
3421                 if (link.lk_header_size > 128)
3422                         link.lk_header_size = 128;
3423
3424                 link.lk_fast_rate /= 8 * 1000 / dgrp_poll_tick;
3425                 link.lk_slow_rate /= 8 * 1000 / dgrp_poll_tick;
3426
3427                 link.lk_fast_delay /= dgrp_poll_tick;
3428                 link.lk_slow_delay /= dgrp_poll_tick;
3429
3430                 nd->nd_link = link;
3431
3432                 break;
3433
3434         case DIGI_GETLINK:
3435                 if (size != sizeof(struct link_struct))
3436                         return -EINVAL;
3437
3438                 if (copy_to_user((void __user *)arg, (void *)(&nd->nd_link),
3439                                  size))
3440                         return -EFAULT;
3441
3442                 break;
3443
3444         default:
3445                 return -EINVAL;
3446
3447         }
3448
3449         return 0;
3450 }
3451
3452 /**
3453  * dgrp_poll_handler() -- handler for poll timer
3454  *
3455  * As each timer expires, it determines (a) whether the "transmit"
3456  * waiter needs to be woken up, and (b) whether the poller needs to
3457  * be rescheduled.
3458  */
3459 void dgrp_poll_handler(unsigned long arg)
3460 {
3461         struct dgrp_poll_data *poll_data;
3462         struct nd_struct *nd;
3463         struct link_struct *lk;
3464         ulong time;
3465         ulong poll_time;
3466         ulong freq;
3467         ulong lock_flags;
3468
3469         poll_data = (struct dgrp_poll_data *) arg;
3470         freq = 1000 / poll_data->poll_tick;
3471         poll_data->poll_round += 17;
3472
3473         if (poll_data->poll_round >= freq)
3474                 poll_data->poll_round -= freq;
3475
3476         /*
3477          * Loop to process all open nodes.
3478          *
3479          * For each node, determine the rate at which it should
3480          * be transmitting data.  Then if the node should wake up
3481          * and transmit data now, enable the net receive select
3482          * to get the transmit going.
3483          */
3484
3485         list_for_each_entry(nd, &nd_struct_list, list) {
3486
3487                 lk = &nd->nd_link;
3488
3489                 /*
3490                  * Decrement statistics.  These are only for use with
3491                  * KME, so don't worry that the operations are done
3492                  * unlocked, and so the results are occasionally wrong.
3493                  */
3494
3495                 nd->nd_read_count -= (nd->nd_read_count +
3496                                       poll_data->poll_round) / freq;
3497                 nd->nd_write_count -= (nd->nd_write_count +
3498                                        poll_data->poll_round) / freq;
3499                 nd->nd_send_count -= (nd->nd_send_count +
3500                                       poll_data->poll_round) / freq;
3501                 nd->nd_tx_byte -= (nd->nd_tx_byte +
3502                                    poll_data->poll_round) / freq;
3503                 nd->nd_rx_byte -= (nd->nd_rx_byte +
3504                                    poll_data->poll_round) / freq;
3505
3506                 /*
3507                  * Wake the daemon to transmit data only when there is
3508                  * enough byte credit to send data.
3509                  *
3510                  * The results are approximate because the operations
3511                  * are performed unlocked, and we are inspecting
3512                  * data asynchronously updated elsewhere.  The whole
3513                  * thing is just approximation anyway, so that should
3514                  * be okay.
3515                  */
3516
3517                 if (lk->lk_slow_rate >= UIO_MAX) {
3518
3519                         nd->nd_delay = 0;
3520                         nd->nd_rate = UIO_MAX;
3521
3522                         nd->nd_tx_deposit = nd->nd_tx_charge + 3 * UIO_MAX;
3523                         nd->nd_tx_credit  = 3 * UIO_MAX;
3524
3525                 } else {
3526
3527                         long rate;
3528                         long delay;
3529                         long deposit;
3530                         long charge;
3531                         long size;
3532                         long excess;
3533
3534                         long seq_in = nd->nd_seq_in;
3535                         long seq_out = nd->nd_seq_out;
3536
3537                         /*
3538                          * If there are no outstanding packets, run at the
3539                          * fastest rate.
3540                          */
3541
3542                         if (seq_in == seq_out) {
3543                                 delay = 0;
3544                                 rate = lk->lk_fast_rate;
3545                         }
3546
3547                         /*
3548                          * Otherwise compute the transmit rate based on the
3549                          * delay since the oldest packet.
3550                          */
3551
3552                         else {
3553                                 /*
3554                                  * The actual delay is computed as the
3555                                  * time since the oldest unacknowledged
3556                                  * packet was sent, minus the time it
3557                                  * took to send that packet to the server.
3558                                  */
3559
3560                                 delay = ((jiffies - nd->nd_seq_time[seq_out])
3561                                         - (nd->nd_seq_size[seq_out] /
3562                                         lk->lk_fast_rate));
3563
3564                                 /*
3565                                  * If the delay is less than the "fast"
3566                                  * delay, transmit full speed.  If greater
3567                                  * than the "slow" delay, transmit at the
3568                                  * "slow" speed.   In between, interpolate
3569                                  * between the fast and slow speeds.
3570                                  */
3571
3572                                 rate =
3573                                   (delay <= lk->lk_fast_delay ?
3574                                     lk->lk_fast_rate :
3575                                     delay >= lk->lk_slow_delay ?
3576                                       lk->lk_slow_rate :
3577                                       (lk->lk_slow_rate +
3578                                        (lk->lk_slow_delay - delay) *
3579                                        (lk->lk_fast_rate - lk->lk_slow_rate) /
3580                                        (lk->lk_slow_delay - lk->lk_fast_delay)
3581                                       )
3582                                   );
3583                         }
3584
3585                         nd->nd_delay = delay;
3586                         nd->nd_rate = rate;
3587
3588                         /*
3589                          * Increase the transmit credit by depositing the
3590                          * current transmit rate.
3591                          */
3592
3593                         deposit = nd->nd_tx_deposit;
3594                         charge  = nd->nd_tx_charge;
3595
3596                         deposit += rate;
3597
3598                         /*
3599                          * If the available transmit credit becomes too large,
3600                          * reduce the deposit to correct the value.
3601                          *
3602                          * Too large is the max of:
3603                          *              6 times the header size
3604                          *              3 times the current transmit rate.
3605                          */
3606
3607                         size = 2 * nd->nd_link.lk_header_size;
3608
3609                         if (size < rate)
3610                                 size = rate;
3611
3612                         size *= 3;
3613
3614                         excess = deposit - charge - size;
3615
3616                         if (excess > 0)
3617                                 deposit -= excess;
3618
3619                         nd->nd_tx_deposit = deposit;
3620                         nd->nd_tx_credit  = deposit - charge;
3621
3622                         /*
3623                          * Wake the transmit task only if the transmit credit
3624                          * is at least 3 times the transmit header size.
3625                          */
3626
3627                         size = 3 * lk->lk_header_size;
3628
3629                         if (nd->nd_tx_credit < size)
3630                                 continue;
3631                 }
3632
3633
3634                 /*
3635                  * Enable the READ select to wake the daemon if there
3636                  * is useful work for the drp_read routine to perform.
3637                  */
3638
3639                 if (waitqueue_active(&nd->nd_tx_waitq) &&
3640                     (nd->nd_tx_work != 0 ||
3641                     (ulong)(jiffies - nd->nd_tx_time) >= IDLE_MAX)) {
3642                         nd->nd_tx_ready = 1;
3643
3644                         wake_up_interruptible(&nd->nd_tx_waitq);
3645
3646                         /* not needed */
3647                         /* nd->nd_flag &= ~ND_SELECT; */
3648                 }
3649         }
3650
3651
3652         /*
3653          * Schedule ourself back at the nominal wakeup interval.
3654          */
3655         spin_lock_irqsave(&poll_data->poll_lock, lock_flags);
3656
3657         poll_data->node_active_count--;
3658         if (poll_data->node_active_count > 0) {
3659                 poll_data->node_active_count++;
3660                 poll_time = poll_data->timer.expires +
3661                         poll_data->poll_tick * HZ / 1000;
3662
3663                 time = poll_time - jiffies;
3664
3665                 if (time >= 2 * poll_data->poll_tick)
3666                         poll_time = jiffies + dgrp_poll_tick * HZ / 1000;
3667
3668                 poll_data->timer.expires = poll_time;
3669                 add_timer(&poll_data->timer);
3670         }
3671
3672         spin_unlock_irqrestore(&poll_data->poll_lock, lock_flags);
3673 }