dock.c 28 KB

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