]> git.itanic.dy.fi Git - glucose/blob - hiddev.c
contour-protocol: Replace bad macros with static functions
[glucose] / hiddev.c
1 /*
2  * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <linux/hiddev.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30
31 #include "hiddev.h"
32 #include "utils.h"
33
34 #define PRINT_FIELD(level, field) trace(level, #field ": %04x\n", field)
35
36 #define HIDDEV_PATH "/dev/usb/"
37
38 int hiddev_read(unsigned char *data, int bufsize, int fd)
39 {
40         struct hiddev_event readBuffer[READ_BUFFER_LENGTH];
41         int ret, n, i;
42
43         ret = read(fd, readBuffer, sizeof(readBuffer));
44         if (ret < 0)
45                 return ret;
46
47         n = ret / sizeof(readBuffer[0]);
48         for (i = 0; i < n && i < bufsize; i++)
49                 data[i] = readBuffer[i].value;
50         return n;
51 }
52
53 int hiddev_write(const unsigned char data[64], int fd , int usage_code)
54 {
55         int rc = 0, uindex;
56
57         struct hiddev_usage_ref uref;
58         struct hiddev_report_info rinfo;
59
60         uref.report_id = *data++;
61         uref.report_type = HID_REPORT_TYPE_OUTPUT;
62         uref.field_index = 0;
63
64         uref.usage_code = usage_code;
65
66         for (uindex = 0; uindex < 63; uindex++) {
67                 uref.usage_index = uindex;
68                 uref.value = *data++;
69
70                 rc = ioctl(fd, HIDIOCSUSAGE, &uref);
71                 if (rc != 0)
72                         goto err;
73         }
74
75         rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
76         rinfo.report_id =  0x0;
77         rinfo.num_fields = 1;
78
79         rc = ioctl(fd, HIDIOCSREPORT, &rinfo);
80         if (rc != 0)
81                 goto err2;
82
83         return 0;
84 err2:
85         printf("HIDIOCSREPORT\n");
86 err:
87         printf("Error in IOCTL: %m\n");
88
89         return rc;
90 }
91
92 static int get_usagecode(int fd)
93 {
94         struct hiddev_usage_ref uref;
95         int rc;
96
97         uref.report_type = HID_REPORT_TYPE_OUTPUT;
98         uref.report_id = 0x0;
99         uref.field_index = 0;
100         uref.usage_index = 0;
101
102         rc = ioctl(fd, HIDIOCGUCODE, &uref);
103         if (rc < 0) {
104                 printf("Error gettin usage code: %m\n");
105                 return rc;
106         }
107
108         return uref.usage_code;
109 }
110
111 static int _hiddev_open(const char *device_path, int *usage_code,
112                         int vendor_id, int product_id)
113 {
114         struct hiddev_devinfo device_info;
115         struct hiddev_report_info rinfo;
116         int ret;
117         int fd;
118
119         trace(3, "Opening device %s\n", device_path);
120         fd = ret = open(device_path, O_RDWR);
121
122         if (fd < 0) {
123                 trace(0, "Error opening device %s: %m\n", device_path);
124                 return -1;
125         }
126
127         rinfo.report_type = HID_REPORT_TYPE_OUTPUT;
128         rinfo.report_id = HID_REPORT_ID_FIRST;
129         ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo);
130         if (ret < 0)
131                 goto err_ioctl;
132
133         PRINT_FIELD(3, rinfo.report_type);
134         PRINT_FIELD(3, rinfo.report_id);
135         PRINT_FIELD(3, rinfo.num_fields);
136
137         *usage_code = get_usagecode(fd);
138
139         if (*usage_code < 0)
140                 goto err_close;
141
142         ret = ioctl(fd, HIDIOCGDEVINFO, &device_info);
143
144         if (ret < 0)
145                 goto err_ioctl;
146
147         PRINT_FIELD(3, device_info.bustype);
148         PRINT_FIELD(3, device_info.busnum);
149         PRINT_FIELD(3, device_info.devnum);
150         PRINT_FIELD(3, device_info.ifnum);
151         PRINT_FIELD(3, device_info.vendor);
152         PRINT_FIELD(3, device_info.product);
153         PRINT_FIELD(3, device_info.version);
154         PRINT_FIELD(3, device_info.num_applications);
155
156         if (product_id && vendor_id) {
157                 if (product_id == device_info.product &&
158                         vendor_id == device_info.vendor)
159                         return fd;
160                 trace(3, "Vendor and product IDs don't match\n");
161                 ret = -1;
162                 goto err_close;
163         }
164
165         return fd;
166
167 err_ioctl:
168         trace(0, "Error issuing ioctl: %m\n");
169
170 err_close:
171         close(fd);
172
173         return ret;
174 }
175
176 int hiddev_open(const char *device_path, int *usage_code)
177 {
178         return _hiddev_open(device_path, usage_code, 0, 0);
179 }
180
181 int hiddev_open_by_id(int vendor_id, int product_id, int *usage_code)
182 {
183         struct dirent *dirent;
184         DIR *dir;
185         int error, fd;
186         char path[256];
187
188         dir = opendir(HIDDEV_PATH);
189         if (dir == NULL) {
190                 error = errno;
191                 trace(4, "Failed to open directory %s: %m\n", HIDDEV_PATH);
192
193                 return -error;
194         }
195
196         while (1) {
197                 dirent = readdir(dir);
198                 if (dirent == NULL)
199                         break;
200
201                 if (strncmp(dirent->d_name, "hiddev", sizeof("hiddev") - 1))
202                         continue;
203
204                 path[0] = 0;
205                 strncat(path, HIDDEV_PATH, sizeof(path) - 1);
206                 strncat(path, dirent->d_name, sizeof(path) - 1);
207
208                 fd = _hiddev_open(path, usage_code, vendor_id, product_id);
209                 if (fd < 0)
210                         continue;
211                 return fd;
212         }
213
214         if (errno) {
215                 error = errno;
216                 trace(0, "Error reading directory %s: %m\n", HIDDEV_PATH);
217
218                 return -error;
219         }
220
221         trace(0, "Canno't find any mathing hiddev devices\n");
222         return -1;
223 }