📄 dock.c
字号:
/* * dock.c - ACPI dock station driver * * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com> * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */#include <linux/kernel.h>#include <linux/module.h>#include <linux/init.h>#include <linux/types.h>#include <linux/notifier.h>#include <linux/platform_device.h>#include <linux/jiffies.h>#include <linux/stddef.h>#include <acpi/acpi_bus.h>#include <acpi/acpi_drivers.h>#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"ACPI_MODULE_NAME("dock");MODULE_AUTHOR("Kristen Carlson Accardi");MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);MODULE_LICENSE("GPL");static int immediate_undock = 1;module_param(immediate_undock, bool, 0644);MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to " "undock immediately when the undock button is pressed, 0 will cause" " the driver to wait for userspace to write the undock sysfs file " " before undocking");static struct atomic_notifier_head dock_notifier_list;static struct platform_device *dock_device;static char dock_device_name[] = "dock";struct dock_station { acpi_handle handle; unsigned long last_dock_time; u32 flags; spinlock_t dd_lock; struct mutex hp_lock; struct list_head dependent_devices; struct list_head hotplug_devices;};struct dock_dependent_device { struct list_head list; struct list_head hotplug_list; acpi_handle handle; acpi_notify_handler handler; void *context;};#define DOCK_DOCKING 0x00000001#define DOCK_UNDOCKING 0x00000002#define DOCK_EVENT 3#define UNDOCK_EVENT 2static struct dock_station *dock_station;/***************************************************************************** * Dock Dependent device functions * *****************************************************************************//** * alloc_dock_dependent_device - allocate and init a dependent device * @handle: the acpi_handle of the dependent device * * Allocate memory for a dependent device structure for a device referenced * by the acpi handle */static struct dock_dependent_device *alloc_dock_dependent_device(acpi_handle handle){ struct dock_dependent_device *dd; dd = kzalloc(sizeof(*dd), GFP_KERNEL); if (dd) { dd->handle = handle; INIT_LIST_HEAD(&dd->list); INIT_LIST_HEAD(&dd->hotplug_list); } return dd;}/** * add_dock_dependent_device - associate a device with the dock station * @ds: The dock station * @dd: The dependent device * * Add the dependent device to the dock's dependent device list. */static voidadd_dock_dependent_device(struct dock_station *ds, struct dock_dependent_device *dd){ spin_lock(&ds->dd_lock); list_add_tail(&dd->list, &ds->dependent_devices); spin_unlock(&ds->dd_lock);}/** * dock_add_hotplug_device - associate a hotplug handler with the dock station * @ds: The dock station * @dd: The dependent device struct * * Add the dependent device to the dock's hotplug device list */static voiddock_add_hotplug_device(struct dock_station *ds, struct dock_dependent_device *dd){ mutex_lock(&ds->hp_lock); list_add_tail(&dd->hotplug_list, &ds->hotplug_devices); mutex_unlock(&ds->hp_lock);}/** * dock_del_hotplug_device - remove a hotplug handler from the dock station * @ds: The dock station * @dd: the dependent device struct * * Delete the dependent device from the dock's hotplug device list */static voiddock_del_hotplug_device(struct dock_station *ds, struct dock_dependent_device *dd){ mutex_lock(&ds->hp_lock); list_del(&dd->hotplug_list); mutex_unlock(&ds->hp_lock);}/** * find_dock_dependent_device - get a device dependent on this dock * @ds: the dock station * @handle: the acpi_handle of the device we want * * iterate over the dependent device list for this dock. If the * dependent device matches the handle, return. */static struct dock_dependent_device *find_dock_dependent_device(struct dock_station *ds, acpi_handle handle){ struct dock_dependent_device *dd; spin_lock(&ds->dd_lock); list_for_each_entry(dd, &ds->dependent_devices, list) { if (handle == dd->handle) { spin_unlock(&ds->dd_lock); return dd; } } spin_unlock(&ds->dd_lock); return NULL;}/***************************************************************************** * Dock functions * *****************************************************************************//** * is_dock - see if a device is a dock station * @handle: acpi handle of the device * * If an acpi object has a _DCK method, then it is by definition a dock * station, so return true. */static int is_dock(acpi_handle handle){ acpi_status status; acpi_handle tmp; status = acpi_get_handle(handle, "_DCK", &tmp); if (ACPI_FAILURE(status)) return 0; return 1;}/** * is_dock_device - see if a device is on a dock station * @handle: acpi handle of the device * * If this device is either the dock station itself, * or is a device dependent on the dock station, then it * is a dock device */int is_dock_device(acpi_handle handle){ if (!dock_station) return 0; if (is_dock(handle) || find_dock_dependent_device(dock_station, handle)) return 1; return 0;}EXPORT_SYMBOL_GPL(is_dock_device);/** * dock_present - see if the dock station is present. * @ds: the dock station * * execute the _STA method. note that present does not * imply that we are docked. */static int dock_present(struct dock_station *ds){ unsigned long sta; acpi_status status; if (ds) { status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta); if (ACPI_SUCCESS(status) && sta) return 1; } return 0;}/** * dock_create_acpi_device - add new devices to acpi * @handle - handle of the device to add * * This function will create a new acpi_device for the given * handle if one does not exist already. This should cause * acpi to scan for drivers for the given devices, and call * matching driver's add routine. * * Returns a pointer to the acpi_device corresponding to the handle. */static struct acpi_device * dock_create_acpi_device(acpi_handle handle){ struct acpi_device *device = NULL; struct acpi_device *parent_device; acpi_handle parent; int ret; if (acpi_bus_get_device(handle, &device)) { /* * no device created for this object, * so we should create one. */ acpi_get_parent(handle, &parent); if (acpi_bus_get_device(parent, &parent_device)) parent_device = NULL; ret = acpi_bus_add(&device, parent_device, handle, ACPI_BUS_TYPE_DEVICE); if (ret) { pr_debug("error adding bus, %x\n", -ret); return NULL; } } return device;}/** * dock_remove_acpi_device - remove the acpi_device struct from acpi * @handle - the handle of the device to remove * * Tell acpi to remove the acpi_device. This should cause any loaded * driver to have it's remove routine called. */static void dock_remove_acpi_device(acpi_handle handle){ struct acpi_device *device; int ret; if (!acpi_bus_get_device(handle, &device)) { ret = acpi_bus_trim(device, 1); if (ret) pr_debug("error removing bus, %x\n", -ret); }}/** * hotplug_dock_devices - insert or remove devices on the dock station * @ds: the dock station * @event: either bus check or eject request * * Some devices on the dock station need to have drivers called * to perform hotplug operations after a dock event has occurred. * Traverse the list of dock devices that have registered a * hotplug handler, and call the handler. */static void hotplug_dock_devices(struct dock_station *ds, u32 event){ struct dock_dependent_device *dd; mutex_lock(&ds->hp_lock); /* * First call driver specific hotplug functions */ list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) { if (dd->handler) dd->handler(dd->handle, event, dd->context); } /* * Now make sure that an acpi_device is created for each * dependent device, or removed if this is an eject request. * This will cause acpi_drivers to be stopped/started if they * exist */ list_for_each_entry(dd, &ds->dependent_devices, list) { if (event == ACPI_NOTIFY_EJECT_REQUEST) dock_remove_acpi_device(dd->handle); else dock_create_acpi_device(dd->handle); } mutex_unlock(&ds->hp_lock);}static void dock_event(struct dock_station *ds, u32 event, int num){ struct device *dev = &dock_device->dev; char event_string[13]; char *envp[] = { event_string, NULL }; if (num == UNDOCK_EVENT) sprintf(event_string, "EVENT=undock"); else sprintf(event_string, "EVENT=dock"); /* * Indicate that the status of the dock station has * changed. */ kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);}/** * eject_dock - respond to a dock eject request * @ds: the dock station * * This is called after _DCK is called, to execute the dock station's * _EJ0 method. */static void eject_dock(struct dock_station *ds){ struct acpi_object_list arg_list; union acpi_object arg; acpi_status status; acpi_handle tmp; /* all dock devices should have _EJ0, but check anyway */ status = acpi_get_handle(ds->handle, "_EJ0", &tmp); if (ACPI_FAILURE(status)) { pr_debug("No _EJ0 support for dock device\n"); return; } arg_list.count = 1; arg_list.pointer = &arg; arg.type = ACPI_TYPE_INTEGER; arg.integer.value = 1; if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL))) pr_debug("Failed to evaluate _EJ0!\n");}/** * handle_dock - handle a dock event * @ds: the dock station * @dock: to dock, or undock - that is the question * * Execute the _DCK method in response to an acpi event */static void handle_dock(struct dock_station *ds, int dock){ acpi_status status; struct acpi_object_list arg_list; union acpi_object arg; struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL }; acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer); printk(KERN_INFO PREFIX "%s - %s\n", (char *)name_buffer.pointer, dock ? "docking" : "undocking"); /* _DCK method has one argument */ arg_list.count = 1; arg_list.pointer = &arg; arg.type = ACPI_TYPE_INTEGER; arg.integer.value = dock; status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer); if (ACPI_FAILURE(status)) printk(KERN_ERR PREFIX "%s - failed to execute _DCK\n", (char *)name_buffer.pointer); kfree(buffer.pointer); kfree(name_buffer.pointer);}static inline void dock(struct dock_station *ds){ handle_dock(ds, 1);}static inline void undock(struct dock_station *ds){ handle_dock(ds, 0);}static inline void begin_dock(struct dock_station *ds){ ds->flags |= DOCK_DOCKING;}static inline void complete_dock(struct dock_station *ds){ ds->flags &= ~(DOCK_DOCKING); ds->last_dock_time = jiffies;}static inline void begin_undock(struct dock_station *ds){ ds->flags |= DOCK_UNDOCKING;}static inline void complete_undock(struct dock_station *ds){ ds->flags &= ~(DOCK_UNDOCKING);}/** * dock_in_progress - see if we are in the middle of handling a dock event * @ds: the dock station * * Sometimes while docking, false dock events can be sent to the driver * because good connections aren't made or some other reason. Ignore these * if we are in the middle of doing something. */static int dock_in_progress(struct dock_station *ds){ if ((ds->flags & DOCK_DOCKING) || time_before(jiffies, (ds->last_dock_time + HZ))) return 1; return 0;}/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -