Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / acpi / dock.c
blob24e076f44d23827874c4d476ba8323d55dfa993c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * dock.c - ACPI dock station driver
5 * Copyright (C) 2006, 2014, Intel Corp.
6 * Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
7 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
8 */
10 #include <linux/kernel.h>
11 #include <linux/moduleparam.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/types.h>
15 #include <linux/notifier.h>
16 #include <linux/platform_device.h>
17 #include <linux/jiffies.h>
18 #include <linux/stddef.h>
19 #include <linux/acpi.h>
21 #include "internal.h"
23 static bool immediate_undock = 1;
24 module_param(immediate_undock, bool, 0644);
25 MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
26 "undock immediately when the undock button is pressed, 0 will cause"
27 " the driver to wait for userspace to write the undock sysfs file "
28 " before undocking");
30 struct dock_station {
31 acpi_handle handle;
32 unsigned long last_dock_time;
33 u32 flags;
34 struct list_head dependent_devices;
36 struct list_head sibling;
37 struct platform_device *dock_device;
39 static LIST_HEAD(dock_stations);
40 static int dock_station_count;
42 struct dock_dependent_device {
43 struct list_head list;
44 struct acpi_device *adev;
47 #define DOCK_DOCKING 0x00000001
48 #define DOCK_UNDOCKING 0x00000002
49 #define DOCK_IS_DOCK 0x00000010
50 #define DOCK_IS_ATA 0x00000020
51 #define DOCK_IS_BAT 0x00000040
52 #define DOCK_EVENT 3
53 #define UNDOCK_EVENT 2
55 enum dock_callback_type {
56 DOCK_CALL_HANDLER,
57 DOCK_CALL_FIXUP,
58 DOCK_CALL_UEVENT,
61 /*****************************************************************************
62 * Dock Dependent device functions *
63 *****************************************************************************/
64 /**
65 * add_dock_dependent_device - associate a device with the dock station
66 * @ds: Dock station.
67 * @adev: Dependent ACPI device object.
69 * Add the dependent device to the dock's dependent device list.
71 static int add_dock_dependent_device(struct dock_station *ds,
72 struct acpi_device *adev)
74 struct dock_dependent_device *dd;
76 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
77 if (!dd)
78 return -ENOMEM;
80 dd->adev = adev;
81 INIT_LIST_HEAD(&dd->list);
82 list_add_tail(&dd->list, &ds->dependent_devices);
84 return 0;
87 static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
88 enum dock_callback_type cb_type)
90 struct acpi_device *adev = dd->adev;
92 acpi_lock_hp_context();
94 if (!adev->hp)
95 goto out;
97 if (cb_type == DOCK_CALL_FIXUP) {
98 void (*fixup)(struct acpi_device *);
100 fixup = adev->hp->fixup;
101 if (fixup) {
102 acpi_unlock_hp_context();
103 fixup(adev);
104 return;
106 } else if (cb_type == DOCK_CALL_UEVENT) {
107 void (*uevent)(struct acpi_device *, u32);
109 uevent = adev->hp->uevent;
110 if (uevent) {
111 acpi_unlock_hp_context();
112 uevent(adev, event);
113 return;
115 } else {
116 int (*notify)(struct acpi_device *, u32);
118 notify = adev->hp->notify;
119 if (notify) {
120 acpi_unlock_hp_context();
121 notify(adev, event);
122 return;
126 out:
127 acpi_unlock_hp_context();
130 static struct dock_station *find_dock_station(acpi_handle handle)
132 struct dock_station *ds;
134 list_for_each_entry(ds, &dock_stations, sibling)
135 if (ds->handle == handle)
136 return ds;
138 return NULL;
142 * find_dock_dependent_device - get a device dependent on this dock
143 * @ds: the dock station
144 * @adev: ACPI device object to find.
146 * iterate over the dependent device list for this dock. If the
147 * dependent device matches the handle, return.
149 static struct dock_dependent_device *
150 find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
152 struct dock_dependent_device *dd;
154 list_for_each_entry(dd, &ds->dependent_devices, list)
155 if (adev == dd->adev)
156 return dd;
158 return NULL;
161 void register_dock_dependent_device(struct acpi_device *adev,
162 acpi_handle dshandle)
164 struct dock_station *ds = find_dock_station(dshandle);
166 if (ds && !find_dock_dependent_device(ds, adev))
167 add_dock_dependent_device(ds, adev);
170 /*****************************************************************************
171 * Dock functions *
172 *****************************************************************************/
175 * is_dock_device - see if a device is on a dock station
176 * @adev: ACPI device object to check.
178 * If this device is either the dock station itself,
179 * or is a device dependent on the dock station, then it
180 * is a dock device
182 int is_dock_device(struct acpi_device *adev)
184 struct dock_station *dock_station;
186 if (!dock_station_count)
187 return 0;
189 if (acpi_dock_match(adev->handle))
190 return 1;
192 list_for_each_entry(dock_station, &dock_stations, sibling)
193 if (find_dock_dependent_device(dock_station, adev))
194 return 1;
196 return 0;
198 EXPORT_SYMBOL_GPL(is_dock_device);
201 * dock_present - see if the dock station is present.
202 * @ds: the dock station
204 * execute the _STA method. note that present does not
205 * imply that we are docked.
207 static int dock_present(struct dock_station *ds)
209 unsigned long long sta;
210 acpi_status status;
212 if (ds) {
213 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
214 if (ACPI_SUCCESS(status) && sta)
215 return 1;
217 return 0;
221 * hot_remove_dock_devices - Remove dock station devices.
222 * @ds: Dock station.
224 static void hot_remove_dock_devices(struct dock_station *ds)
226 struct dock_dependent_device *dd;
229 * Walk the list in reverse order so that devices that have been added
230 * last are removed first (in case there are some indirect dependencies
231 * between them).
233 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
234 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST,
235 DOCK_CALL_HANDLER);
237 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
238 acpi_bus_trim(dd->adev);
242 * hotplug_dock_devices - Insert devices on a dock station.
243 * @ds: the dock station
244 * @event: either bus check or device check request
246 * Some devices on the dock station need to have drivers called
247 * to perform hotplug operations after a dock event has occurred.
248 * Traverse the list of dock devices that have registered a
249 * hotplug handler, and call the handler.
251 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
253 struct dock_dependent_device *dd;
255 /* Call driver specific post-dock fixups. */
256 list_for_each_entry(dd, &ds->dependent_devices, list)
257 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
259 /* Call driver specific hotplug functions. */
260 list_for_each_entry(dd, &ds->dependent_devices, list)
261 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
264 * Check if all devices have been enumerated already. If not, run
265 * acpi_bus_scan() for them and that will cause scan handlers to be
266 * attached to device objects or acpi_drivers to be stopped/started if
267 * they are present.
269 list_for_each_entry(dd, &ds->dependent_devices, list) {
270 struct acpi_device *adev = dd->adev;
272 if (!acpi_device_enumerated(adev)) {
273 int ret = acpi_bus_scan(adev->handle);
274 if (ret)
275 dev_dbg(&adev->dev, "scan error %d\n", -ret);
280 static void dock_event(struct dock_station *ds, u32 event, int num)
282 struct device *dev = &ds->dock_device->dev;
283 char event_string[13];
284 char *envp[] = { event_string, NULL };
285 struct dock_dependent_device *dd;
287 if (num == UNDOCK_EVENT)
288 sprintf(event_string, "EVENT=undock");
289 else
290 sprintf(event_string, "EVENT=dock");
293 * Indicate that the status of the dock station has
294 * changed.
296 if (num == DOCK_EVENT)
297 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
299 list_for_each_entry(dd, &ds->dependent_devices, list)
300 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
302 if (num != DOCK_EVENT)
303 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
307 * handle_dock - handle a dock event
308 * @ds: the dock station
309 * @dock: to dock, or undock - that is the question
311 * Execute the _DCK method in response to an acpi event
313 static void handle_dock(struct dock_station *ds, int dock)
315 acpi_status status;
316 struct acpi_object_list arg_list;
317 union acpi_object arg;
318 unsigned long long value;
320 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
322 /* _DCK method has one argument */
323 arg_list.count = 1;
324 arg_list.pointer = &arg;
325 arg.type = ACPI_TYPE_INTEGER;
326 arg.integer.value = dock;
327 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
328 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
329 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
330 status);
333 static inline void dock(struct dock_station *ds)
335 handle_dock(ds, 1);
338 static inline void undock(struct dock_station *ds)
340 handle_dock(ds, 0);
343 static inline void begin_dock(struct dock_station *ds)
345 ds->flags |= DOCK_DOCKING;
348 static inline void complete_dock(struct dock_station *ds)
350 ds->flags &= ~(DOCK_DOCKING);
351 ds->last_dock_time = jiffies;
354 static inline void begin_undock(struct dock_station *ds)
356 ds->flags |= DOCK_UNDOCKING;
359 static inline void complete_undock(struct dock_station *ds)
361 ds->flags &= ~(DOCK_UNDOCKING);
365 * dock_in_progress - see if we are in the middle of handling a dock event
366 * @ds: the dock station
368 * Sometimes while docking, false dock events can be sent to the driver
369 * because good connections aren't made or some other reason. Ignore these
370 * if we are in the middle of doing something.
372 static int dock_in_progress(struct dock_station *ds)
374 if ((ds->flags & DOCK_DOCKING) ||
375 time_before(jiffies, (ds->last_dock_time + HZ)))
376 return 1;
377 return 0;
381 * handle_eject_request - handle an undock request checking for error conditions
383 * Check to make sure the dock device is still present, then undock and
384 * hotremove all the devices that may need removing.
386 static int handle_eject_request(struct dock_station *ds, u32 event)
388 if (dock_in_progress(ds))
389 return -EBUSY;
392 * here we need to generate the undock
393 * event prior to actually doing the undock
394 * so that the device struct still exists.
395 * Also, even send the dock event if the
396 * device is not present anymore
398 dock_event(ds, event, UNDOCK_EVENT);
400 hot_remove_dock_devices(ds);
401 undock(ds);
402 acpi_evaluate_lck(ds->handle, 0);
403 acpi_evaluate_ej0(ds->handle);
404 if (dock_present(ds)) {
405 acpi_handle_err(ds->handle, "Unable to undock!\n");
406 return -EBUSY;
408 complete_undock(ds);
409 return 0;
413 * dock_notify - Handle ACPI dock notification.
414 * @adev: Dock station's ACPI device object.
415 * @event: Event code.
417 * If we are notified to dock, then check to see if the dock is
418 * present and then dock. Notify all drivers of the dock event,
419 * and then hotplug and devices that may need hotplugging.
421 int dock_notify(struct acpi_device *adev, u32 event)
423 acpi_handle handle = adev->handle;
424 struct dock_station *ds = find_dock_station(handle);
425 int surprise_removal = 0;
427 if (!ds)
428 return -ENODEV;
431 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
432 * is sent and _DCK is present, it is assumed to mean an undock
433 * request.
435 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
436 event = ACPI_NOTIFY_EJECT_REQUEST;
439 * dock station: BUS_CHECK - docked or surprise removal
440 * DEVICE_CHECK - undocked
441 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
443 * To simplify event handling, dock dependent device handler always
444 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
445 * ACPI_NOTIFY_EJECT_REQUEST for removal
447 switch (event) {
448 case ACPI_NOTIFY_BUS_CHECK:
449 case ACPI_NOTIFY_DEVICE_CHECK:
450 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
451 begin_dock(ds);
452 dock(ds);
453 if (!dock_present(ds)) {
454 acpi_handle_err(handle, "Unable to dock!\n");
455 complete_dock(ds);
456 break;
458 hotplug_dock_devices(ds, event);
459 complete_dock(ds);
460 dock_event(ds, event, DOCK_EVENT);
461 acpi_evaluate_lck(ds->handle, 1);
462 acpi_update_all_gpes();
463 break;
465 if (dock_present(ds) || dock_in_progress(ds))
466 break;
467 /* This is a surprise removal */
468 surprise_removal = 1;
469 event = ACPI_NOTIFY_EJECT_REQUEST;
470 /* Fall back */
471 fallthrough;
472 case ACPI_NOTIFY_EJECT_REQUEST:
473 begin_undock(ds);
474 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
475 || surprise_removal)
476 handle_eject_request(ds, event);
477 else
478 dock_event(ds, event, UNDOCK_EVENT);
479 break;
481 return 0;
485 * show_docked - read method for "docked" file in sysfs
487 static ssize_t show_docked(struct device *dev,
488 struct device_attribute *attr, char *buf)
490 struct dock_station *dock_station = dev->platform_data;
491 struct acpi_device *adev = NULL;
493 acpi_bus_get_device(dock_station->handle, &adev);
494 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
496 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
499 * show_flags - read method for flags file in sysfs
501 static ssize_t show_flags(struct device *dev,
502 struct device_attribute *attr, char *buf)
504 struct dock_station *dock_station = dev->platform_data;
505 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
508 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
511 * write_undock - write method for "undock" file in sysfs
513 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
514 const char *buf, size_t count)
516 int ret;
517 struct dock_station *dock_station = dev->platform_data;
519 if (!count)
520 return -EINVAL;
522 acpi_scan_lock_acquire();
523 begin_undock(dock_station);
524 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
525 acpi_scan_lock_release();
526 return ret ? ret: count;
528 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
531 * show_dock_uid - read method for "uid" file in sysfs
533 static ssize_t show_dock_uid(struct device *dev,
534 struct device_attribute *attr, char *buf)
536 unsigned long long lbuf;
537 struct dock_station *dock_station = dev->platform_data;
538 acpi_status status = acpi_evaluate_integer(dock_station->handle,
539 "_UID", NULL, &lbuf);
540 if (ACPI_FAILURE(status))
541 return 0;
543 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
545 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
547 static ssize_t show_dock_type(struct device *dev,
548 struct device_attribute *attr, char *buf)
550 struct dock_station *dock_station = dev->platform_data;
551 char *type;
553 if (dock_station->flags & DOCK_IS_DOCK)
554 type = "dock_station";
555 else if (dock_station->flags & DOCK_IS_ATA)
556 type = "ata_bay";
557 else if (dock_station->flags & DOCK_IS_BAT)
558 type = "battery_bay";
559 else
560 type = "unknown";
562 return snprintf(buf, PAGE_SIZE, "%s\n", type);
564 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
566 static struct attribute *dock_attributes[] = {
567 &dev_attr_docked.attr,
568 &dev_attr_flags.attr,
569 &dev_attr_undock.attr,
570 &dev_attr_uid.attr,
571 &dev_attr_type.attr,
572 NULL
575 static const struct attribute_group dock_attribute_group = {
576 .attrs = dock_attributes
580 * acpi_dock_add - Add a new dock station
581 * @adev: Dock station ACPI device object.
583 * allocated and initialize a new dock station device.
585 void acpi_dock_add(struct acpi_device *adev)
587 struct dock_station *dock_station, ds = { NULL, };
588 struct platform_device_info pdevinfo;
589 acpi_handle handle = adev->handle;
590 struct platform_device *dd;
591 int ret;
593 memset(&pdevinfo, 0, sizeof(pdevinfo));
594 pdevinfo.name = "dock";
595 pdevinfo.id = dock_station_count;
596 pdevinfo.fwnode = acpi_fwnode_handle(adev);
597 pdevinfo.data = &ds;
598 pdevinfo.size_data = sizeof(ds);
599 dd = platform_device_register_full(&pdevinfo);
600 if (IS_ERR(dd))
601 return;
603 dock_station = dd->dev.platform_data;
605 dock_station->handle = handle;
606 dock_station->dock_device = dd;
607 dock_station->last_dock_time = jiffies - HZ;
609 INIT_LIST_HEAD(&dock_station->sibling);
610 INIT_LIST_HEAD(&dock_station->dependent_devices);
612 /* we want the dock device to send uevents */
613 dev_set_uevent_suppress(&dd->dev, 0);
615 if (acpi_dock_match(handle))
616 dock_station->flags |= DOCK_IS_DOCK;
617 if (acpi_ata_match(handle))
618 dock_station->flags |= DOCK_IS_ATA;
619 if (acpi_device_is_battery(adev))
620 dock_station->flags |= DOCK_IS_BAT;
622 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
623 if (ret)
624 goto err_unregister;
626 /* add the dock station as a device dependent on itself */
627 ret = add_dock_dependent_device(dock_station, adev);
628 if (ret)
629 goto err_rmgroup;
631 dock_station_count++;
632 list_add(&dock_station->sibling, &dock_stations);
633 adev->flags.is_dock_station = true;
634 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
635 dock_station_count);
636 return;
638 err_rmgroup:
639 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
641 err_unregister:
642 platform_device_unregister(dd);
643 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);