hid-wiimote-core.c 31 KB

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