blob: bd523bfbaad115de3ffd0bedb28ea5bc0c608868 [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
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100286static ssize_t
287acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
288 char *buf) {
289 struct acpi_device *acpi_dev = to_acpi_device(dev);
290
291 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
292}
293static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
294
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800295static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600297 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800298 acpi_status status;
299 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100300 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800301 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800303 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800304 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800305 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600306 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800307 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600308 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800309 goto end;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200312 if (!list_empty(&dev->pnp.ids)) {
313 result = device_create_file(&dev->dev, &dev_attr_hid);
314 if (result)
315 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200317 result = device_create_file(&dev->dev, &dev_attr_modalias);
318 if (result)
319 goto end;
320 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200321
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600322 /*
323 * If device has _STR, 'description' file is created
324 */
325 status = acpi_get_handle(dev->handle, "_STR", &temp);
326 if (ACPI_SUCCESS(status)) {
327 status = acpi_evaluate_object(dev->handle, "_STR",
328 NULL, &buffer);
329 if (ACPI_FAILURE(status))
330 buffer.pointer = NULL;
331 dev->pnp.str_obj = buffer.pointer;
332 result = device_create_file(&dev->dev, &dev_attr_description);
333 if (result)
334 goto end;
335 }
336
Lv Zheng923d4a42012-10-30 14:43:26 +0100337 if (dev->flags.bus_address)
338 result = device_create_file(&dev->dev, &dev_attr_adr);
339 if (dev->pnp.unique_id)
340 result = device_create_file(&dev->dev, &dev_attr_uid);
341
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100342 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
343 if (ACPI_SUCCESS(status)) {
344 dev->pnp.sun = (unsigned long)sun;
345 result = device_create_file(&dev->dev, &dev_attr_sun);
346 if (result)
347 goto end;
348 } else {
349 dev->pnp.sun = (unsigned long)-1;
350 }
351
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800352 /*
353 * If device has _EJ0, 'eject' file is created that is used to trigger
354 * hot-removal function from userland.
355 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800356 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
357 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800358 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600359end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800360 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800361}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800363static void acpi_device_remove_files(struct acpi_device *dev)
364{
365 acpi_status status;
366 acpi_handle temp;
367
368 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600369 * If device has _STR, remove 'description' file
370 */
371 status = acpi_get_handle(dev->handle, "_STR", &temp);
372 if (ACPI_SUCCESS(status)) {
373 kfree(dev->pnp.str_obj);
374 device_remove_file(&dev->dev, &dev_attr_description);
375 }
376 /*
377 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800378 */
379 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
380 if (ACPI_SUCCESS(status))
381 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800382
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100383 status = acpi_get_handle(dev->handle, "_SUN", &temp);
384 if (ACPI_SUCCESS(status))
385 device_remove_file(&dev->dev, &dev_attr_sun);
386
Lv Zheng923d4a42012-10-30 14:43:26 +0100387 if (dev->pnp.unique_id)
388 device_remove_file(&dev->dev, &dev_attr_uid);
389 if (dev->flags.bus_address)
390 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600391 device_remove_file(&dev->dev, &dev_attr_modalias);
392 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600393 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800394 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800395}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800397 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200399
400int acpi_match_device_ids(struct acpi_device *device,
401 const struct acpi_device_id *ids)
402{
403 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600404 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200405
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800406 /*
407 * If the device is not present, it is unnecessary to load device
408 * driver for it.
409 */
410 if (!device->status.present)
411 return -ENODEV;
412
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600413 for (id = ids; id->id[0]; id++)
414 list_for_each_entry(hwid, &device->pnp.ids, list)
415 if (!strcmp((char *) id->id, hwid->id))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200416 return 0;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200417
418 return -ENOENT;
419}
420EXPORT_SYMBOL(acpi_match_device_ids);
421
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600422static void acpi_free_ids(struct acpi_device *device)
423{
424 struct acpi_hardware_id *id, *tmp;
425
426 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
427 kfree(id->id);
428 kfree(id);
429 }
430}
431
Patrick Mochel1890a972006-12-07 20:56:31 +0800432static void acpi_device_release(struct device *dev)
433{
434 struct acpi_device *acpi_dev = to_acpi_device(dev);
435
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600436 acpi_free_ids(acpi_dev);
Lv Zhengccf78042012-10-30 14:41:07 +0100437 kfree(acpi_dev->pnp.unique_id);
Patrick Mochel1890a972006-12-07 20:56:31 +0800438 kfree(acpi_dev);
439}
440
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800441static int acpi_bus_match(struct device *dev, struct device_driver *drv)
442{
443 struct acpi_device *acpi_dev = to_acpi_device(dev);
444 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
445
Thomas Renninger29b71a12007-07-23 14:43:51 +0200446 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800447}
448
Kay Sievers7eff2e72007-08-14 15:15:12 +0200449static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800450{
451 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200452 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800453
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200454 if (list_empty(&acpi_dev->pnp.ids))
455 return 0;
456
Kay Sievers7eff2e72007-08-14 15:15:12 +0200457 if (add_uevent_var(env, "MODALIAS="))
458 return -ENOMEM;
459 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
460 sizeof(env->buf) - env->buflen);
461 if (len >= (sizeof(env->buf) - env->buflen))
462 return -ENOMEM;
463 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
465}
466
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000467static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
468{
469 struct acpi_device *device = data;
470
471 device->driver->ops.notify(device, event);
472}
473
474static acpi_status acpi_device_notify_fixed(void *data)
475{
476 struct acpi_device *device = data;
477
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000478 /* Fixed hardware devices have no handles */
479 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000480 return AE_OK;
481}
482
483static int acpi_device_install_notify_handler(struct acpi_device *device)
484{
485 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000486
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000487 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000488 status =
489 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
490 acpi_device_notify_fixed,
491 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000492 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000493 status =
494 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
495 acpi_device_notify_fixed,
496 device);
497 else
498 status = acpi_install_notify_handler(device->handle,
499 ACPI_DEVICE_NOTIFY,
500 acpi_device_notify,
501 device);
502
503 if (ACPI_FAILURE(status))
504 return -EINVAL;
505 return 0;
506}
507
508static void acpi_device_remove_notify_handler(struct acpi_device *device)
509{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000510 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000511 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
512 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000513 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000514 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
515 acpi_device_notify_fixed);
516 else
517 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
518 acpi_device_notify);
519}
520
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800521static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
522static int acpi_start_single_object(struct acpi_device *);
523static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800524{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800525 struct acpi_device *acpi_dev = to_acpi_device(dev);
526 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
527 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800529 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
530 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800531 if (acpi_dev->bus_ops.acpi_op_start)
532 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000533
534 if (acpi_drv->ops.notify) {
535 ret = acpi_device_install_notify_handler(acpi_dev);
536 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000537 if (acpi_drv->ops.remove)
538 acpi_drv->ops.remove(acpi_dev,
539 acpi_dev->removal_type);
540 return ret;
541 }
542 }
543
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800544 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
545 "Found driver [%s] for device [%s]\n",
546 acpi_drv->name, acpi_dev->pnp.bus_id));
547 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800548 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800549 return ret;
550}
551
552static int acpi_device_remove(struct device * dev)
553{
554 struct acpi_device *acpi_dev = to_acpi_device(dev);
555 struct acpi_driver *acpi_drv = acpi_dev->driver;
556
557 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000558 if (acpi_drv->ops.notify)
559 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800560 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800561 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800562 }
563 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700564 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800565
566 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800567 return 0;
568}
569
David Brownell55955aa2007-05-08 00:28:35 -0700570struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800571 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800572 .match = acpi_bus_match,
573 .probe = acpi_device_probe,
574 .remove = acpi_device_remove,
575 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576};
577
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000578static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800580 int result;
581 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
582 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /*
585 * Linkage
586 * -------
587 * Link this device to its parent and siblings.
588 */
589 INIT_LIST_HEAD(&device->children);
590 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800592 INIT_LIST_HEAD(&device->physical_node_list);
593 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800595 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
596 if (!new_bus_id) {
597 printk(KERN_ERR PREFIX "Memory allocation error\n");
598 return -ENOMEM;
599 }
600
Shaohua Li90905892009-04-07 10:24:29 +0800601 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800602 /*
603 * Find suitable bus_id and instance number in acpi_bus_id_list
604 * If failed, create one and link it into acpi_bus_id_list
605 */
606 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600607 if (!strcmp(acpi_device_bus_id->bus_id,
608 acpi_device_hid(device))) {
609 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800610 found = 1;
611 kfree(new_bus_id);
612 break;
613 }
614 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600615 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800616 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600617 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800618 acpi_device_bus_id->instance_no = 0;
619 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
620 }
Kay Sievers07944692008-10-30 01:18:59 +0100621 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 +0800622
Len Brown33b57152008-12-15 22:09:26 -0500623 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (device->wakeup.flags.valid)
627 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800628 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Patrick Mochel1890a972006-12-07 20:56:31 +0800630 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000631 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800632 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800633 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600634 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600635 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600636 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800637 goto end;
638 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800639
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800640 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600641 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100642 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
643 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800644
Li Shaohua96333572006-12-07 20:56:46 +0800645 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800646 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600647end:
Shaohua Li90905892009-04-07 10:24:29 +0800648 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500649 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800650 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800651 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800652 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800653 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656static void acpi_device_unregister(struct acpi_device *device, int type)
657{
Shaohua Li90905892009-04-07 10:24:29 +0800658 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500659 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800663 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800666
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800667 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800668 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Zhang Rui9e89dde2006-12-07 20:56:16 +0800671/* --------------------------------------------------------------------------
672 Driver Management
673 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500675 * acpi_bus_driver_init - add a device to a driver
676 * @device: the device to add and initialize
677 * @driver: driver for the device
678 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600679 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800680 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
682static int
Len Brown4be44fc2005-08-05 00:44:28 -0400683acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Len Brown4be44fc2005-08-05 00:44:28 -0400685 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400688 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400691 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 result = driver->ops.add(device);
694 if (result) {
695 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700696 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400697 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
700 device->driver = driver;
701
702 /*
703 * TBD - Configuration Management: Assign resources to device based
704 * upon possible configuration and currently allocated resources.
705 */
706
Len Brown4be44fc2005-08-05 00:44:28 -0400707 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
708 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400709 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700710}
711
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400712static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700713{
714 int result = 0;
715 struct acpi_driver *driver;
716
Rajesh Shah3fb02732005-04-28 00:25:52 -0700717
718 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400719 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (driver->ops.start) {
722 result = driver->ops.start(device);
723 if (result && driver->ops.remove)
724 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Patrick Mocheld550d982006-06-27 00:41:40 -0400727 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500731 * acpi_bus_register_driver - register a driver with the ACPI bus
732 * @driver: driver being registered
733 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500735 * devices that match the driver's criteria and binds. Returns zero for
736 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 */
Len Brown4be44fc2005-08-05 00:44:28 -0400738int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Patrick Mochel1890a972006-12-07 20:56:31 +0800740 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400743 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800744 driver->drv.name = driver->name;
745 driver->drv.bus = &acpi_bus_type;
746 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Patrick Mochel1890a972006-12-07 20:56:31 +0800748 ret = driver_register(&driver->drv);
749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Len Brown4be44fc2005-08-05 00:44:28 -0400752EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500755 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
756 * @driver: driver to unregister
757 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * Unregisters a driver with the ACPI bus. Searches the namespace for all
759 * devices that match the driver's criteria and unbinds.
760 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400761void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Patrick Mochel1890a972006-12-07 20:56:31 +0800763 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
Len Brown4be44fc2005-08-05 00:44:28 -0400765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766EXPORT_SYMBOL(acpi_bus_unregister_driver);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/* --------------------------------------------------------------------------
769 Device Enumeration
770 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000771static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
772{
773 acpi_status status;
774 int ret;
775 struct acpi_device *device;
776
777 /*
778 * Fixed hardware devices do not appear in the namespace and do not
779 * have handles, but we fabricate acpi_devices for them, so we have
780 * to deal with them specially.
781 */
782 if (handle == NULL)
783 return acpi_root;
784
785 do {
786 status = acpi_get_parent(handle, &handle);
787 if (status == AE_NULL_ENTRY)
788 return NULL;
789 if (ACPI_FAILURE(status))
790 return acpi_root;
791
792 ret = acpi_bus_get_device(handle, &device);
793 if (ret == 0)
794 return device;
795 } while (1);
796}
797
Len Brownc8f7a622006-07-09 17:22:28 -0400798acpi_status
799acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
800{
801 acpi_status status;
802 acpi_handle tmp;
803 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
804 union acpi_object *obj;
805
806 status = acpi_get_handle(handle, "_EJD", &tmp);
807 if (ACPI_FAILURE(status))
808 return status;
809
810 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
811 if (ACPI_SUCCESS(status)) {
812 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100813 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
814 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400815 kfree(buffer.pointer);
816 }
817 return status;
818}
819EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
820
Bob Moore8e4319c2009-06-29 13:43:27 +0800821void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823
824 /* TBD */
825
826 return;
827}
828
Zhang Rui9e89dde2006-12-07 20:56:16 +0800829static int acpi_bus_get_perf_flags(struct acpi_device *device)
830{
831 device->performance.state = ACPI_STATE_UNKNOWN;
832 return 0;
833}
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100836acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
837 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100839 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
840 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100842 acpi_status status;
843 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100845 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return AE_BAD_PARAMETER;
847
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100848 /* _PRW */
849 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
850 if (ACPI_FAILURE(status)) {
851 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
852 return status;
853 }
854
855 package = (union acpi_object *)buffer.pointer;
856
857 if (!package || (package->package.count < 2)) {
858 status = AE_BAD_DATA;
859 goto out;
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100863 if (!element) {
864 status = AE_BAD_DATA;
865 goto out;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (element->type == ACPI_TYPE_PACKAGE) {
868 if ((element->package.count < 2) ||
869 (element->package.elements[0].type !=
870 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100871 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
872 status = AE_BAD_DATA;
873 goto out;
874 }
875 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100877 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 (u32) element->package.elements[1].integer.value;
879 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100880 wakeup->gpe_device = NULL;
881 wakeup->gpe_number = element->integer.value;
882 } else {
883 status = AE_BAD_DATA;
884 goto out;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 element = &(package->package.elements[1]);
888 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100889 status = AE_BAD_DATA;
890 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100892 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100895 status = AE_NO_MEMORY;
896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100898 wakeup->resources.count = package->package.count - 2;
899 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100901 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
902 status = AE_BAD_DATA;
903 goto out;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100906 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Lin Mingbba63a22010-12-13 13:39:17 +0800909 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200910
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100911 out:
912 kfree(buffer.pointer);
913
914 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100917static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200919 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +0200920 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +0100921 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +0200922 {"PNP0C0E", 0},
923 {"", 0},
924 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100925 acpi_status status;
926 acpi_event_status event_status;
927
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100928 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100929
930 /* Power button, Lid switch always enable wakeup */
931 if (!acpi_match_device_ids(device, button_device_ids)) {
932 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +0100933 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
934 /* Do not use Lid/sleep button for S5 wakeup */
935 if (device->wakeup.sleep_state == ACPI_STATE_S5)
936 device->wakeup.sleep_state = ACPI_STATE_S4;
937 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100938 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100939 return;
940 }
941
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200942 status = acpi_get_gpe_status(device->wakeup.gpe_device,
943 device->wakeup.gpe_number,
944 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100945 if (status == AE_OK)
946 device->wakeup.flags.run_wake =
947 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
948}
949
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100950static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100951{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100952 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100953 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100954 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200955
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100956 /* Presence of _PRW indicates wake capable */
957 status = acpi_get_handle(device->handle, "_PRW", &temp);
958 if (ACPI_FAILURE(status))
959 return;
960
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100961 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
962 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (ACPI_FAILURE(status)) {
964 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100965 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200969 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100970 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800971 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
972 * system for the ACPI device with the _PRW object.
973 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
974 * So it is necessary to call _DSW object first. Only when it is not
975 * present will the _PSW object used.
976 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200977 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
978 if (psw_error)
979 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
980 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100983static void acpi_bus_add_power_resource(acpi_handle handle);
984
Zhang Rui9e89dde2006-12-07 20:56:16 +0800985static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800987 acpi_status status = 0;
988 acpi_handle handle = NULL;
989 u32 i = 0;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800993 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800995 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800997 device->power.flags.explicit_get = 1;
998 status = acpi_get_handle(device->handle, "_IRC", &handle);
999 if (ACPI_SUCCESS(status))
1000 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001003 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
Lin Ming1cc0c992012-04-23 09:03:49 +08001005 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001006 struct acpi_device_power_state *ps = &device->power.states[i];
1007 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Zhang Rui9e89dde2006-12-07 20:56:16 +08001009 /* Evaluate "_PRx" to se if power resources are referenced */
1010 acpi_evaluate_reference(device->handle, object_name, NULL,
1011 &ps->resources);
1012 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001013 int j;
1014
Zhang Rui9e89dde2006-12-07 20:56:16 +08001015 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001016 for (j = 0; j < ps->resources.count; j++)
1017 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Zhang Rui9e89dde2006-12-07 20:56:16 +08001020 /* Evaluate "_PSx" to see if we can do explicit sets */
1021 object_name[2] = 'S';
1022 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +08001023 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001024 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001025
Lin Ming1cc0c992012-04-23 09:03:49 +08001026 /*
1027 * State is valid if there are means to put the device into it.
1028 * D3hot is only valid if _PR3 present.
1029 */
1030 if (ps->resources.count ||
1031 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001032 ps->flags.valid = 1;
1033
1034 ps->power = -1; /* Unknown - driver assigned */
1035 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Zhang Rui9e89dde2006-12-07 20:56:16 +08001038 /* Set defaults for D0 and D3 states (always valid) */
1039 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1040 device->power.states[ACPI_STATE_D0].power = 100;
1041 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1042 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001044 /* Set D3cold's explicit_set flag if _PS3 exists. */
1045 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1046 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1047
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001048 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 return 0;
1051}
1052
Len Brown4be44fc2005-08-05 00:44:28 -04001053static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Len Brown4be44fc2005-08-05 00:44:28 -04001055 acpi_status status = AE_OK;
1056 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /* Presence of _STA indicates 'dynamic_status' */
1060 status = acpi_get_handle(device->handle, "_STA", &temp);
1061 if (ACPI_SUCCESS(status))
1062 device->flags.dynamic_status = 1;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* Presence of _RMV indicates 'removable' */
1065 status = acpi_get_handle(device->handle, "_RMV", &temp);
1066 if (ACPI_SUCCESS(status))
1067 device->flags.removable = 1;
1068
1069 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1070 status = acpi_get_handle(device->handle, "_EJD", &temp);
1071 if (ACPI_SUCCESS(status))
1072 device->flags.ejectable = 1;
1073 else {
1074 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1075 if (ACPI_SUCCESS(status))
1076 device->flags.ejectable = 1;
1077 }
1078
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001079 /* Power resources cannot be power manageable. */
1080 if (device->device_type == ACPI_BUS_TYPE_POWER)
1081 return 0;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1084 status = acpi_get_handle(device->handle, "_PS0", &temp);
1085 if (ACPI_FAILURE(status))
1086 status = acpi_get_handle(device->handle, "_PR0", &temp);
1087 if (ACPI_SUCCESS(status))
1088 device->flags.power_manageable = 1;
1089
Joe Perches3c5f9be42008-02-03 17:06:17 +02001090 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Patrick Mocheld550d982006-06-27 00:41:40 -04001092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001095static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Len Brown4be44fc2005-08-05 00:44:28 -04001097 char bus_id[5] = { '?', 0 };
1098 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1099 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /*
1102 * Bus ID
1103 * ------
1104 * The device's Bus ID is simply the object name.
1105 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1106 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001107 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001109 return;
1110 }
1111
1112 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 case ACPI_BUS_TYPE_POWER_BUTTON:
1114 strcpy(device->pnp.bus_id, "PWRF");
1115 break;
1116 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1117 strcpy(device->pnp.bus_id, "SLPF");
1118 break;
1119 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001120 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* Clean up trailing underscores (if any) */
1122 for (i = 3; i > 1; i--) {
1123 if (bus_id[i] == '_')
1124 bus_id[i] = '\0';
1125 else
1126 break;
1127 }
1128 strcpy(device->pnp.bus_id, bus_id);
1129 break;
1130 }
1131}
1132
Zhang Rui54735262007-01-11 02:09:09 -05001133/*
1134 * acpi_bay_match - see if a device is an ejectable driver bay
1135 *
1136 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1137 * then we can safely call it an ejectable drive bay
1138 */
1139static int acpi_bay_match(struct acpi_device *device){
1140 acpi_status status;
1141 acpi_handle handle;
1142 acpi_handle tmp;
1143 acpi_handle phandle;
1144
1145 handle = device->handle;
1146
1147 status = acpi_get_handle(handle, "_EJ0", &tmp);
1148 if (ACPI_FAILURE(status))
1149 return -ENODEV;
1150
1151 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1152 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1153 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1154 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1155 return 0;
1156
1157 if (acpi_get_parent(handle, &phandle))
1158 return -ENODEV;
1159
1160 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1161 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1162 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1163 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1164 return 0;
1165
1166 return -ENODEV;
1167}
1168
Frank Seidel3620f2f2007-12-07 13:20:34 +01001169/*
1170 * acpi_dock_match - see if a device has a _DCK method
1171 */
1172static int acpi_dock_match(struct acpi_device *device)
1173{
1174 acpi_handle tmp;
1175 return acpi_get_handle(device->handle, "_DCK", &tmp);
1176}
1177
Thomas Renninger620e1122010-10-01 10:54:00 +02001178const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001179{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001180 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001181
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001182 if (list_empty(&device->pnp.ids))
1183 return dummy_hid;
1184
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001185 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1186 return hid->id;
1187}
1188EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001189
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001190static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1191{
1192 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001193
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001194 id = kmalloc(sizeof(*id), GFP_KERNEL);
1195 if (!id)
1196 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001197
Thomas Meyer581de592011-08-06 11:32:56 +02001198 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001199 if (!id->id) {
1200 kfree(id);
1201 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001202 }
1203
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001204 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001205}
1206
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001207/*
1208 * Old IBM workstations have a DSDT bug wherein the SMBus object
1209 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1210 * prefix. Work around this.
1211 */
1212static int acpi_ibm_smbus_match(struct acpi_device *device)
1213{
1214 acpi_handle h_dummy;
1215 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1216 int result;
1217
1218 if (!dmi_name_in_vendors("IBM"))
1219 return -ENODEV;
1220
1221 /* Look for SMBS object */
1222 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1223 if (result)
1224 return result;
1225
1226 if (strcmp("SMBS", path.pointer)) {
1227 result = -ENODEV;
1228 goto out;
1229 }
1230
1231 /* Does it have the necessary (but misnamed) methods? */
1232 result = -ENODEV;
1233 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1234 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1235 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1236 result = 0;
1237out:
1238 kfree(path.pointer);
1239 return result;
1240}
1241
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001242static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Len Brown4be44fc2005-08-05 00:44:28 -04001244 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001245 struct acpi_device_info *info;
1246 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001247 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001249 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001251 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001252 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001253 break;
1254 }
1255
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001256 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001258 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return;
1260 }
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001263 acpi_add_id(device, info->hardware_id.string);
1264 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001265 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001266 for (i = 0; i < cid_list->count; i++)
1267 acpi_add_id(device, cid_list->ids[i].string);
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (info->valid & ACPI_VALID_ADR) {
1270 device->pnp.bus_address = info->address;
1271 device->flags.bus_address = 1;
1272 }
Lv Zhengccf78042012-10-30 14:41:07 +01001273 if (info->valid & ACPI_VALID_UID)
1274 device->pnp.unique_id = kstrdup(info->unique_id.string,
1275 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001276
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001277 kfree(info);
1278
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001279 /*
1280 * Some devices don't reliably have _HIDs & _CIDs, so add
1281 * synthetic HIDs to make sure drivers can find them.
1282 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001283 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001284 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001285 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001286 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001287 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001288 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001289 else if (!acpi_ibm_smbus_match(device))
1290 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001291 else if (!acpi_device_hid(device) &&
1292 ACPI_IS_ROOT_DEVICE(device->parent)) {
1293 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1294 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1295 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1296 }
Zhang Rui54735262007-01-11 02:09:09 -05001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
1299 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001300 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 break;
1302 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001303 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001306 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
1308 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001309 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001312 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001317static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001319 acpi_status status;
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /*
1322 * Context
1323 * -------
1324 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001325 * resolutions from handle->device very efficient. Fixed hardware
1326 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001328 if (!device->handle)
1329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001331 status = acpi_attach_data(device->handle,
1332 acpi_bus_data_handler, device);
1333 if (ACPI_SUCCESS(status))
1334 return 0;
1335
1336 printk(KERN_ERR PREFIX "Error attaching device data\n");
1337 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Len Brown4be44fc2005-08-05 00:44:28 -04001340static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001343 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Li Shaohua96333572006-12-07 20:56:46 +08001345 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001346 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Rui Zhang2786f6e2006-12-21 02:21:13 -05001351 /*
1352 * unbind _ADR-Based Devices when hot removal
1353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (dev->flags.bus_address) {
1355 if ((dev->parent) && (dev->parent->ops.unbind))
1356 dev->parent->ops.unbind(dev);
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1359
Patrick Mocheld550d982006-06-27 00:41:40 -04001360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001363static int acpi_add_single_object(struct acpi_device **child,
1364 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001365 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001366 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001368 int result;
1369 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001370 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Burman Yan36bcbec2006-12-19 12:56:11 -08001372 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001374 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001375 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001378 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001379 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001381 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001382 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001383 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001384
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001385 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /*
1388 * Flags
1389 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001390 * Note that we only look for object handles -- cannot evaluate objects
1391 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
1393 result = acpi_bus_get_flags(device);
1394 if (result)
1395 goto end;
1396
1397 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 * Initialize Device
1399 * -----------------
1400 * TBD: Synch with Core's enumeration/initialization process.
1401 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001402 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
1405 * Power Management
1406 * ----------------
1407 */
1408 if (device->flags.power_manageable) {
1409 result = acpi_bus_get_power_flags(device);
1410 if (result)
1411 goto end;
1412 }
1413
Len Brown4be44fc2005-08-05 00:44:28 -04001414 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * Wakeup device management
1416 *-----------------------
1417 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001418 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /*
1421 * Performance Management
1422 * ----------------------
1423 */
1424 if (device->flags.performance_manageable) {
1425 result = acpi_bus_get_perf_flags(device);
1426 if (result)
1427 goto end;
1428 }
1429
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001430 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001431 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001433 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001436 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 */
1438 if (device->flags.bus_address) {
1439 if (device->parent && device->parent->ops.bind)
1440 device->parent->ops.bind(device);
1441 }
1442
Alex Chiang0c526d92009-05-14 08:31:37 -06001443end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001444 if (!result) {
1445 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1446 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1447 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1448 (char *) buffer.pointer,
1449 device->parent ? dev_name(&device->parent->dev) :
1450 "(null)"));
1451 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001453 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001454 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Patrick Mocheld550d982006-06-27 00:41:40 -04001456 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001459#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1460 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1461
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001462static void acpi_bus_add_power_resource(acpi_handle handle)
1463{
1464 struct acpi_bus_ops ops = {
1465 .acpi_op_add = 1,
1466 .acpi_op_start = 1,
1467 };
1468 struct acpi_device *device = NULL;
1469
1470 acpi_bus_get_device(handle, &device);
1471 if (!device)
1472 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1473 ACPI_STA_DEFAULT, &ops);
1474}
1475
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001476static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1477 unsigned long long *sta)
1478{
1479 acpi_status status;
1480 acpi_object_type acpi_type;
1481
1482 status = acpi_get_type(handle, &acpi_type);
1483 if (ACPI_FAILURE(status))
1484 return -ENODEV;
1485
1486 switch (acpi_type) {
1487 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1488 case ACPI_TYPE_DEVICE:
1489 *type = ACPI_BUS_TYPE_DEVICE;
1490 status = acpi_bus_get_status_handle(handle, sta);
1491 if (ACPI_FAILURE(status))
1492 return -ENODEV;
1493 break;
1494 case ACPI_TYPE_PROCESSOR:
1495 *type = ACPI_BUS_TYPE_PROCESSOR;
1496 status = acpi_bus_get_status_handle(handle, sta);
1497 if (ACPI_FAILURE(status))
1498 return -ENODEV;
1499 break;
1500 case ACPI_TYPE_THERMAL:
1501 *type = ACPI_BUS_TYPE_THERMAL;
1502 *sta = ACPI_STA_DEFAULT;
1503 break;
1504 case ACPI_TYPE_POWER:
1505 *type = ACPI_BUS_TYPE_POWER;
1506 *sta = ACPI_STA_DEFAULT;
1507 break;
1508 default:
1509 return -ENODEV;
1510 }
1511
1512 return 0;
1513}
1514
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001515static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1516 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001518 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001519 int type;
1520 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001521 struct acpi_device *device;
1522 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001523 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001524
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001525 result = acpi_bus_type_and_status(handle, &type, &sta);
1526 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001527 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001529 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001530 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001531 struct acpi_device_wakeup wakeup;
1532 acpi_handle temp;
1533
1534 status = acpi_get_handle(handle, "_PRW", &temp);
1535 if (ACPI_SUCCESS(status))
1536 acpi_bus_extract_wakeup_device_power_package(handle,
1537 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001538 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001541 /*
1542 * We may already have an acpi_device from a previous enumeration. If
1543 * so, we needn't add it again, but we may still have to start it.
1544 */
1545 device = NULL;
1546 acpi_bus_get_device(handle, &device);
1547 if (ops->acpi_op_add && !device)
1548 acpi_add_single_object(&device, handle, type, sta, ops);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001549
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001550 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001551 return AE_CTRL_DEPTH;
1552
1553 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1554 status = acpi_start_single_object(device);
1555 if (ACPI_FAILURE(status))
1556 return AE_CTRL_DEPTH;
1557 }
1558
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001559 if (!*return_value)
1560 *return_value = device;
1561 return AE_OK;
1562}
1563
1564static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1565 struct acpi_device **child)
1566{
1567 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001568 void *device = NULL;
1569
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001570 status = acpi_bus_check_add(handle, 0, ops, &device);
1571 if (ACPI_SUCCESS(status))
1572 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001573 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001574
1575 if (child)
1576 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001577
1578 if (device)
1579 return 0;
1580 else
1581 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Thomas Renninger77796882010-01-29 17:48:52 +01001584/*
1585 * acpi_bus_add and acpi_bus_start
1586 *
1587 * scan a given ACPI tree and (probably recently hot-plugged)
1588 * create and add or starts found devices.
1589 *
1590 * If no devices were found -ENODEV is returned which does not
1591 * mean that this is a real error, there just have been no suitable
1592 * ACPI objects in the table trunk from which the kernel could create
1593 * a device and add/start an appropriate driver.
1594 */
1595
Rajesh Shah3fb02732005-04-28 00:25:52 -07001596int
Len Brown4be44fc2005-08-05 00:44:28 -04001597acpi_bus_add(struct acpi_device **child,
1598 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001599{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001600 struct acpi_bus_ops ops;
1601
Li Shaohuac4168bf2006-12-07 20:56:41 +08001602 memset(&ops, 0, sizeof(ops));
1603 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001604
Thomas Renninger77796882010-01-29 17:48:52 +01001605 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001606}
1607EXPORT_SYMBOL(acpi_bus_add);
1608
Len Brown4be44fc2005-08-05 00:44:28 -04001609int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001610{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001611 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001612 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001613
Thomas Renningerd2f66502010-01-29 17:48:51 +01001614 if (!device)
1615 return -EINVAL;
1616
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001617 memset(&ops, 0, sizeof(ops));
1618 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001619
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001620 result = acpi_bus_scan(device->handle, &ops, NULL);
1621
Lin Ming3a378982010-12-13 13:36:15 +08001622 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001623
1624 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001625}
1626EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Kristen Accardiceaba662006-02-23 17:56:01 -08001628int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Len Brown4be44fc2005-08-05 00:44:28 -04001630 acpi_status status;
1631 struct acpi_device *parent, *child;
1632 acpi_handle phandle, chandle;
1633 acpi_object_type type;
1634 u32 level = 1;
1635 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Len Brown4be44fc2005-08-05 00:44:28 -04001637 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 phandle = start->handle;
1639 child = chandle = NULL;
1640
1641 while ((level > 0) && parent && (!err)) {
1642 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001643 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /*
1646 * If this scope is exhausted then move our way back up.
1647 */
1648 if (ACPI_FAILURE(status)) {
1649 level--;
1650 chandle = phandle;
1651 acpi_get_parent(phandle, &phandle);
1652 child = parent;
1653 parent = parent->parent;
1654
1655 if (level == 0)
1656 err = acpi_bus_remove(child, rmdevice);
1657 else
1658 err = acpi_bus_remove(child, 1);
1659
1660 continue;
1661 }
1662
1663 status = acpi_get_type(chandle, &type);
1664 if (ACPI_FAILURE(status)) {
1665 continue;
1666 }
1667 /*
1668 * If there is a device corresponding to chandle then
1669 * parse it (depth-first).
1670 */
1671 if (acpi_bus_get_device(chandle, &child) == 0) {
1672 level++;
1673 phandle = chandle;
1674 chandle = NULL;
1675 parent = child;
1676 }
1677 continue;
1678 }
1679 return err;
1680}
Kristen Accardiceaba662006-02-23 17:56:01 -08001681EXPORT_SYMBOL_GPL(acpi_bus_trim);
1682
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001683static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Len Brown4be44fc2005-08-05 00:44:28 -04001685 int result = 0;
1686 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001687 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Li Shaohuac4168bf2006-12-07 20:56:41 +08001689 memset(&ops, 0, sizeof(ops));
1690 ops.acpi_op_add = 1;
1691 ops.acpi_op_start = 1;
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /*
1694 * Enumerate all fixed-feature devices.
1695 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001696 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001697 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001698 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001699 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001700 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001701 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001704 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001705 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001706 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001707 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001708 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Patrick Mocheld550d982006-06-27 00:41:40 -04001711 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Bjorn Helgaase747f272009-03-24 16:49:43 -06001714int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001717 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Li Shaohuac4168bf2006-12-07 20:56:41 +08001719 memset(&ops, 0, sizeof(ops));
1720 ops.acpi_op_add = 1;
1721 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Patrick Mochel5b327262006-05-10 10:33:00 -04001723 result = bus_register(&acpi_bus_type);
1724 if (result) {
1725 /* We don't want to quit even if we failed to add suspend/resume */
1726 printk(KERN_ERR PREFIX "Could not register bus type\n");
1727 }
1728
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001729 acpi_power_init();
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 * Enumerate devices in the ACPI namespace.
1733 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001734 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001735
Li Shaohuac4168bf2006-12-07 20:56:41 +08001736 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001737 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (result)
1740 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001741 else
Lin Ming3a378982010-12-13 13:36:15 +08001742 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Patrick Mocheld550d982006-06-27 00:41:40 -04001744 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}