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 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 "
32 unsigned long last_dock_time
;
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
53 #define UNDOCK_EVENT 2
55 enum dock_callback_type
{
61 /*****************************************************************************
62 * Dock Dependent device functions *
63 *****************************************************************************/
65 * add_dock_dependent_device - associate a device with the 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
);
81 INIT_LIST_HEAD(&dd
->list
);
82 list_add_tail(&dd
->list
, &ds
->dependent_devices
);
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
;
91 acpi_hp_fixup fixup
= NULL
;
92 acpi_hp_uevent uevent
= NULL
;
93 acpi_hp_notify notify
= NULL
;
95 acpi_lock_hp_context();
98 if (cb_type
== DOCK_CALL_FIXUP
)
99 fixup
= adev
->hp
->fixup
;
100 else if (cb_type
== DOCK_CALL_UEVENT
)
101 uevent
= adev
->hp
->uevent
;
103 notify
= adev
->hp
->notify
;
106 acpi_unlock_hp_context();
116 static struct dock_station
*find_dock_station(acpi_handle handle
)
118 struct dock_station
*ds
;
120 list_for_each_entry(ds
, &dock_stations
, sibling
)
121 if (ds
->handle
== handle
)
128 * find_dock_dependent_device - get a device dependent on this dock
129 * @ds: the dock station
130 * @adev: ACPI device object to find.
132 * iterate over the dependent device list for this dock. If the
133 * dependent device matches the handle, return.
135 static struct dock_dependent_device
*
136 find_dock_dependent_device(struct dock_station
*ds
, struct acpi_device
*adev
)
138 struct dock_dependent_device
*dd
;
140 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
141 if (adev
== dd
->adev
)
147 void register_dock_dependent_device(struct acpi_device
*adev
,
148 acpi_handle dshandle
)
150 struct dock_station
*ds
= find_dock_station(dshandle
);
152 if (ds
&& !find_dock_dependent_device(ds
, adev
))
153 add_dock_dependent_device(ds
, adev
);
156 /*****************************************************************************
158 *****************************************************************************/
161 * is_dock_device - see if a device is on a dock station
162 * @adev: ACPI device object to check.
164 * If this device is either the dock station itself,
165 * or is a device dependent on the dock station, then it
168 int is_dock_device(struct acpi_device
*adev
)
170 struct dock_station
*dock_station
;
172 if (!dock_station_count
)
175 if (acpi_dock_match(adev
->handle
))
178 list_for_each_entry(dock_station
, &dock_stations
, sibling
)
179 if (find_dock_dependent_device(dock_station
, adev
))
184 EXPORT_SYMBOL_GPL(is_dock_device
);
187 * dock_present - see if the dock station is present.
188 * @ds: the dock station
190 * execute the _STA method. note that present does not
191 * imply that we are docked.
193 static int dock_present(struct dock_station
*ds
)
195 unsigned long long sta
;
199 status
= acpi_evaluate_integer(ds
->handle
, "_STA", NULL
, &sta
);
200 if (ACPI_SUCCESS(status
) && sta
)
207 * hot_remove_dock_devices - Remove dock station devices.
210 static void hot_remove_dock_devices(struct dock_station
*ds
)
212 struct dock_dependent_device
*dd
;
215 * Walk the list in reverse order so that devices that have been added
216 * last are removed first (in case there are some indirect dependencies
219 list_for_each_entry_reverse(dd
, &ds
->dependent_devices
, list
)
220 dock_hotplug_event(dd
, ACPI_NOTIFY_EJECT_REQUEST
,
223 list_for_each_entry_reverse(dd
, &ds
->dependent_devices
, list
)
224 acpi_bus_trim(dd
->adev
);
228 * hotplug_dock_devices - Insert devices on a dock station.
229 * @ds: the dock station
230 * @event: either bus check or device check request
232 * Some devices on the dock station need to have drivers called
233 * to perform hotplug operations after a dock event has occurred.
234 * Traverse the list of dock devices that have registered a
235 * hotplug handler, and call the handler.
237 static void hotplug_dock_devices(struct dock_station
*ds
, u32 event
)
239 struct dock_dependent_device
*dd
;
241 /* Call driver specific post-dock fixups. */
242 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
243 dock_hotplug_event(dd
, event
, DOCK_CALL_FIXUP
);
245 /* Call driver specific hotplug functions. */
246 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
247 dock_hotplug_event(dd
, event
, DOCK_CALL_HANDLER
);
250 * Check if all devices have been enumerated already. If not, run
251 * acpi_bus_scan() for them and that will cause scan handlers to be
252 * attached to device objects or acpi_drivers to be stopped/started if
255 list_for_each_entry(dd
, &ds
->dependent_devices
, list
) {
256 struct acpi_device
*adev
= dd
->adev
;
258 if (!acpi_device_enumerated(adev
)) {
259 int ret
= acpi_bus_scan(adev
->handle
);
262 dev_dbg(&adev
->dev
, "scan error %d\n", -ret
);
267 static void dock_event(struct dock_station
*ds
, u32 event
, int num
)
269 struct device
*dev
= &ds
->dock_device
->dev
;
270 char event_string
[13];
271 char *envp
[] = { event_string
, NULL
};
272 struct dock_dependent_device
*dd
;
274 if (num
== UNDOCK_EVENT
)
275 sprintf(event_string
, "EVENT=undock");
277 sprintf(event_string
, "EVENT=dock");
280 * Indicate that the status of the dock station has
283 if (num
== DOCK_EVENT
)
284 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
286 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
287 dock_hotplug_event(dd
, event
, DOCK_CALL_UEVENT
);
289 if (num
!= DOCK_EVENT
)
290 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
294 * handle_dock - handle a dock event
295 * @ds: the dock station
296 * @dock: to dock, or undock - that is the question
298 * Execute the _DCK method in response to an acpi event
300 static void handle_dock(struct dock_station
*ds
, int dock
)
303 struct acpi_object_list arg_list
;
304 union acpi_object arg
;
305 unsigned long long value
;
307 acpi_handle_info(ds
->handle
, "%s\n", dock
? "docking" : "undocking");
309 /* _DCK method has one argument */
311 arg_list
.pointer
= &arg
;
312 arg
.type
= ACPI_TYPE_INTEGER
;
313 arg
.integer
.value
= dock
;
314 status
= acpi_evaluate_integer(ds
->handle
, "_DCK", &arg_list
, &value
);
315 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
)
316 acpi_handle_err(ds
->handle
, "Failed to execute _DCK (0x%x)\n",
320 static inline void dock(struct dock_station
*ds
)
325 static inline void undock(struct dock_station
*ds
)
330 static inline void begin_dock(struct dock_station
*ds
)
332 ds
->flags
|= DOCK_DOCKING
;
335 static inline void complete_dock(struct dock_station
*ds
)
337 ds
->flags
&= ~(DOCK_DOCKING
);
338 ds
->last_dock_time
= jiffies
;
341 static inline void begin_undock(struct dock_station
*ds
)
343 ds
->flags
|= DOCK_UNDOCKING
;
346 static inline void complete_undock(struct dock_station
*ds
)
348 ds
->flags
&= ~(DOCK_UNDOCKING
);
352 * dock_in_progress - see if we are in the middle of handling a dock event
353 * @ds: the dock station
355 * Sometimes while docking, false dock events can be sent to the driver
356 * because good connections aren't made or some other reason. Ignore these
357 * if we are in the middle of doing something.
359 static int dock_in_progress(struct dock_station
*ds
)
361 if ((ds
->flags
& DOCK_DOCKING
) ||
362 time_before(jiffies
, (ds
->last_dock_time
+ HZ
)))
368 * handle_eject_request - handle an undock request checking for error conditions
369 * @ds: The dock station to undock.
370 * @event: The ACPI event number associated with the undock request.
372 * Check to make sure the dock device is still present, then undock and
373 * hotremove all the devices that may need removing.
375 static int handle_eject_request(struct dock_station
*ds
, u32 event
)
377 if (dock_in_progress(ds
))
381 * here we need to generate the undock
382 * event prior to actually doing the undock
383 * so that the device struct still exists.
384 * Also, even send the dock event if the
385 * device is not present anymore
387 dock_event(ds
, event
, UNDOCK_EVENT
);
389 hot_remove_dock_devices(ds
);
391 acpi_evaluate_lck(ds
->handle
, 0);
392 acpi_evaluate_ej0(ds
->handle
);
393 if (dock_present(ds
)) {
394 acpi_handle_err(ds
->handle
, "Unable to undock!\n");
402 * dock_notify - Handle ACPI dock notification.
403 * @adev: Dock station's ACPI device object.
404 * @event: Event code.
406 * If we are notified to dock, then check to see if the dock is
407 * present and then dock. Notify all drivers of the dock event,
408 * and then hotplug and devices that may need hotplugging.
410 int dock_notify(struct acpi_device
*adev
, u32 event
)
412 acpi_handle handle
= adev
->handle
;
413 struct dock_station
*ds
= find_dock_station(handle
);
414 int surprise_removal
= 0;
420 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
421 * is sent and _DCK is present, it is assumed to mean an undock
424 if ((ds
->flags
& DOCK_IS_DOCK
) && event
== ACPI_NOTIFY_DEVICE_CHECK
)
425 event
= ACPI_NOTIFY_EJECT_REQUEST
;
428 * dock station: BUS_CHECK - docked or surprise removal
429 * DEVICE_CHECK - undocked
430 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
432 * To simplify event handling, dock dependent device handler always
433 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
434 * ACPI_NOTIFY_EJECT_REQUEST for removal
437 case ACPI_NOTIFY_BUS_CHECK
:
438 case ACPI_NOTIFY_DEVICE_CHECK
:
439 if (!dock_in_progress(ds
) && !acpi_device_enumerated(adev
)) {
442 if (!dock_present(ds
)) {
443 acpi_handle_err(handle
, "Unable to dock!\n");
447 hotplug_dock_devices(ds
, event
);
449 dock_event(ds
, event
, DOCK_EVENT
);
450 acpi_evaluate_lck(ds
->handle
, 1);
451 acpi_update_all_gpes();
454 if (dock_present(ds
) || dock_in_progress(ds
))
456 /* This is a surprise removal */
457 surprise_removal
= 1;
458 event
= ACPI_NOTIFY_EJECT_REQUEST
;
461 case ACPI_NOTIFY_EJECT_REQUEST
:
463 if ((immediate_undock
&& !(ds
->flags
& DOCK_IS_ATA
))
465 handle_eject_request(ds
, event
);
467 dock_event(ds
, event
, UNDOCK_EVENT
);
474 * show_docked - read method for "docked" file in sysfs
476 static ssize_t
docked_show(struct device
*dev
,
477 struct device_attribute
*attr
, char *buf
)
479 struct dock_station
*dock_station
= dev
->platform_data
;
480 struct acpi_device
*adev
= acpi_fetch_acpi_dev(dock_station
->handle
);
482 return sysfs_emit(buf
, "%u\n", acpi_device_enumerated(adev
));
484 static DEVICE_ATTR_RO(docked
);
487 * show_flags - read method for flags file in sysfs
489 static ssize_t
flags_show(struct device
*dev
,
490 struct device_attribute
*attr
, char *buf
)
492 struct dock_station
*dock_station
= dev
->platform_data
;
494 return sysfs_emit(buf
, "%d\n", dock_station
->flags
);
497 static DEVICE_ATTR_RO(flags
);
500 * write_undock - write method for "undock" file in sysfs
502 static ssize_t
undock_store(struct device
*dev
, struct device_attribute
*attr
,
503 const char *buf
, size_t count
)
506 struct dock_station
*dock_station
= dev
->platform_data
;
511 acpi_scan_lock_acquire();
512 begin_undock(dock_station
);
513 ret
= handle_eject_request(dock_station
, ACPI_NOTIFY_EJECT_REQUEST
);
514 acpi_scan_lock_release();
515 return ret
? ret
: count
;
517 static DEVICE_ATTR_WO(undock
);
520 * show_dock_uid - read method for "uid" file in sysfs
522 static ssize_t
uid_show(struct device
*dev
,
523 struct device_attribute
*attr
, char *buf
)
525 unsigned long long lbuf
;
526 struct dock_station
*dock_station
= dev
->platform_data
;
528 acpi_status status
= acpi_evaluate_integer(dock_station
->handle
,
529 "_UID", NULL
, &lbuf
);
530 if (ACPI_FAILURE(status
))
533 return sysfs_emit(buf
, "%llx\n", lbuf
);
535 static DEVICE_ATTR_RO(uid
);
537 static ssize_t
type_show(struct device
*dev
,
538 struct device_attribute
*attr
, char *buf
)
540 struct dock_station
*dock_station
= dev
->platform_data
;
543 if (dock_station
->flags
& DOCK_IS_DOCK
)
544 type
= "dock_station";
545 else if (dock_station
->flags
& DOCK_IS_ATA
)
547 else if (dock_station
->flags
& DOCK_IS_BAT
)
548 type
= "battery_bay";
552 return sysfs_emit(buf
, "%s\n", type
);
554 static DEVICE_ATTR_RO(type
);
556 static struct attribute
*dock_attributes
[] = {
557 &dev_attr_docked
.attr
,
558 &dev_attr_flags
.attr
,
559 &dev_attr_undock
.attr
,
565 static const struct attribute_group dock_attribute_group
= {
566 .attrs
= dock_attributes
570 * acpi_dock_add - Add a new dock station
571 * @adev: Dock station ACPI device object.
573 * allocated and initialize a new dock station device.
575 void acpi_dock_add(struct acpi_device
*adev
)
577 struct dock_station
*dock_station
, ds
= { NULL
, };
578 struct platform_device_info pdevinfo
;
579 acpi_handle handle
= adev
->handle
;
580 struct platform_device
*dd
;
583 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
584 pdevinfo
.name
= "dock";
585 pdevinfo
.id
= dock_station_count
;
586 pdevinfo
.fwnode
= acpi_fwnode_handle(adev
);
588 pdevinfo
.size_data
= sizeof(ds
);
589 dd
= platform_device_register_full(&pdevinfo
);
593 dock_station
= dd
->dev
.platform_data
;
595 dock_station
->handle
= handle
;
596 dock_station
->dock_device
= dd
;
597 dock_station
->last_dock_time
= jiffies
- HZ
;
599 INIT_LIST_HEAD(&dock_station
->sibling
);
600 INIT_LIST_HEAD(&dock_station
->dependent_devices
);
602 /* we want the dock device to send uevents */
603 dev_set_uevent_suppress(&dd
->dev
, 0);
605 if (acpi_dock_match(handle
))
606 dock_station
->flags
|= DOCK_IS_DOCK
;
607 if (acpi_ata_match(handle
))
608 dock_station
->flags
|= DOCK_IS_ATA
;
609 if (acpi_device_is_battery(adev
))
610 dock_station
->flags
|= DOCK_IS_BAT
;
612 ret
= sysfs_create_group(&dd
->dev
.kobj
, &dock_attribute_group
);
616 /* add the dock station as a device dependent on itself */
617 ret
= add_dock_dependent_device(dock_station
, adev
);
621 dock_station_count
++;
622 list_add(&dock_station
->sibling
, &dock_stations
);
623 adev
->flags
.is_dock_station
= true;
624 dev_info(&adev
->dev
, "ACPI dock station (docks/bays count: %d)\n",
629 sysfs_remove_group(&dd
->dev
.kobj
, &dock_attribute_group
);
632 platform_device_unregister(dd
);
633 acpi_handle_err(handle
, "%s encountered error %d\n", __func__
, ret
);