1

I'm learning to create kernel modules on Raspbian Jessie based on The Linux Kernel Module Programming Guide

Currently I'm on hello-5.c part. I tried to add static u8 myByte = 'X'; but u8 is not recognized. Then I changed u8 with uint8_t which is the same thing AFAIK and uint8_t is recognized as a data-type.

The older version of the tutorial here stated static u8 myByte = 'X';, so I want to reuse it with the newer tutorial.

The older tutorial included these:

#define MODULE
#define LINUX
#define __KERNEL__

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

While the newer tutorial included these;

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>

I've tried using the old one, but the define part generates errors, so I used the newer tutorial.

So, why uint8_t can be recognized while u8 cannot in the newer version?

  • I'm afraid that this question is outside the scope of this particular site, but may do better on Stackoverflow or Unix.stackexchange. – Frank Thomas Feb 08 '17 at 14:05
  • Oh, that actually answered my question. Thank you. :) – Widi Widiyanto Feb 08 '17 at 14:13
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – too honest for this site Feb 09 '17 at 14:34
  • @WidiWidiyanto See [this](http://superuser.com/questions/437786/kernel-data-types-u8-u16-u32-u64-not-resolved), [this](http://stackoverflow.com/questions/30896489/why-is-u8-u16-u32-u64-used-instead-of-unsigned-int-in-kernel-programming) and read [chapter 11](http://static.lwn.net/images/pdf/LDD3/ch11.pdf) of LDD3. You'll find your answers there. – Sam Protsenko Feb 09 '17 at 23:59
  • @Olaf I think I've explained it quite clearly. I want to try `static u8 myByte = 'X';` but it only recognized when I use `static uint8_t myByte = 'X'` – Widi Widiyanto Feb 13 '17 at 10:00
  • @WidiWidiyanto: That was not the only part of the citatition. In case you were not able to check out the site-FAQ, here is the link [ask] – too honest for this site Feb 13 '17 at 13:14
  • @Olaf Hmm.. After I read out the FAQs I'm still not sure what's wrong with my question. It's silly, I know. Would you mind pointing it out if you have the time? :) – Widi Widiyanto Feb 13 '17 at 13:28
  • Oh, I didn't read your comment correctly. I should've written the headers part. Thanks. :) – Widi Widiyanto Feb 13 '17 at 14:51

1 Answers1

4

Because u8 is not a standard type, while uint8_t is (typedef'd in C99 and later <stdint.h>). If you want to use u8, include a header with an appropriate typedef, or typedef it in your code.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • 1
    It's not fully correct answer. While it's indeed a non-standard type, it's widely used in Linux kernel. I guess the problem not in the type per se, but rather with its use as a module parameter. In any case there is no code provided to see what author actually did. – 0andriy Feb 09 '17 at 22:15
  • I use the code in [chapter-5.c](http://www.tldp.org/LDP/lkmpg/2.6/html/x323.html#AEN345). I want to add static u8 myByte='X' like in the older version [here](http://www.tldp.org/LDP/lkmpg/2.4/html/x354.htm) – Widi Widiyanto Feb 13 '17 at 10:07
  • 2
    @0andriy: The answer is correct. `u8` is not a standard type. It's use in the kernel is a legacy from before C99 where there were no fixed-width types in the standard. The Linux kernel does not set what is standard or not. However, for the kernel one should stick with these remains, but for other, new projects one should use the standard types. – too honest for this site Feb 13 '17 at 13:12
  • So I better off using the standard form (uint8_t) rather than u8 then. Thank you very much. :) – Widi Widiyanto Feb 13 '17 at 13:33
  • @WidiWidiyanto, no. In kernel better to use `u8`, `u16` and so on. If it's exported to user space the `__u8`, `__u16` and so on **must** be used. – 0andriy Feb 13 '17 at 23:02
  • @Olaf, I stand corrected. Indeed for new code `uint8_t` and a such **can** be used (it's not mandatory as well!), but for existing the style should be preserved. And for user space it must be specific types. (I'm talking about style in Linux kernel exclusively) – 0andriy Feb 13 '17 at 23:12
  • @0andriy: Actually the standard types should be used in user-code definitively. Whether they can be used in the Linux kernel depends on how the legacy types are defined. If they are just `typedef`s now, using the standard types is fine. Otherwise there can be subtle differences, e.g. for `int32_t`, depending on the platform. For other usages like bare-metal embedded, one should not even consider using some homebrew-stuff. – too honest for this site Feb 13 '17 at 23:19