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/mainloop.h>
15 #include <ucw/stkstring.h>
23 static libusb_device_handle *noct_dev;
24 static bool noct_iface_claimed;
26 static void noct_error(int usb_err, char *text);
28 static const char noct_magic[4][9] = {
29 { 3, 0xb0, 0x00, 0x00 },
30 { 8, 0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35 },
31 { 6, 0x2a, 0x02, 0x2c, 0x72, 0x2e, 0x30 },
35 static struct libusb_transfer *noct_read_xfer;
36 static bool noct_read_pending;
37 char noct_rotary_touched[10];
38 char noct_button_pressed[16];
40 static void noct_read_done(struct libusb_transfer *xfer)
42 byte *pkt = xfer->buffer;
43 int len = xfer->actual_length;
44 DBG("Noct: Read done: status %d, length %d", xfer->status, len);
45 noct_read_pending = 0;
47 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
48 return noct_error(0, stk_printf("USB read failed with status %d", xfer->status));
52 mem_to_hex(buf, pkt, len, ' ');
53 DBG("Noct: Read <%s>", buf);
61 msg(L_ERROR, "Unknown USB packet: length %d not divisible by 3", len);
66 msg(L_ERROR, "Unknown USB packet: expected 0xb0 at position %d", i);
75 // Unknown packet sent during init
81 int delta = (arg < 0x40 ? arg : arg - 0x80);
82 DBG("Noct: Rotary %d = %d", r, delta);
83 notify_rotary(r, delta);
90 DBG("Noct: Slider value = %d", arg);
91 notify_rotary(9, arg);
96 // Unknown packet, maybe least significant bit of slider
101 int delta = (arg < 0x40 ? arg : arg - 0x80);
102 DBG("Noct: Center = %d", delta);
103 notify_rotary(8, delta);
108 if (arg == 0x00 || arg == 0x7f)
111 DBG("Noct: Center touch = %d", state);
112 noct_rotary_touched[8] = state;
113 notify_touch(8, state);
118 if (arg == 0x00 || arg == 0x7f)
121 DBG("Noct: Slider touch = %d", state);
122 noct_rotary_touched[9] = state;
123 notify_touch(9, state);
128 if (arg == 0x00 || arg == 0x7f)
132 DBG("Noct: Rotary %d touch = %d", r, state);
133 noct_rotary_touched[r] = state;
134 notify_touch(r, state);
139 if (arg == 0x00 || arg == 0x7f)
143 DBG("Noct: Button %d = %d", b, state);
144 noct_button_pressed[b] = state;
145 notify_button(b, state);
150 msg(L_ERROR, "Unknown USB packet: unrecognized cmd=%02x arg=%02x", cmd, arg);
154 if ((err = libusb_submit_transfer(xfer)) < 0)
155 noct_error(err, "Cannot submit transfer");
157 noct_read_pending = 1;
160 static void noct_read_init(void)
162 DBG("Noct: Read init");
164 noct_read_xfer = libusb_alloc_transfer(0);
165 libusb_fill_interrupt_transfer(noct_read_xfer, noct_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
168 if ((err = libusb_submit_transfer(noct_read_xfer)) < 0)
169 noct_error(err, "Cannot submit transfer");
171 noct_read_pending = 1;
174 static byte noct_button_light[16];
175 static byte noct_ring_mode[8]; // RING_MODE_xxx
176 static byte noct_ring_val[9];
178 static uint noct_dirty_button;
179 static uint noct_dirty_ring_mode;
180 static uint noct_dirty_ring_val;
182 static struct libusb_transfer *noct_write_xfer;
183 static bool noct_write_pending;
184 static void noct_sched_write(void);
186 static void noct_write_done(struct libusb_transfer *xfer)
188 int len = xfer->actual_length;
189 DBG("Noct: Write done: status %d, length %d", xfer->status, len);
191 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
192 return noct_error(0, stk_printf("USB write failed with status %d", xfer->status));
193 if (len < xfer->length)
194 msg(L_ERROR, "USB partial write: %d out of %d", len, xfer->length);
196 noct_write_pending = 0;
200 static void noct_do_write(uint cmd, uint arg)
202 DBG("Noct: Submitting write %02x %02x", cmd, arg);
203 ASSERT(!noct_write_pending);
204 noct_write_pending = 1;
206 struct libusb_transfer *xfer = noct_write_xfer;
207 byte *pkt = xfer->buffer;
213 if ((err = libusb_submit_transfer(xfer)) < 0)
214 noct_error(err, "Cannot submit transfer");
217 static void noct_sched_write(void)
219 if (!noct_dev || noct_write_pending)
222 if (noct_dirty_button)
224 int i = bit_ffs(noct_dirty_button);
225 noct_dirty_button ^= 1U << i;
226 noct_do_write(0x70 + i, noct_button_light[i]);
228 else if (noct_dirty_ring_mode)
230 int i = bit_ffs(noct_dirty_ring_mode);
231 noct_dirty_ring_mode ^= 1U << i;
232 noct_do_write(0x48 + i, noct_ring_mode[i] << 4);
234 else if (noct_dirty_ring_val)
236 int i = bit_ffs(noct_dirty_ring_val);
237 noct_dirty_ring_val ^= 1U << i;
239 noct_do_write(0x50, noct_ring_val[i]);
241 noct_do_write(0x40 + i, noct_ring_val[i]);
245 void noct_set_ring(int ring, int mode, int val)
247 ASSERT(ring >= 0 && ring <= 8);
248 ASSERT(val >= 0 && val <= 0x7f);
249 ASSERT(mode >= 0 && mode <= 5);
250 ASSERT(ring < 8 || !mode);
251 if (noct_ring_mode[ring] != mode)
253 noct_ring_mode[ring] = mode;
254 noct_dirty_ring_mode |= 1U << ring;
255 noct_dirty_ring_val |= 1U << ring; // HW needs to re-send the value
257 if (noct_ring_val[ring] != val)
259 noct_ring_val[ring] = val;
260 noct_dirty_ring_val |= 1U << ring;
265 void noct_set_button(int button, int val)
267 ASSERT(button >= 0 && button < 16);
268 ASSERT(val == 0 || val == 1);
269 if (noct_button_light[button] != val)
271 noct_button_light[button] = val;
272 noct_dirty_button |= 1U << button;
277 void noct_clear(void)
279 for (int i=0; i<=8; i++)
280 noct_set_ring(i, RING_MODE_LEFT, 0);
281 for (int i=0; i<16; i++)
282 noct_set_button(i, 0);
285 static void noct_write_init(void)
287 DBG("Noct: Write init");
289 noct_write_xfer = libusb_alloc_transfer(0);
290 libusb_fill_interrupt_transfer(noct_write_xfer, noct_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
292 bzero(noct_button_pressed, sizeof(noct_button_pressed));
293 bzero(noct_rotary_touched, sizeof(noct_rotary_touched));
294 noct_dirty_button = 0xffff;
295 noct_dirty_ring_mode = 0xff;
296 noct_dirty_ring_val = 0x1ff;
300 static struct main_timer noct_connect_timer;
301 static struct main_hook noct_error_hook;
303 static void noct_connect(struct main_timer *t)
306 msg(L_DEBUG, "Looking for Nocturn");
309 libusb_device **dev_list;
310 libusb_device *found_dev = NULL;
311 ssize_t len = libusb_get_device_list(usb_ctx, &dev_list);
312 for (ssize_t i=0; i < len; i++)
314 libusb_device *dev = dev_list[i];
315 struct libusb_device_descriptor desc;
316 if (libusb_get_device_descriptor(dev, &desc) >= 0 &&
317 desc.idVendor == 0x1235 &&
318 desc.idProduct == 0x000a)
320 msg(L_INFO, "Nocturn found at bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
323 msg(L_ERROR, "Multiple Nocturn devices found. Using the first one.");
326 found_dev = libusb_ref_device(dev);
329 libusb_free_device_list(dev_list, 1);
333 msg(L_INFO, "No Nocturn device found");
334 timer_add_rel(t, 5000);
338 DBG("Initializing Nocturn");
340 if ((err = libusb_open(found_dev, &noct_dev)) < 0)
341 return noct_error(err, "libusb_open failed");
343 // In newer kernels, Nocturn is claimed by snd-usb-audio. Tell it to surrender the device.
344 libusb_detach_kernel_driver(noct_dev, 0);
346 // There exist configurations 1 (high brightness) and 2 (power-save)
347 if ((err = libusb_set_configuration(noct_dev, 2)) < 0)
348 return noct_error(err, "libusb_set_configuration failed");
350 if ((err = libusb_claim_interface(noct_dev, 0)) < 0)
351 return noct_error(err, "libusb_claim_interface failed");
352 noct_iface_claimed = 1;
354 for (int i=0; i<4; i++)
357 if ((err = libusb_interrupt_transfer(noct_dev, 0x02, (byte *) noct_magic[i] + 1, noct_magic[i][0], &done, 5000)) < 0)
358 return noct_error(err, "Cannot send init packets");
359 if (done != noct_magic[i][0])
360 return noct_error(err, stk_printf("Partial send of init packet (%d < %d)", done, noct_magic[i][0]));
368 static int noct_error_handler(struct main_hook *h)
370 DBG("Noct: Entered error handling hook");
377 if (noct_read_pending)
379 DBG("Noct: Cancelling pending read");
380 libusb_cancel_transfer(noct_read_xfer);
381 noct_read_pending = 0;
383 DBG("Noct: Tearing down read xfer");
384 xfree(noct_read_xfer->buffer);
385 libusb_free_transfer(noct_read_xfer);
386 noct_read_xfer = NULL;
390 if (noct_write_pending)
392 DBG("Noct: Cancelling pending write");
393 libusb_cancel_transfer(noct_write_xfer);
394 noct_write_pending = 0;
396 DBG("Noct: Tearing down write xfer");
397 xfree(noct_write_xfer->buffer);
398 libusb_free_transfer(noct_write_xfer);
399 noct_write_xfer = NULL;
401 if (noct_iface_claimed)
403 DBG("Noct: Unclaiming interface");
404 libusb_release_interface(noct_dev, 0);
405 noct_iface_claimed = 0;
407 DBG("Noct: Resetting device");
408 libusb_reset_device(noct_dev);
409 libusb_close(noct_dev);
413 DBG("Noct: Scheduling rescan after error");
414 timer_add_rel(&noct_connect_timer, 3000);
419 static void noct_error(int usb_err, char *text)
422 msg(L_ERROR, "Nocturn: %s: error %d (%s)", text, usb_err, libusb_error_name(usb_err));
424 msg(L_ERROR, "Nocturn: %s", text);
426 DBG("Noct: Scheduling error handling hook");
427 hook_add(&noct_error_hook);
430 bool noct_is_ready(void)
437 // Prepare error handling hook
438 noct_error_hook.handler = noct_error_handler;
440 // Schedule search for the Nocturn
441 noct_connect_timer.handler = noct_connect;
442 timer_add_rel(&noct_connect_timer, 100);