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 char dock_device_name[] = "dock";
53 static const struct acpi_device_id dock_device_ids[] = {
57 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
61 unsigned long last_dock_time;
65 struct list_head dependent_devices;
66 struct list_head hotplug_devices;
68 struct list_head sibiling;
69 struct platform_device *dock_device;
71 static LIST_HEAD(dock_stations);
72 static int dock_station_count;
74 struct dock_dependent_device {
75 struct list_head list;
76 struct list_head hotplug_list;
78 struct acpi_dock_ops *ops;
82 #define DOCK_DOCKING 0x00000001
83 #define DOCK_UNDOCKING 0x00000002
84 #define DOCK_IS_DOCK 0x00000010
85 #define DOCK_IS_ATA 0x00000020
86 #define DOCK_IS_BAT 0x00000040
88 #define UNDOCK_EVENT 2
90 /*****************************************************************************
91 * Dock Dependent device functions *
92 *****************************************************************************/
94 * alloc_dock_dependent_device - allocate and init a dependent device
95 * @handle: the acpi_handle of the dependent device
97 * Allocate memory for a dependent device structure for a device referenced
100 static struct dock_dependent_device *
101 alloc_dock_dependent_device(acpi_handle handle)
103 struct dock_dependent_device *dd;
105 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
108 INIT_LIST_HEAD(&dd->list);
109 INIT_LIST_HEAD(&dd->hotplug_list);
115 * add_dock_dependent_device - associate a device with the dock station
116 * @ds: The dock station
117 * @dd: The dependent device
119 * Add the dependent device to the dock's dependent device list.
122 add_dock_dependent_device(struct dock_station *ds,
123 struct dock_dependent_device *dd)
125 spin_lock(&ds->dd_lock);
126 list_add_tail(&dd->list, &ds->dependent_devices);
127 spin_unlock(&ds->dd_lock);
131 * dock_add_hotplug_device - associate a hotplug handler with the dock station
132 * @ds: The dock station
133 * @dd: The dependent device struct
135 * Add the dependent device to the dock's hotplug device list
138 dock_add_hotplug_device(struct dock_station *ds,
139 struct dock_dependent_device *dd)
141 mutex_lock(&ds->hp_lock);
142 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
143 mutex_unlock(&ds->hp_lock);
147 * dock_del_hotplug_device - remove a hotplug handler from the dock station
148 * @ds: The dock station
149 * @dd: the dependent device struct
151 * Delete the dependent device from the dock's hotplug device list
154 dock_del_hotplug_device(struct dock_station *ds,
155 struct dock_dependent_device *dd)
157 mutex_lock(&ds->hp_lock);
158 list_del(&dd->hotplug_list);
159 mutex_unlock(&ds->hp_lock);
163 * find_dock_dependent_device - get a device dependent on this dock
164 * @ds: the dock station
165 * @handle: the acpi_handle of the device we want
167 * iterate over the dependent device list for this dock. If the
168 * dependent device matches the handle, return.
170 static struct dock_dependent_device *
171 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
173 struct dock_dependent_device *dd;
175 spin_lock(&ds->dd_lock);
176 list_for_each_entry(dd, &ds->dependent_devices, list) {
177 if (handle == dd->handle) {
178 spin_unlock(&ds->dd_lock);
182 spin_unlock(&ds->dd_lock);
186 /*****************************************************************************
188 *****************************************************************************/
190 * is_dock - see if a device is a dock station
191 * @handle: acpi handle of the device
193 * If an acpi object has a _DCK method, then it is by definition a dock
194 * station, so return true.
196 static int is_dock(acpi_handle handle)
201 status = acpi_get_handle(handle, "_DCK", &tmp);
202 if (ACPI_FAILURE(status))
207 static int is_ejectable(acpi_handle handle)
212 status = acpi_get_handle(handle, "_EJ0", &tmp);
213 if (ACPI_FAILURE(status))
218 static int is_ata(acpi_handle handle)
222 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
223 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
224 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
225 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
231 static int is_battery(acpi_handle handle)
233 struct acpi_device_info *info;
234 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
237 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &buffer)))
239 info = buffer.pointer;
240 if (!(info->valid & ACPI_VALID_HID))
243 ret = !strcmp("PNP0C0A", info->hardware_id.value);
245 kfree(buffer.pointer);
249 static int is_ejectable_bay(acpi_handle handle)
252 if (!is_ejectable(handle))
254 if (is_battery(handle) || is_ata(handle))
256 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
262 * is_dock_device - see if a device is on a dock station
263 * @handle: acpi handle of the device
265 * If this device is either the dock station itself,
266 * or is a device dependent on the dock station, then it
269 int is_dock_device(acpi_handle handle)
271 struct dock_station *dock_station;
273 if (!dock_station_count)
278 list_for_each_entry(dock_station, &dock_stations, sibiling) {
279 if (find_dock_dependent_device(dock_station, handle))
286 EXPORT_SYMBOL_GPL(is_dock_device);
289 * dock_present - see if the dock station is present.
290 * @ds: the dock station
292 * execute the _STA method. note that present does not
293 * imply that we are docked.
295 static int dock_present(struct dock_station *ds)
301 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
302 if (ACPI_SUCCESS(status) && sta)
311 * dock_create_acpi_device - add new devices to acpi
312 * @handle - handle of the device to add
314 * This function will create a new acpi_device for the given
315 * handle if one does not exist already. This should cause
316 * acpi to scan for drivers for the given devices, and call
317 * matching driver's add routine.
319 * Returns a pointer to the acpi_device corresponding to the handle.
321 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
323 struct acpi_device *device = NULL;
324 struct acpi_device *parent_device;
328 if (acpi_bus_get_device(handle, &device)) {
330 * no device created for this object,
331 * so we should create one.
333 acpi_get_parent(handle, &parent);
334 if (acpi_bus_get_device(parent, &parent_device))
335 parent_device = NULL;
337 ret = acpi_bus_add(&device, parent_device, handle,
338 ACPI_BUS_TYPE_DEVICE);
340 pr_debug("error adding bus, %x\n",
349 * dock_remove_acpi_device - remove the acpi_device struct from acpi
350 * @handle - the handle of the device to remove
352 * Tell acpi to remove the acpi_device. This should cause any loaded
353 * driver to have it's remove routine called.
355 static void dock_remove_acpi_device(acpi_handle handle)
357 struct acpi_device *device;
360 if (!acpi_bus_get_device(handle, &device)) {
361 ret = acpi_bus_trim(device, 1);
363 pr_debug("error removing bus, %x\n", -ret);
369 * hotplug_dock_devices - insert or remove devices on the dock station
370 * @ds: the dock station
371 * @event: either bus check or eject request
373 * Some devices on the dock station need to have drivers called
374 * to perform hotplug operations after a dock event has occurred.
375 * Traverse the list of dock devices that have registered a
376 * hotplug handler, and call the handler.
378 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
380 struct dock_dependent_device *dd;
382 mutex_lock(&ds->hp_lock);
385 * First call driver specific hotplug functions
387 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
388 if (dd->ops && dd->ops->handler)
389 dd->ops->handler(dd->handle, event, dd->context);
393 * Now make sure that an acpi_device is created for each
394 * dependent device, or removed if this is an eject request.
395 * This will cause acpi_drivers to be stopped/started if they
398 list_for_each_entry(dd, &ds->dependent_devices, list) {
399 if (event == ACPI_NOTIFY_EJECT_REQUEST)
400 dock_remove_acpi_device(dd->handle);
402 dock_create_acpi_device(dd->handle);
404 mutex_unlock(&ds->hp_lock);
407 static void dock_event(struct dock_station *ds, u32 event, int num)
409 struct device *dev = &ds->dock_device->dev;
410 char event_string[13];
411 char *envp[] = { event_string, NULL };
412 struct dock_dependent_device *dd;
414 if (num == UNDOCK_EVENT)
415 sprintf(event_string, "EVENT=undock");
417 sprintf(event_string, "EVENT=dock");
420 * Indicate that the status of the dock station has
423 if (num == DOCK_EVENT)
424 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
426 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
427 if (dd->ops && dd->ops->uevent)
428 dd->ops->uevent(dd->handle, event, dd->context);
429 if (num != DOCK_EVENT)
430 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
434 * eject_dock - respond to a dock eject request
435 * @ds: the dock station
437 * This is called after _DCK is called, to execute the dock station's
440 static void eject_dock(struct dock_station *ds)
442 struct acpi_object_list arg_list;
443 union acpi_object arg;
447 /* all dock devices should have _EJ0, but check anyway */
448 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
449 if (ACPI_FAILURE(status)) {
450 pr_debug("No _EJ0 support for dock device\n");
455 arg_list.pointer = &arg;
456 arg.type = ACPI_TYPE_INTEGER;
457 arg.integer.value = 1;
459 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
461 pr_debug("Failed to evaluate _EJ0!\n");
465 * handle_dock - handle a dock event
466 * @ds: the dock station
467 * @dock: to dock, or undock - that is the question
469 * Execute the _DCK method in response to an acpi event
471 static void handle_dock(struct dock_station *ds, int dock)
474 struct acpi_object_list arg_list;
475 union acpi_object arg;
476 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
477 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
479 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
481 printk(KERN_INFO PREFIX "%s - %s\n",
482 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
484 /* _DCK method has one argument */
486 arg_list.pointer = &arg;
487 arg.type = ACPI_TYPE_INTEGER;
488 arg.integer.value = dock;
489 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
490 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
491 printk(KERN_ERR PREFIX "%s - failed to execute _DCK\n",
492 (char *)name_buffer.pointer);
493 kfree(buffer.pointer);
494 kfree(name_buffer.pointer);
497 static inline void dock(struct dock_station *ds)
502 static inline void undock(struct dock_station *ds)
507 static inline void begin_dock(struct dock_station *ds)
509 ds->flags |= DOCK_DOCKING;
512 static inline void complete_dock(struct dock_station *ds)
514 ds->flags &= ~(DOCK_DOCKING);
515 ds->last_dock_time = jiffies;
518 static inline void begin_undock(struct dock_station *ds)
520 ds->flags |= DOCK_UNDOCKING;
523 static inline void complete_undock(struct dock_station *ds)
525 ds->flags &= ~(DOCK_UNDOCKING);
528 static void dock_lock(struct dock_station *ds, int lock)
530 struct acpi_object_list arg_list;
531 union acpi_object arg;
535 arg_list.pointer = &arg;
536 arg.type = ACPI_TYPE_INTEGER;
537 arg.integer.value = !!lock;
538 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
539 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
541 printk(KERN_WARNING PREFIX "Locking device failed\n");
543 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
548 * dock_in_progress - see if we are in the middle of handling a dock event
549 * @ds: the dock station
551 * Sometimes while docking, false dock events can be sent to the driver
552 * because good connections aren't made or some other reason. Ignore these
553 * if we are in the middle of doing something.
555 static int dock_in_progress(struct dock_station *ds)
557 if ((ds->flags & DOCK_DOCKING) ||
558 time_before(jiffies, (ds->last_dock_time + HZ)))
564 * register_dock_notifier - add yourself to the dock notifier list
565 * @nb: the callers notifier block
567 * If a driver wishes to be notified about dock events, they can
568 * use this function to put a notifier block on the dock notifier list.
569 * this notifier call chain will be called after a dock event, but
570 * before hotplugging any new devices.
572 int register_dock_notifier(struct notifier_block *nb)
574 if (!dock_station_count)
577 return atomic_notifier_chain_register(&dock_notifier_list, nb);
580 EXPORT_SYMBOL_GPL(register_dock_notifier);
583 * unregister_dock_notifier - remove yourself from the dock notifier list
584 * @nb: the callers notifier block
586 void unregister_dock_notifier(struct notifier_block *nb)
588 if (!dock_station_count)
591 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
594 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
597 * register_hotplug_dock_device - register a hotplug function
598 * @handle: the handle of the device
599 * @ops: handlers to call after docking
600 * @context: device specific data
602 * If a driver would like to perform a hotplug operation after a dock
603 * event, they can register an acpi_notifiy_handler to be called by
604 * the dock driver after _DCK is executed.
607 register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
610 struct dock_dependent_device *dd;
611 struct dock_station *dock_station;
614 if (!dock_station_count)
618 * make sure this handle is for a device dependent on the dock,
619 * this would include the dock station itself
621 list_for_each_entry(dock_station, &dock_stations, sibiling) {
623 * An ATA bay can be in a dock and itself can be ejected
624 * seperately, so there are two 'dock stations' which need the
627 dd = find_dock_dependent_device(dock_station, handle);
630 dd->context = context;
631 dock_add_hotplug_device(dock_station, dd);
639 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
642 * unregister_hotplug_dock_device - remove yourself from the hotplug list
643 * @handle: the acpi handle of the device
645 void unregister_hotplug_dock_device(acpi_handle handle)
647 struct dock_dependent_device *dd;
648 struct dock_station *dock_station;
650 if (!dock_station_count)
653 list_for_each_entry(dock_station, &dock_stations, sibiling) {
654 dd = find_dock_dependent_device(dock_station, handle);
656 dock_del_hotplug_device(dock_station, dd);
660 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
663 * handle_eject_request - handle an undock request checking for error conditions
665 * Check to make sure the dock device is still present, then undock and
666 * hotremove all the devices that may need removing.
668 static int handle_eject_request(struct dock_station *ds, u32 event)
670 if (dock_in_progress(ds))
674 * here we need to generate the undock
675 * event prior to actually doing the undock
676 * so that the device struct still exists.
677 * Also, even send the dock event if the
678 * device is not present anymore
680 dock_event(ds, event, UNDOCK_EVENT);
682 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
686 if (dock_present(ds)) {
687 printk(KERN_ERR PREFIX "Unable to undock!\n");
695 * dock_notify - act upon an acpi dock notification
696 * @handle: the dock station handle
697 * @event: the acpi event
698 * @data: our driver data struct
700 * If we are notified to dock, then check to see if the dock is
701 * present and then dock. Notify all drivers of the dock event,
702 * and then hotplug and devices that may need hotplugging.
704 static void dock_notify(acpi_handle handle, u32 event, void *data)
706 struct dock_station *ds = data;
707 struct acpi_device *tmp;
708 int surprise_removal = 0;
711 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
712 * is sent and _DCK is present, it is assumed to mean an undock
715 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
716 event = ACPI_NOTIFY_EJECT_REQUEST;
719 * dock station: BUS_CHECK - docked or surprise removal
720 * DEVICE_CHECK - undocked
721 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
723 * To simplify event handling, dock dependent device handler always
724 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
725 * ACPI_NOTIFY_EJECT_REQUEST for removal
728 case ACPI_NOTIFY_BUS_CHECK:
729 case ACPI_NOTIFY_DEVICE_CHECK:
730 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
734 if (!dock_present(ds)) {
735 printk(KERN_ERR PREFIX "Unable to dock!\n");
739 atomic_notifier_call_chain(&dock_notifier_list,
741 hotplug_dock_devices(ds, event);
743 dock_event(ds, event, DOCK_EVENT);
747 if (dock_present(ds) || dock_in_progress(ds))
749 /* This is a surprise removal */
750 surprise_removal = 1;
751 event = ACPI_NOTIFY_EJECT_REQUEST;
753 case ACPI_NOTIFY_EJECT_REQUEST:
755 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
757 handle_eject_request(ds, event);
759 dock_event(ds, event, UNDOCK_EVENT);
762 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
769 struct dock_station *ds;
772 static void acpi_dock_deferred_cb(void *context)
774 struct dock_data *data = (struct dock_data *)context;
776 dock_notify(data->handle, data->event, data->ds);
780 static int acpi_dock_notifier_call(struct notifier_block *this,
781 unsigned long event, void *data)
783 struct dock_station *dock_station;
784 acpi_handle handle = (acpi_handle)data;
786 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
787 && event != ACPI_NOTIFY_EJECT_REQUEST)
789 list_for_each_entry(dock_station, &dock_stations, sibiling) {
790 if (dock_station->handle == handle) {
791 struct dock_data *dock_data;
793 dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
796 dock_data->handle = handle;
797 dock_data->event = event;
798 dock_data->ds = dock_station;
799 acpi_os_hotplug_execute(acpi_dock_deferred_cb,
807 static struct notifier_block dock_acpi_notifier = {
808 .notifier_call = acpi_dock_notifier_call,
812 * find_dock_devices - find devices on the dock station
813 * @handle: the handle of the device we are examining
815 * @context: the dock station private data
818 * This function is called by acpi_walk_namespace. It will
819 * check to see if an object has an _EJD method. If it does, then it
820 * will see if it is dependent on the dock station.
823 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
826 acpi_handle tmp, parent;
827 struct dock_station *ds = context;
828 struct dock_dependent_device *dd;
830 status = acpi_bus_get_ejd(handle, &tmp);
831 if (ACPI_FAILURE(status)) {
832 /* try the parent device as well */
833 status = acpi_get_parent(handle, &parent);
834 if (ACPI_FAILURE(status))
836 /* see if parent is dependent on dock */
837 status = acpi_bus_get_ejd(parent, &tmp);
838 if (ACPI_FAILURE(status))
842 if (tmp == ds->handle) {
843 dd = alloc_dock_dependent_device(handle);
845 add_dock_dependent_device(ds, dd);
852 * show_docked - read method for "docked" file in sysfs
854 static ssize_t show_docked(struct device *dev,
855 struct device_attribute *attr, char *buf)
857 struct dock_station *dock_station = *((struct dock_station **)
859 return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
862 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
865 * show_flags - read method for flags file in sysfs
867 static ssize_t show_flags(struct device *dev,
868 struct device_attribute *attr, char *buf)
870 struct dock_station *dock_station = *((struct dock_station **)
872 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
875 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
878 * write_undock - write method for "undock" file in sysfs
880 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
881 const char *buf, size_t count)
884 struct dock_station *dock_station = *((struct dock_station **)
890 begin_undock(dock_station);
891 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
892 return ret ? ret: count;
894 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
897 * show_dock_uid - read method for "uid" file in sysfs
899 static ssize_t show_dock_uid(struct device *dev,
900 struct device_attribute *attr, char *buf)
903 struct dock_station *dock_station = *((struct dock_station **)
905 acpi_status status = acpi_evaluate_integer(dock_station->handle,
906 "_UID", NULL, &lbuf);
907 if (ACPI_FAILURE(status))
910 return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf);
912 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
914 static ssize_t show_dock_type(struct device *dev,
915 struct device_attribute *attr, char *buf)
917 struct dock_station *dock_station = *((struct dock_station **)
921 if (dock_station->flags & DOCK_IS_DOCK)
922 type = "dock_station";
923 else if (dock_station->flags & DOCK_IS_ATA)
925 else if (dock_station->flags & DOCK_IS_BAT)
926 type = "battery_bay";
930 return snprintf(buf, PAGE_SIZE, "%s\n", type);
932 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
935 * dock_add - add a new dock station
936 * @handle: the dock station handle
938 * allocated and initialize a new dock station device. Find all devices
939 * that are on the dock station, and register for dock event notifications.
941 static int dock_add(acpi_handle handle)
944 struct dock_dependent_device *dd;
945 struct dock_station *dock_station;
946 struct platform_device *dock_device;
948 /* allocate & initialize the dock_station private data */
949 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
952 dock_station->handle = handle;
953 dock_station->last_dock_time = jiffies - HZ;
954 INIT_LIST_HEAD(&dock_station->dependent_devices);
955 INIT_LIST_HEAD(&dock_station->hotplug_devices);
956 INIT_LIST_HEAD(&dock_station->sibiling);
957 spin_lock_init(&dock_station->dd_lock);
958 mutex_init(&dock_station->hp_lock);
959 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
961 /* initialize platform device stuff */
962 dock_station->dock_device =
963 platform_device_register_simple(dock_device_name,
964 dock_station_count, NULL, 0);
965 dock_device = dock_station->dock_device;
966 if (IS_ERR(dock_device)) {
969 return PTR_ERR(dock_device);
971 platform_device_add_data(dock_device, &dock_station,
972 sizeof(struct dock_station *));
974 /* we want the dock device to send uevents */
975 dock_device->dev.uevent_suppress = 0;
978 dock_station->flags |= DOCK_IS_DOCK;
980 dock_station->flags |= DOCK_IS_ATA;
981 if (is_battery(handle))
982 dock_station->flags |= DOCK_IS_BAT;
984 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
986 printk("Error %d adding sysfs file\n", ret);
987 platform_device_unregister(dock_device);
992 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
994 printk("Error %d adding sysfs file\n", ret);
995 device_remove_file(&dock_device->dev, &dev_attr_docked);
996 platform_device_unregister(dock_device);
1001 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
1003 printk("Error %d adding sysfs file\n", ret);
1004 device_remove_file(&dock_device->dev, &dev_attr_docked);
1005 device_remove_file(&dock_device->dev, &dev_attr_undock);
1006 platform_device_unregister(dock_device);
1007 kfree(dock_station);
1008 dock_station = NULL;
1011 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
1013 printk("Error %d adding sysfs file\n", ret);
1014 device_remove_file(&dock_device->dev, &dev_attr_docked);
1015 device_remove_file(&dock_device->dev, &dev_attr_undock);
1016 device_remove_file(&dock_device->dev, &dev_attr_uid);
1017 platform_device_unregister(dock_device);
1018 kfree(dock_station);
1019 dock_station = NULL;
1022 ret = device_create_file(&dock_device->dev, &dev_attr_type);
1024 printk(KERN_ERR"Error %d adding sysfs file\n", ret);
1026 /* Find dependent devices */
1027 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1028 ACPI_UINT32_MAX, find_dock_devices, dock_station,
1031 /* add the dock station as a device dependent on itself */
1032 dd = alloc_dock_dependent_device(handle);
1034 kfree(dock_station);
1035 dock_station = NULL;
1037 goto dock_add_err_unregister;
1039 add_dock_dependent_device(dock_station, dd);
1041 dock_station_count++;
1042 list_add(&dock_station->sibiling, &dock_stations);
1045 dock_add_err_unregister:
1046 device_remove_file(&dock_device->dev, &dev_attr_type);
1047 device_remove_file(&dock_device->dev, &dev_attr_docked);
1048 device_remove_file(&dock_device->dev, &dev_attr_undock);
1049 device_remove_file(&dock_device->dev, &dev_attr_uid);
1050 device_remove_file(&dock_device->dev, &dev_attr_flags);
1051 platform_device_unregister(dock_device);
1052 kfree(dock_station);
1053 dock_station = NULL;
1058 * dock_remove - free up resources related to the dock station
1060 static int dock_remove(struct dock_station *dock_station)
1062 struct dock_dependent_device *dd, *tmp;
1063 struct platform_device *dock_device = dock_station->dock_device;
1065 if (!dock_station_count)
1068 /* remove dependent devices */
1069 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1074 device_remove_file(&dock_device->dev, &dev_attr_type);
1075 device_remove_file(&dock_device->dev, &dev_attr_docked);
1076 device_remove_file(&dock_device->dev, &dev_attr_undock);
1077 device_remove_file(&dock_device->dev, &dev_attr_uid);
1078 device_remove_file(&dock_device->dev, &dev_attr_flags);
1079 platform_device_unregister(dock_device);
1081 /* free dock station memory */
1082 kfree(dock_station);
1083 dock_station = NULL;
1088 * find_dock - look for a dock station
1089 * @handle: acpi handle of a device
1091 * @context: counter of dock stations found
1094 * This is called by acpi_walk_namespace to look for dock stations.
1097 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1099 acpi_status status = AE_OK;
1101 if (is_dock(handle)) {
1102 if (dock_add(handle) >= 0) {
1103 status = AE_CTRL_TERMINATE;
1110 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1112 /* If bay is a dock, it's already handled */
1113 if (is_ejectable_bay(handle) && !is_dock(handle))
1118 static int __init dock_init(void)
1123 /* look for a dock station */
1124 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1125 ACPI_UINT32_MAX, find_dock, NULL, NULL);
1128 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1129 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1130 if (!dock_station_count) {
1131 printk(KERN_INFO PREFIX "No dock devices found.\n");
1135 register_acpi_bus_notifier(&dock_acpi_notifier);
1136 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1137 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
1141 static void __exit dock_exit(void)
1143 struct dock_station *dock_station;
1145 unregister_acpi_bus_notifier(&dock_acpi_notifier);
1146 list_for_each_entry(dock_station, &dock_stations, sibiling)
1147 dock_remove(dock_station);
1151 * Must be called before drivers of devices in dock, otherwise we can't know
1152 * which devices are in a dock
1154 subsys_initcall(dock_init);
1155 module_exit(dock_exit);