Kernel Programming

This is a discussion on Kernel Programming within the Linux Programming forums, part of the Platform Specific Boards category; insert Code: #include <linux/kernel.h> #include <linux/module.h> #include <linux/usb.h> MODULE_LICENSE("GPL"); struct usb_device_descriptor desc; static int prog_init(void) { if(desc.bDeviceClass==0) { printk(KERN_ALERT "Detected"); ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    5

    Kernel Programming

    insert
    Code:
    #include <linux/kernel.h>
       #include <linux/module.h>
       #include <linux/usb.h>
      
    MODULE_LICENSE("GPL");
    
    struct usb_device_descriptor desc;
    static int prog_init(void)
    {
       if(desc.bDeviceClass==0)
       { 
       
       printk(KERN_ALERT "Detected");
       // char manu=desc.iManufacturer;
        printk(KERN_DEBUG "Manufacturer:%d",desc.idVendor);
       printk(KERN_DEBUG "Product:%d",desc.idProduct);
       
       }
       else 
        printk(KERN_ALERT "No removable storage found");
       return 0;
    }
    static void prog_exit(void)
    {
        printk(KERN_ALERT "Program Terminated\n");
    }
    module_init(prog_init);
    module_exit(prog_exit);
    I have written a simple kernel program to detect USB.The desc.bDeviceClass returns __u16.So when desc.bDeviceClass ==0 a removable drive is detected.But the problem is that it always enters IF even when the drive is removed.Is there something wrong with the comparison?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    I've no idea.
    But perhaps comparing with what other people do might help -> Linux identifier search "usb_device_descriptor" - Linux Cross Reference - Free Electrons
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You are declaring 'desc' in global memory, so all integer variables will be zero at the point where your module commences execution 'prog_init'. If you want to gather USB info, there will surely be a kernel function that will need to called before examining this data. I dont have my books with me so I cant tell you what it is...

    It will be in here somewhere - http://tldp.org/

  4. #4
    Banned
    Join Date
    Jul 2011
    Posts
    3
    maybe this might help u

    endif
    This directive ends the chain of matching if, elseif or else directive(s) controlling the conditional processing of configuration file directives.

    if EXPRESSION
    any config lines
    elseif EXPRESSION
    any config lines
    else
    any config lines
    endif

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    @jason: Hello there, just a quick word of advice -- don't bump old threads. Also:
    endif
    This directive ends the chain of matching if, elseif or else directive(s) controlling the conditional processing of configuration file directives.
    This does not exist in C.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Kernel programming
    By paoloC in forum Linux Programming
    Replies: 6
    Last Post: 01-26-2010, 12:19 PM
  2. kernel programming
    By aliasghar in forum Linux Programming
    Replies: 4
    Last Post: 10-19-2009, 03:46 AM
  3. Linux Kernel I/O C programming
    By inferno_gogo in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 03:44 PM
  4. Programming a Kernel
    By Paz_Rax in forum C++ Programming
    Replies: 0
    Last Post: 07-20-2004, 06:23 AM
  5. kernel programming with C++
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2001, 06:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21