What does a device driver look like? - device-driver

When a manufacturer designs a hardware device, they obviously have someone who is in charge of writing a driver for that device for different platforms.
While I know that there are probably more than one "type" of driver for different types of devices, a driver for a device by it's nature must be very different from a normal application or script.
I've always wanted to pick apart a driver just to find out how it allows an OS to interface with hardware, but my programming knowledge is lacking.
Out of curiosity, I'd just like to know:
How does a device driver work, exactly?
When designing a driver for a device, what things do programmers consider?
What languages are drivers written in?
What is the overall process for designing a driver?

I suggest that you read (at lease the first chapter) "Linux Device Drivers". It will answer your basic questions and will allow you to study how to develop device drivers for Linux OS if you want to.
You can find it here: http://lwn.net/Kernel/LDD3/

When designing a device driver, programmers look at the functionalities of the device that are to be implemented and write the driver accordingly
I prefer C / C++ for writing a device driver
but have seen driver in assembly language also
overall process is dependent on device itself

Related

Programming a software for diagnosing SAE J1939

I'm trying to program a software similar to this
Where could I find specific information on the programming language used, the required environment, etc?
Thanks.
If you have some money to spend, check out the PCAN-USB device. Using this device you can write your own program using their API, which is supported on most major languages, or getting a license for their PCAN-Explorer software whichever you prefer. I believe versions 5.4 and up have a J1939 plugin that you can get.
You can use different hardware devices and create your code in visual studio or other vendor applications such as CANoe or CANAlyzer.
Hardware:
PCAN
Kvazer
IXXAT USB-to-CAN FD
CANcase
VN1610
or create your own CAN to USB gateway design.
All the available hardware supports different development platforms so it will depends on how much time/money you want to expend.
If you are using the Vector CAN interfaces as the hardware, I suggest you review the Vector XL API, which you can use for free. Vector XL API is primarily intended to be used with the C# programming language.

Is it possible to associate single wireless network card to multiple WiFi Access Points at a time?

Is it possible to associate single wireless network interface controller (WNIC) with multiple Wireless Access Points (WAP) at a time? If not: why?
I've never heard about such a feature, so I assume it's technically impossible or fairly difficult and rarely implemented. Is it really that difficult/impossible to implement driver providing such a feature? Is it software or hardware difficulty?
I assume that TCP/IP protocols' specifications doesn't limit us at all because if I attach multiple WNICs to my computer, I can easily connect to multiple APs.
If it's software difficulty, than what's the actual problem? Does Linux/Windows kernel or WNIC's drivers limits it? Or maybe system libraries (like libc on GNU/Linux systems)?
If it's hardware difficulty, what actually limits us? Antennas? Using single radio frequency at a time? If yes, than why can't we implement frequency hopping (like Kismet does)? Because of lost packets during time spent on other channels? If yes, than can we associate WNIC with multiple routers working on the same channel (I know that channel overlapping is bad)?
Note: I'm not talking about dual band routers. I assume that we consider most common WNIC and AP which both work on 2.4GHz channels. If I have to put my question into OS context, than I choose GNU/Linux context.
Yes. The basic technique is that the client tells AP 'A' that it is going to sleep and then talks to AP 'B' while A is buffering frames for it.
Microsoft research worked this out a while ago:
http://research.microsoft.com/en-us/um/redmond/projects/virtualwifi/
Many low-level drivers support Wi-Fi interface virtualization (e.g. the BRCM wl command has options which support this).
Apple's AirDrop and MultiPeer features for OS X and iOS use a similar technique, but instead of talking to a 2nd AP they talk to a peer device.

netbsd ioctl for hard disk information

I can't seem to find a good comprehensive list of available ioctls for netbsd.
I am looking to do some operations on harddisk (getting size, physical sector size, model). I have the code working in linux. It looks something like this, I removed errors etc to make it more compact:
ioctl(fileno(driveptr[i]),HDIO_GET_IDENTITY,&hd);
ioctl(fileno(driveptr[i]),BLKGETSIZE64,&drivesize[i]);
ioctl(fileno(driveptr[i]),BLKPBSZGET,&psztemp);
Is there an equivalent to these ioctls in netbsd?
regards
The driver source for a given type of disk interface (or any other kind of device driver) is probably the best canonical place to find device specific ioctls.
NetBSD at a systems level, like many unix-based systems, tries hard to avoid being hardware specific, even in terms of providing detailed hardware specific information to user level. The goal of unix, after all, is to provide a system that is uniform across a wide variety of hardware platorms, not to provide detailed low-level access to specific hardware. The very best you can get in terms of hardware specific details is the information printed by the drivers at boot time about the hardware as it is probed and attached.
At a more generic level you can basically only get disk and partition labels -- i.e. information pertinent to the way the system presents disk devices to userland. Unfortunately the only driver manual page that documents these is sd(4) (cd(4) has some more detail about more ioctls specific to cd-rom devices). scsi(4) documents bus-level ioctls for SCSI and ATAPI interfaces.
On x86 platforms there's "sysctl machdep.diskinfo" (and the equivalent C level interface via sysctl(3)) to get details about what the BIOS reported about the disks it knew about at boot time, but that may be incomplete.

