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