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
;
54 static const struct acpi_device_id dock_device_ids
[] = {
58 MODULE_DEVICE_TABLE(acpi
, dock_device_ids
);
62 unsigned long last_dock_time
;
66 struct list_head dependent_devices
;
67 struct list_head hotplug_devices
;
69 struct list_head sibling
;
70 struct platform_device
*dock_device
;
72 static LIST_HEAD(dock_stations
);
73 static int dock_station_count
;
75 struct dock_dependent_device
{
76 struct list_head list
;
77 struct list_head hotplug_list
;
79 struct acpi_dock_ops
*ops
;
83 #define DOCK_DOCKING 0x00000001
84 #define DOCK_UNDOCKING 0x00000002
85 #define DOCK_IS_DOCK 0x00000010
86 #define DOCK_IS_ATA 0x00000020
87 #define DOCK_IS_BAT 0x00000040
89 #define UNDOCK_EVENT 2
91 /*****************************************************************************
92 * Dock Dependent device functions *
93 *****************************************************************************/
95 * add_dock_dependent_device - associate a device with the dock station
96 * @ds: The dock station
97 * @handle: handle of the dependent device
99 * Add the dependent device to the dock's dependent device list.
102 add_dock_dependent_device(struct dock_station
*ds
, acpi_handle handle
)
104 struct dock_dependent_device
*dd
;
106 dd
= kzalloc(sizeof(*dd
), GFP_KERNEL
);
111 INIT_LIST_HEAD(&dd
->list
);
112 INIT_LIST_HEAD(&dd
->hotplug_list
);
114 spin_lock(&ds
->dd_lock
);
115 list_add_tail(&dd
->list
, &ds
->dependent_devices
);
116 spin_unlock(&ds
->dd_lock
);
122 * dock_add_hotplug_device - associate a hotplug handler with the dock station
123 * @ds: The dock station
124 * @dd: The dependent device struct
126 * Add the dependent device to the dock's hotplug device list
129 dock_add_hotplug_device(struct dock_station
*ds
,
130 struct dock_dependent_device
*dd
)
132 mutex_lock(&ds
->hp_lock
);
133 list_add_tail(&dd
->hotplug_list
, &ds
->hotplug_devices
);
134 mutex_unlock(&ds
->hp_lock
);
138 * dock_del_hotplug_device - remove a hotplug handler from the dock station
139 * @ds: The dock station
140 * @dd: the dependent device struct
142 * Delete the dependent device from the dock's hotplug device list
145 dock_del_hotplug_device(struct dock_station
*ds
,
146 struct dock_dependent_device
*dd
)
148 mutex_lock(&ds
->hp_lock
);
149 list_del(&dd
->hotplug_list
);
150 mutex_unlock(&ds
->hp_lock
);
154 * find_dock_dependent_device - get a device dependent on this dock
155 * @ds: the dock station
156 * @handle: the acpi_handle of the device we want
158 * iterate over the dependent device list for this dock. If the
159 * dependent device matches the handle, return.
161 static struct dock_dependent_device
*
162 find_dock_dependent_device(struct dock_station
*ds
, acpi_handle handle
)
164 struct dock_dependent_device
*dd
;
166 spin_lock(&ds
->dd_lock
);
167 list_for_each_entry(dd
, &ds
->dependent_devices
, list
) {
168 if (handle
== dd
->handle
) {
169 spin_unlock(&ds
->dd_lock
);
173 spin_unlock(&ds
->dd_lock
);
177 /*****************************************************************************
179 *****************************************************************************/
181 * is_dock - see if a device is a dock station
182 * @handle: acpi handle of the device
184 * If an acpi object has a _DCK method, then it is by definition a dock
185 * station, so return true.
187 static int is_dock(acpi_handle handle
)
192 status
= acpi_get_handle(handle
, "_DCK", &tmp
);
193 if (ACPI_FAILURE(status
))
198 static int is_ejectable(acpi_handle handle
)
203 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
204 if (ACPI_FAILURE(status
))
209 static int is_ata(acpi_handle handle
)
213 if ((ACPI_SUCCESS(acpi_get_handle(handle
, "_GTF", &tmp
))) ||
214 (ACPI_SUCCESS(acpi_get_handle(handle
, "_GTM", &tmp
))) ||
215 (ACPI_SUCCESS(acpi_get_handle(handle
, "_STM", &tmp
))) ||
216 (ACPI_SUCCESS(acpi_get_handle(handle
, "_SDD", &tmp
))))
222 static int is_battery(acpi_handle handle
)
224 struct acpi_device_info
*info
;
227 if (!ACPI_SUCCESS(acpi_get_object_info(handle
, &info
)))
229 if (!(info
->valid
& ACPI_VALID_HID
))
232 ret
= !strcmp("PNP0C0A", info
->hardware_id
.string
);
238 static int is_ejectable_bay(acpi_handle handle
)
241 if (!is_ejectable(handle
))
243 if (is_battery(handle
) || is_ata(handle
))
245 if (!acpi_get_parent(handle
, &phandle
) && is_ata(phandle
))
251 * is_dock_device - see if a device is on a dock station
252 * @handle: acpi handle of the device
254 * If this device is either the dock station itself,
255 * or is a device dependent on the dock station, then it
258 int is_dock_device(acpi_handle handle
)
260 struct dock_station
*dock_station
;
262 if (!dock_station_count
)
267 list_for_each_entry(dock_station
, &dock_stations
, sibling
) {
268 if (find_dock_dependent_device(dock_station
, handle
))
275 EXPORT_SYMBOL_GPL(is_dock_device
);
278 * dock_present - see if the dock station is present.
279 * @ds: the dock station
281 * execute the _STA method. note that present does not
282 * imply that we are docked.
284 static int dock_present(struct dock_station
*ds
)
286 unsigned long long sta
;
290 status
= acpi_evaluate_integer(ds
->handle
, "_STA", NULL
, &sta
);
291 if (ACPI_SUCCESS(status
) && sta
)
300 * dock_create_acpi_device - add new devices to acpi
301 * @handle - handle of the device to add
303 * This function will create a new acpi_device for the given
304 * handle if one does not exist already. This should cause
305 * acpi to scan for drivers for the given devices, and call
306 * matching driver's add routine.
308 * Returns a pointer to the acpi_device corresponding to the handle.
310 static struct acpi_device
* dock_create_acpi_device(acpi_handle handle
)
312 struct acpi_device
*device
= NULL
;
313 struct acpi_device
*parent_device
;
317 if (acpi_bus_get_device(handle
, &device
)) {
319 * no device created for this object,
320 * so we should create one.
322 acpi_get_parent(handle
, &parent
);
323 if (acpi_bus_get_device(parent
, &parent_device
))
324 parent_device
= NULL
;
326 ret
= acpi_bus_add(&device
, parent_device
, handle
,
327 ACPI_BUS_TYPE_DEVICE
);
329 pr_debug("error adding bus, %x\n",
338 * dock_remove_acpi_device - remove the acpi_device struct from acpi
339 * @handle - the handle of the device to remove
341 * Tell acpi to remove the acpi_device. This should cause any loaded
342 * driver to have it's remove routine called.
344 static void dock_remove_acpi_device(acpi_handle handle
)
346 struct acpi_device
*device
;
349 if (!acpi_bus_get_device(handle
, &device
)) {
350 ret
= acpi_bus_trim(device
, 1);
352 pr_debug("error removing bus, %x\n", -ret
);
358 * hotplug_dock_devices - insert or remove devices on the dock station
359 * @ds: the dock station
360 * @event: either bus check or eject request
362 * Some devices on the dock station need to have drivers called
363 * to perform hotplug operations after a dock event has occurred.
364 * Traverse the list of dock devices that have registered a
365 * hotplug handler, and call the handler.
367 static void hotplug_dock_devices(struct dock_station
*ds
, u32 event
)
369 struct dock_dependent_device
*dd
;
371 mutex_lock(&ds
->hp_lock
);
374 * First call driver specific hotplug functions
376 list_for_each_entry(dd
, &ds
->hotplug_devices
, hotplug_list
) {
377 if (dd
->ops
&& dd
->ops
->handler
)
378 dd
->ops
->handler(dd
->handle
, event
, dd
->context
);
382 * Now make sure that an acpi_device is created for each
383 * dependent device, or removed if this is an eject request.
384 * This will cause acpi_drivers to be stopped/started if they
387 list_for_each_entry(dd
, &ds
->dependent_devices
, list
) {
388 if (event
== ACPI_NOTIFY_EJECT_REQUEST
)
389 dock_remove_acpi_device(dd
->handle
);
391 dock_create_acpi_device(dd
->handle
);
393 mutex_unlock(&ds
->hp_lock
);
396 static void dock_event(struct dock_station
*ds
, u32 event
, int num
)
398 struct device
*dev
= &ds
->dock_device
->dev
;
399 char event_string
[13];
400 char *envp
[] = { event_string
, NULL
};
401 struct dock_dependent_device
*dd
;
403 if (num
== UNDOCK_EVENT
)
404 sprintf(event_string
, "EVENT=undock");
406 sprintf(event_string
, "EVENT=dock");
409 * Indicate that the status of the dock station has
412 if (num
== DOCK_EVENT
)
413 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
415 list_for_each_entry(dd
, &ds
->hotplug_devices
, hotplug_list
)
416 if (dd
->ops
&& dd
->ops
->uevent
)
417 dd
->ops
->uevent(dd
->handle
, event
, dd
->context
);
418 if (num
!= DOCK_EVENT
)
419 kobject_uevent_env(&dev
->kobj
, KOBJ_CHANGE
, envp
);
423 * eject_dock - respond to a dock eject request
424 * @ds: the dock station
426 * This is called after _DCK is called, to execute the dock station's
429 static void eject_dock(struct dock_station
*ds
)
431 struct acpi_object_list arg_list
;
432 union acpi_object arg
;
436 /* all dock devices should have _EJ0, but check anyway */
437 status
= acpi_get_handle(ds
->handle
, "_EJ0", &tmp
);
438 if (ACPI_FAILURE(status
)) {
439 pr_debug("No _EJ0 support for dock device\n");
444 arg_list
.pointer
= &arg
;
445 arg
.type
= ACPI_TYPE_INTEGER
;
446 arg
.integer
.value
= 1;
448 if (ACPI_FAILURE(acpi_evaluate_object(ds
->handle
, "_EJ0",
450 pr_debug("Failed to evaluate _EJ0!\n");
454 * handle_dock - handle a dock event
455 * @ds: the dock station
456 * @dock: to dock, or undock - that is the question
458 * Execute the _DCK method in response to an acpi event
460 static void handle_dock(struct dock_station
*ds
, int dock
)
463 struct acpi_object_list arg_list
;
464 union acpi_object arg
;
465 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
466 struct acpi_buffer name_buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
468 acpi_get_name(ds
->handle
, ACPI_FULL_PATHNAME
, &name_buffer
);
470 printk(KERN_INFO PREFIX
"%s - %s\n",
471 (char *)name_buffer
.pointer
, dock
? "docking" : "undocking");
473 /* _DCK method has one argument */
475 arg_list
.pointer
= &arg
;
476 arg
.type
= ACPI_TYPE_INTEGER
;
477 arg
.integer
.value
= dock
;
478 status
= acpi_evaluate_object(ds
->handle
, "_DCK", &arg_list
, &buffer
);
479 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
)
480 ACPI_EXCEPTION((AE_INFO
, status
, "%s - failed to execute"
481 " _DCK\n", (char *)name_buffer
.pointer
));
483 kfree(buffer
.pointer
);
484 kfree(name_buffer
.pointer
);
487 static inline void dock(struct dock_station
*ds
)
492 static inline void undock(struct dock_station
*ds
)
497 static inline void begin_dock(struct dock_station
*ds
)
499 ds
->flags
|= DOCK_DOCKING
;
502 static inline void complete_dock(struct dock_station
*ds
)
504 ds
->flags
&= ~(DOCK_DOCKING
);
505 ds
->last_dock_time
= jiffies
;
508 static inline void begin_undock(struct dock_station
*ds
)
510 ds
->flags
|= DOCK_UNDOCKING
;
513 static inline void complete_undock(struct dock_station
*ds
)
515 ds
->flags
&= ~(DOCK_UNDOCKING
);
518 static void dock_lock(struct dock_station
*ds
, int lock
)
520 struct acpi_object_list arg_list
;
521 union acpi_object arg
;
525 arg_list
.pointer
= &arg
;
526 arg
.type
= ACPI_TYPE_INTEGER
;
527 arg
.integer
.value
= !!lock
;
528 status
= acpi_evaluate_object(ds
->handle
, "_LCK", &arg_list
, NULL
);
529 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
531 printk(KERN_WARNING PREFIX
"Locking device failed\n");
533 printk(KERN_WARNING PREFIX
"Unlocking device failed\n");
538 * dock_in_progress - see if we are in the middle of handling a dock event
539 * @ds: the dock station
541 * Sometimes while docking, false dock events can be sent to the driver
542 * because good connections aren't made or some other reason. Ignore these
543 * if we are in the middle of doing something.
545 static int dock_in_progress(struct dock_station
*ds
)
547 if ((ds
->flags
& DOCK_DOCKING
) ||
548 time_before(jiffies
, (ds
->last_dock_time
+ HZ
)))
554 * register_dock_notifier - add yourself to the dock notifier list
555 * @nb: the callers notifier block
557 * If a driver wishes to be notified about dock events, they can
558 * use this function to put a notifier block on the dock notifier list.
559 * this notifier call chain will be called after a dock event, but
560 * before hotplugging any new devices.
562 int register_dock_notifier(struct notifier_block
*nb
)
564 if (!dock_station_count
)
567 return atomic_notifier_chain_register(&dock_notifier_list
, nb
);
570 EXPORT_SYMBOL_GPL(register_dock_notifier
);
573 * unregister_dock_notifier - remove yourself from the dock notifier list
574 * @nb: the callers notifier block
576 void unregister_dock_notifier(struct notifier_block
*nb
)
578 if (!dock_station_count
)
581 atomic_notifier_chain_unregister(&dock_notifier_list
, nb
);
584 EXPORT_SYMBOL_GPL(unregister_dock_notifier
);
587 * register_hotplug_dock_device - register a hotplug function
588 * @handle: the handle of the device
589 * @ops: handlers to call after docking
590 * @context: device specific data
592 * If a driver would like to perform a hotplug operation after a dock
593 * event, they can register an acpi_notifiy_handler to be called by
594 * the dock driver after _DCK is executed.
597 register_hotplug_dock_device(acpi_handle handle
, struct acpi_dock_ops
*ops
,
600 struct dock_dependent_device
*dd
;
601 struct dock_station
*dock_station
;
604 if (!dock_station_count
)
608 * make sure this handle is for a device dependent on the dock,
609 * this would include the dock station itself
611 list_for_each_entry(dock_station
, &dock_stations
, sibling
) {
613 * An ATA bay can be in a dock and itself can be ejected
614 * seperately, so there are two 'dock stations' which need the
617 dd
= find_dock_dependent_device(dock_station
, handle
);
620 dd
->context
= context
;
621 dock_add_hotplug_device(dock_station
, dd
);
629 EXPORT_SYMBOL_GPL(register_hotplug_dock_device
);
632 * unregister_hotplug_dock_device - remove yourself from the hotplug list
633 * @handle: the acpi handle of the device
635 void unregister_hotplug_dock_device(acpi_handle handle
)
637 struct dock_dependent_device
*dd
;
638 struct dock_station
*dock_station
;
640 if (!dock_station_count
)
643 list_for_each_entry(dock_station
, &dock_stations
, sibling
) {
644 dd
= find_dock_dependent_device(dock_station
, handle
);
646 dock_del_hotplug_device(dock_station
, dd
);
650 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device
);
653 * handle_eject_request - handle an undock request checking for error conditions
655 * Check to make sure the dock device is still present, then undock and
656 * hotremove all the devices that may need removing.
658 static int handle_eject_request(struct dock_station
*ds
, u32 event
)
660 if (dock_in_progress(ds
))
664 * here we need to generate the undock
665 * event prior to actually doing the undock
666 * so that the device struct still exists.
667 * Also, even send the dock event if the
668 * device is not present anymore
670 dock_event(ds
, event
, UNDOCK_EVENT
);
672 hotplug_dock_devices(ds
, ACPI_NOTIFY_EJECT_REQUEST
);
676 if (dock_present(ds
)) {
677 printk(KERN_ERR PREFIX
"Unable to undock!\n");
685 * dock_notify - act upon an acpi dock notification
686 * @handle: the dock station handle
687 * @event: the acpi event
688 * @data: our driver data struct
690 * If we are notified to dock, then check to see if the dock is
691 * present and then dock. Notify all drivers of the dock event,
692 * and then hotplug and devices that may need hotplugging.
694 static void dock_notify(acpi_handle handle
, u32 event
, void *data
)
696 struct dock_station
*ds
= data
;
697 struct acpi_device
*tmp
;
698 int surprise_removal
= 0;
701 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
702 * is sent and _DCK is present, it is assumed to mean an undock
705 if ((ds
->flags
& DOCK_IS_DOCK
) && event
== ACPI_NOTIFY_DEVICE_CHECK
)
706 event
= ACPI_NOTIFY_EJECT_REQUEST
;
709 * dock station: BUS_CHECK - docked or surprise removal
710 * DEVICE_CHECK - undocked
711 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
713 * To simplify event handling, dock dependent device handler always
714 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
715 * ACPI_NOTIFY_EJECT_REQUEST for removal
718 case ACPI_NOTIFY_BUS_CHECK
:
719 case ACPI_NOTIFY_DEVICE_CHECK
:
720 if (!dock_in_progress(ds
) && acpi_bus_get_device(ds
->handle
,
724 if (!dock_present(ds
)) {
725 printk(KERN_ERR PREFIX
"Unable to dock!\n");
729 atomic_notifier_call_chain(&dock_notifier_list
,
731 hotplug_dock_devices(ds
, event
);
733 dock_event(ds
, event
, DOCK_EVENT
);
737 if (dock_present(ds
) || dock_in_progress(ds
))
739 /* This is a surprise removal */
740 surprise_removal
= 1;
741 event
= ACPI_NOTIFY_EJECT_REQUEST
;
743 case ACPI_NOTIFY_EJECT_REQUEST
:
745 if ((immediate_undock
&& !(ds
->flags
& DOCK_IS_ATA
))
747 handle_eject_request(ds
, event
);
749 dock_event(ds
, event
, UNDOCK_EVENT
);
752 printk(KERN_ERR PREFIX
"Unknown dock event %d\n", event
);
759 struct dock_station
*ds
;
762 static void acpi_dock_deferred_cb(void *context
)
764 struct dock_data
*data
= (struct dock_data
*)context
;
766 dock_notify(data
->handle
, data
->event
, data
->ds
);
770 static int acpi_dock_notifier_call(struct notifier_block
*this,
771 unsigned long event
, void *data
)
773 struct dock_station
*dock_station
;
774 acpi_handle handle
= (acpi_handle
)data
;
776 if (event
!= ACPI_NOTIFY_BUS_CHECK
&& event
!= ACPI_NOTIFY_DEVICE_CHECK
777 && event
!= ACPI_NOTIFY_EJECT_REQUEST
)
779 list_for_each_entry(dock_station
, &dock_stations
, sibling
) {
780 if (dock_station
->handle
== handle
) {
781 struct dock_data
*dock_data
;
783 dock_data
= kmalloc(sizeof(*dock_data
), GFP_KERNEL
);
786 dock_data
->handle
= handle
;
787 dock_data
->event
= event
;
788 dock_data
->ds
= dock_station
;
789 acpi_os_hotplug_execute(acpi_dock_deferred_cb
,
797 static struct notifier_block dock_acpi_notifier
= {
798 .notifier_call
= acpi_dock_notifier_call
,
802 * find_dock_devices - find devices on the dock station
803 * @handle: the handle of the device we are examining
805 * @context: the dock station private data
808 * This function is called by acpi_walk_namespace. It will
809 * check to see if an object has an _EJD method. If it does, then it
810 * will see if it is dependent on the dock station.
813 find_dock_devices(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
816 acpi_handle tmp
, parent
;
817 struct dock_station
*ds
= context
;
819 status
= acpi_bus_get_ejd(handle
, &tmp
);
820 if (ACPI_FAILURE(status
)) {
821 /* try the parent device as well */
822 status
= acpi_get_parent(handle
, &parent
);
823 if (ACPI_FAILURE(status
))
825 /* see if parent is dependent on dock */
826 status
= acpi_bus_get_ejd(parent
, &tmp
);
827 if (ACPI_FAILURE(status
))
831 if (tmp
== ds
->handle
)
832 add_dock_dependent_device(ds
, handle
);
839 * show_docked - read method for "docked" file in sysfs
841 static ssize_t
show_docked(struct device
*dev
,
842 struct device_attribute
*attr
, char *buf
)
844 struct acpi_device
*tmp
;
846 struct dock_station
*dock_station
= *((struct dock_station
**)
849 if (ACPI_SUCCESS(acpi_bus_get_device(dock_station
->handle
, &tmp
)))
850 return snprintf(buf
, PAGE_SIZE
, "1\n");
851 return snprintf(buf
, PAGE_SIZE
, "0\n");
853 static DEVICE_ATTR(docked
, S_IRUGO
, show_docked
, NULL
);
856 * show_flags - read method for flags file in sysfs
858 static ssize_t
show_flags(struct device
*dev
,
859 struct device_attribute
*attr
, char *buf
)
861 struct dock_station
*dock_station
= *((struct dock_station
**)
863 return snprintf(buf
, PAGE_SIZE
, "%d\n", dock_station
->flags
);
866 static DEVICE_ATTR(flags
, S_IRUGO
, show_flags
, NULL
);
869 * write_undock - write method for "undock" file in sysfs
871 static ssize_t
write_undock(struct device
*dev
, struct device_attribute
*attr
,
872 const char *buf
, size_t count
)
875 struct dock_station
*dock_station
= *((struct dock_station
**)
881 begin_undock(dock_station
);
882 ret
= handle_eject_request(dock_station
, ACPI_NOTIFY_EJECT_REQUEST
);
883 return ret
? ret
: count
;
885 static DEVICE_ATTR(undock
, S_IWUSR
, NULL
, write_undock
);
888 * show_dock_uid - read method for "uid" file in sysfs
890 static ssize_t
show_dock_uid(struct device
*dev
,
891 struct device_attribute
*attr
, char *buf
)
893 unsigned long long lbuf
;
894 struct dock_station
*dock_station
= *((struct dock_station
**)
896 acpi_status status
= acpi_evaluate_integer(dock_station
->handle
,
897 "_UID", NULL
, &lbuf
);
898 if (ACPI_FAILURE(status
))
901 return snprintf(buf
, PAGE_SIZE
, "%llx\n", lbuf
);
903 static DEVICE_ATTR(uid
, S_IRUGO
, show_dock_uid
, NULL
);
905 static ssize_t
show_dock_type(struct device
*dev
,
906 struct device_attribute
*attr
, char *buf
)
908 struct dock_station
*dock_station
= *((struct dock_station
**)
912 if (dock_station
->flags
& DOCK_IS_DOCK
)
913 type
= "dock_station";
914 else if (dock_station
->flags
& DOCK_IS_ATA
)
916 else if (dock_station
->flags
& DOCK_IS_BAT
)
917 type
= "battery_bay";
921 return snprintf(buf
, PAGE_SIZE
, "%s\n", type
);
923 static DEVICE_ATTR(type
, S_IRUGO
, show_dock_type
, NULL
);
925 static struct attribute
*dock_attributes
[] = {
926 &dev_attr_docked
.attr
,
927 &dev_attr_flags
.attr
,
928 &dev_attr_undock
.attr
,
934 static struct attribute_group dock_attribute_group
= {
935 .attrs
= dock_attributes
939 * dock_add - add a new dock station
940 * @handle: the dock station handle
942 * allocated and initialize a new dock station device. Find all devices
943 * that are on the dock station, and register for dock event notifications.
945 static int dock_add(acpi_handle handle
)
948 struct dock_station
*dock_station
;
949 struct platform_device
*dock_device
;
952 platform_device_register_simple("dock",
953 dock_station_count
, NULL
, 0);
954 if (IS_ERR(dock_device
))
955 return PTR_ERR(dock_device
);
957 /* allocate & initialize the dock_station private data */
958 dock_station
= kzalloc(sizeof(*dock_station
), GFP_KERNEL
);
961 dock_station
->handle
= handle
;
962 dock_station
->dock_device
= dock_device
;
963 dock_station
->last_dock_time
= jiffies
- HZ
;
964 INIT_LIST_HEAD(&dock_station
->dependent_devices
);
965 INIT_LIST_HEAD(&dock_station
->hotplug_devices
);
966 INIT_LIST_HEAD(&dock_station
->sibling
);
967 spin_lock_init(&dock_station
->dd_lock
);
968 mutex_init(&dock_station
->hp_lock
);
969 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list
);
971 platform_device_add_data(dock_device
, &dock_station
,
972 sizeof(struct dock_station
*));
974 /* we want the dock device to send uevents */
975 dev_set_uevent_suppress(&dock_device
->dev
, 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
= sysfs_create_group(&dock_device
->dev
.kobj
, &dock_attribute_group
);
988 /* Find dependent devices */
989 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
990 ACPI_UINT32_MAX
, find_dock_devices
, dock_station
,
993 /* add the dock station as a device dependent on itself */
994 ret
= add_dock_dependent_device(dock_station
, handle
);
998 dock_station_count
++;
999 list_add(&dock_station
->sibling
, &dock_stations
);
1003 sysfs_remove_group(&dock_device
->dev
.kobj
, &dock_attribute_group
);
1005 platform_device_unregister(dock_device
);
1007 kfree(dock_station
);
1008 dock_station
= NULL
;
1009 printk(KERN_ERR
"%s encountered error %d\n", __func__
, ret
);
1014 * dock_remove - free up resources related to the dock station
1016 static int dock_remove(struct dock_station
*dock_station
)
1018 struct dock_dependent_device
*dd
, *tmp
;
1019 struct platform_device
*dock_device
= dock_station
->dock_device
;
1021 if (!dock_station_count
)
1024 /* remove dependent devices */
1025 list_for_each_entry_safe(dd
, tmp
, &dock_station
->dependent_devices
,
1030 sysfs_remove_group(&dock_device
->dev
.kobj
, &dock_attribute_group
);
1031 platform_device_unregister(dock_device
);
1033 /* free dock station memory */
1034 kfree(dock_station
);
1035 dock_station
= NULL
;
1040 * find_dock - look for a dock station
1041 * @handle: acpi handle of a device
1043 * @context: counter of dock stations found
1046 * This is called by acpi_walk_namespace to look for dock stations.
1049 find_dock(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1051 acpi_status status
= AE_OK
;
1053 if (is_dock(handle
)) {
1054 if (dock_add(handle
) >= 0) {
1055 status
= AE_CTRL_TERMINATE
;
1062 find_bay(acpi_handle handle
, u32 lvl
, void *context
, void **rv
)
1064 /* If bay is a dock, it's already handled */
1065 if (is_ejectable_bay(handle
) && !is_dock(handle
))
1070 static int __init
dock_init(void)
1075 /* look for a dock station */
1076 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
1077 ACPI_UINT32_MAX
, find_dock
, NULL
, NULL
);
1080 acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
1081 ACPI_UINT32_MAX
, find_bay
, NULL
, NULL
);
1082 if (!dock_station_count
) {
1083 printk(KERN_INFO PREFIX
"No dock devices found.\n");
1087 register_acpi_bus_notifier(&dock_acpi_notifier
);
1088 printk(KERN_INFO PREFIX
"%s: %d docks/bays found\n",
1089 ACPI_DOCK_DRIVER_DESCRIPTION
, dock_station_count
);
1093 static void __exit
dock_exit(void)
1095 struct dock_station
*dock_station
;
1096 struct dock_station
*tmp
;
1098 unregister_acpi_bus_notifier(&dock_acpi_notifier
);
1099 list_for_each_entry_safe(dock_station
, tmp
, &dock_stations
, sibling
)
1100 dock_remove(dock_station
);
1104 * Must be called before drivers of devices in dock, otherwise we can't know
1105 * which devices are in a dock
1107 subsys_initcall(dock_init
);
1108 module_exit(dock_exit
);