1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dock.c - ACPI dock station driver
5 * Copyright (C) 2006, 2014, Intel Corp.
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;
92 acpi_lock_hp_context();
97 if (cb_type == DOCK_CALL_FIXUP) {
98 void (*fixup)(struct acpi_device *);
100 fixup = adev->hp->fixup;
102 acpi_unlock_hp_context();
106 } else if (cb_type == DOCK_CALL_UEVENT) {
107 void (*uevent)(struct acpi_device *, u32);
109 uevent = adev->hp->uevent;
111 acpi_unlock_hp_context();
116 int (*notify)(struct acpi_device *, u32);
118 notify = adev->hp->notify;
120 acpi_unlock_hp_context();
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)
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)
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 /*****************************************************************************
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
182 int is_dock_device(struct acpi_device *adev)
184 struct dock_station *dock_station;
186 if (!dock_station_count)
189 if (acpi_dock_match(adev->handle))
192 list_for_each_entry(dock_station, &dock_stations, sibling)
193 if (find_dock_dependent_device(dock_station, adev))
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;
213 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
214 if (ACPI_SUCCESS(status) && sta)
221 * hot_remove_dock_devices - Remove dock station devices.
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
233 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
234 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
236 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
237 acpi_bus_trim(dd->adev);
241 * hotplug_dock_devices - Insert devices on a dock station.
242 * @ds: the dock station
243 * @event: either bus check or device check request
245 * Some devices on the dock station need to have drivers called
246 * to perform hotplug operations after a dock event has occurred.
247 * Traverse the list of dock devices that have registered a
248 * hotplug handler, and call the handler.
250 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
252 struct dock_dependent_device *dd;
254 /* Call driver specific post-dock fixups. */
255 list_for_each_entry(dd, &ds->dependent_devices, list)
256 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
258 /* Call driver specific hotplug functions. */
259 list_for_each_entry(dd, &ds->dependent_devices, list)
260 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
263 * Check if all devices have been enumerated already. If not, run
264 * acpi_bus_scan() for them and that will cause scan handlers to be
265 * attached to device objects or acpi_drivers to be stopped/started if
268 list_for_each_entry(dd, &ds->dependent_devices, list) {
269 struct acpi_device *adev = dd->adev;
271 if (!acpi_device_enumerated(adev)) {
272 int ret = acpi_bus_scan(adev->handle);
274 dev_dbg(&adev->dev, "scan error %d\n", -ret);
279 static void dock_event(struct dock_station *ds, u32 event, int num)
281 struct device *dev = &ds->dock_device->dev;
282 char event_string[13];
283 char *envp[] = { event_string, NULL };
284 struct dock_dependent_device *dd;
286 if (num == UNDOCK_EVENT)
287 sprintf(event_string, "EVENT=undock");
289 sprintf(event_string, "EVENT=dock");
292 * Indicate that the status of the dock station has
295 if (num == DOCK_EVENT)
296 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
298 list_for_each_entry(dd, &ds->dependent_devices, list)
299 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
301 if (num != DOCK_EVENT)
302 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
306 * handle_dock - handle a dock event
307 * @ds: the dock station
308 * @dock: to dock, or undock - that is the question
310 * Execute the _DCK method in response to an acpi event
312 static void handle_dock(struct dock_station *ds, int dock)
315 struct acpi_object_list arg_list;
316 union acpi_object arg;
317 unsigned long long value;
319 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
321 /* _DCK method has one argument */
323 arg_list.pointer = &arg;
324 arg.type = ACPI_TYPE_INTEGER;
325 arg.integer.value = dock;
326 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
327 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
328 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
332 static inline void dock(struct dock_station *ds)
337 static inline void undock(struct dock_station *ds)
342 static inline void begin_dock(struct dock_station *ds)
344 ds->flags |= DOCK_DOCKING;
347 static inline void complete_dock(struct dock_station *ds)
349 ds->flags &= ~(DOCK_DOCKING);
350 ds->last_dock_time = jiffies;
353 static inline void begin_undock(struct dock_station *ds)
355 ds->flags |= DOCK_UNDOCKING;
358 static inline void complete_undock(struct dock_station *ds)
360 ds->flags &= ~(DOCK_UNDOCKING);
364 * dock_in_progress - see if we are in the middle of handling a dock event
365 * @ds: the dock station
367 * Sometimes while docking, false dock events can be sent to the driver
368 * because good connections aren't made or some other reason. Ignore these
369 * if we are in the middle of doing something.
371 static int dock_in_progress(struct dock_station *ds)
373 if ((ds->flags & DOCK_DOCKING) ||
374 time_before(jiffies, (ds->last_dock_time + HZ)))
380 * handle_eject_request - handle an undock request checking for error conditions
382 * Check to make sure the dock device is still present, then undock and
383 * hotremove all the devices that may need removing.
385 static int handle_eject_request(struct dock_station *ds, u32 event)
387 if (dock_in_progress(ds))
391 * here we need to generate the undock
392 * event prior to actually doing the undock
393 * so that the device struct still exists.
394 * Also, even send the dock event if the
395 * device is not present anymore
397 dock_event(ds, event, UNDOCK_EVENT);
399 hot_remove_dock_devices(ds);
401 acpi_evaluate_lck(ds->handle, 0);
402 acpi_evaluate_ej0(ds->handle);
403 if (dock_present(ds)) {
404 acpi_handle_err(ds->handle, "Unable to undock!\n");
412 * dock_notify - Handle ACPI dock notification.
413 * @adev: Dock station's ACPI device object.
414 * @event: Event code.
416 * If we are notified to dock, then check to see if the dock is
417 * present and then dock. Notify all drivers of the dock event,
418 * and then hotplug and devices that may need hotplugging.
420 int dock_notify(struct acpi_device *adev, u32 event)
422 acpi_handle handle = adev->handle;
423 struct dock_station *ds = find_dock_station(handle);
424 int surprise_removal = 0;
430 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
431 * is sent and _DCK is present, it is assumed to mean an undock
434 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
435 event = ACPI_NOTIFY_EJECT_REQUEST;
438 * dock station: BUS_CHECK - docked or surprise removal
439 * DEVICE_CHECK - undocked
440 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
442 * To simplify event handling, dock dependent device handler always
443 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
444 * ACPI_NOTIFY_EJECT_REQUEST for removal
447 case ACPI_NOTIFY_BUS_CHECK:
448 case ACPI_NOTIFY_DEVICE_CHECK:
449 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
452 if (!dock_present(ds)) {
453 acpi_handle_err(handle, "Unable to dock!\n");
457 hotplug_dock_devices(ds, event);
459 dock_event(ds, event, DOCK_EVENT);
460 acpi_evaluate_lck(ds->handle, 1);
461 acpi_update_all_gpes();
464 if (dock_present(ds) || dock_in_progress(ds))
466 /* This is a surprise removal */
467 surprise_removal = 1;
468 event = ACPI_NOTIFY_EJECT_REQUEST;
471 case ACPI_NOTIFY_EJECT_REQUEST:
473 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
475 handle_eject_request(ds, event);
477 dock_event(ds, event, UNDOCK_EVENT);
484 * show_docked - read method for "docked" file in sysfs
486 static ssize_t show_docked(struct device *dev,
487 struct device_attribute *attr, char *buf)
489 struct dock_station *dock_station = dev->platform_data;
490 struct acpi_device *adev = NULL;
492 acpi_bus_get_device(dock_station->handle, &adev);
493 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
495 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
498 * show_flags - read method for flags file in sysfs
500 static ssize_t show_flags(struct device *dev,
501 struct device_attribute *attr, char *buf)
503 struct dock_station *dock_station = dev->platform_data;
504 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
507 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
510 * write_undock - write method for "undock" file in sysfs
512 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
513 const char *buf, size_t count)
516 struct dock_station *dock_station = dev->platform_data;
521 acpi_scan_lock_acquire();
522 begin_undock(dock_station);
523 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
524 acpi_scan_lock_release();
525 return ret ? ret: count;
527 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
530 * show_dock_uid - read method for "uid" file in sysfs
532 static ssize_t show_dock_uid(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 unsigned long long lbuf;
536 struct dock_station *dock_station = dev->platform_data;
537 acpi_status status = acpi_evaluate_integer(dock_station->handle,
538 "_UID", NULL, &lbuf);
539 if (ACPI_FAILURE(status))
542 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
544 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
546 static ssize_t show_dock_type(struct device *dev,
547 struct device_attribute *attr, char *buf)
549 struct dock_station *dock_station = dev->platform_data;
552 if (dock_station->flags & DOCK_IS_DOCK)
553 type = "dock_station";
554 else if (dock_station->flags & DOCK_IS_ATA)
556 else if (dock_station->flags & DOCK_IS_BAT)
557 type = "battery_bay";
561 return snprintf(buf, PAGE_SIZE, "%s\n", type);
563 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
565 static struct attribute *dock_attributes[] = {
566 &dev_attr_docked.attr,
567 &dev_attr_flags.attr,
568 &dev_attr_undock.attr,
574 static const struct attribute_group dock_attribute_group = {
575 .attrs = dock_attributes
579 * acpi_dock_add - Add a new dock station
580 * @adev: Dock station ACPI device object.
582 * allocated and initialize a new dock station device.
584 void acpi_dock_add(struct acpi_device *adev)
586 struct dock_station *dock_station, ds = { NULL, };
587 struct platform_device_info pdevinfo;
588 acpi_handle handle = adev->handle;
589 struct platform_device *dd;
592 memset(&pdevinfo, 0, sizeof(pdevinfo));
593 pdevinfo.name = "dock";
594 pdevinfo.id = dock_station_count;
595 pdevinfo.fwnode = acpi_fwnode_handle(adev);
597 pdevinfo.size_data = sizeof(ds);
598 dd = platform_device_register_full(&pdevinfo);
602 dock_station = dd->dev.platform_data;
604 dock_station->handle = handle;
605 dock_station->dock_device = dd;
606 dock_station->last_dock_time = jiffies - HZ;
608 INIT_LIST_HEAD(&dock_station->sibling);
609 INIT_LIST_HEAD(&dock_station->dependent_devices);
611 /* we want the dock device to send uevents */
612 dev_set_uevent_suppress(&dd->dev, 0);
614 if (acpi_dock_match(handle))
615 dock_station->flags |= DOCK_IS_DOCK;
616 if (acpi_ata_match(handle))
617 dock_station->flags |= DOCK_IS_ATA;
618 if (acpi_device_is_battery(adev))
619 dock_station->flags |= DOCK_IS_BAT;
621 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
625 /* add the dock station as a device dependent on itself */
626 ret = add_dock_dependent_device(dock_station, adev);
630 dock_station_count++;
631 list_add(&dock_station->sibling, &dock_stations);
632 adev->flags.is_dock_station = true;
633 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
638 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
641 platform_device_unregister(dd);
642 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);