dock.c 31 KB

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