1

I found a couple of articles, where it is shown how to make your own hello-world driver.

I supposed that function names should be hardcoded (for example, when you read from device, you use read function, etc).

But everywhere are used functions names like hello_read (first article) or device_file_read (second article), when describing what should happen when i cat device.

How does kernel understand, what function he should use?

avasin
  • 9,186
  • 18
  • 80
  • 127

1 Answers1

2

The answer to your question is struct file_operations. You will find it defined in both examples. This structure describes which function callbacks should be called (by providing pointers to those functions) when some operation on a file is performed (like a read, write or open).

The pointer to this structure is passed when your driver registers to some subsystem. In case of a miscdriver (first example), it's set in struct miscdevice structure which is then passed to misc_register function. In case of a character device (second example), it's directly passed as an argument to register_chrdev.

The file_operations structure is described in 2nd example you provided - read it carefully.

Krzysztof Adamski
  • 2,039
  • 11
  • 14
  • `struct file_operations` is only used by character device drivers. For block and platform devices, see http://stackoverflow.com/questions/12917198/linux-device-driver-program-where-the-program-starts/12923107#12923107 – sawdust May 28 '15 at 08:17
  • @sawdust: That's true but the idea is the same. – Krzysztof Adamski May 28 '15 at 08:33