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);
136 DBG("Noct: Slider value = %d", arg);
141 // Unknown packet, maybe least significant bit of slider
146 int delta = (arg < 0x40 ? arg : arg - 0x80);
147 DBG("Noct: Center = %d", delta);
152 if (arg == 0x00 || arg == 0x7f)
155 DBG("Noct: Center touch = %d", state);
160 if (arg == 0x00 || arg == 0x7f)
163 DBG("Noct: Slider touch = %d", state);
168 if (arg == 0x00 || arg == 0x7f)
172 DBG("Noct: Rotary %d touch = %d", r, state);
177 if (arg == 0x00 || arg == 0x7f)
181 DBG("Noct: Button %d = %d", b, state);
186 msg(L_ERROR, "Unknown USB packet: unrecognized cmd=%02x arg=%02x", cmd, arg);
190 if ((err = libusb_submit_transfer(xfer)) < 0)
191 die("Cannot submit transfer: error %d", err);
194 static void noct_read_init(void)
196 DBG("Noct: Read init");
198 struct libusb_transfer *xfer = libusb_alloc_transfer(0);
199 libusb_fill_interrupt_transfer(xfer, usb_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
202 if ((err = libusb_submit_transfer(xfer)) < 0)
203 die("Cannot submit transfer: error %d", err);
206 static byte noct_button_state[16];
207 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
208 static byte noct_ring_val[9];
210 static uns noct_dirty_button;
211 static uns noct_dirty_ring_mode;
212 static uns noct_dirty_ring_val;
214 static struct libusb_transfer *noct_write_xfer;
215 static uns noct_write_pending;
216 static void noct_sched_write(void);
218 static void noct_write_done(struct libusb_transfer *xfer)
220 int len = xfer->actual_length;
221 DBG("USB: Write done: status %d, length %d", xfer->status, len);
223 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
225 msg(L_ERROR, "USB write failed with status %d", xfer->status);
229 noct_write_pending = 0;
233 static void noct_do_write(uns cmd, uns arg)
235 DBG("USB: Submitting write %02x %02x", cmd, arg);
236 ASSERT(!noct_write_pending);
237 noct_write_pending = 1;
239 struct libusb_transfer *xfer = noct_write_xfer;
240 byte *pkt = xfer->buffer;
246 if ((err = libusb_submit_transfer(xfer)) < 0)
247 die("Cannot submit transfer: error %d", err);
250 static void noct_sched_write(void)
252 if (noct_write_pending)
255 if (noct_dirty_button)
257 int i = bit_ffs(noct_dirty_button);
258 noct_dirty_button ^= 1U << i;
259 noct_do_write(0x70 + i, noct_button_state[i]);
261 else if (noct_dirty_ring_mode)
263 int i = bit_ffs(noct_dirty_ring_mode);
264 noct_dirty_ring_mode ^= 1U << i;
265 noct_do_write(0x48 + i, noct_ring_mode[i] << 4);
267 else if (noct_dirty_ring_val)
269 int i = bit_ffs(noct_dirty_ring_val);
270 noct_dirty_ring_val ^= 1U << i;
272 noct_do_write(0x50 + i, noct_ring_val[i]);
274 noct_do_write(0x40 + i, noct_ring_val[i]);
278 static void noct_write_init(void)
280 DBG("Noct: Write init");
282 noct_write_xfer = libusb_alloc_transfer(0);
283 libusb_fill_interrupt_transfer(noct_write_xfer, usb_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
286 noct_button_state[2] = 1;
287 noct_ring_mode[0] = 4;
288 noct_ring_val[0] = 0x40;
291 noct_dirty_button = 0xffff;
292 noct_dirty_ring_mode = 0xff;
293 noct_dirty_ring_val = 0x1ff;
301 if ((err = libusb_init(&usb_ctx)) < 0)
302 die("libusb_init failed: error %d", err);
303 libusb_set_debug(usb_ctx, 3);
305 libusb_device **dev_list;
306 libusb_device *found_dev = NULL;
307 ssize_t len = libusb_get_device_list(usb_ctx, &dev_list);
308 for (ssize_t i=0; i < len; i++)
310 libusb_device *dev = dev_list[i];
311 struct libusb_device_descriptor desc;
312 if (libusb_get_device_descriptor(dev, &desc) >= 0 &&
313 desc.idVendor == 0x1235 &&
314 desc.idProduct == 0x000a)
316 msg(L_DEBUG, "Found device: bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
318 die("Multiple Nocturn devices found. Please fix me to handle it.");
319 found_dev = libusb_ref_device(dev);
322 libusb_free_device_list(dev_list, 1);
325 die("No Nocturn device found");
327 msg(L_DEBUG, "Initializing device");
329 if ((err = libusb_open(found_dev, &usb_dev)) < 0)
330 die("libusb_open failed: error %d", err);
332 // There exist configurations 1 (high brightness) and 2 (power-save)
333 if ((err = libusb_set_configuration(usb_dev, 1)) < 0)
334 die("libusb_set_configuration: error %d", err);
336 if ((err = libusb_claim_interface(usb_dev, 0)) < 0)
337 die("libusb_claim_interface: error %d", err);
339 for (int i=0; i<4; i++)
342 if ((err = libusb_interrupt_transfer(usb_dev, 0x02, (byte *) noct_magic[i] + 1, noct_magic[i][0], &done, 5000)) < 0)
343 die("Cannot send init packets: error %d", err);
344 if (done != noct_magic[i][0])
345 die("Partial send of init packet: %d < %d", done, noct_magic[i][0]);
349 byte xxx[] = { 0x7f, 0x01 };
351 libusb_interrupt_transfer(usb_dev, 0x02, xxx, 2, &done, 5000);
354 DBG("USB: Connecting libusb to mainloop");
356 if (!libusb_pollfds_handle_timeouts(usb_ctx))
357 die("Unsupported version of libusb, please fix me");
359 GARY_INIT_ZERO(usb_fds, 0);
360 libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL);
362 const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx);
364 for (int i=0; fds[i]; i++)
365 usb_added_fd(fds[i]->fd, fds[i]->events, NULL);