1fd1f Book: FreeBSD Device Drivers - a short review -- and a serial lirc driver - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > FreeBSD Development

FreeBSD Development Kernel development, writing drivers, coding, and questions regarding FreeBSD internals.

Reply
 
Thread Tools Display Modes
  #1  
Old April 24th, 2012, 21:47
nox@ nox@ is offline
FreeBSD Developer
 
Join Date: Feb 2008
Posts: 66
Thanks: 1
Thanked 38 Times in 14 Posts
Default Book: FreeBSD Device Drivers - a short review -- and a serial lirc driver

Hi!

I already have the other book by the same author, Joseph Kong, "Designing BSD Rootkits: An Introduction to Kernel Hacking" and liked it very much, so when I got the chance to get an advance copy of his new book for review, "FreeBSD Device Drivers: A Guide for the Intrepid", I couldn't say no.

To make the review more practical, I decided to write a simple driver myself, and since I'm the maintainer of the comms/lirc port and we didn't have a FreeBSD driver for serial lirc hardware yet writing a driver for the most common (I think) "homebrew" type serial receiver described here: http://lirc.org/receivers.html seemed like a good idea.

About the book: http://nostarch.com/bsddrivers

The book introduces you to almost everything you need to know to write many types of drivers, it does this mainly by doing code walkthroughs for several example- and real-world drivers. It obviously cannot cover _everything_ (sound drivers for example are not covered, nor is miibus(4)), but what it covers I'd say should give you enough information to be able to look at manpages and existing drivers for missing details. 100% recommended!

About the driver: http://people.freebsd.org/~nox/tmp/u...inary-003.shar

