hid-wiimote-core.c 33 KB

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