blob: ac7dfefcb50b44a4938a5aa13fd4fdb10b59229e [file] [log] [blame]
Len Brownc8f7a622006-07-09 17:22:28 -04001/*
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>
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080030#include <linux/platform_device.h>
Al Viro914e2632006-10-18 13:55:46 -040031#include <linux/jiffies.h>
Randy Dunlap62a6d7f2007-03-26 21:38:49 -080032#include <linux/stddef.h>
Len Brownc8f7a622006-07-09 17:22:28 -040033#include <acpi/acpi_bus.h>
34#include <acpi/acpi_drivers.h>
35
Len Brown7cda93e2007-02-12 23:50:02 -050036#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a622006-07-09 17:22:28 -040037
Len Brownf52fd662007-02-12 22:42:12 -050038ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040039MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050040MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -040041MODULE_LICENSE("GPL");
42
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070043static int immediate_undock = 1;
44module_param(immediate_undock, bool, 0644);
45MODULE_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
Len Brownc8f7a622006-07-09 17:22:28 -040050static struct atomic_notifier_head dock_notifier_list;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080051static char dock_device_name[] = "dock";
Len Brownc8f7a622006-07-09 17:22:28 -040052
Frank Seidela340af12007-12-07 13:20:42 +010053static const struct acpi_device_id dock_device_ids[] = {
54 {"LNXDOCK", 0},
55 {"", 0},
56};
57MODULE_DEVICE_TABLE(acpi, dock_device_ids);
58
Len Brownc8f7a622006-07-09 17:22:28 -040059struct dock_station {
60 acpi_handle handle;
61 unsigned long last_dock_time;
62 u32 flags;
63 spinlock_t dd_lock;
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -080064 struct mutex hp_lock;
Len Brownc8f7a622006-07-09 17:22:28 -040065 struct list_head dependent_devices;
66 struct list_head hotplug_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080067
68 struct list_head sibiling;
69 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040070};
Shaohua Lidb350b02008-08-28 10:03:58 +080071static LIST_HEAD(dock_stations);
72static int dock_station_count;
Len Brownc8f7a622006-07-09 17:22:28 -040073
74struct dock_dependent_device {
75 struct list_head list;
76 struct list_head hotplug_list;
77 acpi_handle handle;
Shaohua Li1253f7a2008-08-28 10:06:16 +080078 struct acpi_dock_ops *ops;
Len Brownc8f7a622006-07-09 17:22:28 -040079 void *context;
80};
81
82#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070083#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080084#define DOCK_IS_DOCK 0x00000010
85#define DOCK_IS_ATA 0x00000020
86#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070087#define DOCK_EVENT 3
88#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040089
Len Brownc8f7a622006-07-09 17:22:28 -040090/*****************************************************************************
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 */
100static struct dock_dependent_device *
101alloc_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 */
121static void
122add_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 */
137static void
138dock_add_hotplug_device(struct dock_station *ds,
139 struct dock_dependent_device *dd)
140{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800141 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400142 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800143 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400144}
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 */
153static void
154dock_del_hotplug_device(struct dock_station *ds,
155 struct dock_dependent_device *dd)
156{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800157 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400158 list_del(&dd->hotplug_list);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800159 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400160}
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 */
170static struct dock_dependent_device *
171find_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 */
196static 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
Shaohua Lidb350b02008-08-28 10:03:58 +0800207static 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
218static 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
231static int is_battery(acpi_handle handle)
232{
233 struct acpi_device_info *info;
234 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
235 int ret = 1;
236
237 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &buffer)))
238 return 0;
239 info = buffer.pointer;
240 if (!(info->valid & ACPI_VALID_HID))
241 ret = 0;
242 else
243 ret = !strcmp("PNP0C0A", info->hardware_id.value);
244
245 kfree(buffer.pointer);
246 return ret;
247}
248
249static int is_ejectable_bay(acpi_handle handle)
250{
251 acpi_handle phandle;
252 if (!is_ejectable(handle))
253 return 0;
254 if (is_battery(handle) || is_ata(handle))
255 return 1;
256 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
257 return 1;
258 return 0;
259}
260
Len Brownc8f7a622006-07-09 17:22:28 -0400261/**
262 * is_dock_device - see if a device is on a dock station
263 * @handle: acpi handle of the device
264 *
265 * If this device is either the dock station itself,
266 * or is a device dependent on the dock station, then it
267 * is a dock device
268 */
269int is_dock_device(acpi_handle handle)
270{
Shaohua Lidb350b02008-08-28 10:03:58 +0800271 struct dock_station *dock_station;
272
273 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400274 return 0;
275
Shaohua Lidb350b02008-08-28 10:03:58 +0800276 if (is_dock(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400277 return 1;
Shaohua Lidb350b02008-08-28 10:03:58 +0800278 list_for_each_entry(dock_station, &dock_stations, sibiling) {
279 if (find_dock_dependent_device(dock_station, handle))
280 return 1;
281 }
Len Brownc8f7a622006-07-09 17:22:28 -0400282
283 return 0;
284}
285
286EXPORT_SYMBOL_GPL(is_dock_device);
287
288/**
289 * dock_present - see if the dock station is present.
290 * @ds: the dock station
291 *
292 * execute the _STA method. note that present does not
293 * imply that we are docked.
294 */
295static int dock_present(struct dock_station *ds)
296{
297 unsigned long sta;
298 acpi_status status;
299
300 if (ds) {
301 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
302 if (ACPI_SUCCESS(status) && sta)
303 return 1;
304 }
305 return 0;
306}
307
308
309
310/**
311 * dock_create_acpi_device - add new devices to acpi
312 * @handle - handle of the device to add
313 *
314 * This function will create a new acpi_device for the given
315 * handle if one does not exist already. This should cause
316 * acpi to scan for drivers for the given devices, and call
317 * matching driver's add routine.
318 *
319 * Returns a pointer to the acpi_device corresponding to the handle.
320 */
321static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
322{
323 struct acpi_device *device = NULL;
324 struct acpi_device *parent_device;
325 acpi_handle parent;
326 int ret;
327
328 if (acpi_bus_get_device(handle, &device)) {
329 /*
330 * no device created for this object,
331 * so we should create one.
332 */
333 acpi_get_parent(handle, &parent);
334 if (acpi_bus_get_device(parent, &parent_device))
335 parent_device = NULL;
336
337 ret = acpi_bus_add(&device, parent_device, handle,
338 ACPI_BUS_TYPE_DEVICE);
339 if (ret) {
340 pr_debug("error adding bus, %x\n",
341 -ret);
342 return NULL;
343 }
344 }
345 return device;
346}
347
348/**
349 * dock_remove_acpi_device - remove the acpi_device struct from acpi
350 * @handle - the handle of the device to remove
351 *
352 * Tell acpi to remove the acpi_device. This should cause any loaded
353 * driver to have it's remove routine called.
354 */
355static void dock_remove_acpi_device(acpi_handle handle)
356{
357 struct acpi_device *device;
358 int ret;
359
360 if (!acpi_bus_get_device(handle, &device)) {
361 ret = acpi_bus_trim(device, 1);
362 if (ret)
363 pr_debug("error removing bus, %x\n", -ret);
364 }
365}
366
367
368/**
369 * hotplug_dock_devices - insert or remove devices on the dock station
370 * @ds: the dock station
371 * @event: either bus check or eject request
372 *
373 * Some devices on the dock station need to have drivers called
374 * to perform hotplug operations after a dock event has occurred.
375 * Traverse the list of dock devices that have registered a
376 * hotplug handler, and call the handler.
377 */
378static void hotplug_dock_devices(struct dock_station *ds, u32 event)
379{
380 struct dock_dependent_device *dd;
381
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800382 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400383
384 /*
385 * First call driver specific hotplug functions
386 */
387 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800388 if (dd->ops && dd->ops->handler)
389 dd->ops->handler(dd->handle, event, dd->context);
Len Brownc8f7a622006-07-09 17:22:28 -0400390 }
391
392 /*
393 * Now make sure that an acpi_device is created for each
394 * dependent device, or removed if this is an eject request.
395 * This will cause acpi_drivers to be stopped/started if they
396 * exist
397 */
398 list_for_each_entry(dd, &ds->dependent_devices, list) {
399 if (event == ACPI_NOTIFY_EJECT_REQUEST)
400 dock_remove_acpi_device(dd->handle);
401 else
402 dock_create_acpi_device(dd->handle);
403 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800404 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400405}
406
407static void dock_event(struct dock_station *ds, u32 event, int num)
408{
Shaohua Lidb350b02008-08-28 10:03:58 +0800409 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700410 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700411 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800412 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700413
414 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700415 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700416 else
Holger Macht66b56822007-08-10 13:10:32 -0700417 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700418
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700419 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800420 * Indicate that the status of the dock station has
421 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700422 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800423 if (num == DOCK_EVENT)
424 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
425
426 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
427 if (dd->ops && dd->ops->uevent)
428 dd->ops->uevent(dd->handle, event, dd->context);
429 if (num != DOCK_EVENT)
430 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400431}
432
433/**
434 * eject_dock - respond to a dock eject request
435 * @ds: the dock station
436 *
437 * This is called after _DCK is called, to execute the dock station's
438 * _EJ0 method.
439 */
440static void eject_dock(struct dock_station *ds)
441{
442 struct acpi_object_list arg_list;
443 union acpi_object arg;
444 acpi_status status;
445 acpi_handle tmp;
446
447 /* all dock devices should have _EJ0, but check anyway */
448 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
449 if (ACPI_FAILURE(status)) {
450 pr_debug("No _EJ0 support for dock device\n");
451 return;
452 }
453
454 arg_list.count = 1;
455 arg_list.pointer = &arg;
456 arg.type = ACPI_TYPE_INTEGER;
457 arg.integer.value = 1;
458
459 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
460 &arg_list, NULL)))
461 pr_debug("Failed to evaluate _EJ0!\n");
462}
463
464/**
465 * handle_dock - handle a dock event
466 * @ds: the dock station
467 * @dock: to dock, or undock - that is the question
468 *
469 * Execute the _DCK method in response to an acpi event
470 */
471static void handle_dock(struct dock_station *ds, int dock)
472{
473 acpi_status status;
474 struct acpi_object_list arg_list;
475 union acpi_object arg;
476 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
477 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400478
479 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
Len Brownc8f7a622006-07-09 17:22:28 -0400480
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400481 printk(KERN_INFO PREFIX "%s - %s\n",
482 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400483
484 /* _DCK method has one argument */
485 arg_list.count = 1;
486 arg_list.pointer = &arg;
487 arg.type = ACPI_TYPE_INTEGER;
488 arg.integer.value = dock;
489 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800490 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400491 printk(KERN_ERR PREFIX "%s - failed to execute _DCK\n",
492 (char *)name_buffer.pointer);
Len Brownc8f7a622006-07-09 17:22:28 -0400493 kfree(buffer.pointer);
494 kfree(name_buffer.pointer);
495}
496
497static inline void dock(struct dock_station *ds)
498{
499 handle_dock(ds, 1);
500}
501
502static inline void undock(struct dock_station *ds)
503{
504 handle_dock(ds, 0);
505}
506
507static inline void begin_dock(struct dock_station *ds)
508{
509 ds->flags |= DOCK_DOCKING;
510}
511
512static inline void complete_dock(struct dock_station *ds)
513{
514 ds->flags &= ~(DOCK_DOCKING);
515 ds->last_dock_time = jiffies;
516}
517
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700518static inline void begin_undock(struct dock_station *ds)
519{
520 ds->flags |= DOCK_UNDOCKING;
521}
522
523static inline void complete_undock(struct dock_station *ds)
524{
525 ds->flags &= ~(DOCK_UNDOCKING);
526}
527
Shaohua Li406f6922008-08-28 10:03:26 +0800528static void dock_lock(struct dock_station *ds, int lock)
529{
530 struct acpi_object_list arg_list;
531 union acpi_object arg;
532 acpi_status status;
533
534 arg_list.count = 1;
535 arg_list.pointer = &arg;
536 arg.type = ACPI_TYPE_INTEGER;
537 arg.integer.value = !!lock;
538 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
539 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
540 if (lock)
541 printk(KERN_WARNING PREFIX "Locking device failed\n");
542 else
543 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
544 }
545}
546
Len Brownc8f7a622006-07-09 17:22:28 -0400547/**
548 * dock_in_progress - see if we are in the middle of handling a dock event
549 * @ds: the dock station
550 *
551 * Sometimes while docking, false dock events can be sent to the driver
552 * because good connections aren't made or some other reason. Ignore these
553 * if we are in the middle of doing something.
554 */
555static int dock_in_progress(struct dock_station *ds)
556{
557 if ((ds->flags & DOCK_DOCKING) ||
558 time_before(jiffies, (ds->last_dock_time + HZ)))
559 return 1;
560 return 0;
561}
562
563/**
564 * register_dock_notifier - add yourself to the dock notifier list
565 * @nb: the callers notifier block
566 *
567 * If a driver wishes to be notified about dock events, they can
568 * use this function to put a notifier block on the dock notifier list.
569 * this notifier call chain will be called after a dock event, but
570 * before hotplugging any new devices.
571 */
572int register_dock_notifier(struct notifier_block *nb)
573{
Shaohua Lidb350b02008-08-28 10:03:58 +0800574 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800575 return -ENODEV;
576
Len Brownc8f7a622006-07-09 17:22:28 -0400577 return atomic_notifier_chain_register(&dock_notifier_list, nb);
578}
579
580EXPORT_SYMBOL_GPL(register_dock_notifier);
581
582/**
583 * unregister_dock_notifier - remove yourself from the dock notifier list
584 * @nb: the callers notifier block
585 */
586void unregister_dock_notifier(struct notifier_block *nb)
587{
Shaohua Lidb350b02008-08-28 10:03:58 +0800588 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800589 return;
590
Len Brownc8f7a622006-07-09 17:22:28 -0400591 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
592}
593
594EXPORT_SYMBOL_GPL(unregister_dock_notifier);
595
596/**
597 * register_hotplug_dock_device - register a hotplug function
598 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800599 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400600 * @context: device specific data
601 *
602 * If a driver would like to perform a hotplug operation after a dock
603 * event, they can register an acpi_notifiy_handler to be called by
604 * the dock driver after _DCK is executed.
605 */
606int
Shaohua Li1253f7a2008-08-28 10:06:16 +0800607register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
Len Brownc8f7a622006-07-09 17:22:28 -0400608 void *context)
609{
610 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800611 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400612
Shaohua Lidb350b02008-08-28 10:03:58 +0800613 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400614 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 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800620 list_for_each_entry(dock_station, &dock_stations, sibiling) {
621 dd = find_dock_dependent_device(dock_station, handle);
622 if (dd) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800623 dd->ops = ops;
Shaohua Lidb350b02008-08-28 10:03:58 +0800624 dd->context = context;
625 dock_add_hotplug_device(dock_station, dd);
626 return 0;
627 }
Len Brownc8f7a622006-07-09 17:22:28 -0400628 }
629
630 return -EINVAL;
631}
632
633EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
634
635/**
636 * unregister_hotplug_dock_device - remove yourself from the hotplug list
637 * @handle: the acpi handle of the device
638 */
639void unregister_hotplug_dock_device(acpi_handle handle)
640{
641 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800642 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400643
Shaohua Lidb350b02008-08-28 10:03:58 +0800644 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400645 return;
646
Shaohua Lidb350b02008-08-28 10:03:58 +0800647 list_for_each_entry(dock_station, &dock_stations, sibiling) {
648 dd = find_dock_dependent_device(dock_station, handle);
649 if (dd)
650 dock_del_hotplug_device(dock_station, dd);
651 }
Len Brownc8f7a622006-07-09 17:22:28 -0400652}
653
654EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
655
656/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800657 * handle_eject_request - handle an undock request checking for error conditions
658 *
659 * Check to make sure the dock device is still present, then undock and
660 * hotremove all the devices that may need removing.
661 */
662static int handle_eject_request(struct dock_station *ds, u32 event)
663{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800664 if (dock_in_progress(ds))
665 return -EBUSY;
666
667 /*
668 * here we need to generate the undock
669 * event prior to actually doing the undock
670 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200671 * Also, even send the dock event if the
672 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800673 */
674 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200675
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800676 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
677 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800678 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800679 eject_dock(ds);
680 if (dock_present(ds)) {
681 printk(KERN_ERR PREFIX "Unable to undock!\n");
682 return -EBUSY;
683 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700684 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800685 return 0;
686}
687
688/**
Len Brownc8f7a622006-07-09 17:22:28 -0400689 * dock_notify - act upon an acpi dock notification
690 * @handle: the dock station handle
691 * @event: the acpi event
692 * @data: our driver data struct
693 *
694 * If we are notified to dock, then check to see if the dock is
695 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800696 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400697 */
698static void dock_notify(acpi_handle handle, u32 event, void *data)
699{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200700 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800701 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800702 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400703
Shaohua Lidb350b02008-08-28 10:03:58 +0800704 /*
705 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
706 * is sent and _DCK is present, it is assumed to mean an undock
707 * request.
708 */
709 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
710 event = ACPI_NOTIFY_EJECT_REQUEST;
711
712 /*
713 * dock station: BUS_CHECK - docked or surprise removal
714 * DEVICE_CHECK - undocked
715 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
716 *
717 * To simplify event handling, dock dependent device handler always
718 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
719 * ACPI_NOTIFY_EJECT_REQUEST for removal
720 */
Len Brownc8f7a622006-07-09 17:22:28 -0400721 switch (event) {
722 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800723 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800724 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
725 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400726 begin_dock(ds);
727 dock(ds);
728 if (!dock_present(ds)) {
729 printk(KERN_ERR PREFIX "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800730 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400731 break;
732 }
733 atomic_notifier_call_chain(&dock_notifier_list,
734 event, NULL);
735 hotplug_dock_devices(ds, event);
736 complete_dock(ds);
737 dock_event(ds, event, DOCK_EVENT);
Shaohua Li406f6922008-08-28 10:03:26 +0800738 dock_lock(ds, 1);
Shaohua Lidb350b02008-08-28 10:03:58 +0800739 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400740 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800741 if (dock_present(ds) || dock_in_progress(ds))
742 break;
743 /* This is a surprise removal */
744 surprise_removal = 1;
745 event = ACPI_NOTIFY_EJECT_REQUEST;
746 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400747 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700748 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800749 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
750 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700751 handle_eject_request(ds, event);
752 else
753 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400754 break;
755 default:
756 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
757 }
758}
759
Zhang Rui19cd8472008-08-28 10:05:06 +0800760struct dock_data {
761 acpi_handle handle;
762 unsigned long event;
763 struct dock_station *ds;
764};
765
766static void acpi_dock_deferred_cb(void *context)
767{
768 struct dock_data *data = (struct dock_data *)context;
769
770 dock_notify(data->handle, data->event, data->ds);
771 kfree(data);
772}
773
Shaohua Li6bd00a62008-08-28 10:04:29 +0800774static int acpi_dock_notifier_call(struct notifier_block *this,
775 unsigned long event, void *data)
776{
777 struct dock_station *dock_station;
778 acpi_handle handle = (acpi_handle)data;
779
780 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
781 && event != ACPI_NOTIFY_EJECT_REQUEST)
782 return 0;
783 list_for_each_entry(dock_station, &dock_stations, sibiling) {
784 if (dock_station->handle == handle) {
Zhang Rui19cd8472008-08-28 10:05:06 +0800785 struct dock_data *dock_data;
786
787 dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
788 if (!dock_data)
789 return 0;
790 dock_data->handle = handle;
791 dock_data->event = event;
792 dock_data->ds = dock_station;
793 acpi_os_hotplug_execute(acpi_dock_deferred_cb,
794 dock_data);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800795 return 0 ;
796 }
797 }
798 return 0;
799}
800
801static struct notifier_block dock_acpi_notifier = {
802 .notifier_call = acpi_dock_notifier_call,
803};
804
Len Brownc8f7a622006-07-09 17:22:28 -0400805/**
806 * find_dock_devices - find devices on the dock station
807 * @handle: the handle of the device we are examining
808 * @lvl: unused
809 * @context: the dock station private data
810 * @rv: unused
811 *
812 * This function is called by acpi_walk_namespace. It will
813 * check to see if an object has an _EJD method. If it does, then it
814 * will see if it is dependent on the dock station.
815 */
816static acpi_status
817find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
818{
819 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500820 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200821 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400822 struct dock_dependent_device *dd;
823
824 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500825 if (ACPI_FAILURE(status)) {
826 /* try the parent device as well */
827 status = acpi_get_parent(handle, &parent);
828 if (ACPI_FAILURE(status))
829 goto fdd_out;
830 /* see if parent is dependent on dock */
831 status = acpi_bus_get_ejd(parent, &tmp);
832 if (ACPI_FAILURE(status))
833 goto fdd_out;
834 }
Len Brownc8f7a622006-07-09 17:22:28 -0400835
836 if (tmp == ds->handle) {
837 dd = alloc_dock_dependent_device(handle);
838 if (dd)
839 add_dock_dependent_device(ds, dd);
840 }
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500841fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400842 return AE_OK;
843}
844
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800845/*
846 * show_docked - read method for "docked" file in sysfs
847 */
848static ssize_t show_docked(struct device *dev,
849 struct device_attribute *attr, char *buf)
850{
Shaohua Lidb350b02008-08-28 10:03:58 +0800851 struct dock_station *dock_station = *((struct dock_station **)
852 dev->platform_data);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800853 return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
854
855}
Adrian Bunke5685b92007-10-24 18:24:42 +0200856static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800857
858/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700859 * show_flags - read method for flags file in sysfs
860 */
861static ssize_t show_flags(struct device *dev,
862 struct device_attribute *attr, char *buf)
863{
Shaohua Lidb350b02008-08-28 10:03:58 +0800864 struct dock_station *dock_station = *((struct dock_station **)
865 dev->platform_data);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700866 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
867
868}
Adrian Bunke5685b92007-10-24 18:24:42 +0200869static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700870
871/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800872 * write_undock - write method for "undock" file in sysfs
873 */
874static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
875 const char *buf, size_t count)
876{
877 int ret;
Shaohua Lidb350b02008-08-28 10:03:58 +0800878 struct dock_station *dock_station = *((struct dock_station **)
879 dev->platform_data);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800880
881 if (!count)
882 return -EINVAL;
883
Holger Macht9171f832008-03-12 01:07:27 +0100884 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800885 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
886 return ret ? ret: count;
887}
Adrian Bunke5685b92007-10-24 18:24:42 +0200888static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800889
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800890/*
891 * show_dock_uid - read method for "uid" file in sysfs
892 */
893static ssize_t show_dock_uid(struct device *dev,
894 struct device_attribute *attr, char *buf)
895{
896 unsigned long lbuf;
Shaohua Lidb350b02008-08-28 10:03:58 +0800897 struct dock_station *dock_station = *((struct dock_station **)
898 dev->platform_data);
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700899 acpi_status status = acpi_evaluate_integer(dock_station->handle,
900 "_UID", NULL, &lbuf);
901 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800902 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700903
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800904 return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf);
905}
Adrian Bunke5685b92007-10-24 18:24:42 +0200906static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800907
Len Brownc8f7a622006-07-09 17:22:28 -0400908/**
909 * dock_add - add a new dock station
910 * @handle: the dock station handle
911 *
912 * allocated and initialize a new dock station device. Find all devices
913 * that are on the dock station, and register for dock event notifications.
914 */
915static int dock_add(acpi_handle handle)
916{
917 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400918 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800919 struct dock_station *dock_station;
920 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -0400921
922 /* allocate & initialize the dock_station private data */
923 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
924 if (!dock_station)
925 return -ENOMEM;
926 dock_station->handle = handle;
927 dock_station->last_dock_time = jiffies - HZ;
928 INIT_LIST_HEAD(&dock_station->dependent_devices);
929 INIT_LIST_HEAD(&dock_station->hotplug_devices);
Shaohua Lidb350b02008-08-28 10:03:58 +0800930 INIT_LIST_HEAD(&dock_station->sibiling);
Len Brownc8f7a622006-07-09 17:22:28 -0400931 spin_lock_init(&dock_station->dd_lock);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800932 mutex_init(&dock_station->hp_lock);
Kristen Accardi07a186842006-07-10 14:19:15 -0400933 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Len Brownc8f7a622006-07-09 17:22:28 -0400934
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800935 /* initialize platform device stuff */
Shaohua Lidb350b02008-08-28 10:03:58 +0800936 dock_station->dock_device =
937 platform_device_register_simple(dock_device_name,
938 dock_station_count, NULL, 0);
939 dock_device = dock_station->dock_device;
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700940 if (IS_ERR(dock_device)) {
941 kfree(dock_station);
942 dock_station = NULL;
943 return PTR_ERR(dock_device);
944 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800945 platform_device_add_data(dock_device, &dock_station,
946 sizeof(struct dock_station *));
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700947
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700948 /* we want the dock device to send uevents */
949 dock_device->dev.uevent_suppress = 0;
950
Shaohua Lidb350b02008-08-28 10:03:58 +0800951 if (is_dock(handle))
952 dock_station->flags |= DOCK_IS_DOCK;
953 if (is_ata(handle))
954 dock_station->flags |= DOCK_IS_ATA;
955 if (is_battery(handle))
956 dock_station->flags |= DOCK_IS_BAT;
957
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700958 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800959 if (ret) {
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700960 printk("Error %d adding sysfs file\n", ret);
961 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800962 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700963 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800964 return ret;
965 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700966 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800967 if (ret) {
968 printk("Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700969 device_remove_file(&dock_device->dev, &dev_attr_docked);
970 platform_device_unregister(dock_device);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800971 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700972 dock_station = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800973 return ret;
974 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700975 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800976 if (ret) {
977 printk("Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700978 device_remove_file(&dock_device->dev, &dev_attr_docked);
979 device_remove_file(&dock_device->dev, &dev_attr_undock);
980 platform_device_unregister(dock_device);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800981 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700982 dock_station = NULL;
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800983 return ret;
984 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700985 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
986 if (ret) {
987 printk("Error %d adding sysfs file\n", ret);
988 device_remove_file(&dock_device->dev, &dev_attr_docked);
989 device_remove_file(&dock_device->dev, &dev_attr_undock);
990 device_remove_file(&dock_device->dev, &dev_attr_uid);
991 platform_device_unregister(dock_device);
992 kfree(dock_station);
993 dock_station = NULL;
994 return ret;
995 }
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800996
Len Brownc8f7a622006-07-09 17:22:28 -0400997 /* Find dependent devices */
998 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
999 ACPI_UINT32_MAX, find_dock_devices, dock_station,
1000 NULL);
1001
1002 /* add the dock station as a device dependent on itself */
1003 dd = alloc_dock_dependent_device(handle);
1004 if (!dd) {
1005 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001006 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001007 ret = -ENOMEM;
1008 goto dock_add_err_unregister;
Len Brownc8f7a622006-07-09 17:22:28 -04001009 }
1010 add_dock_dependent_device(dock_station, dd);
1011
Shaohua Lidb350b02008-08-28 10:03:58 +08001012 dock_station_count++;
1013 list_add(&dock_station->sibiling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -04001014 return 0;
1015
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001016dock_add_err_unregister:
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001017 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);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001020 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001021 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001022 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001023 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -04001024 return ret;
1025}
1026
1027/**
1028 * dock_remove - free up resources related to the dock station
1029 */
Shaohua Lidb350b02008-08-28 10:03:58 +08001030static int dock_remove(struct dock_station *dock_station)
Len Brownc8f7a622006-07-09 17:22:28 -04001031{
1032 struct dock_dependent_device *dd, *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +08001033 struct platform_device *dock_device = dock_station->dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -04001034
Shaohua Lidb350b02008-08-28 10:03:58 +08001035 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -04001036 return 0;
1037
1038 /* remove dependent devices */
1039 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1040 list)
1041 kfree(dd);
1042
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001043 /* cleanup sysfs */
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001044 device_remove_file(&dock_device->dev, &dev_attr_docked);
1045 device_remove_file(&dock_device->dev, &dev_attr_undock);
1046 device_remove_file(&dock_device->dev, &dev_attr_uid);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001047 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001048 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001049
Len Brownc8f7a622006-07-09 17:22:28 -04001050 /* free dock station memory */
1051 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001052 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -04001053 return 0;
1054}
1055
1056/**
1057 * find_dock - look for a dock station
1058 * @handle: acpi handle of a device
1059 * @lvl: unused
1060 * @context: counter of dock stations found
1061 * @rv: unused
1062 *
1063 * This is called by acpi_walk_namespace to look for dock stations.
1064 */
1065static acpi_status
1066find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1067{
Len Brownc8f7a622006-07-09 17:22:28 -04001068 acpi_status status = AE_OK;
1069
1070 if (is_dock(handle)) {
1071 if (dock_add(handle) >= 0) {
Len Brownc8f7a622006-07-09 17:22:28 -04001072 status = AE_CTRL_TERMINATE;
1073 }
1074 }
1075 return status;
1076}
1077
Shaohua Lidb350b02008-08-28 10:03:58 +08001078static acpi_status
1079find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1080{
1081 /* If bay is in a dock, it's already handled */
1082 if (is_ejectable_bay(handle) && !is_dock_device(handle))
1083 dock_add(handle);
1084 return AE_OK;
1085}
1086
Len Brownc8f7a622006-07-09 17:22:28 -04001087static int __init dock_init(void)
1088{
Len Brown816c2ed2008-06-24 22:57:12 -04001089 if (acpi_disabled)
1090 return 0;
1091
Len Brownc8f7a622006-07-09 17:22:28 -04001092 /* look for a dock station */
1093 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Shaohua Lidb350b02008-08-28 10:03:58 +08001094 ACPI_UINT32_MAX, find_dock, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001095
Shaohua Lidb350b02008-08-28 10:03:58 +08001096 /* look for bay */
1097 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1098 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1099 if (!dock_station_count) {
1100 printk(KERN_INFO PREFIX "No dock devices found.\n");
1101 return 0;
1102 }
Len Brownc8f7a622006-07-09 17:22:28 -04001103
Shaohua Li6bd00a62008-08-28 10:04:29 +08001104 register_acpi_bus_notifier(&dock_acpi_notifier);
Shaohua Lidb350b02008-08-28 10:03:58 +08001105 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1106 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -04001107 return 0;
1108}
1109
1110static void __exit dock_exit(void)
1111{
Shaohua Lidb350b02008-08-28 10:03:58 +08001112 struct dock_station *dock_station;
1113
Shaohua Li6bd00a62008-08-28 10:04:29 +08001114 unregister_acpi_bus_notifier(&dock_acpi_notifier);
Shaohua Lidb350b02008-08-28 10:03:58 +08001115 list_for_each_entry(dock_station, &dock_stations, sibiling)
1116 dock_remove(dock_station);
Len Brownc8f7a622006-07-09 17:22:28 -04001117}
1118
Shaohua Lidb350b02008-08-28 10:03:58 +08001119/*
1120 * Must be called before drivers of devices in dock, otherwise we can't know
1121 * which devices are in a dock
1122 */
1123subsys_initcall(dock_init);
Len Brownc8f7a622006-07-09 17:22:28 -04001124module_exit(dock_exit);