dock.c 28 KB

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