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