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