ACPI / dock: Pass ACPI device pointer to acpi_device_is_battery()
[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;
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 */
1e2380cd 106static int add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
c8f7a62c
LB
107{
108 struct dock_dependent_device *dd;
109
110 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
f69cfdd2
AC
111 if (!dd)
112 return -ENOMEM;
113
114 dd->handle = handle;
115 INIT_LIST_HEAD(&dd->list);
c8f7a62c 116 list_add_tail(&dd->list, &ds->dependent_devices);
f69cfdd2
AC
117
118 return 0;
c8f7a62c
LB
119}
120
a30c4c5e
RW
121static void remove_dock_dependent_devices(struct dock_station *ds)
122{
123 struct dock_dependent_device *dd, *aux;
124
125 list_for_each_entry_safe(dd, aux, &ds->dependent_devices, list) {
126 list_del(&dd->list);
127 kfree(dd);
128 }
129}
130
c8f7a62c 131/**
21a31013
RW
132 * dock_init_hotplug - Initialize a hotplug device on a docking station.
133 * @dd: Dock-dependent device.
134 * @ops: Dock operations to attach to the dependent device.
135 * @context: Data to pass to the @ops callbacks and @release.
136 * @init: Optional initialization routine to run after setting up context.
137 * @release: Optional release routine to run on removal.
c8f7a62c 138 */
21a31013
RW
139static int dock_init_hotplug(struct dock_dependent_device *dd,
140 const struct acpi_dock_ops *ops, void *context,
141 void (*init)(void *), void (*release)(void *))
c8f7a62c 142{
21a31013
RW
143 int ret = 0;
144
145 mutex_lock(&hotplug_lock);
4ec24065 146 if (WARN_ON(dd->hp_context)) {
21a31013
RW
147 ret = -EEXIST;
148 } else {
149 dd->hp_refcount = 1;
150 dd->hp_ops = ops;
151 dd->hp_context = context;
152 dd->hp_release = release;
4ec24065
RW
153 if (init)
154 init(context);
21a31013 155 }
21a31013
RW
156 mutex_unlock(&hotplug_lock);
157 return ret;
c8f7a62c
LB
158}
159
160/**
21a31013
RW
161 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
162 * @dd: Dock-dependent device.
c8f7a62c 163 *
21a31013
RW
164 * Decrement the reference counter of @dd and if 0, detach its hotplug
165 * operations from it, reset its context pointer and run the optional release
166 * routine if present.
c8f7a62c 167 */
21a31013 168static void dock_release_hotplug(struct dock_dependent_device *dd)
c8f7a62c 169{
21a31013 170 mutex_lock(&hotplug_lock);
21a31013 171 if (dd->hp_context && !--dd->hp_refcount) {
4ec24065
RW
172 void (*release)(void *) = dd->hp_release;
173 void *context = dd->hp_context;
174
21a31013 175 dd->hp_ops = NULL;
21a31013 176 dd->hp_context = NULL;
21a31013 177 dd->hp_release = NULL;
4ec24065
RW
178 if (release)
179 release(context);
21a31013 180 }
21a31013
RW
181 mutex_unlock(&hotplug_lock);
182}
183
184static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
f09ce741 185 enum dock_callback_type cb_type)
21a31013
RW
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++;
f09ce741
RW
195 if (dd->hp_ops) {
196 switch (cb_type) {
197 case DOCK_CALL_FIXUP:
198 cb = dd->hp_ops->fixup;
199 break;
200 case DOCK_CALL_UEVENT:
201 cb = dd->hp_ops->uevent;
202 break;
203 default:
204 cb = dd->hp_ops->handler;
205 }
206 }
21a31013
RW
207 }
208
209 mutex_unlock(&hotplug_lock);
210
211 if (!run)
212 return;
213
214 if (cb)
215 cb(dd->handle, event, dd->hp_context);
216
217 dock_release_hotplug(dd);
c8f7a62c
LB
218}
219
1e2380cd
RW
220static struct dock_station *find_dock_station(acpi_handle handle)
221{
222 struct dock_station *ds;
223
224 list_for_each_entry(ds, &dock_stations, sibling)
225 if (ds->handle == handle)
226 return ds;
227
228 return NULL;
229}
230
c8f7a62c
LB
231/**
232 * find_dock_dependent_device - get a device dependent on this dock
233 * @ds: the dock station
234 * @handle: the acpi_handle of the device we want
235 *
236 * iterate over the dependent device list for this dock. If the
237 * dependent device matches the handle, return.
238 */
239static struct dock_dependent_device *
240find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
241{
242 struct dock_dependent_device *dd;
243
ed633e70
JL
244 list_for_each_entry(dd, &ds->dependent_devices, list)
245 if (handle == dd->handle)
c8f7a62c 246 return dd;
ed633e70 247
c8f7a62c
LB
248 return NULL;
249}
250
1e2380cd
RW
251void register_dock_dependent_device(struct acpi_device *adev,
252 acpi_handle dshandle)
db350b08 253{
1e2380cd
RW
254 struct dock_station *ds = find_dock_station(dshandle);
255 acpi_handle handle = adev->handle;
db350b08 256
1e2380cd
RW
257 if (ds && !find_dock_dependent_device(ds, handle))
258 add_dock_dependent_device(ds, handle);
db350b08
SL
259}
260
1e2380cd
RW
261/*****************************************************************************
262 * Dock functions *
263 *****************************************************************************/
db350b08 264
c8f7a62c
LB
265/**
266 * is_dock_device - see if a device is on a dock station
267 * @handle: acpi handle of the device
268 *
269 * If this device is either the dock station itself,
270 * or is a device dependent on the dock station, then it
271 * is a dock device
272 */
273int is_dock_device(acpi_handle handle)
274{
db350b08
SL
275 struct dock_station *dock_station;
276
277 if (!dock_station_count)
c8f7a62c
LB
278 return 0;
279
c9b5471f 280 if (acpi_dock_match(handle))
c8f7a62c 281 return 1;
747479a3
AC
282
283 list_for_each_entry(dock_station, &dock_stations, sibling)
db350b08
SL
284 if (find_dock_dependent_device(dock_station, handle))
285 return 1;
c8f7a62c
LB
286
287 return 0;
288}
c8f7a62c
LB
289EXPORT_SYMBOL_GPL(is_dock_device);
290
291/**
292 * dock_present - see if the dock station is present.
293 * @ds: the dock station
294 *
295 * execute the _STA method. note that present does not
296 * imply that we are docked.
297 */
298static int dock_present(struct dock_station *ds)
299{
27663c58 300 unsigned long long sta;
c8f7a62c
LB
301 acpi_status status;
302
303 if (ds) {
304 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
305 if (ACPI_SUCCESS(status) && sta)
306 return 1;
307 }
308 return 0;
309}
310
c8f7a62c
LB
311/**
312 * dock_create_acpi_device - add new devices to acpi
313 * @handle - handle of the device to add
314 *
315 * This function will create a new acpi_device for the given
316 * handle if one does not exist already. This should cause
317 * acpi to scan for drivers for the given devices, and call
318 * matching driver's add routine.
c8f7a62c 319 */
472d963b 320static void dock_create_acpi_device(acpi_handle handle)
c8f7a62c 321{
202317a5 322 struct acpi_device *device = NULL;
c8f7a62c
LB
323 int ret;
324
202317a5
RW
325 acpi_bus_get_device(handle, &device);
326 if (!acpi_device_enumerated(device)) {
b8bd759a 327 ret = acpi_bus_scan(handle);
636458de 328 if (ret)
747479a3 329 pr_debug("error adding bus, %x\n", -ret);
c8f7a62c 330 }
c8f7a62c
LB
331}
332
333/**
334 * dock_remove_acpi_device - remove the acpi_device struct from acpi
335 * @handle - the handle of the device to remove
336 *
337 * Tell acpi to remove the acpi_device. This should cause any loaded
338 * driver to have it's remove routine called.
339 */
340static void dock_remove_acpi_device(acpi_handle handle)
341{
342 struct acpi_device *device;
c8f7a62c 343
bfee26db
RW
344 if (!acpi_bus_get_device(handle, &device))
345 acpi_bus_trim(device);
c8f7a62c
LB
346}
347
c8f7a62c 348/**
37f90877
RW
349 * hot_remove_dock_devices - Remove dock station devices.
350 * @ds: Dock station.
351 */
352static void hot_remove_dock_devices(struct dock_station *ds)
353{
354 struct dock_dependent_device *dd;
355
356 /*
357 * Walk the list in reverse order so that devices that have been added
358 * last are removed first (in case there are some indirect dependencies
359 * between them).
360 */
361 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
362 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
363
364 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
365 dock_remove_acpi_device(dd->handle);
366}
367
368/**
369 * hotplug_dock_devices - Insert devices on a dock station.
c8f7a62c 370 * @ds: the dock station
37f90877 371 * @event: either bus check or device check request
c8f7a62c
LB
372 *
373 * Some devices on the dock station need to have drivers called
374 * to perform hotplug operations after a dock event has occurred.
375 * Traverse the list of dock devices that have registered a
376 * hotplug handler, and call the handler.
377 */
378static void hotplug_dock_devices(struct dock_station *ds, u32 event)
379{
380 struct dock_dependent_device *dd;
381
f09ce741
RW
382 /* Call driver specific post-dock fixups. */
383 list_for_each_entry(dd, &ds->dependent_devices, list)
384 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
385
37f90877 386 /* Call driver specific hotplug functions. */
21a31013 387 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 388 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
c8f7a62c
LB
389
390 /*
37f90877
RW
391 * Now make sure that an acpi_device is created for each dependent
392 * device. That will cause scan handlers to be attached to device
393 * objects or acpi_drivers to be stopped/started if they are present.
c8f7a62c 394 */
37f90877
RW
395 list_for_each_entry(dd, &ds->dependent_devices, list)
396 dock_create_acpi_device(dd->handle);
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;
61b83695 517 int ret = -EINVAL;
c8f7a62c 518
21a31013
RW
519 if (WARN_ON(!context))
520 return -EINVAL;
521
db350b08 522 if (!dock_station_count)
c8f7a62c
LB
523 return -ENODEV;
524
525 /*
526 * make sure this handle is for a device dependent on the dock,
527 * this would include the dock station itself
528 */
50d716e4 529 list_for_each_entry(dock_station, &dock_stations, sibling) {
61b83695
SL
530 /*
531 * An ATA bay can be in a dock and itself can be ejected
3ad2f3fb 532 * separately, so there are two 'dock stations' which need the
61b83695
SL
533 * ops
534 */
db350b08 535 dd = find_dock_dependent_device(dock_station, handle);
21a31013 536 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
61b83695 537 ret = 0;
c8f7a62c
LB
538 }
539
61b83695 540 return ret;
c8f7a62c 541}
c8f7a62c
LB
542EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
543
544/**
545 * unregister_hotplug_dock_device - remove yourself from the hotplug list
546 * @handle: the acpi handle of the device
547 */
548void unregister_hotplug_dock_device(acpi_handle handle)
549{
550 struct dock_dependent_device *dd;
db350b08 551 struct dock_station *dock_station;
c8f7a62c 552
db350b08 553 if (!dock_station_count)
c8f7a62c
LB
554 return;
555
50d716e4 556 list_for_each_entry(dock_station, &dock_stations, sibling) {
db350b08
SL
557 dd = find_dock_dependent_device(dock_station, handle);
558 if (dd)
21a31013 559 dock_release_hotplug(dd);
db350b08 560 }
c8f7a62c 561}
c8f7a62c
LB
562EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
563
c80fdbe8 564/**
565 * handle_eject_request - handle an undock request checking for error conditions
566 *
567 * Check to make sure the dock device is still present, then undock and
568 * hotremove all the devices that may need removing.
569 */
570static int handle_eject_request(struct dock_station *ds, u32 event)
571{
c80fdbe8 572 if (dock_in_progress(ds))
573 return -EBUSY;
574
575 /*
576 * here we need to generate the undock
577 * event prior to actually doing the undock
578 * so that the device struct still exists.
afd7301d
HM
579 * Also, even send the dock event if the
580 * device is not present anymore
c80fdbe8 581 */
582 dock_event(ds, event, UNDOCK_EVENT);
afd7301d 583
37f90877 584 hot_remove_dock_devices(ds);
c80fdbe8 585 undock(ds);
c9b5471f
JL
586 acpi_evaluate_lck(ds->handle, 0);
587 acpi_evaluate_ej0(ds->handle);
c80fdbe8 588 if (dock_present(ds)) {
cd73018f 589 acpi_handle_err(ds->handle, "Unable to undock!\n");
c80fdbe8 590 return -EBUSY;
591 }
a0cd35fd 592 complete_undock(ds);
c80fdbe8 593 return 0;
594}
595
c8f7a62c 596/**
1e2380cd
RW
597 * dock_notify - Handle ACPI dock notification.
598 * @adev: Dock station's ACPI device object.
599 * @event: Event code.
c8f7a62c
LB
600 *
601 * If we are notified to dock, then check to see if the dock is
602 * present and then dock. Notify all drivers of the dock event,
c80fdbe8 603 * and then hotplug and devices that may need hotplugging.
c8f7a62c 604 */
1e2380cd 605int dock_notify(struct acpi_device *adev, u32 event)
c8f7a62c 606{
1e2380cd
RW
607 acpi_handle handle = adev->handle;
608 struct dock_station *ds = find_dock_station(handle);
db350b08 609 int surprise_removal = 0;
c8f7a62c 610
1e2380cd
RW
611 if (!ds)
612 return -ENODEV;
613
db350b08
SL
614 /*
615 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
616 * is sent and _DCK is present, it is assumed to mean an undock
617 * request.
618 */
619 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
620 event = ACPI_NOTIFY_EJECT_REQUEST;
621
622 /*
623 * dock station: BUS_CHECK - docked or surprise removal
624 * DEVICE_CHECK - undocked
625 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
626 *
627 * To simplify event handling, dock dependent device handler always
628 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
629 * ACPI_NOTIFY_EJECT_REQUEST for removal
630 */
c8f7a62c
LB
631 switch (event) {
632 case ACPI_NOTIFY_BUS_CHECK:
db350b08 633 case ACPI_NOTIFY_DEVICE_CHECK:
0a8e5c3d 634 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
c8f7a62c
LB
635 begin_dock(ds);
636 dock(ds);
637 if (!dock_present(ds)) {
cd73018f 638 acpi_handle_err(handle, "Unable to dock!\n");
8b59560a 639 complete_dock(ds);
c8f7a62c
LB
640 break;
641 }
c8f7a62c
LB
642 hotplug_dock_devices(ds, event);
643 complete_dock(ds);
644 dock_event(ds, event, DOCK_EVENT);
c9b5471f 645 acpi_evaluate_lck(ds->handle, 1);
3a37898d 646 acpi_update_all_gpes();
db350b08 647 break;
c8f7a62c 648 }
db350b08
SL
649 if (dock_present(ds) || dock_in_progress(ds))
650 break;
651 /* This is a surprise removal */
652 surprise_removal = 1;
653 event = ACPI_NOTIFY_EJECT_REQUEST;
654 /* Fall back */
c8f7a62c 655 case ACPI_NOTIFY_EJECT_REQUEST:
a0cd35fd 656 begin_undock(ds);
f730ae18
SL
657 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
658 || surprise_removal)
a0cd35fd
KCA
659 handle_eject_request(ds, event);
660 else
661 dock_event(ds, event, UNDOCK_EVENT);
c8f7a62c 662 break;
c8f7a62c 663 }
1e2380cd 664 return 0;
c8f7a62c
LB
665}
666
c80fdbe8 667/*
668 * show_docked - read method for "docked" file in sysfs
669 */
670static ssize_t show_docked(struct device *dev,
671 struct device_attribute *attr, char *buf)
672{
fe06fba2 673 struct dock_station *dock_station = dev->platform_data;
ab62f9cd 674 struct acpi_device *adev = NULL;
c80fdbe8 675
ab62f9cd
RW
676 acpi_bus_get_device(dock_station->handle, &adev);
677 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
c80fdbe8 678}
e5685b9d 679static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
c80fdbe8 680
a0cd35fd
KCA
681/*
682 * show_flags - read method for flags file in sysfs
683 */
684static ssize_t show_flags(struct device *dev,
685 struct device_attribute *attr, char *buf)
686{
fe06fba2 687 struct dock_station *dock_station = dev->platform_data;
a0cd35fd
KCA
688 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
689
690}
e5685b9d 691static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
a0cd35fd 692
c80fdbe8 693/*
694 * write_undock - write method for "undock" file in sysfs
695 */
696static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
697 const char *buf, size_t count)
698{
699 int ret;
fe06fba2 700 struct dock_station *dock_station = dev->platform_data;
c80fdbe8 701
702 if (!count)
703 return -EINVAL;
704
8112006f 705 acpi_scan_lock_acquire();
9171f834 706 begin_undock(dock_station);
c80fdbe8 707 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
8112006f 708 acpi_scan_lock_release();
c80fdbe8 709 return ret ? ret: count;
710}
e5685b9d 711static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
c80fdbe8 712
ac122bb6
IVE
713/*
714 * show_dock_uid - read method for "uid" file in sysfs
715 */
716static ssize_t show_dock_uid(struct device *dev,
717 struct device_attribute *attr, char *buf)
718{
27663c58 719 unsigned long long lbuf;
fe06fba2 720 struct dock_station *dock_station = dev->platform_data;
38ff4ffc
KCA
721 acpi_status status = acpi_evaluate_integer(dock_station->handle,
722 "_UID", NULL, &lbuf);
723 if (ACPI_FAILURE(status))
ac122bb6 724 return 0;
38ff4ffc 725
27663c58 726 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
ac122bb6 727}
e5685b9d 728static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
ac122bb6 729
8652b00f
SL
730static ssize_t show_dock_type(struct device *dev,
731 struct device_attribute *attr, char *buf)
732{
fe06fba2 733 struct dock_station *dock_station = dev->platform_data;
8652b00f
SL
734 char *type;
735
736 if (dock_station->flags & DOCK_IS_DOCK)
737 type = "dock_station";
738 else if (dock_station->flags & DOCK_IS_ATA)
739 type = "ata_bay";
740 else if (dock_station->flags & DOCK_IS_BAT)
741 type = "battery_bay";
742 else
743 type = "unknown";
744
745 return snprintf(buf, PAGE_SIZE, "%s\n", type);
746}
747static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
748
5f46c2f2
AC
749static struct attribute *dock_attributes[] = {
750 &dev_attr_docked.attr,
751 &dev_attr_flags.attr,
752 &dev_attr_undock.attr,
753 &dev_attr_uid.attr,
754 &dev_attr_type.attr,
755 NULL
756};
757
758static struct attribute_group dock_attribute_group = {
759 .attrs = dock_attributes
760};
761
c8f7a62c 762/**
1e2380cd
RW
763 * acpi_dock_add - Add a new dock station
764 * @adev: Dock station ACPI device object.
c8f7a62c 765 *
1e2380cd 766 * allocated and initialize a new dock station device.
c8f7a62c 767 */
1e2380cd 768void acpi_dock_add(struct acpi_device *adev)
c8f7a62c 769{
2efbca4d 770 struct dock_station *dock_station, ds = { NULL, };
1e2380cd 771 acpi_handle handle = adev->handle;
747479a3 772 struct platform_device *dd;
2efbca4d 773 int ret;
c8f7a62c 774
2efbca4d
RW
775 dd = platform_device_register_data(NULL, "dock", dock_station_count,
776 &ds, sizeof(ds));
747479a3 777 if (IS_ERR(dd))
1e2380cd 778 return;
747479a3
AC
779
780 dock_station = dd->dev.platform_data;
c8f7a62c 781
c8f7a62c 782 dock_station->handle = handle;
747479a3 783 dock_station->dock_device = dd;
c8f7a62c 784 dock_station->last_dock_time = jiffies - HZ;
747479a3 785
747479a3 786 INIT_LIST_HEAD(&dock_station->sibling);
747479a3 787 INIT_LIST_HEAD(&dock_station->dependent_devices);
a0cd35fd 788
9ef2a9a9 789 /* we want the dock device to send uevents */
747479a3 790 dev_set_uevent_suppress(&dd->dev, 0);
9ef2a9a9 791
c9b5471f 792 if (acpi_dock_match(handle))
db350b08 793 dock_station->flags |= DOCK_IS_DOCK;
c9b5471f 794 if (acpi_ata_match(handle))
db350b08 795 dock_station->flags |= DOCK_IS_ATA;
b43109fa 796 if (acpi_device_is_battery(adev))
db350b08
SL
797 dock_station->flags |= DOCK_IS_BAT;
798
747479a3 799 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
8652b00f 800 if (ret)
5f46c2f2 801 goto err_unregister;
671adbec 802
c8f7a62c 803 /* add the dock station as a device dependent on itself */
f69cfdd2
AC
804 ret = add_dock_dependent_device(dock_station, handle);
805 if (ret)
5f46c2f2 806 goto err_rmgroup;
c8f7a62c 807
db350b08 808 dock_station_count++;
50d716e4 809 list_add(&dock_station->sibling, &dock_stations);
1e2380cd
RW
810 adev->flags.is_dock_station = true;
811 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
812 dock_station_count);
813 return;
c8f7a62c 814
5f46c2f2 815err_rmgroup:
a30c4c5e 816 remove_dock_dependent_devices(dock_station);
747479a3 817 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
5f46c2f2 818err_unregister:
747479a3 819 platform_device_unregister(dd);
cd73018f 820 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
c8f7a62c 821}
This page took 0.584307 seconds and 5 git commands to generate.