hid-wiimote-core.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * HID driver for Nintendo Wii / Wii U peripherals
  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. #include <linux/completion.h>
  12. #include <linux/device.h>
  13. #include <linux/hid.h>
  14. #include <linux/input.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/spinlock.h>
  18. #include "hid-ids.h"
  19. #include "hid-wiimote.h"
  20. /* output queue handling */
  21. static int wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
  22. size_t count)
  23. {
  24. __u8 *buf;
  25. int ret;
  26. if (!hdev->hid_output_raw_report)
  27. return -ENODEV;
  28. buf = kmemdup(buffer, count, GFP_KERNEL);
  29. if (!buf)
  30. return -ENOMEM;
  31. ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
  32. kfree(buf);
  33. return ret;
  34. }
  35. static void wiimote_queue_worker(struct work_struct *work)
  36. {
  37. struct wiimote_queue *queue = container_of(work, struct wiimote_queue,
  38. worker);
  39. struct wiimote_data *wdata = container_of(queue, struct wiimote_data,
  40. queue);
  41. unsigned long flags;
  42. int ret;
  43. spin_lock_irqsave(&wdata->queue.lock, flags);
  44. while (wdata->queue.head != wdata->queue.tail) {
  45. spin_unlock_irqrestore(&wdata->queue.lock, flags);
  46. ret = wiimote_hid_send(wdata->hdev,
  47. wdata->queue.outq[wdata->queue.tail].data,
  48. wdata->queue.outq[wdata->queue.tail].size);
  49. if (ret < 0) {
  50. spin_lock_irqsave(&wdata->state.lock, flags);
  51. wiimote_cmd_abort(wdata);
  52. spin_unlock_irqrestore(&wdata->state.lock, flags);
  53. }
  54. spin_lock_irqsave(&wdata->queue.lock, flags);
  55. wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
  56. }
  57. spin_unlock_irqrestore(&wdata->queue.lock, flags);
  58. }
  59. static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
  60. size_t count)
  61. {
  62. unsigned long flags;
  63. __u8 newhead;
  64. if (count > HID_MAX_BUFFER_SIZE) {
  65. hid_warn(wdata->hdev, "Sending too large output report\n");
  66. spin_lock_irqsave(&wdata->queue.lock, flags);
  67. goto out_error;
  68. }
  69. /*
  70. * Copy new request into our output queue and check whether the
  71. * queue is full. If it is full, discard this request.
  72. * If it is empty we need to start a new worker that will
  73. * send out the buffer to the hid device.
  74. * If the queue is not empty, then there must be a worker
  75. * that is currently sending out our buffer and this worker
  76. * will reschedule itself until the queue is empty.
  77. */
  78. spin_lock_irqsave(&wdata->queue.lock, flags);
  79. memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count);
  80. wdata->queue.outq[wdata->queue.head].size = count;
  81. newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE;
  82. if (wdata->queue.head == wdata->queue.tail) {
  83. wdata->queue.head = newhead;
  84. schedule_work(&wdata->queue.worker);
  85. } else if (newhead != wdata->queue.tail) {
  86. wdata->queue.head = newhead;
  87. } else {
  88. hid_warn(wdata->hdev, "Output queue is full");
  89. goto out_error;
  90. }
  91. goto out_unlock;
  92. out_error:
  93. wiimote_cmd_abort(wdata);
  94. out_unlock:
  95. spin_unlock_irqrestore(&wdata->queue.lock, flags);
  96. }
  97. /*
  98. * This sets the rumble bit on the given output report if rumble is
  99. * currently enabled.
  100. * \cmd1 must point to the second byte in the output report => &cmd[1]
  101. * This must be called on nearly every output report before passing it
  102. * into the output queue!
  103. */
  104. static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
  105. {
  106. if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
  107. *cmd1 |= 0x01;
  108. }
  109. void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
  110. {
  111. __u8 cmd[2];
  112. rumble = !!rumble;
  113. if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
  114. return;
  115. if (rumble)
  116. wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
  117. else
  118. wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
  119. cmd[0] = WIIPROTO_REQ_RUMBLE;
  120. cmd[1] = 0;
  121. wiiproto_keep_rumble(wdata, &cmd[1]);
  122. wiimote_queue(wdata, cmd, sizeof(cmd));
  123. }
  124. void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
  125. {
  126. __u8 cmd[2];
  127. leds &= WIIPROTO_FLAGS_LEDS;
  128. if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
  129. return;
  130. wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
  131. cmd[0] = WIIPROTO_REQ_LED;
  132. cmd[1] = 0;
  133. if (leds & WIIPROTO_FLAG_LED1)
  134. cmd[1] |= 0x10;
  135. if (leds & WIIPROTO_FLAG_LED2)
  136. cmd[1] |= 0x20;
  137. if (leds & WIIPROTO_FLAG_LED3)
  138. cmd[1] |= 0x40;
  139. if (leds & WIIPROTO_FLAG_LED4)
  140. cmd[1] |= 0x80;
  141. wiiproto_keep_rumble(wdata, &cmd[1]);
  142. wiimote_queue(wdata, cmd, sizeof(cmd));
  143. }
  144. /*
  145. * Check what peripherals of the wiimote are currently
  146. * active and select a proper DRM that supports all of
  147. * the requested data inputs.
  148. */
  149. static __u8 select_drm(struct wiimote_data *wdata)
  150. {
  151. __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
  152. bool ext = wiiext_active(wdata);
  153. if (ir == WIIPROTO_FLAG_IR_BASIC) {
  154. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
  155. return WIIPROTO_REQ_DRM_KAIE;
  156. else
  157. return WIIPROTO_REQ_DRM_KIE;
  158. } else if (ir == WIIPROTO_FLAG_IR_EXT) {
  159. return WIIPROTO_REQ_DRM_KAI;
  160. } else if (ir == WIIPROTO_FLAG_IR_FULL) {
  161. return WIIPROTO_REQ_DRM_SKAI1;
  162. } else {
  163. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
  164. if (ext)
  165. return WIIPROTO_REQ_DRM_KAE;
  166. else
  167. return WIIPROTO_REQ_DRM_KA;
  168. } else {
  169. if (ext)
  170. return WIIPROTO_REQ_DRM_KE;
  171. else
  172. return WIIPROTO_REQ_DRM_K;
  173. }
  174. }
  175. }
  176. void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
  177. {
  178. __u8 cmd[3];
  179. if (drm == WIIPROTO_REQ_NULL)
  180. drm = select_drm(wdata);
  181. cmd[0] = WIIPROTO_REQ_DRM;
  182. cmd[1] = 0;
  183. cmd[2] = drm;
  184. wdata->state.drm = drm;
  185. wiiproto_keep_rumble(wdata, &cmd[1]);
  186. wiimote_queue(wdata, cmd, sizeof(cmd));
  187. }
  188. void wiiproto_req_status(struct wiimote_data *wdata)
  189. {
  190. __u8 cmd[2];
  191. cmd[0] = WIIPROTO_REQ_SREQ;
  192. cmd[1] = 0;
  193. wiiproto_keep_rumble(wdata, &cmd[1]);
  194. wiimote_queue(wdata, cmd, sizeof(cmd));
  195. }
  196. void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
  197. {
  198. accel = !!accel;
  199. if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  200. return;
  201. if (accel)
  202. wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
  203. else
  204. wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
  205. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  206. }
  207. void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
  208. {
  209. __u8 cmd[2];
  210. cmd[0] = WIIPROTO_REQ_IR1;
  211. cmd[1] = flags;
  212. wiiproto_keep_rumble(wdata, &cmd[1]);
  213. wiimote_queue(wdata, cmd, sizeof(cmd));
  214. }
  215. void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
  216. {
  217. __u8 cmd[2];
  218. cmd[0] = WIIPROTO_REQ_IR2;
  219. cmd[1] = flags;
  220. wiiproto_keep_rumble(wdata, &cmd[1]);
  221. wiimote_queue(wdata, cmd, sizeof(cmd));
  222. }
  223. #define wiiproto_req_wreg(wdata, os, buf, sz) \
  224. wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
  225. #define wiiproto_req_weeprom(wdata, os, buf, sz) \
  226. wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
  227. static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
  228. __u32 offset, const __u8 *buf, __u8 size)
  229. {
  230. __u8 cmd[22];
  231. if (size > 16 || size == 0) {
  232. hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
  233. return;
  234. }
  235. memset(cmd, 0, sizeof(cmd));
  236. cmd[0] = WIIPROTO_REQ_WMEM;
  237. cmd[2] = (offset >> 16) & 0xff;
  238. cmd[3] = (offset >> 8) & 0xff;
  239. cmd[4] = offset & 0xff;
  240. cmd[5] = size;
  241. memcpy(&cmd[6], buf, size);
  242. if (!eeprom)
  243. cmd[1] |= 0x04;
  244. wiiproto_keep_rumble(wdata, &cmd[1]);
  245. wiimote_queue(wdata, cmd, sizeof(cmd));
  246. }
  247. void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
  248. __u16 size)
  249. {
  250. __u8 cmd[7];
  251. if (size == 0) {
  252. hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
  253. return;
  254. }
  255. cmd[0] = WIIPROTO_REQ_RMEM;
  256. cmd[1] = 0;
  257. cmd[2] = (offset >> 16) & 0xff;
  258. cmd[3] = (offset >> 8) & 0xff;
  259. cmd[4] = offset & 0xff;
  260. cmd[5] = (size >> 8) & 0xff;
  261. cmd[6] = size & 0xff;
  262. if (!eeprom)
  263. cmd[1] |= 0x04;
  264. wiiproto_keep_rumble(wdata, &cmd[1]);
  265. wiimote_queue(wdata, cmd, sizeof(cmd));
  266. }
  267. /* requries the cmd-mutex to be held */
  268. int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  269. const __u8 *wmem, __u8 size)
  270. {
  271. unsigned long flags;
  272. int ret;
  273. spin_lock_irqsave(&wdata->state.lock, flags);
  274. wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
  275. wiiproto_req_wreg(wdata, offset, wmem, size);
  276. spin_unlock_irqrestore(&wdata->state.lock, flags);
  277. ret = wiimote_cmd_wait(wdata);
  278. if (!ret && wdata->state.cmd_err)
  279. ret = -EIO;
  280. return ret;
  281. }
  282. /* requries the cmd-mutex to be held */
  283. ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
  284. __u8 size)
  285. {
  286. unsigned long flags;
  287. ssize_t ret;
  288. spin_lock_irqsave(&wdata->state.lock, flags);
  289. wdata->state.cmd_read_size = size;
  290. wdata->state.cmd_read_buf = rmem;
  291. wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
  292. wiiproto_req_rreg(wdata, offset, size);
  293. spin_unlock_irqrestore(&wdata->state.lock, flags);
  294. ret = wiimote_cmd_wait(wdata);
  295. spin_lock_irqsave(&wdata->state.lock, flags);
  296. wdata->state.cmd_read_buf = NULL;
  297. spin_unlock_irqrestore(&wdata->state.lock, flags);
  298. if (!ret) {
  299. if (wdata->state.cmd_read_size == 0)
  300. ret = -EIO;
  301. else
  302. ret = wdata->state.cmd_read_size;
  303. }
  304. return ret;
  305. }
  306. /* requires the cmd-mutex to be held */
  307. static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
  308. {
  309. __u8 wmem;
  310. int ret;
  311. /* initialize extension */
  312. wmem = 0x55;
  313. ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
  314. if (ret)
  315. return ret;
  316. /* disable default encryption */
  317. wmem = 0x0;
  318. ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
  319. if (ret)
  320. return ret;
  321. return 0;
  322. }
  323. /* requires the cmd-mutex to be held */
  324. static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata)
  325. {
  326. __u8 rmem[6];
  327. int ret;
  328. /* read extension ID */
  329. ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
  330. if (ret != 6)
  331. return WIIMOTE_EXT_NONE;
  332. if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
  333. rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
  334. return WIIMOTE_EXT_NONE;
  335. return WIIMOTE_EXT_UNKNOWN;
  336. }
  337. /* device module handling */
  338. static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
  339. [WIIMOTE_DEV_PENDING] = (const __u8[]){
  340. WIIMOD_NULL,
  341. },
  342. [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
  343. WIIMOD_NULL,
  344. },
  345. [WIIMOTE_DEV_GENERIC] = (const __u8[]){
  346. WIIMOD_KEYS,
  347. WIIMOD_RUMBLE,
  348. WIIMOD_BATTERY,
  349. WIIMOD_LED1,
  350. WIIMOD_LED2,
  351. WIIMOD_LED3,
  352. WIIMOD_LED4,
  353. WIIMOD_ACCEL,
  354. WIIMOD_IR,
  355. WIIMOD_NULL,
  356. },
  357. [WIIMOTE_DEV_GEN10] = (const __u8[]){
  358. WIIMOD_KEYS,
  359. WIIMOD_RUMBLE,
  360. WIIMOD_BATTERY,
  361. WIIMOD_LED1,
  362. WIIMOD_LED2,
  363. WIIMOD_LED3,
  364. WIIMOD_LED4,
  365. WIIMOD_ACCEL,
  366. WIIMOD_IR,
  367. WIIMOD_NULL,
  368. },
  369. [WIIMOTE_DEV_GEN20] = (const __u8[]){
  370. WIIMOD_KEYS,
  371. WIIMOD_RUMBLE,
  372. WIIMOD_BATTERY,
  373. WIIMOD_LED1,
  374. WIIMOD_LED2,
  375. WIIMOD_LED3,
  376. WIIMOD_LED4,
  377. WIIMOD_ACCEL,
  378. WIIMOD_IR,
  379. WIIMOD_NULL,
  380. },
  381. };
  382. static void wiimote_modules_load(struct wiimote_data *wdata,
  383. unsigned int devtype)
  384. {
  385. bool need_input = false;
  386. const __u8 *mods, *iter;
  387. const struct wiimod_ops *ops;
  388. int ret;
  389. mods = wiimote_devtype_mods[devtype];
  390. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  391. if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
  392. need_input = true;
  393. break;
  394. }
  395. }
  396. if (need_input) {
  397. wdata->input = input_allocate_device();
  398. if (!wdata->input)
  399. return;
  400. input_set_drvdata(wdata->input, wdata);
  401. wdata->input->dev.parent = &wdata->hdev->dev;
  402. wdata->input->id.bustype = wdata->hdev->bus;
  403. wdata->input->id.vendor = wdata->hdev->vendor;
  404. wdata->input->id.product = wdata->hdev->product;
  405. wdata->input->id.version = wdata->hdev->version;
  406. wdata->input->name = WIIMOTE_NAME;
  407. }
  408. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  409. ops = wiimod_table[*iter];
  410. if (!ops->probe)
  411. continue;
  412. ret = ops->probe(ops, wdata);
  413. if (ret)
  414. goto error;
  415. }
  416. if (wdata->input) {
  417. ret = input_register_device(wdata->input);
  418. if (ret)
  419. goto error;
  420. }
  421. spin_lock_irq(&wdata->state.lock);
  422. wdata->state.devtype = devtype;
  423. spin_unlock_irq(&wdata->state.lock);
  424. return;
  425. error:
  426. for ( ; iter-- != mods; ) {
  427. ops = wiimod_table[*iter];
  428. if (ops->remove)
  429. ops->remove(ops, wdata);
  430. }
  431. if (wdata->input) {
  432. input_free_device(wdata->input);
  433. wdata->input = NULL;
  434. }
  435. }
  436. static void wiimote_modules_unload(struct wiimote_data *wdata)
  437. {
  438. const __u8 *mods, *iter;
  439. const struct wiimod_ops *ops;
  440. unsigned long flags;
  441. mods = wiimote_devtype_mods[wdata->state.devtype];
  442. spin_lock_irqsave(&wdata->state.lock, flags);
  443. wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
  444. spin_unlock_irqrestore(&wdata->state.lock, flags);
  445. /* find end of list */
  446. for (iter = mods; *iter != WIIMOD_NULL; ++iter)
  447. /* empty */ ;
  448. if (wdata->input) {
  449. input_get_device(wdata->input);
  450. input_unregister_device(wdata->input);
  451. }
  452. for ( ; iter-- != mods; ) {
  453. ops = wiimod_table[*iter];
  454. if (ops->remove)
  455. ops->remove(ops, wdata);
  456. }
  457. if (wdata->input) {
  458. input_put_device(wdata->input);
  459. wdata->input = NULL;
  460. }
  461. }
  462. /* device (re-)initialization and detection */
  463. static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
  464. [WIIMOTE_DEV_PENDING] = "Pending",
  465. [WIIMOTE_DEV_UNKNOWN] = "Unknown",
  466. [WIIMOTE_DEV_GENERIC] = "Generic",
  467. [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
  468. [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
  469. };
  470. /* Try to guess the device type based on all collected information. We
  471. * first try to detect by static extension types, then VID/PID and the
  472. * device name. If we cannot detect the device, we use
  473. * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
  474. static void wiimote_init_set_type(struct wiimote_data *wdata,
  475. __u8 exttype)
  476. {
  477. __u8 devtype = WIIMOTE_DEV_GENERIC;
  478. __u16 vendor, product;
  479. const char *name;
  480. vendor = wdata->hdev->vendor;
  481. product = wdata->hdev->product;
  482. name = wdata->hdev->name;
  483. if (!strcmp(name, "Nintendo RVL-CNT-01")) {
  484. devtype = WIIMOTE_DEV_GEN10;
  485. goto done;
  486. } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
  487. devtype = WIIMOTE_DEV_GEN20;
  488. goto done;
  489. }
  490. if (vendor == USB_VENDOR_ID_NINTENDO) {
  491. if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
  492. devtype = WIIMOTE_DEV_GEN10;
  493. goto done;
  494. } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
  495. devtype = WIIMOTE_DEV_GEN20;
  496. goto done;
  497. }
  498. }
  499. done:
  500. if (devtype == WIIMOTE_DEV_GENERIC)
  501. hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
  502. name, vendor, product, exttype);
  503. else
  504. hid_info(wdata->hdev, "detected device: %s\n",
  505. wiimote_devtype_names[devtype]);
  506. wiimote_modules_load(wdata, devtype);
  507. }
  508. static void wiimote_init_detect(struct wiimote_data *wdata)
  509. {
  510. __u8 exttype = WIIMOTE_EXT_NONE;
  511. bool ext;
  512. int ret;
  513. wiimote_cmd_acquire_noint(wdata);
  514. spin_lock_irq(&wdata->state.lock);
  515. wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
  516. wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
  517. wiiproto_req_status(wdata);
  518. spin_unlock_irq(&wdata->state.lock);
  519. ret = wiimote_cmd_wait_noint(wdata);
  520. if (ret)
  521. goto out_release;
  522. spin_lock_irq(&wdata->state.lock);
  523. ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
  524. spin_unlock_irq(&wdata->state.lock);
  525. if (!ext)
  526. goto out_release;
  527. wiimote_cmd_init_ext(wdata);
  528. exttype = wiimote_cmd_read_ext(wdata);
  529. out_release:
  530. wiimote_cmd_release(wdata);
  531. wiimote_init_set_type(wdata, exttype);
  532. }
  533. static void wiimote_init_worker(struct work_struct *work)
  534. {
  535. struct wiimote_data *wdata = container_of(work, struct wiimote_data,
  536. init_worker);
  537. if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
  538. wiimote_init_detect(wdata);
  539. }
  540. /* protocol handlers */
  541. static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
  542. {
  543. const __u8 *iter, *mods;
  544. const struct wiimod_ops *ops;
  545. mods = wiimote_devtype_mods[wdata->state.devtype];
  546. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  547. ops = wiimod_table[*iter];
  548. if (ops->in_keys) {
  549. ops->in_keys(wdata, payload);
  550. break;
  551. }
  552. }
  553. }
  554. static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
  555. {
  556. const __u8 *iter, *mods;
  557. const struct wiimod_ops *ops;
  558. mods = wiimote_devtype_mods[wdata->state.devtype];
  559. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  560. ops = wiimod_table[*iter];
  561. if (ops->in_accel) {
  562. ops->in_accel(wdata, payload);
  563. break;
  564. }
  565. }
  566. }
  567. #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
  568. #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
  569. #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
  570. #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
  571. static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
  572. bool packed, unsigned int id)
  573. {
  574. const __u8 *iter, *mods;
  575. const struct wiimod_ops *ops;
  576. mods = wiimote_devtype_mods[wdata->state.devtype];
  577. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  578. ops = wiimod_table[*iter];
  579. if (ops->in_ir) {
  580. ops->in_ir(wdata, payload, packed, id);
  581. break;
  582. }
  583. }
  584. }
  585. /* reduced status report with "BB BB" key data only */
  586. static void handler_status_K(struct wiimote_data *wdata,
  587. const __u8 *payload)
  588. {
  589. handler_keys(wdata, payload);
  590. /* on status reports the drm is reset so we need to resend the drm */
  591. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  592. }
  593. /* extended status report with "BB BB LF 00 00 VV" data */
  594. static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
  595. {
  596. handler_status_K(wdata, payload);
  597. /* update extension status */
  598. if (payload[2] & 0x02) {
  599. wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
  600. wiiext_event(wdata, true);
  601. } else {
  602. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
  603. wiiext_event(wdata, false);
  604. }
  605. wdata->state.cmd_battery = payload[5];
  606. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
  607. wiimote_cmd_complete(wdata);
  608. }
  609. /* reduced generic report with "BB BB" key data only */
  610. static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
  611. {
  612. handler_keys(wdata, payload);
  613. }
  614. static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
  615. {
  616. __u16 offset = payload[3] << 8 | payload[4];
  617. __u8 size = (payload[2] >> 4) + 1;
  618. __u8 err = payload[2] & 0x0f;
  619. handler_keys(wdata, payload);
  620. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
  621. if (err)
  622. size = 0;
  623. else if (size > wdata->state.cmd_read_size)
  624. size = wdata->state.cmd_read_size;
  625. wdata->state.cmd_read_size = size;
  626. if (wdata->state.cmd_read_buf)
  627. memcpy(wdata->state.cmd_read_buf, &payload[5], size);
  628. wiimote_cmd_complete(wdata);
  629. }
  630. }
  631. static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
  632. {
  633. __u8 err = payload[3];
  634. __u8 cmd = payload[2];
  635. handler_keys(wdata, payload);
  636. if (wiimote_cmd_pending(wdata, cmd, 0)) {
  637. wdata->state.cmd_err = err;
  638. wiimote_cmd_complete(wdata);
  639. } else if (err) {
  640. hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
  641. cmd);
  642. }
  643. }
  644. static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
  645. {
  646. handler_keys(wdata, payload);
  647. handler_accel(wdata, payload);
  648. }
  649. static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
  650. {
  651. handler_keys(wdata, payload);
  652. wiiext_handle(wdata, &payload[2]);
  653. }
  654. static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
  655. {
  656. handler_keys(wdata, payload);
  657. handler_accel(wdata, payload);
  658. ir_to_input0(wdata, &payload[5], false);
  659. ir_to_input1(wdata, &payload[8], false);
  660. ir_to_input2(wdata, &payload[11], false);
  661. ir_to_input3(wdata, &payload[14], false);
  662. }
  663. static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
  664. {
  665. handler_keys(wdata, payload);
  666. wiiext_handle(wdata, &payload[2]);
  667. }
  668. static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
  669. {
  670. handler_keys(wdata, payload);
  671. ir_to_input0(wdata, &payload[2], false);
  672. ir_to_input1(wdata, &payload[4], true);
  673. ir_to_input2(wdata, &payload[7], false);
  674. ir_to_input3(wdata, &payload[9], true);
  675. wiiext_handle(wdata, &payload[12]);
  676. }
  677. static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
  678. {
  679. handler_keys(wdata, payload);
  680. handler_accel(wdata, payload);
  681. wiiext_handle(wdata, &payload[5]);
  682. }
  683. static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
  684. {
  685. handler_keys(wdata, payload);
  686. handler_accel(wdata, payload);
  687. ir_to_input0(wdata, &payload[5], false);
  688. ir_to_input1(wdata, &payload[7], true);
  689. ir_to_input2(wdata, &payload[10], false);
  690. ir_to_input3(wdata, &payload[12], true);
  691. wiiext_handle(wdata, &payload[15]);
  692. }
  693. static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
  694. {
  695. wiiext_handle(wdata, payload);
  696. }
  697. static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
  698. {
  699. handler_keys(wdata, payload);
  700. wdata->state.accel_split[0] = payload[2];
  701. wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
  702. wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
  703. ir_to_input0(wdata, &payload[3], false);
  704. ir_to_input1(wdata, &payload[12], false);
  705. }
  706. static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
  707. {
  708. __u8 buf[5];
  709. handler_keys(wdata, payload);
  710. wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
  711. wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
  712. buf[0] = 0;
  713. buf[1] = 0;
  714. buf[2] = wdata->state.accel_split[0];
  715. buf[3] = payload[2];
  716. buf[4] = wdata->state.accel_split[1];
  717. handler_accel(wdata, buf);
  718. ir_to_input2(wdata, &payload[3], false);
  719. ir_to_input3(wdata, &payload[12], false);
  720. }
  721. struct wiiproto_handler {
  722. __u8 id;
  723. size_t size;
  724. void (*func)(struct wiimote_data *wdata, const __u8 *payload);
  725. };
  726. static struct wiiproto_handler handlers[] = {
  727. { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
  728. { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
  729. { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
  730. { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
  731. { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
  732. { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
  733. { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
  734. { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
  735. { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
  736. { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
  737. { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
  738. { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
  739. { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
  740. { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
  741. { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
  742. { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
  743. { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
  744. { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
  745. { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
  746. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
  747. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
  748. { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
  749. { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
  750. { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
  751. { .id = 0 }
  752. };
  753. static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
  754. u8 *raw_data, int size)
  755. {
  756. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  757. struct wiiproto_handler *h;
  758. int i;
  759. unsigned long flags;
  760. if (size < 1)
  761. return -EINVAL;
  762. spin_lock_irqsave(&wdata->state.lock, flags);
  763. for (i = 0; handlers[i].id; ++i) {
  764. h = &handlers[i];
  765. if (h->id == raw_data[0] && h->size < size) {
  766. h->func(wdata, &raw_data[1]);
  767. break;
  768. }
  769. }
  770. if (!handlers[i].id)
  771. hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
  772. size);
  773. spin_unlock_irqrestore(&wdata->state.lock, flags);
  774. return 0;
  775. }
  776. static struct wiimote_data *wiimote_create(struct hid_device *hdev)
  777. {
  778. struct wiimote_data *wdata;
  779. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  780. if (!wdata)
  781. return NULL;
  782. wdata->hdev = hdev;
  783. hid_set_drvdata(hdev, wdata);
  784. spin_lock_init(&wdata->queue.lock);
  785. INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
  786. spin_lock_init(&wdata->state.lock);
  787. init_completion(&wdata->state.ready);
  788. mutex_init(&wdata->state.sync);
  789. wdata->state.drm = WIIPROTO_REQ_DRM_K;
  790. wdata->state.cmd_battery = 0xff;
  791. INIT_WORK(&wdata->init_worker, wiimote_init_worker);
  792. return wdata;
  793. }
  794. static void wiimote_destroy(struct wiimote_data *wdata)
  795. {
  796. wiidebug_deinit(wdata);
  797. wiiext_deinit(wdata);
  798. cancel_work_sync(&wdata->init_worker);
  799. wiimote_modules_unload(wdata);
  800. cancel_work_sync(&wdata->queue.worker);
  801. hid_hw_close(wdata->hdev);
  802. hid_hw_stop(wdata->hdev);
  803. kfree(wdata);
  804. }
  805. static int wiimote_hid_probe(struct hid_device *hdev,
  806. const struct hid_device_id *id)
  807. {
  808. struct wiimote_data *wdata;
  809. int ret;
  810. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  811. wdata = wiimote_create(hdev);
  812. if (!wdata) {
  813. hid_err(hdev, "Can't alloc device\n");
  814. return -ENOMEM;
  815. }
  816. ret = hid_parse(hdev);
  817. if (ret) {
  818. hid_err(hdev, "HID parse failed\n");
  819. goto err;
  820. }
  821. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  822. if (ret) {
  823. hid_err(hdev, "HW start failed\n");
  824. goto err;
  825. }
  826. ret = hid_hw_open(hdev);
  827. if (ret) {
  828. hid_err(hdev, "cannot start hardware I/O\n");
  829. goto err_stop;
  830. }
  831. ret = wiiext_init(wdata);
  832. if (ret)
  833. goto err_free;
  834. ret = wiidebug_init(wdata);
  835. if (ret)
  836. goto err_free;
  837. hid_info(hdev, "New device registered\n");
  838. /* schedule device detection */
  839. schedule_work(&wdata->init_worker);
  840. return 0;
  841. err_free:
  842. wiimote_destroy(wdata);
  843. return ret;
  844. err_stop:
  845. hid_hw_stop(hdev);
  846. err:
  847. input_free_device(wdata->ir);
  848. input_free_device(wdata->accel);
  849. kfree(wdata);
  850. return ret;
  851. }
  852. static void wiimote_hid_remove(struct hid_device *hdev)
  853. {
  854. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  855. hid_info(hdev, "Device removed\n");
  856. wiimote_destroy(wdata);
  857. }
  858. static const struct hid_device_id wiimote_hid_devices[] = {
  859. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  860. USB_DEVICE_ID_NINTENDO_WIIMOTE) },
  861. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  862. USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
  863. { }
  864. };
  865. MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
  866. static struct hid_driver wiimote_hid_driver = {
  867. .name = "wiimote",
  868. .id_table = wiimote_hid_devices,
  869. .probe = wiimote_hid_probe,
  870. .remove = wiimote_hid_remove,
  871. .raw_event = wiimote_hid_event,
  872. };
  873. module_hid_driver(wiimote_hid_driver);
  874. MODULE_LICENSE("GPL");
  875. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  876. MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");