Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Understanding Linux driver

Status
Not open for further replies.

icemetal

Member level 2
Member level 2
Joined
Sep 2, 2009
Messages
51
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,615
I will like to understand how this driver works , can anyone put comments or send me to a site that would explain most of this commands

Code:
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <linux/gpio.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>


#define DEVICE_NAME "leds"

static unsigned long led_table [] = {
  S3C2410_GPB(5),
  S3C2410_GPB(6),
  S3C2410_GPB(7),
  S3C2410_GPB(8),
};

static unsigned int led_cfg_table [] = {
  S3C2410_GPIO_OUTPUT,
  S3C2410_GPIO_OUTPUT,
  S3C2410_GPIO_OUTPUT,
  S3C2410_GPIO_OUTPUT,
};

static int sbc2440_leds_ioctl(
  struct inode *inode, 
  struct file *file, 
  unsigned int cmd, 
  unsigned long arg)
{
  switch(cmd) {
  case 0:
  case 1:
    if (arg > 4) {
      return -EINVAL;
    }
    s3c2410_gpio_setpin(led_table[arg], !cmd);
    return 0;
  default:
    return -EINVAL;
  }
}

static struct file_operations dev_fops = {
  .owner  =  THIS_MODULE,
  .ioctl  =  sbc2440_leds_ioctl,
};

static struct miscdevice misc = {
  .minor = MISC_DYNAMIC_MINOR,
  .name = DEVICE_NAME,
  .fops = &dev_fops,
};

static int __init dev_init(void)
{
  int ret;

  int i;
  
  for (i = 0; i < 4; i++) {
    s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);
    s3c2410_gpio_setpin(led_table[i], 0);
  }

  ret = misc_register(&misc);

  printk (DEVICE_NAME"\tinitialized\n");

  return ret;
}

static void __exit dev_exit(void)
{
  misc_deregister(&misc);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
 

Thanks, but if you had read the book you'll realize that they don't talk about the GPIO and this driver is about GPIOs, that's what I want to understand,
 

Hi ,

I think the information that is given in the code is not sufficent to only inform that you are communcating with a GPIO appers to be LED toggling project....I cannot see any main in your code....For GPIO based thing how and where you are declearing the Pins and what is the processor( I think it is ARM core) that you are working with....no information... being the embeeded code person....I cannot found the declearation for the GPIO...

In general....GPIO is general perpose input output pin ....can be configured as input or output based on the register discription made at the start of main....

with regards,

Milind
 

Hi Milind, the pins are being declare in this line
static unsigned long led_table [] = {
S3C2410_GPB(5),
S3C2410_GPB(6),
S3C2410_GPB(7),
S3C2410_GPB(8),
};

GPB5, GPB6, GPB7, GPB8, if you ever used regular microcontrollers it means PORT B pin 5, 6, 7 and 8, but what I dont understand is the for loop they are using there and the rest of the stuff some are standar for drivers like
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

but I just need to understand every line of it , if is default or what exactly means.
 

Ok, are u saying loop in dev_init function....then at least with the best of my understanding the first part is to configure the Pin of your port "s3c2410_gpio_cfgpin" as a output and I think in your hardware the Leds are powered with +Vcc and by setting 0 to the port pin your are making them "ON" bcz with the best of my knowledge to set the port pins as output you need to write high to Pin that like "PORTB" =0xff ....so If you want to make led's ON with the corresponding pin you need to write "0" to it......Here is some idea that I will suggest you to do....either take CRO or multimeter and see the PIN status of the Micro-controller when LED's are "ON" and LED's "OFF".....I think will help you to debug and understand the stuff better
 

I don't understand what you mean, is a working driver, why do I need a multimeter for? I'm a lot more confused now , :-(
 
Last edited:

See multimeter or cro....I was suggesting just to check the port toggeling behaviour and it's frequency .....Many times if you write port pins as 1's ( inilization) then the port become output and reverse as an input....Ok...so I will recommand you to check that....that was the only purpose....second thing I think your LED will be OFF when your Port Pin Become 1's ......and onces you write port pin to 0 it will be becoming on....Just geussing......check that....So if you want LED toggeling then you need to write 0's and 1's correspondingly.....also there is one more problem esepacially dealing with LED that the frequency that you write 1's and 0's make lot of difference....you will see LED toggeling only and only if there is sufficently dealy more thant at lest 16 ms ....I mean like 1 sec.....is required as humen vision dose not perceive the filcker less than 16 ms in most of the cases.....I hope you understand what I mean to say....

Any way good luck....

Milind
 

Thanks Milind but I think you're talking about a totally different topic, check what was the question at the beginning of the post.
 
Last edited:
Hi incemental,
This code simply is a linux module to integrated to kernel
when build kernel or when in runtime. And this module is simple
driver.
module_init(dev_init) - to specify function(in this example the func is dev_init)
be called when module be installed to kernel by using "insmod" command. this function
will init needed everythings(static variable, structures,...) .
module_exit(dev_exit) - to specify function(in this example the func is dev_exit)
be called when remove this module from kernel by using "rmmod " command or
when kernel exist.

MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

two marcos be used to provide information in related to module.

ret = misc_register(&misc)
register a new driver to kernel with parameter is pointer point
to a struct miscdevice-typed variable.
In here, struct miscdevice will contain minor/name and list of operations
the driver supports. with this examples, just ioctl() is supported.
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top