hammer(8): Add inline keywords in two prototypes of inline functions.
[dragonfly.git] / sys / bus / iicbus / iiconf.c
blob12275ba9ffe49cfbc3076f0e5b03bbc7dc8ba325
1 /*-
2 * Copyright (c) 1998 Nicolas Souchu
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/sys/dev/iicbus/iiconf.c,v 1.19 2008/08/23 07:38:00 imp Exp $
27 * $DragonFly: src/sys/bus/iicbus/iiconf.c,v 1.5 2005/06/02 20:40:36 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/thread2.h>
37 #include <bus/iicbus/iiconf.h>
38 #include <bus/iicbus/iicbus.h>
39 #include "iicbus_if.h"
42 * iicbus_intr()
44 void
45 iicbus_intr(device_t bus, int event, char *buf)
47 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
49 /* call owner's intr routine */
50 if (sc->owner)
51 IICBUS_INTR(sc->owner, event, buf);
53 return;
56 static int
57 iicbus_poll(struct iicbus_softc *sc, int how)
59 int error;
61 switch (how) {
62 case IIC_WAIT | IIC_INTR:
63 error = tsleep(sc, PCATCH, "iicreq", 0);
64 break;
66 case IIC_WAIT | IIC_NOINTR:
67 error = tsleep(sc, 0, "iicreq", 0);
68 break;
70 default:
71 return (EWOULDBLOCK);
72 break;
75 return (error);
79 * iicbus_request_bus()
81 * Allocate the device to perform transfers.
83 * how : IIC_WAIT or IIC_DONTWAIT
85 int
86 iicbus_request_bus(device_t bus, device_t dev, int how)
88 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
89 int error = 0;
91 /* first, ask the underlying layers if the request is ok */
92 do {
93 error = IICBUS_CALLBACK(device_get_parent(bus),
94 IIC_REQUEST_BUS, (caddr_t)&how);
95 if (error)
96 error = iicbus_poll(sc, how);
97 } while (error == EWOULDBLOCK);
99 while (!error) {
100 crit_enter();
101 if (sc->owner && sc->owner != dev) {
102 crit_exit();
103 error = iicbus_poll(sc, how);
104 } else {
105 sc->owner = dev;
106 crit_exit();
107 return (0);
110 /* free any allocated resource */
111 if (error)
112 IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS,
113 (caddr_t)&how);
116 return (error);
120 * iicbus_release_bus()
122 * Release the device allocated with iicbus_request_dev()
125 iicbus_release_bus(device_t bus, device_t dev)
127 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
128 int error;
130 /* first, ask the underlying layers if the release is ok */
131 error = IICBUS_CALLBACK(device_get_parent(bus), IIC_RELEASE_BUS, NULL);
133 if (error)
134 return (error);
136 crit_enter();
137 if (sc->owner != dev) {
138 crit_exit();
139 return (EACCES);
141 sc->owner = NULL;
142 crit_exit();
144 /* wakeup waiting processes */
145 wakeup(sc);
147 return (0);
151 * iicbus_started()
153 * Test if the iicbus is started by the controller
156 iicbus_started(device_t bus)
158 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
160 return (sc->started);
164 * iicbus_start()
166 * Send start condition to the slave addressed by 'slave'
169 iicbus_start(device_t bus, u_char slave, int timeout)
171 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
172 int error = 0;
174 if (sc->started)
175 return (EINVAL); /* bus already started */
177 if (!(error = IICBUS_START(device_get_parent(bus), slave, timeout)))
178 sc->started = slave;
179 else
180 sc->started = 0;
182 return (error);
186 * iicbus_repeated_start()
188 * Send start condition to the slave addressed by 'slave'
191 iicbus_repeated_start(device_t bus, u_char slave, int timeout)
193 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
194 int error = 0;
196 if (!sc->started)
197 return (EINVAL); /* bus should have been already started */
199 if (!(error = IICBUS_REPEATED_START(device_get_parent(bus), slave, timeout)))
200 sc->started = slave;
201 else
202 sc->started = 0;
204 return (error);
208 * iicbus_stop()
210 * Send stop condition to the bus
213 iicbus_stop(device_t bus)
215 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
216 int error = 0;
218 if (!sc->started)
219 return (EINVAL); /* bus not started */
221 error = IICBUS_STOP(device_get_parent(bus));
223 /* refuse any further access */
224 sc->started = 0;
226 return (error);
230 * iicbus_write()
232 * Write a block of data to the slave previously started by
233 * iicbus_start() call
236 iicbus_write(device_t bus, const char *buf, int len, int *sent, int timeout)
238 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
240 /* a slave must have been started with the appropriate address */
241 if (!sc->started || (sc->started & LSB))
242 return (EINVAL);
244 return (IICBUS_WRITE(device_get_parent(bus), buf, len, sent, timeout));
248 * iicbus_read()
250 * Read a block of data from the slave previously started by
251 * iicbus_read() call
253 int
254 iicbus_read(device_t bus, char *buf, int len, int *read, int last, int delay)
256 struct iicbus_softc *sc = (struct iicbus_softc *)device_get_softc(bus);
258 /* a slave must have been started with the appropriate address */
259 if (!sc->started || !(sc->started & LSB))
260 return (EINVAL);
262 return (IICBUS_READ(device_get_parent(bus), buf, len, read, last, delay));
266 * iicbus_write_byte()
268 * Write a byte to the slave previously started by iicbus_start() call
271 iicbus_write_byte(device_t bus, char byte, int timeout)
273 char data = byte;
274 int sent;
276 return (iicbus_write(bus, &data, 1, &sent, timeout));
280 * iicbus_read_byte()
282 * Read a byte from the slave previously started by iicbus_start() call
285 iicbus_read_byte(device_t bus, char *byte, int timeout)
287 int read;
289 return (iicbus_read(bus, byte, 1, &read, IIC_LAST_READ, timeout));
293 * iicbus_block_write()
295 * Write a block of data to slave ; start/stop protocol managed
298 iicbus_block_write(device_t bus, u_char slave, char *buf, int len, int *sent)
300 u_char addr = slave & ~LSB;
301 int error;
303 if ((error = iicbus_start(bus, addr, 0)))
304 return (error);
306 error = iicbus_write(bus, buf, len, sent, 0);
308 iicbus_stop(bus);
310 return (error);
314 * iicbus_block_read()
316 * Read a block of data from slave ; start/stop protocol managed
319 iicbus_block_read(device_t bus, u_char slave, char *buf, int len, int *read)
321 u_char addr = slave | LSB;
322 int error;
324 if ((error = iicbus_start(bus, addr, 0)))
325 return (error);
327 error = iicbus_read(bus, buf, len, read, IIC_LAST_READ, 0);
329 iicbus_stop(bus);
331 return (error);
335 * iicbus_transfer()
337 * Do an aribtrary number of transfers on the iicbus. We pass these
338 * raw requests to the bridge driver. If the bridge driver supports
339 * them directly, then it manages all the details. If not, it can use
340 * the helper function iicbus_transfer_gen() which will do the
341 * transfers at a low level.
343 * Pointers passed in as part of iic_msg must be kernel pointers.
344 * Callers that have user addresses to manage must do so on their own.
347 iicbus_transfer(device_t bus, struct iic_msg *msgs, uint32_t nmsgs)
349 return (IICBUS_TRANSFER(device_get_parent(bus), msgs, nmsgs));
353 * Generic version of iicbus_transfer that calls the appropriate
354 * routines to accomplish this. See note above about acceptable
355 * buffer addresses.
358 iicbus_transfer_gen(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
360 int i, error, lenread, lenwrote, nkid;
361 device_t *children, bus;
363 if ((error = device_get_children(dev, &children, &nkid)) != 0)
364 return (error);
365 if (nkid != 1) {
366 kfree(children, M_TEMP);
367 return (EIO);
369 bus = children[0];
370 kfree(children, M_TEMP);
371 for (i = 0, error = 0; i < nmsgs && error == 0; i++) {
372 if (msgs[i].flags & IIC_M_RD)
373 error = iicbus_block_read(bus, msgs[i].slave,
374 msgs[i].buf, msgs[i].len, &lenread);
375 else
376 error = iicbus_block_write(bus, msgs[i].slave,
377 msgs[i].buf, msgs[i].len, &lenwrote);
379 return (error);