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