xref: /freebsd/lib/libusbhid/data.c (revision 9e2046dfecbbdda2ba5060c92f583d78cca04bcb)
19e2046dfSNick Hibma /*	$NetBSD: data.c,v 1.8 2000/04/02 11:10:53 augustss Exp $	*/
29e2046dfSNick Hibma 
39e2046dfSNick Hibma /*
49e2046dfSNick Hibma  * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
59e2046dfSNick Hibma  * All rights reserved.
69e2046dfSNick Hibma  *
79e2046dfSNick Hibma  * Redistribution and use in source and binary forms, with or without
89e2046dfSNick Hibma  * modification, are permitted provided that the following conditions
99e2046dfSNick Hibma  * are met:
109e2046dfSNick Hibma  * 1. Redistributions of source code must retain the above copyright
119e2046dfSNick Hibma  *    notice, this list of conditions and the following disclaimer.
129e2046dfSNick Hibma  * 2. Redistributions in binary form must reproduce the above copyright
139e2046dfSNick Hibma  *    notice, this list of conditions and the following disclaimer in the
149e2046dfSNick Hibma  *    documentation and/or other materials provided with the distribution.
159e2046dfSNick Hibma  *
169e2046dfSNick Hibma  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
179e2046dfSNick Hibma  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
189e2046dfSNick Hibma  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199e2046dfSNick Hibma  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
209e2046dfSNick Hibma  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219e2046dfSNick Hibma  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
229e2046dfSNick Hibma  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
239e2046dfSNick Hibma  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
249e2046dfSNick Hibma  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
259e2046dfSNick Hibma  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
269e2046dfSNick Hibma  * SUCH DAMAGE.
279e2046dfSNick Hibma  *
289e2046dfSNick Hibma  * $FreeBSD$
299e2046dfSNick Hibma  */
309e2046dfSNick Hibma 
319e2046dfSNick Hibma #include <assert.h>
329e2046dfSNick Hibma #include <stdlib.h>
339e2046dfSNick Hibma #include "libusb.h"
349e2046dfSNick Hibma 
359e2046dfSNick Hibma int
369e2046dfSNick Hibma hid_get_data(const void *p, const hid_item_t *h)
379e2046dfSNick Hibma {
389e2046dfSNick Hibma 	const unsigned char *buf;
399e2046dfSNick Hibma 	unsigned int hpos;
409e2046dfSNick Hibma 	unsigned int hsize;
419e2046dfSNick Hibma 	int data;
429e2046dfSNick Hibma 	int i, end, offs;
439e2046dfSNick Hibma 
449e2046dfSNick Hibma 	_DIAGASSERT(p != NULL);
459e2046dfSNick Hibma 	_DIAGASSERT(h != NULL);
469e2046dfSNick Hibma 
479e2046dfSNick Hibma 	buf = p;
489e2046dfSNick Hibma 	hpos = h->pos;			/* bit position of data */
499e2046dfSNick Hibma 	hsize = h->report_size;		/* bit length of data */
509e2046dfSNick Hibma 
519e2046dfSNick Hibma 	if (hsize == 0)
529e2046dfSNick Hibma 		return (0);
539e2046dfSNick Hibma 	offs = hpos / 8;
549e2046dfSNick Hibma 	end = (hpos + hsize) / 8 - offs;
559e2046dfSNick Hibma 	data = 0;
569e2046dfSNick Hibma 	for (i = 0; i <= end; i++)
579e2046dfSNick Hibma 		data |= buf[offs + i] << (i*8);
589e2046dfSNick Hibma 	data >>= hpos % 8;
599e2046dfSNick Hibma 	data &= (1 << hsize) - 1;
609e2046dfSNick Hibma 	if (h->logical_minimum < 0) {
619e2046dfSNick Hibma 		/* Need to sign extend */
629e2046dfSNick Hibma 		hsize = sizeof data * 8 - hsize;
639e2046dfSNick Hibma 		data = (data << hsize) >> hsize;
649e2046dfSNick Hibma 	}
659e2046dfSNick Hibma 	return (data);
669e2046dfSNick Hibma }
679e2046dfSNick Hibma 
689e2046dfSNick Hibma void
699e2046dfSNick Hibma hid_set_data(void *p, const hid_item_t *h, int data)
709e2046dfSNick Hibma {
719e2046dfSNick Hibma 	unsigned char *buf;
729e2046dfSNick Hibma 	unsigned int hpos;
739e2046dfSNick Hibma 	unsigned int hsize;
749e2046dfSNick Hibma 	int i, end, offs, mask;
759e2046dfSNick Hibma 
769e2046dfSNick Hibma 	_DIAGASSERT(p != NULL);
779e2046dfSNick Hibma 	_DIAGASSERT(h != NULL);
789e2046dfSNick Hibma 
799e2046dfSNick Hibma 	buf = p;
809e2046dfSNick Hibma 	hpos = h->pos;			/* bit position of data */
819e2046dfSNick Hibma 	hsize = h->report_size;		/* bit length of data */
829e2046dfSNick Hibma 
839e2046dfSNick Hibma 	if (hsize != 32) {
849e2046dfSNick Hibma 		mask = (1 << hsize) - 1;
859e2046dfSNick Hibma 		data &= mask;
869e2046dfSNick Hibma 	} else
879e2046dfSNick Hibma 		mask = ~0;
889e2046dfSNick Hibma 
899e2046dfSNick Hibma 	data <<= (hpos % 8);
909e2046dfSNick Hibma 	mask <<= (hpos % 8);
919e2046dfSNick Hibma 	mask = ~mask;
929e2046dfSNick Hibma 
939e2046dfSNick Hibma 	offs = hpos / 8;
949e2046dfSNick Hibma 	end = (hpos + hsize) / 8 - offs;
959e2046dfSNick Hibma 
969e2046dfSNick Hibma 	for (i = 0; i <= end; i++)
979e2046dfSNick Hibma 		buf[offs + i] = (buf[offs + i] & (mask >> (i*8))) |
989e2046dfSNick Hibma 			((data >> (i*8)) & 0xff);
999e2046dfSNick Hibma }
100