blob: fdda49336560acd2b0eb766430a811d6d842bf0c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060016#include "internal.h"
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050019ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040021extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020024#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define ACPI_BUS_DEVICE_NAME "System Bus"
26
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +000027#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
Thomas Renninger620e1122010-10-01 10:54:00 +020029static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080032static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080033DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034LIST_HEAD(acpi_wakeup_device_list);
35
Zhang Ruie49bd2d2006-12-08 17:23:43 +080036struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080037 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080038 unsigned int instance_no;
39 struct list_head node;
40};
Thomas Renninger29b71a12007-07-23 14:43:51 +020041
42/*
43 * Creates hid/cid(s) string needed for modalias and uevent
44 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
45 * char *modalias: "acpi:IBM0001:ACPI0001"
46*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020047static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
48 int size)
49{
Thomas Renninger29b71a12007-07-23 14:43:51 +020050 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080051 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060052 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020053
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020054 if (list_empty(&acpi_dev->pnp.ids))
55 return 0;
56
Zhang Rui5c9fcb52008-03-20 16:40:32 +080057 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020058 size -= len;
59
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060060 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
61 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080062 if (count < 0 || count >= size)
63 return -EINVAL;
64 len += count;
65 size -= count;
66 }
67
Thomas Renninger29b71a12007-07-23 14:43:51 +020068 modalias[len] = '\0';
69 return len;
70}
71
72static ssize_t
73acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
74 struct acpi_device *acpi_dev = to_acpi_device(dev);
75 int len;
76
77 /* Device has no HID and no CID or string is >1024 */
78 len = create_modalias(acpi_dev, buf, 1024);
79 if (len <= 0)
80 return 0;
81 buf[len++] = '\n';
82 return len;
83}
84static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
85
Zhang Ruic8d72a52009-06-22 11:31:16 +080086static void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Zhang Rui26d46862008-04-29 02:35:48 -040088 struct acpi_device *device;
89 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct acpi_object_list arg_list;
91 union acpi_object arg;
92 acpi_status status = AE_OK;
93
Zhang Rui26d46862008-04-29 02:35:48 -040094 if (acpi_bus_get_device(handle, &device))
Zhang Ruic8d72a52009-06-22 11:31:16 +080095 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Zhang Rui26d46862008-04-29 02:35:48 -040097 if (!device)
Zhang Ruic8d72a52009-06-22 11:31:16 +080098 return;
Zhang Rui26d46862008-04-29 02:35:48 -040099
100 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100101 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400102
103 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800104 printk(KERN_ERR PREFIX
105 "Removing device failed\n");
Zhang Ruic8d72a52009-06-22 11:31:16 +0800106 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400107 }
108
109 /* power off device */
110 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
111 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800112 printk(KERN_WARNING PREFIX
113 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400114
115 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 arg_list.count = 1;
117 arg_list.pointer = &arg;
118 arg.type = ACPI_TYPE_INTEGER;
119 arg.integer.value = 0;
120 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
121 }
122
123 arg_list.count = 1;
124 arg_list.pointer = &arg;
125 arg.type = ACPI_TYPE_INTEGER;
126 arg.integer.value = 1;
127
128 /*
129 * TBD: _EJD support.
130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400132 if (ACPI_FAILURE(status))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800133 printk(KERN_WARNING PREFIX
134 "Eject device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Zhang Ruic8d72a52009-06-22 11:31:16 +0800136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800140acpi_eject_store(struct device *d, struct device_attribute *attr,
141 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Len Brown4be44fc2005-08-05 00:44:28 -0400143 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400144 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800146 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if ((!count) || (buf[0] != '1')) {
149 return -EINVAL;
150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800152 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 ret = -ENODEV;
154 goto err;
155 }
156#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800157 status = acpi_get_type(acpi_device->handle, &type);
158 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 ret = -ENODEV;
160 goto err;
161 }
162
Zhang Ruic8d72a52009-06-22 11:31:16 +0800163 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
Alok N Kataria74523c92008-06-13 12:54:24 -0400164err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800168static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800170static ssize_t
171acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
172 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600174 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800175}
176static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
177
178static ssize_t
179acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
180 struct acpi_device *acpi_dev = to_acpi_device(dev);
181 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
182 int result;
183
184 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600185 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800186 goto end;
187
188 result = sprintf(buf, "%s\n", (char*)path.pointer);
189 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600190end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800191 return result;
192}
193static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
194
195static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800197 acpi_status status;
198 acpi_handle temp;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800199 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800201 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800202 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800203 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600204 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800205 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600206 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800207 goto end;
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200210 if (!list_empty(&dev->pnp.ids)) {
211 result = device_create_file(&dev->dev, &dev_attr_hid);
212 if (result)
213 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200215 result = device_create_file(&dev->dev, &dev_attr_modalias);
216 if (result)
217 goto end;
218 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200219
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800220 /*
221 * If device has _EJ0, 'eject' file is created that is used to trigger
222 * hot-removal function from userland.
223 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800224 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
225 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800226 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600227end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800228 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800231static void acpi_device_remove_files(struct acpi_device *dev)
232{
233 acpi_status status;
234 acpi_handle temp;
235
236 /*
237 * If device has _EJ0, 'eject' file is created that is used to trigger
238 * hot-removal function from userland.
239 */
240 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241 if (ACPI_SUCCESS(status))
242 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800243
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600244 device_remove_file(&dev->dev, &dev_attr_modalias);
245 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600246 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800247 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800250 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200252
253int acpi_match_device_ids(struct acpi_device *device,
254 const struct acpi_device_id *ids)
255{
256 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600257 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200258
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800259 /*
260 * If the device is not present, it is unnecessary to load device
261 * driver for it.
262 */
263 if (!device->status.present)
264 return -ENODEV;
265
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600266 for (id = ids; id->id[0]; id++)
267 list_for_each_entry(hwid, &device->pnp.ids, list)
268 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200269 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200270
271 return -ENOENT;
272}
273EXPORT_SYMBOL(acpi_match_device_ids);
274
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600275static void acpi_free_ids(struct acpi_device *device)
276{
277 struct acpi_hardware_id *id, *tmp;
278
279 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
280 kfree(id->id);
281 kfree(id);
282 }
283}
284
Patrick Mochel1890a972006-12-07 20:56:31 +0800285static void acpi_device_release(struct device *dev)
286{
287 struct acpi_device *acpi_dev = to_acpi_device(dev);
288
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600289 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800290 kfree(acpi_dev);
291}
292
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800293static int acpi_bus_match(struct device *dev, struct device_driver *drv)
294{
295 struct acpi_device *acpi_dev = to_acpi_device(dev);
296 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
297
Thomas Renninger29b71a12007-07-23 14:43:51 +0200298 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800299}
300
Kay Sievers7eff2e72007-08-14 15:15:12 +0200301static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800302{
303 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200304 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800305
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200306 if (list_empty(&acpi_dev->pnp.ids))
307 return 0;
308
Kay Sievers7eff2e72007-08-14 15:15:12 +0200309 if (add_uevent_var(env, "MODALIAS="))
310 return -ENOMEM;
311 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
312 sizeof(env->buf) - env->buflen);
313 if (len >= (sizeof(env->buf) - env->buflen))
314 return -ENOMEM;
315 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000319static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
320{
321 struct acpi_device *device = data;
322
323 device->driver->ops.notify(device, event);
324}
325
326static acpi_status acpi_device_notify_fixed(void *data)
327{
328 struct acpi_device *device = data;
329
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000330 /* Fixed hardware devices have no handles */
331 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000332 return AE_OK;
333}
334
335static int acpi_device_install_notify_handler(struct acpi_device *device)
336{
337 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000338
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000339 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000340 status =
341 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
342 acpi_device_notify_fixed,
343 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000344 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000345 status =
346 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
347 acpi_device_notify_fixed,
348 device);
349 else
350 status = acpi_install_notify_handler(device->handle,
351 ACPI_DEVICE_NOTIFY,
352 acpi_device_notify,
353 device);
354
355 if (ACPI_FAILURE(status))
356 return -EINVAL;
357 return 0;
358}
359
360static void acpi_device_remove_notify_handler(struct acpi_device *device)
361{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000362 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000363 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
364 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000365 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000366 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
367 acpi_device_notify_fixed);
368 else
369 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
370 acpi_device_notify);
371}
372
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800373static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
374static int acpi_start_single_object(struct acpi_device *);
375static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800376{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800377 struct acpi_device *acpi_dev = to_acpi_device(dev);
378 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
379 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800381 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
382 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800383 if (acpi_dev->bus_ops.acpi_op_start)
384 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000385
386 if (acpi_drv->ops.notify) {
387 ret = acpi_device_install_notify_handler(acpi_dev);
388 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000389 if (acpi_drv->ops.remove)
390 acpi_drv->ops.remove(acpi_dev,
391 acpi_dev->removal_type);
392 return ret;
393 }
394 }
395
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800396 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
397 "Found driver [%s] for device [%s]\n",
398 acpi_drv->name, acpi_dev->pnp.bus_id));
399 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800400 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800401 return ret;
402}
403
404static int acpi_device_remove(struct device * dev)
405{
406 struct acpi_device *acpi_dev = to_acpi_device(dev);
407 struct acpi_driver *acpi_drv = acpi_dev->driver;
408
409 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000410 if (acpi_drv->ops.notify)
411 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800412 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800413 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800414 }
415 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700416 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800417
418 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800419 return 0;
420}
421
David Brownell55955aa2007-05-08 00:28:35 -0700422struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800423 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800424 .match = acpi_bus_match,
425 .probe = acpi_device_probe,
426 .remove = acpi_device_remove,
427 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428};
429
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000430static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800432 int result;
433 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
434 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
437 * Linkage
438 * -------
439 * Link this device to its parent and siblings.
440 */
441 INIT_LIST_HEAD(&device->children);
442 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 INIT_LIST_HEAD(&device->wakeup_list);
444
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800445 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
446 if (!new_bus_id) {
447 printk(KERN_ERR PREFIX "Memory allocation error\n");
448 return -ENOMEM;
449 }
450
Shaohua Li90905892009-04-07 10:24:29 +0800451 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800452 /*
453 * Find suitable bus_id and instance number in acpi_bus_id_list
454 * If failed, create one and link it into acpi_bus_id_list
455 */
456 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600457 if (!strcmp(acpi_device_bus_id->bus_id,
458 acpi_device_hid(device))) {
459 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800460 found = 1;
461 kfree(new_bus_id);
462 break;
463 }
464 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600465 if (!found) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800466 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600467 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800468 acpi_device_bus_id->instance_no = 0;
469 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
470 }
Kay Sievers07944692008-10-30 01:18:59 +0100471 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800472
Len Brown33b57152008-12-15 22:09:26 -0500473 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (device->wakeup.flags.valid)
477 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800478 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Patrick Mochel1890a972006-12-07 20:56:31 +0800480 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000481 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800482 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800483 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600484 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600485 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600486 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800487 goto end;
488 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800489
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800490 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600491 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100492 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
493 dev_name(&device->dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800494
Li Shaohua96333572006-12-07 20:56:46 +0800495 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800496 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600497end:
Shaohua Li90905892009-04-07 10:24:29 +0800498 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500499 if (device->parent)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800500 list_del(&device->node);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800501 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800502 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800503 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506static void acpi_device_unregister(struct acpi_device *device, int type)
507{
Shaohua Li90905892009-04-07 10:24:29 +0800508 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500509 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800513 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800516
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800517 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800518 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Zhang Rui9e89dde2006-12-07 20:56:16 +0800521/* --------------------------------------------------------------------------
522 Driver Management
523 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500525 * acpi_bus_driver_init - add a device to a driver
526 * @device: the device to add and initialize
527 * @driver: driver for the device
528 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600529 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800530 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
532static int
Len Brown4be44fc2005-08-05 00:44:28 -0400533acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Len Brown4be44fc2005-08-05 00:44:28 -0400535 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400541 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 result = driver->ops.add(device);
544 if (result) {
545 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700546 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400547 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 device->driver = driver;
551
552 /*
553 * TBD - Configuration Management: Assign resources to device based
554 * upon possible configuration and currently allocated resources.
555 */
556
Len Brown4be44fc2005-08-05 00:44:28 -0400557 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
558 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700560}
561
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400562static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700563{
564 int result = 0;
565 struct acpi_driver *driver;
566
Rajesh Shah3fb02732005-04-28 00:25:52 -0700567
568 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (driver->ops.start) {
572 result = driver->ops.start(device);
573 if (result && driver->ops.remove)
574 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Patrick Mocheld550d982006-06-27 00:41:40 -0400577 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500581 * acpi_bus_register_driver - register a driver with the ACPI bus
582 * @driver: driver being registered
583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500585 * devices that match the driver's criteria and binds. Returns zero for
586 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Len Brown4be44fc2005-08-05 00:44:28 -0400588int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Patrick Mochel1890a972006-12-07 20:56:31 +0800590 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400593 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800594 driver->drv.name = driver->name;
595 driver->drv.bus = &acpi_bus_type;
596 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Patrick Mochel1890a972006-12-07 20:56:31 +0800598 ret = driver_register(&driver->drv);
599 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Len Brown4be44fc2005-08-05 00:44:28 -0400602EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500605 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
606 * @driver: driver to unregister
607 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 * Unregisters a driver with the ACPI bus. Searches the namespace for all
609 * devices that match the driver's criteria and unbinds.
610 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400611void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Patrick Mochel1890a972006-12-07 20:56:31 +0800613 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
Len Brown4be44fc2005-08-05 00:44:28 -0400615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616EXPORT_SYMBOL(acpi_bus_unregister_driver);
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/* --------------------------------------------------------------------------
619 Device Enumeration
620 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000621static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
622{
623 acpi_status status;
624 int ret;
625 struct acpi_device *device;
626
627 /*
628 * Fixed hardware devices do not appear in the namespace and do not
629 * have handles, but we fabricate acpi_devices for them, so we have
630 * to deal with them specially.
631 */
632 if (handle == NULL)
633 return acpi_root;
634
635 do {
636 status = acpi_get_parent(handle, &handle);
637 if (status == AE_NULL_ENTRY)
638 return NULL;
639 if (ACPI_FAILURE(status))
640 return acpi_root;
641
642 ret = acpi_bus_get_device(handle, &device);
643 if (ret == 0)
644 return device;
645 } while (1);
646}
647
Len Brownc8f7a622006-07-09 17:22:28 -0400648acpi_status
649acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
650{
651 acpi_status status;
652 acpi_handle tmp;
653 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
654 union acpi_object *obj;
655
656 status = acpi_get_handle(handle, "_EJD", &tmp);
657 if (ACPI_FAILURE(status))
658 return status;
659
660 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
661 if (ACPI_SUCCESS(status)) {
662 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100663 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
664 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400665 kfree(buffer.pointer);
666 }
667 return status;
668}
669EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
670
Bob Moore8e4319c2009-06-29 13:43:27 +0800671void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673
674 /* TBD */
675
676 return;
677}
678
Zhang Rui9e89dde2006-12-07 20:56:16 +0800679static int acpi_bus_get_perf_flags(struct acpi_device *device)
680{
681 device->performance.state = ACPI_STATE_UNKNOWN;
682 return 0;
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100686acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
687 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100689 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
690 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100692 acpi_status status;
693 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100695 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return AE_BAD_PARAMETER;
697
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100698 /* _PRW */
699 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
700 if (ACPI_FAILURE(status)) {
701 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
702 return status;
703 }
704
705 package = (union acpi_object *)buffer.pointer;
706
707 if (!package || (package->package.count < 2)) {
708 status = AE_BAD_DATA;
709 goto out;
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100713 if (!element) {
714 status = AE_BAD_DATA;
715 goto out;
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (element->type == ACPI_TYPE_PACKAGE) {
718 if ((element->package.count < 2) ||
719 (element->package.elements[0].type !=
720 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100721 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
722 status = AE_BAD_DATA;
723 goto out;
724 }
725 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100727 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 (u32) element->package.elements[1].integer.value;
729 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100730 wakeup->gpe_device = NULL;
731 wakeup->gpe_number = element->integer.value;
732 } else {
733 status = AE_BAD_DATA;
734 goto out;
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 element = &(package->package.elements[1]);
738 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100739 status = AE_BAD_DATA;
740 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100742 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100745 status = AE_NO_MEMORY;
746 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100748 wakeup->resources.count = package->package.count - 2;
749 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100751 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
752 status = AE_BAD_DATA;
753 goto out;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100756 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Lin Mingbba63a22010-12-13 13:39:17 +0800759 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200760
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100761 out:
762 kfree(buffer.pointer);
763
764 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100767static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200769 struct acpi_device_id button_device_ids[] = {
770 {"PNP0C0D", 0},
771 {"PNP0C0C", 0},
772 {"PNP0C0E", 0},
773 {"", 0},
774 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100775 acpi_status status;
776 acpi_event_status event_status;
777
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100778 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100779
780 /* Power button, Lid switch always enable wakeup */
781 if (!acpi_match_device_ids(device, button_device_ids)) {
782 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100783 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100784 return;
785 }
786
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200787 status = acpi_get_gpe_status(device->wakeup.gpe_device,
788 device->wakeup.gpe_number,
789 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100790 if (status == AE_OK)
791 device->wakeup.flags.run_wake =
792 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
793}
794
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100795static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100796{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100797 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100798 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100799 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200800
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100801 /* Presence of _PRW indicates wake capable */
802 status = acpi_get_handle(device->handle, "_PRW", &temp);
803 if (ACPI_FAILURE(status))
804 return;
805
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100806 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
807 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ACPI_FAILURE(status)) {
809 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100810 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200814 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100815 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800816 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
817 * system for the ACPI device with the _PRW object.
818 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
819 * So it is necessary to call _DSW object first. Only when it is not
820 * present will the _PSW object used.
821 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200822 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
823 if (psw_error)
824 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
825 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Rafael J. Wysockibf325f952010-11-25 00:10:44 +0100828static void acpi_bus_add_power_resource(acpi_handle handle);
829
Zhang Rui9e89dde2006-12-07 20:56:16 +0800830static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800832 acpi_status status = 0;
833 acpi_handle handle = NULL;
834 u32 i = 0;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800838 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800840 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800842 device->power.flags.explicit_get = 1;
843 status = acpi_get_handle(device->handle, "_IRC", &handle);
844 if (ACPI_SUCCESS(status))
845 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800848 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800850 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800851 struct acpi_device_power_state *ps = &device->power.states[i];
852 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Zhang Rui9e89dde2006-12-07 20:56:16 +0800854 /* Evaluate "_PRx" to se if power resources are referenced */
855 acpi_evaluate_reference(device->handle, object_name, NULL,
856 &ps->resources);
857 if (ps->resources.count) {
Rafael J. Wysockibf325f952010-11-25 00:10:44 +0100858 int j;
859
Zhang Rui9e89dde2006-12-07 20:56:16 +0800860 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f952010-11-25 00:10:44 +0100861 for (j = 0; j < ps->resources.count; j++)
862 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Zhang Rui9e89dde2006-12-07 20:56:16 +0800865 /* Evaluate "_PSx" to see if we can do explicit sets */
866 object_name[2] = 'S';
867 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800868 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800869 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800870
Lin Ming1cc0c992012-04-23 09:03:49 +0800871 /*
872 * State is valid if there are means to put the device into it.
873 * D3hot is only valid if _PR3 present.
874 */
875 if (ps->resources.count ||
876 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800877 ps->flags.valid = 1;
878
879 ps->power = -1; /* Unknown - driver assigned */
880 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Zhang Rui9e89dde2006-12-07 20:56:16 +0800883 /* Set defaults for D0 and D3 states (always valid) */
884 device->power.states[ACPI_STATE_D0].flags.valid = 1;
885 device->power.states[ACPI_STATE_D0].power = 100;
886 device->power.states[ACPI_STATE_D3].flags.valid = 1;
887 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200889 /* Set D3cold's explicit_set flag if _PS3 exists. */
890 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
891 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
892
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +0100893 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 return 0;
896}
897
Len Brown4be44fc2005-08-05 00:44:28 -0400898static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Len Brown4be44fc2005-08-05 00:44:28 -0400900 acpi_status status = AE_OK;
901 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* Presence of _STA indicates 'dynamic_status' */
905 status = acpi_get_handle(device->handle, "_STA", &temp);
906 if (ACPI_SUCCESS(status))
907 device->flags.dynamic_status = 1;
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* Presence of _RMV indicates 'removable' */
910 status = acpi_get_handle(device->handle, "_RMV", &temp);
911 if (ACPI_SUCCESS(status))
912 device->flags.removable = 1;
913
914 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
915 status = acpi_get_handle(device->handle, "_EJD", &temp);
916 if (ACPI_SUCCESS(status))
917 device->flags.ejectable = 1;
918 else {
919 status = acpi_get_handle(device->handle, "_EJ0", &temp);
920 if (ACPI_SUCCESS(status))
921 device->flags.ejectable = 1;
922 }
923
924 /* Presence of _LCK indicates 'lockable' */
925 status = acpi_get_handle(device->handle, "_LCK", &temp);
926 if (ACPI_SUCCESS(status))
927 device->flags.lockable = 1;
928
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +0200929 /* Power resources cannot be power manageable. */
930 if (device->device_type == ACPI_BUS_TYPE_POWER)
931 return 0;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* Presence of _PS0|_PR0 indicates 'power manageable' */
934 status = acpi_get_handle(device->handle, "_PS0", &temp);
935 if (ACPI_FAILURE(status))
936 status = acpi_get_handle(device->handle, "_PR0", &temp);
937 if (ACPI_SUCCESS(status))
938 device->flags.power_manageable = 1;
939
Joe Perches3c5f9be42008-02-03 17:06:17 +0200940 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Patrick Mocheld550d982006-06-27 00:41:40 -0400942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +0000945static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Len Brown4be44fc2005-08-05 00:44:28 -0400947 char bus_id[5] = { '?', 0 };
948 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
949 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /*
952 * Bus ID
953 * ------
954 * The device's Bus ID is simply the object name.
955 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
956 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000957 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000959 return;
960 }
961
962 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 case ACPI_BUS_TYPE_POWER_BUTTON:
964 strcpy(device->pnp.bus_id, "PWRF");
965 break;
966 case ACPI_BUS_TYPE_SLEEP_BUTTON:
967 strcpy(device->pnp.bus_id, "SLPF");
968 break;
969 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000970 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* Clean up trailing underscores (if any) */
972 for (i = 3; i > 1; i--) {
973 if (bus_id[i] == '_')
974 bus_id[i] = '\0';
975 else
976 break;
977 }
978 strcpy(device->pnp.bus_id, bus_id);
979 break;
980 }
981}
982
Zhang Rui54735262007-01-11 02:09:09 -0500983/*
984 * acpi_bay_match - see if a device is an ejectable driver bay
985 *
986 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
987 * then we can safely call it an ejectable drive bay
988 */
989static int acpi_bay_match(struct acpi_device *device){
990 acpi_status status;
991 acpi_handle handle;
992 acpi_handle tmp;
993 acpi_handle phandle;
994
995 handle = device->handle;
996
997 status = acpi_get_handle(handle, "_EJ0", &tmp);
998 if (ACPI_FAILURE(status))
999 return -ENODEV;
1000
1001 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1002 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1003 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1004 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1005 return 0;
1006
1007 if (acpi_get_parent(handle, &phandle))
1008 return -ENODEV;
1009
1010 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1011 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1012 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1013 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1014 return 0;
1015
1016 return -ENODEV;
1017}
1018
Frank Seidel3620f2f2007-12-07 13:20:34 +01001019/*
1020 * acpi_dock_match - see if a device has a _DCK method
1021 */
1022static int acpi_dock_match(struct acpi_device *device)
1023{
1024 acpi_handle tmp;
1025 return acpi_get_handle(device->handle, "_DCK", &tmp);
1026}
1027
Thomas Renninger620e1122010-10-01 10:54:00 +02001028const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001029{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001030 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001031
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001032 if (list_empty(&device->pnp.ids))
1033 return dummy_hid;
1034
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001035 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1036 return hid->id;
1037}
1038EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001039
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001040static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1041{
1042 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001043
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001044 id = kmalloc(sizeof(*id), GFP_KERNEL);
1045 if (!id)
1046 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001047
Thomas Meyer581de592011-08-06 11:32:56 +02001048 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001049 if (!id->id) {
1050 kfree(id);
1051 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001052 }
1053
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001054 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001055}
1056
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001057/*
1058 * Old IBM workstations have a DSDT bug wherein the SMBus object
1059 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1060 * prefix. Work around this.
1061 */
1062static int acpi_ibm_smbus_match(struct acpi_device *device)
1063{
1064 acpi_handle h_dummy;
1065 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1066 int result;
1067
1068 if (!dmi_name_in_vendors("IBM"))
1069 return -ENODEV;
1070
1071 /* Look for SMBS object */
1072 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1073 if (result)
1074 return result;
1075
1076 if (strcmp("SMBS", path.pointer)) {
1077 result = -ENODEV;
1078 goto out;
1079 }
1080
1081 /* Does it have the necessary (but misnamed) methods? */
1082 result = -ENODEV;
1083 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1084 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1085 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1086 result = 0;
1087out:
1088 kfree(path.pointer);
1089 return result;
1090}
1091
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001092static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Len Brown4be44fc2005-08-05 00:44:28 -04001094 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001095 struct acpi_device_info *info;
1096 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001097 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001099 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001101 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001102 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001103 break;
1104 }
1105
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001106 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001108 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return;
1110 }
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001113 acpi_add_id(device, info->hardware_id.string);
1114 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001115 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001116 for (i = 0; i < cid_list->count; i++)
1117 acpi_add_id(device, cid_list->ids[i].string);
1118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (info->valid & ACPI_VALID_ADR) {
1120 device->pnp.bus_address = info->address;
1121 device->flags.bus_address = 1;
1122 }
Zhang Ruiae843332006-12-07 20:57:10 +08001123
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001124 kfree(info);
1125
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001126 /*
1127 * Some devices don't reliably have _HIDs & _CIDs, so add
1128 * synthetic HIDs to make sure drivers can find them.
1129 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001130 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001131 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001132 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001133 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001134 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001135 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001136 else if (!acpi_ibm_smbus_match(device))
1137 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001138 else if (!acpi_device_hid(device) &&
1139 ACPI_IS_ROOT_DEVICE(device->parent)) {
1140 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1141 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1142 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1143 }
Zhang Rui54735262007-01-11 02:09:09 -05001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 break;
1146 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001147 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001150 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001153 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001156 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001159 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 break;
1161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001164static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001166 acpi_status status;
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Context
1170 * -------
1171 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001172 * resolutions from handle->device very efficient. Fixed hardware
1173 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001175 if (!device->handle)
1176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001178 status = acpi_attach_data(device->handle,
1179 acpi_bus_data_handler, device);
1180 if (ACPI_SUCCESS(status))
1181 return 0;
1182
1183 printk(KERN_ERR PREFIX "Error attaching device data\n");
1184 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Len Brown4be44fc2005-08-05 00:44:28 -04001187static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001190 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Li Shaohua96333572006-12-07 20:56:46 +08001192 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001193 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Rui Zhang2786f6e2006-12-21 02:21:13 -05001198 /*
1199 * unbind _ADR-Based Devices when hot removal
1200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (dev->flags.bus_address) {
1202 if ((dev->parent) && (dev->parent->ops.unbind))
1203 dev->parent->ops.unbind(dev);
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1206
Patrick Mocheld550d982006-06-27 00:41:40 -04001207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001210static int acpi_add_single_object(struct acpi_device **child,
1211 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001212 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001213 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001215 int result;
1216 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001217 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Burman Yan36bcbec2006-12-19 12:56:11 -08001219 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001221 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001222 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001225 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001226 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001228 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001229 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001230 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001231
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001232 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /*
1235 * Flags
1236 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001237 * Note that we only look for object handles -- cannot evaluate objects
1238 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 */
1240 result = acpi_bus_get_flags(device);
1241 if (result)
1242 goto end;
1243
1244 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 * Initialize Device
1246 * -----------------
1247 * TBD: Synch with Core's enumeration/initialization process.
1248 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001249 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /*
1252 * Power Management
1253 * ----------------
1254 */
1255 if (device->flags.power_manageable) {
1256 result = acpi_bus_get_power_flags(device);
1257 if (result)
1258 goto end;
1259 }
1260
Len Brown4be44fc2005-08-05 00:44:28 -04001261 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * Wakeup device management
1263 *-----------------------
1264 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001265 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
1268 * Performance Management
1269 * ----------------------
1270 */
1271 if (device->flags.performance_manageable) {
1272 result = acpi_bus_get_perf_flags(device);
1273 if (result)
1274 goto end;
1275 }
1276
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001277 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001278 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001280 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001283 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
1285 if (device->flags.bus_address) {
1286 if (device->parent && device->parent->ops.bind)
1287 device->parent->ops.bind(device);
1288 }
1289
Alex Chiang0c526d92009-05-14 08:31:37 -06001290end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001291 if (!result) {
1292 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1293 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1294 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1295 (char *) buffer.pointer,
1296 device->parent ? dev_name(&device->parent->dev) :
1297 "(null)"));
1298 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001300 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001301 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Patrick Mocheld550d982006-06-27 00:41:40 -04001303 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001306#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1307 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1308
Rafael J. Wysockibf325f952010-11-25 00:10:44 +01001309static void acpi_bus_add_power_resource(acpi_handle handle)
1310{
1311 struct acpi_bus_ops ops = {
1312 .acpi_op_add = 1,
1313 .acpi_op_start = 1,
1314 };
1315 struct acpi_device *device = NULL;
1316
1317 acpi_bus_get_device(handle, &device);
1318 if (!device)
1319 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1320 ACPI_STA_DEFAULT, &ops);
1321}
1322
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001323static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1324 unsigned long long *sta)
1325{
1326 acpi_status status;
1327 acpi_object_type acpi_type;
1328
1329 status = acpi_get_type(handle, &acpi_type);
1330 if (ACPI_FAILURE(status))
1331 return -ENODEV;
1332
1333 switch (acpi_type) {
1334 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1335 case ACPI_TYPE_DEVICE:
1336 *type = ACPI_BUS_TYPE_DEVICE;
1337 status = acpi_bus_get_status_handle(handle, sta);
1338 if (ACPI_FAILURE(status))
1339 return -ENODEV;
1340 break;
1341 case ACPI_TYPE_PROCESSOR:
1342 *type = ACPI_BUS_TYPE_PROCESSOR;
1343 status = acpi_bus_get_status_handle(handle, sta);
1344 if (ACPI_FAILURE(status))
1345 return -ENODEV;
1346 break;
1347 case ACPI_TYPE_THERMAL:
1348 *type = ACPI_BUS_TYPE_THERMAL;
1349 *sta = ACPI_STA_DEFAULT;
1350 break;
1351 case ACPI_TYPE_POWER:
1352 *type = ACPI_BUS_TYPE_POWER;
1353 *sta = ACPI_STA_DEFAULT;
1354 break;
1355 default:
1356 return -ENODEV;
1357 }
1358
1359 return 0;
1360}
1361
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001362static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1363 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001365 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001366 int type;
1367 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001368 struct acpi_device *device;
1369 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001370 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001371
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001372 result = acpi_bus_type_and_status(handle, &type, &sta);
1373 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001374 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001376 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001377 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001378 struct acpi_device_wakeup wakeup;
1379 acpi_handle temp;
1380
1381 status = acpi_get_handle(handle, "_PRW", &temp);
1382 if (ACPI_SUCCESS(status))
1383 acpi_bus_extract_wakeup_device_power_package(handle,
1384 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001385 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001388 /*
1389 * We may already have an acpi_device from a previous enumeration. If
1390 * so, we needn't add it again, but we may still have to start it.
1391 */
1392 device = NULL;
1393 acpi_bus_get_device(handle, &device);
1394 if (ops->acpi_op_add && !device)
1395 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001396
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001397 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001398 return AE_CTRL_DEPTH;
1399
1400 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1401 status = acpi_start_single_object(device);
1402 if (ACPI_FAILURE(status))
1403 return AE_CTRL_DEPTH;
1404 }
1405
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001406 if (!*return_value)
1407 *return_value = device;
1408 return AE_OK;
1409}
1410
1411static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1412 struct acpi_device **child)
1413{
1414 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001415 void *device = NULL;
1416
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001417 status = acpi_bus_check_add(handle, 0, ops, &device);
1418 if (ACPI_SUCCESS(status))
1419 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001420 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001421
1422 if (child)
1423 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001424
1425 if (device)
1426 return 0;
1427 else
1428 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Thomas Renninger77796882010-01-29 17:48:52 +01001431/*
1432 * acpi_bus_add and acpi_bus_start
1433 *
1434 * scan a given ACPI tree and (probably recently hot-plugged)
1435 * create and add or starts found devices.
1436 *
1437 * If no devices were found -ENODEV is returned which does not
1438 * mean that this is a real error, there just have been no suitable
1439 * ACPI objects in the table trunk from which the kernel could create
1440 * a device and add/start an appropriate driver.
1441 */
1442
Rajesh Shah3fb02732005-04-28 00:25:52 -07001443int
Len Brown4be44fc2005-08-05 00:44:28 -04001444acpi_bus_add(struct acpi_device **child,
1445 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001446{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001447 struct acpi_bus_ops ops;
1448
Li Shaohuac4168bf2006-12-07 20:56:41 +08001449 memset(&ops, 0, sizeof(ops));
1450 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001451
Thomas Renninger77796882010-01-29 17:48:52 +01001452 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001453}
1454EXPORT_SYMBOL(acpi_bus_add);
1455
Len Brown4be44fc2005-08-05 00:44:28 -04001456int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001457{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001458 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001459 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001460
Thomas Renningerd2f66502010-01-29 17:48:51 +01001461 if (!device)
1462 return -EINVAL;
1463
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001464 memset(&ops, 0, sizeof(ops));
1465 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001466
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001467 result = acpi_bus_scan(device->handle, &ops, NULL);
1468
Lin Ming3a378982010-12-13 13:36:15 +08001469 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001470
1471 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001472}
1473EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Kristen Accardiceaba662006-02-23 17:56:01 -08001475int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Len Brown4be44fc2005-08-05 00:44:28 -04001477 acpi_status status;
1478 struct acpi_device *parent, *child;
1479 acpi_handle phandle, chandle;
1480 acpi_object_type type;
1481 u32 level = 1;
1482 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Len Brown4be44fc2005-08-05 00:44:28 -04001484 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 phandle = start->handle;
1486 child = chandle = NULL;
1487
1488 while ((level > 0) && parent && (!err)) {
1489 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001490 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /*
1493 * If this scope is exhausted then move our way back up.
1494 */
1495 if (ACPI_FAILURE(status)) {
1496 level--;
1497 chandle = phandle;
1498 acpi_get_parent(phandle, &phandle);
1499 child = parent;
1500 parent = parent->parent;
1501
1502 if (level == 0)
1503 err = acpi_bus_remove(child, rmdevice);
1504 else
1505 err = acpi_bus_remove(child, 1);
1506
1507 continue;
1508 }
1509
1510 status = acpi_get_type(chandle, &type);
1511 if (ACPI_FAILURE(status)) {
1512 continue;
1513 }
1514 /*
1515 * If there is a device corresponding to chandle then
1516 * parse it (depth-first).
1517 */
1518 if (acpi_bus_get_device(chandle, &child) == 0) {
1519 level++;
1520 phandle = chandle;
1521 chandle = NULL;
1522 parent = child;
1523 }
1524 continue;
1525 }
1526 return err;
1527}
Kristen Accardiceaba662006-02-23 17:56:01 -08001528EXPORT_SYMBOL_GPL(acpi_bus_trim);
1529
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001530static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Len Brown4be44fc2005-08-05 00:44:28 -04001532 int result = 0;
1533 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001534 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Li Shaohuac4168bf2006-12-07 20:56:41 +08001536 memset(&ops, 0, sizeof(ops));
1537 ops.acpi_op_add = 1;
1538 ops.acpi_op_start = 1;
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /*
1541 * Enumerate all fixed-feature devices.
1542 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001543 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001544 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001545 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001546 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001547 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001548 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001551 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001552 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001553 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001554 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001555 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Patrick Mocheld550d982006-06-27 00:41:40 -04001558 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Bjorn Helgaase747f272009-03-24 16:49:43 -06001561int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
1563 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001564 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Li Shaohuac4168bf2006-12-07 20:56:41 +08001566 memset(&ops, 0, sizeof(ops));
1567 ops.acpi_op_add = 1;
1568 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Patrick Mochel5b327262006-05-10 10:33:00 -04001570 result = bus_register(&acpi_bus_type);
1571 if (result) {
1572 /* We don't want to quit even if we failed to add suspend/resume */
1573 printk(KERN_ERR PREFIX "Could not register bus type\n");
1574 }
1575
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001576 acpi_power_init();
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 * Enumerate devices in the ACPI namespace.
1580 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001581 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001582
Li Shaohuac4168bf2006-12-07 20:56:41 +08001583 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001584 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 if (result)
1587 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001588 else
Lin Ming3a378982010-12-13 13:36:15 +08001589 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Patrick Mocheld550d982006-06-27 00:41:40 -04001591 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}