Linux设备驱动之USB网卡驱动程序

本文探讨Linux设备驱动中的USB网卡驱动程序,重点介绍了使用select函数来等待文件描述词状态变化的方法,该函数在处理设备驱动时起着关键作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、select函数
定义函数 int select(int n,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout);
函数说明 select()用来等待文件描述词状态的改变。参数n代表最大的文件描述词加1,参数readfds、writefds 和exceptfds 称为描述词组,是用来回传该描述词的读,写或例外的状况。底下的宏提供了处理这三种描述词组的方式:

- FD_CLR(inr fd,fd_set* set);用来清除描述词组set中相关fd 的位
- FD_ISSET(int fd,fd_set *set);用来测试描述词组set中相关fd 的位是否为真
- FD_SET(int fd,fd_set*set);用来设置描述词组set中相关fd的位
- FD_ZERO(fd_set *set); 用来清除描述词组set的全部位

参数 timeout为结构timeval,用来设置select()的等待时间,其结构定义如下

struct timeval
{
   
   
time_t tv_sec;
time_t tv_usec;
};

```c
返回值 如果参数timeout设为NULL则表示select()没有timeout


通过模拟手机wifi模式,写一个Linux下wifi的驱动程序
摘取重要部分函数:

```c
/*
 * WPA Supplicant - command line interface for wpa_supplicant daemon
 * Copyright (c) 2004-2012, Jouni Malinen <[email protected]>
 *
 * This software may be distributed under the terms of the BSD license.
 * See README for more details.
 */



static int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
{
   
   
    return _wpa_ctrl_command(ctrl, cmd, 0);
}




static int wpa_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args,
               int argc, char *argv[])
{
   
   
    char buf[256];
    if (argc < min_args) {
   
   
        printf("Invalid %s command - at least %d argument%s "
               "required.\n", cmd, min_args,
               min_args > 1 ? "s are" : " is");
        return -1;
    }
    if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
        return -1;
    return wpa_ctrl_command(ctrl, buf);
}





/*WPA命令请求函数*/
static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
{
   
   
    struct wpa_cli_cmd *cmd, *match = NULL;
    int count;
    int ret = 0;

    count = 0;
    cmd = wpa_cli_commands;
    while (cmd->cmd) {
   
   
        if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
        {
   
   
            match = cmd;
            if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
   
   
                /* we have an exact match */
                count = 1;
                break;
            }
            count++;
        }
        cmd++;
    }

    if (count > 1) {
   
   
        printf("Ambiguous command '%s'; possible commands:", argv[0]);
        cmd = wpa_cli_commands;
        while (cmd->cmd) {
   
   
            if (os_strncasecmp(cmd->cmd, argv[0],
                       os_strlen(argv[0])) == 0) {
   
   
                printf(" %s", cmd->cmd);
            }
            cmd++;
        }
        printf("\n");
        ret = 1;
    } else if (count == 0) {
   
   
        printf("Unknown command '%s'\n", argv[0]);
        ret = 1;
    } else {
   
   
        ret = match->handler(ctrl, argc - 1, &argv[1]);
    }

    return ret;
}


#define CFG_MAXARGS 10
/*行解析函数,从U-boot中提取的这个函数,返回的是该行命令有的字段的个数,以空格和tab为分界,进行提取*/
static int parse_line (char *line, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值