"Android Deep Exploration" (Vol. 1) HAL and Driver development Seventh chapter experience

Source: Internet
Author: User

-Driven porting.

In the Drivers/char directory, we create a driver file mini2440_leds.c, which reads as follows:

<span style= "FONT-SIZE:18PX;" > #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" <span style= "Color:rgb (51, 102, 255);" >//device Name (/dev/leds</span>)
<span style= "Color:rgb (51, 102, 255); List of >//led corresponding GPIO ports
</span>static unsigned long led_table [] = {
S3C2410_GPB (5),
S3C2410_GPB (6),
S3C2410_GPB (7),
S3C2410_GPB (8),
};
<span style= "Color:rgb (51, 102, 255); >//led The status list that the corresponding port will output
</span>static unsigned int led_cfg_table [] = {
S3c2410_gpio_output,
S3c2410_gpio_output,
S3c2410_gpio_output,
S3c2410_gpio_output,
};
<span style= "Color:rgb (51, 102, 255); Implementation of >/*IOCTL function
* The application/user layer will pass parameters to the kernel via the IOCTL function to control the output status of the LEDs
*/
</span>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;
}
<span style= "Color:rgb (51, 102, 255); >//the S3c2410_gpio_setpin function to set the port storage of the LEDs according to the parameters (inversion) passed by the application/user layer
Device,</span>
S3c2410_gpio_setpin (Led_table[arg],!cmd);
return 0;
Default:
Return-einval;
}
}
<span style= "Color:rgb (51, 102, 255); >/*
* Device function operation set, where only the IOCTL function, usually also read, write, open, close, etc., because this led driver has been
* Registered as Misc device, so you can not open/close
*/
</span>static struct File_operations dev_fops = {
. Owner = This_module,
. IOCTL = Sbc2440_leds_ioctl,
};
<span style= "Color:rgb (51, 102, 255); ">/*
* Register the LED driver as a misc device
*/
</span>static struct Miscdevice misc = {
. minor = Misc_dynamic_minor,//dynamic device number
. Name = Device_name,
. FoPs = &dev_fops,
};
<span style= "Color:rgb (51, 102, 255); >/*
* Initialization of the device
*/
</span>static int __init dev_init (void)
{
int ret;
int i;
For (i = 0; i < 4; i++) {
<span style= "Color:rgb (51, 102, 255); >//Setting the port register for the LEDs is output
</span>s3c2410_gpio_cfgpin (Led_table[i], led_cfg_table[i]);
<span style= "Color:rgb (51, 102, 255); >//set the LED corresponding to the port register for the low-level output, after the module is loaded, four LEDs should be all glow
Status </span>
S3c2410_gpio_setpin (Led_table[i], 0);
}
ret = Misc_register (&MISC); <span style= "Color:rgb (51, 102, 255);" >//Registration Equipment </span>
PRINTK (device_name "\tinitialized\n"); <span style= "Color:rgb (51, 102, 255);" >//Print initialization Information </span>
return ret;
}
static void __exit dev_exit (void)
{
Misc_deregister (&MISC);
}
Module_init (dev_init); <span style= "Color:rgb (51, 51, 255);" >//module initialization, which is useful only when loading with the Insmod/podprobe command, will not be called if the device is not loaded by module mode
</span>module_exit (Dev_exit<span style= "" >) <span style= "Color:rgb (51, 102, 255);" >//unload the module, when the device is loaded through the module mode, it can be uninstalled by the Rmmod command, which will be called
</span></span><span style= "" > Number
</span>module_license ("GPL"); <span style= "Color:rgb (51, 102, 255);" >//Copyright Information
</span>module_author ("Friendlyarm Inc."); <span style= "Color:rgb (51, 102, 255); ">//Developer Info </span></span>

Next, we add the LED device kernel configuration option, open the Drivers/char/kconfig file, add the following red section:
Config Devkmem
BOOL "/DEV/KMEM Virtual device Support"
Default Y
Help
Say Y here if you want to support THE/DEV/KMEM device. The
/dev/kmem device is rarely used and can be used for certain
Kind of kernel debugging operations.
When in doubt, say "N".

Config leds_mini2440
TriState "LED support for Mini2440 GPIO LEDs"
Depends on mach_mini2440
Default Y if mach_mini2440

Help
This option enables the support for LEDs connected to GPIO lines
On Mini2440 boards.
Config MINI2440_ADC
bool "ADC driver for Friendlyarm Mini2440 development boards"
depends on mach_mini2440
default y if mach_mini2440
Help
This is the ADC driver for friendlyarm Mini2440 development boards
notes:the touch-screen-driver required this option
Next, according to the driver's configuration definition, the corresponding drive target file into the kernel, open the Linux-2.6.32.2/drivers/char/makefile file, add the following red part of the content:
obj-$ (CONFIG_JS_RTC) + = JS-RTC.O
js-rtc-y = RTC.O
obj-$ (config_leds_mini2440) + = MINI2440_LEDS.O

obj-$ (CONFIG_MINI2440_ADC) + = MINI2440_ADC.O
# Files generated that shall is removed upon make clean
Clean-files: = consolemap_deftbl.c defkeymap.c
In this way, we will add the LED driver in the kernel.

"Android Deep Exploration" (Vol. 1) HAL and Driver development Seventh chapter experience

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.