Thread: USB port in linux

  1. #1
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    Question USB port in linux

    HI, i was wondering how to access USB port in C on a linux computer....i would like just send signals to other computers, or just to run a DC motor.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Pretty sure you will have to write a kernel module. Have a look at this thread from today:

    About Device Driver Programming And Socket Programming

    And maybe you will find this interesting:

    Writing a Simple USB Driver

    I do remember getting slightly peeved about that last one since there is some crucial information absent from it* which you will realize after being frustrated by it (the nastiest kind of hole you can leave in a demo, I know!), but it does provide a nice clear acurate (IMO) perspective.

    * even worse I can't remember what it was and am doing totally unrelated things right now, so don't have the headspace to figure it out...sorry
    Last edited by MK27; 09-01-2009 at 04:34 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Technically USB is not a port, it's a bus.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Magos View Post
    Technically USB is not a port, it's a bus.
    Internally its a bus, externally it has a port, electronically its accessed through a logical port.

  5. #5
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Quote Originally Posted by MK27 View Post
    Pretty sure you will have to write a kernel module. Have a look at this thread from today:

    About Device Driver Programming And Socket Programming

    And maybe you will find this interesting:

    Writing a Simple USB Driver

    I do remember getting slightly peeved about that last one since there is some crucial information absent from it* which you will realize after being frustrated by it (the nastiest kind of hole you can leave in a demo, I know!), but it does provide a nice clear acurate (IMO) perspective.

    * even worse I can't remember what it was and am doing totally unrelated things right now, so don't have the headspace to figure it out...sorry
    I know this sound nooblish but, those commands :
    Code:
    static struct usb_driver led_driver = {
    	.owner =	THIS_MODULE,
    	.name =		"usbled",
    	.probe =	led_probe,
    	.disconnect =	led_disconnect,
    	.id_table =	id_table,
    };
    
    and others
    do i execute them in a C program or shell (terminal)?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    No, it is a C structure assignment. (You will need to read up about structs in C if you do not know what they are). struct usb_driver is defined somewhere else (in one of the kernel headers). led_driver is a struct usb_driver. Here's a parallel example:

    Code:
    #include <stdio.h>
    
    struct form {      /* struct form definition */
    	const char *name;
    };
    
    int main() {
    	struct form eg = {   /* struct form eg declaration and assignment */
    		.name = "hello world",
    	};
    	printf("%s\n",eg.name);
    	return 0;
    }
    Make sense? This may be C99 syntax but evidently it is okay with the kernel and linux programming generally.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    Thanks

    Quote Originally Posted by MK27 View Post
    No, it is a C structure assignment. (You will need to read up about structs in C if you do not know what they are). struct usb_driver is defined somewhere else (in one of the kernel headers). led_driver is a struct usb_driver. Here's a parallel example:

    Code:
    #include <stdio.h>
    
    struct form {      /* struct form definition */
    	const char *name;
    };
    
    int main() {
    	struct form eg = {   /* struct form eg declaration and assignment */
    		.name = "hello world",
    	};
    	printf("%s\n",eg.name);
    	return 0;
    }
    Make sense? This may be C99 syntax but evidently it is okay with the kernel and linux programming generally.
    thank you, actually i speak Croatian, not English, I understand "writing ssimple kernel driver" document, but i do not in some way...one thing i dont understansd, is in wich order do these commands go...could you please explain me that(or just post the code) (i know i shouldnt ask you to do that, sry i rely need it please.)):

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by yann View Post
    thank you, actually i speak Croatian, not English, I understand "writing ssimple kernel driver" document, but i do not in some way...one thing i dont understansd, is in wich order do these commands go...
    It must be hard to deal with a language barrier for material like that. I am always surprised more people do not translate stuff like this, voluntarily -- I suppose by the time your english is good enough, maybe you cannot be bothered to help anyone else out? Anyway...

    That's all one C program. However, kernel programming does not use the standard C libraries and is specially compiled, so there are some significant differences. For example, there is no main().

    This is because a module runs as part of the kernel process, and is not really an independent executable. When the module is loaded* -- ie, the program is started -- it registers a number of callback functions and other information with the kernel. A callback is a function which is called in response to some event, eg, in a user interactive program, you could have a callback for when the mouse is moved. Callbacks in modules are called by the kernel itself when, for example, the kernel detects a USB device plugged into the system. So an individual USB module must first register the ID of the device for which it was written, and some functions to deal with generic USB events (that is, basic events common to all USB devices, such as being plugged and probed by the OS). Those functions are a struct usb_driver, the ID is in a struct usb_device_id. These should be global to the scope of the module, eg, they are not assigned in any function.

    Notice there is a pointer in the usb_driver struct to a usb_device_id struct. What does go in an actual function is a call to usb_register(), which takes a pointer to the usb_driver struct which is declared globally (and has a pointer to the device_id struct). That function is the module's initialization function, and it is defined as such at compilation by including this line at the end of the module code:

    module_init (my_inititialization_function);

    I haven't done this stuff in a while. This is the simple part; USB module programming gets quite complex. Don't feel you have to go through with it at this point, it may be "counter productive" for you until you have more experience with the C language -- there is a lot of need for looping thru arrays of nested structs:

    Code:
    for (i=0;i<dev->actconfig->desc.bNumInterfaces;i++) {
         tmp=dev->actconfig->interface[i];
         printk("usbgetinfo: Configuration %d of %d\n",i+1,dev->actconfig->desc.bNumInterfaces);
         for (j=0;j<tmp->num_altsetting; j++) {
            hinfc=&tmp->altsetting[j];
            printk("\tInterface %d of %d\n",j+1,tmp->num_altsetting);
            for (k=0; k<hinfc->desc.bNumEndpoints; k++) {
                endpoint = &hinfc->endpoint[k]; 
                /* endpoint->desc is a struct usb_endpoint_descriptor */
                printk("\t\tEndpoint: %d",endpoint->desc.bEndpointAddress);
                if (endpoint->desc.bEndpointAddress & USB_DIR_IN) printk(" (in) ");
                if (endpoint->desc.bEndpointAddress & USB_DIR_OUT) printk(" (out) ");
                type = endpoint->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
                switch (type) {
                    case (USB_ENDPOINT_XFER_BULK): printk("BULK\n"); break;
                    case (USB_ENDPOINT_XFER_ISOC): printk("ISOCHRONOUS\n"); break;
                    case (USB_ENDPOINT_XFER_INT): printk("INTERRUPT\n"); break;
                    default: printk("??\n"); break;
                }   
            }   
        }
    }
    Notice the use of printk() instead of printf(), since you cannot use stdio.h in a kernel module. Having fun yet?

    Here is an online book you may want to look at:
    Linux Device Drivers, 2nd Edition: Online Book
    Unfortunately, the kernel changes all the time and there were some significant changes to some of the names of usb functions in the past year or so, meaning some material may contain the old function names. But the general idea remains the same.

    * the module can also be placed "in tree" with the other modules and added to a database with the depmod shell command; the kernel can then load the module on the basis of the info in the database (the device ID, etc).
    Last edited by MK27; 09-13-2009 at 08:12 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. USB Linux - live or no?
    By sean in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-23-2008, 10:56 AM
  2. FTP program
    By jakemott in forum Linux Programming
    Replies: 14
    Last Post: 10-06-2008, 01:58 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Port app from Windows to Linux
    By BobS0327 in forum Linux Programming
    Replies: 12
    Last Post: 02-12-2006, 02:35 AM
  5. Stencil Buffer Tutorial Port.. test me! (linux)
    By Perspective in forum Game Programming
    Replies: 3
    Last Post: 08-15-2004, 09:35 PM

Tags for this Thread