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