blob: a0dfdffe54d4ca3229fe77a61667d93b6c262d43 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct acpi_object_list arg_list;
101 union acpi_object arg;
102 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600103 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Zhang Rui26d46862008-04-29 02:35:48 -0400105 if (acpi_bus_get_device(handle, &device))
Toshi Kanic4753e52012-05-23 20:25:20 -0600106 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Zhang Rui26d46862008-04-29 02:35:48 -0400108 if (!device)
Toshi Kanic4753e52012-05-23 20:25:20 -0600109 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400110
111 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100112 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400113
114 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800115 printk(KERN_ERR PREFIX
116 "Removing device failed\n");
Toshi Kanic4753e52012-05-23 20:25:20 -0600117 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400118 }
119
120 /* power off device */
121 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
122 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800123 printk(KERN_WARNING PREFIX
124 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400125
126 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 arg_list.count = 1;
128 arg_list.pointer = &arg;
129 arg.type = ACPI_TYPE_INTEGER;
130 arg.integer.value = 0;
131 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
132 }
133
134 arg_list.count = 1;
135 arg_list.pointer = &arg;
136 arg.type = ACPI_TYPE_INTEGER;
137 arg.integer.value = 1;
138
139 /*
140 * TBD: _EJD support.
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600143 if (ACPI_FAILURE(status)) {
144 if (status != AE_NOT_FOUND)
145 printk(KERN_WARNING PREFIX
146 "Eject device failed\n");
147 goto err_out;
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Toshi Kanic4753e52012-05-23 20:25:20 -0600150 kfree(context);
151 return;
152
153err_out:
154 /* Inform firmware the hot-remove operation has completed w/ error */
155 (void) acpi_evaluate_hotplug_ost(handle,
156 ej_event->event, ost_code, NULL);
157 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800158 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800162acpi_eject_store(struct device *d, struct device_attribute *attr,
163 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Len Brown4be44fc2005-08-05 00:44:28 -0400165 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400167 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800168 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600169 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if ((!count) || (buf[0] != '1')) {
172 return -EINVAL;
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800175 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ret = -ENODEV;
177 goto err;
178 }
179#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800180 status = acpi_get_type(acpi_device->handle, &type);
181 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ret = -ENODEV;
183 goto err;
184 }
185
Toshi Kanic4753e52012-05-23 20:25:20 -0600186 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
187 if (!ej_event) {
188 ret = -ENOMEM;
189 goto err;
190 }
191
192 ej_event->handle = acpi_device->handle;
193 if (acpi_device->flags.eject_pending) {
194 /* event originated from ACPI eject notification */
195 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
196 acpi_device->flags.eject_pending = 0;
197 } else {
198 /* event originated from user */
199 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
200 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
201 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
202 }
203
204 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400205err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800209static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800211static ssize_t
212acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
213 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600215 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800216}
217static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
218
219static ssize_t
220acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
221 struct acpi_device *acpi_dev = to_acpi_device(dev);
222 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
223 int result;
224
225 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600226 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800227 goto end;
228
229 result = sprintf(buf, "%s\n", (char*)path.pointer);
230 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600231end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800232 return result;
233}
234static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
235
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600236/* sysfs file that shows description text from the ACPI _STR method */
237static ssize_t description_show(struct device *dev,
238 struct device_attribute *attr,
239 char *buf) {
240 struct acpi_device *acpi_dev = to_acpi_device(dev);
241 int result;
242
243 if (acpi_dev->pnp.str_obj == NULL)
244 return 0;
245
246 /*
247 * The _STR object contains a Unicode identifier for a device.
248 * We need to convert to utf-8 so it can be displayed.
249 */
250 result = utf16s_to_utf8s(
251 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
252 acpi_dev->pnp.str_obj->buffer.length,
253 UTF16_LITTLE_ENDIAN, buf,
254 PAGE_SIZE);
255
256 buf[result++] = '\n';
257
258 return result;
259}
260static DEVICE_ATTR(description, 0444, description_show, NULL);
261
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800262static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600264 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800265 acpi_status status;
266 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800267 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800269 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800270 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800271 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600272 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800273 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600274 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800275 goto end;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200278 if (!list_empty(&dev->pnp.ids)) {
279 result = device_create_file(&dev->dev, &dev_attr_hid);
280 if (result)
281 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200283 result = device_create_file(&dev->dev, &dev_attr_modalias);
284 if (result)
285 goto end;
286 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200287
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600288 /*
289 * If device has _STR, 'description' file is created
290 */
291 status = acpi_get_handle(dev->handle, "_STR", &temp);
292 if (ACPI_SUCCESS(status)) {
293 status = acpi_evaluate_object(dev->handle, "_STR",
294 NULL, &buffer);
295 if (ACPI_FAILURE(status))
296 buffer.pointer = NULL;
297 dev->pnp.str_obj = buffer.pointer;
298 result = device_create_file(&dev->dev, &dev_attr_description);
299 if (result)
300 goto end;
301 }
302
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800303 /*
304 * If device has _EJ0, 'eject' file is created that is used to trigger
305 * hot-removal function from userland.
306 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800307 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
308 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800309 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600310end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800311 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800312}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800314static void acpi_device_remove_files(struct acpi_device *dev)
315{
316 acpi_status status;
317 acpi_handle temp;
318
319 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600320 * If device has _STR, remove 'description' file
321 */
322 status = acpi_get_handle(dev->handle, "_STR", &temp);
323 if (ACPI_SUCCESS(status)) {
324 kfree(dev->pnp.str_obj);
325 device_remove_file(&dev->dev, &dev_attr_description);
326 }
327 /*
328 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800329 */
330 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
331 if (ACPI_SUCCESS(status))
332 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800333
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600334 device_remove_file(&dev->dev, &dev_attr_modalias);
335 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600336 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800337 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800338}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800340 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200342
Mika Westerbergcf761af2012-10-31 22:44:41 +0100343static const struct acpi_device_id *__acpi_match_device(
344 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200345{
346 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600347 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200348
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800349 /*
350 * If the device is not present, it is unnecessary to load device
351 * driver for it.
352 */
353 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100354 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800355
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600356 for (id = ids; id->id[0]; id++)
357 list_for_each_entry(hwid, &device->pnp.ids, list)
358 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100359 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200360
Mika Westerbergcf761af2012-10-31 22:44:41 +0100361 return NULL;
362}
363
364/**
365 * acpi_match_device - Match a struct device against a given list of ACPI IDs
366 * @ids: Array of struct acpi_device_id object to match against.
367 * @dev: The device structure to match.
368 *
369 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
370 * object for that handle and use that object to match against a given list of
371 * device IDs.
372 *
373 * Return a pointer to the first matching ID on success or %NULL on failure.
374 */
375const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
376 const struct device *dev)
377{
378 struct acpi_device *adev;
379
380 if (!ids || !dev->acpi_handle
381 || ACPI_FAILURE(acpi_bus_get_device(dev->acpi_handle, &adev)))
382 return NULL;
383
384 return __acpi_match_device(adev, ids);
385}
386EXPORT_SYMBOL_GPL(acpi_match_device);
387
388int acpi_match_device_ids(struct acpi_device *device,
389 const struct acpi_device_id *ids)
390{
391 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200392}
393EXPORT_SYMBOL(acpi_match_device_ids);
394
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600395static void acpi_free_ids(struct acpi_device *device)
396{
397 struct acpi_hardware_id *id, *tmp;
398
399 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
400 kfree(id->id);
401 kfree(id);
402 }
403}
404
Patrick Mochel1890a972006-12-07 20:56:31 +0800405static void acpi_device_release(struct device *dev)
406{
407 struct acpi_device *acpi_dev = to_acpi_device(dev);
408
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600409 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800410 kfree(acpi_dev);
411}
412
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800413static int acpi_bus_match(struct device *dev, struct device_driver *drv)
414{
415 struct acpi_device *acpi_dev = to_acpi_device(dev);
416 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
417
Thomas Renninger29b71a12007-07-23 14:43:51 +0200418 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800419}
420
Kay Sievers7eff2e72007-08-14 15:15:12 +0200421static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800422{
423 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200424 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800425
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200426 if (list_empty(&acpi_dev->pnp.ids))
427 return 0;
428
Kay Sievers7eff2e72007-08-14 15:15:12 +0200429 if (add_uevent_var(env, "MODALIAS="))
430 return -ENOMEM;
431 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
432 sizeof(env->buf) - env->buflen);
433 if (len >= (sizeof(env->buf) - env->buflen))
434 return -ENOMEM;
435 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}
438
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000439static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
440{
441 struct acpi_device *device = data;
442
443 device->driver->ops.notify(device, event);
444}
445
446static acpi_status acpi_device_notify_fixed(void *data)
447{
448 struct acpi_device *device = data;
449
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000450 /* Fixed hardware devices have no handles */
451 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000452 return AE_OK;
453}
454
455static int acpi_device_install_notify_handler(struct acpi_device *device)
456{
457 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000458
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000459 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000460 status =
461 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
462 acpi_device_notify_fixed,
463 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000464 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000465 status =
466 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
467 acpi_device_notify_fixed,
468 device);
469 else
470 status = acpi_install_notify_handler(device->handle,
471 ACPI_DEVICE_NOTIFY,
472 acpi_device_notify,
473 device);
474
475 if (ACPI_FAILURE(status))
476 return -EINVAL;
477 return 0;
478}
479
480static void acpi_device_remove_notify_handler(struct acpi_device *device)
481{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000482 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000483 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
484 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000485 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000486 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
487 acpi_device_notify_fixed);
488 else
489 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
490 acpi_device_notify);
491}
492
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800493static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
494static int acpi_start_single_object(struct acpi_device *);
495static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800496{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800497 struct acpi_device *acpi_dev = to_acpi_device(dev);
498 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
499 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800501 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
502 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800503 if (acpi_dev->bus_ops.acpi_op_start)
504 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000505
506 if (acpi_drv->ops.notify) {
507 ret = acpi_device_install_notify_handler(acpi_dev);
508 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000509 if (acpi_drv->ops.remove)
510 acpi_drv->ops.remove(acpi_dev,
511 acpi_dev->removal_type);
512 return ret;
513 }
514 }
515
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800516 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
517 "Found driver [%s] for device [%s]\n",
518 acpi_drv->name, acpi_dev->pnp.bus_id));
519 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800520 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800521 return ret;
522}
523
524static int acpi_device_remove(struct device * dev)
525{
526 struct acpi_device *acpi_dev = to_acpi_device(dev);
527 struct acpi_driver *acpi_drv = acpi_dev->driver;
528
529 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000530 if (acpi_drv->ops.notify)
531 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800532 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800533 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800534 }
535 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700536 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800537
538 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800539 return 0;
540}
541
David Brownell55955aa2007-05-08 00:28:35 -0700542struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800543 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800544 .match = acpi_bus_match,
545 .probe = acpi_device_probe,
546 .remove = acpi_device_remove,
547 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000550static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800552 int result;
553 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
554 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /*
557 * Linkage
558 * -------
559 * Link this device to its parent and siblings.
560 */
561 INIT_LIST_HEAD(&device->children);
562 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800564 INIT_LIST_HEAD(&device->physical_node_list);
565 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800567 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
568 if (!new_bus_id) {
569 printk(KERN_ERR PREFIX "Memory allocation error\n");
570 return -ENOMEM;
571 }
572
Shaohua Li90905892009-04-07 10:24:29 +0800573 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800574 /*
575 * Find suitable bus_id and instance number in acpi_bus_id_list
576 * If failed, create one and link it into acpi_bus_id_list
577 */
578 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600579 if (!strcmp(acpi_device_bus_id->bus_id,
580 acpi_device_hid(device))) {
581 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800582 found = 1;
583 kfree(new_bus_id);
584 break;
585 }
586 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600587 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800588 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600589 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800590 acpi_device_bus_id->instance_no = 0;
591 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
592 }
Kay Sievers07944692008-10-30 01:18:59 +0100593 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 +0800594
Len Brown33b57152008-12-15 22:09:26 -0500595 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (device->wakeup.flags.valid)
599 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800600 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Patrick Mochel1890a972006-12-07 20:56:31 +0800602 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000603 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800604 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800605 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600606 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600607 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600608 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800609 goto end;
610 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800611
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800612 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600613 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100614 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
615 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800616
Li Shaohua96333572006-12-07 20:56:46 +0800617 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800618 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600619end:
Shaohua Li90905892009-04-07 10:24:29 +0800620 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500621 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800622 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800623 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800624 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800625 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628static void acpi_device_unregister(struct acpi_device *device, int type)
629{
Shaohua Li90905892009-04-07 10:24:29 +0800630 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500631 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800635 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800638
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800639 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800640 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Zhang Rui9e89dde2006-12-07 20:56:16 +0800643/* --------------------------------------------------------------------------
644 Driver Management
645 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500647 * acpi_bus_driver_init - add a device to a driver
648 * @device: the device to add and initialize
649 * @driver: driver for the device
650 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600651 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800652 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
654static int
Len Brown4be44fc2005-08-05 00:44:28 -0400655acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Len Brown4be44fc2005-08-05 00:44:28 -0400657 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400660 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400663 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 result = driver->ops.add(device);
666 if (result) {
667 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700668 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 device->driver = driver;
673
674 /*
675 * TBD - Configuration Management: Assign resources to device based
676 * upon possible configuration and currently allocated resources.
677 */
678
Len Brown4be44fc2005-08-05 00:44:28 -0400679 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
680 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400681 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700682}
683
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400684static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700685{
686 int result = 0;
687 struct acpi_driver *driver;
688
Rajesh Shah3fb02732005-04-28 00:25:52 -0700689
690 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400691 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (driver->ops.start) {
694 result = driver->ops.start(device);
695 if (result && driver->ops.remove)
696 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Patrick Mocheld550d982006-06-27 00:41:40 -0400699 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500703 * acpi_bus_register_driver - register a driver with the ACPI bus
704 * @driver: driver being registered
705 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500707 * devices that match the driver's criteria and binds. Returns zero for
708 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Len Brown4be44fc2005-08-05 00:44:28 -0400710int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Patrick Mochel1890a972006-12-07 20:56:31 +0800712 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400715 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800716 driver->drv.name = driver->name;
717 driver->drv.bus = &acpi_bus_type;
718 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Patrick Mochel1890a972006-12-07 20:56:31 +0800720 ret = driver_register(&driver->drv);
721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Len Brown4be44fc2005-08-05 00:44:28 -0400724EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500727 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
728 * @driver: driver to unregister
729 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * Unregisters a driver with the ACPI bus. Searches the namespace for all
731 * devices that match the driver's criteria and unbinds.
732 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400733void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Patrick Mochel1890a972006-12-07 20:56:31 +0800735 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
Len Brown4be44fc2005-08-05 00:44:28 -0400737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738EXPORT_SYMBOL(acpi_bus_unregister_driver);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/* --------------------------------------------------------------------------
741 Device Enumeration
742 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000743static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
744{
745 acpi_status status;
746 int ret;
747 struct acpi_device *device;
748
749 /*
750 * Fixed hardware devices do not appear in the namespace and do not
751 * have handles, but we fabricate acpi_devices for them, so we have
752 * to deal with them specially.
753 */
754 if (handle == NULL)
755 return acpi_root;
756
757 do {
758 status = acpi_get_parent(handle, &handle);
759 if (status == AE_NULL_ENTRY)
760 return NULL;
761 if (ACPI_FAILURE(status))
762 return acpi_root;
763
764 ret = acpi_bus_get_device(handle, &device);
765 if (ret == 0)
766 return device;
767 } while (1);
768}
769
Len Brownc8f7a622006-07-09 17:22:28 -0400770acpi_status
771acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
772{
773 acpi_status status;
774 acpi_handle tmp;
775 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
776 union acpi_object *obj;
777
778 status = acpi_get_handle(handle, "_EJD", &tmp);
779 if (ACPI_FAILURE(status))
780 return status;
781
782 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
783 if (ACPI_SUCCESS(status)) {
784 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100785 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
786 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400787 kfree(buffer.pointer);
788 }
789 return status;
790}
791EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
792
Bob Moore8e4319c2009-06-29 13:43:27 +0800793void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795
796 /* TBD */
797
798 return;
799}
800
Zhang Rui9e89dde2006-12-07 20:56:16 +0800801static int acpi_bus_get_perf_flags(struct acpi_device *device)
802{
803 device->performance.state = ACPI_STATE_UNKNOWN;
804 return 0;
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100808acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
809 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100811 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
812 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100814 acpi_status status;
815 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100817 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return AE_BAD_PARAMETER;
819
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100820 /* _PRW */
821 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
822 if (ACPI_FAILURE(status)) {
823 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
824 return status;
825 }
826
827 package = (union acpi_object *)buffer.pointer;
828
829 if (!package || (package->package.count < 2)) {
830 status = AE_BAD_DATA;
831 goto out;
832 }
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100835 if (!element) {
836 status = AE_BAD_DATA;
837 goto out;
838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (element->type == ACPI_TYPE_PACKAGE) {
840 if ((element->package.count < 2) ||
841 (element->package.elements[0].type !=
842 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100843 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
844 status = AE_BAD_DATA;
845 goto out;
846 }
847 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100849 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 (u32) element->package.elements[1].integer.value;
851 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100852 wakeup->gpe_device = NULL;
853 wakeup->gpe_number = element->integer.value;
854 } else {
855 status = AE_BAD_DATA;
856 goto out;
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 element = &(package->package.elements[1]);
860 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100861 status = AE_BAD_DATA;
862 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100864 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100867 status = AE_NO_MEMORY;
868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100870 wakeup->resources.count = package->package.count - 2;
871 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100873 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
874 status = AE_BAD_DATA;
875 goto out;
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100878 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
Lin Mingbba63a22010-12-13 13:39:17 +0800881 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200882
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100883 out:
884 kfree(buffer.pointer);
885
886 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100889static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200891 struct acpi_device_id button_device_ids[] = {
892 {"PNP0C0D", 0},
893 {"PNP0C0C", 0},
894 {"PNP0C0E", 0},
895 {"", 0},
896 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100897 acpi_status status;
898 acpi_event_status event_status;
899
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100900 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100901
902 /* Power button, Lid switch always enable wakeup */
903 if (!acpi_match_device_ids(device, button_device_ids)) {
904 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100905 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100906 return;
907 }
908
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200909 status = acpi_get_gpe_status(device->wakeup.gpe_device,
910 device->wakeup.gpe_number,
911 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100912 if (status == AE_OK)
913 device->wakeup.flags.run_wake =
914 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
915}
916
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100917static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100918{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100919 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100920 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100921 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200922
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100923 /* Presence of _PRW indicates wake capable */
924 status = acpi_get_handle(device->handle, "_PRW", &temp);
925 if (ACPI_FAILURE(status))
926 return;
927
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100928 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
929 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (ACPI_FAILURE(status)) {
931 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100932 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200936 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100937 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800938 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
939 * system for the ACPI device with the _PRW object.
940 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
941 * So it is necessary to call _DSW object first. Only when it is not
942 * present will the _PSW object used.
943 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200944 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
945 if (psw_error)
946 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
947 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100950static void acpi_bus_add_power_resource(acpi_handle handle);
951
Zhang Rui9e89dde2006-12-07 20:56:16 +0800952static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800954 acpi_status status = 0;
955 acpi_handle handle = NULL;
956 u32 i = 0;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800960 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800962 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800964 device->power.flags.explicit_get = 1;
965 status = acpi_get_handle(device->handle, "_IRC", &handle);
966 if (ACPI_SUCCESS(status))
967 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800970 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800972 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800973 struct acpi_device_power_state *ps = &device->power.states[i];
974 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Zhang Rui9e89dde2006-12-07 20:56:16 +0800976 /* Evaluate "_PRx" to se if power resources are referenced */
977 acpi_evaluate_reference(device->handle, object_name, NULL,
978 &ps->resources);
979 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100980 int j;
981
Zhang Rui9e89dde2006-12-07 20:56:16 +0800982 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100983 for (j = 0; j < ps->resources.count; j++)
984 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Zhang Rui9e89dde2006-12-07 20:56:16 +0800987 /* Evaluate "_PSx" to see if we can do explicit sets */
988 object_name[2] = 'S';
989 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800990 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800991 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800992
Lin Ming1cc0c992012-04-23 09:03:49 +0800993 /*
994 * State is valid if there are means to put the device into it.
995 * D3hot is only valid if _PR3 present.
996 */
997 if (ps->resources.count ||
998 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800999 ps->flags.valid = 1;
1000
1001 ps->power = -1; /* Unknown - driver assigned */
1002 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Zhang Rui9e89dde2006-12-07 20:56:16 +08001005 /* Set defaults for D0 and D3 states (always valid) */
1006 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1007 device->power.states[ACPI_STATE_D0].power = 100;
1008 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1009 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001011 /* Set D3cold's explicit_set flag if _PS3 exists. */
1012 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1013 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1014
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001015 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 return 0;
1018}
1019
Len Brown4be44fc2005-08-05 00:44:28 -04001020static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Len Brown4be44fc2005-08-05 00:44:28 -04001022 acpi_status status = AE_OK;
1023 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /* Presence of _STA indicates 'dynamic_status' */
1027 status = acpi_get_handle(device->handle, "_STA", &temp);
1028 if (ACPI_SUCCESS(status))
1029 device->flags.dynamic_status = 1;
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* Presence of _RMV indicates 'removable' */
1032 status = acpi_get_handle(device->handle, "_RMV", &temp);
1033 if (ACPI_SUCCESS(status))
1034 device->flags.removable = 1;
1035
1036 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1037 status = acpi_get_handle(device->handle, "_EJD", &temp);
1038 if (ACPI_SUCCESS(status))
1039 device->flags.ejectable = 1;
1040 else {
1041 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1042 if (ACPI_SUCCESS(status))
1043 device->flags.ejectable = 1;
1044 }
1045
1046 /* Presence of _LCK indicates 'lockable' */
1047 status = acpi_get_handle(device->handle, "_LCK", &temp);
1048 if (ACPI_SUCCESS(status))
1049 device->flags.lockable = 1;
1050
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001051 /* Power resources cannot be power manageable. */
1052 if (device->device_type == ACPI_BUS_TYPE_POWER)
1053 return 0;
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1056 status = acpi_get_handle(device->handle, "_PS0", &temp);
1057 if (ACPI_FAILURE(status))
1058 status = acpi_get_handle(device->handle, "_PR0", &temp);
1059 if (ACPI_SUCCESS(status))
1060 device->flags.power_manageable = 1;
1061
Joe Perches3c5f9be42008-02-03 17:06:17 +02001062 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Patrick Mocheld550d982006-06-27 00:41:40 -04001064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001067static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Len Brown4be44fc2005-08-05 00:44:28 -04001069 char bus_id[5] = { '?', 0 };
1070 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1071 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /*
1074 * Bus ID
1075 * ------
1076 * The device's Bus ID is simply the object name.
1077 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1078 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001079 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001081 return;
1082 }
1083
1084 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 case ACPI_BUS_TYPE_POWER_BUTTON:
1086 strcpy(device->pnp.bus_id, "PWRF");
1087 break;
1088 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1089 strcpy(device->pnp.bus_id, "SLPF");
1090 break;
1091 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001092 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* Clean up trailing underscores (if any) */
1094 for (i = 3; i > 1; i--) {
1095 if (bus_id[i] == '_')
1096 bus_id[i] = '\0';
1097 else
1098 break;
1099 }
1100 strcpy(device->pnp.bus_id, bus_id);
1101 break;
1102 }
1103}
1104
Zhang Rui54735262007-01-11 02:09:09 -05001105/*
1106 * acpi_bay_match - see if a device is an ejectable driver bay
1107 *
1108 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1109 * then we can safely call it an ejectable drive bay
1110 */
1111static int acpi_bay_match(struct acpi_device *device){
1112 acpi_status status;
1113 acpi_handle handle;
1114 acpi_handle tmp;
1115 acpi_handle phandle;
1116
1117 handle = device->handle;
1118
1119 status = acpi_get_handle(handle, "_EJ0", &tmp);
1120 if (ACPI_FAILURE(status))
1121 return -ENODEV;
1122
1123 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1124 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1125 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1126 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1127 return 0;
1128
1129 if (acpi_get_parent(handle, &phandle))
1130 return -ENODEV;
1131
1132 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1133 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1134 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1135 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1136 return 0;
1137
1138 return -ENODEV;
1139}
1140
Frank Seidel3620f2f2007-12-07 13:20:34 +01001141/*
1142 * acpi_dock_match - see if a device has a _DCK method
1143 */
1144static int acpi_dock_match(struct acpi_device *device)
1145{
1146 acpi_handle tmp;
1147 return acpi_get_handle(device->handle, "_DCK", &tmp);
1148}
1149
Thomas Renninger620e1122010-10-01 10:54:00 +02001150const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001151{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001152 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001153
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001154 if (list_empty(&device->pnp.ids))
1155 return dummy_hid;
1156
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001157 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1158 return hid->id;
1159}
1160EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001161
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001162static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1163{
1164 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001165
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001166 id = kmalloc(sizeof(*id), GFP_KERNEL);
1167 if (!id)
1168 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001169
Thomas Meyer581de592011-08-06 11:32:56 +02001170 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001171 if (!id->id) {
1172 kfree(id);
1173 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001174 }
1175
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001176 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001177}
1178
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001179/*
1180 * Old IBM workstations have a DSDT bug wherein the SMBus object
1181 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1182 * prefix. Work around this.
1183 */
1184static int acpi_ibm_smbus_match(struct acpi_device *device)
1185{
1186 acpi_handle h_dummy;
1187 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1188 int result;
1189
1190 if (!dmi_name_in_vendors("IBM"))
1191 return -ENODEV;
1192
1193 /* Look for SMBS object */
1194 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1195 if (result)
1196 return result;
1197
1198 if (strcmp("SMBS", path.pointer)) {
1199 result = -ENODEV;
1200 goto out;
1201 }
1202
1203 /* Does it have the necessary (but misnamed) methods? */
1204 result = -ENODEV;
1205 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1206 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1207 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1208 result = 0;
1209out:
1210 kfree(path.pointer);
1211 return result;
1212}
1213
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001214static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Len Brown4be44fc2005-08-05 00:44:28 -04001216 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001217 struct acpi_device_info *info;
1218 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001219 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001221 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001223 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001224 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001225 break;
1226 }
1227
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001228 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001230 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
1232 }
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001235 acpi_add_id(device, info->hardware_id.string);
1236 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001237 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001238 for (i = 0; i < cid_list->count; i++)
1239 acpi_add_id(device, cid_list->ids[i].string);
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (info->valid & ACPI_VALID_ADR) {
1242 device->pnp.bus_address = info->address;
1243 device->flags.bus_address = 1;
1244 }
Zhang Ruiae843332006-12-07 20:57:10 +08001245
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001246 kfree(info);
1247
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001248 /*
1249 * Some devices don't reliably have _HIDs & _CIDs, so add
1250 * synthetic HIDs to make sure drivers can find them.
1251 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001252 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001253 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001254 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001255 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001256 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001257 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001258 else if (!acpi_ibm_smbus_match(device))
1259 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001260 else if (!acpi_device_hid(device) &&
1261 ACPI_IS_ROOT_DEVICE(device->parent)) {
1262 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1263 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1264 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1265 }
Zhang Rui54735262007-01-11 02:09:09 -05001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 break;
1268 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001269 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 break;
1271 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001272 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001275 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 break;
1277 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001278 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001281 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001286static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001288 acpi_status status;
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /*
1291 * Context
1292 * -------
1293 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001294 * resolutions from handle->device very efficient. Fixed hardware
1295 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001297 if (!device->handle)
1298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001300 status = acpi_attach_data(device->handle,
1301 acpi_bus_data_handler, device);
1302 if (ACPI_SUCCESS(status))
1303 return 0;
1304
1305 printk(KERN_ERR PREFIX "Error attaching device data\n");
1306 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Len Brown4be44fc2005-08-05 00:44:28 -04001309static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001312 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Li Shaohua96333572006-12-07 20:56:46 +08001314 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001315 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Rui Zhang2786f6e2006-12-21 02:21:13 -05001320 /*
1321 * unbind _ADR-Based Devices when hot removal
1322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (dev->flags.bus_address) {
1324 if ((dev->parent) && (dev->parent->ops.unbind))
1325 dev->parent->ops.unbind(dev);
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1328
Patrick Mocheld550d982006-06-27 00:41:40 -04001329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001332static int acpi_add_single_object(struct acpi_device **child,
1333 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001334 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001335 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001337 int result;
1338 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001339 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Burman Yan36bcbec2006-12-19 12:56:11 -08001341 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001343 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001344 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001347 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001348 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001350 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001351 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001352 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001353
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001354 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /*
1357 * Flags
1358 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001359 * Note that we only look for object handles -- cannot evaluate objects
1360 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
1362 result = acpi_bus_get_flags(device);
1363 if (result)
1364 goto end;
1365
1366 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * Initialize Device
1368 * -----------------
1369 * TBD: Synch with Core's enumeration/initialization process.
1370 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001371 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /*
1374 * Power Management
1375 * ----------------
1376 */
1377 if (device->flags.power_manageable) {
1378 result = acpi_bus_get_power_flags(device);
1379 if (result)
1380 goto end;
1381 }
1382
Len Brown4be44fc2005-08-05 00:44:28 -04001383 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * Wakeup device management
1385 *-----------------------
1386 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001387 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /*
1390 * Performance Management
1391 * ----------------------
1392 */
1393 if (device->flags.performance_manageable) {
1394 result = acpi_bus_get_perf_flags(device);
1395 if (result)
1396 goto end;
1397 }
1398
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001399 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001400 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001402 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001405 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
1407 if (device->flags.bus_address) {
1408 if (device->parent && device->parent->ops.bind)
1409 device->parent->ops.bind(device);
1410 }
1411
Alex Chiang0c526d92009-05-14 08:31:37 -06001412end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001413 if (!result) {
1414 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1415 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1416 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1417 (char *) buffer.pointer,
1418 device->parent ? dev_name(&device->parent->dev) :
1419 "(null)"));
1420 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001422 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001423 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Patrick Mocheld550d982006-06-27 00:41:40 -04001425 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001428#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1429 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1430
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001431static void acpi_bus_add_power_resource(acpi_handle handle)
1432{
1433 struct acpi_bus_ops ops = {
1434 .acpi_op_add = 1,
1435 .acpi_op_start = 1,
1436 };
1437 struct acpi_device *device = NULL;
1438
1439 acpi_bus_get_device(handle, &device);
1440 if (!device)
1441 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1442 ACPI_STA_DEFAULT, &ops);
1443}
1444
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001445static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1446 unsigned long long *sta)
1447{
1448 acpi_status status;
1449 acpi_object_type acpi_type;
1450
1451 status = acpi_get_type(handle, &acpi_type);
1452 if (ACPI_FAILURE(status))
1453 return -ENODEV;
1454
1455 switch (acpi_type) {
1456 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1457 case ACPI_TYPE_DEVICE:
1458 *type = ACPI_BUS_TYPE_DEVICE;
1459 status = acpi_bus_get_status_handle(handle, sta);
1460 if (ACPI_FAILURE(status))
1461 return -ENODEV;
1462 break;
1463 case ACPI_TYPE_PROCESSOR:
1464 *type = ACPI_BUS_TYPE_PROCESSOR;
1465 status = acpi_bus_get_status_handle(handle, sta);
1466 if (ACPI_FAILURE(status))
1467 return -ENODEV;
1468 break;
1469 case ACPI_TYPE_THERMAL:
1470 *type = ACPI_BUS_TYPE_THERMAL;
1471 *sta = ACPI_STA_DEFAULT;
1472 break;
1473 case ACPI_TYPE_POWER:
1474 *type = ACPI_BUS_TYPE_POWER;
1475 *sta = ACPI_STA_DEFAULT;
1476 break;
1477 default:
1478 return -ENODEV;
1479 }
1480
1481 return 0;
1482}
1483
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001484static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1485 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001487 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001488 int type;
1489 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001490 struct acpi_device *device;
1491 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001492 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001493
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001494 result = acpi_bus_type_and_status(handle, &type, &sta);
1495 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001496 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001498 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001499 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001500 struct acpi_device_wakeup wakeup;
1501 acpi_handle temp;
1502
1503 status = acpi_get_handle(handle, "_PRW", &temp);
1504 if (ACPI_SUCCESS(status))
1505 acpi_bus_extract_wakeup_device_power_package(handle,
1506 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001507 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001510 /*
1511 * We may already have an acpi_device from a previous enumeration. If
1512 * so, we needn't add it again, but we may still have to start it.
1513 */
1514 device = NULL;
1515 acpi_bus_get_device(handle, &device);
1516 if (ops->acpi_op_add && !device)
1517 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001518
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001519 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001520 return AE_CTRL_DEPTH;
1521
1522 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1523 status = acpi_start_single_object(device);
1524 if (ACPI_FAILURE(status))
1525 return AE_CTRL_DEPTH;
1526 }
1527
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001528 if (!*return_value)
1529 *return_value = device;
1530 return AE_OK;
1531}
1532
1533static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1534 struct acpi_device **child)
1535{
1536 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001537 void *device = NULL;
1538
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001539 status = acpi_bus_check_add(handle, 0, ops, &device);
1540 if (ACPI_SUCCESS(status))
1541 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001542 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001543
1544 if (child)
1545 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001546
1547 if (device)
1548 return 0;
1549 else
1550 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Thomas Renninger77796882010-01-29 17:48:52 +01001553/*
1554 * acpi_bus_add and acpi_bus_start
1555 *
1556 * scan a given ACPI tree and (probably recently hot-plugged)
1557 * create and add or starts found devices.
1558 *
1559 * If no devices were found -ENODEV is returned which does not
1560 * mean that this is a real error, there just have been no suitable
1561 * ACPI objects in the table trunk from which the kernel could create
1562 * a device and add/start an appropriate driver.
1563 */
1564
Rajesh Shah3fb02732005-04-28 00:25:52 -07001565int
Len Brown4be44fc2005-08-05 00:44:28 -04001566acpi_bus_add(struct acpi_device **child,
1567 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001568{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001569 struct acpi_bus_ops ops;
1570
Li Shaohuac4168bf2006-12-07 20:56:41 +08001571 memset(&ops, 0, sizeof(ops));
1572 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001573
Thomas Renninger77796882010-01-29 17:48:52 +01001574 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001575}
1576EXPORT_SYMBOL(acpi_bus_add);
1577
Len Brown4be44fc2005-08-05 00:44:28 -04001578int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001579{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001580 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001581 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001582
Thomas Renningerd2f66502010-01-29 17:48:51 +01001583 if (!device)
1584 return -EINVAL;
1585
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001586 memset(&ops, 0, sizeof(ops));
1587 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001588
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001589 result = acpi_bus_scan(device->handle, &ops, NULL);
1590
Lin Ming3a378982010-12-13 13:36:15 +08001591 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001592
1593 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001594}
1595EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Kristen Accardiceaba662006-02-23 17:56:01 -08001597int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
Len Brown4be44fc2005-08-05 00:44:28 -04001599 acpi_status status;
1600 struct acpi_device *parent, *child;
1601 acpi_handle phandle, chandle;
1602 acpi_object_type type;
1603 u32 level = 1;
1604 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Len Brown4be44fc2005-08-05 00:44:28 -04001606 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 phandle = start->handle;
1608 child = chandle = NULL;
1609
1610 while ((level > 0) && parent && (!err)) {
1611 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001612 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /*
1615 * If this scope is exhausted then move our way back up.
1616 */
1617 if (ACPI_FAILURE(status)) {
1618 level--;
1619 chandle = phandle;
1620 acpi_get_parent(phandle, &phandle);
1621 child = parent;
1622 parent = parent->parent;
1623
1624 if (level == 0)
1625 err = acpi_bus_remove(child, rmdevice);
1626 else
1627 err = acpi_bus_remove(child, 1);
1628
1629 continue;
1630 }
1631
1632 status = acpi_get_type(chandle, &type);
1633 if (ACPI_FAILURE(status)) {
1634 continue;
1635 }
1636 /*
1637 * If there is a device corresponding to chandle then
1638 * parse it (depth-first).
1639 */
1640 if (acpi_bus_get_device(chandle, &child) == 0) {
1641 level++;
1642 phandle = chandle;
1643 chandle = NULL;
1644 parent = child;
1645 }
1646 continue;
1647 }
1648 return err;
1649}
Kristen Accardiceaba662006-02-23 17:56:01 -08001650EXPORT_SYMBOL_GPL(acpi_bus_trim);
1651
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001652static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Len Brown4be44fc2005-08-05 00:44:28 -04001654 int result = 0;
1655 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001656 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Li Shaohuac4168bf2006-12-07 20:56:41 +08001658 memset(&ops, 0, sizeof(ops));
1659 ops.acpi_op_add = 1;
1660 ops.acpi_op_start = 1;
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /*
1663 * Enumerate all fixed-feature devices.
1664 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001665 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001666 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001667 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001668 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001669 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001670 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001673 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001674 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001675 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001676 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001677 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Patrick Mocheld550d982006-06-27 00:41:40 -04001680 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Bjorn Helgaase747f272009-03-24 16:49:43 -06001683int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001686 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Li Shaohuac4168bf2006-12-07 20:56:41 +08001688 memset(&ops, 0, sizeof(ops));
1689 ops.acpi_op_add = 1;
1690 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Patrick Mochel5b327262006-05-10 10:33:00 -04001692 result = bus_register(&acpi_bus_type);
1693 if (result) {
1694 /* We don't want to quit even if we failed to add suspend/resume */
1695 printk(KERN_ERR PREFIX "Could not register bus type\n");
1696 }
1697
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001698 acpi_power_init();
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 * Enumerate devices in the ACPI namespace.
1702 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001703 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001704
Li Shaohuac4168bf2006-12-07 20:56:41 +08001705 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001706 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (result)
1709 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001710 else
Lin Ming3a378982010-12-13 13:36:15 +08001711 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Patrick Mocheld550d982006-06-27 00:41:40 -04001713 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}