dock.c 16 KB

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