Accessing RTC I2C chip connecting to FPGA through GPIO - linux

I need to get data from a RTC device (stm41t83) through I2C. The device is directly connected to two pins of a GPIO. I tried to use the i2c-gpio driver with the piece of code below,
#include <linux/module.h>
#include <linux/init.h>
#include <linux/i2c-gpio.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#define GPIO_RTC_SDA 100
#define GPIO_RTC_SCL 99
MODULE_DESCRIPTION("i2c via gpio module");
MODULE_LICENSE("GPL");
static struct i2c_gpio_platform_data rtc_device_data = {
.sda_pin = GPIO_RTC_SDA,
.scl_pin = GPIO_RTC_SCL,
.udelay = 25
};
static struct platform_device i2c_gpio_bus_rtc = {
.name = "i2c-gpio",
.id = 0,
.dev = {
.platform_data = &rtc_device_data,
}
};
static int __init i2c_gpio_bus_init(void)
{
return platform_device_register(&i2c_gpio_bus_rtc);
}
static void __exit i2c_gpio_bus_exit(void)
{
platform_device_unregister(&i2c_gpio_bus_rtc);
}
module_init(i2c_gpio_bus_init);
module_exit(i2c_gpio_bus_exit);
but when I use the i2cdetect utility from the i2c tools package, I can't see my RTC device located at 0x68. Do I need to add some other stuff to get it working?
Thanks for answers!
jrm

I finally solved my problem! I was not a software problem but rather a FPGA configuration issue. Follow this link, post #13 to know the answer.
Thanks to all ;-)

Related

Failed to read APIC timer registers

I am writing a Linux kernel module to read dump local APIC timer registers.
I am using Ubuntu 16.04 desktop on X86_64 platform.
X2APIC is disabled, and nohz=off in grub.cfg.
I am using following codes to read APIC timer registers.
#include <linux/slab.h>
#include <linux/time.h>
#include <asm/string.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <asm/apic.h>
void read_apic_timer(void)
{
printk("APIC_TDCR = 0x%x\n", apic_read(APIC_TDCR));
printk("APIC_TMICT = 0x%x\n", apic_read(APIC_TMICT));
printk("APIC_TMCCT = 0x%x\n", apic_read(APIC_TMCCT));
}
static int __init timer_init(void)
{
read_apic_timer();
return 0;
}
static void __exit timer_exit(void)
{
printk("module uninstalling\n");
}
module_init(timer_init);
module_exit(timer_exit);
MODULE_LICENSE("GPL");
And I got these,
[ 5619.047497] APIC_TDCR = 0x0
[ 5619.047498] APIC_TMICT = 0x0
[ 5619.047499] APIC_TMCCT = 0x0
To my surprise, initial counter and current counter are all 0, is it correct?
Or did I miss something or make something wrong?
I think I get the answer. It is because the CPU supports TSC deadline feature/mode for LAPIC timer. In this mode, APIC_TDCR/TMICT/TMCCT are not being used. That is it.

Accessing ethernet phy driver from linux user space

i want to access ethernet phy driver from linux user space,
In uboot we can directly access phy registers using mii commands
similarly i want to read and write phy registers from linux user space .
cause there is no major or minor number comes in case of phy driver(maybe cause its a network driver)
how to do it.and is it possible
There are the following ioctl requests for that purpose:
#define SIOCGMIIREG 0x8948 /* Read MII PHY register. */
#define SIOCSMIIREG 0x8949 /* Write MII PHY register. */
And MII register constants are defined in:
#include <linux/mii.h>
Example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/mii.h>
#include <linux/sockios.h>
int main()
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, "eth1");
struct mii_ioctl_data* mii = (struct mii_ioctl_data*)(&ifr.ifr_data);
mii->phy_id = 1;
mii->reg_num = MII_BMSR;
mii->val_in = 0;
mii->val_out = 0;
const int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd != -1)
{
if (ioctl(fd, SIOCGMIIREG, &ifr) != -1)
{
printf("MII_BMSR = 0x%04hX \n", mii->val_out);
printf("BMSR_LSTATUS = %d \n", (mii->val_out & BMSR_LSTATUS) ? 1 : 0);
}
close(fd);
}
return 0;
}
Try to use mii-tool or ethtool. Look at the sources of those programs how to get access to phy api.

How can I get rq structure in linux kernel?

I made a linux module program to get rq structure in CPU like this
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <../kernel/sched/sched.h>
int init_hello(void)
{
int cpu;
for_each_online_cpu(cpu)
{
struct rq *rq = cpu_rq(cpu)
printk( KERN_ALERT "rq->curr->pid = %d", rq->curr->pid);
}
}
...
Both 'Makefile' and 'insmod hello.ko' are done, But I got a error message like this
...
[30557.012624] hello: Unknown symbol runqueues (err 0)
How can I solve this problem?

Kernel Module to Ethernet Packet Echo

