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