hid-wiimote-core.c 49 KB

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