2 * dock.c - ACPI dock station driver
4 * Copyright (C) 2006, 2014, Intel Corp.
5 * Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
6 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #include <linux/kernel.h>
24 #include <linux/moduleparam.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/notifier.h>
29 #include <linux/platform_device.h>
30 #include <linux/jiffies.h>
31 #include <linux/stddef.h>
32 #include <linux/acpi.h>
36 ACPI_MODULE_NAME("dock");
38 static bool immediate_undock = 1;
39 module_param(immediate_undock, bool, 0644);
40 MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
41 "undock immediately when the undock button is pressed, 0 will cause"
42 " the driver to wait for userspace to write the undock sysfs file "
47 unsigned long last_dock_time;
49 struct list_head dependent_devices;
51 struct list_head sibling;
52 struct platform_device *dock_device;
54 static LIST_HEAD(dock_stations);
55 static int dock_station_count;
57 struct dock_dependent_device {
58 struct list_head list;
59 struct acpi_device *adev;
62 #define DOCK_DOCKING 0x00000001
63 #define DOCK_UNDOCKING 0x00000002
64 #define DOCK_IS_DOCK 0x00000010
65 #define DOCK_IS_ATA 0x00000020
66 #define DOCK_IS_BAT 0x00000040
68 #define UNDOCK_EVENT 2
70 enum dock_callback_type {
76 /*****************************************************************************
77 * Dock Dependent device functions *
78 *****************************************************************************/
80 * add_dock_dependent_device - associate a device with the dock station
82 * @adev: Dependent ACPI device object.
84 * Add the dependent device to the dock's dependent device list.
86 static int add_dock_dependent_device(struct dock_station *ds,
87 struct acpi_device *adev)
89 struct dock_dependent_device *dd;
91 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
96 INIT_LIST_HEAD(&dd->list);
97 list_add_tail(&dd->list, &ds->dependent_devices);
102 static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
103 enum dock_callback_type cb_type)
105 struct acpi_device *adev = dd->adev;
107 acpi_lock_hp_context();
112 if (cb_type == DOCK_CALL_FIXUP) {
113 void (*fixup)(struct acpi_device *);
115 fixup = adev->hp->fixup;
117 acpi_unlock_hp_context();
121 } else if (cb_type == DOCK_CALL_UEVENT) {
122 void (*uevent)(struct acpi_device *, u32);
124 uevent = adev->hp->uevent;
126 acpi_unlock_hp_context();
131 int (*notify)(struct acpi_device *, u32);
133 notify = adev->hp->notify;
135 acpi_unlock_hp_context();
142 acpi_unlock_hp_context();
145 static struct dock_station *find_dock_station(acpi_handle handle)
147 struct dock_station *ds;
149 list_for_each_entry(ds, &dock_stations, sibling)
150 if (ds->handle == handle)
157 * find_dock_dependent_device - get a device dependent on this dock
158 * @ds: the dock station
159 * @adev: ACPI device object to find.
161 * iterate over the dependent device list for this dock. If the
162 * dependent device matches the handle, return.
164 static struct dock_dependent_device *
165 find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
167 struct dock_dependent_device *dd;
169 list_for_each_entry(dd, &ds->dependent_devices, list)
170 if (adev == dd->adev)
176 void register_dock_dependent_device(struct acpi_device *adev,
177 acpi_handle dshandle)
179 struct dock_station *ds = find_dock_station(dshandle);
181 if (ds && !find_dock_dependent_device(ds, adev))
182 add_dock_dependent_device(ds, adev);
185 /*****************************************************************************
187 *****************************************************************************/
190 * is_dock_device - see if a device is on a dock station
191 * @adev: ACPI device object to check.
193 * If this device is either the dock station itself,
194 * or is a device dependent on the dock station, then it
197 int is_dock_device(struct acpi_device *adev)
199 struct dock_station *dock_station;
201 if (!dock_station_count)
204 if (acpi_dock_match(adev->handle))
207 list_for_each_entry(dock_station, &dock_stations, sibling)
208 if (find_dock_dependent_device(dock_station, adev))
213 EXPORT_SYMBOL_GPL(is_dock_device);
216 * dock_present - see if the dock station is present.
217 * @ds: the dock station
219 * execute the _STA method. note that present does not
220 * imply that we are docked.
222 static int dock_present(struct dock_station *ds)
224 unsigned long long sta;
228 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
229 if (ACPI_SUCCESS(status) && sta)
236 * hot_remove_dock_devices - Remove dock station devices.
239 static void hot_remove_dock_devices(struct dock_station *ds)
241 struct dock_dependent_device *dd;
244 * Walk the list in reverse order so that devices that have been added
245 * last are removed first (in case there are some indirect dependencies
248 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
249 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
251 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
252 acpi_bus_trim(dd->adev);
256 * hotplug_dock_devices - Insert devices on a dock station.
257 * @ds: the dock station
258 * @event: either bus check or device check request
260 * Some devices on the dock station need to have drivers called
261 * to perform hotplug operations after a dock event has occurred.
262 * Traverse the list of dock devices that have registered a
263 * hotplug handler, and call the handler.
265 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
267 struct dock_dependent_device *dd;
269 /* Call driver specific post-dock fixups. */
270 list_for_each_entry(dd, &ds->dependent_devices, list)
271 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
273 /* Call driver specific hotplug functions. */
274 list_for_each_entry(dd, &ds->dependent_devices, list)
275 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
278 * Check if all devices have been enumerated already. If not, run
279 * acpi_bus_scan() for them and that will cause scan handlers to be
280 * attached to device objects or acpi_drivers to be stopped/started if
283 list_for_each_entry(dd, &ds->dependent_devices, list) {
284 struct acpi_device *adev = dd->adev;
286 if (!acpi_device_enumerated(adev)) {
287 int ret = acpi_bus_scan(adev->handle);
289 dev_dbg(&adev->dev, "scan error %d\n", -ret);
294 static void dock_event(struct dock_station *ds, u32 event, int num)
296 struct device *dev = &ds->dock_device->dev;
297 char event_string[13];
298 char *envp[] = { event_string, NULL };
299 struct dock_dependent_device *dd;
301 if (num == UNDOCK_EVENT)
302 sprintf(event_string, "EVENT=undock");
304 sprintf(event_string, "EVENT=dock");
307 * Indicate that the status of the dock station has
310 if (num == DOCK_EVENT)
311 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
313 list_for_each_entry(dd, &ds->dependent_devices, list)
314 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
316 if (num != DOCK_EVENT)
317 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
321 * handle_dock - handle a dock event
322 * @ds: the dock station
323 * @dock: to dock, or undock - that is the question
325 * Execute the _DCK method in response to an acpi event
327 static void handle_dock(struct dock_station *ds, int dock)
330 struct acpi_object_list arg_list;
331 union acpi_object arg;
332 unsigned long long value;
334 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
336 /* _DCK method has one argument */
338 arg_list.pointer = &arg;
339 arg.type = ACPI_TYPE_INTEGER;
340 arg.integer.value = dock;
341 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
342 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
343 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
347 static inline void dock(struct dock_station *ds)
352 static inline void undock(struct dock_station *ds)
357 static inline void begin_dock(struct dock_station *ds)
359 ds->flags |= DOCK_DOCKING;
362 static inline void complete_dock(struct dock_station *ds)
364 ds->flags &= ~(DOCK_DOCKING);
365 ds->last_dock_time = jiffies;
368 static inline void begin_undock(struct dock_station *ds)
370 ds->flags |= DOCK_UNDOCKING;
373 static inline void complete_undock(struct dock_station *ds)
375 ds->flags &= ~(DOCK_UNDOCKING);
379 * dock_in_progress - see if we are in the middle of handling a dock event
380 * @ds: the dock station
382 * Sometimes while docking, false dock events can be sent to the driver
383 * because good connections aren't made or some other reason. Ignore these
384 * if we are in the middle of doing something.
386 static int dock_in_progress(struct dock_station *ds)
388 if ((ds->flags & DOCK_DOCKING) ||
389 time_before(jiffies, (ds->last_dock_time + HZ)))
395 * handle_eject_request - handle an undock request checking for error conditions
397 * Check to make sure the dock device is still present, then undock and
398 * hotremove all the devices that may need removing.
400 static int handle_eject_request(struct dock_station *ds, u32 event)
402 if (dock_in_progress(ds))
406 * here we need to generate the undock
407 * event prior to actually doing the undock
408 * so that the device struct still exists.
409 * Also, even send the dock event if the
410 * device is not present anymore
412 dock_event(ds, event, UNDOCK_EVENT);
414 hot_remove_dock_devices(ds);
416 acpi_evaluate_lck(ds->handle, 0);
417 acpi_evaluate_ej0(ds->handle);
418 if (dock_present(ds)) {
419 acpi_handle_err(ds->handle, "Unable to undock!\n");
427 * dock_notify - Handle ACPI dock notification.
428 * @adev: Dock station's ACPI device object.
429 * @event: Event code.
431 * If we are notified to dock, then check to see if the dock is
432 * present and then dock. Notify all drivers of the dock event,
433 * and then hotplug and devices that may need hotplugging.
435 int dock_notify(struct acpi_device *adev, u32 event)
437 acpi_handle handle = adev->handle;
438 struct dock_station *ds = find_dock_station(handle);
439 int surprise_removal = 0;
445 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
446 * is sent and _DCK is present, it is assumed to mean an undock
449 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
450 event = ACPI_NOTIFY_EJECT_REQUEST;
453 * dock station: BUS_CHECK - docked or surprise removal
454 * DEVICE_CHECK - undocked
455 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
457 * To simplify event handling, dock dependent device handler always
458 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
459 * ACPI_NOTIFY_EJECT_REQUEST for removal
462 case ACPI_NOTIFY_BUS_CHECK:
463 case ACPI_NOTIFY_DEVICE_CHECK:
464 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
467 if (!dock_present(ds)) {
468 acpi_handle_err(handle, "Unable to dock!\n");
472 hotplug_dock_devices(ds, event);
474 dock_event(ds, event, DOCK_EVENT);
475 acpi_evaluate_lck(ds->handle, 1);
476 acpi_update_all_gpes();
479 if (dock_present(ds) || dock_in_progress(ds))
481 /* This is a surprise removal */
482 surprise_removal = 1;
483 event = ACPI_NOTIFY_EJECT_REQUEST;
485 case ACPI_NOTIFY_EJECT_REQUEST:
487 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
489 handle_eject_request(ds, event);
491 dock_event(ds, event, UNDOCK_EVENT);
498 * show_docked - read method for "docked" file in sysfs
500 static ssize_t show_docked(struct device *dev,
501 struct device_attribute *attr, char *buf)
503 struct dock_station *dock_station = dev->platform_data;
504 struct acpi_device *adev = NULL;
506 acpi_bus_get_device(dock_station->handle, &adev);
507 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
509 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
512 * show_flags - read method for flags file in sysfs
514 static ssize_t show_flags(struct device *dev,
515 struct device_attribute *attr, char *buf)
517 struct dock_station *dock_station = dev->platform_data;
518 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
521 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
524 * write_undock - write method for "undock" file in sysfs
526 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
530 struct dock_station *dock_station = dev->platform_data;
535 acpi_scan_lock_acquire();
536 begin_undock(dock_station);
537 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
538 acpi_scan_lock_release();
539 return ret ? ret: count;
541 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
544 * show_dock_uid - read method for "uid" file in sysfs
546 static ssize_t show_dock_uid(struct device *dev,
547 struct device_attribute *attr, char *buf)
549 unsigned long long lbuf;
550 struct dock_station *dock_station = dev->platform_data;
551 acpi_status status = acpi_evaluate_integer(dock_station->handle,
552 "_UID", NULL, &lbuf);
553 if (ACPI_FAILURE(status))
556 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
558 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
560 static ssize_t show_dock_type(struct device *dev,
561 struct device_attribute *attr, char *buf)
563 struct dock_station *dock_station = dev->platform_data;
566 if (dock_station->flags & DOCK_IS_DOCK)
567 type = "dock_station";
568 else if (dock_station->flags & DOCK_IS_ATA)
570 else if (dock_station->flags & DOCK_IS_BAT)
571 type = "battery_bay";
575 return snprintf(buf, PAGE_SIZE, "%s\n", type);
577 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
579 static struct attribute *dock_attributes[] = {
580 &dev_attr_docked.attr,
581 &dev_attr_flags.attr,
582 &dev_attr_undock.attr,
588 static const struct attribute_group dock_attribute_group = {
589 .attrs = dock_attributes
593 * acpi_dock_add - Add a new dock station
594 * @adev: Dock station ACPI device object.
596 * allocated and initialize a new dock station device.
598 void acpi_dock_add(struct acpi_device *adev)
600 struct dock_station *dock_station, ds = { NULL, };
601 struct platform_device_info pdevinfo;
602 acpi_handle handle = adev->handle;
603 struct platform_device *dd;
606 memset(&pdevinfo, 0, sizeof(pdevinfo));
607 pdevinfo.name = "dock";
608 pdevinfo.id = dock_station_count;
609 pdevinfo.fwnode = acpi_fwnode_handle(adev);
611 pdevinfo.size_data = sizeof(ds);
612 dd = platform_device_register_full(&pdevinfo);
616 dock_station = dd->dev.platform_data;
618 dock_station->handle = handle;
619 dock_station->dock_device = dd;
620 dock_station->last_dock_time = jiffies - HZ;
622 INIT_LIST_HEAD(&dock_station->sibling);
623 INIT_LIST_HEAD(&dock_station->dependent_devices);
625 /* we want the dock device to send uevents */
626 dev_set_uevent_suppress(&dd->dev, 0);
628 if (acpi_dock_match(handle))
629 dock_station->flags |= DOCK_IS_DOCK;
630 if (acpi_ata_match(handle))
631 dock_station->flags |= DOCK_IS_ATA;
632 if (acpi_device_is_battery(adev))
633 dock_station->flags |= DOCK_IS_BAT;
635 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
639 /* add the dock station as a device dependent on itself */
640 ret = add_dock_dependent_device(dock_station, adev);
644 dock_station_count++;
645 list_add(&dock_station->sibling, &dock_stations);
646 adev->flags.is_dock_station = true;
647 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
652 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
655 platform_device_unregister(dd);
656 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);