hid-wiimote-core.c 49 KB

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