Skip to content

Commit 3f10e21

Browse files
committed
Merge tag 'hid-for-linus-2023121901' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid
Pull HID fixes from Jiri Kosina: - fix for division by zero in Nintendo driver when generic joycon is attached, reported and fixed by SteamOS folks (Guilherme G. Piccoli) - GCC-7 build fix (which is a good cleanup anyway) for Nintendo driver (Ryan McClelland) * tag 'hid-for-linus-2023121901' of git://git.kernel.org/pub/scm/linux/kernel/git/hid/hid: HID: nintendo: Prevent divide-by-zero on code HID: nintendo: fix initializer element is not constant error
2 parents 2cf4f94 + 6eb04ca commit 3f10e21

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

drivers/hid/hid-nintendo.c

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -325,28 +325,28 @@ struct joycon_imu_cal {
325325
* All the controller's button values are stored in a u32.
326326
* They can be accessed with bitwise ANDs.
327327
*/
328-
static const u32 JC_BTN_Y = BIT(0);
329-
static const u32 JC_BTN_X = BIT(1);
330-
static const u32 JC_BTN_B = BIT(2);
331-
static const u32 JC_BTN_A = BIT(3);
332-
static const u32 JC_BTN_SR_R = BIT(4);
333-
static const u32 JC_BTN_SL_R = BIT(5);
334-
static const u32 JC_BTN_R = BIT(6);
335-
static const u32 JC_BTN_ZR = BIT(7);
336-
static const u32 JC_BTN_MINUS = BIT(8);
337-
static const u32 JC_BTN_PLUS = BIT(9);
338-
static const u32 JC_BTN_RSTICK = BIT(10);
339-
static const u32 JC_BTN_LSTICK = BIT(11);
340-
static const u32 JC_BTN_HOME = BIT(12);
341-
static const u32 JC_BTN_CAP = BIT(13); /* capture button */
342-
static const u32 JC_BTN_DOWN = BIT(16);
343-
static const u32 JC_BTN_UP = BIT(17);
344-
static const u32 JC_BTN_RIGHT = BIT(18);
345-
static const u32 JC_BTN_LEFT = BIT(19);
346-
static const u32 JC_BTN_SR_L = BIT(20);
347-
static const u32 JC_BTN_SL_L = BIT(21);
348-
static const u32 JC_BTN_L = BIT(22);
349-
static const u32 JC_BTN_ZL = BIT(23);
328+
#define JC_BTN_Y BIT(0)
329+
#define JC_BTN_X BIT(1)
330+
#define JC_BTN_B BIT(2)
331+
#define JC_BTN_A BIT(3)
332+
#define JC_BTN_SR_R BIT(4)
333+
#define JC_BTN_SL_R BIT(5)
334+
#define JC_BTN_R BIT(6)
335+
#define JC_BTN_ZR BIT(7)
336+
#define JC_BTN_MINUS BIT(8)
337+
#define JC_BTN_PLUS BIT(9)
338+
#define JC_BTN_RSTICK BIT(10)
339+
#define JC_BTN_LSTICK BIT(11)
340+
#define JC_BTN_HOME BIT(12)
341+
#define JC_BTN_CAP BIT(13) /* capture button */
342+
#define JC_BTN_DOWN BIT(16)
343+
#define JC_BTN_UP BIT(17)
344+
#define JC_BTN_RIGHT BIT(18)
345+
#define JC_BTN_LEFT BIT(19)
346+
#define JC_BTN_SR_L BIT(20)
347+
#define JC_BTN_SL_L BIT(21)
348+
#define JC_BTN_L BIT(22)
349+
#define JC_BTN_ZL BIT(23)
350350

351351
enum joycon_msg_type {
352352
JOYCON_MSG_TYPE_NONE,
@@ -927,14 +927,27 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
927927
*/
928928
static void joycon_calc_imu_cal_divisors(struct joycon_ctlr *ctlr)
929929
{
930-
int i;
930+
int i, divz = 0;
931931

932932
for (i = 0; i < 3; i++) {
933933
ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] -
934934
ctlr->accel_cal.offset[i];
935935
ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] -
936936
ctlr->gyro_cal.offset[i];
937+
938+
if (ctlr->imu_cal_accel_divisor[i] == 0) {
939+
ctlr->imu_cal_accel_divisor[i] = 1;
940+
divz++;
941+
}
942+
943+
if (ctlr->imu_cal_gyro_divisor[i] == 0) {
944+
ctlr->imu_cal_gyro_divisor[i] = 1;
945+
divz++;
946+
}
937947
}
948+
949+
if (divz)
950+
hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz);
938951
}
939952

940953
static const s16 DFLT_ACCEL_OFFSET /*= 0*/;
@@ -1163,16 +1176,16 @@ static void joycon_parse_imu_report(struct joycon_ctlr *ctlr,
11631176
JC_IMU_SAMPLES_PER_DELTA_AVG) {
11641177
ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum /
11651178
ctlr->imu_delta_samples_count;
1166-
/* don't ever want divide by zero shenanigans */
1167-
if (ctlr->imu_avg_delta_ms == 0) {
1168-
ctlr->imu_avg_delta_ms = 1;
1169-
hid_warn(ctlr->hdev,
1170-
"calculated avg imu delta of 0\n");
1171-
}
11721179
ctlr->imu_delta_samples_count = 0;
11731180
ctlr->imu_delta_samples_sum = 0;
11741181
}
11751182

1183+
/* don't ever want divide by zero shenanigans */
1184+
if (ctlr->imu_avg_delta_ms == 0) {
1185+
ctlr->imu_avg_delta_ms = 1;
1186+
hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n");
1187+
}
1188+
11761189
/* useful for debugging IMU sample rate */
11771190
hid_dbg(ctlr->hdev,
11781191
"imu_report: ms=%u last_ms=%u delta=%u avg_delta=%u\n",

0 commit comments

Comments
 (0)