dock.c 28 KB

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