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 29209040d8SMatthew Dillon #include <sys/cdefs.h> 30209040d8SMatthew Dillon __FBSDID("$FreeBSD$"); 31209040d8SMatthew Dillon 32cde58781SKai Wang #include <sys/param.h> 339e2046dfSNick Hibma #include <assert.h> 349e2046dfSNick Hibma #include <stdlib.h> 35cf0e07e5SMatthew N. Dodd #include "usbhid.h" 369e2046dfSNick Hibma 37*ff7ad950SAndrew Thompson int32_t 389e2046dfSNick Hibma hid_get_data(const void *p, const hid_item_t *h) 399e2046dfSNick Hibma { 40cde58781SKai Wang const uint8_t *buf; 41cde58781SKai Wang uint32_t hpos; 42cde58781SKai Wang uint32_t hsize; 43cde58781SKai Wang uint32_t data; 449e2046dfSNick Hibma int i, end, offs; 459e2046dfSNick Hibma 469e2046dfSNick Hibma buf = p; 47cde58781SKai Wang 48cde58781SKai Wang /* Skip report ID byte. */ 49cde58781SKai Wang if (h->report_ID > 0) 50cde58781SKai Wang buf++; 51cde58781SKai Wang 529e2046dfSNick Hibma hpos = h->pos; /* bit position of data */ 539e2046dfSNick Hibma hsize = h->report_size; /* bit length of data */ 549e2046dfSNick Hibma 55cde58781SKai Wang /* Range check and limit */ 569e2046dfSNick Hibma if (hsize == 0) 579e2046dfSNick Hibma return (0); 58cde58781SKai Wang if (hsize > 32) 59cde58781SKai Wang hsize = 32; 60cde58781SKai Wang 619e2046dfSNick Hibma offs = hpos / 8; 629e2046dfSNick Hibma end = (hpos + hsize) / 8 - offs; 639e2046dfSNick Hibma data = 0; 649e2046dfSNick Hibma for (i = 0; i <= end; i++) 659e2046dfSNick Hibma data |= buf[offs + i] << (i*8); 665fdb3a67SAndrew Thompson 675fdb3a67SAndrew Thompson /* Correctly shift down data */ 689e2046dfSNick Hibma data >>= hpos % 8; 695fdb3a67SAndrew Thompson hsize = 32 - hsize; 705fdb3a67SAndrew Thompson 715fdb3a67SAndrew Thompson /* Mask and sign extend in one */ 725fdb3a67SAndrew Thompson if ((h->logical_minimum < 0) || (h->logical_maximum < 0)) 735fdb3a67SAndrew Thompson data = (int32_t)((int32_t)data << hsize) >> hsize; 745fdb3a67SAndrew Thompson else 755fdb3a67SAndrew Thompson data = (uint32_t)((uint32_t)data << hsize) >> hsize; 765fdb3a67SAndrew Thompson 779e2046dfSNick Hibma return (data); 789e2046dfSNick Hibma } 799e2046dfSNick Hibma 809e2046dfSNick Hibma void 81*ff7ad950SAndrew Thompson hid_set_data(void *p, const hid_item_t *h, int32_t data) 829e2046dfSNick Hibma { 83cde58781SKai Wang uint8_t *buf; 84cde58781SKai Wang uint32_t hpos; 85cde58781SKai Wang uint32_t hsize; 86*ff7ad950SAndrew Thompson uint32_t mask; 87*ff7ad950SAndrew Thompson int i; 88*ff7ad950SAndrew Thompson int end; 89*ff7ad950SAndrew Thompson int offs; 909e2046dfSNick Hibma 919e2046dfSNick Hibma buf = p; 92cde58781SKai Wang 93cde58781SKai Wang /* Set report ID byte. */ 94cde58781SKai Wang if (h->report_ID > 0) 95cde58781SKai Wang *buf++ = h->report_ID & 0xff; 96cde58781SKai Wang 979e2046dfSNick Hibma hpos = h->pos; /* bit position of data */ 989e2046dfSNick Hibma hsize = h->report_size; /* bit length of data */ 999e2046dfSNick Hibma 1009e2046dfSNick Hibma if (hsize != 32) { 1019e2046dfSNick Hibma mask = (1 << hsize) - 1; 1029e2046dfSNick Hibma data &= mask; 1039e2046dfSNick Hibma } else 1049e2046dfSNick Hibma mask = ~0; 1059e2046dfSNick Hibma 1069e2046dfSNick Hibma data <<= (hpos % 8); 1079e2046dfSNick Hibma mask <<= (hpos % 8); 1089e2046dfSNick Hibma mask = ~mask; 1099e2046dfSNick Hibma 1109e2046dfSNick Hibma offs = hpos / 8; 1119e2046dfSNick Hibma end = (hpos + hsize) / 8 - offs; 1129e2046dfSNick Hibma 1139e2046dfSNick Hibma for (i = 0; i <= end; i++) 1149e2046dfSNick Hibma buf[offs + i] = (buf[offs + i] & (mask >> (i*8))) | 1159e2046dfSNick Hibma ((data >> (i*8)) & 0xff); 1169e2046dfSNick Hibma } 117