xref: /freebsd/sys/dev/smbus/smb.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 
48 	int sc_count;			/* >0 if device opened */
49 	dev_t sc_devnode;
50 };
51 
52 #define IIC_SOFTC(unit) \
53 	((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
54 
55 #define IIC_DEVICE(unit) \
56 	(devclass_get_device(smb_devclass, (unit)))
57 
58 static int smb_probe(device_t);
59 static int smb_attach(device_t);
60 static int smb_detach(device_t);
61 static void smb_identify(driver_t *driver, device_t parent);
62 
63 static devclass_t smb_devclass;
64 
65 static device_method_t smb_methods[] = {
66 	/* device interface */
67 	DEVMETHOD(device_identify,	smb_identify),
68 	DEVMETHOD(device_probe,		smb_probe),
69 	DEVMETHOD(device_attach,	smb_attach),
70 	DEVMETHOD(device_detach,	smb_detach),
71 
72 	/* smbus interface */
73 	DEVMETHOD(smbus_intr,		smbus_generic_intr),
74 
75 	{ 0, 0 }
76 };
77 
78 static driver_t smb_driver = {
79 	"smb",
80 	smb_methods,
81 	sizeof(struct smb_softc),
82 };
83 
84 static	d_open_t	smbopen;
85 static	d_close_t	smbclose;
86 static	d_write_t	smbwrite;
87 static	d_read_t	smbread;
88 static	d_ioctl_t	smbioctl;
89 
90 #define CDEV_MAJOR 106
91 static struct cdevsw smb_cdevsw = {
92 	.d_open =	smbopen,
93 	.d_close =	smbclose,
94 	.d_read =	smbread,
95 	.d_write =	smbwrite,
96 	.d_ioctl =	smbioctl,
97 	.d_name =	"smb",
98 	.d_maj =	CDEV_MAJOR,
99 };
100 
101 static void
102 smb_identify(driver_t *driver, device_t parent)
103 {
104 	BUS_ADD_CHILD(parent, 0, "smb", 0);
105 }
106 
107 static int
108 smb_probe(device_t dev)
109 {
110 	device_set_desc(dev, "SMBus generic I/O");
111 
112 	return (0);
113 }
114 
115 static int
116 smb_attach(device_t dev)
117 {
118 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
119 
120 	if (!sc)
121 		return (ENOMEM);
122 
123 	bzero(sc, sizeof(struct smb_softc *));
124 
125 	sc->sc_devnode = make_dev(&smb_cdevsw, device_get_unit(dev),
126 			UID_ROOT, GID_WHEEL,
127 			0600, "smb%d", device_get_unit(dev));
128 
129 	return (0);
130 }
131 
132 static int
133 smb_detach(device_t dev)
134 {
135 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
136 
137 	if (sc->sc_devnode)
138 		destroy_dev(sc->sc_devnode);
139 
140 	return (0);
141 }
142 
143 static int
144 smbopen (dev_t dev, int flags, int fmt, struct thread *td)
145 {
146 	struct smb_softc *sc = IIC_SOFTC(minor(dev));
147 
148 	if (!sc)
149 		return (EINVAL);
150 
151 	if (sc->sc_count)
152 		return (EBUSY);
153 
154 	sc->sc_count++;
155 
156 	return (0);
157 }
158 
159 static int
160 smbclose(dev_t dev, int flags, int fmt, struct thread *td)
161 {
162 	struct smb_softc *sc = IIC_SOFTC(minor(dev));
163 
164 	if (!sc)
165 		return (EINVAL);
166 
167 	if (!sc->sc_count)
168 		return (EINVAL);
169 
170 	sc->sc_count--;
171 
172 	return (0);
173 }
174 
175 static int
176 smbwrite(dev_t dev, struct uio * uio, int ioflag)
177 {
178 	/* not supported */
179 
180 	return (EINVAL);
181 }
182 
183 static int
184 smbread(dev_t dev, struct uio * uio, int ioflag)
185 {
186 	/* not supported */
187 
188 	return (EINVAL);
189 }
190 
191 static int
192 smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
193 {
194 	device_t smbdev = IIC_DEVICE(minor(dev));
195 	struct smb_softc *sc = IIC_SOFTC(minor(dev));
196 	device_t parent = device_get_parent(smbdev);
197 
198 	int error = 0;
199 	struct smbcmd *s = (struct smbcmd *)data;
200 
201 	if (!sc || !s)
202 		return (EINVAL);
203 
204 	/* allocate the bus */
205 	if ((error = smbus_request_bus(parent, smbdev,
206 			(flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
207 		return (error);
208 
209 	switch (cmd) {
210 	case SMB_QUICK_WRITE:
211 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
212 		break;
213 
214 	case SMB_QUICK_READ:
215 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
216 		break;
217 
218 	case SMB_SENDB:
219 		error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
220 		break;
221 
222 	case SMB_RECVB:
223 		error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
224 		break;
225 
226 	case SMB_WRITEB:
227 		error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
228 						s->data.byte));
229 		break;
230 
231 	case SMB_WRITEW:
232 		error = smbus_error(smbus_writew(parent, s->slave,
233 						s->cmd, s->data.word));
234 		break;
235 
236 	case SMB_READB:
237 		if (s->data.byte_ptr)
238 			error = smbus_error(smbus_readb(parent, s->slave,
239 						s->cmd, s->data.byte_ptr));
240 		break;
241 
242 	case SMB_READW:
243 		if (s->data.word_ptr)
244 			error = smbus_error(smbus_readw(parent, s->slave,
245 						s->cmd, s->data.word_ptr));
246 		break;
247 
248 	case SMB_PCALL:
249 		if (s->data.process.rdata)
250 			error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
251 				s->data.process.sdata, s->data.process.rdata));
252 		break;
253 
254 	case SMB_BWRITE:
255 		if (s->count && s->data.byte_ptr)
256 			error = smbus_error(smbus_bwrite(parent, s->slave,
257 						s->cmd, s->count, s->data.byte_ptr));
258 		break;
259 
260 	case SMB_BREAD:
261 		if (s->count && s->data.byte_ptr)
262 			error = smbus_error(smbus_bread(parent, s->slave,
263 						s->cmd, s->count, s->data.byte_ptr));
264 		break;
265 
266 	default:
267 		error = ENODEV;
268 	}
269 
270 	/* release the bus */
271 	smbus_release_bus(parent, smbdev);
272 
273 	return (error);
274 }
275 
276 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
277 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
278 MODULE_VERSION(smb, 1);
279