Sie sind auf Seite 1von 5

7/19/13

Write For Us Submit Tips

Device Drivers, Part 5: Character Device Files -- Creation & Operations - LINUX For You
Subscribe to Print Edition Search

HOME

REVIEWS

HOW-TOS

CODING

INTERVIEWS

FEATURES

OVERVIEW

BLOGS

SERIES

IT ADMIN

Device Drivers, Part 5: Character Device Files Creation & Operations


By Anil Kumar Pugalia on April 1, 2011 in Coding, Developers 46 Comments and 0 Reactions

Search for:

Search

Get Connected RSS Feed Twitter

This article is a continuation of the series on Linux device drivers, and carries on the discussion on character drivers and their implementation.
In my previous article, I had mentioned that even with the registration for the < m a j o r ,m i n o r > device range, the device files were not created under / d e v instead, Shweta had to create them manually, using m k n o d . However, on further study, Shweta figured out a way to automatically create the device files, using the u d e vdaemon. She also learnt the second step to connect the device file with the device driver linking the device file operations to the device driver functions. Here is what she learnt.

Automatic creation of device files


Earlier, in kernel 2.4, the automatic creation of device files was done by the kernel itself, by calling the appropriate APIs of d e v f s . However, as the kernel evolved, kernel developers realised that device files were more related to user-space and hence, as a policy, that is where they ought to be dealt with, not at the kernel. Based on this idea, the kernel now only populates the appropriate device class and device information into the / s y swindow, for the device under consideration. User-space then needs to interpret it and take appropriate action. In most Linux desktop systems, the u d e vdaemon picks up that information, and accordingly creates the device files.
u d e vcan be further configured via its configuration files to tune the device file names, their

permissions, their types, etc. So, as far as the driver is concerned, the appropriate / s y sentries need to be populated using the Linux device model APIs declared in < l i n u x / d e v i c e . h > . The rest should be handled by u d e v . The device class is created as follows:
s t r u c tc l a s s* c l=c l a s s _ c r e a t e ( T H I S _ M O D U L E ," < d e v i c ec l a s sn a m e > " ) ;

LINUX For You on

Follow

+2,391

Then, the device info (< m a j o r ,m i n o r > ) under this class is populated by:
d e v i c e _ c r e a t e ( c l ,N U L L ,f i r s t ,N U L L ," < d e v i c en a m ef o r m a t > " ,. . . ) ;

Here, the first is d e v _ twith the corresponding < m a j o r ,m i n o r > . The corresponding complementary or the inverse calls, which should be called in chronologically reverse order, are as follows:
d e v i c e _ d e s t r o y ( c l ,f i r s t ) ; c l a s s _ d e s t r o y ( c l ) ;

www.linuxforu.com/2011/04/character-device-files-creation-operations/

1/5

7/19/13

Device Drivers, Part 5: Character Device Files -- Creation & Operations - LINUX For You
Find us on Facebook

Refer to Figure 1 for the / s y sentries created using c h a r d r vas the < d e v i c ec l a s sn a m e > and m y n u l las the < d e v i c en a m ef o r m a t > . That also shows the device file, created by u d e v , based on the < m a j o r > : < m i n o r >entry in the d e vfile.

Open Source For You


Like 250,974 people like Open Source For You.

F acebook social plugin

Popular

Comments

Tag cloud

April 4, 2013 4 Comments Aditya-Pareek

Crunchbang Linux Minimalist and Mac-Friendly


May 6, 2013 4 Comments Priyanka Sarkar

PHP Development: A Smart Career Move


April 1, 2013 2 Comments vinayak-pandey

Learn the Art of Linux Troubleshooting


April 4, 2013 2 Comments Claudia

Top 7 Linux Tips And Tricks For Beginners


Figure 1: Automatic device file creation
April 4, 2013 2 Comments Priyanka Sarkar

In case of multiple minors, the d e v i c e _ c r e a t e ( )and d e v i c e _ d e s t r o y ( )APIs may be put in the for loop, and the < d e v i c en a m ef o r m a t >string could be useful. For example, the
d e v i c e _ c r e a t e ( )call in a for loop indexed by i could be as follows:
d e v i c e _ c r e a t e ( c l ,N U L L ,M K N O D ( M A J O R ( f i r s t ) ,M I N O R ( f i r s t )+i ) ,N U L L ," m y n u l l % d " ,i ) ;

Will Certifications Related to FOSS Help You Get a Job?

