when is probe function of GPIO based MDIO bitbang driver called - linux-device-driver

Following is the definition of the probe function in the standard GPIO based MDIO bitbang driver
static int __devinit mdio_ofgpio_probe(struct of_device *ofdev,const struct of_device_id* match)
I can't figure out the purpose of __devinit in the above code.
Secondly when is the probe function called by the driver? May be when the driver is loaded itself. But it's not the part of driver init functions. Correct me if I am wrong?

There are lists maintained at the bus driver structure for the available devices on the bus and also the available drivers in the system which can support the devices on that bus.
Now when insert the module in the kernel. Only init_module will be called to do basic initialization w.r.t your driver, but when you insert your device, a match function which is part of the bus structure is called to check if the list of drivers has any driver which supports your device. Upon successful match the probe of your driver is called.

Related

example for low layer fatfs for spi flash

I want to launch the FatFs file system library for SPI FLASH memory.
I need an example or a guide to port the low layer of the this library.
FatFs Link:
http://elm-chan.org/fsw/ff/00index_e.html
SPI FLASH Part number: W25Q64FV
MCU Type: STM32F107VC
Just implement the functions named on that page. These are:
disk_status - Get device status
disk_initialize - Initialize device
disk_read - Read sector(s)
disk_write - Write sector(s)
disk_ioctl - Control device dependent functions
get_fattime - Get current time
How you do it doesn't matter. There just needs to be a definition of these functions provided by you for these already declared and used functions inside the library.
You'll need to set the fattime to static for the most basic version of this. And the functions disk_status disk_ioctl get_fattime can do nothing and just return RES_OK

Using STs I2C HAL Library to write no data to a register

I need to write to a register to kick a device out of boot and into application mode for the using I2C on the ST-Nucleo-F767ZI. I am currently using the ST provided HAL function HAL_I2C_Mem_Write to write data to registers, but this function requires the data to not be NULL. What is the correct way to ping a register using the ST HAL? Is it HAL_I2C_Master_Transmit?
The answer to the above question is that yes - the correct way to handle a ping of a register is via the HAL_I2C_Master_Transmit. This function will transmit the data provided - in this case the register address on the device. The HAL_I2C_Mem_Write function is a higher level function which expects that the caller is writing data to the register; hence, in the function itself if the input size is 0 or pData is NULL, then the driver will throw HAL_ERROR.

What is the meaning of "driver core" in the context of Linux kernel device drivers?

I was reading through the Linux Device Drivers, Third Edition book and in the "Putting It All Together" section in Chapter 14, they mention the interaction between "PCI core, driver core and the individual PCI drivers." And they used the word "driver core" multiple other times. Is the
"driver core" different from the "character device driver"?
My question is arises from the intent of understanding the InfiniBand stack. The IB stack spans both the user-space and the kernel-space. So, if I am writing a simple ping-pong InfiniBand program to run on the Mellanox ConnectX-4 NIC, my binary will depend on 2 user-space libraries: libibverbs and libmlx5, AND 3 kernel-modules: ib_uverbs, mlx5_ib and mlx5_core. I know ib_uverbs is a character device driver. But can we consider the mlx5_ib and mlx5_core kernel modules as some category of driver? Or are their functions just globally exported in order to interface with them?
The driver core is the generic code that manages drivers, devices, buses, classes, etc. It is not tied to a specific bus or device. I believe the chapter you refer to provides several examples to the division of labor between the PCI bus driver and the driver core, for example, see Figure 14-3 (Device-creation process).
Of the three kernel modules you mention, two participate in the device core: ib_uverbs registers its character devices to export RDMA functionality to user-space; mlx5_core registers a PCI driver to handle ConnectX NICs; mlx5_ib can also be considered a driver, but the RDMA subsystem doesn't use the device core to register drivers (it has its own API - ib_register_device).
what is driver core ??
Observe following flow of calls in Linux Source.
tps65086_regulator_probe---> devm_regulator_register--> regulator_register-->device_register(/drivers/regulator/tps65086-regulator.c--->/drivers/regulator/core.c---> drivers/base/core.c ).
tps65086 driver calls regulator core which in turn calls driver core.
This driver is following standard driver model.
include/linux/device.h ----> driver model objects are defined here.
drivers/base/ --> All Functions that operate on driver model objects are defined here.
we can refer this as driver core and is base for any driver Frame work.
All the registrations come here from Higher Layers.
Any driver subsystem ..weather PCI/USB/Platform is based on this.
drivers/base/core.c -- is the core file of standard driver model.
A slight confusion in naming - However we can refer drivers/base/ - as driver core .
Any difference character driver vs other driver ??
/drivers/char/tlclk.c
tlclk_init-->register_chrdev -->register_chrdev --> cdev_add --> kobj_map (fs/char_dev.c ---> /drivers/base/map.c ).
a character device registration is done with char_dev, a file system driver, which again uses infrastructure of driver model base.
whare as a non character driver like tps65086-regulator.c, may register with driver core as below.
tps65086_regulator_probe---> devm_regulator_register--> regulator_register-->device_register-->device_add-->kobject_add
(/drivers/regulator/tps65086-regulator.c--->/drivers/regulator/core.c---> drivers/base/core.c )
Also It's not just based on type of driver , but based on what kind of device it is and how device needs to be handled.
Here is a pci-driver which register's a character device.
tw_probe-->register_chrdev --> cdev_add --> kobj_map ( /drivers/scsi/3w-xxxx.c -->fs/char_dev.c ---> /drivers/base/map.c )
There is no standard rule wether a driver should call driver core or not.
Any stack / framework can have its own core to manage a device,which is the case with the driver mlx5_ib (drivers/infiniband/core/).
But Finally mostly it will use Kobject infrastructure and driver model objects,like struct device.
driver model infrastructure is introduced to eliminate redundant kernel code and data structures.
so most drivers are based on this and it is the effective way of writing a linux driver.

