1 /* $OpenBSD: ucc.c,v 1.37 2022/11/14 00:16:46 deraadt Exp $ */
2
3 /*
4 * Copyright (c) 2021 Anton Lindqvist <anton@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/device.h>
22
23 #include <dev/usb/usb.h>
24 #include <dev/usb/usbhid.h>
25 #include <dev/usb/usbdi.h>
26 #include <dev/usb/uhidev.h>
27
28 #include <dev/hid/hidccvar.h>
29
30 struct ucc_softc {
31 struct uhidev sc_hdev;
32 struct hidcc *sc_cc;
33 };
34
35 int ucc_match(struct device *, void *, void *);
36 void ucc_attach(struct device *, struct device *, void *);
37 int ucc_detach(struct device *, int);
38 void ucc_intr(struct uhidev *, void *, u_int);
39
40 int ucc_enable(void *, int);
41
42 struct cfdriver ucc_cd = {
43 NULL, "ucc", DV_DULL
44 };
45
46 const struct cfattach ucc_ca = {
47 sizeof(struct ucc_softc),
48 ucc_match,
49 ucc_attach,
50 ucc_detach,
51 };
52
53 int
ucc_match(struct device * parent,void * match,void * aux)54 ucc_match(struct device *parent, void *match, void *aux)
55 {
56 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
57 void *desc;
58 int size;
59
60 if (UHIDEV_CLAIM_MULTIPLE_REPORTID(uha))
61 return UMATCH_NONE;
62
63 uhidev_get_report_desc(uha->parent, &desc, &size);
64 if (hid_report_size(desc, size, hid_input, uha->reportid) == 0)
65 return UMATCH_NONE;
66 if (!hidcc_match(desc, size, uha->reportid))
67 return UMATCH_NONE;
68
69 return UMATCH_IFACECLASS;
70 }
71
72 void
ucc_attach(struct device * parent,struct device * self,void * aux)73 ucc_attach(struct device *parent, struct device *self, void *aux)
74 {
75 struct ucc_softc *sc = (struct ucc_softc *)self;
76 struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)aux;
77 void *desc;
78 int repid, size;
79
80 sc->sc_hdev.sc_intr = ucc_intr;
81 sc->sc_hdev.sc_parent = uha->parent;
82 sc->sc_hdev.sc_udev = uha->uaa->device;
83 sc->sc_hdev.sc_report_id = uha->reportid;
84
85 uhidev_get_report_desc(uha->parent, &desc, &size);
86 repid = uha->reportid;
87 sc->sc_hdev.sc_isize = hid_report_size(desc, size, hid_input, repid);
88 sc->sc_hdev.sc_osize = hid_report_size(desc, size, hid_output, repid);
89 sc->sc_hdev.sc_fsize = hid_report_size(desc, size, hid_feature, repid);
90
91 struct hidcc_attach_arg hca = {
92 .device = &sc->sc_hdev.sc_dev,
93 .audio_cookie = uha->uaa->cookie,
94 .desc = desc,
95 .descsiz = size,
96 .repid = repid,
97 .isize = sc->sc_hdev.sc_isize,
98 .enable = ucc_enable,
99 .arg = self,
100 };
101 sc->sc_cc = hidcc_attach(&hca);
102 }
103
104 int
ucc_detach(struct device * self,int flags)105 ucc_detach(struct device *self, int flags)
106 {
107 struct ucc_softc *sc = (struct ucc_softc *)self;
108 int error = 0;
109
110 uhidev_close(&sc->sc_hdev);
111 if (sc->sc_cc != NULL)
112 error = hidcc_detach(sc->sc_cc, flags);
113 return error;
114 }
115
116 void
ucc_intr(struct uhidev * addr,void * data,u_int len)117 ucc_intr(struct uhidev *addr, void *data, u_int len)
118 {
119 struct ucc_softc *sc = (struct ucc_softc *)addr;
120
121 if (sc->sc_cc != NULL)
122 hidcc_intr(sc->sc_cc, data, len);
123 }
124
125 int
ucc_enable(void * v,int on)126 ucc_enable(void *v, int on)
127 {
128 struct ucc_softc *sc = (struct ucc_softc *)v;
129 int error = 0;
130
131 if (on)
132 error = uhidev_open(&sc->sc_hdev);
133 else
134 uhidev_close(&sc->sc_hdev);
135 return error;
136 }
137