]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_stream.c
d81504a8046319c6a7e2d35e229ee54803321e84
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1995-1998 Apple Computer, Inc.
25 * All Rights Reserved.
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.
33 #include <sys/errno.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <machine/spl.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/filedesc.h>
41 #include <sys/fcntl.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
46 #include <sys/ioctl.h>
47 #include <sys/malloc.h>
51 #include <netat/sysglue.h>
52 #include <netat/appletalk.h>
53 #include <netat/ddp.h>
54 #include <netat/at_snmp.h>
55 #include <netat/at_pcb.h>
56 #include <netat/debug.h>
57 #include <netat/at_var.h>
58 #include <netat/adsp.h>
59 #include <netat/adsp_internal.h>
63 static void adsp_iocack();
64 static void adsp_iocnak();
65 void adsp_dequeue_ccb();
66 unsigned char adspAssignSocket();
67 int adspallocate(), adsprelease();
70 atlock_t adspall_lock
;
71 atlock_t adspgen_lock
;
77 char adsp_inputC
[256];
78 CCB
*adsp_inputQ
[256];
80 extern at_ifaddr_t
*ifID_home
;
93 switch (gbuf_type(mp
)) {
95 p
= (at_ddp_t
*)gbuf_rptr(mp
);
96 ATDISABLE(s
, adspall_lock
);
97 sp
= adsp_inputQ
[p
->dst_socket
];
98 if ((sp
== 0) || (sp
->gref
==0) || (sp
->state
==sClosed
))
100 ATENABLE(s
, adspall_lock
);
104 else if (sp
->otccbLink
!= 0) {
106 if ((sp
->remoteAddress
.a
.node
== p
->src_node
)
107 && (sp
->remoteAddress
.a
.socket
== p
->src_socket
)
108 && (sp
->remoteAddress
.a
.net
== NET_VALUE(p
->src_net
)))
110 } while ((sp
= sp
->otccbLink
) != 0);
113 ATENABLE(s
, adspall_lock
);
120 if (sp
->deferred_mb
) {
121 for (mb
=sp
->deferred_mb
; gbuf_next(mb
); mb
=gbuf_next(mb
)) ;
124 sp
->deferred_mb
= mp
;
125 ATENABLE(s
, adspall_lock
);
128 ATDISABLE(l
, sp
->lockRemove
);
130 ATENABLE(l
, adspall_lock
);
132 adsp_rput(sp
->gref
, mp
);
133 if ((mp
= sp
->deferred_mb
) != 0) {
134 sp
->deferred_mb
= gbuf_next(mp
);
139 ATENABLE(s
, sp
->lockRemove
);
144 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
148 #ifdef APPLETALK_DEBUG
149 kprintf("unexpected MSG_IOCTL in adsp_input()");
162 int adsp_readable(gref
)
170 * we don't have the structure we need to determine
171 * if there's data available... we return readable in
172 * this case to keep from hanging up in the select
173 * a subsequent read will run into the same missing data
174 * structure and return an error... the ATselect code does
175 * this if it can't retrieve the 'gref' structure from the
176 * file table for the fd specified
180 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
186 int adsp_writeable(gref
)
194 * we don't have the structure we need to determine
195 * if there's room available... we return writeable in
196 * this case to keep from hanging up in the select
197 * a subsequent write will run into the same missing data
198 * structure and return an error... the ATselect code does
199 * this if it can't retrieve the 'gref' structure from the
200 * file table for the fd specified
204 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
205 ATDISABLE(s
, sp
->lock
);
206 rc
= CalcSendQFree(sp
);
207 ATENABLE(s
, sp
->lock
);
212 static void adsp_init()
217 bzero(adsp_pidM
, sizeof(adsp_pidM
));
218 bzero(adsp_inputC
, sizeof(adsp_inputC
));
219 bzero(adsp_inputQ
, sizeof(adsp_inputQ
));
224 * ADSP open and close routines. These routines
225 * initalize and release the ADSP structures. They do not
226 * have anything to do with "connections"
238 if (!adspAllocateCCB(gref
))
239 return(ENOBUFS
); /* can't get buffers */
241 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
242 gref
->readable
= adsp_readable
;
243 gref
->writeable
= adsp_writeable
;
244 ATDISABLE(s
, adspall_lock
);
245 if ((sp
->otccbLink
= ccb_used_list
) != 0)
246 sp
->otccbLink
->ccbLink
= sp
;
248 ATENABLE(s
, adspall_lock
);
256 unsigned char localSocket
;
258 /* make sure we've not yet removed the CCB (e.g., due to TrashSession) */
259 ATDISABLE(l
, adspgen_lock
);
261 CCBPtr sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
262 ATDISABLE(s
, sp
->lock
);
263 ATENABLE(s
, adspgen_lock
);
264 localSocket
= sp
->localSocket
;
265 ATENABLE(l
, sp
->lock
);
270 adsp_dequeue_ccb(sp
);
271 gbuf_freeb((gbuf_t
*)gref
->info
);
274 ATENABLE(l
, adspgen_lock
);
284 * ADSP streams read put and service routines.
287 void adsp_rput(gref
, mp
)
288 gref_t
*gref
; /* READ queue */
291 switch (gbuf_type(mp
)) {
295 switch (adspReadHandler(gref
, mp
)) {
297 atalk_putnext(gref
, mp
);
304 #ifdef APPLETALK_DEBUG
305 kprintf("adsp_rput received MSG_ERROR");
309 CheckReadQueue(gbuf_rptr(((gbuf_t
*)gref
->info
)));
310 CheckSend(gbuf_rptr(((gbuf_t
*)gref
->info
)));
312 switch (gbuf_type(mp
)) {
316 if (adspReadHandler(gref
, mp
) == STR_PUTNEXT
)
317 atalk_putnext(gref
, mp
);
320 atalk_putnext(gref
, mp
);
331 * ADSP streams write put and service routines.
335 int adsp_wput(gref
, mp
)
336 gref_t
*gref
; /* WRITE queue */
346 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
350 if (gbuf_type(mp
) == MSG_IOCTL
) {
351 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
352 switch (iocbp
->ioc_cmd
) {
357 if (gbuf_cont(mp
) == NULL
) {
358 iocbp
->ioc_rval
= -1;
359 adsp_iocnak(gref
, mp
, EINVAL
);
361 v
= *(unsigned char *)gbuf_rptr(gbuf_cont(mp
));
362 ATDISABLE(s
, adspall_lock
);
364 && ((v
> DDP_SOCKET_LAST
) || (v
< 2)
365 || ddp_socket_inuse(v
, DDP_ADSP
))) {
366 ATENABLE(s
, adspall_lock
);
367 iocbp
->ioc_rval
= -1;
368 adsp_iocnak(gref
, mp
, EINVAL
);
372 ATENABLE(s
, adspall_lock
);
373 if ((v
= adspAssignSocket(gref
, 0)) == 0) {
374 iocbp
->ioc_rval
= -1;
375 adsp_iocnak(gref
, mp
, EINVAL
);
381 adsp_pidM
[v
] = sp
->pid
;
382 ATENABLE(s
, adspall_lock
);
383 adsp_dequeue_ccb(sp
);
385 *(unsigned char *)gbuf_rptr(gbuf_cont(mp
)) = v
;
388 adsp_iocack(gref
, mp
);
398 if (((xm
= gbuf_cont(mp
)) == NULL
)
399 && ((xm
= gbuf_alloc(sizeof(at_inet_t
), PRI_MED
)) == NULL
)) {
400 iocbp
->ioc_rval
= -1;
401 adsp_iocnak(gref
, mp
, ENOBUFS
);
405 gbuf_wset(xm
,sizeof(at_inet_t
));
406 addr
= (at_inet_t
*)gbuf_rptr(xm
);
407 if (iocbp
->ioc_cmd
== ADSPGETSOCK
) {
408 /* Obtain Network and Node Id's from DDP */
409 /* *** was ddp_get_cfg() *** */
410 addr
->net
= ifID_home
->ifThisNode
.s_net
;
411 addr
->node
= ifID_home
->ifThisNode
.s_node
;
412 addr
->socket
= (sp
)? sp
->localSocket
: 0;
415 *addr
= sp
->remoteAddress
.a
;
422 adsp_iocack(gref
, mp
);
425 case DDP_IOC_GET_CFG
:
426 /* respond to an DDP_IOC_GET_CFG sent on an adsp fd */
427 if (((xm
= gbuf_cont(mp
)) == NULL
) &&
428 (xm
= gbuf_alloc(sizeof(ddp_addr_t
), PRI_MED
)) == NULL
) {
429 iocbp
->ioc_rval
= -1;
430 adsp_iocnak(gref
, mp
, ENOBUFS
);
434 gbuf_wset(xm
, sizeof(ddp_addr_t
));
435 /* Obtain Network and Node Id's from DDP */
437 /* *** was ddp_get_cfg() *** */
439 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(mp
));
440 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
441 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
442 cfgp
->inet
.socket
= (sp
)? sp
->localSocket
: 0;
443 cfgp
->ddptype
= DDP_ADSP
;
446 adsp_iocack(gref
, mp
);
454 ATDISABLE(s
, sp
->lockClose
);
455 rc
= adspWriteHandler(gref
, mp
);
456 ATENABLE(s
, sp
->lockClose
);
460 if (gbuf_type(mp
) == MSG_IOCTL
) {
461 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
462 iocbp
->ioc_private
= (void *)gref
;
478 void adspioc_ack(errno
, m
, gref
)
487 iocbp
= (ioc_t
*) gbuf_rptr(m
);
489 iocbp
->ioc_error
= errno
; /* set the errno */
490 iocbp
->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
491 if (gbuf_type(m
) == MSG_IOCTL
) /* if an ioctl, this is an ack */
492 gbuf_set_type(m
, MSG_IOCACK
); /* and ALWAYS update the user */
493 /* ioctl structure */
494 trace_mbufs(D_M_ADSP
,"A ", m
);
498 static void adsp_iocack(gref
, m
)
502 if (gbuf_type(m
) == MSG_IOCTL
)
503 gbuf_set_type(m
, MSG_IOCACK
);
506 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
508 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
514 static void adsp_iocnak(gref
, m
, err
)
519 if (gbuf_type(m
) == MSG_IOCTL
)
520 gbuf_set_type(m
, MSG_IOCNAK
);
521 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
525 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
;
528 gbuf_freem(gbuf_cont(m
));
535 adspAssignSocket(gref
, flag
)
539 unsigned char sVal
, sMax
, sMin
, sSav
, inputC
;
543 sMax
= flag
? DDP_SOCKET_LAST
-46 : DDP_SOCKET_LAST
-6;
544 sMin
= DDP_SOCKET_1st_DYNAMIC
;
546 ATDISABLE(s
, adspall_lock
);
547 for (inputC
=255, sVal
=sMax
; sVal
>= sMin
; sVal
--) {
548 if (!ddp_socket_inuse(sVal
, DDP_ADSP
))
551 if (adsp_inputC
[sVal
] &&
552 /* meaning that raw DDP doesn't have it */
553 (adsp_inputC
[sVal
] < inputC
)
554 && (adsp_inputQ
[sVal
]->state
== sOpen
)) {
555 inputC
= adsp_inputC
[sVal
];
561 if (!flag
|| (inputC
== 255)) {
562 ATENABLE(s
, adspall_lock
);
567 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
568 ATENABLE(s
, adspall_lock
);
569 adsp_dequeue_ccb(sp
);
570 ATDISABLE(s
, adspall_lock
);
572 sp
->otccbLink
= adsp_inputQ
[sVal
];
573 adsp_inputQ
[sVal
] = sp
;
575 adsp_pidM
[sVal
] = sp
->pid
;
576 ATENABLE(s
, adspall_lock
);
581 adspDeassignSocket(sp
)
590 dPrintf(D_M_ADSP
, D_L_TRACE
, ("adspDeassignSocket: pid=%d,s=%d\n",
591 sp
->pid
, sp
->localSocket
));
592 ATDISABLE(s
, adspall_lock
);
593 sVal
= sp
->localSocket
;
594 if ((curr_sp
= adsp_inputQ
[sVal
]) != 0) {
596 while (curr_sp
!= sp
) {
598 curr_sp
= curr_sp
->otccbLink
;
601 ATDISABLE(l
, sp
->lockRemove
);
603 prev_sp
->otccbLink
= sp
->otccbLink
;
605 adsp_inputQ
[sVal
] = sp
->otccbLink
;
606 ATENABLE(l
, sp
->lockRemove
);
607 if (adsp_inputQ
[sVal
])
610 pid
= adsp_pidM
[sVal
];
611 adsp_inputC
[sVal
] = 0;
617 ATENABLE(s
, adspall_lock
);
621 ATENABLE(s
, adspall_lock
);
623 dPrintf(D_M_ADSP
, D_L_ERROR
,
624 ("adspDeassignSocket: closing, no CCB block, trouble ahead\n"));
626 } /* adspDeassignSocket */
629 * remove CCB from the use list
637 ATDISABLE(s
, adspall_lock
);
638 if (sp
== ccb_used_list
) {
639 if ((ccb_used_list
= sp
->otccbLink
) != 0)
640 sp
->otccbLink
->ccbLink
= 0;
641 } else if (sp
->ccbLink
) {
642 if ((sp
->ccbLink
->otccbLink
= sp
->otccbLink
) != 0)
643 sp
->otccbLink
->ccbLink
= sp
->ccbLink
;
648 ATENABLE(s
, adspall_lock
);
651 void SndMsgUp(gref
, mp
)
652 gref_t
*gref
; /* WRITE queue */
656 dPrintf(D_M_ADSP, D_L_TRACE,
657 ("SndMsgUp: gref=0x%x, mbuf=0x%x\n", (unsigned)gref, (unsigned)mp));
658 trace_mbufs(D_M_ADSP, " m", mp);
660 atalk_putnext(gref
, mp
);