dock.c 28 KB

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