xref: /illumos-gate/usr/src/uts/common/io/usb/clients/hidparser/hidparser.c (revision 239936d2b877f13d716270a06e56203231806b36)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d2bde62eSgc161489  * Common Development and Distribution License (the "License").
6d2bde62eSgc161489  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22827f5d6bSStrony Zhang - Solaris China Team  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
2615c07adcSJohn Levon /*
2715c07adcSJohn Levon  * Copyright (c) 2018, Joyent, Inc.
2815c07adcSJohn Levon  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
317c478bd9Sstevel@tonic-gate #include <sys/usb/usba.h>
327c478bd9Sstevel@tonic-gate #include <sys/usb/clients/hid/hid.h>
337c478bd9Sstevel@tonic-gate #include <sys/usb/clients/hidparser/hidparser.h>
347c478bd9Sstevel@tonic-gate #include <sys/usb/clients/hidparser/hid_parser_driver.h>
357c478bd9Sstevel@tonic-gate #include <sys/usb/clients/hidparser/hidparser_impl.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * hidparser: Parser to generate parse tree for Report Descriptors
397c478bd9Sstevel@tonic-gate  * in HID devices.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
424610e4a0Sfrits uint_t hparser_errmask = (uint_t)PRINT_MASK_ALL;
434610e4a0Sfrits uint_t	hparser_errlevel = (uint_t)USB_LOG_L1;
447c478bd9Sstevel@tonic-gate static usb_log_handle_t hparser_log_handle;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Array used to store corresponding strings for the
487c478bd9Sstevel@tonic-gate  * different item types for debugging.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate char		*items[500];	/* Print items */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * modload support
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc	= {
587c478bd9Sstevel@tonic-gate 	&mod_miscops,	/* Type	of module */
59e7cc2e17Sbc224572 	"HID Parser"
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
637c478bd9Sstevel@tonic-gate 	MODREV_1, (void	*)&modlmisc, NULL
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int
_init(void)677c478bd9Sstevel@tonic-gate _init(void)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	int rval = mod_install(&modlinkage);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (rval == 0) {
727c478bd9Sstevel@tonic-gate 		hparser_log_handle = usb_alloc_log_hdl(NULL, "hidparser",
737c478bd9Sstevel@tonic-gate 		    &hparser_errlevel, &hparser_errmask, NULL, 0);
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	return (rval);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate int
_fini()807c478bd9Sstevel@tonic-gate _fini()
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	int rval = mod_remove(&modlinkage);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (rval == 0) {
857c478bd9Sstevel@tonic-gate 		usb_free_log_hdl(hparser_log_handle);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	return (rval);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)927c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * These functions are used internally in the parser.
1007c478bd9Sstevel@tonic-gate  * local declarations
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate static void			hidparser_scan(hidparser_tok_t	*);
1037c478bd9Sstevel@tonic-gate static int			hidparser_Items(hidparser_tok_t *);
1047c478bd9Sstevel@tonic-gate static int			hidparser_LocalItem(hidparser_tok_t *);
1057c478bd9Sstevel@tonic-gate static int			hidparser_GlobalItem(hidparser_tok_t *);
1067c478bd9Sstevel@tonic-gate static int			hidparser_ItemList(entity_item_t **,
1077c478bd9Sstevel@tonic-gate 					hidparser_tok_t *);
1087c478bd9Sstevel@tonic-gate static int			hidparser_ReportDescriptor(entity_item_t **,
1097c478bd9Sstevel@tonic-gate 					hidparser_tok_t *);
1107c478bd9Sstevel@tonic-gate static int			hidparser_ReportDescriptorDash(entity_item_t **,
1117c478bd9Sstevel@tonic-gate 					hidparser_tok_t *);
1127c478bd9Sstevel@tonic-gate static int			hidparser_MainItem(entity_item_t **,
1137c478bd9Sstevel@tonic-gate 					hidparser_tok_t *);
1147c478bd9Sstevel@tonic-gate static void			hidparser_free_attribute_list(
1157c478bd9Sstevel@tonic-gate 					entity_attribute_t *);
1167c478bd9Sstevel@tonic-gate static entity_item_t		*hidparser_allocate_entity(hidparser_tok_t *);
1177c478bd9Sstevel@tonic-gate static void			hidparser_add_attribute(hidparser_tok_t	*);
1187c478bd9Sstevel@tonic-gate static entity_attribute_t	*hidparser_cp_attribute_list(
1197c478bd9Sstevel@tonic-gate 				entity_attribute_t *);
1207c478bd9Sstevel@tonic-gate static entity_attribute_t	*hidparser_find_attribute_end(
1217c478bd9Sstevel@tonic-gate 				entity_attribute_t *);
1227c478bd9Sstevel@tonic-gate static entity_attribute_t	*hidparser_alloc_attrib_list(int);
1237c478bd9Sstevel@tonic-gate static void			hidparser_report_err(int, int,
1247c478bd9Sstevel@tonic-gate 					int, int, char *);
1257c478bd9Sstevel@tonic-gate static int			hidparser_isvalid_item(int);
1267c478bd9Sstevel@tonic-gate static entity_attribute_t	*hidparser_lookup_attribute(entity_item_t *,
1277c478bd9Sstevel@tonic-gate 					int);
1287c478bd9Sstevel@tonic-gate static void			hidparser_global_err_check(entity_item_t *);
1297c478bd9Sstevel@tonic-gate static void			hidparser_local_err_check(entity_item_t *);
1307c478bd9Sstevel@tonic-gate static void			hidparser_mainitem_err_check(entity_item_t *);
1317c478bd9Sstevel@tonic-gate static unsigned int		hidparser_find_unsigned_val(
1327c478bd9Sstevel@tonic-gate 					entity_attribute_t *);
1337c478bd9Sstevel@tonic-gate static int			hidparser_find_signed_val(
1347c478bd9Sstevel@tonic-gate 					entity_attribute_t *);
1357c478bd9Sstevel@tonic-gate static void			hidparser_check_correspondence(
1367c478bd9Sstevel@tonic-gate 					entity_item_t *, int, int, int,
1377c478bd9Sstevel@tonic-gate 					int, char *, char *);
1387c478bd9Sstevel@tonic-gate static void			hidparser_check_minmax_val(entity_item_t *,
1397c478bd9Sstevel@tonic-gate 					int, int, int, int);
1407c478bd9Sstevel@tonic-gate static void			hidparser_check_minmax_val_signed(
1417c478bd9Sstevel@tonic-gate 					entity_item_t *,
1427c478bd9Sstevel@tonic-gate 					int, int, int, int);
1437c478bd9Sstevel@tonic-gate static void			hidparser_error_delim(entity_item_t *, int);
1447c478bd9Sstevel@tonic-gate static int			hidparser_get_usage_attribute_report_des(
1457c478bd9Sstevel@tonic-gate 					entity_item_t *,
1467c478bd9Sstevel@tonic-gate 					uint32_t, uint32_t, uint32_t,
1477c478bd9Sstevel@tonic-gate 					uint32_t, uint32_t, int32_t *);
1487c478bd9Sstevel@tonic-gate static int			hidparser_get_packet_size_report_des(
1497c478bd9Sstevel@tonic-gate 					entity_item_t *, uint32_t, uint32_t,
1507c478bd9Sstevel@tonic-gate 					uint32_t *);
1517c478bd9Sstevel@tonic-gate static int			hidparser_get_main_item_data_descr_main(
1527c478bd9Sstevel@tonic-gate 					entity_item_t *, uint32_t,
1537c478bd9Sstevel@tonic-gate 					uint32_t, uint32_t, uint32_t,
1547c478bd9Sstevel@tonic-gate 					uint32_t	*);
1557c478bd9Sstevel@tonic-gate static void			hidparser_print_entity(
1567c478bd9Sstevel@tonic-gate 					entity_item_t *entity,
1577c478bd9Sstevel@tonic-gate 					int indent_level);
1587c478bd9Sstevel@tonic-gate static void			hidparser_print_this_attribute(
1597c478bd9Sstevel@tonic-gate 					entity_attribute_t *attribute,
1607c478bd9Sstevel@tonic-gate 					char *ident_space);
1617c478bd9Sstevel@tonic-gate static int			hidparser_main(unsigned char *, size_t,
1627c478bd9Sstevel@tonic-gate 					entity_item_t **);
1637c478bd9Sstevel@tonic-gate static void			hidparser_initialize_items();
1647c478bd9Sstevel@tonic-gate static void			hidparser_free_report_descr_handle(
1657c478bd9Sstevel@tonic-gate 					entity_item_t *);
1667c478bd9Sstevel@tonic-gate static int			hidparser_print_report_descr_handle(
1677c478bd9Sstevel@tonic-gate 					entity_item_t	*handle,
1687c478bd9Sstevel@tonic-gate 					int		indent_level);
1697c478bd9Sstevel@tonic-gate static int			hidparser_get_usage_list_in_order_internal(
1707c478bd9Sstevel@tonic-gate 					entity_item_t *parse_handle,
1717c478bd9Sstevel@tonic-gate 					uint_t collection_usage,
1727c478bd9Sstevel@tonic-gate 					uint_t report_id,
1737c478bd9Sstevel@tonic-gate 					uint_t main_item_type,
1747c478bd9Sstevel@tonic-gate 					hidparser_rpt_t *rpt);
1757c478bd9Sstevel@tonic-gate static void			hidparser_fill_usage_info(
1767c478bd9Sstevel@tonic-gate 					hidparser_usage_info_t *ui,
1777c478bd9Sstevel@tonic-gate 					entity_attribute_t *attribute);
1787c478bd9Sstevel@tonic-gate static int			hidparser_get_report_id_list_internal(
1797c478bd9Sstevel@tonic-gate 					entity_item_t *parser_handle,
1807c478bd9Sstevel@tonic-gate 					uint_t main_item_type,
1817c478bd9Sstevel@tonic-gate 					hidparser_report_id_list_t *id_lst);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * The hidparser_lookup_first(N) of a non-terminal N is stored as an array of
1857c478bd9Sstevel@tonic-gate  * integer tokens, terminated by 0. Right now there is only one element.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate static hidparser_terminal_t	first_Items[] = {
1887c478bd9Sstevel@tonic-gate 	R_ITEM_USAGE_PAGE, R_ITEM_LOGICAL_MINIMUM, R_ITEM_LOGICAL_MAXIMUM, \
1897c478bd9Sstevel@tonic-gate 	R_ITEM_PHYSICAL_MINIMUM, R_ITEM_PHYSICAL_MAXIMUM, R_ITEM_UNIT, \
1907c478bd9Sstevel@tonic-gate 	R_ITEM_EXPONENT, R_ITEM_REPORT_SIZE, R_ITEM_REPORT_COUNT, \
1917c478bd9Sstevel@tonic-gate 	R_ITEM_REPORT_ID, \
1927c478bd9Sstevel@tonic-gate 	R_ITEM_USAGE, R_ITEM_USAGE_MIN, R_ITEM_USAGE_MAX, \
1937c478bd9Sstevel@tonic-gate 	R_ITEM_DESIGNATOR_INDEX, \
1947c478bd9Sstevel@tonic-gate 	R_ITEM_DESIGNATOR_MIN, R_ITEM_STRING_INDEX, R_ITEM_STRING_MIN, \
1957c478bd9Sstevel@tonic-gate 	R_ITEM_STRING_MAX, \
1967c478bd9Sstevel@tonic-gate 	R_ITEM_SET_DELIMITER, \
1977c478bd9Sstevel@tonic-gate 	0
1987c478bd9Sstevel@tonic-gate };
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Each non-terminal is represented by a function. In a top-down parser,
2037c478bd9Sstevel@tonic-gate  * whenever a non-terminal is encountered on the state diagram, the
2047c478bd9Sstevel@tonic-gate  * corresponding function is called. Because of the grammar, there is NO
2057c478bd9Sstevel@tonic-gate  * backtracking. If there is an error in the middle, the parser returns
2067c478bd9Sstevel@tonic-gate  * HIDPARSER_FAILURE
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate static hidparser_terminal_t *hid_first_list[] = {
2097c478bd9Sstevel@tonic-gate 	first_Items
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * hidparser_parse_report_descriptor:
2157c478bd9Sstevel@tonic-gate  *	Calls the main parser routine
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate int
hidparser_parse_report_descriptor(unsigned char * descriptor,size_t size,usb_hid_descr_t * hid_descriptor,hidparser_handle_t * parse_handle)218*239936d2SToomas Soome hidparser_parse_report_descriptor(unsigned char *descriptor, size_t size,
219*239936d2SToomas Soome     usb_hid_descr_t *hid_descriptor, hidparser_handle_t *parse_handle)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	int	error = 0;
2227c478bd9Sstevel@tonic-gate 	entity_item_t	*root;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	hidparser_initialize_items();
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	error = hidparser_main(descriptor, size, &root);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (error != HIDPARSER_SUCCESS) {
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
2317c478bd9Sstevel@tonic-gate 	} else {
2327c478bd9Sstevel@tonic-gate 		*parse_handle = kmem_zalloc(
2337c478bd9Sstevel@tonic-gate 		    sizeof (hidparser_handle), KM_SLEEP);
2347c478bd9Sstevel@tonic-gate 		(*parse_handle)->hidparser_handle_hid_descr = hid_descriptor;
2357c478bd9Sstevel@tonic-gate 		(*parse_handle)->hidparser_handle_parse_tree = root;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		return (HIDPARSER_SUCCESS);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * hidparser_free_report_descriptor_handle:
2447c478bd9Sstevel@tonic-gate  *	Frees the parse_handle which consists of a pointer to the parse
2457c478bd9Sstevel@tonic-gate  *	tree and a pointer to the Hid descriptor structure
2467c478bd9Sstevel@tonic-gate  */
2477c478bd9Sstevel@tonic-gate int
hidparser_free_report_descriptor_handle(hidparser_handle_t parse_handle)2487c478bd9Sstevel@tonic-gate hidparser_free_report_descriptor_handle(hidparser_handle_t parse_handle)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	if (parse_handle != NULL) {
2517c478bd9Sstevel@tonic-gate 		hidparser_free_report_descr_handle(
2527c478bd9Sstevel@tonic-gate 		    parse_handle->hidparser_handle_parse_tree);
2537c478bd9Sstevel@tonic-gate 		if (parse_handle != NULL) {
2547c478bd9Sstevel@tonic-gate 			kmem_free(parse_handle, sizeof (hidparser_handle));
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * hidparser_get_country_code:
2647c478bd9Sstevel@tonic-gate  *	Return the bCountryCode from the Hid Descriptor
2657c478bd9Sstevel@tonic-gate  *	to the hid module.
2667c478bd9Sstevel@tonic-gate  */
2677c478bd9Sstevel@tonic-gate int
hidparser_get_country_code(hidparser_handle_t parser_handle,uint16_t * country_code)2687c478bd9Sstevel@tonic-gate hidparser_get_country_code(hidparser_handle_t parser_handle,
2697c478bd9Sstevel@tonic-gate     uint16_t *country_code)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	if ((parser_handle == NULL) ||
2727c478bd9Sstevel@tonic-gate 	    (parser_handle->hidparser_handle_hid_descr == NULL)) {
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	*country_code =
2787c478bd9Sstevel@tonic-gate 	    parser_handle->hidparser_handle_hid_descr->bCountryCode;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate  * hidparser_get_packet_size:
2867c478bd9Sstevel@tonic-gate  *	Get the packet size(sum of REPORT_SIZE * REPORT_COUNT)
2877c478bd9Sstevel@tonic-gate  *	corresponding to a report id and an item type
2887c478bd9Sstevel@tonic-gate  */
2897c478bd9Sstevel@tonic-gate int
hidparser_get_packet_size(hidparser_handle_t parser_handle,uint_t report_id,uint_t main_item_type,uint_t * size)2907c478bd9Sstevel@tonic-gate hidparser_get_packet_size(hidparser_handle_t parser_handle,
291*239936d2SToomas Soome     uint_t report_id, uint_t main_item_type, uint_t *size)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate 	if ((parser_handle == NULL) || (parser_handle->
2947c478bd9Sstevel@tonic-gate 	    hidparser_handle_parse_tree == NULL)) {
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	*size = 0;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	return (hidparser_get_packet_size_report_des(
3027c478bd9Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
3037c478bd9Sstevel@tonic-gate 	    report_id, main_item_type, size));
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * hidparser_get_packet_size_report_des:
3097c478bd9Sstevel@tonic-gate  *	Get the packet size(sum of REPORT_SIZE * REPORT_COUNT)
3107c478bd9Sstevel@tonic-gate  *	corresponding to a report id and an item type
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@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)3137c478bd9Sstevel@tonic-gate hidparser_get_packet_size_report_des(entity_item_t *parser_handle,
314*239936d2SToomas Soome     uint32_t report_id, uint32_t main_item_type, uint32_t *size)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	entity_item_t	*current = parser_handle;
3177c478bd9Sstevel@tonic-gate 	entity_attribute_t *attribute;
3187c478bd9Sstevel@tonic-gate 	uint32_t temp;
3197c478bd9Sstevel@tonic-gate 	uchar_t	foundsize, foundcount, foundreportid, right_report_id;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	foundsize = 0;
3227c478bd9Sstevel@tonic-gate 	foundcount = 0;
3237c478bd9Sstevel@tonic-gate 	right_report_id = 0;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	while (current) {
3267c478bd9Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
3277c478bd9Sstevel@tonic-gate 			(void) hidparser_get_packet_size_report_des(
3287c478bd9Sstevel@tonic-gate 			    current->info.child, report_id, main_item_type,
3297c478bd9Sstevel@tonic-gate 			    size);
3307c478bd9Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
3317c478bd9Sstevel@tonic-gate 			temp = 1;
3327c478bd9Sstevel@tonic-gate 			foundsize = 0;
3337c478bd9Sstevel@tonic-gate 			foundcount = 0;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 			foundreportid = 0;
3367c478bd9Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
3377c478bd9Sstevel@tonic-gate 			while (attribute != NULL) {
3387c478bd9Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
3397c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
3407c478bd9Sstevel@tonic-gate 					foundreportid = 1;
3417c478bd9Sstevel@tonic-gate 					if ((attribute->
3427c478bd9Sstevel@tonic-gate 					    entity_attribute_value[0]) ==
3437c478bd9Sstevel@tonic-gate 					    report_id) {
3447c478bd9Sstevel@tonic-gate 						right_report_id = 1;
3457c478bd9Sstevel@tonic-gate 					}
3467c478bd9Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
3477c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_SIZE) {
3487c478bd9Sstevel@tonic-gate 					foundsize = 1;
34941d01d31Sqz150045 					temp *= hidparser_find_unsigned_val(
35041d01d31Sqz150045 					    attribute);
3517c478bd9Sstevel@tonic-gate 					if (foundcount == 1) {
3527c478bd9Sstevel@tonic-gate 						if (report_id &&
3537c478bd9Sstevel@tonic-gate 						    right_report_id) {
3547c478bd9Sstevel@tonic-gate 							break;
3557c478bd9Sstevel@tonic-gate 						}
3567c478bd9Sstevel@tonic-gate 					}
3577c478bd9Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
3587c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_COUNT) {
3597c478bd9Sstevel@tonic-gate 					foundcount = 1;
36041d01d31Sqz150045 					temp *= hidparser_find_unsigned_val(
36141d01d31Sqz150045 					    attribute);
3627c478bd9Sstevel@tonic-gate 					if (foundsize == 1) {
3637c478bd9Sstevel@tonic-gate 						if (report_id &&
3647c478bd9Sstevel@tonic-gate 						    right_report_id) {
3657c478bd9Sstevel@tonic-gate 							break;
3667c478bd9Sstevel@tonic-gate 						}
3677c478bd9Sstevel@tonic-gate 					}
3687c478bd9Sstevel@tonic-gate 				}
3697c478bd9Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
3707c478bd9Sstevel@tonic-gate 			} /* end while */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 			if (foundreportid) {
3737c478bd9Sstevel@tonic-gate 				if (right_report_id) {
3747c478bd9Sstevel@tonic-gate 					*size = *size + temp;
3757c478bd9Sstevel@tonic-gate 				}
3767c478bd9Sstevel@tonic-gate 			} else if (report_id == HID_REPORT_ID_UNDEFINED) {
3777c478bd9Sstevel@tonic-gate 				/* Just sanity checking */
3787c478bd9Sstevel@tonic-gate 				*size = *size + temp;
3797c478bd9Sstevel@tonic-gate 			}
3807c478bd9Sstevel@tonic-gate 			right_report_id = 0;
3817c478bd9Sstevel@tonic-gate 		} /* end else if */
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
3847c478bd9Sstevel@tonic-gate 	} /* end while current */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * hidparser_get_usage_attribute:
3937c478bd9Sstevel@tonic-gate  *	Get the attribute value corresponding to a particular
3947c478bd9Sstevel@tonic-gate  *	report id, main item and usage
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@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)3977c478bd9Sstevel@tonic-gate hidparser_get_usage_attribute(hidparser_handle_t parser_handle,
398*239936d2SToomas Soome     uint_t report_id, uint_t main_item_type, uint_t usage_page,
399*239936d2SToomas Soome     uint_t usage_id, uint_t usage_attribute, int *usage_attribute_value)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	return (hidparser_get_usage_attribute_report_des(
4037c478bd9Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
4047c478bd9Sstevel@tonic-gate 	    report_id, main_item_type, usage_page,
4057c478bd9Sstevel@tonic-gate 	    usage_id, usage_attribute, usage_attribute_value));
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * hidparser_get_usage_attribute_report_des:
4117c478bd9Sstevel@tonic-gate  *	Called by the wrapper function hidparser_get_usage_attribute()
4127c478bd9Sstevel@tonic-gate  */
4137c478bd9Sstevel@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)4147c478bd9Sstevel@tonic-gate hidparser_get_usage_attribute_report_des(entity_item_t *parser_handle,
415*239936d2SToomas Soome     uint_t report_id, uint_t main_item_type, uint_t usage_page,
416*239936d2SToomas Soome     uint_t usage_id, uint_t usage_attribute, int *usage_attribute_value)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
4197c478bd9Sstevel@tonic-gate 	entity_attribute_t *attribute;
4207c478bd9Sstevel@tonic-gate 	uchar_t found_page, found_ret_value, found_usage_id;
4217c478bd9Sstevel@tonic-gate 	uchar_t foundreportid, right_report_id;
4227c478bd9Sstevel@tonic-gate 	uint32_t usage;
4237c478bd9Sstevel@tonic-gate 	short attvalue;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	found_page = 0;
4267c478bd9Sstevel@tonic-gate 	found_ret_value = 0;
4277c478bd9Sstevel@tonic-gate 	found_usage_id = 0;
4287c478bd9Sstevel@tonic-gate 	foundreportid = 0;
4297c478bd9Sstevel@tonic-gate 	right_report_id = 0;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	while (current) {
4327c478bd9Sstevel@tonic-gate 		if (usage_id == HID_USAGE_UNDEFINED) {
4337c478bd9Sstevel@tonic-gate 			found_usage_id = 1;
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 			if (hidparser_get_usage_attribute_report_des(
4387c478bd9Sstevel@tonic-gate 			    current->info.child, report_id, main_item_type,
4397c478bd9Sstevel@tonic-gate 			    usage_page, usage_id, usage_attribute,
4407c478bd9Sstevel@tonic-gate 			    usage_attribute_value) ==
4417c478bd9Sstevel@tonic-gate 			    HIDPARSER_SUCCESS) {
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
4477c478bd9Sstevel@tonic-gate 			/* Match Item Type */
4487c478bd9Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 			while (attribute != NULL) {
4517c478bd9Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
4527c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE) {
4537c478bd9Sstevel@tonic-gate 					usage = hidparser_find_unsigned_val(
4547c478bd9Sstevel@tonic-gate 					    attribute);
4557c478bd9Sstevel@tonic-gate 					if (usage_id == HID_USAGE_ID(usage)) {
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 						found_usage_id = 1;
4587c478bd9Sstevel@tonic-gate 					} else {
4597c478bd9Sstevel@tonic-gate 						/*
4607c478bd9Sstevel@tonic-gate 						 * If we are trying to find out
4617c478bd9Sstevel@tonic-gate 						 * say, report size of usage =
4627c478bd9Sstevel@tonic-gate 						 * 0, a m.i with a valid usage
4637c478bd9Sstevel@tonic-gate 						 * will not contain that
4647c478bd9Sstevel@tonic-gate 						 */
4657c478bd9Sstevel@tonic-gate 						if (usage_id ==
4667c478bd9Sstevel@tonic-gate 						    HID_USAGE_UNDEFINED) {
4677c478bd9Sstevel@tonic-gate 							found_usage_id = 0;
4687c478bd9Sstevel@tonic-gate 						}
4697c478bd9Sstevel@tonic-gate 					}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 					if (found_usage_id && attribute->
4727c478bd9Sstevel@tonic-gate 					    entity_attribute_length == 3) {
4737c478bd9Sstevel@tonic-gate 						/*
4747c478bd9Sstevel@tonic-gate 						 * This is an extended usage ie.
4757c478bd9Sstevel@tonic-gate 						 * usage page in upper 16 bits
4767c478bd9Sstevel@tonic-gate 						 * or-ed with usage in the lower
4777c478bd9Sstevel@tonic-gate 						 * 16 bits.
4787c478bd9Sstevel@tonic-gate 						 */
4797c478bd9Sstevel@tonic-gate 						if (HID_USAGE_PAGE(usage) &&
4807c478bd9Sstevel@tonic-gate 						    HID_USAGE_PAGE(usage) ==
4817c478bd9Sstevel@tonic-gate 						    usage_page) {
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 							found_page = 1;
4847c478bd9Sstevel@tonic-gate 						} else {
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 							found_usage_id = 0;
4877c478bd9Sstevel@tonic-gate 						}
4887c478bd9Sstevel@tonic-gate 					}
4897c478bd9Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
4907c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE_PAGE) {
4917c478bd9Sstevel@tonic-gate 					if (attribute->
4927c478bd9Sstevel@tonic-gate 					    entity_attribute_value[0] ==
4937c478bd9Sstevel@tonic-gate 					    usage_page) {
4947c478bd9Sstevel@tonic-gate 						/* Match Usage Page */
4957c478bd9Sstevel@tonic-gate 						found_page = 1;
4967c478bd9Sstevel@tonic-gate 					}
4977c478bd9Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
4987c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
4997c478bd9Sstevel@tonic-gate 					foundreportid = 1;
5007c478bd9Sstevel@tonic-gate 					if (attribute->
5017c478bd9Sstevel@tonic-gate 					    entity_attribute_value[0] ==
5027c478bd9Sstevel@tonic-gate 					    report_id) {
5037c478bd9Sstevel@tonic-gate 						right_report_id = 1;
5047c478bd9Sstevel@tonic-gate 					}
5057c478bd9Sstevel@tonic-gate 				}
5067c478bd9Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
5077c478bd9Sstevel@tonic-gate 				    usage_attribute) {
5087c478bd9Sstevel@tonic-gate 					/* Match attribute */
5097c478bd9Sstevel@tonic-gate 					found_ret_value = 1;
5107c478bd9Sstevel@tonic-gate 					*usage_attribute_value =
511*239936d2SToomas Soome 					    *attribute->entity_attribute_value;
5127c478bd9Sstevel@tonic-gate 					if (attribute->
5137c478bd9Sstevel@tonic-gate 					    entity_attribute_length == 2) {
5147c478bd9Sstevel@tonic-gate 						attvalue =
5157c478bd9Sstevel@tonic-gate 						    (attribute->
5167c478bd9Sstevel@tonic-gate 						    entity_attribute_value[0] &
5177c478bd9Sstevel@tonic-gate 						    0xff) |
5187c478bd9Sstevel@tonic-gate 						    (attribute->
5197c478bd9Sstevel@tonic-gate 						    entity_attribute_value[1] <<
5207c478bd9Sstevel@tonic-gate 						    8);
5217c478bd9Sstevel@tonic-gate 						*usage_attribute_value =
5227c478bd9Sstevel@tonic-gate 						    attvalue;
5237c478bd9Sstevel@tonic-gate 					}
5247c478bd9Sstevel@tonic-gate 				}
5257c478bd9Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 			if (found_usage_id && found_page && found_ret_value) {
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 				if (foundreportid) {
5317c478bd9Sstevel@tonic-gate 					if (right_report_id) {
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 						return (HIDPARSER_SUCCESS);
5347c478bd9Sstevel@tonic-gate 					} else if (report_id ==
5357c478bd9Sstevel@tonic-gate 					    HID_REPORT_ID_UNDEFINED) {
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 						return (HIDPARSER_SUCCESS);
5387c478bd9Sstevel@tonic-gate 					}
5397c478bd9Sstevel@tonic-gate 				} else {
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 					return (HIDPARSER_SUCCESS);
5427c478bd9Sstevel@tonic-gate 				}
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		/*
5477c478bd9Sstevel@tonic-gate 		 * search the next main item, right sibling of this one
5487c478bd9Sstevel@tonic-gate 		 */
5497c478bd9Sstevel@tonic-gate 		if (current->entity_item_right_sibling != NULL) {
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 			current = current->entity_item_right_sibling;
5527c478bd9Sstevel@tonic-gate 			found_usage_id = found_page = found_ret_value = 0;
5537c478bd9Sstevel@tonic-gate 			/* Don't change foundreportid */
5547c478bd9Sstevel@tonic-gate 			right_report_id = 0;
5557c478bd9Sstevel@tonic-gate 		} else {
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 			break;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	/* Don't give junk result */
5617c478bd9Sstevel@tonic-gate 	*usage_attribute_value = 0;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return (HIDPARSER_NOT_FOUND);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * hidparser_get_main_item_data_descr:
5697c478bd9Sstevel@tonic-gate  *	Get the data value corresponding to a particular
5707c478bd9Sstevel@tonic-gate  *	Main Item (Input, Output, Feature)
5717c478bd9Sstevel@tonic-gate  */
5727c478bd9Sstevel@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)5737c478bd9Sstevel@tonic-gate hidparser_get_main_item_data_descr(hidparser_handle_t parser_handle,
574*239936d2SToomas Soome     uint_t report_id, uint_t main_item_type, uint_t usage_page,
575*239936d2SToomas Soome     uint_t usage_id, uint_t *main_item_descr_value)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	return hidparser_get_main_item_data_descr_main(
5797c478bd9Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
5807c478bd9Sstevel@tonic-gate 	    report_id, main_item_type, usage_page, usage_id,
5817c478bd9Sstevel@tonic-gate 	    main_item_descr_value);
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * hidparser_get_main_item_data_descr_main:
5877c478bd9Sstevel@tonic-gate  *	Called by the wrapper function hidparser_get_main_item_data_descr()
5887c478bd9Sstevel@tonic-gate  */
5897c478bd9Sstevel@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)5907c478bd9Sstevel@tonic-gate hidparser_get_main_item_data_descr_main(entity_item_t *parser_handle,
591*239936d2SToomas Soome     uint_t report_id, uint_t main_item_type, uint_t usage_page,
592*239936d2SToomas Soome     uint_t usage_id, uint_t *main_item_descr_value)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
5957c478bd9Sstevel@tonic-gate 	entity_attribute_t *attribute;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	uchar_t found_page, found_usage_id;
5987c478bd9Sstevel@tonic-gate 	uchar_t foundreportid, right_report_id;
5997c478bd9Sstevel@tonic-gate 	uint32_t usage;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	found_page = 0;
6027c478bd9Sstevel@tonic-gate 	found_usage_id = 0;
6037c478bd9Sstevel@tonic-gate 	foundreportid = 0;
6047c478bd9Sstevel@tonic-gate 	right_report_id = 0;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	while (current) {
6077c478bd9Sstevel@tonic-gate 		if (usage_id == HID_USAGE_UNDEFINED) {
6087c478bd9Sstevel@tonic-gate 			found_usage_id = 1;
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 			if (hidparser_get_main_item_data_descr_main(
6137c478bd9Sstevel@tonic-gate 			    current->info.child, report_id, main_item_type,
6147c478bd9Sstevel@tonic-gate 			    usage_page, usage_id, main_item_descr_value) ==
6157c478bd9Sstevel@tonic-gate 			    HIDPARSER_SUCCESS) {
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
6187c478bd9Sstevel@tonic-gate 			}
6197c478bd9Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
6207c478bd9Sstevel@tonic-gate 			/* Match Item Type */
6217c478bd9Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			if (report_id == HID_REPORT_ID_UNDEFINED) {
6247c478bd9Sstevel@tonic-gate 				foundreportid = right_report_id = 1;
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 			while (attribute != NULL) {
6287c478bd9Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
6297c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE) {
6307c478bd9Sstevel@tonic-gate 					usage = hidparser_find_unsigned_val(
6317c478bd9Sstevel@tonic-gate 					    attribute);
6327c478bd9Sstevel@tonic-gate 					if (usage_id == HID_USAGE_ID(usage)) {
6337c478bd9Sstevel@tonic-gate 						found_usage_id = 1;
6347c478bd9Sstevel@tonic-gate 						if (attribute->
6357c478bd9Sstevel@tonic-gate 						    entity_attribute_length ==
6367c478bd9Sstevel@tonic-gate 						    3) {
6377c478bd9Sstevel@tonic-gate 							if (HID_USAGE_PAGE(
6387c478bd9Sstevel@tonic-gate 							    usage) &&
6397c478bd9Sstevel@tonic-gate 							    HID_USAGE_PAGE(
6407c478bd9Sstevel@tonic-gate 							    usage) ==
6417c478bd9Sstevel@tonic-gate 							    usage_page) {
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 								found_page = 1;
6447c478bd9Sstevel@tonic-gate 							} else {
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 							found_usage_id = 0;
6477c478bd9Sstevel@tonic-gate 							}
6487c478bd9Sstevel@tonic-gate 						}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 						if (found_usage_id &&
6517c478bd9Sstevel@tonic-gate 						    found_page &&
6527c478bd9Sstevel@tonic-gate 						    foundreportid &&
6537c478bd9Sstevel@tonic-gate 						    right_report_id) {
6547c478bd9Sstevel@tonic-gate 						*main_item_descr_value =
6557c478bd9Sstevel@tonic-gate 						    current->
6567c478bd9Sstevel@tonic-gate 						    entity_item_params[0];
6577c478bd9Sstevel@tonic-gate 						break;
6587c478bd9Sstevel@tonic-gate 						}
6597c478bd9Sstevel@tonic-gate 					}
6607c478bd9Sstevel@tonic-gate 				} else if ((attribute->entity_attribute_tag ==
6617c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE_PAGE) &&
6627c478bd9Sstevel@tonic-gate 				    (attribute->entity_attribute_value[0] ==
6637c478bd9Sstevel@tonic-gate 				    usage_page)) {
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 					/* Match Usage Page */
6667c478bd9Sstevel@tonic-gate 					found_page = 1;
6677c478bd9Sstevel@tonic-gate 					if (found_usage_id && foundreportid &&
6687c478bd9Sstevel@tonic-gate 					    right_report_id) {
6697c478bd9Sstevel@tonic-gate 						*main_item_descr_value =
6707c478bd9Sstevel@tonic-gate 						    current->
6717c478bd9Sstevel@tonic-gate 						    entity_item_params[0];
6727c478bd9Sstevel@tonic-gate 						break;
6737c478bd9Sstevel@tonic-gate 					}
6747c478bd9Sstevel@tonic-gate 				} else if (attribute->entity_attribute_tag ==
6757c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
6767c478bd9Sstevel@tonic-gate 					foundreportid = 1;
6777c478bd9Sstevel@tonic-gate 					if (attribute->
6787c478bd9Sstevel@tonic-gate 					    entity_attribute_value[0] ==
6797c478bd9Sstevel@tonic-gate 					    report_id) {
6807c478bd9Sstevel@tonic-gate 						right_report_id = 1;
6817c478bd9Sstevel@tonic-gate 					} else {
6827c478bd9Sstevel@tonic-gate 						break;
6837c478bd9Sstevel@tonic-gate 					}
6847c478bd9Sstevel@tonic-gate 				}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
6877c478bd9Sstevel@tonic-gate 			}
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			if (foundreportid) {
6907c478bd9Sstevel@tonic-gate 				if (right_report_id) {
6917c478bd9Sstevel@tonic-gate 					if (found_usage_id && found_page) {
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 						return (HIDPARSER_SUCCESS);
6947c478bd9Sstevel@tonic-gate 					}
6957c478bd9Sstevel@tonic-gate 				}
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 		}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * search the next main item, right sibling of this one
7017c478bd9Sstevel@tonic-gate 		 */
7027c478bd9Sstevel@tonic-gate 		if (current->entity_item_right_sibling != NULL) {
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 			current = current->entity_item_right_sibling;
7057c478bd9Sstevel@tonic-gate 			found_page = found_usage_id = right_report_id = 0;
7067c478bd9Sstevel@tonic-gate 		} else {
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
712*239936d2SToomas Soome 	*main_item_descr_value = 0;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	return (HIDPARSER_NOT_FOUND);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
717827f5d6bSStrony Zhang - Solaris China Team /*
718827f5d6bSStrony Zhang - Solaris China Team  * hidparser_lookup_usage_collection:
719827f5d6bSStrony Zhang - Solaris China Team  *	Look up the collection specified by the usage page and usage id
720827f5d6bSStrony Zhang - Solaris China Team  */
721827f5d6bSStrony Zhang - Solaris China Team int
hidparser_lookup_usage_collection(hidparser_handle_t parse_handle,uint_t lusage_page,uint_t lusage_id)722827f5d6bSStrony Zhang - Solaris China Team hidparser_lookup_usage_collection(hidparser_handle_t parse_handle,
723*239936d2SToomas Soome     uint_t lusage_page, uint_t lusage_id)
724827f5d6bSStrony Zhang - Solaris China Team {
725827f5d6bSStrony Zhang - Solaris China Team 	entity_item_t *current;
726827f5d6bSStrony Zhang - Solaris China Team 	entity_attribute_t *attribute;
727827f5d6bSStrony Zhang - Solaris China Team 	int found_usage_id = 0;
728827f5d6bSStrony Zhang - Solaris China Team 	int found_page = 0;
729827f5d6bSStrony Zhang - Solaris China Team 	uint32_t usage;
730827f5d6bSStrony Zhang - Solaris China Team 	uint_t usage_page;
731827f5d6bSStrony Zhang - Solaris China Team 	uint_t usage_id;
732827f5d6bSStrony Zhang - Solaris China Team 
733827f5d6bSStrony Zhang - Solaris China Team 	if ((parse_handle == NULL) ||
734827f5d6bSStrony Zhang - Solaris China Team 	    (parse_handle->hidparser_handle_parse_tree == NULL))
735827f5d6bSStrony Zhang - Solaris China Team 		return (HIDPARSER_FAILURE);
736827f5d6bSStrony Zhang - Solaris China Team 
737827f5d6bSStrony Zhang - Solaris China Team 	current = parse_handle->hidparser_handle_parse_tree;
738827f5d6bSStrony Zhang - Solaris China Team 	while (current != NULL) {
739827f5d6bSStrony Zhang - Solaris China Team 
740827f5d6bSStrony Zhang - Solaris China Team 		if (current->entity_item_type != R_ITEM_COLLECTION) {
741827f5d6bSStrony Zhang - Solaris China Team 			current = current->entity_item_right_sibling;
742827f5d6bSStrony Zhang - Solaris China Team 			continue;
743827f5d6bSStrony Zhang - Solaris China Team 		}
744827f5d6bSStrony Zhang - Solaris China Team 
745827f5d6bSStrony Zhang - Solaris China Team 		attribute = current->entity_item_attributes;
746827f5d6bSStrony Zhang - Solaris China Team 		found_usage_id = 0;
747827f5d6bSStrony Zhang - Solaris China Team 		found_page = 0;
748827f5d6bSStrony Zhang - Solaris China Team 
749827f5d6bSStrony Zhang - Solaris China Team 		while (attribute != NULL) {
750827f5d6bSStrony Zhang - Solaris China Team 			if (attribute->entity_attribute_tag == R_ITEM_USAGE) {
751827f5d6bSStrony Zhang - Solaris China Team 				found_usage_id = 1;
752827f5d6bSStrony Zhang - Solaris China Team 				usage = hidparser_find_unsigned_val(attribute);
753827f5d6bSStrony Zhang - Solaris China Team 				usage_id = HID_USAGE_ID(usage);
754827f5d6bSStrony Zhang - Solaris China Team 				if (attribute->entity_attribute_length == 3) {
755827f5d6bSStrony Zhang - Solaris China Team 					if (HID_USAGE_PAGE(usage)) {
756827f5d6bSStrony Zhang - Solaris China Team 						found_page = 1;
757827f5d6bSStrony Zhang - Solaris China Team 						usage_page =
758827f5d6bSStrony Zhang - Solaris China Team 						    HID_USAGE_PAGE(usage);
759827f5d6bSStrony Zhang - Solaris China Team 					}
760827f5d6bSStrony Zhang - Solaris China Team 				}
761827f5d6bSStrony Zhang - Solaris China Team 				if (found_page) {
762827f5d6bSStrony Zhang - Solaris China Team 					goto check_usage;
763827f5d6bSStrony Zhang - Solaris China Team 				}
764827f5d6bSStrony Zhang - Solaris China Team 			} else if (attribute->entity_attribute_tag ==
765827f5d6bSStrony Zhang - Solaris China Team 			    R_ITEM_USAGE_PAGE) {
766827f5d6bSStrony Zhang - Solaris China Team 				found_page = 1;
767827f5d6bSStrony Zhang - Solaris China Team 				usage_page =
768827f5d6bSStrony Zhang - Solaris China Team 				    attribute->entity_attribute_value[0];
769827f5d6bSStrony Zhang - Solaris China Team 				if (found_usage_id) {
770827f5d6bSStrony Zhang - Solaris China Team 					goto check_usage;
771827f5d6bSStrony Zhang - Solaris China Team 				}
772827f5d6bSStrony Zhang - Solaris China Team 			}
773827f5d6bSStrony Zhang - Solaris China Team 			attribute = attribute->entity_attribute_next;
774827f5d6bSStrony Zhang - Solaris China Team 		}
775827f5d6bSStrony Zhang - Solaris China Team check_usage:
776827f5d6bSStrony Zhang - Solaris China Team 		if ((usage_page == lusage_page) && (usage_id == lusage_id))
777827f5d6bSStrony Zhang - Solaris China Team 			return (HIDPARSER_SUCCESS);
778827f5d6bSStrony Zhang - Solaris China Team 		else
779827f5d6bSStrony Zhang - Solaris China Team 			current = current->entity_item_right_sibling;
780827f5d6bSStrony Zhang - Solaris China Team 	}
781827f5d6bSStrony Zhang - Solaris China Team 
782827f5d6bSStrony Zhang - Solaris China Team 	return (HIDPARSER_FAILURE);
783827f5d6bSStrony Zhang - Solaris China Team }
784827f5d6bSStrony Zhang - Solaris China Team 
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * hidparser_get_top_level_collection_usage:
7887c478bd9Sstevel@tonic-gate  *	Get the usage page and usage for the top level collection item
7897c478bd9Sstevel@tonic-gate  */
7907c478bd9Sstevel@tonic-gate int
hidparser_get_top_level_collection_usage(hidparser_handle_t parse_handle,uint_t * usage_page,uint_t * usage_id)7917c478bd9Sstevel@tonic-gate hidparser_get_top_level_collection_usage(hidparser_handle_t parse_handle,
792*239936d2SToomas Soome     uint_t *usage_page, uint_t *usage_id)
7937c478bd9Sstevel@tonic-gate {
7947c478bd9Sstevel@tonic-gate 	entity_item_t *current;
7957c478bd9Sstevel@tonic-gate 	entity_attribute_t *attribute;
7967c478bd9Sstevel@tonic-gate 	int found_usage_id = 0;
7977c478bd9Sstevel@tonic-gate 	int found_page = 0;
7987c478bd9Sstevel@tonic-gate 	uint32_t usage;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if ((parse_handle == NULL) ||
8017c478bd9Sstevel@tonic-gate 	    (parse_handle->hidparser_handle_parse_tree == NULL))
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	current = parse_handle->hidparser_handle_parse_tree;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if (current->entity_item_type != R_ITEM_COLLECTION) {
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 	attribute = current->entity_item_attributes;
8127c478bd9Sstevel@tonic-gate 	while (attribute != NULL) {
8137c478bd9Sstevel@tonic-gate 		if (attribute->entity_attribute_tag == R_ITEM_USAGE) {
8147c478bd9Sstevel@tonic-gate 			found_usage_id = 1;
8157c478bd9Sstevel@tonic-gate 			usage = hidparser_find_unsigned_val(attribute);
8167c478bd9Sstevel@tonic-gate 			*usage_id = HID_USAGE_ID(usage);
8177c478bd9Sstevel@tonic-gate 			if (attribute->entity_attribute_length == 3) {
8187c478bd9Sstevel@tonic-gate 				if (HID_USAGE_PAGE(usage)) {
8197c478bd9Sstevel@tonic-gate 					found_page = 1;
8207c478bd9Sstevel@tonic-gate 					*usage_page = HID_USAGE_PAGE(usage);
8217c478bd9Sstevel@tonic-gate 				}
8227c478bd9Sstevel@tonic-gate 			}
8237c478bd9Sstevel@tonic-gate 			if (found_usage_id && found_page) {
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
8267c478bd9Sstevel@tonic-gate 			}
8277c478bd9Sstevel@tonic-gate 		} else if (attribute->entity_attribute_tag ==
8287c478bd9Sstevel@tonic-gate 		    R_ITEM_USAGE_PAGE) {
8297c478bd9Sstevel@tonic-gate 			found_page = 1;
8307c478bd9Sstevel@tonic-gate 			*usage_page = attribute->entity_attribute_value[0];
8317c478bd9Sstevel@tonic-gate 			if (found_usage_id && found_page) {
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 				return (HIDPARSER_SUCCESS);
8347c478bd9Sstevel@tonic-gate 			}
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		attribute = attribute->entity_attribute_next;
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * hidparser_get_usage_list_in_order:
8457c478bd9Sstevel@tonic-gate  *	Find all the usages corresponding to a main item and report id.
8467c478bd9Sstevel@tonic-gate  *	Note that only short items are supported.
8477c478bd9Sstevel@tonic-gate  *
8487c478bd9Sstevel@tonic-gate  * Arguments:
8497c478bd9Sstevel@tonic-gate  *	parser_handle:
8507c478bd9Sstevel@tonic-gate  *		hid parser handle
8517c478bd9Sstevel@tonic-gate  *	report id:
8527c478bd9Sstevel@tonic-gate  *		report id of the particular report where the usages belong to
8537c478bd9Sstevel@tonic-gate  *	main_item_type:
8547c478bd9Sstevel@tonic-gate  *		type of report, either Input, Output, or Feature
8557c478bd9Sstevel@tonic-gate  *	usage_list:
8567c478bd9Sstevel@tonic-gate  *		Filled in with the pointer to the first element of the
8577c478bd9Sstevel@tonic-gate  *		usage list
8587c478bd9Sstevel@tonic-gate  *
8597c478bd9Sstevel@tonic-gate  * Return values:
8607c478bd9Sstevel@tonic-gate  *	HIDPARSER_SUCCESS - returned success
8617c478bd9Sstevel@tonic-gate  *	HIDPARSER_NOT_FOUND - usage specified by the parameters was not found
8627c478bd9Sstevel@tonic-gate  *	HIDPARSER_FAILURE - unspecified failure
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@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)8657c478bd9Sstevel@tonic-gate hidparser_get_usage_list_in_order(hidparser_handle_t parser_handle,
866*239936d2SToomas Soome     uint_t report_id, uint_t main_item_type, hidparser_rpt_t *rpt)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	if ((parser_handle == NULL) ||
8707c478bd9Sstevel@tonic-gate 	    (parser_handle->hidparser_handle_parse_tree == NULL)) {
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	rpt->no_of_usages = 0;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	return (hidparser_get_usage_list_in_order_internal(
8787c478bd9Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree, HID_USAGE_UNDEFINED,
8797c478bd9Sstevel@tonic-gate 	    report_id, main_item_type, rpt));
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@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)8847c478bd9Sstevel@tonic-gate hidparser_get_usage_list_in_order_internal(entity_item_t *parser_handle,
885*239936d2SToomas Soome     uint_t collection_usage, uint_t report_id, uint_t main_item_type,
8867c478bd9Sstevel@tonic-gate     hidparser_rpt_t *rpt)
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	/* setup wrapper function */
8907c478bd9Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
8917c478bd9Sstevel@tonic-gate 	entity_attribute_t *attribute;
8927c478bd9Sstevel@tonic-gate 	uchar_t foundreportid, right_report_id, valid_usage;
893d2bde62eSgc161489 	uchar_t found_usage_min, found_usage_max, found_usage;
894d2bde62eSgc161489 	int i, j;
8957c478bd9Sstevel@tonic-gate 	int rval;
896d2bde62eSgc161489 	uint32_t usage, usage_min, usage_max, usage_id[USAGE_MAX];
897d2bde62eSgc161489 	hidparser_usage_info_t *ui;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	found_usage_min = 0;
9007c478bd9Sstevel@tonic-gate 	found_usage_max = 0;
9017c478bd9Sstevel@tonic-gate 	foundreportid = 0;
9027c478bd9Sstevel@tonic-gate 	right_report_id = 0;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	while (current) {
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 			/*
9097c478bd9Sstevel@tonic-gate 			 * find collection usage information for this
9107c478bd9Sstevel@tonic-gate 			 * collection
9117c478bd9Sstevel@tonic-gate 			 */
9127c478bd9Sstevel@tonic-gate 			valid_usage = 0;
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 			while (attribute != NULL) {
9177c478bd9Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
9187c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE) {
9197c478bd9Sstevel@tonic-gate 					usage = hidparser_find_unsigned_val(
9207c478bd9Sstevel@tonic-gate 					    attribute);
9217c478bd9Sstevel@tonic-gate 					valid_usage = 1;
9227c478bd9Sstevel@tonic-gate 				}
9237c478bd9Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
9247c478bd9Sstevel@tonic-gate 			}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 			if (!valid_usage) {
9277c478bd9Sstevel@tonic-gate 				usage = HID_USAGE_UNDEFINED;
9287c478bd9Sstevel@tonic-gate 			}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 			rval = hidparser_get_usage_list_in_order_internal(
9317c478bd9Sstevel@tonic-gate 			    current->info.child, usage,
9327c478bd9Sstevel@tonic-gate 			    report_id, main_item_type, rpt);
9337c478bd9Sstevel@tonic-gate 			if (rval != HIDPARSER_SUCCESS) {
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 				return (rval);
9367c478bd9Sstevel@tonic-gate 			}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
9397c478bd9Sstevel@tonic-gate 			/* Match Item Type */
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 			foundreportid = 0;
9427c478bd9Sstevel@tonic-gate 			right_report_id = 0;
9437c478bd9Sstevel@tonic-gate 			found_usage_min = 0;
9447c478bd9Sstevel@tonic-gate 			found_usage_max = 0;
945d2bde62eSgc161489 			found_usage = 0;
9467c478bd9Sstevel@tonic-gate 			valid_usage = 0;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 			while (attribute != NULL) {
9517c478bd9Sstevel@tonic-gate 				switch (attribute->entity_attribute_tag) {
9527c478bd9Sstevel@tonic-gate 				case R_ITEM_REPORT_ID:
9537c478bd9Sstevel@tonic-gate 					foundreportid = 1;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 					if (attribute->
9567c478bd9Sstevel@tonic-gate 					    entity_attribute_value[0] ==
9577c478bd9Sstevel@tonic-gate 					    report_id) {
9587c478bd9Sstevel@tonic-gate 						right_report_id = 1;
9597c478bd9Sstevel@tonic-gate 					} else {
9607c478bd9Sstevel@tonic-gate 						/* different report id */
9617c478bd9Sstevel@tonic-gate 						valid_usage = 1;
9627c478bd9Sstevel@tonic-gate 					}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 					break;
965d2bde62eSgc161489 				case R_ITEM_USAGE:
966d2bde62eSgc161489 					if (found_usage >= USAGE_MAX) {
967d2bde62eSgc161489 
968d2bde62eSgc161489 						return (HIDPARSER_FAILURE);
969d2bde62eSgc161489 					}
970d2bde62eSgc161489 					usage = hidparser_find_unsigned_val(
971d2bde62eSgc161489 					    attribute);
972d2bde62eSgc161489 					if (usage) {
973d2bde62eSgc161489 						usage_id[found_usage] = usage;
974d2bde62eSgc161489 						found_usage++;
975d2bde62eSgc161489 					}
976d2bde62eSgc161489 
977d2bde62eSgc161489 					break;
9787c478bd9Sstevel@tonic-gate 				case R_ITEM_USAGE_MIN:
9797c478bd9Sstevel@tonic-gate 					found_usage_min = 1;
9807c478bd9Sstevel@tonic-gate 					usage_min = hidparser_find_unsigned_val(
9817c478bd9Sstevel@tonic-gate 					    attribute);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 					break;
9847c478bd9Sstevel@tonic-gate 				case R_ITEM_USAGE_MAX:
9857c478bd9Sstevel@tonic-gate 					found_usage_max = 1;
9867c478bd9Sstevel@tonic-gate 					usage_max = hidparser_find_unsigned_val(
9877c478bd9Sstevel@tonic-gate 					    attribute);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 					break;
9907c478bd9Sstevel@tonic-gate 				case R_ITEM_SET_DELIMITER:
991d2bde62eSgc161489 					/* skip over alternate usages */
992d2bde62eSgc161489 					do {
993d2bde62eSgc161489 						attribute = attribute->
994d2bde62eSgc161489 						    entity_attribute_next;
995d2bde62eSgc161489 					} while (attribute->
996d2bde62eSgc161489 					    entity_attribute_tag !=
997d2bde62eSgc161489 					    R_ITEM_SET_DELIMITER);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 					break;
10007c478bd9Sstevel@tonic-gate 				}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
10037c478bd9Sstevel@tonic-gate 			}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 			/*
1006d2bde62eSgc161489 			 * If we have a report id match (or report ids
1007d2bde62eSgc161489 			 * are not present), and have a usage item or
1008d2bde62eSgc161489 			 * usage min&max, put the usage item into the
1009d2bde62eSgc161489 			 * list. Don't put undefined usage items
1010d2bde62eSgc161489 			 * (HID_USAGE_UNDEFINED, 0) into the list;
1011d2bde62eSgc161489 			 * a 0 usage item is used to match padding
1012d2bde62eSgc161489 			 * fields that don't have an attached usage.
1013d2bde62eSgc161489 			 */
1014d2bde62eSgc161489 			if (!foundreportid ||
1015d2bde62eSgc161489 			    (foundreportid && right_report_id)) {
1016d2bde62eSgc161489 
1017d2bde62eSgc161489 				for (j = 0; j < found_usage; j++) {
1018d2bde62eSgc161489 
1019d2bde62eSgc161489 					/* Put in usage list */
1020d2bde62eSgc161489 					if (rpt->no_of_usages >= USAGE_MAX) {
1021d2bde62eSgc161489 
1022d2bde62eSgc161489 						return (HIDPARSER_FAILURE);
1023d2bde62eSgc161489 					}
1024d2bde62eSgc161489 
1025d2bde62eSgc161489 					i = rpt->no_of_usages++;
1026d2bde62eSgc161489 					ui = &(rpt->usage_descr[i]);
1027d2bde62eSgc161489 
1028d2bde62eSgc161489 					hidparser_fill_usage_info(ui,
1029d2bde62eSgc161489 					    current->entity_item_attributes);
1030d2bde62eSgc161489 
1031d2bde62eSgc161489 					ui->rptcnt /= found_usage;
1032d2bde62eSgc161489 					ui->collection_usage = collection_usage;
1033d2bde62eSgc161489 					ui->usage_id = HID_USAGE_ID(
1034d2bde62eSgc161489 					    usage_id[j]);
1035d2bde62eSgc161489 
1036d2bde62eSgc161489 					/*
1037d2bde62eSgc161489 					 * This is an extended usage ie.
1038d2bde62eSgc161489 					 * usage page in upper 16 bits
1039d2bde62eSgc161489 					 * or-ed with usage in the lower
1040d2bde62eSgc161489 					 * 16 bits.
1041d2bde62eSgc161489 					 */
1042d2bde62eSgc161489 					if (usage_id[j] >> 16) {
1043d2bde62eSgc161489 						ui->usage_page =
1044d2bde62eSgc161489 						    HID_USAGE_PAGE(usage_id[j]);
1045d2bde62eSgc161489 					}
1046d2bde62eSgc161489 
1047d2bde62eSgc161489 					rpt->report_id = report_id;
1048d2bde62eSgc161489 					valid_usage = 1;
1049d2bde62eSgc161489 				}
1050d2bde62eSgc161489 
1051d2bde62eSgc161489 				if (found_usage_min && found_usage_max) {
1052d2bde62eSgc161489 
1053d2bde62eSgc161489 					/* Put in usage list */
1054d2bde62eSgc161489 					if (rpt->no_of_usages >= USAGE_MAX) {
1055d2bde62eSgc161489 
1056d2bde62eSgc161489 						return (HIDPARSER_FAILURE);
1057d2bde62eSgc161489 					}
1058d2bde62eSgc161489 
1059d2bde62eSgc161489 					if (found_usage) {
1060d2bde62eSgc161489 
1061d2bde62eSgc161489 						/* handle duplication */
1062d2bde62eSgc161489 						ui->usage_min = HID_USAGE_ID(
1063d2bde62eSgc161489 						    usage_min);
1064d2bde62eSgc161489 						ui->usage_max = HID_USAGE_ID(
1065d2bde62eSgc161489 						    usage_max);
1066d2bde62eSgc161489 					} else {
1067d2bde62eSgc161489 						i = rpt->no_of_usages++;
1068d2bde62eSgc161489 						ui = &(rpt->usage_descr[i]);
1069d2bde62eSgc161489 
1070d2bde62eSgc161489 						hidparser_fill_usage_info(ui,
1071d2bde62eSgc161489 						    current->
1072d2bde62eSgc161489 						    entity_item_attributes);
1073d2bde62eSgc161489 
1074d2bde62eSgc161489 						ui->collection_usage =
1075d2bde62eSgc161489 						    collection_usage;
1076d2bde62eSgc161489 						ui->usage_min = HID_USAGE_ID(
1077d2bde62eSgc161489 						    usage_min);
1078d2bde62eSgc161489 						ui->usage_max = HID_USAGE_ID(
1079d2bde62eSgc161489 						    usage_max);
1080d2bde62eSgc161489 
1081d2bde62eSgc161489 						rpt->report_id = report_id;
1082d2bde62eSgc161489 						valid_usage = 1;
1083d2bde62eSgc161489 					}
1084d2bde62eSgc161489 
1085d2bde62eSgc161489 					/*
1086d2bde62eSgc161489 					 * This is an extended usage ie.
1087d2bde62eSgc161489 					 * usage page in upper 16 bits
1088d2bde62eSgc161489 					 * or-ed with usage_max in the lower
1089d2bde62eSgc161489 					 * 16 bits.
1090d2bde62eSgc161489 					 */
1091d2bde62eSgc161489 					if (usage_max >> 16) {
1092d2bde62eSgc161489 						ui->usage_page =
1093d2bde62eSgc161489 						    HID_USAGE_PAGE(usage_max);
1094d2bde62eSgc161489 					}
1095d2bde62eSgc161489 				}
1096d2bde62eSgc161489 			}
1097d2bde62eSgc161489 
1098d2bde62eSgc161489 			/*
10997c478bd9Sstevel@tonic-gate 			 * This main item contains no usage
11007c478bd9Sstevel@tonic-gate 			 * Fill in with usage "UNDEFINED".
11017c478bd9Sstevel@tonic-gate 			 * If report id is valid, only the
11027c478bd9Sstevel@tonic-gate 			 * main item with matched report id
11037c478bd9Sstevel@tonic-gate 			 * can be filled in.
11047c478bd9Sstevel@tonic-gate 			 */
11057c478bd9Sstevel@tonic-gate 			if (!valid_usage) {
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 				if (rpt->no_of_usages >= USAGE_MAX) {
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 					return (HIDPARSER_FAILURE);
11107c478bd9Sstevel@tonic-gate 				}
11117c478bd9Sstevel@tonic-gate 
1112d2bde62eSgc161489 				i = rpt->no_of_usages++;
1113d2bde62eSgc161489 				ui = &(rpt->usage_descr[i]);
1114d2bde62eSgc161489 
1115d2bde62eSgc161489 				hidparser_fill_usage_info(ui,
11167c478bd9Sstevel@tonic-gate 				    current->entity_item_attributes);
11177c478bd9Sstevel@tonic-gate 
1118d2bde62eSgc161489 				ui->collection_usage = collection_usage;
1119d2bde62eSgc161489 				ui->usage_id = HID_USAGE_UNDEFINED;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 				rpt->report_id = report_id;
11227c478bd9Sstevel@tonic-gate 			}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 		}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	} /* end while current */
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
11317c478bd9Sstevel@tonic-gate }
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate /*
11357c478bd9Sstevel@tonic-gate  * hidparser_fill_usage_info():
11367c478bd9Sstevel@tonic-gate  *	Fill in the mandatory item information for a main item.
11377c478bd9Sstevel@tonic-gate  *	See HID 6.2.2.
11387c478bd9Sstevel@tonic-gate  */
11397c478bd9Sstevel@tonic-gate static void
hidparser_fill_usage_info(hidparser_usage_info_t * ui,entity_attribute_t * attribute)11407c478bd9Sstevel@tonic-gate hidparser_fill_usage_info(hidparser_usage_info_t *ui,
11417c478bd9Sstevel@tonic-gate     entity_attribute_t *attribute)
11427c478bd9Sstevel@tonic-gate {
11437c478bd9Sstevel@tonic-gate 	bzero(ui, sizeof (*ui));
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	while (attribute) {
11467c478bd9Sstevel@tonic-gate 		switch (attribute->entity_attribute_tag) {
11477c478bd9Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MINIMUM:
11487c478bd9Sstevel@tonic-gate 			ui->lmin = hidparser_find_signed_val(attribute);
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 			break;
11517c478bd9Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MAXIMUM:
11527c478bd9Sstevel@tonic-gate 			ui->lmax = hidparser_find_signed_val(attribute);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 			break;
11557c478bd9Sstevel@tonic-gate 		case R_ITEM_REPORT_COUNT:
11567c478bd9Sstevel@tonic-gate 			ui->rptcnt = hidparser_find_unsigned_val(attribute);
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 			break;
11597c478bd9Sstevel@tonic-gate 		case R_ITEM_REPORT_SIZE:
11607c478bd9Sstevel@tonic-gate 			ui->rptsz = hidparser_find_unsigned_val(attribute);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 			break;
11637c478bd9Sstevel@tonic-gate 		case R_ITEM_USAGE_PAGE:
11647c478bd9Sstevel@tonic-gate 			ui->usage_page = hidparser_find_unsigned_val(attribute)
11657c478bd9Sstevel@tonic-gate 			    & 0xffff;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 			break;
11687c478bd9Sstevel@tonic-gate 		}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 		attribute = attribute->entity_attribute_next;
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate /*
11767c478bd9Sstevel@tonic-gate  * hidparser_get_report_id_list:
11777c478bd9Sstevel@tonic-gate  *	Return a list of all report ids used for descriptor items
11787c478bd9Sstevel@tonic-gate  *	corresponding to a main item.
11797c478bd9Sstevel@tonic-gate  *
11807c478bd9Sstevel@tonic-gate  * Arguments:
11817c478bd9Sstevel@tonic-gate  *	parser_handle:
11827c478bd9Sstevel@tonic-gate  *		hid parser handle
11837c478bd9Sstevel@tonic-gate  *	main_item_type:
11847c478bd9Sstevel@tonic-gate  *		type of report, either Input, Output, or Feature
11857c478bd9Sstevel@tonic-gate  *	report_id_list:
11867c478bd9Sstevel@tonic-gate  *		Filled in with a list of report ids found in the descriptor
11877c478bd9Sstevel@tonic-gate  *
11887c478bd9Sstevel@tonic-gate  * Return values:
11897c478bd9Sstevel@tonic-gate  *	HIDPARSER_SUCCESS - returned success
11907c478bd9Sstevel@tonic-gate  *	HIDPARSER_FAILURE - unspecified failure
11917c478bd9Sstevel@tonic-gate  */
11927c478bd9Sstevel@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)11937c478bd9Sstevel@tonic-gate hidparser_get_report_id_list(hidparser_handle_t parser_handle,
1194*239936d2SToomas Soome     uint_t main_item_type, hidparser_report_id_list_t *report_id_list)
11957c478bd9Sstevel@tonic-gate {
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	if ((parser_handle == NULL) ||
11987c478bd9Sstevel@tonic-gate 	    (parser_handle->hidparser_handle_parse_tree == NULL)) {
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	report_id_list->no_of_report_ids = 0;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	return (hidparser_get_report_id_list_internal(
12067c478bd9Sstevel@tonic-gate 	    parser_handle->hidparser_handle_parse_tree,
12077c478bd9Sstevel@tonic-gate 	    main_item_type, report_id_list));
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate /*
12127c478bd9Sstevel@tonic-gate  * hidparser_get_report_id_list_internal:
12137c478bd9Sstevel@tonic-gate  *	internal function that generates list of all report ids
12147c478bd9Sstevel@tonic-gate  */
12157c478bd9Sstevel@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)1216*239936d2SToomas Soome hidparser_get_report_id_list_internal(entity_item_t *parser_handle,
1217*239936d2SToomas Soome     uint_t main_item_type, hidparser_report_id_list_t *id_lst)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate 	/* setup wrapper function */
12207c478bd9Sstevel@tonic-gate 	entity_item_t *current = parser_handle;
12217c478bd9Sstevel@tonic-gate 	entity_attribute_t *attribute;
12227c478bd9Sstevel@tonic-gate 	uint_t report_id = 0;
12237c478bd9Sstevel@tonic-gate 	int i = 0;
12247c478bd9Sstevel@tonic-gate 	int rval;
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	while (current) {
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 			rval = hidparser_get_report_id_list_internal(
12317c478bd9Sstevel@tonic-gate 			    current->info.child, main_item_type, id_lst);
12327c478bd9Sstevel@tonic-gate 			if (rval != HIDPARSER_SUCCESS) {
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 				return (rval);
12357c478bd9Sstevel@tonic-gate 			}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 		} else if (current->entity_item_type == main_item_type) {
12387c478bd9Sstevel@tonic-gate 			/* Match Item Type */
12397c478bd9Sstevel@tonic-gate 			attribute = current->entity_item_attributes;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 			while (attribute != NULL) {
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 				if (attribute->entity_attribute_tag ==
12447c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_ID) {
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 					/* Found a Report ID */
12477c478bd9Sstevel@tonic-gate 					report_id = attribute->
12487c478bd9Sstevel@tonic-gate 					    entity_attribute_value[0];
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 					/* Report ID already in list? */
12517c478bd9Sstevel@tonic-gate 					for (i = 0;
12527c478bd9Sstevel@tonic-gate 					    i < id_lst->no_of_report_ids;
12537c478bd9Sstevel@tonic-gate 					    i++) {
12547c478bd9Sstevel@tonic-gate 						if (report_id == id_lst->
12557c478bd9Sstevel@tonic-gate 						    report_id[i]) {
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 							break;
12587c478bd9Sstevel@tonic-gate 						}
12597c478bd9Sstevel@tonic-gate 					}
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 					if (i >= id_lst->no_of_report_ids) {
12627c478bd9Sstevel@tonic-gate 						/*
12637c478bd9Sstevel@tonic-gate 						 * New Report ID found, put
12647c478bd9Sstevel@tonic-gate 						 * in list
12657c478bd9Sstevel@tonic-gate 						 */
12667c478bd9Sstevel@tonic-gate 						if (i >= REPORT_ID_MAX) {
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 							return
12697c478bd9Sstevel@tonic-gate 							    (HIDPARSER_FAILURE);
12707c478bd9Sstevel@tonic-gate 						}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 						id_lst->report_id[i] =
12737c478bd9Sstevel@tonic-gate 						    report_id;
12747c478bd9Sstevel@tonic-gate 						id_lst->no_of_report_ids++;
12757c478bd9Sstevel@tonic-gate 					}
12767c478bd9Sstevel@tonic-gate 				}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 				attribute = attribute->entity_attribute_next;
12797c478bd9Sstevel@tonic-gate 			}
12807c478bd9Sstevel@tonic-gate 		}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	} /* end while current */
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate /*
12917c478bd9Sstevel@tonic-gate  * hidparser_print_report_descr_handle:
12927c478bd9Sstevel@tonic-gate  *	Functions to print the parse tree. Currently not
12937c478bd9Sstevel@tonic-gate  *	being called.
12947c478bd9Sstevel@tonic-gate  */
12957c478bd9Sstevel@tonic-gate static int
hidparser_print_report_descr_handle(entity_item_t * handle,int indent_level)1296*239936d2SToomas Soome hidparser_print_report_descr_handle(entity_item_t *handle, int indent_level)
12977c478bd9Sstevel@tonic-gate {
12987c478bd9Sstevel@tonic-gate 	entity_item_t *current = handle;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	while (current) {
13017c478bd9Sstevel@tonic-gate 		if (current->info.child) {
13027c478bd9Sstevel@tonic-gate 			hidparser_print_entity(current, indent_level);
13037c478bd9Sstevel@tonic-gate 			/* do children */
13047c478bd9Sstevel@tonic-gate 			(void) hidparser_print_report_descr_handle(
13057c478bd9Sstevel@tonic-gate 			    current->info.child, indent_level+1);
13067c478bd9Sstevel@tonic-gate 		} else /* just a regular entity */ {
13077c478bd9Sstevel@tonic-gate 			hidparser_print_entity(current, indent_level);
13087c478bd9Sstevel@tonic-gate 		}
13097c478bd9Sstevel@tonic-gate 		current = current->entity_item_right_sibling;
13107c478bd9Sstevel@tonic-gate 	}
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate #define	SPACE_PER_LEVEL 5
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate /*
13197c478bd9Sstevel@tonic-gate  * hidparser_print_entity ;
13207c478bd9Sstevel@tonic-gate  * Prints the entity items recursively
13217c478bd9Sstevel@tonic-gate  */
13227c478bd9Sstevel@tonic-gate static void
hidparser_print_entity(entity_item_t * entity,int indent_level)13237c478bd9Sstevel@tonic-gate hidparser_print_entity(entity_item_t *entity, int indent_level)
13247c478bd9Sstevel@tonic-gate {
13257c478bd9Sstevel@tonic-gate 	char indent_space[256];
13267c478bd9Sstevel@tonic-gate 	int count;
13277c478bd9Sstevel@tonic-gate 	entity_attribute_t *attr;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	indent_level *= SPACE_PER_LEVEL;
13307c478bd9Sstevel@tonic-gate 
1331112116d8Sfb209375 	for (count = 0; indent_level--; count++)
1332112116d8Sfb209375 		indent_space[count] = ' ';
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	indent_space[count] = 0;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	attr = entity->entity_item_attributes;
13377c478bd9Sstevel@tonic-gate 	while (attr) {
13387c478bd9Sstevel@tonic-gate 		hidparser_print_this_attribute(attr, indent_space);
13397c478bd9Sstevel@tonic-gate 		attr = attr->entity_attribute_next;
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle, "%s%s(0x%x)",
13437c478bd9Sstevel@tonic-gate 	    indent_space, items[entity->entity_item_type],
13447c478bd9Sstevel@tonic-gate 	    (entity->entity_item_params_leng ?
13457c478bd9Sstevel@tonic-gate 	    entity->entity_item_params[0] & 0xFF : 0x00));
13467c478bd9Sstevel@tonic-gate }
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /*
13507c478bd9Sstevel@tonic-gate  * hidparser_print_this_attribute:
13517c478bd9Sstevel@tonic-gate  *	Prints the attribute passed in the argument
13527c478bd9Sstevel@tonic-gate  */
13537c478bd9Sstevel@tonic-gate static void
hidparser_print_this_attribute(entity_attribute_t * attribute,char * ident_space)1354*239936d2SToomas Soome hidparser_print_this_attribute(entity_attribute_t *attribute, char *ident_space)
13557c478bd9Sstevel@tonic-gate {
13567c478bd9Sstevel@tonic-gate 	if (ident_space == NULL) {
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
13597c478bd9Sstevel@tonic-gate 		    "%s(0x%X)",
13607c478bd9Sstevel@tonic-gate 		    items[attribute->entity_attribute_tag],
13617c478bd9Sstevel@tonic-gate 		    hidparser_find_unsigned_val(attribute));
13627c478bd9Sstevel@tonic-gate 	} else {
13637c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
13647c478bd9Sstevel@tonic-gate 		    "%s%s(0x%X)", ident_space,
13657c478bd9Sstevel@tonic-gate 		    items[attribute->entity_attribute_tag],
13667c478bd9Sstevel@tonic-gate 		    hidparser_find_unsigned_val(attribute));
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate /*
13737c478bd9Sstevel@tonic-gate  * The next few functions will be used for parsing using the
13747c478bd9Sstevel@tonic-gate  * grammar:
13757c478bd9Sstevel@tonic-gate  *
13767c478bd9Sstevel@tonic-gate  *	Start			-> ReportDescriptor <EOF>
13777c478bd9Sstevel@tonic-gate  *
13787c478bd9Sstevel@tonic-gate  *	ReportDescriptor	-> ItemList
13797c478bd9Sstevel@tonic-gate  *
13807c478bd9Sstevel@tonic-gate  *	ItemList		-> Items MainItem ItemList
13817c478bd9Sstevel@tonic-gate  *				   | epsilon
13827c478bd9Sstevel@tonic-gate  *
13837c478bd9Sstevel@tonic-gate  *	MainItem		-> BeginCollection ItemList  EndCollection
13847c478bd9Sstevel@tonic-gate  *				  | Input
13857c478bd9Sstevel@tonic-gate  *				  | Output
13867c478bd9Sstevel@tonic-gate  *				  | Feature
13877c478bd9Sstevel@tonic-gate  *
13887c478bd9Sstevel@tonic-gate  *	Items			-> GlobalItem  Items
13897c478bd9Sstevel@tonic-gate  *				   | LocalItem Items
13907c478bd9Sstevel@tonic-gate  *				   | SetDelimiterOpen LocalItemList
13917c478bd9Sstevel@tonic-gate  *					SetDelimiterClose Items
13927c478bd9Sstevel@tonic-gate  *				   | epsilon
13937c478bd9Sstevel@tonic-gate  *
13947c478bd9Sstevel@tonic-gate  *	LocalItemList		-> LocalItem Temp2
13957c478bd9Sstevel@tonic-gate  *
13967c478bd9Sstevel@tonic-gate  *	Temp2			-> LocalItem Temp2
13977c478bd9Sstevel@tonic-gate  *				   | epsilon
13987c478bd9Sstevel@tonic-gate  *
13997c478bd9Sstevel@tonic-gate  *	GlobalItem		-> UsagePage
14007c478bd9Sstevel@tonic-gate  *				   | LogicalMinimum
14017c478bd9Sstevel@tonic-gate  *				   | LogicalMaximum
14027c478bd9Sstevel@tonic-gate  *				   | PhysicalMinimum
14037c478bd9Sstevel@tonic-gate  *				   | PhysicalMaximum
14047c478bd9Sstevel@tonic-gate  *				   | Unit
14057c478bd9Sstevel@tonic-gate  *				   | Exponent
14067c478bd9Sstevel@tonic-gate  *				   | ReportSize
14077c478bd9Sstevel@tonic-gate  *				   | ReportCount
14087c478bd9Sstevel@tonic-gate  *				   | ReportID
14097c478bd9Sstevel@tonic-gate  *
14107c478bd9Sstevel@tonic-gate  *	LocalItem		-> Usage
14117c478bd9Sstevel@tonic-gate  *				   | UsageMinimum
14127c478bd9Sstevel@tonic-gate  *				   | UsageMaximum
14137c478bd9Sstevel@tonic-gate  *				   | DesignatorIndex
14147c478bd9Sstevel@tonic-gate  *				   | DesignatorMinimum
14157c478bd9Sstevel@tonic-gate  *				   | StringIndex
14167c478bd9Sstevel@tonic-gate  *				   | StringMinimum
14177c478bd9Sstevel@tonic-gate  *				   | StringMaximum
14187c478bd9Sstevel@tonic-gate  *
14197c478bd9Sstevel@tonic-gate  */
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate /*
14237c478bd9Sstevel@tonic-gate  * hidparser_lookup_first:
14247c478bd9Sstevel@tonic-gate  *	Looks up if token belongs to the FIRST of the function tag
14257c478bd9Sstevel@tonic-gate  *	that is passed through the first argument
14267c478bd9Sstevel@tonic-gate  */
14277c478bd9Sstevel@tonic-gate static int
hidparser_lookup_first(int func_index,int token)1428*239936d2SToomas Soome hidparser_lookup_first(int func_index, int token)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	int	*itemp;
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	itemp = hid_first_list[func_index];
14337c478bd9Sstevel@tonic-gate 	while (*itemp != 0) {
14347c478bd9Sstevel@tonic-gate 		/* get the next terminal on the list */
14357c478bd9Sstevel@tonic-gate 		if (*itemp == token) {
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 			return (HIDPARSER_SUCCESS);
14387c478bd9Sstevel@tonic-gate 		}
14397c478bd9Sstevel@tonic-gate 		itemp++;
14407c478bd9Sstevel@tonic-gate 	}
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	/* token is not on the FIRST list */
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate /*
14497c478bd9Sstevel@tonic-gate  * hidparser_main:
14507c478bd9Sstevel@tonic-gate  *	Function called from hidparser_parse_report_descriptor()
14517c478bd9Sstevel@tonic-gate  *	to parse the Report Descriptor
14527c478bd9Sstevel@tonic-gate  */
14537c478bd9Sstevel@tonic-gate static int
hidparser_main(unsigned char * descriptor,size_t size,entity_item_t ** item_ptr)1454*239936d2SToomas Soome hidparser_main(unsigned char *descriptor, size_t size, entity_item_t **item_ptr)
14557c478bd9Sstevel@tonic-gate {
14567c478bd9Sstevel@tonic-gate 	hidparser_tok_t	*scan_ifp;
14577c478bd9Sstevel@tonic-gate 	int retval;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	scan_ifp = kmem_zalloc(sizeof (hidparser_tok_t), KM_SLEEP);
14607c478bd9Sstevel@tonic-gate 	scan_ifp->hidparser_tok_text =
14617c478bd9Sstevel@tonic-gate 	    kmem_zalloc(HIDPARSER_TEXT_LENGTH, KM_SLEEP);
14627c478bd9Sstevel@tonic-gate 	scan_ifp->hidparser_tok_max_bsize = size;
14637c478bd9Sstevel@tonic-gate 	scan_ifp->hidparser_tok_entity_descriptor = descriptor;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	*item_ptr = NULL;
14667c478bd9Sstevel@tonic-gate 	retval =  hidparser_ReportDescriptorDash(item_ptr, scan_ifp);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	/*
14697c478bd9Sstevel@tonic-gate 	 * Free the Local & Global item list
14707c478bd9Sstevel@tonic-gate 	 * It maybe the case that no tree has been built
14717c478bd9Sstevel@tonic-gate 	 * up but there have been allocation in the attribute
14727c478bd9Sstevel@tonic-gate 	 * & control lists
14737c478bd9Sstevel@tonic-gate 	 */
14747c478bd9Sstevel@tonic-gate 	if (scan_ifp->hidparser_tok_gitem_head) {
14757c478bd9Sstevel@tonic-gate 		hidparser_free_attribute_list(
14767c478bd9Sstevel@tonic-gate 		    scan_ifp->hidparser_tok_gitem_head);
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	if (scan_ifp->hidparser_tok_litem_head) {
14807c478bd9Sstevel@tonic-gate 		hidparser_free_attribute_list(
14817c478bd9Sstevel@tonic-gate 		    scan_ifp->hidparser_tok_litem_head);
14827c478bd9Sstevel@tonic-gate 	}
14837c478bd9Sstevel@tonic-gate 	kmem_free(scan_ifp->hidparser_tok_text, HIDPARSER_TEXT_LENGTH);
14847c478bd9Sstevel@tonic-gate 	kmem_free(scan_ifp, sizeof (hidparser_tok_t));
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	return (retval);
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate /*
14917c478bd9Sstevel@tonic-gate  * hidparser_ReportDescriptorDash:
14927c478bd9Sstevel@tonic-gate  *	Synthetic start symbol, implements
14937c478bd9Sstevel@tonic-gate  *	hidparser_ReportDescriptor <EOF>
14947c478bd9Sstevel@tonic-gate  */
14957c478bd9Sstevel@tonic-gate static int
hidparser_ReportDescriptorDash(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)14967c478bd9Sstevel@tonic-gate hidparser_ReportDescriptorDash(entity_item_t **item_ptr,
14977c478bd9Sstevel@tonic-gate     hidparser_tok_t *scan_ifp)
14987c478bd9Sstevel@tonic-gate {
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	if ((hidparser_ReportDescriptor(item_ptr, scan_ifp) ==
15017c478bd9Sstevel@tonic-gate 	    HIDPARSER_SUCCESS) && (scan_ifp->hidparser_tok_token == 0)) {
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 		return (HIDPARSER_SUCCESS);
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	/*
15077c478bd9Sstevel@tonic-gate 	 * In case of failure, free the kernel memory
15087c478bd9Sstevel@tonic-gate 	 * allocated for partial building of the tree,
15097c478bd9Sstevel@tonic-gate 	 * if any
15107c478bd9Sstevel@tonic-gate 	 */
15117c478bd9Sstevel@tonic-gate 	if (*item_ptr != NULL) {
15127c478bd9Sstevel@tonic-gate 		(void) hidparser_free_report_descr_handle(*item_ptr);
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	*item_ptr = NULL;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
15187c478bd9Sstevel@tonic-gate }
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate /*
15227c478bd9Sstevel@tonic-gate  * hidparser_ReportDescriptor:
15237c478bd9Sstevel@tonic-gate  *	Implements the Rule:
15247c478bd9Sstevel@tonic-gate  *	ReportDescriptor -> ItemList
15257c478bd9Sstevel@tonic-gate  */
15267c478bd9Sstevel@tonic-gate static int
hidparser_ReportDescriptor(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)1527*239936d2SToomas Soome hidparser_ReportDescriptor(entity_item_t **item_ptr, hidparser_tok_t *scan_ifp)
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	hidparser_scan(scan_ifp);
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	/*
15327c478bd9Sstevel@tonic-gate 	 * We do not search for the token in FIRST(ReportDescriptor)
15337c478bd9Sstevel@tonic-gate 	 * since -
15347c478bd9Sstevel@tonic-gate 	 *
15357c478bd9Sstevel@tonic-gate 	 * FIRST(ReportDescriptor) == FIRST(ItemList)
15367c478bd9Sstevel@tonic-gate 	 * ReportDescriptor ----> ItemList
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	if (hidparser_ItemList(item_ptr, scan_ifp) == HIDPARSER_SUCCESS) {
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 		return (HIDPARSER_SUCCESS);
15417c478bd9Sstevel@tonic-gate 	}
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate /*
15487c478bd9Sstevel@tonic-gate  * hidparser_ItemList:
15497c478bd9Sstevel@tonic-gate  *	Implements the Rule:
15507c478bd9Sstevel@tonic-gate  *	ItemList -> Items MainItem ItemList | epsilon
15517c478bd9Sstevel@tonic-gate  *
15527c478bd9Sstevel@tonic-gate  *	This function constructs the tree on which depends the "hidparser"
15537c478bd9Sstevel@tonic-gate  *	consumer functions. Basically the structure of the tree is
15547c478bd9Sstevel@tonic-gate  *
15557c478bd9Sstevel@tonic-gate  *	C--[RS]->EC--[RS]->C--[RS]->EC..(and so on)
15567c478bd9Sstevel@tonic-gate  *	|
15577c478bd9Sstevel@tonic-gate  *    [CH] <== This relationship is true for other "C's"
15587c478bd9Sstevel@tonic-gate  *	|      also.
15597c478bd9Sstevel@tonic-gate  *	v
15607c478bd9Sstevel@tonic-gate  *     C/-------------/I/O/F <== [ Any of these ]
15617c478bd9Sstevel@tonic-gate  *     |	      ------
15627c478bd9Sstevel@tonic-gate  *     |		|
15637c478bd9Sstevel@tonic-gate  *     v		v
15647c478bd9Sstevel@tonic-gate  *    [CH      | RS]  [ RS ]
15657c478bd9Sstevel@tonic-gate  *     C/I/O/F | EC    I/O/F
15667c478bd9Sstevel@tonic-gate  *     |
15677c478bd9Sstevel@tonic-gate  *     |
15687c478bd9Sstevel@tonic-gate  *    and so on...
15697c478bd9Sstevel@tonic-gate  *
15707c478bd9Sstevel@tonic-gate  *	where	 C = Collection
15717c478bd9Sstevel@tonic-gate  *		EC = EndCollection
15727c478bd9Sstevel@tonic-gate  *		 I = Input
15737c478bd9Sstevel@tonic-gate  *		 O = Output
15747c478bd9Sstevel@tonic-gate  *		 F = Feature "Main" Items.
15757c478bd9Sstevel@tonic-gate  *
15767c478bd9Sstevel@tonic-gate  *	and the relationships are  [RS] for right sibling and [CH] for
15777c478bd9Sstevel@tonic-gate  *	child. [CH | RS ] stands for "child or right sibling" with the
15787c478bd9Sstevel@tonic-gate  *	possible values below it.
15797c478bd9Sstevel@tonic-gate  */
15807c478bd9Sstevel@tonic-gate static int
hidparser_ItemList(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)15817c478bd9Sstevel@tonic-gate hidparser_ItemList(entity_item_t **item_ptr, hidparser_tok_t *scan_ifp)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	entity_item_t	*curr_ei, *cache_ei, *prev_ei, *tmp_ei;
15847c478bd9Sstevel@tonic-gate 	boolean_t	root_coll = B_FALSE;
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	curr_ei = cache_ei = prev_ei = tmp_ei = NULL;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	while (scan_ifp->hidparser_tok_token != 0) {
15897c478bd9Sstevel@tonic-gate 		if (hidparser_Items(scan_ifp) == HIDPARSER_FAILURE) {
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 			return (HIDPARSER_FAILURE);
15927c478bd9Sstevel@tonic-gate 		}
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 		if (hidparser_MainItem(&curr_ei, scan_ifp) ==
15957c478bd9Sstevel@tonic-gate 		    HIDPARSER_FAILURE) {
15967c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_ALL,
15977c478bd9Sstevel@tonic-gate 			    hparser_log_handle,
15987c478bd9Sstevel@tonic-gate 			    "Invalid MAIN item 0x%x in input stream",
15997c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_token);
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 			return (HIDPARSER_FAILURE);
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 		if (curr_ei->entity_item_type == R_ITEM_COLLECTION) {
16047c478bd9Sstevel@tonic-gate 			if (root_coll == B_FALSE) {
16057c478bd9Sstevel@tonic-gate 				*item_ptr = curr_ei;
16067c478bd9Sstevel@tonic-gate 				root_coll = B_TRUE;
16077c478bd9Sstevel@tonic-gate 			}
16087c478bd9Sstevel@tonic-gate 			curr_ei->prev_coll = cache_ei;
16097c478bd9Sstevel@tonic-gate 			cache_ei = curr_ei;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_ALL,
16127c478bd9Sstevel@tonic-gate 			    hparser_log_handle,
1613112116d8Sfb209375 			    "Start Collection:cache_ei = 0x%p,"
1614112116d8Sfb209375 			    " curr_ei = 0x%p",
1615112116d8Sfb209375 			    (void *)cache_ei, (void *)curr_ei);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 			if (prev_ei == NULL) {
16187c478bd9Sstevel@tonic-gate 				prev_ei = curr_ei;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 				continue;
16217c478bd9Sstevel@tonic-gate 			}
16227c478bd9Sstevel@tonic-gate 			if (prev_ei->entity_item_type ==
16237c478bd9Sstevel@tonic-gate 			    R_ITEM_COLLECTION) {
16247c478bd9Sstevel@tonic-gate 				prev_ei->info.child = curr_ei;
16257c478bd9Sstevel@tonic-gate 			} else {
16267c478bd9Sstevel@tonic-gate 				prev_ei->entity_item_right_sibling =
16277c478bd9Sstevel@tonic-gate 				    curr_ei;
16287c478bd9Sstevel@tonic-gate 			}
16297c478bd9Sstevel@tonic-gate 		} else if (curr_ei->entity_item_type ==
16307c478bd9Sstevel@tonic-gate 		    R_ITEM_END_COLLECTION) {
16317c478bd9Sstevel@tonic-gate 			tmp_ei = cache_ei->prev_coll;
16327c478bd9Sstevel@tonic-gate 			cache_ei->entity_item_right_sibling = curr_ei;
16337c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_ALL,
16347c478bd9Sstevel@tonic-gate 			    hparser_log_handle,
1635112116d8Sfb209375 			    "End Collection: cache_ei = 0x%p, "
1636112116d8Sfb209375 			    "curr_ei = 0x%p",
1637112116d8Sfb209375 			    (void *)cache_ei, (void *)curr_ei);
16387c478bd9Sstevel@tonic-gate 			if (tmp_ei != NULL) {
16397c478bd9Sstevel@tonic-gate 				/*
16407c478bd9Sstevel@tonic-gate 				 * As will be the case for final end
16417c478bd9Sstevel@tonic-gate 				 * collection.
16427c478bd9Sstevel@tonic-gate 				 */
16437c478bd9Sstevel@tonic-gate 				cache_ei = tmp_ei;
16447c478bd9Sstevel@tonic-gate 			}
16457c478bd9Sstevel@tonic-gate 			tmp_ei = NULL;
16467c478bd9Sstevel@tonic-gate 		} else {
1647e7cc2e17Sbc224572 			if (prev_ei == NULL) {
1648e7cc2e17Sbc224572 				USB_DPRINTF_L2(PRINT_MASK_ALL,
1649e7cc2e17Sbc224572 				    hparser_log_handle,
1650e7cc2e17Sbc224572 				    "Invalid First MAIN item 0x%x",
1651e7cc2e17Sbc224572 				    scan_ifp->hidparser_tok_token);
1652e7cc2e17Sbc224572 
1653e7cc2e17Sbc224572 				return (HIDPARSER_FAILURE);
1654e7cc2e17Sbc224572 			}
16557c478bd9Sstevel@tonic-gate 			if (prev_ei->entity_item_type ==
16567c478bd9Sstevel@tonic-gate 			    R_ITEM_COLLECTION) {
16577c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(PRINT_MASK_ALL,
16587c478bd9Sstevel@tonic-gate 				    hparser_log_handle,
16597c478bd9Sstevel@tonic-gate 				    "Main Item: token = 0x%x, "
1660112116d8Sfb209375 				    "curr_ei = 0x%p "
16617c478bd9Sstevel@tonic-gate 				    "will be the child of prev_ei "
1662112116d8Sfb209375 				    "= 0x%p, "
1663112116d8Sfb209375 				    "cache_ei being 0x%p",
16647c478bd9Sstevel@tonic-gate 				    curr_ei->entity_item_type,
1665112116d8Sfb209375 				    (void *)curr_ei, (void *)prev_ei,
1666112116d8Sfb209375 				    (void *)cache_ei);
16677c478bd9Sstevel@tonic-gate 				prev_ei->info.child = curr_ei;
16687c478bd9Sstevel@tonic-gate 			} else {
16697c478bd9Sstevel@tonic-gate 				USB_DPRINTF_L3(PRINT_MASK_ALL,
16707c478bd9Sstevel@tonic-gate 				    hparser_log_handle,
16717c478bd9Sstevel@tonic-gate 				    "Main Item: token = 0x%x, "
1672112116d8Sfb209375 				    "curr_ei = 0x%p "
16737c478bd9Sstevel@tonic-gate 				    "will be the right sibling of "
1674112116d8Sfb209375 				    "prev_ei = 0x%p, "
1675112116d8Sfb209375 				    "cache_ei being 0x%p",
16767c478bd9Sstevel@tonic-gate 				    curr_ei->entity_item_type,
1677112116d8Sfb209375 				    (void *)curr_ei, (void *)prev_ei,
1678112116d8Sfb209375 				    (void *)cache_ei);
16797c478bd9Sstevel@tonic-gate 				prev_ei->entity_item_right_sibling =
16807c478bd9Sstevel@tonic-gate 				    curr_ei;
16817c478bd9Sstevel@tonic-gate 			}
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 		prev_ei = curr_ei;
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 	if (*item_ptr != cache_ei) {
16867c478bd9Sstevel@tonic-gate 		/* Something wrong happened */
16877c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
16887c478bd9Sstevel@tonic-gate 		    "Failed to parse report descriptor");
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 		return (HIDPARSER_FAILURE);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 	(void) hidparser_print_report_descr_handle(cache_ei, 0);
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
16957c478bd9Sstevel@tonic-gate }
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /*
16997c478bd9Sstevel@tonic-gate  * hidparser_MainItem:
17007c478bd9Sstevel@tonic-gate  *	Implements the Rule:
17017c478bd9Sstevel@tonic-gate  *	MainItem ->	BeginCollection ItemList  EndCollection
17027c478bd9Sstevel@tonic-gate  *			| Input
17037c478bd9Sstevel@tonic-gate  *			| Output
17047c478bd9Sstevel@tonic-gate  *			| Feature
17057c478bd9Sstevel@tonic-gate  */
17067c478bd9Sstevel@tonic-gate static int
hidparser_MainItem(entity_item_t ** item_ptr,hidparser_tok_t * scan_ifp)1707*239936d2SToomas Soome hidparser_MainItem(entity_item_t **item_ptr, hidparser_tok_t *scan_ifp)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	switch (scan_ifp->hidparser_tok_token) {
17107c478bd9Sstevel@tonic-gate 		case R_ITEM_INPUT:
17117c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
17127c478bd9Sstevel@tonic-gate 		case R_ITEM_OUTPUT:
17137c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
17147c478bd9Sstevel@tonic-gate 		case R_ITEM_FEATURE:
17157c478bd9Sstevel@tonic-gate 		case R_ITEM_COLLECTION:
17167c478bd9Sstevel@tonic-gate 		case R_ITEM_END_COLLECTION:
17177c478bd9Sstevel@tonic-gate 			*item_ptr = hidparser_allocate_entity(scan_ifp);
17187c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
1719112116d8Sfb209375 			    "hidparser_MainItem:index = 0x%lx token = 0x%x",
17207c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_index -
17217c478bd9Sstevel@tonic-gate 			    (*item_ptr)->entity_item_params_leng - 1,
17227c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_token);
17237c478bd9Sstevel@tonic-gate 			hidparser_scan(scan_ifp);
17247c478bd9Sstevel@tonic-gate 			hidparser_global_err_check(*item_ptr);
17257c478bd9Sstevel@tonic-gate 			hidparser_local_err_check(*item_ptr);
17267c478bd9Sstevel@tonic-gate 			hidparser_mainitem_err_check(*item_ptr);
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			return (HIDPARSER_SUCCESS);
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 		default:
17317c478bd9Sstevel@tonic-gate 			break;
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	*item_ptr = NULL;
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate  * hidparser_Items:
17427c478bd9Sstevel@tonic-gate  *	Implements the Rule:
17437c478bd9Sstevel@tonic-gate  *	Items ->	GlobalItem  Items
17447c478bd9Sstevel@tonic-gate  *			| LocalItem Items
17457c478bd9Sstevel@tonic-gate  *			| SetDelimiterOpen LocalItemList
17467c478bd9Sstevel@tonic-gate  *				SetDelimiterClose Items
17477c478bd9Sstevel@tonic-gate  *			| epsilon
17487c478bd9Sstevel@tonic-gate  */
17497c478bd9Sstevel@tonic-gate static int
hidparser_Items(hidparser_tok_t * scan_ifp)17507c478bd9Sstevel@tonic-gate hidparser_Items(hidparser_tok_t *scan_ifp)
17517c478bd9Sstevel@tonic-gate {
17527c478bd9Sstevel@tonic-gate 	boolean_t delim_pre = B_FALSE;
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	int	token = scan_ifp->hidparser_tok_token;
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	while (hidparser_lookup_first(HIDPARSER_ITEMS, token) ==
17577c478bd9Sstevel@tonic-gate 	    HIDPARSER_SUCCESS) {
17587c478bd9Sstevel@tonic-gate 		if (token == R_ITEM_SET_DELIMITER) {
17597c478bd9Sstevel@tonic-gate 			if (delim_pre == B_FALSE) {
17607c478bd9Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[0] != 1) {
17617c478bd9Sstevel@tonic-gate 					hidparser_error_delim(NULL,
17627c478bd9Sstevel@tonic-gate 					    HIDPARSER_DELIM_ERR1);
17637c478bd9Sstevel@tonic-gate 				} else {
17647c478bd9Sstevel@tonic-gate 					delim_pre = B_TRUE;
17657c478bd9Sstevel@tonic-gate 				}
17667c478bd9Sstevel@tonic-gate 			} else {
17677c478bd9Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[0] !=
17687c478bd9Sstevel@tonic-gate 				    0) {
17697c478bd9Sstevel@tonic-gate 					hidparser_error_delim(NULL,
17707c478bd9Sstevel@tonic-gate 					    HIDPARSER_DELIM_ERR2);
17717c478bd9Sstevel@tonic-gate 				} else {
17727c478bd9Sstevel@tonic-gate 					delim_pre = B_FALSE;
17737c478bd9Sstevel@tonic-gate 				}
17747c478bd9Sstevel@tonic-gate 			}
17757c478bd9Sstevel@tonic-gate 			(void) hidparser_LocalItem(scan_ifp);
17767c478bd9Sstevel@tonic-gate 			token = scan_ifp->hidparser_tok_token;
17777c478bd9Sstevel@tonic-gate 		} else if (hidparser_GlobalItem(scan_ifp) ==
17787c478bd9Sstevel@tonic-gate 		    HIDPARSER_SUCCESS) {
17797c478bd9Sstevel@tonic-gate 			token = scan_ifp->hidparser_tok_token;
17807c478bd9Sstevel@tonic-gate 		} else if (hidparser_LocalItem(scan_ifp) == HIDPARSER_SUCCESS) {
17817c478bd9Sstevel@tonic-gate 			token = scan_ifp->hidparser_tok_token;
17827c478bd9Sstevel@tonic-gate 		}
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);	/* epsilon */
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate /*
17907c478bd9Sstevel@tonic-gate  * hidparser_GlobalItem:
17917c478bd9Sstevel@tonic-gate  *	Implements the Rule:
17927c478bd9Sstevel@tonic-gate  *	GlobalItem ->	UsagePage
17937c478bd9Sstevel@tonic-gate  *			| LogicalMinimum
17947c478bd9Sstevel@tonic-gate  *			| LocgicalMaximum
17957c478bd9Sstevel@tonic-gate  *			| PhysicalMinimum
17967c478bd9Sstevel@tonic-gate  *			| PhysicalMaximum
17977c478bd9Sstevel@tonic-gate  *			| Unit
17987c478bd9Sstevel@tonic-gate  *			| Exponent
17997c478bd9Sstevel@tonic-gate  *			| ReportSize
18007c478bd9Sstevel@tonic-gate  *			| ReportCount
18017c478bd9Sstevel@tonic-gate  *			| ReportID
18027c478bd9Sstevel@tonic-gate  */
18037c478bd9Sstevel@tonic-gate static int
hidparser_GlobalItem(hidparser_tok_t * scan_ifp)18047c478bd9Sstevel@tonic-gate hidparser_GlobalItem(hidparser_tok_t *scan_ifp)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	int i;
18087c478bd9Sstevel@tonic-gate 	entity_attribute_stack_t	*elem;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	switch (scan_ifp->hidparser_tok_token) {
18117c478bd9Sstevel@tonic-gate 		case R_ITEM_USAGE_PAGE:
18127c478bd9Sstevel@tonic-gate 			/* Error check */
18137c478bd9Sstevel@tonic-gate 			for (i = 0; i < scan_ifp->hidparser_tok_leng; i++) {
18147c478bd9Sstevel@tonic-gate 				/* Undefined data value: 0 */
18157c478bd9Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[i] == 0) {
18167c478bd9Sstevel@tonic-gate 					hidparser_report_err(
18177c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_WARN,
18187c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
18197c478bd9Sstevel@tonic-gate 					    R_ITEM_USAGE_PAGE,
18207c478bd9Sstevel@tonic-gate 					    0,
18217c478bd9Sstevel@tonic-gate 					    "Data field should be non-Zero");
18227c478bd9Sstevel@tonic-gate 				}
18237c478bd9Sstevel@tonic-gate 				/* Reserved values 0x0A-0xFE */
18247c478bd9Sstevel@tonic-gate 				else if ((scan_ifp->hidparser_tok_text[i] >=
18257c478bd9Sstevel@tonic-gate 				    0x0a) &&
18267c478bd9Sstevel@tonic-gate 				    (scan_ifp->hidparser_tok_text[i] <=
18277c478bd9Sstevel@tonic-gate 				    0xFE)) {
18287c478bd9Sstevel@tonic-gate 					hidparser_report_err(
18297c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_WARN,
18307c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
18317c478bd9Sstevel@tonic-gate 					    R_ITEM_USAGE_PAGE,
18327c478bd9Sstevel@tonic-gate 					    1,
18337c478bd9Sstevel@tonic-gate 					    "Data field should not use "
18347c478bd9Sstevel@tonic-gate 					    "reserved values");
18357c478bd9Sstevel@tonic-gate 				}
18367c478bd9Sstevel@tonic-gate 			}
18377c478bd9Sstevel@tonic-gate 			break;
18387c478bd9Sstevel@tonic-gate 		case R_ITEM_UNIT:
18397c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
18407c478bd9Sstevel@tonic-gate 		case R_ITEM_EXPONENT:
18417c478bd9Sstevel@tonic-gate 			/*
18427c478bd9Sstevel@tonic-gate 			 * Error check:
18437c478bd9Sstevel@tonic-gate 			 * Nibble 7 should be zero
18447c478bd9Sstevel@tonic-gate 			 */
18457c478bd9Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng == 4) {
18467c478bd9Sstevel@tonic-gate 				if ((scan_ifp->hidparser_tok_text[3] &
18477c478bd9Sstevel@tonic-gate 				    0xf0) != 0) {
18487c478bd9Sstevel@tonic-gate 					hidparser_report_err(
18497c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_WARN,
18507c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
18517c478bd9Sstevel@tonic-gate 					    scan_ifp->hidparser_tok_token,
18527c478bd9Sstevel@tonic-gate 					    0,
18537c478bd9Sstevel@tonic-gate 					    "Data field reserved bits should "
18547c478bd9Sstevel@tonic-gate 					    "be Zero");
18557c478bd9Sstevel@tonic-gate 				}
18567c478bd9Sstevel@tonic-gate 			}
18577c478bd9Sstevel@tonic-gate 			break;
18587c478bd9Sstevel@tonic-gate 		case R_ITEM_REPORT_COUNT:
18597c478bd9Sstevel@tonic-gate 			/*
18607c478bd9Sstevel@tonic-gate 			 * Error Check:
18617c478bd9Sstevel@tonic-gate 			 * Report Count should be nonzero
18627c478bd9Sstevel@tonic-gate 			 */
18637c478bd9Sstevel@tonic-gate 			for (i = 0; i < scan_ifp->hidparser_tok_leng; i++) {
18647c478bd9Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[i])
18657c478bd9Sstevel@tonic-gate 					break;
18667c478bd9Sstevel@tonic-gate 			}
18677c478bd9Sstevel@tonic-gate 			if (i == scan_ifp->hidparser_tok_leng) {
18687c478bd9Sstevel@tonic-gate 				hidparser_report_err(
18697c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
18707c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
18717c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_COUNT,
18727c478bd9Sstevel@tonic-gate 				    0,
18737c478bd9Sstevel@tonic-gate 				    "Report Count = 0");
18747c478bd9Sstevel@tonic-gate 			}
18757c478bd9Sstevel@tonic-gate 			break;
18767c478bd9Sstevel@tonic-gate 		case R_ITEM_REPORT_ID:
18777c478bd9Sstevel@tonic-gate 			/*
18787c478bd9Sstevel@tonic-gate 			 * Error check:
18797c478bd9Sstevel@tonic-gate 			 * Report Id should be nonzero & <= 255
18807c478bd9Sstevel@tonic-gate 			 */
18817c478bd9Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng != 1)	{
18827c478bd9Sstevel@tonic-gate 				hidparser_report_err(
18837c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
18847c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
18857c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_ID,
18867c478bd9Sstevel@tonic-gate 				    1,
18877c478bd9Sstevel@tonic-gate 				    "Must be contained in a byte");
18887c478bd9Sstevel@tonic-gate 			}
18897c478bd9Sstevel@tonic-gate 			if (!scan_ifp->hidparser_tok_text[0]) {
18907c478bd9Sstevel@tonic-gate 				hidparser_report_err(
18917c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
18927c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
18937c478bd9Sstevel@tonic-gate 				    R_ITEM_REPORT_ID,
18947c478bd9Sstevel@tonic-gate 				    0,
18957c478bd9Sstevel@tonic-gate 				    "Report Id must be non-zero");
18967c478bd9Sstevel@tonic-gate 			}
18977c478bd9Sstevel@tonic-gate 			break;
18987c478bd9Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MINIMUM:
18997c478bd9Sstevel@tonic-gate 			break;
19007c478bd9Sstevel@tonic-gate 		case R_ITEM_LOGICAL_MAXIMUM:
19017c478bd9Sstevel@tonic-gate 			break;
19027c478bd9Sstevel@tonic-gate 		case R_ITEM_PHYSICAL_MINIMUM:
19037c478bd9Sstevel@tonic-gate 			break;
19047c478bd9Sstevel@tonic-gate 		case R_ITEM_PHYSICAL_MAXIMUM:
19057c478bd9Sstevel@tonic-gate 			break;
19067c478bd9Sstevel@tonic-gate 		case R_ITEM_REPORT_SIZE:
19077c478bd9Sstevel@tonic-gate 			break;
19087c478bd9Sstevel@tonic-gate 		case R_ITEM_PUSH:
19097c478bd9Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng != 0)	{
19107c478bd9Sstevel@tonic-gate 				hidparser_report_err(
19117c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
19127c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
19137c478bd9Sstevel@tonic-gate 				    scan_ifp->hidparser_tok_token,
19147c478bd9Sstevel@tonic-gate 				    0,
19157c478bd9Sstevel@tonic-gate 				    "Data Field size should be zero");
19167c478bd9Sstevel@tonic-gate 			} else {
19177c478bd9Sstevel@tonic-gate 				elem = (entity_attribute_stack_t *)kmem_zalloc(
19187c478bd9Sstevel@tonic-gate 				    sizeof (entity_attribute_stack_t),
19197c478bd9Sstevel@tonic-gate 				    KM_SLEEP);
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 				elem->list = hidparser_cp_attribute_list(
19227c478bd9Sstevel@tonic-gate 				    scan_ifp->hidparser_tok_gitem_head);
19237c478bd9Sstevel@tonic-gate 				if (scan_ifp->hidparser_head) {
19247c478bd9Sstevel@tonic-gate 					elem->next = scan_ifp->hidparser_head;
19257c478bd9Sstevel@tonic-gate 				}
19267c478bd9Sstevel@tonic-gate 				scan_ifp->hidparser_head = elem;
19277c478bd9Sstevel@tonic-gate 			}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 			break;
19307c478bd9Sstevel@tonic-gate 		case R_ITEM_POP:
19317c478bd9Sstevel@tonic-gate 			if (scan_ifp->hidparser_tok_leng != 0)	{
19327c478bd9Sstevel@tonic-gate 				hidparser_report_err(
19337c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
19347c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
19357c478bd9Sstevel@tonic-gate 				    scan_ifp->hidparser_tok_token,
19367c478bd9Sstevel@tonic-gate 				    0,
19377c478bd9Sstevel@tonic-gate 				    "Data Field size should be zero");
19387c478bd9Sstevel@tonic-gate 			} else {
19397c478bd9Sstevel@tonic-gate 				/* Free the current global list */
19407c478bd9Sstevel@tonic-gate 				hidparser_free_attribute_list(scan_ifp->
19417c478bd9Sstevel@tonic-gate 				    hidparser_tok_gitem_head);
19427c478bd9Sstevel@tonic-gate 				scan_ifp->hidparser_tok_gitem_head =
19437c478bd9Sstevel@tonic-gate 				    scan_ifp->hidparser_head->list;
19447c478bd9Sstevel@tonic-gate 				scan_ifp->hidparser_head->list = NULL;
19457c478bd9Sstevel@tonic-gate 				elem = scan_ifp->hidparser_head;
19467c478bd9Sstevel@tonic-gate 				scan_ifp->hidparser_head = elem->next;
19477c478bd9Sstevel@tonic-gate 				kmem_free(elem,
19487c478bd9Sstevel@tonic-gate 				    sizeof (entity_attribute_stack_t));
19497c478bd9Sstevel@tonic-gate 			}
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 			break;
19527c478bd9Sstevel@tonic-gate 		default:
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 			return (HIDPARSER_FAILURE);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	hidparser_add_attribute(scan_ifp);
19607c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
1961112116d8Sfb209375 	    "hidparser_GlobalItem:index = 0x%lx token = 0x%x",
19627c478bd9Sstevel@tonic-gate 	    scan_ifp->hidparser_tok_index -
19637c478bd9Sstevel@tonic-gate 	    scan_ifp->hidparser_tok_leng - 1,
19647c478bd9Sstevel@tonic-gate 	    scan_ifp->hidparser_tok_token);
19657c478bd9Sstevel@tonic-gate 	hidparser_scan(scan_ifp);
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	return (HIDPARSER_SUCCESS);
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate /*
19727c478bd9Sstevel@tonic-gate  * hidparser_LocalItem:
19737c478bd9Sstevel@tonic-gate  *	Implements the Rule:
19747c478bd9Sstevel@tonic-gate  *	LocalItem ->	Usage
19757c478bd9Sstevel@tonic-gate  *			| UsageMinimum
19767c478bd9Sstevel@tonic-gate  *			| UsageMaximum
19777c478bd9Sstevel@tonic-gate  *			| DesignatorIndex
19787c478bd9Sstevel@tonic-gate  *			| DesignatorMinimum
19797c478bd9Sstevel@tonic-gate  *			| StringIndex
19807c478bd9Sstevel@tonic-gate  *			| StringMinimum
19817c478bd9Sstevel@tonic-gate  *			| StringMaximum
19827c478bd9Sstevel@tonic-gate  */
19837c478bd9Sstevel@tonic-gate static int
hidparser_LocalItem(hidparser_tok_t * scan_ifp)19847c478bd9Sstevel@tonic-gate hidparser_LocalItem(hidparser_tok_t *scan_ifp)
19857c478bd9Sstevel@tonic-gate {
19867c478bd9Sstevel@tonic-gate 	int i;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	switch (scan_ifp->hidparser_tok_token) {
19897c478bd9Sstevel@tonic-gate 		case R_ITEM_USAGE:
19907c478bd9Sstevel@tonic-gate 			/*
19917c478bd9Sstevel@tonic-gate 			 * Error Check:
19927c478bd9Sstevel@tonic-gate 			 * Data Field should be nonzero
19937c478bd9Sstevel@tonic-gate 			 */
19947c478bd9Sstevel@tonic-gate 			for (i = 0; i < scan_ifp->hidparser_tok_leng; i++) {
19957c478bd9Sstevel@tonic-gate 				if (scan_ifp->hidparser_tok_text[i])
19967c478bd9Sstevel@tonic-gate 					break;
19977c478bd9Sstevel@tonic-gate 			}
19987c478bd9Sstevel@tonic-gate 			if (i == scan_ifp->hidparser_tok_leng) {
19997c478bd9Sstevel@tonic-gate 				hidparser_report_err(
20007c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_WARN,
20017c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
20027c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE,
20037c478bd9Sstevel@tonic-gate 				    0,
20047c478bd9Sstevel@tonic-gate 				    "Data Field should be non-zero");
20057c478bd9Sstevel@tonic-gate 			}
20067c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20077c478bd9Sstevel@tonic-gate 		case R_ITEM_USAGE_MIN:
20087c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20097c478bd9Sstevel@tonic-gate 		case R_ITEM_USAGE_MAX:
20107c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20117c478bd9Sstevel@tonic-gate 		case R_ITEM_DESIGNATOR_INDEX:
20127c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20137c478bd9Sstevel@tonic-gate 		case R_ITEM_DESIGNATOR_MIN:
20147c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20157c478bd9Sstevel@tonic-gate 		case R_ITEM_STRING_INDEX:
20167c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20177c478bd9Sstevel@tonic-gate 		case R_ITEM_STRING_MIN:
20187c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20197c478bd9Sstevel@tonic-gate 		case R_ITEM_STRING_MAX:
20207c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
20217c478bd9Sstevel@tonic-gate 		case R_ITEM_SET_DELIMITER:
20227c478bd9Sstevel@tonic-gate 			hidparser_add_attribute(scan_ifp);
20237c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
2024112116d8Sfb209375 			    "hidparser_LocalItem:index = 0x%lx token = 0x%x",
20257c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_index -
20267c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_leng - 1,
20277c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_token);
20287c478bd9Sstevel@tonic-gate 			hidparser_scan(scan_ifp);
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 			return (HIDPARSER_SUCCESS);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
20337c478bd9Sstevel@tonic-gate 		default:
20347c478bd9Sstevel@tonic-gate 			break;
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	return (HIDPARSER_FAILURE);
20387c478bd9Sstevel@tonic-gate }
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate /*
20427c478bd9Sstevel@tonic-gate  * hidparser_allocate_entity:
20437c478bd9Sstevel@tonic-gate  *	Allocate Item of type 'type', length 'leng' and
20447c478bd9Sstevel@tonic-gate  *	params 'text'. Fill in the attributes allocated
20457c478bd9Sstevel@tonic-gate  *	so far from both the local and global item lists.
20467c478bd9Sstevel@tonic-gate  *	Make the child and sibling of the item NULL.
20477c478bd9Sstevel@tonic-gate  */
20487c478bd9Sstevel@tonic-gate static entity_item_t *
hidparser_allocate_entity(hidparser_tok_t * scan_ifp)20497c478bd9Sstevel@tonic-gate hidparser_allocate_entity(hidparser_tok_t *scan_ifp)
20507c478bd9Sstevel@tonic-gate {
20517c478bd9Sstevel@tonic-gate 	entity_item_t *entity;
20527c478bd9Sstevel@tonic-gate 	entity_attribute_t *aend;
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	int	entity_type = scan_ifp->hidparser_tok_token;
20557c478bd9Sstevel@tonic-gate 	unsigned char	*text = scan_ifp->hidparser_tok_text;
20567c478bd9Sstevel@tonic-gate 	int	len = scan_ifp->hidparser_tok_leng;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	entity = kmem_zalloc(sizeof (entity_item_t), KM_SLEEP);
20597c478bd9Sstevel@tonic-gate 	entity->entity_item_type = entity_type;
20607c478bd9Sstevel@tonic-gate 	entity->entity_item_params_leng = len;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	if (len != 0) {
20637c478bd9Sstevel@tonic-gate 		entity->entity_item_params = kmem_zalloc(len, KM_SLEEP);
20647c478bd9Sstevel@tonic-gate 		(void) bcopy(text, entity->entity_item_params, len);
20657c478bd9Sstevel@tonic-gate 	}
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	/*
20687c478bd9Sstevel@tonic-gate 	 * Copy attributes from entity attribute state table if not
20697c478bd9Sstevel@tonic-gate 	 * end collection.
20707c478bd9Sstevel@tonic-gate 	 */
20717c478bd9Sstevel@tonic-gate 	if (entity_type != R_ITEM_END_COLLECTION) {
20727c478bd9Sstevel@tonic-gate 		entity->entity_item_attributes = hidparser_cp_attribute_list(
20737c478bd9Sstevel@tonic-gate 		    scan_ifp->hidparser_tok_gitem_head);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 		/*
20767c478bd9Sstevel@tonic-gate 		 * append the control attributes, then clear out the control
20777c478bd9Sstevel@tonic-gate 		 * attribute state table list
20787c478bd9Sstevel@tonic-gate 		 */
20797c478bd9Sstevel@tonic-gate 		if (entity->entity_item_attributes) {
20807c478bd9Sstevel@tonic-gate 			aend = hidparser_find_attribute_end(
20817c478bd9Sstevel@tonic-gate 			    entity->entity_item_attributes);
20827c478bd9Sstevel@tonic-gate 			aend->entity_attribute_next =
20837c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_litem_head;
20847c478bd9Sstevel@tonic-gate 			scan_ifp->hidparser_tok_litem_head = NULL;
20857c478bd9Sstevel@tonic-gate 		} else {
20867c478bd9Sstevel@tonic-gate 			entity->entity_item_attributes =
20877c478bd9Sstevel@tonic-gate 			    scan_ifp->hidparser_tok_litem_head;
20887c478bd9Sstevel@tonic-gate 			scan_ifp->hidparser_tok_litem_head = NULL;
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	entity->info.child = entity->entity_item_right_sibling = 0;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	return (entity);
20957c478bd9Sstevel@tonic-gate }
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate /*
20997c478bd9Sstevel@tonic-gate  * hidparser_add_attribute:
21007c478bd9Sstevel@tonic-gate  *	Add an attribute to the global or local item list
21017c478bd9Sstevel@tonic-gate  *	If the last 4th bit from right is 1, add to the local item list
21027c478bd9Sstevel@tonic-gate  *	Else add to the global item list
21037c478bd9Sstevel@tonic-gate  */
21047c478bd9Sstevel@tonic-gate static void
hidparser_add_attribute(hidparser_tok_t * scan_ifp)21057c478bd9Sstevel@tonic-gate hidparser_add_attribute(hidparser_tok_t	*scan_ifp)
21067c478bd9Sstevel@tonic-gate {
21077c478bd9Sstevel@tonic-gate 	entity_attribute_t *newattrib, **previous, *elem;
21087c478bd9Sstevel@tonic-gate 	int	entity = scan_ifp->hidparser_tok_token;
21097c478bd9Sstevel@tonic-gate 	unsigned char	*text = scan_ifp->hidparser_tok_text;
21107c478bd9Sstevel@tonic-gate 	int	len = scan_ifp->hidparser_tok_leng;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	if (len == 0) {
21137c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
21147c478bd9Sstevel@tonic-gate 		    "hidparser_add_attribute: len = 0 for item = 0x%x",
21157c478bd9Sstevel@tonic-gate 		    entity);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 		return;
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	if (entity & HIDPARSER_ISLOCAL_MASK) {
21217c478bd9Sstevel@tonic-gate 		previous = &scan_ifp->hidparser_tok_litem_head;
21227c478bd9Sstevel@tonic-gate 	} else {
21237c478bd9Sstevel@tonic-gate 		previous = &scan_ifp->hidparser_tok_gitem_head;
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	elem = *previous;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	/*
21297c478bd9Sstevel@tonic-gate 	 * remove attribute if it is already on list, except
21307c478bd9Sstevel@tonic-gate 	 * for control attributes(local items), as we could have
21317c478bd9Sstevel@tonic-gate 	 * multiple usages...
21327c478bd9Sstevel@tonic-gate 	 * unless we want to hassle with checking for unique parameters.
21337c478bd9Sstevel@tonic-gate 	 */
21347c478bd9Sstevel@tonic-gate 	while (elem) {
21357c478bd9Sstevel@tonic-gate 		if (elem->entity_attribute_tag == entity &&
21367c478bd9Sstevel@tonic-gate 		    !(entity & HIDPARSER_ISLOCAL_MASK)) {
21377c478bd9Sstevel@tonic-gate 			*previous = elem->entity_attribute_next;
21387c478bd9Sstevel@tonic-gate 			kmem_free(elem->entity_attribute_value,
21397c478bd9Sstevel@tonic-gate 			    elem->entity_attribute_length);
21407c478bd9Sstevel@tonic-gate 			kmem_free(elem, sizeof (entity_attribute_t));
21417c478bd9Sstevel@tonic-gate 			elem = *previous;
21427c478bd9Sstevel@tonic-gate 		} else {
21437c478bd9Sstevel@tonic-gate 			previous = &elem->entity_attribute_next;
21447c478bd9Sstevel@tonic-gate 			elem = elem->entity_attribute_next;
21457c478bd9Sstevel@tonic-gate 		}
21467c478bd9Sstevel@tonic-gate 	}
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	/* create new attribute for this entry */
21497c478bd9Sstevel@tonic-gate 	newattrib = hidparser_alloc_attrib_list(1);
21507c478bd9Sstevel@tonic-gate 	newattrib->entity_attribute_tag = entity;
21517c478bd9Sstevel@tonic-gate 	newattrib->entity_attribute_value = kmem_zalloc(len, KM_SLEEP);
21527c478bd9Sstevel@tonic-gate 	(void) bcopy(text, newattrib->entity_attribute_value, len);
21537c478bd9Sstevel@tonic-gate 	newattrib->entity_attribute_length = len;
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	/* attach to end of list */
21567c478bd9Sstevel@tonic-gate 	*previous = newattrib;
21577c478bd9Sstevel@tonic-gate }
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate /*
21617c478bd9Sstevel@tonic-gate  * hidparser_alloc_attrib_list:
21627c478bd9Sstevel@tonic-gate  *	Allocate space for n attributes , create a linked list and
21637c478bd9Sstevel@tonic-gate  *	return the head
21647c478bd9Sstevel@tonic-gate  */
21657c478bd9Sstevel@tonic-gate static entity_attribute_t *
hidparser_alloc_attrib_list(int count)21667c478bd9Sstevel@tonic-gate hidparser_alloc_attrib_list(int count)
21677c478bd9Sstevel@tonic-gate {
21687c478bd9Sstevel@tonic-gate 	entity_attribute_t *head, *current;
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 	if (count <= 0) {
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 		return (NULL);
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	head = kmem_zalloc(sizeof (entity_attribute_t), KM_SLEEP);
21767c478bd9Sstevel@tonic-gate 	count--;
21777c478bd9Sstevel@tonic-gate 	current = head;
21787c478bd9Sstevel@tonic-gate 	while (count--) {
21797c478bd9Sstevel@tonic-gate 		current->entity_attribute_next = kmem_zalloc(
21807c478bd9Sstevel@tonic-gate 		    sizeof (entity_attribute_t), KM_SLEEP);
21817c478bd9Sstevel@tonic-gate 		current = current->entity_attribute_next;
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 	current->entity_attribute_next = NULL;
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	return (head);
21867c478bd9Sstevel@tonic-gate }
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate /*
21907c478bd9Sstevel@tonic-gate  * hidparser_cp_attribute_list:
21917c478bd9Sstevel@tonic-gate  *	Copies the Global item list pointed to by head
21927c478bd9Sstevel@tonic-gate  *	We create a clone of the global item list here
21937c478bd9Sstevel@tonic-gate  *	because we want to retain the Global items to
21947c478bd9Sstevel@tonic-gate  *	the next Main Item.
21957c478bd9Sstevel@tonic-gate  */
21967c478bd9Sstevel@tonic-gate static entity_attribute_t *
hidparser_cp_attribute_list(entity_attribute_t * head)21977c478bd9Sstevel@tonic-gate hidparser_cp_attribute_list(entity_attribute_t *head)
21987c478bd9Sstevel@tonic-gate {
21997c478bd9Sstevel@tonic-gate 	entity_attribute_t *return_value, *current_src, *current_dst;
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	if (!head) {
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 		return (NULL);
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	current_src = head;
22077c478bd9Sstevel@tonic-gate 	current_dst = return_value = hidparser_alloc_attrib_list(1);
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 	while (current_src) {
22107c478bd9Sstevel@tonic-gate 		current_dst->entity_attribute_tag =
22117c478bd9Sstevel@tonic-gate 		    current_src->entity_attribute_tag;
22127c478bd9Sstevel@tonic-gate 		current_dst->entity_attribute_length =
22137c478bd9Sstevel@tonic-gate 		    current_src->entity_attribute_length;
22147c478bd9Sstevel@tonic-gate 		current_dst->entity_attribute_value = kmem_zalloc(
22157c478bd9Sstevel@tonic-gate 		    current_dst->entity_attribute_length, KM_SLEEP);
22167c478bd9Sstevel@tonic-gate 		(void) bcopy(current_src->entity_attribute_value,
22177c478bd9Sstevel@tonic-gate 		    current_dst->entity_attribute_value,
22187c478bd9Sstevel@tonic-gate 		    current_src->entity_attribute_length);
22197c478bd9Sstevel@tonic-gate 		if (current_src->entity_attribute_next) {
22207c478bd9Sstevel@tonic-gate 			current_dst->entity_attribute_next =
22217c478bd9Sstevel@tonic-gate 			    hidparser_alloc_attrib_list(1);
22227c478bd9Sstevel@tonic-gate 		} else {
22237c478bd9Sstevel@tonic-gate 			current_dst->entity_attribute_next = NULL;
22247c478bd9Sstevel@tonic-gate 		}
22257c478bd9Sstevel@tonic-gate 		current_src = current_src->entity_attribute_next;
22267c478bd9Sstevel@tonic-gate 		current_dst = current_dst->entity_attribute_next;
22277c478bd9Sstevel@tonic-gate 	}
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	return (return_value);
22307c478bd9Sstevel@tonic-gate }
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate /*
22347c478bd9Sstevel@tonic-gate  * hidparser_find_attribute_end:
22357c478bd9Sstevel@tonic-gate  *	Find the last item in the attribute list pointed to by head
22367c478bd9Sstevel@tonic-gate  */
22377c478bd9Sstevel@tonic-gate static entity_attribute_t *
hidparser_find_attribute_end(entity_attribute_t * head)22387c478bd9Sstevel@tonic-gate hidparser_find_attribute_end(entity_attribute_t *head)
22397c478bd9Sstevel@tonic-gate {
22407c478bd9Sstevel@tonic-gate 	if (head == NULL) {
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 		return (NULL);
22437c478bd9Sstevel@tonic-gate 	}
22447c478bd9Sstevel@tonic-gate 	while (head->entity_attribute_next != NULL) {
22457c478bd9Sstevel@tonic-gate 		head = head->entity_attribute_next;
22467c478bd9Sstevel@tonic-gate 	}
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	return (head);
22497c478bd9Sstevel@tonic-gate }
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate /*
22537c478bd9Sstevel@tonic-gate  * hidparser_free_report_descr_handle:
22547c478bd9Sstevel@tonic-gate  *	Free the parse tree pointed to by handle
22557c478bd9Sstevel@tonic-gate  */
22567c478bd9Sstevel@tonic-gate static void
hidparser_free_report_descr_handle(entity_item_t * handle)22577c478bd9Sstevel@tonic-gate hidparser_free_report_descr_handle(entity_item_t *handle)
22587c478bd9Sstevel@tonic-gate {
22597c478bd9Sstevel@tonic-gate 	entity_item_t *next, *current, *child;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 	current = handle;
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 	while (current) {
22647c478bd9Sstevel@tonic-gate 		child = current->info.child;
22657c478bd9Sstevel@tonic-gate 		next = current->entity_item_right_sibling;
22667c478bd9Sstevel@tonic-gate 		if (current->entity_item_type == R_ITEM_COLLECTION) {
22677c478bd9Sstevel@tonic-gate 			if (current->entity_item_params != NULL)
22687c478bd9Sstevel@tonic-gate 				kmem_free(current->entity_item_params,
22697c478bd9Sstevel@tonic-gate 				    current->entity_item_params_leng);
22707c478bd9Sstevel@tonic-gate 			if (current->entity_item_attributes != NULL)
22717c478bd9Sstevel@tonic-gate 				hidparser_free_attribute_list(
22727c478bd9Sstevel@tonic-gate 				    current->entity_item_attributes);
22737c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
22747c478bd9Sstevel@tonic-gate 			    "FREE 1: %s",
22757c478bd9Sstevel@tonic-gate 			    items[current->entity_item_type]);
22767c478bd9Sstevel@tonic-gate 			kmem_free(current, sizeof (entity_item_t));
22777c478bd9Sstevel@tonic-gate 			(void) hidparser_free_report_descr_handle(child);
22787c478bd9Sstevel@tonic-gate 		} else {
22797c478bd9Sstevel@tonic-gate 			if (current->entity_item_params != NULL) {
22807c478bd9Sstevel@tonic-gate 				kmem_free(current->entity_item_params,
22817c478bd9Sstevel@tonic-gate 				    current->entity_item_params_leng);
22827c478bd9Sstevel@tonic-gate 			}
22837c478bd9Sstevel@tonic-gate 			if (current->entity_item_attributes != NULL) {
22847c478bd9Sstevel@tonic-gate 				hidparser_free_attribute_list(
22857c478bd9Sstevel@tonic-gate 				    current->entity_item_attributes);
22867c478bd9Sstevel@tonic-gate 			}
22877c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL,
22887c478bd9Sstevel@tonic-gate 			    hparser_log_handle, "FREE 2: %s",
22897c478bd9Sstevel@tonic-gate 			    items[current->entity_item_type]);
22907c478bd9Sstevel@tonic-gate 			kmem_free(current, sizeof (entity_item_t));
22917c478bd9Sstevel@tonic-gate 		}
22927c478bd9Sstevel@tonic-gate 		current = next;
22937c478bd9Sstevel@tonic-gate 	}
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate }
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate /*
22997c478bd9Sstevel@tonic-gate  * hidparser_free_attribute_list:
23007c478bd9Sstevel@tonic-gate  *	Free the attribute list pointed to by head
23017c478bd9Sstevel@tonic-gate  */
23027c478bd9Sstevel@tonic-gate static void
hidparser_free_attribute_list(entity_attribute_t * head)23037c478bd9Sstevel@tonic-gate hidparser_free_attribute_list(entity_attribute_t *head)
23047c478bd9Sstevel@tonic-gate {
23057c478bd9Sstevel@tonic-gate 	entity_attribute_t *next, *current;
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	current = head;
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	while (current) {
23107c478bd9Sstevel@tonic-gate 		next = current->entity_attribute_next;
23117c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
23127c478bd9Sstevel@tonic-gate 		    hparser_log_handle, "FREE: %s value_length = %d",
23137c478bd9Sstevel@tonic-gate 		    items[current->entity_attribute_tag],
23147c478bd9Sstevel@tonic-gate 		    current->entity_attribute_length);
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 		if (current->entity_attribute_value != NULL) {
23177c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL,
23187c478bd9Sstevel@tonic-gate 			    hparser_log_handle,
23197c478bd9Sstevel@tonic-gate 			    "\tvalue = 0x%x",
23207c478bd9Sstevel@tonic-gate 			    current->entity_attribute_value[0]);
23217c478bd9Sstevel@tonic-gate 			kmem_free(current->entity_attribute_value,
23227c478bd9Sstevel@tonic-gate 			    current->entity_attribute_length);
23237c478bd9Sstevel@tonic-gate 		}
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 		kmem_free(current, sizeof (entity_attribute_t));
23267c478bd9Sstevel@tonic-gate 		current = next;
23277c478bd9Sstevel@tonic-gate 	}
23287c478bd9Sstevel@tonic-gate }
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate /*
23327c478bd9Sstevel@tonic-gate  * hidparser_initialize_items:
23337c478bd9Sstevel@tonic-gate  *	Initialize items array before start scanning and parsing.
23347c478bd9Sstevel@tonic-gate  *	This array of strings are used for printing purpose.
23357c478bd9Sstevel@tonic-gate  */
23367c478bd9Sstevel@tonic-gate static void
hidparser_initialize_items(void)23377c478bd9Sstevel@tonic-gate hidparser_initialize_items(void)
23387c478bd9Sstevel@tonic-gate {
23397c478bd9Sstevel@tonic-gate 	items[R_ITEM_USAGE] = "Usage";
23407c478bd9Sstevel@tonic-gate 	items[R_ITEM_USAGE_MIN] = "Usage Minimum";
23417c478bd9Sstevel@tonic-gate 	items[R_ITEM_USAGE_MAX] = "Usage Maximum";
23427c478bd9Sstevel@tonic-gate 	items[R_ITEM_DESIGNATOR_INDEX] = "Designator Index";
23437c478bd9Sstevel@tonic-gate 	items[R_ITEM_DESIGNATOR_MIN] = "Designator Minimum";
23447c478bd9Sstevel@tonic-gate 	items[R_ITEM_DESIGNATOR_MAX] = "Designator Maximum";
23457c478bd9Sstevel@tonic-gate 	items[R_ITEM_STRING_INDEX] = "String Index";
23467c478bd9Sstevel@tonic-gate 	items[R_ITEM_STRING_MIN] = "String Minimum";
23477c478bd9Sstevel@tonic-gate 	items[R_ITEM_STRING_MAX] = "String Maximum";
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	items[R_ITEM_USAGE_PAGE] = "Usage Page";
23517c478bd9Sstevel@tonic-gate 	items[R_ITEM_LOGICAL_MINIMUM] = "Logical Minimum";
23527c478bd9Sstevel@tonic-gate 	items[R_ITEM_LOGICAL_MAXIMUM] = "Logical Maximum";
23537c478bd9Sstevel@tonic-gate 	items[R_ITEM_PHYSICAL_MINIMUM] = "Physical Minimum";
23547c478bd9Sstevel@tonic-gate 	items[R_ITEM_PHYSICAL_MAXIMUM] = "Physical Maximum";
23557c478bd9Sstevel@tonic-gate 	items[R_ITEM_EXPONENT] = "Exponent";
23567c478bd9Sstevel@tonic-gate 	items[R_ITEM_UNIT] = "Unit";
23577c478bd9Sstevel@tonic-gate 	items[R_ITEM_REPORT_SIZE] = "Report Size";
23587c478bd9Sstevel@tonic-gate 	items[R_ITEM_REPORT_ID] = "Report Id";
23597c478bd9Sstevel@tonic-gate 	items[R_ITEM_REPORT_COUNT] = "Report Count";
23607c478bd9Sstevel@tonic-gate 	items[R_ITEM_PUSH] = "Push";
23617c478bd9Sstevel@tonic-gate 	items[R_ITEM_POP] = "Pop";
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	items[R_ITEM_INPUT] = "Input";
23657c478bd9Sstevel@tonic-gate 	items[R_ITEM_OUTPUT] = "Output";
23667c478bd9Sstevel@tonic-gate 	items[R_ITEM_COLLECTION] = "Collection";
23677c478bd9Sstevel@tonic-gate 	items[R_ITEM_FEATURE] = "Feature";
23687c478bd9Sstevel@tonic-gate 	items[R_ITEM_END_COLLECTION] = "End Collection";
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 	items[R_ITEM_SET_DELIMITER] = "Delimiter";
23717c478bd9Sstevel@tonic-gate }
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate /*
23757c478bd9Sstevel@tonic-gate  * hidparser_scan:
23767c478bd9Sstevel@tonic-gate  *	This function scans the input entity descriptor, sees the data
23777c478bd9Sstevel@tonic-gate  *	length, returns the next token, data bytes and length in the
23787c478bd9Sstevel@tonic-gate  *	scan_ifp structure.
23797c478bd9Sstevel@tonic-gate  */
23807c478bd9Sstevel@tonic-gate static void
hidparser_scan(hidparser_tok_t * scan_ifp)23817c478bd9Sstevel@tonic-gate hidparser_scan(hidparser_tok_t	*scan_ifp)
23827c478bd9Sstevel@tonic-gate {
23837c478bd9Sstevel@tonic-gate 	int count;
23847c478bd9Sstevel@tonic-gate 	int ch;
23857c478bd9Sstevel@tonic-gate 	int parsed_length;
23867c478bd9Sstevel@tonic-gate 	unsigned char *parsed_text;
23877c478bd9Sstevel@tonic-gate 	unsigned char *entity_descriptor;
23887c478bd9Sstevel@tonic-gate 	char err_str[32];
23897c478bd9Sstevel@tonic-gate 	size_t	entity_buffer_size, index;
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	index = scan_ifp->hidparser_tok_index;
23927c478bd9Sstevel@tonic-gate 	entity_buffer_size = scan_ifp->hidparser_tok_max_bsize;
23937c478bd9Sstevel@tonic-gate 	parsed_length = 0;
23947c478bd9Sstevel@tonic-gate 	parsed_text = scan_ifp->hidparser_tok_text;
23957c478bd9Sstevel@tonic-gate 	entity_descriptor = scan_ifp->hidparser_tok_entity_descriptor;
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate next_item:
23987c478bd9Sstevel@tonic-gate 	if (index <= entity_buffer_size -1) {
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 		ch = 0xFF & entity_descriptor[index];
24017c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24027c478bd9Sstevel@tonic-gate 		    hparser_log_handle, "scanner: index  = 0x%lx ch = 0x%x",
24037c478bd9Sstevel@tonic-gate 		    index, ch);
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 		index++;
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 		/*
24087c478bd9Sstevel@tonic-gate 		 * Error checking:
24097c478bd9Sstevel@tonic-gate 		 * Unrecognized items should be passed over
24107c478bd9Sstevel@tonic-gate 		 * by the parser.
24117c478bd9Sstevel@tonic-gate 		 * Section 5.4
24127c478bd9Sstevel@tonic-gate 		 */
24137c478bd9Sstevel@tonic-gate 		if (!(hidparser_isvalid_item(ch))) {
24147c478bd9Sstevel@tonic-gate 			(void) sprintf(err_str, "%s: 0x%2x",
24157c478bd9Sstevel@tonic-gate 			    "Unknown or reserved item", ch);
24167c478bd9Sstevel@tonic-gate 			hidparser_report_err(HIDPARSER_ERR_ERROR,
24177c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD, 0, 0x3F, err_str);
24187c478bd9Sstevel@tonic-gate 			goto next_item;
24197c478bd9Sstevel@tonic-gate 		}
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 		if (ch == EXTENDED_ITEM) {
24227c478bd9Sstevel@tonic-gate 			parsed_length = entity_descriptor[index++];
24237c478bd9Sstevel@tonic-gate 			ch = entity_descriptor[index++];
24247c478bd9Sstevel@tonic-gate 			hidparser_report_err(HIDPARSER_ERR_WARN,
24257c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
24267c478bd9Sstevel@tonic-gate 			    0,
24277c478bd9Sstevel@tonic-gate 			    0x3E,
24287c478bd9Sstevel@tonic-gate 			    "Long item defined");
24297c478bd9Sstevel@tonic-gate 		} else {
24307c478bd9Sstevel@tonic-gate 			parsed_length = ch & 0x03;
24317c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL,
24327c478bd9Sstevel@tonic-gate 			    hparser_log_handle,
24337c478bd9Sstevel@tonic-gate 			    "scanner: parsed_length = %x", parsed_length);
24347c478bd9Sstevel@tonic-gate 			/* 3 really means 4.. see p.21 HID */
24357c478bd9Sstevel@tonic-gate 			if (parsed_length == 3)
24367c478bd9Sstevel@tonic-gate 				parsed_length++;
24377c478bd9Sstevel@tonic-gate 		}
24387c478bd9Sstevel@tonic-gate 		for (count = 0; count < parsed_length; count++) {
24397c478bd9Sstevel@tonic-gate 			parsed_text[count] = entity_descriptor[index];
24407c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
24417c478bd9Sstevel@tonic-gate 			    "scanner: parsed_text[%d] = 0x%x,"
24427c478bd9Sstevel@tonic-gate 			    "index = 0x%lx",
24437c478bd9Sstevel@tonic-gate 			    count, parsed_text[count], index);
24447c478bd9Sstevel@tonic-gate 			index++;
24457c478bd9Sstevel@tonic-gate 		}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24487c478bd9Sstevel@tonic-gate 		    hparser_log_handle, "scanner: lexical analyzer found 0x%x "
24497c478bd9Sstevel@tonic-gate 		    "before translation", ch);
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 		scan_ifp->hidparser_tok_index = index;
24527c478bd9Sstevel@tonic-gate 		scan_ifp->hidparser_tok_leng = parsed_length;
24537c478bd9Sstevel@tonic-gate 		scan_ifp->hidparser_tok_token = ch & 0xFC;
24547c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24557c478bd9Sstevel@tonic-gate 		    hparser_log_handle, "scanner: aindex  = 0x%lx", index);
24567c478bd9Sstevel@tonic-gate 	} else {
24577c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L4(PRINT_MASK_ALL,
24587c478bd9Sstevel@tonic-gate 		    hparser_log_handle, "scanner: eindex  = 0x%lx", index);
24597c478bd9Sstevel@tonic-gate 		scan_ifp->hidparser_tok_leng = 0;
24607c478bd9Sstevel@tonic-gate 		scan_ifp->hidparser_tok_token = 0;	/* EOF */
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate }
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate /*
24667c478bd9Sstevel@tonic-gate  * hidparser_report_err:
24677c478bd9Sstevel@tonic-gate  *	Construct and print the error code
24687c478bd9Sstevel@tonic-gate  *	Ref: Hidview error check list
24697c478bd9Sstevel@tonic-gate  */
24707c478bd9Sstevel@tonic-gate static void
hidparser_report_err(int err_level,int err_type,int tag,int subcode,char * msg)2471*239936d2SToomas Soome hidparser_report_err(int err_level, int err_type, int tag, int subcode,
24727c478bd9Sstevel@tonic-gate     char *msg)
24737c478bd9Sstevel@tonic-gate {
24747c478bd9Sstevel@tonic-gate 	unsigned int	BmParserErrorCode = 0;
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	if (err_level) {
24777c478bd9Sstevel@tonic-gate 		BmParserErrorCode |= HIDPARSER_ERR_ERROR;
24787c478bd9Sstevel@tonic-gate 	}
24797c478bd9Sstevel@tonic-gate 	if (err_type) {
24807c478bd9Sstevel@tonic-gate 		BmParserErrorCode |= HIDPARSER_ERR_STANDARD;
24817c478bd9Sstevel@tonic-gate 	}
24827c478bd9Sstevel@tonic-gate 	BmParserErrorCode |= (tag << 8) & HIDPARSER_ERR_TAG_MASK;
24837c478bd9Sstevel@tonic-gate 	BmParserErrorCode |= subcode & HIDPARSER_ERR_SUBCODE_MASK;
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 	if (err_level) {
24867c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
24877c478bd9Sstevel@tonic-gate 		    "err code = 0x%4x, err str = %s",
24887c478bd9Sstevel@tonic-gate 		    BmParserErrorCode, msg);
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	} else {
24917c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
24927c478bd9Sstevel@tonic-gate 		    "wrn code = 0x%4x, wrn str = %s",
24937c478bd9Sstevel@tonic-gate 		    BmParserErrorCode, msg);
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate /*
24997c478bd9Sstevel@tonic-gate  * hidparser_isvalid_item:
25007c478bd9Sstevel@tonic-gate  *	Find if the item tag is a valid one
25017c478bd9Sstevel@tonic-gate  */
25027c478bd9Sstevel@tonic-gate static int
hidparser_isvalid_item(int tag)25037c478bd9Sstevel@tonic-gate hidparser_isvalid_item(int tag)
25047c478bd9Sstevel@tonic-gate {
25057c478bd9Sstevel@tonic-gate 	if (tag == EXTENDED_ITEM) {
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 		return (1);
25087c478bd9Sstevel@tonic-gate 	}
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	tag &= 0xFC;
25117c478bd9Sstevel@tonic-gate 	if ((tag == R_ITEM_INPUT) ||
25127c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_OUTPUT) ||
25137c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_COLLECTION) ||
25147c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_FEATURE) ||
25157c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_END_COLLECTION) ||
25167c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE_PAGE) ||
25177c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_LOGICAL_MINIMUM) ||
25187c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_LOGICAL_MAXIMUM) ||
25197c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_PHYSICAL_MINIMUM) ||
25207c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_PHYSICAL_MAXIMUM) ||
25217c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_EXPONENT) ||
25227c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_UNIT) ||
25237c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_REPORT_SIZE) ||
25247c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_REPORT_ID) ||
25257c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_REPORT_COUNT) ||
25267c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_PUSH) ||
25277c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_POP) ||
25287c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE) ||
25297c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE_MIN) ||
25307c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_USAGE_MAX) ||
25317c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_DESIGNATOR_INDEX) ||
25327c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_DESIGNATOR_MIN) ||
25337c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_DESIGNATOR_MAX) ||
25347c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_STRING_INDEX) ||
25357c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_STRING_MIN) ||
25367c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_STRING_MAX) ||
25377c478bd9Sstevel@tonic-gate 	    (tag == R_ITEM_SET_DELIMITER)) {
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 		return (1);
25407c478bd9Sstevel@tonic-gate 	} else {
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 		return (0);
25437c478bd9Sstevel@tonic-gate 	}
25447c478bd9Sstevel@tonic-gate }
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate /*
25487c478bd9Sstevel@tonic-gate  * hidparser_lookup_attribute:
25497c478bd9Sstevel@tonic-gate  *	Takes an item pointer(report structure) and a tag(e.g Logical
25507c478bd9Sstevel@tonic-gate  *	Min) as input. Returns the corresponding attribute structure.
25517c478bd9Sstevel@tonic-gate  *	Presently used for error checking only.
25527c478bd9Sstevel@tonic-gate  */
25537c478bd9Sstevel@tonic-gate static entity_attribute_t *
hidparser_lookup_attribute(entity_item_t * item,int attr_tag)25547c478bd9Sstevel@tonic-gate hidparser_lookup_attribute(entity_item_t *item, int attr_tag)
25557c478bd9Sstevel@tonic-gate {
25567c478bd9Sstevel@tonic-gate 	entity_attribute_t *temp;
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	if (item == NULL) {
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 		return (NULL);
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	temp = item->entity_item_attributes;
25647c478bd9Sstevel@tonic-gate 	while (temp != NULL) {
25657c478bd9Sstevel@tonic-gate 		if (temp->entity_attribute_tag == attr_tag) {
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 			return (temp);
25687c478bd9Sstevel@tonic-gate 		}
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 		temp = temp->entity_attribute_next;
25717c478bd9Sstevel@tonic-gate 	}
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	return (NULL);
25747c478bd9Sstevel@tonic-gate }
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate /*
25787c478bd9Sstevel@tonic-gate  * hidparser_global_err_check:
25797c478bd9Sstevel@tonic-gate  *	Error checking for Global Items that need to be
25807c478bd9Sstevel@tonic-gate  *	performed in MainItem
25817c478bd9Sstevel@tonic-gate  */
25827c478bd9Sstevel@tonic-gate static void
hidparser_global_err_check(entity_item_t * mainitem)25837c478bd9Sstevel@tonic-gate hidparser_global_err_check(entity_item_t *mainitem)
25847c478bd9Sstevel@tonic-gate {
25857c478bd9Sstevel@tonic-gate 	hidparser_check_minmax_val_signed(mainitem, R_ITEM_LOGICAL_MINIMUM,
25867c478bd9Sstevel@tonic-gate 	    R_ITEM_LOGICAL_MAXIMUM, 0, 0);
25877c478bd9Sstevel@tonic-gate 	hidparser_check_minmax_val_signed(mainitem, R_ITEM_PHYSICAL_MINIMUM,
25887c478bd9Sstevel@tonic-gate 	    R_ITEM_PHYSICAL_MAXIMUM, 0, 0);
25897c478bd9Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_PHYSICAL_MINIMUM,
25907c478bd9Sstevel@tonic-gate 	    R_ITEM_PHYSICAL_MAXIMUM, 0, 0,
25917c478bd9Sstevel@tonic-gate 	    "Must have a corresponding Physical min",
25927c478bd9Sstevel@tonic-gate 	    "Must have a corresponding Physical max");
25937c478bd9Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_PUSH, R_ITEM_POP,
25947c478bd9Sstevel@tonic-gate 	    1, 0, "Should have a corresponding Pop",
25957c478bd9Sstevel@tonic-gate 	    "Must have a corresponding Push");
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate }
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate /*
26017c478bd9Sstevel@tonic-gate  * hidparser_mainitem_err_check:
26027c478bd9Sstevel@tonic-gate  *	Error checking for Main Items
26037c478bd9Sstevel@tonic-gate  */
26047c478bd9Sstevel@tonic-gate static void
hidparser_mainitem_err_check(entity_item_t * mainitem)26057c478bd9Sstevel@tonic-gate hidparser_mainitem_err_check(entity_item_t *mainitem)
26067c478bd9Sstevel@tonic-gate {
26077c478bd9Sstevel@tonic-gate 	int	itemmask = 0;
26087c478bd9Sstevel@tonic-gate 	entity_attribute_t *attr;
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	attr = mainitem->entity_item_attributes;
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	if (attr != NULL) {
26137c478bd9Sstevel@tonic-gate 		while (attr) {
26147c478bd9Sstevel@tonic-gate 			switch (attr->entity_attribute_tag) {
26157c478bd9Sstevel@tonic-gate 				case R_ITEM_LOGICAL_MINIMUM:
26167c478bd9Sstevel@tonic-gate 					itemmask |= 0x01;
26177c478bd9Sstevel@tonic-gate 					break;
26187c478bd9Sstevel@tonic-gate 				case R_ITEM_LOGICAL_MAXIMUM:
26197c478bd9Sstevel@tonic-gate 					itemmask |= 0x02;
26207c478bd9Sstevel@tonic-gate 					break;
26217c478bd9Sstevel@tonic-gate 				case R_ITEM_REPORT_SIZE:
26227c478bd9Sstevel@tonic-gate 					itemmask |= 0x04;
26237c478bd9Sstevel@tonic-gate 					break;
26247c478bd9Sstevel@tonic-gate 				case R_ITEM_REPORT_COUNT:
26257c478bd9Sstevel@tonic-gate 					itemmask |= 0x08;
26267c478bd9Sstevel@tonic-gate 					break;
26277c478bd9Sstevel@tonic-gate 				case R_ITEM_USAGE_PAGE:
26287c478bd9Sstevel@tonic-gate 					itemmask |= 0x10;
26297c478bd9Sstevel@tonic-gate 					break;
26307c478bd9Sstevel@tonic-gate 				default:
26317c478bd9Sstevel@tonic-gate 					break;
26327c478bd9Sstevel@tonic-gate 			} /* switch */
26337c478bd9Sstevel@tonic-gate 			attr = attr->entity_attribute_next;
26347c478bd9Sstevel@tonic-gate 		} /* while */
26357c478bd9Sstevel@tonic-gate 	} /* if */
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	if ((mainitem->entity_item_type == R_ITEM_COLLECTION) ||
26387c478bd9Sstevel@tonic-gate 	    (mainitem->entity_item_type == R_ITEM_END_COLLECTION)) {
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 		return;
26417c478bd9Sstevel@tonic-gate 	}
26427c478bd9Sstevel@tonic-gate 	if (itemmask != 0x1f) {
26437c478bd9Sstevel@tonic-gate 			hidparser_report_err(
26447c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_ERROR,
26457c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
26467c478bd9Sstevel@tonic-gate 			    mainitem->entity_item_type,
26477c478bd9Sstevel@tonic-gate 			    0,
26487c478bd9Sstevel@tonic-gate 			    "Required Global/Local items must be defined");
26497c478bd9Sstevel@tonic-gate 	}
26507c478bd9Sstevel@tonic-gate }
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate /*
26547c478bd9Sstevel@tonic-gate  * hidparser_local_err_check:
26557c478bd9Sstevel@tonic-gate  *	Error checking for Local items that is done when a MainItem
26567c478bd9Sstevel@tonic-gate  *	is encountered
26577c478bd9Sstevel@tonic-gate  */
26587c478bd9Sstevel@tonic-gate static void
hidparser_local_err_check(entity_item_t * mainitem)26597c478bd9Sstevel@tonic-gate hidparser_local_err_check(entity_item_t *mainitem)
26607c478bd9Sstevel@tonic-gate {
26617c478bd9Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_USAGE_MIN,
26627c478bd9Sstevel@tonic-gate 	    R_ITEM_USAGE_MAX, 0, 0,
26637c478bd9Sstevel@tonic-gate 	    "Must have a corresponding Usage Min",
26647c478bd9Sstevel@tonic-gate 	    "Must have a corresponding Usage Max");
26657c478bd9Sstevel@tonic-gate 	hidparser_check_minmax_val(mainitem, R_ITEM_USAGE_MIN,
26667c478bd9Sstevel@tonic-gate 	    R_ITEM_USAGE_MAX, 1, 1);
26677c478bd9Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_DESIGNATOR_MIN,
26687c478bd9Sstevel@tonic-gate 	    R_ITEM_DESIGNATOR_MAX, 0, 0,
26697c478bd9Sstevel@tonic-gate 	    "Must have a corresponding Designator min",
26707c478bd9Sstevel@tonic-gate 	    "Must have a corresponding Designator Max");
26717c478bd9Sstevel@tonic-gate 	hidparser_check_minmax_val(mainitem, R_ITEM_DESIGNATOR_MIN,
26727c478bd9Sstevel@tonic-gate 	    R_ITEM_DESIGNATOR_MAX, 1, 1);
26737c478bd9Sstevel@tonic-gate 	hidparser_check_correspondence(mainitem, R_ITEM_STRING_MIN,
26747c478bd9Sstevel@tonic-gate 	    R_ITEM_STRING_MAX, 0, 0,
26757c478bd9Sstevel@tonic-gate 	    "Must have a corresponding String min",
26767c478bd9Sstevel@tonic-gate 	    "Must have a corresponding String Max");
26777c478bd9Sstevel@tonic-gate 	hidparser_check_minmax_val(mainitem, R_ITEM_STRING_MIN,
26787c478bd9Sstevel@tonic-gate 	    R_ITEM_STRING_MAX, 1, 1);
26797c478bd9Sstevel@tonic-gate }
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate /*
26837c478bd9Sstevel@tonic-gate  * hidparser_find_unsigned_val:
26847c478bd9Sstevel@tonic-gate  *	Find the value for multibyte data
26857c478bd9Sstevel@tonic-gate  *	Ref: Section 5.8 of HID Spec 1.0
26867c478bd9Sstevel@tonic-gate  */
26877c478bd9Sstevel@tonic-gate static unsigned int
hidparser_find_unsigned_val(entity_attribute_t * attr)26887c478bd9Sstevel@tonic-gate hidparser_find_unsigned_val(entity_attribute_t *attr)
26897c478bd9Sstevel@tonic-gate {
26907c478bd9Sstevel@tonic-gate 	char *text;
26917c478bd9Sstevel@tonic-gate 	int len, i;
26927c478bd9Sstevel@tonic-gate 	unsigned int ret = 0;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	text = attr->entity_attribute_value;
26957c478bd9Sstevel@tonic-gate 	len = attr->entity_attribute_length;
26967c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
26977c478bd9Sstevel@tonic-gate 		ret |= ((text[i] & 0xff) << (8*i));
26987c478bd9Sstevel@tonic-gate 	}
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	return (ret);
27017c478bd9Sstevel@tonic-gate }
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate /*
27057c478bd9Sstevel@tonic-gate  * hidparser_find_signed_val:
27067c478bd9Sstevel@tonic-gate  *	Find the value for signed multibyte data
27077c478bd9Sstevel@tonic-gate  *	Ref: Section 5.8 of HID Spec 1.0
27087c478bd9Sstevel@tonic-gate  */
27097c478bd9Sstevel@tonic-gate static signed int
hidparser_find_signed_val(entity_attribute_t * attr)27107c478bd9Sstevel@tonic-gate hidparser_find_signed_val(entity_attribute_t *attr)
27117c478bd9Sstevel@tonic-gate {
27127c478bd9Sstevel@tonic-gate 	char *text;
27137c478bd9Sstevel@tonic-gate 	int len, i;
27147c478bd9Sstevel@tonic-gate 	int ret = 0;
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 	text = attr->entity_attribute_value;
27177c478bd9Sstevel@tonic-gate 	len = attr->entity_attribute_length;
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	for (i = 0; i < len - 1; i++) {
27207c478bd9Sstevel@tonic-gate 		ret |= ((text[i] & 0xff) << (8 * i));
27217c478bd9Sstevel@tonic-gate 	}
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	if (len > 0) {
27247c478bd9Sstevel@tonic-gate 		ret |= (text[i] << (8 * i));
27257c478bd9Sstevel@tonic-gate 	}
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 	return (ret);
27287c478bd9Sstevel@tonic-gate }
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate /*
27327c478bd9Sstevel@tonic-gate  * hidparser_check_correspondence:
27337c478bd9Sstevel@tonic-gate  *	Check if the item item2 corresponding to item1 exists and vice versa
27347c478bd9Sstevel@tonic-gate  *	If not report the appropriate error
27357c478bd9Sstevel@tonic-gate  */
27367c478bd9Sstevel@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)2737*239936d2SToomas Soome hidparser_check_correspondence(entity_item_t *mainitem, int item_tag1,
2738*239936d2SToomas Soome     int item_tag2, int val1, int val2, char *str1, char *str2)
27397c478bd9Sstevel@tonic-gate {
27407c478bd9Sstevel@tonic-gate 	entity_attribute_t *temp1, *temp2;
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	temp1 = hidparser_lookup_attribute(mainitem, item_tag1);
27437c478bd9Sstevel@tonic-gate 	temp2 = hidparser_lookup_attribute(mainitem, item_tag2);
27447c478bd9Sstevel@tonic-gate 	if ((temp1 != NULL) && (temp2 == NULL)) {
27457c478bd9Sstevel@tonic-gate 		hidparser_report_err(
27467c478bd9Sstevel@tonic-gate 		    HIDPARSER_ERR_ERROR,
27477c478bd9Sstevel@tonic-gate 		    HIDPARSER_ERR_STANDARD,
27487c478bd9Sstevel@tonic-gate 		    item_tag1,
27497c478bd9Sstevel@tonic-gate 		    val1,
27507c478bd9Sstevel@tonic-gate 		    str1);
27517c478bd9Sstevel@tonic-gate 	}
27527c478bd9Sstevel@tonic-gate 	if ((temp2 != NULL) && (temp1 == NULL)) {
27537c478bd9Sstevel@tonic-gate 		hidparser_report_err(
27547c478bd9Sstevel@tonic-gate 		    HIDPARSER_ERR_ERROR,
27557c478bd9Sstevel@tonic-gate 		    HIDPARSER_ERR_STANDARD,
27567c478bd9Sstevel@tonic-gate 		    item_tag2,
27577c478bd9Sstevel@tonic-gate 		    val2,
27587c478bd9Sstevel@tonic-gate 		    str2);
27597c478bd9Sstevel@tonic-gate 	}
27607c478bd9Sstevel@tonic-gate }
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate /*
27647c478bd9Sstevel@tonic-gate  * hidparser_check_minmax_val:
27657c478bd9Sstevel@tonic-gate  *	Check if the Min value <= Max and vice versa
27667c478bd9Sstevel@tonic-gate  *	Print for warnings and errors have been taken care separately.
27677c478bd9Sstevel@tonic-gate  */
27687c478bd9Sstevel@tonic-gate static void
hidparser_check_minmax_val(entity_item_t * mainitem,int item_tag1,int item_tag2,int val1,int val2)2769*239936d2SToomas Soome hidparser_check_minmax_val(entity_item_t *mainitem, int item_tag1,
2770*239936d2SToomas Soome     int item_tag2, int val1, int val2)
27717c478bd9Sstevel@tonic-gate {
27727c478bd9Sstevel@tonic-gate 	entity_attribute_t *temp1, *temp2;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	temp1 = hidparser_lookup_attribute(mainitem, item_tag1);
27757c478bd9Sstevel@tonic-gate 	temp2 = hidparser_lookup_attribute(mainitem, item_tag2);
27767c478bd9Sstevel@tonic-gate 	if ((temp1 != NULL) && (temp2 != NULL)) {
27777c478bd9Sstevel@tonic-gate 		if (hidparser_find_unsigned_val(temp1) >
27787c478bd9Sstevel@tonic-gate 		    hidparser_find_unsigned_val(temp2)) {
27797c478bd9Sstevel@tonic-gate 			if ((item_tag1 == R_ITEM_LOGICAL_MINIMUM) ||
27807c478bd9Sstevel@tonic-gate 			    (item_tag1 == R_ITEM_PHYSICAL_MINIMUM)) {
27817c478bd9Sstevel@tonic-gate 				hidparser_report_err(
27827c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_WARN,
27837c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
27847c478bd9Sstevel@tonic-gate 				    item_tag1,
27857c478bd9Sstevel@tonic-gate 				    val1,
27867c478bd9Sstevel@tonic-gate 				    "unsigned: Min should be <= to Max");
27877c478bd9Sstevel@tonic-gate 			} else {
27887c478bd9Sstevel@tonic-gate 				hidparser_report_err(
27897c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
27907c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
27917c478bd9Sstevel@tonic-gate 				    item_tag1,
27927c478bd9Sstevel@tonic-gate 				    val1,
27937c478bd9Sstevel@tonic-gate 				    "Min must be <= to Max");
27947c478bd9Sstevel@tonic-gate 			}
27957c478bd9Sstevel@tonic-gate 		}
27967c478bd9Sstevel@tonic-gate 		if (hidparser_find_unsigned_val(temp2) <
27977c478bd9Sstevel@tonic-gate 		    hidparser_find_unsigned_val(temp1)) {
27987c478bd9Sstevel@tonic-gate 			if ((item_tag2 == R_ITEM_LOGICAL_MAXIMUM) ||
27997c478bd9Sstevel@tonic-gate 			    (item_tag2 == R_ITEM_PHYSICAL_MAXIMUM)) {
28007c478bd9Sstevel@tonic-gate 				hidparser_report_err(
28017c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28027c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28037c478bd9Sstevel@tonic-gate 				    item_tag2,
28047c478bd9Sstevel@tonic-gate 				    val2,
28057c478bd9Sstevel@tonic-gate 				    "unsigned: Max should be >= to Min");
28067c478bd9Sstevel@tonic-gate 			} else {
28077c478bd9Sstevel@tonic-gate 				hidparser_report_err(
28087c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28097c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28107c478bd9Sstevel@tonic-gate 				    item_tag2,
28117c478bd9Sstevel@tonic-gate 				    val2,
28127c478bd9Sstevel@tonic-gate 				    "Max must be >= to Min");
28137c478bd9Sstevel@tonic-gate 			}
28147c478bd9Sstevel@tonic-gate 		}
28157c478bd9Sstevel@tonic-gate 	}	/* if (temp1 != NULL) && (temp2 != NULL) */
28167c478bd9Sstevel@tonic-gate }
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate /*
28207c478bd9Sstevel@tonic-gate  * hidparser_check_minmax_val_signed:
28217c478bd9Sstevel@tonic-gate  *	Check if the Min value <= Max and vice versa
28227c478bd9Sstevel@tonic-gate  *	Print for warnings and errors have been taken care separately.
28237c478bd9Sstevel@tonic-gate  */
28247c478bd9Sstevel@tonic-gate static void
hidparser_check_minmax_val_signed(entity_item_t * mainitem,int item_tag1,int item_tag2,int val1,int val2)2825*239936d2SToomas Soome hidparser_check_minmax_val_signed(entity_item_t *mainitem, int item_tag1,
2826*239936d2SToomas Soome     int item_tag2, int val1, int val2)
28277c478bd9Sstevel@tonic-gate {
28287c478bd9Sstevel@tonic-gate 	entity_attribute_t *temp1, *temp2;
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	temp1 = hidparser_lookup_attribute(mainitem, item_tag1);
28317c478bd9Sstevel@tonic-gate 	temp2 = hidparser_lookup_attribute(mainitem, item_tag2);
28327c478bd9Sstevel@tonic-gate 	if ((temp1 != NULL) && (temp2 != NULL)) {
28337c478bd9Sstevel@tonic-gate 		if (hidparser_find_signed_val(temp1) >
28347c478bd9Sstevel@tonic-gate 		    hidparser_find_signed_val(temp2)) {
28357c478bd9Sstevel@tonic-gate 			if ((item_tag1 == R_ITEM_LOGICAL_MINIMUM) ||
28367c478bd9Sstevel@tonic-gate 			    (item_tag1 == R_ITEM_PHYSICAL_MINIMUM)) {
28377c478bd9Sstevel@tonic-gate 				hidparser_report_err(
28387c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_WARN,
28397c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28407c478bd9Sstevel@tonic-gate 				    item_tag1,
28417c478bd9Sstevel@tonic-gate 				    val1,
28427c478bd9Sstevel@tonic-gate 				    "signed: Min should be <= to Max");
28437c478bd9Sstevel@tonic-gate 			} else {
28447c478bd9Sstevel@tonic-gate 				hidparser_report_err(
28457c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28467c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28477c478bd9Sstevel@tonic-gate 				    item_tag1,
28487c478bd9Sstevel@tonic-gate 				    val1,
28497c478bd9Sstevel@tonic-gate 				    "Min must be <= to Max");
28507c478bd9Sstevel@tonic-gate 			}
28517c478bd9Sstevel@tonic-gate 		}
28527c478bd9Sstevel@tonic-gate 		if (hidparser_find_signed_val(temp2) <
28537c478bd9Sstevel@tonic-gate 		    hidparser_find_signed_val(temp1)) {
28547c478bd9Sstevel@tonic-gate 			if ((item_tag2 == R_ITEM_LOGICAL_MAXIMUM) ||
28557c478bd9Sstevel@tonic-gate 			    (item_tag2 == R_ITEM_PHYSICAL_MAXIMUM)) {
28567c478bd9Sstevel@tonic-gate 				hidparser_report_err(
28577c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28587c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28597c478bd9Sstevel@tonic-gate 				    item_tag2,
28607c478bd9Sstevel@tonic-gate 				    val2,
28617c478bd9Sstevel@tonic-gate 				    "signed: Max should be >= to Min");
28627c478bd9Sstevel@tonic-gate 			} else {
28637c478bd9Sstevel@tonic-gate 				hidparser_report_err(
28647c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_ERROR,
28657c478bd9Sstevel@tonic-gate 				    HIDPARSER_ERR_STANDARD,
28667c478bd9Sstevel@tonic-gate 				    item_tag2,
28677c478bd9Sstevel@tonic-gate 				    val2,
28687c478bd9Sstevel@tonic-gate 				    "Max must be >= to Min");
28697c478bd9Sstevel@tonic-gate 			}
28707c478bd9Sstevel@tonic-gate 		}
28717c478bd9Sstevel@tonic-gate 	}	/* if (temp1 != NULL) && (temp2 != NULL) */
28727c478bd9Sstevel@tonic-gate }
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate /*
28767c478bd9Sstevel@tonic-gate  * hidparser_error_delim:
28777c478bd9Sstevel@tonic-gate  *	Error check for Delimiter Sets
28787c478bd9Sstevel@tonic-gate  */
28797c478bd9Sstevel@tonic-gate static void
hidparser_error_delim(entity_item_t * item,int err)28807c478bd9Sstevel@tonic-gate hidparser_error_delim(entity_item_t *item, int err)
28817c478bd9Sstevel@tonic-gate {
28827c478bd9Sstevel@tonic-gate 	entity_attribute_t *attr;
28837c478bd9Sstevel@tonic-gate 	switch (err) {
28847c478bd9Sstevel@tonic-gate 		case HIDPARSER_DELIM_ERR1:
28857c478bd9Sstevel@tonic-gate 			hidparser_report_err(
28867c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_ERROR,
28877c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
28887c478bd9Sstevel@tonic-gate 			    R_ITEM_SET_DELIMITER,
28897c478bd9Sstevel@tonic-gate 			    0,
28907c478bd9Sstevel@tonic-gate 			    "Must be Delimiter Open");
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 			break;
28937c478bd9Sstevel@tonic-gate 		case HIDPARSER_DELIM_ERR2:
28947c478bd9Sstevel@tonic-gate 			hidparser_report_err(
28957c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_ERROR,
28967c478bd9Sstevel@tonic-gate 			    HIDPARSER_ERR_STANDARD,
28977c478bd9Sstevel@tonic-gate 			    R_ITEM_SET_DELIMITER,
28987c478bd9Sstevel@tonic-gate 			    0,
28997c478bd9Sstevel@tonic-gate 			    "Must be Delimiter Close");
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 			break;
29027c478bd9Sstevel@tonic-gate 		case HIDPARSER_DELIM_ERR3:
29037c478bd9Sstevel@tonic-gate 			attr = item->entity_item_attributes;
29047c478bd9Sstevel@tonic-gate 			while (attr != NULL) {
29057c478bd9Sstevel@tonic-gate 				if ((attr->entity_attribute_tag !=
29067c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE) &&
29077c478bd9Sstevel@tonic-gate 				    (attr->entity_attribute_tag !=
29087c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE_MIN) &&
29097c478bd9Sstevel@tonic-gate 				    (attr->entity_attribute_tag !=
29107c478bd9Sstevel@tonic-gate 				    R_ITEM_USAGE_MAX)) {
29117c478bd9Sstevel@tonic-gate 					hidparser_report_err(
29127c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_ERROR,
29137c478bd9Sstevel@tonic-gate 					    HIDPARSER_ERR_STANDARD,
29147c478bd9Sstevel@tonic-gate 					    R_ITEM_SET_DELIMITER,
29157c478bd9Sstevel@tonic-gate 					    3,
29167c478bd9Sstevel@tonic-gate 					    "May only contain Usage, "
29177c478bd9Sstevel@tonic-gate 					    "Usage Min and Usage Max");
29187c478bd9Sstevel@tonic-gate 				}
29197c478bd9Sstevel@tonic-gate 				attr = attr->entity_attribute_next;
29207c478bd9Sstevel@tonic-gate 			}
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 			break;
29237c478bd9Sstevel@tonic-gate 		default:
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 			break;
29267c478bd9Sstevel@tonic-gate 	}
29277c478bd9Sstevel@tonic-gate }
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate /*
29317c478bd9Sstevel@tonic-gate  * hidparser_find_max_packet_size_from_report_descriptor:
29327c478bd9Sstevel@tonic-gate  *	find packet size of the largest report in the report descriptor
29337c478bd9Sstevel@tonic-gate  */
29347c478bd9Sstevel@tonic-gate void
hidparser_find_max_packet_size_from_report_descriptor(hidparser_handle_t hparser_handle,hidparser_packet_info_t * hpack)29357c478bd9Sstevel@tonic-gate hidparser_find_max_packet_size_from_report_descriptor(
29367c478bd9Sstevel@tonic-gate 			hidparser_handle_t hparser_handle,
29377c478bd9Sstevel@tonic-gate 			hidparser_packet_info_t *hpack)
29387c478bd9Sstevel@tonic-gate {
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	int				rval, i;
29417c478bd9Sstevel@tonic-gate 	uint_t				packet_size;
29427c478bd9Sstevel@tonic-gate 	uint_t				max_packet_size;
29437c478bd9Sstevel@tonic-gate 	uint_t				max_report_id;
29447c478bd9Sstevel@tonic-gate 	hidparser_report_id_list_t	report_id_list;
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	USB_DPRINTF_L4(PRINT_MASK_ALL, hparser_log_handle,
29477c478bd9Sstevel@tonic-gate 	    "hidparser_find_max_packet_size_from_report_descriptor");
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	/* get a list of input reports */
29507c478bd9Sstevel@tonic-gate 	rval = hidparser_get_report_id_list(hparser_handle,
29517c478bd9Sstevel@tonic-gate 	    R_ITEM_INPUT, &report_id_list);
29527c478bd9Sstevel@tonic-gate 	if (rval != HIDPARSER_SUCCESS) {
29537c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
29547c478bd9Sstevel@tonic-gate 		    "No report id used");
29557c478bd9Sstevel@tonic-gate 	} else {
29567c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
29577c478bd9Sstevel@tonic-gate 		    "%d unique report IDs found in hid report descriptor",
29587c478bd9Sstevel@tonic-gate 		    report_id_list.no_of_report_ids);
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 		for (i = 0; i < (report_id_list.no_of_report_ids); i++) {
29617c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L3(PRINT_MASK_ALL, hparser_log_handle,
29627c478bd9Sstevel@tonic-gate 			    "report_id: %d", report_id_list.report_id[i]);
29637c478bd9Sstevel@tonic-gate 		}
29647c478bd9Sstevel@tonic-gate 	}
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	if ((rval != HIDPARSER_SUCCESS) ||
29677c478bd9Sstevel@tonic-gate 	    (report_id_list.no_of_report_ids == 0)) {
29687c478bd9Sstevel@tonic-gate 		/*
29697c478bd9Sstevel@tonic-gate 		 * since no report id is used, get the packet size
29707c478bd9Sstevel@tonic-gate 		 * for the only report available
29717c478bd9Sstevel@tonic-gate 		 */
29727c478bd9Sstevel@tonic-gate 		(void) hidparser_get_packet_size(hparser_handle,
29737c478bd9Sstevel@tonic-gate 		    0, R_ITEM_INPUT, &packet_size);
29747c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
29757c478bd9Sstevel@tonic-gate 		    "Not using report id prefix. HID packet size = %d",
29767c478bd9Sstevel@tonic-gate 		    packet_size);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 		hpack->max_packet_size = packet_size;
29797c478bd9Sstevel@tonic-gate 		hpack->report_id = HID_REPORT_ID_UNDEFINED;
29807c478bd9Sstevel@tonic-gate 	} else {
29817c478bd9Sstevel@tonic-gate 		/*
29827c478bd9Sstevel@tonic-gate 		 * hid device uses multiple reports with report id prefix byte.
29837c478bd9Sstevel@tonic-gate 		 * Find the longest input report.
29847c478bd9Sstevel@tonic-gate 		 * See HID 8.4.
29857c478bd9Sstevel@tonic-gate 		 */
29867c478bd9Sstevel@tonic-gate 		max_packet_size = 0;
29877c478bd9Sstevel@tonic-gate 		max_report_id = 0;
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 		for (i = 0; i < (report_id_list.no_of_report_ids); i++) {
29907c478bd9Sstevel@tonic-gate 			(void) hidparser_get_packet_size(hparser_handle,
29917c478bd9Sstevel@tonic-gate 			    report_id_list.report_id[i], R_ITEM_INPUT,
29927c478bd9Sstevel@tonic-gate 			    &packet_size);
29937c478bd9Sstevel@tonic-gate 			if (packet_size > max_packet_size) {
29947c478bd9Sstevel@tonic-gate 				max_packet_size = packet_size;
29957c478bd9Sstevel@tonic-gate 				max_report_id = report_id_list.report_id[i];
29967c478bd9Sstevel@tonic-gate 			}
29977c478bd9Sstevel@tonic-gate 			USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
29987c478bd9Sstevel@tonic-gate 			    "Report ID %d has a packet size of %d",
29997c478bd9Sstevel@tonic-gate 			    report_id_list.report_id[i], packet_size);
30007c478bd9Sstevel@tonic-gate 		}
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 		hpack->max_packet_size = max_packet_size;
30037c478bd9Sstevel@tonic-gate 		hpack->report_id = max_report_id;
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 		USB_DPRINTF_L2(PRINT_MASK_ALL, hparser_log_handle,
30067c478bd9Sstevel@tonic-gate 		    "Report ID %d has the maximum packet size of %d",
30077c478bd9Sstevel@tonic-gate 		    max_report_id, max_packet_size);
30087c478bd9Sstevel@tonic-gate 	}
30097c478bd9Sstevel@tonic-gate }
3010