ACPI, x86: Fix extended error log driver to depend on CONFIG_X86_LOCAL_APIC
[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
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
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;
c8f7a62c 75 acpi_handle handle;
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
AC
100 * add_dock_dependent_device - associate a device with the dock station
101 * @ds: The dock station
102 * @handle: handle of the dependent device
c8f7a62c 103 *
f69cfdd2 104 * Add the dependent device to the dock's dependent device list.
c8f7a62c 105 */
d423c083 106static int __init
f69cfdd2 107add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
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
115 dd->handle = handle;
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
RW
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++;
f09ce741
RW
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 }
21a31013
RW
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);
c8f7a62c
LB
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 */
229static struct dock_dependent_device *
230find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
231{
232 struct dock_dependent_device *dd;
233
ed633e70
JL
234 list_for_each_entry(dd, &ds->dependent_devices, list)
235 if (handle == dd->handle)
c8f7a62c 236 return dd;
ed633e70 237
c8f7a62c
LB
238 return NULL;
239}
240
241/*****************************************************************************
242 * Dock functions *
243 *****************************************************************************/
d423c083 244static int __init is_battery(acpi_handle handle)
db350b08
SL
245{
246 struct acpi_device_info *info;
db350b08
SL
247 int ret = 1;
248
15b8dd53 249 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
db350b08 250 return 0;
db350b08
SL
251 if (!(info->valid & ACPI_VALID_HID))
252 ret = 0;
253 else
15b8dd53 254 ret = !strcmp("PNP0C0A", info->hardware_id.string);
db350b08 255
15b8dd53 256 kfree(info);
db350b08
SL
257 return ret;
258}
259
c9b5471f
JL
260/* Check whether ACPI object is an ejectable battery or disk bay */
261static bool __init is_ejectable_bay(acpi_handle handle)
db350b08 262{
c9b5471f
JL
263 if (acpi_has_method(handle, "_EJ0") && is_battery(handle))
264 return true;
747479a3 265
c9b5471f 266 return acpi_bay_match(handle);
db350b08
SL
267}
268
c8f7a62c
LB
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 */
277int is_dock_device(acpi_handle handle)
278{
db350b08
SL
279 struct dock_station *dock_station;
280
281 if (!dock_station_count)
c8f7a62c
LB
282 return 0;
283
c9b5471f 284 if (acpi_dock_match(handle))
c8f7a62c 285 return 1;
747479a3
AC
286
287 list_for_each_entry(dock_station, &dock_stations, sibling)
db350b08
SL
288 if (find_dock_dependent_device(dock_station, handle))
289 return 1;
c8f7a62c
LB
290
291 return 0;
292}
c8f7a62c
LB
293EXPORT_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 */
302static int dock_present(struct dock_station *ds)
303{
27663c58 304 unsigned long long sta;
c8f7a62c
LB
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
c8f7a62c
LB
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.
c8f7a62c 323 */
472d963b 324static void dock_create_acpi_device(acpi_handle handle)
c8f7a62c 325{
747479a3 326 struct acpi_device *device;
c8f7a62c
LB
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 */
b8bd759a 334 ret = acpi_bus_scan(handle);
636458de 335 if (ret)
747479a3 336 pr_debug("error adding bus, %x\n", -ret);
c8f7a62c 337 }
c8f7a62c
LB
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 */
347static void dock_remove_acpi_device(acpi_handle handle)
348{
349 struct acpi_device *device;
c8f7a62c 350
bfee26db
RW
351 if (!acpi_bus_get_device(handle, &device))
352 acpi_bus_trim(device);
c8f7a62c
LB
353}
354
c8f7a62c 355/**
37f90877
RW
356 * hot_remove_dock_devices - Remove dock station devices.
357 * @ds: Dock station.
358 */
359static 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.
c8f7a62c 377 * @ds: the dock station
37f90877 378 * @event: either bus check or device check request
c8f7a62c
LB
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 */
385static void hotplug_dock_devices(struct dock_station *ds, u32 event)
386{
387 struct dock_dependent_device *dd;
388
f09ce741
RW
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
37f90877 393 /* Call driver specific hotplug functions. */
21a31013 394 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 395 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
c8f7a62c
LB
396
397 /*
37f90877
RW
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.
c8f7a62c 401 */
37f90877
RW
402 list_for_each_entry(dd, &ds->dependent_devices, list)
403 dock_create_acpi_device(dd->handle);
c8f7a62c
LB
404}
405
406static void dock_event(struct dock_station *ds, u32 event, int num)
407{
db350b08 408 struct device *dev = &ds->dock_device->dev;
66b56821 409 char event_string[13];
79a8f70b 410 char *envp[] = { event_string, NULL };
1253f7aa 411 struct dock_dependent_device *dd;
79a8f70b
KCA
412
413 if (num == UNDOCK_EVENT)
66b56821 414 sprintf(event_string, "EVENT=undock");
79a8f70b 415 else
66b56821 416 sprintf(event_string, "EVENT=dock");
79a8f70b 417
5669021e 418 /*
8ea86e0b
KCA
419 * Indicate that the status of the dock station has
420 * changed.
5669021e 421 */
1253f7aa
SL
422 if (num == DOCK_EVENT)
423 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
424
21a31013 425 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 426 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
747479a3 427
1253f7aa
SL
428 if (num != DOCK_EVENT)
429 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
c8f7a62c
LB
430}
431
c8f7a62c
LB
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 */
439static 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 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
c8f7a62c 445
cd73018f 446 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
c8f7a62c
LB
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_object(ds->handle, "_DCK", &arg_list, &buffer);
db350b08 454 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
cd73018f
TK
455 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
456 status);
0a918a94 457
c8f7a62c 458 kfree(buffer.pointer);
c8f7a62c
LB
459}
460
461static inline void dock(struct dock_station *ds)
462{
463 handle_dock(ds, 1);
464}
465
466static inline void undock(struct dock_station *ds)
467{
468 handle_dock(ds, 0);
469}
470
471static inline void begin_dock(struct dock_station *ds)
472{
473 ds->flags |= DOCK_DOCKING;
474}
475
476static inline void complete_dock(struct dock_station *ds)
477{
478 ds->flags &= ~(DOCK_DOCKING);
479 ds->last_dock_time = jiffies;
480}
481
a0cd35fd
KCA
482static inline void begin_undock(struct dock_station *ds)
483{
484 ds->flags |= DOCK_UNDOCKING;
485}
486
487static inline void complete_undock(struct dock_station *ds)
488{
489 ds->flags &= ~(DOCK_UNDOCKING);
490}
491
c8f7a62c
LB
492/**
493 * dock_in_progress - see if we are in the middle of handling a dock event
494 * @ds: the dock station
495 *
496 * Sometimes while docking, false dock events can be sent to the driver
497 * because good connections aren't made or some other reason. Ignore these
498 * if we are in the middle of doing something.
499 */
500static int dock_in_progress(struct dock_station *ds)
501{
502 if ((ds->flags & DOCK_DOCKING) ||
503 time_before(jiffies, (ds->last_dock_time + HZ)))
504 return 1;
505 return 0;
506}
507
c8f7a62c
LB
508/**
509 * register_hotplug_dock_device - register a hotplug function
510 * @handle: the handle of the device
1253f7aa 511 * @ops: handlers to call after docking
c8f7a62c 512 * @context: device specific data
21a31013
RW
513 * @init: Optional initialization routine to run after registration
514 * @release: Optional release routine to run on unregistration
c8f7a62c
LB
515 *
516 * If a driver would like to perform a hotplug operation after a dock
517 * event, they can register an acpi_notifiy_handler to be called by
518 * the dock driver after _DCK is executed.
519 */
21a31013
RW
520int register_hotplug_dock_device(acpi_handle handle,
521 const struct acpi_dock_ops *ops, void *context,
522 void (*init)(void *), void (*release)(void *))
c8f7a62c
LB
523{
524 struct dock_dependent_device *dd;
db350b08 525 struct dock_station *dock_station;
61b83695 526 int ret = -EINVAL;
c8f7a62c 527
21a31013
RW
528 if (WARN_ON(!context))
529 return -EINVAL;
530
db350b08 531 if (!dock_station_count)
c8f7a62c
LB
532 return -ENODEV;
533
534 /*
535 * make sure this handle is for a device dependent on the dock,
536 * this would include the dock station itself
537 */
50d716e4 538 list_for_each_entry(dock_station, &dock_stations, sibling) {
61b83695
SL
539 /*
540 * An ATA bay can be in a dock and itself can be ejected
3ad2f3fb 541 * separately, so there are two 'dock stations' which need the
61b83695
SL
542 * ops
543 */
db350b08 544 dd = find_dock_dependent_device(dock_station, handle);
21a31013 545 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
61b83695 546 ret = 0;
c8f7a62c
LB
547 }
548
61b83695 549 return ret;
c8f7a62c 550}
c8f7a62c
LB
551EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
552
553/**
554 * unregister_hotplug_dock_device - remove yourself from the hotplug list
555 * @handle: the acpi handle of the device
556 */
557void unregister_hotplug_dock_device(acpi_handle handle)
558{
559 struct dock_dependent_device *dd;
db350b08 560 struct dock_station *dock_station;
c8f7a62c 561
db350b08 562 if (!dock_station_count)
c8f7a62c
LB
563 return;
564
50d716e4 565 list_for_each_entry(dock_station, &dock_stations, sibling) {
db350b08
SL
566 dd = find_dock_dependent_device(dock_station, handle);
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
LB
605/**
606 * dock_notify - act upon an acpi dock notification
59401ccc 607 * @ds: dock station
c8f7a62c 608 * @event: the acpi event
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 */
59401ccc 614static void dock_notify(struct dock_station *ds, u32 event)
c8f7a62c 615{
59401ccc
RW
616 acpi_handle handle = ds->handle;
617 struct acpi_device *ad;
db350b08 618 int surprise_removal = 0;
c8f7a62c 619
db350b08
SL
620 /*
621 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
622 * is sent and _DCK is present, it is assumed to mean an undock
623 * request.
624 */
625 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
626 event = ACPI_NOTIFY_EJECT_REQUEST;
627
628 /*
629 * dock station: BUS_CHECK - docked or surprise removal
630 * DEVICE_CHECK - undocked
631 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
632 *
633 * To simplify event handling, dock dependent device handler always
634 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
635 * ACPI_NOTIFY_EJECT_REQUEST for removal
636 */
c8f7a62c
LB
637 switch (event) {
638 case ACPI_NOTIFY_BUS_CHECK:
db350b08 639 case ACPI_NOTIFY_DEVICE_CHECK:
59401ccc 640 if (!dock_in_progress(ds) && acpi_bus_get_device(handle, &ad)) {
c8f7a62c
LB
641 begin_dock(ds);
642 dock(ds);
643 if (!dock_present(ds)) {
cd73018f 644 acpi_handle_err(handle, "Unable to dock!\n");
8b59560a 645 complete_dock(ds);
c8f7a62c
LB
646 break;
647 }
c8f7a62c
LB
648 hotplug_dock_devices(ds, event);
649 complete_dock(ds);
650 dock_event(ds, event, DOCK_EVENT);
c9b5471f 651 acpi_evaluate_lck(ds->handle, 1);
3a37898d 652 acpi_update_all_gpes();
db350b08 653 break;
c8f7a62c 654 }
db350b08
SL
655 if (dock_present(ds) || dock_in_progress(ds))
656 break;
657 /* This is a surprise removal */
658 surprise_removal = 1;
659 event = ACPI_NOTIFY_EJECT_REQUEST;
660 /* Fall back */
c8f7a62c 661 case ACPI_NOTIFY_EJECT_REQUEST:
a0cd35fd 662 begin_undock(ds);
f730ae18
SL
663 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
664 || surprise_removal)
a0cd35fd
KCA
665 handle_eject_request(ds, event);
666 else
667 dock_event(ds, event, UNDOCK_EVENT);
c8f7a62c
LB
668 break;
669 default:
cd73018f 670 acpi_handle_err(handle, "Unknown dock event %d\n", event);
c8f7a62c
LB
671 }
672}
673
19cd847a 674struct dock_data {
19cd847a 675 struct dock_station *ds;
59401ccc 676 u32 event;
19cd847a
ZR
677};
678
679static void acpi_dock_deferred_cb(void *context)
680{
747479a3 681 struct dock_data *data = context;
19cd847a 682
3757b948 683 acpi_scan_lock_acquire();
59401ccc 684 dock_notify(data->ds, data->event);
3757b948 685 acpi_scan_lock_release();
19cd847a
ZR
686 kfree(data);
687}
688
59401ccc 689static void dock_notify_handler(acpi_handle handle, u32 event, void *data)
6bd00a61 690{
59401ccc 691 struct dock_data *dd;
6bd00a61
SL
692
693 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
694 && event != ACPI_NOTIFY_EJECT_REQUEST)
59401ccc 695 return;
3757b948 696
59401ccc
RW
697 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
698 if (dd) {
699 acpi_status status;
3757b948 700
59401ccc
RW
701 dd->ds = data;
702 dd->event = event;
703 status = acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
704 if (ACPI_FAILURE(status))
705 kfree(dd);
6bd00a61 706 }
6bd00a61
SL
707}
708
c8f7a62c
LB
709/**
710 * find_dock_devices - find devices on the dock station
711 * @handle: the handle of the device we are examining
712 * @lvl: unused
713 * @context: the dock station private data
714 * @rv: unused
715 *
716 * This function is called by acpi_walk_namespace. It will
717 * check to see if an object has an _EJD method. If it does, then it
718 * will see if it is dependent on the dock station.
719 */
96c0a4d4
RW
720static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
721 void *context, void **rv)
c8f7a62c 722{
50dd0969 723 struct dock_station *ds = context;
96c0a4d4 724 acpi_handle ejd = NULL;
c8f7a62c 725
96c0a4d4
RW
726 acpi_bus_get_ejd(handle, &ejd);
727 if (ejd == ds->handle)
f69cfdd2
AC
728 add_dock_dependent_device(ds, handle);
729
c8f7a62c
LB
730 return AE_OK;
731}
732
c80fdbe8 733/*
734 * show_docked - read method for "docked" file in sysfs
735 */
736static ssize_t show_docked(struct device *dev,
737 struct device_attribute *attr, char *buf)
738{
fc5a9f88
HM
739 struct acpi_device *tmp;
740
fe06fba2 741 struct dock_station *dock_station = dev->platform_data;
c80fdbe8 742
02df7349 743 if (!acpi_bus_get_device(dock_station->handle, &tmp))
fc5a9f88
HM
744 return snprintf(buf, PAGE_SIZE, "1\n");
745 return snprintf(buf, PAGE_SIZE, "0\n");
c80fdbe8 746}
e5685b9d 747static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
c80fdbe8 748
a0cd35fd
KCA
749/*
750 * show_flags - read method for flags file in sysfs
751 */
752static ssize_t show_flags(struct device *dev,
753 struct device_attribute *attr, char *buf)
754{
fe06fba2 755 struct dock_station *dock_station = dev->platform_data;
a0cd35fd
KCA
756 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
757
758}
e5685b9d 759static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
a0cd35fd 760
c80fdbe8 761/*
762 * write_undock - write method for "undock" file in sysfs
763 */
764static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
765 const char *buf, size_t count)
766{
767 int ret;
fe06fba2 768 struct dock_station *dock_station = dev->platform_data;
c80fdbe8 769
770 if (!count)
771 return -EINVAL;
772
8112006f 773 acpi_scan_lock_acquire();
9171f834 774 begin_undock(dock_station);
c80fdbe8 775 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
8112006f 776 acpi_scan_lock_release();
c80fdbe8 777 return ret ? ret: count;
778}
e5685b9d 779static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
c80fdbe8 780
ac122bb6
IVE
781/*
782 * show_dock_uid - read method for "uid" file in sysfs
783 */
784static ssize_t show_dock_uid(struct device *dev,
785 struct device_attribute *attr, char *buf)
786{
27663c58 787 unsigned long long lbuf;
fe06fba2 788 struct dock_station *dock_station = dev->platform_data;
38ff4ffc
KCA
789 acpi_status status = acpi_evaluate_integer(dock_station->handle,
790 "_UID", NULL, &lbuf);
791 if (ACPI_FAILURE(status))
ac122bb6 792 return 0;
38ff4ffc 793
27663c58 794 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
ac122bb6 795}
e5685b9d 796static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
ac122bb6 797
8652b00f
SL
798static ssize_t show_dock_type(struct device *dev,
799 struct device_attribute *attr, char *buf)
800{
fe06fba2 801 struct dock_station *dock_station = dev->platform_data;
8652b00f
SL
802 char *type;
803
804 if (dock_station->flags & DOCK_IS_DOCK)
805 type = "dock_station";
806 else if (dock_station->flags & DOCK_IS_ATA)
807 type = "ata_bay";
808 else if (dock_station->flags & DOCK_IS_BAT)
809 type = "battery_bay";
810 else
811 type = "unknown";
812
813 return snprintf(buf, PAGE_SIZE, "%s\n", type);
814}
815static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
816
5f46c2f2
AC
817static struct attribute *dock_attributes[] = {
818 &dev_attr_docked.attr,
819 &dev_attr_flags.attr,
820 &dev_attr_undock.attr,
821 &dev_attr_uid.attr,
822 &dev_attr_type.attr,
823 NULL
824};
825
826static struct attribute_group dock_attribute_group = {
827 .attrs = dock_attributes
828};
829
c8f7a62c
LB
830/**
831 * dock_add - add a new dock station
832 * @handle: the dock station handle
833 *
834 * allocated and initialize a new dock station device. Find all devices
835 * that are on the dock station, and register for dock event notifications.
836 */
d38a5edf 837static int __init dock_add(acpi_handle handle)
c8f7a62c 838{
2efbca4d 839 struct dock_station *dock_station, ds = { NULL, };
747479a3 840 struct platform_device *dd;
59401ccc 841 acpi_status status;
2efbca4d 842 int ret;
c8f7a62c 843
2efbca4d
RW
844 dd = platform_device_register_data(NULL, "dock", dock_station_count,
845 &ds, sizeof(ds));
747479a3
AC
846 if (IS_ERR(dd))
847 return PTR_ERR(dd);
848
849 dock_station = dd->dev.platform_data;
c8f7a62c 850
c8f7a62c 851 dock_station->handle = handle;
747479a3 852 dock_station->dock_device = dd;
c8f7a62c 853 dock_station->last_dock_time = jiffies - HZ;
747479a3 854
747479a3 855 INIT_LIST_HEAD(&dock_station->sibling);
747479a3 856 INIT_LIST_HEAD(&dock_station->dependent_devices);
a0cd35fd 857
9ef2a9a9 858 /* we want the dock device to send uevents */
747479a3 859 dev_set_uevent_suppress(&dd->dev, 0);
9ef2a9a9 860
c9b5471f 861 if (acpi_dock_match(handle))
db350b08 862 dock_station->flags |= DOCK_IS_DOCK;
c9b5471f 863 if (acpi_ata_match(handle))
db350b08
SL
864 dock_station->flags |= DOCK_IS_ATA;
865 if (is_battery(handle))
866 dock_station->flags |= DOCK_IS_BAT;
867
747479a3 868 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
8652b00f 869 if (ret)
5f46c2f2 870 goto err_unregister;
671adbec 871
c8f7a62c
LB
872 /* Find dependent devices */
873 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
2263576c
LM
874 ACPI_UINT32_MAX, find_dock_devices, NULL,
875 dock_station, NULL);
c8f7a62c
LB
876
877 /* add the dock station as a device dependent on itself */
f69cfdd2
AC
878 ret = add_dock_dependent_device(dock_station, handle);
879 if (ret)
5f46c2f2 880 goto err_rmgroup;
c8f7a62c 881
59401ccc
RW
882 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
883 dock_notify_handler, dock_station);
0177f29f
WY
884 if (ACPI_FAILURE(status)) {
885 ret = -ENODEV;
59401ccc 886 goto err_rmgroup;
0177f29f 887 }
59401ccc 888
db350b08 889 dock_station_count++;
50d716e4 890 list_add(&dock_station->sibling, &dock_stations);
c8f7a62c
LB
891 return 0;
892
5f46c2f2 893err_rmgroup:
a30c4c5e 894 remove_dock_dependent_devices(dock_station);
747479a3 895 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
5f46c2f2 896err_unregister:
747479a3 897 platform_device_unregister(dd);
cd73018f 898 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
c8f7a62c
LB
899 return ret;
900}
901
c8f7a62c 902/**
8ab0ab25 903 * find_dock_and_bay - look for dock stations and bays
c8f7a62c
LB
904 * @handle: acpi handle of a device
905 * @lvl: unused
8ab0ab25 906 * @context: unused
c8f7a62c
LB
907 * @rv: unused
908 *
8ab0ab25 909 * This is called by acpi_walk_namespace to look for dock stations and bays.
c8f7a62c 910 */
027951e5 911static acpi_status __init
8ab0ab25 912find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
c8f7a62c 913{
c9b5471f 914 if (acpi_dock_match(handle) || is_ejectable_bay(handle))
1ee4d61f 915 dock_add(handle);
747479a3 916
1ee4d61f 917 return AE_OK;
c8f7a62c
LB
918}
919
2ce65fe8 920void __init acpi_dock_init(void)
db350b08 921{
816c2eda 922 if (acpi_disabled)
2ce65fe8 923 return;
816c2eda 924
8ab0ab25 925 /* look for dock stations and bays */
c8f7a62c 926 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
8ab0ab25 927 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
c8f7a62c 928
db350b08 929 if (!dock_station_count) {
cd73018f 930 pr_info(PREFIX "No dock devices found.\n");
2ce65fe8 931 return;
db350b08 932 }
c8f7a62c 933
cd73018f 934 pr_info(PREFIX "%s: %d docks/bays found\n",
db350b08 935 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
c8f7a62c 936}
This page took 0.723023 seconds and 5 git commands to generate.