bthub.c revision 1.1 1 1.1 gdamore /* $NetBSD: bthub.c,v 1.1 2006/06/19 15:44:45 gdamore Exp $ */
2 1.1 gdamore
3 1.1 gdamore /*-
4 1.1 gdamore * Copyright (c) 2006 Itronix Inc.
5 1.1 gdamore * All rights reserved.
6 1.1 gdamore *
7 1.1 gdamore * Written by Iain Hibbert for Itronix Inc.
8 1.1 gdamore *
9 1.1 gdamore * Redistribution and use in source and binary forms, with or without
10 1.1 gdamore * modification, are permitted provided that the following conditions
11 1.1 gdamore * are met:
12 1.1 gdamore * 1. Redistributions of source code must retain the above copyright
13 1.1 gdamore * notice, this list of conditions and the following disclaimer.
14 1.1 gdamore * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 gdamore * notice, this list of conditions and the following disclaimer in the
16 1.1 gdamore * documentation and/or other materials provided with the distribution.
17 1.1 gdamore * 3. The name of Itronix Inc. may not be used to endorse
18 1.1 gdamore * or promote products derived from this software without specific
19 1.1 gdamore * prior written permission.
20 1.1 gdamore *
21 1.1 gdamore * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22 1.1 gdamore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 gdamore * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 gdamore * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25 1.1 gdamore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 1.1 gdamore * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 gdamore * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 1.1 gdamore * ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 gdamore * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 gdamore * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 gdamore * POSSIBILITY OF SUCH DAMAGE.
32 1.1 gdamore */
33 1.1 gdamore
34 1.1 gdamore #include <sys/cdefs.h>
35 1.1 gdamore __KERNEL_RCSID(0, "$NetBSD: bthub.c,v 1.1 2006/06/19 15:44:45 gdamore Exp $");
36 1.1 gdamore
37 1.1 gdamore #include <sys/param.h>
38 1.1 gdamore #include <sys/conf.h>
39 1.1 gdamore #include <sys/device.h>
40 1.1 gdamore #include <sys/fcntl.h>
41 1.1 gdamore #include <sys/kernel.h>
42 1.1 gdamore #include <sys/queue.h>
43 1.1 gdamore #include <sys/malloc.h>
44 1.1 gdamore #include <sys/mbuf.h>
45 1.1 gdamore #include <sys/proc.h>
46 1.1 gdamore #include <sys/systm.h>
47 1.1 gdamore
48 1.1 gdamore #include <netbt/bluetooth.h>
49 1.1 gdamore #include <netbt/l2cap.h>
50 1.1 gdamore
51 1.1 gdamore #include <dev/bluetooth/btdev.h>
52 1.1 gdamore
53 1.1 gdamore /*****************************************************************************
54 1.1 gdamore *
55 1.1 gdamore * Bluetooth HUB pseudo-device
56 1.1 gdamore *
57 1.1 gdamore * The Bluetooth hub is a conventent place to hang devices that are
58 1.1 gdamore * actually sitting on top of the protocol stack, and not necessarily
59 1.1 gdamore * attached to any particular bluetooth interface.
60 1.1 gdamore */
61 1.1 gdamore
62 1.1 gdamore struct bthub_softc {
63 1.1 gdamore struct device sc_dev;
64 1.1 gdamore LIST_HEAD(,btdev) sc_list;
65 1.1 gdamore };
66 1.1 gdamore
67 1.1 gdamore /* our pseudo-device hook */
68 1.1 gdamore static struct bthub_softc *hook;
69 1.1 gdamore
70 1.1 gdamore /* pseudo-device initialization */
71 1.1 gdamore void bthubattach(int);
72 1.1 gdamore
73 1.1 gdamore /* autoconf(9) glue */
74 1.1 gdamore static int bthub_match(struct device *, struct cfdata *, void *);
75 1.1 gdamore static void bthub_attach(struct device *, struct device *, void *);
76 1.1 gdamore static int bthub_detach(struct device *, int);
77 1.1 gdamore static int bthub_print(void *, const char *);
78 1.1 gdamore
79 1.1 gdamore CFATTACH_DECL(bthub, sizeof(struct bthub_softc),
80 1.1 gdamore bthub_match, bthub_attach, bthub_detach, NULL);
81 1.1 gdamore
82 1.1 gdamore /* pseudo-device control */
83 1.1 gdamore dev_type_ioctl(bthubioctl);
84 1.1 gdamore
85 1.1 gdamore const struct cdevsw bthub_cdevsw = {
86 1.1 gdamore nullopen, nullclose, noread, nowrite, bthubioctl,
87 1.1 gdamore nostop, notty, nopoll, nommap, nokqfilter,
88 1.1 gdamore };
89 1.1 gdamore
90 1.1 gdamore /*****************************************************************************
91 1.1 gdamore *
92 1.1 gdamore * bthub init routine called at system boot
93 1.1 gdamore *
94 1.1 gdamore * we take this opportunity to hang a real device on our hook
95 1.1 gdamore * so that we can operate like a proper parent to our children.
96 1.1 gdamore */
97 1.1 gdamore
98 1.1 gdamore extern struct cfdriver bthub_cd;
99 1.1 gdamore
100 1.1 gdamore static struct cfdata bthub_cfdata = {
101 1.1 gdamore .cf_name = "bthub",
102 1.1 gdamore .cf_atname = "bthub",
103 1.1 gdamore .cf_unit = DVUNIT_ANY,
104 1.1 gdamore .cf_fstate = FSTATE_STAR,
105 1.1 gdamore };
106 1.1 gdamore
107 1.1 gdamore void
108 1.1 gdamore bthubattach(int num)
109 1.1 gdamore {
110 1.1 gdamore int err;
111 1.1 gdamore
112 1.1 gdamore err = config_cfattach_attach(bthub_cd.cd_name, &bthub_ca);
113 1.1 gdamore if (err) {
114 1.1 gdamore aprint_error("%s: unable to register cfattach (%d)\n",
115 1.1 gdamore bthub_cd.cd_name, err);
116 1.1 gdamore
117 1.1 gdamore config_cfdriver_detach(&bthub_cd);
118 1.1 gdamore return;
119 1.1 gdamore }
120 1.1 gdamore
121 1.1 gdamore hook = (struct bthub_softc *)config_attach_pseudo(&bthub_cfdata);
122 1.1 gdamore if (hook == NULL) {
123 1.1 gdamore aprint_error("%s: unable to attach bthub hook(%d)\n",
124 1.1 gdamore bthub_cd.cd_name, err);
125 1.1 gdamore
126 1.1 gdamore config_cfattach_detach(bthub_cd.cd_name, &bthub_ca);
127 1.1 gdamore config_cfdriver_detach(&bthub_cd);
128 1.1 gdamore return;
129 1.1 gdamore }
130 1.1 gdamore }
131 1.1 gdamore
132 1.1 gdamore /*****************************************************************************
133 1.1 gdamore *
134 1.1 gdamore * bthub autoconf(9) routines
135 1.1 gdamore *
136 1.1 gdamore * This is the real device hanging on our hook. Only the
137 1.1 gdamore * attach function will ever be called, I guess.
138 1.1 gdamore */
139 1.1 gdamore
140 1.1 gdamore static int
141 1.1 gdamore bthub_match(struct device *self, struct cfdata *cfdata, void *arg)
142 1.1 gdamore {
143 1.1 gdamore
144 1.1 gdamore return 1;
145 1.1 gdamore }
146 1.1 gdamore
147 1.1 gdamore static void
148 1.1 gdamore bthub_attach(struct device *parent, struct device *self, void *aux)
149 1.1 gdamore {
150 1.1 gdamore struct bthub_softc *sc = (struct bthub_softc *)self;
151 1.1 gdamore
152 1.1 gdamore LIST_INIT(&sc->sc_list);
153 1.1 gdamore }
154 1.1 gdamore
155 1.1 gdamore static int
156 1.1 gdamore bthub_detach(struct device *self, int flags)
157 1.1 gdamore {
158 1.1 gdamore struct bthub_softc *sc = (struct bthub_softc *)self;
159 1.1 gdamore struct btdev *btdev;
160 1.1 gdamore
161 1.1 gdamore while ((btdev = LIST_FIRST(&sc->sc_list)) != NULL) {
162 1.1 gdamore LIST_REMOVE(btdev, sc_next);
163 1.1 gdamore config_detach(&btdev->sc_dev, flags);
164 1.1 gdamore }
165 1.1 gdamore return 0;
166 1.1 gdamore }
167 1.1 gdamore
168 1.1 gdamore /*****************************************************************************
169 1.1 gdamore *
170 1.1 gdamore * bthub control device ioctl(2)
171 1.1 gdamore *
172 1.1 gdamore * This is the method we use to control bluetooth devices,
173 1.1 gdamore * called from userland.
174 1.1 gdamore */
175 1.1 gdamore
176 1.1 gdamore int
177 1.1 gdamore bthubioctl(dev_t devno, unsigned long cmd, caddr_t data, int flag, struct lwp *l)
178 1.1 gdamore {
179 1.1 gdamore struct btdev_attach_args *bda;
180 1.1 gdamore struct btdev *btdev;
181 1.1 gdamore bdaddr_t *bdaddr;
182 1.1 gdamore
183 1.1 gdamore if (hook == NULL)
184 1.1 gdamore return ENXIO;
185 1.1 gdamore
186 1.1 gdamore switch (cmd) {
187 1.1 gdamore case BTDEV_ATTACH: /* attach BTDEV */
188 1.1 gdamore bda = (struct btdev_attach_args *)data;
189 1.1 gdamore
190 1.1 gdamore /*
191 1.1 gdamore * validate our configuration
192 1.1 gdamore */
193 1.1 gdamore if (bdaddr_any(&bda->bd_raddr)
194 1.1 gdamore || bda->bd_type == 0)
195 1.1 gdamore return EINVAL;
196 1.1 gdamore
197 1.1 gdamore LIST_FOREACH(btdev, &hook->sc_list, sc_next) {
198 1.1 gdamore if (bdaddr_same(&btdev->sc_addr, &bda->bd_raddr))
199 1.1 gdamore return EADDRINUSE;
200 1.1 gdamore }
201 1.1 gdamore
202 1.1 gdamore btdev = (struct btdev *)config_found((struct device *)hook,
203 1.1 gdamore bda, bthub_print);
204 1.1 gdamore if (btdev == NULL)
205 1.1 gdamore return ENXIO;
206 1.1 gdamore
207 1.1 gdamore bdaddr_copy(&btdev->sc_addr, &bda->bd_raddr);
208 1.1 gdamore LIST_INSERT_HEAD(&hook->sc_list, btdev, sc_next);
209 1.1 gdamore return 0;
210 1.1 gdamore
211 1.1 gdamore case BTDEV_DETACH: /* detach BTDEV */
212 1.1 gdamore bdaddr = (bdaddr_t *)data;
213 1.1 gdamore
214 1.1 gdamore LIST_FOREACH(btdev, &hook->sc_list, sc_next) {
215 1.1 gdamore if (bdaddr_same(bdaddr, &btdev->sc_addr) == 0)
216 1.1 gdamore continue;
217 1.1 gdamore
218 1.1 gdamore LIST_REMOVE(btdev, sc_next);
219 1.1 gdamore config_detach((struct device *)btdev, DETACH_FORCE);
220 1.1 gdamore return 0;
221 1.1 gdamore }
222 1.1 gdamore return ENXIO;
223 1.1 gdamore
224 1.1 gdamore default:
225 1.1 gdamore break;
226 1.1 gdamore }
227 1.1 gdamore
228 1.1 gdamore return EPASSTHROUGH;
229 1.1 gdamore }
230 1.1 gdamore
231 1.1 gdamore static int
232 1.1 gdamore bthub_print(void *aux, const char *pnp)
233 1.1 gdamore {
234 1.1 gdamore struct btdev_attach_args *bda = aux;
235 1.1 gdamore
236 1.1 gdamore if (pnp != NULL)
237 1.1 gdamore aprint_normal("%s: ", pnp);
238 1.1 gdamore
239 1.1 gdamore aprint_verbose(" bdaddr %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
240 1.1 gdamore bda->bd_raddr.b[5], bda->bd_raddr.b[4],
241 1.1 gdamore bda->bd_raddr.b[3], bda->bd_raddr.b[2],
242 1.1 gdamore bda->bd_raddr.b[1], bda->bd_raddr.b[0]);
243 1.1 gdamore
244 1.1 gdamore return UNCONF;
245 1.1 gdamore }
246