next functions commented

This commit is contained in:
Lutz Eichler
2017-05-06 23:10:50 +02:00
parent 6f2fef7219
commit d448a738c3
6 changed files with 87 additions and 20 deletions
+2 -1
View File
@@ -31,7 +31,8 @@ int setactive_kb(usbdevice* kb, int active);
int setactive_mouse(usbdevice* kb, int active);
///
/// \brief setactive() calls via the corresponding kb->vtable either the active() or the idle() function.
/// \n What function is effectively called is device dependent. Have a look at \a device_vtable.c for more information
/// \n active() is called if the parameter makeactive is true, idle if it is false.
/// \n What function is called effectively is device dependent. Have a look at \a device_vtable.c for more information.
#define setactive(kb, makeactive) ((makeactive) ? (kb)->vtable->active((kb), 0, 0, 0, 0) : (kb)->vtable->idle((kb), 0, 0, 0, 0))
// Command: Activate a keyboard
+17
View File
@@ -35,6 +35,23 @@ int rm_recursive(const char* path){
return remove(path);
}
///
/// \brief _updateconnected Update the list of connected devices.
///
/// <devicepath> normally is /dev/input/ckb or /input/ckb.
/// \n Open the normal file under <devicepath>0/connected for writing.
/// For each device connected, print its devicepath+number,
/// the serial number of the usb device and the usb name of the device connected to that usb interface.
/// \n eg:
/// \n /dev/input/ckb1 0F022014ABABABABABABABABABABA999 Corsair K95 RGB Gaming Keyboard
/// \n /dev/input/ckb2 0D02303DBACBACBACBACBACBACBAC998 Corsair M65 RGB Gaming Mouse
///
/// Set the file ownership to root.
/// If the glob var gid is explicitly set to something different from -1 (the initial value), set file permission to 640, else to 644.
/// This is used if you start the daemon with --gid=<GID> Parameter.
///
/// Because several independent threads may call updateconnected(), protect that procedure with locking/unlocking of \b devmutex.
///
void _updateconnected(){
pthread_mutex_lock(devmutex);
char cpath[strlen(devpath) + 12];
+12 -9
View File
@@ -4,10 +4,10 @@
#include "includes.h"
#include "usb.h"
// Device path base ("/dev/input/ckb" or "/var/run/ckb")
/// Device path base ("/dev/input/ckb" or "/var/run/ckb")
const char *const devpath;
// Group ID for the control nodes. -1 to give read/write access to everybody
/// Group ID for the control nodes. -1 to give read/write access to everybody
extern long gid;
// Simple file permissions
@@ -17,22 +17,25 @@ extern long gid;
#define S_CUSTOM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
#define S_CUSTOM_R (S_IRUSR | S_IWUSR | S_IRGRP)
// Update the list of connected devices.
/// Update the list of connected devices.
void updateconnected();
// Create a dev path for the keyboard at index. Returns 0 on success.
/// Create a dev path for the keyboard at index. Returns 0 on success.
int mkdevpath(usbdevice* kb);
// Remove the dev path for the keyboard at index. Returns 0 on success.
/// Remove the dev path for the keyboard at index. Returns 0 on success.
int rmdevpath(usbdevice* kb);
// Creates a notification node for the specified keyboard.
/// Creates a notification node for the specified keyboard.
int mknotifynode(usbdevice* kb, int notify);
// Removes a notification node for the specified keyboard.
/// Removes a notification node for the specified keyboard.
int rmnotifynode(usbdevice* kb, int notify);
// Writes a keyboard's firmware version and poll rate to its device node.
/// Writes a keyboard's firmware version and poll rate to its device node.
int mkfwnode(usbdevice* kb);
// Custom readline is needed for FIFOs. fopen()/getline() will die if the data is sent in too fast.
/// Custom readline is needed for FIFOs. fopen()/getline() will die if the data is sent in too fast.
typedef struct _readlines_ctx* readlines_ctx;
void readlines_ctx_init(readlines_ctx* ctx);
void readlines_ctx_free(readlines_ctx ctx);
+10 -7
View File
@@ -189,6 +189,8 @@ static void* devmain(usbdevice* kb){
/// the routine goes into the same error handling:
/// It goes via goto to one of two exit labels.
/// The difference is whether or not an unlock has to be performed on the imutex variable.
///
/// In both cases, closeusb() is called, then an unlock is performed on the dmutex.
///
@@ -198,10 +200,11 @@ static void* devmain(usbdevice* kb){
/// In either case, the routine terminates with a void* 0
/// because either devmain() has returned constant null or the routine itself returns zero.
///
/// The basic idea of ​​the routine is the following:
/// The basic idea of this routine is the following:
///
static void* _setupusb(void* context){
/// First some initialization of kb standard structured and local vars is done:
/// \n First some initialization of kb standard structured and local vars is done.
/// - \b kb is set to the pointer given from start environment
/// - local vars \b vendor and \b product are set to the values from the corresponding fields of kb
/// - local var \b vt \b and the \b kb->vtable are both set to the retval of \a get_vtable()
@@ -237,7 +240,7 @@ static void* _setupusb(void* context){
goto fail;
///
/// - The following two statements deal with possible errors when setting the kb values ​​in the current routine:
/// - The following two statements deal with possible errors when setting the kb values in the current routine:
/// If the version or the name was not read correctly, they are set to default values:
/// - serial is set to "<vendor>: <product> -NoID"
/// - the name is set to "<vendor> <product>".
@@ -251,7 +254,7 @@ static void* _setupusb(void* context){
///
/// - Then the user level input subsystem is activated via os_openinput().
/// There are two file descriptors, one for the mouse and one for the keyboard.
/// <b>As mentioned in structures.h, not the just opened FD numbers are stored under kb->uinput_kb or kb->uinput_mouse, but the values ​​increased by 1!</b>
/// <b>As mentioned in structures.h, not the just opened FD numbers are stored under kb->uinput_kb or kb->uinput_mouse, but the values increased by 1!</b>
/// The reason is, if the open fails or not open has been done until now,
/// that struct member is set to 0, not to -1 or other negative value.
/// So all usage of this kb->handle must be something like \c "kb->handle - 1", as you can find it in the code.
@@ -309,7 +312,7 @@ static void* _setupusb(void* context){
/// - In start_kb_nrgb() set the keyboard into a so-called software mode (NK95_HWOFF)
/// via ioctl with \c usbdevfs_ctrltransfer in function _nk95cmd(),
/// which will in turn is called via macro nk95cmd() via start_kb_nrgb().
/// \n Then two dummy values ​​(active and pollrate) are set in the kb structure and ready.
/// \n Then two dummy values (active and pollrate) are set in the kb structure and ready.
/// - start_dev() does a bit more - because this function is for both mouse and keyboard.
/// start_dev() calls - after setting an extended timeout parameter - _start_dev(). Both are located in device.c.
/// - First, _start_dev() attempts to determine the firmware version of the device,
@@ -483,7 +486,7 @@ extern int hwload_mode;
/// An essential constant parameter which is relevant for os_usbsend() only is is_recv = 0, which means sending.
///
/// Now it gets a little complicated again:
/// - Returns os_usbsend() 0, only zero bytes could be sent in one of the packets,
/// - If os_usbsend() returns 0, only zero bytes could be sent in one of the packets,
/// or it was an error (-1 from the systemcall), but not a timeout.
/// How many Bytes were sent in total from earlier calls does not seem to matter,
/// _usbsend() returns a total of 0.
@@ -562,7 +565,7 @@ int _usbsend(usbdevice* kb, const uchar* messages, int count, const char* file,
///
/// os_usbrecv() returns 0, -1 or something else.
/// \n Zero signals a serious error which is not treatable and _usbrecv() also returns 0.
/// \n -1 means that it is a true-to-treatable error - a timeout for example -
/// \n -1 means that it is a treatable error - a timeout for example -
/// and therefore the next transfer attempt is started after a long pause (DELAY_LONG)
/// if not reset_stop or the wrong hwload_mode require a termination with a return value of 0.
///
+2 -2
View File
@@ -220,7 +220,7 @@ int os_resetusb(usbdevice* kb, const char* file, int line);
/// \param[IN] file for debugging
/// \param[IN] line for debugging
/// \param[in] reset_stop global variable is read
/// \return number of Bytes sent (ideal == count * MSG_SIZE);\n 0 if a block could not be sent and it was not a timeout OR reset_stop was required or hw_load is not set to always
/// \return number of Bytes sent (ideal == count * MSG_SIZE);\n 0 if a block could not be sent and it was not a timeout OR \b reset_stop was required or \b hwload_mode is not set to "always"
int _usbsend(usbdevice* kb, const uchar* messages, int count, const char* file, int line);
/// \brief usbsend macro is used to wrap _usbsend() with debugging information (file and lineno)
@@ -250,7 +250,7 @@ int _usbrecv(usbdevice* kb, const uchar* out_msg, uchar* in_msg, const char* fil
/// \brief os_usbsend send a data packet (MSG_SIZE = 64) Bytes long
/// \param kb THE usbdevice*
/// \param out_msg the MSGSIZE char long buffer to send
/// \param is_recv if true, just send an ioctl for further reading packets. if false, send the data at \b out_msg.
/// \param is_recv if true, just send an ioctl for further reading packets. If false, send the data at \b out_msg.
/// \param file for debugging
/// \param line for debugging
/// \return -1 on timeout (try again), 0 on hard error, numer of bytes sent otherwise
+44 -1
View File
@@ -12,7 +12,50 @@ static char kbsyspath[DEV_MAX][FILENAME_MAX];
/// \details
/// \brief os_usbsend send a data packet (MSG_SIZE = 64) Bytes long
/// \todo commenting
///
/// os_usbsend has two functions:
/// - if is_recv == false, it tries to send a given MSG_SIZE buffer via the usb interface given with kb.
/// - otherwise a request is sent via the usb device to initiate the receiving of a message from the remote device.
///
/// The functionality for sending distinguishes two cases,
/// depending on the version number of the firmware of the connected device:
/// \n If the firmware is at or above 1.2, the transmission is done via an ioctl().
/// The ioctl() is given a struct usbdevfs_ctrltransfer, in which the relevant parameters are entered:
/// \n bRequestType = 0x21, bRequest = 0x09, wValue = 0x0200, endpoint to be addressed from epcount, MSG_SIZE, timeout = 5ms, the message buffer pointer.
/// \n The ioctl command is USBDEVFS_CONTROL.
///
/// The same constellation is used if the device is requested to send its data (is_recv = true).
///
/// For a more recent firmware and is_recv = false,
/// the ioctl command USBDEVFS_CONTROL is not used
/// (this tells the bus to enter the control mode),
/// but the bulk method is used: USBDEVFS_BULK.
/// For this purpose, a different structure is used for the ioctl() (struct \b usbdevfs_bulktransfer)
/// and this is also initialized differently:
/// \n The length and timeout parameters are given the same values as above.
/// The formal parameter out_msg is also passed as a buffer pointer.
/// For the endpoints, the firmware version is differentiated again:
/// \n For a firmware version between 1.3 and <2.0 endpoint 4 is used,
/// otherwise (it can only be >=2.0) endpoint 3 is used.
///
/// \todo Since the handling of endpoints has already led to problems elsewhere, this implementation is extremely hardware-dependent and critical!
/// \n Eg. the new keyboard K95PLATINUMRGB has a version number significantly less than 2.0 - will it run with this implementation?
///
/// The ioctl() - no matter what type -
/// returns the number of bytes sent.
/// Now comes the usual check:
/// - If the return value is -1 AND the error is a timeout (ETIMEOUT),
/// os_usbsend() will return -1 to indicate that it is probably a recoverable problem and a retry is recommended.
/// - For another negative value or other error identifier OR 0 bytes sent, 0 is returned as a heavy error identifier.
/// - In all other cases, the function returns the number of bytes sent.
///
/// If this is not the entire blocksize (MSG_SIZE bytes),
/// an error message is issued on the standard error channel
/// \n [warning "Wrote YY bytes (expected 64)"].
///
/// If DEBUG_USB is set during compilation,
/// the number of bytes sent and their representation are logged to the error channel.
///
int os_usbsend(usbdevice* kb, const uchar* out_msg, int is_recv, const char* file, int line){
int res;
if(kb->fwversion >= 0x120 && !is_recv){