hid-wiimote-modules.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  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. /* used by wiimod_rumble and wiipro_rumble */
  111. static void wiimod_rumble_worker(struct work_struct *work)
  112. {
  113. struct wiimote_data *wdata = container_of(work, struct wiimote_data,
  114. rumble_worker);
  115. spin_lock_irq(&wdata->state.lock);
  116. wiiproto_req_rumble(wdata, wdata->state.cache_rumble);
  117. spin_unlock_irq(&wdata->state.lock);
  118. }
  119. static int wiimod_rumble_play(struct input_dev *dev, void *data,
  120. struct ff_effect *eff)
  121. {
  122. struct wiimote_data *wdata = input_get_drvdata(dev);
  123. __u8 value;
  124. /*
  125. * The wiimote supports only a single rumble motor so if any magnitude
  126. * is set to non-zero then we start the rumble motor. If both are set to
  127. * zero, we stop the rumble motor.
  128. */
  129. if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
  130. value = 1;
  131. else
  132. value = 0;
  133. /* Locking state.lock here might deadlock with input_event() calls.
  134. * schedule_work acts as barrier. Merging multiple changes is fine. */
  135. wdata->state.cache_rumble = value;
  136. schedule_work(&wdata->rumble_worker);
  137. return 0;
  138. }
  139. static int wiimod_rumble_probe(const struct wiimod_ops *ops,
  140. struct wiimote_data *wdata)
  141. {
  142. INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
  143. set_bit(FF_RUMBLE, wdata->input->ffbit);
  144. if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
  145. return -ENOMEM;
  146. return 0;
  147. }
  148. static void wiimod_rumble_remove(const struct wiimod_ops *ops,
  149. struct wiimote_data *wdata)
  150. {
  151. unsigned long flags;
  152. cancel_work_sync(&wdata->rumble_worker);
  153. spin_lock_irqsave(&wdata->state.lock, flags);
  154. wiiproto_req_rumble(wdata, 0);
  155. spin_unlock_irqrestore(&wdata->state.lock, flags);
  156. }
  157. static const struct wiimod_ops wiimod_rumble = {
  158. .flags = WIIMOD_FLAG_INPUT,
  159. .arg = 0,
  160. .probe = wiimod_rumble_probe,
  161. .remove = wiimod_rumble_remove,
  162. };
  163. /*
  164. * Battery
  165. * 1 byte of battery capacity information is sent along every protocol status
  166. * report. The wiimote core caches it but we try to update it on every
  167. * user-space request.
  168. * This is supported by nearly every device so it's almost always enabled.
  169. */
  170. static enum power_supply_property wiimod_battery_props[] = {
  171. POWER_SUPPLY_PROP_CAPACITY,
  172. POWER_SUPPLY_PROP_SCOPE,
  173. };
  174. static int wiimod_battery_get_property(struct power_supply *psy,
  175. enum power_supply_property psp,
  176. union power_supply_propval *val)
  177. {
  178. struct wiimote_data *wdata = power_supply_get_drvdata(psy);
  179. int ret = 0, state;
  180. unsigned long flags;
  181. if (psp == POWER_SUPPLY_PROP_SCOPE) {
  182. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  183. return 0;
  184. } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
  185. return -EINVAL;
  186. }
  187. ret = wiimote_cmd_acquire(wdata);
  188. if (ret)
  189. return ret;
  190. spin_lock_irqsave(&wdata->state.lock, flags);
  191. wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
  192. wiiproto_req_status(wdata);
  193. spin_unlock_irqrestore(&wdata->state.lock, flags);
  194. wiimote_cmd_wait(wdata);
  195. wiimote_cmd_release(wdata);
  196. spin_lock_irqsave(&wdata->state.lock, flags);
  197. state = wdata->state.cmd_battery;
  198. spin_unlock_irqrestore(&wdata->state.lock, flags);
  199. val->intval = state * 100 / 255;
  200. return ret;
  201. }
  202. static int wiimod_battery_probe(const struct wiimod_ops *ops,
  203. struct wiimote_data *wdata)
  204. {
  205. struct power_supply_config psy_cfg = { .drv_data = wdata, };
  206. int ret;
  207. wdata->battery_desc.properties = wiimod_battery_props;
  208. wdata->battery_desc.num_properties = ARRAY_SIZE(wiimod_battery_props);
  209. wdata->battery_desc.get_property = wiimod_battery_get_property;
  210. wdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  211. wdata->battery_desc.use_for_apm = 0;
  212. wdata->battery_desc.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
  213. wdata->hdev->uniq);
  214. if (!wdata->battery_desc.name)
  215. return -ENOMEM;
  216. wdata->battery = power_supply_register(&wdata->hdev->dev,
  217. &wdata->battery_desc,
  218. &psy_cfg);
  219. if (IS_ERR(wdata->battery)) {
  220. hid_err(wdata->hdev, "cannot register battery device\n");
  221. ret = PTR_ERR(wdata->battery);
  222. goto err_free;
  223. }
  224. power_supply_powers(wdata->battery, &wdata->hdev->dev);
  225. return 0;
  226. err_free:
  227. kfree(wdata->battery_desc.name);
  228. wdata->battery_desc.name = NULL;
  229. return ret;
  230. }
  231. static void wiimod_battery_remove(const struct wiimod_ops *ops,
  232. struct wiimote_data *wdata)
  233. {
  234. if (!wdata->battery_desc.name)
  235. return;
  236. power_supply_unregister(wdata->battery);
  237. kfree(wdata->battery_desc.name);
  238. wdata->battery_desc.name = NULL;
  239. }
  240. static const struct wiimod_ops wiimod_battery = {
  241. .flags = 0,
  242. .arg = 0,
  243. .probe = wiimod_battery_probe,
  244. .remove = wiimod_battery_remove,
  245. };
  246. /*
  247. * LED
  248. * 0 to 4 player LEDs are supported by devices. The "arg" field of the
  249. * wiimod_ops structure specifies which LED this module controls. This allows
  250. * to register a limited number of LEDs.
  251. * State is managed by wiimote core.
  252. */
  253. static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
  254. {
  255. struct wiimote_data *wdata;
  256. struct device *dev = led_dev->dev->parent;
  257. int i;
  258. unsigned long flags;
  259. bool value = false;
  260. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  261. for (i = 0; i < 4; ++i) {
  262. if (wdata->leds[i] == led_dev) {
  263. spin_lock_irqsave(&wdata->state.lock, flags);
  264. value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
  265. spin_unlock_irqrestore(&wdata->state.lock, flags);
  266. break;
  267. }
  268. }
  269. return value ? LED_FULL : LED_OFF;
  270. }
  271. static void wiimod_led_set(struct led_classdev *led_dev,
  272. enum led_brightness value)
  273. {
  274. struct wiimote_data *wdata;
  275. struct device *dev = led_dev->dev->parent;
  276. int i;
  277. unsigned long flags;
  278. __u8 state, flag;
  279. wdata = hid_get_drvdata(container_of(dev, struct hid_device, dev));
  280. for (i = 0; i < 4; ++i) {
  281. if (wdata->leds[i] == led_dev) {
  282. flag = WIIPROTO_FLAG_LED(i + 1);
  283. spin_lock_irqsave(&wdata->state.lock, flags);
  284. state = wdata->state.flags;
  285. if (value == LED_OFF)
  286. wiiproto_req_leds(wdata, state & ~flag);
  287. else
  288. wiiproto_req_leds(wdata, state | flag);
  289. spin_unlock_irqrestore(&wdata->state.lock, flags);
  290. break;
  291. }
  292. }
  293. }
  294. static int wiimod_led_probe(const struct wiimod_ops *ops,
  295. struct wiimote_data *wdata)
  296. {
  297. struct device *dev = &wdata->hdev->dev;
  298. size_t namesz = strlen(dev_name(dev)) + 9;
  299. struct led_classdev *led;
  300. unsigned long flags;
  301. char *name;
  302. int ret;
  303. led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
  304. if (!led)
  305. return -ENOMEM;
  306. name = (void*)&led[1];
  307. snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
  308. led->name = name;
  309. led->brightness = 0;
  310. led->max_brightness = 1;
  311. led->brightness_get = wiimod_led_get;
  312. led->brightness_set = wiimod_led_set;
  313. wdata->leds[ops->arg] = led;
  314. ret = led_classdev_register(dev, led);
  315. if (ret)
  316. goto err_free;
  317. /* enable LED1 to stop initial LED-blinking */
  318. if (ops->arg == 0) {
  319. spin_lock_irqsave(&wdata->state.lock, flags);
  320. wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
  321. spin_unlock_irqrestore(&wdata->state.lock, flags);
  322. }
  323. return 0;
  324. err_free:
  325. wdata->leds[ops->arg] = NULL;
  326. kfree(led);
  327. return ret;
  328. }
  329. static void wiimod_led_remove(const struct wiimod_ops *ops,
  330. struct wiimote_data *wdata)
  331. {
  332. if (!wdata->leds[ops->arg])
  333. return;
  334. led_classdev_unregister(wdata->leds[ops->arg]);
  335. kfree(wdata->leds[ops->arg]);
  336. wdata->leds[ops->arg] = NULL;
  337. }
  338. static const struct wiimod_ops wiimod_leds[4] = {
  339. {
  340. .flags = 0,
  341. .arg = 0,
  342. .probe = wiimod_led_probe,
  343. .remove = wiimod_led_remove,
  344. },
  345. {
  346. .flags = 0,
  347. .arg = 1,
  348. .probe = wiimod_led_probe,
  349. .remove = wiimod_led_remove,
  350. },
  351. {
  352. .flags = 0,
  353. .arg = 2,
  354. .probe = wiimod_led_probe,
  355. .remove = wiimod_led_remove,
  356. },
  357. {
  358. .flags = 0,
  359. .arg = 3,
  360. .probe = wiimod_led_probe,
  361. .remove = wiimod_led_remove,
  362. },
  363. };
  364. /*
  365. * Accelerometer
  366. * 3 axis accelerometer data is part of nearly all DRMs. If not supported by a
  367. * device, it's mostly cleared to 0. This module parses this data and provides
  368. * it via a separate input device.
  369. */
  370. static void wiimod_accel_in_accel(struct wiimote_data *wdata,
  371. const __u8 *accel)
  372. {
  373. __u16 x, y, z;
  374. if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
  375. return;
  376. /*
  377. * payload is: BB BB XX YY ZZ
  378. * Accelerometer data is encoded into 3 10bit values. XX, YY and ZZ
  379. * contain the upper 8 bits of each value. The lower 2 bits are
  380. * contained in the buttons data BB BB.
  381. * Bits 6 and 7 of the first buttons byte BB is the lower 2 bits of the
  382. * X accel value. Bit 5 of the second buttons byte is the 2nd bit of Y
  383. * accel value and bit 6 is the second bit of the Z value.
  384. * The first bit of Y and Z values is not available and always set to 0.
  385. * 0x200 is returned on no movement.
  386. */
  387. x = accel[2] << 2;
  388. y = accel[3] << 2;
  389. z = accel[4] << 2;
  390. x |= (accel[0] >> 5) & 0x3;
  391. y |= (accel[1] >> 4) & 0x2;
  392. z |= (accel[1] >> 5) & 0x2;
  393. input_report_abs(wdata->accel, ABS_RX, x - 0x200);
  394. input_report_abs(wdata->accel, ABS_RY, y - 0x200);
  395. input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
  396. input_sync(wdata->accel);
  397. }
  398. static int wiimod_accel_open(struct input_dev *dev)
  399. {
  400. struct wiimote_data *wdata = input_get_drvdata(dev);
  401. unsigned long flags;
  402. spin_lock_irqsave(&wdata->state.lock, flags);
  403. wiiproto_req_accel(wdata, true);
  404. spin_unlock_irqrestore(&wdata->state.lock, flags);
  405. return 0;
  406. }
  407. static void wiimod_accel_close(struct input_dev *dev)
  408. {
  409. struct wiimote_data *wdata = input_get_drvdata(dev);
  410. unsigned long flags;
  411. spin_lock_irqsave(&wdata->state.lock, flags);
  412. wiiproto_req_accel(wdata, false);
  413. spin_unlock_irqrestore(&wdata->state.lock, flags);
  414. }
  415. static int wiimod_accel_probe(const struct wiimod_ops *ops,
  416. struct wiimote_data *wdata)
  417. {
  418. int ret;
  419. wdata->accel = input_allocate_device();
  420. if (!wdata->accel)
  421. return -ENOMEM;
  422. input_set_drvdata(wdata->accel, wdata);
  423. wdata->accel->open = wiimod_accel_open;
  424. wdata->accel->close = wiimod_accel_close;
  425. wdata->accel->dev.parent = &wdata->hdev->dev;
  426. wdata->accel->id.bustype = wdata->hdev->bus;
  427. wdata->accel->id.vendor = wdata->hdev->vendor;
  428. wdata->accel->id.product = wdata->hdev->product;
  429. wdata->accel->id.version = wdata->hdev->version;
  430. wdata->accel->name = WIIMOTE_NAME " Accelerometer";
  431. set_bit(EV_ABS, wdata->accel->evbit);
  432. set_bit(ABS_RX, wdata->accel->absbit);
  433. set_bit(ABS_RY, wdata->accel->absbit);
  434. set_bit(ABS_RZ, wdata->accel->absbit);
  435. input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
  436. input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
  437. input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
  438. ret = input_register_device(wdata->accel);
  439. if (ret) {
  440. hid_err(wdata->hdev, "cannot register input device\n");
  441. goto err_free;
  442. }
  443. return 0;
  444. err_free:
  445. input_free_device(wdata->accel);
  446. wdata->accel = NULL;
  447. return ret;
  448. }
  449. static void wiimod_accel_remove(const struct wiimod_ops *ops,
  450. struct wiimote_data *wdata)
  451. {
  452. if (!wdata->accel)
  453. return;
  454. input_unregister_device(wdata->accel);
  455. wdata->accel = NULL;
  456. }
  457. static const struct wiimod_ops wiimod_accel = {
  458. .flags = 0,
  459. .arg = 0,
  460. .probe = wiimod_accel_probe,
  461. .remove = wiimod_accel_remove,
  462. .in_accel = wiimod_accel_in_accel,
  463. };
  464. /*
  465. * IR Cam
  466. * Up to 4 IR sources can be tracked by a normal Wii Remote. The IR cam needs
  467. * to be initialized with a fairly complex procedure and consumes a lot of
  468. * power. Therefore, as long as no application uses the IR input device, it is
  469. * kept offline.
  470. * Nearly no other device than the normal Wii Remotes supports the IR cam so
  471. * you can disable this module for these devices.
  472. */
  473. static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir,
  474. bool packed, unsigned int id)
  475. {
  476. __u16 x, y;
  477. __u8 xid, yid;
  478. bool sync = false;
  479. if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
  480. return;
  481. switch (id) {
  482. case 0:
  483. xid = ABS_HAT0X;
  484. yid = ABS_HAT0Y;
  485. break;
  486. case 1:
  487. xid = ABS_HAT1X;
  488. yid = ABS_HAT1Y;
  489. break;
  490. case 2:
  491. xid = ABS_HAT2X;
  492. yid = ABS_HAT2Y;
  493. break;
  494. case 3:
  495. xid = ABS_HAT3X;
  496. yid = ABS_HAT3Y;
  497. sync = true;
  498. break;
  499. default:
  500. return;
  501. }
  502. /*
  503. * Basic IR data is encoded into 3 bytes. The first two bytes are the
  504. * lower 8 bit of the X/Y data, the 3rd byte contains the upper 2 bits
  505. * of both.
  506. * If data is packed, then the 3rd byte is put first and slightly
  507. * reordered. This allows to interleave packed and non-packed data to
  508. * have two IR sets in 5 bytes instead of 6.
  509. * The resulting 10bit X/Y values are passed to the ABS_HAT? input dev.
  510. */
  511. if (packed) {
  512. x = ir[1] | ((ir[0] & 0x03) << 8);
  513. y = ir[2] | ((ir[0] & 0x0c) << 6);
  514. } else {
  515. x = ir[0] | ((ir[2] & 0x30) << 4);
  516. y = ir[1] | ((ir[2] & 0xc0) << 2);
  517. }
  518. input_report_abs(wdata->ir, xid, x);
  519. input_report_abs(wdata->ir, yid, y);
  520. if (sync)
  521. input_sync(wdata->ir);
  522. }
  523. static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode)
  524. {
  525. int ret;
  526. unsigned long flags;
  527. __u8 format = 0;
  528. static const __u8 data_enable[] = { 0x01 };
  529. static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
  530. 0x00, 0xaa, 0x00, 0x64 };
  531. static const __u8 data_sens2[] = { 0x63, 0x03 };
  532. static const __u8 data_fin[] = { 0x08 };
  533. spin_lock_irqsave(&wdata->state.lock, flags);
  534. if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
  535. spin_unlock_irqrestore(&wdata->state.lock, flags);
  536. return 0;
  537. }
  538. if (mode == 0) {
  539. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  540. wiiproto_req_ir1(wdata, 0);
  541. wiiproto_req_ir2(wdata, 0);
  542. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  543. spin_unlock_irqrestore(&wdata->state.lock, flags);
  544. return 0;
  545. }
  546. spin_unlock_irqrestore(&wdata->state.lock, flags);
  547. ret = wiimote_cmd_acquire(wdata);
  548. if (ret)
  549. return ret;
  550. /* send PIXEL CLOCK ENABLE cmd first */
  551. spin_lock_irqsave(&wdata->state.lock, flags);
  552. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
  553. wiiproto_req_ir1(wdata, 0x06);
  554. spin_unlock_irqrestore(&wdata->state.lock, flags);
  555. ret = wiimote_cmd_wait(wdata);
  556. if (ret)
  557. goto unlock;
  558. if (wdata->state.cmd_err) {
  559. ret = -EIO;
  560. goto unlock;
  561. }
  562. /* enable IR LOGIC */
  563. spin_lock_irqsave(&wdata->state.lock, flags);
  564. wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
  565. wiiproto_req_ir2(wdata, 0x06);
  566. spin_unlock_irqrestore(&wdata->state.lock, flags);
  567. ret = wiimote_cmd_wait(wdata);
  568. if (ret)
  569. goto unlock;
  570. if (wdata->state.cmd_err) {
  571. ret = -EIO;
  572. goto unlock;
  573. }
  574. /* enable IR cam but do not make it send data, yet */
  575. ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
  576. sizeof(data_enable));
  577. if (ret)
  578. goto unlock;
  579. /* write first sensitivity block */
  580. ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
  581. sizeof(data_sens1));
  582. if (ret)
  583. goto unlock;
  584. /* write second sensitivity block */
  585. ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
  586. sizeof(data_sens2));
  587. if (ret)
  588. goto unlock;
  589. /* put IR cam into desired state */
  590. switch (mode) {
  591. case WIIPROTO_FLAG_IR_FULL:
  592. format = 5;
  593. break;
  594. case WIIPROTO_FLAG_IR_EXT:
  595. format = 3;
  596. break;
  597. case WIIPROTO_FLAG_IR_BASIC:
  598. format = 1;
  599. break;
  600. }
  601. ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
  602. if (ret)
  603. goto unlock;
  604. /* make IR cam send data */
  605. ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
  606. if (ret)
  607. goto unlock;
  608. /* request new DRM mode compatible to IR mode */
  609. spin_lock_irqsave(&wdata->state.lock, flags);
  610. wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
  611. wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
  612. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  613. spin_unlock_irqrestore(&wdata->state.lock, flags);
  614. unlock:
  615. wiimote_cmd_release(wdata);
  616. return ret;
  617. }
  618. static int wiimod_ir_open(struct input_dev *dev)
  619. {
  620. struct wiimote_data *wdata = input_get_drvdata(dev);
  621. return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC);
  622. }
  623. static void wiimod_ir_close(struct input_dev *dev)
  624. {
  625. struct wiimote_data *wdata = input_get_drvdata(dev);
  626. wiimod_ir_change(wdata, 0);
  627. }
  628. static int wiimod_ir_probe(const struct wiimod_ops *ops,
  629. struct wiimote_data *wdata)
  630. {
  631. int ret;
  632. wdata->ir = input_allocate_device();
  633. if (!wdata->ir)
  634. return -ENOMEM;
  635. input_set_drvdata(wdata->ir, wdata);
  636. wdata->ir->open = wiimod_ir_open;
  637. wdata->ir->close = wiimod_ir_close;
  638. wdata->ir->dev.parent = &wdata->hdev->dev;
  639. wdata->ir->id.bustype = wdata->hdev->bus;
  640. wdata->ir->id.vendor = wdata->hdev->vendor;
  641. wdata->ir->id.product = wdata->hdev->product;
  642. wdata->ir->id.version = wdata->hdev->version;
  643. wdata->ir->name = WIIMOTE_NAME " IR";
  644. set_bit(EV_ABS, wdata->ir->evbit);
  645. set_bit(ABS_HAT0X, wdata->ir->absbit);
  646. set_bit(ABS_HAT0Y, wdata->ir->absbit);
  647. set_bit(ABS_HAT1X, wdata->ir->absbit);
  648. set_bit(ABS_HAT1Y, wdata->ir->absbit);
  649. set_bit(ABS_HAT2X, wdata->ir->absbit);
  650. set_bit(ABS_HAT2Y, wdata->ir->absbit);
  651. set_bit(ABS_HAT3X, wdata->ir->absbit);
  652. set_bit(ABS_HAT3Y, wdata->ir->absbit);
  653. input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
  654. input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
  655. input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
  656. input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
  657. input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
  658. input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
  659. input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
  660. input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
  661. ret = input_register_device(wdata->ir);
  662. if (ret) {
  663. hid_err(wdata->hdev, "cannot register input device\n");
  664. goto err_free;
  665. }
  666. return 0;
  667. err_free:
  668. input_free_device(wdata->ir);
  669. wdata->ir = NULL;
  670. return ret;
  671. }
  672. static void wiimod_ir_remove(const struct wiimod_ops *ops,
  673. struct wiimote_data *wdata)
  674. {
  675. if (!wdata->ir)
  676. return;
  677. input_unregister_device(wdata->ir);
  678. wdata->ir = NULL;
  679. }
  680. static const struct wiimod_ops wiimod_ir = {
  681. .flags = 0,
  682. .arg = 0,
  683. .probe = wiimod_ir_probe,
  684. .remove = wiimod_ir_remove,
  685. .in_ir = wiimod_ir_in_ir,
  686. };
  687. /*
  688. * Nunchuk Extension
  689. * The Nintendo Wii Nunchuk was the first official extension published by
  690. * Nintendo. It provides two additional keys and a separate accelerometer. It
  691. * can be hotplugged to standard Wii Remotes.
  692. */
  693. enum wiimod_nunchuk_keys {
  694. WIIMOD_NUNCHUK_KEY_C,
  695. WIIMOD_NUNCHUK_KEY_Z,
  696. WIIMOD_NUNCHUK_KEY_NUM,
  697. };
  698. static const __u16 wiimod_nunchuk_map[] = {
  699. BTN_C, /* WIIMOD_NUNCHUK_KEY_C */
  700. BTN_Z, /* WIIMOD_NUNCHUK_KEY_Z */
  701. };
  702. static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext)
  703. {
  704. __s16 x, y, z, bx, by;
  705. /* Byte | 8 7 | 6 5 | 4 3 | 2 | 1 |
  706. * -----+----------+---------+---------+----+-----+
  707. * 1 | Button X <7:0> |
  708. * 2 | Button Y <7:0> |
  709. * -----+----------+---------+---------+----+-----+
  710. * 3 | Speed X <9:2> |
  711. * 4 | Speed Y <9:2> |
  712. * 5 | Speed Z <9:2> |
  713. * -----+----------+---------+---------+----+-----+
  714. * 6 | Z <1:0> | Y <1:0> | X <1:0> | BC | BZ |
  715. * -----+----------+---------+---------+----+-----+
  716. * Button X/Y is the analog stick. Speed X, Y and Z are the
  717. * accelerometer data in the same format as the wiimote's accelerometer.
  718. * The 6th byte contains the LSBs of the accelerometer data.
  719. * BC and BZ are the C and Z buttons: 0 means pressed
  720. *
  721. * If reported interleaved with motionp, then the layout changes. The
  722. * 5th and 6th byte changes to:
  723. * -----+-----------------------------------+-----+
  724. * 5 | Speed Z <9:3> | EXT |
  725. * -----+--------+-----+-----+----+----+----+-----+
  726. * 6 |Z <2:1> |Y <1>|X <1>| BC | BZ | 0 | 0 |
  727. * -----+--------+-----+-----+----+----+----+-----+
  728. * All three accelerometer values lose their LSB. The other data is
  729. * still available but slightly moved.
  730. *
  731. * Center data for button values is 128. Center value for accelerometer
  732. * values it 512 / 0x200
  733. */
  734. bx = ext[0];
  735. by = ext[1];
  736. bx -= 128;
  737. by -= 128;
  738. x = ext[2] << 2;
  739. y = ext[3] << 2;
  740. z = ext[4] << 2;
  741. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  742. x |= (ext[5] >> 3) & 0x02;
  743. y |= (ext[5] >> 4) & 0x02;
  744. z &= ~0x4;
  745. z |= (ext[5] >> 5) & 0x06;
  746. } else {
  747. x |= (ext[5] >> 2) & 0x03;
  748. y |= (ext[5] >> 4) & 0x03;
  749. z |= (ext[5] >> 6) & 0x03;
  750. }
  751. x -= 0x200;
  752. y -= 0x200;
  753. z -= 0x200;
  754. input_report_abs(wdata->extension.input, ABS_HAT0X, bx);
  755. input_report_abs(wdata->extension.input, ABS_HAT0Y, by);
  756. input_report_abs(wdata->extension.input, ABS_RX, x);
  757. input_report_abs(wdata->extension.input, ABS_RY, y);
  758. input_report_abs(wdata->extension.input, ABS_RZ, z);
  759. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  760. input_report_key(wdata->extension.input,
  761. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
  762. !(ext[5] & 0x04));
  763. input_report_key(wdata->extension.input,
  764. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
  765. !(ext[5] & 0x08));
  766. } else {
  767. input_report_key(wdata->extension.input,
  768. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
  769. !(ext[5] & 0x01));
  770. input_report_key(wdata->extension.input,
  771. wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
  772. !(ext[5] & 0x02));
  773. }
  774. input_sync(wdata->extension.input);
  775. }
  776. static int wiimod_nunchuk_open(struct input_dev *dev)
  777. {
  778. struct wiimote_data *wdata = input_get_drvdata(dev);
  779. unsigned long flags;
  780. spin_lock_irqsave(&wdata->state.lock, flags);
  781. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  782. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  783. spin_unlock_irqrestore(&wdata->state.lock, flags);
  784. return 0;
  785. }
  786. static void wiimod_nunchuk_close(struct input_dev *dev)
  787. {
  788. struct wiimote_data *wdata = input_get_drvdata(dev);
  789. unsigned long flags;
  790. spin_lock_irqsave(&wdata->state.lock, flags);
  791. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  792. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  793. spin_unlock_irqrestore(&wdata->state.lock, flags);
  794. }
  795. static int wiimod_nunchuk_probe(const struct wiimod_ops *ops,
  796. struct wiimote_data *wdata)
  797. {
  798. int ret, i;
  799. wdata->extension.input = input_allocate_device();
  800. if (!wdata->extension.input)
  801. return -ENOMEM;
  802. input_set_drvdata(wdata->extension.input, wdata);
  803. wdata->extension.input->open = wiimod_nunchuk_open;
  804. wdata->extension.input->close = wiimod_nunchuk_close;
  805. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  806. wdata->extension.input->id.bustype = wdata->hdev->bus;
  807. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  808. wdata->extension.input->id.product = wdata->hdev->product;
  809. wdata->extension.input->id.version = wdata->hdev->version;
  810. wdata->extension.input->name = WIIMOTE_NAME " Nunchuk";
  811. set_bit(EV_KEY, wdata->extension.input->evbit);
  812. for (i = 0; i < WIIMOD_NUNCHUK_KEY_NUM; ++i)
  813. set_bit(wiimod_nunchuk_map[i],
  814. wdata->extension.input->keybit);
  815. set_bit(EV_ABS, wdata->extension.input->evbit);
  816. set_bit(ABS_HAT0X, wdata->extension.input->absbit);
  817. set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
  818. input_set_abs_params(wdata->extension.input,
  819. ABS_HAT0X, -120, 120, 2, 4);
  820. input_set_abs_params(wdata->extension.input,
  821. ABS_HAT0Y, -120, 120, 2, 4);
  822. set_bit(ABS_RX, wdata->extension.input->absbit);
  823. set_bit(ABS_RY, wdata->extension.input->absbit);
  824. set_bit(ABS_RZ, wdata->extension.input->absbit);
  825. input_set_abs_params(wdata->extension.input,
  826. ABS_RX, -500, 500, 2, 4);
  827. input_set_abs_params(wdata->extension.input,
  828. ABS_RY, -500, 500, 2, 4);
  829. input_set_abs_params(wdata->extension.input,
  830. ABS_RZ, -500, 500, 2, 4);
  831. ret = input_register_device(wdata->extension.input);
  832. if (ret)
  833. goto err_free;
  834. return 0;
  835. err_free:
  836. input_free_device(wdata->extension.input);
  837. wdata->extension.input = NULL;
  838. return ret;
  839. }
  840. static void wiimod_nunchuk_remove(const struct wiimod_ops *ops,
  841. struct wiimote_data *wdata)
  842. {
  843. if (!wdata->extension.input)
  844. return;
  845. input_unregister_device(wdata->extension.input);
  846. wdata->extension.input = NULL;
  847. }
  848. static const struct wiimod_ops wiimod_nunchuk = {
  849. .flags = 0,
  850. .arg = 0,
  851. .probe = wiimod_nunchuk_probe,
  852. .remove = wiimod_nunchuk_remove,
  853. .in_ext = wiimod_nunchuk_in_ext,
  854. };
  855. /*
  856. * Classic Controller
  857. * Another official extension from Nintendo. It provides a classic
  858. * gamecube-like controller that can be hotplugged on the Wii Remote.
  859. * It has several hardware buttons and switches that are all reported via
  860. * a normal extension device.
  861. */
  862. enum wiimod_classic_keys {
  863. WIIMOD_CLASSIC_KEY_A,
  864. WIIMOD_CLASSIC_KEY_B,
  865. WIIMOD_CLASSIC_KEY_X,
  866. WIIMOD_CLASSIC_KEY_Y,
  867. WIIMOD_CLASSIC_KEY_ZL,
  868. WIIMOD_CLASSIC_KEY_ZR,
  869. WIIMOD_CLASSIC_KEY_PLUS,
  870. WIIMOD_CLASSIC_KEY_MINUS,
  871. WIIMOD_CLASSIC_KEY_HOME,
  872. WIIMOD_CLASSIC_KEY_LEFT,
  873. WIIMOD_CLASSIC_KEY_RIGHT,
  874. WIIMOD_CLASSIC_KEY_UP,
  875. WIIMOD_CLASSIC_KEY_DOWN,
  876. WIIMOD_CLASSIC_KEY_LT,
  877. WIIMOD_CLASSIC_KEY_RT,
  878. WIIMOD_CLASSIC_KEY_NUM,
  879. };
  880. static const __u16 wiimod_classic_map[] = {
  881. BTN_A, /* WIIMOD_CLASSIC_KEY_A */
  882. BTN_B, /* WIIMOD_CLASSIC_KEY_B */
  883. BTN_X, /* WIIMOD_CLASSIC_KEY_X */
  884. BTN_Y, /* WIIMOD_CLASSIC_KEY_Y */
  885. BTN_TL2, /* WIIMOD_CLASSIC_KEY_ZL */
  886. BTN_TR2, /* WIIMOD_CLASSIC_KEY_ZR */
  887. KEY_NEXT, /* WIIMOD_CLASSIC_KEY_PLUS */
  888. KEY_PREVIOUS, /* WIIMOD_CLASSIC_KEY_MINUS */
  889. BTN_MODE, /* WIIMOD_CLASSIC_KEY_HOME */
  890. KEY_LEFT, /* WIIMOD_CLASSIC_KEY_LEFT */
  891. KEY_RIGHT, /* WIIMOD_CLASSIC_KEY_RIGHT */
  892. KEY_UP, /* WIIMOD_CLASSIC_KEY_UP */
  893. KEY_DOWN, /* WIIMOD_CLASSIC_KEY_DOWN */
  894. BTN_TL, /* WIIMOD_CLASSIC_KEY_LT */
  895. BTN_TR, /* WIIMOD_CLASSIC_KEY_RT */
  896. };
  897. static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext)
  898. {
  899. __s8 rx, ry, lx, ly, lt, rt;
  900. /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  901. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  902. * 1 | RX <5:4> | LX <5:0> |
  903. * 2 | RX <3:2> | LY <5:0> |
  904. * -----+-----+-----+-----+-----------------------------+
  905. * 3 |RX<1>| LT <5:4> | RY <5:1> |
  906. * -----+-----+-----------+-----------------------------+
  907. * 4 | LT <3:1> | RT <5:1> |
  908. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  909. * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
  910. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  911. * 6 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
  912. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  913. * All buttons are 0 if pressed
  914. * RX and RY are right analog stick
  915. * LX and LY are left analog stick
  916. * LT is left trigger, RT is right trigger
  917. * BLT is 0 if left trigger is fully pressed
  918. * BRT is 0 if right trigger is fully pressed
  919. * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
  920. * BZL is left Z button and BZR is right Z button
  921. * B-, BH, B+ are +, HOME and - buttons
  922. * BB, BY, BA, BX are A, B, X, Y buttons
  923. * LSB of RX, RY, LT, and RT are not transmitted and always 0.
  924. *
  925. * With motionp enabled it changes slightly to this:
  926. * Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  927. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  928. * 1 | RX <5:4> | LX <5:1> | BDU |
  929. * 2 | RX <3:2> | LY <5:1> | BDL |
  930. * -----+-----+-----+-----+-----------------------+-----+
  931. * 3 |RX<1>| LT <5:4> | RY <5:1> |
  932. * -----+-----+-----------+-----------------------------+
  933. * 4 | LT <3:1> | RT <5:1> |
  934. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  935. * 5 | BDR | BDD | BLT | B- | BH | B+ | BRT | EXT |
  936. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  937. * 6 | BZL | BB | BY | BA | BX | BZR | 0 | 0 |
  938. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  939. * Only the LSBs of LX and LY are lost. BDU and BDL are moved, the rest
  940. * is the same as before.
  941. */
  942. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  943. lx = ext[0] & 0x3e;
  944. ly = ext[1] & 0x3e;
  945. } else {
  946. lx = ext[0] & 0x3f;
  947. ly = ext[1] & 0x3f;
  948. }
  949. rx = (ext[0] >> 3) & 0x18;
  950. rx |= (ext[1] >> 5) & 0x06;
  951. rx |= (ext[2] >> 7) & 0x01;
  952. ry = ext[2] & 0x1f;
  953. rt = ext[3] & 0x1f;
  954. lt = (ext[2] >> 2) & 0x18;
  955. lt |= (ext[3] >> 5) & 0x07;
  956. rx <<= 1;
  957. ry <<= 1;
  958. rt <<= 1;
  959. lt <<= 1;
  960. input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20);
  961. input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20);
  962. input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20);
  963. input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20);
  964. input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
  965. input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
  966. input_report_key(wdata->extension.input,
  967. wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
  968. !(ext[4] & 0x80));
  969. input_report_key(wdata->extension.input,
  970. wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
  971. !(ext[4] & 0x40));
  972. input_report_key(wdata->extension.input,
  973. wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT],
  974. !(ext[4] & 0x20));
  975. input_report_key(wdata->extension.input,
  976. wiimod_classic_map[WIIMOD_CLASSIC_KEY_MINUS],
  977. !(ext[4] & 0x10));
  978. input_report_key(wdata->extension.input,
  979. wiimod_classic_map[WIIMOD_CLASSIC_KEY_HOME],
  980. !(ext[4] & 0x08));
  981. input_report_key(wdata->extension.input,
  982. wiimod_classic_map[WIIMOD_CLASSIC_KEY_PLUS],
  983. !(ext[4] & 0x04));
  984. input_report_key(wdata->extension.input,
  985. wiimod_classic_map[WIIMOD_CLASSIC_KEY_RT],
  986. !(ext[4] & 0x02));
  987. input_report_key(wdata->extension.input,
  988. wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZL],
  989. !(ext[5] & 0x80));
  990. input_report_key(wdata->extension.input,
  991. wiimod_classic_map[WIIMOD_CLASSIC_KEY_B],
  992. !(ext[5] & 0x40));
  993. input_report_key(wdata->extension.input,
  994. wiimod_classic_map[WIIMOD_CLASSIC_KEY_Y],
  995. !(ext[5] & 0x20));
  996. input_report_key(wdata->extension.input,
  997. wiimod_classic_map[WIIMOD_CLASSIC_KEY_A],
  998. !(ext[5] & 0x10));
  999. input_report_key(wdata->extension.input,
  1000. wiimod_classic_map[WIIMOD_CLASSIC_KEY_X],
  1001. !(ext[5] & 0x08));
  1002. input_report_key(wdata->extension.input,
  1003. wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR],
  1004. !(ext[5] & 0x04));
  1005. if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
  1006. input_report_key(wdata->extension.input,
  1007. wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
  1008. !(ext[1] & 0x01));
  1009. input_report_key(wdata->extension.input,
  1010. wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
  1011. !(ext[0] & 0x01));
  1012. } else {
  1013. input_report_key(wdata->extension.input,
  1014. wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
  1015. !(ext[5] & 0x02));
  1016. input_report_key(wdata->extension.input,
  1017. wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
  1018. !(ext[5] & 0x01));
  1019. }
  1020. input_sync(wdata->extension.input);
  1021. }
  1022. static int wiimod_classic_open(struct input_dev *dev)
  1023. {
  1024. struct wiimote_data *wdata = input_get_drvdata(dev);
  1025. unsigned long flags;
  1026. spin_lock_irqsave(&wdata->state.lock, flags);
  1027. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  1028. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1029. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1030. return 0;
  1031. }
  1032. static void wiimod_classic_close(struct input_dev *dev)
  1033. {
  1034. struct wiimote_data *wdata = input_get_drvdata(dev);
  1035. unsigned long flags;
  1036. spin_lock_irqsave(&wdata->state.lock, flags);
  1037. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  1038. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1039. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1040. }
  1041. static int wiimod_classic_probe(const struct wiimod_ops *ops,
  1042. struct wiimote_data *wdata)
  1043. {
  1044. int ret, i;
  1045. wdata->extension.input = input_allocate_device();
  1046. if (!wdata->extension.input)
  1047. return -ENOMEM;
  1048. input_set_drvdata(wdata->extension.input, wdata);
  1049. wdata->extension.input->open = wiimod_classic_open;
  1050. wdata->extension.input->close = wiimod_classic_close;
  1051. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  1052. wdata->extension.input->id.bustype = wdata->hdev->bus;
  1053. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  1054. wdata->extension.input->id.product = wdata->hdev->product;
  1055. wdata->extension.input->id.version = wdata->hdev->version;
  1056. wdata->extension.input->name = WIIMOTE_NAME " Classic Controller";
  1057. set_bit(EV_KEY, wdata->extension.input->evbit);
  1058. for (i = 0; i < WIIMOD_CLASSIC_KEY_NUM; ++i)
  1059. set_bit(wiimod_classic_map[i],
  1060. wdata->extension.input->keybit);
  1061. set_bit(EV_ABS, wdata->extension.input->evbit);
  1062. set_bit(ABS_HAT1X, wdata->extension.input->absbit);
  1063. set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
  1064. set_bit(ABS_HAT2X, wdata->extension.input->absbit);
  1065. set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
  1066. set_bit(ABS_HAT3X, wdata->extension.input->absbit);
  1067. set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
  1068. input_set_abs_params(wdata->extension.input,
  1069. ABS_HAT1X, -30, 30, 1, 1);
  1070. input_set_abs_params(wdata->extension.input,
  1071. ABS_HAT1Y, -30, 30, 1, 1);
  1072. input_set_abs_params(wdata->extension.input,
  1073. ABS_HAT2X, -30, 30, 1, 1);
  1074. input_set_abs_params(wdata->extension.input,
  1075. ABS_HAT2Y, -30, 30, 1, 1);
  1076. input_set_abs_params(wdata->extension.input,
  1077. ABS_HAT3X, -30, 30, 1, 1);
  1078. input_set_abs_params(wdata->extension.input,
  1079. ABS_HAT3Y, -30, 30, 1, 1);
  1080. ret = input_register_device(wdata->extension.input);
  1081. if (ret)
  1082. goto err_free;
  1083. return 0;
  1084. err_free:
  1085. input_free_device(wdata->extension.input);
  1086. wdata->extension.input = NULL;
  1087. return ret;
  1088. }
  1089. static void wiimod_classic_remove(const struct wiimod_ops *ops,
  1090. struct wiimote_data *wdata)
  1091. {
  1092. if (!wdata->extension.input)
  1093. return;
  1094. input_unregister_device(wdata->extension.input);
  1095. wdata->extension.input = NULL;
  1096. }
  1097. static const struct wiimod_ops wiimod_classic = {
  1098. .flags = 0,
  1099. .arg = 0,
  1100. .probe = wiimod_classic_probe,
  1101. .remove = wiimod_classic_remove,
  1102. .in_ext = wiimod_classic_in_ext,
  1103. };
  1104. /*
  1105. * Balance Board Extension
  1106. * The Nintendo Wii Balance Board provides four hardware weight sensor plus a
  1107. * single push button. No other peripherals are available. However, the
  1108. * balance-board data is sent via a standard Wii Remote extension. All other
  1109. * data for non-present hardware is zeroed out.
  1110. * Some 3rd party devices react allergic if we try to access normal Wii Remote
  1111. * hardware, so this extension module should be the only module that is loaded
  1112. * on balance boards.
  1113. * The balance board needs 8 bytes extension data instead of basic 6 bytes so
  1114. * it needs the WIIMOD_FLAG_EXT8 flag.
  1115. */
  1116. static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys)
  1117. {
  1118. input_report_key(wdata->extension.input, BTN_A,
  1119. !!(keys[1] & 0x08));
  1120. input_sync(wdata->extension.input);
  1121. }
  1122. static void wiimod_bboard_in_ext(struct wiimote_data *wdata,
  1123. const __u8 *ext)
  1124. {
  1125. __s32 val[4], tmp, div;
  1126. unsigned int i;
  1127. struct wiimote_state *s = &wdata->state;
  1128. /*
  1129. * Balance board data layout:
  1130. *
  1131. * Byte | 8 7 6 5 4 3 2 1 |
  1132. * -----+--------------------------+
  1133. * 1 | Top Right <15:8> |
  1134. * 2 | Top Right <7:0> |
  1135. * -----+--------------------------+
  1136. * 3 | Bottom Right <15:8> |
  1137. * 4 | Bottom Right <7:0> |
  1138. * -----+--------------------------+
  1139. * 5 | Top Left <15:8> |
  1140. * 6 | Top Left <7:0> |
  1141. * -----+--------------------------+
  1142. * 7 | Bottom Left <15:8> |
  1143. * 8 | Bottom Left <7:0> |
  1144. * -----+--------------------------+
  1145. *
  1146. * These values represent the weight-measurements of the Wii-balance
  1147. * board with 16bit precision.
  1148. *
  1149. * The balance-board is never reported interleaved with motionp.
  1150. */
  1151. val[0] = ext[0];
  1152. val[0] <<= 8;
  1153. val[0] |= ext[1];
  1154. val[1] = ext[2];
  1155. val[1] <<= 8;
  1156. val[1] |= ext[3];
  1157. val[2] = ext[4];
  1158. val[2] <<= 8;
  1159. val[2] |= ext[5];
  1160. val[3] = ext[6];
  1161. val[3] <<= 8;
  1162. val[3] |= ext[7];
  1163. /* apply calibration data */
  1164. for (i = 0; i < 4; i++) {
  1165. if (val[i] <= s->calib_bboard[i][0]) {
  1166. tmp = 0;
  1167. } else if (val[i] < s->calib_bboard[i][1]) {
  1168. tmp = val[i] - s->calib_bboard[i][0];
  1169. tmp *= 1700;
  1170. div = s->calib_bboard[i][1] - s->calib_bboard[i][0];
  1171. tmp /= div ? div : 1;
  1172. } else {
  1173. tmp = val[i] - s->calib_bboard[i][1];
  1174. tmp *= 1700;
  1175. div = s->calib_bboard[i][2] - s->calib_bboard[i][1];
  1176. tmp /= div ? div : 1;
  1177. tmp += 1700;
  1178. }
  1179. val[i] = tmp;
  1180. }
  1181. input_report_abs(wdata->extension.input, ABS_HAT0X, val[0]);
  1182. input_report_abs(wdata->extension.input, ABS_HAT0Y, val[1]);
  1183. input_report_abs(wdata->extension.input, ABS_HAT1X, val[2]);
  1184. input_report_abs(wdata->extension.input, ABS_HAT1Y, val[3]);
  1185. input_sync(wdata->extension.input);
  1186. }
  1187. static int wiimod_bboard_open(struct input_dev *dev)
  1188. {
  1189. struct wiimote_data *wdata = input_get_drvdata(dev);
  1190. unsigned long flags;
  1191. spin_lock_irqsave(&wdata->state.lock, flags);
  1192. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  1193. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1194. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1195. return 0;
  1196. }
  1197. static void wiimod_bboard_close(struct input_dev *dev)
  1198. {
  1199. struct wiimote_data *wdata = input_get_drvdata(dev);
  1200. unsigned long flags;
  1201. spin_lock_irqsave(&wdata->state.lock, flags);
  1202. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  1203. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1204. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1205. }
  1206. static ssize_t wiimod_bboard_calib_show(struct device *dev,
  1207. struct device_attribute *attr,
  1208. char *out)
  1209. {
  1210. struct wiimote_data *wdata = dev_to_wii(dev);
  1211. int i, j, ret;
  1212. __u16 val;
  1213. __u8 buf[24], offs;
  1214. ret = wiimote_cmd_acquire(wdata);
  1215. if (ret)
  1216. return ret;
  1217. ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
  1218. if (ret != 12) {
  1219. wiimote_cmd_release(wdata);
  1220. return ret < 0 ? ret : -EIO;
  1221. }
  1222. ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
  1223. if (ret != 12) {
  1224. wiimote_cmd_release(wdata);
  1225. return ret < 0 ? ret : -EIO;
  1226. }
  1227. wiimote_cmd_release(wdata);
  1228. spin_lock_irq(&wdata->state.lock);
  1229. offs = 0;
  1230. for (i = 0; i < 3; ++i) {
  1231. for (j = 0; j < 4; ++j) {
  1232. wdata->state.calib_bboard[j][i] = buf[offs];
  1233. wdata->state.calib_bboard[j][i] <<= 8;
  1234. wdata->state.calib_bboard[j][i] |= buf[offs + 1];
  1235. offs += 2;
  1236. }
  1237. }
  1238. spin_unlock_irq(&wdata->state.lock);
  1239. ret = 0;
  1240. for (i = 0; i < 3; ++i) {
  1241. for (j = 0; j < 4; ++j) {
  1242. val = wdata->state.calib_bboard[j][i];
  1243. if (i == 2 && j == 3)
  1244. ret += sprintf(&out[ret], "%04x\n", val);
  1245. else
  1246. ret += sprintf(&out[ret], "%04x:", val);
  1247. }
  1248. }
  1249. return ret;
  1250. }
  1251. static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL);
  1252. static int wiimod_bboard_probe(const struct wiimod_ops *ops,
  1253. struct wiimote_data *wdata)
  1254. {
  1255. int ret, i, j;
  1256. __u8 buf[24], offs;
  1257. wiimote_cmd_acquire_noint(wdata);
  1258. ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
  1259. if (ret != 12) {
  1260. wiimote_cmd_release(wdata);
  1261. return ret < 0 ? ret : -EIO;
  1262. }
  1263. ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
  1264. if (ret != 12) {
  1265. wiimote_cmd_release(wdata);
  1266. return ret < 0 ? ret : -EIO;
  1267. }
  1268. wiimote_cmd_release(wdata);
  1269. offs = 0;
  1270. for (i = 0; i < 3; ++i) {
  1271. for (j = 0; j < 4; ++j) {
  1272. wdata->state.calib_bboard[j][i] = buf[offs];
  1273. wdata->state.calib_bboard[j][i] <<= 8;
  1274. wdata->state.calib_bboard[j][i] |= buf[offs + 1];
  1275. offs += 2;
  1276. }
  1277. }
  1278. wdata->extension.input = input_allocate_device();
  1279. if (!wdata->extension.input)
  1280. return -ENOMEM;
  1281. ret = device_create_file(&wdata->hdev->dev,
  1282. &dev_attr_bboard_calib);
  1283. if (ret) {
  1284. hid_err(wdata->hdev, "cannot create sysfs attribute\n");
  1285. goto err_free;
  1286. }
  1287. input_set_drvdata(wdata->extension.input, wdata);
  1288. wdata->extension.input->open = wiimod_bboard_open;
  1289. wdata->extension.input->close = wiimod_bboard_close;
  1290. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  1291. wdata->extension.input->id.bustype = wdata->hdev->bus;
  1292. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  1293. wdata->extension.input->id.product = wdata->hdev->product;
  1294. wdata->extension.input->id.version = wdata->hdev->version;
  1295. wdata->extension.input->name = WIIMOTE_NAME " Balance Board";
  1296. set_bit(EV_KEY, wdata->extension.input->evbit);
  1297. set_bit(BTN_A, wdata->extension.input->keybit);
  1298. set_bit(EV_ABS, wdata->extension.input->evbit);
  1299. set_bit(ABS_HAT0X, wdata->extension.input->absbit);
  1300. set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
  1301. set_bit(ABS_HAT1X, wdata->extension.input->absbit);
  1302. set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
  1303. input_set_abs_params(wdata->extension.input,
  1304. ABS_HAT0X, 0, 65535, 2, 4);
  1305. input_set_abs_params(wdata->extension.input,
  1306. ABS_HAT0Y, 0, 65535, 2, 4);
  1307. input_set_abs_params(wdata->extension.input,
  1308. ABS_HAT1X, 0, 65535, 2, 4);
  1309. input_set_abs_params(wdata->extension.input,
  1310. ABS_HAT1Y, 0, 65535, 2, 4);
  1311. ret = input_register_device(wdata->extension.input);
  1312. if (ret)
  1313. goto err_file;
  1314. return 0;
  1315. err_file:
  1316. device_remove_file(&wdata->hdev->dev,
  1317. &dev_attr_bboard_calib);
  1318. err_free:
  1319. input_free_device(wdata->extension.input);
  1320. wdata->extension.input = NULL;
  1321. return ret;
  1322. }
  1323. static void wiimod_bboard_remove(const struct wiimod_ops *ops,
  1324. struct wiimote_data *wdata)
  1325. {
  1326. if (!wdata->extension.input)
  1327. return;
  1328. input_unregister_device(wdata->extension.input);
  1329. wdata->extension.input = NULL;
  1330. device_remove_file(&wdata->hdev->dev,
  1331. &dev_attr_bboard_calib);
  1332. }
  1333. static const struct wiimod_ops wiimod_bboard = {
  1334. .flags = WIIMOD_FLAG_EXT8,
  1335. .arg = 0,
  1336. .probe = wiimod_bboard_probe,
  1337. .remove = wiimod_bboard_remove,
  1338. .in_keys = wiimod_bboard_in_keys,
  1339. .in_ext = wiimod_bboard_in_ext,
  1340. };
  1341. /*
  1342. * Pro Controller
  1343. * Released with the Wii U was the Nintendo Wii U Pro Controller. It does not
  1344. * work together with the classic Wii, but only with the new Wii U. However, it
  1345. * uses the same protocol and provides a builtin "classic controller pro"
  1346. * extension, few standard buttons, a rumble motor, 4 LEDs and a battery.
  1347. * We provide all these via a standard extension device as the device doesn't
  1348. * feature an extension port.
  1349. */
  1350. enum wiimod_pro_keys {
  1351. WIIMOD_PRO_KEY_A,
  1352. WIIMOD_PRO_KEY_B,
  1353. WIIMOD_PRO_KEY_X,
  1354. WIIMOD_PRO_KEY_Y,
  1355. WIIMOD_PRO_KEY_PLUS,
  1356. WIIMOD_PRO_KEY_MINUS,
  1357. WIIMOD_PRO_KEY_HOME,
  1358. WIIMOD_PRO_KEY_LEFT,
  1359. WIIMOD_PRO_KEY_RIGHT,
  1360. WIIMOD_PRO_KEY_UP,
  1361. WIIMOD_PRO_KEY_DOWN,
  1362. WIIMOD_PRO_KEY_TL,
  1363. WIIMOD_PRO_KEY_TR,
  1364. WIIMOD_PRO_KEY_ZL,
  1365. WIIMOD_PRO_KEY_ZR,
  1366. WIIMOD_PRO_KEY_THUMBL,
  1367. WIIMOD_PRO_KEY_THUMBR,
  1368. WIIMOD_PRO_KEY_NUM,
  1369. };
  1370. static const __u16 wiimod_pro_map[] = {
  1371. BTN_EAST, /* WIIMOD_PRO_KEY_A */
  1372. BTN_SOUTH, /* WIIMOD_PRO_KEY_B */
  1373. BTN_NORTH, /* WIIMOD_PRO_KEY_X */
  1374. BTN_WEST, /* WIIMOD_PRO_KEY_Y */
  1375. BTN_START, /* WIIMOD_PRO_KEY_PLUS */
  1376. BTN_SELECT, /* WIIMOD_PRO_KEY_MINUS */
  1377. BTN_MODE, /* WIIMOD_PRO_KEY_HOME */
  1378. BTN_DPAD_LEFT, /* WIIMOD_PRO_KEY_LEFT */
  1379. BTN_DPAD_RIGHT, /* WIIMOD_PRO_KEY_RIGHT */
  1380. BTN_DPAD_UP, /* WIIMOD_PRO_KEY_UP */
  1381. BTN_DPAD_DOWN, /* WIIMOD_PRO_KEY_DOWN */
  1382. BTN_TL, /* WIIMOD_PRO_KEY_TL */
  1383. BTN_TR, /* WIIMOD_PRO_KEY_TR */
  1384. BTN_TL2, /* WIIMOD_PRO_KEY_ZL */
  1385. BTN_TR2, /* WIIMOD_PRO_KEY_ZR */
  1386. BTN_THUMBL, /* WIIMOD_PRO_KEY_THUMBL */
  1387. BTN_THUMBR, /* WIIMOD_PRO_KEY_THUMBR */
  1388. };
  1389. static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
  1390. {
  1391. __s16 rx, ry, lx, ly;
  1392. /* Byte | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
  1393. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1394. * 1 | LX <7:0> |
  1395. * -----+-----------------------+-----------------------+
  1396. * 2 | 0 0 0 0 | LX <11:8> |
  1397. * -----+-----------------------+-----------------------+
  1398. * 3 | RX <7:0> |
  1399. * -----+-----------------------+-----------------------+
  1400. * 4 | 0 0 0 0 | RX <11:8> |
  1401. * -----+-----------------------+-----------------------+
  1402. * 5 | LY <7:0> |
  1403. * -----+-----------------------+-----------------------+
  1404. * 6 | 0 0 0 0 | LY <11:8> |
  1405. * -----+-----------------------+-----------------------+
  1406. * 7 | RY <7:0> |
  1407. * -----+-----------------------+-----------------------+
  1408. * 8 | 0 0 0 0 | RY <11:8> |
  1409. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1410. * 9 | BDR | BDD | BLT | B- | BH | B+ | BRT | 1 |
  1411. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1412. * 10 | BZL | BB | BY | BA | BX | BZR | BDL | BDU |
  1413. * -----+-----+-----+-----+-----+-----+-----+-----+-----+
  1414. * 11 | 1 | BATTERY | USB |CHARG|LTHUM|RTHUM|
  1415. * -----+-----+-----------------+-----------+-----+-----+
  1416. * All buttons are low-active (0 if pressed)
  1417. * RX and RY are right analog stick
  1418. * LX and LY are left analog stick
  1419. * BLT is left trigger, BRT is right trigger.
  1420. * BDR, BDD, BDL, BDU form the D-Pad with right, down, left, up buttons
  1421. * BZL is left Z button and BZR is right Z button
  1422. * B-, BH, B+ are +, HOME and - buttons
  1423. * BB, BY, BA, BX are A, B, X, Y buttons
  1424. *
  1425. * Bits marked as 0/1 are unknown and never changed during tests.
  1426. *
  1427. * Not entirely verified:
  1428. * CHARG: 1 if uncharging, 0 if charging
  1429. * USB: 1 if not connected, 0 if connected
  1430. * BATTERY: battery capacity from 000 (empty) to 100 (full)
  1431. */
  1432. lx = (ext[0] & 0xff) | ((ext[1] & 0x0f) << 8);
  1433. rx = (ext[2] & 0xff) | ((ext[3] & 0x0f) << 8);
  1434. ly = (ext[4] & 0xff) | ((ext[5] & 0x0f) << 8);
  1435. ry = (ext[6] & 0xff) | ((ext[7] & 0x0f) << 8);
  1436. /* zero-point offsets */
  1437. lx -= 0x800;
  1438. ly = 0x800 - ly;
  1439. rx -= 0x800;
  1440. ry = 0x800 - ry;
  1441. /* Trivial automatic calibration. We don't know any calibration data
  1442. * in the EEPROM so we must use the first report to calibrate the
  1443. * null-position of the analog sticks. Users can retrigger calibration
  1444. * via sysfs, or set it explicitly. If data is off more than abs(500),
  1445. * we skip calibration as the sticks are likely to be moved already. */
  1446. if (!(wdata->state.flags & WIIPROTO_FLAG_PRO_CALIB_DONE)) {
  1447. wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
  1448. if (abs(lx) < 500)
  1449. wdata->state.calib_pro_sticks[0] = -lx;
  1450. if (abs(ly) < 500)
  1451. wdata->state.calib_pro_sticks[1] = -ly;
  1452. if (abs(rx) < 500)
  1453. wdata->state.calib_pro_sticks[2] = -rx;
  1454. if (abs(ry) < 500)
  1455. wdata->state.calib_pro_sticks[3] = -ry;
  1456. }
  1457. /* apply calibration data */
  1458. lx += wdata->state.calib_pro_sticks[0];
  1459. ly += wdata->state.calib_pro_sticks[1];
  1460. rx += wdata->state.calib_pro_sticks[2];
  1461. ry += wdata->state.calib_pro_sticks[3];
  1462. input_report_abs(wdata->extension.input, ABS_X, lx);
  1463. input_report_abs(wdata->extension.input, ABS_Y, ly);
  1464. input_report_abs(wdata->extension.input, ABS_RX, rx);
  1465. input_report_abs(wdata->extension.input, ABS_RY, ry);
  1466. input_report_key(wdata->extension.input,
  1467. wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT],
  1468. !(ext[8] & 0x80));
  1469. input_report_key(wdata->extension.input,
  1470. wiimod_pro_map[WIIMOD_PRO_KEY_DOWN],
  1471. !(ext[8] & 0x40));
  1472. input_report_key(wdata->extension.input,
  1473. wiimod_pro_map[WIIMOD_PRO_KEY_TL],
  1474. !(ext[8] & 0x20));
  1475. input_report_key(wdata->extension.input,
  1476. wiimod_pro_map[WIIMOD_PRO_KEY_MINUS],
  1477. !(ext[8] & 0x10));
  1478. input_report_key(wdata->extension.input,
  1479. wiimod_pro_map[WIIMOD_PRO_KEY_HOME],
  1480. !(ext[8] & 0x08));
  1481. input_report_key(wdata->extension.input,
  1482. wiimod_pro_map[WIIMOD_PRO_KEY_PLUS],
  1483. !(ext[8] & 0x04));
  1484. input_report_key(wdata->extension.input,
  1485. wiimod_pro_map[WIIMOD_PRO_KEY_TR],
  1486. !(ext[8] & 0x02));
  1487. input_report_key(wdata->extension.input,
  1488. wiimod_pro_map[WIIMOD_PRO_KEY_ZL],
  1489. !(ext[9] & 0x80));
  1490. input_report_key(wdata->extension.input,
  1491. wiimod_pro_map[WIIMOD_PRO_KEY_B],
  1492. !(ext[9] & 0x40));
  1493. input_report_key(wdata->extension.input,
  1494. wiimod_pro_map[WIIMOD_PRO_KEY_Y],
  1495. !(ext[9] & 0x20));
  1496. input_report_key(wdata->extension.input,
  1497. wiimod_pro_map[WIIMOD_PRO_KEY_A],
  1498. !(ext[9] & 0x10));
  1499. input_report_key(wdata->extension.input,
  1500. wiimod_pro_map[WIIMOD_PRO_KEY_X],
  1501. !(ext[9] & 0x08));
  1502. input_report_key(wdata->extension.input,
  1503. wiimod_pro_map[WIIMOD_PRO_KEY_ZR],
  1504. !(ext[9] & 0x04));
  1505. input_report_key(wdata->extension.input,
  1506. wiimod_pro_map[WIIMOD_PRO_KEY_LEFT],
  1507. !(ext[9] & 0x02));
  1508. input_report_key(wdata->extension.input,
  1509. wiimod_pro_map[WIIMOD_PRO_KEY_UP],
  1510. !(ext[9] & 0x01));
  1511. input_report_key(wdata->extension.input,
  1512. wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL],
  1513. !(ext[10] & 0x02));
  1514. input_report_key(wdata->extension.input,
  1515. wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR],
  1516. !(ext[10] & 0x01));
  1517. input_sync(wdata->extension.input);
  1518. }
  1519. static int wiimod_pro_open(struct input_dev *dev)
  1520. {
  1521. struct wiimote_data *wdata = input_get_drvdata(dev);
  1522. unsigned long flags;
  1523. spin_lock_irqsave(&wdata->state.lock, flags);
  1524. wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
  1525. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1526. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1527. return 0;
  1528. }
  1529. static void wiimod_pro_close(struct input_dev *dev)
  1530. {
  1531. struct wiimote_data *wdata = input_get_drvdata(dev);
  1532. unsigned long flags;
  1533. spin_lock_irqsave(&wdata->state.lock, flags);
  1534. wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
  1535. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1536. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1537. }
  1538. static int wiimod_pro_play(struct input_dev *dev, void *data,
  1539. struct ff_effect *eff)
  1540. {
  1541. struct wiimote_data *wdata = input_get_drvdata(dev);
  1542. __u8 value;
  1543. /*
  1544. * The wiimote supports only a single rumble motor so if any magnitude
  1545. * is set to non-zero then we start the rumble motor. If both are set to
  1546. * zero, we stop the rumble motor.
  1547. */
  1548. if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
  1549. value = 1;
  1550. else
  1551. value = 0;
  1552. /* Locking state.lock here might deadlock with input_event() calls.
  1553. * schedule_work acts as barrier. Merging multiple changes is fine. */
  1554. wdata->state.cache_rumble = value;
  1555. schedule_work(&wdata->rumble_worker);
  1556. return 0;
  1557. }
  1558. static ssize_t wiimod_pro_calib_show(struct device *dev,
  1559. struct device_attribute *attr,
  1560. char *out)
  1561. {
  1562. struct wiimote_data *wdata = dev_to_wii(dev);
  1563. int r;
  1564. r = 0;
  1565. r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[0]);
  1566. r += sprintf(&out[r], "%+06hd ", wdata->state.calib_pro_sticks[1]);
  1567. r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[2]);
  1568. r += sprintf(&out[r], "%+06hd\n", wdata->state.calib_pro_sticks[3]);
  1569. return r;
  1570. }
  1571. static ssize_t wiimod_pro_calib_store(struct device *dev,
  1572. struct device_attribute *attr,
  1573. const char *buf, size_t count)
  1574. {
  1575. struct wiimote_data *wdata = dev_to_wii(dev);
  1576. int r;
  1577. s16 x1, y1, x2, y2;
  1578. if (!strncmp(buf, "scan\n", 5)) {
  1579. spin_lock_irq(&wdata->state.lock);
  1580. wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
  1581. spin_unlock_irq(&wdata->state.lock);
  1582. } else {
  1583. r = sscanf(buf, "%hd:%hd %hd:%hd", &x1, &y1, &x2, &y2);
  1584. if (r != 4)
  1585. return -EINVAL;
  1586. spin_lock_irq(&wdata->state.lock);
  1587. wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
  1588. spin_unlock_irq(&wdata->state.lock);
  1589. wdata->state.calib_pro_sticks[0] = x1;
  1590. wdata->state.calib_pro_sticks[1] = y1;
  1591. wdata->state.calib_pro_sticks[2] = x2;
  1592. wdata->state.calib_pro_sticks[3] = y2;
  1593. }
  1594. return strnlen(buf, PAGE_SIZE);
  1595. }
  1596. static DEVICE_ATTR(pro_calib, S_IRUGO|S_IWUSR|S_IWGRP, wiimod_pro_calib_show,
  1597. wiimod_pro_calib_store);
  1598. static int wiimod_pro_probe(const struct wiimod_ops *ops,
  1599. struct wiimote_data *wdata)
  1600. {
  1601. int ret, i;
  1602. unsigned long flags;
  1603. INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
  1604. wdata->state.calib_pro_sticks[0] = 0;
  1605. wdata->state.calib_pro_sticks[1] = 0;
  1606. wdata->state.calib_pro_sticks[2] = 0;
  1607. wdata->state.calib_pro_sticks[3] = 0;
  1608. spin_lock_irqsave(&wdata->state.lock, flags);
  1609. wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
  1610. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1611. wdata->extension.input = input_allocate_device();
  1612. if (!wdata->extension.input)
  1613. return -ENOMEM;
  1614. set_bit(FF_RUMBLE, wdata->extension.input->ffbit);
  1615. input_set_drvdata(wdata->extension.input, wdata);
  1616. if (input_ff_create_memless(wdata->extension.input, NULL,
  1617. wiimod_pro_play)) {
  1618. ret = -ENOMEM;
  1619. goto err_free;
  1620. }
  1621. ret = device_create_file(&wdata->hdev->dev,
  1622. &dev_attr_pro_calib);
  1623. if (ret) {
  1624. hid_err(wdata->hdev, "cannot create sysfs attribute\n");
  1625. goto err_free;
  1626. }
  1627. wdata->extension.input->open = wiimod_pro_open;
  1628. wdata->extension.input->close = wiimod_pro_close;
  1629. wdata->extension.input->dev.parent = &wdata->hdev->dev;
  1630. wdata->extension.input->id.bustype = wdata->hdev->bus;
  1631. wdata->extension.input->id.vendor = wdata->hdev->vendor;
  1632. wdata->extension.input->id.product = wdata->hdev->product;
  1633. wdata->extension.input->id.version = wdata->hdev->version;
  1634. wdata->extension.input->name = WIIMOTE_NAME " Pro Controller";
  1635. set_bit(EV_KEY, wdata->extension.input->evbit);
  1636. for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i)
  1637. set_bit(wiimod_pro_map[i],
  1638. wdata->extension.input->keybit);
  1639. set_bit(EV_ABS, wdata->extension.input->evbit);
  1640. set_bit(ABS_X, wdata->extension.input->absbit);
  1641. set_bit(ABS_Y, wdata->extension.input->absbit);
  1642. set_bit(ABS_RX, wdata->extension.input->absbit);
  1643. set_bit(ABS_RY, wdata->extension.input->absbit);
  1644. input_set_abs_params(wdata->extension.input,
  1645. ABS_X, -0x400, 0x400, 4, 100);
  1646. input_set_abs_params(wdata->extension.input,
  1647. ABS_Y, -0x400, 0x400, 4, 100);
  1648. input_set_abs_params(wdata->extension.input,
  1649. ABS_RX, -0x400, 0x400, 4, 100);
  1650. input_set_abs_params(wdata->extension.input,
  1651. ABS_RY, -0x400, 0x400, 4, 100);
  1652. ret = input_register_device(wdata->extension.input);
  1653. if (ret)
  1654. goto err_file;
  1655. return 0;
  1656. err_file:
  1657. device_remove_file(&wdata->hdev->dev,
  1658. &dev_attr_pro_calib);
  1659. err_free:
  1660. input_free_device(wdata->extension.input);
  1661. wdata->extension.input = NULL;
  1662. return ret;
  1663. }
  1664. static void wiimod_pro_remove(const struct wiimod_ops *ops,
  1665. struct wiimote_data *wdata)
  1666. {
  1667. unsigned long flags;
  1668. if (!wdata->extension.input)
  1669. return;
  1670. input_unregister_device(wdata->extension.input);
  1671. wdata->extension.input = NULL;
  1672. cancel_work_sync(&wdata->rumble_worker);
  1673. device_remove_file(&wdata->hdev->dev,
  1674. &dev_attr_pro_calib);
  1675. spin_lock_irqsave(&wdata->state.lock, flags);
  1676. wiiproto_req_rumble(wdata, 0);
  1677. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1678. }
  1679. static const struct wiimod_ops wiimod_pro = {
  1680. .flags = WIIMOD_FLAG_EXT16,
  1681. .arg = 0,
  1682. .probe = wiimod_pro_probe,
  1683. .remove = wiimod_pro_remove,
  1684. .in_ext = wiimod_pro_in_ext,
  1685. };
  1686. /*
  1687. * Builtin Motion Plus
  1688. * This module simply sets the WIIPROTO_FLAG_BUILTIN_MP protocol flag which
  1689. * disables polling for Motion-Plus. This should be set only for devices which
  1690. * don't allow MP hotplugging.
  1691. */
  1692. static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops,
  1693. struct wiimote_data *wdata)
  1694. {
  1695. unsigned long flags;
  1696. spin_lock_irqsave(&wdata->state.lock, flags);
  1697. wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
  1698. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1699. return 0;
  1700. }
  1701. static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops,
  1702. struct wiimote_data *wdata)
  1703. {
  1704. unsigned long flags;
  1705. spin_lock_irqsave(&wdata->state.lock, flags);
  1706. wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
  1707. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1708. }
  1709. static const struct wiimod_ops wiimod_builtin_mp = {
  1710. .flags = 0,
  1711. .arg = 0,
  1712. .probe = wiimod_builtin_mp_probe,
  1713. .remove = wiimod_builtin_mp_remove,
  1714. };
  1715. /*
  1716. * No Motion Plus
  1717. * This module simply sets the WIIPROTO_FLAG_NO_MP protocol flag which
  1718. * disables motion-plus. This is needed for devices that advertise this but we
  1719. * don't know how to use it (or whether it is actually present).
  1720. */
  1721. static int wiimod_no_mp_probe(const struct wiimod_ops *ops,
  1722. struct wiimote_data *wdata)
  1723. {
  1724. unsigned long flags;
  1725. spin_lock_irqsave(&wdata->state.lock, flags);
  1726. wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
  1727. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1728. return 0;
  1729. }
  1730. static void wiimod_no_mp_remove(const struct wiimod_ops *ops,
  1731. struct wiimote_data *wdata)
  1732. {
  1733. unsigned long flags;
  1734. spin_lock_irqsave(&wdata->state.lock, flags);
  1735. wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
  1736. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1737. }
  1738. static const struct wiimod_ops wiimod_no_mp = {
  1739. .flags = 0,
  1740. .arg = 0,
  1741. .probe = wiimod_no_mp_probe,
  1742. .remove = wiimod_no_mp_remove,
  1743. };
  1744. /*
  1745. * Motion Plus
  1746. * The Motion Plus extension provides rotation sensors (gyro) as a small
  1747. * extension device for Wii Remotes. Many devices have them built-in so
  1748. * you cannot see them from the outside.
  1749. * Motion Plus extensions are special because they are on a separate extension
  1750. * port and allow other extensions to be used simultaneously. This is all
  1751. * handled by the Wiimote Core so we don't have to deal with it.
  1752. */
  1753. static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext)
  1754. {
  1755. __s32 x, y, z;
  1756. /* | 8 7 6 5 4 3 | 2 | 1 |
  1757. * -----+------------------------------+-----+-----+
  1758. * 1 | Yaw Speed <7:0> |
  1759. * 2 | Roll Speed <7:0> |
  1760. * 3 | Pitch Speed <7:0> |
  1761. * -----+------------------------------+-----+-----+
  1762. * 4 | Yaw Speed <13:8> | Yaw |Pitch|
  1763. * -----+------------------------------+-----+-----+
  1764. * 5 | Roll Speed <13:8> |Roll | Ext |
  1765. * -----+------------------------------+-----+-----+
  1766. * 6 | Pitch Speed <13:8> | 1 | 0 |
  1767. * -----+------------------------------+-----+-----+
  1768. * The single bits Yaw, Roll, Pitch in the lower right corner specify
  1769. * whether the wiimote is rotating fast (0) or slow (1). Speed for slow
  1770. * roation is 440 deg/s and for fast rotation 2000 deg/s. To get a
  1771. * linear scale we multiply by 2000/440 = ~4.5454 which is 18 for fast
  1772. * and 9 for slow.
  1773. * If the wiimote is not rotating the sensor reports 2^13 = 8192.
  1774. * Ext specifies whether an extension is connected to the motionp.
  1775. * which is parsed by wiimote-core.
  1776. */
  1777. x = ext[0];
  1778. y = ext[1];
  1779. z = ext[2];
  1780. x |= (((__u16)ext[3]) << 6) & 0xff00;
  1781. y |= (((__u16)ext[4]) << 6) & 0xff00;
  1782. z |= (((__u16)ext[5]) << 6) & 0xff00;
  1783. x -= 8192;
  1784. y -= 8192;
  1785. z -= 8192;
  1786. if (!(ext[3] & 0x02))
  1787. x *= 18;
  1788. else
  1789. x *= 9;
  1790. if (!(ext[4] & 0x02))
  1791. y *= 18;
  1792. else
  1793. y *= 9;
  1794. if (!(ext[3] & 0x01))
  1795. z *= 18;
  1796. else
  1797. z *= 9;
  1798. input_report_abs(wdata->mp, ABS_RX, x);
  1799. input_report_abs(wdata->mp, ABS_RY, y);
  1800. input_report_abs(wdata->mp, ABS_RZ, z);
  1801. input_sync(wdata->mp);
  1802. }
  1803. static int wiimod_mp_open(struct input_dev *dev)
  1804. {
  1805. struct wiimote_data *wdata = input_get_drvdata(dev);
  1806. unsigned long flags;
  1807. spin_lock_irqsave(&wdata->state.lock, flags);
  1808. wdata->state.flags |= WIIPROTO_FLAG_MP_USED;
  1809. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1810. __wiimote_schedule(wdata);
  1811. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1812. return 0;
  1813. }
  1814. static void wiimod_mp_close(struct input_dev *dev)
  1815. {
  1816. struct wiimote_data *wdata = input_get_drvdata(dev);
  1817. unsigned long flags;
  1818. spin_lock_irqsave(&wdata->state.lock, flags);
  1819. wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
  1820. wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
  1821. __wiimote_schedule(wdata);
  1822. spin_unlock_irqrestore(&wdata->state.lock, flags);
  1823. }
  1824. static int wiimod_mp_probe(const struct wiimod_ops *ops,
  1825. struct wiimote_data *wdata)
  1826. {
  1827. int ret;
  1828. wdata->mp = input_allocate_device();
  1829. if (!wdata->mp)
  1830. return -ENOMEM;
  1831. input_set_drvdata(wdata->mp, wdata);
  1832. wdata->mp->open = wiimod_mp_open;
  1833. wdata->mp->close = wiimod_mp_close;
  1834. wdata->mp->dev.parent = &wdata->hdev->dev;
  1835. wdata->mp->id.bustype = wdata->hdev->bus;
  1836. wdata->mp->id.vendor = wdata->hdev->vendor;
  1837. wdata->mp->id.product = wdata->hdev->product;
  1838. wdata->mp->id.version = wdata->hdev->version;
  1839. wdata->mp->name = WIIMOTE_NAME " Motion Plus";
  1840. set_bit(EV_ABS, wdata->mp->evbit);
  1841. set_bit(ABS_RX, wdata->mp->absbit);
  1842. set_bit(ABS_RY, wdata->mp->absbit);
  1843. set_bit(ABS_RZ, wdata->mp->absbit);
  1844. input_set_abs_params(wdata->mp,
  1845. ABS_RX, -16000, 16000, 4, 8);
  1846. input_set_abs_params(wdata->mp,
  1847. ABS_RY, -16000, 16000, 4, 8);
  1848. input_set_abs_params(wdata->mp,
  1849. ABS_RZ, -16000, 16000, 4, 8);
  1850. ret = input_register_device(wdata->mp);
  1851. if (ret)
  1852. goto err_free;
  1853. return 0;
  1854. err_free:
  1855. input_free_device(wdata->mp);
  1856. wdata->mp = NULL;
  1857. return ret;
  1858. }
  1859. static void wiimod_mp_remove(const struct wiimod_ops *ops,
  1860. struct wiimote_data *wdata)
  1861. {
  1862. if (!wdata->mp)
  1863. return;
  1864. input_unregister_device(wdata->mp);
  1865. wdata->mp = NULL;
  1866. }
  1867. const struct wiimod_ops wiimod_mp = {
  1868. .flags = 0,
  1869. .arg = 0,
  1870. .probe = wiimod_mp_probe,
  1871. .remove = wiimod_mp_remove,
  1872. .in_mp = wiimod_mp_in_mp,
  1873. };
  1874. /* module table */
  1875. static const struct wiimod_ops wiimod_dummy;
  1876. const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
  1877. [WIIMOD_KEYS] = &wiimod_keys,
  1878. [WIIMOD_RUMBLE] = &wiimod_rumble,
  1879. [WIIMOD_BATTERY] = &wiimod_battery,
  1880. [WIIMOD_LED1] = &wiimod_leds[0],
  1881. [WIIMOD_LED2] = &wiimod_leds[1],
  1882. [WIIMOD_LED3] = &wiimod_leds[2],
  1883. [WIIMOD_LED4] = &wiimod_leds[3],
  1884. [WIIMOD_ACCEL] = &wiimod_accel,
  1885. [WIIMOD_IR] = &wiimod_ir,
  1886. [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp,
  1887. [WIIMOD_NO_MP] = &wiimod_no_mp,
  1888. };
  1889. const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = {
  1890. [WIIMOTE_EXT_NONE] = &wiimod_dummy,
  1891. [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy,
  1892. [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk,
  1893. [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic,
  1894. [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard,
  1895. [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro,
  1896. };