dock.c 21.8 KB
Newer Older
L
Len Brown 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *  dock.c - ACPI dock station driver
 *
 *  Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or (at
 *  your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */

#include <linux/kernel.h>
#include <linux/module.h>
27
#include <linux/slab.h>
L
Len Brown 已提交
28 29 30
#include <linux/init.h>
#include <linux/types.h>
#include <linux/notifier.h>
31
#include <linux/platform_device.h>
32
#include <linux/jiffies.h>
R
Randy Dunlap 已提交
33
#include <linux/stddef.h>
34
#include <linux/acpi.h>
L
Len Brown 已提交
35

36 37
#include "internal.h"

38 39
#define PREFIX "ACPI: "

40
#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
L
Len Brown 已提交
41

42
ACPI_MODULE_NAME("dock");
L
Len Brown 已提交
43
MODULE_AUTHOR("Kristen Carlson Accardi");
44
MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
L
Len Brown 已提交
45 46
MODULE_LICENSE("GPL");

47
static bool immediate_undock = 1;
48 49 50 51 52 53
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");

54 55 56 57 58 59
static const struct acpi_device_id dock_device_ids[] = {
	{"LNXDOCK", 0},
	{"", 0},
};
MODULE_DEVICE_TABLE(acpi, dock_device_ids);

L
Len Brown 已提交
60 61 62 63 64
struct dock_station {
	acpi_handle handle;
	unsigned long last_dock_time;
	u32 flags;
	struct list_head dependent_devices;
65

A
Alex Chiang 已提交
66
	struct list_head sibling;
67
	struct platform_device *dock_device;
L
Len Brown 已提交
68
};
69 70
static LIST_HEAD(dock_stations);
static int dock_station_count;
71
static DEFINE_MUTEX(hotplug_lock);
L
Len Brown 已提交
72 73 74

struct dock_dependent_device {
	struct list_head list;
75
	struct acpi_device *adev;
76 77 78 79
	const struct acpi_dock_ops *hp_ops;
	void *hp_context;
	unsigned int hp_refcount;
	void (*hp_release)(void *);
L
Len Brown 已提交
80 81 82
};

#define DOCK_DOCKING	0x00000001
83
#define DOCK_UNDOCKING  0x00000002
84 85 86
#define DOCK_IS_DOCK	0x00000010
#define DOCK_IS_ATA	0x00000020
#define DOCK_IS_BAT	0x00000040
87 88
#define DOCK_EVENT	3
#define UNDOCK_EVENT	2
L
Len Brown 已提交
89

90 91 92 93 94 95
enum dock_callback_type {
	DOCK_CALL_HANDLER,
	DOCK_CALL_FIXUP,
	DOCK_CALL_UEVENT,
};

L
Len Brown 已提交
96 97 98 99
/*****************************************************************************
 *                         Dock Dependent device functions                   *
 *****************************************************************************/
/**
100
 * add_dock_dependent_device - associate a device with the dock station
101 102
 * @ds: Dock station.
 * @adev: Dependent ACPI device object.
L
Len Brown 已提交
103
 *
104
 * Add the dependent device to the dock's dependent device list.
L
Len Brown 已提交
105
 */
106 107
static int add_dock_dependent_device(struct dock_station *ds,
				     struct acpi_device *adev)
L
Len Brown 已提交
108 109 110 111
{
	struct dock_dependent_device *dd;

	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
112 113 114
	if (!dd)
		return -ENOMEM;

115
	dd->adev = adev;
116
	INIT_LIST_HEAD(&dd->list);
L
Len Brown 已提交
117
	list_add_tail(&dd->list, &ds->dependent_devices);
118 119

	return 0;
L
Len Brown 已提交
120 121
}

122 123 124 125 126 127 128 129 130 131
static void remove_dock_dependent_devices(struct dock_station *ds)
{
	struct dock_dependent_device *dd, *aux;

	list_for_each_entry_safe(dd, aux, &ds->dependent_devices, list) {
		list_del(&dd->list);
		kfree(dd);
	}
}

L
Len Brown 已提交
132
/**
133 134 135 136 137 138
 * dock_init_hotplug - Initialize a hotplug device on a docking station.
 * @dd: Dock-dependent device.
 * @ops: Dock operations to attach to the dependent device.
 * @context: Data to pass to the @ops callbacks and @release.
 * @init: Optional initialization routine to run after setting up context.
 * @release: Optional release routine to run on removal.
L
Len Brown 已提交
139
 */
140 141 142
static int dock_init_hotplug(struct dock_dependent_device *dd,
			     const struct acpi_dock_ops *ops, void *context,
			     void (*init)(void *), void (*release)(void *))
L
Len Brown 已提交
143
{
144 145 146
	int ret = 0;

	mutex_lock(&hotplug_lock);
147
	if (WARN_ON(dd->hp_context)) {
148 149 150 151 152 153
		ret = -EEXIST;
	} else {
		dd->hp_refcount = 1;
		dd->hp_ops = ops;
		dd->hp_context = context;
		dd->hp_release = release;
154 155
		if (init)
			init(context);
156 157 158
	}
	mutex_unlock(&hotplug_lock);
	return ret;
L
Len Brown 已提交
159 160 161
}

/**
162 163
 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
 * @dd: Dock-dependent device.
L
Len Brown 已提交
164
 *
165 166 167
 * Decrement the reference counter of @dd and if 0, detach its hotplug
 * operations from it, reset its context pointer and run the optional release
 * routine if present.
L
Len Brown 已提交
168
 */
169
static void dock_release_hotplug(struct dock_dependent_device *dd)
L
Len Brown 已提交
170
{
171 172
	mutex_lock(&hotplug_lock);
	if (dd->hp_context && !--dd->hp_refcount) {
173 174 175
		void (*release)(void *) = dd->hp_release;
		void *context = dd->hp_context;

176 177 178
		dd->hp_ops = NULL;
		dd->hp_context = NULL;
		dd->hp_release = NULL;
179 180
		if (release)
			release(context);
181 182 183 184 185
	}
	mutex_unlock(&hotplug_lock);
}

static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
186
			       enum dock_callback_type cb_type)
