dock.c 16.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Len Brown 已提交
2 3 4
/*
 *  dock.c - ACPI dock station driver
 *
5 6 7
 *  Copyright (C) 2006, 2014, Intel Corp.
 *  Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
 *          Rafael J. Wysocki <rafael.j.wysocki@intel.com>
L
Len Brown 已提交
8 9 10
 */

#include <linux/kernel.h>
11
#include <linux/moduleparam.h>
12
#include <linux/slab.h>
L
Len Brown 已提交
13 14 15
#include <linux/init.h>
#include <linux/types.h>
#include <linux/notifier.h>
16
#include <linux/platform_device.h>
17
#include <linux/jiffies.h>
R
Randy Dunlap 已提交
18
#include <linux/stddef.h>
19
#include <linux/acpi.h>
L
Len Brown 已提交
20

21 22
#include "internal.h"

23
static bool immediate_undock = 1;
24 25 26 27 28 29
module_param(immediate_undock, bool, 0644);
MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
	"undock immediately when the undock button is pressed, 0 will cause"
	" the driver to wait for userspace to write the undock sysfs file "
	" before undocking");

L
Len Brown 已提交
30 31 32 33 34
struct dock_station {
	acpi_handle handle;
	unsigned long last_dock_time;
	u32 flags;
	struct list_head dependent_devices;
35

A
Alex Chiang 已提交
36
	struct list_head sibling;
37
	struct platform_device *dock_device;
L
Len Brown 已提交
38
};
39 40
static LIST_HEAD(dock_stations);
static int dock_station_count;
L
Len Brown 已提交
41 42 43

struct dock_dependent_device {
	struct list_head list;
44
	struct acpi_device *adev;
L
Len Brown 已提交
45 46 47
};

#define DOCK_DOCKING	0x00000001
48
#define DOCK_UNDOCKING  0x00000002
49 50 51
#define DOCK_IS_DOCK	0x00000010
#define DOCK_IS_ATA	0x00000020
#define DOCK_IS_BAT	0x00000040
52 53
#define DOCK_EVENT	3
#define UNDOCK_EVENT	2
L
Len Brown 已提交
54

55 56 57 58 59 60
enum dock_callback_type {
	DOCK_CALL_HANDLER,
	DOCK_CALL_FIXUP,
	DOCK_CALL_UEVENT,
};

L
Len Brown 已提交
61 62 63 64
/*****************************************************************************
 *                         Dock Dependent device functions                   *
 *****************************************************************************/
/**
65
 * add_dock_dependent_device - associate a device with the dock station
66 67
 * @ds: Dock station.
 * @adev: Dependent ACPI device object.
L
Len Brown 已提交
68
 *
69
 * Add the dependent device to the dock's dependent device list.
L
Len Brown 已提交
70
 */
71 72
static int add_dock_dependent_device(struct dock_station *ds,
				     struct acpi_device *adev)
L
Len Brown 已提交
73 74 75 76
{
	struct dock_dependent_device *dd;

	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
77 78 79
	if (!dd)
		return -ENOMEM;

80
	dd->adev = adev;
81
	INIT_LIST_HEAD(&dd->list);
L
Len Brown 已提交
82
	list_add_tail(&dd->list, &ds->dependent_devices);
83 84

	return 0;
L
Len Brown 已提交
85 86
}

87
static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
88
			       enum dock_callback_type cb_type)
89
{
90
	struct acpi_device *adev = dd->adev;
91

92 93 94
	acpi_lock_hp_context();

	if (!adev->hp)
95
		goto out;
96 97 98 99 100 101 102 103 104 105

	if (cb_type == DOCK_CALL_FIXUP) {
		void (*fixup)(struct acpi_device *);

		fixup = adev->hp->fixup;
		if (fixup) {
			acpi_unlock_hp_context();
			fixup(adev);
			return;
		}
106 107 108 109 110 111 112 113 114
	} else if (cb_type == DOCK_CALL_UEVENT) {
		void (*uevent)(struct acpi_device *, u32);

		uevent = adev->hp->uevent;
		if (uevent) {
			acpi_unlock_hp_context();
			uevent(adev, event);
			return;
		}
115 116 117
	} else {
		int (*notify)(struct acpi_device *, u32);

118
		notify = adev->hp->notify;
119 120 121 122 123 124 125
		if (notify) {
			acpi_unlock_hp_context();
			notify(adev, event);
			return;
		}
	}

126
 out:
127
	acpi_unlock_hp_context();
L
Len Brown 已提交
128 129
}

