#include <ucw/lib.h>
#include <ucw/bitops.h>
-#include <ucw/clists.h>
-#include <ucw/gary.h>
#include <ucw/mainloop.h>
#include <ucw/stkstring.h>
-#include <ucw/string.h>
#include <stdio.h>
#include <string.h>
-#include <stdlib.h>
-#include <sys/poll.h>
-
-#include <libusb.h>
#include "ursaryd.h"
+#include "usb.h"
-static libusb_context *usb_ctx;
-static libusb_device_handle *usb_dev;
-static bool usb_iface_claimed;
+static libusb_device_handle *noct_dev;
+static bool noct_iface_claimed;
static void noct_error(int usb_err, char *text);
-static struct main_file **usb_fds;
-
-static int usb_fd_ready(struct main_file *f UNUSED)
-{
- DBG("USB: Handling events (ready on fd %d)", f->fd);
- struct timeval tv = { 0, 0 };
- int comp = 0;
- int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp);
- if (err < 0)
- msg(L_ERROR, "libusb_handle_events: error %d", err);
- return HOOK_IDLE;
-}
-
-static void usb_added_fd(int fd, short events, void *user_data UNUSED)
-{
- if (fd >= (int) GARY_SIZE(usb_fds))
- GARY_RESIZE(usb_fds, fd + 1);
-
- struct main_file *f = usb_fds[fd];
- if (!f)
- {
- f = xmalloc_zero(sizeof(*f));
- usb_fds[fd] = f;
- }
- else if (file_is_active(f))
- {
- DBG("USB: Releasing fd %d", fd);
- file_del(f);
- }
-
- DBG("USB: Adding fd %d with event mask %u", fd, events);
- f->fd = fd;
- f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL;
- f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL;
- file_add(f);
-}
-
-static void usb_removed_fd(int fd, void *user_data UNUSED)
-{
- DBG("USB: Releasing fd %d", fd);
- ASSERT(fd < (int) GARY_SIZE(usb_fds));
- struct main_file *f = usb_fds[fd];
- ASSERT(f);
- ASSERT(file_is_active(f));
- file_del(f);
-}
-
static const char noct_magic[4][9] = {
{ 3, 0xb0, 0x00, 0x00 },
{ 8, 0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35 },
static struct libusb_transfer *noct_read_xfer;
static bool noct_read_pending;
+char noct_rotary_touched[10];
+char noct_button_pressed[16];
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);
+ DBG("Noct: Read done: status %d, length %d", xfer->status, len);
noct_read_pending = 0;
if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
#ifdef LOCAL_DEBUG
char buf[256];
mem_to_hex(buf, pkt, len, ' ');
- DBG("USB: Read <%s>", buf);
+ DBG("Noct: Read <%s>", buf);
#endif
int i = 0;
if (arg < 0x80)
{
DBG("Noct: Slider value = %d", arg);
+ notify_rotary(9, arg);
continue;
}
break;
{
int state = !!arg;
DBG("Noct: Center touch = %d", state);
+ noct_rotary_touched[8] = state;
+ notify_touch(8, state);
continue;
}
break;
{
int state = !!arg;
DBG("Noct: Slider touch = %d", state);
+ noct_rotary_touched[9] = state;
+ notify_touch(9, state);
continue;
}
break;
int r = cmd - 0x60;
int state = !!arg;
DBG("Noct: Rotary %d touch = %d", r, state);
+ noct_rotary_touched[r] = state;
+ notify_touch(r, state);
continue;
}
break;
int b = cmd - 0x70;
int state = !!arg;
DBG("Noct: Button %d = %d", b, state);
+ noct_button_pressed[b] = state;
notify_button(b, state);
continue;
}
DBG("Noct: Read init");
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);
+ libusb_fill_interrupt_transfer(noct_read_xfer, noct_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
int err;
if ((err = libusb_submit_transfer(noct_read_xfer)) < 0)
noct_read_pending = 1;
}
-static byte noct_button_state[16];
+static byte noct_button_light[16];
static byte noct_ring_mode[8]; // RING_MODE_xxx
static byte noct_ring_val[9];
-static uns noct_dirty_button;
-static uns noct_dirty_ring_mode;
-static uns noct_dirty_ring_val;
+static uint noct_dirty_button;
+static uint noct_dirty_ring_mode;
+static uint noct_dirty_ring_val;
static struct libusb_transfer *noct_write_xfer;
static bool noct_write_pending;
static void noct_write_done(struct libusb_transfer *xfer)
{
int len = xfer->actual_length;
- DBG("USB: Write done: status %d, length %d", xfer->status, len);
+ DBG("Noct: Write done: status %d, length %d", xfer->status, len);
if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
return noct_error(0, stk_printf("USB write failed with status %d", xfer->status));
noct_sched_write();
}
-static void noct_do_write(uns cmd, uns arg)
+static void noct_do_write(uint cmd, uint arg)
{
- DBG("USB: Submitting write %02x %02x", cmd, arg);
+ DBG("Noct: Submitting write %02x %02x", cmd, arg);
ASSERT(!noct_write_pending);
noct_write_pending = 1;
static void noct_sched_write(void)
{
- if (noct_write_pending)
+ if (!noct_dev || noct_write_pending)
return;
if (noct_dirty_button)
{
int i = bit_ffs(noct_dirty_button);
noct_dirty_button ^= 1U << i;
- noct_do_write(0x70 + i, noct_button_state[i]);
+ noct_do_write(0x70 + i, noct_button_light[i]);
}
else if (noct_dirty_ring_mode)
{
{
ASSERT(button >= 0 && button < 16);
ASSERT(val == 0 || val == 1);
- if (noct_button_state[button] != val)
+ if (noct_button_light[button] != val)
{
- noct_button_state[button] = val;
+ noct_button_light[button] = val;
noct_dirty_button |= 1U << button;
noct_sched_write();
}
}
+void noct_clear(void)
+{
+ for (int i=0; i<=8; i++)
+ noct_set_ring(i, RING_MODE_LEFT, 0);
+ for (int i=0; i<16; i++)
+ noct_set_button(i, 0);
+}
+
static void noct_write_init(void)
{
DBG("Noct: Write init");
noct_write_xfer = libusb_alloc_transfer(0);
- libusb_fill_interrupt_transfer(noct_write_xfer, usb_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
+ libusb_fill_interrupt_transfer(noct_write_xfer, noct_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
+ bzero(noct_button_pressed, sizeof(noct_button_pressed));
+ bzero(noct_rotary_touched, sizeof(noct_rotary_touched));
noct_dirty_button = 0xffff;
noct_dirty_ring_mode = 0xff;
noct_dirty_ring_val = 0x1ff;
desc.idVendor == 0x1235 &&
desc.idProduct == 0x000a)
{
- msg(L_DEBUG, "Found device: bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
+ msg(L_INFO, "Nocturn found at bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
if (found_dev)
{
msg(L_ERROR, "Multiple Nocturn devices found. Using the first one.");
return;
}
- msg(L_DEBUG, "Initializing Nocturn");
+ DBG("Initializing Nocturn");
- if ((err = libusb_open(found_dev, &usb_dev)) < 0)
+ if ((err = libusb_open(found_dev, &noct_dev)) < 0)
return noct_error(err, "libusb_open failed");
+ // In newer kernels, Nocturn is claimed by snd-usb-audio. Tell it to surrender the device.
+ libusb_detach_kernel_driver(noct_dev, 0);
+
// There exist configurations 1 (high brightness) and 2 (power-save)
- if ((err = libusb_set_configuration(usb_dev, 1)) < 0)
+ if ((err = libusb_set_configuration(noct_dev, 2)) < 0)
return noct_error(err, "libusb_set_configuration failed");
- if ((err = libusb_claim_interface(usb_dev, 0)) < 0)
+ if ((err = libusb_claim_interface(noct_dev, 0)) < 0)
return noct_error(err, "libusb_claim_interface failed");
- usb_iface_claimed = 1;
+ noct_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)
+ if ((err = libusb_interrupt_transfer(noct_dev, 0x02, (byte *) noct_magic[i] + 1, noct_magic[i][0], &done, 5000)) < 0)
return noct_error(err, "Cannot send init packets");
if (done != noct_magic[i][0])
return noct_error(err, stk_printf("Partial send of init packet (%d < %d)", done, noct_magic[i][0]));
DBG("Noct: Entered error handling hook");
hook_del(h);
- if (usb_dev)
+ if (noct_dev)
{
if (noct_read_xfer)
{
noct_read_pending = 0;
}
DBG("Noct: Tearing down read xfer");
+ xfree(noct_read_xfer->buffer);
libusb_free_transfer(noct_read_xfer);
noct_read_xfer = NULL;
}
noct_write_pending = 0;
}
DBG("Noct: Tearing down write xfer");
+ xfree(noct_write_xfer->buffer);
libusb_free_transfer(noct_write_xfer);
noct_write_xfer = NULL;
}
- if (usb_iface_claimed)
+ if (noct_iface_claimed)
{
DBG("Noct: Unclaiming interface");
- libusb_release_interface(usb_dev, 0);
- usb_iface_claimed = 0;
+ libusb_release_interface(noct_dev, 0);
+ noct_iface_claimed = 0;
}
DBG("Noct: Resetting device");
- libusb_reset_device(usb_dev);
- libusb_close(usb_dev);
- usb_dev = NULL;
+ libusb_reset_device(noct_dev);
+ libusb_close(noct_dev);
+ noct_dev = NULL;
}
DBG("Noct: Scheduling rescan after error");
bool noct_is_ready(void)
{
- return !!usb_dev;
+ return !!noct_dev;
}
void noct_init(void)
{
- int err;
-
- // Initialize libusb
- if ((err = libusb_init(&usb_ctx)) < 0)
- die("libusb_init failed: error %d", err);
- libusb_set_debug(usb_ctx, 3);
-
- // Connect libusb to UCW mainloop
-
- if (!libusb_pollfds_handle_timeouts(usb_ctx))
- die("Unsupported version of libusb, please fix me");
-
- GARY_INIT_ZERO(usb_fds, 0);
- libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL);
-
- const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx);
- ASSERT(fds);
- for (int i=0; fds[i]; i++)
- usb_added_fd(fds[i]->fd, fds[i]->events, NULL);
- free(fds);
-
// Prepare error handling hook
noct_error_hook.handler = noct_error_handler;