dock.c 31 KB

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