summaryrefslogtreecommitdiff
path: root/app/src/usb_hid.c
blob: 07c3d5f77ab4c69ca1962d5db0d049871877bf7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

#include <zephyr.h>
#include <device.h>
#include <init.h>

#include <usb/usb_device.h>
#include <usb/class/usb_hid.h>
#include <dt-bindings/zmk/keys.h>

#include <zmk/hid.h>
#include <zmk/keymap.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

static enum usb_dc_status_code usb_status;

static struct device *hid_dev;

static K_SEM_DEFINE(hid_sem, 1, 1);

static void in_ready_cb(void)
{
	k_sem_give(&hid_sem);
}

static const struct hid_ops ops = 
{
	.int_in_ready = in_ready_cb,
};

int zmk_usb_hid_send_report(const u8_t *report, size_t len)
{
	if (usb_status == USB_DC_SUSPEND)
	{
		return usb_wakeup_request();
	}

	k_sem_take(&hid_sem, K_FOREVER);
	return hid_int_ep_write(hid_dev, report, len, NULL);
}

void usb_hid_status_cb(enum usb_dc_status_code status, const u8_t *params)
{
	usb_status = status;
};

static int zmk_usb_hid_init(struct device *_arg)
{
	int usb_enable_ret;

	hid_dev = device_get_binding("HID_0");
	if (hid_dev == NULL)
	{
		LOG_ERR("Unable to locate HID device");
		return -EINVAL;
	}

	usb_hid_register_device(hid_dev,
							zmk_hid_report_desc, sizeof(zmk_hid_report_desc),
							&ops);

	usb_hid_init(hid_dev);

	usb_enable_ret = usb_enable(usb_hid_status_cb);

	if (usb_enable_ret != 0)
	{
		LOG_ERR("Unable to enable USB");
		return -EINVAL;
	}

	return 0;
}

SYS_INIT(zmk_usb_hid_init,
         APPLICATION,
		 CONFIG_ZMK_USB_INIT_PRIORITY);