ubt.c revision 1.2 1 1.2 augustss /* $NetBSD: ubt.c,v 1.2 2002/08/22 10:17:46 augustss Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss * 3. All advertising materials mentioning features or use of this software
19 1.1 augustss * must display the following acknowledgement:
20 1.1 augustss * This product includes software developed by the NetBSD
21 1.1 augustss * Foundation, Inc. and its contributors.
22 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 augustss * contributors may be used to endorse or promote products derived
24 1.1 augustss * from this software without specific prior written permission.
25 1.1 augustss *
26 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
37 1.1 augustss */
38 1.1 augustss
39 1.1 augustss #include <sys/cdefs.h>
40 1.2 augustss __KERNEL_RCSID(0, "$NetBSD: ubt.c,v 1.2 2002/08/22 10:17:46 augustss Exp $");
41 1.1 augustss
42 1.1 augustss #include <sys/param.h>
43 1.1 augustss #include <sys/systm.h>
44 1.1 augustss #include <sys/kernel.h>
45 1.1 augustss #include <sys/device.h>
46 1.1 augustss #include <sys/lock.h>
47 1.1 augustss #include <sys/ioctl.h>
48 1.1 augustss #include <sys/conf.h>
49 1.1 augustss #include <sys/file.h>
50 1.1 augustss #include <sys/poll.h>
51 1.1 augustss #include <sys/select.h>
52 1.1 augustss #include <sys/proc.h>
53 1.1 augustss
54 1.1 augustss #include <dev/usb/usb.h>
55 1.1 augustss #include <dev/usb/usbdi.h>
56 1.1 augustss #include <dev/usb/usbdi_util.h>
57 1.1 augustss #include <dev/usb/usbdevs.h>
58 1.1 augustss
59 1.1 augustss #ifdef UBT_DEBUG
60 1.1 augustss #define DPRINTF(x) if (ubtdebug) logprintf x
61 1.1 augustss #define DPRINTFN(n,x) if (ubtdebug>(n)) logprintf x
62 1.1 augustss int ubtdebug = 0;
63 1.1 augustss #else
64 1.1 augustss #define DPRINTF(x)
65 1.1 augustss #define DPRINTFN(n,x)
66 1.1 augustss #endif
67 1.1 augustss
68 1.1 augustss /*
69 1.1 augustss * Protocol related definitions
70 1.1 augustss */
71 1.1 augustss
72 1.1 augustss struct ubt_softc {
73 1.1 augustss USBBASEDEVICE sc_dev;
74 1.1 augustss usbd_device_handle sc_udev;
75 1.1 augustss usbd_interface_handle sc_ctliface;
76 1.1 augustss
77 1.1 augustss int sc_rd_addr;
78 1.1 augustss usbd_pipe_handle sc_rd_pipe;
79 1.1 augustss
80 1.1 augustss int sc_wr_addr;
81 1.1 augustss usbd_pipe_handle sc_wr_pipe;
82 1.1 augustss
83 1.1 augustss struct device *sc_child;
84 1.1 augustss int sc_refcnt;
85 1.1 augustss char sc_dying;
86 1.1 augustss };
87 1.1 augustss
88 1.1 augustss USB_DECLARE_DRIVER(ubt);
89 1.1 augustss
90 1.1 augustss USB_MATCH(ubt)
91 1.1 augustss {
92 1.1 augustss USB_MATCH_START(ubt, uaa);
93 1.1 augustss usb_interface_descriptor_t *id;
94 1.1 augustss
95 1.1 augustss DPRINTFN(50,("ubt_match\n"));
96 1.1 augustss
97 1.1 augustss if (uaa->iface == NULL)
98 1.1 augustss return (UMATCH_NONE);
99 1.1 augustss
100 1.1 augustss id = usbd_get_interface_descriptor(uaa->iface);
101 1.1 augustss if (id != NULL &&
102 1.1 augustss id->bInterfaceClass == UICLASS_WIRELESS &&
103 1.1 augustss id->bInterfaceSubClass == UISUBCLASS_RF &&
104 1.1 augustss id->bInterfaceProtocol == UIPROTO_BLUETOOTH)
105 1.1 augustss return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
106 1.1 augustss return (UMATCH_NONE);
107 1.1 augustss }
108 1.1 augustss
109 1.1 augustss USB_ATTACH(ubt)
110 1.1 augustss {
111 1.1 augustss USB_ATTACH_START(ubt, sc, uaa);
112 1.1 augustss usbd_device_handle dev = uaa->device;
113 1.1 augustss usbd_interface_handle iface = uaa->iface;
114 1.1 augustss char devinfo[1024];
115 1.1 augustss usb_endpoint_descriptor_t *ed;
116 1.1 augustss u_int8_t epcount;
117 1.1 augustss int i;
118 1.1 augustss
119 1.1 augustss DPRINTFN(10,("ubt_attach: sc=%p\n", sc));
120 1.1 augustss
121 1.1 augustss usbd_devinfo(dev, 0, devinfo);
122 1.1 augustss USB_ATTACH_SETUP;
123 1.1 augustss printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
124 1.1 augustss
125 1.1 augustss sc->sc_udev = dev;
126 1.1 augustss sc->sc_ctliface = iface;
127 1.1 augustss
128 1.1 augustss epcount = 0;
129 1.1 augustss (void)usbd_endpoint_count(iface, &epcount);
130 1.1 augustss
131 1.1 augustss sc->sc_rd_addr = -1;
132 1.1 augustss sc->sc_wr_addr = -1;
133 1.1 augustss for (i = 0; i < epcount; i++) {
134 1.1 augustss ed = usbd_interface2endpoint_descriptor(iface, i);
135 1.1 augustss if (ed == NULL) {
136 1.1 augustss printf("%s: couldn't get ep %d\n",
137 1.1 augustss USBDEVNAME(sc->sc_dev), i);
138 1.1 augustss USB_ATTACH_ERROR_RETURN;
139 1.1 augustss }
140 1.1 augustss if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
141 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
142 1.1 augustss sc->sc_rd_addr = ed->bEndpointAddress;
143 1.1 augustss } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
144 1.1 augustss UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
145 1.1 augustss sc->sc_wr_addr = ed->bEndpointAddress;
146 1.1 augustss }
147 1.1 augustss }
148 1.1 augustss #if 0
149 1.1 augustss if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
150 1.1 augustss printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
151 1.1 augustss USB_ATTACH_ERROR_RETURN;
152 1.1 augustss }
153 1.1 augustss #endif
154 1.1 augustss
155 1.1 augustss usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
156 1.1 augustss USBDEV(sc->sc_dev));
157 1.1 augustss
158 1.1 augustss #if 0
159 1.1 augustss sc->sc_child = config_found(self, &ia, ir_print);
160 1.2 augustss #else
161 1.2 augustss printf("%s: driver not implemented\n", USBDEVNAME(sc->sc_dev));
162 1.1 augustss #endif
163 1.1 augustss
164 1.1 augustss USB_ATTACH_SUCCESS_RETURN;
165 1.1 augustss }
166 1.1 augustss
167 1.1 augustss USB_DETACH(ubt)
168 1.1 augustss {
169 1.1 augustss USB_DETACH_START(ubt, sc);
170 1.1 augustss int s;
171 1.1 augustss int rv = 0;
172 1.1 augustss
173 1.1 augustss DPRINTF(("ubt_detach: sc=%p flags=%d\n", sc, flags));
174 1.1 augustss
175 1.1 augustss sc->sc_dying = 1;
176 1.1 augustss /* Abort all pipes. Causes processes waiting for transfer to wake. */
177 1.1 augustss if (sc->sc_rd_pipe != NULL) {
178 1.1 augustss usbd_abort_pipe(sc->sc_rd_pipe);
179 1.1 augustss usbd_close_pipe(sc->sc_rd_pipe);
180 1.1 augustss sc->sc_rd_pipe = NULL;
181 1.1 augustss }
182 1.1 augustss if (sc->sc_wr_pipe != NULL) {
183 1.1 augustss usbd_abort_pipe(sc->sc_wr_pipe);
184 1.1 augustss usbd_close_pipe(sc->sc_wr_pipe);
185 1.1 augustss sc->sc_wr_pipe = NULL;
186 1.1 augustss }
187 1.1 augustss #if 0
188 1.1 augustss wakeup(&sc->sc_rd_count);
189 1.1 augustss #endif
190 1.1 augustss
191 1.1 augustss s = splusb();
192 1.1 augustss if (--sc->sc_refcnt >= 0) {
193 1.1 augustss /* Wait for processes to go away. */
194 1.1 augustss usb_detach_wait(USBDEV(sc->sc_dev));
195 1.1 augustss }
196 1.1 augustss splx(s);
197 1.1 augustss
198 1.1 augustss if (sc->sc_child != NULL) {
199 1.1 augustss rv = config_detach(sc->sc_child, flags);
200 1.1 augustss sc->sc_child = NULL;
201 1.1 augustss }
202 1.1 augustss
203 1.1 augustss usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
204 1.1 augustss USBDEV(sc->sc_dev));
205 1.1 augustss
206 1.1 augustss return (rv);
207 1.1 augustss }
208 1.1 augustss
209 1.1 augustss int
210 1.1 augustss ubt_activate(device_ptr_t self, enum devact act)
211 1.1 augustss {
212 1.1 augustss struct ubt_softc *sc = (struct ubt_softc *)self;
213 1.1 augustss int error = 0;
214 1.1 augustss
215 1.1 augustss switch (act) {
216 1.1 augustss case DVACT_ACTIVATE:
217 1.1 augustss return (EOPNOTSUPP);
218 1.1 augustss break;
219 1.1 augustss
220 1.1 augustss case DVACT_DEACTIVATE:
221 1.1 augustss sc->sc_dying = 1;
222 1.1 augustss if (sc->sc_child != NULL)
223 1.1 augustss error = config_deactivate(sc->sc_child);
224 1.1 augustss break;
225 1.1 augustss }
226 1.1 augustss return (error);
227 1.1 augustss }
228