dock.c 19 KB

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