1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * dspStatus.c
30 *
31 * From Mike Shoemaker v01.04 06/15/90 mbs
32 *    Modified for MP, 1996 by Tuyen Nguyen
33 *   Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
34 */
35
36#include <sys/errno.h>
37#include <sys/types.h>
38#include <sys/param.h>
39#include <machine/spl.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/proc.h>
43#include <sys/filedesc.h>
44#include <sys/fcntl.h>
45#include <sys/mbuf.h>
46#include <sys/socket.h>
47
48#include <netat/sysglue.h>
49#include <netat/appletalk.h>
50#include <netat/at_pcb.h>
51#include <netat/adsp.h>
52#include <netat/adsp_internal.h>
53
54int calcSendQ(CCBPtr);
55
56/*
57 * calcSendFree
58 *
59 * INPUTS:
60 *		sp		ADSP Stream
61 * OUTPUTS:
62 *		# of bytes avail in local send queue
63 */
64int CalcSendQFree(sp)		/* (CCBPtr sp) */
65    CCBPtr sp;
66{
67    int bytes;
68
69    bytes = calcSendQ(sp);
70    bytes = sp->sbuflen - bytes;
71
72    if (bytes < 0)
73	return 0;
74    return bytes;
75}
76
77int
78calcSendQ(sp)
79    CCBPtr sp;
80{
81    register gbuf_t *mp;
82    int bytes = 0;
83
84    if (sp->sData) {		/* There is data in buffer */
85	if ((mp = sp->sbuf_mb)) {
86	    do {
87		bytes += gbuf_msgsize(mp);
88		mp = gbuf_next(mp);
89	    } while (mp);
90	}
91	if ((mp = sp->csbuf_mb))
92	    bytes += gbuf_msgsize(mp);
93    }
94    return bytes;
95}
96
97/*
98 * dspStatus
99 *
100 * INPUTS:
101 * 	--> ccbRefNum		refnum of connection end
102 *
103 * OUTPUTS:
104 *	<-- statusCCB		Pointer to the connection control block
105 *	<-- sendQPending	bytes waiting to be sent or acknowledged
106 *	<-- sendQFree		available buffer in bytes of send queue
107 *	<-- recvQPending	bytes waiting to be read from queue
108 *	<-- recvQFree		available buffer in bytes of receive queue
109 *
110 * ERRORS:
111 *	errRefNum		bad connection refnum
112 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
113*/
114int adspStatus(sp, pb)	/* (DSPPBPtr pb) */
115    CCBPtr sp;
116    register struct adspcmd *pb;
117{
118    short bytes;
119
120    if (sp == 0) {
121	pb->ioResult = errRefNum;
122	return EINVAL;
123    }
124
125    pb->u.statusParams.ccbPtr 	= (TPCCB)sp;
126
127    /*
128     * pending bytes in send queue
129     */
130    if (sp->sData)
131	bytes = calcSendQ(sp);
132    else
133	bytes = 0;
134    pb->u.statusParams.sendQPending = bytes;
135
136				/* available buffer space in send queue */
137    pb->u.statusParams.sendQFree = CalcSendQFree(sp);
138
139    /*
140     * pending bytes in recv queue
141     */
142    if (sp->rData)
143	bytes = calcRecvQ(sp);
144    else
145	bytes = 0;
146    pb->u.statusParams.recvQPending = bytes;
147
148				/* available buffer space in receive queue */
149    pb->u.statusParams.recvQFree = CalcRecvWdw(sp);
150
151    pb->ioResult = 0;
152    adspioc_ack(0, (gbuf_t *)pb->ioc, pb->gref);
153    return 0;
154
155}
156