summaryrefslogtreecommitdiff
path: root/libfpusb/io.c
blob: f5949eab5fc00412d2c07b776b9a1aa45a0ce5a7 (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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
 * I/O functions for libfpusb
 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
 *
 * Portions based on libusb-0.1
 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

/* signalfd() support is present in glibc-2.7 onwards, but glibc-2.7 contains
 * a bug where the header is neither installed or compilable. This will be
 * fixed for glibc-2.8. */
#if __GLIBC_PREREQ(2, 8)
#include <sys/signalfd.h>
#else
#include "signalfd.h"
#endif

#include "fpusbi.h"

static int sigfd;
static int signum;

/* this is a list of in-flight fp_urb_handles, sorted by timeout expiration.
 * URBs to timeout the soonest are placed at the beginning of the list, URBs
 * that will time out later are placed after, and urbs with infinite timeout
 * are always placed at the very end. */
static struct list_head flying_urbs;

static int setup_signalfd(int _signum)
{
	sigset_t sigset;
	if (_signum == 0)
		_signum = SIGRTMIN;
	fp_dbg("signal %d", _signum);

	sigemptyset(&sigset);
	sigaddset(&sigset, _signum);
	sigfd = signalfd(-1, &sigset, 0);
	if (sigfd < 0) {
		fp_err("signalfd failed, code=%d errno=%d", sigfd, errno);
		return sigfd;
	}
	fp_dbg("got signalfd %d", sigfd);
	signum = _signum;

	sigemptyset(&sigset);
	sigaddset(&sigset, _signum);
	return sigprocmask(SIG_BLOCK, &sigset, NULL);
}

int fpi_io_init(int _signum)
{
	list_init(&flying_urbs);
	return setup_signalfd(signum);
}

void fpi_io_exit(void)
{
	close(sigfd);
}

static int calculate_timeout(struct fpusb_urb_handle *urbh,
	unsigned int timeout)
{
	int r;
	struct timespec current_time;
	struct sigevent sigevt = {
		.sigev_notify = SIGEV_SIGNAL,
		.sigev_signo = signum,
	};
	struct itimerspec itspec;
	struct timespec *it_value = &itspec.it_value;

	if (!timeout)
		return 0;

	r = clock_gettime(CLOCK_MONOTONIC, &current_time);
	if (r < 0) {
		fp_err("failed to read monotonic clock, errno=%d", errno);
		return r;
	}

	r = timer_create(CLOCK_MONOTONIC, &sigevt, &urbh->timer);
	if (r < 0) {
		fp_err("failed to create monotonic timer");
		return r;
	}

	memset(&itspec, 0, sizeof(itspec));
	it_value->tv_sec = current_time.tv_sec + (timeout / 1000);
	it_value->tv_nsec = current_time.tv_nsec +
		((timeout % 1000) * 1000000);

	if (it_value->tv_nsec > 1000000000) {
		it_value->tv_nsec -= 1000000000;
		it_value->tv_sec++;
	}

	r = timer_settime(&urbh->timer, TIMER_ABSTIME, &itspec, NULL);
	if (r < 0) {
		fp_err("failed to arm monotonic timer");
		return r;
	}

	urbh->timeout = itspec.it_value;

	return 0;
}

static void add_to_flying_list(struct fpusb_urb_handle *urbh)
{
	struct fpusb_urb_handle *cur;
	struct timespec *timeout = &urbh->timeout;

	/* if we have no other flying urbs, start the list with this one */
	if (list_empty(&flying_urbs)) {
		list_add(&urbh->list, &flying_urbs);
		return;
	}

	/* if we have infinite timeout, append to end of list */
	if (!TIMESPEC_IS_SET(timeout)) {
		list_add_tail(&urbh->list, &flying_urbs);
		return;
	}

	/* otherwise, find appropriate place in list */
	list_for_each_entry(cur, &flying_urbs, list) {
		/* find first timeout that occurs after the urbh in question */
		struct timespec *cur_ts = &cur->timeout;

		if (!TIMESPEC_IS_SET(cur_ts) || (cur_ts->tv_sec > timeout->tv_sec) ||
				(cur_ts->tv_sec == timeout->tv_sec &&
					cur_ts->tv_nsec > timeout->tv_nsec)) {
			list_add_tail(&urbh->list, &cur->list);
			return;
		}
	}

	/* otherwise we need to be inserted at the end */
	list_add_tail(&urbh->list, &flying_urbs);
}

static int submit_urb(struct fpusb_dev_handle *devh,
	struct fpusb_urb_handle *urbh)
{
	int r;
	struct usb_urb *urb = &urbh->urb;
	int to_be_transferred = urbh->transfer_len - urbh->transferred;

	urb->type = urbh->urb_type;
	urb->endpoint = urbh->endpoint;
	urb->buffer = urbh->buffer + urbh->transferred;
	urb->buffer_length = MIN(to_be_transferred, MAX_URB_BUFFER_LENGTH);
	urb->signr = signum;

	/* FIXME: for requests that we have to split into multiple URBs, we should
	 * submit all the URBs instantly: submit, submit, submit, reap, reap, reap
	 * rather than: submit, reap, submit, reap, submit, reap
	 * this will improve performance and fix bugs concerning behaviour when
	 * the user submits two similar multiple-urb requests */
	fp_dbg("transferring %d from %d bytes", urb->buffer_length,
		to_be_transferred);

	r = ioctl(devh->fd, IOCTL_USB_SUBMITURB, &urbh->urb);
	if (r < 0) {
		fp_err("submiturb failed error %d errno=%d", r, errno);
		return r;
	}

	add_to_flying_list(urbh);
	return 0;
}

API_EXPORTED struct fpusb_urb_handle *fpusb_submit_ctrl_msg(
	struct fpusb_dev_handle *devh, struct fpusb_ctrl_msg *msg,
	fpusb_ctrl_cb_fn callback, void *user_data, unsigned int timeout)
{
	struct fpusb_urb_handle *urbh = malloc(sizeof(*urbh));
	struct usb_ctrl_setup *setup;
	unsigned char *urbdata;
	int urbdata_length = sizeof(struct usb_ctrl_setup) + msg->length;
	int r;

	if (!urbh)
		return NULL;
	memset(urbh, 0, sizeof(*urbh));
	urbh->devh = devh;
	urbh->callback = callback;
	urbh->user_data = user_data;
	r = calculate_timeout(urbh, timeout);
	if (r < 0) {
		free(urbh);
		return NULL;
	}

	urbdata = malloc(urbdata_length);
	if (!urbdata) {
		free(urbh);
		return NULL;
	}

	fp_dbg("RQT=%02x RQ=%02x VAL=%04x IDX=%04x length=%d",
		msg->requesttype, msg->request, msg->value, msg->index, msg->length);

	setup = (struct usb_ctrl_setup *) urbdata;
	setup->bRequestType = msg->requesttype;
	setup->bRequest = msg->request;
	setup->wValue = cpu_to_le16(msg->value);
	setup->wIndex = cpu_to_le16(msg->index);
	setup->wLength = cpu_to_le16(msg->length);

	if ((msg->requesttype & 0x80) == USB_ENDPOINT_OUT)
		memcpy(urbdata + sizeof(struct usb_ctrl_setup), msg->data, msg->length);

	urbh->urb_type = USB_URB_TYPE_CONTROL;
	urbh->buffer = urbdata;
	urbh->transfer_len = urbdata_length;

	r = submit_urb(devh, urbh);
	if (r < 0) {
		free(urbh);
		free(urbdata);
		return NULL;
	}

	return urbh;
}

static struct fpusb_urb_handle *submit_bulk_msg(struct fpusb_dev_handle *devh,
	struct fpusb_bulk_msg *msg, fpusb_bulk_cb_fn callback, void *user_data,
	unsigned int timeout, unsigned char urbtype)
{
	struct fpusb_urb_handle *urbh = malloc(sizeof(*urbh));
	int r;

	fp_dbg("length %d timeout %d", msg->length, timeout);

	if (!urbh)
		return NULL;
	memset(urbh, 0, sizeof(*urbh));
	r = calculate_timeout(urbh, timeout);
	if (r < 0) {
		free(urbh);
		return NULL;
	}
	urbh->devh = devh;
	urbh->callback = callback;
	urbh->user_data = user_data;
	urbh->flags |= FPUSB_URBH_DATA_BELONGS_TO_USER;
	urbh->endpoint = msg->endpoint;
	urbh->urb_type = urbtype;
	urbh->buffer = msg->data;
	urbh->transfer_len = msg->length;

	r = submit_urb(devh, urbh);
	if (r < 0) {
		free(urbh);
		return NULL;
	}

	return urbh;
}

API_EXPORTED struct fpusb_urb_handle *fpusb_submit_bulk_msg(
	struct fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg,
	fpusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
{
	return submit_bulk_msg(devh, msg, callback, user_data, timeout,
		USB_URB_TYPE_BULK);
}

API_EXPORTED struct fpusb_urb_handle *fpusb_submit_intr_msg(
	struct fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg,
	fpusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
{
	return submit_bulk_msg(devh, msg, callback, user_data, timeout,
		USB_URB_TYPE_INTERRUPT);
}

API_EXPORTED int fpusb_urb_handle_cancel(struct fpusb_dev_handle *devh,
	struct fpusb_urb_handle *urbh)
{
	int r;
	fp_dbg("");
	r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
	if (r < 0)
		fp_err("cancel urb failed error %d", r);
	return r;
}

API_EXPORTED int fpusb_urb_handle_cancel_sync(struct fpusb_dev_handle *devh,
	struct fpusb_urb_handle *urbh)
{
	int r;
	fp_dbg("");
	r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
	if (r < 0) {
		fp_err("cancel urb failed error %d", r);
		return r;
	}

	urbh->flags |= FPUSB_URBH_SYNC_CANCELLED;
	while (urbh->flags & FPUSB_URBH_SYNC_CANCELLED) {
		r = fpusb_poll();
		if (r < 0)
			return r;
	}

	return 0;
}

int handle_transfer_completion(struct fpusb_dev_handle *devh,
	struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status)
{
	struct usb_urb *urb = &urbh->urb;

	if (TIMESPEC_IS_SET(&urbh->timeout))
		timer_delete(urbh->timer);

	if (status == FP_URB_SILENT_COMPLETION)
		return 0;

	if (urb->type == USB_URB_TYPE_CONTROL) {
		fpusb_ctrl_cb_fn callback = urbh->callback;
		if (callback)
			callback(devh, urbh, status, urb->buffer,
				urb->buffer + sizeof(struct usb_ctrl_setup), urbh->transferred,
				urbh->user_data);
	} else if (urb->type == USB_URB_TYPE_BULK ||
			urb->type == USB_URB_TYPE_INTERRUPT) {
		fpusb_bulk_cb_fn callback = urbh->callback;
		if (callback)
			callback(devh, urbh, status, urbh->endpoint, urbh->transfer_len,
				urbh->buffer, urbh->transferred, urbh->user_data);
	}
	return 0;
}

static int handle_transfer_cancellation(struct fpusb_dev_handle *devh,
	struct fpusb_urb_handle *urbh)
{
	/* if the URB is being cancelled synchronously, raise cancellation
	 * completion event by unsetting flag, and ensure that user callback does
	 * not get called.
	 */
	if (urbh->flags & FPUSB_URBH_SYNC_CANCELLED) {
		urbh->flags &= ~FPUSB_URBH_SYNC_CANCELLED;
		fp_dbg("detected sync. cancel");
		return handle_transfer_completion(devh, urbh, FP_URB_SILENT_COMPLETION);
	}

	/* if the URB was cancelled due to timeout, report timeout to the user */
	if (urbh->flags & FPUSB_URBH_TIMED_OUT) {
		fp_dbg("detected timeout cancellation");
		return handle_transfer_completion(devh, urbh, FP_URB_TIMEOUT);
	}

	/* otherwise its a normal async cancel */
	return handle_transfer_completion(devh, urbh, FP_URB_CANCELLED);
}

static int reap_for_devh(struct fpusb_dev_handle *devh)
{
	int r;
	struct usb_urb *urb;
	struct fpusb_urb_handle *urbh;
	int trf_requested;

	r = ioctl(devh->fd, IOCTL_USB_REAPURBNDELAY, &urb);
	if (r == -1 && errno == EAGAIN)
		return r;
	if (r < 0) {
		fp_err("reap failed error %d errno=%d", r, errno);
		return r;
	}

	urbh = container_of(urb, struct fpusb_urb_handle, urb);

	fp_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status,
		urb->actual_length);
	list_del(&urbh->list);

	if (urb->status == -2)
		return handle_transfer_cancellation(devh, urbh);
	/* FIXME: research what other status codes may exist */
	if (urb->status != 0)
		fp_warn("unrecognised urb status %d", urb->status);

	/* determine how much data was asked for */
	trf_requested = MIN(urbh->transfer_len - urbh->transferred,
		MAX_URB_BUFFER_LENGTH);

	urbh->transferred += urb->actual_length;	

	/* if we were provided less data than requested, then our transfer is
	 * done */
	if (urb->actual_length < trf_requested) {
		fp_dbg("less data than requested (%d/%d) --> all done",
			urb->actual_length, trf_requested);
		return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
	}

	/* if we've transferred all data, we're done */
	if (urbh->transferred == urbh->transfer_len) {
		fp_dbg("transfer complete --> all done");
		return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
	}

	/* otherwise, we have more data to transfer */
	fp_dbg("more data to transfer...");
	memset(urb, 0, sizeof(*urb));
	return submit_urb(devh, urbh);
}

static void handle_timeout(struct fpusb_urb_handle *urbh)
{
	/* handling timeouts is tricky, as we may race with the kernel: we may
	 * detect a timeout racing with the condition that the urb has actually
	 * completed. we asynchronously cancel the URB and report timeout
	 * to the user when the URB cancellation completes (or not at all if the
	 * URB actually gets delivered as per this race) */
	int r;


	urbh->flags |= FPUSB_URBH_TIMED_OUT;
	r = fpusb_urb_handle_cancel(urbh->devh, urbh);
	if (r < 0)
		fp_warn("async cancel failed %d errno=%d", r, errno);
}

static int handle_timeouts(void)
{
	struct timespec systime;
	struct fpusb_urb_handle *urbh;
	int r;

	if (list_empty(&flying_urbs))
		return 0;

	/* get current time */
	r = clock_gettime(CLOCK_MONOTONIC, &systime);
	if (r < 0)
		return r;

	/* iterate through flying urbs list, finding all urbs that have expired
	 * timeouts */
	list_for_each_entry(urbh, &flying_urbs, list) {
		struct timespec *cur_ts = &urbh->timeout;

		/* if we've reached urbs of infinite timeout, we're all done */
		if (!TIMESPEC_IS_SET(cur_ts))
			return 0;

		/* if urb has non-expired timeout, nothing more to do */
		if ((cur_ts->tv_sec > systime.tv_sec) ||
				(cur_ts->tv_sec == systime.tv_sec &&
					cur_ts->tv_nsec > systime.tv_nsec))
			return 0;
	
		/* otherwise, we've got an expired timeout to handle */
		handle_timeout(urbh);
	}

	return 0;
}

static int reap(void)
{
	struct fpusb_dev_handle *devh;
	int r;

	list_for_each_entry(devh, &open_devs, list) {
		r = reap_for_devh(devh);
		if (r == -1 && errno == EAGAIN)
			continue;
		if (r < 0)
			return r;
	}

	r = handle_timeouts();

	return 0;
}

static int flush_sigfd(void)
{
	int r;
	struct signalfd_siginfo siginfo;
	r = read(sigfd, &siginfo, sizeof(siginfo));
	if (r < 0) {
		fp_err("sigfd read failed %d %d", r, errno);
		return r;
	}
	if ((unsigned int) r < sizeof(siginfo)) {
		fp_err("sigfd short read (%d/%d)", r, sizeof(siginfo));
		return -1;
	}
	return 0;
}

static int poll_io(struct timeval *tv)
{
	int r;
	fd_set fds;

	FD_ZERO(&fds);
	FD_SET(sigfd, &fds);
	r = select(sigfd + 1, &fds, NULL, NULL, tv);
	if (r == -1 && errno == EINTR)
		return 0;
	if (r < 0) {
		fp_err("select failed %d err=%d\n", r, errno);
		return r;
	}

	if (r > 0) {
		flush_sigfd();
		return reap();
	}

	return 0;
}

API_EXPORTED int fpusb_poll_timeout(struct timeval *tv)
{
	return poll_io(tv);
}

API_EXPORTED int fpusb_poll(void)
{
	struct timeval tv;
	tv.tv_sec = 0;
	tv.tv_usec = 500000;
	return poll_io(&tv);
}

struct sync_ctrl_handle {
	enum fp_urb_cb_status status;
	unsigned char *data;
	int actual_length;
};

static void ctrl_msg_cb(struct fpusb_dev_handle *devh,
	struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status,
	struct usb_ctrl_setup *setup, unsigned char *data, int actual_length,
	void *user_data)
{
	struct sync_ctrl_handle *ctrlh = (struct sync_ctrl_handle *) user_data;
	fp_dbg("actual_length=%d", actual_length);

	if (status == FP_URB_COMPLETED) {
		/* copy results into user-defined buffer */
		if (setup->bRequestType & USB_ENDPOINT_IN)
			memcpy(ctrlh->data, data, actual_length);
	}

	ctrlh->status = status;
	ctrlh->actual_length = actual_length;
	/* caller frees urbh */
}

API_EXPORTED int fpusb_ctrl_msg(struct fpusb_dev_handle *devh,
	struct fpusb_ctrl_msg *msg, unsigned int timeout)
{
	struct fpusb_urb_handle *urbh;
	struct sync_ctrl_handle ctrlh;

	memset(&ctrlh, 0, sizeof(ctrlh));
	ctrlh.data = msg->data;

	urbh = fpusb_submit_ctrl_msg(devh, msg, ctrl_msg_cb, &ctrlh, timeout);
	if (!urbh)
		return -1;

	while (!ctrlh.status) {
		int r = fpusb_poll();
		if (r < 0) {
			fpusb_urb_handle_cancel_sync(devh, urbh);
			fpusb_urb_handle_free(urbh);
			return r;
		}
	}

	fpusb_urb_handle_free(urbh);
	switch (ctrlh.status) {
	case FP_URB_COMPLETED:
		return ctrlh.actual_length;
	case FP_URB_TIMEOUT:
		return -ETIMEDOUT;
	default:
		fp_warn("unrecognised status code %d", ctrlh.status);
		return -1;
	}
}

struct sync_bulk_handle {
	enum fp_urb_cb_status status;
	int actual_length;
};

static void bulk_msg_cb(struct fpusb_dev_handle *devh,
	struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status,
	unsigned char endpoint, int rqlength, unsigned char *data,
	int actual_length, void *user_data)
{
	struct sync_bulk_handle *bulkh = (struct sync_bulk_handle *) user_data;
	fp_dbg("");
	bulkh->status = status;
	bulkh->actual_length = actual_length;
	/* caller frees urbh */
}

static int do_sync_bulk_msg(struct fpusb_dev_handle *devh,
	struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout,
	unsigned char urbtype)
{
	struct fpusb_urb_handle *urbh;
	struct sync_bulk_handle bulkh;

	memset(&bulkh, 0, sizeof(bulkh));

	urbh = submit_bulk_msg(devh, msg, bulk_msg_cb, &bulkh, timeout, urbtype);
	if (!urbh)
		return -1;

	while (!bulkh.status) {
		int r = fpusb_poll();
		if (r < 0) {
			fpusb_urb_handle_cancel_sync(devh, urbh);
			fpusb_urb_handle_free(urbh);
			return r;
		}
	}

	*transferred = bulkh.actual_length;
	fpusb_urb_handle_free(urbh);

	switch (bulkh.status) {
	case FP_URB_COMPLETED:
		return 0;
	case FP_URB_TIMEOUT:
		return -ETIMEDOUT;
	default:
		fp_warn("unrecognised status code %d", bulkh.status);
		return -1;
	}
}

API_EXPORTED int fpusb_intr_msg(struct fpusb_dev_handle *devh,
	struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout)
{
	return do_sync_bulk_msg(devh, msg, transferred, timeout,
		USB_URB_TYPE_INTERRUPT);
}

API_EXPORTED int fpusb_bulk_msg(struct fpusb_dev_handle *devh,
	struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout)
{
	return do_sync_bulk_msg(devh, msg, transferred, timeout,
		USB_URB_TYPE_BULK);
}

API_EXPORTED void fpusb_urb_handle_free(struct fpusb_urb_handle *urbh)
{
	if (!urbh)
		return;

	if (!(urbh->flags & FPUSB_URBH_DATA_BELONGS_TO_USER))
		free(urbh->urb.buffer);
	free(urbh);
}

API_EXPORTED int fpusb_get_pollfd(void)
{
	return sigfd;
}