blob: 81aec8d66fd0b19e7fd3b388e7462866b0e14e39 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060016#include "internal.h"
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050019ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040021extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020024#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define ACPI_BUS_DEVICE_NAME "System Bus"
26
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000027#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020029/* Should be const */
30static char* dummy_hid = "device";
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080033static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080034DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035LIST_HEAD(acpi_wakeup_device_list);
36
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080037struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080038 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039 unsigned int instance_no;
40 struct list_head node;
41};
Thomas Renninger29b71a12007-07-23 14:43:51 +020042
43/*
44 * Creates hid/cid(s) string needed for modalias and uevent
45 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
46 * char *modalias: "acpi:IBM0001:ACPI0001"
47*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020048static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
49 int size)
50{
Thomas Renninger29b71a12007-07-23 14:43:51 +020051 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080052 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060053 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020054
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020055 if (list_empty(&acpi_dev->pnp.ids))
56 return 0;
57
Zhang Rui5c9fcb52008-03-20 16:40:32 +080058 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020059 size -= len;
60
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060061 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
62 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080063 if (count < 0 || count >= size)
64 return -EINVAL;
65 len += count;
66 size -= count;
67 }
68
Thomas Renninger29b71a12007-07-23 14:43:51 +020069 modalias[len] = '\0';
70 return len;
71}
72
73static ssize_t
74acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
75 struct acpi_device *acpi_dev = to_acpi_device(dev);
76 int len;
77
78 /* Device has no HID and no CID or string is >1024 */
79 len = create_modalias(acpi_dev, buf, 1024);
80 if (len <= 0)
81 return 0;
82 buf[len++] = '\n';
83 return len;
84}
85static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
86
Zhang Ruic8d72a52009-06-22 11:31:16 +080087static void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Zhang Rui26d46862008-04-29 02:35:48 -040089 struct acpi_device *device;
90 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct acpi_object_list arg_list;
92 union acpi_object arg;
93 acpi_status status = AE_OK;
94
Zhang Rui26d46862008-04-29 02:35:48 -040095 if (acpi_bus_get_device(handle, &device))
Zhang Ruic8d72a52009-06-22 11:31:16 +080096 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Zhang Rui26d46862008-04-29 02:35:48 -040098 if (!device)
Zhang Ruic8d72a52009-06-22 11:31:16 +080099 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400100
101 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100102 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400103
104 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800105 printk(KERN_ERR PREFIX
106 "Removing device failed\n");
Zhang Ruic8d72a52009-06-22 11:31:16 +0800107 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400108 }
109
110 /* power off device */
111 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
112 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800113 printk(KERN_WARNING PREFIX
114 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400115
116 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 arg_list.count = 1;
118 arg_list.pointer = &arg;
119 arg.type = ACPI_TYPE_INTEGER;
120 arg.integer.value = 0;
121 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
122 }
123
124 arg_list.count = 1;
125 arg_list.pointer = &arg;
126 arg.type = ACPI_TYPE_INTEGER;
127 arg.integer.value = 1;
128
129 /*
130 * TBD: _EJD support.
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400133 if (ACPI_FAILURE(status))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800134 printk(KERN_WARNING PREFIX
135 "Eject device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Zhang Ruic8d72a52009-06-22 11:31:16 +0800137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800141acpi_eject_store(struct device *d, struct device_attribute *attr,
142 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Len Brown4be44fc2005-08-05 00:44:28 -0400144 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800147 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if ((!count) || (buf[0] != '1')) {
150 return -EINVAL;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800153 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 ret = -ENODEV;
155 goto err;
156 }
157#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800158 status = acpi_get_type(acpi_device->handle, &type);
159 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ret = -ENODEV;
161 goto err;
162 }
163
Zhang Ruic8d72a52009-06-22 11:31:16 +0800164 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
Alok N Kataria74523c92008-06-13 12:54:24 -0400165err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800169static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800171static ssize_t
172acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
173 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600175 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800176}
177static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
178
179static ssize_t
180acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
181 struct acpi_device *acpi_dev = to_acpi_device(dev);
182 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
183 int result;
184
185 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600186 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800187 goto end;
188
189 result = sprintf(buf, "%s\n", (char*)path.pointer);
190 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600191end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800192 return result;
193}
194static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
195
196static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800198 acpi_status status;
199 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800200 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800202 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800203 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800204 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600205 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800206 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600207 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800208 goto end;
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200211 if (!list_empty(&dev->pnp.ids)) {
212 result = device_create_file(&dev->dev, &dev_attr_hid);
213 if (result)
214 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200216 result = device_create_file(&dev->dev, &dev_attr_modalias);
217 if (result)
218 goto end;
219 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200220
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800221 /*
222 * If device has _EJ0, 'eject' file is created that is used to trigger
223 * hot-removal function from userland.
224 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800225 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
226 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800227 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600228end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800229 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800230}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800232static void acpi_device_remove_files(struct acpi_device *dev)
233{
234 acpi_status status;
235 acpi_handle temp;
236
237 /*
238 * If device has _EJ0, 'eject' file is created that is used to trigger
239 * hot-removal function from userland.
240 */
241 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
242 if (ACPI_SUCCESS(status))
243 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800244
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600245 device_remove_file(&dev->dev, &dev_attr_modalias);
246 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600247 if (dev->handle)
Zhang Ruie49bd2dd2006-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
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200327 if (list_empty(&acpi_dev->pnp.ids))
328 return 0;
329
Kay Sievers7eff2e72007-08-14 15:15:12 +0200330 if (add_uevent_var(env, "MODALIAS="))
331 return -ENOMEM;
332 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
333 sizeof(env->buf) - env->buflen);
334 if (len >= (sizeof(env->buf) - env->buflen))
335 return -ENOMEM;
336 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return 0;
338}
339
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000340static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
341{
342 struct acpi_device *device = data;
343
344 device->driver->ops.notify(device, event);
345}
346
347static acpi_status acpi_device_notify_fixed(void *data)
348{
349 struct acpi_device *device = data;
350
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000351 /* Fixed hardware devices have no handles */
352 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000353 return AE_OK;
354}
355
356static int acpi_device_install_notify_handler(struct acpi_device *device)
357{
358 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000359
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000360 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000361 status =
362 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
363 acpi_device_notify_fixed,
364 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000365 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000366 status =
367 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
368 acpi_device_notify_fixed,
369 device);
370 else
371 status = acpi_install_notify_handler(device->handle,
372 ACPI_DEVICE_NOTIFY,
373 acpi_device_notify,
374 device);
375
376 if (ACPI_FAILURE(status))
377 return -EINVAL;
378 return 0;
379}
380
381static void acpi_device_remove_notify_handler(struct acpi_device *device)
382{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000383 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000384 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
385 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000386 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000387 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
388 acpi_device_notify_fixed);
389 else
390 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
391 acpi_device_notify);
392}
393
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800394static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
395static int acpi_start_single_object(struct acpi_device *);
396static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800397{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800398 struct acpi_device *acpi_dev = to_acpi_device(dev);
399 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
400 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800402 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
403 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800404 if (acpi_dev->bus_ops.acpi_op_start)
405 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000406
407 if (acpi_drv->ops.notify) {
408 ret = acpi_device_install_notify_handler(acpi_dev);
409 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000410 if (acpi_drv->ops.remove)
411 acpi_drv->ops.remove(acpi_dev,
412 acpi_dev->removal_type);
413 return ret;
414 }
415 }
416
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800417 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
418 "Found driver [%s] for device [%s]\n",
419 acpi_drv->name, acpi_dev->pnp.bus_id));
420 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800421 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800422 return ret;
423}
424
425static int acpi_device_remove(struct device * dev)
426{
427 struct acpi_device *acpi_dev = to_acpi_device(dev);
428 struct acpi_driver *acpi_drv = acpi_dev->driver;
429
430 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000431 if (acpi_drv->ops.notify)
432 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800433 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800434 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800435 }
436 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700437 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800438
439 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800440 return 0;
441}
442
David Brownell55955aa2007-05-08 00:28:35 -0700443struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800444 .name = "acpi",
445 .suspend = acpi_device_suspend,
446 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800447 .match = acpi_bus_match,
448 .probe = acpi_device_probe,
449 .remove = acpi_device_remove,
450 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451};
452
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000453static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800455 int result;
456 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
457 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /*
460 * Linkage
461 * -------
462 * Link this device to its parent and siblings.
463 */
464 INIT_LIST_HEAD(&device->children);
465 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 INIT_LIST_HEAD(&device->wakeup_list);
467
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800468 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
469 if (!new_bus_id) {
470 printk(KERN_ERR PREFIX "Memory allocation error\n");
471 return -ENOMEM;
472 }
473
Shaohua Li90905892009-04-07 10:24:29 +0800474 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800475 /*
476 * Find suitable bus_id and instance number in acpi_bus_id_list
477 * If failed, create one and link it into acpi_bus_id_list
478 */
479 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600480 if (!strcmp(acpi_device_bus_id->bus_id,
481 acpi_device_hid(device))) {
482 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800483 found = 1;
484 kfree(new_bus_id);
485 break;
486 }
487 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600488 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800489 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600490 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800491 acpi_device_bus_id->instance_no = 0;
492 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
493 }
Kay Sievers07944692008-10-30 01:18:59 +0100494 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800495
Len Brown33b57152008-12-15 22:09:26 -0500496 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (device->wakeup.flags.valid)
500 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800501 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Patrick Mochel1890a972006-12-07 20:56:31 +0800503 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000504 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800505 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800506 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600507 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600508 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600509 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800510 goto end;
511 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800512
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800513 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600514 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100515 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
516 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800517
Li Shaohua96333572006-12-07 20:56:46 +0800518 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800519 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600520end:
Shaohua Li90905892009-04-07 10:24:29 +0800521 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500522 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800523 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800524 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800525 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800526 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static void acpi_device_unregister(struct acpi_device *device, int type)
530{
Shaohua Li90905892009-04-07 10:24:29 +0800531 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500532 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800536 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800539
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800540 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800541 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Zhang Rui9e89dde2006-12-07 20:56:16 +0800544/* --------------------------------------------------------------------------
545 Driver Management
546 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500548 * acpi_bus_driver_init - add a device to a driver
549 * @device: the device to add and initialize
550 * @driver: driver for the device
551 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600552 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800553 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555static int
Len Brown4be44fc2005-08-05 00:44:28 -0400556acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Len Brown4be44fc2005-08-05 00:44:28 -0400558 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400564 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 result = driver->ops.add(device);
567 if (result) {
568 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700569 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400570 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 device->driver = driver;
574
575 /*
576 * TBD - Configuration Management: Assign resources to device based
577 * upon possible configuration and currently allocated resources.
578 */
579
Len Brown4be44fc2005-08-05 00:44:28 -0400580 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
581 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400582 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700583}
584
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400585static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700586{
587 int result = 0;
588 struct acpi_driver *driver;
589
Rajesh Shah3fb02732005-04-28 00:25:52 -0700590
591 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (driver->ops.start) {
595 result = driver->ops.start(device);
596 if (result && driver->ops.remove)
597 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Patrick Mocheld550d982006-06-27 00:41:40 -0400600 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500604 * acpi_bus_register_driver - register a driver with the ACPI bus
605 * @driver: driver being registered
606 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500608 * devices that match the driver's criteria and binds. Returns zero for
609 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Len Brown4be44fc2005-08-05 00:44:28 -0400611int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Patrick Mochel1890a972006-12-07 20:56:31 +0800613 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400616 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800617 driver->drv.name = driver->name;
618 driver->drv.bus = &acpi_bus_type;
619 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Patrick Mochel1890a972006-12-07 20:56:31 +0800621 ret = driver_register(&driver->drv);
622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Len Brown4be44fc2005-08-05 00:44:28 -0400625EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500628 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
629 * @driver: driver to unregister
630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Unregisters a driver with the ACPI bus. Searches the namespace for all
632 * devices that match the driver's criteria and unbinds.
633 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400634void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Patrick Mochel1890a972006-12-07 20:56:31 +0800636 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Len Brown4be44fc2005-08-05 00:44:28 -0400638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639EXPORT_SYMBOL(acpi_bus_unregister_driver);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* --------------------------------------------------------------------------
642 Device Enumeration
643 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000644static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
645{
646 acpi_status status;
647 int ret;
648 struct acpi_device *device;
649
650 /*
651 * Fixed hardware devices do not appear in the namespace and do not
652 * have handles, but we fabricate acpi_devices for them, so we have
653 * to deal with them specially.
654 */
655 if (handle == NULL)
656 return acpi_root;
657
658 do {
659 status = acpi_get_parent(handle, &handle);
660 if (status == AE_NULL_ENTRY)
661 return NULL;
662 if (ACPI_FAILURE(status))
663 return acpi_root;
664
665 ret = acpi_bus_get_device(handle, &device);
666 if (ret == 0)
667 return device;
668 } while (1);
669}
670
Len Brownc8f7a62c2006-07-09 17:22:28 -0400671acpi_status
672acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
673{
674 acpi_status status;
675 acpi_handle tmp;
676 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
677 union acpi_object *obj;
678
679 status = acpi_get_handle(handle, "_EJD", &tmp);
680 if (ACPI_FAILURE(status))
681 return status;
682
683 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
684 if (ACPI_SUCCESS(status)) {
685 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100686 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
687 ejd);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400688 kfree(buffer.pointer);
689 }
690 return status;
691}
692EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
693
Bob Moore8e4319c2009-06-29 13:43:27 +0800694void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696
697 /* TBD */
698
699 return;
700}
701
Zhang Rui9e89dde2006-12-07 20:56:16 +0800702static int acpi_bus_get_perf_flags(struct acpi_device *device)
703{
704 device->performance.state = ACPI_STATE_UNKNOWN;
705 return 0;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static acpi_status
709acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
710 union acpi_object *package)
711{
712 int i = 0;
713 union acpi_object *element = NULL;
714
715 if (!device || !package || (package->package.count < 2))
716 return AE_BAD_PARAMETER;
717
718 element = &(package->package.elements[0]);
719 if (!element)
720 return AE_BAD_PARAMETER;
721 if (element->type == ACPI_TYPE_PACKAGE) {
722 if ((element->package.count < 2) ||
723 (element->package.elements[0].type !=
724 ACPI_TYPE_LOCAL_REFERENCE)
725 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
726 return AE_BAD_DATA;
727 device->wakeup.gpe_device =
728 element->package.elements[0].reference.handle;
729 device->wakeup.gpe_number =
730 (u32) element->package.elements[1].integer.value;
731 } else if (element->type == ACPI_TYPE_INTEGER) {
732 device->wakeup.gpe_number = element->integer.value;
733 } else
734 return AE_BAD_DATA;
735
736 element = &(package->package.elements[1]);
737 if (element->type != ACPI_TYPE_INTEGER) {
738 return AE_BAD_DATA;
739 }
740 device->wakeup.sleep_state = element->integer.value;
741
742 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
743 return AE_NO_MEMORY;
744 }
745 device->wakeup.resources.count = package->package.count - 2;
746 for (i = 0; i < device->wakeup.resources.count; i++) {
747 element = &(package->package.elements[i + 2]);
Bob Moorecd0b2242008-04-10 19:06:43 +0400748 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 device->wakeup.resources.handles[i] = element->reference.handle;
752 }
753
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200754 acpi_gpe_can_wake(device->wakeup.gpe_device, device->wakeup.gpe_number);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return AE_OK;
757}
758
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100759static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200761 struct acpi_device_id button_device_ids[] = {
762 {"PNP0C0D", 0},
763 {"PNP0C0C", 0},
764 {"PNP0C0E", 0},
765 {"", 0},
766 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100767 acpi_status status;
768 acpi_event_status event_status;
769
770 device->wakeup.run_wake_count = 0;
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100771 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100772
773 /* Power button, Lid switch always enable wakeup */
774 if (!acpi_match_device_ids(device, button_device_ids)) {
775 device->wakeup.flags.run_wake = 1;
776 device->wakeup.flags.always_enabled = 1;
777 return;
778 }
779
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200780 status = acpi_get_gpe_status(device->wakeup.gpe_device,
781 device->wakeup.gpe_number,
782 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100783 if (status == AE_OK)
784 device->wakeup.flags.run_wake =
785 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
786}
787
788static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
789{
790 acpi_status status = 0;
791 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
792 union acpi_object *package = NULL;
793 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* _PRW */
796 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
797 if (ACPI_FAILURE(status)) {
798 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
799 goto end;
800 }
801
802 package = (union acpi_object *)buffer.pointer;
803 status = acpi_bus_extract_wakeup_device_power_package(device, package);
804 if (ACPI_FAILURE(status)) {
805 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
806 goto end;
807 }
808
809 kfree(buffer.pointer);
810
811 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200812 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100813 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800814 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
815 * system for the ACPI device with the _PRW object.
816 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
817 * So it is necessary to call _DSW object first. Only when it is not
818 * present will the _PSW object used.
819 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200820 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
821 if (psw_error)
822 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
823 "error in _DSW or _PSW evaluation\n"));
824
Alex Chiang0c526d92009-05-14 08:31:37 -0600825end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (ACPI_FAILURE(status))
827 device->flags.wake_capable = 0;
828 return 0;
829}
830
Zhang Rui9e89dde2006-12-07 20:56:16 +0800831static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800833 acpi_status status = 0;
834 acpi_handle handle = NULL;
835 u32 i = 0;
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800839 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800841 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800843 device->power.flags.explicit_get = 1;
844 status = acpi_get_handle(device->handle, "_IRC", &handle);
845 if (ACPI_SUCCESS(status))
846 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800849 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800851 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
852 struct acpi_device_power_state *ps = &device->power.states[i];
853 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Zhang Rui9e89dde2006-12-07 20:56:16 +0800855 /* Evaluate "_PRx" to se if power resources are referenced */
856 acpi_evaluate_reference(device->handle, object_name, NULL,
857 &ps->resources);
858 if (ps->resources.count) {
859 device->power.flags.power_resources = 1;
860 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Zhang Rui9e89dde2006-12-07 20:56:16 +0800863 /* Evaluate "_PSx" to see if we can do explicit sets */
864 object_name[2] = 'S';
865 status = acpi_get_handle(device->handle, object_name, &handle);
866 if (ACPI_SUCCESS(status)) {
867 ps->flags.explicit_set = 1;
868 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800870
871 /* State is valid if we have some power control */
872 if (ps->resources.count || ps->flags.explicit_set)
873 ps->flags.valid = 1;
874
875 ps->power = -1; /* Unknown - driver assigned */
876 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Zhang Rui9e89dde2006-12-07 20:56:16 +0800879 /* Set defaults for D0 and D3 states (always valid) */
880 device->power.states[ACPI_STATE_D0].flags.valid = 1;
881 device->power.states[ACPI_STATE_D0].power = 100;
882 device->power.states[ACPI_STATE_D3].flags.valid = 1;
883 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Zhang Rui9e89dde2006-12-07 20:56:16 +0800885 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Zhang Rui9e89dde2006-12-07 20:56:16 +0800887 device->power.state = ACPI_STATE_UNKNOWN;
Zhao Yakuia51e1452008-08-11 14:55:05 +0800888 acpi_bus_get_power(device->handle, &(device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 return 0;
891}
892
Len Brown4be44fc2005-08-05 00:44:28 -0400893static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Len Brown4be44fc2005-08-05 00:44:28 -0400895 acpi_status status = AE_OK;
896 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* Presence of _STA indicates 'dynamic_status' */
900 status = acpi_get_handle(device->handle, "_STA", &temp);
901 if (ACPI_SUCCESS(status))
902 device->flags.dynamic_status = 1;
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Presence of _RMV indicates 'removable' */
905 status = acpi_get_handle(device->handle, "_RMV", &temp);
906 if (ACPI_SUCCESS(status))
907 device->flags.removable = 1;
908
909 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
910 status = acpi_get_handle(device->handle, "_EJD", &temp);
911 if (ACPI_SUCCESS(status))
912 device->flags.ejectable = 1;
913 else {
914 status = acpi_get_handle(device->handle, "_EJ0", &temp);
915 if (ACPI_SUCCESS(status))
916 device->flags.ejectable = 1;
917 }
918
919 /* Presence of _LCK indicates 'lockable' */
920 status = acpi_get_handle(device->handle, "_LCK", &temp);
921 if (ACPI_SUCCESS(status))
922 device->flags.lockable = 1;
923
924 /* Presence of _PS0|_PR0 indicates 'power manageable' */
925 status = acpi_get_handle(device->handle, "_PS0", &temp);
926 if (ACPI_FAILURE(status))
927 status = acpi_get_handle(device->handle, "_PR0", &temp);
928 if (ACPI_SUCCESS(status))
929 device->flags.power_manageable = 1;
930
931 /* Presence of _PRW indicates wake capable */
932 status = acpi_get_handle(device->handle, "_PRW", &temp);
933 if (ACPI_SUCCESS(status))
934 device->flags.wake_capable = 1;
935
Joe Perches3c5f9be42008-02-03 17:06:17 +0200936 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Patrick Mocheld550d982006-06-27 00:41:40 -0400938 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +0000941static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Len Brown4be44fc2005-08-05 00:44:28 -0400943 char bus_id[5] = { '?', 0 };
944 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
945 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /*
948 * Bus ID
949 * ------
950 * The device's Bus ID is simply the object name.
951 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
952 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +0000953 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +0000955 return;
956 }
957
958 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 case ACPI_BUS_TYPE_POWER_BUTTON:
960 strcpy(device->pnp.bus_id, "PWRF");
961 break;
962 case ACPI_BUS_TYPE_SLEEP_BUTTON:
963 strcpy(device->pnp.bus_id, "SLPF");
964 break;
965 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000966 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* Clean up trailing underscores (if any) */
968 for (i = 3; i > 1; i--) {
969 if (bus_id[i] == '_')
970 bus_id[i] = '\0';
971 else
972 break;
973 }
974 strcpy(device->pnp.bus_id, bus_id);
975 break;
976 }
977}
978
Zhang Rui54735262007-01-11 02:09:09 -0500979/*
980 * acpi_bay_match - see if a device is an ejectable driver bay
981 *
982 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
983 * then we can safely call it an ejectable drive bay
984 */
985static int acpi_bay_match(struct acpi_device *device){
986 acpi_status status;
987 acpi_handle handle;
988 acpi_handle tmp;
989 acpi_handle phandle;
990
991 handle = device->handle;
992
993 status = acpi_get_handle(handle, "_EJ0", &tmp);
994 if (ACPI_FAILURE(status))
995 return -ENODEV;
996
997 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
998 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
999 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1000 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1001 return 0;
1002
1003 if (acpi_get_parent(handle, &phandle))
1004 return -ENODEV;
1005
1006 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1007 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1008 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1009 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1010 return 0;
1011
1012 return -ENODEV;
1013}
1014
Frank Seidel3620f2f2007-12-07 13:20:34 +01001015/*
1016 * acpi_dock_match - see if a device has a _DCK method
1017 */
1018static int acpi_dock_match(struct acpi_device *device)
1019{
1020 acpi_handle tmp;
1021 return acpi_get_handle(device->handle, "_DCK", &tmp);
1022}
1023
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001024char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001025{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001026 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001027
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001028 if (list_empty(&device->pnp.ids))
1029 return dummy_hid;
1030
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001031 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1032 return hid->id;
1033}
1034EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001035
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001036static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1037{
1038 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001039
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001040 id = kmalloc(sizeof(*id), GFP_KERNEL);
1041 if (!id)
1042 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001043
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001044 id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1045 if (!id->id) {
1046 kfree(id);
1047 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001048 }
1049
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001050 strcpy(id->id, dev_id);
1051 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001052}
1053
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001054/*
1055 * Old IBM workstations have a DSDT bug wherein the SMBus object
1056 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1057 * prefix. Work around this.
1058 */
1059static int acpi_ibm_smbus_match(struct acpi_device *device)
1060{
1061 acpi_handle h_dummy;
1062 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1063 int result;
1064
1065 if (!dmi_name_in_vendors("IBM"))
1066 return -ENODEV;
1067
1068 /* Look for SMBS object */
1069 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1070 if (result)
1071 return result;
1072
1073 if (strcmp("SMBS", path.pointer)) {
1074 result = -ENODEV;
1075 goto out;
1076 }
1077
1078 /* Does it have the necessary (but misnamed) methods? */
1079 result = -ENODEV;
1080 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1081 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1082 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1083 result = 0;
1084out:
1085 kfree(path.pointer);
1086 return result;
1087}
1088
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001089static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Len Brown4be44fc2005-08-05 00:44:28 -04001091 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001092 struct acpi_device_info *info;
1093 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001094 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001096 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001098 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001099 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001100 break;
1101 }
1102
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001103 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001105 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return;
1107 }
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001110 acpi_add_id(device, info->hardware_id.string);
1111 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001112 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001113 for (i = 0; i < cid_list->count; i++)
1114 acpi_add_id(device, cid_list->ids[i].string);
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (info->valid & ACPI_VALID_ADR) {
1117 device->pnp.bus_address = info->address;
1118 device->flags.bus_address = 1;
1119 }
Zhang Ruiae843332006-12-07 20:57:10 +08001120
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001121 kfree(info);
1122
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001123 /*
1124 * Some devices don't reliably have _HIDs & _CIDs, so add
1125 * synthetic HIDs to make sure drivers can find them.
1126 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001127 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001128 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001129 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001130 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001131 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001132 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001133 else if (!acpi_ibm_smbus_match(device))
1134 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001135 else if (!acpi_device_hid(device) &&
1136 ACPI_IS_ROOT_DEVICE(device->parent)) {
1137 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1138 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1139 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1140 }
Zhang Rui54735262007-01-11 02:09:09 -05001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 break;
1143 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001144 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 break;
1146 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001147 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001150 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001153 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
1155 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001156 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001161static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001163 acpi_status status;
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /*
1166 * Context
1167 * -------
1168 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001169 * resolutions from handle->device very efficient. Fixed hardware
1170 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001172 if (!device->handle)
1173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001175 status = acpi_attach_data(device->handle,
1176 acpi_bus_data_handler, device);
1177 if (ACPI_SUCCESS(status))
1178 return 0;
1179
1180 printk(KERN_ERR PREFIX "Error attaching device data\n");
1181 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Len Brown4be44fc2005-08-05 00:44:28 -04001184static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001187 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Li Shaohua96333572006-12-07 20:56:46 +08001189 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001190 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Rui Zhang2786f6e2006-12-21 02:21:13 -05001195 /*
1196 * unbind _ADR-Based Devices when hot removal
1197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (dev->flags.bus_address) {
1199 if ((dev->parent) && (dev->parent->ops.unbind))
1200 dev->parent->ops.unbind(dev);
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1203
Patrick Mocheld550d982006-06-27 00:41:40 -04001204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001207static int acpi_add_single_object(struct acpi_device **child,
1208 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001209 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001210 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001212 int result;
1213 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001214 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Burman Yan36bcbec2006-12-19 12:56:11 -08001216 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001218 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001219 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001222 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001223 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001225 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001226 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001227 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001228
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001229 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /*
1232 * Flags
1233 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001234 * Note that we only look for object handles -- cannot evaluate objects
1235 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 */
1237 result = acpi_bus_get_flags(device);
1238 if (result)
1239 goto end;
1240
1241 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * Initialize Device
1243 * -----------------
1244 * TBD: Synch with Core's enumeration/initialization process.
1245 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001246 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /*
1249 * Power Management
1250 * ----------------
1251 */
1252 if (device->flags.power_manageable) {
1253 result = acpi_bus_get_power_flags(device);
1254 if (result)
1255 goto end;
1256 }
1257
Len Brown4be44fc2005-08-05 00:44:28 -04001258 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * Wakeup device management
1260 *-----------------------
1261 */
1262 if (device->flags.wake_capable) {
1263 result = acpi_bus_get_wakeup_device_flags(device);
1264 if (result)
1265 goto end;
1266 }
1267
1268 /*
1269 * Performance Management
1270 * ----------------------
1271 */
1272 if (device->flags.performance_manageable) {
1273 result = acpi_bus_get_perf_flags(device);
1274 if (result)
1275 goto end;
1276 }
1277
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001278 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001279 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001281 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001284 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
1286 if (device->flags.bus_address) {
1287 if (device->parent && device->parent->ops.bind)
1288 device->parent->ops.bind(device);
1289 }
1290
Alex Chiang0c526d92009-05-14 08:31:37 -06001291end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001292 if (!result) {
1293 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1295 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1296 (char *) buffer.pointer,
1297 device->parent ? dev_name(&device->parent->dev) :
1298 "(null)"));
1299 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001301 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001302 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Patrick Mocheld550d982006-06-27 00:41:40 -04001304 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001307#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1308 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1309
1310static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1311 unsigned long long *sta)
1312{
1313 acpi_status status;
1314 acpi_object_type acpi_type;
1315
1316 status = acpi_get_type(handle, &acpi_type);
1317 if (ACPI_FAILURE(status))
1318 return -ENODEV;
1319
1320 switch (acpi_type) {
1321 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1322 case ACPI_TYPE_DEVICE:
1323 *type = ACPI_BUS_TYPE_DEVICE;
1324 status = acpi_bus_get_status_handle(handle, sta);
1325 if (ACPI_FAILURE(status))
1326 return -ENODEV;
1327 break;
1328 case ACPI_TYPE_PROCESSOR:
1329 *type = ACPI_BUS_TYPE_PROCESSOR;
1330 status = acpi_bus_get_status_handle(handle, sta);
1331 if (ACPI_FAILURE(status))
1332 return -ENODEV;
1333 break;
1334 case ACPI_TYPE_THERMAL:
1335 *type = ACPI_BUS_TYPE_THERMAL;
1336 *sta = ACPI_STA_DEFAULT;
1337 break;
1338 case ACPI_TYPE_POWER:
1339 *type = ACPI_BUS_TYPE_POWER;
1340 *sta = ACPI_STA_DEFAULT;
1341 break;
1342 default:
1343 return -ENODEV;
1344 }
1345
1346 return 0;
1347}
1348
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001349static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1350 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001352 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001353 int type;
1354 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001355 struct acpi_device *device;
1356 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001357 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001358
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001359 result = acpi_bus_type_and_status(handle, &type, &sta);
1360 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001361 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001363 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1364 !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1365 return AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001367 /*
1368 * We may already have an acpi_device from a previous enumeration. If
1369 * so, we needn't add it again, but we may still have to start it.
1370 */
1371 device = NULL;
1372 acpi_bus_get_device(handle, &device);
1373 if (ops->acpi_op_add && !device)
1374 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001375
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001376 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001377 return AE_CTRL_DEPTH;
1378
1379 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1380 status = acpi_start_single_object(device);
1381 if (ACPI_FAILURE(status))
1382 return AE_CTRL_DEPTH;
1383 }
1384
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001385 if (!*return_value)
1386 *return_value = device;
1387 return AE_OK;
1388}
1389
1390static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1391 struct acpi_device **child)
1392{
1393 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001394 void *device = NULL;
1395
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001396 status = acpi_bus_check_add(handle, 0, ops, &device);
1397 if (ACPI_SUCCESS(status))
1398 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001399 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001400
1401 if (child)
1402 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001403
1404 if (device)
1405 return 0;
1406 else
1407 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Thomas Renninger77796882010-01-29 17:48:52 +01001410/*
1411 * acpi_bus_add and acpi_bus_start
1412 *
1413 * scan a given ACPI tree and (probably recently hot-plugged)
1414 * create and add or starts found devices.
1415 *
1416 * If no devices were found -ENODEV is returned which does not
1417 * mean that this is a real error, there just have been no suitable
1418 * ACPI objects in the table trunk from which the kernel could create
1419 * a device and add/start an appropriate driver.
1420 */
1421
Rajesh Shah3fb02732005-04-28 00:25:52 -07001422int
Len Brown4be44fc2005-08-05 00:44:28 -04001423acpi_bus_add(struct acpi_device **child,
1424 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001425{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001426 struct acpi_bus_ops ops;
1427
Li Shaohuac4168bf2006-12-07 20:56:41 +08001428 memset(&ops, 0, sizeof(ops));
1429 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001430
Thomas Renninger77796882010-01-29 17:48:52 +01001431 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001432}
1433EXPORT_SYMBOL(acpi_bus_add);
1434
Len Brown4be44fc2005-08-05 00:44:28 -04001435int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001436{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001437 struct acpi_bus_ops ops;
1438
Thomas Renningerd2f66502010-01-29 17:48:51 +01001439 if (!device)
1440 return -EINVAL;
1441
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001442 memset(&ops, 0, sizeof(ops));
1443 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001444
Thomas Renninger77796882010-01-29 17:48:52 +01001445 return acpi_bus_scan(device->handle, &ops, NULL);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001446}
1447EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Kristen Accardiceaba662006-02-23 17:56:01 -08001449int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Len Brown4be44fc2005-08-05 00:44:28 -04001451 acpi_status status;
1452 struct acpi_device *parent, *child;
1453 acpi_handle phandle, chandle;
1454 acpi_object_type type;
1455 u32 level = 1;
1456 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Len Brown4be44fc2005-08-05 00:44:28 -04001458 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 phandle = start->handle;
1460 child = chandle = NULL;
1461
1462 while ((level > 0) && parent && (!err)) {
1463 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001464 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /*
1467 * If this scope is exhausted then move our way back up.
1468 */
1469 if (ACPI_FAILURE(status)) {
1470 level--;
1471 chandle = phandle;
1472 acpi_get_parent(phandle, &phandle);
1473 child = parent;
1474 parent = parent->parent;
1475
1476 if (level == 0)
1477 err = acpi_bus_remove(child, rmdevice);
1478 else
1479 err = acpi_bus_remove(child, 1);
1480
1481 continue;
1482 }
1483
1484 status = acpi_get_type(chandle, &type);
1485 if (ACPI_FAILURE(status)) {
1486 continue;
1487 }
1488 /*
1489 * If there is a device corresponding to chandle then
1490 * parse it (depth-first).
1491 */
1492 if (acpi_bus_get_device(chandle, &child) == 0) {
1493 level++;
1494 phandle = chandle;
1495 chandle = NULL;
1496 parent = child;
1497 }
1498 continue;
1499 }
1500 return err;
1501}
Kristen Accardiceaba662006-02-23 17:56:01 -08001502EXPORT_SYMBOL_GPL(acpi_bus_trim);
1503
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001504static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Len Brown4be44fc2005-08-05 00:44:28 -04001506 int result = 0;
1507 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001508 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Li Shaohuac4168bf2006-12-07 20:56:41 +08001510 memset(&ops, 0, sizeof(ops));
1511 ops.acpi_op_add = 1;
1512 ops.acpi_op_start = 1;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /*
1515 * Enumerate all fixed-feature devices.
1516 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001517 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001518 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001519 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001520 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001521 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001524 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001525 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001526 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001527 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001528 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Patrick Mocheld550d982006-06-27 00:41:40 -04001531 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
Bjorn Helgaase747f272009-03-24 16:49:43 -06001534int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001537 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Li Shaohuac4168bf2006-12-07 20:56:41 +08001539 memset(&ops, 0, sizeof(ops));
1540 ops.acpi_op_add = 1;
1541 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Patrick Mochel5b327262006-05-10 10:33:00 -04001543 result = bus_register(&acpi_bus_type);
1544 if (result) {
1545 /* We don't want to quit even if we failed to add suspend/resume */
1546 printk(KERN_ERR PREFIX "Could not register bus type\n");
1547 }
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * Enumerate devices in the ACPI namespace.
1551 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001552 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001553
Li Shaohuac4168bf2006-12-07 20:56:41 +08001554 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001555 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 if (result)
1558 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1559
Patrick Mocheld550d982006-06-27 00:41:40 -04001560 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}