hid-wiimote-core.c 48 KB

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