dock.c 25 KB

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