Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev/acpi Refactor to make this more readable. No functio...



details:   https://anonhg.NetBSD.org/src/rev/348957384cf6
branches:  trunk
changeset: 756699:348957384cf6
user:      jruoho <jruoho%NetBSD.org@localhost>
date:      Thu Jul 29 11:03:09 2010 +0000

description:
Refactor to make this more readable. No functional change.

diffstat:

 sys/dev/acpi/smbus_acpi.c |  414 +++++++++++++++++++++++++++++----------------
 1 files changed, 262 insertions(+), 152 deletions(-)

diffs (truncated from 579 to 300 lines):

diff -r f4a314cf2e12 -r 348957384cf6 sys/dev/acpi/smbus_acpi.c
--- a/sys/dev/acpi/smbus_acpi.c Thu Jul 29 10:54:50 2010 +0000
+++ b/sys/dev/acpi/smbus_acpi.c Thu Jul 29 11:03:09 2010 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: smbus_acpi.c,v 1.12 2010/07/28 16:29:11 jruoho Exp $ */
+/* $NetBSD: smbus_acpi.c,v 1.13 2010/07/29 11:03:09 jruoho Exp $ */
 
 /*-
  * Copyright (c) 2009 The NetBSD Foundation, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: smbus_acpi.c,v 1.12 2010/07/28 16:29:11 jruoho Exp $");
+__KERNEL_RCSID(0, "$NetBSD: smbus_acpi.c,v 1.13 2010/07/29 11:03:09 jruoho Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -54,7 +54,7 @@
 ACPI_MODULE_NAME               ("smbus_acpi")
 
 /*
- * ACPI SMBus CMI protocol codes
+ * ACPI SMBus CMI protocol codes.
  */
 #define        ACPI_SMBUS_RD_QUICK     0x03
 #define        ACPI_SMBUS_RCV_BYTE     0x05
@@ -80,6 +80,7 @@
 static int     acpi_smbus_match(device_t, cfdata_t, void *);
 static void    acpi_smbus_attach(device_t, device_t, void *);
 static int     acpi_smbus_detach(device_t, int);
+static int     acpi_smbus_poll_alert(ACPI_HANDLE, int *);
 static int     acpi_smbus_acquire_bus(void *, int);
 static void    acpi_smbus_release_bus(void *, int);
 static int     acpi_smbus_exec(void *, i2c_op_t, i2c_addr_t, const void *,
@@ -122,17 +123,10 @@
 CFATTACH_DECL_NEW(acpismbus, sizeof(struct acpi_smbus_softc),
     acpi_smbus_match, acpi_smbus_attach, acpi_smbus_detach, NULL);
 
-/*
- * acpi_smbus_match: autoconf(9) match routine
- */
 static int
 acpi_smbus_match(device_t parent, cfdata_t match, void *aux)
 {
        struct acpi_attach_args *aa = aux;
-       int r = 0;
-       ACPI_STATUS rv;
-       ACPI_BUFFER smi_buf;
-       ACPI_OBJECT *e, *p;
 
        if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
                return 0;
@@ -140,39 +134,15 @@
        if (acpi_match_hid(aa->aa_node->ad_devinfo, smbus_acpi_ids) == 0)
                return 0;
 
-       /* Ensure that device's CMI version is supported */
-       rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
-       if (ACPI_FAILURE(rv))
-               goto done;
-
-       p = smi_buf.Pointer;
-       if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
-           p->Package.Count >= 1) {
-               e = p->Package.Elements;
-               if (e[0].Type == ACPI_TYPE_INTEGER &&
-                   e[0].Integer.Value == 0x10)
-                       r = 1;
-       }
-done:
-       if (smi_buf.Pointer != NULL)
-               ACPI_FREE(smi_buf.Pointer);
-
-       return r;
+       return acpi_smbus_poll_alert(aa->aa_node->ad_handle, NULL);
 }
 
