Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * acpi_container.c - ACPI Generic Container Driver |
| 3 | * ($Revision: ) |
| 4 | * |
| 5 | * Copyright (C) 2004 Anil S Keshavamurthy (anil.s.keshavamurthy@intel.com) |
| 6 | * Copyright (C) 2004 Keiichiro Tokunaga (tokunaga.keiich@jp.fujitsu.com) |
| 7 | * Copyright (C) 2004 Motoyuki Ito (motoyuki@soft.fujitsu.com) |
| 8 | * Copyright (C) 2004 Intel Corp. |
| 9 | * Copyright (C) 2004 FUJITSU LIMITED |
| 10 | * |
| 11 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or (at |
| 16 | * your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, but |
| 19 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 26 | * |
| 27 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 28 | */ |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/types.h> |
| 34 | #include <linux/acpi.h> |
| 35 | #include <acpi/acpi_bus.h> |
| 36 | #include <acpi/acpi_drivers.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Andy Shevchenko | 2f9b06f | 2013-03-11 09:17:07 +0000 | [diff] [blame^] | 38 | #include "internal.h" |
| 39 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 40 | #define PREFIX "ACPI: " |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #define _COMPONENT ACPI_CONTAINER_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 43 | ACPI_MODULE_NAME("container"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 45 | static const struct acpi_device_id container_device_ids[] = { |
| 46 | {"ACPI0004", 0}, |
| 47 | {"PNP0A05", 0}, |
| 48 | {"PNP0A06", 0}, |
| 49 | {"", 0}, |
| 50 | }; |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 51 | |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 52 | static int container_device_attach(struct acpi_device *device, |
| 53 | const struct acpi_device_id *not_used) |
| 54 | { |
| 55 | /* |
| 56 | * FIXME: This is necessary, so that acpi_eject_store() doesn't return |
| 57 | * -ENODEV for containers. |
| 58 | */ |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | static struct acpi_scan_handler container_device_handler = { |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 63 | .ids = container_device_ids, |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 64 | .attach = container_device_attach, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 67 | static int is_device_present(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 69 | acpi_handle temp; |
| 70 | acpi_status status; |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 71 | unsigned long long sta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | status = acpi_get_handle(handle, "_STA", &temp); |
| 75 | if (ACPI_FAILURE(status)) |
Bjorn Helgaas | a0bd4ac | 2007-04-25 14:17:39 -0400 | [diff] [blame] | 76 | return 1; /* _STA not found, assume device present */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | status = acpi_evaluate_integer(handle, "_STA", NULL, &sta); |
| 79 | if (ACPI_FAILURE(status)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 80 | return 0; /* Firmware error */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Bjorn Helgaas | a0bd4ac | 2007-04-25 14:17:39 -0400 | [diff] [blame] | 82 | return ((sta & ACPI_STA_DEVICE_PRESENT) == ACPI_STA_DEVICE_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 85 | static void container_notify_cb(acpi_handle handle, u32 type, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 87 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | int result; |
| 89 | int present; |
| 90 | acpi_status status; |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 91 | u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 92 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 93 | acpi_scan_lock_acquire(); |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | switch (type) { |
| 96 | case ACPI_NOTIFY_BUS_CHECK: |
| 97 | /* Fall through */ |
| 98 | case ACPI_NOTIFY_DEVICE_CHECK: |
Toshi Kani | 3d78bd9 | 2012-11-20 23:42:29 +0000 | [diff] [blame] | 99 | pr_debug("Container driver received %s event\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 100 | (type == ACPI_NOTIFY_BUS_CHECK) ? |
| 101 | "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"); |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 102 | |
| 103 | present = is_device_present(handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | status = acpi_bus_get_device(handle, &device); |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 105 | if (!present) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (ACPI_SUCCESS(status)) { |
| 107 | /* device exist and this is a remove request */ |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 108 | device->flags.eject_pending = 1; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 109 | kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 110 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 112 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 114 | |
| 115 | if (!ACPI_FAILURE(status) || device) |
| 116 | break; |
| 117 | |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 118 | result = acpi_bus_scan(handle); |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 119 | if (result) { |
Toshi Kani | 3d78bd9 | 2012-11-20 23:42:29 +0000 | [diff] [blame] | 120 | acpi_handle_warn(handle, "Failed to add container\n"); |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 121 | break; |
| 122 | } |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 123 | result = acpi_bus_get_device(handle, &device); |
| 124 | if (result) { |
| 125 | acpi_handle_warn(handle, "Missing device object\n"); |
| 126 | break; |
| 127 | } |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 128 | |
| 129 | kobject_uevent(&device->dev.kobj, KOBJ_ONLINE); |
| 130 | ost_code = ACPI_OST_SC_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | break; |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | case ACPI_NOTIFY_EJECT_REQUEST: |
| 134 | if (!acpi_bus_get_device(handle, &device) && device) { |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 135 | device->flags.eject_pending = 1; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 136 | kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 137 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | break; |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | default: |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 142 | /* non-hotplug event; possibly handled by other handler */ |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 143 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
Toshi Kani | 0c67dc2 | 2012-05-23 20:25:23 -0600 | [diff] [blame] | 145 | |
| 146 | /* Inform firmware that the hotplug operation has completed */ |
| 147 | (void) acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 148 | |
| 149 | out: |
| 150 | acpi_scan_lock_release(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 153 | static bool is_container(acpi_handle handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 155 | struct acpi_device_info *info; |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 156 | bool ret = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 158 | if (ACPI_FAILURE(acpi_get_object_info(handle, &info))) |
| 159 | return false; |
| 160 | |
| 161 | if (info->valid & ACPI_VALID_HID) { |
| 162 | const struct acpi_device_id *id; |
| 163 | |
| 164 | for (id = container_device_ids; id->id[0]; id++) { |
| 165 | ret = !strcmp((char *)id->id, info->hardware_id.string); |
| 166 | if (ret) |
| 167 | break; |
| 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 170 | kfree(info); |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static acpi_status acpi_container_register_notify_handler(acpi_handle handle, |
| 175 | u32 lvl, void *ctxt, |
| 176 | void **retv) |
| 177 | { |
| 178 | if (is_container(handle)) |
| 179 | acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
| 180 | container_notify_cb, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 182 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 185 | void __init acpi_container_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 187 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX, |
| 188 | acpi_container_register_notify_handler, NULL, |
| 189 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 191 | acpi_scan_add_handler(&container_device_handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |