18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  HID support for Linux
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 1999 Andreas Gal
68c2ecf20Sopenharmony_ci *  Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
78c2ecf20Sopenharmony_ci *  Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
88c2ecf20Sopenharmony_ci *  Copyright (c) 2006-2012 Jiri Kosina
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/init.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/list.h>
218c2ecf20Sopenharmony_ci#include <linux/mm.h>
228c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
238c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
248c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
258c2ecf20Sopenharmony_ci#include <linux/input.h>
268c2ecf20Sopenharmony_ci#include <linux/wait.h>
278c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
288c2ecf20Sopenharmony_ci#include <linux/sched.h>
298c2ecf20Sopenharmony_ci#include <linux/semaphore.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/hid.h>
328c2ecf20Sopenharmony_ci#include <linux/hiddev.h>
338c2ecf20Sopenharmony_ci#include <linux/hid-debug.h>
348c2ecf20Sopenharmony_ci#include <linux/hidraw.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include "hid-ids.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * Version Information
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define DRIVER_DESC "HID core driver"
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ciint hid_debug = 0;
458c2ecf20Sopenharmony_cimodule_param_named(debug, hid_debug, int, 0600);
468c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "toggle HID debugging messages");
478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_debug);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int hid_ignore_special_drivers = 0;
508c2ecf20Sopenharmony_cimodule_param_named(ignore_special_drivers, hid_ignore_special_drivers, int, 0600);
518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ignore_special_drivers, "Ignore any special drivers and handle all devices by generic driver");
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/*
548c2ecf20Sopenharmony_ci * Register a new report for a device.
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct hid_report *hid_register_report(struct hid_device *device,
588c2ecf20Sopenharmony_ci				       unsigned int type, unsigned int id,
598c2ecf20Sopenharmony_ci				       unsigned int application)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct hid_report_enum *report_enum = device->report_enum + type;
628c2ecf20Sopenharmony_ci	struct hid_report *report;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	if (id >= HID_MAX_IDS)
658c2ecf20Sopenharmony_ci		return NULL;
668c2ecf20Sopenharmony_ci	if (report_enum->report_id_hash[id])
678c2ecf20Sopenharmony_ci		return report_enum->report_id_hash[id];
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	report = kzalloc(sizeof(struct hid_report), GFP_KERNEL);
708c2ecf20Sopenharmony_ci	if (!report)
718c2ecf20Sopenharmony_ci		return NULL;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (id != 0)
748c2ecf20Sopenharmony_ci		report_enum->numbered = 1;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	report->id = id;
778c2ecf20Sopenharmony_ci	report->type = type;
788c2ecf20Sopenharmony_ci	report->size = 0;
798c2ecf20Sopenharmony_ci	report->device = device;
808c2ecf20Sopenharmony_ci	report->application = application;
818c2ecf20Sopenharmony_ci	report_enum->report_id_hash[id] = report;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	list_add_tail(&report->list, &report_enum->report_list);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return report;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_register_report);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/*
908c2ecf20Sopenharmony_ci * Register a new field for this report.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic struct hid_field *hid_register_field(struct hid_report *report, unsigned usages)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct hid_field *field;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (report->maxfield == HID_MAX_FIELDS) {
988c2ecf20Sopenharmony_ci		hid_err(report->device, "too many fields in report\n");
998c2ecf20Sopenharmony_ci		return NULL;
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	field = kzalloc((sizeof(struct hid_field) +
1038c2ecf20Sopenharmony_ci			 usages * sizeof(struct hid_usage) +
1048c2ecf20Sopenharmony_ci			 usages * sizeof(unsigned)), GFP_KERNEL);
1058c2ecf20Sopenharmony_ci	if (!field)
1068c2ecf20Sopenharmony_ci		return NULL;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	field->index = report->maxfield++;
1098c2ecf20Sopenharmony_ci	report->field[field->index] = field;
1108c2ecf20Sopenharmony_ci	field->usage = (struct hid_usage *)(field + 1);
1118c2ecf20Sopenharmony_ci	field->value = (s32 *)(field->usage + usages);
1128c2ecf20Sopenharmony_ci	field->report = report;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return field;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/*
1188c2ecf20Sopenharmony_ci * Open a collection. The type/usage is pushed on the stack.
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic int open_collection(struct hid_parser *parser, unsigned type)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct hid_collection *collection;
1248c2ecf20Sopenharmony_ci	unsigned usage;
1258c2ecf20Sopenharmony_ci	int collection_index;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	usage = parser->local.usage[0];
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	if (parser->collection_stack_ptr == parser->collection_stack_size) {
1308c2ecf20Sopenharmony_ci		unsigned int *collection_stack;
1318c2ecf20Sopenharmony_ci		unsigned int new_size = parser->collection_stack_size +
1328c2ecf20Sopenharmony_ci					HID_COLLECTION_STACK_SIZE;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		collection_stack = krealloc(parser->collection_stack,
1358c2ecf20Sopenharmony_ci					    new_size * sizeof(unsigned int),
1368c2ecf20Sopenharmony_ci					    GFP_KERNEL);
1378c2ecf20Sopenharmony_ci		if (!collection_stack)
1388c2ecf20Sopenharmony_ci			return -ENOMEM;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		parser->collection_stack = collection_stack;
1418c2ecf20Sopenharmony_ci		parser->collection_stack_size = new_size;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (parser->device->maxcollection == parser->device->collection_size) {
1458c2ecf20Sopenharmony_ci		collection = kmalloc(
1468c2ecf20Sopenharmony_ci				array3_size(sizeof(struct hid_collection),
1478c2ecf20Sopenharmony_ci					    parser->device->collection_size,
1488c2ecf20Sopenharmony_ci					    2),
1498c2ecf20Sopenharmony_ci				GFP_KERNEL);
1508c2ecf20Sopenharmony_ci		if (collection == NULL) {
1518c2ecf20Sopenharmony_ci			hid_err(parser->device, "failed to reallocate collection array\n");
1528c2ecf20Sopenharmony_ci			return -ENOMEM;
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci		memcpy(collection, parser->device->collection,
1558c2ecf20Sopenharmony_ci			sizeof(struct hid_collection) *
1568c2ecf20Sopenharmony_ci			parser->device->collection_size);
1578c2ecf20Sopenharmony_ci		memset(collection + parser->device->collection_size, 0,
1588c2ecf20Sopenharmony_ci			sizeof(struct hid_collection) *
1598c2ecf20Sopenharmony_ci			parser->device->collection_size);
1608c2ecf20Sopenharmony_ci		kfree(parser->device->collection);
1618c2ecf20Sopenharmony_ci		parser->device->collection = collection;
1628c2ecf20Sopenharmony_ci		parser->device->collection_size *= 2;
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	parser->collection_stack[parser->collection_stack_ptr++] =
1668c2ecf20Sopenharmony_ci		parser->device->maxcollection;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	collection_index = parser->device->maxcollection++;
1698c2ecf20Sopenharmony_ci	collection = parser->device->collection + collection_index;
1708c2ecf20Sopenharmony_ci	collection->type = type;
1718c2ecf20Sopenharmony_ci	collection->usage = usage;
1728c2ecf20Sopenharmony_ci	collection->level = parser->collection_stack_ptr - 1;
1738c2ecf20Sopenharmony_ci	collection->parent_idx = (collection->level == 0) ? -1 :
1748c2ecf20Sopenharmony_ci		parser->collection_stack[collection->level - 1];
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (type == HID_COLLECTION_APPLICATION)
1778c2ecf20Sopenharmony_ci		parser->device->maxapplication++;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return 0;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/*
1838c2ecf20Sopenharmony_ci * Close a collection.
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int close_collection(struct hid_parser *parser)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	if (!parser->collection_stack_ptr) {
1898c2ecf20Sopenharmony_ci		hid_err(parser->device, "collection stack underflow\n");
1908c2ecf20Sopenharmony_ci		return -EINVAL;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci	parser->collection_stack_ptr--;
1938c2ecf20Sopenharmony_ci	return 0;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/*
1978c2ecf20Sopenharmony_ci * Climb up the stack, search for the specified collection type
1988c2ecf20Sopenharmony_ci * and return the usage.
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic unsigned hid_lookup_collection(struct hid_parser *parser, unsigned type)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	struct hid_collection *collection = parser->device->collection;
2048c2ecf20Sopenharmony_ci	int n;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	for (n = parser->collection_stack_ptr - 1; n >= 0; n--) {
2078c2ecf20Sopenharmony_ci		unsigned index = parser->collection_stack[n];
2088c2ecf20Sopenharmony_ci		if (collection[index].type == type)
2098c2ecf20Sopenharmony_ci			return collection[index].usage;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci	return 0; /* we know nothing about this usage type */
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci/*
2158c2ecf20Sopenharmony_ci * Concatenate usage which defines 16 bits or less with the
2168c2ecf20Sopenharmony_ci * currently defined usage page to form a 32 bit usage
2178c2ecf20Sopenharmony_ci */
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic void complete_usage(struct hid_parser *parser, unsigned int index)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	parser->local.usage[index] &= 0xFFFF;
2228c2ecf20Sopenharmony_ci	parser->local.usage[index] |=
2238c2ecf20Sopenharmony_ci		(parser->global.usage_page & 0xFFFF) << 16;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/*
2278c2ecf20Sopenharmony_ci * Add a usage to the temporary parser table.
2288c2ecf20Sopenharmony_ci */
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic int hid_add_usage(struct hid_parser *parser, unsigned usage, u8 size)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	if (parser->local.usage_index >= HID_MAX_USAGES) {
2338c2ecf20Sopenharmony_ci		hid_err(parser->device, "usage index exceeded\n");
2348c2ecf20Sopenharmony_ci		return -1;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci	parser->local.usage[parser->local.usage_index] = usage;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/*
2398c2ecf20Sopenharmony_ci	 * If Usage item only includes usage id, concatenate it with
2408c2ecf20Sopenharmony_ci	 * currently defined usage page
2418c2ecf20Sopenharmony_ci	 */
2428c2ecf20Sopenharmony_ci	if (size <= 2)
2438c2ecf20Sopenharmony_ci		complete_usage(parser, parser->local.usage_index);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	parser->local.usage_size[parser->local.usage_index] = size;
2468c2ecf20Sopenharmony_ci	parser->local.collection_index[parser->local.usage_index] =
2478c2ecf20Sopenharmony_ci		parser->collection_stack_ptr ?
2488c2ecf20Sopenharmony_ci		parser->collection_stack[parser->collection_stack_ptr - 1] : 0;
2498c2ecf20Sopenharmony_ci	parser->local.usage_index++;
2508c2ecf20Sopenharmony_ci	return 0;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/*
2548c2ecf20Sopenharmony_ci * Register a new field for this report.
2558c2ecf20Sopenharmony_ci */
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic int hid_add_field(struct hid_parser *parser, unsigned report_type, unsigned flags)
2588c2ecf20Sopenharmony_ci{
2598c2ecf20Sopenharmony_ci	struct hid_report *report;
2608c2ecf20Sopenharmony_ci	struct hid_field *field;
2618c2ecf20Sopenharmony_ci	unsigned int max_buffer_size = HID_MAX_BUFFER_SIZE;
2628c2ecf20Sopenharmony_ci	unsigned int usages;
2638c2ecf20Sopenharmony_ci	unsigned int offset;
2648c2ecf20Sopenharmony_ci	unsigned int i;
2658c2ecf20Sopenharmony_ci	unsigned int application;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	application = hid_lookup_collection(parser, HID_COLLECTION_APPLICATION);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	report = hid_register_report(parser->device, report_type,
2708c2ecf20Sopenharmony_ci				     parser->global.report_id, application);
2718c2ecf20Sopenharmony_ci	if (!report) {
2728c2ecf20Sopenharmony_ci		hid_err(parser->device, "hid_register_report failed\n");
2738c2ecf20Sopenharmony_ci		return -1;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/* Handle both signed and unsigned cases properly */
2778c2ecf20Sopenharmony_ci	if ((parser->global.logical_minimum < 0 &&
2788c2ecf20Sopenharmony_ci		parser->global.logical_maximum <
2798c2ecf20Sopenharmony_ci		parser->global.logical_minimum) ||
2808c2ecf20Sopenharmony_ci		(parser->global.logical_minimum >= 0 &&
2818c2ecf20Sopenharmony_ci		(__u32)parser->global.logical_maximum <
2828c2ecf20Sopenharmony_ci		(__u32)parser->global.logical_minimum)) {
2838c2ecf20Sopenharmony_ci		dbg_hid("logical range invalid 0x%x 0x%x\n",
2848c2ecf20Sopenharmony_ci			parser->global.logical_minimum,
2858c2ecf20Sopenharmony_ci			parser->global.logical_maximum);
2868c2ecf20Sopenharmony_ci		return -1;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	offset = report->size;
2908c2ecf20Sopenharmony_ci	report->size += parser->global.report_size * parser->global.report_count;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	if (parser->device->ll_driver->max_buffer_size)
2938c2ecf20Sopenharmony_ci		max_buffer_size = parser->device->ll_driver->max_buffer_size;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	/* Total size check: Allow for possible report index byte */
2968c2ecf20Sopenharmony_ci	if (report->size > (max_buffer_size - 1) << 3) {
2978c2ecf20Sopenharmony_ci		hid_err(parser->device, "report is too long\n");
2988c2ecf20Sopenharmony_ci		return -1;
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	if (!parser->local.usage_index) /* Ignore padding fields */
3028c2ecf20Sopenharmony_ci		return 0;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	usages = max_t(unsigned, parser->local.usage_index,
3058c2ecf20Sopenharmony_ci				 parser->global.report_count);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	field = hid_register_field(report, usages);
3088c2ecf20Sopenharmony_ci	if (!field)
3098c2ecf20Sopenharmony_ci		return 0;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	field->physical = hid_lookup_collection(parser, HID_COLLECTION_PHYSICAL);
3128c2ecf20Sopenharmony_ci	field->logical = hid_lookup_collection(parser, HID_COLLECTION_LOGICAL);
3138c2ecf20Sopenharmony_ci	field->application = application;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	for (i = 0; i < usages; i++) {
3168c2ecf20Sopenharmony_ci		unsigned j = i;
3178c2ecf20Sopenharmony_ci		/* Duplicate the last usage we parsed if we have excess values */
3188c2ecf20Sopenharmony_ci		if (i >= parser->local.usage_index)
3198c2ecf20Sopenharmony_ci			j = parser->local.usage_index - 1;
3208c2ecf20Sopenharmony_ci		field->usage[i].hid = parser->local.usage[j];
3218c2ecf20Sopenharmony_ci		field->usage[i].collection_index =
3228c2ecf20Sopenharmony_ci			parser->local.collection_index[j];
3238c2ecf20Sopenharmony_ci		field->usage[i].usage_index = i;
3248c2ecf20Sopenharmony_ci		field->usage[i].resolution_multiplier = 1;
3258c2ecf20Sopenharmony_ci	}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	field->maxusage = usages;
3288c2ecf20Sopenharmony_ci	field->flags = flags;
3298c2ecf20Sopenharmony_ci	field->report_offset = offset;
3308c2ecf20Sopenharmony_ci	field->report_type = report_type;
3318c2ecf20Sopenharmony_ci	field->report_size = parser->global.report_size;
3328c2ecf20Sopenharmony_ci	field->report_count = parser->global.report_count;
3338c2ecf20Sopenharmony_ci	field->logical_minimum = parser->global.logical_minimum;
3348c2ecf20Sopenharmony_ci	field->logical_maximum = parser->global.logical_maximum;
3358c2ecf20Sopenharmony_ci	field->physical_minimum = parser->global.physical_minimum;
3368c2ecf20Sopenharmony_ci	field->physical_maximum = parser->global.physical_maximum;
3378c2ecf20Sopenharmony_ci	field->unit_exponent = parser->global.unit_exponent;
3388c2ecf20Sopenharmony_ci	field->unit = parser->global.unit;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	return 0;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/*
3448c2ecf20Sopenharmony_ci * Read data value from item.
3458c2ecf20Sopenharmony_ci */
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic u32 item_udata(struct hid_item *item)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	switch (item->size) {
3508c2ecf20Sopenharmony_ci	case 1: return item->data.u8;
3518c2ecf20Sopenharmony_ci	case 2: return item->data.u16;
3528c2ecf20Sopenharmony_ci	case 4: return item->data.u32;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci	return 0;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic s32 item_sdata(struct hid_item *item)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	switch (item->size) {
3608c2ecf20Sopenharmony_ci	case 1: return item->data.s8;
3618c2ecf20Sopenharmony_ci	case 2: return item->data.s16;
3628c2ecf20Sopenharmony_ci	case 4: return item->data.s32;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci	return 0;
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci/*
3688c2ecf20Sopenharmony_ci * Process a global item.
3698c2ecf20Sopenharmony_ci */
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic int hid_parser_global(struct hid_parser *parser, struct hid_item *item)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	__s32 raw_value;
3748c2ecf20Sopenharmony_ci	switch (item->tag) {
3758c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_PUSH:
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci		if (parser->global_stack_ptr == HID_GLOBAL_STACK_SIZE) {
3788c2ecf20Sopenharmony_ci			hid_err(parser->device, "global environment stack overflow\n");
3798c2ecf20Sopenharmony_ci			return -1;
3808c2ecf20Sopenharmony_ci		}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		memcpy(parser->global_stack + parser->global_stack_ptr++,
3838c2ecf20Sopenharmony_ci			&parser->global, sizeof(struct hid_global));
3848c2ecf20Sopenharmony_ci		return 0;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_POP:
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		if (!parser->global_stack_ptr) {
3898c2ecf20Sopenharmony_ci			hid_err(parser->device, "global environment stack underflow\n");
3908c2ecf20Sopenharmony_ci			return -1;
3918c2ecf20Sopenharmony_ci		}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci		memcpy(&parser->global, parser->global_stack +
3948c2ecf20Sopenharmony_ci			--parser->global_stack_ptr, sizeof(struct hid_global));
3958c2ecf20Sopenharmony_ci		return 0;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_USAGE_PAGE:
3988c2ecf20Sopenharmony_ci		parser->global.usage_page = item_udata(item);
3998c2ecf20Sopenharmony_ci		return 0;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_LOGICAL_MINIMUM:
4028c2ecf20Sopenharmony_ci		parser->global.logical_minimum = item_sdata(item);
4038c2ecf20Sopenharmony_ci		return 0;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_LOGICAL_MAXIMUM:
4068c2ecf20Sopenharmony_ci		if (parser->global.logical_minimum < 0)
4078c2ecf20Sopenharmony_ci			parser->global.logical_maximum = item_sdata(item);
4088c2ecf20Sopenharmony_ci		else
4098c2ecf20Sopenharmony_ci			parser->global.logical_maximum = item_udata(item);
4108c2ecf20Sopenharmony_ci		return 0;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MINIMUM:
4138c2ecf20Sopenharmony_ci		parser->global.physical_minimum = item_sdata(item);
4148c2ecf20Sopenharmony_ci		return 0;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_PHYSICAL_MAXIMUM:
4178c2ecf20Sopenharmony_ci		if (parser->global.physical_minimum < 0)
4188c2ecf20Sopenharmony_ci			parser->global.physical_maximum = item_sdata(item);
4198c2ecf20Sopenharmony_ci		else
4208c2ecf20Sopenharmony_ci			parser->global.physical_maximum = item_udata(item);
4218c2ecf20Sopenharmony_ci		return 0;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT:
4248c2ecf20Sopenharmony_ci		/* Many devices provide unit exponent as a two's complement
4258c2ecf20Sopenharmony_ci		 * nibble due to the common misunderstanding of HID
4268c2ecf20Sopenharmony_ci		 * specification 1.11, 6.2.2.7 Global Items. Attempt to handle
4278c2ecf20Sopenharmony_ci		 * both this and the standard encoding. */
4288c2ecf20Sopenharmony_ci		raw_value = item_sdata(item);
4298c2ecf20Sopenharmony_ci		if (!(raw_value & 0xfffffff0))
4308c2ecf20Sopenharmony_ci			parser->global.unit_exponent = hid_snto32(raw_value, 4);
4318c2ecf20Sopenharmony_ci		else
4328c2ecf20Sopenharmony_ci			parser->global.unit_exponent = raw_value;
4338c2ecf20Sopenharmony_ci		return 0;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_UNIT:
4368c2ecf20Sopenharmony_ci		parser->global.unit = item_udata(item);
4378c2ecf20Sopenharmony_ci		return 0;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_REPORT_SIZE:
4408c2ecf20Sopenharmony_ci		parser->global.report_size = item_udata(item);
4418c2ecf20Sopenharmony_ci		if (parser->global.report_size > 256) {
4428c2ecf20Sopenharmony_ci			hid_err(parser->device, "invalid report_size %d\n",
4438c2ecf20Sopenharmony_ci					parser->global.report_size);
4448c2ecf20Sopenharmony_ci			return -1;
4458c2ecf20Sopenharmony_ci		}
4468c2ecf20Sopenharmony_ci		return 0;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_REPORT_COUNT:
4498c2ecf20Sopenharmony_ci		parser->global.report_count = item_udata(item);
4508c2ecf20Sopenharmony_ci		if (parser->global.report_count > HID_MAX_USAGES) {
4518c2ecf20Sopenharmony_ci			hid_err(parser->device, "invalid report_count %d\n",
4528c2ecf20Sopenharmony_ci					parser->global.report_count);
4538c2ecf20Sopenharmony_ci			return -1;
4548c2ecf20Sopenharmony_ci		}
4558c2ecf20Sopenharmony_ci		return 0;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	case HID_GLOBAL_ITEM_TAG_REPORT_ID:
4588c2ecf20Sopenharmony_ci		parser->global.report_id = item_udata(item);
4598c2ecf20Sopenharmony_ci		if (parser->global.report_id == 0 ||
4608c2ecf20Sopenharmony_ci		    parser->global.report_id >= HID_MAX_IDS) {
4618c2ecf20Sopenharmony_ci			hid_err(parser->device, "report_id %u is invalid\n",
4628c2ecf20Sopenharmony_ci				parser->global.report_id);
4638c2ecf20Sopenharmony_ci			return -1;
4648c2ecf20Sopenharmony_ci		}
4658c2ecf20Sopenharmony_ci		return 0;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	default:
4688c2ecf20Sopenharmony_ci		hid_err(parser->device, "unknown global tag 0x%x\n", item->tag);
4698c2ecf20Sopenharmony_ci		return -1;
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci}
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci/*
4748c2ecf20Sopenharmony_ci * Process a local item.
4758c2ecf20Sopenharmony_ci */
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int hid_parser_local(struct hid_parser *parser, struct hid_item *item)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	__u32 data;
4808c2ecf20Sopenharmony_ci	unsigned n;
4818c2ecf20Sopenharmony_ci	__u32 count;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	data = item_udata(item);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	switch (item->tag) {
4868c2ecf20Sopenharmony_ci	case HID_LOCAL_ITEM_TAG_DELIMITER:
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci		if (data) {
4898c2ecf20Sopenharmony_ci			/*
4908c2ecf20Sopenharmony_ci			 * We treat items before the first delimiter
4918c2ecf20Sopenharmony_ci			 * as global to all usage sets (branch 0).
4928c2ecf20Sopenharmony_ci			 * In the moment we process only these global
4938c2ecf20Sopenharmony_ci			 * items and the first delimiter set.
4948c2ecf20Sopenharmony_ci			 */
4958c2ecf20Sopenharmony_ci			if (parser->local.delimiter_depth != 0) {
4968c2ecf20Sopenharmony_ci				hid_err(parser->device, "nested delimiters\n");
4978c2ecf20Sopenharmony_ci				return -1;
4988c2ecf20Sopenharmony_ci			}
4998c2ecf20Sopenharmony_ci			parser->local.delimiter_depth++;
5008c2ecf20Sopenharmony_ci			parser->local.delimiter_branch++;
5018c2ecf20Sopenharmony_ci		} else {
5028c2ecf20Sopenharmony_ci			if (parser->local.delimiter_depth < 1) {
5038c2ecf20Sopenharmony_ci				hid_err(parser->device, "bogus close delimiter\n");
5048c2ecf20Sopenharmony_ci				return -1;
5058c2ecf20Sopenharmony_ci			}
5068c2ecf20Sopenharmony_ci			parser->local.delimiter_depth--;
5078c2ecf20Sopenharmony_ci		}
5088c2ecf20Sopenharmony_ci		return 0;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	case HID_LOCAL_ITEM_TAG_USAGE:
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci		if (parser->local.delimiter_branch > 1) {
5138c2ecf20Sopenharmony_ci			dbg_hid("alternative usage ignored\n");
5148c2ecf20Sopenharmony_ci			return 0;
5158c2ecf20Sopenharmony_ci		}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci		return hid_add_usage(parser, data, item->size);
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	case HID_LOCAL_ITEM_TAG_USAGE_MINIMUM:
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci		if (parser->local.delimiter_branch > 1) {
5228c2ecf20Sopenharmony_ci			dbg_hid("alternative usage ignored\n");
5238c2ecf20Sopenharmony_ci			return 0;
5248c2ecf20Sopenharmony_ci		}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci		parser->local.usage_minimum = data;
5278c2ecf20Sopenharmony_ci		return 0;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	case HID_LOCAL_ITEM_TAG_USAGE_MAXIMUM:
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci		if (parser->local.delimiter_branch > 1) {
5328c2ecf20Sopenharmony_ci			dbg_hid("alternative usage ignored\n");
5338c2ecf20Sopenharmony_ci			return 0;
5348c2ecf20Sopenharmony_ci		}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		count = data - parser->local.usage_minimum;
5378c2ecf20Sopenharmony_ci		if (count + parser->local.usage_index >= HID_MAX_USAGES) {
5388c2ecf20Sopenharmony_ci			/*
5398c2ecf20Sopenharmony_ci			 * We do not warn if the name is not set, we are
5408c2ecf20Sopenharmony_ci			 * actually pre-scanning the device.
5418c2ecf20Sopenharmony_ci			 */
5428c2ecf20Sopenharmony_ci			if (dev_name(&parser->device->dev))
5438c2ecf20Sopenharmony_ci				hid_warn(parser->device,
5448c2ecf20Sopenharmony_ci					 "ignoring exceeding usage max\n");
5458c2ecf20Sopenharmony_ci			data = HID_MAX_USAGES - parser->local.usage_index +
5468c2ecf20Sopenharmony_ci				parser->local.usage_minimum - 1;
5478c2ecf20Sopenharmony_ci			if (data <= 0) {
5488c2ecf20Sopenharmony_ci				hid_err(parser->device,
5498c2ecf20Sopenharmony_ci					"no more usage index available\n");
5508c2ecf20Sopenharmony_ci				return -1;
5518c2ecf20Sopenharmony_ci			}
5528c2ecf20Sopenharmony_ci		}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		for (n = parser->local.usage_minimum; n <= data; n++)
5558c2ecf20Sopenharmony_ci			if (hid_add_usage(parser, n, item->size)) {
5568c2ecf20Sopenharmony_ci				dbg_hid("hid_add_usage failed\n");
5578c2ecf20Sopenharmony_ci				return -1;
5588c2ecf20Sopenharmony_ci			}
5598c2ecf20Sopenharmony_ci		return 0;
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	default:
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		dbg_hid("unknown local item tag 0x%x\n", item->tag);
5648c2ecf20Sopenharmony_ci		return 0;
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci	return 0;
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci/*
5708c2ecf20Sopenharmony_ci * Concatenate Usage Pages into Usages where relevant:
5718c2ecf20Sopenharmony_ci * As per specification, 6.2.2.8: "When the parser encounters a main item it
5728c2ecf20Sopenharmony_ci * concatenates the last declared Usage Page with a Usage to form a complete
5738c2ecf20Sopenharmony_ci * usage value."
5748c2ecf20Sopenharmony_ci */
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic void hid_concatenate_last_usage_page(struct hid_parser *parser)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	int i;
5798c2ecf20Sopenharmony_ci	unsigned int usage_page;
5808c2ecf20Sopenharmony_ci	unsigned int current_page;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	if (!parser->local.usage_index)
5838c2ecf20Sopenharmony_ci		return;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	usage_page = parser->global.usage_page;
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	/*
5888c2ecf20Sopenharmony_ci	 * Concatenate usage page again only if last declared Usage Page
5898c2ecf20Sopenharmony_ci	 * has not been already used in previous usages concatenation
5908c2ecf20Sopenharmony_ci	 */
5918c2ecf20Sopenharmony_ci	for (i = parser->local.usage_index - 1; i >= 0; i--) {
5928c2ecf20Sopenharmony_ci		if (parser->local.usage_size[i] > 2)
5938c2ecf20Sopenharmony_ci			/* Ignore extended usages */
5948c2ecf20Sopenharmony_ci			continue;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci		current_page = parser->local.usage[i] >> 16;
5978c2ecf20Sopenharmony_ci		if (current_page == usage_page)
5988c2ecf20Sopenharmony_ci			break;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci		complete_usage(parser, i);
6018c2ecf20Sopenharmony_ci	}
6028c2ecf20Sopenharmony_ci}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci/*
6058c2ecf20Sopenharmony_ci * Process a main item.
6068c2ecf20Sopenharmony_ci */
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_cistatic int hid_parser_main(struct hid_parser *parser, struct hid_item *item)
6098c2ecf20Sopenharmony_ci{
6108c2ecf20Sopenharmony_ci	__u32 data;
6118c2ecf20Sopenharmony_ci	int ret;
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	hid_concatenate_last_usage_page(parser);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	data = item_udata(item);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	switch (item->tag) {
6188c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
6198c2ecf20Sopenharmony_ci		ret = open_collection(parser, data & 0xff);
6208c2ecf20Sopenharmony_ci		break;
6218c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_END_COLLECTION:
6228c2ecf20Sopenharmony_ci		ret = close_collection(parser);
6238c2ecf20Sopenharmony_ci		break;
6248c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_INPUT:
6258c2ecf20Sopenharmony_ci		ret = hid_add_field(parser, HID_INPUT_REPORT, data);
6268c2ecf20Sopenharmony_ci		break;
6278c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_OUTPUT:
6288c2ecf20Sopenharmony_ci		ret = hid_add_field(parser, HID_OUTPUT_REPORT, data);
6298c2ecf20Sopenharmony_ci		break;
6308c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_FEATURE:
6318c2ecf20Sopenharmony_ci		ret = hid_add_field(parser, HID_FEATURE_REPORT, data);
6328c2ecf20Sopenharmony_ci		break;
6338c2ecf20Sopenharmony_ci	default:
6348c2ecf20Sopenharmony_ci		hid_warn(parser->device, "unknown main item tag 0x%x\n", item->tag);
6358c2ecf20Sopenharmony_ci		ret = 0;
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	memset(&parser->local, 0, sizeof(parser->local));	/* Reset the local parser environment */
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	return ret;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci/*
6448c2ecf20Sopenharmony_ci * Process a reserved item.
6458c2ecf20Sopenharmony_ci */
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic int hid_parser_reserved(struct hid_parser *parser, struct hid_item *item)
6488c2ecf20Sopenharmony_ci{
6498c2ecf20Sopenharmony_ci	dbg_hid("reserved item type, tag 0x%x\n", item->tag);
6508c2ecf20Sopenharmony_ci	return 0;
6518c2ecf20Sopenharmony_ci}
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci/*
6548c2ecf20Sopenharmony_ci * Free a report and all registered fields. The field->usage and
6558c2ecf20Sopenharmony_ci * field->value table's are allocated behind the field, so we need
6568c2ecf20Sopenharmony_ci * only to free(field) itself.
6578c2ecf20Sopenharmony_ci */
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_cistatic void hid_free_report(struct hid_report *report)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	unsigned n;
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci	for (n = 0; n < report->maxfield; n++)
6648c2ecf20Sopenharmony_ci		kfree(report->field[n]);
6658c2ecf20Sopenharmony_ci	kfree(report);
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci/*
6698c2ecf20Sopenharmony_ci * Close report. This function returns the device
6708c2ecf20Sopenharmony_ci * state to the point prior to hid_open_report().
6718c2ecf20Sopenharmony_ci */
6728c2ecf20Sopenharmony_cistatic void hid_close_report(struct hid_device *device)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	unsigned i, j;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	for (i = 0; i < HID_REPORT_TYPES; i++) {
6778c2ecf20Sopenharmony_ci		struct hid_report_enum *report_enum = device->report_enum + i;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		for (j = 0; j < HID_MAX_IDS; j++) {
6808c2ecf20Sopenharmony_ci			struct hid_report *report = report_enum->report_id_hash[j];
6818c2ecf20Sopenharmony_ci			if (report)
6828c2ecf20Sopenharmony_ci				hid_free_report(report);
6838c2ecf20Sopenharmony_ci		}
6848c2ecf20Sopenharmony_ci		memset(report_enum, 0, sizeof(*report_enum));
6858c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&report_enum->report_list);
6868c2ecf20Sopenharmony_ci	}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	kfree(device->rdesc);
6898c2ecf20Sopenharmony_ci	device->rdesc = NULL;
6908c2ecf20Sopenharmony_ci	device->rsize = 0;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	kfree(device->collection);
6938c2ecf20Sopenharmony_ci	device->collection = NULL;
6948c2ecf20Sopenharmony_ci	device->collection_size = 0;
6958c2ecf20Sopenharmony_ci	device->maxcollection = 0;
6968c2ecf20Sopenharmony_ci	device->maxapplication = 0;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	device->status &= ~HID_STAT_PARSED;
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci/*
7028c2ecf20Sopenharmony_ci * Free a device structure, all reports, and all fields.
7038c2ecf20Sopenharmony_ci */
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_civoid hiddev_free(struct kref *ref)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	struct hid_device *hid = container_of(ref, struct hid_device, ref);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	hid_close_report(hid);
7108c2ecf20Sopenharmony_ci	kfree(hid->dev_rdesc);
7118c2ecf20Sopenharmony_ci	kfree(hid);
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic void hid_device_release(struct device *dev)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	struct hid_device *hid = to_hid_device(dev);
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	kref_put(&hid->ref, hiddev_free);
7198c2ecf20Sopenharmony_ci}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci/*
7228c2ecf20Sopenharmony_ci * Fetch a report description item from the data stream. We support long
7238c2ecf20Sopenharmony_ci * items, though they are not used yet.
7248c2ecf20Sopenharmony_ci */
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_cistatic u8 *fetch_item(__u8 *start, __u8 *end, struct hid_item *item)
7278c2ecf20Sopenharmony_ci{
7288c2ecf20Sopenharmony_ci	u8 b;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	if ((end - start) <= 0)
7318c2ecf20Sopenharmony_ci		return NULL;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	b = *start++;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	item->type = (b >> 2) & 3;
7368c2ecf20Sopenharmony_ci	item->tag  = (b >> 4) & 15;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (item->tag == HID_ITEM_TAG_LONG) {
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci		item->format = HID_ITEM_FORMAT_LONG;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci		if ((end - start) < 2)
7438c2ecf20Sopenharmony_ci			return NULL;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci		item->size = *start++;
7468c2ecf20Sopenharmony_ci		item->tag  = *start++;
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci		if ((end - start) < item->size)
7498c2ecf20Sopenharmony_ci			return NULL;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci		item->data.longdata = start;
7528c2ecf20Sopenharmony_ci		start += item->size;
7538c2ecf20Sopenharmony_ci		return start;
7548c2ecf20Sopenharmony_ci	}
7558c2ecf20Sopenharmony_ci
7568c2ecf20Sopenharmony_ci	item->format = HID_ITEM_FORMAT_SHORT;
7578c2ecf20Sopenharmony_ci	item->size = b & 3;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	switch (item->size) {
7608c2ecf20Sopenharmony_ci	case 0:
7618c2ecf20Sopenharmony_ci		return start;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	case 1:
7648c2ecf20Sopenharmony_ci		if ((end - start) < 1)
7658c2ecf20Sopenharmony_ci			return NULL;
7668c2ecf20Sopenharmony_ci		item->data.u8 = *start++;
7678c2ecf20Sopenharmony_ci		return start;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	case 2:
7708c2ecf20Sopenharmony_ci		if ((end - start) < 2)
7718c2ecf20Sopenharmony_ci			return NULL;
7728c2ecf20Sopenharmony_ci		item->data.u16 = get_unaligned_le16(start);
7738c2ecf20Sopenharmony_ci		start = (__u8 *)((__le16 *)start + 1);
7748c2ecf20Sopenharmony_ci		return start;
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	case 3:
7778c2ecf20Sopenharmony_ci		item->size++;
7788c2ecf20Sopenharmony_ci		if ((end - start) < 4)
7798c2ecf20Sopenharmony_ci			return NULL;
7808c2ecf20Sopenharmony_ci		item->data.u32 = get_unaligned_le32(start);
7818c2ecf20Sopenharmony_ci		start = (__u8 *)((__le32 *)start + 1);
7828c2ecf20Sopenharmony_ci		return start;
7838c2ecf20Sopenharmony_ci	}
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	return NULL;
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_cistatic void hid_scan_input_usage(struct hid_parser *parser, u32 usage)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	struct hid_device *hid = parser->device;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	if (usage == HID_DG_CONTACTID)
7938c2ecf20Sopenharmony_ci		hid->group = HID_GROUP_MULTITOUCH;
7948c2ecf20Sopenharmony_ci}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_cistatic void hid_scan_feature_usage(struct hid_parser *parser, u32 usage)
7978c2ecf20Sopenharmony_ci{
7988c2ecf20Sopenharmony_ci	if (usage == 0xff0000c5 && parser->global.report_count == 256 &&
7998c2ecf20Sopenharmony_ci	    parser->global.report_size == 8)
8008c2ecf20Sopenharmony_ci		parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	if (usage == 0xff0000c6 && parser->global.report_count == 1 &&
8038c2ecf20Sopenharmony_ci	    parser->global.report_size == 8)
8048c2ecf20Sopenharmony_ci		parser->scan_flags |= HID_SCAN_FLAG_MT_WIN_8;
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cistatic void hid_scan_collection(struct hid_parser *parser, unsigned type)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	struct hid_device *hid = parser->device;
8108c2ecf20Sopenharmony_ci	int i;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	if (((parser->global.usage_page << 16) == HID_UP_SENSOR) &&
8138c2ecf20Sopenharmony_ci	    type == HID_COLLECTION_PHYSICAL)
8148c2ecf20Sopenharmony_ci		hid->group = HID_GROUP_SENSOR_HUB;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	if (hid->vendor == USB_VENDOR_ID_MICROSOFT &&
8178c2ecf20Sopenharmony_ci	    hid->product == USB_DEVICE_ID_MS_POWER_COVER &&
8188c2ecf20Sopenharmony_ci	    hid->group == HID_GROUP_MULTITOUCH)
8198c2ecf20Sopenharmony_ci		hid->group = HID_GROUP_GENERIC;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	if ((parser->global.usage_page << 16) == HID_UP_GENDESK)
8228c2ecf20Sopenharmony_ci		for (i = 0; i < parser->local.usage_index; i++)
8238c2ecf20Sopenharmony_ci			if (parser->local.usage[i] == HID_GD_POINTER)
8248c2ecf20Sopenharmony_ci				parser->scan_flags |= HID_SCAN_FLAG_GD_POINTER;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	if ((parser->global.usage_page << 16) >= HID_UP_MSVENDOR)
8278c2ecf20Sopenharmony_ci		parser->scan_flags |= HID_SCAN_FLAG_VENDOR_SPECIFIC;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	if ((parser->global.usage_page << 16) == HID_UP_GOOGLEVENDOR)
8308c2ecf20Sopenharmony_ci		for (i = 0; i < parser->local.usage_index; i++)
8318c2ecf20Sopenharmony_ci			if (parser->local.usage[i] ==
8328c2ecf20Sopenharmony_ci					(HID_UP_GOOGLEVENDOR | 0x0001))
8338c2ecf20Sopenharmony_ci				parser->device->group =
8348c2ecf20Sopenharmony_ci					HID_GROUP_VIVALDI;
8358c2ecf20Sopenharmony_ci}
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_cistatic int hid_scan_main(struct hid_parser *parser, struct hid_item *item)
8388c2ecf20Sopenharmony_ci{
8398c2ecf20Sopenharmony_ci	__u32 data;
8408c2ecf20Sopenharmony_ci	int i;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	hid_concatenate_last_usage_page(parser);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	data = item_udata(item);
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	switch (item->tag) {
8478c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_BEGIN_COLLECTION:
8488c2ecf20Sopenharmony_ci		hid_scan_collection(parser, data & 0xff);
8498c2ecf20Sopenharmony_ci		break;
8508c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_END_COLLECTION:
8518c2ecf20Sopenharmony_ci		break;
8528c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_INPUT:
8538c2ecf20Sopenharmony_ci		/* ignore constant inputs, they will be ignored by hid-input */
8548c2ecf20Sopenharmony_ci		if (data & HID_MAIN_ITEM_CONSTANT)
8558c2ecf20Sopenharmony_ci			break;
8568c2ecf20Sopenharmony_ci		for (i = 0; i < parser->local.usage_index; i++)
8578c2ecf20Sopenharmony_ci			hid_scan_input_usage(parser, parser->local.usage[i]);
8588c2ecf20Sopenharmony_ci		break;
8598c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_OUTPUT:
8608c2ecf20Sopenharmony_ci		break;
8618c2ecf20Sopenharmony_ci	case HID_MAIN_ITEM_TAG_FEATURE:
8628c2ecf20Sopenharmony_ci		for (i = 0; i < parser->local.usage_index; i++)
8638c2ecf20Sopenharmony_ci			hid_scan_feature_usage(parser, parser->local.usage[i]);
8648c2ecf20Sopenharmony_ci		break;
8658c2ecf20Sopenharmony_ci	}
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	/* Reset the local parser environment */
8688c2ecf20Sopenharmony_ci	memset(&parser->local, 0, sizeof(parser->local));
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	return 0;
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci/*
8748c2ecf20Sopenharmony_ci * Scan a report descriptor before the device is added to the bus.
8758c2ecf20Sopenharmony_ci * Sets device groups and other properties that determine what driver
8768c2ecf20Sopenharmony_ci * to load.
8778c2ecf20Sopenharmony_ci */
8788c2ecf20Sopenharmony_cistatic int hid_scan_report(struct hid_device *hid)
8798c2ecf20Sopenharmony_ci{
8808c2ecf20Sopenharmony_ci	struct hid_parser *parser;
8818c2ecf20Sopenharmony_ci	struct hid_item item;
8828c2ecf20Sopenharmony_ci	__u8 *start = hid->dev_rdesc;
8838c2ecf20Sopenharmony_ci	__u8 *end = start + hid->dev_rsize;
8848c2ecf20Sopenharmony_ci	static int (*dispatch_type[])(struct hid_parser *parser,
8858c2ecf20Sopenharmony_ci				      struct hid_item *item) = {
8868c2ecf20Sopenharmony_ci		hid_scan_main,
8878c2ecf20Sopenharmony_ci		hid_parser_global,
8888c2ecf20Sopenharmony_ci		hid_parser_local,
8898c2ecf20Sopenharmony_ci		hid_parser_reserved
8908c2ecf20Sopenharmony_ci	};
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	parser = vzalloc(sizeof(struct hid_parser));
8938c2ecf20Sopenharmony_ci	if (!parser)
8948c2ecf20Sopenharmony_ci		return -ENOMEM;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	parser->device = hid;
8978c2ecf20Sopenharmony_ci	hid->group = HID_GROUP_GENERIC;
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	/*
9008c2ecf20Sopenharmony_ci	 * The parsing is simpler than the one in hid_open_report() as we should
9018c2ecf20Sopenharmony_ci	 * be robust against hid errors. Those errors will be raised by
9028c2ecf20Sopenharmony_ci	 * hid_open_report() anyway.
9038c2ecf20Sopenharmony_ci	 */
9048c2ecf20Sopenharmony_ci	while ((start = fetch_item(start, end, &item)) != NULL)
9058c2ecf20Sopenharmony_ci		dispatch_type[item.type](parser, &item);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	/*
9088c2ecf20Sopenharmony_ci	 * Handle special flags set during scanning.
9098c2ecf20Sopenharmony_ci	 */
9108c2ecf20Sopenharmony_ci	if ((parser->scan_flags & HID_SCAN_FLAG_MT_WIN_8) &&
9118c2ecf20Sopenharmony_ci	    (hid->group == HID_GROUP_MULTITOUCH))
9128c2ecf20Sopenharmony_ci		hid->group = HID_GROUP_MULTITOUCH_WIN_8;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	/*
9158c2ecf20Sopenharmony_ci	 * Vendor specific handlings
9168c2ecf20Sopenharmony_ci	 */
9178c2ecf20Sopenharmony_ci	switch (hid->vendor) {
9188c2ecf20Sopenharmony_ci	case USB_VENDOR_ID_WACOM:
9198c2ecf20Sopenharmony_ci		hid->group = HID_GROUP_WACOM;
9208c2ecf20Sopenharmony_ci		break;
9218c2ecf20Sopenharmony_ci	case USB_VENDOR_ID_SYNAPTICS:
9228c2ecf20Sopenharmony_ci		if (hid->group == HID_GROUP_GENERIC)
9238c2ecf20Sopenharmony_ci			if ((parser->scan_flags & HID_SCAN_FLAG_VENDOR_SPECIFIC)
9248c2ecf20Sopenharmony_ci			    && (parser->scan_flags & HID_SCAN_FLAG_GD_POINTER))
9258c2ecf20Sopenharmony_ci				/*
9268c2ecf20Sopenharmony_ci				 * hid-rmi should take care of them,
9278c2ecf20Sopenharmony_ci				 * not hid-generic
9288c2ecf20Sopenharmony_ci				 */
9298c2ecf20Sopenharmony_ci				hid->group = HID_GROUP_RMI;
9308c2ecf20Sopenharmony_ci		break;
9318c2ecf20Sopenharmony_ci	}
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	kfree(parser->collection_stack);
9348c2ecf20Sopenharmony_ci	vfree(parser);
9358c2ecf20Sopenharmony_ci	return 0;
9368c2ecf20Sopenharmony_ci}
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci/**
9398c2ecf20Sopenharmony_ci * hid_parse_report - parse device report
9408c2ecf20Sopenharmony_ci *
9418c2ecf20Sopenharmony_ci * @hid: hid device
9428c2ecf20Sopenharmony_ci * @start: report start
9438c2ecf20Sopenharmony_ci * @size: report size
9448c2ecf20Sopenharmony_ci *
9458c2ecf20Sopenharmony_ci * Allocate the device report as read by the bus driver. This function should
9468c2ecf20Sopenharmony_ci * only be called from parse() in ll drivers.
9478c2ecf20Sopenharmony_ci */
9488c2ecf20Sopenharmony_ciint hid_parse_report(struct hid_device *hid, __u8 *start, unsigned size)
9498c2ecf20Sopenharmony_ci{
9508c2ecf20Sopenharmony_ci	hid->dev_rdesc = kmemdup(start, size, GFP_KERNEL);
9518c2ecf20Sopenharmony_ci	if (!hid->dev_rdesc)
9528c2ecf20Sopenharmony_ci		return -ENOMEM;
9538c2ecf20Sopenharmony_ci	hid->dev_rsize = size;
9548c2ecf20Sopenharmony_ci	return 0;
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_parse_report);
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_cistatic const char * const hid_report_names[] = {
9598c2ecf20Sopenharmony_ci	"HID_INPUT_REPORT",
9608c2ecf20Sopenharmony_ci	"HID_OUTPUT_REPORT",
9618c2ecf20Sopenharmony_ci	"HID_FEATURE_REPORT",
9628c2ecf20Sopenharmony_ci};
9638c2ecf20Sopenharmony_ci/**
9648c2ecf20Sopenharmony_ci * hid_validate_values - validate existing device report's value indexes
9658c2ecf20Sopenharmony_ci *
9668c2ecf20Sopenharmony_ci * @hid: hid device
9678c2ecf20Sopenharmony_ci * @type: which report type to examine
9688c2ecf20Sopenharmony_ci * @id: which report ID to examine (0 for first)
9698c2ecf20Sopenharmony_ci * @field_index: which report field to examine
9708c2ecf20Sopenharmony_ci * @report_counts: expected number of values
9718c2ecf20Sopenharmony_ci *
9728c2ecf20Sopenharmony_ci * Validate the number of values in a given field of a given report, after
9738c2ecf20Sopenharmony_ci * parsing.
9748c2ecf20Sopenharmony_ci */
9758c2ecf20Sopenharmony_cistruct hid_report *hid_validate_values(struct hid_device *hid,
9768c2ecf20Sopenharmony_ci				       unsigned int type, unsigned int id,
9778c2ecf20Sopenharmony_ci				       unsigned int field_index,
9788c2ecf20Sopenharmony_ci				       unsigned int report_counts)
9798c2ecf20Sopenharmony_ci{
9808c2ecf20Sopenharmony_ci	struct hid_report *report;
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	if (type > HID_FEATURE_REPORT) {
9838c2ecf20Sopenharmony_ci		hid_err(hid, "invalid HID report type %u\n", type);
9848c2ecf20Sopenharmony_ci		return NULL;
9858c2ecf20Sopenharmony_ci	}
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	if (id >= HID_MAX_IDS) {
9888c2ecf20Sopenharmony_ci		hid_err(hid, "invalid HID report id %u\n", id);
9898c2ecf20Sopenharmony_ci		return NULL;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	/*
9938c2ecf20Sopenharmony_ci	 * Explicitly not using hid_get_report() here since it depends on
9948c2ecf20Sopenharmony_ci	 * ->numbered being checked, which may not always be the case when
9958c2ecf20Sopenharmony_ci	 * drivers go to access report values.
9968c2ecf20Sopenharmony_ci	 */
9978c2ecf20Sopenharmony_ci	if (id == 0) {
9988c2ecf20Sopenharmony_ci		/*
9998c2ecf20Sopenharmony_ci		 * Validating on id 0 means we should examine the first
10008c2ecf20Sopenharmony_ci		 * report in the list.
10018c2ecf20Sopenharmony_ci		 */
10028c2ecf20Sopenharmony_ci		report = list_first_entry_or_null(
10038c2ecf20Sopenharmony_ci				&hid->report_enum[type].report_list,
10048c2ecf20Sopenharmony_ci				struct hid_report, list);
10058c2ecf20Sopenharmony_ci	} else {
10068c2ecf20Sopenharmony_ci		report = hid->report_enum[type].report_id_hash[id];
10078c2ecf20Sopenharmony_ci	}
10088c2ecf20Sopenharmony_ci	if (!report) {
10098c2ecf20Sopenharmony_ci		hid_err(hid, "missing %s %u\n", hid_report_names[type], id);
10108c2ecf20Sopenharmony_ci		return NULL;
10118c2ecf20Sopenharmony_ci	}
10128c2ecf20Sopenharmony_ci	if (report->maxfield <= field_index) {
10138c2ecf20Sopenharmony_ci		hid_err(hid, "not enough fields in %s %u\n",
10148c2ecf20Sopenharmony_ci			hid_report_names[type], id);
10158c2ecf20Sopenharmony_ci		return NULL;
10168c2ecf20Sopenharmony_ci	}
10178c2ecf20Sopenharmony_ci	if (report->field[field_index]->report_count < report_counts) {
10188c2ecf20Sopenharmony_ci		hid_err(hid, "not enough values in %s %u field %u\n",
10198c2ecf20Sopenharmony_ci			hid_report_names[type], id, field_index);
10208c2ecf20Sopenharmony_ci		return NULL;
10218c2ecf20Sopenharmony_ci	}
10228c2ecf20Sopenharmony_ci	return report;
10238c2ecf20Sopenharmony_ci}
10248c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_validate_values);
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_cistatic int hid_calculate_multiplier(struct hid_device *hid,
10278c2ecf20Sopenharmony_ci				     struct hid_field *multiplier)
10288c2ecf20Sopenharmony_ci{
10298c2ecf20Sopenharmony_ci	int m;
10308c2ecf20Sopenharmony_ci	__s32 v = *multiplier->value;
10318c2ecf20Sopenharmony_ci	__s32 lmin = multiplier->logical_minimum;
10328c2ecf20Sopenharmony_ci	__s32 lmax = multiplier->logical_maximum;
10338c2ecf20Sopenharmony_ci	__s32 pmin = multiplier->physical_minimum;
10348c2ecf20Sopenharmony_ci	__s32 pmax = multiplier->physical_maximum;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	/*
10378c2ecf20Sopenharmony_ci	 * "Because OS implementations will generally divide the control's
10388c2ecf20Sopenharmony_ci	 * reported count by the Effective Resolution Multiplier, designers
10398c2ecf20Sopenharmony_ci	 * should take care not to establish a potential Effective
10408c2ecf20Sopenharmony_ci	 * Resolution Multiplier of zero."
10418c2ecf20Sopenharmony_ci	 * HID Usage Table, v1.12, Section 4.3.1, p31
10428c2ecf20Sopenharmony_ci	 */
10438c2ecf20Sopenharmony_ci	if (lmax - lmin == 0)
10448c2ecf20Sopenharmony_ci		return 1;
10458c2ecf20Sopenharmony_ci	/*
10468c2ecf20Sopenharmony_ci	 * Handling the unit exponent is left as an exercise to whoever
10478c2ecf20Sopenharmony_ci	 * finds a device where that exponent is not 0.
10488c2ecf20Sopenharmony_ci	 */
10498c2ecf20Sopenharmony_ci	m = ((v - lmin)/(lmax - lmin) * (pmax - pmin) + pmin);
10508c2ecf20Sopenharmony_ci	if (unlikely(multiplier->unit_exponent != 0)) {
10518c2ecf20Sopenharmony_ci		hid_warn(hid,
10528c2ecf20Sopenharmony_ci			 "unsupported Resolution Multiplier unit exponent %d\n",
10538c2ecf20Sopenharmony_ci			 multiplier->unit_exponent);
10548c2ecf20Sopenharmony_ci	}
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	/* There are no devices with an effective multiplier > 255 */
10578c2ecf20Sopenharmony_ci	if (unlikely(m == 0 || m > 255 || m < -255)) {
10588c2ecf20Sopenharmony_ci		hid_warn(hid, "unsupported Resolution Multiplier %d\n", m);
10598c2ecf20Sopenharmony_ci		m = 1;
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	return m;
10638c2ecf20Sopenharmony_ci}
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_cistatic void hid_apply_multiplier_to_field(struct hid_device *hid,
10668c2ecf20Sopenharmony_ci					  struct hid_field *field,
10678c2ecf20Sopenharmony_ci					  struct hid_collection *multiplier_collection,
10688c2ecf20Sopenharmony_ci					  int effective_multiplier)
10698c2ecf20Sopenharmony_ci{
10708c2ecf20Sopenharmony_ci	struct hid_collection *collection;
10718c2ecf20Sopenharmony_ci	struct hid_usage *usage;
10728c2ecf20Sopenharmony_ci	int i;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	/*
10758c2ecf20Sopenharmony_ci	 * If multiplier_collection is NULL, the multiplier applies
10768c2ecf20Sopenharmony_ci	 * to all fields in the report.
10778c2ecf20Sopenharmony_ci	 * Otherwise, it is the Logical Collection the multiplier applies to
10788c2ecf20Sopenharmony_ci	 * but our field may be in a subcollection of that collection.
10798c2ecf20Sopenharmony_ci	 */
10808c2ecf20Sopenharmony_ci	for (i = 0; i < field->maxusage; i++) {
10818c2ecf20Sopenharmony_ci		usage = &field->usage[i];
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci		collection = &hid->collection[usage->collection_index];
10848c2ecf20Sopenharmony_ci		while (collection->parent_idx != -1 &&
10858c2ecf20Sopenharmony_ci		       collection != multiplier_collection)
10868c2ecf20Sopenharmony_ci			collection = &hid->collection[collection->parent_idx];
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ci		if (collection->parent_idx != -1 ||
10898c2ecf20Sopenharmony_ci		    multiplier_collection == NULL)
10908c2ecf20Sopenharmony_ci			usage->resolution_multiplier = effective_multiplier;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	}
10938c2ecf20Sopenharmony_ci}
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_cistatic void hid_apply_multiplier(struct hid_device *hid,
10968c2ecf20Sopenharmony_ci				 struct hid_field *multiplier)
10978c2ecf20Sopenharmony_ci{
10988c2ecf20Sopenharmony_ci	struct hid_report_enum *rep_enum;
10998c2ecf20Sopenharmony_ci	struct hid_report *rep;
11008c2ecf20Sopenharmony_ci	struct hid_field *field;
11018c2ecf20Sopenharmony_ci	struct hid_collection *multiplier_collection;
11028c2ecf20Sopenharmony_ci	int effective_multiplier;
11038c2ecf20Sopenharmony_ci	int i;
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	/*
11068c2ecf20Sopenharmony_ci	 * "The Resolution Multiplier control must be contained in the same
11078c2ecf20Sopenharmony_ci	 * Logical Collection as the control(s) to which it is to be applied.
11088c2ecf20Sopenharmony_ci	 * If no Resolution Multiplier is defined, then the Resolution
11098c2ecf20Sopenharmony_ci	 * Multiplier defaults to 1.  If more than one control exists in a
11108c2ecf20Sopenharmony_ci	 * Logical Collection, the Resolution Multiplier is associated with
11118c2ecf20Sopenharmony_ci	 * all controls in the collection. If no Logical Collection is
11128c2ecf20Sopenharmony_ci	 * defined, the Resolution Multiplier is associated with all
11138c2ecf20Sopenharmony_ci	 * controls in the report."
11148c2ecf20Sopenharmony_ci	 * HID Usage Table, v1.12, Section 4.3.1, p30
11158c2ecf20Sopenharmony_ci	 *
11168c2ecf20Sopenharmony_ci	 * Thus, search from the current collection upwards until we find a
11178c2ecf20Sopenharmony_ci	 * logical collection. Then search all fields for that same parent
11188c2ecf20Sopenharmony_ci	 * collection. Those are the fields the multiplier applies to.
11198c2ecf20Sopenharmony_ci	 *
11208c2ecf20Sopenharmony_ci	 * If we have more than one multiplier, it will overwrite the
11218c2ecf20Sopenharmony_ci	 * applicable fields later.
11228c2ecf20Sopenharmony_ci	 */
11238c2ecf20Sopenharmony_ci	multiplier_collection = &hid->collection[multiplier->usage->collection_index];
11248c2ecf20Sopenharmony_ci	while (multiplier_collection->parent_idx != -1 &&
11258c2ecf20Sopenharmony_ci	       multiplier_collection->type != HID_COLLECTION_LOGICAL)
11268c2ecf20Sopenharmony_ci		multiplier_collection = &hid->collection[multiplier_collection->parent_idx];
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	effective_multiplier = hid_calculate_multiplier(hid, multiplier);
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	rep_enum = &hid->report_enum[HID_INPUT_REPORT];
11318c2ecf20Sopenharmony_ci	list_for_each_entry(rep, &rep_enum->report_list, list) {
11328c2ecf20Sopenharmony_ci		for (i = 0; i < rep->maxfield; i++) {
11338c2ecf20Sopenharmony_ci			field = rep->field[i];
11348c2ecf20Sopenharmony_ci			hid_apply_multiplier_to_field(hid, field,
11358c2ecf20Sopenharmony_ci						      multiplier_collection,
11368c2ecf20Sopenharmony_ci						      effective_multiplier);
11378c2ecf20Sopenharmony_ci		}
11388c2ecf20Sopenharmony_ci	}
11398c2ecf20Sopenharmony_ci}
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci/*
11428c2ecf20Sopenharmony_ci * hid_setup_resolution_multiplier - set up all resolution multipliers
11438c2ecf20Sopenharmony_ci *
11448c2ecf20Sopenharmony_ci * @device: hid device
11458c2ecf20Sopenharmony_ci *
11468c2ecf20Sopenharmony_ci * Search for all Resolution Multiplier Feature Reports and apply their
11478c2ecf20Sopenharmony_ci * value to all matching Input items. This only updates the internal struct
11488c2ecf20Sopenharmony_ci * fields.
11498c2ecf20Sopenharmony_ci *
11508c2ecf20Sopenharmony_ci * The Resolution Multiplier is applied by the hardware. If the multiplier
11518c2ecf20Sopenharmony_ci * is anything other than 1, the hardware will send pre-multiplied events
11528c2ecf20Sopenharmony_ci * so that the same physical interaction generates an accumulated
11538c2ecf20Sopenharmony_ci *	accumulated_value = value * * multiplier
11548c2ecf20Sopenharmony_ci * This may be achieved by sending
11558c2ecf20Sopenharmony_ci * - "value * multiplier" for each event, or
11568c2ecf20Sopenharmony_ci * - "value" but "multiplier" times as frequently, or
11578c2ecf20Sopenharmony_ci * - a combination of the above
11588c2ecf20Sopenharmony_ci * The only guarantee is that the same physical interaction always generates
11598c2ecf20Sopenharmony_ci * an accumulated 'value * multiplier'.
11608c2ecf20Sopenharmony_ci *
11618c2ecf20Sopenharmony_ci * This function must be called before any event processing and after
11628c2ecf20Sopenharmony_ci * any SetRequest to the Resolution Multiplier.
11638c2ecf20Sopenharmony_ci */
11648c2ecf20Sopenharmony_civoid hid_setup_resolution_multiplier(struct hid_device *hid)
11658c2ecf20Sopenharmony_ci{
11668c2ecf20Sopenharmony_ci	struct hid_report_enum *rep_enum;
11678c2ecf20Sopenharmony_ci	struct hid_report *rep;
11688c2ecf20Sopenharmony_ci	struct hid_usage *usage;
11698c2ecf20Sopenharmony_ci	int i, j;
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	rep_enum = &hid->report_enum[HID_FEATURE_REPORT];
11728c2ecf20Sopenharmony_ci	list_for_each_entry(rep, &rep_enum->report_list, list) {
11738c2ecf20Sopenharmony_ci		for (i = 0; i < rep->maxfield; i++) {
11748c2ecf20Sopenharmony_ci			/* Ignore if report count is out of bounds. */
11758c2ecf20Sopenharmony_ci			if (rep->field[i]->report_count < 1)
11768c2ecf20Sopenharmony_ci				continue;
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci			for (j = 0; j < rep->field[i]->maxusage; j++) {
11798c2ecf20Sopenharmony_ci				usage = &rep->field[i]->usage[j];
11808c2ecf20Sopenharmony_ci				if (usage->hid == HID_GD_RESOLUTION_MULTIPLIER)
11818c2ecf20Sopenharmony_ci					hid_apply_multiplier(hid,
11828c2ecf20Sopenharmony_ci							     rep->field[i]);
11838c2ecf20Sopenharmony_ci			}
11848c2ecf20Sopenharmony_ci		}
11858c2ecf20Sopenharmony_ci	}
11868c2ecf20Sopenharmony_ci}
11878c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_setup_resolution_multiplier);
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci/**
11908c2ecf20Sopenharmony_ci * hid_open_report - open a driver-specific device report
11918c2ecf20Sopenharmony_ci *
11928c2ecf20Sopenharmony_ci * @device: hid device
11938c2ecf20Sopenharmony_ci *
11948c2ecf20Sopenharmony_ci * Parse a report description into a hid_device structure. Reports are
11958c2ecf20Sopenharmony_ci * enumerated, fields are attached to these reports.
11968c2ecf20Sopenharmony_ci * 0 returned on success, otherwise nonzero error value.
11978c2ecf20Sopenharmony_ci *
11988c2ecf20Sopenharmony_ci * This function (or the equivalent hid_parse() macro) should only be
11998c2ecf20Sopenharmony_ci * called from probe() in drivers, before starting the device.
12008c2ecf20Sopenharmony_ci */
12018c2ecf20Sopenharmony_ciint hid_open_report(struct hid_device *device)
12028c2ecf20Sopenharmony_ci{
12038c2ecf20Sopenharmony_ci	struct hid_parser *parser;
12048c2ecf20Sopenharmony_ci	struct hid_item item;
12058c2ecf20Sopenharmony_ci	unsigned int size;
12068c2ecf20Sopenharmony_ci	__u8 *start;
12078c2ecf20Sopenharmony_ci	__u8 *buf;
12088c2ecf20Sopenharmony_ci	__u8 *end;
12098c2ecf20Sopenharmony_ci	__u8 *next;
12108c2ecf20Sopenharmony_ci	int ret;
12118c2ecf20Sopenharmony_ci	int i;
12128c2ecf20Sopenharmony_ci	static int (*dispatch_type[])(struct hid_parser *parser,
12138c2ecf20Sopenharmony_ci				      struct hid_item *item) = {
12148c2ecf20Sopenharmony_ci		hid_parser_main,
12158c2ecf20Sopenharmony_ci		hid_parser_global,
12168c2ecf20Sopenharmony_ci		hid_parser_local,
12178c2ecf20Sopenharmony_ci		hid_parser_reserved
12188c2ecf20Sopenharmony_ci	};
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	if (WARN_ON(device->status & HID_STAT_PARSED))
12218c2ecf20Sopenharmony_ci		return -EBUSY;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	start = device->dev_rdesc;
12248c2ecf20Sopenharmony_ci	if (WARN_ON(!start))
12258c2ecf20Sopenharmony_ci		return -ENODEV;
12268c2ecf20Sopenharmony_ci	size = device->dev_rsize;
12278c2ecf20Sopenharmony_ci
12288c2ecf20Sopenharmony_ci	buf = kmemdup(start, size, GFP_KERNEL);
12298c2ecf20Sopenharmony_ci	if (buf == NULL)
12308c2ecf20Sopenharmony_ci		return -ENOMEM;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	if (device->driver->report_fixup)
12338c2ecf20Sopenharmony_ci		start = device->driver->report_fixup(device, buf, &size);
12348c2ecf20Sopenharmony_ci	else
12358c2ecf20Sopenharmony_ci		start = buf;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	start = kmemdup(start, size, GFP_KERNEL);
12388c2ecf20Sopenharmony_ci	kfree(buf);
12398c2ecf20Sopenharmony_ci	if (start == NULL)
12408c2ecf20Sopenharmony_ci		return -ENOMEM;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	device->rdesc = start;
12438c2ecf20Sopenharmony_ci	device->rsize = size;
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	parser = vzalloc(sizeof(struct hid_parser));
12468c2ecf20Sopenharmony_ci	if (!parser) {
12478c2ecf20Sopenharmony_ci		ret = -ENOMEM;
12488c2ecf20Sopenharmony_ci		goto alloc_err;
12498c2ecf20Sopenharmony_ci	}
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	parser->device = device;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	end = start + size;
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	device->collection = kcalloc(HID_DEFAULT_NUM_COLLECTIONS,
12568c2ecf20Sopenharmony_ci				     sizeof(struct hid_collection), GFP_KERNEL);
12578c2ecf20Sopenharmony_ci	if (!device->collection) {
12588c2ecf20Sopenharmony_ci		ret = -ENOMEM;
12598c2ecf20Sopenharmony_ci		goto err;
12608c2ecf20Sopenharmony_ci	}
12618c2ecf20Sopenharmony_ci	device->collection_size = HID_DEFAULT_NUM_COLLECTIONS;
12628c2ecf20Sopenharmony_ci	for (i = 0; i < HID_DEFAULT_NUM_COLLECTIONS; i++)
12638c2ecf20Sopenharmony_ci		device->collection[i].parent_idx = -1;
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	ret = -EINVAL;
12668c2ecf20Sopenharmony_ci	while ((next = fetch_item(start, end, &item)) != NULL) {
12678c2ecf20Sopenharmony_ci		start = next;
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci		if (item.format != HID_ITEM_FORMAT_SHORT) {
12708c2ecf20Sopenharmony_ci			hid_err(device, "unexpected long global item\n");
12718c2ecf20Sopenharmony_ci			goto err;
12728c2ecf20Sopenharmony_ci		}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci		if (dispatch_type[item.type](parser, &item)) {
12758c2ecf20Sopenharmony_ci			hid_err(device, "item %u %u %u %u parsing failed\n",
12768c2ecf20Sopenharmony_ci				item.format, (unsigned)item.size,
12778c2ecf20Sopenharmony_ci				(unsigned)item.type, (unsigned)item.tag);
12788c2ecf20Sopenharmony_ci			goto err;
12798c2ecf20Sopenharmony_ci		}
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_ci		if (start == end) {
12828c2ecf20Sopenharmony_ci			if (parser->collection_stack_ptr) {
12838c2ecf20Sopenharmony_ci				hid_err(device, "unbalanced collection at end of report description\n");
12848c2ecf20Sopenharmony_ci				goto err;
12858c2ecf20Sopenharmony_ci			}
12868c2ecf20Sopenharmony_ci			if (parser->local.delimiter_depth) {
12878c2ecf20Sopenharmony_ci				hid_err(device, "unbalanced delimiter at end of report description\n");
12888c2ecf20Sopenharmony_ci				goto err;
12898c2ecf20Sopenharmony_ci			}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci			/*
12928c2ecf20Sopenharmony_ci			 * fetch initial values in case the device's
12938c2ecf20Sopenharmony_ci			 * default multiplier isn't the recommended 1
12948c2ecf20Sopenharmony_ci			 */
12958c2ecf20Sopenharmony_ci			hid_setup_resolution_multiplier(device);
12968c2ecf20Sopenharmony_ci
12978c2ecf20Sopenharmony_ci			kfree(parser->collection_stack);
12988c2ecf20Sopenharmony_ci			vfree(parser);
12998c2ecf20Sopenharmony_ci			device->status |= HID_STAT_PARSED;
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci			return 0;
13028c2ecf20Sopenharmony_ci		}
13038c2ecf20Sopenharmony_ci	}
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	hid_err(device, "item fetching failed at offset %u/%u\n",
13068c2ecf20Sopenharmony_ci		size - (unsigned int)(end - start), size);
13078c2ecf20Sopenharmony_cierr:
13088c2ecf20Sopenharmony_ci	kfree(parser->collection_stack);
13098c2ecf20Sopenharmony_cialloc_err:
13108c2ecf20Sopenharmony_ci	vfree(parser);
13118c2ecf20Sopenharmony_ci	hid_close_report(device);
13128c2ecf20Sopenharmony_ci	return ret;
13138c2ecf20Sopenharmony_ci}
13148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_open_report);
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_ci/*
13178c2ecf20Sopenharmony_ci * Convert a signed n-bit integer to signed 32-bit integer. Common
13188c2ecf20Sopenharmony_ci * cases are done through the compiler, the screwed things has to be
13198c2ecf20Sopenharmony_ci * done by hand.
13208c2ecf20Sopenharmony_ci */
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_cistatic s32 snto32(__u32 value, unsigned n)
13238c2ecf20Sopenharmony_ci{
13248c2ecf20Sopenharmony_ci	if (!value || !n)
13258c2ecf20Sopenharmony_ci		return 0;
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	if (n > 32)
13288c2ecf20Sopenharmony_ci		n = 32;
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	switch (n) {
13318c2ecf20Sopenharmony_ci	case 8:  return ((__s8)value);
13328c2ecf20Sopenharmony_ci	case 16: return ((__s16)value);
13338c2ecf20Sopenharmony_ci	case 32: return ((__s32)value);
13348c2ecf20Sopenharmony_ci	}
13358c2ecf20Sopenharmony_ci	return value & (1 << (n - 1)) ? value | (~0U << n) : value;
13368c2ecf20Sopenharmony_ci}
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_cis32 hid_snto32(__u32 value, unsigned n)
13398c2ecf20Sopenharmony_ci{
13408c2ecf20Sopenharmony_ci	return snto32(value, n);
13418c2ecf20Sopenharmony_ci}
13428c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_snto32);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci/*
13458c2ecf20Sopenharmony_ci * Convert a signed 32-bit integer to a signed n-bit integer.
13468c2ecf20Sopenharmony_ci */
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_cistatic u32 s32ton(__s32 value, unsigned n)
13498c2ecf20Sopenharmony_ci{
13508c2ecf20Sopenharmony_ci	s32 a = value >> (n - 1);
13518c2ecf20Sopenharmony_ci	if (a && a != -1)
13528c2ecf20Sopenharmony_ci		return value < 0 ? 1 << (n - 1) : (1 << (n - 1)) - 1;
13538c2ecf20Sopenharmony_ci	return value & ((1 << n) - 1);
13548c2ecf20Sopenharmony_ci}
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci/*
13578c2ecf20Sopenharmony_ci * Extract/implement a data field from/to a little endian report (bit array).
13588c2ecf20Sopenharmony_ci *
13598c2ecf20Sopenharmony_ci * Code sort-of follows HID spec:
13608c2ecf20Sopenharmony_ci *     http://www.usb.org/developers/hidpage/HID1_11.pdf
13618c2ecf20Sopenharmony_ci *
13628c2ecf20Sopenharmony_ci * While the USB HID spec allows unlimited length bit fields in "report
13638c2ecf20Sopenharmony_ci * descriptors", most devices never use more than 16 bits.
13648c2ecf20Sopenharmony_ci * One model of UPS is claimed to report "LINEV" as a 32-bit field.
13658c2ecf20Sopenharmony_ci * Search linux-kernel and linux-usb-devel archives for "hid-core extract".
13668c2ecf20Sopenharmony_ci */
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_cistatic u32 __extract(u8 *report, unsigned offset, int n)
13698c2ecf20Sopenharmony_ci{
13708c2ecf20Sopenharmony_ci	unsigned int idx = offset / 8;
13718c2ecf20Sopenharmony_ci	unsigned int bit_nr = 0;
13728c2ecf20Sopenharmony_ci	unsigned int bit_shift = offset % 8;
13738c2ecf20Sopenharmony_ci	int bits_to_copy = 8 - bit_shift;
13748c2ecf20Sopenharmony_ci	u32 value = 0;
13758c2ecf20Sopenharmony_ci	u32 mask = n < 32 ? (1U << n) - 1 : ~0U;
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	while (n > 0) {
13788c2ecf20Sopenharmony_ci		value |= ((u32)report[idx] >> bit_shift) << bit_nr;
13798c2ecf20Sopenharmony_ci		n -= bits_to_copy;
13808c2ecf20Sopenharmony_ci		bit_nr += bits_to_copy;
13818c2ecf20Sopenharmony_ci		bits_to_copy = 8;
13828c2ecf20Sopenharmony_ci		bit_shift = 0;
13838c2ecf20Sopenharmony_ci		idx++;
13848c2ecf20Sopenharmony_ci	}
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	return value & mask;
13878c2ecf20Sopenharmony_ci}
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ciu32 hid_field_extract(const struct hid_device *hid, u8 *report,
13908c2ecf20Sopenharmony_ci			unsigned offset, unsigned n)
13918c2ecf20Sopenharmony_ci{
13928c2ecf20Sopenharmony_ci	if (n > 32) {
13938c2ecf20Sopenharmony_ci		hid_warn_once(hid, "%s() called with n (%d) > 32! (%s)\n",
13948c2ecf20Sopenharmony_ci			      __func__, n, current->comm);
13958c2ecf20Sopenharmony_ci		n = 32;
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci	return __extract(report, offset, n);
13998c2ecf20Sopenharmony_ci}
14008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_field_extract);
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci/*
14038c2ecf20Sopenharmony_ci * "implement" : set bits in a little endian bit stream.
14048c2ecf20Sopenharmony_ci * Same concepts as "extract" (see comments above).
14058c2ecf20Sopenharmony_ci * The data mangled in the bit stream remains in little endian
14068c2ecf20Sopenharmony_ci * order the whole time. It make more sense to talk about
14078c2ecf20Sopenharmony_ci * endianness of register values by considering a register
14088c2ecf20Sopenharmony_ci * a "cached" copy of the little endian bit stream.
14098c2ecf20Sopenharmony_ci */
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_cistatic void __implement(u8 *report, unsigned offset, int n, u32 value)
14128c2ecf20Sopenharmony_ci{
14138c2ecf20Sopenharmony_ci	unsigned int idx = offset / 8;
14148c2ecf20Sopenharmony_ci	unsigned int bit_shift = offset % 8;
14158c2ecf20Sopenharmony_ci	int bits_to_set = 8 - bit_shift;
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	while (n - bits_to_set >= 0) {
14188c2ecf20Sopenharmony_ci		report[idx] &= ~(0xff << bit_shift);
14198c2ecf20Sopenharmony_ci		report[idx] |= value << bit_shift;
14208c2ecf20Sopenharmony_ci		value >>= bits_to_set;
14218c2ecf20Sopenharmony_ci		n -= bits_to_set;
14228c2ecf20Sopenharmony_ci		bits_to_set = 8;
14238c2ecf20Sopenharmony_ci		bit_shift = 0;
14248c2ecf20Sopenharmony_ci		idx++;
14258c2ecf20Sopenharmony_ci	}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	/* last nibble */
14288c2ecf20Sopenharmony_ci	if (n) {
14298c2ecf20Sopenharmony_ci		u8 bit_mask = ((1U << n) - 1);
14308c2ecf20Sopenharmony_ci		report[idx] &= ~(bit_mask << bit_shift);
14318c2ecf20Sopenharmony_ci		report[idx] |= value << bit_shift;
14328c2ecf20Sopenharmony_ci	}
14338c2ecf20Sopenharmony_ci}
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_cistatic void implement(const struct hid_device *hid, u8 *report,
14368c2ecf20Sopenharmony_ci		      unsigned offset, unsigned n, u32 value)
14378c2ecf20Sopenharmony_ci{
14388c2ecf20Sopenharmony_ci	if (unlikely(n > 32)) {
14398c2ecf20Sopenharmony_ci		hid_warn(hid, "%s() called with n (%d) > 32! (%s)\n",
14408c2ecf20Sopenharmony_ci			 __func__, n, current->comm);
14418c2ecf20Sopenharmony_ci		n = 32;
14428c2ecf20Sopenharmony_ci	} else if (n < 32) {
14438c2ecf20Sopenharmony_ci		u32 m = (1U << n) - 1;
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci		if (unlikely(value > m)) {
14468c2ecf20Sopenharmony_ci			hid_warn(hid,
14478c2ecf20Sopenharmony_ci				 "%s() called with too large value %d (n: %d)! (%s)\n",
14488c2ecf20Sopenharmony_ci				 __func__, value, n, current->comm);
14498c2ecf20Sopenharmony_ci			value &= m;
14508c2ecf20Sopenharmony_ci		}
14518c2ecf20Sopenharmony_ci	}
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	__implement(report, offset, n, value);
14548c2ecf20Sopenharmony_ci}
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci/*
14578c2ecf20Sopenharmony_ci * Search an array for a value.
14588c2ecf20Sopenharmony_ci */
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_cistatic int search(__s32 *array, __s32 value, unsigned n)
14618c2ecf20Sopenharmony_ci{
14628c2ecf20Sopenharmony_ci	while (n--) {
14638c2ecf20Sopenharmony_ci		if (*array++ == value)
14648c2ecf20Sopenharmony_ci			return 0;
14658c2ecf20Sopenharmony_ci	}
14668c2ecf20Sopenharmony_ci	return -1;
14678c2ecf20Sopenharmony_ci}
14688c2ecf20Sopenharmony_ci
14698c2ecf20Sopenharmony_ci/**
14708c2ecf20Sopenharmony_ci * hid_match_report - check if driver's raw_event should be called
14718c2ecf20Sopenharmony_ci *
14728c2ecf20Sopenharmony_ci * @hid: hid device
14738c2ecf20Sopenharmony_ci * @report: hid report to match against
14748c2ecf20Sopenharmony_ci *
14758c2ecf20Sopenharmony_ci * compare hid->driver->report_table->report_type to report->type
14768c2ecf20Sopenharmony_ci */
14778c2ecf20Sopenharmony_cistatic int hid_match_report(struct hid_device *hid, struct hid_report *report)
14788c2ecf20Sopenharmony_ci{
14798c2ecf20Sopenharmony_ci	const struct hid_report_id *id = hid->driver->report_table;
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	if (!id) /* NULL means all */
14828c2ecf20Sopenharmony_ci		return 1;
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	for (; id->report_type != HID_TERMINATOR; id++)
14858c2ecf20Sopenharmony_ci		if (id->report_type == HID_ANY_ID ||
14868c2ecf20Sopenharmony_ci				id->report_type == report->type)
14878c2ecf20Sopenharmony_ci			return 1;
14888c2ecf20Sopenharmony_ci	return 0;
14898c2ecf20Sopenharmony_ci}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci/**
14928c2ecf20Sopenharmony_ci * hid_match_usage - check if driver's event should be called
14938c2ecf20Sopenharmony_ci *
14948c2ecf20Sopenharmony_ci * @hid: hid device
14958c2ecf20Sopenharmony_ci * @usage: usage to match against
14968c2ecf20Sopenharmony_ci *
14978c2ecf20Sopenharmony_ci * compare hid->driver->usage_table->usage_{type,code} to
14988c2ecf20Sopenharmony_ci * usage->usage_{type,code}
14998c2ecf20Sopenharmony_ci */
15008c2ecf20Sopenharmony_cistatic int hid_match_usage(struct hid_device *hid, struct hid_usage *usage)
15018c2ecf20Sopenharmony_ci{
15028c2ecf20Sopenharmony_ci	const struct hid_usage_id *id = hid->driver->usage_table;
15038c2ecf20Sopenharmony_ci
15048c2ecf20Sopenharmony_ci	if (!id) /* NULL means all */
15058c2ecf20Sopenharmony_ci		return 1;
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci	for (; id->usage_type != HID_ANY_ID - 1; id++)
15088c2ecf20Sopenharmony_ci		if ((id->usage_hid == HID_ANY_ID ||
15098c2ecf20Sopenharmony_ci				id->usage_hid == usage->hid) &&
15108c2ecf20Sopenharmony_ci				(id->usage_type == HID_ANY_ID ||
15118c2ecf20Sopenharmony_ci				id->usage_type == usage->type) &&
15128c2ecf20Sopenharmony_ci				(id->usage_code == HID_ANY_ID ||
15138c2ecf20Sopenharmony_ci				 id->usage_code == usage->code))
15148c2ecf20Sopenharmony_ci			return 1;
15158c2ecf20Sopenharmony_ci	return 0;
15168c2ecf20Sopenharmony_ci}
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_cistatic void hid_process_event(struct hid_device *hid, struct hid_field *field,
15198c2ecf20Sopenharmony_ci		struct hid_usage *usage, __s32 value, int interrupt)
15208c2ecf20Sopenharmony_ci{
15218c2ecf20Sopenharmony_ci	struct hid_driver *hdrv = hid->driver;
15228c2ecf20Sopenharmony_ci	int ret;
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	if (!list_empty(&hid->debug_list))
15258c2ecf20Sopenharmony_ci		hid_dump_input(hid, usage, value);
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci	if (hdrv && hdrv->event && hid_match_usage(hid, usage)) {
15288c2ecf20Sopenharmony_ci		ret = hdrv->event(hid, field, usage, value);
15298c2ecf20Sopenharmony_ci		if (ret != 0) {
15308c2ecf20Sopenharmony_ci			if (ret < 0)
15318c2ecf20Sopenharmony_ci				hid_err(hid, "%s's event failed with %d\n",
15328c2ecf20Sopenharmony_ci						hdrv->name, ret);
15338c2ecf20Sopenharmony_ci			return;
15348c2ecf20Sopenharmony_ci		}
15358c2ecf20Sopenharmony_ci	}
15368c2ecf20Sopenharmony_ci
15378c2ecf20Sopenharmony_ci	if (hid->claimed & HID_CLAIMED_INPUT)
15388c2ecf20Sopenharmony_ci		hidinput_hid_event(hid, field, usage, value);
15398c2ecf20Sopenharmony_ci	if (hid->claimed & HID_CLAIMED_HIDDEV && interrupt && hid->hiddev_hid_event)
15408c2ecf20Sopenharmony_ci		hid->hiddev_hid_event(hid, field, usage, value);
15418c2ecf20Sopenharmony_ci}
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci/*
15448c2ecf20Sopenharmony_ci * Analyse a received field, and fetch the data from it. The field
15458c2ecf20Sopenharmony_ci * content is stored for next report processing (we do differential
15468c2ecf20Sopenharmony_ci * reporting to the layer).
15478c2ecf20Sopenharmony_ci */
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_cistatic void hid_input_field(struct hid_device *hid, struct hid_field *field,
15508c2ecf20Sopenharmony_ci			    __u8 *data, int interrupt)
15518c2ecf20Sopenharmony_ci{
15528c2ecf20Sopenharmony_ci	unsigned n;
15538c2ecf20Sopenharmony_ci	unsigned count = field->report_count;
15548c2ecf20Sopenharmony_ci	unsigned offset = field->report_offset;
15558c2ecf20Sopenharmony_ci	unsigned size = field->report_size;
15568c2ecf20Sopenharmony_ci	__s32 min = field->logical_minimum;
15578c2ecf20Sopenharmony_ci	__s32 max = field->logical_maximum;
15588c2ecf20Sopenharmony_ci	__s32 *value;
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	value = kmalloc_array(count, sizeof(__s32), GFP_ATOMIC);
15618c2ecf20Sopenharmony_ci	if (!value)
15628c2ecf20Sopenharmony_ci		return;
15638c2ecf20Sopenharmony_ci
15648c2ecf20Sopenharmony_ci	for (n = 0; n < count; n++) {
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci		value[n] = min < 0 ?
15678c2ecf20Sopenharmony_ci			snto32(hid_field_extract(hid, data, offset + n * size,
15688c2ecf20Sopenharmony_ci			       size), size) :
15698c2ecf20Sopenharmony_ci			hid_field_extract(hid, data, offset + n * size, size);
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci		/* Ignore report if ErrorRollOver */
15728c2ecf20Sopenharmony_ci		if (!(field->flags & HID_MAIN_ITEM_VARIABLE) &&
15738c2ecf20Sopenharmony_ci		    value[n] >= min && value[n] <= max &&
15748c2ecf20Sopenharmony_ci		    value[n] - min < field->maxusage &&
15758c2ecf20Sopenharmony_ci		    field->usage[value[n] - min].hid == HID_UP_KEYBOARD + 1)
15768c2ecf20Sopenharmony_ci			goto exit;
15778c2ecf20Sopenharmony_ci	}
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_ci	for (n = 0; n < count; n++) {
15808c2ecf20Sopenharmony_ci
15818c2ecf20Sopenharmony_ci		if (HID_MAIN_ITEM_VARIABLE & field->flags) {
15828c2ecf20Sopenharmony_ci			hid_process_event(hid, field, &field->usage[n], value[n], interrupt);
15838c2ecf20Sopenharmony_ci			continue;
15848c2ecf20Sopenharmony_ci		}
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_ci		if (field->value[n] >= min && field->value[n] <= max
15878c2ecf20Sopenharmony_ci			&& field->value[n] - min < field->maxusage
15888c2ecf20Sopenharmony_ci			&& field->usage[field->value[n] - min].hid
15898c2ecf20Sopenharmony_ci			&& search(value, field->value[n], count))
15908c2ecf20Sopenharmony_ci				hid_process_event(hid, field, &field->usage[field->value[n] - min], 0, interrupt);
15918c2ecf20Sopenharmony_ci
15928c2ecf20Sopenharmony_ci		if (value[n] >= min && value[n] <= max
15938c2ecf20Sopenharmony_ci			&& value[n] - min < field->maxusage
15948c2ecf20Sopenharmony_ci			&& field->usage[value[n] - min].hid
15958c2ecf20Sopenharmony_ci			&& search(field->value, value[n], count))
15968c2ecf20Sopenharmony_ci				hid_process_event(hid, field, &field->usage[value[n] - min], 1, interrupt);
15978c2ecf20Sopenharmony_ci	}
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	memcpy(field->value, value, count * sizeof(__s32));
16008c2ecf20Sopenharmony_ciexit:
16018c2ecf20Sopenharmony_ci	kfree(value);
16028c2ecf20Sopenharmony_ci}
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci/*
16058c2ecf20Sopenharmony_ci * Output the field into the report.
16068c2ecf20Sopenharmony_ci */
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_cistatic void hid_output_field(const struct hid_device *hid,
16098c2ecf20Sopenharmony_ci			     struct hid_field *field, __u8 *data)
16108c2ecf20Sopenharmony_ci{
16118c2ecf20Sopenharmony_ci	unsigned count = field->report_count;
16128c2ecf20Sopenharmony_ci	unsigned offset = field->report_offset;
16138c2ecf20Sopenharmony_ci	unsigned size = field->report_size;
16148c2ecf20Sopenharmony_ci	unsigned n;
16158c2ecf20Sopenharmony_ci
16168c2ecf20Sopenharmony_ci	for (n = 0; n < count; n++) {
16178c2ecf20Sopenharmony_ci		if (field->logical_minimum < 0)	/* signed values */
16188c2ecf20Sopenharmony_ci			implement(hid, data, offset + n * size, size,
16198c2ecf20Sopenharmony_ci				  s32ton(field->value[n], size));
16208c2ecf20Sopenharmony_ci		else				/* unsigned values */
16218c2ecf20Sopenharmony_ci			implement(hid, data, offset + n * size, size,
16228c2ecf20Sopenharmony_ci				  field->value[n]);
16238c2ecf20Sopenharmony_ci	}
16248c2ecf20Sopenharmony_ci}
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci/*
16278c2ecf20Sopenharmony_ci * Compute the size of a report.
16288c2ecf20Sopenharmony_ci */
16298c2ecf20Sopenharmony_cistatic size_t hid_compute_report_size(struct hid_report *report)
16308c2ecf20Sopenharmony_ci{
16318c2ecf20Sopenharmony_ci	if (report->size)
16328c2ecf20Sopenharmony_ci		return ((report->size - 1) >> 3) + 1;
16338c2ecf20Sopenharmony_ci
16348c2ecf20Sopenharmony_ci	return 0;
16358c2ecf20Sopenharmony_ci}
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci/*
16388c2ecf20Sopenharmony_ci * Create a report. 'data' has to be allocated using
16398c2ecf20Sopenharmony_ci * hid_alloc_report_buf() so that it has proper size.
16408c2ecf20Sopenharmony_ci */
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_civoid hid_output_report(struct hid_report *report, __u8 *data)
16438c2ecf20Sopenharmony_ci{
16448c2ecf20Sopenharmony_ci	unsigned n;
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ci	if (report->id > 0)
16478c2ecf20Sopenharmony_ci		*data++ = report->id;
16488c2ecf20Sopenharmony_ci
16498c2ecf20Sopenharmony_ci	memset(data, 0, hid_compute_report_size(report));
16508c2ecf20Sopenharmony_ci	for (n = 0; n < report->maxfield; n++)
16518c2ecf20Sopenharmony_ci		hid_output_field(report->device, report->field[n], data);
16528c2ecf20Sopenharmony_ci}
16538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_output_report);
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_ci/*
16568c2ecf20Sopenharmony_ci * Allocator for buffer that is going to be passed to hid_output_report()
16578c2ecf20Sopenharmony_ci */
16588c2ecf20Sopenharmony_ciu8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
16598c2ecf20Sopenharmony_ci{
16608c2ecf20Sopenharmony_ci	/*
16618c2ecf20Sopenharmony_ci	 * 7 extra bytes are necessary to achieve proper functionality
16628c2ecf20Sopenharmony_ci	 * of implement() working on 8 byte chunks
16638c2ecf20Sopenharmony_ci	 */
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci	u32 len = hid_report_len(report) + 7;
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci	return kmalloc(len, flags);
16688c2ecf20Sopenharmony_ci}
16698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_alloc_report_buf);
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci/*
16728c2ecf20Sopenharmony_ci * Set a field value. The report this field belongs to has to be
16738c2ecf20Sopenharmony_ci * created and transferred to the device, to set this value in the
16748c2ecf20Sopenharmony_ci * device.
16758c2ecf20Sopenharmony_ci */
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ciint hid_set_field(struct hid_field *field, unsigned offset, __s32 value)
16788c2ecf20Sopenharmony_ci{
16798c2ecf20Sopenharmony_ci	unsigned size;
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	if (!field)
16828c2ecf20Sopenharmony_ci		return -1;
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ci	size = field->report_size;
16858c2ecf20Sopenharmony_ci
16868c2ecf20Sopenharmony_ci	hid_dump_input(field->report->device, field->usage + offset, value);
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	if (offset >= field->report_count) {
16898c2ecf20Sopenharmony_ci		hid_err(field->report->device, "offset (%d) exceeds report_count (%d)\n",
16908c2ecf20Sopenharmony_ci				offset, field->report_count);
16918c2ecf20Sopenharmony_ci		return -1;
16928c2ecf20Sopenharmony_ci	}
16938c2ecf20Sopenharmony_ci	if (field->logical_minimum < 0) {
16948c2ecf20Sopenharmony_ci		if (value != snto32(s32ton(value, size), size)) {
16958c2ecf20Sopenharmony_ci			hid_err(field->report->device, "value %d is out of range\n", value);
16968c2ecf20Sopenharmony_ci			return -1;
16978c2ecf20Sopenharmony_ci		}
16988c2ecf20Sopenharmony_ci	}
16998c2ecf20Sopenharmony_ci	field->value[offset] = value;
17008c2ecf20Sopenharmony_ci	return 0;
17018c2ecf20Sopenharmony_ci}
17028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_set_field);
17038c2ecf20Sopenharmony_ci
17048c2ecf20Sopenharmony_cistatic struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
17058c2ecf20Sopenharmony_ci		const u8 *data)
17068c2ecf20Sopenharmony_ci{
17078c2ecf20Sopenharmony_ci	struct hid_report *report;
17088c2ecf20Sopenharmony_ci	unsigned int n = 0;	/* Normally report number is 0 */
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_ci	/* Device uses numbered reports, data[0] is report number */
17118c2ecf20Sopenharmony_ci	if (report_enum->numbered)
17128c2ecf20Sopenharmony_ci		n = *data;
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci	report = report_enum->report_id_hash[n];
17158c2ecf20Sopenharmony_ci	if (report == NULL)
17168c2ecf20Sopenharmony_ci		dbg_hid("undefined report_id %u received\n", n);
17178c2ecf20Sopenharmony_ci
17188c2ecf20Sopenharmony_ci	return report;
17198c2ecf20Sopenharmony_ci}
17208c2ecf20Sopenharmony_ci
17218c2ecf20Sopenharmony_ci/*
17228c2ecf20Sopenharmony_ci * Implement a generic .request() callback, using .raw_request()
17238c2ecf20Sopenharmony_ci * DO NOT USE in hid drivers directly, but through hid_hw_request instead.
17248c2ecf20Sopenharmony_ci */
17258c2ecf20Sopenharmony_ciint __hid_request(struct hid_device *hid, struct hid_report *report,
17268c2ecf20Sopenharmony_ci		int reqtype)
17278c2ecf20Sopenharmony_ci{
17288c2ecf20Sopenharmony_ci	char *buf;
17298c2ecf20Sopenharmony_ci	int ret;
17308c2ecf20Sopenharmony_ci	u32 len;
17318c2ecf20Sopenharmony_ci
17328c2ecf20Sopenharmony_ci	buf = hid_alloc_report_buf(report, GFP_KERNEL);
17338c2ecf20Sopenharmony_ci	if (!buf)
17348c2ecf20Sopenharmony_ci		return -ENOMEM;
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_ci	len = hid_report_len(report);
17378c2ecf20Sopenharmony_ci
17388c2ecf20Sopenharmony_ci	if (reqtype == HID_REQ_SET_REPORT)
17398c2ecf20Sopenharmony_ci		hid_output_report(report, buf);
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci	ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
17428c2ecf20Sopenharmony_ci					  report->type, reqtype);
17438c2ecf20Sopenharmony_ci	if (ret < 0) {
17448c2ecf20Sopenharmony_ci		dbg_hid("unable to complete request: %d\n", ret);
17458c2ecf20Sopenharmony_ci		goto out;
17468c2ecf20Sopenharmony_ci	}
17478c2ecf20Sopenharmony_ci
17488c2ecf20Sopenharmony_ci	if (reqtype == HID_REQ_GET_REPORT)
17498c2ecf20Sopenharmony_ci		hid_input_report(hid, report->type, buf, ret, 0);
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_ci	ret = 0;
17528c2ecf20Sopenharmony_ci
17538c2ecf20Sopenharmony_ciout:
17548c2ecf20Sopenharmony_ci	kfree(buf);
17558c2ecf20Sopenharmony_ci	return ret;
17568c2ecf20Sopenharmony_ci}
17578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__hid_request);
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_ciint hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
17608c2ecf20Sopenharmony_ci		int interrupt)
17618c2ecf20Sopenharmony_ci{
17628c2ecf20Sopenharmony_ci	struct hid_report_enum *report_enum = hid->report_enum + type;
17638c2ecf20Sopenharmony_ci	struct hid_report *report;
17648c2ecf20Sopenharmony_ci	struct hid_driver *hdrv;
17658c2ecf20Sopenharmony_ci	int max_buffer_size = HID_MAX_BUFFER_SIZE;
17668c2ecf20Sopenharmony_ci	unsigned int a;
17678c2ecf20Sopenharmony_ci	u32 rsize, csize = size;
17688c2ecf20Sopenharmony_ci	u8 *cdata = data;
17698c2ecf20Sopenharmony_ci	int ret = 0;
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci	report = hid_get_report(report_enum, data);
17728c2ecf20Sopenharmony_ci	if (!report)
17738c2ecf20Sopenharmony_ci		goto out;
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci	if (report_enum->numbered) {
17768c2ecf20Sopenharmony_ci		cdata++;
17778c2ecf20Sopenharmony_ci		csize--;
17788c2ecf20Sopenharmony_ci	}
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_ci	rsize = hid_compute_report_size(report);
17818c2ecf20Sopenharmony_ci
17828c2ecf20Sopenharmony_ci	if (hid->ll_driver->max_buffer_size)
17838c2ecf20Sopenharmony_ci		max_buffer_size = hid->ll_driver->max_buffer_size;
17848c2ecf20Sopenharmony_ci
17858c2ecf20Sopenharmony_ci	if (report_enum->numbered && rsize >= max_buffer_size)
17868c2ecf20Sopenharmony_ci		rsize = max_buffer_size - 1;
17878c2ecf20Sopenharmony_ci	else if (rsize > max_buffer_size)
17888c2ecf20Sopenharmony_ci		rsize = max_buffer_size;
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_ci	if (csize < rsize) {
17918c2ecf20Sopenharmony_ci		dbg_hid("report %d is too short, (%d < %d)\n", report->id,
17928c2ecf20Sopenharmony_ci				csize, rsize);
17938c2ecf20Sopenharmony_ci		memset(cdata + csize, 0, rsize - csize);
17948c2ecf20Sopenharmony_ci	}
17958c2ecf20Sopenharmony_ci
17968c2ecf20Sopenharmony_ci	if ((hid->claimed & HID_CLAIMED_HIDDEV) && hid->hiddev_report_event)
17978c2ecf20Sopenharmony_ci		hid->hiddev_report_event(hid, report);
17988c2ecf20Sopenharmony_ci	if (hid->claimed & HID_CLAIMED_HIDRAW) {
17998c2ecf20Sopenharmony_ci		ret = hidraw_report_event(hid, data, size);
18008c2ecf20Sopenharmony_ci		if (ret)
18018c2ecf20Sopenharmony_ci			goto out;
18028c2ecf20Sopenharmony_ci	}
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci	if (hid->claimed != HID_CLAIMED_HIDRAW && report->maxfield) {
18058c2ecf20Sopenharmony_ci		for (a = 0; a < report->maxfield; a++)
18068c2ecf20Sopenharmony_ci			hid_input_field(hid, report->field[a], cdata, interrupt);
18078c2ecf20Sopenharmony_ci		hdrv = hid->driver;
18088c2ecf20Sopenharmony_ci		if (hdrv && hdrv->report)
18098c2ecf20Sopenharmony_ci			hdrv->report(hid, report);
18108c2ecf20Sopenharmony_ci	}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci	if (hid->claimed & HID_CLAIMED_INPUT)
18138c2ecf20Sopenharmony_ci		hidinput_report_event(hid, report);
18148c2ecf20Sopenharmony_ciout:
18158c2ecf20Sopenharmony_ci	return ret;
18168c2ecf20Sopenharmony_ci}
18178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_report_raw_event);
18188c2ecf20Sopenharmony_ci
18198c2ecf20Sopenharmony_ci/**
18208c2ecf20Sopenharmony_ci * hid_input_report - report data from lower layer (usb, bt...)
18218c2ecf20Sopenharmony_ci *
18228c2ecf20Sopenharmony_ci * @hid: hid device
18238c2ecf20Sopenharmony_ci * @type: HID report type (HID_*_REPORT)
18248c2ecf20Sopenharmony_ci * @data: report contents
18258c2ecf20Sopenharmony_ci * @size: size of data parameter
18268c2ecf20Sopenharmony_ci * @interrupt: distinguish between interrupt and control transfers
18278c2ecf20Sopenharmony_ci *
18288c2ecf20Sopenharmony_ci * This is data entry for lower layers.
18298c2ecf20Sopenharmony_ci */
18308c2ecf20Sopenharmony_ciint hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int interrupt)
18318c2ecf20Sopenharmony_ci{
18328c2ecf20Sopenharmony_ci	struct hid_report_enum *report_enum;
18338c2ecf20Sopenharmony_ci	struct hid_driver *hdrv;
18348c2ecf20Sopenharmony_ci	struct hid_report *report;
18358c2ecf20Sopenharmony_ci	int ret = 0;
18368c2ecf20Sopenharmony_ci
18378c2ecf20Sopenharmony_ci	if (!hid)
18388c2ecf20Sopenharmony_ci		return -ENODEV;
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci	if (down_trylock(&hid->driver_input_lock))
18418c2ecf20Sopenharmony_ci		return -EBUSY;
18428c2ecf20Sopenharmony_ci
18438c2ecf20Sopenharmony_ci	if (!hid->driver) {
18448c2ecf20Sopenharmony_ci		ret = -ENODEV;
18458c2ecf20Sopenharmony_ci		goto unlock;
18468c2ecf20Sopenharmony_ci	}
18478c2ecf20Sopenharmony_ci	report_enum = hid->report_enum + type;
18488c2ecf20Sopenharmony_ci	hdrv = hid->driver;
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_ci	if (!size) {
18518c2ecf20Sopenharmony_ci		dbg_hid("empty report\n");
18528c2ecf20Sopenharmony_ci		ret = -1;
18538c2ecf20Sopenharmony_ci		goto unlock;
18548c2ecf20Sopenharmony_ci	}
18558c2ecf20Sopenharmony_ci
18568c2ecf20Sopenharmony_ci	/* Avoid unnecessary overhead if debugfs is disabled */
18578c2ecf20Sopenharmony_ci	if (!list_empty(&hid->debug_list))
18588c2ecf20Sopenharmony_ci		hid_dump_report(hid, type, data, size);
18598c2ecf20Sopenharmony_ci
18608c2ecf20Sopenharmony_ci	report = hid_get_report(report_enum, data);
18618c2ecf20Sopenharmony_ci
18628c2ecf20Sopenharmony_ci	if (!report) {
18638c2ecf20Sopenharmony_ci		ret = -1;
18648c2ecf20Sopenharmony_ci		goto unlock;
18658c2ecf20Sopenharmony_ci	}
18668c2ecf20Sopenharmony_ci
18678c2ecf20Sopenharmony_ci	if (hdrv && hdrv->raw_event && hid_match_report(hid, report)) {
18688c2ecf20Sopenharmony_ci		ret = hdrv->raw_event(hid, report, data, size);
18698c2ecf20Sopenharmony_ci		if (ret < 0)
18708c2ecf20Sopenharmony_ci			goto unlock;
18718c2ecf20Sopenharmony_ci	}
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci	ret = hid_report_raw_event(hid, type, data, size, interrupt);
18748c2ecf20Sopenharmony_ci
18758c2ecf20Sopenharmony_ciunlock:
18768c2ecf20Sopenharmony_ci	up(&hid->driver_input_lock);
18778c2ecf20Sopenharmony_ci	return ret;
18788c2ecf20Sopenharmony_ci}
18798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_input_report);
18808c2ecf20Sopenharmony_ci
18818c2ecf20Sopenharmony_cibool hid_match_one_id(const struct hid_device *hdev,
18828c2ecf20Sopenharmony_ci		      const struct hid_device_id *id)
18838c2ecf20Sopenharmony_ci{
18848c2ecf20Sopenharmony_ci	return (id->bus == HID_BUS_ANY || id->bus == hdev->bus) &&
18858c2ecf20Sopenharmony_ci		(id->group == HID_GROUP_ANY || id->group == hdev->group) &&
18868c2ecf20Sopenharmony_ci		(id->vendor == HID_ANY_ID || id->vendor == hdev->vendor) &&
18878c2ecf20Sopenharmony_ci		(id->product == HID_ANY_ID || id->product == hdev->product);
18888c2ecf20Sopenharmony_ci}
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ciconst struct hid_device_id *hid_match_id(const struct hid_device *hdev,
18918c2ecf20Sopenharmony_ci		const struct hid_device_id *id)
18928c2ecf20Sopenharmony_ci{
18938c2ecf20Sopenharmony_ci	for (; id->bus; id++)
18948c2ecf20Sopenharmony_ci		if (hid_match_one_id(hdev, id))
18958c2ecf20Sopenharmony_ci			return id;
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci	return NULL;
18988c2ecf20Sopenharmony_ci}
18998c2ecf20Sopenharmony_ci
19008c2ecf20Sopenharmony_cistatic const struct hid_device_id hid_hiddev_list[] = {
19018c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS) },
19028c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_MGE, USB_DEVICE_ID_MGE_UPS1) },
19038c2ecf20Sopenharmony_ci	{ }
19048c2ecf20Sopenharmony_ci};
19058c2ecf20Sopenharmony_ci
19068c2ecf20Sopenharmony_cistatic bool hid_hiddev(struct hid_device *hdev)
19078c2ecf20Sopenharmony_ci{
19088c2ecf20Sopenharmony_ci	return !!hid_match_id(hdev, hid_hiddev_list);
19098c2ecf20Sopenharmony_ci}
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci
19128c2ecf20Sopenharmony_cistatic ssize_t
19138c2ecf20Sopenharmony_ciread_report_descriptor(struct file *filp, struct kobject *kobj,
19148c2ecf20Sopenharmony_ci		struct bin_attribute *attr,
19158c2ecf20Sopenharmony_ci		char *buf, loff_t off, size_t count)
19168c2ecf20Sopenharmony_ci{
19178c2ecf20Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
19188c2ecf20Sopenharmony_ci	struct hid_device *hdev = to_hid_device(dev);
19198c2ecf20Sopenharmony_ci
19208c2ecf20Sopenharmony_ci	if (off >= hdev->rsize)
19218c2ecf20Sopenharmony_ci		return 0;
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_ci	if (off + count > hdev->rsize)
19248c2ecf20Sopenharmony_ci		count = hdev->rsize - off;
19258c2ecf20Sopenharmony_ci
19268c2ecf20Sopenharmony_ci	memcpy(buf, hdev->rdesc + off, count);
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci	return count;
19298c2ecf20Sopenharmony_ci}
19308c2ecf20Sopenharmony_ci
19318c2ecf20Sopenharmony_cistatic ssize_t
19328c2ecf20Sopenharmony_cishow_country(struct device *dev, struct device_attribute *attr,
19338c2ecf20Sopenharmony_ci		char *buf)
19348c2ecf20Sopenharmony_ci{
19358c2ecf20Sopenharmony_ci	struct hid_device *hdev = to_hid_device(dev);
19368c2ecf20Sopenharmony_ci
19378c2ecf20Sopenharmony_ci	return sprintf(buf, "%02x\n", hdev->country & 0xff);
19388c2ecf20Sopenharmony_ci}
19398c2ecf20Sopenharmony_ci
19408c2ecf20Sopenharmony_cistatic struct bin_attribute dev_bin_attr_report_desc = {
19418c2ecf20Sopenharmony_ci	.attr = { .name = "report_descriptor", .mode = 0444 },
19428c2ecf20Sopenharmony_ci	.read = read_report_descriptor,
19438c2ecf20Sopenharmony_ci	.size = HID_MAX_DESCRIPTOR_SIZE,
19448c2ecf20Sopenharmony_ci};
19458c2ecf20Sopenharmony_ci
19468c2ecf20Sopenharmony_cistatic const struct device_attribute dev_attr_country = {
19478c2ecf20Sopenharmony_ci	.attr = { .name = "country", .mode = 0444 },
19488c2ecf20Sopenharmony_ci	.show = show_country,
19498c2ecf20Sopenharmony_ci};
19508c2ecf20Sopenharmony_ci
19518c2ecf20Sopenharmony_ciint hid_connect(struct hid_device *hdev, unsigned int connect_mask)
19528c2ecf20Sopenharmony_ci{
19538c2ecf20Sopenharmony_ci	static const char *types[] = { "Device", "Pointer", "Mouse", "Device",
19548c2ecf20Sopenharmony_ci		"Joystick", "Gamepad", "Keyboard", "Keypad",
19558c2ecf20Sopenharmony_ci		"Multi-Axis Controller"
19568c2ecf20Sopenharmony_ci	};
19578c2ecf20Sopenharmony_ci	const char *type, *bus;
19588c2ecf20Sopenharmony_ci	char buf[64] = "";
19598c2ecf20Sopenharmony_ci	unsigned int i;
19608c2ecf20Sopenharmony_ci	int len;
19618c2ecf20Sopenharmony_ci	int ret;
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	if (hdev->quirks & HID_QUIRK_HIDDEV_FORCE)
19648c2ecf20Sopenharmony_ci		connect_mask |= (HID_CONNECT_HIDDEV_FORCE | HID_CONNECT_HIDDEV);
19658c2ecf20Sopenharmony_ci	if (hdev->quirks & HID_QUIRK_HIDINPUT_FORCE)
19668c2ecf20Sopenharmony_ci		connect_mask |= HID_CONNECT_HIDINPUT_FORCE;
19678c2ecf20Sopenharmony_ci	if (hdev->bus != BUS_USB)
19688c2ecf20Sopenharmony_ci		connect_mask &= ~HID_CONNECT_HIDDEV;
19698c2ecf20Sopenharmony_ci	if (hid_hiddev(hdev))
19708c2ecf20Sopenharmony_ci		connect_mask |= HID_CONNECT_HIDDEV_FORCE;
19718c2ecf20Sopenharmony_ci
19728c2ecf20Sopenharmony_ci	if ((connect_mask & HID_CONNECT_HIDINPUT) && !hidinput_connect(hdev,
19738c2ecf20Sopenharmony_ci				connect_mask & HID_CONNECT_HIDINPUT_FORCE))
19748c2ecf20Sopenharmony_ci		hdev->claimed |= HID_CLAIMED_INPUT;
19758c2ecf20Sopenharmony_ci
19768c2ecf20Sopenharmony_ci	if ((connect_mask & HID_CONNECT_HIDDEV) && hdev->hiddev_connect &&
19778c2ecf20Sopenharmony_ci			!hdev->hiddev_connect(hdev,
19788c2ecf20Sopenharmony_ci				connect_mask & HID_CONNECT_HIDDEV_FORCE))
19798c2ecf20Sopenharmony_ci		hdev->claimed |= HID_CLAIMED_HIDDEV;
19808c2ecf20Sopenharmony_ci	if ((connect_mask & HID_CONNECT_HIDRAW) && !hidraw_connect(hdev))
19818c2ecf20Sopenharmony_ci		hdev->claimed |= HID_CLAIMED_HIDRAW;
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_ci	if (connect_mask & HID_CONNECT_DRIVER)
19848c2ecf20Sopenharmony_ci		hdev->claimed |= HID_CLAIMED_DRIVER;
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci	/* Drivers with the ->raw_event callback set are not required to connect
19878c2ecf20Sopenharmony_ci	 * to any other listener. */
19888c2ecf20Sopenharmony_ci	if (!hdev->claimed && !hdev->driver->raw_event) {
19898c2ecf20Sopenharmony_ci		hid_err(hdev, "device has no listeners, quitting\n");
19908c2ecf20Sopenharmony_ci		return -ENODEV;
19918c2ecf20Sopenharmony_ci	}
19928c2ecf20Sopenharmony_ci
19938c2ecf20Sopenharmony_ci	if ((hdev->claimed & HID_CLAIMED_INPUT) &&
19948c2ecf20Sopenharmony_ci			(connect_mask & HID_CONNECT_FF) && hdev->ff_init)
19958c2ecf20Sopenharmony_ci		hdev->ff_init(hdev);
19968c2ecf20Sopenharmony_ci
19978c2ecf20Sopenharmony_ci	len = 0;
19988c2ecf20Sopenharmony_ci	if (hdev->claimed & HID_CLAIMED_INPUT)
19998c2ecf20Sopenharmony_ci		len += sprintf(buf + len, "input");
20008c2ecf20Sopenharmony_ci	if (hdev->claimed & HID_CLAIMED_HIDDEV)
20018c2ecf20Sopenharmony_ci		len += sprintf(buf + len, "%shiddev%d", len ? "," : "",
20028c2ecf20Sopenharmony_ci				((struct hiddev *)hdev->hiddev)->minor);
20038c2ecf20Sopenharmony_ci	if (hdev->claimed & HID_CLAIMED_HIDRAW)
20048c2ecf20Sopenharmony_ci		len += sprintf(buf + len, "%shidraw%d", len ? "," : "",
20058c2ecf20Sopenharmony_ci				((struct hidraw *)hdev->hidraw)->minor);
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci	type = "Device";
20088c2ecf20Sopenharmony_ci	for (i = 0; i < hdev->maxcollection; i++) {
20098c2ecf20Sopenharmony_ci		struct hid_collection *col = &hdev->collection[i];
20108c2ecf20Sopenharmony_ci		if (col->type == HID_COLLECTION_APPLICATION &&
20118c2ecf20Sopenharmony_ci		   (col->usage & HID_USAGE_PAGE) == HID_UP_GENDESK &&
20128c2ecf20Sopenharmony_ci		   (col->usage & 0xffff) < ARRAY_SIZE(types)) {
20138c2ecf20Sopenharmony_ci			type = types[col->usage & 0xffff];
20148c2ecf20Sopenharmony_ci			break;
20158c2ecf20Sopenharmony_ci		}
20168c2ecf20Sopenharmony_ci	}
20178c2ecf20Sopenharmony_ci
20188c2ecf20Sopenharmony_ci	switch (hdev->bus) {
20198c2ecf20Sopenharmony_ci	case BUS_USB:
20208c2ecf20Sopenharmony_ci		bus = "USB";
20218c2ecf20Sopenharmony_ci		break;
20228c2ecf20Sopenharmony_ci	case BUS_BLUETOOTH:
20238c2ecf20Sopenharmony_ci		bus = "BLUETOOTH";
20248c2ecf20Sopenharmony_ci		break;
20258c2ecf20Sopenharmony_ci	case BUS_I2C:
20268c2ecf20Sopenharmony_ci		bus = "I2C";
20278c2ecf20Sopenharmony_ci		break;
20288c2ecf20Sopenharmony_ci	case BUS_VIRTUAL:
20298c2ecf20Sopenharmony_ci		bus = "VIRTUAL";
20308c2ecf20Sopenharmony_ci		break;
20318c2ecf20Sopenharmony_ci	default:
20328c2ecf20Sopenharmony_ci		bus = "<UNKNOWN>";
20338c2ecf20Sopenharmony_ci	}
20348c2ecf20Sopenharmony_ci
20358c2ecf20Sopenharmony_ci	ret = device_create_file(&hdev->dev, &dev_attr_country);
20368c2ecf20Sopenharmony_ci	if (ret)
20378c2ecf20Sopenharmony_ci		hid_warn(hdev,
20388c2ecf20Sopenharmony_ci			 "can't create sysfs country code attribute err: %d\n", ret);
20398c2ecf20Sopenharmony_ci
20408c2ecf20Sopenharmony_ci	hid_info(hdev, "%s: %s HID v%x.%02x %s [%s] on %s\n",
20418c2ecf20Sopenharmony_ci		 buf, bus, hdev->version >> 8, hdev->version & 0xff,
20428c2ecf20Sopenharmony_ci		 type, hdev->name, hdev->phys);
20438c2ecf20Sopenharmony_ci
20448c2ecf20Sopenharmony_ci	return 0;
20458c2ecf20Sopenharmony_ci}
20468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_connect);
20478c2ecf20Sopenharmony_ci
20488c2ecf20Sopenharmony_civoid hid_disconnect(struct hid_device *hdev)
20498c2ecf20Sopenharmony_ci{
20508c2ecf20Sopenharmony_ci	device_remove_file(&hdev->dev, &dev_attr_country);
20518c2ecf20Sopenharmony_ci	if (hdev->claimed & HID_CLAIMED_INPUT)
20528c2ecf20Sopenharmony_ci		hidinput_disconnect(hdev);
20538c2ecf20Sopenharmony_ci	if (hdev->claimed & HID_CLAIMED_HIDDEV)
20548c2ecf20Sopenharmony_ci		hdev->hiddev_disconnect(hdev);
20558c2ecf20Sopenharmony_ci	if (hdev->claimed & HID_CLAIMED_HIDRAW)
20568c2ecf20Sopenharmony_ci		hidraw_disconnect(hdev);
20578c2ecf20Sopenharmony_ci	hdev->claimed = 0;
20588c2ecf20Sopenharmony_ci}
20598c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_disconnect);
20608c2ecf20Sopenharmony_ci
20618c2ecf20Sopenharmony_ci/**
20628c2ecf20Sopenharmony_ci * hid_hw_start - start underlying HW
20638c2ecf20Sopenharmony_ci * @hdev: hid device
20648c2ecf20Sopenharmony_ci * @connect_mask: which outputs to connect, see HID_CONNECT_*
20658c2ecf20Sopenharmony_ci *
20668c2ecf20Sopenharmony_ci * Call this in probe function *after* hid_parse. This will setup HW
20678c2ecf20Sopenharmony_ci * buffers and start the device (if not defeirred to device open).
20688c2ecf20Sopenharmony_ci * hid_hw_stop must be called if this was successful.
20698c2ecf20Sopenharmony_ci */
20708c2ecf20Sopenharmony_ciint hid_hw_start(struct hid_device *hdev, unsigned int connect_mask)
20718c2ecf20Sopenharmony_ci{
20728c2ecf20Sopenharmony_ci	int error;
20738c2ecf20Sopenharmony_ci
20748c2ecf20Sopenharmony_ci	error = hdev->ll_driver->start(hdev);
20758c2ecf20Sopenharmony_ci	if (error)
20768c2ecf20Sopenharmony_ci		return error;
20778c2ecf20Sopenharmony_ci
20788c2ecf20Sopenharmony_ci	if (connect_mask) {
20798c2ecf20Sopenharmony_ci		error = hid_connect(hdev, connect_mask);
20808c2ecf20Sopenharmony_ci		if (error) {
20818c2ecf20Sopenharmony_ci			hdev->ll_driver->stop(hdev);
20828c2ecf20Sopenharmony_ci			return error;
20838c2ecf20Sopenharmony_ci		}
20848c2ecf20Sopenharmony_ci	}
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci	return 0;
20878c2ecf20Sopenharmony_ci}
20888c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_hw_start);
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_ci/**
20918c2ecf20Sopenharmony_ci * hid_hw_stop - stop underlying HW
20928c2ecf20Sopenharmony_ci * @hdev: hid device
20938c2ecf20Sopenharmony_ci *
20948c2ecf20Sopenharmony_ci * This is usually called from remove function or from probe when something
20958c2ecf20Sopenharmony_ci * failed and hid_hw_start was called already.
20968c2ecf20Sopenharmony_ci */
20978c2ecf20Sopenharmony_civoid hid_hw_stop(struct hid_device *hdev)
20988c2ecf20Sopenharmony_ci{
20998c2ecf20Sopenharmony_ci	hid_disconnect(hdev);
21008c2ecf20Sopenharmony_ci	hdev->ll_driver->stop(hdev);
21018c2ecf20Sopenharmony_ci}
21028c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_hw_stop);
21038c2ecf20Sopenharmony_ci
21048c2ecf20Sopenharmony_ci/**
21058c2ecf20Sopenharmony_ci * hid_hw_open - signal underlying HW to start delivering events
21068c2ecf20Sopenharmony_ci * @hdev: hid device
21078c2ecf20Sopenharmony_ci *
21088c2ecf20Sopenharmony_ci * Tell underlying HW to start delivering events from the device.
21098c2ecf20Sopenharmony_ci * This function should be called sometime after successful call
21108c2ecf20Sopenharmony_ci * to hid_hw_start().
21118c2ecf20Sopenharmony_ci */
21128c2ecf20Sopenharmony_ciint hid_hw_open(struct hid_device *hdev)
21138c2ecf20Sopenharmony_ci{
21148c2ecf20Sopenharmony_ci	int ret;
21158c2ecf20Sopenharmony_ci
21168c2ecf20Sopenharmony_ci	ret = mutex_lock_killable(&hdev->ll_open_lock);
21178c2ecf20Sopenharmony_ci	if (ret)
21188c2ecf20Sopenharmony_ci		return ret;
21198c2ecf20Sopenharmony_ci
21208c2ecf20Sopenharmony_ci	if (!hdev->ll_open_count++) {
21218c2ecf20Sopenharmony_ci		ret = hdev->ll_driver->open(hdev);
21228c2ecf20Sopenharmony_ci		if (ret)
21238c2ecf20Sopenharmony_ci			hdev->ll_open_count--;
21248c2ecf20Sopenharmony_ci	}
21258c2ecf20Sopenharmony_ci
21268c2ecf20Sopenharmony_ci	mutex_unlock(&hdev->ll_open_lock);
21278c2ecf20Sopenharmony_ci	return ret;
21288c2ecf20Sopenharmony_ci}
21298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_hw_open);
21308c2ecf20Sopenharmony_ci
21318c2ecf20Sopenharmony_ci/**
21328c2ecf20Sopenharmony_ci * hid_hw_close - signal underlaying HW to stop delivering events
21338c2ecf20Sopenharmony_ci *
21348c2ecf20Sopenharmony_ci * @hdev: hid device
21358c2ecf20Sopenharmony_ci *
21368c2ecf20Sopenharmony_ci * This function indicates that we are not interested in the events
21378c2ecf20Sopenharmony_ci * from this device anymore. Delivery of events may or may not stop,
21388c2ecf20Sopenharmony_ci * depending on the number of users still outstanding.
21398c2ecf20Sopenharmony_ci */
21408c2ecf20Sopenharmony_civoid hid_hw_close(struct hid_device *hdev)
21418c2ecf20Sopenharmony_ci{
21428c2ecf20Sopenharmony_ci	mutex_lock(&hdev->ll_open_lock);
21438c2ecf20Sopenharmony_ci	if (!--hdev->ll_open_count)
21448c2ecf20Sopenharmony_ci		hdev->ll_driver->close(hdev);
21458c2ecf20Sopenharmony_ci	mutex_unlock(&hdev->ll_open_lock);
21468c2ecf20Sopenharmony_ci}
21478c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_hw_close);
21488c2ecf20Sopenharmony_ci
21498c2ecf20Sopenharmony_cistruct hid_dynid {
21508c2ecf20Sopenharmony_ci	struct list_head list;
21518c2ecf20Sopenharmony_ci	struct hid_device_id id;
21528c2ecf20Sopenharmony_ci};
21538c2ecf20Sopenharmony_ci
21548c2ecf20Sopenharmony_ci/**
21558c2ecf20Sopenharmony_ci * store_new_id - add a new HID device ID to this driver and re-probe devices
21568c2ecf20Sopenharmony_ci * @drv: target device driver
21578c2ecf20Sopenharmony_ci * @buf: buffer for scanning device ID data
21588c2ecf20Sopenharmony_ci * @count: input size
21598c2ecf20Sopenharmony_ci *
21608c2ecf20Sopenharmony_ci * Adds a new dynamic hid device ID to this driver,
21618c2ecf20Sopenharmony_ci * and causes the driver to probe for all devices again.
21628c2ecf20Sopenharmony_ci */
21638c2ecf20Sopenharmony_cistatic ssize_t new_id_store(struct device_driver *drv, const char *buf,
21648c2ecf20Sopenharmony_ci		size_t count)
21658c2ecf20Sopenharmony_ci{
21668c2ecf20Sopenharmony_ci	struct hid_driver *hdrv = to_hid_driver(drv);
21678c2ecf20Sopenharmony_ci	struct hid_dynid *dynid;
21688c2ecf20Sopenharmony_ci	__u32 bus, vendor, product;
21698c2ecf20Sopenharmony_ci	unsigned long driver_data = 0;
21708c2ecf20Sopenharmony_ci	int ret;
21718c2ecf20Sopenharmony_ci
21728c2ecf20Sopenharmony_ci	ret = sscanf(buf, "%x %x %x %lx",
21738c2ecf20Sopenharmony_ci			&bus, &vendor, &product, &driver_data);
21748c2ecf20Sopenharmony_ci	if (ret < 3)
21758c2ecf20Sopenharmony_ci		return -EINVAL;
21768c2ecf20Sopenharmony_ci
21778c2ecf20Sopenharmony_ci	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
21788c2ecf20Sopenharmony_ci	if (!dynid)
21798c2ecf20Sopenharmony_ci		return -ENOMEM;
21808c2ecf20Sopenharmony_ci
21818c2ecf20Sopenharmony_ci	dynid->id.bus = bus;
21828c2ecf20Sopenharmony_ci	dynid->id.group = HID_GROUP_ANY;
21838c2ecf20Sopenharmony_ci	dynid->id.vendor = vendor;
21848c2ecf20Sopenharmony_ci	dynid->id.product = product;
21858c2ecf20Sopenharmony_ci	dynid->id.driver_data = driver_data;
21868c2ecf20Sopenharmony_ci
21878c2ecf20Sopenharmony_ci	spin_lock(&hdrv->dyn_lock);
21888c2ecf20Sopenharmony_ci	list_add_tail(&dynid->list, &hdrv->dyn_list);
21898c2ecf20Sopenharmony_ci	spin_unlock(&hdrv->dyn_lock);
21908c2ecf20Sopenharmony_ci
21918c2ecf20Sopenharmony_ci	ret = driver_attach(&hdrv->driver);
21928c2ecf20Sopenharmony_ci
21938c2ecf20Sopenharmony_ci	return ret ? : count;
21948c2ecf20Sopenharmony_ci}
21958c2ecf20Sopenharmony_cistatic DRIVER_ATTR_WO(new_id);
21968c2ecf20Sopenharmony_ci
21978c2ecf20Sopenharmony_cistatic struct attribute *hid_drv_attrs[] = {
21988c2ecf20Sopenharmony_ci	&driver_attr_new_id.attr,
21998c2ecf20Sopenharmony_ci	NULL,
22008c2ecf20Sopenharmony_ci};
22018c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(hid_drv);
22028c2ecf20Sopenharmony_ci
22038c2ecf20Sopenharmony_cistatic void hid_free_dynids(struct hid_driver *hdrv)
22048c2ecf20Sopenharmony_ci{
22058c2ecf20Sopenharmony_ci	struct hid_dynid *dynid, *n;
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_ci	spin_lock(&hdrv->dyn_lock);
22088c2ecf20Sopenharmony_ci	list_for_each_entry_safe(dynid, n, &hdrv->dyn_list, list) {
22098c2ecf20Sopenharmony_ci		list_del(&dynid->list);
22108c2ecf20Sopenharmony_ci		kfree(dynid);
22118c2ecf20Sopenharmony_ci	}
22128c2ecf20Sopenharmony_ci	spin_unlock(&hdrv->dyn_lock);
22138c2ecf20Sopenharmony_ci}
22148c2ecf20Sopenharmony_ci
22158c2ecf20Sopenharmony_ciconst struct hid_device_id *hid_match_device(struct hid_device *hdev,
22168c2ecf20Sopenharmony_ci					     struct hid_driver *hdrv)
22178c2ecf20Sopenharmony_ci{
22188c2ecf20Sopenharmony_ci	struct hid_dynid *dynid;
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ci	spin_lock(&hdrv->dyn_lock);
22218c2ecf20Sopenharmony_ci	list_for_each_entry(dynid, &hdrv->dyn_list, list) {
22228c2ecf20Sopenharmony_ci		if (hid_match_one_id(hdev, &dynid->id)) {
22238c2ecf20Sopenharmony_ci			spin_unlock(&hdrv->dyn_lock);
22248c2ecf20Sopenharmony_ci			return &dynid->id;
22258c2ecf20Sopenharmony_ci		}
22268c2ecf20Sopenharmony_ci	}
22278c2ecf20Sopenharmony_ci	spin_unlock(&hdrv->dyn_lock);
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	return hid_match_id(hdev, hdrv->id_table);
22308c2ecf20Sopenharmony_ci}
22318c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_match_device);
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_cistatic int hid_bus_match(struct device *dev, struct device_driver *drv)
22348c2ecf20Sopenharmony_ci{
22358c2ecf20Sopenharmony_ci	struct hid_driver *hdrv = to_hid_driver(drv);
22368c2ecf20Sopenharmony_ci	struct hid_device *hdev = to_hid_device(dev);
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci	return hid_match_device(hdev, hdrv) != NULL;
22398c2ecf20Sopenharmony_ci}
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_ci/**
22428c2ecf20Sopenharmony_ci * hid_compare_device_paths - check if both devices share the same path
22438c2ecf20Sopenharmony_ci * @hdev_a: hid device
22448c2ecf20Sopenharmony_ci * @hdev_b: hid device
22458c2ecf20Sopenharmony_ci * @separator: char to use as separator
22468c2ecf20Sopenharmony_ci *
22478c2ecf20Sopenharmony_ci * Check if two devices share the same path up to the last occurrence of
22488c2ecf20Sopenharmony_ci * the separator char. Both paths must exist (i.e., zero-length paths
22498c2ecf20Sopenharmony_ci * don't match).
22508c2ecf20Sopenharmony_ci */
22518c2ecf20Sopenharmony_cibool hid_compare_device_paths(struct hid_device *hdev_a,
22528c2ecf20Sopenharmony_ci			      struct hid_device *hdev_b, char separator)
22538c2ecf20Sopenharmony_ci{
22548c2ecf20Sopenharmony_ci	int n1 = strrchr(hdev_a->phys, separator) - hdev_a->phys;
22558c2ecf20Sopenharmony_ci	int n2 = strrchr(hdev_b->phys, separator) - hdev_b->phys;
22568c2ecf20Sopenharmony_ci
22578c2ecf20Sopenharmony_ci	if (n1 != n2 || n1 <= 0 || n2 <= 0)
22588c2ecf20Sopenharmony_ci		return false;
22598c2ecf20Sopenharmony_ci
22608c2ecf20Sopenharmony_ci	return !strncmp(hdev_a->phys, hdev_b->phys, n1);
22618c2ecf20Sopenharmony_ci}
22628c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_compare_device_paths);
22638c2ecf20Sopenharmony_ci
22648c2ecf20Sopenharmony_cistatic int hid_device_probe(struct device *dev)
22658c2ecf20Sopenharmony_ci{
22668c2ecf20Sopenharmony_ci	struct hid_driver *hdrv = to_hid_driver(dev->driver);
22678c2ecf20Sopenharmony_ci	struct hid_device *hdev = to_hid_device(dev);
22688c2ecf20Sopenharmony_ci	const struct hid_device_id *id;
22698c2ecf20Sopenharmony_ci	int ret = 0;
22708c2ecf20Sopenharmony_ci
22718c2ecf20Sopenharmony_ci	if (down_interruptible(&hdev->driver_input_lock)) {
22728c2ecf20Sopenharmony_ci		ret = -EINTR;
22738c2ecf20Sopenharmony_ci		goto end;
22748c2ecf20Sopenharmony_ci	}
22758c2ecf20Sopenharmony_ci	hdev->io_started = false;
22768c2ecf20Sopenharmony_ci
22778c2ecf20Sopenharmony_ci	clear_bit(ffs(HID_STAT_REPROBED), &hdev->status);
22788c2ecf20Sopenharmony_ci
22798c2ecf20Sopenharmony_ci	if (!hdev->driver) {
22808c2ecf20Sopenharmony_ci		id = hid_match_device(hdev, hdrv);
22818c2ecf20Sopenharmony_ci		if (id == NULL) {
22828c2ecf20Sopenharmony_ci			ret = -ENODEV;
22838c2ecf20Sopenharmony_ci			goto unlock;
22848c2ecf20Sopenharmony_ci		}
22858c2ecf20Sopenharmony_ci
22868c2ecf20Sopenharmony_ci		if (hdrv->match) {
22878c2ecf20Sopenharmony_ci			if (!hdrv->match(hdev, hid_ignore_special_drivers)) {
22888c2ecf20Sopenharmony_ci				ret = -ENODEV;
22898c2ecf20Sopenharmony_ci				goto unlock;
22908c2ecf20Sopenharmony_ci			}
22918c2ecf20Sopenharmony_ci		} else {
22928c2ecf20Sopenharmony_ci			/*
22938c2ecf20Sopenharmony_ci			 * hid-generic implements .match(), so if
22948c2ecf20Sopenharmony_ci			 * hid_ignore_special_drivers is set, we can safely
22958c2ecf20Sopenharmony_ci			 * return.
22968c2ecf20Sopenharmony_ci			 */
22978c2ecf20Sopenharmony_ci			if (hid_ignore_special_drivers) {
22988c2ecf20Sopenharmony_ci				ret = -ENODEV;
22998c2ecf20Sopenharmony_ci				goto unlock;
23008c2ecf20Sopenharmony_ci			}
23018c2ecf20Sopenharmony_ci		}
23028c2ecf20Sopenharmony_ci
23038c2ecf20Sopenharmony_ci		/* reset the quirks that has been previously set */
23048c2ecf20Sopenharmony_ci		hdev->quirks = hid_lookup_quirk(hdev);
23058c2ecf20Sopenharmony_ci		hdev->driver = hdrv;
23068c2ecf20Sopenharmony_ci		if (hdrv->probe) {
23078c2ecf20Sopenharmony_ci			ret = hdrv->probe(hdev, id);
23088c2ecf20Sopenharmony_ci		} else { /* default probe */
23098c2ecf20Sopenharmony_ci			ret = hid_open_report(hdev);
23108c2ecf20Sopenharmony_ci			if (!ret)
23118c2ecf20Sopenharmony_ci				ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
23128c2ecf20Sopenharmony_ci		}
23138c2ecf20Sopenharmony_ci		if (ret) {
23148c2ecf20Sopenharmony_ci			hid_close_report(hdev);
23158c2ecf20Sopenharmony_ci			hdev->driver = NULL;
23168c2ecf20Sopenharmony_ci		}
23178c2ecf20Sopenharmony_ci	}
23188c2ecf20Sopenharmony_ciunlock:
23198c2ecf20Sopenharmony_ci	if (!hdev->io_started)
23208c2ecf20Sopenharmony_ci		up(&hdev->driver_input_lock);
23218c2ecf20Sopenharmony_ciend:
23228c2ecf20Sopenharmony_ci	return ret;
23238c2ecf20Sopenharmony_ci}
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_cistatic int hid_device_remove(struct device *dev)
23268c2ecf20Sopenharmony_ci{
23278c2ecf20Sopenharmony_ci	struct hid_device *hdev = to_hid_device(dev);
23288c2ecf20Sopenharmony_ci	struct hid_driver *hdrv;
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	down(&hdev->driver_input_lock);
23318c2ecf20Sopenharmony_ci	hdev->io_started = false;
23328c2ecf20Sopenharmony_ci
23338c2ecf20Sopenharmony_ci	hdrv = hdev->driver;
23348c2ecf20Sopenharmony_ci	if (hdrv) {
23358c2ecf20Sopenharmony_ci		if (hdrv->remove)
23368c2ecf20Sopenharmony_ci			hdrv->remove(hdev);
23378c2ecf20Sopenharmony_ci		else /* default remove */
23388c2ecf20Sopenharmony_ci			hid_hw_stop(hdev);
23398c2ecf20Sopenharmony_ci		hid_close_report(hdev);
23408c2ecf20Sopenharmony_ci		hdev->driver = NULL;
23418c2ecf20Sopenharmony_ci	}
23428c2ecf20Sopenharmony_ci
23438c2ecf20Sopenharmony_ci	if (!hdev->io_started)
23448c2ecf20Sopenharmony_ci		up(&hdev->driver_input_lock);
23458c2ecf20Sopenharmony_ci
23468c2ecf20Sopenharmony_ci	return 0;
23478c2ecf20Sopenharmony_ci}
23488c2ecf20Sopenharmony_ci
23498c2ecf20Sopenharmony_cistatic ssize_t modalias_show(struct device *dev, struct device_attribute *a,
23508c2ecf20Sopenharmony_ci			     char *buf)
23518c2ecf20Sopenharmony_ci{
23528c2ecf20Sopenharmony_ci	struct hid_device *hdev = container_of(dev, struct hid_device, dev);
23538c2ecf20Sopenharmony_ci
23548c2ecf20Sopenharmony_ci	return scnprintf(buf, PAGE_SIZE, "hid:b%04Xg%04Xv%08Xp%08X\n",
23558c2ecf20Sopenharmony_ci			 hdev->bus, hdev->group, hdev->vendor, hdev->product);
23568c2ecf20Sopenharmony_ci}
23578c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(modalias);
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_cistatic struct attribute *hid_dev_attrs[] = {
23608c2ecf20Sopenharmony_ci	&dev_attr_modalias.attr,
23618c2ecf20Sopenharmony_ci	NULL,
23628c2ecf20Sopenharmony_ci};
23638c2ecf20Sopenharmony_cistatic struct bin_attribute *hid_dev_bin_attrs[] = {
23648c2ecf20Sopenharmony_ci	&dev_bin_attr_report_desc,
23658c2ecf20Sopenharmony_ci	NULL
23668c2ecf20Sopenharmony_ci};
23678c2ecf20Sopenharmony_cistatic const struct attribute_group hid_dev_group = {
23688c2ecf20Sopenharmony_ci	.attrs = hid_dev_attrs,
23698c2ecf20Sopenharmony_ci	.bin_attrs = hid_dev_bin_attrs,
23708c2ecf20Sopenharmony_ci};
23718c2ecf20Sopenharmony_ci__ATTRIBUTE_GROUPS(hid_dev);
23728c2ecf20Sopenharmony_ci
23738c2ecf20Sopenharmony_cistatic int hid_uevent(struct device *dev, struct kobj_uevent_env *env)
23748c2ecf20Sopenharmony_ci{
23758c2ecf20Sopenharmony_ci	struct hid_device *hdev = to_hid_device(dev);
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_ci	if (add_uevent_var(env, "HID_ID=%04X:%08X:%08X",
23788c2ecf20Sopenharmony_ci			hdev->bus, hdev->vendor, hdev->product))
23798c2ecf20Sopenharmony_ci		return -ENOMEM;
23808c2ecf20Sopenharmony_ci
23818c2ecf20Sopenharmony_ci	if (add_uevent_var(env, "HID_NAME=%s", hdev->name))
23828c2ecf20Sopenharmony_ci		return -ENOMEM;
23838c2ecf20Sopenharmony_ci
23848c2ecf20Sopenharmony_ci	if (add_uevent_var(env, "HID_PHYS=%s", hdev->phys))
23858c2ecf20Sopenharmony_ci		return -ENOMEM;
23868c2ecf20Sopenharmony_ci
23878c2ecf20Sopenharmony_ci	if (add_uevent_var(env, "HID_UNIQ=%s", hdev->uniq))
23888c2ecf20Sopenharmony_ci		return -ENOMEM;
23898c2ecf20Sopenharmony_ci
23908c2ecf20Sopenharmony_ci	if (add_uevent_var(env, "MODALIAS=hid:b%04Xg%04Xv%08Xp%08X",
23918c2ecf20Sopenharmony_ci			   hdev->bus, hdev->group, hdev->vendor, hdev->product))
23928c2ecf20Sopenharmony_ci		return -ENOMEM;
23938c2ecf20Sopenharmony_ci
23948c2ecf20Sopenharmony_ci	return 0;
23958c2ecf20Sopenharmony_ci}
23968c2ecf20Sopenharmony_ci
23978c2ecf20Sopenharmony_cistruct bus_type hid_bus_type = {
23988c2ecf20Sopenharmony_ci	.name		= "hid",
23998c2ecf20Sopenharmony_ci	.dev_groups	= hid_dev_groups,
24008c2ecf20Sopenharmony_ci	.drv_groups	= hid_drv_groups,
24018c2ecf20Sopenharmony_ci	.match		= hid_bus_match,
24028c2ecf20Sopenharmony_ci	.probe		= hid_device_probe,
24038c2ecf20Sopenharmony_ci	.remove		= hid_device_remove,
24048c2ecf20Sopenharmony_ci	.uevent		= hid_uevent,
24058c2ecf20Sopenharmony_ci};
24068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(hid_bus_type);
24078c2ecf20Sopenharmony_ci
24088c2ecf20Sopenharmony_ciint hid_add_device(struct hid_device *hdev)
24098c2ecf20Sopenharmony_ci{
24108c2ecf20Sopenharmony_ci	static atomic_t id = ATOMIC_INIT(0);
24118c2ecf20Sopenharmony_ci	int ret;
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci	if (WARN_ON(hdev->status & HID_STAT_ADDED))
24148c2ecf20Sopenharmony_ci		return -EBUSY;
24158c2ecf20Sopenharmony_ci
24168c2ecf20Sopenharmony_ci	hdev->quirks = hid_lookup_quirk(hdev);
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci	/* we need to kill them here, otherwise they will stay allocated to
24198c2ecf20Sopenharmony_ci	 * wait for coming driver */
24208c2ecf20Sopenharmony_ci	if (hid_ignore(hdev))
24218c2ecf20Sopenharmony_ci		return -ENODEV;
24228c2ecf20Sopenharmony_ci
24238c2ecf20Sopenharmony_ci	/*
24248c2ecf20Sopenharmony_ci	 * Check for the mandatory transport channel.
24258c2ecf20Sopenharmony_ci	 */
24268c2ecf20Sopenharmony_ci	 if (!hdev->ll_driver->raw_request) {
24278c2ecf20Sopenharmony_ci		hid_err(hdev, "transport driver missing .raw_request()\n");
24288c2ecf20Sopenharmony_ci		return -EINVAL;
24298c2ecf20Sopenharmony_ci	 }
24308c2ecf20Sopenharmony_ci
24318c2ecf20Sopenharmony_ci	/*
24328c2ecf20Sopenharmony_ci	 * Read the device report descriptor once and use as template
24338c2ecf20Sopenharmony_ci	 * for the driver-specific modifications.
24348c2ecf20Sopenharmony_ci	 */
24358c2ecf20Sopenharmony_ci	ret = hdev->ll_driver->parse(hdev);
24368c2ecf20Sopenharmony_ci	if (ret)
24378c2ecf20Sopenharmony_ci		return ret;
24388c2ecf20Sopenharmony_ci	if (!hdev->dev_rdesc)
24398c2ecf20Sopenharmony_ci		return -ENODEV;
24408c2ecf20Sopenharmony_ci
24418c2ecf20Sopenharmony_ci	/*
24428c2ecf20Sopenharmony_ci	 * Scan generic devices for group information
24438c2ecf20Sopenharmony_ci	 */
24448c2ecf20Sopenharmony_ci	if (hid_ignore_special_drivers) {
24458c2ecf20Sopenharmony_ci		hdev->group = HID_GROUP_GENERIC;
24468c2ecf20Sopenharmony_ci	} else if (!hdev->group &&
24478c2ecf20Sopenharmony_ci		   !(hdev->quirks & HID_QUIRK_HAVE_SPECIAL_DRIVER)) {
24488c2ecf20Sopenharmony_ci		ret = hid_scan_report(hdev);
24498c2ecf20Sopenharmony_ci		if (ret)
24508c2ecf20Sopenharmony_ci			hid_warn(hdev, "bad device descriptor (%d)\n", ret);
24518c2ecf20Sopenharmony_ci	}
24528c2ecf20Sopenharmony_ci
24538c2ecf20Sopenharmony_ci	hdev->id = atomic_inc_return(&id);
24548c2ecf20Sopenharmony_ci
24558c2ecf20Sopenharmony_ci	/* XXX hack, any other cleaner solution after the driver core
24568c2ecf20Sopenharmony_ci	 * is converted to allow more than 20 bytes as the device name? */
24578c2ecf20Sopenharmony_ci	dev_set_name(&hdev->dev, "%04X:%04X:%04X.%04X", hdev->bus,
24588c2ecf20Sopenharmony_ci		     hdev->vendor, hdev->product, hdev->id);
24598c2ecf20Sopenharmony_ci
24608c2ecf20Sopenharmony_ci	hid_debug_register(hdev, dev_name(&hdev->dev));
24618c2ecf20Sopenharmony_ci	ret = device_add(&hdev->dev);
24628c2ecf20Sopenharmony_ci	if (!ret)
24638c2ecf20Sopenharmony_ci		hdev->status |= HID_STAT_ADDED;
24648c2ecf20Sopenharmony_ci	else
24658c2ecf20Sopenharmony_ci		hid_debug_unregister(hdev);
24668c2ecf20Sopenharmony_ci
24678c2ecf20Sopenharmony_ci	return ret;
24688c2ecf20Sopenharmony_ci}
24698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_add_device);
24708c2ecf20Sopenharmony_ci
24718c2ecf20Sopenharmony_ci/**
24728c2ecf20Sopenharmony_ci * hid_allocate_device - allocate new hid device descriptor
24738c2ecf20Sopenharmony_ci *
24748c2ecf20Sopenharmony_ci * Allocate and initialize hid device, so that hid_destroy_device might be
24758c2ecf20Sopenharmony_ci * used to free it.
24768c2ecf20Sopenharmony_ci *
24778c2ecf20Sopenharmony_ci * New hid_device pointer is returned on success, otherwise ERR_PTR encoded
24788c2ecf20Sopenharmony_ci * error value.
24798c2ecf20Sopenharmony_ci */
24808c2ecf20Sopenharmony_cistruct hid_device *hid_allocate_device(void)
24818c2ecf20Sopenharmony_ci{
24828c2ecf20Sopenharmony_ci	struct hid_device *hdev;
24838c2ecf20Sopenharmony_ci	int ret = -ENOMEM;
24848c2ecf20Sopenharmony_ci
24858c2ecf20Sopenharmony_ci	hdev = kzalloc(sizeof(*hdev), GFP_KERNEL);
24868c2ecf20Sopenharmony_ci	if (hdev == NULL)
24878c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
24888c2ecf20Sopenharmony_ci
24898c2ecf20Sopenharmony_ci	device_initialize(&hdev->dev);
24908c2ecf20Sopenharmony_ci	hdev->dev.release = hid_device_release;
24918c2ecf20Sopenharmony_ci	hdev->dev.bus = &hid_bus_type;
24928c2ecf20Sopenharmony_ci	device_enable_async_suspend(&hdev->dev);
24938c2ecf20Sopenharmony_ci
24948c2ecf20Sopenharmony_ci	hid_close_report(hdev);
24958c2ecf20Sopenharmony_ci
24968c2ecf20Sopenharmony_ci	init_waitqueue_head(&hdev->debug_wait);
24978c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hdev->debug_list);
24988c2ecf20Sopenharmony_ci	spin_lock_init(&hdev->debug_list_lock);
24998c2ecf20Sopenharmony_ci	sema_init(&hdev->driver_input_lock, 1);
25008c2ecf20Sopenharmony_ci	mutex_init(&hdev->ll_open_lock);
25018c2ecf20Sopenharmony_ci	kref_init(&hdev->ref);
25028c2ecf20Sopenharmony_ci
25038c2ecf20Sopenharmony_ci	return hdev;
25048c2ecf20Sopenharmony_ci}
25058c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_allocate_device);
25068c2ecf20Sopenharmony_ci
25078c2ecf20Sopenharmony_cistatic void hid_remove_device(struct hid_device *hdev)
25088c2ecf20Sopenharmony_ci{
25098c2ecf20Sopenharmony_ci	if (hdev->status & HID_STAT_ADDED) {
25108c2ecf20Sopenharmony_ci		device_del(&hdev->dev);
25118c2ecf20Sopenharmony_ci		hid_debug_unregister(hdev);
25128c2ecf20Sopenharmony_ci		hdev->status &= ~HID_STAT_ADDED;
25138c2ecf20Sopenharmony_ci	}
25148c2ecf20Sopenharmony_ci	kfree(hdev->dev_rdesc);
25158c2ecf20Sopenharmony_ci	hdev->dev_rdesc = NULL;
25168c2ecf20Sopenharmony_ci	hdev->dev_rsize = 0;
25178c2ecf20Sopenharmony_ci}
25188c2ecf20Sopenharmony_ci
25198c2ecf20Sopenharmony_ci/**
25208c2ecf20Sopenharmony_ci * hid_destroy_device - free previously allocated device
25218c2ecf20Sopenharmony_ci *
25228c2ecf20Sopenharmony_ci * @hdev: hid device
25238c2ecf20Sopenharmony_ci *
25248c2ecf20Sopenharmony_ci * If you allocate hid_device through hid_allocate_device, you should ever
25258c2ecf20Sopenharmony_ci * free by this function.
25268c2ecf20Sopenharmony_ci */
25278c2ecf20Sopenharmony_civoid hid_destroy_device(struct hid_device *hdev)
25288c2ecf20Sopenharmony_ci{
25298c2ecf20Sopenharmony_ci	hid_remove_device(hdev);
25308c2ecf20Sopenharmony_ci	put_device(&hdev->dev);
25318c2ecf20Sopenharmony_ci}
25328c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_destroy_device);
25338c2ecf20Sopenharmony_ci
25348c2ecf20Sopenharmony_ci
25358c2ecf20Sopenharmony_cistatic int __hid_bus_reprobe_drivers(struct device *dev, void *data)
25368c2ecf20Sopenharmony_ci{
25378c2ecf20Sopenharmony_ci	struct hid_driver *hdrv = data;
25388c2ecf20Sopenharmony_ci	struct hid_device *hdev = to_hid_device(dev);
25398c2ecf20Sopenharmony_ci
25408c2ecf20Sopenharmony_ci	if (hdev->driver == hdrv &&
25418c2ecf20Sopenharmony_ci	    !hdrv->match(hdev, hid_ignore_special_drivers) &&
25428c2ecf20Sopenharmony_ci	    !test_and_set_bit(ffs(HID_STAT_REPROBED), &hdev->status))
25438c2ecf20Sopenharmony_ci		return device_reprobe(dev);
25448c2ecf20Sopenharmony_ci
25458c2ecf20Sopenharmony_ci	return 0;
25468c2ecf20Sopenharmony_ci}
25478c2ecf20Sopenharmony_ci
25488c2ecf20Sopenharmony_cistatic int __hid_bus_driver_added(struct device_driver *drv, void *data)
25498c2ecf20Sopenharmony_ci{
25508c2ecf20Sopenharmony_ci	struct hid_driver *hdrv = to_hid_driver(drv);
25518c2ecf20Sopenharmony_ci
25528c2ecf20Sopenharmony_ci	if (hdrv->match) {
25538c2ecf20Sopenharmony_ci		bus_for_each_dev(&hid_bus_type, NULL, hdrv,
25548c2ecf20Sopenharmony_ci				 __hid_bus_reprobe_drivers);
25558c2ecf20Sopenharmony_ci	}
25568c2ecf20Sopenharmony_ci
25578c2ecf20Sopenharmony_ci	return 0;
25588c2ecf20Sopenharmony_ci}
25598c2ecf20Sopenharmony_ci
25608c2ecf20Sopenharmony_cistatic int __bus_removed_driver(struct device_driver *drv, void *data)
25618c2ecf20Sopenharmony_ci{
25628c2ecf20Sopenharmony_ci	return bus_rescan_devices(&hid_bus_type);
25638c2ecf20Sopenharmony_ci}
25648c2ecf20Sopenharmony_ci
25658c2ecf20Sopenharmony_ciint __hid_register_driver(struct hid_driver *hdrv, struct module *owner,
25668c2ecf20Sopenharmony_ci		const char *mod_name)
25678c2ecf20Sopenharmony_ci{
25688c2ecf20Sopenharmony_ci	int ret;
25698c2ecf20Sopenharmony_ci
25708c2ecf20Sopenharmony_ci	hdrv->driver.name = hdrv->name;
25718c2ecf20Sopenharmony_ci	hdrv->driver.bus = &hid_bus_type;
25728c2ecf20Sopenharmony_ci	hdrv->driver.owner = owner;
25738c2ecf20Sopenharmony_ci	hdrv->driver.mod_name = mod_name;
25748c2ecf20Sopenharmony_ci
25758c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hdrv->dyn_list);
25768c2ecf20Sopenharmony_ci	spin_lock_init(&hdrv->dyn_lock);
25778c2ecf20Sopenharmony_ci
25788c2ecf20Sopenharmony_ci	ret = driver_register(&hdrv->driver);
25798c2ecf20Sopenharmony_ci
25808c2ecf20Sopenharmony_ci	if (ret == 0)
25818c2ecf20Sopenharmony_ci		bus_for_each_drv(&hid_bus_type, NULL, NULL,
25828c2ecf20Sopenharmony_ci				 __hid_bus_driver_added);
25838c2ecf20Sopenharmony_ci
25848c2ecf20Sopenharmony_ci	return ret;
25858c2ecf20Sopenharmony_ci}
25868c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__hid_register_driver);
25878c2ecf20Sopenharmony_ci
25888c2ecf20Sopenharmony_civoid hid_unregister_driver(struct hid_driver *hdrv)
25898c2ecf20Sopenharmony_ci{
25908c2ecf20Sopenharmony_ci	driver_unregister(&hdrv->driver);
25918c2ecf20Sopenharmony_ci	hid_free_dynids(hdrv);
25928c2ecf20Sopenharmony_ci
25938c2ecf20Sopenharmony_ci	bus_for_each_drv(&hid_bus_type, NULL, hdrv, __bus_removed_driver);
25948c2ecf20Sopenharmony_ci}
25958c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_unregister_driver);
25968c2ecf20Sopenharmony_ci
25978c2ecf20Sopenharmony_ciint hid_check_keys_pressed(struct hid_device *hid)
25988c2ecf20Sopenharmony_ci{
25998c2ecf20Sopenharmony_ci	struct hid_input *hidinput;
26008c2ecf20Sopenharmony_ci	int i;
26018c2ecf20Sopenharmony_ci
26028c2ecf20Sopenharmony_ci	if (!(hid->claimed & HID_CLAIMED_INPUT))
26038c2ecf20Sopenharmony_ci		return 0;
26048c2ecf20Sopenharmony_ci
26058c2ecf20Sopenharmony_ci	list_for_each_entry(hidinput, &hid->inputs, list) {
26068c2ecf20Sopenharmony_ci		for (i = 0; i < BITS_TO_LONGS(KEY_MAX); i++)
26078c2ecf20Sopenharmony_ci			if (hidinput->input->key[i])
26088c2ecf20Sopenharmony_ci				return 1;
26098c2ecf20Sopenharmony_ci	}
26108c2ecf20Sopenharmony_ci
26118c2ecf20Sopenharmony_ci	return 0;
26128c2ecf20Sopenharmony_ci}
26138c2ecf20Sopenharmony_ci
26148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(hid_check_keys_pressed);
26158c2ecf20Sopenharmony_ci
26168c2ecf20Sopenharmony_cistatic int __init hid_init(void)
26178c2ecf20Sopenharmony_ci{
26188c2ecf20Sopenharmony_ci	int ret;
26198c2ecf20Sopenharmony_ci
26208c2ecf20Sopenharmony_ci	if (hid_debug)
26218c2ecf20Sopenharmony_ci		pr_warn("hid_debug is now used solely for parser and driver debugging.\n"
26228c2ecf20Sopenharmony_ci			"debugfs is now used for inspecting the device (report descriptor, reports)\n");
26238c2ecf20Sopenharmony_ci
26248c2ecf20Sopenharmony_ci	ret = bus_register(&hid_bus_type);
26258c2ecf20Sopenharmony_ci	if (ret) {
26268c2ecf20Sopenharmony_ci		pr_err("can't register hid bus\n");
26278c2ecf20Sopenharmony_ci		goto err;
26288c2ecf20Sopenharmony_ci	}
26298c2ecf20Sopenharmony_ci
26308c2ecf20Sopenharmony_ci	ret = hidraw_init();
26318c2ecf20Sopenharmony_ci	if (ret)
26328c2ecf20Sopenharmony_ci		goto err_bus;
26338c2ecf20Sopenharmony_ci
26348c2ecf20Sopenharmony_ci	hid_debug_init();
26358c2ecf20Sopenharmony_ci
26368c2ecf20Sopenharmony_ci	return 0;
26378c2ecf20Sopenharmony_cierr_bus:
26388c2ecf20Sopenharmony_ci	bus_unregister(&hid_bus_type);
26398c2ecf20Sopenharmony_cierr:
26408c2ecf20Sopenharmony_ci	return ret;
26418c2ecf20Sopenharmony_ci}
26428c2ecf20Sopenharmony_ci
26438c2ecf20Sopenharmony_cistatic void __exit hid_exit(void)
26448c2ecf20Sopenharmony_ci{
26458c2ecf20Sopenharmony_ci	hid_debug_exit();
26468c2ecf20Sopenharmony_ci	hidraw_exit();
26478c2ecf20Sopenharmony_ci	bus_unregister(&hid_bus_type);
26488c2ecf20Sopenharmony_ci	hid_quirks_exit(HID_BUS_ANY);
26498c2ecf20Sopenharmony_ci}
26508c2ecf20Sopenharmony_ci
26518c2ecf20Sopenharmony_cimodule_init(hid_init);
26528c2ecf20Sopenharmony_cimodule_exit(hid_exit);
26538c2ecf20Sopenharmony_ci
26548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andreas Gal");
26558c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vojtech Pavlik");
26568c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jiri Kosina");
26578c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2658