Live data from Hacker News

Writing device drivers in Linux: A brief tutorial (2006)

freesoftwaremagazine.com

11–20 of 36 posts

Re: Writing device drivers in Linux: A brief tutorial (2006)

#11
Why wasn't I aware of this guide last week when I had to write a kernel driver for my OS class?!

One thing that's surprised me with Linux development is how nice the APIs are to work with. Things like the file_operations struct, and the linked list stuff are very well thought out and easy to use (as easy as they could be for C programming).

My only complaint is that a lot of the written documentation is out of date about a number of topics. As the kernel has evolved it's gained and lost a number of APIs and depending on when resources were published they may say a number of conflicting things about how to carry out a task (registering a character device and getting it in /dev is the big one that comes to mind).

Re: Writing device drivers in Linux: A brief tutorial (2006)

#12
post #7
post #3

If you're like me and read comments before you read the post, then I urge you to take a look at this how-to. Not only is it well written and informative but it makes fairly complicated concepts easy to understand. If you like the idea of writing drivers and want that kind of control over hardware but don't have a clue where to start, this is pretty much it.

Very exciting and empowering article. Makes me want to learn c, or at least believe that c is possible, straightforward easy and useful.

While not straightforward or easy (for an average chap), it's quite possible, and very useful. Although I wouldn't call it most people's programming language of choice, knowing, and being able to grapple with how C pushes memory management to user-code will definitely change the way you think about programming.

TLDR : Pick up any of the hundreds of books on it and start learning! If you're having trouble motivating yourself, try solving Competition Programming problems in it - it's usually a good way to learn a new language.

Re: Writing device drivers in Linux: A brief tutorial (2006)

#14
post #6

Does anyone have any suggestions for books/learning materials related to the pre requisites mentioned in the article, specifically Microprocessor programming? I've tried to find some in the past but having no prior EE experience I find even some of the basics challenging. I have experience with C, but don't really know where to start with the lower level stuff. I'm thinking I should start with a simple book like Elec…

I'm trying to understand what you mean by "Microprocessor programming." Do you mean something like Assembly language? Or do you mean Microprocessor design?

I'm wondering if I might be able to help out, but I'm not sure I understand the question. I'm an EE with a lot of digital design background and some software.

Re: Writing device drivers in Linux: A brief tutorial (2006)

#15
Wonderful - showing how little magic there really is in kernel programming. I recommend that everyone who does Linux programming go through this tutorial, even if just for fun - and for the sake of seeing how different the world on the other side of the syscall is. When writing a kernel-mode driver, a single bug usually means a reboot: either because you screwed up some refcount, so the module cannot be unloaded anymore - or because you clobbered something badly and the kernel is toast.

Having a driver-writing background, I silently laugh at the recent unit testing hype - I simply know from experience there are areas where you need much more care, gut and skill than def test_something(). And besides, you just can't unit-test a DMA transfer.

Re: Writing device drivers in Linux: A brief tutorial (2006)

#16
post #7

Earlier quoted context omitted.

Very exciting and empowering article. Makes me want to learn c, or at least believe that c is possible, straightforward easy and useful.

While not straightforward or easy (for an average chap), it's quite possible, and very useful. Although I wouldn't call it most people's programming language of choice, knowing, and being able to grapple with how C pushes memory management to user-code will definitely change the way you think about programming. TLDR : Pick up any of the hundreds of books on it and start learning! If you're having trouble motivating y…

Someone needs to write "c for ruby programmers"

Re: Writing device drivers in Linux: A brief tutorial (2006)

#17

Earlier quoted context omitted.

While not straightforward or easy (for an average chap), it's quite possible, and very useful. Although I wouldn't call it most people's programming language of choice, knowing, and being able to grapple with how C pushes memory management to user-code will definitely change the way you think about programming. TLDR : Pick up any of the hundreds of books on it and start learning! If you're having trouble motivating y…

Someone needs to write "c for ruby programmers"

Not exactly what you're looking for, but "Learn C The Hard Way"[0] isn't a bad tutorial...

[0] http://c.learncodethehardway.org/book/

Re: Writing device drivers in Linux: A brief tutorial (2006)

#18
I really recommend a book called Linux Device Drivers (Third Edition) [1]. It's free, and split according to the major kernel subsystems. I used it many times as a reference when I had to recall some obscure API.

Another a great tool while developing for the kernel is the LXR [2] which is a browser-based indexer of the kernel source, for each of the kernel versions. Again great for checking out how to interface with a subsystem or how different calls are used.

[1] http://lwn.net/Kernel/LDD3/ [2] http://lxr.linux.no/

Re: Writing device drivers in Linux: A brief tutorial (2006)

#19
post #6

Does anyone have any suggestions for books/learning materials related to the pre requisites mentioned in the article, specifically Microprocessor programming? I've tried to find some in the past but having no prior EE experience I find even some of the basics challenging. I have experience with C, but don't really know where to start with the lower level stuff. I'm thinking I should start with a simple book like Elec…

The "lower level stuff" you're asking about here isn't exactly EE stuff, it's more about microprocessor interfacing. The kernel driver is getting data from userland, but now it needs to be massaged and placed into appropriate registers in the processor to get it to do something, or push the data out to a helper chip where it can do something (like sending and receiving mouse coordinates over USB to the little chip inside the mouse, for example).

So one one hand you need to understand your host processor (and it's constellation of helper chips) inside and out. Some modern SoC systems like the ones in smartphones have everything built into the same chip, so you wind up combing through 5,700 page Technical Reference Manuals like this one for the Freescale i.MX6:

http://cache.freescale.com/files/32bit/doc/ref_manual/IMX6DQ...

Or, in the case of more generic micro-based systems like an Arduino or something, you're reading datasheets for other little chips and figuring out how to interface them to your host's kernel.

But yeah, knowing how to wire up a transistor or LED to a processor without cooking it (or your power supply) is a good thing. You can learn a lot from taking apart other people's projects and seeing how they do it. Common patterns start showing up.

Post reply on HN