]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/acpi/dock.c
7c86d01346e6d021ad9f385a74087cfe669e7074
[karo-tx-linux.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  * hotplug_dock_devices - insert or remove devices on the dock station
341  * @ds: the dock station
342  * @event: either bus check or eject request
343  *
344  * Some devices on the dock station need to have drivers called
345  * to perform hotplug operations after a dock event has occurred.
346  * Traverse the list of dock devices that have registered a
347  * hotplug handler, and call the handler.
348  */
349 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
350 {
351         struct dock_dependent_device *dd;
352
353         /*
354          * First call driver specific hotplug functions
355          */
356         list_for_each_entry(dd, &ds->dependent_devices, list)
357                 dock_hotplug_event(dd, event, false);
358
359         /*
360          * Now make sure that an acpi_device is created for each
361          * dependent device, or removed if this is an eject request.
362          * This will cause acpi_drivers to be stopped/started if they
363          * exist
364          */
365         list_for_each_entry(dd, &ds->dependent_devices, list) {
366                 if (event == ACPI_NOTIFY_EJECT_REQUEST)
367                         dock_remove_acpi_device(dd->handle);
368                 else
369                         dock_create_acpi_device(dd->handle);
370         }
371 }
372
373 static void dock_event(struct dock_station *ds, u32 event, int num)
374 {
375         struct device *dev = &ds->dock_device->dev;
376         char event_string[13];
377         char *envp[] = { event_string, NULL };
378         struct dock_dependent_device *dd;
379
380         if (num == UNDOCK_EVENT)
381                 sprintf(event_string, "EVENT=undock");
382         else
383                 sprintf(event_string, "EVENT=dock");
384
385         /*
386          * Indicate that the status of the dock station has
387          * changed.
388          */
389         if (num == DOCK_EVENT)
390                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
391
392         list_for_each_entry(dd, &ds->dependent_devices, list)
393                 dock_hotplug_event(dd, event, true);
394
395         if (num != DOCK_EVENT)
396                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
397 }
398
399 /**
400  * handle_dock - handle a dock event
401  * @ds: the dock station
402  * @dock: to dock, or undock - that is the question
403  *
404  * Execute the _DCK method in response to an acpi event
405  */
406 static void handle_dock(struct dock_station *ds, int dock)
407 {
408         acpi_status status;
409         struct acpi_object_list arg_list;
410         union acpi_object arg;
411         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
412
413         acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
414
415         /* _DCK method has one argument */
416         arg_list.count = 1;
417         arg_list.pointer = &arg;
418         arg.type = ACPI_TYPE_INTEGER;
419         arg.integer.value = dock;
420         status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
421         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
422                 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
423                                 status);
424
425         kfree(buffer.pointer);
426 }
427
428 static inline void dock(struct dock_station *ds)
429 {
430         handle_dock(ds, 1);
431 }
432
433 static inline void undock(struct dock_station *ds)
434 {
435         handle_dock(ds, 0);
436 }
437
438 static inline void begin_dock(struct dock_station *ds)
439 {
440         ds->flags |= DOCK_DOCKING;
441 }
442
443 static inline void complete_dock(struct dock_station *ds)
444 {
445         ds->flags &= ~(DOCK_DOCKING);
446         ds->last_dock_time = jiffies;
447 }
448
449 static inline void begin_undock(struct dock_station *ds)
450 {
451         ds->flags |= DOCK_UNDOCKING;
452 }
453
454 static inline void complete_undock(struct dock_station *ds)
455 {
456         ds->flags &= ~(DOCK_UNDOCKING);
457 }
458
459 /**
460  * dock_in_progress - see if we are in the middle of handling a dock event
461  * @ds: the dock station
462  *
463  * Sometimes while docking, false dock events can be sent to the driver
464  * because good connections aren't made or some other reason.  Ignore these
465  * if we are in the middle of doing something.
466  */
467 static int dock_in_progress(struct dock_station *ds)
468 {
469         if ((ds->flags & DOCK_DOCKING) ||
470             time_before(jiffies, (ds->last_dock_time + HZ)))
471                 return 1;
472         return 0;
473 }
474
475 /**
476  * register_dock_notifier - add yourself to the dock notifier list
477  * @nb: the callers notifier block
478  *
479  * If a driver wishes to be notified about dock events, they can
480  * use this function to put a notifier block on the dock notifier list.
481  * this notifier call chain will be called after a dock event, but
482  * before hotplugging any new devices.
483  */
484 int register_dock_notifier(struct notifier_block *nb)
485 {
486         if (!dock_station_count)
487                 return -ENODEV;
488
489         return atomic_notifier_chain_register(&dock_notifier_list, nb);
490 }
491 EXPORT_SYMBOL_GPL(register_dock_notifier);
492
493 /**
494  * unregister_dock_notifier - remove yourself from the dock notifier list
495  * @nb: the callers notifier block
496  */
497 void unregister_dock_notifier(struct notifier_block *nb)
498 {
499         if (!dock_station_count)
500                 return;
501
502         atomic_notifier_chain_unregister(&dock_notifier_list, nb);
503 }
504 EXPORT_SYMBOL_GPL(unregister_dock_notifier);
505
506 /**
507  * register_hotplug_dock_device - register a hotplug function
508  * @handle: the handle of the device
509  * @ops: handlers to call after docking
510  * @context: device specific data
511  * @init: Optional initialization routine to run after registration
512  * @release: Optional release routine to run on unregistration
513  *
514  * If a driver would like to perform a hotplug operation after a dock
515  * event, they can register an acpi_notifiy_handler to be called by
516  * the dock driver after _DCK is executed.
517  */
518 int register_hotplug_dock_device(acpi_handle handle,
519                                  const struct acpi_dock_ops *ops, void *context,
520                                  void (*init)(void *), void (*release)(void *))
521 {
522         struct dock_dependent_device *dd;
523         struct dock_station *dock_station;
524         int ret = -EINVAL;
525
526         if (WARN_ON(!context))
527                 return -EINVAL;
528
529         if (!dock_station_count)
530                 return -ENODEV;
531
532         /*
533          * make sure this handle is for a device dependent on the dock,
534          * this would include the dock station itself
535          */
536         list_for_each_entry(dock_station, &dock_stations, sibling) {
537                 /*
538                  * An ATA bay can be in a dock and itself can be ejected
539                  * separately, so there are two 'dock stations' which need the
540                  * ops
541                  */
542                 dd = find_dock_dependent_device(dock_station, handle);
543                 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
544                         ret = 0;
545         }
546
547         return ret;
548 }
549 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
550
551 /**
552  * unregister_hotplug_dock_device - remove yourself from the hotplug list
553  * @handle: the acpi handle of the device
554  */
555 void unregister_hotplug_dock_device(acpi_handle handle)
556 {
557         struct dock_dependent_device *dd;
558         struct dock_station *dock_station;
559
560         if (!dock_station_count)
561                 return;
562
563         list_for_each_entry(dock_station, &dock_stations, sibling) {
564                 dd = find_dock_dependent_device(dock_station, handle);
565                 if (dd)
566                         dock_release_hotplug(dd);
567         }
568 }
569 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
570
571 /**
572  * handle_eject_request - handle an undock request checking for error conditions
573  *
574  * Check to make sure the dock device is still present, then undock and
575  * hotremove all the devices that may need removing.
576  */
577 static int handle_eject_request(struct dock_station *ds, u32 event)
578 {
579         if (dock_in_progress(ds))
580                 return -EBUSY;
581
582         /*
583          * here we need to generate the undock
584          * event prior to actually doing the undock
585          * so that the device struct still exists.
586          * Also, even send the dock event if the
587          * device is not present anymore
588          */
589         dock_event(ds, event, UNDOCK_EVENT);
590
591         hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
592         undock(ds);
593         acpi_evaluate_lck(ds->handle, 0);
594         acpi_evaluate_ej0(ds->handle);
595         if (dock_present(ds)) {
596                 acpi_handle_err(ds->handle, "Unable to undock!\n");
597                 return -EBUSY;
598         }
599         complete_undock(ds);
600         return 0;
601 }
602
603 /**
604  * dock_notify - act upon an acpi dock notification
605  * @handle: the dock station handle
606  * @event: the acpi event
607  * @data: our driver data struct
608  *
609  * If we are notified to dock, then check to see if the dock is
610  * present and then dock.  Notify all drivers of the dock event,
611  * and then hotplug and devices that may need hotplugging.
612  */
613 static void dock_notify(acpi_handle handle, u32 event, void *data)
614 {
615         struct dock_station *ds = data;
616         struct acpi_device *tmp;
617         int surprise_removal = 0;
618
619         /*
620          * According to acpi spec 3.0a, if a DEVICE_CHECK notification
621          * is sent and _DCK is present, it is assumed to mean an undock
622          * request.
623          */
624         if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
625                 event = ACPI_NOTIFY_EJECT_REQUEST;
626
627         /*
628          * dock station: BUS_CHECK - docked or surprise removal
629          *               DEVICE_CHECK - undocked
630          * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
631          *
632          * To simplify event handling, dock dependent device handler always
633          * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
634          * ACPI_NOTIFY_EJECT_REQUEST for removal
635          */
636         switch (event) {
637         case ACPI_NOTIFY_BUS_CHECK:
638         case ACPI_NOTIFY_DEVICE_CHECK:
639                 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
640                    &tmp)) {
641                         begin_dock(ds);
642                         dock(ds);
643                         if (!dock_present(ds)) {
644                                 acpi_handle_err(handle, "Unable to dock!\n");
645                                 complete_dock(ds);
646                                 break;
647                         }
648                         atomic_notifier_call_chain(&dock_notifier_list,
649                                                    event, NULL);
650                         hotplug_dock_devices(ds, event);
651                         complete_dock(ds);
652                         dock_event(ds, event, DOCK_EVENT);
653                         acpi_evaluate_lck(ds->handle, 1);
654                         acpi_update_all_gpes();
655                         break;
656                 }
657                 if (dock_present(ds) || dock_in_progress(ds))
658                         break;
659                 /* This is a surprise removal */
660                 surprise_removal = 1;
661                 event = ACPI_NOTIFY_EJECT_REQUEST;
662                 /* Fall back */
663         case ACPI_NOTIFY_EJECT_REQUEST:
664                 begin_undock(ds);
665                 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
666                    || surprise_removal)
667                         handle_eject_request(ds, event);
668                 else
669                         dock_event(ds, event, UNDOCK_EVENT);
670                 break;
671         default:
672                 acpi_handle_err(handle, "Unknown dock event %d\n", event);
673         }
674 }
675
676 struct dock_data {
677         acpi_handle handle;
678         unsigned long event;
679         struct dock_station *ds;
680 };
681
682 static void acpi_dock_deferred_cb(void *context)
683 {
684         struct dock_data *data = context;
685
686         acpi_scan_lock_acquire();
687         dock_notify(data->handle, data->event, data->ds);
688         acpi_scan_lock_release();
689         kfree(data);
690 }
691
692 static int acpi_dock_notifier_call(struct notifier_block *this,
693         unsigned long event, void *data)
694 {
695         struct dock_station *dock_station;
696         acpi_handle handle = data;
697
698         if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
699            && event != ACPI_NOTIFY_EJECT_REQUEST)
700                 return 0;
701
702         acpi_scan_lock_acquire();
703
704         list_for_each_entry(dock_station, &dock_stations, sibling) {
705                 if (dock_station->handle == handle) {
706                         struct dock_data *dd;
707                         acpi_status status;
708
709                         dd = kmalloc(sizeof(*dd), GFP_KERNEL);
710                         if (!dd)
711                                 break;
712
713                         dd->handle = handle;
714                         dd->event = event;
715                         dd->ds = dock_station;
716                         status = acpi_os_hotplug_execute(acpi_dock_deferred_cb,
717                                                          dd);
718                         if (ACPI_FAILURE(status))
719                                 kfree(dd);
720
721                         break;
722                 }
723         }
724
725         acpi_scan_lock_release();
726         return 0;
727 }
728
729 static struct notifier_block dock_acpi_notifier = {
730         .notifier_call = acpi_dock_notifier_call,
731 };
732
733 /**
734  * find_dock_devices - find devices on the dock station
735  * @handle: the handle of the device we are examining
736  * @lvl: unused
737  * @context: the dock station private data
738  * @rv: unused
739  *
740  * This function is called by acpi_walk_namespace.  It will
741  * check to see if an object has an _EJD method.  If it does, then it
742  * will see if it is dependent on the dock station.
743  */
744 static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
745                                             void *context, void **rv)
746 {
747         struct dock_station *ds = context;
748         acpi_handle ejd = NULL;
749
750         acpi_bus_get_ejd(handle, &ejd);
751         if (ejd == ds->handle)
752                 add_dock_dependent_device(ds, handle);
753
754         return AE_OK;
755 }
756
757 /*
758  * show_docked - read method for "docked" file in sysfs
759  */
760 static ssize_t show_docked(struct device *dev,
761                            struct device_attribute *attr, char *buf)
762 {
763         struct acpi_device *tmp;
764
765         struct dock_station *dock_station = dev->platform_data;
766
767         if (!acpi_bus_get_device(dock_station->handle, &tmp))
768                 return snprintf(buf, PAGE_SIZE, "1\n");
769         return snprintf(buf, PAGE_SIZE, "0\n");
770 }
771 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
772
773 /*
774  * show_flags - read method for flags file in sysfs
775  */
776 static ssize_t show_flags(struct device *dev,
777                           struct device_attribute *attr, char *buf)
778 {
779         struct dock_station *dock_station = dev->platform_data;
780         return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
781
782 }
783 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
784
785 /*
786  * write_undock - write method for "undock" file in sysfs
787  */
788 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
789                            const char *buf, size_t count)
790 {
791         int ret;
792         struct dock_station *dock_station = dev->platform_data;
793
794         if (!count)
795                 return -EINVAL;
796
797         acpi_scan_lock_acquire();
798         begin_undock(dock_station);
799         ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
800         acpi_scan_lock_release();
801         return ret ? ret: count;
802 }
803 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
804
805 /*
806  * show_dock_uid - read method for "uid" file in sysfs
807  */
808 static ssize_t show_dock_uid(struct device *dev,
809                              struct device_attribute *attr, char *buf)
810 {
811         unsigned long long lbuf;
812         struct dock_station *dock_station = dev->platform_data;
813         acpi_status status = acpi_evaluate_integer(dock_station->handle,
814                                         "_UID", NULL, &lbuf);
815         if (ACPI_FAILURE(status))
816             return 0;
817
818         return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
819 }
820 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
821
822 static ssize_t show_dock_type(struct device *dev,
823                 struct device_attribute *attr, char *buf)
824 {
825         struct dock_station *dock_station = dev->platform_data;
826         char *type;
827
828         if (dock_station->flags & DOCK_IS_DOCK)
829                 type = "dock_station";
830         else if (dock_station->flags & DOCK_IS_ATA)
831                 type = "ata_bay";
832         else if (dock_station->flags & DOCK_IS_BAT)
833                 type = "battery_bay";
834         else
835                 type = "unknown";
836
837         return snprintf(buf, PAGE_SIZE, "%s\n", type);
838 }
839 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
840
841 static struct attribute *dock_attributes[] = {
842         &dev_attr_docked.attr,
843         &dev_attr_flags.attr,
844         &dev_attr_undock.attr,
845         &dev_attr_uid.attr,
846         &dev_attr_type.attr,
847         NULL
848 };
849
850 static struct attribute_group dock_attribute_group = {
851         .attrs = dock_attributes
852 };
853
854 /**
855  * dock_add - add a new dock station
856  * @handle: the dock station handle
857  *
858  * allocated and initialize a new dock station device.  Find all devices
859  * that are on the dock station, and register for dock event notifications.
860  */
861 static int __init dock_add(acpi_handle handle)
862 {
863         int ret, id;
864         struct dock_station ds, *dock_station;
865         struct platform_device *dd;
866
867         id = dock_station_count;
868         memset(&ds, 0, sizeof(ds));
869         dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
870         if (IS_ERR(dd))
871                 return PTR_ERR(dd);
872
873         dock_station = dd->dev.platform_data;
874
875         dock_station->handle = handle;
876         dock_station->dock_device = dd;
877         dock_station->last_dock_time = jiffies - HZ;
878
879         INIT_LIST_HEAD(&dock_station->sibling);
880         INIT_LIST_HEAD(&dock_station->dependent_devices);
881
882         /* we want the dock device to send uevents */
883         dev_set_uevent_suppress(&dd->dev, 0);
884
885         if (acpi_dock_match(handle))
886                 dock_station->flags |= DOCK_IS_DOCK;
887         if (acpi_ata_match(handle))
888                 dock_station->flags |= DOCK_IS_ATA;
889         if (is_battery(handle))
890                 dock_station->flags |= DOCK_IS_BAT;
891
892         ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
893         if (ret)
894                 goto err_unregister;
895
896         /* Find dependent devices */
897         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
898                             ACPI_UINT32_MAX, find_dock_devices, NULL,
899                             dock_station, NULL);
900
901         /* add the dock station as a device dependent on itself */
902         ret = add_dock_dependent_device(dock_station, handle);
903         if (ret)
904                 goto err_rmgroup;
905
906         dock_station_count++;
907         list_add(&dock_station->sibling, &dock_stations);
908         return 0;
909
910 err_rmgroup:
911         sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
912 err_unregister:
913         platform_device_unregister(dd);
914         acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
915         return ret;
916 }
917
918 /**
919  * find_dock_and_bay - look for dock stations and bays
920  * @handle: acpi handle of a device
921  * @lvl: unused
922  * @context: unused
923  * @rv: unused
924  *
925  * This is called by acpi_walk_namespace to look for dock stations and bays.
926  */
927 static __init acpi_status
928 find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
929 {
930         if (acpi_dock_match(handle) || is_ejectable_bay(handle))
931                 dock_add(handle);
932
933         return AE_OK;
934 }
935
936 void __init acpi_dock_init(void)
937 {
938         if (acpi_disabled)
939                 return;
940
941         /* look for dock stations and bays */
942         acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
943                 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
944
945         if (!dock_station_count) {
946                 pr_info(PREFIX "No dock devices found.\n");
947                 return;
948         }
949
950         ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
951         register_acpi_bus_notifier(&dock_acpi_notifier);
952         pr_info(PREFIX "%s: %d docks/bays found\n",
953                 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
954 }