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 sibiling;
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 * alloc_dock_dependent_device - allocate and init a dependent device
97 * @handle: the acpi_handle of the dependent device
99 * Allocate memory for a dependent device structure for a device referenced
102 static struct dock_dependent_device *
103 alloc_dock_dependent_device(acpi_handle handle)
105 struct dock_dependent_device *dd;
107 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
110 INIT_LIST_HEAD(&dd->list);
111 INIT_LIST_HEAD(&dd->hotplug_list);
117 * add_dock_dependent_device - associate a device with the dock station
118 * @ds: The dock station
119 * @dd: The dependent device
121 * Add the dependent device to the dock's dependent device list.
124 add_dock_dependent_device(struct dock_station *ds,
125 struct dock_dependent_device *dd)
127 spin_lock(&ds->dd_lock);
128 list_add_tail(&dd->list, &ds->dependent_devices);
129 spin_unlock(&ds->dd_lock);
133 * dock_add_hotplug_device - associate a hotplug handler with the dock station
134 * @ds: The dock station
135 * @dd: The dependent device struct
137 * Add the dependent device to the dock's hotplug device list
140 dock_add_hotplug_device(struct dock_station *ds,
141 struct dock_dependent_device *dd)
143 mutex_lock(&ds->hp_lock);
144 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
145 mutex_unlock(&ds->hp_lock);
149 * dock_del_hotplug_device - remove a hotplug handler from the dock station
150 * @ds: The dock station
151 * @dd: the dependent device struct
153 * Delete the dependent device from the dock's hotplug device list
156 dock_del_hotplug_device(struct dock_station *ds,
157 struct dock_dependent_device *dd)
159 mutex_lock(&ds->hp_lock);
160 list_del(&dd->hotplug_list);
161 mutex_unlock(&ds->hp_lock);
165 * find_dock_dependent_device - get a device dependent on this dock
166 * @ds: the dock station
167 * @handle: the acpi_handle of the device we want
169 * iterate over the dependent device list for this dock. If the
170 * dependent device matches the handle, return.
172 static struct dock_dependent_device *
173 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
175 struct dock_dependent_device *dd;
177 spin_lock(&ds->dd_lock);
178 list_for_each_entry(dd, &ds->dependent_devices, list) {
179 if (handle == dd->handle) {
180 spin_unlock(&ds->dd_lock);
184 spin_unlock(&ds->dd_lock);
188 /*****************************************************************************
190 *****************************************************************************/
192 * is_dock - see if a device is a dock station
193 * @handle: acpi handle of the device
195 * If an acpi object has a _DCK method, then it is by definition a dock
196 * station, so return true.
198 static int is_dock(acpi_handle handle)
203 status = acpi_get_handle(handle, "_DCK", &tmp);
204 if (ACPI_FAILURE(status))
209 static int is_ejectable(acpi_handle handle)
214 status = acpi_get_handle(handle, "_EJ0", &tmp);
215 if (ACPI_FAILURE(status))
220 static int is_ata(acpi_handle handle)
224 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
225 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
226 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
227 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
233 static int is_battery(acpi_handle handle)
235 struct acpi_device_info *info;
236 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
239 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &buffer)))
241 info = buffer.pointer;
242 if (!(info->valid & ACPI_VALID_HID))
245 ret = !strcmp("PNP0C0A", info->hardware_id.value);
247 kfree(buffer.pointer);
251 static int is_ejectable_bay(acpi_handle handle)
254 if (!is_ejectable(handle))
256 if (is_battery(handle) || is_ata(handle))
258 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
264 * is_dock_device - see if a device is on a dock station
265 * @handle: acpi handle of the device
267 * If this device is either the dock station itself,
268 * or is a device dependent on the dock station, then it
271 int is_dock_device(acpi_handle handle)
273 struct dock_station *dock_station;
275 if (!dock_station_count)
280 list_for_each_entry(dock_station, &dock_stations, sibiling) {
281 if (find_dock_dependent_device(dock_station, handle))
288 EXPORT_SYMBOL_GPL(is_dock_device);
291 * dock_present - see if the dock station is present.
292 * @ds: the dock station
294 * execute the _STA method. note that present does not
295 * imply that we are docked.
297 static int dock_present(struct dock_station *ds)
299 unsigned long long sta;
303 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
304 if (ACPI_SUCCESS(status) && sta)
313 * dock_create_acpi_device - add new devices to acpi
314 * @handle - handle of the device to add
316 * This function will create a new acpi_device for the given
317 * handle if one does not exist already. This should cause
318 * acpi to scan for drivers for the given devices, and call
319 * matching driver's add routine.
321 * Returns a pointer to the acpi_device corresponding to the handle.
323 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
325 struct acpi_device *device = NULL;
326 struct acpi_device *parent_device;
330 if (acpi_bus_get_device(handle, &device)) {
332 * no device created for this object,
333 * so we should create one.
335 acpi_get_parent(handle, &parent);
336 if (acpi_bus_get_device(parent, &parent_device))
337 parent_device = NULL;
339 ret = acpi_bus_add(&device, parent_device, handle,
340 ACPI_BUS_TYPE_DEVICE);
342 pr_debug("error adding bus, %x\n",
351 * dock_remove_acpi_device - remove the acpi_device struct from acpi
352 * @handle - the handle of the device to remove
354 * Tell acpi to remove the acpi_device. This should cause any loaded
355 * driver to have it's remove routine called.
357 static void dock_remove_acpi_device(acpi_handle handle)
359 struct acpi_device *device;
362 if (!acpi_bus_get_device(handle, &device)) {
363 ret = acpi_bus_trim(device, 1);
365 pr_debug("error removing bus, %x\n", -ret);
371 * hotplug_dock_devices - insert or remove devices on the dock station
372 * @ds: the dock station
373 * @event: either bus check or eject request
375 * Some devices on the dock station need to have drivers called
376 * to perform hotplug operations after a dock event has occurred.
377 * Traverse the list of dock devices that have registered a
378 * hotplug handler, and call the handler.
380 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
382 struct dock_dependent_device *dd;
384 mutex_lock(&ds->hp_lock);
387 * First call driver specific hotplug functions
389 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
390 if (dd->ops && dd->ops->handler)
391 dd->ops->handler(dd->handle, event, dd->context);
395 * Now make sure that an acpi_device is created for each
396 * dependent device, or removed if this is an eject request.
397 * This will cause acpi_drivers to be stopped/started if they
400 list_for_each_entry(dd, &ds->dependent_devices, list) {
401 if (event == ACPI_NOTIFY_EJECT_REQUEST)
402 dock_remove_acpi_device(dd->handle);
404 dock_create_acpi_device(dd->handle);
406 mutex_unlock(&ds->hp_lock);
409 static void dock_event(struct dock_station *ds, u32 event, int num)
411 struct device *dev = &ds->dock_device->dev;
412 char event_string[13];
413 char *envp[] = { event_string, NULL };
414 struct dock_dependent_device *dd;
416 if (num == UNDOCK_EVENT)
417 sprintf(event_string, "EVENT=undock");
419 sprintf(event_string, "EVENT=dock");
422 * Indicate that the status of the dock station has
425 if (num == DOCK_EVENT)
426 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
428 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
429 if (dd->ops && dd->ops->uevent)
430 dd->ops->uevent(dd->handle, event, dd->context);
431 if (num != DOCK_EVENT)
432 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
436 * eject_dock - respond to a dock eject request
437 * @ds: the dock station
439 * This is called after _DCK is called, to execute the dock station's
442 static void eject_dock(struct dock_station *ds)
444 struct acpi_object_list arg_list;
445 union acpi_object arg;
449 /* all dock devices should have _EJ0, but check anyway */
450 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
451 if (ACPI_FAILURE(status)) {
452 pr_debug("No _EJ0 support for dock device\n");
457 arg_list.pointer = &arg;
458 arg.type = ACPI_TYPE_INTEGER;
459 arg.integer.value = 1;
461 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
463 pr_debug("Failed to evaluate _EJ0!\n");
467 * handle_dock - handle a dock event
468 * @ds: the dock station
469 * @dock: to dock, or undock - that is the question
471 * Execute the _DCK method in response to an acpi event
473 static void handle_dock(struct dock_station *ds, int dock)
476 struct acpi_object_list arg_list;
477 union acpi_object arg;
478 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
479 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
481 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
483 printk(KERN_INFO PREFIX "%s - %s\n",
484 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
486 /* _DCK method has one argument */
488 arg_list.pointer = &arg;
489 arg.type = ACPI_TYPE_INTEGER;
490 arg.integer.value = dock;
491 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
492 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
493 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
494 " _DCK\n", (char *)name_buffer.pointer));
496 kfree(buffer.pointer);
497 kfree(name_buffer.pointer);
500 static inline void dock(struct dock_station *ds)
505 static inline void undock(struct dock_station *ds)
510 static inline void begin_dock(struct dock_station *ds)
512 ds->flags |= DOCK_DOCKING;
515 static inline void complete_dock(struct dock_station *ds)
517 ds->flags &= ~(DOCK_DOCKING);
518 ds->last_dock_time = jiffies;
521 static inline void begin_undock(struct dock_station *ds)
523 ds->flags |= DOCK_UNDOCKING;
526 static inline void complete_undock(struct dock_station *ds)
528 ds->flags &= ~(DOCK_UNDOCKING);
531 static void dock_lock(struct dock_station *ds, int lock)
533 struct acpi_object_list arg_list;
534 union acpi_object arg;
538 arg_list.pointer = &arg;
539 arg.type = ACPI_TYPE_INTEGER;
540 arg.integer.value = !!lock;
541 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
542 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
544 printk(KERN_WARNING PREFIX "Locking device failed\n");
546 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
551 * dock_in_progress - see if we are in the middle of handling a dock event
552 * @ds: the dock station
554 * Sometimes while docking, false dock events can be sent to the driver
555 * because good connections aren't made or some other reason. Ignore these
556 * if we are in the middle of doing something.
558 static int dock_in_progress(struct dock_station *ds)
560 if ((ds->flags & DOCK_DOCKING) ||
561 time_before(jiffies, (ds->last_dock_time + HZ)))
567 * register_dock_notifier - add yourself to the dock notifier list
568 * @nb: the callers notifier block
570 * If a driver wishes to be notified about dock events, they can
571 * use this function to put a notifier block on the dock notifier list.
572 * this notifier call chain will be called after a dock event, but
573 * before hotplugging any new devices.
575 int register_dock_notifier(struct notifier_block *nb)
577 if (!dock_station_count)
580 return atomic_notifier_chain_register(&dock_notifier_list, nb);
583 EXPORT_SYMBOL_GPL(register_dock_notifier);
586 * unregister_dock_notifier - remove yourself from the dock notifier list
587 * @nb: the callers notifier block
589 void unregister_dock_notifier(struct notifier_block *nb)
591 if (!dock_station_count)
594 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
597 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
600 * register_hotplug_dock_device - register a hotplug function
601 * @handle: the handle of the device
602 * @ops: handlers to call after docking
603 * @context: device specific data
605 * If a driver would like to perform a hotplug operation after a dock
606 * event, they can register an acpi_notifiy_handler to be called by
607 * the dock driver after _DCK is executed.
610 register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
613 struct dock_dependent_device *dd;
614 struct dock_station *dock_station;
617 if (!dock_station_count)
621 * make sure this handle is for a device dependent on the dock,
622 * this would include the dock station itself
624 list_for_each_entry(dock_station, &dock_stations, sibiling) {
626 * An ATA bay can be in a dock and itself can be ejected
627 * seperately, so there are two 'dock stations' which need the
630 dd = find_dock_dependent_device(dock_station, handle);
633 dd->context = context;
634 dock_add_hotplug_device(dock_station, dd);
642 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
645 * unregister_hotplug_dock_device - remove yourself from the hotplug list
646 * @handle: the acpi handle of the device
648 void unregister_hotplug_dock_device(acpi_handle handle)
650 struct dock_dependent_device *dd;
651 struct dock_station *dock_station;
653 if (!dock_station_count)
656 list_for_each_entry(dock_station, &dock_stations, sibiling) {
657 dd = find_dock_dependent_device(dock_station, handle);
659 dock_del_hotplug_device(dock_station, dd);
663 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
666 * handle_eject_request - handle an undock request checking for error conditions
668 * Check to make sure the dock device is still present, then undock and
669 * hotremove all the devices that may need removing.
671 static int handle_eject_request(struct dock_station *ds, u32 event)
673 if (dock_in_progress(ds))
677 * here we need to generate the undock
678 * event prior to actually doing the undock
679 * so that the device struct still exists.
680 * Also, even send the dock event if the
681 * device is not present anymore
683 dock_event(ds, event, UNDOCK_EVENT);
685 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
689 if (dock_present(ds)) {
690 printk(KERN_ERR PREFIX "Unable to undock!\n");
698 * dock_notify - act upon an acpi dock notification
699 * @handle: the dock station handle
700 * @event: the acpi event
701 * @data: our driver data struct
703 * If we are notified to dock, then check to see if the dock is
704 * present and then dock. Notify all drivers of the dock event,
705 * and then hotplug and devices that may need hotplugging.
707 static void dock_notify(acpi_handle handle, u32 event, void *data)
709 struct dock_station *ds = data;
710 struct acpi_device *tmp;
711 int surprise_removal = 0;
714 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
715 * is sent and _DCK is present, it is assumed to mean an undock
718 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
719 event = ACPI_NOTIFY_EJECT_REQUEST;
722 * dock station: BUS_CHECK - docked or surprise removal
723 * DEVICE_CHECK - undocked
724 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
726 * To simplify event handling, dock dependent device handler always
727 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
728 * ACPI_NOTIFY_EJECT_REQUEST for removal
731 case ACPI_NOTIFY_BUS_CHECK:
732 case ACPI_NOTIFY_DEVICE_CHECK:
733 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
737 if (!dock_present(ds)) {
738 printk(KERN_ERR PREFIX "Unable to dock!\n");
742 atomic_notifier_call_chain(&dock_notifier_list,
744 hotplug_dock_devices(ds, event);
746 dock_event(ds, event, DOCK_EVENT);
750 if (dock_present(ds) || dock_in_progress(ds))
752 /* This is a surprise removal */
753 surprise_removal = 1;
754 event = ACPI_NOTIFY_EJECT_REQUEST;
756 case ACPI_NOTIFY_EJECT_REQUEST:
758 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
760 handle_eject_request(ds, event);
762 dock_event(ds, event, UNDOCK_EVENT);
765 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
772 struct dock_station *ds;
775 static void acpi_dock_deferred_cb(void *context)
777 struct dock_data *data = (struct dock_data *)context;
779 dock_notify(data->handle, data->event, data->ds);
783 static int acpi_dock_notifier_call(struct notifier_block *this,
784 unsigned long event, void *data)
786 struct dock_station *dock_station;
787 acpi_handle handle = (acpi_handle)data;
789 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
790 && event != ACPI_NOTIFY_EJECT_REQUEST)
792 list_for_each_entry(dock_station, &dock_stations, sibiling) {
793 if (dock_station->handle == handle) {
794 struct dock_data *dock_data;
796 dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
799 dock_data->handle = handle;
800 dock_data->event = event;
801 dock_data->ds = dock_station;
802 acpi_os_hotplug_execute(acpi_dock_deferred_cb,
810 static struct notifier_block dock_acpi_notifier = {
811 .notifier_call = acpi_dock_notifier_call,
815 * find_dock_devices - find devices on the dock station
816 * @handle: the handle of the device we are examining
818 * @context: the dock station private data
821 * This function is called by acpi_walk_namespace. It will
822 * check to see if an object has an _EJD method. If it does, then it
823 * will see if it is dependent on the dock station.
826 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
829 acpi_handle tmp, parent;
830 struct dock_station *ds = context;
831 struct dock_dependent_device *dd;
833 status = acpi_bus_get_ejd(handle, &tmp);
834 if (ACPI_FAILURE(status)) {
835 /* try the parent device as well */
836 status = acpi_get_parent(handle, &parent);
837 if (ACPI_FAILURE(status))
839 /* see if parent is dependent on dock */
840 status = acpi_bus_get_ejd(parent, &tmp);
841 if (ACPI_FAILURE(status))
845 if (tmp == ds->handle) {
846 dd = alloc_dock_dependent_device(handle);
848 add_dock_dependent_device(ds, dd);
855 * show_docked - read method for "docked" file in sysfs
857 static ssize_t show_docked(struct device *dev,
858 struct device_attribute *attr, char *buf)
860 struct acpi_device *tmp;
862 struct dock_station *dock_station = *((struct dock_station **)
865 if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
866 return snprintf(buf, PAGE_SIZE, "1\n");
867 return snprintf(buf, PAGE_SIZE, "0\n");
869 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
872 * show_flags - read method for flags file in sysfs
874 static ssize_t show_flags(struct device *dev,
875 struct device_attribute *attr, char *buf)
877 struct dock_station *dock_station = *((struct dock_station **)
879 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
882 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
885 * write_undock - write method for "undock" file in sysfs
887 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
888 const char *buf, size_t count)
891 struct dock_station *dock_station = *((struct dock_station **)
897 begin_undock(dock_station);
898 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
899 return ret ? ret: count;
901 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
904 * show_dock_uid - read method for "uid" file in sysfs
906 static ssize_t show_dock_uid(struct device *dev,
907 struct device_attribute *attr, char *buf)
909 unsigned long long lbuf;
910 struct dock_station *dock_station = *((struct dock_station **)
912 acpi_status status = acpi_evaluate_integer(dock_station->handle,
913 "_UID", NULL, &lbuf);
914 if (ACPI_FAILURE(status))
917 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
919 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
921 static ssize_t show_dock_type(struct device *dev,
922 struct device_attribute *attr, char *buf)
924 struct dock_station *dock_station = *((struct dock_station **)
928 if (dock_station->flags & DOCK_IS_DOCK)
929 type = "dock_station";
930 else if (dock_station->flags & DOCK_IS_ATA)
932 else if (dock_station->flags & DOCK_IS_BAT)
933 type = "battery_bay";
937 return snprintf(buf, PAGE_SIZE, "%s\n", type);
939 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
942 * dock_add - add a new dock station
943 * @handle: the dock station handle
945 * allocated and initialize a new dock station device. Find all devices
946 * that are on the dock station, and register for dock event notifications.
948 static int dock_add(acpi_handle handle)
951 struct dock_dependent_device *dd;
952 struct dock_station *dock_station;
953 struct platform_device *dock_device;
955 /* allocate & initialize the dock_station private data */
956 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
959 dock_station->handle = handle;
960 dock_station->last_dock_time = jiffies - HZ;
961 INIT_LIST_HEAD(&dock_station->dependent_devices);
962 INIT_LIST_HEAD(&dock_station->hotplug_devices);
963 INIT_LIST_HEAD(&dock_station->sibiling);
964 spin_lock_init(&dock_station->dd_lock);
965 mutex_init(&dock_station->hp_lock);
966 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
968 /* initialize platform device stuff */
969 dock_station->dock_device =
970 platform_device_register_simple(dock_device_name,
971 dock_station_count, NULL, 0);
972 dock_device = dock_station->dock_device;
973 if (IS_ERR(dock_device)) {
976 return PTR_ERR(dock_device);
978 platform_device_add_data(dock_device, &dock_station,
979 sizeof(struct dock_station *));
981 /* we want the dock device to send uevents */
982 dev_set_uevent_suppress(&dock_device->dev, 0);
985 dock_station->flags |= DOCK_IS_DOCK;
987 dock_station->flags |= DOCK_IS_ATA;
988 if (is_battery(handle))
989 dock_station->flags |= DOCK_IS_BAT;
991 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
993 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
994 platform_device_unregister(dock_device);
999 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
1001 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
1002 device_remove_file(&dock_device->dev, &dev_attr_docked);
1003 platform_device_unregister(dock_device);
1004 kfree(dock_station);
1005 dock_station = NULL;
1008 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
1010 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
1011 device_remove_file(&dock_device->dev, &dev_attr_docked);
1012 device_remove_file(&dock_device->dev, &dev_attr_undock);
1013 platform_device_unregister(dock_device);
1014 kfree(dock_station);
1015 dock_station = NULL;
1018 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
1020 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
1021 device_remove_file(&dock_device->dev, &dev_attr_docked);
1022 device_remove_file(&dock_device->dev, &dev_attr_undock);
1023 device_remove_file(&dock_device->dev, &dev_attr_uid);
1024 platform_device_unregister(dock_device);
1025 kfree(dock_station);
1026 dock_station = NULL;
1029 ret = device_create_file(&dock_device->dev, &dev_attr_type);
1031 printk(KERN_ERR"Error %d adding sysfs file\n", ret);
1033 /* Find dependent devices */
1034 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1035 ACPI_UINT32_MAX, find_dock_devices, dock_station,
1038 /* add the dock station as a device dependent on itself */
1039 dd = alloc_dock_dependent_device(handle);
1041 kfree(dock_station);
1042 dock_station = NULL;
1044 goto dock_add_err_unregister;
1046 add_dock_dependent_device(dock_station, dd);
1048 dock_station_count++;
1049 list_add(&dock_station->sibiling, &dock_stations);
1052 dock_add_err_unregister:
1053 device_remove_file(&dock_device->dev, &dev_attr_type);
1054 device_remove_file(&dock_device->dev, &dev_attr_docked);
1055 device_remove_file(&dock_device->dev, &dev_attr_undock);
1056 device_remove_file(&dock_device->dev, &dev_attr_uid);
1057 device_remove_file(&dock_device->dev, &dev_attr_flags);
1058 platform_device_unregister(dock_device);
1059 kfree(dock_station);
1060 dock_station = NULL;
1065 * dock_remove - free up resources related to the dock station
1067 static int dock_remove(struct dock_station *dock_station)
1069 struct dock_dependent_device *dd, *tmp;
1070 struct platform_device *dock_device = dock_station->dock_device;
1072 if (!dock_station_count)
1075 /* remove dependent devices */
1076 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1081 device_remove_file(&dock_device->dev, &dev_attr_type);
1082 device_remove_file(&dock_device->dev, &dev_attr_docked);
1083 device_remove_file(&dock_device->dev, &dev_attr_undock);
1084 device_remove_file(&dock_device->dev, &dev_attr_uid);
1085 device_remove_file(&dock_device->dev, &dev_attr_flags);
1086 platform_device_unregister(dock_device);
1088 /* free dock station memory */
1089 kfree(dock_station);
1090 dock_station = NULL;
1095 * find_dock - look for a dock station
1096 * @handle: acpi handle of a device
1098 * @context: counter of dock stations found
1101 * This is called by acpi_walk_namespace to look for dock stations.
1104 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1106 acpi_status status = AE_OK;
1108 if (is_dock(handle)) {
1109 if (dock_add(handle) >= 0) {
1110 status = AE_CTRL_TERMINATE;
1117 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1119 /* If bay is a dock, it's already handled */
1120 if (is_ejectable_bay(handle) && !is_dock(handle))
1125 static int __init dock_init(void)
1130 /* look for a dock station */
1131 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1132 ACPI_UINT32_MAX, find_dock, NULL, NULL);
1135 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1136 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1137 if (!dock_station_count) {
1138 printk(KERN_INFO PREFIX "No dock devices found.\n");
1142 register_acpi_bus_notifier(&dock_acpi_notifier);
1143 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1144 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
1148 static void __exit dock_exit(void)
1150 struct dock_station *dock_station;
1151 struct dock_station *tmp;
1153 unregister_acpi_bus_notifier(&dock_acpi_notifier);
1154 list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibiling)
1155 dock_remove(dock_station);
1159 * Must be called before drivers of devices in dock, otherwise we can't know
1160 * which devices are in a dock
1162 subsys_initcall(dock_init);
1163 module_exit(dock_exit);