blob: 86bfa690bc6fcc51f8f60f3b9b78470e0d28e7d7 [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
Lv Zheng923d4a42012-10-30 14:43:26 +0100223static ssize_t acpi_device_uid_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct acpi_device *acpi_dev = to_acpi_device(dev);
227
228 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
229}
230static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
231
232static ssize_t acpi_device_adr_show(struct device *dev,
233 struct device_attribute *attr, char *buf)
234{
235 struct acpi_device *acpi_dev = to_acpi_device(dev);
236
237 return sprintf(buf, "0x%08x\n",
238 (unsigned int)(acpi_dev->pnp.bus_address));
239}
240static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
241
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800242static ssize_t
243acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
244 struct acpi_device *acpi_dev = to_acpi_device(dev);
245 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
246 int result;
247
248 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600249 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800250 goto end;
251
252 result = sprintf(buf, "%s\n", (char*)path.pointer);
253 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600254end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800255 return result;
256}
257static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
258
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600259/* sysfs file that shows description text from the ACPI _STR method */
260static ssize_t description_show(struct device *dev,
261 struct device_attribute *attr,
262 char *buf) {
263 struct acpi_device *acpi_dev = to_acpi_device(dev);
264 int result;
265
266 if (acpi_dev->pnp.str_obj == NULL)
267 return 0;
268
269 /*
270 * The _STR object contains a Unicode identifier for a device.
271 * We need to convert to utf-8 so it can be displayed.
272 */
273 result = utf16s_to_utf8s(
274 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
275 acpi_dev->pnp.str_obj->buffer.length,
276 UTF16_LITTLE_ENDIAN, buf,
277 PAGE_SIZE);
278
279 buf[result++] = '\n';
280
281 return result;
282}
283static DEVICE_ATTR(description, 0444, description_show, NULL);
284
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800285static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600287 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800288 acpi_status status;
289 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800290 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800292 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800293 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800294 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600295 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800296 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600297 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800298 goto end;
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200301 if (!list_empty(&dev->pnp.ids)) {
302 result = device_create_file(&dev->dev, &dev_attr_hid);
303 if (result)
304 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200306 result = device_create_file(&dev->dev, &dev_attr_modalias);
307 if (result)
308 goto end;
309 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200310
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600311 /*
312 * If device has _STR, 'description' file is created
313 */
314 status = acpi_get_handle(dev->handle, "_STR", &temp);
315 if (ACPI_SUCCESS(status)) {
316 status = acpi_evaluate_object(dev->handle, "_STR",
317 NULL, &buffer);
318 if (ACPI_FAILURE(status))
319 buffer.pointer = NULL;
320 dev->pnp.str_obj = buffer.pointer;
321 result = device_create_file(&dev->dev, &dev_attr_description);
322 if (result)
323 goto end;
324 }
325
Lv Zheng923d4a42012-10-30 14:43:26 +0100326 if (dev->flags.bus_address)
327 result = device_create_file(&dev->dev, &dev_attr_adr);
328 if (dev->pnp.unique_id)
329 result = device_create_file(&dev->dev, &dev_attr_uid);
330
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800331 /*
332 * If device has _EJ0, 'eject' file is created that is used to trigger
333 * hot-removal function from userland.
334 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800335 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
336 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800337 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600338end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800339 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800342static void acpi_device_remove_files(struct acpi_device *dev)
343{
344 acpi_status status;
345 acpi_handle temp;
346
347 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600348 * If device has _STR, remove 'description' file
349 */
350 status = acpi_get_handle(dev->handle, "_STR", &temp);
351 if (ACPI_SUCCESS(status)) {
352 kfree(dev->pnp.str_obj);
353 device_remove_file(&dev->dev, &dev_attr_description);
354 }
355 /*
356 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800357 */
358 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
359 if (ACPI_SUCCESS(status))
360 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800361
Lv Zheng923d4a42012-10-30 14:43:26 +0100362 if (dev->pnp.unique_id)
363 device_remove_file(&dev->dev, &dev_attr_uid);
364 if (dev->flags.bus_address)
365 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600366 device_remove_file(&dev->dev, &dev_attr_modalias);
367 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600368 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800369 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800372 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200374
375int acpi_match_device_ids(struct acpi_device *device,
376 const struct acpi_device_id *ids)
377{
378 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600379 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200380
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800381 /*
382 * If the device is not present, it is unnecessary to load device
383 * driver for it.
384 */
385 if (!device->status.present)
386 return -ENODEV;
387
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600388 for (id = ids; id->id[0]; id++)
389 list_for_each_entry(hwid, &device->pnp.ids, list)
390 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200391 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200392
393 return -ENOENT;
394}
395EXPORT_SYMBOL(acpi_match_device_ids);
396
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600397static void acpi_free_ids(struct acpi_device *device)
398{
399 struct acpi_hardware_id *id, *tmp;
400
401 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
402 kfree(id->id);
403 kfree(id);
404 }
405}
406
Patrick Mochel1890a972006-12-07 20:56:31 +0800407static void acpi_device_release(struct device *dev)
408{
409 struct acpi_device *acpi_dev = to_acpi_device(dev);
410
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600411 acpi_free_ids(acpi_dev);
Lv Zhengccf78042012-10-30 14:41:07 +0100412 kfree(acpi_dev->pnp.unique_id);
Patrick Mochel1890a972006-12-07 20:56:31 +0800413 kfree(acpi_dev);
414}
415
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800416static int acpi_bus_match(struct device *dev, struct device_driver *drv)
417{
418 struct acpi_device *acpi_dev = to_acpi_device(dev);
419 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
420
Thomas Renninger29b71a12007-07-23 14:43:51 +0200421 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800422}
423
Kay Sievers7eff2e72007-08-14 15:15:12 +0200424static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800425{
426 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200427 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800428
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200429 if (list_empty(&acpi_dev->pnp.ids))
430 return 0;
431
Kay Sievers7eff2e72007-08-14 15:15:12 +0200432 if (add_uevent_var(env, "MODALIAS="))
433 return -ENOMEM;
434 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
435 sizeof(env->buf) - env->buflen);
436 if (len >= (sizeof(env->buf) - env->buflen))
437 return -ENOMEM;
438 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440}
441
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000442static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
443{
444 struct acpi_device *device = data;
445
446 device->driver->ops.notify(device, event);
447}
448
449static acpi_status acpi_device_notify_fixed(void *data)
450{
451 struct acpi_device *device = data;
452
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000453 /* Fixed hardware devices have no handles */
454 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000455 return AE_OK;
456}
457
458static int acpi_device_install_notify_handler(struct acpi_device *device)
459{
460 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000461
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000462 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000463 status =
464 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
465 acpi_device_notify_fixed,
466 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000467 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000468 status =
469 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
470 acpi_device_notify_fixed,
471 device);
472 else
473 status = acpi_install_notify_handler(device->handle,
474 ACPI_DEVICE_NOTIFY,
475 acpi_device_notify,
476 device);
477
478 if (ACPI_FAILURE(status))
479 return -EINVAL;
480 return 0;
481}
482
483static void acpi_device_remove_notify_handler(struct acpi_device *device)
484{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000485 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000486 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
487 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000488 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000489 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
490 acpi_device_notify_fixed);
491 else
492 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
493 acpi_device_notify);
494}
495
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800496static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
497static int acpi_start_single_object(struct acpi_device *);
498static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800499{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800500 struct acpi_device *acpi_dev = to_acpi_device(dev);
501 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
502 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800504 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
505 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800506 if (acpi_dev->bus_ops.acpi_op_start)
507 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000508
509 if (acpi_drv->ops.notify) {
510 ret = acpi_device_install_notify_handler(acpi_dev);
511 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000512 if (acpi_drv->ops.remove)
513 acpi_drv->ops.remove(acpi_dev,
514 acpi_dev->removal_type);
515 return ret;
516 }
517 }
518
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800519 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
520 "Found driver [%s] for device [%s]\n",
521 acpi_drv->name, acpi_dev->pnp.bus_id));
522 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800523 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800524 return ret;
525}
526
527static int acpi_device_remove(struct device * dev)
528{
529 struct acpi_device *acpi_dev = to_acpi_device(dev);
530 struct acpi_driver *acpi_drv = acpi_dev->driver;
531
532 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000533 if (acpi_drv->ops.notify)
534 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800535 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800536 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800537 }
538 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700539 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800540
541 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800542 return 0;
543}
544
David Brownell55955aa2007-05-08 00:28:35 -0700545struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800546 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800547 .match = acpi_bus_match,
548 .probe = acpi_device_probe,
549 .remove = acpi_device_remove,
550 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551};
552
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000553static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800555 int result;
556 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
557 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /*
560 * Linkage
561 * -------
562 * Link this device to its parent and siblings.
563 */
564 INIT_LIST_HEAD(&device->children);
565 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800567 INIT_LIST_HEAD(&device->physical_node_list);
568 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800570 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
571 if (!new_bus_id) {
572 printk(KERN_ERR PREFIX "Memory allocation error\n");
573 return -ENOMEM;
574 }
575
Shaohua Li90905892009-04-07 10:24:29 +0800576 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800577 /*
578 * Find suitable bus_id and instance number in acpi_bus_id_list
579 * If failed, create one and link it into acpi_bus_id_list
580 */
581 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600582 if (!strcmp(acpi_device_bus_id->bus_id,
583 acpi_device_hid(device))) {
584 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800585 found = 1;
586 kfree(new_bus_id);
587 break;
588 }
589 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600590 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800591 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600592 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800593 acpi_device_bus_id->instance_no = 0;
594 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
595 }
Kay Sievers07944692008-10-30 01:18:59 +0100596 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 +0800597
Len Brown33b57152008-12-15 22:09:26 -0500598 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (device->wakeup.flags.valid)
602 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800603 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Patrick Mochel1890a972006-12-07 20:56:31 +0800605 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000606 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800607 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800608 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600609 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600610 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600611 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800612 goto end;
613 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800614
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800615 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600616 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100617 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
618 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800619
Li Shaohua96333572006-12-07 20:56:46 +0800620 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800621 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600622end:
Shaohua Li90905892009-04-07 10:24:29 +0800623 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500624 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800625 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800626 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800627 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800628 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static void acpi_device_unregister(struct acpi_device *device, int type)
632{
Shaohua Li90905892009-04-07 10:24:29 +0800633 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500634 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800638 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800641
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800642 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800643 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Zhang Rui9e89dde2006-12-07 20:56:16 +0800646/* --------------------------------------------------------------------------
647 Driver Management
648 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500650 * acpi_bus_driver_init - add a device to a driver
651 * @device: the device to add and initialize
652 * @driver: driver for the device
653 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600654 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800655 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657static int
Len Brown4be44fc2005-08-05 00:44:28 -0400658acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Len Brown4be44fc2005-08-05 00:44:28 -0400660 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400663 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400666 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 result = driver->ops.add(device);
669 if (result) {
670 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700671 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400672 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 device->driver = driver;
676
677 /*
678 * TBD - Configuration Management: Assign resources to device based
679 * upon possible configuration and currently allocated resources.
680 */
681
Len Brown4be44fc2005-08-05 00:44:28 -0400682 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
683 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700685}
686
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400687static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700688{
689 int result = 0;
690 struct acpi_driver *driver;
691
Rajesh Shah3fb02732005-04-28 00:25:52 -0700692
693 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400694 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (driver->ops.start) {
697 result = driver->ops.start(device);
698 if (result && driver->ops.remove)
699 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
Patrick Mocheld550d982006-06-27 00:41:40 -0400702 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500706 * acpi_bus_register_driver - register a driver with the ACPI bus
707 * @driver: driver being registered
708 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500710 * devices that match the driver's criteria and binds. Returns zero for
711 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Len Brown4be44fc2005-08-05 00:44:28 -0400713int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Patrick Mochel1890a972006-12-07 20:56:31 +0800715 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400718 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800719 driver->drv.name = driver->name;
720 driver->drv.bus = &acpi_bus_type;
721 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Patrick Mochel1890a972006-12-07 20:56:31 +0800723 ret = driver_register(&driver->drv);
724 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Len Brown4be44fc2005-08-05 00:44:28 -0400727EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500730 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
731 * @driver: driver to unregister
732 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * Unregisters a driver with the ACPI bus. Searches the namespace for all
734 * devices that match the driver's criteria and unbinds.
735 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400736void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Patrick Mochel1890a972006-12-07 20:56:31 +0800738 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Len Brown4be44fc2005-08-05 00:44:28 -0400740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741EXPORT_SYMBOL(acpi_bus_unregister_driver);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/* --------------------------------------------------------------------------
744 Device Enumeration
745 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000746static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
747{
748 acpi_status status;
749 int ret;
750 struct acpi_device *device;
751
752 /*
753 * Fixed hardware devices do not appear in the namespace and do not
754 * have handles, but we fabricate acpi_devices for them, so we have
755 * to deal with them specially.
756 */
757 if (handle == NULL)
758 return acpi_root;
759
760 do {
761 status = acpi_get_parent(handle, &handle);
762 if (status == AE_NULL_ENTRY)
763 return NULL;
764 if (ACPI_FAILURE(status))
765 return acpi_root;
766
767 ret = acpi_bus_get_device(handle, &device);
768 if (ret == 0)
769 return device;
770 } while (1);
771}
772
Len Brownc8f7a622006-07-09 17:22:28 -0400773acpi_status
774acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
775{
776 acpi_status status;
777 acpi_handle tmp;
778 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
779 union acpi_object *obj;
780
781 status = acpi_get_handle(handle, "_EJD", &tmp);
782 if (ACPI_FAILURE(status))
783 return status;
784
785 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
786 if (ACPI_SUCCESS(status)) {
787 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100788 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
789 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400790 kfree(buffer.pointer);
791 }
792 return status;
793}
794EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
795
Bob Moore8e4319c2009-06-29 13:43:27 +0800796void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
798
799 /* TBD */
800
801 return;
802}
803
Zhang Rui9e89dde2006-12-07 20:56:16 +0800804static int acpi_bus_get_perf_flags(struct acpi_device *device)
805{
806 device->performance.state = ACPI_STATE_UNKNOWN;
807 return 0;
808}
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100811acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
812 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100814 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
815 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100817 acpi_status status;
818 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100820 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return AE_BAD_PARAMETER;
822
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100823 /* _PRW */
824 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
825 if (ACPI_FAILURE(status)) {
826 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
827 return status;
828 }
829
830 package = (union acpi_object *)buffer.pointer;
831
832 if (!package || (package->package.count < 2)) {
833 status = AE_BAD_DATA;
834 goto out;
835 }
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100838 if (!element) {
839 status = AE_BAD_DATA;
840 goto out;
841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (element->type == ACPI_TYPE_PACKAGE) {
843 if ((element->package.count < 2) ||
844 (element->package.elements[0].type !=
845 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100846 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
847 status = AE_BAD_DATA;
848 goto out;
849 }
850 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100852 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 (u32) element->package.elements[1].integer.value;
854 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100855 wakeup->gpe_device = NULL;
856 wakeup->gpe_number = element->integer.value;
857 } else {
858 status = AE_BAD_DATA;
859 goto out;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 element = &(package->package.elements[1]);
863 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100864 status = AE_BAD_DATA;
865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100867 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100870 status = AE_NO_MEMORY;
871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100873 wakeup->resources.count = package->package.count - 2;
874 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100876 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
877 status = AE_BAD_DATA;
878 goto out;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100881 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Lin Mingbba63a22010-12-13 13:39:17 +0800884 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200885
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100886 out:
887 kfree(buffer.pointer);
888
889 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100892static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200894 struct acpi_device_id button_device_ids[] = {
895 {"PNP0C0D", 0},
896 {"PNP0C0C", 0},
897 {"PNP0C0E", 0},
898 {"", 0},
899 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100900 acpi_status status;
901 acpi_event_status event_status;
902
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100903 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100904
905 /* Power button, Lid switch always enable wakeup */
906 if (!acpi_match_device_ids(device, button_device_ids)) {
907 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100908 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100909 return;
910 }
911
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200912 status = acpi_get_gpe_status(device->wakeup.gpe_device,
913 device->wakeup.gpe_number,
914 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100915 if (status == AE_OK)
916 device->wakeup.flags.run_wake =
917 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
918}
919
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100920static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100921{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100922 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100923 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100924 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200925
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100926 /* Presence of _PRW indicates wake capable */
927 status = acpi_get_handle(device->handle, "_PRW", &temp);
928 if (ACPI_FAILURE(status))
929 return;
930
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100931 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
932 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (ACPI_FAILURE(status)) {
934 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100935 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200939 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100940 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800941 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
942 * system for the ACPI device with the _PRW object.
943 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
944 * So it is necessary to call _DSW object first. Only when it is not
945 * present will the _PSW object used.
946 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200947 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
948 if (psw_error)
949 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
950 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100953static void acpi_bus_add_power_resource(acpi_handle handle);
954
Zhang Rui9e89dde2006-12-07 20:56:16 +0800955static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800957 acpi_status status = 0;
958 acpi_handle handle = NULL;
959 u32 i = 0;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800963 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800965 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800967 device->power.flags.explicit_get = 1;
968 status = acpi_get_handle(device->handle, "_IRC", &handle);
969 if (ACPI_SUCCESS(status))
970 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800973 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800975 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800976 struct acpi_device_power_state *ps = &device->power.states[i];
977 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Zhang Rui9e89dde2006-12-07 20:56:16 +0800979 /* Evaluate "_PRx" to se if power resources are referenced */
980 acpi_evaluate_reference(device->handle, object_name, NULL,
981 &ps->resources);
982 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100983 int j;
984
Zhang Rui9e89dde2006-12-07 20:56:16 +0800985 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100986 for (j = 0; j < ps->resources.count; j++)
987 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Zhang Rui9e89dde2006-12-07 20:56:16 +0800990 /* Evaluate "_PSx" to see if we can do explicit sets */
991 object_name[2] = 'S';
992 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800993 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800994 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800995
Lin Ming1cc0c992012-04-23 09:03:49 +0800996 /*
997 * State is valid if there are means to put the device into it.
998 * D3hot is only valid if _PR3 present.
999 */
1000 if (ps->resources.count ||
1001 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001002 ps->flags.valid = 1;
1003
1004 ps->power = -1; /* Unknown - driver assigned */
1005 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Zhang Rui9e89dde2006-12-07 20:56:16 +08001008 /* Set defaults for D0 and D3 states (always valid) */
1009 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1010 device->power.states[ACPI_STATE_D0].power = 100;
1011 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1012 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001014 /* Set D3cold's explicit_set flag if _PS3 exists. */
1015 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1016 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1017
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001018 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 return 0;
1021}
1022
Len Brown4be44fc2005-08-05 00:44:28 -04001023static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Len Brown4be44fc2005-08-05 00:44:28 -04001025 acpi_status status = AE_OK;
1026 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /* Presence of _STA indicates 'dynamic_status' */
1030 status = acpi_get_handle(device->handle, "_STA", &temp);
1031 if (ACPI_SUCCESS(status))
1032 device->flags.dynamic_status = 1;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /* Presence of _RMV indicates 'removable' */
1035 status = acpi_get_handle(device->handle, "_RMV", &temp);
1036 if (ACPI_SUCCESS(status))
1037 device->flags.removable = 1;
1038
1039 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1040 status = acpi_get_handle(device->handle, "_EJD", &temp);
1041 if (ACPI_SUCCESS(status))
1042 device->flags.ejectable = 1;
1043 else {
1044 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1045 if (ACPI_SUCCESS(status))
1046 device->flags.ejectable = 1;
1047 }
1048
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001049 /* Power resources cannot be power manageable. */
1050 if (device->device_type == ACPI_BUS_TYPE_POWER)
1051 return 0;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1054 status = acpi_get_handle(device->handle, "_PS0", &temp);
1055 if (ACPI_FAILURE(status))
1056 status = acpi_get_handle(device->handle, "_PR0", &temp);
1057 if (ACPI_SUCCESS(status))
1058 device->flags.power_manageable = 1;
1059
Joe Perches3c5f9be42008-02-03 17:06:17 +02001060 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001065static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Len Brown4be44fc2005-08-05 00:44:28 -04001067 char bus_id[5] = { '?', 0 };
1068 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1069 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /*
1072 * Bus ID
1073 * ------
1074 * The device's Bus ID is simply the object name.
1075 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1076 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001077 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001079 return;
1080 }
1081
1082 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 case ACPI_BUS_TYPE_POWER_BUTTON:
1084 strcpy(device->pnp.bus_id, "PWRF");
1085 break;
1086 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1087 strcpy(device->pnp.bus_id, "SLPF");
1088 break;
1089 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001090 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* Clean up trailing underscores (if any) */
1092 for (i = 3; i > 1; i--) {
1093 if (bus_id[i] == '_')
1094 bus_id[i] = '\0';
1095 else
1096 break;
1097 }
1098 strcpy(device->pnp.bus_id, bus_id);
1099 break;
1100 }
1101}
1102
Zhang Rui54735262007-01-11 02:09:09 -05001103/*
1104 * acpi_bay_match - see if a device is an ejectable driver bay
1105 *
1106 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1107 * then we can safely call it an ejectable drive bay
1108 */
1109static int acpi_bay_match(struct acpi_device *device){
1110 acpi_status status;
1111 acpi_handle handle;
1112 acpi_handle tmp;
1113 acpi_handle phandle;
1114
1115 handle = device->handle;
1116
1117 status = acpi_get_handle(handle, "_EJ0", &tmp);
1118 if (ACPI_FAILURE(status))
1119 return -ENODEV;
1120
1121 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1122 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1123 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1124 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1125 return 0;
1126
1127 if (acpi_get_parent(handle, &phandle))
1128 return -ENODEV;
1129
1130 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1131 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1132 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1133 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1134 return 0;
1135
1136 return -ENODEV;
1137}
1138
Frank Seidel3620f2f2007-12-07 13:20:34 +01001139/*
1140 * acpi_dock_match - see if a device has a _DCK method
1141 */
1142static int acpi_dock_match(struct acpi_device *device)
1143{
1144 acpi_handle tmp;
1145 return acpi_get_handle(device->handle, "_DCK", &tmp);
1146}
1147
Thomas Renninger620e1122010-10-01 10:54:00 +02001148const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001149{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001150 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001151
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001152 if (list_empty(&device->pnp.ids))
1153 return dummy_hid;
1154
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001155 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1156 return hid->id;
1157}
1158EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001159
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001160static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1161{
1162 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001163
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001164 id = kmalloc(sizeof(*id), GFP_KERNEL);
1165 if (!id)
1166 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001167
Thomas Meyer581de592011-08-06 11:32:56 +02001168 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001169 if (!id->id) {
1170 kfree(id);
1171 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001172 }
1173
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001174 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001175}
1176
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001177/*
1178 * Old IBM workstations have a DSDT bug wherein the SMBus object
1179 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1180 * prefix. Work around this.
1181 */
1182static int acpi_ibm_smbus_match(struct acpi_device *device)
1183{
1184 acpi_handle h_dummy;
1185 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1186 int result;
1187
1188 if (!dmi_name_in_vendors("IBM"))
1189 return -ENODEV;
1190
1191 /* Look for SMBS object */
1192 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1193 if (result)
1194 return result;
1195
1196 if (strcmp("SMBS", path.pointer)) {
1197 result = -ENODEV;
1198 goto out;
1199 }
1200
1201 /* Does it have the necessary (but misnamed) methods? */
1202 result = -ENODEV;
1203 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1204 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1205 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1206 result = 0;
1207out:
1208 kfree(path.pointer);
1209 return result;
1210}
1211
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001212static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Len Brown4be44fc2005-08-05 00:44:28 -04001214 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001215 struct acpi_device_info *info;
1216 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001217 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001219 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001221 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001222 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001223 break;
1224 }
1225
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001226 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001228 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return;
1230 }
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001233 acpi_add_id(device, info->hardware_id.string);
1234 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001235 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001236 for (i = 0; i < cid_list->count; i++)
1237 acpi_add_id(device, cid_list->ids[i].string);
1238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (info->valid & ACPI_VALID_ADR) {
1240 device->pnp.bus_address = info->address;
1241 device->flags.bus_address = 1;
1242 }
Lv Zhengccf78042012-10-30 14:41:07 +01001243 if (info->valid & ACPI_VALID_UID)
1244 device->pnp.unique_id = kstrdup(info->unique_id.string,
1245 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001246
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001247 kfree(info);
1248
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001249 /*
1250 * Some devices don't reliably have _HIDs & _CIDs, so add
1251 * synthetic HIDs to make sure drivers can find them.
1252 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001253 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001254 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001255 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001256 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001257 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001258 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001259 else if (!acpi_ibm_smbus_match(device))
1260 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001261 else if (!acpi_device_hid(device) &&
1262 ACPI_IS_ROOT_DEVICE(device->parent)) {
1263 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1264 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1265 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1266 }
Zhang Rui54735262007-01-11 02:09:09 -05001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 break;
1269 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001270 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001273 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001276 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
1278 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001279 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 break;
1281 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001282 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001287static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001289 acpi_status status;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * Context
1293 * -------
1294 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001295 * resolutions from handle->device very efficient. Fixed hardware
1296 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001298 if (!device->handle)
1299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001301 status = acpi_attach_data(device->handle,
1302 acpi_bus_data_handler, device);
1303 if (ACPI_SUCCESS(status))
1304 return 0;
1305
1306 printk(KERN_ERR PREFIX "Error attaching device data\n");
1307 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Len Brown4be44fc2005-08-05 00:44:28 -04001310static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Li Shaohua96333572006-12-07 20:56:46 +08001315 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001316 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Rui Zhang2786f6e2006-12-21 02:21:13 -05001321 /*
1322 * unbind _ADR-Based Devices when hot removal
1323 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (dev->flags.bus_address) {
1325 if ((dev->parent) && (dev->parent->ops.unbind))
1326 dev->parent->ops.unbind(dev);
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1329
Patrick Mocheld550d982006-06-27 00:41:40 -04001330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001333static int acpi_add_single_object(struct acpi_device **child,
1334 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001335 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001336 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001338 int result;
1339 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001340 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Burman Yan36bcbec2006-12-19 12:56:11 -08001342 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001344 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001345 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001348 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001349 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001351 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001352 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001353 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001354
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001355 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
1358 * Flags
1359 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001360 * Note that we only look for object handles -- cannot evaluate objects
1361 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
1363 result = acpi_bus_get_flags(device);
1364 if (result)
1365 goto end;
1366
1367 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * Initialize Device
1369 * -----------------
1370 * TBD: Synch with Core's enumeration/initialization process.
1371 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001372 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /*
1375 * Power Management
1376 * ----------------
1377 */
1378 if (device->flags.power_manageable) {
1379 result = acpi_bus_get_power_flags(device);
1380 if (result)
1381 goto end;
1382 }
1383
Len Brown4be44fc2005-08-05 00:44:28 -04001384 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * Wakeup device management
1386 *-----------------------
1387 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001388 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /*
1391 * Performance Management
1392 * ----------------------
1393 */
1394 if (device->flags.performance_manageable) {
1395 result = acpi_bus_get_perf_flags(device);
1396 if (result)
1397 goto end;
1398 }
1399
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001400 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001401 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001403 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001406 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 */
1408 if (device->flags.bus_address) {
1409 if (device->parent && device->parent->ops.bind)
1410 device->parent->ops.bind(device);
1411 }
1412
Alex Chiang0c526d92009-05-14 08:31:37 -06001413end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001414 if (!result) {
1415 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1416 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1417 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1418 (char *) buffer.pointer,
1419 device->parent ? dev_name(&device->parent->dev) :
1420 "(null)"));
1421 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001423 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001424 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Patrick Mocheld550d982006-06-27 00:41:40 -04001426 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001429#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1430 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1431
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001432static void acpi_bus_add_power_resource(acpi_handle handle)
1433{
1434 struct acpi_bus_ops ops = {
1435 .acpi_op_add = 1,
1436 .acpi_op_start = 1,
1437 };
1438 struct acpi_device *device = NULL;
1439
1440 acpi_bus_get_device(handle, &device);
1441 if (!device)
1442 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1443 ACPI_STA_DEFAULT, &ops);
1444}
1445
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001446static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1447 unsigned long long *sta)
1448{
1449 acpi_status status;
1450 acpi_object_type acpi_type;
1451
1452 status = acpi_get_type(handle, &acpi_type);
1453 if (ACPI_FAILURE(status))
1454 return -ENODEV;
1455
1456 switch (acpi_type) {
1457 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1458 case ACPI_TYPE_DEVICE:
1459 *type = ACPI_BUS_TYPE_DEVICE;
1460 status = acpi_bus_get_status_handle(handle, sta);
1461 if (ACPI_FAILURE(status))
1462 return -ENODEV;
1463 break;
1464 case ACPI_TYPE_PROCESSOR:
1465 *type = ACPI_BUS_TYPE_PROCESSOR;
1466 status = acpi_bus_get_status_handle(handle, sta);
1467 if (ACPI_FAILURE(status))
1468 return -ENODEV;
1469 break;
1470 case ACPI_TYPE_THERMAL:
1471 *type = ACPI_BUS_TYPE_THERMAL;
1472 *sta = ACPI_STA_DEFAULT;
1473 break;
1474 case ACPI_TYPE_POWER:
1475 *type = ACPI_BUS_TYPE_POWER;
1476 *sta = ACPI_STA_DEFAULT;
1477 break;
1478 default:
1479 return -ENODEV;
1480 }
1481
1482 return 0;
1483}
1484
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001485static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1486 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001488 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001489 int type;
1490 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001491 struct acpi_device *device;
1492 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001493 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001494
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001495 result = acpi_bus_type_and_status(handle, &type, &sta);
1496 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001497 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001499 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001500 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001501 struct acpi_device_wakeup wakeup;
1502 acpi_handle temp;
1503
1504 status = acpi_get_handle(handle, "_PRW", &temp);
1505 if (ACPI_SUCCESS(status))
1506 acpi_bus_extract_wakeup_device_power_package(handle,
1507 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001508 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001511 /*
1512 * We may already have an acpi_device from a previous enumeration. If
1513 * so, we needn't add it again, but we may still have to start it.
1514 */
1515 device = NULL;
1516 acpi_bus_get_device(handle, &device);
1517 if (ops->acpi_op_add && !device)
1518 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001519
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001520 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001521 return AE_CTRL_DEPTH;
1522
1523 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1524 status = acpi_start_single_object(device);
1525 if (ACPI_FAILURE(status))
1526 return AE_CTRL_DEPTH;
1527 }
1528
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001529 if (!*return_value)
1530 *return_value = device;
1531 return AE_OK;
1532}
1533
1534static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1535 struct acpi_device **child)
1536{
1537 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001538 void *device = NULL;
1539
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001540 status = acpi_bus_check_add(handle, 0, ops, &device);
1541 if (ACPI_SUCCESS(status))
1542 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001543 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001544
1545 if (child)
1546 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001547
1548 if (device)
1549 return 0;
1550 else
1551 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Thomas Renninger77796882010-01-29 17:48:52 +01001554/*
1555 * acpi_bus_add and acpi_bus_start
1556 *
1557 * scan a given ACPI tree and (probably recently hot-plugged)
1558 * create and add or starts found devices.
1559 *
1560 * If no devices were found -ENODEV is returned which does not
1561 * mean that this is a real error, there just have been no suitable
1562 * ACPI objects in the table trunk from which the kernel could create
1563 * a device and add/start an appropriate driver.
1564 */
1565
Rajesh Shah3fb02732005-04-28 00:25:52 -07001566int
Len Brown4be44fc2005-08-05 00:44:28 -04001567acpi_bus_add(struct acpi_device **child,
1568 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001569{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001570 struct acpi_bus_ops ops;
1571
Li Shaohuac4168bf2006-12-07 20:56:41 +08001572 memset(&ops, 0, sizeof(ops));
1573 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001574
Thomas Renninger77796882010-01-29 17:48:52 +01001575 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001576}
1577EXPORT_SYMBOL(acpi_bus_add);
1578
Len Brown4be44fc2005-08-05 00:44:28 -04001579int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001580{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001581 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001582 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001583
Thomas Renningerd2f66502010-01-29 17:48:51 +01001584 if (!device)
1585 return -EINVAL;
1586
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001587 memset(&ops, 0, sizeof(ops));
1588 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001589
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001590 result = acpi_bus_scan(device->handle, &ops, NULL);
1591
Lin Ming3a378982010-12-13 13:36:15 +08001592 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001593
1594 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001595}
1596EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Kristen Accardiceaba662006-02-23 17:56:01 -08001598int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Len Brown4be44fc2005-08-05 00:44:28 -04001600 acpi_status status;
1601 struct acpi_device *parent, *child;
1602 acpi_handle phandle, chandle;
1603 acpi_object_type type;
1604 u32 level = 1;
1605 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Len Brown4be44fc2005-08-05 00:44:28 -04001607 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 phandle = start->handle;
1609 child = chandle = NULL;
1610
1611 while ((level > 0) && parent && (!err)) {
1612 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001613 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /*
1616 * If this scope is exhausted then move our way back up.
1617 */
1618 if (ACPI_FAILURE(status)) {
1619 level--;
1620 chandle = phandle;
1621 acpi_get_parent(phandle, &phandle);
1622 child = parent;
1623 parent = parent->parent;
1624
1625 if (level == 0)
1626 err = acpi_bus_remove(child, rmdevice);
1627 else
1628 err = acpi_bus_remove(child, 1);
1629
1630 continue;
1631 }
1632
1633 status = acpi_get_type(chandle, &type);
1634 if (ACPI_FAILURE(status)) {
1635 continue;
1636 }
1637 /*
1638 * If there is a device corresponding to chandle then
1639 * parse it (depth-first).
1640 */
1641 if (acpi_bus_get_device(chandle, &child) == 0) {
1642 level++;
1643 phandle = chandle;
1644 chandle = NULL;
1645 parent = child;
1646 }
1647 continue;
1648 }
1649 return err;
1650}
Kristen Accardiceaba662006-02-23 17:56:01 -08001651EXPORT_SYMBOL_GPL(acpi_bus_trim);
1652
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001653static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Len Brown4be44fc2005-08-05 00:44:28 -04001655 int result = 0;
1656 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001657 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Li Shaohuac4168bf2006-12-07 20:56:41 +08001659 memset(&ops, 0, sizeof(ops));
1660 ops.acpi_op_add = 1;
1661 ops.acpi_op_start = 1;
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /*
1664 * Enumerate all fixed-feature devices.
1665 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001666 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001667 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001668 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001669 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001670 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001671 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001674 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001675 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001676 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001677 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001678 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Patrick Mocheld550d982006-06-27 00:41:40 -04001681 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Bjorn Helgaase747f272009-03-24 16:49:43 -06001684int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001687 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Li Shaohuac4168bf2006-12-07 20:56:41 +08001689 memset(&ops, 0, sizeof(ops));
1690 ops.acpi_op_add = 1;
1691 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Patrick Mochel5b327262006-05-10 10:33:00 -04001693 result = bus_register(&acpi_bus_type);
1694 if (result) {
1695 /* We don't want to quit even if we failed to add suspend/resume */
1696 printk(KERN_ERR PREFIX "Could not register bus type\n");
1697 }
1698
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001699 acpi_power_init();
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 * Enumerate devices in the ACPI namespace.
1703 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001704 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001705
Li Shaohuac4168bf2006-12-07 20:56:41 +08001706 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001707 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 if (result)
1710 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001711 else
Lin Ming3a378982010-12-13 13:36:15 +08001712 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Patrick Mocheld550d982006-06-27 00:41:40 -04001714 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}