1*82b8cabaSriastradh /* $NetBSD: hdmicec.c,v 1.2 2017/10/28 04:53:56 riastradh Exp $ */
272fc9317Sjmcneill
372fc9317Sjmcneill /*-
472fc9317Sjmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
572fc9317Sjmcneill * All rights reserved.
672fc9317Sjmcneill *
772fc9317Sjmcneill * Redistribution and use in source and binary forms, with or without
872fc9317Sjmcneill * modification, are permitted provided that the following conditions
972fc9317Sjmcneill * are met:
1072fc9317Sjmcneill * 1. Redistributions of source code must retain the above copyright
1172fc9317Sjmcneill * notice, this list of conditions and the following disclaimer.
1272fc9317Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1372fc9317Sjmcneill * notice, this list of conditions and the following disclaimer in the
1472fc9317Sjmcneill * documentation and/or other materials provided with the distribution.
1572fc9317Sjmcneill *
1672fc9317Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1772fc9317Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1872fc9317Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1972fc9317Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2072fc9317Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2172fc9317Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2272fc9317Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2372fc9317Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2472fc9317Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2572fc9317Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2672fc9317Sjmcneill * SUCH DAMAGE.
2772fc9317Sjmcneill */
2872fc9317Sjmcneill
2972fc9317Sjmcneill #include <sys/cdefs.h>
30*82b8cabaSriastradh __KERNEL_RCSID(0, "$NetBSD: hdmicec.c,v 1.2 2017/10/28 04:53:56 riastradh Exp $");
3172fc9317Sjmcneill
3272fc9317Sjmcneill #include <sys/param.h>
3372fc9317Sjmcneill #include <sys/types.h>
3472fc9317Sjmcneill #include <sys/device.h>
3572fc9317Sjmcneill #include <sys/conf.h>
3672fc9317Sjmcneill #include <sys/ioctl.h>
3772fc9317Sjmcneill #include <sys/select.h>
3872fc9317Sjmcneill #include <sys/atomic.h>
3972fc9317Sjmcneill
4072fc9317Sjmcneill #include <dev/hdmicec/hdmicec_if.h>
4172fc9317Sjmcneill #include <dev/hdmicec/hdmicecio.h>
4272fc9317Sjmcneill
43*82b8cabaSriastradh #include "ioconf.h"
44*82b8cabaSriastradh
4572fc9317Sjmcneill #define CEC_MAX_FRAMESIZE 16
4672fc9317Sjmcneill
4772fc9317Sjmcneill struct hdmicec_softc {
4872fc9317Sjmcneill device_t sc_dev;
4972fc9317Sjmcneill
5072fc9317Sjmcneill void * sc_priv;
5172fc9317Sjmcneill const struct hdmicec_hw_if *sc_hwif;
5272fc9317Sjmcneill
5372fc9317Sjmcneill u_int sc_busy;
5472fc9317Sjmcneill };
5572fc9317Sjmcneill
5672fc9317Sjmcneill static dev_type_open(hdmicec_open);
5772fc9317Sjmcneill static dev_type_close(hdmicec_close);
5872fc9317Sjmcneill static dev_type_read(hdmicec_read);
5972fc9317Sjmcneill static dev_type_write(hdmicec_write);
6072fc9317Sjmcneill static dev_type_ioctl(hdmicec_ioctl);
6172fc9317Sjmcneill static dev_type_poll(hdmicec_poll);
6272fc9317Sjmcneill
6372fc9317Sjmcneill static int hdmicec_match(device_t, cfdata_t, void *);
6472fc9317Sjmcneill static void hdmicec_attach(device_t, device_t, void *);
6572fc9317Sjmcneill
6672fc9317Sjmcneill const struct cdevsw hdmicec_cdevsw = {
6772fc9317Sjmcneill .d_open = hdmicec_open,
6872fc9317Sjmcneill .d_close = hdmicec_close,
6972fc9317Sjmcneill .d_read = hdmicec_read,
7072fc9317Sjmcneill .d_write = hdmicec_write,
7172fc9317Sjmcneill .d_ioctl = hdmicec_ioctl,
7272fc9317Sjmcneill .d_stop = nostop,
7372fc9317Sjmcneill .d_tty = notty,
7472fc9317Sjmcneill .d_poll = hdmicec_poll,
7572fc9317Sjmcneill .d_mmap = nommap,
7672fc9317Sjmcneill .d_kqfilter = nokqfilter,
7772fc9317Sjmcneill .d_flag = D_MPSAFE
7872fc9317Sjmcneill };
7972fc9317Sjmcneill
8072fc9317Sjmcneill CFATTACH_DECL_NEW(hdmicec, sizeof(struct hdmicec_softc), hdmicec_match,
8172fc9317Sjmcneill hdmicec_attach, NULL, NULL);
8272fc9317Sjmcneill
8372fc9317Sjmcneill static int
hdmicec_match(device_t parent,cfdata_t match,void * opaque)8472fc9317Sjmcneill hdmicec_match(device_t parent, cfdata_t match, void *opaque)
8572fc9317Sjmcneill {
8672fc9317Sjmcneill return 1;
8772fc9317Sjmcneill }
8872fc9317Sjmcneill
8972fc9317Sjmcneill static void
hdmicec_attach(device_t parent,device_t self,void * opaque)9072fc9317Sjmcneill hdmicec_attach(device_t parent, device_t self, void *opaque)
9172fc9317Sjmcneill {
9272fc9317Sjmcneill struct hdmicec_softc *sc = device_private(self);
9372fc9317Sjmcneill struct hdmicec_attach_args *caa = opaque;
9472fc9317Sjmcneill
9572fc9317Sjmcneill sc->sc_dev = self;
9672fc9317Sjmcneill sc->sc_priv = caa->priv;
9772fc9317Sjmcneill sc->sc_hwif = caa->hwif;
9872fc9317Sjmcneill
9972fc9317Sjmcneill aprint_naive("\n");
10072fc9317Sjmcneill aprint_normal("\n");
10172fc9317Sjmcneill }
10272fc9317Sjmcneill
10372fc9317Sjmcneill static int
hdmicec_open(dev_t dev,int flag,int fmt,lwp_t * l)10472fc9317Sjmcneill hdmicec_open(dev_t dev, int flag, int fmt, lwp_t *l)
10572fc9317Sjmcneill {
10672fc9317Sjmcneill struct hdmicec_softc *sc =
10772fc9317Sjmcneill device_lookup_private(&hdmicec_cd, minor(dev));
10872fc9317Sjmcneill int error;
10972fc9317Sjmcneill
11072fc9317Sjmcneill if (sc == NULL)
11172fc9317Sjmcneill return ENXIO;
11272fc9317Sjmcneill
11372fc9317Sjmcneill if (atomic_cas_uint(&sc->sc_busy, 0, 1) != 1)
11472fc9317Sjmcneill return EBUSY;
11572fc9317Sjmcneill
11672fc9317Sjmcneill if (sc->sc_hwif->open != NULL) {
11772fc9317Sjmcneill error = sc->sc_hwif->open(sc->sc_priv, flag);
11872fc9317Sjmcneill if (error) {
11972fc9317Sjmcneill atomic_swap_uint(&sc->sc_busy, 0);
12072fc9317Sjmcneill return error;
12172fc9317Sjmcneill }
12272fc9317Sjmcneill }
12372fc9317Sjmcneill
12472fc9317Sjmcneill return 0;
12572fc9317Sjmcneill }
12672fc9317Sjmcneill
12772fc9317Sjmcneill static int
hdmicec_close(dev_t dev,int flag,int fmt,lwp_t * l)12872fc9317Sjmcneill hdmicec_close(dev_t dev, int flag, int fmt, lwp_t *l)
12972fc9317Sjmcneill {
13072fc9317Sjmcneill struct hdmicec_softc *sc =
13172fc9317Sjmcneill device_lookup_private(&hdmicec_cd, minor(dev));
13272fc9317Sjmcneill
13372fc9317Sjmcneill if (sc->sc_hwif->close)
13472fc9317Sjmcneill sc->sc_hwif->close(sc->sc_priv);
13572fc9317Sjmcneill
13672fc9317Sjmcneill atomic_swap_uint(&sc->sc_busy, 0);
13772fc9317Sjmcneill return 0;
13872fc9317Sjmcneill }
13972fc9317Sjmcneill
14072fc9317Sjmcneill static int
hdmicec_read(dev_t dev,struct uio * uio,int flags)14172fc9317Sjmcneill hdmicec_read(dev_t dev, struct uio *uio, int flags)
14272fc9317Sjmcneill {
14372fc9317Sjmcneill struct hdmicec_softc *sc =
14472fc9317Sjmcneill device_lookup_private(&hdmicec_cd, minor(dev));
14572fc9317Sjmcneill uint8_t data[CEC_MAX_FRAMESIZE];
14672fc9317Sjmcneill ssize_t len;
14772fc9317Sjmcneill int error;
14872fc9317Sjmcneill
14972fc9317Sjmcneill if (uio->uio_resid < CEC_MAX_FRAMESIZE)
15072fc9317Sjmcneill return EINVAL;
15172fc9317Sjmcneill
15272fc9317Sjmcneill len = sc->sc_hwif->recv(sc->sc_priv, data, sizeof(data));
15372fc9317Sjmcneill if (len < 0)
15472fc9317Sjmcneill return EIO;
15572fc9317Sjmcneill
15672fc9317Sjmcneill error = uiomove(data, len, uio);
15772fc9317Sjmcneill if (error)
15872fc9317Sjmcneill return error;
15972fc9317Sjmcneill
16072fc9317Sjmcneill return 0;
16172fc9317Sjmcneill }
16272fc9317Sjmcneill
16372fc9317Sjmcneill static int
hdmicec_write(dev_t dev,struct uio * uio,int flags)16472fc9317Sjmcneill hdmicec_write(dev_t dev, struct uio *uio, int flags)
16572fc9317Sjmcneill {
16672fc9317Sjmcneill struct hdmicec_softc *sc =
16772fc9317Sjmcneill device_lookup_private(&hdmicec_cd, minor(dev));
16872fc9317Sjmcneill uint8_t data[CEC_MAX_FRAMESIZE];
16972fc9317Sjmcneill size_t len = uio->uio_resid;
17072fc9317Sjmcneill int error;
17172fc9317Sjmcneill
17272fc9317Sjmcneill if (len > CEC_MAX_FRAMESIZE)
17372fc9317Sjmcneill return EINVAL;
17472fc9317Sjmcneill
17572fc9317Sjmcneill error = uiomove(data, len, uio);
17672fc9317Sjmcneill if (error)
17772fc9317Sjmcneill return error;
17872fc9317Sjmcneill
17972fc9317Sjmcneill return sc->sc_hwif->send(sc->sc_priv, data, len);
18072fc9317Sjmcneill }
18172fc9317Sjmcneill
18272fc9317Sjmcneill static int
hdmicec_ioctl(dev_t dev,u_long cmd,void * data,int flag,lwp_t * l)18372fc9317Sjmcneill hdmicec_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
18472fc9317Sjmcneill {
18572fc9317Sjmcneill struct hdmicec_softc *sc =
18672fc9317Sjmcneill device_lookup_private(&hdmicec_cd, minor(dev));
18772fc9317Sjmcneill
18872fc9317Sjmcneill return sc->sc_hwif->ioctl(sc->sc_priv, cmd, data, flag, l);
18972fc9317Sjmcneill }
19072fc9317Sjmcneill
19172fc9317Sjmcneill static int
hdmicec_poll(dev_t dev,int events,lwp_t * l)19272fc9317Sjmcneill hdmicec_poll(dev_t dev, int events, lwp_t *l)
19372fc9317Sjmcneill {
19472fc9317Sjmcneill struct hdmicec_softc *sc =
19572fc9317Sjmcneill device_lookup_private(&hdmicec_cd, minor(dev));
19672fc9317Sjmcneill
19772fc9317Sjmcneill return sc->sc_hwif->poll(sc->sc_priv, events, l);
19872fc9317Sjmcneill }
199