LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   How to register user space call back function with USB driver in Linux? (https://www.linuxquestions.org/questions/linux-kernel-70/how-to-register-user-space-call-back-function-with-usb-driver-in-linux-899432/)

psmurthy 08-25-2011 01:29 PM

How to register user space call back function with USB driver in Linux?
 
Hi,
Can anybody tell me how to register user space call back function with USB driver? In call back function i would like to do some checks from user space.
Regards,
Murthy

SharpyWarpy 08-25-2011 10:10 PM

I have no idea how to do what you want but in order for others to help you please post the output of the following commands:
lsusb
--which gives information about your usb devices,
lsmod
--which gives a list of all loaded drivers.
When the pertinent loaded drivers are found running:
modinfo driver
The last command should tell you what options for the driver are available. Maybe someone else can help you from there. Good luck.

psmurthy 09-04-2011 01:10 AM

using netlink socket to get uevents on usb insert/remove in Linux
 
Hi .. I did little research and came to know we can get uevents from linux kernek on device insert/remove over netlink sockets.My question is now,How to get uevents to my user space task on USB insert/remove using netlink sockets.
I got piece of code in web. I modified it little bit and ran.
But it is not working.Here is the code.
Code:

#include <sys/socket.h>
#include <linux/netlink.h>
#include <stdio.h>
struct sockaddr_nl src_addr, dest_addr; 
struct nlmsghdr *nlh = NULL; 
struct iovec iov; 
struct msghdr msg; 
int sock_fd; 
void main() {

sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_ISCSI); 
if(sock_fd < 0) 
printf("Socket creatioin failed\n"); 

 else 
  printf("soscet created succfully\n"); 

 memset(&src_addr, 0, sizeof(src_addr)); 
 src_addr.nl_family = AF_NETLINK; 
 src_addr.nl_pid = getpid();  /* self pid */ 
 src_addr.nl_groups = 0;  /* not in mcast groups */ 

 if(bind(sock_fd, (struct sockaddr*)&src_addr,sizeof(src_addr)) < 0) 
  printf("bind Error\n"); 
 else 
  printf("Bind Successful\n"); 

 memset(&dest_addr, 0, sizeof(dest_addr)); 
 dest_addr.nl_family = AF_NETLINK; 
 dest_addr.nl_pid = 0;  /* For Linux Kernel */ 
 dest_addr.nl_groups = 0; /* unicast */ 

 nlh=(struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD)); 

 /* Fill the netlink message header */ 
 nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); 
 nlh->nlmsg_pid = getpid();  /* self pid */ 
 nlh->nlmsg_flags = 0; 

 /* Fill in the netlink message payload */ 
 strcpy(NLMSG_DATA(nlh), "Hello you!"); 

 iov.iov_base = (void *)nlh; 
 iov.iov_len = nlh->nlmsg_len; 

 msg.msg_name = (void *)&dest_addr; 
 msg.msg_namelen = sizeof(dest_addr); 
 msg.msg_iov = &iov; 
 msg.msg_iovlen = 20; 

 // sendmsg(sock_fd, &msg, 0); 

 /* Read message from kernel */ 
 memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD)); 
 printf("%s %d\n",__FILE__,__LINE__); 
 recvmsg(sock_fd, &msg, 0); 
 printf("%s %d\n",__FILE__,__LINE__); 
 printf("Received message payload: %s\n",(char *)NLMSG_DATA(nlh)); 

 /* Close Netlink Socket */ 
 close(sock_fd); 
}

I tested it in following way
1.Start the above program
2.Insert an USB disk.
expected behavior: messages should come on to console
actual behavior: i didn't see any messages and program blocked at "recvmsg"
as it is not receiving any messages from kernel.
Appreciate your help

Regards,
Ramji

archtoad6 09-04-2011 10:56 AM

Welcome to LQ. Hope your time here helps you as much as mine has helped me.

Please put code, command line output, config files, etc. inside [CODE] tags, aka "Code:" blocks.
There are 2 ways to generate "Code:" tags:
  1. You can simply type them yourself -- "[code] ... [/code]". (They are not case sensitive.)

  2. You can also click on the '#' icon above the text entry window. If you highlight the code before you click the '#' icon, your code, command line output, config files, etc. will get put inside a "Code:" block. The '#' icon is available when you start your post by clicking the "Post Reply" button or the "Go Advanced" button; it is not currenetly available in the Quick Reply window.

They will make your posts easier to read, & that will get you more, faster, better answers. -- Help us help you.
BTW, You can edit your post(s) to do this retroactively. Please do so soon.

Thank you, & again, welcome.

---------- Post added 09-04-11 at 10:56 AM ----------

Moved: This thread is more suitable in Kernel and has been moved accordingly to help your thread/question get the exposure it deserves.


All times are GMT -5. The time now is 01:48 PM.