Home | History | Annotate | Line # | Download | only in libusbhid
data.c revision 1.3
      1  1.3      salo /*	$NetBSD: data.c,v 1.3 2003/07/26 19:25:08 salo Exp $	*/
      2  1.1  augustss 
      3  1.1  augustss /*
      4  1.3      salo  * Copyright (c) 1999 Lennart Augustsson <augustss (at) NetBSD.org>
      5  1.1  augustss  * All rights reserved.
      6  1.1  augustss  *
      7  1.1  augustss  * Redistribution and use in source and binary forms, with or without
      8  1.1  augustss  * modification, are permitted provided that the following conditions
      9  1.1  augustss  * are met:
     10  1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     11  1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     12  1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  augustss  *    documentation and/or other materials provided with the distribution.
     15  1.1  augustss  *
     16  1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  augustss  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  augustss  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  augustss  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  augustss  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  augustss  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  augustss  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  augustss  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  augustss  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  augustss  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  augustss  * SUCH DAMAGE.
     27  1.1  augustss  */
     28  1.2     lukem 
     29  1.2     lukem #include <sys/cdefs.h>
     30  1.3      salo __RCSID("$NetBSD: data.c,v 1.3 2003/07/26 19:25:08 salo Exp $");
     31  1.1  augustss 
     32  1.1  augustss #include <assert.h>
     33  1.1  augustss #include <stdlib.h>
     34  1.1  augustss #include "usbhid.h"
     35  1.1  augustss 
     36  1.1  augustss int
     37  1.1  augustss hid_get_data(const void *p, const hid_item_t *h)
     38  1.1  augustss {
     39  1.1  augustss 	const unsigned char *buf;
     40  1.1  augustss 	unsigned int hpos;
     41  1.1  augustss 	unsigned int hsize;
     42  1.1  augustss 	int data;
     43  1.1  augustss 	int i, end, offs;
     44  1.1  augustss 
     45  1.1  augustss 	_DIAGASSERT(p != NULL);
     46  1.1  augustss 	_DIAGASSERT(h != NULL);
     47  1.1  augustss 
     48  1.1  augustss 	buf = p;
     49  1.1  augustss 	hpos = h->pos;			/* bit position of data */
     50  1.1  augustss 	hsize = h->report_size;		/* bit length of data */
     51  1.1  augustss 
     52  1.1  augustss 	if (hsize == 0)
     53  1.1  augustss 		return (0);
     54  1.1  augustss 	offs = hpos / 8;
     55  1.1  augustss 	end = (hpos + hsize) / 8 - offs;
     56  1.1  augustss 	data = 0;
     57  1.1  augustss 	for (i = 0; i <= end; i++)
     58  1.1  augustss 		data |= buf[offs + i] << (i*8);
     59  1.1  augustss 	data >>= hpos % 8;
     60  1.1  augustss 	data &= (1 << hsize) - 1;
     61  1.1  augustss 	if (h->logical_minimum < 0) {
     62  1.1  augustss 		/* Need to sign extend */
     63  1.1  augustss 		hsize = sizeof data * 8 - hsize;
     64  1.1  augustss 		data = (data << hsize) >> hsize;
     65  1.1  augustss 	}
     66  1.1  augustss 	return (data);
     67  1.1  augustss }
     68  1.1  augustss 
     69  1.1  augustss void
     70  1.1  augustss hid_set_data(void *p, const hid_item_t *h, int data)
     71  1.1  augustss {
     72  1.1  augustss 	unsigned char *buf;
     73  1.1  augustss 	unsigned int hpos;
     74  1.1  augustss 	unsigned int hsize;
     75  1.1  augustss 	int i, end, offs, mask;
     76  1.1  augustss 
     77  1.1  augustss 	_DIAGASSERT(p != NULL);
     78  1.1  augustss 	_DIAGASSERT(h != NULL);
     79  1.1  augustss 
     80  1.1  augustss 	buf = p;
     81  1.1  augustss 	hpos = h->pos;			/* bit position of data */
     82  1.1  augustss 	hsize = h->report_size;		/* bit length of data */
     83  1.1  augustss 
     84  1.1  augustss 	if (hsize != 32) {
     85  1.1  augustss 		mask = (1 << hsize) - 1;
     86  1.1  augustss 		data &= mask;
     87  1.1  augustss 	} else
     88  1.1  augustss 		mask = ~0;
     89  1.1  augustss 
     90  1.1  augustss 	data <<= (hpos % 8);
     91  1.1  augustss 	mask <<= (hpos % 8);
     92  1.1  augustss 	mask = ~mask;
     93  1.1  augustss 
     94  1.1  augustss 	offs = hpos / 8;
     95  1.1  augustss 	end = (hpos + hsize) / 8 - offs;
     96  1.1  augustss 
     97  1.1  augustss 	for (i = 0; i <= end; i++)
     98  1.1  augustss 		buf[offs + i] = (buf[offs + i] & (mask >> (i*8))) |
     99  1.1  augustss 			((data >> (i*8)) & 0xff);
    100  1.1  augustss }
    101