blob: 449ada016d81278861d24bddfe070c6bc5bf261e [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 Helgaas859ac9a42009-09-21 19:29:50 +000027#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080030static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080031DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032LIST_HEAD(acpi_wakeup_device_list);
33
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080034struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080035 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080036 unsigned int instance_no;
37 struct list_head node;
38};
Thomas Renninger29b71a12007-07-23 14:43:51 +020039
40/*
41 * Creates hid/cid(s) string needed for modalias and uevent
42 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
43 * char *modalias: "acpi:IBM0001:ACPI0001"
44*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020045static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
46 int size)
47{
Thomas Renninger29b71a12007-07-23 14:43:51 +020048 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080049 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060050 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020051
Zhang Rui5c9fcb52008-03-20 16:40:32 +080052 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020053 size -= len;
54
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060055 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
56 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080057 if (count < 0 || count >= size)
58 return -EINVAL;
59 len += count;
60 size -= count;
61 }
62
Thomas Renninger29b71a12007-07-23 14:43:51 +020063 modalias[len] = '\0';
64 return len;
65}
66
67static ssize_t
68acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
69 struct acpi_device *acpi_dev = to_acpi_device(dev);
70 int len;
71
72 /* Device has no HID and no CID or string is >1024 */
73 len = create_modalias(acpi_dev, buf, 1024);
74 if (len <= 0)
75 return 0;
76 buf[len++] = '\n';
77 return len;
78}
79static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
80
Zhang Ruic8d72a52009-06-22 11:31:16 +080081static void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Zhang Rui26d46862008-04-29 02:35:48 -040083 struct acpi_device *device;
84 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 struct acpi_object_list arg_list;
86 union acpi_object arg;
87 acpi_status status = AE_OK;
88
Zhang Rui26d46862008-04-29 02:35:48 -040089 if (acpi_bus_get_device(handle, &device))
Zhang Ruic8d72a52009-06-22 11:31:16 +080090 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Zhang Rui26d46862008-04-29 02:35:48 -040092 if (!device)
Zhang Ruic8d72a52009-06-22 11:31:16 +080093 return;
Zhang Rui26d46862008-04-29 02:35:48 -040094
95 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +010096 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -040097
98 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +080099 printk(KERN_ERR PREFIX
100 "Removing device failed\n");
Zhang Ruic8d72a52009-06-22 11:31:16 +0800101 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400102 }
103
104 /* power off device */
105 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
106 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800107 printk(KERN_WARNING PREFIX
108 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400109
110 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 arg_list.count = 1;
112 arg_list.pointer = &arg;
113 arg.type = ACPI_TYPE_INTEGER;
114 arg.integer.value = 0;
115 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
116 }
117
118 arg_list.count = 1;
119 arg_list.pointer = &arg;
120 arg.type = ACPI_TYPE_INTEGER;
121 arg.integer.value = 1;
122
123 /*
124 * TBD: _EJD support.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400127 if (ACPI_FAILURE(status))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800128 printk(KERN_WARNING PREFIX
129 "Eject device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Zhang Ruic8d72a52009-06-22 11:31:16 +0800131 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800135acpi_eject_store(struct device *d, struct device_attribute *attr,
136 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Len Brown4be44fc2005-08-05 00:44:28 -0400138 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400139 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400140 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800141 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 if ((!count) || (buf[0] != '1')) {
144 return -EINVAL;
145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800147 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ret = -ENODEV;
149 goto err;
150 }
151#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800152 status = acpi_get_type(acpi_device->handle, &type);
153 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ret = -ENODEV;
155 goto err;
156 }
157
Zhang Ruic8d72a52009-06-22 11:31:16 +0800158 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
Alok N Kataria74523c92008-06-13 12:54:24 -0400159err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800163static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800165static ssize_t
166acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
167 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600169 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800170}
171static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
172
173static ssize_t
174acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
175 struct acpi_device *acpi_dev = to_acpi_device(dev);
176 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
177 int result;
178
179 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600180 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800181 goto end;
182
183 result = sprintf(buf, "%s\n", (char*)path.pointer);
184 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600185end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800186 return result;
187}
188static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
189
190static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800192 acpi_status status;
193 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800194 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800196 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800197 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800198 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600199 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800200 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600201 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800202 goto end;
203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600205 result = device_create_file(&dev->dev, &dev_attr_hid);
206 if (result)
207 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600209 result = device_create_file(&dev->dev, &dev_attr_modalias);
210 if (result)
211 goto end;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200212
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800213 /*
214 * If device has _EJ0, 'eject' file is created that is used to trigger
215 * hot-removal function from userland.
216 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800217 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
218 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800219 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600220end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800221 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800224static void acpi_device_remove_files(struct acpi_device *dev)
225{
226 acpi_status status;
227 acpi_handle temp;
228
229 /*
230 * If device has _EJ0, 'eject' file is created that is used to trigger
231 * hot-removal function from userland.
232 */
233 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
234 if (ACPI_SUCCESS(status))
235 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800236
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600237 device_remove_file(&dev->dev, &dev_attr_modalias);
238 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600239 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800240 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800243 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200245
246int acpi_match_device_ids(struct acpi_device *device,
247 const struct acpi_device_id *ids)
248{
249 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600250 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200251
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800252 /*
253 * If the device is not present, it is unnecessary to load device
254 * driver for it.
255 */
256 if (!device->status.present)
257 return -ENODEV;
258
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600259 for (id = ids; id->id[0]; id++)
260 list_for_each_entry(hwid, &device->pnp.ids, list)
261 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200262 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200263
264 return -ENOENT;
265}
266EXPORT_SYMBOL(acpi_match_device_ids);
267
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600268static void acpi_free_ids(struct acpi_device *device)
269{
270 struct acpi_hardware_id *id, *tmp;
271
272 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
273 kfree(id->id);
274 kfree(id);
275 }
276}
277
Patrick Mochel1890a972006-12-07 20:56:31 +0800278static void acpi_device_release(struct device *dev)
279{
280 struct acpi_device *acpi_dev = to_acpi_device(dev);
281
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600282 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800283 kfree(acpi_dev);
284}
285
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800286static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800287{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800288 struct acpi_device *acpi_dev = to_acpi_device(dev);
289 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800290
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800291 if (acpi_drv && acpi_drv->ops.suspend)
292 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
295
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800296static int acpi_device_resume(struct device *dev)
297{
298 struct acpi_device *acpi_dev = to_acpi_device(dev);
299 struct acpi_driver *acpi_drv = acpi_dev->driver;
300
301 if (acpi_drv && acpi_drv->ops.resume)
302 return acpi_drv->ops.resume(acpi_dev);
303 return 0;
304}
305
306static int acpi_bus_match(struct device *dev, struct device_driver *drv)
307{
308 struct acpi_device *acpi_dev = to_acpi_device(dev);
309 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
310
Thomas Renninger29b71a12007-07-23 14:43:51 +0200311 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800312}
313
Kay Sievers7eff2e72007-08-14 15:15:12 +0200314static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800315{
316 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200317 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800318
Kay Sievers7eff2e72007-08-14 15:15:12 +0200319 if (add_uevent_var(env, "MODALIAS="))
320 return -ENOMEM;
321 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
322 sizeof(env->buf) - env->buflen);
323 if (len >= (sizeof(env->buf) - env->buflen))
324 return -ENOMEM;
325 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return 0;
327}
328
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000329static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
330{
331 struct acpi_device *device = data;
332
333 device->driver->ops.notify(device, event);
334}
335
336static acpi_status acpi_device_notify_fixed(void *data)
337{
338 struct acpi_device *device = data;
339
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000340 /* Fixed hardware devices have no handles */
341 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000342 return AE_OK;
343}
344
345static int acpi_device_install_notify_handler(struct acpi_device *device)
346{
347 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000348
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000349 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000350 status =
351 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
352 acpi_device_notify_fixed,
353 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000354 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000355 status =
356 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
357 acpi_device_notify_fixed,
358 device);
359 else
360 status = acpi_install_notify_handler(device->handle,
361 ACPI_DEVICE_NOTIFY,
362 acpi_device_notify,
363 device);
364
365 if (ACPI_FAILURE(status))
366 return -EINVAL;
367 return 0;
368}
369
370static void acpi_device_remove_notify_handler(struct acpi_device *device)
371{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000372 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000373 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
374 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000375 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000376 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
377 acpi_device_notify_fixed);
378 else
379 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
380 acpi_device_notify);
381}
382
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800383static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
384static int acpi_start_single_object(struct acpi_device *);
385static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800386{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800387 struct acpi_device *acpi_dev = to_acpi_device(dev);
388 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
389 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800391 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
392 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800393 if (acpi_dev->bus_ops.acpi_op_start)
394 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000395
396 if (acpi_drv->ops.notify) {
397 ret = acpi_device_install_notify_handler(acpi_dev);
398 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000399 if (acpi_drv->ops.remove)
400 acpi_drv->ops.remove(acpi_dev,
401 acpi_dev->removal_type);
402 return ret;
403 }
404 }
405
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800406 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
407 "Found driver [%s] for device [%s]\n",
408 acpi_drv->name, acpi_dev->pnp.bus_id));
409 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800410 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800411 return ret;
412}
413
414static int acpi_device_remove(struct device * dev)
415{
416 struct acpi_device *acpi_dev = to_acpi_device(dev);
417 struct acpi_driver *acpi_drv = acpi_dev->driver;
418
419 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000420 if (acpi_drv->ops.notify)
421 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800422 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800423 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800424 }
425 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700426 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800427
428 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800429 return 0;
430}
431
David Brownell55955aa2007-05-08 00:28:35 -0700432struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800433 .name = "acpi",
434 .suspend = acpi_device_suspend,
435 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800436 .match = acpi_bus_match,
437 .probe = acpi_device_probe,
438 .remove = acpi_device_remove,
439 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440};
441
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000442static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800444 int result;
445 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
446 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 * Linkage
450 * -------
451 * Link this device to its parent and siblings.
452 */
453 INIT_LIST_HEAD(&device->children);
454 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 INIT_LIST_HEAD(&device->wakeup_list);
456
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800457 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
458 if (!new_bus_id) {
459 printk(KERN_ERR PREFIX "Memory allocation error\n");
460 return -ENOMEM;
461 }
462
Shaohua Li90905892009-04-07 10:24:29 +0800463 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800464 /*
465 * Find suitable bus_id and instance number in acpi_bus_id_list
466 * If failed, create one and link it into acpi_bus_id_list
467 */
468 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600469 if (!strcmp(acpi_device_bus_id->bus_id,
470 acpi_device_hid(device))) {
471 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800472 found = 1;
473 kfree(new_bus_id);
474 break;
475 }
476 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600477 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800478 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600479 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800480 acpi_device_bus_id->instance_no = 0;
481 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
482 }
Kay Sievers07944692008-10-30 01:18:59 +0100483 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 +0800484
Len Brown33b57152008-12-15 22:09:26 -0500485 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (device->wakeup.flags.valid)
489 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800490 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Patrick Mochel1890a972006-12-07 20:56:31 +0800492 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000493 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800494 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800495 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600496 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600497 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600498 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800499 goto end;
500 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800501
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800502 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600503 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100504 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
505 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800506
Li Shaohua96333572006-12-07 20:56:46 +0800507 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800508 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600509end:
Shaohua Li90905892009-04-07 10:24:29 +0800510 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500511 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800512 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800513 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800514 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800515 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518static void acpi_device_unregister(struct acpi_device *device, int type)
519{
Shaohua Li90905892009-04-07 10:24:29 +0800520 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500521 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800525 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800528
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800529 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800530 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Zhang Rui9e89dde2006-12-07 20:56:16 +0800533/* --------------------------------------------------------------------------
534 Driver Management
535 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500537 * acpi_bus_driver_init - add a device to a driver
538 * @device: the device to add and initialize
539 * @driver: driver for the device
540 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600541 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800542 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544static int
Len Brown4be44fc2005-08-05 00:44:28 -0400545acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Len Brown4be44fc2005-08-05 00:44:28 -0400547 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400553 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 result = driver->ops.add(device);
556 if (result) {
557 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700558 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 device->driver = driver;
563
564 /*
565 * TBD - Configuration Management: Assign resources to device based
566 * upon possible configuration and currently allocated resources.
567 */
568
Len Brown4be44fc2005-08-05 00:44:28 -0400569 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
570 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400571 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700572}
573
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400574static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700575{
576 int result = 0;
577 struct acpi_driver *driver;
578
Rajesh Shah3fb02732005-04-28 00:25:52 -0700579
580 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400581 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (driver->ops.start) {
584 result = driver->ops.start(device);
585 if (result && driver->ops.remove)
586 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Patrick Mocheld550d982006-06-27 00:41:40 -0400589 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500593 * acpi_bus_register_driver - register a driver with the ACPI bus
594 * @driver: driver being registered
595 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500597 * devices that match the driver's criteria and binds. Returns zero for
598 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
Len Brown4be44fc2005-08-05 00:44:28 -0400600int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Patrick Mochel1890a972006-12-07 20:56:31 +0800602 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800606 driver->drv.name = driver->name;
607 driver->drv.bus = &acpi_bus_type;
608 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Patrick Mochel1890a972006-12-07 20:56:31 +0800610 ret = driver_register(&driver->drv);
611 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Len Brown4be44fc2005-08-05 00:44:28 -0400614EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500617 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
618 * @driver: driver to unregister
619 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * Unregisters a driver with the ACPI bus. Searches the namespace for all
621 * devices that match the driver's criteria and unbinds.
622 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400623void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Patrick Mochel1890a972006-12-07 20:56:31 +0800625 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
Len Brown4be44fc2005-08-05 00:44:28 -0400627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628EXPORT_SYMBOL(acpi_bus_unregister_driver);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/* --------------------------------------------------------------------------
631 Device Enumeration
632 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000633static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
634{
635 acpi_status status;
636 int ret;
637 struct acpi_device *device;
638
639 /*
640 * Fixed hardware devices do not appear in the namespace and do not
641 * have handles, but we fabricate acpi_devices for them, so we have
642 * to deal with them specially.
643 */
644 if (handle == NULL)
645 return acpi_root;
646
647 do {
648 status = acpi_get_parent(handle, &handle);
649 if (status == AE_NULL_ENTRY)
650 return NULL;
651 if (ACPI_FAILURE(status))
652 return acpi_root;
653
654 ret = acpi_bus_get_device(handle, &device);
655 if (ret == 0)
656 return device;
657 } while (1);
658}
659
Len Brownc8f7a622006-07-09 17:22:28 -0400660acpi_status
661acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
662{
663 acpi_status status;
664 acpi_handle tmp;
665 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
666 union acpi_object *obj;
667
668 status = acpi_get_handle(handle, "_EJD", &tmp);
669 if (ACPI_FAILURE(status))
670 return status;
671
672 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
673 if (ACPI_SUCCESS(status)) {
674 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100675 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
676 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400677 kfree(buffer.pointer);
678 }
679 return status;
680}
681EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
682
Bob Moore8e4319c2009-06-29 13:43:27 +0800683void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685
686 /* TBD */
687
688 return;
689}
690
Zhang Rui9e89dde2006-12-07 20:56:16 +0800691static int acpi_bus_get_perf_flags(struct acpi_device *device)
692{
693 device->performance.state = ACPI_STATE_UNKNOWN;
694 return 0;
695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697static acpi_status
698acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
699 union acpi_object *package)
700{
701 int i = 0;
702 union acpi_object *element = NULL;
703
704 if (!device || !package || (package->package.count < 2))
705 return AE_BAD_PARAMETER;
706
707 element = &(package->package.elements[0]);
708 if (!element)
709 return AE_BAD_PARAMETER;
710 if (element->type == ACPI_TYPE_PACKAGE) {
711 if ((element->package.count < 2) ||
712 (element->package.elements[0].type !=
713 ACPI_TYPE_LOCAL_REFERENCE)
714 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
715 return AE_BAD_DATA;
716 device->wakeup.gpe_device =
717 element->package.elements[0].reference.handle;
718 device->wakeup.gpe_number =
719 (u32) element->package.elements[1].integer.value;
720 } else if (element->type == ACPI_TYPE_INTEGER) {
721 device->wakeup.gpe_number = element->integer.value;
722 } else
723 return AE_BAD_DATA;
724
725 element = &(package->package.elements[1]);
726 if (element->type != ACPI_TYPE_INTEGER) {
727 return AE_BAD_DATA;
728 }
729 device->wakeup.sleep_state = element->integer.value;
730
731 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
732 return AE_NO_MEMORY;
733 }
734 device->wakeup.resources.count = package->package.count - 2;
735 for (i = 0; i < device->wakeup.resources.count; i++) {
736 element = &(package->package.elements[i + 2]);
Bob Moorecd0b2242008-04-10 19:06:43 +0400737 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 device->wakeup.resources.handles[i] = element->reference.handle;
741 }
742
743 return AE_OK;
744}
745
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100746static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200748 struct acpi_device_id button_device_ids[] = {
749 {"PNP0C0D", 0},
750 {"PNP0C0C", 0},
751 {"PNP0C0E", 0},
752 {"", 0},
753 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100754 acpi_status status;
755 acpi_event_status event_status;
756
757 device->wakeup.run_wake_count = 0;
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100758 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100759
760 /* Power button, Lid switch always enable wakeup */
761 if (!acpi_match_device_ids(device, button_device_ids)) {
762 device->wakeup.flags.run_wake = 1;
763 device->wakeup.flags.always_enabled = 1;
764 return;
765 }
766
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200767 status = acpi_get_gpe_status(device->wakeup.gpe_device,
768 device->wakeup.gpe_number,
769 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100770 if (status == AE_OK)
771 device->wakeup.flags.run_wake =
772 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
773}
774
775static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
776{
777 acpi_status status = 0;
778 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
779 union acpi_object *package = NULL;
780 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* _PRW */
783 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
784 if (ACPI_FAILURE(status)) {
785 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
786 goto end;
787 }
788
789 package = (union acpi_object *)buffer.pointer;
790 status = acpi_bus_extract_wakeup_device_power_package(device, package);
791 if (ACPI_FAILURE(status)) {
792 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
793 goto end;
794 }
795
796 kfree(buffer.pointer);
797
798 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200799 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100800 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800801 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
802 * system for the ACPI device with the _PRW object.
803 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
804 * So it is necessary to call _DSW object first. Only when it is not
805 * present will the _PSW object used.
806 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200807 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
808 if (psw_error)
809 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
810 "error in _DSW or _PSW evaluation\n"));
811
Alex Chiang0c526d92009-05-14 08:31:37 -0600812end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (ACPI_FAILURE(status))
814 device->flags.wake_capable = 0;
815 return 0;
816}
817
Zhang Rui9e89dde2006-12-07 20:56:16 +0800818static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800820 acpi_status status = 0;
821 acpi_handle handle = NULL;
822 u32 i = 0;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800826 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800828 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800830 device->power.flags.explicit_get = 1;
831 status = acpi_get_handle(device->handle, "_IRC", &handle);
832 if (ACPI_SUCCESS(status))
833 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800836 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800838 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
839 struct acpi_device_power_state *ps = &device->power.states[i];
840 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Zhang Rui9e89dde2006-12-07 20:56:16 +0800842 /* Evaluate "_PRx" to se if power resources are referenced */
843 acpi_evaluate_reference(device->handle, object_name, NULL,
844 &ps->resources);
845 if (ps->resources.count) {
846 device->power.flags.power_resources = 1;
847 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Zhang Rui9e89dde2006-12-07 20:56:16 +0800850 /* Evaluate "_PSx" to see if we can do explicit sets */
851 object_name[2] = 'S';
852 status = acpi_get_handle(device->handle, object_name, &handle);
853 if (ACPI_SUCCESS(status)) {
854 ps->flags.explicit_set = 1;
855 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800857
858 /* State is valid if we have some power control */
859 if (ps->resources.count || ps->flags.explicit_set)
860 ps->flags.valid = 1;
861
862 ps->power = -1; /* Unknown - driver assigned */
863 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Zhang Rui9e89dde2006-12-07 20:56:16 +0800866 /* Set defaults for D0 and D3 states (always valid) */
867 device->power.states[ACPI_STATE_D0].flags.valid = 1;
868 device->power.states[ACPI_STATE_D0].power = 100;
869 device->power.states[ACPI_STATE_D3].flags.valid = 1;
870 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Zhang Rui9e89dde2006-12-07 20:56:16 +0800872 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Zhang Rui9e89dde2006-12-07 20:56:16 +0800874 device->power.state = ACPI_STATE_UNKNOWN;
Zhao Yakuia51e1452008-08-11 14:55:05 +0800875 acpi_bus_get_power(device->handle, &(device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 return 0;
878}
879
Len Brown4be44fc2005-08-05 00:44:28 -0400880static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Len Brown4be44fc2005-08-05 00:44:28 -0400882 acpi_status status = AE_OK;
883 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* Presence of _STA indicates 'dynamic_status' */
887 status = acpi_get_handle(device->handle, "_STA", &temp);
888 if (ACPI_SUCCESS(status))
889 device->flags.dynamic_status = 1;
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Presence of _RMV indicates 'removable' */
892 status = acpi_get_handle(device->handle, "_RMV", &temp);
893 if (ACPI_SUCCESS(status))
894 device->flags.removable = 1;
895
896 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
897 status = acpi_get_handle(device->handle, "_EJD", &temp);
898 if (ACPI_SUCCESS(status))
899 device->flags.ejectable = 1;
900 else {
901 status = acpi_get_handle(device->handle, "_EJ0", &temp);
902 if (ACPI_SUCCESS(status))
903 device->flags.ejectable = 1;
904 }
905
906 /* Presence of _LCK indicates 'lockable' */
907 status = acpi_get_handle(device->handle, "_LCK", &temp);
908 if (ACPI_SUCCESS(status))
909 device->flags.lockable = 1;
910
911 /* Presence of _PS0|_PR0 indicates 'power manageable' */
912 status = acpi_get_handle(device->handle, "_PS0", &temp);
913 if (ACPI_FAILURE(status))
914 status = acpi_get_handle(device->handle, "_PR0", &temp);
915 if (ACPI_SUCCESS(status))
916 device->flags.power_manageable = 1;
917
918 /* Presence of _PRW indicates wake capable */
919 status = acpi_get_handle(device->handle, "_PRW", &temp);
920 if (ACPI_SUCCESS(status))
921 device->flags.wake_capable = 1;
922
Joe Perches3c5f9be42008-02-03 17:06:17 +0200923 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Patrick Mocheld550d982006-06-27 00:41:40 -0400925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +0000928static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Len Brown4be44fc2005-08-05 00:44:28 -0400930 char bus_id[5] = { '?', 0 };
931 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
932 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /*
935 * Bus ID
936 * ------
937 * The device's Bus ID is simply the object name.
938 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
939 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +0000940 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +0000942 return;
943 }
944
945 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 case ACPI_BUS_TYPE_POWER_BUTTON:
947 strcpy(device->pnp.bus_id, "PWRF");
948 break;
949 case ACPI_BUS_TYPE_SLEEP_BUTTON:
950 strcpy(device->pnp.bus_id, "SLPF");
951 break;
952 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000953 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* Clean up trailing underscores (if any) */
955 for (i = 3; i > 1; i--) {
956 if (bus_id[i] == '_')
957 bus_id[i] = '\0';
958 else
959 break;
960 }
961 strcpy(device->pnp.bus_id, bus_id);
962 break;
963 }
964}
965
Zhang Rui54735262007-01-11 02:09:09 -0500966/*
967 * acpi_bay_match - see if a device is an ejectable driver bay
968 *
969 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
970 * then we can safely call it an ejectable drive bay
971 */
972static int acpi_bay_match(struct acpi_device *device){
973 acpi_status status;
974 acpi_handle handle;
975 acpi_handle tmp;
976 acpi_handle phandle;
977
978 handle = device->handle;
979
980 status = acpi_get_handle(handle, "_EJ0", &tmp);
981 if (ACPI_FAILURE(status))
982 return -ENODEV;
983
984 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
985 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
986 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
987 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
988 return 0;
989
990 if (acpi_get_parent(handle, &phandle))
991 return -ENODEV;
992
993 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
994 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
995 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
996 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
997 return 0;
998
999 return -ENODEV;
1000}
1001
Frank Seidel3620f2f2007-12-07 13:20:34 +01001002/*
1003 * acpi_dock_match - see if a device has a _DCK method
1004 */
1005static int acpi_dock_match(struct acpi_device *device)
1006{
1007 acpi_handle tmp;
1008 return acpi_get_handle(device->handle, "_DCK", &tmp);
1009}
1010
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001011char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001012{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001013 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001014
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001015 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1016 return hid->id;
1017}
1018EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001019
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001020static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1021{
1022 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001023
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001024 id = kmalloc(sizeof(*id), GFP_KERNEL);
1025 if (!id)
1026 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001027
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001028 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1029 if (!id->id) {
1030 kfree(id);
1031 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001032 }
1033
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001034 strcpy(id->id, dev_id);
1035 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001036}
1037
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001038/*
1039 * Old IBM workstations have a DSDT bug wherein the SMBus object
1040 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1041 * prefix. Work around this.
1042 */
1043static int acpi_ibm_smbus_match(struct acpi_device *device)
1044{
1045 acpi_handle h_dummy;
1046 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1047 int result;
1048
1049 if (!dmi_name_in_vendors("IBM"))
1050 return -ENODEV;
1051
1052 /* Look for SMBS object */
1053 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1054 if (result)
1055 return result;
1056
1057 if (strcmp("SMBS", path.pointer)) {
1058 result = -ENODEV;
1059 goto out;
1060 }
1061
1062 /* Does it have the necessary (but misnamed) methods? */
1063 result = -ENODEV;
1064 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1065 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1066 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1067 result = 0;
1068out:
1069 kfree(path.pointer);
1070 return result;
1071}
1072
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001073static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Len Brown4be44fc2005-08-05 00:44:28 -04001075 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001076 struct acpi_device_info *info;
1077 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001078 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001080 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001082 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001083 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001084 break;
1085 }
1086
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001087 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001089 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return;
1091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001094 acpi_add_id(device, info->hardware_id.string);
1095 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001096 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001097 for (i = 0; i < cid_list->count; i++)
1098 acpi_add_id(device, cid_list->ids[i].string);
1099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (info->valid & ACPI_VALID_ADR) {
1101 device->pnp.bus_address = info->address;
1102 device->flags.bus_address = 1;
1103 }
Zhang Ruiae843332006-12-07 20:57:10 +08001104
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001105 kfree(info);
1106
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001107 /*
1108 * Some devices don't reliably have _HIDs & _CIDs, so add
1109 * synthetic HIDs to make sure drivers can find them.
1110 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001111 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001112 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001113 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001114 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001115 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001116 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001117 else if (!acpi_ibm_smbus_match(device))
1118 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001119 else if (!acpi_device_hid(device) &&
1120 ACPI_IS_ROOT_DEVICE(device->parent)) {
1121 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1122 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1123 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1124 }
Zhang Rui54735262007-01-11 02:09:09 -05001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 break;
1127 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001128 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 break;
1130 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001131 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001134 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
1136 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001137 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
1139 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001140 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
1142 }
1143
Bjorn Helgaasb1fbfb22009-09-21 13:35:14 -06001144 /*
1145 * We build acpi_devices for some objects that don't have _HID or _CID,
1146 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1147 * but we do use them indirectly by traversing the acpi_device tree.
1148 * This generic ID isn't useful for driver binding, but it provides
1149 * the useful property that "every acpi_device has an ID."
1150 */
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001151 if (list_empty(&device->pnp.ids))
1152 acpi_add_id(device, "device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001155static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001157 acpi_status status;
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /*
1160 * Context
1161 * -------
1162 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001163 * resolutions from handle->device very efficient. Fixed hardware
1164 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001166 if (!device->handle)
1167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001169 status = acpi_attach_data(device->handle,
1170 acpi_bus_data_handler, device);
1171 if (ACPI_SUCCESS(status))
1172 return 0;
1173
1174 printk(KERN_ERR PREFIX "Error attaching device data\n");
1175 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
Len Brown4be44fc2005-08-05 00:44:28 -04001178static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001181 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Li Shaohua96333572006-12-07 20:56:46 +08001183 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001184 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Rui Zhang2786f6e2006-12-21 02:21:13 -05001189 /*
1190 * unbind _ADR-Based Devices when hot removal
1191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (dev->flags.bus_address) {
1193 if ((dev->parent) && (dev->parent->ops.unbind))
1194 dev->parent->ops.unbind(dev);
1195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1197
Patrick Mocheld550d982006-06-27 00:41:40 -04001198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001201static int acpi_add_single_object(struct acpi_device **child,
1202 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001203 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001204 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001206 int result;
1207 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001208 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Burman Yan36bcbec2006-12-19 12:56:11 -08001210 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001212 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001216 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001217 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001219 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001220 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001221 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001222
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001223 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 /*
1226 * Flags
1227 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001228 * Note that we only look for object handles -- cannot evaluate objects
1229 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 */
1231 result = acpi_bus_get_flags(device);
1232 if (result)
1233 goto end;
1234
1235 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * Initialize Device
1237 * -----------------
1238 * TBD: Synch with Core's enumeration/initialization process.
1239 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001240 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /*
1243 * Power Management
1244 * ----------------
1245 */
1246 if (device->flags.power_manageable) {
1247 result = acpi_bus_get_power_flags(device);
1248 if (result)
1249 goto end;
1250 }
1251
Len Brown4be44fc2005-08-05 00:44:28 -04001252 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * Wakeup device management
1254 *-----------------------
1255 */
1256 if (device->flags.wake_capable) {
1257 result = acpi_bus_get_wakeup_device_flags(device);
1258 if (result)
1259 goto end;
1260 }
1261
1262 /*
1263 * Performance Management
1264 * ----------------------
1265 */
1266 if (device->flags.performance_manageable) {
1267 result = acpi_bus_get_perf_flags(device);
1268 if (result)
1269 goto end;
1270 }
1271
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001272 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001273 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001275 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001278 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 */
1280 if (device->flags.bus_address) {
1281 if (device->parent && device->parent->ops.bind)
1282 device->parent->ops.bind(device);
1283 }
1284
Alex Chiang0c526d92009-05-14 08:31:37 -06001285end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001286 if (!result) {
1287 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1288 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1289 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1290 (char *) buffer.pointer,
1291 device->parent ? dev_name(&device->parent->dev) :
1292 "(null)"));
1293 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001295 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001296 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Patrick Mocheld550d982006-06-27 00:41:40 -04001298 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001301#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1302 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1303
1304static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1305 unsigned long long *sta)
1306{
1307 acpi_status status;
1308 acpi_object_type acpi_type;
1309
1310 status = acpi_get_type(handle, &acpi_type);
1311 if (ACPI_FAILURE(status))
1312 return -ENODEV;
1313
1314 switch (acpi_type) {
1315 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1316 case ACPI_TYPE_DEVICE:
1317 *type = ACPI_BUS_TYPE_DEVICE;
1318 status = acpi_bus_get_status_handle(handle, sta);
1319 if (ACPI_FAILURE(status))
1320 return -ENODEV;
1321 break;
1322 case ACPI_TYPE_PROCESSOR:
1323 *type = ACPI_BUS_TYPE_PROCESSOR;
1324 status = acpi_bus_get_status_handle(handle, sta);
1325 if (ACPI_FAILURE(status))
1326 return -ENODEV;
1327 break;
1328 case ACPI_TYPE_THERMAL:
1329 *type = ACPI_BUS_TYPE_THERMAL;
1330 *sta = ACPI_STA_DEFAULT;
1331 break;
1332 case ACPI_TYPE_POWER:
1333 *type = ACPI_BUS_TYPE_POWER;
1334 *sta = ACPI_STA_DEFAULT;
1335 break;
1336 default:
1337 return -ENODEV;
1338 }
1339
1340 return 0;
1341}
1342
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001343static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1344 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001346 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001347 int type;
1348 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001349 struct acpi_device *device;
1350 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001351 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001352
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001353 result = acpi_bus_type_and_status(handle, &type, &sta);
1354 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001355 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001357 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1358 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1359 return AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001361 /*
1362 * We may already have an acpi_device from a previous enumeration. If
1363 * so, we needn't add it again, but we may still have to start it.
1364 */
1365 device = NULL;
1366 acpi_bus_get_device(handle, &device);
1367 if (ops->acpi_op_add && !device)
1368 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001369
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001370 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001371 return AE_CTRL_DEPTH;
1372
1373 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1374 status = acpi_start_single_object(device);
1375 if (ACPI_FAILURE(status))
1376 return AE_CTRL_DEPTH;
1377 }
1378
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001379 if (!*return_value)
1380 *return_value = device;
1381 return AE_OK;
1382}
1383
1384static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1385 struct acpi_device **child)
1386{
1387 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001388 void *device = NULL;
1389
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001390 status = acpi_bus_check_add(handle, 0, ops, &device);
1391 if (ACPI_SUCCESS(status))
1392 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001393 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001394
1395 if (child)
1396 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001397
1398 if (device)
1399 return 0;
1400 else
1401 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Thomas Renninger77796882010-01-29 17:48:52 +01001404/*
1405 * acpi_bus_add and acpi_bus_start
1406 *
1407 * scan a given ACPI tree and (probably recently hot-plugged)
1408 * create and add or starts found devices.
1409 *
1410 * If no devices were found -ENODEV is returned which does not
1411 * mean that this is a real error, there just have been no suitable
1412 * ACPI objects in the table trunk from which the kernel could create
1413 * a device and add/start an appropriate driver.
1414 */
1415
Rajesh Shah3fb02732005-04-28 00:25:52 -07001416int
Len Brown4be44fc2005-08-05 00:44:28 -04001417acpi_bus_add(struct acpi_device **child,
1418 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001419{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001420 struct acpi_bus_ops ops;
1421
Li Shaohuac4168bf2006-12-07 20:56:41 +08001422 memset(&ops, 0, sizeof(ops));
1423 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001424
Thomas Renninger77796882010-01-29 17:48:52 +01001425 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001426}
1427EXPORT_SYMBOL(acpi_bus_add);
1428
Len Brown4be44fc2005-08-05 00:44:28 -04001429int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001430{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001431 struct acpi_bus_ops ops;
1432
Thomas Renningerd2f66502010-01-29 17:48:51 +01001433 if (!device)
1434 return -EINVAL;
1435
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001436 memset(&ops, 0, sizeof(ops));
1437 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001438
Thomas Renninger77796882010-01-29 17:48:52 +01001439 return acpi_bus_scan(device->handle, &ops, NULL);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001440}
1441EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Kristen Accardiceaba662006-02-23 17:56:01 -08001443int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Len Brown4be44fc2005-08-05 00:44:28 -04001445 acpi_status status;
1446 struct acpi_device *parent, *child;
1447 acpi_handle phandle, chandle;
1448 acpi_object_type type;
1449 u32 level = 1;
1450 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Len Brown4be44fc2005-08-05 00:44:28 -04001452 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 phandle = start->handle;
1454 child = chandle = NULL;
1455
1456 while ((level > 0) && parent && (!err)) {
1457 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001458 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /*
1461 * If this scope is exhausted then move our way back up.
1462 */
1463 if (ACPI_FAILURE(status)) {
1464 level--;
1465 chandle = phandle;
1466 acpi_get_parent(phandle, &phandle);
1467 child = parent;
1468 parent = parent->parent;
1469
1470 if (level == 0)
1471 err = acpi_bus_remove(child, rmdevice);
1472 else
1473 err = acpi_bus_remove(child, 1);
1474
1475 continue;
1476 }
1477
1478 status = acpi_get_type(chandle, &type);
1479 if (ACPI_FAILURE(status)) {
1480 continue;
1481 }
1482 /*
1483 * If there is a device corresponding to chandle then
1484 * parse it (depth-first).
1485 */
1486 if (acpi_bus_get_device(chandle, &child) == 0) {
1487 level++;
1488 phandle = chandle;
1489 chandle = NULL;
1490 parent = child;
1491 }
1492 continue;
1493 }
1494 return err;
1495}
Kristen Accardiceaba662006-02-23 17:56:01 -08001496EXPORT_SYMBOL_GPL(acpi_bus_trim);
1497
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001498static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Len Brown4be44fc2005-08-05 00:44:28 -04001500 int result = 0;
1501 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001502 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Li Shaohuac4168bf2006-12-07 20:56:41 +08001504 memset(&ops, 0, sizeof(ops));
1505 ops.acpi_op_add = 1;
1506 ops.acpi_op_start = 1;
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /*
1509 * Enumerate all fixed-feature devices.
1510 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001511 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001512 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001513 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001514 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001515 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001518 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001519 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001520 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001521 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001522 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Patrick Mocheld550d982006-06-27 00:41:40 -04001525 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526}
1527
Bjorn Helgaase747f272009-03-24 16:49:43 -06001528int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
1530 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001531 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Li Shaohuac4168bf2006-12-07 20:56:41 +08001533 memset(&ops, 0, sizeof(ops));
1534 ops.acpi_op_add = 1;
1535 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Patrick Mochel5b327262006-05-10 10:33:00 -04001537 result = bus_register(&acpi_bus_type);
1538 if (result) {
1539 /* We don't want to quit even if we failed to add suspend/resume */
1540 printk(KERN_ERR PREFIX "Could not register bus type\n");
1541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * Enumerate devices in the ACPI namespace.
1545 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001546 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001547
Li Shaohuac4168bf2006-12-07 20:56:41 +08001548 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001549 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 if (result)
1552 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1553
Patrick Mocheld550d982006-06-27 00:41:40 -04001554 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}