blob: cfcd8eeb3b2ab43694d731a929185d3a4d89b803 [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;
53
Frank Seidela340af12007-12-07 13:20:42 +010054static const struct acpi_device_id dock_device_ids[] = {
55 {"LNXDOCK", 0},
56 {"", 0},
57};
58MODULE_DEVICE_TABLE(acpi, dock_device_ids);
59
Len Brownc8f7a622006-07-09 17:22:28 -040060struct dock_station {
61 acpi_handle handle;
62 unsigned long last_dock_time;
63 u32 flags;
64 spinlock_t dd_lock;
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -080065 struct mutex hp_lock;
Len Brownc8f7a622006-07-09 17:22:28 -040066 struct list_head dependent_devices;
67 struct list_head hotplug_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080068
Alex Chiang50d716e2009-10-01 11:59:23 -060069 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080070 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040071};
Shaohua Lidb350b02008-08-28 10:03:58 +080072static LIST_HEAD(dock_stations);
73static int dock_station_count;
Len Brownc8f7a622006-07-09 17:22:28 -040074
75struct dock_dependent_device {
76 struct list_head list;
77 struct list_head hotplug_list;
78 acpi_handle handle;
Shaohua Li1253f7a2008-08-28 10:06:16 +080079 struct acpi_dock_ops *ops;
Len Brownc8f7a622006-07-09 17:22:28 -040080 void *context;
81};
82
83#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070084#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080085#define DOCK_IS_DOCK 0x00000010
86#define DOCK_IS_ATA 0x00000020
87#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070088#define DOCK_EVENT 3
89#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040090
Len Brownc8f7a622006-07-09 17:22:28 -040091/*****************************************************************************
92 * Dock Dependent device functions *
93 *****************************************************************************/
94/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060095 * add_dock_dependent_device - associate a device with the dock station
96 * @ds: The dock station
97 * @handle: handle of the dependent device
Len Brownc8f7a622006-07-09 17:22:28 -040098 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -060099 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -0400100 */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600101static int
102add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400103{
104 struct dock_dependent_device *dd;
105
106 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600107 if (!dd)
108 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -0400109
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600110 dd->handle = handle;
111 INIT_LIST_HEAD(&dd->list);
112 INIT_LIST_HEAD(&dd->hotplug_list);
113
Len Brownc8f7a622006-07-09 17:22:28 -0400114 spin_lock(&ds->dd_lock);
115 list_add_tail(&dd->list, &ds->dependent_devices);
116 spin_unlock(&ds->dd_lock);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600117
118 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400119}
120
121/**
122 * dock_add_hotplug_device - associate a hotplug handler with the dock station
123 * @ds: The dock station
124 * @dd: The dependent device struct
125 *
126 * Add the dependent device to the dock's hotplug device list
127 */
128static void
129dock_add_hotplug_device(struct dock_station *ds,
130 struct dock_dependent_device *dd)
131{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800132 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400133 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800134 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400135}
136
137/**
138 * dock_del_hotplug_device - remove a hotplug handler from the dock station
139 * @ds: The dock station
140 * @dd: the dependent device struct
141 *
142 * Delete the dependent device from the dock's hotplug device list
143 */
144static void
145dock_del_hotplug_device(struct dock_station *ds,
146 struct dock_dependent_device *dd)
147{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800148 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400149 list_del(&dd->hotplug_list);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800150 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400151}
152
153/**
154 * find_dock_dependent_device - get a device dependent on this dock
155 * @ds: the dock station
156 * @handle: the acpi_handle of the device we want
157 *
158 * iterate over the dependent device list for this dock. If the
159 * dependent device matches the handle, return.
160 */
161static struct dock_dependent_device *
162find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
163{
164 struct dock_dependent_device *dd;
165
166 spin_lock(&ds->dd_lock);
167 list_for_each_entry(dd, &ds->dependent_devices, list) {
168 if (handle == dd->handle) {
169 spin_unlock(&ds->dd_lock);
170 return dd;
171 }
172 }
173 spin_unlock(&ds->dd_lock);
174 return NULL;
175}
176
177/*****************************************************************************
178 * Dock functions *
179 *****************************************************************************/
180/**
181 * is_dock - see if a device is a dock station
182 * @handle: acpi handle of the device
183 *
184 * If an acpi object has a _DCK method, then it is by definition a dock
185 * station, so return true.
186 */
187static int is_dock(acpi_handle handle)
188{
189 acpi_status status;
190 acpi_handle tmp;
191
192 status = acpi_get_handle(handle, "_DCK", &tmp);
193 if (ACPI_FAILURE(status))
194 return 0;
195 return 1;
196}
197
Shaohua Lidb350b02008-08-28 10:03:58 +0800198static int is_ejectable(acpi_handle handle)
199{
200 acpi_status status;
201 acpi_handle tmp;
202
203 status = acpi_get_handle(handle, "_EJ0", &tmp);
204 if (ACPI_FAILURE(status))
205 return 0;
206 return 1;
207}
208
209static int is_ata(acpi_handle handle)
210{
211 acpi_handle tmp;
212
213 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
214 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
215 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
216 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
217 return 1;
218
219 return 0;
220}
221
222static int is_battery(acpi_handle handle)
223{
224 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800225 int ret = 1;
226
Bob Moore15b8dd52009-06-29 13:39:29 +0800227 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800228 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800229 if (!(info->valid & ACPI_VALID_HID))
230 ret = 0;
231 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800232 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800233
Bob Moore15b8dd52009-06-29 13:39:29 +0800234 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800235 return ret;
236}
237
238static int is_ejectable_bay(acpi_handle handle)
239{
240 acpi_handle phandle;
241 if (!is_ejectable(handle))
242 return 0;
243 if (is_battery(handle) || is_ata(handle))
244 return 1;
245 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
246 return 1;
247 return 0;
248}
249
Len Brownc8f7a622006-07-09 17:22:28 -0400250/**
251 * is_dock_device - see if a device is on a dock station
252 * @handle: acpi handle of the device
253 *
254 * If this device is either the dock station itself,
255 * or is a device dependent on the dock station, then it
256 * is a dock device
257 */
258int is_dock_device(acpi_handle handle)
259{
Shaohua Lidb350b02008-08-28 10:03:58 +0800260 struct dock_station *dock_station;
261
262 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400263 return 0;
264
Shaohua Lidb350b02008-08-28 10:03:58 +0800265 if (is_dock(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400266 return 1;
Alex Chiang50d716e2009-10-01 11:59:23 -0600267 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800268 if (find_dock_dependent_device(dock_station, handle))
269 return 1;
270 }
Len Brownc8f7a622006-07-09 17:22:28 -0400271
272 return 0;
273}
274
275EXPORT_SYMBOL_GPL(is_dock_device);
276
277/**
278 * dock_present - see if the dock station is present.
279 * @ds: the dock station
280 *
281 * execute the _STA method. note that present does not
282 * imply that we are docked.
283 */
284static int dock_present(struct dock_station *ds)
285{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400286 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400287 acpi_status status;
288
289 if (ds) {
290 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
291 if (ACPI_SUCCESS(status) && sta)
292 return 1;
293 }
294 return 0;
295}
296
297
298
299/**
300 * dock_create_acpi_device - add new devices to acpi
301 * @handle - handle of the device to add
302 *
303 * This function will create a new acpi_device for the given
304 * handle if one does not exist already. This should cause
305 * acpi to scan for drivers for the given devices, and call
306 * matching driver's add routine.
307 *
308 * Returns a pointer to the acpi_device corresponding to the handle.
309 */
310static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
311{
312 struct acpi_device *device = NULL;
313 struct acpi_device *parent_device;
314 acpi_handle parent;
315 int ret;
316
317 if (acpi_bus_get_device(handle, &device)) {
318 /*
319 * no device created for this object,
320 * so we should create one.
321 */
322 acpi_get_parent(handle, &parent);
323 if (acpi_bus_get_device(parent, &parent_device))
324 parent_device = NULL;
325
326 ret = acpi_bus_add(&device, parent_device, handle,
327 ACPI_BUS_TYPE_DEVICE);
328 if (ret) {
329 pr_debug("error adding bus, %x\n",
330 -ret);
331 return NULL;
332 }
333 }
334 return device;
335}
336
337/**
338 * dock_remove_acpi_device - remove the acpi_device struct from acpi
339 * @handle - the handle of the device to remove
340 *
341 * Tell acpi to remove the acpi_device. This should cause any loaded
342 * driver to have it's remove routine called.
343 */
344static void dock_remove_acpi_device(acpi_handle handle)
345{
346 struct acpi_device *device;
347 int ret;
348
349 if (!acpi_bus_get_device(handle, &device)) {
350 ret = acpi_bus_trim(device, 1);
351 if (ret)
352 pr_debug("error removing bus, %x\n", -ret);
353 }
354}
355
356
357/**
358 * hotplug_dock_devices - insert or remove devices on the dock station
359 * @ds: the dock station
360 * @event: either bus check or eject request
361 *
362 * Some devices on the dock station need to have drivers called
363 * to perform hotplug operations after a dock event has occurred.
364 * Traverse the list of dock devices that have registered a
365 * hotplug handler, and call the handler.
366 */
367static void hotplug_dock_devices(struct dock_station *ds, u32 event)
368{
369 struct dock_dependent_device *dd;
370
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800371 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400372
373 /*
374 * First call driver specific hotplug functions
375 */
376 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800377 if (dd->ops && dd->ops->handler)
378 dd->ops->handler(dd->handle, event, dd->context);
Len Brownc8f7a622006-07-09 17:22:28 -0400379 }
380
381 /*
382 * Now make sure that an acpi_device is created for each
383 * dependent device, or removed if this is an eject request.
384 * This will cause acpi_drivers to be stopped/started if they
385 * exist
386 */
387 list_for_each_entry(dd, &ds->dependent_devices, list) {
388 if (event == ACPI_NOTIFY_EJECT_REQUEST)
389 dock_remove_acpi_device(dd->handle);
390 else
391 dock_create_acpi_device(dd->handle);
392 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800393 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400394}
395
396static void dock_event(struct dock_station *ds, u32 event, int num)
397{
Shaohua Lidb350b02008-08-28 10:03:58 +0800398 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700399 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700400 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800401 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700402
403 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700404 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700405 else
Holger Macht66b56822007-08-10 13:10:32 -0700406 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700407
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700408 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800409 * Indicate that the status of the dock station has
410 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700411 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800412 if (num == DOCK_EVENT)
413 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
414
415 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
416 if (dd->ops && dd->ops->uevent)
417 dd->ops->uevent(dd->handle, event, dd->context);
418 if (num != DOCK_EVENT)
419 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400420}
421
422/**
423 * eject_dock - respond to a dock eject request
424 * @ds: the dock station
425 *
426 * This is called after _DCK is called, to execute the dock station's
427 * _EJ0 method.
428 */
429static void eject_dock(struct dock_station *ds)
430{
431 struct acpi_object_list arg_list;
432 union acpi_object arg;
433 acpi_status status;
434 acpi_handle tmp;
435
436 /* all dock devices should have _EJ0, but check anyway */
437 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
438 if (ACPI_FAILURE(status)) {
439 pr_debug("No _EJ0 support for dock device\n");
440 return;
441 }
442
443 arg_list.count = 1;
444 arg_list.pointer = &arg;
445 arg.type = ACPI_TYPE_INTEGER;
446 arg.integer.value = 1;
447
448 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
449 &arg_list, NULL)))
450 pr_debug("Failed to evaluate _EJ0!\n");
451}
452
453/**
454 * handle_dock - handle a dock event
455 * @ds: the dock station
456 * @dock: to dock, or undock - that is the question
457 *
458 * Execute the _DCK method in response to an acpi event
459 */
460static void handle_dock(struct dock_station *ds, int dock)
461{
462 acpi_status status;
463 struct acpi_object_list arg_list;
464 union acpi_object arg;
465 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
466 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400467
468 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
Len Brownc8f7a622006-07-09 17:22:28 -0400469
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400470 printk(KERN_INFO PREFIX "%s - %s\n",
471 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400472
473 /* _DCK method has one argument */
474 arg_list.count = 1;
475 arg_list.pointer = &arg;
476 arg.type = ACPI_TYPE_INTEGER;
477 arg.integer.value = dock;
478 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800479 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Thomas Renninger0a918a92008-10-11 00:15:04 -0400480 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
481 " _DCK\n", (char *)name_buffer.pointer));
482
Len Brownc8f7a622006-07-09 17:22:28 -0400483 kfree(buffer.pointer);
484 kfree(name_buffer.pointer);
485}
486
487static inline void dock(struct dock_station *ds)
488{
489 handle_dock(ds, 1);
490}
491
492static inline void undock(struct dock_station *ds)
493{
494 handle_dock(ds, 0);
495}
496
497static inline void begin_dock(struct dock_station *ds)
498{
499 ds->flags |= DOCK_DOCKING;
500}
501
502static inline void complete_dock(struct dock_station *ds)
503{
504 ds->flags &= ~(DOCK_DOCKING);
505 ds->last_dock_time = jiffies;
506}
507
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700508static inline void begin_undock(struct dock_station *ds)
509{
510 ds->flags |= DOCK_UNDOCKING;
511}
512
513static inline void complete_undock(struct dock_station *ds)
514{
515 ds->flags &= ~(DOCK_UNDOCKING);
516}
517
Shaohua Li406f6922008-08-28 10:03:26 +0800518static void dock_lock(struct dock_station *ds, int lock)
519{
520 struct acpi_object_list arg_list;
521 union acpi_object arg;
522 acpi_status status;
523
524 arg_list.count = 1;
525 arg_list.pointer = &arg;
526 arg.type = ACPI_TYPE_INTEGER;
527 arg.integer.value = !!lock;
528 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
529 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
530 if (lock)
531 printk(KERN_WARNING PREFIX "Locking device failed\n");
532 else
533 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
534 }
535}
536
Len Brownc8f7a622006-07-09 17:22:28 -0400537/**
538 * dock_in_progress - see if we are in the middle of handling a dock event
539 * @ds: the dock station
540 *
541 * Sometimes while docking, false dock events can be sent to the driver
542 * because good connections aren't made or some other reason. Ignore these
543 * if we are in the middle of doing something.
544 */
545static int dock_in_progress(struct dock_station *ds)
546{
547 if ((ds->flags & DOCK_DOCKING) ||
548 time_before(jiffies, (ds->last_dock_time + HZ)))
549 return 1;
550 return 0;
551}
552
553/**
554 * register_dock_notifier - add yourself to the dock notifier list
555 * @nb: the callers notifier block
556 *
557 * If a driver wishes to be notified about dock events, they can
558 * use this function to put a notifier block on the dock notifier list.
559 * this notifier call chain will be called after a dock event, but
560 * before hotplugging any new devices.
561 */
562int register_dock_notifier(struct notifier_block *nb)
563{
Shaohua Lidb350b02008-08-28 10:03:58 +0800564 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800565 return -ENODEV;
566
Len Brownc8f7a622006-07-09 17:22:28 -0400567 return atomic_notifier_chain_register(&dock_notifier_list, nb);
568}
569
570EXPORT_SYMBOL_GPL(register_dock_notifier);
571
572/**
573 * unregister_dock_notifier - remove yourself from the dock notifier list
574 * @nb: the callers notifier block
575 */
576void unregister_dock_notifier(struct notifier_block *nb)
577{
Shaohua Lidb350b02008-08-28 10:03:58 +0800578 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800579 return;
580
Len Brownc8f7a622006-07-09 17:22:28 -0400581 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
582}
583
584EXPORT_SYMBOL_GPL(unregister_dock_notifier);
585
586/**
587 * register_hotplug_dock_device - register a hotplug function
588 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800589 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400590 * @context: device specific data
591 *
592 * If a driver would like to perform a hotplug operation after a dock
593 * event, they can register an acpi_notifiy_handler to be called by
594 * the dock driver after _DCK is executed.
595 */
596int
Shaohua Li1253f7a2008-08-28 10:06:16 +0800597register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
Len Brownc8f7a622006-07-09 17:22:28 -0400598 void *context)
599{
600 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800601 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800602 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400603
Shaohua Lidb350b02008-08-28 10:03:58 +0800604 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400605 return -ENODEV;
606
607 /*
608 * make sure this handle is for a device dependent on the dock,
609 * this would include the dock station itself
610 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600611 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800612 /*
613 * An ATA bay can be in a dock and itself can be ejected
614 * seperately, so there are two 'dock stations' which need the
615 * ops
616 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800617 dd = find_dock_dependent_device(dock_station, handle);
618 if (dd) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800619 dd->ops = ops;
Shaohua Lidb350b02008-08-28 10:03:58 +0800620 dd->context = context;
621 dock_add_hotplug_device(dock_station, dd);
Shaohua Li61b83692008-08-28 10:07:14 +0800622 ret = 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800623 }
Len Brownc8f7a622006-07-09 17:22:28 -0400624 }
625
Shaohua Li61b83692008-08-28 10:07:14 +0800626 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400627}
628
629EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
630
631/**
632 * unregister_hotplug_dock_device - remove yourself from the hotplug list
633 * @handle: the acpi handle of the device
634 */
635void unregister_hotplug_dock_device(acpi_handle handle)
636{
637 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800638 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400639
Shaohua Lidb350b02008-08-28 10:03:58 +0800640 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400641 return;
642
Alex Chiang50d716e2009-10-01 11:59:23 -0600643 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800644 dd = find_dock_dependent_device(dock_station, handle);
645 if (dd)
646 dock_del_hotplug_device(dock_station, dd);
647 }
Len Brownc8f7a622006-07-09 17:22:28 -0400648}
649
650EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
651
652/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800653 * handle_eject_request - handle an undock request checking for error conditions
654 *
655 * Check to make sure the dock device is still present, then undock and
656 * hotremove all the devices that may need removing.
657 */
658static int handle_eject_request(struct dock_station *ds, u32 event)
659{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800660 if (dock_in_progress(ds))
661 return -EBUSY;
662
663 /*
664 * here we need to generate the undock
665 * event prior to actually doing the undock
666 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200667 * Also, even send the dock event if the
668 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800669 */
670 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200671
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800672 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
673 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800674 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800675 eject_dock(ds);
676 if (dock_present(ds)) {
677 printk(KERN_ERR PREFIX "Unable to undock!\n");
678 return -EBUSY;
679 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700680 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800681 return 0;
682}
683
684/**
Len Brownc8f7a622006-07-09 17:22:28 -0400685 * dock_notify - act upon an acpi dock notification
686 * @handle: the dock station handle
687 * @event: the acpi event
688 * @data: our driver data struct
689 *
690 * If we are notified to dock, then check to see if the dock is
691 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800692 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400693 */
694static void dock_notify(acpi_handle handle, u32 event, void *data)
695{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200696 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800697 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800698 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400699
Shaohua Lidb350b02008-08-28 10:03:58 +0800700 /*
701 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
702 * is sent and _DCK is present, it is assumed to mean an undock
703 * request.
704 */
705 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
706 event = ACPI_NOTIFY_EJECT_REQUEST;
707
708 /*
709 * dock station: BUS_CHECK - docked or surprise removal
710 * DEVICE_CHECK - undocked
711 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
712 *
713 * To simplify event handling, dock dependent device handler always
714 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
715 * ACPI_NOTIFY_EJECT_REQUEST for removal
716 */
Len Brownc8f7a622006-07-09 17:22:28 -0400717 switch (event) {
718 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800719 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800720 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
721 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400722 begin_dock(ds);
723 dock(ds);
724 if (!dock_present(ds)) {
725 printk(KERN_ERR PREFIX "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800726 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400727 break;
728 }
729 atomic_notifier_call_chain(&dock_notifier_list,
730 event, NULL);
731 hotplug_dock_devices(ds, event);
732 complete_dock(ds);
733 dock_event(ds, event, DOCK_EVENT);
Shaohua Li406f6922008-08-28 10:03:26 +0800734 dock_lock(ds, 1);
Shaohua Lidb350b02008-08-28 10:03:58 +0800735 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400736 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800737 if (dock_present(ds) || dock_in_progress(ds))
738 break;
739 /* This is a surprise removal */
740 surprise_removal = 1;
741 event = ACPI_NOTIFY_EJECT_REQUEST;
742 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400743 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700744 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800745 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
746 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700747 handle_eject_request(ds, event);
748 else
749 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400750 break;
751 default:
752 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
753 }
754}
755
Zhang Rui19cd8472008-08-28 10:05:06 +0800756struct dock_data {
757 acpi_handle handle;
758 unsigned long event;
759 struct dock_station *ds;
760};
761
762static void acpi_dock_deferred_cb(void *context)
763{
764 struct dock_data *data = (struct dock_data *)context;
765
766 dock_notify(data->handle, data->event, data->ds);
767 kfree(data);
768}
769
Shaohua Li6bd00a62008-08-28 10:04:29 +0800770static int acpi_dock_notifier_call(struct notifier_block *this,
771 unsigned long event, void *data)
772{
773 struct dock_station *dock_station;
774 acpi_handle handle = (acpi_handle)data;
775
776 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
777 && event != ACPI_NOTIFY_EJECT_REQUEST)
778 return 0;
Alex Chiang50d716e2009-10-01 11:59:23 -0600779 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li6bd00a62008-08-28 10:04:29 +0800780 if (dock_station->handle == handle) {
Zhang Rui19cd8472008-08-28 10:05:06 +0800781 struct dock_data *dock_data;
782
783 dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
784 if (!dock_data)
785 return 0;
786 dock_data->handle = handle;
787 dock_data->event = event;
788 dock_data->ds = dock_station;
789 acpi_os_hotplug_execute(acpi_dock_deferred_cb,
790 dock_data);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800791 return 0 ;
792 }
793 }
794 return 0;
795}
796
797static struct notifier_block dock_acpi_notifier = {
798 .notifier_call = acpi_dock_notifier_call,
799};
800
Len Brownc8f7a622006-07-09 17:22:28 -0400801/**
802 * find_dock_devices - find devices on the dock station
803 * @handle: the handle of the device we are examining
804 * @lvl: unused
805 * @context: the dock station private data
806 * @rv: unused
807 *
808 * This function is called by acpi_walk_namespace. It will
809 * check to see if an object has an _EJD method. If it does, then it
810 * will see if it is dependent on the dock station.
811 */
812static acpi_status
813find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
814{
815 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500816 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200817 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400818
819 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500820 if (ACPI_FAILURE(status)) {
821 /* try the parent device as well */
822 status = acpi_get_parent(handle, &parent);
823 if (ACPI_FAILURE(status))
824 goto fdd_out;
825 /* see if parent is dependent on dock */
826 status = acpi_bus_get_ejd(parent, &tmp);
827 if (ACPI_FAILURE(status))
828 goto fdd_out;
829 }
Len Brownc8f7a622006-07-09 17:22:28 -0400830
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600831 if (tmp == ds->handle)
832 add_dock_dependent_device(ds, handle);
833
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500834fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400835 return AE_OK;
836}
837
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800838/*
839 * show_docked - read method for "docked" file in sysfs
840 */
841static ssize_t show_docked(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100844 struct acpi_device *tmp;
845
Alex Chiangfe06fba2009-10-19 15:14:45 -0600846 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800847
Holger Machtfc5a9f82009-01-20 12:18:24 +0100848 if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
849 return snprintf(buf, PAGE_SIZE, "1\n");
850 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800851}
Adrian Bunke5685b92007-10-24 18:24:42 +0200852static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800853
854/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700855 * show_flags - read method for flags file in sysfs
856 */
857static ssize_t show_flags(struct device *dev,
858 struct device_attribute *attr, char *buf)
859{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600860 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700861 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
862
863}
Adrian Bunke5685b92007-10-24 18:24:42 +0200864static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700865
866/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800867 * write_undock - write method for "undock" file in sysfs
868 */
869static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
870 const char *buf, size_t count)
871{
872 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600873 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800874
875 if (!count)
876 return -EINVAL;
877
Holger Macht9171f832008-03-12 01:07:27 +0100878 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800879 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
880 return ret ? ret: count;
881}
Adrian Bunke5685b92007-10-24 18:24:42 +0200882static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800883
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800884/*
885 * show_dock_uid - read method for "uid" file in sysfs
886 */
887static ssize_t show_dock_uid(struct device *dev,
888 struct device_attribute *attr, char *buf)
889{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400890 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600891 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700892 acpi_status status = acpi_evaluate_integer(dock_station->handle,
893 "_UID", NULL, &lbuf);
894 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800895 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700896
Matthew Wilcox27663c52008-10-10 02:22:59 -0400897 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800898}
Adrian Bunke5685b92007-10-24 18:24:42 +0200899static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800900
Shaohua Li8652b002008-08-28 10:07:45 +0800901static ssize_t show_dock_type(struct device *dev,
902 struct device_attribute *attr, char *buf)
903{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600904 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800905 char *type;
906
907 if (dock_station->flags & DOCK_IS_DOCK)
908 type = "dock_station";
909 else if (dock_station->flags & DOCK_IS_ATA)
910 type = "ata_bay";
911 else if (dock_station->flags & DOCK_IS_BAT)
912 type = "battery_bay";
913 else
914 type = "unknown";
915
916 return snprintf(buf, PAGE_SIZE, "%s\n", type);
917}
918static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
919
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600920static struct attribute *dock_attributes[] = {
921 &dev_attr_docked.attr,
922 &dev_attr_flags.attr,
923 &dev_attr_undock.attr,
924 &dev_attr_uid.attr,
925 &dev_attr_type.attr,
926 NULL
927};
928
929static struct attribute_group dock_attribute_group = {
930 .attrs = dock_attributes
931};
932
Len Brownc8f7a622006-07-09 17:22:28 -0400933/**
934 * dock_add - add a new dock station
935 * @handle: the dock station handle
936 *
937 * allocated and initialize a new dock station device. Find all devices
938 * that are on the dock station, and register for dock event notifications.
939 */
940static int dock_add(acpi_handle handle)
941{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600942 int ret, id;
943 struct dock_station ds, *dock_station;
Shaohua Lidb350b02008-08-28 10:03:58 +0800944 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -0400945
Alex Chiangfe06fba2009-10-19 15:14:45 -0600946 id = dock_station_count;
Alex Chiang9751cb72009-10-19 15:14:40 -0600947 dock_device =
Alex Chiangfe06fba2009-10-19 15:14:45 -0600948 platform_device_register_data(NULL, "dock",
949 id, &ds, sizeof(ds));
Alex Chiang9751cb72009-10-19 15:14:40 -0600950 if (IS_ERR(dock_device))
951 return PTR_ERR(dock_device);
952
Alex Chiangfe06fba2009-10-19 15:14:45 -0600953 dock_station = dock_device->dev.platform_data;
Len Brownc8f7a622006-07-09 17:22:28 -0400954 dock_station->handle = handle;
Alex Chiang9751cb72009-10-19 15:14:40 -0600955 dock_station->dock_device = dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -0400956 dock_station->last_dock_time = jiffies - HZ;
957 INIT_LIST_HEAD(&dock_station->dependent_devices);
958 INIT_LIST_HEAD(&dock_station->hotplug_devices);
Alex Chiang50d716e2009-10-01 11:59:23 -0600959 INIT_LIST_HEAD(&dock_station->sibling);
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 Accardi9ef2a9a2007-05-09 15:09:12 -0700964 /* we want the dock device to send uevents */
Ming Leif67f1292009-03-01 21:10:49 +0800965 dev_set_uevent_suppress(&dock_device->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700966
Shaohua Lidb350b02008-08-28 10:03:58 +0800967 if (is_dock(handle))
968 dock_station->flags |= DOCK_IS_DOCK;
969 if (is_ata(handle))
970 dock_station->flags |= DOCK_IS_ATA;
971 if (is_battery(handle))
972 dock_station->flags |= DOCK_IS_BAT;
973
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600974 ret = sysfs_create_group(&dock_device->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800975 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600976 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800977
Len Brownc8f7a622006-07-09 17:22:28 -0400978 /* Find dependent devices */
979 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
980 ACPI_UINT32_MAX, find_dock_devices, dock_station,
981 NULL);
982
983 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600984 ret = add_dock_dependent_device(dock_station, handle);
985 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600986 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400987
Shaohua Lidb350b02008-08-28 10:03:58 +0800988 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600989 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -0400990 return 0;
991
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600992err_rmgroup:
993 sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
994err_unregister:
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -0700995 platform_device_unregister(dock_device);
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600996 printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400997 return ret;
998}
999
1000/**
1001 * dock_remove - free up resources related to the dock station
1002 */
Shaohua Lidb350b02008-08-28 10:03:58 +08001003static int dock_remove(struct dock_station *dock_station)
Len Brownc8f7a622006-07-09 17:22:28 -04001004{
1005 struct dock_dependent_device *dd, *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +08001006 struct platform_device *dock_device = dock_station->dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -04001007
Shaohua Lidb350b02008-08-28 10:03:58 +08001008 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -04001009 return 0;
1010
1011 /* remove dependent devices */
1012 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1013 list)
1014 kfree(dd);
1015
Alex Chiangfe06fba2009-10-19 15:14:45 -06001016 list_del(&dock_station->sibling);
1017
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001018 /* cleanup sysfs */
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001019 sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001020 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001021
Len Brownc8f7a622006-07-09 17:22:28 -04001022 return 0;
1023}
1024
1025/**
1026 * find_dock - look for a dock station
1027 * @handle: acpi handle of a device
1028 * @lvl: unused
1029 * @context: counter of dock stations found
1030 * @rv: unused
1031 *
1032 * This is called by acpi_walk_namespace to look for dock stations.
1033 */
1034static acpi_status
1035find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1036{
Len Brownc8f7a622006-07-09 17:22:28 -04001037 acpi_status status = AE_OK;
1038
1039 if (is_dock(handle)) {
1040 if (dock_add(handle) >= 0) {
Len Brownc8f7a622006-07-09 17:22:28 -04001041 status = AE_CTRL_TERMINATE;
1042 }
1043 }
1044 return status;
1045}
1046
Shaohua Lidb350b02008-08-28 10:03:58 +08001047static acpi_status
1048find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1049{
Shaohua Li61b83692008-08-28 10:07:14 +08001050 /* If bay is a dock, it's already handled */
1051 if (is_ejectable_bay(handle) && !is_dock(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +08001052 dock_add(handle);
1053 return AE_OK;
1054}
1055
Len Brownc8f7a622006-07-09 17:22:28 -04001056static int __init dock_init(void)
1057{
Len Brown816c2ed2008-06-24 22:57:12 -04001058 if (acpi_disabled)
1059 return 0;
1060
Len Brownc8f7a622006-07-09 17:22:28 -04001061 /* look for a dock station */
1062 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Shaohua Lidb350b02008-08-28 10:03:58 +08001063 ACPI_UINT32_MAX, find_dock, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001064
Shaohua Lidb350b02008-08-28 10:03:58 +08001065 /* look for bay */
1066 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1067 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1068 if (!dock_station_count) {
1069 printk(KERN_INFO PREFIX "No dock devices found.\n");
1070 return 0;
1071 }
Len Brownc8f7a622006-07-09 17:22:28 -04001072
Shaohua Li6bd00a62008-08-28 10:04:29 +08001073 register_acpi_bus_notifier(&dock_acpi_notifier);
Shaohua Lidb350b02008-08-28 10:03:58 +08001074 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1075 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -04001076 return 0;
1077}
1078
1079static void __exit dock_exit(void)
1080{
Shaohua Lidb350b02008-08-28 10:03:58 +08001081 struct dock_station *dock_station;
Dan Carpenterf2407292009-04-02 08:29:56 +03001082 struct dock_station *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +08001083
Shaohua Li6bd00a62008-08-28 10:04:29 +08001084 unregister_acpi_bus_notifier(&dock_acpi_notifier);
Alex Chiang50d716e2009-10-01 11:59:23 -06001085 list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +08001086 dock_remove(dock_station);
Len Brownc8f7a622006-07-09 17:22:28 -04001087}
1088
Shaohua Lidb350b02008-08-28 10:03:58 +08001089/*
1090 * Must be called before drivers of devices in dock, otherwise we can't know
1091 * which devices are in a dock
1092 */
1093subsys_initcall(dock_init);
Len Brownc8f7a622006-07-09 17:22:28 -04001094module_exit(dock_exit);