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
|
/* $NetBSD: bthub.c,v 1.26 2021/08/07 16:19:09 thorpej Exp $ */
/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Written by Iain Hibbert for Itronix Inc.
*
* 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>
__KERNEL_RCSID(0, "$NetBSD: bthub.c,v 1.26 2021/08/07 16:19:09 thorpej Exp $");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <prop/proplib.h>
#include <netbt/bluetooth.h>
#include <dev/bluetooth/btdev.h>
#include "ioconf.h"
/*****************************************************************************
*
* Bluetooth Device Hub
*/
/* autoconf(9) glue */
static int bthub_match(device_t, cfdata_t, void *);
static void bthub_attach(device_t, device_t, void *);
static int bthub_detach(device_t, int);
CFATTACH_DECL_NEW(bthub, 0,
bthub_match, bthub_attach, bthub_detach, NULL);
/* control file */
dev_type_ioctl(bthubioctl);
const struct cdevsw bthub_cdevsw = {
.d_open = nullopen,
.d_close = nullclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = bthubioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER,
};
/* bthub functions */
static int bthub_print(void *, const char *);
static int bthub_pioctl(dev_t, unsigned long, prop_dictionary_t, int, struct lwp *);
/*****************************************************************************
*
* bthub autoconf(9) routines
*
* A Hub is attached to each Bluetooth Controller as it is enabled
*/
static int
bthub_match(device_t self, cfdata_t cfdata, void *arg)
{
return 1;
}
static void
bthub_attach(device_t parent, device_t self, void *aux)
{
bdaddr_t *addr = aux;
prop_dictionary_t dict;
prop_object_t obj;
dict = device_properties(self);
obj = prop_data_create_copy(addr, sizeof(*addr));
prop_dictionary_set(dict, BTDEVladdr, obj);
prop_object_release(obj);
aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
BTDEVladdr,
addr->b[5], addr->b[4], addr->b[3],
addr->b[2], addr->b[1], addr->b[0]);
aprint_normal("\n");
if (!pmf_device_register(self, NULL, NULL)) {
/*
* XXX this should not be allowed to happen, but
* avoiding it needs a pretty big rearrangement of
* device attachments.
*/
aprint_error_dev(self, "couldn't establish power handler\n");
}
}
static int
bthub_detach(device_t self, int flags)
{
pmf_device_deregister(self);
return config_detach_children(self, flags);
}
/*****************************************************************************
*
* bthub access functions to control device
*/
int
bthubioctl(dev_t devno, unsigned long cmd, void *data, int flag, struct lwp *l)
{
prop_dictionary_t dict;
int err;
switch(cmd) {
case BTDEV_ATTACH:
case BTDEV_DETACH:
/* load dictionary */
err = prop_dictionary_copyin_ioctl(data, cmd, &dict);
if (err == 0) {
err = bthub_pioctl(devno, cmd, dict, flag, l);
prop_object_release(dict);
}
break;
default:
err = EPASSTHROUGH;
break;
}
return err;
}
static int
bthub_pioctl(dev_t devno, unsigned long cmd, prop_dictionary_t dict,
int flag, struct lwp *l)
{
prop_data_t laddr, raddr;
prop_string_t service;
prop_dictionary_t prop;
prop_object_t obj;
device_t dev, self;
deviter_t di;
int unit;
/* validate local address */
laddr = prop_dictionary_get(dict, BTDEVladdr);
if (prop_data_size(laddr) != sizeof(bdaddr_t))
return EINVAL;
/* locate the relevant bthub */
for (unit = 0 ; ; unit++) {
if (unit == bthub_cd.cd_ndevs)
return ENXIO;
self = device_lookup(&bthub_cd, unit);
if (self == NULL)
continue;
prop = device_properties(self);
obj = prop_dictionary_get(prop, BTDEVladdr);
if (prop_data_equals(laddr, obj))
break;
}
/* validate remote address */
raddr = prop_dictionary_get(dict, BTDEVraddr);
if (prop_data_size(raddr) != sizeof(bdaddr_t)
|| bdaddr_any(prop_data_value(raddr)))
return EINVAL;
/* validate service name */
service = prop_dictionary_get(dict, BTDEVservice);
if (prop_object_type(service) != PROP_TYPE_STRING)
return EINVAL;
/* locate matching child device, if any */
deviter_init(&di, 0);
while ((dev = deviter_next(&di)) != NULL) {
if (device_parent(dev) != self)
continue;
prop = device_properties(dev);
obj = prop_dictionary_get(prop, BTDEVraddr);
if (!prop_object_equals(raddr, obj))
continue;
obj = prop_dictionary_get(prop, BTDEVservice);
if (!prop_object_equals(service, obj))
continue;
break;
}
deviter_release(&di);
switch (cmd) {
case BTDEV_ATTACH: /* attach BTDEV */
if (dev != NULL)
return EADDRINUSE;
dev = config_found(self, dict, bthub_print, CFARGS_NONE);
if (dev == NULL)
return ENXIO;
prop = device_properties(dev);
prop_dictionary_set(prop, BTDEVladdr, laddr);
prop_dictionary_set(prop, BTDEVraddr, raddr);
prop_dictionary_set(prop, BTDEVservice, service);
break;
case BTDEV_DETACH: /* detach BTDEV */
if (dev == NULL)
return ENXIO;
config_detach(dev, DETACH_FORCE);
break;
}
return 0;
}
static int
bthub_print(void *aux, const char *pnp)
{
prop_dictionary_t dict = aux;
prop_object_t obj;
const bdaddr_t *raddr;
if (pnp != NULL) {
obj = prop_dictionary_get(dict, BTDEVtype);
aprint_normal("%s: %s '%s',", pnp, BTDEVtype,
prop_string_value(obj));
}
obj = prop_dictionary_get(dict, BTDEVraddr);
raddr = prop_data_value(obj);
aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
BTDEVraddr,
raddr->b[5], raddr->b[4], raddr->b[3],
raddr->b[2], raddr->b[1], raddr->b[0]);
return UNCONF;
}
|