2 * dock.c - ACPI dock station driver
4 * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/notifier.h>
30 #include <linux/platform_device.h>
31 #include <linux/jiffies.h>
32 #include <linux/stddef.h>
33 #include <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
36 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
38 ACPI_MODULE_NAME("dock");
39 MODULE_AUTHOR("Kristen Carlson Accardi");
40 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
41 MODULE_LICENSE("GPL");
43 static struct atomic_notifier_head dock_notifier_list;
44 static struct platform_device dock_device;
45 static char dock_device_name[] = "dock";
49 unsigned long last_dock_time;
53 struct list_head dependent_devices;
54 struct list_head hotplug_devices;
57 struct dock_dependent_device {
58 struct list_head list;
59 struct list_head hotplug_list;
61 acpi_notify_handler handler;
65 #define DOCK_DOCKING 0x00000001
67 #define UNDOCK_EVENT 2
69 static struct dock_station *dock_station;
71 /*****************************************************************************
72 * Dock Dependent device functions *
73 *****************************************************************************/
75 * alloc_dock_dependent_device - allocate and init a dependent device
76 * @handle: the acpi_handle of the dependent device
78 * Allocate memory for a dependent device structure for a device referenced
81 static struct dock_dependent_device *
82 alloc_dock_dependent_device(acpi_handle handle)
84 struct dock_dependent_device *dd;
86 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
89 INIT_LIST_HEAD(&dd->list);
90 INIT_LIST_HEAD(&dd->hotplug_list);
96 * add_dock_dependent_device - associate a device with the dock station
97 * @ds: The dock station
98 * @dd: The dependent device
100 * Add the dependent device to the dock's dependent device list.
103 add_dock_dependent_device(struct dock_station *ds,
104 struct dock_dependent_device *dd)
106 spin_lock(&ds->dd_lock);
107 list_add_tail(&dd->list, &ds->dependent_devices);
108 spin_unlock(&ds->dd_lock);
112 * dock_add_hotplug_device - associate a hotplug handler with the dock station
113 * @ds: The dock station
114 * @dd: The dependent device struct
116 * Add the dependent device to the dock's hotplug device list
119 dock_add_hotplug_device(struct dock_station *ds,
120 struct dock_dependent_device *dd)
122 mutex_lock(&ds->hp_lock);
123 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
124 mutex_unlock(&ds->hp_lock);
128 * dock_del_hotplug_device - remove a hotplug handler from the dock station
129 * @ds: The dock station
130 * @dd: the dependent device struct
132 * Delete the dependent device from the dock's hotplug device list
135 dock_del_hotplug_device(struct dock_station *ds,
136 struct dock_dependent_device *dd)
138 mutex_lock(&ds->hp_lock);
139 list_del(&dd->hotplug_list);
140 mutex_unlock(&ds->hp_lock);
144 * find_dock_dependent_device - get a device dependent on this dock
145 * @ds: the dock station
146 * @handle: the acpi_handle of the device we want
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, acpi_handle handle)
154 struct dock_dependent_device *dd;
156 spin_lock(&ds->dd_lock);
157 list_for_each_entry(dd, &ds->dependent_devices, list) {
158 if (handle == dd->handle) {
159 spin_unlock(&ds->dd_lock);
163 spin_unlock(&ds->dd_lock);
167 /*****************************************************************************
169 *****************************************************************************/
171 * is_dock - see if a device is a dock station
172 * @handle: acpi handle of the device
174 * If an acpi object has a _DCK method, then it is by definition a dock
175 * station, so return true.
177 static int is_dock(acpi_handle handle)
182 status = acpi_get_handle(handle, "_DCK", &tmp);
183 if (ACPI_FAILURE(status))
189 * is_dock_device - see if a device is on a dock station
190 * @handle: acpi handle of the device
192 * If this device is either the dock station itself,
193 * or is a device dependent on the dock station, then it
196 int is_dock_device(acpi_handle handle)
201 if (is_dock(handle) || find_dock_dependent_device(dock_station, handle))
207 EXPORT_SYMBOL_GPL(is_dock_device);
210 * dock_present - see if the dock station is present.
211 * @ds: the dock station
213 * execute the _STA method. note that present does not
214 * imply that we are docked.
216 static int dock_present(struct dock_station *ds)
222 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
223 if (ACPI_SUCCESS(status) && sta)
232 * dock_create_acpi_device - add new devices to acpi
233 * @handle - handle of the device to add
235 * This function will create a new acpi_device for the given
236 * handle if one does not exist already. This should cause
237 * acpi to scan for drivers for the given devices, and call
238 * matching driver's add routine.
240 * Returns a pointer to the acpi_device corresponding to the handle.
242 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
244 struct acpi_device *device = NULL;
245 struct acpi_device *parent_device;
249 if (acpi_bus_get_device(handle, &device)) {
251 * no device created for this object,
252 * so we should create one.
254 acpi_get_parent(handle, &parent);
255 if (acpi_bus_get_device(parent, &parent_device))
256 parent_device = NULL;
258 ret = acpi_bus_add(&device, parent_device, handle,
259 ACPI_BUS_TYPE_DEVICE);
261 pr_debug("error adding bus, %x\n",
270 * dock_remove_acpi_device - remove the acpi_device struct from acpi
271 * @handle - the handle of the device to remove
273 * Tell acpi to remove the acpi_device. This should cause any loaded
274 * driver to have it's remove routine called.
276 static void dock_remove_acpi_device(acpi_handle handle)
278 struct acpi_device *device;
281 if (!acpi_bus_get_device(handle, &device)) {
282 ret = acpi_bus_trim(device, 1);
284 pr_debug("error removing bus, %x\n", -ret);
290 * hotplug_dock_devices - insert or remove devices on the dock station
291 * @ds: the dock station
292 * @event: either bus check or eject request
294 * Some devices on the dock station need to have drivers called
295 * to perform hotplug operations after a dock event has occurred.
296 * Traverse the list of dock devices that have registered a
297 * hotplug handler, and call the handler.
299 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
301 struct dock_dependent_device *dd;
303 mutex_lock(&ds->hp_lock);
306 * First call driver specific hotplug functions
308 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
310 dd->handler(dd->handle, event, dd->context);
314 * Now make sure that an acpi_device is created for each
315 * dependent device, or removed if this is an eject request.
316 * This will cause acpi_drivers to be stopped/started if they
319 list_for_each_entry(dd, &ds->dependent_devices, list) {
320 if (event == ACPI_NOTIFY_EJECT_REQUEST)
321 dock_remove_acpi_device(dd->handle);
323 dock_create_acpi_device(dd->handle);
325 mutex_unlock(&ds->hp_lock);
328 static void dock_event(struct dock_station *ds, u32 event, int num)
330 struct device *dev = &dock_device.dev;
332 * Indicate that the status of the dock station has
335 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
339 * eject_dock - respond to a dock eject request
340 * @ds: the dock station
342 * This is called after _DCK is called, to execute the dock station's
345 static void eject_dock(struct dock_station *ds)
347 struct acpi_object_list arg_list;
348 union acpi_object arg;
352 /* all dock devices should have _EJ0, but check anyway */
353 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
354 if (ACPI_FAILURE(status)) {
355 pr_debug("No _EJ0 support for dock device\n");
360 arg_list.pointer = &arg;
361 arg.type = ACPI_TYPE_INTEGER;
362 arg.integer.value = 1;
364 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
366 pr_debug("Failed to evaluate _EJ0!\n");
370 * handle_dock - handle a dock event
371 * @ds: the dock station
372 * @dock: to dock, or undock - that is the question
374 * Execute the _DCK method in response to an acpi event
376 static void handle_dock(struct dock_station *ds, int dock)
379 struct acpi_object_list arg_list;
380 union acpi_object arg;
381 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
382 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
383 union acpi_object *obj;
385 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
386 obj = name_buffer.pointer;
388 printk(KERN_INFO PREFIX "%s\n", dock ? "docking" : "undocking");
390 /* _DCK method has one argument */
392 arg_list.pointer = &arg;
393 arg.type = ACPI_TYPE_INTEGER;
394 arg.integer.value = dock;
395 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
396 if (ACPI_FAILURE(status))
397 pr_debug("%s: failed to execute _DCK\n", obj->string.pointer);
398 kfree(buffer.pointer);
399 kfree(name_buffer.pointer);
402 static inline void dock(struct dock_station *ds)
407 static inline void undock(struct dock_station *ds)
412 static inline void begin_dock(struct dock_station *ds)
414 ds->flags |= DOCK_DOCKING;
417 static inline void complete_dock(struct dock_station *ds)
419 ds->flags &= ~(DOCK_DOCKING);
420 ds->last_dock_time = jiffies;
424 * dock_in_progress - see if we are in the middle of handling a dock event
425 * @ds: the dock station
427 * Sometimes while docking, false dock events can be sent to the driver
428 * because good connections aren't made or some other reason. Ignore these
429 * if we are in the middle of doing something.
431 static int dock_in_progress(struct dock_station *ds)
433 if ((ds->flags & DOCK_DOCKING) ||
434 time_before(jiffies, (ds->last_dock_time + HZ)))
440 * register_dock_notifier - add yourself to the dock notifier list
441 * @nb: the callers notifier block
443 * If a driver wishes to be notified about dock events, they can
444 * use this function to put a notifier block on the dock notifier list.
445 * this notifier call chain will be called after a dock event, but
446 * before hotplugging any new devices.
448 int register_dock_notifier(struct notifier_block *nb)
453 return atomic_notifier_chain_register(&dock_notifier_list, nb);
456 EXPORT_SYMBOL_GPL(register_dock_notifier);
459 * unregister_dock_notifier - remove yourself from the dock notifier list
460 * @nb: the callers notifier block
462 void unregister_dock_notifier(struct notifier_block *nb)
467 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
470 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
473 * register_hotplug_dock_device - register a hotplug function
474 * @handle: the handle of the device
475 * @handler: the acpi_notifier_handler to call after docking
476 * @context: device specific data
478 * If a driver would like to perform a hotplug operation after a dock
479 * event, they can register an acpi_notifiy_handler to be called by
480 * the dock driver after _DCK is executed.
483 register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
486 struct dock_dependent_device *dd;
492 * make sure this handle is for a device dependent on the dock,
493 * this would include the dock station itself
495 dd = find_dock_dependent_device(dock_station, handle);
497 dd->handler = handler;
498 dd->context = context;
499 dock_add_hotplug_device(dock_station, dd);
506 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
509 * unregister_hotplug_dock_device - remove yourself from the hotplug list
510 * @handle: the acpi handle of the device
512 void unregister_hotplug_dock_device(acpi_handle handle)
514 struct dock_dependent_device *dd;
519 dd = find_dock_dependent_device(dock_station, handle);
521 dock_del_hotplug_device(dock_station, dd);
524 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
527 * handle_eject_request - handle an undock request checking for error conditions
529 * Check to make sure the dock device is still present, then undock and
530 * hotremove all the devices that may need removing.
532 static int handle_eject_request(struct dock_station *ds, u32 event)
534 if (!dock_present(ds))
537 if (dock_in_progress(ds))
541 * here we need to generate the undock
542 * event prior to actually doing the undock
543 * so that the device struct still exists.
545 dock_event(ds, event, UNDOCK_EVENT);
546 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
549 if (dock_present(ds)) {
550 printk(KERN_ERR PREFIX "Unable to undock!\n");
558 * dock_notify - act upon an acpi dock notification
559 * @handle: the dock station handle
560 * @event: the acpi event
561 * @data: our driver data struct
563 * If we are notified to dock, then check to see if the dock is
564 * present and then dock. Notify all drivers of the dock event,
565 * and then hotplug and devices that may need hotplugging.
567 static void dock_notify(acpi_handle handle, u32 event, void *data)
569 struct dock_station *ds = data;
572 case ACPI_NOTIFY_BUS_CHECK:
573 if (!dock_in_progress(ds) && dock_present(ds)) {
576 if (!dock_present(ds)) {
577 printk(KERN_ERR PREFIX "Unable to dock!\n");
580 atomic_notifier_call_chain(&dock_notifier_list,
582 hotplug_dock_devices(ds, event);
584 dock_event(ds, event, DOCK_EVENT);
587 case ACPI_NOTIFY_DEVICE_CHECK:
589 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
590 * is sent and _DCK is present, it is assumed to mean an
591 * undock request. This notify routine will only be called
592 * for objects defining _DCK, so we will fall through to eject
593 * request here. However, we will pass an eject request through
594 * to the driver who wish to hotplug.
596 case ACPI_NOTIFY_EJECT_REQUEST:
597 handle_eject_request(ds, event);
600 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
605 * find_dock_devices - find devices on the dock station
606 * @handle: the handle of the device we are examining
608 * @context: the dock station private data
611 * This function is called by acpi_walk_namespace. It will
612 * check to see if an object has an _EJD method. If it does, then it
613 * will see if it is dependent on the dock station.
616 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
619 acpi_handle tmp, parent;
620 struct dock_station *ds = context;
621 struct dock_dependent_device *dd;
623 status = acpi_bus_get_ejd(handle, &tmp);
624 if (ACPI_FAILURE(status)) {
625 /* try the parent device as well */
626 status = acpi_get_parent(handle, &parent);
627 if (ACPI_FAILURE(status))
629 /* see if parent is dependent on dock */
630 status = acpi_bus_get_ejd(parent, &tmp);
631 if (ACPI_FAILURE(status))
635 if (tmp == ds->handle) {
636 dd = alloc_dock_dependent_device(handle);
638 add_dock_dependent_device(ds, dd);
645 * show_docked - read method for "docked" file in sysfs
647 static ssize_t show_docked(struct device *dev,
648 struct device_attribute *attr, char *buf)
650 return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
653 DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
656 * write_undock - write method for "undock" file in sysfs
658 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
659 const char *buf, size_t count)
666 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
667 return ret ? ret: count;
669 DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
672 * show_dock_uid - read method for "uid" file in sysfs
674 static ssize_t show_dock_uid(struct device *dev,
675 struct device_attribute *attr, char *buf)
678 acpi_status status = acpi_evaluate_integer(dock_station->handle, "_UID", NULL, &lbuf);
679 if(ACPI_FAILURE(status)) {
682 return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf);
684 DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
689 * dock_add - add a new dock station
690 * @handle: the dock station handle
692 * allocated and initialize a new dock station device. Find all devices
693 * that are on the dock station, and register for dock event notifications.
695 static int dock_add(acpi_handle handle)
699 struct dock_dependent_device *dd;
701 /* allocate & initialize the dock_station private data */
702 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
705 dock_station->handle = handle;
706 dock_station->last_dock_time = jiffies - HZ;
707 INIT_LIST_HEAD(&dock_station->dependent_devices);
708 INIT_LIST_HEAD(&dock_station->hotplug_devices);
709 spin_lock_init(&dock_station->dd_lock);
710 mutex_init(&dock_station->hp_lock);
711 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
713 /* initialize platform device stuff */
714 dock_device.name = dock_device_name;
715 ret = platform_device_register(&dock_device);
717 printk(KERN_ERR PREFIX "Error %d registering dock device\n", ret);
721 ret = device_create_file(&dock_device.dev, &dev_attr_docked);
723 printk("Error %d adding sysfs file\n", ret);
724 platform_device_unregister(&dock_device);
728 ret = device_create_file(&dock_device.dev, &dev_attr_undock);
730 printk("Error %d adding sysfs file\n", ret);
731 device_remove_file(&dock_device.dev, &dev_attr_docked);
732 platform_device_unregister(&dock_device);
736 ret = device_create_file(&dock_device.dev, &dev_attr_uid);
738 printk("Error %d adding sysfs file\n", ret);
739 platform_device_unregister(&dock_device);
744 /* Find dependent devices */
745 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
746 ACPI_UINT32_MAX, find_dock_devices, dock_station,
749 /* add the dock station as a device dependent on itself */
750 dd = alloc_dock_dependent_device(handle);
754 goto dock_add_err_unregister;
756 add_dock_dependent_device(dock_station, dd);
758 /* register for dock events */
759 status = acpi_install_notify_handler(dock_station->handle,
761 dock_notify, dock_station);
763 if (ACPI_FAILURE(status)) {
764 printk(KERN_ERR PREFIX "Error installing notify handler\n");
769 printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_DESCRIPTION);
775 dock_add_err_unregister:
776 device_remove_file(&dock_device.dev, &dev_attr_docked);
777 device_remove_file(&dock_device.dev, &dev_attr_undock);
778 platform_device_unregister(&dock_device);
784 * dock_remove - free up resources related to the dock station
786 static int dock_remove(void)
788 struct dock_dependent_device *dd, *tmp;
794 /* remove dependent devices */
795 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
799 /* remove dock notify handler */
800 status = acpi_remove_notify_handler(dock_station->handle,
803 if (ACPI_FAILURE(status))
804 printk(KERN_ERR "Error removing notify handler\n");
807 device_remove_file(&dock_device.dev, &dev_attr_docked);
808 device_remove_file(&dock_device.dev, &dev_attr_undock);
809 platform_device_unregister(&dock_device);
811 /* free dock station memory */
817 * find_dock - look for a dock station
818 * @handle: acpi handle of a device
820 * @context: counter of dock stations found
823 * This is called by acpi_walk_namespace to look for dock stations.
826 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
828 int *count = context;
829 acpi_status status = AE_OK;
831 if (is_dock(handle)) {
832 if (dock_add(handle) >= 0) {
834 status = AE_CTRL_TERMINATE;
840 static int __init dock_init(void)
846 /* look for a dock station */
847 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
848 ACPI_UINT32_MAX, find_dock, &num, NULL);
851 printk(KERN_INFO "No dock devices found.\n");
856 static void __exit dock_exit(void)
861 postcore_initcall(dock_init);
862 module_exit(dock_exit);