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