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