usb function 创建一个设备<二>

1、框架

opts:options缩写,存放与设备相关的选项字段,存放在此处
struct f_usb_function_device_opts {
	struct list_head list;
	struct usb_function_instance inst;
	struct class *cls;
	size_t bufsize;
	dev_t dev;
}
/*
 list:保存新创建的设备
 inst:功能实例
 cls:该功能创建的设备所属类别
 dev:设备主ID
*/

struct usb_function_device {
	struct config_group group
	struct list_head list;
	char name[32];
	bool is_ready;
}
/*
group:保存相关属性
list:设备保存链表
name:设备名
*/

struct f_usb_function_device_port {
	struct usb_ep *ep_in, *ep_out;
}

struct f_usb_function_device {
	struct usb_function func;
	struct f_usb_device_opts *opts;
	struct f_usb_device_port port;
	bool bound;
}

2、usb_function_instance实现

static void f_usb_function_device_instance_free(struct usb_function_instance *fi)
{
	struct f_usb_function_device_opts *opts = container_of(fi, struct f_usb_function_device_opts, inst);
	if (!list_empty(&opts->list)) {
		printk("bug:sub device not empty\n");
		return;
	}
	destroy_class(opts->cls);
	unregister_chrdev_region(opts->dev, 255);
	kfree(opts);
}
static struct usb_function_instance *f_usb_function_device_alloc_inst(void)
{
	struct f_usb_function_device_opts *opts;
	int ret;
	
	opts = kzalloc(sizeof(*opts), GFP_KERNEL);
	if (!opts)
		return ERR_PTR(-ENOMEM);
	
	INIT_LIST_HEAD(&opts->list);
	
	opts->inst.free_func_inst = f_usb_function_device_instance_free;
	
	opts->cls = class_create(THIS_MODULE, "usb_function");
	if (IS_ERR(opts->cls)) {
		void *r = opts->cls;
		kfree(opts);
		return r;
	}
	
	ret = alloc_chrdev_region(&opts->dev, 255, "usb_function");
	if (ret)
		goto out;

	config_group_init_type_name(&opts->inst.group, "", &f_usb_function_root_type);
out:
	destroy_class(opts->cls);
	kfree(opts);
	return ERR_PTR(ret);
}

/*
	之所以将子设备的链表放到opts里是因为在后期创建子设备回调函数中可以直接使用container_of拿到。这样就能将新创建出来的设备保存到opts里的list中。
	该函数在usb_gadget的functions文件夹中执行mkdir usb_function.usb0时被调用。
在此函数中可以先将dev id,class这些在后期创建设备时需要的参数。
	f_usb_function_root_type:该参数是此usb function的属性类型
*/

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是使用 Java 编写的,用于在 Android 应用程序中获取 USBManager 对象、遍历已连接的 USB 设备、获取设备的 VID 和 PID,并请求 USB 权限。以下是相应的 JavaScript 代码示例: ```javascript // 请求连接到 USB 设备 async function requestDevice() { try { const usbManager = navigator.usb; // 获取已连接的 USB 设备列表 const deviceList = await usbManager.getDevices(); // 遍历设备列表 for (const device of deviceList) { const vid = device.vendorId; const pid = device.productId; // 在这里可以根据 VID 和 PID 进行相应的逻辑处理 // 请求 USB 权限 await device.open(); if (device.configuration === null) { await device.selectConfiguration(1); } await device.claimInterface(0); // USB 权限已授权,可以进行相应的操作 // ... // 释放设备和接口 await device.releaseInterface(0); await device.close(); } } catch (error) { console.error('Error:', error); } } // 在按钮点击时请求连接到 USB 设备 const connectButton = document.getElementById('connectButton'); connectButton.addEventListener('click', requestDevice); ``` 上述代码使用了 WebUSB API 中的 `navigator.usb` 接口来获取 USBManager 对象,并使用 `navigator.usb.getDevices()` 方法获取已连接的 USB 设备列表。然后,我们可以通过遍历设备列表来获取每个设备的 VID 和 PID。在请求 USB 权限后,可以进行相应的操作。 请注意,WebUSB API 目前仅在部分浏览器中得到支持。在使用 WebUSB API 时,需要在页面中请求用户授权才能访问 USB 设备。详细的 WebUSB API 使用方法请参考相关文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值