hid-wiimote-core.c 44 KB

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