OS -->USB ?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: OS -->USB ?

Post by Sam111 »

Ok thanks you cleared up alot of my question.

Just a few last things I need to get straight.

So if you use pragma intrinisic then the compiler doesn't need the header file for that function. It is taken as a keyword and translated into direct asm.

For example on msnd site _outp requires <conio.h> if I use the pragma do I not have to include the <conio.h> because I don't want to include any header files except mine. I don't want to depend on their libraries.

Here is the link on what I am talking about.
They say required header file conio.h [-X
http://msdn.microsoft.com/en-us/library ... S.80).aspx

Ahhh , The alignment thing I get it. It's if you want to effect the whole project or just piece's. Although the #pragma pack could be used to effect the whole project just depends where you close it with a pop..etc
I see where it reduces execution time it is just replacing the function with the exact asm equivalent.
Don't know the answer to that, I compile my kernel code as C++ (but actually use only the function overloading feature of C++).
So if I had all my files named .c in my project would something my wrong if I compiled it with the default which is C++.

I guess I can change the extensions to all my files to cpp.
But I am wondering if I can take my old c libraries and create a project in visual studio's 2005 keep them .c files with out any trouble.

Or must you use extern "C" each time you have a c file.

I am just confused how c files are compiled in VS 2005 because their is no extension when creating a file you can only select create cpp file not c file.
However they allow for extension's *.c ...etc

Just wondering if anybody out their knows what's up with this never had an issues in dev c++.

/TD ?


Their is no outportword in VS 2005 the equivalent is this so I guess I would have this

Code: Select all

extern "C" {
void outp(uint port, uint8 value);
void outpw(...); // etc
}

#pragma intrinsic(outp, ...)

#define IoPortWriteByte(port, value) outp(port, value)
..etc etc
Except for the issue if #include <conio.h> is need I would be set if you didn't.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: OS -->USB ?

Post by ru2aqare »

Sam111 wrote:Ok thanks you cleared up alot of my question.

Just a few last things I need to get straight.

So if you use pragma intrinisic then the compiler doesn't need the header file for that function. It is taken as a keyword and translated into direct asm.

For example on msnd site _outp requires <conio.h> if I use the pragma do I not have to include the <conio.h> because I don't want to include any header files except mine. I don't want to depend on their libraries.

Here is the link on what I am talking about.
They say required header file conio.h [-X
http://msdn.microsoft.com/en-us/library ... S.80).aspx
I don't know about _outp, but my msvc documentation lists (at http://msdn.microsoft.com/en-us/library ... S.80).aspx) __outbyte and its companions. No headers required whatsoever.

Edit: look at the list of supported operating systems on the page you mentioned. The <conio.h> header is ancient DOS stuff.
Sam111 wrote: So if I had all my files named .c in my project would something my wrong if I compiled it with the default which is C++.

I guess I can change the extensions to all my files to cpp.
But I am wondering if I can take my old c libraries and create a project in visual studio's 2005 keep them .c files with out any trouble.

Or must you use extern "C" each time you have a c file.
No, you can compile most of the C code as C++. It only matters when it comes to linking, as C++ function names are mangled so that they contain information about the types of the arguments, etc.

extern "C" is required whenever you import declarations of functions not written in C++ (that is, functions written in C or assembly). Since the intrinsic fucntions are C functions, you need the extern "C" bit.
Sam111 wrote: I am just confused how c files are compiled in VS 2005 because their is no extension when creating a file you can only select create cpp file not c file.
Visual Studio creates a cpp file when you select Create C++ file... I don't see what your problem is exactly.

Sam111 wrote: Their is no outportword in VS 2005 the equivalent is this so I guess I would have this ...
See eariler MSDN url. The function is there. It's named __outbyte, __outword and so on, not outportbyte, outportword, and so on. My bad.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: OS -->USB ?

Post by Sam111 »

You must be using AMD 64-bit architecture I am on x86 intel 32 bit
__outbyte
requires Header file <intrin.h> and is a c++ intrinisic function.
This function is vaild for both architecture so I can use it if I don't have to include the header file above.
See Here
http://msdn.microsoft.com/en-us/library/7yxkhewh.aspx

About the .c or .cpp file extension .
I just meant if I already have a ton of .c files in my project are they treated exactly like the .cpp files?

And Ok what is the /TD option for if you just need to use extern "C" {} ?
For c code when would you ever be required to use this or want to use this /TD .

That is all I am talking about.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: OS -->USB ?

Post by ru2aqare »

Sam111 wrote:You must be using AMD 64-bit architecture I am on x86 intel 32 bit
__outbyte
Yes, that's correct, however I use the same source and compile if for both x86 and x64. The intrinsics are (mostly) the same.
Sam111 wrote: requires Header file <intrin.h> and is a c++ intrinisic function.
This function is vaild for both architecture so I can use it if I don't have to include the header file above.
See Here
http://msdn.microsoft.com/en-us/library/7yxkhewh.aspx
No, it doesn't require a header if you declare it manually. I at least don't include any header that comes with Visual Studio, and the source still compiles fine.
Sam111 wrote: About the .c or .cpp file extension .
I just meant if I already have a ton of .c files in my project are they treated exactly like the .cpp files?
If you rename them to have a .cpp extension, or you set the Project Properties -> C/C++ Compiler -> Advanced -> Compile as option to C++, then yes.
Sam111 wrote: And Ok what is the /TD option for if you just need to use extern "C" {} ?
For c code when would you ever be required to use this or want to use this /TD .

That is all I am talking about.
I have no idea what this /TD option is. As I said, extern "C" is only needed if you compile your code as C++ code. If you compiler as C, then it is not needed (and the compiler will probably issue an error).
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: OS -->USB ?

Post by Sam111 »

O sorry about /TD I meant /TC .

And I think we are talk about the same thing compile as C /TC or C++ /TP option in the compiler settings of the project. If you select /TC then your saying I don't need extern "C" {}
stuff right?

I tried everything posted and I am down to one error it is the intrinsic stuff.

Basically I have these

Code: Select all

typedef int size_t;

extern void *memcpy(void *dest, const void *src, size_t count);
extern void *memset(void *dest, char val, size_t count);
extern unsigned short *memsetw(unsigned short *dest, unsigned short val, size_t count);
extern size_t strlen(const char *str);
In my header file and define them in one of my .c files.

As this

Code: Select all

#pragma intrinsic(memcpy) 

void *memcpy(void *dest, const void *src, size_t count)
{
    const char *sp = (const char *)src;
    char *dp = (char *)dest;
    for(; count != 0; count--) *dp++ = *sp++;
    return dest;
}

...etc
I have made sure I have the /TC so it compiles as c.
Never used extern "C" {} just did extern for each function.

I get these error's

Code: Select all

error C2169: 'memcpy' : intrinsic function, cannot be defined
.\main.c(20) : error C2169: 'memset' : intrinsic function, cannot be defined
.\main.c(34) : error C2169: 'strlen' : intrinsic function, cannot be defined
This is why I am think I don't want pragma intrinsic but pragma function. I don't know?
Ah F it if I cann't use these functions I can just rename them everywhere.
I just really want to understand why it is doing it.

The last problem I realized is that how do you tell the compiler in VS2005 to output a .bin file
The compiler settings only have Configuration Type .exe , .dll , .lib , make , util

I guess I can just compile all of them into obj and try to link them with another linker.
But I really would like to do all of this in the VS2005 enviorment.
geppy
Member
Member
Posts: 27
Joined: Wed Jun 11, 2008 5:30 pm

Re: OS -->USB ?

Post by geppy »

you 2 need a live chat ... if only it was about usb & hard drives
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: OS -->USB ?

Post by Sam111 »

I looked up the error
http://msdn.microsoft.com/en-us/library ... S.71).aspx

It say's
A function definition appears for a function already declared intrinsic.

So then I don't really need those functions bodies for memcpy...etc etc
I can just do the one line
#pragma intrinsic(memcpy)
And it should use the built in type.

Correct me if I am wrong.

Either way I don't really get this because the compiler settings have generate intrinsics to NO
be default. Which indicates to me you must define it yourself.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: OS -->USB ?

Post by ru2aqare »

Sam111 wrote:I looked up the error
http://msdn.microsoft.com/en-us/library ... S.71).aspx

It say's
A function definition appears for a function already declared intrinsic.

So then I don't really need those functions bodies for memcpy...etc etc
I can just do the one line
#pragma intrinsic(memcpy)
And it should use the built in type.
If you declare a function as intrinsic, then you can't write a function body. Why would you want a function body? You just told the compiler than you want the code it generates...
rdos
Member
Member
Posts: 3342
Joined: Wed Oct 01, 2008 1:55 pm

Re: OS -->USB ?

Post by rdos »

Providing USB-support is not as problematic as it sounds. There seems to be only three different hardware implementations (UHCI, OHCI and EHCI) that are interfaced via PCI. It is not too hard to write device-drivers for these if you have PCI support. I've implemented two of them (in assembler) in my OS (UHCI and OHCI). Next, you need to code the USB-stack. The level of compexity here is about the same as writing a TCP/IP stack. It is pretty complex, but it is not impossible, far from it. I've coded this as well for my OS (in assembler). Then you need to code the different functions. The situation is not too bad here either. There are a couple of very common device-classes that will take you a long way (bulk / disks mostly). I've not coded a mass-storage class yet, as I've not needed it, but I've coded two USB-serial device classes that covers most USB-serial converters.

Then there is the issue of USB-hubs, that are also pretty complex, but once again, I've not coded that either as the need has not appeared.

I find coding audio-drivers more problematic. There seems to be a huge number of different implentations around. So far I've coded for CS5536 and VT82Cxxx, but there are many others.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: OS -->USB ?

Post by Sam111 »

Ok from the wiki on pci
I have that (0xCF8) is named CONFIG_ADDRESS, and the second (0xCFC) is called CONFIG_DATA

the structure in the pci spec's give the below structure

Code: Select all

typedef struct
{
   unsigned short VendorID;
   unsigned short DeviceID;
   short CommandReg;
   short StatusReg;
   short RevisionID;
   char SubClass;
   char ClassCode;
   char CachelineSize;
   char Latency;
   char HeaderType;
   char BIST;
   int BAR0;
   int BAR1;
   int BAR2;
   int BAR3;
   int BAR4;
   int BAR5;
   int CardbusCISPtr;
   short SubVendorID;
   short SubDeviceID;
   int ExRomAddress;
   int Reserved1;
   int Reserved2;
   char IRQ;
   char PIN;
   char MinGrant;
   char MaxLatency;
}PCI_Device;

I am just curious how somebody found (0xCF8) is named CONFIG_ADDRESS, and the second (0xCFC) is called CONFIG_DATA. I don't see it in the spec's ? Maybe thru the bios?

See my problem is the spec's give you the structure and how it works but where do they give you the start location of these structures?

I am presuming you get the start address for the usb stuff thru the pci structure. But what gives you the start addresses for the pci? Or are they alway's loacted at the same address no matter what chipset ? (0xCF8)
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: OS -->USB ?

Post by ru2aqare »

Sam111 wrote:the structure in the pci spec's give the below structure ...
This structure is located in the PCI configuration space, which is a third address space - you have physical memory space, IO address space (accessible only via the in/out instructions), and then the PCI configuration space. To sum it up, to access this space, you need to write a doubleword to 0xCF8, and you can read or write the data on port 0xCFC.
rdos
Member
Member
Posts: 3342
Joined: Wed Oct 01, 2008 1:55 pm

Re: OS -->USB ?

Post by rdos »

The first step is to read the vendor & device specifications. You match these with the (known) vendor & device of the UHCI, OHCI or EHCI hardware. When you have found these, you can get IO bases and IRQs of the hardware from the PCI configuration space of the particular device.

Look here for how you can implement the basics of PCI:
http://rdos.net/vc/viewvc.cgi/trunk/ker ... iew=markup

At startup, the init_pci_device function is called to collect information about the available PCI-devices. When a new device is checked, FindPciDevice is used. If this function is successful, it will return bus, function and device. These parameters are then used to read (or write) a particular device's configuration space registers (ReadPciByte, ReadPciWord, ReadPciDword, WritePciByte, WritePciWord and WritePciDword).
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: OS -->USB ?

Post by Sam111 »

you need to write a doubleword to 0xCF8, and you can read or write the data on port 0xCFC
My problem is that the stucture begining at 0xCF8 is 32 bit's long the structure

Code: Select all

typedef struct
{
   unsigned short VendorID;
   unsigned short DeviceID;
   short CommandReg;
   short StatusReg;
   short RevisionID;
   char SubClass;
   char ClassCode;
   char CachelineSize;
   char Latency;
   char HeaderType;
   char BIST;
   int BAR0;
   int BAR1;
   int BAR2;
   int BAR3;
   int BAR4;
   int BAR5;
   int CardbusCISPtr;
   short SubVendorID;
   short SubDeviceID;
   int ExRomAddress;
   int Reserved1;
   int Reserved2;
   char IRQ;
   char PIN;
   char MinGrant;
   char MaxLatency;
}PCI_Device;
Is 00h to 3ch = 60 * 4 bytes = 120 bit's

0xCF8 is 32 bit's looking like this
32 enable bit --(31-24 reserved) --- (23 - 16 bus number) ---(15-11 device number) --(10-8 function number) -- (7-2register number) --0 --0-

If I write a 32 bit number to 0xCF8 where is the 120 bit structure located relative to 0xCF8?

And to enumerate all the devices on the pci do you have to have a loop runing thru the 256 possible bus numbers etc. I just don't know how to go from the 0xCF8 address to the structure containing vendor id. ..etc . Is this structure beganing at 0xCFC ?

AHHHHHHH #-o
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: OS -->USB ?

Post by ru2aqare »

Sam111 wrote:
you need to write a doubleword to 0xCF8, and you can read or write the data on port 0xCFC
My problem is that the stucture begining at 0xCF8 is 32 bit's long the structure
There is no such this as a structure that begins at port 0xCF8. Think ports 0xCF8 and 0xCFC as a window that allows you to access a separate address space (the PCI configuration space). You write to 0xCF8 the offset into the PCI config space you want to access, and that location gets mapped to port 0xCFC for the duration of one read or write operation (to access it again, you have to write another address to port 0xCF8).
Sam111 wrote: If I write a 32 bit number to 0xCF8 where is the 120 bit structure located relative to 0xCF8?
First, it's not 120 bits long, it's 256 bytes long. Second, it's not located anywhere relative to 0xCF8. As I said, port 0xCF8 acts like a window to the PCI configuration space. Read more in the wiki.
Sam111 wrote: And to enumerate all the devices on the pci do you have to have a loop runing thru the 256 possible bus numbers etc.
Yes. However, I don't know how to enumerate any bridged PCI buses.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: OS -->USB ?

Post by Sam111 »

On the wiki is offset and slot = to register number and device number.

If that is the case then I think I get how you can enumerate all devices.

Code: Select all

for( int i = 0 ; i < 256 < i++ )
for( int j = 0 ; j < 32 ; j++ )
if( pciConfigReadWord( i , j , 0 , 0) != 0xFFFF )
{
//then do something a device was found
//loop through the  0 - 3ch pci structure by incrementing the register number from 0 to 64.
// So you will need another for loop here to get the vendor , device , command ,...etc fields.

//don't know what the 8 function numbers are for though ?
//does each function number have it's own 0 - 3C structure ?
//if so then you would need a forth loop but the good thing is most of the time the if statment
//would return false .

}

And I think I get what is going on.
:)
Post Reply