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