linux - Linux下如何写入CM108芯片的GPIO管脚?

标签 linux hid

C-Media 的 CM108 有 4 个 GPIO 引脚,您可以通过隐藏接口(interface)访问这些引脚。

使用 Windows 中的通用写入功能,我能够写入 gpio 引脚。

但是我试图在 Linux 中做同样的事情但没有成功。

linux 内核将设备检测为 hidraw 设备。

注意:我能够从设备读取,但不能写入。 (我以 root 身份运行该应用程序只是为了确保这不是权限问题)。

最佳答案

我成功了,方法如下。

我需要创建一个新的 linux hid 内核模块。 (没那么难)/*

/*
 * Driver for the C-Media 108 chips
 *
 * Copyright (C) 2009 Steve Beaulac <steve@sagacity.ca>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2.
 */

/*
 * This driver is based on the cm109.c driver
 */

#include <linux/device.h>
#include <linux/hid.h>
#include <linux/module.h>

#define DRIVER_VERSION "20090526"
#define DRIVER_AUTHOR "Steve Beaulac"
#define DRIVER_DESC "C-Media 108 chip"

#define CM108_VENDOR_ID 0x0d8c
#define CM108_PRODUCT_ID 0x000c

#ifdef CONFIG_USB_DYNAMIC_MINORS
#define CM108_MINOR_BASE 0
#else
#define CM108_MINOR_BASE 96
#endif

/*
 * Linux interface and usb initialisation
 */

static int cm108_hid_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
    int ret;

    ret = hid_parse(hdev);
    if (ret) {
        dev_err(&hdev->dev, "parse failed\n");
        goto error;
    }

    ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
    if (ret) {
        dev_err(&hdev->dev, "hw start failed\n");
        goto error;
    }
    return 0;

error:
    return ret;
}

static struct hid_device_id cm108_device_table[] = {
    { HID_USB_DEVICE (CM108_VENDOR_ID, CM108_PRODUCT_ID) },
    /* you can add more devices here with product ID 0x0008 - 0x000f */
    { }
};
MODULE_DEVICE_TABLE (hid, cm108_device_table);

static struct hid_driver hid_cm108_driver = {
    .name = "cm108",
    .id_table = cm108_device_table, 
    .probe = cm108_hid_probe,
};

static int hid_cm108_init(void)
{
    return hid_register_driver(&hid_cm108_driver);
}

static void hid_cm108_exit(void)
{
    hid_unregister_driver(&hid_cm108_driver);   
}

module_init(hid_cm108_init);
module_exit(hid_cm108_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

使用了这个makefile

obj-m += cm108.o

并编译模块

make -C /lib/modules/`uname -r`/build/ M=`pwd` EXTRAVERSION="-generic" modules

sudo make -C /lib/modules/`uname -r`/build/ M=`pwd` EXTRAVERSION="-generic" modules_install

depmod -a

我必须修改 modules.order 文件,以便我的模块在通用 hid linux 模块之前被查询。

此模块确保 hidraw 使用接口(interface) 2。

然后我就可以使用fopen来读写CM108芯片的GPIO引脚了。

顺便说一句: 写入时,您需要写入 5 个字节,第一个字节用于 HID_OUTPUT_REPORT

关于linux - Linux下如何写入CM108芯片的GPIO管脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/899008/

相关文章:

linux - 使用 gphoto2 捕获图像时如何创建校验和

python - 从 Totem 应用程序获取当前时间点

Android 手机作为 USB 键盘/HID-Gadget/

barcode-scanner - HID 设备在编程时如何工作?

linux - 如何打印单个 ASCII 字符?

linux - 使用 gdbus 实用程序将对象作为参数传递

linux - bash,搜索 USB 存储设备。输出位置

java - 假设您有 IClass 卡的 UID,则读取卡上的卡号

c++ - 与 MinGW (v.4.3.0) 和 libhid 的链接问题

c - 如何在 GadgetFS 中指定 HID 报告