]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/dock.c
libata: remove functions now handed by ACPI dock driver
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / dock.c
CommitLineData
c8f7a62c
LB
1/*
2 * dock.c - ACPI dock station driver
3 *
4 * Copyright (C) 2006 Kristen Carlson Accardi <kristen.c.accardi@intel.com>
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/types.h>
29#include <linux/notifier.h>
671adbec 30#include <linux/platform_device.h>
914e2637 31#include <linux/jiffies.h>
62a6d7fd 32#include <linux/stddef.h>
c8f7a62c
LB
33#include <acpi/acpi_bus.h>
34#include <acpi/acpi_drivers.h>
35
7cda93e0 36#define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
c8f7a62c 37
f52fd66d 38ACPI_MODULE_NAME("dock");
c8f7a62c 39MODULE_AUTHOR("Kristen Carlson Accardi");
7cda93e0 40MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
c8f7a62c
LB
41MODULE_LICENSE("GPL");
42
a0cd35fd
KCA
43static int immediate_undock = 1;
44module_param(immediate_undock, bool, 0644);
45MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
46 "undock immediately when the undock button is pressed, 0 will cause"
47 " the driver to wait for userspace to write the undock sysfs file "
48 " before undocking");
49
c8f7a62c 50static struct atomic_notifier_head dock_notifier_list;
671adbec 51static char dock_device_name[] = "dock";
c8f7a62c 52
a340af14
FS
53static const struct acpi_device_id dock_device_ids[] = {
54 {"LNXDOCK", 0},
55 {"", 0},
56};
57MODULE_DEVICE_TABLE(acpi, dock_device_ids);
58
c8f7a62c
LB
59struct dock_station {
60 acpi_handle handle;
61 unsigned long last_dock_time;
62 u32 flags;
63 spinlock_t dd_lock;
8b0dc866 64 struct mutex hp_lock;
c8f7a62c
LB
65 struct list_head dependent_devices;
66 struct list_head hotplug_devices;
db350b08
SL
67
68 struct list_head sibiling;
69 struct platform_device *dock_device;
c8f7a62c 70};
db350b08
SL
71static LIST_HEAD(dock_stations);
72static int dock_station_count;
c8f7a62c
LB
73
74struct dock_dependent_device {
75 struct list_head list;
76 struct list_head hotplug_list;
77 acpi_handle handle;
78 acpi_notify_handler handler;
79 void *context;
80};
81
82#define DOCK_DOCKING 0x00000001
a0cd35fd 83#define DOCK_UNDOCKING 0x00000002
db350b08
SL
84#define DOCK_IS_DOCK 0x00000010
85#define DOCK_IS_ATA 0x00000020
86#define DOCK_IS_BAT 0x00000040
5669021e
KCA
87#define DOCK_EVENT 3
88#define UNDOCK_EVENT 2
c8f7a62c 89
c8f7a62c
LB
90/*****************************************************************************
91 * Dock Dependent device functions *
92 *****************************************************************************/
93/**
94 * alloc_dock_dependent_device - allocate and init a dependent device
95 * @handle: the acpi_handle of the dependent device
96 *
97 * Allocate memory for a dependent device structure for a device referenced
98 * by the acpi handle
99 */
100static struct dock_dependent_device *
101alloc_dock_dependent_device(acpi_handle handle)
102{
103 struct dock_dependent_device *dd;
104
105 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
106 if (dd) {
107 dd->handle = handle;
108 INIT_LIST_HEAD(&dd->list);
109 INIT_LIST_HEAD(&dd->hotplug_list);
110 }
111 return dd;
112}
113
114/**
115 * add_dock_dependent_device - associate a device with the dock station
116 * @ds: The dock station
117 * @dd: The dependent device
118 *
119 * Add the dependent device to the dock's dependent device list.
120 */
121static void
122add_dock_dependent_device(struct dock_station *ds,
123 struct dock_dependent_device *dd)
124{
125 spin_lock(&ds->dd_lock);
126 list_add_tail(&dd->list, &ds->dependent_devices);
127 spin_unlock(&ds->dd_lock);
128}
129
130/**
131 * dock_add_hotplug_device - associate a hotplug handler with the dock station
132 * @ds: The dock station
133 * @dd: The dependent device struct
134 *
135 * Add the dependent device to the dock's hotplug device list
136 */
137static void
138dock_add_hotplug_device(struct dock_station *ds,
139 struct dock_dependent_device *dd)
140{
8b0dc866 141 mutex_lock(&ds->hp_lock);
c8f7a62c 142 list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
8b0dc866 143 mutex_unlock(&ds->hp_lock);
c8f7a62c
LB
144}
145
146/**
147 * dock_del_hotplug_device - remove a hotplug handler from the dock station
148 * @ds: The dock station
149 * @dd: the dependent device struct
150 *
151 * Delete the dependent device from the dock's hotplug device list
152 */
153static void
154dock_del_hotplug_device(struct dock_station *ds,
155 struct dock_dependent_device *dd)
156{
8b0dc866 157 mutex_lock(&ds->hp_lock);
c8f7a62c 158 list_del(&dd->hotplug_list);
8b0dc866 159 mutex_unlock(&ds->hp_lock);
c8f7a62c
LB
160}
161
162/**
163 * find_dock_dependent_device - get a device dependent on this dock
164 * @ds: the dock station
165 * @handle: the acpi_handle of the device we want
166 *
167 * iterate over the dependent device list for this dock. If the
168 * dependent device matches the handle, return.
169 */
170static struct dock_dependent_device *
171find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
172{
173 struct dock_dependent_device *dd;
174
175 spin_lock(&ds->dd_lock);
176 list_for_each_entry(dd, &ds->dependent_devices, list) {
177 if (handle == dd->handle) {
178 spin_unlock(&ds->dd_lock);
179 return dd;
180 }
181 }
182 spin_unlock(&ds->dd_lock);
183 return NULL;
184}
185
186/*****************************************************************************
187 * Dock functions *
188 *****************************************************************************/
189/**
190 * is_dock - see if a device is a dock station
191 * @handle: acpi handle of the device
192 *
193 * If an acpi object has a _DCK method, then it is by definition a dock
194 * station, so return true.
195 */
196static int is_dock(acpi_handle handle)
197{
198 acpi_status status;
199 acpi_handle tmp;
200
201 status = acpi_get_handle(handle, "_DCK", &tmp);
202 if (ACPI_FAILURE(status))
203 return 0;
204 return 1;
205}
206
db350b08
SL
207static int is_ejectable(acpi_handle handle)
208{
209 acpi_status status;
210 acpi_handle tmp;
211
212 status = acpi_get_handle(handle, "_EJ0", &tmp);
213 if (ACPI_FAILURE(status))
214 return 0;
215 return 1;
216}
217
218static int is_ata(acpi_handle handle)
219{
220 acpi_handle tmp;
221
222 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
223 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
224 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
225 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
226 return 1;
227
228 return 0;
229}
230
231static int is_battery(acpi_handle handle)
232{
233 struct acpi_device_info *info;
234 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
235 int ret = 1;
236
237 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &buffer)))
238 return 0;
239 info = buffer.pointer;
240 if (!(info->valid & ACPI_VALID_HID))
241 ret = 0;
242 else
243 ret = !strcmp("PNP0C0A", info->hardware_id.value);
244
245 kfree(buffer.pointer);
246 return ret;
247}
248
249static int is_ejectable_bay(acpi_handle handle)
250{
251 acpi_handle phandle;
252 if (!is_ejectable(handle))
253 return 0;
254 if (is_battery(handle) || is_ata(handle))
255 return 1;
256 if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
257 return 1;
258 return 0;
259}
260
c8f7a62c
LB
261/**
262 * is_dock_device - see if a device is on a dock station
263 * @handle: acpi handle of the device
264 *
265 * If this device is either the dock station itself,
266 * or is a device dependent on the dock station, then it
267 * is a dock device
268 */
269int is_dock_device(acpi_handle handle)
270{
db350b08
SL
271 struct dock_station *dock_station;
272
273 if (!dock_station_count)
c8f7a62c
LB
274 return 0;
275
db350b08 276 if (is_dock(handle))
c8f7a62c 277 return 1;
db350b08
SL
278 list_for_each_entry(dock_station, &dock_stations, sibiling) {
279 if (find_dock_dependent_device(dock_station, handle))
280 return 1;
281 }
c8f7a62c
LB
282
283 return 0;
284}
285
286EXPORT_SYMBOL_GPL(is_dock_device);
287
288/**
289 * dock_present - see if the dock station is present.
290 * @ds: the dock station
291 *
292 * execute the _STA method. note that present does not
293 * imply that we are docked.
294 */
295static int dock_present(struct dock_station *ds)
296{
297 unsigned long sta;
298 acpi_status status;
299
300 if (ds) {
301 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
302 if (ACPI_SUCCESS(status) && sta)
303 return 1;
304 }
305 return 0;
306}
307
308
309
310/**
311 * dock_create_acpi_device - add new devices to acpi
312 * @handle - handle of the device to add
313 *
314 * This function will create a new acpi_device for the given
315 * handle if one does not exist already. This should cause
316 * acpi to scan for drivers for the given devices, and call
317 * matching driver's add routine.
318 *
319 * Returns a pointer to the acpi_device corresponding to the handle.
320 */
321static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
322{
323 struct acpi_device *device = NULL;
324 struct acpi_device *parent_device;
325 acpi_handle parent;
326 int ret;
327
328 if (acpi_bus_get_device(handle, &device)) {
329 /*
330 * no device created for this object,
331 * so we should create one.
332 */
333 acpi_get_parent(handle, &parent);
334 if (acpi_bus_get_device(parent, &parent_device))
335 parent_device = NULL;
336
337 ret = acpi_bus_add(&device, parent_device, handle,
338 ACPI_BUS_TYPE_DEVICE);
339 if (ret) {
340 pr_debug("error adding bus, %x\n",
341 -ret);
342 return NULL;
343 }
344 }
345 return device;
346}
347
348/**
349 * dock_remove_acpi_device - remove the acpi_device struct from acpi
350 * @handle - the handle of the device to remove
351 *
352 * Tell acpi to remove the acpi_device. This should cause any loaded
353 * driver to have it's remove routine called.
354 */
355static void dock_remove_acpi_device(acpi_handle handle)
356{
357 struct acpi_device *device;
358 int ret;
359
360 if (!acpi_bus_get_device(handle, &device)) {
361 ret = acpi_bus_trim(device, 1);
362 if (ret)
363 pr_debug("error removing bus, %x\n", -ret);
364 }
365}
366
367
368/**
369 * hotplug_dock_devices - insert or remove devices on the dock station
370 * @ds: the dock station
371 * @event: either bus check or eject request
372 *
373 * Some devices on the dock station need to have drivers called
374 * to perform hotplug operations after a dock event has occurred.
375 * Traverse the list of dock devices that have registered a
376 * hotplug handler, and call the handler.
377 */
378static void hotplug_dock_devices(struct dock_station *ds, u32 event)
379{
380 struct dock_dependent_device *dd;
381
8b0dc866 382 mutex_lock(&ds->hp_lock);
c8f7a62c
LB
383
384 /*
385 * First call driver specific hotplug functions
386 */
387 list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
388 if (dd->handler)
389 dd->handler(dd->handle, event, dd->context);
390 }
391
392 /*
393 * Now make sure that an acpi_device is created for each
394 * dependent device, or removed if this is an eject request.
395 * This will cause acpi_drivers to be stopped/started if they
396 * exist
397 */
398 list_for_each_entry(dd, &ds->dependent_devices, list) {
399 if (event == ACPI_NOTIFY_EJECT_REQUEST)
400 dock_remove_acpi_device(dd->handle);
401 else
402 dock_create_acpi_device(dd->handle);
403 }
8b0dc866 404 mutex_unlock(&ds->hp_lock);
c8f7a62c
LB
405}
406
407static void dock_event(struct dock_station *ds, u32 event, int num)
408{
db350b08 409 struct device *dev = &ds->dock_device->dev;
66b56821 410 char event_string[13];
79a8f70b
KCA
411 char *envp[] = { event_string, NULL };
412
413 if (num == UNDOCK_EVENT)
66b56821 414 sprintf(event_string, "EVENT=undock");
79a8f70b 415 else
66b56821 416 sprintf(event_string, "EVENT=dock");
79a8f70b 417
5669021e 418 /*
8ea86e0b
KCA
419 * Indicate that the status of the dock station has
420 * changed.
5669021e 421 */
79a8f70b 422 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
c8f7a62c
LB
423}
424
425/**
426 * eject_dock - respond to a dock eject request
427 * @ds: the dock station
428 *
429 * This is called after _DCK is called, to execute the dock station's
430 * _EJ0 method.
431 */
432static void eject_dock(struct dock_station *ds)
433{
434 struct acpi_object_list arg_list;
435 union acpi_object arg;
436 acpi_status status;
437 acpi_handle tmp;
438
439 /* all dock devices should have _EJ0, but check anyway */
440 status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
441 if (ACPI_FAILURE(status)) {
442 pr_debug("No _EJ0 support for dock device\n");
443 return;
444 }
445
446 arg_list.count = 1;
447 arg_list.pointer = &arg;
448 arg.type = ACPI_TYPE_INTEGER;
449 arg.integer.value = 1;
450
451 if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
452 &arg_list, NULL)))
453 pr_debug("Failed to evaluate _EJ0!\n");
454}
455
456/**
457 * handle_dock - handle a dock event
458 * @ds: the dock station
459 * @dock: to dock, or undock - that is the question
460 *
461 * Execute the _DCK method in response to an acpi event
462 */
463static void handle_dock(struct dock_station *ds, int dock)
464{
465 acpi_status status;
466 struct acpi_object_list arg_list;
467 union acpi_object arg;
468 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
469 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
c8f7a62c
LB
470
471 acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
c8f7a62c 472
9254bc84
DT
473 printk(KERN_INFO PREFIX "%s - %s\n",
474 (char *)name_buffer.pointer, dock ? "docking" : "undocking");
c8f7a62c
LB
475
476 /* _DCK method has one argument */
477 arg_list.count = 1;
478 arg_list.pointer = &arg;
479 arg.type = ACPI_TYPE_INTEGER;
480 arg.integer.value = dock;
481 status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
db350b08 482 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
9254bc84
DT
483 printk(KERN_ERR PREFIX "%s - failed to execute _DCK\n",
484 (char *)name_buffer.pointer);
c8f7a62c
LB
485 kfree(buffer.pointer);
486 kfree(name_buffer.pointer);
487}
488
489static inline void dock(struct dock_station *ds)
490{
491 handle_dock(ds, 1);
492}
493
494static inline void undock(struct dock_station *ds)
495{
496 handle_dock(ds, 0);
497}
498
499static inline void begin_dock(struct dock_station *ds)
500{
501 ds->flags |= DOCK_DOCKING;
502}
503
504static inline void complete_dock(struct dock_station *ds)
505{
506 ds->flags &= ~(DOCK_DOCKING);
507 ds->last_dock_time = jiffies;
508}
509
a0cd35fd
KCA
510static inline void begin_undock(struct dock_station *ds)
511{
512 ds->flags |= DOCK_UNDOCKING;
513}
514
515static inline void complete_undock(struct dock_station *ds)
516{
517 ds->flags &= ~(DOCK_UNDOCKING);
518}
519
406f692d
SL
520static void dock_lock(struct dock_station *ds, int lock)
521{
522 struct acpi_object_list arg_list;
523 union acpi_object arg;
524 acpi_status status;
525
526 arg_list.count = 1;
527 arg_list.pointer = &arg;
528 arg.type = ACPI_TYPE_INTEGER;
529 arg.integer.value = !!lock;
530 status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
531 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
532 if (lock)
533 printk(KERN_WARNING PREFIX "Locking device failed\n");
534 else
535 printk(KERN_WARNING PREFIX "Unlocking device failed\n");
536 }
537}
538
c8f7a62c
LB
539/**
540 * dock_in_progress - see if we are in the middle of handling a dock event
541 * @ds: the dock station
542 *
543 * Sometimes while docking, false dock events can be sent to the driver
544 * because good connections aren't made or some other reason. Ignore these
545 * if we are in the middle of doing something.
546 */
547static int dock_in_progress(struct dock_station *ds)
548{
549 if ((ds->flags & DOCK_DOCKING) ||
550 time_before(jiffies, (ds->last_dock_time + HZ)))
551 return 1;
552 return 0;
553}
554
555/**
556 * register_dock_notifier - add yourself to the dock notifier list
557 * @nb: the callers notifier block
558 *
559 * If a driver wishes to be notified about dock events, they can
560 * use this function to put a notifier block on the dock notifier list.
561 * this notifier call chain will be called after a dock event, but
562 * before hotplugging any new devices.
563 */
564int register_dock_notifier(struct notifier_block *nb)
565{
db350b08 566 if (!dock_station_count)
2548c06b
PB
567 return -ENODEV;
568
c8f7a62c
LB
569 return atomic_notifier_chain_register(&dock_notifier_list, nb);
570}
571
572EXPORT_SYMBOL_GPL(register_dock_notifier);
573
574/**
575 * unregister_dock_notifier - remove yourself from the dock notifier list
576 * @nb: the callers notifier block
577 */
578void unregister_dock_notifier(struct notifier_block *nb)
579{
db350b08 580 if (!dock_station_count)
2548c06b
PB
581 return;
582
c8f7a62c
LB
583 atomic_notifier_chain_unregister(&dock_notifier_list, nb);
584}
585
586EXPORT_SYMBOL_GPL(unregister_dock_notifier);
587
588/**
589 * register_hotplug_dock_device - register a hotplug function
590 * @handle: the handle of the device
591 * @handler: the acpi_notifier_handler to call after docking
592 * @context: device specific data
593 *
594 * If a driver would like to perform a hotplug operation after a dock
595 * event, they can register an acpi_notifiy_handler to be called by
596 * the dock driver after _DCK is executed.
597 */
598int
599register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
600 void *context)
601{
602 struct dock_dependent_device *dd;
db350b08 603 struct dock_station *dock_station;
c8f7a62c 604
db350b08 605 if (!dock_station_count)
c8f7a62c
LB
606 return -ENODEV;
607
608 /*
609 * make sure this handle is for a device dependent on the dock,
610 * this would include the dock station itself
611 */
db350b08
SL
612 list_for_each_entry(dock_station, &dock_stations, sibiling) {
613 dd = find_dock_dependent_device(dock_station, handle);
614 if (dd) {
615 dd->handler = handler;
616 dd->context = context;
617 dock_add_hotplug_device(dock_station, dd);
618 return 0;
619 }
c8f7a62c
LB
620 }
621
622 return -EINVAL;
623}
624
625EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
626
627/**
628 * unregister_hotplug_dock_device - remove yourself from the hotplug list
629 * @handle: the acpi handle of the device
630 */
631void unregister_hotplug_dock_device(acpi_handle handle)
632{
633 struct dock_dependent_device *dd;
db350b08 634 struct dock_station *dock_station;
c8f7a62c 635
db350b08 636 if (!dock_station_count)
c8f7a62c
LB
637 return;
638
db350b08
SL
639 list_for_each_entry(dock_station, &dock_stations, sibiling) {
640 dd = find_dock_dependent_device(dock_station, handle);
641 if (dd)
642 dock_del_hotplug_device(dock_station, dd);
643 }
c8f7a62c
LB
644}
645
646EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
647
c80fdbe8 648/**
649 * handle_eject_request - handle an undock request checking for error conditions
650 *
651 * Check to make sure the dock device is still present, then undock and
652 * hotremove all the devices that may need removing.
653 */
654static int handle_eject_request(struct dock_station *ds, u32 event)
655{
c80fdbe8 656 if (dock_in_progress(ds))
657 return -EBUSY;
658
659 /*
660 * here we need to generate the undock
661 * event prior to actually doing the undock
662 * so that the device struct still exists.
afd7301d
HM
663 * Also, even send the dock event if the
664 * device is not present anymore
c80fdbe8 665 */
666 dock_event(ds, event, UNDOCK_EVENT);
afd7301d 667
c80fdbe8 668 hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
669 undock(ds);
406f692d 670 dock_lock(ds, 0);
c80fdbe8 671 eject_dock(ds);
672 if (dock_present(ds)) {
673 printk(KERN_ERR PREFIX "Unable to undock!\n");
674 return -EBUSY;
675 }
a0cd35fd 676 complete_undock(ds);
c80fdbe8 677 return 0;
678}
679
c8f7a62c
LB
680/**
681 * dock_notify - act upon an acpi dock notification
682 * @handle: the dock station handle
683 * @event: the acpi event
684 * @data: our driver data struct
685 *
686 * If we are notified to dock, then check to see if the dock is
687 * present and then dock. Notify all drivers of the dock event,
c80fdbe8 688 * and then hotplug and devices that may need hotplugging.
c8f7a62c
LB
689 */
690static void dock_notify(acpi_handle handle, u32 event, void *data)
691{
50dd0969 692 struct dock_station *ds = data;
8b59560a 693 struct acpi_device *tmp;
db350b08 694 int surprise_removal = 0;
c8f7a62c 695
db350b08
SL
696 /*
697 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
698 * is sent and _DCK is present, it is assumed to mean an undock
699 * request.
700 */
701 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
702 event = ACPI_NOTIFY_EJECT_REQUEST;
703
704 /*
705 * dock station: BUS_CHECK - docked or surprise removal
706 * DEVICE_CHECK - undocked
707 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
708 *
709 * To simplify event handling, dock dependent device handler always
710 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
711 * ACPI_NOTIFY_EJECT_REQUEST for removal
712 */
c8f7a62c
LB
713 switch (event) {
714 case ACPI_NOTIFY_BUS_CHECK:
db350b08 715 case ACPI_NOTIFY_DEVICE_CHECK:
8b59560a
SL
716 if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
717 &tmp)) {
c8f7a62c
LB
718 begin_dock(ds);
719 dock(ds);
720 if (!dock_present(ds)) {
721 printk(KERN_ERR PREFIX "Unable to dock!\n");
8b59560a 722 complete_dock(ds);
c8f7a62c
LB
723 break;
724 }
725 atomic_notifier_call_chain(&dock_notifier_list,
726 event, NULL);
727 hotplug_dock_devices(ds, event);
728 complete_dock(ds);
729 dock_event(ds, event, DOCK_EVENT);
406f692d 730 dock_lock(ds, 1);
db350b08 731 break;
c8f7a62c 732 }
db350b08
SL
733 if (dock_present(ds) || dock_in_progress(ds))
734 break;
735 /* This is a surprise removal */
736 surprise_removal = 1;
737 event = ACPI_NOTIFY_EJECT_REQUEST;
738 /* Fall back */
c8f7a62c 739 case ACPI_NOTIFY_EJECT_REQUEST:
a0cd35fd 740 begin_undock(ds);
f730ae18
SL
741 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
742 || surprise_removal)
a0cd35fd
KCA
743 handle_eject_request(ds, event);
744 else
745 dock_event(ds, event, UNDOCK_EVENT);
c8f7a62c
LB
746 break;
747 default:
748 printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
749 }
750}
751
19cd847a
ZR
752struct dock_data {
753 acpi_handle handle;
754 unsigned long event;
755 struct dock_station *ds;
756};
757
758static void acpi_dock_deferred_cb(void *context)
759{
760 struct dock_data *data = (struct dock_data *)context;
761
762 dock_notify(data->handle, data->event, data->ds);
763 kfree(data);
764}
765
6bd00a61
SL
766static int acpi_dock_notifier_call(struct notifier_block *this,
767 unsigned long event, void *data)
768{
769 struct dock_station *dock_station;
770 acpi_handle handle = (acpi_handle)data;
771
772 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
773 && event != ACPI_NOTIFY_EJECT_REQUEST)
774 return 0;
775 list_for_each_entry(dock_station, &dock_stations, sibiling) {
776 if (dock_station->handle == handle) {
19cd847a
ZR
777 struct dock_data *dock_data;
778
779 dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
780 if (!dock_data)
781 return 0;
782 dock_data->handle = handle;
783 dock_data->event = event;
784 dock_data->ds = dock_station;
785 acpi_os_hotplug_execute(acpi_dock_deferred_cb,
786 dock_data);
6bd00a61
SL
787 return 0 ;
788 }
789 }
790 return 0;
791}
792
793static struct notifier_block dock_acpi_notifier = {
794 .notifier_call = acpi_dock_notifier_call,
795};
796
c8f7a62c
LB
797/**
798 * find_dock_devices - find devices on the dock station
799 * @handle: the handle of the device we are examining
800 * @lvl: unused
801 * @context: the dock station private data
802 * @rv: unused
803 *
804 * This function is called by acpi_walk_namespace. It will
805 * check to see if an object has an _EJD method. If it does, then it
806 * will see if it is dependent on the dock station.
807 */
808static acpi_status
809find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
810{
811 acpi_status status;
fe9a2f77 812 acpi_handle tmp, parent;
50dd0969 813 struct dock_station *ds = context;
c8f7a62c
LB
814 struct dock_dependent_device *dd;
815
816 status = acpi_bus_get_ejd(handle, &tmp);
fe9a2f77
KCA
817 if (ACPI_FAILURE(status)) {
818 /* try the parent device as well */
819 status = acpi_get_parent(handle, &parent);
820 if (ACPI_FAILURE(status))
821 goto fdd_out;
822 /* see if parent is dependent on dock */
823 status = acpi_bus_get_ejd(parent, &tmp);
824 if (ACPI_FAILURE(status))
825 goto fdd_out;
826 }
c8f7a62c
LB
827
828 if (tmp == ds->handle) {
829 dd = alloc_dock_dependent_device(handle);
830 if (dd)
831 add_dock_dependent_device(ds, dd);
832 }
fe9a2f77 833fdd_out:
c8f7a62c
LB
834 return AE_OK;
835}
836
c80fdbe8 837/*
838 * show_docked - read method for "docked" file in sysfs
839 */
840static ssize_t show_docked(struct device *dev,
841 struct device_attribute *attr, char *buf)
842{
db350b08
SL
843 struct dock_station *dock_station = *((struct dock_station **)
844 dev->platform_data);
c80fdbe8 845 return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
846
847}
e5685b9d 848static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
c80fdbe8 849
a0cd35fd
KCA
850/*
851 * show_flags - read method for flags file in sysfs
852 */
853static ssize_t show_flags(struct device *dev,
854 struct device_attribute *attr, char *buf)
855{
db350b08
SL
856 struct dock_station *dock_station = *((struct dock_station **)
857 dev->platform_data);
a0cd35fd
KCA
858 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
859
860}
e5685b9d 861static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
a0cd35fd 862
c80fdbe8 863/*
864 * write_undock - write method for "undock" file in sysfs
865 */
866static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
867 const char *buf, size_t count)
868{
869 int ret;
db350b08
SL
870 struct dock_station *dock_station = *((struct dock_station **)
871 dev->platform_data);
c80fdbe8 872
873 if (!count)
874 return -EINVAL;
875
9171f834 876 begin_undock(dock_station);
c80fdbe8 877 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
878 return ret ? ret: count;
879}
e5685b9d 880static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
c80fdbe8 881
ac122bb6
IVE
882/*
883 * show_dock_uid - read method for "uid" file in sysfs
884 */
885static ssize_t show_dock_uid(struct device *dev,
886 struct device_attribute *attr, char *buf)
887{
888 unsigned long lbuf;
db350b08
SL
889 struct dock_station *dock_station = *((struct dock_station **)
890 dev->platform_data);
38ff4ffc
KCA
891 acpi_status status = acpi_evaluate_integer(dock_station->handle,
892 "_UID", NULL, &lbuf);
893 if (ACPI_FAILURE(status))
ac122bb6 894 return 0;
38ff4ffc 895
ac122bb6
IVE
896 return snprintf(buf, PAGE_SIZE, "%lx\n", lbuf);
897}
e5685b9d 898static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
ac122bb6 899
c8f7a62c
LB
900/**
901 * dock_add - add a new dock station
902 * @handle: the dock station handle
903 *
904 * allocated and initialize a new dock station device. Find all devices
905 * that are on the dock station, and register for dock event notifications.
906 */
907static int dock_add(acpi_handle handle)
908{
909 int ret;
c8f7a62c 910 struct dock_dependent_device *dd;
db350b08
SL
911 struct dock_station *dock_station;
912 struct platform_device *dock_device;
c8f7a62c
LB
913
914 /* allocate & initialize the dock_station private data */
915 dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
916 if (!dock_station)
917 return -ENOMEM;
918 dock_station->handle = handle;
919 dock_station->last_dock_time = jiffies - HZ;
920 INIT_LIST_HEAD(&dock_station->dependent_devices);
921 INIT_LIST_HEAD(&dock_station->hotplug_devices);
db350b08 922 INIT_LIST_HEAD(&dock_station->sibiling);
c8f7a62c 923 spin_lock_init(&dock_station->dd_lock);
8b0dc866 924 mutex_init(&dock_station->hp_lock);
07a18684 925 ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
c8f7a62c 926
671adbec 927 /* initialize platform device stuff */
db350b08
SL
928 dock_station->dock_device =
929 platform_device_register_simple(dock_device_name,
930 dock_station_count, NULL, 0);
931 dock_device = dock_station->dock_device;
0f6f2804 932 if (IS_ERR(dock_device)) {
671adbec 933 kfree(dock_station);
22fe4c21 934 dock_station = NULL;
0f6f2804 935 return PTR_ERR(dock_device);
671adbec 936 }
db350b08
SL
937 platform_device_add_data(dock_device, &dock_station,
938 sizeof(struct dock_station *));
a0cd35fd 939
9ef2a9a9
KCA
940 /* we want the dock device to send uevents */
941 dock_device->dev.uevent_suppress = 0;
942
db350b08
SL
943 if (is_dock(handle))
944 dock_station->flags |= DOCK_IS_DOCK;
945 if (is_ata(handle))
946 dock_station->flags |= DOCK_IS_ATA;
947 if (is_battery(handle))
948 dock_station->flags |= DOCK_IS_BAT;
949
0f6f2804 950 ret = device_create_file(&dock_device->dev, &dev_attr_docked);
c80fdbe8 951 if (ret) {
952 printk("Error %d adding sysfs file\n", ret);
0f6f2804 953 platform_device_unregister(dock_device);
c80fdbe8 954 kfree(dock_station);
22fe4c21 955 dock_station = NULL;
c80fdbe8 956 return ret;
957 }
0f6f2804 958 ret = device_create_file(&dock_device->dev, &dev_attr_undock);
c80fdbe8 959 if (ret) {
960 printk("Error %d adding sysfs file\n", ret);
0f6f2804
KCA
961 device_remove_file(&dock_device->dev, &dev_attr_docked);
962 platform_device_unregister(dock_device);
c80fdbe8 963 kfree(dock_station);
22fe4c21 964 dock_station = NULL;
c80fdbe8 965 return ret;
966 }
0f6f2804 967 ret = device_create_file(&dock_device->dev, &dev_attr_uid);
ac122bb6
IVE
968 if (ret) {
969 printk("Error %d adding sysfs file\n", ret);
0f6f2804
KCA
970 device_remove_file(&dock_device->dev, &dev_attr_docked);
971 device_remove_file(&dock_device->dev, &dev_attr_undock);
972 platform_device_unregister(dock_device);
ac122bb6 973 kfree(dock_station);
22fe4c21 974 dock_station = NULL;
ac122bb6
IVE
975 return ret;
976 }
a0cd35fd
KCA
977 ret = device_create_file(&dock_device->dev, &dev_attr_flags);
978 if (ret) {
979 printk("Error %d adding sysfs file\n", ret);
980 device_remove_file(&dock_device->dev, &dev_attr_docked);
981 device_remove_file(&dock_device->dev, &dev_attr_undock);
982 device_remove_file(&dock_device->dev, &dev_attr_uid);
983 platform_device_unregister(dock_device);
984 kfree(dock_station);
985 dock_station = NULL;
986 return ret;
987 }
671adbec 988
c8f7a62c
LB
989 /* Find dependent devices */
990 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
991 ACPI_UINT32_MAX, find_dock_devices, dock_station,
992 NULL);
993
994 /* add the dock station as a device dependent on itself */
995 dd = alloc_dock_dependent_device(handle);
996 if (!dd) {
997 kfree(dock_station);
22fe4c21 998 dock_station = NULL;
671adbec
KCA
999 ret = -ENOMEM;
1000 goto dock_add_err_unregister;
c8f7a62c
LB
1001 }
1002 add_dock_dependent_device(dock_station, dd);
1003
db350b08
SL
1004 dock_station_count++;
1005 list_add(&dock_station->sibiling, &dock_stations);
c8f7a62c
LB
1006 return 0;
1007
671adbec 1008dock_add_err_unregister:
0f6f2804
KCA
1009 device_remove_file(&dock_device->dev, &dev_attr_docked);
1010 device_remove_file(&dock_device->dev, &dev_attr_undock);
1011 device_remove_file(&dock_device->dev, &dev_attr_uid);
a0cd35fd 1012 device_remove_file(&dock_device->dev, &dev_attr_flags);
0f6f2804 1013 platform_device_unregister(dock_device);
671adbec 1014 kfree(dock_station);
22fe4c21 1015 dock_station = NULL;
c8f7a62c
LB
1016 return ret;
1017}
1018
1019/**
1020 * dock_remove - free up resources related to the dock station
1021 */
db350b08 1022static int dock_remove(struct dock_station *dock_station)
c8f7a62c
LB
1023{
1024 struct dock_dependent_device *dd, *tmp;
db350b08 1025 struct platform_device *dock_device = dock_station->dock_device;
c8f7a62c 1026
db350b08 1027 if (!dock_station_count)
c8f7a62c
LB
1028 return 0;
1029
1030 /* remove dependent devices */
1031 list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
1032 list)
1033 kfree(dd);
1034
671adbec 1035 /* cleanup sysfs */
0f6f2804
KCA
1036 device_remove_file(&dock_device->dev, &dev_attr_docked);
1037 device_remove_file(&dock_device->dev, &dev_attr_undock);
1038 device_remove_file(&dock_device->dev, &dev_attr_uid);
a0cd35fd 1039 device_remove_file(&dock_device->dev, &dev_attr_flags);
0f6f2804 1040 platform_device_unregister(dock_device);
671adbec 1041
c8f7a62c
LB
1042 /* free dock station memory */
1043 kfree(dock_station);
22fe4c21 1044 dock_station = NULL;
c8f7a62c
LB
1045 return 0;
1046}
1047
1048/**
1049 * find_dock - look for a dock station
1050 * @handle: acpi handle of a device
1051 * @lvl: unused
1052 * @context: counter of dock stations found
1053 * @rv: unused
1054 *
1055 * This is called by acpi_walk_namespace to look for dock stations.
1056 */
1057static acpi_status
1058find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
1059{
c8f7a62c
LB
1060 acpi_status status = AE_OK;
1061
1062 if (is_dock(handle)) {
1063 if (dock_add(handle) >= 0) {
c8f7a62c
LB
1064 status = AE_CTRL_TERMINATE;
1065 }
1066 }
1067 return status;
1068}
1069
db350b08
SL
1070static acpi_status
1071find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
c8f7a62c 1072{
db350b08
SL
1073 /* If bay is in a dock, it's already handled */
1074 if (is_ejectable_bay(handle) && !is_dock_device(handle))
1075 dock_add(handle);
1076 return AE_OK;
1077}
c8f7a62c 1078
db350b08
SL
1079static int __init dock_init(void)
1080{
816c2eda
LB
1081 if (acpi_disabled)
1082 return 0;
1083
c8f7a62c
LB
1084 /* look for a dock station */
1085 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
db350b08 1086 ACPI_UINT32_MAX, find_dock, NULL, NULL);
c8f7a62c 1087
db350b08
SL
1088 /* look for bay */
1089 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
1090 ACPI_UINT32_MAX, find_bay, NULL, NULL);
1091 if (!dock_station_count) {
1092 printk(KERN_INFO PREFIX "No dock devices found.\n");
1093 return 0;
1094 }
c8f7a62c 1095
6bd00a61 1096 register_acpi_bus_notifier(&dock_acpi_notifier);
db350b08
SL
1097 printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
1098 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
c8f7a62c
LB
1099 return 0;
1100}
1101
1102static void __exit dock_exit(void)
1103{
db350b08
SL
1104 struct dock_station *dock_station;
1105
6bd00a61 1106 unregister_acpi_bus_notifier(&dock_acpi_notifier);
db350b08
SL
1107 list_for_each_entry(dock_station, &dock_stations, sibiling)
1108 dock_remove(dock_station);
c8f7a62c
LB
1109}
1110
db350b08
SL
1111/*
1112 * Must be called before drivers of devices in dock, otherwise we can't know
1113 * which devices are in a dock
1114 */
1115subsys_initcall(dock_init);
c8f7a62c 1116module_exit(dock_exit);