187
{
188
	struct acpi_device *adev = dd->adev;
189 190 191
	acpi_notify_handler cb = NULL;
	bool run = false;

192 193 194 195 196 197 198 199 200 201 202 203 204 205
	acpi_lock_hp_context();

	if (!adev->hp)
		goto no_context;

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

		fixup = adev->hp->fixup;
		if (fixup) {
			acpi_unlock_hp_context();
			fixup(adev);
			return;
		}
206 207 208 209 210 211 212 213 214
	} 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;
		}
215 216 217
	} else {
		int (*notify)(struct acpi_device *, u32);

218
		notify = adev->hp->notify;
219 220 221 222 223 224 225 226 227 228
		if (notify) {
			acpi_unlock_hp_context();
			notify(adev, event);
			return;
		}
	}

 no_context:
	acpi_unlock_hp_context();

229 230 231 232 233
	mutex_lock(&hotplug_lock);

	if (dd->hp_context) {
		run = true;
		dd->hp_refcount++;
234 235 236 237 238 239 240 241 242 243 244 245
		if (dd->hp_ops) {
			switch (cb_type) {
			case DOCK_CALL_FIXUP:
				cb = dd->hp_ops->fixup;
				break;
			case DOCK_CALL_UEVENT:
				cb = dd->hp_ops->uevent;
				break;
			default:
				cb = dd->hp_ops->handler;
			}
		}
246 247 248 249 250 251 252 253
	}

	mutex_unlock(&hotplug_lock);

	if (!run)
		return;

	if (cb)
254
		cb(dd->adev->handle, event, dd->hp_context);
255 256

	dock_release_hotplug(dd);
L
Len Brown 已提交
257 258
}

259 260 261 262 263 264 265 266 267 268 269
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 已提交
270 271 272
/**
 * find_dock_dependent_device - get a device dependent on this dock
 * @ds: the dock station
273
 * @adev: ACPI device object to find.
L
Len Brown 已提交
274 275 276 277 278
 *
 * iterate over the dependent device list for this dock.  If the
 * dependent device matches the handle, return.
 */
static struct dock_dependent_device *
279
find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
L
Len Brown 已提交
280 281 282
{
	struct dock_dependent_device *dd;

283
	list_for_each_entry(dd, &ds->dependent_devices, list)
284
		if (adev == dd->adev)
L
Len Brown 已提交
285
			return dd;
286

L
Len Brown 已提交
287 288 289
	return NULL;
}

290 291
void register_dock_dependent_device(struct acpi_device *adev,
				    acpi_handle dshandle)
292
{
293
	struct dock_station *ds = find_dock_station(dshandle);
294

295 296
	if (ds && !find_dock_dependent_device(ds, adev))
		add_dock_dependent_device(ds, adev);
297 298
}

299 300 301
/*****************************************************************************
 *                         Dock functions                                    *
 *****************************************************************************/
302

