Home | History | Annotate | Line # | Download | only in netbt
hci_ioctl.c revision 1.9.4.1
      1  1.9.4.1    rmind /*	$NetBSD: hci_ioctl.c,v 1.9.4.1 2011/03/05 20:55:56 rmind Exp $	*/
      2      1.1  gdamore 
      3      1.1  gdamore /*-
      4      1.1  gdamore  * Copyright (c) 2005 Iain Hibbert.
      5      1.1  gdamore  * Copyright (c) 2006 Itronix Inc.
      6      1.1  gdamore  * All rights reserved.
      7      1.1  gdamore  *
      8      1.1  gdamore  * Redistribution and use in source and binary forms, with or without
      9      1.1  gdamore  * modification, are permitted provided that the following conditions
     10      1.1  gdamore  * are met:
     11      1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     12      1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     13      1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     15      1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     16      1.1  gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     17      1.1  gdamore  *    or promote products derived from this software without specific
     18      1.1  gdamore  *    prior written permission.
     19      1.1  gdamore  *
     20      1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     21      1.1  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22      1.1  gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23      1.1  gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     24      1.1  gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     25      1.1  gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26      1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27      1.1  gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     28      1.1  gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29      1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30      1.1  gdamore  * POSSIBILITY OF SUCH DAMAGE.
     31      1.1  gdamore  */
     32      1.1  gdamore 
     33      1.1  gdamore #include <sys/cdefs.h>
     34  1.9.4.1    rmind __KERNEL_RCSID(0, "$NetBSD: hci_ioctl.c,v 1.9.4.1 2011/03/05 20:55:56 rmind Exp $");
     35      1.1  gdamore 
     36      1.1  gdamore #include <sys/param.h>
     37      1.1  gdamore #include <sys/domain.h>
     38      1.1  gdamore #include <sys/ioctl.h>
     39      1.1  gdamore #include <sys/kauth.h>
     40      1.1  gdamore #include <sys/kernel.h>
     41      1.1  gdamore #include <sys/mbuf.h>
     42      1.1  gdamore #include <sys/proc.h>
     43      1.1  gdamore #include <sys/systm.h>
     44      1.1  gdamore 
     45      1.1  gdamore #include <netbt/bluetooth.h>
     46      1.1  gdamore #include <netbt/hci.h>
     47      1.1  gdamore #include <netbt/l2cap.h>
     48      1.1  gdamore #include <netbt/rfcomm.h>
     49      1.1  gdamore 
     50      1.1  gdamore #ifdef BLUETOOTH_DEBUG
     51      1.1  gdamore #define BDADDR(bd)	(bd).b[5], (bd).b[4], (bd).b[3],	\
     52      1.1  gdamore 			(bd).b[2], (bd).b[1], (bd).b[0]
     53      1.1  gdamore 
     54      1.1  gdamore static void
     55      1.1  gdamore hci_dump(void)
     56      1.1  gdamore {
     57      1.1  gdamore 	struct hci_unit *unit;
     58      1.1  gdamore 	struct hci_link *link;
     59      1.1  gdamore 	struct l2cap_channel *chan;
     60      1.1  gdamore 	struct rfcomm_session *rs;
     61      1.1  gdamore 	struct rfcomm_dlc *dlc;
     62      1.1  gdamore 
     63      1.7   plunky 	uprintf("HCI:\n");
     64      1.1  gdamore 	SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
     65      1.7   plunky 		uprintf("UNIT %s: flags 0x%4.4x, "
     66      1.1  gdamore 			"num_cmd=%d, num_acl=%d, num_sco=%d\n",
     67      1.6   plunky 			device_xname(unit->hci_dev), unit->hci_flags,
     68      1.1  gdamore 			unit->hci_num_cmd_pkts,
     69      1.1  gdamore 			unit->hci_num_acl_pkts,
     70      1.1  gdamore 			unit->hci_num_sco_pkts);
     71      1.1  gdamore 		TAILQ_FOREACH(link, &unit->hci_links, hl_next) {
     72      1.7   plunky 			uprintf("+HANDLE #%d: %s "
     73      1.1  gdamore 			    "raddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
     74      1.1  gdamore 			    "state %d, refcnt %d\n",
     75      1.1  gdamore 			    link->hl_handle,
     76      1.1  gdamore 			    (link->hl_type == HCI_LINK_ACL ? "ACL":"SCO"),
     77      1.1  gdamore 			    BDADDR(link->hl_bdaddr),
     78      1.1  gdamore 			    link->hl_state, link->hl_refcnt);
     79      1.1  gdamore 		}
     80      1.1  gdamore 	}
     81      1.1  gdamore 
     82      1.7   plunky 	uprintf("L2CAP:\n");
     83      1.1  gdamore 	LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) {
     84      1.7   plunky 		uprintf("CID #%d state %d, psm=0x%4.4x, "
     85      1.1  gdamore 		    "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
     86      1.1  gdamore 		    "raddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
     87      1.1  gdamore 		    chan->lc_lcid, chan->lc_state, chan->lc_raddr.bt_psm,
     88      1.1  gdamore 		    BDADDR(chan->lc_laddr.bt_bdaddr),
     89      1.1  gdamore 		    BDADDR(chan->lc_raddr.bt_bdaddr));
     90      1.1  gdamore 	}
     91      1.1  gdamore 
     92      1.1  gdamore 	LIST_FOREACH(chan, &l2cap_listen_list, lc_ncid) {
     93      1.7   plunky 		uprintf("LISTEN psm=0x%4.4x, "
     94      1.1  gdamore 		    "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
     95      1.1  gdamore 		    chan->lc_laddr.bt_psm,
     96      1.1  gdamore 		    BDADDR(chan->lc_laddr.bt_bdaddr));
     97      1.1  gdamore 	}
     98      1.1  gdamore 
     99      1.7   plunky 	uprintf("RFCOMM:\n");
    100      1.1  gdamore 	LIST_FOREACH(rs, &rfcomm_session_active, rs_next) {
    101      1.1  gdamore 		chan = rs->rs_l2cap;
    102      1.7   plunky 		uprintf("SESSION: state=%d, flags=0x%4.4x, psm 0x%4.4x "
    103      1.1  gdamore 		    "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
    104      1.1  gdamore 		    "raddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
    105      1.1  gdamore 		    rs->rs_state, rs->rs_flags, chan->lc_raddr.bt_psm,
    106      1.1  gdamore 		    BDADDR(chan->lc_laddr.bt_bdaddr),
    107      1.1  gdamore 		    BDADDR(chan->lc_raddr.bt_bdaddr));
    108      1.1  gdamore 		LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) {
    109      1.7   plunky 			uprintf("+DLC channel=%d, dlci=%d, "
    110      1.4   plunky 			    "state=%d, flags=0x%4.4x, rxcred=%d, rxsize=%ld, "
    111      1.1  gdamore 			    "txcred=%d, pending=%d, txqlen=%d\n",
    112      1.1  gdamore 			    dlc->rd_raddr.bt_channel, dlc->rd_dlci,
    113      1.1  gdamore 			    dlc->rd_state, dlc->rd_flags,
    114      1.4   plunky 			    dlc->rd_rxcred, (unsigned long)dlc->rd_rxsize,
    115      1.1  gdamore 			    dlc->rd_txcred, dlc->rd_pending,
    116      1.1  gdamore 			    (dlc->rd_txbuf ? dlc->rd_txbuf->m_pkthdr.len : 0));
    117      1.1  gdamore 		}
    118      1.1  gdamore 	}
    119      1.1  gdamore 
    120      1.1  gdamore 	LIST_FOREACH(rs, &rfcomm_session_listen, rs_next) {
    121      1.1  gdamore 		chan = rs->rs_l2cap;
    122      1.7   plunky 		uprintf("LISTEN: psm 0x%4.4x, "
    123      1.1  gdamore 		    "laddr=%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x\n",
    124      1.1  gdamore 		    chan->lc_laddr.bt_psm,
    125      1.1  gdamore 		    BDADDR(chan->lc_laddr.bt_bdaddr));
    126      1.1  gdamore 		LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next)
    127      1.7   plunky 			uprintf("+DLC channel=%d\n", dlc->rd_laddr.bt_channel);
    128      1.1  gdamore 	}
    129      1.1  gdamore }
    130      1.1  gdamore 
    131      1.1  gdamore #undef BDADDR
    132      1.1  gdamore #endif
    133      1.1  gdamore 
    134      1.1  gdamore int
    135      1.2       ad hci_ioctl(unsigned long cmd, void *data, struct lwp *l)
    136      1.1  gdamore {
    137      1.1  gdamore 	struct btreq *btr = data;
    138      1.1  gdamore 	struct hci_unit *unit;
    139      1.7   plunky 	int err = 0;
    140      1.1  gdamore 
    141      1.1  gdamore 	DPRINTFN(1, "cmd %#lx\n", cmd);
    142      1.1  gdamore 
    143      1.1  gdamore 	switch(cmd) {
    144      1.1  gdamore #ifdef BLUETOOTH_DEBUG
    145      1.1  gdamore 	case SIOCBTDUMP:
    146      1.1  gdamore 		hci_dump();
    147      1.1  gdamore 		return 0;
    148      1.1  gdamore #endif
    149      1.1  gdamore 	/*
    150      1.1  gdamore 	 * Get unit info based on address rather than name
    151      1.1  gdamore 	 */
    152      1.1  gdamore 	case SIOCGBTINFOA:
    153      1.1  gdamore 		unit = hci_unit_lookup(&btr->btr_bdaddr);
    154      1.1  gdamore 		if (unit == NULL)
    155      1.1  gdamore 			return ENXIO;
    156      1.1  gdamore 
    157      1.1  gdamore 		break;
    158      1.1  gdamore 
    159      1.1  gdamore 	/*
    160      1.1  gdamore 	 * The remaining ioctl's all use the same btreq structure and
    161      1.1  gdamore 	 * index on the name of the device, so we look that up first.
    162      1.1  gdamore 	 */
    163      1.1  gdamore 	case SIOCNBTINFO:
    164      1.1  gdamore 		/* empty name means give the first unit */
    165      1.1  gdamore 		if (btr->btr_name[0] == '\0') {
    166      1.1  gdamore 			unit = NULL;
    167      1.1  gdamore 			break;
    168      1.1  gdamore 		}
    169      1.1  gdamore 
    170      1.1  gdamore 		/* else fall through and look it up */
    171      1.1  gdamore 	case SIOCGBTINFO:
    172      1.1  gdamore 	case SIOCSBTFLAGS:
    173      1.1  gdamore 	case SIOCSBTPOLICY:
    174      1.1  gdamore 	case SIOCSBTPTYPE:
    175      1.1  gdamore 	case SIOCGBTSTATS:
    176      1.1  gdamore 	case SIOCZBTSTATS:
    177      1.3   plunky 	case SIOCSBTSCOMTU:
    178  1.9.4.1    rmind 	case SIOCGBTFEAT:
    179      1.1  gdamore 		SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
    180      1.6   plunky 			if (strncmp(device_xname(unit->hci_dev),
    181      1.6   plunky 			    btr->btr_name, HCI_DEVNAME_SIZE) == 0)
    182      1.1  gdamore 				break;
    183      1.1  gdamore 		}
    184      1.1  gdamore 
    185      1.1  gdamore 		if (unit == NULL)
    186      1.1  gdamore 			return ENXIO;
    187      1.1  gdamore 
    188      1.1  gdamore 		break;
    189      1.1  gdamore 
    190      1.1  gdamore 	default:	/* not one of mine */
    191      1.1  gdamore 		return EPASSTHROUGH;
    192      1.1  gdamore 	}
    193      1.1  gdamore 
    194      1.1  gdamore 	switch(cmd) {
    195      1.1  gdamore 	case SIOCNBTINFO:	/* get next info */
    196      1.1  gdamore 		if (unit)
    197      1.1  gdamore 			unit = SIMPLEQ_NEXT(unit, hci_next);
    198      1.1  gdamore 		else
    199      1.1  gdamore 			unit = SIMPLEQ_FIRST(&hci_unit_list);
    200      1.1  gdamore 
    201      1.1  gdamore 		if (unit == NULL) {
    202      1.1  gdamore 			err = ENXIO;
    203      1.1  gdamore 			break;
    204      1.1  gdamore 		}
    205      1.1  gdamore 
    206      1.1  gdamore 		/* and fall through to */
    207      1.1  gdamore 	case SIOCGBTINFO:	/* get unit info */
    208      1.1  gdamore 	case SIOCGBTINFOA:	/* get info by address */
    209      1.1  gdamore 		memset(btr, 0, sizeof(struct btreq));
    210      1.6   plunky 		strlcpy(btr->btr_name, device_xname(unit->hci_dev), HCI_DEVNAME_SIZE);
    211      1.1  gdamore 		bdaddr_copy(&btr->btr_bdaddr, &unit->hci_bdaddr);
    212      1.1  gdamore 
    213      1.1  gdamore 		btr->btr_flags = unit->hci_flags;
    214      1.1  gdamore 
    215      1.1  gdamore 		btr->btr_num_cmd = unit->hci_num_cmd_pkts;
    216      1.1  gdamore 		btr->btr_num_acl = unit->hci_num_acl_pkts;
    217      1.1  gdamore 		btr->btr_num_sco = unit->hci_num_sco_pkts;
    218      1.1  gdamore 		btr->btr_acl_mtu = unit->hci_max_acl_size;
    219      1.1  gdamore 		btr->btr_sco_mtu = unit->hci_max_sco_size;
    220  1.9.4.1    rmind 		btr->btr_max_acl = unit->hci_max_acl_pkts;
    221  1.9.4.1    rmind 		btr->btr_max_sco = unit->hci_max_sco_pkts;
    222      1.1  gdamore 
    223      1.1  gdamore 		btr->btr_packet_type = unit->hci_packet_type;
    224      1.1  gdamore 		btr->btr_link_policy = unit->hci_link_policy;
    225      1.1  gdamore 		break;
    226      1.1  gdamore 
    227      1.1  gdamore 	case SIOCSBTFLAGS:	/* set unit flags (privileged) */
    228      1.8     elad 		err = kauth_authorize_device(l->l_cred,
    229      1.8     elad 		    KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd),
    230      1.8     elad 		    btr, NULL);
    231      1.1  gdamore 		if (err)
    232      1.1  gdamore 			break;
    233      1.1  gdamore 
    234      1.1  gdamore 		if ((unit->hci_flags & BTF_UP)
    235      1.1  gdamore 		    && (btr->btr_flags & BTF_UP) == 0) {
    236      1.1  gdamore 			hci_disable(unit);
    237      1.1  gdamore 			unit->hci_flags &= ~BTF_UP;
    238      1.1  gdamore 		}
    239      1.1  gdamore 
    240      1.9   plunky 		unit->hci_flags &= ~BTF_MASTER;
    241      1.9   plunky 		unit->hci_flags |= (btr->btr_flags & (BTF_INIT | BTF_MASTER));
    242      1.1  gdamore 
    243      1.1  gdamore 		if ((unit->hci_flags & BTF_UP) == 0
    244      1.1  gdamore 		    && (btr->btr_flags & BTF_UP)) {
    245      1.1  gdamore 			err = hci_enable(unit);
    246      1.1  gdamore 			if (err)
    247      1.1  gdamore 				break;
    248      1.1  gdamore 
    249      1.1  gdamore 			unit->hci_flags |= BTF_UP;
    250      1.1  gdamore 		}
    251      1.1  gdamore 
    252      1.1  gdamore 		btr->btr_flags = unit->hci_flags;
    253      1.1  gdamore 		break;
    254      1.1  gdamore 
    255      1.1  gdamore 	case SIOCSBTPOLICY:	/* set unit link policy (privileged) */
    256      1.8     elad 		err = kauth_authorize_device(l->l_cred,
    257      1.8     elad 		    KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd),
    258      1.8     elad 		    btr, NULL);
    259      1.1  gdamore 		if (err)
    260      1.1  gdamore 			break;
    261      1.1  gdamore 
    262      1.1  gdamore 		unit->hci_link_policy = btr->btr_link_policy;
    263      1.1  gdamore 		unit->hci_link_policy &= unit->hci_lmp_mask;
    264      1.1  gdamore 		btr->btr_link_policy = unit->hci_link_policy;
    265      1.1  gdamore 		break;
    266      1.1  gdamore 
    267      1.1  gdamore 	case SIOCSBTPTYPE:	/* set unit packet types (privileged) */
    268      1.8     elad 		err = kauth_authorize_device(l->l_cred,
    269      1.8     elad 		    KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd),
    270      1.8     elad 		    btr, NULL);
    271      1.1  gdamore 		if (err)
    272      1.1  gdamore 			break;
    273      1.1  gdamore 
    274      1.1  gdamore 		unit->hci_packet_type = btr->btr_packet_type;
    275      1.1  gdamore 		unit->hci_packet_type &= unit->hci_acl_mask;
    276      1.1  gdamore 		btr->btr_packet_type = unit->hci_packet_type;
    277      1.1  gdamore 		break;
    278      1.1  gdamore 
    279      1.1  gdamore 	case SIOCGBTSTATS:	/* get unit statistics */
    280      1.7   plunky 		(*unit->hci_if->get_stats)(unit->hci_dev, &btr->btr_stats, 0);
    281      1.1  gdamore 		break;
    282      1.1  gdamore 
    283      1.1  gdamore 	case SIOCZBTSTATS:	/* get & reset unit statistics */
    284      1.8     elad 		err = kauth_authorize_device(l->l_cred,
    285      1.8     elad 		    KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd),
    286      1.8     elad 		    btr, NULL);
    287      1.1  gdamore 		if (err)
    288      1.1  gdamore 			break;
    289      1.1  gdamore 
    290      1.7   plunky 		(*unit->hci_if->get_stats)(unit->hci_dev, &btr->btr_stats, 1);
    291      1.1  gdamore 		break;
    292      1.1  gdamore 
    293      1.3   plunky 	case SIOCSBTSCOMTU:	/* set sco_mtu value for unit */
    294      1.3   plunky 		/*
    295      1.3   plunky 		 * This is a temporary ioctl and may not be supported
    296      1.3   plunky 		 * in the future. The need is that if SCO packets are
    297      1.3   plunky 		 * sent to USB bluetooth controllers that are not an
    298      1.3   plunky 		 * integer number of frame sizes, the USB bus locks up.
    299      1.3   plunky 		 */
    300      1.8     elad 		err = kauth_authorize_device(l->l_cred,
    301      1.8     elad 		    KAUTH_DEVICE_BLUETOOTH_SETPRIV, unit, KAUTH_ARG(cmd),
    302      1.8     elad 		    btr, NULL);
    303      1.3   plunky 		if (err)
    304      1.3   plunky 			break;
    305      1.3   plunky 
    306      1.3   plunky 		unit->hci_max_sco_size = btr->btr_sco_mtu;
    307      1.3   plunky 		break;
    308      1.3   plunky 
    309  1.9.4.1    rmind 	case SIOCGBTFEAT:	/* get unit features */
    310  1.9.4.1    rmind 		memset(btr, 0, sizeof(struct btreq));
    311  1.9.4.1    rmind 		strlcpy(btr->btr_name, device_xname(unit->hci_dev), HCI_DEVNAME_SIZE);
    312  1.9.4.1    rmind 		memcpy(btr->btr_features0, unit->hci_feat0, HCI_FEATURES_SIZE);
    313  1.9.4.1    rmind 		memcpy(btr->btr_features1, unit->hci_feat1, HCI_FEATURES_SIZE);
    314  1.9.4.1    rmind 		break;
    315  1.9.4.1    rmind 
    316      1.1  gdamore 	default:
    317      1.1  gdamore 		err = EFAULT;
    318      1.1  gdamore 		break;
    319      1.1  gdamore 	}
    320      1.1  gdamore 
    321      1.1  gdamore 	return err;
    322      1.1  gdamore }
    323