dock.c 27 KB

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