hid-wiimote-ext.c 23 KB

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