blob: 826560753389ca3f1d2dd1bc2670f15f059970ae [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;
66 spinlock_t dd_lock;
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -080067 struct mutex hp_lock;
Len Brownc8f7a622006-07-09 17:22:28 -040068 struct list_head dependent_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;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020075static DEFINE_MUTEX(hotplug_lock);
Len Brownc8f7a622006-07-09 17:22:28 -040076
77struct dock_dependent_device {
78 struct list_head list;
Len Brownc8f7a622006-07-09 17:22:28 -040079 acpi_handle handle;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020080 const struct acpi_dock_ops *hp_ops;
81 void *hp_context;
82 unsigned int hp_refcount;
83 void (*hp_release)(void *);
Len Brownc8f7a622006-07-09 17:22:28 -040084};
85
86#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070087#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080088#define DOCK_IS_DOCK 0x00000010
89#define DOCK_IS_ATA 0x00000020
90#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070091#define DOCK_EVENT 3
92#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040093
Len Brownc8f7a622006-07-09 17:22:28 -040094/*****************************************************************************
95 * Dock Dependent device functions *
96 *****************************************************************************/
97/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060098 * add_dock_dependent_device - associate a device with the dock station
99 * @ds: The dock station
100 * @handle: handle of the dependent device
Len Brownc8f7a622006-07-09 17:22:28 -0400101 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600102 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -0400103 */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600104static int
105add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400106{
107 struct dock_dependent_device *dd;
108
109 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600110 if (!dd)
111 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -0400112
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600113 dd->handle = handle;
114 INIT_LIST_HEAD(&dd->list);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600115
Len Brownc8f7a622006-07-09 17:22:28 -0400116 spin_lock(&ds->dd_lock);
117 list_add_tail(&dd->list, &ds->dependent_devices);
118 spin_unlock(&ds->dd_lock);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600119
120 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400121}
122
123/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200124 * dock_init_hotplug - Initialize a hotplug device on a docking station.
125 * @dd: Dock-dependent device.
126 * @ops: Dock operations to attach to the dependent device.
127 * @context: Data to pass to the @ops callbacks and @release.
128 * @init: Optional initialization routine to run after setting up context.
129 * @release: Optional release routine to run on removal.
Len Brownc8f7a622006-07-09 17:22:28 -0400130 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200131static int dock_init_hotplug(struct dock_dependent_device *dd,
132 const struct acpi_dock_ops *ops, void *context,
133 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400134{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200135 int ret = 0;
136
137 mutex_lock(&hotplug_lock);
138
139 if (dd->hp_context) {
140 ret = -EEXIST;
141 } else {
142 dd->hp_refcount = 1;
143 dd->hp_ops = ops;
144 dd->hp_context = context;
145 dd->hp_release = release;
146 }
147
148 if (!WARN_ON(ret) && init)
149 init(context);
150
151 mutex_unlock(&hotplug_lock);
152 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400153}
154
155/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200156 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
157 * @dd: Dock-dependent device.
Len Brownc8f7a622006-07-09 17:22:28 -0400158 *
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200159 * Decrement the reference counter of @dd and if 0, detach its hotplug
160 * operations from it, reset its context pointer and run the optional release
161 * routine if present.
Len Brownc8f7a622006-07-09 17:22:28 -0400162 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200163static void dock_release_hotplug(struct dock_dependent_device *dd)
Len Brownc8f7a622006-07-09 17:22:28 -0400164{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200165 void (*release)(void *) = NULL;
166 void *context = NULL;
167
168 mutex_lock(&hotplug_lock);
169
170 if (dd->hp_context && !--dd->hp_refcount) {
171 dd->hp_ops = NULL;
172 context = dd->hp_context;
173 dd->hp_context = NULL;
174 release = dd->hp_release;
175 dd->hp_release = NULL;
176 }
177
178 if (release && context)
179 release(context);
180
181 mutex_unlock(&hotplug_lock);
182}
183
184static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
185 bool uevent)
186{
187 acpi_notify_handler cb = NULL;
188 bool run = false;
189
190 mutex_lock(&hotplug_lock);
191
192 if (dd->hp_context) {
193 run = true;
194 dd->hp_refcount++;
195 if (dd->hp_ops)
196 cb = uevent ? dd->hp_ops->uevent : dd->hp_ops->handler;
197 }
198
199 mutex_unlock(&hotplug_lock);
200
201 if (!run)
202 return;
203
204 if (cb)
205 cb(dd->handle, event, dd->hp_context);
206
207 dock_release_hotplug(dd);
Len Brownc8f7a622006-07-09 17:22:28 -0400208}
209
210/**
211 * find_dock_dependent_device - get a device dependent on this dock
212 * @ds: the dock station
213 * @handle: the acpi_handle of the device we want
214 *
215 * iterate over the dependent device list for this dock. If the
216 * dependent device matches the handle, return.
217 */
218static struct dock_dependent_device *
219find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
220{
221 struct dock_dependent_device *dd;
222
223 spin_lock(&ds->dd_lock);
224 list_for_each_entry(dd, &ds->dependent_devices, list) {
225 if (handle == dd->handle) {
226 spin_unlock(&ds->dd_lock);
227 return dd;
228 }
229 }
230 spin_unlock(&ds->dd_lock);
231 return NULL;
232}
233
234/*****************************************************************************
235 * Dock functions *
236 *****************************************************************************/
237/**
238 * is_dock - see if a device is a dock station
239 * @handle: acpi handle of the device
240 *
241 * If an acpi object has a _DCK method, then it is by definition a dock
242 * station, so return true.
243 */
244static int is_dock(acpi_handle handle)
245{
246 acpi_status status;
247 acpi_handle tmp;
248
249 status = acpi_get_handle(handle, "_DCK", &tmp);
250 if (ACPI_FAILURE(status))
251 return 0;
252 return 1;
253}
254
Shaohua Lidb350b02008-08-28 10:03:58 +0800255static int is_ejectable(acpi_handle handle)
256{
257 acpi_status status;
258 acpi_handle tmp;
259
260 status = acpi_get_handle(handle, "_EJ0", &tmp);
261 if (ACPI_FAILURE(status))
262 return 0;
263 return 1;
264}
265
266static int is_ata(acpi_handle handle)
267{
268 acpi_handle tmp;
269
270 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
271 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
272 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
273 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
274 return 1;
275
276 return 0;
277}
278
279static int is_battery(acpi_handle handle)
280{
281 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800282 int ret = 1;
283
Bob Moore15b8dd52009-06-29 13:39:29 +0800284 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800285 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800286 if (!(info->valid & ACPI_VALID_HID))
287 ret = 0;
288 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800289 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800290
Bob Moore15b8dd52009-06-29 13:39:29 +0800291 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800292 return ret;
293}
294
295static int is_ejectable_bay(acpi_handle handle)
296{
297 acpi_handle phandle;
Alex Chiang747479a2009-10-19 15:14:50 -0600298
Shaohua Lidb350b02008-08-28 10:03:58 +0800299 if (!is_ejectable(handle))
300 return 0;
301 if (is_battery(handle) || is_ata(handle))
302 return 1;
303 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
304 return 1;
305 return 0;
306}
307
Len Brownc8f7a622006-07-09 17:22:28 -0400308/**
309 * is_dock_device - see if a device is on a dock station
310 * @handle: acpi handle of the device
311 *
312 * If this device is either the dock station itself,
313 * or is a device dependent on the dock station, then it
314 * is a dock device
315 */
316int is_dock_device(acpi_handle handle)
317{
Shaohua Lidb350b02008-08-28 10:03:58 +0800318 struct dock_station *dock_station;
319
320 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400321 return 0;
322
Shaohua Lidb350b02008-08-28 10:03:58 +0800323 if (is_dock(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400324 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600325
326 list_for_each_entry(dock_station, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +0800327 if (find_dock_dependent_device(dock_station, handle))
328 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400329
330 return 0;
331}
Len Brownc8f7a622006-07-09 17:22:28 -0400332EXPORT_SYMBOL_GPL(is_dock_device);
333
334/**
335 * dock_present - see if the dock station is present.
336 * @ds: the dock station
337 *
338 * execute the _STA method. note that present does not
339 * imply that we are docked.
340 */
341static int dock_present(struct dock_station *ds)
342{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400343 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400344 acpi_status status;
345
346 if (ds) {
347 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
348 if (ACPI_SUCCESS(status) && sta)
349 return 1;
350 }
351 return 0;
352}
353
Len Brownc8f7a622006-07-09 17:22:28 -0400354/**
355 * dock_create_acpi_device - add new devices to acpi
356 * @handle - handle of the device to add
357 *
358 * This function will create a new acpi_device for the given
359 * handle if one does not exist already. This should cause
360 * acpi to scan for drivers for the given devices, and call
361 * matching driver's add routine.
362 *
363 * Returns a pointer to the acpi_device corresponding to the handle.
364 */
365static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
366{
Alex Chiang747479a2009-10-19 15:14:50 -0600367 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400368 int ret;
369
370 if (acpi_bus_get_device(handle, &device)) {
371 /*
372 * no device created for this object,
373 * so we should create one.
374 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100375 ret = acpi_bus_scan(handle);
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100376 if (ret)
Alex Chiang747479a2009-10-19 15:14:50 -0600377 pr_debug("error adding bus, %x\n", -ret);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100378
379 acpi_bus_get_device(handle, &device);
Len Brownc8f7a622006-07-09 17:22:28 -0400380 }
381 return device;
382}
383
384/**
385 * dock_remove_acpi_device - remove the acpi_device struct from acpi
386 * @handle - the handle of the device to remove
387 *
388 * Tell acpi to remove the acpi_device. This should cause any loaded
389 * driver to have it's remove routine called.
390 */
391static void dock_remove_acpi_device(acpi_handle handle)
392{
393 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400394
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100395 if (!acpi_bus_get_device(handle, &device))
396 acpi_bus_trim(device);
Len Brownc8f7a622006-07-09 17:22:28 -0400397}
398
Len Brownc8f7a622006-07-09 17:22:28 -0400399/**
400 * hotplug_dock_devices - insert or remove devices on the dock station
401 * @ds: the dock station
402 * @event: either bus check or eject request
403 *
404 * Some devices on the dock station need to have drivers called
405 * to perform hotplug operations after a dock event has occurred.
406 * Traverse the list of dock devices that have registered a
407 * hotplug handler, and call the handler.
408 */
409static void hotplug_dock_devices(struct dock_station *ds, u32 event)
410{
411 struct dock_dependent_device *dd;
412
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800413 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400414
415 /*
416 * First call driver specific hotplug functions
417 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200418 list_for_each_entry(dd, &ds->dependent_devices, list)
419 dock_hotplug_event(dd, event, false);
Len Brownc8f7a622006-07-09 17:22:28 -0400420
421 /*
422 * Now make sure that an acpi_device is created for each
423 * dependent device, or removed if this is an eject request.
424 * This will cause acpi_drivers to be stopped/started if they
425 * exist
426 */
427 list_for_each_entry(dd, &ds->dependent_devices, list) {
428 if (event == ACPI_NOTIFY_EJECT_REQUEST)
429 dock_remove_acpi_device(dd->handle);
430 else
431 dock_create_acpi_device(dd->handle);
432 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800433 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400434}
435
436static void dock_event(struct dock_station *ds, u32 event, int num)
437{
Shaohua Lidb350b02008-08-28 10:03:58 +0800438 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700439 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700440 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800441 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700442
443 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700444 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700445 else
Holger Macht66b56822007-08-10 13:10:32 -0700446 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700447
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700448 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800449 * Indicate that the status of the dock station has
450 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700451 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800452 if (num == DOCK_EVENT)
453 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
454
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200455 list_for_each_entry(dd, &ds->dependent_devices, list)
456 dock_hotplug_event(dd, event, true);
Alex Chiang747479a2009-10-19 15:14:50 -0600457
Shaohua Li1253f7a2008-08-28 10:06:16 +0800458 if (num != DOCK_EVENT)
459 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400460}
461
462/**
463 * eject_dock - respond to a dock eject request
464 * @ds: the dock station
465 *
466 * This is called after _DCK is called, to execute the dock station's
467 * _EJ0 method.
468 */
469static void eject_dock(struct dock_station *ds)
470{
471 struct acpi_object_list arg_list;
472 union acpi_object arg;
473 acpi_status status;
474 acpi_handle tmp;
475
476 /* all dock devices should have _EJ0, but check anyway */
477 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
478 if (ACPI_FAILURE(status)) {
479 pr_debug("No _EJ0 support for dock device\n");
480 return;
481 }
482
483 arg_list.count = 1;
484 arg_list.pointer = &arg;
485 arg.type = ACPI_TYPE_INTEGER;
486 arg.integer.value = 1;
487
Alex Chiang747479a2009-10-19 15:14:50 -0600488 status = acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL);
489 if (ACPI_FAILURE(status))
Len Brownc8f7a622006-07-09 17:22:28 -0400490 pr_debug("Failed to evaluate _EJ0!\n");
491}
492
493/**
494 * handle_dock - handle a dock event
495 * @ds: the dock station
496 * @dock: to dock, or undock - that is the question
497 *
498 * Execute the _DCK method in response to an acpi event
499 */
500static void handle_dock(struct dock_station *ds, int dock)
501{
502 acpi_status status;
503 struct acpi_object_list arg_list;
504 union acpi_object arg;
505 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400506
Toshi Kanicd730182012-11-20 23:42:30 +0000507 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400508
509 /* _DCK method has one argument */
510 arg_list.count = 1;
511 arg_list.pointer = &arg;
512 arg.type = ACPI_TYPE_INTEGER;
513 arg.integer.value = dock;
514 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800515 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000516 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
517 status);
Thomas Renninger0a918a92008-10-11 00:15:04 -0400518
Len Brownc8f7a622006-07-09 17:22:28 -0400519 kfree(buffer.pointer);
Len Brownc8f7a622006-07-09 17:22:28 -0400520}
521
522static inline void dock(struct dock_station *ds)
523{
524 handle_dock(ds, 1);
525}
526
527static inline void undock(struct dock_station *ds)
528{
529 handle_dock(ds, 0);
530}
531
532static inline void begin_dock(struct dock_station *ds)
533{
534 ds->flags |= DOCK_DOCKING;
535}
536
537static inline void complete_dock(struct dock_station *ds)
538{
539 ds->flags &= ~(DOCK_DOCKING);
540 ds->last_dock_time = jiffies;
541}
542
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700543static inline void begin_undock(struct dock_station *ds)
544{
545 ds->flags |= DOCK_UNDOCKING;
546}
547
548static inline void complete_undock(struct dock_station *ds)
549{
550 ds->flags &= ~(DOCK_UNDOCKING);
551}
552
Shaohua Li406f6922008-08-28 10:03:26 +0800553static void dock_lock(struct dock_station *ds, int lock)
554{
555 struct acpi_object_list arg_list;
556 union acpi_object arg;
557 acpi_status status;
558
559 arg_list.count = 1;
560 arg_list.pointer = &arg;
561 arg.type = ACPI_TYPE_INTEGER;
562 arg.integer.value = !!lock;
563 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
564 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
565 if (lock)
Toshi Kanicd730182012-11-20 23:42:30 +0000566 acpi_handle_warn(ds->handle,
567 "Locking device failed (0x%x)\n", status);
Shaohua Li406f6922008-08-28 10:03:26 +0800568 else
Toshi Kanicd730182012-11-20 23:42:30 +0000569 acpi_handle_warn(ds->handle,
570 "Unlocking device failed (0x%x)\n", status);
Shaohua Li406f6922008-08-28 10:03:26 +0800571 }
572}
573
Len Brownc8f7a622006-07-09 17:22:28 -0400574/**
575 * dock_in_progress - see if we are in the middle of handling a dock event
576 * @ds: the dock station
577 *
578 * Sometimes while docking, false dock events can be sent to the driver
579 * because good connections aren't made or some other reason. Ignore these
580 * if we are in the middle of doing something.
581 */
582static int dock_in_progress(struct dock_station *ds)
583{
584 if ((ds->flags & DOCK_DOCKING) ||
585 time_before(jiffies, (ds->last_dock_time + HZ)))
586 return 1;
587 return 0;
588}
589
590/**
591 * register_dock_notifier - add yourself to the dock notifier list
592 * @nb: the callers notifier block
593 *
594 * If a driver wishes to be notified about dock events, they can
595 * use this function to put a notifier block on the dock notifier list.
596 * this notifier call chain will be called after a dock event, but
597 * before hotplugging any new devices.
598 */
599int register_dock_notifier(struct notifier_block *nb)
600{
Shaohua Lidb350b02008-08-28 10:03:58 +0800601 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800602 return -ENODEV;
603
Len Brownc8f7a622006-07-09 17:22:28 -0400604 return atomic_notifier_chain_register(&dock_notifier_list, nb);
605}
Len Brownc8f7a622006-07-09 17:22:28 -0400606EXPORT_SYMBOL_GPL(register_dock_notifier);
607
608/**
609 * unregister_dock_notifier - remove yourself from the dock notifier list
610 * @nb: the callers notifier block
611 */
612void unregister_dock_notifier(struct notifier_block *nb)
613{
Shaohua Lidb350b02008-08-28 10:03:58 +0800614 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800615 return;
616
Len Brownc8f7a622006-07-09 17:22:28 -0400617 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
618}
Len Brownc8f7a622006-07-09 17:22:28 -0400619EXPORT_SYMBOL_GPL(unregister_dock_notifier);
620
621/**
622 * register_hotplug_dock_device - register a hotplug function
623 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800624 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400625 * @context: device specific data
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200626 * @init: Optional initialization routine to run after registration
627 * @release: Optional release routine to run on unregistration
Len Brownc8f7a622006-07-09 17:22:28 -0400628 *
629 * If a driver would like to perform a hotplug operation after a dock
630 * event, they can register an acpi_notifiy_handler to be called by
631 * the dock driver after _DCK is executed.
632 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200633int register_hotplug_dock_device(acpi_handle handle,
634 const struct acpi_dock_ops *ops, void *context,
635 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400636{
637 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800638 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800639 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400640
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200641 if (WARN_ON(!context))
642 return -EINVAL;
643
Shaohua Lidb350b02008-08-28 10:03:58 +0800644 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400645 return -ENODEV;
646
647 /*
648 * make sure this handle is for a device dependent on the dock,
649 * this would include the dock station itself
650 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600651 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800652 /*
653 * An ATA bay can be in a dock and itself can be ejected
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800654 * separately, so there are two 'dock stations' which need the
Shaohua Li61b83692008-08-28 10:07:14 +0800655 * ops
656 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800657 dd = find_dock_dependent_device(dock_station, handle);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200658 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
Shaohua Li61b83692008-08-28 10:07:14 +0800659 ret = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400660 }
661
Shaohua Li61b83692008-08-28 10:07:14 +0800662 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400663}
Len Brownc8f7a622006-07-09 17:22:28 -0400664EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
665
666/**
667 * unregister_hotplug_dock_device - remove yourself from the hotplug list
668 * @handle: the acpi handle of the device
669 */
670void unregister_hotplug_dock_device(acpi_handle handle)
671{
672 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800673 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400674
Shaohua Lidb350b02008-08-28 10:03:58 +0800675 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400676 return;
677
Alex Chiang50d716e2009-10-01 11:59:23 -0600678 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800679 dd = find_dock_dependent_device(dock_station, handle);
680 if (dd)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200681 dock_release_hotplug(dd);
Shaohua Lidb350b02008-08-28 10:03:58 +0800682 }
Len Brownc8f7a622006-07-09 17:22:28 -0400683}
Len Brownc8f7a622006-07-09 17:22:28 -0400684EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
685
686/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800687 * handle_eject_request - handle an undock request checking for error conditions
688 *
689 * Check to make sure the dock device is still present, then undock and
690 * hotremove all the devices that may need removing.
691 */
692static int handle_eject_request(struct dock_station *ds, u32 event)
693{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800694 if (dock_in_progress(ds))
695 return -EBUSY;
696
697 /*
698 * here we need to generate the undock
699 * event prior to actually doing the undock
700 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200701 * Also, even send the dock event if the
702 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800703 */
704 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200705
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800706 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
707 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800708 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800709 eject_dock(ds);
710 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000711 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800712 return -EBUSY;
713 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700714 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800715 return 0;
716}
717
718/**
Len Brownc8f7a622006-07-09 17:22:28 -0400719 * dock_notify - act upon an acpi dock notification
720 * @handle: the dock station handle
721 * @event: the acpi event
722 * @data: our driver data struct
723 *
724 * If we are notified to dock, then check to see if the dock is
725 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800726 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400727 */
728static void dock_notify(acpi_handle handle, u32 event, void *data)
729{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200730 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800731 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800732 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400733
Shaohua Lidb350b02008-08-28 10:03:58 +0800734 /*
735 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
736 * is sent and _DCK is present, it is assumed to mean an undock
737 * request.
738 */
739 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
740 event = ACPI_NOTIFY_EJECT_REQUEST;
741
742 /*
743 * dock station: BUS_CHECK - docked or surprise removal
744 * DEVICE_CHECK - undocked
745 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
746 *
747 * To simplify event handling, dock dependent device handler always
748 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
749 * ACPI_NOTIFY_EJECT_REQUEST for removal
750 */
Len Brownc8f7a622006-07-09 17:22:28 -0400751 switch (event) {
752 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800753 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800754 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
755 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400756 begin_dock(ds);
757 dock(ds);
758 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000759 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800760 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400761 break;
762 }
763 atomic_notifier_call_chain(&dock_notifier_list,
764 event, NULL);
765 hotplug_dock_devices(ds, event);
766 complete_dock(ds);
767 dock_event(ds, event, DOCK_EVENT);
Shaohua Li406f6922008-08-28 10:03:26 +0800768 dock_lock(ds, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800769 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800770 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400771 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800772 if (dock_present(ds) || dock_in_progress(ds))
773 break;
774 /* This is a surprise removal */
775 surprise_removal = 1;
776 event = ACPI_NOTIFY_EJECT_REQUEST;
777 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400778 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700779 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800780 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
781 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700782 handle_eject_request(ds, event);
783 else
784 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400785 break;
786 default:
Toshi Kanicd730182012-11-20 23:42:30 +0000787 acpi_handle_err(handle, "Unknown dock event %d\n", event);
Len Brownc8f7a622006-07-09 17:22:28 -0400788 }
789}
790
Zhang Rui19cd8472008-08-28 10:05:06 +0800791struct dock_data {
792 acpi_handle handle;
793 unsigned long event;
794 struct dock_station *ds;
795};
796
797static void acpi_dock_deferred_cb(void *context)
798{
Alex Chiang747479a2009-10-19 15:14:50 -0600799 struct dock_data *data = context;
Zhang Rui19cd8472008-08-28 10:05:06 +0800800
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100801 acpi_scan_lock_acquire();
Zhang Rui19cd8472008-08-28 10:05:06 +0800802 dock_notify(data->handle, data->event, data->ds);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100803 acpi_scan_lock_release();
Zhang Rui19cd8472008-08-28 10:05:06 +0800804 kfree(data);
805}
806
Shaohua Li6bd00a62008-08-28 10:04:29 +0800807static int acpi_dock_notifier_call(struct notifier_block *this,
808 unsigned long event, void *data)
809{
810 struct dock_station *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600811 acpi_handle handle = data;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800812
813 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
814 && event != ACPI_NOTIFY_EJECT_REQUEST)
815 return 0;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100816
817 acpi_scan_lock_acquire();
818
Alex Chiang50d716e2009-10-01 11:59:23 -0600819 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li6bd00a62008-08-28 10:04:29 +0800820 if (dock_station->handle == handle) {
Alex Chiang747479a2009-10-19 15:14:50 -0600821 struct dock_data *dd;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100822 acpi_status status;
Zhang Rui19cd8472008-08-28 10:05:06 +0800823
Alex Chiang747479a2009-10-19 15:14:50 -0600824 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
825 if (!dd)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100826 break;
827
Alex Chiang747479a2009-10-19 15:14:50 -0600828 dd->handle = handle;
829 dd->event = event;
830 dd->ds = dock_station;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100831 status = acpi_os_hotplug_execute(acpi_dock_deferred_cb,
832 dd);
833 if (ACPI_FAILURE(status))
834 kfree(dd);
835
836 break;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800837 }
838 }
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100839
840 acpi_scan_lock_release();
Shaohua Li6bd00a62008-08-28 10:04:29 +0800841 return 0;
842}
843
844static struct notifier_block dock_acpi_notifier = {
845 .notifier_call = acpi_dock_notifier_call,
846};
847
Len Brownc8f7a622006-07-09 17:22:28 -0400848/**
849 * find_dock_devices - find devices on the dock station
850 * @handle: the handle of the device we are examining
851 * @lvl: unused
852 * @context: the dock station private data
853 * @rv: unused
854 *
855 * This function is called by acpi_walk_namespace. It will
856 * check to see if an object has an _EJD method. If it does, then it
857 * will see if it is dependent on the dock station.
858 */
859static acpi_status
860find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
861{
862 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500863 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200864 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400865
866 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500867 if (ACPI_FAILURE(status)) {
868 /* try the parent device as well */
869 status = acpi_get_parent(handle, &parent);
870 if (ACPI_FAILURE(status))
871 goto fdd_out;
872 /* see if parent is dependent on dock */
873 status = acpi_bus_get_ejd(parent, &tmp);
874 if (ACPI_FAILURE(status))
875 goto fdd_out;
876 }
Len Brownc8f7a622006-07-09 17:22:28 -0400877
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600878 if (tmp == ds->handle)
879 add_dock_dependent_device(ds, handle);
880
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500881fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400882 return AE_OK;
883}
884
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800885/*
886 * show_docked - read method for "docked" file in sysfs
887 */
888static ssize_t show_docked(struct device *dev,
889 struct device_attribute *attr, char *buf)
890{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100891 struct acpi_device *tmp;
892
Alex Chiangfe06fba2009-10-19 15:14:45 -0600893 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800894
Yasuaki Ishimatsu02df7342013-01-31 03:23:53 +0000895 if (!acpi_bus_get_device(dock_station->handle, &tmp))
Holger Machtfc5a9f82009-01-20 12:18:24 +0100896 return snprintf(buf, PAGE_SIZE, "1\n");
897 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800898}
Adrian Bunke5685b92007-10-24 18:24:42 +0200899static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800900
901/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700902 * show_flags - read method for flags file in sysfs
903 */
904static ssize_t show_flags(struct device *dev,
905 struct device_attribute *attr, char *buf)
906{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600907 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700908 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
909
910}
Adrian Bunke5685b92007-10-24 18:24:42 +0200911static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700912
913/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800914 * write_undock - write method for "undock" file in sysfs
915 */
916static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
917 const char *buf, size_t count)
918{
919 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600920 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800921
922 if (!count)
923 return -EINVAL;
924
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200925 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100926 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800927 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200928 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800929 return ret ? ret: count;
930}
Adrian Bunke5685b92007-10-24 18:24:42 +0200931static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800932
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800933/*
934 * show_dock_uid - read method for "uid" file in sysfs
935 */
936static ssize_t show_dock_uid(struct device *dev,
937 struct device_attribute *attr, char *buf)
938{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400939 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600940 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700941 acpi_status status = acpi_evaluate_integer(dock_station->handle,
942 "_UID", NULL, &lbuf);
943 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800944 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700945
Matthew Wilcox27663c52008-10-10 02:22:59 -0400946 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800947}
Adrian Bunke5685b92007-10-24 18:24:42 +0200948static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800949
Shaohua Li8652b002008-08-28 10:07:45 +0800950static ssize_t show_dock_type(struct device *dev,
951 struct device_attribute *attr, char *buf)
952{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600953 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800954 char *type;
955
956 if (dock_station->flags & DOCK_IS_DOCK)
957 type = "dock_station";
958 else if (dock_station->flags & DOCK_IS_ATA)
959 type = "ata_bay";
960 else if (dock_station->flags & DOCK_IS_BAT)
961 type = "battery_bay";
962 else
963 type = "unknown";
964
965 return snprintf(buf, PAGE_SIZE, "%s\n", type);
966}
967static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
968
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600969static struct attribute *dock_attributes[] = {
970 &dev_attr_docked.attr,
971 &dev_attr_flags.attr,
972 &dev_attr_undock.attr,
973 &dev_attr_uid.attr,
974 &dev_attr_type.attr,
975 NULL
976};
977
978static struct attribute_group dock_attribute_group = {
979 .attrs = dock_attributes
980};
981
Len Brownc8f7a622006-07-09 17:22:28 -0400982/**
983 * dock_add - add a new dock station
984 * @handle: the dock station handle
985 *
986 * allocated and initialize a new dock station device. Find all devices
987 * that are on the dock station, and register for dock event notifications.
988 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200989static int __init dock_add(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400990{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600991 int ret, id;
992 struct dock_station ds, *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600993 struct platform_device *dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400994
Alex Chiangfe06fba2009-10-19 15:14:45 -0600995 id = dock_station_count;
Alex Chiang49c6fb22010-02-01 10:35:18 -0700996 memset(&ds, 0, sizeof(ds));
Alex Chiang747479a2009-10-19 15:14:50 -0600997 dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
998 if (IS_ERR(dd))
999 return PTR_ERR(dd);
Alex Chiang9751cb72009-10-19 15:14:40 -06001000
Alex Chiang747479a2009-10-19 15:14:50 -06001001 dock_station = dd->dev.platform_data;
1002
Len Brownc8f7a622006-07-09 17:22:28 -04001003 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -06001004 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -04001005 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -04001006
Len Brownc8f7a622006-07-09 17:22:28 -04001007 mutex_init(&dock_station->hp_lock);
Alex Chiang747479a2009-10-19 15:14:50 -06001008 spin_lock_init(&dock_station->dd_lock);
1009 INIT_LIST_HEAD(&dock_station->sibling);
Len Brownc8f7a622006-07-09 17:22:28 -04001010 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Alex Chiang747479a2009-10-19 15:14:50 -06001011 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001012
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -07001013 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -06001014 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -07001015
Shaohua Lidb350b02008-08-28 10:03:58 +08001016 if (is_dock(handle))
1017 dock_station->flags |= DOCK_IS_DOCK;
1018 if (is_ata(handle))
1019 dock_station->flags |= DOCK_IS_ATA;
1020 if (is_battery(handle))
1021 dock_station->flags |= DOCK_IS_BAT;
1022
Alex Chiang747479a2009-10-19 15:14:50 -06001023 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +08001024 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001025 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001026
Len Brownc8f7a622006-07-09 17:22:28 -04001027 /* Find dependent devices */
1028 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001029 ACPI_UINT32_MAX, find_dock_devices, NULL,
1030 dock_station, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001031
1032 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -06001033 ret = add_dock_dependent_device(dock_station, handle);
1034 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001035 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -04001036
Shaohua Lidb350b02008-08-28 10:03:58 +08001037 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -06001038 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -04001039 return 0;
1040
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001041err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -06001042 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001043err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -06001044 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +00001045 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -04001046 return ret;
1047}
1048
1049/**
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001050 * find_dock_and_bay - look for dock stations and bays
Len Brownc8f7a622006-07-09 17:22:28 -04001051 * @handle: acpi handle of a device
1052 * @lvl: unused
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001053 * @context: unused
Len Brownc8f7a622006-07-09 17:22:28 -04001054 * @rv: unused
1055 *
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001056 * This is called by acpi_walk_namespace to look for dock stations and bays.
Len Brownc8f7a622006-07-09 17:22:28 -04001057 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +02001058static __init acpi_status
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001059find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -04001060{
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001061 if (is_dock(handle) || is_ejectable_bay(handle))
Zhang Rui1ee4d612010-03-22 15:46:49 +08001062 dock_add(handle);
Alex Chiang747479a2009-10-19 15:14:50 -06001063
Zhang Rui1ee4d612010-03-22 15:46:49 +08001064 return AE_OK;
Len Brownc8f7a622006-07-09 17:22:28 -04001065}
1066
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +02001067void __init acpi_dock_init(void)
Len Brownc8f7a622006-07-09 17:22:28 -04001068{
Len Brown816c2ed2008-06-24 22:57:12 -04001069 if (acpi_disabled)
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +02001070 return;
Len Brown816c2ed2008-06-24 22:57:12 -04001071
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001072 /* look for dock stations and bays */
Len Brownc8f7a622006-07-09 17:22:28 -04001073 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001074 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001075
Shaohua Lidb350b02008-08-28 10:03:58 +08001076 if (!dock_station_count) {
Toshi Kanicd730182012-11-20 23:42:30 +00001077 pr_info(PREFIX "No dock devices found.\n");
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +02001078 return;
Shaohua Lidb350b02008-08-28 10:03:58 +08001079 }
Len Brownc8f7a622006-07-09 17:22:28 -04001080
Shaohua Li6bd00a62008-08-28 10:04:29 +08001081 register_acpi_bus_notifier(&dock_acpi_notifier);
Toshi Kanicd730182012-11-20 23:42:30 +00001082 pr_info(PREFIX "%s: %d docks/bays found\n",
Shaohua Lidb350b02008-08-28 10:03:58 +08001083 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -04001084}