blob: ed87f433cec246073cf595f8a17db75fd74ddf43 [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>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Thomas Renninger620e1122010-10-01 10:54:00 +020030static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020031
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
Toshi Kanic4753e52012-05-23 20:25:20 -060087/**
88 * acpi_bus_hot_remove_device: hot-remove a device and its children
89 * @context: struct acpi_eject_event pointer (freed in this func)
90 *
91 * Hot-remove a device and its children. This function frees up the
92 * memory space passed by arg context, so that the caller may call
93 * this function asynchronously through acpi_os_hotplug_execute().
94 */
95void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Toshi Kanic4753e52012-05-23 20:25:20 -060097 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
Zhang Rui26d46862008-04-29 02:35:48 -040098 struct acpi_device *device;
Toshi Kanic4753e52012-05-23 20:25:20 -060099 acpi_handle handle = ej_event->handle;
Toshi Kanib3c450c2012-10-26 13:38:57 +0200100 acpi_handle temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct acpi_object_list arg_list;
102 union acpi_object arg;
103 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600104 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Zhang Rui26d46862008-04-29 02:35:48 -0400106 if (acpi_bus_get_device(handle, &device))
Toshi Kanic4753e52012-05-23 20:25:20 -0600107 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Zhang Rui26d46862008-04-29 02:35:48 -0400109 if (!device)
Toshi Kanic4753e52012-05-23 20:25:20 -0600110 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400111
112 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100113 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400114
115 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800116 printk(KERN_ERR PREFIX
117 "Removing device failed\n");
Toshi Kanic4753e52012-05-23 20:25:20 -0600118 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400119 }
120
Toshi Kanib3c450c2012-10-26 13:38:57 +0200121 /* device has been freed */
122 device = NULL;
123
Zhang Rui26d46862008-04-29 02:35:48 -0400124 /* power off device */
125 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
126 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800127 printk(KERN_WARNING PREFIX
128 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400129
Toshi Kanib3c450c2012-10-26 13:38:57 +0200130 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 arg_list.count = 1;
132 arg_list.pointer = &arg;
133 arg.type = ACPI_TYPE_INTEGER;
134 arg.integer.value = 0;
135 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
136 }
137
138 arg_list.count = 1;
139 arg_list.pointer = &arg;
140 arg.type = ACPI_TYPE_INTEGER;
141 arg.integer.value = 1;
142
143 /*
144 * TBD: _EJD support.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600147 if (ACPI_FAILURE(status)) {
148 if (status != AE_NOT_FOUND)
149 printk(KERN_WARNING PREFIX
150 "Eject device failed\n");
151 goto err_out;
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Toshi Kanic4753e52012-05-23 20:25:20 -0600154 kfree(context);
155 return;
156
157err_out:
158 /* Inform firmware the hot-remove operation has completed w/ error */
159 (void) acpi_evaluate_hotplug_ost(handle,
160 ej_event->event, ost_code, NULL);
161 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800162 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800166acpi_eject_store(struct device *d, struct device_attribute *attr,
167 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Len Brown4be44fc2005-08-05 00:44:28 -0400169 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400171 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800172 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600173 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if ((!count) || (buf[0] != '1')) {
176 return -EINVAL;
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800179 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ret = -ENODEV;
181 goto err;
182 }
183#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800184 status = acpi_get_type(acpi_device->handle, &type);
185 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ret = -ENODEV;
187 goto err;
188 }
189
Toshi Kanic4753e52012-05-23 20:25:20 -0600190 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
191 if (!ej_event) {
192 ret = -ENOMEM;
193 goto err;
194 }
195
196 ej_event->handle = acpi_device->handle;
197 if (acpi_device->flags.eject_pending) {
198 /* event originated from ACPI eject notification */
199 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
200 acpi_device->flags.eject_pending = 0;
201 } else {
202 /* event originated from user */
203 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
204 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
205 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
206 }
207
208 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400209err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800213static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800215static ssize_t
216acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
217 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600219 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800220}
221static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
222
223static ssize_t
224acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
225 struct acpi_device *acpi_dev = to_acpi_device(dev);
226 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
227 int result;
228
229 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600230 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800231 goto end;
232
233 result = sprintf(buf, "%s\n", (char*)path.pointer);
234 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600235end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800236 return result;
237}
238static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
239
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600240/* sysfs file that shows description text from the ACPI _STR method */
241static ssize_t description_show(struct device *dev,
242 struct device_attribute *attr,
243 char *buf) {
244 struct acpi_device *acpi_dev = to_acpi_device(dev);
245 int result;
246
247 if (acpi_dev->pnp.str_obj == NULL)
248 return 0;
249
250 /*
251 * The _STR object contains a Unicode identifier for a device.
252 * We need to convert to utf-8 so it can be displayed.
253 */
254 result = utf16s_to_utf8s(
255 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
256 acpi_dev->pnp.str_obj->buffer.length,
257 UTF16_LITTLE_ENDIAN, buf,
258 PAGE_SIZE);
259
260 buf[result++] = '\n';
261
262 return result;
263}
264static DEVICE_ATTR(description, 0444, description_show, NULL);
265
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800266static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600268 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800269 acpi_status status;
270 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800271 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800273 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800274 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800275 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600276 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800277 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600278 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800279 goto end;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200282 if (!list_empty(&dev->pnp.ids)) {
283 result = device_create_file(&dev->dev, &dev_attr_hid);
284 if (result)
285 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200287 result = device_create_file(&dev->dev, &dev_attr_modalias);
288 if (result)
289 goto end;
290 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200291
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600292 /*
293 * If device has _STR, 'description' file is created
294 */
295 status = acpi_get_handle(dev->handle, "_STR", &temp);
296 if (ACPI_SUCCESS(status)) {
297 status = acpi_evaluate_object(dev->handle, "_STR",
298 NULL, &buffer);
299 if (ACPI_FAILURE(status))
300 buffer.pointer = NULL;
301 dev->pnp.str_obj = buffer.pointer;
302 result = device_create_file(&dev->dev, &dev_attr_description);
303 if (result)
304 goto end;
305 }
306
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800307 /*
308 * If device has _EJ0, 'eject' file is created that is used to trigger
309 * hot-removal function from userland.
310 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800311 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
312 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800313 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600314end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800315 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800318static void acpi_device_remove_files(struct acpi_device *dev)
319{
320 acpi_status status;
321 acpi_handle temp;
322
323 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600324 * If device has _STR, remove 'description' file
325 */
326 status = acpi_get_handle(dev->handle, "_STR", &temp);
327 if (ACPI_SUCCESS(status)) {
328 kfree(dev->pnp.str_obj);
329 device_remove_file(&dev->dev, &dev_attr_description);
330 }
331 /*
332 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800333 */
334 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
335 if (ACPI_SUCCESS(status))
336 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800337
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600338 device_remove_file(&dev->dev, &dev_attr_modalias);
339 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600340 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800341 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800344 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200346
347int acpi_match_device_ids(struct acpi_device *device,
348 const struct acpi_device_id *ids)
349{
350 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600351 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200352
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800353 /*
354 * If the device is not present, it is unnecessary to load device
355 * driver for it.
356 */
357 if (!device->status.present)
358 return -ENODEV;
359
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600360 for (id = ids; id->id[0]; id++)
361 list_for_each_entry(hwid, &device->pnp.ids, list)
362 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200363 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200364
365 return -ENOENT;
366}
367EXPORT_SYMBOL(acpi_match_device_ids);
368
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600369static void acpi_free_ids(struct acpi_device *device)
370{
371 struct acpi_hardware_id *id, *tmp;
372
373 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
374 kfree(id->id);
375 kfree(id);
376 }
377}
378
Patrick Mochel1890a972006-12-07 20:56:31 +0800379static void acpi_device_release(struct device *dev)
380{
381 struct acpi_device *acpi_dev = to_acpi_device(dev);
382
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600383 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800384 kfree(acpi_dev);
385}
386
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800387static int acpi_bus_match(struct device *dev, struct device_driver *drv)
388{
389 struct acpi_device *acpi_dev = to_acpi_device(dev);
390 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
391
Thomas Renninger29b71a12007-07-23 14:43:51 +0200392 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800393}
394
Kay Sievers7eff2e72007-08-14 15:15:12 +0200395static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800396{
397 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200398 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800399
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200400 if (list_empty(&acpi_dev->pnp.ids))
401 return 0;
402
Kay Sievers7eff2e72007-08-14 15:15:12 +0200403 if (add_uevent_var(env, "MODALIAS="))
404 return -ENOMEM;
405 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
406 sizeof(env->buf) - env->buflen);
407 if (len >= (sizeof(env->buf) - env->buflen))
408 return -ENOMEM;
409 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000413static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
414{
415 struct acpi_device *device = data;
416
417 device->driver->ops.notify(device, event);
418}
419
420static acpi_status acpi_device_notify_fixed(void *data)
421{
422 struct acpi_device *device = data;
423
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000424 /* Fixed hardware devices have no handles */
425 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000426 return AE_OK;
427}
428
429static int acpi_device_install_notify_handler(struct acpi_device *device)
430{
431 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000432
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000433 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000434 status =
435 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
436 acpi_device_notify_fixed,
437 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000438 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000439 status =
440 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
441 acpi_device_notify_fixed,
442 device);
443 else
444 status = acpi_install_notify_handler(device->handle,
445 ACPI_DEVICE_NOTIFY,
446 acpi_device_notify,
447 device);
448
449 if (ACPI_FAILURE(status))
450 return -EINVAL;
451 return 0;
452}
453
454static void acpi_device_remove_notify_handler(struct acpi_device *device)
455{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000456 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000457 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
458 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000459 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000460 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
461 acpi_device_notify_fixed);
462 else
463 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
464 acpi_device_notify);
465}
466
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800467static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
468static int acpi_start_single_object(struct acpi_device *);
469static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800470{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800471 struct acpi_device *acpi_dev = to_acpi_device(dev);
472 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
473 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800475 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
476 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800477 if (acpi_dev->bus_ops.acpi_op_start)
478 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000479
480 if (acpi_drv->ops.notify) {
481 ret = acpi_device_install_notify_handler(acpi_dev);
482 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000483 if (acpi_drv->ops.remove)
484 acpi_drv->ops.remove(acpi_dev,
485 acpi_dev->removal_type);
486 return ret;
487 }
488 }
489
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800490 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
491 "Found driver [%s] for device [%s]\n",
492 acpi_drv->name, acpi_dev->pnp.bus_id));
493 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800494 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800495 return ret;
496}
497
498static int acpi_device_remove(struct device * dev)
499{
500 struct acpi_device *acpi_dev = to_acpi_device(dev);
501 struct acpi_driver *acpi_drv = acpi_dev->driver;
502
503 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000504 if (acpi_drv->ops.notify)
505 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800506 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800507 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800508 }
509 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700510 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800511
512 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800513 return 0;
514}
515
David Brownell55955aa2007-05-08 00:28:35 -0700516struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800517 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800518 .match = acpi_bus_match,
519 .probe = acpi_device_probe,
520 .remove = acpi_device_remove,
521 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522};
523
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000524static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800526 int result;
527 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
528 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * Linkage
532 * -------
533 * Link this device to its parent and siblings.
534 */
535 INIT_LIST_HEAD(&device->children);
536 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800538 INIT_LIST_HEAD(&device->physical_node_list);
539 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800541 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
542 if (!new_bus_id) {
543 printk(KERN_ERR PREFIX "Memory allocation error\n");
544 return -ENOMEM;
545 }
546
Shaohua Li90905892009-04-07 10:24:29 +0800547 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800548 /*
549 * Find suitable bus_id and instance number in acpi_bus_id_list
550 * If failed, create one and link it into acpi_bus_id_list
551 */
552 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600553 if (!strcmp(acpi_device_bus_id->bus_id,
554 acpi_device_hid(device))) {
555 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800556 found = 1;
557 kfree(new_bus_id);
558 break;
559 }
560 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600561 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800562 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600563 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800564 acpi_device_bus_id->instance_no = 0;
565 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
566 }
Kay Sievers07944692008-10-30 01:18:59 +0100567 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 +0800568
Len Brown33b57152008-12-15 22:09:26 -0500569 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (device->wakeup.flags.valid)
573 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800574 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Patrick Mochel1890a972006-12-07 20:56:31 +0800576 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000577 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800578 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800579 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600580 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600581 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600582 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800583 goto end;
584 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800585
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800586 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600587 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100588 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
589 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800590
Li Shaohua96333572006-12-07 20:56:46 +0800591 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800592 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600593end:
Shaohua Li90905892009-04-07 10:24:29 +0800594 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500595 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800596 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800597 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800598 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800599 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static void acpi_device_unregister(struct acpi_device *device, int type)
603{
Shaohua Li90905892009-04-07 10:24:29 +0800604 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500605 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800609 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800612
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800613 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800614 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Zhang Rui9e89dde2006-12-07 20:56:16 +0800617/* --------------------------------------------------------------------------
618 Driver Management
619 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500621 * acpi_bus_driver_init - add a device to a driver
622 * @device: the device to add and initialize
623 * @driver: driver for the device
624 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600625 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800626 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
628static int
Len Brown4be44fc2005-08-05 00:44:28 -0400629acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Len Brown4be44fc2005-08-05 00:44:28 -0400631 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400634 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400637 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 result = driver->ops.add(device);
640 if (result) {
641 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700642 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400643 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 device->driver = driver;
647
648 /*
649 * TBD - Configuration Management: Assign resources to device based
650 * upon possible configuration and currently allocated resources.
651 */
652
Len Brown4be44fc2005-08-05 00:44:28 -0400653 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
654 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400655 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700656}
657
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400658static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700659{
660 int result = 0;
661 struct acpi_driver *driver;
662
Rajesh Shah3fb02732005-04-28 00:25:52 -0700663
664 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400665 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (driver->ops.start) {
668 result = driver->ops.start(device);
669 if (result && driver->ops.remove)
670 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
Patrick Mocheld550d982006-06-27 00:41:40 -0400673 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500677 * acpi_bus_register_driver - register a driver with the ACPI bus
678 * @driver: driver being registered
679 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500681 * devices that match the driver's criteria and binds. Returns zero for
682 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
Len Brown4be44fc2005-08-05 00:44:28 -0400684int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Patrick Mochel1890a972006-12-07 20:56:31 +0800686 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400689 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800690 driver->drv.name = driver->name;
691 driver->drv.bus = &acpi_bus_type;
692 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Patrick Mochel1890a972006-12-07 20:56:31 +0800694 ret = driver_register(&driver->drv);
695 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Len Brown4be44fc2005-08-05 00:44:28 -0400698EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500701 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
702 * @driver: driver to unregister
703 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * Unregisters a driver with the ACPI bus. Searches the namespace for all
705 * devices that match the driver's criteria and unbinds.
706 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400707void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Patrick Mochel1890a972006-12-07 20:56:31 +0800709 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
Len Brown4be44fc2005-08-05 00:44:28 -0400711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712EXPORT_SYMBOL(acpi_bus_unregister_driver);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714/* --------------------------------------------------------------------------
715 Device Enumeration
716 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000717static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
718{
719 acpi_status status;
720 int ret;
721 struct acpi_device *device;
722
723 /*
724 * Fixed hardware devices do not appear in the namespace and do not
725 * have handles, but we fabricate acpi_devices for them, so we have
726 * to deal with them specially.
727 */
728 if (handle == NULL)
729 return acpi_root;
730
731 do {
732 status = acpi_get_parent(handle, &handle);
733 if (status == AE_NULL_ENTRY)
734 return NULL;
735 if (ACPI_FAILURE(status))
736 return acpi_root;
737
738 ret = acpi_bus_get_device(handle, &device);
739 if (ret == 0)
740 return device;
741 } while (1);
742}
743
Len Brownc8f7a622006-07-09 17:22:28 -0400744acpi_status
745acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
746{
747 acpi_status status;
748 acpi_handle tmp;
749 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
750 union acpi_object *obj;
751
752 status = acpi_get_handle(handle, "_EJD", &tmp);
753 if (ACPI_FAILURE(status))
754 return status;
755
756 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
757 if (ACPI_SUCCESS(status)) {
758 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100759 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
760 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400761 kfree(buffer.pointer);
762 }
763 return status;
764}
765EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
766
Bob Moore8e4319c2009-06-29 13:43:27 +0800767void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769
770 /* TBD */
771
772 return;
773}
774
Zhang Rui9e89dde2006-12-07 20:56:16 +0800775static int acpi_bus_get_perf_flags(struct acpi_device *device)
776{
777 device->performance.state = ACPI_STATE_UNKNOWN;
778 return 0;
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100782acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
783 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100785 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
786 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100788 acpi_status status;
789 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100791 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return AE_BAD_PARAMETER;
793
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100794 /* _PRW */
795 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
796 if (ACPI_FAILURE(status)) {
797 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
798 return status;
799 }
800
801 package = (union acpi_object *)buffer.pointer;
802
803 if (!package || (package->package.count < 2)) {
804 status = AE_BAD_DATA;
805 goto out;
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100809 if (!element) {
810 status = AE_BAD_DATA;
811 goto out;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (element->type == ACPI_TYPE_PACKAGE) {
814 if ((element->package.count < 2) ||
815 (element->package.elements[0].type !=
816 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100817 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
818 status = AE_BAD_DATA;
819 goto out;
820 }
821 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100823 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 (u32) element->package.elements[1].integer.value;
825 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100826 wakeup->gpe_device = NULL;
827 wakeup->gpe_number = element->integer.value;
828 } else {
829 status = AE_BAD_DATA;
830 goto out;
831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 element = &(package->package.elements[1]);
834 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100835 status = AE_BAD_DATA;
836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100838 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100841 status = AE_NO_MEMORY;
842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100844 wakeup->resources.count = package->package.count - 2;
845 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100847 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
848 status = AE_BAD_DATA;
849 goto out;
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100852 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
Lin Mingbba63a22010-12-13 13:39:17 +0800855 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200856
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100857 out:
858 kfree(buffer.pointer);
859
860 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100863static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200865 struct acpi_device_id button_device_ids[] = {
866 {"PNP0C0D", 0},
867 {"PNP0C0C", 0},
868 {"PNP0C0E", 0},
869 {"", 0},
870 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100871 acpi_status status;
872 acpi_event_status event_status;
873
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100874 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100875
876 /* Power button, Lid switch always enable wakeup */
877 if (!acpi_match_device_ids(device, button_device_ids)) {
878 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100879 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100880 return;
881 }
882
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200883 status = acpi_get_gpe_status(device->wakeup.gpe_device,
884 device->wakeup.gpe_number,
885 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100886 if (status == AE_OK)
887 device->wakeup.flags.run_wake =
888 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
889}
890
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100891static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100892{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100893 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100894 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100895 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200896
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100897 /* Presence of _PRW indicates wake capable */
898 status = acpi_get_handle(device->handle, "_PRW", &temp);
899 if (ACPI_FAILURE(status))
900 return;
901
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100902 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
903 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (ACPI_FAILURE(status)) {
905 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100906 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200910 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100911 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800912 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
913 * system for the ACPI device with the _PRW object.
914 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
915 * So it is necessary to call _DSW object first. Only when it is not
916 * present will the _PSW object used.
917 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200918 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
919 if (psw_error)
920 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
921 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100924static void acpi_bus_add_power_resource(acpi_handle handle);
925
Zhang Rui9e89dde2006-12-07 20:56:16 +0800926static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800928 acpi_status status = 0;
929 acpi_handle handle = NULL;
930 u32 i = 0;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800934 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800936 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800938 device->power.flags.explicit_get = 1;
939 status = acpi_get_handle(device->handle, "_IRC", &handle);
940 if (ACPI_SUCCESS(status))
941 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800944 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800946 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800947 struct acpi_device_power_state *ps = &device->power.states[i];
948 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Zhang Rui9e89dde2006-12-07 20:56:16 +0800950 /* Evaluate "_PRx" to se if power resources are referenced */
951 acpi_evaluate_reference(device->handle, object_name, NULL,
952 &ps->resources);
953 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100954 int j;
955
Zhang Rui9e89dde2006-12-07 20:56:16 +0800956 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100957 for (j = 0; j < ps->resources.count; j++)
958 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Zhang Rui9e89dde2006-12-07 20:56:16 +0800961 /* Evaluate "_PSx" to see if we can do explicit sets */
962 object_name[2] = 'S';
963 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800964 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800965 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800966
Lin Ming1cc0c992012-04-23 09:03:49 +0800967 /*
968 * State is valid if there are means to put the device into it.
969 * D3hot is only valid if _PR3 present.
970 */
971 if (ps->resources.count ||
972 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800973 ps->flags.valid = 1;
974
975 ps->power = -1; /* Unknown - driver assigned */
976 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Zhang Rui9e89dde2006-12-07 20:56:16 +0800979 /* Set defaults for D0 and D3 states (always valid) */
980 device->power.states[ACPI_STATE_D0].flags.valid = 1;
981 device->power.states[ACPI_STATE_D0].power = 100;
982 device->power.states[ACPI_STATE_D3].flags.valid = 1;
983 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200985 /* Set D3cold's explicit_set flag if _PS3 exists. */
986 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
987 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
988
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +0100989 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 return 0;
992}
993
Len Brown4be44fc2005-08-05 00:44:28 -0400994static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Len Brown4be44fc2005-08-05 00:44:28 -0400996 acpi_status status = AE_OK;
997 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /* Presence of _STA indicates 'dynamic_status' */
1001 status = acpi_get_handle(device->handle, "_STA", &temp);
1002 if (ACPI_SUCCESS(status))
1003 device->flags.dynamic_status = 1;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* Presence of _RMV indicates 'removable' */
1006 status = acpi_get_handle(device->handle, "_RMV", &temp);
1007 if (ACPI_SUCCESS(status))
1008 device->flags.removable = 1;
1009
1010 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1011 status = acpi_get_handle(device->handle, "_EJD", &temp);
1012 if (ACPI_SUCCESS(status))
1013 device->flags.ejectable = 1;
1014 else {
1015 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1016 if (ACPI_SUCCESS(status))
1017 device->flags.ejectable = 1;
1018 }
1019
1020 /* Presence of _LCK indicates 'lockable' */
1021 status = acpi_get_handle(device->handle, "_LCK", &temp);
1022 if (ACPI_SUCCESS(status))
1023 device->flags.lockable = 1;
1024
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001025 /* Power resources cannot be power manageable. */
1026 if (device->device_type == ACPI_BUS_TYPE_POWER)
1027 return 0;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1030 status = acpi_get_handle(device->handle, "_PS0", &temp);
1031 if (ACPI_FAILURE(status))
1032 status = acpi_get_handle(device->handle, "_PR0", &temp);
1033 if (ACPI_SUCCESS(status))
1034 device->flags.power_manageable = 1;
1035
Joe Perches3c5f9be42008-02-03 17:06:17 +02001036 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Patrick Mocheld550d982006-06-27 00:41:40 -04001038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001041static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Len Brown4be44fc2005-08-05 00:44:28 -04001043 char bus_id[5] = { '?', 0 };
1044 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1045 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /*
1048 * Bus ID
1049 * ------
1050 * The device's Bus ID is simply the object name.
1051 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1052 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001053 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001055 return;
1056 }
1057
1058 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 case ACPI_BUS_TYPE_POWER_BUTTON:
1060 strcpy(device->pnp.bus_id, "PWRF");
1061 break;
1062 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1063 strcpy(device->pnp.bus_id, "SLPF");
1064 break;
1065 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001066 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* Clean up trailing underscores (if any) */
1068 for (i = 3; i > 1; i--) {
1069 if (bus_id[i] == '_')
1070 bus_id[i] = '\0';
1071 else
1072 break;
1073 }
1074 strcpy(device->pnp.bus_id, bus_id);
1075 break;
1076 }
1077}
1078
Zhang Rui54735262007-01-11 02:09:09 -05001079/*
1080 * acpi_bay_match - see if a device is an ejectable driver bay
1081 *
1082 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1083 * then we can safely call it an ejectable drive bay
1084 */
1085static int acpi_bay_match(struct acpi_device *device){
1086 acpi_status status;
1087 acpi_handle handle;
1088 acpi_handle tmp;
1089 acpi_handle phandle;
1090
1091 handle = device->handle;
1092
1093 status = acpi_get_handle(handle, "_EJ0", &tmp);
1094 if (ACPI_FAILURE(status))
1095 return -ENODEV;
1096
1097 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1098 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1099 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1100 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1101 return 0;
1102
1103 if (acpi_get_parent(handle, &phandle))
1104 return -ENODEV;
1105
1106 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1107 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1108 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1109 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1110 return 0;
1111
1112 return -ENODEV;
1113}
1114
Frank Seidel3620f2f2007-12-07 13:20:34 +01001115/*
1116 * acpi_dock_match - see if a device has a _DCK method
1117 */
1118static int acpi_dock_match(struct acpi_device *device)
1119{
1120 acpi_handle tmp;
1121 return acpi_get_handle(device->handle, "_DCK", &tmp);
1122}
1123
Thomas Renninger620e1122010-10-01 10:54:00 +02001124const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001125{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001126 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001127
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001128 if (list_empty(&device->pnp.ids))
1129 return dummy_hid;
1130
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001131 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1132 return hid->id;
1133}
1134EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001135
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001136static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1137{
1138 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001139
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001140 id = kmalloc(sizeof(*id), GFP_KERNEL);
1141 if (!id)
1142 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001143
Thomas Meyer581de592011-08-06 11:32:56 +02001144 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001145 if (!id->id) {
1146 kfree(id);
1147 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001148 }
1149
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001150 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001151}
1152
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001153/*
1154 * Old IBM workstations have a DSDT bug wherein the SMBus object
1155 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1156 * prefix. Work around this.
1157 */
1158static int acpi_ibm_smbus_match(struct acpi_device *device)
1159{
1160 acpi_handle h_dummy;
1161 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1162 int result;
1163
1164 if (!dmi_name_in_vendors("IBM"))
1165 return -ENODEV;
1166
1167 /* Look for SMBS object */
1168 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1169 if (result)
1170 return result;
1171
1172 if (strcmp("SMBS", path.pointer)) {
1173 result = -ENODEV;
1174 goto out;
1175 }
1176
1177 /* Does it have the necessary (but misnamed) methods? */
1178 result = -ENODEV;
1179 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1180 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1181 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1182 result = 0;
1183out:
1184 kfree(path.pointer);
1185 return result;
1186}
1187
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001188static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Len Brown4be44fc2005-08-05 00:44:28 -04001190 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001191 struct acpi_device_info *info;
1192 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001193 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001195 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001197 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001198 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001199 break;
1200 }
1201
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001202 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001204 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return;
1206 }
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001209 acpi_add_id(device, info->hardware_id.string);
1210 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001211 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001212 for (i = 0; i < cid_list->count; i++)
1213 acpi_add_id(device, cid_list->ids[i].string);
1214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (info->valid & ACPI_VALID_ADR) {
1216 device->pnp.bus_address = info->address;
1217 device->flags.bus_address = 1;
1218 }
Zhang Ruiae843332006-12-07 20:57:10 +08001219
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001220 kfree(info);
1221
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001222 /*
1223 * Some devices don't reliably have _HIDs & _CIDs, so add
1224 * synthetic HIDs to make sure drivers can find them.
1225 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001226 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001227 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001228 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001229 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001230 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001231 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001232 else if (!acpi_ibm_smbus_match(device))
1233 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001234 else if (!acpi_device_hid(device) &&
1235 ACPI_IS_ROOT_DEVICE(device->parent)) {
1236 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1237 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1238 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1239 }
Zhang Rui54735262007-01-11 02:09:09 -05001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 break;
1242 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001243 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
1245 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001246 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001249 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001252 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001255 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001260static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001262 acpi_status status;
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /*
1265 * Context
1266 * -------
1267 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001268 * resolutions from handle->device very efficient. Fixed hardware
1269 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001271 if (!device->handle)
1272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001274 status = acpi_attach_data(device->handle,
1275 acpi_bus_data_handler, device);
1276 if (ACPI_SUCCESS(status))
1277 return 0;
1278
1279 printk(KERN_ERR PREFIX "Error attaching device data\n");
1280 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Len Brown4be44fc2005-08-05 00:44:28 -04001283static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001286 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Li Shaohua96333572006-12-07 20:56:46 +08001288 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001289 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Rui Zhang2786f6e2006-12-21 02:21:13 -05001294 /*
1295 * unbind _ADR-Based Devices when hot removal
1296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (dev->flags.bus_address) {
1298 if ((dev->parent) && (dev->parent->ops.unbind))
1299 dev->parent->ops.unbind(dev);
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1302
Patrick Mocheld550d982006-06-27 00:41:40 -04001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001306static int acpi_add_single_object(struct acpi_device **child,
1307 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001308 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001309 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001311 int result;
1312 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001313 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Burman Yan36bcbec2006-12-19 12:56:11 -08001315 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001317 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001318 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001321 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001322 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001324 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001325 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001326 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001327
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001328 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 /*
1331 * Flags
1332 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001333 * Note that we only look for object handles -- cannot evaluate objects
1334 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
1336 result = acpi_bus_get_flags(device);
1337 if (result)
1338 goto end;
1339
1340 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * Initialize Device
1342 * -----------------
1343 * TBD: Synch with Core's enumeration/initialization process.
1344 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001345 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /*
1348 * Power Management
1349 * ----------------
1350 */
1351 if (device->flags.power_manageable) {
1352 result = acpi_bus_get_power_flags(device);
1353 if (result)
1354 goto end;
1355 }
1356
Len Brown4be44fc2005-08-05 00:44:28 -04001357 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * Wakeup device management
1359 *-----------------------
1360 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001361 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /*
1364 * Performance Management
1365 * ----------------------
1366 */
1367 if (device->flags.performance_manageable) {
1368 result = acpi_bus_get_perf_flags(device);
1369 if (result)
1370 goto end;
1371 }
1372
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001373 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001374 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001376 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001379 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 */
1381 if (device->flags.bus_address) {
1382 if (device->parent && device->parent->ops.bind)
1383 device->parent->ops.bind(device);
1384 }
1385
Alex Chiang0c526d92009-05-14 08:31:37 -06001386end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001387 if (!result) {
1388 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1389 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1390 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1391 (char *) buffer.pointer,
1392 device->parent ? dev_name(&device->parent->dev) :
1393 "(null)"));
1394 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001396 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001397 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Patrick Mocheld550d982006-06-27 00:41:40 -04001399 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001402#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1403 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1404
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001405static void acpi_bus_add_power_resource(acpi_handle handle)
1406{
1407 struct acpi_bus_ops ops = {
1408 .acpi_op_add = 1,
1409 .acpi_op_start = 1,
1410 };
1411 struct acpi_device *device = NULL;
1412
1413 acpi_bus_get_device(handle, &device);
1414 if (!device)
1415 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1416 ACPI_STA_DEFAULT, &ops);
1417}
1418
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001419static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1420 unsigned long long *sta)
1421{
1422 acpi_status status;
1423 acpi_object_type acpi_type;
1424
1425 status = acpi_get_type(handle, &acpi_type);
1426 if (ACPI_FAILURE(status))
1427 return -ENODEV;
1428
1429 switch (acpi_type) {
1430 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1431 case ACPI_TYPE_DEVICE:
1432 *type = ACPI_BUS_TYPE_DEVICE;
1433 status = acpi_bus_get_status_handle(handle, sta);
1434 if (ACPI_FAILURE(status))
1435 return -ENODEV;
1436 break;
1437 case ACPI_TYPE_PROCESSOR:
1438 *type = ACPI_BUS_TYPE_PROCESSOR;
1439 status = acpi_bus_get_status_handle(handle, sta);
1440 if (ACPI_FAILURE(status))
1441 return -ENODEV;
1442 break;
1443 case ACPI_TYPE_THERMAL:
1444 *type = ACPI_BUS_TYPE_THERMAL;
1445 *sta = ACPI_STA_DEFAULT;
1446 break;
1447 case ACPI_TYPE_POWER:
1448 *type = ACPI_BUS_TYPE_POWER;
1449 *sta = ACPI_STA_DEFAULT;
1450 break;
1451 default:
1452 return -ENODEV;
1453 }
1454
1455 return 0;
1456}
1457
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001458static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1459 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001461 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001462 int type;
1463 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001464 struct acpi_device *device;
1465 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001466 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001467
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001468 result = acpi_bus_type_and_status(handle, &type, &sta);
1469 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001470 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001472 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001473 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001474 struct acpi_device_wakeup wakeup;
1475 acpi_handle temp;
1476
1477 status = acpi_get_handle(handle, "_PRW", &temp);
1478 if (ACPI_SUCCESS(status))
1479 acpi_bus_extract_wakeup_device_power_package(handle,
1480 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001481 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001484 /*
1485 * We may already have an acpi_device from a previous enumeration. If
1486 * so, we needn't add it again, but we may still have to start it.
1487 */
1488 device = NULL;
1489 acpi_bus_get_device(handle, &device);
1490 if (ops->acpi_op_add && !device)
1491 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001492
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001493 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001494 return AE_CTRL_DEPTH;
1495
1496 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1497 status = acpi_start_single_object(device);
1498 if (ACPI_FAILURE(status))
1499 return AE_CTRL_DEPTH;
1500 }
1501
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001502 if (!*return_value)
1503 *return_value = device;
1504 return AE_OK;
1505}
1506
1507static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1508 struct acpi_device **child)
1509{
1510 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001511 void *device = NULL;
1512
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001513 status = acpi_bus_check_add(handle, 0, ops, &device);
1514 if (ACPI_SUCCESS(status))
1515 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001516 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001517
1518 if (child)
1519 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001520
1521 if (device)
1522 return 0;
1523 else
1524 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Thomas Renninger77796882010-01-29 17:48:52 +01001527/*
1528 * acpi_bus_add and acpi_bus_start
1529 *
1530 * scan a given ACPI tree and (probably recently hot-plugged)
1531 * create and add or starts found devices.
1532 *
1533 * If no devices were found -ENODEV is returned which does not
1534 * mean that this is a real error, there just have been no suitable
1535 * ACPI objects in the table trunk from which the kernel could create
1536 * a device and add/start an appropriate driver.
1537 */
1538
Rajesh Shah3fb02732005-04-28 00:25:52 -07001539int
Len Brown4be44fc2005-08-05 00:44:28 -04001540acpi_bus_add(struct acpi_device **child,
1541 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001542{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001543 struct acpi_bus_ops ops;
1544
Li Shaohuac4168bf2006-12-07 20:56:41 +08001545 memset(&ops, 0, sizeof(ops));
1546 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001547
Thomas Renninger77796882010-01-29 17:48:52 +01001548 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001549}
1550EXPORT_SYMBOL(acpi_bus_add);
1551
Len Brown4be44fc2005-08-05 00:44:28 -04001552int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001553{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001554 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001555 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001556
Thomas Renningerd2f66502010-01-29 17:48:51 +01001557 if (!device)
1558 return -EINVAL;
1559
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001560 memset(&ops, 0, sizeof(ops));
1561 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001562
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001563 result = acpi_bus_scan(device->handle, &ops, NULL);
1564
Lin Ming3a378982010-12-13 13:36:15 +08001565 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001566
1567 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001568}
1569EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Kristen Accardiceaba662006-02-23 17:56:01 -08001571int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Len Brown4be44fc2005-08-05 00:44:28 -04001573 acpi_status status;
1574 struct acpi_device *parent, *child;
1575 acpi_handle phandle, chandle;
1576 acpi_object_type type;
1577 u32 level = 1;
1578 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Len Brown4be44fc2005-08-05 00:44:28 -04001580 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 phandle = start->handle;
1582 child = chandle = NULL;
1583
1584 while ((level > 0) && parent && (!err)) {
1585 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001586 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
1589 * If this scope is exhausted then move our way back up.
1590 */
1591 if (ACPI_FAILURE(status)) {
1592 level--;
1593 chandle = phandle;
1594 acpi_get_parent(phandle, &phandle);
1595 child = parent;
1596 parent = parent->parent;
1597
1598 if (level == 0)
1599 err = acpi_bus_remove(child, rmdevice);
1600 else
1601 err = acpi_bus_remove(child, 1);
1602
1603 continue;
1604 }
1605
1606 status = acpi_get_type(chandle, &type);
1607 if (ACPI_FAILURE(status)) {
1608 continue;
1609 }
1610 /*
1611 * If there is a device corresponding to chandle then
1612 * parse it (depth-first).
1613 */
1614 if (acpi_bus_get_device(chandle, &child) == 0) {
1615 level++;
1616 phandle = chandle;
1617 chandle = NULL;
1618 parent = child;
1619 }
1620 continue;
1621 }
1622 return err;
1623}
Kristen Accardiceaba662006-02-23 17:56:01 -08001624EXPORT_SYMBOL_GPL(acpi_bus_trim);
1625
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001626static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Len Brown4be44fc2005-08-05 00:44:28 -04001628 int result = 0;
1629 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001630 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Li Shaohuac4168bf2006-12-07 20:56:41 +08001632 memset(&ops, 0, sizeof(ops));
1633 ops.acpi_op_add = 1;
1634 ops.acpi_op_start = 1;
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /*
1637 * Enumerate all fixed-feature devices.
1638 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001639 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001640 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001641 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001642 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001643 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001644 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001647 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001648 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001649 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001650 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001651 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Patrick Mocheld550d982006-06-27 00:41:40 -04001654 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Bjorn Helgaase747f272009-03-24 16:49:43 -06001657int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001660 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Li Shaohuac4168bf2006-12-07 20:56:41 +08001662 memset(&ops, 0, sizeof(ops));
1663 ops.acpi_op_add = 1;
1664 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Patrick Mochel5b327262006-05-10 10:33:00 -04001666 result = bus_register(&acpi_bus_type);
1667 if (result) {
1668 /* We don't want to quit even if we failed to add suspend/resume */
1669 printk(KERN_ERR PREFIX "Could not register bus type\n");
1670 }
1671
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001672 acpi_power_init();
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * Enumerate devices in the ACPI namespace.
1676 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001677 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001678
Li Shaohuac4168bf2006-12-07 20:56:41 +08001679 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001680 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (result)
1683 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001684 else
Lin Ming3a378982010-12-13 13:36:15 +08001685 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Patrick Mocheld550d982006-06-27 00:41:40 -04001687 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}