0

As I was studying the code of the mxs-auart.c driver I noticed the following declaration:

enum mxs_auart_type {
    IMX23_AUART,
    IMX28_AUART,
    ASM9260_AUART,
};

and then later on:

static const struct platform_device_id mxs_auart_devtype[] = {
    { .name = "mxs-auart-imx23", .driver_data = IMX23_AUART },
    { .name = "mxs-auart-imx28", .driver_data = IMX28_AUART },
    { .name = "as-auart-asm9260", .driver_data = ASM9260_AUART },
    { /* sentinel */ }
};
MODULE_DEVICE_TABLE(platform, mxs_auart_devtype);

static const struct of_device_id mxs_auart_dt_ids[] = {
    {
        .compatible = "fsl,imx28-auart",
        .data = &mxs_auart_devtype[IMX28_AUART]
    }, {
        .compatible = "fsl,imx23-auart",
        .data = &mxs_auart_devtype[IMX23_AUART]
    }, {
        .compatible = "alphascale,asm9260-auart",
        .data = &mxs_auart_devtype[ASM9260_AUART]
    }, { /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, mxs_auart_dt_ids);

The thing I don't understand about this is how can IMX28_AUART, for example, be used like .data = &mxs_auart_devtype[IMX28_AUART]. Don't we have to instanciate a variable beforehand to use the enum values by doing as an example enum mxs_auart_type value = IMX28_AUART? I am aware that in enumerations in C, values are equal to integer starting by default at 0 but I can't help but feel confused about this.

Can anyone help me understand this a bit better? Thanks

2 Answers2

0

An enum allows you to give names to constant values. For all intents and purposes, you can treat the enum name as an integer literal.

In this case:

       .data = &mxs_auart_devtype[IMX28_AUART]

The name IMX28_AUART is treated like 1, so the code is the same as:

       .data = &mxs_auart_devtype[1]
jxh
  • 69,070
  • 8
  • 110
  • 193
0

Don't we have to instanciate a variable beforehand to use the enum values by doing as an example enum mxs_auart_type value = IMX28_AUART?

Not at all.

After doing this:

enum MyEnum {A, B, C};

You will have three globally available names: A, B, and C, with fixed integer values of 0, 1, and 2 accordingly.

Take a look at this useful post to know more: "static const" vs "#define" vs "enum".

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128