xref: /freebsd/sys/dev/smbus/smb.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
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 	/* open */	smbopen,
93 	/* close */	smbclose,
94 	/* read */	smbread,
95 	/* write */	smbwrite,
96 	/* ioctl */	smbioctl,
97 	/* poll */	nopoll,
98 	/* mmap */	nommap,
99 	/* strategy */	nostrategy,
100 	/* name */	"smb",
101 	/* maj */	CDEV_MAJOR,
102 	/* dump */	nodump,
103 	/* psize */	nopsize,
104 	/* flags */	0,
105 };
106 
107 static void
108 smb_identify(driver_t *driver, device_t parent)
109 {
110 	BUS_ADD_CHILD(parent, 0, "smb", 0);
111 }
112 
113 static int
114 smb_probe(device_t dev)
115 {
116 	device_set_desc(dev, "SMBus generic I/O");
117 
118 	return (0);
119 }
120 
121 static int
122 smb_attach(device_t dev)
123 {
124 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
125 
126 	if (!sc)
127 		return (ENOMEM);
128 
129 	bzero(sc, sizeof(struct smb_softc *));
130 
131 	sc->sc_devnode = make_dev(&smb_cdevsw, device_get_unit(dev),
132 			UID_ROOT, GID_WHEEL,
133 			0600, "smb%d", device_get_unit(dev));
134 
135 	return (0);
136 }
137 
138 static int
139 smb_detach(device_t dev)
140 {
141 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
142 
143 	if (sc->sc_devnode)
144 		destroy_dev(sc->sc_devnode);
145 
146 	return (0);
147 }
148 
149 static int
150 smbopen (dev_t dev, int flags, int fmt, struct thread *td)
151 {
152 	struct smb_softc *sc = IIC_SOFTC(minor(dev));
153 
154 	if (!sc)
155 		return (EINVAL);
156 
157 	if (sc->sc_count)
158 		return (EBUSY);
159 
160 	sc->sc_count++;
161 
162 	return (0);
163 }
164 
165 static int
166 smbclose(dev_t dev, int flags, int fmt, struct thread *td)
167 {
168 	struct smb_softc *sc = IIC_SOFTC(minor(dev));
169 
170 	if (!sc)
171 		return (EINVAL);
172 
173 	if (!sc->sc_count)
174 		return (EINVAL);
175 
176 	sc->sc_count--;
177 
178 	return (0);
179 }
180 
181 static int
182 smbwrite(dev_t dev, struct uio * uio, int ioflag)
183 {
184 	/* not supported */
185 
186 	return (EINVAL);
187 }
188 
189 static int
190 smbread(dev_t dev, struct uio * uio, int ioflag)
191 {
192 	/* not supported */
193 
194 	return (EINVAL);
195 }
196 
197 static int
198 smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
199 {
200 	device_t smbdev = IIC_DEVICE(minor(dev));
201 	struct smb_softc *sc = IIC_SOFTC(minor(dev));
202 	device_t parent = device_get_parent(smbdev);
203 
204 	int error = 0;
205 	struct smbcmd *s = (struct smbcmd *)data;
206 
207 	if (!sc || !s)
208 		return (EINVAL);
209 
210 	/* allocate the bus */
211 	if ((error = smbus_request_bus(parent, smbdev,
212 			(flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
213 		return (error);
214 
215 	switch (cmd) {
216 	case SMB_QUICK_WRITE:
217 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
218 		break;
219 
220 	case SMB_QUICK_READ:
221 		error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
222 		break;
223 
224 	case SMB_SENDB:
225 		error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
226 		break;
227 
228 	case SMB_RECVB:
229 		error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
230 		break;
231 
232 	case SMB_WRITEB:
233 		error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
234 						s->data.byte));
235 		break;
236 
237 	case SMB_WRITEW:
238 		error = smbus_error(smbus_writew(parent, s->slave,
239 						s->cmd, s->data.word));
240 		break;
241 
242 	case SMB_READB:
243 		if (s->data.byte_ptr)
244 			error = smbus_error(smbus_readb(parent, s->slave,
245 						s->cmd, s->data.byte_ptr));
246 		break;
247 
248 	case SMB_READW:
249 		if (s->data.word_ptr)
250 			error = smbus_error(smbus_readw(parent, s->slave,
251 						s->cmd, s->data.word_ptr));
252 		break;
253 
254 	case SMB_PCALL:
255 		if (s->data.process.rdata)
256 			error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
257 				s->data.process.sdata, s->data.process.rdata));
258 		break;
259 
260 	case SMB_BWRITE:
261 		if (s->count && s->data.byte_ptr)
262 			error = smbus_error(smbus_bwrite(parent, s->slave,
263 						s->cmd, s->count, s->data.byte_ptr));
264 		break;
265 
266 	case SMB_BREAD:
267 		if (s->count && s->data.byte_ptr)
268 			error = smbus_error(smbus_bread(parent, s->slave,
269 						s->cmd, s->count, s->data.byte_ptr));
270 		break;
271 
272 	default:
273 		error = ENODEV;
274 	}
275 
276 	/* release the bus */
277 	smbus_release_bus(parent, smbdev);
278 
279 	return (error);
280 }
281 
282 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
283 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
284 MODULE_VERSION(smb, 1);
285