summaryrefslogtreecommitdiff
path: root/io/hpmud/dot4.c
blob: ba56e53b0325a51f954f1c94efd1adefd5b7efb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*****************************************************************************\

  dot4.c - 1284.4 support multi-point tranport driver  
 
  (c) 2005-2007 Copyright Hewlett-Packard Development Company, LP

  Permission is hereby granted, free of charge, to any person obtaining a copy 
  of this software and associated documentation files (the "Software"), to deal 
  in the Software without restriction, including without limitation the rights 
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
  of the Software, and to permit persons to whom the Software is furnished to do 
  so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

\*****************************************************************************/

#include "hpmud.h"
#include "hpmudi.h"

/*
 * This 1284.4 implementation does not support "Multiple Outstanding Transactions" which is optional.
 */

/* Write command reply back to peripheral. */
static int Dot4ForwardReply(mud_channel *pc, int fd, unsigned char *buf, int size)
{
   mud_device *pd = &msp->device[pc->dindex];
   int len=0;

   if ((len = (pd->vf.write)(fd, buf, size, HPMUD_EXCEPTION_TIMEOUT)) != size)
   {
      BUG("unable to Dot4ForwarReply: %m\n");
   }   
   return len;
}

/* Execute command from peripheral. */
static int Dot4ExecReverseCmd(mud_channel *pc, int fd, unsigned char *buf)
{
   mud_device *pd = &msp->device[pc->dindex];
   mud_channel *out_of_bound_channel;
   DOT4Cmd *pCmd;
   DOT4Reply *pReply;
   DOT4Credit *pCredit;
   DOT4CreditReply *pCreditReply;
   DOT4CreditRequest *pCreditReq;
   DOT4CreditRequestReply *pCreditReqReply;
   DOT4Error *pError;
   int len, size;
   unsigned char socket;
   static int cnt;

   pCmd = (DOT4Cmd *)buf;

   /* See if this packet is a command packet. */
   if (!(pCmd->h.psid == 0 && pCmd->h.ssid == 0))
   {
      if (pCmd->h.psid == pCmd->h.ssid)
      {
         /* Got a valid data packet handle it. This can happen when channel_read timeouts and p2hcredit=1. */
         out_of_bound_channel = &pd->channel[pCmd->h.psid];

         if (out_of_bound_channel->ta.p2hcredit <= 0)
         {
            BUG("invalid data packet credit=%d\n", out_of_bound_channel->ta.p2hcredit);
            return 0;
         }

         size = ntohs(pCmd->h.length) - sizeof(DOT4Header);
         if (size > (HPMUD_BUFFER_SIZE - out_of_bound_channel->rcnt))
         {
            BUG("invalid data packet size=%d\n", size);
            return 0;
         }
         memcpy(&out_of_bound_channel->rbuf[out_of_bound_channel->rcnt], buf+sizeof(MLCHeader), size);
         out_of_bound_channel->rcnt += size;
         if (pCmd->h.credit)
            out_of_bound_channel->ta.h2pcredit += pCmd->h.credit;  /* note, piggy back credit is 1 byte wide */ 
         out_of_bound_channel->ta.p2hcredit--; /* one data packet was read, decrement credit count */
      }
      else
      {
         len = ntohs(pCmd->h.length);
         BUG("unsolicited data packet: psid=%x, ssid=%x, length=%d, credit=%d, status=%x\n", pCmd->h.psid,
                                     pCmd->h.ssid, len, pCmd->h.credit, pCmd->h.control);
         DBG_DUMP(buf, len);
      }
      return 0;  
   }

   /* Process any command. */
   switch (pCmd->cmd)
   {
      case DOT4_CREDIT:
         pCredit = (DOT4Credit *)buf;
         out_of_bound_channel = &pd->channel[pCredit->psocket];
         out_of_bound_channel->ta.h2pcredit += ntohs(pCredit->credit);
         pCreditReply = (DOT4CreditReply *)buf;
         pCreditReply->h.length = htons(sizeof(DOT4CreditReply));
         pCreditReply->h.credit = 1;       /* transaction credit for next command */
         pCreditReply->h.control = 0;
         pCreditReply->cmd |= 0x80;
         pCreditReply->result = 0;
         pCreditReply->psocket = out_of_bound_channel->sockid;
         pCreditReply->ssocket = out_of_bound_channel->sockid;
         Dot4ForwardReply(pc, fd, (unsigned char *)pCreditReply, sizeof(DOT4CreditReply)); 
         break;
      case DOT4_CREDIT_REQUEST:
         pCreditReq = (DOT4CreditRequest *)buf;
         if (cnt++ < 5)         
            BUG("unexpected DOT4CreditRequest: cmd=%x, hid=%x, pid=%x, maxcredit=%d\n", pCreditReq->cmd,
                                         pCreditReq->psocket, pCreditReq->ssocket, ntohs(pCreditReq->maxcredit));
         socket = pCreditReq->ssocket;
         pCreditReqReply = (DOT4CreditRequestReply *)buf;
         pCreditReqReply->h.length = htons(sizeof(DOT4CreditRequestReply));
         pCreditReqReply->h.credit = 1;       /* transaction credit for next command */
         pCreditReqReply->h.control = 0;
         pCreditReqReply->cmd |= 0x80;
         pCreditReqReply->result = 0;
         pCreditReqReply->psocket = socket;
         pCreditReqReply->ssocket = socket;
         pCreditReqReply->credit = 0;
         Dot4ForwardReply(pc, fd, (unsigned char *)pCreditReqReply, sizeof(DOT4CreditRequestReply)); 
         break;
      case DOT4_ERROR:
         pError = (DOT4Error *)buf;
         BUG("unexpected DOT4Error: cmd=%x, psocket=%d, ssocket=%d, error=%x\n", pError->cmd, pError->psocket, pError->ssocket, pError->error);
         return 1;
      default:
         pReply = (DOT4Reply *)buf;
         BUG("unexpected command: cmd=%x, result=%x\n", pReply->cmd, pReply->result);
         pReply->h.length = htons(sizeof(DOT4Reply));
         pReply->h.credit = 1;       /* transaction credit for next command */
         pReply->h.control = 0;
         pReply->cmd |= 0x80;
         pReply->result = 1;
         Dot4ForwardReply(pc, fd, (unsigned char *)pReply, sizeof(DOT4Reply)); 
         break;
   }
   return 0;
}

