blob: 24e076f44d23827874c4d476ba8323d55dfa993c [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
Rusty Russell90ab5ee2012-01-13 09:32:20 +103023static bool immediate_undock = 1;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070024module_param(immediate_undock, bool, 0644);
25MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
26 "undock immediately when the undock button is pressed, 0 will cause"
27 " the driver to wait for userspace to write the undock sysfs file "
28 " before undocking");
29
Len Brownc8f7a622006-07-09 17:22:28 -040030struct dock_station {
31 acpi_handle handle;
32 unsigned long last_dock_time;
33 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040034 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080035
Alex Chiang50d716e2009-10-01 11:59:23 -060036 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080037 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040038};
Shaohua Lidb350b02008-08-28 10:03:58 +080039static LIST_HEAD(dock_stations);
40static int dock_station_count;
Len Brownc8f7a622006-07-09 17:22:28 -040041
42struct dock_dependent_device {
43 struct list_head list;
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010044 struct acpi_device *adev;
Len Brownc8f7a622006-07-09 17:22:28 -040045};
46
47#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070048#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080049#define DOCK_IS_DOCK 0x00000010
50#define DOCK_IS_ATA 0x00000020
51#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070052#define DOCK_EVENT 3
53#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040054
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020055enum dock_callback_type {
56 DOCK_CALL_HANDLER,
57 DOCK_CALL_FIXUP,
58 DOCK_CALL_UEVENT,
59};
60
Len Brownc8f7a622006-07-09 17:22:28 -040061/*****************************************************************************
62 * Dock Dependent device functions *
63 *****************************************************************************/
64/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060065 * add_dock_dependent_device - associate a device with the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010066 * @ds: Dock station.
67 * @adev: Dependent ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -040068 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -060069 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -040070 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010071static int add_dock_dependent_device(struct dock_station *ds,
72 struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -040073{
74 struct dock_dependent_device *dd;
75
76 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -060077 if (!dd)
78 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -040079
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010080 dd->adev = adev;
Alex Chiangf69cfdd2009-10-19 15:14:29 -060081 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -040082 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -060083
84 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -040085}
86
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020087static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020088 enum dock_callback_type cb_type)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020089{
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +010090 struct acpi_device *adev = dd->adev;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020091
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +010092 acpi_lock_hp_context();
93
94 if (!adev->hp)
Rafael J. Wysocki2f168172014-02-21 01:11:30 +010095 goto out;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +010096
97 if (cb_type == DOCK_CALL_FIXUP) {
98 void (*fixup)(struct acpi_device *);
99
100 fixup = adev->hp->fixup;
101 if (fixup) {
102 acpi_unlock_hp_context();
103 fixup(adev);
104 return;
105 }
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100106 } else if (cb_type == DOCK_CALL_UEVENT) {
107 void (*uevent)(struct acpi_device *, u32);
108
109 uevent = adev->hp->uevent;
110 if (uevent) {
111 acpi_unlock_hp_context();
112 uevent(adev, event);
113 return;
114 }
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100115 } else {
116 int (*notify)(struct acpi_device *, u32);
117
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100118 notify = adev->hp->notify;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100119 if (notify) {
120 acpi_unlock_hp_context();
121 notify(adev, event);
122 return;
123 }
124 }
125
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100126 out:
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100127 acpi_unlock_hp_context();
Len Brownc8f7a622006-07-09 17:22:28 -0400128}
129
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100130static struct dock_station *find_dock_station(acpi_handle handle)
131{
132 struct dock_station *ds;
133
134 list_for_each_entry(ds, &dock_stations, sibling)
135 if (ds->handle == handle)
136 return ds;
137
138 return NULL;
139}
140
Len Brownc8f7a622006-07-09 17:22:28 -0400141/**
142 * find_dock_dependent_device - get a device dependent on this dock
143 * @ds: the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100144 * @adev: ACPI device object to find.
Len Brownc8f7a622006-07-09 17:22:28 -0400145 *
146 * iterate over the dependent device list for this dock. If the
147 * dependent device matches the handle, return.
148 */
149static struct dock_dependent_device *
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100150find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400151{
152 struct dock_dependent_device *dd;
153
Jiang Liued633e72013-06-29 00:24:35 +0800154 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100155 if (adev == dd->adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400156 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800157
Len Brownc8f7a622006-07-09 17:22:28 -0400158 return NULL;
159}
160
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100161void register_dock_dependent_device(struct acpi_device *adev,
162 acpi_handle dshandle)
163{
164 struct dock_station *ds = find_dock_station(dshandle);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100165
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100166 if (ds && !find_dock_dependent_device(ds, adev))
167 add_dock_dependent_device(ds, adev);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100168}
169
Len Brownc8f7a622006-07-09 17:22:28 -0400170/*****************************************************************************
171 * Dock functions *
172 *****************************************************************************/
Shaohua Lidb350b02008-08-28 10:03:58 +0800173
Len Brownc8f7a622006-07-09 17:22:28 -0400174/**
175 * is_dock_device - see if a device is on a dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100176 * @adev: ACPI device object to check.
Len Brownc8f7a622006-07-09 17:22:28 -0400177 *
178 * If this device is either the dock station itself,
179 * or is a device dependent on the dock station, then it
180 * is a dock device
181 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100182int is_dock_device(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400183{
Shaohua Lidb350b02008-08-28 10:03:58 +0800184 struct dock_station *dock_station;
185
186 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400187 return 0;
188
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100189 if (acpi_dock_match(adev->handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400190 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600191
192 list_for_each_entry(dock_station, &dock_stations, sibling)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100193 if (find_dock_dependent_device(dock_station, adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800194 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400195
196 return 0;
197}
Len Brownc8f7a622006-07-09 17:22:28 -0400198EXPORT_SYMBOL_GPL(is_dock_device);
199
200/**
201 * dock_present - see if the dock station is present.
202 * @ds: the dock station
203 *
204 * execute the _STA method. note that present does not
205 * imply that we are docked.
206 */
207static int dock_present(struct dock_station *ds)
208{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400209 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400210 acpi_status status;
211
212 if (ds) {
213 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
214 if (ACPI_SUCCESS(status) && sta)
215 return 1;
216 }
217 return 0;
218}
219
Len Brownc8f7a622006-07-09 17:22:28 -0400220/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200221 * hot_remove_dock_devices - Remove dock station devices.
222 * @ds: Dock station.
223 */
224static void hot_remove_dock_devices(struct dock_station *ds)
225{
226 struct dock_dependent_device *dd;
227
228 /*
229 * Walk the list in reverse order so that devices that have been added
230 * last are removed first (in case there are some indirect dependencies
231 * between them).
232 */
233 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
Arnd Bergmannbe0e9752020-10-26 22:48:34 +0100234 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST,
235 DOCK_CALL_HANDLER);
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200236
237 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100238 acpi_bus_trim(dd->adev);
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200239}
240
241/**
242 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a622006-07-09 17:22:28 -0400243 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200244 * @event: either bus check or device check request
Len Brownc8f7a622006-07-09 17:22:28 -0400245 *
246 * Some devices on the dock station need to have drivers called
247 * to perform hotplug operations after a dock event has occurred.
248 * Traverse the list of dock devices that have registered a
249 * hotplug handler, and call the handler.
250 */
251static void hotplug_dock_devices(struct dock_station *ds, u32 event)
252{
253 struct dock_dependent_device *dd;
254
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200255 /* Call driver specific post-dock fixups. */
256 list_for_each_entry(dd, &ds->dependent_devices, list)
257 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
258
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200259 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200260 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200261 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
Len Brownc8f7a622006-07-09 17:22:28 -0400262
263 /*
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100264 * Check if all devices have been enumerated already. If not, run
265 * acpi_bus_scan() for them and that will cause scan handlers to be
266 * attached to device objects or acpi_drivers to be stopped/started if
267 * they are present.
Len Brownc8f7a622006-07-09 17:22:28 -0400268 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100269 list_for_each_entry(dd, &ds->dependent_devices, list) {
270 struct acpi_device *adev = dd->adev;
271
272 if (!acpi_device_enumerated(adev)) {
273 int ret = acpi_bus_scan(adev->handle);
274 if (ret)
275 dev_dbg(&adev->dev, "scan error %d\n", -ret);
276 }
277 }
Len Brownc8f7a622006-07-09 17:22:28 -0400278}
279
280static void dock_event(struct dock_station *ds, u32 event, int num)
281{
Shaohua Lidb350b02008-08-28 10:03:58 +0800282 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700283 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700284 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800285 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700286
287 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700288 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700289 else
Holger Macht66b56822007-08-10 13:10:32 -0700290 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700291
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700292 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800293 * Indicate that the status of the dock station has
294 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700295 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800296 if (num == DOCK_EVENT)
297 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
298
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200299 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200300 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
Alex Chiang747479a2009-10-19 15:14:50 -0600301
Shaohua Li1253f7a2008-08-28 10:06:16 +0800302 if (num != DOCK_EVENT)
303 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400304}
305
306/**
Len Brownc8f7a622006-07-09 17:22:28 -0400307 * handle_dock - handle a dock event
308 * @ds: the dock station
309 * @dock: to dock, or undock - that is the question
310 *
311 * Execute the _DCK method in response to an acpi event
312 */
313static void handle_dock(struct dock_station *ds, int dock)
314{
315 acpi_status status;
316 struct acpi_object_list arg_list;
317 union acpi_object arg;
Zhang Rui6a868e12013-09-03 08:32:10 +0800318 unsigned long long value;
Len Brownc8f7a622006-07-09 17:22:28 -0400319
Toshi Kanicd730182012-11-20 23:42:30 +0000320 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400321
322 /* _DCK method has one argument */
323 arg_list.count = 1;
324 arg_list.pointer = &arg;
325 arg.type = ACPI_TYPE_INTEGER;
326 arg.integer.value = dock;
Zhang Rui6a868e12013-09-03 08:32:10 +0800327 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
Shaohua Lidb350b02008-08-28 10:03:58 +0800328 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000329 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
330 status);
Len Brownc8f7a622006-07-09 17:22:28 -0400331}
332
333static inline void dock(struct dock_station *ds)
334{
335 handle_dock(ds, 1);
336}
337
338static inline void undock(struct dock_station *ds)
339{
340 handle_dock(ds, 0);
341}
342
343static inline void begin_dock(struct dock_station *ds)
344{
345 ds->flags |= DOCK_DOCKING;
346}
347
348static inline void complete_dock(struct dock_station *ds)
349{
350 ds->flags &= ~(DOCK_DOCKING);
351 ds->last_dock_time = jiffies;
352}
353
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700354static inline void begin_undock(struct dock_station *ds)
355{
356 ds->flags |= DOCK_UNDOCKING;
357}
358
359static inline void complete_undock(struct dock_station *ds)
360{
361 ds->flags &= ~(DOCK_UNDOCKING);
362}
363
Len Brownc8f7a622006-07-09 17:22:28 -0400364/**
365 * dock_in_progress - see if we are in the middle of handling a dock event
366 * @ds: the dock station
367 *
368 * Sometimes while docking, false dock events can be sent to the driver
369 * because good connections aren't made or some other reason. Ignore these
370 * if we are in the middle of doing something.
371 */
372static int dock_in_progress(struct dock_station *ds)
373{
374 if ((ds->flags & DOCK_DOCKING) ||
375 time_before(jiffies, (ds->last_dock_time + HZ)))
376 return 1;
377 return 0;
378}
379
380/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800381 * handle_eject_request - handle an undock request checking for error conditions
382 *
383 * Check to make sure the dock device is still present, then undock and
384 * hotremove all the devices that may need removing.
385 */
386static int handle_eject_request(struct dock_station *ds, u32 event)
387{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800388 if (dock_in_progress(ds))
389 return -EBUSY;
390
391 /*
392 * here we need to generate the undock
393 * event prior to actually doing the undock
394 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200395 * Also, even send the dock event if the
396 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800397 */
398 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200399
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200400 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800401 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800402 acpi_evaluate_lck(ds->handle, 0);
403 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800404 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000405 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800406 return -EBUSY;
407 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700408 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800409 return 0;
410}
411
412/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100413 * dock_notify - Handle ACPI dock notification.
414 * @adev: Dock station's ACPI device object.
415 * @event: Event code.
Len Brownc8f7a622006-07-09 17:22:28 -0400416 *
417 * If we are notified to dock, then check to see if the dock is
418 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800419 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400420 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100421int dock_notify(struct acpi_device *adev, u32 event)
Len Brownc8f7a622006-07-09 17:22:28 -0400422{
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100423 acpi_handle handle = adev->handle;
424 struct dock_station *ds = find_dock_station(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800425 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400426
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100427 if (!ds)
428 return -ENODEV;
429
Shaohua Lidb350b02008-08-28 10:03:58 +0800430 /*
431 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
432 * is sent and _DCK is present, it is assumed to mean an undock
433 * request.
434 */
435 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
436 event = ACPI_NOTIFY_EJECT_REQUEST;
437
438 /*
439 * dock station: BUS_CHECK - docked or surprise removal
440 * DEVICE_CHECK - undocked
441 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
442 *
443 * To simplify event handling, dock dependent device handler always
444 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
445 * ACPI_NOTIFY_EJECT_REQUEST for removal
446 */
Len Brownc8f7a622006-07-09 17:22:28 -0400447 switch (event) {
448 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800449 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki0a8e5c32014-02-10 13:44:20 +0100450 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400451 begin_dock(ds);
452 dock(ds);
453 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000454 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800455 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400456 break;
457 }
Len Brownc8f7a622006-07-09 17:22:28 -0400458 hotplug_dock_devices(ds, event);
459 complete_dock(ds);
460 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800461 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800462 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800463 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400464 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800465 if (dock_present(ds) || dock_in_progress(ds))
466 break;
467 /* This is a surprise removal */
468 surprise_removal = 1;
469 event = ACPI_NOTIFY_EJECT_REQUEST;
470 /* Fall back */
Gustavo A. R. Silva57d2dd42020-07-07 15:09:37 -0500471 fallthrough;
Len Brownc8f7a622006-07-09 17:22:28 -0400472 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700473 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800474 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
475 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700476 handle_eject_request(ds, event);
477 else
478 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400479 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400480 }
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100481 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400482}
483
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800484/*
485 * show_docked - read method for "docked" file in sysfs
486 */
487static ssize_t show_docked(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600490 struct dock_station *dock_station = dev->platform_data;
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100491 struct acpi_device *adev = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800492
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100493 acpi_bus_get_device(dock_station->handle, &adev);
494 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800495}
Adrian Bunke5685b92007-10-24 18:24:42 +0200496static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800497
498/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700499 * show_flags - read method for flags file in sysfs
500 */
501static ssize_t show_flags(struct device *dev,
502 struct device_attribute *attr, char *buf)
503{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600504 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700505 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
506
507}
Adrian Bunke5685b92007-10-24 18:24:42 +0200508static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700509
510/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800511 * write_undock - write method for "undock" file in sysfs
512 */
513static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
514 const char *buf, size_t count)
515{
516 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600517 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800518
519 if (!count)
520 return -EINVAL;
521
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200522 acpi_scan_lock_acquire();
Holger Macht9171f8342008-03-12 01:07:27 +0100523 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800524 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200525 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800526 return ret ? ret: count;
527}
Adrian Bunke5685b92007-10-24 18:24:42 +0200528static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800529
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800530/*
531 * show_dock_uid - read method for "uid" file in sysfs
532 */
533static ssize_t show_dock_uid(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400536 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600537 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700538 acpi_status status = acpi_evaluate_integer(dock_station->handle,
539 "_UID", NULL, &lbuf);
540 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800541 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700542
Matthew Wilcox27663c52008-10-10 02:22:59 -0400543 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800544}
Adrian Bunke5685b92007-10-24 18:24:42 +0200545static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800546
Shaohua Li8652b002008-08-28 10:07:45 +0800547static ssize_t show_dock_type(struct device *dev,
548 struct device_attribute *attr, char *buf)
549{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600550 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800551 char *type;
552
553 if (dock_station->flags & DOCK_IS_DOCK)
554 type = "dock_station";
555 else if (dock_station->flags & DOCK_IS_ATA)
556 type = "ata_bay";
557 else if (dock_station->flags & DOCK_IS_BAT)
558 type = "battery_bay";
559 else
560 type = "unknown";
561
562 return snprintf(buf, PAGE_SIZE, "%s\n", type);
563}
564static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
565
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600566static struct attribute *dock_attributes[] = {
567 &dev_attr_docked.attr,
568 &dev_attr_flags.attr,
569 &dev_attr_undock.attr,
570 &dev_attr_uid.attr,
571 &dev_attr_type.attr,
572 NULL
573};
574
Amitoj Kaur Chawla19dc7132017-08-01 20:28:46 -0400575static const struct attribute_group dock_attribute_group = {
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600576 .attrs = dock_attributes
577};
578
Len Brownc8f7a622006-07-09 17:22:28 -0400579/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100580 * acpi_dock_add - Add a new dock station
581 * @adev: Dock station ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -0400582 *
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100583 * allocated and initialize a new dock station device.
Len Brownc8f7a622006-07-09 17:22:28 -0400584 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100585void acpi_dock_add(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400586{
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200587 struct dock_station *dock_station, ds = { NULL, };
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100588 struct platform_device_info pdevinfo;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100589 acpi_handle handle = adev->handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600590 struct platform_device *dd;
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200591 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400592
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100593 memset(&pdevinfo, 0, sizeof(pdevinfo));
594 pdevinfo.name = "dock";
595 pdevinfo.id = dock_station_count;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100596 pdevinfo.fwnode = acpi_fwnode_handle(adev);
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100597 pdevinfo.data = &ds;
598 pdevinfo.size_data = sizeof(ds);
599 dd = platform_device_register_full(&pdevinfo);
Alex Chiang747479a2009-10-19 15:14:50 -0600600 if (IS_ERR(dd))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100601 return;
Alex Chiang9751cb72009-10-19 15:14:40 -0600602
Alex Chiang747479a2009-10-19 15:14:50 -0600603 dock_station = dd->dev.platform_data;
604
Len Brownc8f7a622006-07-09 17:22:28 -0400605 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600606 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400607 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400608
Alex Chiang747479a2009-10-19 15:14:50 -0600609 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600610 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700611
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700612 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600613 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700614
Jiang Liuc9b54712013-06-29 00:24:42 +0800615 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800616 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800617 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800618 dock_station->flags |= DOCK_IS_ATA;
Rafael J. Wysockib43109f2014-02-16 00:09:34 +0100619 if (acpi_device_is_battery(adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800620 dock_station->flags |= DOCK_IS_BAT;
621
Alex Chiang747479a2009-10-19 15:14:50 -0600622 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800623 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600624 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800625
Len Brownc8f7a622006-07-09 17:22:28 -0400626 /* add the dock station as a device dependent on itself */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100627 ret = add_dock_dependent_device(dock_station, adev);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600628 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600629 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400630
Shaohua Lidb350b02008-08-28 10:03:58 +0800631 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600632 list_add(&dock_station->sibling, &dock_stations);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100633 adev->flags.is_dock_station = true;
634 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
635 dock_station_count);
636 return;
Len Brownc8f7a622006-07-09 17:22:28 -0400637
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600638err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600639 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Rafael J. Wysockif311e1c2014-02-21 01:11:38 +0100640
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600641err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600642 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000643 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400644}