hid-wiimote-modules.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  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. /*
  674. * Nunchuk Extension
  675. * The Nintendo Wii Nunchuk was the first official extension published by
  676. * Nintendo. It provides two additional keys and a separate accelerometer. It
  677. * can be hotplugged to standard Wii Remotes.
  678. */
  679. enum wiimod_nunchuk_keys {
  680. WIIMOD_NUNCHUK_KEY_C,
  681. WIIMOD_NUNCHUK_KEY_Z,
  682. WIIMOD_NUNCHUK_KEY_NUM,
  683. };
  684. static const __u16 wiimod_nunchuk_map[] = {
  685. BTN_C, /* WIIMOD_NUNCHUK_KEY_C */
  686. BTN_Z, /* WIIMOD_NUNCHUK_KEY_Z */
  687. };
  688. static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext)
  689. {
  690. __s16 x, y, z, bx, by;
  691. /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 |
  692. * -----+----------+---------+---------+----+-----+
  693. * 1 | Button X <7:0> |
  694. * 2 | Button Y <7:0> |
  695. * -----+----------+---------+---------+----+-----+
  696. * 3 | Speed X <9:2> |
  697. * 4 | Speed Y <9:2> |
  698. * 5 | Speed Z <9:2> |
  699. * -----+----------+---------+---------+----+-----+
  700. * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ |
  701. * -----+----------+---------+---------+----+-----+
  702. * Button X/Y is the analog stick. Speed X, Y and Z are the
  703. * accelerometer data in the same format as the wiimote's accelerometer.
  704. * The 6th byte contains the LSBs of the accelerometer data.
  705. * BC and BZ are the C and Z buttons: 0 means pressed
  706. *
  707. * If reported interleaved with motionp, then the layout changes. The
  708. * 5th and 6th byte changes to:
  709. * -----+-----------------------------------+-----+
  710. * 5 | Speed Z <9:3> | EXT |
  711. * -----+--------+-----+-----+----+----+----+-----+
  712. * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 |
  713. * -----+--------+-----+-----+----+----+----+-----+
  714. * All three accelerometer values lose their LSB. The other data is
  715. * still available but slightly moved.
  716. *
  717. * Center data for button values is 128. Center value for accelerometer
  718. * values it 512 / 0x200
  719. */
  720. bx = ext[0];
  721. by = ext[1];
  722. bx -= 128;
  723. by -= 128;
  724. x = ext[2] << 2;
  725. y = ext[3] << 2;
  726. z = ext[4] << 2;
  727. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  728. x |= (ext[5] >> 3) & 0x02;
  729. y |= (ext[5] >> 4) & 0x02;
  730. z &= ~0x4;
  731. z |= (ext[5] >> 5) & 0x06;
  732. } else {
  733. x |= (ext[5] >> 2) & 0x03;
  734. y |= (ext[5] >> 4) & 0x03;
  735. z |= (ext[5] >> 6) & 0x03;
  736. }
  737. x -= 0x200;
  738. y -= 0x200;
  739. z -= 0x200;
  740. input_report_abs(wdata->extension.input, ABS_HAT0X, bx);
  741. input_report_abs(wdata->extension.input, ABS_HAT0Y, by);
  742. input_report_abs(wdata->extension.input, ABS_RX, x);
  743. input_report_abs(wdata->extension.input, ABS_RY, y);
  744. input_report_abs(wdata->extension.input, ABS_RZ, z);
  745. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  746. input_report_key(wdata->extension.input,
  747. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
  748. !(ext[5] & 0x04));
  749. input_report_key(wdata->extension.input,
  750. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
  751. !(ext[5] & 0x08));
  752. } else {
  753. input_report_key(wdata->extension.input,
  754. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
  755. !(ext[5] & 0x01));
  756. input_report_key(wdata->extension.input,
  757. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
  758. !(ext[5] & 0x02));
  759. }
  760. input_sync(wdata->extension.input);
  761. }
  762. static int wiimod_nunchuk_open(struct input_dev *dev)
  763. {
  764. struct wiimote_data *wdata = input_get_drvdata(dev);
  765. unsigned long flags;
  766. spin_lock_irqsave(&wdata->state.lock, flags);
  767. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  768. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  769. spin_unlock_irqrestore(&wdata->state.lock, flags);
  770. return 0;
  771. }
  772. static void wiimod_nunchuk_close(struct input_dev *dev)
  773. {
  774. struct wiimote_data *wdata = input_get_drvdata(dev);
  775. unsigned long flags;
  776. spin_lock_irqsave(&wdata->state.lock, flags);
  777. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  778. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  779. spin_unlock_irqrestore(&wdata->state.lock, flags);
  780. }
  781. static int wiimod_nunchuk_probe(const struct wiimod_ops *ops,
  782. struct wiimote_data *wdata)
  783. {
  784. int ret, i;
  785. wdata->extension.input = input_allocate_device();
  786. if (!wdata->extension.input)
  787. return -ENOMEM;
  788. input_set_drvdata(wdata->extension.input, wdata);
  789. wdata->extension.input->open = wiimod_nunchuk_open;
  790. wdata->extension.input->close = wiimod_nunchuk_close;
  791. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  792. wdata->extension.input->id.bustype = wdata->hdev->bus;
  793. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  794. wdata->extension.input->id.product = wdata->hdev->product;
  795. wdata->extension.input->id.version = wdata->hdev->version;
  796. wdata->extension.input->name = WIIMOTE_NAME " Nunchuk";
  797. set_bit(EV_KEY, wdata->extension.input->evbit);
  798. for (i = 0; i < WIIMOD_NUNCHUK_KEY_NUM; ++i)
  799. set_bit(wiimod_nunchuk_map[i],
  800. wdata->extension.input->keybit);
  801. set_bit(EV_ABS, wdata->extension.input->evbit);
  802. set_bit(ABS_HAT0X, wdata->extension.input->absbit);
  803. set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
  804. input_set_abs_params(wdata->extension.input,
  805. ABS_HAT0X, -120, 120, 2, 4);
  806. input_set_abs_params(wdata->extension.input,
  807. ABS_HAT0Y, -120, 120, 2, 4);
  808. set_bit(ABS_RX, wdata->extension.input->absbit);
  809. set_bit(ABS_RY, wdata->extension.input->absbit);
  810. set_bit(ABS_RZ, wdata->extension.input->absbit);
  811. input_set_abs_params(wdata->extension.input,
  812. ABS_RX, -500, 500, 2, 4);
  813. input_set_abs_params(wdata->extension.input,
  814. ABS_RY, -500, 500, 2, 4);
  815. input_set_abs_params(wdata->extension.input,
  816. ABS_RZ, -500, 500, 2, 4);
  817. ret = input_register_device(wdata->extension.input);
  818. if (ret)
  819. goto err_free;
  820. return 0;
  821. err_free:
  822. input_free_device(wdata->extension.input);
  823. wdata->extension.input = NULL;
  824. return ret;
  825. }
  826. static void wiimod_nunchuk_remove(const struct wiimod_ops *ops,
  827. struct wiimote_data *wdata)
  828. {
  829. if (!wdata->extension.input)
  830. return;
  831. input_unregister_device(wdata->extension.input);
  832. wdata->extension.input = NULL;
  833. }
  834. static const struct wiimod_ops wiimod_nunchuk = {
  835. .flags = 0,
  836. .arg = 0,
  837. .probe = wiimod_nunchuk_probe,
  838. .remove = wiimod_nunchuk_remove,
  839. .in_ext = wiimod_nunchuk_in_ext,
  840. };
  841. /*
  842. * Classic Controller
  843. * Another official extension from Nintendo. It provides a classic
  844. * gamecube-like controller that can be hotplugged on the Wii Remote.
  845. * It has several hardware buttons and switches that are all reported via
  846. * a normal extension device.
  847. */
  848. enum wiimod_classic_keys {
  849. WIIMOD_CLASSIC_KEY_A,
  850. WIIMOD_CLASSIC_KEY_B,
  851. WIIMOD_CLASSIC_KEY_X,
  852. WIIMOD_CLASSIC_KEY_Y,
  853. WIIMOD_CLASSIC_KEY_ZL,
  854. WIIMOD_CLASSIC_KEY_ZR,
  855. WIIMOD_CLASSIC_KEY_PLUS,
  856. WIIMOD_CLASSIC_KEY_MINUS,
  857. WIIMOD_CLASSIC_KEY_HOME,
  858. WIIMOD_CLASSIC_KEY_LEFT,
  859. WIIMOD_CLASSIC_KEY_RIGHT,
  860. WIIMOD_CLASSIC_KEY_UP,
  861. WIIMOD_CLASSIC_KEY_DOWN,
  862. WIIMOD_CLASSIC_KEY_LT,
  863. WIIMOD_CLASSIC_KEY_RT,
  864. WIIMOD_CLASSIC_KEY_NUM,
  865. };
  866. static const __u16 wiimod_classic_map[] = {
  867. BTN_A, /* WIIMOD_CLASSIC_KEY_A */
  868. BTN_B, /* WIIMOD_CLASSIC_KEY_B */
  869. BTN_X, /* WIIMOD_CLASSIC_KEY_X */
  870. BTN_Y, /* WIIMOD_CLASSIC_KEY_Y */
  871. BTN_TL2, /* WIIMOD_CLASSIC_KEY_ZL */
  872. BTN_TR2, /* WIIMOD_CLASSIC_KEY_ZR */
  873. KEY_NEXT, /* WIIMOD_CLASSIC_KEY_PLUS */
  874. KEY_PREVIOUS, /* WIIMOD_CLASSIC_KEY_MINUS */
  875. BTN_MODE, /* WIIMOD_CLASSIC_KEY_HOME */
  876. KEY_LEFT, /* WIIMOD_CLASSIC_KEY_LEFT */
  877. KEY_RIGHT, /* WIIMOD_CLASSIC_KEY_RIGHT */
  878. KEY_UP, /* WIIMOD_CLASSIC_KEY_UP */
  879. KEY_DOWN, /* WIIMOD_CLASSIC_KEY_DOWN */
  880. BTN_TL, /* WIIMOD_CLASSIC_KEY_LT */
  881. BTN_TR, /* WIIMOD_CLASSIC_KEY_RT */
  882. };
  883. static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext)
  884. {
  885. __s8 rx, ry, lx, ly, lt, rt;
  886. /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  887. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  888. * 1 | RX <5:4> | LX <5:0> |
  889. * 2 | RX <3:2> | LY <5:0> |
  890. * -----+-----+-----+-----+-----------------------------+
  891. * 3 |RX<1>| LT <5:4> | RY <5:1> |
  892. * -----+-----+-----------+-----------------------------+
  893. * 4 | LT <3:1> | RT <5:1> |
  894. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  895. * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
  896. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  897. * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
  898. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  899. * All buttons are 0 if pressed
  900. * RX and RY are right analog stick
  901. * LX and LY are left analog stick
  902. * LT is left trigger, RT is right trigger
  903. * BLT is 0 if left trigger is fully pressed
  904. * BRT is 0 if right trigger is fully pressed
  905. * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
  906. * BZL is left Z button and BZR is right Z button
  907. * B-, BH, B+ are +, HOME and - buttons
  908. * BB, BY, BA, BX are A, B, X, Y buttons
  909. * LSB of RX, RY, LT, and RT are not transmitted and always 0.
  910. *
  911. * With motionp enabled it changes slightly to this:
  912. * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  913. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  914. * 1 | RX <5:4> | LX <5:1> | BDU |
  915. * 2 | RX <3:2> | LY <5:1> | BDL |
  916. * -----+-----+-----+-----+-----------------------+-----+
  917. * 3 |RX<1>| LT <5:4> | RY <5:1> |
  918. * -----+-----+-----------+-----------------------------+
  919. * 4 | LT <3:1> | RT <5:1> |
  920. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  921. * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT |
  922. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  923. * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 |
  924. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  925. * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
  926. * is the same as before.
  927. */
  928. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  929. lx = ext[0] & 0x3e;
  930. ly = ext[1] & 0x3e;
  931. } else {
  932. lx = ext[0] & 0x3f;
  933. ly = ext[1] & 0x3f;
  934. }
  935. rx = (ext[0] >> 3) & 0x18;
  936. rx |= (ext[1] >> 5) & 0x06;
  937. rx |= (ext[2] >> 7) & 0x01;
  938. ry = ext[2] & 0x1f;
  939. rt = ext[3] & 0x1f;
  940. lt = (ext[2] >> 2) & 0x18;
  941. lt |= (ext[3] >> 5) & 0x07;
  942. rx <<= 1;
  943. ry <<= 1;
  944. rt <<= 1;
  945. lt <<= 1;
  946. input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20);
  947. input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20);
  948. input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20);
  949. input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20);
  950. input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
  951. input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
  952. input_report_key(wdata->extension.input,
  953. wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
  954. !(ext[4] & 0x80));
  955. input_report_key(wdata->extension.input,
  956. wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
  957. !(ext[4] & 0x40));
  958. input_report_key(wdata->extension.input,
  959. wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT],
  960. !(ext[4] & 0x20));
  961. input_report_key(wdata->extension.input,
  962. wiimod_classic_map[WIIMOD_CLASSIC_KEY_MINUS],
  963. !(ext[4] & 0x10));
  964. input_report_key(wdata->extension.input,
  965. wiimod_classic_map[WIIMOD_CLASSIC_KEY_HOME],
  966. !(ext[4] & 0x08));
  967. input_report_key(wdata->extension.input,
  968. wiimod_classic_map[WIIMOD_CLASSIC_KEY_PLUS],
  969. !(ext[4] & 0x04));
  970. input_report_key(wdata->extension.input,
  971. wiimod_classic_map[WIIMOD_CLASSIC_KEY_RT],
  972. !(ext[4] & 0x02));
  973. input_report_key(wdata->extension.input,
  974. wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZL],
  975. !(ext[5] & 0x80));
  976. input_report_key(wdata->extension.input,
  977. wiimod_classic_map[WIIMOD_CLASSIC_KEY_B],
  978. !(ext[5] & 0x40));
  979. input_report_key(wdata->extension.input,
  980. wiimod_classic_map[WIIMOD_CLASSIC_KEY_Y],
  981. !(ext[5] & 0x20));
  982. input_report_key(wdata->extension.input,
  983. wiimod_classic_map[WIIMOD_CLASSIC_KEY_A],
  984. !(ext[5] & 0x10));
  985. input_report_key(wdata->extension.input,
  986. wiimod_classic_map[WIIMOD_CLASSIC_KEY_X],
  987. !(ext[5] & 0x08));
  988. input_report_key(wdata->extension.input,
  989. wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR],
  990. !(ext[5] & 0x04));
  991. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  992. input_report_key(wdata->extension.input,
  993. wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
  994. !(ext[1] & 0x01));
  995. input_report_key(wdata->extension.input,
  996. wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
  997. !(ext[0] & 0x01));
  998. } else {
  999. input_report_key(wdata->extension.input,
  1000. wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
  1001. !(ext[5] & 0x02));
  1002. input_report_key(wdata->extension.input,
  1003. wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
  1004. !(ext[5] & 0x01));
  1005. }
  1006. input_sync(wdata->extension.input);
  1007. }
  1008. static int wiimod_classic_open(struct input_dev *dev)
  1009. {
  1010. struct wiimote_data *wdata = input_get_drvdata(dev);
  1011. unsigned long flags;
  1012. spin_lock_irqsave(&wdata->state.lock, flags);
  1013. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  1014. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1015. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1016. return 0;
  1017. }
  1018. static void wiimod_classic_close(struct input_dev *dev)
  1019. {
  1020. struct wiimote_data *wdata = input_get_drvdata(dev);
  1021. unsigned long flags;
  1022. spin_lock_irqsave(&wdata->state.lock, flags);
  1023. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  1024. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1025. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1026. }
  1027. static int wiimod_classic_probe(const struct wiimod_ops *ops,
  1028. struct wiimote_data *wdata)
  1029. {
  1030. int ret, i;
  1031. wdata->extension.input = input_allocate_device();
  1032. if (!wdata->extension.input)
  1033. return -ENOMEM;
  1034. input_set_drvdata(wdata->extension.input, wdata);
  1035. wdata->extension.input->open = wiimod_classic_open;
  1036. wdata->extension.input->close = wiimod_classic_close;
  1037. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  1038. wdata->extension.input->id.bustype = wdata->hdev->bus;
  1039. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  1040. wdata->extension.input->id.product = wdata->hdev->product;
  1041. wdata->extension.input->id.version = wdata->hdev->version;
  1042. wdata->extension.input->name = WIIMOTE_NAME " Classic Controller";
  1043. set_bit(EV_KEY, wdata->extension.input->evbit);
  1044. for (i = 0; i < WIIMOD_CLASSIC_KEY_NUM; ++i)
  1045. set_bit(wiimod_classic_map[i],
  1046. wdata->extension.input->keybit);
  1047. set_bit(EV_ABS, wdata->extension.input->evbit);
  1048. set_bit(ABS_HAT1X, wdata->extension.input->absbit);
  1049. set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
  1050. set_bit(ABS_HAT2X, wdata->extension.input->absbit);
  1051. set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
  1052. set_bit(ABS_HAT3X, wdata->extension.input->absbit);
  1053. set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
  1054. input_set_abs_params(wdata->extension.input,
  1055. ABS_HAT1X, -30, 30, 1, 1);
  1056. input_set_abs_params(wdata->extension.input,
  1057. ABS_HAT1Y, -30, 30, 1, 1);
  1058. input_set_abs_params(wdata->extension.input,
  1059. ABS_HAT2X, -30, 30, 1, 1);
  1060. input_set_abs_params(wdata->extension.input,
  1061. ABS_HAT2Y, -30, 30, 1, 1);
  1062. input_set_abs_params(wdata->extension.input,
  1063. ABS_HAT3X, -30, 30, 1, 1);
  1064. input_set_abs_params(wdata->extension.input,
  1065. ABS_HAT3Y, -30, 30, 1, 1);
  1066. ret = input_register_device(wdata->extension.input);
  1067. if (ret)
  1068. goto err_free;
  1069. return 0;
  1070. err_free:
  1071. input_free_device(wdata->extension.input);
  1072. wdata->extension.input = NULL;
  1073. return ret;
  1074. }
  1075. static void wiimod_classic_remove(const struct wiimod_ops *ops,
  1076. struct wiimote_data *wdata)
  1077. {
  1078. if (!wdata->extension.input)
  1079. return;
  1080. input_unregister_device(wdata->extension.input);
  1081. wdata->extension.input = NULL;
  1082. }
  1083. static const struct wiimod_ops wiimod_classic = {
  1084. .flags = 0,
  1085. .arg = 0,
  1086. .probe = wiimod_classic_probe,
  1087. .remove = wiimod_classic_remove,
  1088. .in_ext = wiimod_classic_in_ext,
  1089. };
  1090. /*
  1091. * Balance Board Extension
  1092. * The Nintendo Wii Balance Board provides four hardware weight sensor plus a
  1093. * single push button. No other peripherals are available. However, the
  1094. * balance-board data is sent via a standard Wii Remote extension. All other
  1095. * data for non-present hardware is zeroed out.
  1096. * Some 3rd party devices react allergic if we try to access normal Wii Remote
  1097. * hardware, so this extension module should be the only module that is loaded
  1098. * on balance boards.
  1099. * The balance board needs 8 bytes extension data instead of basic 6 bytes so
  1100. * it needs the WIIMOD_FLAG_EXT8 flag.
  1101. */
  1102. static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys)
  1103. {
  1104. input_report_key(wdata->extension.input, BTN_A,
  1105. !!(keys[1] & 0x08));
  1106. input_sync(wdata->extension.input);
  1107. }
  1108. static void wiimod_bboard_in_ext(struct wiimote_data *wdata,
  1109. const __u8 *ext)
  1110. {
  1111. __s32 val[4], tmp, div;
  1112. unsigned int i;
  1113. struct wiimote_state *s = &wdata->state;
  1114. /*
  1115. * Balance board data layout:
  1116. *
  1117. * Byte | 8 7 6 5 4 3 2 1 |
  1118. * -----+--------------------------+
  1119. * 1 | Top Right <15:8> |
  1120. * 2 | Top Right <7:0> |
  1121. * -----+--------------------------+
  1122. * 3 | Bottom Right <15:8> |
  1123. * 4 | Bottom Right <7:0> |
  1124. * -----+--------------------------+
  1125. * 5 | Top Left <15:8> |
  1126. * 6 | Top Left <7:0> |
  1127. * -----+--------------------------+
  1128. * 7 | Bottom Left <15:8> |
  1129. * 8 | Bottom Left <7:0> |
  1130. * -----+--------------------------+
  1131. *
  1132. * These values represent the weight-measurements of the Wii-balance
  1133. * board with 16bit precision.
  1134. *
  1135. * The balance-board is never reported interleaved with motionp.
  1136. */
  1137. val[0] = ext[0];
  1138. val[0] <<= 8;
  1139. val[0] |= ext[1];
  1140. val[1] = ext[2];
  1141. val[1] <<= 8;
  1142. val[1] |= ext[3];
  1143. val[2] = ext[4];
  1144. val[2] <<= 8;
  1145. val[2] |= ext[5];
  1146. val[3] = ext[6];
  1147. val[3] <<= 8;
  1148. val[3] |= ext[7];
  1149. /* apply calibration data */
  1150. for (i = 0; i < 4; i++) {
  1151. if (val[i] <= s->calib_bboard[i][0]) {
  1152. tmp = 0;
  1153. } else if (val[i] < s->calib_bboard[i][1]) {
  1154. tmp = val[i] - s->calib_bboard[i][0];
  1155. tmp *= 1700;
  1156. div = s->calib_bboard[i][1] - s->calib_bboard[i][0];
  1157. tmp /= div ? div : 1;
  1158. } else {
  1159. tmp = val[i] - s->calib_bboard[i][1];
  1160. tmp *= 1700;
  1161. div = s->calib_bboard[i][2] - s->calib_bboard[i][1];
  1162. tmp /= div ? div : 1;
  1163. tmp += 1700;
  1164. }
  1165. val[i] = tmp;
  1166. }
  1167. input_report_abs(wdata->extension.input, ABS_HAT0X, val[0]);
  1168. input_report_abs(wdata->extension.input, ABS_HAT0Y, val[1]);
  1169. input_report_abs(wdata->extension.input, ABS_HAT1X, val[2]);
  1170. input_report_abs(wdata->extension.input, ABS_HAT1Y, val[3]);
  1171. input_sync(wdata->extension.input);
  1172. }
  1173. static int wiimod_bboard_open(struct input_dev *dev)
  1174. {
  1175. struct wiimote_data *wdata = input_get_drvdata(dev);
  1176. unsigned long flags;
  1177. spin_lock_irqsave(&wdata->state.lock, flags);
  1178. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  1179. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1180. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1181. return 0;
  1182. }
  1183. static void wiimod_bboard_close(struct input_dev *dev)
  1184. {
  1185. struct wiimote_data *wdata = input_get_drvdata(dev);
  1186. unsigned long flags;
  1187. spin_lock_irqsave(&wdata->state.lock, flags);
  1188. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  1189. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1190. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1191. }
  1192. static ssize_t wiimod_bboard_calib_show(struct device *dev,
  1193. struct device_attribute *attr,
  1194. char *out)
  1195. {
  1196. struct wiimote_data *wdata = dev_to_wii(dev);
  1197. int i, j, ret;
  1198. __u16 val;
  1199. __u8 buf[24], offs;
  1200. ret = wiimote_cmd_acquire(wdata);
  1201. if (ret)
  1202. return ret;
  1203. ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
  1204. if (ret != 12) {
  1205. wiimote_cmd_release(wdata);
  1206. return ret < 0 ? ret : -EIO;
  1207. }
  1208. ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
  1209. if (ret != 12) {
  1210. wiimote_cmd_release(wdata);
  1211. return ret < 0 ? ret : -EIO;
  1212. }
  1213. wiimote_cmd_release(wdata);
  1214. spin_lock_irq(&wdata->state.lock);
  1215. offs = 0;
  1216. for (i = 0; i < 3; ++i) {
  1217. for (j = 0; j < 4; ++j) {
  1218. wdata->state.calib_bboard[j][i] = buf[offs];
  1219. wdata->state.calib_bboard[j][i] <<= 8;
  1220. wdata->state.calib_bboard[j][i] |= buf[offs + 1];
  1221. offs += 2;
  1222. }
  1223. }
  1224. spin_unlock_irq(&wdata->state.lock);
  1225. ret = 0;
  1226. for (i = 0; i < 3; ++i) {
  1227. for (j = 0; j < 4; ++j) {
  1228. val = wdata->state.calib_bboard[j][i];
  1229. if (i == 2 && j == 3)
  1230. ret += sprintf(&out[ret], "%04x\n", val);
  1231. else
  1232. ret += sprintf(&out[ret], "%04x:", val);
  1233. }
  1234. }
  1235. return ret;
  1236. }
  1237. static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL);
  1238. static int wiimod_bboard_probe(const struct wiimod_ops *ops,
  1239. struct wiimote_data *wdata)
  1240. {
  1241. int ret, i, j;
  1242. __u8 buf[24], offs;
  1243. wiimote_cmd_acquire_noint(wdata);
  1244. ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
  1245. if (ret != 12) {
  1246. wiimote_cmd_release(wdata);
  1247. return ret < 0 ? ret : -EIO;
  1248. }
  1249. ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
  1250. if (ret != 12) {
  1251. wiimote_cmd_release(wdata);
  1252. return ret < 0 ? ret : -EIO;
  1253. }
  1254. wiimote_cmd_release(wdata);
  1255. offs = 0;
  1256. for (i = 0; i < 3; ++i) {
  1257. for (j = 0; j < 4; ++j) {
  1258. wdata->state.calib_bboard[j][i] = buf[offs];
  1259. wdata->state.calib_bboard[j][i] <<= 8;
  1260. wdata->state.calib_bboard[j][i] |= buf[offs + 1];
  1261. offs += 2;
  1262. }
  1263. }
  1264. wdata->extension.input = input_allocate_device();
  1265. if (!wdata->extension.input)
  1266. return -ENOMEM;
  1267. ret = device_create_file(&wdata->hdev->dev,
  1268. &dev_attr_bboard_calib);
  1269. if (ret) {
  1270. hid_err(wdata->hdev, "cannot create sysfs attribute\n");
  1271. goto err_free;
  1272. }
  1273. input_set_drvdata(wdata->extension.input, wdata);
  1274. wdata->extension.input->open = wiimod_bboard_open;
  1275. wdata->extension.input->close = wiimod_bboard_close;
  1276. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  1277. wdata->extension.input->id.bustype = wdata->hdev->bus;
  1278. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  1279. wdata->extension.input->id.product = wdata->hdev->product;
  1280. wdata->extension.input->id.version = wdata->hdev->version;
  1281. wdata->extension.input->name = WIIMOTE_NAME " Balance Board";
  1282. set_bit(EV_KEY, wdata->extension.input->evbit);
  1283. set_bit(BTN_A, wdata->extension.input->keybit);
  1284. set_bit(EV_ABS, wdata->extension.input->evbit);
  1285. set_bit(ABS_HAT0X, wdata->extension.input->absbit);
  1286. set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
  1287. set_bit(ABS_HAT1X, wdata->extension.input->absbit);
  1288. set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
  1289. input_set_abs_params(wdata->extension.input,
  1290. ABS_HAT0X, 0, 65535, 2, 4);
  1291. input_set_abs_params(wdata->extension.input,
  1292. ABS_HAT0Y, 0, 65535, 2, 4);
  1293. input_set_abs_params(wdata->extension.input,
  1294. ABS_HAT1X, 0, 65535, 2, 4);
  1295. input_set_abs_params(wdata->extension.input,
  1296. ABS_HAT1Y, 0, 65535, 2, 4);
  1297. ret = input_register_device(wdata->extension.input);
  1298. if (ret)
  1299. goto err_file;
  1300. return 0;
  1301. err_file:
  1302. device_remove_file(&wdata->hdev->dev,
  1303. &dev_attr_bboard_calib);
  1304. err_free:
  1305. input_free_device(wdata->extension.input);
  1306. wdata->extension.input = NULL;
  1307. return ret;
  1308. }
  1309. static void wiimod_bboard_remove(const struct wiimod_ops *ops,
  1310. struct wiimote_data *wdata)
  1311. {
  1312. if (!wdata->extension.input)
  1313. return;
  1314. input_unregister_device(wdata->extension.input);
  1315. wdata->extension.input = NULL;
  1316. device_remove_file(&wdata->hdev->dev,
  1317. &dev_attr_bboard_calib);
  1318. }
  1319. static const struct wiimod_ops wiimod_bboard = {
  1320. .flags = WIIMOD_FLAG_EXT8,
  1321. .arg = 0,
  1322. .probe = wiimod_bboard_probe,
  1323. .remove = wiimod_bboard_remove,
  1324. .in_keys = wiimod_bboard_in_keys,
  1325. .in_ext = wiimod_bboard_in_ext,
  1326. };
  1327. /*
  1328. * Pro Controller
  1329. * Released with the Wii U was the Nintendo Wii U Pro Controller. It does not
  1330. * work together with the classic Wii, but only with the new Wii U. However, it
  1331. * uses the same protocol and provides a builtin "classic controller pro"
  1332. * extension, few standard buttons, a rumble motor, 4 LEDs and a battery.
  1333. * We provide all these via a standard extension device as the device doesn't
  1334. * feature an extension port.
  1335. */
  1336. enum wiimod_pro_keys {
  1337. WIIMOD_PRO_KEY_A,
  1338. WIIMOD_PRO_KEY_B,
  1339. WIIMOD_PRO_KEY_X,
  1340. WIIMOD_PRO_KEY_Y,
  1341. WIIMOD_PRO_KEY_PLUS,
  1342. WIIMOD_PRO_KEY_MINUS,
  1343. WIIMOD_PRO_KEY_HOME,
  1344. WIIMOD_PRO_KEY_LEFT,
  1345. WIIMOD_PRO_KEY_RIGHT,
  1346. WIIMOD_PRO_KEY_UP,
  1347. WIIMOD_PRO_KEY_DOWN,
  1348. WIIMOD_PRO_KEY_TL,
  1349. WIIMOD_PRO_KEY_TR,
  1350. WIIMOD_PRO_KEY_ZL,
  1351. WIIMOD_PRO_KEY_ZR,
  1352. WIIMOD_PRO_KEY_THUMBL,
  1353. WIIMOD_PRO_KEY_THUMBR,
  1354. WIIMOD_PRO_KEY_NUM,
  1355. };
  1356. static const __u16 wiimod_pro_map[] = {
  1357. BTN_EAST, /* WIIMOD_PRO_KEY_A */
  1358. BTN_SOUTH, /* WIIMOD_PRO_KEY_B */
  1359. BTN_NORTH, /* WIIMOD_PRO_KEY_X */
  1360. BTN_WEST, /* WIIMOD_PRO_KEY_Y */
  1361. BTN_START, /* WIIMOD_PRO_KEY_PLUS */
  1362. BTN_SELECT, /* WIIMOD_PRO_KEY_MINUS */
  1363. BTN_MODE, /* WIIMOD_PRO_KEY_HOME */
  1364. BTN_DPAD_LEFT, /* WIIMOD_PRO_KEY_LEFT */
  1365. BTN_DPAD_RIGHT, /* WIIMOD_PRO_KEY_RIGHT */
  1366. BTN_DPAD_UP, /* WIIMOD_PRO_KEY_UP */
  1367. BTN_DPAD_DOWN, /* WIIMOD_PRO_KEY_DOWN */
  1368. BTN_TL, /* WIIMOD_PRO_KEY_TL */
  1369. BTN_TR, /* WIIMOD_PRO_KEY_TR */
  1370. BTN_TL2, /* WIIMOD_PRO_KEY_ZL */
  1371. BTN_TR2, /* WIIMOD_PRO_KEY_ZR */
  1372. BTN_THUMBL, /* WIIMOD_PRO_KEY_THUMBL */
  1373. BTN_THUMBR, /* WIIMOD_PRO_KEY_THUMBR */
  1374. };
  1375. static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
  1376. {
  1377. __s16 rx, ry, lx, ly;
  1378. /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  1379. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1380. * 1 | LX <7:0> |
  1381. * -----+-----------------------+-----------------------+
  1382. * 2 | 0 0 0 0 | LX <11:8> |
  1383. * -----+-----------------------+-----------------------+
  1384. * 3 | RX <7:0> |
  1385. * -----+-----------------------+-----------------------+
  1386. * 4 | 0 0 0 0 | RX <11:8> |
  1387. * -----+-----------------------+-----------------------+
  1388. * 5 | LY <7:0> |
  1389. * -----+-----------------------+-----------------------+
  1390. * 6 | 0 0 0 0 | LY <11:8> |
  1391. * -----+-----------------------+-----------------------+
  1392. * 7 | RY <7:0> |
  1393. * -----+-----------------------+-----------------------+
  1394. * 8 | 0 0 0 0 | RY <11:8> |
  1395. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1396. * 9 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
  1397. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1398. * 10 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
  1399. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1400. * 11 | 1 | BATTERY | USB |CHARG|LTHUM|RTHUM|
  1401. * -----+-----+-----------------+-----------+-----+-----+
  1402. * All buttons are low-active (0 if pressed)
  1403. * RX and RY are right analog stick
  1404. * LX and LY are left analog stick
  1405. * BLT is left trigger, BRT is right trigger.
  1406. * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
  1407. * BZL is left Z button and BZR is right Z button
  1408. * B-, BH, B+ are +, HOME and - buttons
  1409. * BB, BY, BA, BX are A, B, X, Y buttons
  1410. *
  1411. * Bits marked as 0/1 are unknown and never changed during tests.
  1412. *
  1413. * Not entirely verified:
  1414. * CHARG: 1 if uncharging, 0 if charging
  1415. * USB: 1 if not connected, 0 if connected
  1416. * BATTERY: battery capacity from 000 (empty) to 100 (full)
  1417. */
  1418. lx = (ext[0] & 0xff) | ((ext[1] & 0x0f) << 8);
  1419. rx = (ext[2] & 0xff) | ((ext[3] & 0x0f) << 8);
  1420. ly = (ext[4] & 0xff) | ((ext[5] & 0x0f) << 8);
  1421. ry = (ext[6] & 0xff) | ((ext[7] & 0x0f) << 8);
  1422. input_report_abs(wdata->extension.input, ABS_X, lx - 0x800);
  1423. input_report_abs(wdata->extension.input, ABS_Y, ly - 0x800);
  1424. input_report_abs(wdata->extension.input, ABS_RX, rx - 0x800);
  1425. input_report_abs(wdata->extension.input, ABS_RY, ry - 0x800);
  1426. input_report_key(wdata->extension.input,
  1427. wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT],
  1428. !(ext[8] & 0x80));
  1429. input_report_key(wdata->extension.input,
  1430. wiimod_pro_map[WIIMOD_PRO_KEY_DOWN],
  1431. !(ext[8] & 0x40));
  1432. input_report_key(wdata->extension.input,
  1433. wiimod_pro_map[WIIMOD_PRO_KEY_TL],
  1434. !(ext[8] & 0x20));
  1435. input_report_key(wdata->extension.input,
  1436. wiimod_pro_map[WIIMOD_PRO_KEY_MINUS],
  1437. !(ext[8] & 0x10));
  1438. input_report_key(wdata->extension.input,
  1439. wiimod_pro_map[WIIMOD_PRO_KEY_HOME],
  1440. !(ext[8] & 0x08));
  1441. input_report_key(wdata->extension.input,
  1442. wiimod_pro_map[WIIMOD_PRO_KEY_PLUS],
  1443. !(ext[8] & 0x04));
  1444. input_report_key(wdata->extension.input,
  1445. wiimod_pro_map[WIIMOD_PRO_KEY_TR],
  1446. !(ext[8] & 0x02));
  1447. input_report_key(wdata->extension.input,
  1448. wiimod_pro_map[WIIMOD_PRO_KEY_ZL],
  1449. !(ext[9] & 0x80));
  1450. input_report_key(wdata->extension.input,
  1451. wiimod_pro_map[WIIMOD_PRO_KEY_B],
  1452. !(ext[9] & 0x40));
  1453. input_report_key(wdata->extension.input,
  1454. wiimod_pro_map[WIIMOD_PRO_KEY_Y],
  1455. !(ext[9] & 0x20));
  1456. input_report_key(wdata->extension.input,
  1457. wiimod_pro_map[WIIMOD_PRO_KEY_A],
  1458. !(ext[9] & 0x10));
  1459. input_report_key(wdata->extension.input,
  1460. wiimod_pro_map[WIIMOD_PRO_KEY_X],
  1461. !(ext[9] & 0x08));
  1462. input_report_key(wdata->extension.input,
  1463. wiimod_pro_map[WIIMOD_PRO_KEY_ZR],
  1464. !(ext[9] & 0x04));
  1465. input_report_key(wdata->extension.input,
  1466. wiimod_pro_map[WIIMOD_PRO_KEY_LEFT],
  1467. !(ext[9] & 0x02));
  1468. input_report_key(wdata->extension.input,
  1469. wiimod_pro_map[WIIMOD_PRO_KEY_UP],
  1470. !(ext[9] & 0x01));
  1471. input_report_key(wdata->extension.input,
  1472. wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL],
  1473. !(ext[10] & 0x02));
  1474. input_report_key(wdata->extension.input,
  1475. wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR],
  1476. !(ext[10] & 0x01));
  1477. input_sync(wdata->extension.input);
  1478. }
  1479. static int wiimod_pro_open(struct input_dev *dev)
  1480. {
  1481. struct wiimote_data *wdata = input_get_drvdata(dev);
  1482. unsigned long flags;
  1483. spin_lock_irqsave(&wdata->state.lock, flags);
  1484. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  1485. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1486. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1487. return 0;
  1488. }
  1489. static void wiimod_pro_close(struct input_dev *dev)
  1490. {
  1491. struct wiimote_data *wdata = input_get_drvdata(dev);
  1492. unsigned long flags;
  1493. spin_lock_irqsave(&wdata->state.lock, flags);
  1494. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  1495. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1496. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1497. }
  1498. static int wiimod_pro_play(struct input_dev *dev, void *data,
  1499. struct ff_effect *eff)
  1500. {
  1501. struct wiimote_data *wdata = input_get_drvdata(dev);
  1502. __u8 value;
  1503. unsigned long flags;
  1504. /*
  1505. * The wiimote supports only a single rumble motor so if any magnitude
  1506. * is set to non-zero then we start the rumble motor. If both are set to
  1507. * zero, we stop the rumble motor.
  1508. */
  1509. if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
  1510. value = 1;
  1511. else
  1512. value = 0;
  1513. spin_lock_irqsave(&wdata->state.lock, flags);
  1514. wiiproto_req_rumble(wdata, value);
  1515. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1516. return 0;
  1517. }
  1518. static int wiimod_pro_probe(const struct wiimod_ops *ops,
  1519. struct wiimote_data *wdata)
  1520. {
  1521. int ret, i;
  1522. wdata->extension.input = input_allocate_device();
  1523. if (!wdata->extension.input)
  1524. return -ENOMEM;
  1525. set_bit(FF_RUMBLE, wdata->extension.input->ffbit);
  1526. input_set_drvdata(wdata->extension.input, wdata);
  1527. if (input_ff_create_memless(wdata->extension.input, NULL,
  1528. wiimod_pro_play)) {
  1529. ret = -ENOMEM;
  1530. goto err_free;
  1531. }
  1532. wdata->extension.input->open = wiimod_pro_open;
  1533. wdata->extension.input->close = wiimod_pro_close;
  1534. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  1535. wdata->extension.input->id.bustype = wdata->hdev->bus;
  1536. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  1537. wdata->extension.input->id.product = wdata->hdev->product;
  1538. wdata->extension.input->id.version = wdata->hdev->version;
  1539. wdata->extension.input->name = WIIMOTE_NAME " Pro Controller";
  1540. set_bit(EV_KEY, wdata->extension.input->evbit);
  1541. for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i)
  1542. set_bit(wiimod_pro_map[i],
  1543. wdata->extension.input->keybit);
  1544. set_bit(EV_ABS, wdata->extension.input->evbit);
  1545. set_bit(ABS_X, wdata->extension.input->absbit);
  1546. set_bit(ABS_Y, wdata->extension.input->absbit);
  1547. set_bit(ABS_RX, wdata->extension.input->absbit);
  1548. set_bit(ABS_RY, wdata->extension.input->absbit);
  1549. input_set_abs_params(wdata->extension.input,
  1550. ABS_X, -0x800, 0x800, 2, 4);
  1551. input_set_abs_params(wdata->extension.input,
  1552. ABS_Y, -0x800, 0x800, 2, 4);
  1553. input_set_abs_params(wdata->extension.input,
  1554. ABS_RX, -0x800, 0x800, 2, 4);
  1555. input_set_abs_params(wdata->extension.input,
  1556. ABS_RY, -0x800, 0x800, 2, 4);
  1557. ret = input_register_device(wdata->extension.input);
  1558. if (ret)
  1559. goto err_free;
  1560. return 0;
  1561. err_free:
  1562. input_free_device(wdata->extension.input);
  1563. wdata->extension.input = NULL;
  1564. return ret;
  1565. }
  1566. static void wiimod_pro_remove(const struct wiimod_ops *ops,
  1567. struct wiimote_data *wdata)
  1568. {
  1569. unsigned long flags;
  1570. if (!wdata->extension.input)
  1571. return;
  1572. spin_lock_irqsave(&wdata->state.lock, flags);
  1573. wiiproto_req_rumble(wdata, 0);
  1574. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1575. input_unregister_device(wdata->extension.input);
  1576. wdata->extension.input = NULL;
  1577. }
  1578. static const struct wiimod_ops wiimod_pro = {
  1579. .flags = WIIMOD_FLAG_EXT16,
  1580. .arg = 0,
  1581. .probe = wiimod_pro_probe,
  1582. .remove = wiimod_pro_remove,
  1583. .in_ext = wiimod_pro_in_ext,
  1584. };
  1585. /*
  1586. * Builtin Motion Plus
  1587. * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which
  1588. * disables polling for Motion-Plus. This should be set only for devices which
  1589. * don't allow MP hotplugging.
  1590. */
  1591. static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops,
  1592. struct wiimote_data *wdata)
  1593. {
  1594. unsigned long flags;
  1595. spin_lock_irqsave(&wdata->state.lock, flags);
  1596. wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
  1597. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1598. return 0;
  1599. }
  1600. static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops,
  1601. struct wiimote_data *wdata)
  1602. {
  1603. unsigned long flags;
  1604. spin_lock_irqsave(&wdata->state.lock, flags);
  1605. wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
  1606. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1607. }
  1608. static const struct wiimod_ops wiimod_builtin_mp = {
  1609. .flags = 0,
  1610. .arg = 0,
  1611. .probe = wiimod_builtin_mp_probe,
  1612. .remove = wiimod_builtin_mp_remove,
  1613. };
  1614. /*
  1615. * No Motion Plus
  1616. * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which
  1617. * disables motion-plus. This is needed for devices that advertise this but we
  1618. * don't know how to use it (or whether it is actually present).
  1619. */
  1620. static int wiimod_no_mp_probe(const struct wiimod_ops *ops,
  1621. struct wiimote_data *wdata)
  1622. {
  1623. unsigned long flags;
  1624. spin_lock_irqsave(&wdata->state.lock, flags);
  1625. wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
  1626. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1627. return 0;
  1628. }
  1629. static void wiimod_no_mp_remove(const struct wiimod_ops *ops,
  1630. struct wiimote_data *wdata)
  1631. {
  1632. unsigned long flags;
  1633. spin_lock_irqsave(&wdata->state.lock, flags);
  1634. wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
  1635. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1636. }
  1637. static const struct wiimod_ops wiimod_no_mp = {
  1638. .flags = 0,
  1639. .arg = 0,
  1640. .probe = wiimod_no_mp_probe,
  1641. .remove = wiimod_no_mp_remove,
  1642. };
  1643. /*
  1644. * Motion Plus
  1645. * The Motion Plus extension provides rotation sensors (gyro) as a small
  1646. * extension device for Wii Remotes. Many devices have them built-in so
  1647. * you cannot see them from the outside.
  1648. * Motion Plus extensions are special because they are on a separate extension
  1649. * port and allow other extensions to be used simultaneously. This is all
  1650. * handled by the Wiimote Core so we don't have to deal with it.
  1651. */
  1652. static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext)
  1653. {
  1654. __s32 x, y, z;
  1655. /* | 8 7 6 5 4 3 | 2 | 1 |
  1656. * -----+------------------------------+-----+-----+
  1657. * 1 | Yaw Speed <7:0> |
  1658. * 2 | Roll Speed <7:0> |
  1659. * 3 | Pitch Speed <7:0> |
  1660. * -----+------------------------------+-----+-----+
  1661. * 4 | Yaw Speed <13:8> | Yaw |Pitch|
  1662. * -----+------------------------------+-----+-----+
  1663. * 5 | Roll Speed <13:8> |Roll | Ext |
  1664. * -----+------------------------------+-----+-----+
  1665. * 6 | Pitch Speed <13:8> | 1 | 0 |
  1666. * -----+------------------------------+-----+-----+
  1667. * The single bits Yaw, Roll, Pitch in the lower right corner specify
  1668. * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
  1669. * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
  1670. * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
  1671. * and 9 for slow.
  1672. * If the wiimote is not rotating the sensor reports 2^13 = 8192.
  1673. * Ext specifies whether an extension is connected to the motionp.
  1674. * which is parsed by wiimote-core.
  1675. */
  1676. x = ext[0];
  1677. y = ext[1];
  1678. z = ext[2];
  1679. x |= (((__u16)ext[3]) << 6) & 0xff00;
  1680. y |= (((__u16)ext[4]) << 6) & 0xff00;
  1681. z |= (((__u16)ext[5]) << 6) & 0xff00;
  1682. x -= 8192;
  1683. y -= 8192;
  1684. z -= 8192;
  1685. if (!(ext[3] & 0x02))
  1686. x *= 18;
  1687. else
  1688. x *= 9;
  1689. if (!(ext[4] & 0x02))
  1690. y *= 18;
  1691. else
  1692. y *= 9;
  1693. if (!(ext[3] & 0x01))
  1694. z *= 18;
  1695. else
  1696. z *= 9;
  1697. input_report_abs(wdata->mp, ABS_RX, x);
  1698. input_report_abs(wdata->mp, ABS_RY, y);
  1699. input_report_abs(wdata->mp, ABS_RZ, z);
  1700. input_sync(wdata->mp);
  1701. }
  1702. static int wiimod_mp_open(struct input_dev *dev)
  1703. {
  1704. struct wiimote_data *wdata = input_get_drvdata(dev);
  1705. unsigned long flags;
  1706. spin_lock_irqsave(&wdata->state.lock, flags);
  1707. wdata->state.flags |= WIIPROTO_FLAG_MP_USED;
  1708. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1709. __wiimote_schedule(wdata);
  1710. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1711. return 0;
  1712. }
  1713. static void wiimod_mp_close(struct input_dev *dev)
  1714. {
  1715. struct wiimote_data *wdata = input_get_drvdata(dev);
  1716. unsigned long flags;
  1717. spin_lock_irqsave(&wdata->state.lock, flags);
  1718. wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
  1719. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1720. __wiimote_schedule(wdata);
  1721. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1722. }
  1723. static int wiimod_mp_probe(const struct wiimod_ops *ops,
  1724. struct wiimote_data *wdata)
  1725. {
  1726. int ret;
  1727. wdata->mp = input_allocate_device();
  1728. if (!wdata->mp)
  1729. return -ENOMEM;
  1730. input_set_drvdata(wdata->mp, wdata);
  1731. wdata->mp->open = wiimod_mp_open;
  1732. wdata->mp->close = wiimod_mp_close;
  1733. wdata->mp->dev.parent = &wdata->hdev->dev;
  1734. wdata->mp->id.bustype = wdata->hdev->bus;
  1735. wdata->mp->id.vendor = wdata->hdev->vendor;
  1736. wdata->mp->id.product = wdata->hdev->product;
  1737. wdata->mp->id.version = wdata->hdev->version;
  1738. wdata->mp->name = WIIMOTE_NAME " Motion Plus";
  1739. set_bit(EV_ABS, wdata->mp->evbit);
  1740. set_bit(ABS_RX, wdata->mp->absbit);
  1741. set_bit(ABS_RY, wdata->mp->absbit);
  1742. set_bit(ABS_RZ, wdata->mp->absbit);
  1743. input_set_abs_params(wdata->mp,
  1744. ABS_RX, -16000, 16000, 4, 8);
  1745. input_set_abs_params(wdata->mp,
  1746. ABS_RY, -16000, 16000, 4, 8);
  1747. input_set_abs_params(wdata->mp,
  1748. ABS_RZ, -16000, 16000, 4, 8);
  1749. ret = input_register_device(wdata->mp);
  1750. if (ret)
  1751. goto err_free;
  1752. return 0;
  1753. err_free:
  1754. input_free_device(wdata->mp);
  1755. wdata->mp = NULL;
  1756. return ret;
  1757. }
  1758. static void wiimod_mp_remove(const struct wiimod_ops *ops,
  1759. struct wiimote_data *wdata)
  1760. {
  1761. if (!wdata->mp)
  1762. return;
  1763. input_unregister_device(wdata->mp);
  1764. wdata->mp = NULL;
  1765. }
  1766. const struct wiimod_ops wiimod_mp = {
  1767. .flags = 0,
  1768. .arg = 0,
  1769. .probe = wiimod_mp_probe,
  1770. .remove = wiimod_mp_remove,
  1771. .in_mp = wiimod_mp_in_mp,
  1772. };
  1773. /* module table */
  1774. static const struct wiimod_ops wiimod_dummy;
  1775. const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
  1776. [WIIMOD_KEYS] = &wiimod_keys,
  1777. [WIIMOD_RUMBLE] = &wiimod_rumble,
  1778. [WIIMOD_BATTERY] = &wiimod_battery,
  1779. [WIIMOD_LED1] = &wiimod_leds[0],
  1780. [WIIMOD_LED2] = &wiimod_leds[1],
  1781. [WIIMOD_LED3] = &wiimod_leds[2],
  1782. [WIIMOD_LED4] = &wiimod_leds[3],
  1783. [WIIMOD_ACCEL] = &wiimod_accel,
  1784. [WIIMOD_IR] = &wiimod_ir,
  1785. [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp,
  1786. [WIIMOD_NO_MP] = &wiimod_no_mp,
  1787. };
  1788. const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = {
  1789. [WIIMOTE_EXT_NONE] = &wiimod_dummy,
  1790. [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy,
  1791. [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk,
  1792. [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic,
  1793. [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard,
  1794. [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro,
  1795. };