bthub.c revision 1.3 1 /* $NetBSD: bthub.c,v 1.3 2006/09/10 15:45:56 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.3 2006/09/10 15:45:56 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,
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, flag, &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_object_t obj;
181 int unit;
182
183 /* validate device type */
184 obj = prop_dictionary_get(dict, BTDEVtype);
185 if (prop_object_type(obj) != PROP_TYPE_STRING)
186 return EINVAL;
187
188 /* validate local address */
189 obj = prop_dictionary_get(dict, BTDEVladdr);
190 if (prop_data_size(obj) != sizeof(bdaddr_t))
191 return EINVAL;
192
193 /* locate the relevant bthub */
194 for (unit = 0 ; ; unit++) {
195 if (unit == bthub_cd.cd_ndevs)
196 return ENXIO;
197
198 sc = (struct bthub_softc *)bthub_cd.cd_devs[unit];
199 if (sc == NULL)
200 continue;
201
202 if (prop_data_equals(obj, prop_dictionary_get(device_properties(&sc->sc_dev), BTDEVladdr)))
203 break;
204 }
205
206 /* validate remote address */
207 obj = prop_dictionary_get(dict, BTDEVraddr);
208 if (prop_data_size(obj) != sizeof(bdaddr_t)
209 || bdaddr_any(prop_data_data_nocopy(obj)))
210 return EINVAL;
211
212 /* check for existing child device */
213 LIST_FOREACH(dev, &sc->sc_list, sc_next) {
214 if ((*dev->sc_identify)(dev, dict))
215 break;
216 }
217
218 switch (cmd) {
219 case BTDEV_ATTACH: /* attach BTDEV */
220 if (dev != NULL)
221 return EADDRINUSE;
222
223 dev = (struct btdev *)config_found((struct device *)sc,
224 dict, bthub_print);
225 if (dev == NULL)
226 return ENXIO;
227
228 KASSERT(dev->sc_identify != NULL);
229 LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
230 break;
231
232 case BTDEV_DETACH: /* detach BTDEV */
233 if (dev == NULL)
234 return ENXIO;
235
236 LIST_REMOVE(dev, sc_next);
237 config_detach((struct device *)dev, DETACH_FORCE);
238 break;
239 }
240
241 return 0;
242 }
243
244 static int
245 bthub_print(void *aux, const char *pnp)
246 {
247 prop_dictionary_t dict = aux;
248 prop_object_t obj;
249 const bdaddr_t *raddr;
250
251 if (pnp != NULL) {
252 obj = prop_dictionary_get(dict, BTDEVtype);
253 aprint_normal("%s: %s '%s',", pnp, BTDEVtype,
254 prop_string_cstring_nocopy(obj));
255 }
256
257 obj = prop_dictionary_get(dict, BTDEVraddr);
258 raddr = prop_data_data_nocopy(obj);
259
260 aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
261 BTDEVraddr,
262 raddr->b[5], raddr->b[4], raddr->b[3],
263 raddr->b[2], raddr->b[1], raddr->b[0]);
264
265 return UNCONF;
266 }
267