blob: 395ae129aae0c4f91d59bc81848b3bad1f9f403a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04007#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -04009#include <linux/signal.h>
10#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060014#include "internal.h"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050017ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040019extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020022#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define ACPI_BUS_DEVICE_NAME "System Bus"
24
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +000025#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080028static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080029DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030LIST_HEAD(acpi_wakeup_device_list);
31
Zhang Ruie49bd2d2006-12-08 17:23:43 +080032struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080033 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080034 unsigned int instance_no;
35 struct list_head node;
36};
Thomas Renninger29b71a12007-07-23 14:43:51 +020037
38/*
39 * Creates hid/cid(s) string needed for modalias and uevent
40 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41 * char *modalias: "acpi:IBM0001:ACPI0001"
42*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020043static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44 int size)
45{
Thomas Renninger29b71a12007-07-23 14:43:51 +020046 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080047 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060048 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020049
Bjorn Helgaasb2972f82009-09-21 13:35:24 -060050 if (!acpi_dev->flags.hardware_id)
Thomas Renninger29b71a12007-07-23 14:43:51 +020051 return -ENODEV;
52
Zhang Rui5c9fcb52008-03-20 16:40:32 +080053 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020054 size -= len;
55
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060056 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
57 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080058 if (count < 0 || count >= size)
59 return -EINVAL;
60 len += count;
61 size -= count;
62 }
63
Thomas Renninger29b71a12007-07-23 14:43:51 +020064 modalias[len] = '\0';
65 return len;
66}
67
68static ssize_t
69acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
70 struct acpi_device *acpi_dev = to_acpi_device(dev);
71 int len;
72
73 /* Device has no HID and no CID or string is >1024 */
74 len = create_modalias(acpi_dev, buf, 1024);
75 if (len <= 0)
76 return 0;
77 buf[len++] = '\n';
78 return len;
79}
80static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
81
Zhang Ruic8d72a52009-06-22 11:31:16 +080082static void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Zhang Rui26d46862008-04-29 02:35:48 -040084 struct acpi_device *device;
85 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct acpi_object_list arg_list;
87 union acpi_object arg;
88 acpi_status status = AE_OK;
89
Zhang Rui26d46862008-04-29 02:35:48 -040090 if (acpi_bus_get_device(handle, &device))
Zhang Ruic8d72a52009-06-22 11:31:16 +080091 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Zhang Rui26d46862008-04-29 02:35:48 -040093 if (!device)
Zhang Ruic8d72a52009-06-22 11:31:16 +080094 return;
Zhang Rui26d46862008-04-29 02:35:48 -040095
96 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +010097 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -040098
99 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800100 printk(KERN_ERR PREFIX
101 "Removing device failed\n");
Zhang Ruic8d72a52009-06-22 11:31:16 +0800102 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400103 }
104
105 /* power off device */
106 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
107 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800108 printk(KERN_WARNING PREFIX
109 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400110
111 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 arg_list.count = 1;
113 arg_list.pointer = &arg;
114 arg.type = ACPI_TYPE_INTEGER;
115 arg.integer.value = 0;
116 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
117 }
118
119 arg_list.count = 1;
120 arg_list.pointer = &arg;
121 arg.type = ACPI_TYPE_INTEGER;
122 arg.integer.value = 1;
123
124 /*
125 * TBD: _EJD support.
126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400128 if (ACPI_FAILURE(status))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800129 printk(KERN_WARNING PREFIX
130 "Eject device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Zhang Ruic8d72a52009-06-22 11:31:16 +0800132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800136acpi_eject_store(struct device *d, struct device_attribute *attr,
137 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Len Brown4be44fc2005-08-05 00:44:28 -0400139 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400140 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400141 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800142 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 if ((!count) || (buf[0] != '1')) {
145 return -EINVAL;
146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800148 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ret = -ENODEV;
150 goto err;
151 }
152#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800153 status = acpi_get_type(acpi_device->handle, &type);
154 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 ret = -ENODEV;
156 goto err;
157 }
158
Zhang Ruic8d72a52009-06-22 11:31:16 +0800159 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
Alok N Kataria74523c92008-06-13 12:54:24 -0400160err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800164static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800166static ssize_t
167acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
168 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600170 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800171}
172static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
173
174static ssize_t
175acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
176 struct acpi_device *acpi_dev = to_acpi_device(dev);
177 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
178 int result;
179
180 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600181 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800182 goto end;
183
184 result = sprintf(buf, "%s\n", (char*)path.pointer);
185 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600186end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800187 return result;
188}
189static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
190
191static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800193 acpi_status status;
194 acpi_handle temp;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800195 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800197 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800198 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800199 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600200 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800201 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600202 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800203 goto end;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Alex Chiang0c526d92009-05-14 08:31:37 -0600206 if (dev->flags.hardware_id) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800207 result = device_create_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600208 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800209 goto end;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Bjorn Helgaasb2972f82009-09-21 13:35:24 -0600212 if (dev->flags.hardware_id) {
Thomas Renninger29b71a12007-07-23 14:43:51 +0200213 result = device_create_file(&dev->dev, &dev_attr_modalias);
Alex Chiang0c526d92009-05-14 08:31:37 -0600214 if (result)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200215 goto end;
216 }
217
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800218 /*
219 * If device has _EJ0, 'eject' file is created that is used to trigger
220 * hot-removal function from userland.
221 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800222 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
223 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800224 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600225end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800226 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800229static void acpi_device_remove_files(struct acpi_device *dev)
230{
231 acpi_status status;
232 acpi_handle temp;
233
234 /*
235 * If device has _EJ0, 'eject' file is created that is used to trigger
236 * hot-removal function from userland.
237 */
238 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
239 if (ACPI_SUCCESS(status))
240 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800241
Bjorn Helgaasb2972f82009-09-21 13:35:24 -0600242 if (dev->flags.hardware_id)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200243 device_remove_file(&dev->dev, &dev_attr_modalias);
244
Alex Chiang0c526d92009-05-14 08:31:37 -0600245 if (dev->flags.hardware_id)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800246 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600247 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800248 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800251 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200253
254int acpi_match_device_ids(struct acpi_device *device,
255 const struct acpi_device_id *ids)
256{
257 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600258 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200259
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800260 /*
261 * If the device is not present, it is unnecessary to load device
262 * driver for it.
263 */
264 if (!device->status.present)
265 return -ENODEV;
266
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600267 for (id = ids; id->id[0]; id++)
268 list_for_each_entry(hwid, &device->pnp.ids, list)
269 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200270 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200271
272 return -ENOENT;
273}
274EXPORT_SYMBOL(acpi_match_device_ids);
275
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600276static void acpi_free_ids(struct acpi_device *device)
277{
278 struct acpi_hardware_id *id, *tmp;
279
280 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
281 kfree(id->id);
282 kfree(id);
283 }
284}
285
Patrick Mochel1890a972006-12-07 20:56:31 +0800286static void acpi_device_release(struct device *dev)
287{
288 struct acpi_device *acpi_dev = to_acpi_device(dev);
289
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600290 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800291 kfree(acpi_dev);
292}
293
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800294static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800295{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800296 struct acpi_device *acpi_dev = to_acpi_device(dev);
297 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800298
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800299 if (acpi_drv && acpi_drv->ops.suspend)
300 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302}
303
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800304static int acpi_device_resume(struct device *dev)
305{
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 struct acpi_driver *acpi_drv = acpi_dev->driver;
308
309 if (acpi_drv && acpi_drv->ops.resume)
310 return acpi_drv->ops.resume(acpi_dev);
311 return 0;
312}
313
314static int acpi_bus_match(struct device *dev, struct device_driver *drv)
315{
316 struct acpi_device *acpi_dev = to_acpi_device(dev);
317 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
318
Thomas Renninger29b71a12007-07-23 14:43:51 +0200319 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800320}
321
Kay Sievers7eff2e72007-08-14 15:15:12 +0200322static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800323{
324 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200325 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800326
Kay Sievers7eff2e72007-08-14 15:15:12 +0200327 if (add_uevent_var(env, "MODALIAS="))
328 return -ENOMEM;
329 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
330 sizeof(env->buf) - env->buflen);
331 if (len >= (sizeof(env->buf) - env->buflen))
332 return -ENOMEM;
333 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335}
336
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000337static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
338{
339 struct acpi_device *device = data;
340
341 device->driver->ops.notify(device, event);
342}
343
344static acpi_status acpi_device_notify_fixed(void *data)
345{
346 struct acpi_device *device = data;
347
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000348 /* Fixed hardware devices have no handles */
349 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000350 return AE_OK;
351}
352
353static int acpi_device_install_notify_handler(struct acpi_device *device)
354{
355 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000356
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000357 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000358 status =
359 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
360 acpi_device_notify_fixed,
361 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000362 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000363 status =
364 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
365 acpi_device_notify_fixed,
366 device);
367 else
368 status = acpi_install_notify_handler(device->handle,
369 ACPI_DEVICE_NOTIFY,
370 acpi_device_notify,
371 device);
372
373 if (ACPI_FAILURE(status))
374 return -EINVAL;
375 return 0;
376}
377
378static void acpi_device_remove_notify_handler(struct acpi_device *device)
379{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000380 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000381 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
382 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000383 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000384 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
385 acpi_device_notify_fixed);
386 else
387 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
388 acpi_device_notify);
389}
390
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800391static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
392static int acpi_start_single_object(struct acpi_device *);
393static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800394{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800395 struct acpi_device *acpi_dev = to_acpi_device(dev);
396 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
397 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800399 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
400 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800401 if (acpi_dev->bus_ops.acpi_op_start)
402 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000403
404 if (acpi_drv->ops.notify) {
405 ret = acpi_device_install_notify_handler(acpi_dev);
406 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000407 if (acpi_drv->ops.remove)
408 acpi_drv->ops.remove(acpi_dev,
409 acpi_dev->removal_type);
410 return ret;
411 }
412 }
413
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800414 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
415 "Found driver [%s] for device [%s]\n",
416 acpi_drv->name, acpi_dev->pnp.bus_id));
417 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800418 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800419 return ret;
420}
421
422static int acpi_device_remove(struct device * dev)
423{
424 struct acpi_device *acpi_dev = to_acpi_device(dev);
425 struct acpi_driver *acpi_drv = acpi_dev->driver;
426
427 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000428 if (acpi_drv->ops.notify)
429 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800430 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800431 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800432 }
433 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700434 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800435
436 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800437 return 0;
438}
439
David Brownell55955aa2007-05-08 00:28:35 -0700440struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800441 .name = "acpi",
442 .suspend = acpi_device_suspend,
443 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800444 .match = acpi_bus_match,
445 .probe = acpi_device_probe,
446 .remove = acpi_device_remove,
447 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448};
449
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000450static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800452 int result;
453 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
454 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * Linkage
458 * -------
459 * Link this device to its parent and siblings.
460 */
461 INIT_LIST_HEAD(&device->children);
462 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 INIT_LIST_HEAD(&device->wakeup_list);
464
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800465 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
466 if (!new_bus_id) {
467 printk(KERN_ERR PREFIX "Memory allocation error\n");
468 return -ENOMEM;
469 }
470
Shaohua Li90905892009-04-07 10:24:29 +0800471 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800472 /*
473 * Find suitable bus_id and instance number in acpi_bus_id_list
474 * If failed, create one and link it into acpi_bus_id_list
475 */
476 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600477 if (!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id ? acpi_device_hid(device) : "device")) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800478 acpi_device_bus_id->instance_no ++;
479 found = 1;
480 kfree(new_bus_id);
481 break;
482 }
483 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600484 if (!found) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800485 acpi_device_bus_id = new_bus_id;
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600486 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? acpi_device_hid(device) : "device");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800487 acpi_device_bus_id->instance_no = 0;
488 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
489 }
Kay Sievers07944692008-10-30 01:18:59 +0100490 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 +0800491
Len Brown33b57152008-12-15 22:09:26 -0500492 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (device->wakeup.flags.valid)
496 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800497 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Patrick Mochel1890a972006-12-07 20:56:31 +0800499 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000500 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800501 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800502 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600503 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600504 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600505 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800506 goto end;
507 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800508
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800509 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600510 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100511 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
512 dev_name(&device->dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800513
Li Shaohua96333572006-12-07 20:56:46 +0800514 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800515 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600516end:
Shaohua Li90905892009-04-07 10:24:29 +0800517 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500518 if (device->parent)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800519 list_del(&device->node);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800520 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800521 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800522 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525static void acpi_device_unregister(struct acpi_device *device, int type)
526{
Shaohua Li90905892009-04-07 10:24:29 +0800527 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500528 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800532 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800535
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800536 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800537 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Zhang Rui9e89dde2006-12-07 20:56:16 +0800540/* --------------------------------------------------------------------------
541 Driver Management
542 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500544 * acpi_bus_driver_init - add a device to a driver
545 * @device: the device to add and initialize
546 * @driver: driver for the device
547 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600548 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800549 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 */
551static int
Len Brown4be44fc2005-08-05 00:44:28 -0400552acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Len Brown4be44fc2005-08-05 00:44:28 -0400554 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 result = driver->ops.add(device);
563 if (result) {
564 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700565 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400566 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 device->driver = driver;
570
571 /*
572 * TBD - Configuration Management: Assign resources to device based
573 * upon possible configuration and currently allocated resources.
574 */
575
Len Brown4be44fc2005-08-05 00:44:28 -0400576 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
577 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400578 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700579}
580
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400581static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700582{
583 int result = 0;
584 struct acpi_driver *driver;
585
Rajesh Shah3fb02732005-04-28 00:25:52 -0700586
587 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400588 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (driver->ops.start) {
591 result = driver->ops.start(device);
592 if (result && driver->ops.remove)
593 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
Patrick Mocheld550d982006-06-27 00:41:40 -0400596 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500600 * acpi_bus_register_driver - register a driver with the ACPI bus
601 * @driver: driver being registered
602 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500604 * devices that match the driver's criteria and binds. Returns zero for
605 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Len Brown4be44fc2005-08-05 00:44:28 -0400607int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Patrick Mochel1890a972006-12-07 20:56:31 +0800609 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400612 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800613 driver->drv.name = driver->name;
614 driver->drv.bus = &acpi_bus_type;
615 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Patrick Mochel1890a972006-12-07 20:56:31 +0800617 ret = driver_register(&driver->drv);
618 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Len Brown4be44fc2005-08-05 00:44:28 -0400621EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500624 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
625 * @driver: driver to unregister
626 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 * Unregisters a driver with the ACPI bus. Searches the namespace for all
628 * devices that match the driver's criteria and unbinds.
629 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400630void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Patrick Mochel1890a972006-12-07 20:56:31 +0800632 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Len Brown4be44fc2005-08-05 00:44:28 -0400634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635EXPORT_SYMBOL(acpi_bus_unregister_driver);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/* --------------------------------------------------------------------------
638 Device Enumeration
639 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000640static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
641{
642 acpi_status status;
643 int ret;
644 struct acpi_device *device;
645
646 /*
647 * Fixed hardware devices do not appear in the namespace and do not
648 * have handles, but we fabricate acpi_devices for them, so we have
649 * to deal with them specially.
650 */
651 if (handle == NULL)
652 return acpi_root;
653
654 do {
655 status = acpi_get_parent(handle, &handle);
656 if (status == AE_NULL_ENTRY)
657 return NULL;
658 if (ACPI_FAILURE(status))
659 return acpi_root;
660
661 ret = acpi_bus_get_device(handle, &device);
662 if (ret == 0)
663 return device;
664 } while (1);
665}
666
Len Brownc8f7a622006-07-09 17:22:28 -0400667acpi_status
668acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
669{
670 acpi_status status;
671 acpi_handle tmp;
672 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
673 union acpi_object *obj;
674
675 status = acpi_get_handle(handle, "_EJD", &tmp);
676 if (ACPI_FAILURE(status))
677 return status;
678
679 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
680 if (ACPI_SUCCESS(status)) {
681 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100682 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
683 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400684 kfree(buffer.pointer);
685 }
686 return status;
687}
688EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
689
Bob Moore8e4319c2009-06-29 13:43:27 +0800690void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
692
693 /* TBD */
694
695 return;
696}
697
Zhang Rui9e89dde2006-12-07 20:56:16 +0800698static int acpi_bus_get_perf_flags(struct acpi_device *device)
699{
700 device->performance.state = ACPI_STATE_UNKNOWN;
701 return 0;
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static acpi_status
705acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
706 union acpi_object *package)
707{
708 int i = 0;
709 union acpi_object *element = NULL;
710
711 if (!device || !package || (package->package.count < 2))
712 return AE_BAD_PARAMETER;
713
714 element = &(package->package.elements[0]);
715 if (!element)
716 return AE_BAD_PARAMETER;
717 if (element->type == ACPI_TYPE_PACKAGE) {
718 if ((element->package.count < 2) ||
719 (element->package.elements[0].type !=
720 ACPI_TYPE_LOCAL_REFERENCE)
721 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
722 return AE_BAD_DATA;
723 device->wakeup.gpe_device =
724 element->package.elements[0].reference.handle;
725 device->wakeup.gpe_number =
726 (u32) element->package.elements[1].integer.value;
727 } else if (element->type == ACPI_TYPE_INTEGER) {
728 device->wakeup.gpe_number = element->integer.value;
729 } else
730 return AE_BAD_DATA;
731
732 element = &(package->package.elements[1]);
733 if (element->type != ACPI_TYPE_INTEGER) {
734 return AE_BAD_DATA;
735 }
736 device->wakeup.sleep_state = element->integer.value;
737
738 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
739 return AE_NO_MEMORY;
740 }
741 device->wakeup.resources.count = package->package.count - 2;
742 for (i = 0; i < device->wakeup.resources.count; i++) {
743 element = &(package->package.elements[i + 2]);
Bob Moorecd0b2242008-04-10 19:06:43 +0400744 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 device->wakeup.resources.handles[i] = element->reference.handle;
748 }
749
750 return AE_OK;
751}
752
753static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
754{
755 acpi_status status = 0;
756 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
757 union acpi_object *package = NULL;
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200758 int psw_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Thomas Renninger29b71a12007-07-23 14:43:51 +0200760 struct acpi_device_id button_device_ids[] = {
761 {"PNP0C0D", 0},
762 {"PNP0C0C", 0},
763 {"PNP0C0E", 0},
764 {"", 0},
765 };
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* _PRW */
768 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
769 if (ACPI_FAILURE(status)) {
770 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
771 goto end;
772 }
773
774 package = (union acpi_object *)buffer.pointer;
775 status = acpi_bus_extract_wakeup_device_power_package(device, package);
776 if (ACPI_FAILURE(status)) {
777 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
778 goto end;
779 }
780
781 kfree(buffer.pointer);
782
783 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200784 device->wakeup.prepare_count = 0;
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800785 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
786 * system for the ACPI device with the _PRW object.
787 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
788 * So it is necessary to call _DSW object first. Only when it is not
789 * present will the _PSW object used.
790 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200791 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
792 if (psw_error)
793 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
794 "error in _DSW or _PSW evaluation\n"));
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* Power button, Lid switch always enable wakeup */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200797 if (!acpi_match_device_ids(device, button_device_ids))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 device->wakeup.flags.run_wake = 1;
799
Alex Chiang0c526d92009-05-14 08:31:37 -0600800end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (ACPI_FAILURE(status))
802 device->flags.wake_capable = 0;
803 return 0;
804}
805
Zhang Rui9e89dde2006-12-07 20:56:16 +0800806static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800808 acpi_status status = 0;
809 acpi_handle handle = NULL;
810 u32 i = 0;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800814 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800816 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800818 device->power.flags.explicit_get = 1;
819 status = acpi_get_handle(device->handle, "_IRC", &handle);
820 if (ACPI_SUCCESS(status))
821 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800824 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800826 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
827 struct acpi_device_power_state *ps = &device->power.states[i];
828 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Zhang Rui9e89dde2006-12-07 20:56:16 +0800830 /* Evaluate "_PRx" to se if power resources are referenced */
831 acpi_evaluate_reference(device->handle, object_name, NULL,
832 &ps->resources);
833 if (ps->resources.count) {
834 device->power.flags.power_resources = 1;
835 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Zhang Rui9e89dde2006-12-07 20:56:16 +0800838 /* Evaluate "_PSx" to see if we can do explicit sets */
839 object_name[2] = 'S';
840 status = acpi_get_handle(device->handle, object_name, &handle);
841 if (ACPI_SUCCESS(status)) {
842 ps->flags.explicit_set = 1;
843 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800845
846 /* State is valid if we have some power control */
847 if (ps->resources.count || ps->flags.explicit_set)
848 ps->flags.valid = 1;
849
850 ps->power = -1; /* Unknown - driver assigned */
851 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Zhang Rui9e89dde2006-12-07 20:56:16 +0800854 /* Set defaults for D0 and D3 states (always valid) */
855 device->power.states[ACPI_STATE_D0].flags.valid = 1;
856 device->power.states[ACPI_STATE_D0].power = 100;
857 device->power.states[ACPI_STATE_D3].flags.valid = 1;
858 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Zhang Rui9e89dde2006-12-07 20:56:16 +0800860 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Zhang Rui9e89dde2006-12-07 20:56:16 +0800862 device->power.state = ACPI_STATE_UNKNOWN;
Zhao Yakuia51e1452008-08-11 14:55:05 +0800863 acpi_bus_get_power(device->handle, &(device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 return 0;
866}
867
Len Brown4be44fc2005-08-05 00:44:28 -0400868static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Len Brown4be44fc2005-08-05 00:44:28 -0400870 acpi_status status = AE_OK;
871 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* Presence of _STA indicates 'dynamic_status' */
875 status = acpi_get_handle(device->handle, "_STA", &temp);
876 if (ACPI_SUCCESS(status))
877 device->flags.dynamic_status = 1;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Presence of _RMV indicates 'removable' */
880 status = acpi_get_handle(device->handle, "_RMV", &temp);
881 if (ACPI_SUCCESS(status))
882 device->flags.removable = 1;
883
884 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
885 status = acpi_get_handle(device->handle, "_EJD", &temp);
886 if (ACPI_SUCCESS(status))
887 device->flags.ejectable = 1;
888 else {
889 status = acpi_get_handle(device->handle, "_EJ0", &temp);
890 if (ACPI_SUCCESS(status))
891 device->flags.ejectable = 1;
892 }
893
894 /* Presence of _LCK indicates 'lockable' */
895 status = acpi_get_handle(device->handle, "_LCK", &temp);
896 if (ACPI_SUCCESS(status))
897 device->flags.lockable = 1;
898
899 /* Presence of _PS0|_PR0 indicates 'power manageable' */
900 status = acpi_get_handle(device->handle, "_PS0", &temp);
901 if (ACPI_FAILURE(status))
902 status = acpi_get_handle(device->handle, "_PR0", &temp);
903 if (ACPI_SUCCESS(status))
904 device->flags.power_manageable = 1;
905
906 /* Presence of _PRW indicates wake capable */
907 status = acpi_get_handle(device->handle, "_PRW", &temp);
908 if (ACPI_SUCCESS(status))
909 device->flags.wake_capable = 1;
910
Joe Perches3c5f9be2008-02-03 17:06:17 +0200911 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Patrick Mocheld550d982006-06-27 00:41:40 -0400913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +0000916static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Len Brown4be44fc2005-08-05 00:44:28 -0400918 char bus_id[5] = { '?', 0 };
919 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
920 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /*
923 * Bus ID
924 * ------
925 * The device's Bus ID is simply the object name.
926 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
927 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000928 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000930 return;
931 }
932
933 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case ACPI_BUS_TYPE_POWER_BUTTON:
935 strcpy(device->pnp.bus_id, "PWRF");
936 break;
937 case ACPI_BUS_TYPE_SLEEP_BUTTON:
938 strcpy(device->pnp.bus_id, "SLPF");
939 break;
940 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000941 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* Clean up trailing underscores (if any) */
943 for (i = 3; i > 1; i--) {
944 if (bus_id[i] == '_')
945 bus_id[i] = '\0';
946 else
947 break;
948 }
949 strcpy(device->pnp.bus_id, bus_id);
950 break;
951 }
952}
953
Zhang Rui54735262007-01-11 02:09:09 -0500954/*
955 * acpi_bay_match - see if a device is an ejectable driver bay
956 *
957 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
958 * then we can safely call it an ejectable drive bay
959 */
960static int acpi_bay_match(struct acpi_device *device){
961 acpi_status status;
962 acpi_handle handle;
963 acpi_handle tmp;
964 acpi_handle phandle;
965
966 handle = device->handle;
967
968 status = acpi_get_handle(handle, "_EJ0", &tmp);
969 if (ACPI_FAILURE(status))
970 return -ENODEV;
971
972 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
973 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
974 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
975 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
976 return 0;
977
978 if (acpi_get_parent(handle, &phandle))
979 return -ENODEV;
980
981 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
982 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
983 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
984 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
985 return 0;
986
987 return -ENODEV;
988}
989
Frank Seidel3620f2f2007-12-07 13:20:34 +0100990/*
991 * acpi_dock_match - see if a device has a _DCK method
992 */
993static int acpi_dock_match(struct acpi_device *device)
994{
995 acpi_handle tmp;
996 return acpi_get_handle(device->handle, "_DCK", &tmp);
997}
998
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600999char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001000{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001001 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001002
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001003 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1004 return hid->id;
1005}
1006EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001007
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001008static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1009{
1010 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001011
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001012 id = kmalloc(sizeof(*id), GFP_KERNEL);
1013 if (!id)
1014 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001015
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001016 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1017 if (!id->id) {
1018 kfree(id);
1019 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001020 }
1021
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001022 strcpy(id->id, dev_id);
1023 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001024}
1025
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001026static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Bob Moore15b8dd52009-06-29 13:39:29 +08001028 struct acpi_device_info *info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001029 char *hid = NULL;
1030 char *uid = NULL;
Bob Moore15b8dd52009-06-29 13:39:29 +08001031 struct acpica_device_id_list *cid_list = NULL;
1032 char *cid_add = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001033 acpi_status status;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001034 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001036 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001038 if (ACPI_IS_ROOT_DEVICE(device)) {
1039 hid = ACPI_SYSTEM_HID;
1040 break;
Bjorn Helgaas78b8e142009-09-21 13:35:04 -06001041 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1042 /* \_SB_, the only root-level namespace device */
1043 hid = ACPI_BUS_HID;
1044 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1045 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1046 break;
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001047 }
1048
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001049 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001051 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return;
1053 }
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (info->valid & ACPI_VALID_HID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001056 hid = info->hardware_id.string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (info->valid & ACPI_VALID_UID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001058 uid = info->unique_id.string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (info->valid & ACPI_VALID_CID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001060 cid_list = &info->compatible_id_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (info->valid & ACPI_VALID_ADR) {
1062 device->pnp.bus_address = info->address;
1063 device->flags.bus_address = 1;
1064 }
Zhang Ruiae843332006-12-07 20:57:10 +08001065
Frank Seidel3620f2f2007-12-07 13:20:34 +01001066 /* If we have a video/bay/dock device, add our selfdefined
1067 HID to the CID list. Like that the video/bay/dock drivers
1068 will get autoloaded and the device might still match
1069 against another driver.
1070 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001071 if (acpi_is_video_device(device))
Frank Seidel3620f2f2007-12-07 13:20:34 +01001072 cid_add = ACPI_VIDEO_HID;
1073 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1074 cid_add = ACPI_BAY_HID;
1075 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1076 cid_add = ACPI_DOCK_HID;
Zhang Rui54735262007-01-11 02:09:09 -05001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 break;
1079 case ACPI_BUS_TYPE_POWER:
1080 hid = ACPI_POWER_HID;
1081 break;
1082 case ACPI_BUS_TYPE_PROCESSOR:
Myron Stowead93a762008-11-04 14:52:55 -07001083 hid = ACPI_PROCESSOR_OBJECT_HID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 case ACPI_BUS_TYPE_THERMAL:
1086 hid = ACPI_THERMAL_HID;
1087 break;
1088 case ACPI_BUS_TYPE_POWER_BUTTON:
1089 hid = ACPI_BUTTON_HID_POWERF;
1090 break;
1091 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1092 hid = ACPI_BUTTON_HID_SLEEPF;
1093 break;
1094 }
1095
Bjorn Helgaasb1fbfb22009-09-21 13:35:14 -06001096 /*
1097 * We build acpi_devices for some objects that don't have _HID or _CID,
1098 * e.g., PCI bridges and slots. Drivers can't bind to these objects,
1099 * but we do use them indirectly by traversing the acpi_device tree.
1100 * This generic ID isn't useful for driver binding, but it provides
1101 * the useful property that "every acpi_device has an ID."
1102 */
1103 if (!hid && !cid_list && !cid_add)
1104 hid = "device";
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (hid) {
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001107 acpi_add_id(device, hid);
1108 device->flags.hardware_id = 1;
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001109 }
Bob Moore15b8dd52009-06-29 13:39:29 +08001110 if (uid) {
1111 device->pnp.unique_id = ACPI_ALLOCATE_ZEROED(strlen (uid) + 1);
1112 if (device->pnp.unique_id) {
1113 strcpy(device->pnp.unique_id, uid);
1114 device->flags.unique_id = 1;
1115 }
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001116 }
1117 if (!device->flags.unique_id)
1118 device->pnp.unique_id = "";
Bob Moore15b8dd52009-06-29 13:39:29 +08001119
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001120 if (cid_list)
1121 for (i = 0; i < cid_list->count; i++)
1122 acpi_add_id(device, cid_list->ids[i].string);
Bjorn Helgaasb2972f82009-09-21 13:35:24 -06001123 if (cid_add)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001124 acpi_add_id(device, cid_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Bob Moore15b8dd52009-06-29 13:39:29 +08001126 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001129static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001131 acpi_status status;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /*
1134 * Context
1135 * -------
1136 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001137 * resolutions from handle->device very efficient. Fixed hardware
1138 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001140 if (!device->handle)
1141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001143 status = acpi_attach_data(device->handle,
1144 acpi_bus_data_handler, device);
1145 if (ACPI_SUCCESS(status))
1146 return 0;
1147
1148 printk(KERN_ERR PREFIX "Error attaching device data\n");
1149 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Len Brown4be44fc2005-08-05 00:44:28 -04001152static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Li Shaohua96333572006-12-07 20:56:46 +08001157 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001158 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Rui Zhang2786f6e2006-12-21 02:21:13 -05001163 /*
1164 * unbind _ADR-Based Devices when hot removal
1165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (dev->flags.bus_address) {
1167 if ((dev->parent) && (dev->parent->ops.unbind))
1168 dev->parent->ops.unbind(dev);
1169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1171
Patrick Mocheld550d982006-06-27 00:41:40 -04001172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001175static int acpi_add_single_object(struct acpi_device **child,
1176 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001177 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001178 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001180 int result;
1181 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001182 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Burman Yan36bcbec2006-12-19 12:56:11 -08001184 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001186 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001187 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001190 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001191 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001193 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001194 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001195 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001196
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001197 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 /*
1200 * Flags
1201 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001202 * Note that we only look for object handles -- cannot evaluate objects
1203 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
1205 result = acpi_bus_get_flags(device);
1206 if (result)
1207 goto end;
1208
1209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 * Initialize Device
1211 * -----------------
1212 * TBD: Synch with Core's enumeration/initialization process.
1213 */
1214
1215 /*
1216 * Hardware ID, Unique ID, & Bus Address
1217 * -------------------------------------
1218 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001219 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /*
1222 * Power Management
1223 * ----------------
1224 */
1225 if (device->flags.power_manageable) {
1226 result = acpi_bus_get_power_flags(device);
1227 if (result)
1228 goto end;
1229 }
1230
Len Brown4be44fc2005-08-05 00:44:28 -04001231 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * Wakeup device management
1233 *-----------------------
1234 */
1235 if (device->flags.wake_capable) {
1236 result = acpi_bus_get_wakeup_device_flags(device);
1237 if (result)
1238 goto end;
1239 }
1240
1241 /*
1242 * Performance Management
1243 * ----------------------
1244 */
1245 if (device->flags.performance_manageable) {
1246 result = acpi_bus_get_perf_flags(device);
1247 if (result)
1248 goto end;
1249 }
1250
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001251 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001252 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001254 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001257 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
1259 if (device->flags.bus_address) {
1260 if (device->parent && device->parent->ops.bind)
1261 device->parent->ops.bind(device);
1262 }
1263
Alex Chiang0c526d92009-05-14 08:31:37 -06001264end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001265 if (!result) {
1266 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1267 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1268 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1269 (char *) buffer.pointer,
1270 device->parent ? dev_name(&device->parent->dev) :
1271 "(null)"));
1272 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001274 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001275 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Patrick Mocheld550d982006-06-27 00:41:40 -04001277 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001280#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1281 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1282
1283static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1284 unsigned long long *sta)
1285{
1286 acpi_status status;
1287 acpi_object_type acpi_type;
1288
1289 status = acpi_get_type(handle, &acpi_type);
1290 if (ACPI_FAILURE(status))
1291 return -ENODEV;
1292
1293 switch (acpi_type) {
1294 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1295 case ACPI_TYPE_DEVICE:
1296 *type = ACPI_BUS_TYPE_DEVICE;
1297 status = acpi_bus_get_status_handle(handle, sta);
1298 if (ACPI_FAILURE(status))
1299 return -ENODEV;
1300 break;
1301 case ACPI_TYPE_PROCESSOR:
1302 *type = ACPI_BUS_TYPE_PROCESSOR;
1303 status = acpi_bus_get_status_handle(handle, sta);
1304 if (ACPI_FAILURE(status))
1305 return -ENODEV;
1306 break;
1307 case ACPI_TYPE_THERMAL:
1308 *type = ACPI_BUS_TYPE_THERMAL;
1309 *sta = ACPI_STA_DEFAULT;
1310 break;
1311 case ACPI_TYPE_POWER:
1312 *type = ACPI_BUS_TYPE_POWER;
1313 *sta = ACPI_STA_DEFAULT;
1314 break;
1315 default:
1316 return -ENODEV;
1317 }
1318
1319 return 0;
1320}
1321
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001322static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1323 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001325 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001326 int type;
1327 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001328 struct acpi_device *device;
1329 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001330 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001331
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001332 result = acpi_bus_type_and_status(handle, &type, &sta);
1333 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001334 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001336 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1337 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1338 return AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001340 /*
1341 * We may already have an acpi_device from a previous enumeration. If
1342 * so, we needn't add it again, but we may still have to start it.
1343 */
1344 device = NULL;
1345 acpi_bus_get_device(handle, &device);
1346 if (ops->acpi_op_add && !device)
1347 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001348
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001349 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001350 return AE_CTRL_DEPTH;
1351
1352 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1353 status = acpi_start_single_object(device);
1354 if (ACPI_FAILURE(status))
1355 return AE_CTRL_DEPTH;
1356 }
1357
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001358 if (!*return_value)
1359 *return_value = device;
1360 return AE_OK;
1361}
1362
1363static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1364 struct acpi_device **child)
1365{
1366 acpi_status status;
1367 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1368 void *device = NULL;
1369
1370 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1371 printk(KERN_INFO PREFIX "Enumerating devices from [%s]\n",
1372 (char *) buffer.pointer);
1373
1374 status = acpi_bus_check_add(handle, 0, ops, &device);
1375 if (ACPI_SUCCESS(status))
1376 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1377 acpi_bus_check_add, ops, &device);
1378
1379 if (child)
1380 *child = device;
Patrick Mocheld550d982006-06-27 00:41:40 -04001381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Rajesh Shah3fb02732005-04-28 00:25:52 -07001384int
Len Brown4be44fc2005-08-05 00:44:28 -04001385acpi_bus_add(struct acpi_device **child,
1386 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001387{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001388 struct acpi_bus_ops ops;
1389
Li Shaohuac4168bf2006-12-07 20:56:41 +08001390 memset(&ops, 0, sizeof(ops));
1391 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001392
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001393 acpi_bus_scan(handle, &ops, child);
1394 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001395}
1396EXPORT_SYMBOL(acpi_bus_add);
1397
Len Brown4be44fc2005-08-05 00:44:28 -04001398int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001399{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001400 struct acpi_bus_ops ops;
1401
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001402 memset(&ops, 0, sizeof(ops));
1403 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001404
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001405 acpi_bus_scan(device->handle, &ops, NULL);
1406 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001407}
1408EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Kristen Accardiceaba662006-02-23 17:56:01 -08001410int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Len Brown4be44fc2005-08-05 00:44:28 -04001412 acpi_status status;
1413 struct acpi_device *parent, *child;
1414 acpi_handle phandle, chandle;
1415 acpi_object_type type;
1416 u32 level = 1;
1417 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Len Brown4be44fc2005-08-05 00:44:28 -04001419 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 phandle = start->handle;
1421 child = chandle = NULL;
1422
1423 while ((level > 0) && parent && (!err)) {
1424 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001425 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /*
1428 * If this scope is exhausted then move our way back up.
1429 */
1430 if (ACPI_FAILURE(status)) {
1431 level--;
1432 chandle = phandle;
1433 acpi_get_parent(phandle, &phandle);
1434 child = parent;
1435 parent = parent->parent;
1436
1437 if (level == 0)
1438 err = acpi_bus_remove(child, rmdevice);
1439 else
1440 err = acpi_bus_remove(child, 1);
1441
1442 continue;
1443 }
1444
1445 status = acpi_get_type(chandle, &type);
1446 if (ACPI_FAILURE(status)) {
1447 continue;
1448 }
1449 /*
1450 * If there is a device corresponding to chandle then
1451 * parse it (depth-first).
1452 */
1453 if (acpi_bus_get_device(chandle, &child) == 0) {
1454 level++;
1455 phandle = chandle;
1456 chandle = NULL;
1457 parent = child;
1458 }
1459 continue;
1460 }
1461 return err;
1462}
Kristen Accardiceaba662006-02-23 17:56:01 -08001463EXPORT_SYMBOL_GPL(acpi_bus_trim);
1464
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001465static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Len Brown4be44fc2005-08-05 00:44:28 -04001467 int result = 0;
1468 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001469 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Li Shaohuac4168bf2006-12-07 20:56:41 +08001471 memset(&ops, 0, sizeof(ops));
1472 ops.acpi_op_add = 1;
1473 ops.acpi_op_start = 1;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /*
1476 * Enumerate all fixed-feature devices.
1477 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001478 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001479 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001480 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001481 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001482 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001485 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001486 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001487 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001488 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001489 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Patrick Mocheld550d982006-06-27 00:41:40 -04001492 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494
Bjorn Helgaase747f272009-03-24 16:49:43 -06001495int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001498 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Li Shaohuac4168bf2006-12-07 20:56:41 +08001500 memset(&ops, 0, sizeof(ops));
1501 ops.acpi_op_add = 1;
1502 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Patrick Mochel5b327262006-05-10 10:33:00 -04001504 result = bus_register(&acpi_bus_type);
1505 if (result) {
1506 /* We don't want to quit even if we failed to add suspend/resume */
1507 printk(KERN_ERR PREFIX "Could not register bus type\n");
1508 }
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 * Enumerate devices in the ACPI namespace.
1512 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001513 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001514
Li Shaohuac4168bf2006-12-07 20:56:41 +08001515 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001516 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 if (result)
1519 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1520
Patrick Mocheld550d982006-06-27 00:41:40 -04001521 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}