1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/* $OpenBSD: ums.c,v 1.51 2021/11/22 11:29:17 anton Exp $ */
/* $NetBSD: ums.c,v 1.60 2003/03/11 16:44:00 augustss Exp $ */
/*
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net) at
* Carlstedt Research & Technology.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* HID spec: https://www.usb.org/sites/default/files/hid1_11.pdf
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usb_quirks.h>
#include <dev/usb/uhidev.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>
#include <dev/hid/hidmsvar.h>
struct ums_softc {
struct uhidev sc_hdev;
struct hidms sc_ms;
uint32_t sc_quirks;
};
void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
int ums_enable(void *);
void ums_disable(void *);
int ums_ioctl(void *, u_long, caddr_t, int, struct proc *);
void ums_fix_elecom_descriptor(struct ums_softc *, void *, int, int);
const struct wsmouse_accessops ums_accessops = {
ums_enable,
ums_ioctl,
ums_disable,
};
int ums_match(struct device *, void *, void *);
void ums_attach(struct device *, struct device *, void *);
int ums_detach(struct device *, int);
struct cfdriver ums_cd = {
NULL, "ums", DV_DULL
};
const struct cfattach ums_ca = {
sizeof(struct ums_softc), ums_match, ums_attach, ums_detach
};
int
ums_match(struct device *parent, void *match, void *aux)
{
struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
int size;
void *desc;
if (UHIDEV_CLAIM_MULTIPLE_REPORTID(uha))
return (UMATCH_NONE);
uhidev_get_report_desc(uha->parent, &desc, &size);
if (hid_is_collection(desc, size, uha->reportid,
HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER)))
return (UMATCH_IFACECLASS);
if (hid_is_collection(desc, size, uha->reportid,
HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
return (UMATCH_IFACECLASS);
if (hid_is_collection(desc, size, uha->reportid,
HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCHSCREEN)))
return (UMATCH_IFACECLASS);
if (hid_is_collection(desc, size, uha->reportid,
HID_USAGE2(HUP_DIGITIZERS, HUD_PEN)))
return (UMATCH_IFACECLASS);
return (UMATCH_NONE);
}
void
ums_attach(struct device *parent, struct device *self, void *aux)
{
struct ums_softc *sc = (struct ums_softc *)self;
struct hidms *ms = &sc->sc_ms;
struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
struct usb_attach_arg *uaa = uha->uaa;
int size, repid;
void *desc;
u_int32_t qflags = 0;
sc->sc_hdev.sc_intr = ums_intr;
sc->sc_hdev.sc_parent = uha->parent;
sc->sc_hdev.sc_udev = uaa->device;
sc->sc_hdev.sc_report_id = uha->reportid;
usbd_set_idle(uha->parent->sc_udev, uha->parent->sc_ifaceno, 0, 0);
sc->sc_quirks = usbd_get_quirks(sc->sc_hdev.sc_udev)->uq_flags;
uhidev_get_report_desc(uha->parent, &desc, &size);
if (uaa->vendor == USB_VENDOR_ELECOM)
ums_fix_elecom_descriptor(sc, desc, size, uaa->product);
repid = uha->reportid;
sc->sc_hdev.sc_isize = hid_report_size(desc, size, hid_input, repid);
sc->sc_hdev.sc_osize = hid_report_size(desc, size, hid_output, repid);
sc->sc_hdev.sc_fsize = hid_report_size(desc, size, hid_feature, repid);
if (sc->sc_quirks & UQ_MS_REVZ)
qflags |= HIDMS_REVZ;
if (sc->sc_quirks & UQ_SPUR_BUT_UP)
qflags |= HIDMS_SPUR_BUT_UP;
if (sc->sc_quirks & UQ_MS_BAD_CLASS)
qflags |= HIDMS_MS_BAD_CLASS;
if (sc->sc_quirks & UQ_MS_LEADING_BYTE)
qflags |= HIDMS_LEADINGBYTE;
if (sc->sc_quirks & UQ_MS_VENDOR_BUTTONS)
qflags |= HIDMS_VENDOR_BUTTONS;
if (hidms_setup(self, ms, qflags, uha->reportid, desc, size) != 0)
return;
/*
* The Microsoft Wireless Notebook Optical Mouse 3000 Model 1049 has
* five Report IDs: 19, 23, 24, 17, 18 (in the order they appear in
* report descriptor), it seems that report 17 contains the necessary
* mouse information (3-buttons, X, Y, wheel) so we specify it
* manually.
*/
if (uaa->vendor == USB_VENDOR_MICROSOFT &&
uaa->product == USB_PRODUCT_MICROSOFT_WLNOTEBOOK3) {
ms->sc_flags = HIDMS_Z;
ms->sc_num_buttons = 3;
/* XXX change sc_hdev isize to 5? */
ms->sc_loc_x.pos = 8;
ms->sc_loc_y.pos = 16;
ms->sc_loc_z.pos = 24;
ms->sc_loc_btn[0].pos = 0;
ms->sc_loc_btn[1].pos = 1;
ms->sc_loc_btn[2].pos = 2;
}
hidms_attach(ms, &ums_accessops);
if (sc->sc_quirks & UQ_ALWAYS_OPEN) {
/* open uhidev and keep it open */
ums_enable(sc);
/* but mark the hidms not in use */
ums_disable(sc);
}
}
int
ums_detach(struct device *self, int flags)
{
struct ums_softc *sc = (struct ums_softc *)self;
struct hidms *ms = &sc->sc_ms;
return hidms_detach(ms, flags);
}
void
ums_intr(struct uhidev *addr, void *buf, u_int len)
{
struct ums_softc *sc = (struct ums_softc *)addr;
struct hidms *ms = &sc->sc_ms;
if (ms->sc_enabled != 0)
hidms_input(ms, (uint8_t *)buf, len);
}
int
ums_enable(void *v)
{
struct ums_softc *sc = v;
struct hidms *ms = &sc->sc_ms;
int rv;
if (usbd_is_dying(sc->sc_hdev.sc_udev))
return EIO;
if ((rv = hidms_enable(ms)) != 0)
return rv;
if ((sc->sc_quirks & UQ_ALWAYS_OPEN) &&
(sc->sc_hdev.sc_state & UHIDEV_OPEN))
rv = 0;
else
rv = uhidev_open(&sc->sc_hdev);
return rv;
}
void
ums_disable(void *v)
{
struct ums_softc *sc = v;
struct hidms *ms = &sc->sc_ms;
hidms_disable(ms);
if (sc->sc_quirks & UQ_ALWAYS_OPEN)
return;
uhidev_close(&sc->sc_hdev);
}
int
ums_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
{
struct ums_softc *sc = v;
struct hidms *ms = &sc->sc_ms;
int rc;
rc = uhidev_ioctl(&sc->sc_hdev, cmd, data, flag, p);
if (rc != -1)
return rc;
rc = hidms_ioctl(ms, cmd, data, flag, p);
if (rc != -1)
return rc;
switch (cmd) {
case WSMOUSEIO_GTYPE:
*(u_int *)data = WSMOUSE_TYPE_USB;
return 0;
default:
return -1;
}
}
/*
* Some ELECOM devices present flawed report descriptors. Instead of a 5-bit
* field and a 3-bit padding as defined by the descriptor they actually use
* 6 or 8 Bits to report button states, see
* https://flameeyes.blog/2017/04/24/elecom-deft-and-the-broken-descriptor
* This function adapts the Report Count value for the button page, its Usage
* Maximum and the report size for the padding bits (at offset 31).
*/
void
ums_fix_elecom_descriptor(struct ums_softc *sc, void *desc, int size,
int product)
{
static uByte match[] = { /* a descriptor fragment, offset: 12 */
0x95, 0x05, 0x75, 0x01, /* Report Count (5), Report Size (1) */
0x05, 0x09, 0x19, 0x01, /* Usage Page (Button), Usage Minimum (1) */
0x29, 0x05, /* Usage Maximum (5) */
};
uByte *udesc = desc;
int nbuttons;
switch (product) {
case USB_PRODUCT_ELECOM_MXT3URBK: /* EX-G Trackballs */
case USB_PRODUCT_ELECOM_MXT3DRBK:
case USB_PRODUCT_ELECOM_MXT4DRBK:
nbuttons = 6;
break;
case USB_PRODUCT_ELECOM_MDT1URBK: /* DEFT Trackballs */
case USB_PRODUCT_ELECOM_MDT1DRBK:
case USB_PRODUCT_ELECOM_MHT1URBK: /* HUGE Trackballs */
case USB_PRODUCT_ELECOM_MHT1DRBK:
nbuttons = 8;
break;
default:
return;
}
if (udesc == NULL || size < 32
|| memcmp(&udesc[12], match, sizeof(match))
|| udesc[30] != 0x75 || udesc[31] != 3)
return;
printf("%s: fixing Elecom report descriptor (buttons: %d)\n",
sc->sc_hdev.sc_dev.dv_xname, nbuttons);
udesc[13] = nbuttons;
udesc[21] = nbuttons;
udesc[31] = 8 - nbuttons;
}
|