2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
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.
30 #define CHAOS_VENDOR 0x1d50
31 #define CHAOS_PRODUCT 0x60c6
35 libusb_device_handle *handle;
39 libusb_device_handle *
40 chaoskey_match(libusb_device *dev, char *match_serial)
42 struct libusb_device_descriptor desc;
45 char *device_serial = NULL;
46 libusb_device_handle *handle = NULL;
48 ret = libusb_get_device_descriptor(dev, &desc);
50 fprintf(stderr, "failed to get device descriptor: %s\n", libusb_strerror(ret));
54 if (desc.idVendor != CHAOS_VENDOR)
56 if (desc.idProduct != CHAOS_PRODUCT)
59 ret = libusb_open(dev, &handle);
61 if (match_serial == NULL)
65 fprintf(stderr, "failed to open device: %s\n", libusb_strerror(ret));
69 match_len = strlen(match_serial);
70 device_serial = malloc(match_len + 2);
73 fprintf(stderr, "malloc failed\n");
77 ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, device_serial, match_len + 1);
80 fprintf(stderr, "failed to get serial number: %s\n", libusb_strerror(ret));
84 device_serial[ret] = '\0';
86 ret = strcmp(device_serial, match_serial);
100 chaoskey_open(char *serial)
105 libusb_device **list;
106 libusb_device *device = NULL;
109 ck = calloc(sizeof (struct chaoskey), 1);
112 ret = libusb_init(&ck->ctx);
114 fprintf(stderr, "libusb_init failed: %s\n", libusb_strerror(ret));
118 num = libusb_get_device_list(ck->ctx, &list);
120 fprintf(stderr, "libusb_get_device_list failed: %s\n", libusb_strerror(num));
124 for (d = 0; d < num; d++) {
125 libusb_device_handle *handle;
127 handle = chaoskey_match(list[d], serial);
134 libusb_free_device_list(list, 1);
138 fprintf (stderr, "No chaoskey matching %s\n", serial);
140 fprintf (stderr, "No chaoskey\n");
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);
151 ret = libusb_claim_interface(ck->handle, 0);
157 if (ck->kernel_active)
158 libusb_attach_kernel_driver(ck->handle, 0);
160 libusb_exit(ck->ctx);
166 chaoskey_close(struct chaoskey *ck)
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);
177 chaoskey_transfer_callback(struct libusb_transfer *transfer)
179 struct chaoskey *ck = transfer->user_data;
182 #define ENDPOINT 0x86
185 chaoskey_read(struct chaoskey *ck, void *buffer, int len)
187 uint8_t *buf = buffer;
194 ret = libusb_bulk_transfer(ck->handle, ENDPOINT, buf, len, &transferred, 10000);
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' },
216 static void usage(char *program)
218 fprintf(stderr, "usage: %s [--serial=<serial>] [--length=<length>[kMG]] [--infinite] [--bytes]\n", program);
223 main (int argc, char **argv)
232 unsigned long length = sizeof(buf);
237 while ((c = getopt_long(argc, argv, "s:l:ib", options, NULL)) != -1) {
243 length_string = optarg;
244 length = strtoul(length_string, &length_end, 10);
245 if (!strcasecmp(length_end, "k"))
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))
266 ck = chaoskey_open(serial);
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);
284 for (i = 0; i < got / 2; i++)
285 putchar((buf[i] >> 1 & 0xff));