emdtv_ir.c revision 1.3 1 /* $NetBSD: emdtv_ir.c,v 1.3 2021/04/24 23:36:59 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2008 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: emdtv_ir.c,v 1.3 2021/04/24 23:36:59 thorpej Exp $");
31
32 #include <sys/select.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/conf.h>
37 #include <sys/bus.h>
38
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 #include <dev/usb/usbdi_util.h>
42 #include <dev/usb/usbdivar.h>
43 #include <dev/usb/usbdevs.h>
44
45 #include <dev/usb/emdtvvar.h>
46 #include <dev/usb/emdtvreg.h>
47
48 #include <dev/ir/ir.h>
49 #include <dev/ir/cirio.h>
50 #include <dev/ir/cirvar.h>
51
52 static void emdtv_ir_intr(struct usbd_xfer *, void *,
53 usbd_status);
54 static void emdtv_ir_worker(struct work *, void *);
55
56 static int emdtv_ir_open(void *, int, int, struct proc *);
57 static int emdtv_ir_close(void *, int, int, struct proc *);
58 static int emdtv_ir_read(void *, struct uio *, int);
59 static int emdtv_ir_write(void *, struct uio *, int);
60 static int emdtv_ir_setparams(void *, struct cir_params *);
61
62 static const struct cir_methods emdtv_ir_methods = {
63 .im_open = emdtv_ir_open,
64 .im_close = emdtv_ir_close,
65 .im_read = emdtv_ir_read,
66 .im_write = emdtv_ir_write,
67 .im_setparams = emdtv_ir_setparams,
68 };
69
70 void
71 emdtv_ir_attach(struct emdtv_softc *sc)
72 {
73 struct ir_attach_args ia;
74 usb_endpoint_descriptor_t *ed;
75 usbd_status status;
76 int err;
77
78 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, 0);
79 if (ed == NULL)
80 return;
81
82 status = usbd_open_pipe_intr(sc->sc_iface, ed->bEndpointAddress,
83 USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe, sc, &sc->sc_intr_buf, 1,
84 emdtv_ir_intr, USBD_DEFAULT_INTERVAL);
85 if (status != USBD_NORMAL_COMPLETION) {
86 aprint_error_dev(sc->sc_dev, "couldn't open intr pipe: %s\n",
87 usbd_errstr(status));
88 return;
89 }
90
91 mutex_init(&sc->sc_ir_mutex, MUTEX_DEFAULT, IPL_VM);
92
93 err = workqueue_create(&sc->sc_ir_wq, "emdtvir",
94 emdtv_ir_worker, sc, PRI_NONE, IPL_VM, 0);
95 if (err)
96 aprint_error_dev(sc->sc_dev, "couldn't create workqueue: %d\n",
97 err);
98
99 ia.ia_type = IR_TYPE_CIR;
100 ia.ia_methods = &emdtv_ir_methods;
101 ia.ia_handle = sc;
102
103 sc->sc_cirdev =
104 config_found(sc->sc_dev, &ia, ir_print,
105 CFARG_IATTR, "irbus",
106 CFARG_EOL);
107 }
108
109 void
110 emdtv_ir_detach(struct emdtv_softc *sc, int flags)
111 {
112 if (sc->sc_ir_wq != NULL)
113 workqueue_destroy(sc->sc_ir_wq);
114
115 if (sc->sc_intr_pipe != NULL) {
116 usbd_abort_pipe(sc->sc_intr_pipe);
117 usbd_close_pipe(sc->sc_intr_pipe);
118 sc->sc_intr_pipe = NULL;
119 }
120
121 mutex_enter(&sc->sc_ir_mutex);
122 mutex_exit(&sc->sc_ir_mutex);
123 mutex_destroy(&sc->sc_ir_mutex);
124
125 if (sc->sc_cirdev != NULL)
126 config_detach(sc->sc_cirdev, flags);
127 }
128
129 static void
130 emdtv_ir_intr(struct usbd_xfer *xfer, void * priv,
131 usbd_status status)
132 {
133 struct emdtv_softc *sc = priv;
134 uint32_t len;
135
136 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
137 if (status == USBD_CANCELLED)
138 return;
139
140 if (sc->sc_ir_wq)
141 workqueue_enqueue(sc->sc_ir_wq, &sc->sc_ir_work, NULL);
142 }
143
144 static void
145 emdtv_ir_worker(struct work *wk, void *opaque)
146 {
147 struct emdtv_softc *sc = opaque;
148 struct cir_softc *csc;
149 uint8_t evt[3];
150 int pos;
151
152 if (sc->sc_cirdev == NULL || sc->sc_dying == true ||
153 sc->sc_ir_open == false)
154 return;
155
156 emdtv_read_multi_1(sc, UR_GET_STATUS, EM28XX_REG_IR, evt, sizeof(evt));
157
158 csc = device_private(sc->sc_cirdev);
159
160 mutex_enter(&sc->sc_ir_mutex);
161 pos = (sc->sc_ir_ptr + sc->sc_ir_cnt) % EMDTV_CIR_BUFLEN;
162 memcpy(&sc->sc_ir_queue[pos], evt, sizeof(evt));
163 if (sc->sc_ir_cnt < EMDTV_CIR_BUFLEN - 1) {
164 ++sc->sc_ir_cnt;
165 ++csc->sc_rdframes;
166 }
167 selnotify(&csc->sc_rdsel, 0, 1);
168 mutex_exit(&sc->sc_ir_mutex);
169 }
170
171 /*
172 * cir(4)
173 */
174 static int
175 emdtv_ir_open(void *opaque, int flag, int mode, struct proc *p)
176 {
177 struct emdtv_softc *sc = opaque;
178
179 if (sc->sc_ir_open == true)
180 return EBUSY;
181
182 sc->sc_ir_cnt = 0;
183 sc->sc_ir_ptr = EMDTV_CIR_BUFLEN - 1;
184 sc->sc_ir_open = true;
185
186 return 0;
187 }
188
189 static int
190 emdtv_ir_close(void *opaque, int flag, int mode, struct proc *p)
191 {
192 struct emdtv_softc *sc = opaque;
193
194 sc->sc_ir_open = false;
195
196 return 0;
197 }
198
199 static int
200 emdtv_ir_read(void *opaque, struct uio *uio, int flag)
201 {
202 struct emdtv_softc *sc = opaque;
203 struct cir_softc *csc;
204 int error = 0;
205
206 if (uio->uio_resid != 3) /* 3 byte protocol */
207 return EINVAL;
208
209 if (sc->sc_dying)
210 return EIO;
211
212 csc = device_private(sc->sc_cirdev);
213
214 mutex_enter(&sc->sc_ir_mutex);
215 if (sc->sc_ir_cnt == 0)
216 goto out;
217
218 error = uiomove(&sc->sc_ir_queue[sc->sc_ir_ptr], 3, uio);
219 sc->sc_ir_ptr++;
220 if (sc->sc_ir_ptr == EMDTV_CIR_BUFLEN)
221 sc->sc_ir_ptr = 0;
222 --sc->sc_ir_cnt;
223 --csc->sc_rdframes;
224 KASSERT(sc->sc_ir_cnt >= 0);
225
226 out:
227 mutex_exit(&sc->sc_ir_mutex);
228 return error;
229 }
230
231 static int
232 emdtv_ir_write(void *opaque, struct uio *uio, int flag)
233 {
234 return EINVAL;
235 }
236
237 static int
238 emdtv_ir_setparams(void *opaque, struct cir_params *cp)
239 {
240 return 0;
241 }
242