L
Len Brown 已提交
303 304
/**
 * is_dock_device - see if a device is on a dock station
305
 * @adev: ACPI device object to check.
L
Len Brown 已提交
306 307 308 309 310
 *
 * If this device is either the dock station itself,
 * or is a device dependent on the dock station, then it
 * is a dock device
 */
311
int is_dock_device(struct acpi_device *adev)
L
Len Brown 已提交
312
{
313 314 315
	struct dock_station *dock_station;

	if (!dock_station_count)
L
Len Brown 已提交
316 317
		return 0;

318
	if (acpi_dock_match(adev->handle))
L
Len Brown 已提交
319
		return 1;
320 321

	list_for_each_entry(dock_station, &dock_stations, sibling)
322
		if (find_dock_dependent_device(dock_station, adev))
323
			return 1;
L
Len Brown 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337

	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)
{
338
	unsigned long long sta;
L
Len Brown 已提交
339 340 341 342 343 344 345 346 347 348 349
	acpi_status status;

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

/**
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
 * 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)
		dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);

	list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
366
		acpi_bus_trim(dd->adev);
367 368 369 370
}

/**
 * hotplug_dock_devices - Insert devices on a dock station.
L
Len Brown 已提交
371
 * @ds: the dock station
372
 * @event: either bus check or device check request
L
Len Brown 已提交
373 374 375 376 377 378 379 380 381 382
 *
 * 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;

383 384 385 386
	/* Call driver specific post-dock fixups. */
	list_for_each_entry(dd, &ds->dependent_devices, list)
		dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);

387
	/* Call driver specific hotplug functions. */
388
	list_for_each_entry(dd, &ds->dependent_devices, list)
389
		dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
L
Len Brown 已提交
390 391

	/*
392 393 394 395
	 * 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 已提交
396
	 */
397 398 399 400 401 402 403 404 405
	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);
			if (ret)
				dev_dbg(&adev->dev, "scan error %d\n", -ret);
		}
	}
L
Len Brown 已提交
406 407 408 409
}

static void dock_event(struct dock_station *ds, u32 event, int num)
{
410
	struct device *dev = &ds->dock_device->dev;
411
	char event_string[13];
412
	char *envp[] = { event_string, NULL };
413
	struct dock_dependent_device *dd;
414 415

	if (num == UNDOCK_EVENT)
416
		sprintf(event_string, "EVENT=undock");
417
	else
418
		sprintf(event_string, "EVENT=dock");
419

420
	/*
421 422
	 * Indicate that the status of the dock station has
	 * changed.
423
	 */
424 425 426
	if (num == DOCK_EVENT)
		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);

427
	list_for_each_entry(dd, &ds->dependent_devices, list)
428
		dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
429

430 431
	if (num != DOCK_EVENT)
		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
L
Len Brown 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445
}

/**
 * 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;
446
	unsigned long long value;
L
Len Brown 已提交
447

448
	acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
L
Len Brown 已提交
449 450 451 452 453 454

	/* _DCK method has one argument */
	arg_list.count = 1;
	arg_list.pointer = &arg;
	arg.type = ACPI_TYPE_INTEGER;
	arg.integer.value = dock;
455
	status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
456
	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
457 458
		acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
				status);
L
Len Brown 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
}

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;
}

482 483 484 485 486 487 488 489 490 491
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 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/**
 * 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;
}

/**
 * register_hotplug_dock_device - register a hotplug function
 * @handle: the handle of the device
511
 * @ops: handlers to call after docking
L
Len Brown 已提交
512
 * @context: device specific data
513 514
 * @init: Optional initialization routine to run after registration
 * @release: Optional release routine to run on unregistration
L
Len Brown 已提交
515 516 517 518 519
 *
 * If a driver would like to perform a hotplug operation after a dock
 * event, they can register an acpi_notifiy_handler to be called by
 * the dock driver after _DCK is executed.
 */
520 521 522
int register_hotplug_dock_device(acpi_handle handle,
				 const struct acpi_dock_ops *ops, void *context,
				 void (*init)(void *), void (*release)(void *))
