xref: /netbsd-src/sys/dev/bluetooth/bthub.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: bthub.c,v 1.7 2006/10/12 01:30:55 christos 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.7 2006/10/12 01:30:55 christos 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 __unused, struct cfdata *cfdata __unused,
95     void *arg __unused)
96 {
97 
98 	return 1;
99 }
100 
101 static void
102 bthub_attach(struct device *parent __unused, struct device *self, void *aux)
103 {
104 	struct bthub_softc *sc = (struct bthub_softc *)self;
105 	bdaddr_t *addr = aux;
106 	prop_dictionary_t dict;
107 	prop_object_t obj;
108 
109 	LIST_INIT(&sc->sc_list);
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(struct device *self, int flags)
126 {
127 	struct bthub_softc *sc = (struct bthub_softc *)self;
128 	struct btdev *dev;
129 	int err;
130 
131 	while (!LIST_EMPTY(&sc->sc_list)) {
132 		dev = LIST_FIRST(&sc->sc_list);
133 		LIST_REMOVE(dev, sc_next);
134 
135 		err = config_detach((struct device *)dev, flags);
136 		if (err && (flags & DETACH_FORCE) == 0) {
137 			LIST_INSERT_HEAD(&sc->sc_list, dev, 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, caddr_t 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((const struct plistref *)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 __unused, unsigned long cmd, prop_dictionary_t dict,
178     int flag __unused, struct lwp *l __unused)
179 {
180 	struct bthub_softc *sc;
181 	struct btdev *dev;
182 	prop_data_t laddr, raddr;
183 	prop_string_t service;
184 	prop_dictionary_t prop;
185 	prop_object_t obj;
186 	int unit;
187 
188 	/* validate local address */
189 	laddr = prop_dictionary_get(dict, BTDEVladdr);
190 	if (prop_data_size(laddr) != 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 		prop = device_properties(&sc->sc_dev);
203 		obj = prop_dictionary_get(prop, BTDEVladdr);
204 		if (prop_data_equals(laddr, obj))
205 			break;
206 	}
207 
208 	/* validate remote address */
209 	raddr = prop_dictionary_get(dict, BTDEVraddr);
210 	if (prop_data_size(raddr) != sizeof(bdaddr_t)
211 	    || bdaddr_any(prop_data_data_nocopy(raddr)))
212 		return EINVAL;
213 
214 	/* validate service name */
215 	service = prop_dictionary_get(dict, BTDEVservice);
216 	if (prop_object_type(service) != PROP_TYPE_STRING)
217 		return EINVAL;
218 
219 	/* locate matching child device, if any */
220 	LIST_FOREACH(dev, &sc->sc_list, sc_next) {
221 		prop = device_properties(&dev->sc_dev);
222 
223 		obj = prop_dictionary_get(prop, BTDEVraddr);
224 		if (!prop_object_equals(raddr, obj))
225 			continue;
226 
227 		obj = prop_dictionary_get(prop, BTDEVservice);
228 		if (!prop_object_equals(service, obj))
229 			continue;
230 
231 		break;
232 	}
233 
234 	switch (cmd) {
235 	case BTDEV_ATTACH:	/* attach BTDEV */
236 		if (dev != NULL)
237 			return EADDRINUSE;
238 
239 		dev = (struct btdev *)config_found((struct device *)sc,
240 						dict, bthub_print);
241 		if (dev == NULL)
242 			return ENXIO;
243 
244 		prop = device_properties(&dev->sc_dev);
245 		prop_dictionary_set(prop, BTDEVladdr, laddr);
246 		prop_dictionary_set(prop, BTDEVraddr, raddr);
247 		prop_dictionary_set(prop, BTDEVservice, service);
248 
249 		LIST_INSERT_HEAD(&sc->sc_list, dev, sc_next);
250 		break;
251 
252 	case BTDEV_DETACH:	/* detach BTDEV */
253 		if (dev == NULL)
254 			return ENXIO;
255 
256 		LIST_REMOVE(dev, sc_next);
257 		config_detach((struct device *)dev, DETACH_FORCE);
258 		break;
259 	}
260 
261 	return 0;
262 }
263 
264 static int
265 bthub_print(void *aux, const char *pnp)
266 {
267 	prop_dictionary_t dict = aux;
268 	prop_object_t obj;
269 	const bdaddr_t *raddr;
270 
271 	if (pnp != NULL) {
272 		obj = prop_dictionary_get(dict, BTDEVtype);
273 		aprint_normal("%s: %s '%s',", pnp, BTDEVtype,
274 					prop_string_cstring_nocopy(obj));
275 	}
276 
277 	obj = prop_dictionary_get(dict, BTDEVraddr);
278 	raddr = prop_data_data_nocopy(obj);
279 
280 	aprint_verbose(" %s %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
281 			BTDEVraddr,
282 			raddr->b[5], raddr->b[4], raddr->b[3],
283 			raddr->b[2], raddr->b[1], raddr->b[0]);
284 
285 	return UNCONF;
286 }
287