LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 03-19-2012, 07:24 AM   #1
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Rep: Reputation: Disabled
Kernel code: calling suspend to ram function from keyboard module


Hello to you all linux lovers.
I have a dream.... I(ve been working on it for a while but I am getting out of clues...

My dream is to hack the kernel so that, being in console (not under X), pressing a key on the keyboard directly calls a suspend to ram function (the one that 'echo mem>/sys/power/state' calls for example).
So I looked at the kernel sources and in particular:
kernel/power/main.c and drivers/tty/vt/keyboard.c
In keyboard.c in one of the function handlers I have success to poweroff the machine using:
orderly_poweroff(0);

but now I would like to be able to use something like:
suspend_state_t state = PM_SUSPEND_MEM;
return = pm_suspend(state);

Compilation of the kernel works fine, but when I press the key I get a lot of characters and "call trace..."; I also tried to call enter_state(state) but with no success. Please note that 'echo mem>/sys/power/state' suspend to ram with success.

Any help would be deeply appreciated.

Greetings
 
Old 03-20-2012, 09:47 AM   #2
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
Arrow A good article on power management in the kernel

So... nobody has an idea? Or maybe this problem is not taken the right way?
Anyway, I found a very interesting article on the philosophy and the architecture of power management in the kernel: it's here:

landley.net/kdocs/ols/2003/ols2003-pages-325-339.pdf
(and attached to this post)

I am now trying to identify the variables that are passed to kernel/power/main.c when I type echo mem>/sys/power/state by using printk()...
One thing I want to try is writing directly to /sys/power/state using the sysfs_write_file() function defined in linux/fs/sysfs/file.c

cheers
Attached Files
File Type: pdf ols2003-pages-325-339.pdf (63.0 KB, 20 views)
 
Old 03-22-2012, 09:55 AM   #3
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hoping for ftrace in debugfs

I just found out that you could trace function calls via the debug file system (just check "debug file system" and "function trace" in the kernel hacking section of the kernel configuration menu).
Unfortunately when you turn on function tracing:

# mount -t debugfs none /sys/kernel/debug
# cd /sys/kernel/debug/tracing/
# echo function > current_tracer
# echo 1 > tracing_enabled

then suspend to ram doesn't work (actually it goes to sleep but at wakeup the bios decides to go through the whole booting process instead of just resuming)...

I WILL NEVER GIVE UP!!!!
 
Old 03-27-2012, 07:00 AM   #4
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
still here...

Still not giving up....
 
Old 03-28-2012, 09:30 AM   #5
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
debugfs

Ok this is turning into a debugfs issue.
I am now trying to get a simple echo something into the sysfs

(something not related to suspend to ram, but something that works, like echo 1 > /sys/devices/system/cpu/sched_mc_power_savings )

to be recorded with the function tracer, so I can find out which function I should trigger first...
The thing is, I can't get the tracing to work twice (the trace file stays empty the second time), I guess I get a buffer full or something.

Will work till death...
 
Old 03-29-2012, 06:54 AM   #6
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
ftrace - debugfs

I am progressing (very slightly) on the function identification using ftrace. I now understand that in the "debugfs" directory created at/sys/kernel/debug/tracing
when you compile your kernel (which version I am working on is 3.0.4 by the way) with the ftrace function (kernel hacking directory in make menuconfig), the trace file is not actually a real file, it answers to a reading using cat.

The thing is, reading from that file always gives a 159800 (or so) lines 13 Mo file that just records 1 to 2 seconds (amazing what these cpus accomplish these days !!!).

My new goal is to target the recording of function with ftrace when I echo something (with a reaction) into the sysfs...

This is turning into a blog... I strongly hope it will help other people interested in the kernel.
 
Old 04-11-2012, 05:57 AM   #7
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
Still not giving up, finding my way through the C language...
 
Old 04-20-2012, 06:14 AM   #8
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
Currently reading Linux Device Drivers, 3rd Edition By Jonathan Corbet, Greg Kroah-Hartman, Alessandro Rubini
Publisher : O'Reilly
Pub Date : February 2005
ISBN : 0-596-00590-3
Pages : 636
 
Old 04-27-2012, 08:49 AM   #9
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
narrowing the issue

Succeeded in suspending my system by calling pm_suspend (which is in kernel/power/suspend.c) from a module I created and insmod_ded...

Now my new question is: why can't I call successfully the function pm_suspend from drivers/tty/vt/keyboard.c when I am able to call it successfully from my own module?

Interrupt context origin of the call??
 
Old 05-12-2012, 04:36 AM   #10
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
hope....

To you people out there trying to transform an input into a useful kernel action (such as suspend to ram), keep on believing: I am sure it is possible.
I recently learned that inputs are not always handled by the keyboard module. A very good clue is done in console using
#watch cat /proc/interrupts
and type a key. If the number on the i8042 line increases, you very probably have a PS2 keyboard handled by the module which sources you can find in drivers/input/keyboard/atkbd.c in the linux kernel sources tree. You will find it very interesting that even on this type of keyboard typing the brightness increase/decrease keys is not handled by this module but by the acpi module!!!. On the other hand if you have a usb keyboard, at least in my case, key typing reporting is done by another module found at: drivers/hid/hid-input.c.

Now I have to call pm_suspend from one of this modules, being specific of a key type and key press/release action type, and I still think my problem is that I call a function that modifies a lot of things and is, by definition, long (time) to finish, so calling it from an interrupt context seems like a bad idea. I am planning on using the schedule_delayed_work() function found in atkbd module.

Anybody's help telling me whether or not this is the good direction to take is very welcome.
 
Old 05-18-2012, 06:38 AM   #11
belvy
LQ Newbie
 
Registered: Mar 2012
Location: France
Distribution: Custom
Posts: 15

Original Poster
Rep: Reputation: Disabled
Solved!

This WAS an interrupt issue. I could achieve calling suspend to ram from a keyboard key press by modifying the drivers/input/input.c file:
add to the includes:
#include <linux/workqueue.h>
#include <linux/suspend.h>


add before void input_event():
static void myfonction(struct work_struct *dummy)
{
pm_suspend(PM_SUSPEND_MEM);
}

static DECLARE_WORK(workname, myfonction);


add into input_event():
if (code == 142 && value == 1) {
schedule_work(&workname);
}


the websites that helped me most:
http://www.makelinux.com/ldd3/chp-10.shtml
http://www.ibm.com/developerworks/li...ets/index.html
 
Old 05-18-2012, 07:12 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,147

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
I shall read this (properly) in the morning. In the meantime, sounds like a "well done" is in order.
 
  


Reply



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
Kernel panic after suspend-to-RAM truboy Linux - Kernel 2 12-10-2010 01:39 AM
Keyboard Doesn't Work After Resuming from Suspend to Ram beartooth91 Slackware 11 03-05-2009 01:18 PM
System hangs after suspend-to-ram resume when using radeon module niidea Linux - Laptop and Netbook 1 12-13-2008 06:52 AM
Accessing Linux kernel code in RAM (from a module) bthorsten Linux - Kernel 1 03-25-2008 09:02 PM
calling function in kernel vishalbutte Programming 0 02-14-2006 07:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 10:33 AM.

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