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