dock.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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_scan(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. if (!acpi_bus_get_device(handle, &device))
  297. acpi_bus_trim(device);
  298. }
  299. /**
  300. * hotplug_dock_devices - insert or remove devices on the dock station
  301. * @ds: the dock station
  302. * @event: either bus check or eject request
  303. *
  304. * Some devices on the dock station need to have drivers called
  305. * to perform hotplug operations after a dock event has occurred.
  306. * Traverse the list of dock devices that have registered a
  307. * hotplug handler, and call the handler.
  308. */
  309. static void hotplug_dock_devices(struct dock_station *ds, u32 event)
  310. {
  311. struct dock_dependent_device *dd;
  312. mutex_lock(&ds->hp_lock);
  313. /*
  314. * First call driver specific hotplug functions
  315. */
  316. list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
  317. if (dd->ops && dd->ops->handler)
  318. dd->ops->handler(dd->handle, event, dd->context);
  319. /*
  320. * Now make sure that an acpi_device is created for each
  321. * dependent device, or removed if this is an eject request.
  322. * This will cause acpi_drivers to be stopped/started if they
  323. * exist
  324. */
  325. list_for_each_entry(dd, &ds->dependent_devices, list) {
  326. if (event == ACPI_NOTIFY_EJECT_REQUEST)
  327. dock_remove_acpi_device(dd->handle);
  328. else
  329. dock_create_acpi_device(dd->handle);
  330. }
  331. mutex_unlock(&ds->hp_lock);
  332. }
  333. static void dock_event(struct dock_station *ds, u32 event, int num)
  334. {
  335. struct device *dev = &ds->dock_device->dev;
  336. char event_string[13];
  337. char *envp[] = { event_string, NULL };
  338. struct dock_dependent_device *dd;
  339. if (num == UNDOCK_EVENT)
  340. sprintf(event_string, "EVENT=undock");
  341. else
  342. sprintf(event_string, "EVENT=dock");
  343. /*
  344. * Indicate that the status of the dock station has
  345. * changed.
  346. */
  347. if (num == DOCK_EVENT)
  348. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  349. list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
  350. if (dd->ops && dd->ops->uevent)
  351. dd->ops->uevent(dd->handle, event, dd->context);
  352. if (num != DOCK_EVENT)
  353. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  354. }
  355. /**
  356. * eject_dock - respond to a dock eject request
  357. * @ds: the dock station
  358. *
  359. * This is called after _DCK is called, to execute the dock station's
  360. * _EJ0 method.
  361. */
  362. static void eject_dock(struct dock_station *ds)
  363. {
  364. struct acpi_object_list arg_list;
  365. union acpi_object arg;
  366. acpi_status status;
  367. acpi_handle tmp;
  368. /* all dock devices should have _EJ0, but check anyway */
  369. status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
  370. if (ACPI_FAILURE(status)) {
  371. pr_debug("No _EJ0 support for dock device\n");
  372. return;
  373. }
  374. arg_list.count = 1;
  375. arg_list.pointer = &arg;
  376. arg.type = ACPI_TYPE_INTEGER;
  377. arg.integer.value = 1;
  378. status = acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL);
  379. if (ACPI_FAILURE(status))
  380. pr_debug("Failed to evaluate _EJ0!\n");
  381. }
  382. /**
  383. * handle_dock - handle a dock event
  384. * @ds: the dock station
  385. * @dock: to dock, or undock - that is the question
  386. *
  387. * Execute the _DCK method in response to an acpi event
  388. */
  389. static void handle_dock(struct dock_station *ds, int dock)
  390. {
  391. acpi_status status;
  392. struct acpi_object_list arg_list;
  393. union acpi_object arg;
  394. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  395. acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
  396. /* _DCK method has one argument */
  397. arg_list.count = 1;
  398. arg_list.pointer = &arg;
  399. arg.type = ACPI_TYPE_INTEGER;
  400. arg.integer.value = dock;
  401. status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
  402. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  403. acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
  404. status);
  405. kfree(buffer.pointer);
  406. }
  407. static inline void dock(struct dock_station *ds)
  408. {
  409. handle_dock(ds, 1);
  410. }
  411. static inline void undock(struct dock_station *ds)
  412. {
  413. handle_dock(ds, 0);
  414. }
  415. static inline void begin_dock(struct dock_station *ds)
  416. {
  417. ds->flags |= DOCK_DOCKING;
  418. }
  419. static inline void complete_dock(struct dock_station *ds)
  420. {
  421. ds->flags &= ~(DOCK_DOCKING);
  422. ds->last_dock_time = jiffies;
  423. }
  424. static inline void begin_undock(struct dock_station *ds)
  425. {
  426. ds->flags |= DOCK_UNDOCKING;
  427. }
  428. static inline void complete_undock(struct dock_station *ds)
  429. {
  430. ds->flags &= ~(DOCK_UNDOCKING);
  431. }
  432. static void dock_lock(struct dock_station *ds, int lock)
  433. {
  434. struct acpi_object_list arg_list;
  435. union acpi_object arg;
  436. acpi_status status;
  437. arg_list.count = 1;
  438. arg_list.pointer = &arg;
  439. arg.type = ACPI_TYPE_INTEGER;
  440. arg.integer.value = !!lock;
  441. status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
  442. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  443. if (lock)
  444. acpi_handle_warn(ds->handle,
  445. "Locking device failed (0x%x)\n", status);
  446. else
  447. acpi_handle_warn(ds->handle,
  448. "Unlocking device failed (0x%x)\n", status);
  449. }
  450. }
  451. /**
  452. * dock_in_progress - see if we are in the middle of handling a dock event
  453. * @ds: the dock station
  454. *
  455. * Sometimes while docking, false dock events can be sent to the driver
  456. * because good connections aren't made or some other reason. Ignore these
  457. * if we are in the middle of doing something.
  458. */
  459. static int dock_in_progress(struct dock_station *ds)
  460. {
  461. if ((ds->flags & DOCK_DOCKING) ||
  462. time_before(jiffies, (ds->last_dock_time + HZ)))
  463. return 1;
  464. return 0;
  465. }
  466. /**
  467. * register_dock_notifier - add yourself to the dock notifier list
  468. * @nb: the callers notifier block
  469. *
  470. * If a driver wishes to be notified about dock events, they can
  471. * use this function to put a notifier block on the dock notifier list.
  472. * this notifier call chain will be called after a dock event, but
  473. * before hotplugging any new devices.
  474. */
  475. int register_dock_notifier(struct notifier_block *nb)
  476. {
  477. if (!dock_station_count)
  478. return -ENODEV;
  479. return atomic_notifier_chain_register(&dock_notifier_list, nb);
  480. }
  481. EXPORT_SYMBOL_GPL(register_dock_notifier);
  482. /**
  483. * unregister_dock_notifier - remove yourself from the dock notifier list
  484. * @nb: the callers notifier block
  485. */
  486. void unregister_dock_notifier(struct notifier_block *nb)
  487. {
  488. if (!dock_station_count)
  489. return;
  490. atomic_notifier_chain_unregister(&dock_notifier_list, nb);
  491. }
  492. EXPORT_SYMBOL_GPL(unregister_dock_notifier);
  493. /**
  494. * register_hotplug_dock_device - register a hotplug function
  495. * @handle: the handle of the device
  496. * @ops: handlers to call after docking
  497. * @context: device specific data
  498. *
  499. * If a driver would like to perform a hotplug operation after a dock
  500. * event, they can register an acpi_notifiy_handler to be called by
  501. * the dock driver after _DCK is executed.
  502. */
  503. int
  504. register_hotplug_dock_device(acpi_handle handle, const struct acpi_dock_ops *ops,
  505. void *context)
  506. {
  507. struct dock_dependent_device *dd;
  508. struct dock_station *dock_station;
  509. int ret = -EINVAL;
  510. if (!dock_station_count)
  511. return -ENODEV;
  512. /*
  513. * make sure this handle is for a device dependent on the dock,
  514. * this would include the dock station itself
  515. */
  516. list_for_each_entry(dock_station, &dock_stations, sibling) {
  517. /*
  518. * An ATA bay can be in a dock and itself can be ejected
  519. * separately, so there are two 'dock stations' which need the
  520. * ops
  521. */
  522. dd = find_dock_dependent_device(dock_station, handle);
  523. if (dd) {
  524. dd->ops = ops;
  525. dd->context = context;
  526. dock_add_hotplug_device(dock_station, dd);
  527. ret = 0;
  528. }
  529. }
  530. return ret;
  531. }
  532. EXPORT_SYMBOL_GPL(register_hotplug_dock_device);
  533. /**
  534. * unregister_hotplug_dock_device - remove yourself from the hotplug list
  535. * @handle: the acpi handle of the device
  536. */
  537. void unregister_hotplug_dock_device(acpi_handle handle)
  538. {
  539. struct dock_dependent_device *dd;
  540. struct dock_station *dock_station;
  541. if (!dock_station_count)
  542. return;
  543. list_for_each_entry(dock_station, &dock_stations, sibling) {
  544. dd = find_dock_dependent_device(dock_station, handle);
  545. if (dd)
  546. dock_del_hotplug_device(dock_station, dd);
  547. }
  548. }
  549. EXPORT_SYMBOL_GPL(unregister_hotplug_dock_device);
  550. /**
  551. * handle_eject_request - handle an undock request checking for error conditions
  552. *
  553. * Check to make sure the dock device is still present, then undock and
  554. * hotremove all the devices that may need removing.
  555. */
  556. static int handle_eject_request(struct dock_station *ds, u32 event)
  557. {
  558. if (dock_in_progress(ds))
  559. return -EBUSY;
  560. /*
  561. * here we need to generate the undock
  562. * event prior to actually doing the undock
  563. * so that the device struct still exists.
  564. * Also, even send the dock event if the
  565. * device is not present anymore
  566. */
  567. dock_event(ds, event, UNDOCK_EVENT);
  568. hotplug_dock_devices(ds, ACPI_NOTIFY_EJECT_REQUEST);
  569. undock(ds);
  570. dock_lock(ds, 0);
  571. eject_dock(ds);
  572. if (dock_present(ds)) {
  573. acpi_handle_err(ds->handle, "Unable to undock!\n");
  574. return -EBUSY;
  575. }
  576. complete_undock(ds);
  577. return 0;
  578. }
  579. /**
  580. * dock_notify - act upon an acpi dock notification
  581. * @handle: the dock station handle
  582. * @event: the acpi event
  583. * @data: our driver data struct
  584. *
  585. * If we are notified to dock, then check to see if the dock is
  586. * present and then dock. Notify all drivers of the dock event,
  587. * and then hotplug and devices that may need hotplugging.
  588. */
  589. static void dock_notify(acpi_handle handle, u32 event, void *data)
  590. {
  591. struct dock_station *ds = data;
  592. struct acpi_device *tmp;
  593. int surprise_removal = 0;
  594. /*
  595. * According to acpi spec 3.0a, if a DEVICE_CHECK notification
  596. * is sent and _DCK is present, it is assumed to mean an undock
  597. * request.
  598. */
  599. if ((ds->flags & DOCK_IS_DOCK) && event == ACPI_NOTIFY_DEVICE_CHECK)
  600. event = ACPI_NOTIFY_EJECT_REQUEST;
  601. /*
  602. * dock station: BUS_CHECK - docked or surprise removal
  603. * DEVICE_CHECK - undocked
  604. * other device: BUS_CHECK/DEVICE_CHECK - added or surprise removal
  605. *
  606. * To simplify event handling, dock dependent device handler always
  607. * get ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  608. * ACPI_NOTIFY_EJECT_REQUEST for removal
  609. */
  610. switch (event) {
  611. case ACPI_NOTIFY_BUS_CHECK:
  612. case ACPI_NOTIFY_DEVICE_CHECK:
  613. if (!dock_in_progress(ds) && acpi_bus_get_device(ds->handle,
  614. &tmp)) {
  615. begin_dock(ds);
  616. dock(ds);
  617. if (!dock_present(ds)) {
  618. acpi_handle_err(handle, "Unable to dock!\n");
  619. complete_dock(ds);
  620. break;
  621. }
  622. atomic_notifier_call_chain(&dock_notifier_list,
  623. event, NULL);
  624. hotplug_dock_devices(ds, event);
  625. complete_dock(ds);
  626. dock_event(ds, event, DOCK_EVENT);
  627. dock_lock(ds, 1);
  628. acpi_update_all_gpes();
  629. break;
  630. }
  631. if (dock_present(ds) || dock_in_progress(ds))
  632. break;
  633. /* This is a surprise removal */
  634. surprise_removal = 1;
  635. event = ACPI_NOTIFY_EJECT_REQUEST;
  636. /* Fall back */
  637. case ACPI_NOTIFY_EJECT_REQUEST:
  638. begin_undock(ds);
  639. if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
  640. || surprise_removal)
  641. handle_eject_request(ds, event);
  642. else
  643. dock_event(ds, event, UNDOCK_EVENT);
  644. break;
  645. default:
  646. acpi_handle_err(handle, "Unknown dock event %d\n", event);
  647. }
  648. }
  649. struct dock_data {
  650. acpi_handle handle;
  651. unsigned long event;
  652. struct dock_station *ds;
  653. };
  654. static void acpi_dock_deferred_cb(void *context)
  655. {
  656. struct dock_data *data = context;
  657. acpi_scan_lock_acquire();
  658. dock_notify(data->handle, data->event, data->ds);
  659. acpi_scan_lock_release();
  660. kfree(data);
  661. }
  662. static int acpi_dock_notifier_call(struct notifier_block *this,
  663. unsigned long event, void *data)
  664. {
  665. struct dock_station *dock_station;
  666. acpi_handle handle = data;
  667. if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
  668. && event != ACPI_NOTIFY_EJECT_REQUEST)
  669. return 0;
  670. acpi_scan_lock_acquire();
  671. list_for_each_entry(dock_station, &dock_stations, sibling) {
  672. if (dock_station->handle == handle) {
  673. struct dock_data *dd;
  674. acpi_status status;
  675. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  676. if (!dd)
  677. break;
  678. dd->handle = handle;
  679. dd->event = event;
  680. dd->ds = dock_station;
  681. status = acpi_os_hotplug_execute(acpi_dock_deferred_cb,
  682. dd);
  683. if (ACPI_FAILURE(status))
  684. kfree(dd);
  685. break;
  686. }
  687. }
  688. acpi_scan_lock_release();
  689. return 0;
  690. }
  691. static struct notifier_block dock_acpi_notifier = {
  692. .notifier_call = acpi_dock_notifier_call,
  693. };
  694. /**
  695. * find_dock_devices - find devices on the dock station
  696. * @handle: the handle of the device we are examining
  697. * @lvl: unused
  698. * @context: the dock station private data
  699. * @rv: unused
  700. *
  701. * This function is called by acpi_walk_namespace. It will
  702. * check to see if an object has an _EJD method. If it does, then it
  703. * will see if it is dependent on the dock station.
  704. */
  705. static acpi_status
  706. find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
  707. {
  708. acpi_status status;
  709. acpi_handle tmp, parent;
  710. struct dock_station *ds = context;
  711. status = acpi_bus_get_ejd(handle, &tmp);
  712. if (ACPI_FAILURE(status)) {
  713. /* try the parent device as well */
  714. status = acpi_get_parent(handle, &parent);
  715. if (ACPI_FAILURE(status))
  716. goto fdd_out;
  717. /* see if parent is dependent on dock */
  718. status = acpi_bus_get_ejd(parent, &tmp);
  719. if (ACPI_FAILURE(status))
  720. goto fdd_out;
  721. }
  722. if (tmp == ds->handle)
  723. add_dock_dependent_device(ds, handle);
  724. fdd_out:
  725. return AE_OK;
  726. }
  727. /*
  728. * show_docked - read method for "docked" file in sysfs
  729. */
  730. static ssize_t show_docked(struct device *dev,
  731. struct device_attribute *attr, char *buf)
  732. {
  733. struct acpi_device *tmp;
  734. struct dock_station *dock_station = dev->platform_data;
  735. if (!acpi_bus_get_device(dock_station->handle, &tmp))
  736. return snprintf(buf, PAGE_SIZE, "1\n");
  737. return snprintf(buf, PAGE_SIZE, "0\n");
  738. }
  739. static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
  740. /*
  741. * show_flags - read method for flags file in sysfs
  742. */
  743. static ssize_t show_flags(struct device *dev,
  744. struct device_attribute *attr, char *buf)
  745. {
  746. struct dock_station *dock_station = dev->platform_data;
  747. return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
  748. }
  749. static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
  750. /*
  751. * write_undock - write method for "undock" file in sysfs
  752. */
  753. static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
  754. const char *buf, size_t count)
  755. {
  756. int ret;
  757. struct dock_station *dock_station = dev->platform_data;
  758. if (!count)
  759. return -EINVAL;
  760. begin_undock(dock_station);
  761. ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
  762. return ret ? ret: count;
  763. }
  764. static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
  765. /*
  766. * show_dock_uid - read method for "uid" file in sysfs
  767. */
  768. static ssize_t show_dock_uid(struct device *dev,
  769. struct device_attribute *attr, char *buf)
  770. {
  771. unsigned long long lbuf;
  772. struct dock_station *dock_station = dev->platform_data;
  773. acpi_status status = acpi_evaluate_integer(dock_station->handle,
  774. "_UID", NULL, &lbuf);
  775. if (ACPI_FAILURE(status))
  776. return 0;
  777. return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
  778. }
  779. static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
  780. static ssize_t show_dock_type(struct device *dev,
  781. struct device_attribute *attr, char *buf)
  782. {
  783. struct dock_station *dock_station = dev->platform_data;
  784. char *type;
  785. if (dock_station->flags & DOCK_IS_DOCK)
  786. type = "dock_station";
  787. else if (dock_station->flags & DOCK_IS_ATA)
  788. type = "ata_bay";
  789. else if (dock_station->flags & DOCK_IS_BAT)
  790. type = "battery_bay";
  791. else
  792. type = "unknown";
  793. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  794. }
  795. static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
  796. static struct attribute *dock_attributes[] = {
  797. &dev_attr_docked.attr,
  798. &dev_attr_flags.attr,
  799. &dev_attr_undock.attr,
  800. &dev_attr_uid.attr,
  801. &dev_attr_type.attr,
  802. NULL
  803. };
  804. static struct attribute_group dock_attribute_group = {
  805. .attrs = dock_attributes
  806. };
  807. /**
  808. * dock_add - add a new dock station
  809. * @handle: the dock station handle
  810. *
  811. * allocated and initialize a new dock station device. Find all devices
  812. * that are on the dock station, and register for dock event notifications.
  813. */
  814. static int __init dock_add(acpi_handle handle)
  815. {
  816. int ret, id;
  817. struct dock_station ds, *dock_station;
  818. struct platform_device *dd;
  819. id = dock_station_count;
  820. memset(&ds, 0, sizeof(ds));
  821. dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
  822. if (IS_ERR(dd))
  823. return PTR_ERR(dd);
  824. dock_station = dd->dev.platform_data;
  825. dock_station->handle = handle;
  826. dock_station->dock_device = dd;
  827. dock_station->last_dock_time = jiffies - HZ;
  828. mutex_init(&dock_station->hp_lock);
  829. spin_lock_init(&dock_station->dd_lock);
  830. INIT_LIST_HEAD(&dock_station->sibling);
  831. INIT_LIST_HEAD(&dock_station->hotplug_devices);
  832. ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
  833. INIT_LIST_HEAD(&dock_station->dependent_devices);
  834. /* we want the dock device to send uevents */
  835. dev_set_uevent_suppress(&dd->dev, 0);
  836. if (is_dock(handle))
  837. dock_station->flags |= DOCK_IS_DOCK;
  838. if (is_ata(handle))
  839. dock_station->flags |= DOCK_IS_ATA;
  840. if (is_battery(handle))
  841. dock_station->flags |= DOCK_IS_BAT;
  842. ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
  843. if (ret)
  844. goto err_unregister;
  845. /* Find dependent devices */
  846. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  847. ACPI_UINT32_MAX, find_dock_devices, NULL,
  848. 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. acpi_handle_err(handle, "%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_and_bay - look for dock stations and bays
  883. * @handle: acpi handle of a device
  884. * @lvl: unused
  885. * @context: unused
  886. * @rv: unused
  887. *
  888. * This is called by acpi_walk_namespace to look for dock stations and bays.
  889. */
  890. static __init acpi_status
  891. find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  892. {
  893. if (is_dock(handle) || is_ejectable_bay(handle))
  894. dock_add(handle);
  895. return AE_OK;
  896. }
  897. static int __init dock_init(void)
  898. {
  899. if (acpi_disabled)
  900. return 0;
  901. /* look for dock stations and bays */
  902. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  903. ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
  904. if (!dock_station_count) {
  905. pr_info(PREFIX "No dock devices found.\n");
  906. return 0;
  907. }
  908. register_acpi_bus_notifier(&dock_acpi_notifier);
  909. pr_info(PREFIX "%s: %d docks/bays found\n",
  910. ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
  911. return 0;
  912. }
  913. static void __exit dock_exit(void)
  914. {
  915. struct dock_station *tmp, *dock_station;
  916. unregister_acpi_bus_notifier(&dock_acpi_notifier);
  917. list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
  918. dock_remove(dock_station);
  919. }
  920. /*
  921. * Must be called before drivers of devices in dock, otherwise we can't know
  922. * which devices are in a dock
  923. */
  924. subsys_initcall(dock_init);
  925. module_exit(dock_exit);