dock.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. struct acpi_device *parent_device;
  274. acpi_handle parent;
  275. int ret;
  276. if (acpi_bus_get_device(handle, &device)) {
  277. /*
  278. * no device created for this object,
  279. * so we should create one.
  280. */
  281. acpi_get_parent(handle, &parent);
  282. if (acpi_bus_get_device(parent, &parent_device))
  283. parent_device = NULL;
  284. ret = acpi_bus_add(&device, parent_device, handle,
  285. ACPI_BUS_TYPE_DEVICE);
  286. if (ret) {
  287. pr_debug("error adding bus, %x\n", -ret);
  288. return NULL;
  289. }
  290. }
  291. return device;
  292. }
  293. /**
  294. * dock_remove_acpi_device - remove the acpi_device struct from acpi
  295. * @handle - the handle of the device to remove
  296. *
  297. * Tell acpi to remove the acpi_device. This should cause any loaded
  298. * driver to have it's remove routine called.
  299. */
  300. static void dock_remove_acpi_device(acpi_handle handle)
  301. {
  302. struct acpi_device *device;
  303. int ret;
  304. if (!acpi_bus_get_device(handle, &device)) {
  305. ret = acpi_bus_trim(device, 1);
  306. if (ret)
  307. pr_debug("error removing bus, %x\n", -ret);
  308. }
  309. }
  310. /**
  311. * hotplug_dock_devices - insert or remove devices on the dock station
  312. * @ds: the dock station
  313. * @event: either bus check or eject request
  314. *
  315. * Some devices on the dock station need to have drivers called
  316. * to perform hotplug operations after a dock event has occurred.
  317. * Traverse the list of dock devices that have registered a
  318. * hotplug handler, and call the handler.
  319. */
  320. static void hotplug_dock_devices(struct dock_station *ds, u32 event)
  321. {
  322. struct dock_dependent_device *dd;
  323. mutex_lock(&ds->hp_lock);
  324. /*
  325. * First call driver specific hotplug functions
  326. */
  327. list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
  328. if (dd->ops && dd->ops->handler)
  329. dd->ops->handler(dd->handle, event, dd->context);
  330. /*
  331. * Now make sure that an acpi_device is created for each
  332. * dependent device, or removed if this is an eject request.
  333. * This will cause acpi_drivers to be stopped/started if they
  334. * exist
  335. */
  336. list_for_each_entry(dd, &ds->dependent_devices, list) {
  337. if (event == ACPI_NOTIFY_EJECT_REQUEST)
  338. dock_remove_acpi_device(dd->handle);
  339. else
  340. dock_create_acpi_device(dd->handle);
  341. }
  342. mutex_unlock(&ds->hp_lock);
  343. }
  344. static void dock_event(struct dock_station *ds, u32 event, int num)
  345. {
  346. struct device *dev = &ds->dock_device->dev;
  347. char event_string[13];
  348. char *envp[] = { event_string, NULL };
  349. struct dock_dependent_device *dd;
  350. if (num == UNDOCK_EVENT)
  351. sprintf(event_string, "EVENT=undock");
  352. else
  353. sprintf(event_string, "EVENT=dock");
  354. /*
  355. * Indicate that the status of the dock station has
  356. * changed.
  357. */
  358. if (num == DOCK_EVENT)
  359. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  360. list_for_each_entry(dd, &ds->hotplug_devices, hotplug_list)
  361. if (dd->ops && dd->ops->uevent)
  362. dd->ops->uevent(dd->handle, event, dd->context);
  363. if (num != DOCK_EVENT)
  364. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  365. }
  366. /**
  367. * eject_dock - respond to a dock eject request
  368. * @ds: the dock station
  369. *
  370. * This is called after _DCK is called, to execute the dock station's
  371. * _EJ0 method.
  372. */
  373. static void eject_dock(struct dock_station *ds)
  374. {
  375. struct acpi_object_list arg_list;
  376. union acpi_object arg;
  377. acpi_status status;
  378. acpi_handle tmp;
  379. /* all dock devices should have _EJ0, but check anyway */
  380. status = acpi_get_handle(ds->handle, "_EJ0", &tmp);
  381. if (ACPI_FAILURE(status)) {
  382. pr_debug("No _EJ0 support for dock device\n");
  383. return;
  384. }
  385. arg_list.count = 1;
  386. arg_list.pointer = &arg;
  387. arg.type = ACPI_TYPE_INTEGER;
  388. arg.integer.value = 1;
  389. status = acpi_evaluate_object(ds->handle, "_EJ0", &arg_list, NULL);
  390. if (ACPI_FAILURE(status))
  391. pr_debug("Failed to evaluate _EJ0!\n");
  392. }
  393. /**
  394. * handle_dock - handle a dock event
  395. * @ds: the dock station
  396. * @dock: to dock, or undock - that is the question
  397. *
  398. * Execute the _DCK method in response to an acpi event
  399. */
  400. static void handle_dock(struct dock_station *ds, int dock)
  401. {
  402. acpi_status status;
  403. struct acpi_object_list arg_list;
  404. union acpi_object arg;
  405. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  406. acpi_handle_info(ds->handle, "%s\n", dock ? "docking" : "undocking");
  407. /* _DCK method has one argument */
  408. arg_list.count = 1;
  409. arg_list.pointer = &arg;
  410. arg.type = ACPI_TYPE_INTEGER;
  411. arg.integer.value = dock;
  412. status = acpi_evaluate_object(ds->handle, "_DCK", &arg_list, &buffer);
  413. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  414. acpi_handle_err(ds->handle, "Failed to execute _DCK (0x%x)\n",
  415. status);
  416. kfree(buffer.pointer);
  417. }
  418. static inline void dock(struct dock_station *ds)
  419. {
  420. handle_dock(ds, 1);
  421. }
  422. static inline void undock(struct dock_station *ds)
  423. {
  424. handle_dock(ds, 0);
  425. }
  426. static inline void begin_dock(struct dock_station *ds)
  427. {
  428. ds->flags |= DOCK_DOCKING;
  429. }
  430. static inline void complete_dock(struct dock_station *ds)
  431. {
  432. ds->flags &= ~(DOCK_DOCKING);
  433. ds->last_dock_time = jiffies;
  434. }
  435. static inline void begin_undock(struct dock_station *ds)
  436. {
  437. ds->flags |= DOCK_UNDOCKING;
  438. }
  439. static inline void complete_undock(struct dock_station *ds)
  440. {
  441. ds->flags &= ~(DOCK_UNDOCKING);
  442. }
  443. static void dock_lock(struct dock_station *ds, int lock)
  444. {
  445. struct acpi_object_list arg_list;
  446. union acpi_object arg;
  447. acpi_status status;
  448. arg_list.count = 1;
  449. arg_list.pointer = &arg;
  450. arg.type = ACPI_TYPE_INTEGER;
  451. arg.integer.value = !!lock;
  452. status = acpi_evaluate_object(ds->handle, "_LCK", &arg_list, NULL);
  453. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  454. if (lock)
  455. acpi_handle_warn(ds->handle,
  456. "Locking device failed (0x%x)\n", status);
  457. else
  458. acpi_handle_warn(ds->handle,
  459. "Unlocking device failed (0x%x)\n", status);
  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, const 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. * separately, 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. acpi_handle_err(ds->handle, "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. acpi_handle_err(handle, "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. acpi_update_all_gpes();
  640. break;
  641. }
  642. if (dock_present(ds) || dock_in_progress(ds))
  643. break;
  644. /* This is a surprise removal */
  645. surprise_removal = 1;
  646. event = ACPI_NOTIFY_EJECT_REQUEST;
  647. /* Fall back */
  648. case ACPI_NOTIFY_EJECT_REQUEST:
  649. begin_undock(ds);
  650. if ((immediate_undock && !(ds->flags & DOCK_IS_ATA))
  651. || surprise_removal)
  652. handle_eject_request(ds, event);
  653. else
  654. dock_event(ds, event, UNDOCK_EVENT);
  655. break;
  656. default:
  657. acpi_handle_err(handle, "Unknown dock event %d\n", event);
  658. }
  659. }
  660. struct dock_data {
  661. acpi_handle handle;
  662. unsigned long event;
  663. struct dock_station *ds;
  664. };
  665. static void acpi_dock_deferred_cb(void *context)
  666. {
  667. struct dock_data *data = context;
  668. dock_notify(data->handle, data->event, data->ds);
  669. kfree(data);
  670. }
  671. static int acpi_dock_notifier_call(struct notifier_block *this,
  672. unsigned long event, void *data)
  673. {
  674. struct dock_station *dock_station;
  675. acpi_handle handle = data;
  676. if (event != ACPI_NOTIFY_BUS_CHECK && event != ACPI_NOTIFY_DEVICE_CHECK
  677. && event != ACPI_NOTIFY_EJECT_REQUEST)
  678. return 0;
  679. list_for_each_entry(dock_station, &dock_stations, sibling) {
  680. if (dock_station->handle == handle) {
  681. struct dock_data *dd;
  682. dd = kmalloc(sizeof(*dd), GFP_KERNEL);
  683. if (!dd)
  684. return 0;
  685. dd->handle = handle;
  686. dd->event = event;
  687. dd->ds = dock_station;
  688. acpi_os_hotplug_execute(acpi_dock_deferred_cb, dd);
  689. return 0 ;
  690. }
  691. }
  692. return 0;
  693. }
  694. static struct notifier_block dock_acpi_notifier = {
  695. .notifier_call = acpi_dock_notifier_call,
  696. };
  697. /**
  698. * find_dock_devices - find devices on the dock station
  699. * @handle: the handle of the device we are examining
  700. * @lvl: unused
  701. * @context: the dock station private data
  702. * @rv: unused
  703. *
  704. * This function is called by acpi_walk_namespace. It will
  705. * check to see if an object has an _EJD method. If it does, then it
  706. * will see if it is dependent on the dock station.
  707. */
  708. static acpi_status
  709. find_dock_devices(acpi_handle handle, u32 lvl, void *context, void **rv)
  710. {
  711. acpi_status status;
  712. acpi_handle tmp, parent;
  713. struct dock_station *ds = context;
  714. status = acpi_bus_get_ejd(handle, &tmp);
  715. if (ACPI_FAILURE(status)) {
  716. /* try the parent device as well */
  717. status = acpi_get_parent(handle, &parent);
  718. if (ACPI_FAILURE(status))
  719. goto fdd_out;
  720. /* see if parent is dependent on dock */
  721. status = acpi_bus_get_ejd(parent, &tmp);
  722. if (ACPI_FAILURE(status))
  723. goto fdd_out;
  724. }
  725. if (tmp == ds->handle)
  726. add_dock_dependent_device(ds, handle);
  727. fdd_out:
  728. return AE_OK;
  729. }
  730. /*
  731. * show_docked - read method for "docked" file in sysfs
  732. */
  733. static ssize_t show_docked(struct device *dev,
  734. struct device_attribute *attr, char *buf)
  735. {
  736. struct acpi_device *tmp;
  737. struct dock_station *dock_station = dev->platform_data;
  738. if (ACPI_SUCCESS(acpi_bus_get_device(dock_station->handle, &tmp)))
  739. return snprintf(buf, PAGE_SIZE, "1\n");
  740. return snprintf(buf, PAGE_SIZE, "0\n");
  741. }
  742. static DEVICE_ATTR(docked, S_IRUGO, show_docked, NULL);
  743. /*
  744. * show_flags - read method for flags file in sysfs
  745. */
  746. static ssize_t show_flags(struct device *dev,
  747. struct device_attribute *attr, char *buf)
  748. {
  749. struct dock_station *dock_station = dev->platform_data;
  750. return snprintf(buf, PAGE_SIZE, "%d\n", dock_station->flags);
  751. }
  752. static DEVICE_ATTR(flags, S_IRUGO, show_flags, NULL);
  753. /*
  754. * write_undock - write method for "undock" file in sysfs
  755. */
  756. static ssize_t write_undock(struct device *dev, struct device_attribute *attr,
  757. const char *buf, size_t count)
  758. {
  759. int ret;
  760. struct dock_station *dock_station = dev->platform_data;
  761. if (!count)
  762. return -EINVAL;
  763. begin_undock(dock_station);
  764. ret = handle_eject_request(dock_station, ACPI_NOTIFY_EJECT_REQUEST);
  765. return ret ? ret: count;
  766. }
  767. static DEVICE_ATTR(undock, S_IWUSR, NULL, write_undock);
  768. /*
  769. * show_dock_uid - read method for "uid" file in sysfs
  770. */
  771. static ssize_t show_dock_uid(struct device *dev,
  772. struct device_attribute *attr, char *buf)
  773. {
  774. unsigned long long lbuf;
  775. struct dock_station *dock_station = dev->platform_data;
  776. acpi_status status = acpi_evaluate_integer(dock_station->handle,
  777. "_UID", NULL, &lbuf);
  778. if (ACPI_FAILURE(status))
  779. return 0;
  780. return snprintf(buf, PAGE_SIZE, "%llx\n", lbuf);
  781. }
  782. static DEVICE_ATTR(uid, S_IRUGO, show_dock_uid, NULL);
  783. static ssize_t show_dock_type(struct device *dev,
  784. struct device_attribute *attr, char *buf)
  785. {
  786. struct dock_station *dock_station = dev->platform_data;
  787. char *type;
  788. if (dock_station->flags & DOCK_IS_DOCK)
  789. type = "dock_station";
  790. else if (dock_station->flags & DOCK_IS_ATA)
  791. type = "ata_bay";
  792. else if (dock_station->flags & DOCK_IS_BAT)
  793. type = "battery_bay";
  794. else
  795. type = "unknown";
  796. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  797. }
  798. static DEVICE_ATTR(type, S_IRUGO, show_dock_type, NULL);
  799. static struct attribute *dock_attributes[] = {
  800. &dev_attr_docked.attr,
  801. &dev_attr_flags.attr,
  802. &dev_attr_undock.attr,
  803. &dev_attr_uid.attr,
  804. &dev_attr_type.attr,
  805. NULL
  806. };
  807. static struct attribute_group dock_attribute_group = {
  808. .attrs = dock_attributes
  809. };
  810. /**
  811. * dock_add - add a new dock station
  812. * @handle: the dock station handle
  813. *
  814. * allocated and initialize a new dock station device. Find all devices
  815. * that are on the dock station, and register for dock event notifications.
  816. */
  817. static int __init dock_add(acpi_handle handle)
  818. {
  819. int ret, id;
  820. struct dock_station ds, *dock_station;
  821. struct platform_device *dd;
  822. id = dock_station_count;
  823. memset(&ds, 0, sizeof(ds));
  824. dd = platform_device_register_data(NULL, "dock", id, &ds, sizeof(ds));
  825. if (IS_ERR(dd))
  826. return PTR_ERR(dd);
  827. dock_station = dd->dev.platform_data;
  828. dock_station->handle = handle;
  829. dock_station->dock_device = dd;
  830. dock_station->last_dock_time = jiffies - HZ;
  831. mutex_init(&dock_station->hp_lock);
  832. spin_lock_init(&dock_station->dd_lock);
  833. INIT_LIST_HEAD(&dock_station->sibling);
  834. INIT_LIST_HEAD(&dock_station->hotplug_devices);
  835. ATOMIC_INIT_NOTIFIER_HEAD(&dock_notifier_list);
  836. INIT_LIST_HEAD(&dock_station->dependent_devices);
  837. /* we want the dock device to send uevents */
  838. dev_set_uevent_suppress(&dd->dev, 0);
  839. if (is_dock(handle))
  840. dock_station->flags |= DOCK_IS_DOCK;
  841. if (is_ata(handle))
  842. dock_station->flags |= DOCK_IS_ATA;
  843. if (is_battery(handle))
  844. dock_station->flags |= DOCK_IS_BAT;
  845. ret = sysfs_create_group(&dd->dev.kobj, &dock_attribute_group);
  846. if (ret)
  847. goto err_unregister;
  848. /* Find dependent devices */
  849. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  850. ACPI_UINT32_MAX, find_dock_devices, NULL,
  851. dock_station, NULL);
  852. /* add the dock station as a device dependent on itself */
  853. ret = add_dock_dependent_device(dock_station, handle);
  854. if (ret)
  855. goto err_rmgroup;
  856. dock_station_count++;
  857. list_add(&dock_station->sibling, &dock_stations);
  858. return 0;
  859. err_rmgroup:
  860. sysfs_remove_group(&dd->dev.kobj, &dock_attribute_group);
  861. err_unregister:
  862. platform_device_unregister(dd);
  863. acpi_handle_err(handle, "%s encountered error %d\n", __func__, ret);
  864. return ret;
  865. }
  866. /**
  867. * dock_remove - free up resources related to the dock station
  868. */
  869. static int dock_remove(struct dock_station *ds)
  870. {
  871. struct dock_dependent_device *dd, *tmp;
  872. struct platform_device *dock_device = ds->dock_device;
  873. if (!dock_station_count)
  874. return 0;
  875. /* remove dependent devices */
  876. list_for_each_entry_safe(dd, tmp, &ds->dependent_devices, list)
  877. kfree(dd);
  878. list_del(&ds->sibling);
  879. /* cleanup sysfs */
  880. sysfs_remove_group(&dock_device->dev.kobj, &dock_attribute_group);
  881. platform_device_unregister(dock_device);
  882. return 0;
  883. }
  884. /**
  885. * find_dock_and_bay - look for dock stations and bays
  886. * @handle: acpi handle of a device
  887. * @lvl: unused
  888. * @context: unused
  889. * @rv: unused
  890. *
  891. * This is called by acpi_walk_namespace to look for dock stations and bays.
  892. */
  893. static __init acpi_status
  894. find_dock_and_bay(acpi_handle handle, u32 lvl, void *context, void **rv)
  895. {
  896. if (is_dock(handle) || is_ejectable_bay(handle))
  897. dock_add(handle);
  898. return AE_OK;
  899. }
  900. static int __init dock_init(void)
  901. {
  902. if (acpi_disabled)
  903. return 0;
  904. /* look for dock stations and bays */
  905. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  906. ACPI_UINT32_MAX, find_dock_and_bay, NULL, NULL, NULL);
  907. if (!dock_station_count) {
  908. pr_info(PREFIX "No dock devices found.\n");
  909. return 0;
  910. }
  911. register_acpi_bus_notifier(&dock_acpi_notifier);
  912. pr_info(PREFIX "%s: %d docks/bays found\n",
  913. ACPI_DOCK_DRIVER_DESCRIPTION, dock_station_count);
  914. return 0;
  915. }
  916. static void __exit dock_exit(void)
  917. {
  918. struct dock_station *tmp, *dock_station;
  919. unregister_acpi_bus_notifier(&dock_acpi_notifier);
  920. list_for_each_entry_safe(dock_station, tmp, &dock_stations, sibling)
  921. dock_remove(dock_station);
  922. }
  923. /*
  924. * Must be called before drivers of devices in dock, otherwise we can't know
  925. * which devices are in a dock
  926. */
  927. subsys_initcall(dock_init);
  928. module_exit(dock_exit);