Deleted Added
full compact
smbconf.c (119419) smbconf.c (162234)
1/*-
2 * Copyright (c) 1998, 2001 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

--- 13 unchanged lines hidden (view full) ---

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 *
27 */
28
29#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1998, 2001 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

--- 13 unchanged lines hidden (view full) ---

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 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/smbus/smbconf.c 119419 2003-08-24 18:03:45Z obrien $");
30__FBSDID("$FreeBSD: head/sys/dev/smbus/smbconf.c 162234 2006-09-11 20:52:41Z jhb $");
31#include <sys/param.h>
32#include <sys/systm.h>
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/lock.h>
33#include <sys/module.h>
34#include <sys/module.h>
35#include <sys/mutex.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{
36#include <sys/bus.h>
37
38#include <dev/smbus/smbconf.h>
39#include <dev/smbus/smbus.h>
40#include "smbus_if.h"
41
42/*
43 * smbus_intr()
44 */
45void
46smbus_intr(device_t bus, u_char devaddr, char low, char high, int error)
47{
46 struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
48 struct smbus_softc *sc = device_get_softc(bus);
47
48 /* call owner's intr routine */
49
50 /* call owner's intr routine */
51 mtx_lock(&sc->lock);
49 if (sc->owner)
50 SMBUS_INTR(sc->owner, devaddr, low, high, error);
52 if (sc->owner)
53 SMBUS_INTR(sc->owner, devaddr, low, high, error);
51
52 return;
54 mtx_unlock(&sc->lock);
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
55}
56
57/*
58 * smbus_error()
59 *
60 * Converts an smbus error to a unix error.
61 */
62int
63smbus_error(int smb_error)
64{
65 int error = 0;
66
67 if (smb_error == SMB_ENOERR)
68 return (0);
69
68 if (smb_error & (SMB_ENOTSUPP)) {
70 if (smb_error & (SMB_ENOTSUPP))
69 error = ENODEV;
71 error = ENODEV;
70 } else if (smb_error & (SMB_ENOACK)) {
72 else if (smb_error & (SMB_ENOACK))
71 error = ENXIO;
73 error = ENXIO;
72 } else if (smb_error & (SMB_ETIMEOUT)) {
74 else if (smb_error & (SMB_ETIMEOUT))
73 error = EWOULDBLOCK;
75 error = EWOULDBLOCK;
74 } else if (smb_error & (SMB_EBUSY)) {
76 else if (smb_error & (SMB_EBUSY))
75 error = EBUSY;
77 error = EBUSY;
76 } else {
78 else if (smb_error & (SMB_EABORT | SMB_EBUSERR | SMB_ECOLLI))
79 error = EIO;
80 else
77 error = EINVAL;
81 error = EINVAL;
78 }
79
80 return (error);
81}
82
83static int
84smbus_poll(struct smbus_softc *sc, int how)
85{
86 int error;
87
88 switch (how) {
82
83 return (error);
84}
85
86static int
87smbus_poll(struct smbus_softc *sc, int how)
88{
89 int error;
90
91 switch (how) {
89 case (SMB_WAIT | SMB_INTR):
90 error = tsleep(sc, SMBPRI|PCATCH, "smbreq", 0);
92 case SMB_WAIT | SMB_INTR:
93 error = msleep(sc, &sc->lock, SMBPRI|PCATCH, "smbreq", 0);
91 break;
92
94 break;
95
93 case (SMB_WAIT | SMB_NOINTR):
94 error = tsleep(sc, SMBPRI, "smbreq", 0);
96 case SMB_WAIT | SMB_NOINTR:
97 error = msleep(sc, &sc->lock, SMBPRI, "smbreq", 0);
95 break;
96
97 default:
98 break;
99
100 default:
98 return (EWOULDBLOCK);
101 error = EWOULDBLOCK;
99 break;
100 }
101
102 return (error);
103}
104
105/*
106 * smbus_request_bus()
107 *
108 * Allocate the device to perform transfers.
109 *
110 * how : SMB_WAIT or SMB_DONTWAIT
111 */
112int
113smbus_request_bus(device_t bus, device_t dev, int how)
114{
102 break;
103 }
104
105 return (error);
106}
107
108/*
109 * smbus_request_bus()
110 *
111 * Allocate the device to perform transfers.
112 *
113 * how : SMB_WAIT or SMB_DONTWAIT
114 */
115int
116smbus_request_bus(device_t bus, device_t dev, int how)
117{
115 struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
116 int s, error = 0;
118 struct smbus_softc *sc = device_get_softc(bus);
119 device_t parent;
120 int error;
117
118 /* first, ask the underlying layers if the request is ok */
121
122 /* first, ask the underlying layers if the request is ok */
123 parent = device_get_parent(bus);
124 mtx_lock(&sc->lock);
119 do {
125 do {
120 error = SMBUS_CALLBACK(device_get_parent(bus),
121 SMB_REQUEST_BUS, (caddr_t)&how);
126 mtx_unlock(&sc->lock);
127 error = SMBUS_CALLBACK(parent, SMB_REQUEST_BUS, &how);
128 mtx_lock(&sc->lock);
129
122 if (error)
123 error = smbus_poll(sc, how);
124 } while (error == EWOULDBLOCK);
125
130 if (error)
131 error = smbus_poll(sc, how);
132 } while (error == EWOULDBLOCK);
133
126 while (!error) {
127 s = splhigh();
128 if (sc->owner && sc->owner != dev) {
129 splx(s);
130
134 while (error == 0) {
135 if (sc->owner && sc->owner != dev)
131 error = smbus_poll(sc, how);
136 error = smbus_poll(sc, how);
132 } else {
137 else {
133 sc->owner = dev;
138 sc->owner = dev;
134
135 splx(s);
136 return (0);
139 break;
137 }
138
139 /* free any allocated resource */
140 }
141
142 /* free any allocated resource */
140 if (error)
141 SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS,
142 (caddr_t)&how);
143 if (error) {
144 mtx_unlock(&sc->lock);
145 SMBUS_CALLBACK(parent, SMB_RELEASE_BUS, &how);
146 return (error);
147 }
143 }
148 }
149 mtx_unlock(&sc->lock);
144
145 return (error);
146}
147
148/*
149 * smbus_release_bus()
150 *
151 * Release the device allocated with smbus_request_dev()
152 */
153int
154smbus_release_bus(device_t bus, device_t dev)
155{
150
151 return (error);
152}
153
154/*
155 * smbus_release_bus()
156 *
157 * Release the device allocated with smbus_request_dev()
158 */
159int
160smbus_release_bus(device_t bus, device_t dev)
161{
156 struct smbus_softc *sc = (struct smbus_softc *)device_get_softc(bus);
157 int s, error;
162 struct smbus_softc *sc = device_get_softc(bus);
163 int error;
158
159 /* first, ask the underlying layers if the release is ok */
160 error = SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS, NULL);
161
162 if (error)
163 return (error);
164
164
165 /* first, ask the underlying layers if the release is ok */
166 error = SMBUS_CALLBACK(device_get_parent(bus), SMB_RELEASE_BUS, NULL);
167
168 if (error)
169 return (error);
170
165 s = splhigh();
166 if (sc->owner != dev) {
167 splx(s);
168 return (EACCES);
169 }
171 mtx_lock(&sc->lock);
172 if (sc->owner == dev) {
173 sc->owner = NULL;
170
174
171 sc->owner = 0;
172 splx(s);
175 /* wakeup waiting processes */
176 wakeup(sc);
177 } else
178 error = EACCES;
179 mtx_unlock(&sc->lock);
173
180
174 /* wakeup waiting processes */
175 wakeup(sc);
176
177 return (0);
181 return (error);
178}
182}