blob: 7c86d01346e6d021ad9f385a74087cfe669e7074 [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>
Toshi Kanicd730182012-11-20 23:42:30 +000034#include <linux/acpi.h>
Len Brownc8f7a622006-07-09 17:22:28 -040035#include <acpi/acpi_bus.h>
36#include <acpi/acpi_drivers.h>
37
Len Browna192a952009-07-28 16:45:54 -040038#define PREFIX "ACPI: "
39
Len Brown7cda93e2007-02-12 23:50:02 -050040#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
Len Brownc8f7a622006-07-09 17:22:28 -040041
Len Brownf52fd662007-02-12 22:42:12 -050042ACPI_MODULE_NAME("dock");
Len Brownc8f7a622006-07-09 17:22:28 -040043MODULE_AUTHOR("Kristen Carlson Accardi");
Len Brown7cda93e2007-02-12 23:50:02 -050044MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
Len Brownc8f7a622006-07-09 17:22:28 -040045MODULE_LICENSE("GPL");
46
Rusty Russell90ab5ee2012-01-13 09:32:20 +103047static bool immediate_undock = 1;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070048module_param(immediate_undock, bool, 0644);
49MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
50 "undock immediately when the undock button is pressed, 0 will cause"
51 " the driver to wait for userspace to write the undock sysfs file "
52 " before undocking");
53
Len Brownc8f7a622006-07-09 17:22:28 -040054static struct atomic_notifier_head dock_notifier_list;
55
Frank Seidela340af12007-12-07 13:20:42 +010056static const struct acpi_device_id dock_device_ids[] = {
57 {"LNXDOCK", 0},
58 {"", 0},
59};
60MODULE_DEVICE_TABLE(acpi, dock_device_ids);
61
Len Brownc8f7a622006-07-09 17:22:28 -040062struct dock_station {
63 acpi_handle handle;
64 unsigned long last_dock_time;
65 u32 flags;
Len Brownc8f7a622006-07-09 17:22:28 -040066 struct list_head dependent_devices;
Shaohua Lidb350b02008-08-28 10:03:58 +080067
Alex Chiang50d716e2009-10-01 11:59:23 -060068 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080069 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040070};
Shaohua Lidb350b02008-08-28 10:03:58 +080071static LIST_HEAD(dock_stations);
72static int dock_station_count;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020073static DEFINE_MUTEX(hotplug_lock);
Len Brownc8f7a622006-07-09 17:22:28 -040074
75struct dock_dependent_device {
76 struct list_head list;
Len Brownc8f7a622006-07-09 17:22:28 -040077 acpi_handle handle;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020078 const struct acpi_dock_ops *hp_ops;
79 void *hp_context;
80 unsigned int hp_refcount;
81 void (*hp_release)(void *);
Len Brownc8f7a622006-07-09 17:22:28 -040082};
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 */
Jiang Liud423c082013-06-29 00:24:36 +0800102static int __init
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600103add_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);
Len Brownc8f7a622006-07-09 17:22:28 -0400113 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600114
115 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400116}
117
118/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200119 * dock_init_hotplug - Initialize a hotplug device on a docking station.
120 * @dd: Dock-dependent device.
121 * @ops: Dock operations to attach to the dependent device.
122 * @context: Data to pass to the @ops callbacks and @release.
123 * @init: Optional initialization routine to run after setting up context.
124 * @release: Optional release routine to run on removal.
Len Brownc8f7a622006-07-09 17:22:28 -0400125 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200126static int dock_init_hotplug(struct dock_dependent_device *dd,
127 const struct acpi_dock_ops *ops, void *context,
128 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400129{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200130 int ret = 0;
131
132 mutex_lock(&hotplug_lock);
133
134 if (dd->hp_context) {
135 ret = -EEXIST;
136 } else {
137 dd->hp_refcount = 1;
138 dd->hp_ops = ops;
139 dd->hp_context = context;
140 dd->hp_release = release;
141 }
142
143 if (!WARN_ON(ret) && init)
144 init(context);
145
146 mutex_unlock(&hotplug_lock);
147 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400148}
149
150/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200151 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
152 * @dd: Dock-dependent device.
Len Brownc8f7a622006-07-09 17:22:28 -0400153 *
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200154 * Decrement the reference counter of @dd and if 0, detach its hotplug
155 * operations from it, reset its context pointer and run the optional release
156 * routine if present.
Len Brownc8f7a622006-07-09 17:22:28 -0400157 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200158static void dock_release_hotplug(struct dock_dependent_device *dd)
Len Brownc8f7a622006-07-09 17:22:28 -0400159{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200160 void (*release)(void *) = NULL;
161 void *context = NULL;
162
163 mutex_lock(&hotplug_lock);
164
165 if (dd->hp_context && !--dd->hp_refcount) {
166 dd->hp_ops = NULL;
167 context = dd->hp_context;
168 dd->hp_context = NULL;
169 release = dd->hp_release;
170 dd->hp_release = NULL;
171 }
172
173 if (release && context)
174 release(context);
175
176 mutex_unlock(&hotplug_lock);
177}
178
179static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
180 bool uevent)
181{
182 acpi_notify_handler cb = NULL;
183 bool run = false;
184
185 mutex_lock(&hotplug_lock);
186
187 if (dd->hp_context) {
188 run = true;
189 dd->hp_refcount++;
190 if (dd->hp_ops)
191 cb = uevent ? dd->hp_ops->uevent : dd->hp_ops->handler;
192 }
193
194 mutex_unlock(&hotplug_lock);
195
196 if (!run)
197 return;
198
199 if (cb)
200 cb(dd->handle, event, dd->hp_context);
201
202 dock_release_hotplug(dd);
Len Brownc8f7a622006-07-09 17:22:28 -0400203}
204
205/**
206 * find_dock_dependent_device - get a device dependent on this dock
207 * @ds: the dock station
208 * @handle: the acpi_handle of the device we want
209 *
210 * iterate over the dependent device list for this dock. If the
211 * dependent device matches the handle, return.
212 */
213static struct dock_dependent_device *
214find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
215{
216 struct dock_dependent_device *dd;
217
Jiang Liued633e72013-06-29 00:24:35 +0800218 list_for_each_entry(dd, &ds->dependent_devices, list)
219 if (handle == dd->handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400220 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800221
Len Brownc8f7a622006-07-09 17:22:28 -0400222 return NULL;
223}
224
225/*****************************************************************************
226 * Dock functions *
227 *****************************************************************************/
Jiang Liud423c082013-06-29 00:24:36 +0800228static int __init is_battery(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800229{
230 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800231 int ret = 1;
232
Bob Moore15b8dd52009-06-29 13:39:29 +0800233 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800234 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800235 if (!(info->valid & ACPI_VALID_HID))
236 ret = 0;
237 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800238 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800239
Bob Moore15b8dd52009-06-29 13:39:29 +0800240 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800241 return ret;
242}
243
Jiang Liuc9b54712013-06-29 00:24:42 +0800244/* Check whether ACPI object is an ejectable battery or disk bay */
245static bool __init is_ejectable_bay(acpi_handle handle)
Shaohua Lidb350b02008-08-28 10:03:58 +0800246{
Jiang Liuc9b54712013-06-29 00:24:42 +0800247 if (acpi_has_method(handle, "_EJ0") && is_battery(handle))
248 return true;
Alex Chiang747479a2009-10-19 15:14:50 -0600249
Jiang Liuc9b54712013-06-29 00:24:42 +0800250 return acpi_bay_match(handle);
Shaohua Lidb350b02008-08-28 10:03:58 +0800251}
252
Len Brownc8f7a622006-07-09 17:22:28 -0400253/**
254 * is_dock_device - see if a device is on a dock station
255 * @handle: acpi handle of the device
256 *
257 * If this device is either the dock station itself,
258 * or is a device dependent on the dock station, then it
259 * is a dock device
260 */
261int is_dock_device(acpi_handle handle)
262{
Shaohua Lidb350b02008-08-28 10:03:58 +0800263 struct dock_station *dock_station;
264
265 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400266 return 0;
267
Jiang Liuc9b54712013-06-29 00:24:42 +0800268 if (acpi_dock_match(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400269 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600270
271 list_for_each_entry(dock_station, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +0800272 if (find_dock_dependent_device(dock_station, handle))
273 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400274
275 return 0;
276}
Len Brownc8f7a622006-07-09 17:22:28 -0400277EXPORT_SYMBOL_GPL(is_dock_device);
278
279/**
280 * dock_present - see if the dock station is present.
281 * @ds: the dock station
282 *
283 * execute the _STA method. note that present does not
284 * imply that we are docked.
285 */
286static int dock_present(struct dock_station *ds)
287{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400288 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400289 acpi_status status;
290
291 if (ds) {
292 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
293 if (ACPI_SUCCESS(status) && sta)
294 return 1;
295 }
296 return 0;
297}
298
Len Brownc8f7a622006-07-09 17:22:28 -0400299/**
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.
Len Brownc8f7a622006-07-09 17:22:28 -0400307 */
Jiang Liu472d9632013-06-29 00:24:37 +0800308static void dock_create_acpi_device(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400309{
Alex Chiang747479a2009-10-19 15:14:50 -0600310 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400311 int ret;
312
313 if (acpi_bus_get_device(handle, &device)) {
314 /*
315 * no device created for this object,
316 * so we should create one.
317 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100318 ret = acpi_bus_scan(handle);
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100319 if (ret)
Alex Chiang747479a2009-10-19 15:14:50 -0600320 pr_debug("error adding bus, %x\n", -ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400321 }
Len Brownc8f7a622006-07-09 17:22:28 -0400322}
323
324/**
325 * dock_remove_acpi_device - remove the acpi_device struct from acpi
326 * @handle - the handle of the device to remove
327 *
328 * Tell acpi to remove the acpi_device. This should cause any loaded
329 * driver to have it's remove routine called.
330 */
331static void dock_remove_acpi_device(acpi_handle handle)
332{
333 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400334
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100335 if (!acpi_bus_get_device(handle, &device))
336 acpi_bus_trim(device);
Len Brownc8f7a622006-07-09 17:22:28 -0400337}
338
Len Brownc8f7a622006-07-09 17:22:28 -0400339/**
340 * hotplug_dock_devices - insert or remove devices on the dock station
341 * @ds: the dock station
342 * @event: either bus check or eject request
343 *
344 * Some devices on the dock station need to have drivers called
345 * to perform hotplug operations after a dock event has occurred.
346 * Traverse the list of dock devices that have registered a
347 * hotplug handler, and call the handler.
348 */
349static void hotplug_dock_devices(struct dock_station *ds, u32 event)
350{
351 struct dock_dependent_device *dd;
352
Len Brownc8f7a622006-07-09 17:22:28 -0400353 /*
354 * First call driver specific hotplug functions
355 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200356 list_for_each_entry(dd, &ds->dependent_devices, list)
357 dock_hotplug_event(dd, event, false);
Len Brownc8f7a622006-07-09 17:22:28 -0400358
359 /*
360 * Now make sure that an acpi_device is created for each
361 * dependent device, or removed if this is an eject request.
362 * This will cause acpi_drivers to be stopped/started if they
363 * exist
364 */
365 list_for_each_entry(dd, &ds->dependent_devices, list) {
366 if (event == ACPI_NOTIFY_EJECT_REQUEST)
367 dock_remove_acpi_device(dd->handle);
368 else
369 dock_create_acpi_device(dd->handle);
370 }
Len Brownc8f7a622006-07-09 17:22:28 -0400371}
372
373static void dock_event(struct dock_station *ds, u32 event, int num)
374{
Shaohua Lidb350b02008-08-28 10:03:58 +0800375 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700376 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700377 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800378 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700379
380 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700381 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700382 else
Holger Macht66b56822007-08-10 13:10:32 -0700383 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700384
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700385 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800386 * Indicate that the status of the dock station has
387 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700388 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800389 if (num == DOCK_EVENT)
390 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
391
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200392 list_for_each_entry(dd, &ds->dependent_devices, list)
393 dock_hotplug_event(dd, event, true);
Alex Chiang747479a2009-10-19 15:14:50 -0600394
Shaohua Li1253f7a2008-08-28 10:06:16 +0800395 if (num != DOCK_EVENT)
396 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400397}
398
399/**
Len Brownc8f7a622006-07-09 17:22:28 -0400400 * handle_dock - handle a dock event
401 * @ds: the dock station
402 * @dock: to dock, or undock - that is the question
403 *
404 * Execute the _DCK method in response to an acpi event
405 */
406static void handle_dock(struct dock_station *ds, int dock)
407{
408 acpi_status status;
409 struct acpi_object_list arg_list;
410 union acpi_object arg;
411 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400412
Toshi Kanicd730182012-11-20 23:42:30 +0000413 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400414
415 /* _DCK method has one argument */
416 arg_list.count = 1;
417 arg_list.pointer = &arg;
418 arg.type = ACPI_TYPE_INTEGER;
419 arg.integer.value = dock;
420 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800421 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000422 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
423 status);
Thomas Renninger0a918a92008-10-11 00:15:04 -0400424
Len Brownc8f7a622006-07-09 17:22:28 -0400425 kfree(buffer.pointer);
Len Brownc8f7a622006-07-09 17:22:28 -0400426}
427
428static inline void dock(struct dock_station *ds)
429{
430 handle_dock(ds, 1);
431}
432
433static inline void undock(struct dock_station *ds)
434{
435 handle_dock(ds, 0);
436}
437
438static inline void begin_dock(struct dock_station *ds)
439{
440 ds->flags |= DOCK_DOCKING;
441}
442
443static inline void complete_dock(struct dock_station *ds)
444{
445 ds->flags &= ~(DOCK_DOCKING);
446 ds->last_dock_time = jiffies;
447}
448
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700449static inline void begin_undock(struct dock_station *ds)
450{
451 ds->flags |= DOCK_UNDOCKING;
452}
453
454static inline void complete_undock(struct dock_station *ds)
455{
456 ds->flags &= ~(DOCK_UNDOCKING);
457}
458
Len Brownc8f7a622006-07-09 17:22:28 -0400459/**
460 * dock_in_progress - see if we are in the middle of handling a dock event
461 * @ds: the dock station
462 *
463 * Sometimes while docking, false dock events can be sent to the driver
464 * because good connections aren't made or some other reason. Ignore these
465 * if we are in the middle of doing something.
466 */
467static int dock_in_progress(struct dock_station *ds)
468{
469 if ((ds->flags & DOCK_DOCKING) ||
470 time_before(jiffies, (ds->last_dock_time + HZ)))
471 return 1;
472 return 0;
473}
474
475/**
476 * register_dock_notifier - add yourself to the dock notifier list
477 * @nb: the callers notifier block
478 *
479 * If a driver wishes to be notified about dock events, they can
480 * use this function to put a notifier block on the dock notifier list.
481 * this notifier call chain will be called after a dock event, but
482 * before hotplugging any new devices.
483 */
484int register_dock_notifier(struct notifier_block *nb)
485{
Shaohua Lidb350b02008-08-28 10:03:58 +0800486 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800487 return -ENODEV;
488
Len Brownc8f7a622006-07-09 17:22:28 -0400489 return atomic_notifier_chain_register(&dock_notifier_list, nb);
490}
Len Brownc8f7a622006-07-09 17:22:28 -0400491EXPORT_SYMBOL_GPL(register_dock_notifier);
492
493/**
494 * unregister_dock_notifier - remove yourself from the dock notifier list
495 * @nb: the callers notifier block
496 */
497void unregister_dock_notifier(struct notifier_block *nb)
498{
Shaohua Lidb350b02008-08-28 10:03:58 +0800499 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800500 return;
501
Len Brownc8f7a622006-07-09 17:22:28 -0400502 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
503}
Len Brownc8f7a622006-07-09 17:22:28 -0400504EXPORT_SYMBOL_GPL(unregister_dock_notifier);
505
506/**
507 * register_hotplug_dock_device - register a hotplug function
508 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800509 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400510 * @context: device specific data
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200511 * @init: Optional initialization routine to run after registration
512 * @release: Optional release routine to run on unregistration
Len Brownc8f7a622006-07-09 17:22:28 -0400513 *
514 * If a driver would like to perform a hotplug operation after a dock
515 * event, they can register an acpi_notifiy_handler to be called by
516 * the dock driver after _DCK is executed.
517 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200518int register_hotplug_dock_device(acpi_handle handle,
519 const struct acpi_dock_ops *ops, void *context,
520 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400521{
522 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800523 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800524 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400525
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200526 if (WARN_ON(!context))
527 return -EINVAL;
528
Shaohua Lidb350b02008-08-28 10:03:58 +0800529 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400530 return -ENODEV;
531
532 /*
533 * make sure this handle is for a device dependent on the dock,
534 * this would include the dock station itself
535 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600536 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800537 /*
538 * An ATA bay can be in a dock and itself can be ejected
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800539 * separately, so there are two 'dock stations' which need the
Shaohua Li61b83692008-08-28 10:07:14 +0800540 * ops
541 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800542 dd = find_dock_dependent_device(dock_station, handle);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200543 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
Shaohua Li61b83692008-08-28 10:07:14 +0800544 ret = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400545 }
546
Shaohua Li61b83692008-08-28 10:07:14 +0800547 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400548}
Len Brownc8f7a622006-07-09 17:22:28 -0400549EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
550
551/**
552 * unregister_hotplug_dock_device - remove yourself from the hotplug list
553 * @handle: the acpi handle of the device
554 */
555void unregister_hotplug_dock_device(acpi_handle handle)
556{
557 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800558 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400559
Shaohua Lidb350b02008-08-28 10:03:58 +0800560 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400561 return;
562
Alex Chiang50d716e2009-10-01 11:59:23 -0600563 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800564 dd = find_dock_dependent_device(dock_station, handle);
565 if (dd)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200566 dock_release_hotplug(dd);
Shaohua Lidb350b02008-08-28 10:03:58 +0800567 }
Len Brownc8f7a622006-07-09 17:22:28 -0400568}
Len Brownc8f7a622006-07-09 17:22:28 -0400569EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
570
571/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800572 * handle_eject_request - handle an undock request checking for error conditions
573 *
574 * Check to make sure the dock device is still present, then undock and
575 * hotremove all the devices that may need removing.
576 */
577static int handle_eject_request(struct dock_station *ds, u32 event)
578{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800579 if (dock_in_progress(ds))
580 return -EBUSY;
581
582 /*
583 * here we need to generate the undock
584 * event prior to actually doing the undock
585 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200586 * Also, even send the dock event if the
587 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800588 */
589 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200590
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800591 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
592 undock(ds);
Jiang Liuc9b54712013-06-29 00:24:42 +0800593 acpi_evaluate_lck(ds->handle, 0);
594 acpi_evaluate_ej0(ds->handle);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800595 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000596 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800597 return -EBUSY;
598 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700599 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800600 return 0;
601}
602
603/**
Len Brownc8f7a622006-07-09 17:22:28 -0400604 * dock_notify - act upon an acpi dock notification
605 * @handle: the dock station handle
606 * @event: the acpi event
607 * @data: our driver data struct
608 *
609 * If we are notified to dock, then check to see if the dock is
610 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800611 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400612 */
613static void dock_notify(acpi_handle handle, u32 event, void *data)
614{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200615 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800616 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800617 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400618
Shaohua Lidb350b02008-08-28 10:03:58 +0800619 /*
620 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
621 * is sent and _DCK is present, it is assumed to mean an undock
622 * request.
623 */
624 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
625 event = ACPI_NOTIFY_EJECT_REQUEST;
626
627 /*
628 * dock station: BUS_CHECK - docked or surprise removal
629 * DEVICE_CHECK - undocked
630 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
631 *
632 * To simplify event handling, dock dependent device handler always
633 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
634 * ACPI_NOTIFY_EJECT_REQUEST for removal
635 */
Len Brownc8f7a622006-07-09 17:22:28 -0400636 switch (event) {
637 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800638 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800639 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
640 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400641 begin_dock(ds);
642 dock(ds);
643 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000644 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800645 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400646 break;
647 }
648 atomic_notifier_call_chain(&dock_notifier_list,
649 event, NULL);
650 hotplug_dock_devices(ds, event);
651 complete_dock(ds);
652 dock_event(ds, event, DOCK_EVENT);
Jiang Liuc9b54712013-06-29 00:24:42 +0800653 acpi_evaluate_lck(ds->handle, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800654 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800655 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400656 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800657 if (dock_present(ds) || dock_in_progress(ds))
658 break;
659 /* This is a surprise removal */
660 surprise_removal = 1;
661 event = ACPI_NOTIFY_EJECT_REQUEST;
662 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400663 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700664 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800665 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
666 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700667 handle_eject_request(ds, event);
668 else
669 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400670 break;
671 default:
Toshi Kanicd730182012-11-20 23:42:30 +0000672 acpi_handle_err(handle, "Unknown dock event %d\n", event);
Len Brownc8f7a622006-07-09 17:22:28 -0400673 }
674}
675
Zhang Rui19cd8472008-08-28 10:05:06 +0800676struct dock_data {
677 acpi_handle handle;
678 unsigned long event;
679 struct dock_station *ds;
680};
681
682static void acpi_dock_deferred_cb(void *context)
683{
Alex Chiang747479a2009-10-19 15:14:50 -0600684 struct dock_data *data = context;
Zhang Rui19cd8472008-08-28 10:05:06 +0800685
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100686 acpi_scan_lock_acquire();
Zhang Rui19cd8472008-08-28 10:05:06 +0800687 dock_notify(data->handle, data->event, data->ds);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100688 acpi_scan_lock_release();
Zhang Rui19cd8472008-08-28 10:05:06 +0800689 kfree(data);
690}
691
Shaohua Li6bd00a62008-08-28 10:04:29 +0800692static int acpi_dock_notifier_call(struct notifier_block *this,
693 unsigned long event, void *data)
694{
695 struct dock_station *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600696 acpi_handle handle = data;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800697
698 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
699 && event != ACPI_NOTIFY_EJECT_REQUEST)
700 return 0;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100701
702 acpi_scan_lock_acquire();
703
Alex Chiang50d716e2009-10-01 11:59:23 -0600704 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li6bd00a62008-08-28 10:04:29 +0800705 if (dock_station->handle == handle) {
Alex Chiang747479a2009-10-19 15:14:50 -0600706 struct dock_data *dd;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100707 acpi_status status;
Zhang Rui19cd8472008-08-28 10:05:06 +0800708
Alex Chiang747479a2009-10-19 15:14:50 -0600709 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
710 if (!dd)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100711 break;
712
Alex Chiang747479a2009-10-19 15:14:50 -0600713 dd->handle = handle;
714 dd->event = event;
715 dd->ds = dock_station;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100716 status = acpi_os_hotplug_execute(acpi_dock_deferred_cb,
717 dd);
718 if (ACPI_FAILURE(status))
719 kfree(dd);
720
721 break;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800722 }
723 }
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100724
725 acpi_scan_lock_release();
Shaohua Li6bd00a62008-08-28 10:04:29 +0800726 return 0;
727}
728
729static struct notifier_block dock_acpi_notifier = {
730 .notifier_call = acpi_dock_notifier_call,
731};
732
Len Brownc8f7a622006-07-09 17:22:28 -0400733/**
734 * find_dock_devices - find devices on the dock station
735 * @handle: the handle of the device we are examining
736 * @lvl: unused
737 * @context: the dock station private data
738 * @rv: unused
739 *
740 * This function is called by acpi_walk_namespace. It will
741 * check to see if an object has an _EJD method. If it does, then it
742 * will see if it is dependent on the dock station.
743 */
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200744static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
745 void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400746{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200747 struct dock_station *ds = context;
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200748 acpi_handle ejd = NULL;
Len Brownc8f7a622006-07-09 17:22:28 -0400749
Rafael J. Wysocki96c0a4d2013-06-30 23:46:02 +0200750 acpi_bus_get_ejd(handle, &ejd);
751 if (ejd == ds->handle)
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600752 add_dock_dependent_device(ds, handle);
753
Len Brownc8f7a622006-07-09 17:22:28 -0400754 return AE_OK;
755}
756
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800757/*
758 * show_docked - read method for "docked" file in sysfs
759 */
760static ssize_t show_docked(struct device *dev,
761 struct device_attribute *attr, char *buf)
762{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100763 struct acpi_device *tmp;
764
Alex Chiangfe06fba2009-10-19 15:14:45 -0600765 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800766
Yasuaki Ishimatsu02df7342013-01-31 03:23:53 +0000767 if (!acpi_bus_get_device(dock_station->handle, &tmp))
Holger Machtfc5a9f82009-01-20 12:18:24 +0100768 return snprintf(buf, PAGE_SIZE, "1\n");
769 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800770}
Adrian Bunke5685b92007-10-24 18:24:42 +0200771static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800772
773/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700774 * show_flags - read method for flags file in sysfs
775 */
776static ssize_t show_flags(struct device *dev,
777 struct device_attribute *attr, char *buf)
778{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600779 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700780 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
781
782}
Adrian Bunke5685b92007-10-24 18:24:42 +0200783static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700784
785/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800786 * write_undock - write method for "undock" file in sysfs
787 */
788static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
789 const char *buf, size_t count)
790{
791 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600792 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800793
794 if (!count)
795 return -EINVAL;
796
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200797 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100798 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800799 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200800 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800801 return ret ? ret: count;
802}
Adrian Bunke5685b92007-10-24 18:24:42 +0200803static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800804
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800805/*
806 * show_dock_uid - read method for "uid" file in sysfs
807 */
808static ssize_t show_dock_uid(struct device *dev,
809 struct device_attribute *attr, char *buf)
810{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400811 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600812 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700813 acpi_status status = acpi_evaluate_integer(dock_station->handle,
814 "_UID", NULL, &lbuf);
815 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800816 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700817
Matthew Wilcox27663c52008-10-10 02:22:59 -0400818 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800819}
Adrian Bunke5685b92007-10-24 18:24:42 +0200820static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800821
Shaohua Li8652b002008-08-28 10:07:45 +0800822static ssize_t show_dock_type(struct device *dev,
823 struct device_attribute *attr, char *buf)
824{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600825 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800826 char *type;
827
828 if (dock_station->flags & DOCK_IS_DOCK)
829 type = "dock_station";
830 else if (dock_station->flags & DOCK_IS_ATA)
831 type = "ata_bay";
832 else if (dock_station->flags & DOCK_IS_BAT)
833 type = "battery_bay";
834 else
835 type = "unknown";
836
837 return snprintf(buf, PAGE_SIZE, "%s\n", type);
838}
839static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
840
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600841static struct attribute *dock_attributes[] = {
842 &dev_attr_docked.attr,
843 &dev_attr_flags.attr,
844 &dev_attr_undock.attr,
845 &dev_attr_uid.attr,
846 &dev_attr_type.attr,
847 NULL
848};
849
850static struct attribute_group dock_attribute_group = {
851 .attrs = dock_attributes
852};
853
Len Brownc8f7a622006-07-09 17:22:28 -0400854/**
855 * dock_add - add a new dock station
856 * @handle: the dock station handle
857 *
858 * allocated and initialize a new dock station device. Find all devices
859 * that are on the dock station, and register for dock event notifications.
860 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200861static int __init dock_add(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400862{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600863 int ret, id;
864 struct dock_station ds, *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600865 struct platform_device *dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400866
Alex Chiangfe06fba2009-10-19 15:14:45 -0600867 id = dock_station_count;
Alex Chiang49c6fb22010-02-01 10:35:18 -0700868 memset(&ds, 0, sizeof(ds));
Alex Chiang747479a2009-10-19 15:14:50 -0600869 dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
870 if (IS_ERR(dd))
871 return PTR_ERR(dd);
Alex Chiang9751cb72009-10-19 15:14:40 -0600872
Alex Chiang747479a2009-10-19 15:14:50 -0600873 dock_station = dd->dev.platform_data;
874
Len Brownc8f7a622006-07-09 17:22:28 -0400875 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600876 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400877 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400878
Alex Chiang747479a2009-10-19 15:14:50 -0600879 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -0600880 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700881
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700882 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -0600883 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -0700884
Jiang Liuc9b54712013-06-29 00:24:42 +0800885 if (acpi_dock_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800886 dock_station->flags |= DOCK_IS_DOCK;
Jiang Liuc9b54712013-06-29 00:24:42 +0800887 if (acpi_ata_match(handle))
Shaohua Lidb350b02008-08-28 10:03:58 +0800888 dock_station->flags |= DOCK_IS_ATA;
889 if (is_battery(handle))
890 dock_station->flags |= DOCK_IS_BAT;
891
Alex Chiang747479a2009-10-19 15:14:50 -0600892 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +0800893 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600894 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -0800895
Len Brownc8f7a622006-07-09 17:22:28 -0400896 /* Find dependent devices */
897 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +0800898 ACPI_UINT32_MAX, find_dock_devices, NULL,
899 dock_station, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400900
901 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600902 ret = add_dock_dependent_device(dock_station, handle);
903 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600904 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -0400905
Shaohua Lidb350b02008-08-28 10:03:58 +0800906 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -0600907 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -0400908 return 0;
909
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600910err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -0600911 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600912err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -0600913 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +0000914 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -0400915 return ret;
916}
917
918/**
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200919 * find_dock_and_bay - look for dock stations and bays
Len Brownc8f7a622006-07-09 17:22:28 -0400920 * @handle: acpi handle of a device
921 * @lvl: unused
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200922 * @context: unused
Len Brownc8f7a622006-07-09 17:22:28 -0400923 * @rv: unused
924 *
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200925 * This is called by acpi_walk_namespace to look for dock stations and bays.
Len Brownc8f7a622006-07-09 17:22:28 -0400926 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200927static __init acpi_status
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200928find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -0400929{
Jiang Liuc9b54712013-06-29 00:24:42 +0800930 if (acpi_dock_match(handle) || is_ejectable_bay(handle))
Zhang Rui1ee4d612010-03-22 15:46:49 +0800931 dock_add(handle);
Alex Chiang747479a2009-10-19 15:14:50 -0600932
Zhang Rui1ee4d612010-03-22 15:46:49 +0800933 return AE_OK;
Len Brownc8f7a622006-07-09 17:22:28 -0400934}
935
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200936void __init acpi_dock_init(void)
Len Brownc8f7a622006-07-09 17:22:28 -0400937{
Len Brown816c2ed2008-06-24 22:57:12 -0400938 if (acpi_disabled)
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200939 return;
Len Brown816c2ed2008-06-24 22:57:12 -0400940
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200941 /* look for dock stations and bays */
Len Brownc8f7a622006-07-09 17:22:28 -0400942 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Toshi Kani8ab0ab22012-10-23 01:30:26 +0200943 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -0400944
Shaohua Lidb350b02008-08-28 10:03:58 +0800945 if (!dock_station_count) {
Toshi Kanicd730182012-11-20 23:42:30 +0000946 pr_info(PREFIX "No dock devices found.\n");
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +0200947 return;
Shaohua Lidb350b02008-08-28 10:03:58 +0800948 }
Len Brownc8f7a622006-07-09 17:22:28 -0400949
Jiang Liuf22ff552013-06-29 00:24:34 +0800950 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Shaohua Li6bd00a62008-08-28 10:04:29 +0800951 register_acpi_bus_notifier(&dock_acpi_notifier);
Toshi Kanicd730182012-11-20 23:42:30 +0000952 pr_info(PREFIX "%s: %d docks/bays found\n",
Shaohua Lidb350b02008-08-28 10:03:58 +0800953 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -0400954}