ao-chaosread: Add --infinite and --bytes options
[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, 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         free(device_serial);
95         if (handle)
96                 libusb_close(handle);
97         return 0;
98 }
99
100 struct chaoskey *
101 chaoskey_open(char *serial)
102 {
103         struct chaoskey *ck;
104         int             ret;
105         ssize_t         num;
106         libusb_device   **list;
107         libusb_device   *device = NULL;
108         int             d;
109
110         ck = calloc(sizeof (struct chaoskey), 1);
111         if (!ck)
112                 goto out;
113         ret = libusb_init(&ck->ctx);
114         if (ret ) {
115                 fprintf(stderr, "libusb_init failed: %s\n", libusb_strerror(ret));
116                 goto out;
117         }
118
119         num = libusb_get_device_list(ck->ctx, &list);
120         if (num < 0) {
121                 fprintf(stderr, "libusb_get_device_list failed: %s\n", libusb_strerror(num));
122                 goto out;
123         }
124
125         for (d = 0; d < num; d++) {
126                 libusb_device_handle *handle;
127
128                 handle = chaoskey_match(list[d], serial);
129                 if (handle) {
130                         ck->handle = handle;
131                         break;
132                 }
133         }
134
135         libusb_free_device_list(list, 1);
136
137         if (!ck->handle) {
138                 if (serial)
139                         fprintf (stderr, "No chaoskey matching %s\n", serial);
140                 else
141                         fprintf (stderr, "No chaoskey\n");
142                 goto out;
143         }
144
145         ck->kernel_active = libusb_kernel_driver_active(ck->handle, 0);
146         if (ck->kernel_active) {
147                 ret = libusb_detach_kernel_driver(ck->handle, 0);
148                 if (ret)
149                         goto out;
150         }
151
152         ret = libusb_claim_interface(ck->handle, 0);
153         if (ret)
154                 goto out;
155
156         return ck;
157 out:
158         if (ck->kernel_active)
159                 libusb_attach_kernel_driver(ck->handle, 0);
160         if (ck->ctx)
161                 libusb_exit(ck->ctx);
162         free(ck);
163         return NULL;
164 }
165
166 void
167 chaoskey_close(struct chaoskey *ck)
168 {
169         libusb_release_interface(ck->handle, 0);
170         if (ck->kernel_active)
171                 libusb_attach_kernel_driver(ck->handle, 0);
172         libusb_close(ck->handle);
173         libusb_exit(ck->ctx);
174         free(ck);
175 }
176
177 void
178 chaoskey_transfer_callback(struct libusb_transfer *transfer)
179 {
180         struct chaoskey *ck = transfer->user_data;
181 }
182
183 #define ENDPOINT        0x86
184
185 int
186 chaoskey_read(struct chaoskey *ck, void *buffer, int len)
187 {
188         uint8_t *buf = buffer;
189         int     total = 0;
190
191         while (len) {
192                 int     ret;
193                 int     transferred;
194
195                 ret = libusb_bulk_transfer(ck->handle, ENDPOINT, buf, len, &transferred, 10000);
196                 if (ret) {
197                         if (total)
198                                 return total;
199                         else {
200                                 errno = EIO;
201                                 return -1;
202                         }
203                 }
204                 len -= transferred;
205                 buf += transferred;
206         }
207 }
208
209 static const struct option options[] = {
210         { .name = "serial", .has_arg = 1, .val = 's' },
211         { .name = "length", .has_arg = 1, .val = 'l' },
212         { .name = "infinite", .has_arg = 0, .val = 'i' },
213         { .name = "bytes", .has_arg = 0, .val = 'b' },
214         { 0, 0, 0, 0},
215 };
216
217 static void usage(char *program)
218 {
219         fprintf(stderr, "usage: %s [--serial=<serial>] [--length=<length>[kMG]] [--infinite] [--bytes]\n", program);
220         exit(1);
221 }
222
223 int
224 main (int argc, char **argv)
225 {
226         struct chaoskey *ck;
227         uint16_t        buf[512];
228         int     got;
229         int     c;
230         char    *serial = NULL;
231         char    *length_string;
232         char    *length_end;
233         unsigned long   length = sizeof(buf);
234         int     this_time;
235         int     infinite = 0;
236         int     bytes = 0;
237
238         while ((c = getopt_long(argc, argv, "s:l:", options, NULL)) != -1) {
239                 switch (c) {
240                 case 's':
241                         serial = optarg;
242                         break;
243                 case 'l':
244                         length_string = optarg;
245                         length = strtoul(length_string, &length_end, 10);
246                         if (!strcasecmp(length_end, "k"))
247                                 length *= 1024;
248                         else if (!strcasecmp(length_end, "m"))
249                                 length *= 1024 * 1024;
250                         else if (!strcasecmp(length_end, "g"))
251                                 length *= 1024 * 1024 * 1024;
252                         else if (strlen(length_end))
253                                  usage(argv[0]);
254                         break;
255                 case 'i':
256                         infinite = 1;
257                         break;
258                 case 'b':
259                         bytes = 1;
260                         break;
261                 default:
262                         usage(argv[0]);
263                         break;
264                 }
265         }
266
267         ck = chaoskey_open(serial);
268         if (!ck)
269                 exit(1);
270
271         if (bytes)
272                 length *= 2;
273
274         while (length || infinite) {
275                 this_time = sizeof(buf);
276                 if (!infinite && length < sizeof(buf))
277                         this_time = (int) length;
278                 got = chaoskey_read(ck, buf, this_time);
279                 if (got < 0) {
280                         perror("read");
281                         exit(1);
282                 }
283                 if (bytes) {
284                         int i;
285                         for (i = 0; i < got / 2; i++)
286                                 putchar((buf[i] >> 1 & 0xff));
287                 } else
288                         write(1, buf, got);
289                 length -= got;
290         }
291         exit(0);
292 }