bthub.c revision 1.12 1 /* $NetBSD: bthub.c,v 1.12 2007/11/03 17:41:03 plunky 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.12 2007/11/03 17:41:03 plunky 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 struct bthub_softc {
62 device_t sc_dev;
63 LIST_HEAD(,btdev) sc_list;
64 };
65
66 /* autoconf(9) glue */
67 static int bthub_match(device_t, struct cfdata *, void *);
68 static void bthub_attach(device_t, device_t, void *);
69 static int bthub_detach(device_t, int);
70
71 CFATTACH_DECL_NEW(bthub, sizeof(struct bthub_softc),
72 bthub_match, bthub_attach, bthub_detach, NULL);
73
74 /* control file */
75 dev_type_ioctl(bthubioctl);
76
77 const struct cdevsw bthub_cdevsw = {
78 nullopen, nullclose, noread, nowrite, bthubioctl,
79 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
80 };
81
82 /* bthub functions */
83 static int bthub_print(void *, const char *);
84 static int bthub_pioctl(dev_t, unsigned long, prop_dictionary_t, int, struct lwp *);
85
86 /*****************************************************************************
87 *
88 * bthub autoconf(9) routines
89 *
90 * A Hub is attached to each Bluetooth Controller as it is enabled
91 */
92
93 static int
94 bthub_match(device_t self, struct cfdata *cfdata, void *arg)
95 {
96
97 return 1;
98 }
99
100 static void
101 bthub_attach(device_t parent, device_t self, void *aux)
102 {
103 struct bthub_softc *sc = device_private(self);
104 bdaddr_t *addr = aux;
105 prop_dictionary_t dict;
106 prop_object_t obj;
107
108 LIST_INIT(&sc->sc_list);
109 sc->sc_dev = self;
110
111 dict = device_properties(self);
112 obj = prop_data_create_data(addr, sizeof(*addr));
113 prop_dictionary_set(dict, BTDEVladdr, obj);
114 prop_object_release(obj);
115
116 aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
117 BTDEVladdr,
118 addr->b[5], addr->b[4], addr->b[3],
119 addr->b[2], addr->b[1], addr->b[0]);
120
121 aprint_normal("\n");
122 }
123
124 static int
125 bthub_detach(device_t self, int flags)
126 {
127 struct bthub_softc *sc = device_private(self);
128 struct btdev *btdev;
129 int err;
130
131 while (!LIST_EMPTY(&sc->sc_list)) {
132 btdev = LIST_FIRST(&sc->sc_list);
133 LIST_REMOVE(btdev, sc_next);
134
135 err = config_detach(btdev->sc_dev, flags);
136 if (err && (flags & DETACH_FORCE) == 0) {
137 LIST_INSERT_HEAD(&sc->sc_list, btdev, sc_next);
138 return err;
139 }
140 }
141
142 return 0;
143 }
144
145
146 /*****************************************************************************
147 *
148 * bthub access functions to control device
149 */
150
151 int
152 bthubioctl(dev_t devno, unsigned long cmd, void *data, int flag, struct lwp *l)
153 {
154 prop_dictionary_t dict;
155 int err;
156
157 switch(cmd) {
158 case BTDEV_ATTACH:
159 case BTDEV_DETACH:
160 /* load dictionary */
161 err = prop_dictionary_copyin_ioctl(data, cmd, &dict);
162 if (err == 0) {
163 err = bthub_pioctl(devno, cmd, dict, flag, l);
164 prop_object_release(dict);
165 }
166 break;
167
168 default:
169 err = EPASSTHROUGH;
170 break;
171 }
172
173 return err;
174 }
175
176 static int
177 bthub_pioctl(dev_t devno, unsigned long cmd, prop_dictionary_t dict,
178 int flag, struct lwp *l)
179 {
180 struct bthub_softc *sc;
181 struct btdev *btdev;
182 prop_data_t laddr, raddr;
183 prop_string_t service;
184 prop_dictionary_t prop;
185 prop_object_t obj;
186 device_t dev;
187 int unit;
188
189 /* validate local address */
190 laddr = prop_dictionary_get(dict, BTDEVladdr);
191 if (prop_data_size(laddr) != sizeof(bdaddr_t))
192 return EINVAL;
193
194 /* locate the relevant bthub */
195 for (unit = 0 ; ; unit++) {
196 if (unit == bthub_cd.cd_ndevs)
197 return ENXIO;
198
199 dev = bthub_cd.cd_devs[unit];
200 if (dev == NULL)
201 continue;
202
203 prop = device_properties(dev);
204 obj = prop_dictionary_get(prop, BTDEVladdr);
205 if (prop_data_equals(laddr, obj))
206 break;
207 }
208 sc = device_private(dev);
209
210 /* validate remote address */
211 raddr = prop_dictionary_get(dict, BTDEVraddr);
212 if (prop_data_size(raddr) != sizeof(bdaddr_t)
213 || bdaddr_any(prop_data_data_nocopy(raddr)))
214 return EINVAL;
215
216 /* validate service name */
217 service = prop_dictionary_get(dict, BTDEVservice);
218 if (prop_object_type(service) != PROP_TYPE_STRING)
219 return EINVAL;
220
221 /* locate matching child device, if any */
222 LIST_FOREACH(btdev, &sc->sc_list, sc_next) {
223 prop = device_properties(btdev->sc_dev);
224
225 obj = prop_dictionary_get(prop, BTDEVraddr);
226 if (!prop_object_equals(raddr, obj))
227 continue;
228
229 obj = prop_dictionary_get(prop, BTDEVservice);
230 if (!prop_object_equals(service, obj))
231 continue;
232
233 break;
234 }
235
236 switch (cmd) {
237 case BTDEV_ATTACH: /* attach BTDEV */
238 if (btdev != NULL)
239 return EADDRINUSE;
240
241 dev = config_found(sc->sc_dev, dict, bthub_print);
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
250 btdev = device_private(dev);
251 btdev->sc_dev = dev;
252 LIST_INSERT_HEAD(&sc->sc_list, btdev, sc_next);
253 break;
254
255 case BTDEV_DETACH: /* detach BTDEV */
256 if (btdev == NULL)
257 return ENXIO;
258
259 LIST_REMOVE(btdev, sc_next);
260 config_detach(btdev->sc_dev, DETACH_FORCE);
261 break;
262 }
263
264 return 0;
265 }
266
267 static int
268 bthub_print(void *aux, const char *pnp)
269 {
270 prop_dictionary_t dict = aux;
271 prop_object_t obj;
272 const bdaddr_t *raddr;
273
274 if (pnp != NULL) {
275 obj = prop_dictionary_get(dict, BTDEVtype);
276 aprint_normal("%s: %s '%s',", pnp, BTDEVtype,
277 prop_string_cstring_nocopy(obj));
278 }
279
280 obj = prop_dictionary_get(dict, BTDEVraddr);
281 raddr = prop_data_data_nocopy(obj);
282
283 aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
284 BTDEVraddr,
285 raddr->b[5], raddr->b[4], raddr->b[3],
286 raddr->b[2], raddr->b[1], raddr->b[0]);
287
288 return UNCONF;
289 }
290