hid-wiimote-modules.c 79 KB

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