本章主要是基于之前的学习,实现一个hidraw的驱动,发现有两种用于识别usb设备的方式,放别是usb_device_id和hid_device_id
hid_probe
(1)hid_device_id
kernel/msm-4.19/drivers/hid/usbhid/hid-core.c
bus = usb usb_register 注册驱动 -> sys/bus/usb/driver
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
kernel/msm-4.19/drivers/hid/hid-core.c
bus = hid hid_register_driver 注册驱动 -> sys/bus/hid/driver
hid bus本身没有探测的能力,都是需要其他模块来进行hid_add_device,
(2)usb_device_id
kernel/msm-4.19/drivers/hid/usbhid/usbmouse.c
static const struct usb_device_id usb_mouse_id_table[] = {
USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
USB_INTERFACE_PROTOCOL_MOUSE)
usb探测的过程如下
usb_new_device -> usb_enumerate_device + announce_device + device_add -> bus_probe_device -> device_initial_probe
-> driver_match_device -> usb_device_match (driver.c)-> usb_match_id (usb_device_id usbmouse.c)
-> driver_probe_device -> really_probe -> usb_mouse_probe -> input_register_device(usbmouse.c)
-> usbhid_probe (usbhid/hid-core.c)-> hid_add_device -> device_add
-> driver_match_device -> hid_bus_match (hid/hid-core.c) -> hid_match_id (hid_device_id hid-generic.c)
-> driver_probe_device -> hid_device_probe(hid/hid-core.c) -> hid_generic_probe (hid/hid-generic.c)
issues
E hidraw 0003: 0C2E:1007.0001: device has no listeners, quitting
.raw_event = hidraw_raw_event, //hidwar必须要实现这个函数
CONFIG_HIDRAW=y //需要打开这个函数,不然会调用hidraw.h中的空函数
hidraw_demo
在android的源码中没有只有hidraw的驱动,只有一些定义在hidraw.c中的api, 通过这些参数实现了hidraw接口的功能并测试通过
/* Copyright (c) 2014-2014, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/hid.h>
#include <linux/hid-debug.h>
#include "hid-ids.h"
#include "usbhid/usbhid.h"
#include <linux/usb.h>
#include <linux/vmalloc.h>
#include <linux/completion.h>
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/hid.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/sched/signal.h>
#include <linux/hidraw.h>
#define hidtype 0//1:use clamied , 0: use connect_mask
struct hidraw_data {
struct hid_device *hdev;
};
static int hidraw_raw_event(struct hid_device *hdev,
struct hid_report *report, u8 *raw_data, int size)
{
struct hidraw_data *data = hid_get_drvdata(hdev);
int ret = 0;
if (!data)
return 1;
hidraw_report_event(hdev, raw_data, size);
return ret;
}
#ifdef CONFIG_PM
static int hidraw_suspend(struct hid_device *hdev, pm_message_t message)
{
return 0;
}
static int hidraw_resume(struct hid_device *hdev)
{
return 0;
}
static int hidraw_reset_resume(struct hid_device *hdev)
{
return 0;
}
#endif
static int hidraw_probe(struct hid_device *hdev,
const struct hid_device_id *id)
{
struct hidraw_data *data;
int error = -ENOMEM;
struct usbhid_device *usbhid = hdev->driver_data;
if(usbhid->ifnum != 0) {
hid_err(hdev, "usbhid ifnum = %d\n", usbhid->ifnum);
return error;
}
//hidraw_init();
data = kzalloc(sizeof(struct hidraw_data), GFP_KERNEL);
if (data == NULL) {
hid_err(hdev, "failed to kzallocc \n");
error = -ENOMEM;
goto err_kzalloc;
}
data->hdev = hdev;
hid_set_drvdata(hdev, data);
error = hid_parse(hdev);
if (error) {
hid_err(hdev, "failed to parse \n");
goto err_kzalloc;
}
hdev->quirks = HID_QUIRK_NO_INIT_REPORTS;
#if hidtype
hdev->claimed = HID_CLAIMED_HIDRAW;
error = hid_hw_start(hdev, 0);
#else
error = hid_hw_start(hdev, HID_CONNECT_HIDRAW );
#endif
if (error) {
hid_err(hdev, "failed to hw start\n");
goto err_drvdata_null;
}
#if hidtype
hidraw_connect(hdev);
#endif
return 0;
err_kzalloc:
kfree(data);
err_drvdata_null:
hid_set_drvdata(hdev, NULL);
return error;
}
static void hidraw_remove(struct hid_device *hdev)
{
struct hidraw_data *data = hid_get_drvdata(hdev);
hid_hw_stop(hdev);
#if hidtype
hidraw_disconnect(hdev);
#endif
hid_set_drvdata(hdev, NULL);
//hidraw_exit();
kfree(data);
}
static const struct hid_device_id hidraw_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_xxx, USB_DEVICE_ID_xxx) },
{ }
};
MODULE_DEVICE_TABLE(hid, hidraw_devices);
static struct hid_driver hidraw_driver = {
.name = "hid-own",
.id_table = hidraw_devices,
.probe = hidraw_probe,
.remove = hidraw_remove,
.raw_event = hidraw_raw_event,
#ifdef CONFIG_PM
.suspend = hidraw_suspend,
.resume = hidraw_resume,
.reset_resume = hidraw_reset_resume,
#endif
};
//module_hid_driver(hidraw_driver);
static int __init hidown_init(void)
{
int result;
hidraw_init();
result = hid_register_driver(&hidraw_driver);
return result;
}
static void __exit hidown_exit(void)
{
hid_unregister_driver(&hidraw_driver);
hidraw_exit();
}
module_init(hidown_init);
module_exit(hidown_exit);
MODULE_DESCRIPTION("Hid Raw Demo");
MODULE_LICENSE("GPL v2");
标签:hdev,struct,Driver,hid,device,Qcom,Android,include,hidraw
From: https://www.cnblogs.com/linhaostudy/p/18382979