smbconf.c revision 59391
1/*-
2 * Copyright (c) 1998 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: head/sys/dev/smbus/smbconf.c 59391 2000-04-19 14:58:28Z phk $
27 *
28 */
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/kernel.h>
32#include <sys/module.h>
33#include <sys/bus.h>
34
35#include <dev/smbus/smbconf.h>
36#include <dev/smbus/smbus.h>
37#include "smbus_if.h"
38
39/*
40 * smbus_intr()
41 */
42void
43smbus_intr(device_t bus, u_char devaddr, char low, char high, int error)
44{
45	struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
46
47	/* call owner's intr routine */
48	if (sc->owner)
49		SMBUS_INTR(sc->owner, devaddr, low, high, error);
50
51	return;
52}
53
54/*
55 * smbus_error()
56 *
57 * Converts an smbus error to a unix error.
58 */
59int
60smbus_error(int smb_error)
61{
62	int error = 0;
63
64	if (smb_error == SMB_ENOERR)
65		return (0);
66
67	if (smb_error & (SMB_ENOTSUPP)) {
68		error = ENODEV;
69	} else if (smb_error & (SMB_ENOACK)) {
70		error = ENXIO;
71	} else if (smb_error & (SMB_ETIMEOUT)) {
72		error = EWOULDBLOCK;
73	} else if (smb_error & (SMB_EBUSY)) {
74		error = EBUSY;
75	} else {
76		error = EINVAL;
77	}
78
79	return (error);
80}
81
82/*
83 * smbus_alloc_bus()
84 *
85 * Allocate a new bus connected to the given parent device
86 */
87device_t
88smbus_alloc_bus(device_t parent)
89{
90	device_t child;
91
92	/* add the bus to the parent */
93	child = device_add_child(parent, "smbus", -1);
94
95	return (child);
96}
97
98static int
99smbus_poll(struct smbus_softc *sc, int how)
100{
101	int error;
102
103	switch (how) {
104	case (SMB_WAIT | SMB_INTR):
105		error = tsleep(sc, SMBPRI|PCATCH, "smbreq", 0);
106		break;
107
108	case (SMB_WAIT | SMB_NOINTR):
109		error = tsleep(sc, SMBPRI, "smbreq", 0);
110		break;
111
112	default:
113		return (EWOULDBLOCK);
114		break;
115	}
116
117	return (error);
118}
119
120/*
121 * smbus_request_bus()
122 *
123 * Allocate the device to perform transfers.
124 *
125 * how	: SMB_WAIT or SMB_DONTWAIT
126 */
127int
128smbus_request_bus(device_t bus, device_t dev, int how)
129{
130	struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
131	int s, error = 0;
132
133	/* first, ask the underlying layers if the request is ok */
134	do {
135		error = SMBUS_CALLBACK(device_get_parent(bus),
136						SMB_REQUEST_BUS, (caddr_t)&how);
137		if (error)
138			error = smbus_poll(sc, how);
139	} while (error == EWOULDBLOCK);
140
141	while (!error) {
142		s = splhigh();
143		if (sc->owner && sc->owner != dev) {
144			splx(s);
145
146			error = smbus_poll(sc, how);
147		} else {
148			sc->owner = dev;
149
150			splx(s);
151			return (0);
152		}
153
154		/* free any allocated resource */
155		if (error)
156			SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS,
157					(caddr_t)&how);
158	}
159
160	return (error);
161}
162
163/*
164 * smbus_release_bus()
165 *
166 * Release the device allocated with smbus_request_dev()
167 */
168int
169smbus_release_bus(device_t bus, device_t dev)
170{
171	struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
172	int s, error;
173
174	/* first, ask the underlying layers if the release is ok */
175	error = SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS, NULL);
176
177	if (error)
178		return (error);
179
180	s = splhigh();
181	if (sc->owner != dev) {
182		splx(s);
183		return (EACCES);
184	}
185
186	sc->owner = 0;
187	splx(s);
188
189	/* wakeup waiting processes */
190	wakeup(sc);
191
192	return (0);
193}
194
195/*
196 * smbus_get_addr()
197 *
198 * Get the I2C 7 bits address of the device
199 */
200u_char
201smbus_get_addr(device_t dev)
202{
203	uintptr_t addr;
204	device_t parent = device_get_parent(dev);
205
206	BUS_READ_IVAR(parent, dev, SMBUS_IVAR_ADDR, &addr);
207
208	return ((u_char)addr);
209}
210