Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 1 | /* |
| 2 | * dock.c - ACPI dock station driver |
| 3 | * |
| 4 | * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com> |
| 5 | * |
| 6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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. |
| 21 | * |
| 22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 28 | #include <linux/init.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/notifier.h> |
Kristen Carlson Accardi | 671adbe | 2006-12-04 14:49:43 -0800 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 32 | #include <linux/jiffies.h> |
Randy Dunlap | 62a6d7f | 2007-03-26 21:38:49 -0800 | [diff] [blame] | 33 | #include <linux/stddef.h> |
Toshi Kani | cd73018 | 2012-11-20 23:42:30 +0000 | [diff] [blame] | 34 | #include <linux/acpi.h> |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 35 | |
Rashika | 3f9eed5 | 2013-12-17 15:00:04 +0530 | [diff] [blame] | 36 | #include "internal.h" |
| 37 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 38 | #define PREFIX "ACPI: " |
| 39 | |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 40 | #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver" |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 41 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 42 | ACPI_MODULE_NAME("dock"); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 43 | MODULE_AUTHOR("Kristen Carlson Accardi"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 44 | MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 45 | MODULE_LICENSE("GPL"); |
| 46 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 47 | static bool immediate_undock = 1; |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 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 " |
| 52 | " before undocking"); |
| 53 | |
Frank Seidel | a340af1 | 2007-12-07 13:20:42 +0100 | [diff] [blame] | 54 | static const struct acpi_device_id dock_device_ids[] = { |
| 55 | {"LNXDOCK", 0}, |
| 56 | {"", 0}, |
| 57 | }; |
| 58 | MODULE_DEVICE_TABLE(acpi, dock_device_ids); |
| 59 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 60 | struct dock_station { |
| 61 | acpi_handle handle; |
| 62 | unsigned long last_dock_time; |
| 63 | u32 flags; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 64 | struct list_head dependent_devices; |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 65 | |
Alex Chiang | 50d716e | 2009-10-01 11:59:23 -0600 | [diff] [blame] | 66 | struct list_head sibling; |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 67 | struct platform_device *dock_device; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 68 | }; |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 69 | static LIST_HEAD(dock_stations); |
| 70 | static int dock_station_count; |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 71 | static DEFINE_MUTEX(hotplug_lock); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 72 | |
| 73 | struct dock_dependent_device { |
| 74 | struct list_head list; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 75 | acpi_handle handle; |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 76 | const struct acpi_dock_ops *hp_ops; |
| 77 | void *hp_context; |
| 78 | unsigned int hp_refcount; |
| 79 | void (*hp_release)(void *); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | #define DOCK_DOCKING 0x00000001 |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 83 | #define DOCK_UNDOCKING 0x00000002 |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 84 | #define DOCK_IS_DOCK 0x00000010 |
| 85 | #define DOCK_IS_ATA 0x00000020 |
| 86 | #define DOCK_IS_BAT 0x00000040 |
Kristen Carlson Accardi | 5669021 | 2006-08-01 14:59:19 -0700 | [diff] [blame] | 87 | #define DOCK_EVENT 3 |
| 88 | #define UNDOCK_EVENT 2 |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 89 | |
Rafael J. Wysocki | f09ce74 | 2013-07-05 03:03:25 +0200 | [diff] [blame] | 90 | enum dock_callback_type { |
| 91 | DOCK_CALL_HANDLER, |
| 92 | DOCK_CALL_FIXUP, |
| 93 | DOCK_CALL_UEVENT, |
| 94 | }; |
| 95 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 96 | /***************************************************************************** |
| 97 | * Dock Dependent device functions * |
| 98 | *****************************************************************************/ |
| 99 | /** |
Alex Chiang | f69cfdd | 2009-10-19 15:14:29 -0600 | [diff] [blame] | 100 | * add_dock_dependent_device - associate a device with the dock station |
| 101 | * @ds: The dock station |
| 102 | * @handle: handle of the dependent device |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 103 | * |
Alex Chiang | f69cfdd | 2009-10-19 15:14:29 -0600 | [diff] [blame] | 104 | * Add the dependent device to the dock's dependent device list. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 105 | */ |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 106 | static int add_dock_dependent_device(struct dock_station *ds, acpi_handle handle) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 107 | { |
| 108 | struct dock_dependent_device *dd; |
| 109 | |
| 110 | dd = kzalloc(sizeof(*dd), GFP_KERNEL); |
Alex Chiang | f69cfdd | 2009-10-19 15:14:29 -0600 | [diff] [blame] | 111 | if (!dd) |
| 112 | return -ENOMEM; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 113 | |
Alex Chiang | f69cfdd | 2009-10-19 15:14:29 -0600 | [diff] [blame] | 114 | dd->handle = handle; |
| 115 | INIT_LIST_HEAD(&dd->list); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 116 | list_add_tail(&dd->list, &ds->dependent_devices); |
Alex Chiang | f69cfdd | 2009-10-19 15:14:29 -0600 | [diff] [blame] | 117 | |
| 118 | return 0; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 119 | } |
| 120 | |
Rafael J. Wysocki | a30c4c5 | 2013-06-30 23:50:24 +0200 | [diff] [blame] | 121 | static void remove_dock_dependent_devices(struct dock_station *ds) |
| 122 | { |
| 123 | struct dock_dependent_device *dd, *aux; |
| 124 | |
| 125 | list_for_each_entry_safe(dd, aux, &ds->dependent_devices, list) { |
| 126 | list_del(&dd->list); |
| 127 | kfree(dd); |
| 128 | } |
| 129 | } |
| 130 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 131 | /** |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 132 | * dock_init_hotplug - Initialize a hotplug device on a docking station. |
| 133 | * @dd: Dock-dependent device. |
| 134 | * @ops: Dock operations to attach to the dependent device. |
| 135 | * @context: Data to pass to the @ops callbacks and @release. |
| 136 | * @init: Optional initialization routine to run after setting up context. |
| 137 | * @release: Optional release routine to run on removal. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 138 | */ |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 139 | static int dock_init_hotplug(struct dock_dependent_device *dd, |
| 140 | const struct acpi_dock_ops *ops, void *context, |
| 141 | void (*init)(void *), void (*release)(void *)) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 142 | { |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 143 | int ret = 0; |
| 144 | |
| 145 | mutex_lock(&hotplug_lock); |
Rafael J. Wysocki | 4ec2406 | 2013-06-30 23:47:14 +0200 | [diff] [blame] | 146 | if (WARN_ON(dd->hp_context)) { |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 147 | ret = -EEXIST; |
| 148 | } else { |
| 149 | dd->hp_refcount = 1; |
| 150 | dd->hp_ops = ops; |
| 151 | dd->hp_context = context; |
| 152 | dd->hp_release = release; |
Rafael J. Wysocki | 4ec2406 | 2013-06-30 23:47:14 +0200 | [diff] [blame] | 153 | if (init) |
| 154 | init(context); |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 155 | } |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 156 | mutex_unlock(&hotplug_lock); |
| 157 | return ret; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 161 | * dock_release_hotplug - Decrement hotplug reference counter of dock device. |
| 162 | * @dd: Dock-dependent device. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 163 | * |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 164 | * Decrement the reference counter of @dd and if 0, detach its hotplug |
| 165 | * operations from it, reset its context pointer and run the optional release |
| 166 | * routine if present. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 167 | */ |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 168 | static void dock_release_hotplug(struct dock_dependent_device *dd) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 169 | { |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 170 | mutex_lock(&hotplug_lock); |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 171 | if (dd->hp_context && !--dd->hp_refcount) { |
Rafael J. Wysocki | 4ec2406 | 2013-06-30 23:47:14 +0200 | [diff] [blame] | 172 | void (*release)(void *) = dd->hp_release; |
| 173 | void *context = dd->hp_context; |
| 174 | |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 175 | dd->hp_ops = NULL; |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 176 | dd->hp_context = NULL; |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 177 | dd->hp_release = NULL; |
Rafael J. Wysocki | 4ec2406 | 2013-06-30 23:47:14 +0200 | [diff] [blame] | 178 | if (release) |
| 179 | release(context); |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 180 | } |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 181 | mutex_unlock(&hotplug_lock); |
| 182 | } |
| 183 | |
| 184 | static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event, |
Rafael J. Wysocki | f09ce74 | 2013-07-05 03:03:25 +0200 | [diff] [blame] | 185 | enum dock_callback_type cb_type) |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 186 | { |
| 187 | acpi_notify_handler cb = NULL; |
| 188 | bool run = false; |
| 189 | |
| 190 | mutex_lock(&hotplug_lock); |
| 191 | |
| 192 | if (dd->hp_context) { |
| 193 | run = true; |
| 194 | dd->hp_refcount++; |
Rafael J. Wysocki | f09ce74 | 2013-07-05 03:03:25 +0200 | [diff] [blame] | 195 | if (dd->hp_ops) { |
| 196 | switch (cb_type) { |
| 197 | case DOCK_CALL_FIXUP: |
| 198 | cb = dd->hp_ops->fixup; |
| 199 | break; |
| 200 | case DOCK_CALL_UEVENT: |
| 201 | cb = dd->hp_ops->uevent; |
| 202 | break; |
| 203 | default: |
| 204 | cb = dd->hp_ops->handler; |
| 205 | } |
| 206 | } |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | mutex_unlock(&hotplug_lock); |
| 210 | |
| 211 | if (!run) |
| 212 | return; |
| 213 | |
| 214 | if (cb) |
| 215 | cb(dd->handle, event, dd->hp_context); |
| 216 | |
| 217 | dock_release_hotplug(dd); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 218 | } |
| 219 | |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 220 | static struct dock_station *find_dock_station(acpi_handle handle) |
| 221 | { |
| 222 | struct dock_station *ds; |
| 223 | |
| 224 | list_for_each_entry(ds, &dock_stations, sibling) |
| 225 | if (ds->handle == handle) |
| 226 | return ds; |
| 227 | |
| 228 | return NULL; |
| 229 | } |
| 230 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 231 | /** |
| 232 | * find_dock_dependent_device - get a device dependent on this dock |
| 233 | * @ds: the dock station |
| 234 | * @handle: the acpi_handle of the device we want |
| 235 | * |
| 236 | * iterate over the dependent device list for this dock. If the |
| 237 | * dependent device matches the handle, return. |
| 238 | */ |
| 239 | static struct dock_dependent_device * |
| 240 | find_dock_dependent_device(struct dock_station *ds, acpi_handle handle) |
| 241 | { |
| 242 | struct dock_dependent_device *dd; |
| 243 | |
Jiang Liu | ed633e7 | 2013-06-29 00:24:35 +0800 | [diff] [blame] | 244 | list_for_each_entry(dd, &ds->dependent_devices, list) |
| 245 | if (handle == dd->handle) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 246 | return dd; |
Jiang Liu | ed633e7 | 2013-06-29 00:24:35 +0800 | [diff] [blame] | 247 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
| 250 | |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 251 | void register_dock_dependent_device(struct acpi_device *adev, |
| 252 | acpi_handle dshandle) |
| 253 | { |
| 254 | struct dock_station *ds = find_dock_station(dshandle); |
| 255 | acpi_handle handle = adev->handle; |
| 256 | |
| 257 | if (ds && !find_dock_dependent_device(ds, handle)) |
| 258 | add_dock_dependent_device(ds, handle); |
| 259 | } |
| 260 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 261 | /***************************************************************************** |
| 262 | * Dock functions * |
| 263 | *****************************************************************************/ |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 264 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 265 | /** |
| 266 | * is_dock_device - see if a device is on a dock station |
| 267 | * @handle: acpi handle of the device |
| 268 | * |
| 269 | * If this device is either the dock station itself, |
| 270 | * or is a device dependent on the dock station, then it |
| 271 | * is a dock device |
| 272 | */ |
| 273 | int is_dock_device(acpi_handle handle) |
| 274 | { |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 275 | struct dock_station *dock_station; |
| 276 | |
| 277 | if (!dock_station_count) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 278 | return 0; |
| 279 | |
Jiang Liu | c9b5471 | 2013-06-29 00:24:42 +0800 | [diff] [blame] | 280 | if (acpi_dock_match(handle)) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 281 | return 1; |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 282 | |
| 283 | list_for_each_entry(dock_station, &dock_stations, sibling) |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 284 | if (find_dock_dependent_device(dock_station, handle)) |
| 285 | return 1; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 286 | |
| 287 | return 0; |
| 288 | } |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 289 | EXPORT_SYMBOL_GPL(is_dock_device); |
| 290 | |
| 291 | /** |
| 292 | * dock_present - see if the dock station is present. |
| 293 | * @ds: the dock station |
| 294 | * |
| 295 | * execute the _STA method. note that present does not |
| 296 | * imply that we are docked. |
| 297 | */ |
| 298 | static int dock_present(struct dock_station *ds) |
| 299 | { |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 300 | unsigned long long sta; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 301 | acpi_status status; |
| 302 | |
| 303 | if (ds) { |
| 304 | status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta); |
| 305 | if (ACPI_SUCCESS(status) && sta) |
| 306 | return 1; |
| 307 | } |
| 308 | return 0; |
| 309 | } |
| 310 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 311 | /** |
| 312 | * dock_create_acpi_device - add new devices to acpi |
| 313 | * @handle - handle of the device to add |
| 314 | * |
| 315 | * This function will create a new acpi_device for the given |
| 316 | * handle if one does not exist already. This should cause |
| 317 | * acpi to scan for drivers for the given devices, and call |
| 318 | * matching driver's add routine. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 319 | */ |
Jiang Liu | 472d963 | 2013-06-29 00:24:37 +0800 | [diff] [blame] | 320 | static void dock_create_acpi_device(acpi_handle handle) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 321 | { |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 322 | struct acpi_device *device = NULL; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 323 | int ret; |
| 324 | |
Rafael J. Wysocki | 202317a | 2013-11-22 21:54:37 +0100 | [diff] [blame] | 325 | acpi_bus_get_device(handle, &device); |
| 326 | if (!acpi_device_enumerated(device)) { |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 327 | ret = acpi_bus_scan(handle); |
Rafael J. Wysocki | 636458d | 2012-12-21 00:36:47 +0100 | [diff] [blame] | 328 | if (ret) |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 329 | pr_debug("error adding bus, %x\n", -ret); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 330 | } |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /** |
| 334 | * dock_remove_acpi_device - remove the acpi_device struct from acpi |
| 335 | * @handle - the handle of the device to remove |
| 336 | * |
| 337 | * Tell acpi to remove the acpi_device. This should cause any loaded |
| 338 | * driver to have it's remove routine called. |
| 339 | */ |
| 340 | static void dock_remove_acpi_device(acpi_handle handle) |
| 341 | { |
| 342 | struct acpi_device *device; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 343 | |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 344 | if (!acpi_bus_get_device(handle, &device)) |
| 345 | acpi_bus_trim(device); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 348 | /** |
Rafael J. Wysocki | 37f9087 | 2013-06-30 23:46:42 +0200 | [diff] [blame] | 349 | * hot_remove_dock_devices - Remove dock station devices. |
| 350 | * @ds: Dock station. |
| 351 | */ |
| 352 | static void hot_remove_dock_devices(struct dock_station *ds) |
| 353 | { |
| 354 | struct dock_dependent_device *dd; |
| 355 | |
| 356 | /* |
| 357 | * Walk the list in reverse order so that devices that have been added |
| 358 | * last are removed first (in case there are some indirect dependencies |
| 359 | * between them). |
| 360 | */ |
| 361 | list_for_each_entry_reverse(dd, &ds->dependent_devices, list) |
| 362 | dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false); |
| 363 | |
| 364 | list_for_each_entry_reverse(dd, &ds->dependent_devices, list) |
| 365 | dock_remove_acpi_device(dd->handle); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * hotplug_dock_devices - Insert devices on a dock station. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 370 | * @ds: the dock station |
Rafael J. Wysocki | 37f9087 | 2013-06-30 23:46:42 +0200 | [diff] [blame] | 371 | * @event: either bus check or device check request |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 372 | * |
| 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. |
| 377 | */ |
| 378 | static void hotplug_dock_devices(struct dock_station *ds, u32 event) |
| 379 | { |
| 380 | struct dock_dependent_device *dd; |
| 381 | |
Rafael J. Wysocki | f09ce74 | 2013-07-05 03:03:25 +0200 | [diff] [blame] | 382 | /* Call driver specific post-dock fixups. */ |
| 383 | list_for_each_entry(dd, &ds->dependent_devices, list) |
| 384 | dock_hotplug_event(dd, event, DOCK_CALL_FIXUP); |
| 385 | |
Rafael J. Wysocki | 37f9087 | 2013-06-30 23:46:42 +0200 | [diff] [blame] | 386 | /* Call driver specific hotplug functions. */ |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 387 | list_for_each_entry(dd, &ds->dependent_devices, list) |
Rafael J. Wysocki | f09ce74 | 2013-07-05 03:03:25 +0200 | [diff] [blame] | 388 | dock_hotplug_event(dd, event, DOCK_CALL_HANDLER); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 389 | |
| 390 | /* |
Rafael J. Wysocki | 37f9087 | 2013-06-30 23:46:42 +0200 | [diff] [blame] | 391 | * Now make sure that an acpi_device is created for each dependent |
| 392 | * device. That will cause scan handlers to be attached to device |
| 393 | * objects or acpi_drivers to be stopped/started if they are present. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 394 | */ |
Rafael J. Wysocki | 37f9087 | 2013-06-30 23:46:42 +0200 | [diff] [blame] | 395 | list_for_each_entry(dd, &ds->dependent_devices, list) |
| 396 | dock_create_acpi_device(dd->handle); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void dock_event(struct dock_station *ds, u32 event, int num) |
| 400 | { |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 401 | struct device *dev = &ds->dock_device->dev; |
Holger Macht | 66b5682 | 2007-08-10 13:10:32 -0700 | [diff] [blame] | 402 | char event_string[13]; |
Kristen Carlson Accardi | 79a8f70 | 2007-05-09 15:10:22 -0700 | [diff] [blame] | 403 | char *envp[] = { event_string, NULL }; |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 404 | struct dock_dependent_device *dd; |
Kristen Carlson Accardi | 79a8f70 | 2007-05-09 15:10:22 -0700 | [diff] [blame] | 405 | |
| 406 | if (num == UNDOCK_EVENT) |
Holger Macht | 66b5682 | 2007-08-10 13:10:32 -0700 | [diff] [blame] | 407 | sprintf(event_string, "EVENT=undock"); |
Kristen Carlson Accardi | 79a8f70 | 2007-05-09 15:10:22 -0700 | [diff] [blame] | 408 | else |
Holger Macht | 66b5682 | 2007-08-10 13:10:32 -0700 | [diff] [blame] | 409 | sprintf(event_string, "EVENT=dock"); |
Kristen Carlson Accardi | 79a8f70 | 2007-05-09 15:10:22 -0700 | [diff] [blame] | 410 | |
Kristen Carlson Accardi | 5669021 | 2006-08-01 14:59:19 -0700 | [diff] [blame] | 411 | /* |
Kristen Carlson Accardi | 8ea86e0 | 2006-12-11 12:05:08 -0800 | [diff] [blame] | 412 | * Indicate that the status of the dock station has |
| 413 | * changed. |
Kristen Carlson Accardi | 5669021 | 2006-08-01 14:59:19 -0700 | [diff] [blame] | 414 | */ |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 415 | if (num == DOCK_EVENT) |
| 416 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); |
| 417 | |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 418 | list_for_each_entry(dd, &ds->dependent_devices, list) |
Rafael J. Wysocki | f09ce74 | 2013-07-05 03:03:25 +0200 | [diff] [blame] | 419 | dock_hotplug_event(dd, event, DOCK_CALL_UEVENT); |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 420 | |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 421 | if (num != DOCK_EVENT) |
| 422 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /** |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 426 | * handle_dock - handle a dock event |
| 427 | * @ds: the dock station |
| 428 | * @dock: to dock, or undock - that is the question |
| 429 | * |
| 430 | * Execute the _DCK method in response to an acpi event |
| 431 | */ |
| 432 | static void handle_dock(struct dock_station *ds, int dock) |
| 433 | { |
| 434 | acpi_status status; |
| 435 | struct acpi_object_list arg_list; |
| 436 | union acpi_object arg; |
Zhang Rui | 6a868e1 | 2013-09-03 08:32:10 +0800 | [diff] [blame] | 437 | unsigned long long value; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 438 | |
Toshi Kani | cd73018 | 2012-11-20 23:42:30 +0000 | [diff] [blame] | 439 | acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking"); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 440 | |
| 441 | /* _DCK method has one argument */ |
| 442 | arg_list.count = 1; |
| 443 | arg_list.pointer = &arg; |
| 444 | arg.type = ACPI_TYPE_INTEGER; |
| 445 | arg.integer.value = dock; |
Zhang Rui | 6a868e1 | 2013-09-03 08:32:10 +0800 | [diff] [blame] | 446 | status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value); |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 447 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) |
Toshi Kani | cd73018 | 2012-11-20 23:42:30 +0000 | [diff] [blame] | 448 | acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n", |
| 449 | status); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static inline void dock(struct dock_station *ds) |
| 453 | { |
| 454 | handle_dock(ds, 1); |
| 455 | } |
| 456 | |
| 457 | static inline void undock(struct dock_station *ds) |
| 458 | { |
| 459 | handle_dock(ds, 0); |
| 460 | } |
| 461 | |
| 462 | static inline void begin_dock(struct dock_station *ds) |
| 463 | { |
| 464 | ds->flags |= DOCK_DOCKING; |
| 465 | } |
| 466 | |
| 467 | static inline void complete_dock(struct dock_station *ds) |
| 468 | { |
| 469 | ds->flags &= ~(DOCK_DOCKING); |
| 470 | ds->last_dock_time = jiffies; |
| 471 | } |
| 472 | |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 473 | static inline void begin_undock(struct dock_station *ds) |
| 474 | { |
| 475 | ds->flags |= DOCK_UNDOCKING; |
| 476 | } |
| 477 | |
| 478 | static inline void complete_undock(struct dock_station *ds) |
| 479 | { |
| 480 | ds->flags &= ~(DOCK_UNDOCKING); |
| 481 | } |
| 482 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 483 | /** |
| 484 | * dock_in_progress - see if we are in the middle of handling a dock event |
| 485 | * @ds: the dock station |
| 486 | * |
| 487 | * Sometimes while docking, false dock events can be sent to the driver |
| 488 | * because good connections aren't made or some other reason. Ignore these |
| 489 | * if we are in the middle of doing something. |
| 490 | */ |
| 491 | static int dock_in_progress(struct dock_station *ds) |
| 492 | { |
| 493 | if ((ds->flags & DOCK_DOCKING) || |
| 494 | time_before(jiffies, (ds->last_dock_time + HZ))) |
| 495 | return 1; |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | /** |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 500 | * register_hotplug_dock_device - register a hotplug function |
| 501 | * @handle: the handle of the device |
Shaohua Li | 1253f7a | 2008-08-28 10:06:16 +0800 | [diff] [blame] | 502 | * @ops: handlers to call after docking |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 503 | * @context: device specific data |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 504 | * @init: Optional initialization routine to run after registration |
| 505 | * @release: Optional release routine to run on unregistration |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 506 | * |
| 507 | * If a driver would like to perform a hotplug operation after a dock |
| 508 | * event, they can register an acpi_notifiy_handler to be called by |
| 509 | * the dock driver after _DCK is executed. |
| 510 | */ |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 511 | int register_hotplug_dock_device(acpi_handle handle, |
| 512 | const struct acpi_dock_ops *ops, void *context, |
| 513 | void (*init)(void *), void (*release)(void *)) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 514 | { |
| 515 | struct dock_dependent_device *dd; |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 516 | struct dock_station *dock_station; |
Shaohua Li | 61b8369 | 2008-08-28 10:07:14 +0800 | [diff] [blame] | 517 | int ret = -EINVAL; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 518 | |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 519 | if (WARN_ON(!context)) |
| 520 | return -EINVAL; |
| 521 | |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 522 | if (!dock_station_count) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 523 | return -ENODEV; |
| 524 | |
| 525 | /* |
| 526 | * make sure this handle is for a device dependent on the dock, |
| 527 | * this would include the dock station itself |
| 528 | */ |
Alex Chiang | 50d716e | 2009-10-01 11:59:23 -0600 | [diff] [blame] | 529 | list_for_each_entry(dock_station, &dock_stations, sibling) { |
Shaohua Li | 61b8369 | 2008-08-28 10:07:14 +0800 | [diff] [blame] | 530 | /* |
| 531 | * An ATA bay can be in a dock and itself can be ejected |
Daniel Mack | 3ad2f3fb | 2010-02-03 08:01:28 +0800 | [diff] [blame] | 532 | * separately, so there are two 'dock stations' which need the |
Shaohua Li | 61b8369 | 2008-08-28 10:07:14 +0800 | [diff] [blame] | 533 | * ops |
| 534 | */ |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 535 | dd = find_dock_dependent_device(dock_station, handle); |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 536 | if (dd && !dock_init_hotplug(dd, ops, context, init, release)) |
Shaohua Li | 61b8369 | 2008-08-28 10:07:14 +0800 | [diff] [blame] | 537 | ret = 0; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 538 | } |
| 539 | |
Shaohua Li | 61b8369 | 2008-08-28 10:07:14 +0800 | [diff] [blame] | 540 | return ret; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 541 | } |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 542 | EXPORT_SYMBOL_GPL(register_hotplug_dock_device); |
| 543 | |
| 544 | /** |
| 545 | * unregister_hotplug_dock_device - remove yourself from the hotplug list |
| 546 | * @handle: the acpi handle of the device |
| 547 | */ |
| 548 | void unregister_hotplug_dock_device(acpi_handle handle) |
| 549 | { |
| 550 | struct dock_dependent_device *dd; |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 551 | struct dock_station *dock_station; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 552 | |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 553 | if (!dock_station_count) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 554 | return; |
| 555 | |
Alex Chiang | 50d716e | 2009-10-01 11:59:23 -0600 | [diff] [blame] | 556 | list_for_each_entry(dock_station, &dock_stations, sibling) { |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 557 | dd = find_dock_dependent_device(dock_station, handle); |
| 558 | if (dd) |
Rafael J. Wysocki | 21a3101 | 2013-06-24 11:22:53 +0200 | [diff] [blame] | 559 | dock_release_hotplug(dd); |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 560 | } |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 561 | } |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 562 | EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device); |
| 563 | |
| 564 | /** |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 565 | * handle_eject_request - handle an undock request checking for error conditions |
| 566 | * |
| 567 | * Check to make sure the dock device is still present, then undock and |
| 568 | * hotremove all the devices that may need removing. |
| 569 | */ |
| 570 | static int handle_eject_request(struct dock_station *ds, u32 event) |
| 571 | { |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 572 | if (dock_in_progress(ds)) |
| 573 | return -EBUSY; |
| 574 | |
| 575 | /* |
| 576 | * here we need to generate the undock |
| 577 | * event prior to actually doing the undock |
| 578 | * so that the device struct still exists. |
Holger Macht | afd7301 | 2008-08-06 17:56:01 +0200 | [diff] [blame] | 579 | * Also, even send the dock event if the |
| 580 | * device is not present anymore |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 581 | */ |
| 582 | dock_event(ds, event, UNDOCK_EVENT); |
Holger Macht | afd7301 | 2008-08-06 17:56:01 +0200 | [diff] [blame] | 583 | |
Rafael J. Wysocki | 37f9087 | 2013-06-30 23:46:42 +0200 | [diff] [blame] | 584 | hot_remove_dock_devices(ds); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 585 | undock(ds); |
Jiang Liu | c9b5471 | 2013-06-29 00:24:42 +0800 | [diff] [blame] | 586 | acpi_evaluate_lck(ds->handle, 0); |
| 587 | acpi_evaluate_ej0(ds->handle); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 588 | if (dock_present(ds)) { |
Toshi Kani | cd73018 | 2012-11-20 23:42:30 +0000 | [diff] [blame] | 589 | acpi_handle_err(ds->handle, "Unable to undock!\n"); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 590 | return -EBUSY; |
| 591 | } |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 592 | complete_undock(ds); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /** |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 597 | * dock_notify - Handle ACPI dock notification. |
| 598 | * @adev: Dock station's ACPI device object. |
| 599 | * @event: Event code. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 600 | * |
| 601 | * If we are notified to dock, then check to see if the dock is |
| 602 | * present and then dock. Notify all drivers of the dock event, |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 603 | * and then hotplug and devices that may need hotplugging. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 604 | */ |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 605 | int dock_notify(struct acpi_device *adev, u32 event) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 606 | { |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 607 | acpi_handle handle = adev->handle; |
| 608 | struct dock_station *ds = find_dock_station(handle); |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 609 | int surprise_removal = 0; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 610 | |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 611 | if (!ds) |
| 612 | return -ENODEV; |
| 613 | |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 614 | /* |
| 615 | * According to acpi spec 3.0a, if a DEVICE_CHECK notification |
| 616 | * is sent and _DCK is present, it is assumed to mean an undock |
| 617 | * request. |
| 618 | */ |
| 619 | if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK) |
| 620 | event = ACPI_NOTIFY_EJECT_REQUEST; |
| 621 | |
| 622 | /* |
| 623 | * dock station: BUS_CHECK - docked or surprise removal |
| 624 | * DEVICE_CHECK - undocked |
| 625 | * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal |
| 626 | * |
| 627 | * To simplify event handling, dock dependent device handler always |
| 628 | * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and |
| 629 | * ACPI_NOTIFY_EJECT_REQUEST for removal |
| 630 | */ |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 631 | switch (event) { |
| 632 | case ACPI_NOTIFY_BUS_CHECK: |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 633 | case ACPI_NOTIFY_DEVICE_CHECK: |
Rafael J. Wysocki | 0a8e5c3 | 2014-02-10 13:44:20 +0100 | [diff] [blame] | 634 | if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) { |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 635 | begin_dock(ds); |
| 636 | dock(ds); |
| 637 | if (!dock_present(ds)) { |
Toshi Kani | cd73018 | 2012-11-20 23:42:30 +0000 | [diff] [blame] | 638 | acpi_handle_err(handle, "Unable to dock!\n"); |
Shaohua Li | 8b59560 | 2008-08-28 10:02:03 +0800 | [diff] [blame] | 639 | complete_dock(ds); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 640 | break; |
| 641 | } |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 642 | hotplug_dock_devices(ds, event); |
| 643 | complete_dock(ds); |
| 644 | dock_event(ds, event, DOCK_EVENT); |
Jiang Liu | c9b5471 | 2013-06-29 00:24:42 +0800 | [diff] [blame] | 645 | acpi_evaluate_lck(ds->handle, 1); |
Lin Ming | 3a37898 | 2010-12-13 13:36:15 +0800 | [diff] [blame] | 646 | acpi_update_all_gpes(); |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 647 | break; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 648 | } |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 649 | if (dock_present(ds) || dock_in_progress(ds)) |
| 650 | break; |
| 651 | /* This is a surprise removal */ |
| 652 | surprise_removal = 1; |
| 653 | event = ACPI_NOTIFY_EJECT_REQUEST; |
| 654 | /* Fall back */ |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 655 | case ACPI_NOTIFY_EJECT_REQUEST: |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 656 | begin_undock(ds); |
Shaohua Li | f730ae1 | 2008-08-28 10:05:45 +0800 | [diff] [blame] | 657 | if ((immediate_undock && !(ds->flags & DOCK_IS_ATA)) |
| 658 | || surprise_removal) |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 659 | handle_eject_request(ds, event); |
| 660 | else |
| 661 | dock_event(ds, event, UNDOCK_EVENT); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 662 | break; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 663 | } |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 664 | return 0; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 665 | } |
| 666 | |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 667 | /* |
| 668 | * show_docked - read method for "docked" file in sysfs |
| 669 | */ |
| 670 | static ssize_t show_docked(struct device *dev, |
| 671 | struct device_attribute *attr, char *buf) |
| 672 | { |
Alex Chiang | fe06fba | 2009-10-19 15:14:45 -0600 | [diff] [blame] | 673 | struct dock_station *dock_station = dev->platform_data; |
Rafael J. Wysocki | ab62f9c | 2014-02-15 01:29:06 +0100 | [diff] [blame] | 674 | struct acpi_device *adev = NULL; |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 675 | |
Rafael J. Wysocki | ab62f9c | 2014-02-15 01:29:06 +0100 | [diff] [blame] | 676 | acpi_bus_get_device(dock_station->handle, &adev); |
| 677 | return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev)); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 678 | } |
Adrian Bunk | e5685b9 | 2007-10-24 18:24:42 +0200 | [diff] [blame] | 679 | static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 680 | |
| 681 | /* |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 682 | * show_flags - read method for flags file in sysfs |
| 683 | */ |
| 684 | static ssize_t show_flags(struct device *dev, |
| 685 | struct device_attribute *attr, char *buf) |
| 686 | { |
Alex Chiang | fe06fba | 2009-10-19 15:14:45 -0600 | [diff] [blame] | 687 | struct dock_station *dock_station = dev->platform_data; |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 688 | return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags); |
| 689 | |
| 690 | } |
Adrian Bunk | e5685b9 | 2007-10-24 18:24:42 +0200 | [diff] [blame] | 691 | static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL); |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 692 | |
| 693 | /* |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 694 | * write_undock - write method for "undock" file in sysfs |
| 695 | */ |
| 696 | static ssize_t write_undock(struct device *dev, struct device_attribute *attr, |
| 697 | const char *buf, size_t count) |
| 698 | { |
| 699 | int ret; |
Alex Chiang | fe06fba | 2009-10-19 15:14:45 -0600 | [diff] [blame] | 700 | struct dock_station *dock_station = dev->platform_data; |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 701 | |
| 702 | if (!count) |
| 703 | return -EINVAL; |
| 704 | |
Rafael J. Wysocki | 8112006 | 2013-06-16 00:38:30 +0200 | [diff] [blame] | 705 | acpi_scan_lock_acquire(); |
Holger Macht | 9171f834 | 2008-03-12 01:07:27 +0100 | [diff] [blame] | 706 | begin_undock(dock_station); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 707 | ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST); |
Rafael J. Wysocki | 8112006 | 2013-06-16 00:38:30 +0200 | [diff] [blame] | 708 | acpi_scan_lock_release(); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 709 | return ret ? ret: count; |
| 710 | } |
Adrian Bunk | e5685b9 | 2007-10-24 18:24:42 +0200 | [diff] [blame] | 711 | static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock); |
brandon@ifup.org | c80fdbe | 2006-12-04 14:49:58 -0800 | [diff] [blame] | 712 | |
Ilya A. Volynets-Evenbakh | ac122bb | 2007-02-19 15:19:31 -0800 | [diff] [blame] | 713 | /* |
| 714 | * show_dock_uid - read method for "uid" file in sysfs |
| 715 | */ |
| 716 | static ssize_t show_dock_uid(struct device *dev, |
| 717 | struct device_attribute *attr, char *buf) |
| 718 | { |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 719 | unsigned long long lbuf; |
Alex Chiang | fe06fba | 2009-10-19 15:14:45 -0600 | [diff] [blame] | 720 | struct dock_station *dock_station = dev->platform_data; |
Kristen Carlson Accardi | 38ff4ff | 2007-05-09 15:04:24 -0700 | [diff] [blame] | 721 | acpi_status status = acpi_evaluate_integer(dock_station->handle, |
| 722 | "_UID", NULL, &lbuf); |
| 723 | if (ACPI_FAILURE(status)) |
Ilya A. Volynets-Evenbakh | ac122bb | 2007-02-19 15:19:31 -0800 | [diff] [blame] | 724 | return 0; |
Kristen Carlson Accardi | 38ff4ff | 2007-05-09 15:04:24 -0700 | [diff] [blame] | 725 | |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 726 | return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf); |
Ilya A. Volynets-Evenbakh | ac122bb | 2007-02-19 15:19:31 -0800 | [diff] [blame] | 727 | } |
Adrian Bunk | e5685b9 | 2007-10-24 18:24:42 +0200 | [diff] [blame] | 728 | static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL); |
Ilya A. Volynets-Evenbakh | ac122bb | 2007-02-19 15:19:31 -0800 | [diff] [blame] | 729 | |
Shaohua Li | 8652b00 | 2008-08-28 10:07:45 +0800 | [diff] [blame] | 730 | static ssize_t show_dock_type(struct device *dev, |
| 731 | struct device_attribute *attr, char *buf) |
| 732 | { |
Alex Chiang | fe06fba | 2009-10-19 15:14:45 -0600 | [diff] [blame] | 733 | struct dock_station *dock_station = dev->platform_data; |
Shaohua Li | 8652b00 | 2008-08-28 10:07:45 +0800 | [diff] [blame] | 734 | char *type; |
| 735 | |
| 736 | if (dock_station->flags & DOCK_IS_DOCK) |
| 737 | type = "dock_station"; |
| 738 | else if (dock_station->flags & DOCK_IS_ATA) |
| 739 | type = "ata_bay"; |
| 740 | else if (dock_station->flags & DOCK_IS_BAT) |
| 741 | type = "battery_bay"; |
| 742 | else |
| 743 | type = "unknown"; |
| 744 | |
| 745 | return snprintf(buf, PAGE_SIZE, "%s\n", type); |
| 746 | } |
| 747 | static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL); |
| 748 | |
Alex Chiang | 5f46c2f | 2009-10-19 15:14:24 -0600 | [diff] [blame] | 749 | static struct attribute *dock_attributes[] = { |
| 750 | &dev_attr_docked.attr, |
| 751 | &dev_attr_flags.attr, |
| 752 | &dev_attr_undock.attr, |
| 753 | &dev_attr_uid.attr, |
| 754 | &dev_attr_type.attr, |
| 755 | NULL |
| 756 | }; |
| 757 | |
| 758 | static struct attribute_group dock_attribute_group = { |
| 759 | .attrs = dock_attributes |
| 760 | }; |
| 761 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 762 | /** |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 763 | * acpi_dock_add - Add a new dock station |
| 764 | * @adev: Dock station ACPI device object. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 765 | * |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 766 | * allocated and initialize a new dock station device. |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 767 | */ |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 768 | void acpi_dock_add(struct acpi_device *adev) |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 769 | { |
Rafael J. Wysocki | 2efbca4 | 2013-07-05 03:23:36 +0200 | [diff] [blame] | 770 | struct dock_station *dock_station, ds = { NULL, }; |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 771 | acpi_handle handle = adev->handle; |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 772 | struct platform_device *dd; |
Rafael J. Wysocki | 2efbca4 | 2013-07-05 03:23:36 +0200 | [diff] [blame] | 773 | int ret; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 774 | |
Rafael J. Wysocki | 2efbca4 | 2013-07-05 03:23:36 +0200 | [diff] [blame] | 775 | dd = platform_device_register_data(NULL, "dock", dock_station_count, |
| 776 | &ds, sizeof(ds)); |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 777 | if (IS_ERR(dd)) |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 778 | return; |
Alex Chiang | 9751cb7 | 2009-10-19 15:14:40 -0600 | [diff] [blame] | 779 | |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 780 | dock_station = dd->dev.platform_data; |
| 781 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 782 | dock_station->handle = handle; |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 783 | dock_station->dock_device = dd; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 784 | dock_station->last_dock_time = jiffies - HZ; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 785 | |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 786 | INIT_LIST_HEAD(&dock_station->sibling); |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 787 | INIT_LIST_HEAD(&dock_station->dependent_devices); |
Kristen Carlson Accardi | a0cd35f | 2007-05-09 15:08:15 -0700 | [diff] [blame] | 788 | |
Kristen Carlson Accardi | 9ef2a9a | 2007-05-09 15:09:12 -0700 | [diff] [blame] | 789 | /* we want the dock device to send uevents */ |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 790 | dev_set_uevent_suppress(&dd->dev, 0); |
Kristen Carlson Accardi | 9ef2a9a | 2007-05-09 15:09:12 -0700 | [diff] [blame] | 791 | |
Jiang Liu | c9b5471 | 2013-06-29 00:24:42 +0800 | [diff] [blame] | 792 | if (acpi_dock_match(handle)) |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 793 | dock_station->flags |= DOCK_IS_DOCK; |
Jiang Liu | c9b5471 | 2013-06-29 00:24:42 +0800 | [diff] [blame] | 794 | if (acpi_ata_match(handle)) |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 795 | dock_station->flags |= DOCK_IS_ATA; |
Rafael J. Wysocki | b43109f | 2014-02-16 00:09:34 +0100 | [diff] [blame^] | 796 | if (acpi_device_is_battery(adev)) |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 797 | dock_station->flags |= DOCK_IS_BAT; |
| 798 | |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 799 | ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group); |
Shaohua Li | 8652b00 | 2008-08-28 10:07:45 +0800 | [diff] [blame] | 800 | if (ret) |
Alex Chiang | 5f46c2f | 2009-10-19 15:14:24 -0600 | [diff] [blame] | 801 | goto err_unregister; |
Kristen Carlson Accardi | 671adbe | 2006-12-04 14:49:43 -0800 | [diff] [blame] | 802 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 803 | /* add the dock station as a device dependent on itself */ |
Alex Chiang | f69cfdd | 2009-10-19 15:14:29 -0600 | [diff] [blame] | 804 | ret = add_dock_dependent_device(dock_station, handle); |
| 805 | if (ret) |
Alex Chiang | 5f46c2f | 2009-10-19 15:14:24 -0600 | [diff] [blame] | 806 | goto err_rmgroup; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 807 | |
Shaohua Li | db350b0 | 2008-08-28 10:03:58 +0800 | [diff] [blame] | 808 | dock_station_count++; |
Alex Chiang | 50d716e | 2009-10-01 11:59:23 -0600 | [diff] [blame] | 809 | list_add(&dock_station->sibling, &dock_stations); |
Rafael J. Wysocki | 1e2380c | 2014-02-16 01:51:01 +0100 | [diff] [blame] | 810 | adev->flags.is_dock_station = true; |
| 811 | dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n", |
| 812 | dock_station_count); |
| 813 | return; |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 814 | |
Alex Chiang | 5f46c2f | 2009-10-19 15:14:24 -0600 | [diff] [blame] | 815 | err_rmgroup: |
Rafael J. Wysocki | a30c4c5 | 2013-06-30 23:50:24 +0200 | [diff] [blame] | 816 | remove_dock_dependent_devices(dock_station); |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 817 | sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group); |
Alex Chiang | 5f46c2f | 2009-10-19 15:14:24 -0600 | [diff] [blame] | 818 | err_unregister: |
Alex Chiang | 747479a | 2009-10-19 15:14:50 -0600 | [diff] [blame] | 819 | platform_device_unregister(dd); |
Toshi Kani | cd73018 | 2012-11-20 23:42:30 +0000 | [diff] [blame] | 820 | acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 821 | } |