1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
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>
23 ACPI_MODULE_NAME("dock");
25 static bool immediate_undock
= 1;
26 module_param(immediate_undock
, bool, 0644);
27 MODULE_PARM_DESC(immediate_undock
, "1 (default) will cause the driver to "
28 "undock immediately when the undock button is pressed, 0 will cause"
29 " the driver to wait for userspace to write the undock sysfs file "
34 unsigned long last_dock_time
;
36 struct list_head dependent_devices
;
38 struct list_head sibling
;
39 struct platform_device
*dock_device
;
41 static LIST_HEAD(dock_stations
);
42 static int dock_station_count
;
44 struct dock_dependent_device
{
45 struct list_head list
;
46 struct acpi_device
*adev
;
49 #define DOCK_DOCKING 0x00000001
50 #define DOCK_UNDOCKING 0x00000002
51 #define DOCK_IS_DOCK 0x00000010
52 #define DOCK_IS_ATA 0x00000020
53 #define DOCK_IS_BAT 0x00000040
55 #define UNDOCK_EVENT 2
57 enum dock_callback_type
{
63 /*****************************************************************************
64 * Dock Dependent device functions *
65 *****************************************************************************/
67 * add_dock_dependent_device - associate a device with the dock station
69 * @adev: Dependent ACPI device object.
71 * Add the dependent device to the dock's dependent device list.
73 static int add_dock_dependent_device(struct dock_station
*ds
,
74 struct acpi_device
*adev
)
76 struct dock_dependent_device
*dd
;
78 dd
= kzalloc(sizeof(*dd
), GFP_KERNEL
);
83 INIT_LIST_HEAD(&dd
->list
);
84 list_add_tail(&dd
->list
, &ds
->dependent_devices
);
89 static void dock_hotplug_event(struct dock_dependent_device
*dd
, u32 event
,
90 enum dock_callback_type cb_type
)
92 struct acpi_device
*adev
= dd
->adev
;
94 acpi_lock_hp_context();
99 if (cb_type
== DOCK_CALL_FIXUP
) {
100 void (*fixup
)(struct acpi_device
*);
102 fixup
= adev
->hp
->fixup
;
104 acpi_unlock_hp_context();
108 } else if (cb_type
== DOCK_CALL_UEVENT
) {
109 void (*uevent
)(struct acpi_device
*, u32
);
111 uevent
= adev
->hp
->uevent
;
113 acpi_unlock_hp_context();
118 int (*notify
)(struct acpi_device
*, u32
);
120 notify
= adev
->hp
->notify
;
122 acpi_unlock_hp_context();
129 acpi_unlock_hp_context();
132 static struct dock_station
*find_dock_station(acpi_handle handle
)
134 struct dock_station
*ds
;
136 list_for_each_entry(ds
, &dock_stations
, sibling
)
137 if (ds
->handle
== handle
)
144 * find_dock_dependent_device - get a device dependent on this dock
145 * @ds: the dock station
146 * @adev: ACPI device object to find.
148 * iterate over the dependent device list for this dock. If the
149 * dependent device matches the handle, return.
151 static struct dock_dependent_device
*
152 find_dock_dependent_device(struct dock_station
*ds
, struct acpi_device
*adev
)
154 struct dock_dependent_device
*dd
;
156 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
157 if (adev
== dd
->adev
)
163 void register_dock_dependent_device(struct acpi_device
*adev
,
164 acpi_handle dshandle
)
166 struct dock_station
*ds
= find_dock_station(dshandle
);
168 if (ds
&& !find_dock_dependent_device(ds
, adev
))
169 add_dock_dependent_device(ds
, adev
);
172 /*****************************************************************************
174 *****************************************************************************/
177 * is_dock_device - see if a device is on a dock station
178 * @adev: ACPI device object to check.
180 * If this device is either the dock station itself,
181 * or is a device dependent on the dock station, then it
184 int is_dock_device(struct acpi_device
*adev
)
186 struct dock_station
*dock_station
;
188 if (!dock_station_count
)
191 if (acpi_dock_match(adev
->handle
))
194 list_for_each_entry(dock_station
, &dock_stations
, sibling
)
195 if (find_dock_dependent_device(dock_station
, adev
))
200 EXPORT_SYMBOL_GPL(is_dock_device
);
203 * dock_present - see if the dock station is present.
204 * @ds: the dock station
206 * execute the _STA method. note that present does not
207 * imply that we are docked.
209 static int dock_present(struct dock_station
*ds
)
211 unsigned long long sta
;
215 status
= acpi_evaluate_integer(ds
->handle
, "_STA", NULL
, &sta
);
216 if (ACPI_SUCCESS(status
) && sta
)
223 * hot_remove_dock_devices - Remove dock station devices.
226 static void hot_remove_dock_devices(struct dock_station
*ds
)
228 struct dock_dependent_device
*dd
;
231 * Walk the list in reverse order so that devices that have been added
232 * last are removed first (in case there are some indirect dependencies
235 list_for_each_entry_reverse(dd
, &ds
->dependent_devices
, list
)
236 dock_hotplug_event(dd
, ACPI_NOTIFY_EJECT_REQUEST
, false);
238 list_for_each_entry_reverse(dd
, &ds
->dependent_devices
, list
)
239 acpi_bus_trim(dd
->adev
);
243 * hotplug_dock_devices - Insert devices on a dock station.
244 * @ds: the dock station
245 * @event: either bus check or device check request
247 * Some devices on the dock station need to have drivers called
248 * to perform hotplug operations after a dock event has occurred.
249 * Traverse the list of dock devices that have registered a
250 * hotplug handler, and call the handler.
252 static void hotplug_dock_devices(struct dock_station
*ds
, u32 event
)
254 struct dock_dependent_device
*dd
;
256 /* Call driver specific post-dock fixups. */
257 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
258 dock_hotplug_event(dd
, event
, DOCK_CALL_FIXUP
);
260 /* Call driver specific hotplug functions. */
261 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
262 dock_hotplug_event(dd
, event
, DOCK_CALL_HANDLER
);
265 * Check if all devices have been enumerated already. If not, run
266 * acpi_bus_scan() for them and that will cause scan handlers to be
267 * attached to device objects or acpi_drivers to be stopped/started if
270 list_for_each_entry(dd
, &ds
->dependent_devices
, list
) {
271 struct acpi_device
*adev
= dd
->adev
;
273 if (!acpi_device_enumerated(adev
)) {
274 int ret
= acpi_bus_scan(adev
->handle
);
276 dev_dbg(&adev
->dev
, "scan error %d\n", -ret
);
281 static void dock_event(struct dock_station
*ds
, u32 event
, int num
)
283 struct device
*dev
= &ds
->dock_device
->dev
;
284 char event_string
[13];
285 char *envp
[] = { event_string
, NULL
};
286 struct dock_dependent_device
*dd
;
288 if (num
== UNDOCK_EVENT
)
289 sprintf(event_string
, "EVENT=undock");
291 sprintf(event_string
, "EVENT=dock");
294 * Indicate that the status of the dock station has
297 if (num
== DOCK_EVENT
)
298 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
300 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
301 dock_hotplug_event(dd
, event
, DOCK_CALL_UEVENT
);
303 if (num
!= DOCK_EVENT
)
304 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
308 * handle_dock - handle a dock event
309 * @ds: the dock station
310 * @dock: to dock, or undock - that is the question
312 * Execute the _DCK method in response to an acpi event
314 static void handle_dock(struct dock_station
*ds
, int dock
)
317 struct acpi_object_list arg_list
;
318 union acpi_object arg
;
319 unsigned long long value
;
321 acpi_handle_info(ds
->handle
, "%s\n", dock
? "docking" : "undocking");
323 /* _DCK method has one argument */
325 arg_list
.pointer
= &arg
;
326 arg
.type
= ACPI_TYPE_INTEGER
;
327 arg
.integer
.value
= dock
;
328 status
= acpi_evaluate_integer(ds
->handle
, "_DCK", &arg_list
, &value
);
329 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
)
330 acpi_handle_err(ds
->handle
, "Failed to execute _DCK (0x%x)\n",
334 static inline void dock(struct dock_station
*ds
)
339 static inline void undock(struct dock_station
*ds
)
344 static inline void begin_dock(struct dock_station
*ds
)
346 ds
->flags
|= DOCK_DOCKING
;
349 static inline void complete_dock(struct dock_station
*ds
)
351 ds
->flags
&= ~(DOCK_DOCKING
);
352 ds
->last_dock_time
= jiffies
;
355 static inline void begin_undock(struct dock_station
*ds
)
357 ds
->flags
|= DOCK_UNDOCKING
;
360 static inline void complete_undock(struct dock_station
*ds
)
362 ds
->flags
&= ~(DOCK_UNDOCKING
);
366 * dock_in_progress - see if we are in the middle of handling a dock event
367 * @ds: the dock station
369 * Sometimes while docking, false dock events can be sent to the driver
370 * because good connections aren't made or some other reason. Ignore these
371 * if we are in the middle of doing something.
373 static int dock_in_progress(struct dock_station
*ds
)
375 if ((ds
->flags
& DOCK_DOCKING
) ||
376 time_before(jiffies
, (ds
->last_dock_time
+ HZ
)))
382 * handle_eject_request - handle an undock request checking for error conditions
384 * Check to make sure the dock device is still present, then undock and
385 * hotremove all the devices that may need removing.
387 static int handle_eject_request(struct dock_station
*ds
, u32 event
)
389 if (dock_in_progress(ds
))
393 * here we need to generate the undock
394 * event prior to actually doing the undock
395 * so that the device struct still exists.
396 * Also, even send the dock event if the
397 * device is not present anymore
399 dock_event(ds
, event
, UNDOCK_EVENT
);
401 hot_remove_dock_devices(ds
);
403 acpi_evaluate_lck(ds
->handle
, 0);
404 acpi_evaluate_ej0(ds
->handle
);
405 if (dock_present(ds
)) {
406 acpi_handle_err(ds
->handle
, "Unable to undock!\n");
414 * dock_notify - Handle ACPI dock notification.
415 * @adev: Dock station's ACPI device object.
416 * @event: Event code.
418 * If we are notified to dock, then check to see if the dock is
419 * present and then dock. Notify all drivers of the dock event,
420 * and then hotplug and devices that may need hotplugging.
422 int dock_notify(struct acpi_device
*adev
, u32 event
)
424 acpi_handle handle
= adev
->handle
;
425 struct dock_station
*ds
= find_dock_station(handle
);
426 int surprise_removal
= 0;
432 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
433 * is sent and _DCK is present, it is assumed to mean an undock
436 if ((ds
->flags
& DOCK_IS_DOCK
) && event
== ACPI_NOTIFY_DEVICE_CHECK
)
437 event
= ACPI_NOTIFY_EJECT_REQUEST
;
440 * dock station: BUS_CHECK - docked or surprise removal
441 * DEVICE_CHECK - undocked
442 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
444 * To simplify event handling, dock dependent device handler always
445 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
446 * ACPI_NOTIFY_EJECT_REQUEST for removal
449 case ACPI_NOTIFY_BUS_CHECK
:
450 case ACPI_NOTIFY_DEVICE_CHECK
:
451 if (!dock_in_progress(ds
) && !acpi_device_enumerated(adev
)) {
454 if (!dock_present(ds
)) {
455 acpi_handle_err(handle
, "Unable to dock!\n");
459 hotplug_dock_devices(ds
, event
);
461 dock_event(ds
, event
, DOCK_EVENT
);
462 acpi_evaluate_lck(ds
->handle
, 1);
463 acpi_update_all_gpes();
466 if (dock_present(ds
) || dock_in_progress(ds
))
468 /* This is a surprise removal */
469 surprise_removal
= 1;
470 event
= ACPI_NOTIFY_EJECT_REQUEST
;
473 case ACPI_NOTIFY_EJECT_REQUEST
:
475 if ((immediate_undock
&& !(ds
->flags
& DOCK_IS_ATA
))
477 handle_eject_request(ds
, event
);
479 dock_event(ds
, event
, UNDOCK_EVENT
);
486 * show_docked - read method for "docked" file in sysfs
488 static ssize_t
show_docked(struct device
*dev
,
489 struct device_attribute
*attr
, char *buf
)
491 struct dock_station
*dock_station
= dev
->platform_data
;
492 struct acpi_device
*adev
= NULL
;
494 acpi_bus_get_device(dock_station
->handle
, &adev
);
495 return snprintf(buf
, PAGE_SIZE
, "%u\n", acpi_device_enumerated(adev
));
497 static DEVICE_ATTR(docked
, S_IRUGO
, show_docked
, NULL
);
500 * show_flags - read method for flags file in sysfs
502 static ssize_t
show_flags(struct device
*dev
,
503 struct device_attribute
*attr
, char *buf
)
505 struct dock_station
*dock_station
= dev
->platform_data
;
506 return snprintf(buf
, PAGE_SIZE
, "%d\n", dock_station
->flags
);
509 static DEVICE_ATTR(flags
, S_IRUGO
, show_flags
, NULL
);
512 * write_undock - write method for "undock" file in sysfs
514 static ssize_t
write_undock(struct device
*dev
, struct device_attribute
*attr
,
515 const char *buf
, size_t count
)
518 struct dock_station
*dock_station
= dev
->platform_data
;
523 acpi_scan_lock_acquire();
524 begin_undock(dock_station
);
525 ret
= handle_eject_request(dock_station
, ACPI_NOTIFY_EJECT_REQUEST
);
526 acpi_scan_lock_release();
527 return ret
? ret
: count
;
529 static DEVICE_ATTR(undock
, S_IWUSR
, NULL
, write_undock
);
532 * show_dock_uid - read method for "uid" file in sysfs
534 static ssize_t
show_dock_uid(struct device
*dev
,
535 struct device_attribute
*attr
, char *buf
)
537 unsigned long long lbuf
;
538 struct dock_station
*dock_station
= dev
->platform_data
;
539 acpi_status status
= acpi_evaluate_integer(dock_station
->handle
,
540 "_UID", NULL
, &lbuf
);
541 if (ACPI_FAILURE(status
))
544 return snprintf(buf
, PAGE_SIZE
, "%llx\n", lbuf
);
546 static DEVICE_ATTR(uid
, S_IRUGO
, show_dock_uid
, NULL
);
548 static ssize_t
show_dock_type(struct device
*dev
,
549 struct device_attribute
*attr
, char *buf
)
551 struct dock_station
*dock_station
= dev
->platform_data
;
554 if (dock_station
->flags
& DOCK_IS_DOCK
)
555 type
= "dock_station";
556 else if (dock_station
->flags
& DOCK_IS_ATA
)
558 else if (dock_station
->flags
& DOCK_IS_BAT
)
559 type
= "battery_bay";
563 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
565 static DEVICE_ATTR(type
, S_IRUGO
, show_dock_type
, NULL
);
567 static struct attribute
*dock_attributes
[] = {
568 &dev_attr_docked
.attr
,
569 &dev_attr_flags
.attr
,
570 &dev_attr_undock
.attr
,
576 static const struct attribute_group dock_attribute_group
= {
577 .attrs
= dock_attributes
581 * acpi_dock_add - Add a new dock station
582 * @adev: Dock station ACPI device object.
584 * allocated and initialize a new dock station device.
586 void acpi_dock_add(struct acpi_device
*adev
)
588 struct dock_station
*dock_station
, ds
= { NULL
, };
589 struct platform_device_info pdevinfo
;
590 acpi_handle handle
= adev
->handle
;
591 struct platform_device
*dd
;
594 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
595 pdevinfo
.name
= "dock";
596 pdevinfo
.id
= dock_station_count
;
597 pdevinfo
.fwnode
= acpi_fwnode_handle(adev
);
599 pdevinfo
.size_data
= sizeof(ds
);
600 dd
= platform_device_register_full(&pdevinfo
);
604 dock_station
= dd
->dev
.platform_data
;
606 dock_station
->handle
= handle
;
607 dock_station
->dock_device
= dd
;
608 dock_station
->last_dock_time
= jiffies
- HZ
;
610 INIT_LIST_HEAD(&dock_station
->sibling
);
611 INIT_LIST_HEAD(&dock_station
->dependent_devices
);
613 /* we want the dock device to send uevents */
614 dev_set_uevent_suppress(&dd
->dev
, 0);
616 if (acpi_dock_match(handle
))
617 dock_station
->flags
|= DOCK_IS_DOCK
;
618 if (acpi_ata_match(handle
))
619 dock_station
->flags
|= DOCK_IS_ATA
;
620 if (acpi_device_is_battery(adev
))
621 dock_station
->flags
|= DOCK_IS_BAT
;
623 ret
= sysfs_create_group(&dd
->dev
.kobj
, &dock_attribute_group
);
627 /* add the dock station as a device dependent on itself */
628 ret
= add_dock_dependent_device(dock_station
, adev
);
632 dock_station_count
++;
633 list_add(&dock_station
->sibling
, &dock_stations
);
634 adev
->flags
.is_dock_station
= true;
635 dev_info(&adev
->dev
, "ACPI dock station (docks/bays count: %d)\n",
640 sysfs_remove_group(&dd
->dev
.kobj
, &dock_attribute_group
);
643 platform_device_unregister(dd
);
644 acpi_handle_err(handle
, "%s encountered error %d\n", __func__
, ret
);