Introduction to Linux Driver Development Misc class Equipment

Source: Internet
Author: User

1, what is misc equipment?

Misc is the abbreviation of English, the Chinese name is generally called miscellaneous equipment/stray equipment.

We know that most of the equipment has a clear classification class, some equipment to classify the not very good points, we do not know what kind of equipment in the end should be divided into which type of equipment, so finally these do not know what kind of equipment to be divided into the misc device, that is, scattered in the miscellaneous class. such as buzzer and ADC devices are divided into Misc device spurious devices. Spurious devices correspond to the concept of a class. Random into the system in the/sys/class directory will see a misc directory, this misc is a stray device, into the misc directory, we can see a lot of devices are thrown into the miscellaneous devices.

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/98/6B/wKiom1k7qiOgnuwdAAD_f_CdhjQ050.png-wh_500x0-wm_ 3-wmp_4-s_4204155695.png "title=" Ctmcdj0g9rdq48fjib2l@]o.png "alt=" wkiom1k7qiognuwdaad_f_cdhjq050.png-wh_50 "/ >

In fact, LED can also be thrown into the misc miscellaneous equipment, such as buzzer can also be placed in other equipment class, so this thing is not certain, some people just do not follow the regular work, see also do not strange.

A spurious device is a typical character device, so all that is attributed to a misc spurious device is a character class device. In the back we will also see Misc spurious device itself is to be used as a character device to register to the kernel, the main device number is 10, is a fixed representation of the misc device, the secondary device number is to distinguish the specific spurious devices. For example, the jiuding buzzer driver's main device number is 10, the second device number is the 61,ADC driver's main device number is 10, The secondary device number is 131. Just like we used to achieve led driver, we will board the four LEDs are grouped into the LEDs class, each LED main device number is the same, the second device number is not the same, the main device number as the same class, the second device number is not the same as the different individuals in such devices, to differentiate LED1,LED2 , Led3,led4. The main device number for a spurious device is 10.

If you want to automatically create a device file node, you need to use Udev or Mdev, if you want Udev or Mdev to automatically create device file nodes for some device drivers, then you should drive these devices to a class device driver. The reason why the kernel invented misc miscellaneous equipment is to put some do not know how to classify the device into the misc, in the future to create device node, you can use Udev or mdev mechanism to create, do not need to manually mknode to create a device file node, As well as creating classes in the driver code. We just need to create the device class on the line.

Misc has a set of driver frameworks, the kernel implements a part (MISC.C, in/drvier/char/misc/misc.c), the driver implements a part (X210-BUZZER.C, this part is called Misc Drive framework in the kernel implementation of the part of MISC_ Register function to create the device, the Misc_register function indirectly calls the Device_create function to create the device). The Misc class is created in part of the kernel implementation, and we drive the developer to invoke the interface Misc_register function implemented by the kernel, thereby indirectly invoking the Device_create function to create the device. We just need to create the device, the Misc class has not been created without me. The Misc class has been provided by the Misc driver Framework.

Misc device is actually a kind of character device. Many of the typical character devices can be grouped into misc devices.


2. Drive architecture of Misc class Equipment

(1) The kernel developer realizes the part, the concrete engineer realizes the part. The kernel developed a part of the implemented Misc device is/DRVIER/CHAR/MISC/MISC.C, mainly the creation of the Misc class. such as code