130 131 132 133 134 135 136 137 138 139 140
static struct dock_station *find_dock_station(acpi_handle handle)
{
	struct dock_station *ds;

	list_for_each_entry(ds, &dock_stations, sibling)
		if (ds->handle == handle)
			return ds;

	return NULL;
}

L
Len Brown 已提交
141 142 143
/**
 * find_dock_dependent_device - get a device dependent on this dock
 * @ds: the dock station
144
 * @adev: ACPI device object to find.
L
Len Brown 已提交
145 146 147 148 149
 *
 * iterate over the dependent device list for this dock.  If the
 * dependent device matches the handle, return.
 */
static struct dock_dependent_device *
150
find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
L
Len Brown 已提交
151 152 153
{
	struct dock_dependent_device *dd;

154
	list_for_each_entry(dd, &ds->dependent_devices, list)
155
		if (adev == dd->adev)
L
Len Brown 已提交
156
			return dd;
157

L
Len Brown 已提交
158 159 160
	return NULL;
}

161 162
void register_dock_dependent_device(struct acpi_device *adev,
				    acpi_handle dshandle)
163
{
164
	struct dock_station *ds = find_dock_station(dshandle);
165

166 167
	if (ds && !find_dock_dependent_device(ds, adev))
		add_dock_dependent_device(ds, adev);
168 169
}

170 171 172
/*****************************************************************************
 *                         Dock functions                                    *
 *****************************************************************************/
173

L
Len Brown 已提交
174 175
/**
 * is_dock_device - see if a device is on a dock station
176
 * @adev: ACPI device object to check.
L
Len Brown 已提交
177 178 179 180 181
 *
 * If this device is either the dock station itself,
 * or is a device dependent on the dock station, then it
 * is a dock device
 */
182
int is_dock_device(struct acpi_device *adev)
L
Len Brown 已提交
183
{
184 185 186
	struct dock_station *dock_station;

	if (!dock_station_count)
L
Len Brown 已提交
187 188
		return 0;

189
	if (acpi_dock_match(adev->handle))
L
Len Brown 已提交
190
		return 1;
191 192

	list_for_each_entry(dock_station, &dock_stations, sibling)
193
		if (find_dock_dependent_device(dock_station, adev))
194
			return 1;
L
Len Brown 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208

	return 0;
}
EXPORT_SYMBOL_GPL(is_dock_device);

/**
 * dock_present - see if the dock station is present.
 * @ds: the dock station
 *
 * execute the _STA method.  note that present does not
 * imply that we are docked.
 */
static int dock_present(struct dock_station *ds)
{
209
	unsigned long long sta;
L
Len Brown 已提交
210 211 212 213 214 215 216 217 218 219 220
	acpi_status status;

	if (ds) {
		status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
		if (ACPI_SUCCESS(status) && sta)
			return 1;
	}
	return 0;
}

/**
221 222 223 224 225 226 227 228 229 230 231 232 233
 * hot_remove_dock_devices - Remove dock station devices.
 * @ds: Dock station.
 */
static void hot_remove_dock_devices(struct dock_station *ds)
{
	struct dock_dependent_device *dd;

	/*
	 * Walk the list in reverse order so that devices that have been added
	 * last are removed first (in case there are some indirect dependencies
	 * between them).
	 */
	list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
234 235
		dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST,
				   DOCK_CALL_HANDLER);
236 237

	list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
238
		acpi_bus_trim(dd->adev);
239 240 241 242
}

/**
 * hotplug_dock_devices - Insert devices on a dock station.
L
Len Brown 已提交
243
 * @ds: the dock station
244
 * @event: either bus check or device check request
L
Len Brown 已提交
245 246 247 248 249 250 251 252 253 254
 *
 * Some devices on the dock station need to have drivers called
 * to perform hotplug operations after a dock event has occurred.
 * Traverse the list of dock devices that have registered a
 * hotplug handler, and call the handler.
 */
static void hotplug_dock_devices(struct dock_station *ds, u32 event)
{
	struct dock_dependent_device *dd;

255 256 257 258
	/* Call driver specific post-dock fixups. */
	list_for_each_entry(dd, &ds->dependent_devices, list)
		dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);

259
	/* Call driver specific hotplug functions. */
260
	list_for_each_entry(dd, &ds->dependent_devices, list)
261
		dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
