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