]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_Write.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / bsd / netat / adsp_Write.c
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 /* dspWrite.c
29 * From Mike Shoemaker v01.13 06/21/90 mbs for MacOS
30 */
31 /*
32 * Change log:
33 * 06/29/95 - Modified to handle flow control for writing (Tuyen Nguyen)
34 * 09/07/95 - Modified for performance (Tuyen Nguyen)
35 * Modified for MP, 1996 by Tuyen Nguyen
36 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
37 */
38
39 #include <sys/errno.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <machine/spl.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/filedesc.h>
47 #include <sys/fcntl.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50
51 #include <netat/sysglue.h>
52 #include <netat/appletalk.h>
53 #include <netat/at_pcb.h>
54 #include <netat/debug.h>
55 #include <netat/adsp.h>
56 #include <netat/adsp_internal.h>
57
58 void completepb();
59
60 /*
61 * FillSendQueue
62 *
63 * INPUTS:
64 * sp stream
65 * OUTPUTS:
66 * none
67 */
68 int FillSendQueue(sp, pb) /* (CCBPtr sp) */
69 register CCBPtr sp;
70 register struct adspcmd *pb; /* The write PB we're playing with */
71 {
72 gbuf_t *mb, *nmb;
73 int eom; /* True if should set eom in header */
74 int cnt; /* # of bytes in this write */
75 int err = 0;
76 int s;
77
78 cnt = pb->u.ioParams.reqCount - pb->u.ioParams.actCount;
79 eom = pb->u.ioParams.eom ? F_EOM : 0;
80
81 if (cnt == 0 && eom == 0) /* Nothing to do here, complete it */
82 goto unlink;
83
84 /* The 1st mbuf in the pb->mp chain (mb) is the adspcmd structure.
85 The 2nd mbuf (nmb) will be the beginning of the data. */
86 mb = pb->mp;
87 nmb = gbuf_cont(mb);
88 if (gbuf_len(mb) > sizeof(struct adspcmd)) {
89 if ((nmb = gbuf_dupb(mb)) == 0) {
90 gbuf_wset(mb,sizeof(struct adspcmd));
91 err = errDSPQueueSize;
92 goto unlink;
93 }
94 gbuf_wset(mb,sizeof(struct adspcmd));
95 gbuf_rinc(nmb,sizeof(struct adspcmd));
96 gbuf_cont(nmb) = gbuf_cont(mb);
97 } else if (nmb == 0) {
98 if ((nmb = gbuf_alloc(1, PRI_LO)) == 0) {
99 err = errENOBUFS;
100 goto unlink;
101 }
102 }
103 gbuf_cont(mb) = 0;
104
105 ATDISABLE(s, sp->lock);
106 sp->sData = 1; /* note that there is data to send */
107 if ((mb = sp->csbuf_mb)) { /* add to the current message */
108 gbuf_linkb(mb, nmb);
109 } else
110 sp->csbuf_mb = nmb; /* mark the buffer we are currently filling */
111 if (eom) {
112 if ((mb = sp->sbuf_mb)) {
113 while (gbuf_next(mb))
114 mb = gbuf_next(mb);
115 gbuf_next(mb) = sp->csbuf_mb; /* add the current item */
116 } else
117 sp->sbuf_mb = sp->csbuf_mb;
118 sp->csbuf_mb = 0; /* if its done, no current buffer */
119 }
120 pb->u.ioParams.actCount += cnt; /* Update count field in param blk */
121 ATENABLE(s, sp->lock);
122
123 if (pb->u.ioParams.actCount == pb->u.ioParams.reqCount) {
124 /* Write is complete */
125 unlink:
126 if (pb->u.ioParams.flush) /* flush the send Q? */
127 sp->writeFlush = 1;
128
129 pb->ioResult = err;
130 if (err)
131 atalk_notify(sp->gref, EIO);
132 gbuf_freem(pb->mp);
133 }
134
135 return 0;
136 } /* FillSendQueue */
137
138 /*
139 * dspWrite
140 *
141 * INPUTS:
142 * --> ccbRefNum refnum of connection end
143 * --> reqCount requested number of bytes to write
144 * --> dataPtr pointer to buffer for reading bytes into
145 * --> eom one if end-of-message, zero otherwise
146 *
147 * OUTPUTS:
148 * <-- actCount actual number of bytes written
149 *
150 * ERRORS:
151 * errRefNum bad connection refnum
152 * errState connection is not open
153 * errAborted request aborted by Remove or Close call
154 */
155 int adspWrite(sp, pb) /* (DSPPBPtr pb) */
156 CCBPtr sp;
157 struct adspcmd *pb;
158 {
159 int s;
160
161 if (sp == 0) {
162 pb->ioResult = errRefNum;
163 return EINVAL; /* no stream, so drop the message */
164 }
165
166 ATDISABLE(s, sp->lock);
167 if (sp->state != sOpen) { /* Not allowed */
168 pb->ioResult = errState;
169 ATENABLE(s, sp->lock);
170 atalk_notify(sp->gref, ENOTCONN);
171 gbuf_freem(pb->mp);
172 return 0;
173 }
174
175 pb->u.ioParams.actCount = 0; /* Set # of bytes so far to zero */
176 ATENABLE(s, sp->lock);
177
178 FillSendQueue(sp, pb); /* Copy from write param block to send queue */
179
180 CheckSend(sp); /* See if we should send anything */
181 return 0;
182 }
183
184 #ifdef notdef
185 int adsp_check = 1;
186
187 CheckQueue(sp)
188 CCBPtr sp;
189 {
190 register gbuf_t *mp, *tmp;
191 unsigned char current;
192 int current_valid = 0;
193
194 if (adsp_check == 0)
195 return;
196 if (mp = sp->sbuf_mb) {
197 current = *mp->b_rptr;
198 current_valid = 1;
199 while (mp) {
200 tmp = mp;
201 while (tmp) {
202 current = CheckData(tmp->b_rptr, tmp->b_wptr - tmp->b_rptr,
203 current);
204 tmp = tmp->b_cont;
205 }
206 mp = mp->b_next;
207 }
208 }
209 if (mp = sp->csbuf_mb) {
210 if (current_valid == 0)
211 current = *mp->b_rptr;
212 tmp = mp;
213 while (tmp) {
214 current = CheckData(tmp->b_rptr, tmp->b_wptr - tmp->b_rptr,
215 current);
216 tmp = tmp->b_cont;
217 }
218 }
219 }
220
221
222 int adsp_bad_block_count;
223 char *adsp_bad_block;
224
225 CheckData(block, size, current)
226 char *block;
227 int size;
228 u_char current;
229 {
230 register int anError = 0;
231 register int i;
232
233 for (i = 0; i < size; i++) {
234 if ((block[i] & 0xff) != (current & 0xff)) {
235 if (!anError) {
236 adsp_bad_block = block;
237 }
238 anError++;
239 }
240 current++;
241 }
242
243 if (anError) {
244 adsp_bad_block_count++;
245 }
246 return current;
247 }
248 #endif