dock.c 28 KB

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