2 * Copyright (c) 1998 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.20 1999/11/18 05:44:56 peter Exp $
27 * $DragonFly: src/sys/bus/smbus/smb.c,v 1.4 2003/07/21 07:57:42 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
38 #include <sys/malloc.h>
39 #include <sys/fcntl.h>
41 #include <machine/clock.h>
43 #include <dev/smbus/smbconf.h>
44 #include <dev/smbus/smbus.h>
45 #include <machine/smb.h>
53 int sc_addr; /* address on smbus */
54 int sc_count; /* >0 if device opened */
56 char *sc_cp; /* output buffer pointer */
58 char sc_buffer[BUFSIZE]; /* output buffer */
59 char sc_inbuf[BUFSIZE]; /* input buffer */
62 #define IIC_SOFTC(unit) \
63 ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
65 #define IIC_DEVICE(unit) \
66 (devclass_get_device(smb_devclass, (unit)))
68 static int smb_probe(device_t);
69 static int smb_attach(device_t);
71 static devclass_t smb_devclass;
73 static device_method_t smb_methods[] = {
74 /* device interface */
75 DEVMETHOD(device_probe, smb_probe),
76 DEVMETHOD(device_attach, smb_attach),
79 DEVMETHOD(smbus_intr, smbus_generic_intr),
84 static driver_t smb_driver = {
87 sizeof(struct smb_softc),
90 static d_open_t smbopen;
91 static d_close_t smbclose;
92 static d_write_t smbwrite;
93 static d_read_t smbread;
94 static d_ioctl_t smbioctl;
96 #define CDEV_MAJOR 106
97 static struct cdevsw smb_cdevsw = {
105 /* close */ smbclose,
107 /* write */ smbwrite,
108 /* ioctl */ smbioctl,
111 /* strategy */ nostrategy,
120 smb_probe(device_t dev)
122 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
124 sc->sc_addr = smbus_get_addr(dev);
126 /* XXX detect chip with start/stop conditions */
135 smb_attach(device_t dev)
137 make_dev(&smb_cdevsw, device_get_unit(dev), /* XXX cleanup */
139 0600, "smb%d", device_get_unit(dev));
144 smbopen (dev_t dev, int flags, int fmt, struct thread *td)
146 struct smb_softc *sc = IIC_SOFTC(minor(dev));
160 smbclose(dev_t dev, int flags, int fmt, struct thread *td)
162 struct smb_softc *sc = IIC_SOFTC(minor(dev));
176 smbwrite(dev_t dev, struct uio * uio, int ioflag)
184 smbread(dev_t dev, struct uio * uio, int ioflag)
192 smbioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
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);
199 struct smbcmd *s = (struct smbcmd *)data;
204 /* allocate the bus */
205 if ((error = smbus_request_bus(parent, smbdev,
206 (flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
210 case SMB_QUICK_WRITE:
211 error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
215 error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
219 error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
223 error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
227 error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
232 error = smbus_error(smbus_writew(parent, s->slave,
233 s->cmd, s->data.word));
237 if (s->data.byte_ptr)
238 error = smbus_error(smbus_readb(parent, s->slave,
239 s->cmd, s->data.byte_ptr));
243 if (s->data.word_ptr)
244 error = smbus_error(smbus_readw(parent, s->slave,
245 s->cmd, s->data.word_ptr));
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));
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));
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));
270 /* release the bus */
271 smbus_release_bus(parent, smbdev);
276 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);