static int __init misc_init (void) {int err; #ifdef config_proc_fsproc_create ("misc", 0, NULL, &misc_proc_fops); #endifmisc_class = Class_create (This_module, "misc"), err = Ptr_err (Misc_class), if (Is_err (misc_class)) Goto Fail_ Remove;err =-eio;if (Register_chrdev (misc_major, "MISC", &misc_fops)//register character device Goto fail_printk;misc_class-> Devnode = Misc_devnode;return 0;FAIL_PRINTK:PRINTK ("Unable to get major%d for misc devices\n", misc_major); Class_destroy (Misc_class); Fail_remove:remove_proc_entry ("Misc", NULL); return err;}


and a Misc_regiter function interface for driver developers. The main thing is to create misc devices

Int misc_register (Struct miscdevice * misc) {struct miscdevice *c;dev_t dev; Int err = 0;init_list_head (&misc->list); Mutex_lock (&AMP;MISC_MTX); List_for_each_entry (c,  &misc_list, list)  {if  (C->minor == misc->minor)  {mutex_unlock ( &AMP;MISC_MTX); return -ebusy;}} if  (Misc->minor == misc_dynamic_minor)  {int i = find_first_zero_bit (Misc _minors, dynamic_minors);if  (i >= dynamic_minors)  {mutex_unlock (&AMP;MISC_MTX); Return -ebusy;} Misc->minor = dynamic_minors - i - 1;set_bit (i, misc_minors);} Dev = mkdev (Misc_major, misc->minor); Misc->this_device = device_create (misc_ class, misc->parent, dev,  misc,  "%s",  misc->name);     //Creating a Device if  (Is_err (misc->this_device))  {int i = dynamic_minors - misc->minor - 1;if  (i < dynamic_minors &&  i >= 0) Clear_bit (i, misc_minors); Err = ptr_err (misc->this_device); goto  out;} /* * add it to the front, so that later devices can   "Override"  * earlier defaults */list_add (&misc->list, &misc_list);  out:mutex_unlock (&AMP;MISC_MTX); return err;}


The part that drives the developer is X210-BUZZER.C

#include  <linux/module.h> #include  <linux/kernel.h> #include  <linux/fs.h># include <linux/init.h> #include  <linux/delay.h> #include  <linux/poll.h> #include  <asm/irq.h> #include  <asm/io.h> #include  <linux/interrupt.h> #include  < asm/uaccess.h> #include  <mach/hardware.h> #include  <plat/regs-timer.h> #include   <mach/regs-irq.h> #include  <asm/mach/time.h> #include  <linux/clk.h> #include   <linux/cdev.h> #include  <linux/device.h> #include  <linux/miscdevice.h> #include   <linux/gpio.h> #include  <plat/gpio-cfg.h>//#include  <plat/regs-clock.h>//#include  <plat/regs-gpio.h>//#include  <plat/gpio-bank-e.h>//#include  <plat/gpio-bank-f.h >//#include  <plat/gpio-bank-k.h> #define  DEVICE_NAME      "buzzer" # Define pwm_ioctl_seT_FREQ1#DEFINE&NBSP;PWM_IOCTL_STOP0STATIC&NBSP;STRUCT&NBSP;SEMAPHORE&NBSP;LOCK;//&NBSP;TCFG0 is set in Uboot and is not repeated here  timer0 Input Frequency finput=pclk/(prescaler1+1)/mux1//                      =66M/16/16// TCFG0 =  tcnt =  (PCLK/16/16)/freq;// pwm0 output frequency foutput =finput/tcfg0= freqstatic void  Pwm_set_freq ( unsigned long freq ) {unsigned long tcon;unsigned long  tcnt;unsigned long tcfg1;struct clk *clk_p;unsigned long pclk;//unsigned  tmp;//set Gpd0_2 to PWM output S3c_gpio_cfgpin (s5pv210_gpd0 (2), &NBSP;S3C_GPIO_SFN (2)); Tcon = __raw_readl ( S3c2410_tcon); Tcfg1 = __raw_readl (S3C2410_TCFG1);//mux = 1/16tcfg1 &= ~ (0xf <<8);tcfg1 |=  (0x4<<8); __raw_writel (TCFG1,&NBSP;S3C2410_TCFG1); clk_p = clk_get (null,  "PCLK");p Clk&nbsP; = clk_get_rate (clk_p);tcnt  =  (PCLK/16/16)/freq;__raw_writel (tcnt, S3C2410_ TCNTB (2)); __raw_writel (TCNT/2,&NBSP;S3C2410_TCMPB (2));//Duty ratio is 50%tcon &= ~ (0xf<<12); Tcon  |=  (0xb<<12);//disable deadzone, auto-reload, inv-off, update tcntb0 &tcmpb0, start timer 0__raw_writel (Tcon, s3c2410_tcon); tcon &= ~ (2< &LT;12);//clear manual update bit__raw_writel (Tcon, s3c2410_tcon);} Void pwm_stop ( void ) {//Gpd0_2 set to Inputs3c_gpio_cfgpin (S5pv210_gpd0 (2), &NBSP;S3C_GPIO_SFN (0));} Static int x210_pwm_open (struct inode *inode, struct file *file) {if  (! Down_trylock (&lock)) Return 0;elsereturn -ebusy;} Static int x210_pwm_close (Struct inode *inode, struct file *file) {Up (& lock); return 0;}  pwm:gpf14->pwm0static int x210_pwm_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned  Long arg) {switch  (cmd)  {case pwm_ioctl_set_freq:printk ("pwm_ioctl_set_freq:\r\n");if  (arg == 0) return -einval; Pwm_set_freq (ARG); BREAK;CASE&NBSP;PWM_IOCTL_STOP:DEFAULT:PRINTK ("pwm_ioctl_stop:\r\n"); Pwm_stop (); break;} return 0;} static struct file_operations dev_fops = {    .owner    =   THIS_MODULE,    .open    =    x210_pwm_open,    .release =   x210_pwm_close,      .ioctl   =   x210_pwm_ioctl,};static struct miscdevice misc  = {.minor = misc_dynamic_minor,.name = device_name,.fops = &dev_ Fops,};static int __init dev_init (void) {Int ret;init_MUTEX (&lock); Ret = misc_register (&AMP;MISC);     //Registration misc/* gpd0_2  (PWMTOUT2)  */ret = gpio_request (s5pv210_gpd0 (2),  "GPD0"); if (ret) PRINTK ("buzzer-x210:  REQUEST&NBSP;GPIO&NBSP;GPD0 (2)  fail "); S3c_gpio_setpull (S5pv210_gpd0 (2),  s3c_gpio_pull_up); S3c_gpio_ Cfgpin (S5pv210_gpd0 (2), &NBSP;S3C_GPIO_SFN (1)), Gpio_set_value (S5pv210_gpd0 (2),  0);p rintk  ("x210  "Device_name"  initialized\n ");     return ret;} Static void __exit dev_exit (void) {misc_deregister (&AMP;MISC);} Module_init (Dev_init); Module_exit (Dev_exit); Module_license ("GPL"); Module_author ("www.9tripod.com"); Module_description ("X210 pwm d


This article from "Whylinux" blog, declined reprint!

Introduction to Linux Driver Development Misc class Equipment

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.