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