dock.c 24 KB

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