#include <ucw/clists.h>
#include <ucw/gary.h>
#include <ucw/mainloop.h>
+#include <ucw/stkstring.h>
#include <ucw/string.h>
#include <stdio.h>
static libusb_context *usb_ctx;
static libusb_device_handle *usb_dev;
+static bool usb_iface_claimed;
+
+static void noct_error(int usb_err, char *text);
static struct main_file **usb_fds;
{ 2, 0x7f, 0x00 },
};
+static struct libusb_transfer *noct_read_xfer;
+static bool noct_read_pending;
+
static void noct_read_done(struct libusb_transfer *xfer)
{
byte *pkt = xfer->buffer;
int len = xfer->actual_length;
DBG("USB: Read done: status %d, length %d", xfer->status, len);
+ noct_read_pending = 0;
if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
- {
- msg(L_ERROR, "USB read failed with status %d, not submitting again", xfer->status);
- return;
- }
+ return noct_error(0, stk_printf("USB read failed with status %d", xfer->status));
#ifdef LOCAL_DEBUG
char buf[256];
int err;
if ((err = libusb_submit_transfer(xfer)) < 0)
- die("Cannot submit transfer: error %d", err);
+ noct_error(err, "Cannot submit transfer");
+ else
+ noct_read_pending = 1;
}
static void noct_read_init(void)
{
DBG("Noct: Read init");
- struct libusb_transfer *xfer = libusb_alloc_transfer(0);
- libusb_fill_interrupt_transfer(xfer, usb_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
+ noct_read_xfer = libusb_alloc_transfer(0);
+ libusb_fill_interrupt_transfer(noct_read_xfer, usb_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
int err;
- if ((err = libusb_submit_transfer(xfer)) < 0)
- die("Cannot submit transfer: error %d", err);
+ if ((err = libusb_submit_transfer(noct_read_xfer)) < 0)
+ noct_error(err, "Cannot submit transfer");
+ else
+ noct_read_pending = 1;
}
static byte noct_button_state[16];
static uns noct_dirty_ring_val;
static struct libusb_transfer *noct_write_xfer;
-static uns noct_write_pending;
+static bool noct_write_pending;
static void noct_sched_write(void);
static void noct_write_done(struct libusb_transfer *xfer)
DBG("USB: Write done: status %d, length %d", xfer->status, len);
if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
- {
- msg(L_ERROR, "USB write failed with status %d", xfer->status);
- // FIXME: Handle the error in a meaningful way
- return;
- }
+ return noct_error(0, stk_printf("USB write failed with status %d", xfer->status));
if (len < xfer->length)
msg(L_ERROR, "USB partial write: %d out of %d", len, xfer->length);
int err;
if ((err = libusb_submit_transfer(xfer)) < 0)
- die("Cannot submit transfer: error %d", err);
+ noct_error(err, "Cannot submit transfer");
}
static void noct_sched_write(void)
}
static struct main_timer noct_connect_timer;
+static struct main_hook noct_error_hook;
static void noct_connect(struct main_timer *t)
{
msg(L_DEBUG, "Initializing Nocturn");
if ((err = libusb_open(found_dev, &usb_dev)) < 0)
- die("libusb_open failed: error %d", err);
+ return noct_error(err, "libusb_open failed");
// There exist configurations 1 (high brightness) and 2 (power-save)
if ((err = libusb_set_configuration(usb_dev, 1)) < 0)
- die("libusb_set_configuration: error %d", err);
+ return noct_error(err, "libusb_set_configuration failed");
if ((err = libusb_claim_interface(usb_dev, 0)) < 0)
- die("libusb_claim_interface: error %d", err);
+ return noct_error(err, "libusb_claim_interface failed");
+ usb_iface_claimed = 1;
for (int i=0; i<4; i++)
{
int done;
if ((err = libusb_interrupt_transfer(usb_dev, 0x02, (byte *) noct_magic[i] + 1, noct_magic[i][0], &done, 5000)) < 0)
- die("Cannot send init packets: error %d", err);
+ return noct_error(err, "Cannot send init packets");
if (done != noct_magic[i][0])
- die("Partial send of init packet: %d < %d", done, noct_magic[i][0]);
+ return noct_error(err, stk_printf("Partial send of init packet (%d < %d)", done, noct_magic[i][0]));
}
#if 0
schedule_update();
}
+static int noct_error_handler(struct main_hook *h)
+{
+ DBG("Noct: Entered error handling hook");
+ hook_del(h);
+
+ if (usb_dev)
+ {
+ if (noct_read_xfer)
+ {
+ if (noct_read_pending)
+ {
+ DBG("Noct: Cancelling pending read");
+ libusb_cancel_transfer(noct_read_xfer);
+ noct_read_pending = 0;
+ }
+ DBG("Noct: Tearing down read xfer");
+ libusb_free_transfer(noct_read_xfer);
+ noct_read_xfer = NULL;
+ }
+ if (noct_write_xfer)
+ {
+ if (noct_write_pending)
+ {
+ DBG("Noct: Cancelling pending write");
+ libusb_cancel_transfer(noct_write_xfer);
+ noct_write_pending = 0;
+ }
+ DBG("Noct: Tearing down write xfer");
+ libusb_free_transfer(noct_write_xfer);
+ noct_write_xfer = NULL;
+ }
+ if (usb_iface_claimed)
+ {
+ DBG("Noct: Unclaiming interface");
+ libusb_release_interface(usb_dev, 0);
+ usb_iface_claimed = 0;
+ }
+ DBG("Noct: Resetting device");
+ libusb_reset_device(usb_dev);
+ libusb_close(usb_dev);
+ usb_dev = NULL;
+ }
+
+ DBG("Noct: Scheduling rescan after error");
+ timer_add_rel(&noct_connect_timer, 3000);
+
+ return HOOK_IDLE;
+}
+
+static void noct_error(int usb_err, char *text)
+{
+ if (usb_err)
+ msg(L_ERROR, "Nocturn: %s: error %d (%s)", text, usb_err, libusb_error_name(usb_err));
+ else
+ msg(L_ERROR, "Nocturn: %s", text);
+
+ DBG("Noct: Scheduling error handling hook");
+ hook_add(&noct_error_hook);
+}
+
bool noct_is_ready(void)
{
return !!usb_dev; // FIXME
usb_added_fd(fds[i]->fd, fds[i]->events, NULL);
free(fds);
+ // Prepare error handling hook
+ noct_error_hook.handler = noct_error_handler;
+
// Schedule search for the Nocturn
noct_connect_timer.handler = noct_connect;
timer_add_rel(&noct_connect_timer, 0);