Merge branch 'pm-cpufreq'
[pandora-kernel.git] / drivers / acpi / dock.c
1 /*
2  *  dock.c - ACPI dock station driver
3  *
4  *  Copyright (C) 2006, 2014, Intel Corp.
5  *  Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
6  *          Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  */
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/notifier.h>
33 #include <linux/platform_device.h>
34 #include <linux/jiffies.h>
35 #include <linux/stddef.h>
36 #include <linux/acpi.h>
37
38 #include "internal.h"
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
72 struct dock_dependent_device {
73         struct list_head list;
74         struct acpi_device *adev;
75 };
76
77 #define DOCK_DOCKING    0x00000001
78 #define DOCK_UNDOCKING  0x00000002
79 #define DOCK_IS_DOCK    0x00000010
80 #define DOCK_IS_ATA     0x00000020
81 #define DOCK_IS_BAT     0x00000040
82 #define DOCK_EVENT      3
83 #define UNDOCK_EVENT    2
84
85 enum dock_callback_type {
86         DOCK_CALL_HANDLER,
87         DOCK_CALL_FIXUP,
88         DOCK_CALL_UEVENT,
89 };
90
91 /*****************************************************************************
92  *                         Dock Dependent device functions                   *
93  *****************************************************************************/
94 /**
95  * add_dock_dependent_device - associate a device with the dock station
96  * @ds: Dock station.
97  * @adev: Dependent ACPI device object.
98  *
99  * Add the dependent device to the dock's dependent device list.
100  */
101 static int add_dock_dependent_device(struct dock_station *ds,
102                                      struct acpi_device *adev)
103 {
104         struct dock_dependent_device *dd;
105
106         dd = kzalloc(sizeof(*dd), GFP_KERNEL);
107         if (!dd)
108                 return -ENOMEM;
109
110         dd->adev = adev;
111         INIT_LIST_HEAD(&dd->list);
112         list_add_tail(&dd->list, &ds->dependent_devices);
113
114         return 0;
115 }
116
117 static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
118                                enum dock_callback_type cb_type)
119 {
120         struct acpi_device *adev = dd->adev;
121
122         acpi_lock_hp_context();
123
124         if (!adev->hp)
125                 goto out;
126
127         if (cb_type == DOCK_CALL_FIXUP) {
128                 void (*fixup)(struct acpi_device *);
129
130                 fixup = adev->hp->fixup;
131                 if (fixup) {
132                         acpi_unlock_hp_context();
133                         fixup(adev);
134                         return;
135                 }
136         } else if (cb_type == DOCK_CALL_UEVENT) {
137                 void (*uevent)(struct acpi_device *, u32);
138
139                 uevent = adev->hp->uevent;
140                 if (uevent) {
141                         acpi_unlock_hp_context();
142                         uevent(adev, event);
143                         return;
144                 }
145         } else {
146                 int (*notify)(struct acpi_device *, u32);
147
148                 notify = adev->hp->notify;
149                 if (notify) {
150                         acpi_unlock_hp_context();
151                         notify(adev, event);
152                         return;
153                 }
154         }
155
156  out:
157         acpi_unlock_hp_context();
158 }
159
160 static struct dock_station *find_dock_station(acpi_handle handle)
161 {
162         struct dock_station *ds;
163
164         list_for_each_entry(ds, &dock_stations, sibling)
165                 if (ds->handle == handle)
166                         return ds;
167
168         return NULL;
169 }
170
171 /**
172  * find_dock_dependent_device - get a device dependent on this dock
173  * @ds: the dock station
174  * @adev: ACPI device object to find.
175  *
176  * iterate over the dependent device list for this dock.  If the
177  * dependent device matches the handle, return.
178  */
179 static struct dock_dependent_device *
180 find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
181 {
182         struct dock_dependent_device *dd;
183
184         list_for_each_entry(dd, &ds->dependent_devices, list)
185                 if (adev == dd->adev)
186                         return dd;
187
188         return NULL;
189 }
190
191 void register_dock_dependent_device(struct acpi_device *adev,
192                                     acpi_handle dshandle)
193 {
194         struct dock_station *ds = find_dock_station(dshandle);
195
196         if (ds && !find_dock_dependent_device(ds, adev))
197                 add_dock_dependent_device(ds, adev);
198 }
199
200 /*****************************************************************************
201  *                         Dock functions                                    *
202  *****************************************************************************/
203
204 /**
205  * is_dock_device - see if a device is on a dock station
206  * @adev: ACPI device object to check.
207  *
208  * If this device is either the dock station itself,
209  * or is a device dependent on the dock station, then it
210  * is a dock device
211  */
212 int is_dock_device(struct acpi_device *adev)
213 {
214         struct dock_station *dock_station;
215
216         if (!dock_station_count)
217                 return 0;
218
219         if (acpi_dock_match(adev->handle))
220                 return 1;
221
222         list_for_each_entry(dock_station, &dock_stations, sibling)
223                 if (find_dock_dependent_device(dock_station, adev))
224                         return 1;
225
226         return 0;
227 }
228 EXPORT_SYMBOL_GPL(is_dock_device);
229
230 /**
231  * dock_present - see if the dock station is present.
232  * @ds: the dock station
233  *
234  * execute the _STA method.  note that present does not
235  * imply that we are docked.
236  */
237 static int dock_present(struct dock_station *ds)
238 {
239         unsigned long long sta;
240         acpi_status status;
241
242         if (ds) {
243                 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
244                 if (ACPI_SUCCESS(status) && sta)
245                         return 1;
246         }
247         return 0;
248 }
249
250 /**
251  * hot_remove_dock_devices - Remove dock station devices.
252  * @ds: Dock station.
253  */
254 static void hot_remove_dock_devices(struct dock_station *ds)
255 {
256         struct dock_dependent_device *dd;
257
258         /*
259          * Walk the list in reverse order so that devices that have been added
260          * last are removed first (in case there are some indirect dependencies
261          * between them).
262          */
263         list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
264                 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
265
266         list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
267                 acpi_bus_trim(dd->adev);
268 }
269
270 /**
271  * hotplug_dock_devices - Insert devices on a dock station.
272  * @ds: the dock station
273  * @event: either bus check or device check request
274  *
275  * Some devices on the dock station need to have drivers called
276  * to perform hotplug operations after a dock event has occurred.
277  * Traverse the list of dock devices that have registered a
278  * hotplug handler, and call the handler.
279  */
280 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
281 {
282         struct dock_dependent_device *dd;
283
284         /* Call driver specific post-dock fixups. */
285         list_for_each_entry(dd, &ds->dependent_devices, list)
286                 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
287
288         /* Call driver specific hotplug functions. */
289         list_for_each_entry(dd, &ds->dependent_devices, list)
290                 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
291
292         /*
293          * Check if all devices have been enumerated already.  If not, run
294          * acpi_bus_scan() for them and that will cause scan handlers to be
295          * attached to device objects or acpi_drivers to be stopped/started if
296          * they are present.
297          */
298         list_for_each_entry(dd, &ds->dependent_devices, list) {
299                 struct acpi_device *adev = dd->adev;
300
301                 if (!acpi_device_enumerated(adev)) {
302                         int ret = acpi_bus_scan(adev->handle);
303                         if (ret)
304                                 dev_dbg(&adev->dev, "scan error %d\n", -ret);
305                 }
306         }
307 }
308
309 static void dock_event(struct dock_station *ds, u32 event, int num)
310 {
311         struct device *dev = &ds->dock_device->dev;
312         char event_string[13];
313         char *envp[] = { event_string, NULL };
314         struct dock_dependent_device *dd;
315
316         if (num == UNDOCK_EVENT)
317                 sprintf(event_string, "EVENT=undock");
318         else
319                 sprintf(event_string, "EVENT=dock");
320
321         /*
322          * Indicate that the status of the dock station has
323          * changed.
324          */
325         if (num == DOCK_EVENT)
326                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
327
328         list_for_each_entry(dd, &ds->dependent_devices, list)
329                 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
330
331         if (num != DOCK_EVENT)
332                 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
333 }
334
335 /**
336  * handle_dock - handle a dock event
337  * @ds: the dock station
338  * @dock: to dock, or undock - that is the question
339  *
340  * Execute the _DCK method in response to an acpi event
341  */
342 static void handle_dock(struct dock_station *ds, int dock)
343 {
344         acpi_status status;
345         struct acpi_object_list arg_list;
346         union acpi_object arg;
347         unsigned long long value;
348
349         acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
350
351         /* _DCK method has one argument */
352         arg_list.count = 1;
353         arg_list.pointer = &arg;
354         arg.type = ACPI_TYPE_INTEGER;
355         arg.integer.value = dock;
356         status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
357         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
358                 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
359                                 status);
360 }
361
362 static inline void dock(struct dock_station *ds)
363 {
364         handle_dock(ds, 1);
365 }
366
367 static inline void undock(struct dock_station *ds)
368 {
369         handle_dock(ds, 0);
370 }
371
372 static inline void begin_dock(struct dock_station *ds)
373 {
374         ds->flags |= DOCK_DOCKING;
375 }
376
377 static inline void complete_dock(struct dock_station *ds)
378 {
379         ds->flags &= ~(DOCK_DOCKING);
380         ds->last_dock_time = jiffies;
381 }
382
383 static inline void begin_undock(struct dock_station *ds)
384 {
385         ds->flags |= DOCK_UNDOCKING;
386 }
387
388 static inline void complete_undock(struct dock_station *ds)
389 {
390         ds->flags &= ~(DOCK_UNDOCKING);
391 }
392
393 /**
394  * dock_in_progress - see if we are in the middle of handling a dock event
395  * @ds: the dock station
396  *
397  * Sometimes while docking, false dock events can be sent to the driver
398  * because good connections aren't made or some other reason.  Ignore these
399  * if we are in the middle of doing something.
400  */
401 static int dock_in_progress(struct dock_station *ds)
402 {
403         if ((ds->flags & DOCK_DOCKING) ||
404             time_before(jiffies, (ds->last_dock_time + HZ)))
405                 return 1;
406         return 0;
407 }
408
409 /**
410  * handle_eject_request - handle an undock request checking for error conditions
411  *
412  * Check to make sure the dock device is still present, then undock and
413  * hotremove all the devices that may need removing.
414  */
415 static int handle_eject_request(struct dock_station *ds, u32 event)
416 {
417         if (dock_in_progress(ds))
418                 return -EBUSY;
419
420         /*
421          * here we need to generate the undock
422          * event prior to actually doing the undock
423          * so that the device struct still exists.
424          * Also, even send the dock event if the
425          * device is not present anymore
426          */
427         dock_event(ds, event, UNDOCK_EVENT);
428
429         hot_remove_dock_devices(ds);
430         undock(ds);
431         acpi_evaluate_lck(ds->handle, 0);
432         acpi_evaluate_ej0(ds->handle);
433         if (dock_present(ds)) {
434                 acpi_handle_err(ds->handle, "Unable to undock!\n");
435                 return -EBUSY;
436         }
437         complete_undock(ds);
438         return 0;
439 }
440
441 /**
442  * dock_notify - Handle ACPI dock notification.
443  * @adev: Dock station's ACPI device object.
444  * @event: Event code.
445  *
446  * If we are notified to dock, then check to see if the dock is
447  * present and then dock.  Notify all drivers of the dock event,
448  * and then hotplug and devices that may need hotplugging.
449  */
450 int dock_notify(struct acpi_device *adev, u32 event)
451 {
452         acpi_handle handle = adev->handle;
453         struct dock_station *ds = find_dock_station(handle);
454         int surprise_removal = 0;
455
456         if (!ds)
457                 return -ENODEV;
458
459         /*
460          * According to acpi spec 3.0a, if a DEVICE_CHECK notification
461          * is sent and _DCK is present, it is assumed to mean an undock
462          * request.
463          */
464         if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
465                 event = ACPI_NOTIFY_EJECT_REQUEST;
466
467         /*
468          * dock station: BUS_CHECK - docked or surprise removal
469          *               DEVICE_CHECK - undocked
470          * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
471          *
472          * To simplify event handling, dock dependent device handler always
473          * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
474          * ACPI_NOTIFY_EJECT_REQUEST for removal
475          */
476         switch (event) {
477         case ACPI_NOTIFY_BUS_CHECK:
478         case ACPI_NOTIFY_DEVICE_CHECK:
479                 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
480                         begin_dock(ds);
481                         dock(ds);
482                         if (!dock_present(ds)) {
483                                 acpi_handle_err(handle, "Unable to dock!\n");
484                                 complete_dock(ds);
485                                 break;
486                         }
487                         hotplug_dock_devices(ds, event);
488                         complete_dock(ds);
489                         dock_event(ds, event, DOCK_EVENT);
490                         acpi_evaluate_lck(ds->handle, 1);
491                         acpi_update_all_gpes();
492                         break;
493                 }
494                 if (dock_present(ds) || dock_in_progress(ds))
495                         break;
496                 /* This is a surprise removal */
497                 surprise_removal = 1;
498                 event = ACPI_NOTIFY_EJECT_REQUEST;
499                 /* Fall back */
500         case ACPI_NOTIFY_EJECT_REQUEST:
501                 begin_undock(ds);
502                 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
503                    || surprise_removal)
504                         handle_eject_request(ds, event);
505                 else
506                         dock_event(ds, event, UNDOCK_EVENT);
507                 break;
508         }
509         return 0;
510 }
511
512 /*
513  * show_docked - read method for "docked" file in sysfs
514  */
515 static ssize_t show_docked(struct device *dev,
516                            struct device_attribute *attr, char *buf)
517 {
518         struct dock_station *dock_station = dev->platform_data;
519         struct acpi_device *adev = NULL;
520
521         acpi_bus_get_device(dock_station->handle, &adev);
522         return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
523 }
524 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
525
526 /*
527  * show_flags - read method for flags file in sysfs
528  */
529 static ssize_t show_flags(struct device *dev,
530                           struct device_attribute *attr, char *buf)
531 {
532         struct dock_station *dock_station = dev->platform_data;
533         return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
534
535 }
536 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
537
538 /*
539  * write_undock - write method for "undock" file in sysfs
540  */
541 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
542                            const char *buf, size_t count)
543 {
544         int ret;
545         struct dock_station *dock_station = dev->platform_data;
546
547         if (!count)
548                 return -EINVAL;
549
550         acpi_scan_lock_acquire();
551         begin_undock(dock_station);
552         ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
553         acpi_scan_lock_release();
554         return ret ? ret: count;
555 }
556 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
557
558 /*
559  * show_dock_uid - read method for "uid" file in sysfs
560  */
561 static ssize_t show_dock_uid(struct device *dev,
562                              struct device_attribute *attr, char *buf)
563 {
564         unsigned long long lbuf;
565         struct dock_station *dock_station = dev->platform_data;
566         acpi_status status = acpi_evaluate_integer(dock_station->handle,
567                                         "_UID", NULL, &lbuf);
568         if (ACPI_FAILURE(status))
569             return 0;
570
571         return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
572 }
573 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
574
575 static ssize_t show_dock_type(struct device *dev,
576                 struct device_attribute *attr, char *buf)
577 {
578         struct dock_station *dock_station = dev->platform_data;
579         char *type;
580
581         if (dock_station->flags & DOCK_IS_DOCK)
582                 type = "dock_station";
583         else if (dock_station->flags & DOCK_IS_ATA)
584                 type = "ata_bay";
585         else if (dock_station->flags & DOCK_IS_BAT)
586                 type = "battery_bay";
587         else
588                 type = "unknown";
589
590         return snprintf(buf, PAGE_SIZE, "%s\n", type);
591 }
592 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
593
594 static struct attribute *dock_attributes[] = {
595         &dev_attr_docked.attr,
596         &dev_attr_flags.attr,
597         &dev_attr_undock.attr,
598         &dev_attr_uid.attr,
599         &dev_attr_type.attr,
600         NULL
601 };
602
603 static struct attribute_group dock_attribute_group = {
604         .attrs = dock_attributes
605 };
606
607 /**
608  * acpi_dock_add - Add a new dock station
609  * @adev: Dock station ACPI device object.
610  *
611  * allocated and initialize a new dock station device.
612  */
613 void acpi_dock_add(struct acpi_device *adev)
614 {
615         struct dock_station *dock_station, ds = { NULL, };
616         struct platform_device_info pdevinfo;
617         acpi_handle handle = adev->handle;
618         struct platform_device *dd;
619         int ret;
620
621         memset(&pdevinfo, 0, sizeof(pdevinfo));
622         pdevinfo.name = "dock";
623         pdevinfo.id = dock_station_count;
624         pdevinfo.acpi_node.companion = adev;
625         pdevinfo.data = &ds;
626         pdevinfo.size_data = sizeof(ds);
627         dd = platform_device_register_full(&pdevinfo);
628         if (IS_ERR(dd))
629                 return;
630
631         dock_station = dd->dev.platform_data;
632
633         dock_station->handle = handle;
634         dock_station->dock_device = dd;
635         dock_station->last_dock_time = jiffies - HZ;
636
637         INIT_LIST_HEAD(&dock_station->sibling);
638         INIT_LIST_HEAD(&dock_station->dependent_devices);
639
640         /* we want the dock device to send uevents */
641         dev_set_uevent_suppress(&dd->dev, 0);
642
643         if (acpi_dock_match(handle))
644                 dock_station->flags |= DOCK_IS_DOCK;
645         if (acpi_ata_match(handle))
646                 dock_station->flags |= DOCK_IS_ATA;
647         if (acpi_device_is_battery(adev))
648                 dock_station->flags |= DOCK_IS_BAT;
649
650         ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
651         if (ret)
652                 goto err_unregister;
653
654         /* add the dock station as a device dependent on itself */
655         ret = add_dock_dependent_device(dock_station, adev);
656         if (ret)
657                 goto err_rmgroup;
658
659         dock_station_count++;
660         list_add(&dock_station->sibling, &dock_stations);
661         adev->flags.is_dock_station = true;
662         dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
663                  dock_station_count);
664         return;
665
666 err_rmgroup:
667         sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
668
669 err_unregister:
670         platform_device_unregister(dd);
671         acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
672 }