GNU Linux-libre 4.14.313-gnu1
[releases.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/acpi_iort.h>
11 #include <linux/signal.h>
12 #include <linux/kthread.h>
13 #include <linux/dmi.h>
14 #include <linux/nls.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/platform_data/x86/apple.h>
17
18 #include <asm/pgtable.h>
19
20 #include "internal.h"
21
22 #define _COMPONENT              ACPI_BUS_COMPONENT
23 ACPI_MODULE_NAME("scan");
24 extern struct acpi_device *acpi_root;
25
26 #define ACPI_BUS_CLASS                  "system_bus"
27 #define ACPI_BUS_HID                    "LNXSYBUS"
28 #define ACPI_BUS_DEVICE_NAME            "System Bus"
29
30 #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
31
32 #define INVALID_ACPI_HANDLE     ((acpi_handle)empty_zero_page)
33
34 static const char *dummy_hid = "device";
35
36 static LIST_HEAD(acpi_dep_list);
37 static DEFINE_MUTEX(acpi_dep_list_lock);
38 LIST_HEAD(acpi_bus_id_list);
39 static DEFINE_MUTEX(acpi_scan_lock);
40 static LIST_HEAD(acpi_scan_handlers_list);
41 DEFINE_MUTEX(acpi_device_lock);
42 LIST_HEAD(acpi_wakeup_device_list);
43 static DEFINE_MUTEX(acpi_hp_context_lock);
44
45 /*
46  * The UART device described by the SPCR table is the only object which needs
47  * special-casing. Everything else is covered by ACPI namespace paths in STAO
48  * table.
49  */
50 static u64 spcr_uart_addr;
51
52 struct acpi_dep_data {
53         struct list_head node;
54         acpi_handle master;
55         acpi_handle slave;
56 };
57
58 void acpi_scan_lock_acquire(void)
59 {
60         mutex_lock(&acpi_scan_lock);
61 }
62 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
63
64 void acpi_scan_lock_release(void)
65 {
66         mutex_unlock(&acpi_scan_lock);
67 }
68 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
69
70 void acpi_lock_hp_context(void)
71 {
72         mutex_lock(&acpi_hp_context_lock);
73 }
74
75 void acpi_unlock_hp_context(void)
76 {
77         mutex_unlock(&acpi_hp_context_lock);
78 }
79
80 void acpi_initialize_hp_context(struct acpi_device *adev,
81                                 struct acpi_hotplug_context *hp,
82                                 int (*notify)(struct acpi_device *, u32),
83                                 void (*uevent)(struct acpi_device *, u32))
84 {
85         acpi_lock_hp_context();
86         hp->notify = notify;
87         hp->uevent = uevent;
88         acpi_set_hp_context(adev, hp);
89         acpi_unlock_hp_context();
90 }
91 EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
92
93 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
94 {
95         if (!handler)
96                 return -EINVAL;
97
98         list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
99         return 0;
100 }
101
102 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
103                                        const char *hotplug_profile_name)
104 {
105         int error;
106
107         error = acpi_scan_add_handler(handler);
108         if (error)
109                 return error;
110
111         acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
112         return 0;
113 }
114
115 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
116 {
117         struct acpi_device_physical_node *pn;
118         bool offline = true;
119
120         /*
121          * acpi_container_offline() calls this for all of the container's
122          * children under the container's physical_node_lock lock.
123          */
124         mutex_lock_nested(&adev->physical_node_lock, SINGLE_DEPTH_NESTING);
125
126         list_for_each_entry(pn, &adev->physical_node_list, node)
127                 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
128                         if (uevent)
129                                 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
130
131                         offline = false;
132                         break;
133                 }
134
135         mutex_unlock(&adev->physical_node_lock);
136         return offline;
137 }
138
139 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
140                                     void **ret_p)
141 {
142         struct acpi_device *device = NULL;
143         struct acpi_device_physical_node *pn;
144         bool second_pass = (bool)data;
145         acpi_status status = AE_OK;
146
147         if (acpi_bus_get_device(handle, &device))
148                 return AE_OK;
149
150         if (device->handler && !device->handler->hotplug.enabled) {
151                 *ret_p = &device->dev;
152                 return AE_SUPPORT;
153         }
154
155         mutex_lock(&device->physical_node_lock);
156
157         list_for_each_entry(pn, &device->physical_node_list, node) {
158                 int ret;
159
160                 if (second_pass) {
161                         /* Skip devices offlined by the first pass. */
162                         if (pn->put_online)
163                                 continue;
164                 } else {
165                         pn->put_online = false;
166                 }
167                 ret = device_offline(pn->dev);
168                 if (ret >= 0) {
169                         pn->put_online = !ret;
170                 } else {
171                         *ret_p = pn->dev;
172                         if (second_pass) {
173                                 status = AE_ERROR;
174                                 break;
175                         }
176                 }
177         }
178
179         mutex_unlock(&device->physical_node_lock);
180
181         return status;
182 }
183
184 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
185                                    void **ret_p)
186 {
187         struct acpi_device *device = NULL;
188         struct acpi_device_physical_node *pn;
189
190         if (acpi_bus_get_device(handle, &device))
191                 return AE_OK;
192
193         mutex_lock(&device->physical_node_lock);
194
195         list_for_each_entry(pn, &device->physical_node_list, node)
196                 if (pn->put_online) {
197                         device_online(pn->dev);
198                         pn->put_online = false;
199                 }
200
201         mutex_unlock(&device->physical_node_lock);
202
203         return AE_OK;
204 }
205
206 static int acpi_scan_try_to_offline(struct acpi_device *device)
207 {
208         acpi_handle handle = device->handle;
209         struct device *errdev = NULL;
210         acpi_status status;
211
212         /*
213          * Carry out two passes here and ignore errors in the first pass,
214          * because if the devices in question are memory blocks and
215          * CONFIG_MEMCG is set, one of the blocks may hold data structures
216          * that the other blocks depend on, but it is not known in advance which
217          * block holds them.
218          *
219          * If the first pass is successful, the second one isn't needed, though.
220          */
221         status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
222                                      NULL, acpi_bus_offline, (void *)false,
223                                      (void **)&errdev);
224         if (status == AE_SUPPORT) {
225                 dev_warn(errdev, "Offline disabled.\n");
226                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
227                                     acpi_bus_online, NULL, NULL, NULL);
228                 return -EPERM;
229         }
230         acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
231         if (errdev) {
232                 errdev = NULL;
233                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
234                                     NULL, acpi_bus_offline, (void *)true,
235                                     (void **)&errdev);
236                 if (!errdev)
237                         acpi_bus_offline(handle, 0, (void *)true,
238                                          (void **)&errdev);
239
240                 if (errdev) {
241                         dev_warn(errdev, "Offline failed.\n");
242                         acpi_bus_online(handle, 0, NULL, NULL);
243                         acpi_walk_namespace(ACPI_TYPE_ANY, handle,
244                                             ACPI_UINT32_MAX, acpi_bus_online,
245                                             NULL, NULL, NULL);
246                         return -EBUSY;
247                 }
248         }
249         return 0;
250 }
251
252 static int acpi_scan_hot_remove(struct acpi_device *device)
253 {
254         acpi_handle handle = device->handle;
255         unsigned long long sta;
256         acpi_status status;
257
258         if (device->handler && device->handler->hotplug.demand_offline) {
259                 if (!acpi_scan_is_offline(device, true))
260                         return -EBUSY;
261         } else {
262                 int error = acpi_scan_try_to_offline(device);
263                 if (error)
264                         return error;
265         }
266
267         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
268                 "Hot-removing device %s...\n", dev_name(&device->dev)));
269
270         acpi_bus_trim(device);
271
272         acpi_evaluate_lck(handle, 0);
273         /*
274          * TBD: _EJD support.
275          */
276         status = acpi_evaluate_ej0(handle);
277         if (status == AE_NOT_FOUND)
278                 return -ENODEV;
279         else if (ACPI_FAILURE(status))
280                 return -EIO;
281
282         /*
283          * Verify if eject was indeed successful.  If not, log an error
284          * message.  No need to call _OST since _EJ0 call was made OK.
285          */
286         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
287         if (ACPI_FAILURE(status)) {
288                 acpi_handle_warn(handle,
289                         "Status check after eject failed (0x%x)\n", status);
290         } else if (sta & ACPI_STA_DEVICE_ENABLED) {
291                 acpi_handle_warn(handle,
292                         "Eject incomplete - status 0x%llx\n", sta);
293         }
294
295         return 0;
296 }
297
298 static int acpi_scan_device_not_present(struct acpi_device *adev)
299 {
300         if (!acpi_device_enumerated(adev)) {
301                 dev_warn(&adev->dev, "Still not present\n");
302                 return -EALREADY;
303         }
304         acpi_bus_trim(adev);
305         return 0;
306 }
307
308 static int acpi_scan_device_check(struct acpi_device *adev)
309 {
310         int error;
311
312         acpi_bus_get_status(adev);
313         if (adev->status.present || adev->status.functional) {
314                 /*
315                  * This function is only called for device objects for which
316                  * matching scan handlers exist.  The only situation in which
317                  * the scan handler is not attached to this device object yet
318                  * is when the device has just appeared (either it wasn't
319                  * present at all before or it was removed and then added
320                  * again).
321                  */
322                 if (adev->handler) {
323                         dev_warn(&adev->dev, "Already enumerated\n");
324                         return -EALREADY;
325                 }
326                 error = acpi_bus_scan(adev->handle);
327                 if (error) {
328                         dev_warn(&adev->dev, "Namespace scan failure\n");
329                         return error;
330                 }
331                 if (!adev->handler) {
332                         dev_warn(&adev->dev, "Enumeration failure\n");
333                         error = -ENODEV;
334                 }
335         } else {
336                 error = acpi_scan_device_not_present(adev);
337         }
338         return error;
339 }
340
341 static int acpi_scan_bus_check(struct acpi_device *adev)
342 {
343         struct acpi_scan_handler *handler = adev->handler;
344         struct acpi_device *child;
345         int error;
346
347         acpi_bus_get_status(adev);
348         if (!(adev->status.present || adev->status.functional)) {
349                 acpi_scan_device_not_present(adev);
350                 return 0;
351         }
352         if (handler && handler->hotplug.scan_dependent)
353                 return handler->hotplug.scan_dependent(adev);
354
355         error = acpi_bus_scan(adev->handle);
356         if (error) {
357                 dev_warn(&adev->dev, "Namespace scan failure\n");
358                 return error;
359         }
360         list_for_each_entry(child, &adev->children, node) {
361                 error = acpi_scan_bus_check(child);
362                 if (error)
363                         return error;
364         }
365         return 0;
366 }
367
368 static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
369 {
370         switch (type) {
371         case ACPI_NOTIFY_BUS_CHECK:
372                 return acpi_scan_bus_check(adev);
373         case ACPI_NOTIFY_DEVICE_CHECK:
374                 return acpi_scan_device_check(adev);
375         case ACPI_NOTIFY_EJECT_REQUEST:
376         case ACPI_OST_EC_OSPM_EJECT:
377                 if (adev->handler && !adev->handler->hotplug.enabled) {
378                         dev_info(&adev->dev, "Eject disabled\n");
379                         return -EPERM;
380                 }
381                 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
382                                   ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
383                 return acpi_scan_hot_remove(adev);
384         }
385         return -EINVAL;
386 }
387
388 void acpi_device_hotplug(struct acpi_device *adev, u32 src)
389 {
390         u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
391         int error = -ENODEV;
392
393         lock_device_hotplug();
394         mutex_lock(&acpi_scan_lock);
395
396         /*
397          * The device object's ACPI handle cannot become invalid as long as we
398          * are holding acpi_scan_lock, but it might have become invalid before
399          * that lock was acquired.
400          */
401         if (adev->handle == INVALID_ACPI_HANDLE)
402                 goto err_out;
403
404         if (adev->flags.is_dock_station) {
405                 error = dock_notify(adev, src);
406         } else if (adev->flags.hotplug_notify) {
407                 error = acpi_generic_hotplug_event(adev, src);
408         } else {
409                 int (*notify)(struct acpi_device *, u32);
410
411                 acpi_lock_hp_context();
412                 notify = adev->hp ? adev->hp->notify : NULL;
413                 acpi_unlock_hp_context();
414                 /*
415                  * There may be additional notify handlers for device objects
416                  * without the .event() callback, so ignore them here.
417                  */
418                 if (notify)
419                         error = notify(adev, src);
420                 else
421                         goto out;
422         }
423         switch (error) {
424         case 0:
425                 ost_code = ACPI_OST_SC_SUCCESS;
426                 break;
427         case -EPERM:
428                 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
429                 break;
430         case -EBUSY:
431                 ost_code = ACPI_OST_SC_DEVICE_BUSY;
432                 break;
433         default:
434                 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
435                 break;
436         }
437
438  err_out:
439         acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
440
441  out:
442         acpi_bus_put_acpi_device(adev);
443         mutex_unlock(&acpi_scan_lock);
444         unlock_device_hotplug();
445 }
446
447 static void acpi_free_power_resources_lists(struct acpi_device *device)
448 {
449         int i;
450
451         if (device->wakeup.flags.valid)
452                 acpi_power_resources_list_free(&device->wakeup.resources);
453
454         if (!device->power.flags.power_resources)
455                 return;
456
457         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
458                 struct acpi_device_power_state *ps = &device->power.states[i];
459                 acpi_power_resources_list_free(&ps->resources);
460         }
461 }
462
463 static void acpi_device_release(struct device *dev)
464 {
465         struct acpi_device *acpi_dev = to_acpi_device(dev);
466
467         acpi_free_properties(acpi_dev);
468         acpi_free_pnp_ids(&acpi_dev->pnp);
469         acpi_free_power_resources_lists(acpi_dev);
470         kfree(acpi_dev);
471 }
472
473 static void acpi_device_del(struct acpi_device *device)
474 {
475         struct acpi_device_bus_id *acpi_device_bus_id;
476
477         mutex_lock(&acpi_device_lock);
478         if (device->parent)
479                 list_del(&device->node);
480
481         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
482                 if (!strcmp(acpi_device_bus_id->bus_id,
483                             acpi_device_hid(device))) {
484                         ida_simple_remove(&acpi_device_bus_id->instance_ida, device->pnp.instance_no);
485                         if (ida_is_empty(&acpi_device_bus_id->instance_ida)) {
486                                 list_del(&acpi_device_bus_id->node);
487                                 kfree_const(acpi_device_bus_id->bus_id);
488                                 kfree(acpi_device_bus_id);
489                         }
490                         break;
491                 }
492
493         list_del(&device->wakeup_list);
494         mutex_unlock(&acpi_device_lock);
495
496         acpi_power_add_remove_device(device, false);
497         acpi_device_remove_files(device);
498         if (device->remove)
499                 device->remove(device);
500
501         device_del(&device->dev);
502 }
503
504 static BLOCKING_NOTIFIER_HEAD(acpi_reconfig_chain);
505
506 static LIST_HEAD(acpi_device_del_list);
507 static DEFINE_MUTEX(acpi_device_del_lock);
508
509 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
510 {
511         for (;;) {
512                 struct acpi_device *adev;
513
514                 mutex_lock(&acpi_device_del_lock);
515
516                 if (list_empty(&acpi_device_del_list)) {
517                         mutex_unlock(&acpi_device_del_lock);
518                         break;
519                 }
520                 adev = list_first_entry(&acpi_device_del_list,
521                                         struct acpi_device, del_list);
522                 list_del(&adev->del_list);
523
524                 mutex_unlock(&acpi_device_del_lock);
525
526                 blocking_notifier_call_chain(&acpi_reconfig_chain,
527                                              ACPI_RECONFIG_DEVICE_REMOVE, adev);
528
529                 acpi_device_del(adev);
530                 /*
531                  * Drop references to all power resources that might have been
532                  * used by the device.
533                  */
534                 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
535                 put_device(&adev->dev);
536         }
537 }
538
539 /**
540  * acpi_scan_drop_device - Drop an ACPI device object.
541  * @handle: Handle of an ACPI namespace node, not used.
542  * @context: Address of the ACPI device object to drop.
543  *
544  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
545  * namespace node the device object pointed to by @context is attached to.
546  *
547  * The unregistration is carried out asynchronously to avoid running
548  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
549  * ensure the correct ordering (the device objects must be unregistered in the
550  * same order in which the corresponding namespace nodes are deleted).
551  */
552 static void acpi_scan_drop_device(acpi_handle handle, void *context)
553 {
554         static DECLARE_WORK(work, acpi_device_del_work_fn);
555         struct acpi_device *adev = context;
556
557         mutex_lock(&acpi_device_del_lock);
558
559         /*
560          * Use the ACPI hotplug workqueue which is ordered, so this work item
561          * won't run after any hotplug work items submitted subsequently.  That
562          * prevents attempts to register device objects identical to those being
563          * deleted from happening concurrently (such attempts result from
564          * hotplug events handled via the ACPI hotplug workqueue).  It also will
565          * run after all of the work items submitted previosuly, which helps
566          * those work items to ensure that they are not accessing stale device
567          * objects.
568          */
569         if (list_empty(&acpi_device_del_list))
570                 acpi_queue_hotplug_work(&work);
571
572         list_add_tail(&adev->del_list, &acpi_device_del_list);
573         /* Make acpi_ns_validate_handle() return NULL for this handle. */
574         adev->handle = INVALID_ACPI_HANDLE;
575
576         mutex_unlock(&acpi_device_del_lock);
577 }
578
579 static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
580                                 void (*callback)(void *))
581 {
582         acpi_status status;
583
584         if (!device)
585                 return -EINVAL;
586
587         *device = NULL;
588
589         status = acpi_get_data_full(handle, acpi_scan_drop_device,
590                                     (void **)device, callback);
591         if (ACPI_FAILURE(status) || !*device) {
592                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
593                                   handle));
594                 return -ENODEV;
595         }
596         return 0;
597 }
598
599 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
600 {
601         return acpi_get_device_data(handle, device, NULL);
602 }
603 EXPORT_SYMBOL(acpi_bus_get_device);
604
605 static void get_acpi_device(void *dev)
606 {
607         if (dev)
608                 get_device(&((struct acpi_device *)dev)->dev);
609 }
610
611 struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
612 {
613         struct acpi_device *adev = NULL;
614
615         acpi_get_device_data(handle, &adev, get_acpi_device);
616         return adev;
617 }
618
619 void acpi_bus_put_acpi_device(struct acpi_device *adev)
620 {
621         put_device(&adev->dev);
622 }
623
624 static struct acpi_device_bus_id *acpi_device_bus_id_match(const char *dev_id)
625 {
626         struct acpi_device_bus_id *acpi_device_bus_id;
627
628         /* Find suitable bus_id and instance number in acpi_bus_id_list. */
629         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
630                 if (!strcmp(acpi_device_bus_id->bus_id, dev_id))
631                         return acpi_device_bus_id;
632         }
633         return NULL;
634 }
635
636 static int acpi_device_set_name(struct acpi_device *device,
637                                 struct acpi_device_bus_id *acpi_device_bus_id)
638 {
639         struct ida *instance_ida = &acpi_device_bus_id->instance_ida;
640         int result;
641
642         result = ida_simple_get(instance_ida, 0, ACPI_MAX_DEVICE_INSTANCES, GFP_KERNEL);
643         if (result < 0)
644                 return result;
645
646         device->pnp.instance_no = result;
647         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, result);
648         return 0;
649 }
650
651 int acpi_device_add(struct acpi_device *device,
652                     void (*release)(struct device *))
653 {
654         struct acpi_device_bus_id *acpi_device_bus_id;
655         int result;
656
657         if (device->handle) {
658                 acpi_status status;
659
660                 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
661                                           device);
662                 if (ACPI_FAILURE(status)) {
663                         acpi_handle_err(device->handle,
664                                         "Unable to attach device data\n");
665                         return -ENODEV;
666                 }
667         }
668
669         /*
670          * Linkage
671          * -------
672          * Link this device to its parent and siblings.
673          */
674         INIT_LIST_HEAD(&device->children);
675         INIT_LIST_HEAD(&device->node);
676         INIT_LIST_HEAD(&device->wakeup_list);
677         INIT_LIST_HEAD(&device->physical_node_list);
678         INIT_LIST_HEAD(&device->del_list);
679         mutex_init(&device->physical_node_lock);
680
681         mutex_lock(&acpi_device_lock);
682
683         acpi_device_bus_id = acpi_device_bus_id_match(acpi_device_hid(device));
684         if (acpi_device_bus_id) {
685                 result = acpi_device_set_name(device, acpi_device_bus_id);
686                 if (result)
687                         goto err_unlock;
688         } else {
689                 acpi_device_bus_id = kzalloc(sizeof(*acpi_device_bus_id),
690                                              GFP_KERNEL);
691                 if (!acpi_device_bus_id) {
692                         result = -ENOMEM;
693                         goto err_unlock;
694                 }
695                 acpi_device_bus_id->bus_id =
696                         kstrdup_const(acpi_device_hid(device), GFP_KERNEL);
697                 if (!acpi_device_bus_id->bus_id) {
698                         kfree(acpi_device_bus_id);
699                         result = -ENOMEM;
700                         goto err_unlock;
701                 }
702
703                 ida_init(&acpi_device_bus_id->instance_ida);
704
705                 result = acpi_device_set_name(device, acpi_device_bus_id);
706                 if (result) {
707                         kfree_const(acpi_device_bus_id->bus_id);
708                         kfree(acpi_device_bus_id);
709                         goto err_unlock;
710                 }
711
712                 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
713         }
714
715         if (device->parent)
716                 list_add_tail(&device->node, &device->parent->children);
717
718         if (device->wakeup.flags.valid)
719                 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
720         mutex_unlock(&acpi_device_lock);
721
722         if (device->parent)
723                 device->dev.parent = &device->parent->dev;
724         device->dev.bus = &acpi_bus_type;
725         device->dev.release = release;
726         result = device_add(&device->dev);
727         if (result) {
728                 dev_err(&device->dev, "Error registering device\n");
729                 goto err;
730         }
731
732         result = acpi_device_setup_files(device);
733         if (result)
734                 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
735                        dev_name(&device->dev));
736
737         return 0;
738
739  err:
740         mutex_lock(&acpi_device_lock);
741         if (device->parent)
742                 list_del(&device->node);
743         list_del(&device->wakeup_list);
744
745  err_unlock:
746         mutex_unlock(&acpi_device_lock);
747
748         acpi_detach_data(device->handle, acpi_scan_drop_device);
749         return result;
750 }
751
752 /* --------------------------------------------------------------------------
753                                  Device Enumeration
754    -------------------------------------------------------------------------- */
755 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
756 {
757         struct acpi_device *device = NULL;
758         acpi_status status;
759
760         /*
761          * Fixed hardware devices do not appear in the namespace and do not
762          * have handles, but we fabricate acpi_devices for them, so we have
763          * to deal with them specially.
764          */
765         if (!handle)
766                 return acpi_root;
767
768         do {
769                 status = acpi_get_parent(handle, &handle);
770                 if (ACPI_FAILURE(status))
771                         return status == AE_NULL_ENTRY ? NULL : acpi_root;
772         } while (acpi_bus_get_device(handle, &device));
773         return device;
774 }
775
776 acpi_status
777 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
778 {
779         acpi_status status;
780         acpi_handle tmp;
781         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
782         union acpi_object *obj;
783
784         status = acpi_get_handle(handle, "_EJD", &tmp);
785         if (ACPI_FAILURE(status))
786                 return status;
787
788         status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
789         if (ACPI_SUCCESS(status)) {
790                 obj = buffer.pointer;
791                 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
792                                          ejd);
793                 kfree(buffer.pointer);
794         }
795         return status;
796 }
797 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
798
799 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
800                                         struct acpi_device_wakeup *wakeup)
801 {
802         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
803         union acpi_object *package = NULL;
804         union acpi_object *element = NULL;
805         acpi_status status;
806         int err = -ENODATA;
807
808         if (!wakeup)
809                 return -EINVAL;
810
811         INIT_LIST_HEAD(&wakeup->resources);
812
813         /* _PRW */
814         status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
815         if (ACPI_FAILURE(status)) {
816                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
817                 return err;
818         }
819
820         package = (union acpi_object *)buffer.pointer;
821
822         if (!package || package->package.count < 2)
823                 goto out;
824
825         element = &(package->package.elements[0]);
826         if (!element)
827                 goto out;
828
829         if (element->type == ACPI_TYPE_PACKAGE) {
830                 if ((element->package.count < 2) ||
831                     (element->package.elements[0].type !=
832                      ACPI_TYPE_LOCAL_REFERENCE)
833                     || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
834                         goto out;
835
836                 wakeup->gpe_device =
837                     element->package.elements[0].reference.handle;
838                 wakeup->gpe_number =
839                     (u32) element->package.elements[1].integer.value;
840         } else if (element->type == ACPI_TYPE_INTEGER) {
841                 wakeup->gpe_device = NULL;
842                 wakeup->gpe_number = element->integer.value;
843         } else {
844                 goto out;
845         }
846
847         element = &(package->package.elements[1]);
848         if (element->type != ACPI_TYPE_INTEGER)
849                 goto out;
850
851         wakeup->sleep_state = element->integer.value;
852
853         err = acpi_extract_power_resources(package, 2, &wakeup->resources);
854         if (err)
855                 goto out;
856
857         if (!list_empty(&wakeup->resources)) {
858                 int sleep_state;
859
860                 err = acpi_power_wakeup_list_init(&wakeup->resources,
861                                                   &sleep_state);
862                 if (err) {
863                         acpi_handle_warn(handle, "Retrieving current states "
864                                          "of wakeup power resources failed\n");
865                         acpi_power_resources_list_free(&wakeup->resources);
866                         goto out;
867                 }
868                 if (sleep_state < wakeup->sleep_state) {
869                         acpi_handle_warn(handle, "Overriding _PRW sleep state "
870                                          "(S%d) by S%d from power resources\n",
871                                          (int)wakeup->sleep_state, sleep_state);
872                         wakeup->sleep_state = sleep_state;
873                 }
874         }
875
876  out:
877         kfree(buffer.pointer);
878         return err;
879 }
880
881 static bool acpi_wakeup_gpe_init(struct acpi_device *device)
882 {
883         static const struct acpi_device_id button_device_ids[] = {
884                 {"PNP0C0C", 0},
885                 {"PNP0C0D", 0},
886                 {"PNP0C0E", 0},
887                 {"", 0},
888         };
889         struct acpi_device_wakeup *wakeup = &device->wakeup;
890         acpi_status status;
891
892         wakeup->flags.notifier_present = 0;
893
894         /* Power button, Lid switch always enable wakeup */
895         if (!acpi_match_device_ids(device, button_device_ids)) {
896                 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
897                         /* Do not use Lid/sleep button for S5 wakeup */
898                         if (wakeup->sleep_state == ACPI_STATE_S5)
899                                 wakeup->sleep_state = ACPI_STATE_S4;
900                 }
901                 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
902                 device_set_wakeup_capable(&device->dev, true);
903                 return true;
904         }
905
906         status = acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
907                                          wakeup->gpe_number);
908         return ACPI_SUCCESS(status);
909 }
910
911 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
912 {
913         int err;
914
915         /* Presence of _PRW indicates wake capable */
916         if (!acpi_has_method(device->handle, "_PRW"))
917                 return;
918
919         err = acpi_bus_extract_wakeup_device_power_package(device->handle,
920                                                            &device->wakeup);
921         if (err) {
922                 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
923                 return;
924         }
925
926         device->wakeup.flags.valid = acpi_wakeup_gpe_init(device);
927         device->wakeup.prepare_count = 0;
928         /*
929          * Call _PSW/_DSW object to disable its ability to wake the sleeping
930          * system for the ACPI device with the _PRW object.
931          * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
932          * So it is necessary to call _DSW object first. Only when it is not
933          * present will the _PSW object used.
934          */
935         err = acpi_device_sleep_wake(device, 0, 0, 0);
936         if (err)
937                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
938                                 "error in _DSW or _PSW evaluation\n"));
939 }
940
941 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
942 {
943         struct acpi_device_power_state *ps = &device->power.states[state];
944         char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
945         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
946         acpi_status status;
947
948         INIT_LIST_HEAD(&ps->resources);
949
950         /* Evaluate "_PRx" to get referenced power resources */
951         status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
952         if (ACPI_SUCCESS(status)) {
953                 union acpi_object *package = buffer.pointer;
954
955                 if (buffer.length && package
956                     && package->type == ACPI_TYPE_PACKAGE
957                     && package->package.count)
958                         acpi_extract_power_resources(package, 0, &ps->resources);
959
960                 ACPI_FREE(buffer.pointer);
961         }
962
963         /* Evaluate "_PSx" to see if we can do explicit sets */
964         pathname[2] = 'S';
965         if (acpi_has_method(device->handle, pathname))
966                 ps->flags.explicit_set = 1;
967
968         /* State is valid if there are means to put the device into it. */
969         if (!list_empty(&ps->resources) || ps->flags.explicit_set)
970                 ps->flags.valid = 1;
971
972         ps->power = -1;         /* Unknown - driver assigned */
973         ps->latency = -1;       /* Unknown - driver assigned */
974 }
975
976 static void acpi_bus_get_power_flags(struct acpi_device *device)
977 {
978         u32 i;
979
980         /* Presence of _PS0|_PR0 indicates 'power manageable' */
981         if (!acpi_has_method(device->handle, "_PS0") &&
982             !acpi_has_method(device->handle, "_PR0"))
983                 return;
984
985         device->flags.power_manageable = 1;
986
987         /*
988          * Power Management Flags
989          */
990         if (acpi_has_method(device->handle, "_PSC"))
991                 device->power.flags.explicit_get = 1;
992
993         if (acpi_has_method(device->handle, "_IRC"))
994                 device->power.flags.inrush_current = 1;
995
996         if (acpi_has_method(device->handle, "_DSW"))
997                 device->power.flags.dsw_present = 1;
998
999         /*
1000          * Enumerate supported power management states
1001          */
1002         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1003                 acpi_bus_init_power_state(device, i);
1004
1005         INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1006
1007         /* Set the defaults for D0 and D3hot (always supported). */
1008         device->power.states[ACPI_STATE_D0].flags.valid = 1;
1009         device->power.states[ACPI_STATE_D0].power = 100;
1010         device->power.states[ACPI_STATE_D3_HOT].flags.valid = 1;
1011
1012         /*
1013          * Use power resources only if the D0 list of them is populated, because
1014          * some platforms may provide _PR3 only to indicate D3cold support and
1015          * in those cases the power resources list returned by it may be bogus.
1016          */
1017         if (!list_empty(&device->power.states[ACPI_STATE_D0].resources)) {
1018                 device->power.flags.power_resources = 1;
1019                 /*
1020                  * D3cold is supported if the D3hot list of power resources is
1021                  * not empty.
1022                  */
1023                 if (!list_empty(&device->power.states[ACPI_STATE_D3_HOT].resources))
1024                         device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1025         }
1026
1027         if (acpi_bus_init_power(device))
1028                 device->flags.power_manageable = 0;
1029 }
1030
1031 static void acpi_bus_get_flags(struct acpi_device *device)
1032 {
1033         /* Presence of _STA indicates 'dynamic_status' */
1034         if (acpi_has_method(device->handle, "_STA"))
1035                 device->flags.dynamic_status = 1;
1036
1037         /* Presence of _RMV indicates 'removable' */
1038         if (acpi_has_method(device->handle, "_RMV"))
1039                 device->flags.removable = 1;
1040
1041         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1042         if (acpi_has_method(device->handle, "_EJD") ||
1043             acpi_has_method(device->handle, "_EJ0"))
1044                 device->flags.ejectable = 1;
1045 }
1046
1047 static void acpi_device_get_busid(struct acpi_device *device)
1048 {
1049         char bus_id[5] = { '?', 0 };
1050         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1051         int i = 0;
1052
1053         /*
1054          * Bus ID
1055          * ------
1056          * The device's Bus ID is simply the object name.
1057          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1058          */
1059         if (ACPI_IS_ROOT_DEVICE(device)) {
1060                 strcpy(device->pnp.bus_id, "ACPI");
1061                 return;
1062         }
1063
1064         switch (device->device_type) {
1065         case ACPI_BUS_TYPE_POWER_BUTTON:
1066                 strcpy(device->pnp.bus_id, "PWRF");
1067                 break;
1068         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1069                 strcpy(device->pnp.bus_id, "SLPF");
1070                 break;
1071         case ACPI_BUS_TYPE_ECDT_EC:
1072                 strcpy(device->pnp.bus_id, "ECDT");
1073                 break;
1074         default:
1075                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1076                 /* Clean up trailing underscores (if any) */
1077                 for (i = 3; i > 1; i--) {
1078                         if (bus_id[i] == '_')
1079                                 bus_id[i] = '\0';
1080                         else
1081                                 break;
1082                 }
1083                 strcpy(device->pnp.bus_id, bus_id);
1084                 break;
1085         }
1086 }
1087
1088 /*
1089  * acpi_ata_match - see if an acpi object is an ATA device
1090  *
1091  * If an acpi object has one of the ACPI ATA methods defined,
1092  * then we can safely call it an ATA device.
1093  */
1094 bool acpi_ata_match(acpi_handle handle)
1095 {
1096         return acpi_has_method(handle, "_GTF") ||
1097                acpi_has_method(handle, "_GTM") ||
1098                acpi_has_method(handle, "_STM") ||
1099                acpi_has_method(handle, "_SDD");
1100 }
1101
1102 /*
1103  * acpi_bay_match - see if an acpi object is an ejectable driver bay
1104  *
1105  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1106  * then we can safely call it an ejectable drive bay
1107  */
1108 bool acpi_bay_match(acpi_handle handle)
1109 {
1110         acpi_handle phandle;
1111
1112         if (!acpi_has_method(handle, "_EJ0"))
1113                 return false;
1114         if (acpi_ata_match(handle))
1115                 return true;
1116         if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1117                 return false;
1118
1119         return acpi_ata_match(phandle);
1120 }
1121
1122 bool acpi_device_is_battery(struct acpi_device *adev)
1123 {
1124         struct acpi_hardware_id *hwid;
1125
1126         list_for_each_entry(hwid, &adev->pnp.ids, list)
1127                 if (!strcmp("PNP0C0A", hwid->id))
1128                         return true;
1129
1130         return false;
1131 }
1132
1133 static bool is_ejectable_bay(struct acpi_device *adev)
1134 {
1135         acpi_handle handle = adev->handle;
1136
1137         if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
1138                 return true;
1139
1140         return acpi_bay_match(handle);
1141 }
1142
1143 /*
1144  * acpi_dock_match - see if an acpi object has a _DCK method
1145  */
1146 bool acpi_dock_match(acpi_handle handle)
1147 {
1148         return acpi_has_method(handle, "_DCK");
1149 }
1150
1151 static acpi_status
1152 acpi_backlight_cap_match(acpi_handle handle, u32 level, void *context,
1153                           void **return_value)
1154 {
1155         long *cap = context;
1156
1157         if (acpi_has_method(handle, "_BCM") &&
1158             acpi_has_method(handle, "_BCL")) {
1159                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found generic backlight "
1160                                   "support\n"));
1161                 *cap |= ACPI_VIDEO_BACKLIGHT;
1162                 /* We have backlight support, no need to scan further */
1163                 return AE_CTRL_TERMINATE;
1164         }
1165         return 0;
1166 }
1167
1168 /* Returns true if the ACPI object is a video device which can be
1169  * handled by video.ko.
1170  * The device will get a Linux specific CID added in scan.c to
1171  * identify the device as an ACPI graphics device
1172  * Be aware that the graphics device may not be physically present
1173  * Use acpi_video_get_capabilities() to detect general ACPI video
1174  * capabilities of present cards
1175  */
1176 long acpi_is_video_device(acpi_handle handle)
1177 {
1178         long video_caps = 0;
1179
1180         /* Is this device able to support video switching ? */
1181         if (acpi_has_method(handle, "_DOD") || acpi_has_method(handle, "_DOS"))
1182                 video_caps |= ACPI_VIDEO_OUTPUT_SWITCHING;
1183
1184         /* Is this device able to retrieve a video ROM ? */
1185         if (acpi_has_method(handle, "_ROM"))
1186                 video_caps |= ACPI_VIDEO_ROM_AVAILABLE;
1187
1188         /* Is this device able to configure which video head to be POSTed ? */
1189         if (acpi_has_method(handle, "_VPO") &&
1190             acpi_has_method(handle, "_GPD") &&
1191             acpi_has_method(handle, "_SPD"))
1192                 video_caps |= ACPI_VIDEO_DEVICE_POSTING;
1193
1194         /* Only check for backlight functionality if one of the above hit. */
1195         if (video_caps)
1196                 acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
1197                                     ACPI_UINT32_MAX, acpi_backlight_cap_match, NULL,
1198                                     &video_caps, NULL);
1199
1200         return video_caps;
1201 }
1202 EXPORT_SYMBOL(acpi_is_video_device);
1203
1204 const char *acpi_device_hid(struct acpi_device *device)
1205 {
1206         struct acpi_hardware_id *hid;
1207
1208         if (list_empty(&device->pnp.ids))
1209                 return dummy_hid;
1210
1211         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1212         return hid->id;
1213 }
1214 EXPORT_SYMBOL(acpi_device_hid);
1215
1216 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1217 {
1218         struct acpi_hardware_id *id;
1219
1220         id = kmalloc(sizeof(*id), GFP_KERNEL);
1221         if (!id)
1222                 return;
1223
1224         id->id = kstrdup_const(dev_id, GFP_KERNEL);
1225         if (!id->id) {
1226                 kfree(id);
1227                 return;
1228         }
1229
1230         list_add_tail(&id->list, &pnp->ids);
1231         pnp->type.hardware_id = 1;
1232 }
1233
1234 /*
1235  * Old IBM workstations have a DSDT bug wherein the SMBus object
1236  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1237  * prefix.  Work around this.
1238  */
1239 static bool acpi_ibm_smbus_match(acpi_handle handle)
1240 {
1241         char node_name[ACPI_PATH_SEGMENT_LENGTH];
1242         struct acpi_buffer path = { sizeof(node_name), node_name };
1243
1244         if (!dmi_name_in_vendors("IBM"))
1245                 return false;
1246
1247         /* Look for SMBS object */
1248         if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1249             strcmp("SMBS", path.pointer))
1250                 return false;
1251
1252         /* Does it have the necessary (but misnamed) methods? */
1253         if (acpi_has_method(handle, "SBI") &&
1254             acpi_has_method(handle, "SBR") &&
1255             acpi_has_method(handle, "SBW"))
1256                 return true;
1257
1258         return false;
1259 }
1260
1261 static bool acpi_object_is_system_bus(acpi_handle handle)
1262 {
1263         acpi_handle tmp;
1264
1265         if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1266             tmp == handle)
1267                 return true;
1268         if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1269             tmp == handle)
1270                 return true;
1271
1272         return false;
1273 }
1274
1275 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1276                                 int device_type)
1277 {
1278         acpi_status status;
1279         struct acpi_device_info *info;
1280         struct acpi_pnp_device_id_list *cid_list;
1281         int i;
1282
1283         switch (device_type) {
1284         case ACPI_BUS_TYPE_DEVICE:
1285                 if (handle == ACPI_ROOT_OBJECT) {
1286                         acpi_add_id(pnp, ACPI_SYSTEM_HID);
1287                         break;
1288                 }
1289
1290                 status = acpi_get_object_info(handle, &info);
1291                 if (ACPI_FAILURE(status)) {
1292                         pr_err(PREFIX "%s: Error reading device info\n",
1293                                         __func__);
1294                         return;
1295                 }
1296
1297                 if (info->valid & ACPI_VALID_HID) {
1298                         acpi_add_id(pnp, info->hardware_id.string);
1299                         pnp->type.platform_id = 1;
1300                 }
1301                 if (info->valid & ACPI_VALID_CID) {
1302                         cid_list = &info->compatible_id_list;
1303                         for (i = 0; i < cid_list->count; i++)
1304                                 acpi_add_id(pnp, cid_list->ids[i].string);
1305                 }
1306                 if (info->valid & ACPI_VALID_ADR) {
1307                         pnp->bus_address = info->address;
1308                         pnp->type.bus_address = 1;
1309                 }
1310                 if (info->valid & ACPI_VALID_UID)
1311                         pnp->unique_id = kstrdup(info->unique_id.string,
1312                                                         GFP_KERNEL);
1313                 if (info->valid & ACPI_VALID_CLS)
1314                         acpi_add_id(pnp, info->class_code.string);
1315
1316                 kfree(info);
1317
1318                 /*
1319                  * Some devices don't reliably have _HIDs & _CIDs, so add
1320                  * synthetic HIDs to make sure drivers can find them.
1321                  */
1322                 if (acpi_is_video_device(handle))
1323                         acpi_add_id(pnp, ACPI_VIDEO_HID);
1324                 else if (acpi_bay_match(handle))
1325                         acpi_add_id(pnp, ACPI_BAY_HID);
1326                 else if (acpi_dock_match(handle))
1327                         acpi_add_id(pnp, ACPI_DOCK_HID);
1328                 else if (acpi_ibm_smbus_match(handle))
1329                         acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1330                 else if (list_empty(&pnp->ids) &&
1331                          acpi_object_is_system_bus(handle)) {
1332                         /* \_SB, \_TZ, LNXSYBUS */
1333                         acpi_add_id(pnp, ACPI_BUS_HID);
1334                         strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1335                         strcpy(pnp->device_class, ACPI_BUS_CLASS);
1336                 }
1337
1338                 break;
1339         case ACPI_BUS_TYPE_POWER:
1340                 acpi_add_id(pnp, ACPI_POWER_HID);
1341                 break;
1342         case ACPI_BUS_TYPE_PROCESSOR:
1343                 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1344                 break;
1345         case ACPI_BUS_TYPE_THERMAL:
1346                 acpi_add_id(pnp, ACPI_THERMAL_HID);
1347                 break;
1348         case ACPI_BUS_TYPE_POWER_BUTTON:
1349                 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1350                 break;
1351         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1352                 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1353                 break;
1354         case ACPI_BUS_TYPE_ECDT_EC:
1355                 acpi_add_id(pnp, ACPI_ECDT_HID);
1356                 break;
1357         }
1358 }
1359
1360 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1361 {
1362         struct acpi_hardware_id *id, *tmp;
1363
1364         list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1365                 kfree_const(id->id);
1366                 kfree(id);
1367         }
1368         kfree(pnp->unique_id);
1369 }
1370
1371 /**
1372  * acpi_dma_supported - Check DMA support for the specified device.
1373  * @adev: The pointer to acpi device
1374  *
1375  * Return false if DMA is not supported. Otherwise, return true
1376  */
1377 bool acpi_dma_supported(struct acpi_device *adev)
1378 {
1379         if (!adev)
1380                 return false;
1381
1382         if (adev->flags.cca_seen)
1383                 return true;
1384
1385         /*
1386         * Per ACPI 6.0 sec 6.2.17, assume devices can do cache-coherent
1387         * DMA on "Intel platforms".  Presumably that includes all x86 and
1388         * ia64, and other arches will set CONFIG_ACPI_CCA_REQUIRED=y.
1389         */
1390         if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1391                 return true;
1392
1393         return false;
1394 }
1395
1396 /**
1397  * acpi_get_dma_attr - Check the supported DMA attr for the specified device.
1398  * @adev: The pointer to acpi device
1399  *
1400  * Return enum dev_dma_attr.
1401  */
1402 enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
1403 {
1404         if (!acpi_dma_supported(adev))
1405                 return DEV_DMA_NOT_SUPPORTED;
1406
1407         if (adev->flags.coherent_dma)
1408                 return DEV_DMA_COHERENT;
1409         else
1410                 return DEV_DMA_NON_COHERENT;
1411 }
1412
1413 /**
1414  * acpi_dma_get_range() - Get device DMA parameters.
1415  *
1416  * @dev: device to configure
1417  * @dma_addr: pointer device DMA address result
1418  * @offset: pointer to the DMA offset result
1419  * @size: pointer to DMA range size result
1420  *
1421  * Evaluate DMA regions and return respectively DMA region start, offset
1422  * and size in dma_addr, offset and size on parsing success; it does not
1423  * update the passed in values on failure.
1424  *
1425  * Return 0 on success, < 0 on failure.
1426  */
1427 int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1428                        u64 *size)
1429 {
1430         struct acpi_device *adev;
1431         LIST_HEAD(list);
1432         struct resource_entry *rentry;
1433         int ret;
1434         struct device *dma_dev = dev;
1435         u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1436
1437         /*
1438          * Walk the device tree chasing an ACPI companion with a _DMA
1439          * object while we go. Stop if we find a device with an ACPI
1440          * companion containing a _DMA method.
1441          */
1442         do {
1443                 adev = ACPI_COMPANION(dma_dev);
1444                 if (adev && acpi_has_method(adev->handle, METHOD_NAME__DMA))
1445                         break;
1446
1447                 dma_dev = dma_dev->parent;
1448         } while (dma_dev);
1449
1450         if (!dma_dev)
1451                 return -ENODEV;
1452
1453         if (!acpi_has_method(adev->handle, METHOD_NAME__CRS)) {
1454                 acpi_handle_warn(adev->handle, "_DMA is valid only if _CRS is present\n");
1455                 return -EINVAL;
1456         }
1457
1458         ret = acpi_dev_get_dma_resources(adev, &list);
1459         if (ret > 0) {
1460                 list_for_each_entry(rentry, &list, node) {
1461                         if (dma_offset && rentry->offset != dma_offset) {
1462                                 ret = -EINVAL;
1463                                 dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1464                                 goto out;
1465                         }
1466                         dma_offset = rentry->offset;
1467
1468                         /* Take lower and upper limits */
1469                         if (rentry->res->start < dma_start)
1470                                 dma_start = rentry->res->start;
1471                         if (rentry->res->end > dma_end)
1472                                 dma_end = rentry->res->end;
1473                 }
1474
1475                 if (dma_start >= dma_end) {
1476                         ret = -EINVAL;
1477                         dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1478                         goto out;
1479                 }
1480
1481                 *dma_addr = dma_start - dma_offset;
1482                 len = dma_end - dma_start;
1483                 *size = max(len, len + 1);
1484                 *offset = dma_offset;
1485         }
1486  out:
1487         acpi_dev_free_resource_list(&list);
1488
1489         return ret >= 0 ? 0 : ret;
1490 }
1491
1492 /**
1493  * acpi_dma_configure - Set-up DMA configuration for the device.
1494  * @dev: The pointer to the device
1495  * @attr: device dma attributes
1496  */
1497 int acpi_dma_configure(struct device *dev, enum dev_dma_attr attr)
1498 {
1499         const struct iommu_ops *iommu;
1500         u64 dma_addr = 0, size = 0;
1501
1502         iort_dma_setup(dev, &dma_addr, &size);
1503
1504         iommu = iort_iommu_configure(dev);
1505         if (IS_ERR(iommu) && PTR_ERR(iommu) == -EPROBE_DEFER)
1506                 return -EPROBE_DEFER;
1507
1508         arch_setup_dma_ops(dev, dma_addr, size,
1509                                 iommu, attr == DEV_DMA_COHERENT);
1510
1511         return 0;
1512 }
1513 EXPORT_SYMBOL_GPL(acpi_dma_configure);
1514
1515 /**
1516  * acpi_dma_deconfigure - Tear-down DMA configuration for the device.
1517  * @dev: The pointer to the device
1518  */
1519 void acpi_dma_deconfigure(struct device *dev)
1520 {
1521         arch_teardown_dma_ops(dev);
1522 }
1523 EXPORT_SYMBOL_GPL(acpi_dma_deconfigure);
1524
1525 static void acpi_init_coherency(struct acpi_device *adev)
1526 {
1527         unsigned long long cca = 0;
1528         acpi_status status;
1529         struct acpi_device *parent = adev->parent;
1530
1531         if (parent && parent->flags.cca_seen) {
1532                 /*
1533                  * From ACPI spec, OSPM will ignore _CCA if an ancestor
1534                  * already saw one.
1535                  */
1536                 adev->flags.cca_seen = 1;
1537                 cca = parent->flags.coherent_dma;
1538         } else {
1539                 status = acpi_evaluate_integer(adev->handle, "_CCA",
1540                                                NULL, &cca);
1541                 if (ACPI_SUCCESS(status))
1542                         adev->flags.cca_seen = 1;
1543                 else if (!IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED))
1544                         /*
1545                          * If architecture does not specify that _CCA is
1546                          * required for DMA-able devices (e.g. x86),
1547                          * we default to _CCA=1.
1548                          */
1549                         cca = 1;
1550                 else
1551                         acpi_handle_debug(adev->handle,
1552                                           "ACPI device is missing _CCA.\n");
1553         }
1554
1555         adev->flags.coherent_dma = cca;
1556 }
1557
1558 static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
1559 {
1560         bool *is_spi_i2c_slave_p = data;
1561
1562         if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
1563                 return 1;
1564
1565         /*
1566          * devices that are connected to UART still need to be enumerated to
1567          * platform bus
1568          */
1569         if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
1570                 *is_spi_i2c_slave_p = true;
1571
1572          /* no need to do more checking */
1573         return -1;
1574 }
1575
1576 static bool acpi_is_spi_i2c_slave(struct acpi_device *device)
1577 {
1578         struct list_head resource_list;
1579         bool is_spi_i2c_slave = false;
1580
1581         /* Macs use device properties in lieu of _CRS resources */
1582         if (x86_apple_machine &&
1583             (fwnode_property_present(&device->fwnode, "spiSclkPeriod") ||
1584              fwnode_property_present(&device->fwnode, "i2cAddress")))
1585                 return true;
1586
1587         INIT_LIST_HEAD(&resource_list);
1588         acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
1589                                &is_spi_i2c_slave);
1590         acpi_dev_free_resource_list(&resource_list);
1591
1592         return is_spi_i2c_slave;
1593 }
1594
1595 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1596                              int type, unsigned long long sta)
1597 {
1598         INIT_LIST_HEAD(&device->pnp.ids);
1599         device->device_type = type;
1600         device->handle = handle;
1601         device->parent = acpi_bus_get_parent(handle);
1602         device->fwnode.ops = &acpi_device_fwnode_ops;
1603         acpi_set_device_status(device, sta);
1604         acpi_device_get_busid(device);
1605         acpi_set_pnp_ids(handle, &device->pnp, type);
1606         acpi_init_properties(device);
1607         acpi_bus_get_flags(device);
1608         device->flags.match_driver = false;
1609         device->flags.initialized = true;
1610         device->flags.spi_i2c_slave = acpi_is_spi_i2c_slave(device);
1611         acpi_device_clear_enumerated(device);
1612         device_initialize(&device->dev);
1613         dev_set_uevent_suppress(&device->dev, true);
1614         acpi_init_coherency(device);
1615         /* Assume there are unmet deps until acpi_device_dep_initialize() runs */
1616         device->dep_unmet = 1;
1617 }
1618
1619 void acpi_device_add_finalize(struct acpi_device *device)
1620 {
1621         dev_set_uevent_suppress(&device->dev, false);
1622         kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1623 }
1624
1625 static int acpi_add_single_object(struct acpi_device **child,
1626                                   acpi_handle handle, int type,
1627                                   unsigned long long sta)
1628 {
1629         int result;
1630         struct acpi_device *device;
1631         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1632
1633         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1634         if (!device) {
1635                 printk(KERN_ERR PREFIX "Memory allocation error\n");
1636                 return -ENOMEM;
1637         }
1638
1639         acpi_init_device_object(device, handle, type, sta);
1640         /*
1641          * For ACPI_BUS_TYPE_DEVICE getting the status is delayed till here so
1642          * that we can call acpi_bus_get_status() and use its quirk handling.
1643          * Note this must be done before the get power-/wakeup_dev-flags calls.
1644          */
1645         if (type == ACPI_BUS_TYPE_DEVICE)
1646                 if (acpi_bus_get_status(device) < 0)
1647                         acpi_set_device_status(device, 0);
1648
1649         acpi_bus_get_power_flags(device);
1650         acpi_bus_get_wakeup_device_flags(device);
1651
1652         result = acpi_device_add(device, acpi_device_release);
1653         if (result) {
1654                 acpi_device_release(&device->dev);
1655                 return result;
1656         }
1657
1658         acpi_power_add_remove_device(device, true);
1659         acpi_device_add_finalize(device);
1660         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1661         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1662                 dev_name(&device->dev), (char *) buffer.pointer,
1663                 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1664         kfree(buffer.pointer);
1665         *child = device;
1666         return 0;
1667 }
1668
1669 static acpi_status acpi_get_resource_memory(struct acpi_resource *ares,
1670                                             void *context)
1671 {
1672         struct resource *res = context;
1673
1674         if (acpi_dev_resource_memory(ares, res))
1675                 return AE_CTRL_TERMINATE;
1676
1677         return AE_OK;
1678 }
1679
1680 static bool acpi_device_should_be_hidden(acpi_handle handle)
1681 {
1682         acpi_status status;
1683         struct resource res;
1684
1685         /* Check if it should ignore the UART device */
1686         if (!(spcr_uart_addr && acpi_has_method(handle, METHOD_NAME__CRS)))
1687                 return false;
1688
1689         /*
1690          * The UART device described in SPCR table is assumed to have only one
1691          * memory resource present. So we only look for the first one here.
1692          */
1693         status = acpi_walk_resources(handle, METHOD_NAME__CRS,
1694                                      acpi_get_resource_memory, &res);
1695         if (ACPI_FAILURE(status) || res.start != spcr_uart_addr)
1696                 return false;
1697
1698         acpi_handle_info(handle, "The UART device @%pa in SPCR table will be hidden\n",
1699                          &res.start);
1700
1701         return true;
1702 }
1703
1704 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1705                                     unsigned long long *sta)
1706 {
1707         acpi_status status;
1708         acpi_object_type acpi_type;
1709
1710         status = acpi_get_type(handle, &acpi_type);
1711         if (ACPI_FAILURE(status))
1712                 return -ENODEV;
1713
1714         switch (acpi_type) {
1715         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1716         case ACPI_TYPE_DEVICE:
1717                 if (acpi_device_should_be_hidden(handle))
1718                         return -ENODEV;
1719
1720                 *type = ACPI_BUS_TYPE_DEVICE;
1721                 /*
1722                  * acpi_add_single_object updates this once we've an acpi_device
1723                  * so that acpi_bus_get_status' quirk handling can be used.
1724                  */
1725                 *sta = ACPI_STA_DEFAULT;
1726                 break;
1727         case ACPI_TYPE_PROCESSOR:
1728                 *type = ACPI_BUS_TYPE_PROCESSOR;
1729                 status = acpi_bus_get_status_handle(handle, sta);
1730                 if (ACPI_FAILURE(status))
1731                         return -ENODEV;
1732                 break;
1733         case ACPI_TYPE_THERMAL:
1734                 *type = ACPI_BUS_TYPE_THERMAL;
1735                 *sta = ACPI_STA_DEFAULT;
1736                 break;
1737         case ACPI_TYPE_POWER:
1738                 *type = ACPI_BUS_TYPE_POWER;
1739                 *sta = ACPI_STA_DEFAULT;
1740                 break;
1741         default:
1742                 return -ENODEV;
1743         }
1744
1745         return 0;
1746 }
1747
1748 bool acpi_device_is_present(const struct acpi_device *adev)
1749 {
1750         return adev->status.present || adev->status.functional;
1751 }
1752
1753 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1754                                        const char *idstr,
1755                                        const struct acpi_device_id **matchid)
1756 {
1757         const struct acpi_device_id *devid;
1758
1759         if (handler->match)
1760                 return handler->match(idstr, matchid);
1761
1762         for (devid = handler->ids; devid->id[0]; devid++)
1763                 if (!strcmp((char *)devid->id, idstr)) {
1764                         if (matchid)
1765                                 *matchid = devid;
1766
1767                         return true;
1768                 }
1769
1770         return false;
1771 }
1772
1773 static struct acpi_scan_handler *acpi_scan_match_handler(const char *idstr,
1774                                         const struct acpi_device_id **matchid)
1775 {
1776         struct acpi_scan_handler *handler;
1777
1778         list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1779                 if (acpi_scan_handler_matching(handler, idstr, matchid))
1780                         return handler;
1781
1782         return NULL;
1783 }
1784
1785 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1786 {
1787         if (!!hotplug->enabled == !!val)
1788                 return;
1789
1790         mutex_lock(&acpi_scan_lock);
1791
1792         hotplug->enabled = val;
1793
1794         mutex_unlock(&acpi_scan_lock);
1795 }
1796
1797 static void acpi_scan_init_hotplug(struct acpi_device *adev)
1798 {
1799         struct acpi_hardware_id *hwid;
1800
1801         if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
1802                 acpi_dock_add(adev);
1803                 return;
1804         }
1805         list_for_each_entry(hwid, &adev->pnp.ids, list) {
1806                 struct acpi_scan_handler *handler;
1807
1808                 handler = acpi_scan_match_handler(hwid->id, NULL);
1809                 if (handler) {
1810                         adev->flags.hotplug_notify = true;
1811                         break;
1812                 }
1813         }
1814 }
1815
1816 static void acpi_device_dep_initialize(struct acpi_device *adev)
1817 {
1818         struct acpi_dep_data *dep;
1819         struct acpi_handle_list dep_devices;
1820         acpi_status status;
1821         int i;
1822
1823         adev->dep_unmet = 0;
1824
1825         if (!acpi_has_method(adev->handle, "_DEP"))
1826                 return;
1827
1828         status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
1829                                         &dep_devices);
1830         if (ACPI_FAILURE(status)) {
1831                 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
1832                 return;
1833         }
1834
1835         for (i = 0; i < dep_devices.count; i++) {
1836                 struct acpi_device_info *info;
1837                 int skip;
1838
1839                 status = acpi_get_object_info(dep_devices.handles[i], &info);
1840                 if (ACPI_FAILURE(status)) {
1841                         dev_dbg(&adev->dev, "Error reading _DEP device info\n");
1842                         continue;
1843                 }
1844
1845                 /*
1846                  * Skip the dependency of Windows System Power
1847                  * Management Controller
1848                  */
1849                 skip = info->valid & ACPI_VALID_HID &&
1850                         !strcmp(info->hardware_id.string, "INT3396");
1851
1852                 kfree(info);
1853
1854                 if (skip)
1855                         continue;
1856
1857                 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
1858                 if (!dep)
1859                         return;
1860
1861                 dep->master = dep_devices.handles[i];
1862                 dep->slave  = adev->handle;
1863                 adev->dep_unmet++;
1864
1865                 mutex_lock(&acpi_dep_list_lock);
1866                 list_add_tail(&dep->node , &acpi_dep_list);
1867                 mutex_unlock(&acpi_dep_list_lock);
1868         }
1869 }
1870
1871 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1872                                       void *not_used, void **return_value)
1873 {
1874         struct acpi_device *device = NULL;
1875         int type;
1876         unsigned long long sta;
1877         int result;
1878
1879         acpi_bus_get_device(handle, &device);
1880         if (device)
1881                 goto out;
1882
1883         result = acpi_bus_type_and_status(handle, &type, &sta);
1884         if (result)
1885                 return AE_OK;
1886
1887         if (type == ACPI_BUS_TYPE_POWER) {
1888                 acpi_add_power_resource(handle);
1889                 return AE_OK;
1890         }
1891
1892         acpi_add_single_object(&device, handle, type, sta);
1893         if (!device)
1894                 return AE_CTRL_DEPTH;
1895
1896         acpi_scan_init_hotplug(device);
1897         acpi_device_dep_initialize(device);
1898
1899  out:
1900         if (!*return_value)
1901                 *return_value = device;
1902
1903         return AE_OK;
1904 }
1905
1906 static void acpi_default_enumeration(struct acpi_device *device)
1907 {
1908         /*
1909          * Do not enumerate SPI/I2C slaves as they will be enumerated by their
1910          * respective parents.
1911          */
1912         if (!device->flags.spi_i2c_slave) {
1913                 acpi_create_platform_device(device, NULL);
1914                 acpi_device_set_enumerated(device);
1915         } else {
1916                 blocking_notifier_call_chain(&acpi_reconfig_chain,
1917                                              ACPI_RECONFIG_DEVICE_ADD, device);
1918         }
1919 }
1920
1921 static const struct acpi_device_id generic_device_ids[] = {
1922         {ACPI_DT_NAMESPACE_HID, },
1923         {"", },
1924 };
1925
1926 static int acpi_generic_device_attach(struct acpi_device *adev,
1927                                       const struct acpi_device_id *not_used)
1928 {
1929         /*
1930          * Since ACPI_DT_NAMESPACE_HID is the only ID handled here, the test
1931          * below can be unconditional.
1932          */
1933         if (adev->data.of_compatible)
1934                 acpi_default_enumeration(adev);
1935
1936         return 1;
1937 }
1938
1939 static struct acpi_scan_handler generic_device_handler = {
1940         .ids = generic_device_ids,
1941         .attach = acpi_generic_device_attach,
1942 };
1943
1944 static int acpi_scan_attach_handler(struct acpi_device *device)
1945 {
1946         struct acpi_hardware_id *hwid;
1947         int ret = 0;
1948
1949         list_for_each_entry(hwid, &device->pnp.ids, list) {
1950                 const struct acpi_device_id *devid;
1951                 struct acpi_scan_handler *handler;
1952
1953                 handler = acpi_scan_match_handler(hwid->id, &devid);
1954                 if (handler) {
1955                         if (!handler->attach) {
1956                                 device->pnp.type.platform_id = 0;
1957                                 continue;
1958                         }
1959                         device->handler = handler;
1960                         ret = handler->attach(device, devid);
1961                         if (ret > 0)
1962                                 break;
1963
1964                         device->handler = NULL;
1965                         if (ret < 0)
1966                                 break;
1967                 }
1968         }
1969
1970         return ret;
1971 }
1972
1973 static void acpi_bus_attach(struct acpi_device *device)
1974 {
1975         struct acpi_device *child;
1976         acpi_handle ejd;
1977         int ret;
1978
1979         if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
1980                 register_dock_dependent_device(device, ejd);
1981
1982         acpi_bus_get_status(device);
1983         /* Skip devices that are not present. */
1984         if (!acpi_device_is_present(device)) {
1985                 device->flags.initialized = false;
1986                 acpi_device_clear_enumerated(device);
1987                 device->flags.power_manageable = 0;
1988                 return;
1989         }
1990         if (device->handler)
1991                 goto ok;
1992
1993         if (!device->flags.initialized) {
1994                 device->flags.power_manageable =
1995                         device->power.states[ACPI_STATE_D0].flags.valid;
1996                 if (acpi_bus_init_power(device))
1997                         device->flags.power_manageable = 0;
1998
1999                 device->flags.initialized = true;
2000         } else if (device->flags.visited) {
2001                 goto ok;
2002         }
2003
2004         ret = acpi_scan_attach_handler(device);
2005         if (ret < 0)
2006                 return;
2007
2008         device->flags.match_driver = true;
2009         if (ret > 0 && !device->flags.spi_i2c_slave) {
2010                 acpi_device_set_enumerated(device);
2011                 goto ok;
2012         }
2013
2014         ret = device_attach(&device->dev);
2015         if (ret < 0)
2016                 return;
2017
2018         if (!device->pnp.type.platform_id && !device->flags.spi_i2c_slave)
2019                 acpi_device_set_enumerated(device);
2020         else
2021                 acpi_default_enumeration(device);
2022
2023  ok:
2024         list_for_each_entry(child, &device->children, node)
2025                 acpi_bus_attach(child);
2026
2027         if (device->handler && device->handler->hotplug.notify_online)
2028                 device->handler->hotplug.notify_online(device);
2029 }
2030
2031 void acpi_walk_dep_device_list(acpi_handle handle)
2032 {
2033         struct acpi_dep_data *dep, *tmp;
2034         struct acpi_device *adev;
2035
2036         mutex_lock(&acpi_dep_list_lock);
2037         list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2038                 if (dep->master == handle) {
2039                         acpi_bus_get_device(dep->slave, &adev);
2040                         if (!adev)
2041                                 continue;
2042
2043                         adev->dep_unmet--;
2044                         if (!adev->dep_unmet)
2045                                 acpi_bus_attach(adev);
2046                         list_del(&dep->node);
2047                         kfree(dep);
2048                 }
2049         }
2050         mutex_unlock(&acpi_dep_list_lock);
2051 }
2052 EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2053
2054 /**
2055  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2056  * @handle: Root of the namespace scope to scan.
2057  *
2058  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2059  * found devices.
2060  *
2061  * If no devices were found, -ENODEV is returned, but it does not mean that
2062  * there has been a real error.  There just have been no suitable ACPI objects
2063  * in the table trunk from which the kernel could create a device and add an
2064  * appropriate driver.
2065  *
2066  * Must be called under acpi_scan_lock.
2067  */
2068 int acpi_bus_scan(acpi_handle handle)
2069 {
2070         void *device = NULL;
2071
2072         if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2073                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2074                                     acpi_bus_check_add, NULL, NULL, &device);
2075
2076         if (device) {
2077                 acpi_bus_attach(device);
2078                 return 0;
2079         }
2080         return -ENODEV;
2081 }
2082 EXPORT_SYMBOL(acpi_bus_scan);
2083
2084 /**
2085  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2086  * @adev: Root of the ACPI namespace scope to walk.
2087  *
2088  * Must be called under acpi_scan_lock.
2089  */
2090 void acpi_bus_trim(struct acpi_device *adev)
2091 {
2092         struct acpi_scan_handler *handler = adev->handler;
2093         struct acpi_device *child;
2094
2095         list_for_each_entry_reverse(child, &adev->children, node)
2096                 acpi_bus_trim(child);
2097
2098         adev->flags.match_driver = false;
2099         if (handler) {
2100                 if (handler->detach)
2101                         handler->detach(adev);
2102
2103                 adev->handler = NULL;
2104         } else {
2105                 device_release_driver(&adev->dev);
2106         }
2107         /*
2108          * Most likely, the device is going away, so put it into D3cold before
2109          * that.
2110          */
2111         acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2112         adev->flags.initialized = false;
2113         acpi_device_clear_enumerated(adev);
2114 }
2115 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2116
2117 int acpi_bus_register_early_device(int type)
2118 {
2119         struct acpi_device *device = NULL;
2120         int result;
2121
2122         result = acpi_add_single_object(&device, NULL,
2123                                         type, ACPI_STA_DEFAULT);
2124         if (result)
2125                 return result;
2126
2127         device->flags.match_driver = true;
2128         return device_attach(&device->dev);
2129 }
2130 EXPORT_SYMBOL_GPL(acpi_bus_register_early_device);
2131
2132 static int acpi_bus_scan_fixed(void)
2133 {
2134         int result = 0;
2135
2136         /*
2137          * Enumerate all fixed-feature devices.
2138          */
2139         if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2140                 struct acpi_device *device = NULL;
2141
2142                 result = acpi_add_single_object(&device, NULL,
2143                                                 ACPI_BUS_TYPE_POWER_BUTTON,
2144                                                 ACPI_STA_DEFAULT);
2145                 if (result)
2146                         return result;
2147
2148                 device->flags.match_driver = true;
2149                 result = device_attach(&device->dev);
2150                 if (result < 0)
2151                         return result;
2152
2153                 device_init_wakeup(&device->dev, true);
2154         }
2155
2156         if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2157                 struct acpi_device *device = NULL;
2158
2159                 result = acpi_add_single_object(&device, NULL,
2160                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
2161                                                 ACPI_STA_DEFAULT);
2162                 if (result)
2163                         return result;
2164
2165                 device->flags.match_driver = true;
2166                 result = device_attach(&device->dev);
2167         }
2168
2169         return result < 0 ? result : 0;
2170 }
2171
2172 static void __init acpi_get_spcr_uart_addr(void)
2173 {
2174         acpi_status status;
2175         struct acpi_table_spcr *spcr_ptr;
2176
2177         status = acpi_get_table(ACPI_SIG_SPCR, 0,
2178                                 (struct acpi_table_header **)&spcr_ptr);
2179         if (ACPI_SUCCESS(status))
2180                 spcr_uart_addr = spcr_ptr->serial_port.address;
2181         else
2182                 printk(KERN_WARNING PREFIX "STAO table present, but SPCR is missing\n");
2183 }
2184
2185 static bool acpi_scan_initialized;
2186
2187 int __init acpi_scan_init(void)
2188 {
2189         int result;
2190         acpi_status status;
2191         struct acpi_table_stao *stao_ptr;
2192
2193         acpi_pci_root_init();
2194         acpi_pci_link_init();
2195         acpi_processor_init();
2196         acpi_lpss_init();
2197         acpi_apd_init();
2198         acpi_cmos_rtc_init();
2199         acpi_container_init();
2200         acpi_memory_hotplug_init();
2201         acpi_pnp_init();
2202         acpi_int340x_thermal_init();
2203         acpi_amba_init();
2204         acpi_watchdog_init();
2205
2206         acpi_scan_add_handler(&generic_device_handler);
2207
2208         /*
2209          * If there is STAO table, check whether it needs to ignore the UART
2210          * device in SPCR table.
2211          */
2212         status = acpi_get_table(ACPI_SIG_STAO, 0,
2213                                 (struct acpi_table_header **)&stao_ptr);
2214         if (ACPI_SUCCESS(status)) {
2215                 if (stao_ptr->header.length > sizeof(struct acpi_table_stao))
2216                         printk(KERN_INFO PREFIX "STAO Name List not yet supported.");
2217
2218                 if (stao_ptr->ignore_uart)
2219                         acpi_get_spcr_uart_addr();
2220         }
2221
2222         acpi_gpe_apply_masked_gpes();
2223         acpi_update_all_gpes();
2224
2225         mutex_lock(&acpi_scan_lock);
2226         /*
2227          * Enumerate devices in the ACPI namespace.
2228          */
2229         result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2230         if (result)
2231                 goto out;
2232
2233         result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2234         if (result)
2235                 goto out;
2236
2237         /* Fixed feature devices do not exist on HW-reduced platform */
2238         if (!acpi_gbl_reduced_hardware) {
2239                 result = acpi_bus_scan_fixed();
2240                 if (result) {
2241                         acpi_detach_data(acpi_root->handle,
2242                                          acpi_scan_drop_device);
2243                         acpi_device_del(acpi_root);
2244                         put_device(&acpi_root->dev);
2245                         goto out;
2246                 }
2247         }
2248
2249         acpi_scan_initialized = true;
2250
2251  out:
2252         mutex_unlock(&acpi_scan_lock);
2253         return result;
2254 }
2255
2256 static struct acpi_probe_entry *ape;
2257 static int acpi_probe_count;
2258 static DEFINE_MUTEX(acpi_probe_mutex);
2259
2260 static int __init acpi_match_madt(struct acpi_subtable_header *header,
2261                                   const unsigned long end)
2262 {
2263         if (!ape->subtable_valid || ape->subtable_valid(header, ape))
2264                 if (!ape->probe_subtbl(header, end))
2265                         acpi_probe_count++;
2266
2267         return 0;
2268 }
2269
2270 int __init __acpi_probe_device_table(struct acpi_probe_entry *ap_head, int nr)
2271 {
2272         int count = 0;
2273
2274         if (acpi_disabled)
2275                 return 0;
2276
2277         mutex_lock(&acpi_probe_mutex);
2278         for (ape = ap_head; nr; ape++, nr--) {
2279                 if (ACPI_COMPARE_NAME(ACPI_SIG_MADT, ape->id)) {
2280                         acpi_probe_count = 0;
2281                         acpi_table_parse_madt(ape->type, acpi_match_madt, 0);
2282                         count += acpi_probe_count;
2283                 } else {
2284                         int res;
2285                         res = acpi_table_parse(ape->id, ape->probe_table);
2286                         if (!res)
2287                                 count++;
2288                 }
2289         }
2290         mutex_unlock(&acpi_probe_mutex);
2291
2292         return count;
2293 }
2294
2295 struct acpi_table_events_work {
2296         struct work_struct work;
2297         void *table;
2298         u32 event;
2299 };
2300
2301 static void acpi_table_events_fn(struct work_struct *work)
2302 {
2303         struct acpi_table_events_work *tew;
2304
2305         tew = container_of(work, struct acpi_table_events_work, work);
2306
2307         if (tew->event == ACPI_TABLE_EVENT_LOAD) {
2308                 acpi_scan_lock_acquire();
2309                 acpi_bus_scan(ACPI_ROOT_OBJECT);
2310                 acpi_scan_lock_release();
2311         }
2312
2313         kfree(tew);
2314 }
2315
2316 void acpi_scan_table_handler(u32 event, void *table, void *context)
2317 {
2318         struct acpi_table_events_work *tew;
2319
2320         if (!acpi_scan_initialized)
2321                 return;
2322
2323         if (event != ACPI_TABLE_EVENT_LOAD)
2324                 return;
2325
2326         tew = kmalloc(sizeof(*tew), GFP_KERNEL);
2327         if (!tew)
2328                 return;
2329
2330         INIT_WORK(&tew->work, acpi_table_events_fn);
2331         tew->table = table;
2332         tew->event = event;
2333
2334         schedule_work(&tew->work);
2335 }
2336
2337 int acpi_reconfig_notifier_register(struct notifier_block *nb)
2338 {
2339         return blocking_notifier_chain_register(&acpi_reconfig_chain, nb);
2340 }
2341 EXPORT_SYMBOL(acpi_reconfig_notifier_register);
2342
2343 int acpi_reconfig_notifier_unregister(struct notifier_block *nb)
2344 {
2345         return blocking_notifier_chain_unregister(&acpi_reconfig_chain, nb);
2346 }
2347 EXPORT_SYMBOL(acpi_reconfig_notifier_unregister);