Home | History | Annotate | Line # | Download | only in i2c
i2c.c revision 1.62
      1  1.62   thorpej /*	$NetBSD: i2c.c,v 1.62 2018/06/16 21:22:13 thorpej 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.62   thorpej __KERNEL_RCSID(0, "$NetBSD: i2c.c,v 1.62 2018/06/16 21:22:13 thorpej 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.56  riastrad #include "ioconf.h"
     63   1.1   thorpej #include "locators.h"
     64   1.1   thorpej 
     65  1.45  jmcneill #ifndef I2C_MAX_ADDR
     66  1.27  pgoyette #define I2C_MAX_ADDR	0x3ff	/* 10-bit address, max */
     67  1.45  jmcneill #endif
     68  1.27  pgoyette 
     69   1.1   thorpej struct iic_softc {
     70  1.61   thorpej 	device_t sc_dev;
     71   1.1   thorpej 	i2c_tag_t sc_tag;
     72   1.6  jmcneill 	int sc_type;
     73  1.27  pgoyette 	device_t sc_devices[I2C_MAX_ADDR + 1];
     74   1.1   thorpej };
     75   1.1   thorpej 
     76  1.31  jmcneill static dev_type_open(iic_open);
     77  1.31  jmcneill static dev_type_close(iic_close);
     78  1.31  jmcneill static dev_type_ioctl(iic_ioctl);
     79  1.31  jmcneill 
     80  1.50  pgoyette int iic_init(void);
     81  1.50  pgoyette 
     82  1.50  pgoyette kmutex_t iic_mtx;
     83  1.50  pgoyette int iic_refcnt;
     84  1.50  pgoyette 
     85  1.50  pgoyette ONCE_DECL(iic_once);
     86  1.50  pgoyette 
     87  1.31  jmcneill const struct cdevsw iic_cdevsw = {
     88  1.43  dholland 	.d_open = iic_open,
     89  1.43  dholland 	.d_close = iic_close,
     90  1.43  dholland 	.d_read = noread,
     91  1.43  dholland 	.d_write = nowrite,
     92  1.43  dholland 	.d_ioctl = iic_ioctl,
     93  1.43  dholland 	.d_stop = nostop,
     94  1.43  dholland 	.d_tty = notty,
     95  1.43  dholland 	.d_poll = nopoll,
     96  1.43  dholland 	.d_mmap = nommap,
     97  1.43  dholland 	.d_kqfilter = nokqfilter,
     98  1.44  dholland 	.d_discard = nodiscard,
     99  1.43  dholland 	.d_flag = D_OTHER
    100  1.31  jmcneill };
    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.62   thorpej 		aprint_normal("%s at %s addr 0x%02x",
    113  1.62   thorpej 			      ia->ia_name ? ia->ia_name : "(unknown)",
    114  1.62   thorpej 			      pnp, ia->ia_addr);
    115  1.24    martin 	else
    116  1.24    martin 		aprint_normal(" addr 0x%02x", ia->ia_addr);
    117  1.24    martin 
    118  1.24    martin 	return UNCONF;
    119  1.24    martin }
    120  1.24    martin 
    121  1.24    martin static int
    122  1.10  christos iic_print(void *aux, const char *pnp)
    123   1.1   thorpej {
    124   1.1   thorpej 	struct i2c_attach_args *ia = aux;
    125   1.1   thorpej 
    126  1.58   thorpej 	if (ia->ia_addr != (i2c_addr_t)IICCF_ADDR_DEFAULT)
    127   1.6  jmcneill 		aprint_normal(" addr 0x%x", ia->ia_addr);
    128   1.1   thorpej 
    129  1.30   mbalmer 	return UNCONF;
    130   1.1   thorpej }
    131   1.1   thorpej 
    132  1.61   thorpej static bool
    133  1.61   thorpej iic_is_special_address(i2c_addr_t addr)
    134  1.61   thorpej {
    135  1.61   thorpej 
    136  1.61   thorpej 	/*
    137  1.61   thorpej 	 * See: https://www.i2c-bus.org/addressing/
    138  1.61   thorpej 	 */
    139  1.61   thorpej 
    140  1.61   thorpej 	/* General Call (read) / Start Byte (write) */
    141  1.61   thorpej 	if (addr == 0x00)
    142  1.61   thorpej 		return (true);
    143  1.61   thorpej 
    144  1.61   thorpej 	/* CBUS Addresses */
    145  1.61   thorpej 	if (addr == 0x01)
    146  1.61   thorpej 		return (true);
    147  1.61   thorpej 
    148  1.61   thorpej 	/* Reserved for Different Bus Formats */
    149  1.61   thorpej 	if (addr == 0x02)
    150  1.61   thorpej 		return (true);
    151  1.61   thorpej 
    152  1.61   thorpej 	/* Reserved for future purposes */
    153  1.61   thorpej 	if (addr == 0x03)
    154  1.61   thorpej 		return (true);
    155  1.61   thorpej 
    156  1.61   thorpej 	/* High Speed Master Code */
    157  1.61   thorpej 	if ((addr & 0x7c) == 0x04)
    158  1.61   thorpej 		return (true);
    159  1.61   thorpej 
    160  1.61   thorpej 	/* 10-bit Slave Addressing prefix */
    161  1.61   thorpej 	if ((addr & 0x7c) == 0x78)
    162  1.61   thorpej 		return (true);
    163  1.61   thorpej 
    164  1.61   thorpej 	/* Reserved for future purposes */
    165  1.61   thorpej 	if ((addr & 0x7c) == 0x7c)
    166  1.61   thorpej 		return (true);
    167  1.61   thorpej 
    168  1.61   thorpej 	return (false);
    169  1.61   thorpej }
    170  1.61   thorpej 
    171  1.61   thorpej static int
    172  1.61   thorpej iic_probe_none(struct iic_softc *sc,
    173  1.61   thorpej 	       const struct i2c_attach_args *ia, int flags)
    174  1.61   thorpej {
    175  1.61   thorpej 
    176  1.61   thorpej 	return (0);
    177  1.61   thorpej }
    178  1.61   thorpej 
    179  1.61   thorpej static int
    180  1.61   thorpej iic_probe_smbus_quick_write(struct iic_softc *sc,
    181  1.61   thorpej 			    const struct i2c_attach_args *ia, int flags)
    182  1.61   thorpej {
    183  1.61   thorpej 	int error;
    184  1.61   thorpej 
    185  1.61   thorpej 	if ((error = iic_acquire_bus(ia->ia_tag, flags)) == 0) {
    186  1.61   thorpej 		error = iic_smbus_quick_write(ia->ia_tag, ia->ia_addr, flags);
    187  1.61   thorpej 	}
    188  1.61   thorpej 	(void) iic_release_bus(ia->ia_tag, flags);
    189  1.61   thorpej 
    190  1.61   thorpej 	return (error);
    191  1.61   thorpej }
    192  1.61   thorpej 
    193  1.61   thorpej static int
    194  1.61   thorpej iic_probe_smbus_receive_byte(struct iic_softc *sc,
    195  1.61   thorpej 			     const struct i2c_attach_args *ia, int flags)
    196  1.61   thorpej {
    197  1.61   thorpej 	int error;
    198  1.61   thorpej 
    199  1.61   thorpej 	if ((error = iic_acquire_bus(ia->ia_tag, flags)) == 0) {
    200  1.61   thorpej 		uint8_t dummy;
    201  1.61   thorpej 
    202  1.61   thorpej 		error = iic_smbus_receive_byte(ia->ia_tag, ia->ia_addr,
    203  1.61   thorpej 					       &dummy, flags);
    204  1.61   thorpej 	}
    205  1.61   thorpej 	(void) iic_release_bus(ia->ia_tag, flags);
    206  1.61   thorpej 
    207  1.61   thorpej 	return (error);
    208  1.61   thorpej }
    209  1.61   thorpej 
    210  1.61   thorpej static bool
    211  1.61   thorpej iic_indirect_driver_is_whitelisted(struct iic_softc *sc, cfdata_t cf)
    212  1.61   thorpej {
    213  1.61   thorpej 	prop_object_iterator_t iter;
    214  1.61   thorpej 	prop_array_t whitelist;
    215  1.61   thorpej 	prop_string_t pstr;
    216  1.61   thorpej 	prop_type_t ptype;
    217  1.61   thorpej 	bool rv = false;
    218  1.61   thorpej 
    219  1.61   thorpej 	whitelist = prop_dictionary_get(device_properties(sc->sc_dev),
    220  1.61   thorpej 					I2C_PROP_INDIRECT_DEVICE_WHITELIST);
    221  1.61   thorpej 	if (whitelist == NULL) {
    222  1.61   thorpej 		/* No whitelist -> everything allowed */
    223  1.61   thorpej 		return (true);
    224  1.61   thorpej 	}
    225  1.61   thorpej 
    226  1.61   thorpej 	if ((ptype = prop_object_type(whitelist)) != PROP_TYPE_ARRAY) {
    227  1.61   thorpej 		aprint_error_dev(sc->sc_dev,
    228  1.61   thorpej 		    "invalid property type (%d) for '%s'; must be array (%d)\n",
    229  1.61   thorpej 		    ptype, I2C_PROP_INDIRECT_DEVICE_WHITELIST, PROP_TYPE_ARRAY);
    230  1.61   thorpej 		return (false);
    231  1.61   thorpej 	}
    232  1.61   thorpej 
    233  1.61   thorpej 	iter = prop_array_iterator(whitelist);
    234  1.61   thorpej 	while ((pstr = prop_object_iterator_next(iter)) != NULL) {
    235  1.61   thorpej 		if (prop_string_equals_cstring(pstr, cf->cf_name)) {
    236  1.61   thorpej 			rv = true;
    237  1.61   thorpej 			break;
    238  1.61   thorpej 		}
    239  1.61   thorpej 	}
    240  1.61   thorpej 	prop_object_iterator_release(iter);
    241  1.61   thorpej 
    242  1.61   thorpej 	return (rv);
    243  1.61   thorpej }
    244  1.61   thorpej 
    245   1.1   thorpej static int
    246  1.20   xtraeme iic_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    247   1.1   thorpej {
    248  1.20   xtraeme 	struct iic_softc *sc = device_private(parent);
    249   1.1   thorpej 	struct i2c_attach_args ia;
    250  1.61   thorpej 	int (*probe_func)(struct iic_softc *,
    251  1.61   thorpej 			  const struct i2c_attach_args *, int);
    252  1.61   thorpej 	prop_string_t pstr;
    253  1.61   thorpej 	i2c_addr_t first_addr, last_addr;
    254   1.1   thorpej 
    255  1.58   thorpej 	/*
    256  1.61   thorpej 	 * Before we do any more work, consult the allowed-driver
    257  1.61   thorpej 	 * white-list for this bus (if any).
    258  1.58   thorpej 	 */
    259  1.61   thorpej 	if (iic_indirect_driver_is_whitelisted(sc, cf) == false)
    260  1.61   thorpej 		return (0);
    261  1.61   thorpej 
    262  1.61   thorpej 	/* default to "quick write". */
    263  1.61   thorpej 	probe_func = iic_probe_smbus_quick_write;
    264  1.61   thorpej 
    265  1.61   thorpej 	pstr = prop_dictionary_get(device_properties(sc->sc_dev),
    266  1.61   thorpej 				   I2C_PROP_INDIRECT_PROBE_STRATEGY);
    267  1.61   thorpej 	if (pstr == NULL) {
    268  1.61   thorpej 		/* Use the default. */
    269  1.61   thorpej 	} else if (prop_string_equals_cstring(pstr,
    270  1.61   thorpej 					I2C_PROBE_STRATEGY_QUICK_WRITE)) {
    271  1.61   thorpej 		probe_func = iic_probe_smbus_quick_write;
    272  1.61   thorpej 	} else if (prop_string_equals_cstring(pstr,
    273  1.61   thorpej 					I2C_PROBE_STRATEGY_RECEIVE_BYTE)) {
    274  1.61   thorpej 		probe_func = iic_probe_smbus_receive_byte;
    275  1.61   thorpej 	} else if (prop_string_equals_cstring(pstr,
    276  1.61   thorpej 					I2C_PROBE_STRATEGY_NONE)) {
    277  1.61   thorpej 		probe_func = iic_probe_none;
    278  1.61   thorpej 	} else {
    279  1.61   thorpej 		aprint_error_dev(sc->sc_dev,
    280  1.61   thorpej 			"unknown probe strategy '%s'; defaulting to '%s'\n",
    281  1.61   thorpej 			prop_string_cstring_nocopy(pstr),
    282  1.61   thorpej 			I2C_PROBE_STRATEGY_QUICK_WRITE);
    283  1.61   thorpej 
    284  1.61   thorpej 		/* Use the default. */
    285  1.61   thorpej 	}
    286  1.58   thorpej 
    287   1.1   thorpej 	ia.ia_tag = sc->sc_tag;
    288   1.1   thorpej 	ia.ia_size = cf->cf_loc[IICCF_SIZE];
    289   1.6  jmcneill 	ia.ia_type = sc->sc_type;
    290   1.1   thorpej 
    291  1.25     njoly 	ia.ia_name = NULL;
    292  1.25     njoly 	ia.ia_ncompat = 0;
    293  1.25     njoly 	ia.ia_compat = NULL;
    294  1.57    bouyer 	ia.ia_prop = NULL;
    295  1.25     njoly 
    296  1.61   thorpej 	if (cf->cf_loc[IICCF_ADDR] == IICCF_ADDR_DEFAULT) {
    297  1.61   thorpej 		/*
    298  1.61   thorpej 		 * This particular config directive has
    299  1.61   thorpej 		 * wildcarded the address, so we will
    300  1.61   thorpej 		 * scan the entire bus for it.
    301  1.61   thorpej 		 */
    302  1.61   thorpej 		first_addr = 0;
    303  1.61   thorpej 		last_addr = I2C_MAX_ADDR;
    304  1.61   thorpej 	} else {
    305  1.61   thorpej 		/*
    306  1.61   thorpej 		 * This config directive hard-wires the i2c
    307  1.61   thorpej 		 * bus address for the device, so there is
    308  1.61   thorpej 		 * no need to go poking around at any other
    309  1.61   thorpej 		 * addresses.
    310  1.61   thorpej 		 */
    311  1.61   thorpej 		if (cf->cf_loc[IICCF_ADDR] < 0 ||
    312  1.61   thorpej 		    cf->cf_loc[IICCF_ADDR] > I2C_MAX_ADDR) {
    313  1.61   thorpej 			/* Invalid config directive! */
    314  1.61   thorpej 			return (0);
    315  1.61   thorpej 		}
    316  1.61   thorpej 		first_addr = last_addr = cf->cf_loc[IICCF_ADDR];
    317  1.61   thorpej 	}
    318  1.61   thorpej 
    319  1.61   thorpej 	for (ia.ia_addr = first_addr; ia.ia_addr <= last_addr; ia.ia_addr++) {
    320  1.61   thorpej 		int error, match_result;
    321  1.61   thorpej 
    322  1.61   thorpej 		/*
    323  1.61   thorpej 		 * Skip I2C addresses that are reserved for
    324  1.61   thorpej 		 * special purposes.
    325  1.61   thorpej 		 */
    326  1.61   thorpej 		if (iic_is_special_address(ia.ia_addr))
    327  1.61   thorpej 			continue;
    328  1.61   thorpej 
    329  1.61   thorpej 		/*
    330  1.61   thorpej 		 * Skip addresses where a device is already attached.
    331  1.61   thorpej 		 */
    332  1.40     soren 		if (sc->sc_devices[ia.ia_addr] != NULL)
    333  1.40     soren 			continue;
    334  1.40     soren 
    335  1.61   thorpej 		/*
    336  1.61   thorpej 		 * Call the "match" routine for the device.  If that
    337  1.61   thorpej 		 * returns success, then call the probe strategy
    338  1.61   thorpej 		 * function.
    339  1.61   thorpej 		 *
    340  1.61   thorpej 		 * We do it in this order because i2c devices tend
    341  1.61   thorpej 		 * to be found at a small number of possible addresses
    342  1.61   thorpej 		 * (e.g. read-time clocks that are only ever found at
    343  1.61   thorpej 		 * 0x68).  This gives the driver a chance to skip any
    344  1.61   thorpej 		 * address that are not valid for the device, saving
    345  1.61   thorpej 		 * us from having to poke at the bus to see if anything
    346  1.61   thorpej 		 * is there.
    347  1.61   thorpej 		 */
    348  1.61   thorpej 		match_result = config_match(parent, cf, &ia);
    349  1.61   thorpej 		if (match_result <= 0)
    350  1.61   thorpej 			continue;
    351  1.61   thorpej 
    352  1.61   thorpej 		/*
    353  1.61   thorpej 		 * If the quality of the match by the driver was low
    354  1.61   thorpej 		 * (i.e. matched on being a valid address only, didn't
    355  1.61   thorpej 		 * perform any hardware probe), invoke our probe routine
    356  1.61   thorpej 		 * to see if it looks like something is really there.
    357  1.61   thorpej 		 */
    358  1.61   thorpej 		if (match_result == I2C_MATCH_ADDRESS_ONLY &&
    359  1.61   thorpej 		    (error = (*probe_func)(sc, &ia, I2C_F_POLL)) != 0)
    360  1.40     soren 			continue;
    361  1.40     soren 
    362  1.61   thorpej 		sc->sc_devices[ia.ia_addr] =
    363  1.61   thorpej 		    config_attach(parent, cf, &ia, iic_print);
    364  1.27  pgoyette 	}
    365  1.40     soren 
    366  1.30   mbalmer 	return 0;
    367   1.1   thorpej }
    368   1.1   thorpej 
    369  1.27  pgoyette static void
    370  1.27  pgoyette iic_child_detach(device_t parent, device_t child)
    371  1.27  pgoyette {
    372  1.27  pgoyette 	struct iic_softc *sc = device_private(parent);
    373  1.27  pgoyette 	int i;
    374  1.27  pgoyette 
    375  1.27  pgoyette 	for (i = 0; i <= I2C_MAX_ADDR; i++)
    376  1.27  pgoyette 		if (sc->sc_devices[i] == child) {
    377  1.27  pgoyette 			sc->sc_devices[i] = NULL;
    378  1.27  pgoyette 			break;
    379  1.40     soren 		}
    380  1.27  pgoyette }
    381  1.27  pgoyette 
    382   1.1   thorpej static int
    383  1.26  jmcneill iic_rescan(device_t self, const char *ifattr, const int *locators)
    384  1.26  jmcneill {
    385  1.26  jmcneill 	config_search_ia(iic_search, self, ifattr, NULL);
    386  1.26  jmcneill 	return 0;
    387  1.26  jmcneill }
    388  1.26  jmcneill 
    389  1.26  jmcneill static int
    390  1.20   xtraeme iic_match(device_t parent, cfdata_t cf, void *aux)
    391   1.1   thorpej {
    392   1.1   thorpej 
    393  1.30   mbalmer 	return 1;
    394   1.1   thorpej }
    395   1.1   thorpej 
    396   1.1   thorpej static void
    397  1.20   xtraeme iic_attach(device_t parent, device_t self, void *aux)
    398   1.1   thorpej {
    399   1.7   thorpej 	struct iic_softc *sc = device_private(self);
    400   1.1   thorpej 	struct i2cbus_attach_args *iba = aux;
    401  1.24    martin 	prop_array_t child_devices;
    402  1.42       jdc 	prop_dictionary_t props;
    403  1.24    martin 	char *buf;
    404  1.14        ad 	i2c_tag_t ic;
    405  1.14        ad 	int rv;
    406  1.42       jdc 	bool indirect_config;
    407   1.1   thorpej 
    408  1.33  jmcneill 	aprint_naive("\n");
    409   1.1   thorpej 	aprint_normal(": I2C bus\n");
    410   1.1   thorpej 
    411  1.61   thorpej 	sc->sc_dev = self;
    412   1.1   thorpej 	sc->sc_tag = iba->iba_tag;
    413   1.6  jmcneill 	sc->sc_type = iba->iba_type;
    414  1.14        ad 	ic = sc->sc_tag;
    415  1.19    cegger 	ic->ic_devname = device_xname(self);
    416  1.11  jmcneill 
    417  1.13  jmcneill 	LIST_INIT(&(sc->sc_tag->ic_list));
    418  1.13  jmcneill 	LIST_INIT(&(sc->sc_tag->ic_proc_list));
    419  1.14        ad 
    420  1.33  jmcneill 	rv = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
    421  1.33  jmcneill 	    iic_smbus_intr_thread, ic, &ic->ic_intr_thread,
    422  1.33  jmcneill 	    "%s", ic->ic_devname);
    423  1.14        ad 	if (rv)
    424  1.20   xtraeme 		aprint_error_dev(self, "unable to create intr thread\n");
    425   1.1   thorpej 
    426  1.17  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    427  1.17  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    428  1.17  jmcneill 
    429  1.51  jmcneill 	if (iba->iba_child_devices) {
    430  1.51  jmcneill 		child_devices = iba->iba_child_devices;
    431  1.51  jmcneill 		indirect_config = false;
    432  1.51  jmcneill 	} else {
    433  1.51  jmcneill 		props = device_properties(parent);
    434  1.51  jmcneill 		if (!prop_dictionary_get_bool(props, "i2c-indirect-config",
    435  1.51  jmcneill 		    &indirect_config))
    436  1.51  jmcneill 			indirect_config = true;
    437  1.51  jmcneill 		child_devices = prop_dictionary_get(props, "i2c-child-devices");
    438  1.51  jmcneill 	}
    439  1.51  jmcneill 
    440  1.24    martin 	if (child_devices) {
    441  1.24    martin 		unsigned int i, count;
    442  1.24    martin 		prop_dictionary_t dev;
    443  1.24    martin 		prop_data_t cdata;
    444  1.24    martin 		uint32_t addr, size;
    445  1.24    martin 		uint64_t cookie;
    446  1.24    martin 		const char *name;
    447  1.24    martin 		struct i2c_attach_args ia;
    448  1.53  jakllsch 		int loc[IICCF_NLOCS];
    449  1.24    martin 
    450  1.24    martin 		memset(loc, 0, sizeof loc);
    451  1.24    martin 		count = prop_array_count(child_devices);
    452  1.24    martin 		for (i = 0; i < count; i++) {
    453  1.24    martin 			dev = prop_array_get(child_devices, i);
    454  1.24    martin 			if (!dev) continue;
    455  1.24    martin  			if (!prop_dictionary_get_cstring_nocopy(
    456  1.62   thorpej 			    dev, "name", &name)) {
    457  1.62   thorpej 				/* "name" property is optional. */
    458  1.62   thorpej 				name = NULL;
    459  1.62   thorpej 			}
    460  1.24    martin 			if (!prop_dictionary_get_uint32(dev, "addr", &addr))
    461  1.24    martin 				continue;
    462  1.24    martin 			if (!prop_dictionary_get_uint64(dev, "cookie", &cookie))
    463  1.24    martin 				cookie = 0;
    464  1.53  jakllsch 			loc[IICCF_ADDR] = addr;
    465  1.24    martin 			if (prop_dictionary_get_uint32(dev, "size", &size))
    466  1.53  jakllsch 				loc[IICCF_SIZE] = size;
    467  1.24    martin 			else
    468  1.54  jakllsch 				size = loc[IICCF_SIZE] = IICCF_SIZE_DEFAULT;
    469  1.24    martin 
    470  1.24    martin 			memset(&ia, 0, sizeof ia);
    471  1.24    martin 			ia.ia_addr = addr;
    472  1.24    martin 			ia.ia_type = sc->sc_type;
    473  1.24    martin 			ia.ia_tag = ic;
    474  1.24    martin 			ia.ia_name = name;
    475  1.24    martin 			ia.ia_cookie = cookie;
    476  1.39       jdc 			ia.ia_size = size;
    477  1.57    bouyer 			ia.ia_prop = dev;
    478  1.24    martin 
    479  1.24    martin 			buf = NULL;
    480  1.24    martin 			cdata = prop_dictionary_get(dev, "compatible");
    481  1.24    martin 			if (cdata)
    482  1.24    martin 				iic_fill_compat(&ia,
    483  1.24    martin 				    prop_data_data_nocopy(cdata),
    484  1.24    martin 				    prop_data_size(cdata), &buf);
    485  1.24    martin 
    486  1.62   thorpej 			if (name == NULL && cdata == NULL) {
    487  1.33  jmcneill 				aprint_error_dev(self,
    488  1.62   thorpej 				    "WARNING: ignoring bad child device entry "
    489  1.62   thorpej 				    "for address 0x%02x\n", addr);
    490  1.62   thorpej 			} else {
    491  1.62   thorpej 				if (addr > I2C_MAX_ADDR) {
    492  1.62   thorpej 					aprint_error_dev(self,
    493  1.62   thorpej 					    "WARNING: ignoring bad device "
    494  1.62   thorpej 					    "address @ 0x%02x\n", addr);
    495  1.62   thorpej 				} else if (sc->sc_devices[addr] == NULL) {
    496  1.62   thorpej 					sc->sc_devices[addr] =
    497  1.62   thorpej 					    config_found_sm_loc(self, "iic",
    498  1.62   thorpej 					        loc, &ia, iic_print_direct,
    499  1.62   thorpej 						NULL);
    500  1.62   thorpej 				}
    501  1.33  jmcneill 			}
    502  1.24    martin 
    503  1.24    martin 			if (ia.ia_compat)
    504  1.24    martin 				free(ia.ia_compat, M_TEMP);
    505  1.24    martin 			if (buf)
    506  1.24    martin 				free(buf, M_TEMP);
    507  1.24    martin 		}
    508  1.42       jdc 	} else if (indirect_config) {
    509  1.24    martin 		/*
    510  1.24    martin 		 * Attach all i2c devices described in the kernel
    511  1.24    martin 		 * configuration file.
    512  1.24    martin 		 */
    513  1.26  jmcneill 		iic_rescan(self, "iic", NULL);
    514  1.24    martin 	}
    515   1.1   thorpej }
    516   1.1   thorpej 
    517  1.33  jmcneill static int
    518  1.33  jmcneill iic_detach(device_t self, int flags)
    519  1.33  jmcneill {
    520  1.33  jmcneill 	struct iic_softc *sc = device_private(self);
    521  1.33  jmcneill 	i2c_tag_t ic = sc->sc_tag;
    522  1.33  jmcneill 	int i, error;
    523  1.33  jmcneill 	void *hdl;
    524  1.33  jmcneill 
    525  1.33  jmcneill 	for (i = 0; i <= I2C_MAX_ADDR; i++) {
    526  1.33  jmcneill 		if (sc->sc_devices[i]) {
    527  1.33  jmcneill 			error = config_detach(sc->sc_devices[i], flags);
    528  1.33  jmcneill 			if (error)
    529  1.33  jmcneill 				return error;
    530  1.33  jmcneill 		}
    531  1.33  jmcneill 	}
    532  1.33  jmcneill 
    533  1.33  jmcneill 	if (ic->ic_running) {
    534  1.33  jmcneill 		ic->ic_running = 0;
    535  1.33  jmcneill 		wakeup(ic);
    536  1.33  jmcneill 		kthread_join(ic->ic_intr_thread);
    537  1.33  jmcneill 	}
    538  1.33  jmcneill 
    539  1.33  jmcneill 	if (!LIST_EMPTY(&ic->ic_list)) {
    540  1.33  jmcneill 		device_printf(self, "WARNING: intr handler list not empty\n");
    541  1.33  jmcneill 		while (!LIST_EMPTY(&ic->ic_list)) {
    542  1.33  jmcneill 			hdl = LIST_FIRST(&ic->ic_list);
    543  1.33  jmcneill 			iic_smbus_intr_disestablish(ic, hdl);
    544  1.33  jmcneill 		}
    545  1.33  jmcneill 	}
    546  1.33  jmcneill 	if (!LIST_EMPTY(&ic->ic_proc_list)) {
    547  1.33  jmcneill 		device_printf(self, "WARNING: proc handler list not empty\n");
    548  1.33  jmcneill 		while (!LIST_EMPTY(&ic->ic_proc_list)) {
    549  1.33  jmcneill 			hdl = LIST_FIRST(&ic->ic_proc_list);
    550  1.33  jmcneill 			iic_smbus_intr_disestablish_proc(ic, hdl);
    551  1.33  jmcneill 		}
    552  1.33  jmcneill 	}
    553  1.33  jmcneill 
    554  1.33  jmcneill 	pmf_device_deregister(self);
    555  1.33  jmcneill 
    556  1.33  jmcneill 	return 0;
    557  1.33  jmcneill }
    558  1.33  jmcneill 
    559  1.11  jmcneill static void
    560  1.14        ad iic_smbus_intr_thread(void *aux)
    561  1.11  jmcneill {
    562  1.11  jmcneill 	i2c_tag_t ic;
    563  1.11  jmcneill 	struct ic_intr_list *il;
    564  1.11  jmcneill 
    565  1.11  jmcneill 	ic = (i2c_tag_t)aux;
    566  1.11  jmcneill 	ic->ic_running = 1;
    567  1.11  jmcneill 	ic->ic_pending = 0;
    568  1.11  jmcneill 
    569  1.11  jmcneill 	while (ic->ic_running) {
    570  1.11  jmcneill 		if (ic->ic_pending == 0)
    571  1.41    martin 			tsleep(ic, PZERO, "iicintr", hz);
    572  1.11  jmcneill 		if (ic->ic_pending > 0) {
    573  1.11  jmcneill 			LIST_FOREACH(il, &(ic->ic_proc_list), il_next) {
    574  1.11  jmcneill 				(*il->il_intr)(il->il_intrarg);
    575  1.11  jmcneill 			}
    576  1.11  jmcneill 			ic->ic_pending--;
    577  1.11  jmcneill 		}
    578  1.11  jmcneill 	}
    579  1.11  jmcneill 
    580  1.11  jmcneill 	kthread_exit(0);
    581  1.11  jmcneill }
    582  1.11  jmcneill 
    583  1.11  jmcneill void *
    584  1.11  jmcneill iic_smbus_intr_establish(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
    585  1.11  jmcneill {
    586  1.11  jmcneill 	struct ic_intr_list *il;
    587  1.11  jmcneill 
    588  1.11  jmcneill 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
    589  1.11  jmcneill 	if (il == NULL)
    590  1.11  jmcneill 		return NULL;
    591  1.28   mbalmer 
    592  1.11  jmcneill 	il->il_intr = intr;
    593  1.11  jmcneill 	il->il_intrarg = intrarg;
    594  1.11  jmcneill 
    595  1.11  jmcneill 	LIST_INSERT_HEAD(&(ic->ic_list), il, il_next);
    596  1.11  jmcneill 
    597  1.11  jmcneill 	return il;
    598  1.11  jmcneill }
    599  1.11  jmcneill 
    600  1.11  jmcneill void
    601  1.11  jmcneill iic_smbus_intr_disestablish(i2c_tag_t ic, void *hdl)
    602  1.11  jmcneill {
    603  1.11  jmcneill 	struct ic_intr_list *il;
    604  1.11  jmcneill 
    605  1.11  jmcneill 	il = (struct ic_intr_list *)hdl;
    606  1.11  jmcneill 
    607  1.11  jmcneill 	LIST_REMOVE(il, il_next);
    608  1.11  jmcneill 	free(il, M_DEVBUF);
    609  1.11  jmcneill 
    610  1.11  jmcneill 	return;
    611  1.11  jmcneill }
    612  1.11  jmcneill 
    613  1.11  jmcneill void *
    614  1.11  jmcneill iic_smbus_intr_establish_proc(i2c_tag_t ic, int (*intr)(void *), void *intrarg)
    615  1.11  jmcneill {
    616  1.11  jmcneill 	struct ic_intr_list *il;
    617  1.11  jmcneill 
    618  1.11  jmcneill 	il = malloc(sizeof(struct ic_intr_list), M_DEVBUF, M_WAITOK);
    619  1.11  jmcneill 	if (il == NULL)
    620  1.11  jmcneill 		return NULL;
    621  1.28   mbalmer 
    622  1.11  jmcneill 	il->il_intr = intr;
    623  1.11  jmcneill 	il->il_intrarg = intrarg;
    624  1.11  jmcneill 
    625  1.11  jmcneill 	LIST_INSERT_HEAD(&(ic->ic_proc_list), il, il_next);
    626  1.11  jmcneill 
    627  1.11  jmcneill 	return il;
    628  1.11  jmcneill }
    629  1.11  jmcneill 
    630  1.11  jmcneill void
    631  1.11  jmcneill iic_smbus_intr_disestablish_proc(i2c_tag_t ic, void *hdl)
    632  1.11  jmcneill {
    633  1.11  jmcneill 	struct ic_intr_list *il;
    634  1.11  jmcneill 
    635  1.11  jmcneill 	il = (struct ic_intr_list *)hdl;
    636  1.11  jmcneill 
    637  1.11  jmcneill 	LIST_REMOVE(il, il_next);
    638  1.11  jmcneill 	free(il, M_DEVBUF);
    639  1.11  jmcneill 
    640  1.11  jmcneill 	return;
    641  1.11  jmcneill }
    642  1.11  jmcneill 
    643  1.11  jmcneill int
    644  1.11  jmcneill iic_smbus_intr(i2c_tag_t ic)
    645  1.11  jmcneill {
    646  1.11  jmcneill 	struct ic_intr_list *il;
    647  1.11  jmcneill 
    648  1.11  jmcneill 	LIST_FOREACH(il, &(ic->ic_list), il_next) {
    649  1.11  jmcneill 		(*il->il_intr)(il->il_intrarg);
    650  1.11  jmcneill 	}
    651  1.11  jmcneill 
    652  1.11  jmcneill 	ic->ic_pending++;
    653  1.11  jmcneill 	wakeup(ic);
    654  1.11  jmcneill 
    655  1.11  jmcneill 	return 1;
    656  1.11  jmcneill }
    657  1.11  jmcneill 
    658  1.24    martin static void
    659  1.24    martin iic_fill_compat(struct i2c_attach_args *ia, const char *compat, size_t len,
    660  1.24    martin 	char **buffer)
    661  1.24    martin {
    662  1.24    martin 	int count, i;
    663  1.24    martin 	const char *c, *start, **ptr;
    664  1.24    martin 
    665  1.24    martin 	*buffer = NULL;
    666  1.24    martin 	for (i = count = 0, c = compat; i < len; i++, c++)
    667  1.24    martin 		if (*c == 0)
    668  1.24    martin 			count++;
    669  1.24    martin 	count += 2;
    670  1.24    martin 	ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
    671  1.24    martin 	if (!ptr) return;
    672  1.24    martin 
    673  1.24    martin 	for (i = count = 0, start = c = compat; i < len; i++, c++) {
    674  1.24    martin 		if (*c == 0) {
    675  1.24    martin 			ptr[count++] = start;
    676  1.24    martin 			start = c+1;
    677  1.24    martin 		}
    678  1.24    martin 	}
    679  1.24    martin 	if (start < compat+len) {
    680  1.24    martin 		/* last string not 0 terminated */
    681  1.24    martin 		size_t l = c-start;
    682  1.24    martin 		*buffer = malloc(l+1, M_TEMP, M_WAITOK);
    683  1.24    martin 		memcpy(*buffer, start, l);
    684  1.24    martin 		(*buffer)[l] = 0;
    685  1.24    martin 		ptr[count++] = *buffer;
    686  1.24    martin 	}
    687  1.24    martin 	ptr[count] = NULL;
    688  1.24    martin 
    689  1.24    martin 	ia->ia_compat = ptr;
    690  1.24    martin 	ia->ia_ncompat = count;
    691  1.24    martin }
    692  1.24    martin 
    693  1.62   thorpej /*
    694  1.62   thorpej  * iic_compat_match --
    695  1.62   thorpej  *	Match a device's "compatible" property against the list
    696  1.62   thorpej  *	of compatible strings provided by the driver.  Note that
    697  1.62   thorpej  *	we weight the match to the reverse index of the device's
    698  1.62   thorpej  *	"compatible" property strings so that a driver that matches
    699  1.62   thorpej  *	an lower-indexed "compatible" property is given a higher
    700  1.62   thorpej  *	match priority than one that matches a higher-indexed
    701  1.62   thorpej  *	"compatible" property.
    702  1.62   thorpej  */
    703  1.24    martin int
    704  1.62   thorpej iic_compat_match(const struct i2c_attach_args *ia, const char **compats)
    705  1.24    martin {
    706  1.62   thorpej 	int match_result = 0, i, ri;
    707  1.62   thorpej 
    708  1.62   thorpej 	if (ia->ia_ncompat == 0 || ia->ia_compat == NULL)
    709  1.62   thorpej 		return 0;
    710  1.24    martin 
    711  1.24    martin 	for (; compats && *compats; compats++) {
    712  1.62   thorpej 		for (i = 0, ri = ia->ia_ncompat - 1;
    713  1.62   thorpej 		     i < ia->ia_ncompat;
    714  1.62   thorpej 		     i++, ri--) {
    715  1.62   thorpej 			if (strcmp(*compats, ia->ia_compat[i]) == 0) {
    716  1.62   thorpej 				KASSERT(ri >= 0);
    717  1.62   thorpej 				match_result =
    718  1.62   thorpej 				    I2C_MATCH_DIRECT_COMPATIBLE + ri;
    719  1.62   thorpej 			}
    720  1.24    martin 		}
    721  1.24    martin 	}
    722  1.62   thorpej 	match_result = MIN(match_result, I2C_MATCH_DIRECT_COMPATIBLE_MAX);
    723  1.62   thorpej 	return match_result;
    724  1.62   thorpej }
    725  1.62   thorpej 
    726  1.62   thorpej bool
    727  1.62   thorpej iic_use_direct_match(const struct i2c_attach_args *ia, const cfdata_t cf,
    728  1.62   thorpej 		     const char **compats, int *match_resultp)
    729  1.62   thorpej {
    730  1.62   thorpej 
    731  1.62   thorpej 	KASSERT(match_resultp != NULL);
    732  1.62   thorpej 
    733  1.62   thorpej 	if (ia->ia_name != NULL &&
    734  1.62   thorpej 	    strcmp(ia->ia_name, cf->cf_name) == 0) {
    735  1.62   thorpej 		*match_resultp = I2C_MATCH_DIRECT_SPECIFIC;
    736  1.62   thorpej 		return true;
    737  1.62   thorpej 	}
    738  1.62   thorpej 
    739  1.62   thorpej 	if (ia->ia_ncompat > 0 && ia->ia_compat != NULL) {
    740  1.62   thorpej 		*match_resultp = iic_compat_match(ia, compats);
    741  1.62   thorpej 		return true;
    742  1.62   thorpej 	}
    743  1.62   thorpej 
    744  1.62   thorpej 	return false;
    745  1.24    martin }
    746  1.24    martin 
    747  1.31  jmcneill static int
    748  1.31  jmcneill iic_open(dev_t dev, int flag, int fmt, lwp_t *l)
    749  1.31  jmcneill {
    750  1.31  jmcneill 	struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
    751  1.31  jmcneill 
    752  1.50  pgoyette 	mutex_enter(&iic_mtx);
    753  1.50  pgoyette 	if (sc == NULL) {
    754  1.50  pgoyette 		mutex_exit(&iic_mtx);
    755  1.31  jmcneill 		return ENXIO;
    756  1.50  pgoyette 	}
    757  1.50  pgoyette 	iic_refcnt++;
    758  1.50  pgoyette 	mutex_exit(&iic_mtx);
    759  1.31  jmcneill 
    760  1.31  jmcneill 	return 0;
    761  1.31  jmcneill }
    762  1.31  jmcneill 
    763  1.31  jmcneill static int
    764  1.31  jmcneill iic_close(dev_t dev, int flag, int fmt, lwp_t *l)
    765  1.31  jmcneill {
    766  1.50  pgoyette 
    767  1.50  pgoyette 	mutex_enter(&iic_mtx);
    768  1.50  pgoyette 	iic_refcnt--;
    769  1.50  pgoyette 	mutex_exit(&iic_mtx);
    770  1.50  pgoyette 
    771  1.31  jmcneill 	return 0;
    772  1.31  jmcneill }
    773  1.31  jmcneill 
    774  1.31  jmcneill static int
    775  1.32  jmcneill iic_ioctl_exec(struct iic_softc *sc, i2c_ioctl_exec_t *iie, int flag)
    776  1.31  jmcneill {
    777  1.31  jmcneill 	i2c_tag_t ic = sc->sc_tag;
    778  1.31  jmcneill 	uint8_t buf[I2C_EXEC_MAX_BUFLEN];
    779  1.34  jmcneill 	void *cmd = NULL;
    780  1.31  jmcneill 	int error;
    781  1.31  jmcneill 
    782  1.31  jmcneill 	/* Validate parameters */
    783  1.31  jmcneill 	if (iie->iie_addr > I2C_MAX_ADDR)
    784  1.31  jmcneill 		return EINVAL;
    785  1.31  jmcneill 	if (iie->iie_cmdlen > I2C_EXEC_MAX_CMDLEN ||
    786  1.31  jmcneill 	    iie->iie_buflen > I2C_EXEC_MAX_BUFLEN)
    787  1.31  jmcneill 		return EINVAL;
    788  1.31  jmcneill 	if (iie->iie_cmd != NULL && iie->iie_cmdlen == 0)
    789  1.31  jmcneill 		return EINVAL;
    790  1.31  jmcneill 	if (iie->iie_buf != NULL && iie->iie_buflen == 0)
    791  1.31  jmcneill 		return EINVAL;
    792  1.32  jmcneill 	if (I2C_OP_WRITE_P(iie->iie_op) && (flag & FWRITE) == 0)
    793  1.32  jmcneill 		return EBADF;
    794  1.31  jmcneill 
    795  1.31  jmcneill #if 0
    796  1.31  jmcneill 	/* Disallow userspace access to devices that have drivers attached. */
    797  1.31  jmcneill 	if (sc->sc_devices[iie->iie_addr] != NULL)
    798  1.31  jmcneill 		return EBUSY;
    799  1.31  jmcneill #endif
    800  1.31  jmcneill 
    801  1.31  jmcneill 	if (iie->iie_cmd != NULL) {
    802  1.34  jmcneill 		cmd = kmem_alloc(iie->iie_cmdlen, KM_SLEEP);
    803  1.31  jmcneill 		error = copyin(iie->iie_cmd, cmd, iie->iie_cmdlen);
    804  1.48  christos 		if (error)
    805  1.48  christos 			goto out;
    806  1.31  jmcneill 	}
    807  1.31  jmcneill 
    808  1.46  jakllsch 	if (iie->iie_buf != NULL && I2C_OP_WRITE_P(iie->iie_op)) {
    809  1.46  jakllsch 		error = copyin(iie->iie_buf, buf, iie->iie_buflen);
    810  1.48  christos 		if (error)
    811  1.48  christos 			goto out;
    812  1.46  jakllsch 	}
    813  1.46  jakllsch 
    814  1.31  jmcneill 	iic_acquire_bus(ic, 0);
    815  1.31  jmcneill 	error = iic_exec(ic, iie->iie_op, iie->iie_addr, cmd, iie->iie_cmdlen,
    816  1.31  jmcneill 	    buf, iie->iie_buflen, 0);
    817  1.31  jmcneill 	iic_release_bus(ic, 0);
    818  1.31  jmcneill 
    819  1.36  jmcneill 	/*
    820  1.36  jmcneill 	 * Some drivers return error codes on failure, and others return -1.
    821  1.36  jmcneill 	 */
    822  1.36  jmcneill 	if (error < 0)
    823  1.36  jmcneill 		error = EIO;
    824  1.36  jmcneill 
    825  1.48  christos out:
    826  1.34  jmcneill 	if (cmd)
    827  1.34  jmcneill 		kmem_free(cmd, iie->iie_cmdlen);
    828  1.34  jmcneill 
    829  1.31  jmcneill 	if (error)
    830  1.31  jmcneill 		return error;
    831  1.31  jmcneill 
    832  1.46  jakllsch 	if (iie->iie_buf != NULL && I2C_OP_READ_P(iie->iie_op))
    833  1.31  jmcneill 		error = copyout(buf, iie->iie_buf, iie->iie_buflen);
    834  1.31  jmcneill 
    835  1.31  jmcneill 	return error;
    836  1.31  jmcneill }
    837  1.31  jmcneill 
    838  1.31  jmcneill static int
    839  1.31  jmcneill iic_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    840  1.31  jmcneill {
    841  1.31  jmcneill 	struct iic_softc *sc = device_lookup_private(&iic_cd, minor(dev));
    842  1.31  jmcneill 
    843  1.31  jmcneill 	if (sc == NULL)
    844  1.31  jmcneill 		return ENXIO;
    845  1.31  jmcneill 
    846  1.31  jmcneill 	switch (cmd) {
    847  1.31  jmcneill 	case I2C_IOCTL_EXEC:
    848  1.32  jmcneill 		return iic_ioctl_exec(sc, (i2c_ioctl_exec_t *)data, flag);
    849  1.31  jmcneill 	default:
    850  1.31  jmcneill 		return ENODEV;
    851  1.31  jmcneill 	}
    852  1.31  jmcneill }
    853  1.31  jmcneill 
    854  1.24    martin 
    855  1.26  jmcneill CFATTACH_DECL2_NEW(iic, sizeof(struct iic_softc),
    856  1.33  jmcneill     iic_match, iic_attach, iic_detach, NULL, iic_rescan, iic_child_detach);
    857  1.28   mbalmer 
    858  1.52  pgoyette MODULE(MODULE_CLASS_DRIVER, iic, "i2cexec,i2c_bitbang");
    859  1.28   mbalmer 
    860  1.28   mbalmer #ifdef _MODULE
    861  1.28   mbalmer #include "ioconf.c"
    862  1.28   mbalmer #endif
    863  1.28   mbalmer 
    864  1.50  pgoyette int
    865  1.50  pgoyette iic_init(void)
    866  1.50  pgoyette {
    867  1.50  pgoyette 
    868  1.50  pgoyette 	mutex_init(&iic_mtx, MUTEX_DEFAULT, IPL_NONE);
    869  1.50  pgoyette 	iic_refcnt = 0;
    870  1.50  pgoyette 	return 0;
    871  1.50  pgoyette }
    872  1.50  pgoyette 
    873  1.28   mbalmer static int
    874  1.28   mbalmer iic_modcmd(modcmd_t cmd, void *opaque)
    875  1.28   mbalmer {
    876  1.50  pgoyette #ifdef _MODULE
    877  1.50  pgoyette 	int bmajor, cmajor;
    878  1.50  pgoyette #endif
    879  1.28   mbalmer 	int error;
    880  1.28   mbalmer 
    881  1.28   mbalmer 	error = 0;
    882  1.28   mbalmer 	switch (cmd) {
    883  1.28   mbalmer 	case MODULE_CMD_INIT:
    884  1.50  pgoyette 		RUN_ONCE(&iic_once, iic_init);
    885  1.50  pgoyette 
    886  1.28   mbalmer #ifdef _MODULE
    887  1.50  pgoyette 		mutex_enter(&iic_mtx);
    888  1.50  pgoyette 		bmajor = cmajor = -1;
    889  1.50  pgoyette 		error = devsw_attach("iic", NULL, &bmajor,
    890  1.50  pgoyette 		    &iic_cdevsw, &cmajor);
    891  1.50  pgoyette 		if (error != 0) {
    892  1.50  pgoyette 			mutex_exit(&iic_mtx);
    893  1.50  pgoyette 			break;
    894  1.50  pgoyette 		}
    895  1.28   mbalmer 		error = config_init_component(cfdriver_ioconf_iic,
    896  1.28   mbalmer 		    cfattach_ioconf_iic, cfdata_ioconf_iic);
    897  1.50  pgoyette 		if (error) {
    898  1.28   mbalmer 			aprint_error("%s: unable to init component\n",
    899  1.28   mbalmer 			    iic_cd.cd_name);
    900  1.50  pgoyette 			(void)devsw_detach(NULL, &iic_cdevsw);
    901  1.50  pgoyette 		}
    902  1.50  pgoyette 		mutex_exit(&iic_mtx);
    903  1.28   mbalmer #endif
    904  1.28   mbalmer 		break;
    905  1.28   mbalmer 	case MODULE_CMD_FINI:
    906  1.50  pgoyette 		mutex_enter(&iic_mtx);
    907  1.50  pgoyette 		if (iic_refcnt != 0) {
    908  1.50  pgoyette 			mutex_exit(&iic_mtx);
    909  1.50  pgoyette 			return EBUSY;
    910  1.50  pgoyette 		}
    911  1.28   mbalmer #ifdef _MODULE
    912  1.50  pgoyette 		error = config_fini_component(cfdriver_ioconf_iic,
    913  1.28   mbalmer 		    cfattach_ioconf_iic, cfdata_ioconf_iic);
    914  1.50  pgoyette 		if (error != 0) {
    915  1.50  pgoyette 			mutex_exit(&iic_mtx);
    916  1.50  pgoyette 			break;
    917  1.50  pgoyette 		}
    918  1.50  pgoyette 		error = devsw_detach(NULL, &iic_cdevsw);
    919  1.50  pgoyette 		if (error != 0)
    920  1.50  pgoyette 			config_init_component(cfdriver_ioconf_iic,
    921  1.50  pgoyette 			    cfattach_ioconf_iic, cfdata_ioconf_iic);
    922  1.28   mbalmer #endif
    923  1.50  pgoyette 		mutex_exit(&iic_mtx);
    924  1.28   mbalmer 		break;
    925  1.28   mbalmer 	default:
    926  1.28   mbalmer 		error = ENOTTY;
    927  1.28   mbalmer 	}
    928  1.28   mbalmer 	return error;
    929  1.28   mbalmer }
    930