Skip to content

Commit 03e9a0e

Browse files
committed
ACPI: EC: Consolidate event handler installation code
Commit 406857f ("ACPI: EC: add support for hardware-reduced systems") made ec_install_handlers() return an error on failures to configure a GPIO IRQ for the EC, but that is inconsistent with the handling of the GPE event handler installation failures even though it is exactly the same issue and the driver can respond to it in the same way in both cases (the EC can be actively polled for events through its registers if the event handler installation fails). Moreover, it requires acpi_ec_add() to take that special case into account and disagrees with the ec_install_handlers() header comment. For this reason, rework the event handler installation code in ec_install_handlers() to explicitly take deferred probing (that may be needed in the GPIO IRQ case) into account and to avoid failing the EC initialization in any other case. Among other things, reduce code duplication between install_gpe_event_handler() and install_gpio_irq_event_handler() by moving some code from there into ec_install_handlers() itself and simplify the error code path in acpi_ec_add(). While at it, turn the ec_install_handlers() header comment into a proper kerneldoc one and add some general control flow information to it. Signed-off-by: Rafael J. Wysocki <[email protected]> Tested-by: Jian-Hong Pan <[email protected]>
1 parent 3d9b8dd commit 03e9a0e

File tree

1 file changed

+61
-53
lines changed

1 file changed

+61
-53
lines changed

drivers/acpi/ec.c

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,54 +1427,43 @@ ec_parse_device(acpi_handle handle, u32 Level, void *context, void **retval)
14271427
return AE_CTRL_TERMINATE;
14281428
}
14291429

1430-
static void install_gpe_event_handler(struct acpi_ec *ec)
1431-
{
1432-
acpi_status status =
1433-
acpi_install_gpe_raw_handler(NULL, ec->gpe,
1434-
ACPI_GPE_EDGE_TRIGGERED,
1435-
&acpi_ec_gpe_handler,
1436-
ec);
1437-
if (ACPI_SUCCESS(status)) {
1438-
/* This is not fatal as we can poll EC events */
1439-
set_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags);
1440-
acpi_ec_leave_noirq(ec);
1441-
if (test_bit(EC_FLAGS_STARTED, &ec->flags) &&
1442-
ec->reference_count >= 1)
1443-
acpi_ec_enable_gpe(ec, true);
1444-
}
1445-
}
1446-
1447-
/* ACPI reduced hardware platforms use a GpioInt specified in _CRS. */
1448-
static int install_gpio_irq_event_handler(struct acpi_ec *ec,
1449-
struct acpi_device *device)
1430+
static bool install_gpe_event_handler(struct acpi_ec *ec)
14501431
{
1451-
int irq = acpi_dev_gpio_irq_get(device, 0);
1452-
int ret;
1453-
1454-
if (irq < 0)
1455-
return irq;
1432+
acpi_status status;
14561433

1457-
ret = request_irq(irq, acpi_ec_irq_handler, IRQF_SHARED,
1458-
"ACPI EC", ec);
1434+
status = acpi_install_gpe_raw_handler(NULL, ec->gpe,
1435+
ACPI_GPE_EDGE_TRIGGERED,
1436+
&acpi_ec_gpe_handler, ec);
1437+
if (ACPI_FAILURE(status))
1438+
return false;
14591439

1460-
/*
1461-
* Unlike the GPE case, we treat errors here as fatal, we'll only
1462-
* implement GPIO polling if we find a case that needs it.
1463-
*/
1464-
if (ret < 0)
1465-
return ret;
1440+
if (test_bit(EC_FLAGS_STARTED, &ec->flags) && ec->reference_count >= 1)
1441+
acpi_ec_enable_gpe(ec, true);
14661442

1467-
ec->irq = irq;
1468-
set_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags);
1469-
acpi_ec_leave_noirq(ec);
1443+
return true;
1444+
}
14701445

1471-
return 0;
1446+
static bool install_gpio_irq_event_handler(struct acpi_ec *ec)
1447+
{
1448+
return request_irq(ec->irq, acpi_ec_irq_handler, IRQF_SHARED,
1449+
"ACPI EC", ec) >= 0;
14721450
}
14731451

