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