L
Len Brown 已提交
523 524
{
	struct dock_dependent_device *dd;
525
	struct dock_station *dock_station;
526
	struct acpi_device *adev;
527
	int ret = -EINVAL;
L
Len Brown 已提交
528

529 530 531
	if (WARN_ON(!context))
		return -EINVAL;

532
	if (!dock_station_count)
L
Len Brown 已提交
533 534
		return -ENODEV;

535 536 537 538
	ret = acpi_bus_get_device(handle, &adev);
	if (ret)
		return ret;

L
Len Brown 已提交
539 540 541 542
	/*
	 * make sure this handle is for a device dependent on the dock,
	 * this would include the dock station itself
	 */
A
Alex Chiang 已提交
543
	list_for_each_entry(dock_station, &dock_stations, sibling) {
544 545
		/*
		 * An ATA bay can be in a dock and itself can be ejected
D
Daniel Mack 已提交
546
		 * separately, so there are two 'dock stations' which need the
547 548
		 * ops
		 */
549
		dd = find_dock_dependent_device(dock_station, adev);
550
		if (dd && !dock_init_hotplug(dd, ops, context, init, release))
551
			ret = 0;
L
Len Brown 已提交
552 553
	}

554
	return ret;
L
Len Brown 已提交
555 556 557 558 559 560 561 562 563 564
}
EXPORT_SYMBOL_GPL(register_hotplug_dock_device);

/**
 * unregister_hotplug_dock_device - remove yourself from the hotplug list
 * @handle: the acpi handle of the device
 */
void unregister_hotplug_dock_device(acpi_handle handle)
{
	struct dock_dependent_device *dd;
565
	struct dock_station *dock_station;
566
	struct acpi_device *adev;
L
Len Brown 已提交
567

568
	if (!dock_station_count)
L
Len Brown 已提交
569 570
		return;

571 572 573
	if (acpi_bus_get_device(handle, &adev))
		return;

A
Alex Chiang 已提交
574
	list_for_each_entry(dock_station, &dock_stations, sibling) {
575
		dd = find_dock_dependent_device(dock_station, adev);
576
		if (dd)
577
			dock_release_hotplug(dd);
578
	}
L
Len Brown 已提交
579 580 581
}
EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);

582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
/**
 * 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.
597 598
	 * Also, even send the dock event if the
	 * device is not present anymore
599 600
	 */
	dock_event(ds, event, UNDOCK_EVENT);
601

602
	hot_remove_dock_devices(ds);
603
	undock(ds);
604 605
	acpi_evaluate_lck(ds->handle, 0);
	acpi_evaluate_ej0(ds->handle);
606
	if (dock_present(ds)) {
607
		acpi_handle_err(ds->handle, "Unable to undock!\n");
608 609
		return -EBUSY;
	}
610
	complete_undock(ds);
611 612 613
	return 0;
}

L
Len Brown 已提交
614
/**
615 616 617
 * dock_notify - Handle ACPI dock notification.
 * @adev: Dock station's ACPI device object.
 * @event: Event code.
L
Len Brown 已提交
618 619 620
 *
 * 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,
621
 * and then hotplug and devices that may need hotplugging.
L
Len Brown 已提交
622
 */
623
int dock_notify(struct acpi_device *adev, u32 event)
L
Len Brown 已提交
624
{
625 626
	acpi_handle handle = adev->handle;
	struct dock_station *ds = find_dock_station(handle);
627
	int surprise_removal = 0;
L
Len Brown 已提交
628

629 630 631
	if (!ds)
		return -ENODEV;

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	/*
	 * 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 已提交
649 650
	switch (event) {
	case ACPI_NOTIFY_BUS_CHECK:
651
	case ACPI_NOTIFY_DEVICE_CHECK:
652
		if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
L
Len Brown 已提交
653 654 655
			begin_dock(ds);
			dock(ds);
			if (!dock_present(ds)) {
656
				acpi_handle_err(handle, "Unable to dock!\n");
S
Shaohua Li 已提交
657
				complete_dock(ds);
L
Len Brown 已提交
658 659 660 661 662
				break;
			}
			hotplug_dock_devices(ds, event);
			complete_dock(ds);
			dock_event(ds, event, DOCK_EVENT);
663
			acpi_evaluate_lck(ds->handle, 1);
664
			acpi_update_all_gpes();
665
			break;
L
Len Brown 已提交
666
		}
667 668 669 670 671 672
		if (dock_present(ds) || dock_in_progress(ds))
			break;
		/* This is a surprise removal */
		surprise_removal = 1;
		event = ACPI_NOTIFY_EJECT_REQUEST;
		/* Fall back */
L
Len Brown 已提交
673
	case ACPI_NOTIFY_EJECT_REQUEST:
674
		begin_undock(ds);
675 676
		if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
		   || surprise_removal)
677 678 679
			handle_eject_request(ds, event);
		else
			dock_event(ds, event, UNDOCK_EVENT);
L
Len Brown 已提交
680 681
		break;
	}
682
	return 0;
L
Len Brown 已提交
683 684
}

685 686 687 688 689 690
/*
 * show_docked - read method for "docked" file in sysfs
 */
