hid-wiimote-modules.c 64 KB

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