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. * hotplug_dock_devices - insert or remove devices on the dock station
  289. * @ds: the dock station
  290. * @event: either bus check or eject request
  291. *
  292. * Some devices on the dock station need to have drivers called
  293. * to perform hotplug operations after a dock event has occurred.
  294. * Traverse the list of dock devices that have registered a
  295. * hotplug handler, and call the handler.
  296. */
  297. static void hotplug_dock_devices(struct dock_station *ds, u32 event)
  298. {
  299. struct dock_dependent_device *dd;
  300. /*
  301. * First call driver specific hotplug functions
  302. */
  303. list_for_each_entry(dd, &ds->dependent_devices, list)
  304. dock_hotplug_event(dd, event, false);
  305. /*
  306. * Now make sure that an acpi_device is created for each
  307. * dependent device, or removed if this is an eject request.
  308. * This will cause acpi_drivers to be stopped/started if they
  309. * exist
  310. */
  311. list_for_each_entry(dd, &ds->dependent_devices, list) {
  312. if (event == ACPI_NOTIFY_EJECT_REQUEST)
  313. dock_remove_acpi_device(dd->handle);
  314. else
  315. dock_create_acpi_device(dd->handle);
  316. }
  317. }
  318. static void dock_event(struct dock_station *ds, u32 event, int num)
  319. {
  320. struct device *dev = &ds->dock_device->dev;
  321. char event_string[13];
  322. char *envp[] = { event_string, NULL };
  323. struct dock_dependent_device *dd;
  324. if (num == UNDOCK_EVENT)
  325. sprintf(event_string, "EVENT=undock");
  326. else
  327. sprintf(event_string, "EVENT=dock");
  328. /*
  329. * Indicate that the status of the dock station has
  330. * changed.
  331. */
  332. if (num == DOCK_EVENT)
  333. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  334. list_for_each_entry(dd, &ds->dependent_devices, list)
  335. dock_hotplug_event(dd, event, true);
  336. if (num != DOCK_EVENT)
  337. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  338. }
  339. /**
  340. * handle_dock - handle a dock event
  341. * @ds: the dock station
  342. * @dock: to dock, or undock - that is the question
  343. *
  344. * Execute the _DCK method in response to an acpi event
  345. */
  346. static void handle_dock(struct dock_station *ds, int dock)
  347. {
  348. acpi_status status;
  349. struct acpi_object_list arg_list;
  350. union acpi_object arg;
  351. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  352. acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
  353. /* _DCK method has one argument */
  354. arg_list.count = 1;
  355. arg_list.pointer = &arg;
  356. arg.type = ACPI_TYPE_INTEGER;
  357. arg.integer.value = dock;
  358. status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
  359. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  360. acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
  361. status);
  362. kfree(buffer.pointer);
  363. }
  364. static inline void dock(struct dock_station *ds)
  365. {
  366. handle_dock(ds, 1);
  367. }
  368. static inline void undock(struct dock_station *ds)
  369. {
  370. handle_dock(ds, 0);
  371. }
  372. static inline void begin_dock(struct dock_station *ds)
  373. {
  374. ds->flags |= DOCK_DOCKING;
  375. }
  376. static inline void complete_dock(struct dock_station *ds)
  377. {
  378. ds->flags &= ~(DOCK_DOCKING);
  379. ds->last_dock_time = jiffies;
  380. }
  381. static inline void begin_undock(struct dock_station *ds)
  382. {
  383. ds->flags |= DOCK_UNDOCKING;
  384. }
  385. static inline void complete_undock(struct dock_station *ds)
  386. {
  387. ds->flags &= ~(DOCK_UNDOCKING);
  388. }
  389. /**
  390. * dock_in_progress - see if we are in the middle of handling a dock event
  391. * @ds: the dock station
  392. *
  393. * Sometimes while docking, false dock events can be sent to the driver
  394. * because good connections aren't made or some other reason. Ignore these
  395. * if we are in the middle of doing something.
  396. */
  397. static int dock_in_progress(struct dock_station *ds)
  398. {
  399. if ((ds->flags & DOCK_DOCKING) ||
  400. time_before(jiffies, (ds->last_dock_time + HZ)))
  401. return 1;
  402. return 0;
  403. }
  404. /**
  405. * register_dock_notifier - add yourself to the dock notifier list
  406. * @nb: the callers notifier block
  407. *
  408. * If a driver wishes to be notified about dock events, they can
  409. * use this function to put a notifier block on the dock notifier list.
  410. * this notifier call chain will be called after a dock event, but
  411. * before hotplugging any new devices.
  412. */
  413. int register_dock_notifier(struct notifier_block *nb)
  414. {
  415. if (!dock_station_count)
  416. return -ENODEV;
  417. return atomic_notifier_chain_register(&dock_notifier_list, nb);
  418. }
  419. EXPORT_SYMBOL_GPL(register_dock_notifier);
  420. /**
  421. * unregister_dock_notifier - remove yourself from the dock notifier list
  422. * @nb: the callers notifier block
  423. */
  424. void unregister_dock_notifier(struct notifier_block *nb)
  425. {
  426. if (!dock_station_count)
  427. return;
  428. atomic_notifier_chain_unregister(&dock_notifier_list, nb);
  429. }
  430. EXPORT_SYMBOL_GPL(unregister_dock_notifier);
  431. /**
  432. * register_hotplug_dock_device - register a hotplug function
  433. * @handle: the handle of the device
  434. * @ops: handlers to call after docking
  435. * @context: device specific data
  436. * @init: Optional initialization routine to run after registration
  437. * @release: Optional release routine to run on unregistration
  438. *
  439. * If a driver would like to perform a hotplug operation after a dock
  440. * event, they can register an acpi_notifiy_handler to be called by
  441. * the dock driver after _DCK is executed.
  442. */
  443. int register_hotplug_dock_device(acpi_handle handle,
  444. const struct acpi_dock_ops *ops, void *context,
  445. void (*init)(void *), void (*release)(void *))
  446. {
  447. struct dock_dependent_device *dd;
  448. struct dock_station *dock_station;
  449. int ret = -EINVAL;
  450. if (WARN_ON(!context))
  451. return -EINVAL;
  452. if (!dock_station_count)
  453. return -ENODEV;
  454. /*
  455. * make sure this handle is for a device dependent on the dock,
  456. * this would include the dock station itself
  457. */
  458. list_for_each_entry(dock_station, &dock_stations, sibling) {
  459. /*
  460. * An ATA bay can be in a dock and itself can be ejected
  461. * separately, so there are two 'dock stations' which need the
  462. * ops
  463. */
  464. dd = find_dock_dependent_device(dock_station, handle);
  465. if (dd && !dock_init_hotplug(dd, ops, context, init, release))
  466. ret = 0;
  467. }
  468. return ret;
  469. }
  470. EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
  471. /**
  472. * unregister_hotplug_dock_device - remove yourself from the hotplug list
  473. * @handle: the acpi handle of the device
  474. */
  475. void unregister_hotplug_dock_device(acpi_handle handle)
  476. {
  477. struct dock_dependent_device *dd;
  478. struct dock_station *dock_station;
  479. if (!dock_station_count)
  480. return;
  481. list_for_each_entry(dock_station, &dock_stations, sibling) {
  482. dd = find_dock_dependent_device(dock_station, handle);
  483. if (dd)
  484. dock_release_hotplug(dd);
  485. }
  486. }
  487. EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
  488. /**
  489. * handle_eject_request - handle an undock request checking for error conditions
  490. *
  491. * Check to make sure the dock device is still present, then undock and
  492. * hotremove all the devices that may need removing.
  493. */
  494. static int handle_eject_request(struct dock_station *ds, u32 event)
  495. {
  496. if (dock_in_progress(ds))
  497. return -EBUSY;
  498. /*
  499. * here we need to generate the undock
  500. * event prior to actually doing the undock
  501. * so that the device struct still exists.
  502. * Also, even send the dock event if the
  503. * device is not present anymore
  504. */
  505. dock_event(ds, event, UNDOCK_EVENT);
  506. hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
  507. undock(ds);
  508. acpi_evaluate_lck(ds->handle, 0);
  509. acpi_evaluate_ej0(ds->handle);
  510. if (dock_present(ds)) {
  511. acpi_handle_err(ds->handle, "Unable to undock!\n");
  512. return -EBUSY;
  513. }
  514. complete_undock(ds);
  515. return 0;
  516. }
  517. /**
  518. * dock_notify - act upon an acpi dock notification
  519. * @handle: the dock station handle
  520. * @event: the acpi event
  521. * @data: our driver data struct
  522. *
  523. * If we are notified to dock, then check to see if the dock is
  524. * present and then dock. Notify all drivers of the dock event,
  525. * and then hotplug and devices that may need hotplugging.
  526. */
  527. static void dock_notify(acpi_handle handle, u32 event, void *data)
  528. {
  529. struct dock_station *ds = data;
  530. struct acpi_device *tmp;
  531. int surprise_removal = 0;
  532. /*
  533. * According to acpi spec 3.0a, if a DEVICE_CHECK notification
  534. * is sent and _DCK is present, it is assumed to mean an undock
  535. * request.
  536. */
  537. if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
  538. event = ACPI_NOTIFY_EJECT_REQUEST;
  539. /*
  540. * dock station: BUS_CHECK - docked or surprise removal
  541. * DEVICE_CHECK - undocked
  542. * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
  543. *
  544. * To simplify event handling, dock dependent device handler always
  545. * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  546. * ACPI_NOTIFY_EJECT_REQUEST for removal
  547. */
  548. switch (event) {
  549. case ACPI_NOTIFY_BUS_CHECK:
  550. case ACPI_NOTIFY_DEVICE_CHECK:
  551. if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
  552. &tmp)) {
  553. begin_dock(ds);
  554. dock(ds);
  555. if (!dock_present(ds)) {
  556. acpi_handle_err(handle, "Unable to dock!\n");
  557. complete_dock(ds);
  558. break;
  559. }
  560. atomic_notifier_call_chain(&dock_notifier_list,
  561. event, NULL);
  562. hotplug_dock_devices(ds, event);
  563. complete_dock(ds);
  564. dock_event(ds, event, DOCK_EVENT);
  565. acpi_evaluate_lck(ds->handle, 1);
  566. acpi_update_all_gpes();
  567. break;
  568. }
  569. if (dock_present(ds) || dock_in_progress(ds))
  570. break;
  571. /* This is a surprise removal */
  572. surprise_removal = 1;
  573. event = ACPI_NOTIFY_EJECT_REQUEST;
  574. /* Fall back */
  575. case ACPI_NOTIFY_EJECT_REQUEST:
  576. begin_undock(ds);
  577. if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
  578. || surprise_removal)
  579. handle_eject_request(ds, event);
  580. else
  581. dock_event(ds, event, UNDOCK_EVENT);
  582. break;
  583. default:
  584. acpi_handle_err(handle, "Unknown dock event %d\n", event);
  585. }
  586. }
  587. struct dock_data {
  588. acpi_handle handle;
  589. unsigned long event;
  590. struct dock_station *ds;
  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->handle, data->event, data->ds);
  597. acpi_scan_lock_release();
  598. kfree(data);
  599. }
  600. static int acpi_dock_notifier_call(struct notifier_block *this,
  601. unsigned long event, void *data)
  602. {
  603. struct dock_station *dock_station;
  604. acpi_handle handle = data;
  605. if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
  606. && event != ACPI_NOTIFY_EJECT_REQUEST)
  607. return 0;
  608. acpi_scan_lock_acquire();
  609. list_for_each_entry(dock_station, &dock_stations, sibling) {
  610. if (dock_station->handle == handle) {
  611. struct dock_data *dd;
  612. acpi_status status;
  613. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  614. if (!dd)
  615. break;
  616. dd->handle = handle;
  617. dd->event = event;
  618. dd->ds = dock_station;
  619. status = acpi_os_hotplug_execute(acpi_dock_deferred_cb,
  620. dd);
  621. if (ACPI_FAILURE(status))
  622. kfree(dd);
  623. break;
  624. }
  625. }
  626. acpi_scan_lock_release();
  627. return 0;
  628. }
  629. static struct notifier_block dock_acpi_notifier = {
  630. .notifier_call = acpi_dock_notifier_call,
  631. };
  632. /**
  633. * find_dock_devices - find devices on the dock station
  634. * @handle: the handle of the device we are examining
  635. * @lvl: unused
  636. * @context: the dock station private data
  637. * @rv: unused
  638. *
  639. * This function is called by acpi_walk_namespace. It will
  640. * check to see if an object has an _EJD method. If it does, then it
  641. * will see if it is dependent on the dock station.
  642. */
  643. static acpi_status __init
  644. find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
  645. {
  646. acpi_status status;
  647. acpi_handle tmp, parent;
  648. struct dock_station *ds = context;
  649. status = acpi_bus_get_ejd(handle, &tmp);
  650. if (ACPI_FAILURE(status)) {
  651. /* try the parent device as well */
  652. status = acpi_get_parent(handle, &parent);
  653. if (ACPI_FAILURE(status))
  654. goto fdd_out;
  655. /* see if parent is dependent on dock */
  656. status = acpi_bus_get_ejd(parent, &tmp);
  657. if (ACPI_FAILURE(status))
  658. goto fdd_out;
  659. }
  660. if (tmp == ds->handle)
  661. add_dock_dependent_device(ds, handle);
  662. fdd_out:
  663. return AE_OK;
  664. }
  665. /*
  666. * show_docked - read method for "docked" file in sysfs
  667. */
  668. static ssize_t show_docked(struct device *dev,
  669. struct device_attribute *attr, char *buf)
  670. {
  671. struct acpi_device *tmp;
  672. struct dock_station *dock_station = dev->platform_data;
  673. if (!acpi_bus_get_device(dock_station->handle, &tmp))
  674. return snprintf(buf, PAGE_SIZE, "1\n");
  675. return snprintf(buf, PAGE_SIZE, "0\n");
  676. }
  677. static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
  678. /*
  679. * show_flags - read method for flags file in sysfs
  680. */
  681. static ssize_t show_flags(struct device *dev,
  682. struct device_attribute *attr, char *buf)
  683. {
  684. struct dock_station *dock_station = dev->platform_data;
  685. return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
  686. }
  687. static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
  688. /*
  689. * write_undock - write method for "undock" file in sysfs
  690. */
  691. static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
  692. const char *buf, size_t count)
  693. {
  694. int ret;
  695. struct dock_station *dock_station = dev->platform_data;
  696. if (!count)
  697. return -EINVAL;
  698. acpi_scan_lock_acquire();
  699. begin_undock(dock_station);
  700. ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
  701. acpi_scan_lock_release();
  702. return ret ? ret: count;
  703. }
  704. static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
  705. /*
  706. * show_dock_uid - read method for "uid" file in sysfs
  707. */
  708. static ssize_t show_dock_uid(struct device *dev,
  709. struct device_attribute *attr, char *buf)
  710. {
  711. unsigned long long lbuf;
  712. struct dock_station *dock_station = dev->platform_data;
  713. acpi_status status = acpi_evaluate_integer(dock_station->handle,
  714. "_UID", NULL, &lbuf);
  715. if (ACPI_FAILURE(status))
  716. return 0;
  717. return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
  718. }
  719. static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
  720. static ssize_t show_dock_type(struct device *dev,
  721. struct device_attribute *attr, char *buf)
  722. {
  723. struct dock_station *dock_station = dev->platform_data;
  724. char *type;
  725. if (dock_station->flags & DOCK_IS_DOCK)
  726. type = "dock_station";
  727. else if (dock_station->flags & DOCK_IS_ATA)
  728. type = "ata_bay";
  729. else if (dock_station->flags & DOCK_IS_BAT)
  730. type = "battery_bay";
  731. else
  732. type = "unknown";
  733. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  734. }
  735. static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
  736. static struct attribute *dock_attributes[] = {
  737. &dev_attr_docked.attr,
  738. &dev_attr_flags.attr,
  739. &dev_attr_undock.attr,
  740. &dev_attr_uid.attr,
  741. &dev_attr_type.attr,
  742. NULL
  743. };
  744. static struct attribute_group dock_attribute_group = {
  745. .attrs = dock_attributes
  746. };
  747. /**
  748. * dock_add - add a new dock station
  749. * @handle: the dock station handle
  750. *
  751. * allocated and initialize a new dock station device. Find all devices
  752. * that are on the dock station, and register for dock event notifications.
  753. */
  754. static int __init dock_add(acpi_handle handle)
  755. {
  756. int ret, id;
  757. struct dock_station ds, *dock_station;
  758. struct platform_device *dd;
  759. id = dock_station_count;
  760. memset(&ds, 0, sizeof(ds));
  761. dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
  762. if (IS_ERR(dd))
  763. return PTR_ERR(dd);
  764. dock_station = dd->dev.platform_data;
  765. dock_station->handle = handle;
  766. dock_station->dock_device = dd;
  767. dock_station->last_dock_time = jiffies - HZ;
  768. INIT_LIST_HEAD(&dock_station->sibling);
  769. INIT_LIST_HEAD(&dock_station->dependent_devices);
  770. /* we want the dock device to send uevents */
  771. dev_set_uevent_suppress(&dd->dev, 0);
  772. if (acpi_dock_match(handle))
  773. dock_station->flags |= DOCK_IS_DOCK;
  774. if (acpi_ata_match(handle))
  775. dock_station->flags |= DOCK_IS_ATA;
  776. if (is_battery(handle))
  777. dock_station->flags |= DOCK_IS_BAT;
  778. ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
  779. if (ret)
  780. goto err_unregister;
  781. /* Find dependent devices */
  782. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  783. ACPI_UINT32_MAX, find_dock_devices, NULL,
  784. dock_station, NULL);
  785. /* add the dock station as a device dependent on itself */
  786. ret = add_dock_dependent_device(dock_station, handle);
  787. if (ret)
  788. goto err_rmgroup;
  789. dock_station_count++;
  790. list_add(&dock_station->sibling, &dock_stations);
  791. return 0;
  792. err_rmgroup:
  793. sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
  794. err_unregister:
  795. platform_device_unregister(dd);
  796. acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
  797. return ret;
  798. }
  799. /**
  800. * find_dock_and_bay - look for dock stations and bays
  801. * @handle: acpi handle of a device
  802. * @lvl: unused
  803. * @context: unused
  804. * @rv: unused
  805. *
  806. * This is called by acpi_walk_namespace to look for dock stations and bays.
  807. */
  808. static __init acpi_status
  809. find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  810. {
  811. if (acpi_dock_match(handle) || is_ejectable_bay(handle))
  812. dock_add(handle);
  813. return AE_OK;
  814. }
  815. void __init acpi_dock_init(void)
  816. {
  817. if (acpi_disabled)
  818. return;
  819. /* look for dock stations and bays */
  820. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  821. ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
  822. if (!dock_station_count) {
  823. pr_info(PREFIX "No dock devices found.\n");
  824. return;
  825. }
  826. ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
  827. register_acpi_bus_notifier(&dock_acpi_notifier);
  828. pr_info(PREFIX "%s: %d docks/bays found\n",
  829. ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
  830. }