Skip to content

Commit

Permalink
[ADAPTER] Fix config corruption
Browse files Browse the repository at this point in the history
Loop over axes related items where not bound properly.
  • Loading branch information
darthcloud committed Aug 4, 2020
1 parent 5e65e68 commit 3b28df7
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 206 deletions.
5 changes: 3 additions & 2 deletions main/adapter/adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#define BT_MAX_DEV 7 /* BT limitation */
#define WIRED_MAX_DEV 12 /* Saturn limit */
#define ADAPTER_MAX_AXES 6
#define REPORT_MAX_USAGE 16

/* BT device ID */
Expand Down Expand Up @@ -305,7 +306,7 @@ struct generic_ctrl {
const uint32_t *desc;
uint32_t map_mask[4];
struct ctrl btns[4];
struct ctrl axes[6];
struct ctrl axes[ADAPTER_MAX_AXES];
};

struct generic_fb {
Expand Down Expand Up @@ -351,7 +352,7 @@ struct bt_data {
int32_t report_type;
struct hid_report reports[REPORT_MAX];
uint8_t input[128];
int32_t axes_cal[6];
int32_t axes_cal[ADAPTER_MAX_AXES];
uint32_t sdp_len;
uint8_t sdp_data[2048];
} __packed;
Expand Down
10 changes: 5 additions & 5 deletions main/adapter/dc.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ enum {
DC_LD_RIGHT,
};

const uint8_t dc_axes_idx[6] =
const uint8_t dc_axes_idx[ADAPTER_MAX_AXES] =
{
/* AXIS_LX, AXIS_LY, AXIS_RX, AXIS_RY, TRIG_L, TRIG_R */
7, 6, 5, 4, 0, 1
Expand All @@ -33,7 +33,7 @@ const struct ctrl_meta dc_btns_meta =
.polarity = 1,
};

const struct ctrl_meta dc_axes_meta[6] =
const struct ctrl_meta dc_axes_meta[ADAPTER_MAX_AXES] =
{
{.size_min = -128, .size_max = 127, .neutral = 0x80, .abs_max = 0x80},
{.size_min = -128, .size_max = 127, .neutral = 0x80, .abs_max = 0x80, .polarity = 1},
Expand Down Expand Up @@ -71,7 +71,7 @@ void dc_init_buffer(int32_t dev_mode, struct wired_data *wired_data) {
struct dc_map *map = (struct dc_map *)wired_data->output;

map->buttons = 0xFFFF;
for (uint32_t i = 0; i < ARRAY_SIZE(dc_axes_meta); i++) {
for (uint32_t i = 0; i < ADAPTER_MAX_AXES; i++) {
map->axes[dc_axes_idx[i]] = dc_axes_meta[i].neutral;
}
}
Expand All @@ -80,7 +80,7 @@ void dc_meta_init(int32_t dev_mode, struct generic_ctrl *ctrl_data) {
memset((void *)ctrl_data, 0, sizeof(*ctrl_data)*4);

for (uint32_t i = 0; i < WIRED_MAX_DEV; i++) {
for (uint32_t j = 0; j < ARRAY_SIZE(dc_axes_meta); j++) {
for (uint32_t j = 0; j < ADAPTER_MAX_AXES; j++) {
ctrl_data[i].mask = dc_mask;
ctrl_data[i].desc = dc_desc;
ctrl_data[i].axes[j].meta = &dc_axes_meta[j];
Expand All @@ -104,7 +104,7 @@ void dc_from_generic(int32_t dev_mode, struct generic_ctrl *ctrl_data, struct wi
}
}

for (uint32_t i = 0; i < ARRAY_SIZE(dc_axes_meta); i++) {
for (uint32_t i = 0; i < ADAPTER_MAX_AXES; i++) {
if (ctrl_data->map_mask[0] & (axis_to_btn_mask(i) & dc_desc[0])) {
if (ctrl_data->axes[i].value > ctrl_data->axes[i].meta->size_max) {
map_tmp.axes[dc_axes_idx[i]] = 255;
Expand Down
10 changes: 5 additions & 5 deletions main/adapter/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum {
GC_L,
};

const uint8_t gc_axes_idx[6] =
const uint8_t gc_axes_idx[ADAPTER_MAX_AXES] =
{
/* AXIS_LX, AXIS_LY, AXIS_RX, AXIS_RY, TRIG_L, TRIG_R */
0, 1, 2, 3, 4, 5
Expand All @@ -29,7 +29,7 @@ const struct ctrl_meta gc_btns_meta =
.polarity = 0,
};

const struct ctrl_meta gc_axes_meta[6] =
const struct ctrl_meta gc_axes_meta[ADAPTER_MAX_AXES] =
{
{.size_min = -128, .size_max = 127, .neutral = 0x80, .abs_max = 0x64},
{.size_min = -128, .size_max = 127, .neutral = 0x80, .abs_max = 0x64},
Expand Down Expand Up @@ -62,7 +62,7 @@ void gc_init_buffer(int32_t dev_mode, struct wired_data *wired_data) {
struct gc_map *map = (struct gc_map *)wired_data->output;

map->buttons = 0x8020;
for (uint32_t i = 0; i < ARRAY_SIZE(gc_axes_meta); i++) {
for (uint32_t i = 0; i < ADAPTER_MAX_AXES; i++) {
map->axes[gc_axes_idx[i]] = gc_axes_meta[i].neutral;
}
}
Expand All @@ -71,7 +71,7 @@ void gc_meta_init(int32_t dev_mode, struct generic_ctrl *ctrl_data) {
memset((void *)ctrl_data, 0, sizeof(*ctrl_data)*4);

for (uint32_t i = 0; i < WIRED_MAX_DEV; i++) {
for (uint32_t j = 0; j < ARRAY_SIZE(gc_axes_meta); j++) {
for (uint32_t j = 0; j < ADAPTER_MAX_AXES; j++) {
ctrl_data[i].mask = gc_mask;
ctrl_data[i].desc = gc_desc;
ctrl_data[i].axes[j].meta = &gc_axes_meta[j];
Expand All @@ -97,7 +97,7 @@ void gc_from_generic(int32_t dev_mode, struct generic_ctrl *ctrl_data, struct wi
}
}

for (uint32_t i = 0; i < ARRAY_SIZE(gc_axes_meta); i++) {
for (uint32_t i = 0; i < ADAPTER_MAX_AXES; i++) {
if (ctrl_data->map_mask[0] & (axis_to_btn_mask(i) & gc_desc[0])) {
if (ctrl_data->axes[i].value > ctrl_data->axes[i].meta->size_max) {
map_tmp.axes[gc_axes_idx[i]] = 127;
Expand Down
10 changes: 5 additions & 5 deletions main/adapter/hid_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

struct hid_report_meta {
int8_t hid_btn_idx;
int8_t hid_axes_idx[6];
int8_t hid_axes_idx[ADAPTER_MAX_AXES];
int8_t hid_hat_idx;
struct ctrl_meta hid_axes_meta[6];
struct ctrl_meta hid_axes_meta[ADAPTER_MAX_AXES];
uint32_t hid_mask[4];
uint32_t hid_desc[4];
uint32_t hid_btns_mask[32];
Expand Down Expand Up @@ -211,13 +211,13 @@ static void hid_mouse_to_generic(struct bt_data *bt_data, struct generic_ctrl *c
}

if (!atomic_test_bit(&bt_data->flags, BT_INIT)) {
for (uint32_t i = 0; i < sizeof(meta->hid_axes_idx); i++) {
for (uint32_t i = 0; i < ADAPTER_MAX_AXES; i++) {
bt_data->axes_cal[i] = meta->hid_axes_meta[i].neutral;
}
atomic_set_bit(&bt_data->flags, BT_INIT);
}

for (uint32_t i = 0; i < sizeof(meta->hid_axes_idx); i++) {
for (uint32_t i = 0; i < ADAPTER_MAX_AXES; i++) {
if (meta->hid_axes_idx[i] > -1) {
int32_t len = bt_data->reports[MOUSE].usages[meta->hid_axes_idx[i]].bit_size;
uint32_t offset = bt_data->reports[MOUSE].usages[meta->hid_axes_idx[i]].bit_offset;
Expand Down Expand Up @@ -426,7 +426,7 @@ static void hid_pad_to_generic(struct bt_data *bt_data, struct generic_ctrl *ctr
ctrl_data->btns[0].value |= hat_to_ld_btns[(hat - min) & 0xF];
}

for (uint32_t i = 0; i < sizeof(meta->hid_axes_idx); i++) {
for (uint32_t i = 0; i < ADAPTER_MAX_AXES; i++) {
if (meta->hid_axes_idx[i] > -1) {
int32_t len = bt_data->reports[PAD].usages[meta->hid_axes_idx[i]].bit_size;
uint32_t offset = bt_data->reports[PAD].usages[meta->hid_axes_idx[i]].bit_offset;
Expand Down
148 changes: 0 additions & 148 deletions main/adapter/io.h

This file was deleted.

13 changes: 7 additions & 6 deletions main/adapter/n64.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../util.h"
#include "n64.h"

#define N64_AXES_MAX 2
enum {
N64_LD_RIGHT,
N64_LD_LEFT,
Expand All @@ -20,7 +21,7 @@ enum {
N64_L,
};

const uint8_t n64_axes_idx[2] =
const uint8_t n64_axes_idx[N64_AXES_MAX] =
{
/* AXIS_LX, AXIS_LY */
0, 1
Expand All @@ -31,7 +32,7 @@ const struct ctrl_meta n64_btns_meta =
.polarity = 0,
};

const struct ctrl_meta n64_axes_meta[2] =
const struct ctrl_meta n64_axes_meta[N64_AXES_MAX] =
{
{.size_min = -128, .size_max = 127, .neutral = 0x00, .abs_max = 0x54},
{.size_min = -128, .size_max = 127, .neutral = 0x00, .abs_max = 0x54},
Expand Down Expand Up @@ -72,7 +73,7 @@ void n64_init_buffer(int32_t dev_mode, struct wired_data *wired_data) {
struct n64_map *map = (struct n64_map *)wired_data->output;

map->buttons = 0x0000;
for (uint32_t i = 0; i < ARRAY_SIZE(n64_axes_meta); i++) {
for (uint32_t i = 0; i < N64_AXES_MAX; i++) {
map->axes[n64_axes_idx[i]] = n64_axes_meta[i].neutral;
}
}
Expand All @@ -81,7 +82,7 @@ void n64_meta_init(int32_t dev_mode, struct generic_ctrl *ctrl_data) {
memset((void *)ctrl_data, 0, sizeof(*ctrl_data)*4);

for (uint32_t i = 0; i < WIRED_MAX_DEV; i++) {
for (uint32_t j = 0; j < ARRAY_SIZE(n64_axes_meta); j++) {
for (uint32_t j = 0; j < N64_AXES_MAX; j++) {
switch (dev_mode) {
case DEV_MOUSE:
ctrl_data[i].mask = n64_mouse_mask;
Expand Down Expand Up @@ -117,7 +118,7 @@ static void n64_ctrl_from_generic(struct generic_ctrl *ctrl_data, struct wired_d
}
}

for (uint32_t i = 0; i < ARRAY_SIZE(n64_axes_meta); i++) {
for (uint32_t i = 0; i < N64_AXES_MAX; i++) {
if (ctrl_data->map_mask[0] & (axis_to_btn_mask(i) & n64_desc[0])) {
if (ctrl_data->axes[i].value > ctrl_data->axes[i].meta->size_max) {
map_tmp.axes[n64_axes_idx[i]] = 127;
Expand Down Expand Up @@ -152,7 +153,7 @@ static void n64_mouse_from_generic(struct generic_ctrl *ctrl_data, struct wired_
}
}

for (uint32_t i = 0; i < ARRAY_SIZE(n64_axes_meta); i++) {
for (uint32_t i = 0; i < N64_AXES_MAX; i++) {
if (ctrl_data->map_mask[0] & (axis_to_btn_mask(i + 2) & n64_mouse_desc[0])) {
if (ctrl_data->axes[i + 2].value > ctrl_data->axes[i + 2].meta->size_max) {
map_tmp.axes[n64_axes_idx[i]] = 127;
Expand Down
Loading

0 comments on commit 3b28df7

Please sign in to comment.