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; |
671adbec | 51 | static char dock_device_name[] = "dock"; |
c8f7a62c | 52 | |
a340af14 FS |
53 | static const struct acpi_device_id dock_device_ids[] = { |
54 | {"LNXDOCK", 0}, | |
55 | {"", 0}, | |
56 | }; | |
57 | MODULE_DEVICE_TABLE(acpi, dock_device_ids); | |
58 | ||
c8f7a62c LB |
59 | struct dock_station { |
60 | acpi_handle handle; | |
61 | unsigned long last_dock_time; | |
62 | u32 flags; | |
63 | spinlock_t dd_lock; | |
8b0dc866 | 64 | struct mutex hp_lock; |
c8f7a62c LB |
65 | struct list_head dependent_devices; |
66 | struct list_head hotplug_devices; | |
db350b08 SL |
67 | |
68 | struct list_head sibiling; | |
69 | struct platform_device *dock_device; | |
c8f7a62c | 70 | }; |
db350b08 SL |
71 | static LIST_HEAD(dock_stations); |
72 | static int dock_station_count; | |
c8f7a62c LB |
73 | |
74 | struct dock_dependent_device { | |
75 | struct list_head list; | |
76 | struct list_head hotplug_list; | |
77 | acpi_handle handle; | |
1253f7aa | 78 | struct acpi_dock_ops *ops; |
c8f7a62c LB |
79 | void *context; |
80 | }; | |
81 | ||
82 | #define DOCK_DOCKING 0x00000001 | |
a0cd35fd | 83 | #define DOCK_UNDOCKING 0x00000002 |
db350b08 SL |
84 | #define DOCK_IS_DOCK 0x00000010 |
85 | #define DOCK_IS_ATA 0x00000020 | |
86 | #define DOCK_IS_BAT 0x00000040 | |
5669021e KCA |
87 | #define DOCK_EVENT 3 |
88 | #define UNDOCK_EVENT 2 | |
c8f7a62c | 89 | |
c8f7a62c LB |
90 | /***************************************************************************** |
91 | * Dock Dependent device functions * | |
92 | *****************************************************************************/ | |
93 | /** | |
94 | * alloc_dock_dependent_device - allocate and init a dependent device | |
95 | * @handle: the acpi_handle of the dependent device | |
96 | * | |
97 | * Allocate memory for a dependent device structure for a device referenced | |
98 | * by the acpi handle | |
99 | */ | |
100 | static struct dock_dependent_device * | |
101 | alloc_dock_dependent_device(acpi_handle handle) | |
102 | { | |
103 | struct dock_dependent_device *dd; | |
104 | ||
105 | dd = kzalloc(sizeof(*dd), GFP_KERNEL); | |
106 | if (dd) { | |
107 | dd->handle = handle; | |
108 | INIT_LIST_HEAD(&dd->list); | |
109 | INIT_LIST_HEAD(&dd->hotplug_list); | |
110 | } | |
111 | return dd; | |
112 | } | |
113 | ||
114 | /** | |
115 | * add_dock_dependent_device - associate a device with the dock station | |
116 | * @ds: The dock station | |
117 | * @dd: The dependent device | |
118 | * | |
119 | * Add the dependent device to the dock's dependent device list. | |
120 | */ | |
121 | static void | |
122 | add_dock_dependent_device(struct dock_station *ds, | |
123 | struct dock_dependent_device *dd) | |
124 | { | |
125 | spin_lock(&ds->dd_lock); | |
126 | list_add_tail(&dd->list, &ds->dependent_devices); | |
127 | spin_unlock(&ds->dd_lock); | |
128 | } | |
129 | ||
130 | /** | |
131 | * dock_add_hotplug_device - associate a hotplug handler with the dock station | |
132 | * @ds: The dock station | |
133 | * @dd: The dependent device struct | |
134 | * | |
135 | * Add the dependent device to the dock's hotplug device list | |
136 | */ | |
137 | static void | |
138 | dock_add_hotplug_device(struct dock_station *ds, | |
139 | struct dock_dependent_device *dd) | |
140 | { | |
8b0dc866 | 141 | mutex_lock(&ds->hp_lock); |
c8f7a62c | 142 | list_add_tail(&dd->hotplug_list, &ds->hotplug_devices); |
8b0dc866 | 143 | mutex_unlock(&ds->hp_lock); |
c8f7a62c LB |
144 | } |
145 | ||
146 | /** | |
147 | * dock_del_hotplug_device - remove a hotplug handler from the dock station | |
148 | * @ds: The dock station | |
149 | * @dd: the dependent device struct | |
150 | * | |
151 | * Delete the dependent device from the dock's hotplug device list | |
152 | */ | |
153 | static void | |
154 | dock_del_hotplug_device(struct dock_station *ds, | |
155 | struct dock_dependent_device *dd) | |
156 | { | |
8b0dc866 | 157 | mutex_lock(&ds->hp_lock); |
c8f7a62c | 158 | list_del(&dd->hotplug_list); |
8b0dc866 | 159 | mutex_unlock(&ds->hp_lock); |
c8f7a62c LB |
160 | } |
161 | ||
162 | /** | |
163 | * find_dock_dependent_device - get a device dependent on this dock | |
164 | * @ds: the dock station | |
165 | * @handle: the acpi_handle of the device we want | |
166 | * | |
167 | * iterate over the dependent device list for this dock. If the | |
168 | * dependent device matches the handle, return. | |
169 | */ | |
170 | static struct dock_dependent_device * | |
171 | find_dock_dependent_device(struct dock_station *ds, acpi_handle handle) | |
172 | { | |
173 | struct dock_dependent_device *dd; | |
174 | ||
175 | spin_lock(&ds->dd_lock); | |
176 | list_for_each_entry(dd, &ds->dependent_devices, list) { | |
177 | if (handle == dd->handle) { | |
178 | spin_unlock(&ds->dd_lock); | |
179 | return dd; | |
180 | } | |
181 | } | |
182 | spin_unlock(&ds->dd_lock); | |
183 | return NULL; | |
184 | } | |
185 | ||
186 | /***************************************************************************** | |
187 | * Dock functions * | |
188 | *****************************************************************************/ | |
189 | /** | |
190 | * is_dock - see if a device is a dock station | |
191 | * @handle: acpi handle of the device | |
192 | * | |
193 | * If an acpi object has a _DCK method, then it is by definition a dock | |
194 | * station, so return true. | |
195 | */ | |
196 | static int is_dock(acpi_handle handle) | |
197 | { | |
198 | acpi_status status; | |
199 | acpi_handle tmp; | |
200 | ||
201 | status = acpi_get_handle(handle, "_DCK", &tmp); | |
202 | if (ACPI_FAILURE(status)) | |
203 | return 0; | |
204 | return 1; | |
205 | } | |
206 | ||
db350b08 SL |
207 | static int is_ejectable(acpi_handle handle) |
208 | { | |
209 | acpi_status status; | |
210 | acpi_handle tmp; | |
211 | ||
212 | status = acpi_get_handle(handle, "_EJ0", &tmp); | |
213 | if (ACPI_FAILURE(status)) | |
214 | return 0; | |
215 | return 1; | |
216 | } | |
217 | ||
218 | static int is_ata(acpi_handle handle) | |
219 | { | |
220 | acpi_handle tmp; | |
221 | ||
222 | if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || | |
223 | (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || | |
224 | (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || | |
225 | (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) | |
226 | return 1; | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
231 | static int is_battery(acpi_handle handle) | |
232 | { | |
233 | struct acpi_device_info *info; | |
db350b08 SL |
234 | int ret = 1; |
235 | ||
15b8dd53 | 236 | if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info))) |
db350b08 | 237 | return 0; |
db350b08 SL |
238 | if (!(info->valid & ACPI_VALID_HID)) |
239 | ret = 0; | |
240 | else | |
15b8dd53 | 241 | ret = !strcmp("PNP0C0A", info->hardware_id.string); |
db350b08 | 242 | |
15b8dd53 | 243 | kfree(info); |
db350b08 SL |
244 | return ret; |
245 | } | |
246 | ||
247 | static int is_ejectable_bay(acpi_handle handle) | |
248 | { | |
249 | acpi_handle phandle; | |
250 | if (!is_ejectable(handle)) | |
251 | return 0; | |
252 | if (is_battery(handle) || is_ata(handle)) | |
253 | return 1; | |
254 | if (!acpi_get_parent(handle, &phandle) && is_ata(phandle)) | |
255 | return 1; | |
256 | return 0; | |
257 | } | |
258 | ||
c8f7a62c LB |
259 | /** |
260 | * is_dock_device - see if a device is on a dock station | |
261 | * @handle: acpi handle of the device | |
262 | * | |
263 | * If this device is either the dock station itself, | |
264 | * or is a device dependent on the dock station, then it | |
265 | * is a dock device | |
266 | */ | |
267 | int is_dock_device(acpi_handle handle) | |
268 | { | |
db350b08 SL |
269 | struct dock_station *dock_station; |
270 | ||
271 | if (!dock_station_count) | |
c8f7a62c LB |
272 | return 0; |
273 | ||
db350b08 | 274 | if (is_dock(handle)) |
c8f7a62c | 275 | return 1; |
db350b08 SL |
276 | list_for_each_entry(dock_station, &dock_stations, sibiling) { |
277 | if (find_dock_dependent_device(dock_station, handle)) | |
278 | return 1; | |
279 | } | |
c8f7a62c LB |
280 | |
281 | return 0; | |
282 | } | |
283 | ||
284 | EXPORT_SYMBOL_GPL(is_dock_device); | |
285 | ||
286 | /** | |
287 | * dock_present - see if the dock station is present. | |
288 | * @ds: the dock station | |
289 | * | |
290 | * execute the _STA method. note that present does not | |
291 | * imply that we are docked. | |
292 | */ | |
293 | static int dock_present(struct dock_station *ds) | |
294 | { | |
27663c58 | 295 | unsigned long long sta; |
c8f7a62c LB |
296 | acpi_status status; |
297 | ||
298 | if (ds) { | |
299 | status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta); | |
300 | if (ACPI_SUCCESS(status) && sta) | |
301 | return 1; | |
302 | } | |
303 | return 0; | |
304 | } | |
305 | ||
306 | ||
307 | ||
308 | /** | |
309 | * dock_create_acpi_device - add new devices to acpi | |
310 | * @handle - handle of the device to add | |
311 | * | |
312 | * This function will create a new acpi_device for the given | |
313 | * handle if one does not exist already. This should cause | |
314 | * acpi to scan for drivers for the given devices, and call | |
315 | * matching driver's add routine. | |
316 | * | |
317 | * Returns a pointer to the acpi_device corresponding to the handle. | |
318 | */ | |
319 | static struct acpi_device * dock_create_acpi_device(acpi_handle handle) | |
320 | { | |
321 | struct acpi_device *device = NULL; | |
322 | struct acpi_device *parent_device; | |
323 | acpi_handle parent; | |
324 | int ret; | |
325 | ||
326 | if (acpi_bus_get_device(handle, &device)) { | |
327 | /* | |
328 | * no device created for this object, | |
329 | * so we should create one. | |
330 | */ | |
331 | acpi_get_parent(handle, &parent); | |
332 | if (acpi_bus_get_device(parent, &parent_device)) | |
333 | parent_device = NULL; | |
334 | ||
335 | ret = acpi_bus_add(&device, parent_device, handle, | |
336 | ACPI_BUS_TYPE_DEVICE); | |
337 | if (ret) { | |
338 | pr_debug("error adding bus, %x\n", | |
339 | -ret); | |
340 | return NULL; | |
341 | } | |
342 | } | |
343 | return device; | |
344 | } | |
345 | ||
346 | /** | |
347 | * dock_remove_acpi_device - remove the acpi_device struct from acpi | |
348 | * @handle - the handle of the device to remove | |
349 | * | |
350 | * Tell acpi to remove the acpi_device. This should cause any loaded | |
351 | * driver to have it's remove routine called. | |
352 | */ | |
353 | static void dock_remove_acpi_device(acpi_handle handle) | |
354 | { | |
355 | struct acpi_device *device; | |
356 | int ret; | |
357 | ||
358 | if (!acpi_bus_get_device(handle, &device)) { | |
359 | ret = acpi_bus_trim(device, 1); | |
360 | if (ret) | |
361 | pr_debug("error removing bus, %x\n", -ret); | |
362 | } | |
363 | } | |
364 | ||
365 | ||
366 | /** | |
367 | * hotplug_dock_devices - insert or remove devices on the dock station | |
368 | * @ds: the dock station | |
369 | * @event: either bus check or eject request | |
370 | * | |
371 | * Some devices on the dock station need to have drivers called | |
372 | * to perform hotplug operations after a dock event has occurred. | |
373 | * Traverse the list of dock devices that have registered a | |
374 | * hotplug handler, and call the handler. | |
375 | */ | |
376 | static void hotplug_dock_devices(struct dock_station *ds, u32 event) | |
377 | { | |
378 | struct dock_dependent_device *dd; | |
379 | ||
8b0dc866 | 380 | mutex_lock(&ds->hp_lock); |
c8f7a62c LB |
381 | |
382 | /* | |
383 | * First call driver specific hotplug functions | |
384 | */ | |
385 | list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) { | |
1253f7aa SL |
386 | if (dd->ops && dd->ops->handler) |
387 | dd->ops->handler(dd->handle, event, dd->context); | |
c8f7a62c LB |
388 | } |
389 | ||
390 | /* | |
391 | * Now make sure that an acpi_device is created for each | |
392 | * dependent device, or removed if this is an eject request. | |
393 | * This will cause acpi_drivers to be stopped/started if they | |
394 | * exist | |
395 | */ | |
396 | list_for_each_entry(dd, &ds->dependent_devices, list) { | |
397 | if (event == ACPI_NOTIFY_EJECT_REQUEST) | |
398 | dock_remove_acpi_device(dd->handle); | |
399 | else | |
400 | dock_create_acpi_device(dd->handle); | |
401 | } | |
8b0dc866 | 402 | mutex_unlock(&ds->hp_lock); |
c8f7a62c LB |
403 | } |
404 | ||
405 | static void dock_event(struct dock_station *ds, u32 event, int num) | |
406 | { | |
db350b08 | 407 | struct device *dev = &ds->dock_device->dev; |
66b56821 | 408 | char event_string[13]; |
79a8f70b | 409 | char *envp[] = { event_string, NULL }; |
1253f7aa | 410 | struct dock_dependent_device *dd; |
79a8f70b KCA |
411 | |
412 | if (num == UNDOCK_EVENT) | |
66b56821 | 413 | sprintf(event_string, "EVENT=undock"); |
79a8f70b | 414 | else |
66b56821 | 415 | sprintf(event_string, "EVENT=dock"); |
79a8f70b | 416 | |
5669021e | 417 | /* |
8ea86e0b KCA |
418 | * Indicate that the status of the dock station has |
419 | * changed. | |
5669021e | 420 | */ |
1253f7aa SL |
421 | if (num == DOCK_EVENT) |
422 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); | |
423 | ||
424 | list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) | |
425 | if (dd->ops && dd->ops->uevent) | |
426 | dd->ops->uevent(dd->handle, event, dd->context); | |
427 | if (num != DOCK_EVENT) | |
428 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); | |
c8f7a62c LB |
429 | } |
430 | ||
431 | /** | |
432 | * eject_dock - respond to a dock eject request | |
433 | * @ds: the dock station | |
434 | * | |
435 | * This is called after _DCK is called, to execute the dock station's | |
436 | * _EJ0 method. | |
437 | */ | |
438 | static void eject_dock(struct dock_station *ds) | |
439 | { | |
440 | struct acpi_object_list arg_list; | |
441 | union acpi_object arg; | |
442 | acpi_status status; | |
443 | acpi_handle tmp; | |
444 | ||
445 | /* all dock devices should have _EJ0, but check anyway */ | |
446 | status = acpi_get_handle(ds->handle, "_EJ0", &tmp); | |
447 | if (ACPI_FAILURE(status)) { | |
448 | pr_debug("No _EJ0 support for dock device\n"); | |
449 | return; | |
450 | } | |
451 | ||
452 | arg_list.count = 1; | |
453 | arg_list.pointer = &arg; | |
454 | arg.type = ACPI_TYPE_INTEGER; | |
455 | arg.integer.value = 1; | |
456 | ||
457 | if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0", | |
458 | &arg_list, NULL))) | |
459 | pr_debug("Failed to evaluate _EJ0!\n"); | |
460 | } | |
461 | ||
462 | /** | |
463 | * handle_dock - handle a dock event | |
464 | * @ds: the dock station | |
465 | * @dock: to dock, or undock - that is the question | |
466 | * | |
467 | * Execute the _DCK method in response to an acpi event | |
468 | */ | |
469 | static void handle_dock(struct dock_station *ds, int dock) | |
470 | { | |
471 | acpi_status status; | |
472 | struct acpi_object_list arg_list; | |
473 | union acpi_object arg; | |
474 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
475 | struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
c8f7a62c LB |
476 | |
477 | acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer); | |
c8f7a62c | 478 | |
9254bc84 DT |
479 | printk(KERN_INFO PREFIX "%s - %s\n", |
480 | (char *)name_buffer.pointer, dock ? "docking" : "undocking"); | |
c8f7a62c LB |
481 | |
482 | /* _DCK method has one argument */ | |
483 | arg_list.count = 1; | |
484 | arg_list.pointer = &arg; | |
485 | arg.type = ACPI_TYPE_INTEGER; | |
486 | arg.integer.value = dock; | |
487 | status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer); | |
db350b08 | 488 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) |
0a918a94 TR |
489 | ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute" |
490 | " _DCK\n", (char *)name_buffer.pointer)); | |
491 | ||
c8f7a62c LB |
492 | kfree(buffer.pointer); |
493 | kfree(name_buffer.pointer); | |
494 | } | |
495 | ||
496 | static inline void dock(struct dock_station *ds) | |
497 | { | |
498 | handle_dock(ds, 1); | |
499 | } | |
500 | ||
501 | static inline void undock(struct dock_station *ds) | |
502 | { | |
503 | handle_dock(ds, 0); | |
504 | } | |
505 | ||
506 | static inline void begin_dock(struct dock_station *ds) | |
507 | { | |
508 | ds->flags |= DOCK_DOCKING; | |
509 | } | |
510 | ||
511 | static inline void complete_dock(struct dock_station *ds) | |
512 | { | |
513 | ds->flags &= ~(DOCK_DOCKING); | |
514 | ds->last_dock_time = jiffies; | |
515 | } | |
516 | ||
a0cd35fd KCA |
517 | static inline void begin_undock(struct dock_station *ds) |
518 | { | |
519 | ds->flags |= DOCK_UNDOCKING; | |
520 | } | |
521 | ||
522 | static inline void complete_undock(struct dock_station *ds) | |
523 | { | |
524 | ds->flags &= ~(DOCK_UNDOCKING); | |
525 | } | |
526 | ||
406f692d SL |
527 | static void dock_lock(struct dock_station *ds, int lock) |
528 | { | |
529 | struct acpi_object_list arg_list; | |
530 | union acpi_object arg; | |
531 | acpi_status status; | |
532 | ||
533 | arg_list.count = 1; | |
534 | arg_list.pointer = &arg; | |
535 | arg.type = ACPI_TYPE_INTEGER; | |
536 | arg.integer.value = !!lock; | |
537 | status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL); | |
538 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { | |
539 | if (lock) | |
540 | printk(KERN_WARNING PREFIX "Locking device failed\n"); | |
541 | else | |
542 | printk(KERN_WARNING PREFIX "Unlocking device failed\n"); | |
543 | } | |
544 | } | |
545 | ||
c8f7a62c LB |
546 | /** |
547 | * dock_in_progress - see if we are in the middle of handling a dock event | |
548 | * @ds: the dock station | |
549 | * | |
550 | * Sometimes while docking, false dock events can be sent to the driver | |
551 | * because good connections aren't made or some other reason. Ignore these | |
552 | * if we are in the middle of doing something. | |
553 | */ | |
554 | static int dock_in_progress(struct dock_station *ds) | |
555 | { | |
556 | if ((ds->flags & DOCK_DOCKING) || | |
557 | time_before(jiffies, (ds->last_dock_time + HZ))) | |
558 | return 1; | |
559 | return 0; | |
560 | } | |
561 | ||
562 | /** | |
563 | * register_dock_notifier - add yourself to the dock notifier list | |
564 | * @nb: the callers notifier block | |
565 | * | |
566 | * If a driver wishes to be notified about dock events, they can | |
567 | * use this function to put a notifier block on the dock notifier list. | |
568 | * this notifier call chain will be called after a dock event, but | |
569 | * before hotplugging any new devices. | |
570 | */ | |
571 | int register_dock_notifier(struct notifier_block *nb) | |
572 | { | |
db350b08 | 573 | if (!dock_station_count) |
2548c06b PB |
574 | return -ENODEV; |
575 | ||
c8f7a62c LB |
576 | return atomic_notifier_chain_register(&dock_notifier_list, nb); |
577 | } | |
578 | ||
579 | EXPORT_SYMBOL_GPL(register_dock_notifier); | |
580 | ||
581 | /** | |
582 | * unregister_dock_notifier - remove yourself from the dock notifier list | |
583 | * @nb: the callers notifier block | |
584 | */ | |
585 | void unregister_dock_notifier(struct notifier_block *nb) | |
586 | { | |
db350b08 | 587 | if (!dock_station_count) |
2548c06b PB |
588 | return; |
589 | ||
c8f7a62c LB |
590 | atomic_notifier_chain_unregister(&dock_notifier_list, nb); |
591 | } | |
592 | ||
593 | EXPORT_SYMBOL_GPL(unregister_dock_notifier); | |
594 | ||
595 | /** | |
596 | * register_hotplug_dock_device - register a hotplug function | |
597 | * @handle: the handle of the device | |
1253f7aa | 598 | * @ops: handlers to call after docking |
c8f7a62c LB |
599 | * @context: device specific data |
600 | * | |
601 | * If a driver would like to perform a hotplug operation after a dock | |
602 | * event, they can register an acpi_notifiy_handler to be called by | |
603 | * the dock driver after _DCK is executed. | |
604 | */ | |
605 | int | |
1253f7aa | 606 | register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops, |
c8f7a62c LB |
607 | void *context) |
608 | { | |
609 | struct dock_dependent_device *dd; | |
db350b08 | 610 | struct dock_station *dock_station; |
61b83695 | 611 | int ret = -EINVAL; |
c8f7a62c | 612 | |
db350b08 | 613 | if (!dock_station_count) |
c8f7a62c LB |
614 | return -ENODEV; |
615 | ||
616 | /* | |
617 | * make sure this handle is for a device dependent on the dock, | |
618 | * this would include the dock station itself | |
619 | */ | |
db350b08 | 620 | list_for_each_entry(dock_station, &dock_stations, sibiling) { |
61b83695 SL |
621 | /* |
622 | * An ATA bay can be in a dock and itself can be ejected | |
623 | * seperately, so there are two 'dock stations' which need the | |
624 | * ops | |
625 | */ | |
db350b08 SL |
626 | dd = find_dock_dependent_device(dock_station, handle); |
627 | if (dd) { | |
1253f7aa | 628 | dd->ops = ops; |
db350b08 SL |
629 | dd->context = context; |
630 | dock_add_hotplug_device(dock_station, dd); | |
61b83695 | 631 | ret = 0; |
db350b08 | 632 | } |
c8f7a62c LB |
633 | } |
634 | ||
61b83695 | 635 | return ret; |
c8f7a62c LB |
636 | } |
637 | ||
638 | EXPORT_SYMBOL_GPL(register_hotplug_dock_device); | |
639 | ||
640 | /** | |
641 | * unregister_hotplug_dock_device - remove yourself from the hotplug list | |
642 | * @handle: the acpi handle of the device | |
643 | */ | |
644 | void unregister_hotplug_dock_device(acpi_handle handle) | |
645 | { | |
646 | struct dock_dependent_device *dd; | |
db350b08 | 647 | struct dock_station *dock_station; |
c8f7a62c | 648 | |
db350b08 | 649 | if (!dock_station_count) |
c8f7a62c LB |
650 | return; |
651 | ||
db350b08 SL |
652 | list_for_each_entry(dock_station, &dock_stations, sibiling) { |
653 | dd = find_dock_dependent_device(dock_station, handle); | |
654 | if (dd) | |
655 | dock_del_hotplug_device(dock_station, dd); | |
656 | } | |
c8f7a62c LB |
657 | } |
658 | ||
659 | EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device); | |
660 | ||
c80fdbe8 | 661 | /** |
662 | * handle_eject_request - handle an undock request checking for error conditions | |
663 | * | |
664 | * Check to make sure the dock device is still present, then undock and | |
665 | * hotremove all the devices that may need removing. | |
666 | */ | |
667 | static int handle_eject_request(struct dock_station *ds, u32 event) | |
668 | { | |
c80fdbe8 | 669 | if (dock_in_progress(ds)) |
670 | return -EBUSY; | |
671 | ||
672 | /* | |
673 | * here we need to generate the undock | |
674 | * event prior to actually doing the undock | |
675 | * so that the device struct still exists. | |
afd7301d HM |
676 | * Also, even send the dock event if the |
677 | * device is not present anymore | |
c80fdbe8 | 678 | */ |
679 | dock_event(ds, event, UNDOCK_EVENT); | |
afd7301d | 680 | |
c80fdbe8 | 681 | hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST); |
682 | undock(ds); | |
406f692d | 683 | dock_lock(ds, 0); |
c80fdbe8 | 684 | eject_dock(ds); |
685 | if (dock_present(ds)) { | |
686 | printk(KERN_ERR PREFIX "Unable to undock!\n"); | |
687 | return -EBUSY; | |
688 | } | |
a0cd35fd | 689 | complete_undock(ds); |
c80fdbe8 | 690 | return 0; |
691 | } | |
692 | ||
c8f7a62c LB |
693 | /** |
694 | * dock_notify - act upon an acpi dock notification | |
695 | * @handle: the dock station handle | |
696 | * @event: the acpi event | |
697 | * @data: our driver data struct | |
698 | * | |
699 | * If we are notified to dock, then check to see if the dock is | |
700 | * present and then dock. Notify all drivers of the dock event, | |
c80fdbe8 | 701 | * and then hotplug and devices that may need hotplugging. |
c8f7a62c LB |
702 | */ |
703 | static void dock_notify(acpi_handle handle, u32 event, void *data) | |
704 | { | |
50dd0969 | 705 | struct dock_station *ds = data; |
8b59560a | 706 | struct acpi_device *tmp; |
db350b08 | 707 | int surprise_removal = 0; |
c8f7a62c | 708 | |
db350b08 SL |
709 | /* |
710 | * According to acpi spec 3.0a, if a DEVICE_CHECK notification | |
711 | * is sent and _DCK is present, it is assumed to mean an undock | |
712 | * request. | |
713 | */ | |
714 | if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK) | |
715 | event = ACPI_NOTIFY_EJECT_REQUEST; | |
716 | ||
717 | /* | |
718 | * dock station: BUS_CHECK - docked or surprise removal | |
719 | * DEVICE_CHECK - undocked | |
720 | * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal | |
721 | * | |
722 | * To simplify event handling, dock dependent device handler always | |
723 | * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and | |
724 | * ACPI_NOTIFY_EJECT_REQUEST for removal | |
725 | */ | |
c8f7a62c LB |
726 | switch (event) { |
727 | case ACPI_NOTIFY_BUS_CHECK: | |
db350b08 | 728 | case ACPI_NOTIFY_DEVICE_CHECK: |
8b59560a SL |
729 | if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle, |
730 | &tmp)) { | |
c8f7a62c LB |
731 | begin_dock(ds); |
732 | dock(ds); | |
733 | if (!dock_present(ds)) { | |
734 | printk(KERN_ERR PREFIX "Unable to dock!\n"); | |
8b59560a | 735 | complete_dock(ds); |
c8f7a62c LB |
736 | break; |
737 | } | |
738 | atomic_notifier_call_chain(&dock_notifier_list, | |
739 | event, NULL); | |
740 | hotplug_dock_devices(ds, event); | |
741 | complete_dock(ds); | |
742 | dock_event(ds, event, DOCK_EVENT); | |
406f692d | 743 | dock_lock(ds, 1); |
db350b08 | 744 | break; |
c8f7a62c | 745 | } |
db350b08 SL |
746 | if (dock_present(ds) || dock_in_progress(ds)) |
747 | break; | |
748 | /* This is a surprise removal */ | |
749 | surprise_removal = 1; | |
750 | event = ACPI_NOTIFY_EJECT_REQUEST; | |
751 | /* Fall back */ | |
c8f7a62c | 752 | case ACPI_NOTIFY_EJECT_REQUEST: |
a0cd35fd | 753 | begin_undock(ds); |
f730ae18 SL |
754 | if ((immediate_undock && !(ds->flags & DOCK_IS_ATA)) |
755 | || surprise_removal) | |
a0cd35fd KCA |
756 | handle_eject_request(ds, event); |
757 | else | |
758 | dock_event(ds, event, UNDOCK_EVENT); | |
c8f7a62c LB |
759 | break; |
760 | default: | |
761 | printk(KERN_ERR PREFIX "Unknown dock event %d\n", event); | |
762 | } | |
763 | } | |
764 | ||
19cd847a ZR |
765 | struct dock_data { |
766 | acpi_handle handle; | |
767 | unsigned long event; | |
768 | struct dock_station *ds; | |
769 | }; | |
770 | ||
771 | static void acpi_dock_deferred_cb(void *context) | |
772 | { | |
773 | struct dock_data *data = (struct dock_data *)context; | |
774 | ||
775 | dock_notify(data->handle, data->event, data->ds); | |
776 | kfree(data); | |
777 | } | |
778 | ||
6bd00a61 SL |
779 | static int acpi_dock_notifier_call(struct notifier_block *this, |
780 | unsigned long event, void *data) | |
781 | { | |
782 | struct dock_station *dock_station; | |
783 | acpi_handle handle = (acpi_handle)data; | |
784 | ||
785 | if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK | |
786 | && event != ACPI_NOTIFY_EJECT_REQUEST) | |
787 | return 0; | |
788 | list_for_each_entry(dock_station, &dock_stations, sibiling) { | |
789 | if (dock_station->handle == handle) { | |
19cd847a ZR |
790 | struct dock_data *dock_data; |
791 | ||
792 | dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL); | |
793 | if (!dock_data) | |
794 | return 0; | |
795 | dock_data->handle = handle; | |
796 | dock_data->event = event; | |
797 | dock_data->ds = dock_station; | |
798 | acpi_os_hotplug_execute(acpi_dock_deferred_cb, | |
799 | dock_data); | |
6bd00a61 SL |
800 | return 0 ; |
801 | } | |
802 | } | |
803 | return 0; | |
804 | } | |
805 | ||
806 | static struct notifier_block dock_acpi_notifier = { | |
807 | .notifier_call = acpi_dock_notifier_call, | |
808 | }; | |
809 | ||
c8f7a62c LB |
810 | /** |
811 | * find_dock_devices - find devices on the dock station | |
812 | * @handle: the handle of the device we are examining | |
813 | * @lvl: unused | |
814 | * @context: the dock station private data | |
815 | * @rv: unused | |
816 | * | |
817 | * This function is called by acpi_walk_namespace. It will | |
818 | * check to see if an object has an _EJD method. If it does, then it | |
819 | * will see if it is dependent on the dock station. | |
820 | */ | |
821 | static acpi_status | |
822 | find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv) | |
823 | { | |
824 | acpi_status status; | |
fe9a2f77 | 825 | acpi_handle tmp, parent; |
50dd0969 | 826 | struct dock_station *ds = context; |
c8f7a62c LB |
827 | struct dock_dependent_device *dd; |
828 | ||
829 | status = acpi_bus_get_ejd(handle, &tmp); | |
fe9a2f77 KCA |
830 | if (ACPI_FAILURE(status)) { |
831 | /* try the parent device as well */ | |
832 | status = acpi_get_parent(handle, &parent); | |
833 | if (ACPI_FAILURE(status)) | |
834 | goto fdd_out; | |
835 | /* see if parent is dependent on dock */ | |
836 | status = acpi_bus_get_ejd(parent, &tmp); | |
837 | if (ACPI_FAILURE(status)) | |
838 | goto fdd_out; | |
839 | } | |
c8f7a62c LB |
840 | |
841 | if (tmp == ds->handle) { | |
842 | dd = alloc_dock_dependent_device(handle); | |
843 | if (dd) | |
844 | add_dock_dependent_device(ds, dd); | |
845 | } | |
fe9a2f77 | 846 | fdd_out: |
c8f7a62c LB |
847 | return AE_OK; |
848 | } | |
849 | ||
c80fdbe8 | 850 | /* |
851 | * show_docked - read method for "docked" file in sysfs | |
852 | */ | |
853 | static ssize_t show_docked(struct device *dev, | |
854 | struct device_attribute *attr, char *buf) | |
855 | { | |
fc5a9f88 HM |
856 | struct acpi_device *tmp; |
857 | ||
db350b08 SL |
858 | struct dock_station *dock_station = *((struct dock_station **) |
859 | dev->platform_data); | |
c80fdbe8 | 860 | |
fc5a9f88 HM |
861 | if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp))) |
862 | return snprintf(buf, PAGE_SIZE, "1\n"); | |
863 | return snprintf(buf, PAGE_SIZE, "0\n"); | |
c80fdbe8 | 864 | } |
e5685b9d | 865 | static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL); |
c80fdbe8 | 866 | |
a0cd35fd KCA |
867 | /* |
868 | * show_flags - read method for flags file in sysfs | |
869 | */ | |
870 | static ssize_t show_flags(struct device *dev, | |
871 | struct device_attribute *attr, char *buf) | |
872 | { | |
db350b08 SL |
873 | struct dock_station *dock_station = *((struct dock_station **) |
874 | dev->platform_data); | |
a0cd35fd KCA |
875 | return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags); |
876 | ||
877 | } | |
e5685b9d | 878 | static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL); |
a0cd35fd | 879 | |
c80fdbe8 | 880 | /* |
881 | * write_undock - write method for "undock" file in sysfs | |
882 | */ | |
883 | static ssize_t write_undock(struct device *dev, struct device_attribute *attr, | |
884 | const char *buf, size_t count) | |
885 | { | |
886 | int ret; | |
db350b08 SL |
887 | struct dock_station *dock_station = *((struct dock_station **) |
888 | dev->platform_data); | |
c80fdbe8 | 889 | |
890 | if (!count) | |
891 | return -EINVAL; | |
892 | ||
9171f834 | 893 | begin_undock(dock_station); |
c80fdbe8 | 894 | ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST); |
895 | return ret ? ret: count; | |
896 | } | |
e5685b9d | 897 | static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock); |
c80fdbe8 | 898 | |
ac122bb6 IVE |
899 | /* |
900 | * show_dock_uid - read method for "uid" file in sysfs | |
901 | */ | |
902 | static ssize_t show_dock_uid(struct device *dev, | |
903 | struct device_attribute *attr, char *buf) | |
904 | { | |
27663c58 | 905 | unsigned long long lbuf; |
db350b08 SL |
906 | struct dock_station *dock_station = *((struct dock_station **) |
907 | dev->platform_data); | |
38ff4ffc KCA |
908 | acpi_status status = acpi_evaluate_integer(dock_station->handle, |
909 | "_UID", NULL, &lbuf); | |
910 | if (ACPI_FAILURE(status)) | |
ac122bb6 | 911 | return 0; |
38ff4ffc | 912 | |
27663c58 | 913 | return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf); |
ac122bb6 | 914 | } |
e5685b9d | 915 | static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL); |
ac122bb6 | 916 | |
8652b00f SL |
917 | static ssize_t show_dock_type(struct device *dev, |
918 | struct device_attribute *attr, char *buf) | |
919 | { | |
920 | struct dock_station *dock_station = *((struct dock_station **) | |
921 | dev->platform_data); | |
922 | char *type; | |
923 | ||
924 | if (dock_station->flags & DOCK_IS_DOCK) | |
925 | type = "dock_station"; | |
926 | else if (dock_station->flags & DOCK_IS_ATA) | |
927 | type = "ata_bay"; | |
928 | else if (dock_station->flags & DOCK_IS_BAT) | |
929 | type = "battery_bay"; | |
930 | else | |
931 | type = "unknown"; | |
932 | ||
933 | return snprintf(buf, PAGE_SIZE, "%s\n", type); | |
934 | } | |
935 | static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL); | |
936 | ||
c8f7a62c LB |
937 | /** |
938 | * dock_add - add a new dock station | |
939 | * @handle: the dock station handle | |
940 | * | |
941 | * allocated and initialize a new dock station device. Find all devices | |
942 | * that are on the dock station, and register for dock event notifications. | |
943 | */ | |
944 | static int dock_add(acpi_handle handle) | |
945 | { | |
946 | int ret; | |
c8f7a62c | 947 | struct dock_dependent_device *dd; |
db350b08 SL |
948 | struct dock_station *dock_station; |
949 | struct platform_device *dock_device; | |
c8f7a62c LB |
950 | |
951 | /* allocate & initialize the dock_station private data */ | |
952 | dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL); | |
953 | if (!dock_station) | |
954 | return -ENOMEM; | |
955 | dock_station->handle = handle; | |
956 | dock_station->last_dock_time = jiffies - HZ; | |
957 | INIT_LIST_HEAD(&dock_station->dependent_devices); | |
958 | INIT_LIST_HEAD(&dock_station->hotplug_devices); | |
db350b08 | 959 | INIT_LIST_HEAD(&dock_station->sibiling); |
c8f7a62c | 960 | spin_lock_init(&dock_station->dd_lock); |
8b0dc866 | 961 | mutex_init(&dock_station->hp_lock); |
07a18684 | 962 | ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list); |
c8f7a62c | 963 | |
671adbec | 964 | /* initialize platform device stuff */ |
db350b08 SL |
965 | dock_station->dock_device = |
966 | platform_device_register_simple(dock_device_name, | |
967 | dock_station_count, NULL, 0); | |
968 | dock_device = dock_station->dock_device; | |
0f6f2804 | 969 | if (IS_ERR(dock_device)) { |
671adbec | 970 | kfree(dock_station); |
22fe4c21 | 971 | dock_station = NULL; |
0f6f2804 | 972 | return PTR_ERR(dock_device); |
671adbec | 973 | } |
db350b08 SL |
974 | platform_device_add_data(dock_device, &dock_station, |
975 | sizeof(struct dock_station *)); | |
a0cd35fd | 976 | |
9ef2a9a9 | 977 | /* we want the dock device to send uevents */ |
f67f129e | 978 | dev_set_uevent_suppress(&dock_device->dev, 0); |
9ef2a9a9 | 979 | |
db350b08 SL |
980 | if (is_dock(handle)) |
981 | dock_station->flags |= DOCK_IS_DOCK; | |
982 | if (is_ata(handle)) | |
983 | dock_station->flags |= DOCK_IS_ATA; | |
984 | if (is_battery(handle)) | |
985 | dock_station->flags |= DOCK_IS_BAT; | |
986 | ||
0f6f2804 | 987 | ret = device_create_file(&dock_device->dev, &dev_attr_docked); |
c80fdbe8 | 988 | if (ret) { |
4d939155 | 989 | printk(KERN_ERR "Error %d adding sysfs file\n", ret); |
0f6f2804 | 990 | platform_device_unregister(dock_device); |
c80fdbe8 | 991 | kfree(dock_station); |
22fe4c21 | 992 | dock_station = NULL; |
c80fdbe8 | 993 | return ret; |
994 | } | |
0f6f2804 | 995 | ret = device_create_file(&dock_device->dev, &dev_attr_undock); |
c80fdbe8 | 996 | if (ret) { |
4d939155 | 997 | printk(KERN_ERR "Error %d adding sysfs file\n", ret); |
0f6f2804 KCA |
998 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
999 | platform_device_unregister(dock_device); | |
c80fdbe8 | 1000 | kfree(dock_station); |
22fe4c21 | 1001 | dock_station = NULL; |
c80fdbe8 | 1002 | return ret; |
1003 | } | |
0f6f2804 | 1004 | ret = device_create_file(&dock_device->dev, &dev_attr_uid); |
ac122bb6 | 1005 | if (ret) { |
4d939155 | 1006 | printk(KERN_ERR "Error %d adding sysfs file\n", ret); |
0f6f2804 KCA |
1007 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
1008 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
1009 | platform_device_unregister(dock_device); | |
ac122bb6 | 1010 | kfree(dock_station); |
22fe4c21 | 1011 | dock_station = NULL; |
ac122bb6 IVE |
1012 | return ret; |
1013 | } | |
a0cd35fd KCA |
1014 | ret = device_create_file(&dock_device->dev, &dev_attr_flags); |
1015 | if (ret) { | |
4d939155 | 1016 | printk(KERN_ERR "Error %d adding sysfs file\n", ret); |
a0cd35fd KCA |
1017 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
1018 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
1019 | device_remove_file(&dock_device->dev, &dev_attr_uid); | |
1020 | platform_device_unregister(dock_device); | |
1021 | kfree(dock_station); | |
1022 | dock_station = NULL; | |
1023 | return ret; | |
1024 | } | |
8652b00f SL |
1025 | ret = device_create_file(&dock_device->dev, &dev_attr_type); |
1026 | if (ret) | |
1027 | printk(KERN_ERR"Error %d adding sysfs file\n", ret); | |
671adbec | 1028 | |
c8f7a62c LB |
1029 | /* Find dependent devices */ |
1030 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
1031 | ACPI_UINT32_MAX, find_dock_devices, dock_station, | |
1032 | NULL); | |
1033 | ||
1034 | /* add the dock station as a device dependent on itself */ | |
1035 | dd = alloc_dock_dependent_device(handle); | |
1036 | if (!dd) { | |
1037 | kfree(dock_station); | |
22fe4c21 | 1038 | dock_station = NULL; |
671adbec KCA |
1039 | ret = -ENOMEM; |
1040 | goto dock_add_err_unregister; | |
c8f7a62c LB |
1041 | } |
1042 | add_dock_dependent_device(dock_station, dd); | |
1043 | ||
db350b08 SL |
1044 | dock_station_count++; |
1045 | list_add(&dock_station->sibiling, &dock_stations); | |
c8f7a62c LB |
1046 | return 0; |
1047 | ||
671adbec | 1048 | dock_add_err_unregister: |
8652b00f | 1049 | device_remove_file(&dock_device->dev, &dev_attr_type); |
0f6f2804 KCA |
1050 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
1051 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
1052 | device_remove_file(&dock_device->dev, &dev_attr_uid); | |
a0cd35fd | 1053 | device_remove_file(&dock_device->dev, &dev_attr_flags); |
0f6f2804 | 1054 | platform_device_unregister(dock_device); |
671adbec | 1055 | kfree(dock_station); |
22fe4c21 | 1056 | dock_station = NULL; |
c8f7a62c LB |
1057 | return ret; |
1058 | } | |
1059 | ||
1060 | /** | |
1061 | * dock_remove - free up resources related to the dock station | |
1062 | */ | |
db350b08 | 1063 | static int dock_remove(struct dock_station *dock_station) |
c8f7a62c LB |
1064 | { |
1065 | struct dock_dependent_device *dd, *tmp; | |
db350b08 | 1066 | struct platform_device *dock_device = dock_station->dock_device; |
c8f7a62c | 1067 | |
db350b08 | 1068 | if (!dock_station_count) |
c8f7a62c LB |
1069 | return 0; |
1070 | ||
1071 | /* remove dependent devices */ | |
1072 | list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices, | |
1073 | list) | |
1074 | kfree(dd); | |
1075 | ||
671adbec | 1076 | /* cleanup sysfs */ |
8652b00f | 1077 | device_remove_file(&dock_device->dev, &dev_attr_type); |
0f6f2804 KCA |
1078 | device_remove_file(&dock_device->dev, &dev_attr_docked); |
1079 | device_remove_file(&dock_device->dev, &dev_attr_undock); | |
1080 | device_remove_file(&dock_device->dev, &dev_attr_uid); | |
a0cd35fd | 1081 | device_remove_file(&dock_device->dev, &dev_attr_flags); |
0f6f2804 | 1082 | platform_device_unregister(dock_device); |
671adbec | 1083 | |
c8f7a62c LB |
1084 | /* free dock station memory */ |
1085 | kfree(dock_station); | |
22fe4c21 | 1086 | dock_station = NULL; |
c8f7a62c LB |
1087 | return 0; |
1088 | } | |
1089 | ||
1090 | /** | |
1091 | * find_dock - look for a dock station | |
1092 | * @handle: acpi handle of a device | |
1093 | * @lvl: unused | |
1094 | * @context: counter of dock stations found | |
1095 | * @rv: unused | |
1096 | * | |
1097 | * This is called by acpi_walk_namespace to look for dock stations. | |
1098 | */ | |
1099 | static acpi_status | |
1100 | find_dock(acpi_handle handle, u32 lvl, void *context, void **rv) | |
1101 | { | |
c8f7a62c LB |
1102 | acpi_status status = AE_OK; |
1103 | ||
1104 | if (is_dock(handle)) { | |
1105 | if (dock_add(handle) >= 0) { | |
c8f7a62c LB |
1106 | status = AE_CTRL_TERMINATE; |
1107 | } | |
1108 | } | |
1109 | return status; | |
1110 | } | |
1111 | ||
db350b08 SL |
1112 | static acpi_status |
1113 | find_bay(acpi_handle handle, u32 lvl, void *context, void **rv) | |
c8f7a62c | 1114 | { |
61b83695 SL |
1115 | /* If bay is a dock, it's already handled */ |
1116 | if (is_ejectable_bay(handle) && !is_dock(handle)) | |
db350b08 SL |
1117 | dock_add(handle); |
1118 | return AE_OK; | |
1119 | } | |
c8f7a62c | 1120 | |
db350b08 SL |
1121 | static int __init dock_init(void) |
1122 | { | |
816c2eda LB |
1123 | if (acpi_disabled) |
1124 | return 0; | |
1125 | ||
c8f7a62c LB |
1126 | /* look for a dock station */ |
1127 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
db350b08 | 1128 | ACPI_UINT32_MAX, find_dock, NULL, NULL); |
c8f7a62c | 1129 | |
db350b08 SL |
1130 | /* look for bay */ |
1131 | acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, | |
1132 | ACPI_UINT32_MAX, find_bay, NULL, NULL); | |
1133 | if (!dock_station_count) { | |
1134 | printk(KERN_INFO PREFIX "No dock devices found.\n"); | |
1135 | return 0; | |
1136 | } | |
c8f7a62c | 1137 | |
6bd00a61 | 1138 | register_acpi_bus_notifier(&dock_acpi_notifier); |
db350b08 SL |
1139 | printk(KERN_INFO PREFIX "%s: %d docks/bays found\n", |
1140 | ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count); | |
c8f7a62c LB |
1141 | return 0; |
1142 | } | |
1143 | ||
1144 | static void __exit dock_exit(void) | |
1145 | { | |
db350b08 | 1146 | struct dock_station *dock_station; |
f2407298 | 1147 | struct dock_station *tmp; |
db350b08 | 1148 | |
6bd00a61 | 1149 | unregister_acpi_bus_notifier(&dock_acpi_notifier); |
f2407298 | 1150 | list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibiling) |
db350b08 | 1151 | dock_remove(dock_station); |
c8f7a62c LB |
1152 | } |
1153 | ||
db350b08 SL |
1154 | /* |
1155 | * Must be called before drivers of devices in dock, otherwise we can't know | |
1156 | * which devices are in a dock | |
1157 | */ | |
1158 | subsys_initcall(dock_init); | |
c8f7a62c | 1159 | module_exit(dock_exit); |