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 int immediate_undock = 1;
44 module_param(immediate_undock, bool, 0644);
45 MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
46 "undock immediately when the undock button is pressed, 0 will cause"
47 " the driver to wait for userspace to write the undock sysfs file "
50 static struct atomic_notifier_head dock_notifier_list;
51 static struct platform_device *dock_device;
52 static char dock_device_name[] = "dock";
54 static const struct acpi_device_id dock_device_ids[] = {
58 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
62 unsigned long last_dock_time;
66 struct list_head dependent_devices;
67 struct list_head hotplug_devices;
70 struct dock_dependent_device {
71 struct list_head list;
72 struct list_head hotplug_list;
74 acpi_notify_handler handler;
78 #define DOCK_DOCKING 0x00000001
79 #define DOCK_UNDOCKING 0x00000002
81 #define UNDOCK_EVENT 2
83 static struct dock_station *dock_station;
85 /*****************************************************************************
86 * Dock Dependent device functions *
87 *****************************************************************************/
89 * alloc_dock_dependent_device - allocate and init a dependent device
90 * @handle: the acpi_handle of the dependent device
92 * Allocate memory for a dependent device structure for a device referenced
95 static struct dock_dependent_device *
96 alloc_dock_dependent_device(acpi_handle handle)
98 struct dock_dependent_device *dd;
100 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
103 INIT_LIST_HEAD(&dd->list);
104 INIT_LIST_HEAD(&dd->hotplug_list);
110 * add_dock_dependent_device - associate a device with the dock station
111 * @ds: The dock station
112 * @dd: The dependent device
114 * Add the dependent device to the dock's dependent device list.
117 add_dock_dependent_device(struct dock_station *ds,
118 struct dock_dependent_device *dd)
120 spin_lock(&ds->dd_lock);
121 list_add_tail(&dd->list, &ds->dependent_devices);
122 spin_unlock(&ds->dd_lock);
126 * dock_add_hotplug_device - associate a hotplug handler with the dock station
127 * @ds: The dock station
128 * @dd: The dependent device struct
130 * Add the dependent device to the dock's hotplug device list
133 dock_add_hotplug_device(struct dock_station *ds,
134 struct dock_dependent_device *dd)
136 mutex_lock(&ds->hp_lock);
137 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
138 mutex_unlock(&ds->hp_lock);
142 * dock_del_hotplug_device - remove a hotplug handler from the dock station
143 * @ds: The dock station
144 * @dd: the dependent device struct
146 * Delete the dependent device from the dock's hotplug device list
149 dock_del_hotplug_device(struct dock_station *ds,
150 struct dock_dependent_device *dd)
152 mutex_lock(&ds->hp_lock);
153 list_del(&dd->hotplug_list);
154 mutex_unlock(&ds->hp_lock);
158 * find_dock_dependent_device - get a device dependent on this dock
159 * @ds: the dock station
160 * @handle: the acpi_handle of the device we want
162 * iterate over the dependent device list for this dock. If the
163 * dependent device matches the handle, return.
165 static struct dock_dependent_device *
166 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
168 struct dock_dependent_device *dd;
170 spin_lock(&ds->dd_lock);
171 list_for_each_entry(dd, &ds->dependent_devices, list) {
172 if (handle == dd->handle) {
173 spin_unlock(&ds->dd_lock);
177 spin_unlock(&ds->dd_lock);
181 /*****************************************************************************
183 *****************************************************************************/
185 * is_dock - see if a device is a dock station
186 * @handle: acpi handle of the device
188 * If an acpi object has a _DCK method, then it is by definition a dock
189 * station, so return true.
191 static int is_dock(acpi_handle handle)
196 status = acpi_get_handle(handle, "_DCK", &tmp);
197 if (ACPI_FAILURE(status))
203 * is_dock_device - see if a device is on a dock station
204 * @handle: acpi handle of the device
206 * If this device is either the dock station itself,
207 * or is a device dependent on the dock station, then it
210 int is_dock_device(acpi_handle handle)
215 if (is_dock(handle) || find_dock_dependent_device(dock_station, handle))
221 EXPORT_SYMBOL_GPL(is_dock_device);
224 * dock_present - see if the dock station is present.
225 * @ds: the dock station
227 * execute the _STA method. note that present does not
228 * imply that we are docked.
230 static int dock_present(struct dock_station *ds)
236 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
237 if (ACPI_SUCCESS(status) && sta)
246 * dock_create_acpi_device - add new devices to acpi
247 * @handle - handle of the device to add
249 * This function will create a new acpi_device for the given
250 * handle if one does not exist already. This should cause
251 * acpi to scan for drivers for the given devices, and call
252 * matching driver's add routine.
254 * Returns a pointer to the acpi_device corresponding to the handle.
256 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
258 struct acpi_device *device = NULL;
259 struct acpi_device *parent_device;
263 if (acpi_bus_get_device(handle, &device)) {
265 * no device created for this object,
266 * so we should create one.
268 acpi_get_parent(handle, &parent);
269 if (acpi_bus_get_device(parent, &parent_device))
270 parent_device = NULL;
272 ret = acpi_bus_add(&device, parent_device, handle,
273 ACPI_BUS_TYPE_DEVICE);
275 pr_debug("error adding bus, %x\n",
284 * dock_remove_acpi_device - remove the acpi_device struct from acpi
285 * @handle - the handle of the device to remove
287 * Tell acpi to remove the acpi_device. This should cause any loaded
288 * driver to have it's remove routine called.
290 static void dock_remove_acpi_device(acpi_handle handle)
292 struct acpi_device *device;
295 if (!acpi_bus_get_device(handle, &device)) {
296 ret = acpi_bus_trim(device, 1);
298 pr_debug("error removing bus, %x\n", -ret);
304 * hotplug_dock_devices - insert or remove devices on the dock station
305 * @ds: the dock station
306 * @event: either bus check or eject request
308 * Some devices on the dock station need to have drivers called
309 * to perform hotplug operations after a dock event has occurred.
310 * Traverse the list of dock devices that have registered a
311 * hotplug handler, and call the handler.
313 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
315 struct dock_dependent_device *dd;
317 mutex_lock(&ds->hp_lock);
320 * First call driver specific hotplug functions
322 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
324 dd->handler(dd->handle, event, dd->context);
328 * Now make sure that an acpi_device is created for each
329 * dependent device, or removed if this is an eject request.
330 * This will cause acpi_drivers to be stopped/started if they
333 list_for_each_entry(dd, &ds->dependent_devices, list) {
334 if (event == ACPI_NOTIFY_EJECT_REQUEST)
335 dock_remove_acpi_device(dd->handle);
337 dock_create_acpi_device(dd->handle);
339 mutex_unlock(&ds->hp_lock);
342 static void dock_event(struct dock_station *ds, u32 event, int num)
344 struct device *dev = &dock_device->dev;
345 char event_string[13];
346 char *envp[] = { event_string, NULL };
348 if (num == UNDOCK_EVENT)
349 sprintf(event_string, "EVENT=undock");
351 sprintf(event_string, "EVENT=dock");
354 * Indicate that the status of the dock station has
357 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
361 * eject_dock - respond to a dock eject request
362 * @ds: the dock station
364 * This is called after _DCK is called, to execute the dock station's
367 static void eject_dock(struct dock_station *ds)
369 struct acpi_object_list arg_list;
370 union acpi_object arg;
374 /* all dock devices should have _EJ0, but check anyway */
375 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
376 if (ACPI_FAILURE(status)) {
377 pr_debug("No _EJ0 support for dock device\n");
382 arg_list.pointer = &arg;
383 arg.type = ACPI_TYPE_INTEGER;
384 arg.integer.value = 1;
386 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
388 pr_debug("Failed to evaluate _EJ0!\n");
392 * handle_dock - handle a dock event
393 * @ds: the dock station
394 * @dock: to dock, or undock - that is the question
396 * Execute the _DCK method in response to an acpi event
398 static void handle_dock(struct dock_station *ds, int dock)
401 struct acpi_object_list arg_list;
402 union acpi_object arg;
403 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
404 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
406 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
408 printk(KERN_INFO PREFIX "%s - %s\n",
409 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
411 /* _DCK method has one argument */
413 arg_list.pointer = &arg;
414 arg.type = ACPI_TYPE_INTEGER;
415 arg.integer.value = dock;
416 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
417 if (ACPI_FAILURE(status))
418 printk(KERN_ERR PREFIX "%s - failed to execute _DCK\n",
419 (char *)name_buffer.pointer);
420 kfree(buffer.pointer);
421 kfree(name_buffer.pointer);
424 static inline void dock(struct dock_station *ds)
429 static inline void undock(struct dock_station *ds)
434 static inline void begin_dock(struct dock_station *ds)
436 ds->flags |= DOCK_DOCKING;
439 static inline void complete_dock(struct dock_station *ds)
441 ds->flags &= ~(DOCK_DOCKING);
442 ds->last_dock_time = jiffies;
445 static inline void begin_undock(struct dock_station *ds)
447 ds->flags |= DOCK_UNDOCKING;
450 static inline void complete_undock(struct dock_station *ds)
452 ds->flags &= ~(DOCK_UNDOCKING);
456 * dock_in_progress - see if we are in the middle of handling a dock event
457 * @ds: the dock station
459 * Sometimes while docking, false dock events can be sent to the driver
460 * because good connections aren't made or some other reason. Ignore these
461 * if we are in the middle of doing something.
463 static int dock_in_progress(struct dock_station *ds)
465 if ((ds->flags & DOCK_DOCKING) ||
466 time_before(jiffies, (ds->last_dock_time + HZ)))
472 * register_dock_notifier - add yourself to the dock notifier list
473 * @nb: the callers notifier block
475 * If a driver wishes to be notified about dock events, they can
476 * use this function to put a notifier block on the dock notifier list.
477 * this notifier call chain will be called after a dock event, but
478 * before hotplugging any new devices.
480 int register_dock_notifier(struct notifier_block *nb)
485 return atomic_notifier_chain_register(&dock_notifier_list, nb);
488 EXPORT_SYMBOL_GPL(register_dock_notifier);
491 * unregister_dock_notifier - remove yourself from the dock notifier list
492 * @nb: the callers notifier block
494 void unregister_dock_notifier(struct notifier_block *nb)
499 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
502 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
505 * register_hotplug_dock_device - register a hotplug function
506 * @handle: the handle of the device
507 * @handler: the acpi_notifier_handler to call after docking
508 * @context: device specific data
510 * If a driver would like to perform a hotplug operation after a dock
511 * event, they can register an acpi_notifiy_handler to be called by
512 * the dock driver after _DCK is executed.
515 register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
518 struct dock_dependent_device *dd;
524 * make sure this handle is for a device dependent on the dock,
525 * this would include the dock station itself
527 dd = find_dock_dependent_device(dock_station, handle);
529 dd->handler = handler;
530 dd->context = context;
531 dock_add_hotplug_device(dock_station, dd);
538 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
541 * unregister_hotplug_dock_device - remove yourself from the hotplug list
542 * @handle: the acpi handle of the device
544 void unregister_hotplug_dock_device(acpi_handle handle)
546 struct dock_dependent_device *dd;
551 dd = find_dock_dependent_device(dock_station, handle);
553 dock_del_hotplug_device(dock_station, dd);
556 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
559 * handle_eject_request - handle an undock request checking for error conditions
561 * Check to make sure the dock device is still present, then undock and
562 * hotremove all the devices that may need removing.
564 static int handle_eject_request(struct dock_station *ds, u32 event)
566 if (!dock_present(ds))
569 if (dock_in_progress(ds))
573 * here we need to generate the undock
574 * event prior to actually doing the undock
575 * so that the device struct still exists.
577 dock_event(ds, event, UNDOCK_EVENT);
578 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
581 if (dock_present(ds)) {
582 printk(KERN_ERR PREFIX "Unable to undock!\n");
590 * dock_notify - act upon an acpi dock notification
591 * @handle: the dock station handle
592 * @event: the acpi event
593 * @data: our driver data struct
595 * If we are notified to dock, then check to see if the dock is
596 * present and then dock. Notify all drivers of the dock event,
597 * and then hotplug and devices that may need hotplugging.
599 static void dock_notify(acpi_handle handle, u32 event, void *data)
601 struct dock_station *ds = data;
604 case ACPI_NOTIFY_BUS_CHECK:
605 if (!dock_in_progress(ds) && dock_present(ds)) {
608 if (!dock_present(ds)) {
609 printk(KERN_ERR PREFIX "Unable to dock!\n");
612 atomic_notifier_call_chain(&dock_notifier_list,
614 hotplug_dock_devices(ds, event);
616 dock_event(ds, event, DOCK_EVENT);
619 case ACPI_NOTIFY_DEVICE_CHECK:
621 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
622 * is sent and _DCK is present, it is assumed to mean an
623 * undock request. This notify routine will only be called
624 * for objects defining _DCK, so we will fall through to eject
625 * request here. However, we will pass an eject request through
626 * to the driver who wish to hotplug.
628 case ACPI_NOTIFY_EJECT_REQUEST:
630 if (immediate_undock)
631 handle_eject_request(ds, event);
633 dock_event(ds, event, UNDOCK_EVENT);
636 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
641 * find_dock_devices - find devices on the dock station
642 * @handle: the handle of the device we are examining
644 * @context: the dock station private data
647 * This function is called by acpi_walk_namespace. It will
648 * check to see if an object has an _EJD method. If it does, then it
649 * will see if it is dependent on the dock station.
652 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
655 acpi_handle tmp, parent;
656 struct dock_station *ds = context;
657 struct dock_dependent_device *dd;
659 status = acpi_bus_get_ejd(handle, &tmp);
660 if (ACPI_FAILURE(status)) {
661 /* try the parent device as well */
662 status = acpi_get_parent(handle, &parent);
663 if (ACPI_FAILURE(status))
665 /* see if parent is dependent on dock */
666 status = acpi_bus_get_ejd(parent, &tmp);
667 if (ACPI_FAILURE(status))
671 if (tmp == ds->handle) {
672 dd = alloc_dock_dependent_device(handle);
674 add_dock_dependent_device(ds, dd);
681 * show_docked - read method for "docked" file in sysfs
683 static ssize_t show_docked(struct device *dev,
684 struct device_attribute *attr, char *buf)
686 return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
689 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
692 * show_flags - read method for flags file in sysfs
694 static ssize_t show_flags(struct device *dev,
695 struct device_attribute *attr, char *buf)
697 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
700 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
703 * write_undock - write method for "undock" file in sysfs
705 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
706 const char *buf, size_t count)
713 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
714 return ret ? ret: count;
716 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
719 * show_dock_uid - read method for "uid" file in sysfs
721 static ssize_t show_dock_uid(struct device *dev,
722 struct device_attribute *attr, char *buf)
725 acpi_status status = acpi_evaluate_integer(dock_station->handle,
726 "_UID", NULL, &lbuf);
727 if (ACPI_FAILURE(status))
730 return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf);
732 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
735 * dock_add - add a new dock station
736 * @handle: the dock station handle
738 * allocated and initialize a new dock station device. Find all devices
739 * that are on the dock station, and register for dock event notifications.
741 static int dock_add(acpi_handle handle)
745 struct dock_dependent_device *dd;
747 /* allocate & initialize the dock_station private data */
748 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
751 dock_station->handle = handle;
752 dock_station->last_dock_time = jiffies - HZ;
753 INIT_LIST_HEAD(&dock_station->dependent_devices);
754 INIT_LIST_HEAD(&dock_station->hotplug_devices);
755 spin_lock_init(&dock_station->dd_lock);
756 mutex_init(&dock_station->hp_lock);
757 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
759 /* initialize platform device stuff */
761 platform_device_register_simple(dock_device_name, 0, NULL, 0);
762 if (IS_ERR(dock_device)) {
765 return PTR_ERR(dock_device);
768 /* we want the dock device to send uevents */
769 dock_device->dev.uevent_suppress = 0;
771 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
773 printk("Error %d adding sysfs file\n", ret);
774 platform_device_unregister(dock_device);
779 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
781 printk("Error %d adding sysfs file\n", ret);
782 device_remove_file(&dock_device->dev, &dev_attr_docked);
783 platform_device_unregister(dock_device);
788 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
790 printk("Error %d adding sysfs file\n", ret);
791 device_remove_file(&dock_device->dev, &dev_attr_docked);
792 device_remove_file(&dock_device->dev, &dev_attr_undock);
793 platform_device_unregister(dock_device);
798 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
800 printk("Error %d adding sysfs file\n", ret);
801 device_remove_file(&dock_device->dev, &dev_attr_docked);
802 device_remove_file(&dock_device->dev, &dev_attr_undock);
803 device_remove_file(&dock_device->dev, &dev_attr_uid);
804 platform_device_unregister(dock_device);
810 /* Find dependent devices */
811 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
812 ACPI_UINT32_MAX, find_dock_devices, dock_station,
815 /* add the dock station as a device dependent on itself */
816 dd = alloc_dock_dependent_device(handle);
821 goto dock_add_err_unregister;
823 add_dock_dependent_device(dock_station, dd);
825 /* register for dock events */
826 status = acpi_install_notify_handler(dock_station->handle,
828 dock_notify, dock_station);
830 if (ACPI_FAILURE(status)) {
831 printk(KERN_ERR PREFIX "Error installing notify handler\n");
836 printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_DESCRIPTION);
842 dock_add_err_unregister:
843 device_remove_file(&dock_device->dev, &dev_attr_docked);
844 device_remove_file(&dock_device->dev, &dev_attr_undock);
845 device_remove_file(&dock_device->dev, &dev_attr_uid);
846 device_remove_file(&dock_device->dev, &dev_attr_flags);
847 platform_device_unregister(dock_device);
854 * dock_remove - free up resources related to the dock station
856 static int dock_remove(void)
858 struct dock_dependent_device *dd, *tmp;
864 /* remove dependent devices */
865 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
869 /* remove dock notify handler */
870 status = acpi_remove_notify_handler(dock_station->handle,
873 if (ACPI_FAILURE(status))
874 printk(KERN_ERR "Error removing notify handler\n");
877 device_remove_file(&dock_device->dev, &dev_attr_docked);
878 device_remove_file(&dock_device->dev, &dev_attr_undock);
879 device_remove_file(&dock_device->dev, &dev_attr_uid);
880 device_remove_file(&dock_device->dev, &dev_attr_flags);
881 platform_device_unregister(dock_device);
883 /* free dock station memory */
890 * find_dock - look for a dock station
891 * @handle: acpi handle of a device
893 * @context: counter of dock stations found
896 * This is called by acpi_walk_namespace to look for dock stations.
899 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
901 int *count = context;
902 acpi_status status = AE_OK;
904 if (is_dock(handle)) {
905 if (dock_add(handle) >= 0) {
907 status = AE_CTRL_TERMINATE;
913 static int __init dock_init(void)
919 /* look for a dock station */
920 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
921 ACPI_UINT32_MAX, find_dock, &num, NULL);
924 printk(KERN_INFO "No dock devices found.\n");
929 static void __exit dock_exit(void)
934 postcore_initcall(dock_init);
935 module_exit(dock_exit);