]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_stream.c
c3ea0ae2f28045e95e41bfe4b6028d9b1f197597
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1995-1998 Apple Computer, Inc.
24 * All Rights Reserved.
28 * 09/07/95 - Modified for performance (Tuyen Nguyen)
29 * Modified for MP, 1996 by Tuyen Nguyen
30 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
32 #include <sys/errno.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <machine/spl.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/filedesc.h>
40 #include <sys/fcntl.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
45 #include <sys/ioctl.h>
46 #include <sys/malloc.h>
50 #include <netat/sysglue.h>
51 #include <netat/appletalk.h>
52 #include <netat/ddp.h>
53 #include <netat/at_snmp.h>
54 #include <netat/at_pcb.h>
55 #include <netat/debug.h>
56 #include <netat/at_var.h>
57 #include <netat/adsp.h>
58 #include <netat/adsp_internal.h>
62 static void adsp_iocack();
63 static void adsp_iocnak();
64 void adsp_dequeue_ccb();
65 unsigned char adspAssignSocket();
66 int adspallocate(), adsprelease();
69 atlock_t adspall_lock
;
70 atlock_t adspgen_lock
;
76 char adsp_inputC
[256];
77 CCB
*adsp_inputQ
[256];
79 extern at_ifaddr_t
*ifID_home
;
92 switch (gbuf_type(mp
)) {
94 p
= (at_ddp_t
*)gbuf_rptr(mp
);
95 ATDISABLE(s
, adspall_lock
);
96 sp
= adsp_inputQ
[p
->dst_socket
];
97 if ((sp
== 0) || (sp
->gref
==0) || (sp
->state
==sClosed
))
99 ATENABLE(s
, adspall_lock
);
103 else if (sp
->otccbLink
!= 0) {
105 if ((sp
->remoteAddress
.a
.node
== p
->src_node
)
106 && (sp
->remoteAddress
.a
.socket
== p
->src_socket
)
107 && (sp
->remoteAddress
.a
.net
== NET_VALUE(p
->src_net
)))
109 } while ((sp
= sp
->otccbLink
) != 0);
112 ATENABLE(s
, adspall_lock
);
119 if (sp
->deferred_mb
) {
120 for (mb
=sp
->deferred_mb
; gbuf_next(mb
); mb
=gbuf_next(mb
)) ;
123 sp
->deferred_mb
= mp
;
124 ATENABLE(s
, adspall_lock
);
127 ATDISABLE(l
, sp
->lockRemove
);
129 ATENABLE(l
, adspall_lock
);
131 adsp_rput(sp
->gref
, mp
);
132 if ((mp
= sp
->deferred_mb
) != 0) {
133 sp
->deferred_mb
= gbuf_next(mp
);
138 ATENABLE(s
, sp
->lockRemove
);
143 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
147 #ifdef APPLETALK_DEBUG
148 kprintf("unexpected MSG_IOCTL in adsp_input()");
161 int adsp_readable(gref
)
169 * we don't have the structure we need to determine
170 * if there's data available... we return readable in
171 * this case to keep from hanging up in the select
172 * a subsequent read will run into the same missing data
173 * structure and return an error... the ATselect code does
174 * this if it can't retrieve the 'gref' structure from the
175 * file table for the fd specified
179 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
185 int adsp_writeable(gref
)
193 * we don't have the structure we need to determine
194 * if there's room available... we return writeable in
195 * this case to keep from hanging up in the select
196 * a subsequent write will run into the same missing data
197 * structure and return an error... the ATselect code does
198 * this if it can't retrieve the 'gref' structure from the
199 * file table for the fd specified
203 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
204 ATDISABLE(s
, sp
->lock
);
205 rc
= CalcSendQFree(sp
);
206 ATENABLE(s
, sp
->lock
);
211 static void adsp_init()
216 bzero(adsp_pidM
, sizeof(adsp_pidM
));
217 bzero(adsp_inputC
, sizeof(adsp_inputC
));
218 bzero(adsp_inputQ
, sizeof(adsp_inputQ
));
223 * ADSP open and close routines. These routines
224 * initalize and release the ADSP structures. They do not
225 * have anything to do with "connections"
237 if (!adspAllocateCCB(gref
))
238 return(ENOBUFS
); /* can't get buffers */
240 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
241 gref
->readable
= adsp_readable
;
242 gref
->writeable
= adsp_writeable
;
243 ATDISABLE(s
, adspall_lock
);
244 if ((sp
->otccbLink
= ccb_used_list
) != 0)
245 sp
->otccbLink
->ccbLink
= sp
;
247 ATENABLE(s
, adspall_lock
);
255 unsigned char localSocket
;
257 /* make sure we've not yet removed the CCB (e.g., due to TrashSession) */
258 ATDISABLE(l
, adspgen_lock
);
260 CCBPtr sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
261 ATDISABLE(s
, sp
->lock
);
262 ATENABLE(s
, adspgen_lock
);
263 localSocket
= sp
->localSocket
;
264 ATENABLE(l
, sp
->lock
);
269 adsp_dequeue_ccb(sp
);
270 gbuf_freeb((gbuf_t
*)gref
->info
);
273 ATENABLE(l
, adspgen_lock
);
283 * ADSP streams read put and service routines.
286 void adsp_rput(gref
, mp
)
287 gref_t
*gref
; /* READ queue */
290 switch (gbuf_type(mp
)) {
294 switch (adspReadHandler(gref
, mp
)) {
296 atalk_putnext(gref
, mp
);
303 #ifdef APPLETALK_DEBUG
304 kprintf("adsp_rput received MSG_ERROR");
308 CheckReadQueue(gbuf_rptr(((gbuf_t
*)gref
->info
)));
309 CheckSend(gbuf_rptr(((gbuf_t
*)gref
->info
)));
311 switch (gbuf_type(mp
)) {
315 if (adspReadHandler(gref
, mp
) == STR_PUTNEXT
)
316 atalk_putnext(gref
, mp
);
319 atalk_putnext(gref
, mp
);
330 * ADSP streams write put and service routines.
334 int adsp_wput(gref
, mp
)
335 gref_t
*gref
; /* WRITE queue */
345 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
349 if (gbuf_type(mp
) == MSG_IOCTL
) {
350 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
351 switch (iocbp
->ioc_cmd
) {
356 if (gbuf_cont(mp
) == NULL
) {
357 iocbp
->ioc_rval
= -1;
358 adsp_iocnak(gref
, mp
, EINVAL
);
360 v
= *(unsigned char *)gbuf_rptr(gbuf_cont(mp
));
361 ATDISABLE(s
, adspall_lock
);
363 && ((v
> DDP_SOCKET_LAST
) || (v
< 2)
364 || ddp_socket_inuse(v
, DDP_ADSP
))) {
365 ATENABLE(s
, adspall_lock
);
366 iocbp
->ioc_rval
= -1;
367 adsp_iocnak(gref
, mp
, EINVAL
);
371 ATENABLE(s
, adspall_lock
);
372 if ((v
= adspAssignSocket(gref
, 0)) == 0) {
373 iocbp
->ioc_rval
= -1;
374 adsp_iocnak(gref
, mp
, EINVAL
);
380 adsp_pidM
[v
] = sp
->pid
;
381 ATENABLE(s
, adspall_lock
);
382 adsp_dequeue_ccb(sp
);
384 *(unsigned char *)gbuf_rptr(gbuf_cont(mp
)) = v
;
387 adsp_iocack(gref
, mp
);
397 if (((xm
= gbuf_cont(mp
)) == NULL
)
398 && ((xm
= gbuf_alloc(sizeof(at_inet_t
), PRI_MED
)) == NULL
)) {
399 iocbp
->ioc_rval
= -1;
400 adsp_iocnak(gref
, mp
, ENOBUFS
);
404 gbuf_wset(xm
,sizeof(at_inet_t
));
405 addr
= (at_inet_t
*)gbuf_rptr(xm
);
406 if (iocbp
->ioc_cmd
== ADSPGETSOCK
) {
407 /* Obtain Network and Node Id's from DDP */
408 /* *** was ddp_get_cfg() *** */
409 addr
->net
= ifID_home
->ifThisNode
.s_net
;
410 addr
->node
= ifID_home
->ifThisNode
.s_node
;
411 addr
->socket
= (sp
)? sp
->localSocket
: 0;
414 *addr
= sp
->remoteAddress
.a
;
421 adsp_iocack(gref
, mp
);
424 case DDP_IOC_GET_CFG
:
425 /* respond to an DDP_IOC_GET_CFG sent on an adsp fd */
426 if (((xm
= gbuf_cont(mp
)) == NULL
) &&
427 (xm
= gbuf_alloc(sizeof(at_inet_t
), PRI_MED
)) == NULL
) {
428 iocbp
->ioc_rval
= -1;
429 adsp_iocnak(gref
, mp
, ENOBUFS
);
433 gbuf_wset(xm
, sizeof(ddp_addr_t
));
434 /* Obtain Network and Node Id's from DDP */
436 /* *** was ddp_get_cfg() *** */
438 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(mp
));
439 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
440 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
441 cfgp
->inet
.socket
= (sp
)? sp
->localSocket
: 0;
442 cfgp
->ddptype
= DDP_ADSP
;
445 adsp_iocack(gref
, mp
);
453 ATDISABLE(s
, sp
->lockClose
);
454 rc
= adspWriteHandler(gref
, mp
);
455 ATENABLE(s
, sp
->lockClose
);
459 if (gbuf_type(mp
) == MSG_IOCTL
) {
460 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
461 iocbp
->ioc_private
= (void *)gref
;
477 void adspioc_ack(errno
, m
, gref
)
486 iocbp
= (ioc_t
*) gbuf_rptr(m
);
488 iocbp
->ioc_error
= errno
; /* set the errno */
489 iocbp
->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
490 if (gbuf_type(m
) == MSG_IOCTL
) /* if an ioctl, this is an ack */
491 gbuf_set_type(m
, MSG_IOCACK
); /* and ALWAYS update the user */
492 /* ioctl structure */
493 trace_mbufs(D_M_ADSP
,"A ", m
);
497 static void adsp_iocack(gref
, m
)
501 if (gbuf_type(m
) == MSG_IOCTL
)
502 gbuf_set_type(m
, MSG_IOCACK
);
505 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
507 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
513 static void adsp_iocnak(gref
, m
, err
)
518 if (gbuf_type(m
) == MSG_IOCTL
)
519 gbuf_set_type(m
, MSG_IOCNAK
);
520 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
524 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
;
527 gbuf_freem(gbuf_cont(m
));
534 adspAssignSocket(gref
, flag
)
538 unsigned char sVal
, sMax
, sMin
, sSav
, inputC
;
542 sMax
= flag
? DDP_SOCKET_LAST
-46 : DDP_SOCKET_LAST
-6;
543 sMin
= DDP_SOCKET_1st_DYNAMIC
-64;
545 ATDISABLE(s
, adspall_lock
);
546 for (inputC
=255, sVal
=sMax
; sVal
>= sMin
; sVal
--) {
547 if (!ddp_socket_inuse(sVal
, DDP_ADSP
))
550 if (adsp_inputC
[sVal
] &&
551 /* meaning that raw DDP doesn't have it */
552 (adsp_inputC
[sVal
] < inputC
)
553 && (adsp_inputQ
[sVal
]->state
== sOpen
)) {
554 inputC
= adsp_inputC
[sVal
];
560 if (!flag
|| (inputC
== 255)) {
561 ATENABLE(s
, adspall_lock
);
566 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
567 ATENABLE(s
, adspall_lock
);
568 adsp_dequeue_ccb(sp
);
569 ATDISABLE(s
, adspall_lock
);
571 sp
->otccbLink
= adsp_inputQ
[sVal
];
572 adsp_inputQ
[sVal
] = sp
;
574 adsp_pidM
[sVal
] = sp
->pid
;
575 ATENABLE(s
, adspall_lock
);
580 adspDeassignSocket(sp
)
589 dPrintf(D_M_ADSP
, D_L_TRACE
, ("adspDeassignSocket: pid=%d,s=%d\n",
590 sp
->pid
, sp
->localSocket
));
591 ATDISABLE(s
, adspall_lock
);
592 sVal
= sp
->localSocket
;
593 if ((curr_sp
= adsp_inputQ
[sVal
]) != 0) {
595 while (curr_sp
!= sp
) {
597 curr_sp
= curr_sp
->otccbLink
;
600 ATDISABLE(l
, sp
->lockRemove
);
602 prev_sp
->otccbLink
= sp
->otccbLink
;
604 adsp_inputQ
[sVal
] = sp
->otccbLink
;
605 ATENABLE(l
, sp
->lockRemove
);
606 if (adsp_inputQ
[sVal
])
609 pid
= adsp_pidM
[sVal
];
610 adsp_inputC
[sVal
] = 0;
616 ATENABLE(s
, adspall_lock
);
620 ATENABLE(s
, adspall_lock
);
622 dPrintf(D_M_ADSP
, D_L_ERROR
,
623 ("adspDeassignSocket: closing, no CCB block, trouble ahead\n"));
625 } /* adspDeassignSocket */
628 * remove CCB from the use list
636 ATDISABLE(s
, adspall_lock
);
637 if (sp
== ccb_used_list
) {
638 if ((ccb_used_list
= sp
->otccbLink
) != 0)
639 sp
->otccbLink
->ccbLink
= 0;
640 } else if (sp
->ccbLink
) {
641 if ((sp
->ccbLink
->otccbLink
= sp
->otccbLink
) != 0)
642 sp
->otccbLink
->ccbLink
= sp
->ccbLink
;
647 ATENABLE(s
, adspall_lock
);
650 void SndMsgUp(gref
, mp
)
651 gref_t
*gref
; /* WRITE queue */
655 dPrintf(D_M_ADSP, D_L_TRACE,
656 ("SndMsgUp: gref=0x%x, mbuf=0x%x\n", (unsigned)gref, (unsigned)mp));
657 trace_mbufs(D_M_ADSP, " m", mp);
659 atalk_putnext(gref
, mp
);