dock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * dock.c - ACPI dock station driver
  4. *
  5. * Copyright (C) 2006, 2014, Intel Corp.
  6. * Author: Kristen Carlson Accardi <kristen.c.accardi@intel.com>
  7. * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/notifier.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/stddef.h>
  18. #include <linux/acpi.h>
  19. #include "internal.h"
  20. static bool immediate_undock = 1;
  21. module_param(immediate_undock, bool, 0644);
  22. MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
  23. "undock immediately when the undock button is pressed, 0 will cause"
  24. " the driver to wait for userspace to write the undock sysfs file "
  25. " before undocking");
  26. struct dock_station {
  27. acpi_handle handle;
  28. unsigned long last_dock_time;
  29. u32 flags;
  30. struct list_head dependent_devices;
  31. struct list_head sibling;
  32. struct platform_device *dock_device;
  33. };
  34. static LIST_HEAD(dock_stations);
  35. static int dock_station_count;
  36. struct dock_dependent_device {
  37. struct list_head list;
  38. struct acpi_device *adev;
  39. };
  40. #define DOCK_DOCKING 0x00000001
  41. #define DOCK_UNDOCKING 0x00000002
  42. #define DOCK_IS_DOCK 0x00000010
  43. #define DOCK_IS_ATA 0x00000020
  44. #define DOCK_IS_BAT 0x00000040
  45. #define DOCK_EVENT 3
  46. #define UNDOCK_EVENT 2
  47. enum dock_callback_type {
  48. DOCK_CALL_HANDLER,
  49. DOCK_CALL_FIXUP,
  50. DOCK_CALL_UEVENT,
  51. };
  52. /*****************************************************************************
  53. * Dock Dependent device functions *
  54. *****************************************************************************/
  55. /**
  56. * add_dock_dependent_device - associate a device with the dock station
  57. * @ds: Dock station.
  58. * @adev: Dependent ACPI device object.
  59. *
  60. * Add the dependent device to the dock's dependent device list.
  61. */
  62. static int add_dock_dependent_device(struct dock_station *ds,
  63. struct acpi_device *adev)
  64. {
  65. struct dock_dependent_device *dd;
  66. dd = kzalloc_obj(*dd);
  67. if (!dd)
  68. return -ENOMEM;
  69. dd->adev = adev;
  70. INIT_LIST_HEAD(&dd->list);
  71. list_add_tail(&dd->list, &ds->dependent_devices);
  72. return 0;
  73. }
  74. static void dock_hotplug_event(struct dock_dependent_device *dd, u32 event,
  75. enum dock_callback_type cb_type)
  76. {
  77. struct acpi_device *adev = dd->adev;
  78. acpi_hp_fixup fixup = NULL;
  79. acpi_hp_uevent uevent = NULL;
  80. acpi_hp_notify notify = NULL;
  81. acpi_lock_hp_context();
  82. if (adev->hp) {
  83. if (cb_type == DOCK_CALL_FIXUP)
  84. fixup = adev->hp->fixup;
  85. else if (cb_type == DOCK_CALL_UEVENT)
  86. uevent = adev->hp->uevent;
  87. else
  88. notify = adev->hp->notify;
  89. }
  90. acpi_unlock_hp_context();
  91. if (fixup)
  92. fixup(adev);
  93. else if (uevent)
  94. uevent(adev, event);
  95. else if (notify)
  96. notify(adev, event);
  97. }
  98. static struct dock_station *find_dock_station(acpi_handle handle)
  99. {
  100. struct dock_station *ds;
  101. list_for_each_entry(ds, &dock_stations, sibling)
  102. if (ds->handle == handle)
  103. return ds;
  104. return NULL;
  105. }
  106. /**
  107. * find_dock_dependent_device - get a device dependent on this dock
  108. * @ds: the dock station
  109. * @adev: ACPI device object to find.
  110. *
  111. * iterate over the dependent device list for this dock. If the
  112. * dependent device matches the handle, return.
  113. */
  114. static struct dock_dependent_device *
  115. find_dock_dependent_device(struct dock_station *ds, struct acpi_device *adev)
  116. {
  117. struct dock_dependent_device *dd;
  118. list_for_each_entry(dd, &ds->dependent_devices, list)
  119. if (adev == dd->adev)
  120. return dd;
  121. return NULL;
  122. }
  123. void register_dock_dependent_device(struct acpi_device *adev,
  124. acpi_handle dshandle)
  125. {
  126. struct dock_station *ds = find_dock_station(dshandle);
  127. if (ds && !find_dock_dependent_device(ds, adev))
  128. add_dock_dependent_device(ds, adev);
  129. }
  130. /*****************************************************************************
  131. * Dock functions *
  132. *****************************************************************************/
  133. /**
  134. * is_dock_device - see if a device is on a dock station
  135. * @adev: ACPI device object to check.
  136. *
  137. * If this device is either the dock station itself,
  138. * or is a device dependent on the dock station, then it
  139. * is a dock device
  140. */
  141. int is_dock_device(struct acpi_device *adev)
  142. {
  143. struct dock_station *dock_station;
  144. if (!dock_station_count)
  145. return 0;
  146. if (acpi_dock_match(adev->handle))
  147. return 1;
  148. list_for_each_entry(dock_station, &dock_stations, sibling)
  149. if (find_dock_dependent_device(dock_station, adev))
  150. return 1;
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(is_dock_device);
  154. /**
  155. * dock_present - see if the dock station is present.
  156. * @ds: the dock station
  157. *
  158. * execute the _STA method. note that present does not
  159. * imply that we are docked.
  160. */
  161. static int dock_present(struct dock_station *ds)
  162. {
  163. unsigned long long sta;
  164. acpi_status status;
  165. if (ds) {
  166. status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
  167. if (ACPI_SUCCESS(status) && sta)
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. /**
  173. * hot_remove_dock_devices - Remove dock station devices.
  174. * @ds: Dock station.
  175. */
  176. static void hot_remove_dock_devices(struct dock_station *ds)
  177. {
  178. struct dock_dependent_device *dd;
  179. /*
  180. * Walk the list in reverse order so that devices that have been added
  181. * last are removed first (in case there are some indirect dependencies
  182. * between them).
  183. */
  184. list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
  185. dock_hotplug_event(dd, ACPI_NOTIFY_EJECT_REQUEST,
  186. DOCK_CALL_HANDLER);
  187. list_for_each_entry_reverse(dd, &ds->dependent_devices, list)
  188. acpi_bus_trim(dd->adev);
  189. }
  190. /**
  191. * hotplug_dock_devices - Insert devices on a dock station.
  192. * @ds: the dock station
  193. * @event: either bus check or device check request
  194. *
  195. * Some devices on the dock station need to have drivers called
  196. * to perform hotplug operations after a dock event has occurred.
  197. * Traverse the list of dock devices that have registered a
  198. * hotplug handler, and call the handler.
  199. */
  200. static void hotplug_dock_devices(struct dock_station *ds, u32 event)
  201. {
  202. struct dock_dependent_device *dd;
  203. /* Call driver specific post-dock fixups. */
  204. list_for_each_entry(dd, &ds->dependent_devices, list)
  205. dock_hotplug_event(dd, event, DOCK_CALL_FIXUP);
  206. /* Call driver specific hotplug functions. */
  207. list_for_each_entry(dd, &ds->dependent_devices, list)
  208. dock_hotplug_event(dd, event, DOCK_CALL_HANDLER);
  209. /*
  210. * Check if all devices have been enumerated already. If not, run
  211. * acpi_bus_scan() for them and that will cause scan handlers to be
  212. * attached to device objects or acpi_drivers to be stopped/started if
  213. * they are present.
  214. */
  215. list_for_each_entry(dd, &ds->dependent_devices, list) {
  216. struct acpi_device *adev = dd->adev;
  217. if (!acpi_device_enumerated(adev)) {
  218. int ret = acpi_bus_scan(adev->handle);
  219. if (ret)
  220. dev_dbg(&adev->dev, "scan error %d\n", -ret);
  221. }
  222. }
  223. }
  224. static void dock_event(struct dock_station *ds, u32 event, int num)
  225. {
  226. struct device *dev = &ds->dock_device->dev;
  227. char event_string[13];
  228. char *envp[] = { event_string, NULL };
  229. struct dock_dependent_device *dd;
  230. if (num == UNDOCK_EVENT)
  231. sprintf(event_string, "EVENT=undock");
  232. else
  233. sprintf(event_string, "EVENT=dock");
  234. /*
  235. * Indicate that the status of the dock station has
  236. * changed.
  237. */
  238. if (num == DOCK_EVENT)
  239. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  240. list_for_each_entry(dd, &ds->dependent_devices, list)
  241. dock_hotplug_event(dd, event, DOCK_CALL_UEVENT);
  242. if (num != DOCK_EVENT)
  243. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  244. }
  245. /**
  246. * handle_dock - handle a dock event
  247. * @ds: the dock station
  248. * @dock: to dock, or undock - that is the question
  249. *
  250. * Execute the _DCK method in response to an acpi event
  251. */
  252. static void handle_dock(struct dock_station *ds, int dock)
  253. {
  254. acpi_status status;
  255. struct acpi_object_list arg_list;
  256. union acpi_object arg;
  257. unsigned long long value;
  258. acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
  259. /* _DCK method has one argument */
  260. arg_list.count = 1;
  261. arg_list.pointer = &arg;
  262. arg.type = ACPI_TYPE_INTEGER;
  263. arg.integer.value = dock;
  264. status = acpi_evaluate_integer(ds->handle, "_DCK", &arg_list, &value);
  265. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  266. acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
  267. status);
  268. }
  269. static inline void dock(struct dock_station *ds)
  270. {
  271. handle_dock(ds, 1);
  272. }
  273. static inline void undock(struct dock_station *ds)
  274. {
  275. handle_dock(ds, 0);
  276. }
  277. static inline void begin_dock(struct dock_station *ds)
  278. {
  279. ds->flags |= DOCK_DOCKING;
  280. }
  281. static inline void complete_dock(struct dock_station *ds)
  282. {
  283. ds->flags &= ~(DOCK_DOCKING);
  284. ds->last_dock_time = jiffies;
  285. }
  286. static inline void begin_undock(struct dock_station *ds)
  287. {
  288. ds->flags |= DOCK_UNDOCKING;
  289. }
  290. static inline void complete_undock(struct dock_station *ds)
  291. {
  292. ds->flags &= ~(DOCK_UNDOCKING);
  293. }
  294. /**
  295. * dock_in_progress - see if we are in the middle of handling a dock event
  296. * @ds: the dock station
  297. *
  298. * Sometimes while docking, false dock events can be sent to the driver
  299. * because good connections aren't made or some other reason. Ignore these
  300. * if we are in the middle of doing something.
  301. */
  302. static int dock_in_progress(struct dock_station *ds)
  303. {
  304. if ((ds->flags & DOCK_DOCKING) ||
  305. time_before(jiffies, (ds->last_dock_time + HZ)))
  306. return 1;
  307. return 0;
  308. }
  309. /**
  310. * handle_eject_request - handle an undock request checking for error conditions
  311. * @ds: The dock station to undock.
  312. * @event: The ACPI event number associated with the undock request.
  313. *
  314. * Check to make sure the dock device is still present, then undock and
  315. * hotremove all the devices that may need removing.
  316. */
  317. static int handle_eject_request(struct dock_station *ds, u32 event)
  318. {
  319. if (dock_in_progress(ds))
  320. return -EBUSY;
  321. /*
  322. * here we need to generate the undock
  323. * event prior to actually doing the undock
  324. * so that the device struct still exists.
  325. * Also, even send the dock event if the
  326. * device is not present anymore
  327. */
  328. dock_event(ds, event, UNDOCK_EVENT);
  329. hot_remove_dock_devices(ds);
  330. undock(ds);
  331. acpi_evaluate_lck(ds->handle, 0);
  332. acpi_evaluate_ej0(ds->handle);
  333. if (dock_present(ds)) {
  334. acpi_handle_err(ds->handle, "Unable to undock!\n");
  335. return -EBUSY;
  336. }
  337. complete_undock(ds);
  338. return 0;
  339. }
  340. /**
  341. * dock_notify - Handle ACPI dock notification.
  342. * @adev: Dock station's ACPI device object.
  343. * @event: Event code.
  344. *
  345. * If we are notified to dock, then check to see if the dock is
  346. * present and then dock. Notify all drivers of the dock event,
  347. * and then hotplug and devices that may need hotplugging.
  348. */
  349. int dock_notify(struct acpi_device *adev, u32 event)
  350. {
  351. acpi_handle handle = adev->handle;
  352. struct dock_station *ds = find_dock_station(handle);
  353. int surprise_removal = 0;
  354. if (!ds)
  355. return -ENODEV;
  356. /*
  357. * According to acpi spec 3.0a, if a DEVICE_CHECK notification
  358. * is sent and _DCK is present, it is assumed to mean an undock
  359. * request.
  360. */
  361. if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
  362. event = ACPI_NOTIFY_EJECT_REQUEST;
  363. /*
  364. * dock station: BUS_CHECK - docked or surprise removal
  365. * DEVICE_CHECK - undocked
  366. * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
  367. *
  368. * To simplify event handling, dock dependent device handler always
  369. * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  370. * ACPI_NOTIFY_EJECT_REQUEST for removal
  371. */
  372. switch (event) {
  373. case ACPI_NOTIFY_BUS_CHECK:
  374. case ACPI_NOTIFY_DEVICE_CHECK:
  375. if (!dock_in_progress(ds) && !acpi_device_enumerated(adev)) {
  376. begin_dock(ds);
  377. dock(ds);
  378. if (!dock_present(ds)) {
  379. acpi_handle_err(handle, "Unable to dock!\n");
  380. complete_dock(ds);
  381. break;
  382. }
  383. hotplug_dock_devices(ds, event);
  384. complete_dock(ds);
  385. dock_event(ds, event, DOCK_EVENT);
  386. acpi_evaluate_lck(ds->handle, 1);
  387. acpi_update_all_gpes();
  388. break;
  389. }
  390. if (dock_present(ds) || dock_in_progress(ds))
  391. break;
  392. /* This is a surprise removal */
  393. surprise_removal = 1;
  394. event = ACPI_NOTIFY_EJECT_REQUEST;
  395. /* Fall back */
  396. fallthrough;
  397. case ACPI_NOTIFY_EJECT_REQUEST:
  398. begin_undock(ds);
  399. if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
  400. || surprise_removal)
  401. handle_eject_request(ds, event);
  402. else
  403. dock_event(ds, event, UNDOCK_EVENT);
  404. break;
  405. }
  406. return 0;
  407. }
  408. /*
  409. * show_docked - read method for "docked" file in sysfs
  410. */
  411. static ssize_t docked_show(struct device *dev,
  412. struct device_attribute *attr, char *buf)
  413. {
  414. struct dock_station *dock_station = dev->platform_data;
  415. struct acpi_device *adev = acpi_fetch_acpi_dev(dock_station->handle);
  416. return sysfs_emit(buf, "%u\n", acpi_device_enumerated(adev));
  417. }
  418. static DEVICE_ATTR_RO(docked);
  419. /*
  420. * show_flags - read method for flags file in sysfs
  421. */
  422. static ssize_t flags_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct dock_station *dock_station = dev->platform_data;
  426. return sysfs_emit(buf, "%d\n", dock_station->flags);
  427. }
  428. static DEVICE_ATTR_RO(flags);
  429. /*
  430. * write_undock - write method for "undock" file in sysfs
  431. */
  432. static ssize_t undock_store(struct device *dev, struct device_attribute *attr,
  433. const char *buf, size_t count)
  434. {
  435. int ret;
  436. struct dock_station *dock_station = dev->platform_data;
  437. if (!count)
  438. return -EINVAL;
  439. acpi_scan_lock_acquire();
  440. begin_undock(dock_station);
  441. ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
  442. acpi_scan_lock_release();
  443. return ret ? ret : count;
  444. }
  445. static DEVICE_ATTR_WO(undock);
  446. /*
  447. * show_dock_uid - read method for "uid" file in sysfs
  448. */
  449. static ssize_t uid_show(struct device *dev,
  450. struct device_attribute *attr, char *buf)
  451. {
  452. unsigned long long lbuf;
  453. struct dock_station *dock_station = dev->platform_data;
  454. acpi_status status = acpi_evaluate_integer(dock_station->handle,
  455. "_UID", NULL, &lbuf);
  456. if (ACPI_FAILURE(status))
  457. return 0;
  458. return sysfs_emit(buf, "%llx\n", lbuf);
  459. }
  460. static DEVICE_ATTR_RO(uid);
  461. static ssize_t type_show(struct device *dev,
  462. struct device_attribute *attr, char *buf)
  463. {
  464. struct dock_station *dock_station = dev->platform_data;
  465. char *type;
  466. if (dock_station->flags & DOCK_IS_DOCK)
  467. type = "dock_station";
  468. else if (dock_station->flags & DOCK_IS_ATA)
  469. type = "ata_bay";
  470. else if (dock_station->flags & DOCK_IS_BAT)
  471. type = "battery_bay";
  472. else
  473. type = "unknown";
  474. return sysfs_emit(buf, "%s\n", type);
  475. }
  476. static DEVICE_ATTR_RO(type);
  477. static struct attribute *dock_attributes[] = {
  478. &dev_attr_docked.attr,
  479. &dev_attr_flags.attr,
  480. &dev_attr_undock.attr,
  481. &dev_attr_uid.attr,
  482. &dev_attr_type.attr,
  483. NULL
  484. };
  485. static const struct attribute_group dock_attribute_group = {
  486. .attrs = dock_attributes
  487. };
  488. /**
  489. * acpi_dock_add - Add a new dock station
  490. * @adev: Dock station ACPI device object.
  491. *
  492. * allocated and initialize a new dock station device.
  493. */
  494. void acpi_dock_add(struct acpi_device *adev)
  495. {
  496. struct dock_station *dock_station, ds = { NULL, };
  497. struct platform_device_info pdevinfo;
  498. acpi_handle handle = adev->handle;
  499. struct platform_device *dd;
  500. int ret;
  501. memset(&pdevinfo, 0, sizeof(pdevinfo));
  502. pdevinfo.name = "dock";
  503. pdevinfo.id = dock_station_count;
  504. pdevinfo.fwnode = acpi_fwnode_handle(adev);
  505. pdevinfo.data = &ds;
  506. pdevinfo.size_data = sizeof(ds);
  507. dd = platform_device_register_full(&pdevinfo);
  508. if (IS_ERR(dd))
  509. return;
  510. dock_station = dd->dev.platform_data;
  511. dock_station->handle = handle;
  512. dock_station->dock_device = dd;
  513. dock_station->last_dock_time = jiffies - HZ;
  514. INIT_LIST_HEAD(&dock_station->sibling);
  515. INIT_LIST_HEAD(&dock_station->dependent_devices);
  516. /* we want the dock device to send uevents */
  517. dev_set_uevent_suppress(&dd->dev, 0);
  518. if (acpi_dock_match(handle))
  519. dock_station->flags |= DOCK_IS_DOCK;
  520. if (acpi_ata_match(handle))
  521. dock_station->flags |= DOCK_IS_ATA;
  522. if (acpi_device_is_battery(adev))
  523. dock_station->flags |= DOCK_IS_BAT;
  524. ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
  525. if (ret)
  526. goto err_unregister;
  527. /* add the dock station as a device dependent on itself */
  528. ret = add_dock_dependent_device(dock_station, adev);
  529. if (ret)
  530. goto err_rmgroup;
  531. dock_station_count++;
  532. list_add(&dock_station->sibling, &dock_stations);
  533. adev->flags.is_dock_station = true;
  534. dev_info(&adev->dev, "ACPI dock station (docks/bays count: %d)\n",
  535. dock_station_count);
  536. return;
  537. err_rmgroup:
  538. sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
  539. err_unregister:
  540. platform_device_unregister(dd);
  541. acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
  542. }