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