mm-only debug patch...
[mmotm.git] / drivers / acpi / dock.c
blob1598fb4b5e3b703d6644d6c7c797fc7917211602
1 /*
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 "
50 " before undocking");
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[] = {
56 {"LNXDOCK", 0},
57 {"", 0},
59 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
61 struct dock_station {
62 acpi_handle handle;
63 unsigned long last_dock_time;
64 u32 flags;
65 spinlock_t dd_lock;
66 struct mutex hp_lock;
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;
79 acpi_handle handle;
80 struct acpi_dock_ops *ops;
81 void *context;
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
89 #define DOCK_EVENT 3
90 #define UNDOCK_EVENT 2
92 /*****************************************************************************
93 * Dock Dependent device functions *
94 *****************************************************************************/
95 /**
96 * add_dock_dependent_device - associate a device with the dock station
97 * @handle: handle of the dependent device
98 * @ds: The dock station
100 * Add the dependent device to the dock's dependent device list.
102 static int
103 add_dock_dependent_device(acpi_handle handle, struct dock_station *ds)
105 struct dock_dependent_device *dd;
107 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
108 if (!dd)
109 return -ENOMEM;
111 dd->handle = handle;
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);
119 return 0;
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
129 static void
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
145 static void
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);
171 return dd;
174 spin_unlock(&ds->dd_lock);
175 return NULL;
178 /*****************************************************************************
179 * Dock functions *
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)
190 acpi_status status;
191 acpi_handle tmp;
193 status = acpi_get_handle(handle, "_DCK", &tmp);
194 if (ACPI_FAILURE(status))
195 return 0;
196 return 1;
199 static int is_ejectable(acpi_handle handle)
201 acpi_status status;
202 acpi_handle tmp;
204 status = acpi_get_handle(handle, "_EJ0", &tmp);
205 if (ACPI_FAILURE(status))
206 return 0;
207 return 1;
210 static int is_ata(acpi_handle handle)
212 acpi_handle tmp;
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))))
218 return 1;
220 return 0;
223 static int is_battery(acpi_handle handle)
225 struct acpi_device_info *info;
226 int ret = 1;
228 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
229 return 0;
230 if (!(info->valid & ACPI_VALID_HID))
231 ret = 0;
232 else
233 ret = !strcmp("PNP0C0A", info->hardware_id.string);
235 kfree(info);
236 return ret;
239 static int is_ejectable_bay(acpi_handle handle)
241 acpi_handle phandle;
243 if (!is_ejectable(handle))
244 return 0;
245 if (is_battery(handle) || is_ata(handle))
246 return 1;
247 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
248 return 1;
249 return 0;
253 * is_dock_device - see if a device is on a dock station
254 * @handle: acpi handle of the device
256 * If this device is either the dock station itself,
257 * or is a device dependent on the dock station, then it
258 * is a dock device
260 int is_dock_device(acpi_handle handle)
262 struct dock_station *dock_station;
264 if (!dock_station_count)
265 return 0;
267 if (is_dock(handle))
268 return 1;
270 list_for_each_entry(dock_station, &dock_stations, sibling)
271 if (find_dock_dependent_device(dock_station, handle))
272 return 1;
274 return 0;
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;
288 acpi_status status;
290 if (ds) {
291 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
292 if (ACPI_SUCCESS(status) && sta)
293 return 1;
295 return 0;
299 * dock_create_acpi_device - add new devices to acpi
300 * @handle - handle of the device to add
302 * This function will create a new acpi_device for the given
303 * handle if one does not exist already. This should cause
304 * acpi to scan for drivers for the given devices, and call
305 * matching driver's add routine.
307 * Returns a pointer to the acpi_device corresponding to the handle.
309 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
311 struct acpi_device *device;
312 struct acpi_device *parent_device;
313 acpi_handle parent;
314 int ret;
316 if (acpi_bus_get_device(handle, &device)) {
318 * no device created for this object,
319 * so we should create one.
321 acpi_get_parent(handle, &parent);
322 if (acpi_bus_get_device(parent, &parent_device))
323 parent_device = NULL;
325 ret = acpi_bus_add(&device, parent_device, handle,
326 ACPI_BUS_TYPE_DEVICE);
327 if (ret) {
328 pr_debug("error adding bus, %x\n", -ret);
329 return NULL;
332 return device;
336 * dock_remove_acpi_device - remove the acpi_device struct from acpi
337 * @handle - the handle of the device to remove
339 * Tell acpi to remove the acpi_device. This should cause any loaded
340 * driver to have it's remove routine called.
342 static void dock_remove_acpi_device(acpi_handle handle)
344 struct acpi_device *device;
345 int ret;
347 if (!acpi_bus_get_device(handle, &device)) {
348 ret = acpi_bus_trim(device, 1);
349 if (ret)
350 pr_debug("error removing bus, %x\n", -ret);
355 * hotplug_dock_devices - insert or remove devices on the dock station
356 * @ds: the dock station
357 * @event: either bus check or eject request
359 * Some devices on the dock station need to have drivers called
360 * to perform hotplug operations after a dock event has occurred.
361 * Traverse the list of dock devices that have registered a
362 * hotplug handler, and call the handler.
364 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
366 struct dock_dependent_device *dd;
368 mutex_lock(&ds->hp_lock);
371 * First call driver specific hotplug functions
373 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
374 if (dd->ops && dd->ops->handler)
375 dd->ops->handler(dd->handle, event, dd->context);
378 * Now make sure that an acpi_device is created for each
379 * dependent device, or removed if this is an eject request.
380 * This will cause acpi_drivers to be stopped/started if they
381 * exist
383 list_for_each_entry(dd, &ds->dependent_devices, list) {
384 if (event == ACPI_NOTIFY_EJECT_REQUEST)
385 dock_remove_acpi_device(dd->handle);
386 else
387 dock_create_acpi_device(dd->handle);
389 mutex_unlock(&ds->hp_lock);
392 static void dock_event(struct dock_station *ds, u32 event, int num)
394 struct device *dev = &ds->dock_device->dev;
395 char event_string[13];
396 char *envp[] = { event_string, NULL };
397 struct dock_dependent_device *dd;
399 if (num == UNDOCK_EVENT)
400 sprintf(event_string, "EVENT=undock");
401 else
402 sprintf(event_string, "EVENT=dock");
405 * Indicate that the status of the dock station has
406 * changed.
408 if (num == DOCK_EVENT)
409 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
411 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
412 if (dd->ops && dd->ops->uevent)
413 dd->ops->uevent(dd->handle, event, dd->context);
415 if (num != DOCK_EVENT)
416 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
420 * eject_dock - respond to a dock eject request
421 * @ds: the dock station
423 * This is called after _DCK is called, to execute the dock station's
424 * _EJ0 method.
426 static void eject_dock(struct dock_station *ds)
428 struct acpi_object_list arg_list;
429 union acpi_object arg;
430 acpi_status status;
431 acpi_handle tmp;
433 /* all dock devices should have _EJ0, but check anyway */
434 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
435 if (ACPI_FAILURE(status)) {
436 pr_debug("No _EJ0 support for dock device\n");
437 return;
440 arg_list.count = 1;
441 arg_list.pointer = &arg;
442 arg.type = ACPI_TYPE_INTEGER;
443 arg.integer.value = 1;
445 status = acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL);
446 if (ACPI_FAILURE(status))
447 pr_debug("Failed to evaluate _EJ0!\n");
451 * handle_dock - handle a dock event
452 * @ds: the dock station
453 * @dock: to dock, or undock - that is the question
455 * Execute the _DCK method in response to an acpi event
457 static void handle_dock(struct dock_station *ds, int dock)
459 acpi_status status;
460 struct acpi_object_list arg_list;
461 union acpi_object arg;
462 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
463 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
465 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
467 printk(KERN_INFO PREFIX "%s - %s\n",
468 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
470 /* _DCK method has one argument */
471 arg_list.count = 1;
472 arg_list.pointer = &arg;
473 arg.type = ACPI_TYPE_INTEGER;
474 arg.integer.value = dock;
475 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
476 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
477 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
478 " _DCK\n", (char *)name_buffer.pointer));
480 kfree(buffer.pointer);
481 kfree(name_buffer.pointer);
484 static inline void dock(struct dock_station *ds)
486 handle_dock(ds, 1);
489 static inline void undock(struct dock_station *ds)
491 handle_dock(ds, 0);
494 static inline void begin_dock(struct dock_station *ds)
496 ds->flags |= DOCK_DOCKING;
499 static inline void complete_dock(struct dock_station *ds)
501 ds->flags &= ~(DOCK_DOCKING);
502 ds->last_dock_time = jiffies;
505 static inline void begin_undock(struct dock_station *ds)
507 ds->flags |= DOCK_UNDOCKING;
510 static inline void complete_undock(struct dock_station *ds)
512 ds->flags &= ~(DOCK_UNDOCKING);
515 static void dock_lock(struct dock_station *ds, int lock)
517 struct acpi_object_list arg_list;
518 union acpi_object arg;
519 acpi_status status;
521 arg_list.count = 1;
522 arg_list.pointer = &arg;
523 arg.type = ACPI_TYPE_INTEGER;
524 arg.integer.value = !!lock;
525 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
526 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
527 if (lock)
528 printk(KERN_WARNING PREFIX "Locking device failed\n");
529 else
530 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
535 * dock_in_progress - see if we are in the middle of handling a dock event
536 * @ds: the dock station
538 * Sometimes while docking, false dock events can be sent to the driver
539 * because good connections aren't made or some other reason. Ignore these
540 * if we are in the middle of doing something.
542 static int dock_in_progress(struct dock_station *ds)
544 if ((ds->flags & DOCK_DOCKING) ||
545 time_before(jiffies, (ds->last_dock_time + HZ)))
546 return 1;
547 return 0;
551 * register_dock_notifier - add yourself to the dock notifier list
552 * @nb: the callers notifier block
554 * If a driver wishes to be notified about dock events, they can
555 * use this function to put a notifier block on the dock notifier list.
556 * this notifier call chain will be called after a dock event, but
557 * before hotplugging any new devices.
559 int register_dock_notifier(struct notifier_block *nb)
561 if (!dock_station_count)
562 return -ENODEV;
564 return atomic_notifier_chain_register(&dock_notifier_list, nb);
566 EXPORT_SYMBOL_GPL(register_dock_notifier);
569 * unregister_dock_notifier - remove yourself from the dock notifier list
570 * @nb: the callers notifier block
572 void unregister_dock_notifier(struct notifier_block *nb)
574 if (!dock_station_count)
575 return;
577 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
579 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
582 * register_hotplug_dock_device - register a hotplug function
583 * @handle: the handle of the device
584 * @ops: handlers to call after docking
585 * @context: device specific data
587 * If a driver would like to perform a hotplug operation after a dock
588 * event, they can register an acpi_notifiy_handler to be called by
589 * the dock driver after _DCK is executed.
592 register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
593 void *context)
595 struct dock_dependent_device *dd;
596 struct dock_station *dock_station;
597 int ret = -EINVAL;
599 if (!dock_station_count)
600 return -ENODEV;
603 * make sure this handle is for a device dependent on the dock,
604 * this would include the dock station itself
606 list_for_each_entry(dock_station, &dock_stations, sibling) {
608 * An ATA bay can be in a dock and itself can be ejected
609 * seperately, so there are two 'dock stations' which need the
610 * ops
612 dd = find_dock_dependent_device(dock_station, handle);
613 if (dd) {
614 dd->ops = ops;
615 dd->context = context;
616 dock_add_hotplug_device(dock_station, dd);
617 ret = 0;
621 return ret;
623 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
626 * unregister_hotplug_dock_device - remove yourself from the hotplug list
627 * @handle: the acpi handle of the device
629 void unregister_hotplug_dock_device(acpi_handle handle)
631 struct dock_dependent_device *dd;
632 struct dock_station *dock_station;
634 if (!dock_station_count)
635 return;
637 list_for_each_entry(dock_station, &dock_stations, sibling) {
638 dd = find_dock_dependent_device(dock_station, handle);
639 if (dd)
640 dock_del_hotplug_device(dock_station, dd);
643 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
646 * handle_eject_request - handle an undock request checking for error conditions
648 * Check to make sure the dock device is still present, then undock and
649 * hotremove all the devices that may need removing.
651 static int handle_eject_request(struct dock_station *ds, u32 event)
653 if (dock_in_progress(ds))
654 return -EBUSY;
657 * here we need to generate the undock
658 * event prior to actually doing the undock
659 * so that the device struct still exists.
660 * Also, even send the dock event if the
661 * device is not present anymore
663 dock_event(ds, event, UNDOCK_EVENT);
665 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
666 undock(ds);
667 dock_lock(ds, 0);
668 eject_dock(ds);
669 if (dock_present(ds)) {
670 printk(KERN_ERR PREFIX "Unable to undock!\n");
671 return -EBUSY;
673 complete_undock(ds);
674 return 0;
678 * dock_notify - act upon an acpi dock notification
679 * @handle: the dock station handle
680 * @event: the acpi event
681 * @data: our driver data struct
683 * If we are notified to dock, then check to see if the dock is
684 * present and then dock. Notify all drivers of the dock event,
685 * and then hotplug and devices that may need hotplugging.
687 static void dock_notify(acpi_handle handle, u32 event, void *data)
689 struct dock_station *ds = data;
690 struct acpi_device *tmp;
691 int surprise_removal = 0;
694 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
695 * is sent and _DCK is present, it is assumed to mean an undock
696 * request.
698 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
699 event = ACPI_NOTIFY_EJECT_REQUEST;
702 * dock station: BUS_CHECK - docked or surprise removal
703 * DEVICE_CHECK - undocked
704 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
706 * To simplify event handling, dock dependent device handler always
707 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
708 * ACPI_NOTIFY_EJECT_REQUEST for removal
710 switch (event) {
711 case ACPI_NOTIFY_BUS_CHECK:
712 case ACPI_NOTIFY_DEVICE_CHECK:
713 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
714 &tmp)) {
715 begin_dock(ds);
716 dock(ds);
717 if (!dock_present(ds)) {
718 printk(KERN_ERR PREFIX "Unable to dock!\n");
719 complete_dock(ds);
720 break;
722 atomic_notifier_call_chain(&dock_notifier_list,
723 event, NULL);
724 hotplug_dock_devices(ds, event);
725 complete_dock(ds);
726 dock_event(ds, event, DOCK_EVENT);
727 dock_lock(ds, 1);
728 break;
730 if (dock_present(ds) || dock_in_progress(ds))
731 break;
732 /* This is a surprise removal */
733 surprise_removal = 1;
734 event = ACPI_NOTIFY_EJECT_REQUEST;
735 /* Fall back */
736 case ACPI_NOTIFY_EJECT_REQUEST:
737 begin_undock(ds);
738 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
739 || surprise_removal)
740 handle_eject_request(ds, event);
741 else
742 dock_event(ds, event, UNDOCK_EVENT);
743 break;
744 default:
745 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
749 struct dock_data {
750 acpi_handle handle;
751 unsigned long event;
752 struct dock_station *ds;
755 static void acpi_dock_deferred_cb(void *context)
757 struct dock_data *data = context;
759 dock_notify(data->handle, data->event, data->ds);
760 kfree(data);
763 static int acpi_dock_notifier_call(struct notifier_block *this,
764 unsigned long event, void *data)
766 struct dock_station *dock_station;
767 acpi_handle handle = data;
769 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
770 && event != ACPI_NOTIFY_EJECT_REQUEST)
771 return 0;
772 list_for_each_entry(dock_station, &dock_stations, sibling) {
773 if (dock_station->handle == handle) {
774 struct dock_data *dd;
776 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
777 if (!dd)
778 return 0;
779 dd->handle = handle;
780 dd->event = event;
781 dd->ds = dock_station;
782 acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
783 return 0 ;
786 return 0;
789 static struct notifier_block dock_acpi_notifier = {
790 .notifier_call = acpi_dock_notifier_call,
794 * find_dock_devices - find devices on the dock station
795 * @handle: the handle of the device we are examining
796 * @lvl: unused
797 * @context: the dock station private data
798 * @rv: unused
800 * This function is called by acpi_walk_namespace. It will
801 * check to see if an object has an _EJD method. If it does, then it
802 * will see if it is dependent on the dock station.
804 static acpi_status
805 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
807 acpi_status status;
808 acpi_handle tmp, parent;
809 struct dock_station *ds = context;
811 status = acpi_bus_get_ejd(handle, &tmp);
812 if (ACPI_FAILURE(status)) {
813 /* try the parent device as well */
814 status = acpi_get_parent(handle, &parent);
815 if (ACPI_FAILURE(status))
816 goto fdd_out;
817 /* see if parent is dependent on dock */
818 status = acpi_bus_get_ejd(parent, &tmp);
819 if (ACPI_FAILURE(status))
820 goto fdd_out;
823 if (tmp == ds->handle)
824 add_dock_dependent_device(ds, handle);
826 fdd_out:
827 return AE_OK;
831 * show_docked - read method for "docked" file in sysfs
833 static ssize_t show_docked(struct device *dev,
834 struct device_attribute *attr, char *buf)
836 struct acpi_device *tmp;
838 struct dock_station *dock_station = dev->platform_data;
840 if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
841 return snprintf(buf, PAGE_SIZE, "1\n");
842 return snprintf(buf, PAGE_SIZE, "0\n");
844 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
847 * show_flags - read method for flags file in sysfs
849 static ssize_t show_flags(struct device *dev,
850 struct device_attribute *attr, char *buf)
852 struct dock_station *dock_station = dev->platform_data;
853 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
856 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
859 * write_undock - write method for "undock" file in sysfs
861 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
862 const char *buf, size_t count)
864 int ret;
865 struct dock_station *dock_station = dev->platform_data;
867 if (!count)
868 return -EINVAL;
870 begin_undock(dock_station);
871 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
872 return ret ? ret: count;
874 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
877 * show_dock_uid - read method for "uid" file in sysfs
879 static ssize_t show_dock_uid(struct device *dev,
880 struct device_attribute *attr, char *buf)
882 unsigned long long lbuf;
883 struct dock_station *dock_station = dev->platform_data;
884 acpi_status status = acpi_evaluate_integer(dock_station->handle,
885 "_UID", NULL, &lbuf);
886 if (ACPI_FAILURE(status))
887 return 0;
889 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
891 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
893 static ssize_t show_dock_type(struct device *dev,
894 struct device_attribute *attr, char *buf)
896 struct dock_station *dock_station = dev->platform_data;
897 char *type;
899 if (dock_station->flags & DOCK_IS_DOCK)
900 type = "dock_station";
901 else if (dock_station->flags & DOCK_IS_ATA)
902 type = "ata_bay";
903 else if (dock_station->flags & DOCK_IS_BAT)
904 type = "battery_bay";
905 else
906 type = "unknown";
908 return snprintf(buf, PAGE_SIZE, "%s\n", type);
910 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
913 * dock_add - add a new dock station
914 * @handle: the dock station handle
916 * allocated and initialize a new dock station device. Find all devices
917 * that are on the dock station, and register for dock event notifications.
919 static int dock_add(acpi_handle handle)
921 int ret;
922 struct dock_station *ds;
923 struct platform_device *dock_device;
925 /* allocate & initialize the dock_station private data */
926 ds = kzalloc(sizeof(struct dock_station), GFP_KERNEL);
927 if (!ds)
928 return -ENOMEM;
930 ds->handle = handle;
931 ds->last_dock_time = jiffies - HZ;
933 INIT_LIST_HEAD(&ds->dependent_devices);
934 INIT_LIST_HEAD(&ds->hotplug_devices);
935 INIT_LIST_HEAD(&ds->sibling);
936 spin_lock_init(&ds->dd_lock);
937 mutex_init(&ds->hp_lock);
938 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
940 /* initialize platform device stuff */
941 ds->dock_device = platform_device_register_simple(dock_device_name,
942 dock_station_count, NULL, 0);
943 dock_device = ds->dock_device;
945 ret = IS_ERR(dock_device) ? PTR_ERR(dock_device) : 0;
946 if (ret)
947 goto err_free;
949 platform_device_add_data(dock_device, ds, sizeof(struct dock_station));
951 /* we want the dock device to send uevents */
952 dev_set_uevent_suppress(&dock_device->dev, 0);
954 if (is_dock(handle))
955 ds->flags |= DOCK_IS_DOCK;
956 if (is_ata(handle))
957 ds->flags |= DOCK_IS_ATA;
958 if (is_battery(handle))
959 ds->flags |= DOCK_IS_BAT;
961 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
962 if (ret)
963 goto err_unregister;
965 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
966 if (ret)
967 goto err_unregister1;
969 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
970 if (ret)
971 goto err_unregister2;
973 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
974 if (ret)
975 goto err_unregister3;
977 ret = device_create_file(&dock_device->dev, &dev_attr_type);
978 if (ret)
979 goto err_unregister4;
981 /* Find dependent devices */
982 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
983 ACPI_UINT32_MAX, find_dock_devices, ds, NULL);
985 /* add the dock station as a device dependent on itself */
986 ret = add_dock_dependent_device(ds, handle);
987 if (ret)
988 goto err_unregister5;
990 dock_station_count++;
991 list_add(&ds->sibling, &dock_stations);
992 return 0;
994 err_unregister5:
995 device_remove_file(&dock_device->dev, &dev_attr_type);
996 err_unregister4:
997 device_remove_file(&dock_device->dev, &dev_attr_flags);
998 err_unregister3:
999 device_remove_file(&dock_device->dev, &dev_attr_uid);
1000 err_unregister2:
1001 device_remove_file(&dock_device->dev, &dev_attr_undock);
1002 err_unregister1:
1003 device_remove_file(&dock_device->dev, &dev_attr_docked);
1004 err_unregister:
1005 printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
1006 platform_device_unregister(dock_device);
1007 err_free:
1008 kfree(ds);
1009 ds = NULL;
1010 return ret;
1014 * dock_remove - free up resources related to the dock station
1016 static int dock_remove(struct dock_station *ds)
1018 struct dock_dependent_device *dd, *tmp;
1019 struct platform_device *dock_device = ds->dock_device;
1021 if (!dock_station_count)
1022 return 0;
1024 /* remove dependent devices */
1025 list_for_each_entry_safe(dd, tmp, &ds->dependent_devices, list)
1026 kfree(dd);
1028 /* cleanup sysfs */
1029 device_remove_file(&dock_device->dev, &dev_attr_type);
1030 device_remove_file(&dock_device->dev, &dev_attr_docked);
1031 device_remove_file(&dock_device->dev, &dev_attr_undock);
1032 device_remove_file(&dock_device->dev, &dev_attr_uid);
1033 device_remove_file(&dock_device->dev, &dev_attr_flags);
1034 platform_device_unregister(dock_device);
1036 /* free dock station memory */
1037 kfree(ds);
1038 ds = NULL;
1039 return 0;
1043 * find_dock - look for a dock station
1044 * @handle: acpi handle of a device
1045 * @lvl: unused
1046 * @context: counter of dock stations found
1047 * @rv: unused
1049 * This is called by acpi_walk_namespace to look for dock stations.
1051 static acpi_status
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;
1060 return status;
1063 static acpi_status
1064 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1066 /* If bay is a dock, it's already handled */
1067 if (is_ejectable_bay(handle) && !is_dock(handle))
1068 dock_add(handle);
1069 return AE_OK;
1072 static int __init dock_init(void)
1074 if (acpi_disabled)
1075 return 0;
1077 /* look for a dock station */
1078 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1079 ACPI_UINT32_MAX, find_dock, NULL, NULL);
1081 /* look for bay */
1082 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1083 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1084 if (!dock_station_count) {
1085 printk(KERN_INFO PREFIX "No dock devices found.\n");
1086 return 0;
1089 register_acpi_bus_notifier(&dock_acpi_notifier);
1090 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1091 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
1092 return 0;
1095 static void __exit dock_exit(void)
1097 struct dock_station *dock_station;
1098 struct dock_station *tmp;
1100 unregister_acpi_bus_notifier(&dock_acpi_notifier);
1101 list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
1102 dock_remove(dock_station);
1106 * Must be called before drivers of devices in dock, otherwise we can't know
1107 * which devices are in a dock
1109 subsys_initcall(dock_init);
1110 module_exit(dock_exit);