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/slab.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/notifier.h>
31 #include <linux/platform_device.h>
32 #include <linux/jiffies.h>
33 #include <linux/stddef.h>
34 #include <linux/acpi.h>
38 #define PREFIX "ACPI: "
40 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
42 ACPI_MODULE_NAME("dock");
43 MODULE_AUTHOR("Kristen Carlson Accardi");
44 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION
);
45 MODULE_LICENSE("GPL");
47 static bool immediate_undock
= 1;
48 module_param(immediate_undock
, bool, 0644);
49 MODULE_PARM_DESC(immediate_undock
, "1 (default) will cause the driver to "
50 "undock immediately when the undock button is pressed, 0 will cause"
51 " the driver to wait for userspace to write the undock sysfs file "
54 static const struct acpi_device_id dock_device_ids
[] = {
58 MODULE_DEVICE_TABLE(acpi
, dock_device_ids
);
62 unsigned long last_dock_time
;
64 struct list_head dependent_devices
;
66 struct list_head sibling
;
67 struct platform_device
*dock_device
;
69 static LIST_HEAD(dock_stations
);
70 static int dock_station_count
;
71 static DEFINE_MUTEX(hotplug_lock
);
73 struct dock_dependent_device
{
74 struct list_head list
;
75 struct acpi_device
*adev
;
76 const struct acpi_dock_ops
*hp_ops
;
78 unsigned int hp_refcount
;
79 void (*hp_release
)(void *);
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 enum dock_callback_type
{
96 /*****************************************************************************
97 * Dock Dependent device functions *
98 *****************************************************************************/
100 * add_dock_dependent_device - associate a device with the dock station
102 * @adev: Dependent ACPI device object.
104 * Add the dependent device to the dock's dependent device list.
106 static int add_dock_dependent_device(struct dock_station
*ds
,
107 struct acpi_device
*adev
)
109 struct dock_dependent_device
*dd
;
111 dd
= kzalloc(sizeof(*dd
), GFP_KERNEL
);
116 INIT_LIST_HEAD(&dd
->list
);
117 list_add_tail(&dd
->list
, &ds
->dependent_devices
);
122 static void remove_dock_dependent_devices(struct dock_station
*ds
)
124 struct dock_dependent_device
*dd
, *aux
;
126 list_for_each_entry_safe(dd
, aux
, &ds
->dependent_devices
, list
) {
133 * dock_init_hotplug - Initialize a hotplug device on a docking station.
134 * @dd: Dock-dependent device.
135 * @ops: Dock operations to attach to the dependent device.
136 * @context: Data to pass to the @ops callbacks and @release.
137 * @init: Optional initialization routine to run after setting up context.
138 * @release: Optional release routine to run on removal.
140 static int dock_init_hotplug(struct dock_dependent_device
*dd
,
141 const struct acpi_dock_ops
*ops
, void *context
,
142 void (*init
)(void *), void (*release
)(void *))
146 mutex_lock(&hotplug_lock
);
147 if (WARN_ON(dd
->hp_context
)) {
152 dd
->hp_context
= context
;
153 dd
->hp_release
= release
;
157 mutex_unlock(&hotplug_lock
);
162 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
163 * @dd: Dock-dependent device.
165 * Decrement the reference counter of @dd and if 0, detach its hotplug
166 * operations from it, reset its context pointer and run the optional release
167 * routine if present.
169 static void dock_release_hotplug(struct dock_dependent_device
*dd
)
171 mutex_lock(&hotplug_lock
);
172 if (dd
->hp_context
&& !--dd
->hp_refcount
) {
173 void (*release
)(void *) = dd
->hp_release
;
174 void *context
= dd
->hp_context
;
177 dd
->hp_context
= NULL
;
178 dd
->hp_release
= NULL
;
182 mutex_unlock(&hotplug_lock
);
185 static void dock_hotplug_event(struct dock_dependent_device
*dd
, u32 event
,
186 enum dock_callback_type cb_type
)
188 struct acpi_device
*adev
= dd
->adev
;
189 acpi_notify_handler cb
= NULL
;
192 acpi_lock_hp_context();
197 if (cb_type
== DOCK_CALL_FIXUP
) {
198 void (*fixup
)(struct acpi_device
*);
200 fixup
= adev
->hp
->fixup
;
202 acpi_unlock_hp_context();
206 } else if (cb_type
== DOCK_CALL_UEVENT
) {
207 void (*uevent
)(struct acpi_device
*, u32
);
209 uevent
= adev
->hp
->uevent
;
211 acpi_unlock_hp_context();
216 int (*notify
)(struct acpi_device
*, u32
);
218 notify
= adev
->hp
->notify
;
220 acpi_unlock_hp_context();
227 acpi_unlock_hp_context();
229 mutex_lock(&hotplug_lock
);
231 if (dd
->hp_context
) {
236 case DOCK_CALL_FIXUP
:
237 cb
= dd
->hp_ops
->fixup
;
239 case DOCK_CALL_UEVENT
:
240 cb
= dd
->hp_ops
->uevent
;
243 cb
= dd
->hp_ops
->handler
;
248 mutex_unlock(&hotplug_lock
);
254 cb(dd
->adev
->handle
, event
, dd
->hp_context
);
256 dock_release_hotplug(dd
);
259 static struct dock_station
*find_dock_station(acpi_handle handle
)
261 struct dock_station
*ds
;
263 list_for_each_entry(ds
, &dock_stations
, sibling
)
264 if (ds
->handle
== handle
)
271 * find_dock_dependent_device - get a device dependent on this dock
272 * @ds: the dock station
273 * @adev: ACPI device object to find.
275 * iterate over the dependent device list for this dock. If the
276 * dependent device matches the handle, return.
278 static struct dock_dependent_device
*
279 find_dock_dependent_device(struct dock_station
*ds
, struct acpi_device
*adev
)
281 struct dock_dependent_device
*dd
;
283 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
284 if (adev
== dd
->adev
)
290 void register_dock_dependent_device(struct acpi_device
*adev
,
291 acpi_handle dshandle
)
293 struct dock_station
*ds
= find_dock_station(dshandle
);
295 if (ds
&& !find_dock_dependent_device(ds
, adev
))
296 add_dock_dependent_device(ds
, adev
);
299 /*****************************************************************************
301 *****************************************************************************/
304 * is_dock_device - see if a device is on a dock station
305 * @adev: ACPI device object to check.
307 * If this device is either the dock station itself,
308 * or is a device dependent on the dock station, then it
311 int is_dock_device(struct acpi_device
*adev
)
313 struct dock_station
*dock_station
;
315 if (!dock_station_count
)
318 if (acpi_dock_match(adev
->handle
))
321 list_for_each_entry(dock_station
, &dock_stations
, sibling
)
322 if (find_dock_dependent_device(dock_station
, adev
))
327 EXPORT_SYMBOL_GPL(is_dock_device
);
330 * dock_present - see if the dock station is present.
331 * @ds: the dock station
333 * execute the _STA method. note that present does not
334 * imply that we are docked.
336 static int dock_present(struct dock_station
*ds
)
338 unsigned long long sta
;
342 status
= acpi_evaluate_integer(ds
->handle
, "_STA", NULL
, &sta
);
343 if (ACPI_SUCCESS(status
) && sta
)
350 * hot_remove_dock_devices - Remove dock station devices.
353 static void hot_remove_dock_devices(struct dock_station
*ds
)
355 struct dock_dependent_device
*dd
;
358 * Walk the list in reverse order so that devices that have been added
359 * last are removed first (in case there are some indirect dependencies
362 list_for_each_entry_reverse(dd
, &ds
->dependent_devices
, list
)
363 dock_hotplug_event(dd
, ACPI_NOTIFY_EJECT_REQUEST
, false);
365 list_for_each_entry_reverse(dd
, &ds
->dependent_devices
, list
)
366 acpi_bus_trim(dd
->adev
);
370 * hotplug_dock_devices - Insert devices on a dock station.
371 * @ds: the dock station
372 * @event: either bus check or device check request
374 * Some devices on the dock station need to have drivers called
375 * to perform hotplug operations after a dock event has occurred.
376 * Traverse the list of dock devices that have registered a
377 * hotplug handler, and call the handler.
379 static void hotplug_dock_devices(struct dock_station
*ds
, u32 event
)
381 struct dock_dependent_device
*dd
;
383 /* Call driver specific post-dock fixups. */
384 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
385 dock_hotplug_event(dd
, event
, DOCK_CALL_FIXUP
);
387 /* Call driver specific hotplug functions. */
388 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
389 dock_hotplug_event(dd
, event
, DOCK_CALL_HANDLER
);
392 * Check if all devices have been enumerated already. If not, run
393 * acpi_bus_scan() for them and that will cause scan handlers to be
394 * attached to device objects or acpi_drivers to be stopped/started if
397 list_for_each_entry(dd
, &ds
->dependent_devices
, list
) {
398 struct acpi_device
*adev
= dd
->adev
;
400 if (!acpi_device_enumerated(adev
)) {
401 int ret
= acpi_bus_scan(adev
->handle
);
403 dev_dbg(&adev
->dev
, "scan error %d\n", -ret
);
408 static void dock_event(struct dock_station
*ds
, u32 event
, int num
)
410 struct device
*dev
= &ds
->dock_device
->dev
;
411 char event_string
[13];
412 char *envp
[] = { event_string
, NULL
};
413 struct dock_dependent_device
*dd
;
415 if (num
== UNDOCK_EVENT
)
416 sprintf(event_string
, "EVENT=undock");
418 sprintf(event_string
, "EVENT=dock");
421 * Indicate that the status of the dock station has
424 if (num
== DOCK_EVENT
)
425 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
427 list_for_each_entry(dd
, &ds
->dependent_devices
, list
)
428 dock_hotplug_event(dd
, event
, DOCK_CALL_UEVENT
);
430 if (num
!= DOCK_EVENT
)
431 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
435 * handle_dock - handle a dock event
436 * @ds: the dock station
437 * @dock: to dock, or undock - that is the question
439 * Execute the _DCK method in response to an acpi event
441 static void handle_dock(struct dock_station
*ds
, int dock
)
444 struct acpi_object_list arg_list
;
445 union acpi_object arg
;
446 unsigned long long value
;
448 acpi_handle_info(ds
->handle
, "%s\n", dock
? "docking" : "undocking");
450 /* _DCK method has one argument */
452 arg_list
.pointer
= &arg
;
453 arg
.type
= ACPI_TYPE_INTEGER
;
454 arg
.integer
.value
= dock
;
455 status
= acpi_evaluate_integer(ds
->handle
, "_DCK", &arg_list
, &value
);
456 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
)
457 acpi_handle_err(ds
->handle
, "Failed to execute _DCK (0x%x)\n",
461 static inline void dock(struct dock_station
*ds
)
466 static inline void undock(struct dock_station
*ds
)
471 static inline void begin_dock(struct dock_station
*ds
)
473 ds
->flags
|= DOCK_DOCKING
;
476 static inline void complete_dock(struct dock_station
*ds
)
478 ds
->flags
&= ~(DOCK_DOCKING
);
479 ds
->last_dock_time
= jiffies
;
482 static inline void begin_undock(struct dock_station
*ds
)
484 ds
->flags
|= DOCK_UNDOCKING
;
487 static inline void complete_undock(struct dock_station
*ds
)
489 ds
->flags
&= ~(DOCK_UNDOCKING
);
493 * dock_in_progress - see if we are in the middle of handling a dock event
494 * @ds: the dock station
496 * Sometimes while docking, false dock events can be sent to the driver
497 * because good connections aren't made or some other reason. Ignore these
498 * if we are in the middle of doing something.
500 static int dock_in_progress(struct dock_station
*ds
)
502 if ((ds
->flags
& DOCK_DOCKING
) ||
503 time_before(jiffies
, (ds
->last_dock_time
+ HZ
)))
509 * register_hotplug_dock_device - register a hotplug function
510 * @handle: the handle of the device
511 * @ops: handlers to call after docking
512 * @context: device specific data
513 * @init: Optional initialization routine to run after registration
514 * @release: Optional release routine to run on unregistration
516 * If a driver would like to perform a hotplug operation after a dock
517 * event, they can register an acpi_notifiy_handler to be called by
518 * the dock driver after _DCK is executed.
520 int register_hotplug_dock_device(acpi_handle handle
,
521 const struct acpi_dock_ops
*ops
, void *context
,
522 void (*init
)(void *), void (*release
)(void *))
524 struct dock_dependent_device
*dd
;
525 struct dock_station
*dock_station
;
526 struct acpi_device
*adev
;
529 if (WARN_ON(!context
))
532 if (!dock_station_count
)
535 ret
= acpi_bus_get_device(handle
, &adev
);
540 * make sure this handle is for a device dependent on the dock,
541 * this would include the dock station itself
543 list_for_each_entry(dock_station
, &dock_stations
, sibling
) {
545 * An ATA bay can be in a dock and itself can be ejected
546 * separately, so there are two 'dock stations' which need the
549 dd
= find_dock_dependent_device(dock_station
, adev
);
550 if (dd
&& !dock_init_hotplug(dd
, ops
, context
, init
, release
))
556 EXPORT_SYMBOL_GPL(register_hotplug_dock_device
);
559 * unregister_hotplug_dock_device - remove yourself from the hotplug list
560 * @handle: the acpi handle of the device
562 void unregister_hotplug_dock_device(acpi_handle handle
)
564 struct dock_dependent_device
*dd
;
565 struct dock_station
*dock_station
;
566 struct acpi_device
*adev
;
568 if (!dock_station_count
)
571 if (acpi_bus_get_device(handle
, &adev
))
574 list_for_each_entry(dock_station
, &dock_stations
, sibling
) {
575 dd
= find_dock_dependent_device(dock_station
, adev
);
577 dock_release_hotplug(dd
);
580 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device
);
583 * handle_eject_request - handle an undock request checking for error conditions
585 * Check to make sure the dock device is still present, then undock and
586 * hotremove all the devices that may need removing.
588 static int handle_eject_request(struct dock_station
*ds
, u32 event
)
590 if (dock_in_progress(ds
))
594 * here we need to generate the undock
595 * event prior to actually doing the undock
596 * so that the device struct still exists.
597 * Also, even send the dock event if the
598 * device is not present anymore
600 dock_event(ds
, event
, UNDOCK_EVENT
);
602 hot_remove_dock_devices(ds
);
604 acpi_evaluate_lck(ds
->handle
, 0);
605 acpi_evaluate_ej0(ds
->handle
);
606 if (dock_present(ds
)) {
607 acpi_handle_err(ds
->handle
, "Unable to undock!\n");
615 * dock_notify - Handle ACPI dock notification.
616 * @adev: Dock station's ACPI device object.
617 * @event: Event code.
619 * If we are notified to dock, then check to see if the dock is
620 * present and then dock. Notify all drivers of the dock event,
621 * and then hotplug and devices that may need hotplugging.
623 int dock_notify(struct acpi_device
*adev
, u32 event
)
625 acpi_handle handle
= adev
->handle
;
626 struct dock_station
*ds
= find_dock_station(handle
);
627 int surprise_removal
= 0;
633 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
634 * is sent and _DCK is present, it is assumed to mean an undock
637 if ((ds
->flags
& DOCK_IS_DOCK
) && event
== ACPI_NOTIFY_DEVICE_CHECK
)
638 event
= ACPI_NOTIFY_EJECT_REQUEST
;
641 * dock station: BUS_CHECK - docked or surprise removal
642 * DEVICE_CHECK - undocked
643 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
645 * To simplify event handling, dock dependent device handler always
646 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
647 * ACPI_NOTIFY_EJECT_REQUEST for removal
650 case ACPI_NOTIFY_BUS_CHECK
:
651 case ACPI_NOTIFY_DEVICE_CHECK
:
652 if (!dock_in_progress(ds
) && !acpi_device_enumerated(adev
)) {
655 if (!dock_present(ds
)) {
656 acpi_handle_err(handle
, "Unable to dock!\n");
660 hotplug_dock_devices(ds
, event
);
662 dock_event(ds
, event
, DOCK_EVENT
);
663 acpi_evaluate_lck(ds
->handle
, 1);
664 acpi_update_all_gpes();
667 if (dock_present(ds
) || dock_in_progress(ds
))
669 /* This is a surprise removal */
670 surprise_removal
= 1;
671 event
= ACPI_NOTIFY_EJECT_REQUEST
;
673 case ACPI_NOTIFY_EJECT_REQUEST
:
675 if ((immediate_undock
&& !(ds
->flags
& DOCK_IS_ATA
))
677 handle_eject_request(ds
, event
);
679 dock_event(ds
, event
, UNDOCK_EVENT
);
686 * show_docked - read method for "docked" file in sysfs
688 static ssize_t
show_docked(struct device
*dev
,
689 struct device_attribute
*attr
, char *buf
)
691 struct dock_station
*dock_station
= dev
->platform_data
;
692 struct acpi_device
*adev
= NULL
;
694 acpi_bus_get_device(dock_station
->handle
, &adev
);
695 return snprintf(buf
, PAGE_SIZE
, "%u\n", acpi_device_enumerated(adev
));
697 static DEVICE_ATTR(docked
, S_IRUGO
, show_docked
, NULL
);
700 * show_flags - read method for flags file in sysfs
702 static ssize_t
show_flags(struct device
*dev
,
703 struct device_attribute
*attr
, char *buf
)
705 struct dock_station
*dock_station
= dev
->platform_data
;
706 return snprintf(buf
, PAGE_SIZE
, "%d\n", dock_station
->flags
);
709 static DEVICE_ATTR(flags
, S_IRUGO
, show_flags
, NULL
);
712 * write_undock - write method for "undock" file in sysfs
714 static ssize_t
write_undock(struct device
*dev
, struct device_attribute
*attr
,
715 const char *buf
, size_t count
)
718 struct dock_station
*dock_station
= dev
->platform_data
;
723 acpi_scan_lock_acquire();
724 begin_undock(dock_station
);
725 ret
= handle_eject_request(dock_station
, ACPI_NOTIFY_EJECT_REQUEST
);
726 acpi_scan_lock_release();
727 return ret
? ret
: count
;
729 static DEVICE_ATTR(undock
, S_IWUSR
, NULL
, write_undock
);
732 * show_dock_uid - read method for "uid" file in sysfs
734 static ssize_t
show_dock_uid(struct device
*dev
,
735 struct device_attribute
*attr
, char *buf
)
737 unsigned long long lbuf
;
738 struct dock_station
*dock_station
= dev
->platform_data
;
739 acpi_status status
= acpi_evaluate_integer(dock_station
->handle
,
740 "_UID", NULL
, &lbuf
);
741 if (ACPI_FAILURE(status
))
744 return snprintf(buf
, PAGE_SIZE
, "%llx\n", lbuf
);
746 static DEVICE_ATTR(uid
, S_IRUGO
, show_dock_uid
, NULL
);
748 static ssize_t
show_dock_type(struct device
*dev
,
749 struct device_attribute
*attr
, char *buf
)
751 struct dock_station
*dock_station
= dev
->platform_data
;
754 if (dock_station
->flags
& DOCK_IS_DOCK
)
755 type
= "dock_station";
756 else if (dock_station
->flags
& DOCK_IS_ATA
)
758 else if (dock_station
->flags
& DOCK_IS_BAT
)
759 type
= "battery_bay";
763 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
765 static DEVICE_ATTR(type
, S_IRUGO
, show_dock_type
, NULL
);
767 static struct attribute
*dock_attributes
[] = {
768 &dev_attr_docked
.attr
,
769 &dev_attr_flags
.attr
,
770 &dev_attr_undock
.attr
,
776 static struct attribute_group dock_attribute_group
= {
777 .attrs
= dock_attributes
781 * acpi_dock_add - Add a new dock station
782 * @adev: Dock station ACPI device object.
784 * allocated and initialize a new dock station device.
786 void acpi_dock_add(struct acpi_device
*adev
)
788 struct dock_station
*dock_station
, ds
= { NULL
, };
789 struct platform_device_info pdevinfo
;
790 acpi_handle handle
= adev
->handle
;
791 struct platform_device
*dd
;
794 memset(&pdevinfo
, 0, sizeof(pdevinfo
));
795 pdevinfo
.name
= "dock";
796 pdevinfo
.id
= dock_station_count
;
797 pdevinfo
.acpi_node
.companion
= adev
;
799 pdevinfo
.size_data
= sizeof(ds
);
800 dd
= platform_device_register_full(&pdevinfo
);
804 dock_station
= dd
->dev
.platform_data
;
806 dock_station
->handle
= handle
;
807 dock_station
->dock_device
= dd
;
808 dock_station
->last_dock_time
= jiffies
- HZ
;
810 INIT_LIST_HEAD(&dock_station
->sibling
);
811 INIT_LIST_HEAD(&dock_station
->dependent_devices
);
813 /* we want the dock device to send uevents */
814 dev_set_uevent_suppress(&dd
->dev
, 0);
816 if (acpi_dock_match(handle
))
817 dock_station
->flags
|= DOCK_IS_DOCK
;
818 if (acpi_ata_match(handle
))
819 dock_station
->flags
|= DOCK_IS_ATA
;
820 if (acpi_device_is_battery(adev
))
821 dock_station
->flags
|= DOCK_IS_BAT
;
823 ret
= sysfs_create_group(&dd
->dev
.kobj
, &dock_attribute_group
);
827 /* add the dock station as a device dependent on itself */
828 ret
= add_dock_dependent_device(dock_station
, adev
);
832 dock_station_count
++;
833 list_add(&dock_station
->sibling
, &dock_stations
);
834 adev
->flags
.is_dock_station
= true;
835 dev_info(&adev
->dev
, "ACPI dock station (docks/bays count: %d)\n",
840 remove_dock_dependent_devices(dock_station
);
841 sysfs_remove_group(&dd
->dev
.kobj
, &dock_attribute_group
);
843 platform_device_unregister(dd
);
844 acpi_handle_err(handle
, "%s encountered error %d\n", __func__
, ret
);