I develop a linux kernel module to re-transmit some ethernet packet (do the echo). THe packet arrives, i check the ethernet destination address, and if it is for me, i re-transmit. If not i do nothing.
I used dev_pack_eth to define my protocol handler to recieve all ethernet packet (EHT_P_ALL) and dev_queue_xmit to transmit the skb buff received.
It works, the echo is functional but...
Sometimes, very often. the kernel crashs and i don't know why.
When i re-transmit the packet, i return NET_RX_Sucess.
When i don't re-transmit i use kfree_skb to free the skb buff received and return NET_RX_DROP.
I think the problem my be on this issues. Can you help me?
If needed i could post the kernel module code.
Best Regards!
------------Edit:Code added--------
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
#include <linux/skbuff.h>
#include <linux/if_ether.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_bridge.h>
#include <asm-generic/types.h>
/*Buscar as interfaces de rede*/
struct net_device *dev_eth0;
struct net_device *dev_eth1;
int contador;
static struct packet_type hook; /* Initialisation routine */
void handler_add_config (void);
void handler_remove(void);
void print_mac_hdr(struct ethhdr *eth);
static int hook_func( struct sk_buff *skb)
{
struct ethhdr *eth;
struct ethhdr aux;
eth= eth_hdr(skb)
print_mac_hdr(eth);
/*If destination isn't the same that dev_addr, the packet is not for me: do nothing*/
if(memcmp(eth->h_dest,skb->dev->dev_addr,ETH_ALEN)!=0)
{
printk("Não são iguais!!!\n");
}
else
{
/*Swap addr*/
memcpy(&(aux.h_dest),eth->h_dest,ETH_ALEN);
memcpy(eth->h_dest,eth->h_source,ETH_ALEN);
memcpy(eth->h_source,&(aux.h_dest),ETH_ALEN);
/*Re build ther hearders*/
skb->data = (unsigned char *)skb->mac_header;
skb->len += ETH_HLEN;
skb->pkt_type = PACKET_OUTGOING;
/*Send*/
if(dev_queue_xmit(skb)!= NET_XMIT_SUCCESS)
{
printk("Erro na transmissão\n");
}
else
{
printk("Trama retransmitida com sucesso\n");
return NET_RX_SUCCESS;
}
}
kfree_skb(skb);
return NET_RX_DROP;
}
/*Print eth headers*/
void print_mac_hdr(struct ethhdr *eth)
{
printk("Destino: %02x:%02x:%02x:%02x:%02x:%02x \n",eth->h_dest[0],eth->h_dest[1],eth->h_dest[2],eth->h_dest[3],eth->h_dest[4],eth->h_dest[5]);
printk("Origem: %02x:%02x:%02x:%02x:%02x:%02x\n",eth->h_source[0],eth->h_source[1],eth->h_source[2],eth->h_source[3],eth->h_source[4],eth->h_source[5]);
printk("Proto: 0x%04x\n",ntohs(eth->h_proto));
}
/*Configure Protocol Handler*/
void handler_add_config (void)
{
hook.type = htons(ETH_P_ALL);
hook.func = (void *)hook_func;
hook.dev = NULL;
dev_add_pack(&hook);
printk("Handler Protocol adicionado!!!!\n");
}
/*Unregist protocol handler*/
void handler_remove(void)
{
dev_remove_pack(&hook);
printk("Handler Protocol removido!!!!\n");
synchronize_net();/*Sincronizar a rede!*/
}
/*Init module and protocol handler*/
static int __init hook_init(void)
{
printk("Hello:I'm the hook module!!!!\n");
contador =0;
dev_eth0=dev_get_by_name(&init_net,"eth0");
dev_eth1=dev_get_by_name(&init_net,"eth1");
handler_add_config();
return 0;
}
/*Remove module and protocol handler*/
static void __exit hook_exit(void)
{
printk("Hook module says Goodbye!!!!!\n");
handler_remove();
}
module_init(hook_init);
module_exit(hook_exit);
MODULE_LICENSE("GPL");
i think when u are returning return NET_RX_DROP; it causes problem because there are basically return types in hooks are ...
Return Code Meaning
NF_DROP Discard the packet.
NF_ACCEPT Keep the packet.
NF_STOLEN Forget about the packet.
NF_QUEUE Queue packet for userspace.
NF_REPEAT Call this hook function again.
& u are returning NET_RX_DROP so try to use NF_DROP.
Look at af_x25.c in net/x25 for a sample implementation of the same where they return 0 even on a drop. BTW didn't understand why are you incrementing skb->len when all you are doing is swapping the mac addresses ? I.e why the need to rebuild hdrs in that sense? Am i missing something here?
You are probably crashing because you are freeing the slngle copy of sk_buff by calling kfree_skb(skb);

Linux USB device driver not getting probed

I'm working on a device driver for Linux. It's a USB pen tablet. The problem is that the driver's probe callback never gets called. dmesg just shows:
generic-usb: probe of 0003:099A:2620.000F failed with error -22
and i never get to connect to the device. It seems like the systems drivers are overriding my driver in some way?
My code is registering & unregistering correctly using insmod / rmmod:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/slab.h>
MODULE_DEVICE_TABLE (usb, id_table);
struct usb_device_id id_table[] =
{
{USB_DEVICE(0x099a, 0x2620)}, //Zippy Technology Corp. Digi Tablet
{0}
};
void dt_disconnect(struct usb_interface *interface)
{
printk("dt_disconnect called\n");
}
int dt_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
printk("dt_probe called\n");
return 0;
}
static struct usb_driver dt_driver =
{
.name = "Zippy Technology Corp. Digi Tablet",
.probe = dt_probe,
.disconnect = dt_disconnect,
.id_table = id_table
};
static int __init dt_init(void)
{
//0 means success
int error = usb_register(&dt_driver);
if(error)
printk("dt_init failed\n");
return 0;
}
static void __exit dt_exit(void)
{
//void
usb_deregister(&dt_driver);
}
module_init(dt_init);
module_exit(dt_exit);
MODULE_LICENSE("GPL");
dt_probe is never called. I'm using Linux 2.6.40 (Fedora 15's version of 3.0) and most documentation about this stuff is very old so I thought I'd ask here. Any thoughts?
Yes, usbhid driver overrides your driver. You need to remove the usbhid driver from the running kernel. First deattach your device from the system and use "modprobe -r usbhid" to remove the usbhid module. Now insert your module and attach the device, then your driver will be taken.

Resources