xref: /dragonfly/sys/dev/smbus/smb/smb.c (revision 0db87cb7)
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: src/sys/dev/smbus/smb.c,v 1.34.8.2 2006/09/22 19:19:16 jhb Exp $
27  *
28  */
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/device.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 <bus/smbus/smbconf.h>
41 #include <bus/smbus/smbus.h>
42 #include "smb.h"
43 
44 #include "smbus_if.h"
45 #include "bus_if.h"
46 #include "device_if.h"
47 
48 #define BUFSIZE 1024
49 
50 struct smb_softc {
51 	device_t sc_dev;
52 	int sc_count;			/* >0 if device opened */
53 	int sc_unit;
54 	cdev_t sc_devnode;
55 };
56 
57 #define SMB_SOFTC(unit) \
58 	((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
59 
60 #define SMB_DEVICE(unit) \
61 	(devclass_get_device(smb_devclass, (unit)))
62 
63 static void smb_identify(driver_t *driver, device_t parent);
64 static int smb_probe(device_t);
65 static int smb_attach(device_t);
66 static int smb_detach(device_t);
67 
68 static devclass_t smb_devclass;
69 
70 static device_method_t smb_methods[] = {
71 	/* device interface */
72 	DEVMETHOD(device_identify,	smb_identify),
73 	DEVMETHOD(device_probe,		smb_probe),
74 	DEVMETHOD(device_attach,	smb_attach),
75 	DEVMETHOD(device_detach,	smb_detach),
76 
77 #if 0
78 	/* bus interface */
79 	DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
80 	DEVMETHOD(bus_print_child,      bus_generic_print_child),
81 #endif
82 
83 	/* smbus interface */
84 	DEVMETHOD(smbus_intr,		smbus_generic_intr),
85 
86 	DEVMETHOD_END
87 };
88 
89 static driver_t smb_driver = {
90 	"smb",
91 	smb_methods,
92 	sizeof(struct smb_softc),
93 };
94 
95 static	d_open_t	smbopen;
96 static	d_close_t	smbclose;
97 static	d_ioctl_t	smbioctl;
98 
99 static struct dev_ops smb_ops = {
100 	{ "smb", 0, 0 },
101 	.d_open =	smbopen,
102 	.d_close =	smbclose,
103 	.d_ioctl =	smbioctl,
104 };
105 
106 static void
107 smb_identify(driver_t *driver, device_t parent)
108 {
109 	if (device_find_child(parent, "smb", -1) == NULL)
110 		BUS_ADD_CHILD(parent, parent, 0, "smb", -1);
111 }
112 
113 static int
114 smb_probe(device_t dev)
115 {
116 	device_set_desc(dev, "SMBus generic I/O");
117 
118 	/* Allow other subclasses to override this driver. */
119 	return (BUS_PROBE_GENERIC);
120 }
121 
122 static int
123 smb_attach(device_t dev)
124 {
125 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
126 	int unit;
127 
128 	if (!sc)
129 		return (ENOMEM);
130 
131 	bzero(sc, sizeof(struct smb_softc *));
132 	unit = device_get_unit(dev);
133 	sc->sc_dev = dev;
134 	sc->sc_unit = unit;
135 
136 	if (unit & 0x0400) {
137 		sc->sc_devnode = make_dev(&smb_ops, unit,
138 				UID_ROOT, GID_WHEEL, 0600,
139 				"smb%d-%02x", unit >> 11, unit & 1023);
140 	} else {
141 		sc->sc_devnode = make_dev(&smb_ops, unit,
142 				UID_ROOT, GID_WHEEL, 0600, "smb%d", unit);
143 	}
144 
145 	return (0);
146 }
147 
148 static int
149 smb_detach(device_t dev)
150 {
151 	struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
152 
153 	if (sc->sc_devnode)
154 		dev_ops_remove_minor(&smb_ops, device_get_unit(dev));
155 
156 	return (0);
157 }
158 
159 static int
160 smbopen (struct dev_open_args *ap)
161 {
162 	cdev_t dev = ap->a_head.a_dev;
163 	struct smb_softc *sc = SMB_SOFTC(minor(dev));
164 
165 	if (sc == NULL)
166 		return (ENXIO);
167 
168 	if (sc->sc_count != 0)
169 		return (EBUSY);
170 
171 	sc->sc_count++;
172 
173 	return (0);
174 }
175 
176 static int
177 smbclose(struct dev_close_args *ap)
178 {
179 	cdev_t dev = ap->a_head.a_dev;
180 	struct smb_softc *sc = SMB_SOFTC(minor(dev));
181 
182 	if (sc == NULL)
183 		return (ENXIO);
184 
185 	if (sc->sc_count == 0)
186 		/* This is not supposed to happen. */
187 		return (0);
188 
189 	sc->sc_count--;
190 
191 	return (0);
192 }
193 
194 #if 0
195 static int
196 smbwrite(struct dev_write_args *ap)
197 {
198 	return (EINVAL);
199 }
200 
201 static int
202 smbread(struct dev_read_args *ap)
203 {
204 	return (EINVAL);
205 }
206 #endif
207 
208 static int
209 smbioctl(struct dev_ioctl_args *ap)
210 {
211 	cdev_t dev = ap->a_head.a_dev;
212 	device_t bus;		/* smbbus */
213 	char buf[SMB_MAXBLOCKSIZE];
214 	struct smbcmd *s = (struct smbcmd *)ap->a_data;
215 	struct smb_softc *sc = SMB_SOFTC(minor(dev));
216 	device_t smbdev = SMB_DEVICE(minor(dev));
217 	int error;
218 	int unit;
219 	u_char bcount;
220 
221 	if (sc == NULL)
222 		return (ENXIO);
223 	if (s == NULL)
224 		return (EINVAL);
225 
226 	/*
227 	 * If a specific slave device is being used, override any passed-in
228 	 * slave.
229 	 */
230 	unit = sc->sc_unit;
231 	if (unit & 0x0400) {
232 		s->slave = unit & 1023;
233 	}
234 
235 	/*
236 	 * NOTE: smbus_*() functions automatically recurse the parent to
237 	 *	 get to the actual device driver.
238 	 */
239 	bus = device_get_parent(smbdev);	/* smbus */
240 
241 	/* Allocate the bus. */
242 	if ((error = smbus_request_bus(bus, smbdev,
243 			(ap->a_fflag & O_NONBLOCK) ?
244 			SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
245 		return (error);
246 
247 	switch (ap->a_cmd) {
248 	case SMB_QUICK_WRITE:
249 		error = smbus_error(smbus_quick(bus, s->slave, SMB_QWRITE));
250 		break;
251 
252 	case SMB_QUICK_READ:
253 		error = smbus_error(smbus_quick(bus, s->slave, SMB_QREAD));
254 		break;
255 
256 	case SMB_SENDB:
257 		error = smbus_error(smbus_sendb(bus, s->slave, s->cmd));
258 		break;
259 
260 	case SMB_RECVB:
261 		error = smbus_error(smbus_recvb(bus, s->slave, &s->cmd));
262 		break;
263 
264 	case SMB_WRITEB:
265 		error = smbus_error(smbus_writeb(bus, s->slave, s->cmd,
266 						s->wdata.byte));
267 		break;
268 
269 	case SMB_WRITEW:
270 		error = smbus_error(smbus_writew(bus, s->slave, s->cmd,
271 						s->wdata.word));
272 		break;
273 
274 	case SMB_READB:
275 		error = smbus_error(smbus_readb(bus, s->slave, s->cmd,
276 						&s->rdata.byte));
277 		if (s->rbuf && s->rcount >= 1) {
278 			error = copyout(&s->rdata.byte, s->rbuf, 1);
279 			s->rcount = 1;
280 		}
281 		break;
282 
283 	case SMB_READW:
284 		error = smbus_error(smbus_readw(bus, s->slave, s->cmd,
285 						&s->rdata.word));
286 		if (s->rbuf && s->rcount >= 2) {
287 			buf[0] = (u_char)s->rdata.word;
288 			buf[1] = (u_char)(s->rdata.word >> 8);
289 			error = copyout(buf, s->rbuf, 2);
290 			s->rcount = 2;
291 		}
292 		break;
293 
294 	case SMB_PCALL:
295 		error = smbus_error(smbus_pcall(bus, s->slave, s->cmd,
296 						s->wdata.word, &s->rdata.word));
297 		if (s->rbuf && s->rcount >= 2) {
298 			char buf[2];
299 			buf[0] = (u_char)s->rdata.word;
300 			buf[1] = (u_char)(s->rdata.word >> 8);
301 			error = copyout(buf, s->rbuf, 2);
302 			s->rcount = 2;
303 		}
304 
305 		break;
306 
307 	case SMB_BWRITE:
308 		if (s->wcount < 0)
309 			s->wcount = 0;
310 		if (s->wcount > SMB_MAXBLOCKSIZE)
311 			s->wcount = SMB_MAXBLOCKSIZE;
312 		if (s->wcount)
313 			error = copyin(s->wbuf, buf, s->wcount);
314 		if (error)
315 			break;
316 		error = smbus_error(smbus_bwrite(bus, s->slave, s->cmd,
317 						 s->wcount, buf));
318 		break;
319 
320 	case SMB_BREAD:
321 		if (s->rcount < 0)
322 			s->rcount = 0;
323 		if (s->rcount > SMB_MAXBLOCKSIZE)
324 			s->rcount = SMB_MAXBLOCKSIZE;
325 		error = smbus_bread(bus, s->slave, s->cmd, &bcount, buf);
326 		error = smbus_error(error);
327 		if (error)
328 			break;
329 		if (s->rcount > bcount)
330 			s->rcount = bcount;
331 		error = copyout(buf, s->rbuf, s->rcount);
332 		break;
333 
334 	case SMB_TRANS:
335 		if (s->rcount < 0)
336 			s->rcount = 0;
337 		if (s->rcount > SMB_MAXBLOCKSIZE)
338 			s->rcount = SMB_MAXBLOCKSIZE;
339 		if (s->wcount < 0)
340 			s->wcount = 0;
341 		if (s->wcount > SMB_MAXBLOCKSIZE)
342 			s->wcount = SMB_MAXBLOCKSIZE;
343 		if (s->wcount)
344 			error = copyin(s->wbuf, buf, s->wcount);
345 		if (error)
346 			break;
347 		error = smbus_trans(bus, s->slave, s->cmd, s->op,
348 				    buf, s->wcount, buf, s->rcount, &s->rcount);
349 		error = smbus_error(error);
350 		if (error == 0)
351 			error = copyout(buf, s->rbuf, s->rcount);
352 		break;
353 	default:
354 		error = ENOTTY;
355 		break;
356 	}
357 
358 	smbus_release_bus(bus, smbdev);
359 
360 	return (error);
361 }
362 
363 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, NULL, NULL);
364 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
365 MODULE_VERSION(smb, 1);
366