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/smbconf.c,v 1.9 1999/12/03 08:41:08 mdodd Exp $
27 * $DragonFly: src/sys/bus/smbus/smbconf.c,v 1.4 2003/08/07 21:16:47 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
45 smbus_intr(device_t bus, u_char devaddr, char low, char high, int error)
47 struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
49 /* call owner's intr routine */
51 SMBUS_INTR(sc->owner, devaddr, low, high, error);
59 * Converts an smbus error to a unix error.
62 smbus_error(int smb_error)
66 if (smb_error == SMB_ENOERR)
69 if (smb_error & (SMB_ENOTSUPP)) {
71 } else if (smb_error & (SMB_ENOACK)) {
73 } else if (smb_error & (SMB_ETIMEOUT)) {
75 } else if (smb_error & (SMB_EBUSY)) {
87 * Allocate a new bus connected to the given parent device
90 smbus_alloc_bus(device_t parent)
94 /* add the bus to the parent */
95 child = device_add_child(parent, "smbus", -1);
101 smbus_poll(struct smbus_softc *sc, int how)
106 case (SMB_WAIT | SMB_INTR):
107 error = tsleep(sc, PCATCH, "smbreq", 0);
110 case (SMB_WAIT | SMB_NOINTR):
111 error = tsleep(sc, 0, "smbreq", 0);
115 return (EWOULDBLOCK);
123 * smbus_request_bus()
125 * Allocate the device to perform transfers.
127 * how : SMB_WAIT or SMB_DONTWAIT
130 smbus_request_bus(device_t bus, device_t dev, int how)
132 struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
135 /* first, ask the underlying layers if the request is ok */
137 error = SMBUS_CALLBACK(device_get_parent(bus),
138 SMB_REQUEST_BUS, (caddr_t)&how);
140 error = smbus_poll(sc, how);
141 } while (error == EWOULDBLOCK);
145 if (sc->owner && sc->owner != dev) {
148 error = smbus_poll(sc, how);
156 /* free any allocated resource */
158 SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS,
166 * smbus_release_bus()
168 * Release the device allocated with smbus_request_dev()
171 smbus_release_bus(device_t bus, device_t dev)
173 struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
176 /* first, ask the underlying layers if the release is ok */
177 error = SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS, NULL);
183 if (sc->owner != dev) {
191 /* wakeup waiting processes */
200 * Get the I2C 7 bits address of the device
203 smbus_get_addr(device_t dev)
206 device_t parent = device_get_parent(dev);
208 BUS_READ_IVAR(parent, dev, SMBUS_IVAR_ADDR, &addr);
210 return ((u_char)addr);