Opening a device node in a device driver in linux - linux

I am writing a device driver on Xillinux that will read and write data to an FPGA application over Xillybus.
Basically I want to create device nodes such as /dev/pe1, and when I write to the nodes my device driver will form packets of data and then write the packets into the xillybus nodes eg. /dev/xillybus_write_32
Is it possible to simply open an existing /dev node inside a kernel module, and then perform I/O operations on it? Or is it better to just write a userspace driver?

After reading LDD3 and some other articles on kernel development, I've decided to abandon the idea of writing my custom device driver and put the logic in runtime instead. Thanks for the advice goldilocks.

Related

Why using the libusb requires detaching the kernel driver?

Why using the libusb requires detaching the kernel driver? Why isn't it possible to perform some USB IOs along with the kernel driver?
Mainly to avoid confusion about the state of the USB-device. Each interface can only have one "user" at any given time.
Many USB-devices can enter different execution domains, cache-states, DMA transfers etc. These kinds of devices will then have state-machine-trackers in the driver, and it would easily fall out of sync or other types of conflicts. Not all devices are simple HID interfaces (which can be manipulated via other API's btw)
In order to communicate, each USB device have endpoints. These endpoint are like pipes, in these pipes all the data are travalling.
One endpoint have only one direction and can be use just by 1 driver.
So you need to detach the kernel driver in order to make available these endpoints.
If you want you can always detect and desactivate the driver which use the device in order to avoid the use of detach kernel driver.

What can be removed from the Linux i2c-dev driver to serve as a base for a new driver meant for only one device?

I'm trying to write a Linux character device driver for a device that just happens to communicate over I2C. The device is an Atmel microcontroller with code that provides an I2C address. It already works using the typical i2c-dev method on the Linux-side.
So now I want to replicate i2c-dev as a new driver that works specifically with this particular device, so that I can add some of my own device-specific abstraction code on top. But I'd like to trim out all the unnecessary code from i2c-dev that currently makes it generic. What can be removed in this situation?
What can be removed in this situation?
You're actually asking an XY question.
You would be better off looking at and adapting an existing I2C device driver that is already similar in required functionality, rather than hacking a special case driver for userspace access.
So now I want to replicate i2c-dev as a new driver that works specifically with this particular device, so that I can add some of my own device-specific abstraction code on top
So then you actually need to write a "Client driver" as described below (from Linux Documentation/i2c/summary):
When we talk about I2C, we use the following terms:
Bus -> Algorithm
Adapter
Device -> Driver
Client
An Algorithm driver contains general code that can be used for a whole class
of I2C adapters. Each specific adapter driver either depends on one algorithm
driver, or includes its own implementation.
A Driver driver (yes, this sounds ridiculous, sorry) contains the general
code to access some type of device. Each detected device gets its own
data in the Client structure. Usually, Driver and Client are more closely
integrated than Algorithm and Adapter.
Details are in Documentation/i2c/writing-clients.
To find a driver of similar functionality, scan the list of I2C client drivers. Note that these I2C drivers are located in the Linux source tree by their functionality (e.g. drivers/rtc/ or drivers/hwmon/) and not their interface (i.e. I2C).

difference between device file and device driver

I am currently reading the Linux Module Programming Guide and I have stumbled onto two terms that have confused a bit - device files and device driver. Upon goggling these terms I have come across the following-
A device driver is a piece of software that operates or controls a particular type of device.
A device file is an interface for a device driver that appears in a file system as if it were an ordinary file. In Unix-like operating systems, these are usually found under the /dev directory and are also called device nodes.
What I would like to know is -
1) Are device files an interface between user space programs and the device driver?
2) Does the program access the driver in the kernel via the appropriate device special file?
eg, when using say spidev char dev file, does that allow my userspace program to interact with spi.c and omap2_mcspi.c etc using simple read, write and ioctl calls?
One of the primary abstractions in Unix is the file (source):
Programs, services, texts, images, and so forth, are all files. Input and output devices, and generally all devices, are considered to be files, according to the system.
This lets users treat a variety of entities with a uniform set of operations, even through the implementation of those operations may be wildly different.
As you were getting at with your question, device files are the user facing side of the abstraction. This is what the user sees; a file that they can write to, read from, open, close, etc. The device drivers are the implementation of those operations.
So the user will make a call to a file operation such as write, and then the kernel will then use the device driver to carry out the operation.
Device File like /dev/spidevX.Y is a SW abstraction of a SPI device which exposes Linux low level SPI API to the userspace with syscalls (in Linux driver world known as "file operations"):
That is read(), write(), ioctl()...
spidev.c is a special kind of driver which is registered for generic SPI client(chip) devices, and it's main goal is to export Kernel low level SPI API to userspace.
There is a whole Linux SPI layer in between defined in spi.c
Device driver representing real HW SPI controller is where callbacks (hooks) are implemented and registered to the kernel as a part of spi_master (spi_controller)structure.
Here is a callback initialization for SPI message transfer:
master->transfer_one_message = atmel_spi_transfer_one_message;
everything in linux is a file.
device driver is a software used by operating system to communicate with device.
device driver makes use of device files.

Simple SPI device driver

I have written a simple driver for a character device in Non Blocking mode using epoll.
Now I would like to use that driver for SPI interface.
What should be the device name or how will I map the actual device with the kernel ?
How will I use the interrupt?
Also who is the Master and Slave?Suppose I am connecting a SPI compatible device to the panda board.Will that device becomes a slave and the development board becomes Master?
I am a newbie.I am using a panda board.
In what way should I go through the Technical Reference manual?
I would really appreciate if anybody would explain and clarify my doubts.
You probably want to connect your driver to a SPI bus driver. This requires a slight reworking of your driver. See drivers/spi/spi.c
You'll be using the commands for the bus (master, the CPU side) to read and write the commands to the unit.
I don't know what a Panda board is, but if it connects via SPI, it might be a master, but it is probably a slave.
Go through the basics of spi here

How can I read streaming data from a camera connected to I2C on Linux?

I've looked for ways of reading camera data connected to I2C on Linux.
What is the normal way to achieve the goal?
Could you please show me a skeleton code or running code?
You need to write a userspace program to talk to the i2c-driver. There are some SMbus functions defined to do the data read/write from the userspace to the device driver.
Read here for more: https://www.kernel.org/doc/Documentation/i2c/dev-interface

Resources