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);
213 /* default tx_dejitter */
214 bchannel->b_tx_dejitter = 1;
217 bchannel->b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_L2DSP);
218 if (bchannel->b_sock < 0)
220 CERROR(NULL, NULL, "Failed to open bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
224 /* set nonblocking io */
225 ret = ioctl(bchannel->b_sock, FIONBIO, &on);
228 CERROR(NULL, NULL, "Failed to set bchannel-socket handle 0x%x into nonblocking IO\n", bchannel->handle);
229 close(bchannel->b_sock);
230 bchannel->b_sock = -1;
234 /* bind socket to bchannel */
235 addr.family = AF_ISDN;
236 addr.dev = (bchannel->handle>>8)-1;
237 addr.channel = bchannel->handle && 0xff;
238 ret = bind(bchannel->b_sock, (struct sockaddr *)&addr, sizeof(addr));
241 CERROR(NULL, NULL, "Failed to bind bchannel-socket for handle 0x%x with mISDN-DSP layer. Did you load mISDNdsp.ko?\n", bchannel->handle);
242 close(bchannel->b_sock);
243 bchannel->b_sock = -1;
248 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create socket", DIRECTION_OUT);
249 add_trace("channel", NULL, "%d", i+1+(i>=15));
250 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
256 unsigned char buff[1024];
260 if (bchannel->b_stid)
262 CERROR(NULL, NULL, "Stack already created for address 0x%lx\n", bchannel->b_stid);
266 if (bchannel->b_addr)
268 CERROR(NULL, NULL, "Stack already created for address 0x%lx\n", bchannel->b_addr);
272 /* create new layer */
273 CDEBUG(NULL, NULL, "creating new layer for stid 0x%lx.\n" , bchannel->handle);
274 memset(&li, 0, sizeof(li));
275 memset(&pid, 0, sizeof(pid));
278 li.st = bchannel->handle;
279 strcpy(li.name, "B L4");
280 li.pid.layermask = ISDN_LAYER((4));
281 li.pid.protocol[4] = ISDN_PID_L4_B_USER;
282 ret = mISDN_new_layer(bchannel_device, &li);
286 CERROR(NULL, NULL, "mISDN_new_layer() failed to add bchannel for stid 0x%lx.\n", bchannel->handle);
291 goto failed_new_layer;
293 bchannel->b_stid = bchannel->handle;
294 bchannel->b_addr = li.id;
295 CDEBUG(NULL, NULL, "new layer (b_addr=0x%x)\n", bchannel->b_addr);
297 /* create new stack */
298 pid.protocol[1] = ISDN_PID_L1_B_64TRANS;
299 pid.protocol[2] = ISDN_PID_L2_B_TRANS;
300 pid.protocol[3] = ISDN_PID_L3_B_DSP;
301 pid.protocol[4] = ISDN_PID_L4_B_USER;
302 pid.layermask = ISDN_LAYER((1)) | ISDN_LAYER((2)) | ISDN_LAYER((3)) | ISDN_LAYER((4));
303 ret = mISDN_set_stack(bchannel_device, bchannel->b_stid, &pid);
307 CERROR(NULL, NULL, "mISDN_set_stack() failed (ret=%d) to add bchannel stid=0x%lx\n", ret, bchannel->b_stid);
308 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
311 ret = mISDN_get_setstack_ind(bchannel_device, bchannel->b_addr);
316 bchannel->b_addr = mISDN_get_layerid(bchannel_device, bchannel->b_stid, 4);
317 if (!bchannel->b_addr)
320 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL create stack", DIRECTION_OUT);
321 add_trace("channel", NULL, "%d", i+1+(i>=15));
322 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
323 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
330 bchannel->b_stid = 0;
331 bchannel->b_addr = 0;
338 * activate / deactivate request
340 void bchannel_activate(struct bchannel *bchannel, int activate)
343 struct mISDNhead act;
346 act.prim = (activate)?DL_ESTABLISH_REQ:DL_RELEASE_REQ;
348 ret = sendto(bchannel->b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
350 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
354 /* activate bchannel */
355 act.prim = (activate?DL_ESTABLISH:DL_RELEASE) | REQUEST;
356 act.addr = bchannel->b_addr | FLG_MSG_DOWN;
359 mISDN_write(bchannel_device, &act, mISDN_HEADER_LEN+act.len, TIMEOUT_1SEC);
362 bchannel->b_state = BSTATE_ACTIVATING;
365 chan_trace_header(mISDNport, mISDNport->b_port[i], activate?(char*)"BCHANNEL activate":(char*)"BCHANNEL deactivate", DIRECTION_OUT);
366 add_trace("channel", NULL, "%d", i+1+(i>=15));
375 static void bchannel_activated(struct bchannel *bchannel)
380 handle = bchannel->b_sock;
382 unsigned long handle;
384 handle = bchannel->b_addr;
387 /* set dsp features */
388 if (bchannel->b_txdata)
389 ph_control(handle, (bchannel->b_txdata)?DSP_TXDATA_ON:DSP_TXDATA_OFF, 0, "DSP-TXDATA", bchannel->b_txdata);
390 if (bchannel->b_delay)
391 ph_control(handle, DSP_DELAY, bchannel->b_delay, "DSP-DELAY", bchannel->b_delay);
392 if (bchannel->b_tx_dejitter)
393 ph_control(handle, (bchannel->b_tx_dejitter)?DSP_TX_DEJITTER:DSP_TX_DEJ_OFF, 0, "DSP-TX_DEJITTER", bchannel->b_tx_dejitter);
394 if (bchannel->b_tx_gain)
395 ph_control(handle, DSP_VOL_CHANGE_TX, bchannel->b_tx_gain, "DSP-TX_GAIN", bchannel->b_tx_gain);
396 if (bchannel->b_rx_gain)
397 ph_control(handle, DSP_VOL_CHANGE_RX, bchannel->b_rx_gain, "DSP-RX_GAIN", bchannel->b_rx_gain);
398 if (bchannel->b_pipeline[0])
400 ph_control_block(handle, DSP_PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
402 ph_control_block(handle, PIPELINE_CFG, bchannel->b_pipeline, strlen(bchannel->b_pipeline)+1, "DSP-PIPELINE", 0);
404 if (bchannel->b_conf)
405 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
406 if (bchannel->b_echo)
407 ph_control(handle, DSP_ECHO_ON, 0, "DSP-ECHO", 1);
408 if (bchannel->b_tone)
409 ph_control(handle, DSP_TONE_PATT_ON, bchannel->b_tone, "DSP-TONE", bchannel->b_tone);
410 if (bchannel->b_rxoff)
411 ph_control(handle, DSP_RECEIVE_OFF, 0, "DSP-RXOFF", 1);
412 // if (bchannel->b_txmix)
413 // ph_control(handle, DSP_MIX_ON, 0, "DSP-MIX", 1);
414 if (bchannel->b_dtmf)
415 ph_control(handle, DTMF_TONE_START, 0, "DSP-DTMF", 1);
416 if (bchannel->b_crypt_len)
417 ph_control_block(handle, DSP_BF_ENABLE_KEY, bchannel->b_crypt_key, bchannel->b_crypt_len, "DSP-CRYPT", bchannel->b_crypt_len);
418 if (bchannel->b_conf)
419 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
421 bchannel->b_state = BSTATE_ACTIVE;
427 static void bchannel_destroy(struct bchannel *bchannel)
431 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove socket", DIRECTION_OUT);
432 add_trace("channel", NULL, "%d", i+1+(i>=15));
433 add_trace("socket", NULL, "%d", mISDNport->b_socket[i]);
436 if (bchannel->b_sock > -1)
438 close(bchannel->b_sock);
439 bchannel->b_sock = -1;
442 unsigned char buff[1024];
445 chan_trace_header(mISDNport, mISDNport->b_port[i], "BCHANNEL remove stack", DIRECTION_OUT);
446 add_trace("channel", NULL, "%d", i+1+(i>=15));
447 add_trace("stack", "id", "0x%08x", mISDNport->b_stid[i]);
448 add_trace("stack", "address", "0x%08x", mISDNport->b_addr[i]);
451 /* remove our stack only if set */
452 if (bchannel->b_addr)
454 CDEBUG(NULL, NULL, "free stack (b_addr=0x%x)\n", bchannel->b_addr);
455 mISDN_clear_stack(bchannel_device, bchannel->b_stid);
456 mISDN_write_frame(bchannel_device, buff, bchannel->b_addr | FLG_MSG_DOWN, MGR_DELLAYER | REQUEST, 0, 0, NULL, TIMEOUT_1SEC);
457 bchannel->b_stid = 0;
458 bchannel->b_addr = 0;
461 bchannel->b_state = BSTATE_IDLE;
466 * whenever we get audio data from bchannel, we process it here
468 static void bchannel_receive(struct bchannel *bchannel, unsigned long prim, unsigned long dinfo, unsigned char *data, int len)
470 unsigned long cont = *((unsigned long *)data);
471 // unsigned char *data_temp;
472 // unsigned long length_temp;
477 if (prim == PH_CONTROL_IND)
479 if (prim == (PH_CONTROL | INDICATION))
484 CERROR(NULL, NULL, "SHORT READ OF PH_CONTROL INDICATION\n");
487 if ((cont&(~DTMF_TONE_MASK)) == DTMF_TONE_VAL)
490 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
491 add_trace("DTMF", NULL, "%c", cont & DTMF_TONE_MASK);
494 if (bchannel->rx_dtmf)
495 bchannel->rx_dtmf(bchannel, cont & DTMF_TONE_MASK);
506 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
507 add_trace("DSP-CRYPT", NULL, "error");
518 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
519 add_trace("DSP-CRYPT", NULL, "ok");
526 chan_trace_header(p_m_mISDNport, this, "BCHANNEL control", DIRECTION_IN);
527 add_trace("unknown", NULL, "0x%x", cont);
536 if (prim == PH_DATA_REQ)
538 if (!bchannel->b_txdata)
540 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
541 CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
547 if (prim == (PH_SIGNAL | INDICATION))
552 if (!bchannel->b_txdata)
554 /* if tx is off, it may happen that fifos send us pending informations, we just ignore them */
555 CDEBUG(NULL, NULL, "ignoring tx data, because 'txdata' is turned off\n");
562 chan_trace_header(p_m_mISDNport, this, "BCHANNEL signal", DIRECTION_IN);
563 add_trace("unknown", NULL, "0x%x", frm->dinfo);
572 if (prim != PH_DATA_IND && prim != DL_DATA_IND)
574 CERROR(NULL, NULL, "Bchannel received unknown primitve: 0x%lx\n", prim);
577 /* calls will not process any audio data unless
578 * the call is connected OR interface features audio during call setup.
581 /* if rx is off, it may happen that fifos send us pending informations, we just ignore them */
582 if (bchannel->b_rxoff)
584 CDEBUG(NULL, NULL, "ignoring data, because rx is turned off\n");
590 CDEBUG(NULL, NULL, "ignoring data, because no call associated with bchannel\n");
593 if (!bchannel->call->audiopath)
595 /* return, because we have no audio from port */
598 len = write(bchannel->call->pipe[1], data, len);
601 CDEBUG(NULL, NULL, "broken pipe on bchannel pipe\n");
608 * transmit data to bchannel
610 void bchannel_transmit(struct bchannel *bchannel, unsigned char *data, int len)
612 unsigned char buff[1025];
614 struct mISDNhead *frm = (struct mISDNhead *)buff;
616 iframe_t *frm = (iframe_t *)buff;
620 if (bchannel->b_state != BSTATE_ACTIVE)
623 frm->prim = DL_DATA_REQ;
625 ret = sendto(bchannel->b_sock, data, len, 0, NULL, 0);
627 CERROR(NULL, NULL, "Failed to send to socket %d\n", bchannel->b_sock);
629 frm->prim = DL_DATA | REQUEST;
630 frm->addr = bchannel->b_addr | FLG_MSG_DOWN;
634 mISDN_write(bchannel_device, frm, mISDN_HEADER_LEN+frm->len, TIMEOUT_1SEC);
642 void bchannel_join(struct bchannel *bchannel, unsigned short id)
647 handle = bchannel->b_sock;
649 unsigned long handle;
651 handle = bchannel->b_addr;
654 bchannel->b_conf = (id<<16) + bchannel_pid;
655 bchannel->b_rxoff = 1;
657 bchannel->b_conf = 0;
658 bchannel->b_rxoff = 0;
660 if (bchannel->b_state == BSTATE_ACTIVE)
662 ph_control(handle, DSP_RECEIVE_OFF, bchannel->b_rxoff, "DSP-RX_OFF", bchannel->b_conf);
663 ph_control(handle, DSP_CONF_JOIN, bchannel->b_conf, "DSP-CONF", bchannel->b_conf);
669 * main loop for processing messages from mISDN
672 int bchannel_handle(void)
675 struct bchannel *bchannel;
676 char buffer[2048+MISDN_HEADER_LEN];
677 struct mISDNhead *hh = (struct mISDNhead *)buffer;
679 /* process all bchannels */
680 bchannel = bchannel_first;
683 /* handle message from bchannel */
684 if (bchannel->b_sock > -1)
686 ret = recv(bchannel->b_sock, buffer, sizeof(buffer), 0);
687 if (ret >= MISDN_HEADER_LEN)
692 /* we don't care about confirms, we use rx data to sync tx */
696 /* we receive audio data, we respond to it AND we send tones */
701 bchannel_receive(bchannel, hh->prim, hh->id, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
704 case PH_ACTIVATE_IND:
705 case DL_ESTABLISH_IND:
706 case PH_ACTIVATE_CNF:
707 case DL_ESTABLISH_CNF:
708 CDEBUG(NULL, NULL, "DL_ESTABLISH confirm: bchannel is now activated (socket %d).\n", bchannel->b_sock);
709 bchannel_activated(bchannel);
712 case PH_DEACTIVATE_IND:
714 case PH_DEACTIVATE_CNF:
716 CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (socket %d).\n", bchannel->b_sock);
717 // bchannel_deactivated(bchannel);
721 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);
725 if (ret < 0 && errno != EWOULDBLOCK)
726 CERROR(NULL, NULL, "Read from socket %d failed with return code %d\n", bchannel->b_sock, ret);
729 bchannel = bchannel->next;
732 /* if we received at least one b-frame, we will return 1 */
736 int bchannel_handle(void)
738 struct bchannel *bchannel;
740 unsigned char buffer[2048];
743 /* no device, no read */
744 if (bchannel_device < 0)
747 /* get message from kernel */
748 len = mISDN_read(bchannel_device, buffer, sizeof(buffer), 0);
753 CERROR(NULL, NULL, "Failed to do mISDN_read()\n");
758 // printf("%s: ERROR: mISDN_read() returns nothing\n");
761 frm = (iframe_t *)buffer;
766 case MGR_DELLAYER | CONFIRM:
767 case MGR_INITTIMER | CONFIRM:
768 case MGR_ADDTIMER | CONFIRM:
769 case MGR_DELTIMER | CONFIRM:
770 case MGR_REMOVETIMER | CONFIRM:
774 /* find the mISDNport that belongs to the stack */
775 bchannel = bchannel_first;
778 if (frm->addr == bchannel->b_addr)
780 bchannel = bchannel->next;
784 CERROR(NULL, NULL, "message belongs to no bchannel: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
791 /* we don't care about confirms, we use rx data to sync tx */
792 case PH_DATA | CONFIRM:
793 case DL_DATA | CONFIRM:
796 /* we receive audio data, we respond to it AND we send tones */
797 case PH_DATA | INDICATION:
798 case DL_DATA | INDICATION:
799 case PH_CONTROL | INDICATION:
800 case PH_SIGNAL | INDICATION:
801 bchannel_receive(bchannel, frm->prim, frm->dinfo, (unsigned char *)frm->data.p, frm->len);
804 case PH_ACTIVATE | INDICATION:
805 case DL_ESTABLISH | INDICATION:
806 case PH_ACTIVATE | CONFIRM:
807 case DL_ESTABLISH | CONFIRM:
808 CDEBUG(NULL, NULL, "DL_ESTABLISH confirm: bchannel is now activated (address 0x%x).\n", frm->addr);
809 bchannel_activated(bchannel);
812 case PH_DEACTIVATE | INDICATION:
813 case DL_RELEASE | INDICATION:
814 case PH_DEACTIVATE | CONFIRM:
815 case DL_RELEASE | CONFIRM:
816 CDEBUG(NULL, NULL, "DL_RELEASE confirm: bchannel is now de-activated (address 0x%x).\n", frm->addr);
817 // bchannel_deactivated(bchannel);
821 CERROR(NULL, NULL, "message not handled: prim(0x%x) addr(0x%x) msg->len(%d)\n", frm->prim, frm->addr, len);
831 * bchannel channel handling
833 struct bchannel *bchannel_first = NULL;
834 struct bchannel *find_bchannel_handle(unsigned long handle)
836 struct bchannel *bchannel = bchannel_first;
840 if (bchannel->handle == handle)
842 bchannel = bchannel->next;
848 struct bchannel *find_bchannel_ref(unsigned long ref)
850 struct bchannel *bchannel = bchannel_first;
854 if (bchannel->ref == ref)
856 bchannel = bchannel->next;
862 struct bchannel *alloc_bchannel(unsigned long handle)
864 struct bchannel **bchannelp = &bchannel_first;
867 bchannelp = &((*bchannelp)->next);
869 *bchannelp = (struct bchannel *)malloc(sizeof(struct bchannel));
872 (*bchannelp)->handle = handle;
873 (*bchannelp)->b_state = BSTATE_IDLE;
878 void free_bchannel(struct bchannel *bchannel)
880 struct bchannel **temp = &bchannel_first;
884 if (*temp == bchannel)
886 *temp = (*temp)->next;
888 if (bchannel->b_sock > -1)
890 if (bchannel->b_stid)
892 bchannel_destroy(bchannel);
895 if (bchannel->call->bchannel)
896 bchannel->call->bchannel = NULL;
901 temp = &((*temp)->next);