]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_stream.c
2eea18fd428c1dbc37b9474dc7357da66aeecb75
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1995-1998 Apple Computer, Inc.
27 * All Rights Reserved.
31 * 09/07/95 - Modified for performance (Tuyen Nguyen)
32 * Modified for MP, 1996 by Tuyen Nguyen
33 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <machine/spl.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/filedesc.h>
43 #include <sys/fcntl.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
48 #include <sys/ioctl.h>
49 #include <sys/malloc.h>
53 #include <netat/sysglue.h>
54 #include <netat/appletalk.h>
55 #include <netat/ddp.h>
56 #include <netat/at_snmp.h>
57 #include <netat/at_pcb.h>
58 #include <netat/debug.h>
59 #include <netat/at_var.h>
60 #include <netat/adsp.h>
61 #include <netat/adsp_internal.h>
65 static void adsp_iocack();
66 static void adsp_iocnak();
67 void adsp_dequeue_ccb();
68 unsigned char adspAssignSocket();
69 int adspallocate(), adsprelease();
72 atlock_t adspall_lock
;
73 atlock_t adspgen_lock
;
79 char adsp_inputC
[256];
80 CCB
*adsp_inputQ
[256];
82 extern at_ifaddr_t
*ifID_home
;
95 switch (gbuf_type(mp
)) {
97 p
= (at_ddp_t
*)gbuf_rptr(mp
);
98 ATDISABLE(s
, adspall_lock
);
99 sp
= adsp_inputQ
[p
->dst_socket
];
100 if ((sp
== 0) || (sp
->gref
==0) || (sp
->state
==sClosed
))
102 ATENABLE(s
, adspall_lock
);
106 else if (sp
->otccbLink
!= 0) {
108 if ((sp
->remoteAddress
.a
.node
== p
->src_node
)
109 && (sp
->remoteAddress
.a
.socket
== p
->src_socket
)
110 && (sp
->remoteAddress
.a
.net
== NET_VALUE(p
->src_net
)))
112 } while ((sp
= sp
->otccbLink
) != 0);
115 ATENABLE(s
, adspall_lock
);
122 if (sp
->deferred_mb
) {
123 for (mb
=sp
->deferred_mb
; gbuf_next(mb
); mb
=gbuf_next(mb
)) ;
126 sp
->deferred_mb
= mp
;
127 ATENABLE(s
, adspall_lock
);
130 ATDISABLE(l
, sp
->lockRemove
);
132 ATENABLE(l
, adspall_lock
);
134 adsp_rput(sp
->gref
, mp
);
135 if ((mp
= sp
->deferred_mb
) != 0) {
136 sp
->deferred_mb
= gbuf_next(mp
);
141 ATENABLE(s
, sp
->lockRemove
);
146 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
150 #ifdef APPLETALK_DEBUG
151 kprintf("unexpected MSG_IOCTL in adsp_input()");
164 int adsp_readable(gref
)
172 * we don't have the structure we need to determine
173 * if there's data available... we return readable in
174 * this case to keep from hanging up in the select
175 * a subsequent read will run into the same missing data
176 * structure and return an error... the ATselect code does
177 * this if it can't retrieve the 'gref' structure from the
178 * file table for the fd specified
182 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
188 int adsp_writeable(gref
)
196 * we don't have the structure we need to determine
197 * if there's room available... we return writeable in
198 * this case to keep from hanging up in the select
199 * a subsequent write will run into the same missing data
200 * structure and return an error... the ATselect code does
201 * this if it can't retrieve the 'gref' structure from the
202 * file table for the fd specified
206 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
207 ATDISABLE(s
, sp
->lock
);
208 rc
= CalcSendQFree(sp
);
209 ATENABLE(s
, sp
->lock
);
214 static void adsp_init()
219 bzero(adsp_pidM
, sizeof(adsp_pidM
));
220 bzero(adsp_inputC
, sizeof(adsp_inputC
));
221 bzero(adsp_inputQ
, sizeof(adsp_inputQ
));
226 * ADSP open and close routines. These routines
227 * initalize and release the ADSP structures. They do not
228 * have anything to do with "connections"
240 if (!adspAllocateCCB(gref
))
241 return(ENOBUFS
); /* can't get buffers */
243 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
244 gref
->readable
= adsp_readable
;
245 gref
->writeable
= adsp_writeable
;
246 ATDISABLE(s
, adspall_lock
);
247 if ((sp
->otccbLink
= ccb_used_list
) != 0)
248 sp
->otccbLink
->ccbLink
= sp
;
250 ATENABLE(s
, adspall_lock
);
258 unsigned char localSocket
;
260 /* make sure we've not yet removed the CCB (e.g., due to TrashSession) */
261 ATDISABLE(l
, adspgen_lock
);
263 CCBPtr sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
264 ATDISABLE(s
, sp
->lock
);
265 ATENABLE(s
, adspgen_lock
);
266 localSocket
= sp
->localSocket
;
267 ATENABLE(l
, sp
->lock
);
272 adsp_dequeue_ccb(sp
);
273 gbuf_freeb((gbuf_t
*)gref
->info
);
276 ATENABLE(l
, adspgen_lock
);
286 * ADSP streams read put and service routines.
289 void adsp_rput(gref
, mp
)
290 gref_t
*gref
; /* READ queue */
293 switch (gbuf_type(mp
)) {
297 switch (adspReadHandler(gref
, mp
)) {
299 atalk_putnext(gref
, mp
);
306 #ifdef APPLETALK_DEBUG
307 kprintf("adsp_rput received MSG_ERROR");
311 CheckReadQueue(gbuf_rptr(((gbuf_t
*)gref
->info
)));
312 CheckSend(gbuf_rptr(((gbuf_t
*)gref
->info
)));
314 switch (gbuf_type(mp
)) {
318 if (adspReadHandler(gref
, mp
) == STR_PUTNEXT
)
319 atalk_putnext(gref
, mp
);
322 atalk_putnext(gref
, mp
);
333 * ADSP streams write put and service routines.
337 int adsp_wput(gref
, mp
)
338 gref_t
*gref
; /* WRITE queue */
348 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
352 if (gbuf_type(mp
) == MSG_IOCTL
) {
353 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
354 switch (iocbp
->ioc_cmd
) {
359 if (gbuf_cont(mp
) == NULL
) {
360 iocbp
->ioc_rval
= -1;
361 adsp_iocnak(gref
, mp
, EINVAL
);
363 v
= *(unsigned char *)gbuf_rptr(gbuf_cont(mp
));
364 ATDISABLE(s
, adspall_lock
);
366 && ((v
> DDP_SOCKET_LAST
) || (v
< 2)
367 || ddp_socket_inuse(v
, DDP_ADSP
))) {
368 ATENABLE(s
, adspall_lock
);
369 iocbp
->ioc_rval
= -1;
370 adsp_iocnak(gref
, mp
, EINVAL
);
374 ATENABLE(s
, adspall_lock
);
375 if ((v
= adspAssignSocket(gref
, 0)) == 0) {
376 iocbp
->ioc_rval
= -1;
377 adsp_iocnak(gref
, mp
, EINVAL
);
383 adsp_pidM
[v
] = sp
->pid
;
384 ATENABLE(s
, adspall_lock
);
385 adsp_dequeue_ccb(sp
);
387 *(unsigned char *)gbuf_rptr(gbuf_cont(mp
)) = v
;
390 adsp_iocack(gref
, mp
);
400 if (((xm
= gbuf_cont(mp
)) == NULL
)
401 && ((xm
= gbuf_alloc(sizeof(at_inet_t
), PRI_MED
)) == NULL
)) {
402 iocbp
->ioc_rval
= -1;
403 adsp_iocnak(gref
, mp
, ENOBUFS
);
407 gbuf_wset(xm
,sizeof(at_inet_t
));
408 addr
= (at_inet_t
*)gbuf_rptr(xm
);
409 if (iocbp
->ioc_cmd
== ADSPGETSOCK
) {
410 /* Obtain Network and Node Id's from DDP */
411 /* *** was ddp_get_cfg() *** */
412 addr
->net
= ifID_home
->ifThisNode
.s_net
;
413 addr
->node
= ifID_home
->ifThisNode
.s_node
;
414 addr
->socket
= (sp
)? sp
->localSocket
: 0;
417 *addr
= sp
->remoteAddress
.a
;
424 adsp_iocack(gref
, mp
);
427 case DDP_IOC_GET_CFG
:
428 /* respond to an DDP_IOC_GET_CFG sent on an adsp fd */
429 if (((xm
= gbuf_cont(mp
)) == NULL
) &&
430 (xm
= gbuf_alloc(sizeof(ddp_addr_t
), PRI_MED
)) == NULL
) {
431 iocbp
->ioc_rval
= -1;
432 adsp_iocnak(gref
, mp
, ENOBUFS
);
436 gbuf_wset(xm
, sizeof(ddp_addr_t
));
437 /* Obtain Network and Node Id's from DDP */
439 /* *** was ddp_get_cfg() *** */
441 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(mp
));
442 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
443 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
444 cfgp
->inet
.socket
= (sp
)? sp
->localSocket
: 0;
445 cfgp
->ddptype
= DDP_ADSP
;
448 adsp_iocack(gref
, mp
);
456 ATDISABLE(s
, sp
->lockClose
);
457 rc
= adspWriteHandler(gref
, mp
);
458 ATENABLE(s
, sp
->lockClose
);
462 if (gbuf_type(mp
) == MSG_IOCTL
) {
463 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
464 iocbp
->ioc_private
= (void *)gref
;
480 void adspioc_ack(errno
, m
, gref
)
489 iocbp
= (ioc_t
*) gbuf_rptr(m
);
491 iocbp
->ioc_error
= errno
; /* set the errno */
492 iocbp
->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
493 if (gbuf_type(m
) == MSG_IOCTL
) /* if an ioctl, this is an ack */
494 gbuf_set_type(m
, MSG_IOCACK
); /* and ALWAYS update the user */
495 /* ioctl structure */
496 trace_mbufs(D_M_ADSP
,"A ", m
);
500 static void adsp_iocack(gref
, m
)
504 if (gbuf_type(m
) == MSG_IOCTL
)
505 gbuf_set_type(m
, MSG_IOCACK
);
508 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
510 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
516 static void adsp_iocnak(gref
, m
, err
)
521 if (gbuf_type(m
) == MSG_IOCTL
)
522 gbuf_set_type(m
, MSG_IOCNAK
);
523 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
527 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
;
530 gbuf_freem(gbuf_cont(m
));
537 adspAssignSocket(gref
, flag
)
541 unsigned char sVal
, sMax
, sMin
, sSav
, inputC
;
545 sMax
= flag
? DDP_SOCKET_LAST
-46 : DDP_SOCKET_LAST
-6;
546 sMin
= DDP_SOCKET_1st_DYNAMIC
;
548 ATDISABLE(s
, adspall_lock
);
549 for (inputC
=255, sVal
=sMax
; sVal
>= sMin
; sVal
--) {
550 if (!ddp_socket_inuse(sVal
, DDP_ADSP
))
553 if (adsp_inputC
[sVal
] &&
554 /* meaning that raw DDP doesn't have it */
555 (adsp_inputC
[sVal
] < inputC
)
556 && (adsp_inputQ
[sVal
]->state
== sOpen
)) {
557 inputC
= adsp_inputC
[sVal
];
563 if (!flag
|| (inputC
== 255)) {
564 ATENABLE(s
, adspall_lock
);
569 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
570 ATENABLE(s
, adspall_lock
);
571 adsp_dequeue_ccb(sp
);
572 ATDISABLE(s
, adspall_lock
);
574 sp
->otccbLink
= adsp_inputQ
[sVal
];
575 adsp_inputQ
[sVal
] = sp
;
577 adsp_pidM
[sVal
] = sp
->pid
;
578 ATENABLE(s
, adspall_lock
);
583 adspDeassignSocket(sp
)
592 dPrintf(D_M_ADSP
, D_L_TRACE
, ("adspDeassignSocket: pid=%d,s=%d\n",
593 sp
->pid
, sp
->localSocket
));
594 ATDISABLE(s
, adspall_lock
);
595 sVal
= sp
->localSocket
;
596 if ((curr_sp
= adsp_inputQ
[sVal
]) != 0) {
598 while (curr_sp
!= sp
) {
600 curr_sp
= curr_sp
->otccbLink
;
603 ATDISABLE(l
, sp
->lockRemove
);
605 prev_sp
->otccbLink
= sp
->otccbLink
;
607 adsp_inputQ
[sVal
] = sp
->otccbLink
;
608 ATENABLE(l
, sp
->lockRemove
);
609 if (adsp_inputQ
[sVal
])
612 pid
= adsp_pidM
[sVal
];
613 adsp_inputC
[sVal
] = 0;
619 ATENABLE(s
, adspall_lock
);
623 ATENABLE(s
, adspall_lock
);
625 dPrintf(D_M_ADSP
, D_L_ERROR
,
626 ("adspDeassignSocket: closing, no CCB block, trouble ahead\n"));
628 } /* adspDeassignSocket */
631 * remove CCB from the use list
639 ATDISABLE(s
, adspall_lock
);
640 if (sp
== ccb_used_list
) {
641 if ((ccb_used_list
= sp
->otccbLink
) != 0)
642 sp
->otccbLink
->ccbLink
= 0;
643 } else if (sp
->ccbLink
) {
644 if ((sp
->ccbLink
->otccbLink
= sp
->otccbLink
) != 0)
645 sp
->otccbLink
->ccbLink
= sp
->ccbLink
;
650 ATENABLE(s
, adspall_lock
);
653 void SndMsgUp(gref
, mp
)
654 gref_t
*gref
; /* WRITE queue */
658 dPrintf(D_M_ADSP, D_L_TRACE,
659 ("SndMsgUp: gref=0x%x, mbuf=0x%x\n", (unsigned)gref, (unsigned)mp));
660 trace_mbufs(D_M_ADSP, " m", mp);
662 atalk_putnext(gref
, mp
);