Deleted Added
sdiff udiff text old ( 103588 ) new ( 111815 )
full compact
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
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/iicbus/iic.c 111815 2003-03-03 12:15:54Z phk $
27 *
28 */
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/systm.h>
32#include <sys/module.h>
33#include <sys/bus.h>
34#include <sys/conf.h>
35#include <sys/uio.h>
36#include <sys/fcntl.h>
37
38#include <dev/iicbus/iiconf.h>
39#include <dev/iicbus/iicbus.h>
40#include <dev/iicbus/iic.h>
41
42#include "iicbus_if.h"
43
44#define BUFSIZE 1024
45
46struct iic_softc {
47
48 u_char sc_addr; /* 7 bit address on iicbus */
49 int sc_count; /* >0 if device opened */
50
51 char sc_buffer[BUFSIZE]; /* output buffer */
52 char sc_inbuf[BUFSIZE]; /* input buffer */
53
54 dev_t sc_devnode;
55};
56
57#define IIC_SOFTC(unit) \
58 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit)))
59
60#define IIC_DEVICE(unit) \
61 (devclass_get_device(iic_devclass, (unit)))
62
63static int iic_probe(device_t);
64static int iic_attach(device_t);
65static int iic_detach(device_t);
66static void iic_identify(driver_t *driver, device_t parent);
67
68static devclass_t iic_devclass;
69
70static device_method_t iic_methods[] = {
71 /* device interface */
72 DEVMETHOD(device_identify, iic_identify),
73 DEVMETHOD(device_probe, iic_probe),
74 DEVMETHOD(device_attach, iic_attach),
75 DEVMETHOD(device_detach, iic_detach),
76
77 /* iicbus interface */
78 DEVMETHOD(iicbus_intr, iicbus_generic_intr),
79
80 { 0, 0 }
81};
82
83static driver_t iic_driver = {
84 "iic",
85 iic_methods,
86 sizeof(struct iic_softc),
87};
88
89static d_open_t iicopen;
90static d_close_t iicclose;
91static d_write_t iicwrite;
92static d_read_t iicread;
93static d_ioctl_t iicioctl;
94
95#define CDEV_MAJOR 105
96static struct cdevsw iic_cdevsw = {
97 .d_open = iicopen,
98 .d_close = iicclose,
99 .d_read = iicread,
100 .d_write = iicwrite,
101 .d_ioctl = iicioctl,
102 .d_name = "iic",
103 .d_maj = CDEV_MAJOR,
104};
105
106static void
107iic_identify(driver_t *driver, device_t parent)
108{
109 BUS_ADD_CHILD(parent, 0, "iic", 0);
110}
111
112static int
113iic_probe(device_t dev)
114{
115 device_set_desc(dev, "I2C generic I/O");
116 return (0);
117}
118
119static int
120iic_attach(device_t dev)
121{
122 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
123
124 if (!sc)
125 return (ENOMEM);
126
127 bzero(sc, sizeof(struct iic_softc));
128
129 sc->sc_devnode = make_dev(&iic_cdevsw, device_get_unit(dev),
130 UID_ROOT, GID_WHEEL,
131 0600, "iic%d", device_get_unit(dev));
132
133 return (0);
134}
135
136static int
137iic_detach(device_t dev)
138{
139 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
140
141 if (sc->sc_devnode)
142 destroy_dev(sc->sc_devnode);
143
144 return (0);
145}
146
147static int
148iicopen (dev_t dev, int flags, int fmt, struct thread *td)
149{
150 struct iic_softc *sc = IIC_SOFTC(minor(dev));
151
152 if (!sc)
153 return (EINVAL);
154
155 if (sc->sc_count > 0)
156 return (EBUSY);
157
158 sc->sc_count++;
159
160 return (0);
161}
162
163static int
164iicclose(dev_t dev, int flags, int fmt, struct thread *td)
165{
166 struct iic_softc *sc = IIC_SOFTC(minor(dev));
167
168 if (!sc)
169 return (EINVAL);
170
171 if (!sc->sc_count)
172 return (EINVAL);
173
174 sc->sc_count--;
175
176 if (sc->sc_count < 0)
177 panic("%s: iic_count < 0!", __func__);
178
179 return (0);
180}
181
182static int
183iicwrite(dev_t dev, struct uio * uio, int ioflag)
184{
185 device_t iicdev = IIC_DEVICE(minor(dev));
186 struct iic_softc *sc = IIC_SOFTC(minor(dev));
187 int sent, error, count;
188
189 if (!sc || !iicdev || !sc->sc_addr)
190 return (EINVAL);
191
192 if (sc->sc_count == 0)
193 return (EINVAL);
194
195 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
196 return (error);
197
198 count = min(uio->uio_resid, BUFSIZE);
199 uiomove(sc->sc_buffer, count, uio);
200
201 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
202 sc->sc_buffer, count, &sent);
203
204 iicbus_release_bus(device_get_parent(iicdev), iicdev);
205
206 return(error);
207}
208
209static int
210iicread(dev_t dev, struct uio * uio, int ioflag)
211{
212 device_t iicdev = IIC_DEVICE(minor(dev));
213 struct iic_softc *sc = IIC_SOFTC(minor(dev));
214 int len, error = 0;
215 int bufsize;
216
217 if (!sc || !iicdev || !sc->sc_addr)
218 return (EINVAL);
219
220 if (sc->sc_count == 0)
221 return (EINVAL);
222
223 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
224 return (error);
225
226 /* max amount of data to read */
227 len = min(uio->uio_resid, BUFSIZE);
228
229 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
230 sc->sc_inbuf, len, &bufsize)))
231 return (error);
232
233 if (bufsize > uio->uio_resid)
234 panic("%s: too much data read!", __func__);
235
236 iicbus_release_bus(device_get_parent(iicdev), iicdev);
237
238 return (uiomove(sc->sc_inbuf, bufsize, uio));
239}
240
241static int
242iicioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
243{
244 device_t iicdev = IIC_DEVICE(minor(dev));
245 struct iic_softc *sc = IIC_SOFTC(minor(dev));
246 device_t parent = device_get_parent(iicdev);
247 struct iiccmd *s = (struct iiccmd *)data;
248 int error, count;
249
250 if (!sc)
251 return (EINVAL);
252
253 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
254 (flags & O_NONBLOCK) ? IIC_DONTWAIT :
255 (IIC_WAIT | IIC_INTR))))
256 return (error);
257
258 switch (cmd) {
259 case I2CSTART:
260 error = iicbus_start(parent, s->slave, 0);
261
262 /*
263 * Implicitly set the chip addr to the slave addr passed as
264 * parameter. Consequently, start/stop shall be called before
265 * the read or the write of a block.
266 */
267 if (!error)
268 sc->sc_addr = s->slave;
269
270 break;
271
272 case I2CSTOP:
273 error = iicbus_stop(parent);
274 break;
275
276 case I2CRSTCARD:
277 error = iicbus_reset(parent, 0, 0, NULL);
278 break;
279
280 case I2CWRITE:
281 error = iicbus_write(parent, s->buf, s->count, &count, 10);
282 break;
283
284 case I2CREAD:
285 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 10);
286 break;
287
288 default:
289 error = ENODEV;
290 }
291
292 iicbus_release_bus(device_get_parent(iicdev), iicdev);
293
294 return (error);
295}
296
297DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);
298MODULE_DEPEND(iic, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
299MODULE_VERSION(iic, 1);