dock.c 24 KB

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