blob: da1416af0c8b7681d073282af04ce189af606753 [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
343int acpi_match_device_ids(struct acpi_device *device,
344 const struct acpi_device_id *ids)
345{
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)
354 return -ENODEV;
355
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))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200359 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200360
361 return -ENOENT;
362}
363EXPORT_SYMBOL(acpi_match_device_ids);
364
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600365static void acpi_free_ids(struct acpi_device *device)
366{
367 struct acpi_hardware_id *id, *tmp;
368
369 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
370 kfree(id->id);
371 kfree(id);
372 }
373}
374
Patrick Mochel1890a972006-12-07 20:56:31 +0800375static void acpi_device_release(struct device *dev)
376{
377 struct acpi_device *acpi_dev = to_acpi_device(dev);
378
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600379 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800380 kfree(acpi_dev);
381}
382
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800383static int acpi_bus_match(struct device *dev, struct device_driver *drv)
384{
385 struct acpi_device *acpi_dev = to_acpi_device(dev);
386 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
387
Thomas Renninger29b71a12007-07-23 14:43:51 +0200388 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800389}
390
Kay Sievers7eff2e72007-08-14 15:15:12 +0200391static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800392{
393 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200394 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800395
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200396 if (list_empty(&acpi_dev->pnp.ids))
397 return 0;
398
Kay Sievers7eff2e72007-08-14 15:15:12 +0200399 if (add_uevent_var(env, "MODALIAS="))
400 return -ENOMEM;
401 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
402 sizeof(env->buf) - env->buflen);
403 if (len >= (sizeof(env->buf) - env->buflen))
404 return -ENOMEM;
405 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407}
408
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000409static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
410{
411 struct acpi_device *device = data;
412
413 device->driver->ops.notify(device, event);
414}
415
416static acpi_status acpi_device_notify_fixed(void *data)
417{
418 struct acpi_device *device = data;
419
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000420 /* Fixed hardware devices have no handles */
421 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000422 return AE_OK;
423}
424
425static int acpi_device_install_notify_handler(struct acpi_device *device)
426{
427 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000428
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000429 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000430 status =
431 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
432 acpi_device_notify_fixed,
433 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000434 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000435 status =
436 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
437 acpi_device_notify_fixed,
438 device);
439 else
440 status = acpi_install_notify_handler(device->handle,
441 ACPI_DEVICE_NOTIFY,
442 acpi_device_notify,
443 device);
444
445 if (ACPI_FAILURE(status))
446 return -EINVAL;
447 return 0;
448}
449
450static void acpi_device_remove_notify_handler(struct acpi_device *device)
451{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000452 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000453 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
454 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000455 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000456 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
457 acpi_device_notify_fixed);
458 else
459 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
460 acpi_device_notify);
461}
462
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800463static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
464static int acpi_start_single_object(struct acpi_device *);
465static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800466{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800467 struct acpi_device *acpi_dev = to_acpi_device(dev);
468 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
469 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800471 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
472 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800473 if (acpi_dev->bus_ops.acpi_op_start)
474 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000475
476 if (acpi_drv->ops.notify) {
477 ret = acpi_device_install_notify_handler(acpi_dev);
478 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000479 if (acpi_drv->ops.remove)
480 acpi_drv->ops.remove(acpi_dev,
481 acpi_dev->removal_type);
482 return ret;
483 }
484 }
485
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800486 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
487 "Found driver [%s] for device [%s]\n",
488 acpi_drv->name, acpi_dev->pnp.bus_id));
489 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800490 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800491 return ret;
492}
493
494static int acpi_device_remove(struct device * dev)
495{
496 struct acpi_device *acpi_dev = to_acpi_device(dev);
497 struct acpi_driver *acpi_drv = acpi_dev->driver;
498
499 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000500 if (acpi_drv->ops.notify)
501 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800502 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800503 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800504 }
505 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700506 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800507
508 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800509 return 0;
510}
511
David Brownell55955aa2007-05-08 00:28:35 -0700512struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800513 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800514 .match = acpi_bus_match,
515 .probe = acpi_device_probe,
516 .remove = acpi_device_remove,
517 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518};
519
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000520static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800522 int result;
523 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
524 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * Linkage
528 * -------
529 * Link this device to its parent and siblings.
530 */
531 INIT_LIST_HEAD(&device->children);
532 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800534 INIT_LIST_HEAD(&device->physical_node_list);
535 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800537 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
538 if (!new_bus_id) {
539 printk(KERN_ERR PREFIX "Memory allocation error\n");
540 return -ENOMEM;
541 }
542
Shaohua Li90905892009-04-07 10:24:29 +0800543 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800544 /*
545 * Find suitable bus_id and instance number in acpi_bus_id_list
546 * If failed, create one and link it into acpi_bus_id_list
547 */
548 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600549 if (!strcmp(acpi_device_bus_id->bus_id,
550 acpi_device_hid(device))) {
551 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800552 found = 1;
553 kfree(new_bus_id);
554 break;
555 }
556 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600557 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800558 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600559 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800560 acpi_device_bus_id->instance_no = 0;
561 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
562 }
Kay Sievers07944692008-10-30 01:18:59 +0100563 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 +0800564
Len Brown33b57152008-12-15 22:09:26 -0500565 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (device->wakeup.flags.valid)
569 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800570 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Patrick Mochel1890a972006-12-07 20:56:31 +0800572 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000573 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800574 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800575 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600576 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600577 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600578 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800579 goto end;
580 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800581
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800582 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600583 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100584 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
585 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800586
Li Shaohua96333572006-12-07 20:56:46 +0800587 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800588 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600589end:
Shaohua Li90905892009-04-07 10:24:29 +0800590 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500591 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800592 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800593 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800594 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800595 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static void acpi_device_unregister(struct acpi_device *device, int type)
599{
Shaohua Li90905892009-04-07 10:24:29 +0800600 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500601 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800605 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800608
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800609 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800610 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Zhang Rui9e89dde2006-12-07 20:56:16 +0800613/* --------------------------------------------------------------------------
614 Driver Management
615 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500617 * acpi_bus_driver_init - add a device to a driver
618 * @device: the device to add and initialize
619 * @driver: driver for the device
620 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600621 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800622 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
624static int
Len Brown4be44fc2005-08-05 00:44:28 -0400625acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Len Brown4be44fc2005-08-05 00:44:28 -0400627 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400633 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 result = driver->ops.add(device);
636 if (result) {
637 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700638 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400639 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 device->driver = driver;
643
644 /*
645 * TBD - Configuration Management: Assign resources to device based
646 * upon possible configuration and currently allocated resources.
647 */
648
Len Brown4be44fc2005-08-05 00:44:28 -0400649 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
650 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400651 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700652}
653
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400654static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700655{
656 int result = 0;
657 struct acpi_driver *driver;
658
Rajesh Shah3fb02732005-04-28 00:25:52 -0700659
660 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400661 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (driver->ops.start) {
664 result = driver->ops.start(device);
665 if (result && driver->ops.remove)
666 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
Patrick Mocheld550d982006-06-27 00:41:40 -0400669 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500673 * acpi_bus_register_driver - register a driver with the ACPI bus
674 * @driver: driver being registered
675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500677 * devices that match the driver's criteria and binds. Returns zero for
678 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Len Brown4be44fc2005-08-05 00:44:28 -0400680int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Patrick Mochel1890a972006-12-07 20:56:31 +0800682 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400685 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800686 driver->drv.name = driver->name;
687 driver->drv.bus = &acpi_bus_type;
688 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Patrick Mochel1890a972006-12-07 20:56:31 +0800690 ret = driver_register(&driver->drv);
691 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Len Brown4be44fc2005-08-05 00:44:28 -0400694EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500697 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
698 * @driver: driver to unregister
699 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 * Unregisters a driver with the ACPI bus. Searches the namespace for all
701 * devices that match the driver's criteria and unbinds.
702 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400703void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Patrick Mochel1890a972006-12-07 20:56:31 +0800705 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
Len Brown4be44fc2005-08-05 00:44:28 -0400707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708EXPORT_SYMBOL(acpi_bus_unregister_driver);
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/* --------------------------------------------------------------------------
711 Device Enumeration
712 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000713static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
714{
715 acpi_status status;
716 int ret;
717 struct acpi_device *device;
718
719 /*
720 * Fixed hardware devices do not appear in the namespace and do not
721 * have handles, but we fabricate acpi_devices for them, so we have
722 * to deal with them specially.
723 */
724 if (handle == NULL)
725 return acpi_root;
726
727 do {
728 status = acpi_get_parent(handle, &handle);
729 if (status == AE_NULL_ENTRY)
730 return NULL;
731 if (ACPI_FAILURE(status))
732 return acpi_root;
733
734 ret = acpi_bus_get_device(handle, &device);
735 if (ret == 0)
736 return device;
737 } while (1);
738}
739
Len Brownc8f7a622006-07-09 17:22:28 -0400740acpi_status
741acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
742{
743 acpi_status status;
744 acpi_handle tmp;
745 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
746 union acpi_object *obj;
747
748 status = acpi_get_handle(handle, "_EJD", &tmp);
749 if (ACPI_FAILURE(status))
750 return status;
751
752 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
753 if (ACPI_SUCCESS(status)) {
754 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100755 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
756 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400757 kfree(buffer.pointer);
758 }
759 return status;
760}
761EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
762
Bob Moore8e4319c2009-06-29 13:43:27 +0800763void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765
766 /* TBD */
767
768 return;
769}
770
Zhang Rui9e89dde2006-12-07 20:56:16 +0800771static int acpi_bus_get_perf_flags(struct acpi_device *device)
772{
773 device->performance.state = ACPI_STATE_UNKNOWN;
774 return 0;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100778acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
779 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100781 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
782 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100784 acpi_status status;
785 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100787 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return AE_BAD_PARAMETER;
789
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100790 /* _PRW */
791 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
792 if (ACPI_FAILURE(status)) {
793 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
794 return status;
795 }
796
797 package = (union acpi_object *)buffer.pointer;
798
799 if (!package || (package->package.count < 2)) {
800 status = AE_BAD_DATA;
801 goto out;
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100805 if (!element) {
806 status = AE_BAD_DATA;
807 goto out;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (element->type == ACPI_TYPE_PACKAGE) {
810 if ((element->package.count < 2) ||
811 (element->package.elements[0].type !=
812 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100813 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
814 status = AE_BAD_DATA;
815 goto out;
816 }
817 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100819 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 (u32) element->package.elements[1].integer.value;
821 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100822 wakeup->gpe_device = NULL;
823 wakeup->gpe_number = element->integer.value;
824 } else {
825 status = AE_BAD_DATA;
826 goto out;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 element = &(package->package.elements[1]);
830 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100831 status = AE_BAD_DATA;
832 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100834 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100837 status = AE_NO_MEMORY;
838 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100840 wakeup->resources.count = package->package.count - 2;
841 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100843 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
844 status = AE_BAD_DATA;
845 goto out;
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100848 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
Lin Mingbba63a22010-12-13 13:39:17 +0800851 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200852
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100853 out:
854 kfree(buffer.pointer);
855
856 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100859static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200861 struct acpi_device_id button_device_ids[] = {
862 {"PNP0C0D", 0},
863 {"PNP0C0C", 0},
864 {"PNP0C0E", 0},
865 {"", 0},
866 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100867 acpi_status status;
868 acpi_event_status event_status;
869
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100870 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100871
872 /* Power button, Lid switch always enable wakeup */
873 if (!acpi_match_device_ids(device, button_device_ids)) {
874 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100875 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100876 return;
877 }
878
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200879 status = acpi_get_gpe_status(device->wakeup.gpe_device,
880 device->wakeup.gpe_number,
881 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100882 if (status == AE_OK)
883 device->wakeup.flags.run_wake =
884 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
885}
886
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100887static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100888{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100889 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100890 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100891 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200892
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100893 /* Presence of _PRW indicates wake capable */
894 status = acpi_get_handle(device->handle, "_PRW", &temp);
895 if (ACPI_FAILURE(status))
896 return;
897
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100898 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
899 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (ACPI_FAILURE(status)) {
901 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100902 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200906 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100907 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800908 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
909 * system for the ACPI device with the _PRW object.
910 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
911 * So it is necessary to call _DSW object first. Only when it is not
912 * present will the _PSW object used.
913 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200914 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
915 if (psw_error)
916 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
917 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100920static void acpi_bus_add_power_resource(acpi_handle handle);
921
Zhang Rui9e89dde2006-12-07 20:56:16 +0800922static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800924 acpi_status status = 0;
925 acpi_handle handle = NULL;
926 u32 i = 0;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800930 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800932 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800934 device->power.flags.explicit_get = 1;
935 status = acpi_get_handle(device->handle, "_IRC", &handle);
936 if (ACPI_SUCCESS(status))
937 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800940 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800942 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800943 struct acpi_device_power_state *ps = &device->power.states[i];
944 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Zhang Rui9e89dde2006-12-07 20:56:16 +0800946 /* Evaluate "_PRx" to se if power resources are referenced */
947 acpi_evaluate_reference(device->handle, object_name, NULL,
948 &ps->resources);
949 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100950 int j;
951
Zhang Rui9e89dde2006-12-07 20:56:16 +0800952 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100953 for (j = 0; j < ps->resources.count; j++)
954 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Zhang Rui9e89dde2006-12-07 20:56:16 +0800957 /* Evaluate "_PSx" to see if we can do explicit sets */
958 object_name[2] = 'S';
959 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800960 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800961 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800962
Lin Ming1cc0c992012-04-23 09:03:49 +0800963 /*
964 * State is valid if there are means to put the device into it.
965 * D3hot is only valid if _PR3 present.
966 */
967 if (ps->resources.count ||
Aaron Lu1399dfc2012-11-21 23:33:40 +0100968 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800969 ps->flags.valid = 1;
Aaron Lu1399dfc2012-11-21 23:33:40 +0100970 ps->flags.os_accessible = 1;
971 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800972
973 ps->power = -1; /* Unknown - driver assigned */
974 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Zhang Rui9e89dde2006-12-07 20:56:16 +0800977 /* Set defaults for D0 and D3 states (always valid) */
978 device->power.states[ACPI_STATE_D0].flags.valid = 1;
979 device->power.states[ACPI_STATE_D0].power = 100;
980 device->power.states[ACPI_STATE_D3].flags.valid = 1;
981 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +0200983 /* Set D3cold's explicit_set flag if _PS3 exists. */
984 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
985 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
986
Aaron Lu1399dfc2012-11-21 23:33:40 +0100987 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
988 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
989 device->power.flags.power_resources)
990 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
991
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +0100992 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 return 0;
995}
996
Len Brown4be44fc2005-08-05 00:44:28 -0400997static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Len Brown4be44fc2005-08-05 00:44:28 -0400999 acpi_status status = AE_OK;
1000 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /* Presence of _STA indicates 'dynamic_status' */
1004 status = acpi_get_handle(device->handle, "_STA", &temp);
1005 if (ACPI_SUCCESS(status))
1006 device->flags.dynamic_status = 1;
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* Presence of _RMV indicates 'removable' */
1009 status = acpi_get_handle(device->handle, "_RMV", &temp);
1010 if (ACPI_SUCCESS(status))
1011 device->flags.removable = 1;
1012
1013 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1014 status = acpi_get_handle(device->handle, "_EJD", &temp);
1015 if (ACPI_SUCCESS(status))
1016 device->flags.ejectable = 1;
1017 else {
1018 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1019 if (ACPI_SUCCESS(status))
1020 device->flags.ejectable = 1;
1021 }
1022
1023 /* Presence of _LCK indicates 'lockable' */
1024 status = acpi_get_handle(device->handle, "_LCK", &temp);
1025 if (ACPI_SUCCESS(status))
1026 device->flags.lockable = 1;
1027
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001028 /* Power resources cannot be power manageable. */
1029 if (device->device_type == ACPI_BUS_TYPE_POWER)
1030 return 0;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1033 status = acpi_get_handle(device->handle, "_PS0", &temp);
1034 if (ACPI_FAILURE(status))
1035 status = acpi_get_handle(device->handle, "_PR0", &temp);
1036 if (ACPI_SUCCESS(status))
1037 device->flags.power_manageable = 1;
1038
Joe Perches3c5f9be42008-02-03 17:06:17 +02001039 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Patrick Mocheld550d982006-06-27 00:41:40 -04001041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001044static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Len Brown4be44fc2005-08-05 00:44:28 -04001046 char bus_id[5] = { '?', 0 };
1047 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1048 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /*
1051 * Bus ID
1052 * ------
1053 * The device's Bus ID is simply the object name.
1054 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1055 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001056 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001058 return;
1059 }
1060
1061 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 case ACPI_BUS_TYPE_POWER_BUTTON:
1063 strcpy(device->pnp.bus_id, "PWRF");
1064 break;
1065 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1066 strcpy(device->pnp.bus_id, "SLPF");
1067 break;
1068 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001069 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /* Clean up trailing underscores (if any) */
1071 for (i = 3; i > 1; i--) {
1072 if (bus_id[i] == '_')
1073 bus_id[i] = '\0';
1074 else
1075 break;
1076 }
1077 strcpy(device->pnp.bus_id, bus_id);
1078 break;
1079 }
1080}
1081
Zhang Rui54735262007-01-11 02:09:09 -05001082/*
1083 * acpi_bay_match - see if a device is an ejectable driver bay
1084 *
1085 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1086 * then we can safely call it an ejectable drive bay
1087 */
1088static int acpi_bay_match(struct acpi_device *device){
1089 acpi_status status;
1090 acpi_handle handle;
1091 acpi_handle tmp;
1092 acpi_handle phandle;
1093
1094 handle = device->handle;
1095
1096 status = acpi_get_handle(handle, "_EJ0", &tmp);
1097 if (ACPI_FAILURE(status))
1098 return -ENODEV;
1099
1100 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1101 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1102 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1103 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1104 return 0;
1105
1106 if (acpi_get_parent(handle, &phandle))
1107 return -ENODEV;
1108
1109 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1110 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1111 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1112 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1113 return 0;
1114
1115 return -ENODEV;
1116}
1117
Frank Seidel3620f2f2007-12-07 13:20:34 +01001118/*
1119 * acpi_dock_match - see if a device has a _DCK method
1120 */
1121static int acpi_dock_match(struct acpi_device *device)
1122{
1123 acpi_handle tmp;
1124 return acpi_get_handle(device->handle, "_DCK", &tmp);
1125}
1126
Thomas Renninger620e1122010-10-01 10:54:00 +02001127const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001128{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001129 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001130
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001131 if (list_empty(&device->pnp.ids))
1132 return dummy_hid;
1133
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001134 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1135 return hid->id;
1136}
1137EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001138
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001139static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1140{
1141 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001142
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001143 id = kmalloc(sizeof(*id), GFP_KERNEL);
1144 if (!id)
1145 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001146
Thomas Meyer581de592011-08-06 11:32:56 +02001147 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001148 if (!id->id) {
1149 kfree(id);
1150 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001151 }
1152
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001153 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001154}
1155
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001156/*
1157 * Old IBM workstations have a DSDT bug wherein the SMBus object
1158 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1159 * prefix. Work around this.
1160 */
1161static int acpi_ibm_smbus_match(struct acpi_device *device)
1162{
1163 acpi_handle h_dummy;
1164 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1165 int result;
1166
1167 if (!dmi_name_in_vendors("IBM"))
1168 return -ENODEV;
1169
1170 /* Look for SMBS object */
1171 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1172 if (result)
1173 return result;
1174
1175 if (strcmp("SMBS", path.pointer)) {
1176 result = -ENODEV;
1177 goto out;
1178 }
1179
1180 /* Does it have the necessary (but misnamed) methods? */
1181 result = -ENODEV;
1182 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1183 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1184 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1185 result = 0;
1186out:
1187 kfree(path.pointer);
1188 return result;
1189}
1190
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001191static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Len Brown4be44fc2005-08-05 00:44:28 -04001193 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001194 struct acpi_device_info *info;
1195 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001196 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001198 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001200 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001201 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001202 break;
1203 }
1204
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001205 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001207 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return;
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001212 acpi_add_id(device, info->hardware_id.string);
1213 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001214 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001215 for (i = 0; i < cid_list->count; i++)
1216 acpi_add_id(device, cid_list->ids[i].string);
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (info->valid & ACPI_VALID_ADR) {
1219 device->pnp.bus_address = info->address;
1220 device->flags.bus_address = 1;
1221 }
Zhang Ruiae843332006-12-07 20:57:10 +08001222
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001223 kfree(info);
1224
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001225 /*
1226 * Some devices don't reliably have _HIDs & _CIDs, so add
1227 * synthetic HIDs to make sure drivers can find them.
1228 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001229 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001230 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001231 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001232 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001233 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001234 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001235 else if (!acpi_ibm_smbus_match(device))
1236 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001237 else if (!acpi_device_hid(device) &&
1238 ACPI_IS_ROOT_DEVICE(device->parent)) {
1239 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1240 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1241 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1242 }
Zhang Rui54735262007-01-11 02:09:09 -05001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 break;
1245 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001246 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
1248 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001249 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001252 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001255 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
1257 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001258 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 break;
1260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001263static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001265 acpi_status status;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /*
1268 * Context
1269 * -------
1270 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001271 * resolutions from handle->device very efficient. Fixed hardware
1272 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001274 if (!device->handle)
1275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001277 status = acpi_attach_data(device->handle,
1278 acpi_bus_data_handler, device);
1279 if (ACPI_SUCCESS(status))
1280 return 0;
1281
1282 printk(KERN_ERR PREFIX "Error attaching device data\n");
1283 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Len Brown4be44fc2005-08-05 00:44:28 -04001286static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001289 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Li Shaohua96333572006-12-07 20:56:46 +08001291 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001292 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Rui Zhang2786f6e2006-12-21 02:21:13 -05001297 /*
1298 * unbind _ADR-Based Devices when hot removal
1299 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (dev->flags.bus_address) {
1301 if ((dev->parent) && (dev->parent->ops.unbind))
1302 dev->parent->ops.unbind(dev);
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1305
Patrick Mocheld550d982006-06-27 00:41:40 -04001306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001309static int acpi_add_single_object(struct acpi_device **child,
1310 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001311 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001312 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001314 int result;
1315 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001316 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Burman Yan36bcbec2006-12-19 12:56:11 -08001318 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001320 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001321 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001324 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001325 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001327 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001328 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001329 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001330
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001331 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /*
1334 * Flags
1335 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001336 * Note that we only look for object handles -- cannot evaluate objects
1337 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
1339 result = acpi_bus_get_flags(device);
1340 if (result)
1341 goto end;
1342
1343 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * Initialize Device
1345 * -----------------
1346 * TBD: Synch with Core's enumeration/initialization process.
1347 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001348 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /*
1351 * Power Management
1352 * ----------------
1353 */
1354 if (device->flags.power_manageable) {
1355 result = acpi_bus_get_power_flags(device);
1356 if (result)
1357 goto end;
1358 }
1359
Len Brown4be44fc2005-08-05 00:44:28 -04001360 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * Wakeup device management
1362 *-----------------------
1363 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001364 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /*
1367 * Performance Management
1368 * ----------------------
1369 */
1370 if (device->flags.performance_manageable) {
1371 result = acpi_bus_get_perf_flags(device);
1372 if (result)
1373 goto end;
1374 }
1375
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001376 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001377 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001379 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001382 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
1384 if (device->flags.bus_address) {
1385 if (device->parent && device->parent->ops.bind)
1386 device->parent->ops.bind(device);
1387 }
1388
Alex Chiang0c526d92009-05-14 08:31:37 -06001389end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001390 if (!result) {
1391 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1392 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1393 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1394 (char *) buffer.pointer,
1395 device->parent ? dev_name(&device->parent->dev) :
1396 "(null)"));
1397 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001399 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001400 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Patrick Mocheld550d982006-06-27 00:41:40 -04001402 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001405#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1406 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1407
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001408static void acpi_bus_add_power_resource(acpi_handle handle)
1409{
1410 struct acpi_bus_ops ops = {
1411 .acpi_op_add = 1,
1412 .acpi_op_start = 1,
1413 };
1414 struct acpi_device *device = NULL;
1415
1416 acpi_bus_get_device(handle, &device);
1417 if (!device)
1418 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1419 ACPI_STA_DEFAULT, &ops);
1420}
1421
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001422static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1423 unsigned long long *sta)
1424{
1425 acpi_status status;
1426 acpi_object_type acpi_type;
1427
1428 status = acpi_get_type(handle, &acpi_type);
1429 if (ACPI_FAILURE(status))
1430 return -ENODEV;
1431
1432 switch (acpi_type) {
1433 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1434 case ACPI_TYPE_DEVICE:
1435 *type = ACPI_BUS_TYPE_DEVICE;
1436 status = acpi_bus_get_status_handle(handle, sta);
1437 if (ACPI_FAILURE(status))
1438 return -ENODEV;
1439 break;
1440 case ACPI_TYPE_PROCESSOR:
1441 *type = ACPI_BUS_TYPE_PROCESSOR;
1442 status = acpi_bus_get_status_handle(handle, sta);
1443 if (ACPI_FAILURE(status))
1444 return -ENODEV;
1445 break;
1446 case ACPI_TYPE_THERMAL:
1447 *type = ACPI_BUS_TYPE_THERMAL;
1448 *sta = ACPI_STA_DEFAULT;
1449 break;
1450 case ACPI_TYPE_POWER:
1451 *type = ACPI_BUS_TYPE_POWER;
1452 *sta = ACPI_STA_DEFAULT;
1453 break;
1454 default:
1455 return -ENODEV;
1456 }
1457
1458 return 0;
1459}
1460
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001461static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1462 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001464 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001465 int type;
1466 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001467 struct acpi_device *device;
1468 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001469 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001470
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001471 result = acpi_bus_type_and_status(handle, &type, &sta);
1472 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001473 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001475 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001476 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001477 struct acpi_device_wakeup wakeup;
1478 acpi_handle temp;
1479
1480 status = acpi_get_handle(handle, "_PRW", &temp);
1481 if (ACPI_SUCCESS(status))
1482 acpi_bus_extract_wakeup_device_power_package(handle,
1483 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001484 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001487 /*
1488 * We may already have an acpi_device from a previous enumeration. If
1489 * so, we needn't add it again, but we may still have to start it.
1490 */
1491 device = NULL;
1492 acpi_bus_get_device(handle, &device);
1493 if (ops->acpi_op_add && !device)
1494 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001495
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001496 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001497 return AE_CTRL_DEPTH;
1498
1499 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1500 status = acpi_start_single_object(device);
1501 if (ACPI_FAILURE(status))
1502 return AE_CTRL_DEPTH;
1503 }
1504
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001505 if (!*return_value)
1506 *return_value = device;
1507 return AE_OK;
1508}
1509
1510static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1511 struct acpi_device **child)
1512{
1513 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001514 void *device = NULL;
1515
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001516 status = acpi_bus_check_add(handle, 0, ops, &device);
1517 if (ACPI_SUCCESS(status))
1518 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001519 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001520
1521 if (child)
1522 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001523
1524 if (device)
1525 return 0;
1526 else
1527 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Thomas Renninger77796882010-01-29 17:48:52 +01001530/*
1531 * acpi_bus_add and acpi_bus_start
1532 *
1533 * scan a given ACPI tree and (probably recently hot-plugged)
1534 * create and add or starts found devices.
1535 *
1536 * If no devices were found -ENODEV is returned which does not
1537 * mean that this is a real error, there just have been no suitable
1538 * ACPI objects in the table trunk from which the kernel could create
1539 * a device and add/start an appropriate driver.
1540 */
1541
Rajesh Shah3fb02732005-04-28 00:25:52 -07001542int
Len Brown4be44fc2005-08-05 00:44:28 -04001543acpi_bus_add(struct acpi_device **child,
1544 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001545{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001546 struct acpi_bus_ops ops;
1547
Li Shaohuac4168bf2006-12-07 20:56:41 +08001548 memset(&ops, 0, sizeof(ops));
1549 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001550
Thomas Renninger77796882010-01-29 17:48:52 +01001551 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001552}
1553EXPORT_SYMBOL(acpi_bus_add);
1554
Len Brown4be44fc2005-08-05 00:44:28 -04001555int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001556{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001557 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001558 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001559
Thomas Renningerd2f66502010-01-29 17:48:51 +01001560 if (!device)
1561 return -EINVAL;
1562
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001563 memset(&ops, 0, sizeof(ops));
1564 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001565
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001566 result = acpi_bus_scan(device->handle, &ops, NULL);
1567
Lin Ming3a378982010-12-13 13:36:15 +08001568 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001569
1570 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001571}
1572EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Kristen Accardiceaba662006-02-23 17:56:01 -08001574int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Len Brown4be44fc2005-08-05 00:44:28 -04001576 acpi_status status;
1577 struct acpi_device *parent, *child;
1578 acpi_handle phandle, chandle;
1579 acpi_object_type type;
1580 u32 level = 1;
1581 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Len Brown4be44fc2005-08-05 00:44:28 -04001583 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 phandle = start->handle;
1585 child = chandle = NULL;
1586
1587 while ((level > 0) && parent && (!err)) {
1588 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001589 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 /*
1592 * If this scope is exhausted then move our way back up.
1593 */
1594 if (ACPI_FAILURE(status)) {
1595 level--;
1596 chandle = phandle;
1597 acpi_get_parent(phandle, &phandle);
1598 child = parent;
1599 parent = parent->parent;
1600
1601 if (level == 0)
1602 err = acpi_bus_remove(child, rmdevice);
1603 else
1604 err = acpi_bus_remove(child, 1);
1605
1606 continue;
1607 }
1608
1609 status = acpi_get_type(chandle, &type);
1610 if (ACPI_FAILURE(status)) {
1611 continue;
1612 }
1613 /*
1614 * If there is a device corresponding to chandle then
1615 * parse it (depth-first).
1616 */
1617 if (acpi_bus_get_device(chandle, &child) == 0) {
1618 level++;
1619 phandle = chandle;
1620 chandle = NULL;
1621 parent = child;
1622 }
1623 continue;
1624 }
1625 return err;
1626}
Kristen Accardiceaba662006-02-23 17:56:01 -08001627EXPORT_SYMBOL_GPL(acpi_bus_trim);
1628
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001629static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Len Brown4be44fc2005-08-05 00:44:28 -04001631 int result = 0;
1632 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001633 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Li Shaohuac4168bf2006-12-07 20:56:41 +08001635 memset(&ops, 0, sizeof(ops));
1636 ops.acpi_op_add = 1;
1637 ops.acpi_op_start = 1;
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Enumerate all fixed-feature devices.
1641 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001642 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001643 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001644 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001645 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001646 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001647 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001650 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001651 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001652 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001653 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001654 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Patrick Mocheld550d982006-06-27 00:41:40 -04001657 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Bjorn Helgaase747f272009-03-24 16:49:43 -06001660int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001663 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Li Shaohuac4168bf2006-12-07 20:56:41 +08001665 memset(&ops, 0, sizeof(ops));
1666 ops.acpi_op_add = 1;
1667 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Patrick Mochel5b327262006-05-10 10:33:00 -04001669 result = bus_register(&acpi_bus_type);
1670 if (result) {
1671 /* We don't want to quit even if we failed to add suspend/resume */
1672 printk(KERN_ERR PREFIX "Could not register bus type\n");
1673 }
1674
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001675 acpi_power_init();
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 * Enumerate devices in the ACPI namespace.
1679 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001680 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001681
Li Shaohuac4168bf2006-12-07 20:56:41 +08001682 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001683 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if (result)
1686 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001687 else
Lin Ming3a378982010-12-13 13:36:15 +08001688 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Patrick Mocheld550d982006-06-27 00:41:40 -04001690 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}