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