DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[dragonfly.git] / sys / bus / iicbus / iic.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/dev/iicbus/iic.c,v 1.18 1999/11/18 05:43:32 peter Exp $
b13267a5 27 * $DragonFly: src/sys/bus/iicbus/iic.c,v 1.10 2006/09/10 01:26:32 dillon Exp $
984263bc
MD
28 *
29 */
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/systm.h>
33#include <sys/module.h>
34#include <sys/bus.h>
35#include <sys/conf.h>
36#include <sys/buf.h>
37#include <sys/uio.h>
38#include <sys/malloc.h>
39#include <sys/fcntl.h>
40
41#include <machine/clock.h>
42
1f2de5d4
MD
43#include "iiconf.h"
44#include "iicbus.h"
984263bc
MD
45
46#include <machine/iic.h>
47
48#include "iicbus_if.h"
49
50#define BUFSIZE 1024
51
52struct iic_softc {
53
54 u_char sc_addr; /* address on iicbus */
55 int sc_count; /* >0 if device opened */
56
57 char sc_buffer[BUFSIZE]; /* output buffer */
58 char sc_inbuf[BUFSIZE]; /* input buffer */
59};
60
61#define IIC_SOFTC(unit) \
62 ((struct iic_softc *)devclass_get_softc(iic_devclass, (unit)))
63
64#define IIC_DEVICE(unit) \
65 (devclass_get_device(iic_devclass, (unit)))
66
67static int iic_probe(device_t);
68static int iic_attach(device_t);
69
70static devclass_t iic_devclass;
71
72static device_method_t iic_methods[] = {
73 /* device interface */
74 DEVMETHOD(device_probe, iic_probe),
75 DEVMETHOD(device_attach, iic_attach),
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
fef8985e
MD
96static struct dev_ops iic_ops = {
97 { "iic", CDEV_MAJOR, 0 },
98 .d_open = iicopen,
99 .d_close = iicclose,
100 .d_read = iicread,
101 .d_write = iicwrite,
102 .d_ioctl = iicioctl,
984263bc
MD
103};
104
105/*
106 * iicprobe()
107 */
108static int
109iic_probe(device_t dev)
110{
111 struct iic_softc *sc = (struct iic_softc *)device_get_softc(dev);
112
113 sc->sc_addr = iicbus_get_addr(dev);
114
115 /* XXX detect chip with start/stop conditions */
116
117 return (0);
118}
119
120/*
121 * iicattach()
122 */
123static int
124iic_attach(device_t dev)
125{
fef8985e 126 make_dev(&iic_ops, device_get_unit(dev), /* XXX cleanup */
984263bc
MD
127 UID_ROOT, GID_WHEEL,
128 0600, "iic%d", device_get_unit(dev));
129 return (0);
130}
131
132static int
fef8985e 133iicopen (struct dev_open_args *ap)
984263bc 134{
b13267a5 135 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
136 struct iic_softc *sc = IIC_SOFTC(minor(dev));
137
138 if (!sc)
139 return (EINVAL);
140
141 if (sc->sc_count > 0)
142 return (EBUSY);
143
144 sc->sc_count++;
145
146 return (0);
147}
148
149static int
fef8985e 150iicclose(struct dev_close_args *ap)
984263bc 151{
b13267a5 152 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
153 struct iic_softc *sc = IIC_SOFTC(minor(dev));
154
155 if (!sc)
156 return (EINVAL);
157
158 if (!sc->sc_count)
159 return (EINVAL);
160
161 sc->sc_count--;
162
163 if (sc->sc_count < 0)
5e2195bf 164 panic("%s: iic_count < 0!", __func__);
984263bc
MD
165
166 return (0);
167}
168
169static int
fef8985e 170iicwrite(struct dev_write_args *ap)
984263bc 171{
b13267a5 172 cdev_t dev = ap->a_head.a_dev;
fef8985e 173 struct uio *uio = ap->a_uio;
984263bc
MD
174 device_t iicdev = IIC_DEVICE(minor(dev));
175 struct iic_softc *sc = IIC_SOFTC(minor(dev));
176 int sent, error, count;
177
178 if (!sc || !iicdev)
179 return (EINVAL);
180
181 if (sc->sc_count == 0)
182 return (EINVAL);
183
184 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
185 return (error);
186
187 count = min(uio->uio_resid, BUFSIZE);
188 uiomove(sc->sc_buffer, count, uio);
189
190 error = iicbus_block_write(device_get_parent(iicdev), sc->sc_addr,
191 sc->sc_buffer, count, &sent);
192
193 iicbus_release_bus(device_get_parent(iicdev), iicdev);
194
195 return(error);
196}
197
198static int
fef8985e 199iicread(struct dev_read_args *ap)
984263bc 200{
b13267a5 201 cdev_t dev = ap->a_head.a_dev;
fef8985e 202 struct uio *uio = ap->a_uio;
984263bc
MD
203 device_t iicdev = IIC_DEVICE(minor(dev));
204 struct iic_softc *sc = IIC_SOFTC(minor(dev));
205 int len, error = 0;
206 int bufsize;
207
208 if (!sc || !iicdev)
209 return (EINVAL);
210
211 if (sc->sc_count == 0)
212 return (EINVAL);
213
214 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev, IIC_DONTWAIT)))
215 return (error);
216
217 /* max amount of data to read */
218 len = min(uio->uio_resid, BUFSIZE);
219
220 if ((error = iicbus_block_read(device_get_parent(iicdev), sc->sc_addr,
221 sc->sc_inbuf, len, &bufsize)))
222 return (error);
223
224 if (bufsize > uio->uio_resid)
5e2195bf 225 panic("%s: too much data read!", __func__);
984263bc
MD
226
227 iicbus_release_bus(device_get_parent(iicdev), iicdev);
228
229 return (uiomove(sc->sc_inbuf, bufsize, uio));
230}
231
232static int
fef8985e 233iicioctl(struct dev_ioctl_args *ap)
984263bc 234{
b13267a5 235 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
236 device_t iicdev = IIC_DEVICE(minor(dev));
237 struct iic_softc *sc = IIC_SOFTC(minor(dev));
238 device_t parent = device_get_parent(iicdev);
fef8985e 239 struct iiccmd *s = (struct iiccmd *)ap->a_data;
984263bc
MD
240 int error, count;
241
242 if (!sc)
243 return (EINVAL);
244
245 if ((error = iicbus_request_bus(device_get_parent(iicdev), iicdev,
fef8985e 246 (ap->a_fflag & O_NONBLOCK) ? IIC_DONTWAIT :
984263bc
MD
247 (IIC_WAIT | IIC_INTR))))
248 return (error);
249
fef8985e 250 switch (ap->a_cmd) {
984263bc
MD
251 case I2CSTART:
252 error = iicbus_start(parent, s->slave, 0);
253 break;
254
255 case I2CSTOP:
256 error = iicbus_stop(parent);
257 break;
258
259 case I2CRSTCARD:
260 error = iicbus_reset(parent, 0, 0, NULL);
261 break;
262
263 case I2CWRITE:
264 error = iicbus_write(parent, s->buf, s->count, &count, 0);
265 break;
266
267 case I2CREAD:
268 error = iicbus_read(parent, s->buf, s->count, &count, s->last, 0);
269 break;
270
271 default:
272 error = ENODEV;
273 }
274
275 iicbus_release_bus(device_get_parent(iicdev), iicdev);
276
277 return (error);
278}
279
280DRIVER_MODULE(iic, iicbus, iic_driver, iic_devclass, 0, 0);