dock.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. struct mutex 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. mutex_lock(&ds->hp_lock);
  110. list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
  111. mutex_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. mutex_lock(&ds->hp_lock);
  125. list_del(&dd->hotplug_list);
  126. mutex_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. mutex_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. mutex_unlock(&ds->hp_lock);
  290. }
  291. static void dock_event(struct dock_station *ds, u32 event, int num)
  292. {
  293. struct device *dev = &dock_device.dev;
  294. /*
  295. * Indicate that the status of the dock station has
  296. * changed.
  297. */
  298. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  299. }
  300. /**
  301. * eject_dock - respond to a dock eject request
  302. * @ds: the dock station
  303. *
  304. * This is called after _DCK is called, to execute the dock station's
  305. * _EJ0 method.
  306. */
  307. static void eject_dock(struct dock_station *ds)
  308. {
  309. struct acpi_object_list arg_list;
  310. union acpi_object arg;
  311. acpi_status status;
  312. acpi_handle tmp;
  313. /* all dock devices should have _EJ0, but check anyway */
  314. status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
  315. if (ACPI_FAILURE(status)) {
  316. pr_debug("No _EJ0 support for dock device\n");
  317. return;
  318. }
  319. arg_list.count = 1;
  320. arg_list.pointer = &arg;
  321. arg.type = ACPI_TYPE_INTEGER;
  322. arg.integer.value = 1;
  323. if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
  324. &arg_list, NULL)))
  325. pr_debug("Failed to evaluate _EJ0!\n");
  326. }
  327. /**
  328. * handle_dock - handle a dock event
  329. * @ds: the dock station
  330. * @dock: to dock, or undock - that is the question
  331. *
  332. * Execute the _DCK method in response to an acpi event
  333. */
  334. static void handle_dock(struct dock_station *ds, int dock)
  335. {
  336. acpi_status status;
  337. struct acpi_object_list arg_list;
  338. union acpi_object arg;
  339. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  340. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  341. union acpi_object *obj;
  342. acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
  343. obj = name_buffer.pointer;
  344. printk(KERN_INFO PREFIX "%s\n", dock ? "docking" : "undocking");
  345. /* _DCK method has one argument */
  346. arg_list.count = 1;
  347. arg_list.pointer = &arg;
  348. arg.type = ACPI_TYPE_INTEGER;
  349. arg.integer.value = dock;
  350. status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
  351. if (ACPI_FAILURE(status))
  352. pr_debug("%s: failed to execute _DCK\n", obj->string.pointer);
  353. kfree(buffer.pointer);
  354. kfree(name_buffer.pointer);
  355. }
  356. static inline void dock(struct dock_station *ds)
  357. {
  358. handle_dock(ds, 1);
  359. }
  360. static inline void undock(struct dock_station *ds)
  361. {
  362. handle_dock(ds, 0);
  363. }
  364. static inline void begin_dock(struct dock_station *ds)
  365. {
  366. ds->flags |= DOCK_DOCKING;
  367. }
  368. static inline void complete_dock(struct dock_station *ds)
  369. {
  370. ds->flags &= ~(DOCK_DOCKING);
  371. ds->last_dock_time = jiffies;
  372. }
  373. /**
  374. * dock_in_progress - see if we are in the middle of handling a dock event
  375. * @ds: the dock station
  376. *
  377. * Sometimes while docking, false dock events can be sent to the driver
  378. * because good connections aren't made or some other reason. Ignore these
  379. * if we are in the middle of doing something.
  380. */
  381. static int dock_in_progress(struct dock_station *ds)
  382. {
  383. if ((ds->flags & DOCK_DOCKING) ||
  384. time_before(jiffies, (ds->last_dock_time + HZ)))
  385. return 1;
  386. return 0;
  387. }
  388. /**
  389. * register_dock_notifier - add yourself to the dock notifier list
  390. * @nb: the callers notifier block
  391. *
  392. * If a driver wishes to be notified about dock events, they can
  393. * use this function to put a notifier block on the dock notifier list.
  394. * this notifier call chain will be called after a dock event, but
  395. * before hotplugging any new devices.
  396. */
  397. int register_dock_notifier(struct notifier_block *nb)
  398. {
  399. if (!dock_station)
  400. return -ENODEV;
  401. return atomic_notifier_chain_register(&dock_notifier_list, nb);
  402. }
  403. EXPORT_SYMBOL_GPL(register_dock_notifier);
  404. /**
  405. * unregister_dock_notifier - remove yourself from the dock notifier list
  406. * @nb: the callers notifier block
  407. */
  408. void unregister_dock_notifier(struct notifier_block *nb)
  409. {
  410. if (!dock_station)
  411. return;
  412. atomic_notifier_chain_unregister(&dock_notifier_list, nb);
  413. }
  414. EXPORT_SYMBOL_GPL(unregister_dock_notifier);
  415. /**
  416. * register_hotplug_dock_device - register a hotplug function
  417. * @handle: the handle of the device
  418. * @handler: the acpi_notifier_handler to call after docking
  419. * @context: device specific data
  420. *
  421. * If a driver would like to perform a hotplug operation after a dock
  422. * event, they can register an acpi_notifiy_handler to be called by
  423. * the dock driver after _DCK is executed.
  424. */
  425. int
  426. register_hotplug_dock_device(acpi_handle handle, acpi_notify_handler handler,
  427. void *context)
  428. {
  429. struct dock_dependent_device *dd;
  430. if (!dock_station)
  431. return -ENODEV;
  432. /*
  433. * make sure this handle is for a device dependent on the dock,
  434. * this would include the dock station itself
  435. */
  436. dd = find_dock_dependent_device(dock_station, handle);
  437. if (dd) {
  438. dd->handler = handler;
  439. dd->context = context;
  440. dock_add_hotplug_device(dock_station, dd);
  441. return 0;
  442. }
  443. return -EINVAL;
  444. }
  445. EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
  446. /**
  447. * unregister_hotplug_dock_device - remove yourself from the hotplug list
  448. * @handle: the acpi handle of the device
  449. */
  450. void unregister_hotplug_dock_device(acpi_handle handle)
  451. {
  452. struct dock_dependent_device *dd;
  453. if (!dock_station)
  454. return;
  455. dd = find_dock_dependent_device(dock_station, handle);
  456. if (dd)
  457. dock_del_hotplug_device(dock_station, dd);
  458. }
  459. EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
  460. /**
  461. * handle_eject_request - handle an undock request checking for error conditions
  462. *
  463. * Check to make sure the dock device is still present, then undock and
  464. * hotremove all the devices that may need removing.
  465. */
  466. static int handle_eject_request(struct dock_station *ds, u32 event)
  467. {
  468. if (!dock_present(ds))
  469. return -ENODEV;
  470. if (dock_in_progress(ds))
  471. return -EBUSY;
  472. /*
  473. * here we need to generate the undock
  474. * event prior to actually doing the undock
  475. * so that the device struct still exists.
  476. */
  477. dock_event(ds, event, UNDOCK_EVENT);
  478. hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
  479. undock(ds);
  480. eject_dock(ds);
  481. if (dock_present(ds)) {
  482. printk(KERN_ERR PREFIX "Unable to undock!\n");
  483. return -EBUSY;
  484. }
  485. return 0;
  486. }
  487. /**
  488. * dock_notify - act upon an acpi dock notification
  489. * @handle: the dock station handle
  490. * @event: the acpi event
  491. * @data: our driver data struct
  492. *
  493. * If we are notified to dock, then check to see if the dock is
  494. * present and then dock. Notify all drivers of the dock event,
  495. * and then hotplug and devices that may need hotplugging.
  496. */
  497. static void dock_notify(acpi_handle handle, u32 event, void *data)
  498. {
  499. struct dock_station *ds = data;
  500. switch (event) {
  501. case ACPI_NOTIFY_BUS_CHECK:
  502. if (!dock_in_progress(ds) && dock_present(ds)) {
  503. begin_dock(ds);
  504. dock(ds);
  505. if (!dock_present(ds)) {
  506. printk(KERN_ERR PREFIX "Unable to dock!\n");
  507. break;
  508. }
  509. atomic_notifier_call_chain(&dock_notifier_list,
  510. event, NULL);
  511. hotplug_dock_devices(ds, event);
  512. complete_dock(ds);
  513. dock_event(ds, event, DOCK_EVENT);
  514. }
  515. break;
  516. case ACPI_NOTIFY_DEVICE_CHECK:
  517. /*
  518. * According to acpi spec 3.0a, if a DEVICE_CHECK notification
  519. * is sent and _DCK is present, it is assumed to mean an
  520. * undock request. This notify routine will only be called
  521. * for objects defining _DCK, so we will fall through to eject
  522. * request here. However, we will pass an eject request through
  523. * to the driver who wish to hotplug.
  524. */
  525. case ACPI_NOTIFY_EJECT_REQUEST:
  526. handle_eject_request(ds, event);
  527. break;
  528. default:
  529. printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
  530. }
  531. }
  532. /**
  533. * find_dock_devices - find devices on the dock station
  534. * @handle: the handle of the device we are examining
  535. * @lvl: unused
  536. * @context: the dock station private data
  537. * @rv: unused
  538. *
  539. * This function is called by acpi_walk_namespace. It will
  540. * check to see if an object has an _EJD method. If it does, then it
  541. * will see if it is dependent on the dock station.
  542. */
  543. static acpi_status
  544. find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
  545. {
  546. acpi_status status;
  547. acpi_handle tmp, parent;
  548. struct dock_station *ds = context;
  549. struct dock_dependent_device *dd;
  550. status = acpi_bus_get_ejd(handle, &tmp);
  551. if (ACPI_FAILURE(status)) {
  552. /* try the parent device as well */
  553. status = acpi_get_parent(handle, &parent);
  554. if (ACPI_FAILURE(status))
  555. goto fdd_out;
  556. /* see if parent is dependent on dock */
  557. status = acpi_bus_get_ejd(parent, &tmp);
  558. if (ACPI_FAILURE(status))
  559. goto fdd_out;
  560. }
  561. if (tmp == ds->handle) {
  562. dd = alloc_dock_dependent_device(handle);
  563. if (dd)
  564. add_dock_dependent_device(ds, dd);
  565. }
  566. fdd_out:
  567. return AE_OK;
  568. }
  569. /*
  570. * show_docked - read method for "docked" file in sysfs
  571. */
  572. static ssize_t show_docked(struct device *dev,
  573. struct device_attribute *attr, char *buf)
  574. {
  575. return snprintf(buf, PAGE_SIZE, "%d\n", dock_present(dock_station));
  576. }
  577. DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
  578. /*
  579. * write_undock - write method for "undock" file in sysfs
  580. */
  581. static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
  582. const char *buf, size_t count)
  583. {
  584. int ret;
  585. if (!count)
  586. return -EINVAL;
  587. ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
  588. return ret ? ret: count;
  589. }
  590. DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
  591. /**
  592. * dock_add - add a new dock station
  593. * @handle: the dock station handle
  594. *
  595. * allocated and initialize a new dock station device. Find all devices
  596. * that are on the dock station, and register for dock event notifications.
  597. */
  598. static int dock_add(acpi_handle handle)
  599. {
  600. int ret;
  601. acpi_status status;
  602. struct dock_dependent_device *dd;
  603. /* allocate & initialize the dock_station private data */
  604. dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
  605. if (!dock_station)
  606. return -ENOMEM;
  607. dock_station->handle = handle;
  608. dock_station->last_dock_time = jiffies - HZ;
  609. INIT_LIST_HEAD(&dock_station->dependent_devices);
  610. INIT_LIST_HEAD(&dock_station->hotplug_devices);
  611. spin_lock_init(&dock_station->dd_lock);
  612. mutex_init(&dock_station->hp_lock);
  613. ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
  614. /* initialize platform device stuff */
  615. dock_device.name = dock_device_name;
  616. ret = platform_device_register(&dock_device);
  617. if (ret) {
  618. printk(KERN_ERR PREFIX "Error %d registering dock device\n", ret);
  619. kfree(dock_station);
  620. return ret;
  621. }
  622. ret = device_create_file(&dock_device.dev, &dev_attr_docked);
  623. if (ret) {
  624. printk("Error %d adding sysfs file\n", ret);
  625. platform_device_unregister(&dock_device);
  626. kfree(dock_station);
  627. return ret;
  628. }
  629. ret = device_create_file(&dock_device.dev, &dev_attr_undock);
  630. if (ret) {
  631. printk("Error %d adding sysfs file\n", ret);
  632. device_remove_file(&dock_device.dev, &dev_attr_docked);
  633. platform_device_unregister(&dock_device);
  634. kfree(dock_station);
  635. return ret;
  636. }
  637. /* Find dependent devices */
  638. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  639. ACPI_UINT32_MAX, find_dock_devices, dock_station,
  640. NULL);
  641. /* add the dock station as a device dependent on itself */
  642. dd = alloc_dock_dependent_device(handle);
  643. if (!dd) {
  644. kfree(dock_station);
  645. ret = -ENOMEM;
  646. goto dock_add_err_unregister;
  647. }
  648. add_dock_dependent_device(dock_station, dd);
  649. /* register for dock events */
  650. status = acpi_install_notify_handler(dock_station->handle,
  651. ACPI_SYSTEM_NOTIFY,
  652. dock_notify, dock_station);
  653. if (ACPI_FAILURE(status)) {
  654. printk(KERN_ERR PREFIX "Error installing notify handler\n");
  655. ret = -ENODEV;
  656. goto dock_add_err;
  657. }
  658. printk(KERN_INFO PREFIX "%s \n", ACPI_DOCK_DRIVER_NAME);
  659. return 0;
  660. dock_add_err:
  661. kfree(dd);
  662. dock_add_err_unregister:
  663. device_remove_file(&dock_device.dev, &dev_attr_docked);
  664. device_remove_file(&dock_device.dev, &dev_attr_undock);
  665. platform_device_unregister(&dock_device);
  666. kfree(dock_station);
  667. return ret;
  668. }
  669. /**
  670. * dock_remove - free up resources related to the dock station
  671. */
  672. static int dock_remove(void)
  673. {
  674. struct dock_dependent_device *dd, *tmp;
  675. acpi_status status;
  676. if (!dock_station)
  677. return 0;
  678. /* remove dependent devices */
  679. list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
  680. list)
  681. kfree(dd);
  682. /* remove dock notify handler */
  683. status = acpi_remove_notify_handler(dock_station->handle,
  684. ACPI_SYSTEM_NOTIFY,
  685. dock_notify);
  686. if (ACPI_FAILURE(status))
  687. printk(KERN_ERR "Error removing notify handler\n");
  688. /* cleanup sysfs */
  689. device_remove_file(&dock_device.dev, &dev_attr_docked);
  690. device_remove_file(&dock_device.dev, &dev_attr_undock);
  691. platform_device_unregister(&dock_device);
  692. /* free dock station memory */
  693. kfree(dock_station);
  694. return 0;
  695. }
  696. /**
  697. * find_dock - look for a dock station
  698. * @handle: acpi handle of a device
  699. * @lvl: unused
  700. * @context: counter of dock stations found
  701. * @rv: unused
  702. *
  703. * This is called by acpi_walk_namespace to look for dock stations.
  704. */
  705. static acpi_status
  706. find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
  707. {
  708. int *count = context;
  709. acpi_status status = AE_OK;
  710. if (is_dock(handle)) {
  711. if (dock_add(handle) >= 0) {
  712. (*count)++;
  713. status = AE_CTRL_TERMINATE;
  714. }
  715. }
  716. return status;
  717. }
  718. static int __init dock_init(void)
  719. {
  720. int num = 0;
  721. dock_station = NULL;
  722. /* look for a dock station */
  723. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  724. ACPI_UINT32_MAX, find_dock, &num, NULL);
  725. if (!num)
  726. printk(KERN_INFO "No dock devices found.\n");
  727. return 0;
  728. }
  729. static void __exit dock_exit(void)
  730. {
  731. dock_remove();
  732. }
  733. postcore_initcall(dock_init);
  734. module_exit(dock_exit);