···13781378 char name, const unsigned long *bm,13791379 unsigned int min_bit, unsigned int max_bit)13801380{13811381- int len = 0, i;13811381+ int bit = min_bit;13821382+ int len = 0;1382138313831384 len += snprintf(buf, max(size, 0), "%c", name);13841384- for (i = min_bit; i < max_bit; i++)13851385- if (bm[BIT_WORD(i)] & BIT_MASK(i))13861386- len += snprintf(buf + len, max(size - len, 0), "%X,", i);13851385+ for_each_set_bit_from(bit, bm, max_bit)13861386+ len += snprintf(buf + len, max(size - len, 0), "%X,", bit);13871387 return len;13881388}1389138913901390-static int input_print_modalias(char *buf, int size, const struct input_dev *id,13911391- int add_cr)13901390+static int input_print_modalias_parts(char *buf, int size, int full_len,13911391+ const struct input_dev *id)13921392{13931393- int len;13931393+ int len, klen, remainder, space;1394139413951395 len = snprintf(buf, max(size, 0),13961396 "input:b%04Xv%04Xp%04Xe%04X-",···1399139914001400 len += input_print_modalias_bits(buf + len, size - len,14011401 'e', id->evbit, 0, EV_MAX);14021402- len += input_print_modalias_bits(buf + len, size - len,14021402+14031403+ /*14041404+ * Calculate the remaining space in the buffer making sure we14051405+ * have place for the terminating 0.14061406+ */14071407+ space = max(size - (len + 1), 0);14081408+14091409+ klen = input_print_modalias_bits(buf + len, size - len,14031410 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);14111411+ len += klen;14121412+14131413+ /*14141414+ * If we have more data than we can fit in the buffer, check14151415+ * if we can trim key data to fit in the rest. We will indicate14161416+ * that key data is incomplete by adding "+" sign at the end, like14171417+ * this: * "k1,2,3,45,+,".14181418+ *14191419+ * Note that we shortest key info (if present) is "k+," so we14201420+ * can only try to trim if key data is longer than that.14211421+ */14221422+ if (full_len && size < full_len + 1 && klen > 3) {14231423+ remainder = full_len - len;14241424+ /*14251425+ * We can only trim if we have space for the remainder14261426+ * and also for at least "k+," which is 3 more characters.14271427+ */14281428+ if (remainder <= space - 3) {14291429+ /*14301430+ * We are guaranteed to have 'k' in the buffer, so14311431+ * we need at least 3 additional bytes for storing14321432+ * "+," in addition to the remainder.14331433+ */14341434+ for (int i = size - 1 - remainder - 3; i >= 0; i--) {14351435+ if (buf[i] == 'k' || buf[i] == ',') {14361436+ strcpy(buf + i + 1, "+,");14371437+ len = i + 3; /* Not counting '\0' */14381438+ break;14391439+ }14401440+ }14411441+ }14421442+ }14431443+14041444 len += input_print_modalias_bits(buf + len, size - len,14051445 'r', id->relbit, 0, REL_MAX);14061446 len += input_print_modalias_bits(buf + len, size - len,···14561416 len += input_print_modalias_bits(buf + len, size - len,14571417 'w', id->swbit, 0, SW_MAX);1458141814591459- if (add_cr)14601460- len += snprintf(buf + len, max(size - len, 0), "\n");14611461-14621419 return len;14201420+}14211421+14221422+static int input_print_modalias(char *buf, int size, const struct input_dev *id)14231423+{14241424+ int full_len;14251425+14261426+ /*14271427+ * Printing is done in 2 passes: first one figures out total length14281428+ * needed for the modalias string, second one will try to trim key14291429+ * data in case when buffer is too small for the entire modalias.14301430+ * If the buffer is too small regardless, it will fill as much as it14311431+ * can (without trimming key data) into the buffer and leave it to14321432+ * the caller to figure out what to do with the result.14331433+ */14341434+ full_len = input_print_modalias_parts(NULL, 0, 0, id);14351435+ return input_print_modalias_parts(buf, size, full_len, id);14631436}1464143714651438static ssize_t input_dev_show_modalias(struct device *dev,···14821429 struct input_dev *id = to_input_dev(dev);14831430 ssize_t len;1484143114851485- len = input_print_modalias(buf, PAGE_SIZE, id, 1);14321432+ len = input_print_modalias(buf, PAGE_SIZE, id);14331433+ if (len < PAGE_SIZE - 2)14341434+ len += snprintf(buf + len, PAGE_SIZE - len, "\n");1486143514871436 return min_t(int, len, PAGE_SIZE);14881437}···16961641 return 0;16971642}1698164316441644+/*16451645+ * This is a pretty gross hack. When building uevent data the driver core16461646+ * may try adding more environment variables to kobj_uevent_env without16471647+ * telling us, so we have no idea how much of the buffer we can use to16481648+ * avoid overflows/-ENOMEM elsewhere. To work around this let's artificially16491649+ * reduce amount of memory we will use for the modalias environment variable.16501650+ *16511651+ * The potential additions are:16521652+ *16531653+ * SEQNUM=18446744073709551615 - (%llu - 28 bytes)16541654+ * HOME=/ (6 bytes)16551655+ * PATH=/sbin:/bin:/usr/sbin:/usr/bin (34 bytes)16561656+ *16571657+ * 68 bytes total. Allow extra buffer - 96 bytes16581658+ */16591659+#define UEVENT_ENV_EXTRA_LEN 9616601660+16991661static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,17001662 const struct input_dev *dev)17011663{···17221650 return -ENOMEM;1723165117241652 len = input_print_modalias(&env->buf[env->buflen - 1],17251725- sizeof(env->buf) - env->buflen,17261726- dev, 0);17271727- if (len >= (sizeof(env->buf) - env->buflen))16531653+ (int)sizeof(env->buf) - env->buflen -16541654+ UEVENT_ENV_EXTRA_LEN,16551655+ dev);16561656+ if (len >= ((int)sizeof(env->buf) - env->buflen -16571657+ UEVENT_ENV_EXTRA_LEN))17281658 return -ENOMEM;1729165917301660 env->buflen += len;
···3535 * @rows: Number of rows in the keypad3636 * @cols: Number of columns in the keypad3737 * @row_shift: log2 or number of rows, rounded up3838- * @keymap_data: Matrix keymap data used to convert to keyscan values3938 * @ghost_filter: true to enable the matrix key-ghosting filter4039 * @valid_keys: bitmap of existing keys for each matrix column4140 * @old_kb_state: bitmap of keys pressed last scan···4950 unsigned int rows;5051 unsigned int cols;5152 int row_shift;5252- const struct matrix_keymap_data *keymap_data;5353 bool ghost_filter;5454 uint8_t *valid_keys;5555 uint8_t *old_kb_state;