Creating drivers and firmware

If I was told I needed to create a driver for some product (say, a game controller), how would I go about creating one? Is this something you could do normally in C/C++?
And what about firmware for external deviced connect to USB? How is this created usually? Is this also done in C/C++, or some lower level language?
Thanks!
Device drivers for desktop computer operating systems are generally written in either C or C++. The operating system you would target will have some form of framework or device driver development environment. Often these development kits can be obtained free of charge.
There are books available for Windows, Linux, and MacOS X (and others) that detail the process of creating a device driver.
If your driver is related to a device on a specific hardware bus (PCI, PCI-X, USB, SCSI, SATA, etc.) you can also get books on that specific technology. An understanding of that hardware system can greatly facilitate the design of your driver.
Another good resource is the open source code for similar devices to yours. You can obtain that from the Linux kernel source or FreeBSD source and study how certain aspects of your type of device are implemented.
EDIT: I nearly forgot to mention that you will also need data sheets, schematics, and/or theory of operation information about the device itself.
I'll add to Amardeep's good answer with the following books that will help you think about the context device drivers operate in, and how they're structured:
Linux:
http://www.amazon.com/Understanding-Linux-Kernel-Third-Daniel/dp/0596005652
Windows:
http://www.amazon.com/Programming-Microsoft-Windows-Driver-Model/dp/0735618038/ref=sr_1_1?ie=UTF8&s=books&qid=1277439434&sr=1-1
Mac OS:
http://www.amazon.com/Mac-OS-Internals-Systems-Approach/dp/0321278542/ref=sr_1_1?ie=UTF8&s=books&qid=1277439467&sr=1-1
You do it in any language that can talk to the interface. If it requires poking ports or addresses directly then you use assembly or C. If there's a higher-level interface such as libusb then you can use almost any language you like.

Porting Windows demo apps to WinCE/XP Embedded

We have a range of PC demonstration programs for our microcontroller products. The programs typically connect to a USB HID chip on the microcontroller board. The USB chip acts as a communications bridge, allowing the programs to communicate with the micros over SPI/I2C/UART. The programs can configure the micros, and get back status information to display to the user.
We are now looking to build some standalone demonstrations using single board PCs. We would like to reuse as much as possible of our existing demo app source code. Ideally, we could just run them as-is.
Does anybody have any advice on the best way forward? The basic options seem to be WinCE or XP Embedded boards. WinCE boards seem to pull less power, which would be an advantage from a battery life point of view.
Our existing demos are built either in C++ under Borland Builder, or in Delphi.
Thanks in advance.
EDIT: see my answer below with info from a board vendor.
Free Pascal/Lazarus can compile some forms of Delphi apps to WiNCE/arm. Even visual ones.
There isn't a Delphi version for WinCE, so you would need to rewrite the applications. The same applies for the Borland Builder's control libraries. Only if you have used plain Win32 API, you would be able to port your application to WinCE easily. You may also encounter problems with the hardware access part. The Serial Port driver may not work as is. Also, you need to find a WinCE board that can act as USB host and provides HID drivers (this isn't very common).
In conclusion, I believe that you would be better of with Windows XP Embedded boards. These should run your applications as they are.
As an update, and for future reference, I thought I'd post the results of our discussions with a WinCE board vendor here. Caveat: I haven't actually tried any of this.
The bottom line is that there isn't a straightforward way to do what we were hoping for (i.e., re-compile our existing demo applications to run under WinCE). The reason is that the generic HID drivers and standard APIs that exist in desktop flavours of Windows just aren't there in WinCE.
To talk to HID devices in WinCE you need to implement a custom HID driver. This needs to support an interface allowing user mode applications to communicate with the driver, and to construct HID reports to be sent to the physical device. As this interface would itself be custom, application code needs to be updated accordingly.
WinCE application development is generally done using Visual Studio and the Microsoft compilers. The approach recommended to us was:
Create a custom HID class driver. This could be based on, for instance, the Microsoft keyboard HID driver.
Create an API for talking to the driver.
Use .net to create our GUI applications, and use PInvoke to actually talk to the API.
The end result of all this head-scratching is that to avoid the time and learning curve associated with this approach, we're going to go for a board running XP. We can then use our existing demo applications straight out of box. The trade-off is that we'll have to live with substantially reduced battery life.

Resources