blob: 0c00208b423e0fba1c7a9f2ddc907880108be9c7 [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>
Paul Gortmaker0f093e92016-07-11 16:23:26 -040024#include <linux/moduleparam.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 Brownf52fd662007-02-12 22:42:12 -050036ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040037
Rusty Russell90ab5ee2012-01-13 09:32:20 +103038static bool immediate_undock = 1;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070039module_param(immediate_undock, bool, 0644);
40MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
41 "undock immediately when the undock button is pressed, 0 will cause"
42 " the driver to wait for userspace to write the undock sysfs file "
43 " before undocking");
44
Len Brownc8f7a622006-07-09 17:22:28 -040045struct dock_station {
46 acpi_handle handle;
47 unsigned long last_dock_time;
48 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040049 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080050
Alex Chiang50d716e2009-10-01 11:59:23 -060051 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080052 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040053};
Shaohua Lidb350b02008-08-28 10:03:58 +080054static LIST_HEAD(dock_stations);
55static int dock_station_count;
Len Brownc8f7a622006-07-09 17:22:28 -040056
57struct dock_dependent_device {
58 struct list_head list;
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010059 struct acpi_device *adev;
Len Brownc8f7a622006-07-09 17:22:28 -040060};
61
62#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070063#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080064#define DOCK_IS_DOCK 0x00000010
65#define DOCK_IS_ATA 0x00000020
66#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070067#define DOCK_EVENT 3
68#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040069
Rafael J. Wysockif09ce742013-07-05 03:03:25 +020070enum dock_callback_type {
71 DOCK_CALL_HANDLER,
72 DOCK_CALL_FIXUP,
73 DOCK_CALL_UEVENT,
74};
75
Len Brownc8f7a622006-07-09 17:22:28 -040076/*****************************************************************************
77 * Dock Dependent device functions *
78 *****************************************************************************/
79/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060080 * add_dock_dependent_device - associate a device with the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010081 * @ds: Dock station.
82 * @adev: Dependent ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -040083 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -060084 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -040085 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010086static int add_dock_dependent_device(struct dock_station *ds,
87 struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -040088{
89 struct dock_dependent_device *dd;
90
91 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -060092 if (!dd)
93 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -040094
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +010095 dd->adev = adev;
Alex Chiangf69cfdd2009-10-19 15:14:29 -060096 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -040097 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -060098
99 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400100}
101
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200102static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200103 enum dock_callback_type cb_type)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200104{
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100105 struct acpi_device *adev = dd->adev;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200106
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100107 acpi_lock_hp_context();
108
109 if (!adev->hp)
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100110 goto out;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100111
112 if (cb_type == DOCK_CALL_FIXUP) {
113 void (*fixup)(struct acpi_device *);
114
115 fixup = adev->hp->fixup;
116 if (fixup) {
117 acpi_unlock_hp_context();
118 fixup(adev);
119 return;
120 }
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100121 } else if (cb_type == DOCK_CALL_UEVENT) {
122 void (*uevent)(struct acpi_device *, u32);
123
124 uevent = adev->hp->uevent;
125 if (uevent) {
126 acpi_unlock_hp_context();
127 uevent(adev, event);
128 return;
129 }
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100130 } else {
131 int (*notify)(struct acpi_device *, u32);
132
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100133 notify = adev->hp->notify;
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100134 if (notify) {
135 acpi_unlock_hp_context();
136 notify(adev, event);
137 return;
138 }
139 }
140
Rafael J. Wysocki2f168172014-02-21 01:11:30 +0100141 out:
Rafael J. Wysockiedf5bf32014-02-21 01:10:18 +0100142 acpi_unlock_hp_context();
Len Brownc8f7a622006-07-09 17:22:28 -0400143}
144
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100145static struct dock_station *find_dock_station(acpi_handle handle)
146{
147 struct dock_station *ds;
148
149 list_for_each_entry(ds, &dock_stations, sibling)
150 if (ds->handle == handle)
151 return ds;
152
153 return NULL;
154}
155
Len Brownc8f7a622006-07-09 17:22:28 -0400156/**
157 * find_dock_dependent_device - get a device dependent on this dock
158 * @ds: the dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100159 * @adev: ACPI device object to find.
Len Brownc8f7a622006-07-09 17:22:28 -0400160 *
161 * iterate over the dependent device list for this dock. If the
162 * dependent device matches the handle, return.
163 */
164static struct dock_dependent_device *
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100165find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400166{
167 struct dock_dependent_device *dd;
168
Jiang Liued633e72013-06-29 00:24:35 +0800169 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100170 if (adev == dd->adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400171 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800172
Len Brownc8f7a622006-07-09 17:22:28 -0400173 return NULL;
174}
175
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100176void register_dock_dependent_device(struct acpi_device *adev,
177 acpi_handle dshandle)
178{
179 struct dock_station *ds = find_dock_station(dshandle);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100180
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100181 if (ds && !find_dock_dependent_device(ds, adev))
182 add_dock_dependent_device(ds, adev);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100183}
184
Len Brownc8f7a622006-07-09 17:22:28 -0400185/*****************************************************************************
186 * Dock functions *
187 *****************************************************************************/
Shaohua Lidb350b02008-08-28 10:03:58 +0800188
Len Brownc8f7a622006-07-09 17:22:28 -0400189/**
190 * is_dock_device - see if a device is on a dock station
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100191 * @adev: ACPI device object to check.
Len Brownc8f7a622006-07-09 17:22:28 -0400192 *
193 * If this device is either the dock station itself,
194 * or is a device dependent on the dock station, then it
195 * is a dock device
196 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100197int is_dock_device(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400198{
Shaohua Lidb350b02008-08-28 10:03:58 +0800199 struct dock_station *dock_station;
200
201 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400202 return 0;
203
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100204 if (acpi_dock_match(adev->handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400205 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600206
207 list_for_each_entry(dock_station, &dock_stations, sibling)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100208 if (find_dock_dependent_device(dock_station, adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800209 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400210
211 return 0;
212}
Len Brownc8f7a622006-07-09 17:22:28 -0400213EXPORT_SYMBOL_GPL(is_dock_device);
214
215/**
216 * dock_present - see if the dock station is present.
217 * @ds: the dock station
218 *
219 * execute the _STA method. note that present does not
220 * imply that we are docked.
221 */
222static int dock_present(struct dock_station *ds)
223{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400224 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400225 acpi_status status;
226
227 if (ds) {
228 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
229 if (ACPI_SUCCESS(status) && sta)
230 return 1;
231 }
232 return 0;
233}
234
Len Brownc8f7a622006-07-09 17:22:28 -0400235/**
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200236 * hot_remove_dock_devices - Remove dock station devices.
237 * @ds: Dock station.
238 */
239static void hot_remove_dock_devices(struct dock_station *ds)
240{
241 struct dock_dependent_device *dd;
242
243 /*
244 * Walk the list in reverse order so that devices that have been added
245 * last are removed first (in case there are some indirect dependencies
246 * between them).
247 */
248 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
249 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
250
251 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100252 acpi_bus_trim(dd->adev);
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200253}
254
255/**
256 * hotplug_dock_devices - Insert devices on a dock station.
Len Brownc8f7a622006-07-09 17:22:28 -0400257 * @ds: the dock station
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200258 * @event: either bus check or device check request
Len Brownc8f7a622006-07-09 17:22:28 -0400259 *
260 * Some devices on the dock station need to have drivers called
261 * to perform hotplug operations after a dock event has occurred.
262 * Traverse the list of dock devices that have registered a
263 * hotplug handler, and call the handler.
264 */
265static void hotplug_dock_devices(struct dock_station *ds, u32 event)
266{
267 struct dock_dependent_device *dd;
268
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200269 /* Call driver specific post-dock fixups. */
270 list_for_each_entry(dd, &ds->dependent_devices, list)
271 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
272
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200273 /* Call driver specific hotplug functions. */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200274 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200275 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
Len Brownc8f7a622006-07-09 17:22:28 -0400276
277 /*
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100278 * Check if all devices have been enumerated already. If not, run
279 * acpi_bus_scan() for them and that will cause scan handlers to be
280 * attached to device objects or acpi_drivers to be stopped/started if
281 * they are present.
Len Brownc8f7a622006-07-09 17:22:28 -0400282 */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100283 list_for_each_entry(dd, &ds->dependent_devices, list) {
284 struct acpi_device *adev = dd->adev;
285
286 if (!acpi_device_enumerated(adev)) {
287 int ret = acpi_bus_scan(adev->handle);
288 if (ret)
289 dev_dbg(&adev->dev, "scan error %d\n", -ret);
290 }
291 }
Len Brownc8f7a622006-07-09 17:22:28 -0400292}
293
294static void dock_event(struct dock_station *ds, u32 event, int num)
295{
Shaohua Lidb350b02008-08-28 10:03:58 +0800296 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700297 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700298 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800299 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700300
301 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700302 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700303 else
Holger Macht66b56822007-08-10 13:10:32 -0700304 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700305
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700306 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800307 * Indicate that the status of the dock station has
308 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700309 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800310 if (num == DOCK_EVENT)
311 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
312
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200313 list_for_each_entry(dd, &ds->dependent_devices, list)
Rafael J. Wysockif09ce742013-07-05 03:03:25 +0200314 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
Alex Chiang747479a2009-10-19 15:14:50 -0600315
Shaohua Li1253f7a2008-08-28 10:06:16 +0800316 if (num != DOCK_EVENT)
317 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400318}
319
320/**
Len Brownc8f7a622006-07-09 17:22:28 -0400321 * handle_dock - handle a dock event
322 * @ds: the dock station
323 * @dock: to dock, or undock - that is the question
324 *
325 * Execute the _DCK method in response to an acpi event
326 */
327static void handle_dock(struct dock_station *ds, int dock)
328{
329 acpi_status status;
330 struct acpi_object_list arg_list;
331 union acpi_object arg;
Zhang Rui6a868e12013-09-03 08:32:10 +0800332 unsigned long long value;
Len Brownc8f7a622006-07-09 17:22:28 -0400333
Toshi Kanicd730182012-11-20 23:42:30 +0000334 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400335
336 /* _DCK method has one argument */
337 arg_list.count = 1;
338 arg_list.pointer = &arg;
339 arg.type = ACPI_TYPE_INTEGER;
340 arg.integer.value = dock;
Zhang Rui6a868e12013-09-03 08:32:10 +0800341 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
Shaohua Lidb350b02008-08-28 10:03:58 +0800342 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000343 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
344 status);
Len Brownc8f7a622006-07-09 17:22:28 -0400345}
346
347static inline void dock(struct dock_station *ds)
348{
349 handle_dock(ds, 1);
350}
351
352static inline void undock(struct dock_station *ds)
353{
354 handle_dock(ds, 0);
355}
356
357static inline void begin_dock(struct dock_station *ds)
358{
359 ds->flags |= DOCK_DOCKING;
360}
361
362static inline void complete_dock(struct dock_station *ds)
363{
364 ds->flags &= ~(DOCK_DOCKING);
365 ds->last_dock_time = jiffies;
366}
367
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700368static inline void begin_undock(struct dock_station *ds)
369{
370 ds->flags |= DOCK_UNDOCKING;
371}
372
373static inline void complete_undock(struct dock_station *ds)
374{
375 ds->flags &= ~(DOCK_UNDOCKING);
376}
377
Len Brownc8f7a622006-07-09 17:22:28 -0400378/**
379 * dock_in_progress - see if we are in the middle of handling a dock event
380 * @ds: the dock station
381 *
382 * Sometimes while docking, false dock events can be sent to the driver
383 * because good connections aren't made or some other reason. Ignore these
384 * if we are in the middle of doing something.
385 */
386static int dock_in_progress(struct dock_station *ds)
387{
388 if ((ds->flags & DOCK_DOCKING) ||
389 time_before(jiffies, (ds->last_dock_time + HZ)))
390 return 1;
391 return 0;
392}
393
394/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800395 * handle_eject_request - handle an undock request checking for error conditions
396 *
397 * Check to make sure the dock device is still present, then undock and
398 * hotremove all the devices that may need removing.
399 */
400static int handle_eject_request(struct dock_station *ds, u32 event)
401{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800402 if (dock_in_progress(ds))
403 return -EBUSY;
404
405 /*
406 * here we need to generate the undock
407 * event prior to actually doing the undock
408 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200409 * Also, even send the dock event if the
410 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800411 */
412 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200413
Rafael J. Wysocki37f90872013-06-30 23:46:42 +0200414 hot_remove_dock_devices(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800415 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800416 acpi_evaluate_lck(ds->handle, 0);
417 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800418 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000419 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800420 return -EBUSY;
421 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700422 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800423 return 0;
424}
425
426/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100427 * dock_notify - Handle ACPI dock notification.
428 * @adev: Dock station's ACPI device object.
429 * @event: Event code.
Len Brownc8f7a622006-07-09 17:22:28 -0400430 *
431 * If we are notified to dock, then check to see if the dock is
432 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800433 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400434 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100435int dock_notify(struct acpi_device *adev, u32 event)
Len Brownc8f7a622006-07-09 17:22:28 -0400436{
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100437 acpi_handle handle = adev->handle;
438 struct dock_station *ds = find_dock_station(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800439 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400440
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100441 if (!ds)
442 return -ENODEV;
443
Shaohua Lidb350b02008-08-28 10:03:58 +0800444 /*
445 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
446 * is sent and _DCK is present, it is assumed to mean an undock
447 * request.
448 */
449 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
450 event = ACPI_NOTIFY_EJECT_REQUEST;
451
452 /*
453 * dock station: BUS_CHECK - docked or surprise removal
454 * DEVICE_CHECK - undocked
455 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
456 *
457 * To simplify event handling, dock dependent device handler always
458 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
459 * ACPI_NOTIFY_EJECT_REQUEST for removal
460 */
Len Brownc8f7a622006-07-09 17:22:28 -0400461 switch (event) {
462 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800463 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki0a8e5c32014-02-10 13:44:20 +0100464 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400465 begin_dock(ds);
466 dock(ds);
467 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000468 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800469 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400470 break;
471 }
Len Brownc8f7a622006-07-09 17:22:28 -0400472 hotplug_dock_devices(ds, event);
473 complete_dock(ds);
474 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800475 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800476 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800477 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400478 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800479 if (dock_present(ds) || dock_in_progress(ds))
480 break;
481 /* This is a surprise removal */
482 surprise_removal = 1;
483 event = ACPI_NOTIFY_EJECT_REQUEST;
484 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400485 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700486 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800487 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
488 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700489 handle_eject_request(ds, event);
490 else
491 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400492 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400493 }
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100494 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400495}
496
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800497/*
498 * show_docked - read method for "docked" file in sysfs
499 */
500static ssize_t show_docked(struct device *dev,
501 struct device_attribute *attr, char *buf)
502{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600503 struct dock_station *dock_station = dev->platform_data;
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100504 struct acpi_device *adev = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800505
Rafael J. Wysockiab62f9c2014-02-15 01:29:06 +0100506 acpi_bus_get_device(dock_station->handle, &adev);
507 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800508}
Adrian Bunke5685b92007-10-24 18:24:42 +0200509static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800510
511/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700512 * show_flags - read method for flags file in sysfs
513 */
514static ssize_t show_flags(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600517 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700518 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
519
520}
Adrian Bunke5685b92007-10-24 18:24:42 +0200521static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700522
523/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800524 * write_undock - write method for "undock" file in sysfs
525 */
526static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
527 const char *buf, size_t count)
528{
529 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600530 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800531
532 if (!count)
533 return -EINVAL;
534
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200535 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100536 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800537 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200538 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800539 return ret ? ret: count;
540}
Adrian Bunke5685b92007-10-24 18:24:42 +0200541static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800542
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800543/*
544 * show_dock_uid - read method for "uid" file in sysfs
545 */
546static ssize_t show_dock_uid(struct device *dev,
547 struct device_attribute *attr, char *buf)
548{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400549 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600550 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700551 acpi_status status = acpi_evaluate_integer(dock_station->handle,
552 "_UID", NULL, &lbuf);
553 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800554 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700555
Matthew Wilcox27663c52008-10-10 02:22:59 -0400556 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800557}
Adrian Bunke5685b92007-10-24 18:24:42 +0200558static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800559
Shaohua Li8652b002008-08-28 10:07:45 +0800560static ssize_t show_dock_type(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600563 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800564 char *type;
565
566 if (dock_station->flags & DOCK_IS_DOCK)
567 type = "dock_station";
568 else if (dock_station->flags & DOCK_IS_ATA)
569 type = "ata_bay";
570 else if (dock_station->flags & DOCK_IS_BAT)
571 type = "battery_bay";
572 else
573 type = "unknown";
574
575 return snprintf(buf, PAGE_SIZE, "%s\n", type);
576}
577static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
578
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600579static struct attribute *dock_attributes[] = {
580 &dev_attr_docked.attr,
581 &dev_attr_flags.attr,
582 &dev_attr_undock.attr,
583 &dev_attr_uid.attr,
584 &dev_attr_type.attr,
585 NULL
586};
587
588static struct attribute_group dock_attribute_group = {
589 .attrs = dock_attributes
590};
591
Len Brownc8f7a622006-07-09 17:22:28 -0400592/**
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100593 * acpi_dock_add - Add a new dock station
594 * @adev: Dock station ACPI device object.
Len Brownc8f7a622006-07-09 17:22:28 -0400595 *
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100596 * allocated and initialize a new dock station device.
Len Brownc8f7a622006-07-09 17:22:28 -0400597 */
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100598void acpi_dock_add(struct acpi_device *adev)
Len Brownc8f7a622006-07-09 17:22:28 -0400599{
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200600 struct dock_station *dock_station, ds = { NULL, };
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100601 struct platform_device_info pdevinfo;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100602 acpi_handle handle = adev->handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600603 struct platform_device *dd;
Rafael J. Wysocki2efbca42013-07-05 03:23:36 +0200604 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400605
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100606 memset(&pdevinfo, 0, sizeof(pdevinfo));
607 pdevinfo.name = "dock";
608 pdevinfo.id = dock_station_count;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100609 pdevinfo.fwnode = acpi_fwnode_handle(adev);
Rafael J. Wysockiaf8874492014-02-16 00:09:47 +0100610 pdevinfo.data = &ds;
611 pdevinfo.size_data = sizeof(ds);
612 dd = platform_device_register_full(&pdevinfo);
Alex Chiang747479a2009-10-19 15:14:50 -0600613 if (IS_ERR(dd))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100614 return;
Alex Chiang9751cb72009-10-19 15:14:40 -0600615
Alex Chiang747479a2009-10-19 15:14:50 -0600616 dock_station = dd->dev.platform_data;
617
Len Brownc8f7a622006-07-09 17:22:28 -0400618 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600619 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400620 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400621
Alex Chiang747479a2009-10-19 15:14:50 -0600622 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600623 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700624
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700625 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600626 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700627
Jiang Liuc9b54712013-06-29 00:24:42 +0800628 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800629 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800630 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800631 dock_station->flags |= DOCK_IS_ATA;
Rafael J. Wysockib43109f2014-02-16 00:09:34 +0100632 if (acpi_device_is_battery(adev))
Shaohua Lidb350b02008-08-28 10:03:58 +0800633 dock_station->flags |= DOCK_IS_BAT;
634
Alex Chiang747479a2009-10-19 15:14:50 -0600635 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800636 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600637 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800638
Len Brownc8f7a622006-07-09 17:22:28 -0400639 /* add the dock station as a device dependent on itself */
Rafael J. Wysocki3b52b212014-02-21 01:10:09 +0100640 ret = add_dock_dependent_device(dock_station, adev);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600641 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600642 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400643
Shaohua Lidb350b02008-08-28 10:03:58 +0800644 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600645 list_add(&dock_station->sibling, &dock_stations);
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100646 adev->flags.is_dock_station = true;
647 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
648 dock_station_count);
649 return;
Len Brownc8f7a622006-07-09 17:22:28 -0400650
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600651err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600652 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Rafael J. Wysockif311e1c2014-02-21 01:11:38 +0100653
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600654err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600655 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000656 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400657}