C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 09-01-2009, 03:37 AM   #1
Registered User
 
yann's Avatar
 
Join Date: Sep 2009
Location: Zagreb, Croatia
Posts: 179
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.
yann is offline   Reply With Quote
Old 09-01-2009, 04:19 PM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 09-01-2009 at 04:34 PM.
MK27 is offline   Reply With Quote
Old 09-02-2009, 08:30 AM   #3
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 09-02-2009, 08:49 AM   #4
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,924
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.
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 09-12-2009, 09:24 AM   #5
Registered User
 
yann's Avatar
 
Join Date: Sep 2009
Location: Zagreb, Croatia
Posts: 179
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)?
yann is offline   Reply With Quote
Old 09-12-2009, 11:42 AM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 09-13-2009, 04:38 AM   #7
Registered User
 
yann's Avatar
 
Join Date: Sep 2009
Location: Zagreb, Croatia
Posts: 179
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.)):
yann is offline   Reply With Quote
Old 09-13-2009, 08:08 AM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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).
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 09-13-2009 at 08:12 AM.
MK27 is offline   Reply With Quote
Reply

Tags
linux, port, ports, usb

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
USB Linux - live or no? sean General Discussions 7 12-23-2008 10:56 AM
FTP program jakemott Linux Programming 14 10-06-2008 01:58 PM
brace-enclosed error jdc18 C++ Programming 53 05-03-2007 05:49 PM
Port app from Windows to Linux BobS0327 Linux Programming 12 02-12-2006 02:35 AM
Stencil Buffer Tutorial Port.. test me! (linux) Perspective Game Programming 3 08-15-2004 09:35 PM


All times are GMT -6. The time now is 10:47 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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