/* Get command from peripheral and processes the reverse command. */
int __attribute__ ((visibility ("hidden"))) Dot4ReverseCmd(mud_channel *pc, int fd)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];   
   int stat=0, len, size;
   unsigned int pklen;
   unsigned char *pBuf;
   DOT4Reply *pPk;

   pPk = (DOT4Reply *)buf;

   pBuf = buf;

   /* Read packet header. */
   size = sizeof(DOT4Header);
   while (size > 0)
   {
      if ((len = (pd->vf.read)(fd, pBuf, size, HPMUD_EXCEPTION_TIMEOUT)) < 0)
      {
         BUG("unable to read Dot4ReverseCmd header: %m\n");
         stat = 1;
         goto bugout;
      }
      size-=len;
      pBuf+=len;
   }

   /* Determine packet size. */
   if ((pklen = ntohs(pPk->h.length)) > sizeof(buf))
   {
      BUG("invalid Dot4ReverseCmd packet size: size=%d\n", pklen);
      stat = 1;
      goto bugout;
   }

   /* Read packet data field. */
   size = pklen - sizeof(DOT4Header);
   while (size > 0)
   {
      if ((len = (pd->vf.read)(fd, pBuf, size, HPMUD_EXCEPTION_TIMEOUT)) < 0)
      {
         BUG("unable to read Dot4ReverseCmd data: %m exp=%zd act=%zd\n", pklen-sizeof(DOT4Header), pklen-sizeof(DOT4Header)-size);
         stat = 1;
         goto bugout;
      }
      size-=len;
      pBuf+=len;
   }

   stat = Dot4ExecReverseCmd(pc, fd, buf);

bugout:
   return stat;
}

/*
 * Get command reply from peripheral. Waits for reply then returns. Processes any reverse commands
 * while waiting for a reply.
 */
