blob: 3fe29e992be8aa2cd324f02696388862319327e2 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Len Brownc8f7a622006-07-09 17:22:28 -040028#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/notifier.h>
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -080031#include <linux/platform_device.h>
Al Viro914e2632006-10-18 13:55:46 -040032#include <linux/jiffies.h>
Randy Dunlap62a6d7f2007-03-26 21:38:49 -080033#include <linux/stddef.h>
Len Brownc8f7a622006-07-09 17:22:28 -040034#include <acpi/acpi_bus.h>
35#include <acpi/acpi_drivers.h>
36
Len Browna192a952009-07-28 16:45:54 -040037#define PREFIX "ACPI: "
38
Len Brown7cda93e2007-02-12 23:50:02 -050039#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a622006-07-09 17:22:28 -040040
Len Brownf52fd662007-02-12 22:42:12 -050041ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040042MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050043MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -040044MODULE_LICENSE("GPL");
45
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070046static int immediate_undock = 1;
47module_param(immediate_undock, bool, 0644);
48MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
49 "undock immediately when the undock button is pressed, 0 will cause"
50 " the driver to wait for userspace to write the undock sysfs file "
51 " before undocking");
52
Len Brownc8f7a622006-07-09 17:22:28 -040053static struct atomic_notifier_head dock_notifier_list;
54
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
Alex Chiang50d716e2009-10-01 11:59:23 -060070 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080071 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/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060096 * add_dock_dependent_device - associate a device with the dock station
97 * @ds: The dock station
98 * @handle: handle of the dependent device
Len Brownc8f7a622006-07-09 17:22:28 -040099 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600100 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -0400101 */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600102static int
103add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400104{
105 struct dock_dependent_device *dd;
106
107 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600108 if (!dd)
109 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -0400110
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600111 dd->handle = handle;
112 INIT_LIST_HEAD(&dd->list);
113 INIT_LIST_HEAD(&dd->hotplug_list);
114
Len Brownc8f7a622006-07-09 17:22:28 -0400115 spin_lock(&ds->dd_lock);
116 list_add_tail(&dd->list, &ds->dependent_devices);
117 spin_unlock(&ds->dd_lock);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600118
119 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400120}
121
122/**
123 * dock_add_hotplug_device - associate a hotplug handler with the dock station
124 * @ds: The dock station
125 * @dd: The dependent device struct
126 *
127 * Add the dependent device to the dock's hotplug device list
128 */
129static void
130dock_add_hotplug_device(struct dock_station *ds,
131 struct dock_dependent_device *dd)
132{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800133 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400134 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800135 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400136}
137
138/**
139 * dock_del_hotplug_device - remove a hotplug handler from the dock station
140 * @ds: The dock station
141 * @dd: the dependent device struct
142 *
143 * Delete the dependent device from the dock's hotplug device list
144 */
145static void
146dock_del_hotplug_device(struct dock_station *ds,
147 struct dock_dependent_device *dd)
148{
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800149 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400150 list_del(&dd->hotplug_list);
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800151 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400152}
153
154/**
155 * find_dock_dependent_device - get a device dependent on this dock
156 * @ds: the dock station
157 * @handle: the acpi_handle of the device we want
158 *
159 * iterate over the dependent device list for this dock. If the
160 * dependent device matches the handle, return.
161 */
162static struct dock_dependent_device *
163find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
164{
165 struct dock_dependent_device *dd;
166
167 spin_lock(&ds->dd_lock);
168 list_for_each_entry(dd, &ds->dependent_devices, list) {
169 if (handle == dd->handle) {
170 spin_unlock(&ds->dd_lock);
171 return dd;
172 }
173 }
174 spin_unlock(&ds->dd_lock);
175 return NULL;
176}
177
178/*****************************************************************************
179 * Dock functions *
180 *****************************************************************************/
181/**
182 * is_dock - see if a device is a dock station
183 * @handle: acpi handle of the device
184 *
185 * If an acpi object has a _DCK method, then it is by definition a dock
186 * station, so return true.
187 */
188static int is_dock(acpi_handle handle)
189{
190 acpi_status status;
191 acpi_handle tmp;
192
193 status = acpi_get_handle(handle, "_DCK", &tmp);
194 if (ACPI_FAILURE(status))
195 return 0;
196 return 1;
197}
198
Shaohua Lidb350b02008-08-28 10:03:58 +0800199static int is_ejectable(acpi_handle handle)
200{
201 acpi_status status;
202 acpi_handle tmp;
203
204 status = acpi_get_handle(handle, "_EJ0", &tmp);
205 if (ACPI_FAILURE(status))
206 return 0;
207 return 1;
208}
209
210static int is_ata(acpi_handle handle)
211{
212 acpi_handle tmp;
213
214 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
215 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
216 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
217 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
218 return 1;
219
220 return 0;
221}
222
223static int is_battery(acpi_handle handle)
224{
225 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800226 int ret = 1;
227
Bob Moore15b8dd52009-06-29 13:39:29 +0800228 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800229 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800230 if (!(info->valid & ACPI_VALID_HID))
231 ret = 0;
232 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800233 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800234
Bob Moore15b8dd52009-06-29 13:39:29 +0800235 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800236 return ret;
237}
238
239static int is_ejectable_bay(acpi_handle handle)
240{
241 acpi_handle phandle;
Alex Chiang747479a2009-10-19 15:14:50 -0600242
Shaohua Lidb350b02008-08-28 10:03:58 +0800243 if (!is_ejectable(handle))
244 return 0;
245 if (is_battery(handle) || is_ata(handle))
246 return 1;
247 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
248 return 1;
249 return 0;
250}
251
Len Brownc8f7a622006-07-09 17:22:28 -0400252/**
253 * is_dock_device - see if a device is on a dock station
254 * @handle: acpi handle of the device
255 *
256 * If this device is either the dock station itself,
257 * or is a device dependent on the dock station, then it
258 * is a dock device
259 */
260int is_dock_device(acpi_handle handle)
261{
Shaohua Lidb350b02008-08-28 10:03:58 +0800262 struct dock_station *dock_station;
263
264 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400265 return 0;
266
Shaohua Lidb350b02008-08-28 10:03:58 +0800267 if (is_dock(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400268 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600269
270 list_for_each_entry(dock_station, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +0800271 if (find_dock_dependent_device(dock_station, handle))
272 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400273
274 return 0;
275}
Len Brownc8f7a622006-07-09 17:22:28 -0400276EXPORT_SYMBOL_GPL(is_dock_device);
277
278/**
279 * dock_present - see if the dock station is present.
280 * @ds: the dock station
281 *
282 * execute the _STA method. note that present does not
283 * imply that we are docked.
284 */
285static int dock_present(struct dock_station *ds)
286{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400287 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400288 acpi_status status;
289
290 if (ds) {
291 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
292 if (ACPI_SUCCESS(status) && sta)
293 return 1;
294 }
295 return 0;
296}
297
Len Brownc8f7a622006-07-09 17:22:28 -0400298/**
299 * dock_create_acpi_device - add new devices to acpi
300 * @handle - handle of the device to add
301 *
302 * This function will create a new acpi_device for the given
303 * handle if one does not exist already. This should cause
304 * acpi to scan for drivers for the given devices, and call
305 * matching driver's add routine.
306 *
307 * Returns a pointer to the acpi_device corresponding to the handle.
308 */
309static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
310{
Alex Chiang747479a2009-10-19 15:14:50 -0600311 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400312 struct acpi_device *parent_device;
313 acpi_handle parent;
314 int ret;
315
316 if (acpi_bus_get_device(handle, &device)) {
317 /*
318 * no device created for this object,
319 * so we should create one.
320 */
321 acpi_get_parent(handle, &parent);
322 if (acpi_bus_get_device(parent, &parent_device))
323 parent_device = NULL;
324
325 ret = acpi_bus_add(&device, parent_device, handle,
326 ACPI_BUS_TYPE_DEVICE);
327 if (ret) {
Alex Chiang747479a2009-10-19 15:14:50 -0600328 pr_debug("error adding bus, %x\n", -ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400329 return NULL;
330 }
331 }
332 return device;
333}
334
335/**
336 * dock_remove_acpi_device - remove the acpi_device struct from acpi
337 * @handle - the handle of the device to remove
338 *
339 * Tell acpi to remove the acpi_device. This should cause any loaded
340 * driver to have it's remove routine called.
341 */
342static void dock_remove_acpi_device(acpi_handle handle)
343{
344 struct acpi_device *device;
345 int ret;
346
347 if (!acpi_bus_get_device(handle, &device)) {
348 ret = acpi_bus_trim(device, 1);
349 if (ret)
350 pr_debug("error removing bus, %x\n", -ret);
351 }
352}
353
Len Brownc8f7a622006-07-09 17:22:28 -0400354/**
355 * hotplug_dock_devices - insert or remove devices on the dock station
356 * @ds: the dock station
357 * @event: either bus check or eject request
358 *
359 * Some devices on the dock station need to have drivers called
360 * to perform hotplug operations after a dock event has occurred.
361 * Traverse the list of dock devices that have registered a
362 * hotplug handler, and call the handler.
363 */
364static void hotplug_dock_devices(struct dock_station *ds, u32 event)
365{
366 struct dock_dependent_device *dd;
367
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800368 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400369
370 /*
371 * First call driver specific hotplug functions
372 */
Alex Chiang747479a2009-10-19 15:14:50 -0600373 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
Shaohua Li1253f7a2008-08-28 10:06:16 +0800374 if (dd->ops && dd->ops->handler)
375 dd->ops->handler(dd->handle, event, dd->context);
Len Brownc8f7a622006-07-09 17:22:28 -0400376
377 /*
378 * Now make sure that an acpi_device is created for each
379 * dependent device, or removed if this is an eject request.
380 * This will cause acpi_drivers to be stopped/started if they
381 * exist
382 */
383 list_for_each_entry(dd, &ds->dependent_devices, list) {
384 if (event == ACPI_NOTIFY_EJECT_REQUEST)
385 dock_remove_acpi_device(dd->handle);
386 else
387 dock_create_acpi_device(dd->handle);
388 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800389 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400390}
391
392static void dock_event(struct dock_station *ds, u32 event, int num)
393{
Shaohua Lidb350b02008-08-28 10:03:58 +0800394 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700395 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700396 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800397 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700398
399 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700400 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700401 else
Holger Macht66b56822007-08-10 13:10:32 -0700402 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700403
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700404 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800405 * Indicate that the status of the dock station has
406 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700407 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800408 if (num == DOCK_EVENT)
409 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
410
411 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
412 if (dd->ops && dd->ops->uevent)
413 dd->ops->uevent(dd->handle, event, dd->context);
Alex Chiang747479a2009-10-19 15:14:50 -0600414
Shaohua Li1253f7a2008-08-28 10:06:16 +0800415 if (num != DOCK_EVENT)
416 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400417}
418
419/**
420 * eject_dock - respond to a dock eject request
421 * @ds: the dock station
422 *
423 * This is called after _DCK is called, to execute the dock station's
424 * _EJ0 method.
425 */
426static void eject_dock(struct dock_station *ds)
427{
428 struct acpi_object_list arg_list;
429 union acpi_object arg;
430 acpi_status status;
431 acpi_handle tmp;
432
433 /* all dock devices should have _EJ0, but check anyway */
434 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
435 if (ACPI_FAILURE(status)) {
436 pr_debug("No _EJ0 support for dock device\n");
437 return;
438 }
439
440 arg_list.count = 1;
441 arg_list.pointer = &arg;
442 arg.type = ACPI_TYPE_INTEGER;
443 arg.integer.value = 1;
444
Alex Chiang747479a2009-10-19 15:14:50 -0600445 status = acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL);
446 if (ACPI_FAILURE(status))
Len Brownc8f7a622006-07-09 17:22:28 -0400447 pr_debug("Failed to evaluate _EJ0!\n");
448}
449
450/**
451 * handle_dock - handle a dock event
452 * @ds: the dock station
453 * @dock: to dock, or undock - that is the question
454 *
455 * Execute the _DCK method in response to an acpi event
456 */
457static void handle_dock(struct dock_station *ds, int dock)
458{
459 acpi_status status;
460 struct acpi_object_list arg_list;
461 union acpi_object arg;
462 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
463 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400464
465 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
Len Brownc8f7a622006-07-09 17:22:28 -0400466
Dmitry Torokhov9254bc82007-07-18 01:10:24 -0400467 printk(KERN_INFO PREFIX "%s - %s\n",
468 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400469
470 /* _DCK method has one argument */
471 arg_list.count = 1;
472 arg_list.pointer = &arg;
473 arg.type = ACPI_TYPE_INTEGER;
474 arg.integer.value = dock;
475 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800476 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Thomas Renninger0a918a92008-10-11 00:15:04 -0400477 ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
478 " _DCK\n", (char *)name_buffer.pointer));
479
Len Brownc8f7a622006-07-09 17:22:28 -0400480 kfree(buffer.pointer);
481 kfree(name_buffer.pointer);
482}
483
484static inline void dock(struct dock_station *ds)
485{
486 handle_dock(ds, 1);
487}
488
489static inline void undock(struct dock_station *ds)
490{
491 handle_dock(ds, 0);
492}
493
494static inline void begin_dock(struct dock_station *ds)
495{
496 ds->flags |= DOCK_DOCKING;
497}
498
499static inline void complete_dock(struct dock_station *ds)
500{
501 ds->flags &= ~(DOCK_DOCKING);
502 ds->last_dock_time = jiffies;
503}
504
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700505static inline void begin_undock(struct dock_station *ds)
506{
507 ds->flags |= DOCK_UNDOCKING;
508}
509
510static inline void complete_undock(struct dock_station *ds)
511{
512 ds->flags &= ~(DOCK_UNDOCKING);
513}
514
Shaohua Li406f6922008-08-28 10:03:26 +0800515static void dock_lock(struct dock_station *ds, int lock)
516{
517 struct acpi_object_list arg_list;
518 union acpi_object arg;
519 acpi_status status;
520
521 arg_list.count = 1;
522 arg_list.pointer = &arg;
523 arg.type = ACPI_TYPE_INTEGER;
524 arg.integer.value = !!lock;
525 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
526 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
527 if (lock)
528 printk(KERN_WARNING PREFIX "Locking device failed\n");
529 else
530 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
531 }
532}
533
Len Brownc8f7a622006-07-09 17:22:28 -0400534/**
535 * dock_in_progress - see if we are in the middle of handling a dock event
536 * @ds: the dock station
537 *
538 * Sometimes while docking, false dock events can be sent to the driver
539 * because good connections aren't made or some other reason. Ignore these
540 * if we are in the middle of doing something.
541 */
542static int dock_in_progress(struct dock_station *ds)
543{
544 if ((ds->flags & DOCK_DOCKING) ||
545 time_before(jiffies, (ds->last_dock_time + HZ)))
546 return 1;
547 return 0;
548}
549
550/**
551 * register_dock_notifier - add yourself to the dock notifier list
552 * @nb: the callers notifier block
553 *
554 * If a driver wishes to be notified about dock events, they can
555 * use this function to put a notifier block on the dock notifier list.
556 * this notifier call chain will be called after a dock event, but
557 * before hotplugging any new devices.
558 */
559int register_dock_notifier(struct notifier_block *nb)
560{
Shaohua Lidb350b02008-08-28 10:03:58 +0800561 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800562 return -ENODEV;
563
Len Brownc8f7a622006-07-09 17:22:28 -0400564 return atomic_notifier_chain_register(&dock_notifier_list, nb);
565}
Len Brownc8f7a622006-07-09 17:22:28 -0400566EXPORT_SYMBOL_GPL(register_dock_notifier);
567
568/**
569 * unregister_dock_notifier - remove yourself from the dock notifier list
570 * @nb: the callers notifier block
571 */
572void unregister_dock_notifier(struct notifier_block *nb)
573{
Shaohua Lidb350b02008-08-28 10:03:58 +0800574 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800575 return;
576
Len Brownc8f7a622006-07-09 17:22:28 -0400577 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
578}
Len Brownc8f7a622006-07-09 17:22:28 -0400579EXPORT_SYMBOL_GPL(unregister_dock_notifier);
580
581/**
582 * register_hotplug_dock_device - register a hotplug function
583 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800584 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400585 * @context: device specific data
586 *
587 * If a driver would like to perform a hotplug operation after a dock
588 * event, they can register an acpi_notifiy_handler to be called by
589 * the dock driver after _DCK is executed.
590 */
591int
Shaohua Li1253f7a2008-08-28 10:06:16 +0800592register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
Len Brownc8f7a622006-07-09 17:22:28 -0400593 void *context)
594{
595 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800596 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800597 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400598
Shaohua Lidb350b02008-08-28 10:03:58 +0800599 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400600 return -ENODEV;
601
602 /*
603 * make sure this handle is for a device dependent on the dock,
604 * this would include the dock station itself
605 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600606 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800607 /*
608 * An ATA bay can be in a dock and itself can be ejected
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800609 * separately, so there are two 'dock stations' which need the
Shaohua Li61b83692008-08-28 10:07:14 +0800610 * ops
611 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800612 dd = find_dock_dependent_device(dock_station, handle);
613 if (dd) {
Shaohua Li1253f7a2008-08-28 10:06:16 +0800614 dd->ops = ops;
Shaohua Lidb350b02008-08-28 10:03:58 +0800615 dd->context = context;
616 dock_add_hotplug_device(dock_station, dd);
Shaohua Li61b83692008-08-28 10:07:14 +0800617 ret = 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800618 }
Len Brownc8f7a622006-07-09 17:22:28 -0400619 }
620
Shaohua Li61b83692008-08-28 10:07:14 +0800621 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400622}
Len Brownc8f7a622006-07-09 17:22:28 -0400623EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
624
625/**
626 * unregister_hotplug_dock_device - remove yourself from the hotplug list
627 * @handle: the acpi handle of the device
628 */
629void unregister_hotplug_dock_device(acpi_handle handle)
630{
631 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800632 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400633
Shaohua Lidb350b02008-08-28 10:03:58 +0800634 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400635 return;
636
Alex Chiang50d716e2009-10-01 11:59:23 -0600637 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800638 dd = find_dock_dependent_device(dock_station, handle);
639 if (dd)
640 dock_del_hotplug_device(dock_station, dd);
641 }
Len Brownc8f7a622006-07-09 17:22:28 -0400642}
Len Brownc8f7a622006-07-09 17:22:28 -0400643EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
644
645/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800646 * handle_eject_request - handle an undock request checking for error conditions
647 *
648 * Check to make sure the dock device is still present, then undock and
649 * hotremove all the devices that may need removing.
650 */
651static int handle_eject_request(struct dock_station *ds, u32 event)
652{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800653 if (dock_in_progress(ds))
654 return -EBUSY;
655
656 /*
657 * here we need to generate the undock
658 * event prior to actually doing the undock
659 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200660 * Also, even send the dock event if the
661 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800662 */
663 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200664
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800665 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
666 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800667 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800668 eject_dock(ds);
669 if (dock_present(ds)) {
670 printk(KERN_ERR PREFIX "Unable to undock!\n");
671 return -EBUSY;
672 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700673 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800674 return 0;
675}
676
677/**
Len Brownc8f7a622006-07-09 17:22:28 -0400678 * dock_notify - act upon an acpi dock notification
679 * @handle: the dock station handle
680 * @event: the acpi event
681 * @data: our driver data struct
682 *
683 * If we are notified to dock, then check to see if the dock is
684 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800685 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400686 */
687static void dock_notify(acpi_handle handle, u32 event, void *data)
688{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200689 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800690 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800691 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400692
Shaohua Lidb350b02008-08-28 10:03:58 +0800693 /*
694 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
695 * is sent and _DCK is present, it is assumed to mean an undock
696 * request.
697 */
698 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
699 event = ACPI_NOTIFY_EJECT_REQUEST;
700
701 /*
702 * dock station: BUS_CHECK - docked or surprise removal
703 * DEVICE_CHECK - undocked
704 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
705 *
706 * To simplify event handling, dock dependent device handler always
707 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
708 * ACPI_NOTIFY_EJECT_REQUEST for removal
709 */
Len Brownc8f7a622006-07-09 17:22:28 -0400710 switch (event) {
711 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800712 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800713 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
714 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400715 begin_dock(ds);
716 dock(ds);
717 if (!dock_present(ds)) {
718 printk(KERN_ERR PREFIX "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800719 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400720 break;
721 }
722 atomic_notifier_call_chain(&dock_notifier_list,
723 event, NULL);
724 hotplug_dock_devices(ds, event);
725 complete_dock(ds);
726 dock_event(ds, event, DOCK_EVENT);
Shaohua Li406f6922008-08-28 10:03:26 +0800727 dock_lock(ds, 1);
Shaohua Lidb350b02008-08-28 10:03:58 +0800728 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400729 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800730 if (dock_present(ds) || dock_in_progress(ds))
731 break;
732 /* This is a surprise removal */
733 surprise_removal = 1;
734 event = ACPI_NOTIFY_EJECT_REQUEST;
735 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400736 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700737 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800738 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
739 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700740 handle_eject_request(ds, event);
741 else
742 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400743 break;
744 default:
745 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
746 }
747}
748
Zhang Rui19cd8472008-08-28 10:05:06 +0800749struct dock_data {
750 acpi_handle handle;
751 unsigned long event;
752 struct dock_station *ds;
753};
754
755static void acpi_dock_deferred_cb(void *context)
756{
Alex Chiang747479a2009-10-19 15:14:50 -0600757 struct dock_data *data = context;
Zhang Rui19cd8472008-08-28 10:05:06 +0800758
759 dock_notify(data->handle, data->event, data->ds);
760 kfree(data);
761}
762
Shaohua Li6bd00a62008-08-28 10:04:29 +0800763static int acpi_dock_notifier_call(struct notifier_block *this,
764 unsigned long event, void *data)
765{
766 struct dock_station *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600767 acpi_handle handle = data;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800768
769 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
770 && event != ACPI_NOTIFY_EJECT_REQUEST)
771 return 0;
Alex Chiang50d716e2009-10-01 11:59:23 -0600772 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li6bd00a62008-08-28 10:04:29 +0800773 if (dock_station->handle == handle) {
Alex Chiang747479a2009-10-19 15:14:50 -0600774 struct dock_data *dd;
Zhang Rui19cd8472008-08-28 10:05:06 +0800775
Alex Chiang747479a2009-10-19 15:14:50 -0600776 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
777 if (!dd)
Zhang Rui19cd8472008-08-28 10:05:06 +0800778 return 0;
Alex Chiang747479a2009-10-19 15:14:50 -0600779 dd->handle = handle;
780 dd->event = event;
781 dd->ds = dock_station;
782 acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800783 return 0 ;
784 }
785 }
786 return 0;
787}
788
789static struct notifier_block dock_acpi_notifier = {
790 .notifier_call = acpi_dock_notifier_call,
791};
792
Len Brownc8f7a622006-07-09 17:22:28 -0400793/**
794 * find_dock_devices - find devices on the dock station
795 * @handle: the handle of the device we are examining
796 * @lvl: unused
797 * @context: the dock station private data
798 * @rv: unused
799 *
800 * This function is called by acpi_walk_namespace. It will
801 * check to see if an object has an _EJD method. If it does, then it
802 * will see if it is dependent on the dock station.
803 */
804static acpi_status
805find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
806{
807 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500808 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200809 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400810
811 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500812 if (ACPI_FAILURE(status)) {
813 /* try the parent device as well */
814 status = acpi_get_parent(handle, &parent);
815 if (ACPI_FAILURE(status))
816 goto fdd_out;
817 /* see if parent is dependent on dock */
818 status = acpi_bus_get_ejd(parent, &tmp);
819 if (ACPI_FAILURE(status))
820 goto fdd_out;
821 }
Len Brownc8f7a622006-07-09 17:22:28 -0400822
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600823 if (tmp == ds->handle)
824 add_dock_dependent_device(ds, handle);
825
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500826fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400827 return AE_OK;
828}
829
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800830/*
831 * show_docked - read method for "docked" file in sysfs
832 */
833static ssize_t show_docked(struct device *dev,
834 struct device_attribute *attr, char *buf)
835{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100836 struct acpi_device *tmp;
837
Alex Chiangfe06fba2009-10-19 15:14:45 -0600838 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800839
Holger Machtfc5a9f82009-01-20 12:18:24 +0100840 if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
841 return snprintf(buf, PAGE_SIZE, "1\n");
842 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800843}
Adrian Bunke5685b92007-10-24 18:24:42 +0200844static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800845
846/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700847 * show_flags - read method for flags file in sysfs
848 */
849static ssize_t show_flags(struct device *dev,
850 struct device_attribute *attr, char *buf)
851{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600852 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700853 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
854
855}
Adrian Bunke5685b92007-10-24 18:24:42 +0200856static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700857
858/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800859 * write_undock - write method for "undock" file in sysfs
860 */
861static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
862 const char *buf, size_t count)
863{
864 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600865 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800866
867 if (!count)
868 return -EINVAL;
869
Holger Macht9171f832008-03-12 01:07:27 +0100870 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800871 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
872 return ret ? ret: count;
873}
Adrian Bunke5685b92007-10-24 18:24:42 +0200874static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800875
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800876/*
877 * show_dock_uid - read method for "uid" file in sysfs
878 */
879static ssize_t show_dock_uid(struct device *dev,
880 struct device_attribute *attr, char *buf)
881{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400882 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600883 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700884 acpi_status status = acpi_evaluate_integer(dock_station->handle,
885 "_UID", NULL, &lbuf);
886 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800887 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700888
Matthew Wilcox27663c52008-10-10 02:22:59 -0400889 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800890}
Adrian Bunke5685b92007-10-24 18:24:42 +0200891static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800892
Shaohua Li8652b002008-08-28 10:07:45 +0800893static ssize_t show_dock_type(struct device *dev,
894 struct device_attribute *attr, char *buf)
895{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600896 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800897 char *type;
898
899 if (dock_station->flags & DOCK_IS_DOCK)
900 type = "dock_station";
901 else if (dock_station->flags & DOCK_IS_ATA)
902 type = "ata_bay";
903 else if (dock_station->flags & DOCK_IS_BAT)
904 type = "battery_bay";
905 else
906 type = "unknown";
907
908 return snprintf(buf, PAGE_SIZE, "%s\n", type);
909}
910static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
911
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600912static struct attribute *dock_attributes[] = {
913 &dev_attr_docked.attr,
914 &dev_attr_flags.attr,
915 &dev_attr_undock.attr,
916 &dev_attr_uid.attr,
917 &dev_attr_type.attr,
918 NULL
919};
920
921static struct attribute_group dock_attribute_group = {
922 .attrs = dock_attributes
923};
924
Len Brownc8f7a622006-07-09 17:22:28 -0400925/**
926 * dock_add - add a new dock station
927 * @handle: the dock station handle
928 *
929 * allocated and initialize a new dock station device. Find all devices
930 * that are on the dock station, and register for dock event notifications.
931 */
932static int dock_add(acpi_handle handle)
933{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600934 int ret, id;
935 struct dock_station ds, *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600936 struct platform_device *dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400937
Alex Chiangfe06fba2009-10-19 15:14:45 -0600938 id = dock_station_count;
Alex Chiang49c6fb22010-02-01 10:35:18 -0700939 memset(&ds, 0, sizeof(ds));
Alex Chiang747479a2009-10-19 15:14:50 -0600940 dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
941 if (IS_ERR(dd))
942 return PTR_ERR(dd);
Alex Chiang9751cb72009-10-19 15:14:40 -0600943
Alex Chiang747479a2009-10-19 15:14:50 -0600944 dock_station = dd->dev.platform_data;
945
Len Brownc8f7a622006-07-09 17:22:28 -0400946 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600947 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400948 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400949
Len Brownc8f7a622006-07-09 17:22:28 -0400950 mutex_init(&dock_station->hp_lock);
Alex Chiang747479a2009-10-19 15:14:50 -0600951 spin_lock_init(&dock_station->dd_lock);
952 INIT_LIST_HEAD(&dock_station->sibling);
953 INIT_LIST_HEAD(&dock_station->hotplug_devices);
Len Brownc8f7a622006-07-09 17:22:28 -0400954 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Alex Chiang747479a2009-10-19 15:14:50 -0600955 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700956
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700957 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600958 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700959
Shaohua Lidb350b02008-08-28 10:03:58 +0800960 if (is_dock(handle))
961 dock_station->flags |= DOCK_IS_DOCK;
962 if (is_ata(handle))
963 dock_station->flags |= DOCK_IS_ATA;
964 if (is_battery(handle))
965 dock_station->flags |= DOCK_IS_BAT;
966
Alex Chiang747479a2009-10-19 15:14:50 -0600967 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800968 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600969 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800970
Len Brownc8f7a622006-07-09 17:22:28 -0400971 /* Find dependent devices */
972 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +0800973 ACPI_UINT32_MAX, find_dock_devices, NULL,
974 dock_station, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400975
976 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600977 ret = add_dock_dependent_device(dock_station, handle);
978 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600979 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400980
Shaohua Lidb350b02008-08-28 10:03:58 +0800981 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600982 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -0400983 return 0;
984
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600985err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600986 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600987err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600988 platform_device_unregister(dd);
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600989 printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400990 return ret;
991}
992
993/**
994 * dock_remove - free up resources related to the dock station
995 */
Alex Chiang747479a2009-10-19 15:14:50 -0600996static int dock_remove(struct dock_station *ds)
Len Brownc8f7a622006-07-09 17:22:28 -0400997{
998 struct dock_dependent_device *dd, *tmp;
Alex Chiang747479a2009-10-19 15:14:50 -0600999 struct platform_device *dock_device = ds->dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -04001000
Shaohua Lidb350b02008-08-28 10:03:58 +08001001 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -04001002 return 0;
1003
1004 /* remove dependent devices */
Alex Chiang747479a2009-10-19 15:14:50 -06001005 list_for_each_entry_safe(dd, tmp, &ds->dependent_devices, list)
1006 kfree(dd);
Len Brownc8f7a622006-07-09 17:22:28 -04001007
Alex Chiang747479a2009-10-19 15:14:50 -06001008 list_del(&ds->sibling);
Len Brownc8f7a622006-07-09 17:22:28 -04001009
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001010 /* cleanup sysfs */
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001011 sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
Kristen Carlson Accardi0f6f2802007-05-09 15:07:04 -07001012 platform_device_unregister(dock_device);
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001013
Len Brownc8f7a622006-07-09 17:22:28 -04001014 return 0;
1015}
1016
1017/**
1018 * find_dock - look for a dock station
1019 * @handle: acpi handle of a device
1020 * @lvl: unused
1021 * @context: counter of dock stations found
1022 * @rv: unused
1023 *
1024 * This is called by acpi_walk_namespace to look for dock stations.
1025 */
1026static acpi_status
1027find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1028{
Alex Chiang747479a2009-10-19 15:14:50 -06001029 if (is_dock(handle))
Zhang Rui1ee4d612010-03-22 15:46:49 +08001030 dock_add(handle);
Alex Chiang747479a2009-10-19 15:14:50 -06001031
Zhang Rui1ee4d612010-03-22 15:46:49 +08001032 return AE_OK;
Len Brownc8f7a622006-07-09 17:22:28 -04001033}
1034
Shaohua Lidb350b02008-08-28 10:03:58 +08001035static acpi_status
1036find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
1037{
Shaohua Li61b83692008-08-28 10:07:14 +08001038 /* If bay is a dock, it's already handled */
1039 if (is_ejectable_bay(handle) && !is_dock(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +08001040 dock_add(handle);
1041 return AE_OK;
1042}
1043
Len Brownc8f7a622006-07-09 17:22:28 -04001044static int __init dock_init(void)
1045{
Len Brown816c2ed2008-06-24 22:57:12 -04001046 if (acpi_disabled)
1047 return 0;
1048
Len Brownc8f7a622006-07-09 17:22:28 -04001049 /* look for a dock station */
1050 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001051 ACPI_UINT32_MAX, find_dock, NULL, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001052
Shaohua Lidb350b02008-08-28 10:03:58 +08001053 /* look for bay */
1054 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001055 ACPI_UINT32_MAX, find_bay, NULL, NULL, NULL);
Shaohua Lidb350b02008-08-28 10:03:58 +08001056 if (!dock_station_count) {
1057 printk(KERN_INFO PREFIX "No dock devices found.\n");
1058 return 0;
1059 }
Len Brownc8f7a622006-07-09 17:22:28 -04001060
Shaohua Li6bd00a62008-08-28 10:04:29 +08001061 register_acpi_bus_notifier(&dock_acpi_notifier);
Shaohua Lidb350b02008-08-28 10:03:58 +08001062 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1063 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -04001064 return 0;
1065}
1066
1067static void __exit dock_exit(void)
1068{
Alex Chiang747479a2009-10-19 15:14:50 -06001069 struct dock_station *tmp, *dock_station;
Shaohua Lidb350b02008-08-28 10:03:58 +08001070
Shaohua Li6bd00a62008-08-28 10:04:29 +08001071 unregister_acpi_bus_notifier(&dock_acpi_notifier);
Alex Chiang50d716e2009-10-01 11:59:23 -06001072 list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +08001073 dock_remove(dock_station);
Len Brownc8f7a622006-07-09 17:22:28 -04001074}
1075
Shaohua Lidb350b02008-08-28 10:03:58 +08001076/*
1077 * Must be called before drivers of devices in dock, otherwise we can't know
1078 * which devices are in a dock
1079 */
1080subsys_initcall(dock_init);
Len Brownc8f7a622006-07-09 17:22:28 -04001081module_exit(dock_exit);