blob: 05ea4be01a832ccb3d8b38e4318c66fee0f83a89 [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
Frank Seidela340af12007-12-07 13:20:42 +010054static const struct acpi_device_id dock_device_ids[] = {
55 {"LNXDOCK", 0},
56 {"", 0},
57};
58MODULE_DEVICE_TABLE(acpi, dock_device_ids);
59
Len Brownc8f7a622006-07-09 17:22:28 -040060struct dock_station {
61 acpi_handle handle;
62 unsigned long last_dock_time;
63 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040064 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080065
Alex Chiang50d716e2009-10-01 11:59:23 -060066 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080067 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040068};
Shaohua Lidb350b02008-08-28 10:03:58 +080069static LIST_HEAD(dock_stations);
70static int dock_station_count;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020071static DEFINE_MUTEX(hotplug_lock);
Len Brownc8f7a622006-07-09 17:22:28 -040072
73struct dock_dependent_device {
74 struct list_head list;
Len Brownc8f7a622006-07-09 17:22:28 -040075 acpi_handle handle;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020076 const struct acpi_dock_ops *hp_ops;
77 void *hp_context;
78 unsigned int hp_refcount;
79 void (*hp_release)(void *);
Len Brownc8f7a622006-07-09 17:22:28 -040080};
81
82#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070083#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080084#define DOCK_IS_DOCK 0x00000010
85#define DOCK_IS_ATA 0x00000020
86#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070087#define DOCK_EVENT 3
88#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040089
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020090enum dock_callback_type {
91 DOCK_CALL_HANDLER,
92 DOCK_CALL_FIXUP,
93 DOCK_CALL_UEVENT,
94};
95
Len Brownc8f7a622006-07-09 17:22:28 -040096/*****************************************************************************
97 * Dock Dependent device functions *
98 *****************************************************************************/
99/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600100 * add_dock_dependent_device - associate a device with the dock station
101 * @ds: The dock station
102 * @handle: handle of the dependent device
Len Brownc8f7a622006-07-09 17:22:28 -0400103 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600104 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -0400105 */
Jiang Liud423c082013-06-29 00:24:36 +0800106static int __init
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600107add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400108{
109 struct dock_dependent_device *dd;
110
111 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600112 if (!dd)
113 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -0400114
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600115 dd->handle = handle;
116 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -0400117 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600118
119 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400120}
121
Rafael J. Wysockia30c4c52013-06-30 23:50:24 +0200122static void remove_dock_dependent_devices(struct dock_station *ds)
123{
124 struct dock_dependent_device *dd, *aux;
125
126 list_for_each_entry_safe(dd, aux, &ds->dependent_devices, list) {
127 list_del(&dd->list);
128 kfree(dd);
129 }
130}
131
Len Brownc8f7a622006-07-09 17:22:28 -0400132/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200133 * dock_init_hotplug - Initialize a hotplug device on a docking station.
134 * @dd: Dock-dependent device.
135 * @ops: Dock operations to attach to the dependent device.
136 * @context: Data to pass to the @ops callbacks and @release.
137 * @init: Optional initialization routine to run after setting up context.
138 * @release: Optional release routine to run on removal.
Len Brownc8f7a622006-07-09 17:22:28 -0400139 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200140static int dock_init_hotplug(struct dock_dependent_device *dd,
141 const struct acpi_dock_ops *ops, void *context,
142 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400143{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200144 int ret = 0;
145
146 mutex_lock(&hotplug_lock);
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200147 if (WARN_ON(dd->hp_context)) {
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200148 ret = -EEXIST;
149 } else {
150 dd->hp_refcount = 1;
151 dd->hp_ops = ops;
152 dd->hp_context = context;
153 dd->hp_release = release;
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200154 if (init)
155 init(context);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200156 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200157 mutex_unlock(&hotplug_lock);
158 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400159}
160
161/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200162 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
163 * @dd: Dock-dependent device.
Len Brownc8f7a622006-07-09 17:22:28 -0400164 *
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200165 * Decrement the reference counter of @dd and if 0, detach its hotplug
166 * operations from it, reset its context pointer and run the optional release
167 * routine if present.
Len Brownc8f7a622006-07-09 17:22:28 -0400168 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200169static void dock_release_hotplug(struct dock_dependent_device *dd)
Len Brownc8f7a622006-07-09 17:22:28 -0400170{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200171 mutex_lock(&hotplug_lock);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200172 if (dd->hp_context && !--dd->hp_refcount) {
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200173 void (*release)(void *) = dd->hp_release;
174 void *context = dd->hp_context;
175
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200176 dd->hp_ops = NULL;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200177 dd->hp_context = NULL;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200178 dd->hp_release = NULL;
Rafael J. Wysocki4ec24062013-06-30 23:47:14 +0200179 if (release)
180 release(context);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200181 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200182 mutex_unlock(&hotplug_lock);
183}
184
185static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200186 enum dock_callback_type cb_type)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200187{
188 acpi_notify_handler cb = NULL;
189 bool run = false;
190
191 mutex_lock(&hotplug_lock);
192
193 if (dd->hp_context) {
194 run = true;
195 dd->hp_refcount++;
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200196 if (dd->hp_ops) {
197 switch (cb_type) {
198 case DOCK_CALL_FIXUP:
199 cb = dd->hp_ops->fixup;
200 break;
201 case DOCK_CALL_UEVENT:
202 cb = dd->hp_ops->uevent;
203 break;
204 default:
205 cb = dd->hp_ops->handler;
206 }
207 }
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200208 }
209
210 mutex_unlock(&hotplug_lock);
211
212 if (!run)
213 return;
214
215 if (cb)
216 cb(dd->handle, event, dd->hp_context);
217
218 dock_release_hotplug(dd);
Len Brownc8f7a622006-07-09 17:22:28 -0400219}
220
221/**
222 * find_dock_dependent_device - get a device dependent on this dock
223 * @ds: the dock station
224 * @handle: the acpi_handle of the device we want
225 *
226 * iterate over the dependent device list for this dock. If the
227 * dependent device matches the handle, return.
228 */
229static struct dock_dependent_device *
230find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
231{
232 struct dock_dependent_device *dd;
233
Jiang Liued633e72013-06-29 00:24:35 +0800234 list_for_each_entry(dd, &ds->dependent_devices, list)
235 if (handle == dd->handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400236 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800237
Len Brownc8f7a622006-07-09 17:22:28 -0400238 return NULL;
239}
240
241/*****************************************************************************
242 * Dock functions *
243 *****************************************************************************/
Jiang Liud423c082013-06-29 00:24:36 +0800244static int __init is_battery(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800245{
246 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800247 int ret = 1;
248
Bob Moore15b8dd52009-06-29 13:39:29 +0800249 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800250 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800251 if (!(info->valid & ACPI_VALID_HID))
252 ret = 0;
253 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800254 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800255
Bob Moore15b8dd52009-06-29 13:39:29 +0800256 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800257 return ret;
258}
259
Jiang Liuc9b54712013-06-29 00:24:42 +0800260/* Check whether ACPI object is an ejectable battery or disk bay */
261static bool __init is_ejectable_bay(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800262{
Jiang Liuc9b54712013-06-29 00:24:42 +0800263 if (acpi_has_method(handle, "_EJ0") && is_battery(handle))
264 return true;
Alex Chiang747479a2009-10-19 15:14:50 -0600265
Jiang Liuc9b54712013-06-29 00:24:42 +0800266 return acpi_bay_match(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800267}
268
Len Brownc8f7a622006-07-09 17:22:28 -0400269/**
270 * is_dock_device - see if a device is on a dock station
271 * @handle: acpi handle of the device
272 *
273 * If this device is either the dock station itself,
274 * or is a device dependent on the dock station, then it
275 * is a dock device
276 */
277int is_dock_device(acpi_handle handle)
278{
Shaohua Lidb350b02008-08-28 10:03:58 +0800279 struct dock_station *dock_station;
280
281 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400282 return 0;
283
Jiang Liuc9b54712013-06-29 00:24:42 +0800284 if (acpi_dock_match(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400285 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600286
287 list_for_each_entry(dock_station, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +0800288 if (find_dock_dependent_device(dock_station, handle))
289 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400290
291 return 0;
292}
Len Brownc8f7a622006-07-09 17:22:28 -0400293EXPORT_SYMBOL_GPL(is_dock_device);
294
295/**
296 * dock_present - see if the dock station is present.
297 * @ds: the dock station
298 *
299 * execute the _STA method. note that present does not
300 * imply that we are docked.
301 */
302static int dock_present(struct dock_station *ds)
303{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400304 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400305 acpi_status status;
306
307 if (ds) {
308 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
309 if (ACPI_SUCCESS(status) && sta)
310 return 1;
311 }
312 return 0;
313}
314
Len Brownc8f7a622006-07-09 17:22:28 -0400315/**
316 * dock_create_acpi_device - add new devices to acpi
317 * @handle - handle of the device to add
318 *
319 * This function will create a new acpi_device for the given
320 * handle if one does not exist already. This should cause
321 * acpi to scan for drivers for the given devices, and call
322 * matching driver's add routine.
Len Brownc8f7a622006-07-09 17:22:28 -0400323 */
Jiang Liu472d9632013-06-29 00:24:37 +0800324static void dock_create_acpi_device(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400325{
Alex Chiang747479a2009-10-19 15:14:50 -0600326 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400327 int ret;
328
329 if (acpi_bus_get_device(handle, &device)) {
330 /*
331 * no device created for this object,
332 * so we should create one.
333 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100334 ret = acpi_bus_scan(handle);
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100335 if (ret)
Alex Chiang747479a2009-10-19 15:14:50 -0600336 pr_debug("error adding bus, %x\n", -ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400337 }
Len Brownc8f7a622006-07-09 17:22:28 -0400338}
339
340/**
341 * dock_remove_acpi_device - remove the acpi_device struct from acpi
342 * @handle - the handle of the device to remove
343 *
344 * Tell acpi to remove the acpi_device. This should cause any loaded
345 * driver to have it's remove routine called.
346 */
347static void dock_remove_acpi_device(acpi_handle handle)
348{
349 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400350
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100351 if (!acpi_bus_get_device(handle, &device))
352 acpi_bus_trim(device);
Len Brownc8f7a622006-07-09 17:22:28 -0400353}
354
Len Brownc8f7a622006-07-09 17:22:28 -0400355/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200356 * hot_remove_dock_devices - Remove dock station devices.
357 * @ds: Dock station.
358 */
359static void hot_remove_dock_devices(struct dock_station *ds)
360{
361 struct dock_dependent_device *dd;
362
363 /*
364 * Walk the list in reverse order so that devices that have been added
365 * last are removed first (in case there are some indirect dependencies
366 * between them).
367 */
368 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
369 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
370
371 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
372 dock_remove_acpi_device(dd->handle);
373}
374
375/**
376 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a622006-07-09 17:22:28 -0400377 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200378 * @event: either bus check or device check request
Len Brownc8f7a622006-07-09 17:22:28 -0400379 *
380 * Some devices on the dock station need to have drivers called
381 * to perform hotplug operations after a dock event has occurred.
382 * Traverse the list of dock devices that have registered a
383 * hotplug handler, and call the handler.
384 */
385static void hotplug_dock_devices(struct dock_station *ds, u32 event)
386{
387 struct dock_dependent_device *dd;
388
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200389 /* Call driver specific post-dock fixups. */
390 list_for_each_entry(dd, &ds->dependent_devices, list)
391 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
392
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200393 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200394 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200395 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
Len Brownc8f7a622006-07-09 17:22:28 -0400396
397 /*
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200398 * Now make sure that an acpi_device is created for each dependent
399 * device. That will cause scan handlers to be attached to device
400 * objects or acpi_drivers to be stopped/started if they are present.
Len Brownc8f7a622006-07-09 17:22:28 -0400401 */
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200402 list_for_each_entry(dd, &ds->dependent_devices, list)
403 dock_create_acpi_device(dd->handle);
Len Brownc8f7a622006-07-09 17:22:28 -0400404}
405
406static void dock_event(struct dock_station *ds, u32 event, int num)
407{
Shaohua Lidb350b02008-08-28 10:03:58 +0800408 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700409 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700410 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800411 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700412
413 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700414 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700415 else
Holger Macht66b56822007-08-10 13:10:32 -0700416 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700417
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700418 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800419 * Indicate that the status of the dock station has
420 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700421 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800422 if (num == DOCK_EVENT)
423 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
424
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200425 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200426 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
Alex Chiang747479a2009-10-19 15:14:50 -0600427
Shaohua Li1253f7a2008-08-28 10:06:16 +0800428 if (num != DOCK_EVENT)
429 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400430}
431
432/**
Len Brownc8f7a622006-07-09 17:22:28 -0400433 * handle_dock - handle a dock event
434 * @ds: the dock station
435 * @dock: to dock, or undock - that is the question
436 *
437 * Execute the _DCK method in response to an acpi event
438 */
439static void handle_dock(struct dock_station *ds, int dock)
440{
441 acpi_status status;
442 struct acpi_object_list arg_list;
443 union acpi_object arg;
444 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400445
Toshi Kanicd730182012-11-20 23:42:30 +0000446 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400447
448 /* _DCK method has one argument */
449 arg_list.count = 1;
450 arg_list.pointer = &arg;
451 arg.type = ACPI_TYPE_INTEGER;
452 arg.integer.value = dock;
453 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800454 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000455 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
456 status);
Thomas Renninger0a918a92008-10-11 00:15:04 -0400457
Len Brownc8f7a622006-07-09 17:22:28 -0400458 kfree(buffer.pointer);
Len Brownc8f7a622006-07-09 17:22:28 -0400459}
460
461static inline void dock(struct dock_station *ds)
462{
463 handle_dock(ds, 1);
464}
465
466static inline void undock(struct dock_station *ds)
467{
468 handle_dock(ds, 0);
469}
470
471static inline void begin_dock(struct dock_station *ds)
472{
473 ds->flags |= DOCK_DOCKING;
474}
475
476static inline void complete_dock(struct dock_station *ds)
477{
478 ds->flags &= ~(DOCK_DOCKING);
479 ds->last_dock_time = jiffies;
480}
481
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700482static inline void begin_undock(struct dock_station *ds)
483{
484 ds->flags |= DOCK_UNDOCKING;
485}
486
487static inline void complete_undock(struct dock_station *ds)
488{
489 ds->flags &= ~(DOCK_UNDOCKING);
490}
491
Len Brownc8f7a622006-07-09 17:22:28 -0400492/**
493 * dock_in_progress - see if we are in the middle of handling a dock event
494 * @ds: the dock station
495 *
496 * Sometimes while docking, false dock events can be sent to the driver
497 * because good connections aren't made or some other reason. Ignore these
498 * if we are in the middle of doing something.
499 */
500static int dock_in_progress(struct dock_station *ds)
501{
502 if ((ds->flags & DOCK_DOCKING) ||
503 time_before(jiffies, (ds->last_dock_time + HZ)))
504 return 1;
505 return 0;
506}
507
508/**
Len Brownc8f7a622006-07-09 17:22:28 -0400509 * register_hotplug_dock_device - register a hotplug function
510 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800511 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400512 * @context: device specific data
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200513 * @init: Optional initialization routine to run after registration
514 * @release: Optional release routine to run on unregistration
Len Brownc8f7a622006-07-09 17:22:28 -0400515 *
516 * If a driver would like to perform a hotplug operation after a dock
517 * event, they can register an acpi_notifiy_handler to be called by
518 * the dock driver after _DCK is executed.
519 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200520int register_hotplug_dock_device(acpi_handle handle,
521 const struct acpi_dock_ops *ops, void *context,
522 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400523{
524 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800525 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800526 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400527
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200528 if (WARN_ON(!context))
529 return -EINVAL;
530
Shaohua Lidb350b02008-08-28 10:03:58 +0800531 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400532 return -ENODEV;
533
534 /*
535 * make sure this handle is for a device dependent on the dock,
536 * this would include the dock station itself
537 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600538 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800539 /*
540 * An ATA bay can be in a dock and itself can be ejected
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800541 * separately, so there are two 'dock stations' which need the
Shaohua Li61b83692008-08-28 10:07:14 +0800542 * ops
543 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800544 dd = find_dock_dependent_device(dock_station, handle);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200545 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
Shaohua Li61b83692008-08-28 10:07:14 +0800546 ret = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400547 }
548
Shaohua Li61b83692008-08-28 10:07:14 +0800549 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400550}
Len Brownc8f7a622006-07-09 17:22:28 -0400551EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
552
553/**
554 * unregister_hotplug_dock_device - remove yourself from the hotplug list
555 * @handle: the acpi handle of the device
556 */
557void unregister_hotplug_dock_device(acpi_handle handle)
558{
559 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800560 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400561
Shaohua Lidb350b02008-08-28 10:03:58 +0800562 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400563 return;
564
Alex Chiang50d716e2009-10-01 11:59:23 -0600565 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800566 dd = find_dock_dependent_device(dock_station, handle);
567 if (dd)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200568 dock_release_hotplug(dd);
Shaohua Lidb350b02008-08-28 10:03:58 +0800569 }
Len Brownc8f7a622006-07-09 17:22:28 -0400570}
Len Brownc8f7a622006-07-09 17:22:28 -0400571EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
572
573/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800574 * handle_eject_request - handle an undock request checking for error conditions
575 *
576 * Check to make sure the dock device is still present, then undock and
577 * hotremove all the devices that may need removing.
578 */
579static int handle_eject_request(struct dock_station *ds, u32 event)
580{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800581 if (dock_in_progress(ds))
582 return -EBUSY;
583
584 /*
585 * here we need to generate the undock
586 * event prior to actually doing the undock
587 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200588 * Also, even send the dock event if the
589 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800590 */
591 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200592
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200593 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800594 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800595 acpi_evaluate_lck(ds->handle, 0);
596 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800597 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000598 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800599 return -EBUSY;
600 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700601 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800602 return 0;
603}
604
605/**
Len Brownc8f7a622006-07-09 17:22:28 -0400606 * dock_notify - act upon an acpi dock notification
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200607 * @ds: dock station
Len Brownc8f7a622006-07-09 17:22:28 -0400608 * @event: the acpi event
Len Brownc8f7a622006-07-09 17:22:28 -0400609 *
610 * If we are notified to dock, then check to see if the dock is
611 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800612 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400613 */
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200614static void dock_notify(struct dock_station *ds, u32 event)
Len Brownc8f7a622006-07-09 17:22:28 -0400615{
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200616 acpi_handle handle = ds->handle;
617 struct acpi_device *ad;
Shaohua Lidb350b02008-08-28 10:03:58 +0800618 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400619
Shaohua Lidb350b02008-08-28 10:03:58 +0800620 /*
621 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
622 * is sent and _DCK is present, it is assumed to mean an undock
623 * request.
624 */
625 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
626 event = ACPI_NOTIFY_EJECT_REQUEST;
627
628 /*
629 * dock station: BUS_CHECK - docked or surprise removal
630 * DEVICE_CHECK - undocked
631 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
632 *
633 * To simplify event handling, dock dependent device handler always
634 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
635 * ACPI_NOTIFY_EJECT_REQUEST for removal
636 */
Len Brownc8f7a622006-07-09 17:22:28 -0400637 switch (event) {
638 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800639 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200640 if (!dock_in_progress(ds) && acpi_bus_get_device(handle, &ad)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400641 begin_dock(ds);
642 dock(ds);
643 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000644 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800645 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400646 break;
647 }
Len Brownc8f7a622006-07-09 17:22:28 -0400648 hotplug_dock_devices(ds, event);
649 complete_dock(ds);
650 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800651 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800652 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800653 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400654 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800655 if (dock_present(ds) || dock_in_progress(ds))
656 break;
657 /* This is a surprise removal */
658 surprise_removal = 1;
659 event = ACPI_NOTIFY_EJECT_REQUEST;
660 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400661 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700662 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800663 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
664 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700665 handle_eject_request(ds, event);
666 else
667 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400668 break;
669 default:
Toshi Kanicd730182012-11-20 23:42:30 +0000670 acpi_handle_err(handle, "Unknown dock event %d\n", event);
Len Brownc8f7a622006-07-09 17:22:28 -0400671 }
672}
673
Zhang Rui19cd8472008-08-28 10:05:06 +0800674struct dock_data {
Zhang Rui19cd8472008-08-28 10:05:06 +0800675 struct dock_station *ds;
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200676 u32 event;
Zhang Rui19cd8472008-08-28 10:05:06 +0800677};
678
679static void acpi_dock_deferred_cb(void *context)
680{
Alex Chiang747479a2009-10-19 15:14:50 -0600681 struct dock_data *data = context;
Zhang Rui19cd8472008-08-28 10:05:06 +0800682
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100683 acpi_scan_lock_acquire();
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200684 dock_notify(data->ds, data->event);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100685 acpi_scan_lock_release();
Zhang Rui19cd8472008-08-28 10:05:06 +0800686 kfree(data);
687}
688
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200689static void dock_notify_handler(acpi_handle handle, u32 event, void *data)
Shaohua Li6bd00a62008-08-28 10:04:29 +0800690{
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200691 struct dock_data *dd;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800692
693 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
694 && event != ACPI_NOTIFY_EJECT_REQUEST)
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200695 return;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100696
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200697 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
698 if (dd) {
699 acpi_status status;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100700
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200701 dd->ds = data;
702 dd->event = event;
703 status = acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
704 if (ACPI_FAILURE(status))
705 kfree(dd);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800706 }
Shaohua Li6bd00a62008-08-28 10:04:29 +0800707}
708
Len Brownc8f7a622006-07-09 17:22:28 -0400709/**
710 * find_dock_devices - find devices on the dock station
711 * @handle: the handle of the device we are examining
712 * @lvl: unused
713 * @context: the dock station private data
714 * @rv: unused
715 *
716 * This function is called by acpi_walk_namespace. It will
717 * check to see if an object has an _EJD method. If it does, then it
718 * will see if it is dependent on the dock station.
719 */
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200720static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
721 void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400722{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200723 struct dock_station *ds = context;
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200724 acpi_handle ejd = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -0400725
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200726 acpi_bus_get_ejd(handle, &ejd);
727 if (ejd == ds->handle)
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600728 add_dock_dependent_device(ds, handle);
729
Len Brownc8f7a622006-07-09 17:22:28 -0400730 return AE_OK;
731}
732
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800733/*
734 * show_docked - read method for "docked" file in sysfs
735 */
736static ssize_t show_docked(struct device *dev,
737 struct device_attribute *attr, char *buf)
738{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100739 struct acpi_device *tmp;
740
Alex Chiangfe06fba2009-10-19 15:14:45 -0600741 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800742
Yasuaki Ishimatsu02df7342013-01-31 03:23:53 +0000743 if (!acpi_bus_get_device(dock_station->handle, &tmp))
Holger Machtfc5a9f82009-01-20 12:18:24 +0100744 return snprintf(buf, PAGE_SIZE, "1\n");
745 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800746}
Adrian Bunke5685b92007-10-24 18:24:42 +0200747static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800748
749/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700750 * show_flags - read method for flags file in sysfs
751 */
752static ssize_t show_flags(struct device *dev,
753 struct device_attribute *attr, char *buf)
754{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600755 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700756 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
757
758}
Adrian Bunke5685b92007-10-24 18:24:42 +0200759static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700760
761/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800762 * write_undock - write method for "undock" file in sysfs
763 */
764static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
765 const char *buf, size_t count)
766{
767 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600768 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800769
770 if (!count)
771 return -EINVAL;
772
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200773 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100774 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800775 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200776 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800777 return ret ? ret: count;
778}
Adrian Bunke5685b92007-10-24 18:24:42 +0200779static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800780
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800781/*
782 * show_dock_uid - read method for "uid" file in sysfs
783 */
784static ssize_t show_dock_uid(struct device *dev,
785 struct device_attribute *attr, char *buf)
786{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400787 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600788 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700789 acpi_status status = acpi_evaluate_integer(dock_station->handle,
790 "_UID", NULL, &lbuf);
791 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800792 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700793
Matthew Wilcox27663c52008-10-10 02:22:59 -0400794 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800795}
Adrian Bunke5685b92007-10-24 18:24:42 +0200796static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800797
Shaohua Li8652b002008-08-28 10:07:45 +0800798static ssize_t show_dock_type(struct device *dev,
799 struct device_attribute *attr, char *buf)
800{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600801 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800802 char *type;
803
804 if (dock_station->flags & DOCK_IS_DOCK)
805 type = "dock_station";
806 else if (dock_station->flags & DOCK_IS_ATA)
807 type = "ata_bay";
808 else if (dock_station->flags & DOCK_IS_BAT)
809 type = "battery_bay";
810 else
811 type = "unknown";
812
813 return snprintf(buf, PAGE_SIZE, "%s\n", type);
814}
815static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
816
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600817static struct attribute *dock_attributes[] = {
818 &dev_attr_docked.attr,
819 &dev_attr_flags.attr,
820 &dev_attr_undock.attr,
821 &dev_attr_uid.attr,
822 &dev_attr_type.attr,
823 NULL
824};
825
826static struct attribute_group dock_attribute_group = {
827 .attrs = dock_attributes
828};
829
Len Brownc8f7a622006-07-09 17:22:28 -0400830/**
831 * dock_add - add a new dock station
832 * @handle: the dock station handle
833 *
834 * allocated and initialize a new dock station device. Find all devices
835 * that are on the dock station, and register for dock event notifications.
836 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200837static int __init dock_add(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400838{
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200839 struct dock_station *dock_station, ds = { NULL, };
Alex Chiang747479a2009-10-19 15:14:50 -0600840 struct platform_device *dd;
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200841 acpi_status status;
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200842 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400843
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200844 dd = platform_device_register_data(NULL, "dock", dock_station_count,
845 &ds, sizeof(ds));
Alex Chiang747479a2009-10-19 15:14:50 -0600846 if (IS_ERR(dd))
847 return PTR_ERR(dd);
Alex Chiang9751cb72009-10-19 15:14:40 -0600848
Alex Chiang747479a2009-10-19 15:14:50 -0600849 dock_station = dd->dev.platform_data;
850
Len Brownc8f7a622006-07-09 17:22:28 -0400851 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600852 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400853 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400854
Alex Chiang747479a2009-10-19 15:14:50 -0600855 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600856 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700857
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700858 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600859 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700860
Jiang Liuc9b54712013-06-29 00:24:42 +0800861 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800862 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800863 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800864 dock_station->flags |= DOCK_IS_ATA;
865 if (is_battery(handle))
866 dock_station->flags |= DOCK_IS_BAT;
867
Alex Chiang747479a2009-10-19 15:14:50 -0600868 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800869 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600870 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800871
Len Brownc8f7a622006-07-09 17:22:28 -0400872 /* Find dependent devices */
873 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +0800874 ACPI_UINT32_MAX, find_dock_devices, NULL,
875 dock_station, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400876
877 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600878 ret = add_dock_dependent_device(dock_station, handle);
879 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600880 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400881
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200882 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
883 dock_notify_handler, dock_station);
Wei Yongjun0177f292013-07-17 08:33:25 +0800884 if (ACPI_FAILURE(status)) {
885 ret = -ENODEV;
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200886 goto err_rmgroup;
Wei Yongjun0177f292013-07-17 08:33:25 +0800887 }
Rafael J. Wysocki59401cc2013-06-30 23:48:49 +0200888
Shaohua Lidb350b02008-08-28 10:03:58 +0800889 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600890 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -0400891 return 0;
892
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600893err_rmgroup:
Rafael J. Wysockia30c4c52013-06-30 23:50:24 +0200894 remove_dock_dependent_devices(dock_station);
Alex Chiang747479a2009-10-19 15:14:50 -0600895 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600896err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600897 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000898 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400899 return ret;
900}
901
902/**
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200903 * find_dock_and_bay - look for dock stations and bays
Len Brownc8f7a622006-07-09 17:22:28 -0400904 * @handle: acpi handle of a device
905 * @lvl: unused
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200906 * @context: unused
Len Brownc8f7a622006-07-09 17:22:28 -0400907 * @rv: unused
908 *
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200909 * This is called by acpi_walk_namespace to look for dock stations and bays.
Len Brownc8f7a622006-07-09 17:22:28 -0400910 */
Hanjun Guo027951e2013-08-13 18:31:13 +0800911static acpi_status __init
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200912find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400913{
Jiang Liuc9b54712013-06-29 00:24:42 +0800914 if (acpi_dock_match(handle) || is_ejectable_bay(handle))
Zhang Rui1ee4d612010-03-22 15:46:49 +0800915 dock_add(handle);
Alex Chiang747479a2009-10-19 15:14:50 -0600916
Zhang Rui1ee4d612010-03-22 15:46:49 +0800917 return AE_OK;
Len Brownc8f7a622006-07-09 17:22:28 -0400918}
919
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200920void __init acpi_dock_init(void)
Len Brownc8f7a622006-07-09 17:22:28 -0400921{
Len Brown816c2ed2008-06-24 22:57:12 -0400922 if (acpi_disabled)
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200923 return;
Len Brown816c2ed2008-06-24 22:57:12 -0400924
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200925 /* look for dock stations and bays */
Len Brownc8f7a622006-07-09 17:22:28 -0400926 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200927 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400928
Shaohua Lidb350b02008-08-28 10:03:58 +0800929 if (!dock_station_count) {
Toshi Kanicd730182012-11-20 23:42:30 +0000930 pr_info(PREFIX "No dock devices found.\n");
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200931 return;
Shaohua Lidb350b02008-08-28 10:03:58 +0800932 }
Len Brownc8f7a622006-07-09 17:22:28 -0400933
Toshi Kanicd730182012-11-20 23:42:30 +0000934 pr_info(PREFIX "%s: %d docks/bays found\n",
Shaohua Lidb350b02008-08-28 10:03:58 +0800935 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -0400936}