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