bthub.c revision 1.6 1 /* $NetBSD: bthub.c,v 1.6 2006/10/04 19:23:59 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.6 2006/10/04 19:23:59 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 struct device sc_dev;
63 LIST_HEAD(,btdev) sc_list;
64 };
65
66 /* autoconf(9) glue */
67 static int bthub_match(struct device *, struct cfdata *, void *);
68 static void bthub_attach(struct device *, struct device *, void *);
69 static int bthub_detach(struct device *, int);
70
71 CFATTACH_DECL(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(struct device *self, struct cfdata *cfdata, void *arg)
95 {
96
97 return 1;
98 }
99
100 static void
101 bthub_attach(struct device *parent, struct device *self, void *aux)
102 {
103 struct bthub_softc *sc = (struct bthub_softc *)self;
104 bdaddr_t *addr = aux;
105 prop_dictionary_t dict;
106 prop_object_t obj;
107
108 LIST_INIT(&sc->sc_list);
109
110 dict = device_properties(self);
111 obj = prop_data_create_data(addr, sizeof(*addr));
112 prop_dictionary_set(dict, BTDEVladdr, obj);
113 prop_object_release(obj);
114
115 aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
116 BTDEVladdr,
117 addr->b[5], addr->b[4], addr->b[3],
118 addr->b[2], addr->b[1], addr->b[0]);
119
120 aprint_normal("\n");
121 }
122
123 static int
124 bthub_detach(struct device *self, int flags)
125 {
126 struct bthub_softc *sc = (struct bthub_softc *)self;
127 struct btdev *dev;
128 int err;
129
130 while (!LIST_EMPTY(&sc->sc_list)) {
131 dev = LIST_FIRST(&sc->sc_list);
132 LIST_REMOVE(dev, sc_next);
133
134 err = config_detach((struct device *)dev, flags);
135 if (err && (flags & DETACH_FORCE) == 0) {
136 LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
137 return err;
138 }
139 }
140
141 return 0;
142 }
143
144
145 /*****************************************************************************
146 *
147 * bthub access functions to control device
148 */
149
150 int
151 bthubioctl(dev_t devno, unsigned long cmd, caddr_t data, int flag, struct lwp *l)
152 {
153 prop_dictionary_t dict;
154 int err;
155
156 switch(cmd) {
157 case BTDEV_ATTACH:
158 case BTDEV_DETACH:
159 /* load dictionary */
160 err = prop_dictionary_copyin_ioctl((const struct plistref *)data, cmd, &dict);
161 if (err == 0) {
162 err = bthub_pioctl(devno, cmd, dict, flag, l);
163 prop_object_release(dict);
164 }
165 break;
166
167 default:
168 err = EPASSTHROUGH;
169 break;
170 }
171
172 return err;
173 }
174
175 static int
176 bthub_pioctl(dev_t devno, unsigned long cmd, prop_dictionary_t dict, int flag, struct lwp *l)
177 {
178 struct bthub_softc *sc;
179 struct btdev *dev;
180 prop_data_t laddr, raddr;
181 prop_string_t service;
182 prop_dictionary_t prop;
183 prop_object_t obj;
184 int unit;
185
186 /* validate local address */
187 laddr = prop_dictionary_get(dict, BTDEVladdr);
188 if (prop_data_size(laddr) != sizeof(bdaddr_t))
189 return EINVAL;
190
191 /* locate the relevant bthub */
192 for (unit = 0 ; ; unit++) {
193 if (unit == bthub_cd.cd_ndevs)
194 return ENXIO;
195
196 sc = (struct bthub_softc *)bthub_cd.cd_devs[unit];
197 if (sc == NULL)
198 continue;
199
200 prop = device_properties(&sc->sc_dev);
201 obj = prop_dictionary_get(prop, BTDEVladdr);
202 if (prop_data_equals(laddr, obj))
203 break;
204 }
205
206 /* validate remote address */
207 raddr = prop_dictionary_get(dict, BTDEVraddr);
208 if (prop_data_size(raddr) != sizeof(bdaddr_t)
209 || bdaddr_any(prop_data_data_nocopy(raddr)))
210 return EINVAL;
211
212 /* validate service name */
213 service = prop_dictionary_get(dict, BTDEVservice);
214 if (prop_object_type(service) != PROP_TYPE_STRING)
215 return EINVAL;
216
217 /* locate matching child device, if any */
218 LIST_FOREACH(dev, &sc->sc_list, sc_next) {
219 prop = device_properties(&dev->sc_dev);
220
221 obj = prop_dictionary_get(prop, BTDEVraddr);
222 if (!prop_object_equals(raddr, obj))
223 continue;
224
225 obj = prop_dictionary_get(prop, BTDEVservice);
226 if (!prop_object_equals(service, obj))
227 continue;
228
229 break;
230 }
231
232 switch (cmd) {
233 case BTDEV_ATTACH: /* attach BTDEV */
234 if (dev != NULL)
235 return EADDRINUSE;
236
237 dev = (struct btdev *)config_found((struct device *)sc,
238 dict, bthub_print);
239 if (dev == NULL)
240 return ENXIO;
241
242 prop = device_properties(&dev->sc_dev);
243 prop_dictionary_set(prop, BTDEVladdr, laddr);
244 prop_dictionary_set(prop, BTDEVraddr, raddr);
245 prop_dictionary_set(prop, BTDEVservice, service);
246
247 LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
248 break;
249
250 case BTDEV_DETACH: /* detach BTDEV */
251 if (dev == NULL)
252 return ENXIO;
253
254 LIST_REMOVE(dev, sc_next);
255 config_detach((struct device *)dev, DETACH_FORCE);
256 break;
257 }
258
259 return 0;
260 }
261
262 static int
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_cstring_nocopy(obj));
273 }
274
275 obj = prop_dictionary_get(dict, BTDEVraddr);
276 raddr = prop_data_data_nocopy(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