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 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/notifier.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/stddef.h>
36 #include <linux/acpi.h>
40 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
42 ACPI_MODULE_NAME("dock");
43 MODULE_AUTHOR("Kristen Carlson Accardi");
44 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
45 MODULE_LICENSE("GPL");
47 static bool immediate_undock = 1;
48 module_param(immediate_undock, bool, 0644);
49 MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
50 "undock immediately when the undock button is pressed, 0 will cause"
51 " the driver to wait for userspace to write the undock sysfs file "
54 static const struct acpi_device_id dock_device_ids[] = {
58 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
62 unsigned long last_dock_time;
64 struct list_head dependent_devices;
66 struct list_head sibling;
67 struct platform_device *dock_device;
69 static LIST_HEAD(dock_stations);
70 static int dock_station_count;
72 struct dock_dependent_device {
73 struct list_head list;
74 struct acpi_device *adev;
77 #define DOCK_DOCKING 0x00000001
78 #define DOCK_UNDOCKING 0x00000002
79 #define DOCK_IS_DOCK 0x00000010
80 #define DOCK_IS_ATA 0x00000020
81 #define DOCK_IS_BAT 0x00000040
83 #define UNDOCK_EVENT 2
85 enum dock_callback_type {
91 /*****************************************************************************
92 * Dock Dependent device functions *
93 *****************************************************************************/
95 * add_dock_dependent_device - associate a device with the dock station
97 * @adev: Dependent ACPI device object.
99 * Add the dependent device to the dock's dependent device list.
101 static int add_dock_dependent_device(struct dock_station *ds,
102 struct acpi_device *adev)
104 struct dock_dependent_device *dd;
106 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
111 INIT_LIST_HEAD(&dd->list);
112 list_add_tail(&dd->list, &ds->dependent_devices);
117 static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
118 enum dock_callback_type cb_type)
120 struct acpi_device *adev = dd->adev;
122 acpi_lock_hp_context();
127 if (cb_type == DOCK_CALL_FIXUP) {
128 void (*fixup)(struct acpi_device *);
130 fixup = adev->hp->fixup;
132 acpi_unlock_hp_context();
136 } else if (cb_type == DOCK_CALL_UEVENT) {
137 void (*uevent)(struct acpi_device *, u32);
139 uevent = adev->hp->uevent;
141 acpi_unlock_hp_context();
146 int (*notify)(struct acpi_device *, u32);
148 notify = adev->hp->notify;
150 acpi_unlock_hp_context();
157 acpi_unlock_hp_context();
160 static struct dock_station *find_dock_station(acpi_handle handle)
162 struct dock_station *ds;
164 list_for_each_entry(ds, &dock_stations, sibling)
165 if (ds->handle == handle)
172 * find_dock_dependent_device - get a device dependent on this dock
173 * @ds: the dock station
174 * @adev: ACPI device object to find.
176 * iterate over the dependent device list for this dock. If the
177 * dependent device matches the handle, return.
179 static struct dock_dependent_device *
180 find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
182 struct dock_dependent_device *dd;
184 list_for_each_entry(dd, &ds->dependent_devices, list)
185 if (adev == dd->adev)
191 void register_dock_dependent_device(struct acpi_device *adev,
192 acpi_handle dshandle)
194 struct dock_station *ds = find_dock_station(dshandle);
196 if (ds && !find_dock_dependent_device(ds, adev))
197 add_dock_dependent_device(ds, adev);
200 /*****************************************************************************
202 *****************************************************************************/
205 * is_dock_device - see if a device is on a dock station
206 * @adev: ACPI device object to check.
208 * If this device is either the dock station itself,
209 * or is a device dependent on the dock station, then it
212 int is_dock_device(struct acpi_device *adev)
214 struct dock_station *dock_station;
216 if (!dock_station_count)
219 if (acpi_dock_match(adev->handle))
222 list_for_each_entry(dock_station, &dock_stations, sibling)
223 if (find_dock_dependent_device(dock_station, adev))
228 EXPORT_SYMBOL_GPL(is_dock_device);
231 * dock_present - see if the dock station is present.
232 * @ds: the dock station
234 * execute the _STA method. note that present does not
235 * imply that we are docked.
237 static int dock_present(struct dock_station *ds)
239 unsigned long long sta;
243 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
244 if (ACPI_SUCCESS(status) && sta)
251 * hot_remove_dock_devices - Remove dock station devices.
254 static void hot_remove_dock_devices(struct dock_station *ds)
256 struct dock_dependent_device *dd;
259 * Walk the list in reverse order so that devices that have been added
260 * last are removed first (in case there are some indirect dependencies
263 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
264 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
266 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
267 acpi_bus_trim(dd->adev);
271 * hotplug_dock_devices - Insert devices on a dock station.
272 * @ds: the dock station
273 * @event: either bus check or device check request
275 * Some devices on the dock station need to have drivers called
276 * to perform hotplug operations after a dock event has occurred.
277 * Traverse the list of dock devices that have registered a
278 * hotplug handler, and call the handler.
280 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
282 struct dock_dependent_device *dd;
284 /* Call driver specific post-dock fixups. */
285 list_for_each_entry(dd, &ds->dependent_devices, list)
286 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
288 /* Call driver specific hotplug functions. */
289 list_for_each_entry(dd, &ds->dependent_devices, list)
290 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
293 * Check if all devices have been enumerated already. If not, run
294 * acpi_bus_scan() for them and that will cause scan handlers to be
295 * attached to device objects or acpi_drivers to be stopped/started if
298 list_for_each_entry(dd, &ds->dependent_devices, list) {
299 struct acpi_device *adev = dd->adev;
301 if (!acpi_device_enumerated(adev)) {
302 int ret = acpi_bus_scan(adev->handle);
304 dev_dbg(&adev->dev, "scan error %d\n", -ret);
309 static void dock_event(struct dock_station *ds, u32 event, int num)
311 struct device *dev = &ds->dock_device->dev;
312 char event_string[13];
313 char *envp[] = { event_string, NULL };
314 struct dock_dependent_device *dd;
316 if (num == UNDOCK_EVENT)
317 sprintf(event_string, "EVENT=undock");
319 sprintf(event_string, "EVENT=dock");
322 * Indicate that the status of the dock station has
325 if (num == DOCK_EVENT)
326 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
328 list_for_each_entry(dd, &ds->dependent_devices, list)
329 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
331 if (num != DOCK_EVENT)
332 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
336 * handle_dock - handle a dock event
337 * @ds: the dock station
338 * @dock: to dock, or undock - that is the question
340 * Execute the _DCK method in response to an acpi event
342 static void handle_dock(struct dock_station *ds, int dock)
345 struct acpi_object_list arg_list;
346 union acpi_object arg;
347 unsigned long long value;
349 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
351 /* _DCK method has one argument */
353 arg_list.pointer = &arg;
354 arg.type = ACPI_TYPE_INTEGER;
355 arg.integer.value = dock;
356 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
357 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
358 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
362 static inline void dock(struct dock_station *ds)
367 static inline void undock(struct dock_station *ds)
372 static inline void begin_dock(struct dock_station *ds)
374 ds->flags |= DOCK_DOCKING;
377 static inline void complete_dock(struct dock_station *ds)
379 ds->flags &= ~(DOCK_DOCKING);
380 ds->last_dock_time = jiffies;
383 static inline void begin_undock(struct dock_station *ds)
385 ds->flags |= DOCK_UNDOCKING;
388 static inline void complete_undock(struct dock_station *ds)
390 ds->flags &= ~(DOCK_UNDOCKING);
394 * dock_in_progress - see if we are in the middle of handling a dock event
395 * @ds: the dock station
397 * Sometimes while docking, false dock events can be sent to the driver
398 * because good connections aren't made or some other reason. Ignore these
399 * if we are in the middle of doing something.
401 static int dock_in_progress(struct dock_station *ds)
403 if ((ds->flags & DOCK_DOCKING) ||
404 time_before(jiffies, (ds->last_dock_time + HZ)))
410 * handle_eject_request - handle an undock request checking for error conditions
412 * Check to make sure the dock device is still present, then undock and
413 * hotremove all the devices that may need removing.
415 static int handle_eject_request(struct dock_station *ds, u32 event)
417 if (dock_in_progress(ds))
421 * here we need to generate the undock
422 * event prior to actually doing the undock
423 * so that the device struct still exists.
424 * Also, even send the dock event if the
425 * device is not present anymore
427 dock_event(ds, event, UNDOCK_EVENT);
429 hot_remove_dock_devices(ds);
431 acpi_evaluate_lck(ds->handle, 0);
432 acpi_evaluate_ej0(ds->handle);
433 if (dock_present(ds)) {
434 acpi_handle_err(ds->handle, "Unable to undock!\n");
442 * dock_notify - Handle ACPI dock notification.
443 * @adev: Dock station's ACPI device object.
444 * @event: Event code.
446 * If we are notified to dock, then check to see if the dock is
447 * present and then dock. Notify all drivers of the dock event,
448 * and then hotplug and devices that may need hotplugging.
450 int dock_notify(struct acpi_device *adev, u32 event)
452 acpi_handle handle = adev->handle;
453 struct dock_station *ds = find_dock_station(handle);
454 int surprise_removal = 0;
460 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
461 * is sent and _DCK is present, it is assumed to mean an undock
464 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
465 event = ACPI_NOTIFY_EJECT_REQUEST;
468 * dock station: BUS_CHECK - docked or surprise removal
469 * DEVICE_CHECK - undocked
470 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
472 * To simplify event handling, dock dependent device handler always
473 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
474 * ACPI_NOTIFY_EJECT_REQUEST for removal
477 case ACPI_NOTIFY_BUS_CHECK:
478 case ACPI_NOTIFY_DEVICE_CHECK:
479 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
482 if (!dock_present(ds)) {
483 acpi_handle_err(handle, "Unable to dock!\n");
487 hotplug_dock_devices(ds, event);
489 dock_event(ds, event, DOCK_EVENT);
490 acpi_evaluate_lck(ds->handle, 1);
491 acpi_update_all_gpes();
494 if (dock_present(ds) || dock_in_progress(ds))
496 /* This is a surprise removal */
497 surprise_removal = 1;
498 event = ACPI_NOTIFY_EJECT_REQUEST;
500 case ACPI_NOTIFY_EJECT_REQUEST:
502 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
504 handle_eject_request(ds, event);
506 dock_event(ds, event, UNDOCK_EVENT);
513 * show_docked - read method for "docked" file in sysfs
515 static ssize_t show_docked(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct dock_station *dock_station = dev->platform_data;
519 struct acpi_device *adev = NULL;
521 acpi_bus_get_device(dock_station->handle, &adev);
522 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
524 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
527 * show_flags - read method for flags file in sysfs
529 static ssize_t show_flags(struct device *dev,
530 struct device_attribute *attr, char *buf)
532 struct dock_station *dock_station = dev->platform_data;
533 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
536 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
539 * write_undock - write method for "undock" file in sysfs
541 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
545 struct dock_station *dock_station = dev->platform_data;
550 acpi_scan_lock_acquire();
551 begin_undock(dock_station);
552 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
553 acpi_scan_lock_release();
554 return ret ? ret: count;
556 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
559 * show_dock_uid - read method for "uid" file in sysfs
561 static ssize_t show_dock_uid(struct device *dev,
562 struct device_attribute *attr, char *buf)
564 unsigned long long lbuf;
565 struct dock_station *dock_station = dev->platform_data;
566 acpi_status status = acpi_evaluate_integer(dock_station->handle,
567 "_UID", NULL, &lbuf);
568 if (ACPI_FAILURE(status))
571 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
573 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
575 static ssize_t show_dock_type(struct device *dev,
576 struct device_attribute *attr, char *buf)
578 struct dock_station *dock_station = dev->platform_data;
581 if (dock_station->flags & DOCK_IS_DOCK)
582 type = "dock_station";
583 else if (dock_station->flags & DOCK_IS_ATA)
585 else if (dock_station->flags & DOCK_IS_BAT)
586 type = "battery_bay";
590 return snprintf(buf, PAGE_SIZE, "%s\n", type);
592 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
594 static struct attribute *dock_attributes[] = {
595 &dev_attr_docked.attr,
596 &dev_attr_flags.attr,
597 &dev_attr_undock.attr,
603 static struct attribute_group dock_attribute_group = {
604 .attrs = dock_attributes
608 * acpi_dock_add - Add a new dock station
609 * @adev: Dock station ACPI device object.
611 * allocated and initialize a new dock station device.
613 void acpi_dock_add(struct acpi_device *adev)
615 struct dock_station *dock_station, ds = { NULL, };
616 struct platform_device_info pdevinfo;
617 acpi_handle handle = adev->handle;
618 struct platform_device *dd;
621 memset(&pdevinfo, 0, sizeof(pdevinfo));
622 pdevinfo.name = "dock";
623 pdevinfo.id = dock_station_count;
624 pdevinfo.acpi_node.companion = adev;
626 pdevinfo.size_data = sizeof(ds);
627 dd = platform_device_register_full(&pdevinfo);
631 dock_station = dd->dev.platform_data;
633 dock_station->handle = handle;
634 dock_station->dock_device = dd;
635 dock_station->last_dock_time = jiffies - HZ;
637 INIT_LIST_HEAD(&dock_station->sibling);
638 INIT_LIST_HEAD(&dock_station->dependent_devices);
640 /* we want the dock device to send uevents */
641 dev_set_uevent_suppress(&dd->dev, 0);
643 if (acpi_dock_match(handle))
644 dock_station->flags |= DOCK_IS_DOCK;
645 if (acpi_ata_match(handle))
646 dock_station->flags |= DOCK_IS_ATA;
647 if (acpi_device_is_battery(adev))
648 dock_station->flags |= DOCK_IS_BAT;
650 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
654 /* add the dock station as a device dependent on itself */
655 ret = add_dock_dependent_device(dock_station, adev);
659 dock_station_count++;
660 list_add(&dock_station->sibling, &dock_stations);
661 adev->flags.is_dock_station = true;
662 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
667 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
670 platform_device_unregister(dd);
671 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);