blob: 39536b80bce74177768ebd46a7013c6485a6622b [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;
Shaohua Lidb350b02008-08-28 10:03:58 +0800234 int ret = 1;
235
Bob Moore15b8dd52009-06-29 13:39:29 +0800236 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800237 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800238 if (!(info->valid & ACPI_VALID_HID))
239 ret = 0;
240 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800241 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800242
Bob Moore15b8dd52009-06-29 13:39:29 +0800243 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800244 return ret;
245}
246
247static 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
Len Brownc8f7a622006-07-09 17:22:28 -0400259/**
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 */
267int is_dock_device(acpi_handle handle)
268{
Shaohua Lidb350b02008-08-28 10:03:58 +0800269 struct dock_station *dock_station;
270
271 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400272 return 0;
273
Shaohua Lidb350b02008-08-28 10:03:58 +0800274 if (is_dock(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400275 return 1;
Shaohua Lidb350b02008-08-28 10:03:58 +0800276 list_for_each_entry(dock_station, &dock_stations, sibiling) {
277 if (find_dock_dependent_device(dock_station, handle))
278 return 1;
279 }
Len Brownc8f7a622006-07-09 17:22:28 -0400280
281 return 0;
282}
283
284EXPORT_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 */
293static int dock_present(struct dock_station *ds)
294{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400295 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400296 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 */
319static 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 */
353static 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 */
376static void hotplug_dock_devices(struct dock_station *ds, u32 event)
377{
378 struct dock_dependent_device *dd;
379
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800380 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400381
382 /*
383 * First call driver specific hotplug functions
384 */
385 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800386 if (dd->ops && dd->ops->handler)
387 dd->ops->handler(dd->handle, event, dd->context);
Len Brownc8f7a622006-07-09 17:22:28 -0400388 }
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 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800402 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400403}
404
405static void dock_event(struct dock_station *ds, u32 event, int num)
406{
Shaohua Lidb350b02008-08-28 10:03:58 +0800407 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700408 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700409 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800410 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700411
412 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700413 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700414 else
Holger Macht66b56822007-08-10 13:10:32 -0700415 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700416
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700417 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800418 * Indicate that the status of the dock station has
419 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700420 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800421 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);
Len Brownc8f7a622006-07-09 17:22:28 -0400429}
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 */
438static 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 */
469static 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 };
Len Brownc8f7a622006-07-09 17:22:28 -0400476
477 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
Len Brownc8f7a622006-07-09 17:22:28 -0400478
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400479 printk(KERN_INFO PREFIX "%s - %s\n",
480 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400481
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);
Shaohua Lidb350b02008-08-28 10:03:58 +0800488 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Thomas Renninger0a918a92008-10-11 00:15:04 -0400489 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
490 " _DCK\n", (char *)name_buffer.pointer));
491
Len Brownc8f7a622006-07-09 17:22:28 -0400492 kfree(buffer.pointer);
493 kfree(name_buffer.pointer);
494}
495
496static inline void dock(struct dock_station *ds)
497{
498 handle_dock(ds, 1);
499}
500
501static inline void undock(struct dock_station *ds)
502{
503 handle_dock(ds, 0);
504}
505
506static inline void begin_dock(struct dock_station *ds)
507{
508 ds->flags |= DOCK_DOCKING;
509}
510
511static inline void complete_dock(struct dock_station *ds)
512{
513 ds->flags &= ~(DOCK_DOCKING);
514 ds->last_dock_time = jiffies;
515}
516
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700517static inline void begin_undock(struct dock_station *ds)
518{
519 ds->flags |= DOCK_UNDOCKING;
520}
521
522static inline void complete_undock(struct dock_station *ds)
523{
524 ds->flags &= ~(DOCK_UNDOCKING);
525}
526
Shaohua Li406f6922008-08-28 10:03:26 +0800527static 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
Len Brownc8f7a622006-07-09 17:22:28 -0400546/**
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 */
554static 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 */
571int register_dock_notifier(struct notifier_block *nb)
572{
Shaohua Lidb350b02008-08-28 10:03:58 +0800573 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800574 return -ENODEV;
575
Len Brownc8f7a622006-07-09 17:22:28 -0400576 return atomic_notifier_chain_register(&dock_notifier_list, nb);
577}
578
579EXPORT_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 */
585void unregister_dock_notifier(struct notifier_block *nb)
586{
Shaohua Lidb350b02008-08-28 10:03:58 +0800587 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800588 return;
589
Len Brownc8f7a622006-07-09 17:22:28 -0400590 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
591}
592
593EXPORT_SYMBOL_GPL(unregister_dock_notifier);
594
595/**
596 * register_hotplug_dock_device - register a hotplug function
597 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800598 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400599 * @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 */
605int
Shaohua Li1253f7a2008-08-28 10:06:16 +0800606register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
Len Brownc8f7a622006-07-09 17:22:28 -0400607 void *context)
608{
609 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800610 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800611 int ret = -EINVAL;
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) {
Shaohua Li61b83692008-08-28 10:07:14 +0800621 /*
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 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800626 dd = find_dock_dependent_device(dock_station, handle);
627 if (dd) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800628 dd->ops = ops;
Shaohua Lidb350b02008-08-28 10:03:58 +0800629 dd->context = context;
630 dock_add_hotplug_device(dock_station, dd);
Shaohua Li61b83692008-08-28 10:07:14 +0800631 ret = 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800632 }
Len Brownc8f7a622006-07-09 17:22:28 -0400633 }
634
Shaohua Li61b83692008-08-28 10:07:14 +0800635 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400636}
637
638EXPORT_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 */
644void unregister_hotplug_dock_device(acpi_handle handle)
645{
646 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800647 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400648
Shaohua Lidb350b02008-08-28 10:03:58 +0800649 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400650 return;
651
Shaohua Lidb350b02008-08-28 10:03:58 +0800652 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 }
Len Brownc8f7a622006-07-09 17:22:28 -0400657}
658
659EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
660
661/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800662 * 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 */
667static int handle_eject_request(struct dock_station *ds, u32 event)
668{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800669 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.
Holger Machtafd73012008-08-06 17:56:01 +0200676 * Also, even send the dock event if the
677 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800678 */
679 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200680
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800681 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
682 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800683 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800684 eject_dock(ds);
685 if (dock_present(ds)) {
686 printk(KERN_ERR PREFIX "Unable to undock!\n");
687 return -EBUSY;
688 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700689 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800690 return 0;
691}
692
693/**
Len Brownc8f7a622006-07-09 17:22:28 -0400694 * 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,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800701 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400702 */
703static void dock_notify(acpi_handle handle, u32 event, void *data)
704{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200705 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800706 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800707 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400708
Shaohua Lidb350b02008-08-28 10:03:58 +0800709 /*
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 */
Len Brownc8f7a622006-07-09 17:22:28 -0400726 switch (event) {
727 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800728 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800729 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
730 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400731 begin_dock(ds);
732 dock(ds);
733 if (!dock_present(ds)) {
734 printk(KERN_ERR PREFIX "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800735 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400736 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);
Shaohua Li406f6922008-08-28 10:03:26 +0800743 dock_lock(ds, 1);
Shaohua Lidb350b02008-08-28 10:03:58 +0800744 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400745 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800746 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 */
Len Brownc8f7a622006-07-09 17:22:28 -0400752 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700753 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800754 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
755 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700756 handle_eject_request(ds, event);
757 else
758 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400759 break;
760 default:
761 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
762 }
763}
764
Zhang Rui19cd8472008-08-28 10:05:06 +0800765struct dock_data {
766 acpi_handle handle;
767 unsigned long event;
768 struct dock_station *ds;
769};
770
771static 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
Shaohua Li6bd00a62008-08-28 10:04:29 +0800779static 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) {
Zhang Rui19cd8472008-08-28 10:05:06 +0800790 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);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800800 return 0 ;
801 }
802 }
803 return 0;
804}
805
806static struct notifier_block dock_acpi_notifier = {
807 .notifier_call = acpi_dock_notifier_call,
808};
809
Len Brownc8f7a622006-07-09 17:22:28 -0400810/**
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 */
821static acpi_status
822find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
823{
824 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500825 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200826 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400827 struct dock_dependent_device *dd;
828
829 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500830 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 }
Len Brownc8f7a622006-07-09 17:22:28 -0400840
841 if (tmp == ds->handle) {
842 dd = alloc_dock_dependent_device(handle);
843 if (dd)
844 add_dock_dependent_device(ds, dd);
845 }
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500846fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400847 return AE_OK;
848}
849
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800850/*
851 * show_docked - read method for "docked" file in sysfs
852 */
853static ssize_t show_docked(struct device *dev,
854 struct device_attribute *attr, char *buf)
855{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100856 struct acpi_device *tmp;
857
Shaohua Lidb350b02008-08-28 10:03:58 +0800858 struct dock_station *dock_station = *((struct dock_station **)
859 dev->platform_data);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800860
Holger Machtfc5a9f82009-01-20 12:18:24 +0100861 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");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800864}
Adrian Bunke5685b92007-10-24 18:24:42 +0200865static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800866
867/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700868 * show_flags - read method for flags file in sysfs
869 */
870static ssize_t show_flags(struct device *dev,
871 struct device_attribute *attr, char *buf)
872{
Shaohua Lidb350b02008-08-28 10:03:58 +0800873 struct dock_station *dock_station = *((struct dock_station **)
874 dev->platform_data);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700875 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
876
877}
Adrian Bunke5685b92007-10-24 18:24:42 +0200878static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700879
880/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800881 * write_undock - write method for "undock" file in sysfs
882 */
883static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
884 const char *buf, size_t count)
885{
886 int ret;
Shaohua Lidb350b02008-08-28 10:03:58 +0800887 struct dock_station *dock_station = *((struct dock_station **)
888 dev->platform_data);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800889
890 if (!count)
891 return -EINVAL;
892
Holger Macht9171f832008-03-12 01:07:27 +0100893 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800894 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
895 return ret ? ret: count;
896}
Adrian Bunke5685b92007-10-24 18:24:42 +0200897static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800898
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800899/*
900 * show_dock_uid - read method for "uid" file in sysfs
901 */
902static ssize_t show_dock_uid(struct device *dev,
903 struct device_attribute *attr, char *buf)
904{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400905 unsigned long long lbuf;
Shaohua Lidb350b02008-08-28 10:03:58 +0800906 struct dock_station *dock_station = *((struct dock_station **)
907 dev->platform_data);
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700908 acpi_status status = acpi_evaluate_integer(dock_station->handle,
909 "_UID", NULL, &lbuf);
910 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800911 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700912
Matthew Wilcox27663c52008-10-10 02:22:59 -0400913 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800914}
Adrian Bunke5685b92007-10-24 18:24:42 +0200915static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800916
Shaohua Li8652b002008-08-28 10:07:45 +0800917static 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}
935static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
936
Len Brownc8f7a622006-07-09 17:22:28 -0400937/**
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 */
944static int dock_add(acpi_handle handle)
945{
946 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400947 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800948 struct dock_station *dock_station;
949 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -0400950
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);
Shaohua Lidb350b02008-08-28 10:03:58 +0800959 INIT_LIST_HEAD(&dock_station->sibiling);
Len Brownc8f7a622006-07-09 17:22:28 -0400960 spin_lock_init(&dock_station->dd_lock);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800961 mutex_init(&dock_station->hp_lock);
Kristen Accardi07a186842006-07-10 14:19:15 -0400962 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Len Brownc8f7a622006-07-09 17:22:28 -0400963
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800964 /* initialize platform device stuff */
Shaohua Lidb350b02008-08-28 10:03:58 +0800965 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;
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700969 if (IS_ERR(dock_device)) {
970 kfree(dock_station);
971 dock_station = NULL;
972 return PTR_ERR(dock_device);
973 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800974 platform_device_add_data(dock_device, &dock_station,
975 sizeof(struct dock_station *));
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700976
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700977 /* we want the dock device to send uevents */
Ming Leif67f1292009-03-01 21:10:49 +0800978 dev_set_uevent_suppress(&dock_device->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700979
Shaohua Lidb350b02008-08-28 10:03:58 +0800980 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
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700987 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800988 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +0100989 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700990 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800991 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700992 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800993 return ret;
994 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700995 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800996 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +0100997 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700998 device_remove_file(&dock_device->dev, &dev_attr_docked);
999 platform_device_unregister(dock_device);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -08001000 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001001 dock_station = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -08001002 return ret;
1003 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001004 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -08001005 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +01001006 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001007 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);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -08001010 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001011 dock_station = NULL;
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -08001012 return ret;
1013 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001014 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
1015 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +01001016 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -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);
1020 platform_device_unregister(dock_device);
1021 kfree(dock_station);
1022 dock_station = NULL;
1023 return ret;
1024 }
Shaohua Li8652b002008-08-28 10:07:45 +08001025 ret = device_create_file(&dock_device->dev, &dev_attr_type);
1026 if (ret)
1027 printk(KERN_ERR"Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001028
Len Brownc8f7a622006-07-09 17:22:28 -04001029 /* 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);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001038 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001039 ret = -ENOMEM;
1040 goto dock_add_err_unregister;
Len Brownc8f7a622006-07-09 17:22:28 -04001041 }
1042 add_dock_dependent_device(dock_station, dd);
1043
Shaohua Lidb350b02008-08-28 10:03:58 +08001044 dock_station_count++;
1045 list_add(&dock_station->sibiling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -04001046 return 0;
1047
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001048dock_add_err_unregister:
Shaohua Li8652b002008-08-28 10:07:45 +08001049 device_remove_file(&dock_device->dev, &dev_attr_type);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001050 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);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001053 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001054 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001055 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001056 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -04001057 return ret;
1058}
1059
1060/**
1061 * dock_remove - free up resources related to the dock station
1062 */
Shaohua Lidb350b02008-08-28 10:03:58 +08001063static int dock_remove(struct dock_station *dock_station)
Len Brownc8f7a622006-07-09 17:22:28 -04001064{
1065 struct dock_dependent_device *dd, *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +08001066 struct platform_device *dock_device = dock_station->dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -04001067
Shaohua Lidb350b02008-08-28 10:03:58 +08001068 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -04001069 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
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001076 /* cleanup sysfs */
Shaohua Li8652b002008-08-28 10:07:45 +08001077 device_remove_file(&dock_device->dev, &dev_attr_type);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001078 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);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001081 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001082 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001083
Len Brownc8f7a622006-07-09 17:22:28 -04001084 /* free dock station memory */
1085 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001086 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -04001087 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 */
1099static acpi_status
1100find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1101{
Len Brownc8f7a622006-07-09 17:22:28 -04001102 acpi_status status = AE_OK;
1103
1104 if (is_dock(handle)) {
1105 if (dock_add(handle) >= 0) {
Len Brownc8f7a622006-07-09 17:22:28 -04001106 status = AE_CTRL_TERMINATE;
1107 }
1108 }
1109 return status;
1110}
1111
Shaohua Lidb350b02008-08-28 10:03:58 +08001112static acpi_status
1113find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1114{
Shaohua Li61b83692008-08-28 10:07:14 +08001115 /* If bay is a dock, it's already handled */
1116 if (is_ejectable_bay(handle) && !is_dock(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +08001117 dock_add(handle);
1118 return AE_OK;
1119}
1120
Len Brownc8f7a622006-07-09 17:22:28 -04001121static int __init dock_init(void)
1122{
Len Brown816c2ed2008-06-24 22:57:12 -04001123 if (acpi_disabled)
1124 return 0;
1125
Len Brownc8f7a622006-07-09 17:22:28 -04001126 /* look for a dock station */
1127 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Shaohua Lidb350b02008-08-28 10:03:58 +08001128 ACPI_UINT32_MAX, find_dock, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001129
Shaohua Lidb350b02008-08-28 10:03:58 +08001130 /* 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 }
Len Brownc8f7a622006-07-09 17:22:28 -04001137
Shaohua Li6bd00a62008-08-28 10:04:29 +08001138 register_acpi_bus_notifier(&dock_acpi_notifier);
Shaohua Lidb350b02008-08-28 10:03:58 +08001139 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1140 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -04001141 return 0;
1142}
1143
1144static void __exit dock_exit(void)
1145{
Shaohua Lidb350b02008-08-28 10:03:58 +08001146 struct dock_station *dock_station;
Dan Carpenterf2407292009-04-02 08:29:56 +03001147 struct dock_station *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +08001148
Shaohua Li6bd00a62008-08-28 10:04:29 +08001149 unregister_acpi_bus_notifier(&dock_acpi_notifier);
Dan Carpenterf2407292009-04-02 08:29:56 +03001150 list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibiling)
Shaohua Lidb350b02008-08-28 10:03:58 +08001151 dock_remove(dock_station);
Len Brownc8f7a622006-07-09 17:22:28 -04001152}
1153
Shaohua Lidb350b02008-08-28 10:03:58 +08001154/*
1155 * Must be called before drivers of devices in dock, otherwise we can't know
1156 * which devices are in a dock
1157 */
1158subsys_initcall(dock_init);
Len Brownc8f7a622006-07-09 17:22:28 -04001159module_exit(dock_exit);