]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/acpi/scan.c
688b7f7c23dda7e12548bb58f32369bac42029c9
[mirror_ubuntu-bionic-kernel.git] / drivers / acpi / scan.c
1 /*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/signal.h>
11 #include <linux/kthread.h>
12 #include <linux/dmi.h>
13 #include <linux/nls.h>
14
15 #include <acpi/acpi_drivers.h>
16
17 #include "internal.h"
18
19 #define _COMPONENT ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 #define STRUCT_TO_INT(s) (*((int*)&s))
22 extern struct acpi_device *acpi_root;
23
24 #define ACPI_BUS_CLASS "system_bus"
25 #define ACPI_BUS_HID "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME "System Bus"
27
28 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
30 static const char *dummy_hid = "device";
31
32 static LIST_HEAD(acpi_device_list);
33 static LIST_HEAD(acpi_bus_id_list);
34 static DEFINE_MUTEX(acpi_scan_lock);
35 static LIST_HEAD(acpi_scan_handlers_list);
36 DEFINE_MUTEX(acpi_device_lock);
37 LIST_HEAD(acpi_wakeup_device_list);
38
39 struct acpi_device_bus_id{
40 char bus_id[15];
41 unsigned int instance_no;
42 struct list_head node;
43 };
44
45 int 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
54 /*
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 */
59 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
60 int size)
61 {
62 int len;
63 int count;
64 struct acpi_hardware_id *id;
65
66 if (list_empty(&acpi_dev->pnp.ids))
67 return 0;
68
69 len = snprintf(modalias, size, "acpi:");
70 size -= len;
71
72 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
73 count = snprintf(&modalias[len], size, "%s:", id->id);
74 if (count < 0 || count >= size)
75 return -EINVAL;
76 len += count;
77 size -= count;
78 }
79
80 modalias[len] = '\0';
81 return len;
82 }
83
84 static ssize_t
85 acpi_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 }
96 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
97
98 /**
99 * acpi_bus_hot_remove_device: hot-remove a device and its children
100 * @context: struct acpi_eject_event pointer (freed in this func)
101 *
102 * Hot-remove a device and its children. This function frees up the
103 * memory space passed by arg context, so that the caller may call
104 * this function asynchronously through acpi_os_hotplug_execute().
105 */
106 void acpi_bus_hot_remove_device(void *context)
107 {
108 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
109 struct acpi_device *device = ej_event->device;
110 acpi_handle handle = device->handle;
111 acpi_handle temp;
112 struct acpi_object_list arg_list;
113 union acpi_object arg;
114 acpi_status status = AE_OK;
115 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
116
117 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
118 "Hot-removing device %s...\n", dev_name(&device->dev)));
119
120 acpi_bus_trim(device);
121 /* Device node has been released. */
122 device = NULL;
123
124 /* power off device */
125 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
126 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
127 printk(KERN_WARNING PREFIX
128 "Power-off device failed\n");
129
130 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
131 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 */
146 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
147 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 }
153
154 kfree(context);
155 return;
156
157 err_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);
162 return;
163 }
164 EXPORT_SYMBOL(acpi_bus_hot_remove_device);
165
166 static ssize_t real_power_state_show(struct device *dev,
167 struct device_attribute *attr, char *buf)
168 {
169 struct acpi_device *adev = to_acpi_device(dev);
170 int state;
171 int ret;
172
173 ret = acpi_device_get_power(adev, &state);
174 if (ret)
175 return ret;
176
177 return sprintf(buf, "%s\n", acpi_power_state_string(state));
178 }
179
180 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
181
182 static ssize_t power_state_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
184 {
185 struct acpi_device *adev = to_acpi_device(dev);
186
187 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
188 }
189
190 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
191
192 static ssize_t
193 acpi_eject_store(struct device *d, struct device_attribute *attr,
194 const char *buf, size_t count)
195 {
196 int ret = count;
197 acpi_status status;
198 acpi_object_type type = 0;
199 struct acpi_device *acpi_device = to_acpi_device(d);
200 struct acpi_eject_event *ej_event;
201
202 if ((!count) || (buf[0] != '1')) {
203 return -EINVAL;
204 }
205 #ifndef FORCE_EJECT
206 if (acpi_device->driver == NULL) {
207 ret = -ENODEV;
208 goto err;
209 }
210 #endif
211 status = acpi_get_type(acpi_device->handle, &type);
212 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
213 ret = -ENODEV;
214 goto err;
215 }
216
217 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
218 if (!ej_event) {
219 ret = -ENOMEM;
220 goto err;
221 }
222
223 ej_event->device = acpi_device;
224 if (acpi_device->flags.eject_pending) {
225 /* event originated from ACPI eject notification */
226 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
227 acpi_device->flags.eject_pending = 0;
228 } else {
229 /* event originated from user */
230 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
231 (void) acpi_evaluate_hotplug_ost(acpi_device->handle,
232 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
233 }
234
235 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
236 err:
237 return ret;
238 }
239
240 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
241
242 static ssize_t
243 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
244 struct acpi_device *acpi_dev = to_acpi_device(dev);
245
246 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
247 }
248 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
249
250 static ssize_t acpi_device_uid_show(struct device *dev,
251 struct device_attribute *attr, char *buf)
252 {
253 struct acpi_device *acpi_dev = to_acpi_device(dev);
254
255 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
256 }
257 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
258
259 static ssize_t acpi_device_adr_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
261 {
262 struct acpi_device *acpi_dev = to_acpi_device(dev);
263
264 return sprintf(buf, "0x%08x\n",
265 (unsigned int)(acpi_dev->pnp.bus_address));
266 }
267 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
268
269 static ssize_t
270 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
271 struct acpi_device *acpi_dev = to_acpi_device(dev);
272 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
273 int result;
274
275 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
276 if (result)
277 goto end;
278
279 result = sprintf(buf, "%s\n", (char*)path.pointer);
280 kfree(path.pointer);
281 end:
282 return result;
283 }
284 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
285
286 /* sysfs file that shows description text from the ACPI _STR method */
287 static ssize_t description_show(struct device *dev,
288 struct device_attribute *attr,
289 char *buf) {
290 struct acpi_device *acpi_dev = to_acpi_device(dev);
291 int result;
292
293 if (acpi_dev->pnp.str_obj == NULL)
294 return 0;
295
296 /*
297 * The _STR object contains a Unicode identifier for a device.
298 * We need to convert to utf-8 so it can be displayed.
299 */
300 result = utf16s_to_utf8s(
301 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
302 acpi_dev->pnp.str_obj->buffer.length,
303 UTF16_LITTLE_ENDIAN, buf,
304 PAGE_SIZE);
305
306 buf[result++] = '\n';
307
308 return result;
309 }
310 static DEVICE_ATTR(description, 0444, description_show, NULL);
311
312 static ssize_t
313 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
314 char *buf) {
315 struct acpi_device *acpi_dev = to_acpi_device(dev);
316
317 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
318 }
319 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
320
321 static int acpi_device_setup_files(struct acpi_device *dev)
322 {
323 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
324 acpi_status status;
325 acpi_handle temp;
326 unsigned long long sun;
327 int result = 0;
328
329 /*
330 * Devices gotten from FADT don't have a "path" attribute
331 */
332 if (dev->handle) {
333 result = device_create_file(&dev->dev, &dev_attr_path);
334 if (result)
335 goto end;
336 }
337
338 if (!list_empty(&dev->pnp.ids)) {
339 result = device_create_file(&dev->dev, &dev_attr_hid);
340 if (result)
341 goto end;
342
343 result = device_create_file(&dev->dev, &dev_attr_modalias);
344 if (result)
345 goto end;
346 }
347
348 /*
349 * If device has _STR, 'description' file is created
350 */
351 status = acpi_get_handle(dev->handle, "_STR", &temp);
352 if (ACPI_SUCCESS(status)) {
353 status = acpi_evaluate_object(dev->handle, "_STR",
354 NULL, &buffer);
355 if (ACPI_FAILURE(status))
356 buffer.pointer = NULL;
357 dev->pnp.str_obj = buffer.pointer;
358 result = device_create_file(&dev->dev, &dev_attr_description);
359 if (result)
360 goto end;
361 }
362
363 if (dev->flags.bus_address)
364 result = device_create_file(&dev->dev, &dev_attr_adr);
365 if (dev->pnp.unique_id)
366 result = device_create_file(&dev->dev, &dev_attr_uid);
367
368 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
369 if (ACPI_SUCCESS(status)) {
370 dev->pnp.sun = (unsigned long)sun;
371 result = device_create_file(&dev->dev, &dev_attr_sun);
372 if (result)
373 goto end;
374 } else {
375 dev->pnp.sun = (unsigned long)-1;
376 }
377
378 /*
379 * If device has _EJ0, 'eject' file is created that is used to trigger
380 * hot-removal function from userland.
381 */
382 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
383 if (ACPI_SUCCESS(status)) {
384 result = device_create_file(&dev->dev, &dev_attr_eject);
385 if (result)
386 return result;
387 }
388
389 if (dev->flags.power_manageable) {
390 result = device_create_file(&dev->dev, &dev_attr_power_state);
391 if (result)
392 return result;
393
394 if (dev->power.flags.power_resources)
395 result = device_create_file(&dev->dev,
396 &dev_attr_real_power_state);
397 }
398
399 end:
400 return result;
401 }
402
403 static void acpi_device_remove_files(struct acpi_device *dev)
404 {
405 acpi_status status;
406 acpi_handle temp;
407
408 if (dev->flags.power_manageable) {
409 device_remove_file(&dev->dev, &dev_attr_power_state);
410 if (dev->power.flags.power_resources)
411 device_remove_file(&dev->dev,
412 &dev_attr_real_power_state);
413 }
414
415 /*
416 * If device has _STR, remove 'description' file
417 */
418 status = acpi_get_handle(dev->handle, "_STR", &temp);
419 if (ACPI_SUCCESS(status)) {
420 kfree(dev->pnp.str_obj);
421 device_remove_file(&dev->dev, &dev_attr_description);
422 }
423 /*
424 * If device has _EJ0, remove 'eject' file.
425 */
426 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
427 if (ACPI_SUCCESS(status))
428 device_remove_file(&dev->dev, &dev_attr_eject);
429
430 status = acpi_get_handle(dev->handle, "_SUN", &temp);
431 if (ACPI_SUCCESS(status))
432 device_remove_file(&dev->dev, &dev_attr_sun);
433
434 if (dev->pnp.unique_id)
435 device_remove_file(&dev->dev, &dev_attr_uid);
436 if (dev->flags.bus_address)
437 device_remove_file(&dev->dev, &dev_attr_adr);
438 device_remove_file(&dev->dev, &dev_attr_modalias);
439 device_remove_file(&dev->dev, &dev_attr_hid);
440 if (dev->handle)
441 device_remove_file(&dev->dev, &dev_attr_path);
442 }
443 /* --------------------------------------------------------------------------
444 ACPI Bus operations
445 -------------------------------------------------------------------------- */
446
447 static const struct acpi_device_id *__acpi_match_device(
448 struct acpi_device *device, const struct acpi_device_id *ids)
449 {
450 const struct acpi_device_id *id;
451 struct acpi_hardware_id *hwid;
452
453 /*
454 * If the device is not present, it is unnecessary to load device
455 * driver for it.
456 */
457 if (!device->status.present)
458 return NULL;
459
460 for (id = ids; id->id[0]; id++)
461 list_for_each_entry(hwid, &device->pnp.ids, list)
462 if (!strcmp((char *) id->id, hwid->id))
463 return id;
464
465 return NULL;
466 }
467
468 /**
469 * acpi_match_device - Match a struct device against a given list of ACPI IDs
470 * @ids: Array of struct acpi_device_id object to match against.
471 * @dev: The device structure to match.
472 *
473 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
474 * object for that handle and use that object to match against a given list of
475 * device IDs.
476 *
477 * Return a pointer to the first matching ID on success or %NULL on failure.
478 */
479 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
480 const struct device *dev)
481 {
482 struct acpi_device *adev;
483
484 if (!ids || !ACPI_HANDLE(dev)
485 || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
486 return NULL;
487
488 return __acpi_match_device(adev, ids);
489 }
490 EXPORT_SYMBOL_GPL(acpi_match_device);
491
492 int acpi_match_device_ids(struct acpi_device *device,
493 const struct acpi_device_id *ids)
494 {
495 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
496 }
497 EXPORT_SYMBOL(acpi_match_device_ids);
498
499 void acpi_free_ids(struct acpi_device *device)
500 {
501 struct acpi_hardware_id *id, *tmp;
502
503 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
504 kfree(id->id);
505 kfree(id);
506 }
507 kfree(device->pnp.unique_id);
508 }
509
510 static void acpi_free_power_resources_lists(struct acpi_device *device)
511 {
512 int i;
513
514 if (device->wakeup.flags.valid)
515 acpi_power_resources_list_free(&device->wakeup.resources);
516
517 if (!device->flags.power_manageable)
518 return;
519
520 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
521 struct acpi_device_power_state *ps = &device->power.states[i];
522 acpi_power_resources_list_free(&ps->resources);
523 }
524 }
525
526 static void acpi_device_release(struct device *dev)
527 {
528 struct acpi_device *acpi_dev = to_acpi_device(dev);
529
530 acpi_free_ids(acpi_dev);
531 acpi_free_power_resources_lists(acpi_dev);
532 kfree(acpi_dev);
533 }
534
535 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
536 {
537 struct acpi_device *acpi_dev = to_acpi_device(dev);
538 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
539
540 return acpi_dev->flags.match_driver
541 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
542 }
543
544 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
545 {
546 struct acpi_device *acpi_dev = to_acpi_device(dev);
547 int len;
548
549 if (list_empty(&acpi_dev->pnp.ids))
550 return 0;
551
552 if (add_uevent_var(env, "MODALIAS="))
553 return -ENOMEM;
554 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
555 sizeof(env->buf) - env->buflen);
556 if (len >= (sizeof(env->buf) - env->buflen))
557 return -ENOMEM;
558 env->buflen += len;
559 return 0;
560 }
561
562 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
563 {
564 struct acpi_device *device = data;
565
566 device->driver->ops.notify(device, event);
567 }
568
569 static acpi_status acpi_device_notify_fixed(void *data)
570 {
571 struct acpi_device *device = data;
572
573 /* Fixed hardware devices have no handles */
574 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
575 return AE_OK;
576 }
577
578 static int acpi_device_install_notify_handler(struct acpi_device *device)
579 {
580 acpi_status status;
581
582 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
583 status =
584 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
585 acpi_device_notify_fixed,
586 device);
587 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
588 status =
589 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
590 acpi_device_notify_fixed,
591 device);
592 else
593 status = acpi_install_notify_handler(device->handle,
594 ACPI_DEVICE_NOTIFY,
595 acpi_device_notify,
596 device);
597
598 if (ACPI_FAILURE(status))
599 return -EINVAL;
600 return 0;
601 }
602
603 static void acpi_device_remove_notify_handler(struct acpi_device *device)
604 {
605 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
606 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
607 acpi_device_notify_fixed);
608 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
609 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
610 acpi_device_notify_fixed);
611 else
612 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
613 acpi_device_notify);
614 }
615
616 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
617 static int acpi_device_probe(struct device * dev)
618 {
619 struct acpi_device *acpi_dev = to_acpi_device(dev);
620 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
621 int ret;
622
623 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
624 if (!ret) {
625 if (acpi_drv->ops.notify) {
626 ret = acpi_device_install_notify_handler(acpi_dev);
627 if (ret) {
628 if (acpi_drv->ops.remove)
629 acpi_drv->ops.remove(acpi_dev);
630 return ret;
631 }
632 }
633
634 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
635 "Found driver [%s] for device [%s]\n",
636 acpi_drv->name, acpi_dev->pnp.bus_id));
637 get_device(dev);
638 }
639 return ret;
640 }
641
642 static int acpi_device_remove(struct device * dev)
643 {
644 struct acpi_device *acpi_dev = to_acpi_device(dev);
645 struct acpi_driver *acpi_drv = acpi_dev->driver;
646
647 if (acpi_drv) {
648 if (acpi_drv->ops.notify)
649 acpi_device_remove_notify_handler(acpi_dev);
650 if (acpi_drv->ops.remove)
651 acpi_drv->ops.remove(acpi_dev);
652 }
653 acpi_dev->driver = NULL;
654 acpi_dev->driver_data = NULL;
655
656 put_device(dev);
657 return 0;
658 }
659
660 struct bus_type acpi_bus_type = {
661 .name = "acpi",
662 .match = acpi_bus_match,
663 .probe = acpi_device_probe,
664 .remove = acpi_device_remove,
665 .uevent = acpi_device_uevent,
666 };
667
668 int acpi_device_add(struct acpi_device *device,
669 void (*release)(struct device *))
670 {
671 int result;
672 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
673 int found = 0;
674
675 if (device->handle) {
676 acpi_status status;
677
678 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
679 device);
680 if (ACPI_FAILURE(status)) {
681 acpi_handle_err(device->handle,
682 "Unable to attach device data\n");
683 return -ENODEV;
684 }
685 }
686
687 /*
688 * Linkage
689 * -------
690 * Link this device to its parent and siblings.
691 */
692 INIT_LIST_HEAD(&device->children);
693 INIT_LIST_HEAD(&device->node);
694 INIT_LIST_HEAD(&device->wakeup_list);
695 INIT_LIST_HEAD(&device->physical_node_list);
696 mutex_init(&device->physical_node_lock);
697 INIT_LIST_HEAD(&device->power_dependent);
698
699 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
700 if (!new_bus_id) {
701 pr_err(PREFIX "Memory allocation error\n");
702 result = -ENOMEM;
703 goto err_detach;
704 }
705
706 mutex_lock(&acpi_device_lock);
707 /*
708 * Find suitable bus_id and instance number in acpi_bus_id_list
709 * If failed, create one and link it into acpi_bus_id_list
710 */
711 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
712 if (!strcmp(acpi_device_bus_id->bus_id,
713 acpi_device_hid(device))) {
714 acpi_device_bus_id->instance_no++;
715 found = 1;
716 kfree(new_bus_id);
717 break;
718 }
719 }
720 if (!found) {
721 acpi_device_bus_id = new_bus_id;
722 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
723 acpi_device_bus_id->instance_no = 0;
724 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
725 }
726 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
727
728 if (device->parent)
729 list_add_tail(&device->node, &device->parent->children);
730
731 if (device->wakeup.flags.valid)
732 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
733 mutex_unlock(&acpi_device_lock);
734
735 if (device->parent)
736 device->dev.parent = &device->parent->dev;
737 device->dev.bus = &acpi_bus_type;
738 device->dev.release = release;
739 result = device_add(&device->dev);
740 if (result) {
741 dev_err(&device->dev, "Error registering device\n");
742 goto err;
743 }
744
745 result = acpi_device_setup_files(device);
746 if (result)
747 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
748 dev_name(&device->dev));
749
750 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
751 return 0;
752
753 err:
754 mutex_lock(&acpi_device_lock);
755 if (device->parent)
756 list_del(&device->node);
757 list_del(&device->wakeup_list);
758 mutex_unlock(&acpi_device_lock);
759
760 err_detach:
761 acpi_detach_data(device->handle, acpi_bus_data_handler);
762 return result;
763 }
764
765 static void acpi_device_unregister(struct acpi_device *device)
766 {
767 mutex_lock(&acpi_device_lock);
768 if (device->parent)
769 list_del(&device->node);
770
771 list_del(&device->wakeup_list);
772 mutex_unlock(&acpi_device_lock);
773
774 acpi_detach_data(device->handle, acpi_bus_data_handler);
775
776 acpi_power_add_remove_device(device, false);
777 acpi_device_remove_files(device);
778 if (device->remove)
779 device->remove(device);
780
781 device_del(&device->dev);
782 /*
783 * Drop the reference counts of all power resources the device depends
784 * on and turn off the ones that have no more references.
785 */
786 acpi_power_transition(device, ACPI_STATE_D3_COLD);
787 put_device(&device->dev);
788 }
789
790 /* --------------------------------------------------------------------------
791 Driver Management
792 -------------------------------------------------------------------------- */
793 /**
794 * acpi_bus_driver_init - add a device to a driver
795 * @device: the device to add and initialize
796 * @driver: driver for the device
797 *
798 * Used to initialize a device via its device driver. Called whenever a
799 * driver is bound to a device. Invokes the driver's add() ops.
800 */
801 static int
802 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
803 {
804 int result = 0;
805
806 if (!device || !driver)
807 return -EINVAL;
808
809 if (!driver->ops.add)
810 return -ENOSYS;
811
812 result = driver->ops.add(device);
813 if (result) {
814 device->driver = NULL;
815 device->driver_data = NULL;
816 return result;
817 }
818
819 device->driver = driver;
820
821 /*
822 * TBD - Configuration Management: Assign resources to device based
823 * upon possible configuration and currently allocated resources.
824 */
825
826 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
827 "Driver successfully bound to device\n"));
828 return 0;
829 }
830
831 /**
832 * acpi_bus_register_driver - register a driver with the ACPI bus
833 * @driver: driver being registered
834 *
835 * Registers a driver with the ACPI bus. Searches the namespace for all
836 * devices that match the driver's criteria and binds. Returns zero for
837 * success or a negative error status for failure.
838 */
839 int acpi_bus_register_driver(struct acpi_driver *driver)
840 {
841 int ret;
842
843 if (acpi_disabled)
844 return -ENODEV;
845 driver->drv.name = driver->name;
846 driver->drv.bus = &acpi_bus_type;
847 driver->drv.owner = driver->owner;
848
849 ret = driver_register(&driver->drv);
850 return ret;
851 }
852
853 EXPORT_SYMBOL(acpi_bus_register_driver);
854
855 /**
856 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
857 * @driver: driver to unregister
858 *
859 * Unregisters a driver with the ACPI bus. Searches the namespace for all
860 * devices that match the driver's criteria and unbinds.
861 */
862 void acpi_bus_unregister_driver(struct acpi_driver *driver)
863 {
864 driver_unregister(&driver->drv);
865 }
866
867 EXPORT_SYMBOL(acpi_bus_unregister_driver);
868
869 /* --------------------------------------------------------------------------
870 Device Enumeration
871 -------------------------------------------------------------------------- */
872 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
873 {
874 struct acpi_device *device = NULL;
875 acpi_status status;
876
877 /*
878 * Fixed hardware devices do not appear in the namespace and do not
879 * have handles, but we fabricate acpi_devices for them, so we have
880 * to deal with them specially.
881 */
882 if (!handle)
883 return acpi_root;
884
885 do {
886 status = acpi_get_parent(handle, &handle);
887 if (ACPI_FAILURE(status))
888 return status == AE_NULL_ENTRY ? NULL : acpi_root;
889 } while (acpi_bus_get_device(handle, &device));
890 return device;
891 }
892
893 acpi_status
894 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
895 {
896 acpi_status status;
897 acpi_handle tmp;
898 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
899 union acpi_object *obj;
900
901 status = acpi_get_handle(handle, "_EJD", &tmp);
902 if (ACPI_FAILURE(status))
903 return status;
904
905 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
906 if (ACPI_SUCCESS(status)) {
907 obj = buffer.pointer;
908 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
909 ejd);
910 kfree(buffer.pointer);
911 }
912 return status;
913 }
914 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
915
916 void acpi_bus_data_handler(acpi_handle handle, void *context)
917 {
918
919 /* TBD */
920
921 return;
922 }
923
924 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
925 struct acpi_device_wakeup *wakeup)
926 {
927 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
928 union acpi_object *package = NULL;
929 union acpi_object *element = NULL;
930 acpi_status status;
931 int err = -ENODATA;
932
933 if (!wakeup)
934 return -EINVAL;
935
936 INIT_LIST_HEAD(&wakeup->resources);
937
938 /* _PRW */
939 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
940 if (ACPI_FAILURE(status)) {
941 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
942 return err;
943 }
944
945 package = (union acpi_object *)buffer.pointer;
946
947 if (!package || package->package.count < 2)
948 goto out;
949
950 element = &(package->package.elements[0]);
951 if (!element)
952 goto out;
953
954 if (element->type == ACPI_TYPE_PACKAGE) {
955 if ((element->package.count < 2) ||
956 (element->package.elements[0].type !=
957 ACPI_TYPE_LOCAL_REFERENCE)
958 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
959 goto out;
960
961 wakeup->gpe_device =
962 element->package.elements[0].reference.handle;
963 wakeup->gpe_number =
964 (u32) element->package.elements[1].integer.value;
965 } else if (element->type == ACPI_TYPE_INTEGER) {
966 wakeup->gpe_device = NULL;
967 wakeup->gpe_number = element->integer.value;
968 } else {
969 goto out;
970 }
971
972 element = &(package->package.elements[1]);
973 if (element->type != ACPI_TYPE_INTEGER)
974 goto out;
975
976 wakeup->sleep_state = element->integer.value;
977
978 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
979 if (err)
980 goto out;
981
982 if (!list_empty(&wakeup->resources)) {
983 int sleep_state;
984
985 sleep_state = acpi_power_min_system_level(&wakeup->resources);
986 if (sleep_state < wakeup->sleep_state) {
987 acpi_handle_warn(handle, "Overriding _PRW sleep state "
988 "(S%d) by S%d from power resources\n",
989 (int)wakeup->sleep_state, sleep_state);
990 wakeup->sleep_state = sleep_state;
991 }
992 }
993 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
994
995 out:
996 kfree(buffer.pointer);
997 return err;
998 }
999
1000 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1001 {
1002 struct acpi_device_id button_device_ids[] = {
1003 {"PNP0C0C", 0},
1004 {"PNP0C0D", 0},
1005 {"PNP0C0E", 0},
1006 {"", 0},
1007 };
1008 acpi_status status;
1009 acpi_event_status event_status;
1010
1011 device->wakeup.flags.notifier_present = 0;
1012
1013 /* Power button, Lid switch always enable wakeup */
1014 if (!acpi_match_device_ids(device, button_device_ids)) {
1015 device->wakeup.flags.run_wake = 1;
1016 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1017 /* Do not use Lid/sleep button for S5 wakeup */
1018 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1019 device->wakeup.sleep_state = ACPI_STATE_S4;
1020 }
1021 device_set_wakeup_capable(&device->dev, true);
1022 return;
1023 }
1024
1025 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1026 device->wakeup.gpe_number,
1027 &event_status);
1028 if (status == AE_OK)
1029 device->wakeup.flags.run_wake =
1030 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1031 }
1032
1033 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1034 {
1035 acpi_handle temp;
1036 acpi_status status = 0;
1037 int err;
1038
1039 /* Presence of _PRW indicates wake capable */
1040 status = acpi_get_handle(device->handle, "_PRW", &temp);
1041 if (ACPI_FAILURE(status))
1042 return;
1043
1044 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1045 &device->wakeup);
1046 if (err) {
1047 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1048 return;
1049 }
1050
1051 device->wakeup.flags.valid = 1;
1052 device->wakeup.prepare_count = 0;
1053 acpi_bus_set_run_wake_flags(device);
1054 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1055 * system for the ACPI device with the _PRW object.
1056 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1057 * So it is necessary to call _DSW object first. Only when it is not
1058 * present will the _PSW object used.
1059 */
1060 err = acpi_device_sleep_wake(device, 0, 0, 0);
1061 if (err)
1062 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1063 "error in _DSW or _PSW evaluation\n"));
1064 }
1065
1066 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1067 {
1068 struct acpi_device_power_state *ps = &device->power.states[state];
1069 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1070 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1071 acpi_handle handle;
1072 acpi_status status;
1073
1074 INIT_LIST_HEAD(&ps->resources);
1075
1076 /* Evaluate "_PRx" to get referenced power resources */
1077 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1078 if (ACPI_SUCCESS(status)) {
1079 union acpi_object *package = buffer.pointer;
1080
1081 if (buffer.length && package
1082 && package->type == ACPI_TYPE_PACKAGE
1083 && package->package.count) {
1084 int err = acpi_extract_power_resources(package, 0,
1085 &ps->resources);
1086 if (!err)
1087 device->power.flags.power_resources = 1;
1088 }
1089 ACPI_FREE(buffer.pointer);
1090 }
1091
1092 /* Evaluate "_PSx" to see if we can do explicit sets */
1093 pathname[2] = 'S';
1094 status = acpi_get_handle(device->handle, pathname, &handle);
1095 if (ACPI_SUCCESS(status))
1096 ps->flags.explicit_set = 1;
1097
1098 /*
1099 * State is valid if there are means to put the device into it.
1100 * D3hot is only valid if _PR3 present.
1101 */
1102 if (!list_empty(&ps->resources)
1103 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1104 ps->flags.valid = 1;
1105 ps->flags.os_accessible = 1;
1106 }
1107
1108 ps->power = -1; /* Unknown - driver assigned */
1109 ps->latency = -1; /* Unknown - driver assigned */
1110 }
1111
1112 static void acpi_bus_get_power_flags(struct acpi_device *device)
1113 {
1114 acpi_status status;
1115 acpi_handle handle;
1116 u32 i;
1117
1118 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1119 status = acpi_get_handle(device->handle, "_PS0", &handle);
1120 if (ACPI_FAILURE(status)) {
1121 status = acpi_get_handle(device->handle, "_PR0", &handle);
1122 if (ACPI_FAILURE(status))
1123 return;
1124 }
1125
1126 device->flags.power_manageable = 1;
1127
1128 /*
1129 * Power Management Flags
1130 */
1131 status = acpi_get_handle(device->handle, "_PSC", &handle);
1132 if (ACPI_SUCCESS(status))
1133 device->power.flags.explicit_get = 1;
1134 status = acpi_get_handle(device->handle, "_IRC", &handle);
1135 if (ACPI_SUCCESS(status))
1136 device->power.flags.inrush_current = 1;
1137
1138 /*
1139 * Enumerate supported power management states
1140 */
1141 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1142 acpi_bus_init_power_state(device, i);
1143
1144 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1145
1146 /* Set defaults for D0 and D3 states (always valid) */
1147 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1148 device->power.states[ACPI_STATE_D0].power = 100;
1149 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1150 device->power.states[ACPI_STATE_D3].power = 0;
1151
1152 /* Set D3cold's explicit_set flag if _PS3 exists. */
1153 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1154 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1155
1156 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1157 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1158 device->power.flags.power_resources)
1159 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1160
1161 acpi_bus_init_power(device);
1162 }
1163
1164 static void acpi_bus_get_flags(struct acpi_device *device)
1165 {
1166 acpi_status status = AE_OK;
1167 acpi_handle temp = NULL;
1168
1169 /* Presence of _STA indicates 'dynamic_status' */
1170 status = acpi_get_handle(device->handle, "_STA", &temp);
1171 if (ACPI_SUCCESS(status))
1172 device->flags.dynamic_status = 1;
1173
1174 /* Presence of _RMV indicates 'removable' */
1175 status = acpi_get_handle(device->handle, "_RMV", &temp);
1176 if (ACPI_SUCCESS(status))
1177 device->flags.removable = 1;
1178
1179 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1180 status = acpi_get_handle(device->handle, "_EJD", &temp);
1181 if (ACPI_SUCCESS(status))
1182 device->flags.ejectable = 1;
1183 else {
1184 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1185 if (ACPI_SUCCESS(status))
1186 device->flags.ejectable = 1;
1187 }
1188 }
1189
1190 static void acpi_device_get_busid(struct acpi_device *device)
1191 {
1192 char bus_id[5] = { '?', 0 };
1193 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1194 int i = 0;
1195
1196 /*
1197 * Bus ID
1198 * ------
1199 * The device's Bus ID is simply the object name.
1200 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1201 */
1202 if (ACPI_IS_ROOT_DEVICE(device)) {
1203 strcpy(device->pnp.bus_id, "ACPI");
1204 return;
1205 }
1206
1207 switch (device->device_type) {
1208 case ACPI_BUS_TYPE_POWER_BUTTON:
1209 strcpy(device->pnp.bus_id, "PWRF");
1210 break;
1211 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1212 strcpy(device->pnp.bus_id, "SLPF");
1213 break;
1214 default:
1215 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1216 /* Clean up trailing underscores (if any) */
1217 for (i = 3; i > 1; i--) {
1218 if (bus_id[i] == '_')
1219 bus_id[i] = '\0';
1220 else
1221 break;
1222 }
1223 strcpy(device->pnp.bus_id, bus_id);
1224 break;
1225 }
1226 }
1227
1228 /*
1229 * acpi_bay_match - see if a device is an ejectable driver bay
1230 *
1231 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1232 * then we can safely call it an ejectable drive bay
1233 */
1234 static int acpi_bay_match(struct acpi_device *device){
1235 acpi_status status;
1236 acpi_handle handle;
1237 acpi_handle tmp;
1238 acpi_handle phandle;
1239
1240 handle = device->handle;
1241
1242 status = acpi_get_handle(handle, "_EJ0", &tmp);
1243 if (ACPI_FAILURE(status))
1244 return -ENODEV;
1245
1246 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1247 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1248 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1249 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1250 return 0;
1251
1252 if (acpi_get_parent(handle, &phandle))
1253 return -ENODEV;
1254
1255 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1256 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1257 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1258 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1259 return 0;
1260
1261 return -ENODEV;
1262 }
1263
1264 /*
1265 * acpi_dock_match - see if a device has a _DCK method
1266 */
1267 static int acpi_dock_match(struct acpi_device *device)
1268 {
1269 acpi_handle tmp;
1270 return acpi_get_handle(device->handle, "_DCK", &tmp);
1271 }
1272
1273 const char *acpi_device_hid(struct acpi_device *device)
1274 {
1275 struct acpi_hardware_id *hid;
1276
1277 if (list_empty(&device->pnp.ids))
1278 return dummy_hid;
1279
1280 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1281 return hid->id;
1282 }
1283 EXPORT_SYMBOL(acpi_device_hid);
1284
1285 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1286 {
1287 struct acpi_hardware_id *id;
1288
1289 id = kmalloc(sizeof(*id), GFP_KERNEL);
1290 if (!id)
1291 return;
1292
1293 id->id = kstrdup(dev_id, GFP_KERNEL);
1294 if (!id->id) {
1295 kfree(id);
1296 return;
1297 }
1298
1299 list_add_tail(&id->list, &device->pnp.ids);
1300 }
1301
1302 /*
1303 * Old IBM workstations have a DSDT bug wherein the SMBus object
1304 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1305 * prefix. Work around this.
1306 */
1307 static int acpi_ibm_smbus_match(struct acpi_device *device)
1308 {
1309 acpi_handle h_dummy;
1310 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1311 int result;
1312
1313 if (!dmi_name_in_vendors("IBM"))
1314 return -ENODEV;
1315
1316 /* Look for SMBS object */
1317 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1318 if (result)
1319 return result;
1320
1321 if (strcmp("SMBS", path.pointer)) {
1322 result = -ENODEV;
1323 goto out;
1324 }
1325
1326 /* Does it have the necessary (but misnamed) methods? */
1327 result = -ENODEV;
1328 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1329 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1330 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1331 result = 0;
1332 out:
1333 kfree(path.pointer);
1334 return result;
1335 }
1336
1337 static void acpi_device_set_id(struct acpi_device *device)
1338 {
1339 acpi_status status;
1340 struct acpi_device_info *info;
1341 struct acpi_pnp_device_id_list *cid_list;
1342 int i;
1343
1344 switch (device->device_type) {
1345 case ACPI_BUS_TYPE_DEVICE:
1346 if (ACPI_IS_ROOT_DEVICE(device)) {
1347 acpi_add_id(device, ACPI_SYSTEM_HID);
1348 break;
1349 }
1350
1351 status = acpi_get_object_info(device->handle, &info);
1352 if (ACPI_FAILURE(status)) {
1353 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1354 return;
1355 }
1356
1357 if (info->valid & ACPI_VALID_HID)
1358 acpi_add_id(device, info->hardware_id.string);
1359 if (info->valid & ACPI_VALID_CID) {
1360 cid_list = &info->compatible_id_list;
1361 for (i = 0; i < cid_list->count; i++)
1362 acpi_add_id(device, cid_list->ids[i].string);
1363 }
1364 if (info->valid & ACPI_VALID_ADR) {
1365 device->pnp.bus_address = info->address;
1366 device->flags.bus_address = 1;
1367 }
1368 if (info->valid & ACPI_VALID_UID)
1369 device->pnp.unique_id = kstrdup(info->unique_id.string,
1370 GFP_KERNEL);
1371
1372 kfree(info);
1373
1374 /*
1375 * Some devices don't reliably have _HIDs & _CIDs, so add
1376 * synthetic HIDs to make sure drivers can find them.
1377 */
1378 if (acpi_is_video_device(device))
1379 acpi_add_id(device, ACPI_VIDEO_HID);
1380 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1381 acpi_add_id(device, ACPI_BAY_HID);
1382 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1383 acpi_add_id(device, ACPI_DOCK_HID);
1384 else if (!acpi_ibm_smbus_match(device))
1385 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1386 else if (list_empty(&device->pnp.ids) &&
1387 ACPI_IS_ROOT_DEVICE(device->parent)) {
1388 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1389 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1390 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1391 }
1392
1393 break;
1394 case ACPI_BUS_TYPE_POWER:
1395 acpi_add_id(device, ACPI_POWER_HID);
1396 break;
1397 case ACPI_BUS_TYPE_PROCESSOR:
1398 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1399 break;
1400 case ACPI_BUS_TYPE_THERMAL:
1401 acpi_add_id(device, ACPI_THERMAL_HID);
1402 break;
1403 case ACPI_BUS_TYPE_POWER_BUTTON:
1404 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1405 break;
1406 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1407 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1408 break;
1409 }
1410 }
1411
1412 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1413 int type, unsigned long long sta)
1414 {
1415 INIT_LIST_HEAD(&device->pnp.ids);
1416 device->device_type = type;
1417 device->handle = handle;
1418 device->parent = acpi_bus_get_parent(handle);
1419 STRUCT_TO_INT(device->status) = sta;
1420 acpi_device_get_busid(device);
1421 acpi_device_set_id(device);
1422 acpi_bus_get_flags(device);
1423 device->flags.match_driver = false;
1424 device_initialize(&device->dev);
1425 dev_set_uevent_suppress(&device->dev, true);
1426 }
1427
1428 void acpi_device_add_finalize(struct acpi_device *device)
1429 {
1430 device->flags.match_driver = true;
1431 dev_set_uevent_suppress(&device->dev, false);
1432 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1433 }
1434
1435 static int acpi_add_single_object(struct acpi_device **child,
1436 acpi_handle handle, int type,
1437 unsigned long long sta)
1438 {
1439 int result;
1440 struct acpi_device *device;
1441 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1442
1443 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1444 if (!device) {
1445 printk(KERN_ERR PREFIX "Memory allocation error\n");
1446 return -ENOMEM;
1447 }
1448
1449 acpi_init_device_object(device, handle, type, sta);
1450 acpi_bus_get_power_flags(device);
1451 acpi_bus_get_wakeup_device_flags(device);
1452
1453 result = acpi_device_add(device, acpi_device_release);
1454 if (result) {
1455 acpi_device_release(&device->dev);
1456 return result;
1457 }
1458
1459 acpi_power_add_remove_device(device, true);
1460 acpi_device_add_finalize(device);
1461 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1462 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1463 dev_name(&device->dev), (char *) buffer.pointer,
1464 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1465 kfree(buffer.pointer);
1466 *child = device;
1467 return 0;
1468 }
1469
1470 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1471 unsigned long long *sta)
1472 {
1473 acpi_status status;
1474 acpi_object_type acpi_type;
1475
1476 status = acpi_get_type(handle, &acpi_type);
1477 if (ACPI_FAILURE(status))
1478 return -ENODEV;
1479
1480 switch (acpi_type) {
1481 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1482 case ACPI_TYPE_DEVICE:
1483 *type = ACPI_BUS_TYPE_DEVICE;
1484 status = acpi_bus_get_status_handle(handle, sta);
1485 if (ACPI_FAILURE(status))
1486 return -ENODEV;
1487 break;
1488 case ACPI_TYPE_PROCESSOR:
1489 *type = ACPI_BUS_TYPE_PROCESSOR;
1490 status = acpi_bus_get_status_handle(handle, sta);
1491 if (ACPI_FAILURE(status))
1492 return -ENODEV;
1493 break;
1494 case ACPI_TYPE_THERMAL:
1495 *type = ACPI_BUS_TYPE_THERMAL;
1496 *sta = ACPI_STA_DEFAULT;
1497 break;
1498 case ACPI_TYPE_POWER:
1499 *type = ACPI_BUS_TYPE_POWER;
1500 *sta = ACPI_STA_DEFAULT;
1501 break;
1502 default:
1503 return -ENODEV;
1504 }
1505
1506 return 0;
1507 }
1508
1509 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1510 void *not_used, void **return_value)
1511 {
1512 struct acpi_device *device = NULL;
1513 int type;
1514 unsigned long long sta;
1515 acpi_status status;
1516 int result;
1517
1518 acpi_bus_get_device(handle, &device);
1519 if (device)
1520 goto out;
1521
1522 result = acpi_bus_type_and_status(handle, &type, &sta);
1523 if (result)
1524 return AE_OK;
1525
1526 if (type == ACPI_BUS_TYPE_POWER) {
1527 acpi_add_power_resource(handle);
1528 return AE_OK;
1529 }
1530
1531 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1532 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1533 struct acpi_device_wakeup wakeup;
1534 acpi_handle temp;
1535
1536 status = acpi_get_handle(handle, "_PRW", &temp);
1537 if (ACPI_SUCCESS(status)) {
1538 acpi_bus_extract_wakeup_device_power_package(handle,
1539 &wakeup);
1540 acpi_power_resources_list_free(&wakeup.resources);
1541 }
1542 return AE_CTRL_DEPTH;
1543 }
1544
1545 acpi_add_single_object(&device, handle, type, sta);
1546 if (!device)
1547 return AE_CTRL_DEPTH;
1548
1549 out:
1550 if (!*return_value)
1551 *return_value = device;
1552
1553 return AE_OK;
1554 }
1555
1556 static int acpi_scan_do_attach_handler(struct acpi_device *device, char *id)
1557 {
1558 struct acpi_scan_handler *handler;
1559
1560 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) {
1561 const struct acpi_device_id *devid;
1562
1563 for (devid = handler->ids; devid->id[0]; devid++) {
1564 int ret;
1565
1566 if (strcmp((char *)devid->id, id))
1567 continue;
1568
1569 ret = handler->attach(device, devid);
1570 if (ret > 0) {
1571 device->handler = handler;
1572 return ret;
1573 } else if (ret < 0) {
1574 return ret;
1575 }
1576 }
1577 }
1578 return 0;
1579 }
1580
1581 static int acpi_scan_attach_handler(struct acpi_device *device)
1582 {
1583 struct acpi_hardware_id *hwid;
1584 int ret = 0;
1585
1586 list_for_each_entry(hwid, &device->pnp.ids, list) {
1587 ret = acpi_scan_do_attach_handler(device, hwid->id);
1588 if (ret)
1589 break;
1590
1591 }
1592 return ret;
1593 }
1594
1595 static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1596 void *not_used, void **ret_not_used)
1597 {
1598 struct acpi_device *device;
1599 unsigned long long sta_not_used;
1600 int ret;
1601
1602 /*
1603 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1604 * namespace walks prematurely.
1605 */
1606 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
1607 return AE_OK;
1608
1609 if (acpi_bus_get_device(handle, &device))
1610 return AE_CTRL_DEPTH;
1611
1612 ret = acpi_scan_attach_handler(device);
1613 if (ret)
1614 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1615
1616 ret = device_attach(&device->dev);
1617 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
1618 }
1619
1620 /**
1621 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1622 * @handle: Root of the namespace scope to scan.
1623 *
1624 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1625 * found devices.
1626 *
1627 * If no devices were found, -ENODEV is returned, but it does not mean that
1628 * there has been a real error. There just have been no suitable ACPI objects
1629 * in the table trunk from which the kernel could create a device and add an
1630 * appropriate driver.
1631 */
1632 int acpi_bus_scan(acpi_handle handle)
1633 {
1634 void *device = NULL;
1635 int error = 0;
1636
1637 mutex_lock(&acpi_scan_lock);
1638
1639 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1640 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1641 acpi_bus_check_add, NULL, NULL, &device);
1642
1643 if (!device)
1644 error = -ENODEV;
1645 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
1646 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1647 acpi_bus_device_attach, NULL, NULL, NULL);
1648
1649 mutex_unlock(&acpi_scan_lock);
1650 return error;
1651 }
1652 EXPORT_SYMBOL(acpi_bus_scan);
1653
1654 static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1655 void *not_used, void **ret_not_used)
1656 {
1657 struct acpi_device *device = NULL;
1658
1659 if (!acpi_bus_get_device(handle, &device)) {
1660 struct acpi_scan_handler *dev_handler = device->handler;
1661
1662 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1663 if (dev_handler) {
1664 if (dev_handler->detach)
1665 dev_handler->detach(device);
1666
1667 device->handler = NULL;
1668 } else {
1669 device_release_driver(&device->dev);
1670 }
1671 }
1672 return AE_OK;
1673 }
1674
1675 static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1676 void *not_used, void **ret_not_used)
1677 {
1678 struct acpi_device *device = NULL;
1679
1680 if (!acpi_bus_get_device(handle, &device))
1681 acpi_device_unregister(device);
1682
1683 return AE_OK;
1684 }
1685
1686 void acpi_bus_trim(struct acpi_device *start)
1687 {
1688 mutex_lock(&acpi_scan_lock);
1689
1690 /*
1691 * Execute acpi_bus_device_detach() as a post-order callback to detach
1692 * all ACPI drivers from the device nodes being removed.
1693 */
1694 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1695 acpi_bus_device_detach, NULL, NULL);
1696 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1697 /*
1698 * Execute acpi_bus_remove() as a post-order callback to remove device
1699 * nodes in the given namespace scope.
1700 */
1701 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1702 acpi_bus_remove, NULL, NULL);
1703 acpi_bus_remove(start->handle, 0, NULL, NULL);
1704
1705 mutex_unlock(&acpi_scan_lock);
1706 }
1707 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1708
1709 static int acpi_bus_scan_fixed(void)
1710 {
1711 int result = 0;
1712
1713 /*
1714 * Enumerate all fixed-feature devices.
1715 */
1716 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1717 struct acpi_device *device = NULL;
1718
1719 result = acpi_add_single_object(&device, NULL,
1720 ACPI_BUS_TYPE_POWER_BUTTON,
1721 ACPI_STA_DEFAULT);
1722 if (result)
1723 return result;
1724
1725 result = device_attach(&device->dev);
1726 if (result < 0)
1727 return result;
1728
1729 device_init_wakeup(&device->dev, true);
1730 }
1731
1732 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1733 struct acpi_device *device = NULL;
1734
1735 result = acpi_add_single_object(&device, NULL,
1736 ACPI_BUS_TYPE_SLEEP_BUTTON,
1737 ACPI_STA_DEFAULT);
1738 if (result)
1739 return result;
1740
1741 result = device_attach(&device->dev);
1742 }
1743
1744 return result < 0 ? result : 0;
1745 }
1746
1747 int __init acpi_scan_init(void)
1748 {
1749 int result;
1750
1751 result = bus_register(&acpi_bus_type);
1752 if (result) {
1753 /* We don't want to quit even if we failed to add suspend/resume */
1754 printk(KERN_ERR PREFIX "Could not register bus type\n");
1755 }
1756
1757 acpi_pci_root_init();
1758 acpi_pci_link_init();
1759 acpi_platform_init();
1760 acpi_csrt_init();
1761
1762 /*
1763 * Enumerate devices in the ACPI namespace.
1764 */
1765 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
1766 if (result)
1767 return result;
1768
1769 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
1770 if (result)
1771 return result;
1772
1773 result = acpi_bus_scan_fixed();
1774 if (result) {
1775 acpi_device_unregister(acpi_root);
1776 return result;
1777 }
1778
1779 acpi_update_all_gpes();
1780 return 0;
1781 }