blob: 189cbc2585fa1cf51ca2ee45615b5f2176c85197 [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>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010011#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060015#include "internal.h"
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050018ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040020extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020023#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define ACPI_BUS_DEVICE_NAME "System Bus"
25
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +000026#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080029static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080030DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031LIST_HEAD(acpi_wakeup_device_list);
32
Zhang Ruie49bd2d2006-12-08 17:23:43 +080033struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080034 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080035 unsigned int instance_no;
36 struct list_head node;
37};
Thomas Renninger29b71a12007-07-23 14:43:51 +020038
39/*
40 * Creates hid/cid(s) string needed for modalias and uevent
41 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
42 * char *modalias: "acpi:IBM0001:ACPI0001"
43*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020044static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
45 int size)
46{
Thomas Renninger29b71a12007-07-23 14:43:51 +020047 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080048 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060049 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020050
Zhang Rui5c9fcb52008-03-20 16:40:32 +080051 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020052 size -= len;
53
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060054 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
55 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080056 if (count < 0 || count >= size)
57 return -EINVAL;
58 len += count;
59 size -= count;
60 }
61
Thomas Renninger29b71a12007-07-23 14:43:51 +020062 modalias[len] = '\0';
63 return len;
64}
65
66static ssize_t
67acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
68 struct acpi_device *acpi_dev = to_acpi_device(dev);
69 int len;
70
71 /* Device has no HID and no CID or string is >1024 */
72 len = create_modalias(acpi_dev, buf, 1024);
73 if (len <= 0)
74 return 0;
75 buf[len++] = '\n';
76 return len;
77}
78static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
79
Zhang Ruic8d72a52009-06-22 11:31:16 +080080static void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Zhang Rui26d46862008-04-29 02:35:48 -040082 struct acpi_device *device;
83 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct acpi_object_list arg_list;
85 union acpi_object arg;
86 acpi_status status = AE_OK;
87
Zhang Rui26d46862008-04-29 02:35:48 -040088 if (acpi_bus_get_device(handle, &device))
Zhang Ruic8d72a52009-06-22 11:31:16 +080089 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Zhang Rui26d46862008-04-29 02:35:48 -040091 if (!device)
Zhang Ruic8d72a52009-06-22 11:31:16 +080092 return;
Zhang Rui26d46862008-04-29 02:35:48 -040093
94 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +010095 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -040096
97 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +080098 printk(KERN_ERR PREFIX
99 "Removing device failed\n");
Zhang Ruic8d72a52009-06-22 11:31:16 +0800100 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400101 }
102
103 /* power off device */
104 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
105 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800106 printk(KERN_WARNING PREFIX
107 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400108
109 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 arg_list.count = 1;
111 arg_list.pointer = &arg;
112 arg.type = ACPI_TYPE_INTEGER;
113 arg.integer.value = 0;
114 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
115 }
116
117 arg_list.count = 1;
118 arg_list.pointer = &arg;
119 arg.type = ACPI_TYPE_INTEGER;
120 arg.integer.value = 1;
121
122 /*
123 * TBD: _EJD support.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400126 if (ACPI_FAILURE(status))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800127 printk(KERN_WARNING PREFIX
128 "Eject device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Zhang Ruic8d72a52009-06-22 11:31:16 +0800130 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800134acpi_eject_store(struct device *d, struct device_attribute *attr,
135 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Len Brown4be44fc2005-08-05 00:44:28 -0400137 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400139 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800140 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if ((!count) || (buf[0] != '1')) {
143 return -EINVAL;
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800146 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 ret = -ENODEV;
148 goto err;
149 }
150#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800151 status = acpi_get_type(acpi_device->handle, &type);
152 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 ret = -ENODEV;
154 goto err;
155 }
156
Zhang Ruic8d72a52009-06-22 11:31:16 +0800157 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
Alok N Kataria74523c92008-06-13 12:54:24 -0400158err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800162static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800164static ssize_t
165acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
166 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600168 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800169}
170static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
171
172static ssize_t
173acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
174 struct acpi_device *acpi_dev = to_acpi_device(dev);
175 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
176 int result;
177
178 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600179 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800180 goto end;
181
182 result = sprintf(buf, "%s\n", (char*)path.pointer);
183 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600184end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800185 return result;
186}
187static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
188
189static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800191 acpi_status status;
192 acpi_handle temp;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800193 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800195 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800196 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800197 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600198 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800199 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600200 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800201 goto end;
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600204 result = device_create_file(&dev->dev, &dev_attr_hid);
205 if (result)
206 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600208 result = device_create_file(&dev->dev, &dev_attr_modalias);
209 if (result)
210 goto end;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200211
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800212 /*
213 * If device has _EJ0, 'eject' file is created that is used to trigger
214 * hot-removal function from userland.
215 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800216 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
217 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800218 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600219end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800220 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800223static void acpi_device_remove_files(struct acpi_device *dev)
224{
225 acpi_status status;
226 acpi_handle temp;
227
228 /*
229 * If device has _EJ0, 'eject' file is created that is used to trigger
230 * hot-removal function from userland.
231 */
232 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
233 if (ACPI_SUCCESS(status))
234 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800235
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600236 device_remove_file(&dev->dev, &dev_attr_modalias);
237 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600238 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800239 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800242 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200244
245int acpi_match_device_ids(struct acpi_device *device,
246 const struct acpi_device_id *ids)
247{
248 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600249 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200250
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800251 /*
252 * If the device is not present, it is unnecessary to load device
253 * driver for it.
254 */
255 if (!device->status.present)
256 return -ENODEV;
257
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600258 for (id = ids; id->id[0]; id++)
259 list_for_each_entry(hwid, &device->pnp.ids, list)
260 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200261 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200262
263 return -ENOENT;
264}
265EXPORT_SYMBOL(acpi_match_device_ids);
266
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600267static void acpi_free_ids(struct acpi_device *device)
268{
269 struct acpi_hardware_id *id, *tmp;
270
271 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
272 kfree(id->id);
273 kfree(id);
274 }
275}
276
Patrick Mochel1890a972006-12-07 20:56:31 +0800277static void acpi_device_release(struct device *dev)
278{
279 struct acpi_device *acpi_dev = to_acpi_device(dev);
280
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600281 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800282 kfree(acpi_dev);
283}
284
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800285static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800286{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800287 struct acpi_device *acpi_dev = to_acpi_device(dev);
288 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800289
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800290 if (acpi_drv && acpi_drv->ops.suspend)
291 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return 0;
293}
294
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800295static int acpi_device_resume(struct device *dev)
296{
297 struct acpi_device *acpi_dev = to_acpi_device(dev);
298 struct acpi_driver *acpi_drv = acpi_dev->driver;
299
300 if (acpi_drv && acpi_drv->ops.resume)
301 return acpi_drv->ops.resume(acpi_dev);
302 return 0;
303}
304
305static int acpi_bus_match(struct device *dev, struct device_driver *drv)
306{
307 struct acpi_device *acpi_dev = to_acpi_device(dev);
308 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
309
Thomas Renninger29b71a12007-07-23 14:43:51 +0200310 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800311}
312
Kay Sievers7eff2e72007-08-14 15:15:12 +0200313static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800314{
315 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200316 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800317
Kay Sievers7eff2e72007-08-14 15:15:12 +0200318 if (add_uevent_var(env, "MODALIAS="))
319 return -ENOMEM;
320 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
321 sizeof(env->buf) - env->buflen);
322 if (len >= (sizeof(env->buf) - env->buflen))
323 return -ENOMEM;
324 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000328static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
329{
330 struct acpi_device *device = data;
331
332 device->driver->ops.notify(device, event);
333}
334
335static acpi_status acpi_device_notify_fixed(void *data)
336{
337 struct acpi_device *device = data;
338
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000339 /* Fixed hardware devices have no handles */
340 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000341 return AE_OK;
342}
343
344static int acpi_device_install_notify_handler(struct acpi_device *device)
345{
346 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000347
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000348 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000349 status =
350 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
351 acpi_device_notify_fixed,
352 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000353 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000354 status =
355 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
356 acpi_device_notify_fixed,
357 device);
358 else
359 status = acpi_install_notify_handler(device->handle,
360 ACPI_DEVICE_NOTIFY,
361 acpi_device_notify,
362 device);
363
364 if (ACPI_FAILURE(status))
365 return -EINVAL;
366 return 0;
367}
368
369static void acpi_device_remove_notify_handler(struct acpi_device *device)
370{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000371 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000372 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
373 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000374 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000375 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
376 acpi_device_notify_fixed);
377 else
378 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
379 acpi_device_notify);
380}
381
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800382static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
383static int acpi_start_single_object(struct acpi_device *);
384static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800385{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800386 struct acpi_device *acpi_dev = to_acpi_device(dev);
387 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
388 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800390 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
391 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800392 if (acpi_dev->bus_ops.acpi_op_start)
393 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000394
395 if (acpi_drv->ops.notify) {
396 ret = acpi_device_install_notify_handler(acpi_dev);
397 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000398 if (acpi_drv->ops.remove)
399 acpi_drv->ops.remove(acpi_dev,
400 acpi_dev->removal_type);
401 return ret;
402 }
403 }
404
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
406 "Found driver [%s] for device [%s]\n",
407 acpi_drv->name, acpi_dev->pnp.bus_id));
408 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800409 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800410 return ret;
411}
412
413static int acpi_device_remove(struct device * dev)
414{
415 struct acpi_device *acpi_dev = to_acpi_device(dev);
416 struct acpi_driver *acpi_drv = acpi_dev->driver;
417
418 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000419 if (acpi_drv->ops.notify)
420 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800421 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800422 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800423 }
424 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700425 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800426
427 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800428 return 0;
429}
430
David Brownell55955aa2007-05-08 00:28:35 -0700431struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800432 .name = "acpi",
433 .suspend = acpi_device_suspend,
434 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800435 .match = acpi_bus_match,
436 .probe = acpi_device_probe,
437 .remove = acpi_device_remove,
438 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000441static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800443 int result;
444 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
445 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * Linkage
449 * -------
450 * Link this device to its parent and siblings.
451 */
452 INIT_LIST_HEAD(&device->children);
453 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 INIT_LIST_HEAD(&device->wakeup_list);
455
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800456 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
457 if (!new_bus_id) {
458 printk(KERN_ERR PREFIX "Memory allocation error\n");
459 return -ENOMEM;
460 }
461
Shaohua Li90905892009-04-07 10:24:29 +0800462 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800463 /*
464 * Find suitable bus_id and instance number in acpi_bus_id_list
465 * If failed, create one and link it into acpi_bus_id_list
466 */
467 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600468 if (!strcmp(acpi_device_bus_id->bus_id,
469 acpi_device_hid(device))) {
470 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800471 found = 1;
472 kfree(new_bus_id);
473 break;
474 }
475 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600476 if (!found) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800477 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600478 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800479 acpi_device_bus_id->instance_no = 0;
480 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
481 }
Kay Sievers07944692008-10-30 01:18:59 +0100482 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800483
Len Brown33b57152008-12-15 22:09:26 -0500484 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (device->wakeup.flags.valid)
488 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800489 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Patrick Mochel1890a972006-12-07 20:56:31 +0800491 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000492 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800493 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800494 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600495 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600496 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600497 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800498 goto end;
499 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800500
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800501 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600502 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100503 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
504 dev_name(&device->dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800505
Li Shaohua96333572006-12-07 20:56:46 +0800506 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800507 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600508end:
Shaohua Li90905892009-04-07 10:24:29 +0800509 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500510 if (device->parent)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800511 list_del(&device->node);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800512 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800513 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800514 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517static void acpi_device_unregister(struct acpi_device *device, int type)
518{
Shaohua Li90905892009-04-07 10:24:29 +0800519 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500520 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800524 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800527
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800528 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800529 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Zhang Rui9e89dde2006-12-07 20:56:16 +0800532/* --------------------------------------------------------------------------
533 Driver Management
534 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500536 * acpi_bus_driver_init - add a device to a driver
537 * @device: the device to add and initialize
538 * @driver: driver for the device
539 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600540 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800541 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543static int
Len Brown4be44fc2005-08-05 00:44:28 -0400544acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Len Brown4be44fc2005-08-05 00:44:28 -0400546 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 result = driver->ops.add(device);
555 if (result) {
556 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700557 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400558 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
561 device->driver = driver;
562
563 /*
564 * TBD - Configuration Management: Assign resources to device based
565 * upon possible configuration and currently allocated resources.
566 */
567
Len Brown4be44fc2005-08-05 00:44:28 -0400568 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
569 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400570 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700571}
572
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400573static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700574{
575 int result = 0;
576 struct acpi_driver *driver;
577
Rajesh Shah3fb02732005-04-28 00:25:52 -0700578
579 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400580 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (driver->ops.start) {
583 result = driver->ops.start(device);
584 if (result && driver->ops.remove)
585 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
Patrick Mocheld550d982006-06-27 00:41:40 -0400588 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500592 * acpi_bus_register_driver - register a driver with the ACPI bus
593 * @driver: driver being registered
594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500596 * devices that match the driver's criteria and binds. Returns zero for
597 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Len Brown4be44fc2005-08-05 00:44:28 -0400599int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Patrick Mochel1890a972006-12-07 20:56:31 +0800601 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400604 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800605 driver->drv.name = driver->name;
606 driver->drv.bus = &acpi_bus_type;
607 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Patrick Mochel1890a972006-12-07 20:56:31 +0800609 ret = driver_register(&driver->drv);
610 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Len Brown4be44fc2005-08-05 00:44:28 -0400613EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500616 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
617 * @driver: driver to unregister
618 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * Unregisters a driver with the ACPI bus. Searches the namespace for all
620 * devices that match the driver's criteria and unbinds.
621 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400622void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Patrick Mochel1890a972006-12-07 20:56:31 +0800624 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
Len Brown4be44fc2005-08-05 00:44:28 -0400626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627EXPORT_SYMBOL(acpi_bus_unregister_driver);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/* --------------------------------------------------------------------------
630 Device Enumeration
631 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000632static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
633{
634 acpi_status status;
635 int ret;
636 struct acpi_device *device;
637
638 /*
639 * Fixed hardware devices do not appear in the namespace and do not
640 * have handles, but we fabricate acpi_devices for them, so we have
641 * to deal with them specially.
642 */
643 if (handle == NULL)
644 return acpi_root;
645
646 do {
647 status = acpi_get_parent(handle, &handle);
648 if (status == AE_NULL_ENTRY)
649 return NULL;
650 if (ACPI_FAILURE(status))
651 return acpi_root;
652
653 ret = acpi_bus_get_device(handle, &device);
654 if (ret == 0)
655 return device;
656 } while (1);
657}
658
Len Brownc8f7a622006-07-09 17:22:28 -0400659acpi_status
660acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
661{
662 acpi_status status;
663 acpi_handle tmp;
664 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
665 union acpi_object *obj;
666
667 status = acpi_get_handle(handle, "_EJD", &tmp);
668 if (ACPI_FAILURE(status))
669 return status;
670
671 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
672 if (ACPI_SUCCESS(status)) {
673 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100674 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
675 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400676 kfree(buffer.pointer);
677 }
678 return status;
679}
680EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
681
Bob Moore8e4319c2009-06-29 13:43:27 +0800682void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684
685 /* TBD */
686
687 return;
688}
689
Zhang Rui9e89dde2006-12-07 20:56:16 +0800690static int acpi_bus_get_perf_flags(struct acpi_device *device)
691{
692 device->performance.state = ACPI_STATE_UNKNOWN;
693 return 0;
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696static acpi_status
697acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
698 union acpi_object *package)
699{
700 int i = 0;
701 union acpi_object *element = NULL;
702
703 if (!device || !package || (package->package.count < 2))
704 return AE_BAD_PARAMETER;
705
706 element = &(package->package.elements[0]);
707 if (!element)
708 return AE_BAD_PARAMETER;
709 if (element->type == ACPI_TYPE_PACKAGE) {
710 if ((element->package.count < 2) ||
711 (element->package.elements[0].type !=
712 ACPI_TYPE_LOCAL_REFERENCE)
713 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
714 return AE_BAD_DATA;
715 device->wakeup.gpe_device =
716 element->package.elements[0].reference.handle;
717 device->wakeup.gpe_number =
718 (u32) element->package.elements[1].integer.value;
719 } else if (element->type == ACPI_TYPE_INTEGER) {
720 device->wakeup.gpe_number = element->integer.value;
721 } else
722 return AE_BAD_DATA;
723
724 element = &(package->package.elements[1]);
725 if (element->type != ACPI_TYPE_INTEGER) {
726 return AE_BAD_DATA;
727 }
728 device->wakeup.sleep_state = element->integer.value;
729
730 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
731 return AE_NO_MEMORY;
732 }
733 device->wakeup.resources.count = package->package.count - 2;
734 for (i = 0; i < device->wakeup.resources.count; i++) {
735 element = &(package->package.elements[i + 2]);
Bob Moorecd0b2242008-04-10 19:06:43 +0400736 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 device->wakeup.resources.handles[i] = element->reference.handle;
740 }
741
742 return AE_OK;
743}
744
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100745static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200747 struct acpi_device_id button_device_ids[] = {
748 {"PNP0C0D", 0},
749 {"PNP0C0C", 0},
750 {"PNP0C0E", 0},
751 {"", 0},
752 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100753 acpi_status status;
754 acpi_event_status event_status;
755
756 device->wakeup.run_wake_count = 0;
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100757 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100758
759 /* Power button, Lid switch always enable wakeup */
760 if (!acpi_match_device_ids(device, button_device_ids)) {
761 device->wakeup.flags.run_wake = 1;
762 device->wakeup.flags.always_enabled = 1;
763 return;
764 }
765
766 status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
767 ACPI_NOT_ISR, &event_status);
768 if (status == AE_OK)
769 device->wakeup.flags.run_wake =
770 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
771}
772
773static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
774{
775 acpi_status status = 0;
776 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
777 union acpi_object *package = NULL;
778 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* _PRW */
781 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
782 if (ACPI_FAILURE(status)) {
783 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
784 goto end;
785 }
786
787 package = (union acpi_object *)buffer.pointer;
788 status = acpi_bus_extract_wakeup_device_power_package(device, package);
789 if (ACPI_FAILURE(status)) {
790 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
791 goto end;
792 }
793
794 kfree(buffer.pointer);
795
796 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200797 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100798 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800799 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
800 * system for the ACPI device with the _PRW object.
801 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
802 * So it is necessary to call _DSW object first. Only when it is not
803 * present will the _PSW object used.
804 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200805 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
806 if (psw_error)
807 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
808 "error in _DSW or _PSW evaluation\n"));
809
Alex Chiang0c526d92009-05-14 08:31:37 -0600810end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (ACPI_FAILURE(status))
812 device->flags.wake_capable = 0;
813 return 0;
814}
815
Zhang Rui9e89dde2006-12-07 20:56:16 +0800816static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800818 acpi_status status = 0;
819 acpi_handle handle = NULL;
820 u32 i = 0;
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800824 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800826 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800828 device->power.flags.explicit_get = 1;
829 status = acpi_get_handle(device->handle, "_IRC", &handle);
830 if (ACPI_SUCCESS(status))
831 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800834 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800836 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
837 struct acpi_device_power_state *ps = &device->power.states[i];
838 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Zhang Rui9e89dde2006-12-07 20:56:16 +0800840 /* Evaluate "_PRx" to se if power resources are referenced */
841 acpi_evaluate_reference(device->handle, object_name, NULL,
842 &ps->resources);
843 if (ps->resources.count) {
844 device->power.flags.power_resources = 1;
845 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Zhang Rui9e89dde2006-12-07 20:56:16 +0800848 /* Evaluate "_PSx" to see if we can do explicit sets */
849 object_name[2] = 'S';
850 status = acpi_get_handle(device->handle, object_name, &handle);
851 if (ACPI_SUCCESS(status)) {
852 ps->flags.explicit_set = 1;
853 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800855
856 /* State is valid if we have some power control */
857 if (ps->resources.count || ps->flags.explicit_set)
858 ps->flags.valid = 1;
859
860 ps->power = -1; /* Unknown - driver assigned */
861 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Zhang Rui9e89dde2006-12-07 20:56:16 +0800864 /* Set defaults for D0 and D3 states (always valid) */
865 device->power.states[ACPI_STATE_D0].flags.valid = 1;
866 device->power.states[ACPI_STATE_D0].power = 100;
867 device->power.states[ACPI_STATE_D3].flags.valid = 1;
868 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Zhang Rui9e89dde2006-12-07 20:56:16 +0800870 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Zhang Rui9e89dde2006-12-07 20:56:16 +0800872 device->power.state = ACPI_STATE_UNKNOWN;
Zhao Yakuia51e1452008-08-11 14:55:05 +0800873 acpi_bus_get_power(device->handle, &(device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 return 0;
876}
877
Len Brown4be44fc2005-08-05 00:44:28 -0400878static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Len Brown4be44fc2005-08-05 00:44:28 -0400880 acpi_status status = AE_OK;
881 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /* Presence of _STA indicates 'dynamic_status' */
885 status = acpi_get_handle(device->handle, "_STA", &temp);
886 if (ACPI_SUCCESS(status))
887 device->flags.dynamic_status = 1;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* Presence of _RMV indicates 'removable' */
890 status = acpi_get_handle(device->handle, "_RMV", &temp);
891 if (ACPI_SUCCESS(status))
892 device->flags.removable = 1;
893
894 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
895 status = acpi_get_handle(device->handle, "_EJD", &temp);
896 if (ACPI_SUCCESS(status))
897 device->flags.ejectable = 1;
898 else {
899 status = acpi_get_handle(device->handle, "_EJ0", &temp);
900 if (ACPI_SUCCESS(status))
901 device->flags.ejectable = 1;
902 }
903
904 /* Presence of _LCK indicates 'lockable' */
905 status = acpi_get_handle(device->handle, "_LCK", &temp);
906 if (ACPI_SUCCESS(status))
907 device->flags.lockable = 1;
908
909 /* Presence of _PS0|_PR0 indicates 'power manageable' */
910 status = acpi_get_handle(device->handle, "_PS0", &temp);
911 if (ACPI_FAILURE(status))
912 status = acpi_get_handle(device->handle, "_PR0", &temp);
913 if (ACPI_SUCCESS(status))
914 device->flags.power_manageable = 1;
915
916 /* Presence of _PRW indicates wake capable */
917 status = acpi_get_handle(device->handle, "_PRW", &temp);
918 if (ACPI_SUCCESS(status))
919 device->flags.wake_capable = 1;
920
Joe Perches3c5f9be42008-02-03 17:06:17 +0200921 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Patrick Mocheld550d982006-06-27 00:41:40 -0400923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +0000926static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Len Brown4be44fc2005-08-05 00:44:28 -0400928 char bus_id[5] = { '?', 0 };
929 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
930 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /*
933 * Bus ID
934 * ------
935 * The device's Bus ID is simply the object name.
936 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
937 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000938 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000940 return;
941 }
942
943 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case ACPI_BUS_TYPE_POWER_BUTTON:
945 strcpy(device->pnp.bus_id, "PWRF");
946 break;
947 case ACPI_BUS_TYPE_SLEEP_BUTTON:
948 strcpy(device->pnp.bus_id, "SLPF");
949 break;
950 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000951 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* Clean up trailing underscores (if any) */
953 for (i = 3; i > 1; i--) {
954 if (bus_id[i] == '_')
955 bus_id[i] = '\0';
956 else
957 break;
958 }
959 strcpy(device->pnp.bus_id, bus_id);
960 break;
961 }
962}
963
Zhang Rui54735262007-01-11 02:09:09 -0500964/*
965 * acpi_bay_match - see if a device is an ejectable driver bay
966 *
967 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
968 * then we can safely call it an ejectable drive bay
969 */
970static int acpi_bay_match(struct acpi_device *device){
971 acpi_status status;
972 acpi_handle handle;
973 acpi_handle tmp;
974 acpi_handle phandle;
975
976 handle = device->handle;
977
978 status = acpi_get_handle(handle, "_EJ0", &tmp);
979 if (ACPI_FAILURE(status))
980 return -ENODEV;
981
982 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
983 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
984 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
985 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
986 return 0;
987
988 if (acpi_get_parent(handle, &phandle))
989 return -ENODEV;
990
991 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
992 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
993 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
994 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
995 return 0;
996
997 return -ENODEV;
998}
999
Frank Seidel3620f2f2007-12-07 13:20:34 +01001000/*
1001 * acpi_dock_match - see if a device has a _DCK method
1002 */
1003static int acpi_dock_match(struct acpi_device *device)
1004{
1005 acpi_handle tmp;
1006 return acpi_get_handle(device->handle, "_DCK", &tmp);
1007}
1008
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001009char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001010{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001011 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001012
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001013 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1014 return hid->id;
1015}
1016EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001017
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001018static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1019{
1020 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001021
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001022 id = kmalloc(sizeof(*id), GFP_KERNEL);
1023 if (!id)
1024 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001025
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001026 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1027 if (!id->id) {
1028 kfree(id);
1029 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001030 }
1031
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001032 strcpy(id->id, dev_id);
1033 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001034}
1035
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001036/*
1037 * Old IBM workstations have a DSDT bug wherein the SMBus object
1038 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1039 * prefix. Work around this.
1040 */
1041static int acpi_ibm_smbus_match(struct acpi_device *device)
1042{
1043 acpi_handle h_dummy;
1044 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1045 int result;
1046
1047 if (!dmi_name_in_vendors("IBM"))
1048 return -ENODEV;
1049
1050 /* Look for SMBS object */
1051 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1052 if (result)
1053 return result;
1054
1055 if (strcmp("SMBS", path.pointer)) {
1056 result = -ENODEV;
1057 goto out;
1058 }
1059
1060 /* Does it have the necessary (but misnamed) methods? */
1061 result = -ENODEV;
1062 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1063 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1064 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1065 result = 0;
1066out:
1067 kfree(path.pointer);
1068 return result;
1069}
1070
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001071static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Len Brown4be44fc2005-08-05 00:44:28 -04001073 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001074 struct acpi_device_info *info;
1075 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001076 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001078 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001080 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001081 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001082 break;
Bjorn Helgaas78b8e142009-09-21 13:35:04 -06001083 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1084 /* \_SB_, the only root-level namespace device */
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001085 acpi_add_id(device, ACPI_BUS_HID);
Bjorn Helgaas78b8e142009-09-21 13:35:04 -06001086 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1087 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1088 break;
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001089 }
1090
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001091 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001093 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return;
1095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001098 acpi_add_id(device, info->hardware_id.string);
1099 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001100 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001101 for (i = 0; i < cid_list->count; i++)
1102 acpi_add_id(device, cid_list->ids[i].string);
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (info->valid & ACPI_VALID_ADR) {
1105 device->pnp.bus_address = info->address;
1106 device->flags.bus_address = 1;
1107 }
Zhang Ruiae843332006-12-07 20:57:10 +08001108
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001109 kfree(info);
1110
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001111 /*
1112 * Some devices don't reliably have _HIDs & _CIDs, so add
1113 * synthetic HIDs to make sure drivers can find them.
1114 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001115 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001116 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001117 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001118 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001119 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001120 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001121 else if (!acpi_ibm_smbus_match(device))
1122 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Zhang Rui54735262007-01-11 02:09:09 -05001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001126 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 break;
1128 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001129 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001132 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
1134 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001135 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
1137 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001138 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
1140 }
1141
Bjorn Helgaasb1fbfb22009-09-21 13:35:14 -06001142 /*
1143 * We build acpi_devices for some objects that don't have _HID or _CID,
1144 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1145 * but we do use them indirectly by traversing the acpi_device tree.
1146 * This generic ID isn't useful for driver binding, but it provides
1147 * the useful property that "every acpi_device has an ID."
1148 */
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001149 if (list_empty(&device->pnp.ids))
1150 acpi_add_id(device, "device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001153static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001155 acpi_status status;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Context
1159 * -------
1160 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001161 * resolutions from handle->device very efficient. Fixed hardware
1162 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001164 if (!device->handle)
1165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001167 status = acpi_attach_data(device->handle,
1168 acpi_bus_data_handler, device);
1169 if (ACPI_SUCCESS(status))
1170 return 0;
1171
1172 printk(KERN_ERR PREFIX "Error attaching device data\n");
1173 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Len Brown4be44fc2005-08-05 00:44:28 -04001176static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001179 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Li Shaohua96333572006-12-07 20:56:46 +08001181 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001182 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Rui Zhang2786f6e2006-12-21 02:21:13 -05001187 /*
1188 * unbind _ADR-Based Devices when hot removal
1189 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (dev->flags.bus_address) {
1191 if ((dev->parent) && (dev->parent->ops.unbind))
1192 dev->parent->ops.unbind(dev);
1193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1195
Patrick Mocheld550d982006-06-27 00:41:40 -04001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001199static int acpi_add_single_object(struct acpi_device **child,
1200 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001201 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001202 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001204 int result;
1205 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001206 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Burman Yan36bcbec2006-12-19 12:56:11 -08001208 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001210 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001211 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001214 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001215 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001217 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001218 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001219 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001220
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001221 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /*
1224 * Flags
1225 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001226 * Note that we only look for object handles -- cannot evaluate objects
1227 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 */
1229 result = acpi_bus_get_flags(device);
1230 if (result)
1231 goto end;
1232
1233 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * Initialize Device
1235 * -----------------
1236 * TBD: Synch with Core's enumeration/initialization process.
1237 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001238 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * Power Management
1242 * ----------------
1243 */
1244 if (device->flags.power_manageable) {
1245 result = acpi_bus_get_power_flags(device);
1246 if (result)
1247 goto end;
1248 }
1249
Len Brown4be44fc2005-08-05 00:44:28 -04001250 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 * Wakeup device management
1252 *-----------------------
1253 */
1254 if (device->flags.wake_capable) {
1255 result = acpi_bus_get_wakeup_device_flags(device);
1256 if (result)
1257 goto end;
1258 }
1259
1260 /*
1261 * Performance Management
1262 * ----------------------
1263 */
1264 if (device->flags.performance_manageable) {
1265 result = acpi_bus_get_perf_flags(device);
1266 if (result)
1267 goto end;
1268 }
1269
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001270 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001271 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001273 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001276 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 */
1278 if (device->flags.bus_address) {
1279 if (device->parent && device->parent->ops.bind)
1280 device->parent->ops.bind(device);
1281 }
1282
Alex Chiang0c526d92009-05-14 08:31:37 -06001283end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001284 if (!result) {
1285 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1287 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1288 (char *) buffer.pointer,
1289 device->parent ? dev_name(&device->parent->dev) :
1290 "(null)"));
1291 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001293 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001294 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Patrick Mocheld550d982006-06-27 00:41:40 -04001296 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001299#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1300 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1301
1302static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1303 unsigned long long *sta)
1304{
1305 acpi_status status;
1306 acpi_object_type acpi_type;
1307
1308 status = acpi_get_type(handle, &acpi_type);
1309 if (ACPI_FAILURE(status))
1310 return -ENODEV;
1311
1312 switch (acpi_type) {
1313 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1314 case ACPI_TYPE_DEVICE:
1315 *type = ACPI_BUS_TYPE_DEVICE;
1316 status = acpi_bus_get_status_handle(handle, sta);
1317 if (ACPI_FAILURE(status))
1318 return -ENODEV;
1319 break;
1320 case ACPI_TYPE_PROCESSOR:
1321 *type = ACPI_BUS_TYPE_PROCESSOR;
1322 status = acpi_bus_get_status_handle(handle, sta);
1323 if (ACPI_FAILURE(status))
1324 return -ENODEV;
1325 break;
1326 case ACPI_TYPE_THERMAL:
1327 *type = ACPI_BUS_TYPE_THERMAL;
1328 *sta = ACPI_STA_DEFAULT;
1329 break;
1330 case ACPI_TYPE_POWER:
1331 *type = ACPI_BUS_TYPE_POWER;
1332 *sta = ACPI_STA_DEFAULT;
1333 break;
1334 default:
1335 return -ENODEV;
1336 }
1337
1338 return 0;
1339}
1340
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001341static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1342 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001344 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001345 int type;
1346 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001347 struct acpi_device *device;
1348 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001349 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001350
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001351 result = acpi_bus_type_and_status(handle, &type, &sta);
1352 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001353 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001355 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1356 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1357 return AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001359 /*
1360 * We may already have an acpi_device from a previous enumeration. If
1361 * so, we needn't add it again, but we may still have to start it.
1362 */
1363 device = NULL;
1364 acpi_bus_get_device(handle, &device);
1365 if (ops->acpi_op_add && !device)
1366 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001367
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001368 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001369 return AE_CTRL_DEPTH;
1370
1371 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1372 status = acpi_start_single_object(device);
1373 if (ACPI_FAILURE(status))
1374 return AE_CTRL_DEPTH;
1375 }
1376
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001377 if (!*return_value)
1378 *return_value = device;
1379 return AE_OK;
1380}
1381
1382static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1383 struct acpi_device **child)
1384{
1385 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001386 void *device = NULL;
1387
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001388 status = acpi_bus_check_add(handle, 0, ops, &device);
1389 if (ACPI_SUCCESS(status))
1390 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001391 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001392
1393 if (child)
1394 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001395
1396 if (device)
1397 return 0;
1398 else
1399 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Thomas Renninger77796882010-01-29 17:48:52 +01001402/*
1403 * acpi_bus_add and acpi_bus_start
1404 *
1405 * scan a given ACPI tree and (probably recently hot-plugged)
1406 * create and add or starts found devices.
1407 *
1408 * If no devices were found -ENODEV is returned which does not
1409 * mean that this is a real error, there just have been no suitable
1410 * ACPI objects in the table trunk from which the kernel could create
1411 * a device and add/start an appropriate driver.
1412 */
1413
Rajesh Shah3fb02732005-04-28 00:25:52 -07001414int
Len Brown4be44fc2005-08-05 00:44:28 -04001415acpi_bus_add(struct acpi_device **child,
1416 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001417{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001418 struct acpi_bus_ops ops;
1419
Li Shaohuac4168bf2006-12-07 20:56:41 +08001420 memset(&ops, 0, sizeof(ops));
1421 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001422
Thomas Renninger77796882010-01-29 17:48:52 +01001423 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001424}
1425EXPORT_SYMBOL(acpi_bus_add);
1426
Len Brown4be44fc2005-08-05 00:44:28 -04001427int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001428{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001429 struct acpi_bus_ops ops;
1430
Thomas Renningerd2f66502010-01-29 17:48:51 +01001431 if (!device)
1432 return -EINVAL;
1433
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001434 memset(&ops, 0, sizeof(ops));
1435 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001436
Thomas Renninger77796882010-01-29 17:48:52 +01001437 return acpi_bus_scan(device->handle, &ops, NULL);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001438}
1439EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Kristen Accardiceaba662006-02-23 17:56:01 -08001441int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Len Brown4be44fc2005-08-05 00:44:28 -04001443 acpi_status status;
1444 struct acpi_device *parent, *child;
1445 acpi_handle phandle, chandle;
1446 acpi_object_type type;
1447 u32 level = 1;
1448 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Len Brown4be44fc2005-08-05 00:44:28 -04001450 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 phandle = start->handle;
1452 child = chandle = NULL;
1453
1454 while ((level > 0) && parent && (!err)) {
1455 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001456 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /*
1459 * If this scope is exhausted then move our way back up.
1460 */
1461 if (ACPI_FAILURE(status)) {
1462 level--;
1463 chandle = phandle;
1464 acpi_get_parent(phandle, &phandle);
1465 child = parent;
1466 parent = parent->parent;
1467
1468 if (level == 0)
1469 err = acpi_bus_remove(child, rmdevice);
1470 else
1471 err = acpi_bus_remove(child, 1);
1472
1473 continue;
1474 }
1475
1476 status = acpi_get_type(chandle, &type);
1477 if (ACPI_FAILURE(status)) {
1478 continue;
1479 }
1480 /*
1481 * If there is a device corresponding to chandle then
1482 * parse it (depth-first).
1483 */
1484 if (acpi_bus_get_device(chandle, &child) == 0) {
1485 level++;
1486 phandle = chandle;
1487 chandle = NULL;
1488 parent = child;
1489 }
1490 continue;
1491 }
1492 return err;
1493}
Kristen Accardiceaba662006-02-23 17:56:01 -08001494EXPORT_SYMBOL_GPL(acpi_bus_trim);
1495
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001496static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Len Brown4be44fc2005-08-05 00:44:28 -04001498 int result = 0;
1499 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001500 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Li Shaohuac4168bf2006-12-07 20:56:41 +08001502 memset(&ops, 0, sizeof(ops));
1503 ops.acpi_op_add = 1;
1504 ops.acpi_op_start = 1;
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
1507 * Enumerate all fixed-feature devices.
1508 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001509 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001510 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001511 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001512 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001513 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001516 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001517 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001518 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001519 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001520 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Patrick Mocheld550d982006-06-27 00:41:40 -04001523 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Bjorn Helgaase747f272009-03-24 16:49:43 -06001526int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
1528 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001529 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Li Shaohuac4168bf2006-12-07 20:56:41 +08001531 memset(&ops, 0, sizeof(ops));
1532 ops.acpi_op_add = 1;
1533 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Patrick Mochel5b327262006-05-10 10:33:00 -04001535 result = bus_register(&acpi_bus_type);
1536 if (result) {
1537 /* We don't want to quit even if we failed to add suspend/resume */
1538 printk(KERN_ERR PREFIX "Could not register bus type\n");
1539 }
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * Enumerate devices in the ACPI namespace.
1543 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001544 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001545
Li Shaohuac4168bf2006-12-07 20:56:41 +08001546 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001547 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 if (result)
1550 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1551
Patrick Mocheld550d982006-06-27 00:41:40 -04001552 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}