X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=nocturn.c;h=a313b4949c8f6bd757a4d28b99d3c40283e407ea;hb=594d928cfd447d5f6ef2e493e4cdbeb135e07ba0;hp=8ed7b1835eb51ae2b96180a8482915f48892adfa;hpb=561f0371e40ccc50e7931ff8ca8b113b41aec587;p=ursary.git diff --git a/nocturn.c b/nocturn.c index 8ed7b18..a313b49 100644 --- a/nocturn.c +++ b/nocturn.c @@ -7,13 +7,14 @@ * see https://github.com/dewert/nocturn-linux-midi for inspiration. */ -#define LOCAL_DEBUG +#undef LOCAL_DEBUG #include #include #include #include #include +#include #include #include @@ -27,6 +28,9 @@ 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; @@ -82,17 +86,20 @@ static const char noct_magic[4][9] = { { 2, 0x7f, 0x00 }, }; +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); + 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]; @@ -127,6 +134,7 @@ static void noct_read_done(struct libusb_transfer *xfer) int r = cmd - 0x40; int delta = (arg < 0x40 ? arg : arg - 0x80); DBG("Noct: Rotary %d = %d", r, delta); + notify_rotary(r, delta); continue; } break; @@ -134,6 +142,7 @@ static void noct_read_done(struct libusb_transfer *xfer) if (arg < 0x80) { DBG("Noct: Slider value = %d", arg); + notify_rotary(9, arg); continue; } break; @@ -145,6 +154,7 @@ static void noct_read_done(struct libusb_transfer *xfer) { int delta = (arg < 0x40 ? arg : arg - 0x80); DBG("Noct: Center = %d", delta); + notify_rotary(8, delta); continue; } break; @@ -153,6 +163,8 @@ static void noct_read_done(struct libusb_transfer *xfer) { int state = !!arg; DBG("Noct: Center touch = %d", state); + noct_rotary_touched[8] = state; + notify_touch(8, state); continue; } break; @@ -161,6 +173,8 @@ static void noct_read_done(struct libusb_transfer *xfer) { int state = !!arg; DBG("Noct: Slider touch = %d", state); + noct_rotary_touched[9] = state; + notify_touch(9, state); continue; } break; @@ -170,6 +184,8 @@ static void noct_read_done(struct libusb_transfer *xfer) 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; @@ -179,6 +195,8 @@ static void noct_read_done(struct libusb_transfer *xfer) int b = cmd - 0x70; int state = !!arg; DBG("Noct: Button %d = %d", b, state); + noct_button_pressed[b] = state; + notify_button(b, state); continue; } break; @@ -188,31 +206,35 @@ static void noct_read_done(struct libusb_transfer *xfer) 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 byte noct_ring_mode[8]; // 0=from-min, 1=from-max, 2=from-mid-right, 3=from-mid-both, 4=single-on, 5=single-off +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 uns noct_write_pending; +static bool noct_write_pending; static void noct_sched_write(void); static void noct_write_done(struct libusb_transfer *xfer) @@ -221,16 +243,15 @@ 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); - 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); noct_write_pending = 0; 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); ASSERT(!noct_write_pending); @@ -244,19 +265,19 @@ static void noct_do_write(uns cmd, uns arg) 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) { - if (noct_write_pending) + if (!usb_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) { @@ -269,12 +290,52 @@ static void noct_sched_write(void) int i = bit_ffs(noct_dirty_ring_val); noct_dirty_ring_val ^= 1U << i; if (i == 8) - noct_do_write(0x50 + i, noct_ring_val[i]); + noct_do_write(0x50, noct_ring_val[i]); else noct_do_write(0x40 + i, noct_ring_val[i]); } } +void noct_set_ring(int ring, int mode, int val) +{ + ASSERT(ring >= 0 && ring <= 8); + ASSERT(val >= 0 && val <= 0x7f); + ASSERT(mode >= 0 && mode <= 5); + ASSERT(ring < 8 || !mode); + if (noct_ring_mode[ring] != mode) + { + noct_ring_mode[ring] = mode; + noct_dirty_ring_mode |= 1U << ring; + noct_dirty_ring_val |= 1U << ring; // HW needs to re-send the value + } + if (noct_ring_val[ring] != val) + { + noct_ring_val[ring] = val; + noct_dirty_ring_val |= 1U << ring; + } + noct_sched_write(); +} + +void noct_set_button(int button, int val) +{ + ASSERT(button >= 0 && button < 16); + ASSERT(val == 0 || val == 1); + if (noct_button_light[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"); @@ -282,26 +343,23 @@ static void noct_write_init(void) 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); -#if 0 // FIXME - noct_button_state[2] = 1; - noct_ring_mode[0] = 4; - noct_ring_val[0] = 0x40; -#endif - + 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; noct_sched_write(); } -void noct_init(void) +static struct main_timer noct_connect_timer; +static struct main_hook noct_error_hook; + +static void noct_connect(struct main_timer *t) { + timer_del(t); + msg(L_DEBUG, "Looking for Nocturn"); int err; - if ((err = libusb_init(&usb_ctx)) < 0) - die("libusb_init failed: error %d", err); - libusb_set_debug(usb_ctx, 3); - libusb_device **dev_list; libusb_device *found_dev = NULL; ssize_t len = libusb_get_device_list(usb_ctx, &dev_list); @@ -313,45 +371,128 @@ void noct_init(void) 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) - die("Multiple Nocturn devices found. Please fix me to handle it."); + { + msg(L_ERROR, "Multiple Nocturn devices found. Using the first one."); + break; + } found_dev = libusb_ref_device(dev); } } libusb_free_device_list(dev_list, 1); if (!found_dev) - die("No Nocturn device found"); + { + msg(L_INFO, "No Nocturn device found"); + timer_add_rel(t, 5000); + return; + } - msg(L_DEBUG, "Initializing device"); + DBG("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); + if ((err = libusb_set_configuration(usb_dev, 2)) < 0) + 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 - byte xxx[] = { 0x7f, 0x01 }; - int done; - libusb_interrupt_transfer(usb_dev, 0x02, xxx, 2, &done, 5000); -#endif + noct_read_init(); + noct_write_init(); + 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"); + xfree(noct_read_xfer->buffer); + 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"); + xfree(noct_write_xfer->buffer); + 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); - DBG("USB: Connecting libusb to mainloop"); + 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; +} + +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"); @@ -365,6 +506,10 @@ void noct_init(void) usb_added_fd(fds[i]->fd, fds[i]->events, NULL); free(fds); - noct_read_init(); - noct_write_init(); + // 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, 100); }