blob: 9a855669ff12427e5b4a5e39c9c7d45ec7e374f5 [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 Browna192a952009-07-28 16:45:54 -040036#define PREFIX "ACPI: "
37
Len Brown7cda93e2007-02-12 23:50:02 -050038#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a622006-07-09 17:22:28 -040039
Len Brownf52fd662007-02-12 22:42:12 -050040ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040041MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050042MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -040043MODULE_LICENSE("GPL");
44
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070045static int immediate_undock = 1;
46module_param(immediate_undock, bool, 0644);
47MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
48 "undock immediately when the undock button is pressed, 0 will cause"
49 " the driver to wait for userspace to write the undock sysfs file "
50 " before undocking");
51
Len Brownc8f7a622006-07-09 17:22:28 -040052static struct atomic_notifier_head dock_notifier_list;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080053static char dock_device_name[] = "dock";
Len Brownc8f7a622006-07-09 17:22:28 -040054
Frank Seidela340af12007-12-07 13:20:42 +010055static const struct acpi_device_id dock_device_ids[] = {
56 {"LNXDOCK", 0},
57 {"", 0},
58};
59MODULE_DEVICE_TABLE(acpi, dock_device_ids);
60
Len Brownc8f7a622006-07-09 17:22:28 -040061struct dock_station {
62 acpi_handle handle;
63 unsigned long last_dock_time;
64 u32 flags;
65 spinlock_t dd_lock;
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -080066 struct mutex hp_lock;
Len Brownc8f7a622006-07-09 17:22:28 -040067 struct list_head dependent_devices;
68 struct list_head hotplug_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080069
70 struct list_head sibiling;
71 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040072};
Shaohua Lidb350b02008-08-28 10:03:58 +080073static LIST_HEAD(dock_stations);
74static int dock_station_count;
Len Brownc8f7a622006-07-09 17:22:28 -040075
76struct dock_dependent_device {
77 struct list_head list;
78 struct list_head hotplug_list;
79 acpi_handle handle;
Shaohua Li1253f7a2008-08-28 10:06:16 +080080 struct acpi_dock_ops *ops;
Len Brownc8f7a622006-07-09 17:22:28 -040081 void *context;
82};
83
84#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070085#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080086#define DOCK_IS_DOCK 0x00000010
87#define DOCK_IS_ATA 0x00000020
88#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070089#define DOCK_EVENT 3
90#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040091
Len Brownc8f7a622006-07-09 17:22:28 -040092/*****************************************************************************
93 * Dock Dependent device functions *
94 *****************************************************************************/
95/**
96 * alloc_dock_dependent_device - allocate and init a dependent device
97 * @handle: the acpi_handle of the dependent device
98 *
99 * Allocate memory for a dependent device structure for a device referenced
100 * by the acpi handle
101 */
102static struct dock_dependent_device *
103alloc_dock_dependent_device(acpi_handle handle)
104{
105 struct dock_dependent_device *dd;
106
107 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
108 if (dd) {
109 dd->handle = handle;
110 INIT_LIST_HEAD(&dd->list);
111 INIT_LIST_HEAD(&dd->hotplug_list);
112 }
113 return dd;
114}
115
116/**
117 * add_dock_dependent_device - associate a device with the dock station
118 * @ds: The dock station
119 * @dd: The dependent device
120 *
121 * Add the dependent device to the dock's dependent device list.
122 */
123static void
124add_dock_dependent_device(struct dock_station *ds,
125 struct dock_dependent_device *dd)
126{
127 spin_lock(&ds->dd_lock);
128 list_add_tail(&dd->list, &ds->dependent_devices);
129 spin_unlock(&ds->dd_lock);
130}
131
132/**
133 * dock_add_hotplug_device - associate a hotplug handler with the dock station
134 * @ds: The dock station
135 * @dd: The dependent device struct
136 *
137 * Add the dependent device to the dock's hotplug device list
138 */
139static void
140dock_add_hotplug_device(struct dock_station *ds,
141 struct dock_dependent_device *dd)
142{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800143 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400144 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800145 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400146}
147
148/**
149 * dock_del_hotplug_device - remove a hotplug handler from the dock station
150 * @ds: The dock station
151 * @dd: the dependent device struct
152 *
153 * Delete the dependent device from the dock's hotplug device list
154 */
155static void
156dock_del_hotplug_device(struct dock_station *ds,
157 struct dock_dependent_device *dd)
158{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800159 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400160 list_del(&dd->hotplug_list);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800161 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400162}
163
164/**
165 * find_dock_dependent_device - get a device dependent on this dock
166 * @ds: the dock station
167 * @handle: the acpi_handle of the device we want
168 *
169 * iterate over the dependent device list for this dock. If the
170 * dependent device matches the handle, return.
171 */
172static struct dock_dependent_device *
173find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
174{
175 struct dock_dependent_device *dd;
176
177 spin_lock(&ds->dd_lock);
178 list_for_each_entry(dd, &ds->dependent_devices, list) {
179 if (handle == dd->handle) {
180 spin_unlock(&ds->dd_lock);
181 return dd;
182 }
183 }
184 spin_unlock(&ds->dd_lock);
185 return NULL;
186}
187
188/*****************************************************************************
189 * Dock functions *
190 *****************************************************************************/
191/**
192 * is_dock - see if a device is a dock station
193 * @handle: acpi handle of the device
194 *
195 * If an acpi object has a _DCK method, then it is by definition a dock
196 * station, so return true.
197 */
198static int is_dock(acpi_handle handle)
199{
200 acpi_status status;
201 acpi_handle tmp;
202
203 status = acpi_get_handle(handle, "_DCK", &tmp);
204 if (ACPI_FAILURE(status))
205 return 0;
206 return 1;
207}
208
Shaohua Lidb350b02008-08-28 10:03:58 +0800209static int is_ejectable(acpi_handle handle)
210{
211 acpi_status status;
212 acpi_handle tmp;
213
214 status = acpi_get_handle(handle, "_EJ0", &tmp);
215 if (ACPI_FAILURE(status))
216 return 0;
217 return 1;
218}
219
220static int is_ata(acpi_handle handle)
221{
222 acpi_handle tmp;
223
224 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
225 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
226 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
227 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
228 return 1;
229
230 return 0;
231}
232
233static int is_battery(acpi_handle handle)
234{
235 struct acpi_device_info *info;
236 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
237 int ret = 1;
238
239 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &buffer)))
240 return 0;
241 info = buffer.pointer;
242 if (!(info->valid & ACPI_VALID_HID))
243 ret = 0;
244 else
245 ret = !strcmp("PNP0C0A", info->hardware_id.value);
246
247 kfree(buffer.pointer);
248 return ret;
249}
250
251static int is_ejectable_bay(acpi_handle handle)
252{
253 acpi_handle phandle;
254 if (!is_ejectable(handle))
255 return 0;
256 if (is_battery(handle) || is_ata(handle))
257 return 1;
258 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
259 return 1;
260 return 0;
261}
262
Len Brownc8f7a622006-07-09 17:22:28 -0400263/**
264 * is_dock_device - see if a device is on a dock station
265 * @handle: acpi handle of the device
266 *
267 * If this device is either the dock station itself,
268 * or is a device dependent on the dock station, then it
269 * is a dock device
270 */
271int is_dock_device(acpi_handle handle)
272{
Shaohua Lidb350b02008-08-28 10:03:58 +0800273 struct dock_station *dock_station;
274
275 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400276 return 0;
277
Shaohua Lidb350b02008-08-28 10:03:58 +0800278 if (is_dock(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400279 return 1;
Shaohua Lidb350b02008-08-28 10:03:58 +0800280 list_for_each_entry(dock_station, &dock_stations, sibiling) {
281 if (find_dock_dependent_device(dock_station, handle))
282 return 1;
283 }
Len Brownc8f7a622006-07-09 17:22:28 -0400284
285 return 0;
286}
287
288EXPORT_SYMBOL_GPL(is_dock_device);
289
290/**
291 * dock_present - see if the dock station is present.
292 * @ds: the dock station
293 *
294 * execute the _STA method. note that present does not
295 * imply that we are docked.
296 */
297static int dock_present(struct dock_station *ds)
298{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400299 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400300 acpi_status status;
301
302 if (ds) {
303 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
304 if (ACPI_SUCCESS(status) && sta)
305 return 1;
306 }
307 return 0;
308}
309
310
311
312/**
313 * dock_create_acpi_device - add new devices to acpi
314 * @handle - handle of the device to add
315 *
316 * This function will create a new acpi_device for the given
317 * handle if one does not exist already. This should cause
318 * acpi to scan for drivers for the given devices, and call
319 * matching driver's add routine.
320 *
321 * Returns a pointer to the acpi_device corresponding to the handle.
322 */
323static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
324{
325 struct acpi_device *device = NULL;
326 struct acpi_device *parent_device;
327 acpi_handle parent;
328 int ret;
329
330 if (acpi_bus_get_device(handle, &device)) {
331 /*
332 * no device created for this object,
333 * so we should create one.
334 */
335 acpi_get_parent(handle, &parent);
336 if (acpi_bus_get_device(parent, &parent_device))
337 parent_device = NULL;
338
339 ret = acpi_bus_add(&device, parent_device, handle,
340 ACPI_BUS_TYPE_DEVICE);
341 if (ret) {
342 pr_debug("error adding bus, %x\n",
343 -ret);
344 return NULL;
345 }
346 }
347 return device;
348}
349
350/**
351 * dock_remove_acpi_device - remove the acpi_device struct from acpi
352 * @handle - the handle of the device to remove
353 *
354 * Tell acpi to remove the acpi_device. This should cause any loaded
355 * driver to have it's remove routine called.
356 */
357static void dock_remove_acpi_device(acpi_handle handle)
358{
359 struct acpi_device *device;
360 int ret;
361
362 if (!acpi_bus_get_device(handle, &device)) {
363 ret = acpi_bus_trim(device, 1);
364 if (ret)
365 pr_debug("error removing bus, %x\n", -ret);
366 }
367}
368
369
370/**
371 * hotplug_dock_devices - insert or remove devices on the dock station
372 * @ds: the dock station
373 * @event: either bus check or eject request
374 *
375 * Some devices on the dock station need to have drivers called
376 * to perform hotplug operations after a dock event has occurred.
377 * Traverse the list of dock devices that have registered a
378 * hotplug handler, and call the handler.
379 */
380static void hotplug_dock_devices(struct dock_station *ds, u32 event)
381{
382 struct dock_dependent_device *dd;
383
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800384 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400385
386 /*
387 * First call driver specific hotplug functions
388 */
389 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800390 if (dd->ops && dd->ops->handler)
391 dd->ops->handler(dd->handle, event, dd->context);
Len Brownc8f7a622006-07-09 17:22:28 -0400392 }
393
394 /*
395 * Now make sure that an acpi_device is created for each
396 * dependent device, or removed if this is an eject request.
397 * This will cause acpi_drivers to be stopped/started if they
398 * exist
399 */
400 list_for_each_entry(dd, &ds->dependent_devices, list) {
401 if (event == ACPI_NOTIFY_EJECT_REQUEST)
402 dock_remove_acpi_device(dd->handle);
403 else
404 dock_create_acpi_device(dd->handle);
405 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800406 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400407}
408
409static void dock_event(struct dock_station *ds, u32 event, int num)
410{
Shaohua Lidb350b02008-08-28 10:03:58 +0800411 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700412 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700413 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800414 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700415
416 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700417 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700418 else
Holger Macht66b56822007-08-10 13:10:32 -0700419 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700420
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700421 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800422 * Indicate that the status of the dock station has
423 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700424 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800425 if (num == DOCK_EVENT)
426 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
427
428 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
429 if (dd->ops && dd->ops->uevent)
430 dd->ops->uevent(dd->handle, event, dd->context);
431 if (num != DOCK_EVENT)
432 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400433}
434
435/**
436 * eject_dock - respond to a dock eject request
437 * @ds: the dock station
438 *
439 * This is called after _DCK is called, to execute the dock station's
440 * _EJ0 method.
441 */
442static void eject_dock(struct dock_station *ds)
443{
444 struct acpi_object_list arg_list;
445 union acpi_object arg;
446 acpi_status status;
447 acpi_handle tmp;
448
449 /* all dock devices should have _EJ0, but check anyway */
450 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
451 if (ACPI_FAILURE(status)) {
452 pr_debug("No _EJ0 support for dock device\n");
453 return;
454 }
455
456 arg_list.count = 1;
457 arg_list.pointer = &arg;
458 arg.type = ACPI_TYPE_INTEGER;
459 arg.integer.value = 1;
460
461 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
462 &arg_list, NULL)))
463 pr_debug("Failed to evaluate _EJ0!\n");
464}
465
466/**
467 * handle_dock - handle a dock event
468 * @ds: the dock station
469 * @dock: to dock, or undock - that is the question
470 *
471 * Execute the _DCK method in response to an acpi event
472 */
473static void handle_dock(struct dock_station *ds, int dock)
474{
475 acpi_status status;
476 struct acpi_object_list arg_list;
477 union acpi_object arg;
478 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
479 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400480
481 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
Len Brownc8f7a622006-07-09 17:22:28 -0400482
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400483 printk(KERN_INFO PREFIX "%s - %s\n",
484 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400485
486 /* _DCK method has one argument */
487 arg_list.count = 1;
488 arg_list.pointer = &arg;
489 arg.type = ACPI_TYPE_INTEGER;
490 arg.integer.value = dock;
491 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800492 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Thomas Renninger0a918a92008-10-11 00:15:04 -0400493 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
494 " _DCK\n", (char *)name_buffer.pointer));
495
Len Brownc8f7a622006-07-09 17:22:28 -0400496 kfree(buffer.pointer);
497 kfree(name_buffer.pointer);
498}
499
500static inline void dock(struct dock_station *ds)
501{
502 handle_dock(ds, 1);
503}
504
505static inline void undock(struct dock_station *ds)
506{
507 handle_dock(ds, 0);
508}
509
510static inline void begin_dock(struct dock_station *ds)
511{
512 ds->flags |= DOCK_DOCKING;
513}
514
515static inline void complete_dock(struct dock_station *ds)
516{
517 ds->flags &= ~(DOCK_DOCKING);
518 ds->last_dock_time = jiffies;
519}
520
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700521static inline void begin_undock(struct dock_station *ds)
522{
523 ds->flags |= DOCK_UNDOCKING;
524}
525
526static inline void complete_undock(struct dock_station *ds)
527{
528 ds->flags &= ~(DOCK_UNDOCKING);
529}
530
Shaohua Li406f6922008-08-28 10:03:26 +0800531static void dock_lock(struct dock_station *ds, int lock)
532{
533 struct acpi_object_list arg_list;
534 union acpi_object arg;
535 acpi_status status;
536
537 arg_list.count = 1;
538 arg_list.pointer = &arg;
539 arg.type = ACPI_TYPE_INTEGER;
540 arg.integer.value = !!lock;
541 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
542 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
543 if (lock)
544 printk(KERN_WARNING PREFIX "Locking device failed\n");
545 else
546 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
547 }
548}
549
Len Brownc8f7a622006-07-09 17:22:28 -0400550/**
551 * dock_in_progress - see if we are in the middle of handling a dock event
552 * @ds: the dock station
553 *
554 * Sometimes while docking, false dock events can be sent to the driver
555 * because good connections aren't made or some other reason. Ignore these
556 * if we are in the middle of doing something.
557 */
558static int dock_in_progress(struct dock_station *ds)
559{
560 if ((ds->flags & DOCK_DOCKING) ||
561 time_before(jiffies, (ds->last_dock_time + HZ)))
562 return 1;
563 return 0;
564}
565
566/**
567 * register_dock_notifier - add yourself to the dock notifier list
568 * @nb: the callers notifier block
569 *
570 * If a driver wishes to be notified about dock events, they can
571 * use this function to put a notifier block on the dock notifier list.
572 * this notifier call chain will be called after a dock event, but
573 * before hotplugging any new devices.
574 */
575int register_dock_notifier(struct notifier_block *nb)
576{
Shaohua Lidb350b02008-08-28 10:03:58 +0800577 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800578 return -ENODEV;
579
Len Brownc8f7a622006-07-09 17:22:28 -0400580 return atomic_notifier_chain_register(&dock_notifier_list, nb);
581}
582
583EXPORT_SYMBOL_GPL(register_dock_notifier);
584
585/**
586 * unregister_dock_notifier - remove yourself from the dock notifier list
587 * @nb: the callers notifier block
588 */
589void unregister_dock_notifier(struct notifier_block *nb)
590{
Shaohua Lidb350b02008-08-28 10:03:58 +0800591 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800592 return;
593
Len Brownc8f7a622006-07-09 17:22:28 -0400594 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
595}
596
597EXPORT_SYMBOL_GPL(unregister_dock_notifier);
598
599/**
600 * register_hotplug_dock_device - register a hotplug function
601 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800602 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400603 * @context: device specific data
604 *
605 * If a driver would like to perform a hotplug operation after a dock
606 * event, they can register an acpi_notifiy_handler to be called by
607 * the dock driver after _DCK is executed.
608 */
609int
Shaohua Li1253f7a2008-08-28 10:06:16 +0800610register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
Len Brownc8f7a622006-07-09 17:22:28 -0400611 void *context)
612{
613 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800614 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800615 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400616
Shaohua Lidb350b02008-08-28 10:03:58 +0800617 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400618 return -ENODEV;
619
620 /*
621 * make sure this handle is for a device dependent on the dock,
622 * this would include the dock station itself
623 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800624 list_for_each_entry(dock_station, &dock_stations, sibiling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800625 /*
626 * An ATA bay can be in a dock and itself can be ejected
627 * seperately, so there are two 'dock stations' which need the
628 * ops
629 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800630 dd = find_dock_dependent_device(dock_station, handle);
631 if (dd) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800632 dd->ops = ops;
Shaohua Lidb350b02008-08-28 10:03:58 +0800633 dd->context = context;
634 dock_add_hotplug_device(dock_station, dd);
Shaohua Li61b83692008-08-28 10:07:14 +0800635 ret = 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800636 }
Len Brownc8f7a622006-07-09 17:22:28 -0400637 }
638
Shaohua Li61b83692008-08-28 10:07:14 +0800639 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400640}
641
642EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
643
644/**
645 * unregister_hotplug_dock_device - remove yourself from the hotplug list
646 * @handle: the acpi handle of the device
647 */
648void unregister_hotplug_dock_device(acpi_handle handle)
649{
650 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800651 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400652
Shaohua Lidb350b02008-08-28 10:03:58 +0800653 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400654 return;
655
Shaohua Lidb350b02008-08-28 10:03:58 +0800656 list_for_each_entry(dock_station, &dock_stations, sibiling) {
657 dd = find_dock_dependent_device(dock_station, handle);
658 if (dd)
659 dock_del_hotplug_device(dock_station, dd);
660 }
Len Brownc8f7a622006-07-09 17:22:28 -0400661}
662
663EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
664
665/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800666 * handle_eject_request - handle an undock request checking for error conditions
667 *
668 * Check to make sure the dock device is still present, then undock and
669 * hotremove all the devices that may need removing.
670 */
671static int handle_eject_request(struct dock_station *ds, u32 event)
672{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800673 if (dock_in_progress(ds))
674 return -EBUSY;
675
676 /*
677 * here we need to generate the undock
678 * event prior to actually doing the undock
679 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200680 * Also, even send the dock event if the
681 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800682 */
683 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200684
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800685 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
686 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800687 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800688 eject_dock(ds);
689 if (dock_present(ds)) {
690 printk(KERN_ERR PREFIX "Unable to undock!\n");
691 return -EBUSY;
692 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700693 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800694 return 0;
695}
696
697/**
Len Brownc8f7a622006-07-09 17:22:28 -0400698 * dock_notify - act upon an acpi dock notification
699 * @handle: the dock station handle
700 * @event: the acpi event
701 * @data: our driver data struct
702 *
703 * If we are notified to dock, then check to see if the dock is
704 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800705 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400706 */
707static void dock_notify(acpi_handle handle, u32 event, void *data)
708{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200709 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800710 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800711 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400712
Shaohua Lidb350b02008-08-28 10:03:58 +0800713 /*
714 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
715 * is sent and _DCK is present, it is assumed to mean an undock
716 * request.
717 */
718 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
719 event = ACPI_NOTIFY_EJECT_REQUEST;
720
721 /*
722 * dock station: BUS_CHECK - docked or surprise removal
723 * DEVICE_CHECK - undocked
724 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
725 *
726 * To simplify event handling, dock dependent device handler always
727 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
728 * ACPI_NOTIFY_EJECT_REQUEST for removal
729 */
Len Brownc8f7a622006-07-09 17:22:28 -0400730 switch (event) {
731 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800732 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800733 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
734 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400735 begin_dock(ds);
736 dock(ds);
737 if (!dock_present(ds)) {
738 printk(KERN_ERR PREFIX "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800739 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400740 break;
741 }
742 atomic_notifier_call_chain(&dock_notifier_list,
743 event, NULL);
744 hotplug_dock_devices(ds, event);
745 complete_dock(ds);
746 dock_event(ds, event, DOCK_EVENT);
Shaohua Li406f6922008-08-28 10:03:26 +0800747 dock_lock(ds, 1);
Shaohua Lidb350b02008-08-28 10:03:58 +0800748 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400749 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800750 if (dock_present(ds) || dock_in_progress(ds))
751 break;
752 /* This is a surprise removal */
753 surprise_removal = 1;
754 event = ACPI_NOTIFY_EJECT_REQUEST;
755 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400756 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700757 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800758 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
759 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700760 handle_eject_request(ds, event);
761 else
762 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400763 break;
764 default:
765 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
766 }
767}
768
Zhang Rui19cd8472008-08-28 10:05:06 +0800769struct dock_data {
770 acpi_handle handle;
771 unsigned long event;
772 struct dock_station *ds;
773};
774
775static void acpi_dock_deferred_cb(void *context)
776{
777 struct dock_data *data = (struct dock_data *)context;
778
779 dock_notify(data->handle, data->event, data->ds);
780 kfree(data);
781}
782
Shaohua Li6bd00a62008-08-28 10:04:29 +0800783static int acpi_dock_notifier_call(struct notifier_block *this,
784 unsigned long event, void *data)
785{
786 struct dock_station *dock_station;
787 acpi_handle handle = (acpi_handle)data;
788
789 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
790 && event != ACPI_NOTIFY_EJECT_REQUEST)
791 return 0;
792 list_for_each_entry(dock_station, &dock_stations, sibiling) {
793 if (dock_station->handle == handle) {
Zhang Rui19cd8472008-08-28 10:05:06 +0800794 struct dock_data *dock_data;
795
796 dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
797 if (!dock_data)
798 return 0;
799 dock_data->handle = handle;
800 dock_data->event = event;
801 dock_data->ds = dock_station;
802 acpi_os_hotplug_execute(acpi_dock_deferred_cb,
803 dock_data);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800804 return 0 ;
805 }
806 }
807 return 0;
808}
809
810static struct notifier_block dock_acpi_notifier = {
811 .notifier_call = acpi_dock_notifier_call,
812};
813
Len Brownc8f7a622006-07-09 17:22:28 -0400814/**
815 * find_dock_devices - find devices on the dock station
816 * @handle: the handle of the device we are examining
817 * @lvl: unused
818 * @context: the dock station private data
819 * @rv: unused
820 *
821 * This function is called by acpi_walk_namespace. It will
822 * check to see if an object has an _EJD method. If it does, then it
823 * will see if it is dependent on the dock station.
824 */
825static acpi_status
826find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
827{
828 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500829 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200830 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400831 struct dock_dependent_device *dd;
832
833 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500834 if (ACPI_FAILURE(status)) {
835 /* try the parent device as well */
836 status = acpi_get_parent(handle, &parent);
837 if (ACPI_FAILURE(status))
838 goto fdd_out;
839 /* see if parent is dependent on dock */
840 status = acpi_bus_get_ejd(parent, &tmp);
841 if (ACPI_FAILURE(status))
842 goto fdd_out;
843 }
Len Brownc8f7a622006-07-09 17:22:28 -0400844
845 if (tmp == ds->handle) {
846 dd = alloc_dock_dependent_device(handle);
847 if (dd)
848 add_dock_dependent_device(ds, dd);
849 }
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500850fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400851 return AE_OK;
852}
853
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800854/*
855 * show_docked - read method for "docked" file in sysfs
856 */
857static ssize_t show_docked(struct device *dev,
858 struct device_attribute *attr, char *buf)
859{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100860 struct acpi_device *tmp;
861
Shaohua Lidb350b02008-08-28 10:03:58 +0800862 struct dock_station *dock_station = *((struct dock_station **)
863 dev->platform_data);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800864
Holger Machtfc5a9f82009-01-20 12:18:24 +0100865 if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
866 return snprintf(buf, PAGE_SIZE, "1\n");
867 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800868}
Adrian Bunke5685b92007-10-24 18:24:42 +0200869static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800870
871/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700872 * show_flags - read method for flags file in sysfs
873 */
874static ssize_t show_flags(struct device *dev,
875 struct device_attribute *attr, char *buf)
876{
Shaohua Lidb350b02008-08-28 10:03:58 +0800877 struct dock_station *dock_station = *((struct dock_station **)
878 dev->platform_data);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700879 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
880
881}
Adrian Bunke5685b92007-10-24 18:24:42 +0200882static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700883
884/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800885 * write_undock - write method for "undock" file in sysfs
886 */
887static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
888 const char *buf, size_t count)
889{
890 int ret;
Shaohua Lidb350b02008-08-28 10:03:58 +0800891 struct dock_station *dock_station = *((struct dock_station **)
892 dev->platform_data);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800893
894 if (!count)
895 return -EINVAL;
896
Holger Macht9171f832008-03-12 01:07:27 +0100897 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800898 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
899 return ret ? ret: count;
900}
Adrian Bunke5685b92007-10-24 18:24:42 +0200901static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800902
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800903/*
904 * show_dock_uid - read method for "uid" file in sysfs
905 */
906static ssize_t show_dock_uid(struct device *dev,
907 struct device_attribute *attr, char *buf)
908{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400909 unsigned long long lbuf;
Shaohua Lidb350b02008-08-28 10:03:58 +0800910 struct dock_station *dock_station = *((struct dock_station **)
911 dev->platform_data);
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700912 acpi_status status = acpi_evaluate_integer(dock_station->handle,
913 "_UID", NULL, &lbuf);
914 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800915 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700916
Matthew Wilcox27663c52008-10-10 02:22:59 -0400917 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800918}
Adrian Bunke5685b92007-10-24 18:24:42 +0200919static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800920
Shaohua Li8652b002008-08-28 10:07:45 +0800921static ssize_t show_dock_type(struct device *dev,
922 struct device_attribute *attr, char *buf)
923{
924 struct dock_station *dock_station = *((struct dock_station **)
925 dev->platform_data);
926 char *type;
927
928 if (dock_station->flags & DOCK_IS_DOCK)
929 type = "dock_station";
930 else if (dock_station->flags & DOCK_IS_ATA)
931 type = "ata_bay";
932 else if (dock_station->flags & DOCK_IS_BAT)
933 type = "battery_bay";
934 else
935 type = "unknown";
936
937 return snprintf(buf, PAGE_SIZE, "%s\n", type);
938}
939static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
940
Len Brownc8f7a622006-07-09 17:22:28 -0400941/**
942 * dock_add - add a new dock station
943 * @handle: the dock station handle
944 *
945 * allocated and initialize a new dock station device. Find all devices
946 * that are on the dock station, and register for dock event notifications.
947 */
948static int dock_add(acpi_handle handle)
949{
950 int ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400951 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800952 struct dock_station *dock_station;
953 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -0400954
955 /* allocate & initialize the dock_station private data */
956 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
957 if (!dock_station)
958 return -ENOMEM;
959 dock_station->handle = handle;
960 dock_station->last_dock_time = jiffies - HZ;
961 INIT_LIST_HEAD(&dock_station->dependent_devices);
962 INIT_LIST_HEAD(&dock_station->hotplug_devices);
Shaohua Lidb350b02008-08-28 10:03:58 +0800963 INIT_LIST_HEAD(&dock_station->sibiling);
Len Brownc8f7a622006-07-09 17:22:28 -0400964 spin_lock_init(&dock_station->dd_lock);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800965 mutex_init(&dock_station->hp_lock);
Kristen Accardi07a186842006-07-10 14:19:15 -0400966 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Len Brownc8f7a622006-07-09 17:22:28 -0400967
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800968 /* initialize platform device stuff */
Shaohua Lidb350b02008-08-28 10:03:58 +0800969 dock_station->dock_device =
970 platform_device_register_simple(dock_device_name,
971 dock_station_count, NULL, 0);
972 dock_device = dock_station->dock_device;
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700973 if (IS_ERR(dock_device)) {
974 kfree(dock_station);
975 dock_station = NULL;
976 return PTR_ERR(dock_device);
977 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800978 platform_device_add_data(dock_device, &dock_station,
979 sizeof(struct dock_station *));
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700980
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700981 /* we want the dock device to send uevents */
Ming Leif67f1292009-03-01 21:10:49 +0800982 dev_set_uevent_suppress(&dock_device->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700983
Shaohua Lidb350b02008-08-28 10:03:58 +0800984 if (is_dock(handle))
985 dock_station->flags |= DOCK_IS_DOCK;
986 if (is_ata(handle))
987 dock_station->flags |= DOCK_IS_ATA;
988 if (is_battery(handle))
989 dock_station->flags |= DOCK_IS_BAT;
990
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700991 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800992 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +0100993 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700994 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800995 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -0700996 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800997 return ret;
998 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700999 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -08001000 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +01001001 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001002 device_remove_file(&dock_device->dev, &dev_attr_docked);
1003 platform_device_unregister(dock_device);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -08001004 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001005 dock_station = NULL;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -08001006 return ret;
1007 }
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001008 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -08001009 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +01001010 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001011 device_remove_file(&dock_device->dev, &dev_attr_docked);
1012 device_remove_file(&dock_device->dev, &dev_attr_undock);
1013 platform_device_unregister(dock_device);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -08001014 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001015 dock_station = NULL;
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -08001016 return ret;
1017 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001018 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
1019 if (ret) {
Frank Seidel4d939152009-02-04 17:03:07 +01001020 printk(KERN_ERR "Error %d adding sysfs file\n", ret);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001021 device_remove_file(&dock_device->dev, &dev_attr_docked);
1022 device_remove_file(&dock_device->dev, &dev_attr_undock);
1023 device_remove_file(&dock_device->dev, &dev_attr_uid);
1024 platform_device_unregister(dock_device);
1025 kfree(dock_station);
1026 dock_station = NULL;
1027 return ret;
1028 }
Shaohua Li8652b002008-08-28 10:07:45 +08001029 ret = device_create_file(&dock_device->dev, &dev_attr_type);
1030 if (ret)
1031 printk(KERN_ERR"Error %d adding sysfs file\n", ret);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001032
Len Brownc8f7a622006-07-09 17:22:28 -04001033 /* Find dependent devices */
1034 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1035 ACPI_UINT32_MAX, find_dock_devices, dock_station,
1036 NULL);
1037
1038 /* add the dock station as a device dependent on itself */
1039 dd = alloc_dock_dependent_device(handle);
1040 if (!dd) {
1041 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001042 dock_station = NULL;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001043 ret = -ENOMEM;
1044 goto dock_add_err_unregister;
Len Brownc8f7a622006-07-09 17:22:28 -04001045 }
1046 add_dock_dependent_device(dock_station, dd);
1047
Shaohua Lidb350b02008-08-28 10:03:58 +08001048 dock_station_count++;
1049 list_add(&dock_station->sibiling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -04001050 return 0;
1051
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001052dock_add_err_unregister:
Shaohua Li8652b002008-08-28 10:07:45 +08001053 device_remove_file(&dock_device->dev, &dev_attr_type);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001054 device_remove_file(&dock_device->dev, &dev_attr_docked);
1055 device_remove_file(&dock_device->dev, &dev_attr_undock);
1056 device_remove_file(&dock_device->dev, &dev_attr_uid);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001057 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001058 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001059 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001060 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -04001061 return ret;
1062}
1063
1064/**
1065 * dock_remove - free up resources related to the dock station
1066 */
Shaohua Lidb350b02008-08-28 10:03:58 +08001067static int dock_remove(struct dock_station *dock_station)
Len Brownc8f7a622006-07-09 17:22:28 -04001068{
1069 struct dock_dependent_device *dd, *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +08001070 struct platform_device *dock_device = dock_station->dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -04001071
Shaohua Lidb350b02008-08-28 10:03:58 +08001072 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -04001073 return 0;
1074
1075 /* remove dependent devices */
1076 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1077 list)
1078 kfree(dd);
1079
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001080 /* cleanup sysfs */
Shaohua Li8652b002008-08-28 10:07:45 +08001081 device_remove_file(&dock_device->dev, &dev_attr_type);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001082 device_remove_file(&dock_device->dev, &dev_attr_docked);
1083 device_remove_file(&dock_device->dev, &dev_attr_undock);
1084 device_remove_file(&dock_device->dev, &dev_attr_uid);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001085 device_remove_file(&dock_device->dev, &dev_attr_flags);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001086 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001087
Len Brownc8f7a622006-07-09 17:22:28 -04001088 /* free dock station memory */
1089 kfree(dock_station);
Chuck Ebbert22fe4c22007-05-09 15:05:48 -07001090 dock_station = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -04001091 return 0;
1092}
1093
1094/**
1095 * find_dock - look for a dock station
1096 * @handle: acpi handle of a device
1097 * @lvl: unused
1098 * @context: counter of dock stations found
1099 * @rv: unused
1100 *
1101 * This is called by acpi_walk_namespace to look for dock stations.
1102 */
1103static acpi_status
1104find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1105{
Len Brownc8f7a622006-07-09 17:22:28 -04001106 acpi_status status = AE_OK;
1107
1108 if (is_dock(handle)) {
1109 if (dock_add(handle) >= 0) {
Len Brownc8f7a622006-07-09 17:22:28 -04001110 status = AE_CTRL_TERMINATE;
1111 }
1112 }
1113 return status;
1114}
1115
Shaohua Lidb350b02008-08-28 10:03:58 +08001116static acpi_status
1117find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1118{
Shaohua Li61b83692008-08-28 10:07:14 +08001119 /* If bay is a dock, it's already handled */
1120 if (is_ejectable_bay(handle) && !is_dock(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +08001121 dock_add(handle);
1122 return AE_OK;
1123}
1124
Len Brownc8f7a622006-07-09 17:22:28 -04001125static int __init dock_init(void)
1126{
Len Brown816c2ed2008-06-24 22:57:12 -04001127 if (acpi_disabled)
1128 return 0;
1129
Len Brownc8f7a622006-07-09 17:22:28 -04001130 /* look for a dock station */
1131 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Shaohua Lidb350b02008-08-28 10:03:58 +08001132 ACPI_UINT32_MAX, find_dock, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001133
Shaohua Lidb350b02008-08-28 10:03:58 +08001134 /* look for bay */
1135 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1136 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1137 if (!dock_station_count) {
1138 printk(KERN_INFO PREFIX "No dock devices found.\n");
1139 return 0;
1140 }
Len Brownc8f7a622006-07-09 17:22:28 -04001141
Shaohua Li6bd00a62008-08-28 10:04:29 +08001142 register_acpi_bus_notifier(&dock_acpi_notifier);
Shaohua Lidb350b02008-08-28 10:03:58 +08001143 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1144 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -04001145 return 0;
1146}
1147
1148static void __exit dock_exit(void)
1149{
Shaohua Lidb350b02008-08-28 10:03:58 +08001150 struct dock_station *dock_station;
Dan Carpenterf2407292009-04-02 08:29:56 +03001151 struct dock_station *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +08001152
Shaohua Li6bd00a62008-08-28 10:04:29 +08001153 unregister_acpi_bus_notifier(&dock_acpi_notifier);
Dan Carpenterf2407292009-04-02 08:29:56 +03001154 list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibiling)
Shaohua Lidb350b02008-08-28 10:03:58 +08001155 dock_remove(dock_station);
Len Brownc8f7a622006-07-09 17:22:28 -04001156}
1157
Shaohua Lidb350b02008-08-28 10:03:58 +08001158/*
1159 * Must be called before drivers of devices in dock, otherwise we can't know
1160 * which devices are in a dock
1161 */
1162subsys_initcall(dock_init);
Len Brownc8f7a622006-07-09 17:22:28 -04001163module_exit(dock_exit);