dock.c 22 KB

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