hid-wiimote-core.c 48 KB

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