hid-wiimote-core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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. static __u8 select_drm(struct wiimote_data *wdata)
  150. {
  151. __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
  152. bool ext = wiiext_active(wdata);
  153. if (ir == WIIPROTO_FLAG_IR_BASIC) {
  154. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
  155. return WIIPROTO_REQ_DRM_KAIE;
  156. else
  157. return WIIPROTO_REQ_DRM_KIE;
  158. } else if (ir == WIIPROTO_FLAG_IR_EXT) {
  159. return WIIPROTO_REQ_DRM_KAI;
  160. } else if (ir == WIIPROTO_FLAG_IR_FULL) {
  161. return WIIPROTO_REQ_DRM_SKAI1;
  162. } else {
  163. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
  164. if (ext)
  165. return WIIPROTO_REQ_DRM_KAE;
  166. else
  167. return WIIPROTO_REQ_DRM_KA;
  168. } else {
  169. if (ext)
  170. return WIIPROTO_REQ_DRM_KE;
  171. else
  172. return WIIPROTO_REQ_DRM_K;
  173. }
  174. }
  175. }
  176. void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
  177. {
  178. __u8 cmd[3];
  179. if (drm == WIIPROTO_REQ_NULL)
  180. drm = select_drm(wdata);
  181. cmd[0] = WIIPROTO_REQ_DRM;
  182. cmd[1] = 0;
  183. cmd[2] = drm;
  184. wdata->state.drm = drm;
  185. wiiproto_keep_rumble(wdata, &cmd[1]);
  186. wiimote_queue(wdata, cmd, sizeof(cmd));
  187. }
  188. void wiiproto_req_status(struct wiimote_data *wdata)
  189. {
  190. __u8 cmd[2];
  191. cmd[0] = WIIPROTO_REQ_SREQ;
  192. cmd[1] = 0;
  193. wiiproto_keep_rumble(wdata, &cmd[1]);
  194. wiimote_queue(wdata, cmd, sizeof(cmd));
  195. }
  196. static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
  197. {
  198. accel = !!accel;
  199. if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  200. return;
  201. if (accel)
  202. wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
  203. else
  204. wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
  205. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  206. }
  207. static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
  208. {
  209. __u8 cmd[2];
  210. cmd[0] = WIIPROTO_REQ_IR1;
  211. cmd[1] = flags;
  212. wiiproto_keep_rumble(wdata, &cmd[1]);
  213. wiimote_queue(wdata, cmd, sizeof(cmd));
  214. }
  215. static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
  216. {
  217. __u8 cmd[2];
  218. cmd[0] = WIIPROTO_REQ_IR2;
  219. cmd[1] = flags;
  220. wiiproto_keep_rumble(wdata, &cmd[1]);
  221. wiimote_queue(wdata, cmd, sizeof(cmd));
  222. }
  223. #define wiiproto_req_wreg(wdata, os, buf, sz) \
  224. wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
  225. #define wiiproto_req_weeprom(wdata, os, buf, sz) \
  226. wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
  227. static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
  228. __u32 offset, const __u8 *buf, __u8 size)
  229. {
  230. __u8 cmd[22];
  231. if (size > 16 || size == 0) {
  232. hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
  233. return;
  234. }
  235. memset(cmd, 0, sizeof(cmd));
  236. cmd[0] = WIIPROTO_REQ_WMEM;
  237. cmd[2] = (offset >> 16) & 0xff;
  238. cmd[3] = (offset >> 8) & 0xff;
  239. cmd[4] = offset & 0xff;
  240. cmd[5] = size;
  241. memcpy(&cmd[6], buf, size);
  242. if (!eeprom)
  243. cmd[1] |= 0x04;
  244. wiiproto_keep_rumble(wdata, &cmd[1]);
  245. wiimote_queue(wdata, cmd, sizeof(cmd));
  246. }
  247. void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
  248. __u16 size)
  249. {
  250. __u8 cmd[7];
  251. if (size == 0) {
  252. hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
  253. return;
  254. }
  255. cmd[0] = WIIPROTO_REQ_RMEM;
  256. cmd[1] = 0;
  257. cmd[2] = (offset >> 16) & 0xff;
  258. cmd[3] = (offset >> 8) & 0xff;
  259. cmd[4] = offset & 0xff;
  260. cmd[5] = (size >> 8) & 0xff;
  261. cmd[6] = size & 0xff;
  262. if (!eeprom)
  263. cmd[1] |= 0x04;
  264. wiiproto_keep_rumble(wdata, &cmd[1]);
  265. wiimote_queue(wdata, cmd, sizeof(cmd));
  266. }
  267. /* requries the cmd-mutex to be held */
  268. int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  269. const __u8 *wmem, __u8 size)
  270. {
  271. unsigned long flags;
  272. int ret;
  273. spin_lock_irqsave(&wdata->state.lock, flags);
  274. wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
  275. wiiproto_req_wreg(wdata, offset, wmem, size);
  276. spin_unlock_irqrestore(&wdata->state.lock, flags);
  277. ret = wiimote_cmd_wait(wdata);
  278. if (!ret && wdata->state.cmd_err)
  279. ret = -EIO;
  280. return ret;
  281. }
  282. /* requries the cmd-mutex to be held */
  283. ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
  284. __u8 size)
  285. {
  286. unsigned long flags;
  287. ssize_t ret;
  288. spin_lock_irqsave(&wdata->state.lock, flags);
  289. wdata->state.cmd_read_size = size;
  290. wdata->state.cmd_read_buf = rmem;
  291. wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
  292. wiiproto_req_rreg(wdata, offset, size);
  293. spin_unlock_irqrestore(&wdata->state.lock, flags);
  294. ret = wiimote_cmd_wait(wdata);
  295. spin_lock_irqsave(&wdata->state.lock, flags);
  296. wdata->state.cmd_read_buf = NULL;
  297. spin_unlock_irqrestore(&wdata->state.lock, flags);
  298. if (!ret) {
  299. if (wdata->state.cmd_read_size == 0)
  300. ret = -EIO;
  301. else
  302. ret = wdata->state.cmd_read_size;
  303. }
  304. return ret;
  305. }
  306. /* requires the cmd-mutex to be held */
  307. static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
  308. {
  309. __u8 wmem;
  310. int ret;
  311. /* initialize extension */
  312. wmem = 0x55;
  313. ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
  314. if (ret)
  315. return ret;
  316. /* disable default encryption */
  317. wmem = 0x0;
  318. ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
  319. if (ret)
  320. return ret;
  321. return 0;
  322. }
  323. /* requires the cmd-mutex to be held */
  324. static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata)
  325. {
  326. __u8 rmem[6];
  327. int ret;
  328. /* read extension ID */
  329. ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
  330. if (ret != 6)
  331. return WIIMOTE_EXT_NONE;
  332. if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
  333. rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
  334. return WIIMOTE_EXT_NONE;
  335. return WIIMOTE_EXT_UNKNOWN;
  336. }
  337. static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
  338. {
  339. int ret;
  340. unsigned long flags;
  341. __u8 format = 0;
  342. static const __u8 data_enable[] = { 0x01 };
  343. static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
  344. 0x00, 0xaa, 0x00, 0x64 };
  345. static const __u8 data_sens2[] = { 0x63, 0x03 };
  346. static const __u8 data_fin[] = { 0x08 };
  347. spin_lock_irqsave(&wdata->state.lock, flags);
  348. if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
  349. spin_unlock_irqrestore(&wdata->state.lock, flags);
  350. return 0;
  351. }
  352. if (mode == 0) {
  353. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  354. wiiproto_req_ir1(wdata, 0);
  355. wiiproto_req_ir2(wdata, 0);
  356. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  357. spin_unlock_irqrestore(&wdata->state.lock, flags);
  358. return 0;
  359. }
  360. spin_unlock_irqrestore(&wdata->state.lock, flags);
  361. ret = wiimote_cmd_acquire(wdata);
  362. if (ret)
  363. return ret;
  364. /* send PIXEL CLOCK ENABLE cmd first */
  365. spin_lock_irqsave(&wdata->state.lock, flags);
  366. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
  367. wiiproto_req_ir1(wdata, 0x06);
  368. spin_unlock_irqrestore(&wdata->state.lock, flags);
  369. ret = wiimote_cmd_wait(wdata);
  370. if (ret)
  371. goto unlock;
  372. if (wdata->state.cmd_err) {
  373. ret = -EIO;
  374. goto unlock;
  375. }
  376. /* enable IR LOGIC */
  377. spin_lock_irqsave(&wdata->state.lock, flags);
  378. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
  379. wiiproto_req_ir2(wdata, 0x06);
  380. spin_unlock_irqrestore(&wdata->state.lock, flags);
  381. ret = wiimote_cmd_wait(wdata);
  382. if (ret)
  383. goto unlock;
  384. if (wdata->state.cmd_err) {
  385. ret = -EIO;
  386. goto unlock;
  387. }
  388. /* enable IR cam but do not make it send data, yet */
  389. ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
  390. sizeof(data_enable));
  391. if (ret)
  392. goto unlock;
  393. /* write first sensitivity block */
  394. ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
  395. sizeof(data_sens1));
  396. if (ret)
  397. goto unlock;
  398. /* write second sensitivity block */
  399. ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
  400. sizeof(data_sens2));
  401. if (ret)
  402. goto unlock;
  403. /* put IR cam into desired state */
  404. switch (mode) {
  405. case WIIPROTO_FLAG_IR_FULL:
  406. format = 5;
  407. break;
  408. case WIIPROTO_FLAG_IR_EXT:
  409. format = 3;
  410. break;
  411. case WIIPROTO_FLAG_IR_BASIC:
  412. format = 1;
  413. break;
  414. }
  415. ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
  416. if (ret)
  417. goto unlock;
  418. /* make IR cam send data */
  419. ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
  420. if (ret)
  421. goto unlock;
  422. /* request new DRM mode compatible to IR mode */
  423. spin_lock_irqsave(&wdata->state.lock, flags);
  424. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  425. wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
  426. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  427. spin_unlock_irqrestore(&wdata->state.lock, flags);
  428. unlock:
  429. wiimote_cmd_release(wdata);
  430. return ret;
  431. }
  432. static int wiimote_accel_open(struct input_dev *dev)
  433. {
  434. struct wiimote_data *wdata = input_get_drvdata(dev);
  435. unsigned long flags;
  436. spin_lock_irqsave(&wdata->state.lock, flags);
  437. wiiproto_req_accel(wdata, true);
  438. spin_unlock_irqrestore(&wdata->state.lock, flags);
  439. return 0;
  440. }
  441. static void wiimote_accel_close(struct input_dev *dev)
  442. {
  443. struct wiimote_data *wdata = input_get_drvdata(dev);
  444. unsigned long flags;
  445. spin_lock_irqsave(&wdata->state.lock, flags);
  446. wiiproto_req_accel(wdata, false);
  447. spin_unlock_irqrestore(&wdata->state.lock, flags);
  448. }
  449. static int wiimote_ir_open(struct input_dev *dev)
  450. {
  451. struct wiimote_data *wdata = input_get_drvdata(dev);
  452. return wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
  453. }
  454. static void wiimote_ir_close(struct input_dev *dev)
  455. {
  456. struct wiimote_data *wdata = input_get_drvdata(dev);
  457. wiimote_init_ir(wdata, 0);
  458. }
  459. /* device module handling */
  460. static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
  461. [WIIMOTE_DEV_PENDING] = (const __u8[]){
  462. WIIMOD_NULL,
  463. },
  464. [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
  465. WIIMOD_NULL,
  466. },
  467. [WIIMOTE_DEV_GENERIC] = (const __u8[]){
  468. WIIMOD_KEYS,
  469. WIIMOD_RUMBLE,
  470. WIIMOD_BATTERY,
  471. WIIMOD_LED1,
  472. WIIMOD_LED2,
  473. WIIMOD_LED3,
  474. WIIMOD_LED4,
  475. WIIMOD_NULL,
  476. },
  477. [WIIMOTE_DEV_GEN10] = (const __u8[]){
  478. WIIMOD_KEYS,
  479. WIIMOD_RUMBLE,
  480. WIIMOD_BATTERY,
  481. WIIMOD_LED1,
  482. WIIMOD_LED2,
  483. WIIMOD_LED3,
  484. WIIMOD_LED4,
  485. WIIMOD_NULL,
  486. },
  487. [WIIMOTE_DEV_GEN20] = (const __u8[]){
  488. WIIMOD_KEYS,
  489. WIIMOD_RUMBLE,
  490. WIIMOD_BATTERY,
  491. WIIMOD_LED1,
  492. WIIMOD_LED2,
  493. WIIMOD_LED3,
  494. WIIMOD_LED4,
  495. WIIMOD_NULL,
  496. },
  497. };
  498. static void wiimote_modules_load(struct wiimote_data *wdata,
  499. unsigned int devtype)
  500. {
  501. bool need_input = false;
  502. const __u8 *mods, *iter;
  503. const struct wiimod_ops *ops;
  504. int ret;
  505. mods = wiimote_devtype_mods[devtype];
  506. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  507. if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
  508. need_input = true;
  509. break;
  510. }
  511. }
  512. if (need_input) {
  513. wdata->input = input_allocate_device();
  514. if (!wdata->input)
  515. return;
  516. input_set_drvdata(wdata->input, wdata);
  517. wdata->input->dev.parent = &wdata->hdev->dev;
  518. wdata->input->id.bustype = wdata->hdev->bus;
  519. wdata->input->id.vendor = wdata->hdev->vendor;
  520. wdata->input->id.product = wdata->hdev->product;
  521. wdata->input->id.version = wdata->hdev->version;
  522. wdata->input->name = WIIMOTE_NAME;
  523. }
  524. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  525. ops = wiimod_table[*iter];
  526. if (!ops->probe)
  527. continue;
  528. ret = ops->probe(ops, wdata);
  529. if (ret)
  530. goto error;
  531. }
  532. if (wdata->input) {
  533. ret = input_register_device(wdata->input);
  534. if (ret)
  535. goto error;
  536. }
  537. spin_lock_irq(&wdata->state.lock);
  538. wdata->state.devtype = devtype;
  539. spin_unlock_irq(&wdata->state.lock);
  540. return;
  541. error:
  542. for ( ; iter-- != mods; ) {
  543. ops = wiimod_table[*iter];
  544. if (ops->remove)
  545. ops->remove(ops, wdata);
  546. }
  547. if (wdata->input) {
  548. input_free_device(wdata->input);
  549. wdata->input = NULL;
  550. }
  551. }
  552. static void wiimote_modules_unload(struct wiimote_data *wdata)
  553. {
  554. const __u8 *mods, *iter;
  555. const struct wiimod_ops *ops;
  556. unsigned long flags;
  557. mods = wiimote_devtype_mods[wdata->state.devtype];
  558. spin_lock_irqsave(&wdata->state.lock, flags);
  559. wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
  560. spin_unlock_irqrestore(&wdata->state.lock, flags);
  561. /* find end of list */
  562. for (iter = mods; *iter != WIIMOD_NULL; ++iter)
  563. /* empty */ ;
  564. if (wdata->input) {
  565. input_get_device(wdata->input);
  566. input_unregister_device(wdata->input);
  567. }
  568. for ( ; iter-- != mods; ) {
  569. ops = wiimod_table[*iter];
  570. if (ops->remove)
  571. ops->remove(ops, wdata);
  572. }
  573. if (wdata->input) {
  574. input_put_device(wdata->input);
  575. wdata->input = NULL;
  576. }
  577. }
  578. /* device (re-)initialization and detection */
  579. static const char *wiimote_devtype_names[WIIMOTE_DEV_NUM] = {
  580. [WIIMOTE_DEV_PENDING] = "Pending",
  581. [WIIMOTE_DEV_UNKNOWN] = "Unknown",
  582. [WIIMOTE_DEV_GENERIC] = "Generic",
  583. [WIIMOTE_DEV_GEN10] = "Nintendo Wii Remote (Gen 1)",
  584. [WIIMOTE_DEV_GEN20] = "Nintendo Wii Remote Plus (Gen 2)",
  585. };
  586. /* Try to guess the device type based on all collected information. We
  587. * first try to detect by static extension types, then VID/PID and the
  588. * device name. If we cannot detect the device, we use
  589. * WIIMOTE_DEV_GENERIC so all modules will get probed on the device. */
  590. static void wiimote_init_set_type(struct wiimote_data *wdata,
  591. __u8 exttype)
  592. {
  593. __u8 devtype = WIIMOTE_DEV_GENERIC;
  594. __u16 vendor, product;
  595. const char *name;
  596. vendor = wdata->hdev->vendor;
  597. product = wdata->hdev->product;
  598. name = wdata->hdev->name;
  599. if (!strcmp(name, "Nintendo RVL-CNT-01")) {
  600. devtype = WIIMOTE_DEV_GEN10;
  601. goto done;
  602. } else if (!strcmp(name, "Nintendo RVL-CNT-01-TR")) {
  603. devtype = WIIMOTE_DEV_GEN20;
  604. goto done;
  605. }
  606. if (vendor == USB_VENDOR_ID_NINTENDO) {
  607. if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE) {
  608. devtype = WIIMOTE_DEV_GEN10;
  609. goto done;
  610. } else if (product == USB_DEVICE_ID_NINTENDO_WIIMOTE2) {
  611. devtype = WIIMOTE_DEV_GEN20;
  612. goto done;
  613. }
  614. }
  615. done:
  616. if (devtype == WIIMOTE_DEV_GENERIC)
  617. hid_info(wdata->hdev, "cannot detect device; NAME: %s VID: %04x PID: %04x EXT: %04x\n",
  618. name, vendor, product, exttype);
  619. else
  620. hid_info(wdata->hdev, "detected device: %s\n",
  621. wiimote_devtype_names[devtype]);
  622. wiimote_modules_load(wdata, devtype);
  623. }
  624. static void wiimote_init_detect(struct wiimote_data *wdata)
  625. {
  626. __u8 exttype = WIIMOTE_EXT_NONE;
  627. bool ext;
  628. int ret;
  629. wiimote_cmd_acquire_noint(wdata);
  630. spin_lock_irq(&wdata->state.lock);
  631. wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
  632. wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
  633. wiiproto_req_status(wdata);
  634. spin_unlock_irq(&wdata->state.lock);
  635. ret = wiimote_cmd_wait_noint(wdata);
  636. if (ret)
  637. goto out_release;
  638. spin_lock_irq(&wdata->state.lock);
  639. ext = wdata->state.flags & WIIPROTO_FLAG_EXT_PLUGGED;
  640. spin_unlock_irq(&wdata->state.lock);
  641. if (!ext)
  642. goto out_release;
  643. wiimote_cmd_init_ext(wdata);
  644. exttype = wiimote_cmd_read_ext(wdata);
  645. out_release:
  646. wiimote_cmd_release(wdata);
  647. wiimote_init_set_type(wdata, exttype);
  648. }
  649. static void wiimote_init_worker(struct work_struct *work)
  650. {
  651. struct wiimote_data *wdata = container_of(work, struct wiimote_data,
  652. init_worker);
  653. if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
  654. wiimote_init_detect(wdata);
  655. }
  656. /* protocol handlers */
  657. static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
  658. {
  659. const __u8 *iter, *mods;
  660. const struct wiimod_ops *ops;
  661. mods = wiimote_devtype_mods[wdata->state.devtype];
  662. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  663. ops = wiimod_table[*iter];
  664. if (ops->in_keys) {
  665. ops->in_keys(wdata, payload);
  666. break;
  667. }
  668. }
  669. }
  670. static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
  671. {
  672. __u16 x, y, z;
  673. if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  674. return;
  675. /*
  676. * payload is: BB BB XX YY ZZ
  677. * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
  678. * contain the upper 8 bits of each value. The lower 2 bits are
  679. * contained in the buttons data BB BB.
  680. * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
  681. * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
  682. * accel value and bit 6 is the second bit of the Z value.
  683. * The first bit of Y and Z values is not available and always set to 0.
  684. * 0x200 is returned on no movement.
  685. */
  686. x = payload[2] << 2;
  687. y = payload[3] << 2;
  688. z = payload[4] << 2;
  689. x |= (payload[0] >> 5) & 0x3;
  690. y |= (payload[1] >> 4) & 0x2;
  691. z |= (payload[1] >> 5) & 0x2;
  692. input_report_abs(wdata->accel, ABS_RX, x - 0x200);
  693. input_report_abs(wdata->accel, ABS_RY, y - 0x200);
  694. input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
  695. input_sync(wdata->accel);
  696. }
  697. #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  698. ABS_HAT0X, ABS_HAT0Y)
  699. #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  700. ABS_HAT1X, ABS_HAT1Y)
  701. #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  702. ABS_HAT2X, ABS_HAT2Y)
  703. #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  704. ABS_HAT3X, ABS_HAT3Y)
  705. static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
  706. bool packed, __u8 xid, __u8 yid)
  707. {
  708. __u16 x, y;
  709. if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
  710. return;
  711. /*
  712. * Basic IR data is encoded into 3 bytes. The first two bytes are the
  713. * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
  714. * of both.
  715. * If data is packed, then the 3rd byte is put first and slightly
  716. * reordered. This allows to interleave packed and non-packed data to
  717. * have two IR sets in 5 bytes instead of 6.
  718. * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
  719. */
  720. if (packed) {
  721. x = ir[1] | ((ir[0] & 0x03) << 8);
  722. y = ir[2] | ((ir[0] & 0x0c) << 6);
  723. } else {
  724. x = ir[0] | ((ir[2] & 0x30) << 4);
  725. y = ir[1] | ((ir[2] & 0xc0) << 2);
  726. }
  727. input_report_abs(wdata->ir, xid, x);
  728. input_report_abs(wdata->ir, yid, y);
  729. }
  730. /* reduced status report with "BB BB" key data only */
  731. static void handler_status_K(struct wiimote_data *wdata,
  732. const __u8 *payload)
  733. {
  734. handler_keys(wdata, payload);
  735. /* on status reports the drm is reset so we need to resend the drm */
  736. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  737. }
  738. /* extended status report with "BB BB LF 00 00 VV" data */
  739. static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
  740. {
  741. handler_status_K(wdata, payload);
  742. /* update extension status */
  743. if (payload[2] & 0x02) {
  744. wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
  745. wiiext_event(wdata, true);
  746. } else {
  747. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
  748. wiiext_event(wdata, false);
  749. }
  750. wdata->state.cmd_battery = payload[5];
  751. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
  752. wiimote_cmd_complete(wdata);
  753. }
  754. /* reduced generic report with "BB BB" key data only */
  755. static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
  756. {
  757. handler_keys(wdata, payload);
  758. }
  759. static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
  760. {
  761. __u16 offset = payload[3] << 8 | payload[4];
  762. __u8 size = (payload[2] >> 4) + 1;
  763. __u8 err = payload[2] & 0x0f;
  764. handler_keys(wdata, payload);
  765. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
  766. if (err)
  767. size = 0;
  768. else if (size > wdata->state.cmd_read_size)
  769. size = wdata->state.cmd_read_size;
  770. wdata->state.cmd_read_size = size;
  771. if (wdata->state.cmd_read_buf)
  772. memcpy(wdata->state.cmd_read_buf, &payload[5], size);
  773. wiimote_cmd_complete(wdata);
  774. }
  775. }
  776. static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
  777. {
  778. __u8 err = payload[3];
  779. __u8 cmd = payload[2];
  780. handler_keys(wdata, payload);
  781. if (wiimote_cmd_pending(wdata, cmd, 0)) {
  782. wdata->state.cmd_err = err;
  783. wiimote_cmd_complete(wdata);
  784. } else if (err) {
  785. hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
  786. cmd);
  787. }
  788. }
  789. static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
  790. {
  791. handler_keys(wdata, payload);
  792. handler_accel(wdata, payload);
  793. }
  794. static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
  795. {
  796. handler_keys(wdata, payload);
  797. wiiext_handle(wdata, &payload[2]);
  798. }
  799. static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
  800. {
  801. handler_keys(wdata, payload);
  802. handler_accel(wdata, payload);
  803. ir_to_input0(wdata, &payload[5], false);
  804. ir_to_input1(wdata, &payload[8], false);
  805. ir_to_input2(wdata, &payload[11], false);
  806. ir_to_input3(wdata, &payload[14], false);
  807. input_sync(wdata->ir);
  808. }
  809. static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
  810. {
  811. handler_keys(wdata, payload);
  812. wiiext_handle(wdata, &payload[2]);
  813. }
  814. static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
  815. {
  816. handler_keys(wdata, payload);
  817. ir_to_input0(wdata, &payload[2], false);
  818. ir_to_input1(wdata, &payload[4], true);
  819. ir_to_input2(wdata, &payload[7], false);
  820. ir_to_input3(wdata, &payload[9], true);
  821. input_sync(wdata->ir);
  822. wiiext_handle(wdata, &payload[12]);
  823. }
  824. static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
  825. {
  826. handler_keys(wdata, payload);
  827. handler_accel(wdata, payload);
  828. wiiext_handle(wdata, &payload[5]);
  829. }
  830. static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
  831. {
  832. handler_keys(wdata, payload);
  833. handler_accel(wdata, payload);
  834. ir_to_input0(wdata, &payload[5], false);
  835. ir_to_input1(wdata, &payload[7], true);
  836. ir_to_input2(wdata, &payload[10], false);
  837. ir_to_input3(wdata, &payload[12], true);
  838. input_sync(wdata->ir);
  839. wiiext_handle(wdata, &payload[15]);
  840. }
  841. static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
  842. {
  843. wiiext_handle(wdata, payload);
  844. }
  845. static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
  846. {
  847. handler_keys(wdata, payload);
  848. wdata->state.accel_split[0] = payload[2];
  849. wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
  850. wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
  851. ir_to_input0(wdata, &payload[3], false);
  852. ir_to_input1(wdata, &payload[12], false);
  853. input_sync(wdata->ir);
  854. }
  855. static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
  856. {
  857. __u8 buf[5];
  858. handler_keys(wdata, payload);
  859. wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
  860. wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
  861. buf[0] = 0;
  862. buf[1] = 0;
  863. buf[2] = wdata->state.accel_split[0];
  864. buf[3] = payload[2];
  865. buf[4] = wdata->state.accel_split[1];
  866. handler_accel(wdata, buf);
  867. ir_to_input2(wdata, &payload[3], false);
  868. ir_to_input3(wdata, &payload[12], false);
  869. input_sync(wdata->ir);
  870. }
  871. struct wiiproto_handler {
  872. __u8 id;
  873. size_t size;
  874. void (*func)(struct wiimote_data *wdata, const __u8 *payload);
  875. };
  876. static struct wiiproto_handler handlers[] = {
  877. { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
  878. { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
  879. { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
  880. { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
  881. { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
  882. { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
  883. { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
  884. { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
  885. { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
  886. { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
  887. { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
  888. { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
  889. { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
  890. { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
  891. { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
  892. { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
  893. { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
  894. { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
  895. { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
  896. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
  897. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
  898. { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
  899. { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
  900. { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
  901. { .id = 0 }
  902. };
  903. static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
  904. u8 *raw_data, int size)
  905. {
  906. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  907. struct wiiproto_handler *h;
  908. int i;
  909. unsigned long flags;
  910. if (size < 1)
  911. return -EINVAL;
  912. spin_lock_irqsave(&wdata->state.lock, flags);
  913. for (i = 0; handlers[i].id; ++i) {
  914. h = &handlers[i];
  915. if (h->id == raw_data[0] && h->size < size) {
  916. h->func(wdata, &raw_data[1]);
  917. break;
  918. }
  919. }
  920. if (!handlers[i].id)
  921. hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
  922. size);
  923. spin_unlock_irqrestore(&wdata->state.lock, flags);
  924. return 0;
  925. }
  926. static struct wiimote_data *wiimote_create(struct hid_device *hdev)
  927. {
  928. struct wiimote_data *wdata;
  929. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  930. if (!wdata)
  931. return NULL;
  932. wdata->hdev = hdev;
  933. hid_set_drvdata(hdev, wdata);
  934. wdata->accel = input_allocate_device();
  935. if (!wdata->accel)
  936. goto err;
  937. input_set_drvdata(wdata->accel, wdata);
  938. wdata->accel->open = wiimote_accel_open;
  939. wdata->accel->close = wiimote_accel_close;
  940. wdata->accel->dev.parent = &wdata->hdev->dev;
  941. wdata->accel->id.bustype = wdata->hdev->bus;
  942. wdata->accel->id.vendor = wdata->hdev->vendor;
  943. wdata->accel->id.product = wdata->hdev->product;
  944. wdata->accel->id.version = wdata->hdev->version;
  945. wdata->accel->name = WIIMOTE_NAME " Accelerometer";
  946. set_bit(EV_ABS, wdata->accel->evbit);
  947. set_bit(ABS_RX, wdata->accel->absbit);
  948. set_bit(ABS_RY, wdata->accel->absbit);
  949. set_bit(ABS_RZ, wdata->accel->absbit);
  950. input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
  951. input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
  952. input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
  953. wdata->ir = input_allocate_device();
  954. if (!wdata->ir)
  955. goto err_ir;
  956. input_set_drvdata(wdata->ir, wdata);
  957. wdata->ir->open = wiimote_ir_open;
  958. wdata->ir->close = wiimote_ir_close;
  959. wdata->ir->dev.parent = &wdata->hdev->dev;
  960. wdata->ir->id.bustype = wdata->hdev->bus;
  961. wdata->ir->id.vendor = wdata->hdev->vendor;
  962. wdata->ir->id.product = wdata->hdev->product;
  963. wdata->ir->id.version = wdata->hdev->version;
  964. wdata->ir->name = WIIMOTE_NAME " IR";
  965. set_bit(EV_ABS, wdata->ir->evbit);
  966. set_bit(ABS_HAT0X, wdata->ir->absbit);
  967. set_bit(ABS_HAT0Y, wdata->ir->absbit);
  968. set_bit(ABS_HAT1X, wdata->ir->absbit);
  969. set_bit(ABS_HAT1Y, wdata->ir->absbit);
  970. set_bit(ABS_HAT2X, wdata->ir->absbit);
  971. set_bit(ABS_HAT2Y, wdata->ir->absbit);
  972. set_bit(ABS_HAT3X, wdata->ir->absbit);
  973. set_bit(ABS_HAT3Y, wdata->ir->absbit);
  974. input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
  975. input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
  976. input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
  977. input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
  978. input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
  979. input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
  980. input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
  981. input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
  982. spin_lock_init(&wdata->queue.lock);
  983. INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
  984. spin_lock_init(&wdata->state.lock);
  985. init_completion(&wdata->state.ready);
  986. mutex_init(&wdata->state.sync);
  987. wdata->state.drm = WIIPROTO_REQ_DRM_K;
  988. wdata->state.cmd_battery = 0xff;
  989. INIT_WORK(&wdata->init_worker, wiimote_init_worker);
  990. return wdata;
  991. err_ir:
  992. input_free_device(wdata->accel);
  993. err:
  994. kfree(wdata);
  995. return NULL;
  996. }
  997. static void wiimote_destroy(struct wiimote_data *wdata)
  998. {
  999. wiidebug_deinit(wdata);
  1000. wiiext_deinit(wdata);
  1001. cancel_work_sync(&wdata->init_worker);
  1002. wiimote_modules_unload(wdata);
  1003. input_unregister_device(wdata->accel);
  1004. input_unregister_device(wdata->ir);
  1005. cancel_work_sync(&wdata->queue.worker);
  1006. hid_hw_close(wdata->hdev);
  1007. hid_hw_stop(wdata->hdev);
  1008. kfree(wdata);
  1009. }
  1010. static int wiimote_hid_probe(struct hid_device *hdev,
  1011. const struct hid_device_id *id)
  1012. {
  1013. struct wiimote_data *wdata;
  1014. int ret;
  1015. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  1016. wdata = wiimote_create(hdev);
  1017. if (!wdata) {
  1018. hid_err(hdev, "Can't alloc device\n");
  1019. return -ENOMEM;
  1020. }
  1021. ret = hid_parse(hdev);
  1022. if (ret) {
  1023. hid_err(hdev, "HID parse failed\n");
  1024. goto err;
  1025. }
  1026. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  1027. if (ret) {
  1028. hid_err(hdev, "HW start failed\n");
  1029. goto err;
  1030. }
  1031. ret = hid_hw_open(hdev);
  1032. if (ret) {
  1033. hid_err(hdev, "cannot start hardware I/O\n");
  1034. goto err_stop;
  1035. }
  1036. ret = input_register_device(wdata->accel);
  1037. if (ret) {
  1038. hid_err(hdev, "Cannot register input device\n");
  1039. goto err_close;
  1040. }
  1041. ret = input_register_device(wdata->ir);
  1042. if (ret) {
  1043. hid_err(hdev, "Cannot register input device\n");
  1044. goto err_ir;
  1045. }
  1046. ret = wiiext_init(wdata);
  1047. if (ret)
  1048. goto err_free;
  1049. ret = wiidebug_init(wdata);
  1050. if (ret)
  1051. goto err_free;
  1052. hid_info(hdev, "New device registered\n");
  1053. /* schedule device detection */
  1054. schedule_work(&wdata->init_worker);
  1055. return 0;
  1056. err_free:
  1057. wiimote_destroy(wdata);
  1058. return ret;
  1059. err_ir:
  1060. input_unregister_device(wdata->accel);
  1061. wdata->accel = NULL;
  1062. err_close:
  1063. hid_hw_close(hdev);
  1064. err_stop:
  1065. hid_hw_stop(hdev);
  1066. err:
  1067. input_free_device(wdata->ir);
  1068. input_free_device(wdata->accel);
  1069. kfree(wdata);
  1070. return ret;
  1071. }
  1072. static void wiimote_hid_remove(struct hid_device *hdev)
  1073. {
  1074. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  1075. hid_info(hdev, "Device removed\n");
  1076. wiimote_destroy(wdata);
  1077. }
  1078. static const struct hid_device_id wiimote_hid_devices[] = {
  1079. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  1080. USB_DEVICE_ID_NINTENDO_WIIMOTE) },
  1081. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  1082. USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
  1083. { }
  1084. };
  1085. MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
  1086. static struct hid_driver wiimote_hid_driver = {
  1087. .name = "wiimote",
  1088. .id_table = wiimote_hid_devices,
  1089. .probe = wiimote_hid_probe,
  1090. .remove = wiimote_hid_remove,
  1091. .raw_event = wiimote_hid_event,
  1092. };
  1093. module_hid_driver(wiimote_hid_driver);
  1094. MODULE_LICENSE("GPL");
  1095. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  1096. MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");