14 #include <linux/device.h>
15 #include <linux/hid.h>
16 #include <linux/input.h>
18 #include <linux/module.h>
40 static __u16 wiiproto_keymap[] = {
87 spin_unlock_irqrestore(&wdata->
qlock, flags);
88 wiimote_hid_send(wdata->
hdev, wdata->
outq[wdata->
tail].data,
95 spin_unlock_irqrestore(&wdata->
qlock, flags);
105 hid_warn(wdata->
hdev,
"Sending too large output report\n");
126 wdata->
head = newhead;
128 }
else if (newhead != wdata->
tail) {
129 wdata->
head = newhead;
134 spin_unlock_irqrestore(&wdata->
qlock, flags);
144 static inline void wiiproto_keep_rumble(
struct wiimote_data *wdata,
__u8 *cmd1)
166 wiiproto_keep_rumble(wdata, &cmd[1]);
167 wiimote_queue(wdata, cmd,
sizeof(cmd));
191 wiiproto_keep_rumble(wdata, &cmd[1]);
192 wiimote_queue(wdata, cmd,
sizeof(cmd));
234 drm = select_drm(wdata);
240 wdata->
state.drm = drm;
241 wiiproto_keep_rumble(wdata, &cmd[1]);
242 wiimote_queue(wdata, cmd,
sizeof(cmd));
245 static void wiiproto_req_status(
struct wiimote_data *wdata)
252 wiiproto_keep_rumble(wdata, &cmd[1]);
253 wiimote_queue(wdata, cmd,
sizeof(cmd));
277 wiiproto_keep_rumble(wdata, &cmd[1]);
278 wiimote_queue(wdata, cmd,
sizeof(cmd));
288 wiiproto_keep_rumble(wdata, &cmd[1]);
289 wiimote_queue(wdata, cmd,
sizeof(cmd));
292 #define wiiproto_req_wreg(wdata, os, buf, sz) \
293 wiiproto_req_wmem((wdata), false, (os), (buf), (sz))
295 #define wiiproto_req_weeprom(wdata, os, buf, sz) \
296 wiiproto_req_wmem((wdata), true, (os), (buf), (sz))
303 if (size > 16 || size == 0) {
304 hid_warn(wdata->
hdev,
"Invalid length %d wmem request\n", size);
308 memset(cmd, 0,
sizeof(cmd));
310 cmd[2] = (offset >> 16) & 0xff;
311 cmd[3] = (offset >> 8) & 0xff;
312 cmd[4] = offset & 0xff;
314 memcpy(&cmd[6], buf, size);
319 wiiproto_keep_rumble(wdata, &cmd[1]);
320 wiimote_queue(wdata, cmd,
sizeof(cmd));
329 hid_warn(wdata->
hdev,
"Invalid length %d rmem request\n", size);
335 cmd[2] = (offset >> 16) & 0xff;
336 cmd[3] = (offset >> 8) & 0xff;
337 cmd[4] = offset & 0xff;
338 cmd[5] = (size >> 8) & 0xff;
339 cmd[6] = size & 0xff;
344 wiiproto_keep_rumble(wdata, &cmd[1]);
345 wiimote_queue(wdata, cmd,
sizeof(cmd));
358 spin_unlock_irqrestore(&wdata->
state.lock, flags);
360 ret = wiimote_cmd_wait(wdata);
361 if (!ret && wdata->
state.cmd_err)
376 wdata->
state.cmd_read_buf = rmem;
379 spin_unlock_irqrestore(&wdata->
state.lock, flags);
381 ret = wiimote_cmd_wait(wdata);
385 spin_unlock_irqrestore(&wdata->
state.lock, flags);
388 if (wdata->
state.cmd_read_size == 0)
391 ret = wdata->
state.cmd_read_size;
397 static int wiimote_battery_get_property(
struct power_supply *psy,
411 ret = wiimote_cmd_acquire(wdata);
417 wiiproto_req_status(wdata);
418 spin_unlock_irqrestore(&wdata->
state.lock, flags);
420 ret = wiimote_cmd_wait(wdata);
422 wiimote_cmd_release(wdata);
444 static const __u8 data_enable[] = { 0x01 };
445 static const __u8 data_sens1[] = { 0x02, 0x00, 0x00, 0x71, 0x01,
446 0x00, 0xaa, 0x00, 0x64 };
447 static const __u8 data_sens2[] = { 0x63, 0x03 };
448 static const __u8 data_fin[] = { 0x08 };
453 spin_unlock_irqrestore(&wdata->
state.lock, flags);
459 wiiproto_req_ir1(wdata, 0);
460 wiiproto_req_ir2(wdata, 0);
462 spin_unlock_irqrestore(&wdata->
state.lock, flags);
466 spin_unlock_irqrestore(&wdata->
state.lock, flags);
468 ret = wiimote_cmd_acquire(wdata);
475 wiiproto_req_ir1(wdata, 0x06);
476 spin_unlock_irqrestore(&wdata->
state.lock, flags);
478 ret = wiimote_cmd_wait(wdata);
481 if (wdata->
state.cmd_err) {
489 wiiproto_req_ir2(wdata, 0x06);
490 spin_unlock_irqrestore(&wdata->
state.lock, flags);
492 ret = wiimote_cmd_wait(wdata);
495 if (wdata->
state.cmd_err) {
502 sizeof(data_enable));
544 spin_unlock_irqrestore(&wdata->
state.lock, flags);
547 wiimote_cmd_release(wdata);
561 for (i = 0; i < 4; ++
i) {
562 if (wdata->
leds[i] == led_dev) {
565 spin_unlock_irqrestore(&wdata->
state.lock, flags);
573 static void wiimote_leds_set(
struct led_classdev *led_dev,
577 struct device *dev = led_dev->
dev->parent;
584 for (i = 0; i < 4; ++
i) {
585 if (wdata->
leds[i] == led_dev) {
588 state = wdata->
state.flags;
590 wiiproto_req_leds(wdata, state & ~flag);
592 wiiproto_req_leds(wdata, state | flag);
593 spin_unlock_irqrestore(&wdata->
state.lock, flags);
599 static int wiimote_ff_play(
struct input_dev *dev,
void *
data,
612 if (eff->
u.
rumble.strong_magnitude || eff->
u.
rumble.weak_magnitude)
618 wiiproto_req_rumble(wdata, value);
619 spin_unlock_irqrestore(&wdata->
state.lock, flags);
624 static int wiimote_input_open(
struct input_dev *dev)
628 return hid_hw_open(wdata->
hdev);
631 static void wiimote_input_close(
struct input_dev *dev)
635 hid_hw_close(wdata->
hdev);
638 static int wiimote_accel_open(
struct input_dev *dev)
644 ret = hid_hw_open(wdata->
hdev);
649 wiiproto_req_accel(wdata,
true);
650 spin_unlock_irqrestore(&wdata->
state.lock, flags);
655 static void wiimote_accel_close(
struct input_dev *dev)
661 wiiproto_req_accel(wdata,
false);
662 spin_unlock_irqrestore(&wdata->
state.lock, flags);
664 hid_hw_close(wdata->
hdev);
667 static int wiimote_ir_open(
struct input_dev *dev)
672 ret = hid_hw_open(wdata->
hdev);
678 hid_hw_close(wdata->
hdev);
685 static void wiimote_ir_close(
struct input_dev *dev)
689 wiimote_init_ir(wdata, 0);
690 hid_hw_close(wdata->
hdev);
696 !!(payload[0] & 0x01));
698 !!(payload[0] & 0x02));
700 !!(payload[0] & 0x04));
702 !!(payload[0] & 0x08));
704 !!(payload[0] & 0x10));
706 !!(payload[1] & 0x01));
708 !!(payload[1] & 0x02));
710 !!(payload[1] & 0x04));
712 !!(payload[1] & 0x08));
714 !!(payload[1] & 0x10));
716 !!(payload[1] & 0x80));
717 input_sync(wdata->
input);
743 x |= (payload[0] >> 5) & 0x3;
744 y |= (payload[1] >> 4) & 0x2;
745 z |= (payload[1] >> 5) & 0x2;
750 input_sync(wdata->
accel);
753 #define ir_to_input0(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
754 ABS_HAT0X, ABS_HAT0Y)
755 #define ir_to_input1(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
756 ABS_HAT1X, ABS_HAT1Y)
757 #define ir_to_input2(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
758 ABS_HAT2X, ABS_HAT2Y)
759 #define ir_to_input3(wdata, ir, packed) __ir_to_input((wdata), (ir), (packed), \
760 ABS_HAT3X, ABS_HAT3Y)
781 x = ir[1] | ((ir[0] & 0x03) << 8);
782 y = ir[2] | ((ir[0] & 0x0c) << 6);
784 x = ir[0] | ((ir[2] & 0x30) << 4);
785 y = ir[1] | ((ir[2] & 0xc0) << 2);
788 input_report_abs(wdata->
ir, xid, x);
789 input_report_abs(wdata->
ir, yid, y);
794 handler_keys(wdata, payload);
802 wdata->
state.cmd_battery = payload[5];
803 wiimote_cmd_complete(wdata);
813 handler_keys(wdata, payload);
818 else if (size > wdata->
state.cmd_read_size)
819 size = wdata->
state.cmd_read_size;
822 if (wdata->
state.cmd_read_buf)
823 memcpy(wdata->
state.cmd_read_buf, &payload[5], size);
824 wiimote_cmd_complete(wdata);
830 __u8 err = payload[3];
831 __u8 cmd = payload[2];
833 handler_keys(wdata, payload);
835 if (wiimote_cmd_pending(wdata, cmd, 0)) {
837 wiimote_cmd_complete(wdata);
839 hid_warn(wdata->
hdev,
"Remote error %hhu on req %hhu\n", err,
846 handler_keys(wdata, payload);
847 handler_accel(wdata, payload);
852 handler_keys(wdata, payload);
856 static void handler_drm_KAI(
struct wiimote_data *wdata,
const __u8 *payload)
858 handler_keys(wdata, payload);
859 handler_accel(wdata, payload);
864 input_sync(wdata->
ir);
867 static void handler_drm_KEE(
struct wiimote_data *wdata,
const __u8 *payload)
869 handler_keys(wdata, payload);
873 static void handler_drm_KIE(
struct wiimote_data *wdata,
const __u8 *payload)
875 handler_keys(wdata, payload);
880 input_sync(wdata->
ir);
884 static void handler_drm_KAE(
struct wiimote_data *wdata,
const __u8 *payload)
886 handler_keys(wdata, payload);
887 handler_accel(wdata, payload);
891 static void handler_drm_KAIE(
struct wiimote_data *wdata,
const __u8 *payload)
893 handler_keys(wdata, payload);
894 handler_accel(wdata, payload);
899 input_sync(wdata->
ir);
908 static void handler_drm_SKAI1(
struct wiimote_data *wdata,
const __u8 *payload)
910 handler_keys(wdata, payload);
912 wdata->
state.accel_split[0] = payload[2];
913 wdata->
state.accel_split[1] = (payload[0] >> 1) & (0x10 | 0x20);
914 wdata->
state.accel_split[1] |= (payload[1] << 1) & (0x40 | 0x80);
918 input_sync(wdata->
ir);
921 static void handler_drm_SKAI2(
struct wiimote_data *wdata,
const __u8 *payload)
925 handler_keys(wdata, payload);
927 wdata->
state.accel_split[1] |= (payload[0] >> 5) & (0x01 | 0x02);
928 wdata->
state.accel_split[1] |= (payload[1] >> 3) & (0x04 | 0x08);
932 buf[2] = wdata->
state.accel_split[0];
934 buf[4] = wdata->
state.accel_split[1];
935 handler_accel(wdata, buf);
939 input_sync(wdata->
ir);
967 u8 *raw_data,
int size)
973 bool handled =
false;
980 for (i = 0; handlers[
i].
id; ++
i) {
982 if (h->
id == raw_data[0] && h->
size < size) {
983 h->
func(wdata, &raw_data[1]);
989 hid_warn(hdev,
"Unhandled report %hhu size %d\n", raw_data[0],
992 spin_unlock_irqrestore(&wdata->
state.lock, flags);
997 static void wiimote_leds_destroy(
struct wiimote_data *wdata)
1002 for (i = 0; i < 4; ++
i) {
1003 if (wdata->
leds[i]) {
1004 led = wdata->
leds[
i];
1012 static int wiimote_leds_create(
struct wiimote_data *wdata)
1016 size_t namesz =
strlen(dev_name(dev)) + 9;
1020 for (i = 0; i < 4; ++
i) {
1026 name = (
void*)&led[1];
1027 snprintf(name, namesz,
"%s:blue:p%d", dev_name(dev), i);
1045 wiimote_leds_destroy(wdata);
1058 wdata->
input = input_allocate_device();
1063 hid_set_drvdata(hdev, wdata);
1065 input_set_drvdata(wdata->
input, wdata);
1066 wdata->
input->open = wiimote_input_open;
1067 wdata->
input->close = wiimote_input_close;
1068 wdata->
input->dev.parent = &wdata->
hdev->dev;
1069 wdata->
input->id.bustype = wdata->
hdev->bus;
1070 wdata->
input->id.vendor = wdata->
hdev->vendor;
1071 wdata->
input->id.product = wdata->
hdev->product;
1072 wdata->
input->id.version = wdata->
hdev->version;
1083 wdata->
accel = input_allocate_device();
1087 input_set_drvdata(wdata->
accel, wdata);
1088 wdata->
accel->open = wiimote_accel_open;
1089 wdata->
accel->close = wiimote_accel_close;
1090 wdata->
accel->dev.parent = &wdata->
hdev->dev;
1091 wdata->
accel->id.bustype = wdata->
hdev->bus;
1092 wdata->
accel->id.vendor = wdata->
hdev->vendor;
1093 wdata->
accel->id.product = wdata->
hdev->product;
1094 wdata->
accel->id.version = wdata->
hdev->version;
1101 input_set_abs_params(wdata->
accel,
ABS_RX, -500, 500, 2, 4);
1102 input_set_abs_params(wdata->
accel,
ABS_RY, -500, 500, 2, 4);
1103 input_set_abs_params(wdata->
accel,
ABS_RZ, -500, 500, 2, 4);
1105 wdata->
ir = input_allocate_device();
1109 input_set_drvdata(wdata->
ir, wdata);
1110 wdata->
ir->open = wiimote_ir_open;
1111 wdata->
ir->close = wiimote_ir_close;
1112 wdata->
ir->dev.parent = &wdata->
hdev->dev;
1113 wdata->
ir->id.bustype = wdata->
hdev->bus;
1114 wdata->
ir->id.vendor = wdata->
hdev->vendor;
1115 wdata->
ir->id.product = wdata->
hdev->product;
1116 wdata->
ir->id.version = wdata->
hdev->version;
1128 input_set_abs_params(wdata->
ir,
ABS_HAT0X, 0, 1023, 2, 4);
1129 input_set_abs_params(wdata->
ir,
ABS_HAT0Y, 0, 767, 2, 4);
1130 input_set_abs_params(wdata->
ir,
ABS_HAT1X, 0, 1023, 2, 4);
1131 input_set_abs_params(wdata->
ir,
ABS_HAT1Y, 0, 767, 2, 4);
1132 input_set_abs_params(wdata->
ir,
ABS_HAT2X, 0, 1023, 2, 4);
1133 input_set_abs_params(wdata->
ir,
ABS_HAT2Y, 0, 767, 2, 4);
1134 input_set_abs_params(wdata->
ir,
ABS_HAT3X, 0, 1023, 2, 4);
1135 input_set_abs_params(wdata->
ir,
ABS_HAT3Y, 0, 767, 2, 4);
1141 init_completion(&wdata->
state.ready);
1148 input_free_device(wdata->
accel);
1150 input_free_device(wdata->
input);
1156 static void wiimote_destroy(
struct wiimote_data *wdata)
1160 wiimote_leds_destroy(wdata);
1163 input_unregister_device(wdata->
accel);
1164 input_unregister_device(wdata->
ir);
1165 input_unregister_device(wdata->
input);
1167 hid_hw_stop(wdata->
hdev);
1172 static int wiimote_hid_probe(
struct hid_device *hdev,
1180 wdata = wiimote_create(hdev);
1182 hid_err(hdev,
"Can't alloc device\n");
1186 ret = hid_parse(hdev);
1188 hid_err(hdev,
"HID parse failed\n");
1194 hid_err(hdev,
"HW start failed\n");
1198 ret = input_register_device(wdata->
accel);
1200 hid_err(hdev,
"Cannot register input device\n");
1204 ret = input_register_device(wdata->
ir);
1206 hid_err(hdev,
"Cannot register input device\n");
1210 ret = input_register_device(wdata->
input);
1212 hid_err(hdev,
"Cannot register input device\n");
1216 wdata->
battery.properties = wiimote_battery_props;
1218 wdata->
battery.get_property = wiimote_battery_get_property;
1219 wdata->
battery.name =
"wiimote_battery";
1221 wdata->
battery.use_for_apm = 0;
1225 hid_err(hdev,
"Cannot register battery device\n");
1231 ret = wiimote_leds_create(wdata);
1243 hid_info(hdev,
"New device registered\n");
1246 spin_lock_irq(&wdata->
state.lock);
1247 wiiproto_req_leds(wdata, WIIPROTO_FLAG_LED1);
1248 spin_unlock_irq(&wdata->
state.lock);
1253 wiimote_destroy(wdata);
1257 input_unregister_device(wdata->
input);
1260 input_unregister_device(wdata->
ir);
1263 input_unregister_device(wdata->
accel);
1268 input_free_device(wdata->
ir);
1269 input_free_device(wdata->
accel);
1270 input_free_device(wdata->
input);
1275 static void wiimote_hid_remove(
struct hid_device *hdev)
1279 hid_info(hdev,
"Device removed\n");
1280 wiimote_destroy(wdata);
1290 static struct hid_driver wiimote_hid_driver = {
1292 .id_table = wiimote_hid_devices,
1293 .probe = wiimote_hid_probe,
1294 .remove = wiimote_hid_remove,
1295 .raw_event = wiimote_hid_event,
1298 static int __init wiimote_init(
void)
1304 pr_err(
"Can't register wiimote hid driver\n");
1309 static void __exit wiimote_exit(
void)