summaryrefslogtreecommitdiff
path: root/extra/usb_console/usb_console.c
blob: 7d99bc30824b3ce7a5f571c502f46de0e27dc490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Copyright 2015 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <errno.h>
#include <getopt.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <unistd.h>

#include <libusb.h>

/* Options */
static uint16_t vid = 0x18d1;			/* Google */
static uint16_t pid = 0x500f;			/* discovery-stm32f072 */
static uint8_t ep_num = 4;			/* console endpoint */

static unsigned char rx_buf[1024];		/* much too big */
static unsigned char tx_buf[1024];		/* much too big */
static const struct libusb_pollfd **usb_fds;
static struct libusb_device_handle *devh;
static struct libusb_transfer *rx_transfer;
static struct libusb_transfer *tx_transfer;
static int tx_ready;
static int do_exit;

static void request_exit(const char *format, ...)
{
	va_list ap;
	va_start(ap, format);
	vfprintf(stderr, format, ap);
	va_end(ap);
	do_exit++;
}

#define BOO(msg, r)							\
	request_exit("%s: line %d, %s\n", msg, __LINE__,		\
		     libusb_error_name(r))

static void sighandler(int signum)
{
	request_exit("caught signal %d: %s\n", signum, sys_siglist[signum]);
}

#if 0
static void show_xfer(const char *msg, struct libusb_transfer *t)
{
	printf("%s: f=%02x ep=%02x type=%d status=%d len=%d actlen=%d\n", msg,
	       t->flags,
	       t->endpoint, t->type, t->status, t->length, t->actual_length);
}
#endif

static void LIBUSB_CALL cb_rx(struct libusb_transfer *transfer)
{
	int r;

	if (transfer->actual_length) {
		transfer->buffer[transfer->actual_length] = '\0';
		fputs((char *)transfer->buffer, stdout);
		fflush(stdout);
	}

	if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
		printf("rx_transfer cancelled\n");
		if (rx_transfer)
			libusb_free_transfer(rx_transfer);
		rx_transfer = NULL;
		return;
	}

	/* Try again */
	if (!do_exit) {
		r = libusb_submit_transfer(rx_transfer);
		if (r < 0)
			BOO("resubmit rx_transfer failed", r);
	}
}

static void LIBUSB_CALL cb_tx(struct libusb_transfer *transfer)
{
	if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
		if (tx_transfer)
			libusb_free_transfer(tx_transfer);
		tx_transfer = NULL;
		request_exit("tx_transfer cancelled\n");
		return;
	}

	if (tx_ready != transfer->actual_length)
		printf("%s: only sent %d/%d bytes\n", __func__,
		       transfer->actual_length, tx_ready);

	tx_ready = 0;
}

static void send_tx(int len)
{
	int r;

	libusb_fill_bulk_transfer(tx_transfer, devh,
				  ep_num, tx_buf, len, cb_tx, NULL, 0);

	r = libusb_submit_transfer(tx_transfer);
	if (r < 0)
		BOO("submit tx_transfer failed", r);
}

static void handle_stdin(void)
{
	static unsigned int i;
	int n;

	for (; i < sizeof(tx_buf) - 1; i++) {
		n = read(0, tx_buf + i, 1);
		if (n == 0) {
			request_exit("EOF on stdin\n");
			return;
		}
		if (n < 0) {
			request_exit("stdin: %s\n", strerror(errno));
			return;
		}

		if (tx_buf[i] == '\n') {
			i++;
			tx_buf[i] = '\0';
			break;
		}
	}

	tx_ready = strlen((char *)tx_buf) + 1;
	send_tx(tx_ready);
	i = 0;
}

static void handle_libusb(void)
{
	struct timeval tv = { 0, 0 };
	int r;

	r = libusb_handle_events_timeout_completed(NULL, &tv, &do_exit);
	if (r < 0)
		BOO("libusb event problem", r);
}