static ssize_t show_docked(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
691
	struct dock_station *dock_station = dev->platform_data;
692
	struct acpi_device *adev = NULL;
693

694 695
	acpi_bus_get_device(dock_station->handle, &adev);
	return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
696
}
A
Adrian Bunk 已提交
697
static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
698

699 700 701 702 703 704
/*
 * show_flags - read method for flags file in sysfs
 */
static ssize_t show_flags(struct device *dev,
			  struct device_attribute *attr, char *buf)
{
705
	struct dock_station *dock_station = dev->platform_data;
706 707 708
	return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);

}
A
Adrian Bunk 已提交
709
static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
710

711 712 713 714 715 716 717
/*
 * write_undock - write method for "undock" file in sysfs
 */
static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
{
	int ret;
718
	struct dock_station *dock_station = dev->platform_data;
719 720 721 722

	if (!count)
		return -EINVAL;

723
	acpi_scan_lock_acquire();
724
	begin_undock(dock_station);
725
	ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
726
	acpi_scan_lock_release();
727 728
	return ret ? ret: count;
}
A
Adrian Bunk 已提交
729
static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
730

731 732 733 734 735 736
/*
 * show_dock_uid - read method for "uid" file in sysfs
 */
static ssize_t show_dock_uid(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
737
	unsigned long long lbuf;
738
	struct dock_station *dock_station = dev->platform_data;
739 740 741
	acpi_status status = acpi_evaluate_integer(dock_station->handle,
					"_UID", NULL, &lbuf);
	if (ACPI_FAILURE(status))
742
	    return 0;
743

744
	return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
745
}
A
Adrian Bunk 已提交
746
static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
747

S
Shaohua Li 已提交
748 749 750
static ssize_t show_dock_type(struct device *dev,
		struct device_attribute *attr, char *buf)
{
751
	struct dock_station *dock_station = dev->platform_data;
S
Shaohua Li 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	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";

	return snprintf(buf, PAGE_SIZE, "%s\n", type);
}
static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);

767 768 769 770 771 772 773 774 775 776 777 778 779
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
};

static struct attribute_group dock_attribute_group = {
	.attrs = dock_attributes
};

L
Len Brown 已提交
780
/**
781 782
 * acpi_dock_add - Add a new dock station
 * @adev: Dock station ACPI device object.
L
Len Brown 已提交
783
 *
784
 * allocated and initialize a new dock station device.
L
Len Brown 已提交
785
 */
786
void acpi_dock_add(struct acpi_device *adev)
L
Len Brown 已提交
787
{
788
	struct dock_station *dock_station, ds = { NULL, };
789
	struct platform_device_info pdevinfo;
790
	acpi_handle handle = adev->handle;
791
	struct platform_device *dd;
792
	int ret;
L
Len Brown 已提交
793

794 795 796 797 798 799 800
	memset(&pdevinfo, 0, sizeof(pdevinfo));
	pdevinfo.name = "dock";
	pdevinfo.id = dock_station_count;
	pdevinfo.acpi_node.companion = adev;
	pdevinfo.data = &ds;
	pdevinfo.size_data = sizeof(ds);
	dd = platform_device_register_full(&pdevinfo);
801
	if (IS_ERR(dd))
802
		return;
803 804

	dock_station = dd->dev.platform_data;
L
Len Brown 已提交
805 806

	dock_station->handle = handle;
807
	dock_station->dock_device = dd;
L
Len Brown 已提交
808
	dock_station->last_dock_time = jiffies - HZ;
809 810 811

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

813
	/* we want the dock device to send uevents */
814
	dev_set_uevent_suppress(&dd->dev, 0);
815

816
	if (acpi_dock_match(handle))
817
		dock_station->flags |= DOCK_IS_DOCK;
818
	if (acpi_ata_match(handle))
819
		dock_station->flags |= DOCK_IS_ATA;
820
	if (acpi_device_is_battery(adev))
821 822
		dock_station->flags |= DOCK_IS_BAT;

823
	ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
S
Shaohua Li 已提交
824
	if (ret)
825
		goto err_unregister;
826

L
Len Brown 已提交
827
	/* add the dock station as a device dependent on itself */
828
	ret = add_dock_dependent_device(dock_station, adev);
829
	if (ret)
830
		goto err_rmgroup;
L
Len Brown 已提交
831

832
	dock_station_count++;
A
Alex Chiang 已提交
833
	list_add(&dock_station->sibling, &dock_stations);
834 835 836 837
	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 已提交
838

839
err_rmgroup:
840
	remove_dock_dependent_devices(dock_station);
841
	sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
842
err_unregister:
843
	platform_device_unregister(dd);
844
	acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
L
Len Brown 已提交
845
}