blob: daa88d527e8463e5a6637d69ab8726795862f5f8 [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);
Lv Zhengccf78042012-10-30 14:41:07 +0100384 kfree(acpi_dev->pnp.unique_id);
Patrick Mochel1890a972006-12-07 20:56:31 +0800385 kfree(acpi_dev);
386}
387
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800388static int acpi_bus_match(struct device *dev, struct device_driver *drv)
389{
390 struct acpi_device *acpi_dev = to_acpi_device(dev);
391 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
392
Thomas Renninger29b71a12007-07-23 14:43:51 +0200393 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800394}
395
Kay Sievers7eff2e72007-08-14 15:15:12 +0200396static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800397{
398 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200399 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800400
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200401 if (list_empty(&acpi_dev->pnp.ids))
402 return 0;
403
Kay Sievers7eff2e72007-08-14 15:15:12 +0200404 if (add_uevent_var(env, "MODALIAS="))
405 return -ENOMEM;
406 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
407 sizeof(env->buf) - env->buflen);
408 if (len >= (sizeof(env->buf) - env->buflen))
409 return -ENOMEM;
410 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
412}
413
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000414static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
415{
416 struct acpi_device *device = data;
417
418 device->driver->ops.notify(device, event);
419}
420
421static acpi_status acpi_device_notify_fixed(void *data)
422{
423 struct acpi_device *device = data;
424
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000425 /* Fixed hardware devices have no handles */
426 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000427 return AE_OK;
428}
429
430static int acpi_device_install_notify_handler(struct acpi_device *device)
431{
432 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000433
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000434 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000435 status =
436 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
437 acpi_device_notify_fixed,
438 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000439 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000440 status =
441 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
442 acpi_device_notify_fixed,
443 device);
444 else
445 status = acpi_install_notify_handler(device->handle,
446 ACPI_DEVICE_NOTIFY,
447 acpi_device_notify,
448 device);
449
450 if (ACPI_FAILURE(status))
451 return -EINVAL;
452 return 0;
453}
454
455static void acpi_device_remove_notify_handler(struct acpi_device *device)
456{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000457 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000458 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
459 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000460 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000461 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
462 acpi_device_notify_fixed);
463 else
464 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
465 acpi_device_notify);
466}
467
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800468static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
469static int acpi_start_single_object(struct acpi_device *);
470static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800471{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800472 struct acpi_device *acpi_dev = to_acpi_device(dev);
473 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
474 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800476 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
477 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800478 if (acpi_dev->bus_ops.acpi_op_start)
479 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000480
481 if (acpi_drv->ops.notify) {
482 ret = acpi_device_install_notify_handler(acpi_dev);
483 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000484 if (acpi_drv->ops.remove)
485 acpi_drv->ops.remove(acpi_dev,
486 acpi_dev->removal_type);
487 return ret;
488 }
489 }
490
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800491 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
492 "Found driver [%s] for device [%s]\n",
493 acpi_drv->name, acpi_dev->pnp.bus_id));
494 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800495 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800496 return ret;
497}
498
499static int acpi_device_remove(struct device * dev)
500{
501 struct acpi_device *acpi_dev = to_acpi_device(dev);
502 struct acpi_driver *acpi_drv = acpi_dev->driver;
503
504 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000505 if (acpi_drv->ops.notify)
506 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800507 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800508 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800509 }
510 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700511 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800512
513 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800514 return 0;
515}
516
David Brownell55955aa2007-05-08 00:28:35 -0700517struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800518 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800519 .match = acpi_bus_match,
520 .probe = acpi_device_probe,
521 .remove = acpi_device_remove,
522 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523};
524
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000525static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800527 int result;
528 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
529 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
532 * Linkage
533 * -------
534 * Link this device to its parent and siblings.
535 */
536 INIT_LIST_HEAD(&device->children);
537 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800539 INIT_LIST_HEAD(&device->physical_node_list);
540 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800542 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
543 if (!new_bus_id) {
544 printk(KERN_ERR PREFIX "Memory allocation error\n");
545 return -ENOMEM;
546 }
547
Shaohua Li90905892009-04-07 10:24:29 +0800548 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800549 /*
550 * Find suitable bus_id and instance number in acpi_bus_id_list
551 * If failed, create one and link it into acpi_bus_id_list
552 */
553 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600554 if (!strcmp(acpi_device_bus_id->bus_id,
555 acpi_device_hid(device))) {
556 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800557 found = 1;
558 kfree(new_bus_id);
559 break;
560 }
561 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600562 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800563 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600564 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800565 acpi_device_bus_id->instance_no = 0;
566 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
567 }
Kay Sievers07944692008-10-30 01:18:59 +0100568 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 +0800569
Len Brown33b57152008-12-15 22:09:26 -0500570 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (device->wakeup.flags.valid)
574 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800575 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Patrick Mochel1890a972006-12-07 20:56:31 +0800577 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000578 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800579 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800580 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600581 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600582 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600583 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800584 goto end;
585 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800586
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800587 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600588 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100589 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
590 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800591
Li Shaohua96333572006-12-07 20:56:46 +0800592 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800593 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600594end:
Shaohua Li90905892009-04-07 10:24:29 +0800595 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500596 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800597 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800598 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800599 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800600 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static void acpi_device_unregister(struct acpi_device *device, int type)
604{
Shaohua Li90905892009-04-07 10:24:29 +0800605 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500606 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800610 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800613
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800614 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800615 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Zhang Rui9e89dde2006-12-07 20:56:16 +0800618/* --------------------------------------------------------------------------
619 Driver Management
620 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500622 * acpi_bus_driver_init - add a device to a driver
623 * @device: the device to add and initialize
624 * @driver: driver for the device
625 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600626 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800627 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629static int
Len Brown4be44fc2005-08-05 00:44:28 -0400630acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Len Brown4be44fc2005-08-05 00:44:28 -0400632 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400635 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400638 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 result = driver->ops.add(device);
641 if (result) {
642 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700643 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400644 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 device->driver = driver;
648
649 /*
650 * TBD - Configuration Management: Assign resources to device based
651 * upon possible configuration and currently allocated resources.
652 */
653
Len Brown4be44fc2005-08-05 00:44:28 -0400654 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
655 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400656 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700657}
658
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400659static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700660{
661 int result = 0;
662 struct acpi_driver *driver;
663
Rajesh Shah3fb02732005-04-28 00:25:52 -0700664
665 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (driver->ops.start) {
669 result = driver->ops.start(device);
670 if (result && driver->ops.remove)
671 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
Patrick Mocheld550d982006-06-27 00:41:40 -0400674 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500678 * acpi_bus_register_driver - register a driver with the ACPI bus
679 * @driver: driver being registered
680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500682 * devices that match the driver's criteria and binds. Returns zero for
683 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Len Brown4be44fc2005-08-05 00:44:28 -0400685int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Patrick Mochel1890a972006-12-07 20:56:31 +0800687 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400690 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800691 driver->drv.name = driver->name;
692 driver->drv.bus = &acpi_bus_type;
693 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Patrick Mochel1890a972006-12-07 20:56:31 +0800695 ret = driver_register(&driver->drv);
696 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Len Brown4be44fc2005-08-05 00:44:28 -0400699EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500702 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
703 * @driver: driver to unregister
704 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * Unregisters a driver with the ACPI bus. Searches the namespace for all
706 * devices that match the driver's criteria and unbinds.
707 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400708void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Patrick Mochel1890a972006-12-07 20:56:31 +0800710 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
Len Brown4be44fc2005-08-05 00:44:28 -0400712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713EXPORT_SYMBOL(acpi_bus_unregister_driver);
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/* --------------------------------------------------------------------------
716 Device Enumeration
717 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000718static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
719{
720 acpi_status status;
721 int ret;
722 struct acpi_device *device;
723
724 /*
725 * Fixed hardware devices do not appear in the namespace and do not
726 * have handles, but we fabricate acpi_devices for them, so we have
727 * to deal with them specially.
728 */
729 if (handle == NULL)
730 return acpi_root;
731
732 do {
733 status = acpi_get_parent(handle, &handle);
734 if (status == AE_NULL_ENTRY)
735 return NULL;
736 if (ACPI_FAILURE(status))
737 return acpi_root;
738
739 ret = acpi_bus_get_device(handle, &device);
740 if (ret == 0)
741 return device;
742 } while (1);
743}
744
Len Brownc8f7a622006-07-09 17:22:28 -0400745acpi_status
746acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
747{
748 acpi_status status;
749 acpi_handle tmp;
750 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
751 union acpi_object *obj;
752
753 status = acpi_get_handle(handle, "_EJD", &tmp);
754 if (ACPI_FAILURE(status))
755 return status;
756
757 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
758 if (ACPI_SUCCESS(status)) {
759 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100760 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
761 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400762 kfree(buffer.pointer);
763 }
764 return status;
765}
766EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
767
Bob Moore8e4319c2009-06-29 13:43:27 +0800768void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770
771 /* TBD */
772
773 return;
774}
775
Zhang Rui9e89dde2006-12-07 20:56:16 +0800776static int acpi_bus_get_perf_flags(struct acpi_device *device)
777{
778 device->performance.state = ACPI_STATE_UNKNOWN;
779 return 0;
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100783acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
784 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100786 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
787 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100789 acpi_status status;
790 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100792 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return AE_BAD_PARAMETER;
794
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100795 /* _PRW */
796 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
797 if (ACPI_FAILURE(status)) {
798 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
799 return status;
800 }
801
802 package = (union acpi_object *)buffer.pointer;
803
804 if (!package || (package->package.count < 2)) {
805 status = AE_BAD_DATA;
806 goto out;
807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100810 if (!element) {
811 status = AE_BAD_DATA;
812 goto out;
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (element->type == ACPI_TYPE_PACKAGE) {
815 if ((element->package.count < 2) ||
816 (element->package.elements[0].type !=
817 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100818 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
819 status = AE_BAD_DATA;
820 goto out;
821 }
822 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100824 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 (u32) element->package.elements[1].integer.value;
826 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100827 wakeup->gpe_device = NULL;
828 wakeup->gpe_number = element->integer.value;
829 } else {
830 status = AE_BAD_DATA;
831 goto out;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 element = &(package->package.elements[1]);
835 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100836 status = AE_BAD_DATA;
837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100839 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100842 status = AE_NO_MEMORY;
843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100845 wakeup->resources.count = package->package.count - 2;
846 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100848 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
849 status = AE_BAD_DATA;
850 goto out;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100853 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
Lin Mingbba63a22010-12-13 13:39:17 +0800856 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200857
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100858 out:
859 kfree(buffer.pointer);
860
861 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100864static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200866 struct acpi_device_id button_device_ids[] = {
867 {"PNP0C0D", 0},
868 {"PNP0C0C", 0},
869 {"PNP0C0E", 0},
870 {"", 0},
871 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100872 acpi_status status;
873 acpi_event_status event_status;
874
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100875 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100876
877 /* Power button, Lid switch always enable wakeup */
878 if (!acpi_match_device_ids(device, button_device_ids)) {
879 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100880 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100881 return;
882 }
883
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200884 status = acpi_get_gpe_status(device->wakeup.gpe_device,
885 device->wakeup.gpe_number,
886 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100887 if (status == AE_OK)
888 device->wakeup.flags.run_wake =
889 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
890}
891
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100892static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100893{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100894 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100895 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100896 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200897
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100898 /* Presence of _PRW indicates wake capable */
899 status = acpi_get_handle(device->handle, "_PRW", &temp);
900 if (ACPI_FAILURE(status))
901 return;
902
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100903 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
904 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (ACPI_FAILURE(status)) {
906 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100907 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200911 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100912 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800913 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
914 * system for the ACPI device with the _PRW object.
915 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
916 * So it is necessary to call _DSW object first. Only when it is not
917 * present will the _PSW object used.
918 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200919 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
920 if (psw_error)
921 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
922 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100925static void acpi_bus_add_power_resource(acpi_handle handle);
926
Zhang Rui9e89dde2006-12-07 20:56:16 +0800927static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800929 acpi_status status = 0;
930 acpi_handle handle = NULL;
931 u32 i = 0;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800935 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800937 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800939 device->power.flags.explicit_get = 1;
940 status = acpi_get_handle(device->handle, "_IRC", &handle);
941 if (ACPI_SUCCESS(status))
942 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800945 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800947 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800948 struct acpi_device_power_state *ps = &device->power.states[i];
949 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Zhang Rui9e89dde2006-12-07 20:56:16 +0800951 /* Evaluate "_PRx" to se if power resources are referenced */
952 acpi_evaluate_reference(device->handle, object_name, NULL,
953 &ps->resources);
954 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100955 int j;
956
Zhang Rui9e89dde2006-12-07 20:56:16 +0800957 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100958 for (j = 0; j < ps->resources.count; j++)
959 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Zhang Rui9e89dde2006-12-07 20:56:16 +0800962 /* Evaluate "_PSx" to see if we can do explicit sets */
963 object_name[2] = 'S';
964 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800965 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800966 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800967
Lin Ming1cc0c992012-04-23 09:03:49 +0800968 /*
969 * State is valid if there are means to put the device into it.
970 * D3hot is only valid if _PR3 present.
971 */
972 if (ps->resources.count ||
973 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800974 ps->flags.valid = 1;
975
976 ps->power = -1; /* Unknown - driver assigned */
977 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Zhang Rui9e89dde2006-12-07 20:56:16 +0800980 /* Set defaults for D0 and D3 states (always valid) */
981 device->power.states[ACPI_STATE_D0].flags.valid = 1;
982 device->power.states[ACPI_STATE_D0].power = 100;
983 device->power.states[ACPI_STATE_D3].flags.valid = 1;
984 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200986 /* Set D3cold's explicit_set flag if _PS3 exists. */
987 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
988 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
989
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +0100990 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 return 0;
993}
994
Len Brown4be44fc2005-08-05 00:44:28 -0400995static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Len Brown4be44fc2005-08-05 00:44:28 -0400997 acpi_status status = AE_OK;
998 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 /* Presence of _STA indicates 'dynamic_status' */
1002 status = acpi_get_handle(device->handle, "_STA", &temp);
1003 if (ACPI_SUCCESS(status))
1004 device->flags.dynamic_status = 1;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* Presence of _RMV indicates 'removable' */
1007 status = acpi_get_handle(device->handle, "_RMV", &temp);
1008 if (ACPI_SUCCESS(status))
1009 device->flags.removable = 1;
1010
1011 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1012 status = acpi_get_handle(device->handle, "_EJD", &temp);
1013 if (ACPI_SUCCESS(status))
1014 device->flags.ejectable = 1;
1015 else {
1016 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1017 if (ACPI_SUCCESS(status))
1018 device->flags.ejectable = 1;
1019 }
1020
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001021 /* Power resources cannot be power manageable. */
1022 if (device->device_type == ACPI_BUS_TYPE_POWER)
1023 return 0;
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1026 status = acpi_get_handle(device->handle, "_PS0", &temp);
1027 if (ACPI_FAILURE(status))
1028 status = acpi_get_handle(device->handle, "_PR0", &temp);
1029 if (ACPI_SUCCESS(status))
1030 device->flags.power_manageable = 1;
1031
Joe Perches3c5f9be42008-02-03 17:06:17 +02001032 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Patrick Mocheld550d982006-06-27 00:41:40 -04001034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001037static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Len Brown4be44fc2005-08-05 00:44:28 -04001039 char bus_id[5] = { '?', 0 };
1040 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1041 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /*
1044 * Bus ID
1045 * ------
1046 * The device's Bus ID is simply the object name.
1047 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1048 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001049 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001051 return;
1052 }
1053
1054 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 case ACPI_BUS_TYPE_POWER_BUTTON:
1056 strcpy(device->pnp.bus_id, "PWRF");
1057 break;
1058 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1059 strcpy(device->pnp.bus_id, "SLPF");
1060 break;
1061 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001062 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* Clean up trailing underscores (if any) */
1064 for (i = 3; i > 1; i--) {
1065 if (bus_id[i] == '_')
1066 bus_id[i] = '\0';
1067 else
1068 break;
1069 }
1070 strcpy(device->pnp.bus_id, bus_id);
1071 break;
1072 }
1073}
1074
Zhang Rui54735262007-01-11 02:09:09 -05001075/*
1076 * acpi_bay_match - see if a device is an ejectable driver bay
1077 *
1078 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1079 * then we can safely call it an ejectable drive bay
1080 */
1081static int acpi_bay_match(struct acpi_device *device){
1082 acpi_status status;
1083 acpi_handle handle;
1084 acpi_handle tmp;
1085 acpi_handle phandle;
1086
1087 handle = device->handle;
1088
1089 status = acpi_get_handle(handle, "_EJ0", &tmp);
1090 if (ACPI_FAILURE(status))
1091 return -ENODEV;
1092
1093 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1094 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1095 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1096 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1097 return 0;
1098
1099 if (acpi_get_parent(handle, &phandle))
1100 return -ENODEV;
1101
1102 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1103 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1104 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1105 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1106 return 0;
1107
1108 return -ENODEV;
1109}
1110
Frank Seidel3620f2f2007-12-07 13:20:34 +01001111/*
1112 * acpi_dock_match - see if a device has a _DCK method
1113 */
1114static int acpi_dock_match(struct acpi_device *device)
1115{
1116 acpi_handle tmp;
1117 return acpi_get_handle(device->handle, "_DCK", &tmp);
1118}
1119
Thomas Renninger620e1122010-10-01 10:54:00 +02001120const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001121{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001122 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001123
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001124 if (list_empty(&device->pnp.ids))
1125 return dummy_hid;
1126
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001127 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1128 return hid->id;
1129}
1130EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001131
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001132static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1133{
1134 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001135
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001136 id = kmalloc(sizeof(*id), GFP_KERNEL);
1137 if (!id)
1138 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001139
Thomas Meyer581de592011-08-06 11:32:56 +02001140 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001141 if (!id->id) {
1142 kfree(id);
1143 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001144 }
1145
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001146 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001147}
1148
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001149/*
1150 * Old IBM workstations have a DSDT bug wherein the SMBus object
1151 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1152 * prefix. Work around this.
1153 */
1154static int acpi_ibm_smbus_match(struct acpi_device *device)
1155{
1156 acpi_handle h_dummy;
1157 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1158 int result;
1159
1160 if (!dmi_name_in_vendors("IBM"))
1161 return -ENODEV;
1162
1163 /* Look for SMBS object */
1164 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1165 if (result)
1166 return result;
1167
1168 if (strcmp("SMBS", path.pointer)) {
1169 result = -ENODEV;
1170 goto out;
1171 }
1172
1173 /* Does it have the necessary (but misnamed) methods? */
1174 result = -ENODEV;
1175 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1176 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1177 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1178 result = 0;
1179out:
1180 kfree(path.pointer);
1181 return result;
1182}
1183
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001184static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Len Brown4be44fc2005-08-05 00:44:28 -04001186 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001187 struct acpi_device_info *info;
1188 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001189 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001191 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001193 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001194 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001195 break;
1196 }
1197
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001198 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001200 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return;
1202 }
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001205 acpi_add_id(device, info->hardware_id.string);
1206 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001207 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001208 for (i = 0; i < cid_list->count; i++)
1209 acpi_add_id(device, cid_list->ids[i].string);
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (info->valid & ACPI_VALID_ADR) {
1212 device->pnp.bus_address = info->address;
1213 device->flags.bus_address = 1;
1214 }
Lv Zhengccf78042012-10-30 14:41:07 +01001215 if (info->valid & ACPI_VALID_UID)
1216 device->pnp.unique_id = kstrdup(info->unique_id.string,
1217 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001218
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001219 kfree(info);
1220
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001221 /*
1222 * Some devices don't reliably have _HIDs & _CIDs, so add
1223 * synthetic HIDs to make sure drivers can find them.
1224 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001225 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001226 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001227 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001228 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001229 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001230 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001231 else if (!acpi_ibm_smbus_match(device))
1232 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001233 else if (!acpi_device_hid(device) &&
1234 ACPI_IS_ROOT_DEVICE(device->parent)) {
1235 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1236 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1237 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1238 }
Zhang Rui54735262007-01-11 02:09:09 -05001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 break;
1241 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001242 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 break;
1244 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001245 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001248 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 break;
1250 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001251 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 break;
1253 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001254 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001259static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001261 acpi_status status;
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /*
1264 * Context
1265 * -------
1266 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001267 * resolutions from handle->device very efficient. Fixed hardware
1268 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001270 if (!device->handle)
1271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001273 status = acpi_attach_data(device->handle,
1274 acpi_bus_data_handler, device);
1275 if (ACPI_SUCCESS(status))
1276 return 0;
1277
1278 printk(KERN_ERR PREFIX "Error attaching device data\n");
1279 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Len Brown4be44fc2005-08-05 00:44:28 -04001282static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Li Shaohua96333572006-12-07 20:56:46 +08001287 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001288 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Rui Zhang2786f6e2006-12-21 02:21:13 -05001293 /*
1294 * unbind _ADR-Based Devices when hot removal
1295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (dev->flags.bus_address) {
1297 if ((dev->parent) && (dev->parent->ops.unbind))
1298 dev->parent->ops.unbind(dev);
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1301
Patrick Mocheld550d982006-06-27 00:41:40 -04001302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001305static int acpi_add_single_object(struct acpi_device **child,
1306 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001307 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001308 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001310 int result;
1311 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001312 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Burman Yan36bcbec2006-12-19 12:56:11 -08001314 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001316 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001317 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001320 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001321 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001323 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001324 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001325 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001326
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001327 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /*
1330 * Flags
1331 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001332 * Note that we only look for object handles -- cannot evaluate objects
1333 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 */
1335 result = acpi_bus_get_flags(device);
1336 if (result)
1337 goto end;
1338
1339 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 * Initialize Device
1341 * -----------------
1342 * TBD: Synch with Core's enumeration/initialization process.
1343 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001344 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /*
1347 * Power Management
1348 * ----------------
1349 */
1350 if (device->flags.power_manageable) {
1351 result = acpi_bus_get_power_flags(device);
1352 if (result)
1353 goto end;
1354 }
1355
Len Brown4be44fc2005-08-05 00:44:28 -04001356 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 * Wakeup device management
1358 *-----------------------
1359 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001360 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /*
1363 * Performance Management
1364 * ----------------------
1365 */
1366 if (device->flags.performance_manageable) {
1367 result = acpi_bus_get_perf_flags(device);
1368 if (result)
1369 goto end;
1370 }
1371
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001372 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001373 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001375 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001378 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 */
1380 if (device->flags.bus_address) {
1381 if (device->parent && device->parent->ops.bind)
1382 device->parent->ops.bind(device);
1383 }
1384
Alex Chiang0c526d92009-05-14 08:31:37 -06001385end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001386 if (!result) {
1387 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1388 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1389 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1390 (char *) buffer.pointer,
1391 device->parent ? dev_name(&device->parent->dev) :
1392 "(null)"));
1393 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001395 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001396 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Patrick Mocheld550d982006-06-27 00:41:40 -04001398 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001401#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1402 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1403
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001404static void acpi_bus_add_power_resource(acpi_handle handle)
1405{
1406 struct acpi_bus_ops ops = {
1407 .acpi_op_add = 1,
1408 .acpi_op_start = 1,
1409 };
1410 struct acpi_device *device = NULL;
1411
1412 acpi_bus_get_device(handle, &device);
1413 if (!device)
1414 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1415 ACPI_STA_DEFAULT, &ops);
1416}
1417
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001418static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1419 unsigned long long *sta)
1420{
1421 acpi_status status;
1422 acpi_object_type acpi_type;
1423
1424 status = acpi_get_type(handle, &acpi_type);
1425 if (ACPI_FAILURE(status))
1426 return -ENODEV;
1427
1428 switch (acpi_type) {
1429 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1430 case ACPI_TYPE_DEVICE:
1431 *type = ACPI_BUS_TYPE_DEVICE;
1432 status = acpi_bus_get_status_handle(handle, sta);
1433 if (ACPI_FAILURE(status))
1434 return -ENODEV;
1435 break;
1436 case ACPI_TYPE_PROCESSOR:
1437 *type = ACPI_BUS_TYPE_PROCESSOR;
1438 status = acpi_bus_get_status_handle(handle, sta);
1439 if (ACPI_FAILURE(status))
1440 return -ENODEV;
1441 break;
1442 case ACPI_TYPE_THERMAL:
1443 *type = ACPI_BUS_TYPE_THERMAL;
1444 *sta = ACPI_STA_DEFAULT;
1445 break;
1446 case ACPI_TYPE_POWER:
1447 *type = ACPI_BUS_TYPE_POWER;
1448 *sta = ACPI_STA_DEFAULT;
1449 break;
1450 default:
1451 return -ENODEV;
1452 }
1453
1454 return 0;
1455}
1456
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001457static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1458 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001460 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001461 int type;
1462 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001463 struct acpi_device *device;
1464 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001465 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001466
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001467 result = acpi_bus_type_and_status(handle, &type, &sta);
1468 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001469 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001471 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001472 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001473 struct acpi_device_wakeup wakeup;
1474 acpi_handle temp;
1475
1476 status = acpi_get_handle(handle, "_PRW", &temp);
1477 if (ACPI_SUCCESS(status))
1478 acpi_bus_extract_wakeup_device_power_package(handle,
1479 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001480 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001483 /*
1484 * We may already have an acpi_device from a previous enumeration. If
1485 * so, we needn't add it again, but we may still have to start it.
1486 */
1487 device = NULL;
1488 acpi_bus_get_device(handle, &device);
1489 if (ops->acpi_op_add && !device)
1490 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001491
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001492 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001493 return AE_CTRL_DEPTH;
1494
1495 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1496 status = acpi_start_single_object(device);
1497 if (ACPI_FAILURE(status))
1498 return AE_CTRL_DEPTH;
1499 }
1500
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001501 if (!*return_value)
1502 *return_value = device;
1503 return AE_OK;
1504}
1505
1506static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1507 struct acpi_device **child)
1508{
1509 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001510 void *device = NULL;
1511
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001512 status = acpi_bus_check_add(handle, 0, ops, &device);
1513 if (ACPI_SUCCESS(status))
1514 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001515 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001516
1517 if (child)
1518 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001519
1520 if (device)
1521 return 0;
1522 else
1523 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Thomas Renninger77796882010-01-29 17:48:52 +01001526/*
1527 * acpi_bus_add and acpi_bus_start
1528 *
1529 * scan a given ACPI tree and (probably recently hot-plugged)
1530 * create and add or starts found devices.
1531 *
1532 * If no devices were found -ENODEV is returned which does not
1533 * mean that this is a real error, there just have been no suitable
1534 * ACPI objects in the table trunk from which the kernel could create
1535 * a device and add/start an appropriate driver.
1536 */
1537
Rajesh Shah3fb02732005-04-28 00:25:52 -07001538int
Len Brown4be44fc2005-08-05 00:44:28 -04001539acpi_bus_add(struct acpi_device **child,
1540 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001541{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001542 struct acpi_bus_ops ops;
1543
Li Shaohuac4168bf2006-12-07 20:56:41 +08001544 memset(&ops, 0, sizeof(ops));
1545 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001546
Thomas Renninger77796882010-01-29 17:48:52 +01001547 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001548}
1549EXPORT_SYMBOL(acpi_bus_add);
1550
Len Brown4be44fc2005-08-05 00:44:28 -04001551int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001552{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001553 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001554 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001555
Thomas Renningerd2f66502010-01-29 17:48:51 +01001556 if (!device)
1557 return -EINVAL;
1558
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001559 memset(&ops, 0, sizeof(ops));
1560 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001561
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001562 result = acpi_bus_scan(device->handle, &ops, NULL);
1563
Lin Ming3a378982010-12-13 13:36:15 +08001564 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001565
1566 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001567}
1568EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Kristen Accardiceaba662006-02-23 17:56:01 -08001570int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Len Brown4be44fc2005-08-05 00:44:28 -04001572 acpi_status status;
1573 struct acpi_device *parent, *child;
1574 acpi_handle phandle, chandle;
1575 acpi_object_type type;
1576 u32 level = 1;
1577 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Len Brown4be44fc2005-08-05 00:44:28 -04001579 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 phandle = start->handle;
1581 child = chandle = NULL;
1582
1583 while ((level > 0) && parent && (!err)) {
1584 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001585 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 /*
1588 * If this scope is exhausted then move our way back up.
1589 */
1590 if (ACPI_FAILURE(status)) {
1591 level--;
1592 chandle = phandle;
1593 acpi_get_parent(phandle, &phandle);
1594 child = parent;
1595 parent = parent->parent;
1596
1597 if (level == 0)
1598 err = acpi_bus_remove(child, rmdevice);
1599 else
1600 err = acpi_bus_remove(child, 1);
1601
1602 continue;
1603 }
1604
1605 status = acpi_get_type(chandle, &type);
1606 if (ACPI_FAILURE(status)) {
1607 continue;
1608 }
1609 /*
1610 * If there is a device corresponding to chandle then
1611 * parse it (depth-first).
1612 */
1613 if (acpi_bus_get_device(chandle, &child) == 0) {
1614 level++;
1615 phandle = chandle;
1616 chandle = NULL;
1617 parent = child;
1618 }
1619 continue;
1620 }
1621 return err;
1622}
Kristen Accardiceaba662006-02-23 17:56:01 -08001623EXPORT_SYMBOL_GPL(acpi_bus_trim);
1624
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001625static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Len Brown4be44fc2005-08-05 00:44:28 -04001627 int result = 0;
1628 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001629 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Li Shaohuac4168bf2006-12-07 20:56:41 +08001631 memset(&ops, 0, sizeof(ops));
1632 ops.acpi_op_add = 1;
1633 ops.acpi_op_start = 1;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * Enumerate all fixed-feature devices.
1637 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001638 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001639 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001640 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001641 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001642 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001643 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001646 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001647 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001648 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001649 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001650 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Patrick Mocheld550d982006-06-27 00:41:40 -04001653 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Bjorn Helgaase747f272009-03-24 16:49:43 -06001656int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001659 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Li Shaohuac4168bf2006-12-07 20:56:41 +08001661 memset(&ops, 0, sizeof(ops));
1662 ops.acpi_op_add = 1;
1663 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Patrick Mochel5b327262006-05-10 10:33:00 -04001665 result = bus_register(&acpi_bus_type);
1666 if (result) {
1667 /* We don't want to quit even if we failed to add suspend/resume */
1668 printk(KERN_ERR PREFIX "Could not register bus type\n");
1669 }
1670
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001671 acpi_power_init();
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 * Enumerate devices in the ACPI namespace.
1675 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001676 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001677
Li Shaohuac4168bf2006-12-07 20:56:41 +08001678 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001679 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 if (result)
1682 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001683 else
Lin Ming3a378982010-12-13 13:36:15 +08001684 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Patrick Mocheld550d982006-06-27 00:41:40 -04001686 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}