blob: a326c7993f4f448985d070c2c7660f5e963e18f8 [file] [log] [blame]
Len Brownc8f7a622006-07-09 17:22:28 -04001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Len Brownc8f7a622006-07-09 17:22:28 -040028#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/notifier.h>
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080031#include <linux/platform_device.h>
Al Viro914e2632006-10-18 13:55:46 -040032#include <linux/jiffies.h>
Randy Dunlap62a6d7f2007-03-26 21:38:49 -080033#include <linux/stddef.h>
Toshi Kanicd730182012-11-20 23:42:30 +000034#include <linux/acpi.h>
Len Brownc8f7a622006-07-09 17:22:28 -040035#include <acpi/acpi_bus.h>
36#include <acpi/acpi_drivers.h>
37
Len Browna192a952009-07-28 16:45:54 -040038#define PREFIX "ACPI: "
39
Len Brown7cda93e2007-02-12 23:50:02 -050040#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a622006-07-09 17:22:28 -040041
Len Brownf52fd662007-02-12 22:42:12 -050042ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040043MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050044MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -040045MODULE_LICENSE("GPL");
46
Rusty Russell90ab5ee2012-01-13 09:32:20 +103047static bool immediate_undock = 1;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070048module_param(immediate_undock, bool, 0644);
49MODULE_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 Brownc8f7a622006-07-09 17:22:28 -040054static struct atomic_notifier_head dock_notifier_list;
55
Frank Seidela340af12007-12-07 13:20:42 +010056static const struct acpi_device_id dock_device_ids[] = {
57 {"LNXDOCK", 0},
58 {"", 0},
59};
60MODULE_DEVICE_TABLE(acpi, dock_device_ids);
61
Len Brownc8f7a622006-07-09 17:22:28 -040062struct dock_station {
63 acpi_handle handle;
64 unsigned long last_dock_time;
65 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040066 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080067
Alex Chiang50d716e2009-10-01 11:59:23 -060068 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080069 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040070};
Shaohua Lidb350b02008-08-28 10:03:58 +080071static LIST_HEAD(dock_stations);
72static int dock_station_count;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020073static DEFINE_MUTEX(hotplug_lock);
Len Brownc8f7a622006-07-09 17:22:28 -040074
75struct dock_dependent_device {
76 struct list_head list;
Len Brownc8f7a622006-07-09 17:22:28 -040077 acpi_handle handle;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020078 const struct acpi_dock_ops *hp_ops;
79 void *hp_context;
80 unsigned int hp_refcount;
81 void (*hp_release)(void *);
Len Brownc8f7a622006-07-09 17:22:28 -040082};
83
84#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070085#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080086#define DOCK_IS_DOCK 0x00000010
87#define DOCK_IS_ATA 0x00000020
88#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070089#define DOCK_EVENT 3
90#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040091
Len Brownc8f7a622006-07-09 17:22:28 -040092/*****************************************************************************
93 * Dock Dependent device functions *
94 *****************************************************************************/
95/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060096 * add_dock_dependent_device - associate a device with the dock station
97 * @ds: The dock station
98 * @handle: handle of the dependent device
Len Brownc8f7a622006-07-09 17:22:28 -040099 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600100 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -0400101 */
Jiang Liud423c082013-06-29 00:24:36 +0800102static int __init
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600103add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400104{
105 struct dock_dependent_device *dd;
106
107 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600108 if (!dd)
109 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -0400110
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600111 dd->handle = handle;
112 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -0400113 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600114
115 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400116}
117
118/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200119 * dock_init_hotplug - Initialize a hotplug device on a docking station.
120 * @dd: Dock-dependent device.
121 * @ops: Dock operations to attach to the dependent device.
122 * @context: Data to pass to the @ops callbacks and @release.
123 * @init: Optional initialization routine to run after setting up context.
124 * @release: Optional release routine to run on removal.
Len Brownc8f7a622006-07-09 17:22:28 -0400125 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200126static int dock_init_hotplug(struct dock_dependent_device *dd,
127 const struct acpi_dock_ops *ops, void *context,
128 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400129{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200130 int ret = 0;
131
132 mutex_lock(&hotplug_lock);
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200133 if (WARN_ON(dd->hp_context)) {
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200134 ret = -EEXIST;
135 } else {
136 dd->hp_refcount = 1;
137 dd->hp_ops = ops;
138 dd->hp_context = context;
139 dd->hp_release = release;
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200140 if (init)
141 init(context);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200142 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200143 mutex_unlock(&hotplug_lock);
144 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400145}
146
147/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200148 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
149 * @dd: Dock-dependent device.
Len Brownc8f7a622006-07-09 17:22:28 -0400150 *
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200151 * Decrement the reference counter of @dd and if 0, detach its hotplug
152 * operations from it, reset its context pointer and run the optional release
153 * routine if present.
Len Brownc8f7a622006-07-09 17:22:28 -0400154 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200155static void dock_release_hotplug(struct dock_dependent_device *dd)
Len Brownc8f7a622006-07-09 17:22:28 -0400156{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200157 mutex_lock(&hotplug_lock);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200158 if (dd->hp_context && !--dd->hp_refcount) {
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200159 void (*release)(void *) = dd->hp_release;
160 void *context = dd->hp_context;
161
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200162 dd->hp_ops = NULL;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200163 dd->hp_context = NULL;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200164 dd->hp_release = NULL;
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200165 if (release)
166 release(context);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200167 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200168 mutex_unlock(&hotplug_lock);
169}
170
171static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
172 bool uevent)
173{
174 acpi_notify_handler cb = NULL;
175 bool run = false;
176
177 mutex_lock(&hotplug_lock);
178
179 if (dd->hp_context) {
180 run = true;
181 dd->hp_refcount++;
182 if (dd->hp_ops)
183 cb = uevent ? dd->hp_ops->uevent : dd->hp_ops->handler;
184 }
185
186 mutex_unlock(&hotplug_lock);
187
188 if (!run)
189 return;
190
191 if (cb)
192 cb(dd->handle, event, dd->hp_context);
193
194 dock_release_hotplug(dd);
Len Brownc8f7a622006-07-09 17:22:28 -0400195}
196
197/**
198 * find_dock_dependent_device - get a device dependent on this dock
199 * @ds: the dock station
200 * @handle: the acpi_handle of the device we want
201 *
202 * iterate over the dependent device list for this dock. If the
203 * dependent device matches the handle, return.
204 */
205static struct dock_dependent_device *
206find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
207{
208 struct dock_dependent_device *dd;
209
Jiang Liued633e72013-06-29 00:24:35 +0800210 list_for_each_entry(dd, &ds->dependent_devices, list)
211 if (handle == dd->handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400212 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800213
Len Brownc8f7a622006-07-09 17:22:28 -0400214 return NULL;
215}
216
217/*****************************************************************************
218 * Dock functions *
219 *****************************************************************************/
Jiang Liud423c082013-06-29 00:24:36 +0800220static int __init is_battery(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800221{
222 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800223 int ret = 1;
224
Bob Moore15b8dd52009-06-29 13:39:29 +0800225 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800226 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800227 if (!(info->valid & ACPI_VALID_HID))
228 ret = 0;
229 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800230 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800231
Bob Moore15b8dd52009-06-29 13:39:29 +0800232 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800233 return ret;
234}
235
Jiang Liuc9b54712013-06-29 00:24:42 +0800236/* Check whether ACPI object is an ejectable battery or disk bay */
237static bool __init is_ejectable_bay(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800238{
Jiang Liuc9b54712013-06-29 00:24:42 +0800239 if (acpi_has_method(handle, "_EJ0") && is_battery(handle))
240 return true;
Alex Chiang747479a2009-10-19 15:14:50 -0600241
Jiang Liuc9b54712013-06-29 00:24:42 +0800242 return acpi_bay_match(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800243}
244
Len Brownc8f7a622006-07-09 17:22:28 -0400245/**
246 * is_dock_device - see if a device is on a dock station
247 * @handle: acpi handle of the device
248 *
249 * If this device is either the dock station itself,
250 * or is a device dependent on the dock station, then it
251 * is a dock device
252 */
253int is_dock_device(acpi_handle handle)
254{
Shaohua Lidb350b02008-08-28 10:03:58 +0800255 struct dock_station *dock_station;
256
257 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400258 return 0;
259
Jiang Liuc9b54712013-06-29 00:24:42 +0800260 if (acpi_dock_match(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400261 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600262
263 list_for_each_entry(dock_station, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +0800264 if (find_dock_dependent_device(dock_station, handle))
265 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400266
267 return 0;
268}
Len Brownc8f7a622006-07-09 17:22:28 -0400269EXPORT_SYMBOL_GPL(is_dock_device);
270
271/**
272 * dock_present - see if the dock station is present.
273 * @ds: the dock station
274 *
275 * execute the _STA method. note that present does not
276 * imply that we are docked.
277 */
278static int dock_present(struct dock_station *ds)
279{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400280 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400281 acpi_status status;
282
283 if (ds) {
284 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
285 if (ACPI_SUCCESS(status) && sta)
286 return 1;
287 }
288 return 0;
289}
290
Len Brownc8f7a622006-07-09 17:22:28 -0400291/**
292 * dock_create_acpi_device - add new devices to acpi
293 * @handle - handle of the device to add
294 *
295 * This function will create a new acpi_device for the given
296 * handle if one does not exist already. This should cause
297 * acpi to scan for drivers for the given devices, and call
298 * matching driver's add routine.
Len Brownc8f7a622006-07-09 17:22:28 -0400299 */
Jiang Liu472d9632013-06-29 00:24:37 +0800300static void dock_create_acpi_device(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400301{
Alex Chiang747479a2009-10-19 15:14:50 -0600302 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400303 int ret;
304
305 if (acpi_bus_get_device(handle, &device)) {
306 /*
307 * no device created for this object,
308 * so we should create one.
309 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100310 ret = acpi_bus_scan(handle);
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100311 if (ret)
Alex Chiang747479a2009-10-19 15:14:50 -0600312 pr_debug("error adding bus, %x\n", -ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400313 }
Len Brownc8f7a622006-07-09 17:22:28 -0400314}
315
316/**
317 * dock_remove_acpi_device - remove the acpi_device struct from acpi
318 * @handle - the handle of the device to remove
319 *
320 * Tell acpi to remove the acpi_device. This should cause any loaded
321 * driver to have it's remove routine called.
322 */
323static void dock_remove_acpi_device(acpi_handle handle)
324{
325 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400326
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100327 if (!acpi_bus_get_device(handle, &device))
328 acpi_bus_trim(device);
Len Brownc8f7a622006-07-09 17:22:28 -0400329}
330
Len Brownc8f7a622006-07-09 17:22:28 -0400331/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200332 * hot_remove_dock_devices - Remove dock station devices.
333 * @ds: Dock station.
334 */
335static void hot_remove_dock_devices(struct dock_station *ds)
336{
337 struct dock_dependent_device *dd;
338
339 /*
340 * Walk the list in reverse order so that devices that have been added
341 * last are removed first (in case there are some indirect dependencies
342 * between them).
343 */
344 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
345 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
346
347 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
348 dock_remove_acpi_device(dd->handle);
349}
350
351/**
352 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a622006-07-09 17:22:28 -0400353 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200354 * @event: either bus check or device check request
Len Brownc8f7a622006-07-09 17:22:28 -0400355 *
356 * Some devices on the dock station need to have drivers called
357 * to perform hotplug operations after a dock event has occurred.
358 * Traverse the list of dock devices that have registered a
359 * hotplug handler, and call the handler.
360 */
361static void hotplug_dock_devices(struct dock_station *ds, u32 event)
362{
363 struct dock_dependent_device *dd;
364
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200365 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200366 list_for_each_entry(dd, &ds->dependent_devices, list)
367 dock_hotplug_event(dd, event, false);
Len Brownc8f7a622006-07-09 17:22:28 -0400368
369 /*
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200370 * Now make sure that an acpi_device is created for each dependent
371 * device. That will cause scan handlers to be attached to device
372 * objects or acpi_drivers to be stopped/started if they are present.
Len Brownc8f7a622006-07-09 17:22:28 -0400373 */
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200374 list_for_each_entry(dd, &ds->dependent_devices, list)
375 dock_create_acpi_device(dd->handle);
Len Brownc8f7a622006-07-09 17:22:28 -0400376}
377
378static void dock_event(struct dock_station *ds, u32 event, int num)
379{
Shaohua Lidb350b02008-08-28 10:03:58 +0800380 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700381 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700382 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800383 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700384
385 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700386 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700387 else
Holger Macht66b56822007-08-10 13:10:32 -0700388 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700389
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700390 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800391 * Indicate that the status of the dock station has
392 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700393 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800394 if (num == DOCK_EVENT)
395 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
396
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200397 list_for_each_entry(dd, &ds->dependent_devices, list)
398 dock_hotplug_event(dd, event, true);
Alex Chiang747479a2009-10-19 15:14:50 -0600399
Shaohua Li1253f7a2008-08-28 10:06:16 +0800400 if (num != DOCK_EVENT)
401 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400402}
403
404/**
Len Brownc8f7a622006-07-09 17:22:28 -0400405 * handle_dock - handle a dock event
406 * @ds: the dock station
407 * @dock: to dock, or undock - that is the question
408 *
409 * Execute the _DCK method in response to an acpi event
410 */
411static void handle_dock(struct dock_station *ds, int dock)
412{
413 acpi_status status;
414 struct acpi_object_list arg_list;
415 union acpi_object arg;
416 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400417
Toshi Kanicd730182012-11-20 23:42:30 +0000418 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400419
420 /* _DCK method has one argument */
421 arg_list.count = 1;
422 arg_list.pointer = &arg;
423 arg.type = ACPI_TYPE_INTEGER;
424 arg.integer.value = dock;
425 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800426 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000427 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
428 status);
Thomas Renninger0a918a92008-10-11 00:15:04 -0400429
Len Brownc8f7a622006-07-09 17:22:28 -0400430 kfree(buffer.pointer);
Len Brownc8f7a622006-07-09 17:22:28 -0400431}
432
433static inline void dock(struct dock_station *ds)
434{
435 handle_dock(ds, 1);
436}
437
438static inline void undock(struct dock_station *ds)
439{
440 handle_dock(ds, 0);
441}
442
443static inline void begin_dock(struct dock_station *ds)
444{
445 ds->flags |= DOCK_DOCKING;
446}
447
448static inline void complete_dock(struct dock_station *ds)
449{
450 ds->flags &= ~(DOCK_DOCKING);
451 ds->last_dock_time = jiffies;
452}
453
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700454static inline void begin_undock(struct dock_station *ds)
455{
456 ds->flags |= DOCK_UNDOCKING;
457}
458
459static inline void complete_undock(struct dock_station *ds)
460{
461 ds->flags &= ~(DOCK_UNDOCKING);
462}
463
Len Brownc8f7a622006-07-09 17:22:28 -0400464/**
465 * dock_in_progress - see if we are in the middle of handling a dock event
466 * @ds: the dock station
467 *
468 * Sometimes while docking, false dock events can be sent to the driver
469 * because good connections aren't made or some other reason. Ignore these
470 * if we are in the middle of doing something.
471 */
472static int dock_in_progress(struct dock_station *ds)
473{
474 if ((ds->flags & DOCK_DOCKING) ||
475 time_before(jiffies, (ds->last_dock_time + HZ)))
476 return 1;
477 return 0;
478}
479
480/**
481 * register_dock_notifier - add yourself to the dock notifier list
482 * @nb: the callers notifier block
483 *
484 * If a driver wishes to be notified about dock events, they can
485 * use this function to put a notifier block on the dock notifier list.
486 * this notifier call chain will be called after a dock event, but
487 * before hotplugging any new devices.
488 */
489int register_dock_notifier(struct notifier_block *nb)
490{
Shaohua Lidb350b02008-08-28 10:03:58 +0800491 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800492 return -ENODEV;
493
Len Brownc8f7a622006-07-09 17:22:28 -0400494 return atomic_notifier_chain_register(&dock_notifier_list, nb);
495}
Len Brownc8f7a622006-07-09 17:22:28 -0400496EXPORT_SYMBOL_GPL(register_dock_notifier);
497
498/**
499 * unregister_dock_notifier - remove yourself from the dock notifier list
500 * @nb: the callers notifier block
501 */
502void unregister_dock_notifier(struct notifier_block *nb)
503{
Shaohua Lidb350b02008-08-28 10:03:58 +0800504 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800505 return;
506
Len Brownc8f7a622006-07-09 17:22:28 -0400507 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
508}
Len Brownc8f7a622006-07-09 17:22:28 -0400509EXPORT_SYMBOL_GPL(unregister_dock_notifier);
510
511/**
512 * register_hotplug_dock_device - register a hotplug function
513 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800514 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400515 * @context: device specific data
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200516 * @init: Optional initialization routine to run after registration
517 * @release: Optional release routine to run on unregistration
Len Brownc8f7a622006-07-09 17:22:28 -0400518 *
519 * If a driver would like to perform a hotplug operation after a dock
520 * event, they can register an acpi_notifiy_handler to be called by
521 * the dock driver after _DCK is executed.
522 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200523int register_hotplug_dock_device(acpi_handle handle,
524 const struct acpi_dock_ops *ops, void *context,
525 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400526{
527 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800528 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800529 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400530
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200531 if (WARN_ON(!context))
532 return -EINVAL;
533
Shaohua Lidb350b02008-08-28 10:03:58 +0800534 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400535 return -ENODEV;
536
537 /*
538 * make sure this handle is for a device dependent on the dock,
539 * this would include the dock station itself
540 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600541 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800542 /*
543 * An ATA bay can be in a dock and itself can be ejected
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800544 * separately, so there are two 'dock stations' which need the
Shaohua Li61b83692008-08-28 10:07:14 +0800545 * ops
546 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800547 dd = find_dock_dependent_device(dock_station, handle);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200548 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
Shaohua Li61b83692008-08-28 10:07:14 +0800549 ret = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400550 }
551
Shaohua Li61b83692008-08-28 10:07:14 +0800552 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400553}
Len Brownc8f7a622006-07-09 17:22:28 -0400554EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
555
556/**
557 * unregister_hotplug_dock_device - remove yourself from the hotplug list
558 * @handle: the acpi handle of the device
559 */
560void unregister_hotplug_dock_device(acpi_handle handle)
561{
562 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800563 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400564
Shaohua Lidb350b02008-08-28 10:03:58 +0800565 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400566 return;
567
Alex Chiang50d716e2009-10-01 11:59:23 -0600568 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800569 dd = find_dock_dependent_device(dock_station, handle);
570 if (dd)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200571 dock_release_hotplug(dd);
Shaohua Lidb350b02008-08-28 10:03:58 +0800572 }
Len Brownc8f7a622006-07-09 17:22:28 -0400573}
Len Brownc8f7a622006-07-09 17:22:28 -0400574EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
575
576/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800577 * handle_eject_request - handle an undock request checking for error conditions
578 *
579 * Check to make sure the dock device is still present, then undock and
580 * hotremove all the devices that may need removing.
581 */
582static int handle_eject_request(struct dock_station *ds, u32 event)
583{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800584 if (dock_in_progress(ds))
585 return -EBUSY;
586
587 /*
588 * here we need to generate the undock
589 * event prior to actually doing the undock
590 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200591 * Also, even send the dock event if the
592 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800593 */
594 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200595
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200596 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800597 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800598 acpi_evaluate_lck(ds->handle, 0);
599 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800600 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000601 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800602 return -EBUSY;
603 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700604 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800605 return 0;
606}
607
608/**
Len Brownc8f7a622006-07-09 17:22:28 -0400609 * dock_notify - act upon an acpi dock notification
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200610 * @ds: dock station
Len Brownc8f7a622006-07-09 17:22:28 -0400611 * @event: the acpi event
Len Brownc8f7a622006-07-09 17:22:28 -0400612 *
613 * If we are notified to dock, then check to see if the dock is
614 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800615 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400616 */
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200617static void dock_notify(struct dock_station *ds, u32 event)
Len Brownc8f7a622006-07-09 17:22:28 -0400618{
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200619 acpi_handle handle = ds->handle;
620 struct acpi_device *ad;
Shaohua Lidb350b02008-08-28 10:03:58 +0800621 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400622
Shaohua Lidb350b02008-08-28 10:03:58 +0800623 /*
624 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
625 * is sent and _DCK is present, it is assumed to mean an undock
626 * request.
627 */
628 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
629 event = ACPI_NOTIFY_EJECT_REQUEST;
630
631 /*
632 * dock station: BUS_CHECK - docked or surprise removal
633 * DEVICE_CHECK - undocked
634 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
635 *
636 * To simplify event handling, dock dependent device handler always
637 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
638 * ACPI_NOTIFY_EJECT_REQUEST for removal
639 */
Len Brownc8f7a622006-07-09 17:22:28 -0400640 switch (event) {
641 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800642 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200643 if (!dock_in_progress(ds) && acpi_bus_get_device(handle, &ad)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400644 begin_dock(ds);
645 dock(ds);
646 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000647 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800648 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400649 break;
650 }
651 atomic_notifier_call_chain(&dock_notifier_list,
652 event, NULL);
653 hotplug_dock_devices(ds, event);
654 complete_dock(ds);
655 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800656 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800657 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800658 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400659 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800660 if (dock_present(ds) || dock_in_progress(ds))
661 break;
662 /* This is a surprise removal */
663 surprise_removal = 1;
664 event = ACPI_NOTIFY_EJECT_REQUEST;
665 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400666 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700667 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800668 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
669 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700670 handle_eject_request(ds, event);
671 else
672 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400673 break;
674 default:
Toshi Kanicd730182012-11-20 23:42:30 +0000675 acpi_handle_err(handle, "Unknown dock event %d\n", event);
Len Brownc8f7a622006-07-09 17:22:28 -0400676 }
677}
678
Zhang Rui19cd8472008-08-28 10:05:06 +0800679struct dock_data {
Zhang Rui19cd8472008-08-28 10:05:06 +0800680 struct dock_station *ds;
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200681 u32 event;
Zhang Rui19cd8472008-08-28 10:05:06 +0800682};
683
684static void acpi_dock_deferred_cb(void *context)
685{
Alex Chiang747479a2009-10-19 15:14:50 -0600686 struct dock_data *data = context;
Zhang Rui19cd8472008-08-28 10:05:06 +0800687
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100688 acpi_scan_lock_acquire();
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200689 dock_notify(data->ds, data->event);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100690 acpi_scan_lock_release();
Zhang Rui19cd8472008-08-28 10:05:06 +0800691 kfree(data);
692}
693
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200694static void dock_notify_handler(acpi_handle handle, u32 event, void *data)
Shaohua Li6bd00a62008-08-28 10:04:29 +0800695{
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200696 struct dock_data *dd;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800697
698 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
699 && event != ACPI_NOTIFY_EJECT_REQUEST)
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200700 return;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100701
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200702 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
703 if (dd) {
704 acpi_status status;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100705
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200706 dd->ds = data;
707 dd->event = event;
708 status = acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
709 if (ACPI_FAILURE(status))
710 kfree(dd);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800711 }
Shaohua Li6bd00a62008-08-28 10:04:29 +0800712}
713
Len Brownc8f7a622006-07-09 17:22:28 -0400714/**
715 * find_dock_devices - find devices on the dock station
716 * @handle: the handle of the device we are examining
717 * @lvl: unused
718 * @context: the dock station private data
719 * @rv: unused
720 *
721 * This function is called by acpi_walk_namespace. It will
722 * check to see if an object has an _EJD method. If it does, then it
723 * will see if it is dependent on the dock station.
724 */
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200725static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
726 void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400727{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200728 struct dock_station *ds = context;
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200729 acpi_handle ejd = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -0400730
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200731 acpi_bus_get_ejd(handle, &ejd);
732 if (ejd == ds->handle)
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600733 add_dock_dependent_device(ds, handle);
734
Len Brownc8f7a622006-07-09 17:22:28 -0400735 return AE_OK;
736}
737
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800738/*
739 * show_docked - read method for "docked" file in sysfs
740 */
741static ssize_t show_docked(struct device *dev,
742 struct device_attribute *attr, char *buf)
743{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100744 struct acpi_device *tmp;
745
Alex Chiangfe06fba2009-10-19 15:14:45 -0600746 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800747
Yasuaki Ishimatsu02df7342013-01-31 03:23:53 +0000748 if (!acpi_bus_get_device(dock_station->handle, &tmp))
Holger Machtfc5a9f82009-01-20 12:18:24 +0100749 return snprintf(buf, PAGE_SIZE, "1\n");
750 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800751}
Adrian Bunke5685b92007-10-24 18:24:42 +0200752static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800753
754/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700755 * show_flags - read method for flags file in sysfs
756 */
757static ssize_t show_flags(struct device *dev,
758 struct device_attribute *attr, char *buf)
759{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600760 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700761 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
762
763}
Adrian Bunke5685b92007-10-24 18:24:42 +0200764static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700765
766/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800767 * write_undock - write method for "undock" file in sysfs
768 */
769static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count)
771{
772 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600773 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800774
775 if (!count)
776 return -EINVAL;
777
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200778 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100779 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800780 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200781 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800782 return ret ? ret: count;
783}
Adrian Bunke5685b92007-10-24 18:24:42 +0200784static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800785
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800786/*
787 * show_dock_uid - read method for "uid" file in sysfs
788 */
789static ssize_t show_dock_uid(struct device *dev,
790 struct device_attribute *attr, char *buf)
791{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400792 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600793 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700794 acpi_status status = acpi_evaluate_integer(dock_station->handle,
795 "_UID", NULL, &lbuf);
796 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800797 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700798
Matthew Wilcox27663c52008-10-10 02:22:59 -0400799 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800800}
Adrian Bunke5685b92007-10-24 18:24:42 +0200801static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800802
Shaohua Li8652b002008-08-28 10:07:45 +0800803static ssize_t show_dock_type(struct device *dev,
804 struct device_attribute *attr, char *buf)
805{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600806 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800807 char *type;
808
809 if (dock_station->flags & DOCK_IS_DOCK)
810 type = "dock_station";
811 else if (dock_station->flags & DOCK_IS_ATA)
812 type = "ata_bay";
813 else if (dock_station->flags & DOCK_IS_BAT)
814 type = "battery_bay";
815 else
816 type = "unknown";
817
818 return snprintf(buf, PAGE_SIZE, "%s\n", type);
819}
820static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
821
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600822static struct attribute *dock_attributes[] = {
823 &dev_attr_docked.attr,
824 &dev_attr_flags.attr,
825 &dev_attr_undock.attr,
826 &dev_attr_uid.attr,
827 &dev_attr_type.attr,
828 NULL
829};
830
831static struct attribute_group dock_attribute_group = {
832 .attrs = dock_attributes
833};
834
Len Brownc8f7a622006-07-09 17:22:28 -0400835/**
836 * dock_add - add a new dock station
837 * @handle: the dock station handle
838 *
839 * allocated and initialize a new dock station device. Find all devices
840 * that are on the dock station, and register for dock event notifications.
841 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200842static int __init dock_add(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400843{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600844 int ret, id;
845 struct dock_station ds, *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600846 struct platform_device *dd;
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200847 acpi_status status;
Len Brownc8f7a622006-07-09 17:22:28 -0400848
Alex Chiangfe06fba2009-10-19 15:14:45 -0600849 id = dock_station_count;
Alex Chiang49c6fb22010-02-01 10:35:18 -0700850 memset(&ds, 0, sizeof(ds));
Alex Chiang747479a2009-10-19 15:14:50 -0600851 dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
852 if (IS_ERR(dd))
853 return PTR_ERR(dd);
Alex Chiang9751cb72009-10-19 15:14:40 -0600854
Alex Chiang747479a2009-10-19 15:14:50 -0600855 dock_station = dd->dev.platform_data;
856
Len Brownc8f7a622006-07-09 17:22:28 -0400857 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600858 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400859 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400860
Alex Chiang747479a2009-10-19 15:14:50 -0600861 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600862 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700863
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700864 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600865 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700866
Jiang Liuc9b54712013-06-29 00:24:42 +0800867 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800868 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800869 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800870 dock_station->flags |= DOCK_IS_ATA;
871 if (is_battery(handle))
872 dock_station->flags |= DOCK_IS_BAT;
873
Alex Chiang747479a2009-10-19 15:14:50 -0600874 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800875 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600876 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800877
Len Brownc8f7a622006-07-09 17:22:28 -0400878 /* Find dependent devices */
879 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +0800880 ACPI_UINT32_MAX, find_dock_devices, NULL,
881 dock_station, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400882
883 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600884 ret = add_dock_dependent_device(dock_station, handle);
885 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600886 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400887
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200888 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
889 dock_notify_handler, dock_station);
890 if (ACPI_FAILURE(status))
891 goto err_rmgroup;
892
Shaohua Lidb350b02008-08-28 10:03:58 +0800893 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600894 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -0400895 return 0;
896
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600897err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600898 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600899err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600900 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000901 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400902 return ret;
903}
904
905/**
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200906 * find_dock_and_bay - look for dock stations and bays
Len Brownc8f7a622006-07-09 17:22:28 -0400907 * @handle: acpi handle of a device
908 * @lvl: unused
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200909 * @context: unused
Len Brownc8f7a622006-07-09 17:22:28 -0400910 * @rv: unused
911 *
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200912 * This is called by acpi_walk_namespace to look for dock stations and bays.
Len Brownc8f7a622006-07-09 17:22:28 -0400913 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200914static __init acpi_status
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200915find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400916{
Jiang Liuc9b54712013-06-29 00:24:42 +0800917 if (acpi_dock_match(handle) || is_ejectable_bay(handle))
Zhang Rui1ee4d612010-03-22 15:46:49 +0800918 dock_add(handle);
Alex Chiang747479a2009-10-19 15:14:50 -0600919
Zhang Rui1ee4d612010-03-22 15:46:49 +0800920 return AE_OK;
Len Brownc8f7a622006-07-09 17:22:28 -0400921}
922
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200923void __init acpi_dock_init(void)
Len Brownc8f7a622006-07-09 17:22:28 -0400924{
Len Brown816c2ed2008-06-24 22:57:12 -0400925 if (acpi_disabled)
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200926 return;
Len Brown816c2ed2008-06-24 22:57:12 -0400927
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200928 /* look for dock stations and bays */
Len Brownc8f7a622006-07-09 17:22:28 -0400929 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200930 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400931
Shaohua Lidb350b02008-08-28 10:03:58 +0800932 if (!dock_station_count) {
Toshi Kanicd730182012-11-20 23:42:30 +0000933 pr_info(PREFIX "No dock devices found.\n");
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200934 return;
Shaohua Lidb350b02008-08-28 10:03:58 +0800935 }
Len Brownc8f7a622006-07-09 17:22:28 -0400936
Jiang Liuf22ff552013-06-29 00:24:34 +0800937 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Toshi Kanicd730182012-11-20 23:42:30 +0000938 pr_info(PREFIX "%s: %d docks/bays found\n",
Shaohua Lidb350b02008-08-28 10:03:58 +0800939 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -0400940}