hid-wiimote-ext.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * HID driver for Nintendo Wiimote extension devices
  3. * Copyright (c) 2011 David Herrmann
  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. #include <linux/atomic.h>
  12. #include <linux/module.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/workqueue.h>
  15. #include "hid-wiimote.h"
  16. struct wiimote_ext {
  17. struct wiimote_data *wdata;
  18. struct work_struct worker;
  19. struct input_dev *input;
  20. struct input_dev *mp_input;
  21. atomic_t opened;
  22. atomic_t mp_opened;
  23. bool plugged;
  24. bool mp_plugged;
  25. bool motionp;
  26. __u8 ext_type;
  27. __u16 calib[4][3];
  28. };
  29. enum wiiext_type {
  30. WIIEXT_NONE, /* placeholder */
  31. WIIEXT_CLASSIC, /* Nintendo classic controller */
  32. WIIEXT_NUNCHUCK, /* Nintendo nunchuck controller */
  33. WIIEXT_BALANCE_BOARD, /* Nintendo balance board controller */
  34. };
  35. enum wiiext_keys {
  36. WIIEXT_KEY_C,
  37. WIIEXT_KEY_Z,
  38. WIIEXT_KEY_A,
  39. WIIEXT_KEY_B,
  40. WIIEXT_KEY_X,
  41. WIIEXT_KEY_Y,
  42. WIIEXT_KEY_ZL,
  43. WIIEXT_KEY_ZR,
  44. WIIEXT_KEY_PLUS,
  45. WIIEXT_KEY_MINUS,
  46. WIIEXT_KEY_HOME,
  47. WIIEXT_KEY_LEFT,
  48. WIIEXT_KEY_RIGHT,
  49. WIIEXT_KEY_UP,
  50. WIIEXT_KEY_DOWN,
  51. WIIEXT_KEY_LT,
  52. WIIEXT_KEY_RT,
  53. WIIEXT_KEY_COUNT
  54. };
  55. static __u16 wiiext_keymap[] = {
  56. BTN_C, /* WIIEXT_KEY_C */
  57. BTN_Z, /* WIIEXT_KEY_Z */
  58. BTN_A, /* WIIEXT_KEY_A */
  59. BTN_B, /* WIIEXT_KEY_B */
  60. BTN_X, /* WIIEXT_KEY_X */
  61. BTN_Y, /* WIIEXT_KEY_Y */
  62. BTN_TL2, /* WIIEXT_KEY_ZL */
  63. BTN_TR2, /* WIIEXT_KEY_ZR */
  64. KEY_NEXT, /* WIIEXT_KEY_PLUS */
  65. KEY_PREVIOUS, /* WIIEXT_KEY_MINUS */
  66. BTN_MODE, /* WIIEXT_KEY_HOME */
  67. KEY_LEFT, /* WIIEXT_KEY_LEFT */
  68. KEY_RIGHT, /* WIIEXT_KEY_RIGHT */
  69. KEY_UP, /* WIIEXT_KEY_UP */
  70. KEY_DOWN, /* WIIEXT_KEY_DOWN */
  71. BTN_TL, /* WIIEXT_KEY_LT */
  72. BTN_TR, /* WIIEXT_KEY_RT */
  73. };
  74. /* disable all extensions */
  75. static void ext_disable(struct wiimote_ext *ext)
  76. {
  77. unsigned long flags;
  78. __u8 wmem = 0x55;
  79. if (!wiimote_cmd_acquire(ext->wdata)) {
  80. wiimote_cmd_write(ext->wdata, 0xa400f0, &wmem, sizeof(wmem));
  81. wiimote_cmd_release(ext->wdata);
  82. }
  83. spin_lock_irqsave(&ext->wdata->state.lock, flags);
  84. ext->motionp = false;
  85. ext->ext_type = WIIEXT_NONE;
  86. wiiproto_req_drm(ext->wdata, WIIPROTO_REQ_NULL);
  87. spin_unlock_irqrestore(&ext->wdata->state.lock, flags);
  88. }
  89. static bool motionp_read(struct wiimote_ext *ext)
  90. {
  91. __u8 rmem[2], wmem;
  92. ssize_t ret;
  93. bool avail = false;
  94. if (!atomic_read(&ext->mp_opened))
  95. return false;
  96. if (wiimote_cmd_acquire(ext->wdata))
  97. return false;
  98. /* initialize motion plus */
  99. wmem = 0x55;
  100. ret = wiimote_cmd_write(ext->wdata, 0xa600f0, &wmem, sizeof(wmem));
  101. if (ret)
  102. goto error;
  103. /* read motion plus ID */
  104. ret = wiimote_cmd_read(ext->wdata, 0xa600fe, rmem, 2);
  105. if (ret == 2 || rmem[1] == 0x5)
  106. avail = true;
  107. error:
  108. wiimote_cmd_release(ext->wdata);
  109. return avail;
  110. }
  111. static __u8 ext_read(struct wiimote_ext *ext)
  112. {
  113. ssize_t ret;
  114. __u8 buf[24], i, j, offs = 0;
  115. __u8 rmem[2], wmem;
  116. __u8 type = WIIEXT_NONE;
  117. if (!ext->plugged || !atomic_read(&ext->opened))
  118. return WIIEXT_NONE;
  119. if (wiimote_cmd_acquire(ext->wdata))
  120. return WIIEXT_NONE;
  121. /* initialize extension */
  122. wmem = 0x55;
  123. ret = wiimote_cmd_write(ext->wdata, 0xa400f0, &wmem, sizeof(wmem));
  124. if (!ret) {
  125. /* disable encryption */
  126. wmem = 0x0;
  127. wiimote_cmd_write(ext->wdata, 0xa400fb, &wmem, sizeof(wmem));
  128. }
  129. /* read extension ID */
  130. ret = wiimote_cmd_read(ext->wdata, 0xa400fe, rmem, 2);
  131. if (ret == 2) {
  132. if (rmem[0] == 0 && rmem[1] == 0)
  133. type = WIIEXT_NUNCHUCK;
  134. else if (rmem[0] == 0x01 && rmem[1] == 0x01)
  135. type = WIIEXT_CLASSIC;
  136. else if (rmem[0] == 0x04 && rmem[1] == 0x02)
  137. type = WIIEXT_BALANCE_BOARD;
  138. }
  139. /* get balance board calibration data */
  140. if (type == WIIEXT_BALANCE_BOARD) {
  141. ret = wiimote_cmd_read(ext->wdata, 0xa40024, buf, 12);
  142. ret += wiimote_cmd_read(ext->wdata, 0xa40024 + 12,
  143. buf + 12, 12);
  144. if (ret != 24) {
  145. type = WIIEXT_NONE;
  146. } else {
  147. for (i = 0; i < 3; i++) {
  148. for (j = 0; j < 4; j++) {
  149. ext->calib[j][i] = buf[offs];
  150. ext->calib[j][i] <<= 8;
  151. ext->calib[j][i] |= buf[offs + 1];
  152. offs += 2;
  153. }
  154. }
  155. }
  156. }
  157. wiimote_cmd_release(ext->wdata);
  158. return type;
  159. }
  160. static void ext_enable(struct wiimote_ext *ext, bool motionp, __u8 ext_type)
  161. {
  162. unsigned long flags;
  163. __u8 wmem;
  164. int ret;
  165. if (motionp) {
  166. if (wiimote_cmd_acquire(ext->wdata))
  167. return;
  168. if (ext_type == WIIEXT_CLASSIC)
  169. wmem = 0x07;
  170. else if (ext_type == WIIEXT_NUNCHUCK)
  171. wmem = 0x05;
  172. else
  173. wmem = 0x04;
  174. ret = wiimote_cmd_write(ext->wdata, 0xa600fe, &wmem, sizeof(wmem));
  175. wiimote_cmd_release(ext->wdata);
  176. if (ret)
  177. return;
  178. }
  179. spin_lock_irqsave(&ext->wdata->state.lock, flags);
  180. ext->motionp = motionp;
  181. ext->ext_type = ext_type;
  182. wiiproto_req_drm(ext->wdata, WIIPROTO_REQ_NULL);
  183. spin_unlock_irqrestore(&ext->wdata->state.lock, flags);
  184. }
  185. static void wiiext_worker(struct work_struct *work)
  186. {
  187. struct wiimote_ext *ext = container_of(work, struct wiimote_ext,
  188. worker);
  189. bool motionp;
  190. __u8 ext_type;
  191. ext_disable(ext);
  192. motionp = motionp_read(ext);
  193. ext_type = ext_read(ext);
  194. ext_enable(ext, motionp, ext_type);
  195. }
  196. /* schedule work only once, otherwise mark for reschedule */
  197. static void wiiext_schedule(struct wiimote_ext *ext)
  198. {
  199. schedule_work(&ext->worker);
  200. }
  201. /*
  202. * Reacts on extension port events
  203. * Whenever the driver gets an event from the wiimote that an extension has been
  204. * plugged or unplugged, this funtion shall be called. It checks what extensions
  205. * are connected and initializes and activates them.
  206. * This can be called in atomic context. The initialization is done in a
  207. * separate worker thread. The state.lock spinlock must be held by the caller.
  208. */
  209. void wiiext_event(struct wiimote_data *wdata, bool plugged)
  210. {
  211. if (!wdata->ext)
  212. return;
  213. if (wdata->ext->plugged == plugged)
  214. return;
  215. wdata->ext->plugged = plugged;
  216. if (!plugged)
  217. wdata->ext->mp_plugged = false;
  218. /*
  219. * We need to call wiiext_schedule(wdata->ext) here, however, the
  220. * extension initialization logic is not fully understood and so
  221. * automatic initialization is not supported, yet.
  222. */
  223. }
  224. /*
  225. * Returns true if the current DRM mode should contain extension data and false
  226. * if there is no interest in extension data.
  227. * All supported extensions send 6 byte extension data so any DRM that contains
  228. * extension bytes is fine.
  229. * The caller must hold the state.lock spinlock.
  230. */
  231. bool wiiext_active(struct wiimote_data *wdata)
  232. {
  233. if (!wdata->ext)
  234. return false;
  235. return wdata->ext->motionp || wdata->ext->ext_type;
  236. }
  237. static void handler_motionp(struct wiimote_ext *ext, const __u8 *payload)
  238. {
  239. __s32 x, y, z;
  240. bool plugged;
  241. /* | 8 7 6 5 4 3 | 2 | 1 |
  242. * -----+------------------------------+-----+-----+
  243. * 1 | Yaw Speed <7:0> |
  244. * 2 | Roll Speed <7:0> |
  245. * 3 | Pitch Speed <7:0> |
  246. * -----+------------------------------+-----+-----+
  247. * 4 | Yaw Speed <13:8> | Yaw |Pitch|
  248. * -----+------------------------------+-----+-----+
  249. * 5 | Roll Speed <13:8> |Roll | Ext |
  250. * -----+------------------------------+-----+-----+
  251. * 6 | Pitch Speed <13:8> | 1 | 0 |
  252. * -----+------------------------------+-----+-----+
  253. * The single bits Yaw, Roll, Pitch in the lower right corner specify
  254. * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
  255. * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
  256. * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
  257. * and 9 for slow.
  258. * If the wiimote is not rotating the sensor reports 2^13 = 8192.
  259. * Ext specifies whether an extension is connected to the motionp.
  260. */
  261. x = payload[0];
  262. y = payload[1];
  263. z = payload[2];
  264. x |= (((__u16)payload[3]) << 6) & 0xff00;
  265. y |= (((__u16)payload[4]) << 6) & 0xff00;
  266. z |= (((__u16)payload[5]) << 6) & 0xff00;
  267. x -= 8192;
  268. y -= 8192;
  269. z -= 8192;
  270. if (!(payload[3] & 0x02))
  271. x *= 18;
  272. else
  273. x *= 9;
  274. if (!(payload[4] & 0x02))
  275. y *= 18;
  276. else
  277. y *= 9;
  278. if (!(payload[3] & 0x01))
  279. z *= 18;
  280. else
  281. z *= 9;
  282. input_report_abs(ext->mp_input, ABS_RX, x);
  283. input_report_abs(ext->mp_input, ABS_RY, y);
  284. input_report_abs(ext->mp_input, ABS_RZ, z);
  285. input_sync(ext->mp_input);
  286. plugged = payload[5] & 0x01;
  287. if (plugged != ext->mp_plugged)
  288. ext->mp_plugged = plugged;
  289. }
  290. static void handler_nunchuck(struct wiimote_ext *ext, const __u8 *payload)
  291. {
  292. __s16 x, y, z, bx, by;
  293. /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 |
  294. * -----+----------+---------+---------+----+-----+
  295. * 1 | Button X <7:0> |
  296. * 2 | Button Y <7:0> |
  297. * -----+----------+---------+---------+----+-----+
  298. * 3 | Speed X <9:2> |
  299. * 4 | Speed Y <9:2> |
  300. * 5 | Speed Z <9:2> |
  301. * -----+----------+---------+---------+----+-----+
  302. * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ |
  303. * -----+----------+---------+---------+----+-----+
  304. * Button X/Y is the analog stick. Speed X, Y and Z are the
  305. * accelerometer data in the same format as the wiimote's accelerometer.
  306. * The 6th byte contains the LSBs of the accelerometer data.
  307. * BC and BZ are the C and Z buttons: 0 means pressed
  308. *
  309. * If reported interleaved with motionp, then the layout changes. The
  310. * 5th and 6th byte changes to:
  311. * -----+-----------------------------------+-----+
  312. * 5 | Speed Z <9:3> | EXT |
  313. * -----+--------+-----+-----+----+----+----+-----+
  314. * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 |
  315. * -----+--------+-----+-----+----+----+----+-----+
  316. * All three accelerometer values lose their LSB. The other data is
  317. * still available but slightly moved.
  318. *
  319. * Center data for button values is 128. Center value for accelerometer
  320. * values it 512 / 0x200
  321. */
  322. bx = payload[0];
  323. by = payload[1];
  324. bx -= 128;
  325. by -= 128;
  326. x = payload[2] << 2;
  327. y = payload[3] << 2;
  328. z = payload[4] << 2;
  329. if (ext->motionp) {
  330. x |= (payload[5] >> 3) & 0x02;
  331. y |= (payload[5] >> 4) & 0x02;
  332. z &= ~0x4;
  333. z |= (payload[5] >> 5) & 0x06;
  334. } else {
  335. x |= (payload[5] >> 2) & 0x03;
  336. y |= (payload[5] >> 4) & 0x03;
  337. z |= (payload[5] >> 6) & 0x03;
  338. }
  339. x -= 0x200;
  340. y -= 0x200;
  341. z -= 0x200;
  342. input_report_abs(ext->input, ABS_HAT0X, bx);
  343. input_report_abs(ext->input, ABS_HAT0Y, by);
  344. input_report_abs(ext->input, ABS_RX, x);
  345. input_report_abs(ext->input, ABS_RY, y);
  346. input_report_abs(ext->input, ABS_RZ, z);
  347. if (ext->motionp) {
  348. input_report_key(ext->input,
  349. wiiext_keymap[WIIEXT_KEY_Z], !(payload[5] & 0x04));
  350. input_report_key(ext->input,
  351. wiiext_keymap[WIIEXT_KEY_C], !(payload[5] & 0x08));
  352. } else {
  353. input_report_key(ext->input,
  354. wiiext_keymap[WIIEXT_KEY_Z], !(payload[5] & 0x01));
  355. input_report_key(ext->input,
  356. wiiext_keymap[WIIEXT_KEY_C], !(payload[5] & 0x02));
  357. }
  358. input_sync(ext->input);
  359. }
  360. static void handler_classic(struct wiimote_ext *ext, const __u8 *payload)
  361. {
  362. __s8 rx, ry, lx, ly, lt, rt;
  363. /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  364. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  365. * 1 | RX <5:4> | LX <5:0> |
  366. * 2 | RX <3:2> | LY <5:0> |
  367. * -----+-----+-----+-----+-----------------------------+
  368. * 3 |RX<1>| LT <5:4> | RY <5:1> |
  369. * -----+-----+-----------+-----------------------------+
  370. * 4 | LT <3:1> | RT <5:1> |
  371. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  372. * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
  373. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  374. * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
  375. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  376. * All buttons are 0 if pressed
  377. * RX and RY are right analog stick
  378. * LX and LY are left analog stick
  379. * LT is left trigger, RT is right trigger
  380. * BLT is 0 if left trigger is fully pressed
  381. * BRT is 0 if right trigger is fully pressed
  382. * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
  383. * BZL is left Z button and BZR is right Z button
  384. * B-, BH, B+ are +, HOME and - buttons
  385. * BB, BY, BA, BX are A, B, X, Y buttons
  386. * LSB of RX, RY, LT, and RT are not transmitted and always 0.
  387. *
  388. * With motionp enabled it changes slightly to this:
  389. * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  390. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  391. * 1 | RX <4:3> | LX <5:1> | BDU |
  392. * 2 | RX <2:1> | LY <5:1> | BDL |
  393. * -----+-----+-----+-----+-----------------------+-----+
  394. * 3 |RX<0>| LT <4:3> | RY <4:0> |
  395. * -----+-----+-----------+-----------------------------+
  396. * 4 | LT <2:0> | RT <4:0> |
  397. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  398. * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT |
  399. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  400. * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 |
  401. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  402. * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
  403. * is the same as before.
  404. */
  405. if (ext->motionp) {
  406. lx = payload[0] & 0x3e;
  407. ly = payload[0] & 0x3e;
  408. } else {
  409. lx = payload[0] & 0x3f;
  410. ly = payload[0] & 0x3f;
  411. }
  412. rx = (payload[0] >> 3) & 0x14;
  413. rx |= (payload[1] >> 5) & 0x06;
  414. rx |= (payload[2] >> 7) & 0x01;
  415. ry = payload[2] & 0x1f;
  416. rt = payload[3] & 0x1f;
  417. lt = (payload[2] >> 2) & 0x18;
  418. lt |= (payload[3] >> 5) & 0x07;
  419. rx <<= 1;
  420. ry <<= 1;
  421. rt <<= 1;
  422. lt <<= 1;
  423. input_report_abs(ext->input, ABS_HAT1X, lx - 0x20);
  424. input_report_abs(ext->input, ABS_HAT1Y, ly - 0x20);
  425. input_report_abs(ext->input, ABS_HAT2X, rx - 0x20);
  426. input_report_abs(ext->input, ABS_HAT2Y, ry - 0x20);
  427. input_report_abs(ext->input, ABS_HAT3X, rt - 0x20);
  428. input_report_abs(ext->input, ABS_HAT3Y, lt - 0x20);
  429. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_RIGHT],
  430. !!(payload[4] & 0x80));
  431. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_DOWN],
  432. !!(payload[4] & 0x40));
  433. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LT],
  434. !!(payload[4] & 0x20));
  435. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_MINUS],
  436. !!(payload[4] & 0x10));
  437. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_HOME],
  438. !!(payload[4] & 0x08));
  439. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_PLUS],
  440. !!(payload[4] & 0x04));
  441. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_RT],
  442. !!(payload[4] & 0x02));
  443. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_ZL],
  444. !!(payload[5] & 0x80));
  445. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_B],
  446. !!(payload[5] & 0x40));
  447. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_Y],
  448. !!(payload[5] & 0x20));
  449. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_A],
  450. !!(payload[5] & 0x10));
  451. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_X],
  452. !!(payload[5] & 0x08));
  453. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_ZR],
  454. !!(payload[5] & 0x04));
  455. if (ext->motionp) {
  456. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_UP],
  457. !!(payload[0] & 0x01));
  458. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LEFT],
  459. !!(payload[1] & 0x01));
  460. } else {
  461. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_UP],
  462. !!(payload[5] & 0x01));
  463. input_report_key(ext->input, wiiext_keymap[WIIEXT_KEY_LEFT],
  464. !!(payload[5] & 0x02));
  465. }
  466. input_sync(ext->input);
  467. }
  468. static void handler_balance_board(struct wiimote_ext *ext, const __u8 *payload)
  469. {
  470. __s32 val[4], tmp;
  471. unsigned int i;
  472. /* Byte | 8 7 6 5 4 3 2 1 |
  473. * -----+--------------------------+
  474. * 1 | Top Right <15:8> |
  475. * 2 | Top Right <7:0> |
  476. * -----+--------------------------+
  477. * 3 | Bottom Right <15:8> |
  478. * 4 | Bottom Right <7:0> |
  479. * -----+--------------------------+
  480. * 5 | Top Left <15:8> |
  481. * 6 | Top Left <7:0> |
  482. * -----+--------------------------+
  483. * 7 | Bottom Left <15:8> |
  484. * 8 | Bottom Left <7:0> |
  485. * -----+--------------------------+
  486. *
  487. * These values represent the weight-measurements of the Wii-balance
  488. * board with 16bit precision.
  489. *
  490. * The balance-board is never reported interleaved with motionp.
  491. */
  492. val[0] = payload[0];
  493. val[0] <<= 8;
  494. val[0] |= payload[1];
  495. val[1] = payload[2];
  496. val[1] <<= 8;
  497. val[1] |= payload[3];
  498. val[2] = payload[4];
  499. val[2] <<= 8;
  500. val[2] |= payload[5];
  501. val[3] = payload[6];
  502. val[3] <<= 8;
  503. val[3] |= payload[7];
  504. /* apply calibration data */
  505. for (i = 0; i < 4; i++) {
  506. if (val[i] < ext->calib[i][1]) {
  507. tmp = val[i] - ext->calib[i][0];
  508. tmp *= 1700;
  509. tmp /= ext->calib[i][1] - ext->calib[i][0];
  510. } else {
  511. tmp = val[i] - ext->calib[i][1];
  512. tmp *= 1700;
  513. tmp /= ext->calib[i][2] - ext->calib[i][1];
  514. tmp += 1700;
  515. }
  516. val[i] = tmp;
  517. }
  518. input_report_abs(ext->input, ABS_HAT0X, val[0]);
  519. input_report_abs(ext->input, ABS_HAT0Y, val[1]);
  520. input_report_abs(ext->input, ABS_HAT1X, val[2]);
  521. input_report_abs(ext->input, ABS_HAT1Y, val[3]);
  522. input_sync(ext->input);
  523. }
  524. /* call this with state.lock spinlock held */
  525. void wiiext_handle(struct wiimote_data *wdata, const __u8 *payload)
  526. {
  527. struct wiimote_ext *ext = wdata->ext;
  528. if (!ext)
  529. return;
  530. if (ext->motionp && (payload[5] & 0x02)) {
  531. handler_motionp(ext, payload);
  532. } else if (ext->ext_type == WIIEXT_NUNCHUCK) {
  533. handler_nunchuck(ext, payload);
  534. } else if (ext->ext_type == WIIEXT_CLASSIC) {
  535. handler_classic(ext, payload);
  536. } else if (ext->ext_type == WIIEXT_BALANCE_BOARD) {
  537. handler_balance_board(ext, payload);
  538. }
  539. }
  540. static ssize_t wiiext_show(struct device *dev, struct device_attribute *attr,
  541. char *buf)
  542. {
  543. struct wiimote_data *wdata = dev_to_wii(dev);
  544. __u8 type = WIIEXT_NONE;
  545. bool motionp = false;
  546. unsigned long flags;
  547. spin_lock_irqsave(&wdata->state.lock, flags);
  548. if (wdata->ext) {
  549. motionp = wdata->ext->motionp;
  550. type = wdata->ext->ext_type;
  551. }
  552. spin_unlock_irqrestore(&wdata->state.lock, flags);
  553. if (type == WIIEXT_NUNCHUCK) {
  554. if (motionp)
  555. return sprintf(buf, "motionp+nunchuck\n");
  556. else
  557. return sprintf(buf, "nunchuck\n");
  558. } else if (type == WIIEXT_CLASSIC) {
  559. if (motionp)
  560. return sprintf(buf, "motionp+classic\n");
  561. else
  562. return sprintf(buf, "classic\n");
  563. } else if (type == WIIEXT_BALANCE_BOARD) {
  564. if (motionp)
  565. return sprintf(buf, "motionp+balanceboard\n");
  566. else
  567. return sprintf(buf, "balanceboard\n");
  568. } else {
  569. if (motionp)
  570. return sprintf(buf, "motionp\n");
  571. else
  572. return sprintf(buf, "none\n");
  573. }
  574. }
  575. static DEVICE_ATTR(extension, S_IRUGO, wiiext_show, NULL);
  576. static int wiiext_input_open(struct input_dev *dev)
  577. {
  578. struct wiimote_ext *ext = input_get_drvdata(dev);
  579. int ret;
  580. ret = hid_hw_open(ext->wdata->hdev);
  581. if (ret)
  582. return ret;
  583. atomic_inc(&ext->opened);
  584. wiiext_schedule(ext);
  585. return 0;
  586. }
  587. static void wiiext_input_close(struct input_dev *dev)
  588. {
  589. struct wiimote_ext *ext = input_get_drvdata(dev);
  590. atomic_dec(&ext->opened);
  591. wiiext_schedule(ext);
  592. hid_hw_close(ext->wdata->hdev);
  593. }
  594. static int wiiext_mp_open(struct input_dev *dev)
  595. {
  596. struct wiimote_ext *ext = input_get_drvdata(dev);
  597. int ret;
  598. ret = hid_hw_open(ext->wdata->hdev);
  599. if (ret)
  600. return ret;
  601. atomic_inc(&ext->mp_opened);
  602. wiiext_schedule(ext);
  603. return 0;
  604. }
  605. static void wiiext_mp_close(struct input_dev *dev)
  606. {
  607. struct wiimote_ext *ext = input_get_drvdata(dev);
  608. atomic_dec(&ext->mp_opened);
  609. wiiext_schedule(ext);
  610. hid_hw_close(ext->wdata->hdev);
  611. }
  612. /* Initializes the extension driver of a wiimote */
  613. int wiiext_init(struct wiimote_data *wdata)
  614. {
  615. struct wiimote_ext *ext;
  616. unsigned long flags;
  617. int ret, i;
  618. ext = kzalloc(sizeof(*ext), GFP_KERNEL);
  619. if (!ext)
  620. return -ENOMEM;
  621. ext->wdata = wdata;
  622. INIT_WORK(&ext->worker, wiiext_worker);
  623. ext->input = input_allocate_device();
  624. if (!ext->input) {
  625. ret = -ENOMEM;
  626. goto err_input;
  627. }
  628. input_set_drvdata(ext->input, ext);
  629. ext->input->open = wiiext_input_open;
  630. ext->input->close = wiiext_input_close;
  631. ext->input->dev.parent = &wdata->hdev->dev;
  632. ext->input->id.bustype = wdata->hdev->bus;
  633. ext->input->id.vendor = wdata->hdev->vendor;
  634. ext->input->id.product = wdata->hdev->product;
  635. ext->input->id.version = wdata->hdev->version;
  636. ext->input->name = WIIMOTE_NAME " Extension";
  637. set_bit(EV_KEY, ext->input->evbit);
  638. for (i = 0; i < WIIEXT_KEY_COUNT; ++i)
  639. set_bit(wiiext_keymap[i], ext->input->keybit);
  640. set_bit(EV_ABS, ext->input->evbit);
  641. set_bit(ABS_HAT0X, ext->input->absbit);
  642. set_bit(ABS_HAT0Y, ext->input->absbit);
  643. set_bit(ABS_HAT1X, ext->input->absbit);
  644. set_bit(ABS_HAT1Y, ext->input->absbit);
  645. set_bit(ABS_HAT2X, ext->input->absbit);
  646. set_bit(ABS_HAT2Y, ext->input->absbit);
  647. set_bit(ABS_HAT3X, ext->input->absbit);
  648. set_bit(ABS_HAT3Y, ext->input->absbit);
  649. input_set_abs_params(ext->input, ABS_HAT0X, -120, 120, 2, 4);
  650. input_set_abs_params(ext->input, ABS_HAT0Y, -120, 120, 2, 4);
  651. input_set_abs_params(ext->input, ABS_HAT1X, -30, 30, 1, 1);
  652. input_set_abs_params(ext->input, ABS_HAT1Y, -30, 30, 1, 1);
  653. input_set_abs_params(ext->input, ABS_HAT2X, -30, 30, 1, 1);
  654. input_set_abs_params(ext->input, ABS_HAT2Y, -30, 30, 1, 1);
  655. input_set_abs_params(ext->input, ABS_HAT3X, -30, 30, 1, 1);
  656. input_set_abs_params(ext->input, ABS_HAT3Y, -30, 30, 1, 1);
  657. set_bit(ABS_RX, ext->input->absbit);
  658. set_bit(ABS_RY, ext->input->absbit);
  659. set_bit(ABS_RZ, ext->input->absbit);
  660. input_set_abs_params(ext->input, ABS_RX, -500, 500, 2, 4);
  661. input_set_abs_params(ext->input, ABS_RY, -500, 500, 2, 4);
  662. input_set_abs_params(ext->input, ABS_RZ, -500, 500, 2, 4);
  663. ret = input_register_device(ext->input);
  664. if (ret) {
  665. input_free_device(ext->input);
  666. goto err_input;
  667. }
  668. ext->mp_input = input_allocate_device();
  669. if (!ext->mp_input) {
  670. ret = -ENOMEM;
  671. goto err_mp;
  672. }
  673. input_set_drvdata(ext->mp_input, ext);
  674. ext->mp_input->open = wiiext_mp_open;
  675. ext->mp_input->close = wiiext_mp_close;
  676. ext->mp_input->dev.parent = &wdata->hdev->dev;
  677. ext->mp_input->id.bustype = wdata->hdev->bus;
  678. ext->mp_input->id.vendor = wdata->hdev->vendor;
  679. ext->mp_input->id.product = wdata->hdev->product;
  680. ext->mp_input->id.version = wdata->hdev->version;
  681. ext->mp_input->name = WIIMOTE_NAME " Motion+";
  682. set_bit(EV_ABS, ext->mp_input->evbit);
  683. set_bit(ABS_RX, ext->mp_input->absbit);
  684. set_bit(ABS_RY, ext->mp_input->absbit);
  685. set_bit(ABS_RZ, ext->mp_input->absbit);
  686. input_set_abs_params(ext->mp_input, ABS_RX, -160000, 160000, 4, 8);
  687. input_set_abs_params(ext->mp_input, ABS_RY, -160000, 160000, 4, 8);
  688. input_set_abs_params(ext->mp_input, ABS_RZ, -160000, 160000, 4, 8);
  689. ret = input_register_device(ext->mp_input);
  690. if (ret) {
  691. input_free_device(ext->mp_input);
  692. goto err_mp;
  693. }
  694. ret = device_create_file(&wdata->hdev->dev, &dev_attr_extension);
  695. if (ret)
  696. goto err_dev;
  697. spin_lock_irqsave(&wdata->state.lock, flags);
  698. wdata->ext = ext;
  699. spin_unlock_irqrestore(&wdata->state.lock, flags);
  700. return 0;
  701. err_dev:
  702. input_unregister_device(ext->mp_input);
  703. err_mp:
  704. input_unregister_device(ext->input);
  705. err_input:
  706. kfree(ext);
  707. return ret;
  708. }
  709. /* Deinitializes the extension driver of a wiimote */
  710. void wiiext_deinit(struct wiimote_data *wdata)
  711. {
  712. struct wiimote_ext *ext = wdata->ext;
  713. unsigned long flags;
  714. if (!ext)
  715. return;
  716. /*
  717. * We first unset wdata->ext to avoid further input from the wiimote
  718. * core. The worker thread does not access this pointer so it is not
  719. * affected by this.
  720. * We kill the worker after this so it does not get respawned during
  721. * deinitialization.
  722. */
  723. spin_lock_irqsave(&wdata->state.lock, flags);
  724. wdata->ext = NULL;
  725. spin_unlock_irqrestore(&wdata->state.lock, flags);
  726. device_remove_file(&wdata->hdev->dev, &dev_attr_extension);
  727. input_unregister_device(ext->mp_input);
  728. input_unregister_device(ext->input);
  729. cancel_work_sync(&ext->worker);
  730. kfree(ext);
  731. }