dock.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 PREFIX "ACPI: "
  35. #define ACPI_DOCK_DRIVER_DESCRIPTION "ACPI Dock Station Driver"
  36. ACPI_MODULE_NAME("dock");
  37. MODULE_AUTHOR("Kristen Carlson Accardi");
  38. MODULE_DESCRIPTION(ACPI_DOCK_DRIVER_DESCRIPTION);
  39. MODULE_LICENSE("GPL");
  40. static int immediate_undock = 1;
  41. module_param(immediate_undock, bool, 0644);
  42. MODULE_PARM_DESC(immediate_undock, "1 (default) will cause the driver to "
  43. "undock immediately when the undock button is pressed, 0 will cause"
  44. " the driver to wait for userspace to write the undock sysfs file "
  45. " before undocking");
  46. static struct atomic_notifier_head dock_notifier_list;
  47. static char dock_device_name[] = "dock";
  48. static const struct acpi_device_id dock_device_ids[] = {
  49. {"LNXDOCK", 0},
  50. {"", 0},
  51. };
  52. MODULE_DEVICE_TABLE(acpi, dock_device_ids);
  53. struct dock_station {
  54. acpi_handle handle;
  55. unsigned long last_dock_time;
  56. u32 flags;
  57. spinlock_t dd_lock;
  58. struct mutex hp_lock;
  59. struct list_head dependent_devices;
  60. struct list_head hotplug_devices;
  61. struct list_head sibling;
  62. struct platform_device *dock_device;
  63. };
  64. static LIST_HEAD(dock_stations);
  65. static int dock_station_count;
  66. struct dock_dependent_device {
  67. struct list_head list;
  68. struct list_head hotplug_list;
  69. acpi_handle handle;
  70. struct acpi_dock_ops *ops;
  71. void *context;
  72. };
  73. #define DOCK_DOCKING 0x00000001
  74. #define DOCK_UNDOCKING 0x00000002
  75. #define DOCK_IS_DOCK 0x00000010
  76. #define DOCK_IS_ATA 0x00000020
  77. #define DOCK_IS_BAT 0x00000040
  78. #define DOCK_EVENT 3
  79. #define UNDOCK_EVENT 2
  80. /*****************************************************************************
  81. * Dock Dependent device functions *
  82. *****************************************************************************/
  83. /**
  84. * add_dock_dependent_device - associate a device with the dock station
  85. * @ds: The dock station
  86. * @handle: handle of the dependent device
  87. *
  88. * Add the dependent device to the dock's dependent device list.
  89. */
  90. static int
  91. add_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
  92. {
  93. struct dock_dependent_device *dd;
  94. dd = kzalloc(sizeof(*dd), GFP_KERNEL);
  95. if (!dd)
  96. return -ENOMEM;
  97. dd->handle = handle;
  98. INIT_LIST_HEAD(&dd->list);
  99. INIT_LIST_HEAD(&dd->hotplug_list);
  100. spin_lock(&ds->dd_lock);
  101. list_add_tail(&dd->list, &ds->dependent_devices);
  102. spin_unlock(&ds->dd_lock);
  103. return 0;
  104. }
  105. /**
  106. * dock_add_hotplug_device - associate a hotplug handler with the dock station
  107. * @ds: The dock station
  108. * @dd: The dependent device struct
  109. *
  110. * Add the dependent device to the dock's hotplug device list
  111. */
  112. static void
  113. dock_add_hotplug_device(struct dock_station *ds,
  114. struct dock_dependent_device *dd)
  115. {
  116. mutex_lock(&ds->hp_lock);
  117. list_add_tail(&dd->hotplug_list, &ds->hotplug_devices);
  118. mutex_unlock(&ds->hp_lock);
  119. }
  120. /**
  121. * dock_del_hotplug_device - remove a hotplug handler from the dock station
  122. * @ds: The dock station
  123. * @dd: the dependent device struct
  124. *
  125. * Delete the dependent device from the dock's hotplug device list
  126. */
  127. static void
  128. dock_del_hotplug_device(struct dock_station *ds,
  129. struct dock_dependent_device *dd)
  130. {
  131. mutex_lock(&ds->hp_lock);
  132. list_del(&dd->hotplug_list);
  133. mutex_unlock(&ds->hp_lock);
  134. }
  135. /**
  136. * find_dock_dependent_device - get a device dependent on this dock
  137. * @ds: the dock station
  138. * @handle: the acpi_handle of the device we want
  139. *
  140. * iterate over the dependent device list for this dock. If the
  141. * dependent device matches the handle, return.
  142. */
  143. static struct dock_dependent_device *
  144. find_dock_dependent_device(struct dock_station *ds, acpi_handle handle)
  145. {
  146. struct dock_dependent_device *dd;
  147. spin_lock(&ds->dd_lock);
  148. list_for_each_entry(dd, &ds->dependent_devices, list) {
  149. if (handle == dd->handle) {
  150. spin_unlock(&ds->dd_lock);
  151. return dd;
  152. }
  153. }
  154. spin_unlock(&ds->dd_lock);
  155. return NULL;
  156. }
  157. /*****************************************************************************
  158. * Dock functions *
  159. *****************************************************************************/
  160. /**
  161. * is_dock - see if a device is a dock station
  162. * @handle: acpi handle of the device
  163. *
  164. * If an acpi object has a _DCK method, then it is by definition a dock
  165. * station, so return true.
  166. */
  167. static int is_dock(acpi_handle handle)
  168. {
  169. acpi_status status;
  170. acpi_handle tmp;
  171. status = acpi_get_handle(handle, "_DCK", &tmp);
  172. if (ACPI_FAILURE(status))
  173. return 0;
  174. return 1;
  175. }
  176. static int is_ejectable(acpi_handle handle)
  177. {
  178. acpi_status status;
  179. acpi_handle tmp;
  180. status = acpi_get_handle(handle, "_EJ0", &tmp);
  181. if (ACPI_FAILURE(status))
  182. return 0;
  183. return 1;
  184. }
  185. static int is_ata(acpi_handle handle)
  186. {
  187. acpi_handle tmp;
  188. if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
  189. (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
  190. (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
  191. (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
  192. return 1;
  193. return 0;
  194. }
  195. static int is_battery(acpi_handle handle)
  196. {
  197. struct acpi_device_info *info;
  198. int ret = 1;
  199. if (!ACPI_SUCCESS(acpi_get_object_info(handle, &info)))
  200. return 0;
  201. if (!(info->valid & ACPI_VALID_HID))
  202. ret = 0;
  203. else
  204. ret = !strcmp("PNP0C0A", info->hardware_id.string);
  205. kfree(info);
  206. return ret;
  207. }
  208. static int is_ejectable_bay(acpi_handle handle)
  209. {
  210. acpi_handle phandle;
  211. if (!is_ejectable(handle))
  212. return 0;
  213. if (is_battery(handle) || is_ata(handle))
  214. return 1;
  215. if (!acpi_get_parent(handle, &phandle) && is_ata(phandle))
  216. return 1;
  217. return 0;
  218. }
  219. /**
  220. * is_dock_device - see if a device is on a dock station
  221. * @handle: acpi handle of the device
  222. *
  223. * If this device is either the dock station itself,
  224. * or is a device dependent on the dock station, then it
  225. * is a dock device
  226. */
  227. int is_dock_device(acpi_handle handle)
  228. {
  229. struct dock_station *dock_station;
  230. if (!dock_station_count)
  231. return 0;
  232. if (is_dock(handle))
  233. return 1;
  234. list_for_each_entry(dock_station, &dock_stations, sibling) {
  235. if (find_dock_dependent_device(dock_station, handle))
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. EXPORT_SYMBOL_GPL(is_dock_device);
  241. /**
  242. * dock_present - see if the dock station is present.
  243. * @ds: the dock station
  244. *
  245. * execute the _STA method. note that present does not
  246. * imply that we are docked.
  247. */
  248. static int dock_present(struct dock_station *ds)
  249. {
  250. unsigned long long sta;
  251. acpi_status status;
  252. if (ds) {
  253. status = acpi_evaluate_integer(ds->handle, "_STA", NULL, &sta);
  254. if (ACPI_SUCCESS(status) && sta)
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. /**
  260. * dock_create_acpi_device - add new devices to acpi
  261. * @handle - handle of the device to add
  262. *
  263. * This function will create a new acpi_device for the given
  264. * handle if one does not exist already. This should cause
  265. * acpi to scan for drivers for the given devices, and call
  266. * matching driver's add routine.
  267. *
  268. * Returns a pointer to the acpi_device corresponding to the handle.
  269. */
  270. static struct acpi_device * dock_create_acpi_device(acpi_handle handle)
  271. {
  272. struct acpi_device *device = NULL;
  273. struct acpi_device *parent_device;
  274. acpi_handle parent;
  275. int ret;
  276. if (acpi_bus_get_device(handle, &device)) {
  277. /*
  278. * no device created for this object,
  279. * so we should create one.
  280. */
  281. acpi_get_parent(handle, &parent);
  282. if (acpi_bus_get_device(parent, &parent_device))
  283. parent_device = NULL;
  284. ret = acpi_bus_add(&device, parent_device, handle,
  285. ACPI_BUS_TYPE_DEVICE);
  286. if (ret) {
  287. pr_debug("error adding bus, %x\n",
  288. -ret);
  289. return NULL;
  290. }
  291. }
  292. return device;
  293. }
  294. /**
  295. * dock_remove_acpi_device - remove the acpi_device struct from acpi
  296. * @handle - the handle of the device to remove
  297. *
  298. * Tell acpi to remove the acpi_device. This should cause any loaded
  299. * driver to have it's remove routine called.
  300. */
  301. static void dock_remove_acpi_device(acpi_handle handle)
  302. {
  303. struct acpi_device *device;
  304. int ret;
  305. if (!acpi_bus_get_device(handle, &device)) {
  306. ret = acpi_bus_trim(device, 1);
  307. if (ret)
  308. pr_debug("error removing bus, %x\n", -ret);
  309. }
  310. }
  311. /**
  312. * hotplug_dock_devices - insert or remove devices on the dock station
  313. * @ds: the dock station
  314. * @event: either bus check or eject request
  315. *
  316. * Some devices on the dock station need to have drivers called
  317. * to perform hotplug operations after a dock event has occurred.
  318. * Traverse the list of dock devices that have registered a
  319. * hotplug handler, and call the handler.
  320. */
  321. static void hotplug_dock_devices(struct dock_station *ds, u32 event)
  322. {
  323. struct dock_dependent_device *dd;
  324. mutex_lock(&ds->hp_lock);
  325. /*
  326. * First call driver specific hotplug functions
  327. */
  328. list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list) {
  329. if (dd->ops && dd->ops->handler)
  330. dd->ops->handler(dd->handle, event, dd->context);
  331. }
  332. /*
  333. * Now make sure that an acpi_device is created for each
  334. * dependent device, or removed if this is an eject request.
  335. * This will cause acpi_drivers to be stopped/started if they
  336. * exist
  337. */
  338. list_for_each_entry(dd, &ds->dependent_devices, list) {
  339. if (event == ACPI_NOTIFY_EJECT_REQUEST)
  340. dock_remove_acpi_device(dd->handle);
  341. else
  342. dock_create_acpi_device(dd->handle);
  343. }
  344. mutex_unlock(&ds->hp_lock);
  345. }
  346. static void dock_event(struct dock_station *ds, u32 event, int num)
  347. {
  348. struct device *dev = &ds->dock_device->dev;
  349. char event_string[13];
  350. char *envp[] = { event_string, NULL };
  351. struct dock_dependent_device *dd;
  352. if (num == UNDOCK_EVENT)
  353. sprintf(event_string, "EVENT=undock");
  354. else
  355. sprintf(event_string, "EVENT=dock");
  356. /*
  357. * Indicate that the status of the dock station has
  358. * changed.
  359. */
  360. if (num == DOCK_EVENT)
  361. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  362. list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
  363. if (dd->ops && dd->ops->uevent)
  364. dd->ops->uevent(dd->handle, event, dd->context);
  365. if (num != DOCK_EVENT)
  366. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  367. }
  368. /**
  369. * eject_dock - respond to a dock eject request
  370. * @ds: the dock station
  371. *
  372. * This is called after _DCK is called, to execute the dock station's
  373. * _EJ0 method.
  374. */
  375. static void eject_dock(struct dock_station *ds)
  376. {
  377. struct acpi_object_list arg_list;
  378. union acpi_object arg;
  379. acpi_status status;
  380. acpi_handle tmp;
  381. /* all dock devices should have _EJ0, but check anyway */
  382. status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
  383. if (ACPI_FAILURE(status)) {
  384. pr_debug("No _EJ0 support for dock device\n");
  385. return;
  386. }
  387. arg_list.count = 1;
  388. arg_list.pointer = &arg;
  389. arg.type = ACPI_TYPE_INTEGER;
  390. arg.integer.value = 1;
  391. if (ACPI_FAILURE(acpi_evaluate_object(ds->handle, "_EJ0",
  392. &arg_list, NULL)))
  393. pr_debug("Failed to evaluate _EJ0!\n");
  394. }
  395. /**
  396. * handle_dock - handle a dock event
  397. * @ds: the dock station
  398. * @dock: to dock, or undock - that is the question
  399. *
  400. * Execute the _DCK method in response to an acpi event
  401. */
  402. static void handle_dock(struct dock_station *ds, int dock)
  403. {
  404. acpi_status status;
  405. struct acpi_object_list arg_list;
  406. union acpi_object arg;
  407. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  408. struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  409. acpi_get_name(ds->handle, ACPI_FULL_PATHNAME, &name_buffer);
  410. printk(KERN_INFO PREFIX "%s - %s\n",
  411. (char *)name_buffer.pointer, dock ? "docking" : "undocking");
  412. /* _DCK method has one argument */
  413. arg_list.count = 1;
  414. arg_list.pointer = &arg;
  415. arg.type = ACPI_TYPE_INTEGER;
  416. arg.integer.value = dock;
  417. status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
  418. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  419. ACPI_EXCEPTION((AE_INFO, status, "%s - failed to execute"
  420. " _DCK\n", (char *)name_buffer.pointer));
  421. kfree(buffer.pointer);
  422. kfree(name_buffer.pointer);
  423. }
  424. static inline void dock(struct dock_station *ds)
  425. {
  426. handle_dock(ds, 1);
  427. }
  428. static inline void undock(struct dock_station *ds)
  429. {
  430. handle_dock(ds, 0);
  431. }
  432. static inline void begin_dock(struct dock_station *ds)
  433. {
  434. ds->flags |= DOCK_DOCKING;
  435. }
  436. static inline void complete_dock(struct dock_station *ds)
  437. {
  438. ds->flags &= ~(DOCK_DOCKING);
  439. ds->last_dock_time = jiffies;
  440. }
  441. static inline void begin_undock(struct dock_station *ds)
  442. {
  443. ds->flags |= DOCK_UNDOCKING;
  444. }
  445. static inline void complete_undock(struct dock_station *ds)
  446. {
  447. ds->flags &= ~(DOCK_UNDOCKING);
  448. }
  449. static void dock_lock(struct dock_station *ds, int lock)
  450. {
  451. struct acpi_object_list arg_list;
  452. union acpi_object arg;
  453. acpi_status status;
  454. arg_list.count = 1;
  455. arg_list.pointer = &arg;
  456. arg.type = ACPI_TYPE_INTEGER;
  457. arg.integer.value = !!lock;
  458. status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
  459. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  460. if (lock)
  461. printk(KERN_WARNING PREFIX "Locking device failed\n");
  462. else
  463. printk(KERN_WARNING PREFIX "Unlocking device failed\n");
  464. }
  465. }
  466. /**
  467. * dock_in_progress - see if we are in the middle of handling a dock event
  468. * @ds: the dock station
  469. *
  470. * Sometimes while docking, false dock events can be sent to the driver
  471. * because good connections aren't made or some other reason. Ignore these
  472. * if we are in the middle of doing something.
  473. */
  474. static int dock_in_progress(struct dock_station *ds)
  475. {
  476. if ((ds->flags & DOCK_DOCKING) ||
  477. time_before(jiffies, (ds->last_dock_time + HZ)))
  478. return 1;
  479. return 0;
  480. }
  481. /**
  482. * register_dock_notifier - add yourself to the dock notifier list
  483. * @nb: the callers notifier block
  484. *
  485. * If a driver wishes to be notified about dock events, they can
  486. * use this function to put a notifier block on the dock notifier list.
  487. * this notifier call chain will be called after a dock event, but
  488. * before hotplugging any new devices.
  489. */
  490. int register_dock_notifier(struct notifier_block *nb)
  491. {
  492. if (!dock_station_count)
  493. return -ENODEV;
  494. return atomic_notifier_chain_register(&dock_notifier_list, nb);
  495. }
  496. EXPORT_SYMBOL_GPL(register_dock_notifier);
  497. /**
  498. * unregister_dock_notifier - remove yourself from the dock notifier list
  499. * @nb: the callers notifier block
  500. */
  501. void unregister_dock_notifier(struct notifier_block *nb)
  502. {
  503. if (!dock_station_count)
  504. return;
  505. atomic_notifier_chain_unregister(&dock_notifier_list, nb);
  506. }
  507. EXPORT_SYMBOL_GPL(unregister_dock_notifier);
  508. /**
  509. * register_hotplug_dock_device - register a hotplug function
  510. * @handle: the handle of the device
  511. * @ops: handlers to call after docking
  512. * @context: device specific data
  513. *
  514. * If a driver would like to perform a hotplug operation after a dock
  515. * event, they can register an acpi_notifiy_handler to be called by
  516. * the dock driver after _DCK is executed.
  517. */
  518. int
  519. register_hotplug_dock_device(acpi_handle handle, struct acpi_dock_ops *ops,
  520. void *context)
  521. {
  522. struct dock_dependent_device *dd;
  523. struct dock_station *dock_station;
  524. int ret = -EINVAL;
  525. if (!dock_station_count)
  526. return -ENODEV;
  527. /*
  528. * make sure this handle is for a device dependent on the dock,
  529. * this would include the dock station itself
  530. */
  531. list_for_each_entry(dock_station, &dock_stations, sibling) {
  532. /*
  533. * An ATA bay can be in a dock and itself can be ejected
  534. * seperately, so there are two 'dock stations' which need the
  535. * ops
  536. */
  537. dd = find_dock_dependent_device(dock_station, handle);
  538. if (dd) {
  539. dd->ops = ops;
  540. dd->context = context;
  541. dock_add_hotplug_device(dock_station, dd);
  542. ret = 0;
  543. }
  544. }
  545. return ret;
  546. }
  547. EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
  548. /**
  549. * unregister_hotplug_dock_device - remove yourself from the hotplug list
  550. * @handle: the acpi handle of the device
  551. */
  552. void unregister_hotplug_dock_device(acpi_handle handle)
  553. {
  554. struct dock_dependent_device *dd;
  555. struct dock_station *dock_station;
  556. if (!dock_station_count)
  557. return;
  558. list_for_each_entry(dock_station, &dock_stations, sibling) {
  559. dd = find_dock_dependent_device(dock_station, handle);
  560. if (dd)
  561. dock_del_hotplug_device(dock_station, dd);
  562. }
  563. }
  564. EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
  565. /**
  566. * handle_eject_request - handle an undock request checking for error conditions
  567. *
  568. * Check to make sure the dock device is still present, then undock and
  569. * hotremove all the devices that may need removing.
  570. */
  571. static int handle_eject_request(struct dock_station *ds, u32 event)
  572. {
  573. if (dock_in_progress(ds))
  574. return -EBUSY;
  575. /*
  576. * here we need to generate the undock
  577. * event prior to actually doing the undock
  578. * so that the device struct still exists.
  579. * Also, even send the dock event if the
  580. * device is not present anymore
  581. */
  582. dock_event(ds, event, UNDOCK_EVENT);
  583. hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
  584. undock(ds);
  585. dock_lock(ds, 0);
  586. eject_dock(ds);
  587. if (dock_present(ds)) {
  588. printk(KERN_ERR PREFIX "Unable to undock!\n");
  589. return -EBUSY;
  590. }
  591. complete_undock(ds);
  592. return 0;
  593. }
  594. /**
  595. * dock_notify - act upon an acpi dock notification
  596. * @handle: the dock station handle
  597. * @event: the acpi event
  598. * @data: our driver data struct
  599. *
  600. * If we are notified to dock, then check to see if the dock is
  601. * present and then dock. Notify all drivers of the dock event,
  602. * and then hotplug and devices that may need hotplugging.
  603. */
  604. static void dock_notify(acpi_handle handle, u32 event, void *data)
  605. {
  606. struct dock_station *ds = data;
  607. struct acpi_device *tmp;
  608. int surprise_removal = 0;
  609. /*
  610. * According to acpi spec 3.0a, if a DEVICE_CHECK notification
  611. * is sent and _DCK is present, it is assumed to mean an undock
  612. * request.
  613. */
  614. if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
  615. event = ACPI_NOTIFY_EJECT_REQUEST;
  616. /*
  617. * dock station: BUS_CHECK - docked or surprise removal
  618. * DEVICE_CHECK - undocked
  619. * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
  620. *
  621. * To simplify event handling, dock dependent device handler always
  622. * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  623. * ACPI_NOTIFY_EJECT_REQUEST for removal
  624. */
  625. switch (event) {
  626. case ACPI_NOTIFY_BUS_CHECK:
  627. case ACPI_NOTIFY_DEVICE_CHECK:
  628. if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
  629. &tmp)) {
  630. begin_dock(ds);
  631. dock(ds);
  632. if (!dock_present(ds)) {
  633. printk(KERN_ERR PREFIX "Unable to dock!\n");
  634. complete_dock(ds);
  635. break;
  636. }
  637. atomic_notifier_call_chain(&dock_notifier_list,
  638. event, NULL);
  639. hotplug_dock_devices(ds, event);
  640. complete_dock(ds);
  641. dock_event(ds, event, DOCK_EVENT);
  642. dock_lock(ds, 1);
  643. break;
  644. }
  645. if (dock_present(ds) || dock_in_progress(ds))
  646. break;
  647. /* This is a surprise removal */
  648. surprise_removal = 1;
  649. event = ACPI_NOTIFY_EJECT_REQUEST;
  650. /* Fall back */
  651. case ACPI_NOTIFY_EJECT_REQUEST:
  652. begin_undock(ds);
  653. if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
  654. || surprise_removal)
  655. handle_eject_request(ds, event);
  656. else
  657. dock_event(ds, event, UNDOCK_EVENT);
  658. break;
  659. default:
  660. printk(KERN_ERR PREFIX "Unknown dock event %d\n", event);
  661. }
  662. }
  663. struct dock_data {
  664. acpi_handle handle;
  665. unsigned long event;
  666. struct dock_station *ds;
  667. };
  668. static void acpi_dock_deferred_cb(void *context)
  669. {
  670. struct dock_data *data = (struct dock_data *)context;
  671. dock_notify(data->handle, data->event, data->ds);
  672. kfree(data);
  673. }
  674. static int acpi_dock_notifier_call(struct notifier_block *this,
  675. unsigned long event, void *data)
  676. {
  677. struct dock_station *dock_station;
  678. acpi_handle handle = (acpi_handle)data;
  679. if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
  680. && event != ACPI_NOTIFY_EJECT_REQUEST)
  681. return 0;
  682. list_for_each_entry(dock_station, &dock_stations, sibling) {
  683. if (dock_station->handle == handle) {
  684. struct dock_data *dock_data;
  685. dock_data = kmalloc(sizeof(*dock_data), GFP_KERNEL);
  686. if (!dock_data)
  687. return 0;
  688. dock_data->handle = handle;
  689. dock_data->event = event;
  690. dock_data->ds = dock_station;
  691. acpi_os_hotplug_execute(acpi_dock_deferred_cb,
  692. dock_data);
  693. return 0 ;
  694. }
  695. }
  696. return 0;
  697. }
  698. static struct notifier_block dock_acpi_notifier = {
  699. .notifier_call = acpi_dock_notifier_call,
  700. };
  701. /**
  702. * find_dock_devices - find devices on the dock station
  703. * @handle: the handle of the device we are examining
  704. * @lvl: unused
  705. * @context: the dock station private data
  706. * @rv: unused
  707. *
  708. * This function is called by acpi_walk_namespace. It will
  709. * check to see if an object has an _EJD method. If it does, then it
  710. * will see if it is dependent on the dock station.
  711. */
  712. static acpi_status
  713. find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
  714. {
  715. acpi_status status;
  716. acpi_handle tmp, parent;
  717. struct dock_station *ds = context;
  718. status = acpi_bus_get_ejd(handle, &tmp);
  719. if (ACPI_FAILURE(status)) {
  720. /* try the parent device as well */
  721. status = acpi_get_parent(handle, &parent);
  722. if (ACPI_FAILURE(status))
  723. goto fdd_out;
  724. /* see if parent is dependent on dock */
  725. status = acpi_bus_get_ejd(parent, &tmp);
  726. if (ACPI_FAILURE(status))
  727. goto fdd_out;
  728. }
  729. if (tmp == ds->handle)
  730. add_dock_dependent_device(ds, handle);
  731. fdd_out:
  732. return AE_OK;
  733. }
  734. /*
  735. * show_docked - read method for "docked" file in sysfs
  736. */
  737. static ssize_t show_docked(struct device *dev,
  738. struct device_attribute *attr, char *buf)
  739. {
  740. struct acpi_device *tmp;
  741. struct dock_station *dock_station = *((struct dock_station **)
  742. dev->platform_data);
  743. if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
  744. return snprintf(buf, PAGE_SIZE, "1\n");
  745. return snprintf(buf, PAGE_SIZE, "0\n");
  746. }
  747. static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
  748. /*
  749. * show_flags - read method for flags file in sysfs
  750. */
  751. static ssize_t show_flags(struct device *dev,
  752. struct device_attribute *attr, char *buf)
  753. {
  754. struct dock_station *dock_station = *((struct dock_station **)
  755. dev->platform_data);
  756. return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
  757. }
  758. static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
  759. /*
  760. * write_undock - write method for "undock" file in sysfs
  761. */
  762. static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
  763. const char *buf, size_t count)
  764. {
  765. int ret;
  766. struct dock_station *dock_station = *((struct dock_station **)
  767. dev->platform_data);
  768. if (!count)
  769. return -EINVAL;
  770. begin_undock(dock_station);
  771. ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
  772. return ret ? ret: count;
  773. }
  774. static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
  775. /*
  776. * show_dock_uid - read method for "uid" file in sysfs
  777. */
  778. static ssize_t show_dock_uid(struct device *dev,
  779. struct device_attribute *attr, char *buf)
  780. {
  781. unsigned long long lbuf;
  782. struct dock_station *dock_station = *((struct dock_station **)
  783. dev->platform_data);
  784. acpi_status status = acpi_evaluate_integer(dock_station->handle,
  785. "_UID", NULL, &lbuf);
  786. if (ACPI_FAILURE(status))
  787. return 0;
  788. return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
  789. }
  790. static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
  791. static ssize_t show_dock_type(struct device *dev,
  792. struct device_attribute *attr, char *buf)
  793. {
  794. struct dock_station *dock_station = *((struct dock_station **)
  795. dev->platform_data);
  796. char *type;
  797. if (dock_station->flags & DOCK_IS_DOCK)
  798. type = "dock_station";
  799. else if (dock_station->flags & DOCK_IS_ATA)
  800. type = "ata_bay";
  801. else if (dock_station->flags & DOCK_IS_BAT)
  802. type = "battery_bay";
  803. else
  804. type = "unknown";
  805. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  806. }
  807. static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
  808. static struct attribute *dock_attributes[] = {
  809. &dev_attr_docked.attr,
  810. &dev_attr_flags.attr,
  811. &dev_attr_undock.attr,
  812. &dev_attr_uid.attr,
  813. &dev_attr_type.attr,
  814. NULL
  815. };
  816. static struct attribute_group dock_attribute_group = {
  817. .attrs = dock_attributes
  818. };
  819. /**
  820. * dock_add - add a new dock station
  821. * @handle: the dock station handle
  822. *
  823. * allocated and initialize a new dock station device. Find all devices
  824. * that are on the dock station, and register for dock event notifications.
  825. */
  826. static int dock_add(acpi_handle handle)
  827. {
  828. int ret;
  829. struct dock_station *dock_station;
  830. struct platform_device *dock_device;
  831. /* allocate & initialize the dock_station private data */
  832. dock_station = kzalloc(sizeof(*dock_station), GFP_KERNEL);
  833. if (!dock_station)
  834. return -ENOMEM;
  835. dock_station->handle = handle;
  836. dock_station->last_dock_time = jiffies - HZ;
  837. INIT_LIST_HEAD(&dock_station->dependent_devices);
  838. INIT_LIST_HEAD(&dock_station->hotplug_devices);
  839. INIT_LIST_HEAD(&dock_station->sibling);
  840. spin_lock_init(&dock_station->dd_lock);
  841. mutex_init(&dock_station->hp_lock);
  842. ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
  843. /* initialize platform device stuff */
  844. dock_station->dock_device =
  845. platform_device_register_simple(dock_device_name,
  846. dock_station_count, NULL, 0);
  847. dock_device = dock_station->dock_device;
  848. if (IS_ERR(dock_device)) {
  849. ret = PTR_ERR(dock_device);
  850. goto out;
  851. }
  852. platform_device_add_data(dock_device, &dock_station,
  853. sizeof(struct dock_station *));
  854. /* we want the dock device to send uevents */
  855. dev_set_uevent_suppress(&dock_device->dev, 0);
  856. if (is_dock(handle))
  857. dock_station->flags |= DOCK_IS_DOCK;
  858. if (is_ata(handle))
  859. dock_station->flags |= DOCK_IS_ATA;
  860. if (is_battery(handle))
  861. dock_station->flags |= DOCK_IS_BAT;
  862. ret = sysfs_create_group(&dock_device->dev.kobj, &dock_attribute_group);
  863. if (ret)
  864. goto err_unregister;
  865. /* Find dependent devices */
  866. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  867. ACPI_UINT32_MAX, find_dock_devices, dock_station,
  868. NULL);
  869. /* add the dock station as a device dependent on itself */
  870. ret = add_dock_dependent_device(dock_station, handle);
  871. if (ret)
  872. goto err_rmgroup;
  873. dock_station_count++;
  874. list_add(&dock_station->sibling, &dock_stations);
  875. return 0;
  876. err_rmgroup:
  877. sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
  878. err_unregister:
  879. platform_device_unregister(dock_device);
  880. out:
  881. kfree(dock_station);
  882. dock_station = NULL;
  883. printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
  884. return ret;
  885. }
  886. /**
  887. * dock_remove - free up resources related to the dock station
  888. */
  889. static int dock_remove(struct dock_station *dock_station)
  890. {
  891. struct dock_dependent_device *dd, *tmp;
  892. struct platform_device *dock_device = dock_station->dock_device;
  893. if (!dock_station_count)
  894. return 0;
  895. /* remove dependent devices */
  896. list_for_each_entry_safe(dd, tmp, &dock_station->dependent_devices,
  897. list)
  898. kfree(dd);
  899. /* cleanup sysfs */
  900. sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
  901. platform_device_unregister(dock_device);
  902. /* free dock station memory */
  903. kfree(dock_station);
  904. dock_station = NULL;
  905. return 0;
  906. }
  907. /**
  908. * find_dock - look for a dock station
  909. * @handle: acpi handle of a device
  910. * @lvl: unused
  911. * @context: counter of dock stations found
  912. * @rv: unused
  913. *
  914. * This is called by acpi_walk_namespace to look for dock stations.
  915. */
  916. static acpi_status
  917. find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
  918. {
  919. acpi_status status = AE_OK;
  920. if (is_dock(handle)) {
  921. if (dock_add(handle) >= 0) {
  922. status = AE_CTRL_TERMINATE;
  923. }
  924. }
  925. return status;
  926. }
  927. static acpi_status
  928. find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  929. {
  930. /* If bay is a dock, it's already handled */
  931. if (is_ejectable_bay(handle) && !is_dock(handle))
  932. dock_add(handle);
  933. return AE_OK;
  934. }
  935. static int __init dock_init(void)
  936. {
  937. if (acpi_disabled)
  938. return 0;
  939. /* look for a dock station */
  940. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  941. ACPI_UINT32_MAX, find_dock, NULL, NULL);
  942. /* look for bay */
  943. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  944. ACPI_UINT32_MAX, find_bay, NULL, NULL);
  945. if (!dock_station_count) {
  946. printk(KERN_INFO PREFIX "No dock devices found.\n");
  947. return 0;
  948. }
  949. register_acpi_bus_notifier(&dock_acpi_notifier);
  950. printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
  951. ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
  952. return 0;
  953. }
  954. static void __exit dock_exit(void)
  955. {
  956. struct dock_station *dock_station;
  957. struct dock_station *tmp;
  958. unregister_acpi_bus_notifier(&dock_acpi_notifier);
  959. list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
  960. dock_remove(dock_station);
  961. }
  962. /*
  963. * Must be called before drivers of devices in dock, otherwise we can't know
  964. * which devices are in a dock
  965. */
  966. subsys_initcall(dock_init);
  967. module_exit(dock_exit);