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