blob: c7676ee8eca4dbb84f21d923b934797b76ef76b3 [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);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010034static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010035static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080036DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037LIST_HEAD(acpi_wakeup_device_list);
38
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080040 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080041 unsigned int instance_no;
42 struct list_head node;
43};
Thomas Renninger29b71a12007-07-23 14:43:51 +020044
Rafael J. Wysockica589f92013-01-30 14:27:29 +010045int acpi_scan_add_handler(struct acpi_scan_handler *handler)
46{
47 if (!handler || !handler->attach)
48 return -EINVAL;
49
50 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
51 return 0;
52}
53
Thomas Renninger29b71a12007-07-23 14:43:51 +020054/*
55 * Creates hid/cid(s) string needed for modalias and uevent
56 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
57 * char *modalias: "acpi:IBM0001:ACPI0001"
58*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020059static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
60 int size)
61{
Thomas Renninger29b71a12007-07-23 14:43:51 +020062 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080063 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060064 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020065
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020066 if (list_empty(&acpi_dev->pnp.ids))
67 return 0;
68
Zhang Rui5c9fcb52008-03-20 16:40:32 +080069 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020070 size -= len;
71
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060072 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
73 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080074 if (count < 0 || count >= size)
75 return -EINVAL;
76 len += count;
77 size -= count;
78 }
79
Thomas Renninger29b71a12007-07-23 14:43:51 +020080 modalias[len] = '\0';
81 return len;
82}
83
84static ssize_t
85acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
86 struct acpi_device *acpi_dev = to_acpi_device(dev);
87 int len;
88
89 /* Device has no HID and no CID or string is >1024 */
90 len = create_modalias(acpi_dev, buf, 1024);
91 if (len <= 0)
92 return 0;
93 buf[len++] = '\n';
94 return len;
95}
96static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
97
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +010098static void __acpi_bus_trim(struct acpi_device *start);
99
Toshi Kanic4753e52012-05-23 20:25:20 -0600100/**
101 * acpi_bus_hot_remove_device: hot-remove a device and its children
102 * @context: struct acpi_eject_event pointer (freed in this func)
103 *
104 * Hot-remove a device and its children. This function frees up the
105 * memory space passed by arg context, so that the caller may call
106 * this function asynchronously through acpi_os_hotplug_execute().
107 */
108void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Toshi Kanic4753e52012-05-23 20:25:20 -0600110 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
Yinghai Lu5993c462013-01-11 22:40:41 +0000111 struct acpi_device *device = ej_event->device;
112 acpi_handle handle = device->handle;
Toshi Kanib3c450c2012-10-26 13:38:57 +0200113 acpi_handle temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct acpi_object_list arg_list;
115 union acpi_object arg;
116 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600117 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100119 mutex_lock(&acpi_scan_lock);
120
Zhang Rui26d46862008-04-29 02:35:48 -0400121 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100122 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400123
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100124 __acpi_bus_trim(device);
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100125 /* Device node has been released. */
Toshi Kanib3c450c2012-10-26 13:38:57 +0200126 device = NULL;
127
Toshi Kanib3c450c2012-10-26 13:38:57 +0200128 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 arg_list.count = 1;
130 arg_list.pointer = &arg;
131 arg.type = ACPI_TYPE_INTEGER;
132 arg.integer.value = 0;
133 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
134 }
135
136 arg_list.count = 1;
137 arg_list.pointer = &arg;
138 arg.type = ACPI_TYPE_INTEGER;
139 arg.integer.value = 1;
140
141 /*
142 * TBD: _EJD support.
143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600145 if (ACPI_FAILURE(status)) {
146 if (status != AE_NOT_FOUND)
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100147 acpi_handle_warn(handle, "Eject failed\n");
148
149 /* Tell the firmware the hot-remove operation has failed. */
150 acpi_evaluate_hotplug_ost(handle, ej_event->event,
151 ost_code, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100154 mutex_unlock(&acpi_scan_lock);
Toshi Kanic4753e52012-05-23 20:25:20 -0600155 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800156 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
Toshi Kani61622ac2012-11-01 14:42:12 +0000158EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100160static ssize_t real_power_state_show(struct device *dev,
161 struct device_attribute *attr, char *buf)
162{
163 struct acpi_device *adev = to_acpi_device(dev);
164 int state;
165 int ret;
166
167 ret = acpi_device_get_power(adev, &state);
168 if (ret)
169 return ret;
170
171 return sprintf(buf, "%s\n", acpi_power_state_string(state));
172}
173
174static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
175
176static ssize_t power_state_show(struct device *dev,
177 struct device_attribute *attr, char *buf)
178{
179 struct acpi_device *adev = to_acpi_device(dev);
180
181 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
182}
183
184static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800187acpi_eject_store(struct device *d, struct device_attribute *attr,
188 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Len Brown4be44fc2005-08-05 00:44:28 -0400190 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400191 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400192 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800193 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600194 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if ((!count) || (buf[0] != '1')) {
197 return -EINVAL;
198 }
Toshi Kanice7685a2013-02-07 12:50:53 +0100199 if (!acpi_device->driver && !acpi_device->handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 ret = -ENODEV;
201 goto err;
202 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800203 status = acpi_get_type(acpi_device->handle, &type);
204 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 ret = -ENODEV;
206 goto err;
207 }
208
Toshi Kanic4753e52012-05-23 20:25:20 -0600209 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
210 if (!ej_event) {
211 ret = -ENOMEM;
212 goto err;
213 }
214
Yinghai Lu5993c462013-01-11 22:40:41 +0000215 ej_event->device = acpi_device;
Toshi Kanic4753e52012-05-23 20:25:20 -0600216 if (acpi_device->flags.eject_pending) {
217 /* event originated from ACPI eject notification */
218 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
219 acpi_device->flags.eject_pending = 0;
220 } else {
221 /* event originated from user */
222 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
Yinghai Lu5993c462013-01-11 22:40:41 +0000223 (void) acpi_evaluate_hotplug_ost(acpi_device->handle,
Toshi Kanic4753e52012-05-23 20:25:20 -0600224 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
225 }
226
227 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400228err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800232static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800234static ssize_t
235acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
236 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600238 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800239}
240static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
241
Lv Zheng923d4a42012-10-30 14:43:26 +0100242static ssize_t acpi_device_uid_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
244{
245 struct acpi_device *acpi_dev = to_acpi_device(dev);
246
247 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
248}
249static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
250
251static ssize_t acpi_device_adr_show(struct device *dev,
252 struct device_attribute *attr, char *buf)
253{
254 struct acpi_device *acpi_dev = to_acpi_device(dev);
255
256 return sprintf(buf, "0x%08x\n",
257 (unsigned int)(acpi_dev->pnp.bus_address));
258}
259static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
260
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800261static ssize_t
262acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
263 struct acpi_device *acpi_dev = to_acpi_device(dev);
264 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
265 int result;
266
267 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600268 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800269 goto end;
270
271 result = sprintf(buf, "%s\n", (char*)path.pointer);
272 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600273end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800274 return result;
275}
276static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
277
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600278/* sysfs file that shows description text from the ACPI _STR method */
279static ssize_t description_show(struct device *dev,
280 struct device_attribute *attr,
281 char *buf) {
282 struct acpi_device *acpi_dev = to_acpi_device(dev);
283 int result;
284
285 if (acpi_dev->pnp.str_obj == NULL)
286 return 0;
287
288 /*
289 * The _STR object contains a Unicode identifier for a device.
290 * We need to convert to utf-8 so it can be displayed.
291 */
292 result = utf16s_to_utf8s(
293 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
294 acpi_dev->pnp.str_obj->buffer.length,
295 UTF16_LITTLE_ENDIAN, buf,
296 PAGE_SIZE);
297
298 buf[result++] = '\n';
299
300 return result;
301}
302static DEVICE_ATTR(description, 0444, description_show, NULL);
303
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100304static ssize_t
305acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
306 char *buf) {
307 struct acpi_device *acpi_dev = to_acpi_device(dev);
308
309 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
310}
311static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
312
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800313static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600315 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800316 acpi_status status;
317 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100318 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800319 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800321 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800322 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800323 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600324 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800325 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600326 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800327 goto end;
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200330 if (!list_empty(&dev->pnp.ids)) {
331 result = device_create_file(&dev->dev, &dev_attr_hid);
332 if (result)
333 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200335 result = device_create_file(&dev->dev, &dev_attr_modalias);
336 if (result)
337 goto end;
338 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200339
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600340 /*
341 * If device has _STR, 'description' file is created
342 */
343 status = acpi_get_handle(dev->handle, "_STR", &temp);
344 if (ACPI_SUCCESS(status)) {
345 status = acpi_evaluate_object(dev->handle, "_STR",
346 NULL, &buffer);
347 if (ACPI_FAILURE(status))
348 buffer.pointer = NULL;
349 dev->pnp.str_obj = buffer.pointer;
350 result = device_create_file(&dev->dev, &dev_attr_description);
351 if (result)
352 goto end;
353 }
354
Lv Zheng923d4a42012-10-30 14:43:26 +0100355 if (dev->flags.bus_address)
356 result = device_create_file(&dev->dev, &dev_attr_adr);
357 if (dev->pnp.unique_id)
358 result = device_create_file(&dev->dev, &dev_attr_uid);
359
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100360 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
361 if (ACPI_SUCCESS(status)) {
362 dev->pnp.sun = (unsigned long)sun;
363 result = device_create_file(&dev->dev, &dev_attr_sun);
364 if (result)
365 goto end;
366 } else {
367 dev->pnp.sun = (unsigned long)-1;
368 }
369
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800370 /*
371 * If device has _EJ0, 'eject' file is created that is used to trigger
372 * hot-removal function from userland.
373 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800374 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100375 if (ACPI_SUCCESS(status)) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800376 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100377 if (result)
378 return result;
379 }
380
381 if (dev->flags.power_manageable) {
382 result = device_create_file(&dev->dev, &dev_attr_power_state);
383 if (result)
384 return result;
385
386 if (dev->power.flags.power_resources)
387 result = device_create_file(&dev->dev,
388 &dev_attr_real_power_state);
389 }
390
Alex Chiang0c526d92009-05-14 08:31:37 -0600391end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800392 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800393}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800395static void acpi_device_remove_files(struct acpi_device *dev)
396{
397 acpi_status status;
398 acpi_handle temp;
399
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100400 if (dev->flags.power_manageable) {
401 device_remove_file(&dev->dev, &dev_attr_power_state);
402 if (dev->power.flags.power_resources)
403 device_remove_file(&dev->dev,
404 &dev_attr_real_power_state);
405 }
406
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800407 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600408 * If device has _STR, remove 'description' file
409 */
410 status = acpi_get_handle(dev->handle, "_STR", &temp);
411 if (ACPI_SUCCESS(status)) {
412 kfree(dev->pnp.str_obj);
413 device_remove_file(&dev->dev, &dev_attr_description);
414 }
415 /*
416 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800417 */
418 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
419 if (ACPI_SUCCESS(status))
420 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800421
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100422 status = acpi_get_handle(dev->handle, "_SUN", &temp);
423 if (ACPI_SUCCESS(status))
424 device_remove_file(&dev->dev, &dev_attr_sun);
425
Lv Zheng923d4a42012-10-30 14:43:26 +0100426 if (dev->pnp.unique_id)
427 device_remove_file(&dev->dev, &dev_attr_uid);
428 if (dev->flags.bus_address)
429 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600430 device_remove_file(&dev->dev, &dev_attr_modalias);
431 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600432 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800433 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800434}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800436 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200438
Mika Westerbergcf761af2012-10-31 22:44:41 +0100439static const struct acpi_device_id *__acpi_match_device(
440 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200441{
442 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600443 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200444
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800445 /*
446 * If the device is not present, it is unnecessary to load device
447 * driver for it.
448 */
449 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100450 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800451
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600452 for (id = ids; id->id[0]; id++)
453 list_for_each_entry(hwid, &device->pnp.ids, list)
454 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100455 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200456
Mika Westerbergcf761af2012-10-31 22:44:41 +0100457 return NULL;
458}
459
460/**
461 * acpi_match_device - Match a struct device against a given list of ACPI IDs
462 * @ids: Array of struct acpi_device_id object to match against.
463 * @dev: The device structure to match.
464 *
465 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
466 * object for that handle and use that object to match against a given list of
467 * device IDs.
468 *
469 * Return a pointer to the first matching ID on success or %NULL on failure.
470 */
471const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
472 const struct device *dev)
473{
474 struct acpi_device *adev;
475
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100476 if (!ids || !ACPI_HANDLE(dev)
477 || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100478 return NULL;
479
480 return __acpi_match_device(adev, ids);
481}
482EXPORT_SYMBOL_GPL(acpi_match_device);
483
484int acpi_match_device_ids(struct acpi_device *device,
485 const struct acpi_device_id *ids)
486{
487 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200488}
489EXPORT_SYMBOL(acpi_match_device_ids);
490
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100491void acpi_free_ids(struct acpi_device *device)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600492{
493 struct acpi_hardware_id *id, *tmp;
494
495 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
496 kfree(id->id);
497 kfree(id);
498 }
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100499 kfree(device->pnp.unique_id);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600500}
501
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100502static void acpi_free_power_resources_lists(struct acpi_device *device)
503{
504 int i;
505
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100506 if (device->wakeup.flags.valid)
507 acpi_power_resources_list_free(&device->wakeup.resources);
508
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100509 if (!device->flags.power_manageable)
510 return;
511
512 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
513 struct acpi_device_power_state *ps = &device->power.states[i];
514 acpi_power_resources_list_free(&ps->resources);
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Patrick Mochel1890a972006-12-07 20:56:31 +0800518static void acpi_device_release(struct device *dev)
519{
520 struct acpi_device *acpi_dev = to_acpi_device(dev);
521
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600522 acpi_free_ids(acpi_dev);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100523 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800524 kfree(acpi_dev);
525}
526
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800527static int acpi_bus_match(struct device *dev, struct device_driver *drv)
528{
529 struct acpi_device *acpi_dev = to_acpi_device(dev);
530 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
531
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100532 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100533 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800534}
535
Kay Sievers7eff2e72007-08-14 15:15:12 +0200536static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800537{
538 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200539 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800540
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200541 if (list_empty(&acpi_dev->pnp.ids))
542 return 0;
543
Kay Sievers7eff2e72007-08-14 15:15:12 +0200544 if (add_uevent_var(env, "MODALIAS="))
545 return -ENOMEM;
546 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
547 sizeof(env->buf) - env->buflen);
548 if (len >= (sizeof(env->buf) - env->buflen))
549 return -ENOMEM;
550 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000554static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
555{
556 struct acpi_device *device = data;
557
558 device->driver->ops.notify(device, event);
559}
560
561static acpi_status acpi_device_notify_fixed(void *data)
562{
563 struct acpi_device *device = data;
564
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000565 /* Fixed hardware devices have no handles */
566 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000567 return AE_OK;
568}
569
570static int acpi_device_install_notify_handler(struct acpi_device *device)
571{
572 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000573
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000574 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000575 status =
576 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
577 acpi_device_notify_fixed,
578 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000579 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000580 status =
581 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
582 acpi_device_notify_fixed,
583 device);
584 else
585 status = acpi_install_notify_handler(device->handle,
586 ACPI_DEVICE_NOTIFY,
587 acpi_device_notify,
588 device);
589
590 if (ACPI_FAILURE(status))
591 return -EINVAL;
592 return 0;
593}
594
595static void acpi_device_remove_notify_handler(struct acpi_device *device)
596{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000597 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000598 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
599 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000600 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000601 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
602 acpi_device_notify_fixed);
603 else
604 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
605 acpi_device_notify);
606}
607
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800608static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800609static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800610{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800611 struct acpi_device *acpi_dev = to_acpi_device(dev);
612 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
613 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800615 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
616 if (!ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000617 if (acpi_drv->ops.notify) {
618 ret = acpi_device_install_notify_handler(acpi_dev);
619 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000620 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100621 acpi_drv->ops.remove(acpi_dev);
Toshi Kani5f27ee82013-02-07 21:19:13 +0000622 acpi_dev->driver = NULL;
623 acpi_dev->driver_data = NULL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000624 return ret;
625 }
626 }
627
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800628 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
629 "Found driver [%s] for device [%s]\n",
630 acpi_drv->name, acpi_dev->pnp.bus_id));
631 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800632 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800633 return ret;
634}
635
636static int acpi_device_remove(struct device * dev)
637{
638 struct acpi_device *acpi_dev = to_acpi_device(dev);
639 struct acpi_driver *acpi_drv = acpi_dev->driver;
640
641 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000642 if (acpi_drv->ops.notify)
643 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800644 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100645 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800646 }
647 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700648 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800649
650 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800651 return 0;
652}
653
David Brownell55955aa2007-05-08 00:28:35 -0700654struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800655 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800656 .match = acpi_bus_match,
657 .probe = acpi_device_probe,
658 .remove = acpi_device_remove,
659 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660};
661
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100662int acpi_device_add(struct acpi_device *device,
663 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800665 int result;
666 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
667 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000668
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100669 if (device->handle) {
670 acpi_status status;
671
672 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
673 device);
674 if (ACPI_FAILURE(status)) {
675 acpi_handle_err(device->handle,
676 "Unable to attach device data\n");
677 return -ENODEV;
678 }
679 }
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
682 * Linkage
683 * -------
684 * Link this device to its parent and siblings.
685 */
686 INIT_LIST_HEAD(&device->children);
687 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800689 INIT_LIST_HEAD(&device->physical_node_list);
690 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100691 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800693 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
694 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100695 pr_err(PREFIX "Memory allocation error\n");
696 result = -ENOMEM;
697 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800698 }
699
Shaohua Li90905892009-04-07 10:24:29 +0800700 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800701 /*
702 * Find suitable bus_id and instance number in acpi_bus_id_list
703 * If failed, create one and link it into acpi_bus_id_list
704 */
705 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600706 if (!strcmp(acpi_device_bus_id->bus_id,
707 acpi_device_hid(device))) {
708 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800709 found = 1;
710 kfree(new_bus_id);
711 break;
712 }
713 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600714 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800715 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600716 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800717 acpi_device_bus_id->instance_no = 0;
718 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
719 }
Kay Sievers07944692008-10-30 01:18:59 +0100720 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 +0800721
Len Brown33b57152008-12-15 22:09:26 -0500722 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (device->wakeup.flags.valid)
726 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800727 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Patrick Mochel1890a972006-12-07 20:56:31 +0800729 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000730 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800731 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100732 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100733 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600734 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600735 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100736 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800737 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800738
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800739 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600740 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100741 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
742 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800743
Li Shaohua96333572006-12-07 20:56:46 +0800744 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800745 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100746
747 err:
Shaohua Li90905892009-04-07 10:24:29 +0800748 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500749 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800750 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800751 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800752 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100753
754 err_detach:
755 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800756 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Rafael J. Wysockib17b5372013-01-15 13:23:44 +0100759static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Shaohua Li90905892009-04-07 10:24:29 +0800761 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500762 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800766 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800769
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100770 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800771 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100772 if (device->remove)
773 device->remove(device);
774
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100775 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100776 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100777 * Transition the device to D3cold to drop the reference counts of all
778 * power resources the device depends on and turn off the ones that have
779 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100780 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100781 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100782 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Zhang Rui9e89dde2006-12-07 20:56:16 +0800785/* --------------------------------------------------------------------------
786 Driver Management
787 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500789 * acpi_bus_driver_init - add a device to a driver
790 * @device: the device to add and initialize
791 * @driver: driver for the device
792 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600793 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800794 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
796static int
Len Brown4be44fc2005-08-05 00:44:28 -0400797acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Len Brown4be44fc2005-08-05 00:44:28 -0400799 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400802 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400805 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 result = driver->ops.add(device);
808 if (result) {
809 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700810 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400811 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
814 device->driver = driver;
815
816 /*
817 * TBD - Configuration Management: Assign resources to device based
818 * upon possible configuration and currently allocated resources.
819 */
820
Len Brown4be44fc2005-08-05 00:44:28 -0400821 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
822 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400823 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500827 * acpi_bus_register_driver - register a driver with the ACPI bus
828 * @driver: driver being registered
829 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500831 * devices that match the driver's criteria and binds. Returns zero for
832 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Len Brown4be44fc2005-08-05 00:44:28 -0400834int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Patrick Mochel1890a972006-12-07 20:56:31 +0800836 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400839 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800840 driver->drv.name = driver->name;
841 driver->drv.bus = &acpi_bus_type;
842 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Patrick Mochel1890a972006-12-07 20:56:31 +0800844 ret = driver_register(&driver->drv);
845 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Len Brown4be44fc2005-08-05 00:44:28 -0400848EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500851 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
852 * @driver: driver to unregister
853 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * Unregisters a driver with the ACPI bus. Searches the namespace for all
855 * devices that match the driver's criteria and unbinds.
856 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400857void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Patrick Mochel1890a972006-12-07 20:56:31 +0800859 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
Len Brown4be44fc2005-08-05 00:44:28 -0400861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862EXPORT_SYMBOL(acpi_bus_unregister_driver);
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/* --------------------------------------------------------------------------
865 Device Enumeration
866 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000867static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
868{
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100869 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000870 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000871
872 /*
873 * Fixed hardware devices do not appear in the namespace and do not
874 * have handles, but we fabricate acpi_devices for them, so we have
875 * to deal with them specially.
876 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100877 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000878 return acpi_root;
879
880 do {
881 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000882 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100883 return status == AE_NULL_ENTRY ? NULL : acpi_root;
884 } while (acpi_bus_get_device(handle, &device));
885 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000886}
887
Len Brownc8f7a622006-07-09 17:22:28 -0400888acpi_status
889acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
890{
891 acpi_status status;
892 acpi_handle tmp;
893 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
894 union acpi_object *obj;
895
896 status = acpi_get_handle(handle, "_EJD", &tmp);
897 if (ACPI_FAILURE(status))
898 return status;
899
900 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
901 if (ACPI_SUCCESS(status)) {
902 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100903 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
904 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400905 kfree(buffer.pointer);
906 }
907 return status;
908}
909EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
910
Bob Moore8e4319c2009-06-29 13:43:27 +0800911void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913
914 /* TBD */
915
916 return;
917}
918
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100919static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
920 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100922 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
923 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100925 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100926 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100928 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100929 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100931 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100933 /* _PRW */
934 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
935 if (ACPI_FAILURE(status)) {
936 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100937 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100938 }
939
940 package = (union acpi_object *)buffer.pointer;
941
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100942 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100943 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100946 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100947 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (element->type == ACPI_TYPE_PACKAGE) {
950 if ((element->package.count < 2) ||
951 (element->package.elements[0].type !=
952 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100953 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100954 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100955
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100956 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100958 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 (u32) element->package.elements[1].integer.value;
960 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100961 wakeup->gpe_device = NULL;
962 wakeup->gpe_number = element->integer.value;
963 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100964 goto out;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100968 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100969 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100970
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100971 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100973 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
974 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Rafael J. Wysocki0596a522013-01-17 14:11:07 +0100977 if (!list_empty(&wakeup->resources)) {
978 int sleep_state;
979
980 sleep_state = acpi_power_min_system_level(&wakeup->resources);
981 if (sleep_state < wakeup->sleep_state) {
982 acpi_handle_warn(handle, "Overriding _PRW sleep state "
983 "(S%d) by S%d from power resources\n",
984 (int)wakeup->sleep_state, sleep_state);
985 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Lin Mingbba63a22010-12-13 13:39:17 +0800988 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200989
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100990 out:
991 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100992 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100995static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200997 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +0200998 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +0100999 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001000 {"PNP0C0E", 0},
1001 {"", 0},
1002 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001003 acpi_status status;
1004 acpi_event_status event_status;
1005
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001006 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001007
1008 /* Power button, Lid switch always enable wakeup */
1009 if (!acpi_match_device_ids(device, button_device_ids)) {
1010 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001011 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1012 /* Do not use Lid/sleep button for S5 wakeup */
1013 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1014 device->wakeup.sleep_state = ACPI_STATE_S4;
1015 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001016 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001017 return;
1018 }
1019
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001020 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1021 device->wakeup.gpe_number,
1022 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001023 if (status == AE_OK)
1024 device->wakeup.flags.run_wake =
1025 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1026}
1027
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001028static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001029{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001030 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001031 acpi_status status = 0;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001032 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001033
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001034 /* Presence of _PRW indicates wake capable */
1035 status = acpi_get_handle(device->handle, "_PRW", &temp);
1036 if (ACPI_FAILURE(status))
1037 return;
1038
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001039 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1040 &device->wakeup);
1041 if (err) {
1042 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001043 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001047 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001048 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001049 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1050 * system for the ACPI device with the _PRW object.
1051 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1052 * So it is necessary to call _DSW object first. Only when it is not
1053 * present will the _PSW object used.
1054 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001055 err = acpi_device_sleep_wake(device, 0, 0, 0);
1056 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001057 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1058 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001061static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001063 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001064 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1065 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001066 acpi_handle handle;
1067 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001068
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001069 INIT_LIST_HEAD(&ps->resources);
1070
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001071 /* Evaluate "_PRx" to get referenced power resources */
1072 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1073 if (ACPI_SUCCESS(status)) {
1074 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001075
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001076 if (buffer.length && package
1077 && package->type == ACPI_TYPE_PACKAGE
1078 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001079 int err = acpi_extract_power_resources(package, 0,
1080 &ps->resources);
1081 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001082 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001083 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001084 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001085 }
1086
1087 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001088 pathname[2] = 'S';
1089 status = acpi_get_handle(device->handle, pathname, &handle);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001090 if (ACPI_SUCCESS(status))
1091 ps->flags.explicit_set = 1;
1092
1093 /*
1094 * State is valid if there are means to put the device into it.
1095 * D3hot is only valid if _PR3 present.
1096 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001097 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001098 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1099 ps->flags.valid = 1;
1100 ps->flags.os_accessible = 1;
1101 }
1102
1103 ps->power = -1; /* Unknown - driver assigned */
1104 ps->latency = -1; /* Unknown - driver assigned */
1105}
1106
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001107static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001109 acpi_status status;
1110 acpi_handle handle;
1111 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001113 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1114 status = acpi_get_handle(device->handle, "_PS0", &handle);
1115 if (ACPI_FAILURE(status)) {
1116 status = acpi_get_handle(device->handle, "_PR0", &handle);
1117 if (ACPI_FAILURE(status))
1118 return;
1119 }
1120
1121 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001124 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Zhang Rui9e89dde2006-12-07 20:56:16 +08001126 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001128 device->power.flags.explicit_get = 1;
1129 status = acpi_get_handle(device->handle, "_IRC", &handle);
1130 if (ACPI_SUCCESS(status))
1131 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001134 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001136 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1137 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001139 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Zhang Rui9e89dde2006-12-07 20:56:16 +08001141 /* Set defaults for D0 and D3 states (always valid) */
1142 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1143 device->power.states[ACPI_STATE_D0].power = 100;
1144 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1145 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001147 /* Set D3cold's explicit_set flag if _PS3 exists. */
1148 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1149 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1150
Aaron Lu1399dfc2012-11-21 23:33:40 +01001151 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1152 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1153 device->power.flags.power_resources)
1154 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1155
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001156 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001159static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Len Brown4be44fc2005-08-05 00:44:28 -04001161 acpi_status status = AE_OK;
1162 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /* Presence of _STA indicates 'dynamic_status' */
1165 status = acpi_get_handle(device->handle, "_STA", &temp);
1166 if (ACPI_SUCCESS(status))
1167 device->flags.dynamic_status = 1;
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /* Presence of _RMV indicates 'removable' */
1170 status = acpi_get_handle(device->handle, "_RMV", &temp);
1171 if (ACPI_SUCCESS(status))
1172 device->flags.removable = 1;
1173
1174 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1175 status = acpi_get_handle(device->handle, "_EJD", &temp);
1176 if (ACPI_SUCCESS(status))
1177 device->flags.ejectable = 1;
1178 else {
1179 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1180 if (ACPI_SUCCESS(status))
1181 device->flags.ejectable = 1;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001185static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Len Brown4be44fc2005-08-05 00:44:28 -04001187 char bus_id[5] = { '?', 0 };
1188 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1189 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /*
1192 * Bus ID
1193 * ------
1194 * The device's Bus ID is simply the object name.
1195 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1196 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001197 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001199 return;
1200 }
1201
1202 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 case ACPI_BUS_TYPE_POWER_BUTTON:
1204 strcpy(device->pnp.bus_id, "PWRF");
1205 break;
1206 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1207 strcpy(device->pnp.bus_id, "SLPF");
1208 break;
1209 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001210 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* Clean up trailing underscores (if any) */
1212 for (i = 3; i > 1; i--) {
1213 if (bus_id[i] == '_')
1214 bus_id[i] = '\0';
1215 else
1216 break;
1217 }
1218 strcpy(device->pnp.bus_id, bus_id);
1219 break;
1220 }
1221}
1222
Zhang Rui54735262007-01-11 02:09:09 -05001223/*
1224 * acpi_bay_match - see if a device is an ejectable driver bay
1225 *
1226 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1227 * then we can safely call it an ejectable drive bay
1228 */
1229static int acpi_bay_match(struct acpi_device *device){
1230 acpi_status status;
1231 acpi_handle handle;
1232 acpi_handle tmp;
1233 acpi_handle phandle;
1234
1235 handle = device->handle;
1236
1237 status = acpi_get_handle(handle, "_EJ0", &tmp);
1238 if (ACPI_FAILURE(status))
1239 return -ENODEV;
1240
1241 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1242 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1243 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1244 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1245 return 0;
1246
1247 if (acpi_get_parent(handle, &phandle))
1248 return -ENODEV;
1249
1250 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1251 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1252 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1253 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1254 return 0;
1255
1256 return -ENODEV;
1257}
1258
Frank Seidel3620f2f2007-12-07 13:20:34 +01001259/*
1260 * acpi_dock_match - see if a device has a _DCK method
1261 */
1262static int acpi_dock_match(struct acpi_device *device)
1263{
1264 acpi_handle tmp;
1265 return acpi_get_handle(device->handle, "_DCK", &tmp);
1266}
1267
Thomas Renninger620e1122010-10-01 10:54:00 +02001268const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001269{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001270 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001271
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001272 if (list_empty(&device->pnp.ids))
1273 return dummy_hid;
1274
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001275 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1276 return hid->id;
1277}
1278EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001279
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001280static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1281{
1282 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001283
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001284 id = kmalloc(sizeof(*id), GFP_KERNEL);
1285 if (!id)
1286 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001287
Thomas Meyer581de592011-08-06 11:32:56 +02001288 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001289 if (!id->id) {
1290 kfree(id);
1291 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001292 }
1293
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001294 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001295}
1296
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001297/*
1298 * Old IBM workstations have a DSDT bug wherein the SMBus object
1299 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1300 * prefix. Work around this.
1301 */
1302static int acpi_ibm_smbus_match(struct acpi_device *device)
1303{
1304 acpi_handle h_dummy;
1305 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1306 int result;
1307
1308 if (!dmi_name_in_vendors("IBM"))
1309 return -ENODEV;
1310
1311 /* Look for SMBS object */
1312 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1313 if (result)
1314 return result;
1315
1316 if (strcmp("SMBS", path.pointer)) {
1317 result = -ENODEV;
1318 goto out;
1319 }
1320
1321 /* Does it have the necessary (but misnamed) methods? */
1322 result = -ENODEV;
1323 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1324 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1325 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1326 result = 0;
1327out:
1328 kfree(path.pointer);
1329 return result;
1330}
1331
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001332static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Len Brown4be44fc2005-08-05 00:44:28 -04001334 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001335 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001336 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001337 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001339 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001341 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001342 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001343 break;
1344 }
1345
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001346 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001348 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return;
1350 }
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001353 acpi_add_id(device, info->hardware_id.string);
1354 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001355 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001356 for (i = 0; i < cid_list->count; i++)
1357 acpi_add_id(device, cid_list->ids[i].string);
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (info->valid & ACPI_VALID_ADR) {
1360 device->pnp.bus_address = info->address;
1361 device->flags.bus_address = 1;
1362 }
Lv Zhengccf78042012-10-30 14:41:07 +01001363 if (info->valid & ACPI_VALID_UID)
1364 device->pnp.unique_id = kstrdup(info->unique_id.string,
1365 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001366
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001367 kfree(info);
1368
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001369 /*
1370 * Some devices don't reliably have _HIDs & _CIDs, so add
1371 * synthetic HIDs to make sure drivers can find them.
1372 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001373 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001374 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001375 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001376 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001377 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001378 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001379 else if (!acpi_ibm_smbus_match(device))
1380 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Rafael J. Wysocki4f5f64c2013-01-04 23:00:54 +01001381 else if (list_empty(&device->pnp.ids) &&
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001382 ACPI_IS_ROOT_DEVICE(device->parent)) {
1383 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1384 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1385 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1386 }
Zhang Rui54735262007-01-11 02:09:09 -05001387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001390 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001393 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001396 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 break;
1398 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001399 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 break;
1401 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001402 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 break;
1404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001407void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1408 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001410 INIT_LIST_HEAD(&device->pnp.ids);
1411 device->device_type = type;
1412 device->handle = handle;
1413 device->parent = acpi_bus_get_parent(handle);
1414 STRUCT_TO_INT(device->status) = sta;
1415 acpi_device_get_busid(device);
1416 acpi_device_set_id(device);
1417 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001418 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001419 device_initialize(&device->dev);
1420 dev_set_uevent_suppress(&device->dev, true);
1421}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001422
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001423void acpi_device_add_finalize(struct acpi_device *device)
1424{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001425 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001426 dev_set_uevent_suppress(&device->dev, false);
1427 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001430static int acpi_add_single_object(struct acpi_device **child,
1431 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001432 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001434 int result;
1435 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001436 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Burman Yan36bcbec2006-12-19 12:56:11 -08001438 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001440 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001441 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001444 acpi_init_device_object(device, handle, type, sta);
1445 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001446 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001448 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001449 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001450 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001451 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001454 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001455 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001456 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1457 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1458 dev_name(&device->dev), (char *) buffer.pointer,
1459 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1460 kfree(buffer.pointer);
1461 *child = device;
1462 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001463}
1464
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001465static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1466 unsigned long long *sta)
1467{
1468 acpi_status status;
1469 acpi_object_type acpi_type;
1470
1471 status = acpi_get_type(handle, &acpi_type);
1472 if (ACPI_FAILURE(status))
1473 return -ENODEV;
1474
1475 switch (acpi_type) {
1476 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1477 case ACPI_TYPE_DEVICE:
1478 *type = ACPI_BUS_TYPE_DEVICE;
1479 status = acpi_bus_get_status_handle(handle, sta);
1480 if (ACPI_FAILURE(status))
1481 return -ENODEV;
1482 break;
1483 case ACPI_TYPE_PROCESSOR:
1484 *type = ACPI_BUS_TYPE_PROCESSOR;
1485 status = acpi_bus_get_status_handle(handle, sta);
1486 if (ACPI_FAILURE(status))
1487 return -ENODEV;
1488 break;
1489 case ACPI_TYPE_THERMAL:
1490 *type = ACPI_BUS_TYPE_THERMAL;
1491 *sta = ACPI_STA_DEFAULT;
1492 break;
1493 case ACPI_TYPE_POWER:
1494 *type = ACPI_BUS_TYPE_POWER;
1495 *sta = ACPI_STA_DEFAULT;
1496 break;
1497 default:
1498 return -ENODEV;
1499 }
1500
1501 return 0;
1502}
1503
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001504static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1505 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001507 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001508 int type;
1509 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001510 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001511 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001512
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001513 acpi_bus_get_device(handle, &device);
1514 if (device)
1515 goto out;
1516
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001517 result = acpi_bus_type_and_status(handle, &type, &sta);
1518 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001519 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001521 if (type == ACPI_BUS_TYPE_POWER) {
1522 acpi_add_power_resource(handle);
1523 return AE_OK;
1524 }
1525
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001526 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001527 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001528 struct acpi_device_wakeup wakeup;
1529 acpi_handle temp;
1530
1531 status = acpi_get_handle(handle, "_PRW", &temp);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001532 if (ACPI_SUCCESS(status)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001533 acpi_bus_extract_wakeup_device_power_package(handle,
1534 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001535 acpi_power_resources_list_free(&wakeup.resources);
1536 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001537 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001540 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001541 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001542 return AE_CTRL_DEPTH;
1543
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001544 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001545 if (!*return_value)
1546 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001547
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001548 return AE_OK;
1549}
1550
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001551static int acpi_scan_do_attach_handler(struct acpi_device *device, char *id)
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001552{
1553 struct acpi_scan_handler *handler;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001554
1555 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) {
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001556 const struct acpi_device_id *devid;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001557
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001558 for (devid = handler->ids; devid->id[0]; devid++) {
1559 int ret;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001560
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001561 if (strcmp((char *)devid->id, id))
1562 continue;
1563
1564 ret = handler->attach(device, devid);
1565 if (ret > 0) {
1566 device->handler = handler;
1567 return ret;
1568 } else if (ret < 0) {
1569 return ret;
1570 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001571 }
1572 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001573 return 0;
1574}
1575
1576static int acpi_scan_attach_handler(struct acpi_device *device)
1577{
1578 struct acpi_hardware_id *hwid;
1579 int ret = 0;
1580
1581 list_for_each_entry(hwid, &device->pnp.ids, list) {
1582 ret = acpi_scan_do_attach_handler(device, hwid->id);
1583 if (ret)
1584 break;
1585
1586 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001587 return ret;
1588}
1589
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001590static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1591 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001592{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001593 struct acpi_device *device;
1594 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001595 int ret;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001596
1597 /*
1598 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1599 * namespace walks prematurely.
1600 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001601 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001602 return AE_OK;
1603
1604 if (acpi_bus_get_device(handle, &device))
1605 return AE_CTRL_DEPTH;
1606
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001607 ret = acpi_scan_attach_handler(device);
1608 if (ret)
1609 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1610
1611 ret = device_attach(&device->dev);
1612 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001613}
1614
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001615/**
1616 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1617 * @handle: Root of the namespace scope to scan.
1618 *
1619 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1620 * found devices.
1621 *
1622 * If no devices were found, -ENODEV is returned, but it does not mean that
1623 * there has been a real error. There just have been no suitable ACPI objects
1624 * in the table trunk from which the kernel could create a device and add an
1625 * appropriate driver.
1626 */
1627int acpi_bus_scan(acpi_handle handle)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001628{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001629 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001630 int error = 0;
1631
1632 mutex_lock(&acpi_scan_lock);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001633
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001634 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001635 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001636 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001637
Thomas Renningerd2f66502010-01-29 17:48:51 +01001638 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001639 error = -ENODEV;
1640 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001641 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001642 acpi_bus_device_attach, NULL, NULL, NULL);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001643
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001644 mutex_unlock(&acpi_scan_lock);
1645 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001646}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001647EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001649static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1650 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001652 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001654 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001655 struct acpi_scan_handler *dev_handler = device->handler;
1656
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001657 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001658 if (dev_handler) {
1659 if (dev_handler->detach)
1660 dev_handler->detach(device);
1661
1662 device->handler = NULL;
1663 } else {
1664 device_release_driver(&device->dev);
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001667 return AE_OK;
1668}
1669
1670static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1671 void *not_used, void **ret_not_used)
1672{
1673 struct acpi_device *device = NULL;
1674
1675 if (!acpi_bus_get_device(handle, &device))
1676 acpi_device_unregister(device);
1677
1678 return AE_OK;
1679}
1680
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +01001681static void __acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001683 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001684 * Execute acpi_bus_device_detach() as a post-order callback to detach
1685 * all ACPI drivers from the device nodes being removed.
1686 */
1687 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1688 acpi_bus_device_detach, NULL, NULL);
1689 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1690 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001691 * Execute acpi_bus_remove() as a post-order callback to remove device
1692 * nodes in the given namespace scope.
1693 */
1694 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1695 acpi_bus_remove, NULL, NULL);
1696 acpi_bus_remove(start->handle, 0, NULL, NULL);
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +01001697}
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001698
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +01001699void acpi_bus_trim(struct acpi_device *start)
1700{
1701 mutex_lock(&acpi_scan_lock);
1702 __acpi_bus_trim(start);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001703 mutex_unlock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
Kristen Accardiceaba662006-02-23 17:56:01 -08001705EXPORT_SYMBOL_GPL(acpi_bus_trim);
1706
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001707static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Len Brown4be44fc2005-08-05 00:44:28 -04001709 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 /*
1712 * Enumerate all fixed-feature devices.
1713 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001714 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1715 struct acpi_device *device = NULL;
1716
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001717 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001718 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001719 ACPI_STA_DEFAULT);
1720 if (result)
1721 return result;
1722
1723 result = device_attach(&device->dev);
1724 if (result < 0)
1725 return result;
1726
Daniel Drakec10d7a12012-05-10 00:08:43 +01001727 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001730 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1731 struct acpi_device *device = NULL;
1732
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001733 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001734 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001735 ACPI_STA_DEFAULT);
1736 if (result)
1737 return result;
1738
1739 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001742 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Bjorn Helgaase747f272009-03-24 16:49:43 -06001745int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Patrick Mochel5b327262006-05-10 10:33:00 -04001749 result = bus_register(&acpi_bus_type);
1750 if (result) {
1751 /* We don't want to quit even if we failed to add suspend/resume */
1752 printk(KERN_ERR PREFIX "Could not register bus type\n");
1753 }
1754
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01001755 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01001756 acpi_pci_link_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01001757 acpi_platform_init();
Mika Westerberg13176bb2013-01-17 09:59:33 +00001758 acpi_csrt_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01001759 acpi_container_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 * Enumerate devices in the ACPI namespace.
1763 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001764 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (result)
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001766 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001768 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (result)
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001770 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001772 result = acpi_bus_scan_fixed();
1773 if (result) {
1774 acpi_device_unregister(acpi_root);
1775 return result;
1776 }
1777
1778 acpi_update_all_gpes();
1779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}