14 #include <linux/module.h>
62 static __u16 wiiext_keymap[] = {
88 if (!wiimote_cmd_acquire(ext->
wdata)) {
90 wiimote_cmd_release(ext->
wdata);
97 spin_unlock_irqrestore(&ext->
wdata->state.lock, flags);
109 if (wiimote_cmd_acquire(ext->
wdata))
120 if (ret == 2 || rmem[1] == 0x5)
124 wiimote_cmd_release(ext->
wdata);
138 if (wiimote_cmd_acquire(ext->
wdata))
153 if (rmem[0] == 0 && rmem[1] == 0)
155 else if (rmem[0] == 0x01 && rmem[1] == 0x01)
157 else if (rmem[0] == 0x04 && rmem[1] == 0x02)
170 for (i = 0; i < 3; i++) {
171 for (j = 0; j < 4; j++) {
174 ext->
calib[
j][
i] |= buf[offs + 1];
181 wiimote_cmd_release(ext->
wdata);
186 static void ext_enable(
struct wiimote_ext *ext,
bool motionp,
__u8 ext_type)
193 if (wiimote_cmd_acquire(ext->
wdata))
204 wiimote_cmd_release(ext->
wdata);
213 spin_unlock_irqrestore(&ext->
wdata->state.lock, flags);
224 motionp = motionp_read(ext);
225 ext_type = ext_read(ext);
226 ext_enable(ext, motionp, ext_type);
230 static void wiiext_schedule(
struct wiimote_ext *ext)
248 if (wdata->
ext->plugged == plugged)
254 wdata->
ext->mp_plugged =
false;
275 return wdata->
ext->motionp || wdata->
ext->ext_type;
308 x |= (((
__u16)payload[3]) << 6) & 0xff00;
309 y |= (((
__u16)payload[4]) << 6) & 0xff00;
310 z |= (((
__u16)payload[5]) << 6) & 0xff00;
316 if (!(payload[3] & 0x02))
320 if (!(payload[4] & 0x02))
324 if (!(payload[3] & 0x01))
334 plugged = payload[5] & 0x01;
339 static void handler_nunchuck(
struct wiimote_ext *ext,
const __u8 *payload)
383 x |= (payload[5] >> 3) & 0x02;
384 y |= (payload[5] >> 4) & 0x02;
386 z |= (payload[5] >> 5) & 0x06;
388 x |= (payload[5] >> 2) & 0x03;
389 y |= (payload[5] >> 4) & 0x03;
390 z |= (payload[5] >> 6) & 0x03;
405 input_report_key(ext->
input,
407 input_report_key(ext->
input,
410 input_report_key(ext->
input,
412 input_report_key(ext->
input,
416 input_sync(ext->
input);
419 static void handler_classic(
struct wiimote_ext *ext,
const __u8 *payload)
467 lx = payload[0] & 0x3e;
468 ly = payload[0] & 0x3e;
470 lx = payload[0] & 0x3f;
471 ly = payload[0] & 0x3f;
474 rx = (payload[0] >> 3) & 0x14;
475 rx |= (payload[1] >> 5) & 0x06;
476 rx |= (payload[2] >> 7) & 0x01;
477 ry = payload[2] & 0x1f;
479 rt = payload[3] & 0x1f;
480 lt = (payload[2] >> 2) & 0x18;
481 lt |= (payload[3] >> 5) & 0x07;
496 !!(payload[4] & 0x80));
498 !!(payload[4] & 0x40));
500 !!(payload[4] & 0x20));
502 !!(payload[4] & 0x10));
504 !!(payload[4] & 0x08));
506 !!(payload[4] & 0x04));
508 !!(payload[4] & 0x02));
510 !!(payload[5] & 0x80));
512 !!(payload[5] & 0x40));
514 !!(payload[5] & 0x20));
516 !!(payload[5] & 0x10));
518 !!(payload[5] & 0x08));
520 !!(payload[5] & 0x04));
524 !!(payload[0] & 0x01));
526 !!(payload[1] & 0x01));
529 !!(payload[5] & 0x01));
531 !!(payload[5] & 0x02));
534 input_sync(ext->
input);
537 static void handler_balance_board(
struct wiimote_ext *ext,
const __u8 *payload)
565 val[0] |= payload[1];
569 val[1] |= payload[3];
573 val[2] |= payload[5];
577 val[3] |= payload[7];
580 for (i = 0; i < 4; i++) {
581 if (val[i] < ext->
calib[i][1]) {
582 tmp = val[
i] - ext->
calib[
i][0];
586 tmp = val[
i] - ext->
calib[
i][1];
599 input_sync(ext->
input);
610 if (ext->
motionp && (payload[5] & 0x02)) {
611 handler_motionp(ext, payload);
613 handler_nunchuck(ext, payload);
615 handler_classic(ext, payload);
617 handler_balance_board(ext, payload);
626 bool motionp =
false;
631 motionp = wdata->
ext->motionp;
632 type = wdata->
ext->ext_type;
634 spin_unlock_irqrestore(&wdata->
state.lock, flags);
638 return sprintf(buf,
"motionp+nunchuck\n");
640 return sprintf(buf,
"nunchuck\n");
643 return sprintf(buf,
"motionp+classic\n");
645 return sprintf(buf,
"classic\n");
648 return sprintf(buf,
"motionp+balanceboard\n");
650 return sprintf(buf,
"balanceboard\n");
653 return sprintf(buf,
"motionp\n");
661 static int wiiext_input_open(
struct input_dev *dev)
666 ret = hid_hw_open(ext->
wdata->hdev);
671 wiiext_schedule(ext);
676 static void wiiext_input_close(
struct input_dev *dev)
681 wiiext_schedule(ext);
682 hid_hw_close(ext->
wdata->hdev);
685 static int wiiext_mp_open(
struct input_dev *dev)
690 ret = hid_hw_open(ext->
wdata->hdev);
695 wiiext_schedule(ext);
700 static void wiiext_mp_close(
struct input_dev *dev)
705 wiiext_schedule(ext);
706 hid_hw_close(ext->
wdata->hdev);
723 ext->
input = input_allocate_device();
729 input_set_drvdata(ext->
input, ext);
730 ext->
input->open = wiiext_input_open;
731 ext->
input->close = wiiext_input_close;
732 ext->
input->dev.parent = &wdata->
hdev->dev;
733 ext->
input->id.bustype = wdata->
hdev->bus;
734 ext->
input->id.vendor = wdata->
hdev->vendor;
735 ext->
input->id.product = wdata->
hdev->product;
736 ext->
input->id.version = wdata->
hdev->version;
763 input_set_abs_params(ext->
input,
ABS_RX, -500, 500, 2, 4);
764 input_set_abs_params(ext->
input,
ABS_RY, -500, 500, 2, 4);
765 input_set_abs_params(ext->
input,
ABS_RZ, -500, 500, 2, 4);
767 ret = input_register_device(ext->
input);
769 input_free_device(ext->
input);
773 ext->
mp_input = input_allocate_device();
779 input_set_drvdata(ext->
mp_input, ext);
780 ext->
mp_input->open = wiiext_mp_open;
781 ext->
mp_input->close = wiiext_mp_close;
793 input_set_abs_params(ext->
mp_input,
ABS_RX, -160000, 160000, 4, 8);
794 input_set_abs_params(ext->
mp_input,
ABS_RY, -160000, 160000, 4, 8);
795 input_set_abs_params(ext->
mp_input,
ABS_RZ, -160000, 160000, 4, 8);
797 ret = input_register_device(ext->
mp_input);
809 spin_unlock_irqrestore(&wdata->
state.lock, flags);
814 input_unregister_device(ext->
mp_input);
816 input_unregister_device(ext->
input);
841 spin_unlock_irqrestore(&wdata->
state.lock, flags);
844 input_unregister_device(ext->
mp_input);
845 input_unregister_device(ext->
input);