1 /* $NetBSD: emdtv_ir.c,v 1.6 2022/06/26 22:49:09 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.6 2022/06/26 22:49:09 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 mutex_init(&sc->sc_ir_mutex, MUTEX_DEFAULT, IPL_VM); 79 80 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, 0); 81 if (ed == NULL) 82 return; 83 84 err = workqueue_create(&sc->sc_ir_wq, "emdtvir", 85 emdtv_ir_worker, sc, PRI_NONE, IPL_VM, 0); 86 if (err) { 87 aprint_error_dev(sc->sc_dev, "couldn't create workqueue: %d\n", 88 err); 89 return; 90 } 91 92 status = usbd_open_pipe_intr(sc->sc_iface, ed->bEndpointAddress, 93 USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe, sc, &sc->sc_intr_buf, 1, 94 emdtv_ir_intr, USBD_DEFAULT_INTERVAL); 95 if (status != USBD_NORMAL_COMPLETION) { 96 aprint_error_dev(sc->sc_dev, "couldn't open intr pipe: %s\n", 97 usbd_errstr(status)); 98 return; 99 } 100 101 ia.ia_type = IR_TYPE_CIR; 102 ia.ia_methods = &emdtv_ir_methods; 103 ia.ia_handle = sc; 104 105 sc->sc_cirdev = 106 config_found(sc->sc_dev, &ia, ir_print, 107 CFARGS(.iattr = "irbus")); 108 } 109 110 void 111 emdtv_ir_detach(struct emdtv_softc *sc, int flags) 112 { 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 if (sc->sc_ir_wq != NULL) 121 workqueue_destroy(sc->sc_ir_wq); 122 123 mutex_destroy(&sc->sc_ir_mutex); 124 } 125 126 static void 127 emdtv_ir_intr(struct usbd_xfer *xfer, void * priv, 128 usbd_status status) 129 { 130 struct emdtv_softc *sc = priv; 131 uint32_t len; 132 133 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 134 if (status == USBD_CANCELLED) 135 return; 136 137 if (sc->sc_ir_wq) 138 workqueue_enqueue(sc->sc_ir_wq, &sc->sc_ir_work, NULL); 139 } 140 141 static void 142 emdtv_ir_worker(struct work *wk, void *opaque) 143 { 144 struct emdtv_softc *sc = opaque; 145 struct cir_softc *csc; 146 uint8_t evt[3]; 147 int pos; 148 149 if (sc->sc_cirdev == NULL || sc->sc_dying == true || 150 sc->sc_ir_open == false) 151 return; 152 153 emdtv_read_multi_1(sc, UR_GET_STATUS, EM28XX_REG_IR, evt, sizeof(evt)); 154 155 csc = device_private(sc->sc_cirdev); 156 157 mutex_enter(&sc->sc_ir_mutex); 158 pos = (sc->sc_ir_ptr + sc->sc_ir_cnt) % EMDTV_CIR_BUFLEN; 159 memcpy(&sc->sc_ir_queue[pos], evt, sizeof(evt)); 160 if (sc->sc_ir_cnt < EMDTV_CIR_BUFLEN - 1) { 161 ++sc->sc_ir_cnt; 162 ++csc->sc_rdframes; 163 } 164 selnotify(&csc->sc_rdsel, 0, 1); 165 mutex_exit(&sc->sc_ir_mutex); 166 } 167 168 /* 169 * cir(4) 170 */ 171 static int 172 emdtv_ir_open(void *opaque, int flag, int mode, struct proc *p) 173 { 174 struct emdtv_softc *sc = opaque; 175 176 if (sc->sc_ir_open == true) 177 return EBUSY; 178 179 sc->sc_ir_cnt = 0; 180 sc->sc_ir_ptr = EMDTV_CIR_BUFLEN - 1; 181 sc->sc_ir_open = true; 182 183 return 0; 184 } 185 186 static int 187 emdtv_ir_close(void *opaque, int flag, int mode, struct proc *p) 188 { 189 struct emdtv_softc *sc = opaque; 190 191 sc->sc_ir_open = false; 192 193 return 0; 194 } 195 196 static int 197 emdtv_ir_read(void *opaque, struct uio *uio, int flag) 198 { 199 struct emdtv_softc *sc = opaque; 200 struct cir_softc *csc; 201 int error = 0; 202 203 if (uio->uio_resid != 3) /* 3 byte protocol */ 204 return EINVAL; 205 206 if (sc->sc_dying) 207 return EIO; 208 209 csc = device_private(sc->sc_cirdev); 210 211 mutex_enter(&sc->sc_ir_mutex); 212 if (sc->sc_ir_cnt == 0) 213 goto out; 214 215 error = uiomove(&sc->sc_ir_queue[sc->sc_ir_ptr], 3, uio); 216 sc->sc_ir_ptr++; 217 if (sc->sc_ir_ptr == EMDTV_CIR_BUFLEN) 218 sc->sc_ir_ptr = 0; 219 --sc->sc_ir_cnt; 220 --csc->sc_rdframes; 221 KASSERT(sc->sc_ir_cnt >= 0); 222 223 out: 224 mutex_exit(&sc->sc_ir_mutex); 225 return error; 226 } 227 228 static int 229 emdtv_ir_write(void *opaque, struct uio *uio, int flag) 230 { 231 return EINVAL; 232 } 233 234 static int 235 emdtv_ir_setparams(void *opaque, struct cir_params *cp) 236 { 237 return 0; 238 } 239