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