L
Len Brown 已提交
262 263

	/*
264 265 266 267
	 * Check if all devices have been enumerated already.  If not, run
	 * acpi_bus_scan() for them and that will cause scan handlers to be
	 * attached to device objects or acpi_drivers to be stopped/started if
	 * they are present.
L
Len Brown 已提交
268
	 */
269 270 271 272 273
	list_for_each_entry(dd, &ds->dependent_devices, list) {
		struct acpi_device *adev = dd->adev;

		if (!acpi_device_enumerated(adev)) {
			int ret = acpi_bus_scan(adev->handle);
274

275 276 277 278
			if (ret)
				dev_dbg(&adev->dev, "scan error %d\n", -ret);
		}
	}
L
Len Brown 已提交
279 280 281 282
}

static void dock_event(struct dock_station *ds, u32 event, int num)
{
283
	struct device *dev = &ds->dock_device->dev;
284
	char event_string[13];
285
	char *envp[] = { event_string, NULL };
286
	struct dock_dependent_device *dd;
287 288

	if (num == UNDOCK_EVENT)
289
		sprintf(event_string, "EVENT=undock");
290
	else
291
		sprintf(event_string, "EVENT=dock");
292

293
	/*
294 295
	 * Indicate that the status of the dock station has
	 * changed.
296
	 */
297 298 299
	if (num == DOCK_EVENT)
		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);

300
	list_for_each_entry(dd, &ds->dependent_devices, list)
301
		dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
302

303 304
	if (num != DOCK_EVENT)
		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
L
Len Brown 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318
}

/**
 * handle_dock - handle a dock event
 * @ds: the dock station
 * @dock: to dock, or undock - that is the question
 *
 * Execute the _DCK method in response to an acpi event
 */
static void handle_dock(struct dock_station *ds, int dock)
{
	acpi_status status;
	struct acpi_object_list arg_list;
	union acpi_object arg;
319
	unsigned long long value;
L
Len Brown 已提交
320

321
	acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
L
Len Brown 已提交
322 323 324 325 326 327

	/* _DCK method has one argument */
	arg_list.count = 1;
	arg_list.pointer = &arg;
	arg.type = ACPI_TYPE_INTEGER;
	arg.integer.value = dock;
328
	status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
329
	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
330 331
		acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
				status);
L
Len Brown 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
}

static inline void dock(struct dock_station *ds)
{
	handle_dock(ds, 1);
}

static inline void undock(struct dock_station *ds)
{
	handle_dock(ds, 0);
}

static inline void begin_dock(struct dock_station *ds)
{
	ds->flags |= DOCK_DOCKING;
}

static inline void complete_dock(struct dock_station *ds)
{
	ds->flags &= ~(DOCK_DOCKING);
	ds->last_dock_time = jiffies;
}

355 356 357 358 359 360 361 362 363 364
static inline void begin_undock(struct dock_station *ds)
{
	ds->flags |= DOCK_UNDOCKING;
}

static inline void complete_undock(struct dock_station *ds)
{
	ds->flags &= ~(DOCK_UNDOCKING);
}

L
Len Brown 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
/**
 * dock_in_progress - see if we are in the middle of handling a dock event
 * @ds: the dock station
 *
 * Sometimes while docking, false dock events can be sent to the driver
 * because good connections aren't made or some other reason.  Ignore these
 * if we are in the middle of doing something.
 */
