dock.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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,
  848. ACPI_UINT32_MAX, find_dock_devices, NULL,
  849. dock_station, NULL);
  850. /* add the dock station as a device dependent on itself */
  851. ret = add_dock_dependent_device(dock_station, handle);
  852. if (ret)
  853. goto err_rmgroup;
  854. dock_station_count++;
  855. list_add(&dock_station->sibling, &dock_stations);
  856. return 0;
  857. err_rmgroup:
  858. sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
  859. err_unregister:
  860. platform_device_unregister(dd);
  861. printk(KERN_ERR "%s encountered error %d\n", __func__, ret);
  862. return ret;
  863. }
  864. /**
  865. * dock_remove - free up resources related to the dock station
  866. */
  867. static int dock_remove(struct dock_station *ds)
  868. {
  869. struct dock_dependent_device *dd, *tmp;
  870. struct platform_device *dock_device = ds->dock_device;
  871. if (!dock_station_count)
  872. return 0;
  873. /* remove dependent devices */
  874. list_for_each_entry_safe(dd, tmp, &ds->dependent_devices, list)
  875. kfree(dd);
  876. list_del(&ds->sibling);
  877. /* cleanup sysfs */
  878. sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
  879. platform_device_unregister(dock_device);
  880. return 0;
  881. }
  882. /**
  883. * find_dock - look for a dock station
  884. * @handle: acpi handle of a device
  885. * @lvl: unused
  886. * @context: counter of dock stations found
  887. * @rv: unused
  888. *
  889. * This is called by acpi_walk_namespace to look for dock stations.
  890. */
  891. static acpi_status
  892. find_dock(acpi_handle handle, u32 lvl, void *context, void **rv)
  893. {
  894. acpi_status status = AE_OK;
  895. if (is_dock(handle))
  896. if (dock_add(handle) >= 0)
  897. status = AE_CTRL_TERMINATE;
  898. return status;
  899. }
  900. static acpi_status
  901. find_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  902. {
  903. /* If bay is a dock, it's already handled */
  904. if (is_ejectable_bay(handle) && !is_dock(handle))
  905. dock_add(handle);
  906. return AE_OK;
  907. }
  908. static int __init dock_init(void)
  909. {
  910. if (acpi_disabled)
  911. return 0;
  912. /* look for a dock station */
  913. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  914. ACPI_UINT32_MAX, find_dock, NULL, NULL, NULL);
  915. /* look for bay */
  916. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  917. ACPI_UINT32_MAX, find_bay, NULL, NULL, NULL);
  918. if (!dock_station_count) {
  919. printk(KERN_INFO PREFIX "No dock devices found.\n");
  920. return 0;
  921. }
  922. register_acpi_bus_notifier(&dock_acpi_notifier);
  923. printk(KERN_INFO PREFIX "%s: %d docks/bays found\n",
  924. ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
  925. return 0;
  926. }
  927. static void __exit dock_exit(void)
  928. {
  929. struct dock_station *tmp, *dock_station;
  930. unregister_acpi_bus_notifier(&dock_acpi_notifier);
  931. list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
  932. dock_remove(dock_station);
  933. }
  934. /*
  935. * Must be called before drivers of devices in dock, otherwise we can't know
  936. * which devices are in a dock
  937. */
  938. subsys_initcall(dock_init);
  939. module_exit(dock_exit);