hid-wiimote-modules.c 74 KB

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