Commit 3f10e214 authored by Linus Torvalds's avatar Linus Torvalds
Browse files

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
parents 2cf4f94d 6eb04ca8
Loading
Loading
Loading
Loading
+42 −29
Original line number Diff line number Diff line
@@ -325,28 +325,28 @@ struct joycon_imu_cal {
 * All the controller's button values are stored in a u32.
 * They can be accessed with bitwise ANDs.
 */
static const u32 JC_BTN_Y	= BIT(0);
static const u32 JC_BTN_X	= BIT(1);
static const u32 JC_BTN_B	= BIT(2);
static const u32 JC_BTN_A	= BIT(3);
static const u32 JC_BTN_SR_R	= BIT(4);
static const u32 JC_BTN_SL_R	= BIT(5);
static const u32 JC_BTN_R	= BIT(6);
static const u32 JC_BTN_ZR	= BIT(7);
static const u32 JC_BTN_MINUS	= BIT(8);
static const u32 JC_BTN_PLUS	= BIT(9);
static const u32 JC_BTN_RSTICK	= BIT(10);
static const u32 JC_BTN_LSTICK	= BIT(11);
static const u32 JC_BTN_HOME	= BIT(12);
static const u32 JC_BTN_CAP	= BIT(13); /* capture button */
static const u32 JC_BTN_DOWN	= BIT(16);
static const u32 JC_BTN_UP	= BIT(17);
static const u32 JC_BTN_RIGHT	= BIT(18);
static const u32 JC_BTN_LEFT	= BIT(19);
static const u32 JC_BTN_SR_L	= BIT(20);
static const u32 JC_BTN_SL_L	= BIT(21);
static const u32 JC_BTN_L	= BIT(22);
static const u32 JC_BTN_ZL	= BIT(23);
#define JC_BTN_Y	 BIT(0)
#define JC_BTN_X	 BIT(1)
#define JC_BTN_B	 BIT(2)
#define JC_BTN_A	 BIT(3)
#define JC_BTN_SR_R	 BIT(4)
#define JC_BTN_SL_R	 BIT(5)
#define JC_BTN_R	 BIT(6)
#define JC_BTN_ZR	 BIT(7)
#define JC_BTN_MINUS	 BIT(8)
#define JC_BTN_PLUS	 BIT(9)
#define JC_BTN_RSTICK	 BIT(10)
#define JC_BTN_LSTICK	 BIT(11)
#define JC_BTN_HOME	 BIT(12)
#define JC_BTN_CAP	 BIT(13) /* capture button */
#define JC_BTN_DOWN	 BIT(16)
#define JC_BTN_UP	 BIT(17)
#define JC_BTN_RIGHT	 BIT(18)
#define JC_BTN_LEFT	 BIT(19)
#define JC_BTN_SR_L	 BIT(20)
#define JC_BTN_SL_L	 BIT(21)
#define JC_BTN_L	 BIT(22)
#define JC_BTN_ZL	 BIT(23)

enum joycon_msg_type {
	JOYCON_MSG_TYPE_NONE,
@@ -927,16 +927,29 @@ static int joycon_request_calibration(struct joycon_ctlr *ctlr)
 */
static void joycon_calc_imu_cal_divisors(struct joycon_ctlr *ctlr)
{
	int i;
	int i, divz = 0;

	for (i = 0; i < 3; i++) {
		ctlr->imu_cal_accel_divisor[i] = ctlr->accel_cal.scale[i] -
						ctlr->accel_cal.offset[i];
		ctlr->imu_cal_gyro_divisor[i] = ctlr->gyro_cal.scale[i] -
						ctlr->gyro_cal.offset[i];

		if (ctlr->imu_cal_accel_divisor[i] == 0) {
			ctlr->imu_cal_accel_divisor[i] = 1;
			divz++;
		}

		if (ctlr->imu_cal_gyro_divisor[i] == 0) {
			ctlr->imu_cal_gyro_divisor[i] = 1;
			divz++;
		}
	}

	if (divz)
		hid_warn(ctlr->hdev, "inaccurate IMU divisors (%d)\n", divz);
}

static const s16 DFLT_ACCEL_OFFSET /*= 0*/;
static const s16 DFLT_ACCEL_SCALE = 16384;
static const s16 DFLT_GYRO_OFFSET /*= 0*/;
@@ -1163,14 +1176,14 @@ static void joycon_parse_imu_report(struct joycon_ctlr *ctlr,
		    JC_IMU_SAMPLES_PER_DELTA_AVG) {
			ctlr->imu_avg_delta_ms = ctlr->imu_delta_samples_sum /
						 ctlr->imu_delta_samples_count;
			ctlr->imu_delta_samples_count = 0;
			ctlr->imu_delta_samples_sum = 0;
		}

		/* don't ever want divide by zero shenanigans */
		if (ctlr->imu_avg_delta_ms == 0) {
			ctlr->imu_avg_delta_ms = 1;
				hid_warn(ctlr->hdev,
					 "calculated avg imu delta of 0\n");
			}
			ctlr->imu_delta_samples_count = 0;
			ctlr->imu_delta_samples_sum = 0;
			hid_warn(ctlr->hdev, "calculated avg imu delta of 0\n");
		}

		/* useful for debugging IMU sample rate */