1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** mISDN channel handlin for remote application **
10 \*****************************************************************************/
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
23 #include <netinet/udp.h>
24 #include <netinet/in.h>
26 #include <sys/socket.h>
29 #include <mISDNuser/isdn_net.h>
30 #include <mISDNuser/net_l3.h>
33 #include <asterisk/frame.h>
36 #include "extension.h"
38 #include "lcrsocket.h"
45 #ifndef ISDN_PID_L4_B_USER
46 #define ISDN_PID_L4_B_USER 0x440000ff
59 int bchannel_initialize(void)
64 void bchannel_deinitialize(void)
68 int bchannel_entity = 0; /* used for udevice */
69 int bchannel_device = -1; /* the device handler and port list */
71 int bchannel_initialize(void)
73 unsigned char buff[1025];
74 iframe_t *frm = (iframe_t *)buff;
77 /* open mISDNdevice if not already open */
78 if (bchannel_device < 0)
83 CERROR(NULL, NULL, "cannot open mISDN device ret=%d errno=%d (%s) Check for mISDN modules!\nAlso did you create \"/dev/mISDN\"? Do: \"mknod /dev/mISDN c 46 0\"\n", ret, errno, strerror(errno));
86 bchannel_device = ret;
87 CDEBUG(NULL, NULL, "mISDN device opened.\n");
89 /* create entity for layer 3 TE-mode */
90 mISDN_write_frame(bchannel_device, buff, 0, MGR_NEWENTITY | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
91 ret = mISDN_read_frame(bchannel_device, frm, sizeof(iframe_t), 0, MGR_NEWENTITY | CONFIRM, TIMEOUT_1SEC);
92 if (ret < (int)mISDN_HEADER_LEN)
95 CERROR(NULL, NULL, "Cannot request MGR_NEWENTITY from mISDN. Exitting due to software bug.");
98 bchannel_entity = frm->dinfo & 0xffff;
105 void bchannel_deinitialize(void)
107 unsigned char buff[1025];
109 if (bchannel_device >= 0)
112 mISDN_write_frame(bchannel_device, buff, 0, MGR_DELENTITY | REQUEST, bchannel_entity, 0, NULL, TIMEOUT_1SEC);
114 mISDN_close(bchannel_device);
115 bchannel_device = -1;
121 * send control information to the channel (dsp-module)
123 static void ph_control(unsigned long handle, unsigned long c1, unsigned long c2, char *trace_name, int trace_value)
126 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
127 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
128 unsigned long *d = (unsigned long *)(buffer+MISDN_HEADER_LEN);
131 ctrl->prim = PH_CONTROL_REQ;
135 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)*2, 0, NULL, 0);
137 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
139 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+sizeof(int)];
140 iframe_t *ctrl = (iframe_t *)buffer;
141 unsigned long *d = (unsigned long *)&ctrl->data.p;
143 ctrl->prim = PH_CONTROL | REQUEST;
144 ctrl->addr = handle | FLG_MSG_DOWN;
146 ctrl->len = sizeof(int)*2;
149 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
152 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
153 if (c1 == CMX_CONF_JOIN)
154 add_trace(trace_name, NULL, "0x%08x", trace_value);
156 add_trace(trace_name, NULL, "%d", trace_value);
161 static void ph_control_block(unsigned long handle, unsigned long c1, void *c2, int c2_len, char *trace_name, int trace_value)
164 unsigned char buffer[MISDN_HEADER_LEN+sizeof(int)+c2_len];
165 struct mISDNhead *ctrl = (struct mISDNhead *)buffer;
166 unsigned long *d = (unsigned long *)(buffer+MISDN_HEADER_LEN);
169 ctrl->prim = PH_CONTROL_REQ;
172 memcpy(d, c2, c2_len);
173 ret = sendto(handle, buffer, MISDN_HEADER_LEN+sizeof(int)+c2_len, 0, NULL, 0);
175 CERROR(NULL, NULL, "Failed to send to socket %d\n", handle);
177 unsigned char buffer[mISDN_HEADER_LEN+sizeof(int)+c2_len];
178 iframe_t *ctrl = (iframe_t *)buffer;
179 unsigned long *d = (unsigned long *)&ctrl->data.p;
181 ctrl->prim = PH_CONTROL | REQUEST;
182 ctrl->addr = handle | FLG_MSG_DOWN;
184 ctrl->len = sizeof(int)+c2_len;
186 memcpy(d, c2, c2_len);
187 mISDN_write(bchannel_device, ctrl, mISDN_HEADER_LEN+ctrl->len, TIMEOUT_1SEC);
190 chan_trace_header(mISDNport, isdnport, "BCHANNEL control", DIRECTION_OUT);
191 add_trace(trace_name, NULL, "%d", trace_value);
200 int bchannel_create(struct bchannel *bchannel)
204 unsigned long on = 1;
205 struct sockaddr_mISDN addr;
207 if (bchannel->b_sock)
209 CERROR(NULL, NULL, "Socket already created for handle 0x%x\n", bchannel->handle);
214 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
215 if (bchannel->b_sock < 0)
217 CERROR(NULL, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
221 /* set nonblocking io */
222 ret = ioctl(bchannel->b_sock, FIONBIO, &on);
225 CERROR(NULL, NULL, "Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
226 close(bchannel->b_sock);
227 bchannel->b_sock = -1;
231 /* bind socket to bchannel */
232 addr.family = AF_ISDN;
233 addr.dev = (bchannel->handle>>8)-1;
234 addr.channel = bchannel->handle && 0xff;
235 ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
238 CERROR(NULL, NULL, "Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
239 close(bchannel->b_sock);
240 bchannel->b_sock = -1;
245 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
246 add_trace("channel", NULL, "%d", i+1+(i>=15));
247 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
253 unsigned char buff[1024];
257 if (bchannel->b_stid)
259 CERROR(NULL, NULL, "Stack already created for address 0x%lx\n", bchannel->b_stid);
263 if (bchannel->b_addr)
265 CERROR(NULL, NULL, "Stack already created for address 0x%lx\n", bchannel->b_addr);
269 /* create new layer */
270 CDEBUG(NULL, NULL, "creating new layer for stid 0x%lx.\n" , bchannel->handle);
271 memset(&li, 0, sizeof(li));
272 memset(&pid, 0, sizeof(pid));
275 li.st = bchannel->handle;
276 strcpy(li.name, "B L4");
277 li.pid.layermask = ISDN_LAYER((4));
278 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
279 ret = mISDN_new_layer(bchannel_device, &li);
283 CERROR(NULL, NULL, "mISDN_new_layer() failed to add bchannel for stid 0x%lx.\n", bchannel->handle);
288 goto failed_new_layer;
290 bchannel->b_stid = bchannel->handle;
291 bchannel->b_addr = li.id;
292 CDEBUG(NULL, NULL, "new layer (b_addr=0x%x)\n", bchannel->b_addr);
294 /* create new stack */
295 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
296 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
297 pid.protocol[3] = ISDN_PID_L3_B_DSP;
298 pid.protocol[4] = ISDN_PID_L4_B_USER;
299 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
300 ret = mISDN_set_stack(bchannel_device, bchannel->b_stid, &pid);
304 CERROR(NULL, NULL, "mISDN_set_stack() failed (ret=%d) to add bchannel stid=0x%lx\n", ret, bchannel->b_stid);
305 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
308 ret = mISDN_get_setstack_ind(bchannel_device, bchannel->b_addr);
313 bchannel->b_addr = mISDN_get_layerid(bchannel_device, bchannel->b_stid, 4);
314 if (!bchannel->b_addr)
317 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create stack", DIRECTION_OUT);
318 add_trace("channel", NULL, "%d", i+1+(i>=15));
319 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
320 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
327 bchannel->b_stid = 0;
328 bchannel->b_addr = 0;
335 * activate / deactivate request
337 void bchannel_activate(struct bchannel *bchannel, int activate)
340 struct mISDNhead act;
343 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
345 ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
347 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
351 /* activate bchannel */
352 act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST;
353 act.addr = bchannel->b_addr | FLG_MSG_DOWN;
356 mISDN_write(bchannel_device, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
359 bchannel->b_state = BSTATE_ACTIVATING;
362 chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
363 add_trace("channel", NULL, "%d", i+1+(i>=15));
372 static void bchannel_activated(struct bchannel *bchannel)
377 handle = bchannel->b_sock;
379 unsigned long handle;
381 handle = bchannel->b_addr;
384 /* set dsp features */
385 if (bchannel->b_txdata)
386 ph_control(handle, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
387 if (bchannel->b_delay)
388 ph_control(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
389 if (bchannel->b_tx_dejitter)
390 ph_control(handle, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter);
391 if (bchannel->b_tx_gain)
392 ph_control(handle, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
393 if (bchannel->b_rx_gain)
394 ph_control(handle, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain);
395 if (bchannel->b_pipeline[0])
397 ph_control_block(handle, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
399 ph_control_block(handle, PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
401 if (bchannel->b_conf)
402 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
403 if (bchannel->b_echo)
404 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
405 if (bchannel->b_tone)
406 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
407 if (bchannel->b_rxoff)
408 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
409 // if (bchannel->b_txmix)
410 // ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
411 if (bchannel->b_dtmf)
412 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
413 if (bchannel->b_crypt_len)
414 ph_control_block(handle, DSP_BF_ENABLE_KEY, bchannel->b_crypt_key, bchannel->b_crypt_len, "DSP-CRYPT", bchannel->b_crypt_len);
415 if (bchannel->b_conf)
416 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
418 bchannel->b_state = BSTATE_ACTIVE;
424 static void bchannel_destroy(struct bchannel *bchannel)
428 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
429 add_trace("channel", NULL, "%d", i+1+(i>=15));
430 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
433 if (bchannel->b_sock > -1)
435 close(bchannel->b_sock);
436 bchannel->b_sock = -1;
439 unsigned char buff[1024];
442 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
443 add_trace("channel", NULL, "%d", i+1+(i>=15));
444 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
445 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
448 /* remove our stack only if set */
449 if (bchannel->b_addr)
451 CDEBUG(NULL, NULL, "free stack (b_addr=0x%x)\n", bchannel->b_addr);
452 mISDN_clear_stack(bchannel_device, bchannel->b_stid);
453 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
454 bchannel->b_stid = 0;
455 bchannel->b_addr = 0;
458 bchannel->b_state = BSTATE_IDLE;
463 * whenever we get audio data from bchannel, we process it here
465 static void bchannel_receive(struct bchannel *bchannel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
467 unsigned long cont = *((unsigned long *)data);
468 // unsigned char *data_temp;
469 // unsigned long length_temp;
474 if (prim == PH_CONTROL_IND)
476 if (prim == (PH_CONTROL | INDICATION))
481 CERROR(NULL, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
484 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
487 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
488 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
491 if (bchannel->rx_dtmf)
492 bchannel->rx_dtmf(bchannel, cont & DTMF_TONE_MASK);
503 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
504 add_trace("DSP-CRYPT", NULL, "error");
515 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
516 add_trace("DSP-CRYPT", NULL, "ok");
523 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
524 add_trace("unknown", NULL, "0x%x", cont);
533 if (prim == PH_DATA_REQ)
535 if (!bchannel->b_txdata)
537 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
538 CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
544 if (prim == (PH_SIGNAL | INDICATION))
549 if (!bchannel->b_txdata)
551 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
552 CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
559 chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
560 add_trace("unknown", NULL, "0x%x", frm->dinfo);
569 if (prim != PH_DATA_IND && prim != DL_DATA_IND)
571 CERROR(NULL, NULL, "Bchannel received unknown primitve: 0x%lx\n", prim);
574 /* calls will not process any audio data unless
575 * the call is connected OR interface features audio during call setup.
578 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
579 if (bchannel->b_rxoff)
581 CDEBUG(NULL, NULL, "ignoring data, because rx is turned off\n");
587 CDEBUG(NULL, NULL, "ignoring data, because no call associated with bchannel\n");
590 if (!bchannel->call->audiopath)
592 /* return, because we have no audio from port */
595 len = write(bchannel->call->pipe[1], data, len);
598 CDEBUG(NULL, NULL, "broken pipe on bchannel pipe\n");
605 * transmit data to bchannel
607 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
609 unsigned char buff[1025];
611 struct mISDNhead *frm = (struct mISDNhead *)buff;
613 iframe_t *frm = (iframe_t *)buff;
617 if (bchannel->b_state != BSTATE_ACTIVE)
620 frm->prim = DL_DATA_REQ;
622 ret = sendto(bchannel->b_sock, data, len, 0, NULL, 0);
624 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
626 frm->prim = DL_DATA | REQUEST;
627 frm->addr = bchannel->b_addr | FLG_MSG_DOWN;
631 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
639 void bchannel_join(struct bchannel *bchannel, unsigned short id)
644 handle = bchannel->b_sock;
646 unsigned long handle;
648 handle = bchannel->b_addr;
651 bchannel->b_conf = (id<<16) + bchannel_pid;
652 bchannel->b_rxoff = 1;
654 bchannel->b_conf = 0;
655 bchannel->b_rxoff = 0;
657 if (bchannel->b_state == BSTATE_ACTIVE)
659 ph_control(handle, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf);
660 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
666 * main loop for processing messages from mISDN
669 int bchannel_handle(void)
672 struct bchannel *bchannel;
673 char buffer[2048+MISDN_HEADER_LEN];
674 struct mISDNhead *hh = (struct mISDNhead *)buffer;
676 /* process all bchannels */
677 bchannel = bchannel_first;
680 /* handle message from bchannel */
681 if (bchannel->b_sock > -1)
683 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
684 if (ret >= MISDN_HEADER_LEN)
689 /* we don't care about confirms, we use rx data to sync tx */
693 /* we receive audio data, we respond to it AND we send tones */
698 bchannel_receive(bchannel, hh->prim, hh->id, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
701 case PH_ACTIVATE_IND:
702 case DL_ESTABLISH_IND:
703 case PH_ACTIVATE_CNF:
704 case DL_ESTABLISH_CNF:
705 CDEBUG(NULL, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
706 bchannel_activated(bchannel);
709 case PH_DEACTIVATE_IND:
711 case PH_DEACTIVATE_CNF:
713 CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
714 // bchannel_deactivated(bchannel);
718 CERROR(NULL, NULL, "child message not handled: prim(0x%x) socket(%d) data len(%d)\n", hh->prim, bchannel->b_sock, ret - MISDN_HEADER_LEN);
722 if (ret < 0 && errno != EWOULDBLOCK)
723 CERROR(NULL, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
726 bchannel = bchannel->next;
729 /* if we received at least one b-frame, we will return 1 */
733 int bchannel_handle(void)
735 struct bchannel *bchannel;
737 unsigned char buffer[2048];
740 /* no device, no read */
741 if (bchannel_device < 0)
744 /* get message from kernel */
745 len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
750 CERROR(NULL, NULL, "Failed to do mISDN_read()\n");
755 // printf("%s: ERROR: mISDN_read() returns nothing\n");
758 frm = (iframe_t *)buffer;
763 case MGR_DELLAYER | CONFIRM:
764 case MGR_INITTIMER | CONFIRM:
765 case MGR_ADDTIMER | CONFIRM:
766 case MGR_DELTIMER | CONFIRM:
767 case MGR_REMOVETIMER | CONFIRM:
771 /* find the mISDNport that belongs to the stack */
772 bchannel = bchannel_first;
775 if (frm->addr == bchannel->b_addr)
777 bchannel = bchannel->next;
781 CERROR(NULL, NULL, "message belongs to no bchannel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
788 /* we don't care about confirms, we use rx data to sync tx */
789 case PH_DATA | CONFIRM:
790 case DL_DATA | CONFIRM:
793 /* we receive audio data, we respond to it AND we send tones */
794 case PH_DATA | INDICATION:
795 case DL_DATA | INDICATION:
796 case PH_CONTROL | INDICATION:
797 case PH_SIGNAL | INDICATION:
798 bchannel_receive(bchannel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
801 case PH_ACTIVATE | INDICATION:
802 case DL_ESTABLISH | INDICATION:
803 case PH_ACTIVATE | CONFIRM:
804 case DL_ESTABLISH | CONFIRM:
805 CDEBUG(NULL, NULL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
806 bchannel_activated(bchannel);
809 case PH_DEACTIVATE | INDICATION:
810 case DL_RELEASE | INDICATION:
811 case PH_DEACTIVATE | CONFIRM:
812 case DL_RELEASE | CONFIRM:
813 CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
814 // bchannel_deactivated(bchannel);
818 CERROR(NULL, NULL, "message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
828 * bchannel channel handling
830 struct bchannel *bchannel_first = NULL;
831 struct bchannel *find_bchannel_handle(unsigned long handle)
833 struct bchannel *bchannel = bchannel_first;
837 if (bchannel->handle == handle)
839 bchannel = bchannel->next;
845 struct bchannel *find_bchannel_ref(unsigned long ref)
847 struct bchannel *bchannel = bchannel_first;
851 if (bchannel->ref == ref)
853 bchannel = bchannel->next;
859 struct bchannel *alloc_bchannel(unsigned long handle)
861 struct bchannel **bchannelp = &bchannel_first;
864 bchannelp = &((*bchannelp)->next);
866 *bchannelp = (struct bchannel *)malloc(sizeof(struct bchannel));
869 (*bchannelp)->handle = handle;
870 (*bchannelp)->b_state = BSTATE_IDLE;
875 void free_bchannel(struct bchannel *bchannel)
877 struct bchannel **temp = &bchannel_first;
881 if (*temp == bchannel)
883 *temp = (*temp)->next;
885 if (bchannel->b_sock > -1)
887 if (bchannel->b_stid)
889 bchannel_destroy(bchannel);
892 if (bchannel->call->bchannel)
893 bchannel->call->bchannel = NULL;
898 temp = &((*temp)->next);