1 /* $NetBSD: bthub.c,v 1.26 2021/08/07 16:19:09 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Itronix Inc.
5 * All rights reserved.
6 *
7 * Written by Iain Hibbert for Itronix Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Itronix Inc. may not be used to endorse
18 * or promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: bthub.c,v 1.26 2021/08/07 16:19:09 thorpej Exp $");
36
37 #include <sys/param.h>
38 #include <sys/conf.h>
39 #include <sys/device.h>
40 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47
48 #include <prop/proplib.h>
49
50 #include <netbt/bluetooth.h>
51
52 #include <dev/bluetooth/btdev.h>
53
54 #include "ioconf.h"
55
56 /*****************************************************************************
57 *
58 * Bluetooth Device Hub
59 */
60
61 /* autoconf(9) glue */
62 static int bthub_match(device_t, cfdata_t, void *);
63 static void bthub_attach(device_t, device_t, void *);
64 static int bthub_detach(device_t, int);
65
66 CFATTACH_DECL_NEW(bthub, 0,
67 bthub_match, bthub_attach, bthub_detach, NULL);
68
69 /* control file */
70 dev_type_ioctl(bthubioctl);
71
72 const struct cdevsw bthub_cdevsw = {
73 .d_open = nullopen,
74 .d_close = nullclose,
75 .d_read = noread,
76 .d_write = nowrite,
77 .d_ioctl = bthubioctl,
78 .d_stop = nostop,
79 .d_tty = notty,
80 .d_poll = nopoll,
81 .d_mmap = nommap,
82 .d_kqfilter = nokqfilter,
83 .d_discard = nodiscard,
84 .d_flag = D_OTHER,
85 };
86
87 /* bthub functions */
88 static int bthub_print(void *, const char *);
89 static int bthub_pioctl(dev_t, unsigned long, prop_dictionary_t, int, struct lwp *);
90
91 /*****************************************************************************
92 *
93 * bthub autoconf(9) routines
94 *
95 * A Hub is attached to each Bluetooth Controller as it is enabled
96 */
97
98 static int
bthub_match(device_t self,cfdata_t cfdata,void * arg)99 bthub_match(device_t self, cfdata_t cfdata, void *arg)
100 {
101
102 return 1;
103 }
104
105 static void
bthub_attach(device_t parent,device_t self,void * aux)106 bthub_attach(device_t parent, device_t self, void *aux)
107 {
108 bdaddr_t *addr = aux;
109 prop_dictionary_t dict;
110 prop_object_t obj;
111
112 dict = device_properties(self);
113 obj = prop_data_create_copy(addr, sizeof(*addr));
114 prop_dictionary_set(dict, BTDEVladdr, obj);
115 prop_object_release(obj);
116
117 aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
118 BTDEVladdr,
119 addr->b[5], addr->b[4], addr->b[3],
120 addr->b[2], addr->b[1], addr->b[0]);
121
122 aprint_normal("\n");
123
124 if (!pmf_device_register(self, NULL, NULL)) {
125 /*
126 * XXX this should not be allowed to happen, but
127 * avoiding it needs a pretty big rearrangement of
128 * device attachments.
129 */
130 aprint_error_dev(self, "couldn't establish power handler\n");
131 }
132 }
133
134 static int
bthub_detach(device_t self,int flags)135 bthub_detach(device_t self, int flags)
136 {
137
138 pmf_device_deregister(self);
139
140 return config_detach_children(self, flags);
141 }
142
143 /*****************************************************************************
144 *
145 * bthub access functions to control device
146 */
147
148 int
bthubioctl(dev_t devno,unsigned long cmd,void * data,int flag,struct lwp * l)149 bthubioctl(dev_t devno, unsigned long cmd, void *data, int flag, struct lwp *l)
150 {
151 prop_dictionary_t dict;
152 int err;
153
154 switch(cmd) {
155 case BTDEV_ATTACH:
156 case BTDEV_DETACH:
157 /* load dictionary */
158 err = prop_dictionary_copyin_ioctl(data, cmd, &dict);
159 if (err == 0) {
160 err = bthub_pioctl(devno, cmd, dict, flag, l);
161 prop_object_release(dict);
162 }
163 break;
164
165 default:
166 err = EPASSTHROUGH;
167 break;
168 }
169
170 return err;
171 }
172
173 static int
bthub_pioctl(dev_t devno,unsigned long cmd,prop_dictionary_t dict,int flag,struct lwp * l)174 bthub_pioctl(dev_t devno, unsigned long cmd, prop_dictionary_t dict,
175 int flag, struct lwp *l)
176 {
177 prop_data_t laddr, raddr;
178 prop_string_t service;
179 prop_dictionary_t prop;
180 prop_object_t obj;
181 device_t dev, self;
182 deviter_t di;
183 int unit;
184
185 /* validate local address */
186 laddr = prop_dictionary_get(dict, BTDEVladdr);
187 if (prop_data_size(laddr) != sizeof(bdaddr_t))
188 return EINVAL;
189
190 /* locate the relevant bthub */
191 for (unit = 0 ; ; unit++) {
192 if (unit == bthub_cd.cd_ndevs)
193 return ENXIO;
194
195 self = device_lookup(&bthub_cd, unit);
196 if (self == NULL)
197 continue;
198
199 prop = device_properties(self);
200 obj = prop_dictionary_get(prop, BTDEVladdr);
201 if (prop_data_equals(laddr, obj))
202 break;
203 }
204
205 /* validate remote address */
206 raddr = prop_dictionary_get(dict, BTDEVraddr);
207 if (prop_data_size(raddr) != sizeof(bdaddr_t)
208 || bdaddr_any(prop_data_value(raddr)))
209 return EINVAL;
210
211 /* validate service name */
212 service = prop_dictionary_get(dict, BTDEVservice);
213 if (prop_object_type(service) != PROP_TYPE_STRING)
214 return EINVAL;
215
216 /* locate matching child device, if any */
217 deviter_init(&di, 0);
218 while ((dev = deviter_next(&di)) != NULL) {
219 if (device_parent(dev) != self)
220 continue;
221
222 prop = device_properties(dev);
223
224 obj = prop_dictionary_get(prop, BTDEVraddr);
225 if (!prop_object_equals(raddr, obj))
226 continue;
227
228 obj = prop_dictionary_get(prop, BTDEVservice);
229 if (!prop_object_equals(service, obj))
230 continue;
231
232 break;
233 }
234 deviter_release(&di);
235
236 switch (cmd) {
237 case BTDEV_ATTACH: /* attach BTDEV */
238 if (dev != NULL)
239 return EADDRINUSE;
240
241 dev = config_found(self, dict, bthub_print, CFARGS_NONE);
242 if (dev == NULL)
243 return ENXIO;
244
245 prop = device_properties(dev);
246 prop_dictionary_set(prop, BTDEVladdr, laddr);
247 prop_dictionary_set(prop, BTDEVraddr, raddr);
248 prop_dictionary_set(prop, BTDEVservice, service);
249 break;
250
251 case BTDEV_DETACH: /* detach BTDEV */
252 if (dev == NULL)
253 return ENXIO;
254
255 config_detach(dev, DETACH_FORCE);
256 break;
257 }
258
259 return 0;
260 }
261
262 static int
bthub_print(void * aux,const char * pnp)263 bthub_print(void *aux, const char *pnp)
264 {
265 prop_dictionary_t dict = aux;
266 prop_object_t obj;
267 const bdaddr_t *raddr;
268
269 if (pnp != NULL) {
270 obj = prop_dictionary_get(dict, BTDEVtype);
271 aprint_normal("%s: %s '%s',", pnp, BTDEVtype,
272 prop_string_value(obj));
273 }
274
275 obj = prop_dictionary_get(dict, BTDEVraddr);
276 raddr = prop_data_value(obj);
277
278 aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
279 BTDEVraddr,
280 raddr->b[5], raddr->b[4], raddr->b[3],
281 raddr->b[2], raddr->b[1], raddr->b[0]);
282
283 return UNCONF;
284 }
285