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 PREFIX "ACPI: "
38 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
40 ACPI_MODULE_NAME("dock");
41 MODULE_AUTHOR("Kristen Carlson Accardi");
42 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
43 MODULE_LICENSE("GPL");
45 static int immediate_undock = 1;
46 module_param(immediate_undock, bool, 0644);
47 MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
48 "undock immediately when the undock button is pressed, 0 will cause"
49 " the driver to wait for userspace to write the undock sysfs file "
52 static struct atomic_notifier_head dock_notifier_list;
53 static char dock_device_name[] = "dock";
55 static const struct acpi_device_id dock_device_ids[] = {
59 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
63 unsigned long last_dock_time;
67 struct list_head dependent_devices;
68 struct list_head hotplug_devices;
70 struct list_head sibling;
71 struct platform_device *dock_device;
73 static LIST_HEAD(dock_stations);
74 static int dock_station_count;
76 struct dock_dependent_device {
77 struct list_head list;
78 struct list_head hotplug_list;
80 struct acpi_dock_ops *ops;
84 #define DOCK_DOCKING 0x00000001
85 #define DOCK_UNDOCKING 0x00000002
86 #define DOCK_IS_DOCK 0x00000010
87 #define DOCK_IS_ATA 0x00000020
88 #define DOCK_IS_BAT 0x00000040
90 #define UNDOCK_EVENT 2
92 /*****************************************************************************
93 * Dock Dependent device functions *
94 *****************************************************************************/
96 * add_dock_dependent_device - associate a device with the dock station
97 * @ds: The dock station
98 * @handle: handle of the dependent device
100 * Add the dependent device to the dock's dependent device list.
103 add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
105 struct dock_dependent_device *dd;
107 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
112 INIT_LIST_HEAD(&dd->list);
113 INIT_LIST_HEAD(&dd->hotplug_list);
115 spin_lock(&ds->dd_lock);
116 list_add_tail(&dd->list, &ds->dependent_devices);
117 spin_unlock(&ds->dd_lock);
123 * dock_add_hotplug_device - associate a hotplug handler with the dock station
124 * @ds: The dock station
125 * @dd: The dependent device struct
127 * Add the dependent device to the dock's hotplug device list
130 dock_add_hotplug_device(struct dock_station *ds,
131 struct dock_dependent_device *dd)
133 mutex_lock(&ds->hp_lock);
134 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
135 mutex_unlock(&ds->hp_lock);
139 * dock_del_hotplug_device - remove a hotplug handler from the dock station
140 * @ds: The dock station
141 * @dd: the dependent device struct
143 * Delete the dependent device from the dock's hotplug device list
146 dock_del_hotplug_device(struct dock_station *ds,
147 struct dock_dependent_device *dd)
149 mutex_lock(&ds->hp_lock);
150 list_del(&dd->hotplug_list);
151 mutex_unlock(&ds->hp_lock);
155 * find_dock_dependent_device - get a device dependent on this dock
156 * @ds: the dock station
157 * @handle: the acpi_handle of the device we want
159 * iterate over the dependent device list for this dock. If the
160 * dependent device matches the handle, return.
162 static struct dock_dependent_device *
163 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
165 struct dock_dependent_device *dd;
167 spin_lock(&ds->dd_lock);
168 list_for_each_entry(dd, &ds->dependent_devices, list) {
169 if (handle == dd->handle) {
170 spin_unlock(&ds->dd_lock);
174 spin_unlock(&ds->dd_lock);
178 /*****************************************************************************
180 *****************************************************************************/
182 * is_dock - see if a device is a dock station
183 * @handle: acpi handle of the device
185 * If an acpi object has a _DCK method, then it is by definition a dock
186 * station, so return true.
188 static int is_dock(acpi_handle handle)
193 status = acpi_get_handle(handle, "_DCK", &tmp);
194 if (ACPI_FAILURE(status))
199 static int is_ejectable(acpi_handle handle)
204 status = acpi_get_handle(handle, "_EJ0", &tmp);
205 if (ACPI_FAILURE(status))
210 static int is_ata(acpi_handle handle)
214 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
215 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
216 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
217 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
223 static int is_battery(acpi_handle handle)
225 struct acpi_device_info *info;
228 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
230 if (!(info->valid & ACPI_VALID_HID))
233 ret = !strcmp("PNP0C0A", info->hardware_id.string);
239 static int is_ejectable_bay(acpi_handle handle)
242 if (!is_ejectable(handle))
244 if (is_battery(handle) || is_ata(handle))
246 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
252 * is_dock_device - see if a device is on a dock station
253 * @handle: acpi handle of the device
255 * If this device is either the dock station itself,
256 * or is a device dependent on the dock station, then it
259 int is_dock_device(acpi_handle handle)
261 struct dock_station *dock_station;
263 if (!dock_station_count)
268 list_for_each_entry(dock_station, &dock_stations, sibling) {
269 if (find_dock_dependent_device(dock_station, handle))
276 EXPORT_SYMBOL_GPL(is_dock_device);
279 * dock_present - see if the dock station is present.
280 * @ds: the dock station
282 * execute the _STA method. note that present does not
283 * imply that we are docked.
285 static int dock_present(struct dock_station *ds)
287 unsigned long long sta;
291 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
292 if (ACPI_SUCCESS(status) && sta)
301 * dock_create_acpi_device - add new devices to acpi
302 * @handle - handle of the device to add
304 * This function will create a new acpi_device for the given
305 * handle if one does not exist already. This should cause
306 * acpi to scan for drivers for the given devices, and call
307 * matching driver's add routine.
309 * Returns a pointer to the acpi_device corresponding to the handle.
311 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
313 struct acpi_device *device = NULL;
314 struct acpi_device *parent_device;
318 if (acpi_bus_get_device(handle, &device)) {
320 * no device created for this object,
321 * so we should create one.
323 acpi_get_parent(handle, &parent);
324 if (acpi_bus_get_device(parent, &parent_device))
325 parent_device = NULL;
327 ret = acpi_bus_add(&device, parent_device, handle,
328 ACPI_BUS_TYPE_DEVICE);
330 pr_debug("error adding bus, %x\n",
339 * dock_remove_acpi_device - remove the acpi_device struct from acpi
340 * @handle - the handle of the device to remove
342 * Tell acpi to remove the acpi_device. This should cause any loaded
343 * driver to have it's remove routine called.
345 static void dock_remove_acpi_device(acpi_handle handle)
347 struct acpi_device *device;
350 if (!acpi_bus_get_device(handle, &device)) {
351 ret = acpi_bus_trim(device, 1);
353 pr_debug("error removing bus, %x\n", -ret);
359 * hotplug_dock_devices - insert or remove devices on the dock station
360 * @ds: the dock station
361 * @event: either bus check or eject request
363 * Some devices on the dock station need to have drivers called
364 * to perform hotplug operations after a dock event has occurred.
365 * Traverse the list of dock devices that have registered a
366 * hotplug handler, and call the handler.
368 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
370 struct dock_dependent_device *dd;
372 mutex_lock(&ds->hp_lock);
375 * First call driver specific hotplug functions
377 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
378 if (dd->ops && dd->ops->handler)
379 dd->ops->handler(dd->handle, event, dd->context);
383 * Now make sure that an acpi_device is created for each
384 * dependent device, or removed if this is an eject request.
385 * This will cause acpi_drivers to be stopped/started if they
388 list_for_each_entry(dd, &ds->dependent_devices, list) {
389 if (event == ACPI_NOTIFY_EJECT_REQUEST)
390 dock_remove_acpi_device(dd->handle);
392 dock_create_acpi_device(dd->handle);
394 mutex_unlock(&ds->hp_lock);
397 static void dock_event(struct dock_station *ds, u32 event, int num)
399 struct device *dev = &ds->dock_device->dev;
400 char event_string[13];
401 char *envp[] = { event_string, NULL };
402 struct dock_dependent_device *dd;
404 if (num == UNDOCK_EVENT)
405 sprintf(event_string, "EVENT=undock");
407 sprintf(event_string, "EVENT=dock");
410 * Indicate that the status of the dock station has
413 if (num == DOCK_EVENT)
414 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
416 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
417 if (dd->ops && dd->ops->uevent)
418 dd->ops->uevent(dd->handle, event, dd->context);
419 if (num != DOCK_EVENT)
420 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
424 * eject_dock - respond to a dock eject request
425 * @ds: the dock station
427 * This is called after _DCK is called, to execute the dock station's
430 static void eject_dock(struct dock_station *ds)
432 struct acpi_object_list arg_list;
433 union acpi_object arg;
437 /* all dock devices should have _EJ0, but check anyway */
438 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
439 if (ACPI_FAILURE(status)) {
440 pr_debug("No _EJ0 support for dock device\n");
445 arg_list.pointer = &arg;
446 arg.type = ACPI_TYPE_INTEGER;
447 arg.integer.value = 1;
449 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
451 pr_debug("Failed to evaluate _EJ0!\n");
455 * handle_dock - handle a dock event
456 * @ds: the dock station
457 * @dock: to dock, or undock - that is the question
459 * Execute the _DCK method in response to an acpi event
461 static void handle_dock(struct dock_station *ds, int dock)
464 struct acpi_object_list arg_list;
465 union acpi_object arg;
466 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
467 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
469 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
471 printk(KERN_INFO PREFIX "%s - %s\n",
472 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
474 /* _DCK method has one argument */
476 arg_list.pointer = &arg;
477 arg.type = ACPI_TYPE_INTEGER;
478 arg.integer.value = dock;
479 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
480 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
481 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
482 " _DCK\n", (char *)name_buffer.pointer));
484 kfree(buffer.pointer);
485 kfree(name_buffer.pointer);
488 static inline void dock(struct dock_station *ds)
493 static inline void undock(struct dock_station *ds)
498 static inline void begin_dock(struct dock_station *ds)
500 ds->flags |= DOCK_DOCKING;
503 static inline void complete_dock(struct dock_station *ds)
505 ds->flags &= ~(DOCK_DOCKING);
506 ds->last_dock_time = jiffies;
509 static inline void begin_undock(struct dock_station *ds)
511 ds->flags |= DOCK_UNDOCKING;
514 static inline void complete_undock(struct dock_station *ds)
516 ds->flags &= ~(DOCK_UNDOCKING);
519 static void dock_lock(struct dock_station *ds, int lock)
521 struct acpi_object_list arg_list;
522 union acpi_object arg;
526 arg_list.pointer = &arg;
527 arg.type = ACPI_TYPE_INTEGER;
528 arg.integer.value = !!lock;
529 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
530 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
532 printk(KERN_WARNING PREFIX "Locking device failed\n");
534 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
539 * dock_in_progress - see if we are in the middle of handling a dock event
540 * @ds: the dock station
542 * Sometimes while docking, false dock events can be sent to the driver
543 * because good connections aren't made or some other reason. Ignore these
544 * if we are in the middle of doing something.
546 static int dock_in_progress(struct dock_station *ds)
548 if ((ds->flags & DOCK_DOCKING) ||
549 time_before(jiffies, (ds->last_dock_time + HZ)))
555 * register_dock_notifier - add yourself to the dock notifier list
556 * @nb: the callers notifier block
558 * If a driver wishes to be notified about dock events, they can
559 * use this function to put a notifier block on the dock notifier list.
560 * this notifier call chain will be called after a dock event, but
561 * before hotplugging any new devices.
563 int register_dock_notifier(struct notifier_block *nb)
565 if (!dock_station_count)
568 return atomic_notifier_chain_register(&dock_notifier_list, nb);
571 EXPORT_SYMBOL_GPL(register_dock_notifier);
574 * unregister_dock_notifier - remove yourself from the dock notifier list
575 * @nb: the callers notifier block
577 void unregister_dock_notifier(struct notifier_block *nb)
579 if (!dock_station_count)
582 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
585 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
588 * register_hotplug_dock_device - register a hotplug function
589 * @handle: the handle of the device
590 * @ops: handlers to call after docking
591 * @context: device specific data
593 * If a driver would like to perform a hotplug operation after a dock
594 * event, they can register an acpi_notifiy_handler to be called by
595 * the dock driver after _DCK is executed.
598 register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
601 struct dock_dependent_device *dd;
602 struct dock_station *dock_station;
605 if (!dock_station_count)
609 * make sure this handle is for a device dependent on the dock,
610 * this would include the dock station itself
612 list_for_each_entry(dock_station, &dock_stations, sibling) {
614 * An ATA bay can be in a dock and itself can be ejected
615 * seperately, so there are two 'dock stations' which need the
618 dd = find_dock_dependent_device(dock_station, handle);
621 dd->context = context;
622 dock_add_hotplug_device(dock_station, dd);
630 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
633 * unregister_hotplug_dock_device - remove yourself from the hotplug list
634 * @handle: the acpi handle of the device
636 void unregister_hotplug_dock_device(acpi_handle handle)
638 struct dock_dependent_device *dd;
639 struct dock_station *dock_station;
641 if (!dock_station_count)
644 list_for_each_entry(dock_station, &dock_stations, sibling) {
645 dd = find_dock_dependent_device(dock_station, handle);
647 dock_del_hotplug_device(dock_station, dd);
651 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
654 * handle_eject_request - handle an undock request checking for error conditions
656 * Check to make sure the dock device is still present, then undock and
657 * hotremove all the devices that may need removing.
659 static int handle_eject_request(struct dock_station *ds, u32 event)
661 if (dock_in_progress(ds))
665 * here we need to generate the undock
666 * event prior to actually doing the undock
667 * so that the device struct still exists.
668 * Also, even send the dock event if the
669 * device is not present anymore
671 dock_event(ds, event, UNDOCK_EVENT);
673 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
677 if (dock_present(ds)) {
678 printk(KERN_ERR PREFIX "Unable to undock!\n");
686 * dock_notify - act upon an acpi dock notification
687 * @handle: the dock station handle
688 * @event: the acpi event
689 * @data: our driver data struct
691 * If we are notified to dock, then check to see if the dock is
692 * present and then dock. Notify all drivers of the dock event,
693 * and then hotplug and devices that may need hotplugging.
695 static void dock_notify(acpi_handle handle, u32 event, void *data)
697 struct dock_station *ds = data;
698 struct acpi_device *tmp;
699 int surprise_removal = 0;
702 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
703 * is sent and _DCK is present, it is assumed to mean an undock
706 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
707 event = ACPI_NOTIFY_EJECT_REQUEST;
710 * dock station: BUS_CHECK - docked or surprise removal
711 * DEVICE_CHECK - undocked
712 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
714 * To simplify event handling, dock dependent device handler always
715 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
716 * ACPI_NOTIFY_EJECT_REQUEST for removal
719 case ACPI_NOTIFY_BUS_CHECK:
720 case ACPI_NOTIFY_DEVICE_CHECK:
721 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
725 if (!dock_present(ds)) {
726 printk(KERN_ERR PREFIX "Unable to dock!\n");
730 atomic_notifier_call_chain(&dock_notifier_list,
732 hotplug_dock_devices(ds, event);
734 dock_event(ds, event, DOCK_EVENT);
738 if (dock_present(ds) || dock_in_progress(ds))
740 /* This is a surprise removal */
741 surprise_removal = 1;
742 event = ACPI_NOTIFY_EJECT_REQUEST;
744 case ACPI_NOTIFY_EJECT_REQUEST:
746 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
748 handle_eject_request(ds, event);
750 dock_event(ds, event, UNDOCK_EVENT);
753 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
760 struct dock_station *ds;
763 static void acpi_dock_deferred_cb(void *context)
765 struct dock_data *data = (struct dock_data *)context;
767 dock_notify(data->handle, data->event, data->ds);
771 static int acpi_dock_notifier_call(struct notifier_block *this,
772 unsigned long event, void *data)
774 struct dock_station *dock_station;
775 acpi_handle handle = (acpi_handle)data;
777 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
778 && event != ACPI_NOTIFY_EJECT_REQUEST)
780 list_for_each_entry(dock_station, &dock_stations, sibling) {
781 if (dock_station->handle == handle) {
782 struct dock_data *dock_data;
784 dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
787 dock_data->handle = handle;
788 dock_data->event = event;
789 dock_data->ds = dock_station;
790 acpi_os_hotplug_execute(acpi_dock_deferred_cb,
798 static struct notifier_block dock_acpi_notifier = {
799 .notifier_call = acpi_dock_notifier_call,
803 * find_dock_devices - find devices on the dock station
804 * @handle: the handle of the device we are examining
806 * @context: the dock station private data
809 * This function is called by acpi_walk_namespace. It will
810 * check to see if an object has an _EJD method. If it does, then it
811 * will see if it is dependent on the dock station.
814 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
817 acpi_handle tmp, parent;
818 struct dock_station *ds = context;
820 status = acpi_bus_get_ejd(handle, &tmp);
821 if (ACPI_FAILURE(status)) {
822 /* try the parent device as well */
823 status = acpi_get_parent(handle, &parent);
824 if (ACPI_FAILURE(status))
826 /* see if parent is dependent on dock */
827 status = acpi_bus_get_ejd(parent, &tmp);
828 if (ACPI_FAILURE(status))
832 if (tmp == ds->handle)
833 add_dock_dependent_device(ds, handle);
840 * show_docked - read method for "docked" file in sysfs
842 static ssize_t show_docked(struct device *dev,
843 struct device_attribute *attr, char *buf)
845 struct acpi_device *tmp;
847 struct dock_station *dock_station = *((struct dock_station **)
850 if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
851 return snprintf(buf, PAGE_SIZE, "1\n");
852 return snprintf(buf, PAGE_SIZE, "0\n");
854 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
857 * show_flags - read method for flags file in sysfs
859 static ssize_t show_flags(struct device *dev,
860 struct device_attribute *attr, char *buf)
862 struct dock_station *dock_station = *((struct dock_station **)
864 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
867 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
870 * write_undock - write method for "undock" file in sysfs
872 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
873 const char *buf, size_t count)
876 struct dock_station *dock_station = *((struct dock_station **)
882 begin_undock(dock_station);
883 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
884 return ret ? ret: count;
886 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
889 * show_dock_uid - read method for "uid" file in sysfs
891 static ssize_t show_dock_uid(struct device *dev,
892 struct device_attribute *attr, char *buf)
894 unsigned long long lbuf;
895 struct dock_station *dock_station = *((struct dock_station **)
897 acpi_status status = acpi_evaluate_integer(dock_station->handle,
898 "_UID", NULL, &lbuf);
899 if (ACPI_FAILURE(status))
902 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
904 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
906 static ssize_t show_dock_type(struct device *dev,
907 struct device_attribute *attr, char *buf)
909 struct dock_station *dock_station = *((struct dock_station **)
913 if (dock_station->flags & DOCK_IS_DOCK)
914 type = "dock_station";
915 else if (dock_station->flags & DOCK_IS_ATA)
917 else if (dock_station->flags & DOCK_IS_BAT)
918 type = "battery_bay";
922 return snprintf(buf, PAGE_SIZE, "%s\n", type);
924 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
926 static struct attribute *dock_attributes[] = {
927 &dev_attr_docked.attr,
928 &dev_attr_flags.attr,
929 &dev_attr_undock.attr,
935 static struct attribute_group dock_attribute_group = {
936 .attrs = dock_attributes
940 * dock_add - add a new dock station
941 * @handle: the dock station handle
943 * allocated and initialize a new dock station device. Find all devices
944 * that are on the dock station, and register for dock event notifications.
946 static int dock_add(acpi_handle handle)
949 struct dock_station *dock_station;
950 struct platform_device *dock_device;
952 /* allocate & initialize the dock_station private data */
953 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
956 dock_station->handle = handle;
957 dock_station->last_dock_time = jiffies - HZ;
958 INIT_LIST_HEAD(&dock_station->dependent_devices);
959 INIT_LIST_HEAD(&dock_station->hotplug_devices);
960 INIT_LIST_HEAD(&dock_station->sibling);
961 spin_lock_init(&dock_station->dd_lock);
962 mutex_init(&dock_station->hp_lock);
963 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
965 /* initialize platform device stuff */
966 dock_station->dock_device =
967 platform_device_register_simple(dock_device_name,
968 dock_station_count, NULL, 0);
969 dock_device = dock_station->dock_device;
970 if (IS_ERR(dock_device)) {
971 ret = PTR_ERR(dock_device);
974 platform_device_add_data(dock_device, &dock_station,
975 sizeof(struct dock_station *));
977 /* we want the dock device to send uevents */
978 dev_set_uevent_suppress(&dock_device->dev, 0);
981 dock_station->flags |= DOCK_IS_DOCK;
983 dock_station->flags |= DOCK_IS_ATA;
984 if (is_battery(handle))
985 dock_station->flags |= DOCK_IS_BAT;
987 ret = sysfs_create_group(&dock_device->dev.kobj, &dock_attribute_group);
991 /* Find dependent devices */
992 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
993 ACPI_UINT32_MAX, find_dock_devices, dock_station,
996 /* add the dock station as a device dependent on itself */
997 ret = add_dock_dependent_device(dock_station, handle);
1001 dock_station_count++;
1002 list_add(&dock_station->sibling, &dock_stations);
1006 sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
1008 platform_device_unregister(dock_device);
1010 kfree(dock_station);
1011 dock_station = NULL;
1012 printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
1017 * dock_remove - free up resources related to the dock station
1019 static int dock_remove(struct dock_station *dock_station)
1021 struct dock_dependent_device *dd, *tmp;
1022 struct platform_device *dock_device = dock_station->dock_device;
1024 if (!dock_station_count)
1027 /* remove dependent devices */
1028 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1033 sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
1034 platform_device_unregister(dock_device);
1036 /* free dock station memory */
1037 kfree(dock_station);
1038 dock_station = NULL;
1043 * find_dock - look for a dock station
1044 * @handle: acpi handle of a device
1046 * @context: counter of dock stations found
1049 * This is called by acpi_walk_namespace to look for dock stations.
1052 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1054 acpi_status status = AE_OK;
1056 if (is_dock(handle)) {
1057 if (dock_add(handle) >= 0) {
1058 status = AE_CTRL_TERMINATE;
1065 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1067 /* If bay is a dock, it's already handled */
1068 if (is_ejectable_bay(handle) && !is_dock(handle))
1073 static int __init dock_init(void)
1078 /* look for a dock station */
1079 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1080 ACPI_UINT32_MAX, find_dock, NULL, NULL);
1083 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1084 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1085 if (!dock_station_count) {
1086 printk(KERN_INFO PREFIX "No dock devices found.\n");
1090 register_acpi_bus_notifier(&dock_acpi_notifier);
1091 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1092 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
1096 static void __exit dock_exit(void)
1098 struct dock_station *dock_station;
1099 struct dock_station *tmp;
1101 unregister_acpi_bus_notifier(&dock_acpi_notifier);
1102 list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
1103 dock_remove(dock_station);
1107 * Must be called before drivers of devices in dock, otherwise we can't know
1108 * which devices are in a dock
1110 subsys_initcall(dock_init);
1111 module_exit(dock_exit);