19.02.2015 Views

An Introduction to Linux Device Driver Development - Computer ...

An Introduction to Linux Device Driver Development - Computer ...

An Introduction to Linux Device Driver Development - Computer ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Labora<strong>to</strong>ry Exercise #6 3<br />

(b) Navigate <strong>to</strong> the ‘modules’ direc<strong>to</strong>ry and ensure the ‘Makefile’ points <strong>to</strong> the ‘linux-2.6.35.7’<br />

kernel direc<strong>to</strong>ry.<br />

(c) Create a new kernel module source file called ‘multiply.c’.<br />

(d) Copy the following skele<strong>to</strong>n code in<strong>to</strong> ‘multiply.c’:<br />

# i n c l u d e / ∗ Needed by a l l modules ∗ /<br />

# i n c l u d e / ∗ Needed f o r KERN ∗ and p r i n t k ∗ /<br />

# i n c l u d e / ∗ Needed f o r i n i t and e x i t macros ∗ /<br />

# i n c l u d e / ∗ Needed f o r IO r e a d s and w r i t e s ∗ /<br />

# i n c l u d e ” x p a r a m e t e r s . h ” / ∗ needed f o r p h y s i c a l a d d r e s s o f m u l t i p l i e r ∗ /<br />

/ ∗ from x p a r a m e t e r s . h ∗ /<br />

# d e f i n e PHY ADDR XPAR MULTIPLIER 0 BASEADDR ; / / p h y s i c a l a d d r e s s o f m u l t i p l i e r<br />

/ ∗ s i z e o f p h y s i c a l a d d r e s s range f o r m u l t i p l y ∗ /<br />

# d e f i n e MEMSIZE XPAR MULTIPLIER 0 HIGHADDR − XPAR MULTIPLIER 0 BASEADDR+1;<br />

unsigned v i r t a d d r ; / / v i r t u a l a d d r e s s p o i n t i n g t o m u l t i p l i e r<br />

/ ∗ T h i s f u n c t i o n i s run upon module load . T h i s i s where you s e t u p data<br />

s t r u c t u r e s and r e s e r v e r e s o u r c e s used by t h e module . ∗ /<br />

s t a t i c i n t i n i t m y i n i t ( void )<br />

{<br />

/ ∗ <strong>Linux</strong> k e r n e l ’ s v e r s i o n o f p r i n t f ∗ /<br />

p r i n t k ( KERN INFO ” Mapping v i r t u a l a d d r e s s . . . \ n ” ) ;<br />

/ ∗ map v i r t u a l a d d r e s s t o m u l t i p l i e r p h y s i c a l a d d r e s s ∗ /<br />

/ / use ioremap<br />

/ ∗ w r i t e 7 t o r e g i s t e r 0 ∗ /<br />

p r i n t k ( KERN INFO ” W r i t i n g a 7 t o r e g i s t e r 0\n ” ) ;<br />

i o w r i t e 3 2 ( 7 , v i r t a d d r + 0 ) ; / / base a d d r e s s + o f f s e t<br />

/ ∗ W r i t e 2 t o r e g i s t e r 1 ∗ /<br />

p r i n t k ( KERN INFO ” W r i t i n g a 2 t o r e g i s t e r 1\n ” ) ;<br />

/ / use i o w r i t e 3 2<br />

p r i n t k ( ” Read %d from r e g i s t e r 0\n ” , i o r e a d 3 2 ( v i r t a d d r + 0 ) ) ;<br />

p r i n t k ( ” Read %d from r e g i s t e r 1\n ” , \\ use i o r e a d 3 2 ) ;<br />

p r i n t k ( ” Read %d from r e g i s t e r 2\n ” , i o r e a d 3 2 ( v i r t a d d r + 8 ) ) ;<br />

/ / A non 0 r e t u r n means i n i t m o d u l e f a i l e d ; module can ’ t be loaded .<br />

return 0 ;<br />

}<br />

/ ∗ T h i s f u n c t i o n i s run j u s t p r i o r t o t h e module ’ s removal from t h e<br />

s y s t e m . You s h o u l d r e l e a s e ALL r e s o u r c e s used by your module<br />

h ere ( o t h e r w i s e be p r e p a r e d f o r a r e b o o t ) . ∗ /<br />

s t a t i c void e x i t m y e x i t ( void )<br />

ECEN 449 3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!