blob: e3414131bfcab782c7d65853429cd5a2ae6094f4 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Len Brownc8f7a622006-07-09 17:22:28 -04002/*
3 * dock.c - ACPI dock station driver
4 *
Rafael J. Wysocki8cc25682014-02-21 01:11:46 +01005 * Copyright (C) 2006, 2014, Intel Corp.
6 * Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
7 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Len Brownc8f7a622006-07-09 17:22:28 -04008 */
9
10#include <linux/kernel.h>
Paul Gortmaker0f093e92016-07-11 16:23:26 -040011#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Len Brownc8f7a622006-07-09 17:22:28 -040013#include <linux/init.h>
14#include <linux/types.h>
15#include <linux/notifier.h>
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080016#include <linux/platform_device.h>
Al Viro914e2632006-10-18 13:55:46 -040017#include <linux/jiffies.h>
Randy Dunlap62a6d7f2007-03-26 21:38:49 -080018#include <linux/stddef.h>
Toshi Kanicd730182012-11-20 23:42:30 +000019#include <linux/acpi.h>
Len Brownc8f7a622006-07-09 17:22:28 -040020
Rashika3f9eed52013-12-17 15:00:04 +053021#include "internal.h"
22
Len Brownf52fd662007-02-12 22:42:12 -050023ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040024
Rusty Russell90ab5ee2012-01-13 09:32:20 +103025static bool immediate_undock = 1;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070026module_param(immediate_undock, bool, 0644);
27MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
28 "undock immediately when the undock button is pressed, 0 will cause"
29 " the driver to wait for userspace to write the undock sysfs file "
30 " before undocking");
31
Len Brownc8f7a622006-07-09 17:22:28 -040032struct dock_station {
33 acpi_handle handle;
34 unsigned long last_dock_time;
35 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040036 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080037
Alex Chiang50d716e2009-10-01 11:59:23 -060038 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080039 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040040};
Shaohua Lidb350b02008-08-28 10:03:58 +080041static LIST_HEAD(dock_stations);
42static int dock_station_count;
Len Brownc8f7a622006-07-09 17:22:28 -040043
44struct dock_dependent_device {
45 struct list_head list;
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010046 struct acpi_device *adev;
Len Brownc8f7a622006-07-09 17:22:28 -040047};
48
49#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070050#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080051#define DOCK_IS_DOCK 0x00000010
52#define DOCK_IS_ATA 0x00000020
53#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070054#define DOCK_EVENT 3
55#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040056
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020057enum dock_callback_type {
58 DOCK_CALL_HANDLER,
59 DOCK_CALL_FIXUP,
60 DOCK_CALL_UEVENT,
61};
62
Len Brownc8f7a622006-07-09 17:22:28 -040063/*****************************************************************************
64 * Dock Dependent device functions *
65 *****************************************************************************/
66/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060067 * add_dock_dependent_device - associate a device with the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010068 * @ds: Dock station.
69 * @adev: Dependent ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -040070 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -060071 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -040072 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010073static int add_dock_dependent_device(struct dock_station *ds,
74 struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -040075{
76 struct dock_dependent_device *dd;
77
78 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -060079 if (!dd)
80 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -040081
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010082 dd->adev = adev;
Alex Chiangf69cfdd2009-10-19 15:14:29 -060083 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -040084 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -060085
86 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -040087}
88
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020089static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020090 enum dock_callback_type cb_type)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020091{
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +010092 struct acpi_device *adev = dd->adev;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020093
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +010094 acpi_lock_hp_context();
95
96 if (!adev->hp)
Rafael J. Wysocki2f168172014-02-21 01:11:30 +010097 goto out;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +010098
99 if (cb_type == DOCK_CALL_FIXUP) {
100 void (*fixup)(struct acpi_device *);
101
102 fixup = adev->hp->fixup;
103 if (fixup) {
104 acpi_unlock_hp_context();
105 fixup(adev);
106 return;
107 }
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100108 } else if (cb_type == DOCK_CALL_UEVENT) {
109 void (*uevent)(struct acpi_device *, u32);
110
111 uevent = adev->hp->uevent;
112 if (uevent) {
113 acpi_unlock_hp_context();
114 uevent(adev, event);
115 return;
116 }
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100117 } else {
118 int (*notify)(struct acpi_device *, u32);
119
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100120 notify = adev->hp->notify;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100121 if (notify) {
122 acpi_unlock_hp_context();
123 notify(adev, event);
124 return;
125 }
126 }
127
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100128 out:
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100129 acpi_unlock_hp_context();
Len Brownc8f7a622006-07-09 17:22:28 -0400130}
131
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100132static struct dock_station *find_dock_station(acpi_handle handle)
133{
134 struct dock_station *ds;
135
136 list_for_each_entry(ds, &dock_stations, sibling)
137 if (ds->handle == handle)
138 return ds;
139
140 return NULL;
141}
142
Len Brownc8f7a622006-07-09 17:22:28 -0400143/**
144 * find_dock_dependent_device - get a device dependent on this dock
145 * @ds: the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100146 * @adev: ACPI device object to find.
Len Brownc8f7a622006-07-09 17:22:28 -0400147 *
148 * iterate over the dependent device list for this dock. If the
149 * dependent device matches the handle, return.
150 */
151static struct dock_dependent_device *
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100152find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400153{
154 struct dock_dependent_device *dd;
155
Jiang Liued633e72013-06-29 00:24:35 +0800156 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100157 if (adev == dd->adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400158 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800159
Len Brownc8f7a622006-07-09 17:22:28 -0400160 return NULL;
161}
162
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100163void register_dock_dependent_device(struct acpi_device *adev,
164 acpi_handle dshandle)
165{
166 struct dock_station *ds = find_dock_station(dshandle);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100167
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100168 if (ds && !find_dock_dependent_device(ds, adev))
169 add_dock_dependent_device(ds, adev);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100170}
171
Len Brownc8f7a622006-07-09 17:22:28 -0400172/*****************************************************************************
173 * Dock functions *
174 *****************************************************************************/
Shaohua Lidb350b02008-08-28 10:03:58 +0800175
Len Brownc8f7a622006-07-09 17:22:28 -0400176/**
177 * is_dock_device - see if a device is on a dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100178 * @adev: ACPI device object to check.
Len Brownc8f7a622006-07-09 17:22:28 -0400179 *
180 * If this device is either the dock station itself,
181 * or is a device dependent on the dock station, then it
182 * is a dock device
183 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100184int is_dock_device(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400185{
Shaohua Lidb350b02008-08-28 10:03:58 +0800186 struct dock_station *dock_station;
187
188 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400189 return 0;
190
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100191 if (acpi_dock_match(adev->handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400192 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600193
194 list_for_each_entry(dock_station, &dock_stations, sibling)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100195 if (find_dock_dependent_device(dock_station, adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800196 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400197
198 return 0;
199}
Len Brownc8f7a622006-07-09 17:22:28 -0400200EXPORT_SYMBOL_GPL(is_dock_device);
201
202/**
203 * dock_present - see if the dock station is present.
204 * @ds: the dock station
205 *
206 * execute the _STA method. note that present does not
207 * imply that we are docked.
208 */
209static int dock_present(struct dock_station *ds)
210{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400211 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400212 acpi_status status;
213
214 if (ds) {
215 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
216 if (ACPI_SUCCESS(status) && sta)
217 return 1;
218 }
219 return 0;
220}
221
Len Brownc8f7a622006-07-09 17:22:28 -0400222/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200223 * hot_remove_dock_devices - Remove dock station devices.
224 * @ds: Dock station.
225 */
226static void hot_remove_dock_devices(struct dock_station *ds)
227{
228 struct dock_dependent_device *dd;
229
230 /*
231 * Walk the list in reverse order so that devices that have been added
232 * last are removed first (in case there are some indirect dependencies
233 * between them).
234 */
235 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
236 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
237
238 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100239 acpi_bus_trim(dd->adev);
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200240}
241
242/**
243 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a622006-07-09 17:22:28 -0400244 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200245 * @event: either bus check or device check request
Len Brownc8f7a622006-07-09 17:22:28 -0400246 *
247 * Some devices on the dock station need to have drivers called
248 * to perform hotplug operations after a dock event has occurred.
249 * Traverse the list of dock devices that have registered a
250 * hotplug handler, and call the handler.
251 */
252static void hotplug_dock_devices(struct dock_station *ds, u32 event)
253{
254 struct dock_dependent_device *dd;
255
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200256 /* Call driver specific post-dock fixups. */
257 list_for_each_entry(dd, &ds->dependent_devices, list)
258 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
259
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200260 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200261 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200262 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
Len Brownc8f7a622006-07-09 17:22:28 -0400263
264 /*
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100265 * Check if all devices have been enumerated already. If not, run
266 * acpi_bus_scan() for them and that will cause scan handlers to be
267 * attached to device objects or acpi_drivers to be stopped/started if
268 * they are present.
Len Brownc8f7a622006-07-09 17:22:28 -0400269 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100270 list_for_each_entry(dd, &ds->dependent_devices, list) {
271 struct acpi_device *adev = dd->adev;
272
273 if (!acpi_device_enumerated(adev)) {
274 int ret = acpi_bus_scan(adev->handle);
275 if (ret)
276 dev_dbg(&adev->dev, "scan error %d\n", -ret);
277 }
278 }
Len Brownc8f7a622006-07-09 17:22:28 -0400279}
280
281static void dock_event(struct dock_station *ds, u32 event, int num)
282{
Shaohua Lidb350b02008-08-28 10:03:58 +0800283 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700284 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700285 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800286 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700287
288 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700289 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700290 else
Holger Macht66b56822007-08-10 13:10:32 -0700291 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700292
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700293 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800294 * Indicate that the status of the dock station has
295 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700296 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800297 if (num == DOCK_EVENT)
298 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
299
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200300 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200301 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
Alex Chiang747479a2009-10-19 15:14:50 -0600302
Shaohua Li1253f7a2008-08-28 10:06:16 +0800303 if (num != DOCK_EVENT)
304 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400305}
306
307/**
Len Brownc8f7a622006-07-09 17:22:28 -0400308 * handle_dock - handle a dock event
309 * @ds: the dock station
310 * @dock: to dock, or undock - that is the question
311 *
312 * Execute the _DCK method in response to an acpi event
313 */
314static void handle_dock(struct dock_station *ds, int dock)
315{
316 acpi_status status;
317 struct acpi_object_list arg_list;
318 union acpi_object arg;
Zhang Rui6a868e12013-09-03 08:32:10 +0800319 unsigned long long value;
Len Brownc8f7a622006-07-09 17:22:28 -0400320
Toshi Kanicd730182012-11-20 23:42:30 +0000321 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400322
323 /* _DCK method has one argument */
324 arg_list.count = 1;
325 arg_list.pointer = &arg;
326 arg.type = ACPI_TYPE_INTEGER;
327 arg.integer.value = dock;
Zhang Rui6a868e12013-09-03 08:32:10 +0800328 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
Shaohua Lidb350b02008-08-28 10:03:58 +0800329 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000330 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
331 status);
Len Brownc8f7a622006-07-09 17:22:28 -0400332}
333
334static inline void dock(struct dock_station *ds)
335{
336 handle_dock(ds, 1);
337}
338
339static inline void undock(struct dock_station *ds)
340{
341 handle_dock(ds, 0);
342}
343
344static inline void begin_dock(struct dock_station *ds)
345{
346 ds->flags |= DOCK_DOCKING;
347}
348
349static inline void complete_dock(struct dock_station *ds)
350{
351 ds->flags &= ~(DOCK_DOCKING);
352 ds->last_dock_time = jiffies;
353}
354
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700355static inline void begin_undock(struct dock_station *ds)
356{
357 ds->flags |= DOCK_UNDOCKING;
358}
359
360static inline void complete_undock(struct dock_station *ds)
361{
362 ds->flags &= ~(DOCK_UNDOCKING);
363}
364
Len Brownc8f7a622006-07-09 17:22:28 -0400365/**
366 * dock_in_progress - see if we are in the middle of handling a dock event
367 * @ds: the dock station
368 *
369 * Sometimes while docking, false dock events can be sent to the driver
370 * because good connections aren't made or some other reason. Ignore these
371 * if we are in the middle of doing something.
372 */
373static int dock_in_progress(struct dock_station *ds)
374{
375 if ((ds->flags & DOCK_DOCKING) ||
376 time_before(jiffies, (ds->last_dock_time + HZ)))
377 return 1;
378 return 0;
379}
380
381/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800382 * handle_eject_request - handle an undock request checking for error conditions
383 *
384 * Check to make sure the dock device is still present, then undock and
385 * hotremove all the devices that may need removing.
386 */
387static int handle_eject_request(struct dock_station *ds, u32 event)
388{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800389 if (dock_in_progress(ds))
390 return -EBUSY;
391
392 /*
393 * here we need to generate the undock
394 * event prior to actually doing the undock
395 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200396 * Also, even send the dock event if the
397 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800398 */
399 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200400
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200401 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800402 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800403 acpi_evaluate_lck(ds->handle, 0);
404 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800405 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000406 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800407 return -EBUSY;
408 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700409 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800410 return 0;
411}
412
413/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100414 * dock_notify - Handle ACPI dock notification.
415 * @adev: Dock station's ACPI device object.
416 * @event: Event code.
Len Brownc8f7a622006-07-09 17:22:28 -0400417 *
418 * If we are notified to dock, then check to see if the dock is
419 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800420 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400421 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100422int dock_notify(struct acpi_device *adev, u32 event)
Len Brownc8f7a622006-07-09 17:22:28 -0400423{
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100424 acpi_handle handle = adev->handle;
425 struct dock_station *ds = find_dock_station(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800426 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400427
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100428 if (!ds)
429 return -ENODEV;
430
Shaohua Lidb350b02008-08-28 10:03:58 +0800431 /*
432 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
433 * is sent and _DCK is present, it is assumed to mean an undock
434 * request.
435 */
436 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
437 event = ACPI_NOTIFY_EJECT_REQUEST;
438
439 /*
440 * dock station: BUS_CHECK - docked or surprise removal
441 * DEVICE_CHECK - undocked
442 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
443 *
444 * To simplify event handling, dock dependent device handler always
445 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
446 * ACPI_NOTIFY_EJECT_REQUEST for removal
447 */
Len Brownc8f7a622006-07-09 17:22:28 -0400448 switch (event) {
449 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800450 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki0a8e5c32014-02-10 13:44:20 +0100451 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400452 begin_dock(ds);
453 dock(ds);
454 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000455 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800456 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400457 break;
458 }
Len Brownc8f7a622006-07-09 17:22:28 -0400459 hotplug_dock_devices(ds, event);
460 complete_dock(ds);
461 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800462 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800463 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800464 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400465 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800466 if (dock_present(ds) || dock_in_progress(ds))
467 break;
468 /* This is a surprise removal */
469 surprise_removal = 1;
470 event = ACPI_NOTIFY_EJECT_REQUEST;
471 /* Fall back */
Gustavo A. R. Silva1be9c3a2017-10-12 13:49:18 -0500472 /* fall through */
Len Brownc8f7a622006-07-09 17:22:28 -0400473 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700474 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800475 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
476 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700477 handle_eject_request(ds, event);
478 else
479 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400480 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400481 }
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100482 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400483}
484
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800485/*
486 * show_docked - read method for "docked" file in sysfs
487 */
488static ssize_t show_docked(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600491 struct dock_station *dock_station = dev->platform_data;
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100492 struct acpi_device *adev = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800493
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100494 acpi_bus_get_device(dock_station->handle, &adev);
495 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800496}
Adrian Bunke5685b92007-10-24 18:24:42 +0200497static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800498
499/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700500 * show_flags - read method for flags file in sysfs
501 */
502static ssize_t show_flags(struct device *dev,
503 struct device_attribute *attr, char *buf)
504{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600505 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700506 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
507
508}
Adrian Bunke5685b92007-10-24 18:24:42 +0200509static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700510
511/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800512 * write_undock - write method for "undock" file in sysfs
513 */
514static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
515 const char *buf, size_t count)
516{
517 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600518 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800519
520 if (!count)
521 return -EINVAL;
522
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200523 acpi_scan_lock_acquire();
Holger Macht9171f8342008-03-12 01:07:27 +0100524 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800525 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200526 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800527 return ret ? ret: count;
528}
Adrian Bunke5685b92007-10-24 18:24:42 +0200529static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800530
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800531/*
532 * show_dock_uid - read method for "uid" file in sysfs
533 */
534static ssize_t show_dock_uid(struct device *dev,
535 struct device_attribute *attr, char *buf)
536{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400537 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600538 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700539 acpi_status status = acpi_evaluate_integer(dock_station->handle,
540 "_UID", NULL, &lbuf);
541 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800542 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700543
Matthew Wilcox27663c52008-10-10 02:22:59 -0400544 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800545}
Adrian Bunke5685b92007-10-24 18:24:42 +0200546static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800547
Shaohua Li8652b002008-08-28 10:07:45 +0800548static ssize_t show_dock_type(struct device *dev,
549 struct device_attribute *attr, char *buf)
550{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600551 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800552 char *type;
553
554 if (dock_station->flags & DOCK_IS_DOCK)
555 type = "dock_station";
556 else if (dock_station->flags & DOCK_IS_ATA)
557 type = "ata_bay";
558 else if (dock_station->flags & DOCK_IS_BAT)
559 type = "battery_bay";
560 else
561 type = "unknown";
562
563 return snprintf(buf, PAGE_SIZE, "%s\n", type);
564}
565static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
566
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600567static struct attribute *dock_attributes[] = {
568 &dev_attr_docked.attr,
569 &dev_attr_flags.attr,
570 &dev_attr_undock.attr,
571 &dev_attr_uid.attr,
572 &dev_attr_type.attr,
573 NULL
574};
575
Amitoj Kaur Chawla19dc7132017-08-01 20:28:46 -0400576static const struct attribute_group dock_attribute_group = {
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600577 .attrs = dock_attributes
578};
579
Len Brownc8f7a622006-07-09 17:22:28 -0400580/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100581 * acpi_dock_add - Add a new dock station
582 * @adev: Dock station ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -0400583 *
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100584 * allocated and initialize a new dock station device.
Len Brownc8f7a622006-07-09 17:22:28 -0400585 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100586void acpi_dock_add(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400587{
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200588 struct dock_station *dock_station, ds = { NULL, };
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100589 struct platform_device_info pdevinfo;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100590 acpi_handle handle = adev->handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600591 struct platform_device *dd;
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200592 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400593
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100594 memset(&pdevinfo, 0, sizeof(pdevinfo));
595 pdevinfo.name = "dock";
596 pdevinfo.id = dock_station_count;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100597 pdevinfo.fwnode = acpi_fwnode_handle(adev);
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100598 pdevinfo.data = &ds;
599 pdevinfo.size_data = sizeof(ds);
600 dd = platform_device_register_full(&pdevinfo);
Alex Chiang747479a2009-10-19 15:14:50 -0600601 if (IS_ERR(dd))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100602 return;
Alex Chiang9751cb72009-10-19 15:14:40 -0600603
Alex Chiang747479a2009-10-19 15:14:50 -0600604 dock_station = dd->dev.platform_data;
605
Len Brownc8f7a622006-07-09 17:22:28 -0400606 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600607 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400608 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400609
Alex Chiang747479a2009-10-19 15:14:50 -0600610 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600611 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700612
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700613 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600614 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700615
Jiang Liuc9b54712013-06-29 00:24:42 +0800616 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800617 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800618 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800619 dock_station->flags |= DOCK_IS_ATA;
Rafael J. Wysockib43109f2014-02-16 00:09:34 +0100620 if (acpi_device_is_battery(adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800621 dock_station->flags |= DOCK_IS_BAT;
622
Alex Chiang747479a2009-10-19 15:14:50 -0600623 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800624 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600625 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800626
Len Brownc8f7a622006-07-09 17:22:28 -0400627 /* add the dock station as a device dependent on itself */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100628 ret = add_dock_dependent_device(dock_station, adev);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600629 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600630 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400631
Shaohua Lidb350b02008-08-28 10:03:58 +0800632 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600633 list_add(&dock_station->sibling, &dock_stations);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100634 adev->flags.is_dock_station = true;
635 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
636 dock_station_count);
637 return;
Len Brownc8f7a622006-07-09 17:22:28 -0400638
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600639err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600640 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Rafael J. Wysockif311e1c2014-02-21 01:11:38 +0100641
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600642err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600643 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000644 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400645}