uhid.c revision 1.120 1 1.120 riastrad /* $NetBSD: uhid.c,v 1.120 2022/03/28 12:42:45 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.120 riastrad __KERNEL_RCSID(0, "$NetBSD: uhid.c,v 1.120 2022/03/28 12:42:45 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
93 1.1 augustss int sc_isize;
94 1.1 augustss int sc_osize;
95 1.2 augustss int sc_fsize;
96 1.1 augustss
97 1.33 augustss u_char *sc_obuf;
98 1.1 augustss
99 1.93 mrg struct clist sc_q; /* protected by sc_lock */
100 1.1 augustss struct selinfo sc_rsel;
101 1.84 dyoung proc_t *sc_async; /* process that wants SIGIO */
102 1.80 ad void *sc_sih;
103 1.115 riastrad volatile uint32_t sc_state; /* driver state */
104 1.47 augustss #define UHID_IMMED 0x02 /* return read data immediately */
105 1.18 augustss
106 1.112 christos int sc_raw;
107 1.120 riastrad enum {
108 1.120 riastrad UHID_CLOSED,
109 1.120 riastrad UHID_OPENING,
110 1.120 riastrad UHID_OPEN,
111 1.120 riastrad } sc_open;
112 1.120 riastrad bool sc_closing;
113 1.1 augustss };
114 1.1 augustss
115 1.1 augustss #define UHIDUNIT(dev) (minor(dev))
116 1.1 augustss #define UHID_CHUNK 128 /* chunk size for read */
117 1.1 augustss #define UHID_BSIZE 1020 /* buffer size */
118 1.1 augustss
119 1.109 maxv static dev_type_open(uhidopen);
120 1.120 riastrad static dev_type_cancel(uhidcancel);
121 1.109 maxv static dev_type_close(uhidclose);
122 1.109 maxv static dev_type_read(uhidread);
123 1.109 maxv static dev_type_write(uhidwrite);
124 1.109 maxv static dev_type_ioctl(uhidioctl);
125 1.109 maxv static dev_type_poll(uhidpoll);
126 1.109 maxv static dev_type_kqfilter(uhidkqfilter);
127 1.53 gehenna
128 1.53 gehenna const struct cdevsw uhid_cdevsw = {
129 1.90 dholland .d_open = uhidopen,
130 1.120 riastrad .d_cancel = uhidcancel,
131 1.90 dholland .d_close = uhidclose,
132 1.90 dholland .d_read = uhidread,
133 1.90 dholland .d_write = uhidwrite,
134 1.90 dholland .d_ioctl = uhidioctl,
135 1.90 dholland .d_stop = nostop,
136 1.90 dholland .d_tty = notty,
137 1.90 dholland .d_poll = uhidpoll,
138 1.90 dholland .d_mmap = nommap,
139 1.90 dholland .d_kqfilter = uhidkqfilter,
140 1.92 dholland .d_discard = nodiscard,
141 1.120 riastrad .d_cfdriver = &uhid_cd,
142 1.120 riastrad .d_devtounit = dev_minor_unit,
143 1.93 mrg .d_flag = D_OTHER
144 1.53 gehenna };
145 1.19 augustss
146 1.120 riastrad static void uhid_intr(struct uhidev *, void *, u_int);
147 1.1 augustss
148 1.109 maxv static int uhid_match(device_t, cfdata_t, void *);
149 1.109 maxv static void uhid_attach(device_t, device_t, void *);
150 1.109 maxv static int uhid_detach(device_t, int);
151 1.108 mrg
152 1.98 msaitoh CFATTACH_DECL_NEW(uhid, sizeof(struct uhid_softc), uhid_match, uhid_attach,
153 1.120 riastrad uhid_detach, NULL);
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.47 augustss
184 1.47 augustss uhidev_get_report_desc(uha->parent, &desc, &size);
185 1.47 augustss repid = uha->reportid;
186 1.47 augustss sc->sc_isize = hid_report_size(desc, size, hid_input, repid);
187 1.47 augustss sc->sc_osize = hid_report_size(desc, size, hid_output, repid);
188 1.47 augustss sc->sc_fsize = hid_report_size(desc, size, hid_feature, repid);
189 1.112 christos sc->sc_raw = hid_is_collection(desc, size, uha->reportid,
190 1.112 christos HID_USAGE2(HUP_FIDO, HUF_U2FHID));
191 1.1 augustss
192 1.82 cube aprint_naive("\n");
193 1.82 cube aprint_normal(": input=%d, output=%d, feature=%d\n",
194 1.47 augustss sc->sc_isize, sc->sc_osize, sc->sc_fsize);
195 1.32 augustss
196 1.95 skrll mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
197 1.88 mrg cv_init(&sc->sc_cv, "uhidrea");
198 1.88 mrg
199 1.78 drochner if (!pmf_device_register(self, NULL, NULL))
200 1.78 drochner aprint_error_dev(self, "couldn't establish power handler\n");
201 1.78 drochner
202 1.84 dyoung return;
203 1.1 augustss }
204 1.1 augustss
205 1.109 maxv static int
206 1.82 cube uhid_detach(device_t self, int flags)
207 1.1 augustss {
208 1.82 cube struct uhid_softc *sc = device_private(self);
209 1.18 augustss int maj, mn;
210 1.18 augustss
211 1.18 augustss DPRINTF(("uhid_detach: sc=%p flags=%d\n", sc, flags));
212 1.3 augustss
213 1.115 riastrad pmf_device_deregister(self);
214 1.115 riastrad
215 1.18 augustss /* locate the major number */
216 1.53 gehenna maj = cdevsw_lookup_major(&uhid_cdevsw);
217 1.18 augustss
218 1.18 augustss /* Nuke the vnodes for any open instances (calls close). */
219 1.69 thorpej mn = device_unit(self);
220 1.18 augustss vdevgone(maj, mn, mn, VCHR);
221 1.47 augustss
222 1.120 riastrad KASSERTMSG(sc->sc_open == UHID_CLOSED, "open=%d", (int)sc->sc_open);
223 1.115 riastrad
224 1.88 mrg cv_destroy(&sc->sc_cv);
225 1.88 mrg mutex_destroy(&sc->sc_lock);
226 1.79 rmind seldestroy(&sc->sc_rsel);
227 1.18 augustss
228 1.93 mrg return 0;
229 1.1 augustss }
230 1.1 augustss
231 1.120 riastrad static void
232 1.47 augustss uhid_intr(struct uhidev *addr, void *data, u_int len)
233 1.1 augustss {
234 1.47 augustss struct uhid_softc *sc = (struct uhid_softc *)addr;
235 1.1 augustss
236 1.33 augustss #ifdef UHID_DEBUG
237 1.33 augustss if (uhiddebug > 5) {
238 1.95 skrll uint32_t i;
239 1.52 augustss
240 1.33 augustss DPRINTF(("uhid_intr: data ="));
241 1.47 augustss for (i = 0; i < len; i++)
242 1.51 augustss DPRINTF((" %02x", ((u_char *)data)[i]));
243 1.33 augustss DPRINTF(("\n"));
244 1.33 augustss }
245 1.33 augustss #endif
246 1.1 augustss
247 1.88 mrg mutex_enter(&sc->sc_lock);
248 1.47 augustss (void)b_to_q(data, len, &sc->sc_q);
249 1.52 augustss
250 1.115 riastrad DPRINTFN(5, ("uhid_intr: waking %p\n", &sc->sc_q));
251 1.115 riastrad cv_broadcast(&sc->sc_cv);
252 1.115 riastrad selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
253 1.116 riastrad if (atomic_load_relaxed(&sc->sc_async) != NULL) {
254 1.116 riastrad mutex_enter(&proc_lock);
255 1.116 riastrad if (sc->sc_async != NULL) {
256 1.116 riastrad DPRINTFN(3, ("uhid_intr: sending SIGIO to %jd\n",
257 1.116 riastrad (intmax_t)sc->sc_async->p_pid));
258 1.116 riastrad psignal(sc->sc_async, SIGIO);
259 1.116 riastrad }
260 1.116 riastrad mutex_exit(&proc_lock);
261 1.37 augustss }
262 1.88 mrg mutex_exit(&sc->sc_lock);
263 1.1 augustss }
264 1.1 augustss
265 1.109 maxv static int
266 1.88 mrg uhidopen(dev_t dev, int flag, int mode, struct lwp *l)
267 1.1 augustss {
268 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
269 1.47 augustss int error;
270 1.25 augustss
271 1.120 riastrad DPRINTF(("uhidopen: sc=%p\n", sc));
272 1.84 dyoung if (sc == NULL)
273 1.84 dyoung return ENXIO;
274 1.1 augustss
275 1.93 mrg /*
276 1.120 riastrad * Try to open. If closing in progress, give up. If already
277 1.120 riastrad * open (or opening), fail -- opens are exclusive.
278 1.93 mrg */
279 1.115 riastrad mutex_enter(&sc->sc_lock);
280 1.120 riastrad if (sc->sc_open != UHID_CLOSED || sc->sc_closing) {
281 1.107 mrg mutex_exit(&sc->sc_lock);
282 1.93 mrg return EBUSY;
283 1.93 mrg }
284 1.120 riastrad sc->sc_open = UHID_OPENING;
285 1.115 riastrad atomic_store_relaxed(&sc->sc_state, 0);
286 1.107 mrg mutex_exit(&sc->sc_lock);
287 1.107 mrg
288 1.115 riastrad /* uhid interrupts aren't enabled yet, so setup sc_q now */
289 1.93 mrg if (clalloc(&sc->sc_q, UHID_BSIZE, 0) == -1) {
290 1.115 riastrad error = ENOMEM;
291 1.115 riastrad goto fail0;
292 1.93 mrg }
293 1.93 mrg
294 1.115 riastrad /* Allocate an output buffer if needed. */
295 1.97 mlelstv if (sc->sc_osize > 0)
296 1.97 mlelstv sc->sc_obuf = kmem_alloc(sc->sc_osize, KM_SLEEP);
297 1.97 mlelstv else
298 1.97 mlelstv sc->sc_obuf = NULL;
299 1.88 mrg
300 1.115 riastrad /* Paranoia: reset SIGIO before enabling interrputs. */
301 1.114 ad mutex_enter(&proc_lock);
302 1.116 riastrad atomic_store_relaxed(&sc->sc_async, NULL);
303 1.114 ad mutex_exit(&proc_lock);
304 1.37 augustss
305 1.115 riastrad /* Open the uhidev -- after this point we can get interrupts. */
306 1.115 riastrad error = uhidev_open(&sc->sc_hdev);
307 1.115 riastrad if (error)
308 1.115 riastrad goto fail1;
309 1.115 riastrad
310 1.115 riastrad /* We are open for business. */
311 1.115 riastrad mutex_enter(&sc->sc_lock);
312 1.120 riastrad sc->sc_open = UHID_OPEN;
313 1.120 riastrad cv_broadcast(&sc->sc_cv);
314 1.115 riastrad mutex_exit(&sc->sc_lock);
315 1.115 riastrad
316 1.93 mrg return 0;
317 1.115 riastrad
318 1.115 riastrad fail1: selnotify(&sc->sc_rsel, POLLHUP, 0);
319 1.115 riastrad mutex_enter(&proc_lock);
320 1.116 riastrad atomic_store_relaxed(&sc->sc_async, NULL);
321 1.115 riastrad mutex_exit(&proc_lock);
322 1.115 riastrad if (sc->sc_osize > 0) {
323 1.115 riastrad kmem_free(sc->sc_obuf, sc->sc_osize);
324 1.115 riastrad sc->sc_obuf = NULL;
325 1.115 riastrad }
326 1.115 riastrad clfree(&sc->sc_q);
327 1.115 riastrad fail0: mutex_enter(&sc->sc_lock);
328 1.120 riastrad KASSERT(sc->sc_open == UHID_OPENING);
329 1.120 riastrad sc->sc_open = UHID_CLOSED;
330 1.120 riastrad cv_broadcast(&sc->sc_cv);
331 1.115 riastrad atomic_store_relaxed(&sc->sc_state, 0);
332 1.115 riastrad mutex_exit(&sc->sc_lock);
333 1.115 riastrad return error;
334 1.1 augustss }
335 1.1 augustss
336 1.109 maxv static int
337 1.120 riastrad uhidcancel(dev_t dev, int flag, int mode, struct lwp *l)
338 1.120 riastrad {
339 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
340 1.120 riastrad
341 1.120 riastrad DPRINTF(("uhidcancel: sc=%p\n", sc));
342 1.120 riastrad if (sc == NULL)
343 1.120 riastrad return 0;
344 1.120 riastrad
345 1.120 riastrad /*
346 1.120 riastrad * Mark it closing, wake any waiters, and suspend output.
347 1.120 riastrad * After this point, no new xfers can be submitted.
348 1.120 riastrad */
349 1.120 riastrad mutex_enter(&sc->sc_lock);
350 1.120 riastrad sc->sc_closing = true;
351 1.120 riastrad cv_broadcast(&sc->sc_cv);
352 1.120 riastrad mutex_exit(&sc->sc_lock);
353 1.120 riastrad
354 1.120 riastrad uhidev_stop(&sc->sc_hdev);
355 1.120 riastrad
356 1.120 riastrad return 0;
357 1.120 riastrad }
358 1.120 riastrad
359 1.120 riastrad static int
360 1.88 mrg uhidclose(dev_t dev, int flag, int mode, struct lwp *l)
361 1.1 augustss {
362 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
363 1.1 augustss
364 1.1 augustss DPRINTF(("uhidclose: sc=%p\n", sc));
365 1.120 riastrad if (sc == NULL)
366 1.120 riastrad return 0;
367 1.1 augustss
368 1.115 riastrad mutex_enter(&sc->sc_lock);
369 1.120 riastrad KASSERT(sc->sc_closing);
370 1.120 riastrad KASSERTMSG(sc->sc_open == UHID_OPEN || sc->sc_open == UHID_CLOSED,
371 1.120 riastrad "sc_open=%d", sc->sc_open);
372 1.120 riastrad if (sc->sc_open == UHID_CLOSED)
373 1.120 riastrad goto out;
374 1.120 riastrad
375 1.120 riastrad /* Release the lock while we free things. */
376 1.115 riastrad mutex_exit(&sc->sc_lock);
377 1.115 riastrad
378 1.115 riastrad /* Prevent further interrupts. */
379 1.115 riastrad uhidev_close(&sc->sc_hdev);
380 1.115 riastrad
381 1.115 riastrad /* Hang up all select/poll. */
382 1.115 riastrad selnotify(&sc->sc_rsel, POLLHUP, 0);
383 1.115 riastrad
384 1.115 riastrad /* Reset SIGIO. */
385 1.114 ad mutex_enter(&proc_lock);
386 1.116 riastrad atomic_store_relaxed(&sc->sc_async, NULL);
387 1.114 ad mutex_exit(&proc_lock);
388 1.88 mrg
389 1.115 riastrad /* Free the buffer and queue. */
390 1.115 riastrad if (sc->sc_osize > 0) {
391 1.115 riastrad kmem_free(sc->sc_obuf, sc->sc_osize);
392 1.115 riastrad sc->sc_obuf = NULL;
393 1.115 riastrad }
394 1.115 riastrad clfree(&sc->sc_q);
395 1.115 riastrad
396 1.120 riastrad mutex_enter(&sc->sc_lock);
397 1.120 riastrad
398 1.115 riastrad /* All set. We are now closed. */
399 1.120 riastrad sc->sc_open = UHID_CLOSED;
400 1.120 riastrad out: KASSERT(sc->sc_open == UHID_CLOSED);
401 1.120 riastrad sc->sc_closing = false;
402 1.120 riastrad cv_broadcast(&sc->sc_cv);
403 1.115 riastrad atomic_store_relaxed(&sc->sc_state, 0);
404 1.115 riastrad mutex_exit(&sc->sc_lock);
405 1.93 mrg
406 1.115 riastrad return 0;
407 1.115 riastrad }
408 1.115 riastrad
409 1.115 riastrad static int
410 1.120 riastrad uhidread(dev_t dev, struct uio *uio, int flag)
411 1.1 augustss {
412 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
413 1.1 augustss int error = 0;
414 1.47 augustss int extra;
415 1.1 augustss size_t length;
416 1.1 augustss u_char buffer[UHID_CHUNK];
417 1.27 augustss usbd_status err;
418 1.1 augustss
419 1.1 augustss DPRINTFN(1, ("uhidread\n"));
420 1.115 riastrad if (atomic_load_relaxed(&sc->sc_state) & UHID_IMMED) {
421 1.2 augustss DPRINTFN(1, ("uhidread immed\n"));
422 1.47 augustss extra = sc->sc_hdev.sc_report_id != 0;
423 1.111 maxv if (sc->sc_isize + extra > sizeof(buffer))
424 1.111 maxv return ENOBUFS;
425 1.47 augustss err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
426 1.47 augustss buffer, sc->sc_isize + extra);
427 1.27 augustss if (err)
428 1.93 mrg return EIO;
429 1.93 mrg return uiomove(buffer+extra, sc->sc_isize, uio);
430 1.2 augustss }
431 1.2 augustss
432 1.88 mrg mutex_enter(&sc->sc_lock);
433 1.1 augustss while (sc->sc_q.c_cc == 0) {
434 1.1 augustss if (flag & IO_NDELAY) {
435 1.88 mrg mutex_exit(&sc->sc_lock);
436 1.93 mrg return EWOULDBLOCK;
437 1.1 augustss }
438 1.120 riastrad if (sc->sc_closing) {
439 1.115 riastrad mutex_exit(&sc->sc_lock);
440 1.115 riastrad return EIO;
441 1.115 riastrad }
442 1.44 yamt DPRINTFN(5, ("uhidread: sleep on %p\n", &sc->sc_q));
443 1.88 mrg error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
444 1.1 augustss DPRINTFN(5, ("uhidread: woke, error=%d\n", error));
445 1.1 augustss if (error) {
446 1.18 augustss break;
447 1.1 augustss }
448 1.1 augustss }
449 1.1 augustss
450 1.1 augustss /* Transfer as many chunks as possible. */
451 1.18 augustss while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0 && !error) {
452 1.102 riastrad length = uimin(sc->sc_q.c_cc, uio->uio_resid);
453 1.1 augustss if (length > sizeof(buffer))
454 1.1 augustss length = sizeof(buffer);
455 1.1 augustss
456 1.1 augustss /* Remove a small chunk from the input queue. */
457 1.1 augustss (void) q_to_b(&sc->sc_q, buffer, length);
458 1.29 augustss DPRINTFN(5, ("uhidread: got %lu chars\n", (u_long)length));
459 1.1 augustss
460 1.1 augustss /* Copy the data to the user process. */
461 1.93 mrg mutex_exit(&sc->sc_lock);
462 1.1 augustss if ((error = uiomove(buffer, length, uio)) != 0)
463 1.93 mrg return error;
464 1.93 mrg mutex_enter(&sc->sc_lock);
465 1.1 augustss }
466 1.1 augustss
467 1.93 mrg mutex_exit(&sc->sc_lock);
468 1.93 mrg return error;
469 1.1 augustss }
470 1.1 augustss
471 1.109 maxv static int
472 1.120 riastrad uhidwrite(dev_t dev, struct uio *uio, int flag)
473 1.18 augustss {
474 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
475 1.1 augustss int error;
476 1.1 augustss int size;
477 1.27 augustss usbd_status err;
478 1.1 augustss
479 1.18 augustss DPRINTFN(1, ("uhidwrite\n"));
480 1.52 augustss
481 1.1 augustss size = sc->sc_osize;
482 1.97 mlelstv if (uio->uio_resid != size || size == 0)
483 1.93 mrg return EINVAL;
484 1.18 augustss error = uiomove(sc->sc_obuf, size, uio);
485 1.112 christos #ifdef UHID_DEBUG
486 1.112 christos if (uhiddebug > 5) {
487 1.112 christos uint32_t i;
488 1.112 christos
489 1.112 christos DPRINTF(("%s: outdata[%d] =", device_xname(sc->sc_hdev.sc_dev),
490 1.112 christos error));
491 1.112 christos for (i = 0; i < size; i++)
492 1.112 christos DPRINTF((" %02x", sc->sc_obuf[i]));
493 1.112 christos DPRINTF(("\n"));
494 1.112 christos }
495 1.112 christos #endif
496 1.18 augustss if (!error) {
497 1.112 christos if (sc->sc_raw)
498 1.112 christos err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf,
499 1.112 christos size);
500 1.112 christos else
501 1.112 christos err = uhidev_set_report(&sc->sc_hdev,
502 1.112 christos UHID_OUTPUT_REPORT, sc->sc_obuf, size);
503 1.112 christos if (err) {
504 1.112 christos DPRINTF(("%s: err = %d\n",
505 1.112 christos device_xname(sc->sc_hdev.sc_dev), err));
506 1.1 augustss error = EIO;
507 1.112 christos }
508 1.1 augustss }
509 1.18 augustss
510 1.93 mrg return error;
511 1.1 augustss }
512 1.1 augustss
513 1.120 riastrad static int
514 1.120 riastrad uhidioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
515 1.1 augustss {
516 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
517 1.1 augustss struct usb_ctl_report_desc *rd;
518 1.2 augustss struct usb_ctl_report *re;
519 1.47 augustss u_char buffer[UHID_CHUNK];
520 1.47 augustss int size, extra;
521 1.27 augustss usbd_status err;
522 1.47 augustss void *desc;
523 1.1 augustss
524 1.18 augustss DPRINTFN(2, ("uhidioctl: cmd=%lx\n", cmd));
525 1.18 augustss
526 1.1 augustss switch (cmd) {
527 1.2 augustss case FIONBIO:
528 1.2 augustss /* All handled in the upper FS layer. */
529 1.37 augustss break;
530 1.37 augustss
531 1.37 augustss case FIOASYNC:
532 1.114 ad mutex_enter(&proc_lock);
533 1.37 augustss if (*(int *)addr) {
534 1.111 maxv if (sc->sc_async != NULL) {
535 1.114 ad mutex_exit(&proc_lock);
536 1.93 mrg return EBUSY;
537 1.111 maxv }
538 1.116 riastrad atomic_store_relaxed(&sc->sc_async, l->l_proc);
539 1.68 christos DPRINTF(("uhid_do_ioctl: FIOASYNC %p\n", l->l_proc));
540 1.37 augustss } else
541 1.116 riastrad atomic_store_relaxed(&sc->sc_async, NULL);
542 1.114 ad mutex_exit(&proc_lock);
543 1.37 augustss break;
544 1.37 augustss
545 1.37 augustss /* XXX this is not the most general solution. */
546 1.37 augustss case TIOCSPGRP:
547 1.114 ad mutex_enter(&proc_lock);
548 1.80 ad if (sc->sc_async == NULL) {
549 1.114 ad mutex_exit(&proc_lock);
550 1.93 mrg return EINVAL;
551 1.80 ad }
552 1.80 ad if (*(int *)addr != sc->sc_async->p_pgid) {
553 1.114 ad mutex_exit(&proc_lock);
554 1.93 mrg return EPERM;
555 1.80 ad }
556 1.114 ad mutex_exit(&proc_lock);
557 1.60 jdolecek break;
558 1.60 jdolecek
559 1.60 jdolecek case FIOSETOWN:
560 1.114 ad mutex_enter(&proc_lock);
561 1.80 ad if (sc->sc_async == NULL) {
562 1.114 ad mutex_exit(&proc_lock);
563 1.93 mrg return EINVAL;
564 1.80 ad }
565 1.60 jdolecek if (-*(int *)addr != sc->sc_async->p_pgid
566 1.80 ad && *(int *)addr != sc->sc_async->p_pid) {
567 1.114 ad mutex_exit(&proc_lock);
568 1.93 mrg return EPERM;
569 1.80 ad }
570 1.114 ad mutex_exit(&proc_lock);
571 1.2 augustss break;
572 1.2 augustss
573 1.113 christos case USB_HID_GET_RAW:
574 1.113 christos *(int *)addr = sc->sc_raw;
575 1.113 christos break;
576 1.113 christos
577 1.113 christos case USB_HID_SET_RAW:
578 1.113 christos sc->sc_raw = *(int *)addr;
579 1.113 christos break;
580 1.113 christos
581 1.1 augustss case USB_GET_REPORT_DESC:
582 1.47 augustss uhidev_get_report_desc(sc->sc_hdev.sc_parent, &desc, &size);
583 1.1 augustss rd = (struct usb_ctl_report_desc *)addr;
584 1.102 riastrad size = uimin(size, sizeof(rd->ucrd_data));
585 1.50 christos rd->ucrd_size = size;
586 1.50 christos memcpy(rd->ucrd_data, desc, size);
587 1.1 augustss break;
588 1.2 augustss
589 1.2 augustss case USB_SET_IMMED:
590 1.9 augustss if (*(int *)addr) {
591 1.47 augustss extra = sc->sc_hdev.sc_report_id != 0;
592 1.111 maxv if (sc->sc_isize + extra > sizeof(buffer))
593 1.111 maxv return ENOBUFS;
594 1.47 augustss err = uhidev_get_report(&sc->sc_hdev, UHID_INPUT_REPORT,
595 1.47 augustss buffer, sc->sc_isize + extra);
596 1.27 augustss if (err)
597 1.93 mrg return EOPNOTSUPP;
598 1.12 augustss
599 1.115 riastrad atomic_or_32(&sc->sc_state, UHID_IMMED);
600 1.9 augustss } else
601 1.115 riastrad atomic_and_32(&sc->sc_state, ~UHID_IMMED);
602 1.2 augustss break;
603 1.2 augustss
604 1.2 augustss case USB_GET_REPORT:
605 1.2 augustss re = (struct usb_ctl_report *)addr;
606 1.50 christos switch (re->ucr_report) {
607 1.2 augustss case UHID_INPUT_REPORT:
608 1.2 augustss size = sc->sc_isize;
609 1.2 augustss break;
610 1.2 augustss case UHID_OUTPUT_REPORT:
611 1.2 augustss size = sc->sc_osize;
612 1.2 augustss break;
613 1.2 augustss case UHID_FEATURE_REPORT:
614 1.2 augustss size = sc->sc_fsize;
615 1.2 augustss break;
616 1.2 augustss default:
617 1.93 mrg return EINVAL;
618 1.2 augustss }
619 1.47 augustss extra = sc->sc_hdev.sc_report_id != 0;
620 1.111 maxv if (size + extra > sizeof(re->ucr_data))
621 1.111 maxv return ENOBUFS;
622 1.50 christos err = uhidev_get_report(&sc->sc_hdev, re->ucr_report,
623 1.50 christos re->ucr_data, size + extra);
624 1.47 augustss if (extra)
625 1.99 maya memmove(re->ucr_data, re->ucr_data+1, size);
626 1.35 augustss if (err)
627 1.93 mrg return EIO;
628 1.35 augustss break;
629 1.35 augustss
630 1.35 augustss case USB_SET_REPORT:
631 1.35 augustss re = (struct usb_ctl_report *)addr;
632 1.50 christos switch (re->ucr_report) {
633 1.35 augustss case UHID_INPUT_REPORT:
634 1.35 augustss size = sc->sc_isize;
635 1.35 augustss break;
636 1.35 augustss case UHID_OUTPUT_REPORT:
637 1.35 augustss size = sc->sc_osize;
638 1.35 augustss break;
639 1.35 augustss case UHID_FEATURE_REPORT:
640 1.35 augustss size = sc->sc_fsize;
641 1.35 augustss break;
642 1.35 augustss default:
643 1.93 mrg return EINVAL;
644 1.35 augustss }
645 1.111 maxv if (size > sizeof(re->ucr_data))
646 1.111 maxv return ENOBUFS;
647 1.50 christos err = uhidev_set_report(&sc->sc_hdev, re->ucr_report,
648 1.50 christos re->ucr_data, size);
649 1.27 augustss if (err)
650 1.93 mrg return EIO;
651 1.2 augustss break;
652 1.2 augustss
653 1.47 augustss case USB_GET_REPORT_ID:
654 1.47 augustss *(int *)addr = sc->sc_hdev.sc_report_id;
655 1.47 augustss break;
656 1.47 augustss
657 1.87 erh case USB_GET_DEVICE_DESC:
658 1.87 erh *(usb_device_descriptor_t *)addr =
659 1.87 erh *usbd_get_device_descriptor(sc->sc_hdev.sc_parent->sc_udev);
660 1.87 erh break;
661 1.87 erh
662 1.61 jdolecek case USB_GET_DEVICEINFO:
663 1.61 jdolecek usbd_fill_deviceinfo(sc->sc_hdev.sc_parent->sc_udev,
664 1.95 skrll (struct usb_device_info *)addr, 0);
665 1.61 jdolecek break;
666 1.73 pavel case USB_GET_DEVICEINFO_OLD:
667 1.106 pgoyette MODULE_HOOK_CALL(usb_subr_fill_30_hook,
668 1.103 pgoyette (sc->sc_hdev.sc_parent->sc_udev,
669 1.103 pgoyette (struct usb_device_info_old *)addr, 0,
670 1.103 pgoyette usbd_devinfo_vp, usbd_printBCD),
671 1.103 pgoyette enosys(), err);
672 1.103 pgoyette if (err == 0)
673 1.103 pgoyette return 0;
674 1.73 pavel break;
675 1.95 skrll case USB_GET_STRING_DESC:
676 1.61 jdolecek {
677 1.95 skrll struct usb_string_desc *si = (struct usb_string_desc *)addr;
678 1.95 skrll err = usbd_get_string_desc(sc->sc_hdev.sc_parent->sc_udev,
679 1.61 jdolecek si->usd_string_index,
680 1.95 skrll si->usd_language_id, &si->usd_desc, &size);
681 1.95 skrll if (err)
682 1.95 skrll return EINVAL;
683 1.95 skrll break;
684 1.61 jdolecek }
685 1.61 jdolecek
686 1.1 augustss default:
687 1.93 mrg return EINVAL;
688 1.1 augustss }
689 1.93 mrg return 0;
690 1.1 augustss }
691 1.1 augustss
692 1.109 maxv static int
693 1.68 christos uhidpoll(dev_t dev, int events, struct lwp *l)
694 1.1 augustss {
695 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
696 1.1 augustss int revents = 0;
697 1.25 augustss
698 1.88 mrg mutex_enter(&sc->sc_lock);
699 1.1 augustss if (events & (POLLOUT | POLLWRNORM))
700 1.1 augustss revents |= events & (POLLOUT | POLLWRNORM);
701 1.4 veego if (events & (POLLIN | POLLRDNORM)) {
702 1.1 augustss if (sc->sc_q.c_cc > 0)
703 1.1 augustss revents |= events & (POLLIN | POLLRDNORM);
704 1.1 augustss else
705 1.68 christos selrecord(l, &sc->sc_rsel);
706 1.4 veego }
707 1.88 mrg mutex_exit(&sc->sc_lock);
708 1.1 augustss
709 1.93 mrg return revents;
710 1.55 jdolecek }
711 1.55 jdolecek
712 1.55 jdolecek static void
713 1.55 jdolecek filt_uhidrdetach(struct knote *kn)
714 1.55 jdolecek {
715 1.55 jdolecek struct uhid_softc *sc = kn->kn_hook;
716 1.55 jdolecek
717 1.88 mrg mutex_enter(&sc->sc_lock);
718 1.117 thorpej selremove_knote(&sc->sc_rsel, kn);
719 1.88 mrg mutex_exit(&sc->sc_lock);
720 1.55 jdolecek }
721 1.55 jdolecek
722 1.55 jdolecek static int
723 1.72 christos filt_uhidread(struct knote *kn, long hint)
724 1.55 jdolecek {
725 1.55 jdolecek struct uhid_softc *sc = kn->kn_hook;
726 1.55 jdolecek
727 1.115 riastrad if (hint == NOTE_SUBMIT)
728 1.115 riastrad KASSERT(mutex_owned(&sc->sc_lock));
729 1.115 riastrad else
730 1.115 riastrad mutex_enter(&sc->sc_lock);
731 1.115 riastrad
732 1.55 jdolecek kn->kn_data = sc->sc_q.c_cc;
733 1.115 riastrad
734 1.115 riastrad if (hint == NOTE_SUBMIT)
735 1.115 riastrad KASSERT(mutex_owned(&sc->sc_lock));
736 1.115 riastrad else
737 1.115 riastrad mutex_exit(&sc->sc_lock);
738 1.115 riastrad
739 1.95 skrll return kn->kn_data > 0;
740 1.55 jdolecek }
741 1.55 jdolecek
742 1.100 maya static const struct filterops uhidread_filtops = {
743 1.118 thorpej .f_flags = FILTEROP_ISFD,
744 1.100 maya .f_attach = NULL,
745 1.100 maya .f_detach = filt_uhidrdetach,
746 1.100 maya .f_event = filt_uhidread,
747 1.100 maya };
748 1.55 jdolecek
749 1.109 maxv static int
750 1.55 jdolecek uhidkqfilter(dev_t dev, struct knote *kn)
751 1.55 jdolecek {
752 1.120 riastrad struct uhid_softc *sc = device_lookup_private(&uhid_cd, UHIDUNIT(dev));
753 1.115 riastrad int error;
754 1.55 jdolecek
755 1.55 jdolecek switch (kn->kn_filter) {
756 1.55 jdolecek case EVFILT_READ:
757 1.55 jdolecek kn->kn_fop = &uhidread_filtops;
758 1.119 thorpej kn->kn_hook = sc;
759 1.119 thorpej mutex_enter(&sc->sc_lock);
760 1.119 thorpej selrecord_knote(&sc->sc_rsel, kn);
761 1.119 thorpej mutex_exit(&sc->sc_lock);
762 1.55 jdolecek break;
763 1.119 thorpej
764 1.55 jdolecek case EVFILT_WRITE:
765 1.119 thorpej kn->kn_fop = &seltrue_filtops;
766 1.55 jdolecek break;
767 1.119 thorpej
768 1.55 jdolecek default:
769 1.115 riastrad error = EINVAL;
770 1.120 riastrad break;
771 1.55 jdolecek }
772 1.55 jdolecek
773 1.115 riastrad return error;
774 1.1 augustss }
775