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/string.h>
28 static libusb_context *usb_ctx;
29 static libusb_device_handle *usb_dev;
31 static struct main_file **usb_fds;
33 static int usb_fd_ready(struct main_file *f UNUSED)
35 DBG("USB: Handling events (ready on fd %d)", f->fd);
36 struct timeval tv = { 0, 0 };
38 int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp);
40 msg(L_ERROR, "libusb_handle_events: error %d", err);
44 static void usb_added_fd(int fd, short events, void *user_data UNUSED)
46 if (fd >= (int) GARY_SIZE(usb_fds))
47 GARY_RESIZE(usb_fds, fd + 1);
49 struct main_file *f = usb_fds[fd];
52 f = xmalloc_zero(sizeof(*f));
55 else if (file_is_active(f))
57 DBG("USB: Releasing fd %d", fd);
61 DBG("USB: Adding fd %d with event mask %u", fd, events);
63 f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL;
64 f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL;
68 static void usb_removed_fd(int fd, void *user_data UNUSED)
70 DBG("USB: Releasing fd %d", fd);
71 ASSERT(fd < (int) GARY_SIZE(usb_fds));
72 struct main_file *f = usb_fds[fd];
74 ASSERT(file_is_active(f));
78 static const char noct_magic[4][9] = {
79 { 3, 0xb0, 0x00, 0x00 },
80 { 8, 0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35 },
81 { 6, 0x2a, 0x02, 0x2c, 0x72, 0x2e, 0x30 },
85 static void noct_read_done(struct libusb_transfer *xfer)
87 byte *pkt = xfer->buffer;
88 int len = xfer->actual_length;
89 DBG("USB: Read done: status %d, length %d", xfer->status, len);
91 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
93 msg(L_ERROR, "USB read failed with status %d, not submitting again", xfer->status);
99 mem_to_hex(buf, pkt, len, ' ');
100 DBG("USB: Read <%s>", buf);
108 msg(L_ERROR, "Unknown USB packet: length %d not divisible by 3", len);
113 msg(L_ERROR, "Unknown USB packet: expected 0xb0 at position %d", i);
122 // Unknown packet sent during init
128 int delta = (arg < 0x40 ? arg : arg - 0x80);
129 DBG("Noct: Rotary %d = %d", r, delta);
130 notify_rotary(r, delta);
137 DBG("Noct: Slider value = %d", arg);
142 // Unknown packet, maybe least significant bit of slider
147 int delta = (arg < 0x40 ? arg : arg - 0x80);
148 DBG("Noct: Center = %d", delta);
149 notify_rotary(8, delta);
154 if (arg == 0x00 || arg == 0x7f)
157 DBG("Noct: Center touch = %d", state);
162 if (arg == 0x00 || arg == 0x7f)
165 DBG("Noct: Slider touch = %d", state);
170 if (arg == 0x00 || arg == 0x7f)
174 DBG("Noct: Rotary %d touch = %d", r, state);
179 if (arg == 0x00 || arg == 0x7f)
183 DBG("Noct: Button %d = %d", b, state);
184 notify_button(b, state);
189 msg(L_ERROR, "Unknown USB packet: unrecognized cmd=%02x arg=%02x", cmd, arg);
193 if ((err = libusb_submit_transfer(xfer)) < 0)
194 die("Cannot submit transfer: error %d", err);
197 static void noct_read_init(void)
199 DBG("Noct: Read init");
201 struct libusb_transfer *xfer = libusb_alloc_transfer(0);
202 libusb_fill_interrupt_transfer(xfer, usb_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
205 if ((err = libusb_submit_transfer(xfer)) < 0)
206 die("Cannot submit transfer: error %d", err);
209 static byte noct_button_state[16];
210 static byte noct_ring_mode[8]; // RING_MODE_xxx
211 static byte noct_ring_val[9];
213 static uns noct_dirty_button;
214 static uns noct_dirty_ring_mode;
215 static uns noct_dirty_ring_val;
217 static struct libusb_transfer *noct_write_xfer;
218 static uns noct_write_pending;
219 static void noct_sched_write(void);
221 static void noct_write_done(struct libusb_transfer *xfer)
223 int len = xfer->actual_length;
224 DBG("USB: Write done: status %d, length %d", xfer->status, len);
226 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
228 msg(L_ERROR, "USB write failed with status %d", xfer->status);
229 // FIXME: Handle the error in a meaningful way
232 if (len < xfer->length)
233 msg(L_ERROR, "USB partial write: %d out of %d", len, xfer->length);
235 noct_write_pending = 0;
239 static void noct_do_write(uns cmd, uns arg)
241 DBG("USB: Submitting write %02x %02x", cmd, arg);
242 ASSERT(!noct_write_pending);
243 noct_write_pending = 1;
245 struct libusb_transfer *xfer = noct_write_xfer;
246 byte *pkt = xfer->buffer;
252 if ((err = libusb_submit_transfer(xfer)) < 0)
253 die("Cannot submit transfer: error %d", err);
256 static void noct_sched_write(void)
258 if (noct_write_pending)
261 if (noct_dirty_button)
263 int i = bit_ffs(noct_dirty_button);
264 noct_dirty_button ^= 1U << i;
265 noct_do_write(0x70 + i, noct_button_state[i]);
267 else if (noct_dirty_ring_mode)
269 int i = bit_ffs(noct_dirty_ring_mode);
270 noct_dirty_ring_mode ^= 1U << i;
271 noct_do_write(0x48 + i, noct_ring_mode[i] << 4);
273 else if (noct_dirty_ring_val)
275 int i = bit_ffs(noct_dirty_ring_val);
276 noct_dirty_ring_val ^= 1U << i;
278 noct_do_write(0x50 + i, noct_ring_val[i]);
280 noct_do_write(0x40 + i, noct_ring_val[i]);
284 void noct_set_ring(int ring, int mode, int val)
286 ASSERT(ring >= 0 && ring <= 8);
287 ASSERT(val >= 0 && val <= 0x7f);
288 ASSERT(mode >= 0 && mode <= 5);
289 ASSERT(ring < 8 || !mode);
290 if (noct_ring_mode[ring] != mode)
292 noct_ring_mode[ring] = mode;
293 noct_dirty_ring_mode |= 1U << ring;
294 noct_dirty_ring_val |= 1U << ring; // HW needs to re-send the value
296 if (noct_ring_val[ring] != val)
298 noct_ring_val[ring] = val;
299 noct_dirty_ring_val |= 1U << ring;
304 void noct_set_button(int button, int val)
306 ASSERT(button >= 0 && button < 16);
307 ASSERT(val == 0 || val == 1);
308 if (noct_button_state[button] != val)
310 noct_button_state[button] = val;
311 noct_dirty_button |= 1U << button;
316 static void noct_write_init(void)
318 DBG("Noct: Write init");
320 noct_write_xfer = libusb_alloc_transfer(0);
321 libusb_fill_interrupt_transfer(noct_write_xfer, usb_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
324 noct_button_state[2] = 1;
325 noct_ring_mode[0] = 4;
326 noct_ring_val[0] = 0x40;
329 noct_dirty_button = 0xffff;
330 noct_dirty_ring_mode = 0xff;
331 noct_dirty_ring_val = 0x1ff;
335 static struct main_timer noct_connect_timer;
337 static void noct_connect(struct main_timer *t)
340 msg(L_DEBUG, "Looking for Nocturn");
343 libusb_device **dev_list;
344 libusb_device *found_dev = NULL;
345 ssize_t len = libusb_get_device_list(usb_ctx, &dev_list);
346 for (ssize_t i=0; i < len; i++)
348 libusb_device *dev = dev_list[i];
349 struct libusb_device_descriptor desc;
350 if (libusb_get_device_descriptor(dev, &desc) >= 0 &&
351 desc.idVendor == 0x1235 &&
352 desc.idProduct == 0x000a)
354 msg(L_DEBUG, "Found device: bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
357 msg(L_ERROR, "Multiple Nocturn devices found. Using the first one.");
360 found_dev = libusb_ref_device(dev);
363 libusb_free_device_list(dev_list, 1);
367 msg(L_INFO, "No Nocturn device found");
368 timer_add_rel(t, 5000);
372 msg(L_DEBUG, "Initializing Nocturn");
374 if ((err = libusb_open(found_dev, &usb_dev)) < 0)
375 die("libusb_open failed: error %d", err);
377 // There exist configurations 1 (high brightness) and 2 (power-save)
378 if ((err = libusb_set_configuration(usb_dev, 1)) < 0)
379 die("libusb_set_configuration: error %d", err);
381 if ((err = libusb_claim_interface(usb_dev, 0)) < 0)
382 die("libusb_claim_interface: error %d", err);
384 for (int i=0; i<4; i++)
387 if ((err = libusb_interrupt_transfer(usb_dev, 0x02, (byte *) noct_magic[i] + 1, noct_magic[i][0], &done, 5000)) < 0)
388 die("Cannot send init packets: error %d", err);
389 if (done != noct_magic[i][0])
390 die("Partial send of init packet: %d < %d", done, noct_magic[i][0]);
394 byte xxx[] = { 0x7f, 0x01 };
396 libusb_interrupt_transfer(usb_dev, 0x02, xxx, 2, &done, 5000);
404 bool noct_is_ready(void)
406 return !!usb_dev; // FIXME
414 if ((err = libusb_init(&usb_ctx)) < 0)
415 die("libusb_init failed: error %d", err);
416 libusb_set_debug(usb_ctx, 3);
418 // Connect libusb to UCW mainloop
420 if (!libusb_pollfds_handle_timeouts(usb_ctx))
421 die("Unsupported version of libusb, please fix me");
423 GARY_INIT_ZERO(usb_fds, 0);
424 libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL);
426 const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx);
428 for (int i=0; fds[i]; i++)
429 usb_added_fd(fds[i]->fd, fds[i]->events, NULL);
432 // Schedule search for the Nocturn
433 noct_connect_timer.handler = noct_connect;
434 timer_add_rel(&noct_connect_timer, 0);