hid-wiimote-modules.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * Device Modules for Nintendo Wii / Wii U HID Driver
  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. /*
  12. * Wiimote Modules
  13. * Nintendo devices provide different peripherals and many new devices lack
  14. * initial features like the IR camera. Therefore, each peripheral device is
  15. * implemented as an independent module and we probe on each device only the
  16. * modules for the hardware that really is available.
  17. *
  18. * Module registration is sequential. Unregistration is done in reverse order.
  19. * After device detection, the needed modules are loaded. Users can trigger
  20. * re-detection which causes all modules to be unloaded and then reload the
  21. * modules for the new detected device.
  22. *
  23. * wdata->input is a shared input device. It is always initialized prior to
  24. * module registration. If at least one registered module is marked as
  25. * WIIMOD_FLAG_INPUT, then the input device will get registered after all
  26. * modules were registered.
  27. * Please note that it is unregistered _before_ the "remove" callbacks are
  28. * called. This guarantees that no input interaction is done, anymore. However,
  29. * the wiimote core keeps a reference to the input device so it is freed only
  30. * after all modules were removed. It is safe to send events to unregistered
  31. * input devices.
  32. */
  33. #include <linux/device.h>
  34. #include <linux/hid.h>
  35. #include <linux/input.h>
  36. #include <linux/spinlock.h>
  37. #include "hid-wiimote.h"
  38. /*
  39. * Keys
  40. * The initial Wii Remote provided a bunch of buttons that are reported as
  41. * part of the core protocol. Many later devices dropped these and report
  42. * invalid data in the core button reports. Load this only on devices which
  43. * correctly send button reports.
  44. * It uses the shared input device.
  45. */
  46. static const __u16 wiimod_keys_map[] = {
  47. KEY_LEFT, /* WIIPROTO_KEY_LEFT */
  48. KEY_RIGHT, /* WIIPROTO_KEY_RIGHT */
  49. KEY_UP, /* WIIPROTO_KEY_UP */
  50. KEY_DOWN, /* WIIPROTO_KEY_DOWN */
  51. KEY_NEXT, /* WIIPROTO_KEY_PLUS */
  52. KEY_PREVIOUS, /* WIIPROTO_KEY_MINUS */
  53. BTN_1, /* WIIPROTO_KEY_ONE */
  54. BTN_2, /* WIIPROTO_KEY_TWO */
  55. BTN_A, /* WIIPROTO_KEY_A */
  56. BTN_B, /* WIIPROTO_KEY_B */
  57. BTN_MODE, /* WIIPROTO_KEY_HOME */
  58. };
  59. static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
  60. {
  61. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT],
  62. !!(keys[0] & 0x01));
  63. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT],
  64. !!(keys[0] & 0x02));
  65. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN],
  66. !!(keys[0] & 0x04));
  67. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP],
  68. !!(keys[0] & 0x08));
  69. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS],
  70. !!(keys[0] & 0x10));
  71. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO],
  72. !!(keys[1] & 0x01));
  73. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE],
  74. !!(keys[1] & 0x02));
  75. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B],
  76. !!(keys[1] & 0x04));
  77. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A],
  78. !!(keys[1] & 0x08));
  79. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS],
  80. !!(keys[1] & 0x10));
  81. input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME],
  82. !!(keys[1] & 0x80));
  83. input_sync(wdata->input);
  84. }
  85. static int wiimod_keys_probe(const struct wiimod_ops *ops,
  86. struct wiimote_data *wdata)
  87. {
  88. unsigned int i;
  89. set_bit(EV_KEY, wdata->input->evbit);
  90. for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
  91. set_bit(wiimod_keys_map[i], wdata->input->keybit);
  92. return 0;
  93. }
  94. static const struct wiimod_ops wiimod_keys = {
  95. .flags = WIIMOD_FLAG_INPUT,
  96. .arg = 0,
  97. .probe = wiimod_keys_probe,
  98. .remove = NULL,
  99. .in_keys = wiimod_keys_in_keys,
  100. };
  101. /*
  102. * Rumble
  103. * Nearly all devices provide a rumble feature. A small motor for
  104. * force-feedback effects. We provide an FF_RUMBLE memless ff device on the
  105. * shared input device if this module is loaded.
  106. * The rumble motor is controlled via a flag on almost every output report so
  107. * the wiimote core handles the rumble flag. But if a device doesn't provide
  108. * the rumble motor, this flag shouldn't be set.
  109. */
  110. static int wiimod_rumble_play(struct input_dev *dev, void *data,
  111. struct ff_effect *eff)
  112. {
  113. struct wiimote_data *wdata = input_get_drvdata(dev);
  114. __u8 value;
  115. unsigned long flags;
  116. /*
  117. * The wiimote supports only a single rumble motor so if any magnitude
  118. * is set to non-zero then we start the rumble motor. If both are set to
  119. * zero, we stop the rumble motor.
  120. */
  121. if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
  122. value = 1;
  123. else
  124. value = 0;
  125. spin_lock_irqsave(&wdata->state.lock, flags);
  126. wiiproto_req_rumble(wdata, value);
  127. spin_unlock_irqrestore(&wdata->state.lock, flags);
  128. return 0;
  129. }
  130. static int wiimod_rumble_probe(const struct wiimod_ops *ops,
  131. struct wiimote_data *wdata)
  132. {
  133. set_bit(FF_RUMBLE, wdata->input->ffbit);
  134. if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
  135. return -ENOMEM;
  136. return 0;
  137. }
  138. static void wiimod_rumble_remove(const struct wiimod_ops *ops,
  139. struct wiimote_data *wdata)
  140. {
  141. unsigned long flags;
  142. spin_lock_irqsave(&wdata->state.lock, flags);
  143. wiiproto_req_rumble(wdata, 0);
  144. spin_unlock_irqrestore(&wdata->state.lock, flags);
  145. }
  146. static const struct wiimod_ops wiimod_rumble = {
  147. .flags = WIIMOD_FLAG_INPUT,
  148. .arg = 0,
  149. .probe = wiimod_rumble_probe,
  150. .remove = wiimod_rumble_remove,
  151. };
  152. /*
  153. * Battery
  154. * 1 byte of battery capacity information is sent along every protocol status
  155. * report. The wiimote core caches it but we try to update it on every
  156. * user-space request.
  157. * This is supported by nearly every device so it's almost always enabled.
  158. */
  159. static enum power_supply_property wiimod_battery_props[] = {
  160. POWER_SUPPLY_PROP_CAPACITY,
  161. POWER_SUPPLY_PROP_SCOPE,
  162. };
  163. static int wiimod_battery_get_property(struct power_supply *psy,
  164. enum power_supply_property psp,
  165. union power_supply_propval *val)
  166. {
  167. struct wiimote_data *wdata = container_of(psy, struct wiimote_data,
  168. battery);
  169. int ret = 0, state;
  170. unsigned long flags;
  171. if (psp == POWER_SUPPLY_PROP_SCOPE) {
  172. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  173. return 0;
  174. } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
  175. return -EINVAL;
  176. }
  177. ret = wiimote_cmd_acquire(wdata);
  178. if (ret)
  179. return ret;
  180. spin_lock_irqsave(&wdata->state.lock, flags);
  181. wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
  182. wiiproto_req_status(wdata);
  183. spin_unlock_irqrestore(&wdata->state.lock, flags);
  184. wiimote_cmd_wait(wdata);
  185. wiimote_cmd_release(wdata);
  186. spin_lock_irqsave(&wdata->state.lock, flags);
  187. state = wdata->state.cmd_battery;
  188. spin_unlock_irqrestore(&wdata->state.lock, flags);
  189. val->intval = state * 100 / 255;
  190. return ret;
  191. }
  192. static int wiimod_battery_probe(const struct wiimod_ops *ops,
  193. struct wiimote_data *wdata)
  194. {
  195. int ret;
  196. wdata->battery.properties = wiimod_battery_props;
  197. wdata->battery.num_properties = ARRAY_SIZE(wiimod_battery_props);
  198. wdata->battery.get_property = wiimod_battery_get_property;
  199. wdata->battery.type = POWER_SUPPLY_TYPE_BATTERY;
  200. wdata->battery.use_for_apm = 0;
  201. wdata->battery.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
  202. wdata->hdev->uniq);
  203. if (!wdata->battery.name)
  204. return -ENOMEM;
  205. ret = power_supply_register(&wdata->hdev->dev, &wdata->battery);
  206. if (ret) {
  207. hid_err(wdata->hdev, "cannot register battery device\n");
  208. goto err_free;
  209. }
  210. power_supply_powers(&wdata->battery, &wdata->hdev->dev);
  211. return 0;
  212. err_free:
  213. kfree(wdata->battery.name);
  214. wdata->battery.name = NULL;
  215. return ret;
  216. }
  217. static void wiimod_battery_remove(const struct wiimod_ops *ops,
  218. struct wiimote_data *wdata)
  219. {
  220. if (!wdata->battery.name)
  221. return;
  222. power_supply_unregister(&wdata->battery);
  223. kfree(wdata->battery.name);
  224. wdata->battery.name = NULL;
  225. }
  226. static const struct wiimod_ops wiimod_battery = {
  227. .flags = 0,
  228. .arg = 0,
  229. .probe = wiimod_battery_probe,
  230. .remove = wiimod_battery_remove,
  231. };
  232. /*
  233. * LED
  234. * 0 to 4 player LEDs are supported by devices. The "arg" field of the
  235. * wiimod_ops structure specifies which LED this module controls. This allows
  236. * to register a limited number of LEDs.
  237. * State is managed by wiimote core.
  238. */
  239. static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
  240. {
  241. struct wiimote_data *wdata;
  242. struct device *dev = led_dev->dev->parent;
  243. int i;
  244. unsigned long flags;
  245. bool value = false;
  246. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  247. for (i = 0; i < 4; ++i) {
  248. if (wdata->leds[i] == led_dev) {
  249. spin_lock_irqsave(&wdata->state.lock, flags);
  250. value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
  251. spin_unlock_irqrestore(&wdata->state.lock, flags);
  252. break;
  253. }
  254. }
  255. return value ? LED_FULL : LED_OFF;
  256. }
  257. static void wiimod_led_set(struct led_classdev *led_dev,
  258. enum led_brightness value)
  259. {
  260. struct wiimote_data *wdata;
  261. struct device *dev = led_dev->dev->parent;
  262. int i;
  263. unsigned long flags;
  264. __u8 state, flag;
  265. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  266. for (i = 0; i < 4; ++i) {
  267. if (wdata->leds[i] == led_dev) {
  268. flag = WIIPROTO_FLAG_LED(i + 1);
  269. spin_lock_irqsave(&wdata->state.lock, flags);
  270. state = wdata->state.flags;
  271. if (value == LED_OFF)
  272. wiiproto_req_leds(wdata, state & ~flag);
  273. else
  274. wiiproto_req_leds(wdata, state | flag);
  275. spin_unlock_irqrestore(&wdata->state.lock, flags);
  276. break;
  277. }
  278. }
  279. }
  280. static int wiimod_led_probe(const struct wiimod_ops *ops,
  281. struct wiimote_data *wdata)
  282. {
  283. struct device *dev = &wdata->hdev->dev;
  284. size_t namesz = strlen(dev_name(dev)) + 9;
  285. struct led_classdev *led;
  286. unsigned long flags;
  287. char *name;
  288. int ret;
  289. led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
  290. if (!led)
  291. return -ENOMEM;
  292. name = (void*)&led[1];
  293. snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
  294. led->name = name;
  295. led->brightness = 0;
  296. led->max_brightness = 1;
  297. led->brightness_get = wiimod_led_get;
  298. led->brightness_set = wiimod_led_set;
  299. wdata->leds[ops->arg] = led;
  300. ret = led_classdev_register(dev, led);
  301. if (ret)
  302. goto err_free;
  303. /* enable LED1 to stop initial LED-blinking */
  304. if (ops->arg == 0) {
  305. spin_lock_irqsave(&wdata->state.lock, flags);
  306. wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
  307. spin_unlock_irqrestore(&wdata->state.lock, flags);
  308. }
  309. return 0;
  310. err_free:
  311. wdata->leds[ops->arg] = NULL;
  312. kfree(led);
  313. return ret;
  314. }
  315. static void wiimod_led_remove(const struct wiimod_ops *ops,
  316. struct wiimote_data *wdata)
  317. {
  318. if (!wdata->leds[ops->arg])
  319. return;
  320. led_classdev_unregister(wdata->leds[ops->arg]);
  321. kfree(wdata->leds[ops->arg]);
  322. wdata->leds[ops->arg] = NULL;
  323. }
  324. static const struct wiimod_ops wiimod_leds[4] = {
  325. {
  326. .flags = 0,
  327. .arg = 0,
  328. .probe = wiimod_led_probe,
  329. .remove = wiimod_led_remove,
  330. },
  331. {
  332. .flags = 0,
  333. .arg = 1,
  334. .probe = wiimod_led_probe,
  335. .remove = wiimod_led_remove,
  336. },
  337. {
  338. .flags = 0,
  339. .arg = 2,
  340. .probe = wiimod_led_probe,
  341. .remove = wiimod_led_remove,
  342. },
  343. {
  344. .flags = 0,
  345. .arg = 3,
  346. .probe = wiimod_led_probe,
  347. .remove = wiimod_led_remove,
  348. },
  349. };
  350. /*
  351. * Accelerometer
  352. * 3 axis accelerometer data is part of nearly all DRMs. If not supported by a
  353. * device, it's mostly cleared to 0. This module parses this data and provides
  354. * it via a separate input device.
  355. */
  356. static void wiimod_accel_in_accel(struct wiimote_data *wdata,
  357. const __u8 *accel)
  358. {
  359. __u16 x, y, z;
  360. if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  361. return;
  362. /*
  363. * payload is: BB BB XX YY ZZ
  364. * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
  365. * contain the upper 8 bits of each value. The lower 2 bits are
  366. * contained in the buttons data BB BB.
  367. * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
  368. * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
  369. * accel value and bit 6 is the second bit of the Z value.
  370. * The first bit of Y and Z values is not available and always set to 0.
  371. * 0x200 is returned on no movement.
  372. */
  373. x = accel[2] << 2;
  374. y = accel[3] << 2;
  375. z = accel[4] << 2;
  376. x |= (accel[0] >> 5) & 0x3;
  377. y |= (accel[1] >> 4) & 0x2;
  378. z |= (accel[1] >> 5) & 0x2;
  379. input_report_abs(wdata->accel, ABS_RX, x - 0x200);
  380. input_report_abs(wdata->accel, ABS_RY, y - 0x200);
  381. input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
  382. input_sync(wdata->accel);
  383. }
  384. static int wiimod_accel_open(struct input_dev *dev)
  385. {
  386. struct wiimote_data *wdata = input_get_drvdata(dev);
  387. unsigned long flags;
  388. spin_lock_irqsave(&wdata->state.lock, flags);
  389. wiiproto_req_accel(wdata, true);
  390. spin_unlock_irqrestore(&wdata->state.lock, flags);
  391. return 0;
  392. }
  393. static void wiimod_accel_close(struct input_dev *dev)
  394. {
  395. struct wiimote_data *wdata = input_get_drvdata(dev);
  396. unsigned long flags;
  397. spin_lock_irqsave(&wdata->state.lock, flags);
  398. wiiproto_req_accel(wdata, false);
  399. spin_unlock_irqrestore(&wdata->state.lock, flags);
  400. }
  401. static int wiimod_accel_probe(const struct wiimod_ops *ops,
  402. struct wiimote_data *wdata)
  403. {
  404. int ret;
  405. wdata->accel = input_allocate_device();
  406. if (!wdata->accel)
  407. return -ENOMEM;
  408. input_set_drvdata(wdata->accel, wdata);
  409. wdata->accel->open = wiimod_accel_open;
  410. wdata->accel->close = wiimod_accel_close;
  411. wdata->accel->dev.parent = &wdata->hdev->dev;
  412. wdata->accel->id.bustype = wdata->hdev->bus;
  413. wdata->accel->id.vendor = wdata->hdev->vendor;
  414. wdata->accel->id.product = wdata->hdev->product;
  415. wdata->accel->id.version = wdata->hdev->version;
  416. wdata->accel->name = WIIMOTE_NAME " Accelerometer";
  417. set_bit(EV_ABS, wdata->accel->evbit);
  418. set_bit(ABS_RX, wdata->accel->absbit);
  419. set_bit(ABS_RY, wdata->accel->absbit);
  420. set_bit(ABS_RZ, wdata->accel->absbit);
  421. input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
  422. input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
  423. input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
  424. ret = input_register_device(wdata->accel);
  425. if (ret) {
  426. hid_err(wdata->hdev, "cannot register input device\n");
  427. goto err_free;
  428. }
  429. return 0;
  430. err_free:
  431. input_free_device(wdata->accel);
  432. wdata->accel = NULL;
  433. return ret;
  434. }
  435. static void wiimod_accel_remove(const struct wiimod_ops *ops,
  436. struct wiimote_data *wdata)
  437. {
  438. if (!wdata->accel)
  439. return;
  440. input_unregister_device(wdata->accel);
  441. wdata->accel = NULL;
  442. }
  443. static const struct wiimod_ops wiimod_accel = {
  444. .flags = 0,
  445. .arg = 0,
  446. .probe = wiimod_accel_probe,
  447. .remove = wiimod_accel_remove,
  448. .in_accel = wiimod_accel_in_accel,
  449. };
  450. /*
  451. * IR Cam
  452. * Up to 4 IR sources can be tracked by a normal Wii Remote. The IR cam needs
  453. * to be initialized with a fairly complex procedure and consumes a lot of
  454. * power. Therefore, as long as no application uses the IR input device, it is
  455. * kept offline.
  456. * Nearly no other device than the normal Wii Remotes supports the IR cam so
  457. * you can disable this module for these devices.
  458. */
  459. static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir,
  460. bool packed, unsigned int id)
  461. {
  462. __u16 x, y;
  463. __u8 xid, yid;
  464. bool sync = false;
  465. if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
  466. return;
  467. switch (id) {
  468. case 0:
  469. xid = ABS_HAT0X;
  470. yid = ABS_HAT0Y;
  471. break;
  472. case 1:
  473. xid = ABS_HAT1X;
  474. yid = ABS_HAT1Y;
  475. break;
  476. case 2:
  477. xid = ABS_HAT2X;
  478. yid = ABS_HAT2Y;
  479. break;
  480. case 3:
  481. xid = ABS_HAT3X;
  482. yid = ABS_HAT3Y;
  483. sync = true;
  484. break;
  485. default:
  486. return;
  487. };
  488. /*
  489. * Basic IR data is encoded into 3 bytes. The first two bytes are the
  490. * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
  491. * of both.
  492. * If data is packed, then the 3rd byte is put first and slightly
  493. * reordered. This allows to interleave packed and non-packed data to
  494. * have two IR sets in 5 bytes instead of 6.
  495. * The resulting 10bit X/Y values are passed to the ABS_HAT? input dev.
  496. */
  497. if (packed) {
  498. x = ir[1] | ((ir[0] & 0x03) << 8);
  499. y = ir[2] | ((ir[0] & 0x0c) << 6);
  500. } else {
  501. x = ir[0] | ((ir[2] & 0x30) << 4);
  502. y = ir[1] | ((ir[2] & 0xc0) << 2);
  503. }
  504. input_report_abs(wdata->ir, xid, x);
  505. input_report_abs(wdata->ir, yid, y);
  506. if (sync)
  507. input_sync(wdata->ir);
  508. }
  509. static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode)
  510. {
  511. int ret;
  512. unsigned long flags;
  513. __u8 format = 0;
  514. static const __u8 data_enable[] = { 0x01 };
  515. static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
  516. 0x00, 0xaa, 0x00, 0x64 };
  517. static const __u8 data_sens2[] = { 0x63, 0x03 };
  518. static const __u8 data_fin[] = { 0x08 };
  519. spin_lock_irqsave(&wdata->state.lock, flags);
  520. if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
  521. spin_unlock_irqrestore(&wdata->state.lock, flags);
  522. return 0;
  523. }
  524. if (mode == 0) {
  525. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  526. wiiproto_req_ir1(wdata, 0);
  527. wiiproto_req_ir2(wdata, 0);
  528. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  529. spin_unlock_irqrestore(&wdata->state.lock, flags);
  530. return 0;
  531. }
  532. spin_unlock_irqrestore(&wdata->state.lock, flags);
  533. ret = wiimote_cmd_acquire(wdata);
  534. if (ret)
  535. return ret;
  536. /* send PIXEL CLOCK ENABLE cmd first */
  537. spin_lock_irqsave(&wdata->state.lock, flags);
  538. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
  539. wiiproto_req_ir1(wdata, 0x06);
  540. spin_unlock_irqrestore(&wdata->state.lock, flags);
  541. ret = wiimote_cmd_wait(wdata);
  542. if (ret)
  543. goto unlock;
  544. if (wdata->state.cmd_err) {
  545. ret = -EIO;
  546. goto unlock;
  547. }
  548. /* enable IR LOGIC */
  549. spin_lock_irqsave(&wdata->state.lock, flags);
  550. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
  551. wiiproto_req_ir2(wdata, 0x06);
  552. spin_unlock_irqrestore(&wdata->state.lock, flags);
  553. ret = wiimote_cmd_wait(wdata);
  554. if (ret)
  555. goto unlock;
  556. if (wdata->state.cmd_err) {
  557. ret = -EIO;
  558. goto unlock;
  559. }
  560. /* enable IR cam but do not make it send data, yet */
  561. ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
  562. sizeof(data_enable));
  563. if (ret)
  564. goto unlock;
  565. /* write first sensitivity block */
  566. ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
  567. sizeof(data_sens1));
  568. if (ret)
  569. goto unlock;
  570. /* write second sensitivity block */
  571. ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
  572. sizeof(data_sens2));
  573. if (ret)
  574. goto unlock;
  575. /* put IR cam into desired state */
  576. switch (mode) {
  577. case WIIPROTO_FLAG_IR_FULL:
  578. format = 5;
  579. break;
  580. case WIIPROTO_FLAG_IR_EXT:
  581. format = 3;
  582. break;
  583. case WIIPROTO_FLAG_IR_BASIC:
  584. format = 1;
  585. break;
  586. }
  587. ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
  588. if (ret)
  589. goto unlock;
  590. /* make IR cam send data */
  591. ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
  592. if (ret)
  593. goto unlock;
  594. /* request new DRM mode compatible to IR mode */
  595. spin_lock_irqsave(&wdata->state.lock, flags);
  596. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  597. wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
  598. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  599. spin_unlock_irqrestore(&wdata->state.lock, flags);
  600. unlock:
  601. wiimote_cmd_release(wdata);
  602. return ret;
  603. }
  604. static int wiimod_ir_open(struct input_dev *dev)
  605. {
  606. struct wiimote_data *wdata = input_get_drvdata(dev);
  607. return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC);
  608. }
  609. static void wiimod_ir_close(struct input_dev *dev)
  610. {
  611. struct wiimote_data *wdata = input_get_drvdata(dev);
  612. wiimod_ir_change(wdata, 0);
  613. }
  614. static int wiimod_ir_probe(const struct wiimod_ops *ops,
  615. struct wiimote_data *wdata)
  616. {
  617. int ret;
  618. wdata->ir = input_allocate_device();
  619. if (!wdata->ir)
  620. return -ENOMEM;
  621. input_set_drvdata(wdata->ir, wdata);
  622. wdata->ir->open = wiimod_ir_open;
  623. wdata->ir->close = wiimod_ir_close;
  624. wdata->ir->dev.parent = &wdata->hdev->dev;
  625. wdata->ir->id.bustype = wdata->hdev->bus;
  626. wdata->ir->id.vendor = wdata->hdev->vendor;
  627. wdata->ir->id.product = wdata->hdev->product;
  628. wdata->ir->id.version = wdata->hdev->version;
  629. wdata->ir->name = WIIMOTE_NAME " IR";
  630. set_bit(EV_ABS, wdata->ir->evbit);
  631. set_bit(ABS_HAT0X, wdata->ir->absbit);
  632. set_bit(ABS_HAT0Y, wdata->ir->absbit);
  633. set_bit(ABS_HAT1X, wdata->ir->absbit);
  634. set_bit(ABS_HAT1Y, wdata->ir->absbit);
  635. set_bit(ABS_HAT2X, wdata->ir->absbit);
  636. set_bit(ABS_HAT2Y, wdata->ir->absbit);
  637. set_bit(ABS_HAT3X, wdata->ir->absbit);
  638. set_bit(ABS_HAT3Y, wdata->ir->absbit);
  639. input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
  640. input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
  641. input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
  642. input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
  643. input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
  644. input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
  645. input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
  646. input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
  647. ret = input_register_device(wdata->ir);
  648. if (ret) {
  649. hid_err(wdata->hdev, "cannot register input device\n");
  650. goto err_free;
  651. }
  652. return 0;
  653. err_free:
  654. input_free_device(wdata->ir);
  655. wdata->ir = NULL;
  656. return ret;
  657. }
  658. static void wiimod_ir_remove(const struct wiimod_ops *ops,
  659. struct wiimote_data *wdata)
  660. {
  661. if (!wdata->ir)
  662. return;
  663. input_unregister_device(wdata->ir);
  664. wdata->ir = NULL;
  665. }
  666. static const struct wiimod_ops wiimod_ir = {
  667. .flags = 0,
  668. .arg = 0,
  669. .probe = wiimod_ir_probe,
  670. .remove = wiimod_ir_remove,
  671. .in_ir = wiimod_ir_in_ir,
  672. };
  673. /* module table */
  674. const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
  675. [WIIMOD_KEYS] = &wiimod_keys,
  676. [WIIMOD_RUMBLE] = &wiimod_rumble,
  677. [WIIMOD_BATTERY] = &wiimod_battery,
  678. [WIIMOD_LED1] = &wiimod_leds[0],
  679. [WIIMOD_LED2] = &wiimod_leds[1],
  680. [WIIMOD_LED3] = &wiimod_leds[2],
  681. [WIIMOD_LED4] = &wiimod_leds[3],
  682. [WIIMOD_ACCEL] = &wiimod_accel,
  683. [WIIMOD_IR] = &wiimod_ir,
  684. };