Home | History | Annotate | Line # | Download | only in netbt
rfcomm_upper.c revision 1.3.2.1
      1  1.3.2.1       ad /*	$NetBSD: rfcomm_upper.c,v 1.3.2.1 2007/04/10 13:26:49 ad Exp $	*/
      2      1.1  gdamore 
      3      1.1  gdamore /*-
      4      1.1  gdamore  * Copyright (c) 2006 Itronix Inc.
      5      1.1  gdamore  * All rights reserved.
      6      1.1  gdamore  *
      7      1.1  gdamore  * Written by Iain Hibbert for Itronix Inc.
      8      1.1  gdamore  *
      9      1.1  gdamore  * Redistribution and use in source and binary forms, with or without
     10      1.1  gdamore  * modification, are permitted provided that the following conditions
     11      1.1  gdamore  * are met:
     12      1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     13      1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     14      1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     16      1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     17      1.1  gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     18      1.1  gdamore  *    or promote products derived from this software without specific
     19      1.1  gdamore  *    prior written permission.
     20      1.1  gdamore  *
     21      1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22      1.1  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23      1.1  gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24      1.1  gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25      1.1  gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26      1.1  gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27      1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28      1.1  gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29      1.1  gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30      1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31      1.1  gdamore  * POSSIBILITY OF SUCH DAMAGE.
     32      1.1  gdamore  */
     33      1.1  gdamore 
     34      1.1  gdamore #include <sys/cdefs.h>
     35  1.3.2.1       ad __KERNEL_RCSID(0, "$NetBSD: rfcomm_upper.c,v 1.3.2.1 2007/04/10 13:26:49 ad Exp $");
     36      1.1  gdamore 
     37      1.1  gdamore #include <sys/param.h>
     38      1.1  gdamore #include <sys/kernel.h>
     39      1.1  gdamore #include <sys/mbuf.h>
     40      1.1  gdamore #include <sys/proc.h>
     41      1.1  gdamore #include <sys/systm.h>
     42      1.1  gdamore 
     43      1.1  gdamore #include <netbt/bluetooth.h>
     44      1.1  gdamore #include <netbt/hci.h>
     45      1.1  gdamore #include <netbt/l2cap.h>
     46      1.1  gdamore #include <netbt/rfcomm.h>
     47      1.1  gdamore 
     48      1.1  gdamore /****************************************************************************
     49      1.1  gdamore  *
     50      1.1  gdamore  *	RFCOMM DLC - Upper Protocol API
     51      1.1  gdamore  *
     52      1.1  gdamore  * Currently the only 'Port Emulation Entity' is the RFCOMM socket code
     53      1.1  gdamore  * but it is should be possible to provide a pseudo-device for a direct
     54      1.1  gdamore  * tty interface.
     55      1.1  gdamore  */
     56      1.1  gdamore 
     57      1.1  gdamore /*
     58      1.1  gdamore  * rfcomm_attach(handle, proto, upper)
     59      1.1  gdamore  *
     60      1.1  gdamore  * attach a new RFCOMM DLC to handle, populate with reasonable defaults
     61      1.1  gdamore  */
     62      1.1  gdamore int
     63      1.1  gdamore rfcomm_attach(struct rfcomm_dlc **handle,
     64      1.1  gdamore 		const struct btproto *proto, void *upper)
     65      1.1  gdamore {
     66      1.1  gdamore 	struct rfcomm_dlc *dlc;
     67      1.1  gdamore 
     68  1.3.2.1       ad 	KASSERT(handle != NULL);
     69  1.3.2.1       ad 	KASSERT(proto != NULL);
     70  1.3.2.1       ad 	KASSERT(upper != NULL);
     71      1.1  gdamore 
     72      1.1  gdamore 	dlc = malloc(sizeof(struct rfcomm_dlc), M_BLUETOOTH, M_NOWAIT | M_ZERO);
     73      1.1  gdamore 	if (dlc == NULL)
     74      1.1  gdamore 		return ENOMEM;
     75      1.1  gdamore 
     76      1.1  gdamore 	dlc->rd_state = RFCOMM_DLC_CLOSED;
     77      1.1  gdamore 	dlc->rd_mtu = rfcomm_mtu_default;
     78      1.1  gdamore 
     79      1.1  gdamore 	dlc->rd_proto = proto;
     80      1.1  gdamore 	dlc->rd_upper = upper;
     81      1.1  gdamore 
     82      1.1  gdamore 	dlc->rd_laddr.bt_len = sizeof(struct sockaddr_bt);
     83      1.1  gdamore 	dlc->rd_laddr.bt_family = AF_BLUETOOTH;
     84      1.1  gdamore 	dlc->rd_laddr.bt_psm = L2CAP_PSM_RFCOMM;
     85      1.1  gdamore 
     86      1.1  gdamore 	dlc->rd_raddr.bt_len = sizeof(struct sockaddr_bt);
     87      1.1  gdamore 	dlc->rd_raddr.bt_family = AF_BLUETOOTH;
     88      1.1  gdamore 	dlc->rd_raddr.bt_psm = L2CAP_PSM_RFCOMM;
     89      1.1  gdamore 
     90      1.1  gdamore 	dlc->rd_lmodem = RFCOMM_MSC_RTC | RFCOMM_MSC_RTR | RFCOMM_MSC_DV;
     91      1.1  gdamore 
     92      1.1  gdamore 	callout_init(&dlc->rd_timeout);
     93      1.1  gdamore 	callout_setfunc(&dlc->rd_timeout, rfcomm_dlc_timeout, dlc);
     94      1.1  gdamore 
     95      1.1  gdamore 	*handle = dlc;
     96      1.1  gdamore 	return 0;
     97      1.1  gdamore }
     98      1.1  gdamore 
     99      1.1  gdamore /*
    100      1.1  gdamore  * rfcomm_bind(dlc, sockaddr)
    101      1.1  gdamore  *
    102      1.1  gdamore  * bind DLC to local address
    103      1.1  gdamore  */
    104      1.1  gdamore int
    105      1.1  gdamore rfcomm_bind(struct rfcomm_dlc *dlc, struct sockaddr_bt *addr)
    106      1.1  gdamore {
    107      1.1  gdamore 
    108      1.1  gdamore 	memcpy(&dlc->rd_laddr, addr, sizeof(struct sockaddr_bt));
    109      1.1  gdamore 	return 0;
    110      1.1  gdamore }
    111      1.1  gdamore 
    112      1.1  gdamore /*
    113      1.1  gdamore  * rfcomm_sockaddr(dlc, sockaddr)
    114      1.1  gdamore  *
    115      1.1  gdamore  * return local address
    116      1.1  gdamore  */
    117      1.1  gdamore int
    118      1.1  gdamore rfcomm_sockaddr(struct rfcomm_dlc *dlc, struct sockaddr_bt *addr)
    119      1.1  gdamore {
    120      1.1  gdamore 
    121      1.1  gdamore 	memcpy(addr, &dlc->rd_laddr, sizeof(struct sockaddr_bt));
    122      1.1  gdamore 	return 0;
    123      1.1  gdamore }
    124      1.1  gdamore 
    125      1.1  gdamore /*
    126      1.1  gdamore  * rfcomm_connect(dlc, sockaddr)
    127      1.1  gdamore  *
    128      1.1  gdamore  * Initiate connection of RFCOMM DLC to remote address.
    129      1.1  gdamore  */
    130      1.1  gdamore int
    131      1.1  gdamore rfcomm_connect(struct rfcomm_dlc *dlc, struct sockaddr_bt *dest)
    132      1.1  gdamore {
    133      1.1  gdamore 	struct rfcomm_session *rs;
    134      1.1  gdamore 	int err = 0;
    135      1.1  gdamore 
    136      1.1  gdamore 	if (dlc->rd_state != RFCOMM_DLC_CLOSED)
    137      1.1  gdamore 		return EISCONN;
    138      1.1  gdamore 
    139      1.1  gdamore 	memcpy(&dlc->rd_raddr, dest, sizeof(struct sockaddr_bt));
    140      1.1  gdamore 
    141      1.1  gdamore 	if (dlc->rd_raddr.bt_channel < RFCOMM_CHANNEL_MIN
    142      1.1  gdamore 	    || dlc->rd_raddr.bt_channel > RFCOMM_CHANNEL_MAX
    143      1.1  gdamore 	    || bdaddr_any(&dlc->rd_raddr.bt_bdaddr))
    144      1.1  gdamore 		return EDESTADDRREQ;
    145      1.1  gdamore 
    146      1.1  gdamore 	if (dlc->rd_raddr.bt_psm == L2CAP_PSM_ANY)
    147      1.1  gdamore 		dlc->rd_raddr.bt_psm = L2CAP_PSM_RFCOMM;
    148      1.1  gdamore 	else if (dlc->rd_raddr.bt_psm != L2CAP_PSM_RFCOMM
    149      1.1  gdamore 	    && (dlc->rd_raddr.bt_psm < 0x1001
    150      1.1  gdamore 	    || L2CAP_PSM_INVALID(dlc->rd_raddr.bt_psm)))
    151      1.1  gdamore 		return EINVAL;
    152      1.1  gdamore 
    153      1.1  gdamore 	/*
    154      1.1  gdamore 	 * We are allowed only one RFCOMM session between any 2 Bluetooth
    155      1.1  gdamore 	 * devices, so see if there is a session already otherwise create
    156      1.1  gdamore 	 * one and set it connecting.
    157      1.1  gdamore 	 */
    158      1.1  gdamore 	rs = rfcomm_session_lookup(&dlc->rd_laddr, &dlc->rd_raddr);
    159      1.1  gdamore 	if (rs == NULL) {
    160      1.1  gdamore 		rs = rfcomm_session_alloc(&rfcomm_session_active,
    161      1.1  gdamore 						&dlc->rd_laddr);
    162      1.1  gdamore 		if (rs == NULL)
    163      1.1  gdamore 			return ENOMEM;
    164      1.1  gdamore 
    165      1.1  gdamore 		rs->rs_flags |= RFCOMM_SESSION_INITIATOR;
    166      1.1  gdamore 		rs->rs_state = RFCOMM_SESSION_WAIT_CONNECT;
    167      1.1  gdamore 
    168      1.1  gdamore 		err = l2cap_connect(rs->rs_l2cap, &dlc->rd_raddr);
    169      1.1  gdamore 		if (err) {
    170      1.1  gdamore 			rfcomm_session_free(rs);
    171      1.1  gdamore 			return err;
    172      1.1  gdamore 		}
    173      1.1  gdamore 
    174      1.1  gdamore 		/*
    175      1.1  gdamore 		 * This session will start up automatically when its
    176      1.1  gdamore 		 * L2CAP channel is connected.
    177      1.1  gdamore 		 */
    178      1.1  gdamore 	}
    179      1.1  gdamore 
    180      1.1  gdamore 	/* construct DLC */
    181      1.1  gdamore 	dlc->rd_dlci = RFCOMM_MKDLCI(IS_INITIATOR(rs) ? 0:1, dest->bt_channel);
    182      1.1  gdamore 	if (rfcomm_dlc_lookup(rs, dlc->rd_dlci))
    183      1.1  gdamore 		return EBUSY;
    184      1.1  gdamore 
    185      1.1  gdamore 	l2cap_sockaddr(rs->rs_l2cap, &dlc->rd_laddr);
    186      1.1  gdamore 
    187      1.1  gdamore 	/*
    188      1.1  gdamore 	 * attach the DLC to the session and start it off
    189      1.1  gdamore 	 */
    190      1.1  gdamore 	dlc->rd_session = rs;
    191      1.1  gdamore 	dlc->rd_state = RFCOMM_DLC_WAIT_SESSION;
    192      1.1  gdamore 	LIST_INSERT_HEAD(&rs->rs_dlcs, dlc, rd_next);
    193      1.1  gdamore 
    194      1.1  gdamore 	if (rs->rs_state == RFCOMM_SESSION_OPEN)
    195      1.1  gdamore 		err = rfcomm_dlc_connect(dlc);
    196      1.1  gdamore 
    197      1.1  gdamore 	return err;
    198      1.1  gdamore }
    199      1.1  gdamore 
    200      1.1  gdamore /*
    201      1.1  gdamore  * rfcomm_peeraddr(dlc, sockaddr)
    202      1.1  gdamore  *
    203      1.1  gdamore  * return remote address
    204      1.1  gdamore  */
    205      1.1  gdamore int
    206      1.1  gdamore rfcomm_peeraddr(struct rfcomm_dlc *dlc, struct sockaddr_bt *addr)
    207      1.1  gdamore {
    208      1.1  gdamore 
    209      1.1  gdamore 	memcpy(addr, &dlc->rd_raddr, sizeof(struct sockaddr_bt));
    210      1.1  gdamore 	return 0;
    211      1.1  gdamore }
    212      1.1  gdamore 
    213      1.1  gdamore /*
    214      1.1  gdamore  * rfcomm_disconnect(dlc, linger)
    215      1.1  gdamore  *
    216      1.1  gdamore  * disconnect RFCOMM DLC
    217      1.1  gdamore  */
    218      1.1  gdamore int
    219      1.1  gdamore rfcomm_disconnect(struct rfcomm_dlc *dlc, int linger)
    220      1.1  gdamore {
    221      1.1  gdamore 	struct rfcomm_session *rs = dlc->rd_session;
    222      1.1  gdamore 	int err = 0;
    223      1.1  gdamore 
    224      1.1  gdamore 	KASSERT(dlc != NULL);
    225      1.1  gdamore 
    226      1.1  gdamore 	switch (dlc->rd_state) {
    227      1.1  gdamore 	case RFCOMM_DLC_CLOSED:
    228      1.1  gdamore 	case RFCOMM_DLC_LISTEN:
    229      1.1  gdamore 		return EINVAL;
    230      1.1  gdamore 
    231      1.1  gdamore 	case RFCOMM_DLC_WAIT_SESSION:
    232      1.1  gdamore 		rfcomm_dlc_close(dlc, 0);
    233      1.1  gdamore 		break;
    234      1.1  gdamore 
    235      1.1  gdamore 	case RFCOMM_DLC_OPEN:
    236      1.1  gdamore 		if (dlc->rd_txbuf != NULL && linger != 0) {
    237      1.1  gdamore 			dlc->rd_flags |= RFCOMM_DLC_SHUTDOWN;
    238      1.1  gdamore 			break;
    239      1.1  gdamore 		}
    240      1.1  gdamore 
    241      1.1  gdamore 		/* else fall through */
    242      1.1  gdamore 	case RFCOMM_DLC_WAIT_CONNECT:
    243      1.1  gdamore 		dlc->rd_state = RFCOMM_DLC_WAIT_DISCONNECT;
    244      1.1  gdamore 		err = rfcomm_session_send_frame(rs, RFCOMM_FRAME_DISC,
    245      1.1  gdamore 							dlc->rd_dlci);
    246      1.1  gdamore 		callout_schedule(&dlc->rd_timeout, rfcomm_ack_timeout * hz);
    247      1.1  gdamore 		break;
    248      1.1  gdamore 
    249      1.1  gdamore 	case RFCOMM_DLC_WAIT_DISCONNECT:
    250      1.1  gdamore 		err = EALREADY;
    251      1.1  gdamore 		break;
    252      1.1  gdamore 
    253      1.1  gdamore 	default:
    254      1.1  gdamore 		UNKNOWN(dlc->rd_state);
    255      1.1  gdamore 		break;
    256      1.1  gdamore 	}
    257      1.1  gdamore 
    258      1.1  gdamore 	return err;
    259      1.1  gdamore }
    260      1.1  gdamore 
    261      1.1  gdamore /*
    262      1.1  gdamore  * rfcomm_detach(handle)
    263      1.1  gdamore  *
    264      1.1  gdamore  * detach RFCOMM DLC from handle
    265      1.1  gdamore  */
    266      1.1  gdamore int
    267      1.1  gdamore rfcomm_detach(struct rfcomm_dlc **handle)
    268      1.1  gdamore {
    269      1.1  gdamore 	struct rfcomm_dlc *dlc = *handle;
    270      1.1  gdamore 
    271      1.1  gdamore 	if (dlc->rd_state != RFCOMM_DLC_CLOSED)
    272      1.1  gdamore 		rfcomm_dlc_close(dlc, 0);
    273      1.1  gdamore 
    274      1.1  gdamore 	if (dlc->rd_txbuf != NULL) {
    275      1.1  gdamore 		m_freem(dlc->rd_txbuf);
    276      1.1  gdamore 		dlc->rd_txbuf = NULL;
    277      1.1  gdamore 	}
    278      1.1  gdamore 
    279      1.1  gdamore 	dlc->rd_upper = NULL;
    280      1.1  gdamore 	*handle = NULL;
    281      1.1  gdamore 
    282      1.1  gdamore 	/*
    283      1.1  gdamore 	 * If callout is invoking we can't free the DLC so
    284      1.1  gdamore 	 * mark it and let the callout release it.
    285      1.1  gdamore 	 */
    286      1.1  gdamore 	if (callout_invoking(&dlc->rd_timeout))
    287      1.1  gdamore 		dlc->rd_flags |= RFCOMM_DLC_DETACH;
    288      1.1  gdamore 	else
    289      1.1  gdamore 		free(dlc, M_BLUETOOTH);
    290      1.1  gdamore 
    291      1.1  gdamore 	return 0;
    292      1.1  gdamore }
    293      1.1  gdamore 
    294      1.1  gdamore /*
    295      1.1  gdamore  * rfcomm_listen(dlc)
    296      1.1  gdamore  *
    297      1.1  gdamore  * This DLC is a listener. We look for an existing listening session
    298      1.1  gdamore  * with a matching address to attach to or else create a new one on
    299      1.1  gdamore  * the listeners list.
    300      1.1  gdamore  */
    301      1.1  gdamore int
    302      1.1  gdamore rfcomm_listen(struct rfcomm_dlc *dlc)
    303      1.1  gdamore {
    304      1.1  gdamore 	struct rfcomm_session *rs, *any, *best;
    305      1.1  gdamore 	struct sockaddr_bt addr;
    306      1.1  gdamore 	int err;
    307      1.1  gdamore 
    308      1.1  gdamore 	if (dlc->rd_state != RFCOMM_DLC_CLOSED)
    309      1.1  gdamore 		return EISCONN;
    310      1.1  gdamore 
    311      1.1  gdamore 	if (dlc->rd_laddr.bt_channel < RFCOMM_CHANNEL_MIN
    312      1.1  gdamore 	    || dlc->rd_laddr.bt_channel > RFCOMM_CHANNEL_MAX)
    313      1.1  gdamore 		return EADDRNOTAVAIL;
    314      1.1  gdamore 
    315      1.1  gdamore 	if (dlc->rd_laddr.bt_psm == L2CAP_PSM_ANY)
    316      1.1  gdamore 		dlc->rd_laddr.bt_psm = L2CAP_PSM_RFCOMM;
    317      1.1  gdamore 	else if (dlc->rd_laddr.bt_psm != L2CAP_PSM_RFCOMM
    318      1.1  gdamore 	    && (dlc->rd_laddr.bt_psm < 0x1001
    319      1.1  gdamore 	    || L2CAP_PSM_INVALID(dlc->rd_laddr.bt_psm)))
    320      1.1  gdamore 		return EADDRNOTAVAIL;
    321      1.1  gdamore 
    322      1.1  gdamore 	any = best = NULL;
    323      1.1  gdamore 	LIST_FOREACH(rs, &rfcomm_session_listen, rs_next) {
    324      1.1  gdamore 		l2cap_sockaddr(rs->rs_l2cap, &addr);
    325      1.1  gdamore 
    326      1.1  gdamore 		if (addr.bt_psm != dlc->rd_laddr.bt_psm)
    327      1.1  gdamore 			continue;
    328      1.1  gdamore 
    329      1.1  gdamore 		if (bdaddr_same(&dlc->rd_laddr.bt_bdaddr, &addr.bt_bdaddr))
    330      1.1  gdamore 			best = rs;
    331      1.1  gdamore 
    332      1.1  gdamore 		if (bdaddr_any(&addr.bt_bdaddr))
    333      1.1  gdamore 			any = rs;
    334      1.1  gdamore 	}
    335      1.1  gdamore 
    336      1.1  gdamore 	rs = best ? best : any;
    337      1.1  gdamore 	if (rs == NULL) {
    338      1.1  gdamore 		rs = rfcomm_session_alloc(&rfcomm_session_listen,
    339      1.1  gdamore 						&dlc->rd_laddr);
    340      1.1  gdamore 		if (rs == NULL)
    341      1.1  gdamore 			return ENOMEM;
    342      1.1  gdamore 
    343      1.1  gdamore 		rs->rs_state = RFCOMM_SESSION_LISTEN;
    344      1.1  gdamore 
    345      1.1  gdamore 		err = l2cap_listen(rs->rs_l2cap);
    346      1.1  gdamore 		if (err) {
    347      1.1  gdamore 			rfcomm_session_free(rs);
    348      1.1  gdamore 			return err;
    349      1.1  gdamore 		}
    350      1.1  gdamore 	}
    351      1.1  gdamore 
    352      1.1  gdamore 	dlc->rd_session = rs;
    353      1.1  gdamore 	dlc->rd_state = RFCOMM_DLC_LISTEN;
    354      1.1  gdamore 	LIST_INSERT_HEAD(&rs->rs_dlcs, dlc, rd_next);
    355      1.1  gdamore 
    356      1.1  gdamore 	return 0;
    357      1.1  gdamore }
    358      1.1  gdamore 
    359      1.1  gdamore /*
    360      1.1  gdamore  * rfcomm_send(dlc, mbuf)
    361      1.1  gdamore  *
    362      1.1  gdamore  * Output data on DLC. This is streamed data, so we add it
    363      1.1  gdamore  * to our buffer and start the the DLC, which will assemble
    364      1.1  gdamore  * packets and send them if it can.
    365      1.1  gdamore  */
    366      1.1  gdamore int
    367      1.1  gdamore rfcomm_send(struct rfcomm_dlc *dlc, struct mbuf *m)
    368      1.1  gdamore {
    369      1.1  gdamore 
    370      1.1  gdamore 	if (dlc->rd_txbuf != NULL) {
    371      1.1  gdamore 		dlc->rd_txbuf->m_pkthdr.len += m->m_pkthdr.len;
    372      1.1  gdamore 		m_cat(dlc->rd_txbuf, m);
    373      1.1  gdamore 	} else {
    374      1.1  gdamore 		dlc->rd_txbuf = m;
    375      1.1  gdamore 	}
    376      1.1  gdamore 
    377      1.1  gdamore 	if (dlc->rd_state == RFCOMM_DLC_OPEN)
    378      1.1  gdamore 		rfcomm_dlc_start(dlc);
    379      1.1  gdamore 
    380      1.1  gdamore 	return 0;
    381      1.1  gdamore }
    382      1.1  gdamore 
    383      1.1  gdamore /*
    384      1.1  gdamore  * rfcomm_rcvd(dlc, space)
    385      1.1  gdamore  *
    386      1.1  gdamore  * Indicate space now available in receive buffer
    387      1.1  gdamore  *
    388      1.1  gdamore  * This should be used to give an initial value of the receive buffer
    389      1.1  gdamore  * size when the DLC is attached and anytime data is cleared from the
    390      1.1  gdamore  * buffer after that.
    391      1.1  gdamore  */
    392      1.1  gdamore int
    393      1.1  gdamore rfcomm_rcvd(struct rfcomm_dlc *dlc, size_t space)
    394      1.1  gdamore {
    395      1.1  gdamore 
    396      1.1  gdamore 	KASSERT(dlc != NULL);
    397      1.1  gdamore 
    398      1.1  gdamore 	dlc->rd_rxsize = space;
    399      1.1  gdamore 
    400      1.1  gdamore 	/*
    401      1.1  gdamore 	 * if we are using credit based flow control, we may
    402      1.1  gdamore 	 * want to send some credits..
    403      1.1  gdamore 	 */
    404      1.1  gdamore 	if (dlc->rd_state == RFCOMM_DLC_OPEN
    405      1.1  gdamore 	    && (dlc->rd_session->rs_flags & RFCOMM_SESSION_CFC))
    406      1.1  gdamore 		rfcomm_dlc_start(dlc);
    407      1.1  gdamore 
    408      1.1  gdamore 	return 0;
    409      1.1  gdamore }
    410      1.1  gdamore 
    411      1.1  gdamore /*
    412      1.1  gdamore  * rfcomm_setopt(dlc, option, addr)
    413      1.1  gdamore  *
    414      1.1  gdamore  * set DLC options
    415      1.1  gdamore  */
    416      1.1  gdamore int
    417      1.1  gdamore rfcomm_setopt(struct rfcomm_dlc *dlc, int opt, void *addr)
    418      1.1  gdamore {
    419      1.1  gdamore 	int err = 0;
    420      1.3   plunky 	uint16_t mtu;
    421      1.1  gdamore 
    422      1.1  gdamore 	switch (opt) {
    423      1.1  gdamore 	case SO_RFCOMM_MTU:
    424      1.3   plunky 		mtu = *(uint16_t *)addr;
    425      1.3   plunky 		if (mtu < RFCOMM_MTU_MIN || mtu > RFCOMM_MTU_MAX)
    426      1.1  gdamore 			err = EINVAL;
    427  1.3.2.1       ad 		else if (dlc->rd_state == RFCOMM_DLC_CLOSED)
    428      1.3   plunky 			dlc->rd_mtu = mtu;
    429  1.3.2.1       ad 		else
    430  1.3.2.1       ad 			err = EBUSY;
    431      1.3   plunky 
    432      1.1  gdamore 		break;
    433      1.1  gdamore 
    434      1.1  gdamore 	default:
    435      1.2   plunky 		err = ENOPROTOOPT;
    436      1.1  gdamore 		break;
    437      1.1  gdamore 	}
    438      1.1  gdamore 	return err;
    439      1.1  gdamore }
    440      1.1  gdamore 
    441      1.1  gdamore /*
    442      1.1  gdamore  * rfcomm_getopt(dlc, option, addr)
    443      1.1  gdamore  *
    444      1.1  gdamore  * get DLC options
    445      1.1  gdamore  */
    446      1.1  gdamore int
    447      1.1  gdamore rfcomm_getopt(struct rfcomm_dlc *dlc, int opt, void *addr)
    448      1.1  gdamore {
    449      1.1  gdamore 	struct rfcomm_fc_info *fc;
    450      1.1  gdamore 
    451      1.1  gdamore 	switch (opt) {
    452      1.1  gdamore 	case SO_RFCOMM_MTU:
    453      1.1  gdamore 		*(uint16_t *)addr = dlc->rd_mtu;
    454      1.1  gdamore 		return sizeof(uint16_t);
    455      1.1  gdamore 
    456      1.1  gdamore 	case SO_RFCOMM_FC_INFO:
    457      1.1  gdamore 		fc = addr;
    458      1.1  gdamore 		memset(fc, 0, sizeof(*fc));
    459      1.1  gdamore 		fc->lmodem = dlc->rd_lmodem;
    460      1.1  gdamore 		fc->rmodem = dlc->rd_rmodem;
    461      1.1  gdamore 		fc->tx_cred = max(dlc->rd_txcred, 0xff);
    462      1.1  gdamore 		fc->rx_cred = max(dlc->rd_rxcred, 0xff);
    463      1.1  gdamore 		if (dlc->rd_session
    464      1.1  gdamore 		    && (dlc->rd_session->rs_flags & RFCOMM_SESSION_CFC))
    465      1.1  gdamore 			fc->cfc = 1;
    466      1.1  gdamore 
    467      1.1  gdamore 		return sizeof(*fc);
    468      1.1  gdamore 
    469      1.1  gdamore 	default:
    470      1.1  gdamore 		break;
    471      1.1  gdamore 	}
    472      1.1  gdamore 
    473      1.1  gdamore 	return 0;
    474      1.1  gdamore }
    475