static int wait_for_stuff_to_happen(void)
{
	int i, r, nfds = 0;
	fd_set readset, writeset;
	struct timeval tv = { 1, 0 };

	if (!usb_fds) {
		request_exit("No usb_fds to watch\n");
		return -1;
	}

	FD_ZERO(&readset);
	FD_ZERO(&writeset);
	/* always watch stdin */
	FD_SET(0, &readset);

	for (i = 0; usb_fds[i]; i++) {
		int fd = usb_fds[i]->fd;
		short events = usb_fds[i]->events;
		if (fd > nfds)
			nfds = fd;

		if (events & POLLIN)
			FD_SET(fd, &readset);
		if (events & POLLOUT)
			FD_SET(fd, &writeset);
	}

	r = select(nfds + 1, &readset, &writeset, NULL, &tv);
	if (r < 0) {
		request_exit("select: %s\n", strerror(errno));
		return -1;
	}

	if (r == 0)				/* timed out */
		return 0;

	/* Ignore stdin until we've finished sending the current line */
	if (!tx_ready && FD_ISSET(0, &readset))
		return 1;

	/* libusb, then */
	return 2;
}

static int find_interface_with_endpoint(int want_ep_num)
{
	int iface_num = -1;
	int r, i, j, k;
	struct libusb_device *dev;
	struct libusb_config_descriptor *conf = 0;
	const struct libusb_interface *iface0;
	const struct libusb_interface_descriptor *iface;
	const struct libusb_endpoint_descriptor *ep;

	dev = libusb_get_device(devh);
	r = libusb_get_active_config_descriptor(dev, &conf);
	if (r < 0) {
		BOO("get_active_config", r);
		return -1;
	}

	for (i = 0; i < conf->bNumInterfaces; i++) {
		iface0 = &conf->interface[i];
		for (j = 0; j < iface0->num_altsetting; j++) {
			iface = &iface0->altsetting[j];
			for (k = 0; k < iface->bNumEndpoints; k++) {
				ep = &iface->endpoint[k];
				if (ep->bEndpointAddress == want_ep_num) {
					iface_num = i;
					break;
				}
			}
		}
	}

	libusb_free_config_descriptor(conf);
	return iface_num;
}

static char *progname;
static char *short_opts = ":v:p:e:h";
static const struct option long_opts[] = {
	/* name    hasarg *flag val */
	{"vid",      1,   NULL, 'v'},
	{"pid",      1,   NULL, 'p'},
	{"ep",       1,   NULL, 'e'},
	{"help",     0,   NULL, 'h'},
	{NULL,       0,   NULL, 0},
};

static void usage(int errs)
{
	printf("\nUsage: %s [options]\n"
	       "\n"
	       "A very simple serial console emulator\n"
	       "\n"
	       "Options:\n"
	       "\n"
	       "  -v,--vid    HEXVAL      Vendor ID (default %04x)\n"
	       "  -p,--pid    HEXVAL      Product ID (default %04x)\n"
	       "  -e,--ep     NUM         Endpoint (default %d)\n"
	       "  -h,--help               Show this message\n"
	       "\n", progname, vid, pid, ep_num);

	exit(!!errs);
}

