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