dock.c 24 KB

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