Commit | Line | Data |
---|---|---|
c8f7a62c LB |
1 | /* |
2 | * dock.c - ACPI dock station driver | |
3 | * | |
4 | * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com> | |
5 | * | |
6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or (at | |
11 | * your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
21 | * | |
22 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
23 | */ | |
24 | ||
25 | #include <linux/kernel.h> | |
26 | #include <linux/module.h> | |
5a0e3ad6 | 27 | #include <linux/slab.h> |
c8f7a62c LB |
28 | #include <linux/init.h> |
29 | #include <linux/types.h> | |
30 | #include <linux/notifier.h> | |
671adbec | 31 | #include <linux/platform_device.h> |
914e2637 | 32 | #include <linux/jiffies.h> |
62a6d7fd | 33 | #include <linux/stddef.h> |
cd73018f | 34 | #include <linux/acpi.h> |
c8f7a62c | 35 | |
a192a958 LB |
36 | #define PREFIX "ACPI: " |
37 | ||
7cda93e0 | 38 | #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver" |
c8f7a62c | 39 | |
f52fd66d | 40 | ACPI_MODULE_NAME("dock"); |
c8f7a62c | 41 | MODULE_AUTHOR("Kristen Carlson Accardi"); |
7cda93e0 | 42 | MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION); |
c8f7a62c LB |
43 | MODULE_LICENSE("GPL"); |
44 | ||
90ab5ee9 | 45 | static bool immediate_undock = 1; |
a0cd35fd KCA |
46 | module_param(immediate_undock, bool, 0644); |
47 | MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to " | |
48 | "undock immediately when the undock button is pressed, 0 will cause" | |
49 | " the driver to wait for userspace to write the undock sysfs file " | |
50 | " before undocking"); | |
51 | ||
a340af14 FS |
52 | static const struct acpi_device_id dock_device_ids[] = { |
53 | {"LNXDOCK", 0}, | |
54 | {"", 0}, | |
55 | }; | |
56 | MODULE_DEVICE_TABLE(acpi, dock_device_ids); | |
57 | ||
c8f7a62c LB |
58 | struct dock_station { |
59 | acpi_handle handle; | |
60 | unsigned long last_dock_time; | |
61 | u32 flags; | |
c8f7a62c | 62 | struct list_head dependent_devices; |
db350b08 | 63 | |
50d716e4 | 64 | struct list_head sibling; |
db350b08 | 65 | struct platform_device *dock_device; |
c8f7a62c | 66 | }; |
db350b08 SL |
67 | static LIST_HEAD(dock_stations); |
68 | static int dock_station_count; | |
21a31013 | 69 | static DEFINE_MUTEX(hotplug_lock); |
c8f7a62c LB |
70 | |
71 | struct dock_dependent_device { | |
72 | struct list_head list; | |
c8f7a62c | 73 | acpi_handle handle; |
21a31013 RW |
74 | const struct acpi_dock_ops *hp_ops; |
75 | void *hp_context; | |
76 | unsigned int hp_refcount; | |
77 | void (*hp_release)(void *); | |
c8f7a62c LB |
78 | }; |
79 | ||
80 | #define DOCK_DOCKING 0x00000001 | |
a0cd35fd | 81 | #define DOCK_UNDOCKING 0x00000002 |
db350b08 SL |
82 | #define DOCK_IS_DOCK 0x00000010 |
83 | #define DOCK_IS_ATA 0x00000020 | |
84 | #define DOCK_IS_BAT 0x00000040 | |
5669021e KCA |
85 | #define DOCK_EVENT 3 |
86 | #define UNDOCK_EVENT 2 | |
c8f7a62c | 87 | |
f09ce741 RW |
88 | enum dock_callback_type { |
89 | DOCK_CALL_HANDLER, | |
90 | DOCK_CALL_FIXUP, | |
91 | DOCK_CALL_UEVENT, | |
92 | }; | |
93 | ||
c8f7a62c LB |
94 | /***************************************************************************** |
95 | * Dock Dependent device functions * | |
96 | *****************************************************************************/ | |
97 | /** | |
f69cfdd2 AC |
98 | * add_dock_dependent_device - associate a device with the dock station |
99 | * @ds: The dock station | |
100 | * @handle: handle of the dependent device | |
c8f7a62c | 101 | * |
f69cfdd2 | 102 | * Add the dependent device to the dock's dependent device list. |
c8f7a62c | 103 | */ |
d423c083 | 104 | static int __init |
f69cfdd2 | 105 | add_dock_dependent_device(struct dock_station *ds, acpi_handle handle) |
c8f7a62c LB |
106 | { |
107 | struct dock_dependent_device *dd; | |
108 | ||
109 | dd = kzalloc(sizeof(*dd), GFP_KERNEL); | |
f69cfdd2 AC |
110 | if (!dd) |
111 | return -ENOMEM; | |
112 | ||
113 | dd->handle = handle; | |
114 | INIT_LIST_HEAD(&dd->list); | |
c8f7a62c | 115 | list_add_tail(&dd->list, &ds->dependent_devices); |
f69cfdd2 AC |
116 | |
117 | return 0; | |
c8f7a62c LB |
118 | } |
119 | ||
a30c4c5e RW |
120 | static void remove_dock_dependent_devices(struct dock_station *ds) |
121 | { | |
122 | struct dock_dependent_device *dd, *aux; | |
123 | ||
124 | list_for_each_entry_safe(dd, aux, &ds->dependent_devices, list) { | |
125 | list_del(&dd->list); | |
126 | kfree(dd); | |
127 | } | |
128 | } | |
129 | ||
c8f7a62c | 130 | /** |
21a31013 RW |
131 | * dock_init_hotplug - Initialize a hotplug device on a docking station. |
132 | * @dd: Dock-dependent device. | |
133 | * @ops: Dock operations to attach to the dependent device. | |
134 | * @context: Data to pass to the @ops callbacks and @release. | |
135 | * @init: Optional initialization routine to run after setting up context. | |
136 | * @release: Optional release routine to run on removal. | |
c8f7a62c | 137 | */ |
21a31013 RW |
138 | static int dock_init_hotplug(struct dock_dependent_device *dd, |
139 | const struct acpi_dock_ops *ops, void *context, | |
140 | void (*init)(void *), void (*release)(void *)) | |
c8f7a62c | 141 | { |
21a31013 RW |
142 | int ret = 0; |
143 | ||
144 | mutex_lock(&hotplug_lock); | |
4ec24065 | 145 | if (WARN_ON(dd->hp_context)) { |
21a31013 RW |
146 | ret = -EEXIST; |
147 | } else { | |
148 | dd->hp_refcount = 1; | |
149 | dd->hp_ops = ops; | |
150 | dd->hp_context = context; | |
151 | dd->hp_release = release; | |
4ec24065 RW |
152 | if (init) |
153 | init(context); | |
21a31013 | 154 | } |
21a31013 RW |
155 | mutex_unlock(&hotplug_lock); |
156 | return ret; | |
c8f7a62c LB |
157 | } |
158 | ||
159 | /** | |
21a31013 RW |
160 | * dock_release_hotplug - Decrement hotplug reference counter of dock device. |
161 | * @dd: Dock-dependent device. | |
c8f7a62c | 162 | * |
21a31013 RW |
163 | * Decrement the reference counter of @dd and if 0, detach its hotplug |
164 | * operations from it, reset its context pointer and run the optional release | |
165 | * routine if present. | |
c8f7a62c | 166 | */ |
21a31013 | 167 | static void dock_release_hotplug(struct dock_dependent_device *dd) |
c8f7a62c | 168 | { |
21a31013 | 169 | mutex_lock(&hotplug_lock); |
21a31013 | 170 | if (dd->hp_context && !--dd->hp_refcount) { |
4ec24065 RW |
171 | void (*release)(void *) = dd->hp_release; |
172 | void *context = dd->hp_context; | |
173 | ||
21a31013 | 174 | dd->hp_ops = NULL; |
21a31013 | 175 | dd->hp_context = NULL; |
21a31013 | 176 | dd->hp_release = NULL; |
4ec24065 RW |
177 | if (release) |
178 | release(context); | |
21a31013 | 179 | } |
21a31013 RW |
180 | mutex_unlock(&hotplug_lock); |
181 | } | |
182 | ||
183 | static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event, | |
f09ce741 | 184 | enum dock_callback_type cb_type) |
21a31013 RW |
185 | { |
186 | acpi_notify_handler cb = NULL; | |
187 | bool run = false; | |
188 | ||
189 | mutex_lock(&hotplug_lock); | |
190 | ||
191 | if (dd->hp_context) { | |
192 | run = true; | |
193 | dd->hp_refcount++; | |
f09ce741 RW |
194 | if (dd->hp_ops) { |
195 | switch (cb_type) { | |
196 | case DOCK_CALL_FIXUP: | |
197 | cb = dd->hp_ops->fixup; | |
198 | break; | |
199 | case DOCK_CALL_UEVENT: | |
200 | cb = dd->hp_ops->uevent; | |
201 | break; | |
202 | default: | |
203 | cb = dd->hp_ops->handler; | |
204 | } | |
205 | } | |
21a31013 RW |
206 | } |
207 | ||
208 | mutex_unlock(&hotplug_lock); | |
209 | ||
210 | if (!run) | |
211 | return; | |
212 | ||
213 | if (cb) | |
214 | cb(dd->handle, event, dd->hp_context); | |
215 | ||
216 | dock_release_hotplug(dd); | |
c8f7a62c LB |
217 | } |
218 | ||
219 | /** | |
220 | * find_dock_dependent_device - get a device dependent on this dock | |
221 | * @ds: the dock station | |
222 | * @handle: the acpi_handle of the device we want | |
223 | * | |
224 | * iterate over the dependent device list for this dock. If the | |
225 | * dependent device matches the handle, return. | |
226 | */ | |
227 | static struct dock_dependent_device * | |
228 | find_dock_dependent_device(struct dock_station *ds, acpi_handle handle) | |
229 | { | |
230 | struct dock_dependent_device *dd; | |
231 | ||
ed633e70 JL |
232 | list_for_each_entry(dd, &ds->dependent_devices, list) |
233 | if (handle == dd->handle) | |
c8f7a62c | 234 | return dd; |
ed633e70 | 235 | |
c8f7a62c LB |
236 | return NULL; |
237 | } | |
238 | ||
239 | /***************************************************************************** | |
240 | * Dock functions * | |
241 | *****************************************************************************/ | |
d423c083 | 242 | static int __init is_battery(acpi_handle handle) |
db350b08 SL |
243 | { |
244 | struct acpi_device_info *info; | |
db350b08 SL |
245 | int ret = 1; |
246 | ||
15b8dd53 | 247 | if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info))) |
db350b08 | 248 | return 0; |
db350b08 SL |
249 | if (!(info->valid & ACPI_VALID_HID)) |
250 | ret = 0; | |
251 | else | |
15b8dd53 | 252 | ret = !strcmp("PNP0C0A", info->hardware_id.string); |
db350b08 | 253 | |
15b8dd53 | 254 | kfree(info); |
db350b08 SL |
255 | return ret; |
256 | } | |
257 | ||
c9b5471f JL |
258 | /* Check whether ACPI object is an ejectable battery or disk bay */ |
259 | static bool __init is_ejectable_bay(acpi_handle handle) | |
db350b08 | 260 | { |
c9b5471f JL |
261 | if (acpi_has_method(handle, "_EJ0") && is_battery(handle)) |
262 | return true; | |
747479a3 | 263 | |
c9b5471f | 264 | return acpi_bay_match(handle); |
db350b08 SL |
265 | } |
266 | ||
c8f7a62c LB |
267 | /** |
268 | * is_dock_device - see if a device is on a dock station | |
269 | * @handle: acpi handle of the device | |
270 | * | |
271 | * If this device is either the dock station itself, | |
272 | * or is a device dependent on the dock station, then it | |
273 | * is a dock device | |
274 | */ | |
275 | int is_dock_device(acpi_handle handle) | |
276 | { | |
db350b08 SL |
277 | struct dock_station *dock_station; |
278 | ||
279 | if (!dock_station_count) | |
c8f7a62c LB |
280 | return 0; |
281 | ||
c9b5471f | 282 | if (acpi_dock_match(handle)) |
c8f7a62c | 283 | return 1; |
747479a3 AC |
284 | |
285 | list_for_each_entry(dock_station, &dock_stations, sibling) | |
db350b08 SL |
286 | if (find_dock_dependent_device(dock_station, handle)) |
287 | return 1; | |
c8f7a62c LB |
288 | |
289 | return 0; | |
290 | } | |
c8f7a62c LB |
291 | EXPORT_SYMBOL_GPL(is_dock_device); |
292 | ||
293 | /** | |
294 | * dock_present - see if the dock station is present. | |
295 | * @ds: the dock station | |
296 | * | |
297 | * execute the _STA method. note that present does not | |
298 | * imply that we are docked. | |
299 | */ | |
300 | static int dock_present(struct dock_station *ds) | |
301 | { | |
27663c58 | 302 | unsigned long long sta; |
c8f7a62c LB |
303 | acpi_status status; |
304 | ||
305 | if (ds) { | |
306 | status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta); | |
307 | if (ACPI_SUCCESS(status) && sta) | |
308 | return 1; | |
309 | } | |
310 | return 0; | |
311 | } | |
312 | ||
c8f7a62c LB |
313 | /** |
314 | * dock_create_acpi_device - add new devices to acpi | |
315 | * @handle - handle of the device to add | |
316 | * | |
317 | * This function will create a new acpi_device for the given | |
318 | * handle if one does not exist already. This should cause | |
319 | * acpi to scan for drivers for the given devices, and call | |
320 | * matching driver's add routine. | |
c8f7a62c | 321 | */ |
472d963b | 322 | static void dock_create_acpi_device(acpi_handle handle) |
c8f7a62c | 323 | { |
747479a3 | 324 | struct acpi_device *device; |
c8f7a62c LB |
325 | int ret; |
326 | ||
327 | if (acpi_bus_get_device(handle, &device)) { | |
328 | /* | |
329 | * no device created for this object, | |
330 | * so we should create one. | |
331 | */ | |
b8bd759a | 332 | ret = acpi_bus_scan(handle); |
636458de | 333 | if (ret) |
747479a3 | 334 | pr_debug("error adding bus, %x\n", -ret); |
c8f7a62c | 335 | } |
c8f7a62c LB |
336 | } |
337 | ||
338 | /** | |
339 | * dock_remove_acpi_device - remove the acpi_device struct from acpi | |
340 | * @handle - the handle of the device to remove | |
341 | * | |
342 | * Tell acpi to remove the acpi_device. This should cause any loaded | |
343 | * driver to have it's remove routine called. | |
344 | */ | |
345 | static void dock_remove_acpi_device(acpi_handle handle) | |
346 | { | |
347 | struct acpi_device *device; | |
c8f7a62c | 348 | |
bfee26db RW |
349 | if (!acpi_bus_get_device(handle, &device)) |
350 | acpi_bus_trim(device); | |
c8f7a62c LB |
351 | } |
352 | ||
c8f7a62c | 353 | /** |
37f90877 RW |
354 | * hot_remove_dock_devices - Remove dock station devices. |
355 | * @ds: Dock station. | |
356 | */ | |
357 | static void hot_remove_dock_devices(struct dock_station *ds) | |
358 | { | |
359 | struct dock_dependent_device *dd; | |
360 | ||
361 | /* | |
362 | * Walk the list in reverse order so that devices that have been added | |
363 | * last are removed first (in case there are some indirect dependencies | |
364 | * between them). | |
365 | */ | |
366 | list_for_each_entry_reverse(dd, &ds->dependent_devices, list) | |
367 | dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false); | |
368 | ||
369 | list_for_each_entry_reverse(dd, &ds->dependent_devices, list) | |
370 | dock_remove_acpi_device(dd->handle); | |
371 | } | |
372 | ||
373 | /** | |
374 | * hotplug_dock_devices - Insert devices on a dock station. | |
c8f7a62c | 375 | * @ds: the dock station |
37f90877 | 376 | * @event: either bus check or device check request |
c8f7a62c LB |
377 | * |
378 | * Some devices on the dock station need to have drivers called | |
379 | * to perform hotplug operations after a dock event has occurred. | |
380 | * Traverse the list of dock devices that have registered a | |
381 | * hotplug handler, and call the handler. | |
382 | */ | |
383 | static void hotplug_dock_devices(struct dock_station *ds, u32 event) | |
384 | { | |
385 | struct dock_dependent_device *dd; | |
386 | ||
f09ce741 RW |
387 | /* Call driver specific post-dock fixups. */ |
388 | list_for_each_entry(dd, &ds->dependent_devices, list) | |
389 | dock_hotplug_event(dd, event, DOCK_CALL_FIXUP); | |
390 | ||
37f90877 | 391 | /* Call driver specific hotplug functions. */ |
21a31013 | 392 | list_for_each_entry(dd, &ds->dependent_devices, list) |
f09ce741 | 393 | dock_hotplug_event(dd, event, DOCK_CALL_HANDLER); |
c8f7a62c LB |
394 | |
395 | /* | |
37f90877 RW |
396 | * Now make sure that an acpi_device is created for each dependent |
397 | * device. That will cause scan handlers to be attached to device | |
398 | * objects or acpi_drivers to be stopped/started if they are present. | |
c8f7a62c | 399 | */ |
37f90877 RW |
400 | list_for_each_entry(dd, &ds->dependent_devices, list) |
401 | dock_create_acpi_device(dd->handle); | |
c8f7a62c LB |
402 | } |
403 | ||
404 | static void dock_event(struct dock_station *ds, u32 event, int num) | |
405 | { | |
db350b08 | 406 | struct device *dev = &ds->dock_device->dev; |
66b56821 | 407 | char event_string[13]; |
79a8f70b | 408 | char *envp[] = { event_string, NULL }; |
1253f7aa | 409 | struct dock_dependent_device *dd; |
79a8f70b KCA |
410 | |
411 | if (num == UNDOCK_EVENT) | |
66b56821 | 412 | sprintf(event_string, "EVENT=undock"); |
79a8f70b | 413 | else |
66b56821 | 414 | sprintf(event_string, "EVENT=dock"); |
79a8f70b | 415 | |
5669021e | 416 | /* |
8ea86e0b KCA |
417 | * Indicate that the status of the dock station has |
418 | * changed. | |
5669021e | 419 | */ |
1253f7aa SL |
420 | if (num == DOCK_EVENT) |
421 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); | |
422 | ||
21a31013 | 423 | list_for_each_entry(dd, &ds->dependent_devices, list) |
f09ce741 | 424 | dock_hotplug_event(dd, event, DOCK_CALL_UEVENT); |
747479a3 | 425 | |
1253f7aa SL |
426 | if (num != DOCK_EVENT) |
427 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); | |
c8f7a62c LB |
428 | } |
429 | ||
c8f7a62c LB |
430 | /** |
431 | * handle_dock - handle a dock event | |
432 | * @ds: the dock station | |
433 | * @dock: to dock, or undock - that is the question | |
434 | * | |
435 | * Execute the _DCK method in response to an acpi event | |
436 | */ | |
437 | static void handle_dock(struct dock_station *ds, int dock) | |
438 | { | |
439 | acpi_status status; | |
440 | struct acpi_object_list arg_list; | |
441 | union acpi_object arg; | |
6a868e17 | 442 | unsigned long long value; |
c8f7a62c | 443 | |
cd73018f | 444 | acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking"); |
c8f7a62c LB |
445 | |
446 | /* _DCK method has one argument */ | |
447 | arg_list.count = 1; | |
448 | arg_list.pointer = &arg; | |
449 | arg.type = ACPI_TYPE_INTEGER; | |
450 | arg.integer.value = dock; | |
6a868e17 | 451 | status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value); |
db350b08 | 452 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) |
cd73018f TK |
453 | acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n", |
454 | status); | |
c8f7a62c LB |
455 | } |
456 | ||
457 | static inline void dock(struct dock_station *ds) | |
458 | { | |
459 | handle_dock(ds, 1); | |
460 | } | |
461 | ||
462 | static inline void undock(struct dock_station *ds) | |
463 | { | |
464 | handle_dock(ds, 0); | |
465 | } | |
466 | ||
467 | static inline void begin_dock(struct dock_station *ds) | |
468 | { | |
469 | ds->flags |= DOCK_DOCKING; | |
470 | } | |
471 | ||
472 | static inline void complete_dock(struct dock_station *ds) | |
473 | { | |
474 | ds->flags &= ~(DOCK_DOCKING); | |
475 | ds->last_dock_time = jiffies; | |
476 | } | |
477 | ||
a0cd35fd KCA |
478 | static inline void begin_undock(struct dock_station *ds) |
479 | { | |
480 | ds->flags |= DOCK_UNDOCKING; | |
481 | } | |
482 | ||
483 | static inline void complete_undock(struct dock_station *ds) | |
484 | { | |
485 | ds->flags &= ~(DOCK_UNDOCKING); | |
486 | } | |
487 | ||
c8f7a62c LB |
488 | /** |
489 | * dock_in_progress - see if we are in the middle of handling a dock event | |
490 | * @ds: the dock station | |
491 | * | |
492 | * Sometimes while docking, false dock events can be sent to the driver | |
493 | * because good connections aren't made or some other reason. Ignore these | |
494 | * if we are in the middle of doing something. | |
495 | */ | |
496 | static int dock_in_progress(struct dock_station *ds) | |
497 | { | |
498 | if ((ds->flags & DOCK_DOCKING) || | |
499 | time_before(jiffies, (ds->last_dock_time + HZ))) | |
500 | return 1; | |
501 | return 0; | |
502 | } | |
503 | ||
c8f7a62c LB |
504 | /** |
505 | * register_hotplug_dock_device - register a hotplug function | |
506 | * @handle: the handle of the device | |
1253f7aa | 507 | * @ops: handlers to call after docking |
c8f7a62c | 508 | * @context: device specific data |
21a31013 RW |
509 | * @init: Optional initialization routine to run after registration |
510 | * @release: Optional release routine to run on unregistration | |
c8f7a62c LB |
511 | * |
512 | * If a driver would like to perform a hotplug operation after a dock | |
513 | * event, they can register an acpi_notifiy_handler to be called by | |
514 | * the dock driver after _DCK is executed. | |
515 | */ | |
21a31013 RW |
516 | int register_hotplug_dock_device(acpi_handle handle, |
517 | const struct acpi_dock_ops *ops, void *context, | |
518 | void (*init)(void *), void (*release)(void *)) | |
c8f7a62c LB |
519 | { |
520 | struct dock_dependent_device *dd; | |
db350b08 | 521 | struct dock_station *dock_station; |
61b83695 | 522 | int ret = -EINVAL; |
c8f7a62c | 523 | |
21a31013 RW |
524 | if (WARN_ON(!context)) |
525 | return -EINVAL; | |
526 | ||
db350b08 | 527 | if (!dock_station_count) |
c8f7a62c LB |
528 | return -ENODEV; |
529 | ||
530 | /* | |
531 | * make sure this handle is for a device dependent on the dock, | |
532 | * this would include the dock station itself | |
533 | */ | |
50d716e4 | 534 | list_for_each_entry(dock_station, &dock_stations, sibling) { |
61b83695 SL |
535 | /* |
536 | * An ATA bay can be in a dock and itself can be ejected | |
3ad2f3fb | 537 | * separately, so there are two 'dock stations' which need the |
61b83695 SL |
538 | * ops |
539 | */ | |
db350b08 | 540 | dd = find_dock_dependent_device(dock_station, handle); |
21a31013 | 541 | if (dd && !dock_init_hotplug(dd, ops, context, init, release)) |
61b83695 | 542 | ret = 0; |
c8f7a62c LB |
543 | } |
544 | ||
61b83695 | 545 | return ret; |
c8f7a62c | 546 | } |
c8f7a62c LB |
547 | EXPORT_SYMBOL_GPL(register_hotplug_dock_device); |
548 | ||
549 | /** | |
550 | * unregister_hotplug_dock_device - remove yourself from the hotplug list | |
551 | * @handle: the acpi handle of the device | |
552 | */ | |
553 | void unregister_hotplug_dock_device(acpi_handle handle) | |
554 | { | |
555 | struct dock_dependent_device *dd; | |
db350b08 | 556 | struct dock_station *dock_station; |
c8f7a62c | 557 | |
db350b08 | 558 | if (!dock_station_count) |
c8f7a62c LB |
559 | return; |
560 | ||
50d716e4 | 561 | list_for_each_entry(dock_station, &dock_stations, sibling) { |
db350b08 SL |
562 | dd = find_dock_dependent_device(dock_station, handle); |
563 | if (dd) | |
21a31013 | 564 | dock_release_hotplug(dd); |
db350b08 | 565 | } |
c8f7a62c | 566 | } |
c8f7a62c LB |
567 | EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device); |
568 | ||
c80fdbe8 | 569 | /** |
570 | * handle_eject_request - handle an undock request checking for error conditions | |
571 | * | |
572 | * Check to make sure the dock device is still present, then undock and | |
573 | * hotremove all the devices that may need removing. | |
574 | */ | |
575 | static int handle_eject_request(struct dock_station *ds, u32 event) | |
576 | { | |
c80fdbe8 | 577 | if (dock_in_progress(ds)) |
578 | return -EBUSY; | |
579 | ||
580 | /* | |
581 | * here we need to generate the undock | |
582 | * event prior to actually doing the undock | |
583 | * so that the device struct still exists. | |
afd7301d HM |
584 | * Also, even send the dock event if the |
585 | * device is not present anymore | |
c80fdbe8 | 586 | */ |
587 | dock_event(ds, event, UNDOCK_EVENT); | |
afd7301d | 588 | |
37f90877 | 589 | hot_remove_dock_devices(ds); |
c80fdbe8 | 590 | undock(ds); |
c9b5471f JL |
591 | acpi_evaluate_lck(ds->handle, 0); |
592 | acpi_evaluate_ej0(ds->handle); | |
c80fdbe8 | 593 | if (dock_present(ds)) { |
cd73018f | 594 | acpi_handle_err(ds->handle, "Unable to undock!\n"); |
c80fdbe8 | 595 | return -EBUSY; |
596 | } | |
a0cd35fd | 597 | complete_undock(ds); |
c80fdbe8 | 598 | return 0; |
599 | } | |
600 | ||
c8f7a62c LB |
601 | /** |
602 | * dock_notify - act upon an acpi dock notification | |
59401ccc | 603 | * @ds: dock station |
c8f7a62c | 604 | * @event: the acpi event |
c8f7a62c LB |
605 | * |
606 | * If we are notified to dock, then check to see if the dock is | |
607 | * present and then dock. Notify all drivers of the dock event, | |
c80fdbe8 | 608 | * and then hotplug and devices that may need hotplugging. |
c8f7a62c | 609 | */ |
59401ccc | 610 | static void dock_notify(struct dock_station *ds, u32 event) |
c8f7a62c | 611 | { |
59401ccc RW |
612 | acpi_handle handle = ds->handle; |
613 | struct acpi_device *ad; | |
db350b08 | 614 | int surprise_removal = 0; |
c8f7a62c | 615 | |
db350b08 SL |
616 | /* |
617 | * According to acpi spec 3.0a, if a DEVICE_CHECK notification | |
618 | * is sent and _DCK is present, it is assumed to mean an undock | |
619 | * request. | |
620 | */ | |
621 | if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK) | |
622 | event = ACPI_NOTIFY_EJECT_REQUEST; | |
623 | ||
624 | /* | |
625 | * dock station: BUS_CHECK - docked or surprise removal | |
626 | * DEVICE_CHECK - undocked | |
627 | * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal | |
628 | * | |
629 | * To simplify event handling, dock dependent device handler always | |
630 | * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and | |
631 | * ACPI_NOTIFY_EJECT_REQUEST for removal | |
632 | */ | |
c8f7a62c LB |
633 | switch (event) { |
634 | case ACPI_NOTIFY_BUS_CHECK: | |
db350b08 | 635 | case ACPI_NOTIFY_DEVICE_CHECK: |
59401ccc | 636 | if (!dock_in_progress(ds) && acpi_bus_get_device(handle, &ad)) { |
c8f7a62c LB |
637 | begin_dock(ds); |
638 | dock(ds); | |
639 | if (!dock_present(ds)) { | |
cd73018f | 640 | acpi_handle_err(handle, "Unable to dock!\n"); |
8b59560a | 641 | complete_dock(ds); |
c8f7a62c LB |
642 | break; |
643 | } | |
c8f7a62c LB |
644 | hotplug_dock_devices(ds, event); |
645 | complete_dock(ds); | |
646 | dock_event(ds, event, DOCK_EVENT); | |
c9b5471f | 647 | acpi_evaluate_lck(ds->handle, 1); |
3a37898d | 648 | acpi_update_all_gpes(); |
db350b08 | 649 | break; |
c8f7a62c | 650 | } |
db350b08 SL |
651 | if (dock_present(ds) || dock_in_progress(ds)) |
652 | break; | |
653 | /* This is a surprise removal */ | |
654 | surprise_removal = 1; | |
655 | event = ACPI_NOTIFY_EJECT_REQUEST; | |
656 | /* Fall back */ | |
c8f7a62c | 657 | case ACPI_NOTIFY_EJECT_REQUEST: |
a0cd35fd | 658 | begin_undock(ds); |
f730ae18 SL |
659 | if ((immediate_undock && !(ds->flags & DOCK_IS_ATA)) |
660 | || surprise_removal) | |
a0cd35fd KCA |
661 | handle_eject_request(ds, event); |
662 | else | |
663 | dock_event(ds, event, UNDOCK_EVENT); | |
c8f7a62c LB |
664 | break; |
665 | default: | |
cd73018f | 666 | acpi_handle_err(handle, "Unknown dock event %d\n", event); |
c8f7a62c LB |
667 | } |
668 | } | |
669 | ||
7b98118a | 670 | static void acpi_dock_deferred_cb(void *data, u32 event) |
19cd847a | 671 | { |
3757b948 | 672 | acpi_scan_lock_acquire(); |
7b98118a | 673 | dock_notify(data, event); |
3757b948 | 674 | acpi_scan_lock_release(); |
19cd847a ZR |
675 | } |
676 | ||
59401ccc | 677 | static void dock_notify_handler(acpi_handle handle, u32 event, void *data) |
6bd00a61 | 678 | { |
6bd00a61 SL |
679 | if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK |
680 | && event != ACPI_NOTIFY_EJECT_REQUEST) | |
59401ccc | 681 | return; |
3757b948 | 682 | |
7b98118a | 683 | acpi_hotplug_execute(acpi_dock_deferred_cb, data, event); |
6bd00a61 SL |
684 | } |
685 | ||
c8f7a62c LB |
686 | /** |
687 | * find_dock_devices - find devices on the dock station | |
688 | * @handle: the handle of the device we are examining | |
689 | * @lvl: unused | |
690 | * @context: the dock station private data | |
691 | * @rv: unused | |
692 | * | |
693 | * This function is called by acpi_walk_namespace. It will | |
694 | * check to see if an object has an _EJD method. If it does, then it | |
695 | * will see if it is dependent on the dock station. | |
696 | */ | |
96c0a4d4 RW |
697 | static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl, |
698 | void *context, void **rv) | |
c8f7a62c | 699 | { |
50dd0969 | 700 | struct dock_station *ds = context; |
96c0a4d4 | 701 | acpi_handle ejd = NULL; |
c8f7a62c | 702 | |
96c0a4d4 RW |
703 | acpi_bus_get_ejd(handle, &ejd); |
704 | if (ejd == ds->handle) | |
f69cfdd2 AC |
705 | add_dock_dependent_device(ds, handle); |
706 | ||
c8f7a62c LB |
707 | return AE_OK; |
708 | } | |
709 | ||
c80fdbe8 | 710 | /* |
711 | * show_docked - read method for "docked" file in sysfs | |
712 | */ | |
713 | static ssize_t show_docked(struct device *dev, | |
714 | struct device_attribute *attr, char *buf) | |
715 | { | |
fc5a9f88 HM |
716 | struct acpi_device *tmp; |
717 | ||
fe06fba2 | 718 | struct dock_station *dock_station = dev->platform_data; |
c80fdbe8 | 719 | |
02df7349 | 720 | if (!acpi_bus_get_device(dock_station->handle, &tmp)) |
fc5a9f88 HM |
721 | return snprintf(buf, PAGE_SIZE, "1\n"); |
722 | return snprintf(buf, PAGE_SIZE, "0\n"); | |
c80fdbe8 | 723 | } |
e5685b9d | 724 | static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL); |
c80fdbe8 | 725 | |
a0cd35fd KCA |
726 | /* |
727 | * show_flags - read method for flags file in sysfs | |
728 | */ | |
729 | static ssize_t show_flags(struct device *dev, | |
730 | struct device_attribute *attr, char *buf) | |
731 | { | |
fe06fba2 | 732 | struct dock_station *dock_station = dev->platform_data; |
a0cd35fd KCA |
733 | return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags); |
734 | ||
735 | } | |
e5685b9d | 736 | static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL); |
a0cd35fd | 737 | |
c80fdbe8 | 738 | /* |
739 | * write_undock - write method for "undock" file in sysfs | |
740 | */ | |
741 | static ssize_t write_undock(struct device *dev, struct device_attribute *attr, | |
742 | const char *buf, size_t count) | |
743 | { | |
744 | int ret; | |
fe06fba2 | 745 | struct dock_station *dock_station = dev->platform_data; |
c80fdbe8 | 746 | |
747 | if (!count) | |
748 | return -EINVAL; | |
749 | ||
8112006f | 750 | acpi_scan_lock_acquire(); |
9171f834 | 751 | begin_undock(dock_station); |
c80fdbe8 | 752 | ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST); |
8112006f | 753 | acpi_scan_lock_release(); |
c80fdbe8 | 754 | return ret ? ret: count; |
755 | } | |
e5685b9d | 756 | static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock); |
c80fdbe8 | 757 | |
ac122bb6 IVE |
758 | /* |
759 | * show_dock_uid - read method for "uid" file in sysfs | |
760 | */ | |
761 | static ssize_t show_dock_uid(struct device *dev, | |
762 | struct device_attribute *attr, char *buf) | |
763 | { | |
27663c58 | 764 | unsigned long long lbuf; |
fe06fba2 | 765 | struct dock_station *dock_station = dev->platform_data; |
38ff4ffc KCA |
766 | acpi_status status = acpi_evaluate_integer(dock_station->handle, |
767 | "_UID", NULL, &lbuf); | |
768 | if (ACPI_FAILURE(status)) | |
ac122bb6 | 769 | return 0; |
38ff4ffc | 770 | |
27663c58 | 771 | return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf); |
ac122bb6 | 772 | } |
e5685b9d | 773 | static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL); |
ac122bb6 | 774 | |
8652b00f SL |
775 | static ssize_t show_dock_type(struct device *dev, |
776 | struct device_attribute *attr, char *buf) | |
777 | { | |
fe06fba2 | 778 | struct dock_station *dock_station = dev->platform_data; |
8652b00f SL |
779 | char *type; |
780 | ||
781 | if (dock_station->flags & DOCK_IS_DOCK) | |
782 | type = "dock_station"; | |
783 | else if (dock_station->flags & DOCK_IS_ATA) | |
784 | type = "ata_bay"; | |
785 | else if (dock_station->flags & DOCK_IS_BAT) | |
786 | type = "battery_bay"; | |
787 | else | |
788 | type = "unknown"; | |
789 | ||
790 | return snprintf(buf, PAGE_SIZE, "%s\n", type); | |
791 | } | |
792 | static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL); | |
793 | ||
5f46c2f2 AC |
794 | static struct attribute *dock_attributes[] = { |
795 | &dev_attr_docked.attr, | |
796 | &dev_attr_flags.attr, | |
797 | &dev_attr_undock.attr, | |
798 | &dev_attr_uid.attr, | |
799 | &dev_attr_type.attr, | |
800 | NULL | |
801 | }; | |
802 | ||
803 | static struct attribute_group dock_attribute_group = { | |
804 | .attrs = dock_attributes | |
805 | }; | |
806 | ||
c8f7a62c LB |
807 | /** |
808 | * dock_add - add a new dock station | |
809 | * @handle: the dock station handle | |
810 | * | |
811 | * allocated and initialize a new dock station device. Find all devices | |
812 | * that are on the dock station, and register for dock event notifications. | |
813 | */ | |
d38a5edf | 814 | static int __init dock_add(acpi_handle handle) |
c8f7a62c | 815 | { |
2efbca4d | 816 | struct dock_station *dock_station, ds = { NULL, }; |
747479a3 | 817 | struct platform_device *dd; |
59401ccc | 818 | acpi_status status; |
2efbca4d | 819 | int ret; |
c8f7a62c | 820 | |
2efbca4d RW |
821 | dd = platform_device_register_data(NULL, "dock", dock_station_count, |
822 | &ds, sizeof(ds)); | |
747479a3 AC |
823 | if (IS_ERR(dd)) |
824 | return PTR_ERR(dd); | |
825 | ||
826 | dock_station = dd->dev.platform_data; | |
c8f7a62c | 827 | |
c8f7a62c | 828 | dock_station->handle = handle; |
747479a3 | 829 | dock_station->dock_device = dd; |
c8f7a62c | 830 | dock_station->last_dock_time = jiffies - HZ; |
747479a3 | 831 | |
747479a3 | 832 | INIT_LIST_HEAD(&dock_station->sibling); |
747479a3 | 833 | INIT_LIST_HEAD(&dock_station->dependent_devices); |
a0cd35fd | 834 | |
9ef2a9a9 | 835 | /* we want the dock device to send uevents */ |
747479a3 | 836 | dev_set_uevent_suppress(&dd->dev, 0); |
9ef2a9a9 | 837 | |
c9b5471f | 838 | if (acpi_dock_match(handle)) |
db350b08 | 839 | dock_station->flags |= DOCK_IS_DOCK; |
c9b5471f | 840 | if (acpi_ata_match(handle)) |
db350b08 SL |
841 | dock_station->flags |= DOCK_IS_ATA; |
842 | if (is_battery(handle)) | |
843 | dock_station->flags |= DOCK_IS_BAT; | |
844 | ||
747479a3 | 845 | ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group); |
8652b00f | 846 | if (ret) |
5f46c2f2 | 847 | goto err_unregister; |
671adbec | 848 | |
c8f7a62c LB |
849 | /* Find dependent devices */ |
850 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
2263576c LM |
851 | ACPI_UINT32_MAX, find_dock_devices, NULL, |
852 | dock_station, NULL); | |
c8f7a62c LB |
853 | |
854 | /* add the dock station as a device dependent on itself */ | |
f69cfdd2 AC |
855 | ret = add_dock_dependent_device(dock_station, handle); |
856 | if (ret) | |
5f46c2f2 | 857 | goto err_rmgroup; |
c8f7a62c | 858 | |
59401ccc RW |
859 | status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, |
860 | dock_notify_handler, dock_station); | |
0177f29f WY |
861 | if (ACPI_FAILURE(status)) { |
862 | ret = -ENODEV; | |
59401ccc | 863 | goto err_rmgroup; |
0177f29f | 864 | } |
59401ccc | 865 | |
db350b08 | 866 | dock_station_count++; |
50d716e4 | 867 | list_add(&dock_station->sibling, &dock_stations); |
c8f7a62c LB |
868 | return 0; |
869 | ||
5f46c2f2 | 870 | err_rmgroup: |
a30c4c5e | 871 | remove_dock_dependent_devices(dock_station); |
747479a3 | 872 | sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group); |
5f46c2f2 | 873 | err_unregister: |
747479a3 | 874 | platform_device_unregister(dd); |
cd73018f | 875 | acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret); |
c8f7a62c LB |
876 | return ret; |
877 | } | |
878 | ||
c8f7a62c | 879 | /** |
8ab0ab25 | 880 | * find_dock_and_bay - look for dock stations and bays |
c8f7a62c LB |
881 | * @handle: acpi handle of a device |
882 | * @lvl: unused | |
8ab0ab25 | 883 | * @context: unused |
c8f7a62c LB |
884 | * @rv: unused |
885 | * | |
8ab0ab25 | 886 | * This is called by acpi_walk_namespace to look for dock stations and bays. |
c8f7a62c | 887 | */ |
027951e5 | 888 | static acpi_status __init |
8ab0ab25 | 889 | find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv) |
c8f7a62c | 890 | { |
c9b5471f | 891 | if (acpi_dock_match(handle) || is_ejectable_bay(handle)) |
1ee4d61f | 892 | dock_add(handle); |
747479a3 | 893 | |
1ee4d61f | 894 | return AE_OK; |
c8f7a62c LB |
895 | } |
896 | ||
2ce65fe8 | 897 | void __init acpi_dock_init(void) |
db350b08 | 898 | { |
8ab0ab25 | 899 | /* look for dock stations and bays */ |
c8f7a62c | 900 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, |
8ab0ab25 | 901 | ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL); |
c8f7a62c | 902 | |
db350b08 | 903 | if (!dock_station_count) { |
cd73018f | 904 | pr_info(PREFIX "No dock devices found.\n"); |
2ce65fe8 | 905 | return; |
db350b08 | 906 | } |
c8f7a62c | 907 | |
cd73018f | 908 | pr_info(PREFIX "%s: %d docks/bays found\n", |
db350b08 | 909 | ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count); |
c8f7a62c | 910 | } |