dock.c 30 KB

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