ACPI: dock: fix build warning
[linux-2.6-block.git] / drivers / acpi / dock.c
CommitLineData
c8f7a62c
LB
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>
671adbec 30#include <linux/platform_device.h>
914e2637 31#include <linux/jiffies.h>
c8f7a62c
LB
32#include <acpi/acpi_bus.h>
33#include <acpi/acpi_drivers.h>
34
35#define ACPI_DOCK_DRIVER_NAME "ACPI Dock Station Driver"
36
37ACPI_MODULE_NAME("dock")
38MODULE_AUTHOR("Kristen Carlson Accardi");
39MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_NAME);
40MODULE_LICENSE("GPL");
41
42static struct atomic_notifier_head dock_notifier_list;
671adbec
KCA
43static struct platform_device dock_device;
44static char dock_device_name[] = "dock";
c8f7a62c
LB
45
46struct dock_station {
47 acpi_handle handle;
48 unsigned long last_dock_time;
49 u32 flags;
50 spinlock_t dd_lock;
51 spinlock_t hp_lock;
52 struct list_head dependent_devices;
53 struct list_head hotplug_devices;
54};
55
56struct 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
5669021e
KCA
65#define DOCK_EVENT 3
66#define UNDOCK_EVENT 2
c8f7a62c
LB
67
68static 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 */
80static struct dock_dependent_device *
81alloc_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 */
101static void
102add_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 */
117static void
118dock_add_hotplug_device(struct dock_station *ds,
119 struct dock_dependent_device *dd)
120{
121 spin_lock(&ds->hp_lock);
122 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
123 spin_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 */
133static void
134dock_del_hotplug_device(struct dock_station *ds,
135 struct dock_dependent_device *dd)
136{
137 spin_lock(&ds->hp_lock);
138 list_del(&dd->hotplug_list);
139 spin_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 */
150static struct dock_dependent_device *
151find_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 */
176static 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 */
195int 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
206EXPORT_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 */
215static 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 */
241static 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 */
275static 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 */
298static void hotplug_dock_devices(struct dock_station *ds, u32 event)
299{
300 struct dock_dependent_device *dd;
301
302 spin_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 spin_unlock(&ds->hp_lock);
325}
326
327static void dock_event(struct dock_station *ds, u32 event, int num)
328{
5669021e
KCA
329 /*
330 * we don't do events until someone tells me that
331 * they would like to have them.
332 */
c8f7a62c
LB
333}
334
335/**
336 * eject_dock - respond to a dock eject request
337 * @ds: the dock station
338 *
339 * This is called after _DCK is called, to execute the dock station's
340 * _EJ0 method.
341 */
342static void eject_dock(struct dock_station *ds)
343{
344 struct acpi_object_list arg_list;
345 union acpi_object arg;
346 acpi_status status;
347 acpi_handle tmp;
348
349 /* all dock devices should have _EJ0, but check anyway */
350 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
351 if (ACPI_FAILURE(status)) {
352 pr_debug("No _EJ0 support for dock device\n");
353 return;
354 }
355
356 arg_list.count = 1;
357 arg_list.pointer = &arg;
358 arg.type = ACPI_TYPE_INTEGER;
359 arg.integer.value = 1;
360
361 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
362 &arg_list, NULL)))
363 pr_debug("Failed to evaluate _EJ0!\n");
364}
365
366/**
367 * handle_dock - handle a dock event
368 * @ds: the dock station
369 * @dock: to dock, or undock - that is the question
370 *
371 * Execute the _DCK method in response to an acpi event
372 */
373static void handle_dock(struct dock_station *ds, int dock)
374{
375 acpi_status status;
376 struct acpi_object_list arg_list;
377 union acpi_object arg;
378 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
379 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
380 union acpi_object *obj;
381
382 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
383 obj = name_buffer.pointer;
384
385 printk(KERN_INFO PREFIX "%s\n", dock ? "docking" : "undocking");
386
387 /* _DCK method has one argument */
388 arg_list.count = 1;
389 arg_list.pointer = &arg;
390 arg.type = ACPI_TYPE_INTEGER;
391 arg.integer.value = dock;
392 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
393 if (ACPI_FAILURE(status))
394 pr_debug("%s: failed to execute _DCK\n", obj->string.pointer);
395 kfree(buffer.pointer);
396 kfree(name_buffer.pointer);
397}
398
399static inline void dock(struct dock_station *ds)
400{
401 handle_dock(ds, 1);
402}
403
404static inline void undock(struct dock_station *ds)
405{
406 handle_dock(ds, 0);
407}
408
409static inline void begin_dock(struct dock_station *ds)
410{
411 ds->flags |= DOCK_DOCKING;
412}
413
414static inline void complete_dock(struct dock_station *ds)
415{
416 ds->flags &= ~(DOCK_DOCKING);
417 ds->last_dock_time = jiffies;
418}
419
420/**
421 * dock_in_progress - see if we are in the middle of handling a dock event
422 * @ds: the dock station
423 *
424 * Sometimes while docking, false dock events can be sent to the driver
425 * because good connections aren't made or some other reason. Ignore these
426 * if we are in the middle of doing something.
427 */
428static int dock_in_progress(struct dock_station *ds)
429{
430 if ((ds->flags & DOCK_DOCKING) ||
431 time_before(jiffies, (ds->last_dock_time + HZ)))
432 return 1;
433 return 0;
434}
435
436/**
437 * register_dock_notifier - add yourself to the dock notifier list
438 * @nb: the callers notifier block
439 *
440 * If a driver wishes to be notified about dock events, they can
441 * use this function to put a notifier block on the dock notifier list.
442 * this notifier call chain will be called after a dock event, but
443 * before hotplugging any new devices.
444 */
445int register_dock_notifier(struct notifier_block *nb)
446{
447 return atomic_notifier_chain_register(&dock_notifier_list, nb);
448}
449
450EXPORT_SYMBOL_GPL(register_dock_notifier);
451
452/**
453 * unregister_dock_notifier - remove yourself from the dock notifier list
454 * @nb: the callers notifier block
455 */
456void unregister_dock_notifier(struct notifier_block *nb)
457{
458 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
459}
460
461EXPORT_SYMBOL_GPL(unregister_dock_notifier);
462
463/**
464 * register_hotplug_dock_device - register a hotplug function
465 * @handle: the handle of the device
466 * @handler: the acpi_notifier_handler to call after docking
467 * @context: device specific data
468 *
469 * If a driver would like to perform a hotplug operation after a dock
470 * event, they can register an acpi_notifiy_handler to be called by
471 * the dock driver after _DCK is executed.
472 */
473int
474register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
475 void *context)
476{
477 struct dock_dependent_device *dd;
478
479 if (!dock_station)
480 return -ENODEV;
481
482 /*
483 * make sure this handle is for a device dependent on the dock,
484 * this would include the dock station itself
485 */
486 dd = find_dock_dependent_device(dock_station, handle);
487 if (dd) {
488 dd->handler = handler;
489 dd->context = context;
490 dock_add_hotplug_device(dock_station, dd);
491 return 0;
492 }
493
494 return -EINVAL;
495}
496
497EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
498
499/**
500 * unregister_hotplug_dock_device - remove yourself from the hotplug list
501 * @handle: the acpi handle of the device
502 */
503void unregister_hotplug_dock_device(acpi_handle handle)
504{
505 struct dock_dependent_device *dd;
506
507 if (!dock_station)
508 return;
509
510 dd = find_dock_dependent_device(dock_station, handle);
511 if (dd)
512 dock_del_hotplug_device(dock_station, dd);
513}
514
515EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
516
517/**
518 * dock_notify - act upon an acpi dock notification
519 * @handle: the dock station handle
520 * @event: the acpi event
521 * @data: our driver data struct
522 *
523 * If we are notified to dock, then check to see if the dock is
524 * present and then dock. Notify all drivers of the dock event,
525 * and then hotplug and devices that may need hotplugging. For undock
526 * check to make sure the dock device is still present, then undock
527 * and hotremove all the devices that may need removing.
528 */
529static void dock_notify(acpi_handle handle, u32 event, void *data)
530{
531 struct dock_station *ds = (struct dock_station *)data;
532
533 switch (event) {
534 case ACPI_NOTIFY_BUS_CHECK:
535 if (!dock_in_progress(ds) && dock_present(ds)) {
536 begin_dock(ds);
537 dock(ds);
538 if (!dock_present(ds)) {
539 printk(KERN_ERR PREFIX "Unable to dock!\n");
540 break;
541 }
542 atomic_notifier_call_chain(&dock_notifier_list,
543 event, NULL);
544 hotplug_dock_devices(ds, event);
545 complete_dock(ds);
546 dock_event(ds, event, DOCK_EVENT);
547 }
548 break;
549 case ACPI_NOTIFY_DEVICE_CHECK:
550 /*
551 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
552 * is sent and _DCK is present, it is assumed to mean an
553 * undock request. This notify routine will only be called
554 * for objects defining _DCK, so we will fall through to eject
555 * request here. However, we will pass an eject request through
556 * to the driver who wish to hotplug.
557 */
558 case ACPI_NOTIFY_EJECT_REQUEST:
559 if (!dock_in_progress(ds) && dock_present(ds)) {
560 /*
561 * here we need to generate the undock
562 * event prior to actually doing the undock
563 * so that the device struct still exists.
564 */
565 dock_event(ds, event, UNDOCK_EVENT);
566 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
567 undock(ds);
568 eject_dock(ds);
569 if (dock_present(ds))
570 printk(KERN_ERR PREFIX "Unable to undock!\n");
571 }
572 break;
573 default:
574 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
575 }
576}
577
578/**
579 * find_dock_devices - find devices on the dock station
580 * @handle: the handle of the device we are examining
581 * @lvl: unused
582 * @context: the dock station private data
583 * @rv: unused
584 *
585 * This function is called by acpi_walk_namespace. It will
586 * check to see if an object has an _EJD method. If it does, then it
587 * will see if it is dependent on the dock station.
588 */
589static acpi_status
590find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
591{
592 acpi_status status;
593 acpi_handle tmp;
594 struct dock_station *ds = (struct dock_station *)context;
595 struct dock_dependent_device *dd;
596
597 status = acpi_bus_get_ejd(handle, &tmp);
598 if (ACPI_FAILURE(status))
599 return AE_OK;
600
601 if (tmp == ds->handle) {
602 dd = alloc_dock_dependent_device(handle);
603 if (dd)
604 add_dock_dependent_device(ds, dd);
605 }
606
607 return AE_OK;
608}
609
610/**
611 * dock_add - add a new dock station
612 * @handle: the dock station handle
613 *
614 * allocated and initialize a new dock station device. Find all devices
615 * that are on the dock station, and register for dock event notifications.
616 */
617static int dock_add(acpi_handle handle)
618{
619 int ret;
620 acpi_status status;
621 struct dock_dependent_device *dd;
622
623 /* allocate & initialize the dock_station private data */
624 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
625 if (!dock_station)
626 return -ENOMEM;
627 dock_station->handle = handle;
628 dock_station->last_dock_time = jiffies - HZ;
629 INIT_LIST_HEAD(&dock_station->dependent_devices);
630 INIT_LIST_HEAD(&dock_station->hotplug_devices);
631 spin_lock_init(&dock_station->dd_lock);
632 spin_lock_init(&dock_station->hp_lock);
07a18684 633 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
c8f7a62c 634
671adbec
KCA
635 /* initialize platform device stuff */
636 dock_device.name = dock_device_name;
637 ret = platform_device_register(&dock_device);
638 if (ret) {
e67beb37 639 printk(KERN_ERR PREFIX "Error %d registering dock device\n", ret);
671adbec
KCA
640 kfree(dock_station);
641 return ret;
642 }
643
c8f7a62c
LB
644 /* Find dependent devices */
645 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
646 ACPI_UINT32_MAX, find_dock_devices, dock_station,
647 NULL);
648
649 /* add the dock station as a device dependent on itself */
650 dd = alloc_dock_dependent_device(handle);
651 if (!dd) {
652 kfree(dock_station);
671adbec
KCA
653 ret = -ENOMEM;
654 goto dock_add_err_unregister;
c8f7a62c
LB
655 }
656 add_dock_dependent_device(dock_station, dd);
657
658 /* register for dock events */
659 status = acpi_install_notify_handler(dock_station->handle,
660 ACPI_SYSTEM_NOTIFY,
661 dock_notify, dock_station);
662
663 if (ACPI_FAILURE(status)) {
664 printk(KERN_ERR PREFIX "Error installing notify handler\n");
665 ret = -ENODEV;
666 goto dock_add_err;
667 }
668
669 printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_NAME);
670
671 return 0;
672
673dock_add_err:
c8f7a62c 674 kfree(dd);
671adbec
KCA
675dock_add_err_unregister:
676 platform_device_unregister(&dock_device);
677 kfree(dock_station);
c8f7a62c
LB
678 return ret;
679}
680
681/**
682 * dock_remove - free up resources related to the dock station
683 */
684static int dock_remove(void)
685{
686 struct dock_dependent_device *dd, *tmp;
687 acpi_status status;
688
689 if (!dock_station)
690 return 0;
691
692 /* remove dependent devices */
693 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
694 list)
695 kfree(dd);
696
697 /* remove dock notify handler */
698 status = acpi_remove_notify_handler(dock_station->handle,
699 ACPI_SYSTEM_NOTIFY,
700 dock_notify);
701 if (ACPI_FAILURE(status))
702 printk(KERN_ERR "Error removing notify handler\n");
703
671adbec
KCA
704 /* cleanup sysfs */
705 platform_device_unregister(&dock_device);
706
c8f7a62c
LB
707 /* free dock station memory */
708 kfree(dock_station);
709 return 0;
710}
711
712/**
713 * find_dock - look for a dock station
714 * @handle: acpi handle of a device
715 * @lvl: unused
716 * @context: counter of dock stations found
717 * @rv: unused
718 *
719 * This is called by acpi_walk_namespace to look for dock stations.
720 */
721static acpi_status
722find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
723{
724 int *count = (int *)context;
725 acpi_status status = AE_OK;
726
727 if (is_dock(handle)) {
728 if (dock_add(handle) >= 0) {
729 (*count)++;
730 status = AE_CTRL_TERMINATE;
731 }
732 }
733 return status;
734}
735
736static int __init dock_init(void)
737{
738 int num = 0;
739
740 dock_station = NULL;
741
742 /* look for a dock station */
743 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
744 ACPI_UINT32_MAX, find_dock, &num, NULL);
745
746 if (!num)
747 return -ENODEV;
748
749 return 0;
750}
751
752static void __exit dock_exit(void)
753{
754 dock_remove();
755}
756
757postcore_initcall(dock_init);
758module_exit(dock_exit);