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 File Reference
#include "command.h"
#include "device.h"
#include "firmware.h"
#include "profile.h"
#include "usb.h"
+ Include dependency graph for device.c:

Go to the source code of this file.

Functions

int _start_dev (usbdevice *kb, int makeactive)
 
int start_dev (usbdevice *kb, int makeactive)
 

Variables

int hwload_mode = 1
 hwload_mode = 1 means read hardware once. should be enough More...
 
usbdevice keyboard [9]
 remember all usb devices. Needed for closeusb(). More...
 
pthread_mutex_t devlistmutex = PTHREAD_MUTEX_INITIALIZER
 
pthread_mutex_t devmutex [9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }
 Mutex for handling the usbdevice structure. More...
 
pthread_mutex_t inputmutex [9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }
 Mutex for dealing with usb input frames. More...
 
pthread_mutex_t macromutex [9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }
 Protecting macros against lightning: Both use usb_send. More...
 
pthread_mutex_t macromutex2 [9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }
 Protecting the single link list of threads and the macrovar. More...
 
pthread_cond_t macrovar [9] = { [0 ... 9 -1] = PTHREAD_COND_INITIALIZER }
 This variable is used to stop and wakeup all macro threads which have to wait. More...
 

Function Documentation

int _start_dev ( usbdevice kb,
int  makeactive 
)

_start_dev get fw-info and pollrate; if available, install new firmware; get all hardware profiles.

Parameters
kbthe normal kb pointer to the usbdevice. Is also valid for mice.
makeactiveif set to 1, activate the device via setactive()
Returns
0 if success, other else
  • This hacker code is tricky in mutliple aspects. What it means is:
    if hwload_mode == 0: just set pollrate to 0 and clear features in the bottom lines of the if-block.
    if hwload_mode == 1: if the device has FEAT_HWLOAD active, call getfwversion(). If it returns true, there was an error while detecting fw-version. Put error message, reset FEAT_HWLOAD and finalize as above.
    if hwload_mode == 2: if the device has FEAT_HWLOAD active, call getfwversion(). If it returns true, there was an error while detecting fw-version. Put error message and return directly from function with error.
    Why do not you just write it down?
  • Now check if device needs a firmware update. If so, set it up and leave the function without error.
  • Device needs a firmware update. Finish setting up but don't do anything.
  • Load profile from device if the hw-pointer is not set yet and hw-loading is possible and allowed.
    return error if mode == 2 (load always) and loading got an error. Else reset HWLOAD feature, because hwload must be 1.

    That is real Horror code.

Definition at line 25 of file device.c.

References usbdevice::active, ckb_info, ckb_warn, DEV_MAX, FEAT_ADJRATE, FEAT_FWUPDATE, FEAT_FWVERSION, FEAT_HWLOAD, FEAT_POLLRATE, FEAT_RGB, usbdevice::features, usbdevice::fwversion, getfwversion(), HAS_FEATURES, usbdevice::hw, hwload_mode, hwloadprofile, NEEDS_FW_UPDATE, usbdevice::pollrate, and setactive.

Referenced by start_dev().

25  {
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 }
#define FEAT_FWVERSION
Definition: structures.h:142
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
char active
Definition: structures.h:231
#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
char pollrate
Definition: structures.h:241
#define NEEDS_FW_UPDATE(kb)
Definition: structures.h:161
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
#define HAS_FEATURES(kb, feat)
Definition: structures.h:157
#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
#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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int start_dev ( usbdevice kb,
int  makeactive 
)

Definition at line 89 of file device.c.

References _start_dev(), USB_DELAY_DEFAULT, and usbdevice::usbdelay.

89  {
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 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
int _start_dev(usbdevice *kb, int makeactive)
Definition: device.c:25
char usbdelay
Definition: structures.h:243

+ Here is the call graph for this function:

Variable Documentation

pthread_mutex_t devlistmutex = PTHREAD_MUTEX_INITIALIZER

Definition at line 11 of file device.c.

pthread_mutex_t devmutex[9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }

Definition at line 12 of file device.c.

Referenced by _updateconnected(), quitWithLock(), and usb_rm_device().

int hwload_mode = 1

hwload_mode is defined in device.c

Definition at line 7 of file device.c.

Referenced by _start_dev(), _usbrecv(), _usbsend(), and main().

pthread_mutex_t inputmutex[9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }

Definition at line 13 of file device.c.

pthread_mutex_t macromutex[9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }

Definition at line 14 of file device.c.

pthread_mutex_t macromutex2[9] = { [0 ... 9 -1] = PTHREAD_MUTEX_INITIALIZER }

Definition at line 15 of file device.c.

pthread_cond_t macrovar[9] = { [0 ... 9 -1] = PTHREAD_COND_INITIALIZER }

Definition at line 16 of file device.c.