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