6d860139f71487a59dbd3f9d8e6433532fa018b4
[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 ENDPOINT        0x86
176
177 int
178 chaoskey_read(struct chaoskey *ck, void *buffer, int len)
179 {
180         uint8_t *buf = buffer;
181         int     total = 0;
182
183         while (len) {
184                 int     ret;
185                 int     transferred;
186
187                 ret = libusb_bulk_transfer(ck->handle, ENDPOINT, buf, len, &transferred, 10000);
188                 if (ret) {
189                         if (total)
190                                 return total;
191                         else {
192                                 errno = EIO;
193                                 return -1;
194                         }
195                 }
196                 len -= transferred;
197                 buf += transferred;
198                 total += transferred;
199         }
200         return total;
201 }
202
203 static const struct option options[] = {
204         { .name = "serial", .has_arg = 1, .val = 's' },
205         { .name = "length", .has_arg = 1, .val = 'l' },
206         { .name = "infinite", .has_arg = 0, .val = 'i' },
207         { .name = "bytes", .has_arg = 0, .val = 'b' },
208         { 0, 0, 0, 0},
209 };
210
211 static void usage(char *program)
212 {
213         fprintf(stderr, "usage: %s [--serial=<serial>] [--length=<length>[kMG]] [--infinite] [--bytes]\n", program);
214         exit(1);
215 }
216
217 int
218 main (int argc, char **argv)
219 {
220         struct chaoskey *ck;
221         uint16_t        buf[512];
222         int     got;
223         int     c;
224         char    *serial = NULL;
225         char    *length_string;
226         char    *length_end;
227         unsigned long   length = sizeof(buf);
228         int     this_time;
229         int     infinite = 0;
230         int     bytes = 0;
231
232         while ((c = getopt_long(argc, argv, "s:l:ib", options, NULL)) != -1) {
233                 switch (c) {
234                 case 's':
235                         serial = optarg;
236                         break;
237                 case 'l':
238                         length_string = optarg;
239                         length = strtoul(length_string, &length_end, 10);
240                         if (!strcasecmp(length_end, "k"))
241                                 length *= 1024;
242                         else if (!strcasecmp(length_end, "m"))
243                                 length *= 1024 * 1024;
244                         else if (!strcasecmp(length_end, "g"))
245                                 length *= 1024 * 1024 * 1024;
246                         else if (strlen(length_end))
247                                  usage(argv[0]);
248                         break;
249                 case 'i':
250                         infinite = 1;
251                         break;
252                 case 'b':
253                         bytes = 1;
254                         break;
255                 default:
256                         usage(argv[0]);
257                         break;
258                 }
259         }
260
261         ck = chaoskey_open(serial);
262         if (!ck)
263                 exit(1);
264
265         if (bytes)
266                 length *= 2;
267
268         while (length || infinite) {
269                 this_time = sizeof(buf);
270                 if (!infinite && length < sizeof(buf))
271                         this_time = (int) length;
272                 got = chaoskey_read(ck, buf, this_time);
273                 if (got < 0) {
274                         perror("read");
275                         exit(1);
276                 }
277                 if (bytes) {
278                         int i;
279                         for (i = 0; i < got / 2; i++)
280                                 putchar((buf[i] >> 1 & 0xff));
281                 } else {
282                         int i;
283                         int ret;
284                         for (i = 0; i < got; i += ret) {
285                                 ret = write(1, ((char *) buf) + i, got - i);
286                                 if (ret <= 0) {
287                                         perror("write");
288                                         exit(1);
289                                 }
290                         }
291                 }
292                 length -= got;
293         }
294         exit(0);
295 }