dock.c 27 KB

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