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 <acpi/acpi_bus.h>
33 #include <acpi/acpi_drivers.h>
35 #define ACPI_DOCK_DRIVER_NAME "ACPI Dock Station Driver"
37 ACPI_MODULE_NAME("dock")
38 MODULE_AUTHOR("Kristen Carlson Accardi");
39 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_NAME);
40 MODULE_LICENSE("GPL");
42 static struct atomic_notifier_head dock_notifier_list;
43 static struct platform_device dock_device;
44 static char dock_device_name[] = "dock";
48 unsigned long last_dock_time;
52 struct list_head dependent_devices;
53 struct list_head hotplug_devices;
56 struct dock_dependent_device {
57 struct list_head list;
58 struct list_head hotplug_list;
60 acpi_notify_handler handler;
64 #define DOCK_DOCKING 0x00000001
66 #define UNDOCK_EVENT 2
68 static struct dock_station *dock_station;
70 /*****************************************************************************
71 * Dock Dependent device functions *
72 *****************************************************************************/
74 * alloc_dock_dependent_device - allocate and init a dependent device
75 * @handle: the acpi_handle of the dependent device
77 * Allocate memory for a dependent device structure for a device referenced
80 static struct dock_dependent_device *
81 alloc_dock_dependent_device(acpi_handle handle)
83 struct dock_dependent_device *dd;
85 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
88 INIT_LIST_HEAD(&dd->list);
89 INIT_LIST_HEAD(&dd->hotplug_list);
95 * add_dock_dependent_device - associate a device with the dock station
96 * @ds: The dock station
97 * @dd: The dependent device
99 * Add the dependent device to the dock's dependent device list.
102 add_dock_dependent_device(struct dock_station *ds,
103 struct dock_dependent_device *dd)
105 spin_lock(&ds->dd_lock);
106 list_add_tail(&dd->list, &ds->dependent_devices);
107 spin_unlock(&ds->dd_lock);
111 * dock_add_hotplug_device - associate a hotplug handler with the dock station
112 * @ds: The dock station
113 * @dd: The dependent device struct
115 * Add the dependent device to the dock's hotplug device list
118 dock_add_hotplug_device(struct dock_station *ds,
119 struct dock_dependent_device *dd)
121 spin_lock(&ds->hp_lock);
122 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
123 spin_unlock(&ds->hp_lock);
127 * dock_del_hotplug_device - remove a hotplug handler from the dock station
128 * @ds: The dock station
129 * @dd: the dependent device struct
131 * Delete the dependent device from the dock's hotplug device list
134 dock_del_hotplug_device(struct dock_station *ds,
135 struct dock_dependent_device *dd)
137 spin_lock(&ds->hp_lock);
138 list_del(&dd->hotplug_list);
139 spin_unlock(&ds->hp_lock);
143 * find_dock_dependent_device - get a device dependent on this dock
144 * @ds: the dock station
145 * @handle: the acpi_handle of the device we want
147 * iterate over the dependent device list for this dock. If the
148 * dependent device matches the handle, return.
150 static struct dock_dependent_device *
151 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
153 struct dock_dependent_device *dd;
155 spin_lock(&ds->dd_lock);
156 list_for_each_entry(dd, &ds->dependent_devices, list) {
157 if (handle == dd->handle) {
158 spin_unlock(&ds->dd_lock);
162 spin_unlock(&ds->dd_lock);
166 /*****************************************************************************
168 *****************************************************************************/
170 * is_dock - see if a device is a dock station
171 * @handle: acpi handle of the device
173 * If an acpi object has a _DCK method, then it is by definition a dock
174 * station, so return true.
176 static int is_dock(acpi_handle handle)
181 status = acpi_get_handle(handle, "_DCK", &tmp);
182 if (ACPI_FAILURE(status))
188 * is_dock_device - see if a device is on a dock station
189 * @handle: acpi handle of the device
191 * If this device is either the dock station itself,
192 * or is a device dependent on the dock station, then it
195 int is_dock_device(acpi_handle handle)
200 if (is_dock(handle) || find_dock_dependent_device(dock_station, handle))
206 EXPORT_SYMBOL_GPL(is_dock_device);
209 * dock_present - see if the dock station is present.
210 * @ds: the dock station
212 * execute the _STA method. note that present does not
213 * imply that we are docked.
215 static int dock_present(struct dock_station *ds)
221 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
222 if (ACPI_SUCCESS(status) && sta)
231 * dock_create_acpi_device - add new devices to acpi
232 * @handle - handle of the device to add
234 * This function will create a new acpi_device for the given
235 * handle if one does not exist already. This should cause
236 * acpi to scan for drivers for the given devices, and call
237 * matching driver's add routine.
239 * Returns a pointer to the acpi_device corresponding to the handle.
241 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
243 struct acpi_device *device = NULL;
244 struct acpi_device *parent_device;
248 if (acpi_bus_get_device(handle, &device)) {
250 * no device created for this object,
251 * so we should create one.
253 acpi_get_parent(handle, &parent);
254 if (acpi_bus_get_device(parent, &parent_device))
255 parent_device = NULL;
257 ret = acpi_bus_add(&device, parent_device, handle,
258 ACPI_BUS_TYPE_DEVICE);
260 pr_debug("error adding bus, %x\n",
269 * dock_remove_acpi_device - remove the acpi_device struct from acpi
270 * @handle - the handle of the device to remove
272 * Tell acpi to remove the acpi_device. This should cause any loaded
273 * driver to have it's remove routine called.
275 static void dock_remove_acpi_device(acpi_handle handle)
277 struct acpi_device *device;
280 if (!acpi_bus_get_device(handle, &device)) {
281 ret = acpi_bus_trim(device, 1);
283 pr_debug("error removing bus, %x\n", -ret);
289 * hotplug_dock_devices - insert or remove devices on the dock station
290 * @ds: the dock station
291 * @event: either bus check or eject request
293 * Some devices on the dock station need to have drivers called
294 * to perform hotplug operations after a dock event has occurred.
295 * Traverse the list of dock devices that have registered a
296 * hotplug handler, and call the handler.
298 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
300 struct dock_dependent_device *dd;
302 spin_lock(&ds->hp_lock);
305 * First call driver specific hotplug functions
307 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
309 dd->handler(dd->handle, event, dd->context);
313 * Now make sure that an acpi_device is created for each
314 * dependent device, or removed if this is an eject request.
315 * This will cause acpi_drivers to be stopped/started if they
318 list_for_each_entry(dd, &ds->dependent_devices, list) {
319 if (event == ACPI_NOTIFY_EJECT_REQUEST)
320 dock_remove_acpi_device(dd->handle);
322 dock_create_acpi_device(dd->handle);
324 spin_unlock(&ds->hp_lock);
327 static void dock_event(struct dock_station *ds, u32 event, int num)
330 * we don't do events until someone tells me that
331 * they would like to have them.
336 * eject_dock - respond to a dock eject request
337 * @ds: the dock station
339 * This is called after _DCK is called, to execute the dock station's
342 static void eject_dock(struct dock_station *ds)
344 struct acpi_object_list arg_list;
345 union acpi_object arg;
349 /* all dock devices should have _EJ0, but check anyway */
350 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
351 if (ACPI_FAILURE(status)) {
352 pr_debug("No _EJ0 support for dock device\n");
357 arg_list.pointer = &arg;
358 arg.type = ACPI_TYPE_INTEGER;
359 arg.integer.value = 1;
361 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
363 pr_debug("Failed to evaluate _EJ0!\n");
367 * handle_dock - handle a dock event
368 * @ds: the dock station
369 * @dock: to dock, or undock - that is the question
371 * Execute the _DCK method in response to an acpi event
373 static void handle_dock(struct dock_station *ds, int dock)
376 struct acpi_object_list arg_list;
377 union acpi_object arg;
378 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
379 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
380 union acpi_object *obj;
382 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
383 obj = name_buffer.pointer;
385 printk(KERN_INFO PREFIX "%s\n", dock ? "docking" : "undocking");
387 /* _DCK method has one argument */
389 arg_list.pointer = &arg;
390 arg.type = ACPI_TYPE_INTEGER;
391 arg.integer.value = dock;
392 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
393 if (ACPI_FAILURE(status))
394 pr_debug("%s: failed to execute _DCK\n", obj->string.pointer);
395 kfree(buffer.pointer);
396 kfree(name_buffer.pointer);
399 static inline void dock(struct dock_station *ds)
404 static inline void undock(struct dock_station *ds)
409 static inline void begin_dock(struct dock_station *ds)
411 ds->flags |= DOCK_DOCKING;
414 static inline void complete_dock(struct dock_station *ds)
416 ds->flags &= ~(DOCK_DOCKING);
417 ds->last_dock_time = jiffies;
421 * dock_in_progress - see if we are in the middle of handling a dock event
422 * @ds: the dock station
424 * Sometimes while docking, false dock events can be sent to the driver
425 * because good connections aren't made or some other reason. Ignore these
426 * if we are in the middle of doing something.
428 static int dock_in_progress(struct dock_station *ds)
430 if ((ds->flags & DOCK_DOCKING) ||
431 time_before(jiffies, (ds->last_dock_time + HZ)))
437 * register_dock_notifier - add yourself to the dock notifier list
438 * @nb: the callers notifier block
440 * If a driver wishes to be notified about dock events, they can
441 * use this function to put a notifier block on the dock notifier list.
442 * this notifier call chain will be called after a dock event, but
443 * before hotplugging any new devices.
445 int register_dock_notifier(struct notifier_block *nb)
450 return atomic_notifier_chain_register(&dock_notifier_list, nb);
453 EXPORT_SYMBOL_GPL(register_dock_notifier);
456 * unregister_dock_notifier - remove yourself from the dock notifier list
457 * @nb: the callers notifier block
459 void unregister_dock_notifier(struct notifier_block *nb)
464 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
467 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
470 * register_hotplug_dock_device - register a hotplug function
471 * @handle: the handle of the device
472 * @handler: the acpi_notifier_handler to call after docking
473 * @context: device specific data
475 * If a driver would like to perform a hotplug operation after a dock
476 * event, they can register an acpi_notifiy_handler to be called by
477 * the dock driver after _DCK is executed.
480 register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
483 struct dock_dependent_device *dd;
489 * make sure this handle is for a device dependent on the dock,
490 * this would include the dock station itself
492 dd = find_dock_dependent_device(dock_station, handle);
494 dd->handler = handler;
495 dd->context = context;
496 dock_add_hotplug_device(dock_station, dd);
503 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
506 * unregister_hotplug_dock_device - remove yourself from the hotplug list
507 * @handle: the acpi handle of the device
509 void unregister_hotplug_dock_device(acpi_handle handle)
511 struct dock_dependent_device *dd;
516 dd = find_dock_dependent_device(dock_station, handle);
518 dock_del_hotplug_device(dock_station, dd);
521 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
524 * handle_eject_request - handle an undock request checking for error conditions
526 * Check to make sure the dock device is still present, then undock and
527 * hotremove all the devices that may need removing.
529 static int handle_eject_request(struct dock_station *ds, u32 event)
531 if (!dock_present(ds))
534 if (dock_in_progress(ds))
538 * here we need to generate the undock
539 * event prior to actually doing the undock
540 * so that the device struct still exists.
542 dock_event(ds, event, UNDOCK_EVENT);
543 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
546 if (dock_present(ds)) {
547 printk(KERN_ERR PREFIX "Unable to undock!\n");
555 * dock_notify - act upon an acpi dock notification
556 * @handle: the dock station handle
557 * @event: the acpi event
558 * @data: our driver data struct
560 * If we are notified to dock, then check to see if the dock is
561 * present and then dock. Notify all drivers of the dock event,
562 * and then hotplug and devices that may need hotplugging.
564 static void dock_notify(acpi_handle handle, u32 event, void *data)
566 struct dock_station *ds = (struct dock_station *)data;
569 case ACPI_NOTIFY_BUS_CHECK:
570 if (!dock_in_progress(ds) && dock_present(ds)) {
573 if (!dock_present(ds)) {
574 printk(KERN_ERR PREFIX "Unable to dock!\n");
577 atomic_notifier_call_chain(&dock_notifier_list,
579 hotplug_dock_devices(ds, event);
581 dock_event(ds, event, DOCK_EVENT);
584 case ACPI_NOTIFY_DEVICE_CHECK:
586 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
587 * is sent and _DCK is present, it is assumed to mean an
588 * undock request. This notify routine will only be called
589 * for objects defining _DCK, so we will fall through to eject
590 * request here. However, we will pass an eject request through
591 * to the driver who wish to hotplug.
593 case ACPI_NOTIFY_EJECT_REQUEST:
594 handle_eject_request(ds, event);
597 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
602 * find_dock_devices - find devices on the dock station
603 * @handle: the handle of the device we are examining
605 * @context: the dock station private data
608 * This function is called by acpi_walk_namespace. It will
609 * check to see if an object has an _EJD method. If it does, then it
610 * will see if it is dependent on the dock station.
613 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
617 struct dock_station *ds = (struct dock_station *)context;
618 struct dock_dependent_device *dd;
620 status = acpi_bus_get_ejd(handle, &tmp);
621 if (ACPI_FAILURE(status))
624 if (tmp == ds->handle) {
625 dd = alloc_dock_dependent_device(handle);
627 add_dock_dependent_device(ds, dd);
634 * show_docked - read method for "docked" file in sysfs
636 static ssize_t show_docked(struct device *dev,
637 struct device_attribute *attr, char *buf)
639 return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
642 DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
645 * write_undock - write method for "undock" file in sysfs
647 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
648 const char *buf, size_t count)
655 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
656 return ret ? ret: count;
658 DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
661 * dock_add - add a new dock station
662 * @handle: the dock station handle
664 * allocated and initialize a new dock station device. Find all devices
665 * that are on the dock station, and register for dock event notifications.
667 static int dock_add(acpi_handle handle)
671 struct dock_dependent_device *dd;
673 /* allocate & initialize the dock_station private data */
674 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
677 dock_station->handle = handle;
678 dock_station->last_dock_time = jiffies - HZ;
679 INIT_LIST_HEAD(&dock_station->dependent_devices);
680 INIT_LIST_HEAD(&dock_station->hotplug_devices);
681 spin_lock_init(&dock_station->dd_lock);
682 spin_lock_init(&dock_station->hp_lock);
683 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
685 /* initialize platform device stuff */
686 dock_device.name = dock_device_name;
687 ret = platform_device_register(&dock_device);
689 printk(KERN_ERR PREFIX "Error %d registering dock device\n", ret);
693 ret = device_create_file(&dock_device.dev, &dev_attr_docked);
695 printk("Error %d adding sysfs file\n", ret);
696 platform_device_unregister(&dock_device);
700 ret = device_create_file(&dock_device.dev, &dev_attr_undock);
702 printk("Error %d adding sysfs file\n", ret);
703 device_remove_file(&dock_device.dev, &dev_attr_docked);
704 platform_device_unregister(&dock_device);
709 /* Find dependent devices */
710 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
711 ACPI_UINT32_MAX, find_dock_devices, dock_station,
714 /* add the dock station as a device dependent on itself */
715 dd = alloc_dock_dependent_device(handle);
719 goto dock_add_err_unregister;
721 add_dock_dependent_device(dock_station, dd);
723 /* register for dock events */
724 status = acpi_install_notify_handler(dock_station->handle,
726 dock_notify, dock_station);
728 if (ACPI_FAILURE(status)) {
729 printk(KERN_ERR PREFIX "Error installing notify handler\n");
734 printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_NAME);
740 dock_add_err_unregister:
741 device_remove_file(&dock_device.dev, &dev_attr_docked);
742 device_remove_file(&dock_device.dev, &dev_attr_undock);
743 platform_device_unregister(&dock_device);
749 * dock_remove - free up resources related to the dock station
751 static int dock_remove(void)
753 struct dock_dependent_device *dd, *tmp;
759 /* remove dependent devices */
760 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
764 /* remove dock notify handler */
765 status = acpi_remove_notify_handler(dock_station->handle,
768 if (ACPI_FAILURE(status))
769 printk(KERN_ERR "Error removing notify handler\n");
772 device_remove_file(&dock_device.dev, &dev_attr_docked);
773 device_remove_file(&dock_device.dev, &dev_attr_undock);
774 platform_device_unregister(&dock_device);
776 /* free dock station memory */
782 * find_dock - look for a dock station
783 * @handle: acpi handle of a device
785 * @context: counter of dock stations found
788 * This is called by acpi_walk_namespace to look for dock stations.
791 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
793 int *count = (int *)context;
794 acpi_status status = AE_OK;
796 if (is_dock(handle)) {
797 if (dock_add(handle) >= 0) {
799 status = AE_CTRL_TERMINATE;
805 static int __init dock_init(void)
811 /* look for a dock station */
812 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
813 ACPI_UINT32_MAX, find_dock, &num, NULL);
816 printk(KERN_INFO "No dock devices found.\n");
821 static void __exit dock_exit(void)
826 postcore_initcall(dock_init);
827 module_exit(dock_exit);