dock.c 24 KB

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