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