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