xref: /netbsd-src/sys/dev/usb/emdtv_ir.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /* $NetBSD: emdtv_ir.c,v 1.4 2021/08/07 16:19:16 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2008 Jared D. McNeill <jmcneill@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.4 2021/08/07 16:19:16 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 		CFARGS(.iattr = "irbus"));
106 }
107 
108 void
109 emdtv_ir_detach(struct emdtv_softc *sc, int flags)
110 {
111 	if (sc->sc_ir_wq != NULL)
112 		workqueue_destroy(sc->sc_ir_wq);
113 
114 	if (sc->sc_intr_pipe != NULL) {
115 		usbd_abort_pipe(sc->sc_intr_pipe);
116 		usbd_close_pipe(sc->sc_intr_pipe);
117 		sc->sc_intr_pipe = NULL;
118 	}
119 
120 	mutex_enter(&sc->sc_ir_mutex);
121 	mutex_exit(&sc->sc_ir_mutex);
122 	mutex_destroy(&sc->sc_ir_mutex);
123 
124 	if (sc->sc_cirdev != NULL)
125 		config_detach(sc->sc_cirdev, flags);
126 }
127 
128 static void
129 emdtv_ir_intr(struct usbd_xfer *xfer, void * priv,
130     usbd_status status)
131 {
132 	struct emdtv_softc *sc = priv;
133 	uint32_t len;
134 
135 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
136 	if (status == USBD_CANCELLED)
137 		return;
138 
139 	if (sc->sc_ir_wq)
140 		workqueue_enqueue(sc->sc_ir_wq, &sc->sc_ir_work, NULL);
141 }
142 
143 static void
144 emdtv_ir_worker(struct work *wk, void *opaque)
145 {
146 	struct emdtv_softc *sc = opaque;
147 	struct cir_softc *csc;
148 	uint8_t evt[3];
149 	int pos;
150 
151 	if (sc->sc_cirdev == NULL || sc->sc_dying == true ||
152 	    sc->sc_ir_open == false)
153 		return;
154 
155 	emdtv_read_multi_1(sc, UR_GET_STATUS, EM28XX_REG_IR, evt, sizeof(evt));
156 
157 	csc = device_private(sc->sc_cirdev);
158 
159 	mutex_enter(&sc->sc_ir_mutex);
160 	pos = (sc->sc_ir_ptr + sc->sc_ir_cnt) % EMDTV_CIR_BUFLEN;
161 	memcpy(&sc->sc_ir_queue[pos], evt, sizeof(evt));
162 	if (sc->sc_ir_cnt < EMDTV_CIR_BUFLEN - 1) {
163 		++sc->sc_ir_cnt;
164 		++csc->sc_rdframes;
165 	}
166 	selnotify(&csc->sc_rdsel, 0, 1);
167 	mutex_exit(&sc->sc_ir_mutex);
168 }
169 
170 /*
171  * cir(4)
172  */
173 static int
174 emdtv_ir_open(void *opaque, int flag, int mode, struct proc *p)
175 {
176 	struct emdtv_softc *sc = opaque;
177 
178 	if (sc->sc_ir_open == true)
179 		return EBUSY;
180 
181 	sc->sc_ir_cnt = 0;
182 	sc->sc_ir_ptr = EMDTV_CIR_BUFLEN - 1;
183 	sc->sc_ir_open = true;
184 
185 	return 0;
186 }
187 
188 static int
189 emdtv_ir_close(void *opaque, int flag, int mode, struct proc *p)
190 {
191 	struct emdtv_softc *sc = opaque;
192 
193 	sc->sc_ir_open = false;
194 
195 	return 0;
196 }
197 
198 static int
199 emdtv_ir_read(void *opaque, struct uio *uio, int flag)
200 {
201 	struct emdtv_softc *sc = opaque;
202 	struct cir_softc *csc;
203 	int error = 0;
204 
205 	if (uio->uio_resid != 3)	/* 3 byte protocol */
206 		return EINVAL;
207 
208 	if (sc->sc_dying)
209 		return EIO;
210 
211 	csc = device_private(sc->sc_cirdev);
212 
213 	mutex_enter(&sc->sc_ir_mutex);
214 	if (sc->sc_ir_cnt == 0)
215 		goto out;
216 
217 	error = uiomove(&sc->sc_ir_queue[sc->sc_ir_ptr], 3, uio);
218 	sc->sc_ir_ptr++;
219 	if (sc->sc_ir_ptr == EMDTV_CIR_BUFLEN)
220 		sc->sc_ir_ptr = 0;
221 	--sc->sc_ir_cnt;
222 	--csc->sc_rdframes;
223 	KASSERT(sc->sc_ir_cnt >= 0);
224 
225 out:
226 	mutex_exit(&sc->sc_ir_mutex);
227 	return error;
228 }
229 
230 static int
231 emdtv_ir_write(void *opaque, struct uio *uio, int flag)
232 {
233 	return EINVAL;
234 }
235 
236 static int
237 emdtv_ir_setparams(void *opaque, struct cir_params *cp)
238 {
239 	return 0;
240 }
241