-/*
- * acpitz_attach: autoconf(9) attach routine
- */
 static void
 acpi_smbus_attach(device_t parent, device_t self, void *aux)
 {
        struct acpi_smbus_softc *sc = device_private(self);
        struct acpi_attach_args *aa = aux;
        struct i2cbus_attach_args iba;
-       ACPI_STATUS rv;
-       ACPI_BUFFER smi_buf;
-       ACPI_OBJECT *e, *p;
-       struct SMB_INFO *info;
 
        aprint_naive("\n");
 
@@ -180,28 +150,15 @@
        sc->sc_dv = self;
        sc->sc_poll_alert = 2;
 
-       /* Attach I2C bus */
+       /* Attach I2C bus. */
        mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
+
        sc->sc_i2c_tag.ic_cookie = sc;
        sc->sc_i2c_tag.ic_acquire_bus = acpi_smbus_acquire_bus;
        sc->sc_i2c_tag.ic_release_bus = acpi_smbus_release_bus;
        sc->sc_i2c_tag.ic_exec = acpi_smbus_exec;
 
-       /* Retrieve polling interval for SMBus Alerts */
-       rv = acpi_eval_struct(aa->aa_node->ad_handle, "_SBI", &smi_buf);
-       if (ACPI_SUCCESS(rv)) {
-               p = smi_buf.Pointer;
-               if (p != NULL && p->Type == ACPI_TYPE_PACKAGE &&
-                   p->Package.Count >= 2) {
-                       e = p->Package.Elements;
-                       if (e[1].Type == ACPI_TYPE_BUFFER) {
-                               info = (struct SMB_INFO *)(e[1].Buffer.Pointer);
-                               sc->sc_poll_alert = info->poll_int;
-                       }
-               }
-       }
-       if (smi_buf.Pointer != NULL)
-               ACPI_FREE(smi_buf.Pointer);
+       (void)acpi_smbus_poll_alert(aa->aa_node->ad_handle,&sc->sc_poll_alert);
 
        /* If failed, fall-back to polling. */
        if (acpi_register_notify(sc->sc_devnode,
@@ -211,18 +168,19 @@
        callout_init(&sc->sc_callout, 0);
        callout_setfunc(&sc->sc_callout, acpi_smbus_tick, self);
 
-       if (!pmf_device_register(self, NULL, NULL))
-               aprint_error(": couldn't establish power handler\n");
-
        if (sc->sc_poll_alert != 0) {
-               aprint_debug(" alert_poll %d sec", sc->sc_poll_alert);
+               aprint_debug(": alert_poll %d sec", sc->sc_poll_alert);
                callout_schedule(&sc->sc_callout, sc->sc_poll_alert * hz);
        }
+
        aprint_normal("\n");
 
-       memset(&iba, 0, sizeof(iba));
+       (void)memset(&iba, 0, sizeof(iba));
+       (void)pmf_device_register(self, NULL, NULL);
+
        iba.iba_tag = &sc->sc_i2c_tag;
-       config_found_ia(self, "i2cbus", &iba, iicbus_print);
+
+       (void)config_found_ia(self, "i2cbus", &iba, iicbus_print);
 }
 
 static int
@@ -242,11 +200,72 @@
 }
 
 static int
+acpi_smbus_poll_alert(ACPI_HANDLE hdl, int *alert)
+{
+       struct SMB_INFO *info;
+       ACPI_BUFFER smi_buf;
+       ACPI_OBJECT *e, *p;
+       ACPI_STATUS rv;
+
+       /*
+        * Retrieve polling interval for SMBus Alerts.
+        */
+       rv = acpi_eval_struct(hdl, "_SBI", &smi_buf);
+
+       if (ACPI_FAILURE(rv))
+               return 0;
+
+       p = smi_buf.Pointer;
+
+       if (p->Type != ACPI_TYPE_PACKAGE) {
+               rv = AE_TYPE;
+               goto out;
+       }
+
+       if (p->Package.Count == 0) {
+               rv = AE_LIMIT;
+               goto out;
+       }
+
+       e = p->Package.Elements;
+
+       if (e[0].Type != ACPI_TYPE_INTEGER) {
+               rv = AE_TYPE;
+               goto out;
+       }
+
+       /* Verify CMI version. */
+       if (e[0].Integer.Value != 0x10) {
+               rv = AE_SUPPORT;
+               goto out;
+       }
+
+       if (alert != NULL) {
+
+               if (p->Package.Count < 2)
+                       goto out;
+
+               if (e[1].Type != ACPI_TYPE_BUFFER)
+                       goto out;
+
+               info = (struct SMB_INFO *)(e[1].Buffer.Pointer);
+               *alert = info->poll_int;
+       }
+
+out:
+       if (smi_buf.Pointer != NULL)
+               ACPI_FREE(smi_buf.Pointer);
+
+       return (ACPI_FAILURE(rv)) ? 0 : 1;
+}
+
+static int
 acpi_smbus_acquire_bus(void *cookie, int flags)
 {
         struct acpi_smbus_softc *sc = cookie;
 
         mutex_enter(&sc->sc_i2c_mutex);
+
         return 0;
 }
 
@@ -264,46 +283,71 @@
         struct acpi_smbus_softc *sc = cookie;
        const uint8_t *c = cmdbuf;
        uint8_t *b = buf, *xb;
-       int xlen;
-       int r = 0;
-       ACPI_BUFFER smbuf;
-       ACPI_STATUS rv;
+       const char *path;
        ACPI_OBJECT_LIST args;
        ACPI_OBJECT arg[5];
        ACPI_OBJECT *p, *e;
+       ACPI_BUFFER smbuf;
+       ACPI_STATUS rv;
+       int i, r, xlen;
+
+       /*
+        *      arg[0] : protocol
+        *      arg[1] : slave address
+        *      arg[2] : command
+        *      arg[3] : data length
+        *      arg[4] : data
+        */
+       for (i = r = 0; i < __arraycount(arg); i++)
+               arg[i].Type = ACPI_TYPE_INTEGER;
+
+       args.Pointer = arg;
 
        smbuf.Pointer = NULL;
        smbuf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
-       args.Pointer = arg;
-       arg[0].Type = ACPI_TYPE_INTEGER;        /* Protocol */
-       arg[1].Type = ACPI_TYPE_INTEGER;        /* Slave Addr */
+
        arg[1].Integer.Value = addr;
-       arg[2].Type = ACPI_TYPE_INTEGER;        /* Command */
+
        if (I2C_OP_READ_P(op)) {
+
+               path = "_SBR";
                args.Count = 3;
-               if (len == 0) {
+
+               switch (len) {
+
+               case 0:
+                       arg[0].Integer.Value = (cmdlen != 0) ?
+                           ACPI_SMBUS_RCV_BYTE : ACPI_SMBUS_RD_QUICK;
+
                        arg[2].Integer.Value = 0;
-                       if (cmdlen == 0)
-                               arg[0].Integer.Value = ACPI_SMBUS_RD_QUICK;
-                       else
-                               arg[0].Integer.Value = ACPI_SMBUS_RCV_BYTE;
-               } else
+                       break;
+
+               case 1:
+                       arg[0].Integer.Value = ACPI_SMBUS_RD_BYTE;
                        arg[2].Integer.Value = *c;
-               if (len == 1)
-                       arg[0].Integer.Value = ACPI_SMBUS_RD_BYTE;
-               else if (len == 2)
+                       break;
+
+               case 2:
                        arg[0].Integer.Value = ACPI_SMBUS_RD_WORD;
-               else if (len > 2)
+                       arg[2].Integer.Value = *c;
+                       break;
+
+               default:
                        arg[0].Integer.Value = ACPI_SMBUS_RD_BLOCK;
-               rv = AcpiEvaluateObject(sc->sc_devnode->ad_handle, "_SBR",
-                                   &args, &smbuf);



Home | Main Index | Thread Index | Old Index