Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  dock.c - ACPI dock station driver
  4 *
  5 *  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>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/kernel.h>
 11#include <linux/moduleparam.h>
 12#include <linux/slab.h>
 13#include <linux/init.h>
 14#include <linux/types.h>
 15#include <linux/notifier.h>
 16#include <linux/platform_device.h>
 17#include <linux/jiffies.h>
 18#include <linux/stddef.h>
 19#include <linux/acpi.h>
 20
 21#include "internal.h"
 22
 
 
 
 
 
 
 
 23static bool immediate_undock = 1;
 24module_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
 30struct dock_station {
 31	acpi_handle handle;
 32	unsigned long last_dock_time;
 33	u32 flags;
 34	struct list_head dependent_devices;
 35
 36	struct list_head sibling;
 37	struct platform_device *dock_device;
 38};
 39static LIST_HEAD(dock_stations);
 40static int dock_station_count;
 41
 42struct dock_dependent_device {
 43	struct list_head list;
 44	struct acpi_device *adev;
 45};
 46
 47#define DOCK_DOCKING	0x00000001
 48#define DOCK_UNDOCKING  0x00000002
 49#define DOCK_IS_DOCK	0x00000010
 50#define DOCK_IS_ATA	0x00000020
 51#define DOCK_IS_BAT	0x00000040
 52#define DOCK_EVENT	3
 53#define UNDOCK_EVENT	2
 54
 55enum dock_callback_type {
 56	DOCK_CALL_HANDLER,
 57	DOCK_CALL_FIXUP,
 58	DOCK_CALL_UEVENT,
 59};
 60
 61/*****************************************************************************
 62 *                         Dock Dependent device functions                   *
 63 *****************************************************************************/
 64/**
 65 * add_dock_dependent_device - associate a device with the dock station
 66 * @ds: Dock station.
 67 * @adev: Dependent ACPI device object.
 68 *
 69 * Add the dependent device to the dock's dependent device list.
 70 */
 71static int add_dock_dependent_device(struct dock_station *ds,
 72				     struct acpi_device *adev)
 73{
 74	struct dock_dependent_device *dd;
 75
 76	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
 77	if (!dd)
 78		return -ENOMEM;
 79
 80	dd->adev = adev;
 81	INIT_LIST_HEAD(&dd->list);
 82	list_add_tail(&dd->list, &ds->dependent_devices);
 83
 84	return 0;
 85}
 86
 87static 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	acpi_hp_fixup fixup = NULL;
 92	acpi_hp_uevent uevent = NULL;
 93	acpi_hp_notify notify = NULL;
 94
 95	acpi_lock_hp_context();
 96
 97	if (adev->hp) {
 98		if (cb_type == DOCK_CALL_FIXUP)
 99			fixup = adev->hp->fixup;
100		else if (cb_type == DOCK_CALL_UEVENT)
101			uevent = adev->hp->uevent;
102		else
103			notify = adev->hp->notify;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104	}
105
 
