dock.c 21 KB

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