Merge branch 'acpi-cleanup' into acpi-hotplug
[deliverable/linux.git] / drivers / acpi / dock.c
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/slab.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/notifier.h>
31 #include <linux/platform_device.h>
32 #include <linux/jiffies.h>
33 #include <linux/stddef.h>
34 #include <linux/acpi.h>
35
36 #define PREFIX "ACPI: "
37
38 #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
39
40 ACPI_MODULE_NAME("dock");
41 MODULE_AUTHOR("Kristen Carlson Accardi");
42 MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
43 MODULE_LICENSE("GPL");
44
45 static bool immediate_undock = 1;
46 module_param(immediate_undock, bool, 0644);
47 MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
48 "undock immediately when the undock button is pressed, 0 will cause"
49 " the driver to wait for userspace to write the undock sysfs file "
50 " before undocking");
51
52 static const struct acpi_device_id dock_device_ids[] = {
53 {"LNXDOCK", 0},
54 {"", 0},
55 };
56 MODULE_DEVICE_TABLE(acpi, dock_device_ids);
57
58 struct dock_station {
59 acpi_handle handle;
60 unsigned long last_dock_time;
61 u32 flags;
62 struct list_head dependent_devices;
63
64 struct list_head sibling;
65 struct platform_device *dock_device;
66 };
67 static LIST_HEAD(dock_stations);
68 static int dock_station_count;
69 static DEFINE_MUTEX(hotplug_lock);
70
71 struct dock_dependent_device {
72 struct list_head list;
73 acpi_handle handle;
74 const struct acpi_dock_ops *hp_ops;
75 void *hp_context;
76 unsigned int hp_refcount;
77 void (*hp_release)(void *);
78 };
79
80 #define DOCK_DOCKING 0x00000001
81 #define DOCK_UNDOCKING 0x00000002
82 #define DOCK_IS_DOCK 0x00000010
83 #define DOCK_IS_ATA 0x00000020
84 #define DOCK_IS_BAT 0x00000040
85 #define DOCK_EVENT 3
86 #define UNDOCK_EVENT 2
87
88 enum dock_callback_type {
89 DOCK_CALL_HANDLER,
90 DOCK_CALL_FIXUP,
91 DOCK_CALL_UEVENT,
92 };
93
94 /*****************************************************************************
95 * Dock Dependent device functions *
96 *****************************************************************************/
97 /**
98 * add_dock_dependent_device - associate a device with the dock station
99 * @ds: The dock station
100 * @handle: handle of the dependent device
101 *
102 * Add the dependent device to the dock's dependent device list.
103 */
104 static int __init
105 add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
106 {
107 struct dock_dependent_device *dd;
108
109 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
110 if (!dd)
111 return -ENOMEM;
112
113 dd->handle = handle;
114 INIT_LIST_HEAD(&dd->list);
115 list_add_tail(&dd->list, &ds->dependent_devices);
116
117 return 0;
118 }
119
120 static void remove_dock_dependent_devices(struct dock_station *ds)
121 {
122 struct dock_dependent_device *dd, *aux;
123
124 list_for_each_entry_safe(dd, aux, &ds->dependent_devices, list) {
125 list_del(&dd->list);
126 kfree(dd);
127 }
128 }
129
130 /**
131 * dock_init_hotplug - Initialize a hotplug device on a docking station.
132 * @dd: Dock-dependent device.
133 * @ops: Dock operations to attach to the dependent device.
134 * @context: Data to pass to the @ops callbacks and @release.
135 * @init: Optional initialization routine to run after setting up context.
136 * @release: Optional release routine to run on removal.
137 */
138 static int dock_init_hotplug(struct dock_dependent_device *dd,
139 const struct acpi_dock_ops *ops, void *context,
140 void (*init)(void *), void (*release)(void *))
141 {
142 int ret = 0;
143
144 mutex_lock(&hotplug_lock);
145 if (WARN_ON(dd->hp_context)) {
146 ret = -EEXIST;
147 } else {
148 dd->hp_refcount = 1;
149 dd->hp_ops = ops;
150 dd->hp_context = context;
151 dd->hp_release = release;
152 if (init)
153 init(context);
154 }
155 mutex_unlock(&hotplug_lock);
156 return ret;
157 }
158
159 /**
160 * dock_release_hotplug - Decrement hotplug reference counter of dock device.
161 * @dd: Dock-dependent device.
162 *
163 * Decrement the reference counter of @dd and if 0, detach its hotplug
164 * operations from it, reset its context pointer and run the optional release
165 * routine if present.
166 */
167 static void dock_release_hotplug(struct dock_dependent_device *dd)
168 {
169 mutex_lock(&hotplug_lock);
170 if (dd->hp_context && !--dd->hp_refcount) {
171 void (*release)(void *) = dd->hp_release;
172 void *context = dd->hp_context;
173
174 dd->hp_ops = NULL;
175 dd->hp_context = NULL;
176 dd->hp_release = NULL;
177 if (release)
178 release(context);
179 }
180 mutex_unlock(&hotplug_lock);
181 }
182
183 static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
184 enum dock_callback_type cb_type)
185 {
186 acpi_notify_handler cb = NULL;
187 bool run = false;
188
189 mutex_lock(&hotplug_lock);
190
191 if (dd->hp_context) {
192 run = true;
193 dd->hp_refcount++;
194 if (dd->hp_ops) {
195 switch (cb_type) {
196 case DOCK_CALL_FIXUP:
197 cb = dd->hp_ops->fixup;
198 break;
199 case DOCK_CALL_UEVENT:
200 cb = dd->hp_ops->uevent;
201 break;
202 default:
203 cb = dd->hp_ops->handler;
204 }
205 }
206 }
207
208 mutex_unlock(&hotplug_lock);
209
210 if (!run)
211 return;
212
213 if (cb)
214 cb(dd->handle, event, dd->hp_context);
215
216 dock_release_hotplug(dd);
217 }
218
219 /**
220 * find_dock_dependent_device - get a device dependent on this dock
221 * @ds: the dock station
222 * @handle: the acpi_handle of the device we want
223 *
224 * iterate over the dependent device list for this dock. If the
225 * dependent device matches the handle, return.
226 */
227 static struct dock_dependent_device *
228 find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
229 {
230 struct dock_dependent_device *dd;
231
232 list_for_each_entry(dd, &ds->dependent_devices, list)
233 if (handle == dd->handle)
234 return dd;
235
236 return NULL;
237 }
238
239 /*****************************************************************************
240 * Dock functions *
241 *****************************************************************************/
242 static int __init is_battery(acpi_handle handle)
243 {
244 struct acpi_device_info *info;
245 int ret = 1;
246
247 if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
248 return 0;
249 if (!(info->valid & ACPI_VALID_HID))
250 ret = 0;
251 else
252 ret = !strcmp("PNP0C0A", info->hardware_id.string);
253
254 kfree(info);
255 return ret;
256 }
257
258 /* Check whether ACPI object is an ejectable battery or disk bay */
259 static bool __init is_ejectable_bay(acpi_handle handle)
260 {
261 if (acpi_has_method(handle, "_EJ0") && is_battery(handle))
262 return true;
263
264 return acpi_bay_match(handle);
265 }
266
267 /**
268 * is_dock_device - see if a device is on a dock station
269 * @handle: acpi handle of the device
270 *
271 * If this device is either the dock station itself,
272 * or is a device dependent on the dock station, then it
273 * is a dock device
274 */
275 int is_dock_device(acpi_handle handle)
276 {
277 struct dock_station *dock_station;
278
279 if (!dock_station_count)
280 return 0;
281
282 if (acpi_dock_match(handle))
283 return 1;
284
285 list_for_each_entry(dock_station, &dock_stations, sibling)
286 if (find_dock_dependent_device(dock_station, handle))
287 return 1;
288
289 return 0;
290 }
291 EXPORT_SYMBOL_GPL(is_dock_device);
292
293 /**
294 * dock_present - see if the dock station is present.
295 * @ds: the dock station
296 *
297 * execute the _STA method. note that present does not
298 * imply that we are docked.
299 */
300 static int dock_present(struct dock_station *ds)
301 {
302 unsigned long long sta;
303 acpi_status status;
304
305 if (ds) {
306 status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
307 if (ACPI_SUCCESS(status) && sta)
308 return 1;
309 }
310 return 0;
311 }
312
313 /**
314 * dock_create_acpi_device - add new devices to acpi
315 * @handle - handle of the device to add
316 *
317 * This function will create a new acpi_device for the given
318 * handle if one does not exist already. This should cause
319 * acpi to scan for drivers for the given devices, and call
320 * matching driver's add routine.
321 */
322 static void dock_create_acpi_device(acpi_handle handle)
323 {
324 struct acpi_device *device = NULL;
325 int ret;
326
327 acpi_bus_get_device(handle, &device);
328 if (!acpi_device_enumerated(device)) {
329 ret = acpi_bus_scan(handle);
330 if (ret)
331 pr_debug("error adding bus, %x\n", -ret);
332 }
333 }
334
335 /**
336 * dock_remove_acpi_device - remove the acpi_device struct from acpi
337 * @handle - the handle of the device to remove
338 *
339 * Tell acpi to remove the acpi_device. This should cause any loaded
340 * driver to have it's remove routine called.
341 */
342 static void dock_remove_acpi_device(acpi_handle handle)
343 {
344 struct acpi_device *device;
345
346 if (!acpi_bus_get_device(handle, &device))
347 acpi_bus_trim(device);
348 }
349
350 /**
351 * hot_remove_dock_devices - Remove dock station devices.
352 * @ds: Dock station.
353 */
354 static void hot_remove_dock_devices(struct dock_station *ds)
355 {
356 struct dock_dependent_device *dd;
357
358 /*
359 * Walk the list in reverse order so that devices that have been added
360 * last are removed first (in case there are some indirect dependencies
361 * between them).
362 */
363 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
364 dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST, false);
365
366 list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
367 dock_remove_acpi_device(dd->handle);
368 }
369
370 /**
371 * hotplug_dock_devices - Insert devices on a dock station.
372 * @ds: the dock station
373 * @event: either bus check or device check request
374 *
375 * Some devices on the dock station need to have drivers called
376 * to perform hotplug operations after a dock event has occurred.
377 * Traverse the list of dock devices that have registered a
378 * hotplug handler, and call the handler.
379 */
380 static void hotplug_dock_devices(struct dock_station *ds, u32 event)
381 {
382 struct dock_dependent_device *dd;
383
384 /* Call driver specific post-dock fixups. */
385 list_for_each_entry(dd, &ds->dependent_devices, list)
386 dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
387
388 /* Call driver specific hotplug functions. */
389 list_for_each_entry(dd, &ds->dependent_devices, list)
390 dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
391
392 /*
393 * Now make sure that an acpi_device is created for each dependent
394 * device. That will cause scan handlers to be attached to device
395 * objects or acpi_drivers to be stopped/started if they are present.
396 */
397 list_for_each_entry(dd, &ds->dependent_devices, list)
398 dock_create_acpi_device(dd->handle);
399 }
400
401 static void dock_event(struct dock_station *ds, u32 event, int num)
402 {
403 struct device *dev = &ds->dock_device->dev;
404 char event_string[13];
405 char *envp[] = { event_string, NULL };
406 struct dock_dependent_device *dd;
407
408 if (num == UNDOCK_EVENT)
409 sprintf(event_string, "EVENT=undock");
410 else
411 sprintf(event_string, "EVENT=dock");
412
413 /*
414 * Indicate that the status of the dock station has
415 * changed.
416 */
417 if (num == DOCK_EVENT)
418 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
419
420 list_for_each_entry(dd, &ds->dependent_devices, list)
421 dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
422
423 if (num != DOCK_EVENT)
424 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
425 }
426
427 /**
428 * handle_dock - handle a dock event
429 * @ds: the dock station
430 * @dock: to dock, or undock - that is the question
431 *
432 * Execute the _DCK method in response to an acpi event
433 */
434 static void handle_dock(struct dock_station *ds, int dock)
435 {
436 acpi_status status;
437 struct acpi_object_list arg_list;
438 union acpi_object arg;
439 unsigned long long value;
440
441 acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
442
443 /* _DCK method has one argument */
444 arg_list.count = 1;
445 arg_list.pointer = &arg;
446 arg.type = ACPI_TYPE_INTEGER;
447 arg.integer.value = dock;
448 status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
449 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
450 acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
451 status);
452 }
453
454 static inline void dock(struct dock_station *ds)
455 {
456 handle_dock(ds, 1);
457 }
458
459 static inline void undock(struct dock_station *ds)
460 {
461 handle_dock(ds, 0);
462 }
463
464 static inline void begin_dock(struct dock_station *ds)
465 {
466 ds->flags |= DOCK_DOCKING;
467 }
468
469 static inline void complete_dock(struct dock_station *ds)
470 {
471 ds->flags &= ~(DOCK_DOCKING);
472 ds->last_dock_time = jiffies;
473 }
474
475 static inline void begin_undock(struct dock_station *ds)
476 {
477 ds->flags |= DOCK_UNDOCKING;
478 }
479
480 static inline void complete_undock(struct dock_station *ds)
481 {
482 ds->flags &= ~(DOCK_UNDOCKING);
483 }
484
485 /**
486 * dock_in_progress - see if we are in the middle of handling a dock event
487 * @ds: the dock station
488 *
489 * Sometimes while docking, false dock events can be sent to the driver
490 * because good connections aren't made or some other reason. Ignore these
491 * if we are in the middle of doing something.
492 */
493 static int dock_in_progress(struct dock_station *ds)
494 {
495 if ((ds->flags & DOCK_DOCKING) ||
496 time_before(jiffies, (ds->last_dock_time + HZ)))
497 return 1;
498 return 0;
499 }
500
501 /**
502 * register_hotplug_dock_device - register a hotplug function
503 * @handle: the handle of the device
504 * @ops: handlers to call after docking
505 * @context: device specific data
506 * @init: Optional initialization routine to run after registration
507 * @release: Optional release routine to run on unregistration
508 *
509 * If a driver would like to perform a hotplug operation after a dock
510 * event, they can register an acpi_notifiy_handler to be called by
511 * the dock driver after _DCK is executed.
512 */
513 int register_hotplug_dock_device(acpi_handle handle,
514 const struct acpi_dock_ops *ops, void *context,
515 void (*init)(void *), void (*release)(void *))
516 {
517 struct dock_dependent_device *dd;
518 struct dock_station *dock_station;
519 int ret = -EINVAL;
520
521 if (WARN_ON(!context))
522 return -EINVAL;
523
524 if (!dock_station_count)
525 return -ENODEV;
526
527 /*
528 * make sure this handle is for a device dependent on the dock,
529 * this would include the dock station itself
530 */
531 list_for_each_entry(dock_station, &dock_stations, sibling) {
532 /*
533 * An ATA bay can be in a dock and itself can be ejected
534 * separately, so there are two 'dock stations' which need the
535 * ops
536 */
537 dd = find_dock_dependent_device(dock_station, handle);
538 if (dd && !dock_init_hotplug(dd, ops, context, init, release))
539 ret = 0;
540 }
541
542 return ret;
543 }
544 EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
545
546 /**
547 * unregister_hotplug_dock_device - remove yourself from the hotplug list
548 * @handle: the acpi handle of the device
549 */
550 void unregister_hotplug_dock_device(acpi_handle handle)
551 {
552 struct dock_dependent_device *dd;
553 struct dock_station *dock_station;
554
555 if (!dock_station_count)
556 return;
557
558 list_for_each_entry(dock_station, &dock_stations, sibling) {
559 dd = find_dock_dependent_device(dock_station, handle);
560 if (dd)
561 dock_release_hotplug(dd);
562 }
563 }
564 EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
565
566 /**
567 * handle_eject_request - handle an undock request checking for error conditions
568 *
569 * Check to make sure the dock device is still present, then undock and
570 * hotremove all the devices that may need removing.
571 */
572 static int handle_eject_request(struct dock_station *ds, u32 event)
573 {
574 if (dock_in_progress(ds))
575 return -EBUSY;
576
577 /*
578 * here we need to generate the undock
579 * event prior to actually doing the undock
580 * so that the device struct still exists.
581 * Also, even send the dock event if the
582 * device is not present anymore
583 */
584 dock_event(ds, event, UNDOCK_EVENT);
585
586 hot_remove_dock_devices(ds);
587 undock(ds);
588 acpi_evaluate_lck(ds->handle, 0);
589 acpi_evaluate_ej0(ds->handle);
590 if (dock_present(ds)) {
591 acpi_handle_err(ds->handle, "Unable to undock!\n");
592 return -EBUSY;
593 }
594 complete_undock(ds);
595 return 0;
596 }
597
598 /**
599 * dock_notify - act upon an acpi dock notification
600 * @ds: dock station
601 * @event: the acpi event
602 *
603 * If we are notified to dock, then check to see if the dock is
604 * present and then dock. Notify all drivers of the dock event,
605 * and then hotplug and devices that may need hotplugging.
606 */
607 static void dock_notify(struct dock_station *ds, u32 event)
608 {
609 acpi_handle handle = ds->handle;
610 struct acpi_device *ad;
611 int surprise_removal = 0;
612
613 /*
614 * According to acpi spec 3.0a, if a DEVICE_CHECK notification
615 * is sent and _DCK is present, it is assumed to mean an undock
616 * request.
617 */
618 if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
619 event = ACPI_NOTIFY_EJECT_REQUEST;
620
621 /*
622 * dock station: BUS_CHECK - docked or surprise removal
623 * DEVICE_CHECK - undocked
624 * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
625 *
626 * To simplify event handling, dock dependent device handler always
627 * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
628 * ACPI_NOTIFY_EJECT_REQUEST for removal
629 */
630 switch (event) {
631 case ACPI_NOTIFY_BUS_CHECK:
632 case ACPI_NOTIFY_DEVICE_CHECK:
633 if (!dock_in_progress(ds) && acpi_bus_get_device(handle, &ad)) {
634 begin_dock(ds);
635 dock(ds);
636 if (!dock_present(ds)) {
637 acpi_handle_err(handle, "Unable to dock!\n");
638 complete_dock(ds);
639 break;
640 }
641 hotplug_dock_devices(ds, event);
642 complete_dock(ds);
643 dock_event(ds, event, DOCK_EVENT);
644 acpi_evaluate_lck(ds->handle, 1);
645 acpi_update_all_gpes();
646 break;
647 }
648 if (dock_present(ds) || dock_in_progress(ds))
649 break;
650 /* This is a surprise removal */
651 surprise_removal = 1;
652 event = ACPI_NOTIFY_EJECT_REQUEST;
653 /* Fall back */
654 case ACPI_NOTIFY_EJECT_REQUEST:
655 begin_undock(ds);
656 if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
657 || surprise_removal)
658 handle_eject_request(ds, event);
659 else
660 dock_event(ds, event, UNDOCK_EVENT);
661 break;
662 default:
663 acpi_handle_err(handle, "Unknown dock event %d\n", event);
664 }
665 }
666
667 static void acpi_dock_deferred_cb(void *data, u32 event)
668 {
669 acpi_scan_lock_acquire();
670 dock_notify(data, event);
671 acpi_scan_lock_release();
672 }
673
674 static void dock_notify_handler(acpi_handle handle, u32 event, void *data)
675 {
676 if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
677 && event != ACPI_NOTIFY_EJECT_REQUEST)
678 return;
679
680 acpi_hotplug_execute(acpi_dock_deferred_cb, data, event);
681 }
682
683 /**
684 * find_dock_devices - find devices on the dock station
685 * @handle: the handle of the device we are examining
686 * @lvl: unused
687 * @context: the dock station private data
688 * @rv: unused
689 *
690 * This function is called by acpi_walk_namespace. It will
691 * check to see if an object has an _EJD method. If it does, then it
692 * will see if it is dependent on the dock station.
693 */
694 static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
695 void *context, void **rv)
696 {
697 struct dock_station *ds = context;
698 acpi_handle ejd = NULL;
699
700 acpi_bus_get_ejd(handle, &ejd);
701 if (ejd == ds->handle)
702 add_dock_dependent_device(ds, handle);
703
704 return AE_OK;
705 }
706
707 /*
708 * show_docked - read method for "docked" file in sysfs
709 */
710 static ssize_t show_docked(struct device *dev,
711 struct device_attribute *attr, char *buf)
712 {
713 struct acpi_device *tmp;
714
715 struct dock_station *dock_station = dev->platform_data;
716
717 if (!acpi_bus_get_device(dock_station->handle, &tmp))
718 return snprintf(buf, PAGE_SIZE, "1\n");
719 return snprintf(buf, PAGE_SIZE, "0\n");
720 }
721 static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
722
723 /*
724 * show_flags - read method for flags file in sysfs
725 */
726 static ssize_t show_flags(struct device *dev,
727 struct device_attribute *attr, char *buf)
728 {
729 struct dock_station *dock_station = dev->platform_data;
730 return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
731
732 }
733 static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
734
735 /*
736 * write_undock - write method for "undock" file in sysfs
737 */
738 static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
739 const char *buf, size_t count)
740 {
741 int ret;
742 struct dock_station *dock_station = dev->platform_data;
743
744 if (!count)
745 return -EINVAL;
746
747 acpi_scan_lock_acquire();
748 begin_undock(dock_station);
749 ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
750 acpi_scan_lock_release();
751 return ret ? ret: count;
752 }
753 static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
754
755 /*
756 * show_dock_uid - read method for "uid" file in sysfs
757 */
758 static ssize_t show_dock_uid(struct device *dev,
759 struct device_attribute *attr, char *buf)
760 {
761 unsigned long long lbuf;
762 struct dock_station *dock_station = dev->platform_data;
763 acpi_status status = acpi_evaluate_integer(dock_station->handle,
764 "_UID", NULL, &lbuf);
765 if (ACPI_FAILURE(status))
766 return 0;
767
768 return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
769 }
770 static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
771
772 static ssize_t show_dock_type(struct device *dev,
773 struct device_attribute *attr, char *buf)
774 {
775 struct dock_station *dock_station = dev->platform_data;
776 char *type;
777
778 if (dock_station->flags & DOCK_IS_DOCK)
779 type = "dock_station";
780 else if (dock_station->flags & DOCK_IS_ATA)
781 type = "ata_bay";
782 else if (dock_station->flags & DOCK_IS_BAT)
783 type = "battery_bay";
784 else
785 type = "unknown";
786
787 return snprintf(buf, PAGE_SIZE, "%s\n", type);
788 }
789 static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
790
791 static struct attribute *dock_attributes[] = {
792 &dev_attr_docked.attr,
793 &dev_attr_flags.attr,
794 &dev_attr_undock.attr,
795 &dev_attr_uid.attr,
796 &dev_attr_type.attr,
797 NULL
798 };
799
800 static struct attribute_group dock_attribute_group = {
801 .attrs = dock_attributes
802 };
803
804 /**
805 * dock_add - add a new dock station
806 * @handle: the dock station handle
807 *
808 * allocated and initialize a new dock station device. Find all devices
809 * that are on the dock station, and register for dock event notifications.
810 */
811 static int __init dock_add(acpi_handle handle)
812 {
813 struct dock_station *dock_station, ds = { NULL, };
814 struct platform_device *dd;
815 acpi_status status;
816 int ret;
817
818 dd = platform_device_register_data(NULL, "dock", dock_station_count,
819 &ds, sizeof(ds));
820 if (IS_ERR(dd))
821 return PTR_ERR(dd);
822
823 dock_station = dd->dev.platform_data;
824
825 dock_station->handle = handle;
826 dock_station->dock_device = dd;
827 dock_station->last_dock_time = jiffies - HZ;
828
829 INIT_LIST_HEAD(&dock_station->sibling);
830 INIT_LIST_HEAD(&dock_station->dependent_devices);
831
832 /* we want the dock device to send uevents */
833 dev_set_uevent_suppress(&dd->dev, 0);
834
835 if (acpi_dock_match(handle))
836 dock_station->flags |= DOCK_IS_DOCK;
837 if (acpi_ata_match(handle))
838 dock_station->flags |= DOCK_IS_ATA;
839 if (is_battery(handle))
840 dock_station->flags |= DOCK_IS_BAT;
841
842 ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
843 if (ret)
844 goto err_unregister;
845
846 /* Find dependent devices */
847 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
848 ACPI_UINT32_MAX, find_dock_devices, NULL,
849 dock_station, NULL);
850
851 /* add the dock station as a device dependent on itself */
852 ret = add_dock_dependent_device(dock_station, handle);
853 if (ret)
854 goto err_rmgroup;
855
856 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
857 dock_notify_handler, dock_station);
858 if (ACPI_FAILURE(status)) {
859 ret = -ENODEV;
860 goto err_rmgroup;
861 }
862
863 dock_station_count++;
864 list_add(&dock_station->sibling, &dock_stations);
865 return 0;
866
867 err_rmgroup:
868 remove_dock_dependent_devices(dock_station);
869 sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
870 err_unregister:
871 platform_device_unregister(dd);
872 acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
873 return ret;
874 }
875
876 /**
877 * find_dock_and_bay - look for dock stations and bays
878 * @handle: acpi handle of a device
879 * @lvl: unused
880 * @context: unused
881 * @rv: unused
882 *
883 * This is called by acpi_walk_namespace to look for dock stations and bays.
884 */
885 static acpi_status __init
886 find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
887 {
888 if (acpi_dock_match(handle) || is_ejectable_bay(handle))
889 dock_add(handle);
890
891 return AE_OK;
892 }
893
894 void __init acpi_dock_init(void)
895 {
896 if (acpi_disabled)
897 return;
898
899 /* look for dock stations and bays */
900 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
901 ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
902
903 if (!dock_station_count) {
904 pr_info(PREFIX "No dock devices found.\n");
905 return;
906 }
907
908 pr_info(PREFIX "%s: %d docks/bays found\n",
909 ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
910 }
This page took 0.049742 seconds and 5 git commands to generate.