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