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