hid-wiimote-modules.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * Device Modules for Nintendo Wii / Wii U HID Driver
  3. * Copyright (c) 2011-2013 David Herrmann <dh.herrmann@gmail.com>
  4. */
  5. /*
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. */
  11. /*
  12. * Wiimote Modules
  13. * Nintendo devices provide different peripherals and many new devices lack
  14. * initial features like the IR camera. Therefore, each peripheral device is
  15. * implemented as an independent module and we probe on each device only the
  16. * modules for the hardware that really is available.
  17. *
  18. * Module registration is sequential. Unregistration is done in reverse order.
  19. * After device detection, the needed modules are loaded. Users can trigger
  20. * re-detection which causes all modules to be unloaded and then reload the
  21. * modules for the new detected device.
  22. *
  23. * wdata->input is a shared input device. It is always initialized prior to
  24. * module registration. If at least one registered module is marked as
  25. * WIIMOD_FLAG_INPUT, then the input device will get registered after all
  26. * modules were registered.
  27. * Please note that it is unregistered _before_ the "remove" callbacks are
  28. * called. This guarantees that no input interaction is done, anymore. However,
  29. * the wiimote core keeps a reference to the input device so it is freed only
  30. * after all modules were removed. It is safe to send events to unregistered
  31. * input devices.
  32. */
  33. #include <linux/device.h>
  34. #include <linux/hid.h>
  35. #include <linux/input.h>
  36. #include <linux/spinlock.h>
  37. #include "hid-wiimote.h"
  38. /*
  39. * Keys
  40. * The initial Wii Remote provided a bunch of buttons that are reported as
  41. * part of the core protocol. Many later devices dropped these and report
  42. * invalid data in the core button reports. Load this only on devices which
  43. * correctly send button reports.
  44. * It uses the shared input device.
  45. */
  46. static const __u16 wiimod_keys_map[] = {
  47. KEY_LEFT, /* WIIPROTO_KEY_LEFT */
  48. KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
  49. KEY_UP, /* WIIPROTO_KEY_UP */
  50. KEY_DOWN, /* WIIPROTO_KEY_DOWN */
  51. KEY_NEXT, /* WIIPROTO_KEY_PLUS */
  52. KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
  53. BTN_1, /* WIIPROTO_KEY_ONE */
  54. BTN_2, /* WIIPROTO_KEY_TWO */
  55. BTN_A, /* WIIPROTO_KEY_A */
  56. BTN_B, /* WIIPROTO_KEY_B */
  57. BTN_MODE, /* WIIPROTO_KEY_HOME */
  58. };
  59. static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
  60. {
  61. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT],
  62. !!(keys[0] & 0x01));
  63. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT],
  64. !!(keys[0] & 0x02));
  65. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN],
  66. !!(keys[0] & 0x04));
  67. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP],
  68. !!(keys[0] & 0x08));
  69. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS],
  70. !!(keys[0] & 0x10));
  71. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO],
  72. !!(keys[1] & 0x01));
  73. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE],
  74. !!(keys[1] & 0x02));
  75. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B],
  76. !!(keys[1] & 0x04));
  77. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A],
  78. !!(keys[1] & 0x08));
  79. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS],
  80. !!(keys[1] & 0x10));
  81. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME],
  82. !!(keys[1] & 0x80));
  83. input_sync(wdata->input);
  84. }
  85. static int wiimod_keys_probe(const struct wiimod_ops *ops,
  86. struct wiimote_data *wdata)
  87. {
  88. unsigned int i;
  89. set_bit(EV_KEY, wdata->input->evbit);
  90. for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
  91. set_bit(wiimod_keys_map[i], wdata->input->keybit);
  92. return 0;
  93. }
  94. static const struct wiimod_ops wiimod_keys = {
  95. .flags = WIIMOD_FLAG_INPUT,
  96. .arg = 0,
  97. .probe = wiimod_keys_probe,
  98. .remove = NULL,
  99. .in_keys = wiimod_keys_in_keys,
  100. };
  101. /*
  102. * Rumble
  103. * Nearly all devices provide a rumble feature. A small motor for
  104. * force-feedback effects. We provide an FF_RUMBLE memless ff device on the
  105. * shared input device if this module is loaded.
  106. * The rumble motor is controlled via a flag on almost every output report so
  107. * the wiimote core handles the rumble flag. But if a device doesn't provide
  108. * the rumble motor, this flag shouldn't be set.
  109. */
  110. static int wiimod_rumble_play(struct input_dev *dev, void *data,
  111. struct ff_effect *eff)
  112. {
  113. struct wiimote_data *wdata = input_get_drvdata(dev);
  114. __u8 value;
  115. unsigned long flags;
  116. /*
  117. * The wiimote supports only a single rumble motor so if any magnitude
  118. * is set to non-zero then we start the rumble motor. If both are set to
  119. * zero, we stop the rumble motor.
  120. */
  121. if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
  122. value = 1;
  123. else
  124. value = 0;
  125. spin_lock_irqsave(&wdata->state.lock, flags);
  126. wiiproto_req_rumble(wdata, value);
  127. spin_unlock_irqrestore(&wdata->state.lock, flags);
  128. return 0;
  129. }
  130. static int wiimod_rumble_probe(const struct wiimod_ops *ops,
  131. struct wiimote_data *wdata)
  132. {
  133. set_bit(FF_RUMBLE, wdata->input->ffbit);
  134. if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
  135. return -ENOMEM;
  136. return 0;
  137. }
  138. static void wiimod_rumble_remove(const struct wiimod_ops *ops,
  139. struct wiimote_data *wdata)
  140. {
  141. unsigned long flags;
  142. spin_lock_irqsave(&wdata->state.lock, flags);
  143. wiiproto_req_rumble(wdata, 0);
  144. spin_unlock_irqrestore(&wdata->state.lock, flags);
  145. }
  146. static const struct wiimod_ops wiimod_rumble = {
  147. .flags = WIIMOD_FLAG_INPUT,
  148. .arg = 0,
  149. .probe = wiimod_rumble_probe,
  150. .remove = wiimod_rumble_remove,
  151. };
  152. /*
  153. * Battery
  154. * 1 byte of battery capacity information is sent along every protocol status
  155. * report. The wiimote core caches it but we try to update it on every
  156. * user-space request.
  157. * This is supported by nearly every device so it's almost always enabled.
  158. */
  159. static enum power_supply_property wiimod_battery_props[] = {
  160. POWER_SUPPLY_PROP_CAPACITY,
  161. POWER_SUPPLY_PROP_SCOPE,
  162. };
  163. static int wiimod_battery_get_property(struct power_supply *psy,
  164. enum power_supply_property psp,
  165. union power_supply_propval *val)
  166. {
  167. struct wiimote_data *wdata = container_of(psy, struct wiimote_data,
  168. battery);
  169. int ret = 0, state;
  170. unsigned long flags;
  171. if (psp == POWER_SUPPLY_PROP_SCOPE) {
  172. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  173. return 0;
  174. } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
  175. return -EINVAL;
  176. }
  177. ret = wiimote_cmd_acquire(wdata);
  178. if (ret)
  179. return ret;
  180. spin_lock_irqsave(&wdata->state.lock, flags);
  181. wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
  182. wiiproto_req_status(wdata);
  183. spin_unlock_irqrestore(&wdata->state.lock, flags);
  184. wiimote_cmd_wait(wdata);
  185. wiimote_cmd_release(wdata);
  186. spin_lock_irqsave(&wdata->state.lock, flags);
  187. state = wdata->state.cmd_battery;
  188. spin_unlock_irqrestore(&wdata->state.lock, flags);
  189. val->intval = state * 100 / 255;
  190. return ret;
  191. }
  192. static int wiimod_battery_probe(const struct wiimod_ops *ops,
  193. struct wiimote_data *wdata)
  194. {
  195. int ret;
  196. wdata->battery.properties = wiimod_battery_props;
  197. wdata->battery.num_properties = ARRAY_SIZE(wiimod_battery_props);
  198. wdata->battery.get_property = wiimod_battery_get_property;
  199. wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  200. wdata->battery.use_for_apm = 0;
  201. wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
  202. wdata->hdev->uniq);
  203. if (!wdata->battery.name)
  204. return -ENOMEM;
  205. ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
  206. if (ret) {
  207. hid_err(wdata->hdev, "cannot register battery device\n");
  208. goto err_free;
  209. }
  210. power_supply_powers(&wdata->battery, &wdata->hdev->dev);
  211. return 0;
  212. err_free:
  213. kfree(wdata->battery.name);
  214. wdata->battery.name = NULL;
  215. return ret;
  216. }
  217. static void wiimod_battery_remove(const struct wiimod_ops *ops,
  218. struct wiimote_data *wdata)
  219. {
  220. if (!wdata->battery.name)
  221. return;
  222. power_supply_unregister(&wdata->battery);
  223. kfree(wdata->battery.name);
  224. wdata->battery.name = NULL;
  225. }
  226. static const struct wiimod_ops wiimod_battery = {
  227. .flags = 0,
  228. .arg = 0,
  229. .probe = wiimod_battery_probe,
  230. .remove = wiimod_battery_remove,
  231. };
  232. /*
  233. * LED
  234. * 0 to 4 player LEDs are supported by devices. The "arg" field of the
  235. * wiimod_ops structure specifies which LED this module controls. This allows
  236. * to register a limited number of LEDs.
  237. * State is managed by wiimote core.
  238. */
  239. static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
  240. {
  241. struct wiimote_data *wdata;
  242. struct device *dev = led_dev->dev->parent;
  243. int i;
  244. unsigned long flags;
  245. bool value = false;
  246. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  247. for (i = 0; i < 4; ++i) {
  248. if (wdata->leds[i] == led_dev) {
  249. spin_lock_irqsave(&wdata->state.lock, flags);
  250. value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
  251. spin_unlock_irqrestore(&wdata->state.lock, flags);
  252. break;
  253. }
  254. }
  255. return value ? LED_FULL : LED_OFF;
  256. }
  257. static void wiimod_led_set(struct led_classdev *led_dev,
  258. enum led_brightness value)
  259. {
  260. struct wiimote_data *wdata;
  261. struct device *dev = led_dev->dev->parent;
  262. int i;
  263. unsigned long flags;
  264. __u8 state, flag;
  265. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  266. for (i = 0; i < 4; ++i) {
  267. if (wdata->leds[i] == led_dev) {
  268. flag = WIIPROTO_FLAG_LED(i + 1);
  269. spin_lock_irqsave(&wdata->state.lock, flags);
  270. state = wdata->state.flags;
  271. if (value == LED_OFF)
  272. wiiproto_req_leds(wdata, state & ~flag);
  273. else
  274. wiiproto_req_leds(wdata, state | flag);
  275. spin_unlock_irqrestore(&wdata->state.lock, flags);
  276. break;
  277. }
  278. }
  279. }
  280. static int wiimod_led_probe(const struct wiimod_ops *ops,
  281. struct wiimote_data *wdata)
  282. {
  283. struct device *dev = &wdata->hdev->dev;
  284. size_t namesz = strlen(dev_name(dev)) + 9;
  285. struct led_classdev *led;
  286. unsigned long flags;
  287. char *name;
  288. int ret;
  289. led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
  290. if (!led)
  291. return -ENOMEM;
  292. name = (void*)&led[1];
  293. snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
  294. led->name = name;
  295. led->brightness = 0;
  296. led->max_brightness = 1;
  297. led->brightness_get = wiimod_led_get;
  298. led->brightness_set = wiimod_led_set;
  299. wdata->leds[ops->arg] = led;
  300. ret = led_classdev_register(dev, led);
  301. if (ret)
  302. goto err_free;
  303. /* enable LED1 to stop initial LED-blinking */
  304. if (ops->arg == 0) {
  305. spin_lock_irqsave(&wdata->state.lock, flags);
  306. wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
  307. spin_unlock_irqrestore(&wdata->state.lock, flags);
  308. }
  309. return 0;
  310. err_free:
  311. wdata->leds[ops->arg] = NULL;
  312. kfree(led);
  313. return ret;
  314. }
  315. static void wiimod_led_remove(const struct wiimod_ops *ops,
  316. struct wiimote_data *wdata)
  317. {
  318. if (!wdata->leds[ops->arg])
  319. return;
  320. led_classdev_unregister(wdata->leds[ops->arg]);
  321. kfree(wdata->leds[ops->arg]);
  322. wdata->leds[ops->arg] = NULL;
  323. }
  324. static const struct wiimod_ops wiimod_leds[4] = {
  325. {
  326. .flags = 0,
  327. .arg = 0,
  328. .probe = wiimod_led_probe,
  329. .remove = wiimod_led_remove,
  330. },
  331. {
  332. .flags = 0,
  333. .arg = 1,
  334. .probe = wiimod_led_probe,
  335. .remove = wiimod_led_remove,
  336. },
  337. {
  338. .flags = 0,
  339. .arg = 2,
  340. .probe = wiimod_led_probe,
  341. .remove = wiimod_led_remove,
  342. },
  343. {
  344. .flags = 0,
  345. .arg = 3,
  346. .probe = wiimod_led_probe,
  347. .remove = wiimod_led_remove,
  348. },
  349. };
  350. /* module table */
  351. const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
  352. [WIIMOD_KEYS] = &wiimod_keys,
  353. [WIIMOD_RUMBLE] = &wiimod_rumble,
  354. [WIIMOD_BATTERY] = &wiimod_battery,
  355. [WIIMOD_LED1] = &wiimod_leds[0],
  356. [WIIMOD_LED2] = &wiimod_leds[1],
  357. [WIIMOD_LED3] = &wiimod_leds[2],
  358. [WIIMOD_LED4] = &wiimod_leds[3],
  359. };