static int Dot4ReverseReply(mud_channel *pc, int fd, unsigned char *buf, int bufsize)
{
   mud_device *pd = &msp->device[pc->dindex];
   int stat=0, len, size, pklen;
   unsigned char *pBuf;
   DOT4Reply *pPk;

   pPk = (DOT4Reply *)buf;
   
   while (1)
   {
      pBuf = buf;

      /* Read packet header. */
      size = sizeof(DOT4Header);
      while (size > 0)
      {
         if ((len = (pd->vf.read)(fd, pBuf, size, 4000000)) < 0)   /* wait 4 seconds, 2 fails on PS2575 1200dpi uncompressed scanning  */
         {
            BUG("unable to read Dot4ReverseReply header: %m bytesRead=%zd\n", sizeof(DOT4Header)-size);
            stat = 2;  /* short timeout */
            goto bugout;
         }
         size-=len;
         pBuf+=len;
      }

      /* Determine packet size. */
      pklen = ntohs(pPk->h.length);
      if (pklen <= 0 || pklen > bufsize)
      {
         BUG("invalid Dot4ReverseReply packet size: size=%d, buf=%d\n", pklen, bufsize);
         stat = 1;
         goto bugout;
      }

      /* Read packet data field. */
      size = pklen - sizeof(DOT4Header);
      while (size > 0)
      {
         if ((len = (pd->vf.read)(fd, pBuf, size, HPMUD_EXCEPTION_TIMEOUT)) < 0)
         {
            BUG("unable to read Dot4ReverseReply data: %m exp=%zd act=%zd\n", pklen-sizeof(DOT4Header), pklen-sizeof(DOT4Header)-size);
            stat = 1;
            goto bugout;
         }
         size-=len;
         pBuf+=len;
      }

      /* Check for reply. */
      if (pPk->cmd & 0x80)
         break;

      stat = Dot4ExecReverseCmd(pc, fd, buf);

      if (stat != 0)
         break;

   } /* while (1) */

bugout:
   return stat;
}

int __attribute__ ((visibility ("hidden"))) Dot4Init(mud_channel *pc, int fd)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];
   int stat=0, len, n, cnt;
   DOT4Init *pCmd;
   DOT4InitReply *pReply;

   memset(buf, 0, sizeof(DOT4Init));
   pCmd = (DOT4Init *)buf;
   n = sizeof(DOT4Init);
   pCmd->h.length = htons(n);
   pCmd->h.credit = 1;       /* transaction credit for reply */
   pCmd->cmd = DOT4_INIT;
   pCmd->rev = 0x20;
   
   if ((len = (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT)) != n)
   {
      BUG("unable to write DOT4Init: %m\n");
      stat = 1;
      goto bugout;
   }

   cnt=0;
   while(1)
   {
      stat = Dot4ReverseReply(pc, fd, buf, sizeof(buf));
      pReply = (DOT4InitReply *)buf;

      if ((stat != 0) || (pReply->cmd != (0x80 | DOT4_INIT)) || (pReply->result != 0))
      {
         if (errno == EIO && cnt<1)
         {
            /* hack for usblp.c 2.6.5 */
            BUG("invalid DOT4InitReply retrying...\n");
            sleep(1);   
            cnt++;
            continue;
         }
         if (stat == 2 && cnt<1)
         {
            /* hack for Fullhouse, Swami and Northstar */
            BUG("invalid DOT4InitReply retrying command...\n");
            memset(buf, 0, sizeof(DOT4Init));
            n = sizeof(DOT4Init);
            pCmd->h.length = htons(n);
            pCmd->h.credit = 1;       /* transaction credit for reply */
            pCmd->cmd = DOT4_INIT;
            pCmd->rev = 0x20;
            (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT);
            cnt++;
            continue;
         }
         BUG("invalid DOT4InitReply: cmd=%x, result=%x\n, revision=%x\n", pReply->cmd, pReply->result, pReply->rev);
         stat = 1;
         goto bugout;
      }
      break;
   }

bugout:
   return stat;
}

int __attribute__ ((visibility ("hidden"))) Dot4Exit(mud_channel *pc, int fd)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];
   int stat=0, len, n;
   DOT4Exit *pCmd;
   DOT4ExitReply *pReply;

   memset(buf, 0, sizeof(DOT4Exit));
   pCmd = (DOT4Exit *)buf;
   n = sizeof(DOT4Exit);
   pCmd->h.length = htons(n);
   pCmd->h.credit = 1;
   pCmd->cmd = DOT4_EXIT;
   
   if ((len = (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT)) != n)
   {
      BUG("unable to write DOT4Exit: %m\n");
      stat = 1;
      goto bugout;
   }

   stat = Dot4ReverseReply(pc, fd, buf, sizeof(buf));
   pReply = (DOT4ExitReply *)buf;

   if ((stat != 0) || (pReply->cmd != (0x80 | DOT4_EXIT)) || (pReply->result != 0))
   {
      BUG("invalid DOT4ExitReply: cmd=%x, result=%x\n", pReply->cmd, pReply->result);
      stat = 1;
      goto bugout;
   }

bugout:
   return stat;
}

