我正在为封闭源代码的封闭(无硬件访问)设备制作 Linux 模块(我的第一个)。使用 Wireshark 我找到了要发送的内容和发送位置,所以我在用户空间 Python 中尝试了以下命令并且它有效:
handle._controlTransfer(0x21, 0x09, 0x0300, 3, data, 8, 0) #packet sent, device reacts
遗憾的是,我无法让它在内核模块中工作:
usb_control_msg(dev, usb_sndctrlpipe(dev, 0), 0x09, 0x21, 0x0300, 3, data, 8, 0); //packet sent, device doesn't react
即使是自己创建 urb:
urb = usb_alloc_urb(0, GFP_KERNEL);
cr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
cr->bRequest = 0x09;
cr->wValue = cpu_to_le16(0x0300);
cr->wIndex = cpu_to_le16(3);
cr->wLength = cpu_to_le16(8);
usb_fill_control_urb(urb, dev, usb_sndctrlpipe(dev, 0), (void *)cr, data, 8, urb_callback, context);
usb_submit_urb(urb, GFP_ATOMIC);
kfree(cr);
usb_free_urb(urb);
//packet sent, device doesn't react
当我说设备“反应”时,我的意思是它以另一种颜色闪烁,因此命令成功没有歧义。
我用 Wireshark 记录了所有内容并且数据包是相同的(除了urb_id和urb_ts_*)
你对我有什么建议吗?我忘记的东西或如何调试更多?
编辑
完整的 Python 代码
context = usb1.USBContext()
handle = context.openByVendorIDAndProductID(VENDOR_ID, PRODUCT_ID)
usb1.libusb1.libusb_set_auto_detach_kernel_driver(handle._USBDeviceHandle__handle, 0)
usb1.libusb1.libusb_set_auto_detach_kernel_driver(handle._USBDeviceHandle__handle, 3)
handle.claimInterface(0)
handle.claimInterface(3)
handle._controlTransfer(0x21, 0x09, 0x0300, 3, data, 8, 0)
handle.releaseInterface(3)
handle.releaseInterface(0)