1474-
/*
1475-
* Note: This function returns an error code only when the address space
1476-
* handler is not installed, which means "not able to handle
1477-
* transactions".
1452+
/**
1453+
* ec_install_handlers - Install service callbacks and register query methods.
1454+
* @ec: Target EC.
1455+
* @device: ACPI device object corresponding to @ec.
1456+
*
1457+
* Install a handler for the EC address space type unless it has been installed
1458+
* already. If @device is not NULL, also look for EC query methods in the
1459+
* namespace and register them, and install an event (either GPE or GPIO IRQ)
1460+
* handler for the EC, if possible.
1461+
*
1462+
* Return:
1463+
* -ENODEV if the address space handler cannot be installed, which means
1464+
* "unable to handle transactions",
1465+
* -EPROBE_DEFER if GPIO IRQ acquisition needs to be deferred,
1466+
* or 0 (success) otherwise.
14781467
*/
14791468
static int ec_install_handlers(struct acpi_ec *ec, struct acpi_device *device)
14801469
{
@@ -1498,6 +1487,19 @@ static int ec_install_handlers(struct acpi_ec *ec, struct acpi_device *device)
14981487
if (!device)
14991488
return 0;
15001489

1490+
if (ec->gpe < 0) {
1491+
/* ACPI reduced hardware platforms use a GpioInt from _CRS. */
1492+
int irq = acpi_dev_gpio_irq_get(device, 0);
1493+
/*
1494+
* Bail out right away for deferred probing or complete the
1495+
* initialization regardless of any other errors.
1496+
*/
1497+
if (irq == -EPROBE_DEFER)
1498+
return -EPROBE_DEFER;
1499+
else if (irq >= 0)
1500+
ec->irq = irq;
1501+
}
1502+
15011503
if (!test_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags)) {
15021504
/* Find and register all query methods */
15031505
acpi_walk_namespace(ACPI_TYPE_METHOD, ec->handle, 1,
@@ -1506,13 +1508,21 @@ static int ec_install_handlers(struct acpi_ec *ec, struct acpi_device *device)
15061508
set_bit(EC_FLAGS_QUERY_METHODS_INSTALLED, &ec->flags);
15071509
}
15081510
if (!test_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags)) {
1509-
if (ec->gpe >= 0) {
1510-
install_gpe_event_handler(ec);
1511-
} else {
1512-
int ret = install_gpio_irq_event_handler(ec, device);
1513-
if (ret)
1514-
return ret;
1511+
bool ready = false;
1512+
1513+
if (ec->gpe >= 0)
1514+
ready = install_gpe_event_handler(ec);
1515+
else if (ec->irq >= 0)
1516+
ready = install_gpio_irq_event_handler(ec);
1517+
1518+
if (ready) {
1519+
set_bit(EC_FLAGS_EVENT_HANDLER_INSTALLED, &ec->flags);
1520+
acpi_ec_leave_noirq(ec);
15151521
}
1522+
/*
1523+
* Failures to install an event handler are not fatal, because
1524+
* the EC can be polled for events.
1525+
*/
15161526
}
15171527
/* EC is fully operational, allow queries */
15181528
acpi_ec_enable_event(ec);
@@ -1625,7 +1635,7 @@ static int acpi_ec_add(struct acpi_device *device)
16251635
status = ec_parse_device(device->handle, 0, ec, NULL);
16261636
if (status != AE_CTRL_TERMINATE) {
16271637
ret = -EINVAL;
1628-
goto err_alloc;
1638+
goto err;
16291639
}
16301640

16311641
if (boot_ec && ec->command_addr == boot_ec->command_addr &&
@@ -1646,7 +1656,7 @@ static int acpi_ec_add(struct acpi_device *device)
16461656

16471657
ret = acpi_ec_setup(ec, device);
16481658
if (ret)
1649-
goto err_query;
1659+
goto err;
16501660

16511661
if (ec == boot_ec)
16521662
acpi_handle_info(boot_ec->handle,
@@ -1666,12 +1676,10 @@ static int acpi_ec_add(struct acpi_device *device)
16661676
acpi_handle_debug(ec->handle, "enumerated.\n");
16671677
return 0;
16681678

1669-
err_query:
1670-
if (ec != boot_ec)
1671-
acpi_ec_remove_query_handlers(ec, true, 0);
1672-
err_alloc:
1679+
err:
16731680
if (ec != boot_ec)
16741681
acpi_ec_free(ec);
1682+
16751683
return ret;
16761684
}
16771685

0 commit comments

Comments
 (0)