static int dock_in_progress(struct dock_station *ds)
{
	if ((ds->flags & DOCK_DOCKING) ||
	    time_before(jiffies, (ds->last_dock_time + HZ)))
		return 1;
	return 0;
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
/**
 * handle_eject_request - handle an undock request checking for error conditions
 *
 * Check to make sure the dock device is still present, then undock and
 * hotremove all the devices that may need removing.
 */
static int handle_eject_request(struct dock_station *ds, u32 event)
{
	if (dock_in_progress(ds))
		return -EBUSY;

	/*
	 * here we need to generate the undock
	 * event prior to actually doing the undock
	 * so that the device struct still exists.
396 397
	 * Also, even send the dock event if the
	 * device is not present anymore
398 399
	 */
	dock_event(ds, event, UNDOCK_EVENT);
400

401
	hot_remove_dock_devices(ds);
402
	undock(ds);
403 404
	acpi_evaluate_lck(ds->handle, 0);
	acpi_evaluate_ej0(ds->handle);
405
	if (dock_present(ds)) {
406
		acpi_handle_err(ds->handle, "Unable to undock!\n");
407 408
		return -EBUSY;
	}
409
	complete_undock(ds);
410 411 412
	return 0;
}

L
Len Brown 已提交
413
/**
414 415 416
 * dock_notify - Handle ACPI dock notification.
 * @adev: Dock station's ACPI device object.
 * @event: Event code.
L
Len Brown 已提交
417 418 419
 *
 * If we are notified to dock, then check to see if the dock is
 * present and then dock.  Notify all drivers of the dock event,
420
 * and then hotplug and devices that may need hotplugging.
L
Len Brown 已提交
421
 */
422
int dock_notify(struct acpi_device *adev, u32 event)
L
Len Brown 已提交
423
{
424 425
	acpi_handle handle = adev->handle;
	struct dock_station *ds = find_dock_station(handle);
426
	int surprise_removal = 0;
L
Len Brown 已提交
427

428 429 430
	if (!ds)
		return -ENODEV;

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	/*
	 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
	 * is sent and _DCK is present, it is assumed to mean an undock
	 * request.
	 */
	if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
		event = ACPI_NOTIFY_EJECT_REQUEST;

	/*
	 * dock station: BUS_CHECK - docked or surprise removal
	 *		 DEVICE_CHECK - undocked
	 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
	 *
	 * To simplify event handling, dock dependent device handler always
	 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
	 * ACPI_NOTIFY_EJECT_REQUEST for removal
	 */
L
Len Brown 已提交
448 449
	switch (event) {
	case ACPI_NOTIFY_BUS_CHECK:
450
	case ACPI_NOTIFY_DEVICE_CHECK:
451
		if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
L
Len Brown 已提交
452 453 454
			begin_dock(ds);
			dock(ds);
			if (!dock_present(ds)) {
455
				acpi_handle_err(handle, "Unable to dock!\n");
S
Shaohua Li 已提交
456
				complete_dock(ds);
L
Len Brown 已提交
457 458 459 460 461
				break;
			}
			hotplug_dock_devices(ds, event);
			complete_dock(ds);
			dock_event(ds, event, DOCK_EVENT);
462
			acpi_evaluate_lck(ds->handle, 1);
463
			acpi_update_all_gpes();
464
			break;
L
Len Brown 已提交
465
		}
466 467 468 469 470 471
		if (dock_present(ds) || dock_in_progress(ds))
			break;
		/* This is a surprise removal */
		surprise_removal = 1;
		event = ACPI_NOTIFY_EJECT_REQUEST;
		/* Fall back */
472
		fallthrough;
L
Len Brown 已提交
473
	case ACPI_NOTIFY_EJECT_REQUEST:
474
		begin_undock(ds);
475 476
		if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
		   || surprise_removal)
477 478 479
			handle_eject_request(ds, event);
		else
			dock_event(ds, event, UNDOCK_EVENT);
L
Len Brown 已提交
480 481
		break;
	}
482
	return 0;
L
Len Brown 已提交
483 484
}

485 486 487
/*
 * show_docked - read method for "docked" file in sysfs
 */
488
static ssize_t docked_show(struct device *dev,
489 490
			   struct device_attribute *attr, char *buf)
{
491
	struct dock_station *dock_station = dev->platform_data;
492
	struct acpi_device *adev = acpi_fetch_acpi_dev(dock_station->handle);
493

494
	return sysfs_emit(buf, "%u\n", acpi_device_enumerated(adev));
495
}
496
static DEVICE_ATTR_RO(docked);
497

498 499 500
/*
 * show_flags - read method for flags file in sysfs
 */
501
static ssize_t flags_show(struct device *dev,
502 503
			  struct device_attribute *attr, char *buf)
{
504
	struct dock_station *dock_station = dev->platform_data;
505

506
	return sysfs_emit(buf, "%d\n", dock_station->flags);
507 508

}
509
static DEVICE_ATTR_RO(flags);
510

511 512 513
/*
 * write_undock - write method for "undock" file in sysfs
 */
