ACPICA: Major update for acpi_get_object_info external interface
[safe/jmp/linux-2.6] / drivers / acpi / dock.c
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 <linux/stddef.h>
33 #include <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
35
36 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
37
38 ACPI_MODULE_NAME("dock");
39 MODULE_AUTHOR("Kristen Carlson Accardi");
40 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
41 MODULE_LICENSE("GPL");
42
43 static int immediate_undock = 1;
44 module_param(immediate_undock, bool, 0644);
45 MODULE_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
50 static struct atomic_notifier_head dock_notifier_list;
51 static char dock_device_name[] = "dock";
52
53 static const struct acpi_device_id dock_device_ids[] = {
54         {"LNXDOCK", 0},
55         {"", 0},
56 };
57 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
58
59 struct dock_station {
60         acpi_handle handle;
61         unsigned long last_dock_time;
62         u32 flags;
63         spinlock_t dd_lock;
64         struct mutex hp_lock;
65         struct list_head dependent_devices;
66         struct list_head hotplug_devices;
67
68         struct list_head sibiling;
69         struct platform_device *dock_device;
70 };
71 static LIST_HEAD(dock_stations);
72 static int dock_station_count;
73
74 struct dock_dependent_device {
75         struct list_head list;
76         struct list_head hotplug_list;
77         acpi_handle handle;
78         struct acpi_dock_ops *ops;
79         void *context;
80 };
81
82 #define DOCK_DOCKING    0x00000001
83 #define DOCK_UNDOCKING  0x00000002
84 #define DOCK_IS_DOCK    0x00000010
85 #define DOCK_IS_ATA     0x00000020
86 #define DOCK_IS_BAT     0x00000040
87 #define DOCK_EVENT      3
88 #define UNDOCK_EVENT    2
89
90 /*****************************************************************************
91  *                         Dock Dependent device functions                   *
92  *****************************************************************************/
93 /**
94  *  alloc_dock_dependent_device - allocate and init a dependent device
95  *  @handle: the acpi_handle of the dependent device
96  *
97  *  Allocate memory for a dependent device structure for a device referenced
98  *  by the acpi handle
99  */
100 static struct dock_dependent_device *
101 alloc_dock_dependent_device(acpi_handle handle)
102 {
103         struct dock_dependent_device *dd;
104
105         dd = kzalloc(sizeof(*dd), GFP_KERNEL);
106         if (dd) {
107                 dd->handle = handle;
108                 INIT_LIST_HEAD(&dd->list);
109                 INIT_LIST_HEAD(&dd->hotplug_list);
110         }
111         return dd;
112 }
113
114 /**
115  * add_dock_dependent_device - associate a device with the dock station
116  * @ds: The dock station
117  * @dd: The dependent device
118  *
119  * Add the dependent device to the dock's dependent device list.
120  */
121 static void
122 add_dock_dependent_device(struct dock_station *ds,
123                           struct dock_dependent_device *dd)
124 {
125         spin_lock(&ds->dd_lock);
126         list_add_tail(&dd->list, &ds->dependent_devices);
127         spin_unlock(&ds->dd_lock);
128 }
129
130 /**
131  * dock_add_hotplug_device - associate a hotplug handler with the dock station
132  * @ds: The dock station
133  * @dd: The dependent device struct
134  *
135  * Add the dependent device to the dock's hotplug device list
136  */
137 static void
138 dock_add_hotplug_device(struct dock_station *ds,
139                         struct dock_dependent_device *dd)
140 {
141         mutex_lock(&ds->hp_lock);
142         list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
143         mutex_unlock(&ds->hp_lock);
144 }
145
146 /**
147  * dock_del_hotplug_device - remove a hotplug handler from the dock station
148  * @ds: The dock station
149  * @dd: the dependent device struct
150  *
151  * Delete the dependent device from the dock's hotplug device list
152  */
153 static void
154 dock_del_hotplug_device(struct dock_station *ds,
155                         struct dock_dependent_device *dd)
156 {
157         mutex_lock(&ds->hp_lock);
158         list_del(&dd->hotplug_list);
159         mutex_unlock(&ds->hp_lock);
160 }
161
162 /**
163  * find_dock_dependent_device - get a device dependent on this dock
164  * @ds: the dock station
165  * @handle: the acpi_handle of the device we want
166  *
167  * iterate over the dependent device list for this dock.  If the
168  * dependent device matches the handle, return.
169  */
170 static struct dock_dependent_device *
171 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
172 {
173         struct dock_dependent_device *dd;
174
175         spin_lock(&ds->dd_lock);
176         list_for_each_entry(dd, &ds->dependent_devices, list) {
177                 if (handle == dd->handle) {
178                         spin_unlock(&ds->dd_lock);
179                         return dd;
180                 }
181         }
182         spin_unlock(&ds->dd_lock);
183         return NULL;
184 }
185
186 /*****************************************************************************
187  *                         Dock functions                                    *
188  *****************************************************************************/
189 /**
190  * is_dock - see if a device is a dock station
191  * @handle: acpi handle of the device
192  *
193  * If an acpi object has a _DCK method, then it is by definition a dock
194  * station, so return true.
195  */
196 static int is_dock(acpi_handle handle)
197 {
198         acpi_status status;
199         acpi_handle tmp;
200
201         status = acpi_get_handle(handle, "_DCK", &tmp);
202         if (ACPI_FAILURE(status))
203                 return 0;
204         return 1;
205 }
206
207 static int is_ejectable(acpi_handle handle)
208 {
209         acpi_status status;
210         acpi_handle tmp;
211
212         status = acpi_get_handle(handle, "_EJ0", &tmp);
213         if (ACPI_FAILURE(status))
214                 return 0;
215         return 1;
216 }
217
218 static int is_ata(acpi_handle handle)
219 {
220         acpi_handle tmp;
221
222         if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
223            (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
224            (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
225            (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
226                 return 1;
227
228         return 0;
229 }
230
231 static int is_battery(acpi_handle handle)
232 {
233         struct acpi_device_info *info;
234         int ret = 1;
235
236         if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
237                 return 0;
238         if (!(info->valid & ACPI_VALID_HID))
239                 ret = 0;
240         else
241                 ret = !strcmp("PNP0C0A", info->hardware_id.string);
242
243         kfree(info);
244         return ret;
245 }
246
247 static int is_ejectable_bay(acpi_handle handle)
248 {
249         acpi_handle phandle;
250         if (!is_ejectable(handle))
251                 return 0;
252         if (is_battery(handle) || is_ata(handle))
253                 return 1;
254         if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
255                 return 1;
256         return 0;
257 }
258
259 /**
260  * is_dock_device - see if a device is on a dock station
261  * @handle: acpi handle of the device
262  *
263  * If this device is either the dock station itself,
264  * or is a device dependent on the dock station, then it
265  * is a dock device
266  */
267 int is_dock_device(acpi_handle handle)
268 {
269         struct dock_station *dock_station;
270
271         if (!dock_station_count)
272                 return 0;
273
274         if (is_dock(handle))
275                 return 1;
276         list_for_each_entry(dock_station, &dock_stations, sibiling) {
277                 if (find_dock_dependent_device(dock_station, handle))
278                         return 1;
279         }
280
281         return 0;
282 }
283
284 EXPORT_SYMBOL_GPL(is_dock_device);
285
286 /**
287  * dock_present - see if the dock station is present.
288  * @ds: the dock station
289  *
290  * execute the _STA method.  note that present does not
291  * imply that we are docked.
292  */
293 static int dock_present(struct dock_station *ds)
294 {
295         unsigned long long sta;
296         acpi_status status;
297
298         if (ds) {
299                 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
300                 if (ACPI_SUCCESS(status) && sta)
301                         return 1;
302         }
303         return 0;
304 }
305
306
307
308 /**
309  * dock_create_acpi_device - add new devices to acpi
310  * @handle - handle of the device to add
311  *
312  *  This function will create a new acpi_device for the given
313  *  handle if one does not exist already.  This should cause
314  *  acpi to scan for drivers for the given devices, and call
315  *  matching driver's add routine.
316  *
317  *  Returns a pointer to the acpi_device corresponding to the handle.
318  */
319 static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
320 {
321         struct acpi_device *device = NULL;
322         struct acpi_device *parent_device;
323         acpi_handle parent;
324         int ret;
325
326         if (acpi_bus_get_device(handle, &device)) {
327                 /*
328                  * no device created for this object,
329                  * so we should create one.
330                  */
331                 acpi_get_parent(handle, &parent);
332                 if (acpi_bus_get_device(parent, &parent_device))
333                         parent_device = NULL;
334
335                 ret = acpi_bus_add(&device, parent_device, handle,
336                         ACPI_BUS_TYPE_DEVICE);
337                 if (ret) {
338                         pr_debug("error adding bus, %x\n",
339                                 -ret);
340                         return NULL;
341                 }
342         }
343         return device;
344 }
345
346 /**
347  * dock_remove_acpi_device - remove the acpi_device struct from acpi
348  * @handle - the handle of the device to remove
349  *
350  *  Tell acpi to remove the acpi_device.  This should cause any loaded
351  *  driver to have it's remove routine called.
352  */
353 static void dock_remove_acpi_device(acpi_handle handle)
354 {
355         struct acpi_device *device;
356         int ret;
357
358         if (!acpi_bus_get_device(handle, &device)) {
359                 ret = acpi_bus_trim(device, 1);
360                 if (ret)
361                         pr_debug("error removing bus, %x\n", -ret);
362         }
363 }
364
365
366 /**
367  * hotplug_dock_devices - insert or remove devices on the dock station
368  * @ds: the dock station
369  * @event: either bus check or eject request
370  *
371  * Some devices on the dock station need to have drivers called
372  * to perform hotplug operations after a dock event has occurred.
373  * Traverse the list of dock devices that have registered a
374  * hotplug handler, and call the handler.
375  */
376 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
377 {
378         struct dock_dependent_device *dd;
379
380         mutex_lock(&ds->hp_lock);
381
382         /*
383          * First call driver specific hotplug functions
384          */
385         list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
386                 if (dd->ops && dd->ops->handler)
387                         dd->ops->handler(dd->handle, event, dd->context);
388         }
389
390         /*
391          * Now make sure that an acpi_device is created for each
392          * dependent device, or removed if this is an eject request.
393          * This will cause acpi_drivers to be stopped/started if they
394          * exist
395          */
396         list_for_each_entry(dd, &ds->dependent_devices, list) {
397                 if (event == ACPI_NOTIFY_EJECT_REQUEST)
398                         dock_remove_acpi_device(dd->handle);
399                 else
400                         dock_create_acpi_device(dd->handle);
401         }
402         mutex_unlock(&ds->hp_lock);
403 }
404
405 static void dock_event(struct dock_station *ds, u32 event, int num)
406 {
407         struct device *dev = &ds->dock_device->dev;
408         char event_string[13];
409         char *envp[] = { event_string, NULL };
410         struct dock_dependent_device *dd;
411
412         if (num == UNDOCK_EVENT)
413                 sprintf(event_string, "EVENT=undock");
414         else
415                 sprintf(event_string, "EVENT=dock");
416
417         /*
418          * Indicate that the status of the dock station has
419          * changed.
420          */
421         if (num == DOCK_EVENT)
422                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
423
424         list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
425                 if (dd->ops && dd->ops->uevent)
426                         dd->ops->uevent(dd->handle, event, dd->context);
427         if (num != DOCK_EVENT)
428                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
429 }
430
431 /**
432  * eject_dock - respond to a dock eject request
433  * @ds: the dock station
434  *
435  * This is called after _DCK is called, to execute the dock station's
436  * _EJ0 method.
437  */
438 static void eject_dock(struct dock_station *ds)
439 {
440         struct acpi_object_list arg_list;
441         union acpi_object arg;
442         acpi_status status;
443         acpi_handle tmp;
444
445         /* all dock devices should have _EJ0, but check anyway */
446         status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
447         if (ACPI_FAILURE(status)) {
448                 pr_debug("No _EJ0 support for dock device\n");
449                 return;
450         }
451
452         arg_list.count = 1;
453         arg_list.pointer = &arg;
454         arg.type = ACPI_TYPE_INTEGER;
455         arg.integer.value = 1;
456
457         if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
458                                               &arg_list, NULL)))
459                 pr_debug("Failed to evaluate _EJ0!\n");
460 }
461
462 /**
463  * handle_dock - handle a dock event
464  * @ds: the dock station
465  * @dock: to dock, or undock - that is the question
466  *
467  * Execute the _DCK method in response to an acpi event
468  */
469 static void handle_dock(struct dock_station *ds, int dock)
470 {
471         acpi_status status;
472         struct acpi_object_list arg_list;
473         union acpi_object arg;
474         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
475         struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
476
477         acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
478
479         printk(KERN_INFO PREFIX "%s - %s\n",
480                 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
481
482         /* _DCK method has one argument */
483         arg_list.count = 1;
484         arg_list.pointer = &arg;
485         arg.type = ACPI_TYPE_INTEGER;
486         arg.integer.value = dock;
487         status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
488         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
489                 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
490                         " _DCK\n", (char *)name_buffer.pointer));
491
492         kfree(buffer.pointer);
493         kfree(name_buffer.pointer);
494 }
495
496 static inline void dock(struct dock_station *ds)
497 {
498         handle_dock(ds, 1);
499 }
500
501 static inline void undock(struct dock_station *ds)
502 {
503         handle_dock(ds, 0);
504 }
505
506 static inline void begin_dock(struct dock_station *ds)
507 {
508         ds->flags |= DOCK_DOCKING;
509 }
510
511 static inline void complete_dock(struct dock_station *ds)
512 {
513         ds->flags &= ~(DOCK_DOCKING);
514         ds->last_dock_time = jiffies;
515 }
516
517 static inline void begin_undock(struct dock_station *ds)
518 {
519         ds->flags |= DOCK_UNDOCKING;
520 }
521
522 static inline void complete_undock(struct dock_station *ds)
523 {
524         ds->flags &= ~(DOCK_UNDOCKING);
525 }
526
527 static void dock_lock(struct dock_station *ds, int lock)
528 {
529         struct acpi_object_list arg_list;
530         union acpi_object arg;
531         acpi_status status;
532
533         arg_list.count = 1;
534         arg_list.pointer = &arg;
535         arg.type = ACPI_TYPE_INTEGER;
536         arg.integer.value = !!lock;
537         status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
538         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
539                 if (lock)
540                         printk(KERN_WARNING PREFIX "Locking device failed\n");
541                 else
542                         printk(KERN_WARNING PREFIX "Unlocking device failed\n");
543         }
544 }
545
546 /**
547  * dock_in_progress - see if we are in the middle of handling a dock event
548  * @ds: the dock station
549  *
550  * Sometimes while docking, false dock events can be sent to the driver
551  * because good connections aren't made or some other reason.  Ignore these
552  * if we are in the middle of doing something.
553  */
554 static int dock_in_progress(struct dock_station *ds)
555 {
556         if ((ds->flags & DOCK_DOCKING) ||
557             time_before(jiffies, (ds->last_dock_time + HZ)))
558                 return 1;
559         return 0;
560 }
561
562 /**
563  * register_dock_notifier - add yourself to the dock notifier list
564  * @nb: the callers notifier block
565  *
566  * If a driver wishes to be notified about dock events, they can
567  * use this function to put a notifier block on the dock notifier list.
568  * this notifier call chain will be called after a dock event, but
569  * before hotplugging any new devices.
570  */
571 int register_dock_notifier(struct notifier_block *nb)
572 {
573         if (!dock_station_count)
574                 return -ENODEV;
575
576         return atomic_notifier_chain_register(&dock_notifier_list, nb);
577 }
578
579 EXPORT_SYMBOL_GPL(register_dock_notifier);
580
581 /**
582  * unregister_dock_notifier - remove yourself from the dock notifier list
583  * @nb: the callers notifier block
584  */
585 void unregister_dock_notifier(struct notifier_block *nb)
586 {
587         if (!dock_station_count)
588                 return;
589
590         atomic_notifier_chain_unregister(&dock_notifier_list, nb);
591 }
592
593 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
594
595 /**
596  * register_hotplug_dock_device - register a hotplug function
597  * @handle: the handle of the device
598  * @ops: handlers to call after docking
599  * @context: device specific data
600  *
601  * If a driver would like to perform a hotplug operation after a dock
602  * event, they can register an acpi_notifiy_handler to be called by
603  * the dock driver after _DCK is executed.
604  */
605 int
606 register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
607                              void *context)
608 {
609         struct dock_dependent_device *dd;
610         struct dock_station *dock_station;
611         int ret = -EINVAL;
612
613         if (!dock_station_count)
614                 return -ENODEV;
615
616         /*
617          * make sure this handle is for a device dependent on the dock,
618          * this would include the dock station itself
619          */
620         list_for_each_entry(dock_station, &dock_stations, sibiling) {
621                 /*
622                  * An ATA bay can be in a dock and itself can be ejected
623                  * seperately, so there are two 'dock stations' which need the
624                  * ops
625                  */
626                 dd = find_dock_dependent_device(dock_station, handle);
627                 if (dd) {
628                         dd->ops = ops;
629                         dd->context = context;
630                         dock_add_hotplug_device(dock_station, dd);
631                         ret = 0;
632                 }
633         }
634
635         return ret;
636 }
637
638 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
639
640 /**
641  * unregister_hotplug_dock_device - remove yourself from the hotplug list
642  * @handle: the acpi handle of the device
643  */
644 void unregister_hotplug_dock_device(acpi_handle handle)
645 {
646         struct dock_dependent_device *dd;
647         struct dock_station *dock_station;
648
649         if (!dock_station_count)
650                 return;
651
652         list_for_each_entry(dock_station, &dock_stations, sibiling) {
653                 dd = find_dock_dependent_device(dock_station, handle);
654                 if (dd)
655                         dock_del_hotplug_device(dock_station, dd);
656         }
657 }
658
659 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
660
661 /**
662  * handle_eject_request - handle an undock request checking for error conditions
663  *
664  * Check to make sure the dock device is still present, then undock and
665  * hotremove all the devices that may need removing.
666  */
667 static int handle_eject_request(struct dock_station *ds, u32 event)
668 {
669         if (dock_in_progress(ds))
670                 return -EBUSY;
671
672         /*
673          * here we need to generate the undock
674          * event prior to actually doing the undock
675          * so that the device struct still exists.
676          * Also, even send the dock event if the
677          * device is not present anymore
678          */
679         dock_event(ds, event, UNDOCK_EVENT);
680
681         hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
682         undock(ds);
683         dock_lock(ds, 0);
684         eject_dock(ds);
685         if (dock_present(ds)) {
686                 printk(KERN_ERR PREFIX "Unable to undock!\n");
687                 return -EBUSY;
688         }
689         complete_undock(ds);
690         return 0;
691 }
692
693 /**
694  * dock_notify - act upon an acpi dock notification
695  * @handle: the dock station handle
696  * @event: the acpi event
697  * @data: our driver data struct
698  *
699  * If we are notified to dock, then check to see if the dock is
700  * present and then dock.  Notify all drivers of the dock event,
701  * and then hotplug and devices that may need hotplugging.
702  */
703 static void dock_notify(acpi_handle handle, u32 event, void *data)
704 {
705         struct dock_station *ds = data;
706         struct acpi_device *tmp;
707         int surprise_removal = 0;
708
709         /*
710          * According to acpi spec 3.0a, if a DEVICE_CHECK notification
711          * is sent and _DCK is present, it is assumed to mean an undock
712          * request.
713          */
714         if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
715                 event = ACPI_NOTIFY_EJECT_REQUEST;
716
717         /*
718          * dock station: BUS_CHECK - docked or surprise removal
719          *               DEVICE_CHECK - undocked
720          * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
721          *
722          * To simplify event handling, dock dependent device handler always
723          * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
724          * ACPI_NOTIFY_EJECT_REQUEST for removal
725          */
726         switch (event) {
727         case ACPI_NOTIFY_BUS_CHECK:
728         case ACPI_NOTIFY_DEVICE_CHECK:
729                 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
730                    &tmp)) {
731                         begin_dock(ds);
732                         dock(ds);
733                         if (!dock_present(ds)) {
734                                 printk(KERN_ERR PREFIX "Unable to dock!\n");
735                                 complete_dock(ds);
736                                 break;
737                         }
738                         atomic_notifier_call_chain(&dock_notifier_list,
739                                                    event, NULL);
740                         hotplug_dock_devices(ds, event);
741                         complete_dock(ds);
742                         dock_event(ds, event, DOCK_EVENT);
743                         dock_lock(ds, 1);
744                         break;
745                 }
746                 if (dock_present(ds) || dock_in_progress(ds))
747                         break;
748                 /* This is a surprise removal */
749                 surprise_removal = 1;
750                 event = ACPI_NOTIFY_EJECT_REQUEST;
751                 /* Fall back */
752         case ACPI_NOTIFY_EJECT_REQUEST:
753                 begin_undock(ds);
754                 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
755                    || surprise_removal)
756                         handle_eject_request(ds, event);
757                 else
758                         dock_event(ds, event, UNDOCK_EVENT);
759                 break;
760         default:
761                 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
762         }
763 }
764
765 struct dock_data {
766         acpi_handle handle;
767         unsigned long event;
768         struct dock_station *ds;
769 };
770
771 static void acpi_dock_deferred_cb(void *context)
772 {
773         struct dock_data *data = (struct dock_data *)context;
774
775         dock_notify(data->handle, data->event, data->ds);
776         kfree(data);
777 }
778
779 static int acpi_dock_notifier_call(struct notifier_block *this,
780         unsigned long event, void *data)
781 {
782         struct dock_station *dock_station;
783         acpi_handle handle = (acpi_handle)data;
784
785         if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
786            && event != ACPI_NOTIFY_EJECT_REQUEST)
787                 return 0;
788         list_for_each_entry(dock_station, &dock_stations, sibiling) {
789                 if (dock_station->handle == handle) {
790                         struct dock_data *dock_data;
791
792                         dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
793                         if (!dock_data)
794                                 return 0;
795                         dock_data->handle = handle;
796                         dock_data->event = event;
797                         dock_data->ds = dock_station;
798                         acpi_os_hotplug_execute(acpi_dock_deferred_cb,
799                                 dock_data);
800                         return 0 ;
801                 }
802         }
803         return 0;
804 }
805
806 static struct notifier_block dock_acpi_notifier = {
807         .notifier_call = acpi_dock_notifier_call,
808 };
809
810 /**
811  * find_dock_devices - find devices on the dock station
812  * @handle: the handle of the device we are examining
813  * @lvl: unused
814  * @context: the dock station private data
815  * @rv: unused
816  *
817  * This function is called by acpi_walk_namespace.  It will
818  * check to see if an object has an _EJD method.  If it does, then it
819  * will see if it is dependent on the dock station.
820  */
821 static acpi_status
822 find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
823 {
824         acpi_status status;
825         acpi_handle tmp, parent;
826         struct dock_station *ds = context;
827         struct dock_dependent_device *dd;
828
829         status = acpi_bus_get_ejd(handle, &tmp);
830         if (ACPI_FAILURE(status)) {
831                 /* try the parent device as well */
832                 status = acpi_get_parent(handle, &parent);
833                 if (ACPI_FAILURE(status))
834                         goto fdd_out;
835                 /* see if parent is dependent on dock */
836                 status = acpi_bus_get_ejd(parent, &tmp);
837                 if (ACPI_FAILURE(status))
838                         goto fdd_out;
839         }
840
841         if (tmp == ds->handle) {
842                 dd = alloc_dock_dependent_device(handle);
843                 if (dd)
844                         add_dock_dependent_device(ds, dd);
845         }
846 fdd_out:
847         return AE_OK;
848 }
849
850 /*
851  * show_docked - read method for "docked" file in sysfs
852  */
853 static ssize_t show_docked(struct device *dev,
854                            struct device_attribute *attr, char *buf)
855 {
856         struct acpi_device *tmp;
857
858         struct dock_station *dock_station = *((struct dock_station **)
859                 dev->platform_data);
860
861         if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
862                 return snprintf(buf, PAGE_SIZE, "1\n");
863         return snprintf(buf, PAGE_SIZE, "0\n");
864 }
865 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
866
867 /*
868  * show_flags - read method for flags file in sysfs
869  */
870 static ssize_t show_flags(struct device *dev,
871                           struct device_attribute *attr, char *buf)
872 {
873         struct dock_station *dock_station = *((struct dock_station **)
874                 dev->platform_data);
875         return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
876
877 }
878 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
879
880 /*
881  * write_undock - write method for "undock" file in sysfs
882  */
883 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
884                            const char *buf, size_t count)
885 {
886         int ret;
887         struct dock_station *dock_station = *((struct dock_station **)
888                 dev->platform_data);
889
890         if (!count)
891                 return -EINVAL;
892
893         begin_undock(dock_station);
894         ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
895         return ret ? ret: count;
896 }
897 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
898
899 /*
900  * show_dock_uid - read method for "uid" file in sysfs
901  */
902 static ssize_t show_dock_uid(struct device *dev,
903                              struct device_attribute *attr, char *buf)
904 {
905         unsigned long long lbuf;
906         struct dock_station *dock_station = *((struct dock_station **)
907                 dev->platform_data);
908         acpi_status status = acpi_evaluate_integer(dock_station->handle,
909                                         "_UID", NULL, &lbuf);
910         if (ACPI_FAILURE(status))
911             return 0;
912
913         return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
914 }
915 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
916
917 static ssize_t show_dock_type(struct device *dev,
918                 struct device_attribute *attr, char *buf)
919 {
920         struct dock_station *dock_station = *((struct dock_station **)
921                 dev->platform_data);
922         char *type;
923
924         if (dock_station->flags & DOCK_IS_DOCK)
925                 type = "dock_station";
926         else if (dock_station->flags & DOCK_IS_ATA)
927                 type = "ata_bay";
928         else if (dock_station->flags & DOCK_IS_BAT)
929                 type = "battery_bay";
930         else
931                 type = "unknown";
932
933         return snprintf(buf, PAGE_SIZE, "%s\n", type);
934 }
935 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
936
937 /**
938  * dock_add - add a new dock station
939  * @handle: the dock station handle
940  *
941  * allocated and initialize a new dock station device.  Find all devices
942  * that are on the dock station, and register for dock event notifications.
943  */
944 static int dock_add(acpi_handle handle)
945 {
946         int ret;
947         struct dock_dependent_device *dd;
948         struct dock_station *dock_station;
949         struct platform_device *dock_device;
950
951         /* allocate & initialize the dock_station private data */
952         dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
953         if (!dock_station)
954                 return -ENOMEM;
955         dock_station->handle = handle;
956         dock_station->last_dock_time = jiffies - HZ;
957         INIT_LIST_HEAD(&dock_station->dependent_devices);
958         INIT_LIST_HEAD(&dock_station->hotplug_devices);
959         INIT_LIST_HEAD(&dock_station->sibiling);
960         spin_lock_init(&dock_station->dd_lock);
961         mutex_init(&dock_station->hp_lock);
962         ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
963
964         /* initialize platform device stuff */
965         dock_station->dock_device =
966                 platform_device_register_simple(dock_device_name,
967                         dock_station_count, NULL, 0);
968         dock_device = dock_station->dock_device;
969         if (IS_ERR(dock_device)) {
970                 kfree(dock_station);
971                 dock_station = NULL;
972                 return PTR_ERR(dock_device);
973         }
974         platform_device_add_data(dock_device, &dock_station,
975                 sizeof(struct dock_station *));
976
977         /* we want the dock device to send uevents */
978         dev_set_uevent_suppress(&dock_device->dev, 0);
979
980         if (is_dock(handle))
981                 dock_station->flags |= DOCK_IS_DOCK;
982         if (is_ata(handle))
983                 dock_station->flags |= DOCK_IS_ATA;
984         if (is_battery(handle))
985                 dock_station->flags |= DOCK_IS_BAT;
986
987         ret = device_create_file(&dock_device->dev, &dev_attr_docked);
988         if (ret) {
989                 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
990                 platform_device_unregister(dock_device);
991                 kfree(dock_station);
992                 dock_station = NULL;
993                 return ret;
994         }
995         ret = device_create_file(&dock_device->dev, &dev_attr_undock);
996         if (ret) {
997                 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
998                 device_remove_file(&dock_device->dev, &dev_attr_docked);
999                 platform_device_unregister(dock_device);
1000                 kfree(dock_station);
1001                 dock_station = NULL;
1002                 return ret;
1003         }
1004         ret = device_create_file(&dock_device->dev, &dev_attr_uid);
1005         if (ret) {
1006                 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
1007                 device_remove_file(&dock_device->dev, &dev_attr_docked);
1008                 device_remove_file(&dock_device->dev, &dev_attr_undock);
1009                 platform_device_unregister(dock_device);
1010                 kfree(dock_station);
1011                 dock_station = NULL;
1012                 return ret;
1013         }
1014         ret = device_create_file(&dock_device->dev, &dev_attr_flags);
1015         if (ret) {
1016                 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
1017                 device_remove_file(&dock_device->dev, &dev_attr_docked);
1018                 device_remove_file(&dock_device->dev, &dev_attr_undock);
1019                 device_remove_file(&dock_device->dev, &dev_attr_uid);
1020                 platform_device_unregister(dock_device);
1021                 kfree(dock_station);
1022                 dock_station = NULL;
1023                 return ret;
1024         }
1025         ret = device_create_file(&dock_device->dev, &dev_attr_type);
1026         if (ret)
1027                 printk(KERN_ERR"Error %d adding sysfs file\n", ret);
1028
1029         /* Find dependent devices */
1030         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1031                             ACPI_UINT32_MAX, find_dock_devices, dock_station,
1032                             NULL);
1033
1034         /* add the dock station as a device dependent on itself */
1035         dd = alloc_dock_dependent_device(handle);
1036         if (!dd) {
1037                 kfree(dock_station);
1038                 dock_station = NULL;
1039                 ret = -ENOMEM;
1040                 goto dock_add_err_unregister;
1041         }
1042         add_dock_dependent_device(dock_station, dd);
1043
1044         dock_station_count++;
1045         list_add(&dock_station->sibiling, &dock_stations);
1046         return 0;
1047
1048 dock_add_err_unregister:
1049         device_remove_file(&dock_device->dev, &dev_attr_type);
1050         device_remove_file(&dock_device->dev, &dev_attr_docked);
1051         device_remove_file(&dock_device->dev, &dev_attr_undock);
1052         device_remove_file(&dock_device->dev, &dev_attr_uid);
1053         device_remove_file(&dock_device->dev, &dev_attr_flags);
1054         platform_device_unregister(dock_device);
1055         kfree(dock_station);
1056         dock_station = NULL;
1057         return ret;
1058 }
1059
1060 /**
1061  * dock_remove - free up resources related to the dock station
1062  */
1063 static int dock_remove(struct dock_station *dock_station)
1064 {
1065         struct dock_dependent_device *dd, *tmp;
1066         struct platform_device *dock_device = dock_station->dock_device;
1067
1068         if (!dock_station_count)
1069                 return 0;
1070
1071         /* remove dependent devices */
1072         list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1073                                  list)
1074             kfree(dd);
1075
1076         /* cleanup sysfs */
1077         device_remove_file(&dock_device->dev, &dev_attr_type);
1078         device_remove_file(&dock_device->dev, &dev_attr_docked);
1079         device_remove_file(&dock_device->dev, &dev_attr_undock);
1080         device_remove_file(&dock_device->dev, &dev_attr_uid);
1081         device_remove_file(&dock_device->dev, &dev_attr_flags);
1082         platform_device_unregister(dock_device);
1083
1084         /* free dock station memory */
1085         kfree(dock_station);
1086         dock_station = NULL;
1087         return 0;
1088 }
1089
1090 /**
1091  * find_dock - look for a dock station
1092  * @handle: acpi handle of a device
1093  * @lvl: unused
1094  * @context: counter of dock stations found
1095  * @rv: unused
1096  *
1097  * This is called by acpi_walk_namespace to look for dock stations.
1098  */
1099 static acpi_status
1100 find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1101 {
1102         acpi_status status = AE_OK;
1103
1104         if (is_dock(handle)) {
1105                 if (dock_add(handle) >= 0) {
1106                         status = AE_CTRL_TERMINATE;
1107                 }
1108         }
1109         return status;
1110 }
1111
1112 static acpi_status
1113 find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1114 {
1115         /* If bay is a dock, it's already handled */
1116         if (is_ejectable_bay(handle) && !is_dock(handle))
1117                 dock_add(handle);
1118         return AE_OK;
1119 }
1120
1121 static int __init dock_init(void)
1122 {
1123         if (acpi_disabled)
1124                 return 0;
1125
1126         /* look for a dock station */
1127         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1128                             ACPI_UINT32_MAX, find_dock, NULL, NULL);
1129
1130         /* look for bay */
1131         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1132                         ACPI_UINT32_MAX, find_bay, NULL, NULL);
1133         if (!dock_station_count) {
1134                 printk(KERN_INFO PREFIX "No dock devices found.\n");
1135                 return 0;
1136         }
1137
1138         register_acpi_bus_notifier(&dock_acpi_notifier);
1139         printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1140                 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
1141         return 0;
1142 }
1143
1144 static void __exit dock_exit(void)
1145 {
1146         struct dock_station *dock_station;
1147         struct dock_station *tmp;
1148
1149         unregister_acpi_bus_notifier(&dock_acpi_notifier);
1150         list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibiling)
1151                 dock_remove(dock_station);
1152 }
1153
1154 /*
1155  * Must be called before drivers of devices in dock, otherwise we can't know
1156  * which devices are in a dock
1157  */
1158 subsys_initcall(dock_init);
1159 module_exit(dock_exit);