i2c.c revision 1.54 1 1.54 jakllsch /* $NetBSD: i2c.c,v 1.54 2016/07/23 18:00:01 jakllsch 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.54 jakllsch __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.54 2016/07/23 18:00:01 jakllsch 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.51 jmcneill if (iba->iba_child_devices) {
221 1.51 jmcneill child_devices = iba->iba_child_devices;
222 1.51 jmcneill indirect_config = false;
223 1.51 jmcneill } else {
224 1.51 jmcneill props = device_properties(parent);
225 1.51 jmcneill if (!prop_dictionary_get_bool(props, "i2c-indirect-config",
226 1.51 jmcneill &indirect_config))
227 1.51 jmcneill indirect_config = true;
228 1.51 jmcneill child_devices = prop_dictionary_get(props, "i2c-child-devices");
229 1.51 jmcneill }
230 1.51 jmcneill
231 1.24 martin if (child_devices) {
232 1.24 martin unsigned int i, count;
233 1.24 martin prop_dictionary_t dev;
234 1.24 martin prop_data_t cdata;
235 1.24 martin uint32_t addr, size;
236 1.24 martin uint64_t cookie;
237 1.24 martin const char *name;
238 1.24 martin struct i2c_attach_args ia;
239 1.53 jakllsch int loc[IICCF_NLOCS];
240 1.24 martin
241 1.24 martin memset(loc, 0, sizeof loc);
242 1.24 martin count = prop_array_count(child_devices);
243 1.24 martin for (i = 0; i < count; i++) {
244 1.24 martin dev = prop_array_get(child_devices, i);
245 1.24 martin if (!dev) continue;
246 1.24 martin if (!prop_dictionary_get_cstring_nocopy(
247 1.24 martin dev, "name", &name))
248 1.24 martin continue;
249 1.24 martin if (!prop_dictionary_get_uint32(dev, "addr", &addr))
250 1.24 martin continue;
251 1.24 martin if (!prop_dictionary_get_uint64(dev, "cookie", &cookie))
252 1.24 martin cookie = 0;
253 1.53 jakllsch loc[IICCF_ADDR] = addr;
254 1.24 martin if (prop_dictionary_get_uint32(dev, "size", &size))
255 1.53 jakllsch loc[IICCF_SIZE] = size;
256 1.24 martin else
257 1.54 jakllsch size = loc[IICCF_SIZE] = IICCF_SIZE_DEFAULT;
258 1.24 martin
259 1.24 martin memset(&ia, 0, sizeof ia);
260 1.24 martin ia.ia_addr = addr;
261 1.24 martin ia.ia_type = sc->sc_type;
262 1.24 martin ia.ia_tag = ic;
263 1.24 martin ia.ia_name = name;
264 1.24 martin ia.ia_cookie = cookie;
265 1.39 jdc ia.ia_size = size;
266 1.24 martin
267 1.24 martin buf = NULL;
268 1.24 martin cdata = prop_dictionary_get(dev, "compatible");
269 1.24 martin if (cdata)
270 1.24 martin iic_fill_compat(&ia,
271 1.24 martin prop_data_data_nocopy(cdata),
272 1.24 martin prop_data_size(cdata), &buf);
273 1.24 martin
274 1.33 jmcneill if (addr > I2C_MAX_ADDR) {
275 1.33 jmcneill aprint_error_dev(self,
276 1.33 jmcneill "WARNING: ignoring bad device address "
277 1.33 jmcneill "@ 0x%02x\n", addr);
278 1.33 jmcneill } else if (sc->sc_devices[addr] == NULL) {
279 1.33 jmcneill sc->sc_devices[addr] =
280 1.33 jmcneill config_found_sm_loc(self, "iic", loc, &ia,
281 1.33 jmcneill iic_print_direct, NULL);
282 1.33 jmcneill }
283 1.24 martin
284 1.24 martin if (ia.ia_compat)
285 1.24 martin free(ia.ia_compat, M_TEMP);
286 1.24 martin if (buf)
287 1.24 martin free(buf, M_TEMP);
288 1.24 martin }
289 1.42 jdc } else if (indirect_config) {
290 1.24 martin /*
291 1.24 martin * Attach all i2c devices described in the kernel
292 1.24 martin * configuration file.
293 1.24 martin */
294 1.26 jmcneill iic_rescan(self, "iic", NULL);
295 1.24 martin }
296 1.1 thorpej }
297 1.1 thorpej
298 1.33 jmcneill static int
299 1.33 jmcneill iic_detach(device_t self, int flags)
300 1.33 jmcneill {
301 1.33 jmcneill struct iic_softc *sc = device_private(self);
302 1.33 jmcneill i2c_tag_t ic = sc->sc_tag;
303 1.33 jmcneill int i, error;
304 1.33 jmcneill void *hdl;
305 1.33 jmcneill
306 1.33 jmcneill for (i = 0; i <= I2C_MAX_ADDR; i++) {
307 1.33 jmcneill if (sc->sc_devices[i]) {
308 1.33 jmcneill error = config_detach(sc->sc_devices[i], flags);
309 1.33 jmcneill if (error)
310 1.33 jmcneill return error;
311 1.33 jmcneill }
312 1.33 jmcneill }
313 1.33 jmcneill
314 1.33 jmcneill if (ic->ic_running) {
315 1.33 jmcneill ic->ic_running = 0;
316 1.33 jmcneill wakeup(ic);
317 1.33 jmcneill kthread_join(ic->ic_intr_thread);
318 1.33 jmcneill }
319 1.33 jmcneill
320 1.33 jmcneill if (!LIST_EMPTY(&ic->ic_list)) {
321 1.33 jmcneill device_printf(self, "WARNING: intr handler list not empty\n");
322 1.33 jmcneill while (!LIST_EMPTY(&ic->ic_list)) {
323 1.33 jmcneill hdl = LIST_FIRST(&ic->ic_list);
324 1.33 jmcneill iic_smbus_intr_disestablish(ic, hdl);
325 1.33 jmcneill }
326 1.33 jmcneill }
327 1.33 jmcneill if (!LIST_EMPTY(&ic->ic_proc_list)) {
328 1.33 jmcneill device_printf(self, "WARNING: proc handler list not empty\n");
329 1.33 jmcneill while (!LIST_EMPTY(&ic->ic_proc_list)) {
330 1.33 jmcneill hdl = LIST_FIRST(&ic->ic_proc_list);
331 1.33 jmcneill iic_smbus_intr_disestablish_proc(ic, hdl);
332 1.33 jmcneill }
333 1.33 jmcneill }
334 1.33 jmcneill
335 1.33 jmcneill pmf_device_deregister(self);
336 1.33 jmcneill
337 1.33 jmcneill return 0;
338 1.33 jmcneill }
339 1.33 jmcneill
340 1.11 jmcneill static void
341 1.14 ad iic_smbus_intr_thread(void *aux)
342 1.11 jmcneill {
343 1.11 jmcneill i2c_tag_t ic;
344 1.11 jmcneill struct ic_intr_list *il;
345 1.11 jmcneill
346 1.11 jmcneill ic = (i2c_tag_t)aux;
347 1.11 jmcneill ic->ic_running = 1;
348 1.11 jmcneill ic->ic_pending = 0;
349 1.11 jmcneill
350 1.11 jmcneill while (ic->ic_running) {
351 1.11 jmcneill if (ic->ic_pending == 0)
352 1.41 martin tsleep(ic, PZERO, "iicintr", hz);
353 1.11 jmcneill if (ic->ic_pending > 0) {
354 1.11 jmcneill LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
355 1.11 jmcneill (*il->il_intr)(il->il_intrarg);
356 1.11 jmcneill }
357 1.11 jmcneill ic->ic_pending--;
358 1.11 jmcneill }
359 1.11 jmcneill }
360 1.11 jmcneill
361 1.11 jmcneill kthread_exit(0);
362 1.11 jmcneill }
363 1.11 jmcneill
364 1.11 jmcneill void *
365 1.11 jmcneill iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
366 1.11 jmcneill {
367 1.11 jmcneill struct ic_intr_list *il;
368 1.11 jmcneill
369 1.11 jmcneill il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
370 1.11 jmcneill if (il == NULL)
371 1.11 jmcneill return NULL;
372 1.28 mbalmer
373 1.11 jmcneill il->il_intr = intr;
374 1.11 jmcneill il->il_intrarg = intrarg;
375 1.11 jmcneill
376 1.11 jmcneill LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
377 1.11 jmcneill
378 1.11 jmcneill return il;
379 1.11 jmcneill }
380 1.11 jmcneill
381 1.11 jmcneill void
382 1.11 jmcneill iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
383 1.11 jmcneill {
384 1.11 jmcneill struct ic_intr_list *il;
385 1.11 jmcneill
386 1.11 jmcneill il = (struct ic_intr_list *)hdl;
387 1.11 jmcneill
388 1.11 jmcneill LIST_REMOVE(il, il_next);
389 1.11 jmcneill free(il, M_DEVBUF);
390 1.11 jmcneill
391 1.11 jmcneill return;
392 1.11 jmcneill }
393 1.11 jmcneill
394 1.11 jmcneill void *
395 1.11 jmcneill iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
396 1.11 jmcneill {
397 1.11 jmcneill struct ic_intr_list *il;
398 1.11 jmcneill
399 1.11 jmcneill il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
400 1.11 jmcneill if (il == NULL)
401 1.11 jmcneill return NULL;
402 1.28 mbalmer
403 1.11 jmcneill il->il_intr = intr;
404 1.11 jmcneill il->il_intrarg = intrarg;
405 1.11 jmcneill
406 1.11 jmcneill LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
407 1.11 jmcneill
408 1.11 jmcneill return il;
409 1.11 jmcneill }
410 1.11 jmcneill
411 1.11 jmcneill void
412 1.11 jmcneill iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
413 1.11 jmcneill {
414 1.11 jmcneill struct ic_intr_list *il;
415 1.11 jmcneill
416 1.11 jmcneill il = (struct ic_intr_list *)hdl;
417 1.11 jmcneill
418 1.11 jmcneill LIST_REMOVE(il, il_next);
419 1.11 jmcneill free(il, M_DEVBUF);
420 1.11 jmcneill
421 1.11 jmcneill return;
422 1.11 jmcneill }
423 1.11 jmcneill
424 1.11 jmcneill int
425 1.11 jmcneill iic_smbus_intr(i2c_tag_t ic)
426 1.11 jmcneill {
427 1.11 jmcneill struct ic_intr_list *il;
428 1.11 jmcneill
429 1.11 jmcneill LIST_FOREACH(il, &(ic->ic_list), il_next) {
430 1.11 jmcneill (*il->il_intr)(il->il_intrarg);
431 1.11 jmcneill }
432 1.11 jmcneill
433 1.11 jmcneill ic->ic_pending++;
434 1.11 jmcneill wakeup(ic);
435 1.11 jmcneill
436 1.11 jmcneill return 1;
437 1.11 jmcneill }
438 1.11 jmcneill
439 1.24 martin static void
440 1.24 martin iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len,
441 1.24 martin char **buffer)
442 1.24 martin {
443 1.24 martin int count, i;
444 1.24 martin const char *c, *start, **ptr;
445 1.24 martin
446 1.24 martin *buffer = NULL;
447 1.24 martin for (i = count = 0, c = compat; i < len; i++, c++)
448 1.24 martin if (*c == 0)
449 1.24 martin count++;
450 1.24 martin count += 2;
451 1.24 martin ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
452 1.24 martin if (!ptr) return;
453 1.24 martin
454 1.24 martin for (i = count = 0, start = c = compat; i < len; i++, c++) {
455 1.24 martin if (*c == 0) {
456 1.24 martin ptr[count++] = start;
457 1.24 martin start = c+1;
458 1.24 martin }
459 1.24 martin }
460 1.24 martin if (start < compat+len) {
461 1.24 martin /* last string not 0 terminated */
462 1.24 martin size_t l = c-start;
463 1.24 martin *buffer = malloc(l+1, M_TEMP, M_WAITOK);
464 1.24 martin memcpy(*buffer, start, l);
465 1.24 martin (*buffer)[l] = 0;
466 1.24 martin ptr[count++] = *buffer;
467 1.24 martin }
468 1.24 martin ptr[count] = NULL;
469 1.24 martin
470 1.24 martin ia->ia_compat = ptr;
471 1.24 martin ia->ia_ncompat = count;
472 1.24 martin }
473 1.24 martin
474 1.24 martin int
475 1.24 martin iic_compat_match(struct i2c_attach_args *ia, const char ** compats)
476 1.24 martin {
477 1.24 martin int i;
478 1.24 martin
479 1.24 martin for (; compats && *compats; compats++) {
480 1.24 martin for (i = 0; i < ia->ia_ncompat; i++) {
481 1.24 martin if (strcmp(*compats, ia->ia_compat[i]) == 0)
482 1.24 martin return 1;
483 1.24 martin }
484 1.24 martin }
485 1.24 martin return 0;
486 1.24 martin }
487 1.24 martin
488 1.31 jmcneill static int
489 1.31 jmcneill iic_open(dev_t dev, int flag, int fmt, lwp_t *l)
490 1.31 jmcneill {
491 1.31 jmcneill struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
492 1.31 jmcneill
493 1.50 pgoyette mutex_enter(&iic_mtx);
494 1.50 pgoyette if (sc == NULL) {
495 1.50 pgoyette mutex_exit(&iic_mtx);
496 1.31 jmcneill return ENXIO;
497 1.50 pgoyette }
498 1.50 pgoyette iic_refcnt++;
499 1.50 pgoyette mutex_exit(&iic_mtx);
500 1.31 jmcneill
501 1.31 jmcneill return 0;
502 1.31 jmcneill }
503 1.31 jmcneill
504 1.31 jmcneill static int
505 1.31 jmcneill iic_close(dev_t dev, int flag, int fmt, lwp_t *l)
506 1.31 jmcneill {
507 1.50 pgoyette
508 1.50 pgoyette mutex_enter(&iic_mtx);
509 1.50 pgoyette iic_refcnt--;
510 1.50 pgoyette mutex_exit(&iic_mtx);
511 1.50 pgoyette
512 1.31 jmcneill return 0;
513 1.31 jmcneill }
514 1.31 jmcneill
515 1.31 jmcneill static int
516 1.32 jmcneill iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag)
517 1.31 jmcneill {
518 1.31 jmcneill i2c_tag_t ic = sc->sc_tag;
519 1.31 jmcneill uint8_t buf[I2C_EXEC_MAX_BUFLEN];
520 1.34 jmcneill void *cmd = NULL;
521 1.31 jmcneill int error;
522 1.31 jmcneill
523 1.31 jmcneill /* Validate parameters */
524 1.31 jmcneill if (iie->iie_addr > I2C_MAX_ADDR)
525 1.31 jmcneill return EINVAL;
526 1.31 jmcneill if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN ||
527 1.31 jmcneill iie->iie_buflen > I2C_EXEC_MAX_BUFLEN)
528 1.31 jmcneill return EINVAL;
529 1.31 jmcneill if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0)
530 1.31 jmcneill return EINVAL;
531 1.31 jmcneill if (iie->iie_buf != NULL && iie->iie_buflen == 0)
532 1.31 jmcneill return EINVAL;
533 1.32 jmcneill if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0)
534 1.32 jmcneill return EBADF;
535 1.31 jmcneill
536 1.31 jmcneill #if 0
537 1.31 jmcneill /* Disallow userspace access to devices that have drivers attached. */
538 1.31 jmcneill if (sc->sc_devices[iie->iie_addr] != NULL)
539 1.31 jmcneill return EBUSY;
540 1.31 jmcneill #endif
541 1.31 jmcneill
542 1.31 jmcneill if (iie->iie_cmd != NULL) {
543 1.34 jmcneill cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP);
544 1.34 jmcneill if (cmd == NULL)
545 1.34 jmcneill return ENOMEM;
546 1.31 jmcneill error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen);
547 1.48 christos if (error)
548 1.48 christos goto out;
549 1.31 jmcneill }
550 1.31 jmcneill
551 1.46 jakllsch if (iie->iie_buf != NULL && I2C_OP_WRITE_P(iie->iie_op)) {
552 1.46 jakllsch error = copyin(iie->iie_buf, buf, iie->iie_buflen);
553 1.48 christos if (error)
554 1.48 christos goto out;
555 1.46 jakllsch }
556 1.46 jakllsch
557 1.31 jmcneill iic_acquire_bus(ic, 0);
558 1.31 jmcneill error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen,
559 1.31 jmcneill buf, iie->iie_buflen, 0);
560 1.31 jmcneill iic_release_bus(ic, 0);
561 1.31 jmcneill
562 1.36 jmcneill /*
563 1.36 jmcneill * Some drivers return error codes on failure, and others return -1.
564 1.36 jmcneill */
565 1.36 jmcneill if (error < 0)
566 1.36 jmcneill error = EIO;
567 1.36 jmcneill
568 1.48 christos out:
569 1.34 jmcneill if (cmd)
570 1.34 jmcneill kmem_free(cmd, iie->iie_cmdlen);
571 1.34 jmcneill
572 1.31 jmcneill if (error)
573 1.31 jmcneill return error;
574 1.31 jmcneill
575 1.46 jakllsch if (iie->iie_buf != NULL && I2C_OP_READ_P(iie->iie_op))
576 1.31 jmcneill error = copyout(buf, iie->iie_buf, iie->iie_buflen);
577 1.31 jmcneill
578 1.31 jmcneill return error;
579 1.31 jmcneill }
580 1.31 jmcneill
581 1.31 jmcneill static int
582 1.31 jmcneill iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
583 1.31 jmcneill {
584 1.31 jmcneill struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
585 1.31 jmcneill
586 1.31 jmcneill if (sc == NULL)
587 1.31 jmcneill return ENXIO;
588 1.31 jmcneill
589 1.31 jmcneill switch (cmd) {
590 1.31 jmcneill case I2C_IOCTL_EXEC:
591 1.32 jmcneill return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag);
592 1.31 jmcneill default:
593 1.31 jmcneill return ENODEV;
594 1.31 jmcneill }
595 1.31 jmcneill }
596 1.31 jmcneill
597 1.24 martin
598 1.26 jmcneill CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc),
599 1.33 jmcneill iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach);
600 1.28 mbalmer
601 1.52 pgoyette MODULE(MODULE_CLASS_DRIVER, iic, "i2cexec,i2c_bitbang");
602 1.28 mbalmer
603 1.28 mbalmer #ifdef _MODULE
604 1.28 mbalmer #include "ioconf.c"
605 1.28 mbalmer #endif
606 1.28 mbalmer
607 1.50 pgoyette int
608 1.50 pgoyette iic_init(void)
609 1.50 pgoyette {
610 1.50 pgoyette
611 1.50 pgoyette mutex_init(&iic_mtx, MUTEX_DEFAULT, IPL_NONE);
612 1.50 pgoyette iic_refcnt = 0;
613 1.50 pgoyette return 0;
614 1.50 pgoyette }
615 1.50 pgoyette
616 1.28 mbalmer static int
617 1.28 mbalmer iic_modcmd(modcmd_t cmd, void *opaque)
618 1.28 mbalmer {
619 1.50 pgoyette #ifdef _MODULE
620 1.50 pgoyette int bmajor, cmajor;
621 1.50 pgoyette #endif
622 1.28 mbalmer int error;
623 1.28 mbalmer
624 1.28 mbalmer error = 0;
625 1.28 mbalmer switch (cmd) {
626 1.28 mbalmer case MODULE_CMD_INIT:
627 1.50 pgoyette RUN_ONCE(&iic_once, iic_init);
628 1.50 pgoyette
629 1.28 mbalmer #ifdef _MODULE
630 1.50 pgoyette mutex_enter(&iic_mtx);
631 1.50 pgoyette bmajor = cmajor = -1;
632 1.50 pgoyette error = devsw_attach("iic", NULL, &bmajor,
633 1.50 pgoyette &iic_cdevsw, &cmajor);
634 1.50 pgoyette if (error != 0) {
635 1.50 pgoyette mutex_exit(&iic_mtx);
636 1.50 pgoyette break;
637 1.50 pgoyette }
638 1.28 mbalmer error = config_init_component(cfdriver_ioconf_iic,
639 1.28 mbalmer cfattach_ioconf_iic, cfdata_ioconf_iic);
640 1.50 pgoyette if (error) {
641 1.28 mbalmer aprint_error("%s: unable to init component\n",
642 1.28 mbalmer iic_cd.cd_name);
643 1.50 pgoyette (void)devsw_detach(NULL, &iic_cdevsw);
644 1.50 pgoyette }
645 1.50 pgoyette mutex_exit(&iic_mtx);
646 1.28 mbalmer #endif
647 1.28 mbalmer break;
648 1.28 mbalmer case MODULE_CMD_FINI:
649 1.50 pgoyette mutex_enter(&iic_mtx);
650 1.50 pgoyette if (iic_refcnt != 0) {
651 1.50 pgoyette mutex_exit(&iic_mtx);
652 1.50 pgoyette return EBUSY;
653 1.50 pgoyette }
654 1.28 mbalmer #ifdef _MODULE
655 1.50 pgoyette error = config_fini_component(cfdriver_ioconf_iic,
656 1.28 mbalmer cfattach_ioconf_iic, cfdata_ioconf_iic);
657 1.50 pgoyette if (error != 0) {
658 1.50 pgoyette mutex_exit(&iic_mtx);
659 1.50 pgoyette break;
660 1.50 pgoyette }
661 1.50 pgoyette error = devsw_detach(NULL, &iic_cdevsw);
662 1.50 pgoyette if (error != 0)
663 1.50 pgoyette config_init_component(cfdriver_ioconf_iic,
664 1.50 pgoyette cfattach_ioconf_iic, cfdata_ioconf_iic);
665 1.28 mbalmer #endif
666 1.50 pgoyette mutex_exit(&iic_mtx);
667 1.28 mbalmer break;
668 1.28 mbalmer default:
669 1.28 mbalmer error = ENOTTY;
670 1.28 mbalmer }
671 1.28 mbalmer return error;
672 1.28 mbalmer }
673