File operations
Whatever system calls (or, more commonly, file operations) we talk of on a regular file, are applicable to device files as well. Thats what we say: a file is a file, and in Linux, almost everything is a file from the user-space perspective. The difference lies in the kernel space, where the virtual file system (VFS) decodes the file type and transfers the file operations to the appropriate channel, like a filesystem module in case of a regular file or directory, and the corresponding device driver in case of a device file. Our discussion focuses on the second case. Now, for VFS to pass the device file operations onto the driver, it should have been informed about it. And yes, that is what is called registering the file operations by the driver with the VFS. This involves two steps. (The parenthesised code refers to the null driver code below.) First, lets fill in a file operations structure (s t r u c tf i l e _ o p e r a t i o n sp u g s _ f o p s ) with the desired file operations (m y _ o p e n ,m y _ c l o s e ,m y _ r e a d ,m y _ w r i t e , ) and initialise the character device structure (s t r u c tc d e vc _ d e v ) with that, using c d e v _ i n i t ( ) . Then, hand this structure to the VFS using the call c d e v _ a d d ( ) . Both c d e v _ i n i t ( )and c d e v _ a d d ( )are declared in < l i n u x / c d e v . h > . Obviously, the actual file operations (m y _ o p e n , m y _ c l o s e ,m y _ r e a d ,m y _ w r i t e ) also had to be coded. So, to start with, lets keep them as simple as possible lets say, as easy as the null driver.

The null driver


Following these steps, Shweta put the pieces together, attempting her first character device driver. Lets see what the outcome was. Heres the complete code o f c d . c :
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 # i n c l u d e< l i n u x / m o d u l e . h > # i n c l u d e< l i n u x / v e r s i o n . h > # i n c l u d e< l i n u x / k e r n e l . h > # i n c l u d e< l i n u x / t y p e s . h > # i n c l u d e< l i n u x / k d e v _ t . h > # i n c l u d e< l i n u x / f s . h > # i n c l u d e< l i n u x / d e v i c e . h > # i n c l u d e< l i n u x / c d e v . h > s t a t i cd e v _ tf i r s t ;/ /G l o b a lv a r i a b l ef o rt h ef i r s td e v i c en u m b e r s t a t i cs t r u c tc d e vc _ d e v ;/ /G l o b a lv a r i a b l ef o rt h ec h a r a c t e rd e v i c es t r u c t u r e s t a t i cs t r u c tc l a s s* c l ;/ /G l o b a lv a r i a b l ef o rt h ed e v i c ec l a s s s t a t i ci n tm y _ o p e n ( s t r u c ti n o d e* i ,s t r u c tf i l e* f ) {

www.linuxforu.com/2011/04/character-device-files-creation-operations/

2/5

7/19/13
1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2 8 3 8 4 8 5 8 6 } { } { } { } {

Device Drivers, Part 5: Character Device Files -- Creation & Operations - LINUX For You
p r i n t k ( K E R N _ I N F O" D r i v e r :o p e n ( ) \ n " ) ; r e t u r n0 ; s t a t i ci n tm y _ c l o s e ( s t r u c ti n o d e* i ,s t r u c tf i l e* f ) p r i n t k ( K E R N _ I N F O" D r i v e r :c l o s e ( ) \ n " ) ; r e t u r n0 ; s t a t i cs s i z e _ tm y _ r e a d ( s t r u c tf i l e* f ,c h a r_ _ u s e r* b u f ,s i z e _ t l e n ,l o f f _ t* o f f ) p r i n t k ( K E R N _ I N F O" D r i v e r :r e a d ( ) \ n " ) ; r e t u r n0 ; s t a t i cs s i z e _ tm y _ w r i t e ( s t r u c tf i l e* f ,c o n s tc h a r_ _ u s e r* b u f , s i z e _ tl e n ,l o f f _ t* o f f ) p r i n t k ( K E R N _ I N F O" D r i v e r :w r i t e ( ) \ n " ) ; r e t u r nl e n ; s t a t i cs t r u c tf i l e _ o p e r a t i o n sp u g s _ f o p s=

. o w n e r=T H I S _ M O D U L E , . o p e n=m y _ o p e n , . r e l e a s e=m y _ c l o s e , . r e a d=m y _ r e a d , . w r i t e=m y _ w r i t e } ; s t a t i ci n t_ _ i n i to f c d _ i n i t ( v o i d )/ *C o n s t r u c t o r* / { p r i n t k ( K E R N _ I N F O" N a m a s k a r :o f c dr e g i s t e r e d " ) ; i f( a l l o c _ c h r d e v _ r e g i o n ( & f i r s t ,0 ,1 ," S h w e t a " )<0 ) { r e t u r n1 ; } i f( ( c l=c l a s s _ c r e a t e ( T H I S _ M O D U L E ," c h a r d r v " ) )= =N U L L ) { u n r e g i s t e r _ c h r d e v _ r e g i o n ( f i r s t ,1 ) ; r e t u r n1 ; } i f( d e v i c e _ c r e a t e ( c l ,N U L L ,f i r s t ,N U L L ," m y n u l l " )= =N U L L ) { c l a s s _ d e s t r o y ( c l ) ; u n r e g i s t e r _ c h r d e v _ r e g i o n ( f i r s t ,1 ) ; r e t u r n1 ; } c d e v _ i n i t ( & c _ d e v ,& p u g s _ f o p s ) ; i f( c d e v _ a d d ( & c _ d e v ,f i r s t ,1 )= =1 ) { d e v i c e _ d e s t r o y ( c l ,f i r s t ) ; c l a s s _ d e s t r o y ( c l ) ; u n r e g i s t e r _ c h r d e v _ r e g i o n ( f i r s t ,1 ) ; r e t u r n1 ; } r e t u r n0 ; } s t a t i cv o i d_ _ e x i to f c d _ e x i t ( v o i d )/ *D e s t r u c t o r* / { c d e v _ d e l ( & c _ d e v ) ; d e v i c e _ d e s t r o y ( c l ,f i r s t ) ; c l a s s _ d e s t r o y ( c l ) ; u n r e g i s t e r _ c h r d e v _ r e g i o n ( f i r s t ,1 ) ; p r i n t k ( K E R N _ I N F O" A l v i d a :o f c du n r e g i s t e r e d " ) ; } m o d u l e _ i n i t ( o f c d _ i n i t ) ; m o d u l e _ e x i t ( o f c d _ e x i t ) ; M O D U L E _ L I C E N S E ( " G P L " ) ; M O D U L E _ A U T H O R ( " A n i lK u m a rP u g a l i a< e m a i l _ a t _ s a r i k a p u g s _ d o t _ c o m > " ) ; M O D U L E _ D E S C R I P T I O N ( " O u rF i r s tC h a r a c t e rD r i v e r " ) ;