514 515
static ssize_t undock_store(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
516 517
{
	int ret;
518
	struct dock_station *dock_station = dev->platform_data;
519 520 521 522

	if (!count)
		return -EINVAL;

523
	acpi_scan_lock_acquire();
524
	begin_undock(dock_station);
525
	ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
526
	acpi_scan_lock_release();
527
	return ret ? ret : count;
528
}
529
static DEVICE_ATTR_WO(undock);
530

531 532 533
/*
 * show_dock_uid - read method for "uid" file in sysfs
 */
534 535
static ssize_t uid_show(struct device *dev,
			struct device_attribute *attr, char *buf)
536
{
537
	unsigned long long lbuf;
538
	struct dock_station *dock_station = dev->platform_data;
539

540 541 542
	acpi_status status = acpi_evaluate_integer(dock_station->handle,
					"_UID", NULL, &lbuf);
	if (ACPI_FAILURE(status))
543
		return 0;
544

545
	return sysfs_emit(buf, "%llx\n", lbuf);
546
}
547
static DEVICE_ATTR_RO(uid);
548

549 550
static ssize_t type_show(struct device *dev,
			 struct device_attribute *attr, char *buf)
S
Shaohua Li 已提交
551
{
552
	struct dock_station *dock_station = dev->platform_data;
S
Shaohua Li 已提交
553 554 555 556 557 558 559 560 561 562 563
	char *type;

	if (dock_station->flags & DOCK_IS_DOCK)
		type = "dock_station";
	else if (dock_station->flags & DOCK_IS_ATA)
		type = "ata_bay";
	else if (dock_station->flags & DOCK_IS_BAT)
		type = "battery_bay";
	else
		type = "unknown";

564
	return sysfs_emit(buf, "%s\n", type);
S
Shaohua Li 已提交
565
}
566
static DEVICE_ATTR_RO(type);
S
Shaohua Li 已提交
567

568 569 570 571 572 573 574 575 576
static struct attribute *dock_attributes[] = {
	&dev_attr_docked.attr,
	&dev_attr_flags.attr,
	&dev_attr_undock.attr,
	&dev_attr_uid.attr,
	&dev_attr_type.attr,
	NULL
};

577
static const struct attribute_group dock_attribute_group = {
578 579 580
	.attrs = dock_attributes
};

L
Len Brown 已提交
581
/**
582 583
 * acpi_dock_add - Add a new dock station
 * @adev: Dock station ACPI device object.
L
Len Brown 已提交
584
 *
585
 * allocated and initialize a new dock station device.
L
Len Brown 已提交
586
 */
587
void acpi_dock_add(struct acpi_device *adev)
L
Len Brown 已提交
588
{
589
	struct dock_station *dock_station, ds = { NULL, };
590
	struct platform_device_info pdevinfo;
591
	acpi_handle handle = adev->handle;
592
	struct platform_device *dd;
593
	int ret;
L
Len Brown 已提交
594

595 596 597
	memset(&pdevinfo, 0, sizeof(pdevinfo));
	pdevinfo.name = "dock";
	pdevinfo.id = dock_station_count;
598
	pdevinfo.fwnode = acpi_fwnode_handle(adev);
599 600 601
	pdevinfo.data = &ds;
	pdevinfo.size_data = sizeof(ds);
	dd = platform_device_register_full(&pdevinfo);
602
	if (IS_ERR(dd))
603
		return;
604 605

	dock_station = dd->dev.platform_data;
L
Len Brown 已提交
606 607

	dock_station->handle = handle;
608
	dock_station->dock_device = dd;
L
Len Brown 已提交
609
	dock_station->last_dock_time = jiffies - HZ;
610 611 612

	INIT_LIST_HEAD(&dock_station->sibling);
	INIT_LIST_HEAD(&dock_station->dependent_devices);
613

614
	/* we want the dock device to send uevents */
615
	dev_set_uevent_suppress(&dd->dev, 0);
616

617
	if (acpi_dock_match(handle))
618
		dock_station->flags |= DOCK_IS_DOCK;
619
	if (acpi_ata_match(handle))
620
		dock_station->flags |= DOCK_IS_ATA;
621
	if (acpi_device_is_battery(adev))
622 623
		dock_station->flags |= DOCK_IS_BAT;

624
	ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
S
Shaohua Li 已提交
625
	if (ret)
626
		goto err_unregister;
627

L
Len Brown 已提交
628
	/* add the dock station as a device dependent on itself */
629
	ret = add_dock_dependent_device(dock_station, adev);
630
	if (ret)
631
		goto err_rmgroup;
L
Len Brown 已提交
632

633
	dock_station_count++;
A
Alex Chiang 已提交
634
	list_add(&dock_station->sibling, &dock_stations);
635 636 637 638
	adev->flags.is_dock_station = true;
	dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
		 dock_station_count);
	return;
L
Len Brown 已提交
639

640
err_rmgroup:
641
	sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
642

643
err_unregister:
644
	platform_device_unregister(dd);
645
	acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
L
Len Brown 已提交
646
}