xref: /freebsd/sys/dev/smbus/smb.c (revision fe815331bb40604ba31312acf7e4619674631777)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998, 2001 Nicolas Souchu
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 AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, 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  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/uio.h>
38 #include <sys/fcntl.h>
39 
40 #include <dev/smbus/smbconf.h>
41 #include <dev/smbus/smbus.h>
42 #include <dev/smbus/smb.h>
43 
44 #include "smbus_if.h"
45 
46 #define SMB_OLD_READB	_IOW('i', 7, struct smbcmd)
47 #define SMB_OLD_READW	_IOW('i', 8, struct smbcmd)
48 #define SMB_OLD_PCALL	_IOW('i', 9, struct smbcmd)
49 
50 struct smb_softc {
51 	device_t sc_dev;
52 	struct cdev *sc_devnode;
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 	{ 0, 0 }
72 };
73 
74 static driver_t smb_driver = {
75 	"smb",
76 	smb_methods,
77 	sizeof(struct smb_softc),
78 };
79 
80 static	d_ioctl_t	smbioctl;
81 
82 static struct cdevsw smb_cdevsw = {
83 	.d_version =	D_VERSION,
84 	.d_flags =	D_TRACKCLOSE,
85 	.d_ioctl =	smbioctl,
86 	.d_name =	"smb",
87 };
88 
89 static void
90 smb_identify(driver_t *driver, device_t parent)
91 {
92 
93 	if (device_find_child(parent, "smb", -1) == NULL)
94 		BUS_ADD_CHILD(parent, 0, "smb", -1);
95 }
96 
97 static int
98 smb_probe(device_t dev)
99 {
100 	if (smbus_get_addr(dev) != -1)
101 		return (ENXIO);
102 
103 	device_set_desc(dev, "SMBus generic I/O");
104 	return (BUS_PROBE_NOWILDCARD);
105 }
106 
107 static int
108 smb_attach(device_t dev)
109 {
110 	struct smb_softc *sc;
111 	struct make_dev_args mda;
112 	int error;
113 
114 	sc = device_get_softc(dev);
115 	sc->sc_dev = dev;
116 
117 	make_dev_args_init(&mda);
118 	mda.mda_devsw = &smb_cdevsw;
119 	mda.mda_unit = device_get_unit(dev);
120 	mda.mda_uid = UID_ROOT;
121 	mda.mda_gid = GID_WHEEL;
122 	mda.mda_mode = 0600;
123 	mda.mda_si_drv1 = sc;
124 	error = make_dev_s(&mda, &sc->sc_devnode, "smb%d", mda.mda_unit);
125 	return (error);
126 }
127 
128 static int
129 smb_detach(device_t dev)
130 {
131 	struct smb_softc *sc;
132 
133 	sc = device_get_softc(dev);
134 	destroy_dev(sc->sc_devnode);
135 	return (0);
136 }
137 
138 static int
139 smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
140 {
141 	char buf[SMB_MAXBLOCKSIZE];
142 	device_t parent;
143 	struct smbcmd *s = (struct smbcmd *)data;
144 	struct smb_softc *sc = dev->si_drv1;
145 	device_t smbdev = sc->sc_dev;
146 	int error;
147 	int unit;
148 	u_char bcount;
149 
150 	/*
151 	 * If a specific slave device is being used, override any passed-in
152 	 * slave.
153 	 */
154 	unit = dev2unit(dev);
155 	if (unit & 0x0400)
156 		s->slave = unit & 0x03ff;
157 
158 	parent = device_get_parent(smbdev);
159 
160 	/* Make sure that LSB bit is cleared. */
161 	if (s->slave & 0x1)
162 		return (EINVAL);
163 
164 	/* Allocate the bus. */
165 	if ((error = smbus_request_bus(parent, smbdev,
166 			(flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
167 		return (error);
168 
169 	switch (cmd) {
170 	case SMB_QUICK_WRITE:
171 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
172 		break;
173 
174 	case SMB_QUICK_READ:
175 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
176 		break;
177 
178 	case SMB_SENDB:
179 		error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
180 		break;
181 
182 	case SMB_RECVB:
183 		error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
184 		break;
185 
186 	case SMB_WRITEB:
187 		error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
188 						s->wdata.byte));
189 		break;
190 
191 	case SMB_WRITEW:
192 		error = smbus_error(smbus_writew(parent, s->slave,
193 						s->cmd, s->wdata.word));
194 		break;
195 
196 	case SMB_OLD_READB:
197 	case SMB_READB:
198 		/* NB: for SMB_OLD_READB the read data goes to rbuf only. */
199 		error = smbus_error(smbus_readb(parent, s->slave, s->cmd,
200 		    &s->rdata.byte));
201 		if (error)
202 			break;
203 		if (s->rbuf && s->rcount >= 1) {
204 			error = copyout(&s->rdata.byte, s->rbuf, 1);
205 			s->rcount = 1;
206 		}
207 		break;
208 
209 	case SMB_OLD_READW:
210 	case SMB_READW:
211 		/* NB: for SMB_OLD_READW the read data goes to rbuf only. */
212 		error = smbus_error(smbus_readw(parent, s->slave, s->cmd,
213 		    &s->rdata.word));
214 		if (error)
215 			break;
216 		if (s->rbuf && s->rcount >= 2) {
217 			buf[0] = (u_char)s->rdata.word;
218 			buf[1] = (u_char)(s->rdata.word >> 8);
219 			error = copyout(buf, s->rbuf, 2);
220 			s->rcount = 2;
221 		}
222 		break;
223 
224 	case SMB_OLD_PCALL:
225 	case SMB_PCALL:
226 		/* NB: for SMB_OLD_PCALL the read data goes to rbuf only. */
227 		error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
228 		    s->wdata.word, &s->rdata.word));
229 		if (error)
230 			break;
231 		if (s->rbuf && s->rcount >= 2) {
232 			buf[0] = (u_char)s->rdata.word;
233 			buf[1] = (u_char)(s->rdata.word >> 8);
234 			error = copyout(buf, s->rbuf, 2);
235 			s->rcount = 2;
236 		}
237 
238 		break;
239 
240 	case SMB_BWRITE:
241 		if (s->wcount < 0) {
242 			error = EINVAL;
243 			break;
244 		}
245 		if (s->wcount > SMB_MAXBLOCKSIZE)
246 			s->wcount = SMB_MAXBLOCKSIZE;
247 		if (s->wcount)
248 			error = copyin(s->wbuf, buf, s->wcount);
249 		if (error)
250 			break;
251 		error = smbus_error(smbus_bwrite(parent, s->slave, s->cmd,
252 		    s->wcount, buf));
253 		break;
254 
255 	case SMB_BREAD:
256 		if (s->rcount < 0) {
257 			error = EINVAL;
258 			break;
259 		}
260 		if (s->rcount > SMB_MAXBLOCKSIZE)
261 			s->rcount = SMB_MAXBLOCKSIZE;
262 		error = smbus_error(smbus_bread(parent, s->slave, s->cmd,
263 		    &bcount, buf));
264 		if (error)
265 			break;
266 		if (s->rcount > bcount)
267 			s->rcount = bcount;
268 		error = copyout(buf, s->rbuf, s->rcount);
269 		break;
270 
271 	default:
272 		error = ENOTTY;
273 	}
274 
275 	smbus_release_bus(parent, smbdev);
276 
277 	return (error);
278 }
279 
280 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
281 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
282 MODULE_VERSION(smb, 1);
283