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> | |
27 | #include <linux/init.h> | |
28 | #include <linux/types.h> | |
29 | #include <linux/notifier.h> | |
671adbec | 30 | #include <linux/platform_device.h> |
914e2637 | 31 | #include <linux/jiffies.h> |
62a6d7fd | 32 | #include <linux/stddef.h> |
c8f7a62c LB |
33 | #include <acpi/acpi_bus.h> |
34 | #include <acpi/acpi_drivers.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 | ||
a0cd35fd KCA |
43 | static int immediate_undock = 1; |
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 | 50 | static struct atomic_notifier_head dock_notifier_list; |
0f6f2804 | 51 | static struct platform_device *dock_device; |
671adbec | 52 | static char dock_device_name[] = "dock"; |
c8f7a62c | 53 | |
a340af14 FS |
54 | static const struct acpi_device_id dock_device_ids[] = { |
55 | {"LNXDOCK", 0}, | |
56 | {"", 0}, | |
57 | }; | |
58 | MODULE_DEVICE_TABLE(acpi, dock_device_ids); | |
59 | ||
c8f7a62c LB |
60 | struct dock_station { |
61 | acpi_handle handle; | |
62 | unsigned long last_dock_time; | |
63 | u32 flags; | |
64 | spinlock_t dd_lock; | |
8b0dc866 | 65 | struct mutex hp_lock; |
c8f7a62c LB |
66 | struct list_head dependent_devices; |
67 | struct list_head hotplug_devices; | |
68 | }; | |
69 | ||
70 | struct dock_dependent_device { | |
71 | struct list_head list; | |
72 | struct list_head hotplug_list; | |
73 | acpi_handle handle; | |
74 | acpi_notify_handler handler; | |
75 | void *context; | |
76 | }; | |
77 | ||
78 | #define DOCK_DOCKING 0x00000001 | |
a0cd35fd | 79 | #define DOCK_UNDOCKING 0x00000002 |
5669021e KCA |
80 | #define DOCK_EVENT 3 |
81 | #define UNDOCK_EVENT 2 | |
c8f7a62c LB |
82 | |
83 | static struct dock_station *dock_station; | |
84 | ||
85 | /***************************************************************************** | |
86 | * Dock Dependent device functions * | |
87 | *****************************************************************************/ | |
88 | /** | |
89 | * alloc_dock_dependent_device - allocate and init a dependent device | |
90 | * @handle: the acpi_handle of the dependent device | |
91 | * | |
92 | * Allocate memory for a dependent device structure for a device referenced | |
93 | * by the acpi handle | |
94 | */ | |
95 | static struct dock_dependent_device * | |
96 | alloc_dock_dependent_device(acpi_handle handle) | |
97 | { | |
98 | struct dock_dependent_device *dd; | |
99 | ||
100 | dd = kzalloc(sizeof(*dd), GFP_KERNEL); | |
101 | if (dd) { | |
102 | dd->handle = handle; | |
103 | INIT_LIST_HEAD(&dd->list); | |
104 | INIT_LIST_HEAD(&dd->hotplug_list); | |
105 | } | |
106 | return dd; | |
107 | } | |
108 | ||
109 | /** | |
110 | * add_dock_dependent_device - associate a device with the dock station | |
111 | * @ds: The dock station | |
112 | * @dd: The dependent device | |
113 | * | |
114 | * Add the dependent device to the dock's dependent device list. | |
115 | */ | |
116 | static void | |
117 | add_dock_dependent_device(struct dock_station *ds, | |
118 | struct dock_dependent_device *dd) | |
119 | { | |
120 | spin_lock(&ds->dd_lock); | |
121 | list_add_tail(&dd->list, &ds->dependent_devices); | |
122 | spin_unlock(&ds->dd_lock); | |
123 | } | |
124 | ||
125 | /** | |
126 | * dock_add_hotplug_device - associate a hotplug handler with the dock station | |
127 | * @ds: The dock station | |
128 | * @dd: The dependent device struct | |
129 | * | |
130 | * Add the dependent device to the dock's hotplug device list | |
131 | */ | |
132 | static void | |
133 | dock_add_hotplug_device(struct dock_station *ds, | |
134 | struct dock_dependent_device *dd) | |
135 | { | |
8b0dc866 | 136 | mutex_lock(&ds->hp_lock); |
c8f7a62c | 137 | list_add_tail(&dd->hotplug_list, &ds->hotplug_devices); |
8b0dc866 | 138 | mutex_unlock(&ds->hp_lock); |
c8f7a62c LB |
139 | } |
140 | ||
141 | /** | |
142 | * dock_del_hotplug_device - remove a hotplug handler from the dock station | |
143 | * @ds: The dock station | |
144 | * @dd: the dependent device struct | |
145 | * | |
146 | * Delete the dependent device from the dock's hotplug device list | |
147 | */ | |
148 | static void | |
149 | dock_del_hotplug_device(struct dock_station *ds, | |
150 | struct dock_dependent_device *dd) | |
151 | { | |
8b0dc866 | 152 | mutex_lock(&ds->hp_lock); |
c8f7a62c | 153 | list_del(&dd->hotplug_list); |
8b0dc866 | 154 | mutex_unlock(&ds->hp_lock); |
c8f7a62c LB |
155 | } |
156 | ||
157 | /** | |
158 | * find_dock_dependent_device - get a device dependent on this dock | |
159 | * @ds: the dock station | |
160 | * @handle: the acpi_handle of the device we want | |
161 | * | |
162 | * iterate over the dependent device list for this dock. If the | |
163 | * dependent device matches the handle, return. | |
164 | */ | |
165 | static struct dock_dependent_device * | |
166 | find_dock_dependent_device(struct dock_station *ds, acpi_handle handle) | |
167 | { | |
168 | struct dock_dependent_device *dd; | |
169 | ||
170 | spin_lock(&ds->dd_lock); | |
171 | list_for_each_entry(dd, &ds->dependent_devices, list) { | |
172 | if (handle == dd->handle) { | |
173 | spin_unlock(&ds->dd_lock); | |
174 | return dd; | |
175 | } | |
176 | } | |
177 | spin_unlock(&ds->dd_lock); | |
178 | return NULL; | |
179 | } | |
180 | ||
181 | /***************************************************************************** | |
182 | * Dock functions * | |
183 | *****************************************************************************/ | |
184 | /** | |
185 | * is_dock - see if a device is a dock station | |
186 | * @handle: acpi handle of the device | |
187 | * | |
188 | * If an acpi object has a _DCK method, then it is by definition a dock | |
189 | * station, so return true. | |
190 | */ | |
191 | static int is_dock(acpi_handle handle) | |
192 | { | |
193 | acpi_status status; | |
194 | acpi_handle tmp; | |
195 | ||
196 | status = acpi_get_handle(handle, "_DCK", &tmp); | |
197 | if (ACPI_FAILURE(status)) | |
198 | return 0; | |
199 | return 1; | |
200 | } | |
201 | ||
202 | /** | |
203 | * is_dock_device - see if a device is on a dock station | |
204 | * @handle: acpi handle of the device | |
205 | * | |
206 | * If this device is either the dock station itself, | |
207 | * or is a device dependent on the dock station, then it | |
208 | * is a dock device | |
209 | */ | |
210 | int is_dock_device(acpi_handle handle) | |
211 | { | |
212 | if (!dock_station) | |
213 | return 0; | |
214 | ||
215 | if (is_dock(handle) || find_dock_dependent_device(dock_station, handle)) | |
216 | return 1; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
221 | EXPORT_SYMBOL_GPL(is_dock_device); | |
222 | ||
223 | /** | |
224 | * dock_present - see if the dock station is present. | |
225 | * @ds: the dock station | |
226 | * | |
227 | * execute the _STA method. note that present does not | |
228 | * imply that we are docked. | |
229 | */ | |
230 | static int dock_present(struct dock_station *ds) | |
231 | { | |
232 | unsigned long sta; | |
233 | acpi_status status; | |
234 | ||
235 | if (ds) { | |
236 | status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta); | |
237 | if (ACPI_SUCCESS(status) && sta) | |
238 | return 1; | |
239 | } | |
240 | return 0; | |
241 | } | |
242 | ||
243 | ||
244 | ||
245 | /** | |
246 | * dock_create_acpi_device - add new devices to acpi | |
247 | * @handle - handle of the device to add | |
248 | * | |
249 | * This function will create a new acpi_device for the given | |
250 | * handle if one does not exist already. This should cause | |
251 | * acpi to scan for drivers for the given devices, and call | |
252 | * matching driver's add routine. | |
253 | * | |
254 | * Returns a pointer to the acpi_device corresponding to the handle. | |
255 | */ | |
256 | static struct acpi_device * dock_create_acpi_device(acpi_handle handle) | |
257 | { | |
258 | struct acpi_device *device = NULL; | |
259 | struct acpi_device *parent_device; | |
260 | acpi_handle parent; | |
261 | int ret; | |
262 | ||
263 | if (acpi_bus_get_device(handle, &device)) { | |
264 | /* | |
265 | * no device created for this object, | |
266 | * so we should create one. | |
267 | */ | |
268 | acpi_get_parent(handle, &parent); | |
269 | if (acpi_bus_get_device(parent, &parent_device)) | |
270 | parent_device = NULL; | |
271 | ||
272 | ret = acpi_bus_add(&device, parent_device, handle, | |
273 | ACPI_BUS_TYPE_DEVICE); | |
274 | if (ret) { | |
275 | pr_debug("error adding bus, %x\n", | |
276 | -ret); | |
277 | return NULL; | |
278 | } | |
279 | } | |
280 | return device; | |
281 | } | |
282 | ||
283 | /** | |
284 | * dock_remove_acpi_device - remove the acpi_device struct from acpi | |
285 | * @handle - the handle of the device to remove | |
286 | * | |
287 | * Tell acpi to remove the acpi_device. This should cause any loaded | |
288 | * driver to have it's remove routine called. | |
289 | */ | |
290 | static void dock_remove_acpi_device(acpi_handle handle) | |
291 | { | |
292 | struct acpi_device *device; | |
293 | int ret; | |
294 | ||
295 | if (!acpi_bus_get_device(handle, &device)) { | |
296 | ret = acpi_bus_trim(device, 1); | |
297 | if (ret) | |
298 | pr_debug("error removing bus, %x\n", -ret); | |
299 | } | |
300 | } | |
301 | ||
302 | ||
303 | /** | |
304 | * hotplug_dock_devices - insert or remove devices on the dock station | |
305 | * @ds: the dock station | |
306 | * @event: either bus check or eject request | |
307 | * | |
308 | * Some devices on the dock station need to have drivers called | |
309 | * to perform hotplug operations after a dock event has occurred. | |
310 | * Traverse the list of dock devices that have registered a | |
311 | * hotplug handler, and call the handler. | |
312 | */ | |
313 | static void hotplug_dock_devices(struct dock_station *ds, u32 event) | |
314 | { | |
315 | struct dock_dependent_device *dd; | |
316 | ||
8b0dc866 | 317 | mutex_lock(&ds->hp_lock); |
c8f7a62c LB |
318 | |
319 | /* | |
320 | * First call driver specific hotplug functions | |
321 | */ | |
322 | list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) { | |
323 | if (dd->handler) | |
324 | dd->handler(dd->handle, event, dd->context); | |
325 | } | |
326 | ||
327 | /* | |
328 | * Now make sure that an acpi_device is created for each | |
329 | * dependent device, or removed if this is an eject request. | |
330 | * This will cause acpi_drivers to be stopped/started if they | |
331 | * exist | |
332 | */ | |
333 | list_for_each_entry(dd, &ds->dependent_devices, list) { | |
334 | if (event == ACPI_NOTIFY_EJECT_REQUEST) | |
335 | dock_remove_acpi_device(dd->handle); | |
336 | else | |
337 | dock_create_acpi_device(dd->handle); | |
338 | } | |
8b0dc866 | 339 | mutex_unlock(&ds->hp_lock); |
c8f7a62c LB |
340 | } |
341 | ||
342 | static void dock_event(struct dock_station *ds, u32 event, int num) | |
343 | { | |
0f6f2804 | 344 | struct device *dev = &dock_device->dev; |
66b56821 | 345 | char event_string[13]; |
79a8f70b KCA |
346 | char *envp[] = { event_string, NULL }; |
347 | ||
348 | if (num == UNDOCK_EVENT) | |
66b56821 | 349 | sprintf(event_string, "EVENT=undock"); |
79a8f70b | 350 | else |
66b56821 | 351 | sprintf(event_string, "EVENT=dock"); |
79a8f70b | 352 | |
5669021e | 353 | /* |
8ea86e0b KCA |
354 | * Indicate that the status of the dock station has |
355 | * changed. | |
5669021e | 356 | */ |
79a8f70b | 357 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); |
c8f7a62c LB |
358 | } |
359 | ||
360 | /** | |
361 | * eject_dock - respond to a dock eject request | |
362 | * @ds: the dock station | |
363 | * | |
364 | * This is called after _DCK is called, to execute the dock station's | |
365 | * _EJ0 method. | |
366 | */ | |
367 | static void eject_dock(struct dock_station *ds) | |
368 | { | |
369 | struct acpi_object_list arg_list; | |
370 | union acpi_object arg; | |
371 | acpi_status status; | |
372 | acpi_handle tmp; | |
373 | ||
374 | /* all dock devices should have _EJ0, but check anyway */ | |
375 | status = acpi_get_handle(ds->handle, "_EJ0", &tmp); | |
376 | if (ACPI_FAILURE(status)) { | |
377 | pr_debug("No _EJ0 support for dock device\n"); | |
378 | return; | |
379 | } | |
380 | ||
381 | arg_list.count = 1; | |
382 | arg_list.pointer = &arg; | |
383 | arg.type = ACPI_TYPE_INTEGER; | |
384 | arg.integer.value = 1; | |
385 | ||
386 | if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0", | |
387 | &arg_list, NULL))) | |
388 | pr_debug("Failed to evaluate _EJ0!\n"); | |
389 | } | |
390 | ||
391 | /** | |
392 | * handle_dock - handle a dock event | |
393 | * @ds: the dock station | |
394 | * @dock: to dock, or undock - that is the question | |
395 | * | |
396 | * Execute the _DCK method in response to an acpi event | |
397 | */ | |
398 | static void handle_dock(struct dock_station *ds, int dock) | |
399 | { | |
400 | acpi_status status; | |
401 | struct acpi_object_list arg_list; | |
402 | union acpi_object arg; | |
403 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
404 | struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
c8f7a62c LB |
405 | |
406 | acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer); | |
c8f7a62c | 407 | |
9254bc84 DT |
408 | printk(KERN_INFO PREFIX "%s - %s\n", |
409 | (char *)name_buffer.pointer, dock ? "docking" : "undocking"); | |
c8f7a62c LB |
410 | |
411 | /* _DCK method has one argument */ | |
412 | arg_list.count = 1; | |
413 | arg_list.pointer = &arg; | |
414 | arg.type = ACPI_TYPE_INTEGER; | |
415 | arg.integer.value = dock; | |
416 | status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer); | |
417 | if (ACPI_FAILURE(status)) | |
9254bc84 DT |
418 | printk(KERN_ERR PREFIX "%s - failed to execute _DCK\n", |
419 | (char *)name_buffer.pointer); | |
c8f7a62c LB |
420 | kfree(buffer.pointer); |
421 | kfree(name_buffer.pointer); | |
422 | } | |
423 | ||
424 | static inline void dock(struct dock_station *ds) | |
425 | { | |
426 | handle_dock(ds, 1); | |
427 | } | |
428 | ||
429 | static inline void undock(struct dock_station *ds) | |
430 | { | |
431 | handle_dock(ds, 0); | |
432 | } | |
433 | ||
434 | static inline void begin_dock(struct dock_station *ds) | |
435 | { | |
436 | ds->flags |= DOCK_DOCKING; | |
437 | } | |
438 | ||
439 | static inline void complete_dock(struct dock_station *ds) | |
440 | { | |
441 | ds->flags &= ~(DOCK_DOCKING); | |
442 | ds->last_dock_time = jiffies; | |
443 | } | |
444 | ||
a0cd35fd KCA |
445 | static inline void begin_undock(struct dock_station *ds) |
446 | { | |
447 | ds->flags |= DOCK_UNDOCKING; | |
448 | } | |
449 | ||
450 | static inline void complete_undock(struct dock_station *ds) | |
451 | { | |
452 | ds->flags &= ~(DOCK_UNDOCKING); | |
453 | } | |
454 | ||
c8f7a62c LB |
455 | /** |
456 | * dock_in_progress - see if we are in the middle of handling a dock event | |
457 | * @ds: the dock station | |
458 | * | |
459 | * Sometimes while docking, false dock events can be sent to the driver | |
460 | * because good connections aren't made or some other reason. Ignore these | |
461 | * if we are in the middle of doing something. | |
462 | */ | |
463 | static int dock_in_progress(struct dock_station *ds) | |
464 | { | |
465 | if ((ds->flags & DOCK_DOCKING) || | |
466 | time_before(jiffies, (ds->last_dock_time + HZ))) | |
467 | return 1; | |
468 | return 0; | |
469 | } | |
470 | ||
471 | /** | |
472 | * register_dock_notifier - add yourself to the dock notifier list | |
473 | * @nb: the callers notifier block | |
474 | * | |
475 | * If a driver wishes to be notified about dock events, they can | |
476 | * use this function to put a notifier block on the dock notifier list. | |
477 | * this notifier call chain will be called after a dock event, but | |
478 | * before hotplugging any new devices. | |
479 | */ | |
480 | int register_dock_notifier(struct notifier_block *nb) | |
481 | { | |
2548c06b PB |
482 | if (!dock_station) |
483 | return -ENODEV; | |
484 | ||
c8f7a62c LB |
485 | return atomic_notifier_chain_register(&dock_notifier_list, nb); |
486 | } | |
487 | ||
488 | EXPORT_SYMBOL_GPL(register_dock_notifier); | |
489 | ||
490 | /** | |
491 | * unregister_dock_notifier - remove yourself from the dock notifier list | |
492 | * @nb: the callers notifier block | |
493 | */ | |
494 | void unregister_dock_notifier(struct notifier_block *nb) | |
495 | { | |
2548c06b PB |
496 | if (!dock_station) |
497 | return; | |
498 | ||
c8f7a62c LB |
499 | atomic_notifier_chain_unregister(&dock_notifier_list, nb); |
500 | } | |
501 | ||
502 | EXPORT_SYMBOL_GPL(unregister_dock_notifier); | |
503 | ||
504 | /** | |
505 | * register_hotplug_dock_device - register a hotplug function | |
506 | * @handle: the handle of the device | |
507 | * @handler: the acpi_notifier_handler to call after docking | |
508 | * @context: device specific data | |
509 | * | |
510 | * If a driver would like to perform a hotplug operation after a dock | |
511 | * event, they can register an acpi_notifiy_handler to be called by | |
512 | * the dock driver after _DCK is executed. | |
513 | */ | |
514 | int | |
515 | register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler, | |
516 | void *context) | |
517 | { | |
518 | struct dock_dependent_device *dd; | |
519 | ||
520 | if (!dock_station) | |
521 | return -ENODEV; | |
522 | ||
523 | /* | |
524 | * make sure this handle is for a device dependent on the dock, | |
525 | * this would include the dock station itself | |
526 | */ | |
527 | dd = find_dock_dependent_device(dock_station, handle); | |
528 | if (dd) { | |
529 | dd->handler = handler; | |
530 | dd->context = context; | |
531 | dock_add_hotplug_device(dock_station, dd); | |
532 | return 0; | |
533 | } | |
534 | ||
535 | return -EINVAL; | |
536 | } | |
537 | ||
538 | EXPORT_SYMBOL_GPL(register_hotplug_dock_device); | |
539 | ||
540 | /** | |
541 | * unregister_hotplug_dock_device - remove yourself from the hotplug list | |
542 | * @handle: the acpi handle of the device | |
543 | */ | |
544 | void unregister_hotplug_dock_device(acpi_handle handle) | |
545 | { | |
546 | struct dock_dependent_device *dd; | |
547 | ||
548 | if (!dock_station) | |
549 | return; | |
550 | ||
551 | dd = find_dock_dependent_device(dock_station, handle); | |
552 | if (dd) | |
553 | dock_del_hotplug_device(dock_station, dd); | |
554 | } | |
555 | ||
556 | EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device); | |
557 | ||
c80fdbe8 | 558 | /** |
559 | * handle_eject_request - handle an undock request checking for error conditions | |
560 | * | |
561 | * Check to make sure the dock device is still present, then undock and | |
562 | * hotremove all the devices that may need removing. | |
563 | */ | |
564 | static int handle_eject_request(struct dock_station *ds, u32 event) | |
565 | { | |
c80fdbe8 | 566 | if (dock_in_progress(ds)) |
567 | return -EBUSY; | |
568 | ||
569 | /* | |
570 | * here we need to generate the undock | |
571 | * event prior to actually doing the undock | |
572 | * so that the device struct still exists. | |
afd7301d HM |
573 | * Also, even send the dock event if the |
574 | * device is not present anymore | |
c80fdbe8 | 575 | */ |
576 | dock_event(ds, event, UNDOCK_EVENT); | |
afd7301d HM |
577 | |
578 | if (!dock_present(ds)) { | |
579 | complete_undock(ds); | |
580 | return -ENODEV; | |
581 | } | |
582 | ||
c80fdbe8 | 583 | hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST); |
584 | undock(ds); | |
585 | eject_dock(ds); | |
586 | if (dock_present(ds)) { | |
587 | printk(KERN_ERR PREFIX "Unable to undock!\n"); | |
588 | return -EBUSY; | |
589 | } | |
a0cd35fd | 590 | complete_undock(ds); |
c80fdbe8 | 591 | return 0; |
592 | } | |
593 | ||
c8f7a62c LB |
594 | /** |
595 | * dock_notify - act upon an acpi dock notification | |
596 | * @handle: the dock station handle | |
597 | * @event: the acpi event | |
598 | * @data: our driver data struct | |
599 | * | |
600 | * If we are notified to dock, then check to see if the dock is | |
601 | * present and then dock. Notify all drivers of the dock event, | |
c80fdbe8 | 602 | * and then hotplug and devices that may need hotplugging. |
c8f7a62c LB |
603 | */ |
604 | static void dock_notify(acpi_handle handle, u32 event, void *data) | |
605 | { | |
50dd0969 | 606 | struct dock_station *ds = data; |
c8f7a62c LB |
607 | |
608 | switch (event) { | |
609 | case ACPI_NOTIFY_BUS_CHECK: | |
610 | if (!dock_in_progress(ds) && dock_present(ds)) { | |
611 | begin_dock(ds); | |
612 | dock(ds); | |
613 | if (!dock_present(ds)) { | |
614 | printk(KERN_ERR PREFIX "Unable to dock!\n"); | |
615 | break; | |
616 | } | |
617 | atomic_notifier_call_chain(&dock_notifier_list, | |
618 | event, NULL); | |
619 | hotplug_dock_devices(ds, event); | |
620 | complete_dock(ds); | |
621 | dock_event(ds, event, DOCK_EVENT); | |
622 | } | |
623 | break; | |
624 | case ACPI_NOTIFY_DEVICE_CHECK: | |
625 | /* | |
626 | * According to acpi spec 3.0a, if a DEVICE_CHECK notification | |
627 | * is sent and _DCK is present, it is assumed to mean an | |
628 | * undock request. This notify routine will only be called | |
629 | * for objects defining _DCK, so we will fall through to eject | |
630 | * request here. However, we will pass an eject request through | |
631 | * to the driver who wish to hotplug. | |
632 | */ | |
633 | case ACPI_NOTIFY_EJECT_REQUEST: | |
a0cd35fd KCA |
634 | begin_undock(ds); |
635 | if (immediate_undock) | |
636 | handle_eject_request(ds, event); | |
637 | else | |
638 | dock_event(ds, event, UNDOCK_EVENT); | |
c8f7a62c LB |
639 | break; |
640 | default: | |
641 | printk(KERN_ERR PREFIX "Unknown dock event %d\n", event); | |
642 | } | |
643 | } | |
644 | ||
645 | /** | |
646 | * find_dock_devices - find devices on the dock station | |
647 | * @handle: the handle of the device we are examining | |
648 | * @lvl: unused | |
649 | * @context: the dock station private data | |
650 | * @rv: unused | |
651 | * | |
652 | * This function is called by acpi_walk_namespace. It will | |
653 | * check to see if an object has an _EJD method. If it does, then it | |
654 | * will see if it is dependent on the dock station. | |
655 | */ | |
656 | static acpi_status | |
657 | find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv) | |
658 | { | |
659 | acpi_status status; | |
fe9a2f77 | 660 | acpi_handle tmp, parent; |
50dd0969 | 661 | struct dock_station *ds = context; |
c8f7a62c LB |
662 | struct dock_dependent_device *dd; |
663 | ||
664 | status = acpi_bus_get_ejd(handle, &tmp); | |
fe9a2f77 KCA |
665 | if (ACPI_FAILURE(status)) { |
666 | /* try the parent device as well */ | |
667 | status = acpi_get_parent(handle, &parent); | |
668 | if (ACPI_FAILURE(status)) | |
669 | goto fdd_out; | |
670 | /* see if parent is dependent on dock */ | |
671 | status = acpi_bus_get_ejd(parent, &tmp); | |
672 | if (ACPI_FAILURE(status)) | |
673 | goto fdd_out; | |
674 | } | |
c8f7a62c LB |
675 | |
676 | if (tmp == ds->handle) { | |
677 | dd = alloc_dock_dependent_device(handle); | |
678 | if (dd) | |
679 | add_dock_dependent_device(ds, dd); | |
680 | } | |
fe9a2f77 | 681 | fdd_out: |
c8f7a62c LB |
682 | return AE_OK; |
683 | } | |
684 | ||
c80fdbe8 | 685 | /* |
686 | * show_docked - read method for "docked" file in sysfs | |
687 | */ | |
688 | static ssize_t show_docked(struct device *dev, | |
689 | struct device_attribute *attr, char *buf) | |
690 | { | |
691 | return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station)); | |
692 | ||
693 | } | |
e5685b9d | 694 | static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL); |
c80fdbe8 | 695 | |
a0cd35fd KCA |
696 | /* |
697 | * show_flags - read method for flags file in sysfs | |
698 | */ | |
699 | static ssize_t show_flags(struct device *dev, | |
700 | struct device_attribute *attr, char *buf) | |
701 | { | |
702 | return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags); | |
703 | ||
704 | } | |
e5685b9d | 705 | static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL); |
a0cd35fd | 706 | |
c80fdbe8 | 707 | /* |
708 | * write_undock - write method for "undock" file in sysfs | |
709 | */ | |
710 | static ssize_t write_undock(struct device *dev, struct device_attribute *attr, | |
711 | const char *buf, size_t count) | |
712 | { | |
713 | int ret; | |
714 | ||
715 | if (!count) | |
716 | return -EINVAL; | |
717 | ||
9171f834 | 718 | begin_undock(dock_station); |
c80fdbe8 | 719 | ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST); |
720 | return ret ? ret: count; | |
721 | } | |
e5685b9d | 722 | static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock); |
c80fdbe8 | 723 | |
ac122bb6 IVE |
724 | /* |
725 | * show_dock_uid - read method for "uid" file in sysfs | |
726 | */ | |
727 | static ssize_t show_dock_uid(struct device *dev, | |
728 | struct device_attribute *attr, char *buf) | |
729 | { | |
730 | unsigned long lbuf; | |
38ff4ffc KCA |
731 | acpi_status status = acpi_evaluate_integer(dock_station->handle, |
732 | "_UID", NULL, &lbuf); | |
733 | if (ACPI_FAILURE(status)) | |
ac122bb6 | 734 | return 0; |
38ff4ffc | 735 | |
ac122bb6 IVE |
736 | return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf); |
737 | } | |
e5685b9d | 738 | static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL); |
ac122bb6 | 739 | |
c8f7a62c LB |
740 | /** |
741 | * dock_add - add a new dock station | |
742 | * @handle: the dock station handle | |
743 | * | |
744 | * allocated and initialize a new dock station device. Find all devices | |
745 | * that are on the dock station, and register for dock event notifications. | |
746 | */ | |
747 | static int dock_add(acpi_handle handle) | |
748 | { | |
749 | int ret; | |
750 | acpi_status status; | |
751 | struct dock_dependent_device *dd; | |
752 | ||
753 | /* allocate & initialize the dock_station private data */ | |
754 | dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL); | |
755 | if (!dock_station) | |
756 | return -ENOMEM; | |
757 | dock_station->handle = handle; | |
758 | dock_station->last_dock_time = jiffies - HZ; | |
759 | INIT_LIST_HEAD(&dock_station->dependent_devices); | |
760 | INIT_LIST_HEAD(&dock_station->hotplug_devices); | |
761 | spin_lock_init(&dock_station->dd_lock); | |
8b0dc866 | 762 | mutex_init(&dock_station->hp_lock); |
07a18684 | 763 | ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list); |
c8f7a62c | 764 | |
671adbec | 765 | /* initialize platform device stuff */ |
0f6f2804 KCA |
766 | dock_device = |
767 | platform_device_register_simple(dock_device_name, 0, NULL, 0); | |
768 | if (IS_ERR(dock_device)) { | |
671adbec | 769 | kfree(dock_station); |
22fe4c21 | 770 | dock_station = NULL; |
0f6f2804 | 771 | return PTR_ERR(dock_device); |
671adbec | 772 | } |
a0cd35fd | 773 | |
9ef2a9a9 KCA |
774 | /* we want the dock device to send uevents */ |
775 | dock_device->dev.uevent_suppress = 0; | |
776 | ||
0f6f2804 | 777 | ret = device_create_file(&dock_device->dev, &dev_attr_docked); |
c80fdbe8 | 778 | if (ret) { |
779 | printk("Error %d adding sysfs file\n", ret); | |
0f6f2804 | 780 | platform_device_unregister(dock_device); |
c80fdbe8 | 781 | kfree(dock_station); |
22fe4c21 | 782 | dock_station = NULL; |
c80fdbe8 | 783 | return ret; |
784 | } | |
0f6f2804 | 785 | ret = device_create_file(&dock_device->dev, &dev_attr_undock); |
c80fdbe8 | 786 | if (ret) { |
787 | printk("Error %d adding sysfs file\n", ret); | |
0f6f2804 KCA |
788 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
789 | platform_device_unregister(dock_device); | |
c80fdbe8 | 790 | kfree(dock_station); |
22fe4c21 | 791 | dock_station = NULL; |
c80fdbe8 | 792 | return ret; |
793 | } | |
0f6f2804 | 794 | ret = device_create_file(&dock_device->dev, &dev_attr_uid); |
ac122bb6 IVE |
795 | if (ret) { |
796 | printk("Error %d adding sysfs file\n", ret); | |
0f6f2804 KCA |
797 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
798 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
799 | platform_device_unregister(dock_device); | |
ac122bb6 | 800 | kfree(dock_station); |
22fe4c21 | 801 | dock_station = NULL; |
ac122bb6 IVE |
802 | return ret; |
803 | } | |
a0cd35fd KCA |
804 | ret = device_create_file(&dock_device->dev, &dev_attr_flags); |
805 | if (ret) { | |
806 | printk("Error %d adding sysfs file\n", ret); | |
807 | device_remove_file(&dock_device->dev, &dev_attr_docked); | |
808 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
809 | device_remove_file(&dock_device->dev, &dev_attr_uid); | |
810 | platform_device_unregister(dock_device); | |
811 | kfree(dock_station); | |
812 | dock_station = NULL; | |
813 | return ret; | |
814 | } | |
671adbec | 815 | |
c8f7a62c LB |
816 | /* Find dependent devices */ |
817 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
818 | ACPI_UINT32_MAX, find_dock_devices, dock_station, | |
819 | NULL); | |
820 | ||
821 | /* add the dock station as a device dependent on itself */ | |
822 | dd = alloc_dock_dependent_device(handle); | |
823 | if (!dd) { | |
824 | kfree(dock_station); | |
22fe4c21 | 825 | dock_station = NULL; |
671adbec KCA |
826 | ret = -ENOMEM; |
827 | goto dock_add_err_unregister; | |
c8f7a62c LB |
828 | } |
829 | add_dock_dependent_device(dock_station, dd); | |
830 | ||
831 | /* register for dock events */ | |
832 | status = acpi_install_notify_handler(dock_station->handle, | |
833 | ACPI_SYSTEM_NOTIFY, | |
834 | dock_notify, dock_station); | |
835 | ||
836 | if (ACPI_FAILURE(status)) { | |
837 | printk(KERN_ERR PREFIX "Error installing notify handler\n"); | |
838 | ret = -ENODEV; | |
839 | goto dock_add_err; | |
840 | } | |
841 | ||
1fdd6860 | 842 | printk(KERN_INFO PREFIX "%s\n", ACPI_DOCK_DRIVER_DESCRIPTION); |
c8f7a62c LB |
843 | |
844 | return 0; | |
845 | ||
846 | dock_add_err: | |
c8f7a62c | 847 | kfree(dd); |
671adbec | 848 | dock_add_err_unregister: |
0f6f2804 KCA |
849 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
850 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
851 | device_remove_file(&dock_device->dev, &dev_attr_uid); | |
a0cd35fd | 852 | device_remove_file(&dock_device->dev, &dev_attr_flags); |
0f6f2804 | 853 | platform_device_unregister(dock_device); |
671adbec | 854 | kfree(dock_station); |
22fe4c21 | 855 | dock_station = NULL; |
c8f7a62c LB |
856 | return ret; |
857 | } | |
858 | ||
859 | /** | |
860 | * dock_remove - free up resources related to the dock station | |
861 | */ | |
862 | static int dock_remove(void) | |
863 | { | |
864 | struct dock_dependent_device *dd, *tmp; | |
865 | acpi_status status; | |
866 | ||
867 | if (!dock_station) | |
868 | return 0; | |
869 | ||
870 | /* remove dependent devices */ | |
871 | list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices, | |
872 | list) | |
873 | kfree(dd); | |
874 | ||
875 | /* remove dock notify handler */ | |
876 | status = acpi_remove_notify_handler(dock_station->handle, | |
877 | ACPI_SYSTEM_NOTIFY, | |
878 | dock_notify); | |
879 | if (ACPI_FAILURE(status)) | |
880 | printk(KERN_ERR "Error removing notify handler\n"); | |
881 | ||
671adbec | 882 | /* cleanup sysfs */ |
0f6f2804 KCA |
883 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
884 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
885 | device_remove_file(&dock_device->dev, &dev_attr_uid); | |
a0cd35fd | 886 | device_remove_file(&dock_device->dev, &dev_attr_flags); |
0f6f2804 | 887 | platform_device_unregister(dock_device); |
671adbec | 888 | |
c8f7a62c LB |
889 | /* free dock station memory */ |
890 | kfree(dock_station); | |
22fe4c21 | 891 | dock_station = NULL; |
c8f7a62c LB |
892 | return 0; |
893 | } | |
894 | ||
895 | /** | |
896 | * find_dock - look for a dock station | |
897 | * @handle: acpi handle of a device | |
898 | * @lvl: unused | |
899 | * @context: counter of dock stations found | |
900 | * @rv: unused | |
901 | * | |
902 | * This is called by acpi_walk_namespace to look for dock stations. | |
903 | */ | |
904 | static acpi_status | |
905 | find_dock(acpi_handle handle, u32 lvl, void *context, void **rv) | |
906 | { | |
50dd0969 | 907 | int *count = context; |
c8f7a62c LB |
908 | acpi_status status = AE_OK; |
909 | ||
910 | if (is_dock(handle)) { | |
911 | if (dock_add(handle) >= 0) { | |
912 | (*count)++; | |
913 | status = AE_CTRL_TERMINATE; | |
914 | } | |
915 | } | |
916 | return status; | |
917 | } | |
918 | ||
919 | static int __init dock_init(void) | |
920 | { | |
921 | int num = 0; | |
922 | ||
923 | dock_station = NULL; | |
924 | ||
816c2eda LB |
925 | if (acpi_disabled) |
926 | return 0; | |
927 | ||
c8f7a62c LB |
928 | /* look for a dock station */ |
929 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
930 | ACPI_UINT32_MAX, find_dock, &num, NULL); | |
931 | ||
932 | if (!num) | |
2548c06b | 933 | printk(KERN_INFO "No dock devices found.\n"); |
c8f7a62c LB |
934 | |
935 | return 0; | |
936 | } | |
937 | ||
938 | static void __exit dock_exit(void) | |
939 | { | |
940 | dock_remove(); | |
941 | } | |
942 | ||
943 | postcore_initcall(dock_init); | |
944 | module_exit(dock_exit); |