8a814a009ed7b20b405877431a334d6918c03a73
[fw/altos] / ao-tools / ao-chaosread / ao-chaosread.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdio.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <libusb.h>
24 #include <getopt.h>
25 #include <string.h>
26 #include <strings.h>
27
28 #define CHAOS_SIZE      64
29
30 #define CHAOS_VENDOR    0x1d50
31 #define CHAOS_PRODUCT   0x60c6
32
33 struct chaoskey {
34         libusb_context          *ctx;
35         libusb_device_handle    *handle;
36         int                     kernel_active;
37 };
38
39 libusb_device_handle *
40 chaoskey_match(libusb_device *dev, char *match_serial)
41 {
42         struct libusb_device_descriptor desc;
43         int ret;
44         int match_len;
45         char *device_serial = NULL;
46         libusb_device_handle *handle = NULL;
47
48         ret = libusb_get_device_descriptor(dev, &desc);
49         if (ret < 0) {
50                 fprintf(stderr, "failed to get device descriptor: %s\n", libusb_strerror(ret));
51                 return 0;
52         }
53
54         if (desc.idVendor != CHAOS_VENDOR)
55                 return NULL;
56         if (desc.idProduct != CHAOS_PRODUCT)
57                 return NULL;
58
59         ret = libusb_open(dev, &handle);
60
61         if (match_serial == NULL)
62                 return handle;
63
64         if (ret < 0) {
65                 fprintf(stderr, "failed to open device: %s\n", libusb_strerror(ret));
66                 return NULL;
67         }
68
69         match_len = strlen(match_serial);
70         device_serial = malloc(match_len + 2);
71
72         if (!device_serial) {
73                 fprintf(stderr, "malloc failed\n");
74                 goto out;
75         }
76
77         ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *) device_serial, match_len + 1);
78
79         if (ret < 0) {
80                 fprintf(stderr, "failed to get serial number: %s\n", libusb_strerror(ret));
81                 goto out;
82         }
83
84         device_serial[ret] = '\0';
85
86         ret = strcmp(device_serial, match_serial);
87         free(device_serial);
88         if (ret)
89                 goto out;
90
91         return handle;
92
93 out:
94         if (handle)
95                 libusb_close(handle);
96         return 0;
97 }
98
99 struct chaoskey *
100 chaoskey_open(char *serial)
101 {
102         struct chaoskey *ck;
103         int             ret;
104         ssize_t         num;
105         libusb_device   **list;
106         int             d;
107
108         ck = calloc(sizeof (struct chaoskey), 1);
109         if (!ck)
110                 goto out;
111         ret = libusb_init(&ck->ctx);
112         if (ret ) {
113                 fprintf(stderr, "libusb_init failed: %s\n", libusb_strerror(ret));
114                 goto out;
115         }
116
117         num = libusb_get_device_list(ck->ctx, &list);
118         if (num < 0) {
119                 fprintf(stderr, "libusb_get_device_list failed: %s\n", libusb_strerror(num));
120                 goto out;
121         }
122
123         for (d = 0; d < num; d++) {
124                 libusb_device_handle *handle;
125
126                 handle = chaoskey_match(list[d], serial);
127                 if (handle) {
128                         ck->handle = handle;
129                         break;
130                 }
131         }
132
133         libusb_free_device_list(list, 1);
134
135         if (!ck->handle) {
136                 if (serial)
137                         fprintf (stderr, "No chaoskey matching %s\n", serial);
138                 else
139                         fprintf (stderr, "No chaoskey\n");
140                 goto out;
141         }
142
143         ck->kernel_active = libusb_kernel_driver_active(ck->handle, 0);
144         if (ck->kernel_active) {
145                 ret = libusb_detach_kernel_driver(ck->handle, 0);
146                 if (ret)
147                         goto out;
148         }
149
150         ret = libusb_claim_interface(ck->handle, 0);
151         if (ret)
152                 goto out;
153
154         return ck;
155 out:
156         if (ck->kernel_active)
157                 libusb_attach_kernel_driver(ck->handle, 0);
158         if (ck->ctx)
159                 libusb_exit(ck->ctx);
160         free(ck);
161         return NULL;
162 }
163
164 void
165 chaoskey_close(struct chaoskey *ck)
166 {
167         libusb_release_interface(ck->handle, 0);
168         if (ck->kernel_active)
169                 libusb_attach_kernel_driver(ck->handle, 0);
170         libusb_close(ck->handle);
171         libusb_exit(ck->ctx);
172         free(ck);
173 }
174
175 #define COOKED_ENDPOINT 0x85
176 #define RAW_ENDPOINT    0x86
177
178 int
179 chaoskey_read(struct chaoskey *ck, int endpoint, void *buffer, int len)
180 {
181         uint8_t *buf = buffer;
182         int     total = 0;
183
184         while (len) {
185                 int     ret;
186                 int     transferred;
187
188                 ret = libusb_bulk_transfer(ck->handle, endpoint, buf, len, &transferred, 10000);
189                 if (ret) {
190                         if (total)
191                                 return total;
192                         else {
193                                 errno = EIO;
194                                 return -1;
195                         }
196                 }
197                 len -= transferred;
198                 buf += transferred;
199                 total += transferred;
200         }
201         return total;
202 }
203
204 static const struct option options[] = {
205         { .name = "serial", .has_arg = 1, .val = 's' },
206         { .name = "length", .has_arg = 1, .val = 'l' },
207         { .name = "infinite", .has_arg = 0, .val = 'i' },
208         { .name = "bytes", .has_arg = 0, .val = 'b' },
209         { .name = "cooked", .has_arg = 0, .val = 'c' },
210         { .name = "raw", .has_arg = 0, .val = 'r' },
211         { 0, 0, 0, 0},
212 };
213
214 static void usage(char *program)
215 {
216         fprintf(stderr, "usage: %s [--serial=<serial>] [--length=<length>[kMG]] [--infinite] [--bytes] [--cooked] [--raw]\n", program);
217         exit(1);
218 }
219
220 int
221 main (int argc, char **argv)
222 {
223         struct chaoskey *ck;
224         uint16_t        buf[512];
225         int     got;
226         int     c;
227         char    *serial = NULL;
228         char    *length_string;
229         char    *length_end;
230         unsigned long   length = sizeof(buf);
231         int     this_time;
232         int     infinite = 0;
233         int     bytes = 0;
234         int     endpoint = RAW_ENDPOINT;
235
236         while ((c = getopt_long(argc, argv, "s:l:ibcr", options, NULL)) != -1) {
237                 switch (c) {
238                 case 's':
239                         serial = optarg;
240                         break;
241                 case 'l':
242                         length_string = optarg;
243                         length = strtoul(length_string, &length_end, 10);
244                         if (!strcasecmp(length_end, "k"))
245                                 length *= 1024;
246                         else if (!strcasecmp(length_end, "m"))
247                                 length *= 1024 * 1024;
248                         else if (!strcasecmp(length_end, "g"))
249                                 length *= 1024 * 1024 * 1024;
250                         else if (strlen(length_end))
251                                  usage(argv[0]);
252                         break;
253                 case 'i':
254                         infinite = 1;
255                         break;
256                 case 'b':
257                         bytes = 1;
258                         break;
259                 case 'c':
260                         endpoint = COOKED_ENDPOINT;
261                         break;
262                 case 'r':
263                         endpoint = RAW_ENDPOINT;
264                         break;
265                 default:
266                         usage(argv[0]);
267                         break;
268                 }
269         }
270
271         ck = chaoskey_open(serial);
272         if (!ck)
273                 exit(1);
274
275         if (bytes)
276                 length *= 2;
277
278         while (length || infinite) {
279                 this_time = sizeof(buf);
280                 if (!infinite && length < sizeof(buf))
281                         this_time = (int) length;
282                 got = chaoskey_read(ck, endpoint, buf, this_time);
283                 if (got < 0) {
284                         perror("read");
285                         exit(1);
286                 }
287                 if (bytes) {
288                         int i;
289                         for (i = 0; i < got / 2; i++)
290                                 putchar((buf[i] >> 1 & 0xff));
291                 } else {
292                         int i;
293                         int ret;
294                         for (i = 0; i < got; i += ret) {
295                                 ret = write(1, ((char *) buf) + i, got - i);
296                                 if (ret <= 0) {
297                                         perror("write");
298                                         exit(1);
299                                 }
300                         }
301                 }
302                 length -= got;
303         }
304         exit(0);
305 }