1
2
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 <asm/pgtable.h>
18
19#include "internal.h"
20
21#define _COMPONENT ACPI_BUS_COMPONENT
22ACPI_MODULE_NAME("scan");
23extern struct acpi_device *acpi_root;
24
25#define ACPI_BUS_CLASS "system_bus"
26#define ACPI_BUS_HID "LNXSYBUS"
27#define ACPI_BUS_DEVICE_NAME "System Bus"
28
29#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
30
31#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
32
33
34
35
36
37bool acpi_force_hot_remove;
38
39static const char *dummy_hid = "device";
40
41LIST_HEAD(acpi_bus_id_list);
42static DEFINE_MUTEX(acpi_scan_lock);
43static LIST_HEAD(acpi_scan_handlers_list);
44DEFINE_MUTEX(acpi_device_lock);
45LIST_HEAD(acpi_wakeup_device_list);
46static DEFINE_MUTEX(acpi_hp_context_lock);
47
48void acpi_scan_lock_acquire(void)
49{
50 mutex_lock(&acpi_scan_lock);
51}
52EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
53
54void acpi_scan_lock_release(void)
55{
56 mutex_unlock(&acpi_scan_lock);
57}
58EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
59
60void acpi_lock_hp_context(void)
61{
62 mutex_lock(&acpi_hp_context_lock);
63}
64
65void acpi_unlock_hp_context(void)
66{
67 mutex_unlock(&acpi_hp_context_lock);
68}
69
70void acpi_initialize_hp_context(struct acpi_device *adev,
71 struct acpi_hotplug_context *hp,
72 int (*notify)(struct acpi_device *, u32),
73 void (*uevent)(struct acpi_device *, u32))
74{
75 acpi_lock_hp_context();
76 hp->notify = notify;
77 hp->uevent = uevent;
78 acpi_set_hp_context(adev, hp);
79 acpi_unlock_hp_context();
80}
81EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
82
83int acpi_scan_add_handler(struct acpi_scan_handler *handler)
84{
85 if (!handler || !handler->attach)
86 return -EINVAL;
87
88 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
89 return 0;
90}
91
92int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
93 const char *hotplug_profile_name)
94{
95 int error;
96
97 error = acpi_scan_add_handler(handler);
98 if (error)
99 return error;
100
101 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
102 return 0;
103}
104
105
106
107
108
109
110
111
112
113
114
115
116bool acpi_device_is_first_physical_node(struct acpi_device *adev,
117 const struct device *dev)
118{
119 bool ret = false;
120
121 mutex_lock(&adev->physical_node_lock);
122 if (!list_empty(&adev->physical_node_list)) {
123 const struct acpi_device_physical_node *node;
124
125 node = list_first_entry(&adev->physical_node_list,
126 struct acpi_device_physical_node, node);
127 ret = node->dev == dev;
128 }
129 mutex_unlock(&adev->physical_node_lock);
130
131 return ret;
132}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155struct acpi_device *acpi_companion_match(const struct device *dev)
156{
157 struct acpi_device *adev;
158
159 adev = ACPI_COMPANION((struct device *)dev);
160 if (!adev)
161 return NULL;
162
163 if (list_empty(&adev->pnp.ids))
164 return NULL;
165
166 return acpi_device_is_first_physical_node(adev, dev) ? adev : NULL;
167}
168
169bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
170{
171 struct acpi_device_physical_node *pn;
172 bool offline = true;
173
174 mutex_lock(&adev->physical_node_lock);
175
176 list_for_each_entry(pn, &adev->physical_node_list, node)
177 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
178 if (uevent)
179 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
180
181 offline = false;
182 break;
183 }
184
185 mutex_unlock(&adev->physical_node_lock);
186 return offline;
187}
188
189static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
190 void **ret_p)
191{
192 struct acpi_device *device = NULL;
193 struct acpi_device_physical_node *pn;
194 bool second_pass = (bool)data;
195 acpi_status status = AE_OK;
196
197 if (acpi_bus_get_device(handle, &device))
198 return AE_OK;
199
200 if (device->handler && !device->handler->hotplug.enabled) {
201 *ret_p = &device->dev;
202 return AE_SUPPORT;
203 }
204
205 mutex_lock(&device->physical_node_lock);
206
207 list_for_each_entry(pn, &device->physical_node_list, node) {
208 int ret;
209
210 if (second_pass) {
211
212 if (pn->put_online)
213 continue;
214 } else {
215 pn->put_online = false;
216 }
217 ret = device_offline(pn->dev);
218 if (acpi_force_hot_remove)
219 continue;
220
221 if (ret >= 0) {
222 pn->put_online = !ret;
223 } else {
224 *ret_p = pn->dev;
225 if (second_pass) {
226 status = AE_ERROR;
227 break;
228 }
229 }
230 }
231
232 mutex_unlock(&device->physical_node_lock);
233
234 return status;
235}
236
237static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
238 void **ret_p)
239{
240 struct acpi_device *device = NULL;
241 struct acpi_device_physical_node *pn;
242
243 if (acpi_bus_get_device(handle, &device))
244 return AE_OK;
245
246 mutex_lock(&device->physical_node_lock);
247
248 list_for_each_entry(pn, &device->physical_node_list, node)
249 if (pn->put_online) {
250 device_online(pn->dev);
251 pn->put_online = false;
252 }
253
254 mutex_unlock(&device->physical_node_lock);
255
256 return AE_OK;
257}
258
259static int acpi_scan_try_to_offline(struct acpi_device *device)
260{
261 acpi_handle handle = device->handle;
262 struct device *errdev = NULL;
263 acpi_status status;
264
265
266
267
268
269
270
271
272
273
274 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
275 NULL, acpi_bus_offline, (void *)false,
276 (void **)&errdev);
277 if (status == AE_SUPPORT) {
278 dev_warn(errdev, "Offline disabled.\n");
279 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
280 acpi_bus_online, NULL, NULL, NULL);
281 return -EPERM;
282 }
283 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
284 if (errdev) {
285 errdev = NULL;
286 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
287 NULL, acpi_bus_offline, (void *)true,
288 (void **)&errdev);
289 if (!errdev || acpi_force_hot_remove)
290 acpi_bus_offline(handle, 0, (void *)true,
291 (void **)&errdev);
292
293 if (errdev && !acpi_force_hot_remove) {
294 dev_warn(errdev, "Offline failed.\n");
295 acpi_bus_online(handle, 0, NULL, NULL);
296 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
297 ACPI_UINT32_MAX, acpi_bus_online,
298 NULL, NULL, NULL);
299 return -EBUSY;
300 }
301 }
302 return 0;
303}
304
305static int acpi_scan_hot_remove(struct acpi_device *device)
306{
307 acpi_handle handle = device->handle;
308 unsigned long long sta;
309 acpi_status status;
310
311 if (device->handler && device->handler->hotplug.demand_offline
312 && !acpi_force_hot_remove) {
313 if (!acpi_scan_is_offline(device, true))
314 return -EBUSY;
315 } else {
316 int error = acpi_scan_try_to_offline(device);
317 if (error)
318 return error;
319 }
320
321 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
322 "Hot-removing device %s...\n", dev_name(&device->dev)));
323
324 acpi_bus_trim(device);
325
326 acpi_evaluate_lck(handle, 0);
327
328
329
330 status = acpi_evaluate_ej0(handle);
331 if (status == AE_NOT_FOUND)
332 return -ENODEV;
333 else if (ACPI_FAILURE(status))
334 return -EIO;
335
336
337
338
339
340 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
341 if (ACPI_FAILURE(status)) {
342 acpi_handle_warn(handle,
343 "Status check after eject failed (0x%x)\n", status);
344 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
345 acpi_handle_warn(handle,
346 "Eject incomplete - status 0x%llx\n", sta);
347 }
348
349 return 0;
350}
351
352static int acpi_scan_device_not_present(struct acpi_device *adev)
353{
354 if (!acpi_device_enumerated(adev)) {
355 dev_warn(&adev->dev, "Still not present\n");
356 return -EALREADY;
357 }
358 acpi_bus_trim(adev);
359 return 0;
360}
361
362static int acpi_scan_device_check(struct acpi_device *adev)
363{
364 int error;
365
366 acpi_bus_get_status(adev);
367 if (adev->status.present || adev->status.functional) {
368
369
370
371
372
373
374
375
376 if (adev->handler) {
377 dev_warn(&adev->dev, "Already enumerated\n");
378 return -EALREADY;
379 }
380 error = acpi_bus_scan(adev->handle);
381 if (error) {
382 dev_warn(&adev->dev, "Namespace scan failure\n");
383 return error;
384 }
385 if (!adev->handler) {
386 dev_warn(&adev->dev, "Enumeration failure\n");
387 error = -ENODEV;
388 }
389 } else {
390 error = acpi_scan_device_not_present(adev);
391 }
392 return error;
393}
394
395static int acpi_scan_bus_check(struct acpi_device *adev)
396{
397 struct acpi_scan_handler *handler = adev->handler;
398 struct acpi_device *child;
399 int error;
400
401 acpi_bus_get_status(adev);
402 if (!(adev->status.present || adev->status.functional)) {
403 acpi_scan_device_not_present(adev);
404 return 0;
405 }
406 if (handler && handler->hotplug.scan_dependent)
407 return handler->hotplug.scan_dependent(adev);
408
409 error = acpi_bus_scan(adev->handle);
410 if (error) {
411 dev_warn(&adev->dev, "Namespace scan failure\n");
412 return error;
413 }
414 list_for_each_entry(child, &adev->children, node) {
415 error = acpi_scan_bus_check(child);
416 if (error)
417 return error;
418 }
419 return 0;
420}
421
422static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
423{
424 switch (type) {
425 case ACPI_NOTIFY_BUS_CHECK:
426 return acpi_scan_bus_check(adev);
427 case ACPI_NOTIFY_DEVICE_CHECK:
428 return acpi_scan_device_check(adev);
429 case ACPI_NOTIFY_EJECT_REQUEST:
430 case ACPI_OST_EC_OSPM_EJECT:
431 if (adev->handler && !adev->handler->hotplug.enabled) {
432 dev_info(&adev->dev, "Eject disabled\n");
433 return -EPERM;
434 }
435 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
436 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
437 return acpi_scan_hot_remove(adev);
438 }
439 return -EINVAL;
440}
441
442void acpi_device_hotplug(struct acpi_device *adev, u32 src)
443{
444 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
445 int error = -ENODEV;
446
447 lock_device_hotplug();
448 mutex_lock(&acpi_scan_lock);
449
450
451
452
453
454
455 if (adev->handle == INVALID_ACPI_HANDLE)
456 goto err_out;
457
458 if (adev->flags.is_dock_station) {
459 error = dock_notify(adev, src);
460 } else if (adev->flags.hotplug_notify) {
461 error = acpi_generic_hotplug_event(adev, src);
462 if (error == -EPERM) {
463 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
464 goto err_out;
465 }
466 } else {
467 int (*notify)(struct acpi_device *, u32);
468
469 acpi_lock_hp_context();
470 notify = adev->hp ? adev->hp->notify : NULL;
471 acpi_unlock_hp_context();
472
473
474
475
476 if (notify)
477 error = notify(adev, src);
478 else
479 goto out;
480 }
481 if (!error)
482 ost_code = ACPI_OST_SC_SUCCESS;
483
484 err_out:
485 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
486
487 out:
488 acpi_bus_put_acpi_device(adev);
489 mutex_unlock(&acpi_scan_lock);
490 unlock_device_hotplug();
491}
492
493
494
495
496
497static const struct acpi_device_id *__acpi_match_device(
498 struct acpi_device *device, const struct acpi_device_id *ids)
499{
500 const struct acpi_device_id *id;
501 struct acpi_hardware_id *hwid;
502
503
504
505
506
507 if (!device || !device->status.present)
508 return NULL;
509
510 for (id = ids; id->id[0]; id++)
511 list_for_each_entry(hwid, &device->pnp.ids, list)
512 if (!strcmp((char *) id->id, hwid->id))
513 return id;
514
515 return NULL;
516}
517
518
519
520
521
522
523
524
525
526
527
528
529const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
530 const struct device *dev)
531{
532 return __acpi_match_device(acpi_companion_match(dev), ids);
533}
534EXPORT_SYMBOL_GPL(acpi_match_device);
535
536int acpi_match_device_ids(struct acpi_device *device,
537 const struct acpi_device_id *ids)
538{
539 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
540}
541EXPORT_SYMBOL(acpi_match_device_ids);
542
543
544static bool acpi_of_driver_match_device(struct device *dev,
545 const struct device_driver *drv)
546{
547 const union acpi_object *of_compatible, *obj;
548 struct acpi_device *adev;
549 int i, nval;
550
551 adev = ACPI_COMPANION(dev);
552 if (!adev)
553 return false;
554
555 of_compatible = adev->data.of_compatible;
556 if (!drv->of_match_table || !of_compatible)
557 return false;
558
559 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
560 nval = of_compatible->package.count;
561 obj = of_compatible->package.elements;
562 } else {
563 nval = 1;
564 obj = of_compatible;
565 }
566
567 for (i = 0; i < nval; i++, obj++) {
568 const struct of_device_id *id;
569
570 for (id = drv->of_match_table; id->compatible[0]; id++)
571 if (!strcasecmp(obj->string.pointer, id->compatible))
572 return true;
573 }
574
575 return false;
576}
577
578bool acpi_driver_match_device(struct device *dev,
579 const struct device_driver *drv)
580{
581 if (!drv->acpi_match_table)
582 return acpi_of_driver_match_device(dev, drv);
583
584 return !!acpi_match_device(drv->acpi_match_table, dev);
585}
586EXPORT_SYMBOL_GPL(acpi_driver_match_device);
587
588static void acpi_free_power_resources_lists(struct acpi_device *device)
589{
590 int i;
591
592 if (device->wakeup.flags.valid)
593 acpi_power_resources_list_free(&device->wakeup.resources);
594
595 if (!device->flags.power_manageable)
596 return;
597
598 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
599 struct acpi_device_power_state *ps = &device->power.states[i];
600 acpi_power_resources_list_free(&ps->resources);
601 }
602}
603
604static void acpi_device_release(struct device *dev)
605{
606 struct acpi_device *acpi_dev = to_acpi_device(dev);
607
608 acpi_free_properties(acpi_dev);
609 acpi_free_pnp_ids(&acpi_dev->pnp);
610 acpi_free_power_resources_lists(acpi_dev);
611 kfree(acpi_dev);
612}
613
614static int acpi_bus_match(struct device *dev, struct device_driver *drv)
615{
616 struct acpi_device *acpi_dev = to_acpi_device(dev);
617 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
618
619 return acpi_dev->flags.match_driver
620 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
621}
622
623static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
624{
625 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
626}
627
628static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
629{
630 struct acpi_device *device = data;
631
632 device->driver->ops.notify(device, event);
633}
634
635static acpi_status acpi_device_notify_fixed(void *data)
636{
637 struct acpi_device *device = data;
638
639
640 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
641 return AE_OK;
642}
643
644static int acpi_device_install_notify_handler(struct acpi_device *device)
645{
646 acpi_status status;
647
648 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
649 status =
650 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
651 acpi_device_notify_fixed,
652 device);
653 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
654 status =
655 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
656 acpi_device_notify_fixed,
657 device);
658 else
659 status = acpi_install_notify_handler(device->handle,
660 ACPI_DEVICE_NOTIFY,
661 acpi_device_notify,
662 device);
663
664 if (ACPI_FAILURE(status))
665 return -EINVAL;
666 return 0;
667}
668
669static void acpi_device_remove_notify_handler(struct acpi_device *device)
670{
671 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
672 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
673 acpi_device_notify_fixed);
674 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
675 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
676 acpi_device_notify_fixed);
677 else
678 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
679 acpi_device_notify);
680}
681
682static int acpi_device_probe(struct device *dev)
683{
684 struct acpi_device *acpi_dev = to_acpi_device(dev);
685 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
686 int ret;
687
688 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
689 return -EINVAL;
690
691 if (!acpi_drv->ops.add)
692 return -ENOSYS;
693
694 ret = acpi_drv->ops.add(acpi_dev);
695 if (ret)
696 return ret;
697
698 acpi_dev->driver = acpi_drv;
699 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
700 "Driver [%s] successfully bound to device [%s]\n",
701 acpi_drv->name, acpi_dev->pnp.bus_id));
702
703 if (acpi_drv->ops.notify) {
704 ret = acpi_device_install_notify_handler(acpi_dev);
705 if (ret) {
706 if (acpi_drv->ops.remove)
707 acpi_drv->ops.remove(acpi_dev);
708
709 acpi_dev->driver = NULL;
710 acpi_dev->driver_data = NULL;
711 return ret;
712 }
713 }
714
715 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
716 acpi_drv->name, acpi_dev->pnp.bus_id));
717 get_device(dev);
718 return 0;
719}
720
721static int acpi_device_remove(struct device * dev)
722{
723 struct acpi_device *acpi_dev = to_acpi_device(dev);
724 struct acpi_driver *acpi_drv = acpi_dev->driver;
725
726 if (acpi_drv) {
727 if (acpi_drv->ops.notify)
728 acpi_device_remove_notify_handler(acpi_dev);
729 if (acpi_drv->ops.remove)
730 acpi_drv->ops.remove(acpi_dev);
731 }
732 acpi_dev->driver = NULL;
733 acpi_dev->driver_data = NULL;
734
735 put_device(dev);
736 return 0;
737}
738
739struct bus_type acpi_bus_type = {
740 .name = "acpi",
741 .match = acpi_bus_match,
742 .probe = acpi_device_probe,
743 .remove = acpi_device_remove,
744 .uevent = acpi_device_uevent,
745};
746
747static void acpi_device_del(struct acpi_device *device)
748{
749 mutex_lock(&acpi_device_lock);
750 if (device->parent)
751 list_del(&device->node);
752
753 list_del(&device->wakeup_list);
754 mutex_unlock(&acpi_device_lock);
755
756 acpi_power_add_remove_device(device, false);
757 acpi_device_remove_files(device);
758 if (device->remove)
759 device->remove(device);
760
761 device_del(&device->dev);
762}
763
764static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
765
766static LIST_HEAD(acpi_device_del_list);
767static DEFINE_MUTEX(acpi_device_del_lock);
768
769static void acpi_device_del_work_fn(struct work_struct *work_not_used)
770{
771 for (;;) {
772 struct acpi_device *adev;
773
774 mutex_lock(&acpi_device_del_lock);
775
776 if (list_empty(&acpi_device_del_list)) {
777 mutex_unlock(&acpi_device_del_lock);
778 break;
779 }
780 adev = list_first_entry(&acpi_device_del_list,
781 struct acpi_device, del_list);
782 list_del(&adev->del_list);
783
784 mutex_unlock(&acpi_device_del_lock);
785
786 blocking_notifier_call_chain(&acpi_reconfig_chain,
787 ACPI_RECONFIG_DEVICE_REMOVE, adev);
788
789 acpi_device_del(adev);
790
791
792
793
794 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
795 put_device(&adev->dev);
796 }
797}
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812static void acpi_scan_drop_device(acpi_handle handle, void *context)
813{
814 static DECLARE_WORK(work, acpi_device_del_work_fn);
815 struct acpi_device *adev = context;
816
817 mutex_lock(&acpi_device_del_lock);
818
819
820
821
822
823
824
825
826
827
828
829 if (list_empty(&acpi_device_del_list))
830 acpi_queue_hotplug_work(&work);
831
832 list_add_tail(&adev->del_list, &acpi_device_del_list);
833
834 adev->handle = INVALID_ACPI_HANDLE;
835
836 mutex_unlock(&acpi_device_del_lock);
837}
838
839static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
840 void (*callback)(void *))
841{
842 acpi_status status;
843
844 if (!device)
845 return -EINVAL;
846
847 status = acpi_get_data_full(handle, acpi_scan_drop_device,
848 (void **)device, callback);
849 if (ACPI_FAILURE(status) || !*device) {
850 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
851 handle));
852 return -ENODEV;
853 }
854 return 0;
855}
856
857int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
858{
859 return acpi_get_device_data(handle, device, NULL);
860}
861EXPORT_SYMBOL(acpi_bus_get_device);
862
863static void get_acpi_device(void *dev)
864{
865 if (dev)
866 get_device(&((struct acpi_device *)dev)->dev);
867}
868
869struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
870{
871 struct acpi_device *adev = NULL;
872
873 acpi_get_device_data(handle, &adev, get_acpi_device);
874 return adev;
875}
876
877void acpi_bus_put_acpi_device(struct acpi_device *adev)
878{
879 put_device(&adev->dev);
880}
881
882int acpi_device_add(struct acpi_device *device,
883 void (*release)(struct device *))
884{
885 int result;
886 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
887 int found = 0;
888
889 if (device->handle) {
890 acpi_status status;
891
892 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
893 device);
894 if (ACPI_FAILURE(status)) {
895 acpi_handle_err(device->handle,
896 "Unable to attach device data\n");
897 return -ENODEV;
898 }
899 }
900
901
902
903
904
905
906 INIT_LIST_HEAD(&device->children);
907 INIT_LIST_HEAD(&device->node);
908 INIT_LIST_HEAD(&device->wakeup_list);
909 INIT_LIST_HEAD(&device->physical_node_list);
910 INIT_LIST_HEAD(&device->del_list);
911 mutex_init(&device->physical_node_lock);
912
913 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
914 if (!new_bus_id) {
915 pr_err(PREFIX "Memory allocation error\n");
916 result = -ENOMEM;
917 goto err_detach;
918 }
919
920 mutex_lock(&acpi_device_lock);
921
922
923
924
925 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
926 if (!strcmp(acpi_device_bus_id->bus_id,
927 acpi_device_hid(device))) {
928 acpi_device_bus_id->instance_no++;
929 found = 1;
930 kfree(new_bus_id);
931 break;
932 }
933 }
934 if (!found) {
935 acpi_device_bus_id = new_bus_id;
936 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
937 acpi_device_bus_id->instance_no = 0;
938 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
939 }
940 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
941
942 if (device->parent)
943 list_add_tail(&device->node, &device->parent->children);
944
945 if (device->wakeup.flags.valid)
946 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
947 mutex_unlock(&acpi_device_lock);
948
949 if (device->parent)
950 device->dev.parent = &device->parent->dev;
951 device->dev.bus = &acpi_bus_type;
952 device->dev.release = release;
953 result = device_add(&device->dev);
954 if (result) {
955 dev_err(&device->dev, "Error registering device\n");
956 goto err;
957 }
958
959 result = acpi_device_setup_files(device);
960 if (result)
961 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
962 dev_name(&device->dev));
963
964 return 0;
965
966 err:
967 mutex_lock(&acpi_device_lock);
968 if (device->parent)
969 list_del(&device->node);
970 list_del(&device->wakeup_list);
971 mutex_unlock(&acpi_device_lock);
972
973 err_detach:
974 acpi_detach_data(device->handle, acpi_scan_drop_device);
975 return result;
976}
977
978
979
980
981
982
983
984
985
986
987
988
989int acpi_bus_register_driver(struct acpi_driver *driver)
990{
991 int ret;
992
993 if (acpi_disabled)
994 return -ENODEV;
995 driver->drv.name = driver->name;
996 driver->drv.bus = &acpi_bus_type;
997 driver->drv.owner = driver->owner;
998
999 ret = driver_register(&driver->drv);
1000 return ret;
1001}
1002
1003EXPORT_SYMBOL(acpi_bus_register_driver);
1004
1005
1006
1007
1008
1009
1010
1011
1012void acpi_bus_unregister_driver(struct acpi_driver *driver)
1013{
1014 driver_unregister(&driver->drv);
1015}
1016
1017EXPORT_SYMBOL(acpi_bus_unregister_driver);
1018
1019
1020
1021
1022static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1023{
1024 struct acpi_device *device = NULL;
1025 acpi_status status;
1026
1027
1028
1029
1030
1031
1032 if (!handle)
1033 return acpi_root;
1034
1035 do {
1036 status = acpi_get_parent(handle, &handle);
1037 if (ACPI_FAILURE(status))
1038 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1039 } while (acpi_bus_get_device(handle, &device));
1040 return device;
1041}
1042
1043acpi_status
1044acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1045{
1046 acpi_status status;
1047 acpi_handle tmp;
1048 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1049 union acpi_object *obj;
1050
1051 status = acpi_get_handle(handle, "_EJD", &tmp);
1052 if (ACPI_FAILURE(status))
1053 return status;
1054
1055 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1056 if (ACPI_SUCCESS(status)) {
1057 obj = buffer.pointer;
1058 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1059 ejd);
1060 kfree(buffer.pointer);
1061 }
1062 return status;
1063}
1064EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1065
1066static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1067 struct acpi_device_wakeup *wakeup)
1068{
1069 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1070 union acpi_object *package = NULL;
1071 union acpi_object *element = NULL;
1072 acpi_status status;
1073 int err = -ENODATA;
1074
1075 if (!wakeup)
1076 return -EINVAL;
1077
1078 INIT_LIST_HEAD(&wakeup->resources);
1079
1080
1081 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1082 if (ACPI_FAILURE(status)) {
1083 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1084 return err;
1085 }
1086
1087 package = (union acpi_object *)buffer.pointer;
1088
1089 if (!package || package->package.count < 2)
1090 goto out;
1091
1092 element = &(package->package.elements[0]);
1093 if (!element)
1094 goto out;
1095
1096 if (element->type == ACPI_TYPE_PACKAGE) {
1097 if ((element->package.count < 2) ||
1098 (element->package.elements[0].type !=
1099 ACPI_TYPE_LOCAL_REFERENCE)
1100 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1101 goto out;
1102
1103 wakeup->gpe_device =
1104 element->package.elements[0].reference.handle;
1105 wakeup->gpe_number =
1106 (u32) element->package.elements[1].integer.value;
1107 } else if (element->type == ACPI_TYPE_INTEGER) {
1108 wakeup->gpe_device = NULL;
1109 wakeup->gpe_number = element->integer.value;
1110 } else {
1111 goto out;
1112 }
1113
1114 element = &(package->package.elements[1]);
1115 if (element->type != ACPI_TYPE_INTEGER)
1116 goto out;
1117
1118 wakeup->sleep_state = element->integer.value;
1119
1120 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1121 if (err)
1122 goto out;
1123
1124 if (!list_empty(&wakeup->resources)) {
1125 int sleep_state;
1126
1127 err = acpi_power_wakeup_list_init(&wakeup->resources,
1128 &sleep_state);
1129 if (err) {
1130 acpi_handle_warn(handle, "Retrieving current states "
1131 "of wakeup power resources failed\n");
1132 acpi_power_resources_list_free(&wakeup->resources);
1133 goto out;
1134 }
1135 if (sleep_state < wakeup->sleep_state) {
1136 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1137 "(S%d) by S%d from power resources\n",
1138 (int)wakeup->sleep_state, sleep_state);
1139 wakeup->sleep_state = sleep_state;
1140 }
1141 }
1142 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1143
1144 out:
1145 kfree(buffer.pointer);
1146 return err;
1147}
1148
1149static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1150{
1151 struct acpi_device_id button_device_ids[] = {
1152 {"PNP0C0C", 0},
1153 {"PNP0C0D", 0},
1154 {"PNP0C0E", 0},
1155 {"", 0},
1156 };
1157 acpi_status status;
1158 acpi_event_status event_status;
1159
1160 device->wakeup.flags.notifier_present = 0;
1161
1162
1163 if (!acpi_match_device_ids(device, button_device_ids)) {
1164 device->wakeup.flags.run_wake = 1;
1165 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1166
1167 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1168 device->wakeup.sleep_state = ACPI_STATE_S4;
1169 }
1170 device_set_wakeup_capable(&device->dev, true);
1171 return;
1172 }
1173
1174 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1175 device->wakeup.gpe_number,
1176 &event_status);
1177 if (status == AE_OK)
1178 device->wakeup.flags.run_wake =
1179 !!(event_status & ACPI_EVENT_FLAG_ENABLE_SET);
1180}
1181
1182static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1183{
1184 int err;
1185
1186
1187 if (!acpi_has_method(device->handle, "_PRW"))
1188 return;
1189
1190 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1191 &device->wakeup);
1192 if (err) {
1193 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1194 return;
1195 }
1196
1197 device->wakeup.flags.valid = 1;
1198 device->wakeup.prepare_count = 0;
1199 acpi_bus_set_run_wake_flags(device);
1200
1201
1202
1203
1204
1205
1206 err = acpi_device_sleep_wake(device, 0, 0, 0);
1207 if (err)
1208 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1209 "error in _DSW or _PSW evaluation\n"));
1210}
1211
1212static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1213{
1214 struct acpi_device_power_state *ps = &device->power.states[state];
1215 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1216 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1217 acpi_status status;
1218
1219 INIT_LIST_HEAD(&ps->resources);
1220
1221
1222 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1223 if (ACPI_SUCCESS(status)) {
1224 union acpi_object *package = buffer.pointer;
1225
1226 if (buffer.length && package
1227 && package->type == ACPI_TYPE_PACKAGE
1228 && package->package.count) {
1229 int err = acpi_extract_power_resources(package, 0,
1230 &ps->resources);
1231 if (!err)
1232 device->power.flags.power_resources = 1;
1233 }
1234 ACPI_FREE(buffer.pointer);
1235 }
1236
1237
1238 pathname[2] = 'S';
1239 if (acpi_has_method(device->handle, pathname))
1240 ps->flags.explicit_set = 1;
1241
1242
1243
1244
1245
1246 if (!list_empty(&ps->resources)
1247 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1248 ps->flags.valid = 1;
1249 ps->flags.os_accessible = 1;
1250 }
1251
1252 ps->power = -1;
1253 ps->latency = -1;
1254}
1255
1256static void acpi_bus_get_power_flags(struct acpi_device *device)
1257{
1258 u32 i;
1259
1260
1261 if (!acpi_has_method(device->handle, "_PS0") &&
1262 !acpi_has_method(device->handle, "_PR0"))
1263 return;
1264
1265 device->flags.power_manageable = 1;
1266
1267
1268
1269
1270 if (acpi_has_method(device->handle, "_PSC"))
1271 device->power.flags.explicit_get = 1;
1272 if (acpi_has_method(device->handle, "_IRC"))
1273 device->power.flags.inrush_current = 1;
1274
1275
1276
1277
1278 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1279 acpi_bus_init_power_state(device, i);
1280
1281 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1282
1283
1284 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1285 device->power.states[ACPI_STATE_D0].power = 100;
1286 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1287 device->power.states[ACPI_STATE_D3_COLD].power = 0;
1288
1289
1290 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1291 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1292
1293
1294 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1295 device->power.flags.power_resources)
1296 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1297
1298 if (acpi_bus_init_power(device)) {
1299 acpi_free_power_resources_lists(device);
1300 device->flags.power_manageable = 0;
1301 }
1302}
1303
1304static void acpi_bus_get_flags(struct acpi_device *device)
1305{
1306
1307 if (acpi_has_method(device->handle, "_STA"))
1308 device->flags.dynamic_status = 1;
1309
1310
1311 if (acpi_has_method(device->handle, "_RMV"))
1312 device->flags.removable = 1;
1313
1314
1315 if (acpi_has_method(device->handle, "_EJD") ||
1316 acpi_has_method(device->handle, "_EJ0"))
1317 device->flags.ejectable = 1;
1318}
1319
1320static void acpi_device_get_busid(struct acpi_device *device)
1321{
1322 char bus_id[5] = { '?', 0 };
1323 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1324 int i = 0;
1325
1326
1327
1328
1329
1330
1331
1332 if (ACPI_IS_ROOT_DEVICE(device)) {
1333 strcpy(device->pnp.bus_id, "ACPI");
1334 return;
1335 }
1336
1337 switch (device->device_type) {
1338 case ACPI_BUS_TYPE_POWER_BUTTON:
1339 strcpy(device->pnp.bus_id, "PWRF");
1340 break;
1341 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1342 strcpy(device->pnp.bus_id, "SLPF");
1343 break;
1344 default:
1345 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1346
1347 for (i = 3; i > 1; i--) {
1348 if (bus_id[i] == '_')
1349 bus_id[i] = '\0';
1350 else
1351 break;
1352 }
1353 strcpy(device->pnp.bus_id, bus_id);
1354 break;
1355 }
1356}
1357
1358
1359
1360
1361
1362
1363
1364bool acpi_ata_match(acpi_handle handle)
1365{
1366 return acpi_has_method(handle, "_GTF") ||
1367 acpi_has_method(handle, "_GTM") ||
1368 acpi_has_method(handle, "_STM") ||
1369 acpi_has_method(handle, "_SDD");
1370}
1371
1372
1373
1374
1375
1376
1377
1378bool acpi_bay_match(acpi_handle handle)
1379{
1380 acpi_handle phandle;
1381
1382 if (!acpi_has_method(handle, "_EJ0"))
1383 return false;
1384 if (acpi_ata_match(handle))
1385 return true;
1386 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1387 return false;
1388
1389 return acpi_ata_match(phandle);
1390}
1391
1392bool acpi_device_is_battery(acpi_handle handle)
1393{
1394 struct acpi_device_info *info;
1395 bool ret = false;
1396
1397 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
1398 return false;
1399
1400 if (info->valid & ACPI_VALID_HID)
1401 ret = !strcmp("PNP0C0A", info->hardware_id.string);
1402
1403 kfree(info);
1404 return ret;
1405}
1406
1407static bool is_ejectable_bay(acpi_handle handle)
1408{
1409 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(handle))
1410 return true;
1411
1412 return acpi_bay_match(handle);
1413}
1414
1415
1416
1417
1418bool acpi_dock_match(acpi_handle handle)
1419{
1420 return acpi_has_method(handle, "_DCK");
1421}
1422
1423const char *acpi_device_hid(struct acpi_device *device)
1424{
1425 struct acpi_hardware_id *hid;
1426
1427 if (list_empty(&device->pnp.ids))
1428 return dummy_hid;
1429
1430 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1431 return hid->id;
1432}
1433EXPORT_SYMBOL(acpi_device_hid);
1434
1435static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1436{
1437 struct acpi_hardware_id *id;
1438
1439 id = kmalloc(sizeof(*id), GFP_KERNEL);
1440 if (!id)
1441 return;
1442
1443 id->id = kstrdup(dev_id, GFP_KERNEL);
1444 if (!id->id) {
1445 kfree(id);
1446 return;
1447 }
1448
1449 list_add_tail(&id->list, &pnp->ids);
1450 pnp->type.hardware_id = 1;
1451}
1452
1453
1454
1455
1456
1457
1458static bool acpi_ibm_smbus_match(acpi_handle handle)
1459{
1460 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1461 struct acpi_buffer path = { sizeof(node_name), node_name };
1462
1463 if (!dmi_name_in_vendors("IBM"))
1464 return false;
1465
1466
1467 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1468 strcmp("SMBS", path.pointer))
1469 return false;
1470
1471
1472 if (acpi_has_method(handle, "SBI") &&
1473 acpi_has_method(handle, "SBR") &&
1474 acpi_has_method(handle, "SBW"))
1475 return true;
1476
1477 return false;
1478}
1479
1480static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1481 int device_type)
1482{
1483 acpi_status status;
1484 struct acpi_device_info *info;
1485 struct acpi_pnp_device_id_list *cid_list;
1486 int i;
1487
1488 switch (device_type) {
1489 case ACPI_BUS_TYPE_DEVICE:
1490 if (handle == ACPI_ROOT_OBJECT) {
1491 acpi_add_id(pnp, ACPI_SYSTEM_HID);
1492 break;
1493 }
1494
1495 status = acpi_get_object_info(handle, &info);
1496 if (ACPI_FAILURE(status)) {
1497 pr_err(PREFIX "%s: Error reading device info\n",
1498 __func__);
1499 return;
1500 }
1501
1502 if (info->valid & ACPI_VALID_HID) {
1503 acpi_add_id(pnp, info->hardware_id.string);
1504 pnp->type.platform_id = 1;
1505 }
1506 if (info->valid & ACPI_VALID_CID) {
1507 cid_list = &info->compatible_id_list;
1508 for (i = 0; i < cid_list->count; i++)
1509 acpi_add_id(pnp, cid_list->ids[i].string);
1510 }
1511 if (info->valid & ACPI_VALID_ADR) {
1512 pnp->bus_address = info->address;
1513 pnp->type.bus_address = 1;
1514 }
1515 if (info->valid & ACPI_VALID_UID)
1516 pnp->unique_id = kstrdup(info->unique_id.string,
1517 GFP_KERNEL);
1518
1519 kfree(info);
1520
1521
1522
1523
1524
1525 if (acpi_is_video_device(handle))
1526 acpi_add_id(pnp, ACPI_VIDEO_HID);
1527 else if (acpi_bay_match(handle))
1528 acpi_add_id(pnp, ACPI_BAY_HID);
1529 else if (acpi_dock_match(handle))
1530 acpi_add_id(pnp, ACPI_DOCK_HID);
1531 else if (acpi_ibm_smbus_match(handle))
1532 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1533 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1534 acpi_add_id(pnp, ACPI_BUS_HID);
1535 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1536 strcpy(pnp->device_class, ACPI_BUS_CLASS);
1537 }
1538
1539 break;
1540 case ACPI_BUS_TYPE_POWER:
1541 acpi_add_id(pnp, ACPI_POWER_HID);
1542 break;
1543 case ACPI_BUS_TYPE_PROCESSOR:
1544 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1545 break;
1546 case ACPI_BUS_TYPE_THERMAL:
1547 acpi_add_id(pnp, ACPI_THERMAL_HID);
1548 break;
1549 case ACPI_BUS_TYPE_POWER_BUTTON:
1550 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1551 break;
1552 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1553 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1554 break;
1555 }
1556}
1557
1558void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1559{
1560 struct acpi_hardware_id *id, *tmp;
1561
1562 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1563 kfree(id->id);
1564 kfree(id);
1565 }
1566 kfree(pnp->unique_id);
1567}
1568
1569void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1570 int type, unsigned long long sta)
1571{
1572 INIT_LIST_HEAD(&device->pnp.ids);
1573 device->device_type = type;
1574 device->handle = handle;
1575 device->parent = acpi_bus_get_parent(handle);
1576 device->fwnode.type = FWNODE_ACPI;
1577 acpi_set_device_status(device, sta);
1578 acpi_device_get_busid(device);
1579 acpi_set_pnp_ids(handle, &device->pnp, type);
1580 acpi_init_properties(device);
1581 acpi_bus_get_flags(device);
1582 device->flags.match_driver = false;
1583 device->flags.initialized = true;
1584 acpi_device_clear_enumerated(device);
1585 device_initialize(&device->dev);
1586 dev_set_uevent_suppress(&device->dev, true);
1587}
1588
1589void acpi_device_add_finalize(struct acpi_device *device)
1590{
1591 dev_set_uevent_suppress(&device->dev, false);
1592 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1593}
1594
1595static int acpi_add_single_object(struct acpi_device **child,
1596 acpi_handle handle, int type,
1597 unsigned long long sta)
1598{
1599 int result;
1600 struct acpi_device *device;
1601 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1602
1603 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1604 if (!device) {
1605 printk(KERN_ERR PREFIX "Memory allocation error\n");
1606 return -ENOMEM;
1607 }
1608
1609 acpi_init_device_object(device, handle, type, sta);
1610 acpi_bus_get_power_flags(device);
1611 acpi_bus_get_wakeup_device_flags(device);
1612
1613 result = acpi_device_add(device, acpi_device_release);
1614 if (result) {
1615 acpi_device_release(&device->dev);
1616 return result;
1617 }
1618
1619 acpi_power_add_remove_device(device, true);
1620 acpi_device_add_finalize(device);
1621 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1622 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1623 dev_name(&device->dev), (char *) buffer.pointer,
1624 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1625 kfree(buffer.pointer);
1626 *child = device;
1627 return 0;
1628}
1629
1630static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1631 unsigned long long *sta)
1632{
1633 acpi_status status;
1634 acpi_object_type acpi_type;
1635
1636 status = acpi_get_type(handle, &acpi_type);
1637 if (ACPI_FAILURE(status))
1638 return -ENODEV;
1639
1640 switch (acpi_type) {
1641 case ACPI_TYPE_ANY:
1642 case ACPI_TYPE_DEVICE:
1643 *type = ACPI_BUS_TYPE_DEVICE;
1644 status = acpi_bus_get_status_handle(handle, sta);
1645 if (ACPI_FAILURE(status))
1646 return -ENODEV;
1647 break;
1648 case ACPI_TYPE_PROCESSOR:
1649 *type = ACPI_BUS_TYPE_PROCESSOR;
1650 status = acpi_bus_get_status_handle(handle, sta);
1651 if (ACPI_FAILURE(status))
1652 return -ENODEV;
1653 break;
1654 case ACPI_TYPE_THERMAL:
1655 *type = ACPI_BUS_TYPE_THERMAL;
1656 *sta = ACPI_STA_DEFAULT;
1657 break;
1658 case ACPI_TYPE_POWER:
1659 *type = ACPI_BUS_TYPE_POWER;
1660 *sta = ACPI_STA_DEFAULT;
1661 break;
1662 default:
1663 return -ENODEV;
1664 }
1665
1666 return 0;
1667}
1668
1669bool acpi_device_is_present(struct acpi_device *adev)
1670{
1671 if (adev->status.present || adev->status.functional)
1672 return true;
1673
1674 adev->flags.initialized = false;
1675 return false;
1676}
1677
1678static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1679 char *idstr,
1680 const struct acpi_device_id **matchid)
1681{
1682 const struct acpi_device_id *devid;
1683
1684 if (handler->match)
1685 return handler->match(idstr, matchid);
1686
1687 for (devid = handler->ids; devid->id[0]; devid++)
1688 if (!strcmp((char *)devid->id, idstr)) {
1689 if (matchid)
1690 *matchid = devid;
1691
1692 return true;
1693 }
1694
1695 return false;
1696}
1697
1698static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1699 const struct acpi_device_id **matchid)
1700{
1701 struct acpi_scan_handler *handler;
1702
1703 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1704 if (acpi_scan_handler_matching(handler, idstr, matchid))
1705 return handler;
1706
1707 return NULL;
1708}
1709
1710void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1711{
1712 if (!!hotplug->enabled == !!val)
1713 return;
1714
1715 mutex_lock(&acpi_scan_lock);
1716
1717 hotplug->enabled = val;
1718
1719 mutex_unlock(&acpi_scan_lock);
1720}
1721
1722static void acpi_scan_init_hotplug(struct acpi_device *adev)
1723{
1724 struct acpi_hardware_id *hwid;
1725
1726 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev->handle)) {
1727 acpi_dock_add(adev);
1728 return;
1729 }
1730 list_for_each_entry(hwid, &adev->pnp.ids, list) {
1731 struct acpi_scan_handler *handler;
1732
1733 handler = acpi_scan_match_handler(hwid->id, NULL);
1734 if (handler) {
1735 adev->flags.hotplug_notify = true;
1736 break;
1737 }
1738 }
1739}
1740
1741static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1742 void *not_used, void **return_value)
1743{
1744 struct acpi_device *device = NULL;
1745 int type;
1746 unsigned long long sta;
1747 int result;
1748
1749 acpi_bus_get_device(handle, &device);
1750 if (device)
1751 goto out;
1752
1753 result = acpi_bus_type_and_status(handle, &type, &sta);
1754 if (result)
1755 return AE_OK;
1756
1757 if (type == ACPI_BUS_TYPE_POWER) {
1758 acpi_add_power_resource(handle);
1759 return AE_OK;
1760 }
1761
1762 acpi_add_single_object(&device, handle, type, sta);
1763 if (!device)
1764 return AE_CTRL_DEPTH;
1765
1766 acpi_scan_init_hotplug(device);
1767
1768 out:
1769 if (!*return_value)
1770 *return_value = device;
1771
1772 return AE_OK;
1773}
1774
1775static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
1776{
1777 bool *is_spi_i2c_slave_p = data;
1778
1779 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1780 return 1;
1781
1782
1783
1784
1785
1786 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
1787 *is_spi_i2c_slave_p = true;
1788
1789
1790 return -1;
1791}
1792
1793static void acpi_default_enumeration(struct acpi_device *device)
1794{
1795 struct list_head resource_list;
1796 bool is_spi_i2c_slave = false;
1797
1798 if (!device->pnp.type.platform_id || device->handler)
1799 return;
1800
1801
1802
1803
1804
1805 INIT_LIST_HEAD(&resource_list);
1806 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
1807 &is_spi_i2c_slave);
1808 acpi_dev_free_resource_list(&resource_list);
1809 if (!is_spi_i2c_slave) {
1810 acpi_create_platform_device(device);
1811 acpi_device_set_enumerated(device);
1812 } else {
1813 blocking_notifier_call_chain(&acpi_reconfig_chain,
1814 ACPI_RECONFIG_DEVICE_ADD, device);
1815 }
1816}
1817
1818static int acpi_scan_attach_handler(struct acpi_device *device)
1819{
1820 struct acpi_hardware_id *hwid;
1821 int ret = 0;
1822
1823 list_for_each_entry(hwid, &device->pnp.ids, list) {
1824 const struct acpi_device_id *devid;
1825 struct acpi_scan_handler *handler;
1826
1827 handler = acpi_scan_match_handler(hwid->id, &devid);
1828 if (handler) {
1829 device->handler = handler;
1830 ret = handler->attach(device, devid);
1831 if (ret > 0)
1832 break;
1833
1834 device->handler = NULL;
1835 if (ret < 0)
1836 break;
1837 }
1838 }
1839 if (!ret)
1840 acpi_default_enumeration(device);
1841
1842 return ret;
1843}
1844
1845static void acpi_bus_attach(struct acpi_device *device)
1846{
1847 struct acpi_device *child;
1848 acpi_handle ejd;
1849 int ret;
1850
1851 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
1852 register_dock_dependent_device(device, ejd);
1853
1854 acpi_bus_get_status(device);
1855
1856 if (!acpi_device_is_present(device)) {
1857 acpi_device_clear_enumerated(device);
1858 return;
1859 }
1860 if (device->handler)
1861 goto ok;
1862
1863 if (!device->flags.initialized) {
1864 acpi_bus_update_power(device, NULL);
1865 device->flags.initialized = true;
1866 }
1867
1868 ret = acpi_scan_attach_handler(device);
1869 if (ret < 0)
1870 return;
1871
1872 device->flags.match_driver = true;
1873 if (!ret) {
1874 ret = device_attach(&device->dev);
1875 if (ret < 0)
1876 return;
1877 }
1878
1879 ok:
1880 list_for_each_entry(child, &device->children, node)
1881 acpi_bus_attach(child);
1882
1883 if (device->handler && device->handler->hotplug.notify_online)
1884 device->handler->hotplug.notify_online(device);
1885}
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901int acpi_bus_scan(acpi_handle handle)
1902{
1903 void *device = NULL;
1904
1905 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
1906 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1907 acpi_bus_check_add, NULL, NULL, &device);
1908
1909 if (device) {
1910 acpi_bus_attach(device);
1911 return 0;
1912 }
1913 return -ENODEV;
1914}
1915EXPORT_SYMBOL(acpi_bus_scan);
1916
1917
1918
1919
1920
1921
1922
1923void acpi_bus_trim(struct acpi_device *adev)
1924{
1925 struct acpi_scan_handler *handler = adev->handler;
1926 struct acpi_device *child;
1927
1928 list_for_each_entry_reverse(child, &adev->children, node)
1929 acpi_bus_trim(child);
1930
1931 adev->flags.match_driver = false;
1932 if (handler) {
1933 if (handler->detach)
1934 handler->detach(adev);
1935
1936 adev->handler = NULL;
1937 } else {
1938 device_release_driver(&adev->dev);
1939 }
1940
1941
1942
1943
1944 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
1945 adev->flags.initialized = false;
1946 acpi_device_clear_enumerated(adev);
1947}
1948EXPORT_SYMBOL_GPL(acpi_bus_trim);
1949
1950static int acpi_bus_scan_fixed(void)
1951{
1952 int result = 0;
1953
1954
1955
1956
1957 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1958 struct acpi_device *device = NULL;
1959
1960 result = acpi_add_single_object(&device, NULL,
1961 ACPI_BUS_TYPE_POWER_BUTTON,
1962 ACPI_STA_DEFAULT);
1963 if (result)
1964 return result;
1965
1966 device->flags.match_driver = true;
1967 result = device_attach(&device->dev);
1968 if (result < 0)
1969 return result;
1970
1971 device_init_wakeup(&device->dev, true);
1972 }
1973
1974 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1975 struct acpi_device *device = NULL;
1976
1977 result = acpi_add_single_object(&device, NULL,
1978 ACPI_BUS_TYPE_SLEEP_BUTTON,
1979 ACPI_STA_DEFAULT);
1980 if (result)
1981 return result;
1982
1983 device->flags.match_driver = true;
1984 result = device_attach(&device->dev);
1985 }
1986
1987 return result < 0 ? result : 0;
1988}
1989
1990static bool acpi_scan_initialized;
1991
1992int __init acpi_scan_init(void)
1993{
1994 int result;
1995
1996 result = bus_register(&acpi_bus_type);
1997 if (result) {
1998
1999 printk(KERN_ERR PREFIX "Could not register bus type\n");
2000 }
2001
2002 acpi_pci_root_init();
2003 acpi_pci_link_init();
2004 acpi_processor_init();
2005 acpi_lpss_init();
2006 acpi_apd_init();
2007 acpi_cmos_rtc_init();
2008 acpi_container_init();
2009 acpi_memory_hotplug_init();
2010 acpi_pnp_init();
2011 acpi_int340x_thermal_init();
2012 acpi_watchdog_init();
2013 acpi_init_lpit();
2014
2015 acpi_gpe_apply_masked_gpes();
2016 acpi_update_all_gpes();
2017
2018 mutex_lock(&acpi_scan_lock);
2019
2020
2021
2022 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2023 if (result)
2024 goto out;
2025
2026 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2027 if (result)
2028 goto out;
2029
2030 result = acpi_bus_scan_fixed();
2031 if (result) {
2032 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2033 acpi_device_del(acpi_root);
2034 put_device(&acpi_root->dev);
2035 goto out;
2036 }
2037
2038 acpi_gpe_apply_masked_gpes();
2039 acpi_update_all_gpes();
2040
2041 acpi_scan_initialized = true;
2042
2043 out:
2044 mutex_unlock(&acpi_scan_lock);
2045 return result;
2046}
2047
2048struct acpi_table_events_work {
2049 struct work_struct work;
2050 void *table;
2051 u32 event;
2052};
2053
2054static void acpi_table_events_fn(struct work_struct *work)
2055{
2056 struct acpi_table_events_work *tew;
2057
2058 tew = container_of(work, struct acpi_table_events_work, work);
2059
2060 if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2061 acpi_scan_lock_acquire();
2062 acpi_bus_scan(ACPI_ROOT_OBJECT);
2063 acpi_scan_lock_release();
2064 }
2065
2066 kfree(tew);
2067}
2068
2069void acpi_scan_table_handler(u32 event, void *table, void *context)
2070{
2071 struct acpi_table_events_work *tew;
2072
2073 if (!acpi_scan_initialized)
2074 return;
2075
2076 if (event != ACPI_TABLE_EVENT_LOAD)
2077 return;
2078
2079 tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2080 if (!tew)
2081 return;
2082
2083 INIT_WORK(&tew->work, acpi_table_events_fn);
2084 tew->table = table;
2085 tew->event = event;
2086
2087 schedule_work(&tew->work);
2088}
2089
2090int acpi_reconfig_notifier_register(struct notifier_block *nb)
2091{
2092 return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2093}
2094EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2095
2096int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2097{
2098 return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2099}
2100EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);
2101