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