-   Linux - Newbie (
-   -   Keycodes Print to /Proc Entry (

roboninja123 08-02-2012 04:36 PM

Keycodes Print to /Proc Entry
Hey all,

I've been working on this and researching the /proc file system for a couple of weeks now. I was able to get a simple data structure to fill and print in a desired manner, but it is when I am trying to print data from a data structure that is filled by another function is where I run into problems. All of the code I am positing is added into input.c as a modification. I assume that the keycode data that I need is handled by input.c, that is why I modified it directly.

Here is the code to set up the /proc entry:

static struct proc_dir_entry *keycode_entry;

struct keycode_t {
unsigned int key_code_array[2];

/* The array within this data structure is supposed to be filled
* by the function input_fetch_keycode located in input.c
static struct keycode_t keycode_data;

static struct keycode_t *keycode_out;

* This is the read_proc callback function
static int read_proc_keycode(char *buf, char **start, off_t offset, int count, int *eof, void *data){
int len = 0;
struct keycode_t *new_data = (struct keycode_t *)data;
if(offset > 0){
len = 0;
len += sprintf(buf+len, "Previous Keycode: %d\nCurrent Keycode: %d", new_data->key_code_array[0], new_data->key_code_array[1]);
return len;

* This is the function that initializes the proc entry module
static int keycode_proc_init(void){

// allocate a space in memory for keycode_out
keycode_out = kmalloc(sizeof(struct keycode_t), GFP_KERNEL);

// populate (*keycode_out) with data stored in (keycode_data)
keycode_out->key_code_array[0] = keycode_data.key_code_array[0];
keycode_out->key_code_array[1] = keycode_data.key_code_array[1];

keycode_entry = create_proc_entry("keycodes", 0644, NULL);

keycode_entry->data = keycode_out;
keycode_entry->read_proc = read_proc_keycode;

return 0;

void keycode_proc_exit(void){
remove_proc_entry("keycodes", NULL);


Basic Idea of this code: This code sets up a simple data structure that contains one entry of type unsigned int array[2]. I am assuming that the input_fetch_keycode() function gets called at some point in time when a key on the keyboard is pressed. Therefore I modified the existing code to implement writing to the static data structure keycode_data. This is only part of the input_fetch_keycode() function, the red is the code that I added:

static unsigned int input_fetch_keycode(struct input_dev *dev, unsigned int index)
switch (dev->keycodesize) {
case 1:
if(keycode_data.key_code_array[0] != 0){
if(keycode_data.key_code_array[1] != 0){
keycode_data.key_code_array[0] = keycode_data.key_code_array[1];
keycode_data.key_code_array[1] = ((u8 *)dev->keycode)[index];
keycode_data.key_code_array[1] = ((u8 *)dev->keycode)[index];
keycode_data.key_code_array[0] = ((u8 *)dev->keycode)[index];

return ((u8 *)dev->keycode)[index];

case 2: ...

Here is the output printed in the proc entry:

Previous Keycode: 0
Current Keycode: 0

This is not the only code that I have written for this same idea that has yielded zeroes for the output.

Is it ok to assume that the input_fetch_keycode() function is ALWAYS called somewhere when there is a key press from a USB keyboard?

If it is called EVERY TIME, then why do I constantly see zeroes for my output when I am clearly writing the keycode values to the static structure keycode_data?

If its not guaranteed to be called for every keypress, then how can I get the keycodes into any sort of structure so it can be printed to a /proc entry?

Sorry for the long post and thank you greatly for your replies!!!

All times are GMT -5. The time now is 09:47 PM.