]> mj.ucw.cz Git - ursary.git/blob - nocturn.c
Better IR control of lights
[ursary.git] / nocturn.c
1 /*
2  *      Interface to Novation Nocturn
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  *
6  *      Protocol reverse-engineered by De Wet van Niekerk <dewert@gmail.com>,
7  *      see https://github.com/dewert/nocturn-linux-midi for inspiration.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include <ucw/lib.h>
13 #include <ucw/bitops.h>
14 #include <ucw/mainloop.h>
15 #include <ucw/stkstring.h>
16
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "ursaryd.h"
21 #include "usb.h"
22
23 static libusb_device_handle *noct_dev;
24 static bool noct_iface_claimed;
25
26 static void noct_error(int usb_err, char *text);
27
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 },
32   { 2, 0x7f, 0x00 },
33 };
34
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];
39
40 static void noct_read_done(struct libusb_transfer *xfer)
41 {
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;
46
47   if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
48     return noct_error(0, stk_printf("USB read failed with status %d", xfer->status));
49
50 #ifdef LOCAL_DEBUG
51   char buf[256];
52   mem_to_hex(buf, pkt, len, ' ');
53   DBG("Noct: Read <%s>", buf);
54 #endif
55
56   int i = 0;
57   while (i < len)
58     {
59       if (i + 3 > len)
60         {
61           msg(L_ERROR, "Unknown USB packet: length %d not divisible by 3", len);
62           break;
63         }
64       if (pkt[i] != 0xb0)
65         {
66           msg(L_ERROR, "Unknown USB packet: expected 0xb0 at position %d", i);
67           break;
68         }
69       int cmd = pkt[i+1];
70       int arg = pkt[i+2];
71       i += 3;
72       switch (cmd)
73         {
74         case 0x30:
75           // Unknown packet sent during init
76           continue;
77         case 0x40 ... 0x47:
78           if (arg < 0x80)
79             {
80               int r = cmd - 0x40;
81               int delta = (arg < 0x40 ? arg : arg - 0x80);
82               DBG("Noct: Rotary %d = %d", r, delta);
83               notify_rotary(r, delta);
84               continue;
85             }
86           break;
87         case 0x48:
88           if (arg < 0x80)
89             {
90               DBG("Noct: Slider value = %d", arg);
91               notify_rotary(9, arg);
92               continue;
93             }
94           break;
95         case 0x49:
96           // Unknown packet, maybe least significant bit of slider
97           continue;
98         case 0x4a:
99           if (arg < 0x80)
100             {
101               int delta = (arg < 0x40 ? arg : arg - 0x80);
102               DBG("Noct: Center = %d", delta);
103               notify_rotary(8, delta);
104               continue;
105             }
106           break;
107         case 0x52:
108           if (arg == 0x00 || arg == 0x7f)
109             {
110               int state = !!arg;
111               DBG("Noct: Center touch = %d", state);
112               noct_rotary_touched[8] = state;
113               notify_touch(8, state);
114               continue;
115             }
116           break;
117         case 0x53:
118           if (arg == 0x00 || arg == 0x7f)
119             {
120               int state = !!arg;
121               DBG("Noct: Slider touch = %d", state);
122               noct_rotary_touched[9] = state;
123               notify_touch(9, state);
124               continue;
125             }
126           break;
127         case 0x60 ... 0x67:
128           if (arg == 0x00 || arg == 0x7f)
129             {
130               int r = cmd - 0x60;
131               int state = !!arg;
132               DBG("Noct: Rotary %d touch = %d", r, state);
133               noct_rotary_touched[r] = state;
134               notify_touch(r, state);
135               continue;
136             }
137           break;
138         case 0x70 ... 0x7f:
139           if (arg == 0x00 || arg == 0x7f)
140             {
141               int b = cmd - 0x70;
142               int state = !!arg;
143               DBG("Noct: Button %d = %d", b, state);
144               noct_button_pressed[b] = state;
145               notify_button(b, state);
146               continue;
147             }
148           break;
149         }
150       msg(L_ERROR, "Unknown USB packet: unrecognized cmd=%02x arg=%02x", cmd, arg);
151     }
152
153   int err;
154   if ((err = libusb_submit_transfer(xfer)) < 0)
155     noct_error(err, "Cannot submit transfer");
156   else
157     noct_read_pending = 1;
158 }
159
160 static void noct_read_init(void)
161 {
162   DBG("Noct: Read init");
163
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);
166
167   int err;
168   if ((err = libusb_submit_transfer(noct_read_xfer)) < 0)
169     noct_error(err, "Cannot submit transfer");
170   else
171     noct_read_pending = 1;
172 }
173
174 static byte noct_button_light[16];
175 static byte noct_ring_mode[8];          // RING_MODE_xxx
176 static byte noct_ring_val[9];
177
178 static uint noct_dirty_button;
179 static uint noct_dirty_ring_mode;
180 static uint noct_dirty_ring_val;
181
182 static struct libusb_transfer *noct_write_xfer;
183 static bool noct_write_pending;
184 static void noct_sched_write(void);
185
186 static void noct_write_done(struct libusb_transfer *xfer)
187 {
188   int len = xfer->actual_length;
189   DBG("Noct: Write done: status %d, length %d", xfer->status, len);
190
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);
195
196   noct_write_pending = 0;
197   noct_sched_write();
198 }
199
200 static void noct_do_write(uint cmd, uint arg)
201 {
202   DBG("Noct: Submitting write %02x %02x", cmd, arg);
203   ASSERT(!noct_write_pending);
204   noct_write_pending = 1;
205
206   struct libusb_transfer *xfer = noct_write_xfer;
207   byte *pkt = xfer->buffer;
208   pkt[0] = cmd;
209   pkt[1] = arg;
210   xfer->length = 2;
211
212   int err;
213   if ((err = libusb_submit_transfer(xfer)) < 0)
214     noct_error(err, "Cannot submit transfer");
215 }
216
217 static void noct_sched_write(void)
218 {
219   if (!noct_dev || noct_write_pending)
220     return;
221
222   if (noct_dirty_button)
223     {
224       int i = bit_ffs(noct_dirty_button);
225       noct_dirty_button ^= 1U << i;
226       noct_do_write(0x70 + i, noct_button_light[i]);
227     }
228   else if (noct_dirty_ring_mode)
229     {
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);
233     }
234   else if (noct_dirty_ring_val)
235     {
236       int i = bit_ffs(noct_dirty_ring_val);
237       noct_dirty_ring_val ^= 1U << i;
238       if (i == 8)
239         noct_do_write(0x50, noct_ring_val[i]);
240       else
241         noct_do_write(0x40 + i, noct_ring_val[i]);
242     }
243 }
244
245 void noct_set_ring(int ring, int mode, int val)
246 {
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)
252     {
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
256     }
257   if (noct_ring_val[ring] != val)
258     {
259       noct_ring_val[ring] = val;
260       noct_dirty_ring_val |= 1U << ring;
261     }
262   noct_sched_write();
263 }
264
265 void noct_set_button(int button, int val)
266 {
267   ASSERT(button >= 0 && button < 16);
268   ASSERT(val == 0 || val == 1);
269   if (noct_button_light[button] != val)
270     {
271       noct_button_light[button] = val;
272       noct_dirty_button |= 1U << button;
273       noct_sched_write();
274     }
275 }
276
277 void noct_clear(void)
278 {
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);
283 }
284
285 static void noct_write_init(void)
286 {
287   DBG("Noct: Write init");
288
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);
291
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;
297   noct_sched_write();
298 }
299
300 static struct main_timer noct_connect_timer;
301 static struct main_hook noct_error_hook;
302
303 static void noct_connect(struct main_timer *t)
304 {
305   timer_del(t);
306   msg(L_DEBUG, "Looking for Nocturn");
307   int err;
308
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++)
313     {
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)
319         {
320           msg(L_INFO, "Nocturn found at bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
321           if (found_dev)
322             {
323               msg(L_ERROR, "Multiple Nocturn devices found. Using the first one.");
324               break;
325             }
326           found_dev = libusb_ref_device(dev);
327         }
328     }
329   libusb_free_device_list(dev_list, 1);
330
331   if (!found_dev)
332     {
333       msg(L_INFO, "No Nocturn device found");
334       timer_add_rel(t, 5000);
335       return;
336     }
337
338   DBG("Initializing Nocturn");
339
340   if ((err = libusb_open(found_dev, &noct_dev)) < 0)
341     return noct_error(err, "libusb_open failed");
342
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);
345
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");
349
350   if ((err = libusb_claim_interface(noct_dev, 0)) < 0)
351     return noct_error(err, "libusb_claim_interface failed");
352   noct_iface_claimed = 1;
353
354   for (int i=0; i<4; i++)
355     {
356       int done;
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]));
361     }
362
363   noct_read_init();
364   noct_write_init();
365   schedule_update();
366 }
367
368 static int noct_error_handler(struct main_hook *h)
369 {
370   DBG("Noct: Entered error handling hook");
371   hook_del(h);
372
373   if (noct_dev)
374     {
375       if (noct_read_xfer)
376         {
377           if (noct_read_pending)
378             {
379               DBG("Noct: Cancelling pending read");
380               libusb_cancel_transfer(noct_read_xfer);
381               noct_read_pending = 0;
382             }
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;
387         }
388       if (noct_write_xfer)
389         {
390           if (noct_write_pending)
391             {
392               DBG("Noct: Cancelling pending write");
393               libusb_cancel_transfer(noct_write_xfer);
394               noct_write_pending = 0;
395             }
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;
400         }
401       if (noct_iface_claimed)
402         {
403           DBG("Noct: Unclaiming interface");
404           libusb_release_interface(noct_dev, 0);
405           noct_iface_claimed = 0;
406         }
407       DBG("Noct: Resetting device");
408       libusb_reset_device(noct_dev);
409       libusb_close(noct_dev);
410       noct_dev = NULL;
411     }
412
413   DBG("Noct: Scheduling rescan after error");
414   timer_add_rel(&noct_connect_timer, 3000);
415
416   return HOOK_IDLE;
417 }
418
419 static void noct_error(int usb_err, char *text)
420 {
421   if (usb_err)
422     msg(L_ERROR, "Nocturn: %s: error %d (%s)", text, usb_err, libusb_error_name(usb_err));
423   else
424     msg(L_ERROR, "Nocturn: %s", text);
425
426   DBG("Noct: Scheduling error handling hook");
427   hook_add(&noct_error_hook);
428 }
429
430 bool noct_is_ready(void)
431 {
432   return !!noct_dev;
433 }
434
435 void noct_init(void)
436 {
437   // Prepare error handling hook
438   noct_error_hook.handler = noct_error_handler;
439
440   // Schedule search for the Nocturn
441   noct_connect_timer.handler = noct_connect;
442   timer_add_rel(&noct_connect_timer, 100);
443 }