dock.c 25 KB

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