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