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