blob: e8e128dede2997c1a2de8ca4f0352ee334ca8b9a [file] [log] [blame]
Len Brownc8f7a622006-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 Brownc8f7a622006-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 *
Len Brownc8f7a622006-07-09 17:22:28 -040020 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Len Brownc8f7a622006-07-09 17:22:28 -040026#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/notifier.h>
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080029#include <linux/platform_device.h>
Al Viro914e2632006-10-18 13:55:46 -040030#include <linux/jiffies.h>
Randy Dunlap62a6d7f2007-03-26 21:38:49 -080031#include <linux/stddef.h>
Toshi Kanicd730182012-11-20 23:42:30 +000032#include <linux/acpi.h>
Len Brownc8f7a622006-07-09 17:22:28 -040033
Rashika3f9eed52013-12-17 15:00:04 +053034#include "internal.h"
35
Len Brown7cda93e2007-02-12 23:50:02 -050036#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a622006-07-09 17:22:28 -040037
Len Brownf52fd662007-02-12 22:42:12 -050038ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040039MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050040MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -040041MODULE_LICENSE("GPL");
42
Rusty Russell90ab5ee2012-01-13 09:32:20 +103043static bool immediate_undock = 1;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070044module_param(immediate_undock, bool, 0644);
45MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
46 "undock immediately when the undock button is pressed, 0 will cause"
47 " the driver to wait for userspace to write the undock sysfs file "
48 " before undocking");
49
Len Brownc8f7a622006-07-09 17:22:28 -040050struct dock_station {
51 acpi_handle handle;
52 unsigned long last_dock_time;
53 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040054 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080055
Alex Chiang50d716e2009-10-01 11:59:23 -060056 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080057 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040058};
Shaohua Lidb350b02008-08-28 10:03:58 +080059static LIST_HEAD(dock_stations);
60static int dock_station_count;
Len Brownc8f7a622006-07-09 17:22:28 -040061
62struct dock_dependent_device {
63 struct list_head list;
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010064 struct acpi_device *adev;
Len Brownc8f7a622006-07-09 17:22:28 -040065};
66
67#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070068#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080069#define DOCK_IS_DOCK 0x00000010
70#define DOCK_IS_ATA 0x00000020
71#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070072#define DOCK_EVENT 3
73#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040074
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020075enum dock_callback_type {
76 DOCK_CALL_HANDLER,
77 DOCK_CALL_FIXUP,
78 DOCK_CALL_UEVENT,
79};
80
Len Brownc8f7a622006-07-09 17:22:28 -040081/*****************************************************************************
82 * Dock Dependent device functions *
83 *****************************************************************************/
84/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060085 * add_dock_dependent_device - associate a device with the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010086 * @ds: Dock station.
87 * @adev: Dependent ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -040088 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -060089 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -040090 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010091static int add_dock_dependent_device(struct dock_station *ds,
92 struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -040093{
94 struct dock_dependent_device *dd;
95
96 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -060097 if (!dd)
98 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -040099
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100100 dd->adev = adev;
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600101 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -0400102 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600103
104 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400105}
106
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200107static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200108 enum dock_callback_type cb_type)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200109{
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100110 struct acpi_device *adev = dd->adev;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200111
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100112 acpi_lock_hp_context();
113
114 if (!adev->hp)
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100115 goto out;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100116
117 if (cb_type == DOCK_CALL_FIXUP) {
118 void (*fixup)(struct acpi_device *);
119
120 fixup = adev->hp->fixup;
121 if (fixup) {
122 acpi_unlock_hp_context();
123 fixup(adev);
124 return;
125 }
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100126 } else if (cb_type == DOCK_CALL_UEVENT) {
127 void (*uevent)(struct acpi_device *, u32);
128
129 uevent = adev->hp->uevent;
130 if (uevent) {
131 acpi_unlock_hp_context();
132 uevent(adev, event);
133 return;
134 }
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100135 } else {
136 int (*notify)(struct acpi_device *, u32);
137
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100138 notify = adev->hp->notify;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100139 if (notify) {
140 acpi_unlock_hp_context();
141 notify(adev, event);
142 return;
143 }
144 }
145
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100146 out:
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100147 acpi_unlock_hp_context();
Len Brownc8f7a622006-07-09 17:22:28 -0400148}
149
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100150static struct dock_station *find_dock_station(acpi_handle handle)
151{
152 struct dock_station *ds;
153
154 list_for_each_entry(ds, &dock_stations, sibling)
155 if (ds->handle == handle)
156 return ds;
157
158 return NULL;
159}
160
Len Brownc8f7a622006-07-09 17:22:28 -0400161/**
162 * find_dock_dependent_device - get a device dependent on this dock
163 * @ds: the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100164 * @adev: ACPI device object to find.
Len Brownc8f7a622006-07-09 17:22:28 -0400165 *
166 * iterate over the dependent device list for this dock. If the
167 * dependent device matches the handle, return.
168 */
169static struct dock_dependent_device *
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100170find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400171{
172 struct dock_dependent_device *dd;
173
Jiang Liued633e72013-06-29 00:24:35 +0800174 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100175 if (adev == dd->adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400176 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800177
Len Brownc8f7a622006-07-09 17:22:28 -0400178 return NULL;
179}
180
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100181void register_dock_dependent_device(struct acpi_device *adev,
182 acpi_handle dshandle)
183{
184 struct dock_station *ds = find_dock_station(dshandle);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100185
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100186 if (ds && !find_dock_dependent_device(ds, adev))
187 add_dock_dependent_device(ds, adev);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100188}
189
Len Brownc8f7a622006-07-09 17:22:28 -0400190/*****************************************************************************
191 * Dock functions *
192 *****************************************************************************/
Shaohua Lidb350b02008-08-28 10:03:58 +0800193
Len Brownc8f7a622006-07-09 17:22:28 -0400194/**
195 * is_dock_device - see if a device is on a dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100196 * @adev: ACPI device object to check.
Len Brownc8f7a622006-07-09 17:22:28 -0400197 *
198 * If this device is either the dock station itself,
199 * or is a device dependent on the dock station, then it
200 * is a dock device
201 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100202int is_dock_device(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400203{
Shaohua Lidb350b02008-08-28 10:03:58 +0800204 struct dock_station *dock_station;
205
206 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400207 return 0;
208
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100209 if (acpi_dock_match(adev->handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400210 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600211
212 list_for_each_entry(dock_station, &dock_stations, sibling)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100213 if (find_dock_dependent_device(dock_station, adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800214 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400215
216 return 0;
217}
Len Brownc8f7a622006-07-09 17:22:28 -0400218EXPORT_SYMBOL_GPL(is_dock_device);
219
220/**
221 * dock_present - see if the dock station is present.
222 * @ds: the dock station
223 *
224 * execute the _STA method. note that present does not
225 * imply that we are docked.
226 */
227static int dock_present(struct dock_station *ds)
228{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400229 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400230 acpi_status status;
231
232 if (ds) {
233 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
234 if (ACPI_SUCCESS(status) && sta)
235 return 1;
236 }
237 return 0;
238}
239
Len Brownc8f7a622006-07-09 17:22:28 -0400240/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200241 * hot_remove_dock_devices - Remove dock station devices.
242 * @ds: Dock station.
243 */
244static void hot_remove_dock_devices(struct dock_station *ds)
245{
246 struct dock_dependent_device *dd;
247
248 /*
249 * Walk the list in reverse order so that devices that have been added
250 * last are removed first (in case there are some indirect dependencies
251 * between them).
252 */
253 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
254 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
255
256 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100257 acpi_bus_trim(dd->adev);
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200258}
259
260/**
261 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a622006-07-09 17:22:28 -0400262 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200263 * @event: either bus check or device check request
Len Brownc8f7a622006-07-09 17:22:28 -0400264 *
265 * Some devices on the dock station need to have drivers called
266 * to perform hotplug operations after a dock event has occurred.
267 * Traverse the list of dock devices that have registered a
268 * hotplug handler, and call the handler.
269 */
270static void hotplug_dock_devices(struct dock_station *ds, u32 event)
271{
272 struct dock_dependent_device *dd;
273
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200274 /* Call driver specific post-dock fixups. */
275 list_for_each_entry(dd, &ds->dependent_devices, list)
276 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
277
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200278 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200279 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200280 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
Len Brownc8f7a622006-07-09 17:22:28 -0400281
282 /*
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100283 * Check if all devices have been enumerated already. If not, run
284 * acpi_bus_scan() for them and that will cause scan handlers to be
285 * attached to device objects or acpi_drivers to be stopped/started if
286 * they are present.
Len Brownc8f7a622006-07-09 17:22:28 -0400287 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100288 list_for_each_entry(dd, &ds->dependent_devices, list) {
289 struct acpi_device *adev = dd->adev;
290
291 if (!acpi_device_enumerated(adev)) {
292 int ret = acpi_bus_scan(adev->handle);
293 if (ret)
294 dev_dbg(&adev->dev, "scan error %d\n", -ret);
295 }
296 }
Len Brownc8f7a622006-07-09 17:22:28 -0400297}
298
299static void dock_event(struct dock_station *ds, u32 event, int num)
300{
Shaohua Lidb350b02008-08-28 10:03:58 +0800301 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700302 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700303 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800304 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700305
306 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700307 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700308 else
Holger Macht66b56822007-08-10 13:10:32 -0700309 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700310
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700311 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800312 * Indicate that the status of the dock station has
313 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700314 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800315 if (num == DOCK_EVENT)
316 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
317
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200318 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200319 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
Alex Chiang747479a2009-10-19 15:14:50 -0600320
Shaohua Li1253f7a2008-08-28 10:06:16 +0800321 if (num != DOCK_EVENT)
322 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400323}
324
325/**
Len Brownc8f7a622006-07-09 17:22:28 -0400326 * handle_dock - handle a dock event
327 * @ds: the dock station
328 * @dock: to dock, or undock - that is the question
329 *
330 * Execute the _DCK method in response to an acpi event
331 */
332static void handle_dock(struct dock_station *ds, int dock)
333{
334 acpi_status status;
335 struct acpi_object_list arg_list;
336 union acpi_object arg;
Zhang Rui6a868e12013-09-03 08:32:10 +0800337 unsigned long long value;
Len Brownc8f7a622006-07-09 17:22:28 -0400338
Toshi Kanicd730182012-11-20 23:42:30 +0000339 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400340
341 /* _DCK method has one argument */
342 arg_list.count = 1;
343 arg_list.pointer = &arg;
344 arg.type = ACPI_TYPE_INTEGER;
345 arg.integer.value = dock;
Zhang Rui6a868e12013-09-03 08:32:10 +0800346 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
Shaohua Lidb350b02008-08-28 10:03:58 +0800347 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000348 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
349 status);
Len Brownc8f7a622006-07-09 17:22:28 -0400350}
351
352static inline void dock(struct dock_station *ds)
353{
354 handle_dock(ds, 1);
355}
356
357static inline void undock(struct dock_station *ds)
358{
359 handle_dock(ds, 0);
360}
361
362static inline void begin_dock(struct dock_station *ds)
363{
364 ds->flags |= DOCK_DOCKING;
365}
366
367static inline void complete_dock(struct dock_station *ds)
368{
369 ds->flags &= ~(DOCK_DOCKING);
370 ds->last_dock_time = jiffies;
371}
372
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700373static inline void begin_undock(struct dock_station *ds)
374{
375 ds->flags |= DOCK_UNDOCKING;
376}
377
378static inline void complete_undock(struct dock_station *ds)
379{
380 ds->flags &= ~(DOCK_UNDOCKING);
381}
382
Len Brownc8f7a622006-07-09 17:22:28 -0400383/**
384 * dock_in_progress - see if we are in the middle of handling a dock event
385 * @ds: the dock station
386 *
387 * Sometimes while docking, false dock events can be sent to the driver
388 * because good connections aren't made or some other reason. Ignore these
389 * if we are in the middle of doing something.
390 */
391static int dock_in_progress(struct dock_station *ds)
392{
393 if ((ds->flags & DOCK_DOCKING) ||
394 time_before(jiffies, (ds->last_dock_time + HZ)))
395 return 1;
396 return 0;
397}
398
399/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800400 * handle_eject_request - handle an undock request checking for error conditions
401 *
402 * Check to make sure the dock device is still present, then undock and
403 * hotremove all the devices that may need removing.
404 */
405static int handle_eject_request(struct dock_station *ds, u32 event)
406{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800407 if (dock_in_progress(ds))
408 return -EBUSY;
409
410 /*
411 * here we need to generate the undock
412 * event prior to actually doing the undock
413 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200414 * Also, even send the dock event if the
415 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800416 */
417 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200418
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200419 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800420 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800421 acpi_evaluate_lck(ds->handle, 0);
422 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800423 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000424 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800425 return -EBUSY;
426 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700427 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800428 return 0;
429}
430
431/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100432 * dock_notify - Handle ACPI dock notification.
433 * @adev: Dock station's ACPI device object.
434 * @event: Event code.
Len Brownc8f7a622006-07-09 17:22:28 -0400435 *
436 * If we are notified to dock, then check to see if the dock is
437 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800438 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400439 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100440int dock_notify(struct acpi_device *adev, u32 event)
Len Brownc8f7a622006-07-09 17:22:28 -0400441{
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100442 acpi_handle handle = adev->handle;
443 struct dock_station *ds = find_dock_station(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800444 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400445
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100446 if (!ds)
447 return -ENODEV;
448
Shaohua Lidb350b02008-08-28 10:03:58 +0800449 /*
450 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
451 * is sent and _DCK is present, it is assumed to mean an undock
452 * request.
453 */
454 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
455 event = ACPI_NOTIFY_EJECT_REQUEST;
456
457 /*
458 * dock station: BUS_CHECK - docked or surprise removal
459 * DEVICE_CHECK - undocked
460 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
461 *
462 * To simplify event handling, dock dependent device handler always
463 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
464 * ACPI_NOTIFY_EJECT_REQUEST for removal
465 */
Len Brownc8f7a622006-07-09 17:22:28 -0400466 switch (event) {
467 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800468 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki0a8e5c32014-02-10 13:44:20 +0100469 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400470 begin_dock(ds);
471 dock(ds);
472 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000473 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800474 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400475 break;
476 }
Len Brownc8f7a622006-07-09 17:22:28 -0400477 hotplug_dock_devices(ds, event);
478 complete_dock(ds);
479 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800480 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800481 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800482 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400483 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800484 if (dock_present(ds) || dock_in_progress(ds))
485 break;
486 /* This is a surprise removal */
487 surprise_removal = 1;
488 event = ACPI_NOTIFY_EJECT_REQUEST;
489 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400490 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700491 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800492 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
493 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700494 handle_eject_request(ds, event);
495 else
496 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400497 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400498 }
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100499 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400500}
501
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800502/*
503 * show_docked - read method for "docked" file in sysfs
504 */
505static ssize_t show_docked(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600508 struct dock_station *dock_station = dev->platform_data;
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100509 struct acpi_device *adev = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800510
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100511 acpi_bus_get_device(dock_station->handle, &adev);
512 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800513}
Adrian Bunke5685b92007-10-24 18:24:42 +0200514static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800515
516/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700517 * show_flags - read method for flags file in sysfs
518 */
519static ssize_t show_flags(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600522 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700523 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
524
525}
Adrian Bunke5685b92007-10-24 18:24:42 +0200526static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700527
528/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800529 * write_undock - write method for "undock" file in sysfs
530 */
531static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
532 const char *buf, size_t count)
533{
534 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600535 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800536
537 if (!count)
538 return -EINVAL;
539
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200540 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100541 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800542 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200543 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800544 return ret ? ret: count;
545}
Adrian Bunke5685b92007-10-24 18:24:42 +0200546static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800547
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800548/*
549 * show_dock_uid - read method for "uid" file in sysfs
550 */
551static ssize_t show_dock_uid(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400554 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600555 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700556 acpi_status status = acpi_evaluate_integer(dock_station->handle,
557 "_UID", NULL, &lbuf);
558 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800559 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700560
Matthew Wilcox27663c52008-10-10 02:22:59 -0400561 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800562}
Adrian Bunke5685b92007-10-24 18:24:42 +0200563static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800564
Shaohua Li8652b002008-08-28 10:07:45 +0800565static ssize_t show_dock_type(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600568 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800569 char *type;
570
571 if (dock_station->flags & DOCK_IS_DOCK)
572 type = "dock_station";
573 else if (dock_station->flags & DOCK_IS_ATA)
574 type = "ata_bay";
575 else if (dock_station->flags & DOCK_IS_BAT)
576 type = "battery_bay";
577 else
578 type = "unknown";
579
580 return snprintf(buf, PAGE_SIZE, "%s\n", type);
581}
582static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
583
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600584static struct attribute *dock_attributes[] = {
585 &dev_attr_docked.attr,
586 &dev_attr_flags.attr,
587 &dev_attr_undock.attr,
588 &dev_attr_uid.attr,
589 &dev_attr_type.attr,
590 NULL
591};
592
593static struct attribute_group dock_attribute_group = {
594 .attrs = dock_attributes
595};
596
Len Brownc8f7a622006-07-09 17:22:28 -0400597/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100598 * acpi_dock_add - Add a new dock station
599 * @adev: Dock station ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -0400600 *
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100601 * allocated and initialize a new dock station device.
Len Brownc8f7a622006-07-09 17:22:28 -0400602 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100603void acpi_dock_add(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400604{
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200605 struct dock_station *dock_station, ds = { NULL, };
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100606 struct platform_device_info pdevinfo;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100607 acpi_handle handle = adev->handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600608 struct platform_device *dd;
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200609 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400610
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100611 memset(&pdevinfo, 0, sizeof(pdevinfo));
612 pdevinfo.name = "dock";
613 pdevinfo.id = dock_station_count;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100614 pdevinfo.fwnode = acpi_fwnode_handle(adev);
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100615 pdevinfo.data = &ds;
616 pdevinfo.size_data = sizeof(ds);
617 dd = platform_device_register_full(&pdevinfo);
Alex Chiang747479a2009-10-19 15:14:50 -0600618 if (IS_ERR(dd))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100619 return;
Alex Chiang9751cb72009-10-19 15:14:40 -0600620
Alex Chiang747479a2009-10-19 15:14:50 -0600621 dock_station = dd->dev.platform_data;
622
Len Brownc8f7a622006-07-09 17:22:28 -0400623 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600624 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400625 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400626
Alex Chiang747479a2009-10-19 15:14:50 -0600627 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600628 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700629
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700630 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600631 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700632
Jiang Liuc9b54712013-06-29 00:24:42 +0800633 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800634 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800635 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800636 dock_station->flags |= DOCK_IS_ATA;
Rafael J. Wysockib43109f2014-02-16 00:09:34 +0100637 if (acpi_device_is_battery(adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800638 dock_station->flags |= DOCK_IS_BAT;
639
Alex Chiang747479a2009-10-19 15:14:50 -0600640 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800641 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600642 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800643
Len Brownc8f7a622006-07-09 17:22:28 -0400644 /* add the dock station as a device dependent on itself */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100645 ret = add_dock_dependent_device(dock_station, adev);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600646 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600647 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400648
Shaohua Lidb350b02008-08-28 10:03:58 +0800649 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600650 list_add(&dock_station->sibling, &dock_stations);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100651 adev->flags.is_dock_station = true;
652 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
653 dock_station_count);
654 return;
Len Brownc8f7a622006-07-09 17:22:28 -0400655
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600656err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600657 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Rafael J. Wysockif311e1c2014-02-21 01:11:38 +0100658
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600659err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600660 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000661 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400662}