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