uhid.c revision 1.115 1 1.115 riastrad /* $NetBSD: uhid.c,v 1.115 2020/11/29 22:54:51 riastradh Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.88 mrg * Copyright (c) 1998, 2004, 2008, 2012 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.6 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.38 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.88 mrg * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
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.1 augustss */
32 1.1 augustss
33 1.15 augustss /*
34 1.57 augustss * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35 1.15 augustss */
36 1.46 lukem
37 1.46 lukem #include <sys/cdefs.h>
38 1.115 riastrad __KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.115 2020/11/29 22:54:51 riastradh Exp $");
39 1.73 pavel
40 1.89 christos #ifdef _KERNEL_OPT
41 1.73 pavel #include "opt_compat_netbsd.h"
42 1.96 jakllsch #include "opt_usb.h"
43 1.89 christos #endif
44 1.1 augustss
45 1.1 augustss #include <sys/param.h>
46 1.115 riastrad #include <sys/types.h>
47 1.115 riastrad
48 1.115 riastrad #include <sys/atomic.h>
49 1.115 riastrad #include <sys/compat_stub.h>
50 1.115 riastrad #include <sys/conf.h>
51 1.115 riastrad #include <sys/device.h>
52 1.115 riastrad #include <sys/file.h>
53 1.115 riastrad #include <sys/intr.h>
54 1.115 riastrad #include <sys/ioctl.h>
55 1.1 augustss #include <sys/kernel.h>
56 1.95 skrll #include <sys/kmem.h>
57 1.115 riastrad #include <sys/poll.h>
58 1.115 riastrad #include <sys/proc.h>
59 1.115 riastrad #include <sys/select.h>
60 1.37 augustss #include <sys/signalvar.h>
61 1.115 riastrad #include <sys/systm.h>
62 1.1 augustss #include <sys/tty.h>
63 1.1 augustss #include <sys/vnode.h>
64 1.1 augustss
65 1.1 augustss #include <dev/usb/usb.h>
66 1.1 augustss #include <dev/usb/usbhid.h>
67 1.1 augustss
68 1.42 augustss #include <dev/usb/usbdevs.h>
69 1.1 augustss #include <dev/usb/usbdi.h>
70 1.1 augustss #include <dev/usb/usbdi_util.h>
71 1.1 augustss #include <dev/usb/usb_quirks.h>
72 1.101 bouyer #include <dev/hid/hid.h>
73 1.1 augustss
74 1.47 augustss #include <dev/usb/uhidev.h>
75 1.42 augustss
76 1.108 mrg #include "ioconf.h"
77 1.108 mrg
78 1.26 augustss #ifdef UHID_DEBUG
79 1.84 dyoung #define DPRINTF(x) if (uhiddebug) printf x
80 1.84 dyoung #define DPRINTFN(n,x) if (uhiddebug>(n)) printf x
81 1.1 augustss int uhiddebug = 0;
82 1.1 augustss #else
83 1.1 augustss #define DPRINTF(x)
84 1.1 augustss #define DPRINTFN(n,x)
85 1.1 augustss #endif
86 1.1 augustss
87 1.1 augustss struct uhid_softc {
88 1.47 augustss struct uhidev sc_hdev;
89 1.1 augustss
90 1.115 riastrad kmutex_t sc_lock;
91 1.88 mrg kcondvar_t sc_cv;
92 1.88 mrg kcondvar_t sc_detach_cv;
93 1.88 mrg
94 1.1 augustss int sc_isize;
95 1.1 augustss int sc_osize;
96 1.2 augustss int sc_fsize;
97 1.1 augustss
98 1.33 augustss u_char *sc_obuf;
99 1.1 augustss
100 1.93 mrg struct clist sc_q; /* protected by sc_lock */
101 1.1 augustss struct selinfo sc_rsel;
102 1.84 dyoung proc_t *sc_async; /* process that wants SIGIO */
103 1.80 ad void *sc_sih;
104 1.115 riastrad volatile uint32_t sc_state; /* driver state */
105 1.47 augustss #define UHID_IMMED 0x02 /* return read data immediately */
106 1.18 augustss
107 1.18 augustss int sc_refcnt;
108 1.112 christos int sc_raw;
109 1.115 riastrad u_char sc_open;
110 1.18 augustss u_char sc_dying;
111 1.1 augustss };
112 1.1 augustss
113 1.1 augustss #define UHIDUNIT(dev) (minor(dev))
114 1.1 augustss #define UHID_CHUNK 128 /* chunk size for read */
115 1.1 augustss #define UHID_BSIZE 1020 /* buffer size */
116 1.1 augustss
117 1.109 maxv static dev_type_open(uhidopen);
118 1.109 maxv static dev_type_close(uhidclose);
119 1.109 maxv static dev_type_read(uhidread);
120 1.109 maxv static dev_type_write(uhidwrite);
121 1.109 maxv static dev_type_ioctl(uhidioctl);
122 1.109 maxv static dev_type_poll(uhidpoll);
123 1.109 maxv static dev_type_kqfilter(uhidkqfilter);
124 1.53 gehenna
125 1.53 gehenna const struct cdevsw uhid_cdevsw = {
126 1.90 dholland .d_open = uhidopen,
127 1.90 dholland .d_close = uhidclose,
128 1.90 dholland .d_read = uhidread,
129 1.90 dholland .d_write = uhidwrite,
130 1.90 dholland .d_ioctl = uhidioctl,
131 1.90 dholland .d_stop = nostop,
132 1.90 dholland .d_tty = notty,
133 1.90 dholland .d_poll = uhidpoll,
134 1.90 dholland .d_mmap = nommap,
135 1.90 dholland .d_kqfilter = uhidkqfilter,
136 1.92 dholland .d_discard = nodiscard,
137 1.93 mrg .d_flag = D_OTHER
138 1.53 gehenna };
139 1.19 augustss
140 1.95 skrll Static void uhid_intr(struct uhidev *, void *, u_int);
141 1.80 ad Static void uhid_softintr(void *);
142 1.18 augustss
143 1.95 skrll Static int uhid_do_read(struct uhid_softc *, struct uio *, int);
144 1.95 skrll Static int uhid_do_write(struct uhid_softc *, struct uio *, int);
145 1.76 christos Static int uhid_do_ioctl(struct uhid_softc*, u_long, void *, int, struct lwp *);
146 1.1 augustss
147 1.109 maxv static int uhid_match(device_t, cfdata_t, void *);
148 1.109 maxv static void uhid_attach(device_t, device_t, void *);
149 1.109 maxv static int uhid_detach(device_t, int);
150 1.109 maxv static int uhid_activate(device_t, enum devact);
151 1.108 mrg
152 1.98 msaitoh CFATTACH_DECL_NEW(uhid, sizeof(struct uhid_softc), uhid_match, uhid_attach,
153 1.98 msaitoh uhid_detach, uhid_activate);
154 1.1 augustss
155 1.109 maxv static int
156 1.82 cube uhid_match(device_t parent, cfdata_t match, void *aux)
157 1.1 augustss {
158 1.66 tron #ifdef UHID_DEBUG
159 1.47 augustss struct uhidev_attach_arg *uha = aux;
160 1.66 tron #endif
161 1.47 augustss
162 1.47 augustss DPRINTF(("uhid_match: report=%d\n", uha->reportid));
163 1.47 augustss
164 1.65 augustss if (match->cf_flags & 1)
165 1.93 mrg return UMATCH_HIGHEST;
166 1.67 augustss else
167 1.93 mrg return UMATCH_IFACECLASS_GENERIC;
168 1.1 augustss }
169 1.1 augustss
170 1.109 maxv static void
171 1.82 cube uhid_attach(device_t parent, device_t self, void *aux)
172 1.1 augustss {
173 1.82 cube struct uhid_softc *sc = device_private(self);
174 1.47 augustss struct uhidev_attach_arg *uha = aux;
175 1.47 augustss int size, repid;
176 1.1 augustss void *desc;
177 1.52 augustss
178 1.82 cube sc->sc_hdev.sc_dev = self;
179 1.79 rmind selinit(&sc->sc_rsel);
180 1.47 augustss sc->sc_hdev.sc_intr = uhid_intr;
181 1.47 augustss sc->sc_hdev.sc_parent = uha->parent;
182 1.47 augustss sc->sc_hdev.sc_report_id = uha->reportid;
183 1.98 msaitoh sc->sc_sih = softint_establish(SOFTINT_CLOCK, uhid_softintr, sc);
184 1.47 augustss
185 1.47 augustss uhidev_get_report_desc(uha->parent, &desc, &size);
186 1.47 augustss repid = uha->reportid;
187 1.47 augustss sc->sc_isize = hid_report_size(desc, size, hid_input, repid);
188 1.47 augustss sc->sc_osize = hid_report_size(desc, size, hid_output, repid);
189 1.47 augustss sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
190 1.112 christos sc->sc_raw = hid_is_collection(desc, size, uha->reportid,
191 1.112 christos HID_USAGE2(HUP_FIDO, HUF_U2FHID));
192 1.1 augustss
193 1.82 cube aprint_naive("\n");
194 1.82 cube aprint_normal(": input=%d, output=%d, feature=%d\n",
195 1.47 augustss sc->sc_isize, sc->sc_osize, sc->sc_fsize);
196 1.32 augustss
197 1.95 skrll mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
198 1.88 mrg cv_init(&sc->sc_cv, "uhidrea");
199 1.88 mrg cv_init(&sc->sc_detach_cv, "uhiddet");
200 1.88 mrg
201 1.78 drochner if (!pmf_device_register(self, NULL, NULL))
202 1.78 drochner aprint_error_dev(self, "couldn't establish power handler\n");
203 1.78 drochner
204 1.84 dyoung return;
205 1.1 augustss }
206 1.1 augustss
207 1.109 maxv static int
208 1.84 dyoung uhid_activate(device_t self, enum devact act)
209 1.18 augustss {
210 1.82 cube struct uhid_softc *sc = device_private(self);
211 1.21 augustss
212 1.21 augustss switch (act) {
213 1.21 augustss case DVACT_DEACTIVATE:
214 1.21 augustss sc->sc_dying = 1;
215 1.83 dyoung return 0;
216 1.83 dyoung default:
217 1.83 dyoung return EOPNOTSUPP;
218 1.21 augustss }
219 1.12 augustss }
220 1.12 augustss
221 1.109 maxv static int
222 1.82 cube uhid_detach(device_t self, int flags)
223 1.1 augustss {
224 1.82 cube struct uhid_softc *sc = device_private(self);
225 1.18 augustss int maj, mn;
226 1.18 augustss
227 1.18 augustss DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
228 1.3 augustss
229 1.115 riastrad /* Prevent new I/O operations, and interrupt any pending reads. */
230 1.115 riastrad mutex_enter(&sc->sc_lock);
231 1.18 augustss sc->sc_dying = 1;
232 1.115 riastrad cv_broadcast(&sc->sc_cv);
233 1.115 riastrad mutex_exit(&sc->sc_lock);
234 1.18 augustss
235 1.115 riastrad /* Interrupt any pending uhidev_write. */
236 1.115 riastrad uhidev_stop(&sc->sc_hdev);
237 1.91 christos
238 1.115 riastrad /* Wait for I/O operations to complete. */
239 1.88 mrg mutex_enter(&sc->sc_lock);
240 1.115 riastrad while (sc->sc_refcnt) {
241 1.115 riastrad DPRINTF(("%s: open=%d refcnt=%d\n", __func__,
242 1.115 riastrad sc->sc_open, sc->sc_refcnt));
243 1.115 riastrad cv_wait(&sc->sc_detach_cv, &sc->sc_lock);
244 1.18 augustss }
245 1.88 mrg mutex_exit(&sc->sc_lock);
246 1.18 augustss
247 1.115 riastrad pmf_device_deregister(self);
248 1.115 riastrad
249 1.18 augustss /* locate the major number */
250 1.53 gehenna maj = cdevsw_lookup_major(&uhid_cdevsw);
251 1.18 augustss
252 1.18 augustss /* Nuke the vnodes for any open instances (calls close). */
253 1.69 thorpej mn = device_unit(self);
254 1.18 augustss vdevgone(maj, mn, mn, VCHR);
255 1.47 augustss
256 1.115 riastrad /*
257 1.115 riastrad * Wait for close to finish.
258 1.115 riastrad *
259 1.115 riastrad * XXX I assumed that vdevgone would synchronously call close,
260 1.115 riastrad * and not return before it has completed, but empirically the
261 1.115 riastrad * assertion of sc->sc_open == 0 below fires if we don't wait
262 1.115 riastrad * here. Someone^TM should carefully examine vdevgone to
263 1.115 riastrad * ascertain what it guarantees, and audit all other users of
264 1.115 riastrad * it accordingly.
265 1.115 riastrad */
266 1.115 riastrad mutex_enter(&sc->sc_lock);
267 1.115 riastrad while (sc->sc_open) {
268 1.115 riastrad DPRINTF(("%s: open=%d\n", __func__, sc->sc_open));
269 1.115 riastrad cv_wait(&sc->sc_detach_cv, &sc->sc_lock);
270 1.115 riastrad }
271 1.115 riastrad mutex_exit(&sc->sc_lock);
272 1.115 riastrad
273 1.115 riastrad KASSERT(sc->sc_open == 0);
274 1.115 riastrad KASSERT(sc->sc_refcnt == 0);
275 1.115 riastrad
276 1.88 mrg cv_destroy(&sc->sc_cv);
277 1.88 mrg cv_destroy(&sc->sc_detach_cv);
278 1.88 mrg mutex_destroy(&sc->sc_lock);
279 1.79 rmind seldestroy(&sc->sc_rsel);
280 1.80 ad softint_disestablish(sc->sc_sih);
281 1.18 augustss
282 1.93 mrg return 0;
283 1.1 augustss }
284 1.1 augustss
285 1.1 augustss void
286 1.47 augustss uhid_intr(struct uhidev *addr, void *data, u_int len)
287 1.1 augustss {
288 1.47 augustss struct uhid_softc *sc = (struct uhid_softc *)addr;
289 1.1 augustss
290 1.33 augustss #ifdef UHID_DEBUG
291 1.33 augustss if (uhiddebug > 5) {
292 1.95 skrll uint32_t i;
293 1.52 augustss
294 1.33 augustss DPRINTF(("uhid_intr: data ="));
295 1.47 augustss for (i = 0; i < len; i++)
296 1.51 augustss DPRINTF((" %02x", ((u_char *)data)[i]));
297 1.33 augustss DPRINTF(("\n"));
298 1.33 augustss }
299 1.33 augustss #endif
300 1.1 augustss
301 1.88 mrg mutex_enter(&sc->sc_lock);
302 1.47 augustss (void)b_to_q(data, len, &sc->sc_q);
303 1.52 augustss
304 1.115 riastrad DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
305 1.115 riastrad cv_broadcast(&sc->sc_cv);
306 1.115 riastrad selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
307 1.37 augustss if (sc->sc_async != NULL) {
308 1.37 augustss DPRINTFN(3, ("uhid_intr: sending SIGIO %p\n", sc->sc_async));
309 1.80 ad softint_schedule(sc->sc_sih);
310 1.37 augustss }
311 1.88 mrg mutex_exit(&sc->sc_lock);
312 1.1 augustss }
313 1.1 augustss
314 1.80 ad void
315 1.80 ad uhid_softintr(void *cookie)
316 1.80 ad {
317 1.80 ad struct uhid_softc *sc;
318 1.80 ad
319 1.80 ad sc = cookie;
320 1.80 ad
321 1.114 ad mutex_enter(&proc_lock);
322 1.80 ad if (sc->sc_async != NULL)
323 1.80 ad psignal(sc->sc_async, SIGIO);
324 1.114 ad mutex_exit(&proc_lock);
325 1.80 ad }
326 1.80 ad
327 1.109 maxv static int
328 1.88 mrg uhidopen(dev_t dev, int flag, int mode, struct lwp *l)
329 1.1 augustss {
330 1.25 augustss struct uhid_softc *sc;
331 1.47 augustss int error;
332 1.25 augustss
333 1.84 dyoung sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
334 1.84 dyoung if (sc == NULL)
335 1.84 dyoung return ENXIO;
336 1.1 augustss
337 1.18 augustss DPRINTF(("uhidopen: sc=%p\n", sc));
338 1.1 augustss
339 1.93 mrg /*
340 1.115 riastrad * Try to open. If dying, or if already open (or opening),
341 1.115 riastrad * fail -- opens are exclusive.
342 1.93 mrg */
343 1.115 riastrad mutex_enter(&sc->sc_lock);
344 1.115 riastrad if (sc->sc_dying) {
345 1.115 riastrad mutex_exit(&sc->sc_lock);
346 1.115 riastrad return ENXIO;
347 1.115 riastrad }
348 1.115 riastrad if (sc->sc_open) {
349 1.107 mrg mutex_exit(&sc->sc_lock);
350 1.93 mrg return EBUSY;
351 1.93 mrg }
352 1.115 riastrad sc->sc_open = 1;
353 1.115 riastrad atomic_store_relaxed(&sc->sc_state, 0);
354 1.107 mrg mutex_exit(&sc->sc_lock);
355 1.107 mrg
356 1.115 riastrad /* uhid interrupts aren't enabled yet, so setup sc_q now */
357 1.93 mrg if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
358 1.115 riastrad error = ENOMEM;
359 1.115 riastrad goto fail0;
360 1.93 mrg }
361 1.93 mrg
362 1.115 riastrad /* Allocate an output buffer if needed. */
363 1.97 mlelstv if (sc->sc_osize > 0)
364 1.97 mlelstv sc->sc_obuf = kmem_alloc(sc->sc_osize, KM_SLEEP);
365 1.97 mlelstv else
366 1.97 mlelstv sc->sc_obuf = NULL;
367 1.88 mrg
368 1.115 riastrad /* Paranoia: reset SIGIO before enabling interrputs. */
369 1.114 ad mutex_enter(&proc_lock);
370 1.47 augustss sc->sc_async = NULL;
371 1.114 ad mutex_exit(&proc_lock);
372 1.37 augustss
373 1.115 riastrad /* Open the uhidev -- after this point we can get interrupts. */
374 1.115 riastrad error = uhidev_open(&sc->sc_hdev);
375 1.115 riastrad if (error)
376 1.115 riastrad goto fail1;
377 1.115 riastrad
378 1.115 riastrad /* We are open for business. */
379 1.115 riastrad mutex_enter(&sc->sc_lock);
380 1.115 riastrad sc->sc_open = 2;
381 1.115 riastrad mutex_exit(&sc->sc_lock);
382 1.115 riastrad
383 1.93 mrg return 0;
384 1.115 riastrad
385 1.115 riastrad fail2: __unused
386 1.115 riastrad mutex_enter(&sc->sc_lock);
387 1.115 riastrad KASSERT(sc->sc_open == 2);
388 1.115 riastrad sc->sc_open = 1;
389 1.115 riastrad mutex_exit(&sc->sc_lock);
390 1.115 riastrad uhidev_close(&sc->sc_hdev);
391 1.115 riastrad fail1: selnotify(&sc->sc_rsel, POLLHUP, 0);
392 1.115 riastrad mutex_enter(&proc_lock);
393 1.115 riastrad sc->sc_async = NULL;
394 1.115 riastrad mutex_exit(&proc_lock);
395 1.115 riastrad if (sc->sc_osize > 0) {
396 1.115 riastrad kmem_free(sc->sc_obuf, sc->sc_osize);
397 1.115 riastrad sc->sc_obuf = NULL;
398 1.115 riastrad }
399 1.115 riastrad clfree(&sc->sc_q);
400 1.115 riastrad fail0: mutex_enter(&sc->sc_lock);
401 1.115 riastrad KASSERT(sc->sc_open == 1);
402 1.115 riastrad sc->sc_open = 0;
403 1.115 riastrad cv_broadcast(&sc->sc_detach_cv);
404 1.115 riastrad atomic_store_relaxed(&sc->sc_state, 0);
405 1.115 riastrad mutex_exit(&sc->sc_lock);
406 1.115 riastrad return error;
407 1.1 augustss }
408 1.1 augustss
409 1.109 maxv static int
410 1.88 mrg uhidclose(dev_t dev, int flag, int mode, struct lwp *l)
411 1.1 augustss {
412 1.25 augustss struct uhid_softc *sc;
413 1.25 augustss
414 1.84 dyoung sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
415 1.1 augustss
416 1.1 augustss DPRINTF(("uhidclose: sc=%p\n", sc));
417 1.1 augustss
418 1.115 riastrad /* We are closing up shop. Prevent new opens until we're done. */
419 1.115 riastrad mutex_enter(&sc->sc_lock);
420 1.115 riastrad KASSERT(sc->sc_open == 2);
421 1.115 riastrad sc->sc_open = 1;
422 1.115 riastrad mutex_exit(&sc->sc_lock);
423 1.115 riastrad
424 1.115 riastrad /* Prevent further interrupts. */
425 1.115 riastrad uhidev_close(&sc->sc_hdev);
426 1.115 riastrad
427 1.115 riastrad /* Hang up all select/poll. */
428 1.115 riastrad selnotify(&sc->sc_rsel, POLLHUP, 0);
429 1.115 riastrad
430 1.115 riastrad /* Reset SIGIO. */
431 1.114 ad mutex_enter(&proc_lock);
432 1.47 augustss sc->sc_async = NULL;
433 1.114 ad mutex_exit(&proc_lock);
434 1.88 mrg
435 1.115 riastrad /* Free the buffer and queue. */
436 1.115 riastrad if (sc->sc_osize > 0) {
437 1.115 riastrad kmem_free(sc->sc_obuf, sc->sc_osize);
438 1.115 riastrad sc->sc_obuf = NULL;
439 1.115 riastrad }
440 1.115 riastrad clfree(&sc->sc_q);
441 1.115 riastrad
442 1.115 riastrad /* All set. We are now closed. */
443 1.115 riastrad mutex_enter(&sc->sc_lock);
444 1.115 riastrad KASSERT(sc->sc_open == 1);
445 1.115 riastrad sc->sc_open = 0;
446 1.115 riastrad cv_broadcast(&sc->sc_detach_cv);
447 1.115 riastrad atomic_store_relaxed(&sc->sc_state, 0);
448 1.115 riastrad mutex_exit(&sc->sc_lock);
449 1.93 mrg
450 1.115 riastrad return 0;
451 1.115 riastrad }
452 1.115 riastrad
453 1.115 riastrad static int
454 1.115 riastrad uhid_enter(dev_t dev, struct uhid_softc **scp)
455 1.115 riastrad {
456 1.115 riastrad struct uhid_softc *sc;
457 1.115 riastrad int error;
458 1.115 riastrad
459 1.115 riastrad /* XXX need to hold reference to device */
460 1.115 riastrad sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
461 1.115 riastrad if (sc == NULL)
462 1.115 riastrad return ENXIO;
463 1.93 mrg
464 1.115 riastrad mutex_enter(&sc->sc_lock);
465 1.115 riastrad KASSERT(sc->sc_open == 2);
466 1.115 riastrad if (sc->sc_dying) {
467 1.115 riastrad error = ENXIO;
468 1.115 riastrad } else if (sc->sc_refcnt == INT_MAX) {
469 1.115 riastrad error = EBUSY;
470 1.115 riastrad } else {
471 1.115 riastrad *scp = sc;
472 1.115 riastrad sc->sc_refcnt++;
473 1.115 riastrad error = 0;
474 1.115 riastrad }
475 1.115 riastrad mutex_exit(&sc->sc_lock);
476 1.93 mrg
477 1.115 riastrad return error;
478 1.115 riastrad }
479 1.93 mrg
480 1.115 riastrad static void
481 1.115 riastrad uhid_exit(struct uhid_softc *sc)
482 1.115 riastrad {
483 1.37 augustss
484 1.115 riastrad mutex_enter(&sc->sc_lock);
485 1.115 riastrad KASSERT(sc->sc_open == 2);
486 1.115 riastrad KASSERT(sc->sc_refcnt > 0);
487 1.115 riastrad if (--sc->sc_refcnt == 0)
488 1.115 riastrad cv_broadcast(&sc->sc_detach_cv);
489 1.115 riastrad mutex_exit(&sc->sc_lock);
490 1.1 augustss }
491 1.1 augustss
492 1.110 maxv Static int
493 1.39 augustss uhid_do_read(struct uhid_softc *sc, struct uio *uio, int flag)
494 1.1 augustss {
495 1.1 augustss int error = 0;
496 1.47 augustss int extra;
497 1.1 augustss size_t length;
498 1.1 augustss u_char buffer[UHID_CHUNK];
499 1.27 augustss usbd_status err;
500 1.1 augustss
501 1.1 augustss DPRINTFN(1, ("uhidread\n"));
502 1.115 riastrad if (atomic_load_relaxed(&sc->sc_state) & UHID_IMMED) {
503 1.2 augustss DPRINTFN(1, ("uhidread immed\n"));
504 1.47 augustss extra = sc->sc_hdev.sc_report_id != 0;
505 1.111 maxv if (sc->sc_isize + extra > sizeof(buffer))
506 1.111 maxv return ENOBUFS;
507 1.47 augustss err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
508 1.47 augustss buffer, sc->sc_isize + extra);
509 1.27 augustss if (err)
510 1.93 mrg return EIO;
511 1.93 mrg return uiomove(buffer+extra, sc->sc_isize, uio);
512 1.2 augustss }
513 1.2 augustss
514 1.88 mrg mutex_enter(&sc->sc_lock);
515 1.1 augustss while (sc->sc_q.c_cc == 0) {
516 1.1 augustss if (flag & IO_NDELAY) {
517 1.88 mrg mutex_exit(&sc->sc_lock);
518 1.93 mrg return EWOULDBLOCK;
519 1.1 augustss }
520 1.115 riastrad if (sc->sc_dying) {
521 1.115 riastrad mutex_exit(&sc->sc_lock);
522 1.115 riastrad return EIO;
523 1.115 riastrad }
524 1.44 yamt DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
525 1.88 mrg error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
526 1.1 augustss DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
527 1.1 augustss if (error) {
528 1.18 augustss break;
529 1.1 augustss }
530 1.1 augustss }
531 1.1 augustss
532 1.1 augustss /* Transfer as many chunks as possible. */
533 1.18 augustss while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
534 1.102 riastrad length = uimin(sc->sc_q.c_cc, uio->uio_resid);
535 1.1 augustss if (length > sizeof(buffer))
536 1.1 augustss length = sizeof(buffer);
537 1.1 augustss
538 1.1 augustss /* Remove a small chunk from the input queue. */
539 1.1 augustss (void) q_to_b(&sc->sc_q, buffer, length);
540 1.29 augustss DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
541 1.1 augustss
542 1.1 augustss /* Copy the data to the user process. */
543 1.93 mrg mutex_exit(&sc->sc_lock);
544 1.1 augustss if ((error = uiomove(buffer, length, uio)) != 0)
545 1.93 mrg return error;
546 1.93 mrg mutex_enter(&sc->sc_lock);
547 1.1 augustss }
548 1.1 augustss
549 1.93 mrg mutex_exit(&sc->sc_lock);
550 1.93 mrg return error;
551 1.1 augustss }
552 1.1 augustss
553 1.109 maxv static int
554 1.39 augustss uhidread(dev_t dev, struct uio *uio, int flag)
555 1.1 augustss {
556 1.25 augustss struct uhid_softc *sc;
557 1.25 augustss int error;
558 1.25 augustss
559 1.115 riastrad error = uhid_enter(dev, &sc);
560 1.115 riastrad if (error)
561 1.115 riastrad return error;
562 1.18 augustss error = uhid_do_read(sc, uio, flag);
563 1.115 riastrad uhid_exit(sc);
564 1.93 mrg return error;
565 1.18 augustss }
566 1.18 augustss
567 1.110 maxv Static int
568 1.72 christos uhid_do_write(struct uhid_softc *sc, struct uio *uio, int flag)
569 1.18 augustss {
570 1.1 augustss int error;
571 1.1 augustss int size;
572 1.27 augustss usbd_status err;
573 1.1 augustss
574 1.18 augustss DPRINTFN(1, ("uhidwrite\n"));
575 1.52 augustss
576 1.1 augustss size = sc->sc_osize;
577 1.97 mlelstv if (uio->uio_resid != size || size == 0)
578 1.93 mrg return EINVAL;
579 1.18 augustss error = uiomove(sc->sc_obuf, size, uio);
580 1.112 christos #ifdef UHID_DEBUG
581 1.112 christos if (uhiddebug > 5) {
582 1.112 christos uint32_t i;
583 1.112 christos
584 1.112 christos DPRINTF(("%s: outdata[%d] =", device_xname(sc->sc_hdev.sc_dev),
585 1.112 christos error));
586 1.112 christos for (i = 0; i < size; i++)
587 1.112 christos DPRINTF((" %02x", sc->sc_obuf[i]));
588 1.112 christos DPRINTF(("\n"));
589 1.112 christos }
590 1.112 christos #endif
591 1.18 augustss if (!error) {
592 1.112 christos if (sc->sc_raw)
593 1.112 christos err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf,
594 1.112 christos size);
595 1.112 christos else
596 1.112 christos err = uhidev_set_report(&sc->sc_hdev,
597 1.112 christos UHID_OUTPUT_REPORT, sc->sc_obuf, size);
598 1.112 christos if (err) {
599 1.112 christos DPRINTF(("%s: err = %d\n",
600 1.112 christos device_xname(sc->sc_hdev.sc_dev), err));
601 1.1 augustss error = EIO;
602 1.112 christos }
603 1.1 augustss }
604 1.18 augustss
605 1.93 mrg return error;
606 1.1 augustss }
607 1.1 augustss
608 1.1 augustss int
609 1.39 augustss uhidwrite(dev_t dev, struct uio *uio, int flag)
610 1.18 augustss {
611 1.25 augustss struct uhid_softc *sc;
612 1.25 augustss int error;
613 1.25 augustss
614 1.115 riastrad error = uhid_enter(dev, &sc);
615 1.115 riastrad if (error)
616 1.115 riastrad return error;
617 1.18 augustss error = uhid_do_write(sc, uio, flag);
618 1.115 riastrad uhid_exit(sc);
619 1.93 mrg return error;
620 1.18 augustss }
621 1.18 augustss
622 1.18 augustss int
623 1.76 christos uhid_do_ioctl(struct uhid_softc *sc, u_long cmd, void *addr,
624 1.72 christos int flag, struct lwp *l)
625 1.1 augustss {
626 1.1 augustss struct usb_ctl_report_desc *rd;
627 1.2 augustss struct usb_ctl_report *re;
628 1.47 augustss u_char buffer[UHID_CHUNK];
629 1.47 augustss int size, extra;
630 1.27 augustss usbd_status err;
631 1.47 augustss void *desc;
632 1.1 augustss
633 1.18 augustss DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
634 1.18 augustss
635 1.1 augustss switch (cmd) {
636 1.2 augustss case FIONBIO:
637 1.2 augustss /* All handled in the upper FS layer. */
638 1.37 augustss break;
639 1.37 augustss
640 1.37 augustss case FIOASYNC:
641 1.114 ad mutex_enter(&proc_lock);
642 1.37 augustss if (*(int *)addr) {
643 1.111 maxv if (sc->sc_async != NULL) {
644 1.114 ad mutex_exit(&proc_lock);
645 1.93 mrg return EBUSY;
646 1.111 maxv }
647 1.68 christos sc->sc_async = l->l_proc;
648 1.68 christos DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
649 1.37 augustss } else
650 1.37 augustss sc->sc_async = NULL;
651 1.114 ad mutex_exit(&proc_lock);
652 1.37 augustss break;
653 1.37 augustss
654 1.37 augustss /* XXX this is not the most general solution. */
655 1.37 augustss case TIOCSPGRP:
656 1.114 ad mutex_enter(&proc_lock);
657 1.80 ad if (sc->sc_async == NULL) {
658 1.114 ad mutex_exit(&proc_lock);
659 1.93 mrg return EINVAL;
660 1.80 ad }
661 1.80 ad if (*(int *)addr != sc->sc_async->p_pgid) {
662 1.114 ad mutex_exit(&proc_lock);
663 1.93 mrg return EPERM;
664 1.80 ad }
665 1.114 ad mutex_exit(&proc_lock);
666 1.60 jdolecek break;
667 1.60 jdolecek
668 1.60 jdolecek case FIOSETOWN:
669 1.114 ad mutex_enter(&proc_lock);
670 1.80 ad if (sc->sc_async == NULL) {
671 1.114 ad mutex_exit(&proc_lock);
672 1.93 mrg return EINVAL;
673 1.80 ad }
674 1.60 jdolecek if (-*(int *)addr != sc->sc_async->p_pgid
675 1.80 ad && *(int *)addr != sc->sc_async->p_pid) {
676 1.114 ad mutex_exit(&proc_lock);
677 1.93 mrg return EPERM;
678 1.80 ad }
679 1.114 ad mutex_exit(&proc_lock);
680 1.2 augustss break;
681 1.2 augustss
682 1.113 christos case USB_HID_GET_RAW:
683 1.113 christos *(int *)addr = sc->sc_raw;
684 1.113 christos break;
685 1.113 christos
686 1.113 christos case USB_HID_SET_RAW:
687 1.113 christos sc->sc_raw = *(int *)addr;
688 1.113 christos break;
689 1.113 christos
690 1.1 augustss case USB_GET_REPORT_DESC:
691 1.47 augustss uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
692 1.1 augustss rd = (struct usb_ctl_report_desc *)addr;
693 1.102 riastrad size = uimin(size, sizeof(rd->ucrd_data));
694 1.50 christos rd->ucrd_size = size;
695 1.50 christos memcpy(rd->ucrd_data, desc, size);
696 1.1 augustss break;
697 1.2 augustss
698 1.2 augustss case USB_SET_IMMED:
699 1.9 augustss if (*(int *)addr) {
700 1.47 augustss extra = sc->sc_hdev.sc_report_id != 0;
701 1.111 maxv if (sc->sc_isize + extra > sizeof(buffer))
702 1.111 maxv return ENOBUFS;
703 1.47 augustss err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
704 1.47 augustss buffer, sc->sc_isize + extra);
705 1.27 augustss if (err)
706 1.93 mrg return EOPNOTSUPP;
707 1.12 augustss
708 1.115 riastrad atomic_or_32(&sc->sc_state, UHID_IMMED);
709 1.9 augustss } else
710 1.115 riastrad atomic_and_32(&sc->sc_state, ~UHID_IMMED);
711 1.2 augustss break;
712 1.2 augustss
713 1.2 augustss case USB_GET_REPORT:
714 1.2 augustss re = (struct usb_ctl_report *)addr;
715 1.50 christos switch (re->ucr_report) {
716 1.2 augustss case UHID_INPUT_REPORT:
717 1.2 augustss size = sc->sc_isize;
718 1.2 augustss break;
719 1.2 augustss case UHID_OUTPUT_REPORT:
720 1.2 augustss size = sc->sc_osize;
721 1.2 augustss break;
722 1.2 augustss case UHID_FEATURE_REPORT:
723 1.2 augustss size = sc->sc_fsize;
724 1.2 augustss break;
725 1.2 augustss default:
726 1.93 mrg return EINVAL;
727 1.2 augustss }
728 1.47 augustss extra = sc->sc_hdev.sc_report_id != 0;
729 1.111 maxv if (size + extra > sizeof(re->ucr_data))
730 1.111 maxv return ENOBUFS;
731 1.50 christos err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
732 1.50 christos re->ucr_data, size + extra);
733 1.47 augustss if (extra)
734 1.99 maya memmove(re->ucr_data, re->ucr_data+1, size);
735 1.35 augustss if (err)
736 1.93 mrg return EIO;
737 1.35 augustss break;
738 1.35 augustss
739 1.35 augustss case USB_SET_REPORT:
740 1.35 augustss re = (struct usb_ctl_report *)addr;
741 1.50 christos switch (re->ucr_report) {
742 1.35 augustss case UHID_INPUT_REPORT:
743 1.35 augustss size = sc->sc_isize;
744 1.35 augustss break;
745 1.35 augustss case UHID_OUTPUT_REPORT:
746 1.35 augustss size = sc->sc_osize;
747 1.35 augustss break;
748 1.35 augustss case UHID_FEATURE_REPORT:
749 1.35 augustss size = sc->sc_fsize;
750 1.35 augustss break;
751 1.35 augustss default:
752 1.93 mrg return EINVAL;
753 1.35 augustss }
754 1.111 maxv if (size > sizeof(re->ucr_data))
755 1.111 maxv return ENOBUFS;
756 1.50 christos err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
757 1.50 christos re->ucr_data, size);
758 1.27 augustss if (err)
759 1.93 mrg return EIO;
760 1.2 augustss break;
761 1.2 augustss
762 1.47 augustss case USB_GET_REPORT_ID:
763 1.47 augustss *(int *)addr = sc->sc_hdev.sc_report_id;
764 1.47 augustss break;
765 1.47 augustss
766 1.87 erh case USB_GET_DEVICE_DESC:
767 1.87 erh *(usb_device_descriptor_t *)addr =
768 1.87 erh *usbd_get_device_descriptor(sc->sc_hdev.sc_parent->sc_udev);
769 1.87 erh break;
770 1.87 erh
771 1.61 jdolecek case USB_GET_DEVICEINFO:
772 1.61 jdolecek usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev,
773 1.95 skrll (struct usb_device_info *)addr, 0);
774 1.61 jdolecek break;
775 1.73 pavel case USB_GET_DEVICEINFO_OLD:
776 1.106 pgoyette MODULE_HOOK_CALL(usb_subr_fill_30_hook,
777 1.103 pgoyette (sc->sc_hdev.sc_parent->sc_udev,
778 1.103 pgoyette (struct usb_device_info_old *)addr, 0,
779 1.103 pgoyette usbd_devinfo_vp, usbd_printBCD),
780 1.103 pgoyette enosys(), err);
781 1.103 pgoyette if (err == 0)
782 1.103 pgoyette return 0;
783 1.73 pavel break;
784 1.95 skrll case USB_GET_STRING_DESC:
785 1.61 jdolecek {
786 1.95 skrll struct usb_string_desc *si = (struct usb_string_desc *)addr;
787 1.95 skrll err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev,
788 1.61 jdolecek si->usd_string_index,
789 1.95 skrll si->usd_language_id, &si->usd_desc, &size);
790 1.95 skrll if (err)
791 1.95 skrll return EINVAL;
792 1.95 skrll break;
793 1.61 jdolecek }
794 1.61 jdolecek
795 1.1 augustss default:
796 1.93 mrg return EINVAL;
797 1.1 augustss }
798 1.93 mrg return 0;
799 1.1 augustss }
800 1.1 augustss
801 1.109 maxv static int
802 1.76 christos uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
803 1.18 augustss {
804 1.25 augustss struct uhid_softc *sc;
805 1.25 augustss int error;
806 1.25 augustss
807 1.115 riastrad error = uhid_enter(dev, &sc);
808 1.115 riastrad if (error)
809 1.115 riastrad return error;
810 1.68 christos error = uhid_do_ioctl(sc, cmd, addr, flag, l);
811 1.115 riastrad uhid_exit(sc);
812 1.93 mrg return error;
813 1.18 augustss }
814 1.18 augustss
815 1.109 maxv static int
816 1.68 christos uhidpoll(dev_t dev, int events, struct lwp *l)
817 1.1 augustss {
818 1.25 augustss struct uhid_softc *sc;
819 1.1 augustss int revents = 0;
820 1.25 augustss
821 1.115 riastrad if (uhid_enter(dev, &sc) != 0)
822 1.115 riastrad return POLLHUP;
823 1.1 augustss
824 1.88 mrg mutex_enter(&sc->sc_lock);
825 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
826 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
827 1.4 veego if (events & (POLLIN | POLLRDNORM)) {
828 1.1 augustss if (sc->sc_q.c_cc > 0)
829 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
830 1.1 augustss else
831 1.68 christos selrecord(l, &sc->sc_rsel);
832 1.4 veego }
833 1.88 mrg mutex_exit(&sc->sc_lock);
834 1.1 augustss
835 1.115 riastrad uhid_exit(sc);
836 1.93 mrg return revents;
837 1.55 jdolecek }
838 1.55 jdolecek
839 1.55 jdolecek static void
840 1.55 jdolecek filt_uhidrdetach(struct knote *kn)
841 1.55 jdolecek {
842 1.55 jdolecek struct uhid_softc *sc = kn->kn_hook;
843 1.55 jdolecek
844 1.88 mrg mutex_enter(&sc->sc_lock);
845 1.56 christos SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
846 1.88 mrg mutex_exit(&sc->sc_lock);
847 1.55 jdolecek }
848 1.55 jdolecek
849 1.55 jdolecek static int
850 1.72 christos filt_uhidread(struct knote *kn, long hint)
851 1.55 jdolecek {
852 1.55 jdolecek struct uhid_softc *sc = kn->kn_hook;
853 1.55 jdolecek
854 1.115 riastrad if (hint == NOTE_SUBMIT)
855 1.115 riastrad KASSERT(mutex_owned(&sc->sc_lock));
856 1.115 riastrad else
857 1.115 riastrad mutex_enter(&sc->sc_lock);
858 1.115 riastrad
859 1.55 jdolecek kn->kn_data = sc->sc_q.c_cc;
860 1.115 riastrad
861 1.115 riastrad if (hint == NOTE_SUBMIT)
862 1.115 riastrad KASSERT(mutex_owned(&sc->sc_lock));
863 1.115 riastrad else
864 1.115 riastrad mutex_exit(&sc->sc_lock);
865 1.115 riastrad
866 1.95 skrll return kn->kn_data > 0;
867 1.55 jdolecek }
868 1.55 jdolecek
869 1.100 maya static const struct filterops uhidread_filtops = {
870 1.100 maya .f_isfd = 1,
871 1.100 maya .f_attach = NULL,
872 1.100 maya .f_detach = filt_uhidrdetach,
873 1.100 maya .f_event = filt_uhidread,
874 1.100 maya };
875 1.55 jdolecek
876 1.100 maya static const struct filterops uhid_seltrue_filtops = {
877 1.100 maya .f_isfd = 1,
878 1.100 maya .f_attach = NULL,
879 1.100 maya .f_detach = filt_uhidrdetach,
880 1.100 maya .f_event = filt_seltrue,
881 1.100 maya };
882 1.55 jdolecek
883 1.109 maxv static int
884 1.55 jdolecek uhidkqfilter(dev_t dev, struct knote *kn)
885 1.55 jdolecek {
886 1.55 jdolecek struct uhid_softc *sc;
887 1.55 jdolecek struct klist *klist;
888 1.115 riastrad int error;
889 1.55 jdolecek
890 1.115 riastrad error = uhid_enter(dev, &sc);
891 1.115 riastrad if (error)
892 1.115 riastrad return error;
893 1.55 jdolecek
894 1.55 jdolecek switch (kn->kn_filter) {
895 1.55 jdolecek case EVFILT_READ:
896 1.56 christos klist = &sc->sc_rsel.sel_klist;
897 1.55 jdolecek kn->kn_fop = &uhidread_filtops;
898 1.55 jdolecek break;
899 1.55 jdolecek case EVFILT_WRITE:
900 1.56 christos klist = &sc->sc_rsel.sel_klist;
901 1.55 jdolecek kn->kn_fop = &uhid_seltrue_filtops;
902 1.55 jdolecek break;
903 1.55 jdolecek default:
904 1.115 riastrad error = EINVAL;
905 1.115 riastrad goto out;
906 1.55 jdolecek }
907 1.55 jdolecek
908 1.55 jdolecek kn->kn_hook = sc;
909 1.55 jdolecek
910 1.88 mrg mutex_enter(&sc->sc_lock);
911 1.55 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
912 1.88 mrg mutex_exit(&sc->sc_lock);
913 1.55 jdolecek
914 1.115 riastrad out: uhid_exit(sc);
915 1.115 riastrad return error;
916 1.1 augustss }
917