int __attribute__ ((visibility ("hidden"))) Dot4GetSocket(mud_channel *pc, int fd)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];
   int stat=0, len, n;
   DOT4GetSocket *pCmd;
   DOT4GetSocketReply *pReply;

   memset(buf, 0, sizeof(DOT4GetSocket));
   pCmd = (DOT4GetSocket *)buf;
   n = sizeof(DOT4GetSocket);
   len = strlen(pc->sn);
   memcpy(buf+sizeof(DOT4GetSocket), pc->sn, len);
   n += len;
   pCmd->h.length = htons(n);
   pCmd->h.credit = 1;
   pCmd->cmd = DOT4_GET_SOCKET;
   
   if ((len = (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT)) != n)
   {
      BUG("unable to write DOT4GetSocket: %m\n");
      stat = 1;
      goto bugout;
   }

   stat = Dot4ReverseReply(pc, fd, buf, sizeof(buf));
   pReply = (DOT4GetSocketReply *)buf;

   if ((stat != 0) || (pReply->cmd != (0x80 | DOT4_GET_SOCKET)) || (pReply->result != 0))
   {
      BUG("invalid DOT4GetSocketReply: cmd=%x, result=%x\n", pReply->cmd, pReply->result);
      stat = 1;
      goto bugout;
   }

   pc->sockid = pReply->socket;

   if (pc->sockid != pc->index)
      BUG("invalid sockid match sockid=%d index=%d\n", pc->sockid, pc->index);

bugout:
   return stat;
}

/* Write data to peripheral. */
int __attribute__ ((visibility ("hidden"))) Dot4ForwardData(mud_channel *pc, int fd, const void *buf, int size, int usec_timeout)
{
   mud_device *pd = &msp->device[pc->dindex];
   int stat=0, len, n;
   DOT4Header h;

   memset(&h, 0, sizeof(h));
   n = sizeof(DOT4Header) + size;
   h.length = htons(n);
   h.psid = pc->sockid;
   h.ssid = pc->sockid;
      
   if ((len = (pd->vf.write)(fd, &h, sizeof(DOT4Header), usec_timeout)) != sizeof(DOT4Header))
   {
      BUG("unable to write Dot4ForwardData header: %m\n");
      stat = 1;
      goto bugout;
   }

   if ((len = (pd->vf.write)(fd, buf, size, usec_timeout)) != size)
   {
      BUG("unable to write Dot4ForwardData: %m\n");
      stat = 1;
      goto bugout;
   }

bugout:
   return stat;
}

