blob: 9d43532d69b171e0a063a9b4585e5bf532023319 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Thomas Renninger620e1122010-10-01 10:54:00 +020030static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080033static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080034DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035LIST_HEAD(acpi_wakeup_device_list);
36
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080037struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080038 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039 unsigned int instance_no;
40 struct list_head node;
41};
Thomas Renninger29b71a12007-07-23 14:43:51 +020042
43/*
44 * Creates hid/cid(s) string needed for modalias and uevent
45 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
46 * char *modalias: "acpi:IBM0001:ACPI0001"
47*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020048static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
49 int size)
50{
Thomas Renninger29b71a12007-07-23 14:43:51 +020051 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080052 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060053 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020054
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020055 if (list_empty(&acpi_dev->pnp.ids))
56 return 0;
57
Zhang Rui5c9fcb52008-03-20 16:40:32 +080058 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020059 size -= len;
60
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060061 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
62 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080063 if (count < 0 || count >= size)
64 return -EINVAL;
65 len += count;
66 size -= count;
67 }
68
Thomas Renninger29b71a12007-07-23 14:43:51 +020069 modalias[len] = '\0';
70 return len;
71}
72
73static ssize_t
74acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
75 struct acpi_device *acpi_dev = to_acpi_device(dev);
76 int len;
77
78 /* Device has no HID and no CID or string is >1024 */
79 len = create_modalias(acpi_dev, buf, 1024);
80 if (len <= 0)
81 return 0;
82 buf[len++] = '\n';
83 return len;
84}
85static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
86
Toshi Kanic4753e52012-05-23 20:25:20 -060087/**
88 * acpi_bus_hot_remove_device: hot-remove a device and its children
89 * @context: struct acpi_eject_event pointer (freed in this func)
90 *
91 * Hot-remove a device and its children. This function frees up the
92 * memory space passed by arg context, so that the caller may call
93 * this function asynchronously through acpi_os_hotplug_execute().
94 */
95void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Toshi Kanic4753e52012-05-23 20:25:20 -060097 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
Zhang Rui26d46862008-04-29 02:35:48 -040098 struct acpi_device *device;
Toshi Kanic4753e52012-05-23 20:25:20 -060099 acpi_handle handle = ej_event->handle;
Toshi Kanib3c450c2012-10-26 13:38:57 +0200100 acpi_handle temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct acpi_object_list arg_list;
102 union acpi_object arg;
103 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600104 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Zhang Rui26d46862008-04-29 02:35:48 -0400106 if (acpi_bus_get_device(handle, &device))
Toshi Kanic4753e52012-05-23 20:25:20 -0600107 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Zhang Rui26d46862008-04-29 02:35:48 -0400109 if (!device)
Toshi Kanic4753e52012-05-23 20:25:20 -0600110 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400111
112 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100113 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400114
115 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800116 printk(KERN_ERR PREFIX
117 "Removing device failed\n");
Toshi Kanic4753e52012-05-23 20:25:20 -0600118 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400119 }
120
Toshi Kanib3c450c2012-10-26 13:38:57 +0200121 /* device has been freed */
122 device = NULL;
123
Zhang Rui26d46862008-04-29 02:35:48 -0400124 /* power off device */
125 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
126 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800127 printk(KERN_WARNING PREFIX
128 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400129
Toshi Kanib3c450c2012-10-26 13:38:57 +0200130 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 arg_list.count = 1;
132 arg_list.pointer = &arg;
133 arg.type = ACPI_TYPE_INTEGER;
134 arg.integer.value = 0;
135 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
136 }
137
138 arg_list.count = 1;
139 arg_list.pointer = &arg;
140 arg.type = ACPI_TYPE_INTEGER;
141 arg.integer.value = 1;
142
143 /*
144 * TBD: _EJD support.
145 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600147 if (ACPI_FAILURE(status)) {
148 if (status != AE_NOT_FOUND)
149 printk(KERN_WARNING PREFIX
150 "Eject device failed\n");
151 goto err_out;
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Toshi Kanic4753e52012-05-23 20:25:20 -0600154 kfree(context);
155 return;
156
157err_out:
158 /* Inform firmware the hot-remove operation has completed w/ error */
159 (void) acpi_evaluate_hotplug_ost(handle,
160 ej_event->event, ost_code, NULL);
161 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800162 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
Toshi Kani61622ac2012-11-01 14:42:12 +0000164EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800167acpi_eject_store(struct device *d, struct device_attribute *attr,
168 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Len Brown4be44fc2005-08-05 00:44:28 -0400170 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400171 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400172 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800173 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600174 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if ((!count) || (buf[0] != '1')) {
177 return -EINVAL;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800180 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ret = -ENODEV;
182 goto err;
183 }
184#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800185 status = acpi_get_type(acpi_device->handle, &type);
186 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ret = -ENODEV;
188 goto err;
189 }
190
Toshi Kanic4753e52012-05-23 20:25:20 -0600191 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
192 if (!ej_event) {
193 ret = -ENOMEM;
194 goto err;
195 }
196
197 ej_event->handle = acpi_device->handle;
198 if (acpi_device->flags.eject_pending) {
199 /* event originated from ACPI eject notification */
200 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
201 acpi_device->flags.eject_pending = 0;
202 } else {
203 /* event originated from user */
204 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
205 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
206 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
207 }
208
209 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400210err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800214static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800216static ssize_t
217acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
218 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600220 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800221}
222static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
223
Lv Zheng923d4a42012-10-30 14:43:26 +0100224static ssize_t acpi_device_uid_show(struct device *dev,
225 struct device_attribute *attr, char *buf)
226{
227 struct acpi_device *acpi_dev = to_acpi_device(dev);
228
229 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
230}
231static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
232
233static ssize_t acpi_device_adr_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
235{
236 struct acpi_device *acpi_dev = to_acpi_device(dev);
237
238 return sprintf(buf, "0x%08x\n",
239 (unsigned int)(acpi_dev->pnp.bus_address));
240}
241static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
242
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800243static ssize_t
244acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
245 struct acpi_device *acpi_dev = to_acpi_device(dev);
246 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
247 int result;
248
249 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600250 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800251 goto end;
252
253 result = sprintf(buf, "%s\n", (char*)path.pointer);
254 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600255end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800256 return result;
257}
258static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
259
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600260/* sysfs file that shows description text from the ACPI _STR method */
261static ssize_t description_show(struct device *dev,
262 struct device_attribute *attr,
263 char *buf) {
264 struct acpi_device *acpi_dev = to_acpi_device(dev);
265 int result;
266
267 if (acpi_dev->pnp.str_obj == NULL)
268 return 0;
269
270 /*
271 * The _STR object contains a Unicode identifier for a device.
272 * We need to convert to utf-8 so it can be displayed.
273 */
274 result = utf16s_to_utf8s(
275 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
276 acpi_dev->pnp.str_obj->buffer.length,
277 UTF16_LITTLE_ENDIAN, buf,
278 PAGE_SIZE);
279
280 buf[result++] = '\n';
281
282 return result;
283}
284static DEVICE_ATTR(description, 0444, description_show, NULL);
285
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800286static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600288 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800289 acpi_status status;
290 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800291 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800293 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800294 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800295 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600296 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800297 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600298 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800299 goto end;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200302 if (!list_empty(&dev->pnp.ids)) {
303 result = device_create_file(&dev->dev, &dev_attr_hid);
304 if (result)
305 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200307 result = device_create_file(&dev->dev, &dev_attr_modalias);
308 if (result)
309 goto end;
310 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200311
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600312 /*
313 * If device has _STR, 'description' file is created
314 */
315 status = acpi_get_handle(dev->handle, "_STR", &temp);
316 if (ACPI_SUCCESS(status)) {
317 status = acpi_evaluate_object(dev->handle, "_STR",
318 NULL, &buffer);
319 if (ACPI_FAILURE(status))
320 buffer.pointer = NULL;
321 dev->pnp.str_obj = buffer.pointer;
322 result = device_create_file(&dev->dev, &dev_attr_description);
323 if (result)
324 goto end;
325 }
326
Lv Zheng923d4a42012-10-30 14:43:26 +0100327 if (dev->flags.bus_address)
328 result = device_create_file(&dev->dev, &dev_attr_adr);
329 if (dev->pnp.unique_id)
330 result = device_create_file(&dev->dev, &dev_attr_uid);
331
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800332 /*
333 * If device has _EJ0, 'eject' file is created that is used to trigger
334 * hot-removal function from userland.
335 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800336 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
337 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800338 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600339end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800340 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800341}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800343static void acpi_device_remove_files(struct acpi_device *dev)
344{
345 acpi_status status;
346 acpi_handle temp;
347
348 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600349 * If device has _STR, remove 'description' file
350 */
351 status = acpi_get_handle(dev->handle, "_STR", &temp);
352 if (ACPI_SUCCESS(status)) {
353 kfree(dev->pnp.str_obj);
354 device_remove_file(&dev->dev, &dev_attr_description);
355 }
356 /*
357 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800358 */
359 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
360 if (ACPI_SUCCESS(status))
361 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800362
Lv Zheng923d4a42012-10-30 14:43:26 +0100363 if (dev->pnp.unique_id)
364 device_remove_file(&dev->dev, &dev_attr_uid);
365 if (dev->flags.bus_address)
366 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600367 device_remove_file(&dev->dev, &dev_attr_modalias);
368 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600369 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800370 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800373 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200375
376int acpi_match_device_ids(struct acpi_device *device,
377 const struct acpi_device_id *ids)
378{
379 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600380 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200381
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800382 /*
383 * If the device is not present, it is unnecessary to load device
384 * driver for it.
385 */
386 if (!device->status.present)
387 return -ENODEV;
388
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600389 for (id = ids; id->id[0]; id++)
390 list_for_each_entry(hwid, &device->pnp.ids, list)
391 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200392 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200393
394 return -ENOENT;
395}
396EXPORT_SYMBOL(acpi_match_device_ids);
397
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600398static void acpi_free_ids(struct acpi_device *device)
399{
400 struct acpi_hardware_id *id, *tmp;
401
402 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
403 kfree(id->id);
404 kfree(id);
405 }
406}
407
Patrick Mochel1890a972006-12-07 20:56:31 +0800408static void acpi_device_release(struct device *dev)
409{
410 struct acpi_device *acpi_dev = to_acpi_device(dev);
411
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600412 acpi_free_ids(acpi_dev);
Lv Zhengccf78042012-10-30 14:41:07 +0100413 kfree(acpi_dev->pnp.unique_id);
Patrick Mochel1890a972006-12-07 20:56:31 +0800414 kfree(acpi_dev);
415}
416
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800417static int acpi_bus_match(struct device *dev, struct device_driver *drv)
418{
419 struct acpi_device *acpi_dev = to_acpi_device(dev);
420 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
421
Thomas Renninger29b71a12007-07-23 14:43:51 +0200422 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800423}
424
Kay Sievers7eff2e72007-08-14 15:15:12 +0200425static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800426{
427 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200428 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800429
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200430 if (list_empty(&acpi_dev->pnp.ids))
431 return 0;
432
Kay Sievers7eff2e72007-08-14 15:15:12 +0200433 if (add_uevent_var(env, "MODALIAS="))
434 return -ENOMEM;
435 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
436 sizeof(env->buf) - env->buflen);
437 if (len >= (sizeof(env->buf) - env->buflen))
438 return -ENOMEM;
439 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
441}
442
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000443static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
444{
445 struct acpi_device *device = data;
446
447 device->driver->ops.notify(device, event);
448}
449
450static acpi_status acpi_device_notify_fixed(void *data)
451{
452 struct acpi_device *device = data;
453
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000454 /* Fixed hardware devices have no handles */
455 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000456 return AE_OK;
457}
458
459static int acpi_device_install_notify_handler(struct acpi_device *device)
460{
461 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000462
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000463 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000464 status =
465 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
466 acpi_device_notify_fixed,
467 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000468 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000469 status =
470 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
471 acpi_device_notify_fixed,
472 device);
473 else
474 status = acpi_install_notify_handler(device->handle,
475 ACPI_DEVICE_NOTIFY,
476 acpi_device_notify,
477 device);
478
479 if (ACPI_FAILURE(status))
480 return -EINVAL;
481 return 0;
482}
483
484static void acpi_device_remove_notify_handler(struct acpi_device *device)
485{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000486 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000487 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
488 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000489 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000490 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
491 acpi_device_notify_fixed);
492 else
493 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
494 acpi_device_notify);
495}
496
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800497static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
498static int acpi_start_single_object(struct acpi_device *);
499static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800500{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800501 struct acpi_device *acpi_dev = to_acpi_device(dev);
502 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
503 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800505 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
506 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800507 if (acpi_dev->bus_ops.acpi_op_start)
508 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000509
510 if (acpi_drv->ops.notify) {
511 ret = acpi_device_install_notify_handler(acpi_dev);
512 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000513 if (acpi_drv->ops.remove)
514 acpi_drv->ops.remove(acpi_dev,
515 acpi_dev->removal_type);
516 return ret;
517 }
518 }
519
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800520 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
521 "Found driver [%s] for device [%s]\n",
522 acpi_drv->name, acpi_dev->pnp.bus_id));
523 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800524 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800525 return ret;
526}
527
528static int acpi_device_remove(struct device * dev)
529{
530 struct acpi_device *acpi_dev = to_acpi_device(dev);
531 struct acpi_driver *acpi_drv = acpi_dev->driver;
532
533 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000534 if (acpi_drv->ops.notify)
535 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800536 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800537 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800538 }
539 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700540 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800541
542 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800543 return 0;
544}
545
David Brownell55955aa2007-05-08 00:28:35 -0700546struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800547 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800548 .match = acpi_bus_match,
549 .probe = acpi_device_probe,
550 .remove = acpi_device_remove,
551 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552};
553
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000554static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800556 int result;
557 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
558 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /*
561 * Linkage
562 * -------
563 * Link this device to its parent and siblings.
564 */
565 INIT_LIST_HEAD(&device->children);
566 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800568 INIT_LIST_HEAD(&device->physical_node_list);
569 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800571 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
572 if (!new_bus_id) {
573 printk(KERN_ERR PREFIX "Memory allocation error\n");
574 return -ENOMEM;
575 }
576
Shaohua Li90905892009-04-07 10:24:29 +0800577 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800578 /*
579 * Find suitable bus_id and instance number in acpi_bus_id_list
580 * If failed, create one and link it into acpi_bus_id_list
581 */
582 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600583 if (!strcmp(acpi_device_bus_id->bus_id,
584 acpi_device_hid(device))) {
585 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800586 found = 1;
587 kfree(new_bus_id);
588 break;
589 }
590 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600591 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800592 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600593 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800594 acpi_device_bus_id->instance_no = 0;
595 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
596 }
Kay Sievers07944692008-10-30 01:18:59 +0100597 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 +0800598
Len Brown33b57152008-12-15 22:09:26 -0500599 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (device->wakeup.flags.valid)
603 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800604 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Patrick Mochel1890a972006-12-07 20:56:31 +0800606 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000607 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800608 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800609 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600610 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600611 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600612 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800613 goto end;
614 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800615
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800616 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600617 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100618 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
619 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800620
Li Shaohua96333572006-12-07 20:56:46 +0800621 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800622 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600623end:
Shaohua Li90905892009-04-07 10:24:29 +0800624 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500625 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800626 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800627 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800628 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800629 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632static void acpi_device_unregister(struct acpi_device *device, int type)
633{
Shaohua Li90905892009-04-07 10:24:29 +0800634 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500635 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800639 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800642
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800643 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800644 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Zhang Rui9e89dde2006-12-07 20:56:16 +0800647/* --------------------------------------------------------------------------
648 Driver Management
649 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500651 * acpi_bus_driver_init - add a device to a driver
652 * @device: the device to add and initialize
653 * @driver: driver for the device
654 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600655 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800656 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658static int
Len Brown4be44fc2005-08-05 00:44:28 -0400659acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Len Brown4be44fc2005-08-05 00:44:28 -0400661 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400664 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400667 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 result = driver->ops.add(device);
670 if (result) {
671 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700672 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400673 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
676 device->driver = driver;
677
678 /*
679 * TBD - Configuration Management: Assign resources to device based
680 * upon possible configuration and currently allocated resources.
681 */
682
Len Brown4be44fc2005-08-05 00:44:28 -0400683 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
684 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400685 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700686}
687
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400688static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700689{
690 int result = 0;
691 struct acpi_driver *driver;
692
Rajesh Shah3fb02732005-04-28 00:25:52 -0700693
694 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400695 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (driver->ops.start) {
698 result = driver->ops.start(device);
699 if (result && driver->ops.remove)
700 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
Patrick Mocheld550d982006-06-27 00:41:40 -0400703 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500707 * acpi_bus_register_driver - register a driver with the ACPI bus
708 * @driver: driver being registered
709 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500711 * devices that match the driver's criteria and binds. Returns zero for
712 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Len Brown4be44fc2005-08-05 00:44:28 -0400714int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Patrick Mochel1890a972006-12-07 20:56:31 +0800716 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400719 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800720 driver->drv.name = driver->name;
721 driver->drv.bus = &acpi_bus_type;
722 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Patrick Mochel1890a972006-12-07 20:56:31 +0800724 ret = driver_register(&driver->drv);
725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Len Brown4be44fc2005-08-05 00:44:28 -0400728EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500731 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
732 * @driver: driver to unregister
733 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * Unregisters a driver with the ACPI bus. Searches the namespace for all
735 * devices that match the driver's criteria and unbinds.
736 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400737void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Patrick Mochel1890a972006-12-07 20:56:31 +0800739 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
Len Brown4be44fc2005-08-05 00:44:28 -0400741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742EXPORT_SYMBOL(acpi_bus_unregister_driver);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* --------------------------------------------------------------------------
745 Device Enumeration
746 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000747static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
748{
749 acpi_status status;
750 int ret;
751 struct acpi_device *device;
752
753 /*
754 * Fixed hardware devices do not appear in the namespace and do not
755 * have handles, but we fabricate acpi_devices for them, so we have
756 * to deal with them specially.
757 */
758 if (handle == NULL)
759 return acpi_root;
760
761 do {
762 status = acpi_get_parent(handle, &handle);
763 if (status == AE_NULL_ENTRY)
764 return NULL;
765 if (ACPI_FAILURE(status))
766 return acpi_root;
767
768 ret = acpi_bus_get_device(handle, &device);
769 if (ret == 0)
770 return device;
771 } while (1);
772}
773
Len Brownc8f7a622006-07-09 17:22:28 -0400774acpi_status
775acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
776{
777 acpi_status status;
778 acpi_handle tmp;
779 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
780 union acpi_object *obj;
781
782 status = acpi_get_handle(handle, "_EJD", &tmp);
783 if (ACPI_FAILURE(status))
784 return status;
785
786 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
787 if (ACPI_SUCCESS(status)) {
788 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100789 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
790 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400791 kfree(buffer.pointer);
792 }
793 return status;
794}
795EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
796
Bob Moore8e4319c2009-06-29 13:43:27 +0800797void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799
800 /* TBD */
801
802 return;
803}
804
Zhang Rui9e89dde2006-12-07 20:56:16 +0800805static int acpi_bus_get_perf_flags(struct acpi_device *device)
806{
807 device->performance.state = ACPI_STATE_UNKNOWN;
808 return 0;
809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100812acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
813 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100815 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
816 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100818 acpi_status status;
819 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100821 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return AE_BAD_PARAMETER;
823
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100824 /* _PRW */
825 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
826 if (ACPI_FAILURE(status)) {
827 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
828 return status;
829 }
830
831 package = (union acpi_object *)buffer.pointer;
832
833 if (!package || (package->package.count < 2)) {
834 status = AE_BAD_DATA;
835 goto out;
836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100839 if (!element) {
840 status = AE_BAD_DATA;
841 goto out;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (element->type == ACPI_TYPE_PACKAGE) {
844 if ((element->package.count < 2) ||
845 (element->package.elements[0].type !=
846 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100847 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
848 status = AE_BAD_DATA;
849 goto out;
850 }
851 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100853 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 (u32) element->package.elements[1].integer.value;
855 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100856 wakeup->gpe_device = NULL;
857 wakeup->gpe_number = element->integer.value;
858 } else {
859 status = AE_BAD_DATA;
860 goto out;
861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 element = &(package->package.elements[1]);
864 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100865 status = AE_BAD_DATA;
866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100868 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100871 status = AE_NO_MEMORY;
872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100874 wakeup->resources.count = package->package.count - 2;
875 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100877 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
878 status = AE_BAD_DATA;
879 goto out;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100882 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
Lin Mingbba63a22010-12-13 13:39:17 +0800885 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200886
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100887 out:
888 kfree(buffer.pointer);
889
890 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100893static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200895 struct acpi_device_id button_device_ids[] = {
896 {"PNP0C0D", 0},
897 {"PNP0C0C", 0},
898 {"PNP0C0E", 0},
899 {"", 0},
900 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100901 acpi_status status;
902 acpi_event_status event_status;
903
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100904 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100905
906 /* Power button, Lid switch always enable wakeup */
907 if (!acpi_match_device_ids(device, button_device_ids)) {
908 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100909 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100910 return;
911 }
912
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200913 status = acpi_get_gpe_status(device->wakeup.gpe_device,
914 device->wakeup.gpe_number,
915 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100916 if (status == AE_OK)
917 device->wakeup.flags.run_wake =
918 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
919}
920
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100921static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100922{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100923 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100924 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100925 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200926
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100927 /* Presence of _PRW indicates wake capable */
928 status = acpi_get_handle(device->handle, "_PRW", &temp);
929 if (ACPI_FAILURE(status))
930 return;
931
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100932 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
933 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (ACPI_FAILURE(status)) {
935 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100936 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200940 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100941 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800942 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
943 * system for the ACPI device with the _PRW object.
944 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
945 * So it is necessary to call _DSW object first. Only when it is not
946 * present will the _PSW object used.
947 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200948 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
949 if (psw_error)
950 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
951 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100954static void acpi_bus_add_power_resource(acpi_handle handle);
955
Zhang Rui9e89dde2006-12-07 20:56:16 +0800956static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800958 acpi_status status = 0;
959 acpi_handle handle = NULL;
960 u32 i = 0;
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800964 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800966 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800968 device->power.flags.explicit_get = 1;
969 status = acpi_get_handle(device->handle, "_IRC", &handle);
970 if (ACPI_SUCCESS(status))
971 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800974 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800976 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800977 struct acpi_device_power_state *ps = &device->power.states[i];
978 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Zhang Rui9e89dde2006-12-07 20:56:16 +0800980 /* Evaluate "_PRx" to se if power resources are referenced */
981 acpi_evaluate_reference(device->handle, object_name, NULL,
982 &ps->resources);
983 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100984 int j;
985
Zhang Rui9e89dde2006-12-07 20:56:16 +0800986 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100987 for (j = 0; j < ps->resources.count; j++)
988 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Zhang Rui9e89dde2006-12-07 20:56:16 +0800991 /* Evaluate "_PSx" to see if we can do explicit sets */
992 object_name[2] = 'S';
993 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +0800994 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800995 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800996
Lin Ming1cc0c992012-04-23 09:03:49 +0800997 /*
998 * State is valid if there are means to put the device into it.
999 * D3hot is only valid if _PR3 present.
1000 */
1001 if (ps->resources.count ||
1002 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001003 ps->flags.valid = 1;
1004
1005 ps->power = -1; /* Unknown - driver assigned */
1006 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Zhang Rui9e89dde2006-12-07 20:56:16 +08001009 /* Set defaults for D0 and D3 states (always valid) */
1010 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1011 device->power.states[ACPI_STATE_D0].power = 100;
1012 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1013 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001015 /* Set D3cold's explicit_set flag if _PS3 exists. */
1016 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1017 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1018
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001019 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 return 0;
1022}
1023
Len Brown4be44fc2005-08-05 00:44:28 -04001024static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Len Brown4be44fc2005-08-05 00:44:28 -04001026 acpi_status status = AE_OK;
1027 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* Presence of _STA indicates 'dynamic_status' */
1031 status = acpi_get_handle(device->handle, "_STA", &temp);
1032 if (ACPI_SUCCESS(status))
1033 device->flags.dynamic_status = 1;
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* Presence of _RMV indicates 'removable' */
1036 status = acpi_get_handle(device->handle, "_RMV", &temp);
1037 if (ACPI_SUCCESS(status))
1038 device->flags.removable = 1;
1039
1040 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1041 status = acpi_get_handle(device->handle, "_EJD", &temp);
1042 if (ACPI_SUCCESS(status))
1043 device->flags.ejectable = 1;
1044 else {
1045 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1046 if (ACPI_SUCCESS(status))
1047 device->flags.ejectable = 1;
1048 }
1049
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001050 /* Power resources cannot be power manageable. */
1051 if (device->device_type == ACPI_BUS_TYPE_POWER)
1052 return 0;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1055 status = acpi_get_handle(device->handle, "_PS0", &temp);
1056 if (ACPI_FAILURE(status))
1057 status = acpi_get_handle(device->handle, "_PR0", &temp);
1058 if (ACPI_SUCCESS(status))
1059 device->flags.power_manageable = 1;
1060
Joe Perches3c5f9be42008-02-03 17:06:17 +02001061 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Patrick Mocheld550d982006-06-27 00:41:40 -04001063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001066static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Len Brown4be44fc2005-08-05 00:44:28 -04001068 char bus_id[5] = { '?', 0 };
1069 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1070 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
1073 * Bus ID
1074 * ------
1075 * The device's Bus ID is simply the object name.
1076 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1077 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001078 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001080 return;
1081 }
1082
1083 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 case ACPI_BUS_TYPE_POWER_BUTTON:
1085 strcpy(device->pnp.bus_id, "PWRF");
1086 break;
1087 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1088 strcpy(device->pnp.bus_id, "SLPF");
1089 break;
1090 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001091 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /* Clean up trailing underscores (if any) */
1093 for (i = 3; i > 1; i--) {
1094 if (bus_id[i] == '_')
1095 bus_id[i] = '\0';
1096 else
1097 break;
1098 }
1099 strcpy(device->pnp.bus_id, bus_id);
1100 break;
1101 }
1102}
1103
Zhang Rui54735262007-01-11 02:09:09 -05001104/*
1105 * acpi_bay_match - see if a device is an ejectable driver bay
1106 *
1107 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1108 * then we can safely call it an ejectable drive bay
1109 */
1110static int acpi_bay_match(struct acpi_device *device){
1111 acpi_status status;
1112 acpi_handle handle;
1113 acpi_handle tmp;
1114 acpi_handle phandle;
1115
1116 handle = device->handle;
1117
1118 status = acpi_get_handle(handle, "_EJ0", &tmp);
1119 if (ACPI_FAILURE(status))
1120 return -ENODEV;
1121
1122 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1123 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1124 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1125 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1126 return 0;
1127
1128 if (acpi_get_parent(handle, &phandle))
1129 return -ENODEV;
1130
1131 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1132 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1133 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1134 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1135 return 0;
1136
1137 return -ENODEV;
1138}
1139
Frank Seidel3620f2f2007-12-07 13:20:34 +01001140/*
1141 * acpi_dock_match - see if a device has a _DCK method
1142 */
1143static int acpi_dock_match(struct acpi_device *device)
1144{
1145 acpi_handle tmp;
1146 return acpi_get_handle(device->handle, "_DCK", &tmp);
1147}
1148
Thomas Renninger620e1122010-10-01 10:54:00 +02001149const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001150{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001151 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001152
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001153 if (list_empty(&device->pnp.ids))
1154 return dummy_hid;
1155
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001156 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1157 return hid->id;
1158}
1159EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001160
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001161static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1162{
1163 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001164
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001165 id = kmalloc(sizeof(*id), GFP_KERNEL);
1166 if (!id)
1167 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001168
Thomas Meyer581de592011-08-06 11:32:56 +02001169 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001170 if (!id->id) {
1171 kfree(id);
1172 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001173 }
1174
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001175 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001176}
1177
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001178/*
1179 * Old IBM workstations have a DSDT bug wherein the SMBus object
1180 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1181 * prefix. Work around this.
1182 */
1183static int acpi_ibm_smbus_match(struct acpi_device *device)
1184{
1185 acpi_handle h_dummy;
1186 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1187 int result;
1188
1189 if (!dmi_name_in_vendors("IBM"))
1190 return -ENODEV;
1191
1192 /* Look for SMBS object */
1193 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1194 if (result)
1195 return result;
1196
1197 if (strcmp("SMBS", path.pointer)) {
1198 result = -ENODEV;
1199 goto out;
1200 }
1201
1202 /* Does it have the necessary (but misnamed) methods? */
1203 result = -ENODEV;
1204 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1205 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1206 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1207 result = 0;
1208out:
1209 kfree(path.pointer);
1210 return result;
1211}
1212
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001213static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Len Brown4be44fc2005-08-05 00:44:28 -04001215 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001216 struct acpi_device_info *info;
1217 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001218 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001220 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001222 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001223 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001224 break;
1225 }
1226
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001227 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001229 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return;
1231 }
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001234 acpi_add_id(device, info->hardware_id.string);
1235 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001236 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001237 for (i = 0; i < cid_list->count; i++)
1238 acpi_add_id(device, cid_list->ids[i].string);
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (info->valid & ACPI_VALID_ADR) {
1241 device->pnp.bus_address = info->address;
1242 device->flags.bus_address = 1;
1243 }
Lv Zhengccf78042012-10-30 14:41:07 +01001244 if (info->valid & ACPI_VALID_UID)
1245 device->pnp.unique_id = kstrdup(info->unique_id.string,
1246 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001247
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001248 kfree(info);
1249
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001250 /*
1251 * Some devices don't reliably have _HIDs & _CIDs, so add
1252 * synthetic HIDs to make sure drivers can find them.
1253 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001254 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001255 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001256 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001257 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001258 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001259 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001260 else if (!acpi_ibm_smbus_match(device))
1261 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001262 else if (!acpi_device_hid(device) &&
1263 ACPI_IS_ROOT_DEVICE(device->parent)) {
1264 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1265 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1266 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1267 }
Zhang Rui54735262007-01-11 02:09:09 -05001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 break;
1270 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001271 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
1273 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001274 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001277 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001280 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001283 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 break;
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001288static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001290 acpi_status status;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /*
1293 * Context
1294 * -------
1295 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001296 * resolutions from handle->device very efficient. Fixed hardware
1297 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001299 if (!device->handle)
1300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001302 status = acpi_attach_data(device->handle,
1303 acpi_bus_data_handler, device);
1304 if (ACPI_SUCCESS(status))
1305 return 0;
1306
1307 printk(KERN_ERR PREFIX "Error attaching device data\n");
1308 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Len Brown4be44fc2005-08-05 00:44:28 -04001311static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001314 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Li Shaohua96333572006-12-07 20:56:46 +08001316 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001317 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Rui Zhang2786f6e2006-12-21 02:21:13 -05001322 /*
1323 * unbind _ADR-Based Devices when hot removal
1324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (dev->flags.bus_address) {
1326 if ((dev->parent) && (dev->parent->ops.unbind))
1327 dev->parent->ops.unbind(dev);
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1330
Patrick Mocheld550d982006-06-27 00:41:40 -04001331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001334static int acpi_add_single_object(struct acpi_device **child,
1335 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001336 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001337 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001339 int result;
1340 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001341 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Burman Yan36bcbec2006-12-19 12:56:11 -08001343 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001345 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001349 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001350 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001352 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001353 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001354 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001355
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001356 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /*
1359 * Flags
1360 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001361 * Note that we only look for object handles -- cannot evaluate objects
1362 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
1364 result = acpi_bus_get_flags(device);
1365 if (result)
1366 goto end;
1367
1368 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * Initialize Device
1370 * -----------------
1371 * TBD: Synch with Core's enumeration/initialization process.
1372 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001373 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /*
1376 * Power Management
1377 * ----------------
1378 */
1379 if (device->flags.power_manageable) {
1380 result = acpi_bus_get_power_flags(device);
1381 if (result)
1382 goto end;
1383 }
1384
Len Brown4be44fc2005-08-05 00:44:28 -04001385 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * Wakeup device management
1387 *-----------------------
1388 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001389 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /*
1392 * Performance Management
1393 * ----------------------
1394 */
1395 if (device->flags.performance_manageable) {
1396 result = acpi_bus_get_perf_flags(device);
1397 if (result)
1398 goto end;
1399 }
1400
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001401 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001402 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001404 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001407 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 */
1409 if (device->flags.bus_address) {
1410 if (device->parent && device->parent->ops.bind)
1411 device->parent->ops.bind(device);
1412 }
1413
Alex Chiang0c526d92009-05-14 08:31:37 -06001414end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001415 if (!result) {
1416 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1417 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1418 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1419 (char *) buffer.pointer,
1420 device->parent ? dev_name(&device->parent->dev) :
1421 "(null)"));
1422 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001424 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001425 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Patrick Mocheld550d982006-06-27 00:41:40 -04001427 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001430#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1431 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1432
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001433static void acpi_bus_add_power_resource(acpi_handle handle)
1434{
1435 struct acpi_bus_ops ops = {
1436 .acpi_op_add = 1,
1437 .acpi_op_start = 1,
1438 };
1439 struct acpi_device *device = NULL;
1440
1441 acpi_bus_get_device(handle, &device);
1442 if (!device)
1443 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1444 ACPI_STA_DEFAULT, &ops);
1445}
1446
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001447static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1448 unsigned long long *sta)
1449{
1450 acpi_status status;
1451 acpi_object_type acpi_type;
1452
1453 status = acpi_get_type(handle, &acpi_type);
1454 if (ACPI_FAILURE(status))
1455 return -ENODEV;
1456
1457 switch (acpi_type) {
1458 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1459 case ACPI_TYPE_DEVICE:
1460 *type = ACPI_BUS_TYPE_DEVICE;
1461 status = acpi_bus_get_status_handle(handle, sta);
1462 if (ACPI_FAILURE(status))
1463 return -ENODEV;
1464 break;
1465 case ACPI_TYPE_PROCESSOR:
1466 *type = ACPI_BUS_TYPE_PROCESSOR;
1467 status = acpi_bus_get_status_handle(handle, sta);
1468 if (ACPI_FAILURE(status))
1469 return -ENODEV;
1470 break;
1471 case ACPI_TYPE_THERMAL:
1472 *type = ACPI_BUS_TYPE_THERMAL;
1473 *sta = ACPI_STA_DEFAULT;
1474 break;
1475 case ACPI_TYPE_POWER:
1476 *type = ACPI_BUS_TYPE_POWER;
1477 *sta = ACPI_STA_DEFAULT;
1478 break;
1479 default:
1480 return -ENODEV;
1481 }
1482
1483 return 0;
1484}
1485
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001486static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1487 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001489 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001490 int type;
1491 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001492 struct acpi_device *device;
1493 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001494 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001495
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001496 result = acpi_bus_type_and_status(handle, &type, &sta);
1497 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001498 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001500 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001501 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001502 struct acpi_device_wakeup wakeup;
1503 acpi_handle temp;
1504
1505 status = acpi_get_handle(handle, "_PRW", &temp);
1506 if (ACPI_SUCCESS(status))
1507 acpi_bus_extract_wakeup_device_power_package(handle,
1508 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001509 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001512 /*
1513 * We may already have an acpi_device from a previous enumeration. If
1514 * so, we needn't add it again, but we may still have to start it.
1515 */
1516 device = NULL;
1517 acpi_bus_get_device(handle, &device);
1518 if (ops->acpi_op_add && !device)
1519 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001520
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001521 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001522 return AE_CTRL_DEPTH;
1523
1524 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1525 status = acpi_start_single_object(device);
1526 if (ACPI_FAILURE(status))
1527 return AE_CTRL_DEPTH;
1528 }
1529
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001530 if (!*return_value)
1531 *return_value = device;
1532 return AE_OK;
1533}
1534
1535static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1536 struct acpi_device **child)
1537{
1538 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001539 void *device = NULL;
1540
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001541 status = acpi_bus_check_add(handle, 0, ops, &device);
1542 if (ACPI_SUCCESS(status))
1543 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001544 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001545
1546 if (child)
1547 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001548
1549 if (device)
1550 return 0;
1551 else
1552 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Thomas Renninger77796882010-01-29 17:48:52 +01001555/*
1556 * acpi_bus_add and acpi_bus_start
1557 *
1558 * scan a given ACPI tree and (probably recently hot-plugged)
1559 * create and add or starts found devices.
1560 *
1561 * If no devices were found -ENODEV is returned which does not
1562 * mean that this is a real error, there just have been no suitable
1563 * ACPI objects in the table trunk from which the kernel could create
1564 * a device and add/start an appropriate driver.
1565 */
1566
Rajesh Shah3fb02732005-04-28 00:25:52 -07001567int
Len Brown4be44fc2005-08-05 00:44:28 -04001568acpi_bus_add(struct acpi_device **child,
1569 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001570{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001571 struct acpi_bus_ops ops;
1572
Li Shaohuac4168bf2006-12-07 20:56:41 +08001573 memset(&ops, 0, sizeof(ops));
1574 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001575
Thomas Renninger77796882010-01-29 17:48:52 +01001576 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001577}
1578EXPORT_SYMBOL(acpi_bus_add);
1579
Len Brown4be44fc2005-08-05 00:44:28 -04001580int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001581{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001582 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001583 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001584
Thomas Renningerd2f66502010-01-29 17:48:51 +01001585 if (!device)
1586 return -EINVAL;
1587
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001588 memset(&ops, 0, sizeof(ops));
1589 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001590
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001591 result = acpi_bus_scan(device->handle, &ops, NULL);
1592
Lin Ming3a378982010-12-13 13:36:15 +08001593 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001594
1595 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001596}
1597EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Kristen Accardiceaba662006-02-23 17:56:01 -08001599int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Len Brown4be44fc2005-08-05 00:44:28 -04001601 acpi_status status;
1602 struct acpi_device *parent, *child;
1603 acpi_handle phandle, chandle;
1604 acpi_object_type type;
1605 u32 level = 1;
1606 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Len Brown4be44fc2005-08-05 00:44:28 -04001608 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 phandle = start->handle;
1610 child = chandle = NULL;
1611
1612 while ((level > 0) && parent && (!err)) {
1613 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001614 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /*
1617 * If this scope is exhausted then move our way back up.
1618 */
1619 if (ACPI_FAILURE(status)) {
1620 level--;
1621 chandle = phandle;
1622 acpi_get_parent(phandle, &phandle);
1623 child = parent;
1624 parent = parent->parent;
1625
1626 if (level == 0)
1627 err = acpi_bus_remove(child, rmdevice);
1628 else
1629 err = acpi_bus_remove(child, 1);
1630
1631 continue;
1632 }
1633
1634 status = acpi_get_type(chandle, &type);
1635 if (ACPI_FAILURE(status)) {
1636 continue;
1637 }
1638 /*
1639 * If there is a device corresponding to chandle then
1640 * parse it (depth-first).
1641 */
1642 if (acpi_bus_get_device(chandle, &child) == 0) {
1643 level++;
1644 phandle = chandle;
1645 chandle = NULL;
1646 parent = child;
1647 }
1648 continue;
1649 }
1650 return err;
1651}
Kristen Accardiceaba662006-02-23 17:56:01 -08001652EXPORT_SYMBOL_GPL(acpi_bus_trim);
1653
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001654static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Len Brown4be44fc2005-08-05 00:44:28 -04001656 int result = 0;
1657 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001658 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Li Shaohuac4168bf2006-12-07 20:56:41 +08001660 memset(&ops, 0, sizeof(ops));
1661 ops.acpi_op_add = 1;
1662 ops.acpi_op_start = 1;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /*
1665 * Enumerate all fixed-feature devices.
1666 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001667 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001668 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001669 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001670 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001671 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001672 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001675 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001676 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001677 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001678 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001679 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Patrick Mocheld550d982006-06-27 00:41:40 -04001682 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Bjorn Helgaase747f272009-03-24 16:49:43 -06001685int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001688 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Li Shaohuac4168bf2006-12-07 20:56:41 +08001690 memset(&ops, 0, sizeof(ops));
1691 ops.acpi_op_add = 1;
1692 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Patrick Mochel5b327262006-05-10 10:33:00 -04001694 result = bus_register(&acpi_bus_type);
1695 if (result) {
1696 /* We don't want to quit even if we failed to add suspend/resume */
1697 printk(KERN_ERR PREFIX "Could not register bus type\n");
1698 }
1699
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001700 acpi_power_init();
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * Enumerate devices in the ACPI namespace.
1704 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001705 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001706
Li Shaohuac4168bf2006-12-07 20:56:41 +08001707 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001708 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 if (result)
1711 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001712 else
Lin Ming3a378982010-12-13 13:36:15 +08001713 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Patrick Mocheld550d982006-06-27 00:41:40 -04001715 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}