hid-wiimote-core.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  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. * Not all combinations are actually supported. The following
  150. * combinations work only with limitations:
  151. * - IR cam in extended or full mode disables any data transmission
  152. * of extension controllers. There is no DRM mode that supports
  153. * extension bytes plus extended/full IR.
  154. * - IR cam with accelerometer and extension *_EXT8 is not supported.
  155. * However, all extensions that need *_EXT8 are devices that don't
  156. * support IR cameras. Hence, this shouldn't happen under normal
  157. * operation.
  158. * - *_EXT16 is only supported in combination with buttons and
  159. * accelerometer. No IR or similar can be active simultaneously. As
  160. * above, all modules that require it are mutually exclusive with
  161. * IR/etc. so this doesn't matter.
  162. */
  163. static __u8 select_drm(struct wiimote_data *wdata)
  164. {
  165. __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
  166. bool ext;
  167. ext = (wdata->state.flags & WIIPROTO_FLAG_EXT_USED) ||
  168. (wdata->state.flags & WIIPROTO_FLAG_MP_USED);
  169. /* some 3rd-party balance-boards are hard-coded to KEE, *sigh* */
  170. if (wdata->state.devtype == WIIMOTE_DEV_BALANCE_BOARD) {
  171. if (ext)
  172. return WIIPROTO_REQ_DRM_KEE;
  173. else
  174. return WIIPROTO_REQ_DRM_K;
  175. }
  176. if (ir == WIIPROTO_FLAG_IR_BASIC) {
  177. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
  178. if (ext)
  179. return WIIPROTO_REQ_DRM_KAIE;
  180. else
  181. return WIIPROTO_REQ_DRM_KAI;
  182. } else {
  183. return WIIPROTO_REQ_DRM_KIE;
  184. }
  185. } else if (ir == WIIPROTO_FLAG_IR_EXT) {
  186. return WIIPROTO_REQ_DRM_KAI;
  187. } else if (ir == WIIPROTO_FLAG_IR_FULL) {
  188. return WIIPROTO_REQ_DRM_SKAI1;
  189. } else {
  190. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
  191. if (ext)
  192. return WIIPROTO_REQ_DRM_KAE;
  193. else
  194. return WIIPROTO_REQ_DRM_KA;
  195. } else {
  196. if (ext)
  197. return WIIPROTO_REQ_DRM_KEE;
  198. else
  199. return WIIPROTO_REQ_DRM_K;
  200. }
  201. }
  202. }
  203. void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
  204. {
  205. __u8 cmd[3];
  206. if (drm == WIIPROTO_REQ_NULL)
  207. drm = select_drm(wdata);
  208. cmd[0] = WIIPROTO_REQ_DRM;
  209. cmd[1] = 0;
  210. cmd[2] = drm;
  211. wdata->state.drm = drm;
  212. wiiproto_keep_rumble(wdata, &cmd[1]);
  213. wiimote_queue(wdata, cmd, sizeof(cmd));
  214. }
  215. void wiiproto_req_status(struct wiimote_data *wdata)
  216. {
  217. __u8 cmd[2];
  218. cmd[0] = WIIPROTO_REQ_SREQ;
  219. cmd[1] = 0;
  220. wiiproto_keep_rumble(wdata, &cmd[1]);
  221. wiimote_queue(wdata, cmd, sizeof(cmd));
  222. }
  223. void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
  224. {
  225. accel = !!accel;
  226. if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  227. return;
  228. if (accel)
  229. wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
  230. else
  231. wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
  232. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  233. }
  234. void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
  235. {
  236. __u8 cmd[2];
  237. cmd[0] = WIIPROTO_REQ_IR1;
  238. cmd[1] = flags;
  239. wiiproto_keep_rumble(wdata, &cmd[1]);
  240. wiimote_queue(wdata, cmd, sizeof(cmd));
  241. }
  242. void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
  243. {
  244. __u8 cmd[2];
  245. cmd[0] = WIIPROTO_REQ_IR2;
  246. cmd[1] = flags;
  247. wiiproto_keep_rumble(wdata, &cmd[1]);
  248. wiimote_queue(wdata, cmd, sizeof(cmd));
  249. }
  250. #define wiiproto_req_wreg(wdata, os, buf, sz) \
  251. wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
  252. #define wiiproto_req_weeprom(wdata, os, buf, sz) \
  253. wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
  254. static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
  255. __u32 offset, const __u8 *buf, __u8 size)
  256. {
  257. __u8 cmd[22];
  258. if (size > 16 || size == 0) {
  259. hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
  260. return;
  261. }
  262. memset(cmd, 0, sizeof(cmd));
  263. cmd[0] = WIIPROTO_REQ_WMEM;
  264. cmd[2] = (offset >> 16) & 0xff;
  265. cmd[3] = (offset >> 8) & 0xff;
  266. cmd[4] = offset & 0xff;
  267. cmd[5] = size;
  268. memcpy(&cmd[6], buf, size);
  269. if (!eeprom)
  270. cmd[1] |= 0x04;
  271. wiiproto_keep_rumble(wdata, &cmd[1]);
  272. wiimote_queue(wdata, cmd, sizeof(cmd));
  273. }
  274. void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
  275. __u16 size)
  276. {
  277. __u8 cmd[7];
  278. if (size == 0) {
  279. hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
  280. return;
  281. }
  282. cmd[0] = WIIPROTO_REQ_RMEM;
  283. cmd[1] = 0;
  284. cmd[2] = (offset >> 16) & 0xff;
  285. cmd[3] = (offset >> 8) & 0xff;
  286. cmd[4] = offset & 0xff;
  287. cmd[5] = (size >> 8) & 0xff;
  288. cmd[6] = size & 0xff;
  289. if (!eeprom)
  290. cmd[1] |= 0x04;
  291. wiiproto_keep_rumble(wdata, &cmd[1]);
  292. wiimote_queue(wdata, cmd, sizeof(cmd));
  293. }
  294. /* requries the cmd-mutex to be held */
  295. int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  296. const __u8 *wmem, __u8 size)
  297. {
  298. unsigned long flags;
  299. int ret;
  300. spin_lock_irqsave(&wdata->state.lock, flags);
  301. wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
  302. wiiproto_req_wreg(wdata, offset, wmem, size);
  303. spin_unlock_irqrestore(&wdata->state.lock, flags);
  304. ret = wiimote_cmd_wait(wdata);
  305. if (!ret && wdata->state.cmd_err)
  306. ret = -EIO;
  307. return ret;
  308. }
  309. /* requries the cmd-mutex to be held */
  310. ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
  311. __u8 size)
  312. {
  313. unsigned long flags;
  314. ssize_t ret;
  315. spin_lock_irqsave(&wdata->state.lock, flags);
  316. wdata->state.cmd_read_size = size;
  317. wdata->state.cmd_read_buf = rmem;
  318. wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
  319. wiiproto_req_rreg(wdata, offset, size);
  320. spin_unlock_irqrestore(&wdata->state.lock, flags);
  321. ret = wiimote_cmd_wait(wdata);
  322. spin_lock_irqsave(&wdata->state.lock, flags);
  323. wdata->state.cmd_read_buf = NULL;
  324. spin_unlock_irqrestore(&wdata->state.lock, flags);
  325. if (!ret) {
  326. if (wdata->state.cmd_read_size == 0)
  327. ret = -EIO;
  328. else
  329. ret = wdata->state.cmd_read_size;
  330. }
  331. return ret;
  332. }
  333. /* requires the cmd-mutex to be held */
  334. static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
  335. {
  336. __u8 wmem;
  337. int ret;
  338. /* initialize extension */
  339. wmem = 0x55;
  340. ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
  341. if (ret)
  342. return ret;
  343. /* disable default encryption */
  344. wmem = 0x0;
  345. ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
  346. if (ret)
  347. return ret;
  348. return 0;
  349. }
  350. /* requires the cmd-mutex to be held */
  351. static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata, __u8 *rmem)
  352. {
  353. int ret;
  354. /* read extension ID */
  355. ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
  356. if (ret != 6)
  357. return WIIMOTE_EXT_NONE;
  358. hid_dbg(wdata->hdev, "extension ID: %02x:%02x %02x:%02x %02x:%02x\n",
  359. rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
  360. if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
  361. rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
  362. return WIIMOTE_EXT_NONE;
  363. if (rmem[4] == 0x04 && rmem[5] == 0x02)
  364. return WIIMOTE_EXT_BALANCE_BOARD;
  365. return WIIMOTE_EXT_UNKNOWN;
  366. }
  367. /* requires the cmd-mutex to be held */
  368. static int wiimote_cmd_init_mp(struct wiimote_data *wdata)
  369. {
  370. __u8 wmem;
  371. int ret;
  372. /* initialize MP */
  373. wmem = 0x55;
  374. ret = wiimote_cmd_write(wdata, 0xa600f0, &wmem, sizeof(wmem));
  375. if (ret)
  376. return ret;
  377. /* disable default encryption */
  378. wmem = 0x0;
  379. ret = wiimote_cmd_write(wdata, 0xa600fb, &wmem, sizeof(wmem));
  380. if (ret)
  381. return ret;
  382. return 0;
  383. }
  384. /* requires the cmd-mutex to be held */
  385. static bool wiimote_cmd_map_mp(struct wiimote_data *wdata, __u8 exttype)
  386. {
  387. __u8 wmem;
  388. /* map MP with correct pass-through mode */
  389. switch (exttype) {
  390. default:
  391. wmem = 0x04;
  392. break;
  393. }
  394. return wiimote_cmd_write(wdata, 0xa600fe, &wmem, sizeof(wmem));
  395. }
  396. /* requires the cmd-mutex to be held */
  397. static bool wiimote_cmd_read_mp(struct wiimote_data *wdata, __u8 *rmem)
  398. {
  399. int ret;
  400. /* read motion plus ID */
  401. ret = wiimote_cmd_read(wdata, 0xa600fa, rmem, 6);
  402. if (ret != 6)
  403. return false;
  404. hid_dbg(wdata->hdev, "motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
  405. rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
  406. if (rmem[5] == 0x05)
  407. return true;
  408. hid_info(wdata->hdev, "unknown motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
  409. rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
  410. return false;
  411. }
  412. /* requires the cmd-mutex to be held */
  413. static __u8 wiimote_cmd_read_mp_mapped(struct wiimote_data *wdata)
  414. {
  415. int ret;
  416. __u8 rmem[6];
  417. /* read motion plus ID */
  418. ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
  419. if (ret != 6)
  420. return WIIMOTE_MP_NONE;
  421. hid_dbg(wdata->hdev, "mapped motion plus ID: %02x:%02x %02x:%02x %02x:%02x\n",
  422. rmem[0], rmem[1], rmem[2], rmem[3], rmem[4], rmem[5]);
  423. if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
  424. rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
  425. return WIIMOTE_MP_NONE;
  426. if (rmem[4] == 0x04 && rmem[5] == 0x05)
  427. return WIIMOTE_MP_SINGLE;
  428. else if (rmem[4] == 0x05 && rmem[5] == 0x05)
  429. return WIIMOTE_MP_PASSTHROUGH_NUNCHUK;
  430. else if (rmem[4] == 0x07 && rmem[5] == 0x05)
  431. return WIIMOTE_MP_PASSTHROUGH_CLASSIC;
  432. return WIIMOTE_MP_UNKNOWN;
  433. }
  434. /* device module handling */
  435. static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
  436. [WIIMOTE_DEV_PENDING] = (const __u8[]){
  437. WIIMOD_NULL,
  438. },
  439. [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
  440. WIIMOD_NULL,
  441. },
  442. [WIIMOTE_DEV_GENERIC] = (const __u8[]){
  443. WIIMOD_KEYS,
  444. WIIMOD_RUMBLE,
  445. WIIMOD_BATTERY,
  446. WIIMOD_LED1,
  447. WIIMOD_LED2,
  448. WIIMOD_LED3,
  449. WIIMOD_LED4,
  450. WIIMOD_ACCEL,
  451. WIIMOD_IR,
  452. WIIMOD_NULL,
  453. },
  454. [WIIMOTE_DEV_GEN10] = (const __u8[]){
  455. WIIMOD_KEYS,
  456. WIIMOD_RUMBLE,
  457. WIIMOD_BATTERY,
  458. WIIMOD_LED1,
  459. WIIMOD_LED2,
  460. WIIMOD_LED3,
  461. WIIMOD_LED4,
  462. WIIMOD_ACCEL,
  463. WIIMOD_IR,
  464. WIIMOD_NULL,
  465. },
  466. [WIIMOTE_DEV_GEN20] = (const __u8[]){
  467. WIIMOD_KEYS,
  468. WIIMOD_RUMBLE,
  469. WIIMOD_BATTERY,
  470. WIIMOD_LED1,
  471. WIIMOD_LED2,
  472. WIIMOD_LED3,
  473. WIIMOD_LED4,
  474. WIIMOD_ACCEL,
  475. WIIMOD_IR,
  476. WIIMOD_NULL,
  477. },
  478. [WIIMOTE_DEV_BALANCE_BOARD] = (const __u8[]) {
  479. WIIMOD_BATTERY,
  480. WIIMOD_LED1,
  481. WIIMOD_NULL,
  482. },
  483. };
  484. static void wiimote_modules_load(struct wiimote_data *wdata,
  485. unsigned int devtype)
  486. {
  487. bool need_input = false;
  488. const __u8 *mods, *iter;
  489. const struct wiimod_ops *ops;
  490. int ret;
  491. mods = wiimote_devtype_mods[devtype];
  492. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  493. if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
  494. need_input = true;
  495. break;
  496. }
  497. }
  498. if (need_input) {
  499. wdata->input = input_allocate_device();
  500. if (!wdata->input)
  501. return;
  502. input_set_drvdata(wdata->input, wdata);
  503. wdata->input->dev.parent = &wdata->hdev->dev;
  504. wdata->input->id.bustype = wdata->hdev->bus;
  505. wdata->input->id.vendor = wdata->hdev->vendor;
  506. wdata->input->id.product = wdata->hdev->product;
  507. wdata->input->id.version = wdata->hdev->version;
  508. wdata->input->name = WIIMOTE_NAME;
  509. }
  510. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  511. ops = wiimod_table[*iter];
  512. if (!ops->probe)
  513. continue;
  514. ret = ops->probe(ops, wdata);
  515. if (ret)
  516. goto error;
  517. }
  518. if (wdata->input) {
  519. ret = input_register_device(wdata->input);
  520. if (ret)
  521. goto error;
  522. }
  523. spin_lock_irq(&wdata->state.lock);
  524. wdata->state.devtype = devtype;
  525. spin_unlock_irq(&wdata->state.lock);
  526. return;
  527. error:
  528. for ( ; iter-- != mods; ) {
  529. ops = wiimod_table[*iter];
  530. if (ops->remove)
  531. ops->remove(ops, wdata);
  532. }
  533. if (wdata->input) {
  534. input_free_device(wdata->input);
  535. wdata->input = NULL;
  536. }
  537. }
  538. static void wiimote_modules_unload(struct wiimote_data *wdata)
  539. {
  540. const __u8 *mods, *iter;
  541. const struct wiimod_ops *ops;
  542. unsigned long flags;
  543. mods = wiimote_devtype_mods[wdata->state.devtype];
  544. spin_lock_irqsave(&wdata->state.lock, flags);
  545. wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
  546. spin_unlock_irqrestore(&wdata->state.lock, flags);
  547. /* find end of list */
  548. for (iter = mods; *iter != WIIMOD_NULL; ++iter)
  549. /* empty */ ;
  550. if (wdata->input) {
  551. input_get_device(wdata->input);
  552. input_unregister_device(wdata->input);
  553. }
  554. for ( ; iter-- != mods; ) {
  555. ops = wiimod_table[*iter];
  556. if (ops->remove)
  557. ops->remove(ops, wdata);
  558. }
  559. if (wdata->input) {
  560. input_put_device(wdata->input);
  561. wdata->input = NULL;
  562. }
  563. }
  564. /* device extension handling */
  565. static void wiimote_ext_load(struct wiimote_data *wdata, unsigned int ext)
  566. {
  567. unsigned long flags;
  568. const struct wiimod_ops *ops;
  569. int ret;
  570. ops = wiimod_ext_table[ext];
  571. if (ops->probe) {
  572. ret = ops->probe(ops, wdata);
  573. if (ret)
  574. ext = WIIMOTE_EXT_UNKNOWN;
  575. }
  576. spin_lock_irqsave(&wdata->state.lock, flags);
  577. wdata->state.exttype = ext;
  578. spin_unlock_irqrestore(&wdata->state.lock, flags);
  579. }
  580. static void wiimote_ext_unload(struct wiimote_data *wdata)
  581. {
  582. unsigned long flags;
  583. const struct wiimod_ops *ops;
  584. ops = wiimod_ext_table[wdata->state.exttype];
  585. spin_lock_irqsave(&wdata->state.lock, flags);
  586. wdata->state.exttype = WIIMOTE_EXT_UNKNOWN;
  587. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  588. spin_unlock_irqrestore(&wdata->state.lock, flags);
  589. if (ops->remove)
  590. ops->remove(ops, wdata);
  591. }
  592. static void wiimote_mp_load(struct wiimote_data *wdata)
  593. {
  594. unsigned long flags;
  595. const struct wiimod_ops *ops;
  596. int ret;
  597. __u8 mode = 2;
  598. ops = &wiimod_mp;
  599. if (ops->probe) {
  600. ret = ops->probe(ops, wdata);
  601. if (ret)
  602. mode = 1;
  603. }
  604. spin_lock_irqsave(&wdata->state.lock, flags);
  605. wdata->state.mp = mode;
  606. spin_unlock_irqrestore(&wdata->state.lock, flags);
  607. }
  608. static void wiimote_mp_unload(struct wiimote_data *wdata)
  609. {
  610. unsigned long flags;
  611. const struct wiimod_ops *ops;
  612. if (wdata->state.mp < 2)
  613. return;
  614. ops = &wiimod_mp;
  615. spin_lock_irqsave(&wdata->state.lock, flags);
  616. wdata->state.mp = 0;
  617. wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
  618. spin_unlock_irqrestore(&wdata->state.lock, flags);
  619. if (ops->remove)
  620. ops->remove(ops, wdata);
  621. }
  622. /* device (re-)initialization and detection */
  623. static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
  624. [WIIMOTE_DEV_PENDING] = "Pending",
  625. [WIIMOTE_DEV_UNKNOWN] = "Unknown",
  626. [WIIMOTE_DEV_GENERIC] = "Generic",
  627. [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
  628. [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
  629. [WIIMOTE_DEV_BALANCE_BOARD] = "Nintendo Wii Balance Board",
  630. };
  631. /* Try to guess the device type based on all collected information. We
  632. * first try to detect by static extension types, then VID/PID and the
  633. * device name. If we cannot detect the device, we use
  634. * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
  635. static void wiimote_init_set_type(struct wiimote_data *wdata,
  636. __u8 exttype)
  637. {
  638. __u8 devtype = WIIMOTE_DEV_GENERIC;
  639. __u16 vendor, product;
  640. const char *name;
  641. vendor = wdata->hdev->vendor;
  642. product = wdata->hdev->product;
  643. name = wdata->hdev->name;
  644. if (exttype == WIIMOTE_EXT_BALANCE_BOARD) {
  645. devtype = WIIMOTE_DEV_BALANCE_BOARD;
  646. goto done;
  647. }
  648. if (!strcmp(name, "Nintendo RVL-CNT-01")) {
  649. devtype = WIIMOTE_DEV_GEN10;
  650. goto done;
  651. } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
  652. devtype = WIIMOTE_DEV_GEN20;
  653. goto done;
  654. } else if (!strcmp(name, "Nintendo RVL-WBC-01")) {
  655. devtype = WIIMOTE_DEV_BALANCE_BOARD;
  656. goto done;
  657. }
  658. if (vendor == USB_VENDOR_ID_NINTENDO) {
  659. if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
  660. devtype = WIIMOTE_DEV_GEN10;
  661. goto done;
  662. } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
  663. devtype = WIIMOTE_DEV_GEN20;
  664. goto done;
  665. }
  666. }
  667. done:
  668. if (devtype == WIIMOTE_DEV_GENERIC)
  669. hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
  670. name, vendor, product, exttype);
  671. else
  672. hid_info(wdata->hdev, "detected device: %s\n",
  673. wiimote_devtype_names[devtype]);
  674. wiimote_modules_load(wdata, devtype);
  675. }
  676. static void wiimote_init_detect(struct wiimote_data *wdata)
  677. {
  678. __u8 exttype = WIIMOTE_EXT_NONE, extdata[6];
  679. bool ext;
  680. int ret;
  681. wiimote_cmd_acquire_noint(wdata);
  682. spin_lock_irq(&wdata->state.lock);
  683. wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
  684. wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
  685. wiiproto_req_status(wdata);
  686. spin_unlock_irq(&wdata->state.lock);
  687. ret = wiimote_cmd_wait_noint(wdata);
  688. if (ret)
  689. goto out_release;
  690. spin_lock_irq(&wdata->state.lock);
  691. ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
  692. spin_unlock_irq(&wdata->state.lock);
  693. if (!ext)
  694. goto out_release;
  695. wiimote_cmd_init_ext(wdata);
  696. exttype = wiimote_cmd_read_ext(wdata, extdata);
  697. out_release:
  698. wiimote_cmd_release(wdata);
  699. wiimote_init_set_type(wdata, exttype);
  700. /* schedule MP timer */
  701. mod_timer(&wdata->timer, jiffies + HZ * 4);
  702. }
  703. /*
  704. * MP hotplug events are not generated by the wiimote. Therefore, we need
  705. * polling to detect it. We use a 4s interval for polling MP registers. This
  706. * seems reasonable considering applications can trigger it manually via
  707. * sysfs requests.
  708. */
  709. static void wiimote_init_poll_mp(struct wiimote_data *wdata)
  710. {
  711. bool mp;
  712. __u8 mpdata[6];
  713. wiimote_cmd_acquire_noint(wdata);
  714. wiimote_cmd_init_mp(wdata);
  715. mp = wiimote_cmd_read_mp(wdata, mpdata);
  716. wiimote_cmd_release(wdata);
  717. /* load/unload MP module if it changed */
  718. if (mp) {
  719. if (!wdata->state.mp) {
  720. hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
  721. wiimote_mp_load(wdata);
  722. }
  723. } else if (wdata->state.mp) {
  724. wiimote_mp_unload(wdata);
  725. }
  726. mod_timer(&wdata->timer, jiffies + HZ * 4);
  727. }
  728. /*
  729. * Check whether the wiimote is in the expected state. The extension registers
  730. * may change during hotplug and initialization so we might get hotplug events
  731. * that we caused by remapping some memory.
  732. * We use some heuristics here to check known states. If the wiimote is in the
  733. * expected state, we can ignore the hotplug event.
  734. *
  735. * Returns "true" if the device is in expected state, "false" if we should
  736. * redo hotplug handling and extension initialization.
  737. */
  738. static bool wiimote_init_check(struct wiimote_data *wdata)
  739. {
  740. __u32 flags;
  741. __u8 type, data[6];
  742. bool ret, poll_mp;
  743. spin_lock_irq(&wdata->state.lock);
  744. flags = wdata->state.flags;
  745. spin_unlock_irq(&wdata->state.lock);
  746. wiimote_cmd_acquire_noint(wdata);
  747. /* If MP is used and active, but the extension is not, we expect:
  748. * read_mp_mapped() == WIIMOTE_MP_SINGLE
  749. * state.flags == !EXT_ACTIVE && !MP_PLUGGED && MP_ACTIVE
  750. * We do not check EXT_PLUGGED because it might change during
  751. * initialization of MP without extensions.
  752. * - If MP is unplugged/replugged, read_mp_mapped() fails
  753. * - If EXT is plugged, MP_PLUGGED will get set */
  754. if (wdata->state.exttype == WIIMOTE_EXT_NONE &&
  755. wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
  756. type = wiimote_cmd_read_mp_mapped(wdata);
  757. ret = type == WIIMOTE_MP_SINGLE;
  758. spin_lock_irq(&wdata->state.lock);
  759. ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
  760. ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED);
  761. ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
  762. spin_unlock_irq(&wdata->state.lock);
  763. if (!ret)
  764. hid_dbg(wdata->hdev, "state left: !EXT && MP\n");
  765. /* while MP is mapped, we get EXT_PLUGGED events */
  766. poll_mp = false;
  767. goto out_release;
  768. }
  769. /* If MP is unused, but the extension port is used, we expect:
  770. * read_ext == state.exttype
  771. * state.flags == !MP_ACTIVE && EXT_ACTIVE
  772. * - If MP is plugged/unplugged, our timer detects it
  773. * - If EXT is unplugged/replugged, EXT_ACTIVE will become unset */
  774. if (!(flags & WIIPROTO_FLAG_MP_USED) &&
  775. wdata->state.exttype != WIIMOTE_EXT_NONE) {
  776. type = wiimote_cmd_read_ext(wdata, data);
  777. ret = type == wdata->state.exttype;
  778. spin_lock_irq(&wdata->state.lock);
  779. ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
  780. ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
  781. spin_unlock_irq(&wdata->state.lock);
  782. if (!ret)
  783. hid_dbg(wdata->hdev, "state left: EXT && !MP\n");
  784. /* poll MP for hotplug events */
  785. poll_mp = true;
  786. goto out_release;
  787. }
  788. /* If neither MP nor an extension are used, we expect:
  789. * read_ext() == WIIMOTE_EXT_NONE
  790. * state.flags == !MP_ACTIVE && !EXT_ACTIVE && !EXT_PLUGGED
  791. * No need to perform any action in this case as everything is
  792. * disabled already.
  793. * - If MP is plugged/unplugged, our timer detects it
  794. * - If EXT is plugged, EXT_PLUGGED will be set */
  795. if (!(flags & WIIPROTO_FLAG_MP_USED) &&
  796. wdata->state.exttype == WIIMOTE_EXT_NONE) {
  797. type = wiimote_cmd_read_ext(wdata, data);
  798. ret = type == wdata->state.exttype;
  799. spin_lock_irq(&wdata->state.lock);
  800. ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
  801. ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
  802. ret = ret && !(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
  803. spin_unlock_irq(&wdata->state.lock);
  804. if (!ret)
  805. hid_dbg(wdata->hdev, "state left: !EXT && !MP\n");
  806. /* poll MP for hotplug events */
  807. poll_mp = true;
  808. goto out_release;
  809. }
  810. /* The trickiest part is if both EXT and MP are active. We cannot read
  811. * the EXT ID, anymore, because MP is mapped over it. However, we use
  812. * a handy trick here:
  813. * - EXT_ACTIVE is unset whenever !MP_PLUGGED is sent
  814. * MP_PLUGGED might be re-sent again before we are scheduled, but
  815. * EXT_ACTIVE will stay unset.
  816. * So it is enough to check for mp_mapped() and MP_ACTIVE and
  817. * EXT_ACTIVE. EXT_PLUGGED is a sanity check. */
  818. if (wdata->state.exttype != WIIMOTE_EXT_NONE &&
  819. wdata->state.mp > 0 && (flags & WIIPROTO_FLAG_MP_USED)) {
  820. type = wiimote_cmd_read_mp_mapped(wdata);
  821. ret = type != WIIMOTE_MP_NONE;
  822. ret = ret && type != WIIMOTE_MP_UNKNOWN;
  823. ret = ret && type != WIIMOTE_MP_SINGLE;
  824. spin_lock_irq(&wdata->state.lock);
  825. ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED);
  826. ret = ret && (wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE);
  827. ret = ret && (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE);
  828. spin_unlock_irq(&wdata->state.lock);
  829. if (!ret)
  830. hid_dbg(wdata->hdev, "state left: EXT && MP\n");
  831. /* while MP is mapped, we get EXT_PLUGGED events */
  832. poll_mp = false;
  833. goto out_release;
  834. }
  835. /* unknown state */
  836. ret = false;
  837. out_release:
  838. wiimote_cmd_release(wdata);
  839. /* only poll for MP if requested and if state didn't change */
  840. if (ret && poll_mp)
  841. wiimote_init_poll_mp(wdata);
  842. return ret;
  843. }
  844. static const char *wiimote_exttype_names[WIIMOTE_EXT_NUM] = {
  845. [WIIMOTE_EXT_NONE] = "None",
  846. [WIIMOTE_EXT_UNKNOWN] = "Unknown",
  847. [WIIMOTE_EXT_BALANCE_BOARD] = "Nintendo Wii Balance Board",
  848. };
  849. /*
  850. * Handle hotplug events
  851. * If we receive an hotplug event and the device-check failed, we deinitialize
  852. * the extension ports, re-read all extension IDs and set the device into
  853. * the desired state. This involves mapping MP into the main extension
  854. * registers, setting up extension passthrough modes and initializing the
  855. * requested extensions.
  856. */
  857. static void wiimote_init_hotplug(struct wiimote_data *wdata)
  858. {
  859. __u8 exttype, extdata[6], mpdata[6];
  860. __u32 flags;
  861. bool mp;
  862. hid_dbg(wdata->hdev, "detect extensions..\n");
  863. wiimote_cmd_acquire_noint(wdata);
  864. spin_lock_irq(&wdata->state.lock);
  865. /* get state snapshot that we will then work on */
  866. flags = wdata->state.flags;
  867. /* disable event forwarding temporarily */
  868. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
  869. wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
  870. spin_unlock_irq(&wdata->state.lock);
  871. /* init extension and MP (deactivates current extension or MP) */
  872. wiimote_cmd_init_ext(wdata);
  873. wiimote_cmd_init_mp(wdata);
  874. mp = wiimote_cmd_read_mp(wdata, mpdata);
  875. exttype = wiimote_cmd_read_ext(wdata, extdata);
  876. wiimote_cmd_release(wdata);
  877. /* load/unload extension module if it changed */
  878. if (exttype != wdata->state.exttype) {
  879. /* unload previous extension */
  880. wiimote_ext_unload(wdata);
  881. if (exttype == WIIMOTE_EXT_UNKNOWN) {
  882. hid_info(wdata->hdev, "cannot detect extension; %02x:%02x %02x:%02x %02x:%02x\n",
  883. extdata[0], extdata[1], extdata[2],
  884. extdata[3], extdata[4], extdata[5]);
  885. } else if (exttype == WIIMOTE_EXT_NONE) {
  886. spin_lock_irq(&wdata->state.lock);
  887. wdata->state.exttype = WIIMOTE_EXT_NONE;
  888. spin_unlock_irq(&wdata->state.lock);
  889. } else {
  890. hid_info(wdata->hdev, "detected extension: %s\n",
  891. wiimote_exttype_names[exttype]);
  892. /* try loading new extension */
  893. wiimote_ext_load(wdata, exttype);
  894. }
  895. }
  896. /* load/unload MP module if it changed */
  897. if (mp) {
  898. if (!wdata->state.mp) {
  899. hid_info(wdata->hdev, "detected extension: Nintendo Wii Motion Plus\n");
  900. wiimote_mp_load(wdata);
  901. }
  902. } else if (wdata->state.mp) {
  903. wiimote_mp_unload(wdata);
  904. }
  905. /* if MP is not used, do not map or activate it */
  906. if (!(flags & WIIPROTO_FLAG_MP_USED))
  907. mp = false;
  908. /* map MP into main extension registers if used */
  909. if (mp) {
  910. wiimote_cmd_acquire_noint(wdata);
  911. wiimote_cmd_map_mp(wdata, exttype);
  912. wiimote_cmd_release(wdata);
  913. /* delete MP hotplug timer */
  914. del_timer_sync(&wdata->timer);
  915. } else {
  916. /* reschedule MP hotplug timer */
  917. mod_timer(&wdata->timer, jiffies + HZ * 4);
  918. }
  919. spin_lock_irq(&wdata->state.lock);
  920. /* enable data forwarding again and set expected hotplug state */
  921. if (mp) {
  922. wdata->state.flags |= WIIPROTO_FLAG_MP_ACTIVE;
  923. if (wdata->state.exttype == WIIMOTE_EXT_NONE) {
  924. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
  925. wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
  926. } else {
  927. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
  928. wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
  929. wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
  930. }
  931. } else if (wdata->state.exttype != WIIMOTE_EXT_NONE) {
  932. wdata->state.flags |= WIIPROTO_FLAG_EXT_ACTIVE;
  933. }
  934. /* request status report for hotplug state updates */
  935. wiiproto_req_status(wdata);
  936. spin_unlock_irq(&wdata->state.lock);
  937. hid_dbg(wdata->hdev, "detected extensions: MP: %d EXT: %d\n",
  938. wdata->state.mp, wdata->state.exttype);
  939. }
  940. static void wiimote_init_worker(struct work_struct *work)
  941. {
  942. struct wiimote_data *wdata = container_of(work, struct wiimote_data,
  943. init_worker);
  944. if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
  945. wiimote_init_detect(wdata);
  946. if (!wiimote_init_check(wdata))
  947. wiimote_init_hotplug(wdata);
  948. }
  949. void __wiimote_schedule(struct wiimote_data *wdata)
  950. {
  951. if (!(wdata->state.flags & WIIPROTO_FLAG_EXITING))
  952. schedule_work(&wdata->init_worker);
  953. }
  954. static void wiimote_schedule(struct wiimote_data *wdata)
  955. {
  956. unsigned long flags;
  957. spin_lock_irqsave(&wdata->state.lock, flags);
  958. __wiimote_schedule(wdata);
  959. spin_unlock_irqrestore(&wdata->state.lock, flags);
  960. }
  961. static void wiimote_init_timeout(unsigned long arg)
  962. {
  963. struct wiimote_data *wdata = (void*)arg;
  964. wiimote_schedule(wdata);
  965. }
  966. /* protocol handlers */
  967. static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
  968. {
  969. const __u8 *iter, *mods;
  970. const struct wiimod_ops *ops;
  971. ops = wiimod_ext_table[wdata->state.exttype];
  972. if (ops->in_keys) {
  973. ops->in_keys(wdata, payload);
  974. return;
  975. }
  976. mods = wiimote_devtype_mods[wdata->state.devtype];
  977. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  978. ops = wiimod_table[*iter];
  979. if (ops->in_keys) {
  980. ops->in_keys(wdata, payload);
  981. break;
  982. }
  983. }
  984. }
  985. static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
  986. {
  987. const __u8 *iter, *mods;
  988. const struct wiimod_ops *ops;
  989. ops = wiimod_ext_table[wdata->state.exttype];
  990. if (ops->in_accel) {
  991. ops->in_accel(wdata, payload);
  992. return;
  993. }
  994. mods = wiimote_devtype_mods[wdata->state.devtype];
  995. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  996. ops = wiimod_table[*iter];
  997. if (ops->in_accel) {
  998. ops->in_accel(wdata, payload);
  999. break;
  1000. }
  1001. }
  1002. }
  1003. static bool valid_ext_handler(const struct wiimod_ops *ops, size_t len)
  1004. {
  1005. if (!ops->in_ext)
  1006. return false;
  1007. if ((ops->flags & WIIMOD_FLAG_EXT8) && len < 8)
  1008. return false;
  1009. if ((ops->flags & WIIMOD_FLAG_EXT16) && len < 16)
  1010. return false;
  1011. return true;
  1012. }
  1013. static void handler_ext(struct wiimote_data *wdata, const __u8 *payload,
  1014. size_t len)
  1015. {
  1016. const __u8 *iter, *mods;
  1017. const struct wiimod_ops *ops;
  1018. bool is_mp;
  1019. if (len < 6)
  1020. return;
  1021. /* if MP is active, track MP slot hotplugging */
  1022. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  1023. /* this bit is set for invalid events (eg. during hotplug) */
  1024. if (payload[5] & 0x01)
  1025. return;
  1026. if (payload[4] & 0x01) {
  1027. if (!(wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED)) {
  1028. hid_dbg(wdata->hdev, "MP hotplug: 1\n");
  1029. wdata->state.flags |= WIIPROTO_FLAG_MP_PLUGGED;
  1030. __wiimote_schedule(wdata);
  1031. }
  1032. } else {
  1033. if (wdata->state.flags & WIIPROTO_FLAG_MP_PLUGGED) {
  1034. hid_dbg(wdata->hdev, "MP hotplug: 0\n");
  1035. wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
  1036. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
  1037. __wiimote_schedule(wdata);
  1038. }
  1039. }
  1040. /* detect MP data that is sent interleaved with EXT data */
  1041. is_mp = payload[5] & 0x02;
  1042. } else {
  1043. is_mp = false;
  1044. }
  1045. /* ignore EXT events if no extension is active */
  1046. if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_ACTIVE) && !is_mp)
  1047. return;
  1048. /* try forwarding to extension handler, first */
  1049. ops = wiimod_ext_table[wdata->state.exttype];
  1050. if (is_mp && ops->in_mp) {
  1051. ops->in_mp(wdata, payload);
  1052. return;
  1053. } else if (!is_mp && valid_ext_handler(ops, len)) {
  1054. ops->in_ext(wdata, payload);
  1055. return;
  1056. }
  1057. /* try forwarding to MP handler */
  1058. ops = &wiimod_mp;
  1059. if (is_mp && ops->in_mp) {
  1060. ops->in_mp(wdata, payload);
  1061. return;
  1062. } else if (!is_mp && valid_ext_handler(ops, len)) {
  1063. ops->in_ext(wdata, payload);
  1064. return;
  1065. }
  1066. /* try forwarding to loaded modules */
  1067. mods = wiimote_devtype_mods[wdata->state.devtype];
  1068. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  1069. ops = wiimod_table[*iter];
  1070. if (is_mp && ops->in_mp) {
  1071. ops->in_mp(wdata, payload);
  1072. return;
  1073. } else if (!is_mp && valid_ext_handler(ops, len)) {
  1074. ops->in_ext(wdata, payload);
  1075. return;
  1076. }
  1077. }
  1078. }
  1079. #define ir_to_input0(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 0)
  1080. #define ir_to_input1(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 1)
  1081. #define ir_to_input2(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 2)
  1082. #define ir_to_input3(wdata, ir, packed) handler_ir((wdata), (ir), (packed), 3)
  1083. static void handler_ir(struct wiimote_data *wdata, const __u8 *payload,
  1084. bool packed, unsigned int id)
  1085. {
  1086. const __u8 *iter, *mods;
  1087. const struct wiimod_ops *ops;
  1088. ops = wiimod_ext_table[wdata->state.exttype];
  1089. if (ops->in_ir) {
  1090. ops->in_ir(wdata, payload, packed, id);
  1091. return;
  1092. }
  1093. mods = wiimote_devtype_mods[wdata->state.devtype];
  1094. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  1095. ops = wiimod_table[*iter];
  1096. if (ops->in_ir) {
  1097. ops->in_ir(wdata, payload, packed, id);
  1098. break;
  1099. }
  1100. }
  1101. }
  1102. /* reduced status report with "BB BB" key data only */
  1103. static void handler_status_K(struct wiimote_data *wdata,
  1104. const __u8 *payload)
  1105. {
  1106. handler_keys(wdata, payload);
  1107. /* on status reports the drm is reset so we need to resend the drm */
  1108. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1109. }
  1110. /* extended status report with "BB BB LF 00 00 VV" data */
  1111. static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
  1112. {
  1113. handler_status_K(wdata, payload);
  1114. /* update extension status */
  1115. if (payload[2] & 0x02) {
  1116. if (!(wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED)) {
  1117. hid_dbg(wdata->hdev, "EXT hotplug: 1\n");
  1118. wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
  1119. __wiimote_schedule(wdata);
  1120. }
  1121. } else {
  1122. if (wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED) {
  1123. hid_dbg(wdata->hdev, "EXT hotplug: 0\n");
  1124. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
  1125. wdata->state.flags &= ~WIIPROTO_FLAG_MP_PLUGGED;
  1126. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_ACTIVE;
  1127. wdata->state.flags &= ~WIIPROTO_FLAG_MP_ACTIVE;
  1128. __wiimote_schedule(wdata);
  1129. }
  1130. }
  1131. wdata->state.cmd_battery = payload[5];
  1132. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
  1133. wiimote_cmd_complete(wdata);
  1134. }
  1135. /* reduced generic report with "BB BB" key data only */
  1136. static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
  1137. {
  1138. handler_keys(wdata, payload);
  1139. }
  1140. static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
  1141. {
  1142. __u16 offset = payload[3] << 8 | payload[4];
  1143. __u8 size = (payload[2] >> 4) + 1;
  1144. __u8 err = payload[2] & 0x0f;
  1145. handler_keys(wdata, payload);
  1146. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
  1147. if (err)
  1148. size = 0;
  1149. else if (size > wdata->state.cmd_read_size)
  1150. size = wdata->state.cmd_read_size;
  1151. wdata->state.cmd_read_size = size;
  1152. if (wdata->state.cmd_read_buf)
  1153. memcpy(wdata->state.cmd_read_buf, &payload[5], size);
  1154. wiimote_cmd_complete(wdata);
  1155. }
  1156. }
  1157. static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
  1158. {
  1159. __u8 err = payload[3];
  1160. __u8 cmd = payload[2];
  1161. handler_keys(wdata, payload);
  1162. if (wiimote_cmd_pending(wdata, cmd, 0)) {
  1163. wdata->state.cmd_err = err;
  1164. wiimote_cmd_complete(wdata);
  1165. } else if (err) {
  1166. hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
  1167. cmd);
  1168. }
  1169. }
  1170. static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
  1171. {
  1172. handler_keys(wdata, payload);
  1173. handler_accel(wdata, payload);
  1174. }
  1175. static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
  1176. {
  1177. handler_keys(wdata, payload);
  1178. handler_ext(wdata, &payload[2], 8);
  1179. }
  1180. static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
  1181. {
  1182. handler_keys(wdata, payload);
  1183. handler_accel(wdata, payload);
  1184. ir_to_input0(wdata, &payload[5], false);
  1185. ir_to_input1(wdata, &payload[8], false);
  1186. ir_to_input2(wdata, &payload[11], false);
  1187. ir_to_input3(wdata, &payload[14], false);
  1188. }
  1189. static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
  1190. {
  1191. handler_keys(wdata, payload);
  1192. handler_ext(wdata, &payload[2], 19);
  1193. }
  1194. static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
  1195. {
  1196. handler_keys(wdata, payload);
  1197. ir_to_input0(wdata, &payload[2], false);
  1198. ir_to_input1(wdata, &payload[4], true);
  1199. ir_to_input2(wdata, &payload[7], false);
  1200. ir_to_input3(wdata, &payload[9], true);
  1201. handler_ext(wdata, &payload[12], 9);
  1202. }
  1203. static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
  1204. {
  1205. handler_keys(wdata, payload);
  1206. handler_accel(wdata, payload);
  1207. handler_ext(wdata, &payload[5], 16);
  1208. }
  1209. static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
  1210. {
  1211. handler_keys(wdata, payload);
  1212. handler_accel(wdata, payload);
  1213. ir_to_input0(wdata, &payload[5], false);
  1214. ir_to_input1(wdata, &payload[7], true);
  1215. ir_to_input2(wdata, &payload[10], false);
  1216. ir_to_input3(wdata, &payload[12], true);
  1217. handler_ext(wdata, &payload[15], 6);
  1218. }
  1219. static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
  1220. {
  1221. handler_ext(wdata, payload, 21);
  1222. }
  1223. static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
  1224. {
  1225. handler_keys(wdata, payload);
  1226. wdata->state.accel_split[0] = payload[2];
  1227. wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
  1228. wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
  1229. ir_to_input0(wdata, &payload[3], false);
  1230. ir_to_input1(wdata, &payload[12], false);
  1231. }
  1232. static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
  1233. {
  1234. __u8 buf[5];
  1235. handler_keys(wdata, payload);
  1236. wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
  1237. wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
  1238. buf[0] = 0;
  1239. buf[1] = 0;
  1240. buf[2] = wdata->state.accel_split[0];
  1241. buf[3] = payload[2];
  1242. buf[4] = wdata->state.accel_split[1];
  1243. handler_accel(wdata, buf);
  1244. ir_to_input2(wdata, &payload[3], false);
  1245. ir_to_input3(wdata, &payload[12], false);
  1246. }
  1247. struct wiiproto_handler {
  1248. __u8 id;
  1249. size_t size;
  1250. void (*func)(struct wiimote_data *wdata, const __u8 *payload);
  1251. };
  1252. static struct wiiproto_handler handlers[] = {
  1253. { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
  1254. { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
  1255. { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
  1256. { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
  1257. { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
  1258. { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
  1259. { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
  1260. { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
  1261. { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
  1262. { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
  1263. { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
  1264. { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
  1265. { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
  1266. { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
  1267. { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
  1268. { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
  1269. { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
  1270. { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
  1271. { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
  1272. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
  1273. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
  1274. { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
  1275. { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
  1276. { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
  1277. { .id = 0 }
  1278. };
  1279. static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
  1280. u8 *raw_data, int size)
  1281. {
  1282. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  1283. struct wiiproto_handler *h;
  1284. int i;
  1285. unsigned long flags;
  1286. if (size < 1)
  1287. return -EINVAL;
  1288. spin_lock_irqsave(&wdata->state.lock, flags);
  1289. for (i = 0; handlers[i].id; ++i) {
  1290. h = &handlers[i];
  1291. if (h->id == raw_data[0] && h->size < size) {
  1292. h->func(wdata, &raw_data[1]);
  1293. break;
  1294. }
  1295. }
  1296. if (!handlers[i].id)
  1297. hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
  1298. size);
  1299. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1300. return 0;
  1301. }
  1302. static struct wiimote_data *wiimote_create(struct hid_device *hdev)
  1303. {
  1304. struct wiimote_data *wdata;
  1305. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  1306. if (!wdata)
  1307. return NULL;
  1308. wdata->hdev = hdev;
  1309. hid_set_drvdata(hdev, wdata);
  1310. spin_lock_init(&wdata->queue.lock);
  1311. INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
  1312. spin_lock_init(&wdata->state.lock);
  1313. init_completion(&wdata->state.ready);
  1314. mutex_init(&wdata->state.sync);
  1315. wdata->state.drm = WIIPROTO_REQ_DRM_K;
  1316. wdata->state.cmd_battery = 0xff;
  1317. INIT_WORK(&wdata->init_worker, wiimote_init_worker);
  1318. setup_timer(&wdata->timer, wiimote_init_timeout, (long)wdata);
  1319. return wdata;
  1320. }
  1321. static void wiimote_destroy(struct wiimote_data *wdata)
  1322. {
  1323. unsigned long flags;
  1324. wiidebug_deinit(wdata);
  1325. /* prevent init_worker from being scheduled again */
  1326. spin_lock_irqsave(&wdata->state.lock, flags);
  1327. wdata->state.flags |= WIIPROTO_FLAG_EXITING;
  1328. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1329. cancel_work_sync(&wdata->init_worker);
  1330. del_timer_sync(&wdata->timer);
  1331. wiimote_mp_unload(wdata);
  1332. wiimote_ext_unload(wdata);
  1333. wiimote_modules_unload(wdata);
  1334. cancel_work_sync(&wdata->queue.worker);
  1335. hid_hw_close(wdata->hdev);
  1336. hid_hw_stop(wdata->hdev);
  1337. kfree(wdata);
  1338. }
  1339. static int wiimote_hid_probe(struct hid_device *hdev,
  1340. const struct hid_device_id *id)
  1341. {
  1342. struct wiimote_data *wdata;
  1343. int ret;
  1344. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  1345. wdata = wiimote_create(hdev);
  1346. if (!wdata) {
  1347. hid_err(hdev, "Can't alloc device\n");
  1348. return -ENOMEM;
  1349. }
  1350. ret = hid_parse(hdev);
  1351. if (ret) {
  1352. hid_err(hdev, "HID parse failed\n");
  1353. goto err;
  1354. }
  1355. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  1356. if (ret) {
  1357. hid_err(hdev, "HW start failed\n");
  1358. goto err;
  1359. }
  1360. ret = hid_hw_open(hdev);
  1361. if (ret) {
  1362. hid_err(hdev, "cannot start hardware I/O\n");
  1363. goto err_stop;
  1364. }
  1365. ret = wiidebug_init(wdata);
  1366. if (ret)
  1367. goto err_free;
  1368. hid_info(hdev, "New device registered\n");
  1369. /* schedule device detection */
  1370. wiimote_schedule(wdata);
  1371. return 0;
  1372. err_free:
  1373. wiimote_destroy(wdata);
  1374. return ret;
  1375. err_stop:
  1376. hid_hw_stop(hdev);
  1377. err:
  1378. input_free_device(wdata->ir);
  1379. input_free_device(wdata->accel);
  1380. kfree(wdata);
  1381. return ret;
  1382. }
  1383. static void wiimote_hid_remove(struct hid_device *hdev)
  1384. {
  1385. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  1386. hid_info(hdev, "Device removed\n");
  1387. wiimote_destroy(wdata);
  1388. }
  1389. static const struct hid_device_id wiimote_hid_devices[] = {
  1390. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  1391. USB_DEVICE_ID_NINTENDO_WIIMOTE) },
  1392. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  1393. USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
  1394. { }
  1395. };
  1396. MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
  1397. static struct hid_driver wiimote_hid_driver = {
  1398. .name = "wiimote",
  1399. .id_table = wiimote_hid_devices,
  1400. .probe = wiimote_hid_probe,
  1401. .remove = wiimote_hid_remove,
  1402. .raw_event = wiimote_hid_event,
  1403. };
  1404. module_hid_driver(wiimote_hid_driver);
  1405. MODULE_LICENSE("GPL");
  1406. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  1407. MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");