2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/smbus/smb.c,v 1.34.8.2 2006/09/22 19:19:16 jhb Exp $
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>
38 #include <sys/fcntl.h>
40 #include <bus/smbus/smbconf.h>
41 #include <bus/smbus/smbus.h>
46 #include "device_if.h"
52 int sc_count; /* >0 if device opened */
57 #define SMB_SOFTC(unit) \
58 ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
60 #define SMB_DEVICE(unit) \
61 (devclass_get_device(smb_devclass, (unit)))
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);
68 static devclass_t smb_devclass;
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),
79 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
80 DEVMETHOD(bus_print_child, bus_generic_print_child),
84 DEVMETHOD(smbus_intr, smbus_generic_intr),
89 static driver_t smb_driver = {
92 sizeof(struct smb_softc),
95 static d_open_t smbopen;
96 static d_close_t smbclose;
97 static d_ioctl_t smbioctl;
99 static struct dev_ops smb_ops = {
107 smb_identify(driver_t *driver, device_t parent)
109 if (device_find_child(parent, "smb", -1) == NULL)
110 BUS_ADD_CHILD(parent, parent, 0, "smb", -1);
114 smb_probe(device_t dev)
116 device_set_desc(dev, "SMBus generic I/O");
118 /* Allow other subclasses to override this driver. */
119 return (BUS_PROBE_GENERIC);
123 smb_attach(device_t dev)
125 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
131 bzero(sc, sizeof(struct smb_softc *));
132 unit = device_get_unit(dev);
137 sc->sc_devnode = make_dev(&smb_ops, unit,
138 UID_ROOT, GID_WHEEL, 0600,
139 "smb%d-%02x", unit >> 11, unit & 1023);
141 sc->sc_devnode = make_dev(&smb_ops, unit,
142 UID_ROOT, GID_WHEEL, 0600, "smb%d", unit);
149 smb_detach(device_t dev)
151 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
154 dev_ops_remove_minor(&smb_ops, device_get_unit(dev));
160 smbopen (struct dev_open_args *ap)
162 cdev_t dev = ap->a_head.a_dev;
163 struct smb_softc *sc = SMB_SOFTC(minor(dev));
168 if (sc->sc_count != 0)
177 smbclose(struct dev_close_args *ap)
179 cdev_t dev = ap->a_head.a_dev;
180 struct smb_softc *sc = SMB_SOFTC(minor(dev));
185 if (sc->sc_count == 0)
186 /* This is not supposed to happen. */
196 smbwrite(struct dev_write_args *ap)
202 smbread(struct dev_read_args *ap)
209 smbioctl(struct dev_ioctl_args *ap)
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));
227 * If a specific slave device is being used, override any passed-in
232 s->slave = unit & 1023;
236 * NOTE: smbus_*() functions automatically recurse the parent to
237 * get to the actual device driver.
239 bus = device_get_parent(smbdev); /* smbus */
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))))
248 case SMB_QUICK_WRITE:
249 error = smbus_error(smbus_quick(bus, s->slave, SMB_QWRITE));
253 error = smbus_error(smbus_quick(bus, s->slave, SMB_QREAD));
257 error = smbus_error(smbus_sendb(bus, s->slave, s->cmd));
261 error = smbus_error(smbus_recvb(bus, s->slave, &s->cmd));
265 error = smbus_error(smbus_writeb(bus, s->slave, s->cmd,
270 error = smbus_error(smbus_writew(bus, s->slave, s->cmd,
275 error = smbus_error(smbus_readb(bus, s->slave, s->cmd,
277 if (s->rbuf && s->rcount >= 1) {
278 error = copyout(&s->rdata.byte, s->rbuf, 1);
284 error = smbus_error(smbus_readw(bus, s->slave, s->cmd,
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);
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) {
299 buf[0] = (u_char)s->rdata.word;
300 buf[1] = (u_char)(s->rdata.word >> 8);
301 error = copyout(buf, s->rbuf, 2);
310 if (s->wcount > SMB_MAXBLOCKSIZE)
311 s->wcount = SMB_MAXBLOCKSIZE;
313 error = copyin(s->wbuf, buf, s->wcount);
316 error = smbus_error(smbus_bwrite(bus, s->slave, s->cmd,
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);
329 if (s->rcount > bcount)
331 error = copyout(buf, s->rbuf, s->rcount);
337 if (s->rcount > SMB_MAXBLOCKSIZE)
338 s->rcount = SMB_MAXBLOCKSIZE;
341 if (s->wcount > SMB_MAXBLOCKSIZE)
342 s->wcount = SMB_MAXBLOCKSIZE;
344 error = copyin(s->wbuf, buf, s->wcount);
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);
351 error = copyout(buf, s->rbuf, s->rcount);
358 smbus_release_bus(bus, smbdev);
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);