blob: 750f958ef0bf72f498e2180a04c843dd953b7a2b [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;
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;
Shaohua Lidb350b02008-08-28 10:03:58 +080068
Alex Chiang50d716e2009-10-01 11:59:23 -060069 struct list_head sibling;
Shaohua Lidb350b02008-08-28 10:03:58 +080070 struct platform_device *dock_device;
Len Brownc8f7a622006-07-09 17:22:28 -040071};
Shaohua Lidb350b02008-08-28 10:03:58 +080072static LIST_HEAD(dock_stations);
73static int dock_station_count;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020074static DEFINE_MUTEX(hotplug_lock);
Len Brownc8f7a622006-07-09 17:22:28 -040075
76struct dock_dependent_device {
77 struct list_head list;
Len Brownc8f7a622006-07-09 17:22:28 -040078 acpi_handle handle;
Rafael J. Wysocki21a31012013-06-24 11:22:53 +020079 const struct acpi_dock_ops *hp_ops;
80 void *hp_context;
81 unsigned int hp_refcount;
82 void (*hp_release)(void *);
Len Brownc8f7a622006-07-09 17:22:28 -040083};
84
85#define DOCK_DOCKING 0x00000001
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -070086#define DOCK_UNDOCKING 0x00000002
Shaohua Lidb350b02008-08-28 10:03:58 +080087#define DOCK_IS_DOCK 0x00000010
88#define DOCK_IS_ATA 0x00000020
89#define DOCK_IS_BAT 0x00000040
Kristen Carlson Accardi56690212006-08-01 14:59:19 -070090#define DOCK_EVENT 3
91#define UNDOCK_EVENT 2
Len Brownc8f7a622006-07-09 17:22:28 -040092
Len Brownc8f7a622006-07-09 17:22:28 -040093/*****************************************************************************
94 * Dock Dependent device functions *
95 *****************************************************************************/
96/**
Alex Chiangf69cfdd2009-10-19 15:14:29 -060097 * add_dock_dependent_device - associate a device with the dock station
98 * @ds: The dock station
99 * @handle: handle of the dependent device
Len Brownc8f7a622006-07-09 17:22:28 -0400100 *
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600101 * Add the dependent device to the dock's dependent device list.
Len Brownc8f7a622006-07-09 17:22:28 -0400102 */
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600103static int
104add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400105{
106 struct dock_dependent_device *dd;
107
108 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600109 if (!dd)
110 return -ENOMEM;
Len Brownc8f7a622006-07-09 17:22:28 -0400111
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600112 dd->handle = handle;
113 INIT_LIST_HEAD(&dd->list);
Len Brownc8f7a622006-07-09 17:22:28 -0400114 list_add_tail(&dd->list, &ds->dependent_devices);
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600115
116 return 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400117}
118
119/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200120 * dock_init_hotplug - Initialize a hotplug device on a docking station.
121 * @dd: Dock-dependent device.
122 * @ops: Dock operations to attach to the dependent device.
123 * @context: Data to pass to the @ops callbacks and @release.
124 * @init: Optional initialization routine to run after setting up context.
125 * @release: Optional release routine to run on removal.
Len Brownc8f7a622006-07-09 17:22:28 -0400126 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200127static int dock_init_hotplug(struct dock_dependent_device *dd,
128 const struct acpi_dock_ops *ops, void *context,
129 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400130{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200131 int ret = 0;
132
133 mutex_lock(&hotplug_lock);
134
135 if (dd->hp_context) {
136 ret = -EEXIST;
137 } else {
138 dd->hp_refcount = 1;
139 dd->hp_ops = ops;
140 dd->hp_context = context;
141 dd->hp_release = release;
142 }
143
144 if (!WARN_ON(ret) && init)
145 init(context);
146
147 mutex_unlock(&hotplug_lock);
148 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400149}
150
151/**
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200152 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
153 * @dd: Dock-dependent device.
Len Brownc8f7a622006-07-09 17:22:28 -0400154 *
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200155 * Decrement the reference counter of @dd and if 0, detach its hotplug
156 * operations from it, reset its context pointer and run the optional release
157 * routine if present.
Len Brownc8f7a622006-07-09 17:22:28 -0400158 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200159static void dock_release_hotplug(struct dock_dependent_device *dd)
Len Brownc8f7a622006-07-09 17:22:28 -0400160{
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200161 void (*release)(void *) = NULL;
162 void *context = NULL;
163
164 mutex_lock(&hotplug_lock);
165
166 if (dd->hp_context && !--dd->hp_refcount) {
167 dd->hp_ops = NULL;
168 context = dd->hp_context;
169 dd->hp_context = NULL;
170 release = dd->hp_release;
171 dd->hp_release = NULL;
172 }
173
174 if (release && context)
175 release(context);
176
177 mutex_unlock(&hotplug_lock);
178}
179
180static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
181 bool uevent)
182{
183 acpi_notify_handler cb = NULL;
184 bool run = false;
185
186 mutex_lock(&hotplug_lock);
187
188 if (dd->hp_context) {
189 run = true;
190 dd->hp_refcount++;
191 if (dd->hp_ops)
192 cb = uevent ? dd->hp_ops->uevent : dd->hp_ops->handler;
193 }
194
195 mutex_unlock(&hotplug_lock);
196
197 if (!run)
198 return;
199
200 if (cb)
201 cb(dd->handle, event, dd->hp_context);
202
203 dock_release_hotplug(dd);
Len Brownc8f7a622006-07-09 17:22:28 -0400204}
205
206/**
207 * find_dock_dependent_device - get a device dependent on this dock
208 * @ds: the dock station
209 * @handle: the acpi_handle of the device we want
210 *
211 * iterate over the dependent device list for this dock. If the
212 * dependent device matches the handle, return.
213 */
214static struct dock_dependent_device *
215find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
216{
217 struct dock_dependent_device *dd;
218
Jiang Liued633e72013-06-29 00:24:35 +0800219 list_for_each_entry(dd, &ds->dependent_devices, list)
220 if (handle == dd->handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400221 return dd;
Jiang Liued633e72013-06-29 00:24:35 +0800222
Len Brownc8f7a622006-07-09 17:22:28 -0400223 return NULL;
224}
225
226/*****************************************************************************
227 * Dock functions *
228 *****************************************************************************/
229/**
230 * is_dock - see if a device is a dock station
231 * @handle: acpi handle of the device
232 *
233 * If an acpi object has a _DCK method, then it is by definition a dock
234 * station, so return true.
235 */
236static int is_dock(acpi_handle handle)
237{
238 acpi_status status;
239 acpi_handle tmp;
240
241 status = acpi_get_handle(handle, "_DCK", &tmp);
242 if (ACPI_FAILURE(status))
243 return 0;
244 return 1;
245}
246
Shaohua Lidb350b02008-08-28 10:03:58 +0800247static int is_ejectable(acpi_handle handle)
248{
249 acpi_status status;
250 acpi_handle tmp;
251
252 status = acpi_get_handle(handle, "_EJ0", &tmp);
253 if (ACPI_FAILURE(status))
254 return 0;
255 return 1;
256}
257
258static int is_ata(acpi_handle handle)
259{
260 acpi_handle tmp;
261
262 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
263 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
264 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
265 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
266 return 1;
267
268 return 0;
269}
270
271static int is_battery(acpi_handle handle)
272{
273 struct acpi_device_info *info;
Shaohua Lidb350b02008-08-28 10:03:58 +0800274 int ret = 1;
275
Bob Moore15b8dd52009-06-29 13:39:29 +0800276 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
Shaohua Lidb350b02008-08-28 10:03:58 +0800277 return 0;
Shaohua Lidb350b02008-08-28 10:03:58 +0800278 if (!(info->valid & ACPI_VALID_HID))
279 ret = 0;
280 else
Bob Moore15b8dd52009-06-29 13:39:29 +0800281 ret = !strcmp("PNP0C0A", info->hardware_id.string);
Shaohua Lidb350b02008-08-28 10:03:58 +0800282
Bob Moore15b8dd52009-06-29 13:39:29 +0800283 kfree(info);
Shaohua Lidb350b02008-08-28 10:03:58 +0800284 return ret;
285}
286
287static int is_ejectable_bay(acpi_handle handle)
288{
289 acpi_handle phandle;
Alex Chiang747479a2009-10-19 15:14:50 -0600290
Shaohua Lidb350b02008-08-28 10:03:58 +0800291 if (!is_ejectable(handle))
292 return 0;
293 if (is_battery(handle) || is_ata(handle))
294 return 1;
295 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
296 return 1;
297 return 0;
298}
299
Len Brownc8f7a622006-07-09 17:22:28 -0400300/**
301 * is_dock_device - see if a device is on a dock station
302 * @handle: acpi handle of the device
303 *
304 * If this device is either the dock station itself,
305 * or is a device dependent on the dock station, then it
306 * is a dock device
307 */
308int is_dock_device(acpi_handle handle)
309{
Shaohua Lidb350b02008-08-28 10:03:58 +0800310 struct dock_station *dock_station;
311
312 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400313 return 0;
314
Shaohua Lidb350b02008-08-28 10:03:58 +0800315 if (is_dock(handle))
Len Brownc8f7a622006-07-09 17:22:28 -0400316 return 1;
Alex Chiang747479a2009-10-19 15:14:50 -0600317
318 list_for_each_entry(dock_station, &dock_stations, sibling)
Shaohua Lidb350b02008-08-28 10:03:58 +0800319 if (find_dock_dependent_device(dock_station, handle))
320 return 1;
Len Brownc8f7a622006-07-09 17:22:28 -0400321
322 return 0;
323}
Len Brownc8f7a622006-07-09 17:22:28 -0400324EXPORT_SYMBOL_GPL(is_dock_device);
325
326/**
327 * dock_present - see if the dock station is present.
328 * @ds: the dock station
329 *
330 * execute the _STA method. note that present does not
331 * imply that we are docked.
332 */
333static int dock_present(struct dock_station *ds)
334{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400335 unsigned long long sta;
Len Brownc8f7a622006-07-09 17:22:28 -0400336 acpi_status status;
337
338 if (ds) {
339 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
340 if (ACPI_SUCCESS(status) && sta)
341 return 1;
342 }
343 return 0;
344}
345
Len Brownc8f7a622006-07-09 17:22:28 -0400346/**
347 * dock_create_acpi_device - add new devices to acpi
348 * @handle - handle of the device to add
349 *
350 * This function will create a new acpi_device for the given
351 * handle if one does not exist already. This should cause
352 * acpi to scan for drivers for the given devices, and call
353 * matching driver's add routine.
354 *
355 * Returns a pointer to the acpi_device corresponding to the handle.
356 */
357static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
358{
Alex Chiang747479a2009-10-19 15:14:50 -0600359 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400360 int ret;
361
362 if (acpi_bus_get_device(handle, &device)) {
363 /*
364 * no device created for this object,
365 * so we should create one.
366 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100367 ret = acpi_bus_scan(handle);
Rafael J. Wysocki636458d2012-12-21 00:36:47 +0100368 if (ret)
Alex Chiang747479a2009-10-19 15:14:50 -0600369 pr_debug("error adding bus, %x\n", -ret);
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +0100370
371 acpi_bus_get_device(handle, &device);
Len Brownc8f7a622006-07-09 17:22:28 -0400372 }
373 return device;
374}
375
376/**
377 * dock_remove_acpi_device - remove the acpi_device struct from acpi
378 * @handle - the handle of the device to remove
379 *
380 * Tell acpi to remove the acpi_device. This should cause any loaded
381 * driver to have it's remove routine called.
382 */
383static void dock_remove_acpi_device(acpi_handle handle)
384{
385 struct acpi_device *device;
Len Brownc8f7a622006-07-09 17:22:28 -0400386
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100387 if (!acpi_bus_get_device(handle, &device))
388 acpi_bus_trim(device);
Len Brownc8f7a622006-07-09 17:22:28 -0400389}
390
Len Brownc8f7a622006-07-09 17:22:28 -0400391/**
392 * hotplug_dock_devices - insert or remove devices on the dock station
393 * @ds: the dock station
394 * @event: either bus check or eject request
395 *
396 * Some devices on the dock station need to have drivers called
397 * to perform hotplug operations after a dock event has occurred.
398 * Traverse the list of dock devices that have registered a
399 * hotplug handler, and call the handler.
400 */
401static void hotplug_dock_devices(struct dock_station *ds, u32 event)
402{
403 struct dock_dependent_device *dd;
404
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800405 mutex_lock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400406
407 /*
408 * First call driver specific hotplug functions
409 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200410 list_for_each_entry(dd, &ds->dependent_devices, list)
411 dock_hotplug_event(dd, event, false);
Len Brownc8f7a622006-07-09 17:22:28 -0400412
413 /*
414 * Now make sure that an acpi_device is created for each
415 * dependent device, or removed if this is an eject request.
416 * This will cause acpi_drivers to be stopped/started if they
417 * exist
418 */
419 list_for_each_entry(dd, &ds->dependent_devices, list) {
420 if (event == ACPI_NOTIFY_EJECT_REQUEST)
421 dock_remove_acpi_device(dd->handle);
422 else
423 dock_create_acpi_device(dd->handle);
424 }
Kristen Carlson Accardi8b0dc862006-10-30 11:18:45 -0800425 mutex_unlock(&ds->hp_lock);
Len Brownc8f7a622006-07-09 17:22:28 -0400426}
427
428static void dock_event(struct dock_station *ds, u32 event, int num)
429{
Shaohua Lidb350b02008-08-28 10:03:58 +0800430 struct device *dev = &ds->dock_device->dev;
Holger Macht66b56822007-08-10 13:10:32 -0700431 char event_string[13];
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700432 char *envp[] = { event_string, NULL };
Shaohua Li1253f7a2008-08-28 10:06:16 +0800433 struct dock_dependent_device *dd;
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700434
435 if (num == UNDOCK_EVENT)
Holger Macht66b56822007-08-10 13:10:32 -0700436 sprintf(event_string, "EVENT=undock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700437 else
Holger Macht66b56822007-08-10 13:10:32 -0700438 sprintf(event_string, "EVENT=dock");
Kristen Carlson Accardi79a8f702007-05-09 15:10:22 -0700439
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700440 /*
Kristen Carlson Accardi8ea86e02006-12-11 12:05:08 -0800441 * Indicate that the status of the dock station has
442 * changed.
Kristen Carlson Accardi56690212006-08-01 14:59:19 -0700443 */
Shaohua Li1253f7a2008-08-28 10:06:16 +0800444 if (num == DOCK_EVENT)
445 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
446
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200447 list_for_each_entry(dd, &ds->dependent_devices, list)
448 dock_hotplug_event(dd, event, true);
Alex Chiang747479a2009-10-19 15:14:50 -0600449
Shaohua Li1253f7a2008-08-28 10:06:16 +0800450 if (num != DOCK_EVENT)
451 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
Len Brownc8f7a622006-07-09 17:22:28 -0400452}
453
454/**
455 * eject_dock - respond to a dock eject request
456 * @ds: the dock station
457 *
458 * This is called after _DCK is called, to execute the dock station's
459 * _EJ0 method.
460 */
461static void eject_dock(struct dock_station *ds)
462{
463 struct acpi_object_list arg_list;
464 union acpi_object arg;
465 acpi_status status;
466 acpi_handle tmp;
467
468 /* all dock devices should have _EJ0, but check anyway */
469 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
470 if (ACPI_FAILURE(status)) {
471 pr_debug("No _EJ0 support for dock device\n");
472 return;
473 }
474
475 arg_list.count = 1;
476 arg_list.pointer = &arg;
477 arg.type = ACPI_TYPE_INTEGER;
478 arg.integer.value = 1;
479
Alex Chiang747479a2009-10-19 15:14:50 -0600480 status = acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL);
481 if (ACPI_FAILURE(status))
Len Brownc8f7a622006-07-09 17:22:28 -0400482 pr_debug("Failed to evaluate _EJ0!\n");
483}
484
485/**
486 * handle_dock - handle a dock event
487 * @ds: the dock station
488 * @dock: to dock, or undock - that is the question
489 *
490 * Execute the _DCK method in response to an acpi event
491 */
492static void handle_dock(struct dock_station *ds, int dock)
493{
494 acpi_status status;
495 struct acpi_object_list arg_list;
496 union acpi_object arg;
497 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Len Brownc8f7a622006-07-09 17:22:28 -0400498
Toshi Kanicd730182012-11-20 23:42:30 +0000499 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
Len Brownc8f7a622006-07-09 17:22:28 -0400500
501 /* _DCK method has one argument */
502 arg_list.count = 1;
503 arg_list.pointer = &arg;
504 arg.type = ACPI_TYPE_INTEGER;
505 arg.integer.value = dock;
506 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
Shaohua Lidb350b02008-08-28 10:03:58 +0800507 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Toshi Kanicd730182012-11-20 23:42:30 +0000508 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
509 status);
Thomas Renninger0a918a92008-10-11 00:15:04 -0400510
Len Brownc8f7a622006-07-09 17:22:28 -0400511 kfree(buffer.pointer);
Len Brownc8f7a622006-07-09 17:22:28 -0400512}
513
514static inline void dock(struct dock_station *ds)
515{
516 handle_dock(ds, 1);
517}
518
519static inline void undock(struct dock_station *ds)
520{
521 handle_dock(ds, 0);
522}
523
524static inline void begin_dock(struct dock_station *ds)
525{
526 ds->flags |= DOCK_DOCKING;
527}
528
529static inline void complete_dock(struct dock_station *ds)
530{
531 ds->flags &= ~(DOCK_DOCKING);
532 ds->last_dock_time = jiffies;
533}
534
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700535static inline void begin_undock(struct dock_station *ds)
536{
537 ds->flags |= DOCK_UNDOCKING;
538}
539
540static inline void complete_undock(struct dock_station *ds)
541{
542 ds->flags &= ~(DOCK_UNDOCKING);
543}
544
Shaohua Li406f6922008-08-28 10:03:26 +0800545static void dock_lock(struct dock_station *ds, int lock)
546{
547 struct acpi_object_list arg_list;
548 union acpi_object arg;
549 acpi_status status;
550
551 arg_list.count = 1;
552 arg_list.pointer = &arg;
553 arg.type = ACPI_TYPE_INTEGER;
554 arg.integer.value = !!lock;
555 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
556 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
557 if (lock)
Toshi Kanicd730182012-11-20 23:42:30 +0000558 acpi_handle_warn(ds->handle,
559 "Locking device failed (0x%x)\n", status);
Shaohua Li406f6922008-08-28 10:03:26 +0800560 else
Toshi Kanicd730182012-11-20 23:42:30 +0000561 acpi_handle_warn(ds->handle,
562 "Unlocking device failed (0x%x)\n", status);
Shaohua Li406f6922008-08-28 10:03:26 +0800563 }
564}
565
Len Brownc8f7a622006-07-09 17:22:28 -0400566/**
567 * dock_in_progress - see if we are in the middle of handling a dock event
568 * @ds: the dock station
569 *
570 * Sometimes while docking, false dock events can be sent to the driver
571 * because good connections aren't made or some other reason. Ignore these
572 * if we are in the middle of doing something.
573 */
574static int dock_in_progress(struct dock_station *ds)
575{
576 if ((ds->flags & DOCK_DOCKING) ||
577 time_before(jiffies, (ds->last_dock_time + HZ)))
578 return 1;
579 return 0;
580}
581
582/**
583 * register_dock_notifier - add yourself to the dock notifier list
584 * @nb: the callers notifier block
585 *
586 * If a driver wishes to be notified about dock events, they can
587 * use this function to put a notifier block on the dock notifier list.
588 * this notifier call chain will be called after a dock event, but
589 * before hotplugging any new devices.
590 */
591int register_dock_notifier(struct notifier_block *nb)
592{
Shaohua Lidb350b02008-08-28 10:03:58 +0800593 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800594 return -ENODEV;
595
Len Brownc8f7a622006-07-09 17:22:28 -0400596 return atomic_notifier_chain_register(&dock_notifier_list, nb);
597}
Len Brownc8f7a622006-07-09 17:22:28 -0400598EXPORT_SYMBOL_GPL(register_dock_notifier);
599
600/**
601 * unregister_dock_notifier - remove yourself from the dock notifier list
602 * @nb: the callers notifier block
603 */
604void unregister_dock_notifier(struct notifier_block *nb)
605{
Shaohua Lidb350b02008-08-28 10:03:58 +0800606 if (!dock_station_count)
Prarit Bhargava2548c062006-12-04 14:50:17 -0800607 return;
608
Len Brownc8f7a622006-07-09 17:22:28 -0400609 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
610}
Len Brownc8f7a622006-07-09 17:22:28 -0400611EXPORT_SYMBOL_GPL(unregister_dock_notifier);
612
613/**
614 * register_hotplug_dock_device - register a hotplug function
615 * @handle: the handle of the device
Shaohua Li1253f7a2008-08-28 10:06:16 +0800616 * @ops: handlers to call after docking
Len Brownc8f7a622006-07-09 17:22:28 -0400617 * @context: device specific data
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200618 * @init: Optional initialization routine to run after registration
619 * @release: Optional release routine to run on unregistration
Len Brownc8f7a622006-07-09 17:22:28 -0400620 *
621 * If a driver would like to perform a hotplug operation after a dock
622 * event, they can register an acpi_notifiy_handler to be called by
623 * the dock driver after _DCK is executed.
624 */
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200625int register_hotplug_dock_device(acpi_handle handle,
626 const struct acpi_dock_ops *ops, void *context,
627 void (*init)(void *), void (*release)(void *))
Len Brownc8f7a622006-07-09 17:22:28 -0400628{
629 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800630 struct dock_station *dock_station;
Shaohua Li61b83692008-08-28 10:07:14 +0800631 int ret = -EINVAL;
Len Brownc8f7a622006-07-09 17:22:28 -0400632
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200633 if (WARN_ON(!context))
634 return -EINVAL;
635
Shaohua Lidb350b02008-08-28 10:03:58 +0800636 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400637 return -ENODEV;
638
639 /*
640 * make sure this handle is for a device dependent on the dock,
641 * this would include the dock station itself
642 */
Alex Chiang50d716e2009-10-01 11:59:23 -0600643 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li61b83692008-08-28 10:07:14 +0800644 /*
645 * An ATA bay can be in a dock and itself can be ejected
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800646 * separately, so there are two 'dock stations' which need the
Shaohua Li61b83692008-08-28 10:07:14 +0800647 * ops
648 */
Shaohua Lidb350b02008-08-28 10:03:58 +0800649 dd = find_dock_dependent_device(dock_station, handle);
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200650 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
Shaohua Li61b83692008-08-28 10:07:14 +0800651 ret = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400652 }
653
Shaohua Li61b83692008-08-28 10:07:14 +0800654 return ret;
Len Brownc8f7a622006-07-09 17:22:28 -0400655}
Len Brownc8f7a622006-07-09 17:22:28 -0400656EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
657
658/**
659 * unregister_hotplug_dock_device - remove yourself from the hotplug list
660 * @handle: the acpi handle of the device
661 */
662void unregister_hotplug_dock_device(acpi_handle handle)
663{
664 struct dock_dependent_device *dd;
Shaohua Lidb350b02008-08-28 10:03:58 +0800665 struct dock_station *dock_station;
Len Brownc8f7a622006-07-09 17:22:28 -0400666
Shaohua Lidb350b02008-08-28 10:03:58 +0800667 if (!dock_station_count)
Len Brownc8f7a622006-07-09 17:22:28 -0400668 return;
669
Alex Chiang50d716e2009-10-01 11:59:23 -0600670 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Lidb350b02008-08-28 10:03:58 +0800671 dd = find_dock_dependent_device(dock_station, handle);
672 if (dd)
Rafael J. Wysocki21a31012013-06-24 11:22:53 +0200673 dock_release_hotplug(dd);
Shaohua Lidb350b02008-08-28 10:03:58 +0800674 }
Len Brownc8f7a622006-07-09 17:22:28 -0400675}
Len Brownc8f7a622006-07-09 17:22:28 -0400676EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
677
678/**
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800679 * handle_eject_request - handle an undock request checking for error conditions
680 *
681 * Check to make sure the dock device is still present, then undock and
682 * hotremove all the devices that may need removing.
683 */
684static int handle_eject_request(struct dock_station *ds, u32 event)
685{
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800686 if (dock_in_progress(ds))
687 return -EBUSY;
688
689 /*
690 * here we need to generate the undock
691 * event prior to actually doing the undock
692 * so that the device struct still exists.
Holger Machtafd73012008-08-06 17:56:01 +0200693 * Also, even send the dock event if the
694 * device is not present anymore
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800695 */
696 dock_event(ds, event, UNDOCK_EVENT);
Holger Machtafd73012008-08-06 17:56:01 +0200697
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800698 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
699 undock(ds);
Shaohua Li406f6922008-08-28 10:03:26 +0800700 dock_lock(ds, 0);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800701 eject_dock(ds);
702 if (dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000703 acpi_handle_err(ds->handle, "Unable to undock!\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800704 return -EBUSY;
705 }
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700706 complete_undock(ds);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800707 return 0;
708}
709
710/**
Len Brownc8f7a622006-07-09 17:22:28 -0400711 * dock_notify - act upon an acpi dock notification
712 * @handle: the dock station handle
713 * @event: the acpi event
714 * @data: our driver data struct
715 *
716 * If we are notified to dock, then check to see if the dock is
717 * present and then dock. Notify all drivers of the dock event,
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800718 * and then hotplug and devices that may need hotplugging.
Len Brownc8f7a622006-07-09 17:22:28 -0400719 */
720static void dock_notify(acpi_handle handle, u32 event, void *data)
721{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200722 struct dock_station *ds = data;
Shaohua Li8b595602008-08-28 10:02:03 +0800723 struct acpi_device *tmp;
Shaohua Lidb350b02008-08-28 10:03:58 +0800724 int surprise_removal = 0;
Len Brownc8f7a622006-07-09 17:22:28 -0400725
Shaohua Lidb350b02008-08-28 10:03:58 +0800726 /*
727 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
728 * is sent and _DCK is present, it is assumed to mean an undock
729 * request.
730 */
731 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
732 event = ACPI_NOTIFY_EJECT_REQUEST;
733
734 /*
735 * dock station: BUS_CHECK - docked or surprise removal
736 * DEVICE_CHECK - undocked
737 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
738 *
739 * To simplify event handling, dock dependent device handler always
740 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
741 * ACPI_NOTIFY_EJECT_REQUEST for removal
742 */
Len Brownc8f7a622006-07-09 17:22:28 -0400743 switch (event) {
744 case ACPI_NOTIFY_BUS_CHECK:
Shaohua Lidb350b02008-08-28 10:03:58 +0800745 case ACPI_NOTIFY_DEVICE_CHECK:
Shaohua Li8b595602008-08-28 10:02:03 +0800746 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
747 &tmp)) {
Len Brownc8f7a622006-07-09 17:22:28 -0400748 begin_dock(ds);
749 dock(ds);
750 if (!dock_present(ds)) {
Toshi Kanicd730182012-11-20 23:42:30 +0000751 acpi_handle_err(handle, "Unable to dock!\n");
Shaohua Li8b595602008-08-28 10:02:03 +0800752 complete_dock(ds);
Len Brownc8f7a622006-07-09 17:22:28 -0400753 break;
754 }
755 atomic_notifier_call_chain(&dock_notifier_list,
756 event, NULL);
757 hotplug_dock_devices(ds, event);
758 complete_dock(ds);
759 dock_event(ds, event, DOCK_EVENT);
Shaohua Li406f6922008-08-28 10:03:26 +0800760 dock_lock(ds, 1);
Lin Ming3a378982010-12-13 13:36:15 +0800761 acpi_update_all_gpes();
Shaohua Lidb350b02008-08-28 10:03:58 +0800762 break;
Len Brownc8f7a622006-07-09 17:22:28 -0400763 }
Shaohua Lidb350b02008-08-28 10:03:58 +0800764 if (dock_present(ds) || dock_in_progress(ds))
765 break;
766 /* This is a surprise removal */
767 surprise_removal = 1;
768 event = ACPI_NOTIFY_EJECT_REQUEST;
769 /* Fall back */
Len Brownc8f7a622006-07-09 17:22:28 -0400770 case ACPI_NOTIFY_EJECT_REQUEST:
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700771 begin_undock(ds);
Shaohua Lif730ae12008-08-28 10:05:45 +0800772 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
773 || surprise_removal)
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700774 handle_eject_request(ds, event);
775 else
776 dock_event(ds, event, UNDOCK_EVENT);
Len Brownc8f7a622006-07-09 17:22:28 -0400777 break;
778 default:
Toshi Kanicd730182012-11-20 23:42:30 +0000779 acpi_handle_err(handle, "Unknown dock event %d\n", event);
Len Brownc8f7a622006-07-09 17:22:28 -0400780 }
781}
782
Zhang Rui19cd8472008-08-28 10:05:06 +0800783struct dock_data {
784 acpi_handle handle;
785 unsigned long event;
786 struct dock_station *ds;
787};
788
789static void acpi_dock_deferred_cb(void *context)
790{
Alex Chiang747479a2009-10-19 15:14:50 -0600791 struct dock_data *data = context;
Zhang Rui19cd8472008-08-28 10:05:06 +0800792
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100793 acpi_scan_lock_acquire();
Zhang Rui19cd8472008-08-28 10:05:06 +0800794 dock_notify(data->handle, data->event, data->ds);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100795 acpi_scan_lock_release();
Zhang Rui19cd8472008-08-28 10:05:06 +0800796 kfree(data);
797}
798
Shaohua Li6bd00a62008-08-28 10:04:29 +0800799static int acpi_dock_notifier_call(struct notifier_block *this,
800 unsigned long event, void *data)
801{
802 struct dock_station *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600803 acpi_handle handle = data;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800804
805 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
806 && event != ACPI_NOTIFY_EJECT_REQUEST)
807 return 0;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100808
809 acpi_scan_lock_acquire();
810
Alex Chiang50d716e2009-10-01 11:59:23 -0600811 list_for_each_entry(dock_station, &dock_stations, sibling) {
Shaohua Li6bd00a62008-08-28 10:04:29 +0800812 if (dock_station->handle == handle) {
Alex Chiang747479a2009-10-19 15:14:50 -0600813 struct dock_data *dd;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100814 acpi_status status;
Zhang Rui19cd8472008-08-28 10:05:06 +0800815
Alex Chiang747479a2009-10-19 15:14:50 -0600816 dd = kmalloc(sizeof(*dd), GFP_KERNEL);
817 if (!dd)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100818 break;
819
Alex Chiang747479a2009-10-19 15:14:50 -0600820 dd->handle = handle;
821 dd->event = event;
822 dd->ds = dock_station;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100823 status = acpi_os_hotplug_execute(acpi_dock_deferred_cb,
824 dd);
825 if (ACPI_FAILURE(status))
826 kfree(dd);
827
828 break;
Shaohua Li6bd00a62008-08-28 10:04:29 +0800829 }
830 }
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100831
832 acpi_scan_lock_release();
Shaohua Li6bd00a62008-08-28 10:04:29 +0800833 return 0;
834}
835
836static struct notifier_block dock_acpi_notifier = {
837 .notifier_call = acpi_dock_notifier_call,
838};
839
Len Brownc8f7a622006-07-09 17:22:28 -0400840/**
841 * find_dock_devices - find devices on the dock station
842 * @handle: the handle of the device we are examining
843 * @lvl: unused
844 * @context: the dock station private data
845 * @rv: unused
846 *
847 * This function is called by acpi_walk_namespace. It will
848 * check to see if an object has an _EJD method. If it does, then it
849 * will see if it is dependent on the dock station.
850 */
851static acpi_status
852find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
853{
854 acpi_status status;
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500855 acpi_handle tmp, parent;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200856 struct dock_station *ds = context;
Len Brownc8f7a622006-07-09 17:22:28 -0400857
858 status = acpi_bus_get_ejd(handle, &tmp);
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500859 if (ACPI_FAILURE(status)) {
860 /* try the parent device as well */
861 status = acpi_get_parent(handle, &parent);
862 if (ACPI_FAILURE(status))
863 goto fdd_out;
864 /* see if parent is dependent on dock */
865 status = acpi_bus_get_ejd(parent, &tmp);
866 if (ACPI_FAILURE(status))
867 goto fdd_out;
868 }
Len Brownc8f7a622006-07-09 17:22:28 -0400869
Alex Chiangf69cfdd2009-10-19 15:14:29 -0600870 if (tmp == ds->handle)
871 add_dock_dependent_device(ds, handle);
872
Kristen Carlson Accardife9a2f72007-02-02 22:33:00 -0500873fdd_out:
Len Brownc8f7a622006-07-09 17:22:28 -0400874 return AE_OK;
875}
876
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800877/*
878 * show_docked - read method for "docked" file in sysfs
879 */
880static ssize_t show_docked(struct device *dev,
881 struct device_attribute *attr, char *buf)
882{
Holger Machtfc5a9f82009-01-20 12:18:24 +0100883 struct acpi_device *tmp;
884
Alex Chiangfe06fba2009-10-19 15:14:45 -0600885 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800886
Yasuaki Ishimatsu02df7342013-01-31 03:23:53 +0000887 if (!acpi_bus_get_device(dock_station->handle, &tmp))
Holger Machtfc5a9f82009-01-20 12:18:24 +0100888 return snprintf(buf, PAGE_SIZE, "1\n");
889 return snprintf(buf, PAGE_SIZE, "0\n");
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800890}
Adrian Bunke5685b92007-10-24 18:24:42 +0200891static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800892
893/*
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700894 * show_flags - read method for flags file in sysfs
895 */
896static ssize_t show_flags(struct device *dev,
897 struct device_attribute *attr, char *buf)
898{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600899 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700900 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
901
902}
Adrian Bunke5685b92007-10-24 18:24:42 +0200903static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -0700904
905/*
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800906 * write_undock - write method for "undock" file in sysfs
907 */
908static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
909 const char *buf, size_t count)
910{
911 int ret;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600912 struct dock_station *dock_station = dev->platform_data;
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800913
914 if (!count)
915 return -EINVAL;
916
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200917 acpi_scan_lock_acquire();
Holger Macht9171f832008-03-12 01:07:27 +0100918 begin_undock(dock_station);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800919 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
Rafael J. Wysocki81120062013-06-16 00:38:30 +0200920 acpi_scan_lock_release();
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800921 return ret ? ret: count;
922}
Adrian Bunke5685b92007-10-24 18:24:42 +0200923static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
brandon@ifup.orgc80fdbe2006-12-04 14:49:58 -0800924
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800925/*
926 * show_dock_uid - read method for "uid" file in sysfs
927 */
928static ssize_t show_dock_uid(struct device *dev,
929 struct device_attribute *attr, char *buf)
930{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400931 unsigned long long lbuf;
Alex Chiangfe06fba2009-10-19 15:14:45 -0600932 struct dock_station *dock_station = dev->platform_data;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700933 acpi_status status = acpi_evaluate_integer(dock_station->handle,
934 "_UID", NULL, &lbuf);
935 if (ACPI_FAILURE(status))
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800936 return 0;
Kristen Carlson Accardi38ff4ff2007-05-09 15:04:24 -0700937
Matthew Wilcox27663c52008-10-10 02:22:59 -0400938 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800939}
Adrian Bunke5685b92007-10-24 18:24:42 +0200940static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
Ilya A. Volynets-Evenbakhac122bb2007-02-19 15:19:31 -0800941
Shaohua Li8652b002008-08-28 10:07:45 +0800942static ssize_t show_dock_type(struct device *dev,
943 struct device_attribute *attr, char *buf)
944{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600945 struct dock_station *dock_station = dev->platform_data;
Shaohua Li8652b002008-08-28 10:07:45 +0800946 char *type;
947
948 if (dock_station->flags & DOCK_IS_DOCK)
949 type = "dock_station";
950 else if (dock_station->flags & DOCK_IS_ATA)
951 type = "ata_bay";
952 else if (dock_station->flags & DOCK_IS_BAT)
953 type = "battery_bay";
954 else
955 type = "unknown";
956
957 return snprintf(buf, PAGE_SIZE, "%s\n", type);
958}
959static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
960
Alex Chiang5f46c2f2009-10-19 15:14:24 -0600961static struct attribute *dock_attributes[] = {
962 &dev_attr_docked.attr,
963 &dev_attr_flags.attr,
964 &dev_attr_undock.attr,
965 &dev_attr_uid.attr,
966 &dev_attr_type.attr,
967 NULL
968};
969
970static struct attribute_group dock_attribute_group = {
971 .attrs = dock_attributes
972};
973
Len Brownc8f7a622006-07-09 17:22:28 -0400974/**
975 * dock_add - add a new dock station
976 * @handle: the dock station handle
977 *
978 * allocated and initialize a new dock station device. Find all devices
979 * that are on the dock station, and register for dock event notifications.
980 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +0200981static int __init dock_add(acpi_handle handle)
Len Brownc8f7a622006-07-09 17:22:28 -0400982{
Alex Chiangfe06fba2009-10-19 15:14:45 -0600983 int ret, id;
984 struct dock_station ds, *dock_station;
Alex Chiang747479a2009-10-19 15:14:50 -0600985 struct platform_device *dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400986
Alex Chiangfe06fba2009-10-19 15:14:45 -0600987 id = dock_station_count;
Alex Chiang49c6fb22010-02-01 10:35:18 -0700988 memset(&ds, 0, sizeof(ds));
Alex Chiang747479a2009-10-19 15:14:50 -0600989 dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
990 if (IS_ERR(dd))
991 return PTR_ERR(dd);
Alex Chiang9751cb72009-10-19 15:14:40 -0600992
Alex Chiang747479a2009-10-19 15:14:50 -0600993 dock_station = dd->dev.platform_data;
994
Len Brownc8f7a622006-07-09 17:22:28 -0400995 dock_station->handle = handle;
Alex Chiang747479a2009-10-19 15:14:50 -0600996 dock_station->dock_device = dd;
Len Brownc8f7a622006-07-09 17:22:28 -0400997 dock_station->last_dock_time = jiffies - HZ;
Len Brownc8f7a622006-07-09 17:22:28 -0400998
Len Brownc8f7a622006-07-09 17:22:28 -0400999 mutex_init(&dock_station->hp_lock);
Alex Chiang747479a2009-10-19 15:14:50 -06001000 INIT_LIST_HEAD(&dock_station->sibling);
Alex Chiang747479a2009-10-19 15:14:50 -06001001 INIT_LIST_HEAD(&dock_station->dependent_devices);
Kristen Carlson Accardia0cd35f2007-05-09 15:08:15 -07001002
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -07001003 /* we want the dock device to send uevents */
Alex Chiang747479a2009-10-19 15:14:50 -06001004 dev_set_uevent_suppress(&dd->dev, 0);
Kristen Carlson Accardi9ef2a9a2007-05-09 15:09:12 -07001005
Shaohua Lidb350b02008-08-28 10:03:58 +08001006 if (is_dock(handle))
1007 dock_station->flags |= DOCK_IS_DOCK;
1008 if (is_ata(handle))
1009 dock_station->flags |= DOCK_IS_ATA;
1010 if (is_battery(handle))
1011 dock_station->flags |= DOCK_IS_BAT;
1012
Alex Chiang747479a2009-10-19 15:14:50 -06001013 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
Shaohua Li8652b002008-08-28 10:07:45 +08001014 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001015 goto err_unregister;
Kristen Carlson Accardi671adbe2006-12-04 14:49:43 -08001016
Len Brownc8f7a622006-07-09 17:22:28 -04001017 /* Find dependent devices */
1018 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Lin Ming22635762009-11-13 10:06:08 +08001019 ACPI_UINT32_MAX, find_dock_devices, NULL,
1020 dock_station, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001021
1022 /* add the dock station as a device dependent on itself */
Alex Chiangf69cfdd2009-10-19 15:14:29 -06001023 ret = add_dock_dependent_device(dock_station, handle);
1024 if (ret)
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001025 goto err_rmgroup;
Len Brownc8f7a622006-07-09 17:22:28 -04001026
Shaohua Lidb350b02008-08-28 10:03:58 +08001027 dock_station_count++;
Alex Chiang50d716e2009-10-01 11:59:23 -06001028 list_add(&dock_station->sibling, &dock_stations);
Len Brownc8f7a622006-07-09 17:22:28 -04001029 return 0;
1030
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001031err_rmgroup:
Alex Chiang747479a2009-10-19 15:14:50 -06001032 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
Alex Chiang5f46c2f2009-10-19 15:14:24 -06001033err_unregister:
Alex Chiang747479a2009-10-19 15:14:50 -06001034 platform_device_unregister(dd);
Toshi Kanicd730182012-11-20 23:42:30 +00001035 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
Len Brownc8f7a622006-07-09 17:22:28 -04001036 return ret;
1037}
1038
1039/**
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001040 * find_dock_and_bay - look for dock stations and bays
Len Brownc8f7a622006-07-09 17:22:28 -04001041 * @handle: acpi handle of a device
1042 * @lvl: unused
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001043 * @context: unused
Len Brownc8f7a622006-07-09 17:22:28 -04001044 * @rv: unused
1045 *
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001046 * This is called by acpi_walk_namespace to look for dock stations and bays.
Len Brownc8f7a622006-07-09 17:22:28 -04001047 */
Uwe Kleine-Königd38a5ed2010-10-19 09:13:39 +02001048static __init acpi_status
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001049find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
Len Brownc8f7a622006-07-09 17:22:28 -04001050{
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001051 if (is_dock(handle) || is_ejectable_bay(handle))
Zhang Rui1ee4d612010-03-22 15:46:49 +08001052 dock_add(handle);
Alex Chiang747479a2009-10-19 15:14:50 -06001053
Zhang Rui1ee4d612010-03-22 15:46:49 +08001054 return AE_OK;
Len Brownc8f7a622006-07-09 17:22:28 -04001055}
1056
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +02001057void __init acpi_dock_init(void)
Len Brownc8f7a622006-07-09 17:22:28 -04001058{
Len Brown816c2ed2008-06-24 22:57:12 -04001059 if (acpi_disabled)
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +02001060 return;
Len Brown816c2ed2008-06-24 22:57:12 -04001061
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001062 /* look for dock stations and bays */
Len Brownc8f7a622006-07-09 17:22:28 -04001063 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
Toshi Kani8ab0ab22012-10-23 01:30:26 +02001064 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
Len Brownc8f7a622006-07-09 17:22:28 -04001065
Shaohua Lidb350b02008-08-28 10:03:58 +08001066 if (!dock_station_count) {
Toshi Kanicd730182012-11-20 23:42:30 +00001067 pr_info(PREFIX "No dock devices found.\n");
Rafael J. Wysocki2ce65fe2013-07-04 13:25:04 +02001068 return;
Shaohua Lidb350b02008-08-28 10:03:58 +08001069 }
Len Brownc8f7a622006-07-09 17:22:28 -04001070
Jiang Liuf22ff552013-06-29 00:24:34 +08001071 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
Shaohua Li6bd00a62008-08-28 10:04:29 +08001072 register_acpi_bus_notifier(&dock_acpi_notifier);
Toshi Kanicd730182012-11-20 23:42:30 +00001073 pr_info(PREFIX "%s: %d docks/bays found\n",
Shaohua Lidb350b02008-08-28 10:03:58 +08001074 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
Len Brownc8f7a622006-07-09 17:22:28 -04001075}