blob: d842569395a9f310b4e50e23c18db9a2fad4feb7 [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
Mika Westerberg91e56872012-10-31 22:45:02 +010032/*
33 * The following ACPI IDs are known to be suitable for representing as
34 * platform devices.
35 */
36static const struct acpi_device_id acpi_platform_device_ids[] = {
37
38 { }
39};
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080042static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080043DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044LIST_HEAD(acpi_wakeup_device_list);
45
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080046struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080047 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080048 unsigned int instance_no;
49 struct list_head node;
50};
Thomas Renninger29b71a12007-07-23 14:43:51 +020051
52/*
53 * Creates hid/cid(s) string needed for modalias and uevent
54 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
55 * char *modalias: "acpi:IBM0001:ACPI0001"
56*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020057static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
58 int size)
59{
Thomas Renninger29b71a12007-07-23 14:43:51 +020060 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080061 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060062 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020063
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020064 if (list_empty(&acpi_dev->pnp.ids))
65 return 0;
66
Zhang Rui5c9fcb52008-03-20 16:40:32 +080067 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020068 size -= len;
69
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060070 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
71 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080072 if (count < 0 || count >= size)
73 return -EINVAL;
74 len += count;
75 size -= count;
76 }
77
Thomas Renninger29b71a12007-07-23 14:43:51 +020078 modalias[len] = '\0';
79 return len;
80}
81
82static ssize_t
83acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
84 struct acpi_device *acpi_dev = to_acpi_device(dev);
85 int len;
86
87 /* Device has no HID and no CID or string is >1024 */
88 len = create_modalias(acpi_dev, buf, 1024);
89 if (len <= 0)
90 return 0;
91 buf[len++] = '\n';
92 return len;
93}
94static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
95
Toshi Kanic4753e52012-05-23 20:25:20 -060096/**
97 * acpi_bus_hot_remove_device: hot-remove a device and its children
98 * @context: struct acpi_eject_event pointer (freed in this func)
99 *
100 * Hot-remove a device and its children. This function frees up the
101 * memory space passed by arg context, so that the caller may call
102 * this function asynchronously through acpi_os_hotplug_execute().
103 */
104void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Toshi Kanic4753e52012-05-23 20:25:20 -0600106 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
Zhang Rui26d46862008-04-29 02:35:48 -0400107 struct acpi_device *device;
Toshi Kanic4753e52012-05-23 20:25:20 -0600108 acpi_handle handle = ej_event->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct acpi_object_list arg_list;
110 union acpi_object arg;
111 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600112 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Zhang Rui26d46862008-04-29 02:35:48 -0400114 if (acpi_bus_get_device(handle, &device))
Toshi Kanic4753e52012-05-23 20:25:20 -0600115 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Zhang Rui26d46862008-04-29 02:35:48 -0400117 if (!device)
Toshi Kanic4753e52012-05-23 20:25:20 -0600118 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400119
120 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100121 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400122
123 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800124 printk(KERN_ERR PREFIX
125 "Removing device failed\n");
Toshi Kanic4753e52012-05-23 20:25:20 -0600126 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400127 }
128
129 /* power off device */
130 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
131 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800132 printk(KERN_WARNING PREFIX
133 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400134
135 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 arg_list.count = 1;
137 arg_list.pointer = &arg;
138 arg.type = ACPI_TYPE_INTEGER;
139 arg.integer.value = 0;
140 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
141 }
142
143 arg_list.count = 1;
144 arg_list.pointer = &arg;
145 arg.type = ACPI_TYPE_INTEGER;
146 arg.integer.value = 1;
147
148 /*
149 * TBD: _EJD support.
150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600152 if (ACPI_FAILURE(status)) {
153 if (status != AE_NOT_FOUND)
154 printk(KERN_WARNING PREFIX
155 "Eject device failed\n");
156 goto err_out;
157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Toshi Kanic4753e52012-05-23 20:25:20 -0600159 kfree(context);
160 return;
161
162err_out:
163 /* Inform firmware the hot-remove operation has completed w/ error */
164 (void) acpi_evaluate_hotplug_ost(handle,
165 ej_event->event, ost_code, NULL);
166 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800167 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800171acpi_eject_store(struct device *d, struct device_attribute *attr,
172 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Len Brown4be44fc2005-08-05 00:44:28 -0400174 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400175 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400176 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800177 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600178 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if ((!count) || (buf[0] != '1')) {
181 return -EINVAL;
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800184 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 ret = -ENODEV;
186 goto err;
187 }
188#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800189 status = acpi_get_type(acpi_device->handle, &type);
190 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ret = -ENODEV;
192 goto err;
193 }
194
Toshi Kanic4753e52012-05-23 20:25:20 -0600195 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
196 if (!ej_event) {
197 ret = -ENOMEM;
198 goto err;
199 }
200
201 ej_event->handle = acpi_device->handle;
202 if (acpi_device->flags.eject_pending) {
203 /* event originated from ACPI eject notification */
204 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
205 acpi_device->flags.eject_pending = 0;
206 } else {
207 /* event originated from user */
208 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
209 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
210 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
211 }
212
213 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400214err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800218static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800220static ssize_t
221acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
222 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600224 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800225}
226static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
227
228static ssize_t
229acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
230 struct acpi_device *acpi_dev = to_acpi_device(dev);
231 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
232 int result;
233
234 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600235 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800236 goto end;
237
238 result = sprintf(buf, "%s\n", (char*)path.pointer);
239 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600240end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800241 return result;
242}
243static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
244
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600245/* sysfs file that shows description text from the ACPI _STR method */
246static ssize_t description_show(struct device *dev,
247 struct device_attribute *attr,
248 char *buf) {
249 struct acpi_device *acpi_dev = to_acpi_device(dev);
250 int result;
251
252 if (acpi_dev->pnp.str_obj == NULL)
253 return 0;
254
255 /*
256 * The _STR object contains a Unicode identifier for a device.
257 * We need to convert to utf-8 so it can be displayed.
258 */
259 result = utf16s_to_utf8s(
260 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
261 acpi_dev->pnp.str_obj->buffer.length,
262 UTF16_LITTLE_ENDIAN, buf,
263 PAGE_SIZE);
264
265 buf[result++] = '\n';
266
267 return result;
268}
269static DEVICE_ATTR(description, 0444, description_show, NULL);
270
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800271static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600273 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800274 acpi_status status;
275 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800276 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800278 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800279 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800280 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600281 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800282 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600283 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800284 goto end;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200287 if (!list_empty(&dev->pnp.ids)) {
288 result = device_create_file(&dev->dev, &dev_attr_hid);
289 if (result)
290 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200292 result = device_create_file(&dev->dev, &dev_attr_modalias);
293 if (result)
294 goto end;
295 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200296
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600297 /*
298 * If device has _STR, 'description' file is created
299 */
300 status = acpi_get_handle(dev->handle, "_STR", &temp);
301 if (ACPI_SUCCESS(status)) {
302 status = acpi_evaluate_object(dev->handle, "_STR",
303 NULL, &buffer);
304 if (ACPI_FAILURE(status))
305 buffer.pointer = NULL;
306 dev->pnp.str_obj = buffer.pointer;
307 result = device_create_file(&dev->dev, &dev_attr_description);
308 if (result)
309 goto end;
310 }
311
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800312 /*
313 * If device has _EJ0, 'eject' file is created that is used to trigger
314 * hot-removal function from userland.
315 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800316 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
317 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800318 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600319end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800320 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800321}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800323static void acpi_device_remove_files(struct acpi_device *dev)
324{
325 acpi_status status;
326 acpi_handle temp;
327
328 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600329 * If device has _STR, remove 'description' file
330 */
331 status = acpi_get_handle(dev->handle, "_STR", &temp);
332 if (ACPI_SUCCESS(status)) {
333 kfree(dev->pnp.str_obj);
334 device_remove_file(&dev->dev, &dev_attr_description);
335 }
336 /*
337 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800338 */
339 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
340 if (ACPI_SUCCESS(status))
341 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800342
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600343 device_remove_file(&dev->dev, &dev_attr_modalias);
344 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600345 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800346 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800349 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200351
Mika Westerbergcf761af2012-10-31 22:44:41 +0100352static const struct acpi_device_id *__acpi_match_device(
353 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200354{
355 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600356 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200357
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800358 /*
359 * If the device is not present, it is unnecessary to load device
360 * driver for it.
361 */
362 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100363 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800364
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600365 for (id = ids; id->id[0]; id++)
366 list_for_each_entry(hwid, &device->pnp.ids, list)
367 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100368 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200369
Mika Westerbergcf761af2012-10-31 22:44:41 +0100370 return NULL;
371}
372
373/**
374 * acpi_match_device - Match a struct device against a given list of ACPI IDs
375 * @ids: Array of struct acpi_device_id object to match against.
376 * @dev: The device structure to match.
377 *
378 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
379 * object for that handle and use that object to match against a given list of
380 * device IDs.
381 *
382 * Return a pointer to the first matching ID on success or %NULL on failure.
383 */
384const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
385 const struct device *dev)
386{
387 struct acpi_device *adev;
388
389 if (!ids || !dev->acpi_handle
390 || ACPI_FAILURE(acpi_bus_get_device(dev->acpi_handle, &adev)))
391 return NULL;
392
393 return __acpi_match_device(adev, ids);
394}
395EXPORT_SYMBOL_GPL(acpi_match_device);
396
397int acpi_match_device_ids(struct acpi_device *device,
398 const struct acpi_device_id *ids)
399{
400 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200401}
402EXPORT_SYMBOL(acpi_match_device_ids);
403
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600404static void acpi_free_ids(struct acpi_device *device)
405{
406 struct acpi_hardware_id *id, *tmp;
407
408 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
409 kfree(id->id);
410 kfree(id);
411 }
412}
413
Patrick Mochel1890a972006-12-07 20:56:31 +0800414static void acpi_device_release(struct device *dev)
415{
416 struct acpi_device *acpi_dev = to_acpi_device(dev);
417
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600418 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800419 kfree(acpi_dev);
420}
421
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800422static int acpi_bus_match(struct device *dev, struct device_driver *drv)
423{
424 struct acpi_device *acpi_dev = to_acpi_device(dev);
425 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
426
Thomas Renninger29b71a12007-07-23 14:43:51 +0200427 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800428}
429
Kay Sievers7eff2e72007-08-14 15:15:12 +0200430static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800431{
432 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200433 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800434
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200435 if (list_empty(&acpi_dev->pnp.ids))
436 return 0;
437
Kay Sievers7eff2e72007-08-14 15:15:12 +0200438 if (add_uevent_var(env, "MODALIAS="))
439 return -ENOMEM;
440 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
441 sizeof(env->buf) - env->buflen);
442 if (len >= (sizeof(env->buf) - env->buflen))
443 return -ENOMEM;
444 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446}
447
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000448static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
449{
450 struct acpi_device *device = data;
451
452 device->driver->ops.notify(device, event);
453}
454
455static acpi_status acpi_device_notify_fixed(void *data)
456{
457 struct acpi_device *device = data;
458
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000459 /* Fixed hardware devices have no handles */
460 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000461 return AE_OK;
462}
463
464static int acpi_device_install_notify_handler(struct acpi_device *device)
465{
466 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000467
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000468 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000469 status =
470 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
471 acpi_device_notify_fixed,
472 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000473 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000474 status =
475 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
476 acpi_device_notify_fixed,
477 device);
478 else
479 status = acpi_install_notify_handler(device->handle,
480 ACPI_DEVICE_NOTIFY,
481 acpi_device_notify,
482 device);
483
484 if (ACPI_FAILURE(status))
485 return -EINVAL;
486 return 0;
487}
488
489static void acpi_device_remove_notify_handler(struct acpi_device *device)
490{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000491 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000492 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
493 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000494 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000495 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
496 acpi_device_notify_fixed);
497 else
498 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
499 acpi_device_notify);
500}
501
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800502static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
503static int acpi_start_single_object(struct acpi_device *);
504static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800505{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800506 struct acpi_device *acpi_dev = to_acpi_device(dev);
507 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
508 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800510 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
511 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800512 if (acpi_dev->bus_ops.acpi_op_start)
513 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000514
515 if (acpi_drv->ops.notify) {
516 ret = acpi_device_install_notify_handler(acpi_dev);
517 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000518 if (acpi_drv->ops.remove)
519 acpi_drv->ops.remove(acpi_dev,
520 acpi_dev->removal_type);
521 return ret;
522 }
523 }
524
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800525 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
526 "Found driver [%s] for device [%s]\n",
527 acpi_drv->name, acpi_dev->pnp.bus_id));
528 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800529 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800530 return ret;
531}
532
533static int acpi_device_remove(struct device * dev)
534{
535 struct acpi_device *acpi_dev = to_acpi_device(dev);
536 struct acpi_driver *acpi_drv = acpi_dev->driver;
537
538 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000539 if (acpi_drv->ops.notify)
540 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800541 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800542 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800543 }
544 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700545 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800546
547 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800548 return 0;
549}
550
David Brownell55955aa2007-05-08 00:28:35 -0700551struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800552 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800553 .match = acpi_bus_match,
554 .probe = acpi_device_probe,
555 .remove = acpi_device_remove,
556 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557};
558
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000559static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800561 int result;
562 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
563 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * Linkage
567 * -------
568 * Link this device to its parent and siblings.
569 */
570 INIT_LIST_HEAD(&device->children);
571 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800573 INIT_LIST_HEAD(&device->physical_node_list);
574 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800576 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
577 if (!new_bus_id) {
578 printk(KERN_ERR PREFIX "Memory allocation error\n");
579 return -ENOMEM;
580 }
581
Shaohua Li90905892009-04-07 10:24:29 +0800582 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800583 /*
584 * Find suitable bus_id and instance number in acpi_bus_id_list
585 * If failed, create one and link it into acpi_bus_id_list
586 */
587 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600588 if (!strcmp(acpi_device_bus_id->bus_id,
589 acpi_device_hid(device))) {
590 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800591 found = 1;
592 kfree(new_bus_id);
593 break;
594 }
595 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600596 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800597 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600598 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800599 acpi_device_bus_id->instance_no = 0;
600 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
601 }
Kay Sievers07944692008-10-30 01:18:59 +0100602 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 +0800603
Len Brown33b57152008-12-15 22:09:26 -0500604 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (device->wakeup.flags.valid)
608 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800609 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Patrick Mochel1890a972006-12-07 20:56:31 +0800611 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000612 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800613 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800614 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600615 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600616 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600617 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800618 goto end;
619 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800620
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800621 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600622 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100623 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
624 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800625
Li Shaohua96333572006-12-07 20:56:46 +0800626 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800627 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600628end:
Shaohua Li90905892009-04-07 10:24:29 +0800629 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500630 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800631 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800632 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800633 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800634 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
637static void acpi_device_unregister(struct acpi_device *device, int type)
638{
Shaohua Li90905892009-04-07 10:24:29 +0800639 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500640 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800644 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800647
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800648 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800649 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Zhang Rui9e89dde2006-12-07 20:56:16 +0800652/* --------------------------------------------------------------------------
653 Driver Management
654 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500656 * acpi_bus_driver_init - add a device to a driver
657 * @device: the device to add and initialize
658 * @driver: driver for the device
659 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600660 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800661 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663static int
Len Brown4be44fc2005-08-05 00:44:28 -0400664acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Len Brown4be44fc2005-08-05 00:44:28 -0400666 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400672 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 result = driver->ops.add(device);
675 if (result) {
676 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700677 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400678 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 device->driver = driver;
682
683 /*
684 * TBD - Configuration Management: Assign resources to device based
685 * upon possible configuration and currently allocated resources.
686 */
687
Len Brown4be44fc2005-08-05 00:44:28 -0400688 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
689 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400690 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700691}
692
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400693static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700694{
695 int result = 0;
696 struct acpi_driver *driver;
697
Rajesh Shah3fb02732005-04-28 00:25:52 -0700698
699 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400700 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (driver->ops.start) {
703 result = driver->ops.start(device);
704 if (result && driver->ops.remove)
705 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
Patrick Mocheld550d982006-06-27 00:41:40 -0400708 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500712 * acpi_bus_register_driver - register a driver with the ACPI bus
713 * @driver: driver being registered
714 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500716 * devices that match the driver's criteria and binds. Returns zero for
717 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Len Brown4be44fc2005-08-05 00:44:28 -0400719int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Patrick Mochel1890a972006-12-07 20:56:31 +0800721 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400724 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800725 driver->drv.name = driver->name;
726 driver->drv.bus = &acpi_bus_type;
727 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Patrick Mochel1890a972006-12-07 20:56:31 +0800729 ret = driver_register(&driver->drv);
730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Len Brown4be44fc2005-08-05 00:44:28 -0400733EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500736 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
737 * @driver: driver to unregister
738 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * Unregisters a driver with the ACPI bus. Searches the namespace for all
740 * devices that match the driver's criteria and unbinds.
741 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400742void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Patrick Mochel1890a972006-12-07 20:56:31 +0800744 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
Len Brown4be44fc2005-08-05 00:44:28 -0400746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747EXPORT_SYMBOL(acpi_bus_unregister_driver);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/* --------------------------------------------------------------------------
750 Device Enumeration
751 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000752static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
753{
754 acpi_status status;
755 int ret;
756 struct acpi_device *device;
757
758 /*
759 * Fixed hardware devices do not appear in the namespace and do not
760 * have handles, but we fabricate acpi_devices for them, so we have
761 * to deal with them specially.
762 */
763 if (handle == NULL)
764 return acpi_root;
765
766 do {
767 status = acpi_get_parent(handle, &handle);
768 if (status == AE_NULL_ENTRY)
769 return NULL;
770 if (ACPI_FAILURE(status))
771 return acpi_root;
772
773 ret = acpi_bus_get_device(handle, &device);
774 if (ret == 0)
775 return device;
776 } while (1);
777}
778
Len Brownc8f7a622006-07-09 17:22:28 -0400779acpi_status
780acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
781{
782 acpi_status status;
783 acpi_handle tmp;
784 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
785 union acpi_object *obj;
786
787 status = acpi_get_handle(handle, "_EJD", &tmp);
788 if (ACPI_FAILURE(status))
789 return status;
790
791 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
792 if (ACPI_SUCCESS(status)) {
793 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100794 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
795 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400796 kfree(buffer.pointer);
797 }
798 return status;
799}
800EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
801
Bob Moore8e4319c2009-06-29 13:43:27 +0800802void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804
805 /* TBD */
806
807 return;
808}
809
Zhang Rui9e89dde2006-12-07 20:56:16 +0800810static int acpi_bus_get_perf_flags(struct acpi_device *device)
811{
812 device->performance.state = ACPI_STATE_UNKNOWN;
813 return 0;
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100817acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
818 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100820 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
821 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100823 acpi_status status;
824 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100826 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return AE_BAD_PARAMETER;
828
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100829 /* _PRW */
830 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
831 if (ACPI_FAILURE(status)) {
832 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
833 return status;
834 }
835
836 package = (union acpi_object *)buffer.pointer;
837
838 if (!package || (package->package.count < 2)) {
839 status = AE_BAD_DATA;
840 goto out;
841 }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100844 if (!element) {
845 status = AE_BAD_DATA;
846 goto out;
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (element->type == ACPI_TYPE_PACKAGE) {
849 if ((element->package.count < 2) ||
850 (element->package.elements[0].type !=
851 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100852 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
853 status = AE_BAD_DATA;
854 goto out;
855 }
856 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100858 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 (u32) element->package.elements[1].integer.value;
860 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100861 wakeup->gpe_device = NULL;
862 wakeup->gpe_number = element->integer.value;
863 } else {
864 status = AE_BAD_DATA;
865 goto out;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 element = &(package->package.elements[1]);
869 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100870 status = AE_BAD_DATA;
871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100873 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100876 status = AE_NO_MEMORY;
877 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100879 wakeup->resources.count = package->package.count - 2;
880 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100882 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
883 status = AE_BAD_DATA;
884 goto out;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100887 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
Lin Mingbba63a22010-12-13 13:39:17 +0800890 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200891
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100892 out:
893 kfree(buffer.pointer);
894
895 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100898static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200900 struct acpi_device_id button_device_ids[] = {
901 {"PNP0C0D", 0},
902 {"PNP0C0C", 0},
903 {"PNP0C0E", 0},
904 {"", 0},
905 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100906 acpi_status status;
907 acpi_event_status event_status;
908
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100909 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100910
911 /* Power button, Lid switch always enable wakeup */
912 if (!acpi_match_device_ids(device, button_device_ids)) {
913 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100914 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100915 return;
916 }
917
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200918 status = acpi_get_gpe_status(device->wakeup.gpe_device,
919 device->wakeup.gpe_number,
920 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100921 if (status == AE_OK)
922 device->wakeup.flags.run_wake =
923 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
924}
925
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100926static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100927{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100928 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100929 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100930 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200931
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100932 /* Presence of _PRW indicates wake capable */
933 status = acpi_get_handle(device->handle, "_PRW", &temp);
934 if (ACPI_FAILURE(status))
935 return;
936
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100937 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
938 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (ACPI_FAILURE(status)) {
940 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200945 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100946 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800947 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
948 * system for the ACPI device with the _PRW object.
949 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
950 * So it is necessary to call _DSW object first. Only when it is not
951 * present will the _PSW object used.
952 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200953 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
954 if (psw_error)
955 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
956 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100959static void acpi_bus_add_power_resource(acpi_handle handle);
960
Zhang Rui9e89dde2006-12-07 20:56:16 +0800961static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800963 acpi_status status = 0;
964 acpi_handle handle = NULL;
965 u32 i = 0;
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800969 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800971 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800973 device->power.flags.explicit_get = 1;
974 status = acpi_get_handle(device->handle, "_IRC", &handle);
975 if (ACPI_SUCCESS(status))
976 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800979 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800981 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800982 struct acpi_device_power_state *ps = &device->power.states[i];
983 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Zhang Rui9e89dde2006-12-07 20:56:16 +0800985 /* Evaluate "_PRx" to se if power resources are referenced */
986 acpi_evaluate_reference(device->handle, object_name, NULL,
987 &ps->resources);
988 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100989 int j;
990
Zhang Rui9e89dde2006-12-07 20:56:16 +0800991 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100992 for (j = 0; j < ps->resources.count; j++)
993 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Zhang Rui9e89dde2006-12-07 20:56:16 +0800996 /* Evaluate "_PSx" to see if we can do explicit sets */
997 object_name[2] = 'S';
998 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800999 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001000 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001001
Lin Ming1cc0c992012-04-23 09:03:49 +08001002 /*
1003 * State is valid if there are means to put the device into it.
1004 * D3hot is only valid if _PR3 present.
1005 */
1006 if (ps->resources.count ||
1007 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001008 ps->flags.valid = 1;
1009
1010 ps->power = -1; /* Unknown - driver assigned */
1011 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Zhang Rui9e89dde2006-12-07 20:56:16 +08001014 /* Set defaults for D0 and D3 states (always valid) */
1015 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1016 device->power.states[ACPI_STATE_D0].power = 100;
1017 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1018 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001020 /* Set D3cold's explicit_set flag if _PS3 exists. */
1021 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1022 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1023
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001024 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 return 0;
1027}
1028
Len Brown4be44fc2005-08-05 00:44:28 -04001029static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Len Brown4be44fc2005-08-05 00:44:28 -04001031 acpi_status status = AE_OK;
1032 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /* Presence of _STA indicates 'dynamic_status' */
1036 status = acpi_get_handle(device->handle, "_STA", &temp);
1037 if (ACPI_SUCCESS(status))
1038 device->flags.dynamic_status = 1;
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /* Presence of _RMV indicates 'removable' */
1041 status = acpi_get_handle(device->handle, "_RMV", &temp);
1042 if (ACPI_SUCCESS(status))
1043 device->flags.removable = 1;
1044
1045 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1046 status = acpi_get_handle(device->handle, "_EJD", &temp);
1047 if (ACPI_SUCCESS(status))
1048 device->flags.ejectable = 1;
1049 else {
1050 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1051 if (ACPI_SUCCESS(status))
1052 device->flags.ejectable = 1;
1053 }
1054
1055 /* Presence of _LCK indicates 'lockable' */
1056 status = acpi_get_handle(device->handle, "_LCK", &temp);
1057 if (ACPI_SUCCESS(status))
1058 device->flags.lockable = 1;
1059
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001060 /* Power resources cannot be power manageable. */
1061 if (device->device_type == ACPI_BUS_TYPE_POWER)
1062 return 0;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1065 status = acpi_get_handle(device->handle, "_PS0", &temp);
1066 if (ACPI_FAILURE(status))
1067 status = acpi_get_handle(device->handle, "_PR0", &temp);
1068 if (ACPI_SUCCESS(status))
1069 device->flags.power_manageable = 1;
1070
Joe Perches3c5f9be42008-02-03 17:06:17 +02001071 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Patrick Mocheld550d982006-06-27 00:41:40 -04001073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001076static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Len Brown4be44fc2005-08-05 00:44:28 -04001078 char bus_id[5] = { '?', 0 };
1079 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1080 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /*
1083 * Bus ID
1084 * ------
1085 * The device's Bus ID is simply the object name.
1086 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1087 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001088 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001090 return;
1091 }
1092
1093 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 case ACPI_BUS_TYPE_POWER_BUTTON:
1095 strcpy(device->pnp.bus_id, "PWRF");
1096 break;
1097 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1098 strcpy(device->pnp.bus_id, "SLPF");
1099 break;
1100 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001101 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* Clean up trailing underscores (if any) */
1103 for (i = 3; i > 1; i--) {
1104 if (bus_id[i] == '_')
1105 bus_id[i] = '\0';
1106 else
1107 break;
1108 }
1109 strcpy(device->pnp.bus_id, bus_id);
1110 break;
1111 }
1112}
1113
Zhang Rui54735262007-01-11 02:09:09 -05001114/*
1115 * acpi_bay_match - see if a device is an ejectable driver bay
1116 *
1117 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1118 * then we can safely call it an ejectable drive bay
1119 */
1120static int acpi_bay_match(struct acpi_device *device){
1121 acpi_status status;
1122 acpi_handle handle;
1123 acpi_handle tmp;
1124 acpi_handle phandle;
1125
1126 handle = device->handle;
1127
1128 status = acpi_get_handle(handle, "_EJ0", &tmp);
1129 if (ACPI_FAILURE(status))
1130 return -ENODEV;
1131
1132 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1133 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1134 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1135 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1136 return 0;
1137
1138 if (acpi_get_parent(handle, &phandle))
1139 return -ENODEV;
1140
1141 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1142 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1143 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1144 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1145 return 0;
1146
1147 return -ENODEV;
1148}
1149
Frank Seidel3620f2f2007-12-07 13:20:34 +01001150/*
1151 * acpi_dock_match - see if a device has a _DCK method
1152 */
1153static int acpi_dock_match(struct acpi_device *device)
1154{
1155 acpi_handle tmp;
1156 return acpi_get_handle(device->handle, "_DCK", &tmp);
1157}
1158
Thomas Renninger620e1122010-10-01 10:54:00 +02001159const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001160{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001161 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001162
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001163 if (list_empty(&device->pnp.ids))
1164 return dummy_hid;
1165
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001166 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1167 return hid->id;
1168}
1169EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001170
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001171static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1172{
1173 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001174
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001175 id = kmalloc(sizeof(*id), GFP_KERNEL);
1176 if (!id)
1177 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001178
Thomas Meyer581de592011-08-06 11:32:56 +02001179 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001180 if (!id->id) {
1181 kfree(id);
1182 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001183 }
1184
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001185 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001186}
1187
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001188/*
1189 * Old IBM workstations have a DSDT bug wherein the SMBus object
1190 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1191 * prefix. Work around this.
1192 */
1193static int acpi_ibm_smbus_match(struct acpi_device *device)
1194{
1195 acpi_handle h_dummy;
1196 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1197 int result;
1198
1199 if (!dmi_name_in_vendors("IBM"))
1200 return -ENODEV;
1201
1202 /* Look for SMBS object */
1203 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1204 if (result)
1205 return result;
1206
1207 if (strcmp("SMBS", path.pointer)) {
1208 result = -ENODEV;
1209 goto out;
1210 }
1211
1212 /* Does it have the necessary (but misnamed) methods? */
1213 result = -ENODEV;
1214 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1215 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1216 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1217 result = 0;
1218out:
1219 kfree(path.pointer);
1220 return result;
1221}
1222
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001223static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Len Brown4be44fc2005-08-05 00:44:28 -04001225 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001226 struct acpi_device_info *info;
1227 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001228 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001230 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001232 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001233 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001234 break;
1235 }
1236
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001237 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001239 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return;
1241 }
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001244 acpi_add_id(device, info->hardware_id.string);
1245 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001246 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001247 for (i = 0; i < cid_list->count; i++)
1248 acpi_add_id(device, cid_list->ids[i].string);
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (info->valid & ACPI_VALID_ADR) {
1251 device->pnp.bus_address = info->address;
1252 device->flags.bus_address = 1;
1253 }
Zhang Ruiae843332006-12-07 20:57:10 +08001254
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001255 kfree(info);
1256
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001257 /*
1258 * Some devices don't reliably have _HIDs & _CIDs, so add
1259 * synthetic HIDs to make sure drivers can find them.
1260 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001261 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001262 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001263 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001264 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001265 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001266 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001267 else if (!acpi_ibm_smbus_match(device))
1268 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001269 else if (!acpi_device_hid(device) &&
1270 ACPI_IS_ROOT_DEVICE(device->parent)) {
1271 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1272 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1273 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1274 }
Zhang Rui54735262007-01-11 02:09:09 -05001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 break;
1277 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001278 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001281 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001284 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 break;
1286 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001287 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 break;
1289 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001290 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 break;
1292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001295static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001297 acpi_status status;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /*
1300 * Context
1301 * -------
1302 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001303 * resolutions from handle->device very efficient. Fixed hardware
1304 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001306 if (!device->handle)
1307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001309 status = acpi_attach_data(device->handle,
1310 acpi_bus_data_handler, device);
1311 if (ACPI_SUCCESS(status))
1312 return 0;
1313
1314 printk(KERN_ERR PREFIX "Error attaching device data\n");
1315 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Len Brown4be44fc2005-08-05 00:44:28 -04001318static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001321 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Li Shaohua96333572006-12-07 20:56:46 +08001323 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001324 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Rui Zhang2786f6e2006-12-21 02:21:13 -05001329 /*
1330 * unbind _ADR-Based Devices when hot removal
1331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (dev->flags.bus_address) {
1333 if ((dev->parent) && (dev->parent->ops.unbind))
1334 dev->parent->ops.unbind(dev);
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1337
Patrick Mocheld550d982006-06-27 00:41:40 -04001338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001341static int acpi_add_single_object(struct acpi_device **child,
1342 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001343 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001344 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001346 int result;
1347 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001348 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Burman Yan36bcbec2006-12-19 12:56:11 -08001350 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001352 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001353 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001356 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001357 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001359 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001360 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001361 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001362
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001363 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
1366 * Flags
1367 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001368 * Note that we only look for object handles -- cannot evaluate objects
1369 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
1371 result = acpi_bus_get_flags(device);
1372 if (result)
1373 goto end;
1374
1375 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * Initialize Device
1377 * -----------------
1378 * TBD: Synch with Core's enumeration/initialization process.
1379 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001380 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /*
1383 * Power Management
1384 * ----------------
1385 */
1386 if (device->flags.power_manageable) {
1387 result = acpi_bus_get_power_flags(device);
1388 if (result)
1389 goto end;
1390 }
1391
Len Brown4be44fc2005-08-05 00:44:28 -04001392 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * Wakeup device management
1394 *-----------------------
1395 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001396 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
1399 * Performance Management
1400 * ----------------------
1401 */
1402 if (device->flags.performance_manageable) {
1403 result = acpi_bus_get_perf_flags(device);
1404 if (result)
1405 goto end;
1406 }
1407
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001408 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001409 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001411 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001414 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 */
1416 if (device->flags.bus_address) {
1417 if (device->parent && device->parent->ops.bind)
1418 device->parent->ops.bind(device);
1419 }
1420
Alex Chiang0c526d92009-05-14 08:31:37 -06001421end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001422 if (!result) {
1423 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1424 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1425 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1426 (char *) buffer.pointer,
1427 device->parent ? dev_name(&device->parent->dev) :
1428 "(null)"));
1429 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001431 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001432 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Patrick Mocheld550d982006-06-27 00:41:40 -04001434 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001437#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1438 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1439
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001440static void acpi_bus_add_power_resource(acpi_handle handle)
1441{
1442 struct acpi_bus_ops ops = {
1443 .acpi_op_add = 1,
1444 .acpi_op_start = 1,
1445 };
1446 struct acpi_device *device = NULL;
1447
1448 acpi_bus_get_device(handle, &device);
1449 if (!device)
1450 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1451 ACPI_STA_DEFAULT, &ops);
1452}
1453
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001454static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1455 unsigned long long *sta)
1456{
1457 acpi_status status;
1458 acpi_object_type acpi_type;
1459
1460 status = acpi_get_type(handle, &acpi_type);
1461 if (ACPI_FAILURE(status))
1462 return -ENODEV;
1463
1464 switch (acpi_type) {
1465 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1466 case ACPI_TYPE_DEVICE:
1467 *type = ACPI_BUS_TYPE_DEVICE;
1468 status = acpi_bus_get_status_handle(handle, sta);
1469 if (ACPI_FAILURE(status))
1470 return -ENODEV;
1471 break;
1472 case ACPI_TYPE_PROCESSOR:
1473 *type = ACPI_BUS_TYPE_PROCESSOR;
1474 status = acpi_bus_get_status_handle(handle, sta);
1475 if (ACPI_FAILURE(status))
1476 return -ENODEV;
1477 break;
1478 case ACPI_TYPE_THERMAL:
1479 *type = ACPI_BUS_TYPE_THERMAL;
1480 *sta = ACPI_STA_DEFAULT;
1481 break;
1482 case ACPI_TYPE_POWER:
1483 *type = ACPI_BUS_TYPE_POWER;
1484 *sta = ACPI_STA_DEFAULT;
1485 break;
1486 default:
1487 return -ENODEV;
1488 }
1489
1490 return 0;
1491}
1492
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001493static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1494 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001496 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001497 int type;
1498 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001499 struct acpi_device *device;
1500 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001501 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001502
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001503 result = acpi_bus_type_and_status(handle, &type, &sta);
1504 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001505 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001507 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001508 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001509 struct acpi_device_wakeup wakeup;
1510 acpi_handle temp;
1511
1512 status = acpi_get_handle(handle, "_PRW", &temp);
1513 if (ACPI_SUCCESS(status))
1514 acpi_bus_extract_wakeup_device_power_package(handle,
1515 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001516 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001519 /*
1520 * We may already have an acpi_device from a previous enumeration. If
1521 * so, we needn't add it again, but we may still have to start it.
1522 */
1523 device = NULL;
1524 acpi_bus_get_device(handle, &device);
Mika Westerberg91e56872012-10-31 22:45:02 +01001525 if (ops->acpi_op_add && !device) {
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001526 acpi_add_single_object(&device, handle, type, sta, ops);
Mika Westerberg91e56872012-10-31 22:45:02 +01001527 /* Is the device a known good platform device? */
1528 if (device
1529 && !acpi_match_device_ids(device, acpi_platform_device_ids))
1530 acpi_create_platform_device(device);
1531 }
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001532
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001533 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001534 return AE_CTRL_DEPTH;
1535
1536 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1537 status = acpi_start_single_object(device);
1538 if (ACPI_FAILURE(status))
1539 return AE_CTRL_DEPTH;
1540 }
1541
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001542 if (!*return_value)
1543 *return_value = device;
1544 return AE_OK;
1545}
1546
1547static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1548 struct acpi_device **child)
1549{
1550 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001551 void *device = NULL;
1552
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001553 status = acpi_bus_check_add(handle, 0, ops, &device);
1554 if (ACPI_SUCCESS(status))
1555 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001556 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001557
1558 if (child)
1559 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001560
1561 if (device)
1562 return 0;
1563 else
1564 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Thomas Renninger77796882010-01-29 17:48:52 +01001567/*
1568 * acpi_bus_add and acpi_bus_start
1569 *
1570 * scan a given ACPI tree and (probably recently hot-plugged)
1571 * create and add or starts found devices.
1572 *
1573 * If no devices were found -ENODEV is returned which does not
1574 * mean that this is a real error, there just have been no suitable
1575 * ACPI objects in the table trunk from which the kernel could create
1576 * a device and add/start an appropriate driver.
1577 */
1578
Rajesh Shah3fb02732005-04-28 00:25:52 -07001579int
Len Brown4be44fc2005-08-05 00:44:28 -04001580acpi_bus_add(struct acpi_device **child,
1581 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001582{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001583 struct acpi_bus_ops ops;
1584
Li Shaohuac4168bf2006-12-07 20:56:41 +08001585 memset(&ops, 0, sizeof(ops));
1586 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001587
Thomas Renninger77796882010-01-29 17:48:52 +01001588 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001589}
1590EXPORT_SYMBOL(acpi_bus_add);
1591
Len Brown4be44fc2005-08-05 00:44:28 -04001592int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001593{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001594 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001595 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001596
Thomas Renningerd2f66502010-01-29 17:48:51 +01001597 if (!device)
1598 return -EINVAL;
1599
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001600 memset(&ops, 0, sizeof(ops));
1601 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001602
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001603 result = acpi_bus_scan(device->handle, &ops, NULL);
1604
Lin Ming3a378982010-12-13 13:36:15 +08001605 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001606
1607 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001608}
1609EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Kristen Accardiceaba662006-02-23 17:56:01 -08001611int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Len Brown4be44fc2005-08-05 00:44:28 -04001613 acpi_status status;
1614 struct acpi_device *parent, *child;
1615 acpi_handle phandle, chandle;
1616 acpi_object_type type;
1617 u32 level = 1;
1618 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Len Brown4be44fc2005-08-05 00:44:28 -04001620 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 phandle = start->handle;
1622 child = chandle = NULL;
1623
1624 while ((level > 0) && parent && (!err)) {
1625 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001626 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /*
1629 * If this scope is exhausted then move our way back up.
1630 */
1631 if (ACPI_FAILURE(status)) {
1632 level--;
1633 chandle = phandle;
1634 acpi_get_parent(phandle, &phandle);
1635 child = parent;
1636 parent = parent->parent;
1637
1638 if (level == 0)
1639 err = acpi_bus_remove(child, rmdevice);
1640 else
1641 err = acpi_bus_remove(child, 1);
1642
1643 continue;
1644 }
1645
1646 status = acpi_get_type(chandle, &type);
1647 if (ACPI_FAILURE(status)) {
1648 continue;
1649 }
1650 /*
1651 * If there is a device corresponding to chandle then
1652 * parse it (depth-first).
1653 */
1654 if (acpi_bus_get_device(chandle, &child) == 0) {
1655 level++;
1656 phandle = chandle;
1657 chandle = NULL;
1658 parent = child;
1659 }
1660 continue;
1661 }
1662 return err;
1663}
Kristen Accardiceaba662006-02-23 17:56:01 -08001664EXPORT_SYMBOL_GPL(acpi_bus_trim);
1665
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001666static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Len Brown4be44fc2005-08-05 00:44:28 -04001668 int result = 0;
1669 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001670 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Li Shaohuac4168bf2006-12-07 20:56:41 +08001672 memset(&ops, 0, sizeof(ops));
1673 ops.acpi_op_add = 1;
1674 ops.acpi_op_start = 1;
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
1677 * Enumerate all fixed-feature devices.
1678 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001679 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001680 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001681 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001682 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001683 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001684 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001687 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001688 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001689 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001690 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001691 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Patrick Mocheld550d982006-06-27 00:41:40 -04001694 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Bjorn Helgaase747f272009-03-24 16:49:43 -06001697int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001700 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Li Shaohuac4168bf2006-12-07 20:56:41 +08001702 memset(&ops, 0, sizeof(ops));
1703 ops.acpi_op_add = 1;
1704 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Patrick Mochel5b327262006-05-10 10:33:00 -04001706 result = bus_register(&acpi_bus_type);
1707 if (result) {
1708 /* We don't want to quit even if we failed to add suspend/resume */
1709 printk(KERN_ERR PREFIX "Could not register bus type\n");
1710 }
1711
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001712 acpi_power_init();
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * Enumerate devices in the ACPI namespace.
1716 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001717 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001718
Li Shaohuac4168bf2006-12-07 20:56:41 +08001719 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001720 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 if (result)
1723 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001724 else
Lin Ming3a378982010-12-13 13:36:15 +08001725 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Patrick Mocheld550d982006-06-27 00:41:40 -04001727 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}