hid-wiimote-core.c 43 KB

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