hid-wiimote-ext.c 21 KB

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