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
device.c
Go to the documentation of this file.
1 #include "command.h"
2 #include "device.h"
3 #include "firmware.h"
4 #include "profile.h"
5 #include "usb.h"
6 
7 int hwload_mode = 1;
8 
9 // Device list
11 pthread_mutex_t devlistmutex = PTHREAD_MUTEX_INITIALIZER;
12 pthread_mutex_t devmutex[DEV_MAX] = { [0 ... DEV_MAX-1] = PTHREAD_MUTEX_INITIALIZER };
13 pthread_mutex_t inputmutex[DEV_MAX] = { [0 ... DEV_MAX-1] = PTHREAD_MUTEX_INITIALIZER };
14 pthread_mutex_t macromutex[DEV_MAX] = { [0 ... DEV_MAX-1] = PTHREAD_MUTEX_INITIALIZER };
15 pthread_mutex_t macromutex2[DEV_MAX] = { [0 ... DEV_MAX-1] = PTHREAD_MUTEX_INITIALIZER };
16 pthread_cond_t macrovar[DEV_MAX] = { [0 ... DEV_MAX-1] = PTHREAD_COND_INITIALIZER };
17 
25 int _start_dev(usbdevice* kb, int makeactive){
26  // Get the firmware version from the device
27  if(kb->pollrate == 0){
35  if(!hwload_mode || (HAS_FEATURES(kb, FEAT_HWLOAD) && getfwversion(kb))){
36  if(hwload_mode == 2)
37  // hwload=always. Report setup failure.
38  return -1;
39  else if(hwload_mode){
40  // hwload=once. Log failure, prevent trying again, and continue.
41  ckb_warn("Unable to load firmware version/poll rate\n");
42  kb->features &= ~FEAT_HWLOAD;
43  }
44  kb->pollrate = 0;
46  if(kb->fwversion == 0)
48  }
49  }
54  if(NEEDS_FW_UPDATE(kb)){
56  ckb_info("Device needs a firmware update. Please issue a fwupdate command.\n");
58  kb->active = 1;
59  return 0;
60  }
66  if(!kb->hw && hwload_mode && HAS_FEATURES(kb, FEAT_HWLOAD)){
67  if(hwloadprofile(kb, 1)){
68  if(hwload_mode == 2)
69  return -1;
70  ckb_warn("Unable to load hardware profile\n");
71  kb->features &= ~FEAT_HWLOAD;
72  }
73  }
74  // Activate software mode if requested
75  if(makeactive)
76  return setactive(kb, 1);
77  #ifdef DEBUG
78  // 12 for each device + null terminator
79  char devlist[12*(DEV_MAX-1)+1];
80  int devlistpos = 0;
81  for(unsigned i = 1; i < DEV_MAX; i++){
82  devlistpos += sprintf(&devlist[devlistpos], "%u: 0x%x; ", i, keyboard[i].product);
83  }
84  ckb_info("Attached Devices: %s\n", devlist);
85  #endif
86  return 0;
87 }
88 
89 int start_dev(usbdevice* kb, int makeactive){
90  // Force USB interval to 10ms during initial setup phase; return to nominal 5ms after setup completes.
91  kb->usbdelay = 10;
92  int res = _start_dev(kb, makeactive);
94  return res;
95 }
#define FEAT_FWVERSION
Definition: structures.h:142
int start_dev(usbdevice *kb, int makeactive)
Definition: device.c:89
ushort fwversion
Definition: structures.h:239
#define FEAT_RGB
Definition: structures.h:136
usbdevice keyboard[9]
remember all usb devices. Needed for closeusb().
Definition: device.c:10
pthread_mutex_t macromutex2[9]
Protecting the single link list of threads and the macrovar.
Definition: device.c:15
pthread_mutex_t devmutex[9]
Mutex for handling the usbdevice structure.
Definition: device.c:12
char active
Definition: structures.h:231
pthread_mutex_t devlistmutex
Definition: device.c:11
#define FEAT_HWLOAD
Definition: structures.h:144
#define ckb_warn(fmt, args...)
Definition: includes.h:52
#define ckb_info(fmt, args...)
Definition: includes.h:55
#define USB_DELAY_DEFAULT
This constant is used to initialize kb->usbdelay. It is used in many places (see macros above) but of...
Definition: usb.h:192
char pollrate
Definition: structures.h:241
#define NEEDS_FW_UPDATE(kb)
Definition: structures.h:161
int _start_dev(usbdevice *kb, int makeactive)
Definition: device.c:25
pthread_cond_t macrovar[9]
This variable is used to stop and wakeup all macro threads which have to wait.
Definition: device.c:16
hwprofile * hw
Definition: structures.h:223
int hwload_mode
hwload_mode = 1 means read hardware once. should be enough
Definition: device.c:7
ushort features
Definition: structures.h:229
char usbdelay
Definition: structures.h:243
#define HAS_FEATURES(kb, feat)
Definition: structures.h:157
pthread_mutex_t inputmutex[9]
Mutex for dealing with usb input frames.
Definition: device.c:13
Definitions for using USB interface.
#define DEV_MAX
Definition: device.h:8
#define FEAT_ADJRATE
Definition: structures.h:139
#define hwloadprofile(kb, apply)
Definition: profile.h:52
#define FEAT_FWUPDATE
Definition: structures.h:143
pthread_mutex_t macromutex[9]
Protecting macros against lightning: Both use usb_send.
Definition: device.c:14
#define FEAT_POLLRATE
Definition: structures.h:138
int getfwversion(usbdevice *kb)
Definition: firmware.c:11
#define setactive(kb, makeactive)
setactive() calls via the corresponding kb->vtable either the active() or the idle() function...
Definition: device.h:44