ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
usb.h
Go to the documentation of this file.
1 #ifndef USB_H
2 #define USB_H
3 
4 #include "includes.h"
5 #include "keymap.h"
6 
34 
42 #define V_CORSAIR 0x1b1c
43 #define V_CORSAIR_STR "1b1c"
44 
45 #define P_K63_NRGB 0x1b40
46 #define P_K63_NRGB_STR "1b40"
47 #define IS_K63(kb) ((kb)->vendor == V_CORSAIR && (kb)->product == P_K63_NRGB)
48 
49 #define P_K65 0x1b17
50 #define P_K65_STR "1b17"
51 #define P_K65_NRGB 0x1b07
52 #define P_K65_NRGB_STR "1b07"
53 #define P_K65_LUX 0x1b37
54 #define P_K65_LUX_STR "1b37"
55 #define P_K65_RFIRE 0x1b39
56 #define P_K65_RFIRE_STR "1b39"
57 #define IS_K65(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_K65 || (kb)->product == P_K65_NRGB || (kb)->product == P_K65_LUX || (kb)->product == P_K65_RFIRE))
58 
59 #define P_K68 0x1b3f
60 #define P_K68_STR "1b3f"
61 #define IS_K68(kb) ((kb)->vendor == V_CORSAIR && (kb)->product == P_K68)
62 
63 #define P_K70 0x1b13
64 #define P_K70_STR "1b13"
65 #define P_K70_NRGB 0x1b09
66 #define P_K70_NRGB_STR "1b09"
67 #define P_K70_LUX 0x1b33
68 #define P_K70_LUX_STR "1b33"
69 #define P_K70_LUX_NRGB 0x1b36
70 #define P_K70_LUX_NRGB_STR "1b36"
71 #define P_K70_RFIRE 0x1b38
72 #define P_K70_RFIRE_STR "1b38"
73 #define P_K70_RFIRE_NRGB 0x1b3a
74 #define P_K70_RFIRE_NRGB_STR "1b3a"
75 #define IS_K70(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_K70 || (kb)->product == P_K70_NRGB || (kb)->product == P_K70_RFIRE || (kb)->product == P_K70_RFIRE_NRGB || (kb)->product == P_K70_LUX || (kb)->product == P_K70_LUX_NRGB))
76 
77 #define P_K95 0x1b11
78 #define P_K95_STR "1b11"
79 #define P_K95_NRGB 0x1b08
80 #define P_K95_NRGB_STR "1b08"
81 #define P_K95_PLATINUM 0x1b2d
82 #define P_K95_PLATINUM_STR "1b2d"
83 #define IS_K95(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_K95 || (kb)->product == P_K95_NRGB || (kb)->product == P_K95_PLATINUM))
84 
85 #define P_STRAFE 0x1b20
86 #define P_STRAFE_STR "1b20"
87 #define P_STRAFE_NRGB 0x1b15
88 #define P_STRAFE_NRGB_STR "1b15"
89 #define P_STRAFE_NRGB_2 0x1b44
90 #define P_STRAFE_NRGB_2_STR "1b44"
91 #define IS_STRAFE(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_STRAFE || (kb)->product == P_STRAFE_NRGB || (kb)->product == P_STRAFE_NRGB_2))
92 
93 #define P_M65 0x1b12
94 #define P_M65_STR "1b12"
95 #define P_M65_PRO 0x1b2e
96 #define P_M65_PRO_STR "1b2e"
97 #define IS_M65(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_M65 || (kb)->product == P_M65_PRO))
98 
99 #define P_SABRE_O 0x1b14 /* optical */
100 #define P_SABRE_O_STR "1b14"
101 #define P_SABRE_L 0x1b19 /* laser */
102 #define P_SABRE_L_STR "1b19"
103 #define P_SABRE_N 0x1b2f /* new? */
104 #define P_SABRE_N_STR "1b2f"
105 #define P_SABRE_O2 0x1b32 /* Observed on a CH-9000111-EU model SABRE */
106 #define P_SABRE_O2_STR "1b32"
107 #define IS_SABRE(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_SABRE_O || (kb)->product == P_SABRE_L || (kb)->product == P_SABRE_N || (kb)->product == P_SABRE_O2))
108 
109 #define P_SCIMITAR 0x1b1e
110 #define P_SCIMITAR_STR "1b1e"
111 #define P_SCIMITAR_PRO 0x1b3e
112 #define P_SCIMITAR_PRO_STR "1b3e"
113 #define IS_SCIMITAR(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_SCIMITAR || (kb)->product == P_SCIMITAR_PRO))
114 
115 #define P_HARPOON 0x1b3c
116 #define P_HARPOON_STR "1b3c"
117 #define IS_HARPOON(kb) ((kb)->vendor == V_CORSAIR && (kb)->product == P_HARPOON)
118 
119 #define P_GLAIVE 0x1b34
120 #define P_GLAIVE_STR "1b34"
121 #define IS_GLAIVE(kb) ((kb)->vendor == V_CORSAIR && (kb)->product == P_GLAIVE)
122 
125 // #define DEBUG_USB_SEND
126 
129 // #define DEBUG_USB_RECV
130 
135 const char* vendor_str(short vendor);
136 
141 const char* product_str(short product);
142 
146 #define IS_RGB(vendor, product) ((vendor) == (V_CORSAIR) && (product) != (P_K65_NRGB) && (product) != (P_K70_NRGB) && (product) != (P_K95_NRGB))
147 
151 #define IS_MONOCHROME(vendor, product) ((vendor) == (V_CORSAIR) && ((product) == (P_K68) || (product) == (P_STRAFE_NRGB) || (product) == (P_STRAFE_NRGB_2)))
152 
154 #define IS_RGB_DEV(kb) IS_RGB((kb)->vendor, (kb)->product)
155 
157 #define IS_MONOCHROME_DEV(kb) IS_MONOCHROME((kb)->vendor, (kb)->product)
158 
160 #define IS_FULLRANGE(kb) (IS_RGB((kb)->vendor, (kb)->product) && (kb)->product != P_K65 && (kb)->product != P_K70 && (kb)->product != P_K95)
161 
163 #define IS_MOUSE(vendor, product) ((vendor) == (V_CORSAIR) && ((product) == (P_M65) || (product) == (P_M65_PRO) || (product) == (P_SABRE_O) || (product) == (P_SABRE_L) || (product) == (P_SABRE_N) || (product) == (P_SCIMITAR) || (product) == (P_SCIMITAR_PRO) || (product) == (P_SABRE_O2) || (product) == (P_GLAIVE) || (product) == (P_HARPOON)))
164 
166 #define IS_MOUSE_DEV(kb) IS_MOUSE((kb)->vendor, (kb)->product)
167 
169 #define IS_PLATINUM(kb) ((kb)->vendor == V_CORSAIR && ((kb)->product == P_K95_PLATINUM))
170 
172 #define IS_V2_OVERRIDE(kb) (IS_PLATINUM(kb) || IS_K63(kb) || IS_K68(kb) || IS_HARPOON(kb) || IS_GLAIVE(kb) || (kb)->product == P_STRAFE_NRGB_2)
173 
176 
178 #define DELAY_SHORT(kb) \
179  clock_nanosleep(CLOCK_MONOTONIC, 0, &(struct timespec) {.tv_nsec = ((int) (kb->usbdelay)) * 1000000}, NULL) // base (default: 5ms)
180 
182 #define DELAY_MEDIUM(kb) \
183  clock_nanosleep(CLOCK_MONOTONIC, 0, &(struct timespec) {.tv_nsec = ((int) (kb->usbdelay)) * 10000000}, NULL) // x10 (default: 50ms)
184 
186 #define DELAY_LONG(kb) \
187  clock_nanosleep(CLOCK_MONOTONIC, 0, &(struct timespec) {.tv_nsec = 100000000}, NULL) // long, fixed 100ms
188 
192 #define USB_DELAY_DEFAULT 5
193 
195 int usbmain();
196 
198 void usbkill();
199 
205 void setupusb(usbdevice* kb);
206 
211 int os_setupusb(usbdevice* kb);
212 
218 void* os_inputmain(void* context);
219 
223 int revertusb(usbdevice* kb);
224 
229 int closeusb(usbdevice* kb);
230 
235 void os_closeusb(usbdevice* kb);
236 
243 int _resetusb(usbdevice* kb, const char* file, int line);
244 
246 #define resetusb(kb) _resetusb(kb, __FILE_NOPATH__, __LINE__)
247 
254 int os_resetusb(usbdevice* kb, const char* file, int line);
255 
265 int _usbsend(usbdevice* kb, const uchar* messages, int count, const char* file, int line);
266 
271 #define usbsend(kb, messages, count) _usbsend(kb, messages, count, __FILE_NOPATH__, __LINE__)
272 
282 int _usbrecv(usbdevice* kb, const uchar* out_msg, uchar* in_msg, const char* file, int line);
283 
288 #define usbrecv(kb, out_msg, in_msg) _usbrecv(kb, out_msg, in_msg, __FILE_NOPATH__, __LINE__)
289 
298 int os_usbsend(usbdevice* kb, const uchar* out_msg, int is_recv, const char* file, int line);
299 
307 int os_usbrecv(usbdevice* kb, uchar* in_msg, const char* file, int line);
308 
312 void os_sendindicators(usbdevice* kb);
313 
322 int _nk95cmd(usbdevice* kb, uchar bRequest, ushort wValue, const char* file, int line);
323 
328 #define nk95cmd(kb, command) _nk95cmd(kb, (command) >> 16 & 0xFF, (command) & 0xFFFF, __FILE_NOPATH__, __LINE__)
329 
333 #define NK95_HWOFF 0x020030
334 
336 #define NK95_HWON 0x020001
337 
339 #define NK95_M1 0x140001
340 
342 #define NK95_M2 0x140002
343 
345 #define NK95_M3 0x140003
346 
352 int usb_tryreset(usbdevice* kb);
353 
354 #endif // USB_H
int usbmain()
Start the USB main loop. Returns program exit code when finished.
Definition: usb_linux.c:793
int os_resetusb(usbdevice *kb, const char *file, int line)
os_resetusb is the os specific implementation for resetting usb
Definition: usb_linux.c:510
void * os_inputmain(void *context)
os_inputmain is run in a separate thread and will be detached from the main thread, so it needs to clean up its own resources.
Definition: usb_linux.c:248
void os_closeusb(usbdevice *kb)
os_closeusb unclaim it, destroy the udev device and clear data structures at kb
Definition: usb_linux.c:448
const char * vendor_str(short vendor)
uncomment to see USB packets sent to the device
Definition: usb.c:43
int os_usbrecv(usbdevice *kb, uchar *in_msg, const char *file, int line)
os_usbrecv receives a max MSGSIZE long buffer from usb device
Definition: usb_linux.c:130
int os_setupusb(usbdevice *kb)
os_setupusb OS-specific setup for a specific usb device.
Definition: usb_linux.c:548
unsigned char uchar
Definition: includes.h:24
int usb_tryreset(usbdevice *kb)
usb_tryreset does what the name means: Try to reset the usb via resetusb()
Definition: usb.c:475
unsigned short ushort
Definition: includes.h:25
int closeusb(usbdevice *kb)
closeusb Close a USB device and remove device entry.
Definition: usb.c:687
int _usbsend(usbdevice *kb, const uchar *messages, int count, const char *file, int line)
_usbsend send a logical message completely to the given device
Definition: usb.c:542
const char * product_str(short product)
product_str returns a condensed view on what type of device we have.
Definition: usb.c:70
int revertusb(usbdevice *kb)
revertusb sets a given device to inactive (hardware controlled) mode if not a fw-ugrade is indicated ...
Definition: usb.c:417
void setupusb(usbdevice *kb)
setupusb starts a thread with kb as parameter and _setupusb() as entrypoint.
Definition: usb.c:396
int _resetusb(usbdevice *kb, const char *file, int line)
_resetusb Reset a USB device.
Definition: usb.c:436
void os_sendindicators(usbdevice *kb)
os_sendindicators update the indicators for the special keys (Numlock, Capslock and what else...
Definition: usb_linux.c:214
int _nk95cmd(usbdevice *kb, uchar bRequest, ushort wValue, const char *file, int line)
_nk95cmd If we control a non RGB keyboard, set the keyboard via ioctl with usbdevfs_ctrltransfer ...
Definition: usb_linux.c:189
int _usbrecv(usbdevice *kb, const uchar *out_msg, uchar *in_msg, const char *file, int line)
_usbrecv Request data from a USB device by first sending an output packet and then reading the respon...
Definition: usb.c:611
void usbkill()
Stop the USB system.
Definition: usb_linux.c:853
int os_usbsend(usbdevice *kb, const uchar *out_msg, int is_recv, const char *file, int line)
os_usbsend sends a data packet (MSG_SIZE = 64) Bytes long
Definition: usb_linux.c:68