2

Hi I am following the book Linux device driver development to write a driver in Linux. In an example code as below:

struct my_gpios {
    int reset_gpio;
    int led_gpio;
};
static struct my_gpiosneeded_gpios = {
    .reset_gpio = 47;
    .led_gpio = 41;
};

static struct resource needed_resources[] = {
    [0] = { /* The first memory region */
    .start = JZ4740_UDC_BASE_ADDR,
    .end = JZ4740_UDC_BASE_ADDR + 0x10000 - 1,
    .flags = IORESOURCE_MEM,
    .name = "mem1",
},
    [1] = {
    .start = JZ4740_UDC_BASE_ADDR2,Platform Device Drivers
    [ 126 ]
    .end = JZ4740_UDC_BASE_ADDR2 + 0x10000 -1,
    .flags = IORESOURCE_MEM,
    .name = "mem2",
},
};
static struct platform_devicemy_device = {
    .name = "my-platform-device",
    .id = 0,
    .dev = {
    .platform_data = &needed_gpios,
},
    .resource = needed_resources,
    .num_resources = ARRY_SIZE(needed_resources),
};
platform_device_register(&my_device);

I do not understand the syntax static struct_gpiosneeded_gpios = {} meaning and why have a dot in .reset_gpio. And what is the meaning of the syntax static struct [] = {[0]={}, [1]={}}?

Could you please give me a reference link or keyword or example about static struct {.a = VALUE, .b = VALUE,};?

CRM
  • 4,569
  • 4
  • 27
  • 33
  • https://en.cppreference.com/w/c/language/struct_initialization and https://en.cppreference.com/w/c/language/array_initialization – Shawn May 08 '20 at 05:08
  • It's just specifying initializer values for those named fields. – Tom Karzes May 08 '20 at 05:13
  • 4
    You really shouldn't be writing device drivers if you don't already have experience in C. For your own good, do some exercises in user space development in C first, before taking on kernel programming. – Cheatah May 08 '20 at 06:35
  • Thank you as in your explanation link, when i read the driver code of linux i see many strange syntax is "u32", "__raw",... I do not understand why they named the type is "u32" or what is the purpose of "__raw". Is there any standard or rule for naming the variable, macro, type,... in kernel ? And what C standard i have to compliance when writing code in linux driver ? – Tiến Thành Nguyễn May 08 '20 at 07:17
  • Is there a typo in line 5? I think you are missing a space, and it should say `static struct my_gpios needed_gpios = {`. Then it would define an object named `needed_gpios`, of type `struct my_gpios`, of storage duration `static`, and initialize it, which would all make sense. As written it's a syntax error. The same for `static struct platform_devicemy_device`. – Nate Eldredge May 08 '20 at 13:14
  • Something else looks wrong with `Platform Device Drivers` and `[ 126 ]`, like it should be a comment. Is this really the exact code you're using, or has there perhaps been a copy/paste mistake? – Nate Eldredge May 08 '20 at 13:16
  • Does this answer your question? [What does dot (.) mean in a struct initializer?](https://stackoverflow.com/questions/8047261/what-does-dot-mean-in-a-struct-initializer) – Tsyvarev May 08 '20 at 19:25

1 Answers1

1
static struct something x = {
    .field_one = 123,
    .field_two = 456
};

This is a struct initialization syntax, standard from C99 (see here). This example creates a variable of type struct something named x, with fields field_one and field_two initialized to the specified values, and any other field initialized to 0. The static keyword is a storage duration specifier (see here).


static struct something x[] = {[0]={ ... }, [1]={ ... }};

This is a mix of both struct initialization and array initialization syntax, again standard from C99 (see here). This example creates an array x of variables of type struct something, the one at index 0 is initialized with the contents of the {...} initializer, and the same goes for the one at index 1. Since the greatest specified index is 1, the array size is 2.


I do not understand why they named the type is u32 or what is the purpose of __raw.

The u32 type is just a short alias for uint32_t.

I am not sure exactly where you saw __raw, since I don't seem to find anything like it in the kernel source. In any case, the Linux kernel as a series of compile-time annotations used for variables that have different purposes (__user, __rcu, etc). Those are not part of the C standard and frequently not even GCC extensions. They are mostly hints to be used by Sparse, the Linux kernel semantic checker.

Is there any standard or rule for naming the variable, macro, type,... in kernel?

Refer to the Linux kernel coding style documentation page for more information. I would suggest you to read it all before trying to do any kind of kernel programming. The more documentation pages you read, the better.

And what C standard i have to compliance when writing code in linux driver?

Use anything that is C99 or older and you will be fine. The Linux kernel code does not adhere to a single C standard, and various parts of the code aren't even standard compliant, but use GCC extensions. See here for more information.

You don't usually choose the standard when compiling, the kernel Makefile does this for you, and it should default to C90.


In any case, those are a lot of questions. If you have a specific question I would suggest you to ask it separately so that people are able to give you a focused and more extensive answer, since it's off topic to ask too broad or too many questions.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • Sorry for the mistake of "__raw", it is "__raw_readl", i understand it is a function used to read raw data, but i do not understand why they name it is with two: __ before raw_readl. Is there any implied meaning here with the __ symbol? – Tiến Thành Nguyễn May 12 '20 at 06:41
  • @TiếnThànhNguyễn no particular meaning. You will fine *a lot* of function names with leading underscores. It usually means that the function is internal, and they probably add those underscores to avoid conflicts with other functions which could have the same name. – Marco Bonelli May 12 '20 at 11:57