xref: /freebsd/lib/libusbhid/data.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
19e2046dfSNick Hibma /*	$NetBSD: data.c,v 1.8 2000/04/02 11:10:53 augustss Exp $	*/
29e2046dfSNick Hibma 
35e53a4f9SPedro F. Giffuni /*-
4*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
55e53a4f9SPedro F. Giffuni  *
69e2046dfSNick Hibma  * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
79e2046dfSNick Hibma  * All rights reserved.
89e2046dfSNick Hibma  *
99e2046dfSNick Hibma  * Redistribution and use in source and binary forms, with or without
109e2046dfSNick Hibma  * modification, are permitted provided that the following conditions
119e2046dfSNick Hibma  * are met:
129e2046dfSNick Hibma  * 1. Redistributions of source code must retain the above copyright
139e2046dfSNick Hibma  *    notice, this list of conditions and the following disclaimer.
149e2046dfSNick Hibma  * 2. Redistributions in binary form must reproduce the above copyright
159e2046dfSNick Hibma  *    notice, this list of conditions and the following disclaimer in the
169e2046dfSNick Hibma  *    documentation and/or other materials provided with the distribution.
179e2046dfSNick Hibma  *
189e2046dfSNick Hibma  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199e2046dfSNick Hibma  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209e2046dfSNick Hibma  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219e2046dfSNick Hibma  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
229e2046dfSNick Hibma  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239e2046dfSNick Hibma  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249e2046dfSNick Hibma  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259e2046dfSNick Hibma  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269e2046dfSNick Hibma  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279e2046dfSNick Hibma  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289e2046dfSNick Hibma  * SUCH DAMAGE.
299e2046dfSNick Hibma  */
309e2046dfSNick Hibma 
31cde58781SKai Wang #include <sys/param.h>
329e2046dfSNick Hibma #include <assert.h>
339e2046dfSNick Hibma #include <stdlib.h>
347778ab7eSAlexander Motin #include <string.h>
357778ab7eSAlexander Motin #include <dev/usb/usb_ioctl.h>
36cf0e07e5SMatthew N. Dodd #include "usbhid.h"
377778ab7eSAlexander Motin #include "usbvar.h"
389e2046dfSNick Hibma 
39ff7ad950SAndrew Thompson int32_t
hid_get_data(const void * p,const hid_item_t * h)409e2046dfSNick Hibma hid_get_data(const void *p, const hid_item_t *h)
419e2046dfSNick Hibma {
42cde58781SKai Wang 	const uint8_t *buf;
43cde58781SKai Wang 	uint32_t hpos;
44cde58781SKai Wang 	uint32_t hsize;
45cde58781SKai Wang 	uint32_t data;
469e2046dfSNick Hibma 	int i, end, offs;
479e2046dfSNick Hibma 
489e2046dfSNick Hibma 	buf = p;
49cde58781SKai Wang 
50cde58781SKai Wang 	/* Skip report ID byte. */
51cde58781SKai Wang 	if (h->report_ID > 0)
52cde58781SKai Wang 		buf++;
53cde58781SKai Wang 
549e2046dfSNick Hibma 	hpos = h->pos;			/* bit position of data */
559e2046dfSNick Hibma 	hsize = h->report_size;		/* bit length of data */
569e2046dfSNick Hibma 
57cde58781SKai Wang 	/* Range check and limit */
589e2046dfSNick Hibma 	if (hsize == 0)
599e2046dfSNick Hibma 		return (0);
60cde58781SKai Wang 	if (hsize > 32)
61cde58781SKai Wang 		hsize = 32;
62cde58781SKai Wang 
639e2046dfSNick Hibma 	offs = hpos / 8;
649e2046dfSNick Hibma 	end = (hpos + hsize) / 8 - offs;
659e2046dfSNick Hibma 	data = 0;
669e2046dfSNick Hibma 	for (i = 0; i <= end; i++)
679e2046dfSNick Hibma 		data |= buf[offs + i] << (i*8);
685fdb3a67SAndrew Thompson 
695fdb3a67SAndrew Thompson 	/* Correctly shift down data */
709e2046dfSNick Hibma 	data >>= hpos % 8;
715fdb3a67SAndrew Thompson 	hsize = 32 - hsize;
725fdb3a67SAndrew Thompson 
735fdb3a67SAndrew Thompson 	/* Mask and sign extend in one */
745fdb3a67SAndrew Thompson 	if ((h->logical_minimum < 0) || (h->logical_maximum < 0))
755fdb3a67SAndrew Thompson 		data = (int32_t)((int32_t)data << hsize) >> hsize;
765fdb3a67SAndrew Thompson 	else
775fdb3a67SAndrew Thompson 		data = (uint32_t)((uint32_t)data << hsize) >> hsize;
785fdb3a67SAndrew Thompson 
799e2046dfSNick Hibma 	return (data);
809e2046dfSNick Hibma }
819e2046dfSNick Hibma 
829e2046dfSNick Hibma void
hid_set_data(void * p,const hid_item_t * h,int32_t data)83ff7ad950SAndrew Thompson hid_set_data(void *p, const hid_item_t *h, int32_t data)
849e2046dfSNick Hibma {
85cde58781SKai Wang 	uint8_t *buf;
86cde58781SKai Wang 	uint32_t hpos;
87cde58781SKai Wang 	uint32_t hsize;
88ff7ad950SAndrew Thompson 	uint32_t mask;
89ff7ad950SAndrew Thompson 	int i;
90ff7ad950SAndrew Thompson 	int end;
91ff7ad950SAndrew Thompson 	int offs;
929e2046dfSNick Hibma 
939e2046dfSNick Hibma 	buf = p;
94cde58781SKai Wang 
95cde58781SKai Wang 	/* Set report ID byte. */
96cde58781SKai Wang 	if (h->report_ID > 0)
97cde58781SKai Wang 		*buf++ = h->report_ID & 0xff;
98cde58781SKai Wang 
999e2046dfSNick Hibma 	hpos = h->pos;			/* bit position of data */
1009e2046dfSNick Hibma 	hsize = h->report_size;		/* bit length of data */
1019e2046dfSNick Hibma 
1029e2046dfSNick Hibma 	if (hsize != 32) {
1039e2046dfSNick Hibma 		mask = (1 << hsize) - 1;
1049e2046dfSNick Hibma 		data &= mask;
1059e2046dfSNick Hibma 	} else
1069e2046dfSNick Hibma 		mask = ~0;
1079e2046dfSNick Hibma 
1089e2046dfSNick Hibma 	data <<= (hpos % 8);
1099e2046dfSNick Hibma 	mask <<= (hpos % 8);
1109e2046dfSNick Hibma 	mask = ~mask;
1119e2046dfSNick Hibma 
1129e2046dfSNick Hibma 	offs = hpos / 8;
1139e2046dfSNick Hibma 	end = (hpos + hsize) / 8 - offs;
1149e2046dfSNick Hibma 
1159e2046dfSNick Hibma 	for (i = 0; i <= end; i++)
1169e2046dfSNick Hibma 		buf[offs + i] = (buf[offs + i] & (mask >> (i*8))) |
1179e2046dfSNick Hibma 		    ((data >> (i*8)) & 0xff);
1189e2046dfSNick Hibma }
1197778ab7eSAlexander Motin 
1207778ab7eSAlexander Motin int
hid_get_report(int fd,enum hid_kind k,unsigned char * data,unsigned int size)1217778ab7eSAlexander Motin hid_get_report(int fd, enum hid_kind k, unsigned char *data, unsigned int size)
1227778ab7eSAlexander Motin {
1237778ab7eSAlexander Motin 	struct usb_gen_descriptor ugd;
1247778ab7eSAlexander Motin 
1257778ab7eSAlexander Motin 	memset(&ugd, 0, sizeof(ugd));
1268010f4adSBrooks Davis 	ugd.ugd_data = data;
1277778ab7eSAlexander Motin 	ugd.ugd_maxlen = size;
1287778ab7eSAlexander Motin 	ugd.ugd_report_type = k + 1;
1297778ab7eSAlexander Motin 	return (ioctl(fd, USB_GET_REPORT, &ugd));
1307778ab7eSAlexander Motin }
1317778ab7eSAlexander Motin 
1327778ab7eSAlexander Motin int
hid_set_report(int fd,enum hid_kind k,unsigned char * data,unsigned int size)1337778ab7eSAlexander Motin hid_set_report(int fd, enum hid_kind k, unsigned char *data, unsigned int size)
1347778ab7eSAlexander Motin {
1357778ab7eSAlexander Motin 	struct usb_gen_descriptor ugd;
1367778ab7eSAlexander Motin 
1377778ab7eSAlexander Motin 	memset(&ugd, 0, sizeof(ugd));
1388010f4adSBrooks Davis 	ugd.ugd_data = data;
1397778ab7eSAlexander Motin 	ugd.ugd_maxlen = size;
1407778ab7eSAlexander Motin 	ugd.ugd_report_type = k + 1;
1417778ab7eSAlexander Motin 	return (ioctl(fd, USB_SET_REPORT, &ugd));
1427778ab7eSAlexander Motin }
143