int main(int argc, char *argv[])
{
	struct sigaction sigact;
	int iface_num;
	int claimed_iface = 0;
	int r = 1;
	int errorcnt = 0;
	char *e = 0;
	int i;

	progname = strrchr(argv[0], '/');
	if (progname)
		progname++;
	else
		progname = argv[0];

	opterr = 0;				/* quiet, you */
	while ((i = getopt_long(argc, argv, short_opts, long_opts, 0)) != -1) {
		switch (i) {
		case 'p':
			pid = (uint16_t) strtoul(optarg, &e, 16);
			if (!*optarg || (e && *e)) {
				printf("Invalid argument: \"%s\"\n", optarg);
				errorcnt++;
			}
			break;
		case 'v':
			vid = (uint16_t) strtoul(optarg, &e, 16);
			if (!*optarg || (e && *e)) {
				printf("Invalid argument: \"%s\"\n", optarg);
				errorcnt++;
			}
			break;
		case 'e':
			ep_num = (uint8_t) strtoul(optarg, &e, 0);
			if (!*optarg || (e && *e)) {
				printf("Invalid argument: \"%s\"\n", optarg);
				errorcnt++;
			}
			break;
		case 'h':
			usage(errorcnt);
			break;
		case 0:				/* auto-handled option */
			break;
		case '?':
			if (optopt)
				printf("Unrecognized option: -%c\n", optopt);
			else
				printf("Unrecognized option: %s\n",
				       argv[optind - 1]);
			errorcnt++;
			break;
		case ':':
			printf("Missing argument to %s\n", argv[optind - 1]);
			errorcnt++;
			break;
		default:
			printf("Internal error at %s:%d\n", __FILE__, __LINE__);
			exit(1);
		}
	}

	if (errorcnt)
		usage(errorcnt);

	printf("init\n");
	r = libusb_init(NULL);
	if (r < 0) {
		BOO("init", r);
		exit(1);
	}

	printf("open_device %04x:%04x\n", vid, pid);
	devh = libusb_open_device_with_vid_pid(NULL, vid, pid);
	if (!devh) {
		printf("can't find device\n");
		goto out;
	}

	iface_num = find_interface_with_endpoint(ep_num);
	if (iface_num < 0) {
		printf("can't find interface owning EP %d\n", ep_num);
		goto out;
	}
	/* NOTE: The EP might be on an alternate interface. We should switch
	 * to the correct one. */

	printf("claim_interface %d to use endpoint %d\n", iface_num, ep_num);
	r = libusb_claim_interface(devh, iface_num);
	if (r < 0) {
		BOO("claim interface", r);
		goto out;
	}
	claimed_iface = 1;

	sigact.sa_handler = sighandler;
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = 0;
	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);
	sigaction(SIGQUIT, &sigact, NULL);

	printf("alloc_transfers\n");
	rx_transfer = libusb_alloc_transfer(0);
	if (!rx_transfer) {
		printf("can't alloc rx_transfer");
		goto out;
	}
	libusb_fill_bulk_transfer(rx_transfer, devh,
				  0x80 | ep_num,
				  rx_buf, sizeof(rx_buf), cb_rx, NULL, 0);

	tx_transfer = libusb_alloc_transfer(0);
	if (!tx_transfer) {
		printf("can't alloc tx_transfer");
		goto out;
	}

	printf("get_pollfds\n");
	usb_fds = libusb_get_pollfds(NULL);
	if (!usb_fds) {
		printf("can't get usb_fds\n");
		goto out;
	}

	printf("submit rx_transfer\n");
	r = libusb_submit_transfer(rx_transfer);
	if (r < 0) {
		BOO("submit rx_transfer", r);
		goto out;
	}

	printf("READY\n-------\n");
	while (!do_exit) {
		r = wait_for_stuff_to_happen();
		switch (r) {
		case 0:	/* timed out */
			/* printf("."); */
			/* fflush(stdout); */
			break;
		case 1:	/* stdin ready */
			handle_stdin();
			break;
		case 2:	/* libusb ready */
			handle_libusb();
			break;
		}
	}

	printf("-------\nshutting down\n");

	r = libusb_cancel_transfer(rx_transfer);
	if (r < 0) {
		BOO("cancel rx_transfer", r);
		if (rx_transfer)
			libusb_free_transfer(rx_transfer);
		rx_transfer = 0;
	}

	if (tx_ready) {
		r = libusb_cancel_transfer(tx_transfer);
		if (r < 0) {
			BOO("cancel tx_transfer", r);
			if (tx_transfer)
				libusb_free_transfer(tx_transfer);
			tx_transfer = 0;
		}
	}

	while (rx_transfer) {
		printf("draining events...\n");
		r = libusb_handle_events(NULL);
		if (r < 0) {
			printf("Huh: %s\n", libusb_error_name(r));
			break;
		}
	}

	printf("bye\n");
	r = 0;
 out:
	if (tx_transfer)
		libusb_free_transfer(tx_transfer);
	if (rx_transfer)
		libusb_free_transfer(rx_transfer);

	if (devh) {
		if (claimed_iface)
			libusb_release_interface(devh, iface_num);
		libusb_close(devh);
	}
	libusb_exit(NULL);

	return r;
}