blob: d9339b442a4ebdc6870d4dd0b4dac4db65ed8613 [file] [log] [blame]
Len Brownc8f7a62c2006-07-09 17:22:28 -04001/*
2 * dock.c - ACPI dock station driver
3 *
Rafael J. Wysocki8cc25682014-02-21 01:11:46 +01004 * Copyright (C) 2006, 2014, Intel Corp.
5 * Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
6 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Len Brownc8f7a62c2006-07-09 17:22:28 -04007 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Len Brownc8f7a62c2006-07-09 17:22:28 -040030#include <linux/init.h>
31#include <linux/types.h>
32#include <linux/notifier.h>
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080033#include <linux/platform_device.h>
Al Viro914e2632006-10-18 13:55:46 -040034#include <linux/jiffies.h>
Randy Dunlap62a6d7f2007-03-26 21:38:49 -080035#include <linux/stddef.h>
Toshi Kanicd730182012-11-20 23:42:30 +000036#include <linux/acpi.h>
Len Brownc8f7a62c2006-07-09 17:22:28 -040037
Rashika3f9eed52013-12-17 15:00:04 +053038#include "internal.h"
39
Len Brown7cda93e2007-02-12 23:50:02 -050040#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a62c2006-07-09 17:22:28 -040041
Len Brownf52fd662007-02-12 22:42:12 -050042ACPI_MODULE_NAME("dock");
Len Brownc8f7a62c2006-07-09 17:22:28 -040043MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050044MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a62c2006-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 Brownc8f7a62c2006-07-09 17:22:28 -040054struct dock_station {
55 acpi_handle handle;
56 unsigned long last_dock_time;
57 u32 flags;
Len Brownc8f7a62c2006-07-09 17:22:28 -040058 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080059
Alex Chiang50d716e2009-10-01 11:59:23 -060060 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080061 struct platform_device *dock_device;
Len Brownc8f7a62c2006-07-09 17:22:28 -040062};
Shaohua Lidb350b02008-08-28 10:03:58 +080063static LIST_HEAD(dock_stations);
64static int dock_station_count;
Len Brownc8f7a62c2006-07-09 17:22:28 -040065
66struct dock_dependent_device {
67 struct list_head list;
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010068 struct acpi_device *adev;
Len Brownc8f7a62c2006-07-09 17:22:28 -040069};
70
71#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070072#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080073#define DOCK_IS_DOCK 0x00000010
74#define DOCK_IS_ATA 0x00000020
75#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070076#define DOCK_EVENT 3
77#define UNDOCK_EVENT 2
Len Brownc8f7a62c2006-07-09 17:22:28 -040078
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020079enum dock_callback_type {
80 DOCK_CALL_HANDLER,
81 DOCK_CALL_FIXUP,
82 DOCK_CALL_UEVENT,
83};
84
Len Brownc8f7a62c2006-07-09 17:22:28 -040085/*****************************************************************************
86 * Dock Dependent device functions *
87 *****************************************************************************/
88/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060089 * add_dock_dependent_device - associate a device with the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010090 * @ds: Dock station.
91 * @adev: Dependent ACPI device object.
Len Brownc8f7a62c2006-07-09 17:22:28 -040092 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -060093 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a62c2006-07-09 17:22:28 -040094 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010095static int add_dock_dependent_device(struct dock_station *ds,
96 struct acpi_device *adev)
Len Brownc8f7a62c2006-07-09 17:22:28 -040097{
98 struct dock_dependent_device *dd;
99
100 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600101 if (!dd)
102 return -ENOMEM;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400103
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100104 dd->adev = adev;
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600105 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400106 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600107
108 return 0;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400109}
110
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200111static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200112 enum dock_callback_type cb_type)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200113{
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100114 struct acpi_device *adev = dd->adev;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200115
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100116 acpi_lock_hp_context();
117
118 if (!adev->hp)
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100119 goto out;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100120
121 if (cb_type == DOCK_CALL_FIXUP) {
122 void (*fixup)(struct acpi_device *);
123
124 fixup = adev->hp->fixup;
125 if (fixup) {
126 acpi_unlock_hp_context();
127 fixup(adev);
128 return;
129 }
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100130 } else if (cb_type == DOCK_CALL_UEVENT) {
131 void (*uevent)(struct acpi_device *, u32);
132
133 uevent = adev->hp->uevent;
134 if (uevent) {
135 acpi_unlock_hp_context();
136 uevent(adev, event);
137 return;
138 }
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100139 } else {
140 int (*notify)(struct acpi_device *, u32);
141
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100142 notify = adev->hp->notify;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100143 if (notify) {
144 acpi_unlock_hp_context();
145 notify(adev, event);
146 return;
147 }
148 }
149
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100150 out:
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100151 acpi_unlock_hp_context();
Len Brownc8f7a62c2006-07-09 17:22:28 -0400152}
153
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100154static struct dock_station *find_dock_station(acpi_handle handle)
155{
156 struct dock_station *ds;
157
158 list_for_each_entry(ds, &dock_stations, sibling)
159 if (ds->handle == handle)
160 return ds;
161
162 return NULL;
163}
164
Len Brownc8f7a62c2006-07-09 17:22:28 -0400165/**
166 * find_dock_dependent_device - get a device dependent on this dock
167 * @ds: the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100168 * @adev: ACPI device object to find.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400169 *
170 * iterate over the dependent device list for this dock. If the
171 * dependent device matches the handle, return.
172 */
173static struct dock_dependent_device *
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100174find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
Len Brownc8f7a62c2006-07-09 17:22:28 -0400175{
176 struct dock_dependent_device *dd;
177
Jiang Liued633e72013-06-29 00:24:35 +0800178 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100179 if (adev == dd->adev)
Len Brownc8f7a62c2006-07-09 17:22:28 -0400180 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800181
Len Brownc8f7a62c2006-07-09 17:22:28 -0400182 return NULL;
183}
184
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100185void register_dock_dependent_device(struct acpi_device *adev,
186 acpi_handle dshandle)
187{
188 struct dock_station *ds = find_dock_station(dshandle);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100189
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100190 if (ds && !find_dock_dependent_device(ds, adev))
191 add_dock_dependent_device(ds, adev);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100192}
193
Len Brownc8f7a62c2006-07-09 17:22:28 -0400194/*****************************************************************************
195 * Dock functions *
196 *****************************************************************************/
Shaohua Lidb350b02008-08-28 10:03:58 +0800197
Len Brownc8f7a62c2006-07-09 17:22:28 -0400198/**
199 * is_dock_device - see if a device is on a dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100200 * @adev: ACPI device object to check.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400201 *
202 * If this device is either the dock station itself,
203 * or is a device dependent on the dock station, then it
204 * is a dock device
205 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100206int is_dock_device(struct acpi_device *adev)
Len Brownc8f7a62c2006-07-09 17:22:28 -0400207{
Shaohua Lidb350b02008-08-28 10:03:58 +0800208 struct dock_station *dock_station;
209
210 if (!dock_station_count)
Len Brownc8f7a62c2006-07-09 17:22:28 -0400211 return 0;
212
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100213 if (acpi_dock_match(adev->handle))
Len Brownc8f7a62c2006-07-09 17:22:28 -0400214 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600215
216 list_for_each_entry(dock_station, &dock_stations, sibling)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100217 if (find_dock_dependent_device(dock_station, adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800218 return 1;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400219
220 return 0;
221}
Len Brownc8f7a62c2006-07-09 17:22:28 -0400222EXPORT_SYMBOL_GPL(is_dock_device);
223
224/**
225 * dock_present - see if the dock station is present.
226 * @ds: the dock station
227 *
228 * execute the _STA method. note that present does not
229 * imply that we are docked.
230 */
231static int dock_present(struct dock_station *ds)
232{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400233 unsigned long long sta;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400234 acpi_status status;
235
236 if (ds) {
237 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
238 if (ACPI_SUCCESS(status) && sta)
239 return 1;
240 }
241 return 0;
242}
243
Len Brownc8f7a62c2006-07-09 17:22:28 -0400244/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200245 * hot_remove_dock_devices - Remove dock station devices.
246 * @ds: Dock station.
247 */
248static void hot_remove_dock_devices(struct dock_station *ds)
249{
250 struct dock_dependent_device *dd;
251
252 /*
253 * Walk the list in reverse order so that devices that have been added
254 * last are removed first (in case there are some indirect dependencies
255 * between them).
256 */
257 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
258 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
259
260 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100261 acpi_bus_trim(dd->adev);
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200262}
263
264/**
265 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400266 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200267 * @event: either bus check or device check request
Len Brownc8f7a62c2006-07-09 17:22:28 -0400268 *
269 * Some devices on the dock station need to have drivers called
270 * to perform hotplug operations after a dock event has occurred.
271 * Traverse the list of dock devices that have registered a
272 * hotplug handler, and call the handler.
273 */
274static void hotplug_dock_devices(struct dock_station *ds, u32 event)
275{
276 struct dock_dependent_device *dd;
277
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200278 /* Call driver specific post-dock fixups. */
279 list_for_each_entry(dd, &ds->dependent_devices, list)
280 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
281
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200282 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200283 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200284 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400285
286 /*
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100287 * Check if all devices have been enumerated already. If not, run
288 * acpi_bus_scan() for them and that will cause scan handlers to be
289 * attached to device objects or acpi_drivers to be stopped/started if
290 * they are present.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400291 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100292 list_for_each_entry(dd, &ds->dependent_devices, list) {
293 struct acpi_device *adev = dd->adev;
294
295 if (!acpi_device_enumerated(adev)) {
296 int ret = acpi_bus_scan(adev->handle);
297 if (ret)
298 dev_dbg(&adev->dev, "scan error %d\n", -ret);
299 }
300 }
Len Brownc8f7a62c2006-07-09 17:22:28 -0400301}
302
303static void dock_event(struct dock_station *ds, u32 event, int num)
304{
Shaohua Lidb350b02008-08-28 10:03:58 +0800305 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700306 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700307 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800308 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700309
310 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700311 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700312 else
Holger Macht66b56822007-08-10 13:10:32 -0700313 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700314
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700315 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800316 * Indicate that the status of the dock station has
317 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700318 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800319 if (num == DOCK_EVENT)
320 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
321
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200322 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200323 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
Alex Chiang747479a2009-10-19 15:14:50 -0600324
Shaohua Li1253f7a2008-08-28 10:06:16 +0800325 if (num != DOCK_EVENT)
326 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400327}
328
329/**
Len Brownc8f7a62c2006-07-09 17:22:28 -0400330 * handle_dock - handle a dock event
331 * @ds: the dock station
332 * @dock: to dock, or undock - that is the question
333 *
334 * Execute the _DCK method in response to an acpi event
335 */
336static void handle_dock(struct dock_station *ds, int dock)
337{
338 acpi_status status;
339 struct acpi_object_list arg_list;
340 union acpi_object arg;
Zhang Rui6a868e12013-09-03 08:32:10 +0800341 unsigned long long value;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400342
Toshi Kanicd730182012-11-20 23:42:30 +0000343 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a62c2006-07-09 17:22:28 -0400344
345 /* _DCK method has one argument */
346 arg_list.count = 1;
347 arg_list.pointer = &arg;
348 arg.type = ACPI_TYPE_INTEGER;
349 arg.integer.value = dock;
Zhang Rui6a868e12013-09-03 08:32:10 +0800350 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
Shaohua Lidb350b02008-08-28 10:03:58 +0800351 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000352 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
353 status);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400354}
355
356static inline void dock(struct dock_station *ds)
357{
358 handle_dock(ds, 1);
359}
360
361static inline void undock(struct dock_station *ds)
362{
363 handle_dock(ds, 0);
364}
365
366static inline void begin_dock(struct dock_station *ds)
367{
368 ds->flags |= DOCK_DOCKING;
369}
370
371static inline void complete_dock(struct dock_station *ds)
372{
373 ds->flags &= ~(DOCK_DOCKING);
374 ds->last_dock_time = jiffies;
375}
376
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700377static inline void begin_undock(struct dock_station *ds)
378{
379 ds->flags |= DOCK_UNDOCKING;
380}
381
382static inline void complete_undock(struct dock_station *ds)
383{
384 ds->flags &= ~(DOCK_UNDOCKING);
385}
386
Len Brownc8f7a62c2006-07-09 17:22:28 -0400387/**
388 * dock_in_progress - see if we are in the middle of handling a dock event
389 * @ds: the dock station
390 *
391 * Sometimes while docking, false dock events can be sent to the driver
392 * because good connections aren't made or some other reason. Ignore these
393 * if we are in the middle of doing something.
394 */
395static int dock_in_progress(struct dock_station *ds)
396{
397 if ((ds->flags & DOCK_DOCKING) ||
398 time_before(jiffies, (ds->last_dock_time + HZ)))
399 return 1;
400 return 0;
401}
402
403/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800404 * handle_eject_request - handle an undock request checking for error conditions
405 *
406 * Check to make sure the dock device is still present, then undock and
407 * hotremove all the devices that may need removing.
408 */
409static int handle_eject_request(struct dock_station *ds, u32 event)
410{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800411 if (dock_in_progress(ds))
412 return -EBUSY;
413
414 /*
415 * here we need to generate the undock
416 * event prior to actually doing the undock
417 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200418 * Also, even send the dock event if the
419 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800420 */
421 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200422
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200423 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800424 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800425 acpi_evaluate_lck(ds->handle, 0);
426 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800427 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000428 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800429 return -EBUSY;
430 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700431 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800432 return 0;
433}
434
435/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100436 * dock_notify - Handle ACPI dock notification.
437 * @adev: Dock station's ACPI device object.
438 * @event: Event code.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400439 *
440 * If we are notified to dock, then check to see if the dock is
441 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800442 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400443 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100444int dock_notify(struct acpi_device *adev, u32 event)
Len Brownc8f7a62c2006-07-09 17:22:28 -0400445{
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100446 acpi_handle handle = adev->handle;
447 struct dock_station *ds = find_dock_station(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800448 int surprise_removal = 0;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400449
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100450 if (!ds)
451 return -ENODEV;
452
Shaohua Lidb350b02008-08-28 10:03:58 +0800453 /*
454 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
455 * is sent and _DCK is present, it is assumed to mean an undock
456 * request.
457 */
458 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
459 event = ACPI_NOTIFY_EJECT_REQUEST;
460
461 /*
462 * dock station: BUS_CHECK - docked or surprise removal
463 * DEVICE_CHECK - undocked
464 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
465 *
466 * To simplify event handling, dock dependent device handler always
467 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
468 * ACPI_NOTIFY_EJECT_REQUEST for removal
469 */
Len Brownc8f7a62c2006-07-09 17:22:28 -0400470 switch (event) {
471 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800472 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki0a8e5c32014-02-10 13:44:20 +0100473 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
Len Brownc8f7a62c2006-07-09 17:22:28 -0400474 begin_dock(ds);
475 dock(ds);
476 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000477 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800478 complete_dock(ds);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400479 break;
480 }
Len Brownc8f7a62c2006-07-09 17:22:28 -0400481 hotplug_dock_devices(ds, event);
482 complete_dock(ds);
483 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800484 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800485 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800486 break;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400487 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800488 if (dock_present(ds) || dock_in_progress(ds))
489 break;
490 /* This is a surprise removal */
491 surprise_removal = 1;
492 event = ACPI_NOTIFY_EJECT_REQUEST;
493 /* Fall back */
Len Brownc8f7a62c2006-07-09 17:22:28 -0400494 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700495 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800496 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
497 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700498 handle_eject_request(ds, event);
499 else
500 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400501 break;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400502 }
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100503 return 0;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400504}
505
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800506/*
507 * show_docked - read method for "docked" file in sysfs
508 */
509static ssize_t show_docked(struct device *dev,
510 struct device_attribute *attr, char *buf)
511{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600512 struct dock_station *dock_station = dev->platform_data;
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100513 struct acpi_device *adev = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800514
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100515 acpi_bus_get_device(dock_station->handle, &adev);
516 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800517}
Adrian Bunke5685b92007-10-24 18:24:42 +0200518static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800519
520/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700521 * show_flags - read method for flags file in sysfs
522 */
523static ssize_t show_flags(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600526 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700527 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
528
529}
Adrian Bunke5685b92007-10-24 18:24:42 +0200530static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700531
532/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800533 * write_undock - write method for "undock" file in sysfs
534 */
535static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
536 const char *buf, size_t count)
537{
538 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600539 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800540
541 if (!count)
542 return -EINVAL;
543
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200544 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100545 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800546 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200547 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800548 return ret ? ret: count;
549}
Adrian Bunke5685b92007-10-24 18:24:42 +0200550static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800551
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800552/*
553 * show_dock_uid - read method for "uid" file in sysfs
554 */
555static ssize_t show_dock_uid(struct device *dev,
556 struct device_attribute *attr, char *buf)
557{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400558 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600559 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700560 acpi_status status = acpi_evaluate_integer(dock_station->handle,
561 "_UID", NULL, &lbuf);
562 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800563 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700564
Matthew Wilcox27663c52008-10-10 02:22:59 -0400565 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800566}
Adrian Bunke5685b92007-10-24 18:24:42 +0200567static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800568
Shaohua Li8652b002008-08-28 10:07:45 +0800569static ssize_t show_dock_type(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600572 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800573 char *type;
574
575 if (dock_station->flags & DOCK_IS_DOCK)
576 type = "dock_station";
577 else if (dock_station->flags & DOCK_IS_ATA)
578 type = "ata_bay";
579 else if (dock_station->flags & DOCK_IS_BAT)
580 type = "battery_bay";
581 else
582 type = "unknown";
583
584 return snprintf(buf, PAGE_SIZE, "%s\n", type);
585}
586static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
587
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600588static struct attribute *dock_attributes[] = {
589 &dev_attr_docked.attr,
590 &dev_attr_flags.attr,
591 &dev_attr_undock.attr,
592 &dev_attr_uid.attr,
593 &dev_attr_type.attr,
594 NULL
595};
596
597static struct attribute_group dock_attribute_group = {
598 .attrs = dock_attributes
599};
600
Len Brownc8f7a62c2006-07-09 17:22:28 -0400601/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100602 * acpi_dock_add - Add a new dock station
603 * @adev: Dock station ACPI device object.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400604 *
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100605 * allocated and initialize a new dock station device.
Len Brownc8f7a62c2006-07-09 17:22:28 -0400606 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100607void acpi_dock_add(struct acpi_device *adev)
Len Brownc8f7a62c2006-07-09 17:22:28 -0400608{
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200609 struct dock_station *dock_station, ds = { NULL, };
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100610 struct platform_device_info pdevinfo;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100611 acpi_handle handle = adev->handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600612 struct platform_device *dd;
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200613 int ret;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400614
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100615 memset(&pdevinfo, 0, sizeof(pdevinfo));
616 pdevinfo.name = "dock";
617 pdevinfo.id = dock_station_count;
618 pdevinfo.acpi_node.companion = adev;
619 pdevinfo.data = &ds;
620 pdevinfo.size_data = sizeof(ds);
621 dd = platform_device_register_full(&pdevinfo);
Alex Chiang747479a2009-10-19 15:14:50 -0600622 if (IS_ERR(dd))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100623 return;
Alex Chiang9751cb72009-10-19 15:14:40 -0600624
Alex Chiang747479a2009-10-19 15:14:50 -0600625 dock_station = dd->dev.platform_data;
626
Len Brownc8f7a62c2006-07-09 17:22:28 -0400627 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600628 dock_station->dock_device = dd;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400629 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400630
Alex Chiang747479a2009-10-19 15:14:50 -0600631 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600632 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700633
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700634 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600635 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700636
Jiang Liuc9b54712013-06-29 00:24:42 +0800637 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800638 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800639 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800640 dock_station->flags |= DOCK_IS_ATA;
Rafael J. Wysockib43109f2014-02-16 00:09:34 +0100641 if (acpi_device_is_battery(adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800642 dock_station->flags |= DOCK_IS_BAT;
643
Alex Chiang747479a2009-10-19 15:14:50 -0600644 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800645 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600646 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800647
Len Brownc8f7a62c2006-07-09 17:22:28 -0400648 /* add the dock station as a device dependent on itself */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100649 ret = add_dock_dependent_device(dock_station, adev);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600650 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600651 goto err_rmgroup;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400652
Shaohua Lidb350b02008-08-28 10:03:58 +0800653 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600654 list_add(&dock_station->sibling, &dock_stations);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100655 adev->flags.is_dock_station = true;
656 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
657 dock_station_count);
658 return;
Len Brownc8f7a62c2006-07-09 17:22:28 -0400659
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600660err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600661 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Rafael J. Wysockif311e1c2014-02-21 01:11:38 +0100662
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600663err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600664 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000665 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400666}