blob: c2090c0231024365bd0cad3f1a019ac65d22e8f7 [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
Len Browna192a952009-07-28 16:45:54 -040036#define PREFIX "ACPI: "
37
Len Brown7cda93e2007-02-12 23:50:02 -050038#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a622006-07-09 17:22:28 -040039
Len Brownf52fd662007-02-12 22:42:12 -050040ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040041MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050042MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -040043MODULE_LICENSE("GPL");
44
Rusty Russell90ab5ee2012-01-13 09:32:20 +103045static bool immediate_undock = 1;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070046module_param(immediate_undock, bool, 0644);
47MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
48 "undock immediately when the undock button is pressed, 0 will cause"
49 " the driver to wait for userspace to write the undock sysfs file "
50 " before undocking");
51
Frank Seidela340af12007-12-07 13:20:42 +010052static const struct acpi_device_id dock_device_ids[] = {
53 {"LNXDOCK", 0},
54 {"", 0},
55};
56MODULE_DEVICE_TABLE(acpi, dock_device_ids);
57
Len Brownc8f7a622006-07-09 17:22:28 -040058struct dock_station {
59 acpi_handle handle;
60 unsigned long last_dock_time;
61 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040062 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080063
Alex Chiang50d716e2009-10-01 11:59:23 -060064 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080065 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040066};
Shaohua Lidb350b02008-08-28 10:03:58 +080067static LIST_HEAD(dock_stations);
68static int dock_station_count;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020069static DEFINE_MUTEX(hotplug_lock);
Len Brownc8f7a622006-07-09 17:22:28 -040070
71struct dock_dependent_device {
72 struct list_head list;
Len Brownc8f7a622006-07-09 17:22:28 -040073 acpi_handle handle;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020074 const struct acpi_dock_ops *hp_ops;
75 void *hp_context;
76 unsigned int hp_refcount;
77 void (*hp_release)(void *);
Len Brownc8f7a622006-07-09 17:22:28 -040078};
79
80#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070081#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080082#define DOCK_IS_DOCK 0x00000010
83#define DOCK_IS_ATA 0x00000020
84#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070085#define DOCK_EVENT 3
86#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040087
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020088enum dock_callback_type {
89 DOCK_CALL_HANDLER,
90 DOCK_CALL_FIXUP,
91 DOCK_CALL_UEVENT,
92};
93
Len Brownc8f7a622006-07-09 17:22:28 -040094/*****************************************************************************
95 * Dock Dependent device functions *
96 *****************************************************************************/
97/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060098 * add_dock_dependent_device - associate a device with the dock station
99 * @ds: The dock station
100 * @handle: handle of the dependent device
Len Brownc8f7a622006-07-09 17:22:28 -0400101 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600102 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -0400103 */
Jiang Liud423c082013-06-29 00:24:36 +0800104static int __init
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600105add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400106{
107 struct dock_dependent_device *dd;
108
109 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600110 if (!dd)
111 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -0400112
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600113 dd->handle = handle;
114 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -0400115 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600116
117 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400118}
119
Rafael J. Wysockia30c4c52013-06-30 23:50:24 +0200120static void remove_dock_dependent_devices(struct dock_station *ds)
121{
122 struct dock_dependent_device *dd, *aux;
123
124 list_for_each_entry_safe(dd, aux, &ds->dependent_devices, list) {
125 list_del(&dd->list);
126 kfree(dd);
127 }
128}
129
Len Brownc8f7a622006-07-09 17:22:28 -0400130/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200131 * dock_init_hotplug - Initialize a hotplug device on a docking station.
132 * @dd: Dock-dependent device.
133 * @ops: Dock operations to attach to the dependent device.
134 * @context: Data to pass to the @ops callbacks and @release.
135 * @init: Optional initialization routine to run after setting up context.
136 * @release: Optional release routine to run on removal.
Len Brownc8f7a622006-07-09 17:22:28 -0400137 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200138static int dock_init_hotplug(struct dock_dependent_device *dd,
139 const struct acpi_dock_ops *ops, void *context,
140 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400141{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200142 int ret = 0;
143
144 mutex_lock(&hotplug_lock);
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200145 if (WARN_ON(dd->hp_context)) {
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200146 ret = -EEXIST;
147 } else {
148 dd->hp_refcount = 1;
149 dd->hp_ops = ops;
150 dd->hp_context = context;
151 dd->hp_release = release;
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200152 if (init)
153 init(context);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200154 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200155 mutex_unlock(&hotplug_lock);
156 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400157}
158
159/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200160 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
161 * @dd: Dock-dependent device.
Len Brownc8f7a622006-07-09 17:22:28 -0400162 *
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200163 * Decrement the reference counter of @dd and if 0, detach its hotplug
164 * operations from it, reset its context pointer and run the optional release
165 * routine if present.
Len Brownc8f7a622006-07-09 17:22:28 -0400166 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200167static void dock_release_hotplug(struct dock_dependent_device *dd)
Len Brownc8f7a622006-07-09 17:22:28 -0400168{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200169 mutex_lock(&hotplug_lock);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200170 if (dd->hp_context && !--dd->hp_refcount) {
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200171 void (*release)(void *) = dd->hp_release;
172 void *context = dd->hp_context;
173
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200174 dd->hp_ops = NULL;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200175 dd->hp_context = NULL;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200176 dd->hp_release = NULL;
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200177 if (release)
178 release(context);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200179 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200180 mutex_unlock(&hotplug_lock);
181}
182
183static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200184 enum dock_callback_type cb_type)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200185{
186 acpi_notify_handler cb = NULL;
187 bool run = false;
188
189 mutex_lock(&hotplug_lock);
190
191 if (dd->hp_context) {
192 run = true;
193 dd->hp_refcount++;
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200194 if (dd->hp_ops) {
195 switch (cb_type) {
196 case DOCK_CALL_FIXUP:
197 cb = dd->hp_ops->fixup;
198 break;
199 case DOCK_CALL_UEVENT:
200 cb = dd->hp_ops->uevent;
201 break;
202 default:
203 cb = dd->hp_ops->handler;
204 }
205 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200206 }
207
208 mutex_unlock(&hotplug_lock);
209
210 if (!run)
211 return;
212
213 if (cb)
214 cb(dd->handle, event, dd->hp_context);
215
216 dock_release_hotplug(dd);
Len Brownc8f7a622006-07-09 17:22:28 -0400217}
218
219/**
220 * find_dock_dependent_device - get a device dependent on this dock
221 * @ds: the dock station
222 * @handle: the acpi_handle of the device we want
223 *
224 * iterate over the dependent device list for this dock. If the
225 * dependent device matches the handle, return.
226 */
227static struct dock_dependent_device *
228find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
229{
230 struct dock_dependent_device *dd;
231
Jiang Liued633e72013-06-29 00:24:35 +0800232 list_for_each_entry(dd, &ds->dependent_devices, list)
233 if (handle == dd->handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400234 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800235
Len Brownc8f7a622006-07-09 17:22:28 -0400236 return NULL;
237}
238
239/*****************************************************************************
240 * Dock functions *
241 *****************************************************************************/
Jiang Liud423c082013-06-29 00:24:36 +0800242static int __init is_battery(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800243{
244 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800245 int ret = 1;
246
Bob Moore15b8dd52009-06-29 13:39:29 +0800247 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800248 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800249 if (!(info->valid & ACPI_VALID_HID))
250 ret = 0;
251 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800252 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800253
Bob Moore15b8dd52009-06-29 13:39:29 +0800254 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800255 return ret;
256}
257
Jiang Liuc9b54712013-06-29 00:24:42 +0800258/* Check whether ACPI object is an ejectable battery or disk bay */
259static bool __init is_ejectable_bay(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800260{
Jiang Liuc9b54712013-06-29 00:24:42 +0800261 if (acpi_has_method(handle, "_EJ0") && is_battery(handle))
262 return true;
Alex Chiang747479a2009-10-19 15:14:50 -0600263
Jiang Liuc9b54712013-06-29 00:24:42 +0800264 return acpi_bay_match(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800265}
266
Len Brownc8f7a622006-07-09 17:22:28 -0400267/**
268 * is_dock_device - see if a device is on a dock station
269 * @handle: acpi handle of the device
270 *
271 * If this device is either the dock station itself,
272 * or is a device dependent on the dock station, then it
273 * is a dock device
274 */
275int is_dock_device(acpi_handle handle)
276{
Shaohua Lidb350b02008-08-28 10:03:58 +0800277 struct dock_station *dock_station;
278
279 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400280 return 0;
281
Jiang Liuc9b54712013-06-29 00:24:42 +0800282 if (acpi_dock_match(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400283 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600284
285 list_for_each_entry(dock_station, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +0800286 if (find_dock_dependent_device(dock_station, handle))
287 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400288
289 return 0;
290}
Len Brownc8f7a622006-07-09 17:22:28 -0400291EXPORT_SYMBOL_GPL(is_dock_device);
292
293/**
294 * dock_present - see if the dock station is present.
295 * @ds: the dock station
296 *
297 * execute the _STA method. note that present does not
298 * imply that we are docked.
299 */
300static int dock_present(struct dock_station *ds)
301{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400302 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400303 acpi_status status;
304
305 if (ds) {
306 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
307 if (ACPI_SUCCESS(status) && sta)
308 return 1;
309 }
310 return 0;
311}
312
Len Brownc8f7a622006-07-09 17:22:28 -0400313/**
314 * dock_create_acpi_device - add new devices to acpi
315 * @handle - handle of the device to add
316 *
317 * This function will create a new acpi_device for the given
318 * handle if one does not exist already. This should cause
319 * acpi to scan for drivers for the given devices, and call
320 * matching driver's add routine.
Len Brownc8f7a622006-07-09 17:22:28 -0400321 */
Jiang Liu472d9632013-06-29 00:24:37 +0800322static void dock_create_acpi_device(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400323{
Alex Chiang747479a2009-10-19 15:14:50 -0600324 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400325 int ret;
326
327 if (acpi_bus_get_device(handle, &device)) {
328 /*
329 * no device created for this object,
330 * so we should create one.
331 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100332 ret = acpi_bus_scan(handle);
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100333 if (ret)
Alex Chiang747479a2009-10-19 15:14:50 -0600334 pr_debug("error adding bus, %x\n", -ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400335 }
Len Brownc8f7a622006-07-09 17:22:28 -0400336}
337
338/**
339 * dock_remove_acpi_device - remove the acpi_device struct from acpi
340 * @handle - the handle of the device to remove
341 *
342 * Tell acpi to remove the acpi_device. This should cause any loaded
343 * driver to have it's remove routine called.
344 */
345static void dock_remove_acpi_device(acpi_handle handle)
346{
347 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400348
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100349 if (!acpi_bus_get_device(handle, &device))
350 acpi_bus_trim(device);
Len Brownc8f7a622006-07-09 17:22:28 -0400351}
352
Len Brownc8f7a622006-07-09 17:22:28 -0400353/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200354 * hot_remove_dock_devices - Remove dock station devices.
355 * @ds: Dock station.
356 */
357static void hot_remove_dock_devices(struct dock_station *ds)
358{
359 struct dock_dependent_device *dd;
360
361 /*
362 * Walk the list in reverse order so that devices that have been added
363 * last are removed first (in case there are some indirect dependencies
364 * between them).
365 */
366 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
367 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
368
369 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
370 dock_remove_acpi_device(dd->handle);
371}
372
373/**
374 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a622006-07-09 17:22:28 -0400375 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200376 * @event: either bus check or device check request
Len Brownc8f7a622006-07-09 17:22:28 -0400377 *
378 * Some devices on the dock station need to have drivers called
379 * to perform hotplug operations after a dock event has occurred.
380 * Traverse the list of dock devices that have registered a
381 * hotplug handler, and call the handler.
382 */
383static void hotplug_dock_devices(struct dock_station *ds, u32 event)
384{
385 struct dock_dependent_device *dd;
386
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200387 /* Call driver specific post-dock fixups. */
388 list_for_each_entry(dd, &ds->dependent_devices, list)
389 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
390
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200391 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200392 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200393 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
Len Brownc8f7a622006-07-09 17:22:28 -0400394
395 /*
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200396 * Now make sure that an acpi_device is created for each dependent
397 * device. That will cause scan handlers to be attached to device
398 * objects or acpi_drivers to be stopped/started if they are present.
Len Brownc8f7a622006-07-09 17:22:28 -0400399 */
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200400 list_for_each_entry(dd, &ds->dependent_devices, list)
401 dock_create_acpi_device(dd->handle);
Len Brownc8f7a622006-07-09 17:22:28 -0400402}
403
404static void dock_event(struct dock_station *ds, u32 event, int num)
405{
Shaohua Lidb350b02008-08-28 10:03:58 +0800406 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700407 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700408 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800409 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700410
411 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700412 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700413 else
Holger Macht66b56822007-08-10 13:10:32 -0700414 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700415
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700416 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800417 * Indicate that the status of the dock station has
418 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700419 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800420 if (num == DOCK_EVENT)
421 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
422
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200423 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200424 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
Alex Chiang747479a2009-10-19 15:14:50 -0600425
Shaohua Li1253f7a2008-08-28 10:06:16 +0800426 if (num != DOCK_EVENT)
427 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400428}
429
430/**
Len Brownc8f7a622006-07-09 17:22:28 -0400431 * handle_dock - handle a dock event
432 * @ds: the dock station
433 * @dock: to dock, or undock - that is the question
434 *
435 * Execute the _DCK method in response to an acpi event
436 */
437static void handle_dock(struct dock_station *ds, int dock)
438{
439 acpi_status status;
440 struct acpi_object_list arg_list;
441 union acpi_object arg;
Zhang Rui6a868e12013-09-03 08:32:10 +0800442 unsigned long long value;
Len Brownc8f7a622006-07-09 17:22:28 -0400443
Toshi Kanicd730182012-11-20 23:42:30 +0000444 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400445
446 /* _DCK method has one argument */
447 arg_list.count = 1;
448 arg_list.pointer = &arg;
449 arg.type = ACPI_TYPE_INTEGER;
450 arg.integer.value = dock;
Zhang Rui6a868e12013-09-03 08:32:10 +0800451 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
Shaohua Lidb350b02008-08-28 10:03:58 +0800452 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000453 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
454 status);
Len Brownc8f7a622006-07-09 17:22:28 -0400455}
456
457static inline void dock(struct dock_station *ds)
458{
459 handle_dock(ds, 1);
460}
461
462static inline void undock(struct dock_station *ds)
463{
464 handle_dock(ds, 0);
465}
466
467static inline void begin_dock(struct dock_station *ds)
468{
469 ds->flags |= DOCK_DOCKING;
470}
471
472static inline void complete_dock(struct dock_station *ds)
473{
474 ds->flags &= ~(DOCK_DOCKING);
475 ds->last_dock_time = jiffies;
476}
477
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700478static inline void begin_undock(struct dock_station *ds)
479{
480 ds->flags |= DOCK_UNDOCKING;
481}
482
483static inline void complete_undock(struct dock_station *ds)
484{
485 ds->flags &= ~(DOCK_UNDOCKING);
486}
487
Len Brownc8f7a622006-07-09 17:22:28 -0400488/**
489 * dock_in_progress - see if we are in the middle of handling a dock event
490 * @ds: the dock station
491 *
492 * Sometimes while docking, false dock events can be sent to the driver
493 * because good connections aren't made or some other reason. Ignore these
494 * if we are in the middle of doing something.
495 */
496static int dock_in_progress(struct dock_station *ds)
497{
498 if ((ds->flags & DOCK_DOCKING) ||
499 time_before(jiffies, (ds->last_dock_time + HZ)))
500 return 1;
501 return 0;
502}
503
504/**
Len Brownc8f7a622006-07-09 17:22:28 -0400505 * register_hotplug_dock_device - register a hotplug function
506 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800507 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400508 * @context: device specific data
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200509 * @init: Optional initialization routine to run after registration
510 * @release: Optional release routine to run on unregistration
Len Brownc8f7a622006-07-09 17:22:28 -0400511 *
512 * If a driver would like to perform a hotplug operation after a dock
513 * event, they can register an acpi_notifiy_handler to be called by
514 * the dock driver after _DCK is executed.
515 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200516int register_hotplug_dock_device(acpi_handle handle,
517 const struct acpi_dock_ops *ops, void *context,
518 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400519{
520 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800521 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800522 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400523
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200524 if (WARN_ON(!context))
525 return -EINVAL;
526
Shaohua Lidb350b02008-08-28 10:03:58 +0800527 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400528 return -ENODEV;
529
530 /*
531 * make sure this handle is for a device dependent on the dock,
532 * this would include the dock station itself
533 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600534 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800535 /*
536 * An ATA bay can be in a dock and itself can be ejected
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800537 * separately, so there are two 'dock stations' which need the
Shaohua Li61b83692008-08-28 10:07:14 +0800538 * ops
539 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800540 dd = find_dock_dependent_device(dock_station, handle);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200541 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
Shaohua Li61b83692008-08-28 10:07:14 +0800542 ret = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400543 }
544
Shaohua Li61b83692008-08-28 10:07:14 +0800545 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400546}
Len Brownc8f7a622006-07-09 17:22:28 -0400547EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
548
549/**
550 * unregister_hotplug_dock_device - remove yourself from the hotplug list
551 * @handle: the acpi handle of the device
552 */
553void unregister_hotplug_dock_device(acpi_handle handle)
554{
555 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800556 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400557
Shaohua Lidb350b02008-08-28 10:03:58 +0800558 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400559 return;
560
Alex Chiang50d716e2009-10-01 11:59:23 -0600561 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800562 dd = find_dock_dependent_device(dock_station, handle);
563 if (dd)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200564 dock_release_hotplug(dd);
Shaohua Lidb350b02008-08-28 10:03:58 +0800565 }
Len Brownc8f7a622006-07-09 17:22:28 -0400566}
Len Brownc8f7a622006-07-09 17:22:28 -0400567EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
568
569/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800570 * handle_eject_request - handle an undock request checking for error conditions
571 *
572 * Check to make sure the dock device is still present, then undock and
573 * hotremove all the devices that may need removing.
574 */
575static int handle_eject_request(struct dock_station *ds, u32 event)
576{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800577 if (dock_in_progress(ds))
578 return -EBUSY;
579
580 /*
581 * here we need to generate the undock
582 * event prior to actually doing the undock
583 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200584 * Also, even send the dock event if the
585 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800586 */
587 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200588
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200589 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800590 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800591 acpi_evaluate_lck(ds->handle, 0);
592 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800593 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000594 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800595 return -EBUSY;
596 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700597 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800598 return 0;
599}
600
601/**
Len Brownc8f7a622006-07-09 17:22:28 -0400602 * dock_notify - act upon an acpi dock notification
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200603 * @ds: dock station
Len Brownc8f7a622006-07-09 17:22:28 -0400604 * @event: the acpi event
Len Brownc8f7a622006-07-09 17:22:28 -0400605 *
606 * If we are notified to dock, then check to see if the dock is
607 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800608 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400609 */
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200610static void dock_notify(struct dock_station *ds, u32 event)
Len Brownc8f7a622006-07-09 17:22:28 -0400611{
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200612 acpi_handle handle = ds->handle;
613 struct acpi_device *ad;
Shaohua Lidb350b02008-08-28 10:03:58 +0800614 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400615
Shaohua Lidb350b02008-08-28 10:03:58 +0800616 /*
617 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
618 * is sent and _DCK is present, it is assumed to mean an undock
619 * request.
620 */
621 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
622 event = ACPI_NOTIFY_EJECT_REQUEST;
623
624 /*
625 * dock station: BUS_CHECK - docked or surprise removal
626 * DEVICE_CHECK - undocked
627 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
628 *
629 * To simplify event handling, dock dependent device handler always
630 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
631 * ACPI_NOTIFY_EJECT_REQUEST for removal
632 */
Len Brownc8f7a622006-07-09 17:22:28 -0400633 switch (event) {
634 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800635 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200636 if (!dock_in_progress(ds) && acpi_bus_get_device(handle, &ad)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400637 begin_dock(ds);
638 dock(ds);
639 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000640 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800641 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400642 break;
643 }
Len Brownc8f7a622006-07-09 17:22:28 -0400644 hotplug_dock_devices(ds, event);
645 complete_dock(ds);
646 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800647 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800648 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800649 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400650 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800651 if (dock_present(ds) || dock_in_progress(ds))
652 break;
653 /* This is a surprise removal */
654 surprise_removal = 1;
655 event = ACPI_NOTIFY_EJECT_REQUEST;
656 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400657 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700658 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800659 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
660 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700661 handle_eject_request(ds, event);
662 else
663 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400664 break;
665 default:
Toshi Kanicd730182012-11-20 23:42:30 +0000666 acpi_handle_err(handle, "Unknown dock event %d\n", event);
Len Brownc8f7a622006-07-09 17:22:28 -0400667 }
668}
669
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100670static void acpi_dock_deferred_cb(void *data, u32 event)
Zhang Rui19cd8472008-08-28 10:05:06 +0800671{
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100672 acpi_scan_lock_acquire();
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100673 dock_notify(data, event);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100674 acpi_scan_lock_release();
Zhang Rui19cd8472008-08-28 10:05:06 +0800675}
676
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200677static void dock_notify_handler(acpi_handle handle, u32 event, void *data)
Shaohua Li6bd00a62008-08-28 10:04:29 +0800678{
Shaohua Li6bd00a62008-08-28 10:04:29 +0800679 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
680 && event != ACPI_NOTIFY_EJECT_REQUEST)
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200681 return;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100682
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100683 acpi_hotplug_execute(acpi_dock_deferred_cb, data, event);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800684}
685
Len Brownc8f7a622006-07-09 17:22:28 -0400686/**
687 * find_dock_devices - find devices on the dock station
688 * @handle: the handle of the device we are examining
689 * @lvl: unused
690 * @context: the dock station private data
691 * @rv: unused
692 *
693 * This function is called by acpi_walk_namespace. It will
694 * check to see if an object has an _EJD method. If it does, then it
695 * will see if it is dependent on the dock station.
696 */
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200697static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
698 void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400699{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200700 struct dock_station *ds = context;
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200701 acpi_handle ejd = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -0400702
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200703 acpi_bus_get_ejd(handle, &ejd);
704 if (ejd == ds->handle)
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600705 add_dock_dependent_device(ds, handle);
706
Len Brownc8f7a622006-07-09 17:22:28 -0400707 return AE_OK;
708}
709
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800710/*
711 * show_docked - read method for "docked" file in sysfs
712 */
713static ssize_t show_docked(struct device *dev,
714 struct device_attribute *attr, char *buf)
715{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100716 struct acpi_device *tmp;
717
Alex Chiangfe06fba2009-10-19 15:14:45 -0600718 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800719
Yasuaki Ishimatsu02df7342013-01-31 03:23:53 +0000720 if (!acpi_bus_get_device(dock_station->handle, &tmp))
Holger Machtfc5a9f82009-01-20 12:18:24 +0100721 return snprintf(buf, PAGE_SIZE, "1\n");
722 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800723}
Adrian Bunke5685b92007-10-24 18:24:42 +0200724static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800725
726/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700727 * show_flags - read method for flags file in sysfs
728 */
729static ssize_t show_flags(struct device *dev,
730 struct device_attribute *attr, char *buf)
731{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600732 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700733 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
734
735}
Adrian Bunke5685b92007-10-24 18:24:42 +0200736static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700737
738/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800739 * write_undock - write method for "undock" file in sysfs
740 */
741static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
742 const char *buf, size_t count)
743{
744 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600745 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800746
747 if (!count)
748 return -EINVAL;
749
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200750 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100751 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800752 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200753 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800754 return ret ? ret: count;
755}
Adrian Bunke5685b92007-10-24 18:24:42 +0200756static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800757
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800758/*
759 * show_dock_uid - read method for "uid" file in sysfs
760 */
761static ssize_t show_dock_uid(struct device *dev,
762 struct device_attribute *attr, char *buf)
763{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400764 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600765 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700766 acpi_status status = acpi_evaluate_integer(dock_station->handle,
767 "_UID", NULL, &lbuf);
768 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800769 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700770
Matthew Wilcox27663c52008-10-10 02:22:59 -0400771 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800772}
Adrian Bunke5685b92007-10-24 18:24:42 +0200773static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800774
Shaohua Li8652b002008-08-28 10:07:45 +0800775static ssize_t show_dock_type(struct device *dev,
776 struct device_attribute *attr, char *buf)
777{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600778 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800779 char *type;
780
781 if (dock_station->flags & DOCK_IS_DOCK)
782 type = "dock_station";
783 else if (dock_station->flags & DOCK_IS_ATA)
784 type = "ata_bay";
785 else if (dock_station->flags & DOCK_IS_BAT)
786 type = "battery_bay";
787 else
788 type = "unknown";
789
790 return snprintf(buf, PAGE_SIZE, "%s\n", type);
791}
792static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
793
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600794static struct attribute *dock_attributes[] = {
795 &dev_attr_docked.attr,
796 &dev_attr_flags.attr,
797 &dev_attr_undock.attr,
798 &dev_attr_uid.attr,
799 &dev_attr_type.attr,
800 NULL
801};
802
803static struct attribute_group dock_attribute_group = {
804 .attrs = dock_attributes
805};
806
Len Brownc8f7a622006-07-09 17:22:28 -0400807/**
808 * dock_add - add a new dock station
809 * @handle: the dock station handle
810 *
811 * allocated and initialize a new dock station device. Find all devices
812 * that are on the dock station, and register for dock event notifications.
813 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200814static int __init dock_add(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400815{
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200816 struct dock_station *dock_station, ds = { NULL, };
Alex Chiang747479a2009-10-19 15:14:50 -0600817 struct platform_device *dd;
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200818 acpi_status status;
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200819 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400820
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200821 dd = platform_device_register_data(NULL, "dock", dock_station_count,
822 &ds, sizeof(ds));
Alex Chiang747479a2009-10-19 15:14:50 -0600823 if (IS_ERR(dd))
824 return PTR_ERR(dd);
Alex Chiang9751cb72009-10-19 15:14:40 -0600825
Alex Chiang747479a2009-10-19 15:14:50 -0600826 dock_station = dd->dev.platform_data;
827
Len Brownc8f7a622006-07-09 17:22:28 -0400828 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600829 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400830 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400831
Alex Chiang747479a2009-10-19 15:14:50 -0600832 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600833 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700834
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700835 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600836 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700837
Jiang Liuc9b54712013-06-29 00:24:42 +0800838 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800839 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800840 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800841 dock_station->flags |= DOCK_IS_ATA;
842 if (is_battery(handle))
843 dock_station->flags |= DOCK_IS_BAT;
844
Alex Chiang747479a2009-10-19 15:14:50 -0600845 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800846 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600847 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800848
Len Brownc8f7a622006-07-09 17:22:28 -0400849 /* Find dependent devices */
850 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +0800851 ACPI_UINT32_MAX, find_dock_devices, NULL,
852 dock_station, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400853
854 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600855 ret = add_dock_dependent_device(dock_station, handle);
856 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600857 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400858
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200859 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
860 dock_notify_handler, dock_station);
Wei Yongjun0177f292013-07-17 08:33:25 +0800861 if (ACPI_FAILURE(status)) {
862 ret = -ENODEV;
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200863 goto err_rmgroup;
Wei Yongjun0177f292013-07-17 08:33:25 +0800864 }
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200865
Shaohua Lidb350b02008-08-28 10:03:58 +0800866 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600867 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -0400868 return 0;
869
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600870err_rmgroup:
Rafael J. Wysockia30c4c52013-06-30 23:50:24 +0200871 remove_dock_dependent_devices(dock_station);
Alex Chiang747479a2009-10-19 15:14:50 -0600872 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600873err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600874 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000875 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400876 return ret;
877}
878
879/**
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200880 * find_dock_and_bay - look for dock stations and bays
Len Brownc8f7a622006-07-09 17:22:28 -0400881 * @handle: acpi handle of a device
882 * @lvl: unused
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200883 * @context: unused
Len Brownc8f7a622006-07-09 17:22:28 -0400884 * @rv: unused
885 *
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200886 * This is called by acpi_walk_namespace to look for dock stations and bays.
Len Brownc8f7a622006-07-09 17:22:28 -0400887 */
Hanjun Guo027951e2013-08-13 18:31:13 +0800888static acpi_status __init
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200889find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400890{
Jiang Liuc9b54712013-06-29 00:24:42 +0800891 if (acpi_dock_match(handle) || is_ejectable_bay(handle))
Zhang Rui1ee4d612010-03-22 15:46:49 +0800892 dock_add(handle);
Alex Chiang747479a2009-10-19 15:14:50 -0600893
Zhang Rui1ee4d612010-03-22 15:46:49 +0800894 return AE_OK;
Len Brownc8f7a622006-07-09 17:22:28 -0400895}
896
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200897void __init acpi_dock_init(void)
Len Brownc8f7a622006-07-09 17:22:28 -0400898{
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200899 /* look for dock stations and bays */
Len Brownc8f7a622006-07-09 17:22:28 -0400900 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200901 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400902
Shaohua Lidb350b02008-08-28 10:03:58 +0800903 if (!dock_station_count) {
Toshi Kanicd730182012-11-20 23:42:30 +0000904 pr_info(PREFIX "No dock devices found.\n");
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200905 return;
Shaohua Lidb350b02008-08-28 10:03:58 +0800906 }
Len Brownc8f7a622006-07-09 17:22:28 -0400907
Toshi Kanicd730182012-11-20 23:42:30 +0000908 pr_info(PREFIX "%s: %d docks/bays found\n",
Shaohua Lidb350b02008-08-28 10:03:58 +0800909 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -0400910}