C code, why is the address 0xFF00 discarded into the structure?

I am trying to understand Linux kernel code code written in C for a Wi-Fi USB adapter. Line 1456 in the file /drivers/net/wireless/rtl818x/rtl8187/dev.c (just in case someone wants to refer to the kernel code for context) reads:

  priv->map = (struct rtl818x_csr *)0xFF00; 

I am wondering what exactly the right operand does here - (struct rtl818x_csr *)0xFF00; . I interpreted this to say that "the address of the memory location 0xFF00 is of type rtl818x_csr , and then assign it priv->map ". If my interpretation is correct, what is so special about the memory address 0xFF00 that the driver can reliably say that it will always be at this address after that? Another thing that interests me is that 0xFF00 is only 16 bits. I would expect 32/64-bit if he chose a memory address.

Can someone clarify what is going on in this line of code? I assume there is a flaw in my understanding of the C syntax.

+7
source share
3 answers

Passing an absolute address to a pointer to a structure is the usual way in drivers to access (register) device memory mappings as a normal C. structure.

Using 0xff00 works because C does not make an expansion sign.

+2
source

0xFF00 is the address in the address space of the IO system. If you look in the code, the address will never be dereferenced, but available through the I / O functions.

For example, in a call

 rtl818x_iowrite8(priv, &priv->map->EEPROM_CMD, RTL818X_EEPROM_CMD_CONFIG); 

which then calls the low-level Linux kernel I / O functions.

The address is converted to a pointer to a structure to provide access to offsets from the address, for example here:

 0xFF00 + offsetof(struct rtl818x_csr, EEPROM_CMD) 

Please note that when rtl818x_iowrite8 called above, no play occurs when passing the argument &priv->map->EEPROM_CMD due to the & operator, only the address offset + is calculated. Separation occurs even with internal low-level functions called inside rtl818x_iowrite8 .

+2
source

You must consider this from the point of view of the device.

Starting at 0xFF00, the address space displayed for the rtl8187 device is a memory range that stores structured information in the same way as the rtl818x_csr structure defined here .

So, after the logical display of this region, you can start reading and writing on it to control the device. For example, here (I had to cut two more hyperlinks, because I do not have the reputation necessary to publish more than 3, but you understand). These are just a few examples. If you read the entire file, you will see that reads and writes are sprinkling everywhere.

To understand why this structure looks like this and why 0xFF00 is used instead of 0xBEEF or 0xDEAD, you will have to refer to the data table for this device.

So, if you want to start looking at the kernel code, and especially at the device drivers, you will have to have more than just code. You will also need a specification or specifications. This can be quite difficult to find (see Gazillions of email streams and articles requesting open documentation from suppliers).

In any case, I hope I answered your question. Happy hack!

+2
source

All Articles