Home | History | Annotate | Line # | Download | only in acpi
smbus_acpi.c revision 1.1.2.2
      1      1.1  pgoyette /* $NetBSD: smbus_acpi.c,v 1.1.2.2 2010/08/17 06:46:02 uebayasi Exp $ */
      2      1.1  pgoyette 
      3      1.1  pgoyette /*-
      4      1.1  pgoyette  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5      1.1  pgoyette  * All rights reserved.
      6      1.1  pgoyette  *
      7      1.1  pgoyette  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1  pgoyette  * by Paul Goyette
      9      1.1  pgoyette  *
     10      1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     11      1.1  pgoyette  * modification, are permitted provided that the following conditions
     12      1.1  pgoyette  * are met:
     13      1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     14      1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     15      1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     17      1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     18      1.1  pgoyette  *
     19      1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1  pgoyette  */
     31      1.1  pgoyette 
     32      1.1  pgoyette /*
     33      1.1  pgoyette  * ACPI SMBus Controller driver
     34      1.1  pgoyette  *
     35      1.1  pgoyette  * See http://smbus.org/specs/smbus_cmi10.pdf for specifications
     36      1.1  pgoyette  */
     37      1.1  pgoyette 
     38      1.1  pgoyette #include <sys/cdefs.h>
     39      1.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: smbus_acpi.c,v 1.1.2.2 2010/08/17 06:46:02 uebayasi Exp $");
     40      1.1  pgoyette 
     41      1.1  pgoyette #include <sys/param.h>
     42      1.1  pgoyette #include <sys/device.h>
     43      1.1  pgoyette #include <sys/callout.h>
     44  1.1.2.1  uebayasi #include <sys/kernel.h>
     45      1.1  pgoyette #include <sys/mutex.h>
     46  1.1.2.1  uebayasi #include <sys/systm.h>
     47      1.1  pgoyette 
     48      1.1  pgoyette #include <dev/acpi/acpireg.h>
     49      1.1  pgoyette #include <dev/acpi/acpivar.h>
     50      1.1  pgoyette 
     51      1.1  pgoyette #include <dev/i2c/i2cvar.h>
     52      1.1  pgoyette 
     53  1.1.2.1  uebayasi #define _COMPONENT		ACPI_BUS_COMPONENT
     54  1.1.2.1  uebayasi ACPI_MODULE_NAME		("smbus_acpi")
     55      1.1  pgoyette 
     56      1.1  pgoyette /*
     57  1.1.2.2  uebayasi  * ACPI SMBus CMI protocol codes.
     58      1.1  pgoyette  */
     59      1.1  pgoyette #define	ACPI_SMBUS_RD_QUICK	0x03
     60      1.1  pgoyette #define	ACPI_SMBUS_RCV_BYTE	0x05
     61      1.1  pgoyette #define	ACPI_SMBUS_RD_BYTE	0x07
     62      1.1  pgoyette #define	ACPI_SMBUS_RD_WORD	0x09
     63      1.1  pgoyette #define	ACPI_SMBUS_RD_BLOCK	0x0B
     64      1.1  pgoyette #define	ACPI_SMBUS_WR_QUICK	0x02
     65      1.1  pgoyette #define	ACPI_SMBUS_SND_BYTE	0x04
     66      1.1  pgoyette #define	ACPI_SMBUS_WR_BYTE	0x06
     67      1.1  pgoyette #define	ACPI_SMBUS_WR_WORD	0x08
     68      1.1  pgoyette #define	ACPI_SMBUS_WR_BLOCK	0x0A
     69      1.1  pgoyette #define	ACPI_SMBUS_PROCESS_CALL	0x0C
     70      1.1  pgoyette 
     71      1.1  pgoyette struct acpi_smbus_softc {
     72      1.1  pgoyette 	struct acpi_devnode 	*sc_devnode;
     73      1.1  pgoyette 	struct callout		sc_callout;
     74      1.1  pgoyette 	struct i2c_controller	sc_i2c_tag;
     75      1.1  pgoyette 	device_t		sc_dv;
     76      1.1  pgoyette 	kmutex_t		sc_i2c_mutex;
     77      1.1  pgoyette 	int			sc_poll_alert;
     78      1.1  pgoyette };
     79      1.1  pgoyette 
     80      1.1  pgoyette static int	acpi_smbus_match(device_t, cfdata_t, void *);
     81      1.1  pgoyette static void	acpi_smbus_attach(device_t, device_t, void *);
     82  1.1.2.1  uebayasi static int	acpi_smbus_detach(device_t, int);
     83  1.1.2.2  uebayasi static int	acpi_smbus_poll_alert(ACPI_HANDLE, int *);
     84      1.1  pgoyette static int	acpi_smbus_acquire_bus(void *, int);
     85      1.1  pgoyette static void	acpi_smbus_release_bus(void *, int);
     86      1.1  pgoyette static int	acpi_smbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
     87      1.1  pgoyette 				size_t, void *, size_t, int);
     88      1.1  pgoyette static void	acpi_smbus_alerts(struct acpi_smbus_softc *);
     89      1.1  pgoyette static void	acpi_smbus_tick(void *);
     90  1.1.2.1  uebayasi static void	acpi_smbus_notify_handler(ACPI_HANDLE, uint32_t, void *);
     91      1.1  pgoyette 
     92      1.1  pgoyette struct SMB_UDID {
     93      1.1  pgoyette 	uint8_t		dev_cap;
     94      1.1  pgoyette 	uint8_t		vers_rev;
     95      1.1  pgoyette 	uint16_t	vendor;
     96      1.1  pgoyette 	uint16_t	device;
     97      1.1  pgoyette 	uint16_t	interface;
     98      1.1  pgoyette 	uint16_t	subsys_vendor;
     99      1.1  pgoyette 	uint16_t	subsys_device;
    100      1.1  pgoyette 	uint8_t		reserved[4];
    101      1.1  pgoyette };
    102      1.1  pgoyette 
    103      1.1  pgoyette struct SMB_DEVICE {
    104      1.1  pgoyette 	uint8_t		slave_addr;
    105      1.1  pgoyette 	uint8_t		reserved;
    106      1.1  pgoyette 	struct SMB_UDID	dev_id;
    107      1.1  pgoyette };
    108      1.1  pgoyette 
    109      1.1  pgoyette struct SMB_INFO {
    110      1.1  pgoyette 	uint8_t		struct_ver;
    111      1.1  pgoyette 	uint8_t		spec_ver;
    112      1.1  pgoyette 	uint8_t		hw_cap;
    113      1.1  pgoyette 	uint8_t		poll_int;
    114      1.1  pgoyette 	uint8_t		dev_count;
    115      1.1  pgoyette 	struct SMB_DEVICE device[1];
    116      1.1  pgoyette };
    117      1.1  pgoyette 
    118      1.1  pgoyette static const char * const smbus_acpi_ids[] = {
    119      1.1  pgoyette 	"SMBUS01",	/* SMBus CMI v1.0 */
    120      1.1  pgoyette 	NULL
    121      1.1  pgoyette };
    122      1.1  pgoyette 
    123      1.1  pgoyette CFATTACH_DECL_NEW(acpismbus, sizeof(struct acpi_smbus_softc),
    124      1.1  pgoyette     acpi_smbus_match, acpi_smbus_attach, acpi_smbus_detach, NULL);
    125      1.1  pgoyette 
    126      1.1  pgoyette static int
    127      1.1  pgoyette acpi_smbus_match(device_t parent, cfdata_t match, void *aux)
    128      1.1  pgoyette {
    129      1.1  pgoyette 	struct acpi_attach_args *aa = aux;
    130      1.1  pgoyette 
    131      1.1  pgoyette 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    132      1.1  pgoyette 		return 0;
    133      1.1  pgoyette 
    134      1.1  pgoyette 	if (acpi_match_hid(aa->aa_node->ad_devinfo, smbus_acpi_ids) == 0)
    135      1.1  pgoyette 		return 0;
    136      1.1  pgoyette 
    137  1.1.2.2  uebayasi 	return acpi_smbus_poll_alert(aa->aa_node->ad_handle, NULL);
    138      1.1  pgoyette }
    139      1.1  pgoyette 
    140      1.1  pgoyette static void
    141      1.1  pgoyette acpi_smbus_attach(device_t parent, device_t self, void *aux)
    142      1.1  pgoyette {
    143      1.1  pgoyette 	struct acpi_smbus_softc *sc = device_private(self);
    144      1.1  pgoyette 	struct acpi_attach_args *aa = aux;
    145      1.1  pgoyette 	struct i2cbus_attach_args iba;
    146      1.1  pgoyette 
    147      1.1  pgoyette 	aprint_naive("\n");
    148      1.1  pgoyette 
    149      1.1  pgoyette 	sc->sc_devnode = aa->aa_node;
    150      1.1  pgoyette 	sc->sc_dv = self;
    151      1.1  pgoyette 	sc->sc_poll_alert = 2;
    152      1.1  pgoyette 
    153  1.1.2.2  uebayasi 	/* Attach I2C bus. */
    154      1.1  pgoyette 	mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
    155  1.1.2.2  uebayasi 
    156      1.1  pgoyette 	sc->sc_i2c_tag.ic_cookie = sc;
    157      1.1  pgoyette 	sc->sc_i2c_tag.ic_acquire_bus = acpi_smbus_acquire_bus;
    158      1.1  pgoyette 	sc->sc_i2c_tag.ic_release_bus = acpi_smbus_release_bus;
    159      1.1  pgoyette 	sc->sc_i2c_tag.ic_exec = acpi_smbus_exec;
    160      1.1  pgoyette 
    161  1.1.2.2  uebayasi 	(void)acpi_smbus_poll_alert(aa->aa_node->ad_handle,&sc->sc_poll_alert);
    162      1.1  pgoyette 
    163  1.1.2.1  uebayasi 	/* If failed, fall-back to polling. */
    164  1.1.2.1  uebayasi 	if (acpi_register_notify(sc->sc_devnode,
    165  1.1.2.1  uebayasi 		acpi_smbus_notify_handler) != true)
    166  1.1.2.1  uebayasi 		sc->sc_poll_alert = 2;
    167      1.1  pgoyette 
    168      1.1  pgoyette 	callout_init(&sc->sc_callout, 0);
    169      1.1  pgoyette 	callout_setfunc(&sc->sc_callout, acpi_smbus_tick, self);
    170      1.1  pgoyette 
    171      1.1  pgoyette 	if (sc->sc_poll_alert != 0) {
    172  1.1.2.2  uebayasi 		aprint_debug(": alert_poll %d sec", sc->sc_poll_alert);
    173      1.1  pgoyette 		callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
    174      1.1  pgoyette 	}
    175      1.1  pgoyette 
    176  1.1.2.2  uebayasi 	aprint_normal("\n");
    177  1.1.2.1  uebayasi 
    178  1.1.2.2  uebayasi 	(void)memset(&iba, 0, sizeof(iba));
    179  1.1.2.2  uebayasi 	(void)pmf_device_register(self, NULL, NULL);
    180  1.1.2.1  uebayasi 
    181      1.1  pgoyette 	iba.iba_tag = &sc->sc_i2c_tag;
    182  1.1.2.2  uebayasi 
    183  1.1.2.2  uebayasi 	(void)config_found_ia(self, "i2cbus", &iba, iicbus_print);
    184      1.1  pgoyette }
    185      1.1  pgoyette 
    186      1.1  pgoyette static int
    187  1.1.2.1  uebayasi acpi_smbus_detach(device_t self, int flags)
    188      1.1  pgoyette {
    189      1.1  pgoyette 	struct acpi_smbus_softc *sc = device_private(self);
    190  1.1.2.1  uebayasi 
    191      1.1  pgoyette 	pmf_device_deregister(self);
    192  1.1.2.1  uebayasi 	acpi_deregister_notify(sc->sc_devnode);
    193      1.1  pgoyette 
    194      1.1  pgoyette 	callout_halt(&sc->sc_callout, NULL);
    195  1.1.2.1  uebayasi 	callout_destroy(&sc->sc_callout);
    196      1.1  pgoyette 
    197      1.1  pgoyette 	mutex_destroy(&sc->sc_i2c_mutex);
    198  1.1.2.1  uebayasi 
    199      1.1  pgoyette 	return 0;
    200      1.1  pgoyette }
    201      1.1  pgoyette 
    202      1.1  pgoyette static int
    203  1.1.2.2  uebayasi acpi_smbus_poll_alert(ACPI_HANDLE hdl, int *alert)
    204  1.1.2.2  uebayasi {
    205  1.1.2.2  uebayasi 	struct SMB_INFO *info;
    206  1.1.2.2  uebayasi 	ACPI_BUFFER smi_buf;
    207  1.1.2.2  uebayasi 	ACPI_OBJECT *e, *p;
    208  1.1.2.2  uebayasi 	ACPI_STATUS rv;
    209  1.1.2.2  uebayasi 
    210  1.1.2.2  uebayasi 	/*
    211  1.1.2.2  uebayasi 	 * Retrieve polling interval for SMBus Alerts.
    212  1.1.2.2  uebayasi 	 */
    213  1.1.2.2  uebayasi 	rv = acpi_eval_struct(hdl, "_SBI", &smi_buf);
    214  1.1.2.2  uebayasi 
    215  1.1.2.2  uebayasi 	if (ACPI_FAILURE(rv))
    216  1.1.2.2  uebayasi 		return 0;
    217  1.1.2.2  uebayasi 
    218  1.1.2.2  uebayasi 	p = smi_buf.Pointer;
    219  1.1.2.2  uebayasi 
    220  1.1.2.2  uebayasi 	if (p->Type != ACPI_TYPE_PACKAGE) {
    221  1.1.2.2  uebayasi 		rv = AE_TYPE;
    222  1.1.2.2  uebayasi 		goto out;
    223  1.1.2.2  uebayasi 	}
    224  1.1.2.2  uebayasi 
    225  1.1.2.2  uebayasi 	if (p->Package.Count == 0) {
    226  1.1.2.2  uebayasi 		rv = AE_LIMIT;
    227  1.1.2.2  uebayasi 		goto out;
    228  1.1.2.2  uebayasi 	}
    229  1.1.2.2  uebayasi 
    230  1.1.2.2  uebayasi 	e = p->Package.Elements;
    231  1.1.2.2  uebayasi 
    232  1.1.2.2  uebayasi 	if (e[0].Type != ACPI_TYPE_INTEGER) {
    233  1.1.2.2  uebayasi 		rv = AE_TYPE;
    234  1.1.2.2  uebayasi 		goto out;
    235  1.1.2.2  uebayasi 	}
    236  1.1.2.2  uebayasi 
    237  1.1.2.2  uebayasi 	/* Verify CMI version. */
    238  1.1.2.2  uebayasi 	if (e[0].Integer.Value != 0x10) {
    239  1.1.2.2  uebayasi 		rv = AE_SUPPORT;
    240  1.1.2.2  uebayasi 		goto out;
    241  1.1.2.2  uebayasi 	}
    242  1.1.2.2  uebayasi 
    243  1.1.2.2  uebayasi 	if (alert != NULL) {
    244  1.1.2.2  uebayasi 
    245  1.1.2.2  uebayasi 		if (p->Package.Count < 2)
    246  1.1.2.2  uebayasi 			goto out;
    247  1.1.2.2  uebayasi 
    248  1.1.2.2  uebayasi 		if (e[1].Type != ACPI_TYPE_BUFFER)
    249  1.1.2.2  uebayasi 			goto out;
    250  1.1.2.2  uebayasi 
    251  1.1.2.2  uebayasi 		info = (struct SMB_INFO *)(e[1].Buffer.Pointer);
    252  1.1.2.2  uebayasi 		*alert = info->poll_int;
    253  1.1.2.2  uebayasi 	}
    254  1.1.2.2  uebayasi 
    255  1.1.2.2  uebayasi out:
    256  1.1.2.2  uebayasi 	if (smi_buf.Pointer != NULL)
    257  1.1.2.2  uebayasi 		ACPI_FREE(smi_buf.Pointer);
    258  1.1.2.2  uebayasi 
    259  1.1.2.2  uebayasi 	return (ACPI_FAILURE(rv)) ? 0 : 1;
    260  1.1.2.2  uebayasi }
    261  1.1.2.2  uebayasi 
    262  1.1.2.2  uebayasi static int
    263      1.1  pgoyette acpi_smbus_acquire_bus(void *cookie, int flags)
    264      1.1  pgoyette {
    265      1.1  pgoyette         struct acpi_smbus_softc *sc = cookie;
    266  1.1.2.1  uebayasi 
    267      1.1  pgoyette         mutex_enter(&sc->sc_i2c_mutex);
    268  1.1.2.2  uebayasi 
    269      1.1  pgoyette         return 0;
    270  1.1.2.1  uebayasi }
    271  1.1.2.1  uebayasi 
    272      1.1  pgoyette static void
    273      1.1  pgoyette acpi_smbus_release_bus(void *cookie, int flags)
    274  1.1.2.1  uebayasi {
    275      1.1  pgoyette         struct acpi_smbus_softc *sc = cookie;
    276  1.1.2.1  uebayasi 
    277      1.1  pgoyette         mutex_exit(&sc->sc_i2c_mutex);
    278      1.1  pgoyette }
    279      1.1  pgoyette static int
    280      1.1  pgoyette acpi_smbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    281      1.1  pgoyette 	const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    282      1.1  pgoyette {
    283      1.1  pgoyette         struct acpi_smbus_softc *sc = cookie;
    284      1.1  pgoyette 	const uint8_t *c = cmdbuf;
    285      1.1  pgoyette 	uint8_t *b = buf, *xb;
    286  1.1.2.2  uebayasi 	const char *path;
    287      1.1  pgoyette 	ACPI_OBJECT_LIST args;
    288      1.1  pgoyette 	ACPI_OBJECT arg[5];
    289      1.1  pgoyette 	ACPI_OBJECT *p, *e;
    290  1.1.2.2  uebayasi 	ACPI_BUFFER smbuf;
    291  1.1.2.2  uebayasi 	ACPI_STATUS rv;
    292  1.1.2.2  uebayasi 	int i, r, xlen;
    293  1.1.2.2  uebayasi 
    294  1.1.2.2  uebayasi 	/*
    295  1.1.2.2  uebayasi 	 *	arg[0] : protocol
    296  1.1.2.2  uebayasi 	 *	arg[1] : slave address
    297  1.1.2.2  uebayasi 	 *	arg[2] : command
    298  1.1.2.2  uebayasi 	 *	arg[3] : data length
    299  1.1.2.2  uebayasi 	 *	arg[4] : data
    300  1.1.2.2  uebayasi 	 */
    301  1.1.2.2  uebayasi 	for (i = r = 0; i < __arraycount(arg); i++)
    302  1.1.2.2  uebayasi 		arg[i].Type = ACPI_TYPE_INTEGER;
    303  1.1.2.2  uebayasi 
    304  1.1.2.2  uebayasi 	args.Pointer = arg;
    305      1.1  pgoyette 
    306      1.1  pgoyette 	smbuf.Pointer = NULL;
    307      1.1  pgoyette 	smbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    308  1.1.2.2  uebayasi 
    309      1.1  pgoyette 	arg[1].Integer.Value = addr;
    310  1.1.2.2  uebayasi 
    311      1.1  pgoyette 	if (I2C_OP_READ_P(op)) {
    312  1.1.2.2  uebayasi 
    313  1.1.2.2  uebayasi 		path = "_SBR";
    314      1.1  pgoyette 		args.Count = 3;
    315  1.1.2.2  uebayasi 
    316  1.1.2.2  uebayasi 		switch (len) {
    317  1.1.2.2  uebayasi 
    318  1.1.2.2  uebayasi 		case 0:
    319  1.1.2.2  uebayasi 			arg[0].Integer.Value = (cmdlen != 0) ?
    320  1.1.2.2  uebayasi 			    ACPI_SMBUS_RCV_BYTE : ACPI_SMBUS_RD_QUICK;
    321  1.1.2.2  uebayasi 
    322      1.1  pgoyette 			arg[2].Integer.Value = 0;
    323  1.1.2.2  uebayasi 			break;
    324  1.1.2.2  uebayasi 
    325  1.1.2.2  uebayasi 		case 1:
    326      1.1  pgoyette 			arg[0].Integer.Value = ACPI_SMBUS_RD_BYTE;
    327  1.1.2.2  uebayasi 			arg[2].Integer.Value = *c;
    328  1.1.2.2  uebayasi 			break;
    329  1.1.2.2  uebayasi 
    330  1.1.2.2  uebayasi 		case 2:
    331      1.1  pgoyette 			arg[0].Integer.Value = ACPI_SMBUS_RD_WORD;
    332  1.1.2.2  uebayasi 			arg[2].Integer.Value = *c;
    333  1.1.2.2  uebayasi 			break;
    334  1.1.2.2  uebayasi 
    335  1.1.2.2  uebayasi 		default:
    336      1.1  pgoyette 			arg[0].Integer.Value = ACPI_SMBUS_RD_BLOCK;
    337  1.1.2.2  uebayasi 			arg[2].Integer.Value = *c;
    338  1.1.2.2  uebayasi 			break;
    339  1.1.2.2  uebayasi 		}
    340  1.1.2.2  uebayasi 
    341      1.1  pgoyette 	} else {
    342  1.1.2.2  uebayasi 
    343  1.1.2.2  uebayasi 		path = "_SBW";
    344      1.1  pgoyette 		args.Count = 5;
    345  1.1.2.2  uebayasi 
    346      1.1  pgoyette 		arg[3].Integer.Value = len;
    347  1.1.2.2  uebayasi 
    348  1.1.2.2  uebayasi 		switch (len) {
    349  1.1.2.2  uebayasi 
    350  1.1.2.2  uebayasi 		case 0:
    351      1.1  pgoyette 			if (cmdlen == 0) {
    352      1.1  pgoyette 				arg[2].Integer.Value = 0;
    353      1.1  pgoyette 				arg[0].Integer.Value = ACPI_SMBUS_WR_QUICK;
    354      1.1  pgoyette 			} else {
    355      1.1  pgoyette 				arg[2].Integer.Value = *c;
    356      1.1  pgoyette 				arg[0].Integer.Value = ACPI_SMBUS_SND_BYTE;
    357      1.1  pgoyette 			}
    358  1.1.2.2  uebayasi 
    359  1.1.2.2  uebayasi 			arg[4].Integer.Value = 0;
    360  1.1.2.2  uebayasi 			break;
    361  1.1.2.2  uebayasi 
    362  1.1.2.2  uebayasi 		case 1:
    363      1.1  pgoyette 			arg[0].Integer.Value = ACPI_SMBUS_WR_BYTE;
    364  1.1.2.2  uebayasi 			arg[2].Integer.Value = *c;
    365      1.1  pgoyette 			arg[4].Integer.Value = *b;
    366  1.1.2.2  uebayasi 			break;
    367  1.1.2.2  uebayasi 
    368  1.1.2.2  uebayasi 		case 2:
    369      1.1  pgoyette 			arg[0].Integer.Value = ACPI_SMBUS_WR_WORD;
    370  1.1.2.2  uebayasi 			arg[2].Integer.Value = *c;
    371      1.1  pgoyette 			arg[4].Integer.Value = *b++;
    372      1.1  pgoyette 			arg[4].Integer.Value += (*b--) << 8;
    373  1.1.2.2  uebayasi 			break;
    374  1.1.2.2  uebayasi 
    375  1.1.2.2  uebayasi 		default:
    376      1.1  pgoyette 			arg[0].Integer.Value = ACPI_SMBUS_WR_BLOCK;
    377  1.1.2.2  uebayasi 			arg[2].Integer.Value = *c;
    378      1.1  pgoyette 			arg[4].Type = ACPI_TYPE_BUFFER;
    379      1.1  pgoyette 			arg[4].Buffer.Pointer = buf;
    380  1.1.2.2  uebayasi 			arg[4].Buffer.Length = (len < 32) ? len : 32;
    381  1.1.2.2  uebayasi 			break;
    382      1.1  pgoyette 		}
    383      1.1  pgoyette 	}
    384  1.1.2.2  uebayasi 
    385  1.1.2.2  uebayasi 	rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, path, &args,&smbuf);
    386  1.1.2.2  uebayasi 
    387      1.1  pgoyette 	if (ACPI_FAILURE(rv))
    388  1.1.2.2  uebayasi 		goto out;
    389  1.1.2.2  uebayasi 
    390  1.1.2.2  uebayasi 	p = smbuf.Pointer;
    391  1.1.2.2  uebayasi 
    392  1.1.2.2  uebayasi 	if (p->Type != ACPI_TYPE_PACKAGE) {
    393  1.1.2.2  uebayasi 		rv = AE_TYPE;
    394  1.1.2.2  uebayasi 		goto out;
    395  1.1.2.2  uebayasi 	}
    396  1.1.2.2  uebayasi 
    397  1.1.2.2  uebayasi 	if (p->Package.Count < 1) {
    398  1.1.2.2  uebayasi 		rv = AE_LIMIT;
    399  1.1.2.2  uebayasi 		goto out;
    400  1.1.2.2  uebayasi 	}
    401  1.1.2.2  uebayasi 
    402  1.1.2.2  uebayasi 	e = p->Package.Elements;
    403  1.1.2.2  uebayasi 
    404  1.1.2.2  uebayasi 	if (e->Type != ACPI_TYPE_INTEGER) {
    405  1.1.2.2  uebayasi 		rv = AE_TYPE;
    406  1.1.2.2  uebayasi 		goto out;
    407  1.1.2.2  uebayasi 	}
    408  1.1.2.2  uebayasi 
    409  1.1.2.2  uebayasi 	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT,
    410  1.1.2.2  uebayasi 		"return status: %"PRIu64"\n", e[0].Integer.Value));
    411  1.1.2.2  uebayasi 
    412  1.1.2.2  uebayasi 	if (e[0].Integer.Value != 0) {
    413  1.1.2.2  uebayasi 		rv = AE_BAD_VALUE;
    414  1.1.2.2  uebayasi 		goto out;
    415  1.1.2.2  uebayasi 	}
    416  1.1.2.2  uebayasi 
    417  1.1.2.2  uebayasi 	/*
    418  1.1.2.2  uebayasi 	 * For read operations, copy data to user buffer.
    419  1.1.2.2  uebayasi 	 */
    420  1.1.2.2  uebayasi 	if (I2C_OP_READ_P(op)) {
    421  1.1.2.2  uebayasi 
    422  1.1.2.2  uebayasi 		if (p->Package.Count < 3) {
    423  1.1.2.2  uebayasi 			rv = AE_LIMIT;
    424  1.1.2.2  uebayasi 			goto out;
    425      1.1  pgoyette 		}
    426      1.1  pgoyette 
    427  1.1.2.2  uebayasi 		if (e[1].Type != ACPI_TYPE_INTEGER) {
    428  1.1.2.2  uebayasi 			rv = AE_TYPE;
    429  1.1.2.2  uebayasi 			goto out;
    430  1.1.2.2  uebayasi 		}
    431  1.1.2.2  uebayasi 
    432  1.1.2.2  uebayasi 		xlen = e[1].Integer.Value;
    433  1.1.2.2  uebayasi 
    434  1.1.2.2  uebayasi 		if (xlen > len)
    435  1.1.2.2  uebayasi 			xlen = len;
    436  1.1.2.2  uebayasi 
    437  1.1.2.2  uebayasi 		switch (e[2].Type) {
    438  1.1.2.2  uebayasi 
    439  1.1.2.2  uebayasi 		case ACPI_TYPE_BUFFER:
    440  1.1.2.2  uebayasi 
    441  1.1.2.2  uebayasi 			if (xlen == 0) {
    442  1.1.2.2  uebayasi 				rv = AE_LIMIT;
    443  1.1.2.2  uebayasi 				goto out;
    444  1.1.2.2  uebayasi 			}
    445  1.1.2.2  uebayasi 
    446  1.1.2.2  uebayasi 			xb = e[2].Buffer.Pointer;
    447  1.1.2.2  uebayasi 
    448  1.1.2.2  uebayasi 			if (xb == NULL) {
    449  1.1.2.2  uebayasi 				rv = AE_NULL_OBJECT;
    450  1.1.2.2  uebayasi 				goto out;
    451  1.1.2.2  uebayasi 			}
    452  1.1.2.2  uebayasi 
    453  1.1.2.2  uebayasi 			(void)memcpy(b, xb, xlen);
    454  1.1.2.2  uebayasi 			break;
    455  1.1.2.2  uebayasi 
    456  1.1.2.2  uebayasi 		case ACPI_TYPE_INTEGER:
    457  1.1.2.2  uebayasi 
    458  1.1.2.2  uebayasi 			if (xlen > 0)
    459  1.1.2.2  uebayasi 				*b++ = e[2].Integer.Value & 0xff;
    460  1.1.2.2  uebayasi 
    461  1.1.2.2  uebayasi 			if (xlen > 1)
    462  1.1.2.2  uebayasi 				*b = e[2].Integer.Value >> 8;
    463  1.1.2.2  uebayasi 
    464  1.1.2.2  uebayasi 			break;
    465  1.1.2.2  uebayasi 
    466  1.1.2.2  uebayasi 		default:
    467  1.1.2.2  uebayasi 			rv = AE_TYPE;
    468  1.1.2.2  uebayasi 			goto out;
    469      1.1  pgoyette 		}
    470      1.1  pgoyette 	}
    471  1.1.2.2  uebayasi 
    472  1.1.2.2  uebayasi out:
    473  1.1.2.2  uebayasi 	if (smbuf.Pointer != NULL)
    474      1.1  pgoyette 		ACPI_FREE(smbuf.Pointer);
    475      1.1  pgoyette 
    476  1.1.2.2  uebayasi 	if (ACPI_SUCCESS(rv))
    477  1.1.2.2  uebayasi 		return 0;
    478  1.1.2.2  uebayasi 
    479  1.1.2.2  uebayasi 	ACPI_DEBUG_PRINT((ACPI_DB_DEBUG_OBJECT, "failed to "
    480  1.1.2.2  uebayasi 		"evaluate %s: %s\n", path, AcpiFormatException(rv)));
    481  1.1.2.2  uebayasi 
    482  1.1.2.2  uebayasi 	return 1;
    483      1.1  pgoyette }
    484      1.1  pgoyette 
    485      1.1  pgoyette /*
    486  1.1.2.2  uebayasi  * Whether triggered by periodic polling or a Notify(),
    487  1.1.2.2  uebayasi  * retrieve all pending SMBus device alerts.
    488      1.1  pgoyette  */
    489      1.1  pgoyette static void
    490      1.1  pgoyette acpi_smbus_alerts(struct acpi_smbus_softc *sc)
    491      1.1  pgoyette {
    492  1.1.2.2  uebayasi 	const ACPI_HANDLE hdl = sc->sc_devnode->ad_handle;
    493      1.1  pgoyette 	ACPI_OBJECT *e, *p;
    494  1.1.2.2  uebayasi 	ACPI_BUFFER alert;
    495  1.1.2.2  uebayasi 	ACPI_STATUS rv;
    496  1.1.2.2  uebayasi 	int status = 0;
    497  1.1.2.2  uebayasi 	uint8_t addr;
    498      1.1  pgoyette 
    499      1.1  pgoyette 	do {
    500  1.1.2.2  uebayasi 		rv = acpi_eval_struct(hdl, "_SBA", &alert);
    501  1.1.2.2  uebayasi 
    502      1.1  pgoyette 		if (ACPI_FAILURE(rv)) {
    503      1.1  pgoyette 			status = 1;
    504      1.1  pgoyette 			goto done;
    505      1.1  pgoyette 		}
    506      1.1  pgoyette 
    507      1.1  pgoyette 		p = alert.Pointer;
    508  1.1.2.2  uebayasi 
    509  1.1.2.2  uebayasi 		if (p->Type == ACPI_TYPE_PACKAGE && p->Package.Count >= 2) {
    510  1.1.2.2  uebayasi 
    511  1.1.2.2  uebayasi 			status = 1;
    512  1.1.2.2  uebayasi 
    513      1.1  pgoyette 			e = p->Package.Elements;
    514  1.1.2.2  uebayasi 
    515      1.1  pgoyette 			if (e[0].Type == ACPI_TYPE_INTEGER)
    516      1.1  pgoyette 				status = e[0].Integer.Value;
    517  1.1.2.2  uebayasi 
    518  1.1.2.2  uebayasi 			if (status == 0 && e[1].Type == ACPI_TYPE_INTEGER) {
    519  1.1.2.2  uebayasi 				addr = e[1].Integer.Value;
    520  1.1.2.2  uebayasi 
    521  1.1.2.2  uebayasi 				aprint_debug_dev(sc->sc_dv,
    522  1.1.2.2  uebayasi 				    "alert for 0x%x\n", addr);
    523  1.1.2.2  uebayasi 
    524      1.1  pgoyette 				(void)iic_smbus_intr(&sc->sc_i2c_tag);
    525      1.1  pgoyette 			}
    526      1.1  pgoyette 		}
    527      1.1  pgoyette done:
    528      1.1  pgoyette 		if (alert.Pointer != NULL)
    529      1.1  pgoyette 			ACPI_FREE(alert.Pointer);
    530  1.1.2.2  uebayasi 
    531      1.1  pgoyette 	} while (status == 0);
    532      1.1  pgoyette }
    533      1.1  pgoyette 
    534      1.1  pgoyette static void
    535      1.1  pgoyette acpi_smbus_tick(void *opaque)
    536      1.1  pgoyette {
    537      1.1  pgoyette 	device_t dv = opaque;
    538      1.1  pgoyette 	struct acpi_smbus_softc *sc = device_private(dv);
    539      1.1  pgoyette 
    540      1.1  pgoyette 	acpi_smbus_alerts(sc);
    541      1.1  pgoyette 
    542      1.1  pgoyette 	callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
    543      1.1  pgoyette }
    544      1.1  pgoyette 
    545      1.1  pgoyette static void
    546  1.1.2.1  uebayasi acpi_smbus_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
    547      1.1  pgoyette {
    548      1.1  pgoyette 	device_t dv = opaque;
    549      1.1  pgoyette 	struct acpi_smbus_softc *sc = device_private(dv);
    550      1.1  pgoyette 
    551      1.1  pgoyette 	aprint_debug_dev(dv, "received notify message 0x%x\n", notify);
    552  1.1.2.2  uebayasi 
    553      1.1  pgoyette 	acpi_smbus_alerts(sc);
    554      1.1  pgoyette }
    555