2 * Interface to Novation Nocturn
4 * (c) 2014 Martin Mares <mj@ucw.cz>
6 * Protocol reverse-engineered by De Wet van Niekerk <dewert@gmail.com>,
7 * see https://github.com/dewert/nocturn-linux-midi for inspiration.
13 #include <ucw/bitops.h>
14 #include <ucw/clists.h>
16 #include <ucw/mainloop.h>
17 #include <ucw/stkstring.h>
18 #include <ucw/string.h>
29 static libusb_context *usb_ctx;
30 static libusb_device_handle *usb_dev;
31 static bool usb_iface_claimed;
33 static void noct_error(int usb_err, char *text);
35 static struct main_file **usb_fds;
37 static int usb_fd_ready(struct main_file *f UNUSED)
39 DBG("USB: Handling events (ready on fd %d)", f->fd);
40 struct timeval tv = { 0, 0 };
42 int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp);
44 msg(L_ERROR, "libusb_handle_events: error %d", err);
48 static void usb_added_fd(int fd, short events, void *user_data UNUSED)
50 if (fd >= (int) GARY_SIZE(usb_fds))
51 GARY_RESIZE(usb_fds, fd + 1);
53 struct main_file *f = usb_fds[fd];
56 f = xmalloc_zero(sizeof(*f));
59 else if (file_is_active(f))
61 DBG("USB: Releasing fd %d", fd);
65 DBG("USB: Adding fd %d with event mask %u", fd, events);
67 f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL;
68 f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL;
72 static void usb_removed_fd(int fd, void *user_data UNUSED)
74 DBG("USB: Releasing fd %d", fd);
75 ASSERT(fd < (int) GARY_SIZE(usb_fds));
76 struct main_file *f = usb_fds[fd];
78 ASSERT(file_is_active(f));
82 static const char noct_magic[4][9] = {
83 { 3, 0xb0, 0x00, 0x00 },
84 { 8, 0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35 },
85 { 6, 0x2a, 0x02, 0x2c, 0x72, 0x2e, 0x30 },
89 static struct libusb_transfer *noct_read_xfer;
90 static bool noct_read_pending;
91 char noct_rotary_touched[10];
92 char noct_button_pressed[16];
94 static void noct_read_done(struct libusb_transfer *xfer)
96 byte *pkt = xfer->buffer;
97 int len = xfer->actual_length;
98 DBG("USB: Read done: status %d, length %d", xfer->status, len);
99 noct_read_pending = 0;
101 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
102 return noct_error(0, stk_printf("USB read failed with status %d", xfer->status));
106 mem_to_hex(buf, pkt, len, ' ');
107 DBG("USB: Read <%s>", buf);
115 msg(L_ERROR, "Unknown USB packet: length %d not divisible by 3", len);
120 msg(L_ERROR, "Unknown USB packet: expected 0xb0 at position %d", i);
129 // Unknown packet sent during init
135 int delta = (arg < 0x40 ? arg : arg - 0x80);
136 DBG("Noct: Rotary %d = %d", r, delta);
137 notify_rotary(r, delta);
144 DBG("Noct: Slider value = %d", arg);
145 notify_rotary(9, arg);
150 // Unknown packet, maybe least significant bit of slider
155 int delta = (arg < 0x40 ? arg : arg - 0x80);
156 DBG("Noct: Center = %d", delta);
157 notify_rotary(8, delta);
162 if (arg == 0x00 || arg == 0x7f)
165 DBG("Noct: Center touch = %d", state);
166 noct_rotary_touched[8] = state;
167 notify_touch(8, state);
172 if (arg == 0x00 || arg == 0x7f)
175 DBG("Noct: Slider touch = %d", state);
176 noct_rotary_touched[9] = state;
177 notify_touch(9, state);
182 if (arg == 0x00 || arg == 0x7f)
186 DBG("Noct: Rotary %d touch = %d", r, state);
187 noct_rotary_touched[r] = state;
188 notify_touch(r, state);
193 if (arg == 0x00 || arg == 0x7f)
197 DBG("Noct: Button %d = %d", b, state);
198 noct_button_pressed[b] = state;
199 notify_button(b, state);
204 msg(L_ERROR, "Unknown USB packet: unrecognized cmd=%02x arg=%02x", cmd, arg);
208 if ((err = libusb_submit_transfer(xfer)) < 0)
209 noct_error(err, "Cannot submit transfer");
211 noct_read_pending = 1;
214 static void noct_read_init(void)
216 DBG("Noct: Read init");
218 noct_read_xfer = libusb_alloc_transfer(0);
219 libusb_fill_interrupt_transfer(noct_read_xfer, usb_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
222 if ((err = libusb_submit_transfer(noct_read_xfer)) < 0)
223 noct_error(err, "Cannot submit transfer");
225 noct_read_pending = 1;
228 static byte noct_button_light[16];
229 static byte noct_ring_mode[8]; // RING_MODE_xxx
230 static byte noct_ring_val[9];
232 static uint noct_dirty_button;
233 static uint noct_dirty_ring_mode;
234 static uint noct_dirty_ring_val;
236 static struct libusb_transfer *noct_write_xfer;
237 static bool noct_write_pending;
238 static void noct_sched_write(void);
240 static void noct_write_done(struct libusb_transfer *xfer)
242 int len = xfer->actual_length;
243 DBG("USB: Write done: status %d, length %d", xfer->status, len);
245 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
246 return noct_error(0, stk_printf("USB write failed with status %d", xfer->status));
247 if (len < xfer->length)
248 msg(L_ERROR, "USB partial write: %d out of %d", len, xfer->length);
250 noct_write_pending = 0;
254 static void noct_do_write(uint cmd, uint arg)
256 DBG("USB: Submitting write %02x %02x", cmd, arg);
257 ASSERT(!noct_write_pending);
258 noct_write_pending = 1;
260 struct libusb_transfer *xfer = noct_write_xfer;
261 byte *pkt = xfer->buffer;
267 if ((err = libusb_submit_transfer(xfer)) < 0)
268 noct_error(err, "Cannot submit transfer");
271 static void noct_sched_write(void)
273 if (!usb_dev || noct_write_pending)
276 if (noct_dirty_button)
278 int i = bit_ffs(noct_dirty_button);
279 noct_dirty_button ^= 1U << i;
280 noct_do_write(0x70 + i, noct_button_light[i]);
282 else if (noct_dirty_ring_mode)
284 int i = bit_ffs(noct_dirty_ring_mode);
285 noct_dirty_ring_mode ^= 1U << i;
286 noct_do_write(0x48 + i, noct_ring_mode[i] << 4);
288 else if (noct_dirty_ring_val)
290 int i = bit_ffs(noct_dirty_ring_val);
291 noct_dirty_ring_val ^= 1U << i;
293 noct_do_write(0x50, noct_ring_val[i]);
295 noct_do_write(0x40 + i, noct_ring_val[i]);
299 void noct_set_ring(int ring, int mode, int val)
301 ASSERT(ring >= 0 && ring <= 8);
302 ASSERT(val >= 0 && val <= 0x7f);
303 ASSERT(mode >= 0 && mode <= 5);
304 ASSERT(ring < 8 || !mode);
305 if (noct_ring_mode[ring] != mode)
307 noct_ring_mode[ring] = mode;
308 noct_dirty_ring_mode |= 1U << ring;
309 noct_dirty_ring_val |= 1U << ring; // HW needs to re-send the value
311 if (noct_ring_val[ring] != val)
313 noct_ring_val[ring] = val;
314 noct_dirty_ring_val |= 1U << ring;
319 void noct_set_button(int button, int val)
321 ASSERT(button >= 0 && button < 16);
322 ASSERT(val == 0 || val == 1);
323 if (noct_button_light[button] != val)
325 noct_button_light[button] = val;
326 noct_dirty_button |= 1U << button;
331 void noct_clear(void)
333 for (int i=0; i<=8; i++)
334 noct_set_ring(i, RING_MODE_LEFT, 0);
335 for (int i=0; i<16; i++)
336 noct_set_button(i, 0);
339 static void noct_write_init(void)
341 DBG("Noct: Write init");
343 noct_write_xfer = libusb_alloc_transfer(0);
344 libusb_fill_interrupt_transfer(noct_write_xfer, usb_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
346 bzero(noct_button_pressed, sizeof(noct_button_pressed));
347 bzero(noct_rotary_touched, sizeof(noct_rotary_touched));
348 noct_dirty_button = 0xffff;
349 noct_dirty_ring_mode = 0xff;
350 noct_dirty_ring_val = 0x1ff;
354 static struct main_timer noct_connect_timer;
355 static struct main_hook noct_error_hook;
357 static void noct_connect(struct main_timer *t)
360 msg(L_DEBUG, "Looking for Nocturn");
363 libusb_device **dev_list;
364 libusb_device *found_dev = NULL;
365 ssize_t len = libusb_get_device_list(usb_ctx, &dev_list);
366 for (ssize_t i=0; i < len; i++)
368 libusb_device *dev = dev_list[i];
369 struct libusb_device_descriptor desc;
370 if (libusb_get_device_descriptor(dev, &desc) >= 0 &&
371 desc.idVendor == 0x1235 &&
372 desc.idProduct == 0x000a)
374 msg(L_INFO, "Nocturn found at bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
377 msg(L_ERROR, "Multiple Nocturn devices found. Using the first one.");
380 found_dev = libusb_ref_device(dev);
383 libusb_free_device_list(dev_list, 1);
387 msg(L_INFO, "No Nocturn device found");
388 timer_add_rel(t, 5000);
392 DBG("Initializing Nocturn");
394 if ((err = libusb_open(found_dev, &usb_dev)) < 0)
395 return noct_error(err, "libusb_open failed");
397 // In newer kernels, Nocturn is claimed by snd-usb-audio. Tell it to surrender the device.
398 libusb_detach_kernel_driver(usb_dev, 0);
400 // There exist configurations 1 (high brightness) and 2 (power-save)
401 if ((err = libusb_set_configuration(usb_dev, 2)) < 0)
402 return noct_error(err, "libusb_set_configuration failed");
404 if ((err = libusb_claim_interface(usb_dev, 0)) < 0)
405 return noct_error(err, "libusb_claim_interface failed");
406 usb_iface_claimed = 1;
408 for (int i=0; i<4; i++)
411 if ((err = libusb_interrupt_transfer(usb_dev, 0x02, (byte *) noct_magic[i] + 1, noct_magic[i][0], &done, 5000)) < 0)
412 return noct_error(err, "Cannot send init packets");
413 if (done != noct_magic[i][0])
414 return noct_error(err, stk_printf("Partial send of init packet (%d < %d)", done, noct_magic[i][0]));
422 static int noct_error_handler(struct main_hook *h)
424 DBG("Noct: Entered error handling hook");
431 if (noct_read_pending)
433 DBG("Noct: Cancelling pending read");
434 libusb_cancel_transfer(noct_read_xfer);
435 noct_read_pending = 0;
437 DBG("Noct: Tearing down read xfer");
438 xfree(noct_read_xfer->buffer);
439 libusb_free_transfer(noct_read_xfer);
440 noct_read_xfer = NULL;
444 if (noct_write_pending)
446 DBG("Noct: Cancelling pending write");
447 libusb_cancel_transfer(noct_write_xfer);
448 noct_write_pending = 0;
450 DBG("Noct: Tearing down write xfer");
451 xfree(noct_write_xfer->buffer);
452 libusb_free_transfer(noct_write_xfer);
453 noct_write_xfer = NULL;
455 if (usb_iface_claimed)
457 DBG("Noct: Unclaiming interface");
458 libusb_release_interface(usb_dev, 0);
459 usb_iface_claimed = 0;
461 DBG("Noct: Resetting device");
462 libusb_reset_device(usb_dev);
463 libusb_close(usb_dev);
467 DBG("Noct: Scheduling rescan after error");
468 timer_add_rel(&noct_connect_timer, 3000);
473 static void noct_error(int usb_err, char *text)
476 msg(L_ERROR, "Nocturn: %s: error %d (%s)", text, usb_err, libusb_error_name(usb_err));
478 msg(L_ERROR, "Nocturn: %s", text);
480 DBG("Noct: Scheduling error handling hook");
481 hook_add(&noct_error_hook);
484 bool noct_is_ready(void)
494 if ((err = libusb_init(&usb_ctx)) < 0)
495 die("libusb_init failed: error %d", err);
497 // Connect libusb to UCW mainloop
499 if (!libusb_pollfds_handle_timeouts(usb_ctx))
500 die("Unsupported version of libusb, please fix me");
502 GARY_INIT_ZERO(usb_fds, 0);
503 libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL);
505 const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx);
507 for (int i=0; fds[i]; i++)
508 usb_added_fd(fds[i]->fd, fds[i]->events, NULL);
511 // Prepare error handling hook
512 noct_error_hook.handler = noct_error_handler;
514 // Schedule search for the Nocturn
515 noct_connect_timer.handler = noct_connect;
516 timer_add_rel(&noct_connect_timer, 100);