Shweta repeated the usual build process, with some new test steps, as follows: 1. Build the driver (. k ofile) by running m a k e . 2. Load the driver using i n s m o d . 3. List the loaded modules using l s m o d . 4. List the major number allocated, using c a t/ p r o c / d e v i c e s . 5. null driver-specific experiments (refer to Figure 2 for details). 6. Unload the driver using r m m o d .

www.linuxforu.com/2011/04/character-device-files-creation-operations/

3/5

7/19/13

Device Drivers, Part 5: Character Device Files -- Creation & Operations - LINUX For You

Figure 2: 'null driver' experiments

Summing up
Shweta was certainly happy; all on her own, shed got a character driver written, which works the same as the standard / d e v / n u l ldevice file. To understand what this means, check the
< m a j o r ,m i n o r >tuple for / d e v / n u l l , and similarly, also try out the e c h oand c a tcommands

with it. However, one thing began to bother Shweta. She had got her own calls (m y _ o p e n ,m y _ c l o s e , m y _ r e a d ,m y _ w r i t e ) in her driver, but wondered why they worked so unusually, unlike any regular file system calls. What was unusual? Whatever was written, she got nothing when reading unusual, at least from the regular file operations perspective. How would she crack this problem? Watch out for the next article.

Related Posts:
Device Drivers, Part 6: Decoding Character Device File Operations Device Drivers, Part 4: Linux Character Drivers Device Drivers, Part 7: Generic Hardware Access in Linux Device Drivers, Part 9: I/O Control in Linux Device Drivers, Part 13: Data Transfer to and from USB Devices
Tags: character drivers, devfs, device drivers, device model, driver functions, kernel developers, LFY April 2011, linux device drivers, Linux Device Drivers Series, null driver, udev, VFS

Article written by:


Anil Kumar Pugalia
The author is a freelance trainer in Linux internals, Linux device drivers, embedded Linux and related topics. Prior to this, he had worked at Intel and Nvidia. He has been exploring Linux since 1994. A gold medallist from the Indian Institute of Science, Linux and knowledge-sharing are two of his many passions. Connect with him: Website - Twitter - Facebook - Google+

Previous Post

Next Post

Kernel Debugging Using Kprobe and Jprobe

Better Queries with MySQL, Part 3: The MyISAM Storage Engine

www.linuxforu.com/2011/04/character-device-files-creation-operations/

4/5

7/19/13
Reviews How-Tos

Device Drivers, Part 5: Character Device Files -- Creation & Operations - LINUX For You
Coding Interviews Features Overview Blogs

Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems

For You & Me Developers Sysadmins Open Gurus CXOs Columns

All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.

www.linuxforu.com/2011/04/character-device-files-creation-operations/

5/5

Das könnte Ihnen auch gefallen