106	acpi_unlock_hp_context();
107
108	if (fixup)
109		fixup(adev);
110	else if (uevent)
111		uevent(adev, event);
112	else if (notify)
113		notify(adev, event);
114}
115
116static struct dock_station *find_dock_station(acpi_handle handle)
117{
118	struct dock_station *ds;
119
120	list_for_each_entry(ds, &dock_stations, sibling)
121		if (ds->handle == handle)
122			return ds;
123
124	return NULL;
125}
126
127/**
128 * find_dock_dependent_device - get a device dependent on this dock
129 * @ds: the dock station
130 * @adev: ACPI device object to find.
131 *
132 * iterate over the dependent device list for this dock.  If the
133 * dependent device matches the handle, return.
134 */
135static struct dock_dependent_device *
136find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
137{
138	struct dock_dependent_device *dd;
139
140	list_for_each_entry(dd, &ds->dependent_devices, list)
141		if (adev == dd->adev)
142			return dd;
143
144	return NULL;
145}
146
147void register_dock_dependent_device(struct acpi_device *adev,
148				    acpi_handle dshandle)
149{
150	struct dock_station *ds = find_dock_station(dshandle);
151
152	if (ds && !find_dock_dependent_device(ds, adev))
153		add_dock_dependent_device(ds, adev);
154}
155
156/*****************************************************************************
157 *                         Dock functions                                    *
158 *****************************************************************************/
159
160/**
161 * is_dock_device - see if a device is on a dock station
162 * @adev: ACPI device object to check.
163 *
164 * If this device is either the dock station itself,
165 * or is a device dependent on the dock station, then it
166 * is a dock device
167 */
168int is_dock_device(struct acpi_device *adev)
169{
170	struct dock_station *dock_station;
171
172	if (!dock_station_count)
173		return 0;
174
175	if (acpi_dock_match(adev->handle))
176		return 1;
177
178	list_for_each_entry(dock_station, &dock_stations, sibling)
179		if (find_dock_dependent_device(dock_station, adev))
180			return 1;
181
182	return 0;
183}
184EXPORT_SYMBOL_GPL(is_dock_device);
185
186/**
187 * dock_present - see if the dock station is present.
188 * @ds: the dock station
189 *
190 * execute the _STA method.  note that present does not
191 * imply that we are docked.
192 */
193static int dock_present(struct dock_station *ds)
194{
195	unsigned long long sta;
196	acpi_status status;
197
198	if (ds) {
199		status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
200		if (ACPI_SUCCESS(status) && sta)
201			return 1;
202	}
203	return 0;
204}
205
206/**
207 * hot_remove_dock_devices - Remove dock station devices.
208 * @ds: Dock station.
209 */
210static void hot_remove_dock_devices(struct dock_station *ds)
211{
212	struct dock_dependent_device *dd;
213
214	/*
215	 * Walk the list in reverse order so that devices that have been added
216	 * last are removed first (in case there are some indirect dependencies
217	 * between them).
218	 */
219	list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
220		dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST,
221				   DOCK_CALL_HANDLER);
222
223	list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
224		acpi_bus_trim(dd->adev);
225}
226
227/**
228 * hotplug_dock_devices - Insert devices on a dock station.
229 * @ds: the dock station
230 * @event: either bus check or device check request
231 *
232 * Some devices on the dock station need to have drivers called
233 * to perform hotplug operations after a dock event has occurred.
234 * Traverse the list of dock devices that have registered a
235 * hotplug handler, and call the handler.
236 */
237static void hotplug_dock_devices(struct dock_station *ds, u32 event)
238{
239	struct dock_dependent_device *dd;
240
241	/* Call driver specific post-dock fixups. */
242	list_for_each_entry(dd, &ds->dependent_devices, list)
243		dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
244
245	/* Call driver specific hotplug functions. */
246	list_for_each_entry(dd, &ds->dependent_devices, list)
247		dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
248
249	/*
250	 * Check if all devices have been enumerated already.  If not, run
251	 * acpi_bus_scan() for them and that will cause scan handlers to be
252	 * attached to device objects or acpi_drivers to be stopped/started if
253	 * they are present.
254	 */
255	list_for_each_entry(dd, &ds->dependent_devices, list) {
256		struct acpi_device *adev = dd->adev;
257
258		if (!acpi_device_enumerated(adev)) {
259			int ret = acpi_bus_scan(adev->handle);
260
261			if (ret)
262				dev_dbg(&adev->dev, "scan error %d\n", -ret);
263		}
264	}
265}
266
267static void dock_event(struct dock_station *ds, u32 event, int num)
268{
269	struct device *dev = &ds->dock_device->dev;
270	char event_string[13];
271	char *envp[] = { event_string, NULL };
272	struct dock_dependent_device *dd;
273
274	if (num == UNDOCK_EVENT)
275		sprintf(event_string, "EVENT=undock");
276	else
277		sprintf(event_string, "EVENT=dock");
278
279	/*
280	 * Indicate that the status of the dock station has
281	 * changed.
282	 */
283	if (num == DOCK_EVENT)
284		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
285
286	list_for_each_entry(dd, &ds->dependent_devices, list)
287		dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
288
289	if (num != DOCK_EVENT)
290		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
291}
292
293/**
294 * handle_dock - handle a dock event
295 * @ds: the dock station
296 * @dock: to dock, or undock - that is the question
297 *
298 * Execute the _DCK method in response to an acpi event
299 */
300static void handle_dock(struct dock_station *ds, int dock)
301{
302	acpi_status status;
303	struct acpi_object_list arg_list;
304	union acpi_object arg;
305	unsigned long long value;
306
307	acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
308
309	/* _DCK method has one argument */
310	arg_list.count = 1;
311	arg_list.pointer = &arg;
312	arg.type = ACPI_TYPE_INTEGER;
313	arg.integer.value = dock;
314	status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
315	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
316		acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
317				status);
318}
319
320static inline void dock(struct dock_station *ds)
321{
322	handle_dock(ds, 1);
323}
324
325static inline void undock(struct dock_station *ds)
326{
327	handle_dock(ds, 0);
328}
329
330static inline void begin_dock(struct dock_station *ds)
331{
332	ds->flags |= DOCK_DOCKING;
333}
334
335static inline void complete_dock(struct dock_station *ds)
336{
337	ds->flags &= ~(DOCK_DOCKING);
338	ds->last_dock_time = jiffies;
339}
340
341static inline void begin_undock(struct dock_station *ds)
342{
343	ds->flags |= DOCK_UNDOCKING;
344}
345
346static inline void complete_undock(struct dock_station *ds)
347{
348	ds->flags &= ~(DOCK_UNDOCKING);
349}
350
351/**
352 * dock_in_progress - see if we are in the middle of handling a dock event
353 * @ds: the dock station
354 *
355 * Sometimes while docking, false dock events can be sent to the driver
356 * because good connections aren't made or some other reason.  Ignore these
357 * if we are in the middle of doing something.
358 */
359static int dock_in_progress(struct dock_station *ds)
360{
361	if ((ds->flags & DOCK_DOCKING) ||
362	    time_before(jiffies, (ds->last_dock_time + HZ)))
363		return 1;
364	return 0;
365}
366
367/**
368 * handle_eject_request - handle an undock request checking for error conditions
369 * @ds: The dock station to undock.
370 * @event: The ACPI event number associated with the undock request.
371 *
372 * Check to make sure the dock device is still present, then undock and
373 * hotremove all the devices that may need removing.
374 */
375static int handle_eject_request(struct dock_station *ds, u32 event)
376{
377	if (dock_in_progress(ds))
378		return -EBUSY;
379
380	/*
381	 * here we need to generate the undock
382	 * event prior to actually doing the undock
383	 * so that the device struct still exists.
384	 * Also, even send the dock event if the
385	 * device is not present anymore
386	 */
387	dock_event(ds, event, UNDOCK_EVENT);
388
389	hot_remove_dock_devices(ds);
390	undock(ds);
391	acpi_evaluate_lck(ds->handle, 0);
392	acpi_evaluate_ej0(ds->handle);
393	if (dock_present(ds)) {
394		acpi_handle_err(ds->handle, "Unable to undock!\n");
395		return -EBUSY;
396	}
397	complete_undock(ds);
398	return 0;
399}
400
401/**
402 * dock_notify - Handle ACPI dock notification.
403 * @adev: Dock station's ACPI device object.
404 * @event: Event code.
405 *
406 * If we are notified to dock, then check to see if the dock is
407 * present and then dock.  Notify all drivers of the dock event,
408 * and then hotplug and devices that may need hotplugging.
409 */
410int dock_notify(struct acpi_device *adev, u32 event)
411{
412	acpi_handle handle = adev->handle;
413	struct dock_station *ds = find_dock_station(handle);
414	int surprise_removal = 0;
415
416	if (!ds)
417		return -ENODEV;
418
419	/*
420	 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
421	 * is sent and _DCK is present, it is assumed to mean an undock
422	 * request.
423	 */
424	if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
425		event = ACPI_NOTIFY_EJECT_REQUEST;
426
427	/*
428	 * dock station: BUS_CHECK - docked or surprise removal
429	 *		 DEVICE_CHECK - undocked
430	 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
431	 *
432	 * To simplify event handling, dock dependent device handler always
433	 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
434	 * ACPI_NOTIFY_EJECT_REQUEST for removal
435	 */
436	switch (event) {
437	case ACPI_NOTIFY_BUS_CHECK:
438	case ACPI_NOTIFY_DEVICE_CHECK:
439		if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
440			begin_dock(ds);
441			dock(ds);
442			if (!dock_present(ds)) {
443				acpi_handle_err(handle, "Unable to dock!\n");
444				complete_dock(ds);
445				break;
446			}
447			hotplug_dock_devices(ds, event);
448			complete_dock(ds);
449			dock_event(ds, event, DOCK_EVENT);
450			acpi_evaluate_lck(ds->handle, 1);
451			acpi_update_all_gpes();
452			break;
453		}
454		if (dock_present(ds) || dock_in_progress(ds))
455			break;
456		/* This is a surprise removal */
457		surprise_removal = 1;
458		event = ACPI_NOTIFY_EJECT_REQUEST;
459		/* Fall back */
460		fallthrough;
461	case ACPI_NOTIFY_EJECT_REQUEST:
462		begin_undock(ds);
463		if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
464		   || surprise_removal)
465			handle_eject_request(ds, event);
466		else
467			dock_event(ds, event, UNDOCK_EVENT);
468		break;
469	}
470	return 0;
471}
472
473/*
474 * show_docked - read method for "docked" file in sysfs
475 */
476static ssize_t docked_show(struct device *dev,
477			   struct device_attribute *attr, char *buf)
478{
479	struct dock_station *dock_station = dev->platform_data;
480	struct acpi_device *adev = acpi_fetch_acpi_dev(dock_station->handle);
481
482	return sysfs_emit(buf, "%u\n", acpi_device_enumerated(adev));
 
483}
484static DEVICE_ATTR_RO(docked);
485
486/*
487 * show_flags - read method for flags file in sysfs
488 */
489static ssize_t flags_show(struct device *dev,
490			  struct device_attribute *attr, char *buf)
491{
492	struct dock_station *dock_station = dev->platform_data;
493
494	return sysfs_emit(buf, "%d\n", dock_station->flags);
495
496}
497static DEVICE_ATTR_RO(flags);
498
499/*
500 * write_undock - write method for "undock" file in sysfs
501 */
502static ssize_t undock_store(struct device *dev, struct device_attribute *attr,
503			    const char *buf, size_t count)
504{
505	int ret;
506	struct dock_station *dock_station = dev->platform_data;
507
508	if (!count)
509		return -EINVAL;
510
511	acpi_scan_lock_acquire();
512	begin_undock(dock_station);
513	ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
514	acpi_scan_lock_release();
515	return ret ? ret : count;
516}
517static DEVICE_ATTR_WO(undock);
518
519/*
520 * show_dock_uid - read method for "uid" file in sysfs
521 */
522static ssize_t uid_show(struct device *dev,
523			struct device_attribute *attr, char *buf)
524{
525	unsigned long long lbuf;
526	struct dock_station *dock_station = dev->platform_data;
527
528	acpi_status status = acpi_evaluate_integer(dock_station->handle,
529					"_UID", NULL, &lbuf);
530	if (ACPI_FAILURE(status))
531		return 0;
532
533	return sysfs_emit(buf, "%llx\n", lbuf);
534}
535static DEVICE_ATTR_RO(uid);
536
537static ssize_t type_show(struct device *dev,
538			 struct device_attribute *attr, char *buf)
539{
540	struct dock_station *dock_station = dev->platform_data;
541	char *type;
542
543	if (dock_station->flags & DOCK_IS_DOCK)
544		type = "dock_station";
545	else if (dock_station->flags & DOCK_IS_ATA)
546		type = "ata_bay";
547	else if (dock_station->flags & DOCK_IS_BAT)
548		type = "battery_bay";
549	else
550		type = "unknown";
551
552	return sysfs_emit(buf, "%s\n", type);
553}
554static DEVICE_ATTR_RO(type);
555
556static struct attribute *dock_attributes[] = {
557	&dev_attr_docked.attr,
558	&dev_attr_flags.attr,
559	&dev_attr_undock.attr,
560	&dev_attr_uid.attr,
561	&dev_attr_type.attr,
562	NULL
563};
564
565static const struct attribute_group dock_attribute_group = {
566	.attrs = dock_attributes
567};
568
569/**
570 * acpi_dock_add - Add a new dock station
571 * @adev: Dock station ACPI device object.
572 *
573 * allocated and initialize a new dock station device.
574 */
575void acpi_dock_add(struct acpi_device *adev)
576{
577	struct dock_station *dock_station, ds = { NULL, };
578	struct platform_device_info pdevinfo;
579	acpi_handle handle = adev->handle;
580	struct platform_device *dd;
581	int ret;
582
583	memset(&pdevinfo, 0, sizeof(pdevinfo));
584	pdevinfo.name = "dock";
585	pdevinfo.id = dock_station_count;
586	pdevinfo.fwnode = acpi_fwnode_handle(adev);
587	pdevinfo.data = &ds;
588	pdevinfo.size_data = sizeof(ds);
589	dd = platform_device_register_full(&pdevinfo);
590	if (IS_ERR(dd))
591		return;
592
593	dock_station = dd->dev.platform_data;
594
595	dock_station->handle = handle;
596	dock_station->dock_device = dd;
597	dock_station->last_dock_time = jiffies - HZ;
598
599	INIT_LIST_HEAD(&dock_station->sibling);
600	INIT_LIST_HEAD(&dock_station->dependent_devices);
601
602	/* we want the dock device to send uevents */
603	dev_set_uevent_suppress(&dd->dev, 0);
604
605	if (acpi_dock_match(handle))
606		dock_station->flags |= DOCK_IS_DOCK;
607	if (acpi_ata_match(handle))
608		dock_station->flags |= DOCK_IS_ATA;
609	if (acpi_device_is_battery(adev))
610		dock_station->flags |= DOCK_IS_BAT;
611
612	ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
613	if (ret)
614		goto err_unregister;
615
616	/* add the dock station as a device dependent on itself */
617	ret = add_dock_dependent_device(dock_station, adev);
618	if (ret)
619		goto err_rmgroup;
620
621	dock_station_count++;
622	list_add(&dock_station->sibling, &dock_stations);
623	adev->flags.is_dock_station = true;
624	dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
625		 dock_station_count);
626	return;
627
628err_rmgroup:
629	sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
630
631err_unregister:
632	platform_device_unregister(dd);
633	acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
634}
v4.6
 
  1/*
  2 *  dock.c - ACPI dock station driver
  3 *
  4 *  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>
  7 *
  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 *
 20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 21 */
 22
 23#include <linux/kernel.h>
 24#include <linux/module.h>
 25#include <linux/slab.h>
 26#include <linux/init.h>
 27#include <linux/types.h>
 28#include <linux/notifier.h>
 29#include <linux/platform_device.h>
 30#include <linux/jiffies.h>
 31#include <linux/stddef.h>
 32#include <linux/acpi.h>
 33
 34#include "internal.h"
 35
 36#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
 37
 38ACPI_MODULE_NAME("dock");
 39MODULE_AUTHOR("Kristen Carlson Accardi");
 40MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
 41MODULE_LICENSE("GPL");
 42
 43static bool immediate_undock = 1;
 44module_param(immediate_undock, bool, 0644);
 45MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
 46	"undock immediately when the undock button is pressed, 0 will cause"
 47	" the driver to wait for userspace to write the undock sysfs file "
 48	" before undocking");
 49
 50struct dock_station {
 51	acpi_handle handle;
 52	unsigned long last_dock_time;
 53	u32 flags;
 54	struct list_head dependent_devices;
 55
 56	struct list_head sibling;
 57	struct platform_device *dock_device;
 58};
 59static LIST_HEAD(dock_stations);
 60static int dock_station_count;
 61
 62struct dock_dependent_device {
 63	struct list_head list;
 64	struct acpi_device *adev;
 65};
 66
 67#define DOCK_DOCKING	0x00000001
 68#define DOCK_UNDOCKING  0x00000002
 69#define DOCK_IS_DOCK	0x00000010
 70#define DOCK_IS_ATA	0x00000020
 71#define DOCK_IS_BAT	0x00000040
 72#define DOCK_EVENT	3
 73#define UNDOCK_EVENT	2
 74
 75enum dock_callback_type {
 76	DOCK_CALL_HANDLER,
 77	DOCK_CALL_FIXUP,
 78	DOCK_CALL_UEVENT,
 79};
 80
 81/*****************************************************************************
 82 *                         Dock Dependent device functions                   *
 83 *****************************************************************************/
 84/**
 85 * add_dock_dependent_device - associate a device with the dock station
 86 * @ds: Dock station.
 87 * @adev: Dependent ACPI device object.
 88 *
 89 * Add the dependent device to the dock's dependent device list.
 90 */
 91static int add_dock_dependent_device(struct dock_station *ds,
 92				     struct acpi_device *adev)
 93{
 94	struct dock_dependent_device *dd;
 95
 96	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
 97	if (!dd)
 98		return -ENOMEM;
 99
100	dd->adev = adev;
101	INIT_LIST_HEAD(&dd->list);
102	list_add_tail(&dd->list, &ds->dependent_devices);
103
104	return 0;
105}
106
107static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
108			       enum dock_callback_type cb_type)
109{
110	struct acpi_device *adev = dd->adev;
 
 
 
111
112	acpi_lock_hp_context();
113
114	if (!adev->hp)
115		goto out;
116
117	if (cb_type == DOCK_CALL_FIXUP) {
118		void (*fixup)(struct acpi_device *);
119
120		fixup = adev->hp->fixup;
121		if (fixup) {
122			acpi_unlock_hp_context();
123			fixup(adev);
124			return;
125		}
126	} else if (cb_type == DOCK_CALL_UEVENT) {
127		void (*uevent)(struct acpi_device *, u32);
128
129		uevent = adev->hp->uevent;
130		if (uevent) {
131			acpi_unlock_hp_context();
132			uevent(adev, event);
133			return;
134		}
135	} else {
136		int (*notify)(struct acpi_device *, u32);
137
138		notify = adev->hp->notify;
139		if (notify) {
140			acpi_unlock_hp_context();
141			notify(adev, event);
142			return;
143		}
144	}
145
146 out:
147	acpi_unlock_hp_context();
 
 
 
 
 
 
 
148}
149
150static struct dock_station *find_dock_station(acpi_handle handle)
151{
152	struct dock_station *ds;
153
154	list_for_each_entry(ds, &dock_stations, sibling)
155		if (ds->handle == handle)
156			return ds;
157
158	return NULL;
159}
160
161/**
162 * find_dock_dependent_device - get a device dependent on this dock
163 * @ds: the dock station
164 * @adev: ACPI device object to find.
165 *
166 * iterate over the dependent device list for this dock.  If the
167 * dependent device matches the handle, return.
168 */
169static struct dock_dependent_device *
170find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
171{
172	struct dock_dependent_device *dd;
173
174	list_for_each_entry(dd, &ds->dependent_devices, list)
175		if (adev == dd->adev)
176			return dd;
177
178	return NULL;
179}
180
181void register_dock_dependent_device(struct acpi_device *adev,
182				    acpi_handle dshandle)
183{
184	struct dock_station *ds = find_dock_station(dshandle);
185
186	if (ds && !find_dock_dependent_device(ds, adev))
187		add_dock_dependent_device(ds, adev);
188}
189
190/*****************************************************************************
191 *                         Dock functions                                    *
192 *****************************************************************************/
193
194/**
195 * is_dock_device - see if a device is on a dock station
196 * @adev: ACPI device object to check.
197 *
198 * If this device is either the dock station itself,
199 * or is a device dependent on the dock station, then it
200 * is a dock device
201 */
202int is_dock_device(struct acpi_device *adev)
203{
204	struct dock_station *dock_station;
205
206	if (!dock_station_count)
207		return 0;
208
209	if (acpi_dock_match(adev->handle))
210		return 1;
211
212	list_for_each_entry(dock_station, &dock_stations, sibling)
213		if (find_dock_dependent_device(dock_station, adev))
214			return 1;
215
216	return 0;
217}
218EXPORT_SYMBOL_GPL(is_dock_device);
219
220/**
221 * dock_present - see if the dock station is present.
222 * @ds: the dock station
223 *
224 * execute the _STA method.  note that present does not
225 * imply that we are docked.
226 */
227static int dock_present(struct dock_station *ds)
228{
229	unsigned long long sta;
230	acpi_status status;
231
232	if (ds) {
233		status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
234		if (ACPI_SUCCESS(status) && sta)
235			return 1;
236	}
237	return 0;
238}
239
240/**
241 * hot_remove_dock_devices - Remove dock station devices.
242 * @ds: Dock station.
243 */
244static void hot_remove_dock_devices(struct dock_station *ds)
245{
246	struct dock_dependent_device *dd;
247
248	/*
249	 * Walk the list in reverse order so that devices that have been added
250	 * last are removed first (in case there are some indirect dependencies
251	 * between them).
252	 */
253	list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
254		dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
 
255
256	list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
257		acpi_bus_trim(dd->adev);
258}
259
260/**
261 * hotplug_dock_devices - Insert devices on a dock station.
262 * @ds: the dock station
263 * @event: either bus check or device check request
264 *
265 * Some devices on the dock station need to have drivers called
266 * to perform hotplug operations after a dock event has occurred.
267 * Traverse the list of dock devices that have registered a
268 * hotplug handler, and call the handler.
269 */
270static void hotplug_dock_devices(struct dock_station *ds, u32 event)
271{
272	struct dock_dependent_device *dd;
273
274	/* Call driver specific post-dock fixups. */
275	list_for_each_entry(dd, &ds->dependent_devices, list)
276		dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
277
278	/* Call driver specific hotplug functions. */
279	list_for_each_entry(dd, &ds->dependent_devices, list)
280		dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
281
282	/*
283	 * Check if all devices have been enumerated already.  If not, run
284	 * acpi_bus_scan() for them and that will cause scan handlers to be
285	 * attached to device objects or acpi_drivers to be stopped/started if
286	 * they are present.
287	 */
288	list_for_each_entry(dd, &ds->dependent_devices, list) {
289		struct acpi_device *adev = dd->adev;
290
291		if (!acpi_device_enumerated(adev)) {
292			int ret = acpi_bus_scan(adev->handle);
 
293			if (ret)
294				dev_dbg(&adev->dev, "scan error %d\n", -ret);
295		}
296	}
297}
298
299static void dock_event(struct dock_station *ds, u32 event, int num)
300{
301	struct device *dev = &ds->dock_device->dev;
302	char event_string[13];
303	char *envp[] = { event_string, NULL };
304	struct dock_dependent_device *dd;
305
306	if (num == UNDOCK_EVENT)
307		sprintf(event_string, "EVENT=undock");
308	else
309		sprintf(event_string, "EVENT=dock");
310
311	/*
312	 * Indicate that the status of the dock station has
313	 * changed.
314	 */
315	if (num == DOCK_EVENT)
316		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
317
318	list_for_each_entry(dd, &ds->dependent_devices, list)
319		dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
320
321	if (num != DOCK_EVENT)
322		kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
323}
324
325/**
326 * handle_dock - handle a dock event
327 * @ds: the dock station
328 * @dock: to dock, or undock - that is the question
329 *
330 * Execute the _DCK method in response to an acpi event
331 */
332static void handle_dock(struct dock_station *ds, int dock)
333{
334	acpi_status status;
335	struct acpi_object_list arg_list;
336	union acpi_object arg;
337	unsigned long long value;
338
339	acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
340
341	/* _DCK method has one argument */
342	arg_list.count = 1;
343	arg_list.pointer = &arg;
344	arg.type = ACPI_TYPE_INTEGER;
345	arg.integer.value = dock;
346	status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
347	if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
348		acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
349				status);
350}
351
352static inline void dock(struct dock_station *ds)
353{
354	handle_dock(ds, 1);
355}
356
357static inline void undock(struct dock_station *ds)
358{
359	handle_dock(ds, 0);
360}
361
362static inline void begin_dock(struct dock_station *ds)
363{
364	ds->flags |= DOCK_DOCKING;
365}
366
367static inline void complete_dock(struct dock_station *ds)
368{
369	ds->flags &= ~(DOCK_DOCKING);
370	ds->last_dock_time = jiffies;
371}
372
373static inline void begin_undock(struct dock_station *ds)
374{
375	ds->flags |= DOCK_UNDOCKING;
376}
377
378static inline void complete_undock(struct dock_station *ds)
379{
380	ds->flags &= ~(DOCK_UNDOCKING);
381}
382
383/**
384 * dock_in_progress - see if we are in the middle of handling a dock event
385 * @ds: the dock station
386 *
387 * Sometimes while docking, false dock events can be sent to the driver
388 * because good connections aren't made or some other reason.  Ignore these
389 * if we are in the middle of doing something.
390 */
391static int dock_in_progress(struct dock_station *ds)
392{
393	if ((ds->flags & DOCK_DOCKING) ||
394	    time_before(jiffies, (ds->last_dock_time + HZ)))
395		return 1;
396	return 0;
397}
398
399/**
400 * handle_eject_request - handle an undock request checking for error conditions
 
 
401 *
402 * Check to make sure the dock device is still present, then undock and
403 * hotremove all the devices that may need removing.
404 */
405static int handle_eject_request(struct dock_station *ds, u32 event)
406{
407	if (dock_in_progress(ds))
408		return -EBUSY;
409
410	/*
411	 * here we need to generate the undock
412	 * event prior to actually doing the undock
413	 * so that the device struct still exists.
414	 * Also, even send the dock event if the
415	 * device is not present anymore
416	 */
417	dock_event(ds, event, UNDOCK_EVENT);
418
419	hot_remove_dock_devices(ds);
420	undock(ds);
421	acpi_evaluate_lck(ds->handle, 0);
422	acpi_evaluate_ej0(ds->handle);
423	if (dock_present(ds)) {
424		acpi_handle_err(ds->handle, "Unable to undock!\n");
425		return -EBUSY;
426	}
427	complete_undock(ds);
428	return 0;
429}
430
431/**
432 * dock_notify - Handle ACPI dock notification.
433 * @adev: Dock station's ACPI device object.
434 * @event: Event code.
435 *
436 * If we are notified to dock, then check to see if the dock is
437 * present and then dock.  Notify all drivers of the dock event,
438 * and then hotplug and devices that may need hotplugging.
439 */
440int dock_notify(struct acpi_device *adev, u32 event)
441{
442	acpi_handle handle = adev->handle;
443	struct dock_station *ds = find_dock_station(handle);
444	int surprise_removal = 0;
445
446	if (!ds)
447		return -ENODEV;
448
449	/*
450	 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
451	 * is sent and _DCK is present, it is assumed to mean an undock
452	 * request.
453	 */
454	if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
455		event = ACPI_NOTIFY_EJECT_REQUEST;
456
457	/*
458	 * dock station: BUS_CHECK - docked or surprise removal
459	 *		 DEVICE_CHECK - undocked
460	 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
461	 *
462	 * To simplify event handling, dock dependent device handler always
463	 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
464	 * ACPI_NOTIFY_EJECT_REQUEST for removal
465	 */
466	switch (event) {
467	case ACPI_NOTIFY_BUS_CHECK:
468	case ACPI_NOTIFY_DEVICE_CHECK:
469		if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
470			begin_dock(ds);
471			dock(ds);
472			if (!dock_present(ds)) {
473				acpi_handle_err(handle, "Unable to dock!\n");
474				complete_dock(ds);
475				break;
476			}
477			hotplug_dock_devices(ds, event);
478			complete_dock(ds);
479			dock_event(ds, event, DOCK_EVENT);
480			acpi_evaluate_lck(ds->handle, 1);
481			acpi_update_all_gpes();
482			break;
483		}
484		if (dock_present(ds) || dock_in_progress(ds))
485			break;
486		/* This is a surprise removal */
487		surprise_removal = 1;
488		event = ACPI_NOTIFY_EJECT_REQUEST;
489		/* Fall back */
 
490	case ACPI_NOTIFY_EJECT_REQUEST:
491		begin_undock(ds);
492		if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
493		   || surprise_removal)
494			handle_eject_request(ds, event);
495		else
496			dock_event(ds, event, UNDOCK_EVENT);
497		break;
498	}
499	return 0;
500}
501
502/*
503 * show_docked - read method for "docked" file in sysfs
504 */
505static ssize_t show_docked(struct device *dev,
506			   struct device_attribute *attr, char *buf)
507{
508	struct dock_station *dock_station = dev->platform_data;
509	struct acpi_device *adev = NULL;
510
511	acpi_bus_get_device(dock_station->handle, &adev);
512	return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
513}
514static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
515
516/*
517 * show_flags - read method for flags file in sysfs
518 */
519static ssize_t show_flags(struct device *dev,
520			  struct device_attribute *attr, char *buf)
521{
522	struct dock_station *dock_station = dev->platform_data;
523	return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
 
524
525}
526static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
527
528/*
529 * write_undock - write method for "undock" file in sysfs
530 */
531static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
532			   const char *buf, size_t count)
533{
534	int ret;
535	struct dock_station *dock_station = dev->platform_data;
536
537	if (!count)
538		return -EINVAL;
539
540	acpi_scan_lock_acquire();
541	begin_undock(dock_station);
542	ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
543	acpi_scan_lock_release();
544	return ret ? ret: count;
545}
546static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
547
548/*
549 * show_dock_uid - read method for "uid" file in sysfs
550 */
551static ssize_t show_dock_uid(struct device *dev,
552			     struct device_attribute *attr, char *buf)
553{
554	unsigned long long lbuf;
555	struct dock_station *dock_station = dev->platform_data;
 
556	acpi_status status = acpi_evaluate_integer(dock_station->handle,
557					"_UID", NULL, &lbuf);
558	if (ACPI_FAILURE(status))
559	    return 0;
560
561	return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
562}
563static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
564
565static ssize_t show_dock_type(struct device *dev,
566		struct device_attribute *attr, char *buf)
567{
568	struct dock_station *dock_station = dev->platform_data;
569	char *type;
570
571	if (dock_station->flags & DOCK_IS_DOCK)
572		type = "dock_station";
573	else if (dock_station->flags & DOCK_IS_ATA)
574		type = "ata_bay";
575	else if (dock_station->flags & DOCK_IS_BAT)
576		type = "battery_bay";
577	else
578		type = "unknown";
579
580	return snprintf(buf, PAGE_SIZE, "%s\n", type);
581}
582static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
583
584static struct attribute *dock_attributes[] = {
585	&dev_attr_docked.attr,
586	&dev_attr_flags.attr,
587	&dev_attr_undock.attr,
588	&dev_attr_uid.attr,
589	&dev_attr_type.attr,
590	NULL
591};
592
593static struct attribute_group dock_attribute_group = {
594	.attrs = dock_attributes
595};
596
597/**
598 * acpi_dock_add - Add a new dock station
599 * @adev: Dock station ACPI device object.
600 *
601 * allocated and initialize a new dock station device.
602 */
603void acpi_dock_add(struct acpi_device *adev)
604{
605	struct dock_station *dock_station, ds = { NULL, };
606	struct platform_device_info pdevinfo;
607	acpi_handle handle = adev->handle;
608	struct platform_device *dd;
609	int ret;
610
611	memset(&pdevinfo, 0, sizeof(pdevinfo));
612	pdevinfo.name = "dock";
613	pdevinfo.id = dock_station_count;
614	pdevinfo.fwnode = acpi_fwnode_handle(adev);
615	pdevinfo.data = &ds;
616	pdevinfo.size_data = sizeof(ds);
617	dd = platform_device_register_full(&pdevinfo);
618	if (IS_ERR(dd))
619		return;
620
621	dock_station = dd->dev.platform_data;
622
623	dock_station->handle = handle;
624	dock_station->dock_device = dd;
625	dock_station->last_dock_time = jiffies - HZ;
626
627	INIT_LIST_HEAD(&dock_station->sibling);
628	INIT_LIST_HEAD(&dock_station->dependent_devices);
629
630	/* we want the dock device to send uevents */
631	dev_set_uevent_suppress(&dd->dev, 0);
632
633	if (acpi_dock_match(handle))
634		dock_station->flags |= DOCK_IS_DOCK;
635	if (acpi_ata_match(handle))
636		dock_station->flags |= DOCK_IS_ATA;
637	if (acpi_device_is_battery(adev))
638		dock_station->flags |= DOCK_IS_BAT;
639
640	ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
641	if (ret)
642		goto err_unregister;
643
644	/* add the dock station as a device dependent on itself */
645	ret = add_dock_dependent_device(dock_station, adev);
646	if (ret)
647		goto err_rmgroup;
648
649	dock_station_count++;
650	list_add(&dock_station->sibling, &dock_stations);
651	adev->flags.is_dock_station = true;
652	dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
653		 dock_station_count);
654	return;
655
656err_rmgroup:
657	sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
658
659err_unregister:
660	platform_device_unregister(dd);
661	acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
662}