xref: /openbmc/linux/drivers/acpi/dock.c (revision e67beb37)
1c8f7a62cSLen Brown /*
2c8f7a62cSLen Brown  *  dock.c - ACPI dock station driver
3c8f7a62cSLen Brown  *
4c8f7a62cSLen Brown  *  Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
5c8f7a62cSLen Brown  *
6c8f7a62cSLen Brown  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7c8f7a62cSLen Brown  *
8c8f7a62cSLen Brown  *  This program is free software; you can redistribute it and/or modify
9c8f7a62cSLen Brown  *  it under the terms of the GNU General Public License as published by
10c8f7a62cSLen Brown  *  the Free Software Foundation; either version 2 of the License, or (at
11c8f7a62cSLen Brown  *  your option) any later version.
12c8f7a62cSLen Brown  *
13c8f7a62cSLen Brown  *  This program is distributed in the hope that it will be useful, but
14c8f7a62cSLen Brown  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15c8f7a62cSLen Brown  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16c8f7a62cSLen Brown  *  General Public License for more details.
17c8f7a62cSLen Brown  *
18c8f7a62cSLen Brown  *  You should have received a copy of the GNU General Public License along
19c8f7a62cSLen Brown  *  with this program; if not, write to the Free Software Foundation, Inc.,
20c8f7a62cSLen Brown  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21c8f7a62cSLen Brown  *
22c8f7a62cSLen Brown  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23c8f7a62cSLen Brown  */
24c8f7a62cSLen Brown 
25c8f7a62cSLen Brown #include <linux/kernel.h>
26c8f7a62cSLen Brown #include <linux/module.h>
27c8f7a62cSLen Brown #include <linux/init.h>
28c8f7a62cSLen Brown #include <linux/types.h>
29c8f7a62cSLen Brown #include <linux/notifier.h>
30671adbecSKristen Carlson Accardi #include <linux/platform_device.h>
31914e2637SAl Viro #include <linux/jiffies.h>
32c8f7a62cSLen Brown #include <acpi/acpi_bus.h>
33c8f7a62cSLen Brown #include <acpi/acpi_drivers.h>
34c8f7a62cSLen Brown 
35c8f7a62cSLen Brown #define ACPI_DOCK_DRIVER_NAME "ACPI Dock Station Driver"
36c8f7a62cSLen Brown 
37c8f7a62cSLen Brown ACPI_MODULE_NAME("dock")
38c8f7a62cSLen Brown MODULE_AUTHOR("Kristen Carlson Accardi");
39c8f7a62cSLen Brown MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_NAME);
40c8f7a62cSLen Brown MODULE_LICENSE("GPL");
41c8f7a62cSLen Brown 
42c8f7a62cSLen Brown static struct atomic_notifier_head dock_notifier_list;
43671adbecSKristen Carlson Accardi static struct platform_device dock_device;
44671adbecSKristen Carlson Accardi static char dock_device_name[] = "dock";
45c8f7a62cSLen Brown 
46c8f7a62cSLen Brown struct dock_station {
47c8f7a62cSLen Brown 	acpi_handle handle;
48c8f7a62cSLen Brown 	unsigned long last_dock_time;
49c8f7a62cSLen Brown 	u32 flags;
50c8f7a62cSLen Brown 	spinlock_t dd_lock;
51c8f7a62cSLen Brown 	spinlock_t hp_lock;
52c8f7a62cSLen Brown 	struct list_head dependent_devices;
53c8f7a62cSLen Brown 	struct list_head hotplug_devices;
54c8f7a62cSLen Brown };
55c8f7a62cSLen Brown 
56c8f7a62cSLen Brown struct dock_dependent_device {
57c8f7a62cSLen Brown 	struct list_head list;
58c8f7a62cSLen Brown 	struct list_head hotplug_list;
59c8f7a62cSLen Brown 	acpi_handle handle;
60c8f7a62cSLen Brown 	acpi_notify_handler handler;
61c8f7a62cSLen Brown 	void *context;
62c8f7a62cSLen Brown };
63c8f7a62cSLen Brown 
64c8f7a62cSLen Brown #define DOCK_DOCKING	0x00000001
655669021eSKristen Carlson Accardi #define DOCK_EVENT	3
665669021eSKristen Carlson Accardi #define UNDOCK_EVENT	2
67c8f7a62cSLen Brown 
68c8f7a62cSLen Brown static struct dock_station *dock_station;
69c8f7a62cSLen Brown 
70c8f7a62cSLen Brown /*****************************************************************************
71c8f7a62cSLen Brown  *                         Dock Dependent device functions                   *
72c8f7a62cSLen Brown  *****************************************************************************/
73c8f7a62cSLen Brown /**
74c8f7a62cSLen Brown  *  alloc_dock_dependent_device - allocate and init a dependent device
75c8f7a62cSLen Brown  *  @handle: the acpi_handle of the dependent device
76c8f7a62cSLen Brown  *
77c8f7a62cSLen Brown  *  Allocate memory for a dependent device structure for a device referenced
78c8f7a62cSLen Brown  *  by the acpi handle
79c8f7a62cSLen Brown  */
80c8f7a62cSLen Brown static struct dock_dependent_device *
81c8f7a62cSLen Brown alloc_dock_dependent_device(acpi_handle handle)
82c8f7a62cSLen Brown {
83c8f7a62cSLen Brown 	struct dock_dependent_device *dd;
84c8f7a62cSLen Brown 
85c8f7a62cSLen Brown 	dd = kzalloc(sizeof(*dd), GFP_KERNEL);
86c8f7a62cSLen Brown 	if (dd) {
87c8f7a62cSLen Brown 		dd->handle = handle;
88c8f7a62cSLen Brown 		INIT_LIST_HEAD(&dd->list);
89c8f7a62cSLen Brown 		INIT_LIST_HEAD(&dd->hotplug_list);
90c8f7a62cSLen Brown 	}
91c8f7a62cSLen Brown 	return dd;
92c8f7a62cSLen Brown }
93c8f7a62cSLen Brown 
94c8f7a62cSLen Brown /**
95c8f7a62cSLen Brown  * add_dock_dependent_device - associate a device with the dock station
96c8f7a62cSLen Brown  * @ds: The dock station
97c8f7a62cSLen Brown  * @dd: The dependent device
98c8f7a62cSLen Brown  *
99c8f7a62cSLen Brown  * Add the dependent device to the dock's dependent device list.
100c8f7a62cSLen Brown  */
101c8f7a62cSLen Brown static void
102c8f7a62cSLen Brown add_dock_dependent_device(struct dock_station *ds,
103c8f7a62cSLen Brown 			  struct dock_dependent_device *dd)
104c8f7a62cSLen Brown {
105c8f7a62cSLen Brown 	spin_lock(&ds->dd_lock);
106c8f7a62cSLen Brown 	list_add_tail(&dd->list, &ds->dependent_devices);
107c8f7a62cSLen Brown 	spin_unlock(&ds->dd_lock);
108c8f7a62cSLen Brown }
109c8f7a62cSLen Brown 
110c8f7a62cSLen Brown /**
111c8f7a62cSLen Brown  * dock_add_hotplug_device - associate a hotplug handler with the dock station
112c8f7a62cSLen Brown  * @ds: The dock station
113c8f7a62cSLen Brown  * @dd: The dependent device struct
114c8f7a62cSLen Brown  *
115c8f7a62cSLen Brown  * Add the dependent device to the dock's hotplug device list
116c8f7a62cSLen Brown  */
117c8f7a62cSLen Brown static void
118c8f7a62cSLen Brown dock_add_hotplug_device(struct dock_station *ds,
119c8f7a62cSLen Brown 			struct dock_dependent_device *dd)
120c8f7a62cSLen Brown {
121c8f7a62cSLen Brown 	spin_lock(&ds->hp_lock);
122c8f7a62cSLen Brown 	list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
123c8f7a62cSLen Brown 	spin_unlock(&ds->hp_lock);
124c8f7a62cSLen Brown }
125c8f7a62cSLen Brown 
126c8f7a62cSLen Brown /**
127c8f7a62cSLen Brown  * dock_del_hotplug_device - remove a hotplug handler from the dock station
128c8f7a62cSLen Brown  * @ds: The dock station
129c8f7a62cSLen Brown  * @dd: the dependent device struct
130c8f7a62cSLen Brown  *
131c8f7a62cSLen Brown  * Delete the dependent device from the dock's hotplug device list
132c8f7a62cSLen Brown  */
133c8f7a62cSLen Brown static void
134c8f7a62cSLen Brown dock_del_hotplug_device(struct dock_station *ds,
135c8f7a62cSLen Brown 			struct dock_dependent_device *dd)
136c8f7a62cSLen Brown {
137c8f7a62cSLen Brown 	spin_lock(&ds->hp_lock);
138c8f7a62cSLen Brown 	list_del(&dd->hotplug_list);
139c8f7a62cSLen Brown 	spin_unlock(&ds->hp_lock);
140c8f7a62cSLen Brown }
141c8f7a62cSLen Brown 
142c8f7a62cSLen Brown /**
143c8f7a62cSLen Brown  * find_dock_dependent_device - get a device dependent on this dock
144c8f7a62cSLen Brown  * @ds: the dock station
145c8f7a62cSLen Brown  * @handle: the acpi_handle of the device we want
146c8f7a62cSLen Brown  *
147c8f7a62cSLen Brown  * iterate over the dependent device list for this dock.  If the
148c8f7a62cSLen Brown  * dependent device matches the handle, return.
149c8f7a62cSLen Brown  */
150c8f7a62cSLen Brown static struct dock_dependent_device *
151c8f7a62cSLen Brown find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
152c8f7a62cSLen Brown {
153c8f7a62cSLen Brown 	struct dock_dependent_device *dd;
154c8f7a62cSLen Brown 
155c8f7a62cSLen Brown 	spin_lock(&ds->dd_lock);
156c8f7a62cSLen Brown 	list_for_each_entry(dd, &ds->dependent_devices, list) {
157c8f7a62cSLen Brown 		if (handle == dd->handle) {
158c8f7a62cSLen Brown 			spin_unlock(&ds->dd_lock);
159c8f7a62cSLen Brown 			return dd;
160c8f7a62cSLen Brown 		}
161c8f7a62cSLen Brown 	}
162c8f7a62cSLen Brown 	spin_unlock(&ds->dd_lock);
163c8f7a62cSLen Brown 	return NULL;
164c8f7a62cSLen Brown }
165c8f7a62cSLen Brown 
166c8f7a62cSLen Brown /*****************************************************************************
167c8f7a62cSLen Brown  *                         Dock functions                                    *
168c8f7a62cSLen Brown  *****************************************************************************/
169c8f7a62cSLen Brown /**
170c8f7a62cSLen Brown  * is_dock - see if a device is a dock station
171c8f7a62cSLen Brown  * @handle: acpi handle of the device
172c8f7a62cSLen Brown  *
173c8f7a62cSLen Brown  * If an acpi object has a _DCK method, then it is by definition a dock
174c8f7a62cSLen Brown  * station, so return true.
175c8f7a62cSLen Brown  */
176c8f7a62cSLen Brown static int is_dock(acpi_handle handle)
177c8f7a62cSLen Brown {
178c8f7a62cSLen Brown 	acpi_status status;
179c8f7a62cSLen Brown 	acpi_handle tmp;
180c8f7a62cSLen Brown 
181c8f7a62cSLen Brown 	status = acpi_get_handle(handle, "_DCK", &tmp);
182c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
183c8f7a62cSLen Brown 		return 0;
184c8f7a62cSLen Brown 	return 1;
185c8f7a62cSLen Brown }
186c8f7a62cSLen Brown 
187c8f7a62cSLen Brown /**
188c8f7a62cSLen Brown  * is_dock_device - see if a device is on a dock station
189c8f7a62cSLen Brown  * @handle: acpi handle of the device
190c8f7a62cSLen Brown  *
191c8f7a62cSLen Brown  * If this device is either the dock station itself,
192c8f7a62cSLen Brown  * or is a device dependent on the dock station, then it
193c8f7a62cSLen Brown  * is a dock device
194c8f7a62cSLen Brown  */
195c8f7a62cSLen Brown int is_dock_device(acpi_handle handle)
196c8f7a62cSLen Brown {
197c8f7a62cSLen Brown 	if (!dock_station)
198c8f7a62cSLen Brown 		return 0;
199c8f7a62cSLen Brown 
200c8f7a62cSLen Brown 	if (is_dock(handle) || find_dock_dependent_device(dock_station, handle))
201c8f7a62cSLen Brown 		return 1;
202c8f7a62cSLen Brown 
203c8f7a62cSLen Brown 	return 0;
204c8f7a62cSLen Brown }
205c8f7a62cSLen Brown 
206c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(is_dock_device);
207c8f7a62cSLen Brown 
208c8f7a62cSLen Brown /**
209c8f7a62cSLen Brown  * dock_present - see if the dock station is present.
210c8f7a62cSLen Brown  * @ds: the dock station
211c8f7a62cSLen Brown  *
212c8f7a62cSLen Brown  * execute the _STA method.  note that present does not
213c8f7a62cSLen Brown  * imply that we are docked.
214c8f7a62cSLen Brown  */
215c8f7a62cSLen Brown static int dock_present(struct dock_station *ds)
216c8f7a62cSLen Brown {
217c8f7a62cSLen Brown 	unsigned long sta;
218c8f7a62cSLen Brown 	acpi_status status;
219c8f7a62cSLen Brown 
220c8f7a62cSLen Brown 	if (ds) {
221c8f7a62cSLen Brown 		status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
222c8f7a62cSLen Brown 		if (ACPI_SUCCESS(status) && sta)
223c8f7a62cSLen Brown 			return 1;
224c8f7a62cSLen Brown 	}
225c8f7a62cSLen Brown 	return 0;
226c8f7a62cSLen Brown }
227c8f7a62cSLen Brown 
228c8f7a62cSLen Brown 
229c8f7a62cSLen Brown 
230c8f7a62cSLen Brown /**
231c8f7a62cSLen Brown  * dock_create_acpi_device - add new devices to acpi
232c8f7a62cSLen Brown  * @handle - handle of the device to add
233c8f7a62cSLen Brown  *
234c8f7a62cSLen Brown  *  This function will create a new acpi_device for the given
235c8f7a62cSLen Brown  *  handle if one does not exist already.  This should cause
236c8f7a62cSLen Brown  *  acpi to scan for drivers for the given devices, and call
237c8f7a62cSLen Brown  *  matching driver's add routine.
238c8f7a62cSLen Brown  *
239c8f7a62cSLen Brown  *  Returns a pointer to the acpi_device corresponding to the handle.
240c8f7a62cSLen Brown  */
241c8f7a62cSLen Brown static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
242c8f7a62cSLen Brown {
243c8f7a62cSLen Brown 	struct acpi_device *device = NULL;
244c8f7a62cSLen Brown 	struct acpi_device *parent_device;
245c8f7a62cSLen Brown 	acpi_handle parent;
246c8f7a62cSLen Brown 	int ret;
247c8f7a62cSLen Brown 
248c8f7a62cSLen Brown 	if (acpi_bus_get_device(handle, &device)) {
249c8f7a62cSLen Brown 		/*
250c8f7a62cSLen Brown 		 * no device created for this object,
251c8f7a62cSLen Brown 		 * so we should create one.
252c8f7a62cSLen Brown 		 */
253c8f7a62cSLen Brown 		acpi_get_parent(handle, &parent);
254c8f7a62cSLen Brown 		if (acpi_bus_get_device(parent, &parent_device))
255c8f7a62cSLen Brown 			parent_device = NULL;
256c8f7a62cSLen Brown 
257c8f7a62cSLen Brown 		ret = acpi_bus_add(&device, parent_device, handle,
258c8f7a62cSLen Brown 			ACPI_BUS_TYPE_DEVICE);
259c8f7a62cSLen Brown 		if (ret) {
260c8f7a62cSLen Brown 			pr_debug("error adding bus, %x\n",
261c8f7a62cSLen Brown 				-ret);
262c8f7a62cSLen Brown 			return NULL;
263c8f7a62cSLen Brown 		}
264c8f7a62cSLen Brown 	}
265c8f7a62cSLen Brown 	return device;
266c8f7a62cSLen Brown }
267c8f7a62cSLen Brown 
268c8f7a62cSLen Brown /**
269c8f7a62cSLen Brown  * dock_remove_acpi_device - remove the acpi_device struct from acpi
270c8f7a62cSLen Brown  * @handle - the handle of the device to remove
271c8f7a62cSLen Brown  *
272c8f7a62cSLen Brown  *  Tell acpi to remove the acpi_device.  This should cause any loaded
273c8f7a62cSLen Brown  *  driver to have it's remove routine called.
274c8f7a62cSLen Brown  */
275c8f7a62cSLen Brown static void dock_remove_acpi_device(acpi_handle handle)
276c8f7a62cSLen Brown {
277c8f7a62cSLen Brown 	struct acpi_device *device;
278c8f7a62cSLen Brown 	int ret;
279c8f7a62cSLen Brown 
280c8f7a62cSLen Brown 	if (!acpi_bus_get_device(handle, &device)) {
281c8f7a62cSLen Brown 		ret = acpi_bus_trim(device, 1);
282c8f7a62cSLen Brown 		if (ret)
283c8f7a62cSLen Brown 			pr_debug("error removing bus, %x\n", -ret);
284c8f7a62cSLen Brown 	}
285c8f7a62cSLen Brown }
286c8f7a62cSLen Brown 
287c8f7a62cSLen Brown 
288c8f7a62cSLen Brown /**
289c8f7a62cSLen Brown  * hotplug_dock_devices - insert or remove devices on the dock station
290c8f7a62cSLen Brown  * @ds: the dock station
291c8f7a62cSLen Brown  * @event: either bus check or eject request
292c8f7a62cSLen Brown  *
293c8f7a62cSLen Brown  * Some devices on the dock station need to have drivers called
294c8f7a62cSLen Brown  * to perform hotplug operations after a dock event has occurred.
295c8f7a62cSLen Brown  * Traverse the list of dock devices that have registered a
296c8f7a62cSLen Brown  * hotplug handler, and call the handler.
297c8f7a62cSLen Brown  */
298c8f7a62cSLen Brown static void hotplug_dock_devices(struct dock_station *ds, u32 event)
299c8f7a62cSLen Brown {
300c8f7a62cSLen Brown 	struct dock_dependent_device *dd;
301c8f7a62cSLen Brown 
302c8f7a62cSLen Brown 	spin_lock(&ds->hp_lock);
303c8f7a62cSLen Brown 
304c8f7a62cSLen Brown 	/*
305c8f7a62cSLen Brown 	 * First call driver specific hotplug functions
306c8f7a62cSLen Brown 	 */
307c8f7a62cSLen Brown 	list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
308c8f7a62cSLen Brown 		if (dd->handler)
309c8f7a62cSLen Brown 			dd->handler(dd->handle, event, dd->context);
310c8f7a62cSLen Brown 	}
311c8f7a62cSLen Brown 
312c8f7a62cSLen Brown 	/*
313c8f7a62cSLen Brown 	 * Now make sure that an acpi_device is created for each
314c8f7a62cSLen Brown 	 * dependent device, or removed if this is an eject request.
315c8f7a62cSLen Brown 	 * This will cause acpi_drivers to be stopped/started if they
316c8f7a62cSLen Brown 	 * exist
317c8f7a62cSLen Brown 	 */
318c8f7a62cSLen Brown 	list_for_each_entry(dd, &ds->dependent_devices, list) {
319c8f7a62cSLen Brown 		if (event == ACPI_NOTIFY_EJECT_REQUEST)
320c8f7a62cSLen Brown 			dock_remove_acpi_device(dd->handle);
321c8f7a62cSLen Brown 		else
322c8f7a62cSLen Brown 			dock_create_acpi_device(dd->handle);
323c8f7a62cSLen Brown 	}
324c8f7a62cSLen Brown 	spin_unlock(&ds->hp_lock);
325c8f7a62cSLen Brown }
326c8f7a62cSLen Brown 
327c8f7a62cSLen Brown static void dock_event(struct dock_station *ds, u32 event, int num)
328c8f7a62cSLen Brown {
3295669021eSKristen Carlson Accardi 	/*
3305669021eSKristen Carlson Accardi 	 * we don't do events until someone tells me that
3315669021eSKristen Carlson Accardi 	 * they would like to have them.
3325669021eSKristen Carlson Accardi 	 */
333c8f7a62cSLen Brown }
334c8f7a62cSLen Brown 
335c8f7a62cSLen Brown /**
336c8f7a62cSLen Brown  * eject_dock - respond to a dock eject request
337c8f7a62cSLen Brown  * @ds: the dock station
338c8f7a62cSLen Brown  *
339c8f7a62cSLen Brown  * This is called after _DCK is called, to execute the dock station's
340c8f7a62cSLen Brown  * _EJ0 method.
341c8f7a62cSLen Brown  */
342c8f7a62cSLen Brown static void eject_dock(struct dock_station *ds)
343c8f7a62cSLen Brown {
344c8f7a62cSLen Brown 	struct acpi_object_list arg_list;
345c8f7a62cSLen Brown 	union acpi_object arg;
346c8f7a62cSLen Brown 	acpi_status status;
347c8f7a62cSLen Brown 	acpi_handle tmp;
348c8f7a62cSLen Brown 
349c8f7a62cSLen Brown 	/* all dock devices should have _EJ0, but check anyway */
350c8f7a62cSLen Brown 	status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
351c8f7a62cSLen Brown 	if (ACPI_FAILURE(status)) {
352c8f7a62cSLen Brown 		pr_debug("No _EJ0 support for dock device\n");
353c8f7a62cSLen Brown 		return;
354c8f7a62cSLen Brown 	}
355c8f7a62cSLen Brown 
356c8f7a62cSLen Brown 	arg_list.count = 1;
357c8f7a62cSLen Brown 	arg_list.pointer = &arg;
358c8f7a62cSLen Brown 	arg.type = ACPI_TYPE_INTEGER;
359c8f7a62cSLen Brown 	arg.integer.value = 1;
360c8f7a62cSLen Brown 
361c8f7a62cSLen Brown 	if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
362c8f7a62cSLen Brown 					      &arg_list, NULL)))
363c8f7a62cSLen Brown 		pr_debug("Failed to evaluate _EJ0!\n");
364c8f7a62cSLen Brown }
365c8f7a62cSLen Brown 
366c8f7a62cSLen Brown /**
367c8f7a62cSLen Brown  * handle_dock - handle a dock event
368c8f7a62cSLen Brown  * @ds: the dock station
369c8f7a62cSLen Brown  * @dock: to dock, or undock - that is the question
370c8f7a62cSLen Brown  *
371c8f7a62cSLen Brown  * Execute the _DCK method in response to an acpi event
372c8f7a62cSLen Brown  */
373c8f7a62cSLen Brown static void handle_dock(struct dock_station *ds, int dock)
374c8f7a62cSLen Brown {
375c8f7a62cSLen Brown 	acpi_status status;
376c8f7a62cSLen Brown 	struct acpi_object_list arg_list;
377c8f7a62cSLen Brown 	union acpi_object arg;
378c8f7a62cSLen Brown 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
379c8f7a62cSLen Brown 	struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
380c8f7a62cSLen Brown 	union acpi_object *obj;
381c8f7a62cSLen Brown 
382c8f7a62cSLen Brown 	acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
383c8f7a62cSLen Brown 	obj = name_buffer.pointer;
384c8f7a62cSLen Brown 
385c8f7a62cSLen Brown 	printk(KERN_INFO PREFIX "%s\n", dock ? "docking" : "undocking");
386c8f7a62cSLen Brown 
387c8f7a62cSLen Brown 	/* _DCK method has one argument */
388c8f7a62cSLen Brown 	arg_list.count = 1;
389c8f7a62cSLen Brown 	arg_list.pointer = &arg;
390c8f7a62cSLen Brown 	arg.type = ACPI_TYPE_INTEGER;
391c8f7a62cSLen Brown 	arg.integer.value = dock;
392c8f7a62cSLen Brown 	status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
393c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
394c8f7a62cSLen Brown 		pr_debug("%s: failed to execute _DCK\n", obj->string.pointer);
395c8f7a62cSLen Brown 	kfree(buffer.pointer);
396c8f7a62cSLen Brown 	kfree(name_buffer.pointer);
397c8f7a62cSLen Brown }
398c8f7a62cSLen Brown 
399c8f7a62cSLen Brown static inline void dock(struct dock_station *ds)
400c8f7a62cSLen Brown {
401c8f7a62cSLen Brown 	handle_dock(ds, 1);
402c8f7a62cSLen Brown }
403c8f7a62cSLen Brown 
404c8f7a62cSLen Brown static inline void undock(struct dock_station *ds)
405c8f7a62cSLen Brown {
406c8f7a62cSLen Brown 	handle_dock(ds, 0);
407c8f7a62cSLen Brown }
408c8f7a62cSLen Brown 
409c8f7a62cSLen Brown static inline void begin_dock(struct dock_station *ds)
410c8f7a62cSLen Brown {
411c8f7a62cSLen Brown 	ds->flags |= DOCK_DOCKING;
412c8f7a62cSLen Brown }
413c8f7a62cSLen Brown 
414c8f7a62cSLen Brown static inline void complete_dock(struct dock_station *ds)
415c8f7a62cSLen Brown {
416c8f7a62cSLen Brown 	ds->flags &= ~(DOCK_DOCKING);
417c8f7a62cSLen Brown 	ds->last_dock_time = jiffies;
418c8f7a62cSLen Brown }
419c8f7a62cSLen Brown 
420c8f7a62cSLen Brown /**
421c8f7a62cSLen Brown  * dock_in_progress - see if we are in the middle of handling a dock event
422c8f7a62cSLen Brown  * @ds: the dock station
423c8f7a62cSLen Brown  *
424c8f7a62cSLen Brown  * Sometimes while docking, false dock events can be sent to the driver
425c8f7a62cSLen Brown  * because good connections aren't made or some other reason.  Ignore these
426c8f7a62cSLen Brown  * if we are in the middle of doing something.
427c8f7a62cSLen Brown  */
428c8f7a62cSLen Brown static int dock_in_progress(struct dock_station *ds)
429c8f7a62cSLen Brown {
430c8f7a62cSLen Brown 	if ((ds->flags & DOCK_DOCKING) ||
431c8f7a62cSLen Brown 	    time_before(jiffies, (ds->last_dock_time + HZ)))
432c8f7a62cSLen Brown 		return 1;
433c8f7a62cSLen Brown 	return 0;
434c8f7a62cSLen Brown }
435c8f7a62cSLen Brown 
436c8f7a62cSLen Brown /**
437c8f7a62cSLen Brown  * register_dock_notifier - add yourself to the dock notifier list
438c8f7a62cSLen Brown  * @nb: the callers notifier block
439c8f7a62cSLen Brown  *
440c8f7a62cSLen Brown  * If a driver wishes to be notified about dock events, they can
441c8f7a62cSLen Brown  * use this function to put a notifier block on the dock notifier list.
442c8f7a62cSLen Brown  * this notifier call chain will be called after a dock event, but
443c8f7a62cSLen Brown  * before hotplugging any new devices.
444c8f7a62cSLen Brown  */
445c8f7a62cSLen Brown int register_dock_notifier(struct notifier_block *nb)
446c8f7a62cSLen Brown {
447c8f7a62cSLen Brown 	return atomic_notifier_chain_register(&dock_notifier_list, nb);
448c8f7a62cSLen Brown }
449c8f7a62cSLen Brown 
450c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(register_dock_notifier);
451c8f7a62cSLen Brown 
452c8f7a62cSLen Brown /**
453c8f7a62cSLen Brown  * unregister_dock_notifier - remove yourself from the dock notifier list
454c8f7a62cSLen Brown  * @nb: the callers notifier block
455c8f7a62cSLen Brown  */
456c8f7a62cSLen Brown void unregister_dock_notifier(struct notifier_block *nb)
457c8f7a62cSLen Brown {
458c8f7a62cSLen Brown 	atomic_notifier_chain_unregister(&dock_notifier_list, nb);
459c8f7a62cSLen Brown }
460c8f7a62cSLen Brown 
461c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(unregister_dock_notifier);
462c8f7a62cSLen Brown 
463c8f7a62cSLen Brown /**
464c8f7a62cSLen Brown  * register_hotplug_dock_device - register a hotplug function
465c8f7a62cSLen Brown  * @handle: the handle of the device
466c8f7a62cSLen Brown  * @handler: the acpi_notifier_handler to call after docking
467c8f7a62cSLen Brown  * @context: device specific data
468c8f7a62cSLen Brown  *
469c8f7a62cSLen Brown  * If a driver would like to perform a hotplug operation after a dock
470c8f7a62cSLen Brown  * event, they can register an acpi_notifiy_handler to be called by
471c8f7a62cSLen Brown  * the dock driver after _DCK is executed.
472c8f7a62cSLen Brown  */
473c8f7a62cSLen Brown int
474c8f7a62cSLen Brown register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
475c8f7a62cSLen Brown 			     void *context)
476c8f7a62cSLen Brown {
477c8f7a62cSLen Brown 	struct dock_dependent_device *dd;
478c8f7a62cSLen Brown 
479c8f7a62cSLen Brown 	if (!dock_station)
480c8f7a62cSLen Brown 		return -ENODEV;
481c8f7a62cSLen Brown 
482c8f7a62cSLen Brown 	/*
483c8f7a62cSLen Brown 	 * make sure this handle is for a device dependent on the dock,
484c8f7a62cSLen Brown 	 * this would include the dock station itself
485c8f7a62cSLen Brown 	 */
486c8f7a62cSLen Brown 	dd = find_dock_dependent_device(dock_station, handle);
487c8f7a62cSLen Brown 	if (dd) {
488c8f7a62cSLen Brown 		dd->handler = handler;
489c8f7a62cSLen Brown 		dd->context = context;
490c8f7a62cSLen Brown 		dock_add_hotplug_device(dock_station, dd);
491c8f7a62cSLen Brown 		return 0;
492c8f7a62cSLen Brown 	}
493c8f7a62cSLen Brown 
494c8f7a62cSLen Brown 	return -EINVAL;
495c8f7a62cSLen Brown }
496c8f7a62cSLen Brown 
497c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
498c8f7a62cSLen Brown 
499c8f7a62cSLen Brown /**
500c8f7a62cSLen Brown  * unregister_hotplug_dock_device - remove yourself from the hotplug list
501c8f7a62cSLen Brown  * @handle: the acpi handle of the device
502c8f7a62cSLen Brown  */
503c8f7a62cSLen Brown void unregister_hotplug_dock_device(acpi_handle handle)
504c8f7a62cSLen Brown {
505c8f7a62cSLen Brown 	struct dock_dependent_device *dd;
506c8f7a62cSLen Brown 
507c8f7a62cSLen Brown 	if (!dock_station)
508c8f7a62cSLen Brown 		return;
509c8f7a62cSLen Brown 
510c8f7a62cSLen Brown 	dd = find_dock_dependent_device(dock_station, handle);
511c8f7a62cSLen Brown 	if (dd)
512c8f7a62cSLen Brown 		dock_del_hotplug_device(dock_station, dd);
513c8f7a62cSLen Brown }
514c8f7a62cSLen Brown 
515c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
516c8f7a62cSLen Brown 
517c8f7a62cSLen Brown /**
518c8f7a62cSLen Brown  * dock_notify - act upon an acpi dock notification
519c8f7a62cSLen Brown  * @handle: the dock station handle
520c8f7a62cSLen Brown  * @event: the acpi event
521c8f7a62cSLen Brown  * @data: our driver data struct
522c8f7a62cSLen Brown  *
523c8f7a62cSLen Brown  * If we are notified to dock, then check to see if the dock is
524c8f7a62cSLen Brown  * present and then dock.  Notify all drivers of the dock event,
525c8f7a62cSLen Brown  * and then hotplug and devices that may need hotplugging.  For undock
526c8f7a62cSLen Brown  * check to make sure the dock device is still present, then undock
527c8f7a62cSLen Brown  * and hotremove all the devices that may need removing.
528c8f7a62cSLen Brown  */
529c8f7a62cSLen Brown static void dock_notify(acpi_handle handle, u32 event, void *data)
530c8f7a62cSLen Brown {
531c8f7a62cSLen Brown 	struct dock_station *ds = (struct dock_station *)data;
532c8f7a62cSLen Brown 
533c8f7a62cSLen Brown 	switch (event) {
534c8f7a62cSLen Brown 	case ACPI_NOTIFY_BUS_CHECK:
535c8f7a62cSLen Brown 		if (!dock_in_progress(ds) && dock_present(ds)) {
536c8f7a62cSLen Brown 			begin_dock(ds);
537c8f7a62cSLen Brown 			dock(ds);
538c8f7a62cSLen Brown 			if (!dock_present(ds)) {
539c8f7a62cSLen Brown 				printk(KERN_ERR PREFIX "Unable to dock!\n");
540c8f7a62cSLen Brown 				break;
541c8f7a62cSLen Brown 			}
542c8f7a62cSLen Brown 			atomic_notifier_call_chain(&dock_notifier_list,
543c8f7a62cSLen Brown 						   event, NULL);
544c8f7a62cSLen Brown 			hotplug_dock_devices(ds, event);
545c8f7a62cSLen Brown 			complete_dock(ds);
546c8f7a62cSLen Brown 			dock_event(ds, event, DOCK_EVENT);
547c8f7a62cSLen Brown 		}
548c8f7a62cSLen Brown 		break;
549c8f7a62cSLen Brown 	case ACPI_NOTIFY_DEVICE_CHECK:
550c8f7a62cSLen Brown 	/*
551c8f7a62cSLen Brown          * According to acpi spec 3.0a, if a DEVICE_CHECK notification
552c8f7a62cSLen Brown          * is sent and _DCK is present, it is assumed to mean an
553c8f7a62cSLen Brown          * undock request.  This notify routine will only be called
554c8f7a62cSLen Brown          * for objects defining _DCK, so we will fall through to eject
555c8f7a62cSLen Brown          * request here.  However, we will pass an eject request through
556c8f7a62cSLen Brown 	 * to the driver who wish to hotplug.
557c8f7a62cSLen Brown          */
558c8f7a62cSLen Brown 	case ACPI_NOTIFY_EJECT_REQUEST:
559c8f7a62cSLen Brown 		if (!dock_in_progress(ds) && dock_present(ds)) {
560c8f7a62cSLen Brown 			/*
561c8f7a62cSLen Brown 			 * here we need to generate the undock
562c8f7a62cSLen Brown 			 * event prior to actually doing the undock
563c8f7a62cSLen Brown 			 * so that the device struct still exists.
564c8f7a62cSLen Brown 			 */
565c8f7a62cSLen Brown 			dock_event(ds, event, UNDOCK_EVENT);
566c8f7a62cSLen Brown 			hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
567c8f7a62cSLen Brown 			undock(ds);
568c8f7a62cSLen Brown 			eject_dock(ds);
569c8f7a62cSLen Brown 			if (dock_present(ds))
570c8f7a62cSLen Brown 				printk(KERN_ERR PREFIX "Unable to undock!\n");
571c8f7a62cSLen Brown 		}
572c8f7a62cSLen Brown 		break;
573c8f7a62cSLen Brown 	default:
574c8f7a62cSLen Brown 		printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
575c8f7a62cSLen Brown 	}
576c8f7a62cSLen Brown }
577c8f7a62cSLen Brown 
578c8f7a62cSLen Brown /**
579c8f7a62cSLen Brown  * find_dock_devices - find devices on the dock station
580c8f7a62cSLen Brown  * @handle: the handle of the device we are examining
581c8f7a62cSLen Brown  * @lvl: unused
582c8f7a62cSLen Brown  * @context: the dock station private data
583c8f7a62cSLen Brown  * @rv: unused
584c8f7a62cSLen Brown  *
585c8f7a62cSLen Brown  * This function is called by acpi_walk_namespace.  It will
586c8f7a62cSLen Brown  * check to see if an object has an _EJD method.  If it does, then it
587c8f7a62cSLen Brown  * will see if it is dependent on the dock station.
588c8f7a62cSLen Brown  */
589c8f7a62cSLen Brown static acpi_status
590c8f7a62cSLen Brown find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
591c8f7a62cSLen Brown {
592c8f7a62cSLen Brown 	acpi_status status;
593c8f7a62cSLen Brown 	acpi_handle tmp;
594c8f7a62cSLen Brown 	struct dock_station *ds = (struct dock_station *)context;
595c8f7a62cSLen Brown 	struct dock_dependent_device *dd;
596c8f7a62cSLen Brown 
597c8f7a62cSLen Brown 	status = acpi_bus_get_ejd(handle, &tmp);
598c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
599c8f7a62cSLen Brown 		return AE_OK;
600c8f7a62cSLen Brown 
601c8f7a62cSLen Brown 	if (tmp == ds->handle) {
602c8f7a62cSLen Brown 		dd = alloc_dock_dependent_device(handle);
603c8f7a62cSLen Brown 		if (dd)
604c8f7a62cSLen Brown 			add_dock_dependent_device(ds, dd);
605c8f7a62cSLen Brown 	}
606c8f7a62cSLen Brown 
607c8f7a62cSLen Brown 	return AE_OK;
608c8f7a62cSLen Brown }
609c8f7a62cSLen Brown 
610c8f7a62cSLen Brown /**
611c8f7a62cSLen Brown  * dock_add - add a new dock station
612c8f7a62cSLen Brown  * @handle: the dock station handle
613c8f7a62cSLen Brown  *
614c8f7a62cSLen Brown  * allocated and initialize a new dock station device.  Find all devices
615c8f7a62cSLen Brown  * that are on the dock station, and register for dock event notifications.
616c8f7a62cSLen Brown  */
617c8f7a62cSLen Brown static int dock_add(acpi_handle handle)
618c8f7a62cSLen Brown {
619c8f7a62cSLen Brown 	int ret;
620c8f7a62cSLen Brown 	acpi_status status;
621c8f7a62cSLen Brown 	struct dock_dependent_device *dd;
622c8f7a62cSLen Brown 
623c8f7a62cSLen Brown 	/* allocate & initialize the dock_station private data */
624c8f7a62cSLen Brown 	dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
625c8f7a62cSLen Brown 	if (!dock_station)
626c8f7a62cSLen Brown 		return -ENOMEM;
627c8f7a62cSLen Brown 	dock_station->handle = handle;
628c8f7a62cSLen Brown 	dock_station->last_dock_time = jiffies - HZ;
629c8f7a62cSLen Brown 	INIT_LIST_HEAD(&dock_station->dependent_devices);
630c8f7a62cSLen Brown 	INIT_LIST_HEAD(&dock_station->hotplug_devices);
631c8f7a62cSLen Brown 	spin_lock_init(&dock_station->dd_lock);
632c8f7a62cSLen Brown 	spin_lock_init(&dock_station->hp_lock);
63307a18684SKristen Accardi 	ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
634c8f7a62cSLen Brown 
635671adbecSKristen Carlson Accardi 	/* initialize platform device stuff */
636671adbecSKristen Carlson Accardi 	dock_device.name = dock_device_name;
637671adbecSKristen Carlson Accardi 	ret = platform_device_register(&dock_device);
638671adbecSKristen Carlson Accardi 	if (ret) {
639e67beb37SLen Brown 		printk(KERN_ERR PREFIX "Error %d registering dock device\n", ret);
640671adbecSKristen Carlson Accardi 		kfree(dock_station);
641671adbecSKristen Carlson Accardi 		return ret;
642671adbecSKristen Carlson Accardi 	}
643671adbecSKristen Carlson Accardi 
644c8f7a62cSLen Brown 	/* Find dependent devices */
645c8f7a62cSLen Brown 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
646c8f7a62cSLen Brown 			    ACPI_UINT32_MAX, find_dock_devices, dock_station,
647c8f7a62cSLen Brown 			    NULL);
648c8f7a62cSLen Brown 
649c8f7a62cSLen Brown 	/* add the dock station as a device dependent on itself */
650c8f7a62cSLen Brown 	dd = alloc_dock_dependent_device(handle);
651c8f7a62cSLen Brown 	if (!dd) {
652c8f7a62cSLen Brown 		kfree(dock_station);
653671adbecSKristen Carlson Accardi 		ret = -ENOMEM;
654671adbecSKristen Carlson Accardi 		goto dock_add_err_unregister;
655c8f7a62cSLen Brown 	}
656c8f7a62cSLen Brown 	add_dock_dependent_device(dock_station, dd);
657c8f7a62cSLen Brown 
658c8f7a62cSLen Brown 	/* register for dock events */
659c8f7a62cSLen Brown 	status = acpi_install_notify_handler(dock_station->handle,
660c8f7a62cSLen Brown 					     ACPI_SYSTEM_NOTIFY,
661c8f7a62cSLen Brown 					     dock_notify, dock_station);
662c8f7a62cSLen Brown 
663c8f7a62cSLen Brown 	if (ACPI_FAILURE(status)) {
664c8f7a62cSLen Brown 		printk(KERN_ERR PREFIX "Error installing notify handler\n");
665c8f7a62cSLen Brown 		ret = -ENODEV;
666c8f7a62cSLen Brown 		goto dock_add_err;
667c8f7a62cSLen Brown 	}
668c8f7a62cSLen Brown 
669c8f7a62cSLen Brown 	printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_NAME);
670c8f7a62cSLen Brown 
671c8f7a62cSLen Brown 	return 0;
672c8f7a62cSLen Brown 
673c8f7a62cSLen Brown dock_add_err:
674c8f7a62cSLen Brown 	kfree(dd);
675671adbecSKristen Carlson Accardi dock_add_err_unregister:
676671adbecSKristen Carlson Accardi 	platform_device_unregister(&dock_device);
677671adbecSKristen Carlson Accardi 	kfree(dock_station);
678c8f7a62cSLen Brown 	return ret;
679c8f7a62cSLen Brown }
680c8f7a62cSLen Brown 
681c8f7a62cSLen Brown /**
682c8f7a62cSLen Brown  * dock_remove - free up resources related to the dock station
683c8f7a62cSLen Brown  */
684c8f7a62cSLen Brown static int dock_remove(void)
685c8f7a62cSLen Brown {
686c8f7a62cSLen Brown 	struct dock_dependent_device *dd, *tmp;
687c8f7a62cSLen Brown 	acpi_status status;
688c8f7a62cSLen Brown 
689c8f7a62cSLen Brown 	if (!dock_station)
690c8f7a62cSLen Brown 		return 0;
691c8f7a62cSLen Brown 
692c8f7a62cSLen Brown 	/* remove dependent devices */
693c8f7a62cSLen Brown 	list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
694c8f7a62cSLen Brown 				 list)
695c8f7a62cSLen Brown 	    kfree(dd);
696c8f7a62cSLen Brown 
697c8f7a62cSLen Brown 	/* remove dock notify handler */
698c8f7a62cSLen Brown 	status = acpi_remove_notify_handler(dock_station->handle,
699c8f7a62cSLen Brown 					    ACPI_SYSTEM_NOTIFY,
700c8f7a62cSLen Brown 					    dock_notify);
701c8f7a62cSLen Brown 	if (ACPI_FAILURE(status))
702c8f7a62cSLen Brown 		printk(KERN_ERR "Error removing notify handler\n");
703c8f7a62cSLen Brown 
704671adbecSKristen Carlson Accardi 	/* cleanup sysfs */
705671adbecSKristen Carlson Accardi 	platform_device_unregister(&dock_device);
706671adbecSKristen Carlson Accardi 
707c8f7a62cSLen Brown 	/* free dock station memory */
708c8f7a62cSLen Brown 	kfree(dock_station);
709c8f7a62cSLen Brown 	return 0;
710c8f7a62cSLen Brown }
711c8f7a62cSLen Brown 
712c8f7a62cSLen Brown /**
713c8f7a62cSLen Brown  * find_dock - look for a dock station
714c8f7a62cSLen Brown  * @handle: acpi handle of a device
715c8f7a62cSLen Brown  * @lvl: unused
716c8f7a62cSLen Brown  * @context: counter of dock stations found
717c8f7a62cSLen Brown  * @rv: unused
718c8f7a62cSLen Brown  *
719c8f7a62cSLen Brown  * This is called by acpi_walk_namespace to look for dock stations.
720c8f7a62cSLen Brown  */
721c8f7a62cSLen Brown static acpi_status
722c8f7a62cSLen Brown find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
723c8f7a62cSLen Brown {
724c8f7a62cSLen Brown 	int *count = (int *)context;
725c8f7a62cSLen Brown 	acpi_status status = AE_OK;
726c8f7a62cSLen Brown 
727c8f7a62cSLen Brown 	if (is_dock(handle)) {
728c8f7a62cSLen Brown 		if (dock_add(handle) >= 0) {
729c8f7a62cSLen Brown 			(*count)++;
730c8f7a62cSLen Brown 			status = AE_CTRL_TERMINATE;
731c8f7a62cSLen Brown 		}
732c8f7a62cSLen Brown 	}
733c8f7a62cSLen Brown 	return status;
734c8f7a62cSLen Brown }
735c8f7a62cSLen Brown 
736c8f7a62cSLen Brown static int __init dock_init(void)
737c8f7a62cSLen Brown {
738c8f7a62cSLen Brown 	int num = 0;
739c8f7a62cSLen Brown 
740c8f7a62cSLen Brown 	dock_station = NULL;
741c8f7a62cSLen Brown 
742c8f7a62cSLen Brown 	/* look for a dock station */
743c8f7a62cSLen Brown 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
744c8f7a62cSLen Brown 			    ACPI_UINT32_MAX, find_dock, &num, NULL);
745c8f7a62cSLen Brown 
746c8f7a62cSLen Brown 	if (!num)
747c8f7a62cSLen Brown 		return -ENODEV;
748c8f7a62cSLen Brown 
749c8f7a62cSLen Brown 	return 0;
750c8f7a62cSLen Brown }
751c8f7a62cSLen Brown 
752c8f7a62cSLen Brown static void __exit dock_exit(void)
753c8f7a62cSLen Brown {
754c8f7a62cSLen Brown 	dock_remove();
755c8f7a62cSLen Brown }
756c8f7a62cSLen Brown 
757c8f7a62cSLen Brown postcore_initcall(dock_init);
758c8f7a62cSLen Brown module_exit(dock_exit);
759