/* Read data from peripheral. */
int __attribute__ ((visibility ("hidden"))) Dot4ReverseData(mud_channel *pc, int fd, void *buf, int length, int usec_timeout)
{
   mud_device *pd = &msp->device[pc->dindex];
   mud_channel *out_of_bound_channel;
   int len, size, total;
   DOT4Header *pPk;

   pPk = (DOT4Header *)buf;

   while (1)
   {
      total = 0;

      /* Read packet header. */
      size = sizeof(DOT4Header);
      while (size > 0)
      {
         /* Use requested client timeout until we start reading. */
         if (total == 0)
            len = (pd->vf.read)(fd, buf+total, size, usec_timeout);
         else
            len = (pd->vf.read)(fd, buf+total, size, HPMUD_EXCEPTION_TIMEOUT);

         if (len < 0)
         {
            /* Got a timeout, if exception timeout or timeout occured after read started thats an error. */
            if (usec_timeout >= HPMUD_EXCEPTION_TIMEOUT || total > 0)
               BUG("unable to read Dot4ReverseData header: %m %s\n", pd->uri);
            goto bugout;
         }
         size-=len;
         total+=len;
      }

      /* Determine data size. */
      size = ntohs(pPk->length) - sizeof(DOT4Header);

      if (size > length)
      {
         BUG("invalid Dot4ReverseData size: size=%d, buf=%d\n", size, length);
         goto bugout;
      } 

      /* Make sure data packet is for this channel. */
      if (pPk->psid != pc->sockid && pPk->ssid != pc->sockid)
      {
         if (pPk->psid == 0 && pPk->ssid == 0)
         {
            /* Ok, got a command channel packet instead of a data packet, handle it... */
            while (size > 0)
            {
               if ((len = (pd->vf.read)(fd, buf+total, size, HPMUD_EXCEPTION_TIMEOUT)) < 0)
               {
                  BUG("unable to read Dot4ReverseData command: %m\n");
                  goto bugout;
               }
               size-=len;
               total=len;
            }
            Dot4ExecReverseCmd(pc, fd, buf);
            continue;   /* try again for data packet */
         }
         else if (pPk->psid == pPk->ssid)
         {
            /* Got a valid data packet for another channel handle it. This can happen when ReadData timeouts and p2hcredit=1. */
            out_of_bound_channel = &pd->channel[pPk->psid];
            unsigned char *pBuf;

            if (out_of_bound_channel->ta.p2hcredit <= 0)
            {
               BUG("invalid data packet credit=%d\n", out_of_bound_channel->ta.p2hcredit);
               goto bugout;
            }

            if (size > (HPMUD_BUFFER_SIZE - out_of_bound_channel->rcnt))
            {
               BUG("invalid data packet size=%d\n", size);
               goto bugout;
            }
            
            total = 0;
            pBuf = &out_of_bound_channel->rbuf[out_of_bound_channel->rcnt];
            while (size > 0)
            {
               if ((len = (pd->vf.read)(fd, pBuf+total, size, HPMUD_EXCEPTION_TIMEOUT)) < 0)
               {
                  BUG("unable to read MlcReverseData: %m\n");
                  goto bugout;
               }
               size-=len;
               total+=len;
            }

            out_of_bound_channel->rcnt += total;
            if (pPk->credit)
               out_of_bound_channel->ta.h2pcredit += pPk->credit;  /* note, piggy back credit is 1 byte wide */ 
            out_of_bound_channel->ta.p2hcredit--; /* one data packet was read, decrement credit count */
            continue;   /* try again for data packet */
         }
         else
         {
            DOT4Cmd *pCmd = (DOT4Cmd *)buf;
            BUG("invalid Dot4ReverseData state: unexpected packet psid=%x, ssid=%x, cmd=%x\n", pPk->psid, pPk->ssid, pCmd->cmd);
            goto bugout;
         }
      }

      if (pPk->credit)
      {
         pc->ta.h2pcredit += pPk->credit;  /* note, piggy back credit is 1 byte wide */ 
      }

      total = 0;  /* eat packet header */
   
      /* Read packet data field with exception_timeout. */
      while (size > 0)
      {
         if ((len = (pd->vf.read)(fd, buf+total, size, HPMUD_EXCEPTION_TIMEOUT)) < 0)
         {
            BUG("unable to read Dot4ReverseData: %m\n");
            goto bugout;
         }
         size-=len;
         total+=len;
      }
      break; /* done reading data packet */
   }  /* while (1) */

bugout:
   return total;
}

int __attribute__ ((visibility ("hidden"))) Dot4OpenChannel(mud_channel *pc, int fd)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];
   int stat=0, len, n;
   DOT4OpenChannel *pCmd;
   DOT4OpenChannelReply *pReply;

   memset(buf, 0, sizeof(DOT4OpenChannel));
   pCmd = (DOT4OpenChannel *)buf;
   n = sizeof(DOT4OpenChannel);
   pCmd->h.length = htons(n);
   pCmd->h.credit = 1;
   pCmd->cmd = DOT4_OPEN_CHANNEL;
   pCmd->psocket = pc->sockid;
   pCmd->ssocket = pc->sockid;
   pCmd->maxp2s = htons(HPMUD_BUFFER_SIZE);  /* max primary to secondary packet size in bytes */
   pCmd->maxs2p = htons(HPMUD_BUFFER_SIZE);  /* max secondary to primary packet size in bytes */
   pCmd->maxcredit = htons(0xffff);          /* "unlimited credit" mode, give primary (sender) as much credit as possible */
   
   if ((len = (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT)) != n)
   {
      BUG("unable to write Dot4OpenChannel: %m\n");
      stat = 1;
      goto bugout;
   }

   stat = Dot4ReverseReply(pc, fd, buf, sizeof(buf));
   pReply = (DOT4OpenChannelReply *)buf;

   if ((stat != 0) || (pReply->cmd != (0x80 | DOT4_OPEN_CHANNEL)) || (pReply->result != 0))
   {
      BUG("invalid Dot4OpenChannelReply: cmd=%x, result=%x\n", pReply->cmd, pReply->result);
      stat = 1;
      goto bugout;
   }

   pc->ta.h2psize = ntohs(pReply->maxp2s);
   pc->ta.p2hsize = ntohs(pReply->maxs2p);
   pc->ta.h2pcredit = ntohs(pReply->credit);

bugout:
   return stat;
}

