Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 08-02-2012, 05:36 PM   #1
LQ Newbie
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
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!!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] write on /proc entry through user space karan2386 Linux - Kernel 1 03-26-2012 09:38 AM
command to write into proc entry pravin.embedded Linux - Newbie 1 09-11-2008 09:03 AM
maximum size of entry in /proc linuxdoniv Programming 3 04-01-2008 04:41 PM
writing a structure to the a entry in /proc linuxdoniv Programming 2 03-20-2008 08:05 AM
How to read from /proc entry in another kernel module? linux_lover2005 Programming 0 01-14-2005 06:44 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration