xref: /freebsd/sys/dev/smbus/smb.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
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 SMB_OLD_READB	_IOW('i', 7, struct smbcmd)
45 #define SMB_OLD_READW	_IOW('i', 8, struct smbcmd)
46 #define SMB_OLD_PCALL	_IOW('i', 9, struct smbcmd)
47 
48 struct smb_softc {
49 	device_t sc_dev;
50 	int sc_count;			/* >0 if device opened */
51 	struct cdev *sc_devnode;
52 	struct mtx sc_lock;
53 };
54 
55 static void smb_identify(driver_t *driver, device_t parent);
56 static int smb_probe(device_t);
57 static int smb_attach(device_t);
58 static int smb_detach(device_t);
59 
60 static devclass_t smb_devclass;
61 
62 static device_method_t smb_methods[] = {
63 	/* device interface */
64 	DEVMETHOD(device_identify,	smb_identify),
65 	DEVMETHOD(device_probe,		smb_probe),
66 	DEVMETHOD(device_attach,	smb_attach),
67 	DEVMETHOD(device_detach,	smb_detach),
68 
69 	/* smbus interface */
70 	DEVMETHOD(smbus_intr,		smbus_generic_intr),
71 
72 	{ 0, 0 }
73 };
74 
75 static driver_t smb_driver = {
76 	"smb",
77 	smb_methods,
78 	sizeof(struct smb_softc),
79 };
80 
81 static	d_open_t	smbopen;
82 static	d_close_t	smbclose;
83 static	d_ioctl_t	smbioctl;
84 
85 static struct cdevsw smb_cdevsw = {
86 	.d_version =	D_VERSION,
87 	.d_flags =	D_TRACKCLOSE,
88 	.d_open =	smbopen,
89 	.d_close =	smbclose,
90 	.d_ioctl =	smbioctl,
91 	.d_name =	"smb",
92 };
93 
94 static void
95 smb_identify(driver_t *driver, device_t parent)
96 {
97 
98 	if (device_find_child(parent, "smb", -1) == NULL)
99 		BUS_ADD_CHILD(parent, 0, "smb", -1);
100 }
101 
102 static int
103 smb_probe(device_t dev)
104 {
105 	if (smbus_get_addr(dev) != -1)
106 		return (ENXIO);
107 
108 	device_set_desc(dev, "SMBus generic I/O");
109 	return (BUS_PROBE_NOWILDCARD);
110 }
111 
112 static int
113 smb_attach(device_t dev)
114 {
115 	struct smb_softc *sc = device_get_softc(dev);
116 	int unit;
117 
118 	unit = device_get_unit(dev);
119 	sc->sc_dev = dev;
120 
121 	sc->sc_devnode = make_dev(&smb_cdevsw, unit, UID_ROOT, GID_WHEEL,
122 	    0600, "smb%d", unit);
123 	sc->sc_devnode->si_drv1 = sc;
124 	mtx_init(&sc->sc_lock, device_get_nameunit(dev), NULL, MTX_DEF);
125 
126 	return (0);
127 }
128 
129 static int
130 smb_detach(device_t dev)
131 {
132 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
133 
134 	if (sc->sc_devnode)
135 		destroy_dev(sc->sc_devnode);
136 	mtx_destroy(&sc->sc_lock);
137 
138 	return (0);
139 }
140 
141 static int
142 smbopen(struct cdev *dev, int flags, int fmt, struct thread *td)
143 {
144 	struct smb_softc *sc = dev->si_drv1;
145 
146 	mtx_lock(&sc->sc_lock);
147 	if (sc->sc_count != 0) {
148 		mtx_unlock(&sc->sc_lock);
149 		return (EBUSY);
150 	}
151 
152 	sc->sc_count++;
153 	mtx_unlock(&sc->sc_lock);
154 
155 	return (0);
156 }
157 
158 static int
159 smbclose(struct cdev *dev, int flags, int fmt, struct thread *td)
160 {
161 	struct smb_softc *sc = dev->si_drv1;
162 
163 	mtx_lock(&sc->sc_lock);
164 	KASSERT(sc->sc_count == 1, ("device not busy"));
165 	sc->sc_count--;
166 	mtx_unlock(&sc->sc_lock);
167 
168 	return (0);
169 }
170 
171 static int
172 smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
173 {
174 	char buf[SMB_MAXBLOCKSIZE];
175 	device_t parent;
176 	struct smbcmd *s = (struct smbcmd *)data;
177 	struct smb_softc *sc = dev->si_drv1;
178 	device_t smbdev = sc->sc_dev;
179 	int error;
180 	int unit;
181 	u_char bcount;
182 
183 	/*
184 	 * If a specific slave device is being used, override any passed-in
185 	 * slave.
186 	 */
187 	unit = dev2unit(dev);
188 	if (unit & 0x0400)
189 		s->slave = unit & 0x03ff;
190 
191 	parent = device_get_parent(smbdev);
192 
193 	/* Make sure that LSB bit is cleared. */
194 	if (s->slave & 0x1)
195 		return (EINVAL);
196 
197 	/* Allocate the bus. */
198 	if ((error = smbus_request_bus(parent, smbdev,
199 			(flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
200 		return (error);
201 
202 	switch (cmd) {
203 	case SMB_QUICK_WRITE:
204 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
205 		break;
206 
207 	case SMB_QUICK_READ:
208 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
209 		break;
210 
211 	case SMB_SENDB:
212 		error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
213 		break;
214 
215 	case SMB_RECVB:
216 		error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
217 		break;
218 
219 	case SMB_WRITEB:
220 		error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
221 						s->wdata.byte));
222 		break;
223 
224 	case SMB_WRITEW:
225 		error = smbus_error(smbus_writew(parent, s->slave,
226 						s->cmd, s->wdata.word));
227 		break;
228 
229 	case SMB_OLD_READB:
230 	case SMB_READB:
231 		/* NB: for SMB_OLD_READB the read data goes to rbuf only. */
232 		error = smbus_error(smbus_readb(parent, s->slave, s->cmd,
233 		    &s->rdata.byte));
234 		if (error)
235 			break;
236 		if (s->rbuf && s->rcount >= 1) {
237 			error = copyout(&s->rdata.byte, s->rbuf, 1);
238 			s->rcount = 1;
239 		}
240 		break;
241 
242 	case SMB_OLD_READW:
243 	case SMB_READW:
244 		/* NB: for SMB_OLD_READW the read data goes to rbuf only. */
245 		error = smbus_error(smbus_readw(parent, s->slave, s->cmd,
246 		    &s->rdata.word));
247 		if (error)
248 			break;
249 		if (s->rbuf && s->rcount >= 2) {
250 			buf[0] = (u_char)s->rdata.word;
251 			buf[1] = (u_char)(s->rdata.word >> 8);
252 			error = copyout(buf, s->rbuf, 2);
253 			s->rcount = 2;
254 		}
255 		break;
256 
257 	case SMB_OLD_PCALL:
258 	case SMB_PCALL:
259 		/* NB: for SMB_OLD_PCALL the read data goes to rbuf only. */
260 		error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
261 		    s->wdata.word, &s->rdata.word));
262 		if (error)
263 			break;
264 		if (s->rbuf && s->rcount >= 2) {
265 			buf[0] = (u_char)s->rdata.word;
266 			buf[1] = (u_char)(s->rdata.word >> 8);
267 			error = copyout(buf, s->rbuf, 2);
268 			s->rcount = 2;
269 		}
270 
271 		break;
272 
273 	case SMB_BWRITE:
274 		if (s->wcount < 0) {
275 			error = EINVAL;
276 			break;
277 		}
278 		if (s->wcount > SMB_MAXBLOCKSIZE)
279 			s->wcount = SMB_MAXBLOCKSIZE;
280 		if (s->wcount)
281 			error = copyin(s->wbuf, buf, s->wcount);
282 		if (error)
283 			break;
284 		error = smbus_error(smbus_bwrite(parent, s->slave, s->cmd,
285 		    s->wcount, buf));
286 		break;
287 
288 	case SMB_BREAD:
289 		if (s->rcount < 0) {
290 			error = EINVAL;
291 			break;
292 		}
293 		if (s->rcount > SMB_MAXBLOCKSIZE)
294 			s->rcount = SMB_MAXBLOCKSIZE;
295 		error = smbus_error(smbus_bread(parent, s->slave, s->cmd,
296 		    &bcount, buf));
297 		if (error)
298 			break;
299 		if (s->rcount > bcount)
300 			s->rcount = bcount;
301 		error = copyout(buf, s->rbuf, s->rcount);
302 		break;
303 
304 	default:
305 		error = ENOTTY;
306 	}
307 
308 	smbus_release_bus(parent, smbdev);
309 
310 	return (error);
311 }
312 
313 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
314 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
315 MODULE_VERSION(smb, 1);
316