Device Drivers vs the /dev + glibc Interface

I am looking to have the processor read from I2C and store the data in DDR in an embedded system. As I have been looking at solutions, I have been introduced to Linux device drivers as well as the GNU C Library. It seems like for many operations you can perform with the basic Linux drivers you can also perform with basic glibc system calls. I am somewhat confused when one should be used over the other. Both interfaces can be accessed from the user space.
When should I use a kernel driver to access a device like I2C or USB and when should I use the GNU C Library system functions?
The GNU C Library forwards function calls such as read, write, ioctl directly to the kernel. These functions are just very thin wrappers around the system calls. You could call the kernel all by yourself using inline assembly, but that is rarely helpful. So in this sense, all interactions with the kernel driver will go through these glibc functions.
If you have questions about specific interfaces and their trade-offs, you need to name them explicitly.
In ARM:
Privilege states are built into the processor and are changed via assembly commands. A memory protection unit, a part of the chip, is configured to disallow access to arbitrary ranges of memory depending on the privilege status.
In the case of the Linux kernel, ALL physical memory is privileged- memory addresses in userspace are virtual (fake) addresses, translated to real addresses once in privileged mode.
So, to access a privileged memory range, the mechanics are like a function call- you set the parameters indicating what you want, and then make a ('SVC')- an interrupt function which removes control of the program from userspace, gives it to the kernel. The kernel looks at your parameters and does what you need.
The standard library basically makes that whole process easier.
Drivers create interfaces to physical memory addresses and provide an API through the SVC call and whatever 'arguments' it's passed.
If physical memory is not reserved by a driver, the kernel generally won't allow anyone to access it.
Accessing physical memory you're not privileged to will cause a "bus error".
BTW: You can use a driver like UIO to put physical memory into userspace.

Matlab and FTDI

I am trying to send/retreieve data from/to FPGA using Matlab. I connected FPGA using Virtual com port. Now how to send data from Matlab to FPGA or read data of FPGA ?
FTDI 2232H is on the FPGA as well. I connected external LED's and switches on the I/O ports of the FPGA.
I am new in this field, so want some guideline to start communication b/w MAtlab and FPGA:
I tried following code:
s1= serial('COM9')
fopen(s1)
. Is it the right way to communicate ? Kindly guide. thanks
FPGA's are configured using a Hardware Description Language (HDL) such as Verilog or VHDL. These languages let you specify how the switch configuration within the FPGA, which in turn lets you construct your custom digital logic and processing system.
The HDL Coder Toolbox in Matlab lets you design and prototype your custom logic using higher-level functions, which are then translated into HDL and can be be used to directly program your chip. This tutorial describes the process in detail.
If you already have a design implemented on your FPGA and want to communicate with that implementation, you would use Matlab's serial port communication functions. The exact protocol will depend on the interface you have implemented.
Some intermediate debugging steps I find helpful:
Verify that you can send serial port data from your computer. In Windows XP, you can do this easily with HyperTerminal, and hooking up a scope to the output pins of your serial cable. Set up a trigger to capture the event. For Windows 7 and newer, you'll need to download a HyperTerminal client.
Repeat this same process with Matlab. Using a scope, verify that you see the serial port signal when sent from Matlab, and that the output matches the results from step 1. Again, set up a scope trigger to capture the event.
Now connect the serial cable directly to the FPGA board. Modify your HDL to include a latch on the serial input that displays the output on the LED's. Verify that your board initializes to the correct LED state, and that the LED state changes when you send the serial message.
Lastly, verify that you are interpreting the message correctly on the FPGA side. This includes making sure that the bit-ordering is correct, etc. Again, the LED outputs can be very helpful for this part.
The key here is to take small, incremental steps, physically verifying that things are working each step of the way.