hid-wiimote-modules.c 63 KB

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