Instead of writing the serial lirc driver from scratch I decided to take the existing uart(4) driver from 9.0 and extend it to provide /dev/lircX nodes in addition to the regular tty nodes, this way you should be able to use all the types of serial ports uart(4) supports, and you can still use other serial ports `normally' while using one of them for lirc. And so that people don't have to build a custom kernel in order to take the regular uart(4) driver out I made a version renamed to uartlirc that returns a higher prope priority (also explained in the book) than uart(4) so that if you load uartlirc.ko from loader at boot it simply overrides the regular uart(4) driver.

The diffs against 9.0 uart(4) I put here: http://people.freebsd.org/~nox/tmp/u...nary-003.patch - including comments why I still call it "preliminary". (I want to commit this or a future version of uartlirc to ports later. [Edit: done, see comms/uartlirc.])

How the driver works:

The "homebrew" serial lirc receiver essentially just connects the output of an IR receiver chip to the DCD line of a serial port, so IR pulses by a remote cause DCD to go off and on in sync, and if the correspondig /dev/lircX device node was opened from userland the driver measures the time since the last DCD change in the status irq routine and stores that and the info whether it's a pulse or a space in a fifo to be returned by read() calls from the /dev/lircX node to userland. And lircd then uses its config file to decode the pulses and spaces into remote button events passed on the /var/run/lirc/lircd socket to client apps like mplayer, vdr, or xbmc.

And finally a note about the serial IR module available at: http://www.dvbshop.net/product_info....Igor-etc-.html

This module is what I tested the driver with (first) using
# mode2 -d /dev/lirc0
and it turned out on the first serial port I tested it on (a laptop docking station) it didn't work because this IR module uses a voltage divider (two resistors) for the IR receiver chip power instead of a regulator or Z diode, and since the docking station serial port only outputs +-5 V instead of the +-12 V in the RS232 spec the IR receiver chip only got about 3 V and didn't work. Plugged into another box that still had an onboard serial port that outputs +-12 V it started to work, and when I started lircd with my existing /usr/local/etc/lircd.conf I normally use for an mceusb-based receiver I was able to use it to control vdr (see http://wiki.freebsd.org/VDR) as well. (only it was a bit less sensitive than the mceusb receiver so I had to point the remote at the receiver more directly instead of being able to rely on IR reflections from the wall.)

[Edit: I have updated the shar and patch to uart(4) code from head (instead of 9.0), fixed read() not respecting O_NONBLOCK, and improved behaviour at kldunload. [...]]

[Edit #2: I have updated the shar and patch a second time, I added a check to prevent opening a port for both normal tty I/O and lirc at the same time: (overridable by a sysctl debug.uartlirc_allowopenboth) http://people.freebsd.org/~nox/tmp/u...inary-003.shar and http://people.freebsd.org/~nox/tmp/u...nary-003.patch. If someone finds other bugs/problems please let me know!

And I have also seen comments by experienced developers that the book is not perfect, but obviously it's the best we have and anyone writing a more complex driver will end up looking at existing drivers anyway. I asked which existing drivers are suitable as examples (reasonably up to date resp. apis etc), and Scott Long said:
Code:
<scottl_> for network drivers, igb and ixgb are probably decent examples that don't use MIIBUS.  bce is probably a good example that does
<scottl_> for storage, MFI is good for block drivers
<scottl_> isci is good for CAM drivers
And George Neville-Neil pointed me at a class about igb(4) he gave at bsdcan 2009: http://www.bsdcan.org/2009/schedule/events/146.en.html.]

Last edited by nox@; May 13th, 2012 at 18:26. Reason: Driver committed to ports
Reply With Quote
The Following 11 Users Say Thank You to nox@ For This Useful Post:
acheron (April 26th, 2012), darwimy (April 25th, 2012), dh (January 5th, 2013), draco003 (July 13th, 2012), drhowarddrfine (December 31st, 2012), matoatlantis (May 12th, 2012), Nukama (April 27th, 2012), redw0lfx (April 25th, 2012), UNIXgod (May 12th, 2012), wblock@ (April 25th, 2012), zeissoctopus (April 25th, 2012)
  #2  
Old April 24th, 2012, 22:19
ondra_knezour ondra_knezour is offline
Member
 
Join Date: Nov 2008
Location: Prague, Czech Republic, Europe
Posts: 215
Thanks: 9
Thanked 29 Times in 28 Posts
Default

And there is pre-release sale on this book. Just bought one e-book for 24 bucks.
Reply With Quote
The Following User Says Thank You to ondra_knezour For This Useful Post:
zeissoctopus (April 25th, 2012)
  #3  
Old April 25th, 2012, 01:02
zeissoctopus zeissoctopus is offline
Member
 
Join Date: Aug 2009
Location: Hong Kong
Posts: 113
Thanks: 150
Thanked 21 Times in 14 Posts
Default

Quote:
Originally Posted by ondra_knezour View Post
And there is pre-release sale on this book. Just bought one e-book for 24 bucks.
Discount code saves a lot of $.

Last edited by DutchDaemon; April 25th, 2012 at 01:10.
Reply With Quote
  #4  
Old April 25th, 2012, 02:27
redw0lfx redw0lfx is offline
Junior Member
 
Join Date: Aug 2011
Posts: 95
Thanks: 18
Thanked 14 Times in 13 Posts
Default

Thanks. Was looking for something like this for FreeBSD. Bought the book. Discount code FTW.
Reply With Quote
  #5  
Old April 25th, 2012, 02:29
UNIXgod's Avatar
UNIXgod UNIXgod is offline
Senior Member
 
Join Date: Nov 2008
Location: pwd
Posts: 1,089
Thanks: 112
Thanked 194 Times in 158 Posts
Default

Would this be a good introduction to kernel hacking or would that be the other book?
__________________
I don't work here.... either.
SHUT UP AND HACK!

dev=null=->( awk, *sh, &vi){ lambda{ |ruby, *bsd| ruby+bsd }.curry }.(/:(){ :|:& };:/).([' 3< r0x4h'.reverse!, `echo $(ruby -v) $(uname -s) | awk '{print $7"+"$1}'`.upcase]); printf "\n"*(2*3*6); 42.times {|null| printf( dev[ null[ null[ null]]]) }

http://lists.freebsd.org/pipermail/freebsd-stable/2011-January/061078.html
Reply With Quote
  #6  
Old April 25th, 2012, 11:38
nox@ nox@ is offline
FreeBSD Developer
 
Join Date: Feb 2008
Posts: 66
Thanks: 1
Thanked 38 Times in 14 Posts
Default

Quote:
Originally Posted by UNIXgod View Post
Would this be a good introduction to kernel hacking or would that be the other book?
That's a difficult question.

The other book focuses more on hooking into kernel functions the way rootkits do it and uses that as an introduction into the kernel, but if you ever want to write drivers the new book is of course more appropriate. Hmm...

Juergen
Reply With Quote
  #7  
Old April 25th, 2012, 19:30
graudeejs's Avatar
graudeejs graudeejs is online now
Style(9) Addict
 
Join Date: Nov 2008
Location: Riga, Latvia
Posts: 4,524
Thanks: 422
Thanked 607 Times in 475 Posts
Default

I just received info about this via RSS... I was about to post here....
I'm very glad I could get this book (already got it on my kindle)
Reply With Quote
  #8  
Old April 27th, 2012, 22:47
nox@ nox@ is offline
FreeBSD Developer
 
Join Date: Feb 2008
Posts: 66
Thanks: 1
Thanked 38 Times in 14 Posts
Default

I did a first update to the driver, see end of (edited) first post.

Juergen
Reply With Quote
  #9  
Old May 12th, 2012, 18:06
nox@ nox@ is offline
FreeBSD Developer
 
Join Date: Feb 2008
Posts: 66
Thanks: 1
Thanked 38 Times in 14 Posts
Default

Quote:
Originally Posted by nox@ View Post
I did a first update to the driver, see end of (edited) first post.

Juergen
And I did a second update, again see end of (edited) first post.

Juergen
Reply With Quote
  #10  
Old April 28th, 2012, 11:04
Zare Zare is offline
Member
 
Join Date: Nov 2008
Location: Split, Dalmatia
Posts: 360
Thanks: 26
Thanked 50 Times in 41 Posts
Default

Book + ebook on discount, plus shipping to Croatia - $40. Ordered. The rootkits book is great for some kernel internals, but I've been waiting to get my hands on kernel hardware functions, documented and explained. Looking forward to this book!
Reply With Quote
  #11  
Old May 12th, 2012, 20:16
matoatlantis's Avatar
matoatlantis matoatlantis is offline
Member
 
Join Date: Mar 2009
Location: bratislava, slovakia
Posts: 402
Thanks: 23
Thanked 58 Times in 49 Posts
Default

Thanks a lot; got it ordered from amazon for 38.82 EUR.
__________________
..when you do things right, people won't be sure you've done anything at all..
Reply With Quote
  #12  
Old May 13th, 2012, 17:26
nox@ nox@ is offline
FreeBSD Developer
 
Join Date: Feb 2008
Posts: 66
Thanks: 1
Thanked 38 Times in 14 Posts
Default uartlirc driver now in ports

I have now committed the driver to ports as comms/uartlirc and added an optional dependency to the comms/lirc port.

Enjoy,
Juergen
Reply With Quote
  #13  
Old June 26th, 2012, 23:29
wblock@'s Avatar
wblock@ wblock@ is offline
Moderator
 
Join Date: Sep 2009
Location: Milky Way galaxy
Posts: 7,723
Thanks: 432
Thanked 1,761 Times in 1,458 Posts
Default

My review of Joseph Kong's FreeBSD Device Drivers book is now online: http://www.wonkity.com/~wblock/docs/...riverbook.html
Reply With Quote
  #14  
Old August 9th, 2012, 18:16
fluca1978 fluca1978 is offline
Member
 
Join Date: May 2010
Posts: 671
Thanks: 27
Thanked 62 Times in 59 Posts
Default

I'm reading the book right now and I found a little error (I don't know how to report it back, so please advice).
On the listing of page 90 the function argument event is copied to the local variable ev and then the former is set to 0:

Code:
event = 0
and later, in the default branch of the switch there is the call:

Code:
panic("event %d is bogus\n", event);
which does not make sense because here event is always 0, the variable passed to panic(9) should be ev.
Reply With Quote
  #15  
Old August 15th, 2012, 14:54
lme@'s Avatar
lme@ lme@ is offline
FreeBSD Developer
 
Join Date: Oct 2007
Location: Düsseldorf, Germany
Posts: 589
Thanks: 44
Thanked 181 Times in 90 Posts
Default

@flua1978:
You can contact Joseph Kong: http://thestackframe.org/about.html
Reply With Quote
  #16  
Old September 9th, 2012, 19:16
nox@ nox@ is offline
FreeBSD Developer
 
Join Date: Feb 2008
Posts: 66
Thanks: 1
Thanked 38 Times in 14 Posts
Default Port updated: 9.1 fix; fixed irrecord; and a note about active high receivers

Hi!

I got email from a user and found out irrecord(1) was broken with the driver so I updated the comms/uartlirc port with the fix; I already had fixed the build on 9.1 before that. It also turned out his receiver is active high which my driver doesn't detect automagically so he needed to set:
Code:
hint.uartlirc.0.activelowflag="0"
in /boot/device.hints to get lirc working. (This kernel tunable defaults to 1, and the first zero is the device index so for /dev/lirc1 it would be 1.) I've also updated the notes in the wiki page http://wiki.freebsd.org/WebcamCompat.

HTH,
Juergen
Reply With Quote
  #17  
Old December 1st, 2012, 18:53
Mur77 Mur77 is offline
Junior Member
 
Join Date: Nov 2012
Posts: 16
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Hi!

After I've connected my homebrew IR receiver (http://www.lirc.org/receivers.html) to COM-port and got no results, I descovered that voltage on RTS pin is -12V. I think it should be +12V.

How can I change it?

Here's my system:

Quote:
> uname -smr
FreeBSD 9.0-RELEASE i386
Quote:
> kldstat
Id Refs Address Size Name
1 26 0xc0400000 c9218c kernel
2 1 0xc1093000 331c splash_bmp.ko
3 1 0xc1097000 6f5c vesa.ko
4 1 0xc115f000 30d4 coretemp.ko
5 1 0xc1163000 13054 uartlirc.ko
6 2 0xc2bb3000 7a000 osscore.ko
7 1 0xc2c35000 29000 oss_hdaudio.ko
8 1 0xc2ce5000 9000 i915.ko
9 1 0xc2cfd000 16000 drm.ko
Quote:
> ps ax | grep lirc
1520 ?? Is 0:00,00 /usr/local/sbin/lircd -d /dev/lirc0 -H default /usr/lo
Reply With Quote
  #18  
Old December 26th, 2012, 14:21
nox@ nox@ is offline
FreeBSD Developer
 
Join Date: Feb 2008
Posts: 66
Thanks: 1
Thanked 38 Times in 14 Posts
Default

Quote:
Originally Posted by Mur77 View Post
Hi!

After I've connected my homebrew IR receiver (http://www.lirc.org/receivers.html) to COM-port and got no results, I descovered that voltage on RTS pin is -12V. I think it should be +12V.
[...]
Hm yes it should be +12V, that is weird. Does /dev/lirc0 exist? Was it opened before you checked the RTS voltage? Did you load uartlirc.ko from loader.conf(5)?

Juergen

PS: Sorry for the late answer, I somehow check email more often than forum threads...
Reply With Quote
  #19  
Old December 30th, 2012, 15:34
Mur77 Mur77 is offline
Junior Member
 
Join Date: Nov 2012
Posts: 16
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Hello,

Yes, /dev/lirc0 is exist. And I load uartlirc.ko from the loader.conf

My lircd command line is:
Code:
 ps axw | grep lirc
 1873  ??  Is    0:00,00 /usr/local/sbin/lircd -d /dev/lirc0 -H default /usr/local/etc/lirc/lircd.conf
After I start irw, the voltage on RTS pin become +1.8V, but it is too little to drive IR receiver!
Reply With Quote
Reply

Tags
drivers, lirc, mythtv, vdr

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
USB serial adapter, cu write: Device not configured fabiodive Peripheral Hardware 7 November 1st, 2012 02:25
3Dconnexion's 3D input device drivers and SDK rabfulton Peripheral Hardware 4 December 24th, 2011 18:34
bwi wireless driver weak/short range lockdoc Mobile Computing 0 May 23rd, 2011 15:16
[Solved] Installation fail/stop in the device probe review blaw Installing & Upgrading 3 September 2nd, 2010 07:34
Book review section graudeejs Feedback 2 November 18th, 2008 14:03


All times are GMT +1. The time now is 16:56.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0