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