xref: /freebsd/sys/dev/smbus/smb.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 1998, 2001 Nicolas Souchu
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/uio.h>
36 #include <sys/fcntl.h>
37 
38 #include <dev/smbus/smbconf.h>
39 #include <dev/smbus/smbus.h>
40 #include <dev/smbus/smb.h>
41 
42 #include "smbus_if.h"
43 
44 #define BUFSIZE 1024
45 
46 struct smb_softc {
47 	device_t sc_dev;
48 	int sc_count;			/* >0 if device opened */
49 	struct cdev *sc_devnode;
50 	struct mtx sc_lock;
51 };
52 
53 static void smb_identify(driver_t *driver, device_t parent);
54 static int smb_probe(device_t);
55 static int smb_attach(device_t);
56 static int smb_detach(device_t);
57 
58 static devclass_t smb_devclass;
59 
60 static device_method_t smb_methods[] = {
61 	/* device interface */
62 	DEVMETHOD(device_identify,	smb_identify),
63 	DEVMETHOD(device_probe,		smb_probe),
64 	DEVMETHOD(device_attach,	smb_attach),
65 	DEVMETHOD(device_detach,	smb_detach),
66 
67 	/* smbus interface */
68 	DEVMETHOD(smbus_intr,		smbus_generic_intr),
69 
70 	{ 0, 0 }
71 };
72 
73 static driver_t smb_driver = {
74 	"smb",
75 	smb_methods,
76 	sizeof(struct smb_softc),
77 };
78 
79 static	d_open_t	smbopen;
80 static	d_close_t	smbclose;
81 static	d_ioctl_t	smbioctl;
82 
83 static struct cdevsw smb_cdevsw = {
84 	.d_version =	D_VERSION,
85 	.d_flags =	D_TRACKCLOSE,
86 	.d_open =	smbopen,
87 	.d_close =	smbclose,
88 	.d_ioctl =	smbioctl,
89 	.d_name =	"smb",
90 };
91 
92 static void
93 smb_identify(driver_t *driver, device_t parent)
94 {
95 
96 	if (device_find_child(parent, "smb", -1) == NULL)
97 		BUS_ADD_CHILD(parent, 0, "smb", -1);
98 }
99 
100 static int
101 smb_probe(device_t dev)
102 {
103 	if (smbus_get_addr(dev) != -1)
104 		return (ENXIO);
105 
106 	device_set_desc(dev, "SMBus generic I/O");
107 	return (BUS_PROBE_NOWILDCARD);
108 }
109 
110 static int
111 smb_attach(device_t dev)
112 {
113 	struct smb_softc *sc = device_get_softc(dev);
114 	int unit;
115 
116 	unit = device_get_unit(dev);
117 	sc->sc_dev = dev;
118 
119 	sc->sc_devnode = make_dev(&smb_cdevsw, unit, UID_ROOT, GID_WHEEL,
120 	    0600, "smb%d", unit);
121 	sc->sc_devnode->si_drv1 = sc;
122 	mtx_init(&sc->sc_lock, device_get_nameunit(dev), NULL, MTX_DEF);
123 
124 	return (0);
125 }
126 
127 static int
128 smb_detach(device_t dev)
129 {
130 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
131 
132 	if (sc->sc_devnode)
133 		destroy_dev(sc->sc_devnode);
134 	mtx_destroy(&sc->sc_lock);
135 
136 	return (0);
137 }
138 
139 static int
140 smbopen(struct cdev *dev, int flags, int fmt, struct thread *td)
141 {
142 	struct smb_softc *sc = dev->si_drv1;
143 
144 	mtx_lock(&sc->sc_lock);
145 	if (sc->sc_count != 0) {
146 		mtx_unlock(&sc->sc_lock);
147 		return (EBUSY);
148 	}
149 
150 	sc->sc_count++;
151 	mtx_unlock(&sc->sc_lock);
152 
153 	return (0);
154 }
155 
156 static int
157 smbclose(struct cdev *dev, int flags, int fmt, struct thread *td)
158 {
159 	struct smb_softc *sc = dev->si_drv1;
160 
161 	mtx_lock(&sc->sc_lock);
162 	KASSERT(sc->sc_count == 1, ("device not busy"));
163 	sc->sc_count--;
164 	mtx_unlock(&sc->sc_lock);
165 
166 	return (0);
167 }
168 
169 static int
170 smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
171 {
172 	char buf[SMB_MAXBLOCKSIZE];
173 	device_t parent;
174 	struct smbcmd *s = (struct smbcmd *)data;
175 	struct smb_softc *sc = dev->si_drv1;
176 	device_t smbdev = sc->sc_dev;
177 	int error;
178 	int unit;
179 	u_char bcount;
180 
181 	/*
182 	 * If a specific slave device is being used, override any passed-in
183 	 * slave.
184 	 */
185 	unit = dev2unit(dev);
186 	if (unit & 0x0400)
187 		s->slave = unit & 0x03ff;
188 
189 	parent = device_get_parent(smbdev);
190 
191 	/* Make sure that LSB bit is cleared. */
192 	if (s->slave & 0x1)
193 		return (EINVAL);
194 
195 	/* Allocate the bus. */
196 	if ((error = smbus_request_bus(parent, smbdev,
197 			(flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
198 		return (error);
199 
200 	switch (cmd) {
201 	case SMB_QUICK_WRITE:
202 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
203 		break;
204 
205 	case SMB_QUICK_READ:
206 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
207 		break;
208 
209 	case SMB_SENDB:
210 		error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
211 		break;
212 
213 	case SMB_RECVB:
214 		error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
215 		break;
216 
217 	case SMB_WRITEB:
218 		error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
219 						s->wdata.byte));
220 		break;
221 
222 	case SMB_WRITEW:
223 		error = smbus_error(smbus_writew(parent, s->slave,
224 						s->cmd, s->wdata.word));
225 		break;
226 
227 	case SMB_READB:
228 		error = smbus_error(smbus_readb(parent, s->slave, s->cmd,
229 		    &s->rdata.byte));
230 		if (error)
231 			break;
232 		if (s->rbuf && s->rcount >= 1) {
233 			error = copyout(&s->rdata.byte, s->rbuf, 1);
234 			s->rcount = 1;
235 		}
236 		break;
237 
238 	case SMB_READW:
239 		error = smbus_error(smbus_readw(parent, s->slave, s->cmd,
240 		    &s->rdata.word));
241 		if (error)
242 			break;
243 		if (s->rbuf && s->rcount >= 2) {
244 			buf[0] = (u_char)s->rdata.word;
245 			buf[1] = (u_char)(s->rdata.word >> 8);
246 			error = copyout(buf, s->rbuf, 2);
247 			s->rcount = 2;
248 		}
249 		break;
250 
251 	case SMB_PCALL:
252 		error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
253 		    s->wdata.word, &s->rdata.word));
254 		if (error)
255 			break;
256 		if (s->rbuf && s->rcount >= 2) {
257 			buf[0] = (u_char)s->rdata.word;
258 			buf[1] = (u_char)(s->rdata.word >> 8);
259 			error = copyout(buf, s->rbuf, 2);
260 			s->rcount = 2;
261 		}
262 
263 		break;
264 
265 	case SMB_BWRITE:
266 		if (s->wcount < 0) {
267 			error = EINVAL;
268 			break;
269 		}
270 		if (s->wcount > SMB_MAXBLOCKSIZE)
271 			s->wcount = SMB_MAXBLOCKSIZE;
272 		if (s->wcount)
273 			error = copyin(s->wbuf, buf, s->wcount);
274 		if (error)
275 			break;
276 		error = smbus_error(smbus_bwrite(parent, s->slave, s->cmd,
277 		    s->wcount, buf));
278 		break;
279 
280 	case SMB_BREAD:
281 		if (s->rcount < 0) {
282 			error = EINVAL;
283 			break;
284 		}
285 		if (s->rcount > SMB_MAXBLOCKSIZE)
286 			s->rcount = SMB_MAXBLOCKSIZE;
287 		error = smbus_error(smbus_bread(parent, s->slave, s->cmd,
288 		    &bcount, buf));
289 		if (error)
290 			break;
291 		if (s->rcount > bcount)
292 			s->rcount = bcount;
293 		error = copyout(buf, s->rbuf, s->rcount);
294 		break;
295 
296 	case SMB_TRANS:
297 		if (s->rcount < 0 || s->wcount < 0) {
298 			error = EINVAL;
299 			break;
300 		}
301 		if (s->rcount > SMB_MAXBLOCKSIZE)
302 			s->rcount = SMB_MAXBLOCKSIZE;
303 		if (s->wcount > SMB_MAXBLOCKSIZE)
304 			s->wcount = SMB_MAXBLOCKSIZE;
305 		if (s->wcount)
306 			error = copyin(s->wbuf, buf, s->wcount);
307 		if (error)
308 			break;
309 		error = smbus_error(smbus_trans(parent, s->slave, s->cmd,
310 		    s->op, buf, s->wcount, buf, s->rcount, &s->rcount));
311 		if (error == 0)
312 			error = copyout(buf, s->rbuf, s->rcount);
313 		break;
314 	default:
315 		error = ENOTTY;
316 	}
317 
318 	smbus_release_bus(parent, smbdev);
319 
320 	return (error);
321 }
322 
323 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
324 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
325 MODULE_VERSION(smb, 1);
326