Home | History | Annotate | Line # | Download | only in netbt
hci_ioctl.c revision 1.5.24.1
      1  1.5.24.1   bouyer /*	$NetBSD: hci_ioctl.c,v 1.5.24.1 2007/11/13 16:02:47 bouyer 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.5.24.1   bouyer __KERNEL_RCSID(0, "$NetBSD: hci_ioctl.c,v 1.5.24.1 2007/11/13 16:02:47 bouyer 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.1  gdamore 	printf("HCI:\n");
     64       1.1  gdamore 	SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
     65       1.1  gdamore 		printf("UNIT %s: flags 0x%4.4x, "
     66       1.1  gdamore 			"num_cmd=%d, num_acl=%d, num_sco=%d\n",
     67  1.5.24.1   bouyer 			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.1  gdamore 			printf("+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.1  gdamore 	printf("L2CAP:\n");
     83       1.1  gdamore 	LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) {
     84       1.1  gdamore 		printf("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.1  gdamore 		printf("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.1  gdamore 	printf("RFCOMM:\n");
    100       1.1  gdamore 	LIST_FOREACH(rs, &rfcomm_session_active, rs_next) {
    101       1.1  gdamore 		chan = rs->rs_l2cap;
    102       1.1  gdamore 		printf("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.1  gdamore 			printf("+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.1  gdamore 		printf("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.1  gdamore 			printf("+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.1  gdamore 	int s, 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.1  gdamore 		SIMPLEQ_FOREACH(unit, &hci_unit_list, hci_next) {
    179  1.5.24.1   bouyer 			if (strncmp(device_xname(unit->hci_dev),
    180  1.5.24.1   bouyer 			    btr->btr_name, HCI_DEVNAME_SIZE) == 0)
    181       1.1  gdamore 				break;
    182       1.1  gdamore 		}
    183       1.1  gdamore 
    184       1.1  gdamore 		if (unit == NULL)
    185       1.1  gdamore 			return ENXIO;
    186       1.1  gdamore 
    187       1.1  gdamore 		break;
    188       1.1  gdamore 
    189       1.1  gdamore 	default:	/* not one of mine */
    190       1.1  gdamore 		return EPASSTHROUGH;
    191       1.1  gdamore 	}
    192       1.1  gdamore 
    193       1.1  gdamore 	switch(cmd) {
    194       1.1  gdamore 	case SIOCNBTINFO:	/* get next info */
    195       1.1  gdamore 		if (unit)
    196       1.1  gdamore 			unit = SIMPLEQ_NEXT(unit, hci_next);
    197       1.1  gdamore 		else
    198       1.1  gdamore 			unit = SIMPLEQ_FIRST(&hci_unit_list);
    199       1.1  gdamore 
    200       1.1  gdamore 		if (unit == NULL) {
    201       1.1  gdamore 			err = ENXIO;
    202       1.1  gdamore 			break;
    203       1.1  gdamore 		}
    204       1.1  gdamore 
    205       1.1  gdamore 		/* and fall through to */
    206       1.1  gdamore 	case SIOCGBTINFO:	/* get unit info */
    207       1.1  gdamore 	case SIOCGBTINFOA:	/* get info by address */
    208       1.1  gdamore 		memset(btr, 0, sizeof(struct btreq));
    209  1.5.24.1   bouyer 		strlcpy(btr->btr_name, device_xname(unit->hci_dev), HCI_DEVNAME_SIZE);
    210       1.1  gdamore 		bdaddr_copy(&btr->btr_bdaddr, &unit->hci_bdaddr);
    211       1.1  gdamore 
    212       1.1  gdamore 		btr->btr_flags = unit->hci_flags;
    213       1.1  gdamore 
    214       1.1  gdamore 		btr->btr_num_cmd = unit->hci_num_cmd_pkts;
    215       1.1  gdamore 		btr->btr_num_acl = unit->hci_num_acl_pkts;
    216       1.1  gdamore 		btr->btr_num_sco = unit->hci_num_sco_pkts;
    217       1.1  gdamore 		btr->btr_acl_mtu = unit->hci_max_acl_size;
    218       1.1  gdamore 		btr->btr_sco_mtu = unit->hci_max_sco_size;
    219       1.1  gdamore 
    220       1.1  gdamore 		btr->btr_packet_type = unit->hci_packet_type;
    221       1.1  gdamore 		btr->btr_link_policy = unit->hci_link_policy;
    222       1.1  gdamore 		break;
    223       1.1  gdamore 
    224       1.1  gdamore 	case SIOCSBTFLAGS:	/* set unit flags (privileged) */
    225       1.2       ad 		err = kauth_authorize_generic(l->l_cred,
    226       1.5     elad 		    KAUTH_GENERIC_ISSUSER, NULL);
    227       1.1  gdamore 		if (err)
    228       1.1  gdamore 			break;
    229       1.1  gdamore 
    230       1.1  gdamore 		if ((unit->hci_flags & BTF_UP)
    231       1.1  gdamore 		    && (btr->btr_flags & BTF_UP) == 0) {
    232       1.1  gdamore 			hci_disable(unit);
    233       1.1  gdamore 			unit->hci_flags &= ~BTF_UP;
    234       1.1  gdamore 		}
    235       1.1  gdamore 
    236       1.1  gdamore 		s = splraiseipl(unit->hci_ipl);
    237       1.1  gdamore 		unit->hci_flags |= (btr->btr_flags & BTF_INIT);
    238       1.1  gdamore 		splx(s);
    239       1.1  gdamore 
    240       1.1  gdamore 		if ((unit->hci_flags & BTF_UP) == 0
    241       1.1  gdamore 		    && (btr->btr_flags & BTF_UP)) {
    242       1.1  gdamore 			err = hci_enable(unit);
    243       1.1  gdamore 			if (err)
    244       1.1  gdamore 				break;
    245       1.1  gdamore 
    246       1.1  gdamore 			s = splraiseipl(unit->hci_ipl);
    247       1.1  gdamore 			unit->hci_flags |= BTF_UP;
    248       1.1  gdamore 			splx(s);
    249       1.1  gdamore 		}
    250       1.1  gdamore 
    251       1.1  gdamore 		btr->btr_flags = unit->hci_flags;
    252       1.1  gdamore 		break;
    253       1.1  gdamore 
    254       1.1  gdamore 	case SIOCSBTPOLICY:	/* set unit link policy (privileged) */
    255       1.2       ad 		err = kauth_authorize_generic(l->l_cred,
    256       1.5     elad 		    KAUTH_GENERIC_ISSUSER, NULL);
    257       1.1  gdamore 		if (err)
    258       1.1  gdamore 			break;
    259       1.1  gdamore 
    260       1.1  gdamore 		unit->hci_link_policy = btr->btr_link_policy;
    261       1.1  gdamore 		unit->hci_link_policy &= unit->hci_lmp_mask;
    262       1.1  gdamore 		btr->btr_link_policy = unit->hci_link_policy;
    263       1.1  gdamore 		break;
    264       1.1  gdamore 
    265       1.1  gdamore 	case SIOCSBTPTYPE:	/* set unit packet types (privileged) */
    266       1.2       ad 		err = kauth_authorize_generic(l->l_cred,
    267       1.5     elad 		    KAUTH_GENERIC_ISSUSER, NULL);
    268       1.1  gdamore 		if (err)
    269       1.1  gdamore 			break;
    270       1.1  gdamore 
    271       1.1  gdamore 		unit->hci_packet_type = btr->btr_packet_type;
    272       1.1  gdamore 		unit->hci_packet_type &= unit->hci_acl_mask;
    273       1.1  gdamore 		btr->btr_packet_type = unit->hci_packet_type;
    274       1.1  gdamore 		break;
    275       1.1  gdamore 
    276       1.1  gdamore 	case SIOCGBTSTATS:	/* get unit statistics */
    277       1.1  gdamore 		s = splraiseipl(unit->hci_ipl);
    278       1.1  gdamore 		memcpy(&btr->btr_stats, &unit->hci_stats,
    279       1.1  gdamore 			sizeof(struct bt_stats));
    280       1.1  gdamore 		splx(s);
    281       1.1  gdamore 		break;
    282       1.1  gdamore 
    283       1.1  gdamore 	case SIOCZBTSTATS:	/* get & reset unit statistics */
    284       1.2       ad 		err = kauth_authorize_generic(l->l_cred,
    285       1.5     elad 		    KAUTH_GENERIC_ISSUSER, NULL);
    286       1.1  gdamore 		if (err)
    287       1.1  gdamore 			break;
    288       1.1  gdamore 
    289       1.1  gdamore 		s = splraiseipl(unit->hci_ipl);
    290       1.1  gdamore 		memcpy(&btr->btr_stats, &unit->hci_stats,
    291       1.1  gdamore 			sizeof(struct bt_stats));
    292       1.1  gdamore 		memset(&unit->hci_stats, 0, sizeof(struct bt_stats));
    293       1.1  gdamore 		splx(s);
    294       1.1  gdamore 
    295       1.1  gdamore 		break;
    296       1.1  gdamore 
    297       1.3   plunky 	case SIOCSBTSCOMTU:	/* set sco_mtu value for unit */
    298       1.3   plunky 		/*
    299       1.3   plunky 		 * This is a temporary ioctl and may not be supported
    300       1.3   plunky 		 * in the future. The need is that if SCO packets are
    301       1.3   plunky 		 * sent to USB bluetooth controllers that are not an
    302       1.3   plunky 		 * integer number of frame sizes, the USB bus locks up.
    303       1.3   plunky 		 */
    304       1.3   plunky 		err = kauth_authorize_generic(l->l_cred,
    305       1.5     elad 		    KAUTH_GENERIC_ISSUSER, NULL);
    306       1.3   plunky 		if (err)
    307       1.3   plunky 			break;
    308       1.3   plunky 
    309       1.3   plunky 		unit->hci_max_sco_size = btr->btr_sco_mtu;
    310       1.3   plunky 		break;
    311       1.3   plunky 
    312       1.1  gdamore 	default:
    313       1.1  gdamore 		err = EFAULT;
    314       1.1  gdamore 		break;
    315       1.1  gdamore 	}
    316       1.1  gdamore 
    317       1.1  gdamore 	return err;
    318       1.1  gdamore }
    319