xref: /onnv-gate/usr/src/uts/common/io/usb/clients/hidparser/hidparser.c (revision 9237:93fae803ca54)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53336Sgc161489  * Common Development and Distribution License (the "License").
63336Sgc161489  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*9237SStrony.Zhang@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
280Sstevel@tonic-gate #include <sys/usb/usba.h>
290Sstevel@tonic-gate #include <sys/usb/clients/hid/hid.h>
300Sstevel@tonic-gate #include <sys/usb/clients/hidparser/hidparser.h>
310Sstevel@tonic-gate #include <sys/usb/clients/hidparser/hid_parser_driver.h>
320Sstevel@tonic-gate #include <sys/usb/clients/hidparser/hidparser_impl.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * hidparser: Parser to generate parse tree for Report Descriptors
360Sstevel@tonic-gate  * in HID devices.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
39880Sfrits uint_t hparser_errmask = (uint_t)PRINT_MASK_ALL;
406898Sfb209375 uint_t	hparser_errlevel = (uint_t)USB_LOG_L1;
410Sstevel@tonic-gate static usb_log_handle_t hparser_log_handle;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * Array used to store corresponding strings for the
450Sstevel@tonic-gate  * different item types for debugging.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate char		*items[500];	/* Print items */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * modload support
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate extern struct mod_ops mod_miscops;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static struct modlmisc modlmisc	= {
550Sstevel@tonic-gate 	&mod_miscops,	/* Type	of module */
567015Sbc224572 	"HID Parser"
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static struct modlinkage modlinkage = {
600Sstevel@tonic-gate 	MODREV_1, (void	*)&modlmisc, NULL
610Sstevel@tonic-gate };
620Sstevel@tonic-gate 
630Sstevel@tonic-gate int
_init(void)640Sstevel@tonic-gate _init(void)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	int rval = mod_install(&modlinkage);
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (rval == 0) {
690Sstevel@tonic-gate 		hparser_log_handle = usb_alloc_log_hdl(NULL, "hidparser",
700Sstevel@tonic-gate 		    &hparser_errlevel, &hparser_errmask, NULL, 0);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	return (rval);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate int
_fini()770Sstevel@tonic-gate _fini()
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	int rval = mod_remove(&modlinkage);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (rval == 0) {
820Sstevel@tonic-gate 		usb_free_log_hdl(hparser_log_handle);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	return (rval);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int
_info(struct modinfo * modinfop)890Sstevel@tonic-gate _info(struct modinfo *modinfop)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
930Sstevel@tonic-gate }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * These functions are used internally in the parser.
970Sstevel@tonic-gate  * local declarations
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate static void			hidparser_scan(hidparser_tok_t	*);
1000Sstevel@tonic-gate static int			hidparser_Items(hidparser_tok_t *);
1010Sstevel@tonic-gate static int			hidparser_LocalItem(hidparser_tok_t *);
1020Sstevel@tonic-gate static int			hidparser_GlobalItem(hidparser_tok_t *);
1030Sstevel@tonic-gate static int			hidparser_ItemList(entity_item_t **,
1040Sstevel@tonic-gate 					hidparser_tok_t *);
1050Sstevel@tonic-gate static int			hidparser_ReportDescriptor(entity_item_t **,
1060Sstevel@tonic-gate 					hidparser_tok_t *);
1070Sstevel@tonic-gate static int			hidparser_ReportDescriptorDash(entity_item_t **,
1080Sstevel@tonic-gate 					hidparser_tok_t *);
1090Sstevel@tonic-gate static int			hidparser_MainItem(entity_item_t **,
1100Sstevel@tonic-gate 					hidparser_tok_t *);
1110Sstevel@tonic-gate static void			hidparser_free_attribute_list(
1120Sstevel@tonic-gate 					entity_attribute_t *);
1130Sstevel@tonic-gate static entity_item_t		*hidparser_allocate_entity(hidparser_tok_t *);
1140Sstevel@tonic-gate static void			hidparser_add_attribute(hidparser_tok_t	*);
1150Sstevel@tonic-gate static entity_attribute_t	*hidparser_cp_attribute_list(
1160Sstevel@tonic-gate 				entity_attribute_t *);
1170Sstevel@tonic-gate static entity_attribute_t	*hidparser_find_attribute_end(
1180Sstevel@tonic-gate 				entity_attribute_t *);
1190Sstevel@tonic-gate static entity_attribute_t	*hidparser_alloc_attrib_list(int);
1200Sstevel@tonic-gate static void			hidparser_report_err(int, int,
1210Sstevel@tonic-gate 					int, int, char *);
1220Sstevel@tonic-gate static int			hidparser_isvalid_item(int);
1230Sstevel@tonic-gate static entity_attribute_t	*hidparser_lookup_attribute(entity_item_t *,
1240Sstevel@tonic-gate 					int);
1250Sstevel@tonic-gate static void			hidparser_global_err_check(entity_item_t *);
1260Sstevel@tonic-gate static void			hidparser_local_err_check(entity_item_t *);
1270Sstevel@tonic-gate static void			hidparser_mainitem_err_check(entity_item_t *);
1280Sstevel@tonic-gate static unsigned int		hidparser_find_unsigned_val(
1290Sstevel@tonic-gate 					entity_attribute_t *);
1300Sstevel@tonic-gate static int			hidparser_find_signed_val(
1310Sstevel@tonic-gate 					entity_attribute_t *);
1320Sstevel@tonic-gate static void			hidparser_check_correspondence(
1330Sstevel@tonic-gate 					entity_item_t *, int, int, int,
1340Sstevel@tonic-gate 					int, char *, char *);
1350Sstevel@tonic-gate static void			hidparser_check_minmax_val(entity_item_t *,
1360Sstevel@tonic-gate 					int, int, int, int);
1370Sstevel@tonic-gate static void			hidparser_check_minmax_val_signed(
1380Sstevel@tonic-gate 					entity_item_t *,
1390Sstevel@tonic-gate 					int, int, int, int);
1400Sstevel@tonic-gate static void			hidparser_error_delim(entity_item_t *, int);
1410Sstevel@tonic-gate static int			hidparser_get_usage_attribute_report_des(
1420Sstevel@tonic-gate 					entity_item_t *,
1430Sstevel@tonic-gate 					uint32_t, uint32_t, uint32_t,
1440Sstevel@tonic-gate 					uint32_t, uint32_t, int32_t *);
1450Sstevel@tonic-gate static int			hidparser_get_packet_size_report_des(
1460Sstevel@tonic-gate 					entity_item_t *, uint32_t, uint32_t,
1470Sstevel@tonic-gate 					uint32_t *);
1480Sstevel@tonic-gate static int			hidparser_get_main_item_data_descr_main(
1490Sstevel@tonic-gate 					entity_item_t *, uint32_t,
1500Sstevel@tonic-gate 					uint32_t, uint32_t, uint32_t,
1510Sstevel@tonic-gate 					uint32_t	*);
1520Sstevel@tonic-gate static void			hidparser_print_entity(
1530Sstevel@tonic-gate 					entity_item_t *entity,
1540Sstevel@tonic-gate 					int indent_level);
1550Sstevel@tonic-gate static void			hidparser_print_this_attribute(
1560Sstevel@tonic-gate 					entity_attribute_t *attribute,
1570Sstevel@tonic-gate 					char *ident_space);
1580Sstevel@tonic-gate static int			hidparser_main(unsigned char *, size_t,
1590Sstevel@tonic-gate 					entity_item_t **);
1600Sstevel@tonic-gate static void			hidparser_initialize_items();
1610Sstevel@tonic-gate static void			hidparser_free_report_descr_handle(
1620Sstevel@tonic-gate 					entity_item_t *);
1630Sstevel@tonic-gate static int			hidparser_print_report_descr_handle(
1640Sstevel@tonic-gate 					entity_item_t	*handle,
1650Sstevel@tonic-gate 					int		indent_level);
1660Sstevel@tonic-gate static int			hidparser_get_usage_list_in_order_internal(
1670Sstevel@tonic-gate 					entity_item_t *parse_handle,
1680Sstevel@tonic-gate 					uint_t collection_usage,
1690Sstevel@tonic-gate 					uint_t report_id,
1700Sstevel@tonic-gate 					uint_t main_item_type,
1710Sstevel@tonic-gate 					hidparser_rpt_t *rpt);
1720Sstevel@tonic-gate static void			hidparser_fill_usage_info(
1730Sstevel@tonic-gate 					hidparser_usage_info_t *ui,
1740Sstevel@tonic-gate 					entity_attribute_t *attribute);
1750Sstevel@tonic-gate static int			hidparser_get_report_id_list_internal(
1760Sstevel@tonic-gate 					entity_item_t *parser_handle,
1770Sstevel@tonic-gate 					uint_t main_item_type,
1780Sstevel@tonic-gate 					hidparser_report_id_list_t *id_lst);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * The hidparser_lookup_first(N) of a non-terminal N is stored as an array of
1820Sstevel@tonic-gate  * integer tokens, terminated by 0. Right now there is only one element.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate static hidparser_terminal_t	first_Items[] = {
1850Sstevel@tonic-gate 	R_ITEM_USAGE_PAGE, R_ITEM_LOGICAL_MINIMUM, R_ITEM_LOGICAL_MAXIMUM, \
1860Sstevel@tonic-gate 	R_ITEM_PHYSICAL_MINIMUM, R_ITEM_PHYSICAL_MAXIMUM, R_ITEM_UNIT, \
1870Sstevel@tonic-gate 	R_ITEM_EXPONENT, R_ITEM_REPORT_SIZE, R_ITEM_REPORT_COUNT, \
1880Sstevel@tonic-gate 	R_ITEM_REPORT_ID, \
1890Sstevel@tonic-gate 	R_ITEM_USAGE, R_ITEM_USAGE_MIN, R_ITEM_USAGE_MAX, \
1900Sstevel@tonic-gate 	R_ITEM_DESIGNATOR_INDEX, \
1910Sstevel@tonic-gate 	R_ITEM_DESIGNATOR_MIN, R_ITEM_STRING_INDEX, R_ITEM_STRING_MIN, \
1920Sstevel@tonic-gate 	R_ITEM_STRING_MAX, \
1930Sstevel@tonic-gate 	R_ITEM_SET_DELIMITER, \
1940Sstevel@tonic-gate 	0
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * Each non-terminal is represented by a function. In a top-down parser,
2000Sstevel@tonic-gate  * whenever a non-terminal is encountered on the state diagram, the
2010Sstevel@tonic-gate  * corresponding function is called. Because of the grammar, there is NO
2020Sstevel@tonic-gate  * backtracking. If there is an error in the middle, the parser returns
2030Sstevel@tonic-gate  * HIDPARSER_FAILURE
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static hidparser_terminal_t *hid_first_list[] = {
2060Sstevel@tonic-gate 	first_Items
2070Sstevel@tonic-gate };
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * hidparser_parse_report_descriptor:
2120Sstevel@tonic-gate  *	Calls the main parser routine
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate int
hidparser_parse_report_descriptor(unsigned char * descriptor,size_t size,usb_hid_descr_t * hid_descriptor,hidparser_handle_t * parse_handle)2150Sstevel@tonic-gate hidparser_parse_report_descriptor(
2160Sstevel@tonic-gate 			unsigned char *descriptor,
2170Sstevel@tonic-gate 			size_t size,
2180Sstevel@tonic-gate 			usb_hid_descr_t *hid_descriptor,
2190Sstevel@tonic-gate 			hidparser_handle_t *parse_handle)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate 	int	error = 0;
2220Sstevel@tonic-gate 	entity_item_t	*root;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	hidparser_initialize_items();
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	error = hidparser_main(descriptor, size, &root);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	if (error != HIDPARSER_SUCCESS) {
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
2310Sstevel@tonic-gate 	} else {
2320Sstevel@tonic-gate 		*parse_handle = kmem_zalloc(
2330Sstevel@tonic-gate 		    sizeof (hidparser_handle), KM_SLEEP);
2340Sstevel@tonic-gate 		(*parse_handle)->hidparser_handle_hid_descr = hid_descriptor;
2350Sstevel@tonic-gate 		(*parse_handle)->hidparser_handle_parse_tree = root;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		return (HIDPARSER_SUCCESS);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /*
2430Sstevel@tonic-gate  * hidparser_free_report_descriptor_handle:
2440Sstevel@tonic-gate  *	Frees the parse_handle which consists of a pointer to the parse
2450Sstevel@tonic-gate  *	tree and a pointer to the Hid descriptor structure
2460Sstevel@tonic-gate  */
2470Sstevel@tonic-gate int
hidparser_free_report_descriptor_handle(hidparser_handle_t parse_handle)2480Sstevel@tonic-gate hidparser_free_report_descriptor_handle(hidparser_handle_t parse_handle)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	if (parse_handle != NULL) {
2510Sstevel@tonic-gate 		hidparser_free_report_descr_handle(
2520Sstevel@tonic-gate 		    parse_handle->hidparser_handle_parse_tree);
2530Sstevel@tonic-gate 		if (parse_handle != NULL) {
2540Sstevel@tonic-gate 			kmem_free(parse_handle, sizeof (hidparser_handle));
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate  * hidparser_get_country_code:
2640Sstevel@tonic-gate  *	Return the bCountryCode from the Hid Descriptor
2650Sstevel@tonic-gate  *	to the hid module.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate int
hidparser_get_country_code(hidparser_handle_t parser_handle,uint16_t * country_code)2680Sstevel@tonic-gate hidparser_get_country_code(hidparser_handle_t parser_handle,
2690Sstevel@tonic-gate 			uint16_t *country_code)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate 	if ((parser_handle == NULL) ||
2720Sstevel@tonic-gate 	    (parser_handle->hidparser_handle_hid_descr == NULL)) {
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	*country_code =
2780Sstevel@tonic-gate 	    parser_handle->hidparser_handle_hid_descr->bCountryCode;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * hidparser_get_packet_size:
2860Sstevel@tonic-gate  *	Get the packet size(sum of REPORT_SIZE * REPORT_COUNT)
2870Sstevel@tonic-gate  *	corresponding to a report id and an item type
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate int
hidparser_get_packet_size(hidparser_handle_t parser_handle,uint_t report_id,uint_t main_item_type,uint_t * size)2900Sstevel@tonic-gate hidparser_get_packet_size(hidparser_handle_t parser_handle,
2910Sstevel@tonic-gate 			uint_t report_id,
2920Sstevel@tonic-gate 			uint_t main_item_type,
2930Sstevel@tonic-gate 			uint_t *size)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	if ((parser_handle == NULL) || (parser_handle->
2960Sstevel@tonic-gate 	    hidparser_handle_parse_tree == NULL)) {
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
2990Sstevel@tonic-gate 	}
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	*size = 0;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	return (hidparser_get_packet_size_report_des(
3040Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
3050Sstevel@tonic-gate 	    report_id, main_item_type, size));
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate  * hidparser_get_packet_size_report_des:
3110Sstevel@tonic-gate  *	Get the packet size(sum of REPORT_SIZE * REPORT_COUNT)
3120Sstevel@tonic-gate  *	corresponding to a report id and an item type
3130Sstevel@tonic-gate  */
3140Sstevel@tonic-gate int
hidparser_get_packet_size_report_des(entity_item_t * parser_handle,uint32_t report_id,uint32_t main_item_type,uint32_t * size)3150Sstevel@tonic-gate hidparser_get_packet_size_report_des(entity_item_t *parser_handle,
3160Sstevel@tonic-gate 			uint32_t report_id,
3170Sstevel@tonic-gate 			uint32_t main_item_type,
3180Sstevel@tonic-gate 			uint32_t *size)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	entity_item_t	*current = parser_handle;
3210Sstevel@tonic-gate 	entity_attribute_t *attribute;
3220Sstevel@tonic-gate 	uint32_t	temp;
3230Sstevel@tonic-gate 	uchar_t 	foundsize, foundcount, foundreportid, right_report_id;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	foundsize = 0;
3260Sstevel@tonic-gate 	foundcount = 0;
3270Sstevel@tonic-gate 	right_report_id = 0;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	while (current) {
3300Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
3310Sstevel@tonic-gate 			(void) hidparser_get_packet_size_report_des(
3320Sstevel@tonic-gate 			    current->info.child, report_id, main_item_type,
3330Sstevel@tonic-gate 			    size);
3340Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
3350Sstevel@tonic-gate 			temp = 1;
3360Sstevel@tonic-gate 			foundsize = 0;
3370Sstevel@tonic-gate 			foundcount = 0;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 			foundreportid = 0;
3400Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
3410Sstevel@tonic-gate 			while (attribute != NULL) {
3420Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
3430Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
3440Sstevel@tonic-gate 					foundreportid = 1;
3450Sstevel@tonic-gate 					if ((attribute->
3460Sstevel@tonic-gate 					    entity_attribute_value[0]) ==
3470Sstevel@tonic-gate 					    report_id) {
3480Sstevel@tonic-gate 						right_report_id = 1;
3490Sstevel@tonic-gate 					}
3500Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
3510Sstevel@tonic-gate 				    R_ITEM_REPORT_SIZE) {
3520Sstevel@tonic-gate 					foundsize = 1;
3531190Sqz150045 					temp *= hidparser_find_unsigned_val(
3546898Sfb209375 					    attribute);
3550Sstevel@tonic-gate 					if (foundcount == 1) {
3560Sstevel@tonic-gate 						if (report_id &&
3570Sstevel@tonic-gate 						    right_report_id) {
3580Sstevel@tonic-gate 							break;
3590Sstevel@tonic-gate 						}
3600Sstevel@tonic-gate 					}
3610Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
3620Sstevel@tonic-gate 				    R_ITEM_REPORT_COUNT) {
3630Sstevel@tonic-gate 					foundcount = 1;
3641190Sqz150045 					temp *= hidparser_find_unsigned_val(
3656898Sfb209375 					    attribute);
3660Sstevel@tonic-gate 					if (foundsize == 1) {
3670Sstevel@tonic-gate 						if (report_id &&
3680Sstevel@tonic-gate 						    right_report_id) {
3690Sstevel@tonic-gate 							break;
3700Sstevel@tonic-gate 						}
3710Sstevel@tonic-gate 					}
3720Sstevel@tonic-gate 				}
3730Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
3740Sstevel@tonic-gate 			} /* end while */
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 			if (foundreportid) {
3770Sstevel@tonic-gate 				if (right_report_id) {
3780Sstevel@tonic-gate 					*size = *size + temp;
3790Sstevel@tonic-gate 				}
3800Sstevel@tonic-gate 			} else if (report_id == HID_REPORT_ID_UNDEFINED) {
3810Sstevel@tonic-gate 				/* Just sanity checking */
3820Sstevel@tonic-gate 				*size = *size + temp;
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 			right_report_id = 0;
3850Sstevel@tonic-gate 		} /* end else if */
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
3880Sstevel@tonic-gate 	} /* end while current */
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate  * hidparser_get_usage_attribute:
3970Sstevel@tonic-gate  *	Get the attribute value corresponding to a particular
3980Sstevel@tonic-gate  *	report id, main item and usage
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate int
hidparser_get_usage_attribute(hidparser_handle_t parser_handle,uint_t report_id,uint_t main_item_type,uint_t usage_page,uint_t usage_id,uint_t usage_attribute,int * usage_attribute_value)4010Sstevel@tonic-gate hidparser_get_usage_attribute(hidparser_handle_t parser_handle,
4020Sstevel@tonic-gate 			uint_t report_id,
4030Sstevel@tonic-gate 			uint_t main_item_type,
4040Sstevel@tonic-gate 			uint_t usage_page,
4050Sstevel@tonic-gate 			uint_t usage_id,
4060Sstevel@tonic-gate 			uint_t usage_attribute,
4070Sstevel@tonic-gate 			int *usage_attribute_value)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	return (hidparser_get_usage_attribute_report_des(
4110Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
4120Sstevel@tonic-gate 	    report_id, main_item_type, usage_page,
4130Sstevel@tonic-gate 	    usage_id, usage_attribute, usage_attribute_value));
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate /*
4180Sstevel@tonic-gate  * hidparser_get_usage_attribute_report_des:
4190Sstevel@tonic-gate  *	Called by the wrapper function hidparser_get_usage_attribute()
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate static int
hidparser_get_usage_attribute_report_des(entity_item_t * parser_handle,uint_t report_id,uint_t main_item_type,uint_t usage_page,uint_t usage_id,uint_t usage_attribute,int * usage_attribute_value)4220Sstevel@tonic-gate hidparser_get_usage_attribute_report_des(entity_item_t *parser_handle,
4230Sstevel@tonic-gate 			uint_t report_id,
4240Sstevel@tonic-gate 			uint_t main_item_type,
4250Sstevel@tonic-gate 			uint_t usage_page,
4260Sstevel@tonic-gate 			uint_t usage_id,
4270Sstevel@tonic-gate 			uint_t usage_attribute,
4280Sstevel@tonic-gate 			int *usage_attribute_value)
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
4320Sstevel@tonic-gate 	entity_attribute_t *attribute;
4330Sstevel@tonic-gate 	uchar_t found_page, found_ret_value, found_usage_id;
4340Sstevel@tonic-gate 	uchar_t foundreportid, right_report_id;
4350Sstevel@tonic-gate 	uint32_t usage;
4360Sstevel@tonic-gate 	short attvalue;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	found_page = 0;
4390Sstevel@tonic-gate 	found_ret_value = 0;
4400Sstevel@tonic-gate 	found_usage_id = 0;
4410Sstevel@tonic-gate 	foundreportid = 0;
4420Sstevel@tonic-gate 	right_report_id = 0;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	while (current) {
4450Sstevel@tonic-gate 		if (usage_id == HID_USAGE_UNDEFINED) {
4460Sstevel@tonic-gate 			found_usage_id = 1;
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 			if (hidparser_get_usage_attribute_report_des(
4510Sstevel@tonic-gate 			    current->info.child, report_id, main_item_type,
4520Sstevel@tonic-gate 			    usage_page, usage_id, usage_attribute,
4530Sstevel@tonic-gate 			    usage_attribute_value) ==
4540Sstevel@tonic-gate 			    HIDPARSER_SUCCESS) {
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
4570Sstevel@tonic-gate 			}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
4600Sstevel@tonic-gate 			/* Match Item Type */
4610Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 			while (attribute != NULL) {
4640Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
4650Sstevel@tonic-gate 				    R_ITEM_USAGE) {
4660Sstevel@tonic-gate 					usage = hidparser_find_unsigned_val(
4676898Sfb209375 					    attribute);
4680Sstevel@tonic-gate 					if (usage_id == HID_USAGE_ID(usage)) {
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 						found_usage_id = 1;
4710Sstevel@tonic-gate 					} else {
4720Sstevel@tonic-gate 						/*
4730Sstevel@tonic-gate 						 * If we are trying to find out
4740Sstevel@tonic-gate 						 * say, report size of usage =
4750Sstevel@tonic-gate 						 * 0, a m.i with a valid usage
4760Sstevel@tonic-gate 						 * will not contain that
4770Sstevel@tonic-gate 						 */
4780Sstevel@tonic-gate 						if (usage_id ==
4790Sstevel@tonic-gate 						    HID_USAGE_UNDEFINED) {
4800Sstevel@tonic-gate 							found_usage_id = 0;
4810Sstevel@tonic-gate 						}
4820Sstevel@tonic-gate 					}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 					if (found_usage_id && attribute->
4850Sstevel@tonic-gate 					    entity_attribute_length == 3) {
4860Sstevel@tonic-gate 						/*
4870Sstevel@tonic-gate 						 * This is an extended usage ie.
4880Sstevel@tonic-gate 						 * usage page in upper 16 bits
4890Sstevel@tonic-gate 						 * or-ed with usage in the lower
4900Sstevel@tonic-gate 						 * 16 bits.
4910Sstevel@tonic-gate 						 */
4920Sstevel@tonic-gate 						if (HID_USAGE_PAGE(usage) &&
4930Sstevel@tonic-gate 						    HID_USAGE_PAGE(usage) ==
4946898Sfb209375 						    usage_page) {
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 							found_page = 1;
4970Sstevel@tonic-gate 						} else {
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 							found_usage_id = 0;
5000Sstevel@tonic-gate 						}
5010Sstevel@tonic-gate 					}
5020Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
5030Sstevel@tonic-gate 				    R_ITEM_USAGE_PAGE) {
5040Sstevel@tonic-gate 					if (attribute->
5050Sstevel@tonic-gate 					    entity_attribute_value[0] ==
5060Sstevel@tonic-gate 					    usage_page) {
5070Sstevel@tonic-gate 						/* Match Usage Page */
5080Sstevel@tonic-gate 						found_page = 1;
5090Sstevel@tonic-gate 					}
5100Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
5110Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
5120Sstevel@tonic-gate 					foundreportid = 1;
5130Sstevel@tonic-gate 					if (attribute->
5140Sstevel@tonic-gate 					    entity_attribute_value[0] ==
5150Sstevel@tonic-gate 					    report_id) {
5160Sstevel@tonic-gate 						right_report_id = 1;
5170Sstevel@tonic-gate 					}
5180Sstevel@tonic-gate 				}
5190Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
5206898Sfb209375 				    usage_attribute) {
5210Sstevel@tonic-gate 					/* Match attribute */
5220Sstevel@tonic-gate 					found_ret_value = 1;
5230Sstevel@tonic-gate 					*usage_attribute_value =
5240Sstevel@tonic-gate 					attribute->entity_attribute_value[0];
5250Sstevel@tonic-gate 					if (attribute->
5260Sstevel@tonic-gate 					    entity_attribute_length == 2) {
5270Sstevel@tonic-gate 						attvalue =
5286898Sfb209375 						    (attribute->
5290Sstevel@tonic-gate 						    entity_attribute_value[0] &
5300Sstevel@tonic-gate 						    0xff) |
5310Sstevel@tonic-gate 						    (attribute->
5320Sstevel@tonic-gate 						    entity_attribute_value[1] <<
5330Sstevel@tonic-gate 						    8);
5340Sstevel@tonic-gate 						*usage_attribute_value =
5350Sstevel@tonic-gate 						    attvalue;
5360Sstevel@tonic-gate 					}
5370Sstevel@tonic-gate 				}
5380Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
5390Sstevel@tonic-gate 			}
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 			if (found_usage_id && found_page && found_ret_value) {
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 				if (foundreportid) {
5440Sstevel@tonic-gate 					if (right_report_id) {
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 						return (HIDPARSER_SUCCESS);
5470Sstevel@tonic-gate 					} else if (report_id ==
5480Sstevel@tonic-gate 					    HID_REPORT_ID_UNDEFINED) {
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 						return (HIDPARSER_SUCCESS);
5510Sstevel@tonic-gate 					}
5520Sstevel@tonic-gate 				} else {
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 					return (HIDPARSER_SUCCESS);
5550Sstevel@tonic-gate 				}
5560Sstevel@tonic-gate 			}
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		/*
5600Sstevel@tonic-gate 		 * search the next main item, right sibling of this one
5610Sstevel@tonic-gate 		 */
5620Sstevel@tonic-gate 		if (current->entity_item_right_sibling != NULL) {
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 			current = current->entity_item_right_sibling;
5650Sstevel@tonic-gate 			found_usage_id = found_page = found_ret_value = 0;
5660Sstevel@tonic-gate 			/* Don't change foundreportid */
5670Sstevel@tonic-gate 			right_report_id = 0;
5680Sstevel@tonic-gate 		} else {
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 			break;
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 	/* Don't give junk result */
5740Sstevel@tonic-gate 	*usage_attribute_value = 0;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	return (HIDPARSER_NOT_FOUND);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * hidparser_get_main_item_data_descr:
5820Sstevel@tonic-gate  *	Get the data value corresponding to a particular
5830Sstevel@tonic-gate  *	Main Item (Input, Output, Feature)
5840Sstevel@tonic-gate  */
5850Sstevel@tonic-gate int
hidparser_get_main_item_data_descr(hidparser_handle_t parser_handle,uint_t report_id,uint_t main_item_type,uint_t usage_page,uint_t usage_id,uint_t * main_item_descr_value)5860Sstevel@tonic-gate hidparser_get_main_item_data_descr(hidparser_handle_t parser_handle,
5870Sstevel@tonic-gate 			uint_t report_id,
5880Sstevel@tonic-gate 			uint_t main_item_type,
5890Sstevel@tonic-gate 			uint_t usage_page,
5900Sstevel@tonic-gate 			uint_t usage_id,
5910Sstevel@tonic-gate 			uint_t *main_item_descr_value)
5920Sstevel@tonic-gate {
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	return hidparser_get_main_item_data_descr_main(
5950Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
5960Sstevel@tonic-gate 	    report_id, main_item_type, usage_page, usage_id,
5970Sstevel@tonic-gate 	    main_item_descr_value);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate  * hidparser_get_main_item_data_descr_main:
6030Sstevel@tonic-gate  *	Called by the wrapper function hidparser_get_main_item_data_descr()
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate static int
hidparser_get_main_item_data_descr_main(entity_item_t * parser_handle,uint_t report_id,uint_t main_item_type,uint_t usage_page,uint_t usage_id,uint_t * main_item_descr_value)6060Sstevel@tonic-gate hidparser_get_main_item_data_descr_main(entity_item_t *parser_handle,
6070Sstevel@tonic-gate 			uint_t report_id,
6080Sstevel@tonic-gate 			uint_t main_item_type,
6090Sstevel@tonic-gate 			uint_t usage_page,
6100Sstevel@tonic-gate 			uint_t usage_id,
6110Sstevel@tonic-gate 			uint_t *main_item_descr_value)
6120Sstevel@tonic-gate {
6130Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
6140Sstevel@tonic-gate 	entity_attribute_t *attribute;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	uchar_t found_page, found_usage_id;
6170Sstevel@tonic-gate 	uchar_t foundreportid, right_report_id;
6180Sstevel@tonic-gate 	uint32_t usage;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	found_page = 0;
6210Sstevel@tonic-gate 	found_usage_id = 0;
6220Sstevel@tonic-gate 	foundreportid = 0;
6230Sstevel@tonic-gate 	right_report_id = 0;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	while (current) {
6260Sstevel@tonic-gate 		if (usage_id == HID_USAGE_UNDEFINED) {
6270Sstevel@tonic-gate 			found_usage_id = 1;
6280Sstevel@tonic-gate 		}
6290Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 			if (hidparser_get_main_item_data_descr_main(
6320Sstevel@tonic-gate 			    current->info.child, report_id, main_item_type,
6330Sstevel@tonic-gate 			    usage_page, usage_id, main_item_descr_value) ==
6340Sstevel@tonic-gate 			    HIDPARSER_SUCCESS) {
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
6370Sstevel@tonic-gate 			}
6380Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
6390Sstevel@tonic-gate 			/* Match Item Type */
6400Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 			if (report_id == HID_REPORT_ID_UNDEFINED) {
6430Sstevel@tonic-gate 				foundreportid = right_report_id = 1;
6440Sstevel@tonic-gate 			}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 			while (attribute != NULL) {
6470Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
6480Sstevel@tonic-gate 				    R_ITEM_USAGE) {
6490Sstevel@tonic-gate 					usage = hidparser_find_unsigned_val(
6506898Sfb209375 					    attribute);
6510Sstevel@tonic-gate 					if (usage_id == HID_USAGE_ID(usage)) {
6520Sstevel@tonic-gate 						found_usage_id = 1;
6530Sstevel@tonic-gate 						if (attribute->
6540Sstevel@tonic-gate 						    entity_attribute_length ==
6550Sstevel@tonic-gate 						    3) {
6560Sstevel@tonic-gate 							if (HID_USAGE_PAGE(
6576898Sfb209375 							    usage) &&
6580Sstevel@tonic-gate 							    HID_USAGE_PAGE(
6596898Sfb209375 							    usage) ==
6600Sstevel@tonic-gate 							    usage_page) {
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 								found_page = 1;
6630Sstevel@tonic-gate 							} else {
6640Sstevel@tonic-gate 
6656898Sfb209375 							found_usage_id = 0;
6660Sstevel@tonic-gate 							}
6670Sstevel@tonic-gate 						}
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 						if (found_usage_id &&
6700Sstevel@tonic-gate 						    found_page &&
6710Sstevel@tonic-gate 						    foundreportid &&
6720Sstevel@tonic-gate 						    right_report_id) {
6736898Sfb209375 						*main_item_descr_value =
6746898Sfb209375 						    current->
6756898Sfb209375 						    entity_item_params[0];
6766898Sfb209375 						break;
6770Sstevel@tonic-gate 						}
6780Sstevel@tonic-gate 					}
6790Sstevel@tonic-gate 				} else if ((attribute->entity_attribute_tag ==
6800Sstevel@tonic-gate 				    R_ITEM_USAGE_PAGE) &&
6810Sstevel@tonic-gate 				    (attribute->entity_attribute_value[0] ==
6820Sstevel@tonic-gate 				    usage_page)) {
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 					/* Match Usage Page */
6850Sstevel@tonic-gate 					found_page = 1;
6860Sstevel@tonic-gate 					if (found_usage_id && foundreportid &&
6870Sstevel@tonic-gate 					    right_report_id) {
6880Sstevel@tonic-gate 						*main_item_descr_value =
6890Sstevel@tonic-gate 						    current->
6900Sstevel@tonic-gate 						    entity_item_params[0];
6910Sstevel@tonic-gate 						break;
6920Sstevel@tonic-gate 					}
6930Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
6940Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
6950Sstevel@tonic-gate 					foundreportid = 1;
6960Sstevel@tonic-gate 					if (attribute->
6970Sstevel@tonic-gate 					    entity_attribute_value[0] ==
6980Sstevel@tonic-gate 					    report_id) {
6990Sstevel@tonic-gate 						right_report_id = 1;
7000Sstevel@tonic-gate 					} else {
7010Sstevel@tonic-gate 						break;
7020Sstevel@tonic-gate 					}
7030Sstevel@tonic-gate 				}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
7060Sstevel@tonic-gate 			}
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 			if (foundreportid) {
7090Sstevel@tonic-gate 				if (right_report_id) {
7100Sstevel@tonic-gate 					if (found_usage_id && found_page) {
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 						return (HIDPARSER_SUCCESS);
7130Sstevel@tonic-gate 					}
7140Sstevel@tonic-gate 				}
7150Sstevel@tonic-gate 			}
7160Sstevel@tonic-gate 		}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		/*
7190Sstevel@tonic-gate 		 * search the next main item, right sibling of this one
7200Sstevel@tonic-gate 		 */
7210Sstevel@tonic-gate 		if (current->entity_item_right_sibling != NULL) {
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 			current = current->entity_item_right_sibling;
7240Sstevel@tonic-gate 			found_page = found_usage_id = right_report_id = 0;
7250Sstevel@tonic-gate 		} else {
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 			break;
7280Sstevel@tonic-gate 		}
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	*main_item_descr_value = (uint_t)NULL;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	return (HIDPARSER_NOT_FOUND);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
736*9237SStrony.Zhang@Sun.COM /*
737*9237SStrony.Zhang@Sun.COM  * hidparser_lookup_usage_collection:
738*9237SStrony.Zhang@Sun.COM  *	Look up the collection specified by the usage page and usage id
739*9237SStrony.Zhang@Sun.COM  */
740*9237SStrony.Zhang@Sun.COM int
hidparser_lookup_usage_collection(hidparser_handle_t parse_handle,uint_t lusage_page,uint_t lusage_id)741*9237SStrony.Zhang@Sun.COM hidparser_lookup_usage_collection(hidparser_handle_t parse_handle,
742*9237SStrony.Zhang@Sun.COM 			uint_t lusage_page,
743*9237SStrony.Zhang@Sun.COM 			uint_t lusage_id)
744*9237SStrony.Zhang@Sun.COM {
745*9237SStrony.Zhang@Sun.COM 	entity_item_t *current;
746*9237SStrony.Zhang@Sun.COM 	entity_attribute_t *attribute;
747*9237SStrony.Zhang@Sun.COM 	int found_usage_id = 0;
748*9237SStrony.Zhang@Sun.COM 	int found_page = 0;
749*9237SStrony.Zhang@Sun.COM 	uint32_t usage;
750*9237SStrony.Zhang@Sun.COM 	uint_t usage_page;
751*9237SStrony.Zhang@Sun.COM 	uint_t usage_id;
752*9237SStrony.Zhang@Sun.COM 
753*9237SStrony.Zhang@Sun.COM 	if ((parse_handle == NULL) ||
754*9237SStrony.Zhang@Sun.COM 	    (parse_handle->hidparser_handle_parse_tree == NULL))
755*9237SStrony.Zhang@Sun.COM 		return (HIDPARSER_FAILURE);
756*9237SStrony.Zhang@Sun.COM 
757*9237SStrony.Zhang@Sun.COM 	current = parse_handle->hidparser_handle_parse_tree;
758*9237SStrony.Zhang@Sun.COM 	while (current != NULL) {
759*9237SStrony.Zhang@Sun.COM 
760*9237SStrony.Zhang@Sun.COM 		if (current->entity_item_type != R_ITEM_COLLECTION) {
761*9237SStrony.Zhang@Sun.COM 			current = current->entity_item_right_sibling;
762*9237SStrony.Zhang@Sun.COM 			continue;
763*9237SStrony.Zhang@Sun.COM 		}
764*9237SStrony.Zhang@Sun.COM 
765*9237SStrony.Zhang@Sun.COM 		attribute = current->entity_item_attributes;
766*9237SStrony.Zhang@Sun.COM 		found_usage_id = 0;
767*9237SStrony.Zhang@Sun.COM 		found_page = 0;
768*9237SStrony.Zhang@Sun.COM 
769*9237SStrony.Zhang@Sun.COM 		while (attribute != NULL) {
770*9237SStrony.Zhang@Sun.COM 			if (attribute->entity_attribute_tag == R_ITEM_USAGE) {
771*9237SStrony.Zhang@Sun.COM 				found_usage_id = 1;
772*9237SStrony.Zhang@Sun.COM 				usage = hidparser_find_unsigned_val(attribute);
773*9237SStrony.Zhang@Sun.COM 				usage_id = HID_USAGE_ID(usage);
774*9237SStrony.Zhang@Sun.COM 				if (attribute->entity_attribute_length == 3) {
775*9237SStrony.Zhang@Sun.COM 					if (HID_USAGE_PAGE(usage)) {
776*9237SStrony.Zhang@Sun.COM 						found_page = 1;
777*9237SStrony.Zhang@Sun.COM 						usage_page =
778*9237SStrony.Zhang@Sun.COM 						    HID_USAGE_PAGE(usage);
779*9237SStrony.Zhang@Sun.COM 					}
780*9237SStrony.Zhang@Sun.COM 				}
781*9237SStrony.Zhang@Sun.COM 				if (found_page) {
782*9237SStrony.Zhang@Sun.COM 					goto check_usage;
783*9237SStrony.Zhang@Sun.COM 				}
784*9237SStrony.Zhang@Sun.COM 			} else if (attribute->entity_attribute_tag ==
785*9237SStrony.Zhang@Sun.COM 			    R_ITEM_USAGE_PAGE) {
786*9237SStrony.Zhang@Sun.COM 				found_page = 1;
787*9237SStrony.Zhang@Sun.COM 				usage_page =
788*9237SStrony.Zhang@Sun.COM 				    attribute->entity_attribute_value[0];
789*9237SStrony.Zhang@Sun.COM 				if (found_usage_id) {
790*9237SStrony.Zhang@Sun.COM 					goto check_usage;
791*9237SStrony.Zhang@Sun.COM 				}
792*9237SStrony.Zhang@Sun.COM 			}
793*9237SStrony.Zhang@Sun.COM 			attribute = attribute->entity_attribute_next;
794*9237SStrony.Zhang@Sun.COM 		}
795*9237SStrony.Zhang@Sun.COM check_usage:
796*9237SStrony.Zhang@Sun.COM 		if ((usage_page == lusage_page) && (usage_id == lusage_id))
797*9237SStrony.Zhang@Sun.COM 			return (HIDPARSER_SUCCESS);
798*9237SStrony.Zhang@Sun.COM 		else
799*9237SStrony.Zhang@Sun.COM 			current = current->entity_item_right_sibling;
800*9237SStrony.Zhang@Sun.COM 	}
801*9237SStrony.Zhang@Sun.COM 
802*9237SStrony.Zhang@Sun.COM 	return (HIDPARSER_FAILURE);
803*9237SStrony.Zhang@Sun.COM }
804*9237SStrony.Zhang@Sun.COM 
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /*
8070Sstevel@tonic-gate  * hidparser_get_top_level_collection_usage:
8080Sstevel@tonic-gate  *	Get the usage page and usage for the top level collection item
8090Sstevel@tonic-gate  */
8100Sstevel@tonic-gate int
hidparser_get_top_level_collection_usage(hidparser_handle_t parse_handle,uint_t * usage_page,uint_t * usage_id)8110Sstevel@tonic-gate hidparser_get_top_level_collection_usage(hidparser_handle_t parse_handle,
8120Sstevel@tonic-gate 			uint_t *usage_page,
8130Sstevel@tonic-gate 			uint_t *usage_id)
8140Sstevel@tonic-gate {
8150Sstevel@tonic-gate 	entity_item_t *current;
8160Sstevel@tonic-gate 	entity_attribute_t *attribute;
8170Sstevel@tonic-gate 	int found_usage_id = 0;
8180Sstevel@tonic-gate 	int found_page = 0;
8190Sstevel@tonic-gate 	uint32_t usage;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if ((parse_handle == NULL) ||
8220Sstevel@tonic-gate 	    (parse_handle->hidparser_handle_parse_tree == NULL))
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	current = parse_handle->hidparser_handle_parse_tree;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if (current->entity_item_type != R_ITEM_COLLECTION) {
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 	attribute = current->entity_item_attributes;
8330Sstevel@tonic-gate 	while (attribute != NULL) {
8340Sstevel@tonic-gate 		if (attribute->entity_attribute_tag == R_ITEM_USAGE) {
8350Sstevel@tonic-gate 			found_usage_id = 1;
8360Sstevel@tonic-gate 			usage = hidparser_find_unsigned_val(attribute);
8370Sstevel@tonic-gate 			*usage_id = HID_USAGE_ID(usage);
8380Sstevel@tonic-gate 			if (attribute->entity_attribute_length == 3) {
8390Sstevel@tonic-gate 				if (HID_USAGE_PAGE(usage)) {
8400Sstevel@tonic-gate 					found_page = 1;
8410Sstevel@tonic-gate 					*usage_page = HID_USAGE_PAGE(usage);
8420Sstevel@tonic-gate 				}
8430Sstevel@tonic-gate 			}
8440Sstevel@tonic-gate 			if (found_usage_id && found_page) {
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
8470Sstevel@tonic-gate 			}
8480Sstevel@tonic-gate 		} else if (attribute->entity_attribute_tag ==
8496898Sfb209375 		    R_ITEM_USAGE_PAGE) {
8500Sstevel@tonic-gate 			found_page = 1;
8510Sstevel@tonic-gate 			*usage_page = attribute->entity_attribute_value[0];
8520Sstevel@tonic-gate 			if (found_usage_id && found_page) {
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
8550Sstevel@tonic-gate 			}
8560Sstevel@tonic-gate 		}
8570Sstevel@tonic-gate 		attribute = attribute->entity_attribute_next;
8580Sstevel@tonic-gate 	}
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*
8650Sstevel@tonic-gate  * hidparser_get_usage_list_in_order:
8660Sstevel@tonic-gate  *	Find all the usages corresponding to a main item and report id.
8670Sstevel@tonic-gate  *	Note that only short items are supported.
8680Sstevel@tonic-gate  *
8690Sstevel@tonic-gate  * Arguments:
8700Sstevel@tonic-gate  *	parser_handle:
8710Sstevel@tonic-gate  *		hid parser handle
8720Sstevel@tonic-gate  *	report id:
8730Sstevel@tonic-gate  *		report id of the particular report where the usages belong to
8740Sstevel@tonic-gate  *	main_item_type:
8750Sstevel@tonic-gate  *		type of report, either Input, Output, or Feature
8760Sstevel@tonic-gate  *	usage_list:
8770Sstevel@tonic-gate  *		Filled in with the pointer to the first element of the
8780Sstevel@tonic-gate  *		usage list
8790Sstevel@tonic-gate  *
8800Sstevel@tonic-gate  * Return values:
8810Sstevel@tonic-gate  *	HIDPARSER_SUCCESS - returned success
8820Sstevel@tonic-gate  *	HIDPARSER_NOT_FOUND - usage specified by the parameters was not found
8830Sstevel@tonic-gate  *	HIDPARSER_FAILURE - unspecified failure
8840Sstevel@tonic-gate  */
8850Sstevel@tonic-gate int
hidparser_get_usage_list_in_order(hidparser_handle_t parser_handle,uint_t report_id,uint_t main_item_type,hidparser_rpt_t * rpt)8860Sstevel@tonic-gate hidparser_get_usage_list_in_order(hidparser_handle_t parser_handle,
8870Sstevel@tonic-gate 			uint_t report_id,
8880Sstevel@tonic-gate 			uint_t main_item_type,
8890Sstevel@tonic-gate 			hidparser_rpt_t *rpt)
8900Sstevel@tonic-gate {
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if ((parser_handle == NULL) ||
8930Sstevel@tonic-gate 	    (parser_handle->hidparser_handle_parse_tree == NULL)) {
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	rpt->no_of_usages = 0;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	return (hidparser_get_usage_list_in_order_internal(
9010Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree, HID_USAGE_UNDEFINED,
9020Sstevel@tonic-gate 	    report_id, main_item_type, rpt));
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate static int
hidparser_get_usage_list_in_order_internal(entity_item_t * parser_handle,uint_t collection_usage,uint_t report_id,uint_t main_item_type,hidparser_rpt_t * rpt)9070Sstevel@tonic-gate hidparser_get_usage_list_in_order_internal(entity_item_t *parser_handle,
9080Sstevel@tonic-gate 			uint_t collection_usage,
9090Sstevel@tonic-gate 			uint_t report_id,
9100Sstevel@tonic-gate 			uint_t main_item_type,
9110Sstevel@tonic-gate 			hidparser_rpt_t *rpt)
9120Sstevel@tonic-gate {
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	/* setup wrapper function */
9150Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
9160Sstevel@tonic-gate 	entity_attribute_t *attribute;
9170Sstevel@tonic-gate 	uchar_t foundreportid, right_report_id, valid_usage;
9183336Sgc161489 	uchar_t found_usage_min, found_usage_max, found_usage;
9193336Sgc161489 	int i, j;
9200Sstevel@tonic-gate 	int rval;
9213336Sgc161489 	uint32_t usage, usage_min, usage_max, usage_id[USAGE_MAX];
9223336Sgc161489 	hidparser_usage_info_t *ui;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	found_usage_min = 0;
9250Sstevel@tonic-gate 	found_usage_max = 0;
9260Sstevel@tonic-gate 	foundreportid = 0;
9270Sstevel@tonic-gate 	right_report_id = 0;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	while (current) {
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 			/*
9340Sstevel@tonic-gate 			 * find collection usage information for this
9350Sstevel@tonic-gate 			 * collection
9360Sstevel@tonic-gate 			 */
9370Sstevel@tonic-gate 			valid_usage = 0;
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 			while (attribute != NULL) {
9420Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
9430Sstevel@tonic-gate 				    R_ITEM_USAGE) {
9440Sstevel@tonic-gate 					usage = hidparser_find_unsigned_val(
9450Sstevel@tonic-gate 					    attribute);
9460Sstevel@tonic-gate 					valid_usage = 1;
9470Sstevel@tonic-gate 				}
9480Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
9490Sstevel@tonic-gate 			}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 			if (!valid_usage) {
9520Sstevel@tonic-gate 				usage = HID_USAGE_UNDEFINED;
9530Sstevel@tonic-gate 			}
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 			rval = hidparser_get_usage_list_in_order_internal(
9560Sstevel@tonic-gate 			    current->info.child, usage,
9570Sstevel@tonic-gate 			    report_id, main_item_type, rpt);
9580Sstevel@tonic-gate 			if (rval != HIDPARSER_SUCCESS) {
9590Sstevel@tonic-gate 
9606898Sfb209375 				return (rval);
9610Sstevel@tonic-gate 			}
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
9640Sstevel@tonic-gate 			/* Match Item Type */
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 			foundreportid = 0;
9670Sstevel@tonic-gate 			right_report_id = 0;
9680Sstevel@tonic-gate 			found_usage_min = 0;
9690Sstevel@tonic-gate 			found_usage_max = 0;
9703336Sgc161489 			found_usage = 0;
9710Sstevel@tonic-gate 			valid_usage = 0;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 			while (attribute != NULL) {
9760Sstevel@tonic-gate 				switch (attribute->entity_attribute_tag) {
9770Sstevel@tonic-gate 				case R_ITEM_REPORT_ID:
9780Sstevel@tonic-gate 					foundreportid = 1;
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 					if (attribute->
9810Sstevel@tonic-gate 					    entity_attribute_value[0] ==
9820Sstevel@tonic-gate 					    report_id) {
9830Sstevel@tonic-gate 						right_report_id = 1;
9840Sstevel@tonic-gate 					} else {
9850Sstevel@tonic-gate 						/* different report id */
9860Sstevel@tonic-gate 						valid_usage = 1;
9870Sstevel@tonic-gate 					}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 					break;
9903336Sgc161489 				case R_ITEM_USAGE:
9913336Sgc161489 					if (found_usage >= USAGE_MAX) {
9923336Sgc161489 
9933336Sgc161489 						return (HIDPARSER_FAILURE);
9943336Sgc161489 					}
9953336Sgc161489 					usage = hidparser_find_unsigned_val(
9963336Sgc161489 					    attribute);
9973336Sgc161489 					if (usage) {
9983336Sgc161489 						usage_id[found_usage] = usage;
9993336Sgc161489 						found_usage++;
10003336Sgc161489 					}
10013336Sgc161489 
10023336Sgc161489 					break;
10030Sstevel@tonic-gate 				case R_ITEM_USAGE_MIN:
10040Sstevel@tonic-gate 					found_usage_min = 1;
10050Sstevel@tonic-gate 					usage_min = hidparser_find_unsigned_val(
10060Sstevel@tonic-gate 					    attribute);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 					break;
10090Sstevel@tonic-gate 				case R_ITEM_USAGE_MAX:
10100Sstevel@tonic-gate 					found_usage_max = 1;
10110Sstevel@tonic-gate 					usage_max = hidparser_find_unsigned_val(
10120Sstevel@tonic-gate 					    attribute);
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 					break;
10150Sstevel@tonic-gate 				case R_ITEM_SET_DELIMITER:
10163336Sgc161489 					/* skip over alternate usages */
10173336Sgc161489 					do {
10183336Sgc161489 						attribute = attribute->
10193336Sgc161489 						    entity_attribute_next;
10203336Sgc161489 					} while (attribute->
10213336Sgc161489 					    entity_attribute_tag !=
10223336Sgc161489 					    R_ITEM_SET_DELIMITER);
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 					break;
10250Sstevel@tonic-gate 				}
10260Sstevel@tonic-gate 
10273336Sgc161489 				attribute = attribute->entity_attribute_next;
10283336Sgc161489 			}
10293336Sgc161489 
10303336Sgc161489 			/*
10313336Sgc161489 			 * If we have a report id match (or report ids
10323336Sgc161489 			 * are not present), and have a usage item or
10333336Sgc161489 			 * usage min&max, put the usage item into the
10343336Sgc161489 			 * list. Don't put undefined usage items
10353336Sgc161489 			 * (HID_USAGE_UNDEFINED, 0) into the list;
10363336Sgc161489 			 * a 0 usage item is used to match padding
10373336Sgc161489 			 * fields that don't have an attached usage.
10383336Sgc161489 			 */
10393336Sgc161489 			if (!foundreportid ||
10403336Sgc161489 			    (foundreportid && right_report_id)) {
10413336Sgc161489 
10423336Sgc161489 				for (j = 0; j < found_usage; j++) {
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 					/* Put in usage list */
10450Sstevel@tonic-gate 					if (rpt->no_of_usages >= USAGE_MAX) {
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 						return (HIDPARSER_FAILURE);
10480Sstevel@tonic-gate 					}
10490Sstevel@tonic-gate 
10503336Sgc161489 					i = rpt->no_of_usages++;
10513336Sgc161489 					ui = &(rpt->usage_descr[i]);
10523336Sgc161489 
10533336Sgc161489 					hidparser_fill_usage_info(ui,
10540Sstevel@tonic-gate 					    current->entity_item_attributes);
10550Sstevel@tonic-gate 
10563336Sgc161489 					ui->rptcnt /= found_usage;
10573336Sgc161489 					ui->collection_usage = collection_usage;
10583336Sgc161489 					ui->usage_id = HID_USAGE_ID(
10593336Sgc161489 					    usage_id[j]);
10603336Sgc161489 
10613336Sgc161489 					/*
10623336Sgc161489 					 * This is an extended usage ie.
10633336Sgc161489 					 * usage page in upper 16 bits
10643336Sgc161489 					 * or-ed with usage in the lower
10653336Sgc161489 					 * 16 bits.
10663336Sgc161489 					 */
10673336Sgc161489 					if (usage_id[j] >> 16) {
10683336Sgc161489 						ui->usage_page =
10693336Sgc161489 						    HID_USAGE_PAGE(usage_id[j]);
10703336Sgc161489 					}
10713336Sgc161489 
10720Sstevel@tonic-gate 					rpt->report_id = report_id;
10730Sstevel@tonic-gate 					valid_usage = 1;
10743336Sgc161489 				}
10753336Sgc161489 
10763336Sgc161489 				if (found_usage_min && found_usage_max) {
10773336Sgc161489 
10783336Sgc161489 					/* Put in usage list */
10793336Sgc161489 					if (rpt->no_of_usages >= USAGE_MAX) {
10803336Sgc161489 
10813336Sgc161489 						return (HIDPARSER_FAILURE);
10820Sstevel@tonic-gate 					}
10830Sstevel@tonic-gate 
10843336Sgc161489 					if (found_usage) {
10853336Sgc161489 
10863336Sgc161489 						/* handle duplication */
10873336Sgc161489 						ui->usage_min = HID_USAGE_ID(
10883336Sgc161489 						    usage_min);
10893336Sgc161489 						ui->usage_max = HID_USAGE_ID(
10903336Sgc161489 						    usage_max);
10913336Sgc161489 					} else {
10923336Sgc161489 						i = rpt->no_of_usages++;
10933336Sgc161489 						ui = &(rpt->usage_descr[i]);
10943336Sgc161489 
10953336Sgc161489 						hidparser_fill_usage_info(ui,
10963336Sgc161489 						    current->
10973336Sgc161489 						    entity_item_attributes);
10983336Sgc161489 
10993336Sgc161489 						ui->collection_usage =
11003336Sgc161489 						    collection_usage;
11013336Sgc161489 						ui->usage_min = HID_USAGE_ID(
11023336Sgc161489 						    usage_min);
11033336Sgc161489 						ui->usage_max = HID_USAGE_ID(
11043336Sgc161489 						    usage_max);
11053336Sgc161489 
11063336Sgc161489 						rpt->report_id = report_id;
11073336Sgc161489 						valid_usage = 1;
11083336Sgc161489 					}
11093336Sgc161489 
11103336Sgc161489 					/*
11113336Sgc161489 					 * This is an extended usage ie.
11123336Sgc161489 					 * usage page in upper 16 bits
11133336Sgc161489 					 * or-ed with usage_max in the lower
11143336Sgc161489 					 * 16 bits.
11153336Sgc161489 					 */
11163336Sgc161489 					if (usage_max >> 16) {
11173336Sgc161489 						ui->usage_page =
11183336Sgc161489 						    HID_USAGE_PAGE(usage_max);
11193336Sgc161489 					}
11200Sstevel@tonic-gate 				}
11210Sstevel@tonic-gate 			}
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 			/*
11240Sstevel@tonic-gate 			 * This main item contains no usage
11250Sstevel@tonic-gate 			 * Fill in with usage "UNDEFINED".
11260Sstevel@tonic-gate 			 * If report id is valid, only the
11270Sstevel@tonic-gate 			 * main item with matched report id
11280Sstevel@tonic-gate 			 * can be filled in.
11290Sstevel@tonic-gate 			 */
11300Sstevel@tonic-gate 			if (!valid_usage) {
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 				if (rpt->no_of_usages >= USAGE_MAX) {
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 					return (HIDPARSER_FAILURE);
11350Sstevel@tonic-gate 				}
11360Sstevel@tonic-gate 
11373336Sgc161489 				i = rpt->no_of_usages++;
11383336Sgc161489 				ui = &(rpt->usage_descr[i]);
11393336Sgc161489 
11403336Sgc161489 				hidparser_fill_usage_info(ui,
11410Sstevel@tonic-gate 				    current->entity_item_attributes);
11420Sstevel@tonic-gate 
11433336Sgc161489 				ui->collection_usage = collection_usage;
11443336Sgc161489 				ui->usage_id = HID_USAGE_UNDEFINED;
11453336Sgc161489 
11460Sstevel@tonic-gate 				rpt->report_id = report_id;
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 		}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	} /* end while current */
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
11560Sstevel@tonic-gate }
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate  * hidparser_fill_usage_info():
11610Sstevel@tonic-gate  *	Fill in the mandatory item information for a main item.
11620Sstevel@tonic-gate  *	See HID 6.2.2.
11630Sstevel@tonic-gate  */
11640Sstevel@tonic-gate static void
hidparser_fill_usage_info(hidparser_usage_info_t * ui,entity_attribute_t * attribute)11650Sstevel@tonic-gate hidparser_fill_usage_info(hidparser_usage_info_t *ui,
11660Sstevel@tonic-gate 			entity_attribute_t *attribute)
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	bzero(ui, sizeof (*ui));
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	while (attribute) {
11710Sstevel@tonic-gate 		switch (attribute->entity_attribute_tag) {
11720Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MINIMUM:
11730Sstevel@tonic-gate 			ui->lmin = hidparser_find_signed_val(attribute);
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 			break;
11760Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MAXIMUM:
11770Sstevel@tonic-gate 			ui->lmax = hidparser_find_signed_val(attribute);
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			break;
11800Sstevel@tonic-gate 		case R_ITEM_REPORT_COUNT:
11810Sstevel@tonic-gate 			ui->rptcnt = hidparser_find_unsigned_val(attribute);
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate 			break;
11840Sstevel@tonic-gate 		case R_ITEM_REPORT_SIZE:
11850Sstevel@tonic-gate 			ui->rptsz = hidparser_find_unsigned_val(attribute);
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 			break;
11880Sstevel@tonic-gate 		case R_ITEM_USAGE_PAGE:
11890Sstevel@tonic-gate 			ui->usage_page = hidparser_find_unsigned_val(attribute)
11900Sstevel@tonic-gate 			    & 0xffff;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 			break;
11930Sstevel@tonic-gate 		}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		attribute = attribute->entity_attribute_next;
11960Sstevel@tonic-gate 	}
11970Sstevel@tonic-gate }
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate /*
12010Sstevel@tonic-gate  * hidparser_get_report_id_list:
12020Sstevel@tonic-gate  *	Return a list of all report ids used for descriptor items
12030Sstevel@tonic-gate  *	corresponding to a main item.
12040Sstevel@tonic-gate  *
12050Sstevel@tonic-gate  * Arguments:
12060Sstevel@tonic-gate  *	parser_handle:
12070Sstevel@tonic-gate  *		hid parser handle
12080Sstevel@tonic-gate  *	main_item_type:
12090Sstevel@tonic-gate  *		type of report, either Input, Output, or Feature
12100Sstevel@tonic-gate  *	report_id_list:
12110Sstevel@tonic-gate  *		Filled in with a list of report ids found in the descriptor
12120Sstevel@tonic-gate  *
12130Sstevel@tonic-gate  * Return values:
12140Sstevel@tonic-gate  *	HIDPARSER_SUCCESS - returned success
12150Sstevel@tonic-gate  *	HIDPARSER_FAILURE - unspecified failure
12160Sstevel@tonic-gate  */
12170Sstevel@tonic-gate int
hidparser_get_report_id_list(hidparser_handle_t parser_handle,uint_t main_item_type,hidparser_report_id_list_t * report_id_list)12180Sstevel@tonic-gate hidparser_get_report_id_list(hidparser_handle_t parser_handle,
12190Sstevel@tonic-gate 			uint_t main_item_type,
12200Sstevel@tonic-gate 			hidparser_report_id_list_t *report_id_list)
12210Sstevel@tonic-gate {
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	if ((parser_handle == NULL) ||
12240Sstevel@tonic-gate 	    (parser_handle->hidparser_handle_parse_tree == NULL)) {
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	report_id_list->no_of_report_ids = 0;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	return (hidparser_get_report_id_list_internal(
12320Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
12330Sstevel@tonic-gate 	    main_item_type, report_id_list));
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate /*
12380Sstevel@tonic-gate  * hidparser_get_report_id_list_internal:
12390Sstevel@tonic-gate  *	internal function that generates list of all report ids
12400Sstevel@tonic-gate  */
12410Sstevel@tonic-gate int
hidparser_get_report_id_list_internal(entity_item_t * parser_handle,uint_t main_item_type,hidparser_report_id_list_t * id_lst)12420Sstevel@tonic-gate hidparser_get_report_id_list_internal(
12430Sstevel@tonic-gate 			entity_item_t *parser_handle,
12440Sstevel@tonic-gate 			uint_t main_item_type,
12450Sstevel@tonic-gate 			hidparser_report_id_list_t *id_lst)
12460Sstevel@tonic-gate {
12470Sstevel@tonic-gate 	/* setup wrapper function */
12480Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
12490Sstevel@tonic-gate 	entity_attribute_t *attribute;
12500Sstevel@tonic-gate 	uint_t report_id = 0;
12510Sstevel@tonic-gate 	int i = 0;
12520Sstevel@tonic-gate 	int rval;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	while (current) {
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 			rval = hidparser_get_report_id_list_internal(
12590Sstevel@tonic-gate 			    current->info.child, main_item_type, id_lst);
12600Sstevel@tonic-gate 			if (rval != HIDPARSER_SUCCESS) {
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 				return (rval);
12630Sstevel@tonic-gate 			}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
12660Sstevel@tonic-gate 			/* Match Item Type */
12670Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 			while (attribute != NULL) {
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
12720Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 					/* Found a Report ID */
12750Sstevel@tonic-gate 					report_id = attribute->
12760Sstevel@tonic-gate 					    entity_attribute_value[0];
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 					/* Report ID already in list? */
12790Sstevel@tonic-gate 					for (i = 0;
12800Sstevel@tonic-gate 					    i < id_lst->no_of_report_ids;
12810Sstevel@tonic-gate 					    i++) {
12820Sstevel@tonic-gate 						if (report_id == id_lst->
12830Sstevel@tonic-gate 						    report_id[i]) {
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 							break;
12860Sstevel@tonic-gate 						}
12870Sstevel@tonic-gate 					}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 					if (i >= id_lst->no_of_report_ids) {
12900Sstevel@tonic-gate 						/*
12910Sstevel@tonic-gate 						 * New Report ID found, put
12920Sstevel@tonic-gate 						 * in list
12930Sstevel@tonic-gate 						 */
12940Sstevel@tonic-gate 						if (i >= REPORT_ID_MAX) {
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 							return
12970Sstevel@tonic-gate 							    (HIDPARSER_FAILURE);
12980Sstevel@tonic-gate 						}
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 						id_lst->report_id[i] =
13010Sstevel@tonic-gate 						    report_id;
13020Sstevel@tonic-gate 						id_lst->no_of_report_ids++;
13030Sstevel@tonic-gate 					}
13040Sstevel@tonic-gate 				}
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
13070Sstevel@tonic-gate 			}
13080Sstevel@tonic-gate 		}
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	} /* end while current */
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate /*
13190Sstevel@tonic-gate  * hidparser_print_report_descr_handle:
13200Sstevel@tonic-gate  *	Functions to print the parse tree. Currently not
13210Sstevel@tonic-gate  *	being called.
13220Sstevel@tonic-gate  */
13230Sstevel@tonic-gate static int
hidparser_print_report_descr_handle(entity_item_t * handle,int indent_level)13240Sstevel@tonic-gate hidparser_print_report_descr_handle(entity_item_t *handle,
13250Sstevel@tonic-gate 			int indent_level)
13260Sstevel@tonic-gate {
13270Sstevel@tonic-gate 	entity_item_t *current = handle;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	while (current) {
13300Sstevel@tonic-gate 		if (current->info.child) {
13310Sstevel@tonic-gate 			hidparser_print_entity(current, indent_level);
13320Sstevel@tonic-gate 			/* do children */
13330Sstevel@tonic-gate 			(void) hidparser_print_report_descr_handle(
13346898Sfb209375 			    current->info.child, indent_level+1);
13350Sstevel@tonic-gate 		} else /* just a regular entity */ {
13360Sstevel@tonic-gate 			hidparser_print_entity(current, indent_level);
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
13390Sstevel@tonic-gate 	}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate #define	SPACE_PER_LEVEL 5
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate /*
13480Sstevel@tonic-gate  * hidparser_print_entity ;
13490Sstevel@tonic-gate  * Prints the entity items recursively
13500Sstevel@tonic-gate  */
13510Sstevel@tonic-gate static void
hidparser_print_entity(entity_item_t * entity,int indent_level)13520Sstevel@tonic-gate hidparser_print_entity(entity_item_t *entity, int indent_level)
13530Sstevel@tonic-gate {
13540Sstevel@tonic-gate 	char indent_space[256];
13550Sstevel@tonic-gate 	int count;
13560Sstevel@tonic-gate 	entity_attribute_t *attr;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	indent_level *= SPACE_PER_LEVEL;
13590Sstevel@tonic-gate 
13606898Sfb209375 	for (count = 0; indent_level--; count++)
13616898Sfb209375 		indent_space[count] = ' ';
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	indent_space[count] = 0;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 	attr = entity->entity_item_attributes;
13660Sstevel@tonic-gate 	while (attr) {
13670Sstevel@tonic-gate 		hidparser_print_this_attribute(attr, indent_space);
13680Sstevel@tonic-gate 		attr = attr->entity_attribute_next;
13690Sstevel@tonic-gate 	}
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle, "%s%s(0x%x)",
13720Sstevel@tonic-gate 	    indent_space, items[entity->entity_item_type],
13730Sstevel@tonic-gate 	    (entity->entity_item_params_leng ?
13746898Sfb209375 	    entity->entity_item_params[0] & 0xFF : 0x00));
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate  * hidparser_print_this_attribute:
13800Sstevel@tonic-gate  *	Prints the attribute passed in the argument
13810Sstevel@tonic-gate  */
13820Sstevel@tonic-gate static void
hidparser_print_this_attribute(entity_attribute_t * attribute,char * ident_space)13830Sstevel@tonic-gate hidparser_print_this_attribute(entity_attribute_t *attribute,
13840Sstevel@tonic-gate 			char *ident_space)
13850Sstevel@tonic-gate {
13860Sstevel@tonic-gate 	if (ident_space == NULL) {
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
13890Sstevel@tonic-gate 		    "%s(0x%X)",
13900Sstevel@tonic-gate 		    items[attribute->entity_attribute_tag],
13910Sstevel@tonic-gate 		    hidparser_find_unsigned_val(attribute));
13920Sstevel@tonic-gate 	} else {
13930Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
13940Sstevel@tonic-gate 		    "%s%s(0x%X)", ident_space,
13950Sstevel@tonic-gate 		    items[attribute->entity_attribute_tag],
13960Sstevel@tonic-gate 		    hidparser_find_unsigned_val(attribute));
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate }
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate /*
14030Sstevel@tonic-gate  * The next few functions will be used for parsing using the
14040Sstevel@tonic-gate  * grammar:
14050Sstevel@tonic-gate  *
14060Sstevel@tonic-gate  *	Start			-> ReportDescriptor <EOF>
14070Sstevel@tonic-gate  *
14080Sstevel@tonic-gate  *	ReportDescriptor	-> ItemList
14090Sstevel@tonic-gate  *
14100Sstevel@tonic-gate  *	ItemList		-> Items MainItem ItemList
14110Sstevel@tonic-gate  *				   | epsilon
14120Sstevel@tonic-gate  *
14130Sstevel@tonic-gate  *	MainItem		-> BeginCollection ItemList  EndCollection
14140Sstevel@tonic-gate  *				  | Input
14150Sstevel@tonic-gate  *				  | Output
14160Sstevel@tonic-gate  *				  | Feature
14170Sstevel@tonic-gate  *
14180Sstevel@tonic-gate  *	Items			-> GlobalItem  Items
14190Sstevel@tonic-gate  *				   | LocalItem Items
14200Sstevel@tonic-gate  *				   | SetDelimiterOpen LocalItemList
14210Sstevel@tonic-gate  *					SetDelimiterClose Items
14220Sstevel@tonic-gate  *				   | epsilon
14230Sstevel@tonic-gate  *
14240Sstevel@tonic-gate  *	LocalItemList		-> LocalItem Temp2
14250Sstevel@tonic-gate  *
14260Sstevel@tonic-gate  *	Temp2			-> LocalItem Temp2
14270Sstevel@tonic-gate  *				   | epsilon
14280Sstevel@tonic-gate  *
14290Sstevel@tonic-gate  *	GlobalItem		-> UsagePage
14300Sstevel@tonic-gate  *				   | LogicalMinimum
14310Sstevel@tonic-gate  *				   | LogicalMaximum
14320Sstevel@tonic-gate  *				   | PhysicalMinimum
14330Sstevel@tonic-gate  *				   | PhysicalMaximum
14340Sstevel@tonic-gate  *				   | Unit
14350Sstevel@tonic-gate  *				   | Exponent
14360Sstevel@tonic-gate  *				   | ReportSize
14370Sstevel@tonic-gate  *				   | ReportCount
14380Sstevel@tonic-gate  *				   | ReportID
14390Sstevel@tonic-gate  *
14400Sstevel@tonic-gate  *	LocalItem		-> Usage
14410Sstevel@tonic-gate  *				   | UsageMinimum
14420Sstevel@tonic-gate  *				   | UsageMaximum
14430Sstevel@tonic-gate  *				   | DesignatorIndex
14440Sstevel@tonic-gate  *				   | DesignatorMinimum
14450Sstevel@tonic-gate  *				   | StringIndex
14460Sstevel@tonic-gate  *				   | StringMinimum
14470Sstevel@tonic-gate  *				   | StringMaximum
14480Sstevel@tonic-gate  *
14490Sstevel@tonic-gate  */
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate /*
14530Sstevel@tonic-gate  * hidparser_lookup_first:
14540Sstevel@tonic-gate  *	Looks up if token belongs to the FIRST of the function tag
14550Sstevel@tonic-gate  *	that is passed through the first argument
14560Sstevel@tonic-gate  */
14570Sstevel@tonic-gate static int
hidparser_lookup_first(int func_index,int token)14580Sstevel@tonic-gate hidparser_lookup_first(int func_index,
14590Sstevel@tonic-gate 			int token)
14600Sstevel@tonic-gate {
14610Sstevel@tonic-gate 	int	*itemp;
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	itemp = hid_first_list[func_index];
14640Sstevel@tonic-gate 	while (*itemp != 0) {
14650Sstevel@tonic-gate 		/* get the next terminal on the list */
14660Sstevel@tonic-gate 		if (*itemp == token) {
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 			return (HIDPARSER_SUCCESS);
14690Sstevel@tonic-gate 		}
14700Sstevel@tonic-gate 		itemp++;
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	/* token is not on the FIRST list */
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate /*
14800Sstevel@tonic-gate  * hidparser_main:
14810Sstevel@tonic-gate  *	Function called from hidparser_parse_report_descriptor()
14820Sstevel@tonic-gate  *	to parse the Report Descriptor
14830Sstevel@tonic-gate  */
14840Sstevel@tonic-gate static int
hidparser_main(unsigned char * descriptor,size_t size,entity_item_t ** item_ptr)14850Sstevel@tonic-gate hidparser_main(unsigned char *descriptor,
14860Sstevel@tonic-gate 			size_t size,
14870Sstevel@tonic-gate 			entity_item_t **item_ptr)
14880Sstevel@tonic-gate {
14890Sstevel@tonic-gate 	hidparser_tok_t	*scan_ifp;
14900Sstevel@tonic-gate 	int retval;
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 	scan_ifp = kmem_zalloc(sizeof (hidparser_tok_t), KM_SLEEP);
14930Sstevel@tonic-gate 	scan_ifp->hidparser_tok_text =
14946898Sfb209375 	    kmem_zalloc(HIDPARSER_TEXT_LENGTH, KM_SLEEP);
14950Sstevel@tonic-gate 	scan_ifp->hidparser_tok_max_bsize = size;
14960Sstevel@tonic-gate 	scan_ifp->hidparser_tok_entity_descriptor = descriptor;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	*item_ptr = NULL;
14990Sstevel@tonic-gate 	retval =  hidparser_ReportDescriptorDash(item_ptr, scan_ifp);
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	/*
15020Sstevel@tonic-gate 	 * Free the Local & Global item list
15030Sstevel@tonic-gate 	 * It maybe the case that no tree has been built
15040Sstevel@tonic-gate 	 * up but there have been allocation in the attribute
15050Sstevel@tonic-gate 	 * & control lists
15060Sstevel@tonic-gate 	 */
15070Sstevel@tonic-gate 	if (scan_ifp->hidparser_tok_gitem_head) {
15080Sstevel@tonic-gate 		hidparser_free_attribute_list(
15090Sstevel@tonic-gate 		    scan_ifp->hidparser_tok_gitem_head);
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if (scan_ifp->hidparser_tok_litem_head) {
15130Sstevel@tonic-gate 		hidparser_free_attribute_list(
15140Sstevel@tonic-gate 		    scan_ifp->hidparser_tok_litem_head);
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate 	kmem_free(scan_ifp->hidparser_tok_text, HIDPARSER_TEXT_LENGTH);
15170Sstevel@tonic-gate 	kmem_free(scan_ifp, sizeof (hidparser_tok_t));
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	return (retval);
15200Sstevel@tonic-gate }
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate /*
15240Sstevel@tonic-gate  * hidparser_ReportDescriptorDash:
15250Sstevel@tonic-gate  *	Synthetic start symbol, implements
15260Sstevel@tonic-gate  *	hidparser_ReportDescriptor <EOF>
15270Sstevel@tonic-gate  */
15280Sstevel@tonic-gate static int
hidparser_ReportDescriptorDash(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)15290Sstevel@tonic-gate hidparser_ReportDescriptorDash(entity_item_t ** item_ptr,
15300Sstevel@tonic-gate 			hidparser_tok_t *scan_ifp)
15310Sstevel@tonic-gate {
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	if ((hidparser_ReportDescriptor(item_ptr, scan_ifp) ==
15340Sstevel@tonic-gate 	    HIDPARSER_SUCCESS) && (scan_ifp->hidparser_tok_token == 0)) {
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 		return (HIDPARSER_SUCCESS);
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	/*
15400Sstevel@tonic-gate 	 * In case of failure, free the kernel memory
15410Sstevel@tonic-gate 	 * allocated for partial building of the tree,
15420Sstevel@tonic-gate 	 * if any
15430Sstevel@tonic-gate 	 */
15440Sstevel@tonic-gate 	if (*item_ptr != NULL) {
15450Sstevel@tonic-gate 		(void) hidparser_free_report_descr_handle(*item_ptr);
15460Sstevel@tonic-gate 	}
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 	*item_ptr = NULL;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
15510Sstevel@tonic-gate }
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate /*
15550Sstevel@tonic-gate  * hidparser_ReportDescriptor:
15560Sstevel@tonic-gate  *	Implements the Rule:
15570Sstevel@tonic-gate  *	ReportDescriptor -> ItemList
15580Sstevel@tonic-gate  */
15590Sstevel@tonic-gate static int
hidparser_ReportDescriptor(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)15600Sstevel@tonic-gate hidparser_ReportDescriptor(entity_item_t ** item_ptr,
15610Sstevel@tonic-gate 			hidparser_tok_t	*scan_ifp)
15620Sstevel@tonic-gate {
15630Sstevel@tonic-gate 	hidparser_scan(scan_ifp);
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	/*
15660Sstevel@tonic-gate 	 * We do not search for the token in FIRST(ReportDescriptor)
15670Sstevel@tonic-gate 	 * since -
15680Sstevel@tonic-gate 	 *
15690Sstevel@tonic-gate 	 * FIRST(ReportDescriptor) == FIRST(ItemList)
15700Sstevel@tonic-gate 	 * ReportDescriptor ----> ItemList
15710Sstevel@tonic-gate 	 */
15720Sstevel@tonic-gate 	if (hidparser_ItemList(item_ptr, scan_ifp) == HIDPARSER_SUCCESS) {
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 		return (HIDPARSER_SUCCESS);
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
15780Sstevel@tonic-gate }
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate /*
15820Sstevel@tonic-gate  * hidparser_ItemList:
15830Sstevel@tonic-gate  *	Implements the Rule:
15840Sstevel@tonic-gate  *	ItemList -> Items MainItem ItemList | epsilon
15850Sstevel@tonic-gate  *
15866898Sfb209375  *	This function constructs the tree on which depends the "hidparser"
15876898Sfb209375  *	consumer functions. Basically the structure of the tree is
15880Sstevel@tonic-gate  *
15890Sstevel@tonic-gate  *	C--[RS]->EC--[RS]->C--[RS]->EC..(and so on)
15900Sstevel@tonic-gate  *	|
15910Sstevel@tonic-gate  *    [CH] <== This relationship is true for other "C's"
15926898Sfb209375  *	|      also.
15936898Sfb209375  *	v
15940Sstevel@tonic-gate  *     C/-------------/I/O/F <== [ Any of these ]
15956898Sfb209375  *     |	      ------
15966898Sfb209375  *     |		|
15976898Sfb209375  *     v		v
15980Sstevel@tonic-gate  *    [CH      | RS]  [ RS ]
15990Sstevel@tonic-gate  *     C/I/O/F | EC    I/O/F
16000Sstevel@tonic-gate  *     |
16010Sstevel@tonic-gate  *     |
16020Sstevel@tonic-gate  *    and so on...
16030Sstevel@tonic-gate  *
16046898Sfb209375  *	where	 C = Collection
16050Sstevel@tonic-gate  *		EC = EndCollection
16060Sstevel@tonic-gate  *		 I = Input
16070Sstevel@tonic-gate  *		 O = Output
16080Sstevel@tonic-gate  *		 F = Feature "Main" Items.
16090Sstevel@tonic-gate  *
16100Sstevel@tonic-gate  *	and the relationships are  [RS] for right sibling and [CH] for
16110Sstevel@tonic-gate  *	child. [CH | RS ] stands for "child or right sibling" with the
16120Sstevel@tonic-gate  *	possible values below it.
16130Sstevel@tonic-gate  */
16140Sstevel@tonic-gate static int
hidparser_ItemList(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)16150Sstevel@tonic-gate hidparser_ItemList(entity_item_t ** item_ptr, hidparser_tok_t *scan_ifp)
16160Sstevel@tonic-gate {
16170Sstevel@tonic-gate 	entity_item_t	*curr_ei, *cache_ei, *prev_ei, *tmp_ei;
16180Sstevel@tonic-gate 	boolean_t	root_coll = B_FALSE;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	curr_ei = cache_ei = prev_ei = tmp_ei = NULL;
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	while (scan_ifp->hidparser_tok_token != 0) {
16230Sstevel@tonic-gate 		if (hidparser_Items(scan_ifp) == HIDPARSER_FAILURE) {
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 			return (HIDPARSER_FAILURE);
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 		if (hidparser_MainItem(&curr_ei, scan_ifp) ==
16290Sstevel@tonic-gate 		    HIDPARSER_FAILURE) {
16300Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_ALL,
16310Sstevel@tonic-gate 			    hparser_log_handle,
16326898Sfb209375 			    "Invalid MAIN item 0x%x in input stream",
16336898Sfb209375 			    scan_ifp->hidparser_tok_token);
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 			return (HIDPARSER_FAILURE);
16360Sstevel@tonic-gate 		}
16370Sstevel@tonic-gate 		if (curr_ei->entity_item_type == R_ITEM_COLLECTION) {
16380Sstevel@tonic-gate 			if (root_coll == B_FALSE) {
16390Sstevel@tonic-gate 				*item_ptr = curr_ei;
16400Sstevel@tonic-gate 				root_coll = B_TRUE;
16410Sstevel@tonic-gate 			}
16420Sstevel@tonic-gate 			curr_ei->prev_coll = cache_ei;
16430Sstevel@tonic-gate 			cache_ei = curr_ei;
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 				USB_DPRINTF_L3(PRINT_MASK_ALL,
16460Sstevel@tonic-gate 				    hparser_log_handle,
16476898Sfb209375 				    "Start Collection:cache_ei = 0x%p,"
16486898Sfb209375 				    " curr_ei = 0x%p",
16496898Sfb209375 				    (void *)cache_ei, (void *)curr_ei);
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 			if (prev_ei == NULL) {
16520Sstevel@tonic-gate 				prev_ei = curr_ei;
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 				continue;
16550Sstevel@tonic-gate 			}
16560Sstevel@tonic-gate 			if (prev_ei->entity_item_type ==
16576898Sfb209375 			    R_ITEM_COLLECTION) {
16580Sstevel@tonic-gate 				prev_ei->info.child = curr_ei;
16590Sstevel@tonic-gate 			} else {
16600Sstevel@tonic-gate 				prev_ei->entity_item_right_sibling =
16616898Sfb209375 				    curr_ei;
16620Sstevel@tonic-gate 			}
16630Sstevel@tonic-gate 		} else if (curr_ei->entity_item_type ==
16640Sstevel@tonic-gate 		    R_ITEM_END_COLLECTION) {
16650Sstevel@tonic-gate 			tmp_ei = cache_ei->prev_coll;
16660Sstevel@tonic-gate 			cache_ei->entity_item_right_sibling = curr_ei;
16670Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_ALL,
16680Sstevel@tonic-gate 			    hparser_log_handle,
16696898Sfb209375 			    "End Collection: cache_ei = 0x%p, "
16706898Sfb209375 			    "curr_ei = 0x%p",
16716898Sfb209375 			    (void *)cache_ei, (void *)curr_ei);
16720Sstevel@tonic-gate 			if (tmp_ei != NULL) {
16730Sstevel@tonic-gate 				/*
16740Sstevel@tonic-gate 				 * As will be the case for final end
16750Sstevel@tonic-gate 				 * collection.
16760Sstevel@tonic-gate 				 */
16770Sstevel@tonic-gate 				cache_ei = tmp_ei;
16780Sstevel@tonic-gate 			}
16790Sstevel@tonic-gate 			tmp_ei = NULL;
16800Sstevel@tonic-gate 		} else {
16817015Sbc224572 			if (prev_ei == NULL) {
16827015Sbc224572 				USB_DPRINTF_L2(PRINT_MASK_ALL,
16837015Sbc224572 				    hparser_log_handle,
16847015Sbc224572 				    "Invalid First MAIN item 0x%x",
16857015Sbc224572 				    scan_ifp->hidparser_tok_token);
16867015Sbc224572 
16877015Sbc224572 				return (HIDPARSER_FAILURE);
16887015Sbc224572 			}
16890Sstevel@tonic-gate 			if (prev_ei->entity_item_type ==
16900Sstevel@tonic-gate 			    R_ITEM_COLLECTION) {
16910Sstevel@tonic-gate 				USB_DPRINTF_L3(PRINT_MASK_ALL,
16920Sstevel@tonic-gate 				    hparser_log_handle,
16930Sstevel@tonic-gate 				    "Main Item: token = 0x%x, "
16946898Sfb209375 				    "curr_ei = 0x%p "
16950Sstevel@tonic-gate 				    "will be the child of prev_ei "
16966898Sfb209375 				    "= 0x%p, "
16976898Sfb209375 				    "cache_ei being 0x%p",
16980Sstevel@tonic-gate 				    curr_ei->entity_item_type,
16996898Sfb209375 				    (void *)curr_ei, (void *)prev_ei,
17006898Sfb209375 				    (void *)cache_ei);
17010Sstevel@tonic-gate 				prev_ei->info.child = curr_ei;
17020Sstevel@tonic-gate 			} else {
17030Sstevel@tonic-gate 				USB_DPRINTF_L3(PRINT_MASK_ALL,
17040Sstevel@tonic-gate 				    hparser_log_handle,
17050Sstevel@tonic-gate 				    "Main Item: token = 0x%x, "
17066898Sfb209375 				    "curr_ei = 0x%p "
17070Sstevel@tonic-gate 				    "will be the right sibling of "
17086898Sfb209375 				    "prev_ei = 0x%p, "
17096898Sfb209375 				    "cache_ei being 0x%p",
17100Sstevel@tonic-gate 				    curr_ei->entity_item_type,
17116898Sfb209375 				    (void *)curr_ei, (void *)prev_ei,
17126898Sfb209375 				    (void *)cache_ei);
17130Sstevel@tonic-gate 				prev_ei->entity_item_right_sibling =
17140Sstevel@tonic-gate 				    curr_ei;
17150Sstevel@tonic-gate 			}
17160Sstevel@tonic-gate 		}
17170Sstevel@tonic-gate 		prev_ei = curr_ei;
17180Sstevel@tonic-gate 	}
17190Sstevel@tonic-gate 	if (*item_ptr != cache_ei) {
17200Sstevel@tonic-gate 		/* Something wrong happened */
17210Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
17220Sstevel@tonic-gate 		    "Failed to parse report descriptor");
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
17250Sstevel@tonic-gate 	}
17260Sstevel@tonic-gate 	(void) hidparser_print_report_descr_handle(cache_ei, 0);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
17290Sstevel@tonic-gate }
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate /*
17330Sstevel@tonic-gate  * hidparser_MainItem:
17340Sstevel@tonic-gate  *	Implements the Rule:
17350Sstevel@tonic-gate  *	MainItem ->	BeginCollection ItemList  EndCollection
17360Sstevel@tonic-gate  *			| Input
17370Sstevel@tonic-gate  *			| Output
17380Sstevel@tonic-gate  *			| Feature
17390Sstevel@tonic-gate  */
17400Sstevel@tonic-gate static int
hidparser_MainItem(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)17410Sstevel@tonic-gate hidparser_MainItem(entity_item_t ** item_ptr,
17420Sstevel@tonic-gate 			hidparser_tok_t *scan_ifp)
17430Sstevel@tonic-gate {
17440Sstevel@tonic-gate 	switch (scan_ifp->hidparser_tok_token) {
17450Sstevel@tonic-gate 		case R_ITEM_INPUT:
17460Sstevel@tonic-gate 			/* FALLTHRU */
17470Sstevel@tonic-gate 		case R_ITEM_OUTPUT:
17480Sstevel@tonic-gate 			/* FALLTHRU */
17490Sstevel@tonic-gate 		case R_ITEM_FEATURE:
17500Sstevel@tonic-gate 		case R_ITEM_COLLECTION:
17510Sstevel@tonic-gate 		case R_ITEM_END_COLLECTION:
17520Sstevel@tonic-gate 			*item_ptr = hidparser_allocate_entity(scan_ifp);
17530Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
17546898Sfb209375 			    "hidparser_MainItem:index = 0x%lx token = 0x%x",
17556898Sfb209375 			    scan_ifp->hidparser_tok_index -
17566898Sfb209375 			    (*item_ptr)->entity_item_params_leng - 1,
17576898Sfb209375 			    scan_ifp->hidparser_tok_token);
17580Sstevel@tonic-gate 			hidparser_scan(scan_ifp);
17590Sstevel@tonic-gate 			hidparser_global_err_check(*item_ptr);
17600Sstevel@tonic-gate 			hidparser_local_err_check(*item_ptr);
17610Sstevel@tonic-gate 			hidparser_mainitem_err_check(*item_ptr);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 			return (HIDPARSER_SUCCESS);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 		default:
17660Sstevel@tonic-gate 			break;
17670Sstevel@tonic-gate 	}
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate 	*item_ptr = NULL;
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate /*
17760Sstevel@tonic-gate  * hidparser_Items:
17770Sstevel@tonic-gate  *	Implements the Rule:
17780Sstevel@tonic-gate  *	Items ->	GlobalItem  Items
17790Sstevel@tonic-gate  *			| LocalItem Items
17800Sstevel@tonic-gate  *			| SetDelimiterOpen LocalItemList
17810Sstevel@tonic-gate  *				SetDelimiterClose Items
17820Sstevel@tonic-gate  *			| epsilon
17830Sstevel@tonic-gate  */
17840Sstevel@tonic-gate static int
hidparser_Items(hidparser_tok_t * scan_ifp)17850Sstevel@tonic-gate hidparser_Items(hidparser_tok_t *scan_ifp)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	boolean_t delim_pre = B_FALSE;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	int	token = scan_ifp->hidparser_tok_token;
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	while (hidparser_lookup_first(HIDPARSER_ITEMS, token) ==
17920Sstevel@tonic-gate 	    HIDPARSER_SUCCESS) {
17930Sstevel@tonic-gate 		if (token == R_ITEM_SET_DELIMITER) {
17940Sstevel@tonic-gate 			if (delim_pre == B_FALSE) {
17950Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[0] != 1) {
17960Sstevel@tonic-gate 					hidparser_error_delim(NULL,
17976898Sfb209375 					    HIDPARSER_DELIM_ERR1);
17980Sstevel@tonic-gate 				} else {
17990Sstevel@tonic-gate 					delim_pre = B_TRUE;
18000Sstevel@tonic-gate 				}
18010Sstevel@tonic-gate 			} else {
18020Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[0] !=
18030Sstevel@tonic-gate 				    0) {
18040Sstevel@tonic-gate 					hidparser_error_delim(NULL,
18056898Sfb209375 					    HIDPARSER_DELIM_ERR2);
18060Sstevel@tonic-gate 				} else {
18070Sstevel@tonic-gate 					delim_pre = B_FALSE;
18080Sstevel@tonic-gate 				}
18090Sstevel@tonic-gate 			}
18100Sstevel@tonic-gate 			(void) hidparser_LocalItem(scan_ifp);
18110Sstevel@tonic-gate 			token = scan_ifp->hidparser_tok_token;
18120Sstevel@tonic-gate 		} else if (hidparser_GlobalItem(scan_ifp) ==
18130Sstevel@tonic-gate 		    HIDPARSER_SUCCESS) {
18140Sstevel@tonic-gate 			token = scan_ifp->hidparser_tok_token;
18150Sstevel@tonic-gate 		} else if (hidparser_LocalItem(scan_ifp) == HIDPARSER_SUCCESS) {
18160Sstevel@tonic-gate 			token = scan_ifp->hidparser_tok_token;
18170Sstevel@tonic-gate 		}
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);	/* epsilon */
18210Sstevel@tonic-gate }
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate /*
18250Sstevel@tonic-gate  * hidparser_GlobalItem:
18260Sstevel@tonic-gate  *	Implements the Rule:
18270Sstevel@tonic-gate  *	GlobalItem ->	UsagePage
18280Sstevel@tonic-gate  *			| LogicalMinimum
18290Sstevel@tonic-gate  *			| LocgicalMaximum
18300Sstevel@tonic-gate  *			| PhysicalMinimum
18310Sstevel@tonic-gate  *			| PhysicalMaximum
18320Sstevel@tonic-gate  *			| Unit
18330Sstevel@tonic-gate  *			| Exponent
18340Sstevel@tonic-gate  *			| ReportSize
18350Sstevel@tonic-gate  *			| ReportCount
18360Sstevel@tonic-gate  *			| ReportID
18370Sstevel@tonic-gate  */
18380Sstevel@tonic-gate static int
hidparser_GlobalItem(hidparser_tok_t * scan_ifp)18390Sstevel@tonic-gate hidparser_GlobalItem(hidparser_tok_t	*scan_ifp)
18400Sstevel@tonic-gate {
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 	int i;
18430Sstevel@tonic-gate 	entity_attribute_stack_t	*elem;
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate 	switch (scan_ifp->hidparser_tok_token) {
18460Sstevel@tonic-gate 		case R_ITEM_USAGE_PAGE:
18470Sstevel@tonic-gate 			/* Error check */
18480Sstevel@tonic-gate 			for (i = 0; i < scan_ifp->hidparser_tok_leng; i++) {
18490Sstevel@tonic-gate 				/* Undefined data value: 0 */
18500Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[i] == 0) {
18510Sstevel@tonic-gate 					hidparser_report_err(
18520Sstevel@tonic-gate 					    HIDPARSER_ERR_WARN,
18530Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
18540Sstevel@tonic-gate 					    R_ITEM_USAGE_PAGE,
18550Sstevel@tonic-gate 					    0,
18560Sstevel@tonic-gate 					    "Data field should be non-Zero");
18570Sstevel@tonic-gate 				}
18580Sstevel@tonic-gate 				/* Reserved values 0x0A-0xFE */
18590Sstevel@tonic-gate 				else if ((scan_ifp->hidparser_tok_text[i] >=
18600Sstevel@tonic-gate 				    0x0a) &&
18610Sstevel@tonic-gate 				    (scan_ifp->hidparser_tok_text[i] <=
18620Sstevel@tonic-gate 				    0xFE)) {
18630Sstevel@tonic-gate 					hidparser_report_err(
18640Sstevel@tonic-gate 					    HIDPARSER_ERR_WARN,
18650Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
18660Sstevel@tonic-gate 					    R_ITEM_USAGE_PAGE,
18670Sstevel@tonic-gate 					    1,
18680Sstevel@tonic-gate 					    "Data field should not use "
18690Sstevel@tonic-gate 					    "reserved values");
18700Sstevel@tonic-gate 				}
18710Sstevel@tonic-gate 			}
18720Sstevel@tonic-gate 			break;
18730Sstevel@tonic-gate 		case R_ITEM_UNIT:
18740Sstevel@tonic-gate 			/* FALLTHRU */
18750Sstevel@tonic-gate 		case R_ITEM_EXPONENT:
18760Sstevel@tonic-gate 			/*
18770Sstevel@tonic-gate 			 * Error check:
18780Sstevel@tonic-gate 			 * Nibble 7 should be zero
18790Sstevel@tonic-gate 			 */
18800Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng == 4) {
18810Sstevel@tonic-gate 				if ((scan_ifp->hidparser_tok_text[3] &
18820Sstevel@tonic-gate 				    0xf0) != 0) {
18830Sstevel@tonic-gate 					hidparser_report_err(
18840Sstevel@tonic-gate 					    HIDPARSER_ERR_WARN,
18850Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
18860Sstevel@tonic-gate 					    scan_ifp->hidparser_tok_token,
18870Sstevel@tonic-gate 					    0,
18880Sstevel@tonic-gate 					    "Data field reserved bits should "
18890Sstevel@tonic-gate 					    "be Zero");
18900Sstevel@tonic-gate 				}
18910Sstevel@tonic-gate 			}
18920Sstevel@tonic-gate 			break;
18930Sstevel@tonic-gate 		case R_ITEM_REPORT_COUNT:
18940Sstevel@tonic-gate 			/*
18950Sstevel@tonic-gate 			 * Error Check:
18960Sstevel@tonic-gate 			 * Report Count should be nonzero
18970Sstevel@tonic-gate 			 */
18980Sstevel@tonic-gate 			for (i = 0; i < scan_ifp->hidparser_tok_leng; i++) {
18990Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[i])
19000Sstevel@tonic-gate 					break;
19010Sstevel@tonic-gate 			}
19020Sstevel@tonic-gate 			if (i == scan_ifp->hidparser_tok_leng) {
19030Sstevel@tonic-gate 				hidparser_report_err(
19040Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
19050Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
19060Sstevel@tonic-gate 				    R_ITEM_REPORT_COUNT,
19070Sstevel@tonic-gate 				    0,
19080Sstevel@tonic-gate 				    "Report Count = 0");
19090Sstevel@tonic-gate 			}
19100Sstevel@tonic-gate 			break;
19110Sstevel@tonic-gate 		case R_ITEM_REPORT_ID:
19120Sstevel@tonic-gate 			/*
19130Sstevel@tonic-gate 			 * Error check:
19140Sstevel@tonic-gate 			 * Report Id should be nonzero & <= 255
19150Sstevel@tonic-gate 			 */
19160Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng != 1)	{
19170Sstevel@tonic-gate 				hidparser_report_err(
19180Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
19190Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
19200Sstevel@tonic-gate 				    R_ITEM_REPORT_ID,
19210Sstevel@tonic-gate 				    1,
19220Sstevel@tonic-gate 				    "Must be contained in a byte");
19230Sstevel@tonic-gate 			}
19240Sstevel@tonic-gate 			if (!scan_ifp->hidparser_tok_text[0]) {
19250Sstevel@tonic-gate 				hidparser_report_err(
19260Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
19270Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
19280Sstevel@tonic-gate 				    R_ITEM_REPORT_ID,
19290Sstevel@tonic-gate 				    0,
19300Sstevel@tonic-gate 				    "Report Id must be non-zero");
19310Sstevel@tonic-gate 			}
19320Sstevel@tonic-gate 			break;
19330Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MINIMUM:
19340Sstevel@tonic-gate 			break;
19350Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MAXIMUM:
19360Sstevel@tonic-gate 			break;
19370Sstevel@tonic-gate 		case R_ITEM_PHYSICAL_MINIMUM:
19380Sstevel@tonic-gate 			break;
19390Sstevel@tonic-gate 		case R_ITEM_PHYSICAL_MAXIMUM:
19400Sstevel@tonic-gate 			break;
19410Sstevel@tonic-gate 		case R_ITEM_REPORT_SIZE:
19420Sstevel@tonic-gate 			break;
19430Sstevel@tonic-gate 		case R_ITEM_PUSH:
19440Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng != 0)	{
19450Sstevel@tonic-gate 				hidparser_report_err(
19460Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
19470Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
19480Sstevel@tonic-gate 				    scan_ifp->hidparser_tok_token,
19490Sstevel@tonic-gate 				    0,
19500Sstevel@tonic-gate 				    "Data Field size should be zero");
19510Sstevel@tonic-gate 			} else {
19520Sstevel@tonic-gate 				elem = (entity_attribute_stack_t *)kmem_zalloc(
19536898Sfb209375 				    sizeof (entity_attribute_stack_t),
19546898Sfb209375 				    KM_SLEEP);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 				elem->list = hidparser_cp_attribute_list(
19576898Sfb209375 				    scan_ifp->hidparser_tok_gitem_head);
19580Sstevel@tonic-gate 				if (scan_ifp->hidparser_head) {
19590Sstevel@tonic-gate 					elem->next = scan_ifp->hidparser_head;
19600Sstevel@tonic-gate 				}
19610Sstevel@tonic-gate 				scan_ifp->hidparser_head = elem;
19620Sstevel@tonic-gate 			}
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate 			break;
19650Sstevel@tonic-gate 		case R_ITEM_POP:
19660Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng != 0)	{
19670Sstevel@tonic-gate 				hidparser_report_err(
19680Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
19690Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
19700Sstevel@tonic-gate 				    scan_ifp->hidparser_tok_token,
19710Sstevel@tonic-gate 				    0,
19720Sstevel@tonic-gate 				    "Data Field size should be zero");
19730Sstevel@tonic-gate 			} else {
19740Sstevel@tonic-gate 				/* Free the current global list */
19750Sstevel@tonic-gate 				hidparser_free_attribute_list(scan_ifp->
19766898Sfb209375 				    hidparser_tok_gitem_head);
19770Sstevel@tonic-gate 				scan_ifp->hidparser_tok_gitem_head =
19780Sstevel@tonic-gate 				    scan_ifp->hidparser_head->list;
19790Sstevel@tonic-gate 				scan_ifp->hidparser_head->list = NULL;
19800Sstevel@tonic-gate 				elem = scan_ifp->hidparser_head;
19810Sstevel@tonic-gate 				scan_ifp->hidparser_head = elem->next;
19820Sstevel@tonic-gate 				kmem_free(elem,
19836898Sfb209375 				    sizeof (entity_attribute_stack_t));
19840Sstevel@tonic-gate 			}
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 			break;
19870Sstevel@tonic-gate 		default:
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 			return (HIDPARSER_FAILURE);
19900Sstevel@tonic-gate 
19910Sstevel@tonic-gate 			/*NOTREACHED*/
19920Sstevel@tonic-gate 	}
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	hidparser_add_attribute(scan_ifp);
19950Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
19966898Sfb209375 	    "hidparser_GlobalItem:index = 0x%lx token = 0x%x",
19976898Sfb209375 	    scan_ifp->hidparser_tok_index -
19986898Sfb209375 	    scan_ifp->hidparser_tok_leng - 1,
19996898Sfb209375 	    scan_ifp->hidparser_tok_token);
20000Sstevel@tonic-gate 	hidparser_scan(scan_ifp);
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate /*
20070Sstevel@tonic-gate  * hidparser_LocalItem:
20080Sstevel@tonic-gate  *	Implements the Rule:
20090Sstevel@tonic-gate  *	LocalItem ->	Usage
20100Sstevel@tonic-gate  *			| UsageMinimum
20110Sstevel@tonic-gate  *			| UsageMaximum
20120Sstevel@tonic-gate  *			| DesignatorIndex
20130Sstevel@tonic-gate  *			| DesignatorMinimum
20140Sstevel@tonic-gate  *			| StringIndex
20150Sstevel@tonic-gate  *			| StringMinimum
20160Sstevel@tonic-gate  *			| StringMaximum
20170Sstevel@tonic-gate  */
20180Sstevel@tonic-gate static int
hidparser_LocalItem(hidparser_tok_t * scan_ifp)20190Sstevel@tonic-gate hidparser_LocalItem(hidparser_tok_t	*scan_ifp)
20200Sstevel@tonic-gate {
20210Sstevel@tonic-gate 	int i;
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 	switch (scan_ifp->hidparser_tok_token) {
20240Sstevel@tonic-gate 		case R_ITEM_USAGE:
20250Sstevel@tonic-gate 			/*
20260Sstevel@tonic-gate 			 * Error Check:
20270Sstevel@tonic-gate 			 * Data Field should be nonzero
20280Sstevel@tonic-gate 			 */
20290Sstevel@tonic-gate 			for (i = 0; i < scan_ifp->hidparser_tok_leng; i++) {
20300Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[i])
20310Sstevel@tonic-gate 					break;
20320Sstevel@tonic-gate 			}
20330Sstevel@tonic-gate 			if (i == scan_ifp->hidparser_tok_leng) {
20340Sstevel@tonic-gate 				hidparser_report_err(
20350Sstevel@tonic-gate 				    HIDPARSER_ERR_WARN,
20360Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
20370Sstevel@tonic-gate 				    R_ITEM_USAGE,
20380Sstevel@tonic-gate 				    0,
20390Sstevel@tonic-gate 				    "Data Field should be non-zero");
20400Sstevel@tonic-gate 			}
20410Sstevel@tonic-gate 			/* FALLTHRU */
20420Sstevel@tonic-gate 		case R_ITEM_USAGE_MIN:
20430Sstevel@tonic-gate 			/* FALLTHRU */
20440Sstevel@tonic-gate 		case R_ITEM_USAGE_MAX:
20450Sstevel@tonic-gate 			/* FALLTHRU */
20460Sstevel@tonic-gate 		case R_ITEM_DESIGNATOR_INDEX:
20470Sstevel@tonic-gate 			/* FALLTHRU */
20480Sstevel@tonic-gate 		case R_ITEM_DESIGNATOR_MIN:
20490Sstevel@tonic-gate 			/* FALLTHRU */
20500Sstevel@tonic-gate 		case R_ITEM_STRING_INDEX:
20510Sstevel@tonic-gate 			/* FALLTHRU */
20520Sstevel@tonic-gate 		case R_ITEM_STRING_MIN:
20530Sstevel@tonic-gate 			/* FALLTHRU */
20540Sstevel@tonic-gate 		case R_ITEM_STRING_MAX:
20550Sstevel@tonic-gate 			/* FALLTHRU */
20560Sstevel@tonic-gate 		case R_ITEM_SET_DELIMITER:
20570Sstevel@tonic-gate 			hidparser_add_attribute(scan_ifp);
20580Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
20596898Sfb209375 			    "hidparser_LocalItem:index = 0x%lx token = 0x%x",
20600Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_index -
20610Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_leng - 1,
20620Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_token);
20630Sstevel@tonic-gate 			hidparser_scan(scan_ifp);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 			return (HIDPARSER_SUCCESS);
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 			/*NOTREACHED*/
20680Sstevel@tonic-gate 		default:
20690Sstevel@tonic-gate 			break;
20700Sstevel@tonic-gate 	}
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
20730Sstevel@tonic-gate }
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 
20760Sstevel@tonic-gate /*
20770Sstevel@tonic-gate  * hidparser_allocate_entity:
20780Sstevel@tonic-gate  *	Allocate Item of type 'type', length 'leng' and
20790Sstevel@tonic-gate  *	params 'text'. Fill in the attributes allocated
20800Sstevel@tonic-gate  *	so far from both the local and global item lists.
20810Sstevel@tonic-gate  *	Make the child and sibling of the item NULL.
20820Sstevel@tonic-gate  */
20830Sstevel@tonic-gate static entity_item_t *
hidparser_allocate_entity(hidparser_tok_t * scan_ifp)20840Sstevel@tonic-gate hidparser_allocate_entity(hidparser_tok_t	*scan_ifp)
20850Sstevel@tonic-gate {
20860Sstevel@tonic-gate 	entity_item_t *entity;
20870Sstevel@tonic-gate 	entity_attribute_t *aend;
20880Sstevel@tonic-gate 
20890Sstevel@tonic-gate 	int	entity_type = scan_ifp->hidparser_tok_token;
20900Sstevel@tonic-gate 	unsigned char	*text = scan_ifp->hidparser_tok_text;
20910Sstevel@tonic-gate 	int	len = scan_ifp->hidparser_tok_leng;
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 	entity = kmem_zalloc(sizeof (entity_item_t), KM_SLEEP);
20940Sstevel@tonic-gate 	entity->entity_item_type = entity_type;
20950Sstevel@tonic-gate 	entity->entity_item_params_leng = len;
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	if (len != 0) {
20980Sstevel@tonic-gate 		entity->entity_item_params = kmem_zalloc(len, KM_SLEEP);
20990Sstevel@tonic-gate 		(void) bcopy(text, entity->entity_item_params, len);
21000Sstevel@tonic-gate 	}
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 	/*
21030Sstevel@tonic-gate 	 * Copy attributes from entity attribute state table if not
21040Sstevel@tonic-gate 	 * end collection.
21050Sstevel@tonic-gate 	 */
21060Sstevel@tonic-gate 	if (entity_type != R_ITEM_END_COLLECTION) {
21070Sstevel@tonic-gate 		entity->entity_item_attributes = hidparser_cp_attribute_list(
21086898Sfb209375 		    scan_ifp->hidparser_tok_gitem_head);
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 		/*
21110Sstevel@tonic-gate 		 * append the control attributes, then clear out the control
21120Sstevel@tonic-gate 		 * attribute state table list
21130Sstevel@tonic-gate 		 */
21140Sstevel@tonic-gate 		if (entity->entity_item_attributes) {
21150Sstevel@tonic-gate 			aend = hidparser_find_attribute_end(
21166898Sfb209375 			    entity->entity_item_attributes);
21170Sstevel@tonic-gate 			aend->entity_attribute_next =
21186898Sfb209375 			    scan_ifp->hidparser_tok_litem_head;
21190Sstevel@tonic-gate 			scan_ifp->hidparser_tok_litem_head = NULL;
21200Sstevel@tonic-gate 		} else {
21210Sstevel@tonic-gate 			entity->entity_item_attributes =
21226898Sfb209375 			    scan_ifp->hidparser_tok_litem_head;
21230Sstevel@tonic-gate 			scan_ifp->hidparser_tok_litem_head = NULL;
21240Sstevel@tonic-gate 		}
21250Sstevel@tonic-gate 	}
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 	entity->info.child = entity->entity_item_right_sibling = 0;
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 	return (entity);
21300Sstevel@tonic-gate }
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate /*
21340Sstevel@tonic-gate  * hidparser_add_attribute:
21350Sstevel@tonic-gate  *	Add an attribute to the global or local item list
21360Sstevel@tonic-gate  *	If the last 4th bit from right is 1, add to the local item list
21370Sstevel@tonic-gate  *	Else add to the global item list
21380Sstevel@tonic-gate  */
21390Sstevel@tonic-gate static void
hidparser_add_attribute(hidparser_tok_t * scan_ifp)21400Sstevel@tonic-gate hidparser_add_attribute(hidparser_tok_t	*scan_ifp)
21410Sstevel@tonic-gate {
21420Sstevel@tonic-gate 	entity_attribute_t *newattrib, **previous, *elem;
21430Sstevel@tonic-gate 	int	entity = scan_ifp->hidparser_tok_token;
21440Sstevel@tonic-gate 	unsigned char	*text = scan_ifp->hidparser_tok_text;
21450Sstevel@tonic-gate 	int	len = scan_ifp->hidparser_tok_leng;
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	if (len == 0) {
21480Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
21490Sstevel@tonic-gate 		    "hidparser_add_attribute: len = 0 for item = 0x%x",
21500Sstevel@tonic-gate 		    entity);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 		return;
21530Sstevel@tonic-gate 	}
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	if (entity & HIDPARSER_ISLOCAL_MASK) {
21560Sstevel@tonic-gate 		previous = &scan_ifp->hidparser_tok_litem_head;
21570Sstevel@tonic-gate 	} else {
21580Sstevel@tonic-gate 		previous = &scan_ifp->hidparser_tok_gitem_head;
21590Sstevel@tonic-gate 	}
21600Sstevel@tonic-gate 
21610Sstevel@tonic-gate 	elem = *previous;
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	/*
21640Sstevel@tonic-gate 	 * remove attribute if it is already on list, except
21650Sstevel@tonic-gate 	 * for control attributes(local items), as we could have
21660Sstevel@tonic-gate 	 * multiple usages...
21670Sstevel@tonic-gate 	 * unless we want to hassle with checking for unique parameters.
21680Sstevel@tonic-gate 	 */
21690Sstevel@tonic-gate 	while (elem) {
21700Sstevel@tonic-gate 		if (elem->entity_attribute_tag == entity &&
21710Sstevel@tonic-gate 		    !(entity & HIDPARSER_ISLOCAL_MASK)) {
21720Sstevel@tonic-gate 			*previous = elem->entity_attribute_next;
21730Sstevel@tonic-gate 			kmem_free(elem->entity_attribute_value,
21740Sstevel@tonic-gate 			    elem->entity_attribute_length);
21750Sstevel@tonic-gate 			kmem_free(elem, sizeof (entity_attribute_t));
21760Sstevel@tonic-gate 			elem = *previous;
21770Sstevel@tonic-gate 		} else {
21780Sstevel@tonic-gate 			previous = &elem->entity_attribute_next;
21790Sstevel@tonic-gate 			elem = elem->entity_attribute_next;
21800Sstevel@tonic-gate 		}
21810Sstevel@tonic-gate 	}
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 	/* create new attribute for this entry */
21840Sstevel@tonic-gate 	newattrib = hidparser_alloc_attrib_list(1);
21850Sstevel@tonic-gate 	newattrib->entity_attribute_tag = entity;
21860Sstevel@tonic-gate 	newattrib->entity_attribute_value = kmem_zalloc(len, KM_SLEEP);
21870Sstevel@tonic-gate 	(void) bcopy(text, newattrib->entity_attribute_value, len);
21880Sstevel@tonic-gate 	newattrib->entity_attribute_length = len;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	/* attach to end of list */
21910Sstevel@tonic-gate 	*previous = newattrib;
21920Sstevel@tonic-gate }
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 
21950Sstevel@tonic-gate /*
21960Sstevel@tonic-gate  * hidparser_alloc_attrib_list:
21970Sstevel@tonic-gate  *	Allocate space for n attributes , create a linked list and
21980Sstevel@tonic-gate  *	return the head
21990Sstevel@tonic-gate  */
22000Sstevel@tonic-gate static entity_attribute_t *
hidparser_alloc_attrib_list(int count)22010Sstevel@tonic-gate hidparser_alloc_attrib_list(int count)
22020Sstevel@tonic-gate {
22030Sstevel@tonic-gate 	entity_attribute_t *head, *current;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	if (count <= 0) {
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 		return (NULL);
22080Sstevel@tonic-gate 	}
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	head = kmem_zalloc(sizeof (entity_attribute_t), KM_SLEEP);
22110Sstevel@tonic-gate 	count--;
22120Sstevel@tonic-gate 	current = head;
22130Sstevel@tonic-gate 	while (count--) {
22140Sstevel@tonic-gate 		current->entity_attribute_next = kmem_zalloc(
22150Sstevel@tonic-gate 		    sizeof (entity_attribute_t), KM_SLEEP);
22160Sstevel@tonic-gate 		current = current->entity_attribute_next;
22170Sstevel@tonic-gate 	}
22180Sstevel@tonic-gate 	current->entity_attribute_next = NULL;
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	return (head);
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate /*
22250Sstevel@tonic-gate  * hidparser_cp_attribute_list:
22260Sstevel@tonic-gate  *	Copies the Global item list pointed to by head
22270Sstevel@tonic-gate  *	We create a clone of the global item list here
22280Sstevel@tonic-gate  *	because we want to retain the Global items to
22290Sstevel@tonic-gate  *	the next Main Item.
22300Sstevel@tonic-gate  */
22310Sstevel@tonic-gate static entity_attribute_t *
hidparser_cp_attribute_list(entity_attribute_t * head)22320Sstevel@tonic-gate hidparser_cp_attribute_list(entity_attribute_t *head)
22330Sstevel@tonic-gate {
22340Sstevel@tonic-gate 	entity_attribute_t *return_value, *current_src, *current_dst;
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate 	if (!head) {
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 		return (NULL);
22390Sstevel@tonic-gate 	}
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 	current_src = head;
22420Sstevel@tonic-gate 	current_dst = return_value = hidparser_alloc_attrib_list(1);
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	while (current_src) {
22450Sstevel@tonic-gate 		current_dst->entity_attribute_tag =
22466898Sfb209375 		    current_src->entity_attribute_tag;
22470Sstevel@tonic-gate 		current_dst->entity_attribute_length =
22486898Sfb209375 		    current_src->entity_attribute_length;
22490Sstevel@tonic-gate 		current_dst->entity_attribute_value = kmem_zalloc(
22500Sstevel@tonic-gate 		    current_dst->entity_attribute_length, KM_SLEEP);
22510Sstevel@tonic-gate 		(void) bcopy(current_src->entity_attribute_value,
22520Sstevel@tonic-gate 		    current_dst->entity_attribute_value,
22530Sstevel@tonic-gate 		    current_src->entity_attribute_length);
22540Sstevel@tonic-gate 		if (current_src->entity_attribute_next) {
22550Sstevel@tonic-gate 			current_dst->entity_attribute_next =
22566898Sfb209375 			    hidparser_alloc_attrib_list(1);
22570Sstevel@tonic-gate 		} else {
22580Sstevel@tonic-gate 			current_dst->entity_attribute_next = NULL;
22590Sstevel@tonic-gate 		}
22600Sstevel@tonic-gate 		current_src = current_src->entity_attribute_next;
22610Sstevel@tonic-gate 		current_dst = current_dst->entity_attribute_next;
22620Sstevel@tonic-gate 	}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	return (return_value);
22650Sstevel@tonic-gate }
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate /*
22690Sstevel@tonic-gate  * hidparser_find_attribute_end:
22700Sstevel@tonic-gate  *	Find the last item in the attribute list pointed to by head
22710Sstevel@tonic-gate  */
22720Sstevel@tonic-gate static entity_attribute_t *
hidparser_find_attribute_end(entity_attribute_t * head)22730Sstevel@tonic-gate hidparser_find_attribute_end(entity_attribute_t *head)
22740Sstevel@tonic-gate {
22750Sstevel@tonic-gate 	if (head == NULL) {
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 		return (NULL);
22780Sstevel@tonic-gate 	}
22790Sstevel@tonic-gate 	while (head->entity_attribute_next != NULL) {
22800Sstevel@tonic-gate 		head = head->entity_attribute_next;
22810Sstevel@tonic-gate 	}
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	return (head);
22840Sstevel@tonic-gate }
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate /*
22880Sstevel@tonic-gate  * hidparser_free_report_descr_handle:
22890Sstevel@tonic-gate  *	Free the parse tree pointed to by handle
22900Sstevel@tonic-gate  */
22910Sstevel@tonic-gate static void
hidparser_free_report_descr_handle(entity_item_t * handle)22920Sstevel@tonic-gate hidparser_free_report_descr_handle(entity_item_t *handle)
22930Sstevel@tonic-gate {
22940Sstevel@tonic-gate 	entity_item_t *next, *current, *child;
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	current = handle;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	while (current) {
22990Sstevel@tonic-gate 		child = current->info.child;
23000Sstevel@tonic-gate 		next = current->entity_item_right_sibling;
23010Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
23020Sstevel@tonic-gate 			if (current->entity_item_params != NULL)
23030Sstevel@tonic-gate 				kmem_free(current->entity_item_params,
23040Sstevel@tonic-gate 				    current->entity_item_params_leng);
23050Sstevel@tonic-gate 			if (current->entity_item_attributes != NULL)
23060Sstevel@tonic-gate 				hidparser_free_attribute_list(
23070Sstevel@tonic-gate 				    current->entity_item_attributes);
23080Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
23090Sstevel@tonic-gate 			    "FREE 1: %s",
23100Sstevel@tonic-gate 			    items[current->entity_item_type]);
23110Sstevel@tonic-gate 			kmem_free(current, sizeof (entity_item_t));
23120Sstevel@tonic-gate 			(void) hidparser_free_report_descr_handle(child);
23130Sstevel@tonic-gate 		} else {
23140Sstevel@tonic-gate 			if (current->entity_item_params != NULL) {
23150Sstevel@tonic-gate 				kmem_free(current->entity_item_params,
23166898Sfb209375 				    current->entity_item_params_leng);
23170Sstevel@tonic-gate 			}
23180Sstevel@tonic-gate 			if (current->entity_item_attributes != NULL) {
23190Sstevel@tonic-gate 				hidparser_free_attribute_list(
23200Sstevel@tonic-gate 				    current->entity_item_attributes);
23210Sstevel@tonic-gate 			}
23220Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL,
23236898Sfb209375 			    hparser_log_handle, "FREE 2: %s",
23246898Sfb209375 			    items[current->entity_item_type]);
23250Sstevel@tonic-gate 			kmem_free(current, sizeof (entity_item_t));
23260Sstevel@tonic-gate 		}
23270Sstevel@tonic-gate 		current = next;
23280Sstevel@tonic-gate 	}
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate }
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate /*
23340Sstevel@tonic-gate  * hidparser_free_attribute_list:
23350Sstevel@tonic-gate  *	Free the attribute list pointed to by head
23360Sstevel@tonic-gate  */
23370Sstevel@tonic-gate static void
hidparser_free_attribute_list(entity_attribute_t * head)23380Sstevel@tonic-gate hidparser_free_attribute_list(entity_attribute_t *head)
23390Sstevel@tonic-gate {
23400Sstevel@tonic-gate 	entity_attribute_t *next, *current;
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	current = head;
23430Sstevel@tonic-gate 
23440Sstevel@tonic-gate 	while (current) {
23450Sstevel@tonic-gate 		next = current->entity_attribute_next;
23460Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
23470Sstevel@tonic-gate 		    hparser_log_handle, "FREE: %s value_length = %d",
23480Sstevel@tonic-gate 		    items[current->entity_attribute_tag],
23490Sstevel@tonic-gate 		    current->entity_attribute_length);
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 		if (current->entity_attribute_value != NULL) {
23520Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL,
23530Sstevel@tonic-gate 			    hparser_log_handle,
23540Sstevel@tonic-gate 			    "\tvalue = 0x%x",
23550Sstevel@tonic-gate 			    current->entity_attribute_value[0]);
23560Sstevel@tonic-gate 			kmem_free(current->entity_attribute_value,
23570Sstevel@tonic-gate 			    current->entity_attribute_length);
23580Sstevel@tonic-gate 		}
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 		kmem_free(current, sizeof (entity_attribute_t));
23610Sstevel@tonic-gate 		current = next;
23620Sstevel@tonic-gate 	}
23630Sstevel@tonic-gate }
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate /*
23670Sstevel@tonic-gate  * hidparser_initialize_items:
23680Sstevel@tonic-gate  *	Initialize items array before start scanning and parsing.
23690Sstevel@tonic-gate  *	This array of strings are used for printing purpose.
23700Sstevel@tonic-gate  */
23710Sstevel@tonic-gate static void
hidparser_initialize_items(void)23720Sstevel@tonic-gate hidparser_initialize_items(void)
23730Sstevel@tonic-gate {
23740Sstevel@tonic-gate 	items[R_ITEM_USAGE] = "Usage";
23750Sstevel@tonic-gate 	items[R_ITEM_USAGE_MIN] = "Usage Minimum";
23760Sstevel@tonic-gate 	items[R_ITEM_USAGE_MAX] = "Usage Maximum";
23770Sstevel@tonic-gate 	items[R_ITEM_DESIGNATOR_INDEX] = "Designator Index";
23780Sstevel@tonic-gate 	items[R_ITEM_DESIGNATOR_MIN] = "Designator Minimum";
23790Sstevel@tonic-gate 	items[R_ITEM_DESIGNATOR_MAX] = "Designator Maximum";
23800Sstevel@tonic-gate 	items[R_ITEM_STRING_INDEX] = "String Index";
23810Sstevel@tonic-gate 	items[R_ITEM_STRING_MIN] = "String Minimum";
23820Sstevel@tonic-gate 	items[R_ITEM_STRING_MAX] = "String Maximum";
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	items[R_ITEM_USAGE_PAGE] = "Usage Page";
23860Sstevel@tonic-gate 	items[R_ITEM_LOGICAL_MINIMUM] = "Logical Minimum";
23870Sstevel@tonic-gate 	items[R_ITEM_LOGICAL_MAXIMUM] = "Logical Maximum";
23880Sstevel@tonic-gate 	items[R_ITEM_PHYSICAL_MINIMUM] = "Physical Minimum";
23890Sstevel@tonic-gate 	items[R_ITEM_PHYSICAL_MAXIMUM] = "Physical Maximum";
23900Sstevel@tonic-gate 	items[R_ITEM_EXPONENT] = "Exponent";
23910Sstevel@tonic-gate 	items[R_ITEM_UNIT] = "Unit";
23920Sstevel@tonic-gate 	items[R_ITEM_REPORT_SIZE] = "Report Size";
23930Sstevel@tonic-gate 	items[R_ITEM_REPORT_ID] = "Report Id";
23940Sstevel@tonic-gate 	items[R_ITEM_REPORT_COUNT] = "Report Count";
23950Sstevel@tonic-gate 	items[R_ITEM_PUSH] = "Push";
23960Sstevel@tonic-gate 	items[R_ITEM_POP] = "Pop";
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	items[R_ITEM_INPUT] = "Input";
24000Sstevel@tonic-gate 	items[R_ITEM_OUTPUT] = "Output";
24010Sstevel@tonic-gate 	items[R_ITEM_COLLECTION] = "Collection";
24020Sstevel@tonic-gate 	items[R_ITEM_FEATURE] = "Feature";
24030Sstevel@tonic-gate 	items[R_ITEM_END_COLLECTION] = "End Collection";
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 	items[R_ITEM_SET_DELIMITER] = "Delimiter";
24060Sstevel@tonic-gate }
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate /*
24100Sstevel@tonic-gate  * hidparser_scan:
24110Sstevel@tonic-gate  *	This function scans the input entity descriptor, sees the data
24120Sstevel@tonic-gate  *	length, returns the next token, data bytes and length in the
24130Sstevel@tonic-gate  *	scan_ifp structure.
24140Sstevel@tonic-gate  */
24150Sstevel@tonic-gate static void
hidparser_scan(hidparser_tok_t * scan_ifp)24160Sstevel@tonic-gate hidparser_scan(hidparser_tok_t	*scan_ifp)
24170Sstevel@tonic-gate {
24180Sstevel@tonic-gate 	int count;
24190Sstevel@tonic-gate 	int ch;
24200Sstevel@tonic-gate 	int parsed_length;
24210Sstevel@tonic-gate 	unsigned char *parsed_text;
24220Sstevel@tonic-gate 	unsigned char *entity_descriptor;
24230Sstevel@tonic-gate 	char err_str[32];
24240Sstevel@tonic-gate 	size_t	entity_buffer_size, index;
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 	index = scan_ifp->hidparser_tok_index;
24270Sstevel@tonic-gate 	entity_buffer_size = scan_ifp->hidparser_tok_max_bsize;
24280Sstevel@tonic-gate 	parsed_length = 0;
24290Sstevel@tonic-gate 	parsed_text = scan_ifp->hidparser_tok_text;
24300Sstevel@tonic-gate 	entity_descriptor = scan_ifp->hidparser_tok_entity_descriptor;
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate next_item:
24330Sstevel@tonic-gate 	if (index <= entity_buffer_size -1) {
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate 		ch = 0xFF & entity_descriptor[index];
24360Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24370Sstevel@tonic-gate 		    hparser_log_handle, "scanner: index  = 0x%lx ch = 0x%x",
24380Sstevel@tonic-gate 		    index, ch);
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 		index++;
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 		/*
24430Sstevel@tonic-gate 		 * Error checking:
24440Sstevel@tonic-gate 		 * Unrecognized items should be passed over
24450Sstevel@tonic-gate 		 * by the parser.
24460Sstevel@tonic-gate 		 * Section 5.4
24470Sstevel@tonic-gate 		 */
24480Sstevel@tonic-gate 		if (!(hidparser_isvalid_item(ch))) {
24490Sstevel@tonic-gate 			(void) sprintf(err_str, "%s: 0x%2x",
24500Sstevel@tonic-gate 			    "Unknown or reserved item", ch);
24510Sstevel@tonic-gate 			hidparser_report_err(HIDPARSER_ERR_ERROR,
24520Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD, 0, 0x3F, err_str);
24530Sstevel@tonic-gate 			goto next_item;
24540Sstevel@tonic-gate 		}
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 		if (ch == EXTENDED_ITEM) {
24570Sstevel@tonic-gate 			parsed_length = entity_descriptor[index++];
24580Sstevel@tonic-gate 			ch = entity_descriptor[index++];
24590Sstevel@tonic-gate 			hidparser_report_err(HIDPARSER_ERR_WARN,
24600Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
24610Sstevel@tonic-gate 			    0,
24620Sstevel@tonic-gate 			    0x3E,
24630Sstevel@tonic-gate 			    "Long item defined");
24640Sstevel@tonic-gate 		} else {
24650Sstevel@tonic-gate 			parsed_length = ch & 0x03;
24660Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL,
24670Sstevel@tonic-gate 			    hparser_log_handle,
24680Sstevel@tonic-gate 			    "scanner: parsed_length = %x", parsed_length);
24690Sstevel@tonic-gate 			/* 3 really means 4.. see p.21 HID */
24700Sstevel@tonic-gate 			if (parsed_length == 3)
24710Sstevel@tonic-gate 				parsed_length++;
24720Sstevel@tonic-gate 		}
24730Sstevel@tonic-gate 		for (count = 0; count < parsed_length; count++) {
24740Sstevel@tonic-gate 			parsed_text[count] = entity_descriptor[index];
24750Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
24760Sstevel@tonic-gate 			    "scanner: parsed_text[%d] = 0x%x,"
24770Sstevel@tonic-gate 			    "index = 0x%lx",
24780Sstevel@tonic-gate 			    count, parsed_text[count], index);
24790Sstevel@tonic-gate 			index++;
24800Sstevel@tonic-gate 		}
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24830Sstevel@tonic-gate 		    hparser_log_handle, "scanner: lexical analyzer found 0x%x "
24840Sstevel@tonic-gate 		    "before translation", ch);
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 		scan_ifp->hidparser_tok_index = index;
24870Sstevel@tonic-gate 		scan_ifp->hidparser_tok_leng = parsed_length;
24880Sstevel@tonic-gate 		scan_ifp->hidparser_tok_token = ch & 0xFC;
24890Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24900Sstevel@tonic-gate 		    hparser_log_handle, "scanner: aindex  = 0x%lx", index);
24910Sstevel@tonic-gate 	} else {
24920Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24930Sstevel@tonic-gate 		    hparser_log_handle, "scanner: eindex  = 0x%lx", index);
24940Sstevel@tonic-gate 		scan_ifp->hidparser_tok_leng = 0;
24950Sstevel@tonic-gate 		scan_ifp->hidparser_tok_token = 0;	/* EOF */
24960Sstevel@tonic-gate 	}
24970Sstevel@tonic-gate }
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate /*
25010Sstevel@tonic-gate  * hidparser_report_err:
25020Sstevel@tonic-gate  *	Construct and print the error code
25030Sstevel@tonic-gate  *	Ref: Hidview error check list
25040Sstevel@tonic-gate  */
25050Sstevel@tonic-gate static void
hidparser_report_err(int err_level,int err_type,int tag,int subcode,char * msg)25060Sstevel@tonic-gate hidparser_report_err(int err_level,
25070Sstevel@tonic-gate 			int err_type,
25080Sstevel@tonic-gate 			int tag,
25090Sstevel@tonic-gate 			int subcode,
25100Sstevel@tonic-gate 			char *msg)
25110Sstevel@tonic-gate {
25120Sstevel@tonic-gate 	unsigned int	BmParserErrorCode = 0;
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	if (err_level) {
25150Sstevel@tonic-gate 		BmParserErrorCode |= HIDPARSER_ERR_ERROR;
25160Sstevel@tonic-gate 	}
25170Sstevel@tonic-gate 	if (err_type) {
25180Sstevel@tonic-gate 		BmParserErrorCode |= HIDPARSER_ERR_STANDARD;
25190Sstevel@tonic-gate 	}
25200Sstevel@tonic-gate 	BmParserErrorCode |= (tag << 8) & HIDPARSER_ERR_TAG_MASK;
25210Sstevel@tonic-gate 	BmParserErrorCode |= subcode & HIDPARSER_ERR_SUBCODE_MASK;
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	if (err_level) {
25240Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
25250Sstevel@tonic-gate 		    "err code = 0x%4x, err str = %s",
25260Sstevel@tonic-gate 		    BmParserErrorCode, msg);
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	} else {
25290Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
25300Sstevel@tonic-gate 		    "wrn code = 0x%4x, wrn str = %s",
25310Sstevel@tonic-gate 		    BmParserErrorCode, msg);
25320Sstevel@tonic-gate 	}
25330Sstevel@tonic-gate }
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate /*
25370Sstevel@tonic-gate  * hidparser_isvalid_item:
25380Sstevel@tonic-gate  *	Find if the item tag is a valid one
25390Sstevel@tonic-gate  */
25400Sstevel@tonic-gate static int
hidparser_isvalid_item(int tag)25410Sstevel@tonic-gate hidparser_isvalid_item(int tag)
25420Sstevel@tonic-gate {
25430Sstevel@tonic-gate 	if (tag == EXTENDED_ITEM) {
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 		return (1);
25460Sstevel@tonic-gate 	}
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 	tag &= 0xFC;
25490Sstevel@tonic-gate 	if ((tag == R_ITEM_INPUT) ||
25500Sstevel@tonic-gate 	    (tag == R_ITEM_OUTPUT) ||
25510Sstevel@tonic-gate 	    (tag == R_ITEM_COLLECTION) ||
25520Sstevel@tonic-gate 	    (tag == R_ITEM_FEATURE) ||
25530Sstevel@tonic-gate 	    (tag == R_ITEM_END_COLLECTION) ||
25540Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE_PAGE) ||
25550Sstevel@tonic-gate 	    (tag == R_ITEM_LOGICAL_MINIMUM) ||
25560Sstevel@tonic-gate 	    (tag == R_ITEM_LOGICAL_MAXIMUM) ||
25570Sstevel@tonic-gate 	    (tag == R_ITEM_PHYSICAL_MINIMUM) ||
25580Sstevel@tonic-gate 	    (tag == R_ITEM_PHYSICAL_MAXIMUM) ||
25590Sstevel@tonic-gate 	    (tag == R_ITEM_EXPONENT) ||
25600Sstevel@tonic-gate 	    (tag == R_ITEM_UNIT) ||
25610Sstevel@tonic-gate 	    (tag == R_ITEM_REPORT_SIZE) ||
25620Sstevel@tonic-gate 	    (tag == R_ITEM_REPORT_ID) ||
25630Sstevel@tonic-gate 	    (tag == R_ITEM_REPORT_COUNT) ||
25640Sstevel@tonic-gate 	    (tag == R_ITEM_PUSH) ||
25650Sstevel@tonic-gate 	    (tag == R_ITEM_POP) ||
25660Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE) ||
25670Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE_MIN) ||
25680Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE_MAX) ||
25690Sstevel@tonic-gate 	    (tag == R_ITEM_DESIGNATOR_INDEX) ||
25700Sstevel@tonic-gate 	    (tag == R_ITEM_DESIGNATOR_MIN) ||
25710Sstevel@tonic-gate 	    (tag == R_ITEM_DESIGNATOR_MAX) ||
25720Sstevel@tonic-gate 	    (tag == R_ITEM_STRING_INDEX) ||
25730Sstevel@tonic-gate 	    (tag == R_ITEM_STRING_MIN) ||
25740Sstevel@tonic-gate 	    (tag == R_ITEM_STRING_MAX) ||
25750Sstevel@tonic-gate 	    (tag == R_ITEM_SET_DELIMITER)) {
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 		return (1);
25780Sstevel@tonic-gate 	} else {
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 		return (0);
25810Sstevel@tonic-gate 	}
25820Sstevel@tonic-gate }
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate /*
25860Sstevel@tonic-gate  * hidparser_lookup_attribute:
25870Sstevel@tonic-gate  *	Takes an item pointer(report structure) and a tag(e.g Logical
25880Sstevel@tonic-gate  *	Min) as input. Returns the corresponding attribute structure.
25890Sstevel@tonic-gate  *	Presently used for error checking only.
25900Sstevel@tonic-gate  */
25910Sstevel@tonic-gate static entity_attribute_t *
hidparser_lookup_attribute(entity_item_t * item,int attr_tag)25920Sstevel@tonic-gate hidparser_lookup_attribute(entity_item_t *item, int attr_tag)
25930Sstevel@tonic-gate {
25940Sstevel@tonic-gate 	entity_attribute_t *temp;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 	if (item == NULL) {
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate 		return (NULL);
25990Sstevel@tonic-gate 	}
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 	temp = item->entity_item_attributes;
26020Sstevel@tonic-gate 	while (temp != NULL) {
26030Sstevel@tonic-gate 		if (temp->entity_attribute_tag == attr_tag) {
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 			return (temp);
26060Sstevel@tonic-gate 		}
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 		temp = temp->entity_attribute_next;
26090Sstevel@tonic-gate 	}
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	return (NULL);
26120Sstevel@tonic-gate }
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate /*
26160Sstevel@tonic-gate  * hidparser_global_err_check:
26170Sstevel@tonic-gate  *	Error checking for Global Items that need to be
26180Sstevel@tonic-gate  *	performed in MainItem
26190Sstevel@tonic-gate  */
26200Sstevel@tonic-gate static void
hidparser_global_err_check(entity_item_t * mainitem)26210Sstevel@tonic-gate hidparser_global_err_check(entity_item_t *mainitem)
26220Sstevel@tonic-gate {
26230Sstevel@tonic-gate 	hidparser_check_minmax_val_signed(mainitem, R_ITEM_LOGICAL_MINIMUM,
26240Sstevel@tonic-gate 	    R_ITEM_LOGICAL_MAXIMUM, 0, 0);
26250Sstevel@tonic-gate 	hidparser_check_minmax_val_signed(mainitem, R_ITEM_PHYSICAL_MINIMUM,
26260Sstevel@tonic-gate 	    R_ITEM_PHYSICAL_MAXIMUM, 0, 0);
26270Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_PHYSICAL_MINIMUM,
26280Sstevel@tonic-gate 	    R_ITEM_PHYSICAL_MAXIMUM, 0, 0,
26290Sstevel@tonic-gate 	    "Must have a corresponding Physical min",
26300Sstevel@tonic-gate 	    "Must have a corresponding Physical max");
26310Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_PUSH, R_ITEM_POP,
26320Sstevel@tonic-gate 	    1, 0, "Should have a corresponding Pop",
26330Sstevel@tonic-gate 	    "Must have a corresponding Push");
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate }
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate /*
26390Sstevel@tonic-gate  * hidparser_mainitem_err_check:
26400Sstevel@tonic-gate  *	Error checking for Main Items
26410Sstevel@tonic-gate  */
26420Sstevel@tonic-gate static void
hidparser_mainitem_err_check(entity_item_t * mainitem)26430Sstevel@tonic-gate hidparser_mainitem_err_check(entity_item_t *mainitem)
26440Sstevel@tonic-gate {
26450Sstevel@tonic-gate 	int	itemmask = 0;
26460Sstevel@tonic-gate 	entity_attribute_t *attr;
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 	attr = mainitem->entity_item_attributes;
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	if (attr != NULL) {
26510Sstevel@tonic-gate 		while (attr) {
26520Sstevel@tonic-gate 			switch (attr->entity_attribute_tag) {
26530Sstevel@tonic-gate 				case R_ITEM_LOGICAL_MINIMUM:
26540Sstevel@tonic-gate 					itemmask |= 0x01;
26550Sstevel@tonic-gate 					break;
26560Sstevel@tonic-gate 				case R_ITEM_LOGICAL_MAXIMUM:
26570Sstevel@tonic-gate 					itemmask |= 0x02;
26580Sstevel@tonic-gate 					break;
26590Sstevel@tonic-gate 				case R_ITEM_REPORT_SIZE:
26600Sstevel@tonic-gate 					itemmask |= 0x04;
26610Sstevel@tonic-gate 					break;
26620Sstevel@tonic-gate 				case R_ITEM_REPORT_COUNT:
26630Sstevel@tonic-gate 					itemmask |= 0x08;
26640Sstevel@tonic-gate 					break;
26650Sstevel@tonic-gate 				case R_ITEM_USAGE_PAGE:
26660Sstevel@tonic-gate 					itemmask |= 0x10;
26670Sstevel@tonic-gate 					break;
26680Sstevel@tonic-gate 				default:
26690Sstevel@tonic-gate 					break;
26700Sstevel@tonic-gate 			} /* switch */
26710Sstevel@tonic-gate 			attr = attr->entity_attribute_next;
26720Sstevel@tonic-gate 		} /* while */
26730Sstevel@tonic-gate 	} /* if */
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	if ((mainitem->entity_item_type == R_ITEM_COLLECTION) ||
26766898Sfb209375 	    (mainitem->entity_item_type == R_ITEM_END_COLLECTION)) {
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 		return;
26790Sstevel@tonic-gate 	}
26800Sstevel@tonic-gate 	if (itemmask != 0x1f) {
26810Sstevel@tonic-gate 			hidparser_report_err(
26820Sstevel@tonic-gate 			    HIDPARSER_ERR_ERROR,
26830Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
26840Sstevel@tonic-gate 			    mainitem->entity_item_type,
26850Sstevel@tonic-gate 			    0,
26860Sstevel@tonic-gate 			    "Required Global/Local items must be defined");
26870Sstevel@tonic-gate 	}
26880Sstevel@tonic-gate }
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate /*
26920Sstevel@tonic-gate  * hidparser_local_err_check:
26930Sstevel@tonic-gate  *	Error checking for Local items that is done when a MainItem
26940Sstevel@tonic-gate  *	is encountered
26950Sstevel@tonic-gate  */
26960Sstevel@tonic-gate static void
hidparser_local_err_check(entity_item_t * mainitem)26970Sstevel@tonic-gate hidparser_local_err_check(entity_item_t *mainitem)
26980Sstevel@tonic-gate {
26990Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_USAGE_MIN,
27000Sstevel@tonic-gate 	    R_ITEM_USAGE_MAX, 0, 0,
27010Sstevel@tonic-gate 	    "Must have a corresponding Usage Min",
27020Sstevel@tonic-gate 	    "Must have a corresponding Usage Max");
27030Sstevel@tonic-gate 	hidparser_check_minmax_val(mainitem, R_ITEM_USAGE_MIN,
27040Sstevel@tonic-gate 	    R_ITEM_USAGE_MAX, 1, 1);
27050Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_DESIGNATOR_MIN,
27060Sstevel@tonic-gate 	    R_ITEM_DESIGNATOR_MAX, 0, 0,
27070Sstevel@tonic-gate 	    "Must have a corresponding Designator min",
27080Sstevel@tonic-gate 	    "Must have a corresponding Designator Max");
27090Sstevel@tonic-gate 	hidparser_check_minmax_val(mainitem, R_ITEM_DESIGNATOR_MIN,
27100Sstevel@tonic-gate 	    R_ITEM_DESIGNATOR_MAX, 1, 1);
27110Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_STRING_MIN,
27120Sstevel@tonic-gate 	    R_ITEM_STRING_MAX, 0, 0,
27130Sstevel@tonic-gate 	    "Must have a corresponding String min",
27140Sstevel@tonic-gate 	    "Must have a corresponding String Max");
27150Sstevel@tonic-gate 	hidparser_check_minmax_val(mainitem, R_ITEM_STRING_MIN,
27160Sstevel@tonic-gate 	    R_ITEM_STRING_MAX, 1, 1);
27170Sstevel@tonic-gate }
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate /*
27210Sstevel@tonic-gate  * hidparser_find_unsigned_val:
27220Sstevel@tonic-gate  *	Find the value for multibyte data
27230Sstevel@tonic-gate  *	Ref: Section 5.8 of HID Spec 1.0
27240Sstevel@tonic-gate  */
27250Sstevel@tonic-gate static unsigned int
hidparser_find_unsigned_val(entity_attribute_t * attr)27260Sstevel@tonic-gate hidparser_find_unsigned_val(entity_attribute_t *attr)
27270Sstevel@tonic-gate {
27280Sstevel@tonic-gate 	char *text;
27290Sstevel@tonic-gate 	int len, i;
27300Sstevel@tonic-gate 	unsigned int ret = 0;
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	text = attr->entity_attribute_value;
27330Sstevel@tonic-gate 	len = attr->entity_attribute_length;
27340Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
27350Sstevel@tonic-gate 		ret |= ((text[i] & 0xff) << (8*i));
27360Sstevel@tonic-gate 	}
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	return (ret);
27390Sstevel@tonic-gate }
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate /*
27430Sstevel@tonic-gate  * hidparser_find_signed_val:
27440Sstevel@tonic-gate  *	Find the value for signed multibyte data
27450Sstevel@tonic-gate  *	Ref: Section 5.8 of HID Spec 1.0
27460Sstevel@tonic-gate  */
27470Sstevel@tonic-gate static signed int
hidparser_find_signed_val(entity_attribute_t * attr)27480Sstevel@tonic-gate hidparser_find_signed_val(entity_attribute_t *attr)
27490Sstevel@tonic-gate {
27500Sstevel@tonic-gate 	char *text;
27510Sstevel@tonic-gate 	int len, i;
27520Sstevel@tonic-gate 	int ret = 0;
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	text = attr->entity_attribute_value;
27550Sstevel@tonic-gate 	len = attr->entity_attribute_length;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	for (i = 0; i < len - 1; i++) {
27580Sstevel@tonic-gate 		ret |= ((text[i] & 0xff) << (8 * i));
27590Sstevel@tonic-gate 	}
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	if (len > 0) {
27620Sstevel@tonic-gate 		ret |= (text[i] << (8 * i));
27630Sstevel@tonic-gate 	}
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	return (ret);
27660Sstevel@tonic-gate }
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate /*
27700Sstevel@tonic-gate  * hidparser_check_correspondence:
27710Sstevel@tonic-gate  *	Check if the item item2 corresponding to item1 exists and vice versa
27720Sstevel@tonic-gate  *	If not report the appropriate error
27730Sstevel@tonic-gate  */
27740Sstevel@tonic-gate static void
hidparser_check_correspondence(entity_item_t * mainitem,int item_tag1,int item_tag2,int val1,int val2,char * str1,char * str2)27750Sstevel@tonic-gate hidparser_check_correspondence(entity_item_t *mainitem,
27760Sstevel@tonic-gate 			int item_tag1,
27770Sstevel@tonic-gate 			int item_tag2,
27780Sstevel@tonic-gate 			int val1,
27790Sstevel@tonic-gate 			int val2,
27800Sstevel@tonic-gate 			char *str1,
27810Sstevel@tonic-gate 			char *str2)
27820Sstevel@tonic-gate {
27830Sstevel@tonic-gate 	entity_attribute_t *temp1, *temp2;
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	temp1 = hidparser_lookup_attribute(mainitem, item_tag1);
27860Sstevel@tonic-gate 	temp2 = hidparser_lookup_attribute(mainitem, item_tag2);
27870Sstevel@tonic-gate 	if ((temp1 != NULL) && (temp2 == NULL)) {
27880Sstevel@tonic-gate 		hidparser_report_err(
27890Sstevel@tonic-gate 		    HIDPARSER_ERR_ERROR,
27900Sstevel@tonic-gate 		    HIDPARSER_ERR_STANDARD,
27910Sstevel@tonic-gate 		    item_tag1,
27920Sstevel@tonic-gate 		    val1,
27930Sstevel@tonic-gate 		    str1);
27940Sstevel@tonic-gate 	}
27950Sstevel@tonic-gate 	if ((temp2 != NULL) && (temp1 == NULL)) {
27960Sstevel@tonic-gate 		hidparser_report_err(
27970Sstevel@tonic-gate 		    HIDPARSER_ERR_ERROR,
27980Sstevel@tonic-gate 		    HIDPARSER_ERR_STANDARD,
27990Sstevel@tonic-gate 		    item_tag2,
28000Sstevel@tonic-gate 		    val2,
28010Sstevel@tonic-gate 		    str2);
28020Sstevel@tonic-gate 	}
28030Sstevel@tonic-gate }
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate /*
28070Sstevel@tonic-gate  * hidparser_check_minmax_val:
28080Sstevel@tonic-gate  *	Check if the Min value <= Max and vice versa
28090Sstevel@tonic-gate  *	Print for warnings and errors have been taken care separately.
28100Sstevel@tonic-gate  */
28110Sstevel@tonic-gate static void
hidparser_check_minmax_val(entity_item_t * mainitem,int item_tag1,int item_tag2,int val1,int val2)28120Sstevel@tonic-gate hidparser_check_minmax_val(entity_item_t *mainitem,
28130Sstevel@tonic-gate 			int item_tag1,
28140Sstevel@tonic-gate 			int item_tag2,
28150Sstevel@tonic-gate 			int val1,
28160Sstevel@tonic-gate 			int val2)
28170Sstevel@tonic-gate {
28180Sstevel@tonic-gate 	entity_attribute_t *temp1, *temp2;
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 	temp1 = hidparser_lookup_attribute(mainitem, item_tag1);
28210Sstevel@tonic-gate 	temp2 = hidparser_lookup_attribute(mainitem, item_tag2);
28220Sstevel@tonic-gate 	if ((temp1 != NULL) && (temp2 != NULL)) {
28230Sstevel@tonic-gate 		if (hidparser_find_unsigned_val(temp1) >
28246898Sfb209375 		    hidparser_find_unsigned_val(temp2)) {
28250Sstevel@tonic-gate 			if ((item_tag1 == R_ITEM_LOGICAL_MINIMUM) ||
28260Sstevel@tonic-gate 			    (item_tag1 == R_ITEM_PHYSICAL_MINIMUM)) {
28270Sstevel@tonic-gate 				hidparser_report_err(
28280Sstevel@tonic-gate 				    HIDPARSER_ERR_WARN,
28290Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28300Sstevel@tonic-gate 				    item_tag1,
28310Sstevel@tonic-gate 				    val1,
28320Sstevel@tonic-gate 				    "unsigned: Min should be <= to Max");
28330Sstevel@tonic-gate 			} else {
28340Sstevel@tonic-gate 				hidparser_report_err(
28350Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28360Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28370Sstevel@tonic-gate 				    item_tag1,
28380Sstevel@tonic-gate 				    val1,
28390Sstevel@tonic-gate 				    "Min must be <= to Max");
28400Sstevel@tonic-gate 			}
28410Sstevel@tonic-gate 		}
28420Sstevel@tonic-gate 		if (hidparser_find_unsigned_val(temp2) <
28436898Sfb209375 		    hidparser_find_unsigned_val(temp1)) {
28440Sstevel@tonic-gate 			if ((item_tag2 == R_ITEM_LOGICAL_MAXIMUM) ||
28450Sstevel@tonic-gate 			    (item_tag2 == R_ITEM_PHYSICAL_MAXIMUM)) {
28460Sstevel@tonic-gate 				hidparser_report_err(
28470Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28480Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28490Sstevel@tonic-gate 				    item_tag2,
28500Sstevel@tonic-gate 				    val2,
28510Sstevel@tonic-gate 				    "unsigned: Max should be >= to Min");
28520Sstevel@tonic-gate 			} else {
28530Sstevel@tonic-gate 				hidparser_report_err(
28540Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28550Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28560Sstevel@tonic-gate 				    item_tag2,
28570Sstevel@tonic-gate 				    val2,
28580Sstevel@tonic-gate 				    "Max must be >= to Min");
28590Sstevel@tonic-gate 			}
28600Sstevel@tonic-gate 		}
28610Sstevel@tonic-gate 	}	/* if (temp1 != NULL) && (temp2 != NULL) */
28620Sstevel@tonic-gate }
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate /*
28660Sstevel@tonic-gate  * hidparser_check_minmax_val_signed:
28670Sstevel@tonic-gate  *	Check if the Min value <= Max and vice versa
28680Sstevel@tonic-gate  *	Print for warnings and errors have been taken care separately.
28690Sstevel@tonic-gate  */
28700Sstevel@tonic-gate static void
hidparser_check_minmax_val_signed(entity_item_t * mainitem,int item_tag1,int item_tag2,int val1,int val2)28710Sstevel@tonic-gate hidparser_check_minmax_val_signed(entity_item_t *mainitem,
28720Sstevel@tonic-gate 			int item_tag1,
28730Sstevel@tonic-gate 			int item_tag2,
28740Sstevel@tonic-gate 			int val1,
28750Sstevel@tonic-gate 			int val2)
28760Sstevel@tonic-gate {
28770Sstevel@tonic-gate 	entity_attribute_t *temp1, *temp2;
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 	temp1 = hidparser_lookup_attribute(mainitem, item_tag1);
28800Sstevel@tonic-gate 	temp2 = hidparser_lookup_attribute(mainitem, item_tag2);
28810Sstevel@tonic-gate 	if ((temp1 != NULL) && (temp2 != NULL)) {
28820Sstevel@tonic-gate 		if (hidparser_find_signed_val(temp1) >
28836898Sfb209375 		    hidparser_find_signed_val(temp2)) {
28840Sstevel@tonic-gate 			if ((item_tag1 == R_ITEM_LOGICAL_MINIMUM) ||
28850Sstevel@tonic-gate 			    (item_tag1 == R_ITEM_PHYSICAL_MINIMUM)) {
28860Sstevel@tonic-gate 				hidparser_report_err(
28870Sstevel@tonic-gate 				    HIDPARSER_ERR_WARN,
28880Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28890Sstevel@tonic-gate 				    item_tag1,
28900Sstevel@tonic-gate 				    val1,
28910Sstevel@tonic-gate 				    "signed: Min should be <= to Max");
28920Sstevel@tonic-gate 			} else {
28930Sstevel@tonic-gate 				hidparser_report_err(
28940Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28950Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28960Sstevel@tonic-gate 				    item_tag1,
28970Sstevel@tonic-gate 				    val1,
28980Sstevel@tonic-gate 				    "Min must be <= to Max");
28990Sstevel@tonic-gate 			}
29000Sstevel@tonic-gate 		}
29010Sstevel@tonic-gate 		if (hidparser_find_signed_val(temp2) <
29026898Sfb209375 		    hidparser_find_signed_val(temp1)) {
29030Sstevel@tonic-gate 			if ((item_tag2 == R_ITEM_LOGICAL_MAXIMUM) ||
29040Sstevel@tonic-gate 			    (item_tag2 == R_ITEM_PHYSICAL_MAXIMUM)) {
29050Sstevel@tonic-gate 				hidparser_report_err(
29060Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
29070Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
29080Sstevel@tonic-gate 				    item_tag2,
29090Sstevel@tonic-gate 				    val2,
29100Sstevel@tonic-gate 				    "signed: Max should be >= to Min");
29110Sstevel@tonic-gate 			} else {
29120Sstevel@tonic-gate 				hidparser_report_err(
29130Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
29140Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
29150Sstevel@tonic-gate 				    item_tag2,
29160Sstevel@tonic-gate 				    val2,
29170Sstevel@tonic-gate 				    "Max must be >= to Min");
29180Sstevel@tonic-gate 			}
29190Sstevel@tonic-gate 		}
29200Sstevel@tonic-gate 	}	/* if (temp1 != NULL) && (temp2 != NULL) */
29210Sstevel@tonic-gate }
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate /*
29250Sstevel@tonic-gate  * hidparser_error_delim:
29260Sstevel@tonic-gate  *	Error check for Delimiter Sets
29270Sstevel@tonic-gate  */
29280Sstevel@tonic-gate static void
hidparser_error_delim(entity_item_t * item,int err)29290Sstevel@tonic-gate hidparser_error_delim(entity_item_t *item, int err)
29300Sstevel@tonic-gate {
29310Sstevel@tonic-gate 	entity_attribute_t *attr;
29320Sstevel@tonic-gate 	switch (err) {
29330Sstevel@tonic-gate 		case HIDPARSER_DELIM_ERR1:
29340Sstevel@tonic-gate 			hidparser_report_err(
29350Sstevel@tonic-gate 			    HIDPARSER_ERR_ERROR,
29360Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
29370Sstevel@tonic-gate 			    R_ITEM_SET_DELIMITER,
29380Sstevel@tonic-gate 			    0,
29390Sstevel@tonic-gate 			    "Must be Delimiter Open");
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 			break;
29420Sstevel@tonic-gate 		case HIDPARSER_DELIM_ERR2:
29430Sstevel@tonic-gate 			hidparser_report_err(
29440Sstevel@tonic-gate 			    HIDPARSER_ERR_ERROR,
29450Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
29460Sstevel@tonic-gate 			    R_ITEM_SET_DELIMITER,
29470Sstevel@tonic-gate 			    0,
29480Sstevel@tonic-gate 			    "Must be Delimiter Close");
29490Sstevel@tonic-gate 
29500Sstevel@tonic-gate 			break;
29510Sstevel@tonic-gate 		case HIDPARSER_DELIM_ERR3:
29520Sstevel@tonic-gate 			attr = item->entity_item_attributes;
29530Sstevel@tonic-gate 			while (attr != NULL) {
29540Sstevel@tonic-gate 				if ((attr->entity_attribute_tag !=
29550Sstevel@tonic-gate 				    R_ITEM_USAGE) &&
29560Sstevel@tonic-gate 				    (attr->entity_attribute_tag !=
29570Sstevel@tonic-gate 				    R_ITEM_USAGE_MIN) &&
29580Sstevel@tonic-gate 				    (attr->entity_attribute_tag !=
29590Sstevel@tonic-gate 				    R_ITEM_USAGE_MAX)) {
29600Sstevel@tonic-gate 					hidparser_report_err(
29610Sstevel@tonic-gate 					    HIDPARSER_ERR_ERROR,
29620Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
29630Sstevel@tonic-gate 					    R_ITEM_SET_DELIMITER,
29640Sstevel@tonic-gate 					    3,
29650Sstevel@tonic-gate 					    "May only contain Usage, "
29660Sstevel@tonic-gate 					    "Usage Min and Usage Max");
29670Sstevel@tonic-gate 				}
29680Sstevel@tonic-gate 				attr = attr->entity_attribute_next;
29690Sstevel@tonic-gate 			}
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 			break;
29720Sstevel@tonic-gate 		default:
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 			break;
29750Sstevel@tonic-gate 	}
29760Sstevel@tonic-gate }
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 
29790Sstevel@tonic-gate /*
29800Sstevel@tonic-gate  * hidparser_find_max_packet_size_from_report_descriptor:
29810Sstevel@tonic-gate  *	find packet size of the largest report in the report descriptor
29820Sstevel@tonic-gate  */
29830Sstevel@tonic-gate void
hidparser_find_max_packet_size_from_report_descriptor(hidparser_handle_t hparser_handle,hidparser_packet_info_t * hpack)29840Sstevel@tonic-gate hidparser_find_max_packet_size_from_report_descriptor(
29850Sstevel@tonic-gate 			hidparser_handle_t hparser_handle,
29860Sstevel@tonic-gate 			hidparser_packet_info_t *hpack)
29870Sstevel@tonic-gate {
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate 	int				rval, i;
29900Sstevel@tonic-gate 	uint_t				packet_size;
29910Sstevel@tonic-gate 	uint_t				max_packet_size;
29920Sstevel@tonic-gate 	uint_t				max_report_id;
29930Sstevel@tonic-gate 	hidparser_report_id_list_t	report_id_list;
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
29960Sstevel@tonic-gate 	    "hidparser_find_max_packet_size_from_report_descriptor");
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 	/* get a list of input reports */
29990Sstevel@tonic-gate 	rval = hidparser_get_report_id_list(hparser_handle,
30000Sstevel@tonic-gate 	    R_ITEM_INPUT, &report_id_list);
30010Sstevel@tonic-gate 	if (rval != HIDPARSER_SUCCESS) {
30020Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
30030Sstevel@tonic-gate 		    "No report id used");
30040Sstevel@tonic-gate 	} else {
30050Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
30060Sstevel@tonic-gate 		    "%d unique report IDs found in hid report descriptor",
30070Sstevel@tonic-gate 		    report_id_list.no_of_report_ids);
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 		for (i = 0; i < (report_id_list.no_of_report_ids); i++) {
30100Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
30110Sstevel@tonic-gate 			    "report_id: %d", report_id_list.report_id[i]);
30120Sstevel@tonic-gate 		}
30130Sstevel@tonic-gate 	}
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	if ((rval != HIDPARSER_SUCCESS) ||
30160Sstevel@tonic-gate 	    (report_id_list.no_of_report_ids == 0)) {
30170Sstevel@tonic-gate 		/*
30180Sstevel@tonic-gate 		 * since no report id is used, get the packet size
30190Sstevel@tonic-gate 		 * for the only report available
30200Sstevel@tonic-gate 		 */
30210Sstevel@tonic-gate 		(void) hidparser_get_packet_size(hparser_handle,
30220Sstevel@tonic-gate 		    0, R_ITEM_INPUT, &packet_size);
30230Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
30240Sstevel@tonic-gate 		    "Not using report id prefix. HID packet size = %d",
30250Sstevel@tonic-gate 		    packet_size);
30260Sstevel@tonic-gate 
30270Sstevel@tonic-gate 		hpack->max_packet_size = packet_size;
30280Sstevel@tonic-gate 		hpack->report_id = HID_REPORT_ID_UNDEFINED;
30290Sstevel@tonic-gate 	} else {
30300Sstevel@tonic-gate 		/*
30310Sstevel@tonic-gate 		 * hid device uses multiple reports with report id prefix byte.
30320Sstevel@tonic-gate 		 * Find the longest input report.
30330Sstevel@tonic-gate 		 * See HID 8.4.
30340Sstevel@tonic-gate 		 */
30350Sstevel@tonic-gate 		max_packet_size = 0;
30360Sstevel@tonic-gate 		max_report_id = 0;
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 		for (i = 0; i < (report_id_list.no_of_report_ids); i++) {
30390Sstevel@tonic-gate 			(void) hidparser_get_packet_size(hparser_handle,
30400Sstevel@tonic-gate 			    report_id_list.report_id[i], R_ITEM_INPUT,
30410Sstevel@tonic-gate 			    &packet_size);
30420Sstevel@tonic-gate 			if (packet_size > max_packet_size) {
30430Sstevel@tonic-gate 				max_packet_size = packet_size;
30440Sstevel@tonic-gate 				max_report_id = report_id_list.report_id[i];
30450Sstevel@tonic-gate 			}
30460Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
30470Sstevel@tonic-gate 			    "Report ID %d has a packet size of %d",
30480Sstevel@tonic-gate 			    report_id_list.report_id[i], packet_size);
30490Sstevel@tonic-gate 		}
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 		hpack->max_packet_size = max_packet_size;
30520Sstevel@tonic-gate 		hpack->report_id = max_report_id;
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
30550Sstevel@tonic-gate 		    "Report ID %d has the maximum packet size of %d",
30560Sstevel@tonic-gate 		    max_report_id, max_packet_size);
30570Sstevel@tonic-gate 	}
30580Sstevel@tonic-gate }
3059