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