]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/dock.c
ACPI / ATA: Add hotplug contexts to ACPI companions of SATA devices
[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 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 }
be27b3dc
RW
206 } else if (cb_type == DOCK_CALL_UEVENT) {
207 void (*uevent)(struct acpi_device *, u32);
208
209 uevent = adev->hp->uevent;
210 if (uevent) {
211 acpi_unlock_hp_context();
212 uevent(adev, event);
213 return;
214 }
edf5bf34
RW
215 } else {
216 int (*notify)(struct acpi_device *, u32);
217
be27b3dc 218 notify = adev->hp->notify;
edf5bf34
RW
219 if (notify) {
220 acpi_unlock_hp_context();
221 notify(adev, event);
222 return;
223 }
224 }
225
226 no_context:
227 acpi_unlock_hp_context();
228
21a31013
RW
229 mutex_lock(&hotplug_lock);
230
231 if (dd->hp_context) {
232 run = true;
233 dd->hp_refcount++;
f09ce741
RW
234 if (dd->hp_ops) {
235 switch (cb_type) {
236 case DOCK_CALL_FIXUP:
237 cb = dd->hp_ops->fixup;
238 break;
239 case DOCK_CALL_UEVENT:
240 cb = dd->hp_ops->uevent;
241 break;
242 default:
243 cb = dd->hp_ops->handler;
244 }
245 }
21a31013
RW
246 }
247
248 mutex_unlock(&hotplug_lock);
249
250 if (!run)
251 return;
252
253 if (cb)
3b52b21f 254 cb(dd->adev->handle, event, dd->hp_context);
21a31013
RW
255
256 dock_release_hotplug(dd);
c8f7a62c
LB
257}
258
1e2380cd
RW
259static struct dock_station *find_dock_station(acpi_handle handle)
260{
261 struct dock_station *ds;
262
263 list_for_each_entry(ds, &dock_stations, sibling)
264 if (ds->handle == handle)
265 return ds;
266
267 return NULL;
268}
269
c8f7a62c
LB
270/**
271 * find_dock_dependent_device - get a device dependent on this dock
272 * @ds: the dock station
3b52b21f 273 * @adev: ACPI device object to find.
c8f7a62c
LB
274 *
275 * iterate over the dependent device list for this dock. If the
276 * dependent device matches the handle, return.
277 */
278static struct dock_dependent_device *
3b52b21f 279find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
c8f7a62c
LB
280{
281 struct dock_dependent_device *dd;
282
ed633e70 283 list_for_each_entry(dd, &ds->dependent_devices, list)
3b52b21f 284 if (adev == dd->adev)
c8f7a62c 285 return dd;
ed633e70 286
c8f7a62c
LB
287 return NULL;
288}
289
1e2380cd
RW
290void register_dock_dependent_device(struct acpi_device *adev,
291 acpi_handle dshandle)
db350b08 292{
1e2380cd 293 struct dock_station *ds = find_dock_station(dshandle);
db350b08 294
3b52b21f
RW
295 if (ds && !find_dock_dependent_device(ds, adev))
296 add_dock_dependent_device(ds, adev);
db350b08
SL
297}
298
1e2380cd
RW
299/*****************************************************************************
300 * Dock functions *
301 *****************************************************************************/
db350b08 302
c8f7a62c
LB
303/**
304 * is_dock_device - see if a device is on a dock station
3b52b21f 305 * @adev: ACPI device object to check.
c8f7a62c
LB
306 *
307 * If this device is either the dock station itself,
308 * or is a device dependent on the dock station, then it
309 * is a dock device
310 */
3b52b21f 311int is_dock_device(struct acpi_device *adev)
c8f7a62c 312{
db350b08
SL
313 struct dock_station *dock_station;
314
315 if (!dock_station_count)
c8f7a62c
LB
316 return 0;
317
3b52b21f 318 if (acpi_dock_match(adev->handle))
c8f7a62c 319 return 1;
747479a3
AC
320
321 list_for_each_entry(dock_station, &dock_stations, sibling)
3b52b21f 322 if (find_dock_dependent_device(dock_station, adev))
db350b08 323 return 1;
c8f7a62c
LB
324
325 return 0;
326}
c8f7a62c
LB
327EXPORT_SYMBOL_GPL(is_dock_device);
328
329/**
330 * dock_present - see if the dock station is present.
331 * @ds: the dock station
332 *
333 * execute the _STA method. note that present does not
334 * imply that we are docked.
335 */
336static int dock_present(struct dock_station *ds)
337{
27663c58 338 unsigned long long sta;
c8f7a62c
LB
339 acpi_status status;
340
341 if (ds) {
342 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
343 if (ACPI_SUCCESS(status) && sta)
344 return 1;
345 }
346 return 0;
347}
348
c8f7a62c 349/**
37f90877
RW
350 * hot_remove_dock_devices - Remove dock station devices.
351 * @ds: Dock station.
352 */
353static void hot_remove_dock_devices(struct dock_station *ds)
354{
355 struct dock_dependent_device *dd;
356
357 /*
358 * Walk the list in reverse order so that devices that have been added
359 * last are removed first (in case there are some indirect dependencies
360 * between them).
361 */
362 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
363 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
364
365 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
3b52b21f 366 acpi_bus_trim(dd->adev);
37f90877
RW
367}
368
369/**
370 * hotplug_dock_devices - Insert devices on a dock station.
c8f7a62c 371 * @ds: the dock station
37f90877 372 * @event: either bus check or device check request
c8f7a62c
LB
373 *
374 * Some devices on the dock station need to have drivers called
375 * to perform hotplug operations after a dock event has occurred.
376 * Traverse the list of dock devices that have registered a
377 * hotplug handler, and call the handler.
378 */
379static void hotplug_dock_devices(struct dock_station *ds, u32 event)
380{
381 struct dock_dependent_device *dd;
382
f09ce741
RW
383 /* Call driver specific post-dock fixups. */
384 list_for_each_entry(dd, &ds->dependent_devices, list)
385 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
386
37f90877 387 /* Call driver specific hotplug functions. */
21a31013 388 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 389 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
c8f7a62c
LB
390
391 /*
3b52b21f
RW
392 * Check if all devices have been enumerated already. If not, run
393 * acpi_bus_scan() for them and that will cause scan handlers to be
394 * attached to device objects or acpi_drivers to be stopped/started if
395 * they are present.
c8f7a62c 396 */
3b52b21f
RW
397 list_for_each_entry(dd, &ds->dependent_devices, list) {
398 struct acpi_device *adev = dd->adev;
399
400 if (!acpi_device_enumerated(adev)) {
401 int ret = acpi_bus_scan(adev->handle);
402 if (ret)
403 dev_dbg(&adev->dev, "scan error %d\n", -ret);
404 }
405 }
c8f7a62c
LB
406}
407
408static void dock_event(struct dock_station *ds, u32 event, int num)
409{
db350b08 410 struct device *dev = &ds->dock_device->dev;
66b56821 411 char event_string[13];
79a8f70b 412 char *envp[] = { event_string, NULL };
1253f7aa 413 struct dock_dependent_device *dd;
79a8f70b
KCA
414
415 if (num == UNDOCK_EVENT)
66b56821 416 sprintf(event_string, "EVENT=undock");
79a8f70b 417 else
66b56821 418 sprintf(event_string, "EVENT=dock");
79a8f70b 419
5669021e 420 /*
8ea86e0b
KCA
421 * Indicate that the status of the dock station has
422 * changed.
5669021e 423 */
1253f7aa
SL
424 if (num == DOCK_EVENT)
425 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
426
21a31013 427 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 428 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
747479a3 429
1253f7aa
SL
430 if (num != DOCK_EVENT)
431 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
c8f7a62c
LB
432}
433
c8f7a62c
LB
434/**
435 * handle_dock - handle a dock event
436 * @ds: the dock station
437 * @dock: to dock, or undock - that is the question
438 *
439 * Execute the _DCK method in response to an acpi event
440 */
441static void handle_dock(struct dock_station *ds, int dock)
442{
443 acpi_status status;
444 struct acpi_object_list arg_list;
445 union acpi_object arg;
6a868e17 446 unsigned long long value;
c8f7a62c 447
cd73018f 448 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
c8f7a62c
LB
449
450 /* _DCK method has one argument */
451 arg_list.count = 1;
452 arg_list.pointer = &arg;
453 arg.type = ACPI_TYPE_INTEGER;
454 arg.integer.value = dock;
6a868e17 455 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
db350b08 456 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
cd73018f
TK
457 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
458 status);
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;
3b52b21f 526 struct acpi_device *adev;
61b83695 527 int ret = -EINVAL;
c8f7a62c 528
21a31013
RW
529 if (WARN_ON(!context))
530 return -EINVAL;
531
db350b08 532 if (!dock_station_count)
c8f7a62c
LB
533 return -ENODEV;
534
3b52b21f
RW
535 ret = acpi_bus_get_device(handle, &adev);
536 if (ret)
537 return ret;
538
c8f7a62c
LB
539 /*
540 * make sure this handle is for a device dependent on the dock,
541 * this would include the dock station itself
542 */
50d716e4 543 list_for_each_entry(dock_station, &dock_stations, sibling) {
61b83695
SL
544 /*
545 * An ATA bay can be in a dock and itself can be ejected
3ad2f3fb 546 * separately, so there are two 'dock stations' which need the
61b83695
SL
547 * ops
548 */
3b52b21f 549 dd = find_dock_dependent_device(dock_station, adev);
21a31013 550 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
61b83695 551 ret = 0;
c8f7a62c
LB
552 }
553
61b83695 554 return ret;
c8f7a62c 555}
c8f7a62c
LB
556EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
557
558/**
559 * unregister_hotplug_dock_device - remove yourself from the hotplug list
560 * @handle: the acpi handle of the device
561 */
562void unregister_hotplug_dock_device(acpi_handle handle)
563{
564 struct dock_dependent_device *dd;
db350b08 565 struct dock_station *dock_station;
3b52b21f 566 struct acpi_device *adev;
c8f7a62c 567
db350b08 568 if (!dock_station_count)
c8f7a62c
LB
569 return;
570
3b52b21f
RW
571 if (acpi_bus_get_device(handle, &adev))
572 return;
573
50d716e4 574 list_for_each_entry(dock_station, &dock_stations, sibling) {
3b52b21f 575 dd = find_dock_dependent_device(dock_station, adev);
db350b08 576 if (dd)
21a31013 577 dock_release_hotplug(dd);
db350b08 578 }
c8f7a62c 579}
c8f7a62c
LB
580EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
581
c80fdbe8 582/**
583 * handle_eject_request - handle an undock request checking for error conditions
584 *
585 * Check to make sure the dock device is still present, then undock and
586 * hotremove all the devices that may need removing.
587 */
588static int handle_eject_request(struct dock_station *ds, u32 event)
589{
c80fdbe8 590 if (dock_in_progress(ds))
591 return -EBUSY;
592
593 /*
594 * here we need to generate the undock
595 * event prior to actually doing the undock
596 * so that the device struct still exists.
afd7301d
HM
597 * Also, even send the dock event if the
598 * device is not present anymore
c80fdbe8 599 */
600 dock_event(ds, event, UNDOCK_EVENT);
afd7301d 601
37f90877 602 hot_remove_dock_devices(ds);
c80fdbe8 603 undock(ds);
c9b5471f
JL
604 acpi_evaluate_lck(ds->handle, 0);
605 acpi_evaluate_ej0(ds->handle);
c80fdbe8 606 if (dock_present(ds)) {
cd73018f 607 acpi_handle_err(ds->handle, "Unable to undock!\n");
c80fdbe8 608 return -EBUSY;
609 }
a0cd35fd 610 complete_undock(ds);
c80fdbe8 611 return 0;
612}
613
c8f7a62c 614/**
1e2380cd
RW
615 * dock_notify - Handle ACPI dock notification.
616 * @adev: Dock station's ACPI device object.
617 * @event: Event code.
c8f7a62c
LB
618 *
619 * If we are notified to dock, then check to see if the dock is
620 * present and then dock. Notify all drivers of the dock event,
c80fdbe8 621 * and then hotplug and devices that may need hotplugging.
c8f7a62c 622 */
1e2380cd 623int dock_notify(struct acpi_device *adev, u32 event)
c8f7a62c 624{
1e2380cd
RW
625 acpi_handle handle = adev->handle;
626 struct dock_station *ds = find_dock_station(handle);
db350b08 627 int surprise_removal = 0;
c8f7a62c 628
1e2380cd
RW
629 if (!ds)
630 return -ENODEV;
631
db350b08
SL
632 /*
633 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
634 * is sent and _DCK is present, it is assumed to mean an undock
635 * request.
636 */
637 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
638 event = ACPI_NOTIFY_EJECT_REQUEST;
639
640 /*
641 * dock station: BUS_CHECK - docked or surprise removal
642 * DEVICE_CHECK - undocked
643 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
644 *
645 * To simplify event handling, dock dependent device handler always
646 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
647 * ACPI_NOTIFY_EJECT_REQUEST for removal
648 */
c8f7a62c
LB
649 switch (event) {
650 case ACPI_NOTIFY_BUS_CHECK:
db350b08 651 case ACPI_NOTIFY_DEVICE_CHECK:
0a8e5c3d 652 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
c8f7a62c
LB
653 begin_dock(ds);
654 dock(ds);
655 if (!dock_present(ds)) {
cd73018f 656 acpi_handle_err(handle, "Unable to dock!\n");
8b59560a 657 complete_dock(ds);
c8f7a62c
LB
658 break;
659 }
c8f7a62c
LB
660 hotplug_dock_devices(ds, event);
661 complete_dock(ds);
662 dock_event(ds, event, DOCK_EVENT);
c9b5471f 663 acpi_evaluate_lck(ds->handle, 1);
3a37898d 664 acpi_update_all_gpes();
db350b08 665 break;
c8f7a62c 666 }
db350b08
SL
667 if (dock_present(ds) || dock_in_progress(ds))
668 break;
669 /* This is a surprise removal */
670 surprise_removal = 1;
671 event = ACPI_NOTIFY_EJECT_REQUEST;
672 /* Fall back */
c8f7a62c 673 case ACPI_NOTIFY_EJECT_REQUEST:
a0cd35fd 674 begin_undock(ds);
f730ae18
SL
675 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
676 || surprise_removal)
a0cd35fd
KCA
677 handle_eject_request(ds, event);
678 else
679 dock_event(ds, event, UNDOCK_EVENT);
c8f7a62c 680 break;
c8f7a62c 681 }
1e2380cd 682 return 0;
c8f7a62c
LB
683}
684
c80fdbe8 685/*
686 * show_docked - read method for "docked" file in sysfs
687 */
688static ssize_t show_docked(struct device *dev,
689 struct device_attribute *attr, char *buf)
690{
fe06fba2 691 struct dock_station *dock_station = dev->platform_data;
ab62f9cd 692 struct acpi_device *adev = NULL;
c80fdbe8 693
ab62f9cd
RW
694 acpi_bus_get_device(dock_station->handle, &adev);
695 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
c80fdbe8 696}
e5685b9d 697static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
c80fdbe8 698
a0cd35fd
KCA
699/*
700 * show_flags - read method for flags file in sysfs
701 */
702static ssize_t show_flags(struct device *dev,
703 struct device_attribute *attr, char *buf)
704{
fe06fba2 705 struct dock_station *dock_station = dev->platform_data;
a0cd35fd
KCA
706 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
707
708}
e5685b9d 709static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
a0cd35fd 710
c80fdbe8 711/*
712 * write_undock - write method for "undock" file in sysfs
713 */
714static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
715 const char *buf, size_t count)
716{
717 int ret;
fe06fba2 718 struct dock_station *dock_station = dev->platform_data;
c80fdbe8 719
720 if (!count)
721 return -EINVAL;
722
8112006f 723 acpi_scan_lock_acquire();
9171f834 724 begin_undock(dock_station);
c80fdbe8 725 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
8112006f 726 acpi_scan_lock_release();
c80fdbe8 727 return ret ? ret: count;
728}
e5685b9d 729static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
c80fdbe8 730
ac122bb6
IVE
731/*
732 * show_dock_uid - read method for "uid" file in sysfs
733 */
734static ssize_t show_dock_uid(struct device *dev,
735 struct device_attribute *attr, char *buf)
736{
27663c58 737 unsigned long long lbuf;
fe06fba2 738 struct dock_station *dock_station = dev->platform_data;
38ff4ffc
KCA
739 acpi_status status = acpi_evaluate_integer(dock_station->handle,
740 "_UID", NULL, &lbuf);
741 if (ACPI_FAILURE(status))
ac122bb6 742 return 0;
38ff4ffc 743
27663c58 744 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
ac122bb6 745}
e5685b9d 746static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
ac122bb6 747
8652b00f
SL
748static ssize_t show_dock_type(struct device *dev,
749 struct device_attribute *attr, char *buf)
750{
fe06fba2 751 struct dock_station *dock_station = dev->platform_data;
8652b00f
SL
752 char *type;
753
754 if (dock_station->flags & DOCK_IS_DOCK)
755 type = "dock_station";
756 else if (dock_station->flags & DOCK_IS_ATA)
757 type = "ata_bay";
758 else if (dock_station->flags & DOCK_IS_BAT)
759 type = "battery_bay";
760 else
761 type = "unknown";
762
763 return snprintf(buf, PAGE_SIZE, "%s\n", type);
764}
765static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
766
5f46c2f2
AC
767static struct attribute *dock_attributes[] = {
768 &dev_attr_docked.attr,
769 &dev_attr_flags.attr,
770 &dev_attr_undock.attr,
771 &dev_attr_uid.attr,
772 &dev_attr_type.attr,
773 NULL
774};
775
776static struct attribute_group dock_attribute_group = {
777 .attrs = dock_attributes
778};
779
c8f7a62c 780/**
1e2380cd
RW
781 * acpi_dock_add - Add a new dock station
782 * @adev: Dock station ACPI device object.
c8f7a62c 783 *
1e2380cd 784 * allocated and initialize a new dock station device.
c8f7a62c 785 */
1e2380cd 786void acpi_dock_add(struct acpi_device *adev)
c8f7a62c 787{
2efbca4d 788 struct dock_station *dock_station, ds = { NULL, };
af887449 789 struct platform_device_info pdevinfo;
1e2380cd 790 acpi_handle handle = adev->handle;
747479a3 791 struct platform_device *dd;
2efbca4d 792 int ret;
c8f7a62c 793
af887449
RW
794 memset(&pdevinfo, 0, sizeof(pdevinfo));
795 pdevinfo.name = "dock";
796 pdevinfo.id = dock_station_count;
797 pdevinfo.acpi_node.companion = adev;
798 pdevinfo.data = &ds;
799 pdevinfo.size_data = sizeof(ds);
800 dd = platform_device_register_full(&pdevinfo);
747479a3 801 if (IS_ERR(dd))
1e2380cd 802 return;
747479a3
AC
803
804 dock_station = dd->dev.platform_data;
c8f7a62c 805
c8f7a62c 806 dock_station->handle = handle;
747479a3 807 dock_station->dock_device = dd;
c8f7a62c 808 dock_station->last_dock_time = jiffies - HZ;
747479a3 809
747479a3 810 INIT_LIST_HEAD(&dock_station->sibling);
747479a3 811 INIT_LIST_HEAD(&dock_station->dependent_devices);
a0cd35fd 812
9ef2a9a9 813 /* we want the dock device to send uevents */
747479a3 814 dev_set_uevent_suppress(&dd->dev, 0);
9ef2a9a9 815
c9b5471f 816 if (acpi_dock_match(handle))
db350b08 817 dock_station->flags |= DOCK_IS_DOCK;
c9b5471f 818 if (acpi_ata_match(handle))
db350b08 819 dock_station->flags |= DOCK_IS_ATA;
b43109fa 820 if (acpi_device_is_battery(adev))
db350b08
SL
821 dock_station->flags |= DOCK_IS_BAT;
822
747479a3 823 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
8652b00f 824 if (ret)
5f46c2f2 825 goto err_unregister;
671adbec 826
c8f7a62c 827 /* add the dock station as a device dependent on itself */
3b52b21f 828 ret = add_dock_dependent_device(dock_station, adev);
f69cfdd2 829 if (ret)
5f46c2f2 830 goto err_rmgroup;
c8f7a62c 831
db350b08 832 dock_station_count++;
50d716e4 833 list_add(&dock_station->sibling, &dock_stations);
1e2380cd
RW
834 adev->flags.is_dock_station = true;
835 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
836 dock_station_count);
837 return;
c8f7a62c 838
5f46c2f2 839err_rmgroup:
a30c4c5e 840 remove_dock_dependent_devices(dock_station);
747479a3 841 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
5f46c2f2 842err_unregister:
747479a3 843 platform_device_unregister(dd);
cd73018f 844 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
c8f7a62c 845}