2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
40 #include <dev/smbus/smbconf.h>
41 #include <dev/smbus/smbus.h>
48 smbus_intr(device_t bus, u_char devaddr, char low, char high, int error)
50 struct smbus_softc *sc = device_get_softc(bus);
52 /* call owner's intr routine */
55 SMBUS_INTR(sc->owner, devaddr, low, high, error);
56 mtx_unlock(&sc->lock);
62 * Converts an smbus error to a unix error.
65 smbus_error(int smb_error)
69 if (smb_error == SMB_ENOERR)
72 if (smb_error & (SMB_ENOTSUPP))
74 else if (smb_error & (SMB_ENOACK))
76 else if (smb_error & (SMB_ETIMEOUT))
78 else if (smb_error & (SMB_EBUSY))
80 else if (smb_error & (SMB_EABORT | SMB_EBUSERR | SMB_ECOLLI))
89 smbus_poll(struct smbus_softc *sc, int how)
94 case SMB_WAIT | SMB_INTR:
95 error = msleep(sc, &sc->lock, SMBPRI|PCATCH, "smbreq", 0);
98 case SMB_WAIT | SMB_NOINTR:
99 error = msleep(sc, &sc->lock, SMBPRI, "smbreq", 0);
111 * smbus_request_bus()
113 * Allocate the device to perform transfers.
115 * how : SMB_WAIT or SMB_DONTWAIT
118 smbus_request_bus(device_t bus, device_t dev, int how)
120 struct smbus_softc *sc = device_get_softc(bus);
124 /* first, ask the underlying layers if the request is ok */
125 parent = device_get_parent(bus);
128 mtx_unlock(&sc->lock);
129 error = SMBUS_CALLBACK(parent, SMB_REQUEST_BUS, &how);
133 error = smbus_poll(sc, how);
134 } while (error == EWOULDBLOCK);
137 if (sc->owner && sc->owner != dev)
138 error = smbus_poll(sc, how);
144 /* free any allocated resource */
146 mtx_unlock(&sc->lock);
147 SMBUS_CALLBACK(parent, SMB_RELEASE_BUS, &how);
151 mtx_unlock(&sc->lock);
157 * smbus_release_bus()
159 * Release the device allocated with smbus_request_dev()
162 smbus_release_bus(device_t bus, device_t dev)
164 struct smbus_softc *sc = device_get_softc(bus);
167 /* first, ask the underlying layers if the release is ok */
168 error = SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS, NULL);
174 if (sc->owner == dev) {
177 /* wakeup waiting processes */
181 mtx_unlock(&sc->lock);