dock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 <acpi/acpi_bus.h>
  30. #include <acpi/acpi_drivers.h>
  31. #define ACPI_DOCK_DRIVER_NAME "ACPI Dock Station Driver"
  32. ACPI_MODULE_NAME("dock")
  33. MODULE_AUTHOR("Kristen Carlson Accardi");
  34. MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_NAME);
  35. MODULE_LICENSE("GPL");
  36. static struct atomic_notifier_head dock_notifier_list;
  37. struct dock_station {
  38. acpi_handle handle;
  39. unsigned long last_dock_time;
  40. u32 flags;
  41. spinlock_t dd_lock;
  42. spinlock_t hp_lock;
  43. struct list_head dependent_devices;
  44. struct list_head hotplug_devices;
  45. };
  46. struct dock_dependent_device {
  47. struct list_head list;
  48. struct list_head hotplug_list;
  49. acpi_handle handle;
  50. acpi_notify_handler handler;
  51. void *context;
  52. };
  53. #define DOCK_DOCKING 0x00000001
  54. #define DOCK_EVENT 3
  55. #define UNDOCK_EVENT 2
  56. static struct dock_station *dock_station;
  57. /*****************************************************************************
  58. * Dock Dependent device functions *
  59. *****************************************************************************/
  60. /**
  61. * alloc_dock_dependent_device - allocate and init a dependent device
  62. * @handle: the acpi_handle of the dependent device
  63. *
  64. * Allocate memory for a dependent device structure for a device referenced
  65. * by the acpi handle
  66. */
  67. static struct dock_dependent_device *
  68. alloc_dock_dependent_device(acpi_handle handle)
  69. {
  70. struct dock_dependent_device *dd;
  71. dd = kzalloc(sizeof(*dd), GFP_KERNEL);
  72. if (dd) {
  73. dd->handle = handle;
  74. INIT_LIST_HEAD(&dd->list);
  75. INIT_LIST_HEAD(&dd->hotplug_list);
  76. }
  77. return dd;
  78. }
  79. /**
  80. * add_dock_dependent_device - associate a device with the dock station
  81. * @ds: The dock station
  82. * @dd: The dependent device
  83. *
  84. * Add the dependent device to the dock's dependent device list.
  85. */
  86. static void
  87. add_dock_dependent_device(struct dock_station *ds,
  88. struct dock_dependent_device *dd)
  89. {
  90. spin_lock(&ds->dd_lock);
  91. list_add_tail(&dd->list, &ds->dependent_devices);
  92. spin_unlock(&ds->dd_lock);
  93. }
  94. /**
  95. * dock_add_hotplug_device - associate a hotplug handler with the dock station
  96. * @ds: The dock station
  97. * @dd: The dependent device struct
  98. *
  99. * Add the dependent device to the dock's hotplug device list
  100. */
  101. static void
  102. dock_add_hotplug_device(struct dock_station *ds,
  103. struct dock_dependent_device *dd)
  104. {
  105. spin_lock(&ds->hp_lock);
  106. list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
  107. spin_unlock(&ds->hp_lock);
  108. }
  109. /**
  110. * dock_del_hotplug_device - remove a hotplug handler from the dock station
  111. * @ds: The dock station
  112. * @dd: the dependent device struct
  113. *
  114. * Delete the dependent device from the dock's hotplug device list
  115. */
  116. static void
  117. dock_del_hotplug_device(struct dock_station *ds,
  118. struct dock_dependent_device *dd)
  119. {
  120. spin_lock(&ds->hp_lock);
  121. list_del(&dd->hotplug_list);
  122. spin_unlock(&ds->hp_lock);
  123. }
  124. /**
  125. * find_dock_dependent_device - get a device dependent on this dock
  126. * @ds: the dock station
  127. * @handle: the acpi_handle of the device we want
  128. *
  129. * iterate over the dependent device list for this dock. If the
  130. * dependent device matches the handle, return.
  131. */
  132. static struct dock_dependent_device *
  133. find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
  134. {
  135. struct dock_dependent_device *dd;
  136. spin_lock(&ds->dd_lock);
  137. list_for_each_entry(dd, &ds->dependent_devices, list) {
  138. if (handle == dd->handle) {
  139. spin_unlock(&ds->dd_lock);
  140. return dd;
  141. }
  142. }
  143. spin_unlock(&ds->dd_lock);
  144. return NULL;
  145. }
  146. /*****************************************************************************
  147. * Dock functions *
  148. *****************************************************************************/
  149. /**
  150. * is_dock - see if a device is a dock station
  151. * @handle: acpi handle of the device
  152. *
  153. * If an acpi object has a _DCK method, then it is by definition a dock
  154. * station, so return true.
  155. */
  156. static int is_dock(acpi_handle handle)
  157. {
  158. acpi_status status;
  159. acpi_handle tmp;
  160. status = acpi_get_handle(handle, "_DCK", &tmp);
  161. if (ACPI_FAILURE(status))
  162. return 0;
  163. return 1;
  164. }
  165. /**
  166. * is_dock_device - see if a device is on a dock station
  167. * @handle: acpi handle of the device
  168. *
  169. * If this device is either the dock station itself,
  170. * or is a device dependent on the dock station, then it
  171. * is a dock device
  172. */
  173. int is_dock_device(acpi_handle handle)
  174. {
  175. if (!dock_station)
  176. return 0;
  177. if (is_dock(handle) || find_dock_dependent_device(dock_station, handle))
  178. return 1;
  179. return 0;
  180. }
  181. EXPORT_SYMBOL_GPL(is_dock_device);
  182. /**
  183. * dock_present - see if the dock station is present.
  184. * @ds: the dock station
  185. *
  186. * execute the _STA method. note that present does not
  187. * imply that we are docked.
  188. */
  189. static int dock_present(struct dock_station *ds)
  190. {
  191. unsigned long sta;
  192. acpi_status status;
  193. if (ds) {
  194. status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
  195. if (ACPI_SUCCESS(status) && sta)
  196. return 1;
  197. }
  198. return 0;
  199. }
  200. /**
  201. * dock_create_acpi_device - add new devices to acpi
  202. * @handle - handle of the device to add
  203. *
  204. * This function will create a new acpi_device for the given
  205. * handle if one does not exist already. This should cause
  206. * acpi to scan for drivers for the given devices, and call
  207. * matching driver's add routine.
  208. *
  209. * Returns a pointer to the acpi_device corresponding to the handle.
  210. */
  211. static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
  212. {
  213. struct acpi_device *device = NULL;
  214. struct acpi_device *parent_device;
  215. acpi_handle parent;
  216. int ret;
  217. if (acpi_bus_get_device(handle, &device)) {
  218. /*
  219. * no device created for this object,
  220. * so we should create one.
  221. */
  222. acpi_get_parent(handle, &parent);
  223. if (acpi_bus_get_device(parent, &parent_device))
  224. parent_device = NULL;
  225. ret = acpi_bus_add(&device, parent_device, handle,
  226. ACPI_BUS_TYPE_DEVICE);
  227. if (ret) {
  228. pr_debug("error adding bus, %x\n",
  229. -ret);
  230. return NULL;
  231. }
  232. }
  233. return device;
  234. }
  235. /**
  236. * dock_remove_acpi_device - remove the acpi_device struct from acpi
  237. * @handle - the handle of the device to remove
  238. *
  239. * Tell acpi to remove the acpi_device. This should cause any loaded
  240. * driver to have it's remove routine called.
  241. */
  242. static void dock_remove_acpi_device(acpi_handle handle)
  243. {
  244. struct acpi_device *device;
  245. int ret;
  246. if (!acpi_bus_get_device(handle, &device)) {
  247. ret = acpi_bus_trim(device, 1);
  248. if (ret)
  249. pr_debug("error removing bus, %x\n", -ret);
  250. }
  251. }
  252. /**
  253. * hotplug_dock_devices - insert or remove devices on the dock station
  254. * @ds: the dock station
  255. * @event: either bus check or eject request
  256. *
  257. * Some devices on the dock station need to have drivers called
  258. * to perform hotplug operations after a dock event has occurred.
  259. * Traverse the list of dock devices that have registered a
  260. * hotplug handler, and call the handler.
  261. */
  262. static void hotplug_dock_devices(struct dock_station *ds, u32 event)
  263. {
  264. struct dock_dependent_device *dd;
  265. spin_lock(&ds->hp_lock);
  266. /*
  267. * First call driver specific hotplug functions
  268. */
  269. list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
  270. if (dd->handler)
  271. dd->handler(dd->handle, event, dd->context);
  272. }
  273. /*
  274. * Now make sure that an acpi_device is created for each
  275. * dependent device, or removed if this is an eject request.
  276. * This will cause acpi_drivers to be stopped/started if they
  277. * exist
  278. */
  279. list_for_each_entry(dd, &ds->dependent_devices, list) {
  280. if (event == ACPI_NOTIFY_EJECT_REQUEST)
  281. dock_remove_acpi_device(dd->handle);
  282. else
  283. dock_create_acpi_device(dd->handle);
  284. }
  285. spin_unlock(&ds->hp_lock);
  286. }
  287. static void dock_event(struct dock_station *ds, u32 event, int num)
  288. {
  289. /*
  290. * we don't do events until someone tells me that
  291. * they would like to have them.
  292. */
  293. }
  294. /**
  295. * eject_dock - respond to a dock eject request
  296. * @ds: the dock station
  297. *
  298. * This is called after _DCK is called, to execute the dock station's
  299. * _EJ0 method.
  300. */
  301. static void eject_dock(struct dock_station *ds)
  302. {
  303. struct acpi_object_list arg_list;
  304. union acpi_object arg;
  305. acpi_status status;
  306. acpi_handle tmp;
  307. /* all dock devices should have _EJ0, but check anyway */
  308. status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
  309. if (ACPI_FAILURE(status)) {
  310. pr_debug("No _EJ0 support for dock device\n");
  311. return;
  312. }
  313. arg_list.count = 1;
  314. arg_list.pointer = &arg;
  315. arg.type = ACPI_TYPE_INTEGER;
  316. arg.integer.value = 1;
  317. if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
  318. &arg_list, NULL)))
  319. pr_debug("Failed to evaluate _EJ0!\n");
  320. }
  321. /**
  322. * handle_dock - handle a dock event
  323. * @ds: the dock station
  324. * @dock: to dock, or undock - that is the question
  325. *
  326. * Execute the _DCK method in response to an acpi event
  327. */
  328. static void handle_dock(struct dock_station *ds, int dock)
  329. {
  330. acpi_status status;
  331. struct acpi_object_list arg_list;
  332. union acpi_object arg;
  333. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  334. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  335. union acpi_object *obj;
  336. acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
  337. obj = name_buffer.pointer;
  338. printk(KERN_INFO PREFIX "%s\n", dock ? "docking" : "undocking");
  339. /* _DCK method has one argument */
  340. arg_list.count = 1;
  341. arg_list.pointer = &arg;
  342. arg.type = ACPI_TYPE_INTEGER;
  343. arg.integer.value = dock;
  344. status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
  345. if (ACPI_FAILURE(status))
  346. pr_debug("%s: failed to execute _DCK\n", obj->string.pointer);
  347. kfree(buffer.pointer);
  348. kfree(name_buffer.pointer);
  349. }
  350. static inline void dock(struct dock_station *ds)
  351. {
  352. handle_dock(ds, 1);
  353. }
  354. static inline void undock(struct dock_station *ds)
  355. {
  356. handle_dock(ds, 0);
  357. }
  358. static inline void begin_dock(struct dock_station *ds)
  359. {
  360. ds->flags |= DOCK_DOCKING;
  361. }
  362. static inline void complete_dock(struct dock_station *ds)
  363. {
  364. ds->flags &= ~(DOCK_DOCKING);
  365. ds->last_dock_time = jiffies;
  366. }
  367. /**
  368. * dock_in_progress - see if we are in the middle of handling a dock event
  369. * @ds: the dock station
  370. *
  371. * Sometimes while docking, false dock events can be sent to the driver
  372. * because good connections aren't made or some other reason. Ignore these
  373. * if we are in the middle of doing something.
  374. */
  375. static int dock_in_progress(struct dock_station *ds)
  376. {
  377. if ((ds->flags & DOCK_DOCKING) ||
  378. time_before(jiffies, (ds->last_dock_time + HZ)))
  379. return 1;
  380. return 0;
  381. }
  382. /**
  383. * register_dock_notifier - add yourself to the dock notifier list
  384. * @nb: the callers notifier block
  385. *
  386. * If a driver wishes to be notified about dock events, they can
  387. * use this function to put a notifier block on the dock notifier list.
  388. * this notifier call chain will be called after a dock event, but
  389. * before hotplugging any new devices.
  390. */
  391. int register_dock_notifier(struct notifier_block *nb)
  392. {
  393. return atomic_notifier_chain_register(&dock_notifier_list, nb);
  394. }
  395. EXPORT_SYMBOL_GPL(register_dock_notifier);
  396. /**
  397. * unregister_dock_notifier - remove yourself from the dock notifier list
  398. * @nb: the callers notifier block
  399. */
  400. void unregister_dock_notifier(struct notifier_block *nb)
  401. {
  402. atomic_notifier_chain_unregister(&dock_notifier_list, nb);
  403. }
  404. EXPORT_SYMBOL_GPL(unregister_dock_notifier);
  405. /**
  406. * register_hotplug_dock_device - register a hotplug function
  407. * @handle: the handle of the device
  408. * @handler: the acpi_notifier_handler to call after docking
  409. * @context: device specific data
  410. *
  411. * If a driver would like to perform a hotplug operation after a dock
  412. * event, they can register an acpi_notifiy_handler to be called by
  413. * the dock driver after _DCK is executed.
  414. */
  415. int
  416. register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
  417. void *context)
  418. {
  419. struct dock_dependent_device *dd;
  420. if (!dock_station)
  421. return -ENODEV;
  422. /*
  423. * make sure this handle is for a device dependent on the dock,
  424. * this would include the dock station itself
  425. */
  426. dd = find_dock_dependent_device(dock_station, handle);
  427. if (dd) {
  428. dd->handler = handler;
  429. dd->context = context;
  430. dock_add_hotplug_device(dock_station, dd);
  431. return 0;
  432. }
  433. return -EINVAL;
  434. }
  435. EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
  436. /**
  437. * unregister_hotplug_dock_device - remove yourself from the hotplug list
  438. * @handle: the acpi handle of the device
  439. */
  440. void unregister_hotplug_dock_device(acpi_handle handle)
  441. {
  442. struct dock_dependent_device *dd;
  443. if (!dock_station)
  444. return;
  445. dd = find_dock_dependent_device(dock_station, handle);
  446. if (dd)
  447. dock_del_hotplug_device(dock_station, dd);
  448. }
  449. EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
  450. /**
  451. * dock_notify - act upon an acpi dock notification
  452. * @handle: the dock station handle
  453. * @event: the acpi event
  454. * @data: our driver data struct
  455. *
  456. * If we are notified to dock, then check to see if the dock is
  457. * present and then dock. Notify all drivers of the dock event,
  458. * and then hotplug and devices that may need hotplugging. For undock
  459. * check to make sure the dock device is still present, then undock
  460. * and hotremove all the devices that may need removing.
  461. */
  462. static void dock_notify(acpi_handle handle, u32 event, void *data)
  463. {
  464. struct dock_station *ds = (struct dock_station *)data;
  465. switch (event) {
  466. case ACPI_NOTIFY_BUS_CHECK:
  467. if (!dock_in_progress(ds) && dock_present(ds)) {
  468. begin_dock(ds);
  469. dock(ds);
  470. if (!dock_present(ds)) {
  471. printk(KERN_ERR PREFIX "Unable to dock!\n");
  472. break;
  473. }
  474. atomic_notifier_call_chain(&dock_notifier_list,
  475. event, NULL);
  476. hotplug_dock_devices(ds, event);
  477. complete_dock(ds);
  478. dock_event(ds, event, DOCK_EVENT);
  479. }
  480. break;
  481. case ACPI_NOTIFY_DEVICE_CHECK:
  482. /*
  483. * According to acpi spec 3.0a, if a DEVICE_CHECK notification
  484. * is sent and _DCK is present, it is assumed to mean an
  485. * undock request. This notify routine will only be called
  486. * for objects defining _DCK, so we will fall through to eject
  487. * request here. However, we will pass an eject request through
  488. * to the driver who wish to hotplug.
  489. */
  490. case ACPI_NOTIFY_EJECT_REQUEST:
  491. if (!dock_in_progress(ds) && dock_present(ds)) {
  492. /*
  493. * here we need to generate the undock
  494. * event prior to actually doing the undock
  495. * so that the device struct still exists.
  496. */
  497. dock_event(ds, event, UNDOCK_EVENT);
  498. hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
  499. undock(ds);
  500. eject_dock(ds);
  501. if (dock_present(ds))
  502. printk(KERN_ERR PREFIX "Unable to undock!\n");
  503. }
  504. break;
  505. default:
  506. printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
  507. }
  508. }
  509. /**
  510. * find_dock_devices - find devices on the dock station
  511. * @handle: the handle of the device we are examining
  512. * @lvl: unused
  513. * @context: the dock station private data
  514. * @rv: unused
  515. *
  516. * This function is called by acpi_walk_namespace. It will
  517. * check to see if an object has an _EJD method. If it does, then it
  518. * will see if it is dependent on the dock station.
  519. */
  520. static acpi_status
  521. find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
  522. {
  523. acpi_status status;
  524. acpi_handle tmp;
  525. struct dock_station *ds = (struct dock_station *)context;
  526. struct dock_dependent_device *dd;
  527. status = acpi_bus_get_ejd(handle, &tmp);
  528. if (ACPI_FAILURE(status))
  529. return AE_OK;
  530. if (tmp == ds->handle) {
  531. dd = alloc_dock_dependent_device(handle);
  532. if (dd)
  533. add_dock_dependent_device(ds, dd);
  534. }
  535. return AE_OK;
  536. }
  537. /**
  538. * dock_add - add a new dock station
  539. * @handle: the dock station handle
  540. *
  541. * allocated and initialize a new dock station device. Find all devices
  542. * that are on the dock station, and register for dock event notifications.
  543. */
  544. static int dock_add(acpi_handle handle)
  545. {
  546. int ret;
  547. acpi_status status;
  548. struct dock_dependent_device *dd;
  549. /* allocate & initialize the dock_station private data */
  550. dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
  551. if (!dock_station)
  552. return -ENOMEM;
  553. dock_station->handle = handle;
  554. dock_station->last_dock_time = jiffies - HZ;
  555. INIT_LIST_HEAD(&dock_station->dependent_devices);
  556. INIT_LIST_HEAD(&dock_station->hotplug_devices);
  557. spin_lock_init(&dock_station->dd_lock);
  558. spin_lock_init(&dock_station->hp_lock);
  559. ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
  560. /* Find dependent devices */
  561. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  562. ACPI_UINT32_MAX, find_dock_devices, dock_station,
  563. NULL);
  564. /* add the dock station as a device dependent on itself */
  565. dd = alloc_dock_dependent_device(handle);
  566. if (!dd) {
  567. kfree(dock_station);
  568. return -ENOMEM;
  569. }
  570. add_dock_dependent_device(dock_station, dd);
  571. /* register for dock events */
  572. status = acpi_install_notify_handler(dock_station->handle,
  573. ACPI_SYSTEM_NOTIFY,
  574. dock_notify, dock_station);
  575. if (ACPI_FAILURE(status)) {
  576. printk(KERN_ERR PREFIX "Error installing notify handler\n");
  577. ret = -ENODEV;
  578. goto dock_add_err;
  579. }
  580. printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_NAME);
  581. return 0;
  582. dock_add_err:
  583. kfree(dock_station);
  584. kfree(dd);
  585. return ret;
  586. }
  587. /**
  588. * dock_remove - free up resources related to the dock station
  589. */
  590. static int dock_remove(void)
  591. {
  592. struct dock_dependent_device *dd, *tmp;
  593. acpi_status status;
  594. if (!dock_station)
  595. return 0;
  596. /* remove dependent devices */
  597. list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
  598. list)
  599. kfree(dd);
  600. /* remove dock notify handler */
  601. status = acpi_remove_notify_handler(dock_station->handle,
  602. ACPI_SYSTEM_NOTIFY,
  603. dock_notify);
  604. if (ACPI_FAILURE(status))
  605. printk(KERN_ERR "Error removing notify handler\n");
  606. /* free dock station memory */
  607. kfree(dock_station);
  608. return 0;
  609. }
  610. /**
  611. * find_dock - look for a dock station
  612. * @handle: acpi handle of a device
  613. * @lvl: unused
  614. * @context: counter of dock stations found
  615. * @rv: unused
  616. *
  617. * This is called by acpi_walk_namespace to look for dock stations.
  618. */
  619. static acpi_status
  620. find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
  621. {
  622. int *count = (int *)context;
  623. acpi_status status = AE_OK;
  624. if (is_dock(handle)) {
  625. if (dock_add(handle) >= 0) {
  626. (*count)++;
  627. status = AE_CTRL_TERMINATE;
  628. }
  629. }
  630. return status;
  631. }
  632. static int __init dock_init(void)
  633. {
  634. int num = 0;
  635. dock_station = NULL;
  636. /* look for a dock station */
  637. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  638. ACPI_UINT32_MAX, find_dock, &num, NULL);
  639. if (!num)
  640. return -ENODEV;
  641. return 0;
  642. }
  643. static void __exit dock_exit(void)
  644. {
  645. dock_remove();
  646. }
  647. postcore_initcall(dock_init);
  648. module_exit(dock_exit);