dev_t devno;
- o! b# D7 J# f7 P" B. Y+ x& ~ ' s1 o* b, n# P2 C0 Y; r
struct file_operations hello_fops = {7 N8 |# E( t% L; `( @$ {; E
, Y- c' ?! B5 k, s1 R5 o7 y+ ~! C .owner = THIS_MODULE
5 H2 G1 d, t/ a M) u" Q; r9 l
7 X! A% N) B& p };/ M, j% h& F$ v' F. @
* l0 _8 U- {# c& x& P% i6 ~* Z static int __init hello_init (void)
0 s/ R: ^9 x! b6 ]9 ]8 }% z6 M
1 O1 O8 X- B- ]5 }9 W6 C4 [2 Q% _ {
' r$ p j5 [* P: _' V2 {- o$ b
+ M- c0 B7 Q' H# w4 ~ int result;7 }' V( ~- p. i- R
3 ?6 f- J. g9 M4 j" Z
devno = MKDEV(HELLO_MAJOR, HELLO_MINOR);, d; S3 e9 z+ w
4 Y% m4 a' @8 I/ D if (HELLO_MAJOR)
* o$ \, r2 q! c' Z; k I 2 a/ n9 k7 M: T4 i' s5 Y
result = register_chrdev_region(devno, 2, "memdev");; I1 H8 T# a$ ~! U2 V2 c+ n2 d
8 s" S( k. ?0 V# O5 l' O' N5 j else
8 ?1 n& R, J6 s. w" Z
( m: F- x9 e; ?% Z4 r' W; M0 P {
: e" {' E, i2 Q# g9 w4 {: z/ w, ^& G
* s* _) R+ M: a+ z result = alloc_chrdev_region(&devno, 0, 2, "memdev");
5 c) Y* [3 `. p' l5 E1 ~! M# o% K3 v ' i& g* F( x: [0 F
HELLO_MAJOR = MAJOR(devno);9 u y9 n4 X! M8 n9 y
- y% f) A4 [" C6 e }
* q- j, Q( x2 l! I( I. f
7 Y% j& r& S6 z# D. e: ? printk("MAJOR IS %d\n",HELLO_MAJOR);
) V$ E. r0 h- }/ @$ L' ` + F; o& T- s0 g* g7 V3 ?; Z
my_class = class_create(THIS_MODULE,"hello_char_class"); //类名为hello_char_class
. l4 r+ O) x0 R% J0 o( F- w" k0 N ' \+ A \ l$ d; k" r; C+ S
if(IS_ERR(my_class))
& E6 }- i4 |: k" @: O
0 Y3 V! \; Q8 y* |; g {8 O: [3 z' j, r, o* H8 H* p2 m
& ?" K, L9 V1 J! b printk("Err: failed in creating class.\n");
3 i& s) P5 R2 r( ^, y, y8 @ 4 G: ^* a% l0 N: ?) g
return -1;
( l' ]% ], l+ W& o6 H8 H8 I* B ) G2 u/ ~0 j! a8 V! N
}
8 j" L: Y4 [# \ L8 a m5 n
8 {8 q( ^! h! M" C7 x device_create(my_class,NULL,devno,NULL,"memdev"); //设备名为memdev0 J6 b$ j- N2 q
; p* a9 t3 q; C% p! J4 b* E
if (result |