hid-wiimote-core.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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/leds.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/spinlock.h>
  19. #include "hid-ids.h"
  20. #include "hid-wiimote.h"
  21. /* output queue handling */
  22. static int wiimote_hid_send(struct hid_device *hdev, __u8 *buffer,
  23. size_t count)
  24. {
  25. __u8 *buf;
  26. int ret;
  27. if (!hdev->hid_output_raw_report)
  28. return -ENODEV;
  29. buf = kmemdup(buffer, count, GFP_KERNEL);
  30. if (!buf)
  31. return -ENOMEM;
  32. ret = hdev->hid_output_raw_report(hdev, buf, count, HID_OUTPUT_REPORT);
  33. kfree(buf);
  34. return ret;
  35. }
  36. static void wiimote_queue_worker(struct work_struct *work)
  37. {
  38. struct wiimote_queue *queue = container_of(work, struct wiimote_queue,
  39. worker);
  40. struct wiimote_data *wdata = container_of(queue, struct wiimote_data,
  41. queue);
  42. unsigned long flags;
  43. int ret;
  44. spin_lock_irqsave(&wdata->queue.lock, flags);
  45. while (wdata->queue.head != wdata->queue.tail) {
  46. spin_unlock_irqrestore(&wdata->queue.lock, flags);
  47. ret = wiimote_hid_send(wdata->hdev,
  48. wdata->queue.outq[wdata->queue.tail].data,
  49. wdata->queue.outq[wdata->queue.tail].size);
  50. if (ret < 0) {
  51. spin_lock_irqsave(&wdata->state.lock, flags);
  52. wiimote_cmd_abort(wdata);
  53. spin_unlock_irqrestore(&wdata->state.lock, flags);
  54. }
  55. spin_lock_irqsave(&wdata->queue.lock, flags);
  56. wdata->queue.tail = (wdata->queue.tail + 1) % WIIMOTE_BUFSIZE;
  57. }
  58. spin_unlock_irqrestore(&wdata->queue.lock, flags);
  59. }
  60. static void wiimote_queue(struct wiimote_data *wdata, const __u8 *buffer,
  61. size_t count)
  62. {
  63. unsigned long flags;
  64. __u8 newhead;
  65. if (count > HID_MAX_BUFFER_SIZE) {
  66. hid_warn(wdata->hdev, "Sending too large output report\n");
  67. spin_lock_irqsave(&wdata->queue.lock, flags);
  68. goto out_error;
  69. }
  70. /*
  71. * Copy new request into our output queue and check whether the
  72. * queue is full. If it is full, discard this request.
  73. * If it is empty we need to start a new worker that will
  74. * send out the buffer to the hid device.
  75. * If the queue is not empty, then there must be a worker
  76. * that is currently sending out our buffer and this worker
  77. * will reschedule itself until the queue is empty.
  78. */
  79. spin_lock_irqsave(&wdata->queue.lock, flags);
  80. memcpy(wdata->queue.outq[wdata->queue.head].data, buffer, count);
  81. wdata->queue.outq[wdata->queue.head].size = count;
  82. newhead = (wdata->queue.head + 1) % WIIMOTE_BUFSIZE;
  83. if (wdata->queue.head == wdata->queue.tail) {
  84. wdata->queue.head = newhead;
  85. schedule_work(&wdata->queue.worker);
  86. } else if (newhead != wdata->queue.tail) {
  87. wdata->queue.head = newhead;
  88. } else {
  89. hid_warn(wdata->hdev, "Output queue is full");
  90. goto out_error;
  91. }
  92. goto out_unlock;
  93. out_error:
  94. wiimote_cmd_abort(wdata);
  95. out_unlock:
  96. spin_unlock_irqrestore(&wdata->queue.lock, flags);
  97. }
  98. /*
  99. * This sets the rumble bit on the given output report if rumble is
  100. * currently enabled.
  101. * \cmd1 must point to the second byte in the output report => &cmd[1]
  102. * This must be called on nearly every output report before passing it
  103. * into the output queue!
  104. */
  105. static inline void wiiproto_keep_rumble(struct wiimote_data *wdata, __u8 *cmd1)
  106. {
  107. if (wdata->state.flags & WIIPROTO_FLAG_RUMBLE)
  108. *cmd1 |= 0x01;
  109. }
  110. void wiiproto_req_rumble(struct wiimote_data *wdata, __u8 rumble)
  111. {
  112. __u8 cmd[2];
  113. rumble = !!rumble;
  114. if (rumble == !!(wdata->state.flags & WIIPROTO_FLAG_RUMBLE))
  115. return;
  116. if (rumble)
  117. wdata->state.flags |= WIIPROTO_FLAG_RUMBLE;
  118. else
  119. wdata->state.flags &= ~WIIPROTO_FLAG_RUMBLE;
  120. cmd[0] = WIIPROTO_REQ_RUMBLE;
  121. cmd[1] = 0;
  122. wiiproto_keep_rumble(wdata, &cmd[1]);
  123. wiimote_queue(wdata, cmd, sizeof(cmd));
  124. }
  125. static void wiiproto_req_leds(struct wiimote_data *wdata, int leds)
  126. {
  127. __u8 cmd[2];
  128. leds &= WIIPROTO_FLAGS_LEDS;
  129. if ((wdata->state.flags & WIIPROTO_FLAGS_LEDS) == leds)
  130. return;
  131. wdata->state.flags = (wdata->state.flags & ~WIIPROTO_FLAGS_LEDS) | leds;
  132. cmd[0] = WIIPROTO_REQ_LED;
  133. cmd[1] = 0;
  134. if (leds & WIIPROTO_FLAG_LED1)
  135. cmd[1] |= 0x10;
  136. if (leds & WIIPROTO_FLAG_LED2)
  137. cmd[1] |= 0x20;
  138. if (leds & WIIPROTO_FLAG_LED3)
  139. cmd[1] |= 0x40;
  140. if (leds & WIIPROTO_FLAG_LED4)
  141. cmd[1] |= 0x80;
  142. wiiproto_keep_rumble(wdata, &cmd[1]);
  143. wiimote_queue(wdata, cmd, sizeof(cmd));
  144. }
  145. /*
  146. * Check what peripherals of the wiimote are currently
  147. * active and select a proper DRM that supports all of
  148. * the requested data inputs.
  149. */
  150. static __u8 select_drm(struct wiimote_data *wdata)
  151. {
  152. __u8 ir = wdata->state.flags & WIIPROTO_FLAGS_IR;
  153. bool ext = wiiext_active(wdata);
  154. if (ir == WIIPROTO_FLAG_IR_BASIC) {
  155. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL)
  156. return WIIPROTO_REQ_DRM_KAIE;
  157. else
  158. return WIIPROTO_REQ_DRM_KIE;
  159. } else if (ir == WIIPROTO_FLAG_IR_EXT) {
  160. return WIIPROTO_REQ_DRM_KAI;
  161. } else if (ir == WIIPROTO_FLAG_IR_FULL) {
  162. return WIIPROTO_REQ_DRM_SKAI1;
  163. } else {
  164. if (wdata->state.flags & WIIPROTO_FLAG_ACCEL) {
  165. if (ext)
  166. return WIIPROTO_REQ_DRM_KAE;
  167. else
  168. return WIIPROTO_REQ_DRM_KA;
  169. } else {
  170. if (ext)
  171. return WIIPROTO_REQ_DRM_KE;
  172. else
  173. return WIIPROTO_REQ_DRM_K;
  174. }
  175. }
  176. }
  177. void wiiproto_req_drm(struct wiimote_data *wdata, __u8 drm)
  178. {
  179. __u8 cmd[3];
  180. if (drm == WIIPROTO_REQ_NULL)
  181. drm = select_drm(wdata);
  182. cmd[0] = WIIPROTO_REQ_DRM;
  183. cmd[1] = 0;
  184. cmd[2] = drm;
  185. wdata->state.drm = drm;
  186. wiiproto_keep_rumble(wdata, &cmd[1]);
  187. wiimote_queue(wdata, cmd, sizeof(cmd));
  188. }
  189. void wiiproto_req_status(struct wiimote_data *wdata)
  190. {
  191. __u8 cmd[2];
  192. cmd[0] = WIIPROTO_REQ_SREQ;
  193. cmd[1] = 0;
  194. wiiproto_keep_rumble(wdata, &cmd[1]);
  195. wiimote_queue(wdata, cmd, sizeof(cmd));
  196. }
  197. static void wiiproto_req_accel(struct wiimote_data *wdata, __u8 accel)
  198. {
  199. accel = !!accel;
  200. if (accel == !!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  201. return;
  202. if (accel)
  203. wdata->state.flags |= WIIPROTO_FLAG_ACCEL;
  204. else
  205. wdata->state.flags &= ~WIIPROTO_FLAG_ACCEL;
  206. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  207. }
  208. static void wiiproto_req_ir1(struct wiimote_data *wdata, __u8 flags)
  209. {
  210. __u8 cmd[2];
  211. cmd[0] = WIIPROTO_REQ_IR1;
  212. cmd[1] = flags;
  213. wiiproto_keep_rumble(wdata, &cmd[1]);
  214. wiimote_queue(wdata, cmd, sizeof(cmd));
  215. }
  216. static void wiiproto_req_ir2(struct wiimote_data *wdata, __u8 flags)
  217. {
  218. __u8 cmd[2];
  219. cmd[0] = WIIPROTO_REQ_IR2;
  220. cmd[1] = flags;
  221. wiiproto_keep_rumble(wdata, &cmd[1]);
  222. wiimote_queue(wdata, cmd, sizeof(cmd));
  223. }
  224. #define wiiproto_req_wreg(wdata, os, buf, sz) \
  225. wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
  226. #define wiiproto_req_weeprom(wdata, os, buf, sz) \
  227. wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
  228. static void wiiproto_req_wmem(struct wiimote_data *wdata, bool eeprom,
  229. __u32 offset, const __u8 *buf, __u8 size)
  230. {
  231. __u8 cmd[22];
  232. if (size > 16 || size == 0) {
  233. hid_warn(wdata->hdev, "Invalid length %d wmem request\n", size);
  234. return;
  235. }
  236. memset(cmd, 0, sizeof(cmd));
  237. cmd[0] = WIIPROTO_REQ_WMEM;
  238. cmd[2] = (offset >> 16) & 0xff;
  239. cmd[3] = (offset >> 8) & 0xff;
  240. cmd[4] = offset & 0xff;
  241. cmd[5] = size;
  242. memcpy(&cmd[6], buf, size);
  243. if (!eeprom)
  244. cmd[1] |= 0x04;
  245. wiiproto_keep_rumble(wdata, &cmd[1]);
  246. wiimote_queue(wdata, cmd, sizeof(cmd));
  247. }
  248. void wiiproto_req_rmem(struct wiimote_data *wdata, bool eeprom, __u32 offset,
  249. __u16 size)
  250. {
  251. __u8 cmd[7];
  252. if (size == 0) {
  253. hid_warn(wdata->hdev, "Invalid length %d rmem request\n", size);
  254. return;
  255. }
  256. cmd[0] = WIIPROTO_REQ_RMEM;
  257. cmd[1] = 0;
  258. cmd[2] = (offset >> 16) & 0xff;
  259. cmd[3] = (offset >> 8) & 0xff;
  260. cmd[4] = offset & 0xff;
  261. cmd[5] = (size >> 8) & 0xff;
  262. cmd[6] = size & 0xff;
  263. if (!eeprom)
  264. cmd[1] |= 0x04;
  265. wiiproto_keep_rumble(wdata, &cmd[1]);
  266. wiimote_queue(wdata, cmd, sizeof(cmd));
  267. }
  268. /* requries the cmd-mutex to be held */
  269. int wiimote_cmd_write(struct wiimote_data *wdata, __u32 offset,
  270. const __u8 *wmem, __u8 size)
  271. {
  272. unsigned long flags;
  273. int ret;
  274. spin_lock_irqsave(&wdata->state.lock, flags);
  275. wiimote_cmd_set(wdata, WIIPROTO_REQ_WMEM, 0);
  276. wiiproto_req_wreg(wdata, offset, wmem, size);
  277. spin_unlock_irqrestore(&wdata->state.lock, flags);
  278. ret = wiimote_cmd_wait(wdata);
  279. if (!ret && wdata->state.cmd_err)
  280. ret = -EIO;
  281. return ret;
  282. }
  283. /* requries the cmd-mutex to be held */
  284. ssize_t wiimote_cmd_read(struct wiimote_data *wdata, __u32 offset, __u8 *rmem,
  285. __u8 size)
  286. {
  287. unsigned long flags;
  288. ssize_t ret;
  289. spin_lock_irqsave(&wdata->state.lock, flags);
  290. wdata->state.cmd_read_size = size;
  291. wdata->state.cmd_read_buf = rmem;
  292. wiimote_cmd_set(wdata, WIIPROTO_REQ_RMEM, offset & 0xffff);
  293. wiiproto_req_rreg(wdata, offset, size);
  294. spin_unlock_irqrestore(&wdata->state.lock, flags);
  295. ret = wiimote_cmd_wait(wdata);
  296. spin_lock_irqsave(&wdata->state.lock, flags);
  297. wdata->state.cmd_read_buf = NULL;
  298. spin_unlock_irqrestore(&wdata->state.lock, flags);
  299. if (!ret) {
  300. if (wdata->state.cmd_read_size == 0)
  301. ret = -EIO;
  302. else
  303. ret = wdata->state.cmd_read_size;
  304. }
  305. return ret;
  306. }
  307. /* requires the cmd-mutex to be held */
  308. static int wiimote_cmd_init_ext(struct wiimote_data *wdata)
  309. {
  310. __u8 wmem;
  311. int ret;
  312. /* initialize extension */
  313. wmem = 0x55;
  314. ret = wiimote_cmd_write(wdata, 0xa400f0, &wmem, sizeof(wmem));
  315. if (ret)
  316. return ret;
  317. /* disable default encryption */
  318. wmem = 0x0;
  319. ret = wiimote_cmd_write(wdata, 0xa400fb, &wmem, sizeof(wmem));
  320. if (ret)
  321. return ret;
  322. return 0;
  323. }
  324. /* requires the cmd-mutex to be held */
  325. static __u8 wiimote_cmd_read_ext(struct wiimote_data *wdata)
  326. {
  327. __u8 rmem[6];
  328. int ret;
  329. /* read extension ID */
  330. ret = wiimote_cmd_read(wdata, 0xa400fa, rmem, 6);
  331. if (ret != 6)
  332. return WIIMOTE_EXT_NONE;
  333. if (rmem[0] == 0xff && rmem[1] == 0xff && rmem[2] == 0xff &&
  334. rmem[3] == 0xff && rmem[4] == 0xff && rmem[5] == 0xff)
  335. return WIIMOTE_EXT_NONE;
  336. return WIIMOTE_EXT_UNKNOWN;
  337. }
  338. static int wiimote_init_ir(struct wiimote_data *wdata, __u16 mode)
  339. {
  340. int ret;
  341. unsigned long flags;
  342. __u8 format = 0;
  343. static const __u8 data_enable[] = { 0x01 };
  344. static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
  345. 0x00, 0xaa, 0x00, 0x64 };
  346. static const __u8 data_sens2[] = { 0x63, 0x03 };
  347. static const __u8 data_fin[] = { 0x08 };
  348. spin_lock_irqsave(&wdata->state.lock, flags);
  349. if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
  350. spin_unlock_irqrestore(&wdata->state.lock, flags);
  351. return 0;
  352. }
  353. if (mode == 0) {
  354. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  355. wiiproto_req_ir1(wdata, 0);
  356. wiiproto_req_ir2(wdata, 0);
  357. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  358. spin_unlock_irqrestore(&wdata->state.lock, flags);
  359. return 0;
  360. }
  361. spin_unlock_irqrestore(&wdata->state.lock, flags);
  362. ret = wiimote_cmd_acquire(wdata);
  363. if (ret)
  364. return ret;
  365. /* send PIXEL CLOCK ENABLE cmd first */
  366. spin_lock_irqsave(&wdata->state.lock, flags);
  367. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
  368. wiiproto_req_ir1(wdata, 0x06);
  369. spin_unlock_irqrestore(&wdata->state.lock, flags);
  370. ret = wiimote_cmd_wait(wdata);
  371. if (ret)
  372. goto unlock;
  373. if (wdata->state.cmd_err) {
  374. ret = -EIO;
  375. goto unlock;
  376. }
  377. /* enable IR LOGIC */
  378. spin_lock_irqsave(&wdata->state.lock, flags);
  379. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
  380. wiiproto_req_ir2(wdata, 0x06);
  381. spin_unlock_irqrestore(&wdata->state.lock, flags);
  382. ret = wiimote_cmd_wait(wdata);
  383. if (ret)
  384. goto unlock;
  385. if (wdata->state.cmd_err) {
  386. ret = -EIO;
  387. goto unlock;
  388. }
  389. /* enable IR cam but do not make it send data, yet */
  390. ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
  391. sizeof(data_enable));
  392. if (ret)
  393. goto unlock;
  394. /* write first sensitivity block */
  395. ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
  396. sizeof(data_sens1));
  397. if (ret)
  398. goto unlock;
  399. /* write second sensitivity block */
  400. ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
  401. sizeof(data_sens2));
  402. if (ret)
  403. goto unlock;
  404. /* put IR cam into desired state */
  405. switch (mode) {
  406. case WIIPROTO_FLAG_IR_FULL:
  407. format = 5;
  408. break;
  409. case WIIPROTO_FLAG_IR_EXT:
  410. format = 3;
  411. break;
  412. case WIIPROTO_FLAG_IR_BASIC:
  413. format = 1;
  414. break;
  415. }
  416. ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
  417. if (ret)
  418. goto unlock;
  419. /* make IR cam send data */
  420. ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
  421. if (ret)
  422. goto unlock;
  423. /* request new DRM mode compatible to IR mode */
  424. spin_lock_irqsave(&wdata->state.lock, flags);
  425. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  426. wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
  427. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  428. spin_unlock_irqrestore(&wdata->state.lock, flags);
  429. unlock:
  430. wiimote_cmd_release(wdata);
  431. return ret;
  432. }
  433. static enum led_brightness wiimote_leds_get(struct led_classdev *led_dev)
  434. {
  435. struct wiimote_data *wdata;
  436. struct device *dev = led_dev->dev->parent;
  437. int i;
  438. unsigned long flags;
  439. bool value = false;
  440. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  441. for (i = 0; i < 4; ++i) {
  442. if (wdata->leds[i] == led_dev) {
  443. spin_lock_irqsave(&wdata->state.lock, flags);
  444. value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
  445. spin_unlock_irqrestore(&wdata->state.lock, flags);
  446. break;
  447. }
  448. }
  449. return value ? LED_FULL : LED_OFF;
  450. }
  451. static void wiimote_leds_set(struct led_classdev *led_dev,
  452. enum led_brightness value)
  453. {
  454. struct wiimote_data *wdata;
  455. struct device *dev = led_dev->dev->parent;
  456. int i;
  457. unsigned long flags;
  458. __u8 state, flag;
  459. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  460. for (i = 0; i < 4; ++i) {
  461. if (wdata->leds[i] == led_dev) {
  462. flag = WIIPROTO_FLAG_LED(i + 1);
  463. spin_lock_irqsave(&wdata->state.lock, flags);
  464. state = wdata->state.flags;
  465. if (value == LED_OFF)
  466. wiiproto_req_leds(wdata, state & ~flag);
  467. else
  468. wiiproto_req_leds(wdata, state | flag);
  469. spin_unlock_irqrestore(&wdata->state.lock, flags);
  470. break;
  471. }
  472. }
  473. }
  474. static int wiimote_accel_open(struct input_dev *dev)
  475. {
  476. struct wiimote_data *wdata = input_get_drvdata(dev);
  477. unsigned long flags;
  478. spin_lock_irqsave(&wdata->state.lock, flags);
  479. wiiproto_req_accel(wdata, true);
  480. spin_unlock_irqrestore(&wdata->state.lock, flags);
  481. return 0;
  482. }
  483. static void wiimote_accel_close(struct input_dev *dev)
  484. {
  485. struct wiimote_data *wdata = input_get_drvdata(dev);
  486. unsigned long flags;
  487. spin_lock_irqsave(&wdata->state.lock, flags);
  488. wiiproto_req_accel(wdata, false);
  489. spin_unlock_irqrestore(&wdata->state.lock, flags);
  490. }
  491. static int wiimote_ir_open(struct input_dev *dev)
  492. {
  493. struct wiimote_data *wdata = input_get_drvdata(dev);
  494. return wiimote_init_ir(wdata, WIIPROTO_FLAG_IR_BASIC);
  495. }
  496. static void wiimote_ir_close(struct input_dev *dev)
  497. {
  498. struct wiimote_data *wdata = input_get_drvdata(dev);
  499. wiimote_init_ir(wdata, 0);
  500. }
  501. /* device module handling */
  502. static const __u8 * const wiimote_devtype_mods[WIIMOTE_DEV_NUM] = {
  503. [WIIMOTE_DEV_PENDING] = (const __u8[]){
  504. WIIMOD_NULL,
  505. },
  506. [WIIMOTE_DEV_UNKNOWN] = (const __u8[]){
  507. WIIMOD_NULL,
  508. },
  509. [WIIMOTE_DEV_GENERIC] = (const __u8[]){
  510. WIIMOD_KEYS,
  511. WIIMOD_RUMBLE,
  512. WIIMOD_BATTERY,
  513. WIIMOD_NULL,
  514. },
  515. [WIIMOTE_DEV_GEN10] = (const __u8[]){
  516. WIIMOD_KEYS,
  517. WIIMOD_RUMBLE,
  518. WIIMOD_BATTERY,
  519. WIIMOD_NULL,
  520. },
  521. [WIIMOTE_DEV_GEN20] = (const __u8[]){
  522. WIIMOD_KEYS,
  523. WIIMOD_RUMBLE,
  524. WIIMOD_BATTERY,
  525. WIIMOD_NULL,
  526. },
  527. };
  528. static void wiimote_modules_load(struct wiimote_data *wdata,
  529. unsigned int devtype)
  530. {
  531. bool need_input = false;
  532. const __u8 *mods, *iter;
  533. const struct wiimod_ops *ops;
  534. int ret;
  535. mods = wiimote_devtype_mods[devtype];
  536. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  537. if (wiimod_table[*iter]->flags & WIIMOD_FLAG_INPUT) {
  538. need_input = true;
  539. break;
  540. }
  541. }
  542. if (need_input) {
  543. wdata->input = input_allocate_device();
  544. if (!wdata->input)
  545. return;
  546. input_set_drvdata(wdata->input, wdata);
  547. wdata->input->dev.parent = &wdata->hdev->dev;
  548. wdata->input->id.bustype = wdata->hdev->bus;
  549. wdata->input->id.vendor = wdata->hdev->vendor;
  550. wdata->input->id.product = wdata->hdev->product;
  551. wdata->input->id.version = wdata->hdev->version;
  552. wdata->input->name = WIIMOTE_NAME;
  553. }
  554. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  555. ops = wiimod_table[*iter];
  556. if (!ops->probe)
  557. continue;
  558. ret = ops->probe(ops, wdata);
  559. if (ret)
  560. goto error;
  561. }
  562. if (wdata->input) {
  563. ret = input_register_device(wdata->input);
  564. if (ret)
  565. goto error;
  566. }
  567. spin_lock_irq(&wdata->state.lock);
  568. wdata->state.devtype = devtype;
  569. spin_unlock_irq(&wdata->state.lock);
  570. return;
  571. error:
  572. for ( ; iter-- != mods; ) {
  573. ops = wiimod_table[*iter];
  574. if (ops->remove)
  575. ops->remove(ops, wdata);
  576. }
  577. if (wdata->input) {
  578. input_free_device(wdata->input);
  579. wdata->input = NULL;
  580. }
  581. }
  582. static void wiimote_modules_unload(struct wiimote_data *wdata)
  583. {
  584. const __u8 *mods, *iter;
  585. const struct wiimod_ops *ops;
  586. unsigned long flags;
  587. mods = wiimote_devtype_mods[wdata->state.devtype];
  588. spin_lock_irqsave(&wdata->state.lock, flags);
  589. wdata->state.devtype = WIIMOTE_DEV_UNKNOWN;
  590. spin_unlock_irqrestore(&wdata->state.lock, flags);
  591. /* find end of list */
  592. for (iter = mods; *iter != WIIMOD_NULL; ++iter)
  593. /* empty */ ;
  594. if (wdata->input) {
  595. input_get_device(wdata->input);
  596. input_unregister_device(wdata->input);
  597. }
  598. for ( ; iter-- != mods; ) {
  599. ops = wiimod_table[*iter];
  600. if (ops->remove)
  601. ops->remove(ops, wdata);
  602. }
  603. if (wdata->input) {
  604. input_put_device(wdata->input);
  605. wdata->input = NULL;
  606. }
  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;
  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);
  675. out_release:
  676. wiimote_cmd_release(wdata);
  677. wiimote_init_set_type(wdata, exttype);
  678. }
  679. static void wiimote_init_worker(struct work_struct *work)
  680. {
  681. struct wiimote_data *wdata = container_of(work, struct wiimote_data,
  682. init_worker);
  683. if (wdata->state.devtype == WIIMOTE_DEV_PENDING)
  684. wiimote_init_detect(wdata);
  685. }
  686. /* protocol handlers */
  687. static void handler_keys(struct wiimote_data *wdata, const __u8 *payload)
  688. {
  689. const __u8 *iter, *mods;
  690. const struct wiimod_ops *ops;
  691. mods = wiimote_devtype_mods[wdata->state.devtype];
  692. for (iter = mods; *iter != WIIMOD_NULL; ++iter) {
  693. ops = wiimod_table[*iter];
  694. if (ops->in_keys) {
  695. ops->in_keys(wdata, payload);
  696. break;
  697. }
  698. }
  699. }
  700. static void handler_accel(struct wiimote_data *wdata, const __u8 *payload)
  701. {
  702. __u16 x, y, z;
  703. if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  704. return;
  705. /*
  706. * payload is: BB BB XX YY ZZ
  707. * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
  708. * contain the upper 8 bits of each value. The lower 2 bits are
  709. * contained in the buttons data BB BB.
  710. * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
  711. * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
  712. * accel value and bit 6 is the second bit of the Z value.
  713. * The first bit of Y and Z values is not available and always set to 0.
  714. * 0x200 is returned on no movement.
  715. */
  716. x = payload[2] << 2;
  717. y = payload[3] << 2;
  718. z = payload[4] << 2;
  719. x |= (payload[0] >> 5) & 0x3;
  720. y |= (payload[1] >> 4) & 0x2;
  721. z |= (payload[1] >> 5) & 0x2;
  722. input_report_abs(wdata->accel, ABS_RX, x - 0x200);
  723. input_report_abs(wdata->accel, ABS_RY, y - 0x200);
  724. input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
  725. input_sync(wdata->accel);
  726. }
  727. #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  728. ABS_HAT0X, ABS_HAT0Y)
  729. #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  730. ABS_HAT1X, ABS_HAT1Y)
  731. #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  732. ABS_HAT2X, ABS_HAT2Y)
  733. #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
  734. ABS_HAT3X, ABS_HAT3Y)
  735. static void __ir_to_input(struct wiimote_data *wdata, const __u8 *ir,
  736. bool packed, __u8 xid, __u8 yid)
  737. {
  738. __u16 x, y;
  739. if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
  740. return;
  741. /*
  742. * Basic IR data is encoded into 3 bytes. The first two bytes are the
  743. * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
  744. * of both.
  745. * If data is packed, then the 3rd byte is put first and slightly
  746. * reordered. This allows to interleave packed and non-packed data to
  747. * have two IR sets in 5 bytes instead of 6.
  748. * The resulting 10bit X/Y values are passed to the ABS_HATXY input dev.
  749. */
  750. if (packed) {
  751. x = ir[1] | ((ir[0] & 0x03) << 8);
  752. y = ir[2] | ((ir[0] & 0x0c) << 6);
  753. } else {
  754. x = ir[0] | ((ir[2] & 0x30) << 4);
  755. y = ir[1] | ((ir[2] & 0xc0) << 2);
  756. }
  757. input_report_abs(wdata->ir, xid, x);
  758. input_report_abs(wdata->ir, yid, y);
  759. }
  760. /* reduced status report with "BB BB" key data only */
  761. static void handler_status_K(struct wiimote_data *wdata,
  762. const __u8 *payload)
  763. {
  764. handler_keys(wdata, payload);
  765. /* on status reports the drm is reset so we need to resend the drm */
  766. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  767. }
  768. /* extended status report with "BB BB LF 00 00 VV" data */
  769. static void handler_status(struct wiimote_data *wdata, const __u8 *payload)
  770. {
  771. handler_status_K(wdata, payload);
  772. /* update extension status */
  773. if (payload[2] & 0x02) {
  774. wdata->state.flags |= WIIPROTO_FLAG_EXT_PLUGGED;
  775. wiiext_event(wdata, true);
  776. } else {
  777. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_PLUGGED;
  778. wiiext_event(wdata, false);
  779. }
  780. wdata->state.cmd_battery = payload[5];
  781. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_SREQ, 0))
  782. wiimote_cmd_complete(wdata);
  783. }
  784. /* reduced generic report with "BB BB" key data only */
  785. static void handler_generic_K(struct wiimote_data *wdata, const __u8 *payload)
  786. {
  787. handler_keys(wdata, payload);
  788. }
  789. static void handler_data(struct wiimote_data *wdata, const __u8 *payload)
  790. {
  791. __u16 offset = payload[3] << 8 | payload[4];
  792. __u8 size = (payload[2] >> 4) + 1;
  793. __u8 err = payload[2] & 0x0f;
  794. handler_keys(wdata, payload);
  795. if (wiimote_cmd_pending(wdata, WIIPROTO_REQ_RMEM, offset)) {
  796. if (err)
  797. size = 0;
  798. else if (size > wdata->state.cmd_read_size)
  799. size = wdata->state.cmd_read_size;
  800. wdata->state.cmd_read_size = size;
  801. if (wdata->state.cmd_read_buf)
  802. memcpy(wdata->state.cmd_read_buf, &payload[5], size);
  803. wiimote_cmd_complete(wdata);
  804. }
  805. }
  806. static void handler_return(struct wiimote_data *wdata, const __u8 *payload)
  807. {
  808. __u8 err = payload[3];
  809. __u8 cmd = payload[2];
  810. handler_keys(wdata, payload);
  811. if (wiimote_cmd_pending(wdata, cmd, 0)) {
  812. wdata->state.cmd_err = err;
  813. wiimote_cmd_complete(wdata);
  814. } else if (err) {
  815. hid_warn(wdata->hdev, "Remote error %hhu on req %hhu\n", err,
  816. cmd);
  817. }
  818. }
  819. static void handler_drm_KA(struct wiimote_data *wdata, const __u8 *payload)
  820. {
  821. handler_keys(wdata, payload);
  822. handler_accel(wdata, payload);
  823. }
  824. static void handler_drm_KE(struct wiimote_data *wdata, const __u8 *payload)
  825. {
  826. handler_keys(wdata, payload);
  827. wiiext_handle(wdata, &payload[2]);
  828. }
  829. static void handler_drm_KAI(struct wiimote_data *wdata, const __u8 *payload)
  830. {
  831. handler_keys(wdata, payload);
  832. handler_accel(wdata, payload);
  833. ir_to_input0(wdata, &payload[5], false);
  834. ir_to_input1(wdata, &payload[8], false);
  835. ir_to_input2(wdata, &payload[11], false);
  836. ir_to_input3(wdata, &payload[14], false);
  837. input_sync(wdata->ir);
  838. }
  839. static void handler_drm_KEE(struct wiimote_data *wdata, const __u8 *payload)
  840. {
  841. handler_keys(wdata, payload);
  842. wiiext_handle(wdata, &payload[2]);
  843. }
  844. static void handler_drm_KIE(struct wiimote_data *wdata, const __u8 *payload)
  845. {
  846. handler_keys(wdata, payload);
  847. ir_to_input0(wdata, &payload[2], false);
  848. ir_to_input1(wdata, &payload[4], true);
  849. ir_to_input2(wdata, &payload[7], false);
  850. ir_to_input3(wdata, &payload[9], true);
  851. input_sync(wdata->ir);
  852. wiiext_handle(wdata, &payload[12]);
  853. }
  854. static void handler_drm_KAE(struct wiimote_data *wdata, const __u8 *payload)
  855. {
  856. handler_keys(wdata, payload);
  857. handler_accel(wdata, payload);
  858. wiiext_handle(wdata, &payload[5]);
  859. }
  860. static void handler_drm_KAIE(struct wiimote_data *wdata, const __u8 *payload)
  861. {
  862. handler_keys(wdata, payload);
  863. handler_accel(wdata, payload);
  864. ir_to_input0(wdata, &payload[5], false);
  865. ir_to_input1(wdata, &payload[7], true);
  866. ir_to_input2(wdata, &payload[10], false);
  867. ir_to_input3(wdata, &payload[12], true);
  868. input_sync(wdata->ir);
  869. wiiext_handle(wdata, &payload[15]);
  870. }
  871. static void handler_drm_E(struct wiimote_data *wdata, const __u8 *payload)
  872. {
  873. wiiext_handle(wdata, payload);
  874. }
  875. static void handler_drm_SKAI1(struct wiimote_data *wdata, const __u8 *payload)
  876. {
  877. handler_keys(wdata, payload);
  878. wdata->state.accel_split[0] = payload[2];
  879. wdata->state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
  880. wdata->state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
  881. ir_to_input0(wdata, &payload[3], false);
  882. ir_to_input1(wdata, &payload[12], false);
  883. input_sync(wdata->ir);
  884. }
  885. static void handler_drm_SKAI2(struct wiimote_data *wdata, const __u8 *payload)
  886. {
  887. __u8 buf[5];
  888. handler_keys(wdata, payload);
  889. wdata->state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
  890. wdata->state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
  891. buf[0] = 0;
  892. buf[1] = 0;
  893. buf[2] = wdata->state.accel_split[0];
  894. buf[3] = payload[2];
  895. buf[4] = wdata->state.accel_split[1];
  896. handler_accel(wdata, buf);
  897. ir_to_input2(wdata, &payload[3], false);
  898. ir_to_input3(wdata, &payload[12], false);
  899. input_sync(wdata->ir);
  900. }
  901. struct wiiproto_handler {
  902. __u8 id;
  903. size_t size;
  904. void (*func)(struct wiimote_data *wdata, const __u8 *payload);
  905. };
  906. static struct wiiproto_handler handlers[] = {
  907. { .id = WIIPROTO_REQ_STATUS, .size = 6, .func = handler_status },
  908. { .id = WIIPROTO_REQ_STATUS, .size = 2, .func = handler_status_K },
  909. { .id = WIIPROTO_REQ_DATA, .size = 21, .func = handler_data },
  910. { .id = WIIPROTO_REQ_DATA, .size = 2, .func = handler_generic_K },
  911. { .id = WIIPROTO_REQ_RETURN, .size = 4, .func = handler_return },
  912. { .id = WIIPROTO_REQ_RETURN, .size = 2, .func = handler_generic_K },
  913. { .id = WIIPROTO_REQ_DRM_K, .size = 2, .func = handler_keys },
  914. { .id = WIIPROTO_REQ_DRM_KA, .size = 5, .func = handler_drm_KA },
  915. { .id = WIIPROTO_REQ_DRM_KA, .size = 2, .func = handler_generic_K },
  916. { .id = WIIPROTO_REQ_DRM_KE, .size = 10, .func = handler_drm_KE },
  917. { .id = WIIPROTO_REQ_DRM_KE, .size = 2, .func = handler_generic_K },
  918. { .id = WIIPROTO_REQ_DRM_KAI, .size = 17, .func = handler_drm_KAI },
  919. { .id = WIIPROTO_REQ_DRM_KAI, .size = 2, .func = handler_generic_K },
  920. { .id = WIIPROTO_REQ_DRM_KEE, .size = 21, .func = handler_drm_KEE },
  921. { .id = WIIPROTO_REQ_DRM_KEE, .size = 2, .func = handler_generic_K },
  922. { .id = WIIPROTO_REQ_DRM_KAE, .size = 21, .func = handler_drm_KAE },
  923. { .id = WIIPROTO_REQ_DRM_KAE, .size = 2, .func = handler_generic_K },
  924. { .id = WIIPROTO_REQ_DRM_KIE, .size = 21, .func = handler_drm_KIE },
  925. { .id = WIIPROTO_REQ_DRM_KIE, .size = 2, .func = handler_generic_K },
  926. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 21, .func = handler_drm_KAIE },
  927. { .id = WIIPROTO_REQ_DRM_KAIE, .size = 2, .func = handler_generic_K },
  928. { .id = WIIPROTO_REQ_DRM_E, .size = 21, .func = handler_drm_E },
  929. { .id = WIIPROTO_REQ_DRM_SKAI1, .size = 21, .func = handler_drm_SKAI1 },
  930. { .id = WIIPROTO_REQ_DRM_SKAI2, .size = 21, .func = handler_drm_SKAI2 },
  931. { .id = 0 }
  932. };
  933. static int wiimote_hid_event(struct hid_device *hdev, struct hid_report *report,
  934. u8 *raw_data, int size)
  935. {
  936. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  937. struct wiiproto_handler *h;
  938. int i;
  939. unsigned long flags;
  940. if (size < 1)
  941. return -EINVAL;
  942. spin_lock_irqsave(&wdata->state.lock, flags);
  943. for (i = 0; handlers[i].id; ++i) {
  944. h = &handlers[i];
  945. if (h->id == raw_data[0] && h->size < size) {
  946. h->func(wdata, &raw_data[1]);
  947. break;
  948. }
  949. }
  950. if (!handlers[i].id)
  951. hid_warn(hdev, "Unhandled report %hhu size %d\n", raw_data[0],
  952. size);
  953. spin_unlock_irqrestore(&wdata->state.lock, flags);
  954. return 0;
  955. }
  956. static void wiimote_leds_destroy(struct wiimote_data *wdata)
  957. {
  958. int i;
  959. struct led_classdev *led;
  960. for (i = 0; i < 4; ++i) {
  961. if (wdata->leds[i]) {
  962. led = wdata->leds[i];
  963. wdata->leds[i] = NULL;
  964. led_classdev_unregister(led);
  965. kfree(led);
  966. }
  967. }
  968. }
  969. static int wiimote_leds_create(struct wiimote_data *wdata)
  970. {
  971. int i, ret;
  972. struct device *dev = &wdata->hdev->dev;
  973. size_t namesz = strlen(dev_name(dev)) + 9;
  974. struct led_classdev *led;
  975. char *name;
  976. for (i = 0; i < 4; ++i) {
  977. led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
  978. if (!led) {
  979. ret = -ENOMEM;
  980. goto err;
  981. }
  982. name = (void*)&led[1];
  983. snprintf(name, namesz, "%s:blue:p%d", dev_name(dev), i);
  984. led->name = name;
  985. led->brightness = 0;
  986. led->max_brightness = 1;
  987. led->brightness_get = wiimote_leds_get;
  988. led->brightness_set = wiimote_leds_set;
  989. ret = led_classdev_register(dev, led);
  990. if (ret) {
  991. kfree(led);
  992. goto err;
  993. }
  994. wdata->leds[i] = led;
  995. }
  996. return 0;
  997. err:
  998. wiimote_leds_destroy(wdata);
  999. return ret;
  1000. }
  1001. static struct wiimote_data *wiimote_create(struct hid_device *hdev)
  1002. {
  1003. struct wiimote_data *wdata;
  1004. wdata = kzalloc(sizeof(*wdata), GFP_KERNEL);
  1005. if (!wdata)
  1006. return NULL;
  1007. wdata->hdev = hdev;
  1008. hid_set_drvdata(hdev, wdata);
  1009. wdata->accel = input_allocate_device();
  1010. if (!wdata->accel)
  1011. goto err;
  1012. input_set_drvdata(wdata->accel, wdata);
  1013. wdata->accel->open = wiimote_accel_open;
  1014. wdata->accel->close = wiimote_accel_close;
  1015. wdata->accel->dev.parent = &wdata->hdev->dev;
  1016. wdata->accel->id.bustype = wdata->hdev->bus;
  1017. wdata->accel->id.vendor = wdata->hdev->vendor;
  1018. wdata->accel->id.product = wdata->hdev->product;
  1019. wdata->accel->id.version = wdata->hdev->version;
  1020. wdata->accel->name = WIIMOTE_NAME " Accelerometer";
  1021. set_bit(EV_ABS, wdata->accel->evbit);
  1022. set_bit(ABS_RX, wdata->accel->absbit);
  1023. set_bit(ABS_RY, wdata->accel->absbit);
  1024. set_bit(ABS_RZ, wdata->accel->absbit);
  1025. input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
  1026. input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
  1027. input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
  1028. wdata->ir = input_allocate_device();
  1029. if (!wdata->ir)
  1030. goto err_ir;
  1031. input_set_drvdata(wdata->ir, wdata);
  1032. wdata->ir->open = wiimote_ir_open;
  1033. wdata->ir->close = wiimote_ir_close;
  1034. wdata->ir->dev.parent = &wdata->hdev->dev;
  1035. wdata->ir->id.bustype = wdata->hdev->bus;
  1036. wdata->ir->id.vendor = wdata->hdev->vendor;
  1037. wdata->ir->id.product = wdata->hdev->product;
  1038. wdata->ir->id.version = wdata->hdev->version;
  1039. wdata->ir->name = WIIMOTE_NAME " IR";
  1040. set_bit(EV_ABS, wdata->ir->evbit);
  1041. set_bit(ABS_HAT0X, wdata->ir->absbit);
  1042. set_bit(ABS_HAT0Y, wdata->ir->absbit);
  1043. set_bit(ABS_HAT1X, wdata->ir->absbit);
  1044. set_bit(ABS_HAT1Y, wdata->ir->absbit);
  1045. set_bit(ABS_HAT2X, wdata->ir->absbit);
  1046. set_bit(ABS_HAT2Y, wdata->ir->absbit);
  1047. set_bit(ABS_HAT3X, wdata->ir->absbit);
  1048. set_bit(ABS_HAT3Y, wdata->ir->absbit);
  1049. input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
  1050. input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
  1051. input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
  1052. input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
  1053. input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
  1054. input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
  1055. input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
  1056. input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
  1057. spin_lock_init(&wdata->queue.lock);
  1058. INIT_WORK(&wdata->queue.worker, wiimote_queue_worker);
  1059. spin_lock_init(&wdata->state.lock);
  1060. init_completion(&wdata->state.ready);
  1061. mutex_init(&wdata->state.sync);
  1062. wdata->state.drm = WIIPROTO_REQ_DRM_K;
  1063. wdata->state.cmd_battery = 0xff;
  1064. INIT_WORK(&wdata->init_worker, wiimote_init_worker);
  1065. return wdata;
  1066. err_ir:
  1067. input_free_device(wdata->accel);
  1068. err:
  1069. kfree(wdata);
  1070. return NULL;
  1071. }
  1072. static void wiimote_destroy(struct wiimote_data *wdata)
  1073. {
  1074. wiidebug_deinit(wdata);
  1075. wiiext_deinit(wdata);
  1076. wiimote_leds_destroy(wdata);
  1077. cancel_work_sync(&wdata->init_worker);
  1078. wiimote_modules_unload(wdata);
  1079. input_unregister_device(wdata->accel);
  1080. input_unregister_device(wdata->ir);
  1081. cancel_work_sync(&wdata->queue.worker);
  1082. hid_hw_close(wdata->hdev);
  1083. hid_hw_stop(wdata->hdev);
  1084. kfree(wdata);
  1085. }
  1086. static int wiimote_hid_probe(struct hid_device *hdev,
  1087. const struct hid_device_id *id)
  1088. {
  1089. struct wiimote_data *wdata;
  1090. int ret;
  1091. hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
  1092. wdata = wiimote_create(hdev);
  1093. if (!wdata) {
  1094. hid_err(hdev, "Can't alloc device\n");
  1095. return -ENOMEM;
  1096. }
  1097. ret = hid_parse(hdev);
  1098. if (ret) {
  1099. hid_err(hdev, "HID parse failed\n");
  1100. goto err;
  1101. }
  1102. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  1103. if (ret) {
  1104. hid_err(hdev, "HW start failed\n");
  1105. goto err;
  1106. }
  1107. ret = hid_hw_open(hdev);
  1108. if (ret) {
  1109. hid_err(hdev, "cannot start hardware I/O\n");
  1110. goto err_stop;
  1111. }
  1112. ret = input_register_device(wdata->accel);
  1113. if (ret) {
  1114. hid_err(hdev, "Cannot register input device\n");
  1115. goto err_close;
  1116. }
  1117. ret = input_register_device(wdata->ir);
  1118. if (ret) {
  1119. hid_err(hdev, "Cannot register input device\n");
  1120. goto err_ir;
  1121. }
  1122. ret = wiimote_leds_create(wdata);
  1123. if (ret)
  1124. goto err_free;
  1125. ret = wiiext_init(wdata);
  1126. if (ret)
  1127. goto err_free;
  1128. ret = wiidebug_init(wdata);
  1129. if (ret)
  1130. goto err_free;
  1131. hid_info(hdev, "New device registered\n");
  1132. /* by default set led1 after device initialization */
  1133. spin_lock_irq(&wdata->state.lock);
  1134. wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
  1135. spin_unlock_irq(&wdata->state.lock);
  1136. /* schedule device detection */
  1137. schedule_work(&wdata->init_worker);
  1138. return 0;
  1139. err_free:
  1140. wiimote_destroy(wdata);
  1141. return ret;
  1142. err_ir:
  1143. input_unregister_device(wdata->accel);
  1144. wdata->accel = NULL;
  1145. err_close:
  1146. hid_hw_close(hdev);
  1147. err_stop:
  1148. hid_hw_stop(hdev);
  1149. err:
  1150. input_free_device(wdata->ir);
  1151. input_free_device(wdata->accel);
  1152. kfree(wdata);
  1153. return ret;
  1154. }
  1155. static void wiimote_hid_remove(struct hid_device *hdev)
  1156. {
  1157. struct wiimote_data *wdata = hid_get_drvdata(hdev);
  1158. hid_info(hdev, "Device removed\n");
  1159. wiimote_destroy(wdata);
  1160. }
  1161. static const struct hid_device_id wiimote_hid_devices[] = {
  1162. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  1163. USB_DEVICE_ID_NINTENDO_WIIMOTE) },
  1164. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO,
  1165. USB_DEVICE_ID_NINTENDO_WIIMOTE2) },
  1166. { }
  1167. };
  1168. MODULE_DEVICE_TABLE(hid, wiimote_hid_devices);
  1169. static struct hid_driver wiimote_hid_driver = {
  1170. .name = "wiimote",
  1171. .id_table = wiimote_hid_devices,
  1172. .probe = wiimote_hid_probe,
  1173. .remove = wiimote_hid_remove,
  1174. .raw_event = wiimote_hid_event,
  1175. };
  1176. module_hid_driver(wiimote_hid_driver);
  1177. MODULE_LICENSE("GPL");
  1178. MODULE_AUTHOR("David Herrmann <dh.herrmann@gmail.com>");
  1179. MODULE_DESCRIPTION("Driver for Nintendo Wii / Wii U peripherals");