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