Red de conocimiento informático - Conocimiento del nombre de dominio - Cómo crear un nodo de proceso en el controlador de Linux

Cómo crear un nodo de proceso en el controlador de Linux

1. Defina las funciones de lectura y escritura del nodo proc

static int tp_switch_writeproc(struct file *file,const char *buffer,

unsigned long count. ,void *data)

{

sscanf(buffer,"%d", &tp_dbg);

printk("tpd: proc-->tp_dbg = %d \n", tp_dbg);

recuento de retornos;

}

static int tp_switch_readproc(char *página, char **inicio, off_t off,

int count,int *eof, void *data)

{

int len;

unsigned char tmp =tp_dbg&0x0F;

len = sprintf(page,"%c\n", tmp);

return 2;

}

2. cargando el controlador Crea la entrada del nodo proc

#include

static struct proc_dir_entry *tp_root

static struct proc_dir_entry * debug_entry;

#define USER_ROOT_DIR "tp_debug"

#define USER_ENTRY1 "debug_switch"

staticint goodix_ts_probe(struct i2c_client *cliente, const struct i2c_device_id *id)

{

… …

init_debug_port(); //Crear nodo de depuración de proceso

… …

}

static int init_debug_port(void)

{

pt_root =proc_mkdir(USER_ROOT_DIR, NULL

if (NULL==pt_root); )

{

printk(KERN_ALERT"¡Crear directorio /proc/%s error!\n", USER_ROOT_DIR

return -1; >

}

printk(KERN_INFO"Create dir /proc/%s\n", USER_ROOT_DIR

// Crea una entrada de prueba en USER_RO);

OT_DIR

pt_entry1 =create_proc_entry(USER_ENTRY1, 0666, pt_root);

if (NULL ==pt_entry1)

{

printk( KERN_ALERT"¡Crear entrada %s bajo /proc/%s error!\n",

USER_ENTRY1,USER_ROOT_DIR

goto err_out; p>

pt_entry1->write_proc= tp_switch_writeproc;

pt_entry1->read_proc =tp_switch_readproc;

printk(KERN_INFO"Crear /proc/%s/%s\n",

USER_ROOT_DIR,USER_ENTRY1);

devuelve 0;

err_out:

pt_entry1->read_proc =NULL; p> pt_entry1->write_proc= NULL;

remove_proc_entry(USER_ROOT_DIR,pt_root);

devuelve -1; , elimine el nodo proc al desinstalar el controlador

static void remove_debug_port(void)

{

remove_proc_entry(USER_ENTRY1,pt_root

remove_proc_entry (USER_ROOT_DIR,NULL);

}

static int goodix_ts_remove(struct i2c_client *cliente)

{

… …

remove_debug_port();

… …

}