xref: /freebsd/lib/libusbhid/descr.c (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
1 /*	$NetBSD: descr.c,v 1.9 2000/09/24 02:13:24 augustss Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 
34 #include <assert.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <sys/time.h>
40 #include <sys/ioctl.h>
41 #include <dev/usb/usb_ioctl.h>
42 
43 #include "usbhid.h"
44 #include "usbvar.h"
45 
46 int
47 hid_set_immed(int fd, int enable)
48 {
49 	int ret;
50 	ret = ioctl(fd, USB_SET_IMMED, &enable);
51 #ifdef HID_COMPAT7
52 	if (ret < 0)
53 		ret = hid_set_immed_compat7(fd, enable);
54 #endif
55 	return (ret);
56 }
57 
58 int
59 hid_get_report_id(int fd)
60 {
61 	report_desc_t rep;
62 	hid_data_t d;
63 	hid_item_t h;
64 	int kindset;
65 	int temp = -1;
66 	int ret;
67 
68 	if ((rep = hid_get_report_desc(fd)) == NULL)
69 		goto use_ioctl;
70 	kindset = 1 << hid_input | 1 << hid_output | 1 << hid_feature;
71 	for (d = hid_start_parse(rep, kindset, -1); hid_get_item(d, &h); ) {
72 		/* Return the first report ID we met. */
73 		if (h.report_ID != 0) {
74 			temp = h.report_ID;
75 			break;
76 		}
77 	}
78 	hid_end_parse(d);
79 	hid_dispose_report_desc(rep);
80 
81 	if (temp > 0)
82 		return (temp);
83 
84 use_ioctl:
85 	ret = ioctl(fd, USB_GET_REPORT_ID, &temp);
86 #ifdef HID_COMPAT7
87 	if (ret < 0)
88 		ret = hid_get_report_id_compat7(fd);
89 	else
90 #endif
91 		ret = temp;
92 
93 	return (ret);
94 }
95 
96 report_desc_t
97 hid_get_report_desc(int fd)
98 {
99 	struct usb_gen_descriptor ugd;
100 	report_desc_t rep;
101 	void *data;
102 
103 	memset(&ugd, 0, sizeof(ugd));
104 
105 	/* get actual length first */
106 	ugd.ugd_data = hid_pass_ptr(NULL);
107 	ugd.ugd_maxlen = 65535;
108 	if (ioctl(fd, USB_GET_REPORT_DESC, &ugd) < 0) {
109 #ifdef HID_COMPAT7
110 		/* could not read descriptor */
111 		/* try FreeBSD 7 compat code */
112 		return (hid_get_report_desc_compat7(fd));
113 #else
114 		return (NULL);
115 #endif
116 	}
117 
118 	/*
119 	 * NOTE: The kernel will return a failure if
120 	 * "ugd_actlen" is zero.
121 	 */
122 	data = malloc(ugd.ugd_actlen);
123 	if (data == NULL)
124 		return (NULL);
125 
126 	/* fetch actual descriptor */
127 	ugd.ugd_data = hid_pass_ptr(data);
128 	ugd.ugd_maxlen = ugd.ugd_actlen;
129 	if (ioctl(fd, USB_GET_REPORT_DESC, &ugd) < 0) {
130 		/* could not read descriptor */
131 		free(data);
132 		return (NULL);
133 	}
134 
135 	/* sanity check */
136 	if (ugd.ugd_actlen < 1) {
137 		/* invalid report descriptor */
138 		free(data);
139 		return (NULL);
140 	}
141 
142 	/* check END_COLLECTION */
143 	if (((unsigned char *)data)[ugd.ugd_actlen -1] != 0xC0) {
144 		/* invalid end byte */
145 		free(data);
146 		return (NULL);
147 	}
148 
149 	rep = hid_use_report_desc(data, ugd.ugd_actlen);
150 
151 	free(data);
152 
153 	return (rep);
154 }
155 
156 report_desc_t
157 hid_use_report_desc(unsigned char *data, unsigned int size)
158 {
159 	report_desc_t r;
160 
161 	r = malloc(sizeof(*r) + size);
162 	if (r == 0) {
163 		errno = ENOMEM;
164 		return (NULL);
165 	}
166 	r->size = size;
167 	memcpy(r->data, data, size);
168 	return (r);
169 }
170 
171 void
172 hid_dispose_report_desc(report_desc_t r)
173 {
174 
175 	free(r);
176 }
177