blob: 5523ba7d764dff989c108f5262cbc283c95f329f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/types.h>
34#include <linux/acpi.h>
35#include <acpi/acpi_bus.h>
36#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Len Browna192a952009-07-28 16:45:54 -040038#define PREFIX "ACPI: "
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define _COMPONENT ACPI_CONTAINER_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050041ACPI_MODULE_NAME("container");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Thomas Renninger1ba90e32007-07-23 14:44:41 +020043static const struct acpi_device_id container_device_ids[] = {
44 {"ACPI0004", 0},
45 {"PNP0A05", 0},
46 {"PNP0A06", 0},
47 {"", 0},
48};
Thomas Renninger1ba90e32007-07-23 14:44:41 +020049
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +010050static int container_device_attach(struct acpi_device *device,
51 const struct acpi_device_id *not_used)
52{
53 /*
54 * FIXME: This is necessary, so that acpi_eject_store() doesn't return
55 * -ENODEV for containers.
56 */
57 return 1;
58}
59
60static struct acpi_scan_handler container_device_handler = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +020061 .ids = container_device_ids,
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +010062 .attach = container_device_attach,
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Len Brown4be44fc2005-08-05 00:44:28 -040065static int is_device_present(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Len Brown4be44fc2005-08-05 00:44:28 -040067 acpi_handle temp;
68 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -040069 unsigned long long sta;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 status = acpi_get_handle(handle, "_STA", &temp);
73 if (ACPI_FAILURE(status))
Bjorn Helgaasa0bd4ac2007-04-25 14:17:39 -040074 return 1; /* _STA not found, assume device present */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
77 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -040078 return 0; /* Firmware error */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Bjorn Helgaasa0bd4ac2007-04-25 14:17:39 -040080 return ((sta & ACPI_STA_DEVICE_PRESENT) == ACPI_STA_DEVICE_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Len Brown4be44fc2005-08-05 00:44:28 -040083static void container_notify_cb(acpi_handle handle, u32 type, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Len Brown4be44fc2005-08-05 00:44:28 -040085 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int result;
87 int present;
88 acpi_status status;
Toshi Kani0c67dc22012-05-23 20:25:23 -060089 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Len Brown4be44fc2005-08-05 00:44:28 -040090
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010091 acpi_scan_lock_acquire();
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 switch (type) {
94 case ACPI_NOTIFY_BUS_CHECK:
95 /* Fall through */
96 case ACPI_NOTIFY_DEVICE_CHECK:
Toshi Kani3d78bd92012-11-20 23:42:29 +000097 pr_debug("Container driver received %s event\n",
Len Brown4be44fc2005-08-05 00:44:28 -040098 (type == ACPI_NOTIFY_BUS_CHECK) ?
99 "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK");
Toshi Kani0c67dc22012-05-23 20:25:23 -0600100
101 present = is_device_present(handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 status = acpi_bus_get_device(handle, &device);
Toshi Kani0c67dc22012-05-23 20:25:23 -0600103 if (!present) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (ACPI_SUCCESS(status)) {
105 /* device exist and this is a remove request */
Toshi Kani0c67dc22012-05-23 20:25:23 -0600106 device->flags.eject_pending = 1;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800107 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Toshi Kani0c67dc22012-05-23 20:25:23 -0600110 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Toshi Kani0c67dc22012-05-23 20:25:23 -0600112
113 if (!ACPI_FAILURE(status) || device)
114 break;
115
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100116 result = acpi_bus_scan(handle);
Toshi Kani0c67dc22012-05-23 20:25:23 -0600117 if (result) {
Toshi Kani3d78bd92012-11-20 23:42:29 +0000118 acpi_handle_warn(handle, "Failed to add container\n");
Toshi Kani0c67dc22012-05-23 20:25:23 -0600119 break;
120 }
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100121 result = acpi_bus_get_device(handle, &device);
122 if (result) {
123 acpi_handle_warn(handle, "Missing device object\n");
124 break;
125 }
Toshi Kani0c67dc22012-05-23 20:25:23 -0600126
127 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
128 ost_code = ACPI_OST_SC_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 break;
Toshi Kani0c67dc22012-05-23 20:25:23 -0600130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 case ACPI_NOTIFY_EJECT_REQUEST:
132 if (!acpi_bus_get_device(handle, &device) && device) {
Toshi Kani0c67dc22012-05-23 20:25:23 -0600133 device->flags.eject_pending = 1;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800134 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100135 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 break;
Toshi Kani0c67dc22012-05-23 20:25:23 -0600138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 default:
Toshi Kani0c67dc22012-05-23 20:25:23 -0600140 /* non-hotplug event; possibly handled by other handler */
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
Toshi Kani0c67dc22012-05-23 20:25:23 -0600143
144 /* Inform firmware that the hotplug operation has completed */
145 (void) acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100146
147 out:
148 acpi_scan_lock_release();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +0100151static bool is_container(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Len Brown4be44fc2005-08-05 00:44:28 -0400153 struct acpi_device_info *info;
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +0100154 bool ret = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +0100156 if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
157 return false;
158
159 if (info->valid & ACPI_VALID_HID) {
160 const struct acpi_device_id *id;
161
162 for (id = container_device_ids; id->id[0]; id++) {
163 ret = !strcmp((char *)id->id, info->hardware_id.string);
164 if (ret)
165 break;
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Bob Moore15b8dd52009-06-29 13:39:29 +0800168 kfree(info);
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +0100169 return ret;
170}
171
172static acpi_status acpi_container_register_notify_handler(acpi_handle handle,
173 u32 lvl, void *ctxt,
174 void **retv)
175{
176 if (is_container(handle))
177 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
178 container_notify_cb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Patrick Mocheld550d982006-06-27 00:41:40 -0400180 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +0100183void __init acpi_container_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +0100185 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
186 acpi_container_register_notify_handler, NULL,
187 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +0100189 acpi_scan_add_handler(&container_device_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}