ACPI / dock: Use callback pointers from devices' ACPI hotplug contexts
[deliverable/linux.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 35
3f9eed5c
R
36#include "internal.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
a340af14
FS
54static const struct acpi_device_id dock_device_ids[] = {
55 {"LNXDOCK", 0},
56 {"", 0},
57};
58MODULE_DEVICE_TABLE(acpi, dock_device_ids);
59
c8f7a62c
LB
60struct dock_station {
61 acpi_handle handle;
62 unsigned long last_dock_time;
63 u32 flags;
c8f7a62c 64 struct list_head dependent_devices;
db350b08 65
50d716e4 66 struct list_head sibling;
db350b08 67 struct platform_device *dock_device;
c8f7a62c 68};
db350b08
SL
69static LIST_HEAD(dock_stations);
70static int dock_station_count;
21a31013 71static DEFINE_MUTEX(hotplug_lock);
c8f7a62c
LB
72
73struct dock_dependent_device {
74 struct list_head list;
3b52b21f 75 struct acpi_device *adev;
21a31013
RW
76 const struct acpi_dock_ops *hp_ops;
77 void *hp_context;
78 unsigned int hp_refcount;
79 void (*hp_release)(void *);
c8f7a62c
LB
80};
81
82#define DOCK_DOCKING 0x00000001
a0cd35fd 83#define DOCK_UNDOCKING 0x00000002
db350b08
SL
84#define DOCK_IS_DOCK 0x00000010
85#define DOCK_IS_ATA 0x00000020
86#define DOCK_IS_BAT 0x00000040
5669021e
KCA
87#define DOCK_EVENT 3
88#define UNDOCK_EVENT 2
c8f7a62c 89
f09ce741
RW
90enum dock_callback_type {
91 DOCK_CALL_HANDLER,
92 DOCK_CALL_FIXUP,
93 DOCK_CALL_UEVENT,
94};
95
c8f7a62c
LB
96/*****************************************************************************
97 * Dock Dependent device functions *
98 *****************************************************************************/
99/**
f69cfdd2 100 * add_dock_dependent_device - associate a device with the dock station
3b52b21f
RW
101 * @ds: Dock station.
102 * @adev: Dependent ACPI device object.
c8f7a62c 103 *
f69cfdd2 104 * Add the dependent device to the dock's dependent device list.
c8f7a62c 105 */
3b52b21f
RW
106static int add_dock_dependent_device(struct dock_station *ds,
107 struct acpi_device *adev)
c8f7a62c
LB
108{
109 struct dock_dependent_device *dd;
110
111 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
f69cfdd2
AC
112 if (!dd)
113 return -ENOMEM;
114
3b52b21f 115 dd->adev = adev;
f69cfdd2 116 INIT_LIST_HEAD(&dd->list);
c8f7a62c 117 list_add_tail(&dd->list, &ds->dependent_devices);
f69cfdd2
AC
118
119 return 0;
c8f7a62c
LB
120}
121
a30c4c5e
RW
122static 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
c8f7a62c 132/**
21a31013
RW
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.
c8f7a62c 139 */
21a31013
RW
140static 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 *))
c8f7a62c 143{
21a31013
RW
144 int ret = 0;
145
146 mutex_lock(&hotplug_lock);
4ec24065 147 if (WARN_ON(dd->hp_context)) {
21a31013
RW
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;
4ec24065
RW
154 if (init)
155 init(context);
21a31013 156 }
21a31013
RW
157 mutex_unlock(&hotplug_lock);
158 return ret;
c8f7a62c
LB
159}
160
161/**
21a31013
RW
162 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
163 * @dd: Dock-dependent device.
c8f7a62c 164 *
21a31013
RW
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.
c8f7a62c 168 */
21a31013 169static void dock_release_hotplug(struct dock_dependent_device *dd)
c8f7a62c 170{
21a31013 171 mutex_lock(&hotplug_lock);
21a31013 172 if (dd->hp_context && !--dd->hp_refcount) {
4ec24065
RW
173 void (*release)(void *) = dd->hp_release;
174 void *context = dd->hp_context;
175
21a31013 176 dd->hp_ops = NULL;
21a31013 177 dd->hp_context = NULL;
21a31013 178 dd->hp_release = NULL;
4ec24065
RW
179 if (release)
180 release(context);
21a31013 181 }
21a31013
RW
182 mutex_unlock(&hotplug_lock);
183}
184
185static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
f09ce741 186 enum dock_callback_type cb_type)
21a31013 187{
edf5bf34 188 struct acpi_device *adev = dd->adev;
21a31013
RW
189 acpi_notify_handler cb = NULL;
190 bool run = false;
191
edf5bf34
RW
192 acpi_lock_hp_context();
193
194 if (!adev->hp)
195 goto no_context;
196
197 if (cb_type == DOCK_CALL_FIXUP) {
198 void (*fixup)(struct acpi_device *);
199
200 fixup = adev->hp->fixup;
201 if (fixup) {
202 acpi_unlock_hp_context();
203 fixup(adev);
204 return;
205 }
206 } else {
207 int (*notify)(struct acpi_device *, u32);
208
209 notify = adev->hp->event;
210 if (notify) {
211 acpi_unlock_hp_context();
212 notify(adev, event);
213 return;
214 }
215 }
216
217 no_context:
218 acpi_unlock_hp_context();
219
21a31013
RW
220 mutex_lock(&hotplug_lock);
221
222 if (dd->hp_context) {
223 run = true;
224 dd->hp_refcount++;
f09ce741
RW
225 if (dd->hp_ops) {
226 switch (cb_type) {
227 case DOCK_CALL_FIXUP:
228 cb = dd->hp_ops->fixup;
229 break;
230 case DOCK_CALL_UEVENT:
231 cb = dd->hp_ops->uevent;
232 break;
233 default:
234 cb = dd->hp_ops->handler;
235 }
236 }
21a31013
RW
237 }
238
239 mutex_unlock(&hotplug_lock);
240
241 if (!run)
242 return;
243
244 if (cb)
3b52b21f 245 cb(dd->adev->handle, event, dd->hp_context);
21a31013
RW
246
247 dock_release_hotplug(dd);
c8f7a62c
LB
248}
249
1e2380cd
RW
250static struct dock_station *find_dock_station(acpi_handle handle)
251{
252 struct dock_station *ds;
253
254 list_for_each_entry(ds, &dock_stations, sibling)
255 if (ds->handle == handle)
256 return ds;
257
258 return NULL;
259}
260
c8f7a62c
LB
261/**
262 * find_dock_dependent_device - get a device dependent on this dock
263 * @ds: the dock station
3b52b21f 264 * @adev: ACPI device object to find.
c8f7a62c
LB
265 *
266 * iterate over the dependent device list for this dock. If the
267 * dependent device matches the handle, return.
268 */
269static struct dock_dependent_device *
3b52b21f 270find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
c8f7a62c
LB
271{
272 struct dock_dependent_device *dd;
273
ed633e70 274 list_for_each_entry(dd, &ds->dependent_devices, list)
3b52b21f 275 if (adev == dd->adev)
c8f7a62c 276 return dd;
ed633e70 277
c8f7a62c
LB
278 return NULL;
279}
280
1e2380cd
RW
281void register_dock_dependent_device(struct acpi_device *adev,
282 acpi_handle dshandle)
db350b08 283{
1e2380cd 284 struct dock_station *ds = find_dock_station(dshandle);
db350b08 285
3b52b21f
RW
286 if (ds && !find_dock_dependent_device(ds, adev))
287 add_dock_dependent_device(ds, adev);
db350b08
SL
288}
289
1e2380cd
RW
290/*****************************************************************************
291 * Dock functions *
292 *****************************************************************************/
db350b08 293
c8f7a62c
LB
294/**
295 * is_dock_device - see if a device is on a dock station
3b52b21f 296 * @adev: ACPI device object to check.
c8f7a62c
LB
297 *
298 * If this device is either the dock station itself,
299 * or is a device dependent on the dock station, then it
300 * is a dock device
301 */
3b52b21f 302int is_dock_device(struct acpi_device *adev)
c8f7a62c 303{
db350b08
SL
304 struct dock_station *dock_station;
305
306 if (!dock_station_count)
c8f7a62c
LB
307 return 0;
308
3b52b21f 309 if (acpi_dock_match(adev->handle))
c8f7a62c 310 return 1;
747479a3
AC
311
312 list_for_each_entry(dock_station, &dock_stations, sibling)
3b52b21f 313 if (find_dock_dependent_device(dock_station, adev))
db350b08 314 return 1;
c8f7a62c
LB
315
316 return 0;
317}
c8f7a62c
LB
318EXPORT_SYMBOL_GPL(is_dock_device);
319
320/**
321 * dock_present - see if the dock station is present.
322 * @ds: the dock station
323 *
324 * execute the _STA method. note that present does not
325 * imply that we are docked.
326 */
327static int dock_present(struct dock_station *ds)
328{
27663c58 329 unsigned long long sta;
c8f7a62c
LB
330 acpi_status status;
331
332 if (ds) {
333 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
334 if (ACPI_SUCCESS(status) && sta)
335 return 1;
336 }
337 return 0;
338}
339
c8f7a62c 340/**
37f90877
RW
341 * hot_remove_dock_devices - Remove dock station devices.
342 * @ds: Dock station.
343 */
344static void hot_remove_dock_devices(struct dock_station *ds)
345{
346 struct dock_dependent_device *dd;
347
348 /*
349 * Walk the list in reverse order so that devices that have been added
350 * last are removed first (in case there are some indirect dependencies
351 * between them).
352 */
353 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
354 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
355
356 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
3b52b21f 357 acpi_bus_trim(dd->adev);
37f90877
RW
358}
359
360/**
361 * hotplug_dock_devices - Insert devices on a dock station.
c8f7a62c 362 * @ds: the dock station
37f90877 363 * @event: either bus check or device check request
c8f7a62c
LB
364 *
365 * Some devices on the dock station need to have drivers called
366 * to perform hotplug operations after a dock event has occurred.
367 * Traverse the list of dock devices that have registered a
368 * hotplug handler, and call the handler.
369 */
370static void hotplug_dock_devices(struct dock_station *ds, u32 event)
371{
372 struct dock_dependent_device *dd;
373
f09ce741
RW
374 /* Call driver specific post-dock fixups. */
375 list_for_each_entry(dd, &ds->dependent_devices, list)
376 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
377
37f90877 378 /* Call driver specific hotplug functions. */
21a31013 379 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 380 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
c8f7a62c
LB
381
382 /*
3b52b21f
RW
383 * Check if all devices have been enumerated already. If not, run
384 * acpi_bus_scan() for them and that will cause scan handlers to be
385 * attached to device objects or acpi_drivers to be stopped/started if
386 * they are present.
c8f7a62c 387 */
3b52b21f
RW
388 list_for_each_entry(dd, &ds->dependent_devices, list) {
389 struct acpi_device *adev = dd->adev;
390
391 if (!acpi_device_enumerated(adev)) {
392 int ret = acpi_bus_scan(adev->handle);
393 if (ret)
394 dev_dbg(&adev->dev, "scan error %d\n", -ret);
395 }
396 }
c8f7a62c
LB
397}
398
399static void dock_event(struct dock_station *ds, u32 event, int num)
400{
db350b08 401 struct device *dev = &ds->dock_device->dev;
66b56821 402 char event_string[13];
79a8f70b 403 char *envp[] = { event_string, NULL };
1253f7aa 404 struct dock_dependent_device *dd;
79a8f70b
KCA
405
406 if (num == UNDOCK_EVENT)
66b56821 407 sprintf(event_string, "EVENT=undock");
79a8f70b 408 else
66b56821 409 sprintf(event_string, "EVENT=dock");
79a8f70b 410
5669021e 411 /*
8ea86e0b
KCA
412 * Indicate that the status of the dock station has
413 * changed.
5669021e 414 */
1253f7aa
SL
415 if (num == DOCK_EVENT)
416 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
417
21a31013 418 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 419 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
747479a3 420
1253f7aa
SL
421 if (num != DOCK_EVENT)
422 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
c8f7a62c
LB
423}
424
c8f7a62c
LB
425/**
426 * handle_dock - handle a dock event
427 * @ds: the dock station
428 * @dock: to dock, or undock - that is the question
429 *
430 * Execute the _DCK method in response to an acpi event
431 */
432static void handle_dock(struct dock_station *ds, int dock)
433{
434 acpi_status status;
435 struct acpi_object_list arg_list;
436 union acpi_object arg;
6a868e17 437 unsigned long long value;
c8f7a62c 438
cd73018f 439 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
c8f7a62c
LB
440
441 /* _DCK method has one argument */
442 arg_list.count = 1;
443 arg_list.pointer = &arg;
444 arg.type = ACPI_TYPE_INTEGER;
445 arg.integer.value = dock;
6a868e17 446 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
db350b08 447 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
cd73018f
TK
448 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
449 status);
c8f7a62c
LB
450}
451
452static inline void dock(struct dock_station *ds)
453{
454 handle_dock(ds, 1);
455}
456
457static inline void undock(struct dock_station *ds)
458{
459 handle_dock(ds, 0);
460}
461
462static inline void begin_dock(struct dock_station *ds)
463{
464 ds->flags |= DOCK_DOCKING;
465}
466
467static inline void complete_dock(struct dock_station *ds)
468{
469 ds->flags &= ~(DOCK_DOCKING);
470 ds->last_dock_time = jiffies;
471}
472
a0cd35fd
KCA
473static inline void begin_undock(struct dock_station *ds)
474{
475 ds->flags |= DOCK_UNDOCKING;
476}
477
478static inline void complete_undock(struct dock_station *ds)
479{
480 ds->flags &= ~(DOCK_UNDOCKING);
481}
482
c8f7a62c
LB
483/**
484 * dock_in_progress - see if we are in the middle of handling a dock event
485 * @ds: the dock station
486 *
487 * Sometimes while docking, false dock events can be sent to the driver
488 * because good connections aren't made or some other reason. Ignore these
489 * if we are in the middle of doing something.
490 */
491static int dock_in_progress(struct dock_station *ds)
492{
493 if ((ds->flags & DOCK_DOCKING) ||
494 time_before(jiffies, (ds->last_dock_time + HZ)))
495 return 1;
496 return 0;
497}
498
c8f7a62c
LB
499/**
500 * register_hotplug_dock_device - register a hotplug function
501 * @handle: the handle of the device
1253f7aa 502 * @ops: handlers to call after docking
c8f7a62c 503 * @context: device specific data
21a31013
RW
504 * @init: Optional initialization routine to run after registration
505 * @release: Optional release routine to run on unregistration
c8f7a62c
LB
506 *
507 * If a driver would like to perform a hotplug operation after a dock
508 * event, they can register an acpi_notifiy_handler to be called by
509 * the dock driver after _DCK is executed.
510 */
21a31013
RW
511int register_hotplug_dock_device(acpi_handle handle,
512 const struct acpi_dock_ops *ops, void *context,
513 void (*init)(void *), void (*release)(void *))
c8f7a62c
LB
514{
515 struct dock_dependent_device *dd;
db350b08 516 struct dock_station *dock_station;
3b52b21f 517 struct acpi_device *adev;
61b83695 518 int ret = -EINVAL;
c8f7a62c 519
21a31013
RW
520 if (WARN_ON(!context))
521 return -EINVAL;
522
db350b08 523 if (!dock_station_count)
c8f7a62c
LB
524 return -ENODEV;
525
3b52b21f
RW
526 ret = acpi_bus_get_device(handle, &adev);
527 if (ret)
528 return ret;
529
c8f7a62c
LB
530 /*
531 * make sure this handle is for a device dependent on the dock,
532 * this would include the dock station itself
533 */
50d716e4 534 list_for_each_entry(dock_station, &dock_stations, sibling) {
61b83695
SL
535 /*
536 * An ATA bay can be in a dock and itself can be ejected
3ad2f3fb 537 * separately, so there are two 'dock stations' which need the
61b83695
SL
538 * ops
539 */
3b52b21f 540 dd = find_dock_dependent_device(dock_station, adev);
21a31013 541 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
61b83695 542 ret = 0;
c8f7a62c
LB
543 }
544
61b83695 545 return ret;
c8f7a62c 546}
c8f7a62c
LB
547EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
548
549/**
550 * unregister_hotplug_dock_device - remove yourself from the hotplug list
551 * @handle: the acpi handle of the device
552 */
553void unregister_hotplug_dock_device(acpi_handle handle)
554{
555 struct dock_dependent_device *dd;
db350b08 556 struct dock_station *dock_station;
3b52b21f 557 struct acpi_device *adev;
c8f7a62c 558
db350b08 559 if (!dock_station_count)
c8f7a62c
LB
560 return;
561
3b52b21f
RW
562 if (acpi_bus_get_device(handle, &adev))
563 return;
564
50d716e4 565 list_for_each_entry(dock_station, &dock_stations, sibling) {
3b52b21f 566 dd = find_dock_dependent_device(dock_station, adev);
db350b08 567 if (dd)
21a31013 568 dock_release_hotplug(dd);
db350b08 569 }
c8f7a62c 570}
c8f7a62c
LB
571EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
572
c80fdbe8 573/**
574 * handle_eject_request - handle an undock request checking for error conditions
575 *
576 * Check to make sure the dock device is still present, then undock and
577 * hotremove all the devices that may need removing.
578 */
579static int handle_eject_request(struct dock_station *ds, u32 event)
580{
c80fdbe8 581 if (dock_in_progress(ds))
582 return -EBUSY;
583
584 /*
585 * here we need to generate the undock
586 * event prior to actually doing the undock
587 * so that the device struct still exists.
afd7301d
HM
588 * Also, even send the dock event if the
589 * device is not present anymore
c80fdbe8 590 */
591 dock_event(ds, event, UNDOCK_EVENT);
afd7301d 592
37f90877 593 hot_remove_dock_devices(ds);
c80fdbe8 594 undock(ds);
c9b5471f
JL
595 acpi_evaluate_lck(ds->handle, 0);
596 acpi_evaluate_ej0(ds->handle);
c80fdbe8 597 if (dock_present(ds)) {
cd73018f 598 acpi_handle_err(ds->handle, "Unable to undock!\n");
c80fdbe8 599 return -EBUSY;
600 }
a0cd35fd 601 complete_undock(ds);
c80fdbe8 602 return 0;
603}
604
c8f7a62c 605/**
1e2380cd
RW
606 * dock_notify - Handle ACPI dock notification.
607 * @adev: Dock station's ACPI device object.
608 * @event: Event code.
c8f7a62c
LB
609 *
610 * If we are notified to dock, then check to see if the dock is
611 * present and then dock. Notify all drivers of the dock event,
c80fdbe8 612 * and then hotplug and devices that may need hotplugging.
c8f7a62c 613 */
1e2380cd 614int dock_notify(struct acpi_device *adev, u32 event)
c8f7a62c 615{
1e2380cd
RW
616 acpi_handle handle = adev->handle;
617 struct dock_station *ds = find_dock_station(handle);
db350b08 618 int surprise_removal = 0;
c8f7a62c 619
1e2380cd
RW
620 if (!ds)
621 return -ENODEV;
622
db350b08
SL
623 /*
624 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
625 * is sent and _DCK is present, it is assumed to mean an undock
626 * request.
627 */
628 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
629 event = ACPI_NOTIFY_EJECT_REQUEST;
630
631 /*
632 * dock station: BUS_CHECK - docked or surprise removal
633 * DEVICE_CHECK - undocked
634 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
635 *
636 * To simplify event handling, dock dependent device handler always
637 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
638 * ACPI_NOTIFY_EJECT_REQUEST for removal
639 */
c8f7a62c
LB
640 switch (event) {
641 case ACPI_NOTIFY_BUS_CHECK:
db350b08 642 case ACPI_NOTIFY_DEVICE_CHECK:
0a8e5c3d 643 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
c8f7a62c
LB
644 begin_dock(ds);
645 dock(ds);
646 if (!dock_present(ds)) {
cd73018f 647 acpi_handle_err(handle, "Unable to dock!\n");
8b59560a 648 complete_dock(ds);
c8f7a62c
LB
649 break;
650 }
c8f7a62c
LB
651 hotplug_dock_devices(ds, event);
652 complete_dock(ds);
653 dock_event(ds, event, DOCK_EVENT);
c9b5471f 654 acpi_evaluate_lck(ds->handle, 1);
3a37898d 655 acpi_update_all_gpes();
db350b08 656 break;
c8f7a62c 657 }
db350b08
SL
658 if (dock_present(ds) || dock_in_progress(ds))
659 break;
660 /* This is a surprise removal */
661 surprise_removal = 1;
662 event = ACPI_NOTIFY_EJECT_REQUEST;
663 /* Fall back */
c8f7a62c 664 case ACPI_NOTIFY_EJECT_REQUEST:
a0cd35fd 665 begin_undock(ds);
f730ae18
SL
666 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
667 || surprise_removal)
a0cd35fd
KCA
668 handle_eject_request(ds, event);
669 else
670 dock_event(ds, event, UNDOCK_EVENT);
c8f7a62c 671 break;
c8f7a62c 672 }
1e2380cd 673 return 0;
c8f7a62c
LB
674}
675
c80fdbe8 676/*
677 * show_docked - read method for "docked" file in sysfs
678 */
679static ssize_t show_docked(struct device *dev,
680 struct device_attribute *attr, char *buf)
681{
fe06fba2 682 struct dock_station *dock_station = dev->platform_data;
ab62f9cd 683 struct acpi_device *adev = NULL;
c80fdbe8 684
ab62f9cd
RW
685 acpi_bus_get_device(dock_station->handle, &adev);
686 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
c80fdbe8 687}
e5685b9d 688static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
c80fdbe8 689
a0cd35fd
KCA
690/*
691 * show_flags - read method for flags file in sysfs
692 */
693static ssize_t show_flags(struct device *dev,
694 struct device_attribute *attr, char *buf)
695{
fe06fba2 696 struct dock_station *dock_station = dev->platform_data;
a0cd35fd
KCA
697 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
698
699}
e5685b9d 700static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
a0cd35fd 701
c80fdbe8 702/*
703 * write_undock - write method for "undock" file in sysfs
704 */
705static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
706 const char *buf, size_t count)
707{
708 int ret;
fe06fba2 709 struct dock_station *dock_station = dev->platform_data;
c80fdbe8 710
711 if (!count)
712 return -EINVAL;
713
8112006f 714 acpi_scan_lock_acquire();
9171f834 715 begin_undock(dock_station);
c80fdbe8 716 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
8112006f 717 acpi_scan_lock_release();
c80fdbe8 718 return ret ? ret: count;
719}
e5685b9d 720static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
c80fdbe8 721
ac122bb6
IVE
722/*
723 * show_dock_uid - read method for "uid" file in sysfs
724 */
725static ssize_t show_dock_uid(struct device *dev,
726 struct device_attribute *attr, char *buf)
727{
27663c58 728 unsigned long long lbuf;
fe06fba2 729 struct dock_station *dock_station = dev->platform_data;
38ff4ffc
KCA
730 acpi_status status = acpi_evaluate_integer(dock_station->handle,
731 "_UID", NULL, &lbuf);
732 if (ACPI_FAILURE(status))
ac122bb6 733 return 0;
38ff4ffc 734
27663c58 735 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
ac122bb6 736}
e5685b9d 737static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
ac122bb6 738
8652b00f
SL
739static ssize_t show_dock_type(struct device *dev,
740 struct device_attribute *attr, char *buf)
741{
fe06fba2 742 struct dock_station *dock_station = dev->platform_data;
8652b00f
SL
743 char *type;
744
745 if (dock_station->flags & DOCK_IS_DOCK)
746 type = "dock_station";
747 else if (dock_station->flags & DOCK_IS_ATA)
748 type = "ata_bay";
749 else if (dock_station->flags & DOCK_IS_BAT)
750 type = "battery_bay";
751 else
752 type = "unknown";
753
754 return snprintf(buf, PAGE_SIZE, "%s\n", type);
755}
756static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
757
5f46c2f2
AC
758static struct attribute *dock_attributes[] = {
759 &dev_attr_docked.attr,
760 &dev_attr_flags.attr,
761 &dev_attr_undock.attr,
762 &dev_attr_uid.attr,
763 &dev_attr_type.attr,
764 NULL
765};
766
767static struct attribute_group dock_attribute_group = {
768 .attrs = dock_attributes
769};
770
c8f7a62c 771/**
1e2380cd
RW
772 * acpi_dock_add - Add a new dock station
773 * @adev: Dock station ACPI device object.
c8f7a62c 774 *
1e2380cd 775 * allocated and initialize a new dock station device.
c8f7a62c 776 */
1e2380cd 777void acpi_dock_add(struct acpi_device *adev)
c8f7a62c 778{
2efbca4d 779 struct dock_station *dock_station, ds = { NULL, };
af887449 780 struct platform_device_info pdevinfo;
1e2380cd 781 acpi_handle handle = adev->handle;
747479a3 782 struct platform_device *dd;
2efbca4d 783 int ret;
c8f7a62c 784
af887449
RW
785 memset(&pdevinfo, 0, sizeof(pdevinfo));
786 pdevinfo.name = "dock";
787 pdevinfo.id = dock_station_count;
788 pdevinfo.acpi_node.companion = adev;
789 pdevinfo.data = &ds;
790 pdevinfo.size_data = sizeof(ds);
791 dd = platform_device_register_full(&pdevinfo);
747479a3 792 if (IS_ERR(dd))
1e2380cd 793 return;
747479a3
AC
794
795 dock_station = dd->dev.platform_data;
c8f7a62c 796
c8f7a62c 797 dock_station->handle = handle;
747479a3 798 dock_station->dock_device = dd;
c8f7a62c 799 dock_station->last_dock_time = jiffies - HZ;
747479a3 800
747479a3 801 INIT_LIST_HEAD(&dock_station->sibling);
747479a3 802 INIT_LIST_HEAD(&dock_station->dependent_devices);
a0cd35fd 803
9ef2a9a9 804 /* we want the dock device to send uevents */
747479a3 805 dev_set_uevent_suppress(&dd->dev, 0);
9ef2a9a9 806
c9b5471f 807 if (acpi_dock_match(handle))
db350b08 808 dock_station->flags |= DOCK_IS_DOCK;
c9b5471f 809 if (acpi_ata_match(handle))
db350b08 810 dock_station->flags |= DOCK_IS_ATA;
b43109fa 811 if (acpi_device_is_battery(adev))
db350b08
SL
812 dock_station->flags |= DOCK_IS_BAT;
813
747479a3 814 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
8652b00f 815 if (ret)
5f46c2f2 816 goto err_unregister;
671adbec 817
c8f7a62c 818 /* add the dock station as a device dependent on itself */
3b52b21f 819 ret = add_dock_dependent_device(dock_station, adev);
f69cfdd2 820 if (ret)
5f46c2f2 821 goto err_rmgroup;
c8f7a62c 822
db350b08 823 dock_station_count++;
50d716e4 824 list_add(&dock_station->sibling, &dock_stations);
1e2380cd
RW
825 adev->flags.is_dock_station = true;
826 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
827 dock_station_count);
828 return;
c8f7a62c 829
5f46c2f2 830err_rmgroup:
a30c4c5e 831 remove_dock_dependent_devices(dock_station);
747479a3 832 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
5f46c2f2 833err_unregister:
747479a3 834 platform_device_unregister(dd);
cd73018f 835 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
c8f7a62c 836}
This page took 0.618137 seconds and 5 git commands to generate.