dock.c 24 KB

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