# Gentoo driver example code and makefile

## briek

Anyone, I am looking for an example (bare-bones) device driver (in C) and a makefile for Gentoo.  I am porting a Linux Redhat PCI device driver over to Gentoo, but obviously there are differences between the two.  I notice some of the object files on my Gentoo system now have the .ko extension, whereas mine is just .o.  So, I am in search of an example and any info on building it, configuring it, and inserting it into the kernel.  

Thanks,

briek@amtelco.com

----------

## Ferdinando

.ko is the extension of kernel object files (modules), while .o is for normal object files and .so for shared object files (dynamic libraries).

Why don't you look at some external kernel driver like madwifi-driver? It compiles modules into the kernel, so you could see how it works. There are others but I know little of them; you could try also net-wireless/ipw2200.

Bye

----------

## dsd

there is no difference between modules used on Gentoo and Red Hat.

however, if your red hat driver is a .o file then that indicates that you are on an old 2.4 kernel there - Gentoo uses Linux 2.6, which is the result of several years of heavy development on top of 2.4. one of the changes was in the module system, the extension changed to .ko

LWN has a 2.4-to-2.6 porting guide: http://lwn.net/Articles/driver-porting/

as for a simple driver for 2.6, you can look at one of my projects: http://developer.berlios.de/project/showfiles.php?group_id=5701

it is for USB though...

----------

## briek

 *dsd wrote:*   

> there is no difference between modules used on Gentoo and Red Hat.
> 
> however, if your red hat driver is a .o file then that indicates that you are on an old 2.4 kernel there - Gentoo uses Linux 2.6, which is the result of several years of heavy development on top of 2.4. one of the changes was in the module system, the extension changed to .ko
> 
> LWN has a 2.4-to-2.6 porting guide: http://lwn.net/Articles/driver-porting/
> ...

 

Thank you for the example, I will look at it shortly.  Is there a method for the user to open a "handle" to the driver?  For example, in RH Linux, the user app would do a open("/dev/device_name", O_RDWR);  I will need to aloow users to pass I/O info to it......

----------

## briek

Also, how do I build a proper ".ko" kernel module for Gentoo Linux?  Any ideas the syntax of the "cc" compile command?

----------

## dsd

yes your driver can create a device (through sysfs classes) and have an associated set of fops (file operations): open, read, close, release, seek, ...

LDD3 is a great resource about this and also details compilation through kbuild:

http://lwn.net/Kernel/LDD3/

----------

## briek

 *dsd wrote:*   

> yes your driver can create a device (through sysfs classes) and have an associated set of fops (file operations): open, read, close, release, seek, ...
> 
> LDD3 is a great resource about this and also details compilation through kbuild:
> 
> http://lwn.net/Kernel/LDD3/

 

Thanks for the tip.  I got my module to build now, but how/where do I "insert" it to make it so the system starts the module on system start?  Is there a general config file?

----------

## dsd

use insmod for a one-time insert

for automatic loading, install the module into an appropriate location under /lib/modules and test loading it with "modprobe modulename". if that works, add modulename to /etc/modules.autoload.d/kernel-2.6 for load-on-bootup behaviour

----------

## briek

 *dsd wrote:*   

> use insmod for a one-time insert
> 
> for automatic loading, install the module into an appropriate location under /lib/modules and test loading it with "modprobe modulename". if that works, add modulename to /etc/modules.autoload.d/kernel-2.6 for load-on-bootup behaviour

 

Sorry for all the Q's, obviously I am new....

So, insmod works.  Do I copy the .ko file to a specific /lib/module directory too?  Also, since it is a PCI device driver, do I need to put my Vendor and Device ID in the the modules.pcimap file?  How come I do not see any other modules listed in the /etc/modules.autoload.d/kernel-2.6 file now?  How are other modules started/loaded on boot?  Like the ethernet?

----------

## Ferdinando

 *briek wrote:*   

> How are other modules started/loaded on boot?  Like the ethernet?

 

Via coldplug/hotplug. I've got a problem about that too:

 */etc/hotplug/usb/speedtch.usermap wrote:*   

> speedtouch 0x0003 0x06b9 0x4061 0x0000 0x0000 0x00 0x00 0x00 0x00 0x00 0x00 0x00000000

 

 */etc/hotplug/usb/speedtch wrote:*   

> #!/bin/sh
> 
> mkdir -p $(dirname ${REMOVER})
> 
> echo "${REMOVER}" | grep '0$' &> /dev/null
> ...

 

Sorry, it lost the indentation. Those two files are modifications from the ones installed by the speedtouch package for an adsl modem; as far as I've understood the first loads the speedtch module when I plug the modem in, while the second is called after that. The original one started the script provided with the package, but since I'm using the kernel module with the latest baselayout I'd want it to start net.ppp0 instead; it works, but I'm getting a 'service already started' or 'starting' everytime. I've tried commenting that line, thinking that hotplug loaded the service on its own, but then it doesn't start at all.

I'm a bit confused here; I've modified the file based only on suppositions, since 'man hotplug' mentions /etc/hotplug/usb/DRIVER.usermap but not /etc/hotplug/usb/DRIVER, so I don't know when it's called and if I've done something wrong.

Could you help me with that?

Thanks

Bye

----------

## dsd

 *Quote:*   

> So, insmod works.  Do I copy the .ko file to a specific /lib/module directory too?

 

copying it to the appropriate /lib/modules location gives the advantage that you can load the module with "modprobe mymodule" *without* having to specify the full path. the advantage of being able to load your module with modprobe is taht you can autoload it via gentoo's /etc/modules.autoload.d/ system.

 *Quote:*   

>   Also, since it is a PCI device driver, do I need to put my Vendor and Device ID in the the modules.pcimap file?

 

yes

 *Quote:*   

>   How come I do not see any other modules listed in the /etc/modules.autoload.d/kernel-2.6 file now?  How are other modules started/loaded on boot?  Like the ethernet?

 

do you use genkernel? if you do:

genkernel scans your systems hardware at every boot, and matches PCI ID's of your hardware to PCI ID's provided by drivers. if it finds a matching driver, it automatically loads that driver for you.

if you dont use genkernel:

chances are you didnt actually build any modules. you built support for your network card directly into the kernel. this is the recommended way of doing it because it avoids the complication of module loading. the kernel will automatically initialize the network card driver when it sees your network card.

----------

