i2c.c revision 1.40 1 1.40 soren /* $NetBSD: i2c.c,v 1.40 2013/08/07 19:38:45 soren 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.18 lukem #include <sys/cdefs.h>
39 1.40 soren __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.40 2013/08/07 19:38:45 soren Exp $");
40 1.18 lukem
41 1.1 thorpej #include <sys/param.h>
42 1.1 thorpej #include <sys/systm.h>
43 1.1 thorpej #include <sys/device.h>
44 1.1 thorpej #include <sys/event.h>
45 1.1 thorpej #include <sys/conf.h>
46 1.11 jmcneill #include <sys/malloc.h>
47 1.34 jmcneill #include <sys/kmem.h>
48 1.11 jmcneill #include <sys/kthread.h>
49 1.11 jmcneill #include <sys/proc.h>
50 1.11 jmcneill #include <sys/kernel.h>
51 1.32 jmcneill #include <sys/fcntl.h>
52 1.28 mbalmer #include <sys/module.h>
53 1.1 thorpej
54 1.1 thorpej #include <dev/i2c/i2cvar.h>
55 1.1 thorpej
56 1.1 thorpej #include "locators.h"
57 1.1 thorpej
58 1.27 pgoyette #define I2C_MAX_ADDR 0x3ff /* 10-bit address, max */
59 1.27 pgoyette
60 1.1 thorpej struct iic_softc {
61 1.1 thorpej i2c_tag_t sc_tag;
62 1.6 jmcneill int sc_type;
63 1.27 pgoyette device_t sc_devices[I2C_MAX_ADDR + 1];
64 1.1 thorpej };
65 1.1 thorpej
66 1.31 jmcneill static dev_type_open(iic_open);
67 1.31 jmcneill static dev_type_close(iic_close);
68 1.31 jmcneill static dev_type_ioctl(iic_ioctl);
69 1.31 jmcneill
70 1.31 jmcneill const struct cdevsw iic_cdevsw = {
71 1.31 jmcneill iic_open, iic_close, noread, nowrite, iic_ioctl,
72 1.31 jmcneill nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
73 1.31 jmcneill };
74 1.31 jmcneill
75 1.31 jmcneill extern struct cfdriver iic_cd;
76 1.31 jmcneill
77 1.11 jmcneill static void iic_smbus_intr_thread(void *);
78 1.24 martin static void iic_fill_compat(struct i2c_attach_args*, const char*,
79 1.24 martin size_t, char **);
80 1.11 jmcneill
81 1.1 thorpej static int
82 1.24 martin iic_print_direct(void *aux, const char *pnp)
83 1.24 martin {
84 1.24 martin struct i2c_attach_args *ia = aux;
85 1.24 martin
86 1.24 martin if (pnp != NULL)
87 1.24 martin aprint_normal("%s at %s addr 0x%02x", ia->ia_name, pnp,
88 1.24 martin ia->ia_addr);
89 1.24 martin else
90 1.24 martin aprint_normal(" addr 0x%02x", ia->ia_addr);
91 1.24 martin
92 1.24 martin return UNCONF;
93 1.24 martin }
94 1.24 martin
95 1.24 martin static int
96 1.10 christos iic_print(void *aux, const char *pnp)
97 1.1 thorpej {
98 1.1 thorpej struct i2c_attach_args *ia = aux;
99 1.1 thorpej
100 1.6 jmcneill if (ia->ia_addr != (i2c_addr_t)-1)
101 1.6 jmcneill aprint_normal(" addr 0x%x", ia->ia_addr);
102 1.1 thorpej
103 1.30 mbalmer return UNCONF;
104 1.1 thorpej }
105 1.1 thorpej
106 1.1 thorpej static int
107 1.20 xtraeme iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
108 1.1 thorpej {
109 1.20 xtraeme struct iic_softc *sc = device_private(parent);
110 1.1 thorpej struct i2c_attach_args ia;
111 1.1 thorpej
112 1.1 thorpej ia.ia_tag = sc->sc_tag;
113 1.1 thorpej ia.ia_size = cf->cf_loc[IICCF_SIZE];
114 1.6 jmcneill ia.ia_type = sc->sc_type;
115 1.1 thorpej
116 1.25 njoly ia.ia_name = NULL;
117 1.25 njoly ia.ia_ncompat = 0;
118 1.25 njoly ia.ia_compat = NULL;
119 1.25 njoly
120 1.40 soren for (ia.ia_addr = 0; ia.ia_addr <= I2C_MAX_ADDR; ia.ia_addr++) {
121 1.40 soren if (sc->sc_devices[ia.ia_addr] != NULL)
122 1.40 soren continue;
123 1.40 soren
124 1.40 soren if (cf->cf_loc[IICCF_ADDR] != -1 &&
125 1.40 soren cf->cf_loc[IICCF_ADDR] != ia.ia_addr)
126 1.40 soren continue;
127 1.40 soren
128 1.40 soren if (config_match(parent, cf, &ia) > 0)
129 1.27 pgoyette sc->sc_devices[ia.ia_addr] =
130 1.27 pgoyette config_attach(parent, cf, &ia, iic_print);
131 1.27 pgoyette }
132 1.40 soren
133 1.30 mbalmer return 0;
134 1.1 thorpej }
135 1.1 thorpej
136 1.27 pgoyette static void
137 1.27 pgoyette iic_child_detach(device_t parent, device_t child)
138 1.27 pgoyette {
139 1.27 pgoyette struct iic_softc *sc = device_private(parent);
140 1.27 pgoyette int i;
141 1.27 pgoyette
142 1.27 pgoyette for (i = 0; i <= I2C_MAX_ADDR; i++)
143 1.27 pgoyette if (sc->sc_devices[i] == child) {
144 1.27 pgoyette sc->sc_devices[i] = NULL;
145 1.27 pgoyette break;
146 1.40 soren }
147 1.27 pgoyette }
148 1.27 pgoyette
149 1.1 thorpej static int
150 1.26 jmcneill iic_rescan(device_t self, const char *ifattr, const int *locators)
151 1.26 jmcneill {
152 1.26 jmcneill config_search_ia(iic_search, self, ifattr, NULL);
153 1.26 jmcneill return 0;
154 1.26 jmcneill }
155 1.26 jmcneill
156 1.26 jmcneill static int
157 1.20 xtraeme iic_match(device_t parent, cfdata_t cf, void *aux)
158 1.1 thorpej {
159 1.1 thorpej
160 1.30 mbalmer return 1;
161 1.1 thorpej }
162 1.1 thorpej
163 1.1 thorpej static void
164 1.20 xtraeme iic_attach(device_t parent, device_t self, void *aux)
165 1.1 thorpej {
166 1.7 thorpej struct iic_softc *sc = device_private(self);
167 1.1 thorpej struct i2cbus_attach_args *iba = aux;
168 1.24 martin prop_array_t child_devices;
169 1.24 martin char *buf;
170 1.14 ad i2c_tag_t ic;
171 1.14 ad int rv;
172 1.1 thorpej
173 1.33 jmcneill aprint_naive("\n");
174 1.1 thorpej aprint_normal(": I2C bus\n");
175 1.1 thorpej
176 1.1 thorpej sc->sc_tag = iba->iba_tag;
177 1.6 jmcneill sc->sc_type = iba->iba_type;
178 1.14 ad ic = sc->sc_tag;
179 1.19 cegger ic->ic_devname = device_xname(self);
180 1.11 jmcneill
181 1.13 jmcneill LIST_INIT(&(sc->sc_tag->ic_list));
182 1.13 jmcneill LIST_INIT(&(sc->sc_tag->ic_proc_list));
183 1.14 ad
184 1.33 jmcneill rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
185 1.33 jmcneill iic_smbus_intr_thread, ic, &ic->ic_intr_thread,
186 1.33 jmcneill "%s", ic->ic_devname);
187 1.14 ad if (rv)
188 1.20 xtraeme aprint_error_dev(self, "unable to create intr thread\n");
189 1.1 thorpej
190 1.17 jmcneill if (!pmf_device_register(self, NULL, NULL))
191 1.17 jmcneill aprint_error_dev(self, "couldn't establish power handler\n");
192 1.17 jmcneill
193 1.24 martin child_devices = prop_dictionary_get(device_properties(parent),
194 1.24 martin "i2c-child-devices");
195 1.24 martin if (child_devices) {
196 1.24 martin unsigned int i, count;
197 1.24 martin prop_dictionary_t dev;
198 1.24 martin prop_data_t cdata;
199 1.24 martin uint32_t addr, size;
200 1.24 martin uint64_t cookie;
201 1.24 martin const char *name;
202 1.24 martin struct i2c_attach_args ia;
203 1.24 martin int loc[2];
204 1.24 martin
205 1.24 martin memset(loc, 0, sizeof loc);
206 1.24 martin count = prop_array_count(child_devices);
207 1.24 martin for (i = 0; i < count; i++) {
208 1.24 martin dev = prop_array_get(child_devices, i);
209 1.24 martin if (!dev) continue;
210 1.24 martin if (!prop_dictionary_get_cstring_nocopy(
211 1.24 martin dev, "name", &name))
212 1.24 martin continue;
213 1.24 martin if (!prop_dictionary_get_uint32(dev, "addr", &addr))
214 1.24 martin continue;
215 1.24 martin if (!prop_dictionary_get_uint64(dev, "cookie", &cookie))
216 1.24 martin cookie = 0;
217 1.24 martin loc[0] = addr;
218 1.24 martin if (prop_dictionary_get_uint32(dev, "size", &size))
219 1.24 martin loc[1] = size;
220 1.24 martin else
221 1.24 martin loc[1] = -1;
222 1.24 martin
223 1.24 martin memset(&ia, 0, sizeof ia);
224 1.24 martin ia.ia_addr = addr;
225 1.24 martin ia.ia_type = sc->sc_type;
226 1.24 martin ia.ia_tag = ic;
227 1.24 martin ia.ia_name = name;
228 1.24 martin ia.ia_cookie = cookie;
229 1.39 jdc ia.ia_size = size;
230 1.24 martin
231 1.24 martin buf = NULL;
232 1.24 martin cdata = prop_dictionary_get(dev, "compatible");
233 1.24 martin if (cdata)
234 1.24 martin iic_fill_compat(&ia,
235 1.24 martin prop_data_data_nocopy(cdata),
236 1.24 martin prop_data_size(cdata), &buf);
237 1.24 martin
238 1.33 jmcneill if (addr > I2C_MAX_ADDR) {
239 1.33 jmcneill aprint_error_dev(self,
240 1.33 jmcneill "WARNING: ignoring bad device address "
241 1.33 jmcneill "@ 0x%02x\n", addr);
242 1.33 jmcneill } else if (sc->sc_devices[addr] == NULL) {
243 1.33 jmcneill sc->sc_devices[addr] =
244 1.33 jmcneill config_found_sm_loc(self, "iic", loc, &ia,
245 1.33 jmcneill iic_print_direct, NULL);
246 1.33 jmcneill }
247 1.24 martin
248 1.24 martin if (ia.ia_compat)
249 1.24 martin free(ia.ia_compat, M_TEMP);
250 1.24 martin if (buf)
251 1.24 martin free(buf, M_TEMP);
252 1.24 martin }
253 1.24 martin } else {
254 1.24 martin /*
255 1.24 martin * Attach all i2c devices described in the kernel
256 1.24 martin * configuration file.
257 1.24 martin */
258 1.26 jmcneill iic_rescan(self, "iic", NULL);
259 1.24 martin }
260 1.1 thorpej }
261 1.1 thorpej
262 1.33 jmcneill static int
263 1.33 jmcneill iic_detach(device_t self, int flags)
264 1.33 jmcneill {
265 1.33 jmcneill struct iic_softc *sc = device_private(self);
266 1.33 jmcneill i2c_tag_t ic = sc->sc_tag;
267 1.33 jmcneill int i, error;
268 1.33 jmcneill void *hdl;
269 1.33 jmcneill
270 1.33 jmcneill for (i = 0; i <= I2C_MAX_ADDR; i++) {
271 1.33 jmcneill if (sc->sc_devices[i]) {
272 1.33 jmcneill error = config_detach(sc->sc_devices[i], flags);
273 1.33 jmcneill if (error)
274 1.33 jmcneill return error;
275 1.33 jmcneill }
276 1.33 jmcneill }
277 1.33 jmcneill
278 1.33 jmcneill if (ic->ic_running) {
279 1.33 jmcneill ic->ic_running = 0;
280 1.33 jmcneill wakeup(ic);
281 1.33 jmcneill kthread_join(ic->ic_intr_thread);
282 1.33 jmcneill }
283 1.33 jmcneill
284 1.33 jmcneill if (!LIST_EMPTY(&ic->ic_list)) {
285 1.33 jmcneill device_printf(self, "WARNING: intr handler list not empty\n");
286 1.33 jmcneill while (!LIST_EMPTY(&ic->ic_list)) {
287 1.33 jmcneill hdl = LIST_FIRST(&ic->ic_list);
288 1.33 jmcneill iic_smbus_intr_disestablish(ic, hdl);
289 1.33 jmcneill }
290 1.33 jmcneill }
291 1.33 jmcneill if (!LIST_EMPTY(&ic->ic_proc_list)) {
292 1.33 jmcneill device_printf(self, "WARNING: proc handler list not empty\n");
293 1.33 jmcneill while (!LIST_EMPTY(&ic->ic_proc_list)) {
294 1.33 jmcneill hdl = LIST_FIRST(&ic->ic_proc_list);
295 1.33 jmcneill iic_smbus_intr_disestablish_proc(ic, hdl);
296 1.33 jmcneill }
297 1.33 jmcneill }
298 1.33 jmcneill
299 1.33 jmcneill pmf_device_deregister(self);
300 1.33 jmcneill
301 1.33 jmcneill return 0;
302 1.33 jmcneill }
303 1.33 jmcneill
304 1.11 jmcneill static void
305 1.14 ad iic_smbus_intr_thread(void *aux)
306 1.11 jmcneill {
307 1.11 jmcneill i2c_tag_t ic;
308 1.11 jmcneill struct ic_intr_list *il;
309 1.11 jmcneill int rv;
310 1.11 jmcneill
311 1.11 jmcneill ic = (i2c_tag_t)aux;
312 1.11 jmcneill ic->ic_running = 1;
313 1.11 jmcneill ic->ic_pending = 0;
314 1.11 jmcneill
315 1.11 jmcneill while (ic->ic_running) {
316 1.11 jmcneill if (ic->ic_pending == 0)
317 1.11 jmcneill rv = tsleep(ic, PZERO, "iicintr", hz);
318 1.11 jmcneill if (ic->ic_pending > 0) {
319 1.11 jmcneill LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
320 1.11 jmcneill (*il->il_intr)(il->il_intrarg);
321 1.11 jmcneill }
322 1.11 jmcneill ic->ic_pending--;
323 1.11 jmcneill }
324 1.11 jmcneill }
325 1.11 jmcneill
326 1.11 jmcneill kthread_exit(0);
327 1.11 jmcneill }
328 1.11 jmcneill
329 1.11 jmcneill void *
330 1.11 jmcneill iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
331 1.11 jmcneill {
332 1.11 jmcneill struct ic_intr_list *il;
333 1.11 jmcneill
334 1.11 jmcneill il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
335 1.11 jmcneill if (il == NULL)
336 1.11 jmcneill return NULL;
337 1.28 mbalmer
338 1.11 jmcneill il->il_intr = intr;
339 1.11 jmcneill il->il_intrarg = intrarg;
340 1.11 jmcneill
341 1.11 jmcneill LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
342 1.11 jmcneill
343 1.11 jmcneill return il;
344 1.11 jmcneill }
345 1.11 jmcneill
346 1.11 jmcneill void
347 1.11 jmcneill iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
348 1.11 jmcneill {
349 1.11 jmcneill struct ic_intr_list *il;
350 1.11 jmcneill
351 1.11 jmcneill il = (struct ic_intr_list *)hdl;
352 1.11 jmcneill
353 1.11 jmcneill LIST_REMOVE(il, il_next);
354 1.11 jmcneill free(il, M_DEVBUF);
355 1.11 jmcneill
356 1.11 jmcneill return;
357 1.11 jmcneill }
358 1.11 jmcneill
359 1.11 jmcneill void *
360 1.11 jmcneill iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
361 1.11 jmcneill {
362 1.11 jmcneill struct ic_intr_list *il;
363 1.11 jmcneill
364 1.11 jmcneill il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
365 1.11 jmcneill if (il == NULL)
366 1.11 jmcneill return NULL;
367 1.28 mbalmer
368 1.11 jmcneill il->il_intr = intr;
369 1.11 jmcneill il->il_intrarg = intrarg;
370 1.11 jmcneill
371 1.11 jmcneill LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
372 1.11 jmcneill
373 1.11 jmcneill return il;
374 1.11 jmcneill }
375 1.11 jmcneill
376 1.11 jmcneill void
377 1.11 jmcneill iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
378 1.11 jmcneill {
379 1.11 jmcneill struct ic_intr_list *il;
380 1.11 jmcneill
381 1.11 jmcneill il = (struct ic_intr_list *)hdl;
382 1.11 jmcneill
383 1.11 jmcneill LIST_REMOVE(il, il_next);
384 1.11 jmcneill free(il, M_DEVBUF);
385 1.11 jmcneill
386 1.11 jmcneill return;
387 1.11 jmcneill }
388 1.11 jmcneill
389 1.11 jmcneill int
390 1.11 jmcneill iic_smbus_intr(i2c_tag_t ic)
391 1.11 jmcneill {
392 1.11 jmcneill struct ic_intr_list *il;
393 1.11 jmcneill
394 1.11 jmcneill LIST_FOREACH(il, &(ic->ic_list), il_next) {
395 1.11 jmcneill (*il->il_intr)(il->il_intrarg);
396 1.11 jmcneill }
397 1.11 jmcneill
398 1.11 jmcneill ic->ic_pending++;
399 1.11 jmcneill wakeup(ic);
400 1.11 jmcneill
401 1.11 jmcneill return 1;
402 1.11 jmcneill }
403 1.11 jmcneill
404 1.24 martin static void
405 1.24 martin iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len,
406 1.24 martin char **buffer)
407 1.24 martin {
408 1.24 martin int count, i;
409 1.24 martin const char *c, *start, **ptr;
410 1.24 martin
411 1.24 martin *buffer = NULL;
412 1.24 martin for (i = count = 0, c = compat; i < len; i++, c++)
413 1.24 martin if (*c == 0)
414 1.24 martin count++;
415 1.24 martin count += 2;
416 1.24 martin ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
417 1.24 martin if (!ptr) return;
418 1.24 martin
419 1.24 martin for (i = count = 0, start = c = compat; i < len; i++, c++) {
420 1.24 martin if (*c == 0) {
421 1.24 martin ptr[count++] = start;
422 1.24 martin start = c+1;
423 1.24 martin }
424 1.24 martin }
425 1.24 martin if (start < compat+len) {
426 1.24 martin /* last string not 0 terminated */
427 1.24 martin size_t l = c-start;
428 1.24 martin *buffer = malloc(l+1, M_TEMP, M_WAITOK);
429 1.24 martin memcpy(*buffer, start, l);
430 1.24 martin (*buffer)[l] = 0;
431 1.24 martin ptr[count++] = *buffer;
432 1.24 martin }
433 1.24 martin ptr[count] = NULL;
434 1.24 martin
435 1.24 martin ia->ia_compat = ptr;
436 1.24 martin ia->ia_ncompat = count;
437 1.24 martin }
438 1.24 martin
439 1.24 martin int
440 1.24 martin iic_compat_match(struct i2c_attach_args *ia, const char ** compats)
441 1.24 martin {
442 1.24 martin int i;
443 1.24 martin
444 1.24 martin for (; compats && *compats; compats++) {
445 1.24 martin for (i = 0; i < ia->ia_ncompat; i++) {
446 1.24 martin if (strcmp(*compats, ia->ia_compat[i]) == 0)
447 1.24 martin return 1;
448 1.24 martin }
449 1.24 martin }
450 1.24 martin return 0;
451 1.24 martin }
452 1.24 martin
453 1.31 jmcneill static int
454 1.31 jmcneill iic_open(dev_t dev, int flag, int fmt, lwp_t *l)
455 1.31 jmcneill {
456 1.31 jmcneill struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
457 1.31 jmcneill
458 1.31 jmcneill if (sc == NULL)
459 1.31 jmcneill return ENXIO;
460 1.31 jmcneill
461 1.31 jmcneill return 0;
462 1.31 jmcneill }
463 1.31 jmcneill
464 1.31 jmcneill static int
465 1.31 jmcneill iic_close(dev_t dev, int flag, int fmt, lwp_t *l)
466 1.31 jmcneill {
467 1.31 jmcneill return 0;
468 1.31 jmcneill }
469 1.31 jmcneill
470 1.31 jmcneill static int
471 1.32 jmcneill iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag)
472 1.31 jmcneill {
473 1.31 jmcneill i2c_tag_t ic = sc->sc_tag;
474 1.31 jmcneill uint8_t buf[I2C_EXEC_MAX_BUFLEN];
475 1.34 jmcneill void *cmd = NULL;
476 1.31 jmcneill int error;
477 1.31 jmcneill
478 1.31 jmcneill /* Validate parameters */
479 1.31 jmcneill if (iie->iie_addr > I2C_MAX_ADDR)
480 1.31 jmcneill return EINVAL;
481 1.31 jmcneill if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN ||
482 1.31 jmcneill iie->iie_buflen > I2C_EXEC_MAX_BUFLEN)
483 1.31 jmcneill return EINVAL;
484 1.31 jmcneill if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0)
485 1.31 jmcneill return EINVAL;
486 1.31 jmcneill if (iie->iie_buf != NULL && iie->iie_buflen == 0)
487 1.31 jmcneill return EINVAL;
488 1.32 jmcneill if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0)
489 1.32 jmcneill return EBADF;
490 1.31 jmcneill
491 1.31 jmcneill #if 0
492 1.31 jmcneill /* Disallow userspace access to devices that have drivers attached. */
493 1.31 jmcneill if (sc->sc_devices[iie->iie_addr] != NULL)
494 1.31 jmcneill return EBUSY;
495 1.31 jmcneill #endif
496 1.31 jmcneill
497 1.31 jmcneill if (iie->iie_cmd != NULL) {
498 1.34 jmcneill cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP);
499 1.34 jmcneill if (cmd == NULL)
500 1.34 jmcneill return ENOMEM;
501 1.31 jmcneill error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen);
502 1.34 jmcneill if (error) {
503 1.34 jmcneill kmem_free(cmd, iie->iie_cmdlen);
504 1.31 jmcneill return error;
505 1.34 jmcneill }
506 1.31 jmcneill }
507 1.31 jmcneill
508 1.31 jmcneill iic_acquire_bus(ic, 0);
509 1.31 jmcneill error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen,
510 1.31 jmcneill buf, iie->iie_buflen, 0);
511 1.31 jmcneill iic_release_bus(ic, 0);
512 1.31 jmcneill
513 1.36 jmcneill /*
514 1.36 jmcneill * Some drivers return error codes on failure, and others return -1.
515 1.36 jmcneill */
516 1.36 jmcneill if (error < 0)
517 1.36 jmcneill error = EIO;
518 1.36 jmcneill
519 1.34 jmcneill if (cmd)
520 1.34 jmcneill kmem_free(cmd, iie->iie_cmdlen);
521 1.34 jmcneill
522 1.31 jmcneill if (error)
523 1.31 jmcneill return error;
524 1.31 jmcneill
525 1.31 jmcneill if (iie->iie_buf)
526 1.31 jmcneill error = copyout(buf, iie->iie_buf, iie->iie_buflen);
527 1.31 jmcneill
528 1.31 jmcneill return error;
529 1.31 jmcneill }
530 1.31 jmcneill
531 1.31 jmcneill static int
532 1.31 jmcneill iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
533 1.31 jmcneill {
534 1.31 jmcneill struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
535 1.31 jmcneill
536 1.31 jmcneill if (sc == NULL)
537 1.31 jmcneill return ENXIO;
538 1.31 jmcneill
539 1.31 jmcneill switch (cmd) {
540 1.31 jmcneill case I2C_IOCTL_EXEC:
541 1.32 jmcneill return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag);
542 1.31 jmcneill default:
543 1.31 jmcneill return ENODEV;
544 1.31 jmcneill }
545 1.31 jmcneill }
546 1.31 jmcneill
547 1.24 martin
548 1.26 jmcneill CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc),
549 1.33 jmcneill iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach);
550 1.28 mbalmer
551 1.28 mbalmer MODULE(MODULE_CLASS_DRIVER, iic, NULL);
552 1.28 mbalmer
553 1.28 mbalmer #ifdef _MODULE
554 1.28 mbalmer #include "ioconf.c"
555 1.28 mbalmer #endif
556 1.28 mbalmer
557 1.28 mbalmer static int
558 1.28 mbalmer iic_modcmd(modcmd_t cmd, void *opaque)
559 1.28 mbalmer {
560 1.28 mbalmer int error;
561 1.28 mbalmer
562 1.28 mbalmer error = 0;
563 1.28 mbalmer switch (cmd) {
564 1.28 mbalmer case MODULE_CMD_INIT:
565 1.28 mbalmer #ifdef _MODULE
566 1.28 mbalmer error = config_init_component(cfdriver_ioconf_iic,
567 1.28 mbalmer cfattach_ioconf_iic, cfdata_ioconf_iic);
568 1.28 mbalmer if (error)
569 1.28 mbalmer aprint_error("%s: unable to init component\n",
570 1.28 mbalmer iic_cd.cd_name);
571 1.28 mbalmer #endif
572 1.28 mbalmer break;
573 1.28 mbalmer case MODULE_CMD_FINI:
574 1.28 mbalmer #ifdef _MODULE
575 1.28 mbalmer config_fini_component(cfdriver_ioconf_iic,
576 1.28 mbalmer cfattach_ioconf_iic, cfdata_ioconf_iic);
577 1.28 mbalmer #endif
578 1.28 mbalmer break;
579 1.28 mbalmer default:
580 1.28 mbalmer error = ENOTTY;
581 1.28 mbalmer }
582 1.28 mbalmer return error;
583 1.28 mbalmer }
584