hid-wiimote-core.c 49 KB

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