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