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