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