dock.c 31 KB

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