1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
40
41
42
43
44
45
46
47
48static const __u16 wiimod_keys_map[] = {
49 KEY_LEFT,
50 KEY_RIGHT,
51 KEY_UP,
52 KEY_DOWN,
53 KEY_NEXT,
54 KEY_PREVIOUS,
55 BTN_1,
56 BTN_2,
57 BTN_A,
58 BTN_B,
59 BTN_MODE,
60};
61
62static void wiimod_keys_in_keys(struct wiimote_data *wdata, const __u8 *keys)
63{
64 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_LEFT],
65 !!(keys[0] & 0x01));
66 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_RIGHT],
67 !!(keys[0] & 0x02));
68 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_DOWN],
69 !!(keys[0] & 0x04));
70 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_UP],
71 !!(keys[0] & 0x08));
72 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_PLUS],
73 !!(keys[0] & 0x10));
74 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_TWO],
75 !!(keys[1] & 0x01));
76 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_ONE],
77 !!(keys[1] & 0x02));
78 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_B],
79 !!(keys[1] & 0x04));
80 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_A],
81 !!(keys[1] & 0x08));
82 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_MINUS],
83 !!(keys[1] & 0x10));
84 input_report_key(wdata->input, wiimod_keys_map[WIIPROTO_KEY_HOME],
85 !!(keys[1] & 0x80));
86 input_sync(wdata->input);
87}
88
89static int wiimod_keys_probe(const struct wiimod_ops *ops,
90 struct wiimote_data *wdata)
91{
92 unsigned int i;
93
94 set_bit(EV_KEY, wdata->input->evbit);
95 for (i = 0; i < WIIPROTO_KEY_COUNT; ++i)
96 set_bit(wiimod_keys_map[i], wdata->input->keybit);
97
98 return 0;
99}
100
101static const struct wiimod_ops wiimod_keys = {
102 .flags = WIIMOD_FLAG_INPUT,
103 .arg = 0,
104 .probe = wiimod_keys_probe,
105 .remove = NULL,
106 .in_keys = wiimod_keys_in_keys,
107};
108
109
110
111
112
113
114
115
116
117
118
119
120static void wiimod_rumble_worker(struct work_struct *work)
121{
122 struct wiimote_data *wdata = container_of(work, struct wiimote_data,
123 rumble_worker);
124
125 spin_lock_irq(&wdata->state.lock);
126 wiiproto_req_rumble(wdata, wdata->state.cache_rumble);
127 spin_unlock_irq(&wdata->state.lock);
128}
129
130static int wiimod_rumble_play(struct input_dev *dev, void *data,
131 struct ff_effect *eff)
132{
133 struct wiimote_data *wdata = input_get_drvdata(dev);
134 __u8 value;
135
136
137
138
139
140
141
142 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
143 value = 1;
144 else
145 value = 0;
146
147
148
149 wdata->state.cache_rumble = value;
150 schedule_work(&wdata->rumble_worker);
151
152 return 0;
153}
154
155static int wiimod_rumble_probe(const struct wiimod_ops *ops,
156 struct wiimote_data *wdata)
157{
158 INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
159
160 set_bit(FF_RUMBLE, wdata->input->ffbit);
161 if (input_ff_create_memless(wdata->input, NULL, wiimod_rumble_play))
162 return -ENOMEM;
163
164 return 0;
165}
166
167static void wiimod_rumble_remove(const struct wiimod_ops *ops,
168 struct wiimote_data *wdata)
169{
170 unsigned long flags;
171
172 cancel_work_sync(&wdata->rumble_worker);
173
174 spin_lock_irqsave(&wdata->state.lock, flags);
175 wiiproto_req_rumble(wdata, 0);
176 spin_unlock_irqrestore(&wdata->state.lock, flags);
177}
178
179static const struct wiimod_ops wiimod_rumble = {
180 .flags = WIIMOD_FLAG_INPUT,
181 .arg = 0,
182 .probe = wiimod_rumble_probe,
183 .remove = wiimod_rumble_remove,
184};
185
186
187
188
189
190
191
192
193
194static enum power_supply_property wiimod_battery_props[] = {
195 POWER_SUPPLY_PROP_CAPACITY,
196 POWER_SUPPLY_PROP_SCOPE,
197};
198
199static int wiimod_battery_get_property(struct power_supply *psy,
200 enum power_supply_property psp,
201 union power_supply_propval *val)
202{
203 struct wiimote_data *wdata = power_supply_get_drvdata(psy);
204 int ret = 0, state;
205 unsigned long flags;
206
207 if (psp == POWER_SUPPLY_PROP_SCOPE) {
208 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
209 return 0;
210 } else if (psp != POWER_SUPPLY_PROP_CAPACITY) {
211 return -EINVAL;
212 }
213
214 ret = wiimote_cmd_acquire(wdata);
215 if (ret)
216 return ret;
217
218 spin_lock_irqsave(&wdata->state.lock, flags);
219 wiimote_cmd_set(wdata, WIIPROTO_REQ_SREQ, 0);
220 wiiproto_req_status(wdata);
221 spin_unlock_irqrestore(&wdata->state.lock, flags);
222
223 wiimote_cmd_wait(wdata);
224 wiimote_cmd_release(wdata);
225
226 spin_lock_irqsave(&wdata->state.lock, flags);
227 state = wdata->state.cmd_battery;
228 spin_unlock_irqrestore(&wdata->state.lock, flags);
229
230 val->intval = state * 100 / 255;
231 return ret;
232}
233
234static int wiimod_battery_probe(const struct wiimod_ops *ops,
235 struct wiimote_data *wdata)
236{
237 struct power_supply_config psy_cfg = { .drv_data = wdata, };
238 int ret;
239
240 wdata->battery_desc.properties = wiimod_battery_props;
241 wdata->battery_desc.num_properties = ARRAY_SIZE(wiimod_battery_props);
242 wdata->battery_desc.get_property = wiimod_battery_get_property;
243 wdata->battery_desc.type = POWER_SUPPLY_TYPE_BATTERY;
244 wdata->battery_desc.use_for_apm = 0;
245 wdata->battery_desc.name = kasprintf(GFP_KERNEL, "wiimote_battery_%s",
246 wdata->hdev->uniq);
247 if (!wdata->battery_desc.name)
248 return -ENOMEM;
249
250 wdata->battery = power_supply_register(&wdata->hdev->dev,
251 &wdata->battery_desc,
252 &psy_cfg);
253 if (IS_ERR(wdata->battery)) {
254 hid_err(wdata->hdev, "cannot register battery device\n");
255 ret = PTR_ERR(wdata->battery);
256 goto err_free;
257 }
258
259 power_supply_powers(wdata->battery, &wdata->hdev->dev);
260 return 0;
261
262err_free:
263 kfree(wdata->battery_desc.name);
264 wdata->battery_desc.name = NULL;
265 return ret;
266}
267
268static void wiimod_battery_remove(const struct wiimod_ops *ops,
269 struct wiimote_data *wdata)
270{
271 if (!wdata->battery_desc.name)
272 return;
273
274 power_supply_unregister(wdata->battery);
275 kfree(wdata->battery_desc.name);
276 wdata->battery_desc.name = NULL;
277}
278
279static const struct wiimod_ops wiimod_battery = {
280 .flags = 0,
281 .arg = 0,
282 .probe = wiimod_battery_probe,
283 .remove = wiimod_battery_remove,
284};
285
286
287
288
289
290
291
292
293
294static enum led_brightness wiimod_led_get(struct led_classdev *led_dev)
295{
296 struct device *dev = led_dev->dev->parent;
297 struct wiimote_data *wdata = dev_to_wii(dev);
298 int i;
299 unsigned long flags;
300 bool value = false;
301
302 for (i = 0; i < 4; ++i) {
303 if (wdata->leds[i] == led_dev) {
304 spin_lock_irqsave(&wdata->state.lock, flags);
305 value = wdata->state.flags & WIIPROTO_FLAG_LED(i + 1);
306 spin_unlock_irqrestore(&wdata->state.lock, flags);
307 break;
308 }
309 }
310
311 return value ? LED_FULL : LED_OFF;
312}
313
314static void wiimod_led_set(struct led_classdev *led_dev,
315 enum led_brightness value)
316{
317 struct device *dev = led_dev->dev->parent;
318 struct wiimote_data *wdata = dev_to_wii(dev);
319 int i;
320 unsigned long flags;
321 __u8 state, flag;
322
323 for (i = 0; i < 4; ++i) {
324 if (wdata->leds[i] == led_dev) {
325 flag = WIIPROTO_FLAG_LED(i + 1);
326 spin_lock_irqsave(&wdata->state.lock, flags);
327 state = wdata->state.flags;
328 if (value == LED_OFF)
329 wiiproto_req_leds(wdata, state & ~flag);
330 else
331 wiiproto_req_leds(wdata, state | flag);
332 spin_unlock_irqrestore(&wdata->state.lock, flags);
333 break;
334 }
335 }
336}
337
338static int wiimod_led_probe(const struct wiimod_ops *ops,
339 struct wiimote_data *wdata)
340{
341 struct device *dev = &wdata->hdev->dev;
342 size_t namesz = strlen(dev_name(dev)) + 9;
343 struct led_classdev *led;
344 unsigned long flags;
345 char *name;
346 int ret;
347
348 led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
349 if (!led)
350 return -ENOMEM;
351
352 name = (void*)&led[1];
353 snprintf(name, namesz, "%s:blue:p%lu", dev_name(dev), ops->arg);
354 led->name = name;
355 led->brightness = 0;
356 led->max_brightness = 1;
357 led->brightness_get = wiimod_led_get;
358 led->brightness_set = wiimod_led_set;
359
360 wdata->leds[ops->arg] = led;
361 ret = led_classdev_register(dev, led);
362 if (ret)
363 goto err_free;
364
365
366 if (ops->arg == 0) {
367 spin_lock_irqsave(&wdata->state.lock, flags);
368 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
369 spin_unlock_irqrestore(&wdata->state.lock, flags);
370 }
371
372 return 0;
373
374err_free:
375 wdata->leds[ops->arg] = NULL;
376 kfree(led);
377 return ret;
378}
379
380static void wiimod_led_remove(const struct wiimod_ops *ops,
381 struct wiimote_data *wdata)
382{
383 if (!wdata->leds[ops->arg])
384 return;
385
386 led_classdev_unregister(wdata->leds[ops->arg]);
387 kfree(wdata->leds[ops->arg]);
388 wdata->leds[ops->arg] = NULL;
389}
390
391static const struct wiimod_ops wiimod_leds[4] = {
392 {
393 .flags = 0,
394 .arg = 0,
395 .probe = wiimod_led_probe,
396 .remove = wiimod_led_remove,
397 },
398 {
399 .flags = 0,
400 .arg = 1,
401 .probe = wiimod_led_probe,
402 .remove = wiimod_led_remove,
403 },
404 {
405 .flags = 0,
406 .arg = 2,
407 .probe = wiimod_led_probe,
408 .remove = wiimod_led_remove,
409 },
410 {
411 .flags = 0,
412 .arg = 3,
413 .probe = wiimod_led_probe,
414 .remove = wiimod_led_remove,
415 },
416};
417
418
419
420
421
422
423
424
425static void wiimod_accel_in_accel(struct wiimote_data *wdata,
426 const __u8 *accel)
427{
428 __u16 x, y, z;
429
430 if (!(wdata->state.flags & WIIPROTO_FLAG_ACCEL))
431 return;
432
433
434
435
436
437
438
439
440
441
442
443
444
445 x = accel[2] << 2;
446 y = accel[3] << 2;
447 z = accel[4] << 2;
448
449 x |= (accel[0] >> 5) & 0x3;
450 y |= (accel[1] >> 4) & 0x2;
451 z |= (accel[1] >> 5) & 0x2;
452
453 input_report_abs(wdata->accel, ABS_RX, x - 0x200);
454 input_report_abs(wdata->accel, ABS_RY, y - 0x200);
455 input_report_abs(wdata->accel, ABS_RZ, z - 0x200);
456 input_sync(wdata->accel);
457}
458
459static int wiimod_accel_open(struct input_dev *dev)
460{
461 struct wiimote_data *wdata = input_get_drvdata(dev);
462 unsigned long flags;
463
464 spin_lock_irqsave(&wdata->state.lock, flags);
465 wiiproto_req_accel(wdata, true);
466 spin_unlock_irqrestore(&wdata->state.lock, flags);
467
468 return 0;
469}
470
471static void wiimod_accel_close(struct input_dev *dev)
472{
473 struct wiimote_data *wdata = input_get_drvdata(dev);
474 unsigned long flags;
475
476 spin_lock_irqsave(&wdata->state.lock, flags);
477 wiiproto_req_accel(wdata, false);
478 spin_unlock_irqrestore(&wdata->state.lock, flags);
479}
480
481static int wiimod_accel_probe(const struct wiimod_ops *ops,
482 struct wiimote_data *wdata)
483{
484 int ret;
485
486 wdata->accel = input_allocate_device();
487 if (!wdata->accel)
488 return -ENOMEM;
489
490 input_set_drvdata(wdata->accel, wdata);
491 wdata->accel->open = wiimod_accel_open;
492 wdata->accel->close = wiimod_accel_close;
493 wdata->accel->dev.parent = &wdata->hdev->dev;
494 wdata->accel->id.bustype = wdata->hdev->bus;
495 wdata->accel->id.vendor = wdata->hdev->vendor;
496 wdata->accel->id.product = wdata->hdev->product;
497 wdata->accel->id.version = wdata->hdev->version;
498 wdata->accel->name = WIIMOTE_NAME " Accelerometer";
499
500 set_bit(EV_ABS, wdata->accel->evbit);
501 set_bit(ABS_RX, wdata->accel->absbit);
502 set_bit(ABS_RY, wdata->accel->absbit);
503 set_bit(ABS_RZ, wdata->accel->absbit);
504 input_set_abs_params(wdata->accel, ABS_RX, -500, 500, 2, 4);
505 input_set_abs_params(wdata->accel, ABS_RY, -500, 500, 2, 4);
506 input_set_abs_params(wdata->accel, ABS_RZ, -500, 500, 2, 4);
507
508 ret = input_register_device(wdata->accel);
509 if (ret) {
510 hid_err(wdata->hdev, "cannot register input device\n");
511 goto err_free;
512 }
513
514 return 0;
515
516err_free:
517 input_free_device(wdata->accel);
518 wdata->accel = NULL;
519 return ret;
520}
521
522static void wiimod_accel_remove(const struct wiimod_ops *ops,
523 struct wiimote_data *wdata)
524{
525 if (!wdata->accel)
526 return;
527
528 input_unregister_device(wdata->accel);
529 wdata->accel = NULL;
530}
531
532static const struct wiimod_ops wiimod_accel = {
533 .flags = 0,
534 .arg = 0,
535 .probe = wiimod_accel_probe,
536 .remove = wiimod_accel_remove,
537 .in_accel = wiimod_accel_in_accel,
538};
539
540
541
542
543
544
545
546
547
548
549
550static void wiimod_ir_in_ir(struct wiimote_data *wdata, const __u8 *ir,
551 bool packed, unsigned int id)
552{
553 __u16 x, y;
554 __u8 xid, yid;
555 bool sync = false;
556
557 if (!(wdata->state.flags & WIIPROTO_FLAGS_IR))
558 return;
559
560 switch (id) {
561 case 0:
562 xid = ABS_HAT0X;
563 yid = ABS_HAT0Y;
564 break;
565 case 1:
566 xid = ABS_HAT1X;
567 yid = ABS_HAT1Y;
568 break;
569 case 2:
570 xid = ABS_HAT2X;
571 yid = ABS_HAT2Y;
572 break;
573 case 3:
574 xid = ABS_HAT3X;
575 yid = ABS_HAT3Y;
576 sync = true;
577 break;
578 default:
579 return;
580 }
581
582
583
584
585
586
587
588
589
590
591
592 if (packed) {
593 x = ir[1] | ((ir[0] & 0x03) << 8);
594 y = ir[2] | ((ir[0] & 0x0c) << 6);
595 } else {
596 x = ir[0] | ((ir[2] & 0x30) << 4);
597 y = ir[1] | ((ir[2] & 0xc0) << 2);
598 }
599
600 input_report_abs(wdata->ir, xid, x);
601 input_report_abs(wdata->ir, yid, y);
602
603 if (sync)
604 input_sync(wdata->ir);
605}
606
607static int wiimod_ir_change(struct wiimote_data *wdata, __u16 mode)
608{
609 int ret;
610 unsigned long flags;
611 __u8 format = 0;
612 static const __u8 data_enable[] = { 0x01 };
613 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
614 0x00, 0xaa, 0x00, 0x64 };
615 static const __u8 data_sens2[] = { 0x63, 0x03 };
616 static const __u8 data_fin[] = { 0x08 };
617
618 spin_lock_irqsave(&wdata->state.lock, flags);
619
620 if (mode == (wdata->state.flags & WIIPROTO_FLAGS_IR)) {
621 spin_unlock_irqrestore(&wdata->state.lock, flags);
622 return 0;
623 }
624
625 if (mode == 0) {
626 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
627 wiiproto_req_ir1(wdata, 0);
628 wiiproto_req_ir2(wdata, 0);
629 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
630 spin_unlock_irqrestore(&wdata->state.lock, flags);
631 return 0;
632 }
633
634 spin_unlock_irqrestore(&wdata->state.lock, flags);
635
636 ret = wiimote_cmd_acquire(wdata);
637 if (ret)
638 return ret;
639
640
641 spin_lock_irqsave(&wdata->state.lock, flags);
642 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR1, 0);
643 wiiproto_req_ir1(wdata, 0x06);
644 spin_unlock_irqrestore(&wdata->state.lock, flags);
645
646 ret = wiimote_cmd_wait(wdata);
647 if (ret)
648 goto unlock;
649 if (wdata->state.cmd_err) {
650 ret = -EIO;
651 goto unlock;
652 }
653
654
655 spin_lock_irqsave(&wdata->state.lock, flags);
656 wiimote_cmd_set(wdata, WIIPROTO_REQ_IR2, 0);
657 wiiproto_req_ir2(wdata, 0x06);
658 spin_unlock_irqrestore(&wdata->state.lock, flags);
659
660 ret = wiimote_cmd_wait(wdata);
661 if (ret)
662 goto unlock;
663 if (wdata->state.cmd_err) {
664 ret = -EIO;
665 goto unlock;
666 }
667
668
669 ret = wiimote_cmd_write(wdata, 0xb00030, data_enable,
670 sizeof(data_enable));
671 if (ret)
672 goto unlock;
673
674
675 ret = wiimote_cmd_write(wdata, 0xb00000, data_sens1,
676 sizeof(data_sens1));
677 if (ret)
678 goto unlock;
679
680
681 ret = wiimote_cmd_write(wdata, 0xb0001a, data_sens2,
682 sizeof(data_sens2));
683 if (ret)
684 goto unlock;
685
686
687 switch (mode) {
688 case WIIPROTO_FLAG_IR_FULL:
689 format = 5;
690 break;
691 case WIIPROTO_FLAG_IR_EXT:
692 format = 3;
693 break;
694 case WIIPROTO_FLAG_IR_BASIC:
695 format = 1;
696 break;
697 }
698 ret = wiimote_cmd_write(wdata, 0xb00033, &format, sizeof(format));
699 if (ret)
700 goto unlock;
701
702
703 ret = wiimote_cmd_write(wdata, 0xb00030, data_fin, sizeof(data_fin));
704 if (ret)
705 goto unlock;
706
707
708 spin_lock_irqsave(&wdata->state.lock, flags);
709 wdata->state.flags &= ~WIIPROTO_FLAGS_IR;
710 wdata->state.flags |= mode & WIIPROTO_FLAGS_IR;
711 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
712 spin_unlock_irqrestore(&wdata->state.lock, flags);
713
714unlock:
715 wiimote_cmd_release(wdata);
716 return ret;
717}
718
719static int wiimod_ir_open(struct input_dev *dev)
720{
721 struct wiimote_data *wdata = input_get_drvdata(dev);
722
723 return wiimod_ir_change(wdata, WIIPROTO_FLAG_IR_BASIC);
724}
725
726static void wiimod_ir_close(struct input_dev *dev)
727{
728 struct wiimote_data *wdata = input_get_drvdata(dev);
729
730 wiimod_ir_change(wdata, 0);
731}
732
733static int wiimod_ir_probe(const struct wiimod_ops *ops,
734 struct wiimote_data *wdata)
735{
736 int ret;
737
738 wdata->ir = input_allocate_device();
739 if (!wdata->ir)
740 return -ENOMEM;
741
742 input_set_drvdata(wdata->ir, wdata);
743 wdata->ir->open = wiimod_ir_open;
744 wdata->ir->close = wiimod_ir_close;
745 wdata->ir->dev.parent = &wdata->hdev->dev;
746 wdata->ir->id.bustype = wdata->hdev->bus;
747 wdata->ir->id.vendor = wdata->hdev->vendor;
748 wdata->ir->id.product = wdata->hdev->product;
749 wdata->ir->id.version = wdata->hdev->version;
750 wdata->ir->name = WIIMOTE_NAME " IR";
751
752 set_bit(EV_ABS, wdata->ir->evbit);
753 set_bit(ABS_HAT0X, wdata->ir->absbit);
754 set_bit(ABS_HAT0Y, wdata->ir->absbit);
755 set_bit(ABS_HAT1X, wdata->ir->absbit);
756 set_bit(ABS_HAT1Y, wdata->ir->absbit);
757 set_bit(ABS_HAT2X, wdata->ir->absbit);
758 set_bit(ABS_HAT2Y, wdata->ir->absbit);
759 set_bit(ABS_HAT3X, wdata->ir->absbit);
760 set_bit(ABS_HAT3Y, wdata->ir->absbit);
761 input_set_abs_params(wdata->ir, ABS_HAT0X, 0, 1023, 2, 4);
762 input_set_abs_params(wdata->ir, ABS_HAT0Y, 0, 767, 2, 4);
763 input_set_abs_params(wdata->ir, ABS_HAT1X, 0, 1023, 2, 4);
764 input_set_abs_params(wdata->ir, ABS_HAT1Y, 0, 767, 2, 4);
765 input_set_abs_params(wdata->ir, ABS_HAT2X, 0, 1023, 2, 4);
766 input_set_abs_params(wdata->ir, ABS_HAT2Y, 0, 767, 2, 4);
767 input_set_abs_params(wdata->ir, ABS_HAT3X, 0, 1023, 2, 4);
768 input_set_abs_params(wdata->ir, ABS_HAT3Y, 0, 767, 2, 4);
769
770 ret = input_register_device(wdata->ir);
771 if (ret) {
772 hid_err(wdata->hdev, "cannot register input device\n");
773 goto err_free;
774 }
775
776 return 0;
777
778err_free:
779 input_free_device(wdata->ir);
780 wdata->ir = NULL;
781 return ret;
782}
783
784static void wiimod_ir_remove(const struct wiimod_ops *ops,
785 struct wiimote_data *wdata)
786{
787 if (!wdata->ir)
788 return;
789
790 input_unregister_device(wdata->ir);
791 wdata->ir = NULL;
792}
793
794static const struct wiimod_ops wiimod_ir = {
795 .flags = 0,
796 .arg = 0,
797 .probe = wiimod_ir_probe,
798 .remove = wiimod_ir_remove,
799 .in_ir = wiimod_ir_in_ir,
800};
801
802
803
804
805
806
807
808
809enum wiimod_nunchuk_keys {
810 WIIMOD_NUNCHUK_KEY_C,
811 WIIMOD_NUNCHUK_KEY_Z,
812 WIIMOD_NUNCHUK_KEY_NUM,
813};
814
815static const __u16 wiimod_nunchuk_map[] = {
816 BTN_C,
817 BTN_Z,
818};
819
820static void wiimod_nunchuk_in_ext(struct wiimote_data *wdata, const __u8 *ext)
821{
822 __s16 x, y, z, bx, by;
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854 bx = ext[0];
855 by = ext[1];
856 bx -= 128;
857 by -= 128;
858
859 x = ext[2] << 2;
860 y = ext[3] << 2;
861 z = ext[4] << 2;
862
863 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
864 x |= (ext[5] >> 3) & 0x02;
865 y |= (ext[5] >> 4) & 0x02;
866 z &= ~0x4;
867 z |= (ext[5] >> 5) & 0x06;
868 } else {
869 x |= (ext[5] >> 2) & 0x03;
870 y |= (ext[5] >> 4) & 0x03;
871 z |= (ext[5] >> 6) & 0x03;
872 }
873
874 x -= 0x200;
875 y -= 0x200;
876 z -= 0x200;
877
878 input_report_abs(wdata->extension.input, ABS_HAT0X, bx);
879 input_report_abs(wdata->extension.input, ABS_HAT0Y, by);
880
881 input_report_abs(wdata->extension.input, ABS_RX, x);
882 input_report_abs(wdata->extension.input, ABS_RY, y);
883 input_report_abs(wdata->extension.input, ABS_RZ, z);
884
885 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
886 input_report_key(wdata->extension.input,
887 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
888 !(ext[5] & 0x04));
889 input_report_key(wdata->extension.input,
890 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
891 !(ext[5] & 0x08));
892 } else {
893 input_report_key(wdata->extension.input,
894 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_Z],
895 !(ext[5] & 0x01));
896 input_report_key(wdata->extension.input,
897 wiimod_nunchuk_map[WIIMOD_NUNCHUK_KEY_C],
898 !(ext[5] & 0x02));
899 }
900
901 input_sync(wdata->extension.input);
902}
903
904static int wiimod_nunchuk_open(struct input_dev *dev)
905{
906 struct wiimote_data *wdata = input_get_drvdata(dev);
907 unsigned long flags;
908
909 spin_lock_irqsave(&wdata->state.lock, flags);
910 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
911 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
912 spin_unlock_irqrestore(&wdata->state.lock, flags);
913
914 return 0;
915}
916
917static void wiimod_nunchuk_close(struct input_dev *dev)
918{
919 struct wiimote_data *wdata = input_get_drvdata(dev);
920 unsigned long flags;
921
922 spin_lock_irqsave(&wdata->state.lock, flags);
923 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
924 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
925 spin_unlock_irqrestore(&wdata->state.lock, flags);
926}
927
928static int wiimod_nunchuk_probe(const struct wiimod_ops *ops,
929 struct wiimote_data *wdata)
930{
931 int ret, i;
932
933 wdata->extension.input = input_allocate_device();
934 if (!wdata->extension.input)
935 return -ENOMEM;
936
937 input_set_drvdata(wdata->extension.input, wdata);
938 wdata->extension.input->open = wiimod_nunchuk_open;
939 wdata->extension.input->close = wiimod_nunchuk_close;
940 wdata->extension.input->dev.parent = &wdata->hdev->dev;
941 wdata->extension.input->id.bustype = wdata->hdev->bus;
942 wdata->extension.input->id.vendor = wdata->hdev->vendor;
943 wdata->extension.input->id.product = wdata->hdev->product;
944 wdata->extension.input->id.version = wdata->hdev->version;
945 wdata->extension.input->name = WIIMOTE_NAME " Nunchuk";
946
947 set_bit(EV_KEY, wdata->extension.input->evbit);
948 for (i = 0; i < WIIMOD_NUNCHUK_KEY_NUM; ++i)
949 set_bit(wiimod_nunchuk_map[i],
950 wdata->extension.input->keybit);
951
952 set_bit(EV_ABS, wdata->extension.input->evbit);
953 set_bit(ABS_HAT0X, wdata->extension.input->absbit);
954 set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
955 input_set_abs_params(wdata->extension.input,
956 ABS_HAT0X, -120, 120, 2, 4);
957 input_set_abs_params(wdata->extension.input,
958 ABS_HAT0Y, -120, 120, 2, 4);
959 set_bit(ABS_RX, wdata->extension.input->absbit);
960 set_bit(ABS_RY, wdata->extension.input->absbit);
961 set_bit(ABS_RZ, wdata->extension.input->absbit);
962 input_set_abs_params(wdata->extension.input,
963 ABS_RX, -500, 500, 2, 4);
964 input_set_abs_params(wdata->extension.input,
965 ABS_RY, -500, 500, 2, 4);
966 input_set_abs_params(wdata->extension.input,
967 ABS_RZ, -500, 500, 2, 4);
968
969 ret = input_register_device(wdata->extension.input);
970 if (ret)
971 goto err_free;
972
973 return 0;
974
975err_free:
976 input_free_device(wdata->extension.input);
977 wdata->extension.input = NULL;
978 return ret;
979}
980
981static void wiimod_nunchuk_remove(const struct wiimod_ops *ops,
982 struct wiimote_data *wdata)
983{
984 if (!wdata->extension.input)
985 return;
986
987 input_unregister_device(wdata->extension.input);
988 wdata->extension.input = NULL;
989}
990
991static const struct wiimod_ops wiimod_nunchuk = {
992 .flags = 0,
993 .arg = 0,
994 .probe = wiimod_nunchuk_probe,
995 .remove = wiimod_nunchuk_remove,
996 .in_ext = wiimod_nunchuk_in_ext,
997};
998
999
1000
1001
1002
1003
1004
1005
1006
1007enum wiimod_classic_keys {
1008 WIIMOD_CLASSIC_KEY_A,
1009 WIIMOD_CLASSIC_KEY_B,
1010 WIIMOD_CLASSIC_KEY_X,
1011 WIIMOD_CLASSIC_KEY_Y,
1012 WIIMOD_CLASSIC_KEY_ZL,
1013 WIIMOD_CLASSIC_KEY_ZR,
1014 WIIMOD_CLASSIC_KEY_PLUS,
1015 WIIMOD_CLASSIC_KEY_MINUS,
1016 WIIMOD_CLASSIC_KEY_HOME,
1017 WIIMOD_CLASSIC_KEY_LEFT,
1018 WIIMOD_CLASSIC_KEY_RIGHT,
1019 WIIMOD_CLASSIC_KEY_UP,
1020 WIIMOD_CLASSIC_KEY_DOWN,
1021 WIIMOD_CLASSIC_KEY_LT,
1022 WIIMOD_CLASSIC_KEY_RT,
1023 WIIMOD_CLASSIC_KEY_NUM,
1024};
1025
1026static const __u16 wiimod_classic_map[] = {
1027 BTN_A,
1028 BTN_B,
1029 BTN_X,
1030 BTN_Y,
1031 BTN_TL2,
1032 BTN_TR2,
1033 KEY_NEXT,
1034 KEY_PREVIOUS,
1035 BTN_MODE,
1036 KEY_LEFT,
1037 KEY_RIGHT,
1038 KEY_UP,
1039 KEY_DOWN,
1040 BTN_TL,
1041 BTN_TR,
1042};
1043
1044static void wiimod_classic_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1045{
1046 __s8 rx, ry, lx, ly, lt, rt;
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1092 lx = ext[0] & 0x3e;
1093 ly = ext[1] & 0x3e;
1094 } else {
1095 lx = ext[0] & 0x3f;
1096 ly = ext[1] & 0x3f;
1097 }
1098
1099 rx = (ext[0] >> 3) & 0x18;
1100 rx |= (ext[1] >> 5) & 0x06;
1101 rx |= (ext[2] >> 7) & 0x01;
1102 ry = ext[2] & 0x1f;
1103
1104 rt = ext[3] & 0x1f;
1105 lt = (ext[2] >> 2) & 0x18;
1106 lt |= (ext[3] >> 5) & 0x07;
1107
1108 rx <<= 1;
1109 ry <<= 1;
1110 rt <<= 1;
1111 lt <<= 1;
1112
1113 input_report_abs(wdata->extension.input, ABS_HAT1X, lx - 0x20);
1114 input_report_abs(wdata->extension.input, ABS_HAT1Y, ly - 0x20);
1115 input_report_abs(wdata->extension.input, ABS_HAT2X, rx - 0x20);
1116 input_report_abs(wdata->extension.input, ABS_HAT2Y, ry - 0x20);
1117 input_report_abs(wdata->extension.input, ABS_HAT3X, rt);
1118 input_report_abs(wdata->extension.input, ABS_HAT3Y, lt);
1119
1120 input_report_key(wdata->extension.input,
1121 wiimod_classic_map[WIIMOD_CLASSIC_KEY_RIGHT],
1122 !(ext[4] & 0x80));
1123 input_report_key(wdata->extension.input,
1124 wiimod_classic_map[WIIMOD_CLASSIC_KEY_DOWN],
1125 !(ext[4] & 0x40));
1126 input_report_key(wdata->extension.input,
1127 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LT],
1128 !(ext[4] & 0x20));
1129 input_report_key(wdata->extension.input,
1130 wiimod_classic_map[WIIMOD_CLASSIC_KEY_MINUS],
1131 !(ext[4] & 0x10));
1132 input_report_key(wdata->extension.input,
1133 wiimod_classic_map[WIIMOD_CLASSIC_KEY_HOME],
1134 !(ext[4] & 0x08));
1135 input_report_key(wdata->extension.input,
1136 wiimod_classic_map[WIIMOD_CLASSIC_KEY_PLUS],
1137 !(ext[4] & 0x04));
1138 input_report_key(wdata->extension.input,
1139 wiimod_classic_map[WIIMOD_CLASSIC_KEY_RT],
1140 !(ext[4] & 0x02));
1141 input_report_key(wdata->extension.input,
1142 wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZL],
1143 !(ext[5] & 0x80));
1144 input_report_key(wdata->extension.input,
1145 wiimod_classic_map[WIIMOD_CLASSIC_KEY_B],
1146 !(ext[5] & 0x40));
1147 input_report_key(wdata->extension.input,
1148 wiimod_classic_map[WIIMOD_CLASSIC_KEY_Y],
1149 !(ext[5] & 0x20));
1150 input_report_key(wdata->extension.input,
1151 wiimod_classic_map[WIIMOD_CLASSIC_KEY_A],
1152 !(ext[5] & 0x10));
1153 input_report_key(wdata->extension.input,
1154 wiimod_classic_map[WIIMOD_CLASSIC_KEY_X],
1155 !(ext[5] & 0x08));
1156 input_report_key(wdata->extension.input,
1157 wiimod_classic_map[WIIMOD_CLASSIC_KEY_ZR],
1158 !(ext[5] & 0x04));
1159
1160 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
1161 input_report_key(wdata->extension.input,
1162 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1163 !(ext[1] & 0x01));
1164 input_report_key(wdata->extension.input,
1165 wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1166 !(ext[0] & 0x01));
1167 } else {
1168 input_report_key(wdata->extension.input,
1169 wiimod_classic_map[WIIMOD_CLASSIC_KEY_LEFT],
1170 !(ext[5] & 0x02));
1171 input_report_key(wdata->extension.input,
1172 wiimod_classic_map[WIIMOD_CLASSIC_KEY_UP],
1173 !(ext[5] & 0x01));
1174 }
1175
1176 input_sync(wdata->extension.input);
1177}
1178
1179static int wiimod_classic_open(struct input_dev *dev)
1180{
1181 struct wiimote_data *wdata = input_get_drvdata(dev);
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&wdata->state.lock, flags);
1185 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1186 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1187 spin_unlock_irqrestore(&wdata->state.lock, flags);
1188
1189 return 0;
1190}
1191
1192static void wiimod_classic_close(struct input_dev *dev)
1193{
1194 struct wiimote_data *wdata = input_get_drvdata(dev);
1195 unsigned long flags;
1196
1197 spin_lock_irqsave(&wdata->state.lock, flags);
1198 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1199 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1200 spin_unlock_irqrestore(&wdata->state.lock, flags);
1201}
1202
1203static int wiimod_classic_probe(const struct wiimod_ops *ops,
1204 struct wiimote_data *wdata)
1205{
1206 int ret, i;
1207
1208 wdata->extension.input = input_allocate_device();
1209 if (!wdata->extension.input)
1210 return -ENOMEM;
1211
1212 input_set_drvdata(wdata->extension.input, wdata);
1213 wdata->extension.input->open = wiimod_classic_open;
1214 wdata->extension.input->close = wiimod_classic_close;
1215 wdata->extension.input->dev.parent = &wdata->hdev->dev;
1216 wdata->extension.input->id.bustype = wdata->hdev->bus;
1217 wdata->extension.input->id.vendor = wdata->hdev->vendor;
1218 wdata->extension.input->id.product = wdata->hdev->product;
1219 wdata->extension.input->id.version = wdata->hdev->version;
1220 wdata->extension.input->name = WIIMOTE_NAME " Classic Controller";
1221
1222 set_bit(EV_KEY, wdata->extension.input->evbit);
1223 for (i = 0; i < WIIMOD_CLASSIC_KEY_NUM; ++i)
1224 set_bit(wiimod_classic_map[i],
1225 wdata->extension.input->keybit);
1226
1227 set_bit(EV_ABS, wdata->extension.input->evbit);
1228 set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1229 set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1230 set_bit(ABS_HAT2X, wdata->extension.input->absbit);
1231 set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
1232 set_bit(ABS_HAT3X, wdata->extension.input->absbit);
1233 set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
1234 input_set_abs_params(wdata->extension.input,
1235 ABS_HAT1X, -30, 30, 1, 1);
1236 input_set_abs_params(wdata->extension.input,
1237 ABS_HAT1Y, -30, 30, 1, 1);
1238 input_set_abs_params(wdata->extension.input,
1239 ABS_HAT2X, -30, 30, 1, 1);
1240 input_set_abs_params(wdata->extension.input,
1241 ABS_HAT2Y, -30, 30, 1, 1);
1242 input_set_abs_params(wdata->extension.input,
1243 ABS_HAT3X, -30, 30, 1, 1);
1244 input_set_abs_params(wdata->extension.input,
1245 ABS_HAT3Y, -30, 30, 1, 1);
1246
1247 ret = input_register_device(wdata->extension.input);
1248 if (ret)
1249 goto err_free;
1250
1251 return 0;
1252
1253err_free:
1254 input_free_device(wdata->extension.input);
1255 wdata->extension.input = NULL;
1256 return ret;
1257}
1258
1259static void wiimod_classic_remove(const struct wiimod_ops *ops,
1260 struct wiimote_data *wdata)
1261{
1262 if (!wdata->extension.input)
1263 return;
1264
1265 input_unregister_device(wdata->extension.input);
1266 wdata->extension.input = NULL;
1267}
1268
1269static const struct wiimod_ops wiimod_classic = {
1270 .flags = 0,
1271 .arg = 0,
1272 .probe = wiimod_classic_probe,
1273 .remove = wiimod_classic_remove,
1274 .in_ext = wiimod_classic_in_ext,
1275};
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290static void wiimod_bboard_in_keys(struct wiimote_data *wdata, const __u8 *keys)
1291{
1292 input_report_key(wdata->extension.input, BTN_A,
1293 !!(keys[1] & 0x08));
1294 input_sync(wdata->extension.input);
1295}
1296
1297static void wiimod_bboard_in_ext(struct wiimote_data *wdata,
1298 const __u8 *ext)
1299{
1300 __s32 val[4], tmp, div;
1301 unsigned int i;
1302 struct wiimote_state *s = &wdata->state;
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328 val[0] = ext[0];
1329 val[0] <<= 8;
1330 val[0] |= ext[1];
1331
1332 val[1] = ext[2];
1333 val[1] <<= 8;
1334 val[1] |= ext[3];
1335
1336 val[2] = ext[4];
1337 val[2] <<= 8;
1338 val[2] |= ext[5];
1339
1340 val[3] = ext[6];
1341 val[3] <<= 8;
1342 val[3] |= ext[7];
1343
1344
1345 for (i = 0; i < 4; i++) {
1346 if (val[i] <= s->calib_bboard[i][0]) {
1347 tmp = 0;
1348 } else if (val[i] < s->calib_bboard[i][1]) {
1349 tmp = val[i] - s->calib_bboard[i][0];
1350 tmp *= 1700;
1351 div = s->calib_bboard[i][1] - s->calib_bboard[i][0];
1352 tmp /= div ? div : 1;
1353 } else {
1354 tmp = val[i] - s->calib_bboard[i][1];
1355 tmp *= 1700;
1356 div = s->calib_bboard[i][2] - s->calib_bboard[i][1];
1357 tmp /= div ? div : 1;
1358 tmp += 1700;
1359 }
1360 val[i] = tmp;
1361 }
1362
1363 input_report_abs(wdata->extension.input, ABS_HAT0X, val[0]);
1364 input_report_abs(wdata->extension.input, ABS_HAT0Y, val[1]);
1365 input_report_abs(wdata->extension.input, ABS_HAT1X, val[2]);
1366 input_report_abs(wdata->extension.input, ABS_HAT1Y, val[3]);
1367 input_sync(wdata->extension.input);
1368}
1369
1370static int wiimod_bboard_open(struct input_dev *dev)
1371{
1372 struct wiimote_data *wdata = input_get_drvdata(dev);
1373 unsigned long flags;
1374
1375 spin_lock_irqsave(&wdata->state.lock, flags);
1376 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1377 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1378 spin_unlock_irqrestore(&wdata->state.lock, flags);
1379
1380 return 0;
1381}
1382
1383static void wiimod_bboard_close(struct input_dev *dev)
1384{
1385 struct wiimote_data *wdata = input_get_drvdata(dev);
1386 unsigned long flags;
1387
1388 spin_lock_irqsave(&wdata->state.lock, flags);
1389 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1390 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1391 spin_unlock_irqrestore(&wdata->state.lock, flags);
1392}
1393
1394static ssize_t wiimod_bboard_calib_show(struct device *dev,
1395 struct device_attribute *attr,
1396 char *out)
1397{
1398 struct wiimote_data *wdata = dev_to_wii(dev);
1399 int i, j, ret;
1400 __u16 val;
1401 __u8 buf[24], offs;
1402
1403 ret = wiimote_cmd_acquire(wdata);
1404 if (ret)
1405 return ret;
1406
1407 ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1408 if (ret != 12) {
1409 wiimote_cmd_release(wdata);
1410 return ret < 0 ? ret : -EIO;
1411 }
1412 ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1413 if (ret != 12) {
1414 wiimote_cmd_release(wdata);
1415 return ret < 0 ? ret : -EIO;
1416 }
1417
1418 wiimote_cmd_release(wdata);
1419
1420 spin_lock_irq(&wdata->state.lock);
1421 offs = 0;
1422 for (i = 0; i < 3; ++i) {
1423 for (j = 0; j < 4; ++j) {
1424 wdata->state.calib_bboard[j][i] = buf[offs];
1425 wdata->state.calib_bboard[j][i] <<= 8;
1426 wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1427 offs += 2;
1428 }
1429 }
1430 spin_unlock_irq(&wdata->state.lock);
1431
1432 ret = 0;
1433 for (i = 0; i < 3; ++i) {
1434 for (j = 0; j < 4; ++j) {
1435 val = wdata->state.calib_bboard[j][i];
1436 if (i == 2 && j == 3)
1437 ret += sprintf(&out[ret], "%04x\n", val);
1438 else
1439 ret += sprintf(&out[ret], "%04x:", val);
1440 }
1441 }
1442
1443 return ret;
1444}
1445
1446static DEVICE_ATTR(bboard_calib, S_IRUGO, wiimod_bboard_calib_show, NULL);
1447
1448static int wiimod_bboard_probe(const struct wiimod_ops *ops,
1449 struct wiimote_data *wdata)
1450{
1451 int ret, i, j;
1452 __u8 buf[24], offs;
1453
1454 wiimote_cmd_acquire_noint(wdata);
1455
1456 ret = wiimote_cmd_read(wdata, 0xa40024, buf, 12);
1457 if (ret != 12) {
1458 wiimote_cmd_release(wdata);
1459 return ret < 0 ? ret : -EIO;
1460 }
1461 ret = wiimote_cmd_read(wdata, 0xa40024 + 12, buf + 12, 12);
1462 if (ret != 12) {
1463 wiimote_cmd_release(wdata);
1464 return ret < 0 ? ret : -EIO;
1465 }
1466
1467 wiimote_cmd_release(wdata);
1468
1469 offs = 0;
1470 for (i = 0; i < 3; ++i) {
1471 for (j = 0; j < 4; ++j) {
1472 wdata->state.calib_bboard[j][i] = buf[offs];
1473 wdata->state.calib_bboard[j][i] <<= 8;
1474 wdata->state.calib_bboard[j][i] |= buf[offs + 1];
1475 offs += 2;
1476 }
1477 }
1478
1479 wdata->extension.input = input_allocate_device();
1480 if (!wdata->extension.input)
1481 return -ENOMEM;
1482
1483 ret = device_create_file(&wdata->hdev->dev,
1484 &dev_attr_bboard_calib);
1485 if (ret) {
1486 hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1487 goto err_free;
1488 }
1489
1490 input_set_drvdata(wdata->extension.input, wdata);
1491 wdata->extension.input->open = wiimod_bboard_open;
1492 wdata->extension.input->close = wiimod_bboard_close;
1493 wdata->extension.input->dev.parent = &wdata->hdev->dev;
1494 wdata->extension.input->id.bustype = wdata->hdev->bus;
1495 wdata->extension.input->id.vendor = wdata->hdev->vendor;
1496 wdata->extension.input->id.product = wdata->hdev->product;
1497 wdata->extension.input->id.version = wdata->hdev->version;
1498 wdata->extension.input->name = WIIMOTE_NAME " Balance Board";
1499
1500 set_bit(EV_KEY, wdata->extension.input->evbit);
1501 set_bit(BTN_A, wdata->extension.input->keybit);
1502
1503 set_bit(EV_ABS, wdata->extension.input->evbit);
1504 set_bit(ABS_HAT0X, wdata->extension.input->absbit);
1505 set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
1506 set_bit(ABS_HAT1X, wdata->extension.input->absbit);
1507 set_bit(ABS_HAT1Y, wdata->extension.input->absbit);
1508 input_set_abs_params(wdata->extension.input,
1509 ABS_HAT0X, 0, 65535, 2, 4);
1510 input_set_abs_params(wdata->extension.input,
1511 ABS_HAT0Y, 0, 65535, 2, 4);
1512 input_set_abs_params(wdata->extension.input,
1513 ABS_HAT1X, 0, 65535, 2, 4);
1514 input_set_abs_params(wdata->extension.input,
1515 ABS_HAT1Y, 0, 65535, 2, 4);
1516
1517 ret = input_register_device(wdata->extension.input);
1518 if (ret)
1519 goto err_file;
1520
1521 return 0;
1522
1523err_file:
1524 device_remove_file(&wdata->hdev->dev,
1525 &dev_attr_bboard_calib);
1526err_free:
1527 input_free_device(wdata->extension.input);
1528 wdata->extension.input = NULL;
1529 return ret;
1530}
1531
1532static void wiimod_bboard_remove(const struct wiimod_ops *ops,
1533 struct wiimote_data *wdata)
1534{
1535 if (!wdata->extension.input)
1536 return;
1537
1538 input_unregister_device(wdata->extension.input);
1539 wdata->extension.input = NULL;
1540 device_remove_file(&wdata->hdev->dev,
1541 &dev_attr_bboard_calib);
1542}
1543
1544static const struct wiimod_ops wiimod_bboard = {
1545 .flags = WIIMOD_FLAG_EXT8,
1546 .arg = 0,
1547 .probe = wiimod_bboard_probe,
1548 .remove = wiimod_bboard_remove,
1549 .in_keys = wiimod_bboard_in_keys,
1550 .in_ext = wiimod_bboard_in_ext,
1551};
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563enum wiimod_pro_keys {
1564 WIIMOD_PRO_KEY_A,
1565 WIIMOD_PRO_KEY_B,
1566 WIIMOD_PRO_KEY_X,
1567 WIIMOD_PRO_KEY_Y,
1568 WIIMOD_PRO_KEY_PLUS,
1569 WIIMOD_PRO_KEY_MINUS,
1570 WIIMOD_PRO_KEY_HOME,
1571 WIIMOD_PRO_KEY_LEFT,
1572 WIIMOD_PRO_KEY_RIGHT,
1573 WIIMOD_PRO_KEY_UP,
1574 WIIMOD_PRO_KEY_DOWN,
1575 WIIMOD_PRO_KEY_TL,
1576 WIIMOD_PRO_KEY_TR,
1577 WIIMOD_PRO_KEY_ZL,
1578 WIIMOD_PRO_KEY_ZR,
1579 WIIMOD_PRO_KEY_THUMBL,
1580 WIIMOD_PRO_KEY_THUMBR,
1581 WIIMOD_PRO_KEY_NUM,
1582};
1583
1584static const __u16 wiimod_pro_map[] = {
1585 BTN_EAST,
1586 BTN_SOUTH,
1587 BTN_NORTH,
1588 BTN_WEST,
1589 BTN_START,
1590 BTN_SELECT,
1591 BTN_MODE,
1592 BTN_DPAD_LEFT,
1593 BTN_DPAD_RIGHT,
1594 BTN_DPAD_UP,
1595 BTN_DPAD_DOWN,
1596 BTN_TL,
1597 BTN_TR,
1598 BTN_TL2,
1599 BTN_TR2,
1600 BTN_THUMBL,
1601 BTN_THUMBR,
1602};
1603
1604static void wiimod_pro_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1605{
1606 __s16 rx, ry, lx, ly;
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649 lx = (ext[0] & 0xff) | ((ext[1] & 0x0f) << 8);
1650 rx = (ext[2] & 0xff) | ((ext[3] & 0x0f) << 8);
1651 ly = (ext[4] & 0xff) | ((ext[5] & 0x0f) << 8);
1652 ry = (ext[6] & 0xff) | ((ext[7] & 0x0f) << 8);
1653
1654
1655 lx -= 0x800;
1656 ly = 0x800 - ly;
1657 rx -= 0x800;
1658 ry = 0x800 - ry;
1659
1660
1661
1662
1663
1664
1665 if (!(wdata->state.flags & WIIPROTO_FLAG_PRO_CALIB_DONE)) {
1666 wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1667 if (abs(lx) < 500)
1668 wdata->state.calib_pro_sticks[0] = -lx;
1669 if (abs(ly) < 500)
1670 wdata->state.calib_pro_sticks[1] = -ly;
1671 if (abs(rx) < 500)
1672 wdata->state.calib_pro_sticks[2] = -rx;
1673 if (abs(ry) < 500)
1674 wdata->state.calib_pro_sticks[3] = -ry;
1675 }
1676
1677
1678 lx += wdata->state.calib_pro_sticks[0];
1679 ly += wdata->state.calib_pro_sticks[1];
1680 rx += wdata->state.calib_pro_sticks[2];
1681 ry += wdata->state.calib_pro_sticks[3];
1682
1683 input_report_abs(wdata->extension.input, ABS_X, lx);
1684 input_report_abs(wdata->extension.input, ABS_Y, ly);
1685 input_report_abs(wdata->extension.input, ABS_RX, rx);
1686 input_report_abs(wdata->extension.input, ABS_RY, ry);
1687
1688 input_report_key(wdata->extension.input,
1689 wiimod_pro_map[WIIMOD_PRO_KEY_RIGHT],
1690 !(ext[8] & 0x80));
1691 input_report_key(wdata->extension.input,
1692 wiimod_pro_map[WIIMOD_PRO_KEY_DOWN],
1693 !(ext[8] & 0x40));
1694 input_report_key(wdata->extension.input,
1695 wiimod_pro_map[WIIMOD_PRO_KEY_TL],
1696 !(ext[8] & 0x20));
1697 input_report_key(wdata->extension.input,
1698 wiimod_pro_map[WIIMOD_PRO_KEY_MINUS],
1699 !(ext[8] & 0x10));
1700 input_report_key(wdata->extension.input,
1701 wiimod_pro_map[WIIMOD_PRO_KEY_HOME],
1702 !(ext[8] & 0x08));
1703 input_report_key(wdata->extension.input,
1704 wiimod_pro_map[WIIMOD_PRO_KEY_PLUS],
1705 !(ext[8] & 0x04));
1706 input_report_key(wdata->extension.input,
1707 wiimod_pro_map[WIIMOD_PRO_KEY_TR],
1708 !(ext[8] & 0x02));
1709
1710 input_report_key(wdata->extension.input,
1711 wiimod_pro_map[WIIMOD_PRO_KEY_ZL],
1712 !(ext[9] & 0x80));
1713 input_report_key(wdata->extension.input,
1714 wiimod_pro_map[WIIMOD_PRO_KEY_B],
1715 !(ext[9] & 0x40));
1716 input_report_key(wdata->extension.input,
1717 wiimod_pro_map[WIIMOD_PRO_KEY_Y],
1718 !(ext[9] & 0x20));
1719 input_report_key(wdata->extension.input,
1720 wiimod_pro_map[WIIMOD_PRO_KEY_A],
1721 !(ext[9] & 0x10));
1722 input_report_key(wdata->extension.input,
1723 wiimod_pro_map[WIIMOD_PRO_KEY_X],
1724 !(ext[9] & 0x08));
1725 input_report_key(wdata->extension.input,
1726 wiimod_pro_map[WIIMOD_PRO_KEY_ZR],
1727 !(ext[9] & 0x04));
1728 input_report_key(wdata->extension.input,
1729 wiimod_pro_map[WIIMOD_PRO_KEY_LEFT],
1730 !(ext[9] & 0x02));
1731 input_report_key(wdata->extension.input,
1732 wiimod_pro_map[WIIMOD_PRO_KEY_UP],
1733 !(ext[9] & 0x01));
1734
1735 input_report_key(wdata->extension.input,
1736 wiimod_pro_map[WIIMOD_PRO_KEY_THUMBL],
1737 !(ext[10] & 0x02));
1738 input_report_key(wdata->extension.input,
1739 wiimod_pro_map[WIIMOD_PRO_KEY_THUMBR],
1740 !(ext[10] & 0x01));
1741
1742 input_sync(wdata->extension.input);
1743}
1744
1745static int wiimod_pro_open(struct input_dev *dev)
1746{
1747 struct wiimote_data *wdata = input_get_drvdata(dev);
1748 unsigned long flags;
1749
1750 spin_lock_irqsave(&wdata->state.lock, flags);
1751 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
1752 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1753 spin_unlock_irqrestore(&wdata->state.lock, flags);
1754
1755 return 0;
1756}
1757
1758static void wiimod_pro_close(struct input_dev *dev)
1759{
1760 struct wiimote_data *wdata = input_get_drvdata(dev);
1761 unsigned long flags;
1762
1763 spin_lock_irqsave(&wdata->state.lock, flags);
1764 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
1765 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
1766 spin_unlock_irqrestore(&wdata->state.lock, flags);
1767}
1768
1769static int wiimod_pro_play(struct input_dev *dev, void *data,
1770 struct ff_effect *eff)
1771{
1772 struct wiimote_data *wdata = input_get_drvdata(dev);
1773 __u8 value;
1774
1775
1776
1777
1778
1779
1780
1781 if (eff->u.rumble.strong_magnitude || eff->u.rumble.weak_magnitude)
1782 value = 1;
1783 else
1784 value = 0;
1785
1786
1787
1788 wdata->state.cache_rumble = value;
1789 schedule_work(&wdata->rumble_worker);
1790
1791 return 0;
1792}
1793
1794static ssize_t wiimod_pro_calib_show(struct device *dev,
1795 struct device_attribute *attr,
1796 char *out)
1797{
1798 struct wiimote_data *wdata = dev_to_wii(dev);
1799 int r;
1800
1801 r = 0;
1802 r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[0]);
1803 r += sprintf(&out[r], "%+06hd ", wdata->state.calib_pro_sticks[1]);
1804 r += sprintf(&out[r], "%+06hd:", wdata->state.calib_pro_sticks[2]);
1805 r += sprintf(&out[r], "%+06hd\n", wdata->state.calib_pro_sticks[3]);
1806
1807 return r;
1808}
1809
1810static ssize_t wiimod_pro_calib_store(struct device *dev,
1811 struct device_attribute *attr,
1812 const char *buf, size_t count)
1813{
1814 struct wiimote_data *wdata = dev_to_wii(dev);
1815 int r;
1816 s16 x1, y1, x2, y2;
1817
1818 if (!strncmp(buf, "scan\n", 5)) {
1819 spin_lock_irq(&wdata->state.lock);
1820 wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1821 spin_unlock_irq(&wdata->state.lock);
1822 } else {
1823 r = sscanf(buf, "%hd:%hd %hd:%hd", &x1, &y1, &x2, &y2);
1824 if (r != 4)
1825 return -EINVAL;
1826
1827 spin_lock_irq(&wdata->state.lock);
1828 wdata->state.flags |= WIIPROTO_FLAG_PRO_CALIB_DONE;
1829 spin_unlock_irq(&wdata->state.lock);
1830
1831 wdata->state.calib_pro_sticks[0] = x1;
1832 wdata->state.calib_pro_sticks[1] = y1;
1833 wdata->state.calib_pro_sticks[2] = x2;
1834 wdata->state.calib_pro_sticks[3] = y2;
1835 }
1836
1837 return strnlen(buf, PAGE_SIZE);
1838}
1839
1840static DEVICE_ATTR(pro_calib, S_IRUGO|S_IWUSR|S_IWGRP, wiimod_pro_calib_show,
1841 wiimod_pro_calib_store);
1842
1843static int wiimod_pro_probe(const struct wiimod_ops *ops,
1844 struct wiimote_data *wdata)
1845{
1846 int ret, i;
1847 unsigned long flags;
1848
1849 INIT_WORK(&wdata->rumble_worker, wiimod_rumble_worker);
1850 wdata->state.calib_pro_sticks[0] = 0;
1851 wdata->state.calib_pro_sticks[1] = 0;
1852 wdata->state.calib_pro_sticks[2] = 0;
1853 wdata->state.calib_pro_sticks[3] = 0;
1854
1855 spin_lock_irqsave(&wdata->state.lock, flags);
1856 wdata->state.flags &= ~WIIPROTO_FLAG_PRO_CALIB_DONE;
1857 spin_unlock_irqrestore(&wdata->state.lock, flags);
1858
1859 wdata->extension.input = input_allocate_device();
1860 if (!wdata->extension.input)
1861 return -ENOMEM;
1862
1863 set_bit(FF_RUMBLE, wdata->extension.input->ffbit);
1864 input_set_drvdata(wdata->extension.input, wdata);
1865
1866 if (input_ff_create_memless(wdata->extension.input, NULL,
1867 wiimod_pro_play)) {
1868 ret = -ENOMEM;
1869 goto err_free;
1870 }
1871
1872 ret = device_create_file(&wdata->hdev->dev,
1873 &dev_attr_pro_calib);
1874 if (ret) {
1875 hid_err(wdata->hdev, "cannot create sysfs attribute\n");
1876 goto err_free;
1877 }
1878
1879 wdata->extension.input->open = wiimod_pro_open;
1880 wdata->extension.input->close = wiimod_pro_close;
1881 wdata->extension.input->dev.parent = &wdata->hdev->dev;
1882 wdata->extension.input->id.bustype = wdata->hdev->bus;
1883 wdata->extension.input->id.vendor = wdata->hdev->vendor;
1884 wdata->extension.input->id.product = wdata->hdev->product;
1885 wdata->extension.input->id.version = wdata->hdev->version;
1886 wdata->extension.input->name = WIIMOTE_NAME " Pro Controller";
1887
1888 set_bit(EV_KEY, wdata->extension.input->evbit);
1889 for (i = 0; i < WIIMOD_PRO_KEY_NUM; ++i)
1890 set_bit(wiimod_pro_map[i],
1891 wdata->extension.input->keybit);
1892
1893 set_bit(EV_ABS, wdata->extension.input->evbit);
1894 set_bit(ABS_X, wdata->extension.input->absbit);
1895 set_bit(ABS_Y, wdata->extension.input->absbit);
1896 set_bit(ABS_RX, wdata->extension.input->absbit);
1897 set_bit(ABS_RY, wdata->extension.input->absbit);
1898 input_set_abs_params(wdata->extension.input,
1899 ABS_X, -0x400, 0x400, 4, 100);
1900 input_set_abs_params(wdata->extension.input,
1901 ABS_Y, -0x400, 0x400, 4, 100);
1902 input_set_abs_params(wdata->extension.input,
1903 ABS_RX, -0x400, 0x400, 4, 100);
1904 input_set_abs_params(wdata->extension.input,
1905 ABS_RY, -0x400, 0x400, 4, 100);
1906
1907 ret = input_register_device(wdata->extension.input);
1908 if (ret)
1909 goto err_file;
1910
1911 return 0;
1912
1913err_file:
1914 device_remove_file(&wdata->hdev->dev,
1915 &dev_attr_pro_calib);
1916err_free:
1917 input_free_device(wdata->extension.input);
1918 wdata->extension.input = NULL;
1919 return ret;
1920}
1921
1922static void wiimod_pro_remove(const struct wiimod_ops *ops,
1923 struct wiimote_data *wdata)
1924{
1925 unsigned long flags;
1926
1927 if (!wdata->extension.input)
1928 return;
1929
1930 input_unregister_device(wdata->extension.input);
1931 wdata->extension.input = NULL;
1932 cancel_work_sync(&wdata->rumble_worker);
1933 device_remove_file(&wdata->hdev->dev,
1934 &dev_attr_pro_calib);
1935
1936 spin_lock_irqsave(&wdata->state.lock, flags);
1937 wiiproto_req_rumble(wdata, 0);
1938 spin_unlock_irqrestore(&wdata->state.lock, flags);
1939}
1940
1941static const struct wiimod_ops wiimod_pro = {
1942 .flags = WIIMOD_FLAG_EXT16,
1943 .arg = 0,
1944 .probe = wiimod_pro_probe,
1945 .remove = wiimod_pro_remove,
1946 .in_ext = wiimod_pro_in_ext,
1947};
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958static inline void wiimod_drums_report_pressure(struct wiimote_data *wdata,
1959 __u8 none, __u8 which,
1960 __u8 pressure, __u8 onoff,
1961 __u8 *store, __u16 code,
1962 __u8 which_code)
1963{
1964 static const __u8 default_pressure = 3;
1965
1966 if (!none && which == which_code) {
1967 *store = pressure;
1968 input_report_abs(wdata->extension.input, code, *store);
1969 } else if (onoff != !!*store) {
1970 *store = onoff ? default_pressure : 0;
1971 input_report_abs(wdata->extension.input, code, *store);
1972 }
1973}
1974
1975static void wiimod_drums_in_ext(struct wiimote_data *wdata, const __u8 *ext)
1976{
1977 __u8 pressure, which, none, hhp, sx, sy;
1978 __u8 o, r, y, g, b, bass, bm, bp;
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011 pressure = 7 - (ext[3] >> 5);
2012 which = (ext[2] >> 1) & 0x1f;
2013 none = !!(ext[2] & 0x40);
2014 hhp = !(ext[2] & 0x80);
2015 sx = ext[0] & 0x3f;
2016 sy = ext[1] & 0x3f;
2017 o = !(ext[5] & 0x80);
2018 r = !(ext[5] & 0x40);
2019 y = !(ext[5] & 0x20);
2020 g = !(ext[5] & 0x10);
2021 b = !(ext[5] & 0x08);
2022 bass = !(ext[5] & 0x04);
2023 bm = !(ext[4] & 0x10);
2024 bp = !(ext[4] & 0x04);
2025
2026 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
2027 sx &= 0x3e;
2028 sy &= 0x3e;
2029 }
2030
2031 wiimod_drums_report_pressure(wdata, none, which, pressure,
2032 o, &wdata->state.pressure_drums[0],
2033 ABS_HAT2Y, 0x0e);
2034 wiimod_drums_report_pressure(wdata, none, which, pressure,
2035 r, &wdata->state.pressure_drums[1],
2036 ABS_HAT0X, 0x19);
2037 wiimod_drums_report_pressure(wdata, none, which, pressure,
2038 y, &wdata->state.pressure_drums[2],
2039 ABS_HAT2X, 0x11);
2040 wiimod_drums_report_pressure(wdata, none, which, pressure,
2041 g, &wdata->state.pressure_drums[3],
2042 ABS_HAT1X, 0x12);
2043 wiimod_drums_report_pressure(wdata, none, which, pressure,
2044 b, &wdata->state.pressure_drums[4],
2045 ABS_HAT0Y, 0x0f);
2046
2047
2048 wiimod_drums_report_pressure(wdata, none, hhp ? 0xff : which, pressure,
2049 bass, &wdata->state.pressure_drums[5],
2050 ABS_HAT3X, 0x1b);
2051
2052 wiimod_drums_report_pressure(wdata, none, hhp ? which : 0xff, pressure,
2053 0, &wdata->state.pressure_drums[6],
2054 ABS_HAT3Y, 0x0e);
2055
2056 input_report_abs(wdata->extension.input, ABS_X, sx - 0x20);
2057 input_report_abs(wdata->extension.input, ABS_Y, sy - 0x20);
2058
2059 input_report_key(wdata->extension.input, BTN_START, bp);
2060 input_report_key(wdata->extension.input, BTN_SELECT, bm);
2061
2062 input_sync(wdata->extension.input);
2063}
2064
2065static int wiimod_drums_open(struct input_dev *dev)
2066{
2067 struct wiimote_data *wdata = input_get_drvdata(dev);
2068 unsigned long flags;
2069
2070 spin_lock_irqsave(&wdata->state.lock, flags);
2071 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
2072 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2073 spin_unlock_irqrestore(&wdata->state.lock, flags);
2074
2075 return 0;
2076}
2077
2078static void wiimod_drums_close(struct input_dev *dev)
2079{
2080 struct wiimote_data *wdata = input_get_drvdata(dev);
2081 unsigned long flags;
2082
2083 spin_lock_irqsave(&wdata->state.lock, flags);
2084 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
2085 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2086 spin_unlock_irqrestore(&wdata->state.lock, flags);
2087}
2088
2089static int wiimod_drums_probe(const struct wiimod_ops *ops,
2090 struct wiimote_data *wdata)
2091{
2092 int ret;
2093
2094 wdata->extension.input = input_allocate_device();
2095 if (!wdata->extension.input)
2096 return -ENOMEM;
2097
2098 input_set_drvdata(wdata->extension.input, wdata);
2099 wdata->extension.input->open = wiimod_drums_open;
2100 wdata->extension.input->close = wiimod_drums_close;
2101 wdata->extension.input->dev.parent = &wdata->hdev->dev;
2102 wdata->extension.input->id.bustype = wdata->hdev->bus;
2103 wdata->extension.input->id.vendor = wdata->hdev->vendor;
2104 wdata->extension.input->id.product = wdata->hdev->product;
2105 wdata->extension.input->id.version = wdata->hdev->version;
2106 wdata->extension.input->name = WIIMOTE_NAME " Drums";
2107
2108 set_bit(EV_KEY, wdata->extension.input->evbit);
2109 set_bit(BTN_START, wdata->extension.input->keybit);
2110 set_bit(BTN_SELECT, wdata->extension.input->keybit);
2111
2112 set_bit(EV_ABS, wdata->extension.input->evbit);
2113 set_bit(ABS_X, wdata->extension.input->absbit);
2114 set_bit(ABS_Y, wdata->extension.input->absbit);
2115 set_bit(ABS_HAT0X, wdata->extension.input->absbit);
2116 set_bit(ABS_HAT0Y, wdata->extension.input->absbit);
2117 set_bit(ABS_HAT1X, wdata->extension.input->absbit);
2118 set_bit(ABS_HAT2X, wdata->extension.input->absbit);
2119 set_bit(ABS_HAT2Y, wdata->extension.input->absbit);
2120 set_bit(ABS_HAT3X, wdata->extension.input->absbit);
2121 set_bit(ABS_HAT3Y, wdata->extension.input->absbit);
2122 input_set_abs_params(wdata->extension.input,
2123 ABS_X, -32, 31, 1, 1);
2124 input_set_abs_params(wdata->extension.input,
2125 ABS_Y, -32, 31, 1, 1);
2126 input_set_abs_params(wdata->extension.input,
2127 ABS_HAT0X, 0, 7, 0, 0);
2128 input_set_abs_params(wdata->extension.input,
2129 ABS_HAT0Y, 0, 7, 0, 0);
2130 input_set_abs_params(wdata->extension.input,
2131 ABS_HAT1X, 0, 7, 0, 0);
2132 input_set_abs_params(wdata->extension.input,
2133 ABS_HAT2X, 0, 7, 0, 0);
2134 input_set_abs_params(wdata->extension.input,
2135 ABS_HAT2Y, 0, 7, 0, 0);
2136 input_set_abs_params(wdata->extension.input,
2137 ABS_HAT3X, 0, 7, 0, 0);
2138 input_set_abs_params(wdata->extension.input,
2139 ABS_HAT3Y, 0, 7, 0, 0);
2140
2141 ret = input_register_device(wdata->extension.input);
2142 if (ret)
2143 goto err_free;
2144
2145 return 0;
2146
2147err_free:
2148 input_free_device(wdata->extension.input);
2149 wdata->extension.input = NULL;
2150 return ret;
2151}
2152
2153static void wiimod_drums_remove(const struct wiimod_ops *ops,
2154 struct wiimote_data *wdata)
2155{
2156 if (!wdata->extension.input)
2157 return;
2158
2159 input_unregister_device(wdata->extension.input);
2160 wdata->extension.input = NULL;
2161}
2162
2163static const struct wiimod_ops wiimod_drums = {
2164 .flags = 0,
2165 .arg = 0,
2166 .probe = wiimod_drums_probe,
2167 .remove = wiimod_drums_remove,
2168 .in_ext = wiimod_drums_in_ext,
2169};
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179enum wiimod_guitar_keys {
2180 WIIMOD_GUITAR_KEY_G,
2181 WIIMOD_GUITAR_KEY_R,
2182 WIIMOD_GUITAR_KEY_Y,
2183 WIIMOD_GUITAR_KEY_B,
2184 WIIMOD_GUITAR_KEY_O,
2185 WIIMOD_GUITAR_KEY_UP,
2186 WIIMOD_GUITAR_KEY_DOWN,
2187 WIIMOD_GUITAR_KEY_PLUS,
2188 WIIMOD_GUITAR_KEY_MINUS,
2189 WIIMOD_GUITAR_KEY_NUM,
2190};
2191
2192static const __u16 wiimod_guitar_map[] = {
2193 BTN_1,
2194 BTN_2,
2195 BTN_3,
2196 BTN_4,
2197 BTN_5,
2198 BTN_DPAD_UP,
2199 BTN_DPAD_DOWN,
2200 BTN_START,
2201 BTN_SELECT,
2202};
2203
2204static void wiimod_guitar_in_ext(struct wiimote_data *wdata, const __u8 *ext)
2205{
2206 __u8 sx, sy, tb, wb, bd, bm, bp, bo, br, bb, bg, by, bu;
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239 sx = ext[0] & 0x3f;
2240 sy = ext[1] & 0x3f;
2241 tb = ext[2] & 0x1f;
2242 wb = ext[3] & 0x1f;
2243 bd = !(ext[4] & 0x40);
2244 bm = !(ext[4] & 0x10);
2245 bp = !(ext[4] & 0x04);
2246 bo = !(ext[5] & 0x80);
2247 br = !(ext[5] & 0x40);
2248 bb = !(ext[5] & 0x20);
2249 bg = !(ext[5] & 0x10);
2250 by = !(ext[5] & 0x08);
2251 bu = !(ext[5] & 0x01);
2252
2253 if (wdata->state.flags & WIIPROTO_FLAG_MP_ACTIVE) {
2254 bu = !(ext[0] & 0x01);
2255 sx &= 0x3e;
2256 sy &= 0x3e;
2257 }
2258
2259 input_report_abs(wdata->extension.input, ABS_X, sx - 0x20);
2260 input_report_abs(wdata->extension.input, ABS_Y, sy - 0x20);
2261 input_report_abs(wdata->extension.input, ABS_HAT0X, tb);
2262 input_report_abs(wdata->extension.input, ABS_HAT1X, wb - 0x10);
2263
2264 input_report_key(wdata->extension.input,
2265 wiimod_guitar_map[WIIMOD_GUITAR_KEY_G],
2266 bg);
2267 input_report_key(wdata->extension.input,
2268 wiimod_guitar_map[WIIMOD_GUITAR_KEY_R],
2269 br);
2270 input_report_key(wdata->extension.input,
2271 wiimod_guitar_map[WIIMOD_GUITAR_KEY_Y],
2272 by);
2273 input_report_key(wdata->extension.input,
2274 wiimod_guitar_map[WIIMOD_GUITAR_KEY_B],
2275 bb);
2276 input_report_key(wdata->extension.input,
2277 wiimod_guitar_map[WIIMOD_GUITAR_KEY_O],
2278 bo);
2279 input_report_key(wdata->extension.input,
2280 wiimod_guitar_map[WIIMOD_GUITAR_KEY_UP],
2281 bu);
2282 input_report_key(wdata->extension.input,
2283 wiimod_guitar_map[WIIMOD_GUITAR_KEY_DOWN],
2284 bd);
2285 input_report_key(wdata->extension.input,
2286 wiimod_guitar_map[WIIMOD_GUITAR_KEY_PLUS],
2287 bp);
2288 input_report_key(wdata->extension.input,
2289 wiimod_guitar_map[WIIMOD_GUITAR_KEY_MINUS],
2290 bm);
2291
2292 input_sync(wdata->extension.input);
2293}
2294
2295static int wiimod_guitar_open(struct input_dev *dev)
2296{
2297 struct wiimote_data *wdata = input_get_drvdata(dev);
2298 unsigned long flags;
2299
2300 spin_lock_irqsave(&wdata->state.lock, flags);
2301 wdata->state.flags |= WIIPROTO_FLAG_EXT_USED;
2302 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2303 spin_unlock_irqrestore(&wdata->state.lock, flags);
2304
2305 return 0;
2306}
2307
2308static void wiimod_guitar_close(struct input_dev *dev)
2309{
2310 struct wiimote_data *wdata = input_get_drvdata(dev);
2311 unsigned long flags;
2312
2313 spin_lock_irqsave(&wdata->state.lock, flags);
2314 wdata->state.flags &= ~WIIPROTO_FLAG_EXT_USED;
2315 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2316 spin_unlock_irqrestore(&wdata->state.lock, flags);
2317}
2318
2319static int wiimod_guitar_probe(const struct wiimod_ops *ops,
2320 struct wiimote_data *wdata)
2321{
2322 int ret, i;
2323
2324 wdata->extension.input = input_allocate_device();
2325 if (!wdata->extension.input)
2326 return -ENOMEM;
2327
2328 input_set_drvdata(wdata->extension.input, wdata);
2329 wdata->extension.input->open = wiimod_guitar_open;
2330 wdata->extension.input->close = wiimod_guitar_close;
2331 wdata->extension.input->dev.parent = &wdata->hdev->dev;
2332 wdata->extension.input->id.bustype = wdata->hdev->bus;
2333 wdata->extension.input->id.vendor = wdata->hdev->vendor;
2334 wdata->extension.input->id.product = wdata->hdev->product;
2335 wdata->extension.input->id.version = wdata->hdev->version;
2336 wdata->extension.input->name = WIIMOTE_NAME " Guitar";
2337
2338 set_bit(EV_KEY, wdata->extension.input->evbit);
2339 for (i = 0; i < WIIMOD_GUITAR_KEY_NUM; ++i)
2340 set_bit(wiimod_guitar_map[i],
2341 wdata->extension.input->keybit);
2342
2343 set_bit(EV_ABS, wdata->extension.input->evbit);
2344 set_bit(ABS_X, wdata->extension.input->absbit);
2345 set_bit(ABS_Y, wdata->extension.input->absbit);
2346 set_bit(ABS_HAT0X, wdata->extension.input->absbit);
2347 set_bit(ABS_HAT1X, wdata->extension.input->absbit);
2348 input_set_abs_params(wdata->extension.input,
2349 ABS_X, -32, 31, 1, 1);
2350 input_set_abs_params(wdata->extension.input,
2351 ABS_Y, -32, 31, 1, 1);
2352 input_set_abs_params(wdata->extension.input,
2353 ABS_HAT0X, 0, 0x1f, 1, 1);
2354 input_set_abs_params(wdata->extension.input,
2355 ABS_HAT1X, 0, 0x0f, 1, 1);
2356
2357 ret = input_register_device(wdata->extension.input);
2358 if (ret)
2359 goto err_free;
2360
2361 return 0;
2362
2363err_free:
2364 input_free_device(wdata->extension.input);
2365 wdata->extension.input = NULL;
2366 return ret;
2367}
2368
2369static void wiimod_guitar_remove(const struct wiimod_ops *ops,
2370 struct wiimote_data *wdata)
2371{
2372 if (!wdata->extension.input)
2373 return;
2374
2375 input_unregister_device(wdata->extension.input);
2376 wdata->extension.input = NULL;
2377}
2378
2379static const struct wiimod_ops wiimod_guitar = {
2380 .flags = 0,
2381 .arg = 0,
2382 .probe = wiimod_guitar_probe,
2383 .remove = wiimod_guitar_remove,
2384 .in_ext = wiimod_guitar_in_ext,
2385};
2386
2387
2388
2389
2390
2391
2392
2393
2394static int wiimod_builtin_mp_probe(const struct wiimod_ops *ops,
2395 struct wiimote_data *wdata)
2396{
2397 unsigned long flags;
2398
2399 spin_lock_irqsave(&wdata->state.lock, flags);
2400 wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
2401 spin_unlock_irqrestore(&wdata->state.lock, flags);
2402
2403 return 0;
2404}
2405
2406static void wiimod_builtin_mp_remove(const struct wiimod_ops *ops,
2407 struct wiimote_data *wdata)
2408{
2409 unsigned long flags;
2410
2411 spin_lock_irqsave(&wdata->state.lock, flags);
2412 wdata->state.flags |= WIIPROTO_FLAG_BUILTIN_MP;
2413 spin_unlock_irqrestore(&wdata->state.lock, flags);
2414}
2415
2416static const struct wiimod_ops wiimod_builtin_mp = {
2417 .flags = 0,
2418 .arg = 0,
2419 .probe = wiimod_builtin_mp_probe,
2420 .remove = wiimod_builtin_mp_remove,
2421};
2422
2423
2424
2425
2426
2427
2428
2429
2430static int wiimod_no_mp_probe(const struct wiimod_ops *ops,
2431 struct wiimote_data *wdata)
2432{
2433 unsigned long flags;
2434
2435 spin_lock_irqsave(&wdata->state.lock, flags);
2436 wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2437 spin_unlock_irqrestore(&wdata->state.lock, flags);
2438
2439 return 0;
2440}
2441
2442static void wiimod_no_mp_remove(const struct wiimod_ops *ops,
2443 struct wiimote_data *wdata)
2444{
2445 unsigned long flags;
2446
2447 spin_lock_irqsave(&wdata->state.lock, flags);
2448 wdata->state.flags |= WIIPROTO_FLAG_NO_MP;
2449 spin_unlock_irqrestore(&wdata->state.lock, flags);
2450}
2451
2452static const struct wiimod_ops wiimod_no_mp = {
2453 .flags = 0,
2454 .arg = 0,
2455 .probe = wiimod_no_mp_probe,
2456 .remove = wiimod_no_mp_remove,
2457};
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469static void wiimod_mp_in_mp(struct wiimote_data *wdata, const __u8 *ext)
2470{
2471 __s32 x, y, z;
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497 x = ext[0];
2498 y = ext[1];
2499 z = ext[2];
2500
2501 x |= (((__u16)ext[3]) << 6) & 0xff00;
2502 y |= (((__u16)ext[4]) << 6) & 0xff00;
2503 z |= (((__u16)ext[5]) << 6) & 0xff00;
2504
2505 x -= 8192;
2506 y -= 8192;
2507 z -= 8192;
2508
2509 if (!(ext[3] & 0x02))
2510 x = (x * 2000 * 9) / 440;
2511 else
2512 x *= 9;
2513 if (!(ext[4] & 0x02))
2514 y = (y * 2000 * 9) / 440;
2515 else
2516 y *= 9;
2517 if (!(ext[3] & 0x01))
2518 z = (z * 2000 * 9) / 440;
2519 else
2520 z *= 9;
2521
2522 input_report_abs(wdata->mp, ABS_RX, x);
2523 input_report_abs(wdata->mp, ABS_RY, y);
2524 input_report_abs(wdata->mp, ABS_RZ, z);
2525 input_sync(wdata->mp);
2526}
2527
2528static int wiimod_mp_open(struct input_dev *dev)
2529{
2530 struct wiimote_data *wdata = input_get_drvdata(dev);
2531 unsigned long flags;
2532
2533 spin_lock_irqsave(&wdata->state.lock, flags);
2534 wdata->state.flags |= WIIPROTO_FLAG_MP_USED;
2535 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2536 __wiimote_schedule(wdata);
2537 spin_unlock_irqrestore(&wdata->state.lock, flags);
2538
2539 return 0;
2540}
2541
2542static void wiimod_mp_close(struct input_dev *dev)
2543{
2544 struct wiimote_data *wdata = input_get_drvdata(dev);
2545 unsigned long flags;
2546
2547 spin_lock_irqsave(&wdata->state.lock, flags);
2548 wdata->state.flags &= ~WIIPROTO_FLAG_MP_USED;
2549 wiiproto_req_drm(wdata, WIIPROTO_REQ_NULL);
2550 __wiimote_schedule(wdata);
2551 spin_unlock_irqrestore(&wdata->state.lock, flags);
2552}
2553
2554static int wiimod_mp_probe(const struct wiimod_ops *ops,
2555 struct wiimote_data *wdata)
2556{
2557 int ret;
2558
2559 wdata->mp = input_allocate_device();
2560 if (!wdata->mp)
2561 return -ENOMEM;
2562
2563 input_set_drvdata(wdata->mp, wdata);
2564 wdata->mp->open = wiimod_mp_open;
2565 wdata->mp->close = wiimod_mp_close;
2566 wdata->mp->dev.parent = &wdata->hdev->dev;
2567 wdata->mp->id.bustype = wdata->hdev->bus;
2568 wdata->mp->id.vendor = wdata->hdev->vendor;
2569 wdata->mp->id.product = wdata->hdev->product;
2570 wdata->mp->id.version = wdata->hdev->version;
2571 wdata->mp->name = WIIMOTE_NAME " Motion Plus";
2572
2573 set_bit(EV_ABS, wdata->mp->evbit);
2574 set_bit(ABS_RX, wdata->mp->absbit);
2575 set_bit(ABS_RY, wdata->mp->absbit);
2576 set_bit(ABS_RZ, wdata->mp->absbit);
2577 input_set_abs_params(wdata->mp,
2578 ABS_RX, -16000, 16000, 4, 8);
2579 input_set_abs_params(wdata->mp,
2580 ABS_RY, -16000, 16000, 4, 8);
2581 input_set_abs_params(wdata->mp,
2582 ABS_RZ, -16000, 16000, 4, 8);
2583
2584 ret = input_register_device(wdata->mp);
2585 if (ret)
2586 goto err_free;
2587
2588 return 0;
2589
2590err_free:
2591 input_free_device(wdata->mp);
2592 wdata->mp = NULL;
2593 return ret;
2594}
2595
2596static void wiimod_mp_remove(const struct wiimod_ops *ops,
2597 struct wiimote_data *wdata)
2598{
2599 if (!wdata->mp)
2600 return;
2601
2602 input_unregister_device(wdata->mp);
2603 wdata->mp = NULL;
2604}
2605
2606const struct wiimod_ops wiimod_mp = {
2607 .flags = 0,
2608 .arg = 0,
2609 .probe = wiimod_mp_probe,
2610 .remove = wiimod_mp_remove,
2611 .in_mp = wiimod_mp_in_mp,
2612};
2613
2614
2615
2616static const struct wiimod_ops wiimod_dummy;
2617
2618const struct wiimod_ops *wiimod_table[WIIMOD_NUM] = {
2619 [WIIMOD_KEYS] = &wiimod_keys,
2620 [WIIMOD_RUMBLE] = &wiimod_rumble,
2621 [WIIMOD_BATTERY] = &wiimod_battery,
2622 [WIIMOD_LED1] = &wiimod_leds[0],
2623 [WIIMOD_LED2] = &wiimod_leds[1],
2624 [WIIMOD_LED3] = &wiimod_leds[2],
2625 [WIIMOD_LED4] = &wiimod_leds[3],
2626 [WIIMOD_ACCEL] = &wiimod_accel,
2627 [WIIMOD_IR] = &wiimod_ir,
2628 [WIIMOD_BUILTIN_MP] = &wiimod_builtin_mp,
2629 [WIIMOD_NO_MP] = &wiimod_no_mp,
2630};
2631
2632const struct wiimod_ops *wiimod_ext_table[WIIMOTE_EXT_NUM] = {
2633 [WIIMOTE_EXT_NONE] = &wiimod_dummy,
2634 [WIIMOTE_EXT_UNKNOWN] = &wiimod_dummy,
2635 [WIIMOTE_EXT_NUNCHUK] = &wiimod_nunchuk,
2636 [WIIMOTE_EXT_CLASSIC_CONTROLLER] = &wiimod_classic,
2637 [WIIMOTE_EXT_BALANCE_BOARD] = &wiimod_bboard,
2638 [WIIMOTE_EXT_PRO_CONTROLLER] = &wiimod_pro,
2639 [WIIMOTE_EXT_DRUMS] = &wiimod_drums,
2640 [WIIMOTE_EXT_GUITAR] = &wiimod_guitar,
2641};
2642