hid-wiimote-core.c 38 KB

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