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