int __attribute__ ((visibility ("hidden"))) Dot4CloseChannel(mud_channel *pc, int fd)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];
   int stat=0, len, n;
   DOT4CloseChannel *pCmd;
   DOT4CloseChannelReply *pReply;

   memset(buf, 0, sizeof(DOT4CloseChannel));
   pCmd = (DOT4CloseChannel *)buf;
   n = sizeof(DOT4CloseChannel);
   pCmd->h.length = htons(n);
   pCmd->h.credit = 1;
   pCmd->cmd = DOT4_CLOSE_CHANNEL;
   pCmd->psocket = pc->sockid;
   pCmd->ssocket = pc->sockid;

   if ((len = (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT)) != n)
   {
      BUG("unable to write Dot4CloseChannel: %m\n");
      stat = 1;
      goto bugout;
   }

   stat = Dot4ReverseReply(pc, fd, buf, sizeof(buf));
   pReply = (DOT4CloseChannelReply *)buf;

   if ((stat != 0) || (pReply->cmd != (0x80 | DOT4_CLOSE_CHANNEL)) || (pReply->result != 0))
   {
      BUG("invalid Dot4CloseChannelReply: cmd=%x, result=%x\n", pReply->cmd, pReply->result);
      stat = 1;
      goto bugout;
   }

bugout:
   return stat;
}

int __attribute__ ((visibility ("hidden"))) Dot4Credit(mud_channel *pc, int fd, unsigned short credit)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];
   int stat=0, len, n;
   DOT4Credit *pCmd;
   DOT4CreditReply *pReply;

   memset(buf, 0, sizeof(DOT4Credit));
   pCmd = (DOT4Credit *)buf;
   n = sizeof(DOT4Credit);
   pCmd->h.length = htons(n);
   pCmd->h.credit = 1;
   pCmd->cmd = DOT4_CREDIT;
   pCmd->psocket = pc->sockid;
   pCmd->ssocket = pc->sockid;
   pCmd->credit = htons(credit);                /* set peripheral to host credit */
   
   if ((len = (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT)) != n)
   {
      BUG("unable to write Dot4Credit: %m\n");
      stat = 1;
      goto bugout;
   }

   stat = Dot4ReverseReply(pc, fd, buf, sizeof(buf));
   pReply = (DOT4CreditReply *)buf;

   if ((stat != 0) || (pReply->cmd != (0x80 | DOT4_CREDIT)) || (pReply->result != 0))
   {
      BUG("invalid Dot4CreditReply: cmd=%x, result=%x\n", pReply->cmd, pReply->result);
      stat = 1;
      goto bugout;
   }

   pc->ta.p2hcredit += credit;

bugout:
   return stat;
}

int __attribute__ ((visibility ("hidden"))) Dot4CreditRequest(mud_channel *pc, int fd, unsigned short credit)
{
   mud_device *pd = &msp->device[pc->dindex];
   unsigned char buf[HPMUD_BUFFER_SIZE];
   int stat=0, len, n;
   DOT4CreditRequest *pCmd;
   DOT4CreditRequestReply *pReply;

   memset(buf, 0, sizeof(DOT4CreditRequest));
   pCmd = (DOT4CreditRequest *)buf;
   n = sizeof(DOT4CreditRequest);
   pCmd->h.length = htons(n);
   pCmd->h.credit = 1;
   pCmd->cmd = DOT4_CREDIT_REQUEST;
   pCmd->psocket = pc->sockid;
   pCmd->ssocket = pc->sockid;
   //   pCmd->maxcredit = htons(credit);                /* request host to peripheral credit */
   pCmd->maxcredit = htons(0xffff);                /* request host to peripheral credit */
   
   if ((len = (pd->vf.write)(fd, pCmd, n, HPMUD_EXCEPTION_TIMEOUT)) != n)
   {
      BUG("unable to write Dot4CreditRequest: %m\n");
      stat = 1;
      goto bugout;
   }

   stat = Dot4ReverseReply(pc, fd, buf, sizeof(buf));
   pReply = (DOT4CreditRequestReply *)buf;

   if ((stat != 0) || (pReply->cmd != (0x80 | DOT4_CREDIT_REQUEST)) || (pReply->result != 0))
   {
      BUG("invalid Dot4CreditRequestReply: cmd=%x, result=%x\n", pReply->cmd, pReply->result);
      stat = 1;
      goto bugout;
   }

   pc->ta.h2pcredit += ntohs(pReply->credit);

bugout:
   return stat;
}