dock.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  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_object(ds->handle, "_DCK", &arg_list, &buffer);
  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. kfree(buffer.pointer);
  396. }
  397. static inline void dock(struct dock_station *ds)
  398. {
  399. handle_dock(ds, 1);
  400. }
  401. static inline void undock(struct dock_station *ds)
  402. {
  403. handle_dock(ds, 0);
  404. }
  405. static inline void begin_dock(struct dock_station *ds)
  406. {
  407. ds->flags |= DOCK_DOCKING;
  408. }
  409. static inline void complete_dock(struct dock_station *ds)
  410. {
  411. ds->flags &= ~(DOCK_DOCKING);
  412. ds->last_dock_time = jiffies;
  413. }
  414. static inline void begin_undock(struct dock_station *ds)
  415. {
  416. ds->flags |= DOCK_UNDOCKING;
  417. }
  418. static inline void complete_undock(struct dock_station *ds)
  419. {
  420. ds->flags &= ~(DOCK_UNDOCKING);
  421. }
  422. /**
  423. * dock_in_progress - see if we are in the middle of handling a dock event
  424. * @ds: the dock station
  425. *
  426. * Sometimes while docking, false dock events can be sent to the driver
  427. * because good connections aren't made or some other reason. Ignore these
  428. * if we are in the middle of doing something.
  429. */
  430. static int dock_in_progress(struct dock_station *ds)
  431. {
  432. if ((ds->flags & DOCK_DOCKING) ||
  433. time_before(jiffies, (ds->last_dock_time + HZ)))
  434. return 1;
  435. return 0;
  436. }
  437. /**
  438. * register_hotplug_dock_device - register a hotplug function
  439. * @handle: the handle of the device
  440. * @ops: handlers to call after docking
  441. * @context: device specific data
  442. * @init: Optional initialization routine to run after registration
  443. * @release: Optional release routine to run on unregistration
  444. *
  445. * If a driver would like to perform a hotplug operation after a dock
  446. * event, they can register an acpi_notifiy_handler to be called by
  447. * the dock driver after _DCK is executed.
  448. */
  449. int register_hotplug_dock_device(acpi_handle handle,
  450. const struct acpi_dock_ops *ops, void *context,
  451. void (*init)(void *), void (*release)(void *))
  452. {
  453. struct dock_dependent_device *dd;
  454. struct dock_station *dock_station;
  455. int ret = -EINVAL;
  456. if (WARN_ON(!context))
  457. return -EINVAL;
  458. if (!dock_station_count)
  459. return -ENODEV;
  460. /*
  461. * make sure this handle is for a device dependent on the dock,
  462. * this would include the dock station itself
  463. */
  464. list_for_each_entry(dock_station, &dock_stations, sibling) {
  465. /*
  466. * An ATA bay can be in a dock and itself can be ejected
  467. * separately, so there are two 'dock stations' which need the
  468. * ops
  469. */
  470. dd = find_dock_dependent_device(dock_station, handle);
  471. if (dd && !dock_init_hotplug(dd, ops, context, init, release))
  472. ret = 0;
  473. }
  474. return ret;
  475. }
  476. EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
  477. /**
  478. * unregister_hotplug_dock_device - remove yourself from the hotplug list
  479. * @handle: the acpi handle of the device
  480. */
  481. void unregister_hotplug_dock_device(acpi_handle handle)
  482. {
  483. struct dock_dependent_device *dd;
  484. struct dock_station *dock_station;
  485. if (!dock_station_count)
  486. return;
  487. list_for_each_entry(dock_station, &dock_stations, sibling) {
  488. dd = find_dock_dependent_device(dock_station, handle);
  489. if (dd)
  490. dock_release_hotplug(dd);
  491. }
  492. }
  493. EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
  494. /**
  495. * handle_eject_request - handle an undock request checking for error conditions
  496. *
  497. * Check to make sure the dock device is still present, then undock and
  498. * hotremove all the devices that may need removing.
  499. */
  500. static int handle_eject_request(struct dock_station *ds, u32 event)
  501. {
  502. if (dock_in_progress(ds))
  503. return -EBUSY;
  504. /*
  505. * here we need to generate the undock
  506. * event prior to actually doing the undock
  507. * so that the device struct still exists.
  508. * Also, even send the dock event if the
  509. * device is not present anymore
  510. */
  511. dock_event(ds, event, UNDOCK_EVENT);
  512. hot_remove_dock_devices(ds);
  513. undock(ds);
  514. acpi_evaluate_lck(ds->handle, 0);
  515. acpi_evaluate_ej0(ds->handle);
  516. if (dock_present(ds)) {
  517. acpi_handle_err(ds->handle, "Unable to undock!\n");
  518. return -EBUSY;
  519. }
  520. complete_undock(ds);
  521. return 0;
  522. }
  523. /**
  524. * dock_notify - act upon an acpi dock notification
  525. * @ds: dock station
  526. * @event: the acpi event
  527. *
  528. * If we are notified to dock, then check to see if the dock is
  529. * present and then dock. Notify all drivers of the dock event,
  530. * and then hotplug and devices that may need hotplugging.
  531. */
  532. static void dock_notify(struct dock_station *ds, u32 event)
  533. {
  534. acpi_handle handle = ds->handle;
  535. struct acpi_device *ad;
  536. int surprise_removal = 0;
  537. /*
  538. * According to acpi spec 3.0a, if a DEVICE_CHECK notification
  539. * is sent and _DCK is present, it is assumed to mean an undock
  540. * request.
  541. */
  542. if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
  543. event = ACPI_NOTIFY_EJECT_REQUEST;
  544. /*
  545. * dock station: BUS_CHECK - docked or surprise removal
  546. * DEVICE_CHECK - undocked
  547. * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
  548. *
  549. * To simplify event handling, dock dependent device handler always
  550. * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  551. * ACPI_NOTIFY_EJECT_REQUEST for removal
  552. */
  553. switch (event) {
  554. case ACPI_NOTIFY_BUS_CHECK:
  555. case ACPI_NOTIFY_DEVICE_CHECK:
  556. if (!dock_in_progress(ds) && acpi_bus_get_device(handle, &ad)) {
  557. begin_dock(ds);
  558. dock(ds);
  559. if (!dock_present(ds)) {
  560. acpi_handle_err(handle, "Unable to dock!\n");
  561. complete_dock(ds);
  562. break;
  563. }
  564. hotplug_dock_devices(ds, event);
  565. complete_dock(ds);
  566. dock_event(ds, event, DOCK_EVENT);
  567. acpi_evaluate_lck(ds->handle, 1);
  568. acpi_update_all_gpes();
  569. break;
  570. }
  571. if (dock_present(ds) || dock_in_progress(ds))
  572. break;
  573. /* This is a surprise removal */
  574. surprise_removal = 1;
  575. event = ACPI_NOTIFY_EJECT_REQUEST;
  576. /* Fall back */
  577. case ACPI_NOTIFY_EJECT_REQUEST:
  578. begin_undock(ds);
  579. if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
  580. || surprise_removal)
  581. handle_eject_request(ds, event);
  582. else
  583. dock_event(ds, event, UNDOCK_EVENT);
  584. break;
  585. default:
  586. acpi_handle_err(handle, "Unknown dock event %d\n", event);
  587. }
  588. }
  589. struct dock_data {
  590. struct dock_station *ds;
  591. u32 event;
  592. };
  593. static void acpi_dock_deferred_cb(void *context)
  594. {
  595. struct dock_data *data = context;
  596. acpi_scan_lock_acquire();
  597. dock_notify(data->ds, data->event);
  598. acpi_scan_lock_release();
  599. kfree(data);
  600. }
  601. static void dock_notify_handler(acpi_handle handle, u32 event, void *data)
  602. {
  603. struct dock_data *dd;
  604. if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
  605. && event != ACPI_NOTIFY_EJECT_REQUEST)
  606. return;
  607. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  608. if (dd) {
  609. acpi_status status;
  610. dd->ds = data;
  611. dd->event = event;
  612. status = acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
  613. if (ACPI_FAILURE(status))
  614. kfree(dd);
  615. }
  616. }
  617. /**
  618. * find_dock_devices - find devices on the dock station
  619. * @handle: the handle of the device we are examining
  620. * @lvl: unused
  621. * @context: the dock station private data
  622. * @rv: unused
  623. *
  624. * This function is called by acpi_walk_namespace. It will
  625. * check to see if an object has an _EJD method. If it does, then it
  626. * will see if it is dependent on the dock station.
  627. */
  628. static acpi_status __init find_dock_devices(acpi_handle handle, u32 lvl,
  629. void *context, void **rv)
  630. {
  631. struct dock_station *ds = context;
  632. acpi_handle ejd = NULL;
  633. acpi_bus_get_ejd(handle, &ejd);
  634. if (ejd == ds->handle)
  635. add_dock_dependent_device(ds, handle);
  636. return AE_OK;
  637. }
  638. /*
  639. * show_docked - read method for "docked" file in sysfs
  640. */
  641. static ssize_t show_docked(struct device *dev,
  642. struct device_attribute *attr, char *buf)
  643. {
  644. struct acpi_device *tmp;
  645. struct dock_station *dock_station = dev->platform_data;
  646. if (!acpi_bus_get_device(dock_station->handle, &tmp))
  647. return snprintf(buf, PAGE_SIZE, "1\n");
  648. return snprintf(buf, PAGE_SIZE, "0\n");
  649. }
  650. static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
  651. /*
  652. * show_flags - read method for flags file in sysfs
  653. */
  654. static ssize_t show_flags(struct device *dev,
  655. struct device_attribute *attr, char *buf)
  656. {
  657. struct dock_station *dock_station = dev->platform_data;
  658. return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
  659. }
  660. static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
  661. /*
  662. * write_undock - write method for "undock" file in sysfs
  663. */
  664. static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
  665. const char *buf, size_t count)
  666. {
  667. int ret;
  668. struct dock_station *dock_station = dev->platform_data;
  669. if (!count)
  670. return -EINVAL;
  671. acpi_scan_lock_acquire();
  672. begin_undock(dock_station);
  673. ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
  674. acpi_scan_lock_release();
  675. return ret ? ret: count;
  676. }
  677. static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
  678. /*
  679. * show_dock_uid - read method for "uid" file in sysfs
  680. */
  681. static ssize_t show_dock_uid(struct device *dev,
  682. struct device_attribute *attr, char *buf)
  683. {
  684. unsigned long long lbuf;
  685. struct dock_station *dock_station = dev->platform_data;
  686. acpi_status status = acpi_evaluate_integer(dock_station->handle,
  687. "_UID", NULL, &lbuf);
  688. if (ACPI_FAILURE(status))
  689. return 0;
  690. return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
  691. }
  692. static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
  693. static ssize_t show_dock_type(struct device *dev,
  694. struct device_attribute *attr, char *buf)
  695. {
  696. struct dock_station *dock_station = dev->platform_data;
  697. char *type;
  698. if (dock_station->flags & DOCK_IS_DOCK)
  699. type = "dock_station";
  700. else if (dock_station->flags & DOCK_IS_ATA)
  701. type = "ata_bay";
  702. else if (dock_station->flags & DOCK_IS_BAT)
  703. type = "battery_bay";
  704. else
  705. type = "unknown";
  706. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  707. }
  708. static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
  709. static struct attribute *dock_attributes[] = {
  710. &dev_attr_docked.attr,
  711. &dev_attr_flags.attr,
  712. &dev_attr_undock.attr,
  713. &dev_attr_uid.attr,
  714. &dev_attr_type.attr,
  715. NULL
  716. };
  717. static struct attribute_group dock_attribute_group = {
  718. .attrs = dock_attributes
  719. };
  720. /**
  721. * dock_add - add a new dock station
  722. * @handle: the dock station handle
  723. *
  724. * allocated and initialize a new dock station device. Find all devices
  725. * that are on the dock station, and register for dock event notifications.
  726. */
  727. static int __init dock_add(acpi_handle handle)
  728. {
  729. struct dock_station *dock_station, ds = { NULL, };
  730. struct platform_device *dd;
  731. acpi_status status;
  732. int ret;
  733. dd = platform_device_register_data(NULL, "dock", dock_station_count,
  734. &ds, sizeof(ds));
  735. if (IS_ERR(dd))
  736. return PTR_ERR(dd);
  737. dock_station = dd->dev.platform_data;
  738. dock_station->handle = handle;
  739. dock_station->dock_device = dd;
  740. dock_station->last_dock_time = jiffies - HZ;
  741. INIT_LIST_HEAD(&dock_station->sibling);
  742. INIT_LIST_HEAD(&dock_station->dependent_devices);
  743. /* we want the dock device to send uevents */
  744. dev_set_uevent_suppress(&dd->dev, 0);
  745. if (acpi_dock_match(handle))
  746. dock_station->flags |= DOCK_IS_DOCK;
  747. if (acpi_ata_match(handle))
  748. dock_station->flags |= DOCK_IS_ATA;
  749. if (is_battery(handle))
  750. dock_station->flags |= DOCK_IS_BAT;
  751. ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
  752. if (ret)
  753. goto err_unregister;
  754. /* Find dependent devices */
  755. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  756. ACPI_UINT32_MAX, find_dock_devices, NULL,
  757. dock_station, NULL);
  758. /* add the dock station as a device dependent on itself */
  759. ret = add_dock_dependent_device(dock_station, handle);
  760. if (ret)
  761. goto err_rmgroup;
  762. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  763. dock_notify_handler, dock_station);
  764. if (ACPI_FAILURE(status)) {
  765. ret = -ENODEV;
  766. goto err_rmgroup;
  767. }
  768. dock_station_count++;
  769. list_add(&dock_station->sibling, &dock_stations);
  770. return 0;
  771. err_rmgroup:
  772. remove_dock_dependent_devices(dock_station);
  773. sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
  774. err_unregister:
  775. platform_device_unregister(dd);
  776. acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
  777. return ret;
  778. }
  779. /**
  780. * find_dock_and_bay - look for dock stations and bays
  781. * @handle: acpi handle of a device
  782. * @lvl: unused
  783. * @context: unused
  784. * @rv: unused
  785. *
  786. * This is called by acpi_walk_namespace to look for dock stations and bays.
  787. */
  788. static __init acpi_status
  789. find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  790. {
  791. if (acpi_dock_match(handle) || is_ejectable_bay(handle))
  792. dock_add(handle);
  793. return AE_OK;
  794. }
  795. void __init acpi_dock_init(void)
  796. {
  797. if (acpi_disabled)
  798. return;
  799. /* look for dock stations and bays */
  800. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  801. ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
  802. if (!dock_station_count) {
  803. pr_info(PREFIX "No dock devices found.\n");
  804. return;
  805. }
  806. pr_info(PREFIX "%s: %d docks/bays found\n",
  807. ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
  808. }