i2c.c revision 1.50 1 1.50 pgoyette /* $NetBSD: i2c.c,v 1.50 2015/12/10 05:33:28 pgoyette Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 2003 Wasabi Systems, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed for the NetBSD Project by
20 1.1 thorpej * Wasabi Systems, Inc.
21 1.1 thorpej * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.1 thorpej * or promote products derived from this software without specific prior
23 1.1 thorpej * written permission.
24 1.1 thorpej *
25 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
36 1.1 thorpej */
37 1.1 thorpej
38 1.45 jmcneill #ifdef _KERNEL_OPT
39 1.45 jmcneill #include "opt_i2c.h"
40 1.45 jmcneill #endif
41 1.45 jmcneill
42 1.18 lukem #include <sys/cdefs.h>
43 1.50 pgoyette __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.50 2015/12/10 05:33:28 pgoyette Exp $");
44 1.18 lukem
45 1.1 thorpej #include <sys/param.h>
46 1.1 thorpej #include <sys/systm.h>
47 1.1 thorpej #include <sys/device.h>
48 1.1 thorpej #include <sys/event.h>
49 1.1 thorpej #include <sys/conf.h>
50 1.11 jmcneill #include <sys/malloc.h>
51 1.34 jmcneill #include <sys/kmem.h>
52 1.11 jmcneill #include <sys/kthread.h>
53 1.11 jmcneill #include <sys/proc.h>
54 1.11 jmcneill #include <sys/kernel.h>
55 1.32 jmcneill #include <sys/fcntl.h>
56 1.28 mbalmer #include <sys/module.h>
57 1.50 pgoyette #include <sys/once.h>
58 1.50 pgoyette #include <sys/mutex.h>
59 1.1 thorpej
60 1.1 thorpej #include <dev/i2c/i2cvar.h>
61 1.1 thorpej
62 1.1 thorpej #include "locators.h"
63 1.1 thorpej
64 1.45 jmcneill #ifndef I2C_MAX_ADDR
65 1.27 pgoyette #define I2C_MAX_ADDR 0x3ff /* 10-bit address, max */
66 1.45 jmcneill #endif
67 1.27 pgoyette
68 1.1 thorpej struct iic_softc {
69 1.1 thorpej i2c_tag_t sc_tag;
70 1.6 jmcneill int sc_type;
71 1.27 pgoyette device_t sc_devices[I2C_MAX_ADDR + 1];
72 1.1 thorpej };
73 1.1 thorpej
74 1.31 jmcneill static dev_type_open(iic_open);
75 1.31 jmcneill static dev_type_close(iic_close);
76 1.31 jmcneill static dev_type_ioctl(iic_ioctl);
77 1.31 jmcneill
78 1.50 pgoyette int iic_init(void);
79 1.50 pgoyette
80 1.50 pgoyette kmutex_t iic_mtx;
81 1.50 pgoyette int iic_refcnt;
82 1.50 pgoyette
83 1.50 pgoyette ONCE_DECL(iic_once);
84 1.50 pgoyette
85 1.31 jmcneill const struct cdevsw iic_cdevsw = {
86 1.43 dholland .d_open = iic_open,
87 1.43 dholland .d_close = iic_close,
88 1.43 dholland .d_read = noread,
89 1.43 dholland .d_write = nowrite,
90 1.43 dholland .d_ioctl = iic_ioctl,
91 1.43 dholland .d_stop = nostop,
92 1.43 dholland .d_tty = notty,
93 1.43 dholland .d_poll = nopoll,
94 1.43 dholland .d_mmap = nommap,
95 1.43 dholland .d_kqfilter = nokqfilter,
96 1.44 dholland .d_discard = nodiscard,
97 1.43 dholland .d_flag = D_OTHER
98 1.31 jmcneill };
99 1.31 jmcneill
100 1.31 jmcneill extern struct cfdriver iic_cd;
101 1.31 jmcneill
102 1.11 jmcneill static void iic_smbus_intr_thread(void *);
103 1.24 martin static void iic_fill_compat(struct i2c_attach_args*, const char*,
104 1.24 martin size_t, char **);
105 1.11 jmcneill
106 1.1 thorpej static int
107 1.24 martin iic_print_direct(void *aux, const char *pnp)
108 1.24 martin {
109 1.24 martin struct i2c_attach_args *ia = aux;
110 1.24 martin
111 1.24 martin if (pnp != NULL)
112 1.24 martin aprint_normal("%s at %s addr 0x%02x", ia->ia_name, pnp,
113 1.24 martin ia->ia_addr);
114 1.24 martin else
115 1.24 martin aprint_normal(" addr 0x%02x", ia->ia_addr);
116 1.24 martin
117 1.24 martin return UNCONF;
118 1.24 martin }
119 1.24 martin
120 1.24 martin static int
121 1.10 christos iic_print(void *aux, const char *pnp)
122 1.1 thorpej {
123 1.1 thorpej struct i2c_attach_args *ia = aux;
124 1.1 thorpej
125 1.6 jmcneill if (ia->ia_addr != (i2c_addr_t)-1)
126 1.6 jmcneill aprint_normal(" addr 0x%x", ia->ia_addr);
127 1.1 thorpej
128 1.30 mbalmer return UNCONF;
129 1.1 thorpej }
130 1.1 thorpej
131 1.1 thorpej static int
132 1.20 xtraeme iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
133 1.1 thorpej {
134 1.20 xtraeme struct iic_softc *sc = device_private(parent);
135 1.1 thorpej struct i2c_attach_args ia;
136 1.1 thorpej
137 1.1 thorpej ia.ia_tag = sc->sc_tag;
138 1.1 thorpej ia.ia_size = cf->cf_loc[IICCF_SIZE];
139 1.6 jmcneill ia.ia_type = sc->sc_type;
140 1.1 thorpej
141 1.25 njoly ia.ia_name = NULL;
142 1.25 njoly ia.ia_ncompat = 0;
143 1.25 njoly ia.ia_compat = NULL;
144 1.25 njoly
145 1.40 soren for (ia.ia_addr = 0; ia.ia_addr <= I2C_MAX_ADDR; ia.ia_addr++) {
146 1.40 soren if (sc->sc_devices[ia.ia_addr] != NULL)
147 1.40 soren continue;
148 1.40 soren
149 1.40 soren if (cf->cf_loc[IICCF_ADDR] != -1 &&
150 1.40 soren cf->cf_loc[IICCF_ADDR] != ia.ia_addr)
151 1.40 soren continue;
152 1.40 soren
153 1.40 soren if (config_match(parent, cf, &ia) > 0)
154 1.27 pgoyette sc->sc_devices[ia.ia_addr] =
155 1.27 pgoyette config_attach(parent, cf, &ia, iic_print);
156 1.27 pgoyette }
157 1.40 soren
158 1.30 mbalmer return 0;
159 1.1 thorpej }
160 1.1 thorpej
161 1.27 pgoyette static void
162 1.27 pgoyette iic_child_detach(device_t parent, device_t child)
163 1.27 pgoyette {
164 1.27 pgoyette struct iic_softc *sc = device_private(parent);
165 1.27 pgoyette int i;
166 1.27 pgoyette
167 1.27 pgoyette for (i = 0; i <= I2C_MAX_ADDR; i++)
168 1.27 pgoyette if (sc->sc_devices[i] == child) {
169 1.27 pgoyette sc->sc_devices[i] = NULL;
170 1.27 pgoyette break;
171 1.40 soren }
172 1.27 pgoyette }
173 1.27 pgoyette
174 1.1 thorpej static int
175 1.26 jmcneill iic_rescan(device_t self, const char *ifattr, const int *locators)
176 1.26 jmcneill {
177 1.26 jmcneill config_search_ia(iic_search, self, ifattr, NULL);
178 1.26 jmcneill return 0;
179 1.26 jmcneill }
180 1.26 jmcneill
181 1.26 jmcneill static int
182 1.20 xtraeme iic_match(device_t parent, cfdata_t cf, void *aux)
183 1.1 thorpej {
184 1.1 thorpej
185 1.30 mbalmer return 1;
186 1.1 thorpej }
187 1.1 thorpej
188 1.1 thorpej static void
189 1.20 xtraeme iic_attach(device_t parent, device_t self, void *aux)
190 1.1 thorpej {
191 1.7 thorpej struct iic_softc *sc = device_private(self);
192 1.1 thorpej struct i2cbus_attach_args *iba = aux;
193 1.24 martin prop_array_t child_devices;
194 1.42 jdc prop_dictionary_t props;
195 1.24 martin char *buf;
196 1.14 ad i2c_tag_t ic;
197 1.14 ad int rv;
198 1.42 jdc bool indirect_config;
199 1.1 thorpej
200 1.33 jmcneill aprint_naive("\n");
201 1.1 thorpej aprint_normal(": I2C bus\n");
202 1.1 thorpej
203 1.1 thorpej sc->sc_tag = iba->iba_tag;
204 1.6 jmcneill sc->sc_type = iba->iba_type;
205 1.14 ad ic = sc->sc_tag;
206 1.19 cegger ic->ic_devname = device_xname(self);
207 1.11 jmcneill
208 1.13 jmcneill LIST_INIT(&(sc->sc_tag->ic_list));
209 1.13 jmcneill LIST_INIT(&(sc->sc_tag->ic_proc_list));
210 1.14 ad
211 1.33 jmcneill rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
212 1.33 jmcneill iic_smbus_intr_thread, ic, &ic->ic_intr_thread,
213 1.33 jmcneill "%s", ic->ic_devname);
214 1.14 ad if (rv)
215 1.20 xtraeme aprint_error_dev(self, "unable to create intr thread\n");
216 1.1 thorpej
217 1.17 jmcneill if (!pmf_device_register(self, NULL, NULL))
218 1.17 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
219 1.17 jmcneill
220 1.42 jdc props = device_properties(parent);
221 1.42 jdc if (!prop_dictionary_get_bool(props, "i2c-indirect-config",
222 1.42 jdc &indirect_config))
223 1.42 jdc indirect_config = true;
224 1.42 jdc child_devices = prop_dictionary_get(props, "i2c-child-devices");
225 1.24 martin if (child_devices) {
226 1.24 martin unsigned int i, count;
227 1.24 martin prop_dictionary_t dev;
228 1.24 martin prop_data_t cdata;
229 1.24 martin uint32_t addr, size;
230 1.24 martin uint64_t cookie;
231 1.24 martin const char *name;
232 1.24 martin struct i2c_attach_args ia;
233 1.24 martin int loc[2];
234 1.24 martin
235 1.24 martin memset(loc, 0, sizeof loc);
236 1.24 martin count = prop_array_count(child_devices);
237 1.24 martin for (i = 0; i < count; i++) {
238 1.24 martin dev = prop_array_get(child_devices, i);
239 1.24 martin if (!dev) continue;
240 1.24 martin if (!prop_dictionary_get_cstring_nocopy(
241 1.24 martin dev, "name", &name))
242 1.24 martin continue;
243 1.24 martin if (!prop_dictionary_get_uint32(dev, "addr", &addr))
244 1.24 martin continue;
245 1.24 martin if (!prop_dictionary_get_uint64(dev, "cookie", &cookie))
246 1.24 martin cookie = 0;
247 1.24 martin loc[0] = addr;
248 1.24 martin if (prop_dictionary_get_uint32(dev, "size", &size))
249 1.24 martin loc[1] = size;
250 1.24 martin else
251 1.24 martin loc[1] = -1;
252 1.24 martin
253 1.24 martin memset(&ia, 0, sizeof ia);
254 1.24 martin ia.ia_addr = addr;
255 1.24 martin ia.ia_type = sc->sc_type;
256 1.24 martin ia.ia_tag = ic;
257 1.24 martin ia.ia_name = name;
258 1.24 martin ia.ia_cookie = cookie;
259 1.39 jdc ia.ia_size = size;
260 1.24 martin
261 1.24 martin buf = NULL;
262 1.24 martin cdata = prop_dictionary_get(dev, "compatible");
263 1.24 martin if (cdata)
264 1.24 martin iic_fill_compat(&ia,
265 1.24 martin prop_data_data_nocopy(cdata),
266 1.24 martin prop_data_size(cdata), &buf);
267 1.24 martin
268 1.33 jmcneill if (addr > I2C_MAX_ADDR) {
269 1.33 jmcneill aprint_error_dev(self,
270 1.33 jmcneill "WARNING: ignoring bad device address "
271 1.33 jmcneill "@ 0x%02x\n", addr);
272 1.33 jmcneill } else if (sc->sc_devices[addr] == NULL) {
273 1.33 jmcneill sc->sc_devices[addr] =
274 1.33 jmcneill config_found_sm_loc(self, "iic", loc, &ia,
275 1.33 jmcneill iic_print_direct, NULL);
276 1.33 jmcneill }
277 1.24 martin
278 1.24 martin if (ia.ia_compat)
279 1.24 martin free(ia.ia_compat, M_TEMP);
280 1.24 martin if (buf)
281 1.24 martin free(buf, M_TEMP);
282 1.24 martin }
283 1.42 jdc } else if (indirect_config) {
284 1.24 martin /*
285 1.24 martin * Attach all i2c devices described in the kernel
286 1.24 martin * configuration file.
287 1.24 martin */
288 1.26 jmcneill iic_rescan(self, "iic", NULL);
289 1.24 martin }
290 1.1 thorpej }
291 1.1 thorpej
292 1.33 jmcneill static int
293 1.33 jmcneill iic_detach(device_t self, int flags)
294 1.33 jmcneill {
295 1.33 jmcneill struct iic_softc *sc = device_private(self);
296 1.33 jmcneill i2c_tag_t ic = sc->sc_tag;
297 1.33 jmcneill int i, error;
298 1.33 jmcneill void *hdl;
299 1.33 jmcneill
300 1.33 jmcneill for (i = 0; i <= I2C_MAX_ADDR; i++) {
301 1.33 jmcneill if (sc->sc_devices[i]) {
302 1.33 jmcneill error = config_detach(sc->sc_devices[i], flags);
303 1.33 jmcneill if (error)
304 1.33 jmcneill return error;
305 1.33 jmcneill }
306 1.33 jmcneill }
307 1.33 jmcneill
308 1.33 jmcneill if (ic->ic_running) {
309 1.33 jmcneill ic->ic_running = 0;
310 1.33 jmcneill wakeup(ic);
311 1.33 jmcneill kthread_join(ic->ic_intr_thread);
312 1.33 jmcneill }
313 1.33 jmcneill
314 1.33 jmcneill if (!LIST_EMPTY(&ic->ic_list)) {
315 1.33 jmcneill device_printf(self, "WARNING: intr handler list not empty\n");
316 1.33 jmcneill while (!LIST_EMPTY(&ic->ic_list)) {
317 1.33 jmcneill hdl = LIST_FIRST(&ic->ic_list);
318 1.33 jmcneill iic_smbus_intr_disestablish(ic, hdl);
319 1.33 jmcneill }
320 1.33 jmcneill }
321 1.33 jmcneill if (!LIST_EMPTY(&ic->ic_proc_list)) {
322 1.33 jmcneill device_printf(self, "WARNING: proc handler list not empty\n");
323 1.33 jmcneill while (!LIST_EMPTY(&ic->ic_proc_list)) {
324 1.33 jmcneill hdl = LIST_FIRST(&ic->ic_proc_list);
325 1.33 jmcneill iic_smbus_intr_disestablish_proc(ic, hdl);
326 1.33 jmcneill }
327 1.33 jmcneill }
328 1.33 jmcneill
329 1.33 jmcneill pmf_device_deregister(self);
330 1.33 jmcneill
331 1.33 jmcneill return 0;
332 1.33 jmcneill }
333 1.33 jmcneill
334 1.11 jmcneill static void
335 1.14 ad iic_smbus_intr_thread(void *aux)
336 1.11 jmcneill {
337 1.11 jmcneill i2c_tag_t ic;
338 1.11 jmcneill struct ic_intr_list *il;
339 1.11 jmcneill
340 1.11 jmcneill ic = (i2c_tag_t)aux;
341 1.11 jmcneill ic->ic_running = 1;
342 1.11 jmcneill ic->ic_pending = 0;
343 1.11 jmcneill
344 1.11 jmcneill while (ic->ic_running) {
345 1.11 jmcneill if (ic->ic_pending == 0)
346 1.41 martin tsleep(ic, PZERO, "iicintr", hz);
347 1.11 jmcneill if (ic->ic_pending > 0) {
348 1.11 jmcneill LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
349 1.11 jmcneill (*il->il_intr)(il->il_intrarg);
350 1.11 jmcneill }
351 1.11 jmcneill ic->ic_pending--;
352 1.11 jmcneill }
353 1.11 jmcneill }
354 1.11 jmcneill
355 1.11 jmcneill kthread_exit(0);
356 1.11 jmcneill }
357 1.11 jmcneill
358 1.11 jmcneill void *
359 1.11 jmcneill iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
360 1.11 jmcneill {
361 1.11 jmcneill struct ic_intr_list *il;
362 1.11 jmcneill
363 1.11 jmcneill il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
364 1.11 jmcneill if (il == NULL)
365 1.11 jmcneill return NULL;
366 1.28 mbalmer
367 1.11 jmcneill il->il_intr = intr;
368 1.11 jmcneill il->il_intrarg = intrarg;
369 1.11 jmcneill
370 1.11 jmcneill LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
371 1.11 jmcneill
372 1.11 jmcneill return il;
373 1.11 jmcneill }
374 1.11 jmcneill
375 1.11 jmcneill void
376 1.11 jmcneill iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
377 1.11 jmcneill {
378 1.11 jmcneill struct ic_intr_list *il;
379 1.11 jmcneill
380 1.11 jmcneill il = (struct ic_intr_list *)hdl;
381 1.11 jmcneill
382 1.11 jmcneill LIST_REMOVE(il, il_next);
383 1.11 jmcneill free(il, M_DEVBUF);
384 1.11 jmcneill
385 1.11 jmcneill return;
386 1.11 jmcneill }
387 1.11 jmcneill
388 1.11 jmcneill void *
389 1.11 jmcneill iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
390 1.11 jmcneill {
391 1.11 jmcneill struct ic_intr_list *il;
392 1.11 jmcneill
393 1.11 jmcneill il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
394 1.11 jmcneill if (il == NULL)
395 1.11 jmcneill return NULL;
396 1.28 mbalmer
397 1.11 jmcneill il->il_intr = intr;
398 1.11 jmcneill il->il_intrarg = intrarg;
399 1.11 jmcneill
400 1.11 jmcneill LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
401 1.11 jmcneill
402 1.11 jmcneill return il;
403 1.11 jmcneill }
404 1.11 jmcneill
405 1.11 jmcneill void
406 1.11 jmcneill iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
407 1.11 jmcneill {
408 1.11 jmcneill struct ic_intr_list *il;
409 1.11 jmcneill
410 1.11 jmcneill il = (struct ic_intr_list *)hdl;
411 1.11 jmcneill
412 1.11 jmcneill LIST_REMOVE(il, il_next);
413 1.11 jmcneill free(il, M_DEVBUF);
414 1.11 jmcneill
415 1.11 jmcneill return;
416 1.11 jmcneill }
417 1.11 jmcneill
418 1.11 jmcneill int
419 1.11 jmcneill iic_smbus_intr(i2c_tag_t ic)
420 1.11 jmcneill {
421 1.11 jmcneill struct ic_intr_list *il;
422 1.11 jmcneill
423 1.11 jmcneill LIST_FOREACH(il, &(ic->ic_list), il_next) {
424 1.11 jmcneill (*il->il_intr)(il->il_intrarg);
425 1.11 jmcneill }
426 1.11 jmcneill
427 1.11 jmcneill ic->ic_pending++;
428 1.11 jmcneill wakeup(ic);
429 1.11 jmcneill
430 1.11 jmcneill return 1;
431 1.11 jmcneill }
432 1.11 jmcneill
433 1.24 martin static void
434 1.24 martin iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len,
435 1.24 martin char **buffer)
436 1.24 martin {
437 1.24 martin int count, i;
438 1.24 martin const char *c, *start, **ptr;
439 1.24 martin
440 1.24 martin *buffer = NULL;
441 1.24 martin for (i = count = 0, c = compat; i < len; i++, c++)
442 1.24 martin if (*c == 0)
443 1.24 martin count++;
444 1.24 martin count += 2;
445 1.24 martin ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
446 1.24 martin if (!ptr) return;
447 1.24 martin
448 1.24 martin for (i = count = 0, start = c = compat; i < len; i++, c++) {
449 1.24 martin if (*c == 0) {
450 1.24 martin ptr[count++] = start;
451 1.24 martin start = c+1;
452 1.24 martin }
453 1.24 martin }
454 1.24 martin if (start < compat+len) {
455 1.24 martin /* last string not 0 terminated */
456 1.24 martin size_t l = c-start;
457 1.24 martin *buffer = malloc(l+1, M_TEMP, M_WAITOK);
458 1.24 martin memcpy(*buffer, start, l);
459 1.24 martin (*buffer)[l] = 0;
460 1.24 martin ptr[count++] = *buffer;
461 1.24 martin }
462 1.24 martin ptr[count] = NULL;
463 1.24 martin
464 1.24 martin ia->ia_compat = ptr;
465 1.24 martin ia->ia_ncompat = count;
466 1.24 martin }
467 1.24 martin
468 1.24 martin int
469 1.24 martin iic_compat_match(struct i2c_attach_args *ia, const char ** compats)
470 1.24 martin {
471 1.24 martin int i;
472 1.24 martin
473 1.24 martin for (; compats && *compats; compats++) {
474 1.24 martin for (i = 0; i < ia->ia_ncompat; i++) {
475 1.24 martin if (strcmp(*compats, ia->ia_compat[i]) == 0)
476 1.24 martin return 1;
477 1.24 martin }
478 1.24 martin }
479 1.24 martin return 0;
480 1.24 martin }
481 1.24 martin
482 1.31 jmcneill static int
483 1.31 jmcneill iic_open(dev_t dev, int flag, int fmt, lwp_t *l)
484 1.31 jmcneill {
485 1.31 jmcneill struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
486 1.31 jmcneill
487 1.50 pgoyette mutex_enter(&iic_mtx);
488 1.50 pgoyette if (sc == NULL) {
489 1.50 pgoyette mutex_exit(&iic_mtx);
490 1.31 jmcneill return ENXIO;
491 1.50 pgoyette }
492 1.50 pgoyette iic_refcnt++;
493 1.50 pgoyette mutex_exit(&iic_mtx);
494 1.31 jmcneill
495 1.31 jmcneill return 0;
496 1.31 jmcneill }
497 1.31 jmcneill
498 1.31 jmcneill static int
499 1.31 jmcneill iic_close(dev_t dev, int flag, int fmt, lwp_t *l)
500 1.31 jmcneill {
501 1.50 pgoyette
502 1.50 pgoyette mutex_enter(&iic_mtx);
503 1.50 pgoyette iic_refcnt--;
504 1.50 pgoyette mutex_exit(&iic_mtx);
505 1.50 pgoyette
506 1.31 jmcneill return 0;
507 1.31 jmcneill }
508 1.31 jmcneill
509 1.31 jmcneill static int
510 1.32 jmcneill iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag)
511 1.31 jmcneill {
512 1.31 jmcneill i2c_tag_t ic = sc->sc_tag;
513 1.31 jmcneill uint8_t buf[I2C_EXEC_MAX_BUFLEN];
514 1.34 jmcneill void *cmd = NULL;
515 1.31 jmcneill int error;
516 1.31 jmcneill
517 1.31 jmcneill /* Validate parameters */
518 1.31 jmcneill if (iie->iie_addr > I2C_MAX_ADDR)
519 1.31 jmcneill return EINVAL;
520 1.31 jmcneill if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN ||
521 1.31 jmcneill iie->iie_buflen > I2C_EXEC_MAX_BUFLEN)
522 1.31 jmcneill return EINVAL;
523 1.31 jmcneill if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0)
524 1.31 jmcneill return EINVAL;
525 1.31 jmcneill if (iie->iie_buf != NULL && iie->iie_buflen == 0)
526 1.31 jmcneill return EINVAL;
527 1.32 jmcneill if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0)
528 1.32 jmcneill return EBADF;
529 1.31 jmcneill
530 1.31 jmcneill #if 0
531 1.31 jmcneill /* Disallow userspace access to devices that have drivers attached. */
532 1.31 jmcneill if (sc->sc_devices[iie->iie_addr] != NULL)
533 1.31 jmcneill return EBUSY;
534 1.31 jmcneill #endif
535 1.31 jmcneill
536 1.31 jmcneill if (iie->iie_cmd != NULL) {
537 1.34 jmcneill cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP);
538 1.34 jmcneill if (cmd == NULL)
539 1.34 jmcneill return ENOMEM;
540 1.31 jmcneill error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen);
541 1.48 christos if (error)
542 1.48 christos goto out;
543 1.31 jmcneill }
544 1.31 jmcneill
545 1.46 jakllsch if (iie->iie_buf != NULL && I2C_OP_WRITE_P(iie->iie_op)) {
546 1.46 jakllsch error = copyin(iie->iie_buf, buf, iie->iie_buflen);
547 1.48 christos if (error)
548 1.48 christos goto out;
549 1.46 jakllsch }
550 1.46 jakllsch
551 1.31 jmcneill iic_acquire_bus(ic, 0);
552 1.31 jmcneill error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen,
553 1.31 jmcneill buf, iie->iie_buflen, 0);
554 1.31 jmcneill iic_release_bus(ic, 0);
555 1.31 jmcneill
556 1.36 jmcneill /*
557 1.36 jmcneill * Some drivers return error codes on failure, and others return -1.
558 1.36 jmcneill */
559 1.36 jmcneill if (error < 0)
560 1.36 jmcneill error = EIO;
561 1.36 jmcneill
562 1.48 christos out:
563 1.34 jmcneill if (cmd)
564 1.34 jmcneill kmem_free(cmd, iie->iie_cmdlen);
565 1.34 jmcneill
566 1.31 jmcneill if (error)
567 1.31 jmcneill return error;
568 1.31 jmcneill
569 1.46 jakllsch if (iie->iie_buf != NULL && I2C_OP_READ_P(iie->iie_op))
570 1.31 jmcneill error = copyout(buf, iie->iie_buf, iie->iie_buflen);
571 1.31 jmcneill
572 1.31 jmcneill return error;
573 1.31 jmcneill }
574 1.31 jmcneill
575 1.31 jmcneill static int
576 1.31 jmcneill iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
577 1.31 jmcneill {
578 1.31 jmcneill struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
579 1.31 jmcneill
580 1.31 jmcneill if (sc == NULL)
581 1.31 jmcneill return ENXIO;
582 1.31 jmcneill
583 1.31 jmcneill switch (cmd) {
584 1.31 jmcneill case I2C_IOCTL_EXEC:
585 1.32 jmcneill return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag);
586 1.31 jmcneill default:
587 1.31 jmcneill return ENODEV;
588 1.31 jmcneill }
589 1.31 jmcneill }
590 1.31 jmcneill
591 1.24 martin
592 1.26 jmcneill CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc),
593 1.33 jmcneill iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach);
594 1.28 mbalmer
595 1.49 pgoyette MODULE(MODULE_CLASS_DRIVER, iic, "i2cexec");
596 1.28 mbalmer
597 1.28 mbalmer #ifdef _MODULE
598 1.28 mbalmer #include "ioconf.c"
599 1.28 mbalmer #endif
600 1.28 mbalmer
601 1.50 pgoyette int
602 1.50 pgoyette iic_init(void)
603 1.50 pgoyette {
604 1.50 pgoyette
605 1.50 pgoyette mutex_init(&iic_mtx, MUTEX_DEFAULT, IPL_NONE);
606 1.50 pgoyette iic_refcnt = 0;
607 1.50 pgoyette return 0;
608 1.50 pgoyette }
609 1.50 pgoyette
610 1.28 mbalmer static int
611 1.28 mbalmer iic_modcmd(modcmd_t cmd, void *opaque)
612 1.28 mbalmer {
613 1.50 pgoyette #ifdef _MODULE
614 1.50 pgoyette int bmajor, cmajor;
615 1.50 pgoyette #endif
616 1.28 mbalmer int error;
617 1.28 mbalmer
618 1.28 mbalmer error = 0;
619 1.28 mbalmer switch (cmd) {
620 1.28 mbalmer case MODULE_CMD_INIT:
621 1.50 pgoyette RUN_ONCE(&iic_once, iic_init);
622 1.50 pgoyette
623 1.28 mbalmer #ifdef _MODULE
624 1.50 pgoyette mutex_enter(&iic_mtx);
625 1.50 pgoyette bmajor = cmajor = -1;
626 1.50 pgoyette error = devsw_attach("iic", NULL, &bmajor,
627 1.50 pgoyette &iic_cdevsw, &cmajor);
628 1.50 pgoyette if (error != 0) {
629 1.50 pgoyette mutex_exit(&iic_mtx);
630 1.50 pgoyette break;
631 1.50 pgoyette }
632 1.28 mbalmer error = config_init_component(cfdriver_ioconf_iic,
633 1.28 mbalmer cfattach_ioconf_iic, cfdata_ioconf_iic);
634 1.50 pgoyette if (error) {
635 1.28 mbalmer aprint_error("%s: unable to init component\n",
636 1.28 mbalmer iic_cd.cd_name);
637 1.50 pgoyette (void)devsw_detach(NULL, &iic_cdevsw);
638 1.50 pgoyette }
639 1.50 pgoyette mutex_exit(&iic_mtx);
640 1.28 mbalmer #endif
641 1.28 mbalmer break;
642 1.28 mbalmer case MODULE_CMD_FINI:
643 1.50 pgoyette mutex_enter(&iic_mtx);
644 1.50 pgoyette if (iic_refcnt != 0) {
645 1.50 pgoyette mutex_exit(&iic_mtx);
646 1.50 pgoyette return EBUSY;
647 1.50 pgoyette }
648 1.28 mbalmer #ifdef _MODULE
649 1.50 pgoyette error = config_fini_component(cfdriver_ioconf_iic,
650 1.28 mbalmer cfattach_ioconf_iic, cfdata_ioconf_iic);
651 1.50 pgoyette if (error != 0) {
652 1.50 pgoyette mutex_exit(&iic_mtx);
653 1.50 pgoyette break;
654 1.50 pgoyette }
655 1.50 pgoyette error = devsw_detach(NULL, &iic_cdevsw);
656 1.50 pgoyette if (error != 0)
657 1.50 pgoyette config_init_component(cfdriver_ioconf_iic,
658 1.50 pgoyette cfattach_ioconf_iic, cfdata_ioconf_iic);
659 1.28 mbalmer #endif
660 1.50 pgoyette mutex_exit(&iic_mtx);
661 1.28 mbalmer break;
662 1.28 mbalmer default:
663 1.28 mbalmer error = ENOTTY;
664 1.28 mbalmer }
665 1.28 mbalmer return error;
666 1.28 mbalmer }
667