xref: /netbsd-src/sys/dev/hdmicec/hdmicec.c (revision 82b8caba92d0ece070500659c43d13d9ecc6921e)
1 /* $NetBSD: hdmicec.c,v 1.2 2017/10/28 04:53:56 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: hdmicec.c,v 1.2 2017/10/28 04:53:56 riastradh Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/device.h>
35 #include <sys/conf.h>
36 #include <sys/ioctl.h>
37 #include <sys/select.h>
38 #include <sys/atomic.h>
39 
40 #include <dev/hdmicec/hdmicec_if.h>
41 #include <dev/hdmicec/hdmicecio.h>
42 
43 #include "ioconf.h"
44 
45 #define CEC_MAX_FRAMESIZE	16
46 
47 struct hdmicec_softc {
48 	device_t	sc_dev;
49 
50 	void *		sc_priv;
51 	const struct hdmicec_hw_if *sc_hwif;
52 
53 	u_int		sc_busy;
54 };
55 
56 static dev_type_open(hdmicec_open);
57 static dev_type_close(hdmicec_close);
58 static dev_type_read(hdmicec_read);
59 static dev_type_write(hdmicec_write);
60 static dev_type_ioctl(hdmicec_ioctl);
61 static dev_type_poll(hdmicec_poll);
62 
63 static int	hdmicec_match(device_t, cfdata_t, void *);
64 static void	hdmicec_attach(device_t, device_t, void *);
65 
66 const struct cdevsw hdmicec_cdevsw = {
67 	.d_open = hdmicec_open,
68 	.d_close = hdmicec_close,
69 	.d_read = hdmicec_read,
70 	.d_write = hdmicec_write,
71 	.d_ioctl = hdmicec_ioctl,
72 	.d_stop = nostop,
73 	.d_tty = notty,
74 	.d_poll = hdmicec_poll,
75 	.d_mmap = nommap,
76 	.d_kqfilter = nokqfilter,
77 	.d_flag = D_MPSAFE
78 };
79 
80 CFATTACH_DECL_NEW(hdmicec, sizeof(struct hdmicec_softc), hdmicec_match,
81     hdmicec_attach, NULL, NULL);
82 
83 static int
hdmicec_match(device_t parent,cfdata_t match,void * opaque)84 hdmicec_match(device_t parent, cfdata_t match, void *opaque)
85 {
86 	return 1;
87 }
88 
89 static void
hdmicec_attach(device_t parent,device_t self,void * opaque)90 hdmicec_attach(device_t parent, device_t self, void *opaque)
91 {
92 	struct hdmicec_softc *sc = device_private(self);
93 	struct hdmicec_attach_args *caa = opaque;
94 
95 	sc->sc_dev = self;
96 	sc->sc_priv = caa->priv;
97 	sc->sc_hwif = caa->hwif;
98 
99 	aprint_naive("\n");
100 	aprint_normal("\n");
101 }
102 
103 static int
hdmicec_open(dev_t dev,int flag,int fmt,lwp_t * l)104 hdmicec_open(dev_t dev, int flag, int fmt, lwp_t *l)
105 {
106 	struct hdmicec_softc *sc =
107 	    device_lookup_private(&hdmicec_cd, minor(dev));
108 	int error;
109 
110 	if (sc == NULL)
111 		return ENXIO;
112 
113 	if (atomic_cas_uint(&sc->sc_busy, 0, 1) != 1)
114 		return EBUSY;
115 
116 	if (sc->sc_hwif->open != NULL) {
117 		error = sc->sc_hwif->open(sc->sc_priv, flag);
118 		if (error) {
119 			atomic_swap_uint(&sc->sc_busy, 0);
120 			return error;
121 		}
122 	}
123 
124 	return 0;
125 }
126 
127 static int
hdmicec_close(dev_t dev,int flag,int fmt,lwp_t * l)128 hdmicec_close(dev_t dev, int flag, int fmt, lwp_t *l)
129 {
130 	struct hdmicec_softc *sc =
131 	    device_lookup_private(&hdmicec_cd, minor(dev));
132 
133 	if (sc->sc_hwif->close)
134 		sc->sc_hwif->close(sc->sc_priv);
135 
136 	atomic_swap_uint(&sc->sc_busy, 0);
137 	return 0;
138 }
139 
140 static int
hdmicec_read(dev_t dev,struct uio * uio,int flags)141 hdmicec_read(dev_t dev, struct uio *uio, int flags)
142 {
143 	struct hdmicec_softc *sc =
144 	    device_lookup_private(&hdmicec_cd, minor(dev));
145 	uint8_t data[CEC_MAX_FRAMESIZE];
146 	ssize_t len;
147 	int error;
148 
149 	if (uio->uio_resid < CEC_MAX_FRAMESIZE)
150 		return EINVAL;
151 
152 	len = sc->sc_hwif->recv(sc->sc_priv, data, sizeof(data));
153 	if (len < 0)
154 		return EIO;
155 
156 	error = uiomove(data, len, uio);
157 	if (error)
158 		return error;
159 
160 	return 0;
161 }
162 
163 static int
hdmicec_write(dev_t dev,struct uio * uio,int flags)164 hdmicec_write(dev_t dev, struct uio *uio, int flags)
165 {
166 	struct hdmicec_softc *sc =
167 	    device_lookup_private(&hdmicec_cd, minor(dev));
168 	uint8_t data[CEC_MAX_FRAMESIZE];
169 	size_t len = uio->uio_resid;
170 	int error;
171 
172 	if (len > CEC_MAX_FRAMESIZE)
173 		return EINVAL;
174 
175 	error = uiomove(data, len, uio);
176 	if (error)
177 		return error;
178 
179 	return sc->sc_hwif->send(sc->sc_priv, data, len);
180 }
181 
182 static int
hdmicec_ioctl(dev_t dev,u_long cmd,void * data,int flag,lwp_t * l)183 hdmicec_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
184 {
185 	struct hdmicec_softc *sc =
186 	    device_lookup_private(&hdmicec_cd, minor(dev));
187 
188 	return sc->sc_hwif->ioctl(sc->sc_priv, cmd, data, flag, l);
189 }
190 
191 static int
hdmicec_poll(dev_t dev,int events,lwp_t * l)192 hdmicec_poll(dev_t dev, int events, lwp_t *l)
193 {
194 	struct hdmicec_softc *sc =
195 	    device_lookup_private(&hdmicec_cd, minor(dev));
196 
197 	return sc->sc_hwif->poll(sc->sc_priv, events, l);
198 }
199