dock.c 17 KB

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