aboutsummaryrefslogtreecommitdiffstats
path: root/halfkay.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2017-02-11 16:13:49 +0000
committerTomasz Kramkowski <tk@the-tk.com>2017-02-11 16:13:49 +0000
commita6b325d7bfc68282ccbedec8778e993529af26b2 (patch)
tree3fadbf7bc6148f97ce7f2ed723574f37c64698ff /halfkay.c
parentb8ede3c284299173f3981aa85dcbd16806a57f55 (diff)
downloadhktool-a6b325d7bfc68282ccbedec8778e993529af26b2.tar.gz
hktool-a6b325d7bfc68282ccbedec8778e993529af26b2.tar.xz
hktool-a6b325d7bfc68282ccbedec8778e993529af26b2.zip
halfkay: Replace usleep with udelay based on nanosleep
A point was raised that usleep is deprecated, a new function was written to replace it: udelay.
Diffstat (limited to 'halfkay.c')
-rw-r--r--halfkay.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/halfkay.c b/halfkay.c
index 8158888..3f0aeec 100644
--- a/halfkay.c
+++ b/halfkay.c
@@ -86,16 +86,32 @@ static void usbclose(void)
hk_usb_hnd = NULL;
}
+/* TODO: move somewhere or consider a cleaner replacement */
+/* udelay: usleep replacement */
+static void udelay(unsigned long usec)
+{
+ enum { USECSEC = 1000000, NSECUSEC = 1000, };
+ struct timespec ts = {
+ .tv_sec = usec / USECSEC,
+ .tv_nsec = usec % USECSEC * NSECUSEC,
+ };
+ /* If we get interrupted, oh well */
+ nanosleep(&ts, NULL);
+}
+
static void usbsendcmd(void *data, size_t size, bool firstxfer)
{
unsigned int timeout = firstxfer ? TIMEOUT_FIRST : TIMEOUT;
int rc, attempt = 0;
- useconds_t delay = SEND_DELAY;
+ unsigned long delay = SEND_DELAY;
+
+ _Static_assert(SEND_DELAY << SEND_ATTEMPTS <= (1UL << 32) - 1,
+ "SEND_DELAY << SEND_ATTEMPTS > 2^32 - 1");
do {
rc = libusb_control_transfer(hk_usb_hnd, HK_USB_REQTYP, HK_USB_REQ,
HK_USB_VAL, 0, data, size, timeout);
- usleep(delay);
+ udelay(delay);
delay *= 2;
} while (rc == LIBUSB_ERROR_PIPE && ++attempt < SEND_ATTEMPTS);