blob: 7e031b90c09cc156826ab7c9867ac1569ff00c1a [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>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04007#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -04009#include <linux/signal.h>
10#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060014#include "internal.h"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050017ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040019extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020022#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define ACPI_BUS_DEVICE_NAME "System Bus"
24
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000025#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080028static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080029DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030LIST_HEAD(acpi_wakeup_device_list);
31
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080032struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080033 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080034 unsigned int instance_no;
35 struct list_head node;
36};
Thomas Renninger29b71a12007-07-23 14:43:51 +020037
38/*
39 * Creates hid/cid(s) string needed for modalias and uevent
40 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41 * char *modalias: "acpi:IBM0001:ACPI0001"
42*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020043static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44 int size)
45{
Thomas Renninger29b71a12007-07-23 14:43:51 +020046 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080047 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060048 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020049
Zhang Rui5c9fcb52008-03-20 16:40:32 +080050 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020051 size -= len;
52
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060053 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
54 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080055 if (count < 0 || count >= size)
56 return -EINVAL;
57 len += count;
58 size -= count;
59 }
60
Thomas Renninger29b71a12007-07-23 14:43:51 +020061 modalias[len] = '\0';
62 return len;
63}
64
65static ssize_t
66acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
67 struct acpi_device *acpi_dev = to_acpi_device(dev);
68 int len;
69
70 /* Device has no HID and no CID or string is >1024 */
71 len = create_modalias(acpi_dev, buf, 1024);
72 if (len <= 0)
73 return 0;
74 buf[len++] = '\n';
75 return len;
76}
77static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
78
Zhang Ruic8d72a52009-06-22 11:31:16 +080079static void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Zhang Rui26d46862008-04-29 02:35:48 -040081 struct acpi_device *device;
82 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct acpi_object_list arg_list;
84 union acpi_object arg;
85 acpi_status status = AE_OK;
86
Zhang Rui26d46862008-04-29 02:35:48 -040087 if (acpi_bus_get_device(handle, &device))
Zhang Ruic8d72a52009-06-22 11:31:16 +080088 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Zhang Rui26d46862008-04-29 02:35:48 -040090 if (!device)
Zhang Ruic8d72a52009-06-22 11:31:16 +080091 return;
Zhang Rui26d46862008-04-29 02:35:48 -040092
93 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +010094 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -040095
96 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +080097 printk(KERN_ERR PREFIX
98 "Removing device failed\n");
Zhang Ruic8d72a52009-06-22 11:31:16 +080099 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400100 }
101
102 /* power off device */
103 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
104 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800105 printk(KERN_WARNING PREFIX
106 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400107
108 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 arg_list.count = 1;
110 arg_list.pointer = &arg;
111 arg.type = ACPI_TYPE_INTEGER;
112 arg.integer.value = 0;
113 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
114 }
115
116 arg_list.count = 1;
117 arg_list.pointer = &arg;
118 arg.type = ACPI_TYPE_INTEGER;
119 arg.integer.value = 1;
120
121 /*
122 * TBD: _EJD support.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400125 if (ACPI_FAILURE(status))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800126 printk(KERN_WARNING PREFIX
127 "Eject device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Zhang Ruic8d72a52009-06-22 11:31:16 +0800129 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800133acpi_eject_store(struct device *d, struct device_attribute *attr,
134 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Len Brown4be44fc2005-08-05 00:44:28 -0400136 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400137 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800139 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 if ((!count) || (buf[0] != '1')) {
142 return -EINVAL;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800145 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 ret = -ENODEV;
147 goto err;
148 }
149#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800150 status = acpi_get_type(acpi_device->handle, &type);
151 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 ret = -ENODEV;
153 goto err;
154 }
155
Zhang Ruic8d72a52009-06-22 11:31:16 +0800156 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
Alok N Kataria74523c92008-06-13 12:54:24 -0400157err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800161static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800163static ssize_t
164acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
165 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600167 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800168}
169static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
170
171static ssize_t
172acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
173 struct acpi_device *acpi_dev = to_acpi_device(dev);
174 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
175 int result;
176
177 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600178 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800179 goto end;
180
181 result = sprintf(buf, "%s\n", (char*)path.pointer);
182 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600183end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800184 return result;
185}
186static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
187
188static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800190 acpi_status status;
191 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800192 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800194 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800195 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800196 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600197 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800198 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600199 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800200 goto end;
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600203 result = device_create_file(&dev->dev, &dev_attr_hid);
204 if (result)
205 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600207 result = device_create_file(&dev->dev, &dev_attr_modalias);
208 if (result)
209 goto end;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200210
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800211 /*
212 * If device has _EJ0, 'eject' file is created that is used to trigger
213 * hot-removal function from userland.
214 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800215 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
216 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800217 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600218end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800219 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800220}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800222static void acpi_device_remove_files(struct acpi_device *dev)
223{
224 acpi_status status;
225 acpi_handle temp;
226
227 /*
228 * If device has _EJ0, 'eject' file is created that is used to trigger
229 * hot-removal function from userland.
230 */
231 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
232 if (ACPI_SUCCESS(status))
233 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800234
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600235 device_remove_file(&dev->dev, &dev_attr_modalias);
236 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600237 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800238 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800239}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800241 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200243
244int acpi_match_device_ids(struct acpi_device *device,
245 const struct acpi_device_id *ids)
246{
247 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600248 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200249
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800250 /*
251 * If the device is not present, it is unnecessary to load device
252 * driver for it.
253 */
254 if (!device->status.present)
255 return -ENODEV;
256
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600257 for (id = ids; id->id[0]; id++)
258 list_for_each_entry(hwid, &device->pnp.ids, list)
259 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200260 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200261
262 return -ENOENT;
263}
264EXPORT_SYMBOL(acpi_match_device_ids);
265
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600266static void acpi_free_ids(struct acpi_device *device)
267{
268 struct acpi_hardware_id *id, *tmp;
269
270 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
271 kfree(id->id);
272 kfree(id);
273 }
274}
275
Patrick Mochel1890a972006-12-07 20:56:31 +0800276static void acpi_device_release(struct device *dev)
277{
278 struct acpi_device *acpi_dev = to_acpi_device(dev);
279
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600280 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800281 kfree(acpi_dev);
282}
283
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800284static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800285{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800286 struct acpi_device *acpi_dev = to_acpi_device(dev);
287 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800288
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800289 if (acpi_drv && acpi_drv->ops.suspend)
290 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292}
293
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800294static int acpi_device_resume(struct device *dev)
295{
296 struct acpi_device *acpi_dev = to_acpi_device(dev);
297 struct acpi_driver *acpi_drv = acpi_dev->driver;
298
299 if (acpi_drv && acpi_drv->ops.resume)
300 return acpi_drv->ops.resume(acpi_dev);
301 return 0;
302}
303
304static int acpi_bus_match(struct device *dev, struct device_driver *drv)
305{
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
308
Thomas Renninger29b71a12007-07-23 14:43:51 +0200309 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800310}
311
Kay Sievers7eff2e72007-08-14 15:15:12 +0200312static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800313{
314 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200315 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800316
Kay Sievers7eff2e72007-08-14 15:15:12 +0200317 if (add_uevent_var(env, "MODALIAS="))
318 return -ENOMEM;
319 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
320 sizeof(env->buf) - env->buflen);
321 if (len >= (sizeof(env->buf) - env->buflen))
322 return -ENOMEM;
323 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 0;
325}
326
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000327static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
328{
329 struct acpi_device *device = data;
330
331 device->driver->ops.notify(device, event);
332}
333
334static acpi_status acpi_device_notify_fixed(void *data)
335{
336 struct acpi_device *device = data;
337
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000338 /* Fixed hardware devices have no handles */
339 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000340 return AE_OK;
341}
342
343static int acpi_device_install_notify_handler(struct acpi_device *device)
344{
345 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000346
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000347 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000348 status =
349 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
350 acpi_device_notify_fixed,
351 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000352 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000353 status =
354 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
355 acpi_device_notify_fixed,
356 device);
357 else
358 status = acpi_install_notify_handler(device->handle,
359 ACPI_DEVICE_NOTIFY,
360 acpi_device_notify,
361 device);
362
363 if (ACPI_FAILURE(status))
364 return -EINVAL;
365 return 0;
366}
367
368static void acpi_device_remove_notify_handler(struct acpi_device *device)
369{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000370 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000371 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
372 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000373 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000374 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
375 acpi_device_notify_fixed);
376 else
377 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
378 acpi_device_notify);
379}
380
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800381static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
382static int acpi_start_single_object(struct acpi_device *);
383static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800384{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800385 struct acpi_device *acpi_dev = to_acpi_device(dev);
386 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
387 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800389 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
390 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800391 if (acpi_dev->bus_ops.acpi_op_start)
392 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000393
394 if (acpi_drv->ops.notify) {
395 ret = acpi_device_install_notify_handler(acpi_dev);
396 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000397 if (acpi_drv->ops.remove)
398 acpi_drv->ops.remove(acpi_dev,
399 acpi_dev->removal_type);
400 return ret;
401 }
402 }
403
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800404 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
405 "Found driver [%s] for device [%s]\n",
406 acpi_drv->name, acpi_dev->pnp.bus_id));
407 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800408 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800409 return ret;
410}
411
412static int acpi_device_remove(struct device * dev)
413{
414 struct acpi_device *acpi_dev = to_acpi_device(dev);
415 struct acpi_driver *acpi_drv = acpi_dev->driver;
416
417 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000418 if (acpi_drv->ops.notify)
419 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800420 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800421 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800422 }
423 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700424 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800425
426 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800427 return 0;
428}
429
David Brownell55955aa2007-05-08 00:28:35 -0700430struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800431 .name = "acpi",
432 .suspend = acpi_device_suspend,
433 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800434 .match = acpi_bus_match,
435 .probe = acpi_device_probe,
436 .remove = acpi_device_remove,
437 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438};
439
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000440static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800442 int result;
443 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
444 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Linkage
448 * -------
449 * Link this device to its parent and siblings.
450 */
451 INIT_LIST_HEAD(&device->children);
452 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 INIT_LIST_HEAD(&device->wakeup_list);
454
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800455 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
456 if (!new_bus_id) {
457 printk(KERN_ERR PREFIX "Memory allocation error\n");
458 return -ENOMEM;
459 }
460
Shaohua Li90905892009-04-07 10:24:29 +0800461 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800462 /*
463 * Find suitable bus_id and instance number in acpi_bus_id_list
464 * If failed, create one and link it into acpi_bus_id_list
465 */
466 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600467 if (!strcmp(acpi_device_bus_id->bus_id,
468 acpi_device_hid(device))) {
469 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800470 found = 1;
471 kfree(new_bus_id);
472 break;
473 }
474 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600475 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800476 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600477 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800478 acpi_device_bus_id->instance_no = 0;
479 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
480 }
Kay Sievers07944692008-10-30 01:18:59 +0100481 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800482
Len Brown33b57152008-12-15 22:09:26 -0500483 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (device->wakeup.flags.valid)
487 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800488 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Patrick Mochel1890a972006-12-07 20:56:31 +0800490 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000491 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800492 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800493 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600494 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600495 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600496 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800497 goto end;
498 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800499
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800500 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600501 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100502 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
503 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800504
Li Shaohua96333572006-12-07 20:56:46 +0800505 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800506 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600507end:
Shaohua Li90905892009-04-07 10:24:29 +0800508 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500509 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800510 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800511 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800512 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800513 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516static void acpi_device_unregister(struct acpi_device *device, int type)
517{
Shaohua Li90905892009-04-07 10:24:29 +0800518 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500519 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800523 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800526
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800527 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800528 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Zhang Rui9e89dde2006-12-07 20:56:16 +0800531/* --------------------------------------------------------------------------
532 Driver Management
533 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500535 * acpi_bus_driver_init - add a device to a driver
536 * @device: the device to add and initialize
537 * @driver: driver for the device
538 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600539 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800540 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542static int
Len Brown4be44fc2005-08-05 00:44:28 -0400543acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Len Brown4be44fc2005-08-05 00:44:28 -0400545 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400548 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400551 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 result = driver->ops.add(device);
554 if (result) {
555 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700556 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 device->driver = driver;
561
562 /*
563 * TBD - Configuration Management: Assign resources to device based
564 * upon possible configuration and currently allocated resources.
565 */
566
Len Brown4be44fc2005-08-05 00:44:28 -0400567 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
568 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700570}
571
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400572static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700573{
574 int result = 0;
575 struct acpi_driver *driver;
576
Rajesh Shah3fb02732005-04-28 00:25:52 -0700577
578 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (driver->ops.start) {
582 result = driver->ops.start(device);
583 if (result && driver->ops.remove)
584 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586
Patrick Mocheld550d982006-06-27 00:41:40 -0400587 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500591 * acpi_bus_register_driver - register a driver with the ACPI bus
592 * @driver: driver being registered
593 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500595 * devices that match the driver's criteria and binds. Returns zero for
596 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Len Brown4be44fc2005-08-05 00:44:28 -0400598int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Patrick Mochel1890a972006-12-07 20:56:31 +0800600 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400603 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800604 driver->drv.name = driver->name;
605 driver->drv.bus = &acpi_bus_type;
606 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Patrick Mochel1890a972006-12-07 20:56:31 +0800608 ret = driver_register(&driver->drv);
609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Len Brown4be44fc2005-08-05 00:44:28 -0400612EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500615 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
616 * @driver: driver to unregister
617 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 * Unregisters a driver with the ACPI bus. Searches the namespace for all
619 * devices that match the driver's criteria and unbinds.
620 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400621void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Patrick Mochel1890a972006-12-07 20:56:31 +0800623 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
Len Brown4be44fc2005-08-05 00:44:28 -0400625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626EXPORT_SYMBOL(acpi_bus_unregister_driver);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/* --------------------------------------------------------------------------
629 Device Enumeration
630 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000631static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
632{
633 acpi_status status;
634 int ret;
635 struct acpi_device *device;
636
637 /*
638 * Fixed hardware devices do not appear in the namespace and do not
639 * have handles, but we fabricate acpi_devices for them, so we have
640 * to deal with them specially.
641 */
642 if (handle == NULL)
643 return acpi_root;
644
645 do {
646 status = acpi_get_parent(handle, &handle);
647 if (status == AE_NULL_ENTRY)
648 return NULL;
649 if (ACPI_FAILURE(status))
650 return acpi_root;
651
652 ret = acpi_bus_get_device(handle, &device);
653 if (ret == 0)
654 return device;
655 } while (1);
656}
657
Len Brownc8f7a622006-07-09 17:22:28 -0400658acpi_status
659acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
660{
661 acpi_status status;
662 acpi_handle tmp;
663 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
664 union acpi_object *obj;
665
666 status = acpi_get_handle(handle, "_EJD", &tmp);
667 if (ACPI_FAILURE(status))
668 return status;
669
670 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
671 if (ACPI_SUCCESS(status)) {
672 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100673 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
674 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400675 kfree(buffer.pointer);
676 }
677 return status;
678}
679EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
680
Bob Moore8e4319c2009-06-29 13:43:27 +0800681void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683
684 /* TBD */
685
686 return;
687}
688
Zhang Rui9e89dde2006-12-07 20:56:16 +0800689static int acpi_bus_get_perf_flags(struct acpi_device *device)
690{
691 device->performance.state = ACPI_STATE_UNKNOWN;
692 return 0;
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static acpi_status
696acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
697 union acpi_object *package)
698{
699 int i = 0;
700 union acpi_object *element = NULL;
701
702 if (!device || !package || (package->package.count < 2))
703 return AE_BAD_PARAMETER;
704
705 element = &(package->package.elements[0]);
706 if (!element)
707 return AE_BAD_PARAMETER;
708 if (element->type == ACPI_TYPE_PACKAGE) {
709 if ((element->package.count < 2) ||
710 (element->package.elements[0].type !=
711 ACPI_TYPE_LOCAL_REFERENCE)
712 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
713 return AE_BAD_DATA;
714 device->wakeup.gpe_device =
715 element->package.elements[0].reference.handle;
716 device->wakeup.gpe_number =
717 (u32) element->package.elements[1].integer.value;
718 } else if (element->type == ACPI_TYPE_INTEGER) {
719 device->wakeup.gpe_number = element->integer.value;
720 } else
721 return AE_BAD_DATA;
722
723 element = &(package->package.elements[1]);
724 if (element->type != ACPI_TYPE_INTEGER) {
725 return AE_BAD_DATA;
726 }
727 device->wakeup.sleep_state = element->integer.value;
728
729 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
730 return AE_NO_MEMORY;
731 }
732 device->wakeup.resources.count = package->package.count - 2;
733 for (i = 0; i < device->wakeup.resources.count; i++) {
734 element = &(package->package.elements[i + 2]);
Bob Moorecd0b2242008-04-10 19:06:43 +0400735 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 device->wakeup.resources.handles[i] = element->reference.handle;
739 }
740
741 return AE_OK;
742}
743
744static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
745{
746 acpi_status status = 0;
747 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
748 union acpi_object *package = NULL;
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200749 int psw_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Thomas Renninger29b71a12007-07-23 14:43:51 +0200751 struct acpi_device_id button_device_ids[] = {
752 {"PNP0C0D", 0},
753 {"PNP0C0C", 0},
754 {"PNP0C0E", 0},
755 {"", 0},
756 };
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 /* _PRW */
759 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
760 if (ACPI_FAILURE(status)) {
761 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
762 goto end;
763 }
764
765 package = (union acpi_object *)buffer.pointer;
766 status = acpi_bus_extract_wakeup_device_power_package(device, package);
767 if (ACPI_FAILURE(status)) {
768 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
769 goto end;
770 }
771
772 kfree(buffer.pointer);
773
774 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200775 device->wakeup.prepare_count = 0;
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800776 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
777 * system for the ACPI device with the _PRW object.
778 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
779 * So it is necessary to call _DSW object first. Only when it is not
780 * present will the _PSW object used.
781 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200782 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
783 if (psw_error)
784 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
785 "error in _DSW or _PSW evaluation\n"));
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* Power button, Lid switch always enable wakeup */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200788 if (!acpi_match_device_ids(device, button_device_ids))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 device->wakeup.flags.run_wake = 1;
790
Alex Chiang0c526d92009-05-14 08:31:37 -0600791end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (ACPI_FAILURE(status))
793 device->flags.wake_capable = 0;
794 return 0;
795}
796
Zhang Rui9e89dde2006-12-07 20:56:16 +0800797static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800799 acpi_status status = 0;
800 acpi_handle handle = NULL;
801 u32 i = 0;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800805 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800807 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800809 device->power.flags.explicit_get = 1;
810 status = acpi_get_handle(device->handle, "_IRC", &handle);
811 if (ACPI_SUCCESS(status))
812 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800815 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800817 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
818 struct acpi_device_power_state *ps = &device->power.states[i];
819 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Zhang Rui9e89dde2006-12-07 20:56:16 +0800821 /* Evaluate "_PRx" to se if power resources are referenced */
822 acpi_evaluate_reference(device->handle, object_name, NULL,
823 &ps->resources);
824 if (ps->resources.count) {
825 device->power.flags.power_resources = 1;
826 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Zhang Rui9e89dde2006-12-07 20:56:16 +0800829 /* Evaluate "_PSx" to see if we can do explicit sets */
830 object_name[2] = 'S';
831 status = acpi_get_handle(device->handle, object_name, &handle);
832 if (ACPI_SUCCESS(status)) {
833 ps->flags.explicit_set = 1;
834 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800836
837 /* State is valid if we have some power control */
838 if (ps->resources.count || ps->flags.explicit_set)
839 ps->flags.valid = 1;
840
841 ps->power = -1; /* Unknown - driver assigned */
842 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Zhang Rui9e89dde2006-12-07 20:56:16 +0800845 /* Set defaults for D0 and D3 states (always valid) */
846 device->power.states[ACPI_STATE_D0].flags.valid = 1;
847 device->power.states[ACPI_STATE_D0].power = 100;
848 device->power.states[ACPI_STATE_D3].flags.valid = 1;
849 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Zhang Rui9e89dde2006-12-07 20:56:16 +0800851 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Zhang Rui9e89dde2006-12-07 20:56:16 +0800853 device->power.state = ACPI_STATE_UNKNOWN;
Zhao Yakuia51e1452008-08-11 14:55:05 +0800854 acpi_bus_get_power(device->handle, &(device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 return 0;
857}
858
Len Brown4be44fc2005-08-05 00:44:28 -0400859static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Len Brown4be44fc2005-08-05 00:44:28 -0400861 acpi_status status = AE_OK;
862 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* Presence of _STA indicates 'dynamic_status' */
866 status = acpi_get_handle(device->handle, "_STA", &temp);
867 if (ACPI_SUCCESS(status))
868 device->flags.dynamic_status = 1;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /* Presence of _RMV indicates 'removable' */
871 status = acpi_get_handle(device->handle, "_RMV", &temp);
872 if (ACPI_SUCCESS(status))
873 device->flags.removable = 1;
874
875 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
876 status = acpi_get_handle(device->handle, "_EJD", &temp);
877 if (ACPI_SUCCESS(status))
878 device->flags.ejectable = 1;
879 else {
880 status = acpi_get_handle(device->handle, "_EJ0", &temp);
881 if (ACPI_SUCCESS(status))
882 device->flags.ejectable = 1;
883 }
884
885 /* Presence of _LCK indicates 'lockable' */
886 status = acpi_get_handle(device->handle, "_LCK", &temp);
887 if (ACPI_SUCCESS(status))
888 device->flags.lockable = 1;
889
890 /* Presence of _PS0|_PR0 indicates 'power manageable' */
891 status = acpi_get_handle(device->handle, "_PS0", &temp);
892 if (ACPI_FAILURE(status))
893 status = acpi_get_handle(device->handle, "_PR0", &temp);
894 if (ACPI_SUCCESS(status))
895 device->flags.power_manageable = 1;
896
897 /* Presence of _PRW indicates wake capable */
898 status = acpi_get_handle(device->handle, "_PRW", &temp);
899 if (ACPI_SUCCESS(status))
900 device->flags.wake_capable = 1;
901
Joe Perches3c5f9be42008-02-03 17:06:17 +0200902 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Patrick Mocheld550d982006-06-27 00:41:40 -0400904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +0000907static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Len Brown4be44fc2005-08-05 00:44:28 -0400909 char bus_id[5] = { '?', 0 };
910 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
911 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /*
914 * Bus ID
915 * ------
916 * The device's Bus ID is simply the object name.
917 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
918 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +0000919 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +0000921 return;
922 }
923
924 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 case ACPI_BUS_TYPE_POWER_BUTTON:
926 strcpy(device->pnp.bus_id, "PWRF");
927 break;
928 case ACPI_BUS_TYPE_SLEEP_BUTTON:
929 strcpy(device->pnp.bus_id, "SLPF");
930 break;
931 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000932 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* Clean up trailing underscores (if any) */
934 for (i = 3; i > 1; i--) {
935 if (bus_id[i] == '_')
936 bus_id[i] = '\0';
937 else
938 break;
939 }
940 strcpy(device->pnp.bus_id, bus_id);
941 break;
942 }
943}
944
Zhang Rui54735262007-01-11 02:09:09 -0500945/*
946 * acpi_bay_match - see if a device is an ejectable driver bay
947 *
948 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
949 * then we can safely call it an ejectable drive bay
950 */
951static int acpi_bay_match(struct acpi_device *device){
952 acpi_status status;
953 acpi_handle handle;
954 acpi_handle tmp;
955 acpi_handle phandle;
956
957 handle = device->handle;
958
959 status = acpi_get_handle(handle, "_EJ0", &tmp);
960 if (ACPI_FAILURE(status))
961 return -ENODEV;
962
963 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
964 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
965 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
966 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
967 return 0;
968
969 if (acpi_get_parent(handle, &phandle))
970 return -ENODEV;
971
972 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
973 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
974 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
975 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
976 return 0;
977
978 return -ENODEV;
979}
980
Frank Seidel3620f2f2007-12-07 13:20:34 +0100981/*
982 * acpi_dock_match - see if a device has a _DCK method
983 */
984static int acpi_dock_match(struct acpi_device *device)
985{
986 acpi_handle tmp;
987 return acpi_get_handle(device->handle, "_DCK", &tmp);
988}
989
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600990char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +0800991{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600992 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +0800993
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600994 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
995 return hid->id;
996}
997EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +0800998
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600999static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1000{
1001 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001002
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001003 id = kmalloc(sizeof(*id), GFP_KERNEL);
1004 if (!id)
1005 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001006
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001007 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1008 if (!id->id) {
1009 kfree(id);
1010 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001011 }
1012
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001013 strcpy(id->id, dev_id);
1014 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001015}
1016
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001017static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Bob Moore15b8dd52009-06-29 13:39:29 +08001019 struct acpi_device_info *info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001020 char *hid = NULL;
1021 char *uid = NULL;
Bob Moore15b8dd52009-06-29 13:39:29 +08001022 struct acpica_device_id_list *cid_list = NULL;
1023 char *cid_add = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001024 acpi_status status;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001025 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001027 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001029 if (ACPI_IS_ROOT_DEVICE(device)) {
1030 hid = ACPI_SYSTEM_HID;
1031 break;
Bjorn Helgaas78b8e142009-09-21 13:35:04 -06001032 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1033 /* \_SB_, the only root-level namespace device */
1034 hid = ACPI_BUS_HID;
1035 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1036 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1037 break;
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001038 }
1039
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001040 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001042 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return;
1044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (info->valid & ACPI_VALID_HID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001047 hid = info->hardware_id.string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (info->valid & ACPI_VALID_UID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001049 uid = info->unique_id.string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (info->valid & ACPI_VALID_CID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001051 cid_list = &info->compatible_id_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (info->valid & ACPI_VALID_ADR) {
1053 device->pnp.bus_address = info->address;
1054 device->flags.bus_address = 1;
1055 }
Zhang Ruiae843332006-12-07 20:57:10 +08001056
Frank Seidel3620f2f2007-12-07 13:20:34 +01001057 /* If we have a video/bay/dock device, add our selfdefined
1058 HID to the CID list. Like that the video/bay/dock drivers
1059 will get autoloaded and the device might still match
1060 against another driver.
1061 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001062 if (acpi_is_video_device(device))
Frank Seidel3620f2f2007-12-07 13:20:34 +01001063 cid_add = ACPI_VIDEO_HID;
1064 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1065 cid_add = ACPI_BAY_HID;
1066 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1067 cid_add = ACPI_DOCK_HID;
Zhang Rui54735262007-01-11 02:09:09 -05001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 break;
1070 case ACPI_BUS_TYPE_POWER:
1071 hid = ACPI_POWER_HID;
1072 break;
1073 case ACPI_BUS_TYPE_PROCESSOR:
Myron Stowead93a762008-11-04 14:52:55 -07001074 hid = ACPI_PROCESSOR_OBJECT_HID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 case ACPI_BUS_TYPE_THERMAL:
1077 hid = ACPI_THERMAL_HID;
1078 break;
1079 case ACPI_BUS_TYPE_POWER_BUTTON:
1080 hid = ACPI_BUTTON_HID_POWERF;
1081 break;
1082 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1083 hid = ACPI_BUTTON_HID_SLEEPF;
1084 break;
1085 }
1086
Bjorn Helgaasb1fbfb22009-09-21 13:35:14 -06001087 /*
1088 * We build acpi_devices for some objects that don't have _HID or _CID,
1089 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1090 * but we do use them indirectly by traversing the acpi_device tree.
1091 * This generic ID isn't useful for driver binding, but it provides
1092 * the useful property that "every acpi_device has an ID."
1093 */
1094 if (!hid && !cid_list && !cid_add)
1095 hid = "device";
1096
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001097 if (hid)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001098 acpi_add_id(device, hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001099 if (uid) {
1100 device->pnp.unique_id = ACPI_ALLOCATE_ZEROED(strlen (uid) + 1);
1101 if (device->pnp.unique_id) {
1102 strcpy(device->pnp.unique_id, uid);
1103 device->flags.unique_id = 1;
1104 }
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001105 }
1106 if (!device->flags.unique_id)
1107 device->pnp.unique_id = "";
Bob Moore15b8dd52009-06-29 13:39:29 +08001108
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001109 if (cid_list)
1110 for (i = 0; i < cid_list->count; i++)
1111 acpi_add_id(device, cid_list->ids[i].string);
Bjorn Helgaasb2972f82009-09-21 13:35:24 -06001112 if (cid_add)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001113 acpi_add_id(device, cid_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Bob Moore15b8dd52009-06-29 13:39:29 +08001115 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001118static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001120 acpi_status status;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /*
1123 * Context
1124 * -------
1125 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001126 * resolutions from handle->device very efficient. Fixed hardware
1127 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001129 if (!device->handle)
1130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001132 status = acpi_attach_data(device->handle,
1133 acpi_bus_data_handler, device);
1134 if (ACPI_SUCCESS(status))
1135 return 0;
1136
1137 printk(KERN_ERR PREFIX "Error attaching device data\n");
1138 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
Len Brown4be44fc2005-08-05 00:44:28 -04001141static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001144 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Li Shaohua96333572006-12-07 20:56:46 +08001146 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001147 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Rui Zhang2786f6e2006-12-21 02:21:13 -05001152 /*
1153 * unbind _ADR-Based Devices when hot removal
1154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (dev->flags.bus_address) {
1156 if ((dev->parent) && (dev->parent->ops.unbind))
1157 dev->parent->ops.unbind(dev);
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1160
Patrick Mocheld550d982006-06-27 00:41:40 -04001161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001164static int acpi_add_single_object(struct acpi_device **child,
1165 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001166 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001167 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001169 int result;
1170 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001171 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Burman Yan36bcbec2006-12-19 12:56:11 -08001173 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001175 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001176 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001179 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001180 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001182 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001183 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001184 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001185
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001186 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /*
1189 * Flags
1190 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001191 * Note that we only look for object handles -- cannot evaluate objects
1192 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
1194 result = acpi_bus_get_flags(device);
1195 if (result)
1196 goto end;
1197
1198 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 * Initialize Device
1200 * -----------------
1201 * TBD: Synch with Core's enumeration/initialization process.
1202 */
1203
1204 /*
1205 * Hardware ID, Unique ID, & Bus Address
1206 * -------------------------------------
1207 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001208 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /*
1211 * Power Management
1212 * ----------------
1213 */
1214 if (device->flags.power_manageable) {
1215 result = acpi_bus_get_power_flags(device);
1216 if (result)
1217 goto end;
1218 }
1219
Len Brown4be44fc2005-08-05 00:44:28 -04001220 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * Wakeup device management
1222 *-----------------------
1223 */
1224 if (device->flags.wake_capable) {
1225 result = acpi_bus_get_wakeup_device_flags(device);
1226 if (result)
1227 goto end;
1228 }
1229
1230 /*
1231 * Performance Management
1232 * ----------------------
1233 */
1234 if (device->flags.performance_manageable) {
1235 result = acpi_bus_get_perf_flags(device);
1236 if (result)
1237 goto end;
1238 }
1239
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001240 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001241 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001243 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001246 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
1248 if (device->flags.bus_address) {
1249 if (device->parent && device->parent->ops.bind)
1250 device->parent->ops.bind(device);
1251 }
1252
Alex Chiang0c526d92009-05-14 08:31:37 -06001253end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001254 if (!result) {
1255 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1256 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1257 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1258 (char *) buffer.pointer,
1259 device->parent ? dev_name(&device->parent->dev) :
1260 "(null)"));
1261 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001263 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001264 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Patrick Mocheld550d982006-06-27 00:41:40 -04001266 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001269#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1270 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1271
1272static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1273 unsigned long long *sta)
1274{
1275 acpi_status status;
1276 acpi_object_type acpi_type;
1277
1278 status = acpi_get_type(handle, &acpi_type);
1279 if (ACPI_FAILURE(status))
1280 return -ENODEV;
1281
1282 switch (acpi_type) {
1283 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1284 case ACPI_TYPE_DEVICE:
1285 *type = ACPI_BUS_TYPE_DEVICE;
1286 status = acpi_bus_get_status_handle(handle, sta);
1287 if (ACPI_FAILURE(status))
1288 return -ENODEV;
1289 break;
1290 case ACPI_TYPE_PROCESSOR:
1291 *type = ACPI_BUS_TYPE_PROCESSOR;
1292 status = acpi_bus_get_status_handle(handle, sta);
1293 if (ACPI_FAILURE(status))
1294 return -ENODEV;
1295 break;
1296 case ACPI_TYPE_THERMAL:
1297 *type = ACPI_BUS_TYPE_THERMAL;
1298 *sta = ACPI_STA_DEFAULT;
1299 break;
1300 case ACPI_TYPE_POWER:
1301 *type = ACPI_BUS_TYPE_POWER;
1302 *sta = ACPI_STA_DEFAULT;
1303 break;
1304 default:
1305 return -ENODEV;
1306 }
1307
1308 return 0;
1309}
1310
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001311static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1312 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001314 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001315 int type;
1316 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001317 struct acpi_device *device;
1318 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001319 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001320
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001321 result = acpi_bus_type_and_status(handle, &type, &sta);
1322 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001323 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001325 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1326 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1327 return AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001329 /*
1330 * We may already have an acpi_device from a previous enumeration. If
1331 * so, we needn't add it again, but we may still have to start it.
1332 */
1333 device = NULL;
1334 acpi_bus_get_device(handle, &device);
1335 if (ops->acpi_op_add && !device)
1336 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001337
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001338 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001339 return AE_CTRL_DEPTH;
1340
1341 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1342 status = acpi_start_single_object(device);
1343 if (ACPI_FAILURE(status))
1344 return AE_CTRL_DEPTH;
1345 }
1346
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001347 if (!*return_value)
1348 *return_value = device;
1349 return AE_OK;
1350}
1351
1352static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1353 struct acpi_device **child)
1354{
1355 acpi_status status;
1356 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1357 void *device = NULL;
1358
1359 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1360 printk(KERN_INFO PREFIX "Enumerating devices from [%s]\n",
1361 (char *) buffer.pointer);
1362
1363 status = acpi_bus_check_add(handle, 0, ops, &device);
1364 if (ACPI_SUCCESS(status))
1365 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1366 acpi_bus_check_add, ops, &device);
1367
1368 if (child)
1369 *child = device;
Patrick Mocheld550d982006-06-27 00:41:40 -04001370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Rajesh Shah3fb02732005-04-28 00:25:52 -07001373int
Len Brown4be44fc2005-08-05 00:44:28 -04001374acpi_bus_add(struct acpi_device **child,
1375 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001376{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001377 struct acpi_bus_ops ops;
1378
Li Shaohuac4168bf2006-12-07 20:56:41 +08001379 memset(&ops, 0, sizeof(ops));
1380 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001381
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001382 acpi_bus_scan(handle, &ops, child);
1383 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001384}
1385EXPORT_SYMBOL(acpi_bus_add);
1386
Len Brown4be44fc2005-08-05 00:44:28 -04001387int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001388{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001389 struct acpi_bus_ops ops;
1390
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001391 memset(&ops, 0, sizeof(ops));
1392 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001393
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001394 acpi_bus_scan(device->handle, &ops, NULL);
1395 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001396}
1397EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Kristen Accardiceaba662006-02-23 17:56:01 -08001399int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Len Brown4be44fc2005-08-05 00:44:28 -04001401 acpi_status status;
1402 struct acpi_device *parent, *child;
1403 acpi_handle phandle, chandle;
1404 acpi_object_type type;
1405 u32 level = 1;
1406 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Len Brown4be44fc2005-08-05 00:44:28 -04001408 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 phandle = start->handle;
1410 child = chandle = NULL;
1411
1412 while ((level > 0) && parent && (!err)) {
1413 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001414 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /*
1417 * If this scope is exhausted then move our way back up.
1418 */
1419 if (ACPI_FAILURE(status)) {
1420 level--;
1421 chandle = phandle;
1422 acpi_get_parent(phandle, &phandle);
1423 child = parent;
1424 parent = parent->parent;
1425
1426 if (level == 0)
1427 err = acpi_bus_remove(child, rmdevice);
1428 else
1429 err = acpi_bus_remove(child, 1);
1430
1431 continue;
1432 }
1433
1434 status = acpi_get_type(chandle, &type);
1435 if (ACPI_FAILURE(status)) {
1436 continue;
1437 }
1438 /*
1439 * If there is a device corresponding to chandle then
1440 * parse it (depth-first).
1441 */
1442 if (acpi_bus_get_device(chandle, &child) == 0) {
1443 level++;
1444 phandle = chandle;
1445 chandle = NULL;
1446 parent = child;
1447 }
1448 continue;
1449 }
1450 return err;
1451}
Kristen Accardiceaba662006-02-23 17:56:01 -08001452EXPORT_SYMBOL_GPL(acpi_bus_trim);
1453
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001454static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Len Brown4be44fc2005-08-05 00:44:28 -04001456 int result = 0;
1457 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001458 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Li Shaohuac4168bf2006-12-07 20:56:41 +08001460 memset(&ops, 0, sizeof(ops));
1461 ops.acpi_op_add = 1;
1462 ops.acpi_op_start = 1;
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /*
1465 * Enumerate all fixed-feature devices.
1466 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001467 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001468 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001469 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001470 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001471 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001474 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001475 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001476 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001477 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001478 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Patrick Mocheld550d982006-06-27 00:41:40 -04001481 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Bjorn Helgaase747f272009-03-24 16:49:43 -06001484int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001487 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Li Shaohuac4168bf2006-12-07 20:56:41 +08001489 memset(&ops, 0, sizeof(ops));
1490 ops.acpi_op_add = 1;
1491 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Patrick Mochel5b327262006-05-10 10:33:00 -04001493 result = bus_register(&acpi_bus_type);
1494 if (result) {
1495 /* We don't want to quit even if we failed to add suspend/resume */
1496 printk(KERN_ERR PREFIX "Could not register bus type\n");
1497 }
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 * Enumerate devices in the ACPI namespace.
1501 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001502 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001503
Li Shaohuac4168bf2006-12-07 20:56:41 +08001504 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001505 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (result)
1508 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1509
Patrick Mocheld550d982006-06-27 00:41:40 -04001510 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}