ACPI / dock: Walk list in reverse order during removal of devices
[firefly-linux-kernel-4.4.55.git] / 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/slab.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/notifier.h>
31 #include <linux/platform_device.h>
32 #include <linux/jiffies.h>
33 #include <linux/stddef.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37
38 #define PREFIX "ACPI: "
39
40 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
41
42 ACPI_MODULE_NAME("dock");
43 MODULE_AUTHOR("Kristen Carlson Accardi");
44 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
45 MODULE_LICENSE("GPL");
46
47 static bool immediate_undock = 1;
48 module_param(immediate_undock, bool, 0644);
49 MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
50         "undock immediately when the undock button is pressed, 0 will cause"
51         " the driver to wait for userspace to write the undock sysfs file "
52         " before undocking");
53
54 static struct atomic_notifier_head dock_notifier_list;
55
56 static const struct acpi_device_id dock_device_ids[] = {
57         {"LNXDOCK", 0},
58         {"", 0},
59 };
60 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
61
62 struct dock_station {
63         acpi_handle handle;
64         unsigned long last_dock_time;
65         u32 flags;
66         struct list_head dependent_devices;
67
68         struct list_head sibling;
69         struct platform_device *dock_device;
70 };
71 static LIST_HEAD(dock_stations);
72 static int dock_station_count;
73 static DEFINE_MUTEX(hotplug_lock);
74
75 struct dock_dependent_device {
76         struct list_head list;
77         acpi_handle handle;
78         const struct acpi_dock_ops *hp_ops;
79         void *hp_context;
80         unsigned int hp_refcount;
81         void (*hp_release)(void *);
82 };
83
84 #define DOCK_DOCKING    0x00000001
85 #define DOCK_UNDOCKING  0x00000002
86 #define DOCK_IS_DOCK    0x00000010
87 #define DOCK_IS_ATA     0x00000020
88 #define DOCK_IS_BAT     0x00000040
89 #define DOCK_EVENT      3
90 #define UNDOCK_EVENT    2
91
92 /*****************************************************************************
93  *                         Dock Dependent device functions                   *
94  *****************************************************************************/
95 /**
96  * add_dock_dependent_device - associate a device with the dock station
97  * @ds: The dock station
98  * @handle: handle of the dependent device
99  *
100  * Add the dependent device to the dock's dependent device list.
101  */
102 static int __init
103 add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
104 {
105         struct dock_dependent_device *dd;
106
107         dd = kzalloc(sizeof(*dd), GFP_KERNEL);
108         if (!dd)
109                 return -ENOMEM;
110
111         dd->handle = handle;
112         INIT_LIST_HEAD(&dd->list);
113         list_add_tail(&dd->list, &ds->dependent_devices);
114
115         return 0;
116 }
117
118 /**
119  * dock_init_hotplug - Initialize a hotplug device on a docking station.
120  * @dd: Dock-dependent device.
121  * @ops: Dock operations to attach to the dependent device.
122  * @context: Data to pass to the @ops callbacks and @release.
123  * @init: Optional initialization routine to run after setting up context.
124  * @release: Optional release routine to run on removal.
125  */
126 static int dock_init_hotplug(struct dock_dependent_device *dd,
127                              const struct acpi_dock_ops *ops, void *context,
128                              void (*init)(void *), void (*release)(void *))
129 {
130         int ret = 0;
131
132         mutex_lock(&hotplug_lock);
133
134         if (dd->hp_context) {
135                 ret = -EEXIST;
136         } else {
137                 dd->hp_refcount = 1;
138                 dd->hp_ops = ops;
139                 dd->hp_context = context;
140                 dd->hp_release = release;
141         }
142
143         if (!WARN_ON(ret) && init)
144                 init(context);
145
146         mutex_unlock(&hotplug_lock);
147         return ret;
148 }
149
150 /**
151  * dock_release_hotplug - Decrement hotplug reference counter of dock device.
152  * @dd: Dock-dependent device.
153  *
154  * Decrement the reference counter of @dd and if 0, detach its hotplug
155  * operations from it, reset its context pointer and run the optional release
156  * routine if present.
157  */
158 static void dock_release_hotplug(struct dock_dependent_device *dd)
159 {
160         void (*release)(void *) = NULL;
161         void *context = NULL;
162
163         mutex_lock(&hotplug_lock);
164
165         if (dd->hp_context && !--dd->hp_refcount) {
166                 dd->hp_ops = NULL;
167                 context = dd->hp_context;
168                 dd->hp_context = NULL;
169                 release = dd->hp_release;
170                 dd->hp_release = NULL;
171         }
172
173         if (release && context)
174                 release(context);
175
176         mutex_unlock(&hotplug_lock);
177 }
178
179 static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
180                                bool uevent)
181 {
182         acpi_notify_handler cb = NULL;
183         bool run = false;
184
185         mutex_lock(&hotplug_lock);
186
187         if (dd->hp_context) {
188                 run = true;
189                 dd->hp_refcount++;
190                 if (dd->hp_ops)
191                         cb = uevent ? dd->hp_ops->uevent : dd->hp_ops->handler;
192         }
193
194         mutex_unlock(&hotplug_lock);
195
196         if (!run)
197                 return;
198
199         if (cb)
200                 cb(dd->handle, event, dd->hp_context);
201
202         dock_release_hotplug(dd);
203 }
204
205 /**
206  * find_dock_dependent_device - get a device dependent on this dock
207  * @ds: the dock station
208  * @handle: the acpi_handle of the device we want
209  *
210  * iterate over the dependent device list for this dock.  If the
211  * dependent device matches the handle, return.
212  */
213 static struct dock_dependent_device *
214 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
215 {
216         struct dock_dependent_device *dd;
217
218         list_for_each_entry(dd, &ds->dependent_devices, list)
219                 if (handle == dd->handle)
220                         return dd;
221
222         return NULL;
223 }
224
225 /*****************************************************************************
226  *                         Dock functions                                    *
227  *****************************************************************************/
228 static int __init is_battery(acpi_handle handle)
229 {
230         struct acpi_device_info *info;
231         int ret = 1;
232
233         if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
234                 return 0;
235         if (!(info->valid & ACPI_VALID_HID))
236                 ret = 0;
237         else
238                 ret = !strcmp("PNP0C0A", info->hardware_id.string);
239
240         kfree(info);
241         return ret;
242 }
243
244 /* Check whether ACPI object is an ejectable battery or disk bay */
245 static bool __init is_ejectable_bay(acpi_handle handle)
246 {
247         if (acpi_has_method(handle, "_EJ0") && is_battery(handle))
248                 return true;
249
250         return acpi_bay_match(handle);
251 }
252
253 /**
254  * is_dock_device - see if a device is on a dock station
255  * @handle: acpi handle of the device
256  *
257  * If this device is either the dock station itself,
258  * or is a device dependent on the dock station, then it
259  * is a dock device
260  */
261 int is_dock_device(acpi_handle handle)
262 {
263         struct dock_station *dock_station;
264
265         if (!dock_station_count)
266                 return 0;
267
268         if (acpi_dock_match(handle))
269                 return 1;
270
271         list_for_each_entry(dock_station, &dock_stations, sibling)
272                 if (find_dock_dependent_device(dock_station, handle))
273                         return 1;
274
275         return 0;
276 }
277 EXPORT_SYMBOL_GPL(is_dock_device);
278
279 /**
280  * dock_present - see if the dock station is present.
281  * @ds: the dock station
282  *
283  * execute the _STA method.  note that present does not
284  * imply that we are docked.
285  */
286 static int dock_present(struct dock_station *ds)
287 {
288         unsigned long long sta;
289         acpi_status status;
290
291         if (ds) {
292                 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
293                 if (ACPI_SUCCESS(status) && sta)
294                         return 1;
295         }
296         return 0;
297 }
298
299 /**
300  * dock_create_acpi_device - add new devices to acpi
301  * @handle - handle of the device to add
302  *
303  *  This function will create a new acpi_device for the given
304  *  handle if one does not exist already.  This should cause
305  *  acpi to scan for drivers for the given devices, and call
306  *  matching driver's add routine.
307  */
308 static void dock_create_acpi_device(acpi_handle handle)
309 {
310         struct acpi_device *device;
311         int ret;
312
313         if (acpi_bus_get_device(handle, &device)) {
314                 /*
315                  * no device created for this object,
316                  * so we should create one.
317                  */
318                 ret = acpi_bus_scan(handle);
319                 if (ret)
320                         pr_debug("error adding bus, %x\n", -ret);
321         }
322 }
323
324 /**
325  * dock_remove_acpi_device - remove the acpi_device struct from acpi
326  * @handle - the handle of the device to remove
327  *
328  *  Tell acpi to remove the acpi_device.  This should cause any loaded
329  *  driver to have it's remove routine called.
330  */
331 static void dock_remove_acpi_device(acpi_handle handle)
332 {
333         struct acpi_device *device;
334
335         if (!acpi_bus_get_device(handle, &device))
336                 acpi_bus_trim(device);
337 }
338
339 /**
340  * hot_remove_dock_devices - Remove dock station devices.
341  * @ds: Dock station.
342  */
343 static void hot_remove_dock_devices(struct dock_station *ds)
344 {
345         struct dock_dependent_device *dd;
346
347         /*
348          * Walk the list in reverse order so that devices that have been added
349          * last are removed first (in case there are some indirect dependencies
350          * between them).
351          */
352         list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
353                 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
354
355         list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
356                 dock_remove_acpi_device(dd->handle);
357 }
358
359 /**
360  * hotplug_dock_devices - Insert devices on a dock station.
361  * @ds: the dock station
362  * @event: either bus check or device check request
363  *
364  * Some devices on the dock station need to have drivers called
365  * to perform hotplug operations after a dock event has occurred.
366  * Traverse the list of dock devices that have registered a
367  * hotplug handler, and call the handler.
368  */
369 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
370 {
371         struct dock_dependent_device *dd;
372
373         /* Call driver specific hotplug functions. */
374         list_for_each_entry(dd, &ds->dependent_devices, list)
375                 dock_hotplug_event(dd, event, false);
376
377         /*
378          * Now make sure that an acpi_device is created for each dependent
379          * device.  That will cause scan handlers to be attached to device
380          * objects or acpi_drivers to be stopped/started if they are present.
381          */
382         list_for_each_entry(dd, &ds->dependent_devices, list)
383                 dock_create_acpi_device(dd->handle);
384 }
385
386 static void dock_event(struct dock_station *ds, u32 event, int num)
387 {
388         struct device *dev = &ds->dock_device->dev;
389         char event_string[13];
390         char *envp[] = { event_string, NULL };
391         struct dock_dependent_device *dd;
392
393         if (num == UNDOCK_EVENT)
394                 sprintf(event_string, "EVENT=undock");
395         else
396                 sprintf(event_string, "EVENT=dock");
397
398         /*
399          * Indicate that the status of the dock station has
400          * changed.
401          */
402         if (num == DOCK_EVENT)
403                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
404
405         list_for_each_entry(dd, &ds->dependent_devices, list)
406                 dock_hotplug_event(dd, event, true);
407
408         if (num != DOCK_EVENT)
409                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
410 }
411
412 /**
413  * handle_dock - handle a dock event
414  * @ds: the dock station
415  * @dock: to dock, or undock - that is the question
416  *
417  * Execute the _DCK method in response to an acpi event
418  */
419 static void handle_dock(struct dock_station *ds, int dock)
420 {
421         acpi_status status;
422         struct acpi_object_list arg_list;
423         union acpi_object arg;
424         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
425
426         acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
427
428         /* _DCK method has one argument */
429         arg_list.count = 1;
430         arg_list.pointer = &arg;
431         arg.type = ACPI_TYPE_INTEGER;
432         arg.integer.value = dock;
433         status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
434         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
435                 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
436                                 status);
437
438         kfree(buffer.pointer);
439 }
440
441 static inline void dock(struct dock_station *ds)
442 {
443         handle_dock(ds, 1);
444 }
445
446 static inline void undock(struct dock_station *ds)
447 {
448         handle_dock(ds, 0);
449 }
450
451 static inline void begin_dock(struct dock_station *ds)
452 {
453         ds->flags |= DOCK_DOCKING;
454 }
455
456 static inline void complete_dock(struct dock_station *ds)
457 {
458         ds->flags &= ~(DOCK_DOCKING);
459         ds->last_dock_time = jiffies;
460 }
461
462 static inline void begin_undock(struct dock_station *ds)
463 {
464         ds->flags |= DOCK_UNDOCKING;
465 }
466
467 static inline void complete_undock(struct dock_station *ds)
468 {
469         ds->flags &= ~(DOCK_UNDOCKING);
470 }
471
472 /**
473  * dock_in_progress - see if we are in the middle of handling a dock event
474  * @ds: the dock station
475  *
476  * Sometimes while docking, false dock events can be sent to the driver
477  * because good connections aren't made or some other reason.  Ignore these
478  * if we are in the middle of doing something.
479  */
480 static int dock_in_progress(struct dock_station *ds)
481 {
482         if ((ds->flags & DOCK_DOCKING) ||
483             time_before(jiffies, (ds->last_dock_time + HZ)))
484                 return 1;
485         return 0;
486 }
487
488 /**
489  * register_dock_notifier - add yourself to the dock notifier list
490  * @nb: the callers notifier block
491  *
492  * If a driver wishes to be notified about dock events, they can
493  * use this function to put a notifier block on the dock notifier list.
494  * this notifier call chain will be called after a dock event, but
495  * before hotplugging any new devices.
496  */
497 int register_dock_notifier(struct notifier_block *nb)
498 {
499         if (!dock_station_count)
500                 return -ENODEV;
501
502         return atomic_notifier_chain_register(&dock_notifier_list, nb);
503 }
504 EXPORT_SYMBOL_GPL(register_dock_notifier);
505
506 /**
507  * unregister_dock_notifier - remove yourself from the dock notifier list
508  * @nb: the callers notifier block
509  */
510 void unregister_dock_notifier(struct notifier_block *nb)
511 {
512         if (!dock_station_count)
513                 return;
514
515         atomic_notifier_chain_unregister(&dock_notifier_list, nb);
516 }
517 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
518
519 /**
520  * register_hotplug_dock_device - register a hotplug function
521  * @handle: the handle of the device
522  * @ops: handlers to call after docking
523  * @context: device specific data
524  * @init: Optional initialization routine to run after registration
525  * @release: Optional release routine to run on unregistration
526  *
527  * If a driver would like to perform a hotplug operation after a dock
528  * event, they can register an acpi_notifiy_handler to be called by
529  * the dock driver after _DCK is executed.
530  */
531 int register_hotplug_dock_device(acpi_handle handle,
532                                  const struct acpi_dock_ops *ops, void *context,
533                                  void (*init)(void *), void (*release)(void *))
534 {
535         struct dock_dependent_device *dd;
536         struct dock_station *dock_station;
537         int ret = -EINVAL;
538
539         if (WARN_ON(!context))
540                 return -EINVAL;
541
542         if (!dock_station_count)
543                 return -ENODEV;
544
545         /*
546          * make sure this handle is for a device dependent on the dock,
547          * this would include the dock station itself
548          */
549         list_for_each_entry(dock_station, &dock_stations, sibling) {
550                 /*
551                  * An ATA bay can be in a dock and itself can be ejected
552                  * separately, so there are two 'dock stations' which need the
553                  * ops
554                  */
555                 dd = find_dock_dependent_device(dock_station, handle);
556                 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
557                         ret = 0;
558         }
559
560         return ret;
561 }
562 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
563
564 /**
565  * unregister_hotplug_dock_device - remove yourself from the hotplug list
566  * @handle: the acpi handle of the device
567  */
568 void unregister_hotplug_dock_device(acpi_handle handle)
569 {
570         struct dock_dependent_device *dd;
571         struct dock_station *dock_station;
572
573         if (!dock_station_count)
574                 return;
575
576         list_for_each_entry(dock_station, &dock_stations, sibling) {
577                 dd = find_dock_dependent_device(dock_station, handle);
578                 if (dd)
579                         dock_release_hotplug(dd);
580         }
581 }
582 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
583
584 /**
585  * handle_eject_request - handle an undock request checking for error conditions
586  *
587  * Check to make sure the dock device is still present, then undock and
588  * hotremove all the devices that may need removing.
589  */
590 static int handle_eject_request(struct dock_station *ds, u32 event)
591 {
592         if (dock_in_progress(ds))
593                 return -EBUSY;
594
595         /*
596          * here we need to generate the undock
597          * event prior to actually doing the undock
598          * so that the device struct still exists.
599          * Also, even send the dock event if the
600          * device is not present anymore
601          */
602         dock_event(ds, event, UNDOCK_EVENT);
603
604         hot_remove_dock_devices(ds);
605         undock(ds);
606         acpi_evaluate_lck(ds->handle, 0);
607         acpi_evaluate_ej0(ds->handle);
608         if (dock_present(ds)) {
609                 acpi_handle_err(ds->handle, "Unable to undock!\n");
610                 return -EBUSY;
611         }
612         complete_undock(ds);
613         return 0;
614 }
615
616 /**
617  * dock_notify - act upon an acpi dock notification
618  * @handle: the dock station handle
619  * @event: the acpi event
620  * @data: our driver data struct
621  *
622  * If we are notified to dock, then check to see if the dock is
623  * present and then dock.  Notify all drivers of the dock event,
624  * and then hotplug and devices that may need hotplugging.
625  */
626 static void dock_notify(acpi_handle handle, u32 event, void *data)
627 {
628         struct dock_station *ds = data;
629         struct acpi_device *tmp;
630         int surprise_removal = 0;
631
632         /*
633          * According to acpi spec 3.0a, if a DEVICE_CHECK notification
634          * is sent and _DCK is present, it is assumed to mean an undock
635          * request.
636          */
637         if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
638                 event = ACPI_NOTIFY_EJECT_REQUEST;
639
640         /*
641          * dock station: BUS_CHECK - docked or surprise removal
642          *               DEVICE_CHECK - undocked
643          * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
644          *
645          * To simplify event handling, dock dependent device handler always
646          * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
647          * ACPI_NOTIFY_EJECT_REQUEST for removal
648          */
649         switch (event) {
650         case ACPI_NOTIFY_BUS_CHECK:
651         case ACPI_NOTIFY_DEVICE_CHECK:
652                 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
653                    &tmp)) {
654                         begin_dock(ds);
655                         dock(ds);
656                         if (!dock_present(ds)) {
657                                 acpi_handle_err(handle, "Unable to dock!\n");
658                                 complete_dock(ds);
659                                 break;
660                         }
661                         atomic_notifier_call_chain(&dock_notifier_list,
662                                                    event, NULL);
663                         hotplug_dock_devices(ds, event);
664                         complete_dock(ds);
665                         dock_event(ds, event, DOCK_EVENT);
666                         acpi_evaluate_lck(ds->handle, 1);
667                         acpi_update_all_gpes();
668                         break;
669                 }
670                 if (dock_present(ds) || dock_in_progress(ds))
671                         break;
672                 /* This is a surprise removal */
673                 surprise_removal = 1;
674                 event = ACPI_NOTIFY_EJECT_REQUEST;
675                 /* Fall back */
676         case ACPI_NOTIFY_EJECT_REQUEST:
677                 begin_undock(ds);
678                 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
679                    || surprise_removal)
680                         handle_eject_request(ds, event);
681                 else
682                         dock_event(ds, event, UNDOCK_EVENT);
683                 break;
684         default:
685                 acpi_handle_err(handle, "Unknown dock event %d\n", event);
686         }
687 }
688
689 struct dock_data {
690         acpi_handle handle;
691         unsigned long event;
692         struct dock_station *ds;
693 };
694
695 static void acpi_dock_deferred_cb(void *context)
696 {
697         struct dock_data *data = context;
698
699         acpi_scan_lock_acquire();
700         dock_notify(data->handle, data->event, data->ds);
701         acpi_scan_lock_release();
702         kfree(data);
703 }
704
705 static int acpi_dock_notifier_call(struct notifier_block *this,
706         unsigned long event, void *data)
707 {
708         struct dock_station *dock_station;
709         acpi_handle handle = data;
710
711         if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
712            && event != ACPI_NOTIFY_EJECT_REQUEST)
713                 return 0;
714
715         acpi_scan_lock_acquire();
716
717         list_for_each_entry(dock_station, &dock_stations, sibling) {
718                 if (dock_station->handle == handle) {
719                         struct dock_data *dd;
720                         acpi_status status;
721
722                         dd = kmalloc(sizeof(*dd), GFP_KERNEL);
723                         if (!dd)
724                                 break;
725
726                         dd->handle = handle;
727                         dd->event = event;
728                         dd->ds = dock_station;
729                         status = acpi_os_hotplug_execute(acpi_dock_deferred_cb,
730                                                          dd);
731                         if (ACPI_FAILURE(status))
732                                 kfree(dd);
733
734                         break;
735                 }
736         }
737
738         acpi_scan_lock_release();
739         return 0;
740 }
741
742 static struct notifier_block dock_acpi_notifier = {
743         .notifier_call = acpi_dock_notifier_call,
744 };
745
746 /**
747  * find_dock_devices - find devices on the dock station
748  * @handle: the handle of the device we are examining
749  * @lvl: unused
750  * @context: the dock station private data
751  * @rv: unused
752  *
753  * This function is called by acpi_walk_namespace.  It will
754  * check to see if an object has an _EJD method.  If it does, then it
755  * will see if it is dependent on the dock station.
756  */
757 static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
758                                             void *context, void **rv)
759 {
760         struct dock_station *ds = context;
761         acpi_handle ejd = NULL;
762
763         acpi_bus_get_ejd(handle, &ejd);
764         if (ejd == ds->handle)
765                 add_dock_dependent_device(ds, handle);
766
767         return AE_OK;
768 }
769
770 /*
771  * show_docked - read method for "docked" file in sysfs
772  */
773 static ssize_t show_docked(struct device *dev,
774                            struct device_attribute *attr, char *buf)
775 {
776         struct acpi_device *tmp;
777
778         struct dock_station *dock_station = dev->platform_data;
779
780         if (!acpi_bus_get_device(dock_station->handle, &tmp))
781                 return snprintf(buf, PAGE_SIZE, "1\n");
782         return snprintf(buf, PAGE_SIZE, "0\n");
783 }
784 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
785
786 /*
787  * show_flags - read method for flags file in sysfs
788  */
789 static ssize_t show_flags(struct device *dev,
790                           struct device_attribute *attr, char *buf)
791 {
792         struct dock_station *dock_station = dev->platform_data;
793         return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
794
795 }
796 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
797
798 /*
799  * write_undock - write method for "undock" file in sysfs
800  */
801 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
802                            const char *buf, size_t count)
803 {
804         int ret;
805         struct dock_station *dock_station = dev->platform_data;
806
807         if (!count)
808                 return -EINVAL;
809
810         acpi_scan_lock_acquire();
811         begin_undock(dock_station);
812         ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
813         acpi_scan_lock_release();
814         return ret ? ret: count;
815 }
816 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
817
818 /*
819  * show_dock_uid - read method for "uid" file in sysfs
820  */
821 static ssize_t show_dock_uid(struct device *dev,
822                              struct device_attribute *attr, char *buf)
823 {
824         unsigned long long lbuf;
825         struct dock_station *dock_station = dev->platform_data;
826         acpi_status status = acpi_evaluate_integer(dock_station->handle,
827                                         "_UID", NULL, &lbuf);
828         if (ACPI_FAILURE(status))
829             return 0;
830
831         return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
832 }
833 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
834
835 static ssize_t show_dock_type(struct device *dev,
836                 struct device_attribute *attr, char *buf)
837 {
838         struct dock_station *dock_station = dev->platform_data;
839         char *type;
840
841         if (dock_station->flags & DOCK_IS_DOCK)
842                 type = "dock_station";
843         else if (dock_station->flags & DOCK_IS_ATA)
844                 type = "ata_bay";
845         else if (dock_station->flags & DOCK_IS_BAT)
846                 type = "battery_bay";
847         else
848                 type = "unknown";
849
850         return snprintf(buf, PAGE_SIZE, "%s\n", type);
851 }
852 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
853
854 static struct attribute *dock_attributes[] = {
855         &dev_attr_docked.attr,
856         &dev_attr_flags.attr,
857         &dev_attr_undock.attr,
858         &dev_attr_uid.attr,
859         &dev_attr_type.attr,
860         NULL
861 };
862
863 static struct attribute_group dock_attribute_group = {
864         .attrs = dock_attributes
865 };
866
867 /**
868  * dock_add - add a new dock station
869  * @handle: the dock station handle
870  *
871  * allocated and initialize a new dock station device.  Find all devices
872  * that are on the dock station, and register for dock event notifications.
873  */
874 static int __init dock_add(acpi_handle handle)
875 {
876         int ret, id;
877         struct dock_station ds, *dock_station;
878         struct platform_device *dd;
879
880         id = dock_station_count;
881         memset(&ds, 0, sizeof(ds));
882         dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
883         if (IS_ERR(dd))
884                 return PTR_ERR(dd);
885
886         dock_station = dd->dev.platform_data;
887
888         dock_station->handle = handle;
889         dock_station->dock_device = dd;
890         dock_station->last_dock_time = jiffies - HZ;
891
892         INIT_LIST_HEAD(&dock_station->sibling);
893         INIT_LIST_HEAD(&dock_station->dependent_devices);
894
895         /* we want the dock device to send uevents */
896         dev_set_uevent_suppress(&dd->dev, 0);
897
898         if (acpi_dock_match(handle))
899                 dock_station->flags |= DOCK_IS_DOCK;
900         if (acpi_ata_match(handle))
901                 dock_station->flags |= DOCK_IS_ATA;
902         if (is_battery(handle))
903                 dock_station->flags |= DOCK_IS_BAT;
904
905         ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
906         if (ret)
907                 goto err_unregister;
908
909         /* Find dependent devices */
910         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
911                             ACPI_UINT32_MAX, find_dock_devices, NULL,
912                             dock_station, NULL);
913
914         /* add the dock station as a device dependent on itself */
915         ret = add_dock_dependent_device(dock_station, handle);
916         if (ret)
917                 goto err_rmgroup;
918
919         dock_station_count++;
920         list_add(&dock_station->sibling, &dock_stations);
921         return 0;
922
923 err_rmgroup:
924         sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
925 err_unregister:
926         platform_device_unregister(dd);
927         acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
928         return ret;
929 }
930
931 /**
932  * find_dock_and_bay - look for dock stations and bays
933  * @handle: acpi handle of a device
934  * @lvl: unused
935  * @context: unused
936  * @rv: unused
937  *
938  * This is called by acpi_walk_namespace to look for dock stations and bays.
939  */
940 static __init acpi_status
941 find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
942 {
943         if (acpi_dock_match(handle) || is_ejectable_bay(handle))
944                 dock_add(handle);
945
946         return AE_OK;
947 }
948
949 void __init acpi_dock_init(void)
950 {
951         if (acpi_disabled)
952                 return;
953
954         /* look for dock stations and bays */
955         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
956                 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
957
958         if (!dock_station_count) {
959                 pr_info(PREFIX "No dock devices found.\n");
960                 return;
961         }
962
963         ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
964         register_acpi_bus_notifier(&dock_acpi_notifier);
965         pr_info(PREFIX "%s: %d docks/bays found\n",
966                 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
967 }