ums.c revision 1.93.2.1 1 1.93.2.1 martin /* $NetBSD: ums.c,v 1.93.2.1 2020/01/21 10:39:59 martin Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.91 bouyer * Copyright (c) 1998, 2017 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.11 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.43 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.11 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss *
20 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
31 1.20 augustss */
32 1.20 augustss
33 1.20 augustss /*
34 1.60 augustss * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35 1.1 augustss */
36 1.52 lukem
37 1.52 lukem #include <sys/cdefs.h>
38 1.93.2.1 martin __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.93.2.1 2020/01/21 10:39:59 martin Exp $");
39 1.90 jakllsch
40 1.90 jakllsch #ifdef _KERNEL_OPT
41 1.90 jakllsch #include "opt_usb.h"
42 1.90 jakllsch #endif
43 1.1 augustss
44 1.1 augustss #include <sys/param.h>
45 1.1 augustss #include <sys/systm.h>
46 1.1 augustss #include <sys/kernel.h>
47 1.1 augustss #include <sys/device.h>
48 1.1 augustss #include <sys/ioctl.h>
49 1.1 augustss #include <sys/file.h>
50 1.1 augustss #include <sys/select.h>
51 1.1 augustss #include <sys/proc.h>
52 1.1 augustss #include <sys/vnode.h>
53 1.1 augustss #include <sys/poll.h>
54 1.1 augustss
55 1.1 augustss #include <dev/usb/usb.h>
56 1.1 augustss #include <dev/usb/usbhid.h>
57 1.1 augustss
58 1.1 augustss #include <dev/usb/usbdi.h>
59 1.1 augustss #include <dev/usb/usbdi_util.h>
60 1.1 augustss #include <dev/usb/usbdevs.h>
61 1.1 augustss #include <dev/usb/usb_quirks.h>
62 1.53 augustss #include <dev/usb/uhidev.h>
63 1.91 bouyer #include <dev/hid/hid.h>
64 1.91 bouyer #include <dev/hid/hidms.h>
65 1.3 augustss
66 1.86 christos #ifdef UMS_DEBUG
67 1.81 dyoung #define DPRINTF(x) if (umsdebug) printf x
68 1.81 dyoung #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
69 1.1 augustss int umsdebug = 0;
70 1.1 augustss #else
71 1.1 augustss #define DPRINTF(x)
72 1.1 augustss #define DPRINTFN(n,x)
73 1.1 augustss #endif
74 1.1 augustss
75 1.27 augustss #define UMSUNIT(s) (minor(s))
76 1.16 augustss
77 1.1 augustss struct ums_softc {
78 1.53 augustss struct uhidev sc_hdev;
79 1.91 bouyer struct hidms sc_ms;
80 1.53 augustss
81 1.93.2.1 martin bool sc_alwayson;
82 1.93.2.1 martin
83 1.93.2.1 martin bool sc_alwayson;
84 1.93.2.1 martin
85 1.93.2.1 martin bool sc_alwayson;
86 1.93.2.1 martin
87 1.91 bouyer int sc_enabled;
88 1.91 bouyer char sc_dying;
89 1.84 christos };
90 1.84 christos
91 1.89 skrll Static void ums_intr(struct uhidev *, void *, u_int);
92 1.1 augustss
93 1.44 augustss Static int ums_enable(void *);
94 1.44 augustss Static void ums_disable(void *);
95 1.89 skrll Static int ums_ioctl(void *, u_long, void *, int, struct lwp *);
96 1.3 augustss
97 1.3 augustss const struct wsmouse_accessops ums_accessops = {
98 1.3 augustss ums_enable,
99 1.3 augustss ums_ioctl,
100 1.3 augustss ums_disable,
101 1.3 augustss };
102 1.3 augustss
103 1.73 cube int ums_match(device_t, cfdata_t, void *);
104 1.71 dyoung void ums_attach(device_t, device_t, void *);
105 1.71 dyoung void ums_childdet(device_t, device_t);
106 1.71 dyoung int ums_detach(device_t, int);
107 1.71 dyoung int ums_activate(device_t, enum devact);
108 1.93 mrg
109 1.73 cube CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach,
110 1.71 dyoung ums_detach, ums_activate, NULL, ums_childdet);
111 1.1 augustss
112 1.53 augustss int
113 1.73 cube ums_match(device_t parent, cfdata_t match, void *aux)
114 1.1 augustss {
115 1.53 augustss struct uhidev_attach_arg *uha = aux;
116 1.93.2.1 martin int size, error;
117 1.1 augustss void *desc;
118 1.57 augustss
119 1.79 jakllsch /*
120 1.79 jakllsch * Some (older) Griffin PowerMate knobs may masquerade as a
121 1.79 jakllsch * mouse, avoid treating them as such, they have only one axis.
122 1.79 jakllsch */
123 1.89 skrll if (uha->uiaa->uiaa_vendor == USB_VENDOR_GRIFFIN &&
124 1.89 skrll uha->uiaa->uiaa_product == USB_PRODUCT_GRIFFIN_POWERMATE)
125 1.89 skrll return UMATCH_NONE;
126 1.79 jakllsch
127 1.53 augustss uhidev_get_report_desc(uha->parent, &desc, &size);
128 1.53 augustss if (!hid_is_collection(desc, size, uha->reportid,
129 1.78 jakllsch HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)) &&
130 1.78 jakllsch !hid_is_collection(desc, size, uha->reportid,
131 1.84 christos HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER)) &&
132 1.84 christos !hid_is_collection(desc, size, uha->reportid,
133 1.89 skrll HID_USAGE2(HUP_DIGITIZERS, 0x0002)))
134 1.89 skrll return UMATCH_NONE;
135 1.1 augustss
136 1.89 skrll return UMATCH_IFACECLASS;
137 1.1 augustss }
138 1.1 augustss
139 1.53 augustss void
140 1.73 cube ums_attach(device_t parent, device_t self, void *aux)
141 1.1 augustss {
142 1.73 cube struct ums_softc *sc = device_private(self);
143 1.53 augustss struct uhidev_attach_arg *uha = aux;
144 1.93.2.1 martin int size, error;
145 1.1 augustss void *desc;
146 1.91 bouyer uint32_t quirks;
147 1.1 augustss
148 1.69 jmcneill aprint_naive("\n");
149 1.69 jmcneill
150 1.73 cube sc->sc_hdev.sc_dev = self;
151 1.53 augustss sc->sc_hdev.sc_intr = ums_intr;
152 1.53 augustss sc->sc_hdev.sc_parent = uha->parent;
153 1.53 augustss sc->sc_hdev.sc_report_id = uha->reportid;
154 1.1 augustss
155 1.53 augustss quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
156 1.35 augustss if (quirks & UQ_MS_REVZ)
157 1.91 bouyer sc->sc_ms.flags |= HIDMS_REVZ;
158 1.35 augustss if (quirks & UQ_SPUR_BUT_UP)
159 1.91 bouyer sc->sc_ms.flags |= HIDMS_SPUR_BUT_UP;
160 1.84 christos
161 1.70 jmcneill if (!pmf_device_register(self, NULL, NULL))
162 1.70 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
163 1.70 jmcneill
164 1.91 bouyer uhidev_get_report_desc(uha->parent, &desc, &size);
165 1.10 augustss
166 1.91 bouyer if (!hidms_setup(self, &sc->sc_ms, uha->reportid, desc, size))
167 1.81 dyoung return;
168 1.6 augustss
169 1.89 skrll if (uha->uiaa->uiaa_vendor == USB_VENDOR_MICROSOFT) {
170 1.88 christos int fixpos;
171 1.88 christos /*
172 1.88 christos * The Microsoft Wireless Laser Mouse 6000 v2.0 and the
173 1.88 christos * Microsoft Comfort Mouse 2.0 report a bad position for
174 1.88 christos * the wheel and wheel tilt controls -- should be in bytes
175 1.88 christos * 3 & 4 of the report. Fix this if necessary.
176 1.88 christos */
177 1.89 skrll switch (uha->uiaa->uiaa_product) {
178 1.88 christos case USB_PRODUCT_MICROSOFT_24GHZ_XCVR10:
179 1.88 christos case USB_PRODUCT_MICROSOFT_24GHZ_XCVR20:
180 1.92 maya case USB_PRODUCT_MICROSOFT_NATURAL_6000:
181 1.88 christos fixpos = 24;
182 1.88 christos break;
183 1.88 christos case USB_PRODUCT_MICROSOFT_CM6000:
184 1.88 christos fixpos = 40;
185 1.88 christos break;
186 1.88 christos default:
187 1.88 christos fixpos = 0;
188 1.88 christos break;
189 1.88 christos }
190 1.88 christos if (fixpos) {
191 1.91 bouyer if ((sc->sc_ms.flags & HIDMS_Z) &&
192 1.91 bouyer sc->sc_ms.hidms_loc_z.pos == 0)
193 1.91 bouyer sc->sc_ms.hidms_loc_z.pos = fixpos;
194 1.91 bouyer if ((sc->sc_ms.flags & HIDMS_W) &&
195 1.91 bouyer sc->sc_ms.hidms_loc_w.pos == 0)
196 1.91 bouyer sc->sc_ms.hidms_loc_w.pos =
197 1.91 bouyer sc->sc_ms.hidms_loc_z.pos + 8;
198 1.88 christos }
199 1.75 rafal }
200 1.63 augustss
201 1.93.2.1 martin if (uha->uiaa->uiaa_vendor == USB_VENDOR_HAILUCK &&
202 1.93.2.1 martin uha->uiaa->uiaa_product == USB_PRODUCT_HAILUCK_KEYBOARD) {
203 1.93.2.1 martin /*
204 1.93.2.1 martin * The HAILUCK USB Keyboard has a built-in touchpad, which
205 1.93.2.1 martin * needs to be active for the keyboard to function properly.
206 1.93.2.1 martin */
207 1.93.2.1 martin sc->sc_alwayson = true;
208 1.93.2.1 martin }
209 1.93.2.1 martin
210 1.91 bouyer hidms_attach(self, &sc->sc_ms, &ums_accessops);
211 1.93.2.1 martin
212 1.93.2.1 martin if (sc->sc_alwayson) {
213 1.93.2.1 martin error = uhidev_open(&sc->sc_hdev);
214 1.93.2.1 martin if (error != 0) {
215 1.93.2.1 martin aprint_error_dev(self,
216 1.93.2.1 martin "WARNING: couldn't open always-on device\n");
217 1.93.2.1 martin sc->sc_alwayson = false;
218 1.93.2.1 martin }
219 1.93.2.1 martin }
220 1.16 augustss }
221 1.16 augustss
222 1.25 augustss int
223 1.81 dyoung ums_activate(device_t self, enum devact act)
224 1.16 augustss {
225 1.73 cube struct ums_softc *sc = device_private(self);
226 1.28 augustss
227 1.28 augustss switch (act) {
228 1.28 augustss case DVACT_DEACTIVATE:
229 1.28 augustss sc->sc_dying = 1;
230 1.76 dyoung return 0;
231 1.76 dyoung default:
232 1.76 dyoung return EOPNOTSUPP;
233 1.28 augustss }
234 1.1 augustss }
235 1.1 augustss
236 1.71 dyoung void
237 1.71 dyoung ums_childdet(device_t self, device_t child)
238 1.71 dyoung {
239 1.71 dyoung struct ums_softc *sc = device_private(self);
240 1.71 dyoung
241 1.91 bouyer KASSERT(sc->sc_ms.hidms_wsmousedev == child);
242 1.91 bouyer sc->sc_ms.hidms_wsmousedev = NULL;
243 1.71 dyoung }
244 1.71 dyoung
245 1.53 augustss int
246 1.71 dyoung ums_detach(device_t self, int flags)
247 1.1 augustss {
248 1.71 dyoung struct ums_softc *sc = device_private(self);
249 1.25 augustss int rv = 0;
250 1.8 augustss
251 1.25 augustss DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
252 1.33 augustss
253 1.93.2.1 martin if (sc->sc_alwayson)
254 1.93.2.1 martin uhidev_close(&sc->sc_hdev);
255 1.93.2.1 martin
256 1.93.2.1 martin if (sc->sc_alwayson)
257 1.93.2.1 martin uhidev_close(&sc->sc_hdev);
258 1.93.2.1 martin
259 1.93.2.1 martin if (sc->sc_alwayson)
260 1.93.2.1 martin uhidev_close(&sc->sc_hdev);
261 1.93.2.1 martin
262 1.51 augustss /* No need to do reference counting of ums, wsmouse has all the goo. */
263 1.91 bouyer if (sc->sc_ms.hidms_wsmousedev != NULL)
264 1.91 bouyer rv = config_detach(sc->sc_ms.hidms_wsmousedev, flags);
265 1.40 augustss
266 1.70 jmcneill pmf_device_deregister(self);
267 1.70 jmcneill
268 1.89 skrll return rv;
269 1.1 augustss }
270 1.1 augustss
271 1.1 augustss void
272 1.67 christos ums_intr(struct uhidev *addr, void *ibuf, u_int len)
273 1.1 augustss {
274 1.53 augustss struct ums_softc *sc = (struct ums_softc *)addr;
275 1.93.2.1 martin
276 1.93.2.1 martin if (sc->sc_enabled)
277 1.93.2.1 martin hidms_intr(&sc->sc_ms, ibuf, len);
278 1.1 augustss }
279 1.3 augustss
280 1.42 augustss Static int
281 1.44 augustss ums_enable(void *v)
282 1.3 augustss {
283 1.3 augustss struct ums_softc *sc = v;
284 1.93.2.1 martin int error = 0;
285 1.16 augustss
286 1.25 augustss DPRINTFN(1,("ums_enable: sc=%p\n", sc));
287 1.28 augustss
288 1.28 augustss if (sc->sc_dying)
289 1.89 skrll return EIO;
290 1.28 augustss
291 1.3 augustss if (sc->sc_enabled)
292 1.89 skrll return EBUSY;
293 1.3 augustss
294 1.3 augustss sc->sc_enabled = 1;
295 1.91 bouyer sc->sc_ms.hidms_buttons = 0;
296 1.3 augustss
297 1.93.2.1 martin if (!sc->sc_alwayson) {
298 1.93.2.1 martin error = uhidev_open(&sc->sc_hdev);
299 1.93.2.1 martin if (error)
300 1.93.2.1 martin sc->sc_enabled = 0;
301 1.93.2.1 martin }
302 1.87 mlelstv
303 1.87 mlelstv return error;
304 1.3 augustss }
305 1.3 augustss
306 1.42 augustss Static void
307 1.44 augustss ums_disable(void *v)
308 1.3 augustss {
309 1.3 augustss struct ums_softc *sc = v;
310 1.25 augustss
311 1.25 augustss DPRINTFN(1,("ums_disable: sc=%p\n", sc));
312 1.25 augustss #ifdef DIAGNOSTIC
313 1.25 augustss if (!sc->sc_enabled) {
314 1.25 augustss printf("ums_disable: not enabled\n");
315 1.25 augustss return;
316 1.25 augustss }
317 1.25 augustss #endif
318 1.3 augustss
319 1.87 mlelstv if (sc->sc_enabled) {
320 1.87 mlelstv sc->sc_enabled = 0;
321 1.93.2.1 martin if (!sc->sc_alwayson)
322 1.93.2.1 martin uhidev_close(&sc->sc_hdev);
323 1.87 mlelstv }
324 1.3 augustss }
325 1.3 augustss
326 1.42 augustss Static int
327 1.68 christos ums_ioctl(void *v, u_long cmd, void *data, int flag,
328 1.67 christos struct lwp * p)
329 1.16 augustss
330 1.3 augustss {
331 1.77 mbalmer struct ums_softc *sc = v;
332 1.77 mbalmer
333 1.3 augustss switch (cmd) {
334 1.3 augustss case WSMOUSEIO_GTYPE:
335 1.91 bouyer if (sc->sc_ms.flags & HIDMS_ABS)
336 1.77 mbalmer *(u_int *)data = WSMOUSE_TYPE_TPANEL;
337 1.77 mbalmer else
338 1.77 mbalmer *(u_int *)data = WSMOUSE_TYPE_USB;
339 1.89 skrll return 0;
340 1.3 augustss }
341 1.16 augustss
342 1.89 skrll return EPASSTHROUGH;
343 1.3 augustss }
344