Merge tag 'pci-v3.15-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[deliverable/linux.git] / drivers / acpi / dock.c
CommitLineData
c8f7a62c
LB
1/*
2 * dock.c - ACPI dock station driver
3 *
8cc25681
RW
4 * Copyright (C) 2006, 2014, Intel Corp.
5 * Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
6 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
c8f7a62c
LB
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
5a0e3ad6 29#include <linux/slab.h>
c8f7a62c
LB
30#include <linux/init.h>
31#include <linux/types.h>
32#include <linux/notifier.h>
671adbec 33#include <linux/platform_device.h>
914e2637 34#include <linux/jiffies.h>
62a6d7fd 35#include <linux/stddef.h>
cd73018f 36#include <linux/acpi.h>
c8f7a62c 37
3f9eed5c
R
38#include "internal.h"
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;
c8f7a62c
LB
71
72struct dock_dependent_device {
73 struct list_head list;
3b52b21f 74 struct acpi_device *adev;
c8f7a62c
LB
75};
76
77#define DOCK_DOCKING 0x00000001
a0cd35fd 78#define DOCK_UNDOCKING 0x00000002
db350b08
SL
79#define DOCK_IS_DOCK 0x00000010
80#define DOCK_IS_ATA 0x00000020
81#define DOCK_IS_BAT 0x00000040
5669021e
KCA
82#define DOCK_EVENT 3
83#define UNDOCK_EVENT 2
c8f7a62c 84
f09ce741
RW
85enum dock_callback_type {
86 DOCK_CALL_HANDLER,
87 DOCK_CALL_FIXUP,
88 DOCK_CALL_UEVENT,
89};
90
c8f7a62c
LB
91/*****************************************************************************
92 * Dock Dependent device functions *
93 *****************************************************************************/
94/**
f69cfdd2 95 * add_dock_dependent_device - associate a device with the dock station
3b52b21f
RW
96 * @ds: Dock station.
97 * @adev: Dependent ACPI device object.
c8f7a62c 98 *
f69cfdd2 99 * Add the dependent device to the dock's dependent device list.
c8f7a62c 100 */
3b52b21f
RW
101static int add_dock_dependent_device(struct dock_station *ds,
102 struct acpi_device *adev)
c8f7a62c
LB
103{
104 struct dock_dependent_device *dd;
105
106 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
f69cfdd2
AC
107 if (!dd)
108 return -ENOMEM;
109
3b52b21f 110 dd->adev = adev;
f69cfdd2 111 INIT_LIST_HEAD(&dd->list);
c8f7a62c 112 list_add_tail(&dd->list, &ds->dependent_devices);
f69cfdd2
AC
113
114 return 0;
c8f7a62c
LB
115}
116
21a31013 117static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
f09ce741 118 enum dock_callback_type cb_type)
21a31013 119{
edf5bf34 120 struct acpi_device *adev = dd->adev;
21a31013 121
edf5bf34
RW
122 acpi_lock_hp_context();
123
124 if (!adev->hp)
2f16817d 125 goto out;
edf5bf34
RW
126
127 if (cb_type == DOCK_CALL_FIXUP) {
128 void (*fixup)(struct acpi_device *);
129
130 fixup = adev->hp->fixup;
131 if (fixup) {
132 acpi_unlock_hp_context();
133 fixup(adev);
134 return;
135 }
be27b3dc
RW
136 } else if (cb_type == DOCK_CALL_UEVENT) {
137 void (*uevent)(struct acpi_device *, u32);
138
139 uevent = adev->hp->uevent;
140 if (uevent) {
141 acpi_unlock_hp_context();
142 uevent(adev, event);
143 return;
144 }
edf5bf34
RW
145 } else {
146 int (*notify)(struct acpi_device *, u32);
147
be27b3dc 148 notify = adev->hp->notify;
edf5bf34
RW
149 if (notify) {
150 acpi_unlock_hp_context();
151 notify(adev, event);
152 return;
153 }
154 }
155
2f16817d 156 out:
edf5bf34 157 acpi_unlock_hp_context();
c8f7a62c
LB
158}
159
1e2380cd
RW
160static struct dock_station *find_dock_station(acpi_handle handle)
161{
162 struct dock_station *ds;
163
164 list_for_each_entry(ds, &dock_stations, sibling)
165 if (ds->handle == handle)
166 return ds;
167
168 return NULL;
169}
170
c8f7a62c
LB
171/**
172 * find_dock_dependent_device - get a device dependent on this dock
173 * @ds: the dock station
3b52b21f 174 * @adev: ACPI device object to find.
c8f7a62c
LB
175 *
176 * iterate over the dependent device list for this dock. If the
177 * dependent device matches the handle, return.
178 */
179static struct dock_dependent_device *
3b52b21f 180find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
c8f7a62c
LB
181{
182 struct dock_dependent_device *dd;
183
ed633e70 184 list_for_each_entry(dd, &ds->dependent_devices, list)
3b52b21f 185 if (adev == dd->adev)
c8f7a62c 186 return dd;
ed633e70 187
c8f7a62c
LB
188 return NULL;
189}
190
1e2380cd
RW
191void register_dock_dependent_device(struct acpi_device *adev,
192 acpi_handle dshandle)
db350b08 193{
1e2380cd 194 struct dock_station *ds = find_dock_station(dshandle);
db350b08 195
3b52b21f
RW
196 if (ds && !find_dock_dependent_device(ds, adev))
197 add_dock_dependent_device(ds, adev);
db350b08
SL
198}
199
1e2380cd
RW
200/*****************************************************************************
201 * Dock functions *
202 *****************************************************************************/
db350b08 203
c8f7a62c
LB
204/**
205 * is_dock_device - see if a device is on a dock station
3b52b21f 206 * @adev: ACPI device object to check.
c8f7a62c
LB
207 *
208 * If this device is either the dock station itself,
209 * or is a device dependent on the dock station, then it
210 * is a dock device
211 */
3b52b21f 212int is_dock_device(struct acpi_device *adev)
c8f7a62c 213{
db350b08
SL
214 struct dock_station *dock_station;
215
216 if (!dock_station_count)
c8f7a62c
LB
217 return 0;
218
3b52b21f 219 if (acpi_dock_match(adev->handle))
c8f7a62c 220 return 1;
747479a3
AC
221
222 list_for_each_entry(dock_station, &dock_stations, sibling)
3b52b21f 223 if (find_dock_dependent_device(dock_station, adev))
db350b08 224 return 1;
c8f7a62c
LB
225
226 return 0;
227}
c8f7a62c
LB
228EXPORT_SYMBOL_GPL(is_dock_device);
229
230/**
231 * dock_present - see if the dock station is present.
232 * @ds: the dock station
233 *
234 * execute the _STA method. note that present does not
235 * imply that we are docked.
236 */
237static int dock_present(struct dock_station *ds)
238{
27663c58 239 unsigned long long sta;
c8f7a62c
LB
240 acpi_status status;
241
242 if (ds) {
243 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
244 if (ACPI_SUCCESS(status) && sta)
245 return 1;
246 }
247 return 0;
248}
249
c8f7a62c 250/**
37f90877
RW
251 * hot_remove_dock_devices - Remove dock station devices.
252 * @ds: Dock station.
253 */
254static void hot_remove_dock_devices(struct dock_station *ds)
255{
256 struct dock_dependent_device *dd;
257
258 /*
259 * Walk the list in reverse order so that devices that have been added
260 * last are removed first (in case there are some indirect dependencies
261 * between them).
262 */
263 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
264 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
265
266 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
3b52b21f 267 acpi_bus_trim(dd->adev);
37f90877
RW
268}
269
270/**
271 * hotplug_dock_devices - Insert devices on a dock station.
c8f7a62c 272 * @ds: the dock station
37f90877 273 * @event: either bus check or device check request
c8f7a62c
LB
274 *
275 * Some devices on the dock station need to have drivers called
276 * to perform hotplug operations after a dock event has occurred.
277 * Traverse the list of dock devices that have registered a
278 * hotplug handler, and call the handler.
279 */
280static void hotplug_dock_devices(struct dock_station *ds, u32 event)
281{
282 struct dock_dependent_device *dd;
283
f09ce741
RW
284 /* Call driver specific post-dock fixups. */
285 list_for_each_entry(dd, &ds->dependent_devices, list)
286 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
287
37f90877 288 /* Call driver specific hotplug functions. */
21a31013 289 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 290 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
c8f7a62c
LB
291
292 /*
3b52b21f
RW
293 * Check if all devices have been enumerated already. If not, run
294 * acpi_bus_scan() for them and that will cause scan handlers to be
295 * attached to device objects or acpi_drivers to be stopped/started if
296 * they are present.
c8f7a62c 297 */
3b52b21f
RW
298 list_for_each_entry(dd, &ds->dependent_devices, list) {
299 struct acpi_device *adev = dd->adev;
300
301 if (!acpi_device_enumerated(adev)) {
302 int ret = acpi_bus_scan(adev->handle);
303 if (ret)
304 dev_dbg(&adev->dev, "scan error %d\n", -ret);
305 }
306 }
c8f7a62c
LB
307}
308
309static void dock_event(struct dock_station *ds, u32 event, int num)
310{
db350b08 311 struct device *dev = &ds->dock_device->dev;
66b56821 312 char event_string[13];
79a8f70b 313 char *envp[] = { event_string, NULL };
1253f7aa 314 struct dock_dependent_device *dd;
79a8f70b
KCA
315
316 if (num == UNDOCK_EVENT)
66b56821 317 sprintf(event_string, "EVENT=undock");
79a8f70b 318 else
66b56821 319 sprintf(event_string, "EVENT=dock");
79a8f70b 320
5669021e 321 /*
8ea86e0b
KCA
322 * Indicate that the status of the dock station has
323 * changed.
5669021e 324 */
1253f7aa
SL
325 if (num == DOCK_EVENT)
326 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
327
21a31013 328 list_for_each_entry(dd, &ds->dependent_devices, list)
f09ce741 329 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
747479a3 330
1253f7aa
SL
331 if (num != DOCK_EVENT)
332 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
c8f7a62c
LB
333}
334
c8f7a62c
LB
335/**
336 * handle_dock - handle a dock event
337 * @ds: the dock station
338 * @dock: to dock, or undock - that is the question
339 *
340 * Execute the _DCK method in response to an acpi event
341 */
342static void handle_dock(struct dock_station *ds, int dock)
343{
344 acpi_status status;
345 struct acpi_object_list arg_list;
346 union acpi_object arg;
6a868e17 347 unsigned long long value;
c8f7a62c 348
cd73018f 349 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
c8f7a62c
LB
350
351 /* _DCK method has one argument */
352 arg_list.count = 1;
353 arg_list.pointer = &arg;
354 arg.type = ACPI_TYPE_INTEGER;
355 arg.integer.value = dock;
6a868e17 356 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
db350b08 357 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
cd73018f
TK
358 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
359 status);
c8f7a62c
LB
360}
361
362static inline void dock(struct dock_station *ds)
363{
364 handle_dock(ds, 1);
365}
366
367static inline void undock(struct dock_station *ds)
368{
369 handle_dock(ds, 0);
370}
371
372static inline void begin_dock(struct dock_station *ds)
373{
374 ds->flags |= DOCK_DOCKING;
375}
376
377static inline void complete_dock(struct dock_station *ds)
378{
379 ds->flags &= ~(DOCK_DOCKING);
380 ds->last_dock_time = jiffies;
381}
382
a0cd35fd
KCA
383static inline void begin_undock(struct dock_station *ds)
384{
385 ds->flags |= DOCK_UNDOCKING;
386}
387
388static inline void complete_undock(struct dock_station *ds)
389{
390 ds->flags &= ~(DOCK_UNDOCKING);
391}
392
c8f7a62c
LB
393/**
394 * dock_in_progress - see if we are in the middle of handling a dock event
395 * @ds: the dock station
396 *
397 * Sometimes while docking, false dock events can be sent to the driver
398 * because good connections aren't made or some other reason. Ignore these
399 * if we are in the middle of doing something.
400 */
401static int dock_in_progress(struct dock_station *ds)
402{
403 if ((ds->flags & DOCK_DOCKING) ||
404 time_before(jiffies, (ds->last_dock_time + HZ)))
405 return 1;
406 return 0;
407}
408
c80fdbe8 409/**
410 * handle_eject_request - handle an undock request checking for error conditions
411 *
412 * Check to make sure the dock device is still present, then undock and
413 * hotremove all the devices that may need removing.
414 */
415static int handle_eject_request(struct dock_station *ds, u32 event)
416{
c80fdbe8 417 if (dock_in_progress(ds))
418 return -EBUSY;
419
420 /*
421 * here we need to generate the undock
422 * event prior to actually doing the undock
423 * so that the device struct still exists.
afd7301d
HM
424 * Also, even send the dock event if the
425 * device is not present anymore
c80fdbe8 426 */
427 dock_event(ds, event, UNDOCK_EVENT);
afd7301d 428
37f90877 429 hot_remove_dock_devices(ds);
c80fdbe8 430 undock(ds);
c9b5471f
JL
431 acpi_evaluate_lck(ds->handle, 0);
432 acpi_evaluate_ej0(ds->handle);
c80fdbe8 433 if (dock_present(ds)) {
cd73018f 434 acpi_handle_err(ds->handle, "Unable to undock!\n");
c80fdbe8 435 return -EBUSY;
436 }
a0cd35fd 437 complete_undock(ds);
c80fdbe8 438 return 0;
439}
440
c8f7a62c 441/**
1e2380cd
RW
442 * dock_notify - Handle ACPI dock notification.
443 * @adev: Dock station's ACPI device object.
444 * @event: Event code.
c8f7a62c
LB
445 *
446 * If we are notified to dock, then check to see if the dock is
447 * present and then dock. Notify all drivers of the dock event,
c80fdbe8 448 * and then hotplug and devices that may need hotplugging.
c8f7a62c 449 */
1e2380cd 450int dock_notify(struct acpi_device *adev, u32 event)
c8f7a62c 451{
1e2380cd
RW
452 acpi_handle handle = adev->handle;
453 struct dock_station *ds = find_dock_station(handle);
db350b08 454 int surprise_removal = 0;
c8f7a62c 455
1e2380cd
RW
456 if (!ds)
457 return -ENODEV;
458
db350b08
SL
459 /*
460 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
461 * is sent and _DCK is present, it is assumed to mean an undock
462 * request.
463 */
464 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
465 event = ACPI_NOTIFY_EJECT_REQUEST;
466
467 /*
468 * dock station: BUS_CHECK - docked or surprise removal
469 * DEVICE_CHECK - undocked
470 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
471 *
472 * To simplify event handling, dock dependent device handler always
473 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
474 * ACPI_NOTIFY_EJECT_REQUEST for removal
475 */
c8f7a62c
LB
476 switch (event) {
477 case ACPI_NOTIFY_BUS_CHECK:
db350b08 478 case ACPI_NOTIFY_DEVICE_CHECK:
0a8e5c3d 479 if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
c8f7a62c
LB
480 begin_dock(ds);
481 dock(ds);
482 if (!dock_present(ds)) {
cd73018f 483 acpi_handle_err(handle, "Unable to dock!\n");
8b59560a 484 complete_dock(ds);
c8f7a62c
LB
485 break;
486 }
c8f7a62c
LB
487 hotplug_dock_devices(ds, event);
488 complete_dock(ds);
489 dock_event(ds, event, DOCK_EVENT);
c9b5471f 490 acpi_evaluate_lck(ds->handle, 1);
3a37898d 491 acpi_update_all_gpes();
db350b08 492 break;
c8f7a62c 493 }
db350b08
SL
494 if (dock_present(ds) || dock_in_progress(ds))
495 break;
496 /* This is a surprise removal */
497 surprise_removal = 1;
498 event = ACPI_NOTIFY_EJECT_REQUEST;
499 /* Fall back */
c8f7a62c 500 case ACPI_NOTIFY_EJECT_REQUEST:
a0cd35fd 501 begin_undock(ds);
f730ae18
SL
502 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
503 || surprise_removal)
a0cd35fd
KCA
504 handle_eject_request(ds, event);
505 else
506 dock_event(ds, event, UNDOCK_EVENT);
c8f7a62c 507 break;
c8f7a62c 508 }
1e2380cd 509 return 0;
c8f7a62c
LB
510}
511
c80fdbe8 512/*
513 * show_docked - read method for "docked" file in sysfs
514 */
515static ssize_t show_docked(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
fe06fba2 518 struct dock_station *dock_station = dev->platform_data;
ab62f9cd 519 struct acpi_device *adev = NULL;
c80fdbe8 520
ab62f9cd
RW
521 acpi_bus_get_device(dock_station->handle, &adev);
522 return snprintf(buf, PAGE_SIZE, "%u\n", acpi_device_enumerated(adev));
c80fdbe8 523}
e5685b9d 524static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
c80fdbe8 525
a0cd35fd
KCA
526/*
527 * show_flags - read method for flags file in sysfs
528 */
529static ssize_t show_flags(struct device *dev,
530 struct device_attribute *attr, char *buf)
531{
fe06fba2 532 struct dock_station *dock_station = dev->platform_data;
a0cd35fd
KCA
533 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
534
535}
e5685b9d 536static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
a0cd35fd 537
c80fdbe8 538/*
539 * write_undock - write method for "undock" file in sysfs
540 */
541static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
542 const char *buf, size_t count)
543{
544 int ret;
fe06fba2 545 struct dock_station *dock_station = dev->platform_data;
c80fdbe8 546
547 if (!count)
548 return -EINVAL;
549
8112006f 550 acpi_scan_lock_acquire();
9171f834 551 begin_undock(dock_station);
c80fdbe8 552 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
8112006f 553 acpi_scan_lock_release();
c80fdbe8 554 return ret ? ret: count;
555}
e5685b9d 556static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
c80fdbe8 557
ac122bb6
IVE
558/*
559 * show_dock_uid - read method for "uid" file in sysfs
560 */
561static ssize_t show_dock_uid(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
27663c58 564 unsigned long long lbuf;
fe06fba2 565 struct dock_station *dock_station = dev->platform_data;
38ff4ffc
KCA
566 acpi_status status = acpi_evaluate_integer(dock_station->handle,
567 "_UID", NULL, &lbuf);
568 if (ACPI_FAILURE(status))
ac122bb6 569 return 0;
38ff4ffc 570
27663c58 571 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
ac122bb6 572}
e5685b9d 573static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
ac122bb6 574
8652b00f
SL
575static ssize_t show_dock_type(struct device *dev,
576 struct device_attribute *attr, char *buf)
577{
fe06fba2 578 struct dock_station *dock_station = dev->platform_data;
8652b00f
SL
579 char *type;
580
581 if (dock_station->flags & DOCK_IS_DOCK)
582 type = "dock_station";
583 else if (dock_station->flags & DOCK_IS_ATA)
584 type = "ata_bay";
585 else if (dock_station->flags & DOCK_IS_BAT)
586 type = "battery_bay";
587 else
588 type = "unknown";
589
590 return snprintf(buf, PAGE_SIZE, "%s\n", type);
591}
592static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
593
5f46c2f2
AC
594static struct attribute *dock_attributes[] = {
595 &dev_attr_docked.attr,
596 &dev_attr_flags.attr,
597 &dev_attr_undock.attr,
598 &dev_attr_uid.attr,
599 &dev_attr_type.attr,
600 NULL
601};
602
603static struct attribute_group dock_attribute_group = {
604 .attrs = dock_attributes
605};
606
c8f7a62c 607/**
1e2380cd
RW
608 * acpi_dock_add - Add a new dock station
609 * @adev: Dock station ACPI device object.
c8f7a62c 610 *
1e2380cd 611 * allocated and initialize a new dock station device.
c8f7a62c 612 */
1e2380cd 613void acpi_dock_add(struct acpi_device *adev)
c8f7a62c 614{
2efbca4d 615 struct dock_station *dock_station, ds = { NULL, };
af887449 616 struct platform_device_info pdevinfo;
1e2380cd 617 acpi_handle handle = adev->handle;
747479a3 618 struct platform_device *dd;
2efbca4d 619 int ret;
c8f7a62c 620
af887449
RW
621 memset(&pdevinfo, 0, sizeof(pdevinfo));
622 pdevinfo.name = "dock";
623 pdevinfo.id = dock_station_count;
624 pdevinfo.acpi_node.companion = adev;
625 pdevinfo.data = &ds;
626 pdevinfo.size_data = sizeof(ds);
627 dd = platform_device_register_full(&pdevinfo);
747479a3 628 if (IS_ERR(dd))
1e2380cd 629 return;
747479a3
AC
630
631 dock_station = dd->dev.platform_data;
c8f7a62c 632
c8f7a62c 633 dock_station->handle = handle;
747479a3 634 dock_station->dock_device = dd;
c8f7a62c 635 dock_station->last_dock_time = jiffies - HZ;
747479a3 636
747479a3 637 INIT_LIST_HEAD(&dock_station->sibling);
747479a3 638 INIT_LIST_HEAD(&dock_station->dependent_devices);
a0cd35fd 639
9ef2a9a9 640 /* we want the dock device to send uevents */
747479a3 641 dev_set_uevent_suppress(&dd->dev, 0);
9ef2a9a9 642
c9b5471f 643 if (acpi_dock_match(handle))
db350b08 644 dock_station->flags |= DOCK_IS_DOCK;
c9b5471f 645 if (acpi_ata_match(handle))
db350b08 646 dock_station->flags |= DOCK_IS_ATA;
b43109fa 647 if (acpi_device_is_battery(adev))
db350b08
SL
648 dock_station->flags |= DOCK_IS_BAT;
649
747479a3 650 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
8652b00f 651 if (ret)
5f46c2f2 652 goto err_unregister;
671adbec 653
c8f7a62c 654 /* add the dock station as a device dependent on itself */
3b52b21f 655 ret = add_dock_dependent_device(dock_station, adev);
f69cfdd2 656 if (ret)
5f46c2f2 657 goto err_rmgroup;
c8f7a62c 658
db350b08 659 dock_station_count++;
50d716e4 660 list_add(&dock_station->sibling, &dock_stations);
1e2380cd
RW
661 adev->flags.is_dock_station = true;
662 dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
663 dock_station_count);
664 return;
c8f7a62c 665
5f46c2f2 666err_rmgroup:
747479a3 667 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
f311e1c4 668
5f46c2f2 669err_unregister:
747479a3 670 platform_device_unregister(dd);
cd73018f 671 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
c8f7a62c 672}
This page took 0.635055 seconds and 5 git commands to generate.