xref: /netbsd-src/lib/libusbhid/descr.c (revision f8ec6d43b41f4e2b54b824004361276d94911915)
1*f8ec6d43Sjakllsch /*	$NetBSD: descr.c,v 1.6 2015/12/31 20:35:21 jakllsch Exp $	*/
28ac1932eSaugustss 
38ac1932eSaugustss /*
499410184Ssalo  * Copyright (c) 1999 Lennart Augustsson <augustss@NetBSD.org>
58ac1932eSaugustss  * All rights reserved.
68ac1932eSaugustss  *
78ac1932eSaugustss  * Redistribution and use in source and binary forms, with or without
88ac1932eSaugustss  * modification, are permitted provided that the following conditions
98ac1932eSaugustss  * are met:
108ac1932eSaugustss  * 1. Redistributions of source code must retain the above copyright
118ac1932eSaugustss  *    notice, this list of conditions and the following disclaimer.
128ac1932eSaugustss  * 2. Redistributions in binary form must reproduce the above copyright
138ac1932eSaugustss  *    notice, this list of conditions and the following disclaimer in the
148ac1932eSaugustss  *    documentation and/or other materials provided with the distribution.
158ac1932eSaugustss  *
168ac1932eSaugustss  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178ac1932eSaugustss  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188ac1932eSaugustss  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198ac1932eSaugustss  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208ac1932eSaugustss  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218ac1932eSaugustss  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228ac1932eSaugustss  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238ac1932eSaugustss  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248ac1932eSaugustss  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258ac1932eSaugustss  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268ac1932eSaugustss  * SUCH DAMAGE.
278ac1932eSaugustss  */
288ac1932eSaugustss 
29e2d78706Slukem #include <sys/cdefs.h>
30*f8ec6d43Sjakllsch __RCSID("$NetBSD: descr.c,v 1.6 2015/12/31 20:35:21 jakllsch Exp $");
31e2d78706Slukem 
328ac1932eSaugustss #include <sys/types.h>
338ac1932eSaugustss 
348ac1932eSaugustss #include <assert.h>
358ac1932eSaugustss #include <errno.h>
368ac1932eSaugustss #include <stdlib.h>
378ac1932eSaugustss #include <string.h>
388ac1932eSaugustss #include <unistd.h>
398ac1932eSaugustss #include <sys/time.h>
408ac1932eSaugustss 
418ac1932eSaugustss #include <dev/usb/usb.h>
428ac1932eSaugustss 
438ac1932eSaugustss #include "usbhid.h"
448ac1932eSaugustss #include "usbvar.h"
458ac1932eSaugustss 
468ac1932eSaugustss report_desc_t
hid_get_report_desc(int fd)478ac1932eSaugustss hid_get_report_desc(int fd)
488ac1932eSaugustss {
498ac1932eSaugustss 	struct usb_ctl_report_desc rep;
508ac1932eSaugustss 
518ac1932eSaugustss 	_DIAGASSERT(fd != -1);
528ac1932eSaugustss 
536728d906Schristos 	rep.ucrd_size = 0;
548ac1932eSaugustss 	if (ioctl(fd, USB_GET_REPORT_DESC, &rep) < 0)
558ac1932eSaugustss 		return (NULL);
568ac1932eSaugustss 
576728d906Schristos 	return hid_use_report_desc(rep.ucrd_data, (unsigned int)rep.ucrd_size);
588ac1932eSaugustss }
598ac1932eSaugustss 
608ac1932eSaugustss report_desc_t
hid_use_report_desc(const uint8_t * data,unsigned int size)61a7ea3f4eSplunky hid_use_report_desc(const uint8_t *data, unsigned int size)
628ac1932eSaugustss {
638ac1932eSaugustss 	report_desc_t r;
648ac1932eSaugustss 
658ac1932eSaugustss 	r = malloc(sizeof(*r) + size);
66*f8ec6d43Sjakllsch 	if (r == NULL) {
678ac1932eSaugustss 		errno = ENOMEM;
688ac1932eSaugustss 		return (NULL);
698ac1932eSaugustss 	}
708ac1932eSaugustss 	r->size = size;
718ac1932eSaugustss 	memcpy(r->data, data, size);
728ac1932eSaugustss 	return (r);
738ac1932eSaugustss }
748ac1932eSaugustss 
758ac1932eSaugustss void
hid_dispose_report_desc(report_desc_t r)768ac1932eSaugustss hid_dispose_report_desc(report_desc_t r)
778ac1932eSaugustss {
788ac1932eSaugustss 
798ac1932eSaugustss 	free(r);
808ac1932eSaugustss }
81