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