hid-wiimote-modules.c 78 KB

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