dock.c 27 KB

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