blob: 7bdf93b746904c5713d5686be00d6b4efdc36501 [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 Accardi0f6f2802007-05-09 15:07:04 -070051static struct platform_device *dock_device;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080052static char dock_device_name[] = "dock";
Len Brownc8f7a622006-07-09 17:22:28 -040053
Frank Seidela340af12007-12-07 13:20:42 +010054static const struct acpi_device_id dock_device_ids[] = {
55 {"LNXDOCK", 0},
56 {"", 0},
57};
58MODULE_DEVICE_TABLE(acpi, dock_device_ids);
59
Len Brownc8f7a622006-07-09 17:22:28 -040060struct dock_station {
61 acpi_handle handle;
62 unsigned long last_dock_time;
63 u32 flags;
64 spinlock_t dd_lock;
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -080065 struct mutex hp_lock;
Len Brownc8f7a622006-07-09 17:22:28 -040066 struct list_head dependent_devices;
67 struct list_head hotplug_devices;
68};
69
70struct 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
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070079#define DOCK_UNDOCKING 0x00000002
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070080#define DOCK_EVENT 3
81#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040082
83static 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 */
95static struct dock_dependent_device *
96alloc_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 */
116static void
117add_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 */
132static void
133dock_add_hotplug_device(struct dock_station *ds,
134 struct dock_dependent_device *dd)
135{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800136 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400137 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800138 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400139}
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 */
148static void
149dock_del_hotplug_device(struct dock_station *ds,
150 struct dock_dependent_device *dd)
151{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800152 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400153 list_del(&dd->hotplug_list);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800154 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400155}
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 */
165static struct dock_dependent_device *
166find_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 */
191static 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 */
210int 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
221EXPORT_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 */
230static 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 */
256static 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 */
290static 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 */
313static void hotplug_dock_devices(struct dock_station *ds, u32 event)
314{
315 struct dock_dependent_device *dd;
316
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800317 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400318
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 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800339 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400340}
341
342static void dock_event(struct dock_station *ds, u32 event, int num)
343{
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700344 struct device *dev = &dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700345 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700346 char *envp[] = { event_string, NULL };
347
348 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700349 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700350 else
Holger Macht66b56822007-08-10 13:10:32 -0700351 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700352
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700353 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800354 * Indicate that the status of the dock station has
355 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700356 */
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700357 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400358}
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 */
367static 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 */
398static 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 };
Len Brownc8f7a622006-07-09 17:22:28 -0400405
406 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
Len Brownc8f7a622006-07-09 17:22:28 -0400407
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400408 printk(KERN_INFO PREFIX "%s - %s\n",
409 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400410
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))
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400418 printk(KERN_ERR PREFIX "%s - failed to execute _DCK\n",
419 (char *)name_buffer.pointer);
Len Brownc8f7a622006-07-09 17:22:28 -0400420 kfree(buffer.pointer);
421 kfree(name_buffer.pointer);
422}
423
424static inline void dock(struct dock_station *ds)
425{
426 handle_dock(ds, 1);
427}
428
429static inline void undock(struct dock_station *ds)
430{
431 handle_dock(ds, 0);
432}
433
434static inline void begin_dock(struct dock_station *ds)
435{
436 ds->flags |= DOCK_DOCKING;
437}
438
439static inline void complete_dock(struct dock_station *ds)
440{
441 ds->flags &= ~(DOCK_DOCKING);
442 ds->last_dock_time = jiffies;
443}
444
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700445static inline void begin_undock(struct dock_station *ds)
446{
447 ds->flags |= DOCK_UNDOCKING;
448}
449
450static inline void complete_undock(struct dock_station *ds)
451{
452 ds->flags &= ~(DOCK_UNDOCKING);
453}
454
Shaohua Li406f6922008-08-28 10:03:26 +0800455static void dock_lock(struct dock_station *ds, int lock)
456{
457 struct acpi_object_list arg_list;
458 union acpi_object arg;
459 acpi_status status;
460
461 arg_list.count = 1;
462 arg_list.pointer = &arg;
463 arg.type = ACPI_TYPE_INTEGER;
464 arg.integer.value = !!lock;
465 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
466 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
467 if (lock)
468 printk(KERN_WARNING PREFIX "Locking device failed\n");
469 else
470 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
471 }
472}
473
Len Brownc8f7a622006-07-09 17:22:28 -0400474/**
475 * dock_in_progress - see if we are in the middle of handling a dock event
476 * @ds: the dock station
477 *
478 * Sometimes while docking, false dock events can be sent to the driver
479 * because good connections aren't made or some other reason. Ignore these
480 * if we are in the middle of doing something.
481 */
482static int dock_in_progress(struct dock_station *ds)
483{
484 if ((ds->flags & DOCK_DOCKING) ||
485 time_before(jiffies, (ds->last_dock_time + HZ)))
486 return 1;
487 return 0;
488}
489
490/**
491 * register_dock_notifier - add yourself to the dock notifier list
492 * @nb: the callers notifier block
493 *
494 * If a driver wishes to be notified about dock events, they can
495 * use this function to put a notifier block on the dock notifier list.
496 * this notifier call chain will be called after a dock event, but
497 * before hotplugging any new devices.
498 */
499int register_dock_notifier(struct notifier_block *nb)
500{
Prarit Bhargava2548c062006-12-04 14:50:17 -0800501 if (!dock_station)
502 return -ENODEV;
503
Len Brownc8f7a622006-07-09 17:22:28 -0400504 return atomic_notifier_chain_register(&dock_notifier_list, nb);
505}
506
507EXPORT_SYMBOL_GPL(register_dock_notifier);
508
509/**
510 * unregister_dock_notifier - remove yourself from the dock notifier list
511 * @nb: the callers notifier block
512 */
513void unregister_dock_notifier(struct notifier_block *nb)
514{
Prarit Bhargava2548c062006-12-04 14:50:17 -0800515 if (!dock_station)
516 return;
517
Len Brownc8f7a622006-07-09 17:22:28 -0400518 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
519}
520
521EXPORT_SYMBOL_GPL(unregister_dock_notifier);
522
523/**
524 * register_hotplug_dock_device - register a hotplug function
525 * @handle: the handle of the device
526 * @handler: the acpi_notifier_handler to call after docking
527 * @context: device specific data
528 *
529 * If a driver would like to perform a hotplug operation after a dock
530 * event, they can register an acpi_notifiy_handler to be called by
531 * the dock driver after _DCK is executed.
532 */
533int
534register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
535 void *context)
536{
537 struct dock_dependent_device *dd;
538
539 if (!dock_station)
540 return -ENODEV;
541
542 /*
543 * make sure this handle is for a device dependent on the dock,
544 * this would include the dock station itself
545 */
546 dd = find_dock_dependent_device(dock_station, handle);
547 if (dd) {
548 dd->handler = handler;
549 dd->context = context;
550 dock_add_hotplug_device(dock_station, dd);
551 return 0;
552 }
553
554 return -EINVAL;
555}
556
557EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
558
559/**
560 * unregister_hotplug_dock_device - remove yourself from the hotplug list
561 * @handle: the acpi handle of the device
562 */
563void unregister_hotplug_dock_device(acpi_handle handle)
564{
565 struct dock_dependent_device *dd;
566
567 if (!dock_station)
568 return;
569
570 dd = find_dock_dependent_device(dock_station, handle);
571 if (dd)
572 dock_del_hotplug_device(dock_station, dd);
573}
574
575EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
576
577/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800578 * handle_eject_request - handle an undock request checking for error conditions
579 *
580 * Check to make sure the dock device is still present, then undock and
581 * hotremove all the devices that may need removing.
582 */
583static int handle_eject_request(struct dock_station *ds, u32 event)
584{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800585 if (dock_in_progress(ds))
586 return -EBUSY;
587
588 /*
589 * here we need to generate the undock
590 * event prior to actually doing the undock
591 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200592 * Also, even send the dock event if the
593 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800594 */
595 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200596
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800597 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
598 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800599 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800600 eject_dock(ds);
601 if (dock_present(ds)) {
602 printk(KERN_ERR PREFIX "Unable to undock!\n");
603 return -EBUSY;
604 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700605 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800606 return 0;
607}
608
609/**
Len Brownc8f7a622006-07-09 17:22:28 -0400610 * dock_notify - act upon an acpi dock notification
611 * @handle: the dock station handle
612 * @event: the acpi event
613 * @data: our driver data struct
614 *
615 * If we are notified to dock, then check to see if the dock is
616 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800617 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400618 */
619static void dock_notify(acpi_handle handle, u32 event, void *data)
620{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200621 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800622 struct acpi_device *tmp;
Len Brownc8f7a622006-07-09 17:22:28 -0400623
624 switch (event) {
625 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800626 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
627 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400628 begin_dock(ds);
629 dock(ds);
630 if (!dock_present(ds)) {
631 printk(KERN_ERR PREFIX "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800632 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400633 break;
634 }
635 atomic_notifier_call_chain(&dock_notifier_list,
636 event, NULL);
637 hotplug_dock_devices(ds, event);
638 complete_dock(ds);
639 dock_event(ds, event, DOCK_EVENT);
Shaohua Li406f6922008-08-28 10:03:26 +0800640 dock_lock(ds, 1);
Len Brownc8f7a622006-07-09 17:22:28 -0400641 }
642 break;
643 case ACPI_NOTIFY_DEVICE_CHECK:
644 /*
645 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
646 * is sent and _DCK is present, it is assumed to mean an
647 * undock request. This notify routine will only be called
648 * for objects defining _DCK, so we will fall through to eject
649 * request here. However, we will pass an eject request through
650 * to the driver who wish to hotplug.
651 */
652 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700653 begin_undock(ds);
654 if (immediate_undock)
655 handle_eject_request(ds, event);
656 else
657 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400658 break;
659 default:
660 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
661 }
662}
663
664/**
665 * find_dock_devices - find devices on the dock station
666 * @handle: the handle of the device we are examining
667 * @lvl: unused
668 * @context: the dock station private data
669 * @rv: unused
670 *
671 * This function is called by acpi_walk_namespace. It will
672 * check to see if an object has an _EJD method. If it does, then it
673 * will see if it is dependent on the dock station.
674 */
675static acpi_status
676find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
677{
678 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500679 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200680 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400681 struct dock_dependent_device *dd;
682
683 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500684 if (ACPI_FAILURE(status)) {
685 /* try the parent device as well */
686 status = acpi_get_parent(handle, &parent);
687 if (ACPI_FAILURE(status))
688 goto fdd_out;
689 /* see if parent is dependent on dock */
690 status = acpi_bus_get_ejd(parent, &tmp);
691 if (ACPI_FAILURE(status))
692 goto fdd_out;
693 }
Len Brownc8f7a622006-07-09 17:22:28 -0400694
695 if (tmp == ds->handle) {
696 dd = alloc_dock_dependent_device(handle);
697 if (dd)
698 add_dock_dependent_device(ds, dd);
699 }
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500700fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400701 return AE_OK;
702}
703
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800704/*
705 * show_docked - read method for "docked" file in sysfs
706 */
707static ssize_t show_docked(struct device *dev,
708 struct device_attribute *attr, char *buf)
709{
710 return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
711
712}
Adrian Bunke5685b92007-10-24 18:24:42 +0200713static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800714
715/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700716 * show_flags - read method for flags file in sysfs
717 */
718static ssize_t show_flags(struct device *dev,
719 struct device_attribute *attr, char *buf)
720{
721 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
722
723}
Adrian Bunke5685b92007-10-24 18:24:42 +0200724static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700725
726/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800727 * write_undock - write method for "undock" file in sysfs
728 */
729static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
730 const char *buf, size_t count)
731{
732 int ret;
733
734 if (!count)
735 return -EINVAL;
736
Holger Macht9171f832008-03-12 01:07:27 +0100737 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800738 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
739 return ret ? ret: count;
740}
Adrian Bunke5685b92007-10-24 18:24:42 +0200741static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800742
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800743/*
744 * show_dock_uid - read method for "uid" file in sysfs
745 */
746static ssize_t show_dock_uid(struct device *dev,
747 struct device_attribute *attr, char *buf)
748{
749 unsigned long lbuf;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700750 acpi_status status = acpi_evaluate_integer(dock_station->handle,
751 "_UID", NULL, &lbuf);
752 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800753 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700754
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800755 return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf);
756}
Adrian Bunke5685b92007-10-24 18:24:42 +0200757static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800758
Len Brownc8f7a622006-07-09 17:22:28 -0400759/**
760 * dock_add - add a new dock station
761 * @handle: the dock station handle
762 *
763 * allocated and initialize a new dock station device. Find all devices
764 * that are on the dock station, and register for dock event notifications.
765 */
766static int dock_add(acpi_handle handle)
767{
768 int ret;
769 acpi_status status;
770 struct dock_dependent_device *dd;
771
772 /* allocate & initialize the dock_station private data */
773 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
774 if (!dock_station)
775 return -ENOMEM;
776 dock_station->handle = handle;
777 dock_station->last_dock_time = jiffies - HZ;
778 INIT_LIST_HEAD(&dock_station->dependent_devices);
779 INIT_LIST_HEAD(&dock_station->hotplug_devices);
780 spin_lock_init(&dock_station->dd_lock);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800781 mutex_init(&dock_station->hp_lock);
Kristen Accardi07a186842006-07-10 14:19:15 -0400782 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Len Brownc8f7a622006-07-09 17:22:28 -0400783
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800784 /* initialize platform device stuff */
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700785 dock_device =
786 platform_device_register_simple(dock_device_name, 0, NULL, 0);
787 if (IS_ERR(dock_device)) {
788 kfree(dock_station);
789 dock_station = NULL;
790 return PTR_ERR(dock_device);
791 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700792
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700793 /* we want the dock device to send uevents */
794 dock_device->dev.uevent_suppress = 0;
795
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700796 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800797 if (ret) {
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700798 printk("Error %d adding sysfs file\n", ret);
799 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800800 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700801 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800802 return ret;
803 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700804 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800805 if (ret) {
806 printk("Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700807 device_remove_file(&dock_device->dev, &dev_attr_docked);
808 platform_device_unregister(dock_device);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800809 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700810 dock_station = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800811 return ret;
812 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700813 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800814 if (ret) {
815 printk("Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700816 device_remove_file(&dock_device->dev, &dev_attr_docked);
817 device_remove_file(&dock_device->dev, &dev_attr_undock);
818 platform_device_unregister(dock_device);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800819 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700820 dock_station = NULL;
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800821 return ret;
822 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700823 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
824 if (ret) {
825 printk("Error %d adding sysfs file\n", ret);
826 device_remove_file(&dock_device->dev, &dev_attr_docked);
827 device_remove_file(&dock_device->dev, &dev_attr_undock);
828 device_remove_file(&dock_device->dev, &dev_attr_uid);
829 platform_device_unregister(dock_device);
830 kfree(dock_station);
831 dock_station = NULL;
832 return ret;
833 }
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800834
Len Brownc8f7a622006-07-09 17:22:28 -0400835 /* Find dependent devices */
836 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
837 ACPI_UINT32_MAX, find_dock_devices, dock_station,
838 NULL);
839
840 /* add the dock station as a device dependent on itself */
841 dd = alloc_dock_dependent_device(handle);
842 if (!dd) {
843 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700844 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800845 ret = -ENOMEM;
846 goto dock_add_err_unregister;
Len Brownc8f7a622006-07-09 17:22:28 -0400847 }
848 add_dock_dependent_device(dock_station, dd);
849
850 /* register for dock events */
851 status = acpi_install_notify_handler(dock_station->handle,
852 ACPI_SYSTEM_NOTIFY,
853 dock_notify, dock_station);
854
855 if (ACPI_FAILURE(status)) {
856 printk(KERN_ERR PREFIX "Error installing notify handler\n");
857 ret = -ENODEV;
858 goto dock_add_err;
859 }
860
Tim Pepper1fdd6862008-06-09 16:22:25 -0700861 printk(KERN_INFO PREFIX "%s\n", ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -0400862
863 return 0;
864
865dock_add_err:
Len Brownc8f7a622006-07-09 17:22:28 -0400866 kfree(dd);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800867dock_add_err_unregister:
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700868 device_remove_file(&dock_device->dev, &dev_attr_docked);
869 device_remove_file(&dock_device->dev, &dev_attr_undock);
870 device_remove_file(&dock_device->dev, &dev_attr_uid);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700871 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700872 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800873 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700874 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -0400875 return ret;
876}
877
878/**
879 * dock_remove - free up resources related to the dock station
880 */
881static int dock_remove(void)
882{
883 struct dock_dependent_device *dd, *tmp;
884 acpi_status status;
885
886 if (!dock_station)
887 return 0;
888
889 /* remove dependent devices */
890 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
891 list)
892 kfree(dd);
893
894 /* remove dock notify handler */
895 status = acpi_remove_notify_handler(dock_station->handle,
896 ACPI_SYSTEM_NOTIFY,
897 dock_notify);
898 if (ACPI_FAILURE(status))
899 printk(KERN_ERR "Error removing notify handler\n");
900
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800901 /* cleanup sysfs */
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700902 device_remove_file(&dock_device->dev, &dev_attr_docked);
903 device_remove_file(&dock_device->dev, &dev_attr_undock);
904 device_remove_file(&dock_device->dev, &dev_attr_uid);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700905 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700906 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800907
Len Brownc8f7a622006-07-09 17:22:28 -0400908 /* free dock station memory */
909 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700910 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -0400911 return 0;
912}
913
914/**
915 * find_dock - look for a dock station
916 * @handle: acpi handle of a device
917 * @lvl: unused
918 * @context: counter of dock stations found
919 * @rv: unused
920 *
921 * This is called by acpi_walk_namespace to look for dock stations.
922 */
923static acpi_status
924find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
925{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200926 int *count = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400927 acpi_status status = AE_OK;
928
929 if (is_dock(handle)) {
930 if (dock_add(handle) >= 0) {
931 (*count)++;
932 status = AE_CTRL_TERMINATE;
933 }
934 }
935 return status;
936}
937
938static int __init dock_init(void)
939{
940 int num = 0;
941
942 dock_station = NULL;
943
Len Brown816c2ed2008-06-24 22:57:12 -0400944 if (acpi_disabled)
945 return 0;
946
Len Brownc8f7a622006-07-09 17:22:28 -0400947 /* look for a dock station */
948 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
949 ACPI_UINT32_MAX, find_dock, &num, NULL);
950
951 if (!num)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800952 printk(KERN_INFO "No dock devices found.\n");
Len Brownc8f7a622006-07-09 17:22:28 -0400953
954 return 0;
955}
956
957static void __exit dock_exit(void)
958{
959 dock_remove();
960}
961
962postcore_initcall(dock_init);
963module_exit(dock_exit);